1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.43.1. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite. To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library. (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file. Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19**
20** The content in this amalgamation comes from Fossil check-in
21** d3a40c05c49e1a49264912b1a05bc2143ac.
22*/
23#define SQLITE_CORE 1
24#define SQLITE_AMALGAMATION 1
25#ifndef SQLITE_PRIVATE
26# define SQLITE_PRIVATE static
27#endif
28/************** Begin file sqliteInt.h ***************************************/
29/*
30** 2001 September 15
31**
32** The author disclaims copyright to this source code. In place of
33** a legal notice, here is a blessing:
34**
35** May you do good and not evil.
36** May you find forgiveness for yourself and forgive others.
37** May you share freely, never taking more than you give.
38**
39*************************************************************************
40** Internal interface definitions for SQLite.
41**
42*/
43#ifndef SQLITEINT_H
44#define SQLITEINT_H
45
46/* Special Comments:
47**
48** Some comments have special meaning to the tools that measure test
49** coverage:
50**
51** NO_TEST - The branches on this line are not
52** measured by branch coverage. This is
53** used on lines of code that actually
54** implement parts of coverage testing.
55**
56** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
57** and the correct answer is still obtained,
58** though perhaps more slowly.
59**
60** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
61** and the correct answer is still obtained,
62** though perhaps more slowly.
63**
64** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
65** that would be harmless and undetectable
66** if it did occur.
67**
68** In all cases, the special comment must be enclosed in the usual
69** slash-asterisk...asterisk-slash comment marks, with no spaces between the
70** asterisks and the comment text.
71*/
72
73/*
74** Make sure the Tcl calling convention macro is defined. This macro is
75** only used by test code and Tcl integration code.
76*/
77#ifndef SQLITE_TCLAPI
78# define SQLITE_TCLAPI
79#endif
80
81/*
82** Include the header file used to customize the compiler options for MSVC.
83** This should be done first so that it can successfully prevent spurious
84** compiler warnings due to subsequent content in this file and other files
85** that are included by this file.
86*/
87/************** Include msvc.h in the middle of sqliteInt.h ******************/
88/************** Begin file msvc.h ********************************************/
89/*
90** 2015 January 12
91**
92** The author disclaims copyright to this source code. In place of
93** a legal notice, here is a blessing:
94**
95** May you do good and not evil.
96** May you find forgiveness for yourself and forgive others.
97** May you share freely, never taking more than you give.
98**
99******************************************************************************
100**
101** This file contains code that is specific to MSVC.
102*/
103#ifndef SQLITE_MSVC_H
104#define SQLITE_MSVC_H
105
106#if defined(_MSC_VER)
107#pragma warning(disable : 4054)
108#pragma warning(disable : 4055)
109#pragma warning(disable : 4100)
110#pragma warning(disable : 4127)
111#pragma warning(disable : 4130)
112#pragma warning(disable : 4152)
113#pragma warning(disable : 4189)
114#pragma warning(disable : 4206)
115#pragma warning(disable : 4210)
116#pragma warning(disable : 4232)
117#pragma warning(disable : 4244)
118#pragma warning(disable : 4305)
119#pragma warning(disable : 4306)
120#pragma warning(disable : 4702)
121#pragma warning(disable : 4706)
122#endif /* defined(_MSC_VER) */
123
124#if defined(_MSC_VER) && !defined(_WIN64)
125#undef SQLITE_4_BYTE_ALIGNED_MALLOC
126#define SQLITE_4_BYTE_ALIGNED_MALLOC
127#endif /* defined(_MSC_VER) && !defined(_WIN64) */
128
129#if !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800
130#define HAVE_LOG2 0
131#endif /* !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800 */
132
133#endif /* SQLITE_MSVC_H */
134
135/************** End of msvc.h ************************************************/
136/************** Continuing where we left off in sqliteInt.h ******************/
137
138/*
139** Special setup for VxWorks
140*/
141/************** Include vxworks.h in the middle of sqliteInt.h ***************/
142/************** Begin file vxworks.h *****************************************/
143/*
144** 2015-03-02
145**
146** The author disclaims copyright to this source code. In place of
147** a legal notice, here is a blessing:
148**
149** May you do good and not evil.
150** May you find forgiveness for yourself and forgive others.
151** May you share freely, never taking more than you give.
152**
153******************************************************************************
154**
155** This file contains code that is specific to Wind River's VxWorks
156*/
157#if defined(__RTP__) || defined(_WRS_KERNEL)
158/* This is VxWorks. Set up things specially for that OS
159*/
160#include <vxWorks.h>
161#include <pthread.h> /* amalgamator: dontcache */
162#define OS_VXWORKS 1
163#define SQLITE_OS_OTHER 0
164#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
165#define SQLITE_OMIT_LOAD_EXTENSION 1
166#define SQLITE_ENABLE_LOCKING_STYLE 0
167#define HAVE_UTIME 1
168#else
169/* This is not VxWorks. */
170#define OS_VXWORKS 0
171#define HAVE_FCHOWN 1
172#define HAVE_READLINK 1
173#define HAVE_LSTAT 1
174#endif /* defined(_WRS_KERNEL) */
175
176/************** End of vxworks.h *********************************************/
177/************** Continuing where we left off in sqliteInt.h ******************/
178
179/*
180** These #defines should enable >2GB file support on POSIX if the
181** underlying operating system supports it. If the OS lacks
182** large file support, or if the OS is windows, these should be no-ops.
183**
184** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
185** system #includes. Hence, this block of code must be the very first
186** code in all source files.
187**
188** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
189** on the compiler command line. This is necessary if you are compiling
190** on a recent machine (ex: Red Hat 7.2) but you want your code to work
191** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
192** without this option, LFS is enable. But LFS does not exist in the kernel
193** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
194** portability you should omit LFS.
195**
196** The previous paragraph was written in 2005. (This paragraph is written
197** on 2008-11-28.) These days, all Linux kernels support large files, so
198** you should probably leave LFS enabled. But some embedded platforms might
199** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
200**
201** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
202*/
203#ifndef SQLITE_DISABLE_LFS
204# define _LARGE_FILE 1
205# ifndef _FILE_OFFSET_BITS
206# define _FILE_OFFSET_BITS 64
207# endif
208# define _LARGEFILE_SOURCE 1
209#endif
210
211/* The GCC_VERSION and MSVC_VERSION macros are used to
212** conditionally include optimizations for each of these compilers. A
213** value of 0 means that compiler is not being used. The
214** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
215** optimizations, and hence set all compiler macros to 0
216**
217** There was once also a CLANG_VERSION macro. However, we learn that the
218** version numbers in clang are for "marketing" only and are inconsistent
219** and unreliable. Fortunately, all versions of clang also recognize the
220** gcc version numbers and have reasonable settings for gcc version numbers,
221** so the GCC_VERSION macro will be set to a correct non-zero value even
222** when compiling with clang.
223*/
224#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
225# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
226#else
227# define GCC_VERSION 0
228#endif
229#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
230# define MSVC_VERSION _MSC_VER
231#else
232# define MSVC_VERSION 0
233#endif
234
235/*
236** Some C99 functions in "math.h" are only present for MSVC when its version
237** is associated with Visual Studio 2013 or higher.
238*/
239#ifndef SQLITE_HAVE_C99_MATH_FUNCS
240# if MSVC_VERSION==0 || MSVC_VERSION>=1800
241# define SQLITE_HAVE_C99_MATH_FUNCS (1)
242# else
243# define SQLITE_HAVE_C99_MATH_FUNCS (0)
244# endif
245#endif
246
247/* Needed for various definitions... */
248#if defined(__GNUC__) && !defined(_GNU_SOURCE)
249# define _GNU_SOURCE
250#endif
251
252#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
253# define _BSD_SOURCE
254#endif
255
256/*
257** Macro to disable warnings about missing "break" at the end of a "case".
258*/
259#if GCC_VERSION>=7000000
260# define deliberate_fall_through __attribute__((fallthrough));
261#else
262# define deliberate_fall_through
263#endif
264
265/*
266** For MinGW, check to see if we can include the header file containing its
267** version information, among other things. Normally, this internal MinGW
268** header file would [only] be included automatically by other MinGW header
269** files; however, the contained version information is now required by this
270** header file to work around binary compatibility issues (see below) and
271** this is the only known way to reliably obtain it. This entire #if block
272** would be completely unnecessary if there was any other way of detecting
273** MinGW via their preprocessor (e.g. if they customized their GCC to define
274** some MinGW-specific macros). When compiling for MinGW, either the
275** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
276** defined; otherwise, detection of conditions specific to MinGW will be
277** disabled.
278*/
279#if defined(_HAVE_MINGW_H)
280# include "mingw.h"
281#elif defined(_HAVE__MINGW_H)
282# include "_mingw.h"
283#endif
284
285/*
286** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
287** define is required to maintain binary compatibility with the MSVC runtime
288** library in use (e.g. for Windows XP).
289*/
290#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
291 defined(_WIN32) && !defined(_WIN64) && \
292 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
293 defined(__MSVCRT__)
294# define _USE_32BIT_TIME_T
295#endif
296
297/* Optionally #include a user-defined header, whereby compilation options
298** may be set prior to where they take effect, but after platform setup.
299** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
300** file.
301*/
302#ifdef SQLITE_CUSTOM_INCLUDE
303# define INC_STRINGIFY_(f) #f
304# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
305# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
306#endif
307
308/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
309** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
310** MinGW.
311*/
312/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
313/************** Begin file sqlite3.h *****************************************/
314/*
315** 2001-09-15
316**
317** The author disclaims copyright to this source code. In place of
318** a legal notice, here is a blessing:
319**
320** May you do good and not evil.
321** May you find forgiveness for yourself and forgive others.
322** May you share freely, never taking more than you give.
323**
324*************************************************************************
325** This header file defines the interface that the SQLite library
326** presents to client programs. If a C-function, structure, datatype,
327** or constant definition does not appear in this file, then it is
328** not a published API of SQLite, is subject to change without
329** notice, and should not be referenced by programs that use SQLite.
330**
331** Some of the definitions that are in this file are marked as
332** "experimental". Experimental interfaces are normally new
333** features recently added to SQLite. We do not anticipate changes
334** to experimental interfaces but reserve the right to make minor changes
335** if experience from use "in the wild" suggest such changes are prudent.
336**
337** The official C-language API documentation for SQLite is derived
338** from comments in this file. This file is the authoritative source
339** on how SQLite interfaces are supposed to operate.
340**
341** The name of this file under configuration management is "sqlite.h.in".
342** The makefile makes some minor changes to this file (such as inserting
343** the version number) and changes its name to "sqlite3.h" as
344** part of the build process.
345*/
346#ifndef SQLITE3_H
347#define SQLITE3_H
348#include <stdarg.h> /* Needed for the definition of va_list */
349
350/*
351** Make sure we can call this stuff from C++.
352*/
353#if 0
354extern "C" {
355#endif
356
357
358/*
359** Facilitate override of interface linkage and calling conventions.
360** Be aware that these macros may not be used within this particular
361** translation of the amalgamation and its associated header file.
362**
363** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the
364** compiler that the target identifier should have external linkage.
365**
366** The SQLITE_CDECL macro is used to set the calling convention for
367** public functions that accept a variable number of arguments.
368**
369** The SQLITE_APICALL macro is used to set the calling convention for
370** public functions that accept a fixed number of arguments.
371**
372** The SQLITE_STDCALL macro is no longer used and is now deprecated.
373**
374** The SQLITE_CALLBACK macro is used to set the calling convention for
375** function pointers.
376**
377** The SQLITE_SYSAPI macro is used to set the calling convention for
378** functions provided by the operating system.
379**
380** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and
381** SQLITE_SYSAPI macros are used only when building for environments
382** that require non-default calling conventions.
383*/
384#ifndef SQLITE_EXTERN
385# define SQLITE_EXTERN extern
386#endif
387#ifndef SQLITE_API
388# define SQLITE_API
389#endif
390#ifndef SQLITE_CDECL
391# define SQLITE_CDECL
392#endif
393#ifndef SQLITE_APICALL
394# define SQLITE_APICALL
395#endif
396#ifndef SQLITE_STDCALL
397# define SQLITE_STDCALL SQLITE_APICALL
398#endif
399#ifndef SQLITE_CALLBACK
400# define SQLITE_CALLBACK
401#endif
402#ifndef SQLITE_SYSAPI
403# define SQLITE_SYSAPI
404#endif
405
406/*
407** These no-op macros are used in front of interfaces to mark those
408** interfaces as either deprecated or experimental. New applications
409** should not use deprecated interfaces - they are supported for backwards
410** compatibility only. Application writers should be aware that
411** experimental interfaces are subject to change in point releases.
412**
413** These macros used to resolve to various kinds of compiler magic that
414** would generate warning messages when they were used. But that
415** compiler magic ended up generating such a flurry of bug reports
416** that we have taken it all out and gone back to using simple
417** noop macros.
418*/
419#define SQLITE_DEPRECATED
420#define SQLITE_EXPERIMENTAL
421
422/*
423** Ensure these symbols were not defined by some previous header file.
424*/
425#ifdef SQLITE_VERSION
426# undef SQLITE_VERSION
427#endif
428#ifdef SQLITE_VERSION_NUMBER
429# undef SQLITE_VERSION_NUMBER
430#endif
431
432/*
433** CAPI3REF: Compile-Time Library Version Numbers
434**
435** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
436** evaluates to a string literal that is the SQLite version in the
437** format "X.Y.Z" where X is the major version number (always 3 for
438** SQLite3) and Y is the minor version number and Z is the release number.)^
439** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
440** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
441** numbers used in [SQLITE_VERSION].)^
442** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
443** be larger than the release from which it is derived. Either Y will
444** be held constant and Z will be incremented or else Y will be incremented
445** and Z will be reset to zero.
446**
447** Since [version 3.6.18] ([dateof:3.6.18]),
448** SQLite source code has been stored in the
449** <a href="http://www.fossil-scm.org/">Fossil configuration management
450** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
451** a string which identifies a particular check-in of SQLite
452** within its configuration management system. ^The SQLITE_SOURCE_ID
453** string contains the date and time of the check-in (UTC) and a SHA1
454** or SHA3-256 hash of the entire source tree. If the source code has
455** been edited in any way since it was last checked in, then the last
456** four hexadecimal digits of the hash may be modified.
457**
458** See also: [sqlite3_libversion()],
459** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460** [sqlite_version()] and [sqlite_source_id()].
461*/
462#define SQLITE_VERSION "3.43.1"
463#define SQLITE_VERSION_NUMBER 3043001
464#define SQLITE_SOURCE_ID "2023-09-11 12:01:27 2d3a40c05c49e1a49264912b1a05bc2143ac0e7c3df588276ce80a4cbc9bd1b0"
465
466/*
467** CAPI3REF: Run-Time Library Version Numbers
468** KEYWORDS: sqlite3_version sqlite3_sourceid
469**
470** These interfaces provide the same information as the [SQLITE_VERSION],
471** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
472** but are associated with the library instead of the header file. ^(Cautious
473** programmers might include assert() statements in their application to
474** verify that values returned by these interfaces match the macros in
475** the header, and thus ensure that the application is
476** compiled with matching library and header files.
477**
478** <blockquote><pre>
479** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
480** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
481** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
482** </pre></blockquote>)^
483**
484** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
485** macro. ^The sqlite3_libversion() function returns a pointer to the
486** to the sqlite3_version[] string constant. The sqlite3_libversion()
487** function is provided for use in DLLs since DLL users usually do not have
488** direct access to string constants within the DLL. ^The
489** sqlite3_libversion_number() function returns an integer equal to
490** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
491** a pointer to a string constant whose value is the same as the
492** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
493** using an edited copy of [the amalgamation], then the last four characters
494** of the hash might be different from [SQLITE_SOURCE_ID].)^
495**
496** See also: [sqlite_version()] and [sqlite_source_id()].
497*/
498SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
499SQLITE_API const char *sqlite3_libversion(void);
500SQLITE_API const char *sqlite3_sourceid(void);
501SQLITE_API int sqlite3_libversion_number(void);
502
503/*
504** CAPI3REF: Run-Time Library Compilation Options Diagnostics
505**
506** ^The sqlite3_compileoption_used() function returns 0 or 1
507** indicating whether the specified option was defined at
508** compile time. ^The SQLITE_ prefix may be omitted from the
509** option name passed to sqlite3_compileoption_used().
510**
511** ^The sqlite3_compileoption_get() function allows iterating
512** over the list of options that were defined at compile time by
513** returning the N-th compile time option string. ^If N is out of range,
514** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
515** prefix is omitted from any strings returned by
516** sqlite3_compileoption_get().
517**
518** ^Support for the diagnostic functions sqlite3_compileoption_used()
519** and sqlite3_compileoption_get() may be omitted by specifying the
520** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
521**
522** See also: SQL functions [sqlite_compileoption_used()] and
523** [sqlite_compileoption_get()] and the [compile_options pragma].
524*/
525#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
526SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
527SQLITE_API const char *sqlite3_compileoption_get(int N);
528#else
529# define sqlite3_compileoption_used(X) 0
530# define sqlite3_compileoption_get(X) ((void*)0)
531#endif
532
533/*
534** CAPI3REF: Test To See If The Library Is Threadsafe
535**
536** ^The sqlite3_threadsafe() function returns zero if and only if
537** SQLite was compiled with mutexing code omitted due to the
538** [SQLITE_THREADSAFE] compile-time option being set to 0.
539**
540** SQLite can be compiled with or without mutexes. When
541** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
542** are enabled and SQLite is threadsafe. When the
543** [SQLITE_THREADSAFE] macro is 0,
544** the mutexes are omitted. Without the mutexes, it is not safe
545** to use SQLite concurrently from more than one thread.
546**
547** Enabling mutexes incurs a measurable performance penalty.
548** So if speed is of utmost importance, it makes sense to disable
549** the mutexes. But for maximum safety, mutexes should be enabled.
550** ^The default behavior is for mutexes to be enabled.
551**
552** This interface can be used by an application to make sure that the
553** version of SQLite that it is linking against was compiled with
554** the desired setting of the [SQLITE_THREADSAFE] macro.
555**
556** This interface only reports on the compile-time mutex setting
557** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
558** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
559** can be fully or partially disabled using a call to [sqlite3_config()]
560** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
561** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
562** sqlite3_threadsafe() function shows only the compile-time setting of
563** thread safety, not any run-time changes to that setting made by
564** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
565** is unchanged by calls to sqlite3_config().)^
566**
567** See the [threading mode] documentation for additional information.
568*/
569SQLITE_API int sqlite3_threadsafe(void);
570
571/*
572** CAPI3REF: Database Connection Handle
573** KEYWORDS: {database connection} {database connections}
574**
575** Each open SQLite database is represented by a pointer to an instance of
576** the opaque structure named "sqlite3". It is useful to think of an sqlite3
577** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
578** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
579** and [sqlite3_close_v2()] are its destructors. There are many other
580** interfaces (such as
581** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
582** [sqlite3_busy_timeout()] to name but three) that are methods on an
583** sqlite3 object.
584*/
585typedef struct sqlite3 sqlite3;
586
587/*
588** CAPI3REF: 64-Bit Integer Types
589** KEYWORDS: sqlite_int64 sqlite_uint64
590**
591** Because there is no cross-platform way to specify 64-bit integer types
592** SQLite includes typedefs for 64-bit signed and unsigned integers.
593**
594** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
595** The sqlite_int64 and sqlite_uint64 types are supported for backwards
596** compatibility only.
597**
598** ^The sqlite3_int64 and sqlite_int64 types can store integer values
599** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
600** sqlite3_uint64 and sqlite_uint64 types can store integer values
601** between 0 and +18446744073709551615 inclusive.
602*/
603#ifdef SQLITE_INT64_TYPE
604 typedef SQLITE_INT64_TYPE sqlite_int64;
605# ifdef SQLITE_UINT64_TYPE
606 typedef SQLITE_UINT64_TYPE sqlite_uint64;
607# else
608 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
609# endif
610#elif defined(_MSC_VER) || defined(__BORLANDC__)
611 typedef __int64 sqlite_int64;
612 typedef unsigned __int64 sqlite_uint64;
613#else
614 typedef long long int sqlite_int64;
615 typedef unsigned long long int sqlite_uint64;
616#endif
617typedef sqlite_int64 sqlite3_int64;
618typedef sqlite_uint64 sqlite3_uint64;
619
620/*
621** If compiling for a processor that lacks floating point support,
622** substitute integer for floating-point.
623*/
624#ifdef SQLITE_OMIT_FLOATING_POINT
625# define double sqlite3_int64
626#endif
627
628/*
629** CAPI3REF: Closing A Database Connection
630** DESTRUCTOR: sqlite3
631**
632** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
633** for the [sqlite3] object.
634** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
635** the [sqlite3] object is successfully destroyed and all associated
636** resources are deallocated.
637**
638** Ideally, applications should [sqlite3_finalize | finalize] all
639** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
640** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
641** with the [sqlite3] object prior to attempting to close the object.
642** ^If the database connection is associated with unfinalized prepared
643** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
644** sqlite3_close() will leave the database connection open and return
645** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
646** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
647** it returns [SQLITE_OK] regardless, but instead of deallocating the database
648** connection immediately, it marks the database connection as an unusable
649** "zombie" and makes arrangements to automatically deallocate the database
650** connection after all prepared statements are finalized, all BLOB handles
651** are closed, and all backups have finished. The sqlite3_close_v2() interface
652** is intended for use with host languages that are garbage collected, and
653** where the order in which destructors are called is arbitrary.
654**
655** ^If an [sqlite3] object is destroyed while a transaction is open,
656** the transaction is automatically rolled back.
657**
658** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
659** must be either a NULL
660** pointer or an [sqlite3] object pointer obtained
661** from [sqlite3_open()], [sqlite3_open16()], or
662** [sqlite3_open_v2()], and not previously closed.
663** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
664** argument is a harmless no-op.
665*/
666SQLITE_API int sqlite3_close(sqlite3*);
667SQLITE_API int sqlite3_close_v2(sqlite3*);
668
669/*
670** The type for a callback function.
671** This is legacy and deprecated. It is included for historical
672** compatibility and is not documented.
673*/
674typedef int (*sqlite3_callback)(void*,int,char**, char**);
675
676/*
677** CAPI3REF: One-Step Query Execution Interface
678** METHOD: sqlite3
679**
680** The sqlite3_exec() interface is a convenience wrapper around
681** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
682** that allows an application to run multiple statements of SQL
683** without having to use a lot of C code.
684**
685** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
686** semicolon-separate SQL statements passed into its 2nd argument,
687** in the context of the [database connection] passed in as its 1st
688** argument. ^If the callback function of the 3rd argument to
689** sqlite3_exec() is not NULL, then it is invoked for each result row
690** coming out of the evaluated SQL statements. ^The 4th argument to
691** sqlite3_exec() is relayed through to the 1st argument of each
692** callback invocation. ^If the callback pointer to sqlite3_exec()
693** is NULL, then no callback is ever invoked and result rows are
694** ignored.
695**
696** ^If an error occurs while evaluating the SQL statements passed into
697** sqlite3_exec(), then execution of the current statement stops and
698** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
699** is not NULL then any error message is written into memory obtained
700** from [sqlite3_malloc()] and passed back through the 5th parameter.
701** To avoid memory leaks, the application should invoke [sqlite3_free()]
702** on error message strings returned through the 5th parameter of
703** sqlite3_exec() after the error message string is no longer needed.
704** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
705** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
706** NULL before returning.
707**
708** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
709** routine returns SQLITE_ABORT without invoking the callback again and
710** without running any subsequent SQL statements.
711**
712** ^The 2nd argument to the sqlite3_exec() callback function is the
713** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
714** callback is an array of pointers to strings obtained as if from
715** [sqlite3_column_text()], one for each column. ^If an element of a
716** result row is NULL then the corresponding string pointer for the
717** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
718** sqlite3_exec() callback is an array of pointers to strings where each
719** entry represents the name of corresponding result column as obtained
720** from [sqlite3_column_name()].
721**
722** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
723** to an empty string, or a pointer that contains only whitespace and/or
724** SQL comments, then no SQL statements are evaluated and the database
725** is not changed.
726**
727** Restrictions:
728**
729** <ul>
730** <li> The application must ensure that the 1st parameter to sqlite3_exec()
731** is a valid and open [database connection].
732** <li> The application must not close the [database connection] specified by
733** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
734** <li> The application must not modify the SQL statement text passed into
735** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
736** </ul>
737*/
738SQLITE_API int sqlite3_exec(
739 sqlite3*, /* An open database */
740 const char *sql, /* SQL to be evaluated */
741 int (*callback)(void*,int,char**,char**), /* Callback function */
742 void *, /* 1st argument to callback */
743 char **errmsg /* Error msg written here */
744);
745
746/*
747** CAPI3REF: Result Codes
748** KEYWORDS: {result code definitions}
749**
750** Many SQLite functions return an integer result code from the set shown
751** here in order to indicate success or failure.
752**
753** New error codes may be added in future versions of SQLite.
754**
755** See also: [extended result code definitions]
756*/
757#define SQLITE_OK 0 /* Successful result */
758/* beginning-of-error-codes */
759#define SQLITE_ERROR 1 /* Generic error */
760#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
761#define SQLITE_PERM 3 /* Access permission denied */
762#define SQLITE_ABORT 4 /* Callback routine requested an abort */
763#define SQLITE_BUSY 5 /* The database file is locked */
764#define SQLITE_LOCKED 6 /* A table in the database is locked */
765#define SQLITE_NOMEM 7 /* A malloc() failed */
766#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
767#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
768#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
769#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
770#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
771#define SQLITE_FULL 13 /* Insertion failed because database is full */
772#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
773#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
774#define SQLITE_EMPTY 16 /* Internal use only */
775#define SQLITE_SCHEMA 17 /* The database schema changed */
776#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
777#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
778#define SQLITE_MISMATCH 20 /* Data type mismatch */
779#define SQLITE_MISUSE 21 /* Library used incorrectly */
780#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
781#define SQLITE_AUTH 23 /* Authorization denied */
782#define SQLITE_FORMAT 24 /* Not used */
783#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
784#define SQLITE_NOTADB 26 /* File opened that is not a database file */
785#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
786#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
787#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
788#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
789/* end-of-error-codes */
790
791/*
792** CAPI3REF: Extended Result Codes
793** KEYWORDS: {extended result code definitions}
794**
795** In its default configuration, SQLite API routines return one of 30 integer
796** [result codes]. However, experience has shown that many of
797** these result codes are too coarse-grained. They do not provide as
798** much information about problems as programmers might like. In an effort to
799** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
800** and later) include
801** support for additional result codes that provide more detailed information
802** about errors. These [extended result codes] are enabled or disabled
803** on a per database connection basis using the
804** [sqlite3_extended_result_codes()] API. Or, the extended code for
805** the most recent error can be obtained using
806** [sqlite3_extended_errcode()].
807*/
808#define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8))
809#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8))
810#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8))
811#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
812#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
813#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
814#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
815#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
816#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
817#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
818#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
819#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
820#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
821#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
822#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
823#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
824#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
825#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
826#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
827#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
828#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
829#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
830#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
831#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
832#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
833#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
834#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
835#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
836#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
837#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8))
838#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
839#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
840#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
841#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
842#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8))
843#define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8))
844#define SQLITE_IOERR_IN_PAGE (SQLITE_IOERR | (34<<8))
845#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
846#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
847#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
848#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
849#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8))
850#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
851#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
852#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
853#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
854#define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
855#define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8))
856#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
857#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
858#define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8))
859#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
860#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
861#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
862#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
863#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
864#define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8))
865#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
866#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8))
867#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8))
868#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8))
869#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8))
870#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
871#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
872#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
873#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
874#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
875#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8))
876#define SQLITE_CONSTRAINT_PINNED (SQLITE_CONSTRAINT |(11<<8))
877#define SQLITE_CONSTRAINT_DATATYPE (SQLITE_CONSTRAINT |(12<<8))
878#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
879#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
880#define SQLITE_NOTICE_RBU (SQLITE_NOTICE | (3<<8))
881#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8))
882#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8))
883#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8))
884#define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8)) /* internal use only */
885
886/*
887** CAPI3REF: Flags For File Open Operations
888**
889** These bit values are intended for use in the
890** 3rd parameter to the [sqlite3_open_v2()] interface and
891** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
892**
893** Only those flags marked as "Ok for sqlite3_open_v2()" may be
894** used as the third argument to the [sqlite3_open_v2()] interface.
895** The other flags have historically been ignored by sqlite3_open_v2(),
896** though future versions of SQLite might change so that an error is
897** raised if any of the disallowed bits are passed into sqlite3_open_v2().
898** Applications should not depend on the historical behavior.
899**
900** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into
901** [sqlite3_open_v2()] does *not* cause the underlying database file
902** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into
903** [sqlite3_open_v2()] has historically be a no-op and might become an
904** error in future versions of SQLite.
905*/
906#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
907#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
908#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
909#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
910#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
911#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
912#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
913#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */
914#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
915#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
916#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
917#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
918#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
919#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
920#define SQLITE_OPEN_SUPER_JOURNAL 0x00004000 /* VFS only */
921#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
922#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
923#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
924#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
925#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
926#define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */
927#define SQLITE_OPEN_EXRESCODE 0x02000000 /* Extended result codes */
928
929/* Reserved: 0x00F00000 */
930/* Legacy compatibility: */
931#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
932
933
934/*
935** CAPI3REF: Device Characteristics
936**
937** The xDeviceCharacteristics method of the [sqlite3_io_methods]
938** object returns an integer which is a vector of these
939** bit values expressing I/O characteristics of the mass storage
940** device that holds the file that the [sqlite3_io_methods]
941** refers to.
942**
943** The SQLITE_IOCAP_ATOMIC property means that all writes of
944** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
945** mean that writes of blocks that are nnn bytes in size and
946** are aligned to an address which is an integer multiple of
947** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
948** that when data is appended to a file, the data is appended
949** first then the size of the file is extended, never the other
950** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
951** information is written to disk in the same order as calls
952** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
953** after reboot following a crash or power loss, the only bytes in a
954** file that were written at the application level might have changed
955** and that adjacent bytes, even bytes within the same sector are
956** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
957** flag indicates that a file cannot be deleted when open. The
958** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
959** read-only media and cannot be changed even by processes with
960** elevated privileges.
961**
962** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
963** filesystem supports doing multiple write operations atomically when those
964** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
965** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
966*/
967#define SQLITE_IOCAP_ATOMIC 0x00000001
968#define SQLITE_IOCAP_ATOMIC512 0x00000002
969#define SQLITE_IOCAP_ATOMIC1K 0x00000004
970#define SQLITE_IOCAP_ATOMIC2K 0x00000008
971#define SQLITE_IOCAP_ATOMIC4K 0x00000010
972#define SQLITE_IOCAP_ATOMIC8K 0x00000020
973#define SQLITE_IOCAP_ATOMIC16K 0x00000040
974#define SQLITE_IOCAP_ATOMIC32K 0x00000080
975#define SQLITE_IOCAP_ATOMIC64K 0x00000100
976#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
977#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
978#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
979#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
980#define SQLITE_IOCAP_IMMUTABLE 0x00002000
981#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
982
983/*
984** CAPI3REF: File Locking Levels
985**
986** SQLite uses one of these integer values as the second
987** argument to calls it makes to the xLock() and xUnlock() methods
988** of an [sqlite3_io_methods] object. These values are ordered from
989** lest restrictive to most restrictive.
990**
991** The argument to xLock() is always SHARED or higher. The argument to
992** xUnlock is either SHARED or NONE.
993*/
994#define SQLITE_LOCK_NONE 0 /* xUnlock() only */
995#define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */
996#define SQLITE_LOCK_RESERVED 2 /* xLock() only */
997#define SQLITE_LOCK_PENDING 3 /* xLock() only */
998#define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */
999
1000/*
1001** CAPI3REF: Synchronization Type Flags
1002**
1003** When SQLite invokes the xSync() method of an
1004** [sqlite3_io_methods] object it uses a combination of
1005** these integer values as the second argument.
1006**
1007** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1008** sync operation only needs to flush data to mass storage. Inode
1009** information need not be flushed. If the lower four bits of the flag
1010** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1011** If the lower four bits equal SQLITE_SYNC_FULL, that means
1012** to use Mac OS X style fullsync instead of fsync().
1013**
1014** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1015** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1016** settings. The [synchronous pragma] determines when calls to the
1017** xSync VFS method occur and applies uniformly across all platforms.
1018** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1019** energetic or rigorous or forceful the sync operations are and
1020** only make a difference on Mac OSX for the default SQLite code.
1021** (Third-party VFS implementations might also make the distinction
1022** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1023** operating systems natively supported by SQLite, only Mac OSX
1024** cares about the difference.)
1025*/
1026#define SQLITE_SYNC_NORMAL 0x00002
1027#define SQLITE_SYNC_FULL 0x00003
1028#define SQLITE_SYNC_DATAONLY 0x00010
1029
1030/*
1031** CAPI3REF: OS Interface Open File Handle
1032**
1033** An [sqlite3_file] object represents an open file in the
1034** [sqlite3_vfs | OS interface layer]. Individual OS interface
1035** implementations will
1036** want to subclass this object by appending additional fields
1037** for their own use. The pMethods entry is a pointer to an
1038** [sqlite3_io_methods] object that defines methods for performing
1039** I/O operations on the open file.
1040*/
1041typedef struct sqlite3_file sqlite3_file;
1042struct sqlite3_file {
1043 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1044};
1045
1046/*
1047** CAPI3REF: OS Interface File Virtual Methods Object
1048**
1049** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1050** [sqlite3_file] object (or, more commonly, a subclass of the
1051** [sqlite3_file] object) with a pointer to an instance of this object.
1052** This object defines the methods used to perform various operations
1053** against the open file represented by the [sqlite3_file] object.
1054**
1055** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1056** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1057** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1058** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1059** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1060** to NULL.
1061**
1062** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1063** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1064** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1065** flag may be ORed in to indicate that only the data of the file
1066** and not its inode needs to be synced.
1067**
1068** The integer values to xLock() and xUnlock() are one of
1069** <ul>
1070** <li> [SQLITE_LOCK_NONE],
1071** <li> [SQLITE_LOCK_SHARED],
1072** <li> [SQLITE_LOCK_RESERVED],
1073** <li> [SQLITE_LOCK_PENDING], or
1074** <li> [SQLITE_LOCK_EXCLUSIVE].
1075** </ul>
1076** xLock() upgrades the database file lock. In other words, xLock() moves the
1077** database file lock in the direction NONE toward EXCLUSIVE. The argument to
1078** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1079** SQLITE_LOCK_NONE. If the database file lock is already at or above the
1080** requested lock, then the call to xLock() is a no-op.
1081** xUnlock() downgrades the database file lock to either SHARED or NONE.
1082* If the lock is already at or below the requested lock state, then the call
1083** to xUnlock() is a no-op.
1084** The xCheckReservedLock() method checks whether any database connection,
1085** either in this process or in some other process, is holding a RESERVED,
1086** PENDING, or EXCLUSIVE lock on the file. It returns true
1087** if such a lock exists and false otherwise.
1088**
1089** The xFileControl() method is a generic interface that allows custom
1090** VFS implementations to directly control an open file using the
1091** [sqlite3_file_control()] interface. The second "op" argument is an
1092** integer opcode. The third argument is a generic pointer intended to
1093** point to a structure that may contain arguments or space in which to
1094** write return values. Potential uses for xFileControl() might be
1095** functions to enable blocking locks with timeouts, to change the
1096** locking strategy (for example to use dot-file locks), to inquire
1097** about the status of a lock, or to break stale locks. The SQLite
1098** core reserves all opcodes less than 100 for its own use.
1099** A [file control opcodes | list of opcodes] less than 100 is available.
1100** Applications that define a custom xFileControl method should use opcodes
1101** greater than 100 to avoid conflicts. VFS implementations should
1102** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1103** recognize.
1104**
1105** The xSectorSize() method returns the sector size of the
1106** device that underlies the file. The sector size is the
1107** minimum write that can be performed without disturbing
1108** other bytes in the file. The xDeviceCharacteristics()
1109** method returns a bit vector describing behaviors of the
1110** underlying device:
1111**
1112** <ul>
1113** <li> [SQLITE_IOCAP_ATOMIC]
1114** <li> [SQLITE_IOCAP_ATOMIC512]
1115** <li> [SQLITE_IOCAP_ATOMIC1K]
1116** <li> [SQLITE_IOCAP_ATOMIC2K]
1117** <li> [SQLITE_IOCAP_ATOMIC4K]
1118** <li> [SQLITE_IOCAP_ATOMIC8K]
1119** <li> [SQLITE_IOCAP_ATOMIC16K]
1120** <li> [SQLITE_IOCAP_ATOMIC32K]
1121** <li> [SQLITE_IOCAP_ATOMIC64K]
1122** <li> [SQLITE_IOCAP_SAFE_APPEND]
1123** <li> [SQLITE_IOCAP_SEQUENTIAL]
1124** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1125** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1126** <li> [SQLITE_IOCAP_IMMUTABLE]
1127** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1128** </ul>
1129**
1130** The SQLITE_IOCAP_ATOMIC property means that all writes of
1131** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1132** mean that writes of blocks that are nnn bytes in size and
1133** are aligned to an address which is an integer multiple of
1134** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1135** that when data is appended to a file, the data is appended
1136** first then the size of the file is extended, never the other
1137** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1138** information is written to disk in the same order as calls
1139** to xWrite().
1140**
1141** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1142** in the unread portions of the buffer with zeros. A VFS that
1143** fails to zero-fill short reads might seem to work. However,
1144** failure to zero-fill short reads will eventually lead to
1145** database corruption.
1146*/
1147typedef struct sqlite3_io_methods sqlite3_io_methods;
1148struct sqlite3_io_methods {
1149 int iVersion;
1150 int (*xClose)(sqlite3_file*);
1151 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1152 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1153 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1154 int (*xSync)(sqlite3_file*, int flags);
1155 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1156 int (*xLock)(sqlite3_file*, int);
1157 int (*xUnlock)(sqlite3_file*, int);
1158 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1159 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1160 int (*xSectorSize)(sqlite3_file*);
1161 int (*xDeviceCharacteristics)(sqlite3_file*);
1162 /* Methods above are valid for version 1 */
1163 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1164 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1165 void (*xShmBarrier)(sqlite3_file*);
1166 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1167 /* Methods above are valid for version 2 */
1168 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1169 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1170 /* Methods above are valid for version 3 */
1171 /* Additional methods may be added in future releases */
1172};
1173
1174/*
1175** CAPI3REF: Standard File Control Opcodes
1176** KEYWORDS: {file control opcodes} {file control opcode}
1177**
1178** These integer constants are opcodes for the xFileControl method
1179** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1180** interface.
1181**
1182** <ul>
1183** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1184** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1185** opcode causes the xFileControl method to write the current state of
1186** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1187** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1188** into an integer that the pArg argument points to.
1189** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
1190**
1191** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1192** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1193** layer a hint of how large the database file will grow to be during the
1194** current transaction. This hint is not guaranteed to be accurate but it
1195** is often close. The underlying VFS might choose to preallocate database
1196** file space based on this hint in order to help writes to the database
1197** file run faster.
1198**
1199** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1200** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1201** implements [sqlite3_deserialize()] to set an upper bound on the size
1202** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1203** If the integer pointed to is negative, then it is filled in with the
1204** current limit. Otherwise the limit is set to the larger of the value
1205** of the integer pointed to and the current database size. The integer
1206** pointed to is set to the new limit.
1207**
1208** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1209** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1210** extends and truncates the database file in chunks of a size specified
1211** by the user. The fourth argument to [sqlite3_file_control()] should
1212** point to an integer (type int) containing the new chunk-size to use
1213** for the nominated database. Allocating database file space in large
1214** chunks (say 1MB at a time), may reduce file-system fragmentation and
1215** improve performance on some systems.
1216**
1217** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1218** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1219** to the [sqlite3_file] object associated with a particular database
1220** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1221**
1222** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1223** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1224** to the [sqlite3_file] object associated with the journal file (either
1225** the [rollback journal] or the [write-ahead log]) for a particular database
1226** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1227**
1228** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1229** No longer in use.
1230**
1231** <li>[[SQLITE_FCNTL_SYNC]]
1232** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1233** sent to the VFS immediately before the xSync method is invoked on a
1234** database file descriptor. Or, if the xSync method is not invoked
1235** because the user has configured SQLite with
1236** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1237** of the xSync method. In most cases, the pointer argument passed with
1238** this file-control is NULL. However, if the database file is being synced
1239** as part of a multi-database commit, the argument points to a nul-terminated
1240** string containing the transactions super-journal file name. VFSes that
1241** do not need this signal should silently ignore this opcode. Applications
1242** should not call [sqlite3_file_control()] with this opcode as doing so may
1243** disrupt the operation of the specialized VFSes that do require it.
1244**
1245** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1246** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1247** and sent to the VFS after a transaction has been committed immediately
1248** but before the database is unlocked. VFSes that do not need this signal
1249** should silently ignore this opcode. Applications should not call
1250** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1251** operation of the specialized VFSes that do require it.
1252**
1253** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1254** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1255** retry counts and intervals for certain disk I/O operations for the
1256** windows [VFS] in order to provide robustness in the presence of
1257** anti-virus programs. By default, the windows VFS will retry file read,
1258** file write, and file delete operations up to 10 times, with a delay
1259** of 25 milliseconds before the first retry and with the delay increasing
1260** by an additional 25 milliseconds with each subsequent retry. This
1261** opcode allows these two values (10 retries and 25 milliseconds of delay)
1262** to be adjusted. The values are changed for all database connections
1263** within the same process. The argument is a pointer to an array of two
1264** integers where the first integer is the new retry count and the second
1265** integer is the delay. If either integer is negative, then the setting
1266** is not changed but instead the prior value of that setting is written
1267** into the array entry, allowing the current retry settings to be
1268** interrogated. The zDbName parameter is ignored.
1269**
1270** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1271** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1272** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1273** write ahead log ([WAL file]) and shared memory
1274** files used for transaction control
1275** are automatically deleted when the latest connection to the database
1276** closes. Setting persistent WAL mode causes those files to persist after
1277** close. Persisting the files is useful when other processes that do not
1278** have write permission on the directory containing the database file want
1279** to read the database file, as the WAL and shared memory files must exist
1280** in order for the database to be readable. The fourth parameter to
1281** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1282** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1283** WAL mode. If the integer is -1, then it is overwritten with the current
1284** WAL persistence setting.
1285**
1286** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1287** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1288** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1289** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1290** xDeviceCharacteristics methods. The fourth parameter to
1291** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1292** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1293** mode. If the integer is -1, then it is overwritten with the current
1294** zero-damage mode setting.
1295**
1296** <li>[[SQLITE_FCNTL_OVERWRITE]]
1297** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1298** a write transaction to indicate that, unless it is rolled back for some
1299** reason, the entire database file will be overwritten by the current
1300** transaction. This is used by VACUUM operations.
1301**
1302** <li>[[SQLITE_FCNTL_VFSNAME]]
1303** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1304** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1305** final bottom-level VFS are written into memory obtained from
1306** [sqlite3_malloc()] and the result is stored in the char* variable
1307** that the fourth parameter of [sqlite3_file_control()] points to.
1308** The caller is responsible for freeing the memory when done. As with
1309** all file-control actions, there is no guarantee that this will actually
1310** do anything. Callers should initialize the char* variable to a NULL
1311** pointer in case this file-control is not implemented. This file-control
1312** is intended for diagnostic use only.
1313**
1314** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1315** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1316** [VFSes] currently in use. ^(The argument X in
1317** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1318** of type "[sqlite3_vfs] **". This opcodes will set *X
1319** to a pointer to the top-level VFS.)^
1320** ^When there are multiple VFS shims in the stack, this opcode finds the
1321** upper-most shim only.
1322**
1323** <li>[[SQLITE_FCNTL_PRAGMA]]
1324** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1325** file control is sent to the open [sqlite3_file] object corresponding
1326** to the database file to which the pragma statement refers. ^The argument
1327** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1328** pointers to strings (char**) in which the second element of the array
1329** is the name of the pragma and the third element is the argument to the
1330** pragma or NULL if the pragma has no argument. ^The handler for an
1331** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1332** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1333** or the equivalent and that string will become the result of the pragma or
1334** the error message if the pragma fails. ^If the
1335** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1336** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1337** file control returns [SQLITE_OK], then the parser assumes that the
1338** VFS has handled the PRAGMA itself and the parser generates a no-op
1339** prepared statement if result string is NULL, or that returns a copy
1340** of the result string if the string is non-NULL.
1341** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1342** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1343** that the VFS encountered an error while handling the [PRAGMA] and the
1344** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1345** file control occurs at the beginning of pragma statement analysis and so
1346** it is able to override built-in [PRAGMA] statements.
1347**
1348** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1349** ^The [SQLITE_FCNTL_BUSYHANDLER]
1350** file-control may be invoked by SQLite on the database file handle
1351** shortly after it is opened in order to provide a custom VFS with access
1352** to the connection's busy-handler callback. The argument is of type (void**)
1353** - an array of two (void *) values. The first (void *) actually points
1354** to a function of type (int (*)(void *)). In order to invoke the connection's
1355** busy-handler, this function should be invoked with the second (void *) in
1356** the array as the only argument. If it returns non-zero, then the operation
1357** should be retried. If it returns zero, the custom VFS should abandon the
1358** current operation.
1359**
1360** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1361** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1362** to have SQLite generate a
1363** temporary filename using the same algorithm that is followed to generate
1364** temporary filenames for TEMP tables and other internal uses. The
1365** argument should be a char** which will be filled with the filename
1366** written into memory obtained from [sqlite3_malloc()]. The caller should
1367** invoke [sqlite3_free()] on the result to avoid a memory leak.
1368**
1369** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1370** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1371** maximum number of bytes that will be used for memory-mapped I/O.
1372** The argument is a pointer to a value of type sqlite3_int64 that
1373** is an advisory maximum number of bytes in the file to memory map. The
1374** pointer is overwritten with the old value. The limit is not changed if
1375** the value originally pointed to is negative, and so the current limit
1376** can be queried by passing in a pointer to a negative number. This
1377** file-control is used internally to implement [PRAGMA mmap_size].
1378**
1379** <li>[[SQLITE_FCNTL_TRACE]]
1380** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1381** to the VFS about what the higher layers of the SQLite stack are doing.
1382** This file control is used by some VFS activity tracing [shims].
1383** The argument is a zero-terminated string. Higher layers in the
1384** SQLite stack may generate instances of this file control if
1385** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1386**
1387** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1388** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1389** pointer to an integer and it writes a boolean into that integer depending
1390** on whether or not the file has been renamed, moved, or deleted since it
1391** was first opened.
1392**
1393** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
1394** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
1395** underlying native file handle associated with a file handle. This file
1396** control interprets its argument as a pointer to a native file handle and
1397** writes the resulting value there.
1398**
1399** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1400** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1401** opcode causes the xFileControl method to swap the file handle with the one
1402** pointed to by the pArg argument. This capability is used during testing
1403** and only needs to be supported when SQLITE_TEST is defined.
1404**
1405** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1406** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1407** be advantageous to block on the next WAL lock if the lock is not immediately
1408** available. The WAL subsystem issues this signal during rare
1409** circumstances in order to fix a problem with priority inversion.
1410** Applications should <em>not</em> use this file-control.
1411**
1412** <li>[[SQLITE_FCNTL_ZIPVFS]]
1413** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1414** VFS should return SQLITE_NOTFOUND for this opcode.
1415**
1416** <li>[[SQLITE_FCNTL_RBU]]
1417** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1418** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1419** this opcode.
1420**
1421** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
1422** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
1423** the file descriptor is placed in "batch write mode", which
1424** means all subsequent write operations will be deferred and done
1425** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
1426** that do not support batch atomic writes will return SQLITE_NOTFOUND.
1427** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
1428** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
1429** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
1430** no VFS interface calls on the same [sqlite3_file] file descriptor
1431** except for calls to the xWrite method and the xFileControl method
1432** with [SQLITE_FCNTL_SIZE_HINT].
1433**
1434** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
1435** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
1436** operations since the previous successful call to
1437** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
1438** This file control returns [SQLITE_OK] if and only if the writes were
1439** all performed successfully and have been committed to persistent storage.
1440** ^Regardless of whether or not it is successful, this file control takes
1441** the file descriptor out of batch write mode so that all subsequent
1442** write operations are independent.
1443** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
1444** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1445**
1446** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
1447** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
1448** operations since the previous successful call to
1449** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1450** ^This file control takes the file descriptor out of batch write mode
1451** so that all subsequent write operations are independent.
1452** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1453** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1454**
1455** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1456** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
1457** to block for up to M milliseconds before failing when attempting to
1458** obtain a file lock using the xLock or xShmLock methods of the VFS.
1459** The parameter is a pointer to a 32-bit signed integer that contains
1460** the value that M is to be set to. Before returning, the 32-bit signed
1461** integer is overwritten with the previous value of M.
1462**
1463** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1464** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1465** a database file. The argument is a pointer to a 32-bit unsigned integer.
1466** The "data version" for the pager is written into the pointer. The
1467** "data version" changes whenever any change occurs to the corresponding
1468** database file, either through SQL statements on the same database
1469** connection or through transactions committed by separate database
1470** connections possibly in other processes. The [sqlite3_total_changes()]
1471** interface can be used to find if any database on the connection has changed,
1472** but that interface responds to changes on TEMP as well as MAIN and does
1473** not provide a mechanism to detect changes to MAIN only. Also, the
1474** [sqlite3_total_changes()] interface responds to internal changes only and
1475** omits changes made by other database connections. The
1476** [PRAGMA data_version] command provides a mechanism to detect changes to
1477** a single attached database that occur due to other database connections,
1478** but omits changes implemented by the database connection on which it is
1479** called. This file control is the only mechanism to detect changes that
1480** happen either internally or externally and that are associated with
1481** a particular attached database.
1482**
1483** <li>[[SQLITE_FCNTL_CKPT_START]]
1484** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
1485** in wal mode before the client starts to copy pages from the wal
1486** file to the database file.
1487**
1488** <li>[[SQLITE_FCNTL_CKPT_DONE]]
1489** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
1490** in wal mode after the client has finished copying pages from the wal
1491** file to the database file, but before the *-shm file is updated to
1492** record the fact that the pages have been checkpointed.
1493**
1494** <li>[[SQLITE_FCNTL_EXTERNAL_READER]]
1495** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect
1496** whether or not there is a database client in another process with a wal-mode
1497** transaction open on the database or not. It is only available on unix.The
1498** (void*) argument passed with this file-control should be a pointer to a
1499** value of type (int). The integer value is set to 1 if the database is a wal
1500** mode database and there exists at least one client in another process that
1501** currently has an SQL transaction open on the database. It is set to 0 if
1502** the database is not a wal-mode db, or if there is no such connection in any
1503** other process. This opcode cannot be used to detect transactions opened
1504** by clients within the current process, only within other processes.
1505**
1506** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1507** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the
1508** [checksum VFS shim] only.
1509**
1510** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1511** If there is currently no transaction open on the database, and the
1512** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1513** purges the contents of the in-memory page cache. If there is an open
1514** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1515** </ul>
1516*/
1517#define SQLITE_FCNTL_LOCKSTATE 1
1518#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1519#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
1520#define SQLITE_FCNTL_LAST_ERRNO 4
1521#define SQLITE_FCNTL_SIZE_HINT 5
1522#define SQLITE_FCNTL_CHUNK_SIZE 6
1523#define SQLITE_FCNTL_FILE_POINTER 7
1524#define SQLITE_FCNTL_SYNC_OMITTED 8
1525#define SQLITE_FCNTL_WIN32_AV_RETRY 9
1526#define SQLITE_FCNTL_PERSIST_WAL 10
1527#define SQLITE_FCNTL_OVERWRITE 11
1528#define SQLITE_FCNTL_VFSNAME 12
1529#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1530#define SQLITE_FCNTL_PRAGMA 14
1531#define SQLITE_FCNTL_BUSYHANDLER 15
1532#define SQLITE_FCNTL_TEMPFILENAME 16
1533#define SQLITE_FCNTL_MMAP_SIZE 18
1534#define SQLITE_FCNTL_TRACE 19
1535#define SQLITE_FCNTL_HAS_MOVED 20
1536#define SQLITE_FCNTL_SYNC 21
1537#define SQLITE_FCNTL_COMMIT_PHASETWO 22
1538#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1539#define SQLITE_FCNTL_WAL_BLOCK 24
1540#define SQLITE_FCNTL_ZIPVFS 25
1541#define SQLITE_FCNTL_RBU 26
1542#define SQLITE_FCNTL_VFS_POINTER 27
1543#define SQLITE_FCNTL_JOURNAL_POINTER 28
1544#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
1545#define SQLITE_FCNTL_PDB 30
1546#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
1547#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
1548#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
1549#define SQLITE_FCNTL_LOCK_TIMEOUT 34
1550#define SQLITE_FCNTL_DATA_VERSION 35
1551#define SQLITE_FCNTL_SIZE_LIMIT 36
1552#define SQLITE_FCNTL_CKPT_DONE 37
1553#define SQLITE_FCNTL_RESERVE_BYTES 38
1554#define SQLITE_FCNTL_CKPT_START 39
1555#define SQLITE_FCNTL_EXTERNAL_READER 40
1556#define SQLITE_FCNTL_CKSM_FILE 41
1557#define SQLITE_FCNTL_RESET_CACHE 42
1558
1559/* deprecated names */
1560#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1561#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1562#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
1563
1564
1565/*
1566** CAPI3REF: Mutex Handle
1567**
1568** The mutex module within SQLite defines [sqlite3_mutex] to be an
1569** abstract type for a mutex object. The SQLite core never looks
1570** at the internal representation of an [sqlite3_mutex]. It only
1571** deals with pointers to the [sqlite3_mutex] object.
1572**
1573** Mutexes are created using [sqlite3_mutex_alloc()].
1574*/
1575typedef struct sqlite3_mutex sqlite3_mutex;
1576
1577/*
1578** CAPI3REF: Loadable Extension Thunk
1579**
1580** A pointer to the opaque sqlite3_api_routines structure is passed as
1581** the third parameter to entry points of [loadable extensions]. This
1582** structure must be typedefed in order to work around compiler warnings
1583** on some platforms.
1584*/
1585typedef struct sqlite3_api_routines sqlite3_api_routines;
1586
1587/*
1588** CAPI3REF: File Name
1589**
1590** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1591** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1592** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1593** may also be passed to special APIs such as:
1594**
1595** <ul>
1596** <li> sqlite3_filename_database()
1597** <li> sqlite3_filename_journal()
1598** <li> sqlite3_filename_wal()
1599** <li> sqlite3_uri_parameter()
1600** <li> sqlite3_uri_boolean()
1601** <li> sqlite3_uri_int64()
1602** <li> sqlite3_uri_key()
1603** </ul>
1604*/
1605typedef const char *sqlite3_filename;
1606
1607/*
1608** CAPI3REF: OS Interface Object
1609**
1610** An instance of the sqlite3_vfs object defines the interface between
1611** the SQLite core and the underlying operating system. The "vfs"
1612** in the name of the object stands for "virtual file system". See
1613** the [VFS | VFS documentation] for further information.
1614**
1615** The VFS interface is sometimes extended by adding new methods onto
1616** the end. Each time such an extension occurs, the iVersion field
1617** is incremented. The iVersion value started out as 1 in
1618** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
1619** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
1620** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
1621** may be appended to the sqlite3_vfs object and the iVersion value
1622** may increase again in future versions of SQLite.
1623** Note that due to an oversight, the structure
1624** of the sqlite3_vfs object changed in the transition from
1625** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
1626** and yet the iVersion field was not increased.
1627**
1628** The szOsFile field is the size of the subclassed [sqlite3_file]
1629** structure used by this VFS. mxPathname is the maximum length of
1630** a pathname in this VFS.
1631**
1632** Registered sqlite3_vfs objects are kept on a linked list formed by
1633** the pNext pointer. The [sqlite3_vfs_register()]
1634** and [sqlite3_vfs_unregister()] interfaces manage this list
1635** in a thread-safe way. The [sqlite3_vfs_find()] interface
1636** searches the list. Neither the application code nor the VFS
1637** implementation should use the pNext pointer.
1638**
1639** The pNext field is the only field in the sqlite3_vfs
1640** structure that SQLite will ever modify. SQLite will only access
1641** or modify this field while holding a particular static mutex.
1642** The application should never modify anything within the sqlite3_vfs
1643** object once the object has been registered.
1644**
1645** The zName field holds the name of the VFS module. The name must
1646** be unique across all VFS modules.
1647**
1648** [[sqlite3_vfs.xOpen]]
1649** ^SQLite guarantees that the zFilename parameter to xOpen
1650** is either a NULL pointer or string obtained
1651** from xFullPathname() with an optional suffix added.
1652** ^If a suffix is added to the zFilename parameter, it will
1653** consist of a single "-" character followed by no more than
1654** 11 alphanumeric and/or "-" characters.
1655** ^SQLite further guarantees that
1656** the string will be valid and unchanged until xClose() is
1657** called. Because of the previous sentence,
1658** the [sqlite3_file] can safely store a pointer to the
1659** filename if it needs to remember the filename for some reason.
1660** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1661** must invent its own temporary name for the file. ^Whenever the
1662** xFilename parameter is NULL it will also be the case that the
1663** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1664**
1665** The flags argument to xOpen() includes all bits set in
1666** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
1667** or [sqlite3_open16()] is used, then flags includes at least
1668** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1669** If xOpen() opens a file read-only then it sets *pOutFlags to
1670** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
1671**
1672** ^(SQLite will also add one of the following flags to the xOpen()
1673** call, depending on the object being opened:
1674**
1675** <ul>
1676** <li> [SQLITE_OPEN_MAIN_DB]
1677** <li> [SQLITE_OPEN_MAIN_JOURNAL]
1678** <li> [SQLITE_OPEN_TEMP_DB]
1679** <li> [SQLITE_OPEN_TEMP_JOURNAL]
1680** <li> [SQLITE_OPEN_TRANSIENT_DB]
1681** <li> [SQLITE_OPEN_SUBJOURNAL]
1682** <li> [SQLITE_OPEN_SUPER_JOURNAL]
1683** <li> [SQLITE_OPEN_WAL]
1684** </ul>)^
1685**
1686** The file I/O implementation can use the object type flags to
1687** change the way it deals with files. For example, an application
1688** that does not care about crash recovery or rollback might make
1689** the open of a journal file a no-op. Writes to this journal would
1690** also be no-ops, and any attempt to read the journal would return
1691** SQLITE_IOERR. Or the implementation might recognize that a database
1692** file will be doing page-aligned sector reads and writes in a random
1693** order and set up its I/O subsystem accordingly.
1694**
1695** SQLite might also add one of the following flags to the xOpen method:
1696**
1697** <ul>
1698** <li> [SQLITE_OPEN_DELETEONCLOSE]
1699** <li> [SQLITE_OPEN_EXCLUSIVE]
1700** </ul>
1701**
1702** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1703** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
1704** will be set for TEMP databases and their journals, transient
1705** databases, and subjournals.
1706**
1707** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1708** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1709** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1710** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1711** SQLITE_OPEN_CREATE, is used to indicate that file should always
1712** be created, and that it is an error if it already exists.
1713** It is <i>not</i> used to indicate the file should be opened
1714** for exclusive access.
1715**
1716** ^At least szOsFile bytes of memory are allocated by SQLite
1717** to hold the [sqlite3_file] structure passed as the third
1718** argument to xOpen. The xOpen method does not have to
1719** allocate the structure; it should just fill it in. Note that
1720** the xOpen method must set the sqlite3_file.pMethods to either
1721** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1722** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1723** element will be valid after xOpen returns regardless of the success
1724** or failure of the xOpen call.
1725**
1726** [[sqlite3_vfs.xAccess]]
1727** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1728** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1729** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1730** to test whether a file is at least readable. The SQLITE_ACCESS_READ
1731** flag is never actually used and is not implemented in the built-in
1732** VFSes of SQLite. The file is named by the second argument and can be a
1733** directory. The xAccess method returns [SQLITE_OK] on success or some
1734** non-zero error code if there is an I/O error or if the name of
1735** the file given in the second argument is illegal. If SQLITE_OK
1736** is returned, then non-zero or zero is written into *pResOut to indicate
1737** whether or not the file is accessible.
1738**
1739** ^SQLite will always allocate at least mxPathname+1 bytes for the
1740** output buffer xFullPathname. The exact size of the output buffer
1741** is also passed as a parameter to both methods. If the output buffer
1742** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1743** handled as a fatal error by SQLite, vfs implementations should endeavor
1744** to prevent this by setting mxPathname to a sufficiently large value.
1745**
1746** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1747** interfaces are not strictly a part of the filesystem, but they are
1748** included in the VFS structure for completeness.
1749** The xRandomness() function attempts to return nBytes bytes
1750** of good-quality randomness into zOut. The return value is
1751** the actual number of bytes of randomness obtained.
1752** The xSleep() method causes the calling thread to sleep for at
1753** least the number of microseconds given. ^The xCurrentTime()
1754** method returns a Julian Day Number for the current date and time as
1755** a floating point value.
1756** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1757** Day Number multiplied by 86400000 (the number of milliseconds in
1758** a 24-hour day).
1759** ^SQLite will use the xCurrentTimeInt64() method to get the current
1760** date and time if that method is available (if iVersion is 2 or
1761** greater and the function pointer is not NULL) and will fall back
1762** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1763**
1764** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1765** are not used by the SQLite core. These optional interfaces are provided
1766** by some VFSes to facilitate testing of the VFS code. By overriding
1767** system calls with functions under its control, a test program can
1768** simulate faults and error conditions that would otherwise be difficult
1769** or impossible to induce. The set of system calls that can be overridden
1770** varies from one VFS to another, and from one version of the same VFS to the
1771** next. Applications that use these interfaces must be prepared for any
1772** or all of these interfaces to be NULL or for their behavior to change
1773** from one release to the next. Applications must not attempt to access
1774** any of these methods if the iVersion of the VFS is less than 3.
1775*/
1776typedef struct sqlite3_vfs sqlite3_vfs;
1777typedef void (*sqlite3_syscall_ptr)(void);
1778struct sqlite3_vfs {
1779 int iVersion; /* Structure version number (currently 3) */
1780 int szOsFile; /* Size of subclassed sqlite3_file */
1781 int mxPathname; /* Maximum file pathname length */
1782 sqlite3_vfs *pNext; /* Next registered VFS */
1783 const char *zName; /* Name of this virtual file system */
1784 void *pAppData; /* Pointer to application-specific data */
1785 int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
1786 int flags, int *pOutFlags);
1787 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1788 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1789 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1790 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1791 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1792 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1793 void (*xDlClose)(sqlite3_vfs*, void*);
1794 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1795 int (*xSleep)(sqlite3_vfs*, int microseconds);
1796 int (*xCurrentTime)(sqlite3_vfs*, double*);
1797 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1798 /*
1799 ** The methods above are in version 1 of the sqlite_vfs object
1800 ** definition. Those that follow are added in version 2 or later
1801 */
1802 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1803 /*
1804 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1805 ** Those below are for version 3 and greater.
1806 */
1807 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1808 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1809 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1810 /*
1811 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1812 ** New fields may be appended in future versions. The iVersion
1813 ** value will increment whenever this happens.
1814 */
1815};
1816
1817/*
1818** CAPI3REF: Flags for the xAccess VFS method
1819**
1820** These integer constants can be used as the third parameter to
1821** the xAccess method of an [sqlite3_vfs] object. They determine
1822** what kind of permissions the xAccess method is looking for.
1823** With SQLITE_ACCESS_EXISTS, the xAccess method
1824** simply checks whether the file exists.
1825** With SQLITE_ACCESS_READWRITE, the xAccess method
1826** checks whether the named directory is both readable and writable
1827** (in other words, if files can be added, removed, and renamed within
1828** the directory).
1829** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1830** [temp_store_directory pragma], though this could change in a future
1831** release of SQLite.
1832** With SQLITE_ACCESS_READ, the xAccess method
1833** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
1834** currently unused, though it might be used in a future release of
1835** SQLite.
1836*/
1837#define SQLITE_ACCESS_EXISTS 0
1838#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
1839#define SQLITE_ACCESS_READ 2 /* Unused */
1840
1841/*
1842** CAPI3REF: Flags for the xShmLock VFS method
1843**
1844** These integer constants define the various locking operations
1845** allowed by the xShmLock method of [sqlite3_io_methods]. The
1846** following are the only legal combinations of flags to the
1847** xShmLock method:
1848**
1849** <ul>
1850** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1851** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1852** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1853** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1854** </ul>
1855**
1856** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1857** was given on the corresponding lock.
1858**
1859** The xShmLock method can transition between unlocked and SHARED or
1860** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1861** and EXCLUSIVE.
1862*/
1863#define SQLITE_SHM_UNLOCK 1
1864#define SQLITE_SHM_LOCK 2
1865#define SQLITE_SHM_SHARED 4
1866#define SQLITE_SHM_EXCLUSIVE 8
1867
1868/*
1869** CAPI3REF: Maximum xShmLock index
1870**
1871** The xShmLock method on [sqlite3_io_methods] may use values
1872** between 0 and this upper bound as its "offset" argument.
1873** The SQLite core will never attempt to acquire or release a
1874** lock outside of this range
1875*/
1876#define SQLITE_SHM_NLOCK 8
1877
1878
1879/*
1880** CAPI3REF: Initialize The SQLite Library
1881**
1882** ^The sqlite3_initialize() routine initializes the
1883** SQLite library. ^The sqlite3_shutdown() routine
1884** deallocates any resources that were allocated by sqlite3_initialize().
1885** These routines are designed to aid in process initialization and
1886** shutdown on embedded systems. Workstation applications using
1887** SQLite normally do not need to invoke either of these routines.
1888**
1889** A call to sqlite3_initialize() is an "effective" call if it is
1890** the first time sqlite3_initialize() is invoked during the lifetime of
1891** the process, or if it is the first time sqlite3_initialize() is invoked
1892** following a call to sqlite3_shutdown(). ^(Only an effective call
1893** of sqlite3_initialize() does any initialization. All other calls
1894** are harmless no-ops.)^
1895**
1896** A call to sqlite3_shutdown() is an "effective" call if it is the first
1897** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
1898** an effective call to sqlite3_shutdown() does any deinitialization.
1899** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1900**
1901** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1902** is not. The sqlite3_shutdown() interface must only be called from a
1903** single thread. All open [database connections] must be closed and all
1904** other SQLite resources must be deallocated prior to invoking
1905** sqlite3_shutdown().
1906**
1907** Among other things, ^sqlite3_initialize() will invoke
1908** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
1909** will invoke sqlite3_os_end().
1910**
1911** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1912** ^If for some reason, sqlite3_initialize() is unable to initialize
1913** the library (perhaps it is unable to allocate a needed resource such
1914** as a mutex) it returns an [error code] other than [SQLITE_OK].
1915**
1916** ^The sqlite3_initialize() routine is called internally by many other
1917** SQLite interfaces so that an application usually does not need to
1918** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
1919** calls sqlite3_initialize() so the SQLite library will be automatically
1920** initialized when [sqlite3_open()] is called if it has not be initialized
1921** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1922** compile-time option, then the automatic calls to sqlite3_initialize()
1923** are omitted and the application must call sqlite3_initialize() directly
1924** prior to using any other SQLite interface. For maximum portability,
1925** it is recommended that applications always invoke sqlite3_initialize()
1926** directly prior to using any other SQLite interface. Future releases
1927** of SQLite may require this. In other words, the behavior exhibited
1928** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1929** default behavior in some future release of SQLite.
1930**
1931** The sqlite3_os_init() routine does operating-system specific
1932** initialization of the SQLite library. The sqlite3_os_end()
1933** routine undoes the effect of sqlite3_os_init(). Typical tasks
1934** performed by these routines include allocation or deallocation
1935** of static resources, initialization of global variables,
1936** setting up a default [sqlite3_vfs] module, or setting up
1937** a default configuration using [sqlite3_config()].
1938**
1939** The application should never invoke either sqlite3_os_init()
1940** or sqlite3_os_end() directly. The application should only invoke
1941** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
1942** interface is called automatically by sqlite3_initialize() and
1943** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
1944** implementations for sqlite3_os_init() and sqlite3_os_end()
1945** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1946** When [custom builds | built for other platforms]
1947** (using the [SQLITE_OS_OTHER=1] compile-time
1948** option) the application must supply a suitable implementation for
1949** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1950** implementation of sqlite3_os_init() or sqlite3_os_end()
1951** must return [SQLITE_OK] on success and some other [error code] upon
1952** failure.
1953*/
1954SQLITE_API int sqlite3_initialize(void);
1955SQLITE_API int sqlite3_shutdown(void);
1956SQLITE_API int sqlite3_os_init(void);
1957SQLITE_API int sqlite3_os_end(void);
1958
1959/*
1960** CAPI3REF: Configuring The SQLite Library
1961**
1962** The sqlite3_config() interface is used to make global configuration
1963** changes to SQLite in order to tune SQLite to the specific needs of
1964** the application. The default configuration is recommended for most
1965** applications and so this routine is usually not necessary. It is
1966** provided to support rare applications with unusual needs.
1967**
1968** <b>The sqlite3_config() interface is not threadsafe. The application
1969** must ensure that no other SQLite interfaces are invoked by other
1970** threads while sqlite3_config() is running.</b>
1971**
1972** The first argument to sqlite3_config() is an integer
1973** [configuration option] that determines
1974** what property of SQLite is to be configured. Subsequent arguments
1975** vary depending on the [configuration option]
1976** in the first argument.
1977**
1978** For most configuration options, the sqlite3_config() interface
1979** may only be invoked prior to library initialization using
1980** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1981** The exceptional configuration options that may be invoked at any time
1982** are called "anytime configuration options".
1983** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1984** [sqlite3_shutdown()] with a first argument that is not an anytime
1985** configuration option, then the sqlite3_config() call will return SQLITE_MISUSE.
1986** Note, however, that ^sqlite3_config() can be called as part of the
1987** implementation of an application-defined [sqlite3_os_init()].
1988**
1989** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1990** ^If the option is unknown or SQLite is unable to set the option
1991** then this routine returns a non-zero [error code].
1992*/
1993SQLITE_API int sqlite3_config(int, ...);
1994
1995/*
1996** CAPI3REF: Configure database connections
1997** METHOD: sqlite3
1998**
1999** The sqlite3_db_config() interface is used to make configuration
2000** changes to a [database connection]. The interface is similar to
2001** [sqlite3_config()] except that the changes apply to a single
2002** [database connection] (specified in the first argument).
2003**
2004** The second argument to sqlite3_db_config(D,V,...) is the
2005** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2006** that indicates what aspect of the [database connection] is being configured.
2007** Subsequent arguments vary depending on the configuration verb.
2008**
2009** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2010** the call is considered successful.
2011*/
2012SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2013
2014/*
2015** CAPI3REF: Memory Allocation Routines
2016**
2017** An instance of this object defines the interface between SQLite
2018** and low-level memory allocation routines.
2019**
2020** This object is used in only one place in the SQLite interface.
2021** A pointer to an instance of this object is the argument to
2022** [sqlite3_config()] when the configuration option is
2023** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2024** By creating an instance of this object
2025** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2026** during configuration, an application can specify an alternative
2027** memory allocation subsystem for SQLite to use for all of its
2028** dynamic memory needs.
2029**
2030** Note that SQLite comes with several [built-in memory allocators]
2031** that are perfectly adequate for the overwhelming majority of applications
2032** and that this object is only useful to a tiny minority of applications
2033** with specialized memory allocation requirements. This object is
2034** also used during testing of SQLite in order to specify an alternative
2035** memory allocator that simulates memory out-of-memory conditions in
2036** order to verify that SQLite recovers gracefully from such
2037** conditions.
2038**
2039** The xMalloc, xRealloc, and xFree methods must work like the
2040** malloc(), realloc() and free() functions from the standard C library.
2041** ^SQLite guarantees that the second argument to
2042** xRealloc is always a value returned by a prior call to xRoundup.
2043**
2044** xSize should return the allocated size of a memory allocation
2045** previously obtained from xMalloc or xRealloc. The allocated size
2046** is always at least as big as the requested size but may be larger.
2047**
2048** The xRoundup method returns what would be the allocated size of
2049** a memory allocation given a particular requested size. Most memory
2050** allocators round up memory allocations at least to the next multiple
2051** of 8. Some allocators round up to a larger multiple or to a power of 2.
2052** Every memory allocation request coming in through [sqlite3_malloc()]
2053** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2054** that causes the corresponding memory allocation to fail.
2055**
2056** The xInit method initializes the memory allocator. For example,
2057** it might allocate any required mutexes or initialize internal data
2058** structures. The xShutdown method is invoked (indirectly) by
2059** [sqlite3_shutdown()] and should deallocate any resources acquired
2060** by xInit. The pAppData pointer is used as the only parameter to
2061** xInit and xShutdown.
2062**
2063** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
2064** the xInit method, so the xInit method need not be threadsafe. The
2065** xShutdown method is only called from [sqlite3_shutdown()] so it does
2066** not need to be threadsafe either. For all other methods, SQLite
2067** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2068** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2069** it is by default) and so the methods are automatically serialized.
2070** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2071** methods must be threadsafe or else make their own arrangements for
2072** serialization.
2073**
2074** SQLite will never invoke xInit() more than once without an intervening
2075** call to xShutdown().
2076*/
2077typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2078struct sqlite3_mem_methods {
2079 void *(*xMalloc)(int); /* Memory allocation function */
2080 void (*xFree)(void*); /* Free a prior allocation */
2081 void *(*xRealloc)(void*,int); /* Resize an allocation */
2082 int (*xSize)(void*); /* Return the size of an allocation */
2083 int (*xRoundup)(int); /* Round up request size to allocation size */
2084 int (*xInit)(void*); /* Initialize the memory allocator */
2085 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2086 void *pAppData; /* Argument to xInit() and xShutdown() */
2087};
2088
2089/*
2090** CAPI3REF: Configuration Options
2091** KEYWORDS: {configuration option}
2092**
2093** These constants are the available integer configuration options that
2094** can be passed as the first argument to the [sqlite3_config()] interface.
2095**
2096** Most of the configuration options for sqlite3_config()
2097** will only work if invoked prior to [sqlite3_initialize()] or after
2098** [sqlite3_shutdown()]. The few exceptions to this rule are called
2099** "anytime configuration options".
2100** ^Calling [sqlite3_config()] with a first argument that is not an
2101** anytime configuration option in between calls to [sqlite3_initialize()] and
2102** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2103**
2104** The set of anytime configuration options can change (by insertions
2105** and/or deletions) from one release of SQLite to the next.
2106** As of SQLite version 3.42.0, the complete set of anytime configuration
2107** options is:
2108** <ul>
2109** <li> SQLITE_CONFIG_LOG
2110** <li> SQLITE_CONFIG_PCACHE_HDRSZ
2111** </ul>
2112**
2113** New configuration options may be added in future releases of SQLite.
2114** Existing configuration options might be discontinued. Applications
2115** should check the return code from [sqlite3_config()] to make sure that
2116** the call worked. The [sqlite3_config()] interface will return a
2117** non-zero [error code] if a discontinued or unsupported configuration option
2118** is invoked.
2119**
2120** <dl>
2121** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2122** <dd>There are no arguments to this option. ^This option sets the
2123** [threading mode] to Single-thread. In other words, it disables
2124** all mutexing and puts SQLite into a mode where it can only be used
2125** by a single thread. ^If SQLite is compiled with
2126** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2127** it is not possible to change the [threading mode] from its default
2128** value of Single-thread and so [sqlite3_config()] will return
2129** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2130** configuration option.</dd>
2131**
2132** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2133** <dd>There are no arguments to this option. ^This option sets the
2134** [threading mode] to Multi-thread. In other words, it disables
2135** mutexing on [database connection] and [prepared statement] objects.
2136** The application is responsible for serializing access to
2137** [database connections] and [prepared statements]. But other mutexes
2138** are enabled so that SQLite will be safe to use in a multi-threaded
2139** environment as long as no two threads attempt to use the same
2140** [database connection] at the same time. ^If SQLite is compiled with
2141** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2142** it is not possible to set the Multi-thread [threading mode] and
2143** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2144** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2145**
2146** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2147** <dd>There are no arguments to this option. ^This option sets the
2148** [threading mode] to Serialized. In other words, this option enables
2149** all mutexes including the recursive
2150** mutexes on [database connection] and [prepared statement] objects.
2151** In this mode (which is the default when SQLite is compiled with
2152** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2153** to [database connections] and [prepared statements] so that the
2154** application is free to use the same [database connection] or the
2155** same [prepared statement] in different threads at the same time.
2156** ^If SQLite is compiled with
2157** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2158** it is not possible to set the Serialized [threading mode] and
2159** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2160** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2161**
2162** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2163** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2164** a pointer to an instance of the [sqlite3_mem_methods] structure.
2165** The argument specifies
2166** alternative low-level memory allocation routines to be used in place of
2167** the memory allocation routines built into SQLite.)^ ^SQLite makes
2168** its own private copy of the content of the [sqlite3_mem_methods] structure
2169** before the [sqlite3_config()] call returns.</dd>
2170**
2171** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2172** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2173** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2174** The [sqlite3_mem_methods]
2175** structure is filled with the currently defined memory allocation routines.)^
2176** This option can be used to overload the default memory allocation
2177** routines with a wrapper that simulations memory allocation failure or
2178** tracks memory usage, for example. </dd>
2179**
2180** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2181** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2182** type int, interpreted as a boolean, which if true provides a hint to
2183** SQLite that it should avoid large memory allocations if possible.
2184** SQLite will run faster if it is free to make large memory allocations,
2185** but some application might prefer to run slower in exchange for
2186** guarantees about memory fragmentation that are possible if large
2187** allocations are avoided. This hint is normally off.
2188** </dd>
2189**
2190** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2191** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2192** interpreted as a boolean, which enables or disables the collection of
2193** memory allocation statistics. ^(When memory allocation statistics are
2194** disabled, the following SQLite interfaces become non-operational:
2195** <ul>
2196** <li> [sqlite3_hard_heap_limit64()]
2197** <li> [sqlite3_memory_used()]
2198** <li> [sqlite3_memory_highwater()]
2199** <li> [sqlite3_soft_heap_limit64()]
2200** <li> [sqlite3_status64()]
2201** </ul>)^
2202** ^Memory allocation statistics are enabled by default unless SQLite is
2203** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2204** allocation statistics are disabled by default.
2205** </dd>
2206**
2207** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2208** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2209** </dd>
2210**
2211** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2212** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2213** that SQLite can use for the database page cache with the default page
2214** cache implementation.
2215** This configuration option is a no-op if an application-defined page
2216** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2217** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2218** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2219** and the number of cache lines (N).
2220** The sz argument should be the size of the largest database page
2221** (a power of two between 512 and 65536) plus some extra bytes for each
2222** page header. ^The number of extra bytes needed by the page header
2223** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2224** ^It is harmless, apart from the wasted memory,
2225** for the sz parameter to be larger than necessary. The pMem
2226** argument must be either a NULL pointer or a pointer to an 8-byte
2227** aligned block of memory of at least sz*N bytes, otherwise
2228** subsequent behavior is undefined.
2229** ^When pMem is not NULL, SQLite will strive to use the memory provided
2230** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2231** a page cache line is larger than sz bytes or if all of the pMem buffer
2232** is exhausted.
2233** ^If pMem is NULL and N is non-zero, then each database connection
2234** does an initial bulk allocation for page cache memory
2235** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2236** of -1024*N bytes if N is negative, . ^If additional
2237** page cache memory is needed beyond what is provided by the initial
2238** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2239** additional cache line. </dd>
2240**
2241** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2242** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2243** that SQLite will use for all of its dynamic memory allocation needs
2244** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2245** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2246** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2247** [SQLITE_ERROR] if invoked otherwise.
2248** ^There are three arguments to SQLITE_CONFIG_HEAP:
2249** An 8-byte aligned pointer to the memory,
2250** the number of bytes in the memory buffer, and the minimum allocation size.
2251** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2252** to using its default memory allocator (the system malloc() implementation),
2253** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2254** memory pointer is not NULL then the alternative memory
2255** allocator is engaged to handle all of SQLites memory allocation needs.
2256** The first pointer (the memory pointer) must be aligned to an 8-byte
2257** boundary or subsequent behavior of SQLite will be undefined.
2258** The minimum allocation size is capped at 2**12. Reasonable values
2259** for the minimum allocation size are 2**5 through 2**8.</dd>
2260**
2261** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2262** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2263** pointer to an instance of the [sqlite3_mutex_methods] structure.
2264** The argument specifies alternative low-level mutex routines to be used
2265** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2266** the content of the [sqlite3_mutex_methods] structure before the call to
2267** [sqlite3_config()] returns. ^If SQLite is compiled with
2268** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2269** the entire mutexing subsystem is omitted from the build and hence calls to
2270** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2271** return [SQLITE_ERROR].</dd>
2272**
2273** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2274** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2275** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2276** [sqlite3_mutex_methods]
2277** structure is filled with the currently defined mutex routines.)^
2278** This option can be used to overload the default mutex allocation
2279** routines with a wrapper used to track mutex usage for performance
2280** profiling or testing, for example. ^If SQLite is compiled with
2281** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2282** the entire mutexing subsystem is omitted from the build and hence calls to
2283** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2284** return [SQLITE_ERROR].</dd>
2285**
2286** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2287** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2288** the default size of lookaside memory on each [database connection].
2289** The first argument is the
2290** size of each lookaside buffer slot and the second is the number of
2291** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2292** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2293** option to [sqlite3_db_config()] can be used to change the lookaside
2294** configuration on individual connections.)^ </dd>
2295**
2296** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2297** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2298** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2299** the interface to a custom page cache implementation.)^
2300** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2301**
2302** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2303** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2304** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2305** the current page cache implementation into that object.)^ </dd>
2306**
2307** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2308** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2309** global [error log].
2310** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2311** function with a call signature of void(*)(void*,int,const char*),
2312** and a pointer to void. ^If the function pointer is not NULL, it is
2313** invoked by [sqlite3_log()] to process each logging event. ^If the
2314** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2315** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2316** passed through as the first parameter to the application-defined logger
2317** function whenever that function is invoked. ^The second parameter to
2318** the logger function is a copy of the first parameter to the corresponding
2319** [sqlite3_log()] call and is intended to be a [result code] or an
2320** [extended result code]. ^The third parameter passed to the logger is
2321** log message after formatting via [sqlite3_snprintf()].
2322** The SQLite logging interface is not reentrant; the logger function
2323** supplied by the application must not invoke any SQLite interface.
2324** In a multi-threaded application, the application-defined logger
2325** function must be threadsafe. </dd>
2326**
2327** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2328** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2329** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2330** then URI handling is globally disabled.)^ ^If URI handling is globally
2331** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2332** [sqlite3_open16()] or
2333** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2334** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2335** connection is opened. ^If it is globally disabled, filenames are
2336** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2337** database connection is opened. ^(By default, URI handling is globally
2338** disabled. The default value may be changed by compiling with the
2339** [SQLITE_USE_URI] symbol defined.)^
2340**
2341** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2342** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2343** argument which is interpreted as a boolean in order to enable or disable
2344** the use of covering indices for full table scans in the query optimizer.
2345** ^The default setting is determined
2346** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2347** if that compile-time option is omitted.
2348** The ability to disable the use of covering indices for full table scans
2349** is because some incorrectly coded legacy applications might malfunction
2350** when the optimization is enabled. Providing the ability to
2351** disable the optimization allows the older, buggy application code to work
2352** without change even with newer versions of SQLite.
2353**
2354** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2355** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2356** <dd> These options are obsolete and should not be used by new code.
2357** They are retained for backwards compatibility but are now no-ops.
2358** </dd>
2359**
2360** [[SQLITE_CONFIG_SQLLOG]]
2361** <dt>SQLITE_CONFIG_SQLLOG
2362** <dd>This option is only available if sqlite is compiled with the
2363** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2364** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2365** The second should be of type (void*). The callback is invoked by the library
2366** in three separate circumstances, identified by the value passed as the
2367** fourth parameter. If the fourth parameter is 0, then the database connection
2368** passed as the second argument has just been opened. The third argument
2369** points to a buffer containing the name of the main database file. If the
2370** fourth parameter is 1, then the SQL statement that the third parameter
2371** points to has just been executed. Or, if the fourth parameter is 2, then
2372** the connection being passed as the second parameter is being closed. The
2373** third parameter is passed NULL In this case. An example of using this
2374** configuration option can be seen in the "test_sqllog.c" source file in
2375** the canonical SQLite source tree.</dd>
2376**
2377** [[SQLITE_CONFIG_MMAP_SIZE]]
2378** <dt>SQLITE_CONFIG_MMAP_SIZE
2379** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2380** that are the default mmap size limit (the default setting for
2381** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2382** ^The default setting can be overridden by each database connection using
2383** either the [PRAGMA mmap_size] command, or by using the
2384** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2385** will be silently truncated if necessary so that it does not exceed the
2386** compile-time maximum mmap size set by the
2387** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2388** ^If either argument to this option is negative, then that argument is
2389** changed to its compile-time default.
2390**
2391** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2392** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2393** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2394** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2395** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2396** that specifies the maximum size of the created heap.
2397**
2398** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2399** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2400** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2401** is a pointer to an integer and writes into that integer the number of extra
2402** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2403** The amount of extra space required can change depending on the compiler,
2404** target platform, and SQLite version.
2405**
2406** [[SQLITE_CONFIG_PMASZ]]
2407** <dt>SQLITE_CONFIG_PMASZ
2408** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2409** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2410** sorter to that integer. The default minimum PMA Size is set by the
2411** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2412** to help with sort operations when multithreaded sorting
2413** is enabled (using the [PRAGMA threads] command) and the amount of content
2414** to be sorted exceeds the page size times the minimum of the
2415** [PRAGMA cache_size] setting and this value.
2416**
2417** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2418** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2419** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2420** becomes the [statement journal] spill-to-disk threshold.
2421** [Statement journals] are held in memory until their size (in bytes)
2422** exceeds this threshold, at which point they are written to disk.
2423** Or if the threshold is -1, statement journals are always held
2424** exclusively in memory.
2425** Since many statement journals never become large, setting the spill
2426** threshold to a value such as 64KiB can greatly reduce the amount of
2427** I/O required to support statement rollback.
2428** The default value for this setting is controlled by the
2429** [SQLITE_STMTJRNL_SPILL] compile-time option.
2430**
2431** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2432** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2433** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2434** of type (int) - the new value of the sorter-reference size threshold.
2435** Usually, when SQLite uses an external sort to order records according
2436** to an ORDER BY clause, all fields required by the caller are present in the
2437** sorted records. However, if SQLite determines based on the declared type
2438** of a table column that its values are likely to be very large - larger
2439** than the configured sorter-reference size threshold - then a reference
2440** is stored in each sorted record and the required column values loaded
2441** from the database as records are returned in sorted order. The default
2442** value for this option is to never use this optimization. Specifying a
2443** negative value for this option restores the default behaviour.
2444** This option is only available if SQLite is compiled with the
2445** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2446**
2447** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
2448** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
2449** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
2450** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2451** database created using [sqlite3_deserialize()]. This default maximum
2452** size can be adjusted up or down for individual databases using the
2453** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2454** configuration setting is never used, then the default maximum is determined
2455** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2456** compile-time option is not set, then the default maximum is 1073741824.
2457** </dl>
2458*/
2459#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
2460#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
2461#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
2462#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
2463#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
2464#define SQLITE_CONFIG_SCRATCH 6 /* No longer used */
2465#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
2466#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
2467#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
2468#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
2469#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
2470/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2471#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
2472#define SQLITE_CONFIG_PCACHE 14 /* no-op */
2473#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2474#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
2475#define SQLITE_CONFIG_URI 17 /* int */
2476#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
2477#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
2478#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
2479#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
2480#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
2481#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
2482#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
2483#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */
2484#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */
2485#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
2486#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
2487#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */
2488
2489/*
2490** CAPI3REF: Database Connection Configuration Options
2491**
2492** These constants are the available integer configuration options that
2493** can be passed as the second argument to the [sqlite3_db_config()] interface.
2494**
2495** New configuration options may be added in future releases of SQLite.
2496** Existing configuration options might be discontinued. Applications
2497** should check the return code from [sqlite3_db_config()] to make sure that
2498** the call worked. ^The [sqlite3_db_config()] interface will return a
2499** non-zero [error code] if a discontinued or unsupported configuration option
2500** is invoked.
2501**
2502** <dl>
2503** [[SQLITE_DBCONFIG_LOOKASIDE]]
2504** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2505** <dd> ^This option takes three additional arguments that determine the
2506** [lookaside memory allocator] configuration for the [database connection].
2507** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2508** pointer to a memory buffer to use for lookaside memory.
2509** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2510** may be NULL in which case SQLite will allocate the
2511** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2512** size of each lookaside buffer slot. ^The third argument is the number of
2513** slots. The size of the buffer in the first argument must be greater than
2514** or equal to the product of the second and third arguments. The buffer
2515** must be aligned to an 8-byte boundary. ^If the second argument to
2516** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2517** rounded down to the next smaller multiple of 8. ^(The lookaside memory
2518** configuration for a database connection can only be changed when that
2519** connection is not currently using lookaside memory, or in other words
2520** when the "current value" returned by
2521** [sqlite3_db_status](D,[SQLITE_DBSTATUS_LOOKASIDE_USED],...) is zero.
2522** Any attempt to change the lookaside memory configuration when lookaside
2523** memory is in use leaves the configuration unchanged and returns
2524** [SQLITE_BUSY].)^</dd>
2525**
2526** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2527** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2528** <dd> ^This option is used to enable or disable the enforcement of
2529** [foreign key constraints]. There should be two additional arguments.
2530** The first argument is an integer which is 0 to disable FK enforcement,
2531** positive to enable FK enforcement or negative to leave FK enforcement
2532** unchanged. The second parameter is a pointer to an integer into which
2533** is written 0 or 1 to indicate whether FK enforcement is off or on
2534** following this call. The second parameter may be a NULL pointer, in
2535** which case the FK enforcement setting is not reported back. </dd>
2536**
2537** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
2538** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2539** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2540** There should be two additional arguments.
2541** The first argument is an integer which is 0 to disable triggers,
2542** positive to enable triggers or negative to leave the setting unchanged.
2543** The second parameter is a pointer to an integer into which
2544** is written 0 or 1 to indicate whether triggers are disabled or enabled
2545** following this call. The second parameter may be a NULL pointer, in
2546** which case the trigger setting is not reported back.
2547**
2548** <p>Originally this option disabled all triggers. ^(However, since
2549** SQLite version 3.35.0, TEMP triggers are still allowed even if
2550** this option is off. So, in other words, this option now only disables
2551** triggers in the main database schema or in the schemas of ATTACH-ed
2552** databases.)^ </dd>
2553**
2554** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
2555** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
2556** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
2557** There should be two additional arguments.
2558** The first argument is an integer which is 0 to disable views,
2559** positive to enable views or negative to leave the setting unchanged.
2560** The second parameter is a pointer to an integer into which
2561** is written 0 or 1 to indicate whether views are disabled or enabled
2562** following this call. The second parameter may be a NULL pointer, in
2563** which case the view setting is not reported back.
2564**
2565** <p>Originally this option disabled all views. ^(However, since
2566** SQLite version 3.35.0, TEMP views are still allowed even if
2567** this option is off. So, in other words, this option now only disables
2568** views in the main database schema or in the schemas of ATTACH-ed
2569** databases.)^ </dd>
2570**
2571** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
2572** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2573** <dd> ^This option is used to enable or disable the
2574** [fts3_tokenizer()] function which is part of the
2575** [FTS3] full-text search engine extension.
2576** There should be two additional arguments.
2577** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2578** positive to enable fts3_tokenizer() or negative to leave the setting
2579** unchanged.
2580** The second parameter is a pointer to an integer into which
2581** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2582** following this call. The second parameter may be a NULL pointer, in
2583** which case the new setting is not reported back. </dd>
2584**
2585** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
2586** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
2587** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
2588** interface independently of the [load_extension()] SQL function.
2589** The [sqlite3_enable_load_extension()] API enables or disables both the
2590** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2591** There should be two additional arguments.
2592** When the first argument to this interface is 1, then only the C-API is
2593** enabled and the SQL function remains disabled. If the first argument to
2594** this interface is 0, then both the C-API and the SQL function are disabled.
2595** If the first argument is -1, then no changes are made to state of either the
2596** C-API or the SQL function.
2597** The second parameter is a pointer to an integer into which
2598** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
2599** is disabled or enabled following this call. The second parameter may
2600** be a NULL pointer, in which case the new setting is not reported back.
2601** </dd>
2602**
2603** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
2604** <dd> ^This option is used to change the name of the "main" database
2605** schema. ^The sole argument is a pointer to a constant UTF8 string
2606** which will become the new schema name in place of "main". ^SQLite
2607** does not make a copy of the new main schema name string, so the application
2608** must ensure that the argument passed into this DBCONFIG option is unchanged
2609** until after the database connection closes.
2610** </dd>
2611**
2612** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
2613** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
2614** <dd> Usually, when a database in wal mode is closed or detached from a
2615** database handle, SQLite checks if this will mean that there are now no
2616** connections at all to the database. If so, it performs a checkpoint
2617** operation before closing the connection. This option may be used to
2618** override this behaviour. The first parameter passed to this operation
2619** is an integer - positive to disable checkpoints-on-close, or zero (the
2620** default) to enable them, and negative to leave the setting unchanged.
2621** The second parameter is a pointer to an integer
2622** into which is written 0 or 1 to indicate whether checkpoints-on-close
2623** have been disabled - 0 if they are not disabled, 1 if they are.
2624** </dd>
2625**
2626** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
2627** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
2628** the [query planner stability guarantee] (QPSG). When the QPSG is active,
2629** a single SQL query statement will always use the same algorithm regardless
2630** of values of [bound parameters].)^ The QPSG disables some query optimizations
2631** that look at the values of bound parameters, which can make some queries
2632** slower. But the QPSG has the advantage of more predictable behavior. With
2633** the QPSG active, SQLite will always use the same query plan in the field as
2634** was used during testing in the lab.
2635** The first argument to this setting is an integer which is 0 to disable
2636** the QPSG, positive to enable QPSG, or negative to leave the setting
2637** unchanged. The second parameter is a pointer to an integer into which
2638** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
2639** following this call.
2640** </dd>
2641**
2642** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
2643** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
2644** include output for any operations performed by trigger programs. This
2645** option is used to set or clear (the default) a flag that governs this
2646** behavior. The first parameter passed to this operation is an integer -
2647** positive to enable output for trigger programs, or zero to disable it,
2648** or negative to leave the setting unchanged.
2649** The second parameter is a pointer to an integer into which is written
2650** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2651** it is not disabled, 1 if it is.
2652** </dd>
2653**
2654** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2655** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2656** [VACUUM] in order to reset a database back to an empty database
2657** with no schema and no content. The following process works even for
2658** a badly corrupted database file:
2659** <ol>
2660** <li> If the database connection is newly opened, make sure it has read the
2661** database schema by preparing then discarding some query against the
2662** database, or calling sqlite3_table_column_metadata(), ignoring any
2663** errors. This step is only necessary if the application desires to keep
2664** the database in WAL mode after the reset if it was in WAL mode before
2665** the reset.
2666** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2667** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2668** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2669** </ol>
2670** Because resetting a database is destructive and irreversible, the
2671** process requires the use of this obscure API and multiple steps to
2672** help ensure that it does not happen by accident. Because this
2673** feature must be capable of resetting corrupt databases, and
2674** shutting down virtual tables may require access to that corrupt
2675** storage, the library must abandon any installed virtual tables
2676** without calling their xDestroy() methods.
2677**
2678** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
2679** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
2680** "defensive" flag for a database connection. When the defensive
2681** flag is enabled, language features that allow ordinary SQL to
2682** deliberately corrupt the database file are disabled. The disabled
2683** features include but are not limited to the following:
2684** <ul>
2685** <li> The [PRAGMA writable_schema=ON] statement.
2686** <li> The [PRAGMA journal_mode=OFF] statement.
2687** <li> The [PRAGMA schema_version=N] statement.
2688** <li> Writes to the [sqlite_dbpage] virtual table.
2689** <li> Direct writes to [shadow tables].
2690** </ul>
2691** </dd>
2692**
2693** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
2694** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
2695** "writable_schema" flag. This has the same effect and is logically equivalent
2696** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
2697** The first argument to this setting is an integer which is 0 to disable
2698** the writable_schema, positive to enable writable_schema, or negative to
2699** leave the setting unchanged. The second parameter is a pointer to an
2700** integer into which is written 0 or 1 to indicate whether the writable_schema
2701** is enabled or disabled following this call.
2702** </dd>
2703**
2704** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
2705** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
2706** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
2707** the legacy behavior of the [ALTER TABLE RENAME] command such it
2708** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2709** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
2710** additional information. This feature can also be turned on and off
2711** using the [PRAGMA legacy_alter_table] statement.
2712** </dd>
2713**
2714** [[SQLITE_DBCONFIG_DQS_DML]]
2715** <dt>SQLITE_DBCONFIG_DQS_DML</dt>
2716** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
2717** the legacy [double-quoted string literal] misfeature for DML statements
2718** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
2719** default value of this setting is determined by the [-DSQLITE_DQS]
2720** compile-time option.
2721** </dd>
2722**
2723** [[SQLITE_DBCONFIG_DQS_DDL]]
2724** <dt>SQLITE_DBCONFIG_DQS_DDL</dt>
2725** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
2726** the legacy [double-quoted string literal] misfeature for DDL statements,
2727** such as CREATE TABLE and CREATE INDEX. The
2728** default value of this setting is determined by the [-DSQLITE_DQS]
2729** compile-time option.
2730** </dd>
2731**
2732** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
2733** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</dt>
2734** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
2735** assume that database schemas are untainted by malicious content.
2736** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
2737** takes additional defensive steps to protect the application from harm
2738** including:
2739** <ul>
2740** <li> Prohibit the use of SQL functions inside triggers, views,
2741** CHECK constraints, DEFAULT clauses, expression indexes,
2742** partial indexes, or generated columns
2743** unless those functions are tagged with [SQLITE_INNOCUOUS].
2744** <li> Prohibit the use of virtual tables inside of triggers or views
2745** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
2746** </ul>
2747** This setting defaults to "on" for legacy compatibility, however
2748** all applications are advised to turn it off if possible. This setting
2749** can also be controlled using the [PRAGMA trusted_schema] statement.
2750** </dd>
2751**
2752** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
2753** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</dt>
2754** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
2755** the legacy file format flag. When activated, this flag causes all newly
2756** created database file to have a schema format version number (the 4-byte
2757** integer found at offset 44 into the database header) of 1. This in turn
2758** means that the resulting database file will be readable and writable by
2759** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting,
2760** newly created databases are generally not understandable by SQLite versions
2761** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there
2762** is now scarcely any need to generate database files that are compatible
2763** all the way back to version 3.0.0, and so this setting is of little
2764** practical use, but is provided so that SQLite can continue to claim the
2765** ability to generate new database files that are compatible with version
2766** 3.0.0.
2767** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
2768** the [VACUUM] command will fail with an obscure error when attempting to
2769** process a table with generated columns and a descending index. This is
2770** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2771** either generated columns or descending indexes.
2772** </dd>
2773**
2774** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]]
2775** <dt>SQLITE_DBCONFIG_STMT_SCANSTATUS</dt>
2776** <dd>The SQLITE_DBCONFIG_STMT_SCANSTATUS option is only useful in
2777** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears
2778** a flag that enables collection of the sqlite3_stmt_scanstatus_v2()
2779** statistics. For statistics to be collected, the flag must be set on
2780** the database handle both when the SQL statement is prepared and when it
2781** is stepped. The flag is set (collection of statistics is enabled)
2782** by default. This option takes two arguments: an integer and a pointer to
2783** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2784** leave unchanged the statement scanstatus option. If the second argument
2785** is not NULL, then the value of the statement scanstatus setting after
2786** processing the first argument is written into the integer that the second
2787** argument points to.
2788** </dd>
2789**
2790** [[SQLITE_DBCONFIG_REVERSE_SCANORDER]]
2791** <dt>SQLITE_DBCONFIG_REVERSE_SCANORDER</dt>
2792** <dd>The SQLITE_DBCONFIG_REVERSE_SCANORDER option changes the default order
2793** in which tables and indexes are scanned so that the scans start at the end
2794** and work toward the beginning rather than starting at the beginning and
2795** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the
2796** same as setting [PRAGMA reverse_unordered_selects]. This option takes
2797** two arguments which are an integer and a pointer to an integer. The first
2798** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2799** reverse scan order flag, respectively. If the second argument is not NULL,
2800** then 0 or 1 is written into the integer that the second argument points to
2801** depending on if the reverse scan order flag is set after processing the
2802** first argument.
2803** </dd>
2804**
2805** </dl>
2806*/
2807#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
2808#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
2809#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
2810#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
2811#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
2812#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
2813#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
2814#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
2815#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
2816#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
2817#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */
2818#define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */
2819#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012 /* int int* */
2820#define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */
2821#define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */
2822#define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */
2823#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */
2824#define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */
2825#define SQLITE_DBCONFIG_STMT_SCANSTATUS 1018 /* int int* */
2826#define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */
2827#define SQLITE_DBCONFIG_MAX 1019 /* Largest DBCONFIG */
2828
2829/*
2830** CAPI3REF: Enable Or Disable Extended Result Codes
2831** METHOD: sqlite3
2832**
2833** ^The sqlite3_extended_result_codes() routine enables or disables the
2834** [extended result codes] feature of SQLite. ^The extended result
2835** codes are disabled by default for historical compatibility.
2836*/
2837SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2838
2839/*
2840** CAPI3REF: Last Insert Rowid
2841** METHOD: sqlite3
2842**
2843** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
2844** has a unique 64-bit signed
2845** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2846** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2847** names are not also used by explicitly declared columns. ^If
2848** the table has a column of type [INTEGER PRIMARY KEY] then that column
2849** is another alias for the rowid.
2850**
2851** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2852** the most recent successful [INSERT] into a rowid table or [virtual table]
2853** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2854** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2855** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2856** zero.
2857**
2858** As well as being set automatically as rows are inserted into database
2859** tables, the value returned by this function may be set explicitly by
2860** [sqlite3_set_last_insert_rowid()]
2861**
2862** Some virtual table implementations may INSERT rows into rowid tables as
2863** part of committing a transaction (e.g. to flush data accumulated in memory
2864** to disk). In this case subsequent calls to this function return the rowid
2865** associated with these internal INSERT operations, which leads to
2866** unintuitive results. Virtual table implementations that do write to rowid
2867** tables in this way can avoid this problem by restoring the original
2868** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2869** control to the user.
2870**
2871** ^(If an [INSERT] occurs within a trigger then this routine will
2872** return the [rowid] of the inserted row as long as the trigger is
2873** running. Once the trigger program ends, the value returned
2874** by this routine reverts to what it was before the trigger was fired.)^
2875**
2876** ^An [INSERT] that fails due to a constraint violation is not a
2877** successful [INSERT] and does not change the value returned by this
2878** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2879** and INSERT OR ABORT make no changes to the return value of this
2880** routine when their insertion fails. ^(When INSERT OR REPLACE
2881** encounters a constraint violation, it does not fail. The
2882** INSERT continues to completion after deleting rows that caused
2883** the constraint problem so INSERT OR REPLACE will always change
2884** the return value of this interface.)^
2885**
2886** ^For the purposes of this routine, an [INSERT] is considered to
2887** be successful even if it is subsequently rolled back.
2888**
2889** This function is accessible to SQL statements via the
2890** [last_insert_rowid() SQL function].
2891**
2892** If a separate thread performs a new [INSERT] on the same
2893** database connection while the [sqlite3_last_insert_rowid()]
2894** function is running and thus changes the last insert [rowid],
2895** then the value returned by [sqlite3_last_insert_rowid()] is
2896** unpredictable and might not equal either the old or the new
2897** last insert [rowid].
2898*/
2899SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2900
2901/*
2902** CAPI3REF: Set the Last Insert Rowid value.
2903** METHOD: sqlite3
2904**
2905** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2906** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2907** without inserting a row into the database.
2908*/
2909SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2910
2911/*
2912** CAPI3REF: Count The Number Of Rows Modified
2913** METHOD: sqlite3
2914**
2915** ^These functions return the number of rows modified, inserted or
2916** deleted by the most recently completed INSERT, UPDATE or DELETE
2917** statement on the database connection specified by the only parameter.
2918** The two functions are identical except for the type of the return value
2919** and that if the number of rows modified by the most recent INSERT, UPDATE
2920** or DELETE is greater than the maximum value supported by type "int", then
2921** the return value of sqlite3_changes() is undefined. ^Executing any other
2922** type of SQL statement does not modify the value returned by these functions.
2923**
2924** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2925** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2926** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2927**
2928** Changes to a view that are intercepted by
2929** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2930** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2931** DELETE statement run on a view is always zero. Only changes made to real
2932** tables are counted.
2933**
2934** Things are more complicated if the sqlite3_changes() function is
2935** executed while a trigger program is running. This may happen if the
2936** program uses the [changes() SQL function], or if some other callback
2937** function invokes sqlite3_changes() directly. Essentially:
2938**
2939** <ul>
2940** <li> ^(Before entering a trigger program the value returned by
2941** sqlite3_changes() function is saved. After the trigger program
2942** has finished, the original value is restored.)^
2943**
2944** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2945** statement sets the value returned by sqlite3_changes()
2946** upon completion as normal. Of course, this value will not include
2947** any changes performed by sub-triggers, as the sqlite3_changes()
2948** value will be saved and restored after each sub-trigger has run.)^
2949** </ul>
2950**
2951** ^This means that if the changes() SQL function (or similar) is used
2952** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2953** returns the value as set when the calling statement began executing.
2954** ^If it is used by the second or subsequent such statement within a trigger
2955** program, the value returned reflects the number of rows modified by the
2956** previous INSERT, UPDATE or DELETE statement within the same trigger.
2957**
2958** If a separate thread makes changes on the same database connection
2959** while [sqlite3_changes()] is running then the value returned
2960** is unpredictable and not meaningful.
2961**
2962** See also:
2963** <ul>
2964** <li> the [sqlite3_total_changes()] interface
2965** <li> the [count_changes pragma]
2966** <li> the [changes() SQL function]
2967** <li> the [data_version pragma]
2968** </ul>
2969*/
2970SQLITE_API int sqlite3_changes(sqlite3*);
2971SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3*);
2972
2973/*
2974** CAPI3REF: Total Number Of Rows Modified
2975** METHOD: sqlite3
2976**
2977** ^These functions return the total number of rows inserted, modified or
2978** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2979** since the database connection was opened, including those executed as
2980** part of trigger programs. The two functions are identical except for the
2981** type of the return value and that if the number of rows modified by the
2982** connection exceeds the maximum value supported by type "int", then
2983** the return value of sqlite3_total_changes() is undefined. ^Executing
2984** any other type of SQL statement does not affect the value returned by
2985** sqlite3_total_changes().
2986**
2987** ^Changes made as part of [foreign key actions] are included in the
2988** count, but those made as part of REPLACE constraint resolution are
2989** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2990** are not counted.
2991**
2992** The [sqlite3_total_changes(D)] interface only reports the number
2993** of rows that changed due to SQL statement run against database
2994** connection D. Any changes by other database connections are ignored.
2995** To detect changes against a database file from other database
2996** connections use the [PRAGMA data_version] command or the
2997** [SQLITE_FCNTL_DATA_VERSION] [file control].
2998**
2999** If a separate thread makes changes on the same database connection
3000** while [sqlite3_total_changes()] is running then the value
3001** returned is unpredictable and not meaningful.
3002**
3003** See also:
3004** <ul>
3005** <li> the [sqlite3_changes()] interface
3006** <li> the [count_changes pragma]
3007** <li> the [changes() SQL function]
3008** <li> the [data_version pragma]
3009** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3010** </ul>
3011*/
3012SQLITE_API int sqlite3_total_changes(sqlite3*);
3013SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
3014
3015/*
3016** CAPI3REF: Interrupt A Long-Running Query
3017** METHOD: sqlite3
3018**
3019** ^This function causes any pending database operation to abort and
3020** return at its earliest opportunity. This routine is typically
3021** called in response to a user action such as pressing "Cancel"
3022** or Ctrl-C where the user wants a long query operation to halt
3023** immediately.
3024**
3025** ^It is safe to call this routine from a thread different from the
3026** thread that is currently running the database operation. But it
3027** is not safe to call this routine with a [database connection] that
3028** is closed or might close before sqlite3_interrupt() returns.
3029**
3030** ^If an SQL operation is very nearly finished at the time when
3031** sqlite3_interrupt() is called, then it might not have an opportunity
3032** to be interrupted and might continue to completion.
3033**
3034** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3035** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3036** that is inside an explicit transaction, then the entire transaction
3037** will be rolled back automatically.
3038**
3039** ^The sqlite3_interrupt(D) call is in effect until all currently running
3040** SQL statements on [database connection] D complete. ^Any new SQL statements
3041** that are started after the sqlite3_interrupt() call and before the
3042** running statement count reaches zero are interrupted as if they had been
3043** running prior to the sqlite3_interrupt() call. ^New SQL statements
3044** that are started after the running statement count reaches zero are
3045** not effected by the sqlite3_interrupt().
3046** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3047** SQL statements is a no-op and has no effect on SQL statements
3048** that are started after the sqlite3_interrupt() call returns.
3049**
3050** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether
3051** or not an interrupt is currently in effect for [database connection] D.
3052** It returns 1 if an interrupt is currently in effect, or 0 otherwise.
3053*/
3054SQLITE_API void sqlite3_interrupt(sqlite3*);
3055SQLITE_API int sqlite3_is_interrupted(sqlite3*);
3056
3057/*
3058** CAPI3REF: Determine If An SQL Statement Is Complete
3059**
3060** These routines are useful during command-line input to determine if the
3061** currently entered text seems to form a complete SQL statement or
3062** if additional input is needed before sending the text into
3063** SQLite for parsing. ^These routines return 1 if the input string
3064** appears to be a complete SQL statement. ^A statement is judged to be
3065** complete if it ends with a semicolon token and is not a prefix of a
3066** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3067** string literals or quoted identifier names or comments are not
3068** independent tokens (they are part of the token in which they are
3069** embedded) and thus do not count as a statement terminator. ^Whitespace
3070** and comments that follow the final semicolon are ignored.
3071**
3072** ^These routines return 0 if the statement is incomplete. ^If a
3073** memory allocation fails, then SQLITE_NOMEM is returned.
3074**
3075** ^These routines do not parse the SQL statements thus
3076** will not detect syntactically incorrect SQL.
3077**
3078** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3079** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3080** automatically by sqlite3_complete16(). If that initialization fails,
3081** then the return value from sqlite3_complete16() will be non-zero
3082** regardless of whether or not the input SQL is complete.)^
3083**
3084** The input to [sqlite3_complete()] must be a zero-terminated
3085** UTF-8 string.
3086**
3087** The input to [sqlite3_complete16()] must be a zero-terminated
3088** UTF-16 string in native byte order.
3089*/
3090SQLITE_API int sqlite3_complete(const char *sql);
3091SQLITE_API int sqlite3_complete16(const void *sql);
3092
3093/*
3094** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3095** KEYWORDS: {busy-handler callback} {busy handler}
3096** METHOD: sqlite3
3097**
3098** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3099** that might be invoked with argument P whenever
3100** an attempt is made to access a database table associated with
3101** [database connection] D when another thread
3102** or process has the table locked.
3103** The sqlite3_busy_handler() interface is used to implement
3104** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3105**
3106** ^If the busy callback is NULL, then [SQLITE_BUSY]
3107** is returned immediately upon encountering the lock. ^If the busy callback
3108** is not NULL, then the callback might be invoked with two arguments.
3109**
3110** ^The first argument to the busy handler is a copy of the void* pointer which
3111** is the third argument to sqlite3_busy_handler(). ^The second argument to
3112** the busy handler callback is the number of times that the busy handler has
3113** been invoked previously for the same locking event. ^If the
3114** busy callback returns 0, then no additional attempts are made to
3115** access the database and [SQLITE_BUSY] is returned
3116** to the application.
3117** ^If the callback returns non-zero, then another attempt
3118** is made to access the database and the cycle repeats.
3119**
3120** The presence of a busy handler does not guarantee that it will be invoked
3121** when there is lock contention. ^If SQLite determines that invoking the busy
3122** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3123** to the application instead of invoking the
3124** busy handler.
3125** Consider a scenario where one process is holding a read lock that
3126** it is trying to promote to a reserved lock and
3127** a second process is holding a reserved lock that it is trying
3128** to promote to an exclusive lock. The first process cannot proceed
3129** because it is blocked by the second and the second process cannot
3130** proceed because it is blocked by the first. If both processes
3131** invoke the busy handlers, neither will make any progress. Therefore,
3132** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3133** will induce the first process to release its read lock and allow
3134** the second process to proceed.
3135**
3136** ^The default busy callback is NULL.
3137**
3138** ^(There can only be a single busy handler defined for each
3139** [database connection]. Setting a new busy handler clears any
3140** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3141** or evaluating [PRAGMA busy_timeout=N] will change the
3142** busy handler and thus clear any previously set busy handler.
3143**
3144** The busy callback should not take any actions which modify the
3145** database connection that invoked the busy handler. In other words,
3146** the busy handler is not reentrant. Any such actions
3147** result in undefined behavior.
3148**
3149** A busy handler must not close the database connection
3150** or [prepared statement] that invoked the busy handler.
3151*/
3152SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3153
3154/*
3155** CAPI3REF: Set A Busy Timeout
3156** METHOD: sqlite3
3157**
3158** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3159** for a specified amount of time when a table is locked. ^The handler
3160** will sleep multiple times until at least "ms" milliseconds of sleeping
3161** have accumulated. ^After at least "ms" milliseconds of sleeping,
3162** the handler returns 0 which causes [sqlite3_step()] to return
3163** [SQLITE_BUSY].
3164**
3165** ^Calling this routine with an argument less than or equal to zero
3166** turns off all busy handlers.
3167**
3168** ^(There can only be a single busy handler for a particular
3169** [database connection] at any given moment. If another busy handler
3170** was defined (using [sqlite3_busy_handler()]) prior to calling
3171** this routine, that other busy handler is cleared.)^
3172**
3173** See also: [PRAGMA busy_timeout]
3174*/
3175SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3176
3177/*
3178** CAPI3REF: Convenience Routines For Running Queries
3179** METHOD: sqlite3
3180**
3181** This is a legacy interface that is preserved for backwards compatibility.
3182** Use of this interface is not recommended.
3183**
3184** Definition: A <b>result table</b> is memory data structure created by the
3185** [sqlite3_get_table()] interface. A result table records the
3186** complete query results from one or more queries.
3187**
3188** The table conceptually has a number of rows and columns. But
3189** these numbers are not part of the result table itself. These
3190** numbers are obtained separately. Let N be the number of rows
3191** and M be the number of columns.
3192**
3193** A result table is an array of pointers to zero-terminated UTF-8 strings.
3194** There are (N+1)*M elements in the array. The first M pointers point
3195** to zero-terminated strings that contain the names of the columns.
3196** The remaining entries all point to query results. NULL values result
3197** in NULL pointers. All other values are in their UTF-8 zero-terminated
3198** string representation as returned by [sqlite3_column_text()].
3199**
3200** A result table might consist of one or more memory allocations.
3201** It is not safe to pass a result table directly to [sqlite3_free()].
3202** A result table should be deallocated using [sqlite3_free_table()].
3203**
3204** ^(As an example of the result table format, suppose a query result
3205** is as follows:
3206**
3207** <blockquote><pre>
3208** Name | Age
3209** -----------------------
3210** Alice | 43
3211** Bob | 28
3212** Cindy | 21
3213** </pre></blockquote>
3214**
3215** There are two columns (M==2) and three rows (N==3). Thus the
3216** result table has 8 entries. Suppose the result table is stored
3217** in an array named azResult. Then azResult holds this content:
3218**
3219** <blockquote><pre>
3220** azResult&#91;0] = "Name";
3221** azResult&#91;1] = "Age";
3222** azResult&#91;2] = "Alice";
3223** azResult&#91;3] = "43";
3224** azResult&#91;4] = "Bob";
3225** azResult&#91;5] = "28";
3226** azResult&#91;6] = "Cindy";
3227** azResult&#91;7] = "21";
3228** </pre></blockquote>)^
3229**
3230** ^The sqlite3_get_table() function evaluates one or more
3231** semicolon-separated SQL statements in the zero-terminated UTF-8
3232** string of its 2nd parameter and returns a result table to the
3233** pointer given in its 3rd parameter.
3234**
3235** After the application has finished with the result from sqlite3_get_table(),
3236** it must pass the result table pointer to sqlite3_free_table() in order to
3237** release the memory that was malloced. Because of the way the
3238** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3239** function must not try to call [sqlite3_free()] directly. Only
3240** [sqlite3_free_table()] is able to release the memory properly and safely.
3241**
3242** The sqlite3_get_table() interface is implemented as a wrapper around
3243** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3244** to any internal data structures of SQLite. It uses only the public
3245** interface defined here. As a consequence, errors that occur in the
3246** wrapper layer outside of the internal [sqlite3_exec()] call are not
3247** reflected in subsequent calls to [sqlite3_errcode()] or
3248** [sqlite3_errmsg()].
3249*/
3250SQLITE_API int sqlite3_get_table(
3251 sqlite3 *db, /* An open database */
3252 const char *zSql, /* SQL to be evaluated */
3253 char ***pazResult, /* Results of the query */
3254 int *pnRow, /* Number of result rows written here */
3255 int *pnColumn, /* Number of result columns written here */
3256 char **pzErrmsg /* Error msg written here */
3257);
3258SQLITE_API void sqlite3_free_table(char **result);
3259
3260/*
3261** CAPI3REF: Formatted String Printing Functions
3262**
3263** These routines are work-alikes of the "printf()" family of functions
3264** from the standard C library.
3265** These routines understand most of the common formatting options from
3266** the standard library printf()
3267** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3268** See the [built-in printf()] documentation for details.
3269**
3270** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3271** results into memory obtained from [sqlite3_malloc64()].
3272** The strings returned by these two routines should be
3273** released by [sqlite3_free()]. ^Both routines return a
3274** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3275** memory to hold the resulting string.
3276**
3277** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3278** the standard C library. The result is written into the
3279** buffer supplied as the second parameter whose size is given by
3280** the first parameter. Note that the order of the
3281** first two parameters is reversed from snprintf().)^ This is an
3282** historical accident that cannot be fixed without breaking
3283** backwards compatibility. ^(Note also that sqlite3_snprintf()
3284** returns a pointer to its buffer instead of the number of
3285** characters actually written into the buffer.)^ We admit that
3286** the number of characters written would be a more useful return
3287** value but we cannot change the implementation of sqlite3_snprintf()
3288** now without breaking compatibility.
3289**
3290** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3291** guarantees that the buffer is always zero-terminated. ^The first
3292** parameter "n" is the total size of the buffer, including space for
3293** the zero terminator. So the longest string that can be completely
3294** written will be n-1 characters.
3295**
3296** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3297**
3298** See also: [built-in printf()], [printf() SQL function]
3299*/
3300SQLITE_API char *sqlite3_mprintf(const char*,...);
3301SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3302SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3303SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3304
3305/*
3306** CAPI3REF: Memory Allocation Subsystem
3307**
3308** The SQLite core uses these three routines for all of its own
3309** internal memory allocation needs. "Core" in the previous sentence
3310** does not include operating-system specific [VFS] implementation. The
3311** Windows VFS uses native malloc() and free() for some operations.
3312**
3313** ^The sqlite3_malloc() routine returns a pointer to a block
3314** of memory at least N bytes in length, where N is the parameter.
3315** ^If sqlite3_malloc() is unable to obtain sufficient free
3316** memory, it returns a NULL pointer. ^If the parameter N to
3317** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3318** a NULL pointer.
3319**
3320** ^The sqlite3_malloc64(N) routine works just like
3321** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3322** of a signed 32-bit integer.
3323**
3324** ^Calling sqlite3_free() with a pointer previously returned
3325** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3326** that it might be reused. ^The sqlite3_free() routine is
3327** a no-op if is called with a NULL pointer. Passing a NULL pointer
3328** to sqlite3_free() is harmless. After being freed, memory
3329** should neither be read nor written. Even reading previously freed
3330** memory might result in a segmentation fault or other severe error.
3331** Memory corruption, a segmentation fault, or other severe error
3332** might result if sqlite3_free() is called with a non-NULL pointer that
3333** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3334**
3335** ^The sqlite3_realloc(X,N) interface attempts to resize a
3336** prior memory allocation X to be at least N bytes.
3337** ^If the X parameter to sqlite3_realloc(X,N)
3338** is a NULL pointer then its behavior is identical to calling
3339** sqlite3_malloc(N).
3340** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3341** negative then the behavior is exactly the same as calling
3342** sqlite3_free(X).
3343** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3344** of at least N bytes in size or NULL if insufficient memory is available.
3345** ^If M is the size of the prior allocation, then min(N,M) bytes
3346** of the prior allocation are copied into the beginning of buffer returned
3347** by sqlite3_realloc(X,N) and the prior allocation is freed.
3348** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3349** prior allocation is not freed.
3350**
3351** ^The sqlite3_realloc64(X,N) interfaces works the same as
3352** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3353** of a 32-bit signed integer.
3354**
3355** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3356** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3357** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3358** ^The value returned by sqlite3_msize(X) might be larger than the number
3359** of bytes requested when X was allocated. ^If X is a NULL pointer then
3360** sqlite3_msize(X) returns zero. If X points to something that is not
3361** the beginning of memory allocation, or if it points to a formerly
3362** valid memory allocation that has now been freed, then the behavior
3363** of sqlite3_msize(X) is undefined and possibly harmful.
3364**
3365** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3366** sqlite3_malloc64(), and sqlite3_realloc64()
3367** is always aligned to at least an 8 byte boundary, or to a
3368** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3369** option is used.
3370**
3371** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3372** must be either NULL or else pointers obtained from a prior
3373** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3374** not yet been released.
3375**
3376** The application must not read or write any part of
3377** a block of memory after it has been released using
3378** [sqlite3_free()] or [sqlite3_realloc()].
3379*/
3380SQLITE_API void *sqlite3_malloc(int);
3381SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3382SQLITE_API void *sqlite3_realloc(void*, int);
3383SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3384SQLITE_API void sqlite3_free(void*);
3385SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3386
3387/*
3388** CAPI3REF: Memory Allocator Statistics
3389**
3390** SQLite provides these two interfaces for reporting on the status
3391** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3392** routines, which form the built-in memory allocation subsystem.
3393**
3394** ^The [sqlite3_memory_used()] routine returns the number of bytes
3395** of memory currently outstanding (malloced but not freed).
3396** ^The [sqlite3_memory_highwater()] routine returns the maximum
3397** value of [sqlite3_memory_used()] since the high-water mark
3398** was last reset. ^The values returned by [sqlite3_memory_used()] and
3399** [sqlite3_memory_highwater()] include any overhead
3400** added by SQLite in its implementation of [sqlite3_malloc()],
3401** but not overhead added by the any underlying system library
3402** routines that [sqlite3_malloc()] may call.
3403**
3404** ^The memory high-water mark is reset to the current value of
3405** [sqlite3_memory_used()] if and only if the parameter to
3406** [sqlite3_memory_highwater()] is true. ^The value returned
3407** by [sqlite3_memory_highwater(1)] is the high-water mark
3408** prior to the reset.
3409*/
3410SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3411SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3412
3413/*
3414** CAPI3REF: Pseudo-Random Number Generator
3415**
3416** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3417** select random [ROWID | ROWIDs] when inserting new records into a table that
3418** already uses the largest possible [ROWID]. The PRNG is also used for
3419** the built-in random() and randomblob() SQL functions. This interface allows
3420** applications to access the same PRNG for other purposes.
3421**
3422** ^A call to this routine stores N bytes of randomness into buffer P.
3423** ^The P parameter can be a NULL pointer.
3424**
3425** ^If this routine has not been previously called or if the previous
3426** call had N less than one or a NULL pointer for P, then the PRNG is
3427** seeded using randomness obtained from the xRandomness method of
3428** the default [sqlite3_vfs] object.
3429** ^If the previous call to this routine had an N of 1 or more and a
3430** non-NULL P then the pseudo-randomness is generated
3431** internally and without recourse to the [sqlite3_vfs] xRandomness
3432** method.
3433*/
3434SQLITE_API void sqlite3_randomness(int N, void *P);
3435
3436/*
3437** CAPI3REF: Compile-Time Authorization Callbacks
3438** METHOD: sqlite3
3439** KEYWORDS: {authorizer callback}
3440**
3441** ^This routine registers an authorizer callback with a particular
3442** [database connection], supplied in the first argument.
3443** ^The authorizer callback is invoked as SQL statements are being compiled
3444** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3445** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3446** and [sqlite3_prepare16_v3()]. ^At various
3447** points during the compilation process, as logic is being created
3448** to perform various actions, the authorizer callback is invoked to
3449** see if those actions are allowed. ^The authorizer callback should
3450** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3451** specific action but allow the SQL statement to continue to be
3452** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3453** rejected with an error. ^If the authorizer callback returns
3454** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3455** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3456** the authorizer will fail with an error message.
3457**
3458** When the callback returns [SQLITE_OK], that means the operation
3459** requested is ok. ^When the callback returns [SQLITE_DENY], the
3460** [sqlite3_prepare_v2()] or equivalent call that triggered the
3461** authorizer will fail with an error message explaining that
3462** access is denied.
3463**
3464** ^The first parameter to the authorizer callback is a copy of the third
3465** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3466** to the callback is an integer [SQLITE_COPY | action code] that specifies
3467** the particular action to be authorized. ^The third through sixth parameters
3468** to the callback are either NULL pointers or zero-terminated strings
3469** that contain additional details about the action to be authorized.
3470** Applications must always be prepared to encounter a NULL pointer in any
3471** of the third through the sixth parameters of the authorization callback.
3472**
3473** ^If the action code is [SQLITE_READ]
3474** and the callback returns [SQLITE_IGNORE] then the
3475** [prepared statement] statement is constructed to substitute
3476** a NULL value in place of the table column that would have
3477** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3478** return can be used to deny an untrusted user access to individual
3479** columns of a table.
3480** ^When a table is referenced by a [SELECT] but no column values are
3481** extracted from that table (for example in a query like
3482** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3483** is invoked once for that table with a column name that is an empty string.
3484** ^If the action code is [SQLITE_DELETE] and the callback returns
3485** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3486** [truncate optimization] is disabled and all rows are deleted individually.
3487**
3488** An authorizer is used when [sqlite3_prepare | preparing]
3489** SQL statements from an untrusted source, to ensure that the SQL statements
3490** do not try to access data they are not allowed to see, or that they do not
3491** try to execute malicious statements that damage the database. For
3492** example, an application may allow a user to enter arbitrary
3493** SQL queries for evaluation by a database. But the application does
3494** not want the user to be able to make arbitrary changes to the
3495** database. An authorizer could then be put in place while the
3496** user-entered SQL is being [sqlite3_prepare | prepared] that
3497** disallows everything except [SELECT] statements.
3498**
3499** Applications that need to process SQL from untrusted sources
3500** might also consider lowering resource limits using [sqlite3_limit()]
3501** and limiting database size using the [max_page_count] [PRAGMA]
3502** in addition to using an authorizer.
3503**
3504** ^(Only a single authorizer can be in place on a database connection
3505** at a time. Each call to sqlite3_set_authorizer overrides the
3506** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3507** The authorizer is disabled by default.
3508**
3509** The authorizer callback must not do anything that will modify
3510** the database connection that invoked the authorizer callback.
3511** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3512** database connections for the meaning of "modify" in this paragraph.
3513**
3514** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3515** statement might be re-prepared during [sqlite3_step()] due to a
3516** schema change. Hence, the application should ensure that the
3517** correct authorizer callback remains in place during the [sqlite3_step()].
3518**
3519** ^Note that the authorizer callback is invoked only during
3520** [sqlite3_prepare()] or its variants. Authorization is not
3521** performed during statement evaluation in [sqlite3_step()], unless
3522** as stated in the previous paragraph, sqlite3_step() invokes
3523** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3524*/
3525SQLITE_API int sqlite3_set_authorizer(
3526 sqlite3*,
3527 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3528 void *pUserData
3529);
3530
3531/*
3532** CAPI3REF: Authorizer Return Codes
3533**
3534** The [sqlite3_set_authorizer | authorizer callback function] must
3535** return either [SQLITE_OK] or one of these two constants in order
3536** to signal SQLite whether or not the action is permitted. See the
3537** [sqlite3_set_authorizer | authorizer documentation] for additional
3538** information.
3539**
3540** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3541** returned from the [sqlite3_vtab_on_conflict()] interface.
3542*/
3543#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
3544#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
3545
3546/*
3547** CAPI3REF: Authorizer Action Codes
3548**
3549** The [sqlite3_set_authorizer()] interface registers a callback function
3550** that is invoked to authorize certain SQL statement actions. The
3551** second parameter to the callback is an integer code that specifies
3552** what action is being authorized. These are the integer action codes that
3553** the authorizer callback may be passed.
3554**
3555** These action code values signify what kind of operation is to be
3556** authorized. The 3rd and 4th parameters to the authorization
3557** callback function will be parameters or NULL depending on which of these
3558** codes is used as the second parameter. ^(The 5th parameter to the
3559** authorizer callback is the name of the database ("main", "temp",
3560** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3561** is the name of the inner-most trigger or view that is responsible for
3562** the access attempt or NULL if this access attempt is directly from
3563** top-level SQL code.
3564*/
3565/******************************************* 3rd ************ 4th ***********/
3566#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
3567#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
3568#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
3569#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
3570#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
3571#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
3572#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
3573#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
3574#define SQLITE_DELETE 9 /* Table Name NULL */
3575#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
3576#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
3577#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
3578#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
3579#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
3580#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
3581#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
3582#define SQLITE_DROP_VIEW 17 /* View Name NULL */
3583#define SQLITE_INSERT 18 /* Table Name NULL */
3584#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
3585#define SQLITE_READ 20 /* Table Name Column Name */
3586#define SQLITE_SELECT 21 /* NULL NULL */
3587#define SQLITE_TRANSACTION 22 /* Operation NULL */
3588#define SQLITE_UPDATE 23 /* Table Name Column Name */
3589#define SQLITE_ATTACH 24 /* Filename NULL */
3590#define SQLITE_DETACH 25 /* Database Name NULL */
3591#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
3592#define SQLITE_REINDEX 27 /* Index Name NULL */
3593#define SQLITE_ANALYZE 28 /* Table Name NULL */
3594#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
3595#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
3596#define SQLITE_FUNCTION 31 /* NULL Function Name */
3597#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
3598#define SQLITE_COPY 0 /* No longer used */
3599#define SQLITE_RECURSIVE 33 /* NULL NULL */
3600
3601/*
3602** CAPI3REF: Tracing And Profiling Functions
3603** METHOD: sqlite3
3604**
3605** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3606** instead of the routines described here.
3607**
3608** These routines register callback functions that can be used for
3609** tracing and profiling the execution of SQL statements.
3610**
3611** ^The callback function registered by sqlite3_trace() is invoked at
3612** various times when an SQL statement is being run by [sqlite3_step()].
3613** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3614** SQL statement text as the statement first begins executing.
3615** ^(Additional sqlite3_trace() callbacks might occur
3616** as each triggered subprogram is entered. The callbacks for triggers
3617** contain a UTF-8 SQL comment that identifies the trigger.)^
3618**
3619** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3620** the length of [bound parameter] expansion in the output of sqlite3_trace().
3621**
3622** ^The callback function registered by sqlite3_profile() is invoked
3623** as each SQL statement finishes. ^The profile callback contains
3624** the original statement text and an estimate of wall-clock time
3625** of how long that statement took to run. ^The profile callback
3626** time is in units of nanoseconds, however the current implementation
3627** is only capable of millisecond resolution so the six least significant
3628** digits in the time are meaningless. Future versions of SQLite
3629** might provide greater resolution on the profiler callback. Invoking
3630** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
3631** profile callback.
3632*/
3633SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
3634 void(*xTrace)(void*,const char*), void*);
3635SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3636 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
3637
3638/*
3639** CAPI3REF: SQL Trace Event Codes
3640** KEYWORDS: SQLITE_TRACE
3641**
3642** These constants identify classes of events that can be monitored
3643** using the [sqlite3_trace_v2()] tracing logic. The M argument
3644** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3645** the following constants. ^The first argument to the trace callback
3646** is one of the following constants.
3647**
3648** New tracing constants may be added in future releases.
3649**
3650** ^A trace callback has four arguments: xCallback(T,C,P,X).
3651** ^The T argument is one of the integer type codes above.
3652** ^The C argument is a copy of the context pointer passed in as the
3653** fourth argument to [sqlite3_trace_v2()].
3654** The P and X arguments are pointers whose meanings depend on T.
3655**
3656** <dl>
3657** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
3658** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3659** first begins running and possibly at other times during the
3660** execution of the prepared statement, such as at the start of each
3661** trigger subprogram. ^The P argument is a pointer to the
3662** [prepared statement]. ^The X argument is a pointer to a string which
3663** is the unexpanded SQL text of the prepared statement or an SQL comment
3664** that indicates the invocation of a trigger. ^The callback can compute
3665** the same text that would have been returned by the legacy [sqlite3_trace()]
3666** interface by using the X argument when X begins with "--" and invoking
3667** [sqlite3_expanded_sql(P)] otherwise.
3668**
3669** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
3670** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
3671** information as is provided by the [sqlite3_profile()] callback.
3672** ^The P argument is a pointer to the [prepared statement] and the
3673** X argument points to a 64-bit integer which is approximately
3674** the number of nanoseconds that the prepared statement took to run.
3675** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
3676**
3677** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
3678** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3679** statement generates a single row of result.
3680** ^The P argument is a pointer to the [prepared statement] and the
3681** X argument is unused.
3682**
3683** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
3684** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3685** connection closes.
3686** ^The P argument is a pointer to the [database connection] object
3687** and the X argument is unused.
3688** </dl>
3689*/
3690#define SQLITE_TRACE_STMT 0x01
3691#define SQLITE_TRACE_PROFILE 0x02
3692#define SQLITE_TRACE_ROW 0x04
3693#define SQLITE_TRACE_CLOSE 0x08
3694
3695/*
3696** CAPI3REF: SQL Trace Hook
3697** METHOD: sqlite3
3698**
3699** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3700** function X against [database connection] D, using property mask M
3701** and context pointer P. ^If the X callback is
3702** NULL or if the M mask is zero, then tracing is disabled. The
3703** M argument should be the bitwise OR-ed combination of
3704** zero or more [SQLITE_TRACE] constants.
3705**
3706** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P)
3707** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or
3708** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each
3709** database connection may have at most one trace callback.
3710**
3711** ^The X callback is invoked whenever any of the events identified by
3712** mask M occur. ^The integer return value from the callback is currently
3713** ignored, though this may change in future releases. Callback
3714** implementations should return zero to ensure future compatibility.
3715**
3716** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3717** ^The T argument is one of the [SQLITE_TRACE]
3718** constants to indicate why the callback was invoked.
3719** ^The C argument is a copy of the context pointer.
3720** The P and X arguments are pointers whose meanings depend on T.
3721**
3722** The sqlite3_trace_v2() interface is intended to replace the legacy
3723** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
3724** are deprecated.
3725*/
3726SQLITE_API int sqlite3_trace_v2(
3727 sqlite3*,
3728 unsigned uMask,
3729 int(*xCallback)(unsigned,void*,void*,void*),
3730 void *pCtx
3731);
3732
3733/*
3734** CAPI3REF: Query Progress Callbacks
3735** METHOD: sqlite3
3736**
3737** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
3738** function X to be invoked periodically during long running calls to
3739** [sqlite3_step()] and [sqlite3_prepare()] and similar for
3740** database connection D. An example use for this
3741** interface is to keep a GUI updated during a large query.
3742**
3743** ^The parameter P is passed through as the only parameter to the
3744** callback function X. ^The parameter N is the approximate number of
3745** [virtual machine instructions] that are evaluated between successive
3746** invocations of the callback X. ^If N is less than one then the progress
3747** handler is disabled.
3748**
3749** ^Only a single progress handler may be defined at one time per
3750** [database connection]; setting a new progress handler cancels the
3751** old one. ^Setting parameter X to NULL disables the progress handler.
3752** ^The progress handler is also disabled by setting N to a value less
3753** than 1.
3754**
3755** ^If the progress callback returns non-zero, the operation is
3756** interrupted. This feature can be used to implement a
3757** "Cancel" button on a GUI progress dialog box.
3758**
3759** The progress handler callback must not do anything that will modify
3760** the database connection that invoked the progress handler.
3761** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3762** database connections for the meaning of "modify" in this paragraph.
3763**
3764** The progress handler callback would originally only be invoked from the
3765** bytecode engine. It still might be invoked during [sqlite3_prepare()]
3766** and similar because those routines might force a reparse of the schema
3767** which involves running the bytecode engine. However, beginning with
3768** SQLite version 3.41.0, the progress handler callback might also be
3769** invoked directly from [sqlite3_prepare()] while analyzing and generating
3770** code for complex queries.
3771*/
3772SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3773
3774/*
3775** CAPI3REF: Opening A New Database Connection
3776** CONSTRUCTOR: sqlite3
3777**
3778** ^These routines open an SQLite database file as specified by the
3779** filename argument. ^The filename argument is interpreted as UTF-8 for
3780** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3781** order for sqlite3_open16(). ^(A [database connection] handle is usually
3782** returned in *ppDb, even if an error occurs. The only exception is that
3783** if SQLite is unable to allocate memory to hold the [sqlite3] object,
3784** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
3785** object.)^ ^(If the database is opened (and/or created) successfully, then
3786** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
3787** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
3788** an English language description of the error following a failure of any
3789** of the sqlite3_open() routines.
3790**
3791** ^The default encoding will be UTF-8 for databases created using
3792** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
3793** created using sqlite3_open16() will be UTF-16 in the native byte order.
3794**
3795** Whether or not an error occurs when it is opened, resources
3796** associated with the [database connection] handle should be released by
3797** passing it to [sqlite3_close()] when it is no longer required.
3798**
3799** The sqlite3_open_v2() interface works like sqlite3_open()
3800** except that it accepts two additional parameters for additional control
3801** over the new database connection. ^(The flags parameter to
3802** sqlite3_open_v2() must include, at a minimum, one of the following
3803** three flag combinations:)^
3804**
3805** <dl>
3806** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
3807** <dd>The database is opened in read-only mode. If the database does
3808** not already exist, an error is returned.</dd>)^
3809**
3810** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
3811** <dd>The database is opened for reading and writing if possible, or
3812** reading only if the file is write protected by the operating
3813** system. In either case the database must already exist, otherwise
3814** an error is returned. For historical reasons, if opening in
3815** read-write mode fails due to OS-level permissions, an attempt is
3816** made to open it in read-only mode. [sqlite3_db_readonly()] can be
3817** used to determine whether the database is actually
3818** read-write.</dd>)^
3819**
3820** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
3821** <dd>The database is opened for reading and writing, and is created if
3822** it does not already exist. This is the behavior that is always used for
3823** sqlite3_open() and sqlite3_open16().</dd>)^
3824** </dl>
3825**
3826** In addition to the required flags, the following optional flags are
3827** also supported:
3828**
3829** <dl>
3830** ^(<dt>[SQLITE_OPEN_URI]</dt>
3831** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
3832**
3833** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
3834** <dd>The database will be opened as an in-memory database. The database
3835** is named by the "filename" argument for the purposes of cache-sharing,
3836** if shared cache mode is enabled, but the "filename" is otherwise ignored.
3837** </dd>)^
3838**
3839** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
3840** <dd>The new database connection will use the "multi-thread"
3841** [threading mode].)^ This means that separate threads are allowed
3842** to use SQLite at the same time, as long as each thread is using
3843** a different [database connection].
3844**
3845** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
3846** <dd>The new database connection will use the "serialized"
3847** [threading mode].)^ This means the multiple threads can safely
3848** attempt to use the same database connection at the same time.
3849** (Mutexes will block any actual concurrency, but in this mode
3850** there is no harm in trying.)
3851**
3852** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3853** <dd>The database is opened [shared cache] enabled, overriding
3854** the default shared cache setting provided by
3855** [sqlite3_enable_shared_cache()].)^
3856** The [use of shared cache mode is discouraged] and hence shared cache
3857** capabilities may be omitted from many builds of SQLite. In such cases,
3858** this option is a no-op.
3859**
3860** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3861** <dd>The database is opened [shared cache] disabled, overriding
3862** the default shared cache setting provided by
3863** [sqlite3_enable_shared_cache()].)^
3864**
3865** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt>
3866** <dd>The database connection comes up in "extended result code mode".
3867** In other words, the database behaves has if
3868** [sqlite3_extended_result_codes(db,1)] where called on the database
3869** connection as soon as the connection is created. In addition to setting
3870** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3871** to return an extended result code.</dd>
3872**
3873** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3874** <dd>The database filename is not allowed to contain a symbolic link</dd>
3875** </dl>)^
3876**
3877** If the 3rd parameter to sqlite3_open_v2() is not one of the
3878** required combinations shown above optionally combined with other
3879** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
3880** then the behavior is undefined. Historic versions of SQLite
3881** have silently ignored surplus bits in the flags parameter to
3882** sqlite3_open_v2(), however that behavior might not be carried through
3883** into future versions of SQLite and so applications should not rely
3884** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3885** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause
3886** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE
3887** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not
3888** by sqlite3_open_v2().
3889**
3890** ^The fourth parameter to sqlite3_open_v2() is the name of the
3891** [sqlite3_vfs] object that defines the operating system interface that
3892** the new database connection should use. ^If the fourth parameter is
3893** a NULL pointer then the default [sqlite3_vfs] object is used.
3894**
3895** ^If the filename is ":memory:", then a private, temporary in-memory database
3896** is created for the connection. ^This in-memory database will vanish when
3897** the database connection is closed. Future versions of SQLite might
3898** make use of additional special filenames that begin with the ":" character.
3899** It is recommended that when a database filename actually does begin with
3900** a ":" character you should prefix the filename with a pathname such as
3901** "./" to avoid ambiguity.
3902**
3903** ^If the filename is an empty string, then a private, temporary
3904** on-disk database will be created. ^This private database will be
3905** automatically deleted as soon as the database connection is closed.
3906**
3907** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3908**
3909** ^If [URI filename] interpretation is enabled, and the filename argument
3910** begins with "file:", then the filename is interpreted as a URI. ^URI
3911** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3912** set in the third argument to sqlite3_open_v2(), or if it has
3913** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3914** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3915** URI filename interpretation is turned off
3916** by default, but future releases of SQLite might enable URI filename
3917** interpretation by default. See "[URI filenames]" for additional
3918** information.
3919**
3920** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3921** authority, then it must be either an empty string or the string
3922** "localhost". ^If the authority is not an empty string or "localhost", an
3923** error is returned to the caller. ^The fragment component of a URI, if
3924** present, is ignored.
3925**
3926** ^SQLite uses the path component of the URI as the name of the disk file
3927** which contains the database. ^If the path begins with a '/' character,
3928** then it is interpreted as an absolute path. ^If the path does not begin
3929** with a '/' (meaning that the authority section is omitted from the URI)
3930** then the path is interpreted as a relative path.
3931** ^(On windows, the first component of an absolute path
3932** is a drive specification (e.g. "C:").)^
3933**
3934** [[core URI query parameters]]
3935** The query component of a URI may contain parameters that are interpreted
3936** either by SQLite itself, or by a [VFS | custom VFS implementation].
3937** SQLite and its built-in [VFSes] interpret the
3938** following query parameters:
3939**
3940** <ul>
3941** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3942** a VFS object that provides the operating system interface that should
3943** be used to access the database file on disk. ^If this option is set to
3944** an empty string the default VFS object is used. ^Specifying an unknown
3945** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3946** present, then the VFS specified by the option takes precedence over
3947** the value passed as the fourth parameter to sqlite3_open_v2().
3948**
3949** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
3950** "rwc", or "memory". Attempting to set it to any other value is
3951** an error)^.
3952** ^If "ro" is specified, then the database is opened for read-only
3953** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3954** third argument to sqlite3_open_v2(). ^If the mode option is set to
3955** "rw", then the database is opened for read-write (but not create)
3956** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3957** been set. ^Value "rwc" is equivalent to setting both
3958** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
3959** set to "memory" then a pure [in-memory database] that never reads
3960** or writes from disk is used. ^It is an error to specify a value for
3961** the mode parameter that is less restrictive than that specified by
3962** the flags passed in the third parameter to sqlite3_open_v2().
3963**
3964** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3965** "private". ^Setting it to "shared" is equivalent to setting the
3966** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3967** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3968** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3969** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3970** a URI filename, its value overrides any behavior requested by setting
3971** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3972**
3973** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
3974** [powersafe overwrite] property does or does not apply to the
3975** storage media on which the database file resides.
3976**
3977** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
3978** which if set disables file locking in rollback journal modes. This
3979** is useful for accessing a database on a filesystem that does not
3980** support locking. Caution: Database corruption might result if two
3981** or more processes write to the same database and any one of those
3982** processes uses nolock=1.
3983**
3984** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
3985** parameter that indicates that the database file is stored on
3986** read-only media. ^When immutable is set, SQLite assumes that the
3987** database file cannot be changed, even by a process with higher
3988** privilege, and so the database is opened read-only and all locking
3989** and change detection is disabled. Caution: Setting the immutable
3990** property on a database file that does in fact change can result
3991** in incorrect query results and/or [SQLITE_CORRUPT] errors.
3992** See also: [SQLITE_IOCAP_IMMUTABLE].
3993**
3994** </ul>
3995**
3996** ^Specifying an unknown parameter in the query component of a URI is not an
3997** error. Future versions of SQLite might understand additional query
3998** parameters. See "[query parameters with special meaning to SQLite]" for
3999** additional information.
4000**
4001** [[URI filename examples]] <h3>URI filename examples</h3>
4002**
4003** <table border="1" align=center cellpadding=5>
4004** <tr><th> URI filenames <th> Results
4005** <tr><td> file:data.db <td>
4006** Open the file "data.db" in the current directory.
4007** <tr><td> file:/home/fred/data.db<br>
4008** file:///home/fred/data.db <br>
4009** file://localhost/home/fred/data.db <br> <td>
4010** Open the database file "/home/fred/data.db".
4011** <tr><td> file://darkstar/home/fred/data.db <td>
4012** An error. "darkstar" is not a recognized authority.
4013** <tr><td style="white-space:nowrap">
4014** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4015** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4016** C:. Note that the %20 escaping in this example is not strictly
4017** necessary - space characters can be used literally
4018** in URI filenames.
4019** <tr><td> file:data.db?mode=ro&cache=private <td>
4020** Open file "data.db" in the current directory for read-only access.
4021** Regardless of whether or not shared-cache mode is enabled by
4022** default, use a private cache.
4023** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4024** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4025** that uses dot-files in place of posix advisory locking.
4026** <tr><td> file:data.db?mode=readonly <td>
4027** An error. "readonly" is not a valid option for the "mode" parameter.
4028** Use "ro" instead: "file:data.db?mode=ro".
4029** </table>
4030**
4031** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4032** query components of a URI. A hexadecimal escape sequence consists of a
4033** percent sign - "%" - followed by exactly two hexadecimal digits
4034** specifying an octet value. ^Before the path or query components of a
4035** URI filename are interpreted, they are encoded using UTF-8 and all
4036** hexadecimal escape sequences replaced by a single byte containing the
4037** corresponding octet. If this process generates an invalid UTF-8 encoding,
4038** the results are undefined.
4039**
4040** <b>Note to Windows users:</b> The encoding used for the filename argument
4041** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4042** codepage is currently defined. Filenames containing international
4043** characters must be converted to UTF-8 prior to passing them into
4044** sqlite3_open() or sqlite3_open_v2().
4045**
4046** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4047** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4048** features that require the use of temporary files may fail.
4049**
4050** See also: [sqlite3_temp_directory]
4051*/
4052SQLITE_API int sqlite3_open(
4053 const char *filename, /* Database filename (UTF-8) */
4054 sqlite3 **ppDb /* OUT: SQLite db handle */
4055);
4056SQLITE_API int sqlite3_open16(
4057 const void *filename, /* Database filename (UTF-16) */
4058 sqlite3 **ppDb /* OUT: SQLite db handle */
4059);
4060SQLITE_API int sqlite3_open_v2(
4061 const char *filename, /* Database filename (UTF-8) */
4062 sqlite3 **ppDb, /* OUT: SQLite db handle */
4063 int flags, /* Flags */
4064 const char *zVfs /* Name of VFS module to use */
4065);
4066
4067/*
4068** CAPI3REF: Obtain Values For URI Parameters
4069**
4070** These are utility routines, useful to [VFS|custom VFS implementations],
4071** that check if a database file was a URI that contained a specific query
4072** parameter, and if so obtains the value of that query parameter.
4073**
4074** The first parameter to these interfaces (hereafter referred to
4075** as F) must be one of:
4076** <ul>
4077** <li> A database filename pointer created by the SQLite core and
4078** passed into the xOpen() method of a VFS implementation, or
4079** <li> A filename obtained from [sqlite3_db_filename()], or
4080** <li> A new filename constructed using [sqlite3_create_filename()].
4081** </ul>
4082** If the F parameter is not one of the above, then the behavior is
4083** undefined and probably undesirable. Older versions of SQLite were
4084** more tolerant of invalid F parameters than newer versions.
4085**
4086** If F is a suitable filename (as described in the previous paragraph)
4087** and if P is the name of the query parameter, then
4088** sqlite3_uri_parameter(F,P) returns the value of the P
4089** parameter if it exists or a NULL pointer if P does not appear as a
4090** query parameter on F. If P is a query parameter of F and it
4091** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4092** a pointer to an empty string.
4093**
4094** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4095** parameter and returns true (1) or false (0) according to the value
4096** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4097** value of query parameter P is one of "yes", "true", or "on" in any
4098** case or if the value begins with a non-zero number. The
4099** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4100** query parameter P is one of "no", "false", or "off" in any case or
4101** if the value begins with a numeric zero. If P is not a query
4102** parameter on F or if the value of P does not match any of the
4103** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4104**
4105** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4106** 64-bit signed integer and returns that integer, or D if P does not
4107** exist. If the value of P is something other than an integer, then
4108** zero is returned.
4109**
4110** The sqlite3_uri_key(F,N) returns a pointer to the name (not
4111** the value) of the N-th query parameter for filename F, or a NULL
4112** pointer if N is less than zero or greater than the number of query
4113** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4114** the name of the first query parameter, 1 for the second parameter, and
4115** so forth.
4116**
4117** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4118** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4119** is not a database file pathname pointer that the SQLite core passed
4120** into the xOpen VFS method, then the behavior of this routine is undefined
4121** and probably undesirable.
4122**
4123** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
4124** parameter can also be the name of a rollback journal file or WAL file
4125** in addition to the main database file. Prior to version 3.31.0, these
4126** routines would only work if F was the name of the main database file.
4127** When the F parameter is the name of the rollback journal or WAL file,
4128** it has access to all the same query parameters as were found on the
4129** main database file.
4130**
4131** See the [URI filename] documentation for additional information.
4132*/
4133SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
4134SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
4135SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
4136SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
4137
4138/*
4139** CAPI3REF: Translate filenames
4140**
4141** These routines are available to [VFS|custom VFS implementations] for
4142** translating filenames between the main database file, the journal file,
4143** and the WAL file.
4144**
4145** If F is the name of an sqlite database file, journal file, or WAL file
4146** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4147** returns the name of the corresponding database file.
4148**
4149** If F is the name of an sqlite database file, journal file, or WAL file
4150** passed by the SQLite core into the VFS, or if F is a database filename
4151** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4152** returns the name of the corresponding rollback journal file.
4153**
4154** If F is the name of an sqlite database file, journal file, or WAL file
4155** that was passed by the SQLite core into the VFS, or if F is a database
4156** filename obtained from [sqlite3_db_filename()], then
4157** sqlite3_filename_wal(F) returns the name of the corresponding
4158** WAL file.
4159**
4160** In all of the above, if F is not the name of a database, journal or WAL
4161** filename passed into the VFS from the SQLite core and F is not the
4162** return value from [sqlite3_db_filename()], then the result is
4163** undefined and is likely a memory access violation.
4164*/
4165SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
4166SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
4167SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
4168
4169/*
4170** CAPI3REF: Database File Corresponding To A Journal
4171**
4172** ^If X is the name of a rollback or WAL-mode journal file that is
4173** passed into the xOpen method of [sqlite3_vfs], then
4174** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4175** object that represents the main database file.
4176**
4177** This routine is intended for use in custom [VFS] implementations
4178** only. It is not a general-purpose interface.
4179** The argument sqlite3_file_object(X) must be a filename pointer that
4180** has been passed into [sqlite3_vfs].xOpen method where the
4181** flags parameter to xOpen contains one of the bits
4182** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use
4183** of this routine results in undefined and probably undesirable
4184** behavior.
4185*/
4186SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4187
4188/*
4189** CAPI3REF: Create and Destroy VFS Filenames
4190**
4191** These interfaces are provided for use by [VFS shim] implementations and
4192** are not useful outside of that context.
4193**
4194** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4195** database filename D with corresponding journal file J and WAL file W and
4196** with N URI parameters key/values pairs in the array P. The result from
4197** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4198** is safe to pass to routines like:
4199** <ul>
4200** <li> [sqlite3_uri_parameter()],
4201** <li> [sqlite3_uri_boolean()],
4202** <li> [sqlite3_uri_int64()],
4203** <li> [sqlite3_uri_key()],
4204** <li> [sqlite3_filename_database()],
4205** <li> [sqlite3_filename_journal()], or
4206** <li> [sqlite3_filename_wal()].
4207** </ul>
4208** If a memory allocation error occurs, sqlite3_create_filename() might
4209** return a NULL pointer. The memory obtained from sqlite3_create_filename(X)
4210** must be released by a corresponding call to sqlite3_free_filename(Y).
4211**
4212** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4213** of 2*N pointers to strings. Each pair of pointers in this array corresponds
4214** to a key and value for a query parameter. The P parameter may be a NULL
4215** pointer if N is zero. None of the 2*N pointers in the P array may be
4216** NULL pointers and key pointers should not be empty strings.
4217** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4218** be NULL pointers, though they can be empty strings.
4219**
4220** The sqlite3_free_filename(Y) routine releases a memory allocation
4221** previously obtained from sqlite3_create_filename(). Invoking
4222** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4223**
4224** If the Y parameter to sqlite3_free_filename(Y) is anything other
4225** than a NULL pointer or a pointer previously acquired from
4226** sqlite3_create_filename(), then bad things such as heap
4227** corruption or segfaults may occur. The value Y should not be
4228** used again after sqlite3_free_filename(Y) has been called. This means
4229** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4230** then the corresponding [sqlite3_module.xClose() method should also be
4231** invoked prior to calling sqlite3_free_filename(Y).
4232*/
4233SQLITE_API sqlite3_filename sqlite3_create_filename(
4234 const char *zDatabase,
4235 const char *zJournal,
4236 const char *zWal,
4237 int nParam,
4238 const char **azParam
4239);
4240SQLITE_API void sqlite3_free_filename(sqlite3_filename);
4241
4242/*
4243** CAPI3REF: Error Codes And Messages
4244** METHOD: sqlite3
4245**
4246** ^If the most recent sqlite3_* API call associated with
4247** [database connection] D failed, then the sqlite3_errcode(D) interface
4248** returns the numeric [result code] or [extended result code] for that
4249** API call.
4250** ^The sqlite3_extended_errcode()
4251** interface is the same except that it always returns the
4252** [extended result code] even when extended result codes are
4253** disabled.
4254**
4255** The values returned by sqlite3_errcode() and/or
4256** sqlite3_extended_errcode() might change with each API call.
4257** Except, there are some interfaces that are guaranteed to never
4258** change the value of the error code. The error-code preserving
4259** interfaces include the following:
4260**
4261** <ul>
4262** <li> sqlite3_errcode()
4263** <li> sqlite3_extended_errcode()
4264** <li> sqlite3_errmsg()
4265** <li> sqlite3_errmsg16()
4266** <li> sqlite3_error_offset()
4267** </ul>
4268**
4269** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4270** text that describes the error, as either UTF-8 or UTF-16 respectively.
4271** ^(Memory to hold the error message string is managed internally.
4272** The application does not need to worry about freeing the result.
4273** However, the error string might be overwritten or deallocated by
4274** subsequent calls to other SQLite interface functions.)^
4275**
4276** ^The sqlite3_errstr() interface returns the English-language text
4277** that describes the [result code], as UTF-8.
4278** ^(Memory to hold the error message string is managed internally
4279** and must not be freed by the application)^.
4280**
4281** ^If the most recent error references a specific token in the input
4282** SQL, the sqlite3_error_offset() interface returns the byte offset
4283** of the start of that token. ^The byte offset returned by
4284** sqlite3_error_offset() assumes that the input SQL is UTF8.
4285** ^If the most recent error does not reference a specific token in the input
4286** SQL, then the sqlite3_error_offset() function returns -1.
4287**
4288** When the serialized [threading mode] is in use, it might be the
4289** case that a second error occurs on a separate thread in between
4290** the time of the first error and the call to these interfaces.
4291** When that happens, the second error will be reported since these
4292** interfaces always report the most recent result. To avoid
4293** this, each thread can obtain exclusive use of the [database connection] D
4294** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4295** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4296** all calls to the interfaces listed here are completed.
4297**
4298** If an interface fails with SQLITE_MISUSE, that means the interface
4299** was invoked incorrectly by the application. In that case, the
4300** error code and message may or may not be set.
4301*/
4302SQLITE_API int sqlite3_errcode(sqlite3 *db);
4303SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4304SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4305SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4306SQLITE_API const char *sqlite3_errstr(int);
4307SQLITE_API int sqlite3_error_offset(sqlite3 *db);
4308
4309/*
4310** CAPI3REF: Prepared Statement Object
4311** KEYWORDS: {prepared statement} {prepared statements}
4312**
4313** An instance of this object represents a single SQL statement that
4314** has been compiled into binary form and is ready to be evaluated.
4315**
4316** Think of each SQL statement as a separate computer program. The
4317** original SQL text is source code. A prepared statement object
4318** is the compiled object code. All SQL must be converted into a
4319** prepared statement before it can be run.
4320**
4321** The life-cycle of a prepared statement object usually goes like this:
4322**
4323** <ol>
4324** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4325** <li> Bind values to [parameters] using the sqlite3_bind_*()
4326** interfaces.
4327** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4328** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4329** to step 2. Do this zero or more times.
4330** <li> Destroy the object using [sqlite3_finalize()].
4331** </ol>
4332*/
4333typedef struct sqlite3_stmt sqlite3_stmt;
4334
4335/*
4336** CAPI3REF: Run-time Limits
4337** METHOD: sqlite3
4338**
4339** ^(This interface allows the size of various constructs to be limited
4340** on a connection by connection basis. The first parameter is the
4341** [database connection] whose limit is to be set or queried. The
4342** second parameter is one of the [limit categories] that define a
4343** class of constructs to be size limited. The third parameter is the
4344** new limit for that construct.)^
4345**
4346** ^If the new limit is a negative number, the limit is unchanged.
4347** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4348** [limits | hard upper bound]
4349** set at compile-time by a C preprocessor macro called
4350** [limits | SQLITE_MAX_<i>NAME</i>].
4351** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4352** ^Attempts to increase a limit above its hard upper bound are
4353** silently truncated to the hard upper bound.
4354**
4355** ^Regardless of whether or not the limit was changed, the
4356** [sqlite3_limit()] interface returns the prior value of the limit.
4357** ^Hence, to find the current value of a limit without changing it,
4358** simply invoke this interface with the third parameter set to -1.
4359**
4360** Run-time limits are intended for use in applications that manage
4361** both their own internal database and also databases that are controlled
4362** by untrusted external sources. An example application might be a
4363** web browser that has its own databases for storing history and
4364** separate databases controlled by JavaScript applications downloaded
4365** off the Internet. The internal databases can be given the
4366** large, default limits. Databases managed by external sources can
4367** be given much smaller limits designed to prevent a denial of service
4368** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4369** interface to further control untrusted SQL. The size of the database
4370** created by an untrusted script can be contained using the
4371** [max_page_count] [PRAGMA].
4372**
4373** New run-time limit categories may be added in future releases.
4374*/
4375SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4376
4377/*
4378** CAPI3REF: Run-Time Limit Categories
4379** KEYWORDS: {limit category} {*limit categories}
4380**
4381** These constants define various performance limits
4382** that can be lowered at run-time using [sqlite3_limit()].
4383** The synopsis of the meanings of the various limits is shown below.
4384** Additional information is available at [limits | Limits in SQLite].
4385**
4386** <dl>
4387** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4388** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4389**
4390** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4391** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4392**
4393** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4394** <dd>The maximum number of columns in a table definition or in the
4395** result set of a [SELECT] or the maximum number of columns in an index
4396** or in an ORDER BY or GROUP BY clause.</dd>)^
4397**
4398** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4399** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4400**
4401** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4402** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4403**
4404** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4405** <dd>The maximum number of instructions in a virtual machine program
4406** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4407** the equivalent tries to allocate space for more than this many opcodes
4408** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4409**
4410** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4411** <dd>The maximum number of arguments on a function.</dd>)^
4412**
4413** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4414** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4415**
4416** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4417** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4418** <dd>The maximum length of the pattern argument to the [LIKE] or
4419** [GLOB] operators.</dd>)^
4420**
4421** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4422** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4423** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4424**
4425** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4426** <dd>The maximum depth of recursion for triggers.</dd>)^
4427**
4428** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4429** <dd>The maximum number of auxiliary worker threads that a single
4430** [prepared statement] may start.</dd>)^
4431** </dl>
4432*/
4433#define SQLITE_LIMIT_LENGTH 0
4434#define SQLITE_LIMIT_SQL_LENGTH 1
4435#define SQLITE_LIMIT_COLUMN 2
4436#define SQLITE_LIMIT_EXPR_DEPTH 3
4437#define SQLITE_LIMIT_COMPOUND_SELECT 4
4438#define SQLITE_LIMIT_VDBE_OP 5
4439#define SQLITE_LIMIT_FUNCTION_ARG 6
4440#define SQLITE_LIMIT_ATTACHED 7
4441#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
4442#define SQLITE_LIMIT_VARIABLE_NUMBER 9
4443#define SQLITE_LIMIT_TRIGGER_DEPTH 10
4444#define SQLITE_LIMIT_WORKER_THREADS 11
4445
4446/*
4447** CAPI3REF: Prepare Flags
4448**
4449** These constants define various flags that can be passed into
4450** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4451** [sqlite3_prepare16_v3()] interfaces.
4452**
4453** New flags may be added in future releases of SQLite.
4454**
4455** <dl>
4456** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4457** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4458** that the prepared statement will be retained for a long time and
4459** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4460** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4461** be used just once or at most a few times and then destroyed using
4462** [sqlite3_finalize()] relatively soon. The current implementation acts
4463** on this hint by avoiding the use of [lookaside memory] so as not to
4464** deplete the limited store of lookaside memory. Future versions of
4465** SQLite may act on this hint differently.
4466**
4467** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4468** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4469** to be required for any prepared statement that wanted to use the
4470** [sqlite3_normalized_sql()] interface. However, the
4471** [sqlite3_normalized_sql()] interface is now available to all
4472** prepared statements, regardless of whether or not they use this
4473** flag.
4474**
4475** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4476** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4477** to return an error (error code SQLITE_ERROR) if the statement uses
4478** any virtual tables.
4479** </dl>
4480*/
4481#define SQLITE_PREPARE_PERSISTENT 0x01
4482#define SQLITE_PREPARE_NORMALIZE 0x02
4483#define SQLITE_PREPARE_NO_VTAB 0x04
4484
4485/*
4486** CAPI3REF: Compiling An SQL Statement
4487** KEYWORDS: {SQL statement compiler}
4488** METHOD: sqlite3
4489** CONSTRUCTOR: sqlite3_stmt
4490**
4491** To execute an SQL statement, it must first be compiled into a byte-code
4492** program using one of these routines. Or, in other words, these routines
4493** are constructors for the [prepared statement] object.
4494**
4495** The preferred routine to use is [sqlite3_prepare_v2()]. The
4496** [sqlite3_prepare()] interface is legacy and should be avoided.
4497** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4498** for special purposes.
4499**
4500** The use of the UTF-8 interfaces is preferred, as SQLite currently
4501** does all parsing using UTF-8. The UTF-16 interfaces are provided
4502** as a convenience. The UTF-16 interfaces work by converting the
4503** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4504**
4505** The first argument, "db", is a [database connection] obtained from a
4506** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4507** [sqlite3_open16()]. The database connection must not have been closed.
4508**
4509** The second argument, "zSql", is the statement to be compiled, encoded
4510** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4511** and sqlite3_prepare_v3()
4512** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4513** and sqlite3_prepare16_v3() use UTF-16.
4514**
4515** ^If the nByte argument is negative, then zSql is read up to the
4516** first zero terminator. ^If nByte is positive, then it is the
4517** number of bytes read from zSql. ^If nByte is zero, then no prepared
4518** statement is generated.
4519** If the caller knows that the supplied string is nul-terminated, then
4520** there is a small performance advantage to passing an nByte parameter that
4521** is the number of bytes in the input string <i>including</i>
4522** the nul-terminator.
4523**
4524** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4525** past the end of the first SQL statement in zSql. These routines only
4526** compile the first statement in zSql, so *pzTail is left pointing to
4527** what remains uncompiled.
4528**
4529** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4530** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4531** to NULL. ^If the input text contains no SQL (if the input is an empty
4532** string or a comment) then *ppStmt is set to NULL.
4533** The calling procedure is responsible for deleting the compiled
4534** SQL statement using [sqlite3_finalize()] after it has finished with it.
4535** ppStmt may not be NULL.
4536**
4537** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4538** otherwise an [error code] is returned.
4539**
4540** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4541** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4542** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4543** are retained for backwards compatibility, but their use is discouraged.
4544** ^In the "vX" interfaces, the prepared statement
4545** that is returned (the [sqlite3_stmt] object) contains a copy of the
4546** original SQL text. This causes the [sqlite3_step()] interface to
4547** behave differently in three ways:
4548**
4549** <ol>
4550** <li>
4551** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4552** always used to do, [sqlite3_step()] will automatically recompile the SQL
4553** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4554** retries will occur before sqlite3_step() gives up and returns an error.
4555** </li>
4556**
4557** <li>
4558** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4559** [error codes] or [extended error codes]. ^The legacy behavior was that
4560** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4561** and the application would have to make a second call to [sqlite3_reset()]
4562** in order to find the underlying cause of the problem. With the "v2" prepare
4563** interfaces, the underlying reason for the error is returned immediately.
4564** </li>
4565**
4566** <li>
4567** ^If the specific value bound to a [parameter | host parameter] in the
4568** WHERE clause might influence the choice of query plan for a statement,
4569** then the statement will be automatically recompiled, as if there had been
4570** a schema change, on the first [sqlite3_step()] call following any change
4571** to the [sqlite3_bind_text | bindings] of that [parameter].
4572** ^The specific value of a WHERE-clause [parameter] might influence the
4573** choice of query plan if the parameter is the left-hand side of a [LIKE]
4574** or [GLOB] operator or if the parameter is compared to an indexed column
4575** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4576** </li>
4577** </ol>
4578**
4579** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4580** the extra prepFlags parameter, which is a bit array consisting of zero or
4581** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4582** sqlite3_prepare_v2() interface works exactly the same as
4583** sqlite3_prepare_v3() with a zero prepFlags parameter.
4584*/
4585SQLITE_API int sqlite3_prepare(
4586 sqlite3 *db, /* Database handle */
4587 const char *zSql, /* SQL statement, UTF-8 encoded */
4588 int nByte, /* Maximum length of zSql in bytes. */
4589 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4590 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4591);
4592SQLITE_API int sqlite3_prepare_v2(
4593 sqlite3 *db, /* Database handle */
4594 const char *zSql, /* SQL statement, UTF-8 encoded */
4595 int nByte, /* Maximum length of zSql in bytes. */
4596 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4597 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4598);
4599SQLITE_API int sqlite3_prepare_v3(
4600 sqlite3 *db, /* Database handle */
4601 const char *zSql, /* SQL statement, UTF-8 encoded */
4602 int nByte, /* Maximum length of zSql in bytes. */
4603 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4604 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4605 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4606);
4607SQLITE_API int sqlite3_prepare16(
4608 sqlite3 *db, /* Database handle */
4609 const void *zSql, /* SQL statement, UTF-16 encoded */
4610 int nByte, /* Maximum length of zSql in bytes. */
4611 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4612 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4613);
4614SQLITE_API int sqlite3_prepare16_v2(
4615 sqlite3 *db, /* Database handle */
4616 const void *zSql, /* SQL statement, UTF-16 encoded */
4617 int nByte, /* Maximum length of zSql in bytes. */
4618 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4619 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4620);
4621SQLITE_API int sqlite3_prepare16_v3(
4622 sqlite3 *db, /* Database handle */
4623 const void *zSql, /* SQL statement, UTF-16 encoded */
4624 int nByte, /* Maximum length of zSql in bytes. */
4625 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4626 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4627 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4628);
4629
4630/*
4631** CAPI3REF: Retrieving Statement SQL
4632** METHOD: sqlite3_stmt
4633**
4634** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4635** SQL text used to create [prepared statement] P if P was
4636** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4637** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4638** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4639** string containing the SQL text of prepared statement P with
4640** [bound parameters] expanded.
4641** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4642** string containing the normalized SQL text of prepared statement P. The
4643** semantics used to normalize a SQL statement are unspecified and subject
4644** to change. At a minimum, literal values will be replaced with suitable
4645** placeholders.
4646**
4647** ^(For example, if a prepared statement is created using the SQL
4648** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4649** and parameter :xyz is unbound, then sqlite3_sql() will return
4650** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4651** will return "SELECT 2345,NULL".)^
4652**
4653** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4654** is available to hold the result, or if the result would exceed the
4655** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4656**
4657** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4658** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4659** option causes sqlite3_expanded_sql() to always return NULL.
4660**
4661** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4662** are managed by SQLite and are automatically freed when the prepared
4663** statement is finalized.
4664** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4665** is obtained from [sqlite3_malloc()] and must be freed by the application
4666** by passing it to [sqlite3_free()].
4667**
4668** ^The sqlite3_normalized_sql() interface is only available if
4669** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4670*/
4671SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4672SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4673#ifdef SQLITE_ENABLE_NORMALIZE
4674SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4675#endif
4676
4677/*
4678** CAPI3REF: Determine If An SQL Statement Writes The Database
4679** METHOD: sqlite3_stmt
4680**
4681** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4682** and only if the [prepared statement] X makes no direct changes to
4683** the content of the database file.
4684**
4685** Note that [application-defined SQL functions] or
4686** [virtual tables] might change the database indirectly as a side effect.
4687** ^(For example, if an application defines a function "eval()" that
4688** calls [sqlite3_exec()], then the following SQL statement would
4689** change the database file through side-effects:
4690**
4691** <blockquote><pre>
4692** SELECT eval('DELETE FROM t1') FROM t2;
4693** </pre></blockquote>
4694**
4695** But because the [SELECT] statement does not change the database file
4696** directly, sqlite3_stmt_readonly() would still return true.)^
4697**
4698** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4699** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4700** since the statements themselves do not actually modify the database but
4701** rather they control the timing of when other statements modify the
4702** database. ^The [ATTACH] and [DETACH] statements also cause
4703** sqlite3_stmt_readonly() to return true since, while those statements
4704** change the configuration of a database connection, they do not make
4705** changes to the content of the database files on disk.
4706** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4707** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4708** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4709** sqlite3_stmt_readonly() returns false for those commands.
4710**
4711** ^This routine returns false if there is any possibility that the
4712** statement might change the database file. ^A false return does
4713** not guarantee that the statement will change the database file.
4714** ^For example, an UPDATE statement might have a WHERE clause that
4715** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4716** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a
4717** read-only no-op if the table already exists, but
4718** sqlite3_stmt_readonly() still returns false for such a statement.
4719**
4720** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN]
4721** statement, then sqlite3_stmt_readonly(X) returns the same value as
4722** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted.
4723*/
4724SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4725
4726/*
4727** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4728** METHOD: sqlite3_stmt
4729**
4730** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4731** prepared statement S is an EXPLAIN statement, or 2 if the
4732** statement S is an EXPLAIN QUERY PLAN.
4733** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4734** an ordinary statement or a NULL pointer.
4735*/
4736SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4737
4738/*
4739** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement
4740** METHOD: sqlite3_stmt
4741**
4742** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN
4743** setting for [prepared statement] S. If E is zero, then S becomes
4744** a normal prepared statement. If E is 1, then S behaves as if
4745** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if
4746** its SQL text began with "[EXPLAIN QUERY PLAN]".
4747**
4748** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared.
4749** SQLite tries to avoid a reprepare, but a reprepare might be necessary
4750** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode.
4751**
4752** Because of the potential need to reprepare, a call to
4753** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be
4754** reprepared because it was created using [sqlite3_prepare()] instead of
4755** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and
4756** hence has no saved SQL text with which to reprepare.
4757**
4758** Changing the explain setting for a prepared statement does not change
4759** the original SQL text for the statement. Hence, if the SQL text originally
4760** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0)
4761** is called to convert the statement into an ordinary statement, the EXPLAIN
4762** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S)
4763** output, even though the statement now acts like a normal SQL statement.
4764**
4765** This routine returns SQLITE_OK if the explain mode is successfully
4766** changed, or an error code if the explain mode could not be changed.
4767** The explain mode cannot be changed while a statement is active.
4768** Hence, it is good practice to call [sqlite3_reset(S)]
4769** immediately prior to calling sqlite3_stmt_explain(S,E).
4770*/
4771SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode);
4772
4773/*
4774** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4775** METHOD: sqlite3_stmt
4776**
4777** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4778** [prepared statement] S has been stepped at least once using
4779** [sqlite3_step(S)] but has neither run to completion (returned
4780** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4781** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
4782** interface returns false if S is a NULL pointer. If S is not a
4783** NULL pointer and is not a pointer to a valid [prepared statement]
4784** object, then the behavior is undefined and probably undesirable.
4785**
4786** This interface can be used in combination [sqlite3_next_stmt()]
4787** to locate all prepared statements associated with a database
4788** connection that are in need of being reset. This can be used,
4789** for example, in diagnostic routines to search for prepared
4790** statements that are holding a transaction open.
4791*/
4792SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4793
4794/*
4795** CAPI3REF: Dynamically Typed Value Object
4796** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4797**
4798** SQLite uses the sqlite3_value object to represent all values
4799** that can be stored in a database table. SQLite uses dynamic typing
4800** for the values it stores. ^Values stored in sqlite3_value objects
4801** can be integers, floating point values, strings, BLOBs, or NULL.
4802**
4803** An sqlite3_value object may be either "protected" or "unprotected".
4804** Some interfaces require a protected sqlite3_value. Other interfaces
4805** will accept either a protected or an unprotected sqlite3_value.
4806** Every interface that accepts sqlite3_value arguments specifies
4807** whether or not it requires a protected sqlite3_value. The
4808** [sqlite3_value_dup()] interface can be used to construct a new
4809** protected sqlite3_value from an unprotected sqlite3_value.
4810**
4811** The terms "protected" and "unprotected" refer to whether or not
4812** a mutex is held. An internal mutex is held for a protected
4813** sqlite3_value object but no mutex is held for an unprotected
4814** sqlite3_value object. If SQLite is compiled to be single-threaded
4815** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4816** or if SQLite is run in one of reduced mutex modes
4817** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4818** then there is no distinction between protected and unprotected
4819** sqlite3_value objects and they can be used interchangeably. However,
4820** for maximum code portability it is recommended that applications
4821** still make the distinction between protected and unprotected
4822** sqlite3_value objects even when not strictly required.
4823**
4824** ^The sqlite3_value objects that are passed as parameters into the
4825** implementation of [application-defined SQL functions] are protected.
4826** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4827** are protected.
4828** ^The sqlite3_value object returned by
4829** [sqlite3_column_value()] is unprotected.
4830** Unprotected sqlite3_value objects may only be used as arguments
4831** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4832** [sqlite3_value_dup()].
4833** The [sqlite3_value_blob | sqlite3_value_type()] family of
4834** interfaces require protected sqlite3_value objects.
4835*/
4836typedef struct sqlite3_value sqlite3_value;
4837
4838/*
4839** CAPI3REF: SQL Function Context Object
4840**
4841** The context in which an SQL function executes is stored in an
4842** sqlite3_context object. ^A pointer to an sqlite3_context object
4843** is always first parameter to [application-defined SQL functions].
4844** The application-defined SQL function implementation will pass this
4845** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4846** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4847** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4848** and/or [sqlite3_set_auxdata()].
4849*/
4850typedef struct sqlite3_context sqlite3_context;
4851
4852/*
4853** CAPI3REF: Binding Values To Prepared Statements
4854** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4855** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4856** METHOD: sqlite3_stmt
4857**
4858** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4859** literals may be replaced by a [parameter] that matches one of following
4860** templates:
4861**
4862** <ul>
4863** <li> ?
4864** <li> ?NNN
4865** <li> :VVV
4866** <li> @VVV
4867** <li> $VVV
4868** </ul>
4869**
4870** In the templates above, NNN represents an integer literal,
4871** and VVV represents an alphanumeric identifier.)^ ^The values of these
4872** parameters (also called "host parameter names" or "SQL parameters")
4873** can be set using the sqlite3_bind_*() routines defined here.
4874**
4875** ^The first argument to the sqlite3_bind_*() routines is always
4876** a pointer to the [sqlite3_stmt] object returned from
4877** [sqlite3_prepare_v2()] or its variants.
4878**
4879** ^The second argument is the index of the SQL parameter to be set.
4880** ^The leftmost SQL parameter has an index of 1. ^When the same named
4881** SQL parameter is used more than once, second and subsequent
4882** occurrences have the same index as the first occurrence.
4883** ^The index for named parameters can be looked up using the
4884** [sqlite3_bind_parameter_index()] API if desired. ^The index
4885** for "?NNN" parameters is the value of NNN.
4886** ^The NNN value must be between 1 and the [sqlite3_limit()]
4887** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
4888**
4889** ^The third argument is the value to bind to the parameter.
4890** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4891** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4892** is ignored and the end result is the same as sqlite3_bind_null().
4893** ^If the third parameter to sqlite3_bind_text() is not NULL, then
4894** it should be a pointer to well-formed UTF8 text.
4895** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4896** it should be a pointer to well-formed UTF16 text.
4897** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4898** it should be a pointer to a well-formed unicode string that is
4899** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4900** otherwise.
4901**
4902** [[byte-order determination rules]] ^The byte-order of
4903** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4904** found in first character, which is removed, or in the absence of a BOM
4905** the byte order is the native byte order of the host
4906** machine for sqlite3_bind_text16() or the byte order specified in
4907** the 6th parameter for sqlite3_bind_text64().)^
4908** ^If UTF16 input text contains invalid unicode
4909** characters, then SQLite might change those invalid characters
4910** into the unicode replacement character: U+FFFD.
4911**
4912** ^(In those routines that have a fourth argument, its value is the
4913** number of bytes in the parameter. To be clear: the value is the
4914** number of <u>bytes</u> in the value, not the number of characters.)^
4915** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4916** is negative, then the length of the string is
4917** the number of bytes up to the first zero terminator.
4918** If the fourth parameter to sqlite3_bind_blob() is negative, then
4919** the behavior is undefined.
4920** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4921** or sqlite3_bind_text16() or sqlite3_bind_text64() then
4922** that parameter must be the byte offset
4923** where the NUL terminator would occur assuming the string were NUL
4924** terminated. If any NUL characters occurs at byte offsets less than
4925** the value of the fourth parameter then the resulting string value will
4926** contain embedded NULs. The result of expressions involving strings
4927** with embedded NULs is undefined.
4928**
4929** ^The fifth argument to the BLOB and string binding interfaces controls
4930** or indicates the lifetime of the object referenced by the third parameter.
4931** These three options exist:
4932** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished
4933** with it may be passed. ^It is called to dispose of the BLOB or string even
4934** if the call to the bind API fails, except the destructor is not called if
4935** the third parameter is a NULL pointer or the fourth parameter is negative.
4936** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that
4937** the application remains responsible for disposing of the object. ^In this
4938** case, the object and the provided pointer to it must remain valid until
4939** either the prepared statement is finalized or the same SQL parameter is
4940** bound to something else, whichever occurs sooner.
4941** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
4942** object is to be copied prior to the return from sqlite3_bind_*(). ^The
4943** object and pointer to it must remain valid until then. ^SQLite will then
4944** manage the lifetime of its private copy.
4945**
4946** ^The sixth argument to sqlite3_bind_text64() must be one of
4947** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4948** to specify the encoding of the text in the third parameter. If
4949** the sixth argument to sqlite3_bind_text64() is not one of the
4950** allowed values shown above, or if the text encoding is different
4951** from the encoding specified by the sixth parameter, then the behavior
4952** is undefined.
4953**
4954** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
4955** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
4956** (just an integer to hold its size) while it is being processed.
4957** Zeroblobs are intended to serve as placeholders for BLOBs whose
4958** content is later written using
4959** [sqlite3_blob_open | incremental BLOB I/O] routines.
4960** ^A negative value for the zeroblob results in a zero-length BLOB.
4961**
4962** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4963** [prepared statement] S to have an SQL value of NULL, but to also be
4964** associated with the pointer P of type T. ^D is either a NULL pointer or
4965** a pointer to a destructor function for P. ^SQLite will invoke the
4966** destructor D with a single argument of P when it is finished using
4967** P. The T parameter should be a static string, preferably a string
4968** literal. The sqlite3_bind_pointer() routine is part of the
4969** [pointer passing interface] added for SQLite 3.20.0.
4970**
4971** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
4972** for the [prepared statement] or with a prepared statement for which
4973** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
4974** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
4975** routine is passed a [prepared statement] that has been finalized, the
4976** result is undefined and probably harmful.
4977**
4978** ^Bindings are not cleared by the [sqlite3_reset()] routine.
4979** ^Unbound parameters are interpreted as NULL.
4980**
4981** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
4982** [error code] if anything goes wrong.
4983** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
4984** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
4985** [SQLITE_MAX_LENGTH].
4986** ^[SQLITE_RANGE] is returned if the parameter
4987** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
4988**
4989** See also: [sqlite3_bind_parameter_count()],
4990** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
4991*/
4992SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
4993SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
4994 void(*)(void*));
4995SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
4996SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
4997SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
4998SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
4999SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5000SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5001SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5002 void(*)(void*), unsigned char encoding);
5003SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5004SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5005SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5006SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5007
5008/*
5009** CAPI3REF: Number Of SQL Parameters
5010** METHOD: sqlite3_stmt
5011**
5012** ^This routine can be used to find the number of [SQL parameters]
5013** in a [prepared statement]. SQL parameters are tokens of the
5014** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5015** placeholders for values that are [sqlite3_bind_blob | bound]
5016** to the parameters at a later time.
5017**
5018** ^(This routine actually returns the index of the largest (rightmost)
5019** parameter. For all forms except ?NNN, this will correspond to the
5020** number of unique parameters. If parameters of the ?NNN form are used,
5021** there may be gaps in the list.)^
5022**
5023** See also: [sqlite3_bind_blob|sqlite3_bind()],
5024** [sqlite3_bind_parameter_name()], and
5025** [sqlite3_bind_parameter_index()].
5026*/
5027SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5028
5029/*
5030** CAPI3REF: Name Of A Host Parameter
5031** METHOD: sqlite3_stmt
5032**
5033** ^The sqlite3_bind_parameter_name(P,N) interface returns
5034** the name of the N-th [SQL parameter] in the [prepared statement] P.
5035** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5036** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5037** respectively.
5038** In other words, the initial ":" or "$" or "@" or "?"
5039** is included as part of the name.)^
5040** ^Parameters of the form "?" without a following integer have no name
5041** and are referred to as "nameless" or "anonymous parameters".
5042**
5043** ^The first host parameter has an index of 1, not 0.
5044**
5045** ^If the value N is out of range or if the N-th parameter is
5046** nameless, then NULL is returned. ^The returned string is
5047** always in UTF-8 encoding even if the named parameter was
5048** originally specified as UTF-16 in [sqlite3_prepare16()],
5049** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5050**
5051** See also: [sqlite3_bind_blob|sqlite3_bind()],
5052** [sqlite3_bind_parameter_count()], and
5053** [sqlite3_bind_parameter_index()].
5054*/
5055SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5056
5057/*
5058** CAPI3REF: Index Of A Parameter With A Given Name
5059** METHOD: sqlite3_stmt
5060**
5061** ^Return the index of an SQL parameter given its name. ^The
5062** index value returned is suitable for use as the second
5063** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5064** is returned if no matching parameter is found. ^The parameter
5065** name must be given in UTF-8 even if the original statement
5066** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5067** [sqlite3_prepare16_v3()].
5068**
5069** See also: [sqlite3_bind_blob|sqlite3_bind()],
5070** [sqlite3_bind_parameter_count()], and
5071** [sqlite3_bind_parameter_name()].
5072*/
5073SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5074
5075/*
5076** CAPI3REF: Reset All Bindings On A Prepared Statement
5077** METHOD: sqlite3_stmt
5078**
5079** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5080** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5081** ^Use this routine to reset all host parameters to NULL.
5082*/
5083SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5084
5085/*
5086** CAPI3REF: Number Of Columns In A Result Set
5087** METHOD: sqlite3_stmt
5088**
5089** ^Return the number of columns in the result set returned by the
5090** [prepared statement]. ^If this routine returns 0, that means the
5091** [prepared statement] returns no data (for example an [UPDATE]).
5092** ^However, just because this routine returns a positive number does not
5093** mean that one or more rows of data will be returned. ^A SELECT statement
5094** will always have a positive sqlite3_column_count() but depending on the
5095** WHERE clause constraints and the table content, it might return no rows.
5096**
5097** See also: [sqlite3_data_count()]
5098*/
5099SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5100
5101/*
5102** CAPI3REF: Column Names In A Result Set
5103** METHOD: sqlite3_stmt
5104**
5105** ^These routines return the name assigned to a particular column
5106** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5107** interface returns a pointer to a zero-terminated UTF-8 string
5108** and sqlite3_column_name16() returns a pointer to a zero-terminated
5109** UTF-16 string. ^The first parameter is the [prepared statement]
5110** that implements the [SELECT] statement. ^The second parameter is the
5111** column number. ^The leftmost column is number 0.
5112**
5113** ^The returned string pointer is valid until either the [prepared statement]
5114** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5115** reprepared by the first call to [sqlite3_step()] for a particular run
5116** or until the next call to
5117** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5118**
5119** ^If sqlite3_malloc() fails during the processing of either routine
5120** (for example during a conversion from UTF-8 to UTF-16) then a
5121** NULL pointer is returned.
5122**
5123** ^The name of a result column is the value of the "AS" clause for
5124** that column, if there is an AS clause. If there is no AS clause
5125** then the name of the column is unspecified and may change from
5126** one release of SQLite to the next.
5127*/
5128SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5129SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5130
5131/*
5132** CAPI3REF: Source Of Data In A Query Result
5133** METHOD: sqlite3_stmt
5134**
5135** ^These routines provide a means to determine the database, table, and
5136** table column that is the origin of a particular result column in
5137** [SELECT] statement.
5138** ^The name of the database or table or column can be returned as
5139** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5140** the database name, the _table_ routines return the table name, and
5141** the origin_ routines return the column name.
5142** ^The returned string is valid until the [prepared statement] is destroyed
5143** using [sqlite3_finalize()] or until the statement is automatically
5144** reprepared by the first call to [sqlite3_step()] for a particular run
5145** or until the same information is requested
5146** again in a different encoding.
5147**
5148** ^The names returned are the original un-aliased names of the
5149** database, table, and column.
5150**
5151** ^The first argument to these interfaces is a [prepared statement].
5152** ^These functions return information about the Nth result column returned by
5153** the statement, where N is the second function argument.
5154** ^The left-most column is column 0 for these routines.
5155**
5156** ^If the Nth column returned by the statement is an expression or
5157** subquery and is not a column value, then all of these functions return
5158** NULL. ^These routines might also return NULL if a memory allocation error
5159** occurs. ^Otherwise, they return the name of the attached database, table,
5160** or column that query result column was extracted from.
5161**
5162** ^As with all other SQLite APIs, those whose names end with "16" return
5163** UTF-16 encoded strings and the other functions return UTF-8.
5164**
5165** ^These APIs are only available if the library was compiled with the
5166** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5167**
5168** If two or more threads call one or more
5169** [sqlite3_column_database_name | column metadata interfaces]
5170** for the same [prepared statement] and result column
5171** at the same time then the results are undefined.
5172*/
5173SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5174SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5175SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5176SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5177SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5178SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5179
5180/*
5181** CAPI3REF: Declared Datatype Of A Query Result
5182** METHOD: sqlite3_stmt
5183**
5184** ^(The first parameter is a [prepared statement].
5185** If this statement is a [SELECT] statement and the Nth column of the
5186** returned result set of that [SELECT] is a table column (not an
5187** expression or subquery) then the declared type of the table
5188** column is returned.)^ ^If the Nth column of the result set is an
5189** expression or subquery, then a NULL pointer is returned.
5190** ^The returned string is always UTF-8 encoded.
5191**
5192** ^(For example, given the database schema:
5193**
5194** CREATE TABLE t1(c1 VARIANT);
5195**
5196** and the following statement to be compiled:
5197**
5198** SELECT c1 + 1, c1 FROM t1;
5199**
5200** this routine would return the string "VARIANT" for the second result
5201** column (i==1), and a NULL pointer for the first result column (i==0).)^
5202**
5203** ^SQLite uses dynamic run-time typing. ^So just because a column
5204** is declared to contain a particular type does not mean that the
5205** data stored in that column is of the declared type. SQLite is
5206** strongly typed, but the typing is dynamic not static. ^Type
5207** is associated with individual values, not with the containers
5208** used to hold those values.
5209*/
5210SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5211SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5212
5213/*
5214** CAPI3REF: Evaluate An SQL Statement
5215** METHOD: sqlite3_stmt
5216**
5217** After a [prepared statement] has been prepared using any of
5218** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5219** or [sqlite3_prepare16_v3()] or one of the legacy
5220** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5221** must be called one or more times to evaluate the statement.
5222**
5223** The details of the behavior of the sqlite3_step() interface depend
5224** on whether the statement was prepared using the newer "vX" interfaces
5225** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5226** [sqlite3_prepare16_v2()] or the older legacy
5227** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5228** new "vX" interface is recommended for new applications but the legacy
5229** interface will continue to be supported.
5230**
5231** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5232** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5233** ^With the "v2" interface, any of the other [result codes] or
5234** [extended result codes] might be returned as well.
5235**
5236** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5237** database locks it needs to do its job. ^If the statement is a [COMMIT]
5238** or occurs outside of an explicit transaction, then you can retry the
5239** statement. If the statement is not a [COMMIT] and occurs within an
5240** explicit transaction then you should rollback the transaction before
5241** continuing.
5242**
5243** ^[SQLITE_DONE] means that the statement has finished executing
5244** successfully. sqlite3_step() should not be called again on this virtual
5245** machine without first calling [sqlite3_reset()] to reset the virtual
5246** machine back to its initial state.
5247**
5248** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5249** is returned each time a new row of data is ready for processing by the
5250** caller. The values may be accessed using the [column access functions].
5251** sqlite3_step() is called again to retrieve the next row of data.
5252**
5253** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5254** violation) has occurred. sqlite3_step() should not be called again on
5255** the VM. More information may be found by calling [sqlite3_errmsg()].
5256** ^With the legacy interface, a more specific error code (for example,
5257** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5258** can be obtained by calling [sqlite3_reset()] on the
5259** [prepared statement]. ^In the "v2" interface,
5260** the more specific error code is returned directly by sqlite3_step().
5261**
5262** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5263** Perhaps it was called on a [prepared statement] that has
5264** already been [sqlite3_finalize | finalized] or on one that had
5265** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5266** be the case that the same database connection is being used by two or
5267** more threads at the same moment in time.
5268**
5269** For all versions of SQLite up to and including 3.6.23.1, a call to
5270** [sqlite3_reset()] was required after sqlite3_step() returned anything
5271** other than [SQLITE_ROW] before any subsequent invocation of
5272** sqlite3_step(). Failure to reset the prepared statement using
5273** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5274** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5275** sqlite3_step() began
5276** calling [sqlite3_reset()] automatically in this circumstance rather
5277** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5278** break because any application that ever receives an SQLITE_MISUSE error
5279** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5280** can be used to restore the legacy behavior.
5281**
5282** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5283** API always returns a generic error code, [SQLITE_ERROR], following any
5284** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5285** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5286** specific [error codes] that better describes the error.
5287** We admit that this is a goofy design. The problem has been fixed
5288** with the "v2" interface. If you prepare all of your SQL statements
5289** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5290** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5291** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5292** then the more specific [error codes] are returned directly
5293** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5294*/
5295SQLITE_API int sqlite3_step(sqlite3_stmt*);
5296
5297/*
5298** CAPI3REF: Number of columns in a result set
5299** METHOD: sqlite3_stmt
5300**
5301** ^The sqlite3_data_count(P) interface returns the number of columns in the
5302** current row of the result set of [prepared statement] P.
5303** ^If prepared statement P does not have results ready to return
5304** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5305** interfaces) then sqlite3_data_count(P) returns 0.
5306** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5307** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5308** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5309** will return non-zero if previous call to [sqlite3_step](P) returned
5310** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5311** where it always returns zero since each step of that multi-step
5312** pragma returns 0 columns of data.
5313**
5314** See also: [sqlite3_column_count()]
5315*/
5316SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5317
5318/*
5319** CAPI3REF: Fundamental Datatypes
5320** KEYWORDS: SQLITE_TEXT
5321**
5322** ^(Every value in SQLite has one of five fundamental datatypes:
5323**
5324** <ul>
5325** <li> 64-bit signed integer
5326** <li> 64-bit IEEE floating point number
5327** <li> string
5328** <li> BLOB
5329** <li> NULL
5330** </ul>)^
5331**
5332** These constants are codes for each of those types.
5333**
5334** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5335** for a completely different meaning. Software that links against both
5336** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5337** SQLITE_TEXT.
5338*/
5339#define SQLITE_INTEGER 1
5340#define SQLITE_FLOAT 2
5341#define SQLITE_BLOB 4
5342#define SQLITE_NULL 5
5343#ifdef SQLITE_TEXT
5344# undef SQLITE_TEXT
5345#else
5346# define SQLITE_TEXT 3
5347#endif
5348#define SQLITE3_TEXT 3
5349
5350/*
5351** CAPI3REF: Result Values From A Query
5352** KEYWORDS: {column access functions}
5353** METHOD: sqlite3_stmt
5354**
5355** <b>Summary:</b>
5356** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5357** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5358** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5359** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5360** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5361** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5362** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5363** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5364** [sqlite3_value|unprotected sqlite3_value] object.
5365** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5366** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5367** or a UTF-8 TEXT result in bytes
5368** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5369** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5370** TEXT in bytes
5371** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5372** datatype of the result
5373** </table></blockquote>
5374**
5375** <b>Details:</b>
5376**
5377** ^These routines return information about a single column of the current
5378** result row of a query. ^In every case the first argument is a pointer
5379** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5380** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5381** and the second argument is the index of the column for which information
5382** should be returned. ^The leftmost column of the result set has the index 0.
5383** ^The number of columns in the result can be determined using
5384** [sqlite3_column_count()].
5385**
5386** If the SQL statement does not currently point to a valid row, or if the
5387** column index is out of range, the result is undefined.
5388** These routines may only be called when the most recent call to
5389** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5390** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5391** If any of these routines are called after [sqlite3_reset()] or
5392** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5393** something other than [SQLITE_ROW], the results are undefined.
5394** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5395** are called from a different thread while any of these routines
5396** are pending, then the results are undefined.
5397**
5398** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5399** each return the value of a result column in a specific data format. If
5400** the result column is not initially in the requested format (for example,
5401** if the query returns an integer but the sqlite3_column_text() interface
5402** is used to extract the value) then an automatic type conversion is performed.
5403**
5404** ^The sqlite3_column_type() routine returns the
5405** [SQLITE_INTEGER | datatype code] for the initial data type
5406** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5407** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5408** The return value of sqlite3_column_type() can be used to decide which
5409** of the first six interface should be used to extract the column value.
5410** The value returned by sqlite3_column_type() is only meaningful if no
5411** automatic type conversions have occurred for the value in question.
5412** After a type conversion, the result of calling sqlite3_column_type()
5413** is undefined, though harmless. Future
5414** versions of SQLite may change the behavior of sqlite3_column_type()
5415** following a type conversion.
5416**
5417** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5418** or sqlite3_column_bytes16() interfaces can be used to determine the size
5419** of that BLOB or string.
5420**
5421** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5422** routine returns the number of bytes in that BLOB or string.
5423** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5424** the string to UTF-8 and then returns the number of bytes.
5425** ^If the result is a numeric value then sqlite3_column_bytes() uses
5426** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5427** the number of bytes in that string.
5428** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5429**
5430** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5431** routine returns the number of bytes in that BLOB or string.
5432** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5433** the string to UTF-16 and then returns the number of bytes.
5434** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5435** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5436** the number of bytes in that string.
5437** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5438**
5439** ^The values returned by [sqlite3_column_bytes()] and
5440** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5441** of the string. ^For clarity: the values returned by
5442** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5443** bytes in the string, not the number of characters.
5444**
5445** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5446** even empty strings, are always zero-terminated. ^The return
5447** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5448**
5449** ^Strings returned by sqlite3_column_text16() always have the endianness
5450** which is native to the platform, regardless of the text encoding set
5451** for the database.
5452**
5453** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5454** [unprotected sqlite3_value] object. In a multithreaded environment,
5455** an unprotected sqlite3_value object may only be used safely with
5456** [sqlite3_bind_value()] and [sqlite3_result_value()].
5457** If the [unprotected sqlite3_value] object returned by
5458** [sqlite3_column_value()] is used in any other way, including calls
5459** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5460** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5461** Hence, the sqlite3_column_value() interface
5462** is normally only useful within the implementation of
5463** [application-defined SQL functions] or [virtual tables], not within
5464** top-level application code.
5465**
5466** These routines may attempt to convert the datatype of the result.
5467** ^For example, if the internal representation is FLOAT and a text result
5468** is requested, [sqlite3_snprintf()] is used internally to perform the
5469** conversion automatically. ^(The following table details the conversions
5470** that are applied:
5471**
5472** <blockquote>
5473** <table border="1">
5474** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5475**
5476** <tr><td> NULL <td> INTEGER <td> Result is 0
5477** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5478** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5479** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5480** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5481** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5482** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5483** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5484** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5485** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5486** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5487** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5488** <tr><td> TEXT <td> BLOB <td> No change
5489** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5490** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5491** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator
5492** </table>
5493** </blockquote>)^
5494**
5495** Note that when type conversions occur, pointers returned by prior
5496** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5497** sqlite3_column_text16() may be invalidated.
5498** Type conversions and pointer invalidations might occur
5499** in the following cases:
5500**
5501** <ul>
5502** <li> The initial content is a BLOB and sqlite3_column_text() or
5503** sqlite3_column_text16() is called. A zero-terminator might
5504** need to be added to the string.</li>
5505** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5506** sqlite3_column_text16() is called. The content must be converted
5507** to UTF-16.</li>
5508** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5509** sqlite3_column_text() is called. The content must be converted
5510** to UTF-8.</li>
5511** </ul>
5512**
5513** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5514** not invalidate a prior pointer, though of course the content of the buffer
5515** that the prior pointer references will have been modified. Other kinds
5516** of conversion are done in place when it is possible, but sometimes they
5517** are not possible and in those cases prior pointers are invalidated.
5518**
5519** The safest policy is to invoke these routines
5520** in one of the following ways:
5521**
5522** <ul>
5523** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5524** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5525** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5526** </ul>
5527**
5528** In other words, you should call sqlite3_column_text(),
5529** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5530** into the desired format, then invoke sqlite3_column_bytes() or
5531** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5532** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5533** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5534** with calls to sqlite3_column_bytes().
5535**
5536** ^The pointers returned are valid until a type conversion occurs as
5537** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5538** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5539** and BLOBs is freed automatically. Do not pass the pointers returned
5540** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5541** [sqlite3_free()].
5542**
5543** As long as the input parameters are correct, these routines will only
5544** fail if an out-of-memory error occurs during a format conversion.
5545** Only the following subset of interfaces are subject to out-of-memory
5546** errors:
5547**
5548** <ul>
5549** <li> sqlite3_column_blob()
5550** <li> sqlite3_column_text()
5551** <li> sqlite3_column_text16()
5552** <li> sqlite3_column_bytes()
5553** <li> sqlite3_column_bytes16()
5554** </ul>
5555**
5556** If an out-of-memory error occurs, then the return value from these
5557** routines is the same as if the column had contained an SQL NULL value.
5558** Valid SQL NULL returns can be distinguished from out-of-memory errors
5559** by invoking the [sqlite3_errcode()] immediately after the suspect
5560** return value is obtained and before any
5561** other SQLite interface is called on the same [database connection].
5562*/
5563SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5564SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5565SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5566SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5567SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5568SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5569SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5570SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5571SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5572SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5573
5574/*
5575** CAPI3REF: Destroy A Prepared Statement Object
5576** DESTRUCTOR: sqlite3_stmt
5577**
5578** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5579** ^If the most recent evaluation of the statement encountered no errors
5580** or if the statement is never been evaluated, then sqlite3_finalize() returns
5581** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5582** sqlite3_finalize(S) returns the appropriate [error code] or
5583** [extended error code].
5584**
5585** ^The sqlite3_finalize(S) routine can be called at any point during
5586** the life cycle of [prepared statement] S:
5587** before statement S is ever evaluated, after
5588** one or more calls to [sqlite3_reset()], or after any call
5589** to [sqlite3_step()] regardless of whether or not the statement has
5590** completed execution.
5591**
5592** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5593**
5594** The application must finalize every [prepared statement] in order to avoid
5595** resource leaks. It is a grievous error for the application to try to use
5596** a prepared statement after it has been finalized. Any use of a prepared
5597** statement after it has been finalized can result in undefined and
5598** undesirable behavior such as segfaults and heap corruption.
5599*/
5600SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5601
5602/*
5603** CAPI3REF: Reset A Prepared Statement Object
5604** METHOD: sqlite3_stmt
5605**
5606** The sqlite3_reset() function is called to reset a [prepared statement]
5607** object back to its initial state, ready to be re-executed.
5608** ^Any SQL statement variables that had values bound to them using
5609** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5610** Use [sqlite3_clear_bindings()] to reset the bindings.
5611**
5612** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5613** back to the beginning of its program.
5614**
5615** ^The return code from [sqlite3_reset(S)] indicates whether or not
5616** the previous evaluation of prepared statement S completed successfully.
5617** ^If [sqlite3_step(S)] has never before been called on S or if
5618** [sqlite3_step(S)] has not been called since the previous call
5619** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return
5620** [SQLITE_OK].
5621**
5622** ^If the most recent call to [sqlite3_step(S)] for the
5623** [prepared statement] S indicated an error, then
5624** [sqlite3_reset(S)] returns an appropriate [error code].
5625** ^The [sqlite3_reset(S)] interface might also return an [error code]
5626** if there were no prior errors but the process of resetting
5627** the prepared statement caused a new error. ^For example, if an
5628** [INSERT] statement with a [RETURNING] clause is only stepped one time,
5629** that one call to [sqlite3_step(S)] might return SQLITE_ROW but
5630** the overall statement might still fail and the [sqlite3_reset(S)] call
5631** might return SQLITE_BUSY if locking constraints prevent the
5632** database change from committing. Therefore, it is important that
5633** applications check the return code from [sqlite3_reset(S)] even if
5634** no prior call to [sqlite3_step(S)] indicated a problem.
5635**
5636** ^The [sqlite3_reset(S)] interface does not change the values
5637** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5638*/
5639SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5640
5641/*
5642** CAPI3REF: Create Or Redefine SQL Functions
5643** KEYWORDS: {function creation routines}
5644** METHOD: sqlite3
5645**
5646** ^These functions (collectively known as "function creation routines")
5647** are used to add SQL functions or aggregates or to redefine the behavior
5648** of existing SQL functions or aggregates. The only differences between
5649** the three "sqlite3_create_function*" routines are the text encoding
5650** expected for the second parameter (the name of the function being
5651** created) and the presence or absence of a destructor callback for
5652** the application data pointer. Function sqlite3_create_window_function()
5653** is similar, but allows the user to supply the extra callback functions
5654** needed by [aggregate window functions].
5655**
5656** ^The first parameter is the [database connection] to which the SQL
5657** function is to be added. ^If an application uses more than one database
5658** connection then application-defined SQL functions must be added
5659** to each database connection separately.
5660**
5661** ^The second parameter is the name of the SQL function to be created or
5662** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5663** representation, exclusive of the zero-terminator. ^Note that the name
5664** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5665** ^Any attempt to create a function with a longer name
5666** will result in [SQLITE_MISUSE] being returned.
5667**
5668** ^The third parameter (nArg)
5669** is the number of arguments that the SQL function or
5670** aggregate takes. ^If this parameter is -1, then the SQL function or
5671** aggregate may take any number of arguments between 0 and the limit
5672** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5673** parameter is less than -1 or greater than 127 then the behavior is
5674** undefined.
5675**
5676** ^The fourth parameter, eTextRep, specifies what
5677** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5678** its parameters. The application should set this parameter to
5679** [SQLITE_UTF16LE] if the function implementation invokes
5680** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5681** implementation invokes [sqlite3_value_text16be()] on an input, or
5682** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5683** otherwise. ^The same SQL function may be registered multiple times using
5684** different preferred text encodings, with different implementations for
5685** each encoding.
5686** ^When multiple implementations of the same function are available, SQLite
5687** will pick the one that involves the least amount of data conversion.
5688**
5689** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5690** to signal that the function will always return the same result given
5691** the same inputs within a single SQL statement. Most SQL functions are
5692** deterministic. The built-in [random()] SQL function is an example of a
5693** function that is not deterministic. The SQLite query planner is able to
5694** perform additional optimizations on deterministic functions, so use
5695** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5696**
5697** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5698** flag, which if present prevents the function from being invoked from
5699** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
5700** index expressions, or the WHERE clause of partial indexes.
5701**
5702** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
5703** all application-defined SQL functions that do not need to be
5704** used inside of triggers, view, CHECK constraints, or other elements of
5705** the database schema. This flags is especially recommended for SQL
5706** functions that have side effects or reveal internal application state.
5707** Without this flag, an attacker might be able to modify the schema of
5708** a database file to include invocations of the function with parameters
5709** chosen by the attacker, which the application will then execute when
5710** the database file is opened and read.
5711**
5712** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5713** function can gain access to this pointer using [sqlite3_user_data()].)^
5714**
5715** ^The sixth, seventh and eighth parameters passed to the three
5716** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5717** pointers to C-language functions that implement the SQL function or
5718** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5719** callback only; NULL pointers must be passed as the xStep and xFinal
5720** parameters. ^An aggregate SQL function requires an implementation of xStep
5721** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5722** SQL function or aggregate, pass NULL pointers for all three function
5723** callbacks.
5724**
5725** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5726** and xInverse) passed to sqlite3_create_window_function are pointers to
5727** C-language callbacks that implement the new function. xStep and xFinal
5728** must both be non-NULL. xValue and xInverse may either both be NULL, in
5729** which case a regular aggregate function is created, or must both be
5730** non-NULL, in which case the new function may be used as either an aggregate
5731** or aggregate window function. More details regarding the implementation
5732** of aggregate window functions are
5733** [user-defined window functions|available here].
5734**
5735** ^(If the final parameter to sqlite3_create_function_v2() or
5736** sqlite3_create_window_function() is not NULL, then it is destructor for
5737** the application data pointer. The destructor is invoked when the function
5738** is deleted, either by being overloaded or when the database connection
5739** closes.)^ ^The destructor is also invoked if the call to
5740** sqlite3_create_function_v2() fails. ^When the destructor callback is
5741** invoked, it is passed a single argument which is a copy of the application
5742** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5743**
5744** ^It is permitted to register multiple implementations of the same
5745** functions with the same name but with either differing numbers of
5746** arguments or differing preferred text encodings. ^SQLite will use
5747** the implementation that most closely matches the way in which the
5748** SQL function is used. ^A function implementation with a non-negative
5749** nArg parameter is a better match than a function implementation with
5750** a negative nArg. ^A function where the preferred text encoding
5751** matches the database encoding is a better
5752** match than a function where the encoding is different.
5753** ^A function where the encoding difference is between UTF16le and UTF16be
5754** is a closer match than a function where the encoding difference is
5755** between UTF8 and UTF16.
5756**
5757** ^Built-in functions may be overloaded by new application-defined functions.
5758**
5759** ^An application-defined function is permitted to call other
5760** SQLite interfaces. However, such calls must not
5761** close the database connection nor finalize or reset the prepared
5762** statement in which the function is running.
5763*/
5764SQLITE_API int sqlite3_create_function(
5765 sqlite3 *db,
5766 const char *zFunctionName,
5767 int nArg,
5768 int eTextRep,
5769 void *pApp,
5770 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5771 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5772 void (*xFinal)(sqlite3_context*)
5773);
5774SQLITE_API int sqlite3_create_function16(
5775 sqlite3 *db,
5776 const void *zFunctionName,
5777 int nArg,
5778 int eTextRep,
5779 void *pApp,
5780 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5781 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5782 void (*xFinal)(sqlite3_context*)
5783);
5784SQLITE_API int sqlite3_create_function_v2(
5785 sqlite3 *db,
5786 const char *zFunctionName,
5787 int nArg,
5788 int eTextRep,
5789 void *pApp,
5790 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5791 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5792 void (*xFinal)(sqlite3_context*),
5793 void(*xDestroy)(void*)
5794);
5795SQLITE_API int sqlite3_create_window_function(
5796 sqlite3 *db,
5797 const char *zFunctionName,
5798 int nArg,
5799 int eTextRep,
5800 void *pApp,
5801 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5802 void (*xFinal)(sqlite3_context*),
5803 void (*xValue)(sqlite3_context*),
5804 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5805 void(*xDestroy)(void*)
5806);
5807
5808/*
5809** CAPI3REF: Text Encodings
5810**
5811** These constant define integer codes that represent the various
5812** text encodings supported by SQLite.
5813*/
5814#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5815#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5816#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5817#define SQLITE_UTF16 4 /* Use native byte order */
5818#define SQLITE_ANY 5 /* Deprecated */
5819#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
5820
5821/*
5822** CAPI3REF: Function Flags
5823**
5824** These constants may be ORed together with the
5825** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5826** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5827** [sqlite3_create_function_v2()].
5828**
5829** <dl>
5830** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
5831** The SQLITE_DETERMINISTIC flag means that the new function always gives
5832** the same output when the input parameters are the same.
5833** The [abs|abs() function] is deterministic, for example, but
5834** [randomblob|randomblob()] is not. Functions must
5835** be deterministic in order to be used in certain contexts such as
5836** with the WHERE clause of [partial indexes] or in [generated columns].
5837** SQLite might also optimize deterministic functions by factoring them
5838** out of inner loops.
5839** </dd>
5840**
5841** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
5842** The SQLITE_DIRECTONLY flag means that the function may only be invoked
5843** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5844** schema structures such as [CHECK constraints], [DEFAULT clauses],
5845** [expression indexes], [partial indexes], or [generated columns].
5846** <p>
5847** The SQLITE_DIRECTONLY flag is recommended for any
5848** [application-defined SQL function]
5849** that has side-effects or that could potentially leak sensitive information.
5850** This will prevent attacks in which an application is tricked
5851** into using a database file that has had its schema surreptitiously
5852** modified to invoke the application-defined function in ways that are
5853** harmful.
5854** <p>
5855** Some people say it is good practice to set SQLITE_DIRECTONLY on all
5856** [application-defined SQL functions], regardless of whether or not they
5857** are security sensitive, as doing so prevents those functions from being used
5858** inside of the database schema, and thus ensures that the database
5859** can be inspected and modified using generic tools (such as the [CLI])
5860** that do not have access to the application-defined functions.
5861** </dd>
5862**
5863** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
5864** The SQLITE_INNOCUOUS flag means that the function is unlikely
5865** to cause problems even if misused. An innocuous function should have
5866** no side effects and should not depend on any values other than its
5867** input parameters. The [abs|abs() function] is an example of an
5868** innocuous function.
5869** The [load_extension() SQL function] is not innocuous because of its
5870** side effects.
5871** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
5872** exactly the same. The [random|random() function] is an example of a
5873** function that is innocuous but not deterministic.
5874** <p>Some heightened security settings
5875** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
5876** disable the use of SQL functions inside views and triggers and in
5877** schema structures such as [CHECK constraints], [DEFAULT clauses],
5878** [expression indexes], [partial indexes], and [generated columns] unless
5879** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5880** are innocuous. Developers are advised to avoid using the
5881** SQLITE_INNOCUOUS flag for application-defined functions unless the
5882** function has been carefully audited and found to be free of potentially
5883** security-adverse side-effects and information-leaks.
5884** </dd>
5885**
5886** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5887** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5888** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5889** Specifying this flag makes no difference for scalar or aggregate user
5890** functions. However, if it is not specified for a user-defined window
5891** function, then any sub-types belonging to arguments passed to the window
5892** function may be discarded before the window function is called (i.e.
5893** sqlite3_value_subtype() will always return 0).
5894** </dd>
5895** </dl>
5896*/
5897#define SQLITE_DETERMINISTIC 0x000000800
5898#define SQLITE_DIRECTONLY 0x000080000
5899#define SQLITE_SUBTYPE 0x000100000
5900#define SQLITE_INNOCUOUS 0x000200000
5901
5902/*
5903** CAPI3REF: Deprecated Functions
5904** DEPRECATED
5905**
5906** These functions are [deprecated]. In order to maintain
5907** backwards compatibility with older code, these functions continue
5908** to be supported. However, new applications should avoid
5909** the use of these functions. To encourage programmers to avoid
5910** these functions, we will not explain what they do.
5911*/
5912#ifndef SQLITE_OMIT_DEPRECATED
5913SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5914SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5915SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5916SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5917SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5918SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5919 void*,sqlite3_int64);
5920#endif
5921
5922/*
5923** CAPI3REF: Obtaining SQL Values
5924** METHOD: sqlite3_value
5925**
5926** <b>Summary:</b>
5927** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5928** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5929** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5930** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5931** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5932** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5933** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5934** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5935** the native byteorder
5936** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5937** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5938** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5939** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
5940** or a UTF-8 TEXT in bytes
5941** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
5942** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5943** TEXT in bytes
5944** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
5945** datatype of the value
5946** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
5947** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
5948** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
5949** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
5950** against a virtual table.
5951** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
5952** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
5953** </table></blockquote>
5954**
5955** <b>Details:</b>
5956**
5957** These routines extract type, size, and content information from
5958** [protected sqlite3_value] objects. Protected sqlite3_value objects
5959** are used to pass parameter information into the functions that
5960** implement [application-defined SQL functions] and [virtual tables].
5961**
5962** These routines work only with [protected sqlite3_value] objects.
5963** Any attempt to use these routines on an [unprotected sqlite3_value]
5964** is not threadsafe.
5965**
5966** ^These routines work just like the corresponding [column access functions]
5967** except that these routines take a single [protected sqlite3_value] object
5968** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
5969**
5970** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5971** in the native byte-order of the host machine. ^The
5972** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
5973** extract UTF-16 strings as big-endian and little-endian respectively.
5974**
5975** ^If [sqlite3_value] object V was initialized
5976** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
5977** and if X and Y are strings that compare equal according to strcmp(X,Y),
5978** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
5979** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5980** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5981**
5982** ^(The sqlite3_value_type(V) interface returns the
5983** [SQLITE_INTEGER | datatype code] for the initial datatype of the
5984** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
5985** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
5986** Other interfaces might change the datatype for an sqlite3_value object.
5987** For example, if the datatype is initially SQLITE_INTEGER and
5988** sqlite3_value_text(V) is called to extract a text value for that
5989** integer, then subsequent calls to sqlite3_value_type(V) might return
5990** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
5991** occurs is undefined and may change from one release of SQLite to the next.
5992**
5993** ^(The sqlite3_value_numeric_type() interface attempts to apply
5994** numeric affinity to the value. This means that an attempt is
5995** made to convert the value to an integer or floating point. If
5996** such a conversion is possible without loss of information (in other
5997** words, if the value is a string that looks like a number)
5998** then the conversion is performed. Otherwise no conversion occurs.
5999** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6000**
6001** ^Within the [xUpdate] method of a [virtual table], the
6002** sqlite3_value_nochange(X) interface returns true if and only if
6003** the column corresponding to X is unchanged by the UPDATE operation
6004** that the xUpdate method call was invoked to implement and if
6005** and the prior [xColumn] method call that was invoked to extracted
6006** the value for that column returned without setting a result (probably
6007** because it queried [sqlite3_vtab_nochange()] and found that the column
6008** was unchanging). ^Within an [xUpdate] method, any value for which
6009** sqlite3_value_nochange(X) is true will in all other respects appear
6010** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
6011** than within an [xUpdate] method call for an UPDATE statement, then
6012** the return value is arbitrary and meaningless.
6013**
6014** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6015** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6016** interfaces. ^If X comes from an SQL literal value, or a table column,
6017** or an expression, then sqlite3_value_frombind(X) returns zero.
6018**
6019** Please pay particular attention to the fact that the pointer returned
6020** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6021** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6022** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6023** or [sqlite3_value_text16()].
6024**
6025** These routines must be called from the same thread as
6026** the SQL function that supplied the [sqlite3_value*] parameters.
6027**
6028** As long as the input parameter is correct, these routines can only
6029** fail if an out-of-memory error occurs during a format conversion.
6030** Only the following subset of interfaces are subject to out-of-memory
6031** errors:
6032**
6033** <ul>
6034** <li> sqlite3_value_blob()
6035** <li> sqlite3_value_text()
6036** <li> sqlite3_value_text16()
6037** <li> sqlite3_value_text16le()
6038** <li> sqlite3_value_text16be()
6039** <li> sqlite3_value_bytes()
6040** <li> sqlite3_value_bytes16()
6041** </ul>
6042**
6043** If an out-of-memory error occurs, then the return value from these
6044** routines is the same as if the column had contained an SQL NULL value.
6045** Valid SQL NULL returns can be distinguished from out-of-memory errors
6046** by invoking the [sqlite3_errcode()] immediately after the suspect
6047** return value is obtained and before any
6048** other SQLite interface is called on the same [database connection].
6049*/
6050SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6051SQLITE_API double sqlite3_value_double(sqlite3_value*);
6052SQLITE_API int sqlite3_value_int(sqlite3_value*);
6053SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6054SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6055SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6056SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6057SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6058SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6059SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6060SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6061SQLITE_API int sqlite3_value_type(sqlite3_value*);
6062SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6063SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6064SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6065
6066/*
6067** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
6068** METHOD: sqlite3_value
6069**
6070** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
6071** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
6072** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
6073** returns something other than SQLITE_TEXT, then the return value from
6074** sqlite3_value_encoding(X) is meaningless. ^Calls to
6075** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
6076** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
6077** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
6078** thus change the return from subsequent calls to sqlite3_value_encoding(X).
6079**
6080** This routine is intended for used by applications that test and validate
6081** the SQLite implementation. This routine is inquiring about the opaque
6082** internal state of an [sqlite3_value] object. Ordinary applications should
6083** not need to know what the internal state of an sqlite3_value object is and
6084** hence should not need to use this interface.
6085*/
6086SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6087
6088/*
6089** CAPI3REF: Finding The Subtype Of SQL Values
6090** METHOD: sqlite3_value
6091**
6092** The sqlite3_value_subtype(V) function returns the subtype for
6093** an [application-defined SQL function] argument V. The subtype
6094** information can be used to pass a limited amount of context from
6095** one SQL function to another. Use the [sqlite3_result_subtype()]
6096** routine to set the subtype for the return value of an SQL function.
6097*/
6098SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6099
6100/*
6101** CAPI3REF: Copy And Free SQL Values
6102** METHOD: sqlite3_value
6103**
6104** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6105** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
6106** is a [protected sqlite3_value] object even if the input is not.
6107** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6108** memory allocation fails. ^If V is a [pointer value], then the result
6109** of sqlite3_value_dup(V) is a NULL value.
6110**
6111** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6112** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6113** then sqlite3_value_free(V) is a harmless no-op.
6114*/
6115SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6116SQLITE_API void sqlite3_value_free(sqlite3_value*);
6117
6118/*
6119** CAPI3REF: Obtain Aggregate Function Context
6120** METHOD: sqlite3_context
6121**
6122** Implementations of aggregate SQL functions use this
6123** routine to allocate memory for storing their state.
6124**
6125** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6126** for a particular aggregate function, SQLite allocates
6127** N bytes of memory, zeroes out that memory, and returns a pointer
6128** to the new memory. ^On second and subsequent calls to
6129** sqlite3_aggregate_context() for the same aggregate function instance,
6130** the same buffer is returned. Sqlite3_aggregate_context() is normally
6131** called once for each invocation of the xStep callback and then one
6132** last time when the xFinal callback is invoked. ^(When no rows match
6133** an aggregate query, the xStep() callback of the aggregate function
6134** implementation is never called and xFinal() is called exactly once.
6135** In those cases, sqlite3_aggregate_context() might be called for the
6136** first time from within xFinal().)^
6137**
6138** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6139** when first called if N is less than or equal to zero or if a memory
6140** allocation error occurs.
6141**
6142** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6143** determined by the N parameter on first successful call. Changing the
6144** value of N in any subsequent call to sqlite3_aggregate_context() within
6145** the same aggregate function instance will not resize the memory
6146** allocation.)^ Within the xFinal callback, it is customary to set
6147** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6148** pointless memory allocations occur.
6149**
6150** ^SQLite automatically frees the memory allocated by
6151** sqlite3_aggregate_context() when the aggregate query concludes.
6152**
6153** The first parameter must be a copy of the
6154** [sqlite3_context | SQL function context] that is the first parameter
6155** to the xStep or xFinal callback routine that implements the aggregate
6156** function.
6157**
6158** This routine must be called from the same thread in which
6159** the aggregate SQL function is running.
6160*/
6161SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6162
6163/*
6164** CAPI3REF: User Data For Functions
6165** METHOD: sqlite3_context
6166**
6167** ^The sqlite3_user_data() interface returns a copy of
6168** the pointer that was the pUserData parameter (the 5th parameter)
6169** of the [sqlite3_create_function()]
6170** and [sqlite3_create_function16()] routines that originally
6171** registered the application defined function.
6172**
6173** This routine must be called from the same thread in which
6174** the application-defined function is running.
6175*/
6176SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6177
6178/*
6179** CAPI3REF: Database Connection For Functions
6180** METHOD: sqlite3_context
6181**
6182** ^The sqlite3_context_db_handle() interface returns a copy of
6183** the pointer to the [database connection] (the 1st parameter)
6184** of the [sqlite3_create_function()]
6185** and [sqlite3_create_function16()] routines that originally
6186** registered the application defined function.
6187*/
6188SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6189
6190/*
6191** CAPI3REF: Function Auxiliary Data
6192** METHOD: sqlite3_context
6193**
6194** These functions may be used by (non-aggregate) SQL functions to
6195** associate metadata with argument values. If the same value is passed to
6196** multiple invocations of the same SQL function during query execution, under
6197** some circumstances the associated metadata may be preserved. An example
6198** of where this might be useful is in a regular-expression matching
6199** function. The compiled version of the regular expression can be stored as
6200** metadata associated with the pattern string.
6201** Then as long as the pattern string remains the same,
6202** the compiled regular expression can be reused on multiple
6203** invocations of the same function.
6204**
6205** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6206** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6207** value to the application-defined function. ^N is zero for the left-most
6208** function argument. ^If there is no metadata
6209** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6210** returns a NULL pointer.
6211**
6212** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6213** argument of the application-defined function. ^Subsequent
6214** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6215** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6216** NULL if the metadata has been discarded.
6217** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6218** SQLite will invoke the destructor function X with parameter P exactly
6219** once, when the metadata is discarded.
6220** SQLite is free to discard the metadata at any time, including: <ul>
6221** <li> ^(when the corresponding function parameter changes)^, or
6222** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6223** SQL statement)^, or
6224** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6225** parameter)^, or
6226** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6227** allocation error occurs.)^ </ul>
6228**
6229** Note the last bullet in particular. The destructor X in
6230** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6231** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6232** should be called near the end of the function implementation and the
6233** function implementation should not make any use of P after
6234** sqlite3_set_auxdata() has been called.
6235**
6236** ^(In practice, metadata is preserved between function calls for
6237** function parameters that are compile-time constants, including literal
6238** values and [parameters] and expressions composed from the same.)^
6239**
6240** The value of the N parameter to these interfaces should be non-negative.
6241** Future enhancements may make use of negative N values to define new
6242** kinds of function caching behavior.
6243**
6244** These routines must be called from the same thread in which
6245** the SQL function is running.
6246*/
6247SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6248SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6249
6250
6251/*
6252** CAPI3REF: Constants Defining Special Destructor Behavior
6253**
6254** These are special values for the destructor that is passed in as the
6255** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6256** argument is SQLITE_STATIC, it means that the content pointer is constant
6257** and will never change. It does not need to be destroyed. ^The
6258** SQLITE_TRANSIENT value means that the content will likely change in
6259** the near future and that SQLite should make its own private copy of
6260** the content before returning.
6261**
6262** The typedef is necessary to work around problems in certain
6263** C++ compilers.
6264*/
6265typedef void (*sqlite3_destructor_type)(void*);
6266#define SQLITE_STATIC ((sqlite3_destructor_type)0)
6267#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6268
6269/*
6270** CAPI3REF: Setting The Result Of An SQL Function
6271** METHOD: sqlite3_context
6272**
6273** These routines are used by the xFunc or xFinal callbacks that
6274** implement SQL functions and aggregates. See
6275** [sqlite3_create_function()] and [sqlite3_create_function16()]
6276** for additional information.
6277**
6278** These functions work very much like the [parameter binding] family of
6279** functions used to bind values to host parameters in prepared statements.
6280** Refer to the [SQL parameter] documentation for additional information.
6281**
6282** ^The sqlite3_result_blob() interface sets the result from
6283** an application-defined function to be the BLOB whose content is pointed
6284** to by the second parameter and which is N bytes long where N is the
6285** third parameter.
6286**
6287** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6288** interfaces set the result of the application-defined function to be
6289** a BLOB containing all zero bytes and N bytes in size.
6290**
6291** ^The sqlite3_result_double() interface sets the result from
6292** an application-defined function to be a floating point value specified
6293** by its 2nd argument.
6294**
6295** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6296** cause the implemented SQL function to throw an exception.
6297** ^SQLite uses the string pointed to by the
6298** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6299** as the text of an error message. ^SQLite interprets the error
6300** message string from sqlite3_result_error() as UTF-8. ^SQLite
6301** interprets the string from sqlite3_result_error16() as UTF-16 using
6302** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6303** ^If the third parameter to sqlite3_result_error()
6304** or sqlite3_result_error16() is negative then SQLite takes as the error
6305** message all text up through the first zero character.
6306** ^If the third parameter to sqlite3_result_error() or
6307** sqlite3_result_error16() is non-negative then SQLite takes that many
6308** bytes (not characters) from the 2nd parameter as the error message.
6309** ^The sqlite3_result_error() and sqlite3_result_error16()
6310** routines make a private copy of the error message text before
6311** they return. Hence, the calling function can deallocate or
6312** modify the text after they return without harm.
6313** ^The sqlite3_result_error_code() function changes the error code
6314** returned by SQLite as a result of an error in a function. ^By default,
6315** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6316** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6317**
6318** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6319** error indicating that a string or BLOB is too long to represent.
6320**
6321** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6322** error indicating that a memory allocation failed.
6323**
6324** ^The sqlite3_result_int() interface sets the return value
6325** of the application-defined function to be the 32-bit signed integer
6326** value given in the 2nd argument.
6327** ^The sqlite3_result_int64() interface sets the return value
6328** of the application-defined function to be the 64-bit signed integer
6329** value given in the 2nd argument.
6330**
6331** ^The sqlite3_result_null() interface sets the return value
6332** of the application-defined function to be NULL.
6333**
6334** ^The sqlite3_result_text(), sqlite3_result_text16(),
6335** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6336** set the return value of the application-defined function to be
6337** a text string which is represented as UTF-8, UTF-16 native byte order,
6338** UTF-16 little endian, or UTF-16 big endian, respectively.
6339** ^The sqlite3_result_text64() interface sets the return value of an
6340** application-defined function to be a text string in an encoding
6341** specified by the fifth (and last) parameter, which must be one
6342** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6343** ^SQLite takes the text result from the application from
6344** the 2nd parameter of the sqlite3_result_text* interfaces.
6345** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6346** other than sqlite3_result_text64() is negative, then SQLite computes
6347** the string length itself by searching the 2nd parameter for the first
6348** zero character.
6349** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6350** is non-negative, then as many bytes (not characters) of the text
6351** pointed to by the 2nd parameter are taken as the application-defined
6352** function result. If the 3rd parameter is non-negative, then it
6353** must be the byte offset into the string where the NUL terminator would
6354** appear if the string where NUL terminated. If any NUL characters occur
6355** in the string at a byte offset that is less than the value of the 3rd
6356** parameter, then the resulting string will contain embedded NULs and the
6357** result of expressions operating on strings with embedded NULs is undefined.
6358** ^If the 4th parameter to the sqlite3_result_text* interfaces
6359** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6360** function as the destructor on the text or BLOB result when it has
6361** finished using that result.
6362** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6363** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6364** assumes that the text or BLOB result is in constant space and does not
6365** copy the content of the parameter nor call a destructor on the content
6366** when it has finished using that result.
6367** ^If the 4th parameter to the sqlite3_result_text* interfaces
6368** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6369** then SQLite makes a copy of the result into space obtained
6370** from [sqlite3_malloc()] before it returns.
6371**
6372** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6373** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6374** when the encoding is not UTF8, if the input UTF16 begins with a
6375** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6376** string and the rest of the string is interpreted according to the
6377** byte-order specified by the BOM. ^The byte-order specified by
6378** the BOM at the beginning of the text overrides the byte-order
6379** specified by the interface procedure. ^So, for example, if
6380** sqlite3_result_text16le() is invoked with text that begins
6381** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6382** first two bytes of input are skipped and the remaining input
6383** is interpreted as UTF16BE text.
6384**
6385** ^For UTF16 input text to the sqlite3_result_text16(),
6386** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6387** sqlite3_result_text64() routines, if the text contains invalid
6388** UTF16 characters, the invalid characters might be converted
6389** into the unicode replacement character, U+FFFD.
6390**
6391** ^The sqlite3_result_value() interface sets the result of
6392** the application-defined function to be a copy of the
6393** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6394** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6395** so that the [sqlite3_value] specified in the parameter may change or
6396** be deallocated after sqlite3_result_value() returns without harm.
6397** ^A [protected sqlite3_value] object may always be used where an
6398** [unprotected sqlite3_value] object is required, so either
6399** kind of [sqlite3_value] object can be used with this interface.
6400**
6401** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6402** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6403** also associates the host-language pointer P or type T with that
6404** NULL value such that the pointer can be retrieved within an
6405** [application-defined SQL function] using [sqlite3_value_pointer()].
6406** ^If the D parameter is not NULL, then it is a pointer to a destructor
6407** for the P parameter. ^SQLite invokes D with P as its only argument
6408** when SQLite is finished with P. The T parameter should be a static
6409** string and preferably a string literal. The sqlite3_result_pointer()
6410** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6411**
6412** If these routines are called from within the different thread
6413** than the one containing the application-defined function that received
6414** the [sqlite3_context] pointer, the results are undefined.
6415*/
6416SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6417SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6418 sqlite3_uint64,void(*)(void*));
6419SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6420SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6421SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6422SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6423SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6424SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6425SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6426SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6427SQLITE_API void sqlite3_result_null(sqlite3_context*);
6428SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6429SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6430 void(*)(void*), unsigned char encoding);
6431SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6432SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6433SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6434SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6435SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6436SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6437SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6438
6439
6440/*
6441** CAPI3REF: Setting The Subtype Of An SQL Function
6442** METHOD: sqlite3_context
6443**
6444** The sqlite3_result_subtype(C,T) function causes the subtype of
6445** the result from the [application-defined SQL function] with
6446** [sqlite3_context] C to be the value T. Only the lower 8 bits
6447** of the subtype T are preserved in current versions of SQLite;
6448** higher order bits are discarded.
6449** The number of subtype bytes preserved by SQLite might increase
6450** in future releases of SQLite.
6451*/
6452SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6453
6454/*
6455** CAPI3REF: Define New Collating Sequences
6456** METHOD: sqlite3
6457**
6458** ^These functions add, remove, or modify a [collation] associated
6459** with the [database connection] specified as the first argument.
6460**
6461** ^The name of the collation is a UTF-8 string
6462** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6463** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6464** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6465** considered to be the same name.
6466**
6467** ^(The third argument (eTextRep) must be one of the constants:
6468** <ul>
6469** <li> [SQLITE_UTF8],
6470** <li> [SQLITE_UTF16LE],
6471** <li> [SQLITE_UTF16BE],
6472** <li> [SQLITE_UTF16], or
6473** <li> [SQLITE_UTF16_ALIGNED].
6474** </ul>)^
6475** ^The eTextRep argument determines the encoding of strings passed
6476** to the collating function callback, xCompare.
6477** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6478** force strings to be UTF16 with native byte order.
6479** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6480** on an even byte address.
6481**
6482** ^The fourth argument, pArg, is an application data pointer that is passed
6483** through as the first argument to the collating function callback.
6484**
6485** ^The fifth argument, xCompare, is a pointer to the collating function.
6486** ^Multiple collating functions can be registered using the same name but
6487** with different eTextRep parameters and SQLite will use whichever
6488** function requires the least amount of data transformation.
6489** ^If the xCompare argument is NULL then the collating function is
6490** deleted. ^When all collating functions having the same name are deleted,
6491** that collation is no longer usable.
6492**
6493** ^The collating function callback is invoked with a copy of the pArg
6494** application data pointer and with two strings in the encoding specified
6495** by the eTextRep argument. The two integer parameters to the collating
6496** function callback are the length of the two strings, in bytes. The collating
6497** function must return an integer that is negative, zero, or positive
6498** if the first string is less than, equal to, or greater than the second,
6499** respectively. A collating function must always return the same answer
6500** given the same inputs. If two or more collating functions are registered
6501** to the same collation name (using different eTextRep values) then all
6502** must give an equivalent answer when invoked with equivalent strings.
6503** The collating function must obey the following properties for all
6504** strings A, B, and C:
6505**
6506** <ol>
6507** <li> If A==B then B==A.
6508** <li> If A==B and B==C then A==C.
6509** <li> If A&lt;B THEN B&gt;A.
6510** <li> If A&lt;B and B&lt;C then A&lt;C.
6511** </ol>
6512**
6513** If a collating function fails any of the above constraints and that
6514** collating function is registered and used, then the behavior of SQLite
6515** is undefined.
6516**
6517** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6518** with the addition that the xDestroy callback is invoked on pArg when
6519** the collating function is deleted.
6520** ^Collating functions are deleted when they are overridden by later
6521** calls to the collation creation functions or when the
6522** [database connection] is closed using [sqlite3_close()].
6523**
6524** ^The xDestroy callback is <u>not</u> called if the
6525** sqlite3_create_collation_v2() function fails. Applications that invoke
6526** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6527** check the return code and dispose of the application data pointer
6528** themselves rather than expecting SQLite to deal with it for them.
6529** This is different from every other SQLite interface. The inconsistency
6530** is unfortunate but cannot be changed without breaking backwards
6531** compatibility.
6532**
6533** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6534*/
6535SQLITE_API int sqlite3_create_collation(
6536 sqlite3*,
6537 const char *zName,
6538 int eTextRep,
6539 void *pArg,
6540 int(*xCompare)(void*,int,const void*,int,const void*)
6541);
6542SQLITE_API int sqlite3_create_collation_v2(
6543 sqlite3*,
6544 const char *zName,
6545 int eTextRep,
6546 void *pArg,
6547 int(*xCompare)(void*,int,const void*,int,const void*),
6548 void(*xDestroy)(void*)
6549);
6550SQLITE_API int sqlite3_create_collation16(
6551 sqlite3*,
6552 const void *zName,
6553 int eTextRep,
6554 void *pArg,
6555 int(*xCompare)(void*,int,const void*,int,const void*)
6556);
6557
6558/*
6559** CAPI3REF: Collation Needed Callbacks
6560** METHOD: sqlite3
6561**
6562** ^To avoid having to register all collation sequences before a database
6563** can be used, a single callback function may be registered with the
6564** [database connection] to be invoked whenever an undefined collation
6565** sequence is required.
6566**
6567** ^If the function is registered using the sqlite3_collation_needed() API,
6568** then it is passed the names of undefined collation sequences as strings
6569** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6570** the names are passed as UTF-16 in machine native byte order.
6571** ^A call to either function replaces the existing collation-needed callback.
6572**
6573** ^(When the callback is invoked, the first argument passed is a copy
6574** of the second argument to sqlite3_collation_needed() or
6575** sqlite3_collation_needed16(). The second argument is the database
6576** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6577** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6578** sequence function required. The fourth parameter is the name of the
6579** required collation sequence.)^
6580**
6581** The callback function should register the desired collation using
6582** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6583** [sqlite3_create_collation_v2()].
6584*/
6585SQLITE_API int sqlite3_collation_needed(
6586 sqlite3*,
6587 void*,
6588 void(*)(void*,sqlite3*,int eTextRep,const char*)
6589);
6590SQLITE_API int sqlite3_collation_needed16(
6591 sqlite3*,
6592 void*,
6593 void(*)(void*,sqlite3*,int eTextRep,const void*)
6594);
6595
6596#ifdef SQLITE_ENABLE_CEROD
6597/*
6598** Specify the activation key for a CEROD database. Unless
6599** activated, none of the CEROD routines will work.
6600*/
6601SQLITE_API void sqlite3_activate_cerod(
6602 const char *zPassPhrase /* Activation phrase */
6603);
6604#endif
6605
6606/*
6607** CAPI3REF: Suspend Execution For A Short Time
6608**
6609** The sqlite3_sleep() function causes the current thread to suspend execution
6610** for at least a number of milliseconds specified in its parameter.
6611**
6612** If the operating system does not support sleep requests with
6613** millisecond time resolution, then the time will be rounded up to
6614** the nearest second. The number of milliseconds of sleep actually
6615** requested from the operating system is returned.
6616**
6617** ^SQLite implements this interface by calling the xSleep()
6618** method of the default [sqlite3_vfs] object. If the xSleep() method
6619** of the default VFS is not implemented correctly, or not implemented at
6620** all, then the behavior of sqlite3_sleep() may deviate from the description
6621** in the previous paragraphs.
6622**
6623** If a negative argument is passed to sqlite3_sleep() the results vary by
6624** VFS and operating system. Some system treat a negative argument as an
6625** instruction to sleep forever. Others understand it to mean do not sleep
6626** at all. ^In SQLite version 3.42.0 and later, a negative
6627** argument passed into sqlite3_sleep() is changed to zero before it is relayed
6628** down into the xSleep method of the VFS.
6629*/
6630SQLITE_API int sqlite3_sleep(int);
6631
6632/*
6633** CAPI3REF: Name Of The Folder Holding Temporary Files
6634**
6635** ^(If this global variable is made to point to a string which is
6636** the name of a folder (a.k.a. directory), then all temporary files
6637** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6638** will be placed in that directory.)^ ^If this variable
6639** is a NULL pointer, then SQLite performs a search for an appropriate
6640** temporary file directory.
6641**
6642** Applications are strongly discouraged from using this global variable.
6643** It is required to set a temporary folder on Windows Runtime (WinRT).
6644** But for all other platforms, it is highly recommended that applications
6645** neither read nor write this variable. This global variable is a relic
6646** that exists for backwards compatibility of legacy applications and should
6647** be avoided in new projects.
6648**
6649** It is not safe to read or modify this variable in more than one
6650** thread at a time. It is not safe to read or modify this variable
6651** if a [database connection] is being used at the same time in a separate
6652** thread.
6653** It is intended that this variable be set once
6654** as part of process initialization and before any SQLite interface
6655** routines have been called and that this variable remain unchanged
6656** thereafter.
6657**
6658** ^The [temp_store_directory pragma] may modify this variable and cause
6659** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6660** the [temp_store_directory pragma] always assumes that any string
6661** that this variable points to is held in memory obtained from
6662** [sqlite3_malloc] and the pragma may attempt to free that memory
6663** using [sqlite3_free].
6664** Hence, if this variable is modified directly, either it should be
6665** made NULL or made to point to memory obtained from [sqlite3_malloc]
6666** or else the use of the [temp_store_directory pragma] should be avoided.
6667** Except when requested by the [temp_store_directory pragma], SQLite
6668** does not free the memory that sqlite3_temp_directory points to. If
6669** the application wants that memory to be freed, it must do
6670** so itself, taking care to only do so after all [database connection]
6671** objects have been destroyed.
6672**
6673** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6674** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6675** features that require the use of temporary files may fail. Here is an
6676** example of how to do this using C++ with the Windows Runtime:
6677**
6678** <blockquote><pre>
6679** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6680** &nbsp; TemporaryFolder->Path->Data();
6681** char zPathBuf&#91;MAX_PATH + 1&#93;;
6682** memset(zPathBuf, 0, sizeof(zPathBuf));
6683** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6684** &nbsp; NULL, NULL);
6685** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6686** </pre></blockquote>
6687*/
6688SQLITE_API char *sqlite3_temp_directory;
6689
6690/*
6691** CAPI3REF: Name Of The Folder Holding Database Files
6692**
6693** ^(If this global variable is made to point to a string which is
6694** the name of a folder (a.k.a. directory), then all database files
6695** specified with a relative pathname and created or accessed by
6696** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6697** to be relative to that directory.)^ ^If this variable is a NULL
6698** pointer, then SQLite assumes that all database files specified
6699** with a relative pathname are relative to the current directory
6700** for the process. Only the windows VFS makes use of this global
6701** variable; it is ignored by the unix VFS.
6702**
6703** Changing the value of this variable while a database connection is
6704** open can result in a corrupt database.
6705**
6706** It is not safe to read or modify this variable in more than one
6707** thread at a time. It is not safe to read or modify this variable
6708** if a [database connection] is being used at the same time in a separate
6709** thread.
6710** It is intended that this variable be set once
6711** as part of process initialization and before any SQLite interface
6712** routines have been called and that this variable remain unchanged
6713** thereafter.
6714**
6715** ^The [data_store_directory pragma] may modify this variable and cause
6716** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6717** the [data_store_directory pragma] always assumes that any string
6718** that this variable points to is held in memory obtained from
6719** [sqlite3_malloc] and the pragma may attempt to free that memory
6720** using [sqlite3_free].
6721** Hence, if this variable is modified directly, either it should be
6722** made NULL or made to point to memory obtained from [sqlite3_malloc]
6723** or else the use of the [data_store_directory pragma] should be avoided.
6724*/
6725SQLITE_API char *sqlite3_data_directory;
6726
6727/*
6728** CAPI3REF: Win32 Specific Interface
6729**
6730** These interfaces are available only on Windows. The
6731** [sqlite3_win32_set_directory] interface is used to set the value associated
6732** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6733** zValue, depending on the value of the type parameter. The zValue parameter
6734** should be NULL to cause the previous value to be freed via [sqlite3_free];
6735** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6736** prior to being used. The [sqlite3_win32_set_directory] interface returns
6737** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6738** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6739** [sqlite3_data_directory] variable is intended to act as a replacement for
6740** the current directory on the sub-platforms of Win32 where that concept is
6741** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6742** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6743** sqlite3_win32_set_directory interface except the string parameter must be
6744** UTF-8 or UTF-16, respectively.
6745*/
6746SQLITE_API int sqlite3_win32_set_directory(
6747 unsigned long type, /* Identifier for directory being set or reset */
6748 void *zValue /* New value for directory being set or reset */
6749);
6750SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6751SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6752
6753/*
6754** CAPI3REF: Win32 Directory Types
6755**
6756** These macros are only available on Windows. They define the allowed values
6757** for the type argument to the [sqlite3_win32_set_directory] interface.
6758*/
6759#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
6760#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
6761
6762/*
6763** CAPI3REF: Test For Auto-Commit Mode
6764** KEYWORDS: {autocommit mode}
6765** METHOD: sqlite3
6766**
6767** ^The sqlite3_get_autocommit() interface returns non-zero or
6768** zero if the given database connection is or is not in autocommit mode,
6769** respectively. ^Autocommit mode is on by default.
6770** ^Autocommit mode is disabled by a [BEGIN] statement.
6771** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6772**
6773** If certain kinds of errors occur on a statement within a multi-statement
6774** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6775** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6776** transaction might be rolled back automatically. The only way to
6777** find out whether SQLite automatically rolled back the transaction after
6778** an error is to use this function.
6779**
6780** If another thread changes the autocommit status of the database
6781** connection while this routine is running, then the return value
6782** is undefined.
6783*/
6784SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6785
6786/*
6787** CAPI3REF: Find The Database Handle Of A Prepared Statement
6788** METHOD: sqlite3_stmt
6789**
6790** ^The sqlite3_db_handle interface returns the [database connection] handle
6791** to which a [prepared statement] belongs. ^The [database connection]
6792** returned by sqlite3_db_handle is the same [database connection]
6793** that was the first argument
6794** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6795** create the statement in the first place.
6796*/
6797SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6798
6799/*
6800** CAPI3REF: Return The Schema Name For A Database Connection
6801** METHOD: sqlite3
6802**
6803** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6804** for the N-th database on database connection D, or a NULL pointer of N is
6805** out of range. An N value of 0 means the main database file. An N of 1 is
6806** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6807** databases.
6808**
6809** Space to hold the string that is returned by sqlite3_db_name() is managed
6810** by SQLite itself. The string might be deallocated by any operation that
6811** changes the schema, including [ATTACH] or [DETACH] or calls to
6812** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6813** occur on a different thread. Applications that need to
6814** remember the string long-term should make their own copy. Applications that
6815** are accessing the same database connection simultaneously on multiple
6816** threads should mutex-protect calls to this API and should make their own
6817** private copy of the result prior to releasing the mutex.
6818*/
6819SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6820
6821/*
6822** CAPI3REF: Return The Filename For A Database Connection
6823** METHOD: sqlite3
6824**
6825** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6826** associated with database N of connection D.
6827** ^If there is no attached database N on the database
6828** connection D, or if database N is a temporary or in-memory database, then
6829** this function will return either a NULL pointer or an empty string.
6830**
6831** ^The string value returned by this routine is owned and managed by
6832** the database connection. ^The value will be valid until the database N
6833** is [DETACH]-ed or until the database connection closes.
6834**
6835** ^The filename returned by this function is the output of the
6836** xFullPathname method of the [VFS]. ^In other words, the filename
6837** will be an absolute pathname, even if the filename used
6838** to open the database originally was a URI or relative pathname.
6839**
6840** If the filename pointer returned by this routine is not NULL, then it
6841** can be used as the filename input parameter to these routines:
6842** <ul>
6843** <li> [sqlite3_uri_parameter()]
6844** <li> [sqlite3_uri_boolean()]
6845** <li> [sqlite3_uri_int64()]
6846** <li> [sqlite3_filename_database()]
6847** <li> [sqlite3_filename_journal()]
6848** <li> [sqlite3_filename_wal()]
6849** </ul>
6850*/
6851SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6852
6853/*
6854** CAPI3REF: Determine if a database is read-only
6855** METHOD: sqlite3
6856**
6857** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6858** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6859** the name of a database on connection D.
6860*/
6861SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6862
6863/*
6864** CAPI3REF: Determine the transaction state of a database
6865** METHOD: sqlite3
6866**
6867** ^The sqlite3_txn_state(D,S) interface returns the current
6868** [transaction state] of schema S in database connection D. ^If S is NULL,
6869** then the highest transaction state of any schema on database connection D
6870** is returned. Transaction states are (in order of lowest to highest):
6871** <ol>
6872** <li value="0"> SQLITE_TXN_NONE
6873** <li value="1"> SQLITE_TXN_READ
6874** <li value="2"> SQLITE_TXN_WRITE
6875** </ol>
6876** ^If the S argument to sqlite3_txn_state(D,S) is not the name of
6877** a valid schema, then -1 is returned.
6878*/
6879SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
6880
6881/*
6882** CAPI3REF: Allowed return values from [sqlite3_txn_state()]
6883** KEYWORDS: {transaction state}
6884**
6885** These constants define the current transaction state of a database file.
6886** ^The [sqlite3_txn_state(D,S)] interface returns one of these
6887** constants in order to describe the transaction state of schema S
6888** in [database connection] D.
6889**
6890** <dl>
6891** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt>
6892** <dd>The SQLITE_TXN_NONE state means that no transaction is currently
6893** pending.</dd>
6894**
6895** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt>
6896** <dd>The SQLITE_TXN_READ state means that the database is currently
6897** in a read transaction. Content has been read from the database file
6898** but nothing in the database file has changed. The transaction state
6899** will advanced to SQLITE_TXN_WRITE if any changes occur and there are
6900** no other conflicting concurrent write transactions. The transaction
6901** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or
6902** [COMMIT].</dd>
6903**
6904** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt>
6905** <dd>The SQLITE_TXN_WRITE state means that the database is currently
6906** in a write transaction. Content has been written to the database file
6907** but has not yet committed. The transaction state will change to
6908** to SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd>
6909*/
6910#define SQLITE_TXN_NONE 0
6911#define SQLITE_TXN_READ 1
6912#define SQLITE_TXN_WRITE 2
6913
6914/*
6915** CAPI3REF: Find the next prepared statement
6916** METHOD: sqlite3
6917**
6918** ^This interface returns a pointer to the next [prepared statement] after
6919** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
6920** then this interface returns a pointer to the first prepared statement
6921** associated with the database connection pDb. ^If no prepared statement
6922** satisfies the conditions of this routine, it returns NULL.
6923**
6924** The [database connection] pointer D in a call to
6925** [sqlite3_next_stmt(D,S)] must refer to an open database
6926** connection and in particular must not be a NULL pointer.
6927*/
6928SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6929
6930/*
6931** CAPI3REF: Commit And Rollback Notification Callbacks
6932** METHOD: sqlite3
6933**
6934** ^The sqlite3_commit_hook() interface registers a callback
6935** function to be invoked whenever a transaction is [COMMIT | committed].
6936** ^Any callback set by a previous call to sqlite3_commit_hook()
6937** for the same database connection is overridden.
6938** ^The sqlite3_rollback_hook() interface registers a callback
6939** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6940** ^Any callback set by a previous call to sqlite3_rollback_hook()
6941** for the same database connection is overridden.
6942** ^The pArg argument is passed through to the callback.
6943** ^If the callback on a commit hook function returns non-zero,
6944** then the commit is converted into a rollback.
6945**
6946** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6947** return the P argument from the previous call of the same function
6948** on the same [database connection] D, or NULL for
6949** the first call for each function on D.
6950**
6951** The commit and rollback hook callbacks are not reentrant.
6952** The callback implementation must not do anything that will modify
6953** the database connection that invoked the callback. Any actions
6954** to modify the database connection must be deferred until after the
6955** completion of the [sqlite3_step()] call that triggered the commit
6956** or rollback hook in the first place.
6957** Note that running any other SQL statements, including SELECT statements,
6958** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6959** the database connections for the meaning of "modify" in this paragraph.
6960**
6961** ^Registering a NULL function disables the callback.
6962**
6963** ^When the commit hook callback routine returns zero, the [COMMIT]
6964** operation is allowed to continue normally. ^If the commit hook
6965** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6966** ^The rollback hook is invoked on a rollback that results from a commit
6967** hook returning non-zero, just as it would be with any other rollback.
6968**
6969** ^For the purposes of this API, a transaction is said to have been
6970** rolled back if an explicit "ROLLBACK" statement is executed, or
6971** an error or constraint causes an implicit rollback to occur.
6972** ^The rollback callback is not invoked if a transaction is
6973** automatically rolled back because the database connection is closed.
6974**
6975** See also the [sqlite3_update_hook()] interface.
6976*/
6977SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6978SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6979
6980/*
6981** CAPI3REF: Autovacuum Compaction Amount Callback
6982** METHOD: sqlite3
6983**
6984** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6985** function C that is invoked prior to each autovacuum of the database
6986** file. ^The callback is passed a copy of the generic data pointer (P),
6987** the schema-name of the attached database that is being autovacuumed,
6988** the size of the database file in pages, the number of free pages,
6989** and the number of bytes per page, respectively. The callback should
6990** return the number of free pages that should be removed by the
6991** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6992** ^If the value returned is greater than or equal to the number of
6993** free pages, then a complete autovacuum happens.
6994**
6995** <p>^If there are multiple ATTACH-ed database files that are being
6996** modified as part of a transaction commit, then the autovacuum pages
6997** callback is invoked separately for each file.
6998**
6999** <p><b>The callback is not reentrant.</b> The callback function should
7000** not attempt to invoke any other SQLite interface. If it does, bad
7001** things may happen, including segmentation faults and corrupt database
7002** files. The callback function should be a simple function that
7003** does some arithmetic on its input parameters and returns a result.
7004**
7005** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional
7006** destructor for the P parameter. ^If X is not NULL, then X(P) is
7007** invoked whenever the database connection closes or when the callback
7008** is overwritten by another invocation of sqlite3_autovacuum_pages().
7009**
7010** <p>^There is only one autovacuum pages callback per database connection.
7011** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
7012** previous invocations for that database connection. ^If the callback
7013** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
7014** then the autovacuum steps callback is cancelled. The return value
7015** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
7016** be some other error code if something goes wrong. The current
7017** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
7018** return codes might be added in future releases.
7019**
7020** <p>If no autovacuum pages callback is specified (the usual case) or
7021** a NULL pointer is provided for the callback,
7022** then the default behavior is to vacuum all free pages. So, in other
7023** words, the default behavior is the same as if the callback function
7024** were something like this:
7025**
7026** <blockquote><pre>
7027** &nbsp; unsigned int demonstration_autovac_pages_callback(
7028** &nbsp; void *pClientData,
7029** &nbsp; const char *zSchema,
7030** &nbsp; unsigned int nDbPage,
7031** &nbsp; unsigned int nFreePage,
7032** &nbsp; unsigned int nBytePerPage
7033** &nbsp; ){
7034** &nbsp; return nFreePage;
7035** &nbsp; }
7036** </pre></blockquote>
7037*/
7038SQLITE_API int sqlite3_autovacuum_pages(
7039 sqlite3 *db,
7040 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
7041 void*,
7042 void(*)(void*)
7043);
7044
7045
7046/*
7047** CAPI3REF: Data Change Notification Callbacks
7048** METHOD: sqlite3
7049**
7050** ^The sqlite3_update_hook() interface registers a callback function
7051** with the [database connection] identified by the first argument
7052** to be invoked whenever a row is updated, inserted or deleted in
7053** a [rowid table].
7054** ^Any callback set by a previous call to this function
7055** for the same database connection is overridden.
7056**
7057** ^The second argument is a pointer to the function to invoke when a
7058** row is updated, inserted or deleted in a rowid table.
7059** ^The first argument to the callback is a copy of the third argument
7060** to sqlite3_update_hook().
7061** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
7062** or [SQLITE_UPDATE], depending on the operation that caused the callback
7063** to be invoked.
7064** ^The third and fourth arguments to the callback contain pointers to the
7065** database and table name containing the affected row.
7066** ^The final callback parameter is the [rowid] of the row.
7067** ^In the case of an update, this is the [rowid] after the update takes place.
7068**
7069** ^(The update hook is not invoked when internal system tables are
7070** modified (i.e. sqlite_sequence).)^
7071** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
7072**
7073** ^In the current implementation, the update hook
7074** is not invoked when conflicting rows are deleted because of an
7075** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
7076** invoked when rows are deleted using the [truncate optimization].
7077** The exceptions defined in this paragraph might change in a future
7078** release of SQLite.
7079**
7080** The update hook implementation must not do anything that will modify
7081** the database connection that invoked the update hook. Any actions
7082** to modify the database connection must be deferred until after the
7083** completion of the [sqlite3_step()] call that triggered the update hook.
7084** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
7085** database connections for the meaning of "modify" in this paragraph.
7086**
7087** ^The sqlite3_update_hook(D,C,P) function
7088** returns the P argument from the previous call
7089** on the same [database connection] D, or NULL for
7090** the first call on D.
7091**
7092** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
7093** and [sqlite3_preupdate_hook()] interfaces.
7094*/
7095SQLITE_API void *sqlite3_update_hook(
7096 sqlite3*,
7097 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7098 void*
7099);
7100
7101/*
7102** CAPI3REF: Enable Or Disable Shared Pager Cache
7103**
7104** ^(This routine enables or disables the sharing of the database cache
7105** and schema data structures between [database connection | connections]
7106** to the same database. Sharing is enabled if the argument is true
7107** and disabled if the argument is false.)^
7108**
7109** This interface is omitted if SQLite is compiled with
7110** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7111** compile-time option is recommended because the
7112** [use of shared cache mode is discouraged].
7113**
7114** ^Cache sharing is enabled and disabled for an entire process.
7115** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
7116** In prior versions of SQLite,
7117** sharing was enabled or disabled for each thread separately.
7118**
7119** ^(The cache sharing mode set by this interface effects all subsequent
7120** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
7121** Existing database connections continue to use the sharing mode
7122** that was in effect at the time they were opened.)^
7123**
7124** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
7125** successfully. An [error code] is returned otherwise.)^
7126**
7127** ^Shared cache is disabled by default. It is recommended that it stay
7128** that way. In other words, do not use this routine. This interface
7129** continues to be provided for historical compatibility, but its use is
7130** discouraged. Any use of shared cache is discouraged. If shared cache
7131** must be used, it is recommended that shared cache only be enabled for
7132** individual database connections using the [sqlite3_open_v2()] interface
7133** with the [SQLITE_OPEN_SHAREDCACHE] flag.
7134**
7135** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
7136** and will always return SQLITE_MISUSE. On those systems,
7137** shared cache mode should be enabled per-database connection via
7138** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
7139**
7140** This interface is threadsafe on processors where writing a
7141** 32-bit integer is atomic.
7142**
7143** See Also: [SQLite Shared-Cache Mode]
7144*/
7145SQLITE_API int sqlite3_enable_shared_cache(int);
7146
7147/*
7148** CAPI3REF: Attempt To Free Heap Memory
7149**
7150** ^The sqlite3_release_memory() interface attempts to free N bytes
7151** of heap memory by deallocating non-essential memory allocations
7152** held by the database library. Memory used to cache database
7153** pages to improve performance is an example of non-essential memory.
7154** ^sqlite3_release_memory() returns the number of bytes actually freed,
7155** which might be more or less than the amount requested.
7156** ^The sqlite3_release_memory() routine is a no-op returning zero
7157** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7158**
7159** See also: [sqlite3_db_release_memory()]
7160*/
7161SQLITE_API int sqlite3_release_memory(int);
7162
7163/*
7164** CAPI3REF: Free Memory Used By A Database Connection
7165** METHOD: sqlite3
7166**
7167** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7168** memory as possible from database connection D. Unlike the
7169** [sqlite3_release_memory()] interface, this interface is in effect even
7170** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7171** omitted.
7172**
7173** See also: [sqlite3_release_memory()]
7174*/
7175SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7176
7177/*
7178** CAPI3REF: Impose A Limit On Heap Size
7179**
7180** These interfaces impose limits on the amount of heap memory that will be
7181** by all database connections within a single process.
7182**
7183** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7184** soft limit on the amount of heap memory that may be allocated by SQLite.
7185** ^SQLite strives to keep heap memory utilization below the soft heap
7186** limit by reducing the number of pages held in the page cache
7187** as heap memory usages approaches the limit.
7188** ^The soft heap limit is "soft" because even though SQLite strives to stay
7189** below the limit, it will exceed the limit rather than generate
7190** an [SQLITE_NOMEM] error. In other words, the soft heap limit
7191** is advisory only.
7192**
7193** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
7194** N bytes on the amount of memory that will be allocated. ^The
7195** sqlite3_hard_heap_limit64(N) interface is similar to
7196** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
7197** when the hard heap limit is reached.
7198**
7199** ^The return value from both sqlite3_soft_heap_limit64() and
7200** sqlite3_hard_heap_limit64() is the size of
7201** the heap limit prior to the call, or negative in the case of an
7202** error. ^If the argument N is negative
7203** then no change is made to the heap limit. Hence, the current
7204** size of heap limits can be determined by invoking
7205** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7206**
7207** ^Setting the heap limits to zero disables the heap limiter mechanism.
7208**
7209** ^The soft heap limit may not be greater than the hard heap limit.
7210** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
7211** is invoked with a value of N that is greater than the hard heap limit,
7212** the soft heap limit is set to the value of the hard heap limit.
7213** ^The soft heap limit is automatically enabled whenever the hard heap
7214** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
7215** the soft heap limit is outside the range of 1..N, then the soft heap
7216** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
7217** hard heap limit is enabled makes the soft heap limit equal to the
7218** hard heap limit.
7219**
7220** The memory allocation limits can also be adjusted using
7221** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
7222**
7223** ^(The heap limits are not enforced in the current implementation
7224** if one or more of following conditions are true:
7225**
7226** <ul>
7227** <li> The limit value is set to zero.
7228** <li> Memory accounting is disabled using a combination of the
7229** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7230** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7231** <li> An alternative page cache implementation is specified using
7232** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7233** <li> The page cache allocates from its own memory pool supplied
7234** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7235** from the heap.
7236** </ul>)^
7237**
7238** The circumstances under which SQLite will enforce the heap limits may
7239** changes in future releases of SQLite.
7240*/
7241SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7242SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
7243
7244/*
7245** CAPI3REF: Deprecated Soft Heap Limit Interface
7246** DEPRECATED
7247**
7248** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7249** interface. This routine is provided for historical compatibility
7250** only. All new applications should use the
7251** [sqlite3_soft_heap_limit64()] interface rather than this one.
7252*/
7253SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7254
7255
7256/*
7257** CAPI3REF: Extract Metadata About A Column Of A Table
7258** METHOD: sqlite3
7259**
7260** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7261** information about column C of table T in database D
7262** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7263** interface returns SQLITE_OK and fills in the non-NULL pointers in
7264** the final five arguments with appropriate values if the specified
7265** column exists. ^The sqlite3_table_column_metadata() interface returns
7266** SQLITE_ERROR if the specified column does not exist.
7267** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7268** NULL pointer, then this routine simply checks for the existence of the
7269** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7270** does not. If the table name parameter T in a call to
7271** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7272** undefined behavior.
7273**
7274** ^The column is identified by the second, third and fourth parameters to
7275** this function. ^(The second parameter is either the name of the database
7276** (i.e. "main", "temp", or an attached database) containing the specified
7277** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7278** for the table using the same algorithm used by the database engine to
7279** resolve unqualified table references.
7280**
7281** ^The third and fourth parameters to this function are the table and column
7282** name of the desired column, respectively.
7283**
7284** ^Metadata is returned by writing to the memory locations passed as the 5th
7285** and subsequent parameters to this function. ^Any of these arguments may be
7286** NULL, in which case the corresponding element of metadata is omitted.
7287**
7288** ^(<blockquote>
7289** <table border="1">
7290** <tr><th> Parameter <th> Output<br>Type <th> Description
7291**
7292** <tr><td> 5th <td> const char* <td> Data type
7293** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7294** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7295** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7296** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7297** </table>
7298** </blockquote>)^
7299**
7300** ^The memory pointed to by the character pointers returned for the
7301** declaration type and collation sequence is valid until the next
7302** call to any SQLite API function.
7303**
7304** ^If the specified table is actually a view, an [error code] is returned.
7305**
7306** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7307** is not a [WITHOUT ROWID] table and an
7308** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7309** parameters are set for the explicitly declared column. ^(If there is no
7310** [INTEGER PRIMARY KEY] column, then the outputs
7311** for the [rowid] are set as follows:
7312**
7313** <pre>
7314** data type: "INTEGER"
7315** collation sequence: "BINARY"
7316** not null: 0
7317** primary key: 1
7318** auto increment: 0
7319** </pre>)^
7320**
7321** ^This function causes all database schemas to be read from disk and
7322** parsed, if that has not already been done, and returns an error if
7323** any errors are encountered while loading the schema.
7324*/
7325SQLITE_API int sqlite3_table_column_metadata(
7326 sqlite3 *db, /* Connection handle */
7327 const char *zDbName, /* Database name or NULL */
7328 const char *zTableName, /* Table name */
7329 const char *zColumnName, /* Column name */
7330 char const **pzDataType, /* OUTPUT: Declared data type */
7331 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7332 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7333 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7334 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7335);
7336
7337/*
7338** CAPI3REF: Load An Extension
7339** METHOD: sqlite3
7340**
7341** ^This interface loads an SQLite extension library from the named file.
7342**
7343** ^The sqlite3_load_extension() interface attempts to load an
7344** [SQLite extension] library contained in the file zFile. If
7345** the file cannot be loaded directly, attempts are made to load
7346** with various operating-system specific extensions added.
7347** So for example, if "samplelib" cannot be loaded, then names like
7348** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7349** be tried also.
7350**
7351** ^The entry point is zProc.
7352** ^(zProc may be 0, in which case SQLite will try to come up with an
7353** entry point name on its own. It first tries "sqlite3_extension_init".
7354** If that does not work, it constructs a name "sqlite3_X_init" where the
7355** X is consists of the lower-case equivalent of all ASCII alphabetic
7356** characters in the filename from the last "/" to the first following
7357** "." and omitting any initial "lib".)^
7358** ^The sqlite3_load_extension() interface returns
7359** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7360** ^If an error occurs and pzErrMsg is not 0, then the
7361** [sqlite3_load_extension()] interface shall attempt to
7362** fill *pzErrMsg with error message text stored in memory
7363** obtained from [sqlite3_malloc()]. The calling function
7364** should free this memory by calling [sqlite3_free()].
7365**
7366** ^Extension loading must be enabled using
7367** [sqlite3_enable_load_extension()] or
7368** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7369** prior to calling this API,
7370** otherwise an error will be returned.
7371**
7372** <b>Security warning:</b> It is recommended that the
7373** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7374** interface. The use of the [sqlite3_enable_load_extension()] interface
7375** should be avoided. This will keep the SQL function [load_extension()]
7376** disabled and prevent SQL injections from giving attackers
7377** access to extension loading capabilities.
7378**
7379** See also the [load_extension() SQL function].
7380*/
7381SQLITE_API int sqlite3_load_extension(
7382 sqlite3 *db, /* Load the extension into this database connection */
7383 const char *zFile, /* Name of the shared library containing extension */
7384 const char *zProc, /* Entry point. Derived from zFile if 0 */
7385 char **pzErrMsg /* Put error message here if not 0 */
7386);
7387
7388/*
7389** CAPI3REF: Enable Or Disable Extension Loading
7390** METHOD: sqlite3
7391**
7392** ^So as not to open security holes in older applications that are
7393** unprepared to deal with [extension loading], and as a means of disabling
7394** [extension loading] while evaluating user-entered SQL, the following API
7395** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7396**
7397** ^Extension loading is off by default.
7398** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7399** to turn extension loading on and call it with onoff==0 to turn
7400** it back off again.
7401**
7402** ^This interface enables or disables both the C-API
7403** [sqlite3_load_extension()] and the SQL function [load_extension()].
7404** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7405** to enable or disable only the C-API.)^
7406**
7407** <b>Security warning:</b> It is recommended that extension loading
7408** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7409** rather than this interface, so the [load_extension()] SQL function
7410** remains disabled. This will prevent SQL injections from giving attackers
7411** access to extension loading capabilities.
7412*/
7413SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7414
7415/*
7416** CAPI3REF: Automatically Load Statically Linked Extensions
7417**
7418** ^This interface causes the xEntryPoint() function to be invoked for
7419** each new [database connection] that is created. The idea here is that
7420** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7421** that is to be automatically loaded into all new database connections.
7422**
7423** ^(Even though the function prototype shows that xEntryPoint() takes
7424** no arguments and returns void, SQLite invokes xEntryPoint() with three
7425** arguments and expects an integer result as if the signature of the
7426** entry point where as follows:
7427**
7428** <blockquote><pre>
7429** &nbsp; int xEntryPoint(
7430** &nbsp; sqlite3 *db,
7431** &nbsp; const char **pzErrMsg,
7432** &nbsp; const struct sqlite3_api_routines *pThunk
7433** &nbsp; );
7434** </pre></blockquote>)^
7435**
7436** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7437** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7438** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7439** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7440** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7441** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7442** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7443**
7444** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7445** on the list of automatic extensions is a harmless no-op. ^No entry point
7446** will be called more than once for each database connection that is opened.
7447**
7448** See also: [sqlite3_reset_auto_extension()]
7449** and [sqlite3_cancel_auto_extension()]
7450*/
7451SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7452
7453/*
7454** CAPI3REF: Cancel Automatic Extension Loading
7455**
7456** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7457** initialization routine X that was registered using a prior call to
7458** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7459** routine returns 1 if initialization routine X was successfully
7460** unregistered and it returns 0 if X was not on the list of initialization
7461** routines.
7462*/
7463SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7464
7465/*
7466** CAPI3REF: Reset Automatic Extension Loading
7467**
7468** ^This interface disables all automatic extensions previously
7469** registered using [sqlite3_auto_extension()].
7470*/
7471SQLITE_API void sqlite3_reset_auto_extension(void);
7472
7473/*
7474** Structures used by the virtual table interface
7475*/
7476typedef struct sqlite3_vtab sqlite3_vtab;
7477typedef struct sqlite3_index_info sqlite3_index_info;
7478typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7479typedef struct sqlite3_module sqlite3_module;
7480
7481/*
7482** CAPI3REF: Virtual Table Object
7483** KEYWORDS: sqlite3_module {virtual table module}
7484**
7485** This structure, sometimes called a "virtual table module",
7486** defines the implementation of a [virtual table].
7487** This structure consists mostly of methods for the module.
7488**
7489** ^A virtual table module is created by filling in a persistent
7490** instance of this structure and passing a pointer to that instance
7491** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7492** ^The registration remains valid until it is replaced by a different
7493** module or until the [database connection] closes. The content
7494** of this structure must not change while it is registered with
7495** any database connection.
7496*/
7497struct sqlite3_module {
7498 int iVersion;
7499 int (*xCreate)(sqlite3*, void *pAux,
7500 int argc, const char *const*argv,
7501 sqlite3_vtab **ppVTab, char**);
7502 int (*xConnect)(sqlite3*, void *pAux,
7503 int argc, const char *const*argv,
7504 sqlite3_vtab **ppVTab, char**);
7505 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7506 int (*xDisconnect)(sqlite3_vtab *pVTab);
7507 int (*xDestroy)(sqlite3_vtab *pVTab);
7508 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7509 int (*xClose)(sqlite3_vtab_cursor*);
7510 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7511 int argc, sqlite3_value **argv);
7512 int (*xNext)(sqlite3_vtab_cursor*);
7513 int (*xEof)(sqlite3_vtab_cursor*);
7514 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7515 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7516 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7517 int (*xBegin)(sqlite3_vtab *pVTab);
7518 int (*xSync)(sqlite3_vtab *pVTab);
7519 int (*xCommit)(sqlite3_vtab *pVTab);
7520 int (*xRollback)(sqlite3_vtab *pVTab);
7521 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7522 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7523 void **ppArg);
7524 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7525 /* The methods above are in version 1 of the sqlite_module object. Those
7526 ** below are for version 2 and greater. */
7527 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7528 int (*xRelease)(sqlite3_vtab *pVTab, int);
7529 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7530 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7531 ** Those below are for version 3 and greater. */
7532 int (*xShadowName)(const char*);
7533};
7534
7535/*
7536** CAPI3REF: Virtual Table Indexing Information
7537** KEYWORDS: sqlite3_index_info
7538**
7539** The sqlite3_index_info structure and its substructures is used as part
7540** of the [virtual table] interface to
7541** pass information into and receive the reply from the [xBestIndex]
7542** method of a [virtual table module]. The fields under **Inputs** are the
7543** inputs to xBestIndex and are read-only. xBestIndex inserts its
7544** results into the **Outputs** fields.
7545**
7546** ^(The aConstraint[] array records WHERE clause constraints of the form:
7547**
7548** <blockquote>column OP expr</blockquote>
7549**
7550** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7551** stored in aConstraint[].op using one of the
7552** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7553** ^(The index of the column is stored in
7554** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7555** expr on the right-hand side can be evaluated (and thus the constraint
7556** is usable) and false if it cannot.)^
7557**
7558** ^The optimizer automatically inverts terms of the form "expr OP column"
7559** and makes other simplifications to the WHERE clause in an attempt to
7560** get as many WHERE clause terms into the form shown above as possible.
7561** ^The aConstraint[] array only reports WHERE clause terms that are
7562** relevant to the particular virtual table being queried.
7563**
7564** ^Information about the ORDER BY clause is stored in aOrderBy[].
7565** ^Each term of aOrderBy records a column of the ORDER BY clause.
7566**
7567** The colUsed field indicates which columns of the virtual table may be
7568** required by the current scan. Virtual table columns are numbered from
7569** zero in the order in which they appear within the CREATE TABLE statement
7570** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7571** the corresponding bit is set within the colUsed mask if the column may be
7572** required by SQLite. If the table has at least 64 columns and any column
7573** to the right of the first 63 is required, then bit 63 of colUsed is also
7574** set. In other words, column iCol may be required if the expression
7575** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7576** non-zero.
7577**
7578** The [xBestIndex] method must fill aConstraintUsage[] with information
7579** about what parameters to pass to xFilter. ^If argvIndex>0 then
7580** the right-hand side of the corresponding aConstraint[] is evaluated
7581** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7582** is true, then the constraint is assumed to be fully handled by the
7583** virtual table and might not be checked again by the byte code.)^ ^(The
7584** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7585** is left in its default setting of false, the constraint will always be
7586** checked separately in byte code. If the omit flag is change to true, then
7587** the constraint may or may not be checked in byte code. In other words,
7588** when the omit flag is true there is no guarantee that the constraint will
7589** not be checked again using byte code.)^
7590**
7591** ^The idxNum and idxStr values are recorded and passed into the
7592** [xFilter] method.
7593** ^[sqlite3_free()] is used to free idxStr if and only if
7594** needToFreeIdxStr is true.
7595**
7596** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7597** the correct order to satisfy the ORDER BY clause so that no separate
7598** sorting step is required.
7599**
7600** ^The estimatedCost value is an estimate of the cost of a particular
7601** strategy. A cost of N indicates that the cost of the strategy is similar
7602** to a linear scan of an SQLite table with N rows. A cost of log(N)
7603** indicates that the expense of the operation is similar to that of a
7604** binary search on a unique indexed field of an SQLite table with N rows.
7605**
7606** ^The estimatedRows value is an estimate of the number of rows that
7607** will be returned by the strategy.
7608**
7609** The xBestIndex method may optionally populate the idxFlags field with a
7610** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7611** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7612** assumes that the strategy may visit at most one row.
7613**
7614** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7615** SQLite also assumes that if a call to the xUpdate() method is made as
7616** part of the same statement to delete or update a virtual table row and the
7617** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7618** any database changes. In other words, if the xUpdate() returns
7619** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7620** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7621** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7622** the xUpdate method are automatically rolled back by SQLite.
7623**
7624** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7625** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7626** If a virtual table extension is
7627** used with an SQLite version earlier than 3.8.2, the results of attempting
7628** to read or write the estimatedRows field are undefined (but are likely
7629** to include crashing the application). The estimatedRows field should
7630** therefore only be used if [sqlite3_libversion_number()] returns a
7631** value greater than or equal to 3008002. Similarly, the idxFlags field
7632** was added for [version 3.9.0] ([dateof:3.9.0]).
7633** It may therefore only be used if
7634** sqlite3_libversion_number() returns a value greater than or equal to
7635** 3009000.
7636*/
7637struct sqlite3_index_info {
7638 /* Inputs */
7639 int nConstraint; /* Number of entries in aConstraint */
7640 struct sqlite3_index_constraint {
7641 int iColumn; /* Column constrained. -1 for ROWID */
7642 unsigned char op; /* Constraint operator */
7643 unsigned char usable; /* True if this constraint is usable */
7644 int iTermOffset; /* Used internally - xBestIndex should ignore */
7645 } *aConstraint; /* Table of WHERE clause constraints */
7646 int nOrderBy; /* Number of terms in the ORDER BY clause */
7647 struct sqlite3_index_orderby {
7648 int iColumn; /* Column number */
7649 unsigned char desc; /* True for DESC. False for ASC. */
7650 } *aOrderBy; /* The ORDER BY clause */
7651 /* Outputs */
7652 struct sqlite3_index_constraint_usage {
7653 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7654 unsigned char omit; /* Do not code a test for this constraint */
7655 } *aConstraintUsage;
7656 int idxNum; /* Number used to identify the index */
7657 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7658 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7659 int orderByConsumed; /* True if output is already ordered */
7660 double estimatedCost; /* Estimated cost of using this index */
7661 /* Fields below are only available in SQLite 3.8.2 and later */
7662 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7663 /* Fields below are only available in SQLite 3.9.0 and later */
7664 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7665 /* Fields below are only available in SQLite 3.10.0 and later */
7666 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7667};
7668
7669/*
7670** CAPI3REF: Virtual Table Scan Flags
7671**
7672** Virtual table implementations are allowed to set the
7673** [sqlite3_index_info].idxFlags field to some combination of
7674** these bits.
7675*/
7676#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7677
7678/*
7679** CAPI3REF: Virtual Table Constraint Operator Codes
7680**
7681** These macros define the allowed values for the
7682** [sqlite3_index_info].aConstraint[].op field. Each value represents
7683** an operator that is part of a constraint term in the WHERE clause of
7684** a query that uses a [virtual table].
7685**
7686** ^The left-hand operand of the operator is given by the corresponding
7687** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7688** operand is the rowid.
7689** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7690** operators have no left-hand operand, and so for those operators the
7691** corresponding aConstraint[].iColumn is meaningless and should not be
7692** used.
7693**
7694** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7695** value 255 are reserved to represent functions that are overloaded
7696** by the [xFindFunction|xFindFunction method] of the virtual table
7697** implementation.
7698**
7699** The right-hand operands for each constraint might be accessible using
7700** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7701** operand is only available if it appears as a single constant literal
7702** in the input SQL. If the right-hand operand is another column or an
7703** expression (even a constant expression) or a parameter, then the
7704** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7705** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7706** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7707** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7708** always return SQLITE_NOTFOUND.
7709**
7710** The collating sequence to be used for comparison can be found using
7711** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7712** tables, the collating sequence of constraints does not matter (for example
7713** because the constraints are numeric) and so the sqlite3_vtab_collation()
7714** interface is not commonly needed.
7715*/
7716#define SQLITE_INDEX_CONSTRAINT_EQ 2
7717#define SQLITE_INDEX_CONSTRAINT_GT 4
7718#define SQLITE_INDEX_CONSTRAINT_LE 8
7719#define SQLITE_INDEX_CONSTRAINT_LT 16
7720#define SQLITE_INDEX_CONSTRAINT_GE 32
7721#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7722#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7723#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7724#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7725#define SQLITE_INDEX_CONSTRAINT_NE 68
7726#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7727#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7728#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7729#define SQLITE_INDEX_CONSTRAINT_IS 72
7730#define SQLITE_INDEX_CONSTRAINT_LIMIT 73
7731#define SQLITE_INDEX_CONSTRAINT_OFFSET 74
7732#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7733
7734/*
7735** CAPI3REF: Register A Virtual Table Implementation
7736** METHOD: sqlite3
7737**
7738** ^These routines are used to register a new [virtual table module] name.
7739** ^Module names must be registered before
7740** creating a new [virtual table] using the module and before using a
7741** preexisting [virtual table] for the module.
7742**
7743** ^The module name is registered on the [database connection] specified
7744** by the first parameter. ^The name of the module is given by the
7745** second parameter. ^The third parameter is a pointer to
7746** the implementation of the [virtual table module]. ^The fourth
7747** parameter is an arbitrary client data pointer that is passed through
7748** into the [xCreate] and [xConnect] methods of the virtual table module
7749** when a new virtual table is be being created or reinitialized.
7750**
7751** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7752** is a pointer to a destructor for the pClientData. ^SQLite will
7753** invoke the destructor function (if it is not NULL) when SQLite
7754** no longer needs the pClientData pointer. ^The destructor will also
7755** be invoked if the call to sqlite3_create_module_v2() fails.
7756** ^The sqlite3_create_module()
7757** interface is equivalent to sqlite3_create_module_v2() with a NULL
7758** destructor.
7759**
7760** ^If the third parameter (the pointer to the sqlite3_module object) is
7761** NULL then no new module is created and any existing modules with the
7762** same name are dropped.
7763**
7764** See also: [sqlite3_drop_modules()]
7765*/
7766SQLITE_API int sqlite3_create_module(
7767 sqlite3 *db, /* SQLite connection to register module with */
7768 const char *zName, /* Name of the module */
7769 const sqlite3_module *p, /* Methods for the module */
7770 void *pClientData /* Client data for xCreate/xConnect */
7771);
7772SQLITE_API int sqlite3_create_module_v2(
7773 sqlite3 *db, /* SQLite connection to register module with */
7774 const char *zName, /* Name of the module */
7775 const sqlite3_module *p, /* Methods for the module */
7776 void *pClientData, /* Client data for xCreate/xConnect */
7777 void(*xDestroy)(void*) /* Module destructor function */
7778);
7779
7780/*
7781** CAPI3REF: Remove Unnecessary Virtual Table Implementations
7782** METHOD: sqlite3
7783**
7784** ^The sqlite3_drop_modules(D,L) interface removes all virtual
7785** table modules from database connection D except those named on list L.
7786** The L parameter must be either NULL or a pointer to an array of pointers
7787** to strings where the array is terminated by a single NULL pointer.
7788** ^If the L parameter is NULL, then all virtual table modules are removed.
7789**
7790** See also: [sqlite3_create_module()]
7791*/
7792SQLITE_API int sqlite3_drop_modules(
7793 sqlite3 *db, /* Remove modules from this connection */
7794 const char **azKeep /* Except, do not remove the ones named here */
7795);
7796
7797/*
7798** CAPI3REF: Virtual Table Instance Object
7799** KEYWORDS: sqlite3_vtab
7800**
7801** Every [virtual table module] implementation uses a subclass
7802** of this object to describe a particular instance
7803** of the [virtual table]. Each subclass will
7804** be tailored to the specific needs of the module implementation.
7805** The purpose of this superclass is to define certain fields that are
7806** common to all module implementations.
7807**
7808** ^Virtual tables methods can set an error message by assigning a
7809** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7810** take care that any prior string is freed by a call to [sqlite3_free()]
7811** prior to assigning a new string to zErrMsg. ^After the error message
7812** is delivered up to the client application, the string will be automatically
7813** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7814*/
7815struct sqlite3_vtab {
7816 const sqlite3_module *pModule; /* The module for this virtual table */
7817 int nRef; /* Number of open cursors */
7818 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7819 /* Virtual table implementations will typically add additional fields */
7820};
7821
7822/*
7823** CAPI3REF: Virtual Table Cursor Object
7824** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7825**
7826** Every [virtual table module] implementation uses a subclass of the
7827** following structure to describe cursors that point into the
7828** [virtual table] and are used
7829** to loop through the virtual table. Cursors are created using the
7830** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7831** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7832** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7833** of the module. Each module implementation will define
7834** the content of a cursor structure to suit its own needs.
7835**
7836** This superclass exists in order to define fields of the cursor that
7837** are common to all implementations.
7838*/
7839struct sqlite3_vtab_cursor {
7840 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
7841 /* Virtual table implementations will typically add additional fields */
7842};
7843
7844/*
7845** CAPI3REF: Declare The Schema Of A Virtual Table
7846**
7847** ^The [xCreate] and [xConnect] methods of a
7848** [virtual table module] call this interface
7849** to declare the format (the names and datatypes of the columns) of
7850** the virtual tables they implement.
7851*/
7852SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7853
7854/*
7855** CAPI3REF: Overload A Function For A Virtual Table
7856** METHOD: sqlite3
7857**
7858** ^(Virtual tables can provide alternative implementations of functions
7859** using the [xFindFunction] method of the [virtual table module].
7860** But global versions of those functions
7861** must exist in order to be overloaded.)^
7862**
7863** ^(This API makes sure a global version of a function with a particular
7864** name and number of parameters exists. If no such function exists
7865** before this API is called, a new function is created.)^ ^The implementation
7866** of the new function always causes an exception to be thrown. So
7867** the new function is not good for anything by itself. Its only
7868** purpose is to be a placeholder function that can be overloaded
7869** by a [virtual table].
7870*/
7871SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7872
7873/*
7874** CAPI3REF: A Handle To An Open BLOB
7875** KEYWORDS: {BLOB handle} {BLOB handles}
7876**
7877** An instance of this object represents an open BLOB on which
7878** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7879** ^Objects of this type are created by [sqlite3_blob_open()]
7880** and destroyed by [sqlite3_blob_close()].
7881** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7882** can be used to read or write small subsections of the BLOB.
7883** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7884*/
7885typedef struct sqlite3_blob sqlite3_blob;
7886
7887/*
7888** CAPI3REF: Open A BLOB For Incremental I/O
7889** METHOD: sqlite3
7890** CONSTRUCTOR: sqlite3_blob
7891**
7892** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7893** in row iRow, column zColumn, table zTable in database zDb;
7894** in other words, the same BLOB that would be selected by:
7895**
7896** <pre>
7897** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7898** </pre>)^
7899**
7900** ^(Parameter zDb is not the filename that contains the database, but
7901** rather the symbolic name of the database. For attached databases, this is
7902** the name that appears after the AS keyword in the [ATTACH] statement.
7903** For the main database file, the database name is "main". For TEMP
7904** tables, the database name is "temp".)^
7905**
7906** ^If the flags parameter is non-zero, then the BLOB is opened for read
7907** and write access. ^If the flags parameter is zero, the BLOB is opened for
7908** read-only access.
7909**
7910** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7911** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7912** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7913** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7914** on *ppBlob after this function it returns.
7915**
7916** This function fails with SQLITE_ERROR if any of the following are true:
7917** <ul>
7918** <li> ^(Database zDb does not exist)^,
7919** <li> ^(Table zTable does not exist within database zDb)^,
7920** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7921** <li> ^(Column zColumn does not exist)^,
7922** <li> ^(Row iRow is not present in the table)^,
7923** <li> ^(The specified column of row iRow contains a value that is not
7924** a TEXT or BLOB value)^,
7925** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7926** constraint and the blob is being opened for read/write access)^,
7927** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7928** column zColumn is part of a [child key] definition and the blob is
7929** being opened for read/write access)^.
7930** </ul>
7931**
7932** ^Unless it returns SQLITE_MISUSE, this function sets the
7933** [database connection] error code and message accessible via
7934** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7935**
7936** A BLOB referenced by sqlite3_blob_open() may be read using the
7937** [sqlite3_blob_read()] interface and modified by using
7938** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7939** different row of the same table using the [sqlite3_blob_reopen()]
7940** interface. However, the column, table, or database of a [BLOB handle]
7941** cannot be changed after the [BLOB handle] is opened.
7942**
7943** ^(If the row that a BLOB handle points to is modified by an
7944** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7945** then the BLOB handle is marked as "expired".
7946** This is true if any column of the row is changed, even a column
7947** other than the one the BLOB handle is open on.)^
7948** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7949** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7950** ^(Changes written into a BLOB prior to the BLOB expiring are not
7951** rolled back by the expiration of the BLOB. Such changes will eventually
7952** commit if the transaction continues to completion.)^
7953**
7954** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7955** the opened blob. ^The size of a blob may not be changed by this
7956** interface. Use the [UPDATE] SQL command to change the size of a
7957** blob.
7958**
7959** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7960** and the built-in [zeroblob] SQL function may be used to create a
7961** zero-filled blob to read or write using the incremental-blob interface.
7962**
7963** To avoid a resource leak, every open [BLOB handle] should eventually
7964** be released by a call to [sqlite3_blob_close()].
7965**
7966** See also: [sqlite3_blob_close()],
7967** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7968** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7969*/
7970SQLITE_API int sqlite3_blob_open(
7971 sqlite3*,
7972 const char *zDb,
7973 const char *zTable,
7974 const char *zColumn,
7975 sqlite3_int64 iRow,
7976 int flags,
7977 sqlite3_blob **ppBlob
7978);
7979
7980/*
7981** CAPI3REF: Move a BLOB Handle to a New Row
7982** METHOD: sqlite3_blob
7983**
7984** ^This function is used to move an existing [BLOB handle] so that it points
7985** to a different row of the same database table. ^The new row is identified
7986** by the rowid value passed as the second argument. Only the row can be
7987** changed. ^The database, table and column on which the blob handle is open
7988** remain the same. Moving an existing [BLOB handle] to a new row is
7989** faster than closing the existing handle and opening a new one.
7990**
7991** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7992** it must exist and there must be either a blob or text value stored in
7993** the nominated column.)^ ^If the new row is not present in the table, or if
7994** it does not contain a blob or text value, or if another error occurs, an
7995** SQLite error code is returned and the blob handle is considered aborted.
7996** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7997** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7998** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7999** always returns zero.
8000**
8001** ^This function sets the database handle error code and message.
8002*/
8003SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8004
8005/*
8006** CAPI3REF: Close A BLOB Handle
8007** DESTRUCTOR: sqlite3_blob
8008**
8009** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
8010** unconditionally. Even if this routine returns an error code, the
8011** handle is still closed.)^
8012**
8013** ^If the blob handle being closed was opened for read-write access, and if
8014** the database is in auto-commit mode and there are no other open read-write
8015** blob handles or active write statements, the current transaction is
8016** committed. ^If an error occurs while committing the transaction, an error
8017** code is returned and the transaction rolled back.
8018**
8019** Calling this function with an argument that is not a NULL pointer or an
8020** open blob handle results in undefined behaviour. ^Calling this routine
8021** with a null pointer (such as would be returned by a failed call to
8022** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8023** is passed a valid open blob handle, the values returned by the
8024** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
8025*/
8026SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
8027
8028/*
8029** CAPI3REF: Return The Size Of An Open BLOB
8030** METHOD: sqlite3_blob
8031**
8032** ^Returns the size in bytes of the BLOB accessible via the
8033** successfully opened [BLOB handle] in its only argument. ^The
8034** incremental blob I/O routines can only read or overwriting existing
8035** blob content; they cannot change the size of a blob.
8036**
8037** This routine only works on a [BLOB handle] which has been created
8038** by a prior successful call to [sqlite3_blob_open()] and which has not
8039** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8040** to this routine results in undefined and probably undesirable behavior.
8041*/
8042SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
8043
8044/*
8045** CAPI3REF: Read Data From A BLOB Incrementally
8046** METHOD: sqlite3_blob
8047**
8048** ^(This function is used to read data from an open [BLOB handle] into a
8049** caller-supplied buffer. N bytes of data are copied into buffer Z
8050** from the open BLOB, starting at offset iOffset.)^
8051**
8052** ^If offset iOffset is less than N bytes from the end of the BLOB,
8053** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
8054** less than zero, [SQLITE_ERROR] is returned and no data is read.
8055** ^The size of the blob (and hence the maximum value of N+iOffset)
8056** can be determined using the [sqlite3_blob_bytes()] interface.
8057**
8058** ^An attempt to read from an expired [BLOB handle] fails with an
8059** error code of [SQLITE_ABORT].
8060**
8061** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
8062** Otherwise, an [error code] or an [extended error code] is returned.)^
8063**
8064** This routine only works on a [BLOB handle] which has been created
8065** by a prior successful call to [sqlite3_blob_open()] and which has not
8066** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8067** to this routine results in undefined and probably undesirable behavior.
8068**
8069** See also: [sqlite3_blob_write()].
8070*/
8071SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
8072
8073/*
8074** CAPI3REF: Write Data Into A BLOB Incrementally
8075** METHOD: sqlite3_blob
8076**
8077** ^(This function is used to write data into an open [BLOB handle] from a
8078** caller-supplied buffer. N bytes of data are copied from the buffer Z
8079** into the open BLOB, starting at offset iOffset.)^
8080**
8081** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
8082** Otherwise, an [error code] or an [extended error code] is returned.)^
8083** ^Unless SQLITE_MISUSE is returned, this function sets the
8084** [database connection] error code and message accessible via
8085** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8086**
8087** ^If the [BLOB handle] passed as the first argument was not opened for
8088** writing (the flags parameter to [sqlite3_blob_open()] was zero),
8089** this function returns [SQLITE_READONLY].
8090**
8091** This function may only modify the contents of the BLOB; it is
8092** not possible to increase the size of a BLOB using this API.
8093** ^If offset iOffset is less than N bytes from the end of the BLOB,
8094** [SQLITE_ERROR] is returned and no data is written. The size of the
8095** BLOB (and hence the maximum value of N+iOffset) can be determined
8096** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
8097** than zero [SQLITE_ERROR] is returned and no data is written.
8098**
8099** ^An attempt to write to an expired [BLOB handle] fails with an
8100** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
8101** before the [BLOB handle] expired are not rolled back by the
8102** expiration of the handle, though of course those changes might
8103** have been overwritten by the statement that expired the BLOB handle
8104** or by other independent statements.
8105**
8106** This routine only works on a [BLOB handle] which has been created
8107** by a prior successful call to [sqlite3_blob_open()] and which has not
8108** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8109** to this routine results in undefined and probably undesirable behavior.
8110**
8111** See also: [sqlite3_blob_read()].
8112*/
8113SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8114
8115/*
8116** CAPI3REF: Virtual File System Objects
8117**
8118** A virtual filesystem (VFS) is an [sqlite3_vfs] object
8119** that SQLite uses to interact
8120** with the underlying operating system. Most SQLite builds come with a
8121** single default VFS that is appropriate for the host computer.
8122** New VFSes can be registered and existing VFSes can be unregistered.
8123** The following interfaces are provided.
8124**
8125** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
8126** ^Names are case sensitive.
8127** ^Names are zero-terminated UTF-8 strings.
8128** ^If there is no match, a NULL pointer is returned.
8129** ^If zVfsName is NULL then the default VFS is returned.
8130**
8131** ^New VFSes are registered with sqlite3_vfs_register().
8132** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
8133** ^The same VFS can be registered multiple times without injury.
8134** ^To make an existing VFS into the default VFS, register it again
8135** with the makeDflt flag set. If two different VFSes with the
8136** same name are registered, the behavior is undefined. If a
8137** VFS is registered with a name that is NULL or an empty string,
8138** then the behavior is undefined.
8139**
8140** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
8141** ^(If the default VFS is unregistered, another VFS is chosen as
8142** the default. The choice for the new VFS is arbitrary.)^
8143*/
8144SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
8145SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8146SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8147
8148/*
8149** CAPI3REF: Mutexes
8150**
8151** The SQLite core uses these routines for thread
8152** synchronization. Though they are intended for internal
8153** use by SQLite, code that links against SQLite is
8154** permitted to use any of these routines.
8155**
8156** The SQLite source code contains multiple implementations
8157** of these mutex routines. An appropriate implementation
8158** is selected automatically at compile-time. The following
8159** implementations are available in the SQLite core:
8160**
8161** <ul>
8162** <li> SQLITE_MUTEX_PTHREADS
8163** <li> SQLITE_MUTEX_W32
8164** <li> SQLITE_MUTEX_NOOP
8165** </ul>
8166**
8167** The SQLITE_MUTEX_NOOP implementation is a set of routines
8168** that does no real locking and is appropriate for use in
8169** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8170** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
8171** and Windows.
8172**
8173** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
8174** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8175** implementation is included with the library. In this case the
8176** application must supply a custom mutex implementation using the
8177** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
8178** before calling sqlite3_initialize() or any other public sqlite3_
8179** function that calls sqlite3_initialize().
8180**
8181** ^The sqlite3_mutex_alloc() routine allocates a new
8182** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
8183** routine returns NULL if it is unable to allocate the requested
8184** mutex. The argument to sqlite3_mutex_alloc() must one of these
8185** integer constants:
8186**
8187** <ul>
8188** <li> SQLITE_MUTEX_FAST
8189** <li> SQLITE_MUTEX_RECURSIVE
8190** <li> SQLITE_MUTEX_STATIC_MAIN
8191** <li> SQLITE_MUTEX_STATIC_MEM
8192** <li> SQLITE_MUTEX_STATIC_OPEN
8193** <li> SQLITE_MUTEX_STATIC_PRNG
8194** <li> SQLITE_MUTEX_STATIC_LRU
8195** <li> SQLITE_MUTEX_STATIC_PMEM
8196** <li> SQLITE_MUTEX_STATIC_APP1
8197** <li> SQLITE_MUTEX_STATIC_APP2
8198** <li> SQLITE_MUTEX_STATIC_APP3
8199** <li> SQLITE_MUTEX_STATIC_VFS1
8200** <li> SQLITE_MUTEX_STATIC_VFS2
8201** <li> SQLITE_MUTEX_STATIC_VFS3
8202** </ul>
8203**
8204** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8205** cause sqlite3_mutex_alloc() to create
8206** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8207** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8208** The mutex implementation does not need to make a distinction
8209** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8210** not want to. SQLite will only request a recursive mutex in
8211** cases where it really needs one. If a faster non-recursive mutex
8212** implementation is available on the host platform, the mutex subsystem
8213** might return such a mutex in response to SQLITE_MUTEX_FAST.
8214**
8215** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8216** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8217** a pointer to a static preexisting mutex. ^Nine static mutexes are
8218** used by the current version of SQLite. Future versions of SQLite
8219** may add additional static mutexes. Static mutexes are for internal
8220** use by SQLite only. Applications that use SQLite mutexes should
8221** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8222** SQLITE_MUTEX_RECURSIVE.
8223**
8224** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8225** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8226** returns a different mutex on every call. ^For the static
8227** mutex types, the same mutex is returned on every call that has
8228** the same type number.
8229**
8230** ^The sqlite3_mutex_free() routine deallocates a previously
8231** allocated dynamic mutex. Attempting to deallocate a static
8232** mutex results in undefined behavior.
8233**
8234** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8235** to enter a mutex. ^If another thread is already within the mutex,
8236** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8237** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8238** upon successful entry. ^(Mutexes created using
8239** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8240** In such cases, the
8241** mutex must be exited an equal number of times before another thread
8242** can enter.)^ If the same thread tries to enter any mutex other
8243** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8244**
8245** ^(Some systems (for example, Windows 95) do not support the operation
8246** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8247** will always return SQLITE_BUSY. The SQLite core only ever uses
8248** sqlite3_mutex_try() as an optimization so this is acceptable
8249** behavior.)^
8250**
8251** ^The sqlite3_mutex_leave() routine exits a mutex that was
8252** previously entered by the same thread. The behavior
8253** is undefined if the mutex is not currently entered by the
8254** calling thread or is not currently allocated.
8255**
8256** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(),
8257** sqlite3_mutex_leave(), or sqlite3_mutex_free() is a NULL pointer,
8258** then any of the four routines behaves as a no-op.
8259**
8260** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8261*/
8262SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8263SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8264SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8265SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8266SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8267
8268/*
8269** CAPI3REF: Mutex Methods Object
8270**
8271** An instance of this structure defines the low-level routines
8272** used to allocate and use mutexes.
8273**
8274** Usually, the default mutex implementations provided by SQLite are
8275** sufficient, however the application has the option of substituting a custom
8276** implementation for specialized deployments or systems for which SQLite
8277** does not provide a suitable implementation. In this case, the application
8278** creates and populates an instance of this structure to pass
8279** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8280** Additionally, an instance of this structure can be used as an
8281** output variable when querying the system for the current mutex
8282** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8283**
8284** ^The xMutexInit method defined by this structure is invoked as
8285** part of system initialization by the sqlite3_initialize() function.
8286** ^The xMutexInit routine is called by SQLite exactly once for each
8287** effective call to [sqlite3_initialize()].
8288**
8289** ^The xMutexEnd method defined by this structure is invoked as
8290** part of system shutdown by the sqlite3_shutdown() function. The
8291** implementation of this method is expected to release all outstanding
8292** resources obtained by the mutex methods implementation, especially
8293** those obtained by the xMutexInit method. ^The xMutexEnd()
8294** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8295**
8296** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8297** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8298** xMutexNotheld) implement the following interfaces (respectively):
8299**
8300** <ul>
8301** <li> [sqlite3_mutex_alloc()] </li>
8302** <li> [sqlite3_mutex_free()] </li>
8303** <li> [sqlite3_mutex_enter()] </li>
8304** <li> [sqlite3_mutex_try()] </li>
8305** <li> [sqlite3_mutex_leave()] </li>
8306** <li> [sqlite3_mutex_held()] </li>
8307** <li> [sqlite3_mutex_notheld()] </li>
8308** </ul>)^
8309**
8310** The only difference is that the public sqlite3_XXX functions enumerated
8311** above silently ignore any invocations that pass a NULL pointer instead
8312** of a valid mutex handle. The implementations of the methods defined
8313** by this structure are not required to handle this case. The results
8314** of passing a NULL pointer instead of a valid mutex handle are undefined
8315** (i.e. it is acceptable to provide an implementation that segfaults if
8316** it is passed a NULL pointer).
8317**
8318** The xMutexInit() method must be threadsafe. It must be harmless to
8319** invoke xMutexInit() multiple times within the same process and without
8320** intervening calls to xMutexEnd(). Second and subsequent calls to
8321** xMutexInit() must be no-ops.
8322**
8323** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8324** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8325** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8326** memory allocation for a fast or recursive mutex.
8327**
8328** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8329** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8330** If xMutexInit fails in any way, it is expected to clean up after itself
8331** prior to returning.
8332*/
8333typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8334struct sqlite3_mutex_methods {
8335 int (*xMutexInit)(void);
8336 int (*xMutexEnd)(void);
8337 sqlite3_mutex *(*xMutexAlloc)(int);
8338 void (*xMutexFree)(sqlite3_mutex *);
8339 void (*xMutexEnter)(sqlite3_mutex *);
8340 int (*xMutexTry)(sqlite3_mutex *);
8341 void (*xMutexLeave)(sqlite3_mutex *);
8342 int (*xMutexHeld)(sqlite3_mutex *);
8343 int (*xMutexNotheld)(sqlite3_mutex *);
8344};
8345
8346/*
8347** CAPI3REF: Mutex Verification Routines
8348**
8349** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8350** are intended for use inside assert() statements. The SQLite core
8351** never uses these routines except inside an assert() and applications
8352** are advised to follow the lead of the core. The SQLite core only
8353** provides implementations for these routines when it is compiled
8354** with the SQLITE_DEBUG flag. External mutex implementations
8355** are only required to provide these routines if SQLITE_DEBUG is
8356** defined and if NDEBUG is not defined.
8357**
8358** These routines should return true if the mutex in their argument
8359** is held or not held, respectively, by the calling thread.
8360**
8361** The implementation is not required to provide versions of these
8362** routines that actually work. If the implementation does not provide working
8363** versions of these routines, it should at least provide stubs that always
8364** return true so that one does not get spurious assertion failures.
8365**
8366** If the argument to sqlite3_mutex_held() is a NULL pointer then
8367** the routine should return 1. This seems counter-intuitive since
8368** clearly the mutex cannot be held if it does not exist. But
8369** the reason the mutex does not exist is because the build is not
8370** using mutexes. And we do not want the assert() containing the
8371** call to sqlite3_mutex_held() to fail, so a non-zero return is
8372** the appropriate thing to do. The sqlite3_mutex_notheld()
8373** interface should also return 1 when given a NULL pointer.
8374*/
8375#ifndef NDEBUG
8376SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8377SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8378#endif
8379
8380/*
8381** CAPI3REF: Mutex Types
8382**
8383** The [sqlite3_mutex_alloc()] interface takes a single argument
8384** which is one of these integer constants.
8385**
8386** The set of static mutexes may change from one SQLite release to the
8387** next. Applications that override the built-in mutex logic must be
8388** prepared to accommodate additional static mutexes.
8389*/
8390#define SQLITE_MUTEX_FAST 0
8391#define SQLITE_MUTEX_RECURSIVE 1
8392#define SQLITE_MUTEX_STATIC_MAIN 2
8393#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
8394#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
8395#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
8396#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */
8397#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
8398#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
8399#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
8400#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
8401#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
8402#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
8403#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8404#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */
8405#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */
8406
8407/* Legacy compatibility: */
8408#define SQLITE_MUTEX_STATIC_MASTER 2
8409
8410
8411/*
8412** CAPI3REF: Retrieve the mutex for a database connection
8413** METHOD: sqlite3
8414**
8415** ^This interface returns a pointer the [sqlite3_mutex] object that
8416** serializes access to the [database connection] given in the argument
8417** when the [threading mode] is Serialized.
8418** ^If the [threading mode] is Single-thread or Multi-thread then this
8419** routine returns a NULL pointer.
8420*/
8421SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8422
8423/*
8424** CAPI3REF: Low-Level Control Of Database Files
8425** METHOD: sqlite3
8426** KEYWORDS: {file control}
8427**
8428** ^The [sqlite3_file_control()] interface makes a direct call to the
8429** xFileControl method for the [sqlite3_io_methods] object associated
8430** with a particular database identified by the second argument. ^The
8431** name of the database is "main" for the main database or "temp" for the
8432** TEMP database, or the name that appears after the AS keyword for
8433** databases that are added using the [ATTACH] SQL command.
8434** ^A NULL pointer can be used in place of "main" to refer to the
8435** main database file.
8436** ^The third and fourth parameters to this routine
8437** are passed directly through to the second and third parameters of
8438** the xFileControl method. ^The return value of the xFileControl
8439** method becomes the return value of this routine.
8440**
8441** A few opcodes for [sqlite3_file_control()] are handled directly
8442** by the SQLite core and never invoke the
8443** sqlite3_io_methods.xFileControl method.
8444** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8445** a pointer to the underlying [sqlite3_file] object to be written into
8446** the space pointed to by the 4th parameter. The
8447** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8448** the [sqlite3_file] object associated with the journal file instead of
8449** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8450** a pointer to the underlying [sqlite3_vfs] object for the file.
8451** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8452** from the pager.
8453**
8454** ^If the second parameter (zDbName) does not match the name of any
8455** open database file, then SQLITE_ERROR is returned. ^This error
8456** code is not remembered and will not be recalled by [sqlite3_errcode()]
8457** or [sqlite3_errmsg()]. The underlying xFileControl method might
8458** also return SQLITE_ERROR. There is no way to distinguish between
8459** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8460** xFileControl method.
8461**
8462** See also: [file control opcodes]
8463*/
8464SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8465
8466/*
8467** CAPI3REF: Testing Interface
8468**
8469** ^The sqlite3_test_control() interface is used to read out internal
8470** state of SQLite and to inject faults into SQLite for testing
8471** purposes. ^The first parameter is an operation code that determines
8472** the number, meaning, and operation of all subsequent parameters.
8473**
8474** This interface is not for use by applications. It exists solely
8475** for verifying the correct operation of the SQLite library. Depending
8476** on how the SQLite library is compiled, this interface might not exist.
8477**
8478** The details of the operation codes, their meanings, the parameters
8479** they take, and what they do are all subject to change without notice.
8480** Unlike most of the SQLite API, this function is not guaranteed to
8481** operate consistently from one release to the next.
8482*/
8483SQLITE_API int sqlite3_test_control(int op, ...);
8484
8485/*
8486** CAPI3REF: Testing Interface Operation Codes
8487**
8488** These constants are the valid operation code parameters used
8489** as the first argument to [sqlite3_test_control()].
8490**
8491** These parameters and their meanings are subject to change
8492** without notice. These values are for testing purposes only.
8493** Applications should not use any of these parameters or the
8494** [sqlite3_test_control()] interface.
8495*/
8496#define SQLITE_TESTCTRL_FIRST 5
8497#define SQLITE_TESTCTRL_PRNG_SAVE 5
8498#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8499#define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */
8500#define SQLITE_TESTCTRL_BITVEC_TEST 8
8501#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8502#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
8503#define SQLITE_TESTCTRL_PENDING_BYTE 11
8504#define SQLITE_TESTCTRL_ASSERT 12
8505#define SQLITE_TESTCTRL_ALWAYS 13
8506#define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */
8507#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8508#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8509#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
8510#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17
8511#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
8512#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
8513#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19
8514#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
8515#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
8516#define SQLITE_TESTCTRL_BYTEORDER 22
8517#define SQLITE_TESTCTRL_ISINIT 23
8518#define SQLITE_TESTCTRL_SORTER_MMAP 24
8519#define SQLITE_TESTCTRL_IMPOSTER 25
8520#define SQLITE_TESTCTRL_PARSER_COVERAGE 26
8521#define SQLITE_TESTCTRL_RESULT_INTREAL 27
8522#define SQLITE_TESTCTRL_PRNG_SEED 28
8523#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8524#define SQLITE_TESTCTRL_SEEK_COUNT 30
8525#define SQLITE_TESTCTRL_TRACEFLAGS 31
8526#define SQLITE_TESTCTRL_TUNE 32
8527#define SQLITE_TESTCTRL_LOGEST 33
8528#define SQLITE_TESTCTRL_USELONGDOUBLE 34
8529#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */
8530
8531/*
8532** CAPI3REF: SQL Keyword Checking
8533**
8534** These routines provide access to the set of SQL language keywords
8535** recognized by SQLite. Applications can uses these routines to determine
8536** whether or not a specific identifier needs to be escaped (for example,
8537** by enclosing in double-quotes) so as not to confuse the parser.
8538**
8539** The sqlite3_keyword_count() interface returns the number of distinct
8540** keywords understood by SQLite.
8541**
8542** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8543** makes *Z point to that keyword expressed as UTF8 and writes the number
8544** of bytes in the keyword into *L. The string that *Z points to is not
8545** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8546** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8547** or L are NULL or invalid pointers then calls to
8548** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8549**
8550** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8551** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8552** if it is and zero if not.
8553**
8554** The parser used by SQLite is forgiving. It is often possible to use
8555** a keyword as an identifier as long as such use does not result in a
8556** parsing ambiguity. For example, the statement
8557** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8558** creates a new table named "BEGIN" with three columns named
8559** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8560** using keywords as identifiers. Common techniques used to avoid keyword
8561** name collisions include:
8562** <ul>
8563** <li> Put all identifier names inside double-quotes. This is the official
8564** SQL way to escape identifier names.
8565** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8566** but it is what SQL Server does and so lots of programmers use this
8567** technique.
8568** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8569** with "Z".
8570** <li> Include a digit somewhere in every identifier name.
8571** </ul>
8572**
8573** Note that the number of keywords understood by SQLite can depend on
8574** compile-time options. For example, "VACUUM" is not a keyword if
8575** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8576** new keywords may be added to future releases of SQLite.
8577*/
8578SQLITE_API int sqlite3_keyword_count(void);
8579SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8580SQLITE_API int sqlite3_keyword_check(const char*,int);
8581
8582/*
8583** CAPI3REF: Dynamic String Object
8584** KEYWORDS: {dynamic string}
8585**
8586** An instance of the sqlite3_str object contains a dynamically-sized
8587** string under construction.
8588**
8589** The lifecycle of an sqlite3_str object is as follows:
8590** <ol>
8591** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8592** <li> ^Text is appended to the sqlite3_str object using various
8593** methods, such as [sqlite3_str_appendf()].
8594** <li> ^The sqlite3_str object is destroyed and the string it created
8595** is returned using the [sqlite3_str_finish()] interface.
8596** </ol>
8597*/
8598typedef struct sqlite3_str sqlite3_str;
8599
8600/*
8601** CAPI3REF: Create A New Dynamic String Object
8602** CONSTRUCTOR: sqlite3_str
8603**
8604** ^The [sqlite3_str_new(D)] interface allocates and initializes
8605** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8606** [sqlite3_str_new()] must be freed by a subsequent call to
8607** [sqlite3_str_finish(X)].
8608**
8609** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8610** valid [sqlite3_str] object, though in the event of an out-of-memory
8611** error the returned object might be a special singleton that will
8612** silently reject new text, always return SQLITE_NOMEM from
8613** [sqlite3_str_errcode()], always return 0 for
8614** [sqlite3_str_length()], and always return NULL from
8615** [sqlite3_str_finish(X)]. It is always safe to use the value
8616** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8617** to any of the other [sqlite3_str] methods.
8618**
8619** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8620** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8621** length of the string contained in the [sqlite3_str] object will be
8622** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8623** of [SQLITE_MAX_LENGTH].
8624*/
8625SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8626
8627/*
8628** CAPI3REF: Finalize A Dynamic String
8629** DESTRUCTOR: sqlite3_str
8630**
8631** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8632** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8633** that contains the constructed string. The calling application should
8634** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8635** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8636** errors were encountered during construction of the string. ^The
8637** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8638** string in [sqlite3_str] object X is zero bytes long.
8639*/
8640SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8641
8642/*
8643** CAPI3REF: Add Content To A Dynamic String
8644** METHOD: sqlite3_str
8645**
8646** These interfaces add content to an sqlite3_str object previously obtained
8647** from [sqlite3_str_new()].
8648**
8649** ^The [sqlite3_str_appendf(X,F,...)] and
8650** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8651** functionality of SQLite to append formatted text onto the end of
8652** [sqlite3_str] object X.
8653**
8654** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8655** onto the end of the [sqlite3_str] object X. N must be non-negative.
8656** S must contain at least N non-zero bytes of content. To append a
8657** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8658** method instead.
8659**
8660** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8661** zero-terminated string S onto the end of [sqlite3_str] object X.
8662**
8663** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8664** single-byte character C onto the end of [sqlite3_str] object X.
8665** ^This method can be used, for example, to add whitespace indentation.
8666**
8667** ^The [sqlite3_str_reset(X)] method resets the string under construction
8668** inside [sqlite3_str] object X back to zero bytes in length.
8669**
8670** These methods do not return a result code. ^If an error occurs, that fact
8671** is recorded in the [sqlite3_str] object and can be recovered by a
8672** subsequent call to [sqlite3_str_errcode(X)].
8673*/
8674SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8675SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8676SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8677SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8678SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8679SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8680
8681/*
8682** CAPI3REF: Status Of A Dynamic String
8683** METHOD: sqlite3_str
8684**
8685** These interfaces return the current status of an [sqlite3_str] object.
8686**
8687** ^If any prior errors have occurred while constructing the dynamic string
8688** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8689** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8690** [SQLITE_NOMEM] following any out-of-memory error, or
8691** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8692** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8693**
8694** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8695** of the dynamic string under construction in [sqlite3_str] object X.
8696** ^The length returned by [sqlite3_str_length(X)] does not include the
8697** zero-termination byte.
8698**
8699** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8700** content of the dynamic string under construction in X. The value
8701** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8702** and might be freed or altered by any subsequent method on the same
8703** [sqlite3_str] object. Applications must not used the pointer returned
8704** [sqlite3_str_value(X)] after any subsequent method call on the same
8705** object. ^Applications may change the content of the string returned
8706** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8707** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8708** write any byte after any subsequent sqlite3_str method call.
8709*/
8710SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8711SQLITE_API int sqlite3_str_length(sqlite3_str*);
8712SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8713
8714/*
8715** CAPI3REF: SQLite Runtime Status
8716**
8717** ^These interfaces are used to retrieve runtime status information
8718** about the performance of SQLite, and optionally to reset various
8719** highwater marks. ^The first argument is an integer code for
8720** the specific parameter to measure. ^(Recognized integer codes
8721** are of the form [status parameters | SQLITE_STATUS_...].)^
8722** ^The current value of the parameter is returned into *pCurrent.
8723** ^The highest recorded value is returned in *pHighwater. ^If the
8724** resetFlag is true, then the highest record value is reset after
8725** *pHighwater is written. ^(Some parameters do not record the highest
8726** value. For those parameters
8727** nothing is written into *pHighwater and the resetFlag is ignored.)^
8728** ^(Other parameters record only the highwater mark and not the current
8729** value. For these latter parameters nothing is written into *pCurrent.)^
8730**
8731** ^The sqlite3_status() and sqlite3_status64() routines return
8732** SQLITE_OK on success and a non-zero [error code] on failure.
8733**
8734** If either the current value or the highwater mark is too large to
8735** be represented by a 32-bit integer, then the values returned by
8736** sqlite3_status() are undefined.
8737**
8738** See also: [sqlite3_db_status()]
8739*/
8740SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8741SQLITE_API int sqlite3_status64(
8742 int op,
8743 sqlite3_int64 *pCurrent,
8744 sqlite3_int64 *pHighwater,
8745 int resetFlag
8746);
8747
8748
8749/*
8750** CAPI3REF: Status Parameters
8751** KEYWORDS: {status parameters}
8752**
8753** These integer constants designate various run-time status parameters
8754** that can be returned by [sqlite3_status()].
8755**
8756** <dl>
8757** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8758** <dd>This parameter is the current amount of memory checked out
8759** using [sqlite3_malloc()], either directly or indirectly. The
8760** figure includes calls made to [sqlite3_malloc()] by the application
8761** and internal memory usage by the SQLite library. Auxiliary page-cache
8762** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8763** this parameter. The amount returned is the sum of the allocation
8764** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8765**
8766** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8767** <dd>This parameter records the largest memory allocation request
8768** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8769** internal equivalents). Only the value returned in the
8770** *pHighwater parameter to [sqlite3_status()] is of interest.
8771** The value written into the *pCurrent parameter is undefined.</dd>)^
8772**
8773** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8774** <dd>This parameter records the number of separate memory allocations
8775** currently checked out.</dd>)^
8776**
8777** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8778** <dd>This parameter returns the number of pages used out of the
8779** [pagecache memory allocator] that was configured using
8780** [SQLITE_CONFIG_PAGECACHE]. The
8781** value returned is in pages, not in bytes.</dd>)^
8782**
8783** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8784** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8785** <dd>This parameter returns the number of bytes of page cache
8786** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8787** buffer and where forced to overflow to [sqlite3_malloc()]. The
8788** returned value includes allocations that overflowed because they
8789** where too large (they were larger than the "sz" parameter to
8790** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8791** no space was left in the page cache.</dd>)^
8792**
8793** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8794** <dd>This parameter records the largest memory allocation request
8795** handed to the [pagecache memory allocator]. Only the value returned in the
8796** *pHighwater parameter to [sqlite3_status()] is of interest.
8797** The value written into the *pCurrent parameter is undefined.</dd>)^
8798**
8799** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8800** <dd>No longer used.</dd>
8801**
8802** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8803** <dd>No longer used.</dd>
8804**
8805** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8806** <dd>No longer used.</dd>
8807**
8808** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8809** <dd>The *pHighwater parameter records the deepest parser stack.
8810** The *pCurrent value is undefined. The *pHighwater value is only
8811** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8812** </dl>
8813**
8814** New status parameters may be added from time to time.
8815*/
8816#define SQLITE_STATUS_MEMORY_USED 0
8817#define SQLITE_STATUS_PAGECACHE_USED 1
8818#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
8819#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */
8820#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */
8821#define SQLITE_STATUS_MALLOC_SIZE 5
8822#define SQLITE_STATUS_PARSER_STACK 6
8823#define SQLITE_STATUS_PAGECACHE_SIZE 7
8824#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */
8825#define SQLITE_STATUS_MALLOC_COUNT 9
8826
8827/*
8828** CAPI3REF: Database Connection Status
8829** METHOD: sqlite3
8830**
8831** ^This interface is used to retrieve runtime status information
8832** about a single [database connection]. ^The first argument is the
8833** database connection object to be interrogated. ^The second argument
8834** is an integer constant, taken from the set of
8835** [SQLITE_DBSTATUS options], that
8836** determines the parameter to interrogate. The set of
8837** [SQLITE_DBSTATUS options] is likely
8838** to grow in future releases of SQLite.
8839**
8840** ^The current value of the requested parameter is written into *pCur
8841** and the highest instantaneous value is written into *pHiwtr. ^If
8842** the resetFlg is true, then the highest instantaneous value is
8843** reset back down to the current value.
8844**
8845** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8846** non-zero [error code] on failure.
8847**
8848** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8849*/
8850SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8851
8852/*
8853** CAPI3REF: Status Parameters for database connections
8854** KEYWORDS: {SQLITE_DBSTATUS options}
8855**
8856** These constants are the available integer "verbs" that can be passed as
8857** the second argument to the [sqlite3_db_status()] interface.
8858**
8859** New verbs may be added in future releases of SQLite. Existing verbs
8860** might be discontinued. Applications should check the return code from
8861** [sqlite3_db_status()] to make sure that the call worked.
8862** The [sqlite3_db_status()] interface will return a non-zero error code
8863** if a discontinued or unsupported verb is invoked.
8864**
8865** <dl>
8866** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8867** <dd>This parameter returns the number of lookaside memory slots currently
8868** checked out.</dd>)^
8869**
8870** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8871** <dd>This parameter returns the number of malloc attempts that were
8872** satisfied using lookaside memory. Only the high-water value is meaningful;
8873** the current value is always zero.)^
8874**
8875** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8876** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8877** <dd>This parameter returns the number malloc attempts that might have
8878** been satisfied using lookaside memory but failed due to the amount of
8879** memory requested being larger than the lookaside slot size.
8880** Only the high-water value is meaningful;
8881** the current value is always zero.)^
8882**
8883** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8884** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8885** <dd>This parameter returns the number malloc attempts that might have
8886** been satisfied using lookaside memory but failed due to all lookaside
8887** memory already being in use.
8888** Only the high-water value is meaningful;
8889** the current value is always zero.)^
8890**
8891** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8892** <dd>This parameter returns the approximate number of bytes of heap
8893** memory used by all pager caches associated with the database connection.)^
8894** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8895**
8896** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8897** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8898** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8899** pager cache is shared between two or more connections the bytes of heap
8900** memory used by that pager cache is divided evenly between the attached
8901** connections.)^ In other words, if none of the pager caches associated
8902** with the database connection are shared, this request returns the same
8903** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8904** shared, the value returned by this call will be smaller than that returned
8905** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8906** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8907**
8908** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8909** <dd>This parameter returns the approximate number of bytes of heap
8910** memory used to store the schema for all databases associated
8911** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8912** ^The full amount of memory used by the schemas is reported, even if the
8913** schema memory is shared with other database connections due to
8914** [shared cache mode] being enabled.
8915** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8916**
8917** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8918** <dd>This parameter returns the approximate number of bytes of heap
8919** and lookaside memory used by all prepared statements associated with
8920** the database connection.)^
8921** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8922** </dd>
8923**
8924** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8925** <dd>This parameter returns the number of pager cache hits that have
8926** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8927** is always 0.
8928** </dd>
8929**
8930** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8931** <dd>This parameter returns the number of pager cache misses that have
8932** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8933** is always 0.
8934** </dd>
8935**
8936** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8937** <dd>This parameter returns the number of dirty cache entries that have
8938** been written to disk. Specifically, the number of pages written to the
8939** wal file in wal mode databases, or the number of pages written to the
8940** database file in rollback mode databases. Any pages written as part of
8941** transaction rollback or database recovery operations are not included.
8942** If an IO or other error occurs while writing a page to disk, the effect
8943** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8944** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8945** </dd>
8946**
8947** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8948** <dd>This parameter returns the number of dirty cache entries that have
8949** been written to disk in the middle of a transaction due to the page
8950** cache overflowing. Transactions are more efficient if they are written
8951** to disk all at once. When pages spill mid-transaction, that introduces
8952** additional overhead. This parameter can be used help identify
8953** inefficiencies that can be resolved by increasing the cache size.
8954** </dd>
8955**
8956** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8957** <dd>This parameter returns zero for the current value if and only if
8958** all foreign key constraints (deferred or immediate) have been
8959** resolved.)^ ^The highwater mark is always 0.
8960** </dd>
8961** </dl>
8962*/
8963#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
8964#define SQLITE_DBSTATUS_CACHE_USED 1
8965#define SQLITE_DBSTATUS_SCHEMA_USED 2
8966#define SQLITE_DBSTATUS_STMT_USED 3
8967#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
8968#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
8969#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
8970#define SQLITE_DBSTATUS_CACHE_HIT 7
8971#define SQLITE_DBSTATUS_CACHE_MISS 8
8972#define SQLITE_DBSTATUS_CACHE_WRITE 9
8973#define SQLITE_DBSTATUS_DEFERRED_FKS 10
8974#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8975#define SQLITE_DBSTATUS_CACHE_SPILL 12
8976#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
8977
8978
8979/*
8980** CAPI3REF: Prepared Statement Status
8981** METHOD: sqlite3_stmt
8982**
8983** ^(Each prepared statement maintains various
8984** [SQLITE_STMTSTATUS counters] that measure the number
8985** of times it has performed specific operations.)^ These counters can
8986** be used to monitor the performance characteristics of the prepared
8987** statements. For example, if the number of table steps greatly exceeds
8988** the number of table searches or result rows, that would tend to indicate
8989** that the prepared statement is using a full table scan rather than
8990** an index.
8991**
8992** ^(This interface is used to retrieve and reset counter values from
8993** a [prepared statement]. The first argument is the prepared statement
8994** object to be interrogated. The second argument
8995** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8996** to be interrogated.)^
8997** ^The current value of the requested counter is returned.
8998** ^If the resetFlg is true, then the counter is reset to zero after this
8999** interface call returns.
9000**
9001** See also: [sqlite3_status()] and [sqlite3_db_status()].
9002*/
9003SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
9004
9005/*
9006** CAPI3REF: Status Parameters for prepared statements
9007** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
9008**
9009** These preprocessor macros define integer codes that name counter
9010** values associated with the [sqlite3_stmt_status()] interface.
9011** The meanings of the various counters are as follows:
9012**
9013** <dl>
9014** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
9015** <dd>^This is the number of times that SQLite has stepped forward in
9016** a table as part of a full table scan. Large numbers for this counter
9017** may indicate opportunities for performance improvement through
9018** careful use of indices.</dd>
9019**
9020** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
9021** <dd>^This is the number of sort operations that have occurred.
9022** A non-zero value in this counter may indicate an opportunity to
9023** improvement performance through careful use of indices.</dd>
9024**
9025** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
9026** <dd>^This is the number of rows inserted into transient indices that
9027** were created automatically in order to help joins run faster.
9028** A non-zero value in this counter may indicate an opportunity to
9029** improvement performance by adding permanent indices that do not
9030** need to be reinitialized each time the statement is run.</dd>
9031**
9032** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
9033** <dd>^This is the number of virtual machine operations executed
9034** by the prepared statement if that number is less than or equal
9035** to 2147483647. The number of virtual machine operations can be
9036** used as a proxy for the total work done by the prepared statement.
9037** If the number of virtual machine operations exceeds 2147483647
9038** then the value returned by this statement status code is undefined.
9039**
9040** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
9041** <dd>^This is the number of times that the prepare statement has been
9042** automatically regenerated due to schema changes or changes to
9043** [bound parameters] that might affect the query plan.
9044**
9045** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
9046** <dd>^This is the number of times that the prepared statement has
9047** been run. A single "run" for the purposes of this counter is one
9048** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
9049** The counter is incremented on the first [sqlite3_step()] call of each
9050** cycle.
9051**
9052** [[SQLITE_STMTSTATUS_FILTER_MISS]]
9053** [[SQLITE_STMTSTATUS_FILTER HIT]]
9054** <dt>SQLITE_STMTSTATUS_FILTER_HIT<br>
9055** SQLITE_STMTSTATUS_FILTER_MISS</dt>
9056** <dd>^SQLITE_STMTSTATUS_FILTER_HIT is the number of times that a join
9057** step was bypassed because a Bloom filter returned not-found. The
9058** corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of
9059** times that the Bloom filter returned a find, and thus the join step
9060** had to be processed as normal.
9061**
9062** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
9063** <dd>^This is the approximate number of bytes of heap memory
9064** used to store the prepared statement. ^This value is not actually
9065** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
9066** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
9067** </dd>
9068** </dl>
9069*/
9070#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
9071#define SQLITE_STMTSTATUS_SORT 2
9072#define SQLITE_STMTSTATUS_AUTOINDEX 3
9073#define SQLITE_STMTSTATUS_VM_STEP 4
9074#define SQLITE_STMTSTATUS_REPREPARE 5
9075#define SQLITE_STMTSTATUS_RUN 6
9076#define SQLITE_STMTSTATUS_FILTER_MISS 7
9077#define SQLITE_STMTSTATUS_FILTER_HIT 8
9078#define SQLITE_STMTSTATUS_MEMUSED 99
9079
9080/*
9081** CAPI3REF: Custom Page Cache Object
9082**
9083** The sqlite3_pcache type is opaque. It is implemented by
9084** the pluggable module. The SQLite core has no knowledge of
9085** its size or internal structure and never deals with the
9086** sqlite3_pcache object except by holding and passing pointers
9087** to the object.
9088**
9089** See [sqlite3_pcache_methods2] for additional information.
9090*/
9091typedef struct sqlite3_pcache sqlite3_pcache;
9092
9093/*
9094** CAPI3REF: Custom Page Cache Object
9095**
9096** The sqlite3_pcache_page object represents a single page in the
9097** page cache. The page cache will allocate instances of this
9098** object. Various methods of the page cache use pointers to instances
9099** of this object as parameters or as their return value.
9100**
9101** See [sqlite3_pcache_methods2] for additional information.
9102*/
9103typedef struct sqlite3_pcache_page sqlite3_pcache_page;
9104struct sqlite3_pcache_page {
9105 void *pBuf; /* The content of the page */
9106 void *pExtra; /* Extra information associated with the page */
9107};
9108
9109/*
9110** CAPI3REF: Application Defined Page Cache.
9111** KEYWORDS: {page cache}
9112**
9113** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
9114** register an alternative page cache implementation by passing in an
9115** instance of the sqlite3_pcache_methods2 structure.)^
9116** In many applications, most of the heap memory allocated by
9117** SQLite is used for the page cache.
9118** By implementing a
9119** custom page cache using this API, an application can better control
9120** the amount of memory consumed by SQLite, the way in which
9121** that memory is allocated and released, and the policies used to
9122** determine exactly which parts of a database file are cached and for
9123** how long.
9124**
9125** The alternative page cache mechanism is an
9126** extreme measure that is only needed by the most demanding applications.
9127** The built-in page cache is recommended for most uses.
9128**
9129** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
9130** internal buffer by SQLite within the call to [sqlite3_config]. Hence
9131** the application may discard the parameter after the call to
9132** [sqlite3_config()] returns.)^
9133**
9134** [[the xInit() page cache method]]
9135** ^(The xInit() method is called once for each effective
9136** call to [sqlite3_initialize()])^
9137** (usually only once during the lifetime of the process). ^(The xInit()
9138** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
9139** The intent of the xInit() method is to set up global data structures
9140** required by the custom page cache implementation.
9141** ^(If the xInit() method is NULL, then the
9142** built-in default page cache is used instead of the application defined
9143** page cache.)^
9144**
9145** [[the xShutdown() page cache method]]
9146** ^The xShutdown() method is called by [sqlite3_shutdown()].
9147** It can be used to clean up
9148** any outstanding resources before process shutdown, if required.
9149** ^The xShutdown() method may be NULL.
9150**
9151** ^SQLite automatically serializes calls to the xInit method,
9152** so the xInit method need not be threadsafe. ^The
9153** xShutdown method is only called from [sqlite3_shutdown()] so it does
9154** not need to be threadsafe either. All other methods must be threadsafe
9155** in multithreaded applications.
9156**
9157** ^SQLite will never invoke xInit() more than once without an intervening
9158** call to xShutdown().
9159**
9160** [[the xCreate() page cache methods]]
9161** ^SQLite invokes the xCreate() method to construct a new cache instance.
9162** SQLite will typically create one cache instance for each open database file,
9163** though this is not guaranteed. ^The
9164** first parameter, szPage, is the size in bytes of the pages that must
9165** be allocated by the cache. ^szPage will always a power of two. ^The
9166** second parameter szExtra is a number of bytes of extra storage
9167** associated with each page cache entry. ^The szExtra parameter will
9168** a number less than 250. SQLite will use the
9169** extra szExtra bytes on each page to store metadata about the underlying
9170** database page on disk. The value passed into szExtra depends
9171** on the SQLite version, the target platform, and how SQLite was compiled.
9172** ^The third argument to xCreate(), bPurgeable, is true if the cache being
9173** created will be used to cache database pages of a file stored on disk, or
9174** false if it is used for an in-memory database. The cache implementation
9175** does not have to do anything special based with the value of bPurgeable;
9176** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
9177** never invoke xUnpin() except to deliberately delete a page.
9178** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
9179** false will always have the "discard" flag set to true.
9180** ^Hence, a cache created with bPurgeable false will
9181** never contain any unpinned pages.
9182**
9183** [[the xCachesize() page cache method]]
9184** ^(The xCachesize() method may be called at any time by SQLite to set the
9185** suggested maximum cache-size (number of pages stored by) the cache
9186** instance passed as the first argument. This is the value configured using
9187** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
9188** parameter, the implementation is not required to do anything with this
9189** value; it is advisory only.
9190**
9191** [[the xPagecount() page cache methods]]
9192** The xPagecount() method must return the number of pages currently
9193** stored in the cache, both pinned and unpinned.
9194**
9195** [[the xFetch() page cache methods]]
9196** The xFetch() method locates a page in the cache and returns a pointer to
9197** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9198** The pBuf element of the returned sqlite3_pcache_page object will be a
9199** pointer to a buffer of szPage bytes used to store the content of a
9200** single database page. The pExtra element of sqlite3_pcache_page will be
9201** a pointer to the szExtra bytes of extra storage that SQLite has requested
9202** for each entry in the page cache.
9203**
9204** The page to be fetched is determined by the key. ^The minimum key value
9205** is 1. After it has been retrieved using xFetch, the page is considered
9206** to be "pinned".
9207**
9208** If the requested page is already in the page cache, then the page cache
9209** implementation must return a pointer to the page buffer with its content
9210** intact. If the requested page is not already in the cache, then the
9211** cache implementation should use the value of the createFlag
9212** parameter to help it determined what action to take:
9213**
9214** <table border=1 width=85% align=center>
9215** <tr><th> createFlag <th> Behavior when page is not already in cache
9216** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9217** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9218** Otherwise return NULL.
9219** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9220** NULL if allocating a new page is effectively impossible.
9221** </table>
9222**
9223** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
9224** will only use a createFlag of 2 after a prior call with a createFlag of 1
9225** failed.)^ In between the xFetch() calls, SQLite may
9226** attempt to unpin one or more cache pages by spilling the content of
9227** pinned pages to disk and synching the operating system disk cache.
9228**
9229** [[the xUnpin() page cache method]]
9230** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9231** as its second argument. If the third parameter, discard, is non-zero,
9232** then the page must be evicted from the cache.
9233** ^If the discard parameter is
9234** zero, then the page may be discarded or retained at the discretion of
9235** page cache implementation. ^The page cache implementation
9236** may choose to evict unpinned pages at any time.
9237**
9238** The cache must not perform any reference counting. A single
9239** call to xUnpin() unpins the page regardless of the number of prior calls
9240** to xFetch().
9241**
9242** [[the xRekey() page cache methods]]
9243** The xRekey() method is used to change the key value associated with the
9244** page passed as the second argument. If the cache
9245** previously contains an entry associated with newKey, it must be
9246** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9247** to be pinned.
9248**
9249** When SQLite calls the xTruncate() method, the cache must discard all
9250** existing cache entries with page numbers (keys) greater than or equal
9251** to the value of the iLimit parameter passed to xTruncate(). If any
9252** of these pages are pinned, they are implicitly unpinned, meaning that
9253** they can be safely discarded.
9254**
9255** [[the xDestroy() page cache method]]
9256** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9257** All resources associated with the specified cache should be freed. ^After
9258** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9259** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9260** functions.
9261**
9262** [[the xShrink() page cache method]]
9263** ^SQLite invokes the xShrink() method when it wants the page cache to
9264** free up as much of heap memory as possible. The page cache implementation
9265** is not obligated to free any memory, but well-behaved implementations should
9266** do their best.
9267*/
9268typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9269struct sqlite3_pcache_methods2 {
9270 int iVersion;
9271 void *pArg;
9272 int (*xInit)(void*);
9273 void (*xShutdown)(void*);
9274 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9275 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9276 int (*xPagecount)(sqlite3_pcache*);
9277 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9278 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9279 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9280 unsigned oldKey, unsigned newKey);
9281 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9282 void (*xDestroy)(sqlite3_pcache*);
9283 void (*xShrink)(sqlite3_pcache*);
9284};
9285
9286/*
9287** This is the obsolete pcache_methods object that has now been replaced
9288** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9289** retained in the header file for backwards compatibility only.
9290*/
9291typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9292struct sqlite3_pcache_methods {
9293 void *pArg;
9294 int (*xInit)(void*);
9295 void (*xShutdown)(void*);
9296 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9297 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9298 int (*xPagecount)(sqlite3_pcache*);
9299 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9300 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9301 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9302 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9303 void (*xDestroy)(sqlite3_pcache*);
9304};
9305
9306
9307/*
9308** CAPI3REF: Online Backup Object
9309**
9310** The sqlite3_backup object records state information about an ongoing
9311** online backup operation. ^The sqlite3_backup object is created by
9312** a call to [sqlite3_backup_init()] and is destroyed by a call to
9313** [sqlite3_backup_finish()].
9314**
9315** See Also: [Using the SQLite Online Backup API]
9316*/
9317typedef struct sqlite3_backup sqlite3_backup;
9318
9319/*
9320** CAPI3REF: Online Backup API.
9321**
9322** The backup API copies the content of one database into another.
9323** It is useful either for creating backups of databases or
9324** for copying in-memory databases to or from persistent files.
9325**
9326** See Also: [Using the SQLite Online Backup API]
9327**
9328** ^SQLite holds a write transaction open on the destination database file
9329** for the duration of the backup operation.
9330** ^The source database is read-locked only while it is being read;
9331** it is not locked continuously for the entire backup operation.
9332** ^Thus, the backup may be performed on a live source database without
9333** preventing other database connections from
9334** reading or writing to the source database while the backup is underway.
9335**
9336** ^(To perform a backup operation:
9337** <ol>
9338** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9339** backup,
9340** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9341** the data between the two databases, and finally
9342** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9343** associated with the backup operation.
9344** </ol>)^
9345** There should be exactly one call to sqlite3_backup_finish() for each
9346** successful call to sqlite3_backup_init().
9347**
9348** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9349**
9350** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9351** [database connection] associated with the destination database
9352** and the database name, respectively.
9353** ^The database name is "main" for the main database, "temp" for the
9354** temporary database, or the name specified after the AS keyword in
9355** an [ATTACH] statement for an attached database.
9356** ^The S and M arguments passed to
9357** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9358** and database name of the source database, respectively.
9359** ^The source and destination [database connections] (parameters S and D)
9360** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9361** an error.
9362**
9363** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9364** there is already a read or read-write transaction open on the
9365** destination database.
9366**
9367** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9368** returned and an error code and error message are stored in the
9369** destination [database connection] D.
9370** ^The error code and message for the failed call to sqlite3_backup_init()
9371** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9372** [sqlite3_errmsg16()] functions.
9373** ^A successful call to sqlite3_backup_init() returns a pointer to an
9374** [sqlite3_backup] object.
9375** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9376** sqlite3_backup_finish() functions to perform the specified backup
9377** operation.
9378**
9379** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9380**
9381** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9382** the source and destination databases specified by [sqlite3_backup] object B.
9383** ^If N is negative, all remaining source pages are copied.
9384** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9385** are still more pages to be copied, then the function returns [SQLITE_OK].
9386** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9387** from source to destination, then it returns [SQLITE_DONE].
9388** ^If an error occurs while running sqlite3_backup_step(B,N),
9389** then an [error code] is returned. ^As well as [SQLITE_OK] and
9390** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9391** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9392** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9393**
9394** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9395** <ol>
9396** <li> the destination database was opened read-only, or
9397** <li> the destination database is using write-ahead-log journaling
9398** and the destination and source page sizes differ, or
9399** <li> the destination database is an in-memory database and the
9400** destination and source page sizes differ.
9401** </ol>)^
9402**
9403** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9404** the [sqlite3_busy_handler | busy-handler function]
9405** is invoked (if one is specified). ^If the
9406** busy-handler returns non-zero before the lock is available, then
9407** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9408** sqlite3_backup_step() can be retried later. ^If the source
9409** [database connection]
9410** is being used to write to the source database when sqlite3_backup_step()
9411** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9412** case the call to sqlite3_backup_step() can be retried later on. ^(If
9413** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9414** [SQLITE_READONLY] is returned, then
9415** there is no point in retrying the call to sqlite3_backup_step(). These
9416** errors are considered fatal.)^ The application must accept
9417** that the backup operation has failed and pass the backup operation handle
9418** to the sqlite3_backup_finish() to release associated resources.
9419**
9420** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9421** on the destination file. ^The exclusive lock is not released until either
9422** sqlite3_backup_finish() is called or the backup operation is complete
9423** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9424** sqlite3_backup_step() obtains a [shared lock] on the source database that
9425** lasts for the duration of the sqlite3_backup_step() call.
9426** ^Because the source database is not locked between calls to
9427** sqlite3_backup_step(), the source database may be modified mid-way
9428** through the backup process. ^If the source database is modified by an
9429** external process or via a database connection other than the one being
9430** used by the backup operation, then the backup will be automatically
9431** restarted by the next call to sqlite3_backup_step(). ^If the source
9432** database is modified by the using the same database connection as is used
9433** by the backup operation, then the backup database is automatically
9434** updated at the same time.
9435**
9436** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9437**
9438** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9439** application wishes to abandon the backup operation, the application
9440** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9441** ^The sqlite3_backup_finish() interfaces releases all
9442** resources associated with the [sqlite3_backup] object.
9443** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9444** active write-transaction on the destination database is rolled back.
9445** The [sqlite3_backup] object is invalid
9446** and may not be used following a call to sqlite3_backup_finish().
9447**
9448** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9449** sqlite3_backup_step() errors occurred, regardless or whether or not
9450** sqlite3_backup_step() completed.
9451** ^If an out-of-memory condition or IO error occurred during any prior
9452** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9453** sqlite3_backup_finish() returns the corresponding [error code].
9454**
9455** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9456** is not a permanent error and does not affect the return value of
9457** sqlite3_backup_finish().
9458**
9459** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9460** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9461**
9462** ^The sqlite3_backup_remaining() routine returns the number of pages still
9463** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9464** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9465** in the source database at the conclusion of the most recent
9466** sqlite3_backup_step().
9467** ^(The values returned by these functions are only updated by
9468** sqlite3_backup_step(). If the source database is modified in a way that
9469** changes the size of the source database or the number of pages remaining,
9470** those changes are not reflected in the output of sqlite3_backup_pagecount()
9471** and sqlite3_backup_remaining() until after the next
9472** sqlite3_backup_step().)^
9473**
9474** <b>Concurrent Usage of Database Handles</b>
9475**
9476** ^The source [database connection] may be used by the application for other
9477** purposes while a backup operation is underway or being initialized.
9478** ^If SQLite is compiled and configured to support threadsafe database
9479** connections, then the source database connection may be used concurrently
9480** from within other threads.
9481**
9482** However, the application must guarantee that the destination
9483** [database connection] is not passed to any other API (by any thread) after
9484** sqlite3_backup_init() is called and before the corresponding call to
9485** sqlite3_backup_finish(). SQLite does not currently check to see
9486** if the application incorrectly accesses the destination [database connection]
9487** and so no error code is reported, but the operations may malfunction
9488** nevertheless. Use of the destination database connection while a
9489** backup is in progress might also cause a mutex deadlock.
9490**
9491** If running in [shared cache mode], the application must
9492** guarantee that the shared cache used by the destination database
9493** is not accessed while the backup is running. In practice this means
9494** that the application must guarantee that the disk file being
9495** backed up to is not accessed by any connection within the process,
9496** not just the specific connection that was passed to sqlite3_backup_init().
9497**
9498** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9499** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9500** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9501** APIs are not strictly speaking threadsafe. If they are invoked at the
9502** same time as another thread is invoking sqlite3_backup_step() it is
9503** possible that they return invalid values.
9504*/
9505SQLITE_API sqlite3_backup *sqlite3_backup_init(
9506 sqlite3 *pDest, /* Destination database handle */
9507 const char *zDestName, /* Destination database name */
9508 sqlite3 *pSource, /* Source database handle */
9509 const char *zSourceName /* Source database name */
9510);
9511SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9512SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9513SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9514SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9515
9516/*
9517** CAPI3REF: Unlock Notification
9518** METHOD: sqlite3
9519**
9520** ^When running in shared-cache mode, a database operation may fail with
9521** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9522** individual tables within the shared-cache cannot be obtained. See
9523** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9524** ^This API may be used to register a callback that SQLite will invoke
9525** when the connection currently holding the required lock relinquishes it.
9526** ^This API is only available if the library was compiled with the
9527** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9528**
9529** See Also: [Using the SQLite Unlock Notification Feature].
9530**
9531** ^Shared-cache locks are released when a database connection concludes
9532** its current transaction, either by committing it or rolling it back.
9533**
9534** ^When a connection (known as the blocked connection) fails to obtain a
9535** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9536** identity of the database connection (the blocking connection) that
9537** has locked the required resource is stored internally. ^After an
9538** application receives an SQLITE_LOCKED error, it may call the
9539** sqlite3_unlock_notify() method with the blocked connection handle as
9540** the first argument to register for a callback that will be invoked
9541** when the blocking connections current transaction is concluded. ^The
9542** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9543** call that concludes the blocking connection's transaction.
9544**
9545** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9546** there is a chance that the blocking connection will have already
9547** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9548** If this happens, then the specified callback is invoked immediately,
9549** from within the call to sqlite3_unlock_notify().)^
9550**
9551** ^If the blocked connection is attempting to obtain a write-lock on a
9552** shared-cache table, and more than one other connection currently holds
9553** a read-lock on the same table, then SQLite arbitrarily selects one of
9554** the other connections to use as the blocking connection.
9555**
9556** ^(There may be at most one unlock-notify callback registered by a
9557** blocked connection. If sqlite3_unlock_notify() is called when the
9558** blocked connection already has a registered unlock-notify callback,
9559** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9560** called with a NULL pointer as its second argument, then any existing
9561** unlock-notify callback is cancelled. ^The blocked connections
9562** unlock-notify callback may also be cancelled by closing the blocked
9563** connection using [sqlite3_close()].
9564**
9565** The unlock-notify callback is not reentrant. If an application invokes
9566** any sqlite3_xxx API functions from within an unlock-notify callback, a
9567** crash or deadlock may be the result.
9568**
9569** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9570** returns SQLITE_OK.
9571**
9572** <b>Callback Invocation Details</b>
9573**
9574** When an unlock-notify callback is registered, the application provides a
9575** single void* pointer that is passed to the callback when it is invoked.
9576** However, the signature of the callback function allows SQLite to pass
9577** it an array of void* context pointers. The first argument passed to
9578** an unlock-notify callback is a pointer to an array of void* pointers,
9579** and the second is the number of entries in the array.
9580**
9581** When a blocking connection's transaction is concluded, there may be
9582** more than one blocked connection that has registered for an unlock-notify
9583** callback. ^If two or more such blocked connections have specified the
9584** same callback function, then instead of invoking the callback function
9585** multiple times, it is invoked once with the set of void* context pointers
9586** specified by the blocked connections bundled together into an array.
9587** This gives the application an opportunity to prioritize any actions
9588** related to the set of unblocked database connections.
9589**
9590** <b>Deadlock Detection</b>
9591**
9592** Assuming that after registering for an unlock-notify callback a
9593** database waits for the callback to be issued before taking any further
9594** action (a reasonable assumption), then using this API may cause the
9595** application to deadlock. For example, if connection X is waiting for
9596** connection Y's transaction to be concluded, and similarly connection
9597** Y is waiting on connection X's transaction, then neither connection
9598** will proceed and the system may remain deadlocked indefinitely.
9599**
9600** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9601** detection. ^If a given call to sqlite3_unlock_notify() would put the
9602** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9603** unlock-notify callback is registered. The system is said to be in
9604** a deadlocked state if connection A has registered for an unlock-notify
9605** callback on the conclusion of connection B's transaction, and connection
9606** B has itself registered for an unlock-notify callback when connection
9607** A's transaction is concluded. ^Indirect deadlock is also detected, so
9608** the system is also considered to be deadlocked if connection B has
9609** registered for an unlock-notify callback on the conclusion of connection
9610** C's transaction, where connection C is waiting on connection A. ^Any
9611** number of levels of indirection are allowed.
9612**
9613** <b>The "DROP TABLE" Exception</b>
9614**
9615** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9616** always appropriate to call sqlite3_unlock_notify(). There is however,
9617** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9618** SQLite checks if there are any currently executing SELECT statements
9619** that belong to the same connection. If there are, SQLITE_LOCKED is
9620** returned. In this case there is no "blocking connection", so invoking
9621** sqlite3_unlock_notify() results in the unlock-notify callback being
9622** invoked immediately. If the application then re-attempts the "DROP TABLE"
9623** or "DROP INDEX" query, an infinite loop might be the result.
9624**
9625** One way around this problem is to check the extended error code returned
9626** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9627** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9628** the special "DROP TABLE/INDEX" case, the extended error code is just
9629** SQLITE_LOCKED.)^
9630*/
9631SQLITE_API int sqlite3_unlock_notify(
9632 sqlite3 *pBlocked, /* Waiting connection */
9633 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9634 void *pNotifyArg /* Argument to pass to xNotify */
9635);
9636
9637
9638/*
9639** CAPI3REF: String Comparison
9640**
9641** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9642** and extensions to compare the contents of two buffers containing UTF-8
9643** strings in a case-independent fashion, using the same definition of "case
9644** independence" that SQLite uses internally when comparing identifiers.
9645*/
9646SQLITE_API int sqlite3_stricmp(const char *, const char *);
9647SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9648
9649/*
9650** CAPI3REF: String Globbing
9651*
9652** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9653** string X matches the [GLOB] pattern P.
9654** ^The definition of [GLOB] pattern matching used in
9655** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9656** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9657** is case sensitive.
9658**
9659** Note that this routine returns zero on a match and non-zero if the strings
9660** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9661**
9662** See also: [sqlite3_strlike()].
9663*/
9664SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9665
9666/*
9667** CAPI3REF: String LIKE Matching
9668*
9669** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9670** string X matches the [LIKE] pattern P with escape character E.
9671** ^The definition of [LIKE] pattern matching used in
9672** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9673** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9674** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9675** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9676** insensitive - equivalent upper and lower case ASCII characters match
9677** one another.
9678**
9679** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9680** only ASCII characters are case folded.
9681**
9682** Note that this routine returns zero on a match and non-zero if the strings
9683** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9684**
9685** See also: [sqlite3_strglob()].
9686*/
9687SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9688
9689/*
9690** CAPI3REF: Error Logging Interface
9691**
9692** ^The [sqlite3_log()] interface writes a message into the [error log]
9693** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9694** ^If logging is enabled, the zFormat string and subsequent arguments are
9695** used with [sqlite3_snprintf()] to generate the final output string.
9696**
9697** The sqlite3_log() interface is intended for use by extensions such as
9698** virtual tables, collating functions, and SQL functions. While there is
9699** nothing to prevent an application from calling sqlite3_log(), doing so
9700** is considered bad form.
9701**
9702** The zFormat string must not be NULL.
9703**
9704** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9705** will not use dynamically allocated memory. The log message is stored in
9706** a fixed-length buffer on the stack. If the log message is longer than
9707** a few hundred characters, it will be truncated to the length of the
9708** buffer.
9709*/
9710SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9711
9712/*
9713** CAPI3REF: Write-Ahead Log Commit Hook
9714** METHOD: sqlite3
9715**
9716** ^The [sqlite3_wal_hook()] function is used to register a callback that
9717** is invoked each time data is committed to a database in wal mode.
9718**
9719** ^(The callback is invoked by SQLite after the commit has taken place and
9720** the associated write-lock on the database released)^, so the implementation
9721** may read, write or [checkpoint] the database as required.
9722**
9723** ^The first parameter passed to the callback function when it is invoked
9724** is a copy of the third parameter passed to sqlite3_wal_hook() when
9725** registering the callback. ^The second is a copy of the database handle.
9726** ^The third parameter is the name of the database that was written to -
9727** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9728** is the number of pages currently in the write-ahead log file,
9729** including those that were just committed.
9730**
9731** The callback function should normally return [SQLITE_OK]. ^If an error
9732** code is returned, that error will propagate back up through the
9733** SQLite code base to cause the statement that provoked the callback
9734** to report an error, though the commit will have still occurred. If the
9735** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9736** that does not correspond to any valid SQLite error code, the results
9737** are undefined.
9738**
9739** A single database handle may have at most a single write-ahead log callback
9740** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9741** previously registered write-ahead log callback. ^The return value is
9742** a copy of the third parameter from the previous call, if any, or 0.
9743** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the
9744** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9745** overwrite any prior [sqlite3_wal_hook()] settings.
9746*/
9747SQLITE_API void *sqlite3_wal_hook(
9748 sqlite3*,
9749 int(*)(void *,sqlite3*,const char*,int),
9750 void*
9751);
9752
9753/*
9754** CAPI3REF: Configure an auto-checkpoint
9755** METHOD: sqlite3
9756**
9757** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9758** [sqlite3_wal_hook()] that causes any database on [database connection] D
9759** to automatically [checkpoint]
9760** after committing a transaction if there are N or
9761** more frames in the [write-ahead log] file. ^Passing zero or
9762** a negative value as the nFrame parameter disables automatic
9763** checkpoints entirely.
9764**
9765** ^The callback registered by this function replaces any existing callback
9766** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9767** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9768** configured by this function.
9769**
9770** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9771** from SQL.
9772**
9773** ^Checkpoints initiated by this mechanism are
9774** [sqlite3_wal_checkpoint_v2|PASSIVE].
9775**
9776** ^Every new [database connection] defaults to having the auto-checkpoint
9777** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9778** pages. The use of this interface
9779** is only necessary if the default setting is found to be suboptimal
9780** for a particular application.
9781*/
9782SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9783
9784/*
9785** CAPI3REF: Checkpoint a database
9786** METHOD: sqlite3
9787**
9788** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9789** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9790**
9791** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9792** [write-ahead log] for database X on [database connection] D to be
9793** transferred into the database file and for the write-ahead log to
9794** be reset. See the [checkpointing] documentation for addition
9795** information.
9796**
9797** This interface used to be the only way to cause a checkpoint to
9798** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9799** interface was added. This interface is retained for backwards
9800** compatibility and as a convenience for applications that need to manually
9801** start a callback but which do not need the full power (and corresponding
9802** complication) of [sqlite3_wal_checkpoint_v2()].
9803*/
9804SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9805
9806/*
9807** CAPI3REF: Checkpoint a database
9808** METHOD: sqlite3
9809**
9810** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9811** operation on database X of [database connection] D in mode M. Status
9812** information is written back into integers pointed to by L and C.)^
9813** ^(The M parameter must be a valid [checkpoint mode]:)^
9814**
9815** <dl>
9816** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9817** ^Checkpoint as many frames as possible without waiting for any database
9818** readers or writers to finish, then sync the database file if all frames
9819** in the log were checkpointed. ^The [busy-handler callback]
9820** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9821** ^On the other hand, passive mode might leave the checkpoint unfinished
9822** if there are concurrent readers or writers.
9823**
9824** <dt>SQLITE_CHECKPOINT_FULL<dd>
9825** ^This mode blocks (it invokes the
9826** [sqlite3_busy_handler|busy-handler callback]) until there is no
9827** database writer and all readers are reading from the most recent database
9828** snapshot. ^It then checkpoints all frames in the log file and syncs the
9829** database file. ^This mode blocks new database writers while it is pending,
9830** but new database readers are allowed to continue unimpeded.
9831**
9832** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9833** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9834** that after checkpointing the log file it blocks (calls the
9835** [busy-handler callback])
9836** until all readers are reading from the database file only. ^This ensures
9837** that the next writer will restart the log file from the beginning.
9838** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9839** database writer attempts while it is pending, but does not impede readers.
9840**
9841** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9842** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9843** addition that it also truncates the log file to zero bytes just prior
9844** to a successful return.
9845** </dl>
9846**
9847** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9848** the log file or to -1 if the checkpoint could not run because
9849** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9850** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9851** log file (including any that were already checkpointed before the function
9852** was called) or to -1 if the checkpoint could not run due to an error or
9853** because the database is not in WAL mode. ^Note that upon successful
9854** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9855** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9856**
9857** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9858** any other process is running a checkpoint operation at the same time, the
9859** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9860** busy-handler configured, it will not be invoked in this case.
9861**
9862** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9863** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9864** obtained immediately, and a busy-handler is configured, it is invoked and
9865** the writer lock retried until either the busy-handler returns 0 or the lock
9866** is successfully obtained. ^The busy-handler is also invoked while waiting for
9867** database readers as described above. ^If the busy-handler returns 0 before
9868** the writer lock is obtained or while waiting for database readers, the
9869** checkpoint operation proceeds from that point in the same way as
9870** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9871** without blocking any further. ^SQLITE_BUSY is returned in this case.
9872**
9873** ^If parameter zDb is NULL or points to a zero length string, then the
9874** specified operation is attempted on all WAL databases [attached] to
9875** [database connection] db. In this case the
9876** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9877** an SQLITE_BUSY error is encountered when processing one or more of the
9878** attached WAL databases, the operation is still attempted on any remaining
9879** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9880** error occurs while processing an attached database, processing is abandoned
9881** and the error code is returned to the caller immediately. ^If no error
9882** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9883** databases, SQLITE_OK is returned.
9884**
9885** ^If database zDb is the name of an attached database that is not in WAL
9886** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9887** zDb is not NULL (or a zero length string) and is not the name of any
9888** attached database, SQLITE_ERROR is returned to the caller.
9889**
9890** ^Unless it returns SQLITE_MISUSE,
9891** the sqlite3_wal_checkpoint_v2() interface
9892** sets the error information that is queried by
9893** [sqlite3_errcode()] and [sqlite3_errmsg()].
9894**
9895** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9896** from SQL.
9897*/
9898SQLITE_API int sqlite3_wal_checkpoint_v2(
9899 sqlite3 *db, /* Database handle */
9900 const char *zDb, /* Name of attached database (or NULL) */
9901 int eMode, /* SQLITE_CHECKPOINT_* value */
9902 int *pnLog, /* OUT: Size of WAL log in frames */
9903 int *pnCkpt /* OUT: Total number of frames checkpointed */
9904);
9905
9906/*
9907** CAPI3REF: Checkpoint Mode Values
9908** KEYWORDS: {checkpoint mode}
9909**
9910** These constants define all valid values for the "checkpoint mode" passed
9911** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9912** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9913** meaning of each of these checkpoint modes.
9914*/
9915#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9916#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9917#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */
9918#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9919
9920/*
9921** CAPI3REF: Virtual Table Interface Configuration
9922**
9923** This function may be called by either the [xConnect] or [xCreate] method
9924** of a [virtual table] implementation to configure
9925** various facets of the virtual table interface.
9926**
9927** If this interface is invoked outside the context of an xConnect or
9928** xCreate virtual table method then the behavior is undefined.
9929**
9930** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
9931** [database connection] in which the virtual table is being created and
9932** which is passed in as the first argument to the [xConnect] or [xCreate]
9933** method that is invoking sqlite3_vtab_config(). The C parameter is one
9934** of the [virtual table configuration options]. The presence and meaning
9935** of parameters after C depend on which [virtual table configuration option]
9936** is used.
9937*/
9938SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9939
9940/*
9941** CAPI3REF: Virtual Table Configuration Options
9942** KEYWORDS: {virtual table configuration options}
9943** KEYWORDS: {virtual table configuration option}
9944**
9945** These macros define the various options to the
9946** [sqlite3_vtab_config()] interface that [virtual table] implementations
9947** can use to customize and optimize their behavior.
9948**
9949** <dl>
9950** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
9951** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
9952** <dd>Calls of the form
9953** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9954** where X is an integer. If X is zero, then the [virtual table] whose
9955** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9956** support constraints. In this configuration (which is the default) if
9957** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9958** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9959** specified as part of the users SQL statement, regardless of the actual
9960** ON CONFLICT mode specified.
9961**
9962** If X is non-zero, then the virtual table implementation guarantees
9963** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9964** any modifications to internal or persistent data structures have been made.
9965** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9966** is able to roll back a statement or database transaction, and abandon
9967** or continue processing the current SQL statement as appropriate.
9968** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9969** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9970** had been ABORT.
9971**
9972** Virtual table implementations that are required to handle OR REPLACE
9973** must do so within the [xUpdate] method. If a call to the
9974** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9975** CONFLICT policy is REPLACE, the virtual table implementation should
9976** silently replace the appropriate rows within the xUpdate callback and
9977** return SQLITE_OK. Or, if this is not possible, it may return
9978** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9979** constraint handling.
9980** </dd>
9981**
9982** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
9983** <dd>Calls of the form
9984** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
9985** the [xConnect] or [xCreate] methods of a [virtual table] implementation
9986** prohibits that virtual table from being used from within triggers and
9987** views.
9988** </dd>
9989**
9990** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
9991** <dd>Calls of the form
9992** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
9993** the [xConnect] or [xCreate] methods of a [virtual table] implementation
9994** identify that virtual table as being safe to use from within triggers
9995** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
9996** virtual table can do no serious harm even if it is controlled by a
9997** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
9998** flag unless absolutely necessary.
9999** </dd>
10000**
10001** [[SQLITE_VTAB_USES_ALL_SCHEMAS]]<dt>SQLITE_VTAB_USES_ALL_SCHEMAS</dt>
10002** <dd>Calls of the form
10003** [sqlite3_vtab_config](db,SQLITE_VTAB_USES_ALL_SCHEMA) from within the
10004** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10005** instruct the query planner to begin at least a read transaction on
10006** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10007** virtual table is used.
10008** </dd>
10009** </dl>
10010*/
10011#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
10012#define SQLITE_VTAB_INNOCUOUS 2
10013#define SQLITE_VTAB_DIRECTONLY 3
10014#define SQLITE_VTAB_USES_ALL_SCHEMAS 4
10015
10016/*
10017** CAPI3REF: Determine The Virtual Table Conflict Policy
10018**
10019** This function may only be called from within a call to the [xUpdate] method
10020** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
10021** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
10022** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
10023** of the SQL statement that triggered the call to the [xUpdate] method of the
10024** [virtual table].
10025*/
10026SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
10027
10028/*
10029** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
10030**
10031** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
10032** method of a [virtual table], then it might return true if the
10033** column is being fetched as part of an UPDATE operation during which the
10034** column value will not change. The virtual table implementation can use
10035** this hint as permission to substitute a return value that is less
10036** expensive to compute and that the corresponding
10037** [xUpdate] method understands as a "no-change" value.
10038**
10039** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
10040** the column is not changed by the UPDATE statement, then the xColumn
10041** method can optionally return without setting a result, without calling
10042** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
10043** In that case, [sqlite3_value_nochange(X)] will return true for the
10044** same column in the [xUpdate] method.
10045**
10046** The sqlite3_vtab_nochange() routine is an optimization. Virtual table
10047** implementations should continue to give a correct answer even if the
10048** sqlite3_vtab_nochange() interface were to always return false. In the
10049** current implementation, the sqlite3_vtab_nochange() interface does always
10050** returns false for the enhanced [UPDATE FROM] statement.
10051*/
10052SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
10053
10054/*
10055** CAPI3REF: Determine The Collation For a Virtual Table Constraint
10056** METHOD: sqlite3_index_info
10057**
10058** This function may only be called from within a call to the [xBestIndex]
10059** method of a [virtual table]. This function returns a pointer to a string
10060** that is the name of the appropriate collation sequence to use for text
10061** comparisons on the constraint identified by its arguments.
10062**
10063** The first argument must be the pointer to the [sqlite3_index_info] object
10064** that is the first parameter to the xBestIndex() method. The second argument
10065** must be an index into the aConstraint[] array belonging to the
10066** sqlite3_index_info structure passed to xBestIndex.
10067**
10068** Important:
10069** The first parameter must be the same pointer that is passed into the
10070** xBestMethod() method. The first parameter may not be a pointer to a
10071** different [sqlite3_index_info] object, even an exact copy.
10072**
10073** The return value is computed as follows:
10074**
10075** <ol>
10076** <li><p> If the constraint comes from a WHERE clause expression that contains
10077** a [COLLATE operator], then the name of the collation specified by
10078** that COLLATE operator is returned.
10079** <li><p> If there is no COLLATE operator, but the column that is the subject
10080** of the constraint specifies an alternative collating sequence via
10081** a [COLLATE clause] on the column definition within the CREATE TABLE
10082** statement that was passed into [sqlite3_declare_vtab()], then the
10083** name of that alternative collating sequence is returned.
10084** <li><p> Otherwise, "BINARY" is returned.
10085** </ol>
10086*/
10087SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10088
10089/*
10090** CAPI3REF: Determine if a virtual table query is DISTINCT
10091** METHOD: sqlite3_index_info
10092**
10093** This API may only be used from within an [xBestIndex|xBestIndex method]
10094** of a [virtual table] implementation. The result of calling this
10095** interface from outside of xBestIndex() is undefined and probably harmful.
10096**
10097** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and
10098** 3. The integer returned by sqlite3_vtab_distinct()
10099** gives the virtual table additional information about how the query
10100** planner wants the output to be ordered. As long as the virtual table
10101** can meet the ordering requirements of the query planner, it may set
10102** the "orderByConsumed" flag.
10103**
10104** <ol><li value="0"><p>
10105** ^If the sqlite3_vtab_distinct() interface returns 0, that means
10106** that the query planner needs the virtual table to return all rows in the
10107** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
10108** [sqlite3_index_info] object. This is the default expectation. If the
10109** virtual table outputs all rows in sorted order, then it is always safe for
10110** the xBestIndex method to set the "orderByConsumed" flag, regardless of
10111** the return value from sqlite3_vtab_distinct().
10112** <li value="1"><p>
10113** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
10114** that the query planner does not need the rows to be returned in sorted order
10115** as long as all rows with the same values in all columns identified by the
10116** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
10117** is doing a GROUP BY.
10118** <li value="2"><p>
10119** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
10120** that the query planner does not need the rows returned in any particular
10121** order, as long as rows with the same values in all "aOrderBy" columns
10122** are adjacent.)^ ^(Furthermore, only a single row for each particular
10123** combination of values in the columns identified by the "aOrderBy" field
10124** needs to be returned.)^ ^It is always ok for two or more rows with the same
10125** values in all "aOrderBy" columns to be returned, as long as all such rows
10126** are adjacent. ^The virtual table may, if it chooses, omit extra rows
10127** that have the same value for all columns identified by "aOrderBy".
10128** ^However omitting the extra rows is optional.
10129** This mode is used for a DISTINCT query.
10130** <li value="3"><p>
10131** ^(If the sqlite3_vtab_distinct() interface returns 3, that means
10132** that the query planner needs only distinct rows but it does need the
10133** rows to be sorted.)^ ^The virtual table implementation is free to omit
10134** rows that are identical in all aOrderBy columns, if it wants to, but
10135** it is not required to omit any rows. This mode is used for queries
10136** that have both DISTINCT and ORDER BY clauses.
10137** </ol>
10138**
10139** ^For the purposes of comparing virtual table output values to see if the
10140** values are same value for sorting purposes, two NULL values are considered
10141** to be the same. In other words, the comparison operator is "IS"
10142** (or "IS NOT DISTINCT FROM") and not "==".
10143**
10144** If a virtual table implementation is unable to meet the requirements
10145** specified above, then it must not set the "orderByConsumed" flag in the
10146** [sqlite3_index_info] object or an incorrect answer may result.
10147**
10148** ^A virtual table implementation is always free to return rows in any order
10149** it wants, as long as the "orderByConsumed" flag is not set. ^When the
10150** the "orderByConsumed" flag is unset, the query planner will add extra
10151** [bytecode] to ensure that the final results returned by the SQL query are
10152** ordered correctly. The use of the "orderByConsumed" flag and the
10153** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful
10154** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed"
10155** flag might help queries against a virtual table to run faster. Being
10156** overly aggressive and setting the "orderByConsumed" flag when it is not
10157** valid to do so, on the other hand, might cause SQLite to return incorrect
10158** results.
10159*/
10160SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
10161
10162/*
10163** CAPI3REF: Identify and handle IN constraints in xBestIndex
10164**
10165** This interface may only be used from within an
10166** [xBestIndex|xBestIndex() method] of a [virtual table] implementation.
10167** The result of invoking this interface from any other context is
10168** undefined and probably harmful.
10169**
10170** ^(A constraint on a virtual table of the form
10171** "[IN operator|column IN (...)]" is
10172** communicated to the xBestIndex method as a
10173** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use
10174** this constraint, it must set the corresponding
10175** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under
10176** the usual mode of handling IN operators, SQLite generates [bytecode]
10177** that invokes the [xFilter|xFilter() method] once for each value
10178** on the right-hand side of the IN operator.)^ Thus the virtual table
10179** only sees a single value from the right-hand side of the IN operator
10180** at a time.
10181**
10182** In some cases, however, it would be advantageous for the virtual
10183** table to see all values on the right-hand of the IN operator all at
10184** once. The sqlite3_vtab_in() interfaces facilitates this in two ways:
10185**
10186** <ol>
10187** <li><p>
10188** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10189** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10190** is an [IN operator] that can be processed all at once. ^In other words,
10191** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10192** by which the virtual table can ask SQLite if all-at-once processing
10193** of the IN operator is even possible.
10194**
10195** <li><p>
10196** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates
10197** to SQLite that the virtual table does or does not want to process
10198** the IN operator all-at-once, respectively. ^Thus when the third
10199** parameter (F) is non-negative, this interface is the mechanism by
10200** which the virtual table tells SQLite how it wants to process the
10201** IN operator.
10202** </ol>
10203**
10204** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times
10205** within the same xBestIndex method call. ^For any given P,N pair,
10206** the return value from sqlite3_vtab_in(P,N,F) will always be the same
10207** within the same xBestIndex call. ^If the interface returns true
10208** (non-zero), that means that the constraint is an IN operator
10209** that can be processed all-at-once. ^If the constraint is not an IN
10210** operator or cannot be processed all-at-once, then the interface returns
10211** false.
10212**
10213** ^(All-at-once processing of the IN operator is selected if both of the
10214** following conditions are met:
10215**
10216** <ol>
10217** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10218** integer. This is how the virtual table tells SQLite that it wants to
10219** use the N-th constraint.
10220**
10221** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was
10222** non-negative had F>=1.
10223** </ol>)^
10224**
10225** ^If either or both of the conditions above are false, then SQLite uses
10226** the traditional one-at-a-time processing strategy for the IN constraint.
10227** ^If both conditions are true, then the argvIndex-th parameter to the
10228** xFilter method will be an [sqlite3_value] that appears to be NULL,
10229** but which can be passed to [sqlite3_vtab_in_first()] and
10230** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10231** of the IN constraint.
10232*/
10233SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle);
10234
10235/*
10236** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10237**
10238** These interfaces are only useful from within the
10239** [xFilter|xFilter() method] of a [virtual table] implementation.
10240** The result of invoking these interfaces from any other context
10241** is undefined and probably harmful.
10242**
10243** The X parameter in a call to sqlite3_vtab_in_first(X,P) or
10244** sqlite3_vtab_in_next(X,P) should be one of the parameters to the
10245** xFilter method which invokes these routines, and specifically
10246** a parameter that was previously selected for all-at-once IN constraint
10247** processing use the [sqlite3_vtab_in()] interface in the
10248** [xBestIndex|xBestIndex method]. ^(If the X parameter is not
10249** an xFilter argument that was selected for all-at-once IN constraint
10250** processing, then these routines return [SQLITE_ERROR].)^
10251**
10252** ^(Use these routines to access all values on the right-hand side
10253** of the IN constraint using code like the following:
10254**
10255** <blockquote><pre>
10256** &nbsp; for(rc=sqlite3_vtab_in_first(pList, &pVal);
10257** &nbsp; rc==SQLITE_OK && pVal;
10258** &nbsp; rc=sqlite3_vtab_in_next(pList, &pVal)
10259** &nbsp; ){
10260** &nbsp; // do something with pVal
10261** &nbsp; }
10262** &nbsp; if( rc!=SQLITE_OK ){
10263** &nbsp; // an error has occurred
10264** &nbsp; }
10265** </pre></blockquote>)^
10266**
10267** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P)
10268** routines return SQLITE_OK and set *P to point to the first or next value
10269** on the RHS of the IN constraint. ^If there are no more values on the
10270** right hand side of the IN constraint, then *P is set to NULL and these
10271** routines return [SQLITE_DONE]. ^The return value might be
10272** some other value, such as SQLITE_NOMEM, in the event of a malfunction.
10273**
10274** The *ppOut values returned by these routines are only valid until the
10275** next call to either of these routines or until the end of the xFilter
10276** method from which these routines were called. If the virtual table
10277** implementation needs to retain the *ppOut values for longer, it must make
10278** copies. The *ppOut values are [protected sqlite3_value|protected].
10279*/
10280SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut);
10281SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10282
10283/*
10284** CAPI3REF: Constraint values in xBestIndex()
10285** METHOD: sqlite3_index_info
10286**
10287** This API may only be used from within the [xBestIndex|xBestIndex method]
10288** of a [virtual table] implementation. The result of calling this interface
10289** from outside of an xBestIndex method are undefined and probably harmful.
10290**
10291** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
10292** the [xBestIndex] method of a [virtual table] implementation, with P being
10293** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
10294** J being a 0-based index into P->aConstraint[], then this routine
10295** attempts to set *V to the value of the right-hand operand of
10296** that constraint if the right-hand operand is known. ^If the
10297** right-hand operand is not known, then *V is set to a NULL pointer.
10298** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
10299** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
10300** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10301** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
10302** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
10303** something goes wrong.
10304**
10305** The sqlite3_vtab_rhs_value() interface is usually only successful if
10306** the right-hand operand of a constraint is a literal value in the original
10307** SQL statement. If the right-hand operand is an expression or a reference
10308** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
10309** will probably return [SQLITE_NOTFOUND].
10310**
10311** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
10312** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10313** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
10314**
10315** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
10316** and remains valid for the duration of the xBestIndex method call.
10317** ^When xBestIndex returns, the sqlite3_value object returned by
10318** sqlite3_vtab_rhs_value() is automatically deallocated.
10319**
10320** The "_rhs_" in the name of this routine is an abbreviation for
10321** "Right-Hand Side".
10322*/
10323SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
10324
10325/*
10326** CAPI3REF: Conflict resolution modes
10327** KEYWORDS: {conflict resolution mode}
10328**
10329** These constants are returned by [sqlite3_vtab_on_conflict()] to
10330** inform a [virtual table] implementation what the [ON CONFLICT] mode
10331** is for the SQL statement being evaluated.
10332**
10333** Note that the [SQLITE_IGNORE] constant is also used as a potential
10334** return value from the [sqlite3_set_authorizer()] callback and that
10335** [SQLITE_ABORT] is also a [result code].
10336*/
10337#define SQLITE_ROLLBACK 1
10338/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
10339#define SQLITE_FAIL 3
10340/* #define SQLITE_ABORT 4 // Also an error code */
10341#define SQLITE_REPLACE 5
10342
10343/*
10344** CAPI3REF: Prepared Statement Scan Status Opcodes
10345** KEYWORDS: {scanstatus options}
10346**
10347** The following constants can be used for the T parameter to the
10348** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
10349** different metric for sqlite3_stmt_scanstatus() to return.
10350**
10351** When the value returned to V is a string, space to hold that string is
10352** managed by the prepared statement S and will be automatically freed when
10353** S is finalized.
10354**
10355** Not all values are available for all query elements. When a value is
10356** not available, the output variable is set to -1 if the value is numeric,
10357** or to NULL if it is a string (SQLITE_SCANSTAT_NAME).
10358**
10359** <dl>
10360** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
10361** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
10362** set to the total number of times that the X-th loop has run.</dd>
10363**
10364** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
10365** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
10366** to the total number of rows examined by all iterations of the X-th loop.</dd>
10367**
10368** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
10369** <dd>^The "double" variable pointed to by the V parameter will be set to the
10370** query planner's estimate for the average number of rows output from each
10371** iteration of the X-th loop. If the query planner's estimates was accurate,
10372** then this value will approximate the quotient NVISIT/NLOOP and the
10373** product of this value for all prior loops with the same SELECTID will
10374** be the NLOOP value for the current loop.
10375**
10376** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
10377** <dd>^The "const char *" variable pointed to by the V parameter will be set
10378** to a zero-terminated UTF-8 string containing the name of the index or table
10379** used for the X-th loop.
10380**
10381** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
10382** <dd>^The "const char *" variable pointed to by the V parameter will be set
10383** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10384** description for the X-th loop.
10385**
10386** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECTID</dt>
10387** <dd>^The "int" variable pointed to by the V parameter will be set to the
10388** id for the X-th query plan element. The id value is unique within the
10389** statement. The select-id is the same value as is output in the first
10390** column of an [EXPLAIN QUERY PLAN] query.
10391**
10392** [[SQLITE_SCANSTAT_PARENTID]] <dt>SQLITE_SCANSTAT_PARENTID</dt>
10393** <dd>The "int" variable pointed to by the V parameter will be set to the
10394** the id of the parent of the current query element, if applicable, or
10395** to zero if the query element has no parent. This is the same value as
10396** returned in the second column of an [EXPLAIN QUERY PLAN] query.
10397**
10398** [[SQLITE_SCANSTAT_NCYCLE]] <dt>SQLITE_SCANSTAT_NCYCLE</dt>
10399** <dd>The sqlite3_int64 output value is set to the number of cycles,
10400** according to the processor time-stamp counter, that elapsed while the
10401** query element was being processed. This value is not available for
10402** all query elements - if it is unavailable the output variable is
10403** set to -1.
10404** </dl>
10405*/
10406#define SQLITE_SCANSTAT_NLOOP 0
10407#define SQLITE_SCANSTAT_NVISIT 1
10408#define SQLITE_SCANSTAT_EST 2
10409#define SQLITE_SCANSTAT_NAME 3
10410#define SQLITE_SCANSTAT_EXPLAIN 4
10411#define SQLITE_SCANSTAT_SELECTID 5
10412#define SQLITE_SCANSTAT_PARENTID 6
10413#define SQLITE_SCANSTAT_NCYCLE 7
10414
10415/*
10416** CAPI3REF: Prepared Statement Scan Status
10417** METHOD: sqlite3_stmt
10418**
10419** These interfaces return information about the predicted and measured
10420** performance for pStmt. Advanced applications can use this
10421** interface to compare the predicted and the measured performance and
10422** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
10423**
10424** Since this interface is expected to be rarely used, it is only
10425** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
10426** compile-time option.
10427**
10428** The "iScanStatusOp" parameter determines which status information to return.
10429** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
10430** of this interface is undefined. ^The requested measurement is written into
10431** a variable pointed to by the "pOut" parameter.
10432**
10433** The "flags" parameter must be passed a mask of flags. At present only
10434** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10435** is specified, then status information is available for all elements
10436** of a query plan that are reported by "EXPLAIN QUERY PLAN" output. If
10437** SQLITE_SCANSTAT_COMPLEX is not specified, then only query plan elements
10438** that correspond to query loops (the "SCAN..." and "SEARCH..." elements of
10439** the EXPLAIN QUERY PLAN output) are available. Invoking API
10440** sqlite3_stmt_scanstatus() is equivalent to calling
10441** sqlite3_stmt_scanstatus_v2() with a zeroed flags parameter.
10442**
10443** Parameter "idx" identifies the specific query element to retrieve statistics
10444** for. Query elements are numbered starting from zero. A value of -1 may be
10445** to query for statistics regarding the entire query. ^If idx is out of range
10446** - less than -1 or greater than or equal to the total number of query
10447** elements used to implement the statement - a non-zero value is returned and
10448** the variable that pOut points to is unchanged.
10449**
10450** See also: [sqlite3_stmt_scanstatus_reset()]
10451*/
10452SQLITE_API int sqlite3_stmt_scanstatus(
10453 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10454 int idx, /* Index of loop to report on */
10455 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10456 void *pOut /* Result written here */
10457);
10458SQLITE_API int sqlite3_stmt_scanstatus_v2(
10459 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10460 int idx, /* Index of loop to report on */
10461 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10462 int flags, /* Mask of flags defined below */
10463 void *pOut /* Result written here */
10464);
10465
10466/*
10467** CAPI3REF: Prepared Statement Scan Status
10468** KEYWORDS: {scan status flags}
10469*/
10470#define SQLITE_SCANSTAT_COMPLEX 0x0001
10471
10472/*
10473** CAPI3REF: Zero Scan-Status Counters
10474** METHOD: sqlite3_stmt
10475**
10476** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10477**
10478** This API is only available if the library is built with pre-processor
10479** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10480*/
10481SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10482
10483/*
10484** CAPI3REF: Flush caches to disk mid-transaction
10485** METHOD: sqlite3
10486**
10487** ^If a write-transaction is open on [database connection] D when the
10488** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
10489** pages in the pager-cache that are not currently in use are written out
10490** to disk. A dirty page may be in use if a database cursor created by an
10491** active SQL statement is reading from it, or if it is page 1 of a database
10492** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
10493** interface flushes caches for all schemas - "main", "temp", and
10494** any [attached] databases.
10495**
10496** ^If this function needs to obtain extra database locks before dirty pages
10497** can be flushed to disk, it does so. ^If those locks cannot be obtained
10498** immediately and there is a busy-handler callback configured, it is invoked
10499** in the usual manner. ^If the required lock still cannot be obtained, then
10500** the database is skipped and an attempt made to flush any dirty pages
10501** belonging to the next (if any) database. ^If any databases are skipped
10502** because locks cannot be obtained, but no other error occurs, this
10503** function returns SQLITE_BUSY.
10504**
10505** ^If any other error occurs while flushing dirty pages to disk (for
10506** example an IO error or out-of-memory condition), then processing is
10507** abandoned and an SQLite [error code] is returned to the caller immediately.
10508**
10509** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10510**
10511** ^This function does not set the database handle error code or message
10512** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10513*/
10514SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10515
10516/*
10517** CAPI3REF: The pre-update hook.
10518** METHOD: sqlite3
10519**
10520** ^These interfaces are only available if SQLite is compiled using the
10521** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10522**
10523** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10524** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10525** on a database table.
10526** ^At most one preupdate hook may be registered at a time on a single
10527** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10528** the previous setting.
10529** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10530** with a NULL pointer as the second parameter.
10531** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10532** the first parameter to callbacks.
10533**
10534** ^The preupdate hook only fires for changes to real database tables; the
10535** preupdate hook is not invoked for changes to [virtual tables] or to
10536** system tables like sqlite_sequence or sqlite_stat1.
10537**
10538** ^The second parameter to the preupdate callback is a pointer to
10539** the [database connection] that registered the preupdate hook.
10540** ^The third parameter to the preupdate callback is one of the constants
10541** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10542** kind of update operation that is about to occur.
10543** ^(The fourth parameter to the preupdate callback is the name of the
10544** database within the database connection that is being modified. This
10545** will be "main" for the main database or "temp" for TEMP tables or
10546** the name given after the AS keyword in the [ATTACH] statement for attached
10547** databases.)^
10548** ^The fifth parameter to the preupdate callback is the name of the
10549** table that is being modified.
10550**
10551** For an UPDATE or DELETE operation on a [rowid table], the sixth
10552** parameter passed to the preupdate callback is the initial [rowid] of the
10553** row being modified or deleted. For an INSERT operation on a rowid table,
10554** or any operation on a WITHOUT ROWID table, the value of the sixth
10555** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10556** seventh parameter is the final rowid value of the row being inserted
10557** or updated. The value of the seventh parameter passed to the callback
10558** function is not defined for operations on WITHOUT ROWID tables, or for
10559** DELETE operations on rowid tables.
10560**
10561** ^The sqlite3_preupdate_hook(D,C,P) function returns the P argument from
10562** the previous call on the same [database connection] D, or NULL for
10563** the first call on D.
10564**
10565** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10566** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10567** provide additional information about a preupdate event. These routines
10568** may only be called from within a preupdate callback. Invoking any of
10569** these routines from outside of a preupdate callback or with a
10570** [database connection] pointer that is different from the one supplied
10571** to the preupdate callback results in undefined and probably undesirable
10572** behavior.
10573**
10574** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10575** in the row that is being inserted, updated, or deleted.
10576**
10577** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10578** a [protected sqlite3_value] that contains the value of the Nth column of
10579** the table row before it is updated. The N parameter must be between 0
10580** and one less than the number of columns or the behavior will be
10581** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10582** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10583** behavior is undefined. The [sqlite3_value] that P points to
10584** will be destroyed when the preupdate callback returns.
10585**
10586** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10587** a [protected sqlite3_value] that contains the value of the Nth column of
10588** the table row after it is updated. The N parameter must be between 0
10589** and one less than the number of columns or the behavior will be
10590** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10591** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10592** behavior is undefined. The [sqlite3_value] that P points to
10593** will be destroyed when the preupdate callback returns.
10594**
10595** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10596** callback was invoked as a result of a direct insert, update, or delete
10597** operation; or 1 for inserts, updates, or deletes invoked by top-level
10598** triggers; or 2 for changes resulting from triggers called by top-level
10599** triggers; and so forth.
10600**
10601** When the [sqlite3_blob_write()] API is used to update a blob column,
10602** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10603** in this case the new values are not available. In this case, when a
10604** callback made with op==SQLITE_DELETE is actually a write using the
10605** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10606** the index of the column being written. In other cases, where the
10607** pre-update hook is being invoked for some other reason, including a
10608** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10609**
10610** See also: [sqlite3_update_hook()]
10611*/
10612#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
10613SQLITE_API void *sqlite3_preupdate_hook(
10614 sqlite3 *db,
10615 void(*xPreUpdate)(
10616 void *pCtx, /* Copy of third arg to preupdate_hook() */
10617 sqlite3 *db, /* Database handle */
10618 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10619 char const *zDb, /* Database name */
10620 char const *zName, /* Table name */
10621 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10622 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10623 ),
10624 void*
10625);
10626SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10627SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10628SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10629SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10630SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *);
10631#endif
10632
10633/*
10634** CAPI3REF: Low-level system error code
10635** METHOD: sqlite3
10636**
10637** ^Attempt to return the underlying operating system error code or error
10638** number that caused the most recent I/O error or failure to open a file.
10639** The return value is OS-dependent. For example, on unix systems, after
10640** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10641** called to get back the underlying "errno" that caused the problem, such
10642** as ENOSPC, EAUTH, EISDIR, and so forth.
10643*/
10644SQLITE_API int sqlite3_system_errno(sqlite3*);
10645
10646/*
10647** CAPI3REF: Database Snapshot
10648** KEYWORDS: {snapshot} {sqlite3_snapshot}
10649**
10650** An instance of the snapshot object records the state of a [WAL mode]
10651** database for some specific point in history.
10652**
10653** In [WAL mode], multiple [database connections] that are open on the
10654** same database file can each be reading a different historical version
10655** of the database file. When a [database connection] begins a read
10656** transaction, that connection sees an unchanging copy of the database
10657** as it existed for the point in time when the transaction first started.
10658** Subsequent changes to the database from other connections are not seen
10659** by the reader until a new read transaction is started.
10660**
10661** The sqlite3_snapshot object records state information about an historical
10662** version of the database file so that it is possible to later open a new read
10663** transaction that sees that historical version of the database rather than
10664** the most recent version.
10665*/
10666typedef struct sqlite3_snapshot {
10667 unsigned char hidden[48];
10668} sqlite3_snapshot;
10669
10670/*
10671** CAPI3REF: Record A Database Snapshot
10672** CONSTRUCTOR: sqlite3_snapshot
10673**
10674** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10675** new [sqlite3_snapshot] object that records the current state of
10676** schema S in database connection D. ^On success, the
10677** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10678** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10679** If there is not already a read-transaction open on schema S when
10680** this function is called, one is opened automatically.
10681**
10682** The following must be true for this function to succeed. If any of
10683** the following statements are false when sqlite3_snapshot_get() is
10684** called, SQLITE_ERROR is returned. The final value of *P is undefined
10685** in this case.
10686**
10687** <ul>
10688** <li> The database handle must not be in [autocommit mode].
10689**
10690** <li> Schema S of [database connection] D must be a [WAL mode] database.
10691**
10692** <li> There must not be a write transaction open on schema S of database
10693** connection D.
10694**
10695** <li> One or more transactions must have been written to the current wal
10696** file since it was created on disk (by any connection). This means
10697** that a snapshot cannot be taken on a wal mode database with no wal
10698** file immediately after it is first opened. At least one transaction
10699** must be written to it first.
10700** </ul>
10701**
10702** This function may also return SQLITE_NOMEM. If it is called with the
10703** database handle in autocommit mode but fails for some other reason,
10704** whether or not a read transaction is opened on schema S is undefined.
10705**
10706** The [sqlite3_snapshot] object returned from a successful call to
10707** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10708** to avoid a memory leak.
10709**
10710** The [sqlite3_snapshot_get()] interface is only available when the
10711** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10712*/
10713SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10714 sqlite3 *db,
10715 const char *zSchema,
10716 sqlite3_snapshot **ppSnapshot
10717);
10718
10719/*
10720** CAPI3REF: Start a read transaction on an historical snapshot
10721** METHOD: sqlite3_snapshot
10722**
10723** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10724** transaction or upgrades an existing one for schema S of
10725** [database connection] D such that the read transaction refers to
10726** historical [snapshot] P, rather than the most recent change to the
10727** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10728** on success or an appropriate [error code] if it fails.
10729**
10730** ^In order to succeed, the database connection must not be in
10731** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10732** is already a read transaction open on schema S, then the database handle
10733** must have no active statements (SELECT statements that have been passed
10734** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10735** SQLITE_ERROR is returned if either of these conditions is violated, or
10736** if schema S does not exist, or if the snapshot object is invalid.
10737**
10738** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10739** snapshot has been overwritten by a [checkpoint]. In this case
10740** SQLITE_ERROR_SNAPSHOT is returned.
10741**
10742** If there is already a read transaction open when this function is
10743** invoked, then the same read transaction remains open (on the same
10744** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10745** is returned. If another error code - for example SQLITE_PROTOCOL or an
10746** SQLITE_IOERR error code - is returned, then the final state of the
10747** read transaction is undefined. If SQLITE_OK is returned, then the
10748** read transaction is now open on database snapshot P.
10749**
10750** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10751** database connection D does not know that the database file for
10752** schema S is in [WAL mode]. A database connection might not know
10753** that the database file is in [WAL mode] if there has been no prior
10754** I/O on that database connection, or if the database entered [WAL mode]
10755** after the most recent I/O on the database connection.)^
10756** (Hint: Run "[PRAGMA application_id]" against a newly opened
10757** database connection in order to make it ready to use snapshots.)
10758**
10759** The [sqlite3_snapshot_open()] interface is only available when the
10760** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10761*/
10762SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10763 sqlite3 *db,
10764 const char *zSchema,
10765 sqlite3_snapshot *pSnapshot
10766);
10767
10768/*
10769** CAPI3REF: Destroy a snapshot
10770** DESTRUCTOR: sqlite3_snapshot
10771**
10772** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10773** The application must eventually free every [sqlite3_snapshot] object
10774** using this routine to avoid a memory leak.
10775**
10776** The [sqlite3_snapshot_free()] interface is only available when the
10777** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10778*/
10779SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10780
10781/*
10782** CAPI3REF: Compare the ages of two snapshot handles.
10783** METHOD: sqlite3_snapshot
10784**
10785** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10786** of two valid snapshot handles.
10787**
10788** If the two snapshot handles are not associated with the same database
10789** file, the result of the comparison is undefined.
10790**
10791** Additionally, the result of the comparison is only valid if both of the
10792** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10793** last time the wal file was deleted. The wal file is deleted when the
10794** database is changed back to rollback mode or when the number of database
10795** clients drops to zero. If either snapshot handle was obtained before the
10796** wal file was last deleted, the value returned by this function
10797** is undefined.
10798**
10799** Otherwise, this API returns a negative value if P1 refers to an older
10800** snapshot than P2, zero if the two handles refer to the same database
10801** snapshot, and a positive value if P1 is a newer snapshot than P2.
10802**
10803** This interface is only available if SQLite is compiled with the
10804** [SQLITE_ENABLE_SNAPSHOT] option.
10805*/
10806SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10807 sqlite3_snapshot *p1,
10808 sqlite3_snapshot *p2
10809);
10810
10811/*
10812** CAPI3REF: Recover snapshots from a wal file
10813** METHOD: sqlite3_snapshot
10814**
10815** If a [WAL file] remains on disk after all database connections close
10816** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10817** or because the last process to have the database opened exited without
10818** calling [sqlite3_close()]) and a new connection is subsequently opened
10819** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10820** will only be able to open the last transaction added to the WAL file
10821** even though the WAL file contains other valid transactions.
10822**
10823** This function attempts to scan the WAL file associated with database zDb
10824** of database handle db and make all valid snapshots available to
10825** sqlite3_snapshot_open(). It is an error if there is already a read
10826** transaction open on the database, or if the database is not a WAL mode
10827** database.
10828**
10829** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10830**
10831** This interface is only available if SQLite is compiled with the
10832** [SQLITE_ENABLE_SNAPSHOT] option.
10833*/
10834SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10835
10836/*
10837** CAPI3REF: Serialize a database
10838**
10839** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10840** that is a serialization of the S database on [database connection] D.
10841** If P is not a NULL pointer, then the size of the database in bytes
10842** is written into *P.
10843**
10844** For an ordinary on-disk database file, the serialization is just a
10845** copy of the disk file. For an in-memory database or a "TEMP" database,
10846** the serialization is the same sequence of bytes which would be written
10847** to disk if that database where backed up to disk.
10848**
10849** The usual case is that sqlite3_serialize() copies the serialization of
10850** the database into memory obtained from [sqlite3_malloc64()] and returns
10851** a pointer to that memory. The caller is responsible for freeing the
10852** returned value to avoid a memory leak. However, if the F argument
10853** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10854** are made, and the sqlite3_serialize() function will return a pointer
10855** to the contiguous memory representation of the database that SQLite
10856** is currently using for that database, or NULL if the no such contiguous
10857** memory representation of the database exists. A contiguous memory
10858** representation of the database will usually only exist if there has
10859** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10860** values of D and S.
10861** The size of the database is written into *P even if the
10862** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10863** of the database exists.
10864**
10865** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10866** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10867** allocation error occurs.
10868**
10869** This interface is omitted if SQLite is compiled with the
10870** [SQLITE_OMIT_DESERIALIZE] option.
10871*/
10872SQLITE_API unsigned char *sqlite3_serialize(
10873 sqlite3 *db, /* The database connection */
10874 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10875 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10876 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10877);
10878
10879/*
10880** CAPI3REF: Flags for sqlite3_serialize
10881**
10882** Zero or more of the following constants can be OR-ed together for
10883** the F argument to [sqlite3_serialize(D,S,P,F)].
10884**
10885** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10886** a pointer to contiguous in-memory database that it is currently using,
10887** without making a copy of the database. If SQLite is not currently using
10888** a contiguous in-memory database, then this option causes
10889** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10890** using a contiguous in-memory database if it has been initialized by a
10891** prior call to [sqlite3_deserialize()].
10892*/
10893#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
10894
10895/*
10896** CAPI3REF: Deserialize a database
10897**
10898** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10899** [database connection] D to disconnect from database S and then
10900** reopen S as an in-memory database based on the serialization contained
10901** in P. The serialized database P is N bytes in size. M is the size of
10902** the buffer P, which might be larger than N. If M is larger than N, and
10903** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10904** permitted to add content to the in-memory database as long as the total
10905** size does not exceed M bytes.
10906**
10907** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10908** invoke sqlite3_free() on the serialization buffer when the database
10909** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10910** SQLite will try to increase the buffer size using sqlite3_realloc64()
10911** if writes on the database cause it to grow larger than M bytes.
10912**
10913** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10914** database is currently in a read transaction or is involved in a backup
10915** operation.
10916**
10917** It is not possible to deserialized into the TEMP database. If the
10918** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
10919** function returns SQLITE_ERROR.
10920**
10921** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10922** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10923** [sqlite3_free()] is invoked on argument P prior to returning.
10924**
10925** This interface is omitted if SQLite is compiled with the
10926** [SQLITE_OMIT_DESERIALIZE] option.
10927*/
10928SQLITE_API int sqlite3_deserialize(
10929 sqlite3 *db, /* The database connection */
10930 const char *zSchema, /* Which DB to reopen with the deserialization */
10931 unsigned char *pData, /* The serialized database content */
10932 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10933 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10934 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10935);
10936
10937/*
10938** CAPI3REF: Flags for sqlite3_deserialize()
10939**
10940** The following are allowed values for 6th argument (the F argument) to
10941** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10942**
10943** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10944** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10945** and that SQLite should take ownership of this memory and automatically
10946** free it when it has finished using it. Without this flag, the caller
10947** is responsible for freeing any dynamically allocated memory.
10948**
10949** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10950** grow the size of the database using calls to [sqlite3_realloc64()]. This
10951** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10952** Without this flag, the deserialized database cannot increase in size beyond
10953** the number of bytes specified by the M parameter.
10954**
10955** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10956** should be treated as read-only.
10957*/
10958#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
10959#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
10960#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10961
10962/*
10963** Undo the hack that converts floating point types to integer for
10964** builds on processors without floating point support.
10965*/
10966#ifdef SQLITE_OMIT_FLOATING_POINT
10967# undef double
10968#endif
10969
10970#if defined(__wasi__)
10971# undef SQLITE_WASI
10972# define SQLITE_WASI 1
10973# undef SQLITE_OMIT_WAL
10974# define SQLITE_OMIT_WAL 1/* because it requires shared memory APIs */
10975# ifndef SQLITE_OMIT_LOAD_EXTENSION
10976# define SQLITE_OMIT_LOAD_EXTENSION
10977# endif
10978# ifndef SQLITE_THREADSAFE
10979# define SQLITE_THREADSAFE 0
10980# endif
10981#endif
10982
10983#if 0
10984} /* End of the 'extern "C"' block */
10985#endif
10986#endif /* SQLITE3_H */
10987
10988/******** Begin file sqlite3rtree.h *********/
10989/*
10990** 2010 August 30
10991**
10992** The author disclaims copyright to this source code. In place of
10993** a legal notice, here is a blessing:
10994**
10995** May you do good and not evil.
10996** May you find forgiveness for yourself and forgive others.
10997** May you share freely, never taking more than you give.
10998**
10999*************************************************************************
11000*/
11001
11002#ifndef _SQLITE3RTREE_H_
11003#define _SQLITE3RTREE_H_
11004
11005
11006#if 0
11007extern "C" {
11008#endif
11009
11010typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
11011typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
11012
11013/* The double-precision datatype used by RTree depends on the
11014** SQLITE_RTREE_INT_ONLY compile-time option.
11015*/
11016#ifdef SQLITE_RTREE_INT_ONLY
11017 typedef sqlite3_int64 sqlite3_rtree_dbl;
11018#else
11019 typedef double sqlite3_rtree_dbl;
11020#endif
11021
11022/*
11023** Register a geometry callback named zGeom that can be used as part of an
11024** R-Tree geometry query as follows:
11025**
11026** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
11027*/
11028SQLITE_API int sqlite3_rtree_geometry_callback(
11029 sqlite3 *db,
11030 const char *zGeom,
11031 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
11032 void *pContext
11033);
11034
11035
11036/*
11037** A pointer to a structure of the following type is passed as the first
11038** argument to callbacks registered using rtree_geometry_callback().
11039*/
11040struct sqlite3_rtree_geometry {
11041 void *pContext; /* Copy of pContext passed to s_r_g_c() */
11042 int nParam; /* Size of array aParam[] */
11043 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
11044 void *pUser; /* Callback implementation user data */
11045 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
11046};
11047
11048/*
11049** Register a 2nd-generation geometry callback named zScore that can be
11050** used as part of an R-Tree geometry query as follows:
11051**
11052** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
11053*/
11054SQLITE_API int sqlite3_rtree_query_callback(
11055 sqlite3 *db,
11056 const char *zQueryFunc,
11057 int (*xQueryFunc)(sqlite3_rtree_query_info*),
11058 void *pContext,
11059 void (*xDestructor)(void*)
11060);
11061
11062
11063/*
11064** A pointer to a structure of the following type is passed as the
11065** argument to scored geometry callback registered using
11066** sqlite3_rtree_query_callback().
11067**
11068** Note that the first 5 fields of this structure are identical to
11069** sqlite3_rtree_geometry. This structure is a subclass of
11070** sqlite3_rtree_geometry.
11071*/
11072struct sqlite3_rtree_query_info {
11073 void *pContext; /* pContext from when function registered */
11074 int nParam; /* Number of function parameters */
11075 sqlite3_rtree_dbl *aParam; /* value of function parameters */
11076 void *pUser; /* callback can use this, if desired */
11077 void (*xDelUser)(void*); /* function to free pUser */
11078 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
11079 unsigned int *anQueue; /* Number of pending entries in the queue */
11080 int nCoord; /* Number of coordinates */
11081 int iLevel; /* Level of current node or entry */
11082 int mxLevel; /* The largest iLevel value in the tree */
11083 sqlite3_int64 iRowid; /* Rowid for current entry */
11084 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
11085 int eParentWithin; /* Visibility of parent node */
11086 int eWithin; /* OUT: Visibility */
11087 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
11088 /* The following fields are only available in 3.8.11 and later */
11089 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
11090};
11091
11092/*
11093** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
11094*/
11095#define NOT_WITHIN 0 /* Object completely outside of query region */
11096#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
11097#define FULLY_WITHIN 2 /* Object fully contained within query region */
11098
11099
11100#if 0
11101} /* end of the 'extern "C"' block */
11102#endif
11103
11104#endif /* ifndef _SQLITE3RTREE_H_ */
11105
11106/******** End of sqlite3rtree.h *********/
11107/******** Begin file sqlite3session.h *********/
11108
11109#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
11110#define __SQLITESESSION_H_ 1
11111
11112/*
11113** Make sure we can call this stuff from C++.
11114*/
11115#if 0
11116extern "C" {
11117#endif
11118
11119
11120/*
11121** CAPI3REF: Session Object Handle
11122**
11123** An instance of this object is a [session] that can be used to
11124** record changes to a database.
11125*/
11126typedef struct sqlite3_session sqlite3_session;
11127
11128/*
11129** CAPI3REF: Changeset Iterator Handle
11130**
11131** An instance of this object acts as a cursor for iterating
11132** over the elements of a [changeset] or [patchset].
11133*/
11134typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
11135
11136/*
11137** CAPI3REF: Create A New Session Object
11138** CONSTRUCTOR: sqlite3_session
11139**
11140** Create a new session object attached to database handle db. If successful,
11141** a pointer to the new object is written to *ppSession and SQLITE_OK is
11142** returned. If an error occurs, *ppSession is set to NULL and an SQLite
11143** error code (e.g. SQLITE_NOMEM) is returned.
11144**
11145** It is possible to create multiple session objects attached to a single
11146** database handle.
11147**
11148** Session objects created using this function should be deleted using the
11149** [sqlite3session_delete()] function before the database handle that they
11150** are attached to is itself closed. If the database handle is closed before
11151** the session object is deleted, then the results of calling any session
11152** module function, including [sqlite3session_delete()] on the session object
11153** are undefined.
11154**
11155** Because the session module uses the [sqlite3_preupdate_hook()] API, it
11156** is not possible for an application to register a pre-update hook on a
11157** database handle that has one or more session objects attached. Nor is
11158** it possible to create a session object attached to a database handle for
11159** which a pre-update hook is already defined. The results of attempting
11160** either of these things are undefined.
11161**
11162** The session object will be used to create changesets for tables in
11163** database zDb, where zDb is either "main", or "temp", or the name of an
11164** attached database. It is not an error if database zDb is not attached
11165** to the database when the session object is created.
11166*/
11167SQLITE_API int sqlite3session_create(
11168 sqlite3 *db, /* Database handle */
11169 const char *zDb, /* Name of db (e.g. "main") */
11170 sqlite3_session **ppSession /* OUT: New session object */
11171);
11172
11173/*
11174** CAPI3REF: Delete A Session Object
11175** DESTRUCTOR: sqlite3_session
11176**
11177** Delete a session object previously allocated using
11178** [sqlite3session_create()]. Once a session object has been deleted, the
11179** results of attempting to use pSession with any other session module
11180** function are undefined.
11181**
11182** Session objects must be deleted before the database handle to which they
11183** are attached is closed. Refer to the documentation for
11184** [sqlite3session_create()] for details.
11185*/
11186SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
11187
11188/*
11189** CAPI3REF: Configure a Session Object
11190** METHOD: sqlite3_session
11191**
11192** This method is used to configure a session object after it has been
11193** created. At present the only valid values for the second parameter are
11194** [SQLITE_SESSION_OBJCONFIG_SIZE] and [SQLITE_SESSION_OBJCONFIG_ROWID].
11195**
11196*/
11197SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
11198
11199/*
11200** CAPI3REF: Options for sqlite3session_object_config
11201**
11202** The following values may passed as the the 2nd parameter to
11203** sqlite3session_object_config().
11204**
11205** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
11206** This option is used to set, clear or query the flag that enables
11207** the [sqlite3session_changeset_size()] API. Because it imposes some
11208** computational overhead, this API is disabled by default. Argument
11209** pArg must point to a value of type (int). If the value is initially
11210** 0, then the sqlite3session_changeset_size() API is disabled. If it
11211** is greater than 0, then the same API is enabled. Or, if the initial
11212** value is less than zero, no change is made. In all cases the (int)
11213** variable is set to 1 if the sqlite3session_changeset_size() API is
11214** enabled following the current call, or 0 otherwise.
11215**
11216** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
11217** the first table has been attached to the session object.
11218**
11219** <dt>SQLITE_SESSION_OBJCONFIG_ROWID <dd>
11220** This option is used to set, clear or query the flag that enables
11221** collection of data for tables with no explicit PRIMARY KEY.
11222**
11223** Normally, tables with no explicit PRIMARY KEY are simply ignored
11224** by the sessions module. However, if this flag is set, it behaves
11225** as if such tables have a column "_rowid_ INTEGER PRIMARY KEY" inserted
11226** as their leftmost columns.
11227**
11228** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
11229** the first table has been attached to the session object.
11230*/
11231#define SQLITE_SESSION_OBJCONFIG_SIZE 1
11232#define SQLITE_SESSION_OBJCONFIG_ROWID 2
11233
11234/*
11235** CAPI3REF: Enable Or Disable A Session Object
11236** METHOD: sqlite3_session
11237**
11238** Enable or disable the recording of changes by a session object. When
11239** enabled, a session object records changes made to the database. When
11240** disabled - it does not. A newly created session object is enabled.
11241** Refer to the documentation for [sqlite3session_changeset()] for further
11242** details regarding how enabling and disabling a session object affects
11243** the eventual changesets.
11244**
11245** Passing zero to this function disables the session. Passing a value
11246** greater than zero enables it. Passing a value less than zero is a
11247** no-op, and may be used to query the current state of the session.
11248**
11249** The return value indicates the final state of the session object: 0 if
11250** the session is disabled, or 1 if it is enabled.
11251*/
11252SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
11253
11254/*
11255** CAPI3REF: Set Or Clear the Indirect Change Flag
11256** METHOD: sqlite3_session
11257**
11258** Each change recorded by a session object is marked as either direct or
11259** indirect. A change is marked as indirect if either:
11260**
11261** <ul>
11262** <li> The session object "indirect" flag is set when the change is
11263** made, or
11264** <li> The change is made by an SQL trigger or foreign key action
11265** instead of directly as a result of a users SQL statement.
11266** </ul>
11267**
11268** If a single row is affected by more than one operation within a session,
11269** then the change is considered indirect if all operations meet the criteria
11270** for an indirect change above, or direct otherwise.
11271**
11272** This function is used to set, clear or query the session object indirect
11273** flag. If the second argument passed to this function is zero, then the
11274** indirect flag is cleared. If it is greater than zero, the indirect flag
11275** is set. Passing a value less than zero does not modify the current value
11276** of the indirect flag, and may be used to query the current state of the
11277** indirect flag for the specified session object.
11278**
11279** The return value indicates the final state of the indirect flag: 0 if
11280** it is clear, or 1 if it is set.
11281*/
11282SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11283
11284/*
11285** CAPI3REF: Attach A Table To A Session Object
11286** METHOD: sqlite3_session
11287**
11288** If argument zTab is not NULL, then it is the name of a table to attach
11289** to the session object passed as the first argument. All subsequent changes
11290** made to the table while the session object is enabled will be recorded. See
11291** documentation for [sqlite3session_changeset()] for further details.
11292**
11293** Or, if argument zTab is NULL, then changes are recorded for all tables
11294** in the database. If additional tables are added to the database (by
11295** executing "CREATE TABLE" statements) after this call is made, changes for
11296** the new tables are also recorded.
11297**
11298** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
11299** defined as part of their CREATE TABLE statement. It does not matter if the
11300** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
11301** KEY may consist of a single column, or may be a composite key.
11302**
11303** It is not an error if the named table does not exist in the database. Nor
11304** is it an error if the named table does not have a PRIMARY KEY. However,
11305** no changes will be recorded in either of these scenarios.
11306**
11307** Changes are not recorded for individual rows that have NULL values stored
11308** in one or more of their PRIMARY KEY columns.
11309**
11310** SQLITE_OK is returned if the call completes without error. Or, if an error
11311** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
11312**
11313** <h3>Special sqlite_stat1 Handling</h3>
11314**
11315** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
11316** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
11317** <pre>
11318** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
11319** </pre>
11320**
11321** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
11322** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
11323** are recorded for rows for which (idx IS NULL) is true. However, for such
11324** rows a zero-length blob (SQL value X'') is stored in the changeset or
11325** patchset instead of a NULL value. This allows such changesets to be
11326** manipulated by legacy implementations of sqlite3changeset_invert(),
11327** concat() and similar.
11328**
11329** The sqlite3changeset_apply() function automatically converts the
11330** zero-length blob back to a NULL value when updating the sqlite_stat1
11331** table. However, if the application calls sqlite3changeset_new(),
11332** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
11333** iterator directly (including on a changeset iterator passed to a
11334** conflict-handler callback) then the X'' value is returned. The application
11335** must translate X'' to NULL itself if required.
11336**
11337** Legacy (older than 3.22.0) versions of the sessions module cannot capture
11338** changes made to the sqlite_stat1 table. Legacy versions of the
11339** sqlite3changeset_apply() function silently ignore any modifications to the
11340** sqlite_stat1 table that are part of a changeset or patchset.
11341*/
11342SQLITE_API int sqlite3session_attach(
11343 sqlite3_session *pSession, /* Session object */
11344 const char *zTab /* Table name */
11345);
11346
11347/*
11348** CAPI3REF: Set a table filter on a Session Object.
11349** METHOD: sqlite3_session
11350**
11351** The second argument (xFilter) is the "filter callback". For changes to rows
11352** in tables that are not attached to the Session object, the filter is called
11353** to determine whether changes to the table's rows should be tracked or not.
11354** If xFilter returns 0, changes are not tracked. Note that once a table is
11355** attached, xFilter will not be called again.
11356*/
11357SQLITE_API void sqlite3session_table_filter(
11358 sqlite3_session *pSession, /* Session object */
11359 int(*xFilter)(
11360 void *pCtx, /* Copy of third arg to _filter_table() */
11361 const char *zTab /* Table name */
11362 ),
11363 void *pCtx /* First argument passed to xFilter */
11364);
11365
11366/*
11367** CAPI3REF: Generate A Changeset From A Session Object
11368** METHOD: sqlite3_session
11369**
11370** Obtain a changeset containing changes to the tables attached to the
11371** session object passed as the first argument. If successful,
11372** set *ppChangeset to point to a buffer containing the changeset
11373** and *pnChangeset to the size of the changeset in bytes before returning
11374** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
11375** zero and return an SQLite error code.
11376**
11377** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
11378** each representing a change to a single row of an attached table. An INSERT
11379** change contains the values of each field of a new database row. A DELETE
11380** contains the original values of each field of a deleted database row. An
11381** UPDATE change contains the original values of each field of an updated
11382** database row along with the updated values for each updated non-primary-key
11383** column. It is not possible for an UPDATE change to represent a change that
11384** modifies the values of primary key columns. If such a change is made, it
11385** is represented in a changeset as a DELETE followed by an INSERT.
11386**
11387** Changes are not recorded for rows that have NULL values stored in one or
11388** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
11389** no corresponding change is present in the changesets returned by this
11390** function. If an existing row with one or more NULL values stored in
11391** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11392** only an INSERT is appears in the changeset. Similarly, if an existing row
11393** with non-NULL PRIMARY KEY values is updated so that one or more of its
11394** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
11395** DELETE change only.
11396**
11397** The contents of a changeset may be traversed using an iterator created
11398** using the [sqlite3changeset_start()] API. A changeset may be applied to
11399** a database with a compatible schema using the [sqlite3changeset_apply()]
11400** API.
11401**
11402** Within a changeset generated by this function, all changes related to a
11403** single table are grouped together. In other words, when iterating through
11404** a changeset or when applying a changeset to a database, all changes related
11405** to a single table are processed before moving on to the next table. Tables
11406** are sorted in the same order in which they were attached (or auto-attached)
11407** to the sqlite3_session object. The order in which the changes related to
11408** a single table are stored is undefined.
11409**
11410** Following a successful call to this function, it is the responsibility of
11411** the caller to eventually free the buffer that *ppChangeset points to using
11412** [sqlite3_free()].
11413**
11414** <h3>Changeset Generation</h3>
11415**
11416** Once a table has been attached to a session object, the session object
11417** records the primary key values of all new rows inserted into the table.
11418** It also records the original primary key and other column values of any
11419** deleted or updated rows. For each unique primary key value, data is only
11420** recorded once - the first time a row with said primary key is inserted,
11421** updated or deleted in the lifetime of the session.
11422**
11423** There is one exception to the previous paragraph: when a row is inserted,
11424** updated or deleted, if one or more of its primary key columns contain a
11425** NULL value, no record of the change is made.
11426**
11427** The session object therefore accumulates two types of records - those
11428** that consist of primary key values only (created when the user inserts
11429** a new record) and those that consist of the primary key values and the
11430** original values of other table columns (created when the users deletes
11431** or updates a record).
11432**
11433** When this function is called, the requested changeset is created using
11434** both the accumulated records and the current contents of the database
11435** file. Specifically:
11436**
11437** <ul>
11438** <li> For each record generated by an insert, the database is queried
11439** for a row with a matching primary key. If one is found, an INSERT
11440** change is added to the changeset. If no such row is found, no change
11441** is added to the changeset.
11442**
11443** <li> For each record generated by an update or delete, the database is
11444** queried for a row with a matching primary key. If such a row is
11445** found and one or more of the non-primary key fields have been
11446** modified from their original values, an UPDATE change is added to
11447** the changeset. Or, if no such row is found in the table, a DELETE
11448** change is added to the changeset. If there is a row with a matching
11449** primary key in the database, but all fields contain their original
11450** values, no change is added to the changeset.
11451** </ul>
11452**
11453** This means, amongst other things, that if a row is inserted and then later
11454** deleted while a session object is active, neither the insert nor the delete
11455** will be present in the changeset. Or if a row is deleted and then later a
11456** row with the same primary key values inserted while a session object is
11457** active, the resulting changeset will contain an UPDATE change instead of
11458** a DELETE and an INSERT.
11459**
11460** When a session object is disabled (see the [sqlite3session_enable()] API),
11461** it does not accumulate records when rows are inserted, updated or deleted.
11462** This may appear to have some counter-intuitive effects if a single row
11463** is written to more than once during a session. For example, if a row
11464** is inserted while a session object is enabled, then later deleted while
11465** the same session object is disabled, no INSERT record will appear in the
11466** changeset, even though the delete took place while the session was disabled.
11467** Or, if one field of a row is updated while a session is disabled, and
11468** another field of the same row is updated while the session is enabled, the
11469** resulting changeset will contain an UPDATE change that updates both fields.
11470*/
11471SQLITE_API int sqlite3session_changeset(
11472 sqlite3_session *pSession, /* Session object */
11473 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
11474 void **ppChangeset /* OUT: Buffer containing changeset */
11475);
11476
11477/*
11478** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11479** METHOD: sqlite3_session
11480**
11481** By default, this function always returns 0. For it to return
11482** a useful result, the sqlite3_session object must have been configured
11483** to enable this API using sqlite3session_object_config() with the
11484** SQLITE_SESSION_OBJCONFIG_SIZE verb.
11485**
11486** When enabled, this function returns an upper limit, in bytes, for the size
11487** of the changeset that might be produced if sqlite3session_changeset() were
11488** called. The final changeset size might be equal to or smaller than the
11489** size in bytes returned by this function.
11490*/
11491SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
11492
11493/*
11494** CAPI3REF: Load The Difference Between Tables Into A Session
11495** METHOD: sqlite3_session
11496**
11497** If it is not already attached to the session object passed as the first
11498** argument, this function attaches table zTbl in the same manner as the
11499** [sqlite3session_attach()] function. If zTbl does not exist, or if it
11500** does not have a primary key, this function is a no-op (but does not return
11501** an error).
11502**
11503** Argument zFromDb must be the name of a database ("main", "temp" etc.)
11504** attached to the same database handle as the session object that contains
11505** a table compatible with the table attached to the session by this function.
11506** A table is considered compatible if it:
11507**
11508** <ul>
11509** <li> Has the same name,
11510** <li> Has the same set of columns declared in the same order, and
11511** <li> Has the same PRIMARY KEY definition.
11512** </ul>
11513**
11514** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
11515** are compatible but do not have any PRIMARY KEY columns, it is not an error
11516** but no changes are added to the session object. As with other session
11517** APIs, tables without PRIMARY KEYs are simply ignored.
11518**
11519** This function adds a set of changes to the session object that could be
11520** used to update the table in database zFrom (call this the "from-table")
11521** so that its content is the same as the table attached to the session
11522** object (call this the "to-table"). Specifically:
11523**
11524** <ul>
11525** <li> For each row (primary key) that exists in the to-table but not in
11526** the from-table, an INSERT record is added to the session object.
11527**
11528** <li> For each row (primary key) that exists in the to-table but not in
11529** the from-table, a DELETE record is added to the session object.
11530**
11531** <li> For each row (primary key) that exists in both tables, but features
11532** different non-PK values in each, an UPDATE record is added to the
11533** session.
11534** </ul>
11535**
11536** To clarify, if this function is called and then a changeset constructed
11537** using [sqlite3session_changeset()], then after applying that changeset to
11538** database zFrom the contents of the two compatible tables would be
11539** identical.
11540**
11541** It an error if database zFrom does not exist or does not contain the
11542** required compatible table.
11543**
11544** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11545** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
11546** may be set to point to a buffer containing an English language error
11547** message. It is the responsibility of the caller to free this buffer using
11548** sqlite3_free().
11549*/
11550SQLITE_API int sqlite3session_diff(
11551 sqlite3_session *pSession,
11552 const char *zFromDb,
11553 const char *zTbl,
11554 char **pzErrMsg
11555);
11556
11557
11558/*
11559** CAPI3REF: Generate A Patchset From A Session Object
11560** METHOD: sqlite3_session
11561**
11562** The differences between a patchset and a changeset are that:
11563**
11564** <ul>
11565** <li> DELETE records consist of the primary key fields only. The
11566** original values of other fields are omitted.
11567** <li> The original values of any modified fields are omitted from
11568** UPDATE records.
11569** </ul>
11570**
11571** A patchset blob may be used with up to date versions of all
11572** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11573** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11574** attempting to use a patchset blob with old versions of the
11575** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11576**
11577** Because the non-primary key "old.*" fields are omitted, no
11578** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11579** is passed to the sqlite3changeset_apply() API. Other conflict types work
11580** in the same way as for changesets.
11581**
11582** Changes within a patchset are ordered in the same way as for changesets
11583** generated by the sqlite3session_changeset() function (i.e. all changes for
11584** a single table are grouped together, tables appear in the order in which
11585** they were attached to the session object).
11586*/
11587SQLITE_API int sqlite3session_patchset(
11588 sqlite3_session *pSession, /* Session object */
11589 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
11590 void **ppPatchset /* OUT: Buffer containing patchset */
11591);
11592
11593/*
11594** CAPI3REF: Test if a changeset has recorded any changes.
11595**
11596** Return non-zero if no changes to attached tables have been recorded by
11597** the session object passed as the first argument. Otherwise, if one or
11598** more changes have been recorded, return zero.
11599**
11600** Even if this function returns zero, it is possible that calling
11601** [sqlite3session_changeset()] on the session handle may still return a
11602** changeset that contains no changes. This can happen when a row in
11603** an attached table is modified and then later on the original values
11604** are restored. However, if this function returns non-zero, then it is
11605** guaranteed that a call to sqlite3session_changeset() will return a
11606** changeset containing zero changes.
11607*/
11608SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11609
11610/*
11611** CAPI3REF: Query for the amount of heap memory used by a session object.
11612**
11613** This API returns the total amount of heap memory in bytes currently
11614** used by the session object passed as the only argument.
11615*/
11616SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
11617
11618/*
11619** CAPI3REF: Create An Iterator To Traverse A Changeset
11620** CONSTRUCTOR: sqlite3_changeset_iter
11621**
11622** Create an iterator used to iterate through the contents of a changeset.
11623** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11624** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11625** SQLite error code is returned.
11626**
11627** The following functions can be used to advance and query a changeset
11628** iterator created by this function:
11629**
11630** <ul>
11631** <li> [sqlite3changeset_next()]
11632** <li> [sqlite3changeset_op()]
11633** <li> [sqlite3changeset_new()]
11634** <li> [sqlite3changeset_old()]
11635** </ul>
11636**
11637** It is the responsibility of the caller to eventually destroy the iterator
11638** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11639** changeset (pChangeset) must remain valid until after the iterator is
11640** destroyed.
11641**
11642** Assuming the changeset blob was created by one of the
11643** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11644** [sqlite3changeset_invert()] functions, all changes within the changeset
11645** that apply to a single table are grouped together. This means that when
11646** an application iterates through a changeset using an iterator created by
11647** this function, all changes that relate to a single table are visited
11648** consecutively. There is no chance that the iterator will visit a change
11649** the applies to table X, then one for table Y, and then later on visit
11650** another change for table X.
11651**
11652** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11653** may be modified by passing a combination of
11654** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11655**
11656** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11657** and therefore subject to change.
11658*/
11659SQLITE_API int sqlite3changeset_start(
11660 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11661 int nChangeset, /* Size of changeset blob in bytes */
11662 void *pChangeset /* Pointer to blob containing changeset */
11663);
11664SQLITE_API int sqlite3changeset_start_v2(
11665 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11666 int nChangeset, /* Size of changeset blob in bytes */
11667 void *pChangeset, /* Pointer to blob containing changeset */
11668 int flags /* SESSION_CHANGESETSTART_* flags */
11669);
11670
11671/*
11672** CAPI3REF: Flags for sqlite3changeset_start_v2
11673**
11674** The following flags may passed via the 4th parameter to
11675** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11676**
11677** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11678** Invert the changeset while iterating through it. This is equivalent to
11679** inverting a changeset using sqlite3changeset_invert() before applying it.
11680** It is an error to specify this flag with a patchset.
11681*/
11682#define SQLITE_CHANGESETSTART_INVERT 0x0002
11683
11684
11685/*
11686** CAPI3REF: Advance A Changeset Iterator
11687** METHOD: sqlite3_changeset_iter
11688**
11689** This function may only be used with iterators created by the function
11690** [sqlite3changeset_start()]. If it is called on an iterator passed to
11691** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11692** is returned and the call has no effect.
11693**
11694** Immediately after an iterator is created by sqlite3changeset_start(), it
11695** does not point to any change in the changeset. Assuming the changeset
11696** is not empty, the first call to this function advances the iterator to
11697** point to the first change in the changeset. Each subsequent call advances
11698** the iterator to point to the next change in the changeset (if any). If
11699** no error occurs and the iterator points to a valid change after a call
11700** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11701** Otherwise, if all changes in the changeset have already been visited,
11702** SQLITE_DONE is returned.
11703**
11704** If an error occurs, an SQLite error code is returned. Possible error
11705** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11706** SQLITE_NOMEM.
11707*/
11708SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11709
11710/*
11711** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11712** METHOD: sqlite3_changeset_iter
11713**
11714** The pIter argument passed to this function may either be an iterator
11715** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11716** created by [sqlite3changeset_start()]. In the latter case, the most recent
11717** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11718** is not the case, this function returns [SQLITE_MISUSE].
11719**
11720** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
11721** outputs are set through these pointers:
11722**
11723** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
11724** depending on the type of change that the iterator currently points to;
11725**
11726** *pnCol is set to the number of columns in the table affected by the change; and
11727**
11728** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11729** the name of the table affected by the current change. The buffer remains
11730** valid until either sqlite3changeset_next() is called on the iterator
11731** or until the conflict-handler function returns.
11732**
11733** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11734** is an indirect change, or false (0) otherwise. See the documentation for
11735** [sqlite3session_indirect()] for a description of direct and indirect
11736** changes.
11737**
11738** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11739** SQLite error code is returned. The values of the output variables may not
11740** be trusted in this case.
11741*/
11742SQLITE_API int sqlite3changeset_op(
11743 sqlite3_changeset_iter *pIter, /* Iterator object */
11744 const char **pzTab, /* OUT: Pointer to table name */
11745 int *pnCol, /* OUT: Number of columns in table */
11746 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11747 int *pbIndirect /* OUT: True for an 'indirect' change */
11748);
11749
11750/*
11751** CAPI3REF: Obtain The Primary Key Definition Of A Table
11752** METHOD: sqlite3_changeset_iter
11753**
11754** For each modified table, a changeset includes the following:
11755**
11756** <ul>
11757** <li> The number of columns in the table, and
11758** <li> Which of those columns make up the tables PRIMARY KEY.
11759** </ul>
11760**
11761** This function is used to find which columns comprise the PRIMARY KEY of
11762** the table modified by the change that iterator pIter currently points to.
11763** If successful, *pabPK is set to point to an array of nCol entries, where
11764** nCol is the number of columns in the table. Elements of *pabPK are set to
11765** 0x01 if the corresponding column is part of the tables primary key, or
11766** 0x00 if it is not.
11767**
11768** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11769** in the table.
11770**
11771** If this function is called when the iterator does not point to a valid
11772** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11773** SQLITE_OK is returned and the output variables populated as described
11774** above.
11775*/
11776SQLITE_API int sqlite3changeset_pk(
11777 sqlite3_changeset_iter *pIter, /* Iterator object */
11778 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11779 int *pnCol /* OUT: Number of entries in output array */
11780);
11781
11782/*
11783** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11784** METHOD: sqlite3_changeset_iter
11785**
11786** The pIter argument passed to this function may either be an iterator
11787** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11788** created by [sqlite3changeset_start()]. In the latter case, the most recent
11789** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11790** Furthermore, it may only be called if the type of change that the iterator
11791** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11792** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11793**
11794** Argument iVal must be greater than or equal to 0, and less than the number
11795** of columns in the table affected by the current change. Otherwise,
11796** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11797**
11798** If successful, this function sets *ppValue to point to a protected
11799** sqlite3_value object containing the iVal'th value from the vector of
11800** original row values stored as part of the UPDATE or DELETE change and
11801** returns SQLITE_OK. The name of the function comes from the fact that this
11802** is similar to the "old.*" columns available to update or delete triggers.
11803**
11804** If some other error occurs (e.g. an OOM condition), an SQLite error code
11805** is returned and *ppValue is set to NULL.
11806*/
11807SQLITE_API int sqlite3changeset_old(
11808 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11809 int iVal, /* Column number */
11810 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11811);
11812
11813/*
11814** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11815** METHOD: sqlite3_changeset_iter
11816**
11817** The pIter argument passed to this function may either be an iterator
11818** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11819** created by [sqlite3changeset_start()]. In the latter case, the most recent
11820** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11821** Furthermore, it may only be called if the type of change that the iterator
11822** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11823** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11824**
11825** Argument iVal must be greater than or equal to 0, and less than the number
11826** of columns in the table affected by the current change. Otherwise,
11827** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11828**
11829** If successful, this function sets *ppValue to point to a protected
11830** sqlite3_value object containing the iVal'th value from the vector of
11831** new row values stored as part of the UPDATE or INSERT change and
11832** returns SQLITE_OK. If the change is an UPDATE and does not include
11833** a new value for the requested column, *ppValue is set to NULL and
11834** SQLITE_OK returned. The name of the function comes from the fact that
11835** this is similar to the "new.*" columns available to update or delete
11836** triggers.
11837**
11838** If some other error occurs (e.g. an OOM condition), an SQLite error code
11839** is returned and *ppValue is set to NULL.
11840*/
11841SQLITE_API int sqlite3changeset_new(
11842 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11843 int iVal, /* Column number */
11844 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11845);
11846
11847/*
11848** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11849** METHOD: sqlite3_changeset_iter
11850**
11851** This function should only be used with iterator objects passed to a
11852** conflict-handler callback by [sqlite3changeset_apply()] with either
11853** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11854** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11855** is set to NULL.
11856**
11857** Argument iVal must be greater than or equal to 0, and less than the number
11858** of columns in the table affected by the current change. Otherwise,
11859** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11860**
11861** If successful, this function sets *ppValue to point to a protected
11862** sqlite3_value object containing the iVal'th value from the
11863** "conflicting row" associated with the current conflict-handler callback
11864** and returns SQLITE_OK.
11865**
11866** If some other error occurs (e.g. an OOM condition), an SQLite error code
11867** is returned and *ppValue is set to NULL.
11868*/
11869SQLITE_API int sqlite3changeset_conflict(
11870 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11871 int iVal, /* Column number */
11872 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11873);
11874
11875/*
11876** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11877** METHOD: sqlite3_changeset_iter
11878**
11879** This function may only be called with an iterator passed to an
11880** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11881** it sets the output variable to the total number of known foreign key
11882** violations in the destination database and returns SQLITE_OK.
11883**
11884** In all other cases this function returns SQLITE_MISUSE.
11885*/
11886SQLITE_API int sqlite3changeset_fk_conflicts(
11887 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11888 int *pnOut /* OUT: Number of FK violations */
11889);
11890
11891
11892/*
11893** CAPI3REF: Finalize A Changeset Iterator
11894** METHOD: sqlite3_changeset_iter
11895**
11896** This function is used to finalize an iterator allocated with
11897** [sqlite3changeset_start()].
11898**
11899** This function should only be called on iterators created using the
11900** [sqlite3changeset_start()] function. If an application calls this
11901** function with an iterator passed to a conflict-handler by
11902** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11903** call has no effect.
11904**
11905** If an error was encountered within a call to an sqlite3changeset_xxx()
11906** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11907** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11908** to that error is returned by this function. Otherwise, SQLITE_OK is
11909** returned. This is to allow the following pattern (pseudo-code):
11910**
11911** <pre>
11912** sqlite3changeset_start();
11913** while( SQLITE_ROW==sqlite3changeset_next() ){
11914** // Do something with change.
11915** }
11916** rc = sqlite3changeset_finalize();
11917** if( rc!=SQLITE_OK ){
11918** // An error has occurred
11919** }
11920** </pre>
11921*/
11922SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11923
11924/*
11925** CAPI3REF: Invert A Changeset
11926**
11927** This function is used to "invert" a changeset object. Applying an inverted
11928** changeset to a database reverses the effects of applying the uninverted
11929** changeset. Specifically:
11930**
11931** <ul>
11932** <li> Each DELETE change is changed to an INSERT, and
11933** <li> Each INSERT change is changed to a DELETE, and
11934** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11935** </ul>
11936**
11937** This function does not change the order in which changes appear within
11938** the changeset. It merely reverses the sense of each individual change.
11939**
11940** If successful, a pointer to a buffer containing the inverted changeset
11941** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11942** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11943** zeroed and an SQLite error code returned.
11944**
11945** It is the responsibility of the caller to eventually call sqlite3_free()
11946** on the *ppOut pointer to free the buffer allocation following a successful
11947** call to this function.
11948**
11949** WARNING/TODO: This function currently assumes that the input is a valid
11950** changeset. If it is not, the results are undefined.
11951*/
11952SQLITE_API int sqlite3changeset_invert(
11953 int nIn, const void *pIn, /* Input changeset */
11954 int *pnOut, void **ppOut /* OUT: Inverse of input */
11955);
11956
11957/*
11958** CAPI3REF: Concatenate Two Changeset Objects
11959**
11960** This function is used to concatenate two changesets, A and B, into a
11961** single changeset. The result is a changeset equivalent to applying
11962** changeset A followed by changeset B.
11963**
11964** This function combines the two input changesets using an
11965** sqlite3_changegroup object. Calling it produces similar results as the
11966** following code fragment:
11967**
11968** <pre>
11969** sqlite3_changegroup *pGrp;
11970** rc = sqlite3_changegroup_new(&pGrp);
11971** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11972** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11973** if( rc==SQLITE_OK ){
11974** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11975** }else{
11976** *ppOut = 0;
11977** *pnOut = 0;
11978** }
11979** </pre>
11980**
11981** Refer to the sqlite3_changegroup documentation below for details.
11982*/
11983SQLITE_API int sqlite3changeset_concat(
11984 int nA, /* Number of bytes in buffer pA */
11985 void *pA, /* Pointer to buffer containing changeset A */
11986 int nB, /* Number of bytes in buffer pB */
11987 void *pB, /* Pointer to buffer containing changeset B */
11988 int *pnOut, /* OUT: Number of bytes in output changeset */
11989 void **ppOut /* OUT: Buffer containing output changeset */
11990);
11991
11992
11993/*
11994** CAPI3REF: Changegroup Handle
11995**
11996** A changegroup is an object used to combine two or more
11997** [changesets] or [patchsets]
11998*/
11999typedef struct sqlite3_changegroup sqlite3_changegroup;
12000
12001/*
12002** CAPI3REF: Create A New Changegroup Object
12003** CONSTRUCTOR: sqlite3_changegroup
12004**
12005** An sqlite3_changegroup object is used to combine two or more changesets
12006** (or patchsets) into a single changeset (or patchset). A single changegroup
12007** object may combine changesets or patchsets, but not both. The output is
12008** always in the same format as the input.
12009**
12010** If successful, this function returns SQLITE_OK and populates (*pp) with
12011** a pointer to a new sqlite3_changegroup object before returning. The caller
12012** should eventually free the returned object using a call to
12013** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
12014** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
12015**
12016** The usual usage pattern for an sqlite3_changegroup object is as follows:
12017**
12018** <ul>
12019** <li> It is created using a call to sqlite3changegroup_new().
12020**
12021** <li> Zero or more changesets (or patchsets) are added to the object
12022** by calling sqlite3changegroup_add().
12023**
12024** <li> The result of combining all input changesets together is obtained
12025** by the application via a call to sqlite3changegroup_output().
12026**
12027** <li> The object is deleted using a call to sqlite3changegroup_delete().
12028** </ul>
12029**
12030** Any number of calls to add() and output() may be made between the calls to
12031** new() and delete(), and in any order.
12032**
12033** As well as the regular sqlite3changegroup_add() and
12034** sqlite3changegroup_output() functions, also available are the streaming
12035** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
12036*/
12037SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
12038
12039/*
12040** CAPI3REF: Add A Changeset To A Changegroup
12041** METHOD: sqlite3_changegroup
12042**
12043** Add all changes within the changeset (or patchset) in buffer pData (size
12044** nData bytes) to the changegroup.
12045**
12046** If the buffer contains a patchset, then all prior calls to this function
12047** on the same changegroup object must also have specified patchsets. Or, if
12048** the buffer contains a changeset, so must have the earlier calls to this
12049** function. Otherwise, SQLITE_ERROR is returned and no changes are added
12050** to the changegroup.
12051**
12052** Rows within the changeset and changegroup are identified by the values in
12053** their PRIMARY KEY columns. A change in the changeset is considered to
12054** apply to the same row as a change already present in the changegroup if
12055** the two rows have the same primary key.
12056**
12057** Changes to rows that do not already appear in the changegroup are
12058** simply copied into it. Or, if both the new changeset and the changegroup
12059** contain changes that apply to a single row, the final contents of the
12060** changegroup depends on the type of each change, as follows:
12061**
12062** <table border=1 style="margin-left:8ex;margin-right:8ex">
12063** <tr><th style="white-space:pre">Existing Change </th>
12064** <th style="white-space:pre">New Change </th>
12065** <th>Output Change
12066** <tr><td>INSERT <td>INSERT <td>
12067** The new change is ignored. This case does not occur if the new
12068** changeset was recorded immediately after the changesets already
12069** added to the changegroup.
12070** <tr><td>INSERT <td>UPDATE <td>
12071** The INSERT change remains in the changegroup. The values in the
12072** INSERT change are modified as if the row was inserted by the
12073** existing change and then updated according to the new change.
12074** <tr><td>INSERT <td>DELETE <td>
12075** The existing INSERT is removed from the changegroup. The DELETE is
12076** not added.
12077** <tr><td>UPDATE <td>INSERT <td>
12078** The new change is ignored. This case does not occur if the new
12079** changeset was recorded immediately after the changesets already
12080** added to the changegroup.
12081** <tr><td>UPDATE <td>UPDATE <td>
12082** The existing UPDATE remains within the changegroup. It is amended
12083** so that the accompanying values are as if the row was updated once
12084** by the existing change and then again by the new change.
12085** <tr><td>UPDATE <td>DELETE <td>
12086** The existing UPDATE is replaced by the new DELETE within the
12087** changegroup.
12088** <tr><td>DELETE <td>INSERT <td>
12089** If one or more of the column values in the row inserted by the
12090** new change differ from those in the row deleted by the existing
12091** change, the existing DELETE is replaced by an UPDATE within the
12092** changegroup. Otherwise, if the inserted row is exactly the same
12093** as the deleted row, the existing DELETE is simply discarded.
12094** <tr><td>DELETE <td>UPDATE <td>
12095** The new change is ignored. This case does not occur if the new
12096** changeset was recorded immediately after the changesets already
12097** added to the changegroup.
12098** <tr><td>DELETE <td>DELETE <td>
12099** The new change is ignored. This case does not occur if the new
12100** changeset was recorded immediately after the changesets already
12101** added to the changegroup.
12102** </table>
12103**
12104** If the new changeset contains changes to a table that is already present
12105** in the changegroup, then the number of columns and the position of the
12106** primary key columns for the table must be consistent. If this is not the
12107** case, this function fails with SQLITE_SCHEMA. If the input changeset
12108** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
12109** returned. Or, if an out-of-memory condition occurs during processing, this
12110** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
12111** of the final contents of the changegroup is undefined.
12112**
12113** If no error occurs, SQLITE_OK is returned.
12114*/
12115SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12116
12117/*
12118** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12119** METHOD: sqlite3_changegroup
12120**
12121** Obtain a buffer containing a changeset (or patchset) representing the
12122** current contents of the changegroup. If the inputs to the changegroup
12123** were themselves changesets, the output is a changeset. Or, if the
12124** inputs were patchsets, the output is also a patchset.
12125**
12126** As with the output of the sqlite3session_changeset() and
12127** sqlite3session_patchset() functions, all changes related to a single
12128** table are grouped together in the output of this function. Tables appear
12129** in the same order as for the very first changeset added to the changegroup.
12130** If the second or subsequent changesets added to the changegroup contain
12131** changes for tables that do not appear in the first changeset, they are
12132** appended onto the end of the output changeset, again in the order in
12133** which they are first encountered.
12134**
12135** If an error occurs, an SQLite error code is returned and the output
12136** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
12137** is returned and the output variables are set to the size of and a
12138** pointer to the output buffer, respectively. In this case it is the
12139** responsibility of the caller to eventually free the buffer using a
12140** call to sqlite3_free().
12141*/
12142SQLITE_API int sqlite3changegroup_output(
12143 sqlite3_changegroup*,
12144 int *pnData, /* OUT: Size of output buffer in bytes */
12145 void **ppData /* OUT: Pointer to output buffer */
12146);
12147
12148/*
12149** CAPI3REF: Delete A Changegroup Object
12150** DESTRUCTOR: sqlite3_changegroup
12151*/
12152SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
12153
12154/*
12155** CAPI3REF: Apply A Changeset To A Database
12156**
12157** Apply a changeset or patchset to a database. These functions attempt to
12158** update the "main" database attached to handle db with the changes found in
12159** the changeset passed via the second and third arguments.
12160**
12161** The fourth argument (xFilter) passed to these functions is the "filter
12162** callback". If it is not NULL, then for each table affected by at least one
12163** change in the changeset, the filter callback is invoked with
12164** the table name as the second argument, and a copy of the context pointer
12165** passed as the sixth argument as the first. If the "filter callback"
12166** returns zero, then no attempt is made to apply any changes to the table.
12167** Otherwise, if the return value is non-zero or the xFilter argument to
12168** is NULL, all changes related to the table are attempted.
12169**
12170** For each table that is not excluded by the filter callback, this function
12171** tests that the target database contains a compatible table. A table is
12172** considered compatible if all of the following are true:
12173**
12174** <ul>
12175** <li> The table has the same name as the name recorded in the
12176** changeset, and
12177** <li> The table has at least as many columns as recorded in the
12178** changeset, and
12179** <li> The table has primary key columns in the same position as
12180** recorded in the changeset.
12181** </ul>
12182**
12183** If there is no compatible table, it is not an error, but none of the
12184** changes associated with the table are applied. A warning message is issued
12185** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
12186** one such warning is issued for each table in the changeset.
12187**
12188** For each change for which there is a compatible table, an attempt is made
12189** to modify the table contents according to the UPDATE, INSERT or DELETE
12190** change. If a change cannot be applied cleanly, the conflict handler
12191** function passed as the fifth argument to sqlite3changeset_apply() may be
12192** invoked. A description of exactly when the conflict handler is invoked for
12193** each type of change is below.
12194**
12195** Unlike the xFilter argument, xConflict may not be passed NULL. The results
12196** of passing anything other than a valid function pointer as the xConflict
12197** argument are undefined.
12198**
12199** Each time the conflict handler function is invoked, it must return one
12200** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
12201** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
12202** if the second argument passed to the conflict handler is either
12203** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12204** returns an illegal value, any changes already made are rolled back and
12205** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
12206** actions are taken by sqlite3changeset_apply() depending on the value
12207** returned by each invocation of the conflict-handler function. Refer to
12208** the documentation for the three
12209** [SQLITE_CHANGESET_OMIT|available return values] for details.
12210**
12211** <dl>
12212** <dt>DELETE Changes<dd>
12213** For each DELETE change, the function checks if the target database
12214** contains a row with the same primary key value (or values) as the
12215** original row values stored in the changeset. If it does, and the values
12216** stored in all non-primary key columns also match the values stored in
12217** the changeset the row is deleted from the target database.
12218**
12219** If a row with matching primary key values is found, but one or more of
12220** the non-primary key fields contains a value different from the original
12221** row value stored in the changeset, the conflict-handler function is
12222** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
12223** database table has more columns than are recorded in the changeset,
12224** only the values of those non-primary key fields are compared against
12225** the current database contents - any trailing database table columns
12226** are ignored.
12227**
12228** If no row with matching primary key values is found in the database,
12229** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12230** passed as the second argument.
12231**
12232** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
12233** (which can only happen if a foreign key constraint is violated), the
12234** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12235** passed as the second argument. This includes the case where the DELETE
12236** operation is attempted because an earlier call to the conflict handler
12237** function returned [SQLITE_CHANGESET_REPLACE].
12238**
12239** <dt>INSERT Changes<dd>
12240** For each INSERT change, an attempt is made to insert the new row into
12241** the database. If the changeset row contains fewer fields than the
12242** database table, the trailing fields are populated with their default
12243** values.
12244**
12245** If the attempt to insert the row fails because the database already
12246** contains a row with the same primary key values, the conflict handler
12247** function is invoked with the second argument set to
12248** [SQLITE_CHANGESET_CONFLICT].
12249**
12250** If the attempt to insert the row fails because of some other constraint
12251** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
12252** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
12253** This includes the case where the INSERT operation is re-attempted because
12254** an earlier call to the conflict handler function returned
12255** [SQLITE_CHANGESET_REPLACE].
12256**
12257** <dt>UPDATE Changes<dd>
12258** For each UPDATE change, the function checks if the target database
12259** contains a row with the same primary key value (or values) as the
12260** original row values stored in the changeset. If it does, and the values
12261** stored in all modified non-primary key columns also match the values
12262** stored in the changeset the row is updated within the target database.
12263**
12264** If a row with matching primary key values is found, but one or more of
12265** the modified non-primary key fields contains a value different from an
12266** original row value stored in the changeset, the conflict-handler function
12267** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
12268** UPDATE changes only contain values for non-primary key fields that are
12269** to be modified, only those fields need to match the original values to
12270** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12271**
12272** If no row with matching primary key values is found in the database,
12273** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12274** passed as the second argument.
12275**
12276** If the UPDATE operation is attempted, but SQLite returns
12277** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12278** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
12279** This includes the case where the UPDATE operation is attempted after
12280** an earlier call to the conflict handler function returned
12281** [SQLITE_CHANGESET_REPLACE].
12282** </dl>
12283**
12284** It is safe to execute SQL statements, including those that write to the
12285** table that the callback related to, from within the xConflict callback.
12286** This can be used to further customize the application's conflict
12287** resolution strategy.
12288**
12289** All changes made by these functions are enclosed in a savepoint transaction.
12290** If any other error (aside from a constraint failure when attempting to
12291** write to the target database) occurs, then the savepoint transaction is
12292** rolled back, restoring the target database to its original state, and an
12293** SQLite error code returned.
12294**
12295** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12296** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
12297** may set (*ppRebase) to point to a "rebase" that may be used with the
12298** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
12299** is set to the size of the buffer in bytes. It is the responsibility of the
12300** caller to eventually free any such buffer using sqlite3_free(). The buffer
12301** is only allocated and populated if one or more conflicts were encountered
12302** while applying the patchset. See comments surrounding the sqlite3_rebaser
12303** APIs for further details.
12304**
12305** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
12306** may be modified by passing a combination of
12307** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
12308**
12309** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
12310** and therefore subject to change.
12311*/
12312SQLITE_API int sqlite3changeset_apply(
12313 sqlite3 *db, /* Apply change to "main" db of this handle */
12314 int nChangeset, /* Size of changeset in bytes */
12315 void *pChangeset, /* Changeset blob */
12316 int(*xFilter)(
12317 void *pCtx, /* Copy of sixth arg to _apply() */
12318 const char *zTab /* Table name */
12319 ),
12320 int(*xConflict)(
12321 void *pCtx, /* Copy of sixth arg to _apply() */
12322 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12323 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12324 ),
12325 void *pCtx /* First argument passed to xConflict */
12326);
12327SQLITE_API int sqlite3changeset_apply_v2(
12328 sqlite3 *db, /* Apply change to "main" db of this handle */
12329 int nChangeset, /* Size of changeset in bytes */
12330 void *pChangeset, /* Changeset blob */
12331 int(*xFilter)(
12332 void *pCtx, /* Copy of sixth arg to _apply() */
12333 const char *zTab /* Table name */
12334 ),
12335 int(*xConflict)(
12336 void *pCtx, /* Copy of sixth arg to _apply() */
12337 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12338 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12339 ),
12340 void *pCtx, /* First argument passed to xConflict */
12341 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12342 int flags /* SESSION_CHANGESETAPPLY_* flags */
12343);
12344
12345/*
12346** CAPI3REF: Flags for sqlite3changeset_apply_v2
12347**
12348** The following flags may passed via the 9th parameter to
12349** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
12350**
12351** <dl>
12352** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
12353** Usually, the sessions module encloses all operations performed by
12354** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
12355** SAVEPOINT is committed if the changeset or patchset is successfully
12356** applied, or rolled back if an error occurs. Specifying this flag
12357** causes the sessions module to omit this savepoint. In this case, if the
12358** caller has an open transaction or savepoint when apply_v2() is called,
12359** it may revert the partially applied changeset by rolling it back.
12360**
12361** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12362** Invert the changeset before applying it. This is equivalent to inverting
12363** a changeset using sqlite3changeset_invert() before applying it. It is
12364** an error to specify this flag with a patchset.
12365**
12366** <dt>SQLITE_CHANGESETAPPLY_IGNORENOOP <dd>
12367** Do not invoke the conflict handler callback for any changes that
12368** would not actually modify the database even if they were applied.
12369** Specifically, this means that the conflict handler is not invoked
12370** for:
12371** <ul>
12372** <li>a delete change if the row being deleted cannot be found,
12373** <li>an update change if the modified fields are already set to
12374** their new values in the conflicting row, or
12375** <li>an insert change if all fields of the conflicting row match
12376** the row being inserted.
12377** </ul>
12378*/
12379#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
12380#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
12381#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004
12382
12383/*
12384** CAPI3REF: Constants Passed To The Conflict Handler
12385**
12386** Values that may be passed as the second argument to a conflict-handler.
12387**
12388** <dl>
12389** <dt>SQLITE_CHANGESET_DATA<dd>
12390** The conflict handler is invoked with CHANGESET_DATA as the second argument
12391** when processing a DELETE or UPDATE change if a row with the required
12392** PRIMARY KEY fields is present in the database, but one or more other
12393** (non primary-key) fields modified by the update do not contain the
12394** expected "before" values.
12395**
12396** The conflicting row, in this case, is the database row with the matching
12397** primary key.
12398**
12399** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12400** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12401** argument when processing a DELETE or UPDATE change if a row with the
12402** required PRIMARY KEY fields is not present in the database.
12403**
12404** There is no conflicting row in this case. The results of invoking the
12405** sqlite3changeset_conflict() API are undefined.
12406**
12407** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12408** CHANGESET_CONFLICT is passed as the second argument to the conflict
12409** handler while processing an INSERT change if the operation would result
12410** in duplicate primary key values.
12411**
12412** The conflicting row in this case is the database row with the matching
12413** primary key.
12414**
12415** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
12416** If foreign key handling is enabled, and applying a changeset leaves the
12417** database in a state containing foreign key violations, the conflict
12418** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
12419** exactly once before the changeset is committed. If the conflict handler
12420** returns CHANGESET_OMIT, the changes, including those that caused the
12421** foreign key constraint violation, are committed. Or, if it returns
12422** CHANGESET_ABORT, the changeset is rolled back.
12423**
12424** No current or conflicting row information is provided. The only function
12425** it is possible to call on the supplied sqlite3_changeset_iter handle
12426** is sqlite3changeset_fk_conflicts().
12427**
12428** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12429** If any other constraint violation occurs while applying a change (i.e.
12430** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
12431** invoked with CHANGESET_CONSTRAINT as the second argument.
12432**
12433** There is no conflicting row in this case. The results of invoking the
12434** sqlite3changeset_conflict() API are undefined.
12435**
12436** </dl>
12437*/
12438#define SQLITE_CHANGESET_DATA 1
12439#define SQLITE_CHANGESET_NOTFOUND 2
12440#define SQLITE_CHANGESET_CONFLICT 3
12441#define SQLITE_CHANGESET_CONSTRAINT 4
12442#define SQLITE_CHANGESET_FOREIGN_KEY 5
12443
12444/*
12445** CAPI3REF: Constants Returned By The Conflict Handler
12446**
12447** A conflict handler callback must return one of the following three values.
12448**
12449** <dl>
12450** <dt>SQLITE_CHANGESET_OMIT<dd>
12451** If a conflict handler returns this value no special action is taken. The
12452** change that caused the conflict is not applied. The session module
12453** continues to the next change in the changeset.
12454**
12455** <dt>SQLITE_CHANGESET_REPLACE<dd>
12456** This value may only be returned if the second argument to the conflict
12457** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12458** is not the case, any changes applied so far are rolled back and the
12459** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12460**
12461** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12462** handler, then the conflicting row is either updated or deleted, depending
12463** on the type of change.
12464**
12465** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12466** handler, then the conflicting row is removed from the database and a
12467** second attempt to apply the change is made. If this second attempt fails,
12468** the original row is restored to the database before continuing.
12469**
12470** <dt>SQLITE_CHANGESET_ABORT<dd>
12471** If this value is returned, any changes applied so far are rolled back
12472** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12473** </dl>
12474*/
12475#define SQLITE_CHANGESET_OMIT 0
12476#define SQLITE_CHANGESET_REPLACE 1
12477#define SQLITE_CHANGESET_ABORT 2
12478
12479/*
12480** CAPI3REF: Rebasing changesets
12481** EXPERIMENTAL
12482**
12483** Suppose there is a site hosting a database in state S0. And that
12484** modifications are made that move that database to state S1 and a
12485** changeset recorded (the "local" changeset). Then, a changeset based
12486** on S0 is received from another site (the "remote" changeset) and
12487** applied to the database. The database is then in state
12488** (S1+"remote"), where the exact state depends on any conflict
12489** resolution decisions (OMIT or REPLACE) made while applying "remote".
12490** Rebasing a changeset is to update it to take those conflict
12491** resolution decisions into account, so that the same conflicts
12492** do not have to be resolved elsewhere in the network.
12493**
12494** For example, if both the local and remote changesets contain an
12495** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
12496**
12497** local: INSERT INTO t1 VALUES(1, 'v1');
12498** remote: INSERT INTO t1 VALUES(1, 'v2');
12499**
12500** and the conflict resolution is REPLACE, then the INSERT change is
12501** removed from the local changeset (it was overridden). Or, if the
12502** conflict resolution was "OMIT", then the local changeset is modified
12503** to instead contain:
12504**
12505** UPDATE t1 SET b = 'v2' WHERE a=1;
12506**
12507** Changes within the local changeset are rebased as follows:
12508**
12509** <dl>
12510** <dt>Local INSERT<dd>
12511** This may only conflict with a remote INSERT. If the conflict
12512** resolution was OMIT, then add an UPDATE change to the rebased
12513** changeset. Or, if the conflict resolution was REPLACE, add
12514** nothing to the rebased changeset.
12515**
12516** <dt>Local DELETE<dd>
12517** This may conflict with a remote UPDATE or DELETE. In both cases the
12518** only possible resolution is OMIT. If the remote operation was a
12519** DELETE, then add no change to the rebased changeset. If the remote
12520** operation was an UPDATE, then the old.* fields of change are updated
12521** to reflect the new.* values in the UPDATE.
12522**
12523** <dt>Local UPDATE<dd>
12524** This may conflict with a remote UPDATE or DELETE. If it conflicts
12525** with a DELETE, and the conflict resolution was OMIT, then the update
12526** is changed into an INSERT. Any undefined values in the new.* record
12527** from the update change are filled in using the old.* values from
12528** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
12529** the UPDATE change is simply omitted from the rebased changeset.
12530**
12531** If conflict is with a remote UPDATE and the resolution is OMIT, then
12532** the old.* values are rebased using the new.* values in the remote
12533** change. Or, if the resolution is REPLACE, then the change is copied
12534** into the rebased changeset with updates to columns also updated by
12535** the conflicting remote UPDATE removed. If this means no columns would
12536** be updated, the change is omitted.
12537** </dl>
12538**
12539** A local change may be rebased against multiple remote changes
12540** simultaneously. If a single key is modified by multiple remote
12541** changesets, they are combined as follows before the local changeset
12542** is rebased:
12543**
12544** <ul>
12545** <li> If there has been one or more REPLACE resolutions on a
12546** key, it is rebased according to a REPLACE.
12547**
12548** <li> If there have been no REPLACE resolutions on a key, then
12549** the local changeset is rebased according to the most recent
12550** of the OMIT resolutions.
12551** </ul>
12552**
12553** Note that conflict resolutions from multiple remote changesets are
12554** combined on a per-field basis, not per-row. This means that in the
12555** case of multiple remote UPDATE operations, some fields of a single
12556** local change may be rebased for REPLACE while others are rebased for
12557** OMIT.
12558**
12559** In order to rebase a local changeset, the remote changeset must first
12560** be applied to the local database using sqlite3changeset_apply_v2() and
12561** the buffer of rebase information captured. Then:
12562**
12563** <ol>
12564** <li> An sqlite3_rebaser object is created by calling
12565** sqlite3rebaser_create().
12566** <li> The new object is configured with the rebase buffer obtained from
12567** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
12568** If the local changeset is to be rebased against multiple remote
12569** changesets, then sqlite3rebaser_configure() should be called
12570** multiple times, in the same order that the multiple
12571** sqlite3changeset_apply_v2() calls were made.
12572** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
12573** <li> The sqlite3_rebaser object is deleted by calling
12574** sqlite3rebaser_delete().
12575** </ol>
12576*/
12577typedef struct sqlite3_rebaser sqlite3_rebaser;
12578
12579/*
12580** CAPI3REF: Create a changeset rebaser object.
12581** EXPERIMENTAL
12582**
12583** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
12584** point to the new object and return SQLITE_OK. Otherwise, if an error
12585** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
12586** to NULL.
12587*/
12588SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
12589
12590/*
12591** CAPI3REF: Configure a changeset rebaser object.
12592** EXPERIMENTAL
12593**
12594** Configure the changeset rebaser object to rebase changesets according
12595** to the conflict resolutions described by buffer pRebase (size nRebase
12596** bytes), which must have been obtained from a previous call to
12597** sqlite3changeset_apply_v2().
12598*/
12599SQLITE_API int sqlite3rebaser_configure(
12600 sqlite3_rebaser*,
12601 int nRebase, const void *pRebase
12602);
12603
12604/*
12605** CAPI3REF: Rebase a changeset
12606** EXPERIMENTAL
12607**
12608** Argument pIn must point to a buffer containing a changeset nIn bytes
12609** in size. This function allocates and populates a buffer with a copy
12610** of the changeset rebased according to the configuration of the
12611** rebaser object passed as the first argument. If successful, (*ppOut)
12612** is set to point to the new buffer containing the rebased changeset and
12613** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
12614** responsibility of the caller to eventually free the new buffer using
12615** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
12616** are set to zero and an SQLite error code returned.
12617*/
12618SQLITE_API int sqlite3rebaser_rebase(
12619 sqlite3_rebaser*,
12620 int nIn, const void *pIn,
12621 int *pnOut, void **ppOut
12622);
12623
12624/*
12625** CAPI3REF: Delete a changeset rebaser object.
12626** EXPERIMENTAL
12627**
12628** Delete the changeset rebaser object and all associated resources. There
12629** should be one call to this function for each successful invocation
12630** of sqlite3rebaser_create().
12631*/
12632SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
12633
12634/*
12635** CAPI3REF: Streaming Versions of API functions.
12636**
12637** The six streaming API xxx_strm() functions serve similar purposes to the
12638** corresponding non-streaming API functions:
12639**
12640** <table border=1 style="margin-left:8ex;margin-right:8ex">
12641** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12642** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12643** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12644** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12645** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12646** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12647** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12648** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12649** </table>
12650**
12651** Non-streaming functions that accept changesets (or patchsets) as input
12652** require that the entire changeset be stored in a single buffer in memory.
12653** Similarly, those that return a changeset or patchset do so by returning
12654** a pointer to a single large buffer allocated using sqlite3_malloc().
12655** Normally this is convenient. However, if an application running in a
12656** low-memory environment is required to handle very large changesets, the
12657** large contiguous memory allocations required can become onerous.
12658**
12659** In order to avoid this problem, instead of a single large buffer, input
12660** is passed to a streaming API functions by way of a callback function that
12661** the sessions module invokes to incrementally request input data as it is
12662** required. In all cases, a pair of API function parameters such as
12663**
12664** <pre>
12665** &nbsp; int nChangeset,
12666** &nbsp; void *pChangeset,
12667** </pre>
12668**
12669** Is replaced by:
12670**
12671** <pre>
12672** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12673** &nbsp; void *pIn,
12674** </pre>
12675**
12676** Each time the xInput callback is invoked by the sessions module, the first
12677** argument passed is a copy of the supplied pIn context pointer. The second
12678** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12679** error occurs the xInput method should copy up to (*pnData) bytes of data
12680** into the buffer and set (*pnData) to the actual number of bytes copied
12681** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12682** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12683** error code should be returned. In all cases, if an xInput callback returns
12684** an error, all processing is abandoned and the streaming API function
12685** returns a copy of the error code to the caller.
12686**
12687** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12688** invoked by the sessions module at any point during the lifetime of the
12689** iterator. If such an xInput callback returns an error, the iterator enters
12690** an error state, whereby all subsequent calls to iterator functions
12691** immediately fail with the same error code as returned by xInput.
12692**
12693** Similarly, streaming API functions that return changesets (or patchsets)
12694** return them in chunks by way of a callback function instead of via a
12695** pointer to a single large buffer. In this case, a pair of parameters such
12696** as:
12697**
12698** <pre>
12699** &nbsp; int *pnChangeset,
12700** &nbsp; void **ppChangeset,
12701** </pre>
12702**
12703** Is replaced by:
12704**
12705** <pre>
12706** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12707** &nbsp; void *pOut
12708** </pre>
12709**
12710** The xOutput callback is invoked zero or more times to return data to
12711** the application. The first parameter passed to each call is a copy of the
12712** pOut pointer supplied by the application. The second parameter, pData,
12713** points to a buffer nData bytes in size containing the chunk of output
12714** data being returned. If the xOutput callback successfully processes the
12715** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12716** it should return some other SQLite error code. In this case processing
12717** is immediately abandoned and the streaming API function returns a copy
12718** of the xOutput error code to the application.
12719**
12720** The sessions module never invokes an xOutput callback with the third
12721** parameter set to a value less than or equal to zero. Other than this,
12722** no guarantees are made as to the size of the chunks of data returned.
12723*/
12724SQLITE_API int sqlite3changeset_apply_strm(
12725 sqlite3 *db, /* Apply change to "main" db of this handle */
12726 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12727 void *pIn, /* First arg for xInput */
12728 int(*xFilter)(
12729 void *pCtx, /* Copy of sixth arg to _apply() */
12730 const char *zTab /* Table name */
12731 ),
12732 int(*xConflict)(
12733 void *pCtx, /* Copy of sixth arg to _apply() */
12734 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12735 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12736 ),
12737 void *pCtx /* First argument passed to xConflict */
12738);
12739SQLITE_API int sqlite3changeset_apply_v2_strm(
12740 sqlite3 *db, /* Apply change to "main" db of this handle */
12741 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12742 void *pIn, /* First arg for xInput */
12743 int(*xFilter)(
12744 void *pCtx, /* Copy of sixth arg to _apply() */
12745 const char *zTab /* Table name */
12746 ),
12747 int(*xConflict)(
12748 void *pCtx, /* Copy of sixth arg to _apply() */
12749 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12750 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12751 ),
12752 void *pCtx, /* First argument passed to xConflict */
12753 void **ppRebase, int *pnRebase,
12754 int flags
12755);
12756SQLITE_API int sqlite3changeset_concat_strm(
12757 int (*xInputA)(void *pIn, void *pData, int *pnData),
12758 void *pInA,
12759 int (*xInputB)(void *pIn, void *pData, int *pnData),
12760 void *pInB,
12761 int (*xOutput)(void *pOut, const void *pData, int nData),
12762 void *pOut
12763);
12764SQLITE_API int sqlite3changeset_invert_strm(
12765 int (*xInput)(void *pIn, void *pData, int *pnData),
12766 void *pIn,
12767 int (*xOutput)(void *pOut, const void *pData, int nData),
12768 void *pOut
12769);
12770SQLITE_API int sqlite3changeset_start_strm(
12771 sqlite3_changeset_iter **pp,
12772 int (*xInput)(void *pIn, void *pData, int *pnData),
12773 void *pIn
12774);
12775SQLITE_API int sqlite3changeset_start_v2_strm(
12776 sqlite3_changeset_iter **pp,
12777 int (*xInput)(void *pIn, void *pData, int *pnData),
12778 void *pIn,
12779 int flags
12780);
12781SQLITE_API int sqlite3session_changeset_strm(
12782 sqlite3_session *pSession,
12783 int (*xOutput)(void *pOut, const void *pData, int nData),
12784 void *pOut
12785);
12786SQLITE_API int sqlite3session_patchset_strm(
12787 sqlite3_session *pSession,
12788 int (*xOutput)(void *pOut, const void *pData, int nData),
12789 void *pOut
12790);
12791SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12792 int (*xInput)(void *pIn, void *pData, int *pnData),
12793 void *pIn
12794);
12795SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12796 int (*xOutput)(void *pOut, const void *pData, int nData),
12797 void *pOut
12798);
12799SQLITE_API int sqlite3rebaser_rebase_strm(
12800 sqlite3_rebaser *pRebaser,
12801 int (*xInput)(void *pIn, void *pData, int *pnData),
12802 void *pIn,
12803 int (*xOutput)(void *pOut, const void *pData, int nData),
12804 void *pOut
12805);
12806
12807/*
12808** CAPI3REF: Configure global parameters
12809**
12810** The sqlite3session_config() interface is used to make global configuration
12811** changes to the sessions module in order to tune it to the specific needs
12812** of the application.
12813**
12814** The sqlite3session_config() interface is not threadsafe. If it is invoked
12815** while any other thread is inside any other sessions method then the
12816** results are undefined. Furthermore, if it is invoked after any sessions
12817** related objects have been created, the results are also undefined.
12818**
12819** The first argument to the sqlite3session_config() function must be one
12820** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12821** interpretation of the (void*) value passed as the second parameter and
12822** the effect of calling this function depends on the value of the first
12823** parameter.
12824**
12825** <dl>
12826** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12827** By default, the sessions module streaming interfaces attempt to input
12828** and output data in approximately 1 KiB chunks. This operand may be used
12829** to set and query the value of this configuration setting. The pointer
12830** passed as the second argument must point to a value of type (int).
12831** If this value is greater than 0, it is used as the new streaming data
12832** chunk size for both input and output. Before returning, the (int) value
12833** pointed to by pArg is set to the final value of the streaming interface
12834** chunk size.
12835** </dl>
12836**
12837** This function returns SQLITE_OK if successful, or an SQLite error code
12838** otherwise.
12839*/
12840SQLITE_API int sqlite3session_config(int op, void *pArg);
12841
12842/*
12843** CAPI3REF: Values for sqlite3session_config().
12844*/
12845#define SQLITE_SESSION_CONFIG_STRMSIZE 1
12846
12847/*
12848** Make sure we can call this stuff from C++.
12849*/
12850#if 0
12851}
12852#endif
12853
12854#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12855
12856/******** End of sqlite3session.h *********/
12857/******** Begin file fts5.h *********/
12858/*
12859** 2014 May 31
12860**
12861** The author disclaims copyright to this source code. In place of
12862** a legal notice, here is a blessing:
12863**
12864** May you do good and not evil.
12865** May you find forgiveness for yourself and forgive others.
12866** May you share freely, never taking more than you give.
12867**
12868******************************************************************************
12869**
12870** Interfaces to extend FTS5. Using the interfaces defined in this file,
12871** FTS5 may be extended with:
12872**
12873** * custom tokenizers, and
12874** * custom auxiliary functions.
12875*/
12876
12877
12878#ifndef _FTS5_H
12879#define _FTS5_H
12880
12881
12882#if 0
12883extern "C" {
12884#endif
12885
12886/*************************************************************************
12887** CUSTOM AUXILIARY FUNCTIONS
12888**
12889** Virtual table implementations may overload SQL functions by implementing
12890** the sqlite3_module.xFindFunction() method.
12891*/
12892
12893typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12894typedef struct Fts5Context Fts5Context;
12895typedef struct Fts5PhraseIter Fts5PhraseIter;
12896
12897typedef void (*fts5_extension_function)(
12898 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12899 Fts5Context *pFts, /* First arg to pass to pApi functions */
12900 sqlite3_context *pCtx, /* Context for returning result/error */
12901 int nVal, /* Number of values in apVal[] array */
12902 sqlite3_value **apVal /* Array of trailing arguments */
12903);
12904
12905struct Fts5PhraseIter {
12906 const unsigned char *a;
12907 const unsigned char *b;
12908};
12909
12910/*
12911** EXTENSION API FUNCTIONS
12912**
12913** xUserData(pFts):
12914** Return a copy of the context pointer the extension function was
12915** registered with.
12916**
12917** xColumnTotalSize(pFts, iCol, pnToken):
12918** If parameter iCol is less than zero, set output variable *pnToken
12919** to the total number of tokens in the FTS5 table. Or, if iCol is
12920** non-negative but less than the number of columns in the table, return
12921** the total number of tokens in column iCol, considering all rows in
12922** the FTS5 table.
12923**
12924** If parameter iCol is greater than or equal to the number of columns
12925** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12926** an OOM condition or IO error), an appropriate SQLite error code is
12927** returned.
12928**
12929** xColumnCount(pFts):
12930** Return the number of columns in the table.
12931**
12932** xColumnSize(pFts, iCol, pnToken):
12933** If parameter iCol is less than zero, set output variable *pnToken
12934** to the total number of tokens in the current row. Or, if iCol is
12935** non-negative but less than the number of columns in the table, set
12936** *pnToken to the number of tokens in column iCol of the current row.
12937**
12938** If parameter iCol is greater than or equal to the number of columns
12939** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12940** an OOM condition or IO error), an appropriate SQLite error code is
12941** returned.
12942**
12943** This function may be quite inefficient if used with an FTS5 table
12944** created with the "columnsize=0" option.
12945**
12946** xColumnText:
12947** This function attempts to retrieve the text of column iCol of the
12948** current document. If successful, (*pz) is set to point to a buffer
12949** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12950** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12951** if an error occurs, an SQLite error code is returned and the final values
12952** of (*pz) and (*pn) are undefined.
12953**
12954** xPhraseCount:
12955** Returns the number of phrases in the current query expression.
12956**
12957** xPhraseSize:
12958** Returns the number of tokens in phrase iPhrase of the query. Phrases
12959** are numbered starting from zero.
12960**
12961** xInstCount:
12962** Set *pnInst to the total number of occurrences of all phrases within
12963** the query within the current row. Return SQLITE_OK if successful, or
12964** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12965**
12966** This API can be quite slow if used with an FTS5 table created with the
12967** "detail=none" or "detail=column" option. If the FTS5 table is created
12968** with either "detail=none" or "detail=column" and "content=" option
12969** (i.e. if it is a contentless table), then this API always returns 0.
12970**
12971** xInst:
12972** Query for the details of phrase match iIdx within the current row.
12973** Phrase matches are numbered starting from zero, so the iIdx argument
12974** should be greater than or equal to zero and smaller than the value
12975** output by xInstCount().
12976**
12977** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12978** to the column in which it occurs and *piOff the token offset of the
12979** first token of the phrase. Returns SQLITE_OK if successful, or an error
12980** code (i.e. SQLITE_NOMEM) if an error occurs.
12981**
12982** This API can be quite slow if used with an FTS5 table created with the
12983** "detail=none" or "detail=column" option.
12984**
12985** xRowid:
12986** Returns the rowid of the current row.
12987**
12988** xTokenize:
12989** Tokenize text using the tokenizer belonging to the FTS5 table.
12990**
12991** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12992** This API function is used to query the FTS table for phrase iPhrase
12993** of the current query. Specifically, a query equivalent to:
12994**
12995** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12996**
12997** with $p set to a phrase equivalent to the phrase iPhrase of the
12998** current query is executed. Any column filter that applies to
12999** phrase iPhrase of the current query is included in $p. For each
13000** row visited, the callback function passed as the fourth argument
13001** is invoked. The context and API objects passed to the callback
13002** function may be used to access the properties of each matched row.
13003** Invoking Api.xUserData() returns a copy of the pointer passed as
13004** the third argument to pUserData.
13005**
13006** If the callback function returns any value other than SQLITE_OK, the
13007** query is abandoned and the xQueryPhrase function returns immediately.
13008** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
13009** Otherwise, the error code is propagated upwards.
13010**
13011** If the query runs to completion without incident, SQLITE_OK is returned.
13012** Or, if some error occurs before the query completes or is aborted by
13013** the callback, an SQLite error code is returned.
13014**
13015**
13016** xSetAuxdata(pFts5, pAux, xDelete)
13017**
13018** Save the pointer passed as the second argument as the extension function's
13019** "auxiliary data". The pointer may then be retrieved by the current or any
13020** future invocation of the same fts5 extension function made as part of
13021** the same MATCH query using the xGetAuxdata() API.
13022**
13023** Each extension function is allocated a single auxiliary data slot for
13024** each FTS query (MATCH expression). If the extension function is invoked
13025** more than once for a single FTS query, then all invocations share a
13026** single auxiliary data context.
13027**
13028** If there is already an auxiliary data pointer when this function is
13029** invoked, then it is replaced by the new pointer. If an xDelete callback
13030** was specified along with the original pointer, it is invoked at this
13031** point.
13032**
13033** The xDelete callback, if one is specified, is also invoked on the
13034** auxiliary data pointer after the FTS5 query has finished.
13035**
13036** If an error (e.g. an OOM condition) occurs within this function,
13037** the auxiliary data is set to NULL and an error code returned. If the
13038** xDelete parameter was not NULL, it is invoked on the auxiliary data
13039** pointer before returning.
13040**
13041**
13042** xGetAuxdata(pFts5, bClear)
13043**
13044** Returns the current auxiliary data pointer for the fts5 extension
13045** function. See the xSetAuxdata() method for details.
13046**
13047** If the bClear argument is non-zero, then the auxiliary data is cleared
13048** (set to NULL) before this function returns. In this case the xDelete,
13049** if any, is not invoked.
13050**
13051**
13052** xRowCount(pFts5, pnRow)
13053**
13054** This function is used to retrieve the total number of rows in the table.
13055** In other words, the same value that would be returned by:
13056**
13057** SELECT count(*) FROM ftstable;
13058**
13059** xPhraseFirst()
13060** This function is used, along with type Fts5PhraseIter and the xPhraseNext
13061** method, to iterate through all instances of a single query phrase within
13062** the current row. This is the same information as is accessible via the
13063** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
13064** to use, this API may be faster under some circumstances. To iterate
13065** through instances of phrase iPhrase, use the following code:
13066**
13067** Fts5PhraseIter iter;
13068** int iCol, iOff;
13069** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13070** iCol>=0;
13071** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13072** ){
13073** // An instance of phrase iPhrase at offset iOff of column iCol
13074** }
13075**
13076** The Fts5PhraseIter structure is defined above. Applications should not
13077** modify this structure directly - it should only be used as shown above
13078** with the xPhraseFirst() and xPhraseNext() API methods (and by
13079** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
13080**
13081** This API can be quite slow if used with an FTS5 table created with the
13082** "detail=none" or "detail=column" option. If the FTS5 table is created
13083** with either "detail=none" or "detail=column" and "content=" option
13084** (i.e. if it is a contentless table), then this API always iterates
13085** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13086**
13087** xPhraseNext()
13088** See xPhraseFirst above.
13089**
13090** xPhraseFirstColumn()
13091** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
13092** and xPhraseNext() APIs described above. The difference is that instead
13093** of iterating through all instances of a phrase in the current row, these
13094** APIs are used to iterate through the set of columns in the current row
13095** that contain one or more instances of a specified phrase. For example:
13096**
13097** Fts5PhraseIter iter;
13098** int iCol;
13099** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13100** iCol>=0;
13101** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13102** ){
13103** // Column iCol contains at least one instance of phrase iPhrase
13104** }
13105**
13106** This API can be quite slow if used with an FTS5 table created with the
13107** "detail=none" option. If the FTS5 table is created with either
13108** "detail=none" "content=" option (i.e. if it is a contentless table),
13109** then this API always iterates through an empty set (all calls to
13110** xPhraseFirstColumn() set iCol to -1).
13111**
13112** The information accessed using this API and its companion
13113** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
13114** (or xInst/xInstCount). The chief advantage of this API is that it is
13115** significantly more efficient than those alternatives when used with
13116** "detail=column" tables.
13117**
13118** xPhraseNextColumn()
13119** See xPhraseFirstColumn above.
13120*/
13121struct Fts5ExtensionApi {
13122 int iVersion; /* Currently always set to 2 */
13123
13124 void *(*xUserData)(Fts5Context*);
13125
13126 int (*xColumnCount)(Fts5Context*);
13127 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
13128 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
13129
13130 int (*xTokenize)(Fts5Context*,
13131 const char *pText, int nText, /* Text to tokenize */
13132 void *pCtx, /* Context passed to xToken() */
13133 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
13134 );
13135
13136 int (*xPhraseCount)(Fts5Context*);
13137 int (*xPhraseSize)(Fts5Context*, int iPhrase);
13138
13139 int (*xInstCount)(Fts5Context*, int *pnInst);
13140 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
13141
13142 sqlite3_int64 (*xRowid)(Fts5Context*);
13143 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
13144 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
13145
13146 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
13147 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
13148 );
13149 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
13150 void *(*xGetAuxdata)(Fts5Context*, int bClear);
13151
13152 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
13153 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
13154
13155 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
13156 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
13157};
13158
13159/*
13160** CUSTOM AUXILIARY FUNCTIONS
13161*************************************************************************/
13162
13163/*************************************************************************
13164** CUSTOM TOKENIZERS
13165**
13166** Applications may also register custom tokenizer types. A tokenizer
13167** is registered by providing fts5 with a populated instance of the
13168** following structure. All structure methods must be defined, setting
13169** any member of the fts5_tokenizer struct to NULL leads to undefined
13170** behaviour. The structure methods are expected to function as follows:
13171**
13172** xCreate:
13173** This function is used to allocate and initialize a tokenizer instance.
13174** A tokenizer instance is required to actually tokenize text.
13175**
13176** The first argument passed to this function is a copy of the (void*)
13177** pointer provided by the application when the fts5_tokenizer object
13178** was registered with FTS5 (the third argument to xCreateTokenizer()).
13179** The second and third arguments are an array of nul-terminated strings
13180** containing the tokenizer arguments, if any, specified following the
13181** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
13182** to create the FTS5 table.
13183**
13184** The final argument is an output variable. If successful, (*ppOut)
13185** should be set to point to the new tokenizer handle and SQLITE_OK
13186** returned. If an error occurs, some value other than SQLITE_OK should
13187** be returned. In this case, fts5 assumes that the final value of *ppOut
13188** is undefined.
13189**
13190** xDelete:
13191** This function is invoked to delete a tokenizer handle previously
13192** allocated using xCreate(). Fts5 guarantees that this function will
13193** be invoked exactly once for each successful call to xCreate().
13194**
13195** xTokenize:
13196** This function is expected to tokenize the nText byte string indicated
13197** by argument pText. pText may or may not be nul-terminated. The first
13198** argument passed to this function is a pointer to an Fts5Tokenizer object
13199** returned by an earlier call to xCreate().
13200**
13201** The second argument indicates the reason that FTS5 is requesting
13202** tokenization of the supplied text. This is always one of the following
13203** four values:
13204**
13205** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13206** or removed from the FTS table. The tokenizer is being invoked to
13207** determine the set of tokens to add to (or delete from) the
13208** FTS index.
13209**
13210** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13211** against the FTS index. The tokenizer is being called to tokenize
13212** a bareword or quoted string specified as part of the query.
13213**
13214** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13215** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
13216** followed by a "*" character, indicating that the last token
13217** returned by the tokenizer will be treated as a token prefix.
13218**
13219** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13220** satisfy an fts5_api.xTokenize() request made by an auxiliary
13221** function. Or an fts5_api.xColumnSize() request made by the same
13222** on a columnsize=0 database.
13223** </ul>
13224**
13225** For each token in the input string, the supplied callback xToken() must
13226** be invoked. The first argument to it should be a copy of the pointer
13227** passed as the second argument to xTokenize(). The third and fourth
13228** arguments are a pointer to a buffer containing the token text, and the
13229** size of the token in bytes. The 4th and 5th arguments are the byte offsets
13230** of the first byte of and first byte immediately following the text from
13231** which the token is derived within the input.
13232**
13233** The second argument passed to the xToken() callback ("tflags") should
13234** normally be set to 0. The exception is if the tokenizer supports
13235** synonyms. In this case see the discussion below for details.
13236**
13237** FTS5 assumes the xToken() callback is invoked for each token in the
13238** order that they occur within the input text.
13239**
13240** If an xToken() callback returns any value other than SQLITE_OK, then
13241** the tokenization should be abandoned and the xTokenize() method should
13242** immediately return a copy of the xToken() return value. Or, if the
13243** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
13244** if an error occurs with the xTokenize() implementation itself, it
13245** may abandon the tokenization and return any error code other than
13246** SQLITE_OK or SQLITE_DONE.
13247**
13248** SYNONYM SUPPORT
13249**
13250** Custom tokenizers may also support synonyms. Consider a case in which a
13251** user wishes to query for a phrase such as "first place". Using the
13252** built-in tokenizers, the FTS5 query 'first + place' will match instances
13253** of "first place" within the document set, but not alternative forms
13254** such as "1st place". In some applications, it would be better to match
13255** all instances of "first place" or "1st place" regardless of which form
13256** the user specified in the MATCH query text.
13257**
13258** There are several ways to approach this in FTS5:
13259**
13260** <ol><li> By mapping all synonyms to a single token. In this case, using
13261** the above example, this means that the tokenizer returns the
13262** same token for inputs "first" and "1st". Say that token is in
13263** fact "first", so that when the user inserts the document "I won
13264** 1st place" entries are added to the index for tokens "i", "won",
13265** "first" and "place". If the user then queries for '1st + place',
13266** the tokenizer substitutes "first" for "1st" and the query works
13267** as expected.
13268**
13269** <li> By querying the index for all synonyms of each query term
13270** separately. In this case, when tokenizing query text, the
13271** tokenizer may provide multiple synonyms for a single term
13272** within the document. FTS5 then queries the index for each
13273** synonym individually. For example, faced with the query:
13274**
13275** <codeblock>
13276** ... MATCH 'first place'</codeblock>
13277**
13278** the tokenizer offers both "1st" and "first" as synonyms for the
13279** first token in the MATCH query and FTS5 effectively runs a query
13280** similar to:
13281**
13282** <codeblock>
13283** ... MATCH '(first OR 1st) place'</codeblock>
13284**
13285** except that, for the purposes of auxiliary functions, the query
13286** still appears to contain just two phrases - "(first OR 1st)"
13287** being treated as a single phrase.
13288**
13289** <li> By adding multiple synonyms for a single term to the FTS index.
13290** Using this method, when tokenizing document text, the tokenizer
13291** provides multiple synonyms for each token. So that when a
13292** document such as "I won first place" is tokenized, entries are
13293** added to the FTS index for "i", "won", "first", "1st" and
13294** "place".
13295**
13296** This way, even if the tokenizer does not provide synonyms
13297** when tokenizing query text (it should not - to do so would be
13298** inefficient), it doesn't matter if the user queries for
13299** 'first + place' or '1st + place', as there are entries in the
13300** FTS index corresponding to both forms of the first token.
13301** </ol>
13302**
13303** Whether it is parsing document or query text, any call to xToken that
13304** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
13305** is considered to supply a synonym for the previous token. For example,
13306** when parsing the document "I won first place", a tokenizer that supports
13307** synonyms would call xToken() 5 times, as follows:
13308**
13309** <codeblock>
13310** xToken(pCtx, 0, "i", 1, 0, 1);
13311** xToken(pCtx, 0, "won", 3, 2, 5);
13312** xToken(pCtx, 0, "first", 5, 6, 11);
13313** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
13314** xToken(pCtx, 0, "place", 5, 12, 17);
13315**</codeblock>
13316**
13317** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
13318** xToken() is called. Multiple synonyms may be specified for a single token
13319** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
13320** There is no limit to the number of synonyms that may be provided for a
13321** single token.
13322**
13323** In many cases, method (1) above is the best approach. It does not add
13324** extra data to the FTS index or require FTS5 to query for multiple terms,
13325** so it is efficient in terms of disk space and query speed. However, it
13326** does not support prefix queries very well. If, as suggested above, the
13327** token "first" is substituted for "1st" by the tokenizer, then the query:
13328**
13329** <codeblock>
13330** ... MATCH '1s*'</codeblock>
13331**
13332** will not match documents that contain the token "1st" (as the tokenizer
13333** will probably not map "1s" to any prefix of "first").
13334**
13335** For full prefix support, method (3) may be preferred. In this case,
13336** because the index contains entries for both "first" and "1st", prefix
13337** queries such as 'fi*' or '1s*' will match correctly. However, because
13338** extra entries are added to the FTS index, this method uses more space
13339** within the database.
13340**
13341** Method (2) offers a midpoint between (1) and (3). Using this method,
13342** a query such as '1s*' will match documents that contain the literal
13343** token "1st", but not "first" (assuming the tokenizer is not able to
13344** provide synonyms for prefixes). However, a non-prefix query like '1st'
13345** will match against "1st" and "first". This method does not require
13346** extra disk space, as no extra entries are added to the FTS index.
13347** On the other hand, it may require more CPU cycles to run MATCH queries,
13348** as separate queries of the FTS index are required for each synonym.
13349**
13350** When using methods (2) or (3), it is important that the tokenizer only
13351** provide synonyms when tokenizing document text (method (3)) or query
13352** text (method (2)), not both. Doing so will not cause any errors, but is
13353** inefficient.
13354*/
13355typedef struct Fts5Tokenizer Fts5Tokenizer;
13356typedef struct fts5_tokenizer fts5_tokenizer;
13357struct fts5_tokenizer {
13358 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13359 void (*xDelete)(Fts5Tokenizer*);
13360 int (*xTokenize)(Fts5Tokenizer*,
13361 void *pCtx,
13362 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13363 const char *pText, int nText,
13364 int (*xToken)(
13365 void *pCtx, /* Copy of 2nd argument to xTokenize() */
13366 int tflags, /* Mask of FTS5_TOKEN_* flags */
13367 const char *pToken, /* Pointer to buffer containing token */
13368 int nToken, /* Size of token in bytes */
13369 int iStart, /* Byte offset of token within input text */
13370 int iEnd /* Byte offset of end of token within input text */
13371 )
13372 );
13373};
13374
13375/* Flags that may be passed as the third argument to xTokenize() */
13376#define FTS5_TOKENIZE_QUERY 0x0001
13377#define FTS5_TOKENIZE_PREFIX 0x0002
13378#define FTS5_TOKENIZE_DOCUMENT 0x0004
13379#define FTS5_TOKENIZE_AUX 0x0008
13380
13381/* Flags that may be passed by the tokenizer implementation back to FTS5
13382** as the third argument to the supplied xToken callback. */
13383#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
13384
13385/*
13386** END OF CUSTOM TOKENIZERS
13387*************************************************************************/
13388
13389/*************************************************************************
13390** FTS5 EXTENSION REGISTRATION API
13391*/
13392typedef struct fts5_api fts5_api;
13393struct fts5_api {
13394 int iVersion; /* Currently always set to 2 */
13395
13396 /* Create a new tokenizer */
13397 int (*xCreateTokenizer)(
13398 fts5_api *pApi,
13399 const char *zName,
13400 void *pUserData,
13401 fts5_tokenizer *pTokenizer,
13402 void (*xDestroy)(void*)
13403 );
13404
13405 /* Find an existing tokenizer */
13406 int (*xFindTokenizer)(
13407 fts5_api *pApi,
13408 const char *zName,
13409 void **ppUserData,
13410 fts5_tokenizer *pTokenizer
13411 );
13412
13413 /* Create a new auxiliary function */
13414 int (*xCreateFunction)(
13415 fts5_api *pApi,
13416 const char *zName,
13417 void *pUserData,
13418 fts5_extension_function xFunction,
13419 void (*xDestroy)(void*)
13420 );
13421};
13422
13423/*
13424** END OF REGISTRATION API
13425*************************************************************************/
13426
13427#if 0
13428} /* end of the 'extern "C"' block */
13429#endif
13430
13431#endif /* _FTS5_H */
13432
13433/******** End of fts5.h *********/
13434
13435/************** End of sqlite3.h *********************************************/
13436/************** Continuing where we left off in sqliteInt.h ******************/
13437
13438/*
13439** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
13440*/
13441#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1
13442
13443/*
13444** Include the configuration header output by 'configure' if we're using the
13445** autoconf-based build
13446*/
13447#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13448#include "sqlite_cfg.h"
13449#define SQLITECONFIG_H 1
13450#endif
13451
13452/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13453/************** Begin file sqliteLimit.h *************************************/
13454/*
13455** 2007 May 7
13456**
13457** The author disclaims copyright to this source code. In place of
13458** a legal notice, here is a blessing:
13459**
13460** May you do good and not evil.
13461** May you find forgiveness for yourself and forgive others.
13462** May you share freely, never taking more than you give.
13463**
13464*************************************************************************
13465**
13466** This file defines various limits of what SQLite can process.
13467*/
13468
13469/*
13470** The maximum length of a TEXT or BLOB in bytes. This also
13471** limits the size of a row in a table or index.
13472**
13473** The hard limit is the ability of a 32-bit signed integer
13474** to count the size: 2^31-1 or 2147483647.
13475*/
13476#ifndef SQLITE_MAX_LENGTH
13477# define SQLITE_MAX_LENGTH 1000000000
13478#endif
13479
13480/*
13481** This is the maximum number of
13482**
13483** * Columns in a table
13484** * Columns in an index
13485** * Columns in a view
13486** * Terms in the SET clause of an UPDATE statement
13487** * Terms in the result set of a SELECT statement
13488** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
13489** * Terms in the VALUES clause of an INSERT statement
13490**
13491** The hard upper limit here is 32676. Most database people will
13492** tell you that in a well-normalized database, you usually should
13493** not have more than a dozen or so columns in any table. And if
13494** that is the case, there is no point in having more than a few
13495** dozen values in any of the other situations described above.
13496*/
13497#ifndef SQLITE_MAX_COLUMN
13498# define SQLITE_MAX_COLUMN 2000
13499#endif
13500
13501/*
13502** The maximum length of a single SQL statement in bytes.
13503**
13504** It used to be the case that setting this value to zero would
13505** turn the limit off. That is no longer true. It is not possible
13506** to turn this limit off.
13507*/
13508#ifndef SQLITE_MAX_SQL_LENGTH
13509# define SQLITE_MAX_SQL_LENGTH 1000000000
13510#endif
13511
13512/*
13513** The maximum depth of an expression tree. This is limited to
13514** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
13515** want to place more severe limits on the complexity of an
13516** expression. A value of 0 means that there is no limit.
13517*/
13518#ifndef SQLITE_MAX_EXPR_DEPTH
13519# define SQLITE_MAX_EXPR_DEPTH 1000
13520#endif
13521
13522/*
13523** The maximum number of terms in a compound SELECT statement.
13524** The code generator for compound SELECT statements does one
13525** level of recursion for each term. A stack overflow can result
13526** if the number of terms is too large. In practice, most SQL
13527** never has more than 3 or 4 terms. Use a value of 0 to disable
13528** any limit on the number of terms in a compound SELECT.
13529*/
13530#ifndef SQLITE_MAX_COMPOUND_SELECT
13531# define SQLITE_MAX_COMPOUND_SELECT 500
13532#endif
13533
13534/*
13535** The maximum number of opcodes in a VDBE program.
13536** Not currently enforced.
13537*/
13538#ifndef SQLITE_MAX_VDBE_OP
13539# define SQLITE_MAX_VDBE_OP 250000000
13540#endif
13541
13542/*
13543** The maximum number of arguments to an SQL function.
13544*/
13545#ifndef SQLITE_MAX_FUNCTION_ARG
13546# define SQLITE_MAX_FUNCTION_ARG 127
13547#endif
13548
13549/*
13550** The suggested maximum number of in-memory pages to use for
13551** the main database table and for temporary tables.
13552**
13553** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13554** which means the cache size is limited to 2048000 bytes of memory.
13555** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13556** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13557*/
13558#ifndef SQLITE_DEFAULT_CACHE_SIZE
13559# define SQLITE_DEFAULT_CACHE_SIZE -2000
13560#endif
13561
13562/*
13563** The default number of frames to accumulate in the log file before
13564** checkpointing the database in WAL mode.
13565*/
13566#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
13567# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
13568#endif
13569
13570/*
13571** The maximum number of attached databases. This must be between 0
13572** and 125. The upper bound of 125 is because the attached databases are
13573** counted using a signed 8-bit integer which has a maximum value of 127
13574** and we have to allow 2 extra counts for the "main" and "temp" databases.
13575*/
13576#ifndef SQLITE_MAX_ATTACHED
13577# define SQLITE_MAX_ATTACHED 10
13578#endif
13579
13580
13581/*
13582** The maximum value of a ?nnn wildcard that the parser will accept.
13583** If the value exceeds 32767 then extra space is required for the Expr
13584** structure. But otherwise, we believe that the number can be as large
13585** as a signed 32-bit integer can hold.
13586*/
13587#ifndef SQLITE_MAX_VARIABLE_NUMBER
13588# define SQLITE_MAX_VARIABLE_NUMBER 32766
13589#endif
13590
13591/* Maximum page size. The upper bound on this value is 65536. This a limit
13592** imposed by the use of 16-bit offsets within each page.
13593**
13594** Earlier versions of SQLite allowed the user to change this value at
13595** compile time. This is no longer permitted, on the grounds that it creates
13596** a library that is technically incompatible with an SQLite library
13597** compiled with a different limit. If a process operating on a database
13598** with a page-size of 65536 bytes crashes, then an instance of SQLite
13599** compiled with the default page-size limit will not be able to rollback
13600** the aborted transaction. This could lead to database corruption.
13601*/
13602#ifdef SQLITE_MAX_PAGE_SIZE
13603# undef SQLITE_MAX_PAGE_SIZE
13604#endif
13605#define SQLITE_MAX_PAGE_SIZE 65536
13606
13607
13608/*
13609** The default size of a database page.
13610*/
13611#ifndef SQLITE_DEFAULT_PAGE_SIZE
13612# define SQLITE_DEFAULT_PAGE_SIZE 4096
13613#endif
13614#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
13615# undef SQLITE_DEFAULT_PAGE_SIZE
13616# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
13617#endif
13618
13619/*
13620** Ordinarily, if no value is explicitly provided, SQLite creates databases
13621** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
13622** device characteristics (sector-size and atomic write() support),
13623** SQLite may choose a larger value. This constant is the maximum value
13624** SQLite will choose on its own.
13625*/
13626#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
13627# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
13628#endif
13629#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
13630# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
13631# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
13632#endif
13633
13634
13635/*
13636** Maximum number of pages in one database file.
13637**
13638** This is really just the default value for the max_page_count pragma.
13639** This value can be lowered (or raised) at run-time using that the
13640** max_page_count macro.
13641*/
13642#ifndef SQLITE_MAX_PAGE_COUNT
13643# define SQLITE_MAX_PAGE_COUNT 1073741823
13644#endif
13645
13646/*
13647** Maximum length (in bytes) of the pattern in a LIKE or GLOB
13648** operator.
13649*/
13650#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
13651# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
13652#endif
13653
13654/*
13655** Maximum depth of recursion for triggers.
13656**
13657** A value of 1 means that a trigger program will not be able to itself
13658** fire any triggers. A value of 0 means that no trigger programs at all
13659** may be executed.
13660*/
13661#ifndef SQLITE_MAX_TRIGGER_DEPTH
13662# define SQLITE_MAX_TRIGGER_DEPTH 1000
13663#endif
13664
13665/************** End of sqliteLimit.h *****************************************/
13666/************** Continuing where we left off in sqliteInt.h ******************/
13667
13668/* Disable nuisance warnings on Borland compilers */
13669#if defined(__BORLANDC__)
13670#pragma warn -rch /* unreachable code */
13671#pragma warn -ccc /* Condition is always true or false */
13672#pragma warn -aus /* Assigned value is never used */
13673#pragma warn -csu /* Comparing signed and unsigned */
13674#pragma warn -spa /* Suspicious pointer arithmetic */
13675#endif
13676
13677/*
13678** A few places in the code require atomic load/store of aligned
13679** integer values.
13680*/
13681#ifndef __has_extension
13682# define __has_extension(x) 0 /* compatibility with non-clang compilers */
13683#endif
13684#if GCC_VERSION>=4007000 || __has_extension(c_atomic)
13685# define SQLITE_ATOMIC_INTRINSICS 1
13686# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
13687# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
13688#else
13689# define SQLITE_ATOMIC_INTRINSICS 0
13690# define AtomicLoad(PTR) (*(PTR))
13691# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
13692#endif
13693
13694/*
13695** Include standard header files as necessary
13696*/
13697#ifdef HAVE_STDINT_H
13698#include <stdint.h>
13699#endif
13700#ifdef HAVE_INTTYPES_H
13701#include <inttypes.h>
13702#endif
13703
13704/*
13705** The following macros are used to cast pointers to integers and
13706** integers to pointers. The way you do this varies from one compiler
13707** to the next, so we have developed the following set of #if statements
13708** to generate appropriate macros for a wide range of compilers.
13709**
13710** The correct "ANSI" way to do this is to use the intptr_t type.
13711** Unfortunately, that typedef is not available on all compilers, or
13712** if it is available, it requires an #include of specific headers
13713** that vary from one machine to the next.
13714**
13715** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13716** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
13717** So we have to define the macros in different ways depending on the
13718** compiler.
13719*/
13720#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
13721# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
13722# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
13723#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
13724# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
13725# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
13726#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
13727# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
13728# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13729#else /* Generates a warning - but it always works */
13730# define SQLITE_INT_TO_PTR(X) ((void*)(X))
13731# define SQLITE_PTR_TO_INT(X) ((int)(X))
13732#endif
13733
13734/*
13735** Macros to hint to the compiler that a function should or should not be
13736** inlined.
13737*/
13738#if defined(__GNUC__)
13739# define SQLITE_NOINLINE __attribute__((noinline))
13740# define SQLITE_INLINE __attribute__((always_inline)) inline
13741#elif defined(_MSC_VER) && _MSC_VER>=1310
13742# define SQLITE_NOINLINE __declspec(noinline)
13743# define SQLITE_INLINE __forceinline
13744#else
13745# define SQLITE_NOINLINE
13746# define SQLITE_INLINE
13747#endif
13748#if defined(SQLITE_COVERAGE_TEST) || defined(__STRICT_ANSI__)
13749# undef SQLITE_INLINE
13750# define SQLITE_INLINE
13751#endif
13752
13753/*
13754** Make sure that the compiler intrinsics we desire are enabled when
13755** compiling with an appropriate version of MSVC unless prevented by
13756** the SQLITE_DISABLE_INTRINSIC define.
13757*/
13758#if !defined(SQLITE_DISABLE_INTRINSIC)
13759# if defined(_MSC_VER) && _MSC_VER>=1400
13760# if !defined(_WIN32_WCE)
13761# include <intrin.h>
13762# pragma intrinsic(_byteswap_ushort)
13763# pragma intrinsic(_byteswap_ulong)
13764# pragma intrinsic(_byteswap_uint64)
13765# pragma intrinsic(_ReadWriteBarrier)
13766# else
13767# include <cmnintrin.h>
13768# endif
13769# endif
13770#endif
13771
13772/*
13773** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13774** 0 means mutexes are permanently disable and the library is never
13775** threadsafe. 1 means the library is serialized which is the highest
13776** level of threadsafety. 2 means the library is multithreaded - multiple
13777** threads can use SQLite as long as no two threads try to use the same
13778** database connection at the same time.
13779**
13780** Older versions of SQLite used an optional THREADSAFE macro.
13781** We support that for legacy.
13782**
13783** To ensure that the correct value of "THREADSAFE" is reported when querying
13784** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13785** logic is partially replicated in ctime.c. If it is updated here, it should
13786** also be updated there.
13787*/
13788#if !defined(SQLITE_THREADSAFE)
13789# if defined(THREADSAFE)
13790# define SQLITE_THREADSAFE THREADSAFE
13791# else
13792# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13793# endif
13794#endif
13795
13796/*
13797** Powersafe overwrite is on by default. But can be turned off using
13798** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13799*/
13800#ifndef SQLITE_POWERSAFE_OVERWRITE
13801# define SQLITE_POWERSAFE_OVERWRITE 1
13802#endif
13803
13804/*
13805** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13806** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13807** which case memory allocation statistics are disabled by default.
13808*/
13809#if !defined(SQLITE_DEFAULT_MEMSTATUS)
13810# define SQLITE_DEFAULT_MEMSTATUS 1
13811#endif
13812
13813/*
13814** Exactly one of the following macros must be defined in order to
13815** specify which memory allocation subsystem to use.
13816**
13817** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
13818** SQLITE_WIN32_MALLOC // Use Win32 native heap API
13819** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
13820** SQLITE_MEMDEBUG // Debugging version of system malloc()
13821**
13822** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13823** assert() macro is enabled, each call into the Win32 native heap subsystem
13824** will cause HeapValidate to be called. If heap validation should fail, an
13825** assertion will be triggered.
13826**
13827** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13828** the default.
13829*/
13830#if defined(SQLITE_SYSTEM_MALLOC) \
13831 + defined(SQLITE_WIN32_MALLOC) \
13832 + defined(SQLITE_ZERO_MALLOC) \
13833 + defined(SQLITE_MEMDEBUG)>1
13834# error "Two or more of the following compile-time configuration options\
13835 are defined but at most one is allowed:\
13836 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13837 SQLITE_ZERO_MALLOC"
13838#endif
13839#if defined(SQLITE_SYSTEM_MALLOC) \
13840 + defined(SQLITE_WIN32_MALLOC) \
13841 + defined(SQLITE_ZERO_MALLOC) \
13842 + defined(SQLITE_MEMDEBUG)==0
13843# define SQLITE_SYSTEM_MALLOC 1
13844#endif
13845
13846/*
13847** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13848** sizes of memory allocations below this value where possible.
13849*/
13850#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
13851# define SQLITE_MALLOC_SOFT_LIMIT 1024
13852#endif
13853
13854/*
13855** We need to define _XOPEN_SOURCE as follows in order to enable
13856** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13857** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13858** it.
13859*/
13860#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
13861# define _XOPEN_SOURCE 600
13862#endif
13863
13864/*
13865** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13866** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13867** make it true by defining or undefining NDEBUG.
13868**
13869** Setting NDEBUG makes the code smaller and faster by disabling the
13870** assert() statements in the code. So we want the default action
13871** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13872** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13873** feature.
13874*/
13875#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
13876# define NDEBUG 1
13877#endif
13878#if defined(NDEBUG) && defined(SQLITE_DEBUG)
13879# undef NDEBUG
13880#endif
13881
13882/*
13883** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13884*/
13885#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13886# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13887#endif
13888
13889/*
13890** The testcase() macro is used to aid in coverage testing. When
13891** doing coverage testing, the condition inside the argument to
13892** testcase() must be evaluated both true and false in order to
13893** get full branch coverage. The testcase() macro is inserted
13894** to help ensure adequate test coverage in places where simple
13895** condition/decision coverage is inadequate. For example, testcase()
13896** can be used to make sure boundary values are tested. For
13897** bitmask tests, testcase() can be used to make sure each bit
13898** is significant and used at least once. On switch statements
13899** where multiple cases go to the same block of code, testcase()
13900** can insure that all cases are evaluated.
13901*/
13902#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
13903# ifndef SQLITE_AMALGAMATION
13904 extern unsigned int sqlite3CoverageCounter;
13905# endif
13906# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__; }
13907#else
13908# define testcase(X)
13909#endif
13910
13911/*
13912** The TESTONLY macro is used to enclose variable declarations or
13913** other bits of code that are needed to support the arguments
13914** within testcase() and assert() macros.
13915*/
13916#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
13917# define TESTONLY(X) X
13918#else
13919# define TESTONLY(X)
13920#endif
13921
13922/*
13923** Sometimes we need a small amount of code such as a variable initialization
13924** to setup for a later assert() statement. We do not want this code to
13925** appear when assert() is disabled. The following macro is therefore
13926** used to contain that setup code. The "VVA" acronym stands for
13927** "Verification, Validation, and Accreditation". In other words, the
13928** code within VVA_ONLY() will only run during verification processes.
13929*/
13930#ifndef NDEBUG
13931# define VVA_ONLY(X) X
13932#else
13933# define VVA_ONLY(X)
13934#endif
13935
13936/*
13937** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13938** and mutation testing
13939*/
13940#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13941# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
13942#endif
13943
13944/*
13945** The ALWAYS and NEVER macros surround boolean expressions which
13946** are intended to always be true or false, respectively. Such
13947** expressions could be omitted from the code completely. But they
13948** are included in a few cases in order to enhance the resilience
13949** of SQLite to unexpected behavior - to make the code "self-healing"
13950** or "ductile" rather than being "brittle" and crashing at the first
13951** hint of unplanned behavior.
13952**
13953** In other words, ALWAYS and NEVER are added for defensive code.
13954**
13955** When doing coverage testing ALWAYS and NEVER are hard-coded to
13956** be true and false so that the unreachable code they specify will
13957** not be counted as untested code.
13958*/
13959#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
13960# define ALWAYS(X) (1)
13961# define NEVER(X) (0)
13962#elif !defined(NDEBUG)
13963# define ALWAYS(X) ((X)?1:(assert(0),0))
13964# define NEVER(X) ((X)?(assert(0),1):0)
13965#else
13966# define ALWAYS(X) (X)
13967# define NEVER(X) (X)
13968#endif
13969
13970/*
13971** Some conditionals are optimizations only. In other words, if the
13972** conditionals are replaced with a constant 1 (true) or 0 (false) then
13973** the correct answer is still obtained, though perhaps not as quickly.
13974**
13975** The following macros mark these optimizations conditionals.
13976*/
13977#if defined(SQLITE_MUTATION_TEST)
13978# define OK_IF_ALWAYS_TRUE(X) (1)
13979# define OK_IF_ALWAYS_FALSE(X) (0)
13980#else
13981# define OK_IF_ALWAYS_TRUE(X) (X)
13982# define OK_IF_ALWAYS_FALSE(X) (X)
13983#endif
13984
13985/*
13986** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13987** defined. We need to defend against those failures when testing with
13988** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13989** during a normal build. The following macro can be used to disable tests
13990** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13991*/
13992#if defined(SQLITE_TEST_REALLOC_STRESS)
13993# define ONLY_IF_REALLOC_STRESS(X) (X)
13994#elif !defined(NDEBUG)
13995# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
13996#else
13997# define ONLY_IF_REALLOC_STRESS(X) (0)
13998#endif
13999
14000/*
14001** Declarations used for tracing the operating system interfaces.
14002*/
14003#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
14004 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
14005 extern int sqlite3OSTrace;
14006# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
14007# define SQLITE_HAVE_OS_TRACE
14008#else
14009# define OSTRACE(X)
14010# undef SQLITE_HAVE_OS_TRACE
14011#endif
14012
14013/*
14014** Is the sqlite3ErrName() function needed in the build? Currently,
14015** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
14016** OSTRACE is enabled), and by several "test*.c" files (which are
14017** compiled using SQLITE_TEST).
14018*/
14019#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
14020 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
14021# define SQLITE_NEED_ERR_NAME
14022#else
14023# undef SQLITE_NEED_ERR_NAME
14024#endif
14025
14026/*
14027** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
14028*/
14029#ifdef SQLITE_OMIT_EXPLAIN
14030# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
14031#endif
14032
14033/*
14034** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE
14035*/
14036#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)
14037# define SQLITE_OMIT_ALTERTABLE
14038#endif
14039
14040/*
14041** Return true (non-zero) if the input is an integer that is too large
14042** to fit in 32-bits. This macro is used inside of various testcase()
14043** macros to verify that we have tested SQLite for large-file support.
14044*/
14045#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
14046
14047/*
14048** The macro unlikely() is a hint that surrounds a boolean
14049** expression that is usually false. Macro likely() surrounds
14050** a boolean expression that is usually true. These hints could,
14051** in theory, be used by the compiler to generate better code, but
14052** currently they are just comments for human readers.
14053*/
14054#define likely(X) (X)
14055#define unlikely(X) (X)
14056
14057/************** Include hash.h in the middle of sqliteInt.h ******************/
14058/************** Begin file hash.h ********************************************/
14059/*
14060** 2001 September 22
14061**
14062** The author disclaims copyright to this source code. In place of
14063** a legal notice, here is a blessing:
14064**
14065** May you do good and not evil.
14066** May you find forgiveness for yourself and forgive others.
14067** May you share freely, never taking more than you give.
14068**
14069*************************************************************************
14070** This is the header file for the generic hash-table implementation
14071** used in SQLite.
14072*/
14073#ifndef SQLITE_HASH_H
14074#define SQLITE_HASH_H
14075
14076/* Forward declarations of structures. */
14077typedef struct Hash Hash;
14078typedef struct HashElem HashElem;
14079
14080/* A complete hash table is an instance of the following structure.
14081** The internals of this structure are intended to be opaque -- client
14082** code should not attempt to access or modify the fields of this structure
14083** directly. Change this structure only by using the routines below.
14084** However, some of the "procedures" and "functions" for modifying and
14085** accessing this structure are really macros, so we can't really make
14086** this structure opaque.
14087**
14088** All elements of the hash table are on a single doubly-linked list.
14089** Hash.first points to the head of this list.
14090**
14091** There are Hash.htsize buckets. Each bucket points to a spot in
14092** the global doubly-linked list. The contents of the bucket are the
14093** element pointed to plus the next _ht.count-1 elements in the list.
14094**
14095** Hash.htsize and Hash.ht may be zero. In that case lookup is done
14096** by a linear search of the global list. For small tables, the
14097** Hash.ht table is never allocated because if there are few elements
14098** in the table, it is faster to do a linear search than to manage
14099** the hash table.
14100*/
14101struct Hash {
14102 unsigned int htsize; /* Number of buckets in the hash table */
14103 unsigned int count; /* Number of entries in this table */
14104 HashElem *first; /* The first element of the array */
14105 struct _ht { /* the hash table */
14106 unsigned int count; /* Number of entries with this hash */
14107 HashElem *chain; /* Pointer to first entry with this hash */
14108 } *ht;
14109};
14110
14111/* Each element in the hash table is an instance of the following
14112** structure. All elements are stored on a single doubly-linked list.
14113**
14114** Again, this structure is intended to be opaque, but it can't really
14115** be opaque because it is used by macros.
14116*/
14117struct HashElem {
14118 HashElem *next, *prev; /* Next and previous elements in the table */
14119 void *data; /* Data associated with this element */
14120 const char *pKey; /* Key associated with this element */
14121};
14122
14123/*
14124** Access routines. To delete, insert a NULL pointer.
14125*/
14126SQLITE_PRIVATE void sqlite3HashInit(Hash*);
14127SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
14128SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
14129SQLITE_PRIVATE void sqlite3HashClear(Hash*);
14130
14131/*
14132** Macros for looping over all elements of a hash table. The idiom is
14133** like this:
14134**
14135** Hash h;
14136** HashElem *p;
14137** ...
14138** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
14139** SomeStructure *pData = sqliteHashData(p);
14140** // do something with pData
14141** }
14142*/
14143#define sqliteHashFirst(H) ((H)->first)
14144#define sqliteHashNext(E) ((E)->next)
14145#define sqliteHashData(E) ((E)->data)
14146/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14147/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14148
14149/*
14150** Number of entries in a hash table
14151*/
14152#define sqliteHashCount(H) ((H)->count)
14153
14154#endif /* SQLITE_HASH_H */
14155
14156/************** End of hash.h ************************************************/
14157/************** Continuing where we left off in sqliteInt.h ******************/
14158/************** Include parse.h in the middle of sqliteInt.h *****************/
14159/************** Begin file parse.h *******************************************/
14160#define TK_SEMI 1
14161#define TK_EXPLAIN 2
14162#define TK_QUERY 3
14163#define TK_PLAN 4
14164#define TK_BEGIN 5
14165#define TK_TRANSACTION 6
14166#define TK_DEFERRED 7
14167#define TK_IMMEDIATE 8
14168#define TK_EXCLUSIVE 9
14169#define TK_COMMIT 10
14170#define TK_END 11
14171#define TK_ROLLBACK 12
14172#define TK_SAVEPOINT 13
14173#define TK_RELEASE 14
14174#define TK_TO 15
14175#define TK_TABLE 16
14176#define TK_CREATE 17
14177#define TK_IF 18
14178#define TK_NOT 19
14179#define TK_EXISTS 20
14180#define TK_TEMP 21
14181#define TK_LP 22
14182#define TK_RP 23
14183#define TK_AS 24
14184#define TK_COMMA 25
14185#define TK_WITHOUT 26
14186#define TK_ABORT 27
14187#define TK_ACTION 28
14188#define TK_AFTER 29
14189#define TK_ANALYZE 30
14190#define TK_ASC 31
14191#define TK_ATTACH 32
14192#define TK_BEFORE 33
14193#define TK_BY 34
14194#define TK_CASCADE 35
14195#define TK_CAST 36
14196#define TK_CONFLICT 37
14197#define TK_DATABASE 38
14198#define TK_DESC 39
14199#define TK_DETACH 40
14200#define TK_EACH 41
14201#define TK_FAIL 42
14202#define TK_OR 43
14203#define TK_AND 44
14204#define TK_IS 45
14205#define TK_MATCH 46
14206#define TK_LIKE_KW 47
14207#define TK_BETWEEN 48
14208#define TK_IN 49
14209#define TK_ISNULL 50
14210#define TK_NOTNULL 51
14211#define TK_NE 52
14212#define TK_EQ 53
14213#define TK_GT 54
14214#define TK_LE 55
14215#define TK_LT 56
14216#define TK_GE 57
14217#define TK_ESCAPE 58
14218#define TK_ID 59
14219#define TK_COLUMNKW 60
14220#define TK_DO 61
14221#define TK_FOR 62
14222#define TK_IGNORE 63
14223#define TK_INITIALLY 64
14224#define TK_INSTEAD 65
14225#define TK_NO 66
14226#define TK_KEY 67
14227#define TK_OF 68
14228#define TK_OFFSET 69
14229#define TK_PRAGMA 70
14230#define TK_RAISE 71
14231#define TK_RECURSIVE 72
14232#define TK_REPLACE 73
14233#define TK_RESTRICT 74
14234#define TK_ROW 75
14235#define TK_ROWS 76
14236#define TK_TRIGGER 77
14237#define TK_VACUUM 78
14238#define TK_VIEW 79
14239#define TK_VIRTUAL 80
14240#define TK_WITH 81
14241#define TK_NULLS 82
14242#define TK_FIRST 83
14243#define TK_LAST 84
14244#define TK_CURRENT 85
14245#define TK_FOLLOWING 86
14246#define TK_PARTITION 87
14247#define TK_PRECEDING 88
14248#define TK_RANGE 89
14249#define TK_UNBOUNDED 90
14250#define TK_EXCLUDE 91
14251#define TK_GROUPS 92
14252#define TK_OTHERS 93
14253#define TK_TIES 94
14254#define TK_GENERATED 95
14255#define TK_ALWAYS 96
14256#define TK_MATERIALIZED 97
14257#define TK_REINDEX 98
14258#define TK_RENAME 99
14259#define TK_CTIME_KW 100
14260#define TK_ANY 101
14261#define TK_BITAND 102
14262#define TK_BITOR 103
14263#define TK_LSHIFT 104
14264#define TK_RSHIFT 105
14265#define TK_PLUS 106
14266#define TK_MINUS 107
14267#define TK_STAR 108
14268#define TK_SLASH 109
14269#define TK_REM 110
14270#define TK_CONCAT 111
14271#define TK_PTR 112
14272#define TK_COLLATE 113
14273#define TK_BITNOT 114
14274#define TK_ON 115
14275#define TK_INDEXED 116
14276#define TK_STRING 117
14277#define TK_JOIN_KW 118
14278#define TK_CONSTRAINT 119
14279#define TK_DEFAULT 120
14280#define TK_NULL 121
14281#define TK_PRIMARY 122
14282#define TK_UNIQUE 123
14283#define TK_CHECK 124
14284#define TK_REFERENCES 125
14285#define TK_AUTOINCR 126
14286#define TK_INSERT 127
14287#define TK_DELETE 128
14288#define TK_UPDATE 129
14289#define TK_SET 130
14290#define TK_DEFERRABLE 131
14291#define TK_FOREIGN 132
14292#define TK_DROP 133
14293#define TK_UNION 134
14294#define TK_ALL 135
14295#define TK_EXCEPT 136
14296#define TK_INTERSECT 137
14297#define TK_SELECT 138
14298#define TK_VALUES 139
14299#define TK_DISTINCT 140
14300#define TK_DOT 141
14301#define TK_FROM 142
14302#define TK_JOIN 143
14303#define TK_USING 144
14304#define TK_ORDER 145
14305#define TK_GROUP 146
14306#define TK_HAVING 147
14307#define TK_LIMIT 148
14308#define TK_WHERE 149
14309#define TK_RETURNING 150
14310#define TK_INTO 151
14311#define TK_NOTHING 152
14312#define TK_FLOAT 153
14313#define TK_BLOB 154
14314#define TK_INTEGER 155
14315#define TK_VARIABLE 156
14316#define TK_CASE 157
14317#define TK_WHEN 158
14318#define TK_THEN 159
14319#define TK_ELSE 160
14320#define TK_INDEX 161
14321#define TK_ALTER 162
14322#define TK_ADD 163
14323#define TK_WINDOW 164
14324#define TK_OVER 165
14325#define TK_FILTER 166
14326#define TK_COLUMN 167
14327#define TK_AGG_FUNCTION 168
14328#define TK_AGG_COLUMN 169
14329#define TK_TRUEFALSE 170
14330#define TK_ISNOT 171
14331#define TK_FUNCTION 172
14332#define TK_UMINUS 173
14333#define TK_UPLUS 174
14334#define TK_TRUTH 175
14335#define TK_REGISTER 176
14336#define TK_VECTOR 177
14337#define TK_SELECT_COLUMN 178
14338#define TK_IF_NULL_ROW 179
14339#define TK_ASTERISK 180
14340#define TK_SPAN 181
14341#define TK_ERROR 182
14342#define TK_SPACE 183
14343#define TK_ILLEGAL 184
14344
14345/************** End of parse.h ***********************************************/
14346/************** Continuing where we left off in sqliteInt.h ******************/
14347#include <stdio.h>
14348#include <stdlib.h>
14349#include <string.h>
14350#include <assert.h>
14351#include <stddef.h>
14352
14353/*
14354** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
14355** This allows better measurements of where memcpy() is used when running
14356** cachegrind. But this macro version of memcpy() is very slow so it
14357** should not be used in production. This is a performance measurement
14358** hack only.
14359*/
14360#ifdef SQLITE_INLINE_MEMCPY
14361# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
14362 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14363#endif
14364
14365/*
14366** If compiling for a processor that lacks floating point support,
14367** substitute integer for floating-point
14368*/
14369#ifdef SQLITE_OMIT_FLOATING_POINT
14370# define double sqlite_int64
14371# define float sqlite_int64
14372# define LONGDOUBLE_TYPE sqlite_int64
14373# ifndef SQLITE_BIG_DBL
14374# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
14375# endif
14376# define SQLITE_OMIT_DATETIME_FUNCS 1
14377# define SQLITE_OMIT_TRACE 1
14378# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
14379# undef SQLITE_HAVE_ISNAN
14380#endif
14381#ifndef SQLITE_BIG_DBL
14382# define SQLITE_BIG_DBL (1e99)
14383#endif
14384
14385/*
14386** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
14387** afterward. Having this macro allows us to cause the C compiler
14388** to omit code used by TEMP tables without messy #ifndef statements.
14389*/
14390#ifdef SQLITE_OMIT_TEMPDB
14391#define OMIT_TEMPDB 1
14392#else
14393#define OMIT_TEMPDB 0
14394#endif
14395
14396/*
14397** The "file format" number is an integer that is incremented whenever
14398** the VDBE-level file format changes. The following macros define the
14399** the default file format for new databases and the maximum file format
14400** that the library can read.
14401*/
14402#define SQLITE_MAX_FILE_FORMAT 4
14403#ifndef SQLITE_DEFAULT_FILE_FORMAT
14404# define SQLITE_DEFAULT_FILE_FORMAT 4
14405#endif
14406
14407/*
14408** Determine whether triggers are recursive by default. This can be
14409** changed at run-time using a pragma.
14410*/
14411#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
14412# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
14413#endif
14414
14415/*
14416** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
14417** on the command-line
14418*/
14419#ifndef SQLITE_TEMP_STORE
14420# define SQLITE_TEMP_STORE 1
14421#endif
14422
14423/*
14424** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
14425** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
14426** to zero.
14427*/
14428#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
14429# undef SQLITE_MAX_WORKER_THREADS
14430# define SQLITE_MAX_WORKER_THREADS 0
14431#endif
14432#ifndef SQLITE_MAX_WORKER_THREADS
14433# define SQLITE_MAX_WORKER_THREADS 8
14434#endif
14435#ifndef SQLITE_DEFAULT_WORKER_THREADS
14436# define SQLITE_DEFAULT_WORKER_THREADS 0
14437#endif
14438#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
14439# undef SQLITE_MAX_WORKER_THREADS
14440# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
14441#endif
14442
14443/*
14444** The default initial allocation for the pagecache when using separate
14445** pagecaches for each database connection. A positive number is the
14446** number of pages. A negative number N translations means that a buffer
14447** of -1024*N bytes is allocated and used for as many pages as it will hold.
14448**
14449** The default value of "20" was chosen to minimize the run-time of the
14450** speedtest1 test program with options: --shrink-memory --reprepare
14451*/
14452#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
14453# define SQLITE_DEFAULT_PCACHE_INITSZ 20
14454#endif
14455
14456/*
14457** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
14458*/
14459#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
14460# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
14461#endif
14462
14463/*
14464** The compile-time options SQLITE_MMAP_READWRITE and
14465** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
14466** You must choose one or the other (or neither) but not both.
14467*/
14468#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
14469#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
14470#endif
14471
14472/*
14473** GCC does not define the offsetof() macro so we'll have to do it
14474** ourselves.
14475*/
14476#ifndef offsetof
14477#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14478#endif
14479
14480/*
14481** Macros to compute minimum and maximum of two numbers.
14482*/
14483#ifndef MIN
14484# define MIN(A,B) ((A)<(B)?(A):(B))
14485#endif
14486#ifndef MAX
14487# define MAX(A,B) ((A)>(B)?(A):(B))
14488#endif
14489
14490/*
14491** Swap two objects of type TYPE.
14492*/
14493#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
14494
14495/*
14496** Check to see if this machine uses EBCDIC. (Yes, believe it or
14497** not, there are still machines out there that use EBCDIC.)
14498*/
14499#if 'A' == '\301'
14500# define SQLITE_EBCDIC 1
14501#else
14502# define SQLITE_ASCII 1
14503#endif
14504
14505/*
14506** Integers of known sizes. These typedefs might change for architectures
14507** where the sizes very. Preprocessor macros are available so that the
14508** types can be conveniently redefined at compile-type. Like this:
14509**
14510** cc '-DUINTPTR_TYPE=long long int' ...
14511*/
14512#ifndef UINT32_TYPE
14513# ifdef HAVE_UINT32_T
14514# define UINT32_TYPE uint32_t
14515# else
14516# define UINT32_TYPE unsigned int
14517# endif
14518#endif
14519#ifndef UINT16_TYPE
14520# ifdef HAVE_UINT16_T
14521# define UINT16_TYPE uint16_t
14522# else
14523# define UINT16_TYPE unsigned short int
14524# endif
14525#endif
14526#ifndef INT16_TYPE
14527# ifdef HAVE_INT16_T
14528# define INT16_TYPE int16_t
14529# else
14530# define INT16_TYPE short int
14531# endif
14532#endif
14533#ifndef UINT8_TYPE
14534# ifdef HAVE_UINT8_T
14535# define UINT8_TYPE uint8_t
14536# else
14537# define UINT8_TYPE unsigned char
14538# endif
14539#endif
14540#ifndef INT8_TYPE
14541# ifdef HAVE_INT8_T
14542# define INT8_TYPE int8_t
14543# else
14544# define INT8_TYPE signed char
14545# endif
14546#endif
14547#ifndef LONGDOUBLE_TYPE
14548# define LONGDOUBLE_TYPE long double
14549#endif
14550typedef sqlite_int64 i64; /* 8-byte signed integer */
14551typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14552typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14553typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14554typedef INT16_TYPE i16; /* 2-byte signed integer */
14555typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14556typedef INT8_TYPE i8; /* 1-byte signed integer */
14557
14558/*
14559** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
14560** that can be stored in a u32 without loss of data. The value
14561** is 0x00000000ffffffff. But because of quirks of some compilers, we
14562** have to specify the value in the less intuitive manner shown:
14563*/
14564#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14565
14566/*
14567** The datatype used to store estimates of the number of rows in a
14568** table or index.
14569*/
14570typedef u64 tRowcnt;
14571
14572/*
14573** Estimated quantities used for query planning are stored as 16-bit
14574** logarithms. For quantity X, the value stored is 10*log2(X). This
14575** gives a possible range of values of approximately 1.0e986 to 1e-986.
14576** But the allowed values are "grainy". Not every value is representable.
14577** For example, quantities 16 and 17 are both represented by a LogEst
14578** of 40. However, since LogEst quantities are suppose to be estimates,
14579** not exact values, this imprecision is not a problem.
14580**
14581** "LogEst" is short for "Logarithmic Estimate".
14582**
14583** Examples:
14584** 1 -> 0 20 -> 43 10000 -> 132
14585** 2 -> 10 25 -> 46 25000 -> 146
14586** 3 -> 16 100 -> 66 1000000 -> 199
14587** 4 -> 20 1000 -> 99 1048576 -> 200
14588** 10 -> 33 1024 -> 100 4294967296 -> 320
14589**
14590** The LogEst can be negative to indicate fractional values.
14591** Examples:
14592**
14593** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14594*/
14595typedef INT16_TYPE LogEst;
14596
14597/*
14598** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
14599*/
14600#ifndef SQLITE_PTRSIZE
14601# if defined(__SIZEOF_POINTER__)
14602# define SQLITE_PTRSIZE __SIZEOF_POINTER__
14603# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14604 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
14605 (defined(__APPLE__) && defined(__POWERPC__)) || \
14606 (defined(__TOS_AIX__) && !defined(__64BIT__))
14607# define SQLITE_PTRSIZE 4
14608# else
14609# define SQLITE_PTRSIZE 8
14610# endif
14611#endif
14612
14613/* The uptr type is an unsigned integer large enough to hold a pointer
14614*/
14615#if defined(HAVE_STDINT_H)
14616 typedef uintptr_t uptr;
14617#elif SQLITE_PTRSIZE==4
14618 typedef u32 uptr;
14619#else
14620 typedef u64 uptr;
14621#endif
14622
14623/*
14624** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
14625** something between S (inclusive) and E (exclusive).
14626**
14627** In other words, S is a buffer and E is a pointer to the first byte after
14628** the end of buffer S. This macro returns true if P points to something
14629** contained within the buffer S.
14630*/
14631#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
14632
14633/*
14634** P is one byte past the end of a large buffer. Return true if a span of bytes
14635** between S..E crosses the end of that buffer. In other words, return true
14636** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
14637**
14638** S is the start of the span. E is one byte past the end of end of span.
14639**
14640** P
14641** |-----------------| FALSE
14642** |-------|
14643** S E
14644**
14645** P
14646** |-----------------|
14647** |-------| TRUE
14648** S E
14649**
14650** P
14651** |-----------------|
14652** |-------| FALSE
14653** S E
14654*/
14655#define SQLITE_OVERFLOW(P,S,E) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P)))
14656
14657/*
14658** Macros to determine whether the machine is big or little endian,
14659** and whether or not that determination is run-time or compile-time.
14660**
14661** For best performance, an attempt is made to guess at the byte-order
14662** using C-preprocessor macros. If that is unsuccessful, or if
14663** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14664** at run-time.
14665*/
14666#ifndef SQLITE_BYTEORDER
14667# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14668 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
14669 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
14670 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14671# define SQLITE_BYTEORDER 1234
14672# elif defined(sparc) || defined(__ppc__) || \
14673 defined(__ARMEB__) || defined(__AARCH64EB__)
14674# define SQLITE_BYTEORDER 4321
14675# else
14676# define SQLITE_BYTEORDER 0
14677# endif
14678#endif
14679#if SQLITE_BYTEORDER==4321
14680# define SQLITE_BIGENDIAN 1
14681# define SQLITE_LITTLEENDIAN 0
14682# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
14683#elif SQLITE_BYTEORDER==1234
14684# define SQLITE_BIGENDIAN 0
14685# define SQLITE_LITTLEENDIAN 1
14686# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
14687#else
14688# ifdef SQLITE_AMALGAMATION
14689 const int sqlite3one = 1;
14690# else
14691 extern const int sqlite3one;
14692# endif
14693# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
14694# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
14695# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
14696#endif
14697
14698/*
14699** Constants for the largest and smallest possible 64-bit signed integers.
14700** These macros are designed to work correctly on both 32-bit and 64-bit
14701** compilers.
14702*/
14703#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
14704#define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32))
14705#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14706
14707/*
14708** Round up a number to the next larger multiple of 8. This is used
14709** to force 8-byte alignment on 64-bit architectures.
14710**
14711** ROUND8() always does the rounding, for any argument.
14712**
14713** ROUND8P() assumes that the argument is already an integer number of
14714** pointers in size, and so it is a no-op on systems where the pointer
14715** size is 8.
14716*/
14717#define ROUND8(x) (((x)+7)&~7)
14718#if SQLITE_PTRSIZE==8
14719# define ROUND8P(x) (x)
14720#else
14721# define ROUND8P(x) (((x)+7)&~7)
14722#endif
14723
14724/*
14725** Round down to the nearest multiple of 8
14726*/
14727#define ROUNDDOWN8(x) ((x)&~7)
14728
14729/*
14730** Assert that the pointer X is aligned to an 8-byte boundary. This
14731** macro is used only within assert() to verify that the code gets
14732** all alignment restrictions correct.
14733**
14734** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
14735** underlying malloc() implementation might return us 4-byte aligned
14736** pointers. In that case, only verify 4-byte alignment.
14737*/
14738#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
14739# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
14740#else
14741# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
14742#endif
14743
14744/*
14745** Disable MMAP on platforms where it is known to not work
14746*/
14747#if defined(__OpenBSD__) || defined(__QNXNTO__)
14748# undef SQLITE_MAX_MMAP_SIZE
14749# define SQLITE_MAX_MMAP_SIZE 0
14750#endif
14751
14752/*
14753** Default maximum size of memory used by memory-mapped I/O in the VFS
14754*/
14755#ifdef __APPLE__
14756# include <TargetConditionals.h>
14757#endif
14758#ifndef SQLITE_MAX_MMAP_SIZE
14759# if defined(__linux__) \
14760 || defined(_WIN32) \
14761 || (defined(__APPLE__) && defined(__MACH__)) \
14762 || defined(__sun) \
14763 || defined(__FreeBSD__) \
14764 || defined(__DragonFly__)
14765# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
14766# else
14767# define SQLITE_MAX_MMAP_SIZE 0
14768# endif
14769#endif
14770
14771/*
14772** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
14773** default MMAP_SIZE is specified at compile-time, make sure that it does
14774** not exceed the maximum mmap size.
14775*/
14776#ifndef SQLITE_DEFAULT_MMAP_SIZE
14777# define SQLITE_DEFAULT_MMAP_SIZE 0
14778#endif
14779#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
14780# undef SQLITE_DEFAULT_MMAP_SIZE
14781# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
14782#endif
14783
14784/*
14785** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not
14786** the Abstract Syntax Tree tracing logic is turned on.
14787*/
14788#if !defined(SQLITE_AMALGAMATION)
14789SQLITE_PRIVATE u32 sqlite3TreeTrace;
14790#endif
14791#if defined(SQLITE_DEBUG) \
14792 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
14793 || defined(SQLITE_ENABLE_TREETRACE))
14794# define TREETRACE_ENABLED 1
14795# define TREETRACE(K,P,S,X) \
14796 if(sqlite3TreeTrace&(K)) \
14797 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14798 sqlite3DebugPrintf X
14799#else
14800# define TREETRACE(K,P,S,X)
14801# define TREETRACE_ENABLED 0
14802#endif
14803
14804/* TREETRACE flag meanings:
14805**
14806** 0x00000001 Beginning and end of SELECT processing
14807** 0x00000002 WHERE clause processing
14808** 0x00000004 Query flattener
14809** 0x00000008 Result-set wildcard expansion
14810** 0x00000010 Query name resolution
14811** 0x00000020 Aggregate analysis
14812** 0x00000040 Window functions
14813** 0x00000080 Generated column names
14814** 0x00000100 Move HAVING terms into WHERE
14815** 0x00000200 Count-of-view optimization
14816** 0x00000400 Compound SELECT processing
14817** 0x00000800 Drop superfluous ORDER BY
14818** 0x00001000 LEFT JOIN simplifies to JOIN
14819** 0x00002000 Constant propagation
14820** 0x00004000 Push-down optimization
14821** 0x00008000 After all FROM-clause analysis
14822** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
14823** 0x00020000 Transform DISTINCT into GROUP BY
14824** 0x00040000 SELECT tree dump after all code has been generated
14825*/
14826
14827/*
14828** Macros for "wheretrace"
14829*/
14830SQLITE_PRIVATE u32 sqlite3WhereTrace;
14831#if defined(SQLITE_DEBUG) \
14832 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
14833# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14834# define WHERETRACE_ENABLED 1
14835#else
14836# define WHERETRACE(K,X)
14837#endif
14838
14839/*
14840** Bits for the sqlite3WhereTrace mask:
14841**
14842** (---any--) Top-level block structure
14843** 0x-------F High-level debug messages
14844** 0x----FFF- More detail
14845** 0xFFFF---- Low-level debug messages
14846**
14847** 0x00000001 Code generation
14848** 0x00000002 Solver
14849** 0x00000004 Solver costs
14850** 0x00000008 WhereLoop inserts
14851**
14852** 0x00000010 Display sqlite3_index_info xBestIndex calls
14853** 0x00000020 Range an equality scan metrics
14854** 0x00000040 IN operator decisions
14855** 0x00000080 WhereLoop cost adjustements
14856** 0x00000100
14857** 0x00000200 Covering index decisions
14858** 0x00000400 OR optimization
14859** 0x00000800 Index scanner
14860** 0x00001000 More details associated with code generation
14861** 0x00002000
14862** 0x00004000 Show all WHERE terms at key points
14863** 0x00008000 Show the full SELECT statement at key places
14864**
14865** 0x00010000 Show more detail when printing WHERE terms
14866** 0x00020000 Show WHERE terms returned from whereScanNext()
14867*/
14868
14869
14870/*
14871** An instance of the following structure is used to store the busy-handler
14872** callback for a given sqlite handle.
14873**
14874** The sqlite.busyHandler member of the sqlite struct contains the busy
14875** callback for the database handle. Each pager opened via the sqlite
14876** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14877** callback is currently invoked only from within pager.c.
14878*/
14879typedef struct BusyHandler BusyHandler;
14880struct BusyHandler {
14881 int (*xBusyHandler)(void *,int); /* The busy callback */
14882 void *pBusyArg; /* First arg to busy callback */
14883 int nBusy; /* Incremented with each busy call */
14884};
14885
14886/*
14887** Name of table that holds the database schema.
14888**
14889** The PREFERRED names are used wherever possible. But LEGACY is also
14890** used for backwards compatibility.
14891**
14892** 1. Queries can use either the PREFERRED or the LEGACY names
14893** 2. The sqlite3_set_authorizer() callback uses the LEGACY name
14894** 3. The PRAGMA table_list statement uses the PREFERRED name
14895**
14896** The LEGACY names are stored in the internal symbol hash table
14897** in support of (2). Names are translated using sqlite3PreferredTableName()
14898** for (3). The sqlite3FindTable() function takes care of translating
14899** names for (1).
14900**
14901** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema".
14902*/
14903#define LEGACY_SCHEMA_TABLE "sqlite_master"
14904#define LEGACY_TEMP_SCHEMA_TABLE "sqlite_temp_master"
14905#define PREFERRED_SCHEMA_TABLE "sqlite_schema"
14906#define PREFERRED_TEMP_SCHEMA_TABLE "sqlite_temp_schema"
14907
14908
14909/*
14910** The root-page of the schema table.
14911*/
14912#define SCHEMA_ROOT 1
14913
14914/*
14915** The name of the schema table. The name is different for TEMP.
14916*/
14917#define SCHEMA_TABLE(x) \
14918 ((!OMIT_TEMPDB)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE:LEGACY_SCHEMA_TABLE)
14919
14920/*
14921** A convenience macro that returns the number of elements in
14922** an array.
14923*/
14924#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
14925
14926/*
14927** Determine if the argument is a power of two
14928*/
14929#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14930
14931/*
14932** The following value as a destructor means to use sqlite3DbFree().
14933** The sqlite3DbFree() routine requires two parameters instead of the
14934** one parameter that destructors normally want. So we have to introduce
14935** this magic value that the code knows to handle differently. Any
14936** pointer will work here as long as it is distinct from SQLITE_STATIC
14937** and SQLITE_TRANSIENT.
14938*/
14939#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3OomClear)
14940
14941/*
14942** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14943** not support Writable Static Data (WSD) such as global and static variables.
14944** All variables must either be on the stack or dynamically allocated from
14945** the heap. When WSD is unsupported, the variable declarations scattered
14946** throughout the SQLite code must become constants instead. The SQLITE_WSD
14947** macro is used for this purpose. And instead of referencing the variable
14948** directly, we use its constant as a key to lookup the run-time allocated
14949** buffer that holds real variable. The constant is also the initializer
14950** for the run-time allocated buffer.
14951**
14952** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14953** macros become no-ops and have zero performance impact.
14954*/
14955#ifdef SQLITE_OMIT_WSD
14956 #define SQLITE_WSD const
14957 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14958 #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
14959SQLITE_API int sqlite3_wsd_init(int N, int J);
14960SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14961#else
14962 #define SQLITE_WSD
14963 #define GLOBAL(t,v) v
14964 #define sqlite3GlobalConfig sqlite3Config
14965#endif
14966
14967/*
14968** The following macros are used to suppress compiler warnings and to
14969** make it clear to human readers when a function parameter is deliberately
14970** left unused within the body of a function. This usually happens when
14971** a function is called via a function pointer. For example the
14972** implementation of an SQL aggregate step callback may not use the
14973** parameter indicating the number of arguments passed to the aggregate,
14974** if it knows that this is enforced elsewhere.
14975**
14976** When a function parameter is not used at all within the body of a function,
14977** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14978** However, these macros may also be used to suppress warnings related to
14979** parameters that may or may not be used depending on compilation options.
14980** For example those parameters only used in assert() statements. In these
14981** cases the parameters are named as per the usual conventions.
14982*/
14983#define UNUSED_PARAMETER(x) (void)(x)
14984#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
14985
14986/*
14987** Forward references to structures
14988*/
14989typedef struct AggInfo AggInfo;
14990typedef struct AuthContext AuthContext;
14991typedef struct AutoincInfo AutoincInfo;
14992typedef struct Bitvec Bitvec;
14993typedef struct CollSeq CollSeq;
14994typedef struct Column Column;
14995typedef struct Cte Cte;
14996typedef struct CteUse CteUse;
14997typedef struct Db Db;
14998typedef struct DbFixer DbFixer;
14999typedef struct Schema Schema;
15000typedef struct Expr Expr;
15001typedef struct ExprList ExprList;
15002typedef struct FKey FKey;
15003typedef struct FpDecode FpDecode;
15004typedef struct FuncDestructor FuncDestructor;
15005typedef struct FuncDef FuncDef;
15006typedef struct FuncDefHash FuncDefHash;
15007typedef struct IdList IdList;
15008typedef struct Index Index;
15009typedef struct IndexedExpr IndexedExpr;
15010typedef struct IndexSample IndexSample;
15011typedef struct KeyClass KeyClass;
15012typedef struct KeyInfo KeyInfo;
15013typedef struct Lookaside Lookaside;
15014typedef struct LookasideSlot LookasideSlot;
15015typedef struct Module Module;
15016typedef struct NameContext NameContext;
15017typedef struct OnOrUsing OnOrUsing;
15018typedef struct Parse Parse;
15019typedef struct ParseCleanup ParseCleanup;
15020typedef struct PreUpdate PreUpdate;
15021typedef struct PrintfArguments PrintfArguments;
15022typedef struct RCStr RCStr;
15023typedef struct RenameToken RenameToken;
15024typedef struct Returning Returning;
15025typedef struct RowSet RowSet;
15026typedef struct Savepoint Savepoint;
15027typedef struct Select Select;
15028typedef struct SQLiteThread SQLiteThread;
15029typedef struct SelectDest SelectDest;
15030typedef struct SrcItem SrcItem;
15031typedef struct SrcList SrcList;
15032typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
15033typedef struct Table Table;
15034typedef struct TableLock TableLock;
15035typedef struct Token Token;
15036typedef struct TreeView TreeView;
15037typedef struct Trigger Trigger;
15038typedef struct TriggerPrg TriggerPrg;
15039typedef struct TriggerStep TriggerStep;
15040typedef struct UnpackedRecord UnpackedRecord;
15041typedef struct Upsert Upsert;
15042typedef struct VTable VTable;
15043typedef struct VtabCtx VtabCtx;
15044typedef struct Walker Walker;
15045typedef struct WhereInfo WhereInfo;
15046typedef struct Window Window;
15047typedef struct With With;
15048
15049
15050/*
15051** The bitmask datatype defined below is used for various optimizations.
15052**
15053** Changing this from a 64-bit to a 32-bit type limits the number of
15054** tables in a join to 32 instead of 64. But it also reduces the size
15055** of the library by 738 bytes on ix86.
15056*/
15057#ifdef SQLITE_BITMASK_TYPE
15058 typedef SQLITE_BITMASK_TYPE Bitmask;
15059#else
15060 typedef u64 Bitmask;
15061#endif
15062
15063/*
15064** The number of bits in a Bitmask. "BMS" means "BitMask Size".
15065*/
15066#define BMS ((int)(sizeof(Bitmask)*8))
15067
15068/*
15069** A bit in a Bitmask
15070*/
15071#define MASKBIT(n) (((Bitmask)1)<<(n))
15072#define MASKBIT64(n) (((u64)1)<<(n))
15073#define MASKBIT32(n) (((unsigned int)1)<<(n))
15074#define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0)
15075#define ALLBITS ((Bitmask)-1)
15076#define TOPBIT (((Bitmask)1)<<(BMS-1))
15077
15078/* A VList object records a mapping between parameters/variables/wildcards
15079** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
15080** variable number associated with that parameter. See the format description
15081** on the sqlite3VListAdd() routine for more information. A VList is really
15082** just an array of integers.
15083*/
15084typedef int VList;
15085
15086/*
15087** Defer sourcing vdbe.h and btree.h until after the "u8" and
15088** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
15089** pointer types (i.e. FuncDef) defined above.
15090*/
15091/************** Include os.h in the middle of sqliteInt.h ********************/
15092/************** Begin file os.h **********************************************/
15093/*
15094** 2001 September 16
15095**
15096** The author disclaims copyright to this source code. In place of
15097** a legal notice, here is a blessing:
15098**
15099** May you do good and not evil.
15100** May you find forgiveness for yourself and forgive others.
15101** May you share freely, never taking more than you give.
15102**
15103******************************************************************************
15104**
15105** This header file (together with is companion C source-code file
15106** "os.c") attempt to abstract the underlying operating system so that
15107** the SQLite library will work on both POSIX and windows systems.
15108**
15109** This header file is #include-ed by sqliteInt.h and thus ends up
15110** being included by every source file.
15111*/
15112#ifndef _SQLITE_OS_H_
15113#define _SQLITE_OS_H_
15114
15115/*
15116** Attempt to automatically detect the operating system and setup the
15117** necessary pre-processor macros for it.
15118*/
15119/************** Include os_setup.h in the middle of os.h *********************/
15120/************** Begin file os_setup.h ****************************************/
15121/*
15122** 2013 November 25
15123**
15124** The author disclaims copyright to this source code. In place of
15125** a legal notice, here is a blessing:
15126**
15127** May you do good and not evil.
15128** May you find forgiveness for yourself and forgive others.
15129** May you share freely, never taking more than you give.
15130**
15131******************************************************************************
15132**
15133** This file contains pre-processor directives related to operating system
15134** detection and/or setup.
15135*/
15136#ifndef SQLITE_OS_SETUP_H
15137#define SQLITE_OS_SETUP_H
15138
15139/*
15140** Figure out if we are dealing with Unix, Windows, or some other operating
15141** system.
15142**
15143** After the following block of preprocess macros, all of
15144**
15145** SQLITE_OS_KV
15146** SQLITE_OS_OTHER
15147** SQLITE_OS_UNIX
15148** SQLITE_OS_WIN
15149**
15150** will defined to either 1 or 0. One of them will be 1. The others will be 0.
15151** If none of the macros are initially defined, then select either
15152** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform.
15153**
15154** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15155** must provide its own VFS implementation together with sqlite3_os_init()
15156** and sqlite3_os_end() routines.
15157*/
15158#if !defined(SQLITE_OS_KV) && !defined(SQLITE_OS_OTHER) && \
15159 !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_WIN)
15160# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15161 defined(__MINGW32__) || defined(__BORLANDC__)
15162# define SQLITE_OS_WIN 1
15163# define SQLITE_OS_UNIX 0
15164# else
15165# define SQLITE_OS_WIN 0
15166# define SQLITE_OS_UNIX 1
15167# endif
15168#endif
15169#if SQLITE_OS_OTHER+1>1
15170# undef SQLITE_OS_KV
15171# define SQLITE_OS_KV 0
15172# undef SQLITE_OS_UNIX
15173# define SQLITE_OS_UNIX 0
15174# undef SQLITE_OS_WIN
15175# define SQLITE_OS_WIN 0
15176#endif
15177#if SQLITE_OS_KV+1>1
15178# undef SQLITE_OS_OTHER
15179# define SQLITE_OS_OTHER 0
15180# undef SQLITE_OS_UNIX
15181# define SQLITE_OS_UNIX 0
15182# undef SQLITE_OS_WIN
15183# define SQLITE_OS_WIN 0
15184# define SQLITE_OMIT_LOAD_EXTENSION 1
15185# define SQLITE_OMIT_WAL 1
15186# define SQLITE_OMIT_DEPRECATED 1
15187# undef SQLITE_TEMP_STORE
15188# define SQLITE_TEMP_STORE 3 /* Always use memory for temporary storage */
15189# define SQLITE_DQS 0
15190# define SQLITE_OMIT_SHARED_CACHE 1
15191# define SQLITE_OMIT_AUTOINIT 1
15192#endif
15193#if SQLITE_OS_UNIX+1>1
15194# undef SQLITE_OS_KV
15195# define SQLITE_OS_KV 0
15196# undef SQLITE_OS_OTHER
15197# define SQLITE_OS_OTHER 0
15198# undef SQLITE_OS_WIN
15199# define SQLITE_OS_WIN 0
15200#endif
15201#if SQLITE_OS_WIN+1>1
15202# undef SQLITE_OS_KV
15203# define SQLITE_OS_KV 0
15204# undef SQLITE_OS_OTHER
15205# define SQLITE_OS_OTHER 0
15206# undef SQLITE_OS_UNIX
15207# define SQLITE_OS_UNIX 0
15208#endif
15209
15210
15211#endif /* SQLITE_OS_SETUP_H */
15212
15213/************** End of os_setup.h ********************************************/
15214/************** Continuing where we left off in os.h *************************/
15215
15216/* If the SET_FULLSYNC macro is not defined above, then make it
15217** a no-op
15218*/
15219#ifndef SET_FULLSYNC
15220# define SET_FULLSYNC(x,y)
15221#endif
15222
15223/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
15224*/
15225#ifndef SQLITE_MAX_PATHLEN
15226# define SQLITE_MAX_PATHLEN FILENAME_MAX
15227#endif
15228
15229/* Maximum number of symlinks that will be resolved while trying to
15230** expand a filename in xFullPathname() in the VFS.
15231*/
15232#ifndef SQLITE_MAX_SYMLINK
15233# define SQLITE_MAX_SYMLINK 200
15234#endif
15235
15236/*
15237** The default size of a disk sector
15238*/
15239#ifndef SQLITE_DEFAULT_SECTOR_SIZE
15240# define SQLITE_DEFAULT_SECTOR_SIZE 4096
15241#endif
15242
15243/*
15244** Temporary files are named starting with this prefix followed by 16 random
15245** alphanumeric characters, and no file extension. They are stored in the
15246** OS's standard temporary file directory, and are deleted prior to exit.
15247** If sqlite is being embedded in another program, you may wish to change the
15248** prefix to reflect your program's name, so that if your program exits
15249** prematurely, old temporary files can be easily identified. This can be done
15250** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15251**
15252** 2006-10-31: The default prefix used to be "sqlite_". But then
15253** Mcafee started using SQLite in their anti-virus product and it
15254** started putting files with the "sqlite" name in the c:/temp folder.
15255** This annoyed many windows users. Those users would then do a
15256** Google search for "sqlite", find the telephone numbers of the
15257** developers and call to wake them up at night and complain.
15258** For this reason, the default name prefix is changed to be "sqlite"
15259** spelled backwards. So the temp files are still identified, but
15260** anybody smart enough to figure out the code is also likely smart
15261** enough to know that calling the developer will not help get rid
15262** of the file.
15263*/
15264#ifndef SQLITE_TEMP_FILE_PREFIX
15265# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
15266#endif
15267
15268/*
15269** The following values may be passed as the second argument to
15270** sqlite3OsLock(). The various locks exhibit the following semantics:
15271**
15272** SHARED: Any number of processes may hold a SHARED lock simultaneously.
15273** RESERVED: A single process may hold a RESERVED lock on a file at
15274** any time. Other processes may hold and obtain new SHARED locks.
15275** PENDING: A single process may hold a PENDING lock on a file at
15276** any one time. Existing SHARED locks may persist, but no new
15277** SHARED locks may be obtained by other processes.
15278** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
15279**
15280** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
15281** process that requests an EXCLUSIVE lock may actually obtain a PENDING
15282** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
15283** sqlite3OsLock().
15284*/
15285#define NO_LOCK 0
15286#define SHARED_LOCK 1
15287#define RESERVED_LOCK 2
15288#define PENDING_LOCK 3
15289#define EXCLUSIVE_LOCK 4
15290
15291/*
15292** File Locking Notes: (Mostly about windows but also some info for Unix)
15293**
15294** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
15295** those functions are not available. So we use only LockFile() and
15296** UnlockFile().
15297**
15298** LockFile() prevents not just writing but also reading by other processes.
15299** A SHARED_LOCK is obtained by locking a single randomly-chosen
15300** byte out of a specific range of bytes. The lock byte is obtained at
15301** random so two separate readers can probably access the file at the
15302** same time, unless they are unlucky and choose the same lock byte.
15303** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
15304** There can only be one writer. A RESERVED_LOCK is obtained by locking
15305** a single byte of the file that is designated as the reserved lock byte.
15306** A PENDING_LOCK is obtained by locking a designated byte different from
15307** the RESERVED_LOCK byte.
15308**
15309** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
15310** which means we can use reader/writer locks. When reader/writer locks
15311** are used, the lock is placed on the same range of bytes that is used
15312** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
15313** will support two or more Win95 readers or two or more WinNT readers.
15314** But a single Win95 reader will lock out all WinNT readers and a single
15315** WinNT reader will lock out all other Win95 readers.
15316**
15317** The following #defines specify the range of bytes used for locking.
15318** SHARED_SIZE is the number of bytes available in the pool from which
15319** a random byte is selected for a shared lock. The pool of bytes for
15320** shared locks begins at SHARED_FIRST.
15321**
15322** The same locking strategy and
15323** byte ranges are used for Unix. This leaves open the possibility of having
15324** clients on win95, winNT, and unix all talking to the same shared file
15325** and all locking correctly. To do so would require that samba (or whatever
15326** tool is being used for file sharing) implements locks correctly between
15327** windows and unix. I'm guessing that isn't likely to happen, but by
15328** using the same locking range we are at least open to the possibility.
15329**
15330** Locking in windows is manditory. For this reason, we cannot store
15331** actual data in the bytes used for locking. The pager never allocates
15332** the pages involved in locking therefore. SHARED_SIZE is selected so
15333** that all locks will fit on a single page even at the minimum page size.
15334** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
15335** is set high so that we don't have to allocate an unused page except
15336** for very large databases. But one should test the page skipping logic
15337** by setting PENDING_BYTE low and running the entire regression suite.
15338**
15339** Changing the value of PENDING_BYTE results in a subtly incompatible
15340** file format. Depending on how it is changed, you might not notice
15341** the incompatibility right away, even running a full regression test.
15342** The default location of PENDING_BYTE is the first byte past the
15343** 1GB boundary.
15344**
15345*/
15346#ifdef SQLITE_OMIT_WSD
15347# define PENDING_BYTE (0x40000000)
15348#else
15349# define PENDING_BYTE sqlite3PendingByte
15350#endif
15351#define RESERVED_BYTE (PENDING_BYTE+1)
15352#define SHARED_FIRST (PENDING_BYTE+2)
15353#define SHARED_SIZE 510
15354
15355/*
15356** Wrapper around OS specific sqlite3_os_init() function.
15357*/
15358SQLITE_PRIVATE int sqlite3OsInit(void);
15359
15360/*
15361** Functions for accessing sqlite3_file methods
15362*/
15363SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
15364SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15365SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15366SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
15367SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
15368SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15369SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
15370SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
15371SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15372SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
15373SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15374#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
15375SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
15376SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15377#ifndef SQLITE_OMIT_WAL
15378SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15379SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15380SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
15381SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
15382#endif /* SQLITE_OMIT_WAL */
15383SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15384SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15385
15386
15387/*
15388** Functions for accessing sqlite3_vfs methods
15389*/
15390SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15391SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15392SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15393SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15394#ifndef SQLITE_OMIT_LOAD_EXTENSION
15395SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
15396SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15397SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
15398SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
15399#endif /* SQLITE_OMIT_LOAD_EXTENSION */
15400SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15401SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
15402SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
15403SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15404
15405/*
15406** Convenience functions for opening and closing files using
15407** sqlite3_malloc() to obtain space for the file-handle structure.
15408*/
15409SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15410SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
15411
15412#endif /* _SQLITE_OS_H_ */
15413
15414/************** End of os.h **************************************************/
15415/************** Continuing where we left off in sqliteInt.h ******************/
15416/************** Include pager.h in the middle of sqliteInt.h *****************/
15417/************** Begin file pager.h *******************************************/
15418/*
15419** 2001 September 15
15420**
15421** The author disclaims copyright to this source code. In place of
15422** a legal notice, here is a blessing:
15423**
15424** May you do good and not evil.
15425** May you find forgiveness for yourself and forgive others.
15426** May you share freely, never taking more than you give.
15427**
15428*************************************************************************
15429** This header file defines the interface that the sqlite page cache
15430** subsystem. The page cache subsystem reads and writes a file a page
15431** at a time and provides a journal for rollback.
15432*/
15433
15434#ifndef SQLITE_PAGER_H
15435#define SQLITE_PAGER_H
15436
15437/*
15438** Default maximum size for persistent journal files. A negative
15439** value means no limit. This value may be overridden using the
15440** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
15441*/
15442#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
15443 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15444#endif
15445
15446/*
15447** The type used to represent a page number. The first page in a file
15448** is called page 1. 0 is used to represent "not a page".
15449*/
15450typedef u32 Pgno;
15451
15452/*
15453** Each open file is managed by a separate instance of the "Pager" structure.
15454*/
15455typedef struct Pager Pager;
15456
15457/*
15458** Handle type for pages.
15459*/
15460typedef struct PgHdr DbPage;
15461
15462/*
15463** Page number PAGER_SJ_PGNO is never used in an SQLite database (it is
15464** reserved for working around a windows/posix incompatibility). It is
15465** used in the journal to signify that the remainder of the journal file
15466** is devoted to storing a super-journal name - there are no more pages to
15467** roll back. See comments for function writeSuperJournal() in pager.c
15468** for details.
15469*/
15470#define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15471#define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15472
15473/*
15474** Allowed values for the flags parameter to sqlite3PagerOpen().
15475**
15476** NOTE: These values must match the corresponding BTREE_ values in btree.h.
15477*/
15478#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
15479#define PAGER_MEMORY 0x0002 /* In-memory database */
15480
15481/*
15482** Valid values for the second argument to sqlite3PagerLockingMode().
15483*/
15484#define PAGER_LOCKINGMODE_QUERY -1
15485#define PAGER_LOCKINGMODE_NORMAL 0
15486#define PAGER_LOCKINGMODE_EXCLUSIVE 1
15487
15488/*
15489** Numeric constants that encode the journalmode.
15490**
15491** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
15492** are exposed in the API via the "PRAGMA journal_mode" command and
15493** therefore cannot be changed without a compatibility break.
15494*/
15495#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15496#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
15497#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
15498#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
15499#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
15500#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15501#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15502
15503/*
15504** Flags that make up the mask passed to sqlite3PagerGet().
15505*/
15506#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
15507#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15508
15509/*
15510** Flags for sqlite3PagerSetFlags()
15511**
15512** Value constraints (enforced via assert()):
15513** PAGER_FULLFSYNC == SQLITE_FullFSync
15514** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
15515** PAGER_CACHE_SPILL == SQLITE_CacheSpill
15516*/
15517#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
15518#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
15519#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
15520#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
15521#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
15522#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
15523#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
15524#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
15525#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
15526
15527/*
15528** The remainder of this file contains the declarations of the functions
15529** that make up the Pager sub-system API. See source code comments for
15530** a detailed description of each routine.
15531*/
15532
15533/* Open and close a Pager connection. */
15534SQLITE_PRIVATE int sqlite3PagerOpen(
15535 sqlite3_vfs*,
15536 Pager **ppPager,
15537 const char*,
15538 int,
15539 int,
15540 int,
15541 void(*)(DbPage*)
15542);
15543SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
15544SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
15545
15546/* Functions used to configure a Pager object. */
15547SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
15548SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
15549SQLITE_PRIVATE Pgno sqlite3PagerMaxPageCount(Pager*, Pgno);
15550SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
15551SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
15552SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
15553SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
15554SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
15555SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
15556SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
15557SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
15558SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
15559SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
15560SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
15561SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
15562
15563/* Functions used to obtain and release page references. */
15564SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
15565SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
15566SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
15567SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
15568SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
15569SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
15570
15571/* Operations on page references. */
15572SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
15573SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
15574SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
15575SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
15576SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
15577SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
15578
15579/* Functions used to manage pager transactions and savepoints. */
15580SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
15581SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
15582SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
15583SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
15584SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper);
15585SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
15586SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
15587SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
15588SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
15589SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
15590
15591#ifndef SQLITE_OMIT_WAL
15592SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
15593SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
15594SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
15595SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
15596SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
15597# ifdef SQLITE_ENABLE_SNAPSHOT
15598SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
15599SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
15600SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
15601SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
15602SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager);
15603# endif
15604#endif
15605
15606#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
15607SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager*, int);
15608SQLITE_PRIVATE void sqlite3PagerWalDb(Pager*, sqlite3*);
15609#else
15610# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK
15611# define sqlite3PagerWalDb(x,y)
15612#endif
15613
15614#ifdef SQLITE_DIRECT_OVERFLOW_READ
15615SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
15616#endif
15617
15618#ifdef SQLITE_ENABLE_ZIPVFS
15619SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
15620#endif
15621
15622/* Functions used to query pager state and configuration. */
15623SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
15624SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
15625#ifdef SQLITE_DEBUG
15626SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
15627#endif
15628SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
15629SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager*, int);
15630SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
15631SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
15632SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15633SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15634SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15635SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15636SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15637SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15638SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15639
15640/* Functions used to truncate the database file. */
15641SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
15642
15643SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
15644
15645/* Functions to support testing and debugging. */
15646#if !defined(NDEBUG) || defined(SQLITE_TEST)
15647SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
15648SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
15649#endif
15650#ifdef SQLITE_TEST
15651SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
15652SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
15653 void disable_simulated_io_errors(void);
15654 void enable_simulated_io_errors(void);
15655#else
15656# define disable_simulated_io_errors()
15657# define enable_simulated_io_errors()
15658#endif
15659
15660#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
15661SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager*);
15662#endif
15663
15664#endif /* SQLITE_PAGER_H */
15665
15666/************** End of pager.h ***********************************************/
15667/************** Continuing where we left off in sqliteInt.h ******************/
15668/************** Include btree.h in the middle of sqliteInt.h *****************/
15669/************** Begin file btree.h *******************************************/
15670/*
15671** 2001 September 15
15672**
15673** The author disclaims copyright to this source code. In place of
15674** a legal notice, here is a blessing:
15675**
15676** May you do good and not evil.
15677** May you find forgiveness for yourself and forgive others.
15678** May you share freely, never taking more than you give.
15679**
15680*************************************************************************
15681** This header file defines the interface that the sqlite B-Tree file
15682** subsystem. See comments in the source code for a detailed description
15683** of what each interface routine does.
15684*/
15685#ifndef SQLITE_BTREE_H
15686#define SQLITE_BTREE_H
15687
15688/* TODO: This definition is just included so other modules compile. It
15689** needs to be revisited.
15690*/
15691#define SQLITE_N_BTREE_META 16
15692
15693/*
15694** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15695** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
15696*/
15697#ifndef SQLITE_DEFAULT_AUTOVACUUM
15698 #define SQLITE_DEFAULT_AUTOVACUUM 0
15699#endif
15700
15701#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15702#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15703#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
15704
15705/*
15706** Forward declarations of structure
15707*/
15708typedef struct Btree Btree;
15709typedef struct BtCursor BtCursor;
15710typedef struct BtShared BtShared;
15711typedef struct BtreePayload BtreePayload;
15712
15713
15714SQLITE_PRIVATE int sqlite3BtreeOpen(
15715 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15716 const char *zFilename, /* Name of database file to open */
15717 sqlite3 *db, /* Associated database connection */
15718 Btree **ppBtree, /* Return open Btree* here */
15719 int flags, /* Flags */
15720 int vfsFlags /* Flags passed through to VFS open */
15721);
15722
15723/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
15724** following values.
15725**
15726** NOTE: These values must match the corresponding PAGER_ values in
15727** pager.h.
15728*/
15729#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
15730#define BTREE_MEMORY 2 /* This is an in-memory DB */
15731#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15732#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
15733
15734SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
15735SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
15736SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
15737#if SQLITE_MAX_MMAP_SIZE>0
15738SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
15739#endif
15740SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
15741SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
15742SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
15743SQLITE_PRIVATE Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
15744SQLITE_PRIVATE Pgno sqlite3BtreeLastPage(Btree*);
15745SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
15746SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree*);
15747SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
15748SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
15749SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
15750SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
15751SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
15752SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
15753SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
15754SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
15755SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
15756SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
15757SQLITE_PRIVATE int sqlite3BtreeTxnState(Btree*);
15758SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
15759
15760SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
15761SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
15762#ifndef SQLITE_OMIT_SHARED_CACHE
15763SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
15764#endif
15765
15766/* Savepoints are named, nestable SQL transactions mostly implemented */
15767/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */
15768SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
15769
15770/* "Checkpoint" only refers to WAL. See https://sqlite.org/wal.html#ckpt */
15771#ifndef SQLITE_OMIT_WAL
15772SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15773#endif
15774
15775SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
15776SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
15777SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
15778
15779SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
15780
15781/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
15782** of the flags shown below.
15783**
15784** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
15785** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15786** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
15787** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
15788** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15789** indices.)
15790*/
15791#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15792#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15793
15794SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
15795SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, i64*);
15796SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
15797SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
15798
15799SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
15800SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
15801
15802SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
15803
15804/*
15805** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
15806** should be one of the following values. The integer values are assigned
15807** to constants so that the offset of the corresponding field in an
15808** SQLite database header may be found using the following formula:
15809**
15810** offset = 36 + (idx * 4)
15811**
15812** For example, the free-page-count field is located at byte offset 36 of
15813** the database file header. The incr-vacuum-flag field is located at
15814** byte offset 64 (== 36+4*7).
15815**
15816** The BTREE_DATA_VERSION value is not really a value stored in the header.
15817** It is a read-only number computed by the pager. But we merge it with
15818** the header value access routines since its access pattern is the same.
15819** Call it a "virtual meta value".
15820*/
15821#define BTREE_FREE_PAGE_COUNT 0
15822#define BTREE_SCHEMA_VERSION 1
15823#define BTREE_FILE_FORMAT 2
15824#define BTREE_DEFAULT_CACHE_SIZE 3
15825#define BTREE_LARGEST_ROOT_PAGE 4
15826#define BTREE_TEXT_ENCODING 5
15827#define BTREE_USER_VERSION 6
15828#define BTREE_INCR_VACUUM 7
15829#define BTREE_APPLICATION_ID 8
15830#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15831
15832/*
15833** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
15834** interface.
15835**
15836** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
15837**
15838** The first argument is an Expr* (which is guaranteed to be constant for
15839** the lifetime of the cursor) that defines constraints on which rows
15840** might be fetched with this cursor. The Expr* tree may contain
15841** TK_REGISTER nodes that refer to values stored in the array of registers
15842** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
15843** then the value of the node is the value in Mem[pExpr.iTable]. Any
15844** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15845** column of the b-tree of the cursor. The Expr tree will not contain
15846** any function calls nor subqueries nor references to b-trees other than
15847** the cursor being hinted.
15848**
15849** The design of the _RANGE hint is aid b-tree implementations that try
15850** to prefetch content from remote machines - to provide those
15851** implementations with limits on what needs to be prefetched and thereby
15852** reduce network bandwidth.
15853**
15854** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
15855** standard SQLite. The other hints are provided for extensions that use
15856** the SQLite parser and code generator but substitute their own storage
15857** engine.
15858*/
15859#define BTREE_HINT_RANGE 0 /* Range constraints on queries */
15860
15861/*
15862** Values that may be OR'd together to form the argument to the
15863** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
15864**
15865** The BTREE_BULKLOAD flag is set on index cursors when the index is going
15866** to be filled with content that is already in sorted order.
15867**
15868** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
15869** OP_SeekLE opcodes for a range search, but where the range of entries
15870** selected will all have the same key. In other words, the cursor will
15871** be used only for equality key searches.
15872**
15873*/
15874#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
15875#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15876
15877/*
15878** Flags passed as the third argument to sqlite3BtreeCursor().
15879**
15880** For read-only cursors the wrFlag argument is always zero. For read-write
15881** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
15882** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
15883** only be used by SQLite for the following:
15884**
15885** * to seek to and then delete specific entries, and/or
15886**
15887** * to read values that will be used to create keys that other
15888** BTREE_FORDELETE cursors will seek to and delete.
15889**
15890** The BTREE_FORDELETE flag is an optimization hint. It is not used by
15891** by this, the native b-tree engine of SQLite, but it is available to
15892** alternative storage engines that might be substituted in place of this
15893** b-tree system. For alternative storage engines in which a delete of
15894** the main table row automatically deletes corresponding index rows,
15895** the FORDELETE flag hint allows those alternative storage engines to
15896** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
15897** and DELETE operations as no-ops, and any READ operation against a
15898** FORDELETE cursor may return a null row: 0x01 0x00.
15899*/
15900#define BTREE_WRCSR 0x00000004 /* read-write cursor */
15901#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
15902
15903SQLITE_PRIVATE int sqlite3BtreeCursor(
15904 Btree*, /* BTree containing table to open */
15905 Pgno iTable, /* Index of root page */
15906 int wrFlag, /* 1 for writing. 0 for read-only */
15907 struct KeyInfo*, /* First argument to compare function */
15908 BtCursor *pCursor /* Space to write cursor structure */
15909);
15910SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
15911SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
15912SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
15913SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
15914#ifdef SQLITE_ENABLE_CURSOR_HINTS
15915SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
15916#endif
15917
15918SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
15919SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
15920 BtCursor*,
15921 i64 intKey,
15922 int bias,
15923 int *pRes
15924);
15925SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
15926 BtCursor*,
15927 UnpackedRecord *pUnKey,
15928 int *pRes
15929);
15930SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
15931SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
15932SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
15933
15934/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
15935#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
15936#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
15937#define BTREE_APPEND 0x08 /* Insert is likely an append */
15938#define BTREE_PREFORMAT 0x80 /* Inserted data is a preformated cell */
15939
15940/* An instance of the BtreePayload object describes the content of a single
15941** entry in either an index or table btree.
15942**
15943** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
15944** an arbitrary key and no data. These btrees have pKey,nKey set to the
15945** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
15946** fields give an array of Mem objects that are a decomposition of the key.
15947** The nMem field might be zero, indicating that no decomposition is available.
15948**
15949** Table btrees (used for rowid tables) contain an integer rowid used as
15950** the key and passed in the nKey field. The pKey field is zero.
15951** pData,nData hold the content of the new entry. nZero extra zero bytes
15952** are appended to the end of the content when constructing the entry.
15953** The aMem,nMem fields are uninitialized for table btrees.
15954**
15955** Field usage summary:
15956**
15957** Table BTrees Index Btrees
15958**
15959** pKey always NULL encoded key
15960** nKey the ROWID length of pKey
15961** pData data not used
15962** aMem not used decomposed key value
15963** nMem not used entries in aMem
15964** nData length of pData not used
15965** nZero extra zeros after pData not used
15966**
15967** This object is used to pass information into sqlite3BtreeInsert(). The
15968** same information used to be passed as five separate parameters. But placing
15969** the information into this object helps to keep the interface more
15970** organized and understandable, and it also helps the resulting code to
15971** run a little faster by using fewer registers for parameter passing.
15972*/
15973struct BtreePayload {
15974 const void *pKey; /* Key content for indexes. NULL for tables */
15975 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
15976 const void *pData; /* Data for tables. */
15977 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
15978 u16 nMem; /* Number of aMem[] value. Might be zero */
15979 int nData; /* Size of pData. 0 if none. */
15980 int nZero; /* Extra zero data appended after pData,nData */
15981};
15982
15983SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
15984 int flags, int seekResult);
15985SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
15986SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
15987SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
15988SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
15989SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
15990SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
15991SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor*);
15992SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor*);
15993SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
15994SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
15995SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
15996SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
15997SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
15998
15999SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
16000 sqlite3 *db, /* Database connection that is running the check */
16001 Btree *p, /* The btree to be checked */
16002 Pgno *aRoot, /* An array of root pages numbers for individual trees */
16003 int nRoot, /* Number of entries in aRoot[] */
16004 int mxErr, /* Stop reporting errors after this many */
16005 int *pnErr, /* OUT: Write number of errors seen to this variable */
16006 char **pzOut /* OUT: Write the error message string here */
16007);
16008SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
16009SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
16010
16011#ifndef SQLITE_OMIT_INCRBLOB
16012SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
16013SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
16014SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
16015#endif
16016SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
16017SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
16018SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
16019SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
16020SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
16021
16022#ifdef SQLITE_DEBUG
16023SQLITE_PRIVATE sqlite3_uint64 sqlite3BtreeSeekCount(Btree*);
16024#else
16025# define sqlite3BtreeSeekCount(X) 0
16026#endif
16027
16028#ifndef NDEBUG
16029SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
16030#endif
16031SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
16032
16033SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
16034
16035#ifdef SQLITE_TEST
16036SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
16037SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
16038#endif
16039
16040#ifndef SQLITE_OMIT_WAL
16041SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16042#endif
16043
16044SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
16045
16046SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree*);
16047
16048/*
16049** If we are not using shared cache, then there is no need to
16050** use mutexes to access the BtShared structures. So make the
16051** Enter and Leave procedures no-ops.
16052*/
16053#ifndef SQLITE_OMIT_SHARED_CACHE
16054SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
16055SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
16056SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
16057SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
16058SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*);
16059#else
16060# define sqlite3BtreeEnter(X)
16061# define sqlite3BtreeEnterAll(X)
16062# define sqlite3BtreeSharable(X) 0
16063# define sqlite3BtreeEnterCursor(X)
16064# define sqlite3BtreeConnectionCount(X) 1
16065#endif
16066
16067#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
16068SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
16069SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
16070SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
16071#ifndef NDEBUG
16072 /* These routines are used inside assert() statements only. */
16073SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
16074SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
16075SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
16076#endif
16077#else
16078
16079# define sqlite3BtreeLeave(X)
16080# define sqlite3BtreeLeaveCursor(X)
16081# define sqlite3BtreeLeaveAll(X)
16082
16083# define sqlite3BtreeHoldsMutex(X) 1
16084# define sqlite3BtreeHoldsAllMutexes(X) 1
16085# define sqlite3SchemaMutexHeld(X,Y,Z) 1
16086#endif
16087
16088
16089#endif /* SQLITE_BTREE_H */
16090
16091/************** End of btree.h ***********************************************/
16092/************** Continuing where we left off in sqliteInt.h ******************/
16093/************** Include vdbe.h in the middle of sqliteInt.h ******************/
16094/************** Begin file vdbe.h ********************************************/
16095/*
16096** 2001 September 15
16097**
16098** The author disclaims copyright to this source code. In place of
16099** a legal notice, here is a blessing:
16100**
16101** May you do good and not evil.
16102** May you find forgiveness for yourself and forgive others.
16103** May you share freely, never taking more than you give.
16104**
16105*************************************************************************
16106** Header file for the Virtual DataBase Engine (VDBE)
16107**
16108** This header defines the interface to the virtual database engine
16109** or VDBE. The VDBE implements an abstract machine that runs a
16110** simple program to access and modify the underlying database.
16111*/
16112#ifndef SQLITE_VDBE_H
16113#define SQLITE_VDBE_H
16114/* #include <stdio.h> */
16115
16116/*
16117** A single VDBE is an opaque structure named "Vdbe". Only routines
16118** in the source file sqliteVdbe.c are allowed to see the insides
16119** of this structure.
16120*/
16121typedef struct Vdbe Vdbe;
16122
16123/*
16124** The names of the following types declared in vdbeInt.h are required
16125** for the VdbeOp definition.
16126*/
16127typedef struct sqlite3_value Mem;
16128typedef struct SubProgram SubProgram;
16129
16130/*
16131** A single instruction of the virtual machine has an opcode
16132** and as many as three operands. The instruction is recorded
16133** as an instance of the following structure:
16134*/
16135struct VdbeOp {
16136 u8 opcode; /* What operation to perform */
16137 signed char p4type; /* One of the P4_xxx constants for p4 */
16138 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16139 int p1; /* First operand */
16140 int p2; /* Second parameter (often the jump destination) */
16141 int p3; /* The third parameter */
16142 union p4union { /* fourth parameter */
16143 int i; /* Integer value if p4type==P4_INT32 */
16144 void *p; /* Generic pointer */
16145 char *z; /* Pointer to data for string (char array) types */
16146 i64 *pI64; /* Used when p4type is P4_INT64 */
16147 double *pReal; /* Used when p4type is P4_REAL */
16148 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
16149 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
16150 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
16151 Mem *pMem; /* Used when p4type is P4_MEM */
16152 VTable *pVtab; /* Used when p4type is P4_VTAB */
16153 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
16154 u32 *ai; /* Used when p4type is P4_INTARRAY */
16155 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
16156 Table *pTab; /* Used when p4type is P4_TABLE */
16157#ifdef SQLITE_ENABLE_CURSOR_HINTS
16158 Expr *pExpr; /* Used when p4type is P4_EXPR */
16159#endif
16160 } p4;
16161#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
16162 char *zComment; /* Comment to improve readability */
16163#endif
16164#ifdef SQLITE_VDBE_COVERAGE
16165 u32 iSrcLine; /* Source-code line that generated this opcode
16166 ** with flags in the upper 8 bits */
16167#endif
16168#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
16169 u64 nExec;
16170 u64 nCycle;
16171#endif
16172};
16173typedef struct VdbeOp VdbeOp;
16174
16175
16176/*
16177** A sub-routine used to implement a trigger program.
16178*/
16179struct SubProgram {
16180 VdbeOp *aOp; /* Array of opcodes for sub-program */
16181 int nOp; /* Elements in aOp[] */
16182 int nMem; /* Number of memory cells required */
16183 int nCsr; /* Number of cursors required */
16184 u8 *aOnce; /* Array of OP_Once flags */
16185 void *token; /* id that may be used to recursive triggers */
16186 SubProgram *pNext; /* Next sub-program already visited */
16187};
16188
16189/*
16190** A smaller version of VdbeOp used for the VdbeAddOpList() function because
16191** it takes up less space.
16192*/
16193struct VdbeOpList {
16194 u8 opcode; /* What operation to perform */
16195 signed char p1; /* First operand */
16196 signed char p2; /* Second parameter (often the jump destination) */
16197 signed char p3; /* Third parameter */
16198};
16199typedef struct VdbeOpList VdbeOpList;
16200
16201/*
16202** Allowed values of VdbeOp.p4type
16203*/
16204#define P4_NOTUSED 0 /* The P4 parameter is not used */
16205#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
16206#define P4_STATIC (-1) /* Pointer to a static string */
16207#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
16208#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
16209#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
16210#define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
16211/* Above do not own any resources. Must free those below */
16212#define P4_FREE_IF_LE (-6)
16213#define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
16214#define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
16215#define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
16216#define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
16217#define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
16218#define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
16219#define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16220#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16221#define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16222#define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16223
16224/* Error message codes for OP_Halt */
16225#define P5_ConstraintNotNull 1
16226#define P5_ConstraintUnique 2
16227#define P5_ConstraintCheck 3
16228#define P5_ConstraintFK 4
16229
16230/*
16231** The Vdbe.aColName array contains 5n Mem structures, where n is the
16232** number of columns of data returned by the statement.
16233*/
16234#define COLNAME_NAME 0
16235#define COLNAME_DECLTYPE 1
16236#define COLNAME_DATABASE 2
16237#define COLNAME_TABLE 3
16238#define COLNAME_COLUMN 4
16239#ifdef SQLITE_ENABLE_COLUMN_METADATA
16240# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
16241#else
16242# ifdef SQLITE_OMIT_DECLTYPE
16243# define COLNAME_N 1 /* Store only the name */
16244# else
16245# define COLNAME_N 2 /* Store the name and decltype */
16246# endif
16247#endif
16248
16249/*
16250** The following macro converts a label returned by sqlite3VdbeMakeLabel()
16251** into an index into the Parse.aLabel[] array that contains the resolved
16252** address of that label.
16253*/
16254#define ADDR(X) (~(X))
16255
16256/*
16257** The makefile scans the vdbe.c source file and creates the "opcodes.h"
16258** header file that defines a number for each opcode used by the VDBE.
16259*/
16260/************** Include opcodes.h in the middle of vdbe.h ********************/
16261/************** Begin file opcodes.h *****************************************/
16262/* Automatically generated. Do not edit */
16263/* See the tool/mkopcodeh.tcl script for details */
16264#define OP_Savepoint 0
16265#define OP_AutoCommit 1
16266#define OP_Transaction 2
16267#define OP_Checkpoint 3
16268#define OP_JournalMode 4
16269#define OP_Vacuum 5
16270#define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
16271#define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */
16272#define OP_Init 8 /* jump, synopsis: Start at P2 */
16273#define OP_Goto 9 /* jump */
16274#define OP_Gosub 10 /* jump */
16275#define OP_InitCoroutine 11 /* jump */
16276#define OP_Yield 12 /* jump */
16277#define OP_MustBeInt 13 /* jump */
16278#define OP_Jump 14 /* jump */
16279#define OP_Once 15 /* jump */
16280#define OP_If 16 /* jump */
16281#define OP_IfNot 17 /* jump */
16282#define OP_IsType 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */
16283#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
16284#define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
16285#define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */
16286#define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */
16287#define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */
16288#define OP_SeekGT 24 /* jump, synopsis: key=r[P3@P4] */
16289#define OP_IfNotOpen 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */
16290#define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */
16291#define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */
16292#define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */
16293#define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */
16294#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */
16295#define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */
16296#define OP_Last 32 /* jump */
16297#define OP_IfSmaller 33 /* jump */
16298#define OP_SorterSort 34 /* jump */
16299#define OP_Sort 35 /* jump */
16300#define OP_Rewind 36 /* jump */
16301#define OP_SorterNext 37 /* jump */
16302#define OP_Prev 38 /* jump */
16303#define OP_Next 39 /* jump */
16304#define OP_IdxLE 40 /* jump, synopsis: key=r[P3@P4] */
16305#define OP_IdxGT 41 /* jump, synopsis: key=r[P3@P4] */
16306#define OP_IdxLT 42 /* jump, synopsis: key=r[P3@P4] */
16307#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
16308#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
16309#define OP_IdxGE 45 /* jump, synopsis: key=r[P3@P4] */
16310#define OP_RowSetRead 46 /* jump, synopsis: r[P3]=rowset(P1) */
16311#define OP_RowSetTest 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
16312#define OP_Program 48 /* jump */
16313#define OP_FkIfZero 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
16314#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
16315#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
16316#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
16317#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
16318#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
16319#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
16320#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
16321#define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
16322#define OP_ElseEq 58 /* jump, same as TK_ESCAPE */
16323#define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16324#define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16325#define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16326#define OP_IncrVacuum 62 /* jump */
16327#define OP_VNext 63 /* jump */
16328#define OP_Filter 64 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
16329#define OP_PureFunc 65 /* synopsis: r[P3]=func(r[P2@NP]) */
16330#define OP_Function 66 /* synopsis: r[P3]=func(r[P2@NP]) */
16331#define OP_Return 67
16332#define OP_EndCoroutine 68
16333#define OP_HaltIfNull 69 /* synopsis: if r[P3]=null halt */
16334#define OP_Halt 70
16335#define OP_Integer 71 /* synopsis: r[P2]=P1 */
16336#define OP_Int64 72 /* synopsis: r[P2]=P4 */
16337#define OP_String 73 /* synopsis: r[P2]='P4' (len=P1) */
16338#define OP_BeginSubrtn 74 /* synopsis: r[P2]=NULL */
16339#define OP_Null 75 /* synopsis: r[P2..P3]=NULL */
16340#define OP_SoftNull 76 /* synopsis: r[P1]=NULL */
16341#define OP_Blob 77 /* synopsis: r[P2]=P4 (len=P1) */
16342#define OP_Variable 78 /* synopsis: r[P2]=parameter(P1,P4) */
16343#define OP_Move 79 /* synopsis: r[P2@P3]=r[P1@P3] */
16344#define OP_Copy 80 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
16345#define OP_SCopy 81 /* synopsis: r[P2]=r[P1] */
16346#define OP_IntCopy 82 /* synopsis: r[P2]=r[P1] */
16347#define OP_FkCheck 83
16348#define OP_ResultRow 84 /* synopsis: output=r[P1@P2] */
16349#define OP_CollSeq 85
16350#define OP_AddImm 86 /* synopsis: r[P1]=r[P1]+P2 */
16351#define OP_RealAffinity 87
16352#define OP_Cast 88 /* synopsis: affinity(r[P1]) */
16353#define OP_Permutation 89
16354#define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16355#define OP_IsTrue 91 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
16356#define OP_ZeroOrNull 92 /* synopsis: r[P2] = 0 OR NULL */
16357#define OP_Offset 93 /* synopsis: r[P3] = sqlite_offset(P1) */
16358#define OP_Column 94 /* synopsis: r[P3]=PX cursor P1 column P2 */
16359#define OP_TypeCheck 95 /* synopsis: typecheck(r[P1@P2]) */
16360#define OP_Affinity 96 /* synopsis: affinity(r[P1@P2]) */
16361#define OP_MakeRecord 97 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
16362#define OP_Count 98 /* synopsis: r[P2]=count() */
16363#define OP_ReadCookie 99
16364#define OP_SetCookie 100
16365#define OP_ReopenIdx 101 /* synopsis: root=P2 iDb=P3 */
16366#define OP_BitAnd 102 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
16367#define OP_BitOr 103 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
16368#define OP_ShiftLeft 104 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
16369#define OP_ShiftRight 105 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
16370#define OP_Add 106 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
16371#define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16372#define OP_Multiply 108 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
16373#define OP_Divide 109 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
16374#define OP_Remainder 110 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
16375#define OP_Concat 111 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
16376#define OP_OpenRead 112 /* synopsis: root=P2 iDb=P3 */
16377#define OP_OpenWrite 113 /* synopsis: root=P2 iDb=P3 */
16378#define OP_BitNot 114 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
16379#define OP_OpenDup 115
16380#define OP_OpenAutoindex 116 /* synopsis: nColumn=P2 */
16381#define OP_String8 117 /* same as TK_STRING, synopsis: r[P2]='P4' */
16382#define OP_OpenEphemeral 118 /* synopsis: nColumn=P2 */
16383#define OP_SorterOpen 119
16384#define OP_SequenceTest 120 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
16385#define OP_OpenPseudo 121 /* synopsis: P3 columns in r[P2] */
16386#define OP_Close 122
16387#define OP_ColumnsUsed 123
16388#define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16389#define OP_SeekHit 125 /* synopsis: set P2<=seekHit<=P3 */
16390#define OP_Sequence 126 /* synopsis: r[P2]=cursor[P1].ctr++ */
16391#define OP_NewRowid 127 /* synopsis: r[P2]=rowid */
16392#define OP_Insert 128 /* synopsis: intkey=r[P3] data=r[P2] */
16393#define OP_RowCell 129
16394#define OP_Delete 130
16395#define OP_ResetCount 131
16396#define OP_SorterCompare 132 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
16397#define OP_SorterData 133 /* synopsis: r[P2]=data */
16398#define OP_RowData 134 /* synopsis: r[P2]=data */
16399#define OP_Rowid 135 /* synopsis: r[P2]=PX rowid of P1 */
16400#define OP_NullRow 136
16401#define OP_SeekEnd 137
16402#define OP_IdxInsert 138 /* synopsis: key=r[P2] */
16403#define OP_SorterInsert 139 /* synopsis: key=r[P2] */
16404#define OP_IdxDelete 140 /* synopsis: key=r[P2@P3] */
16405#define OP_DeferredSeek 141 /* synopsis: Move P3 to P1.rowid if needed */
16406#define OP_IdxRowid 142 /* synopsis: r[P2]=rowid */
16407#define OP_FinishSeek 143
16408#define OP_Destroy 144
16409#define OP_Clear 145
16410#define OP_ResetSorter 146
16411#define OP_CreateBtree 147 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
16412#define OP_SqlExec 148
16413#define OP_ParseSchema 149
16414#define OP_LoadAnalysis 150
16415#define OP_DropTable 151
16416#define OP_DropIndex 152
16417#define OP_Real 153 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
16418#define OP_DropTrigger 154
16419#define OP_IntegrityCk 155
16420#define OP_RowSetAdd 156 /* synopsis: rowset(P1)=r[P2] */
16421#define OP_Param 157
16422#define OP_FkCounter 158 /* synopsis: fkctr[P1]+=P2 */
16423#define OP_MemMax 159 /* synopsis: r[P1]=max(r[P1],r[P2]) */
16424#define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
16425#define OP_AggInverse 161 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
16426#define OP_AggStep 162 /* synopsis: accum=r[P3] step(r[P2@P5]) */
16427#define OP_AggStep1 163 /* synopsis: accum=r[P3] step(r[P2@P5]) */
16428#define OP_AggValue 164 /* synopsis: r[P3]=value N=P2 */
16429#define OP_AggFinal 165 /* synopsis: accum=r[P1] N=P2 */
16430#define OP_Expire 166
16431#define OP_CursorLock 167
16432#define OP_CursorUnlock 168
16433#define OP_TableLock 169 /* synopsis: iDb=P1 root=P2 write=P3 */
16434#define OP_VBegin 170
16435#define OP_VCreate 171
16436#define OP_VDestroy 172
16437#define OP_VOpen 173
16438#define OP_VInitIn 174 /* synopsis: r[P2]=ValueList(P1,P3) */
16439#define OP_VColumn 175 /* synopsis: r[P3]=vcolumn(P2) */
16440#define OP_VRename 176
16441#define OP_Pagecount 177
16442#define OP_MaxPgcnt 178
16443#define OP_ClrSubtype 179 /* synopsis: r[P1].subtype = 0 */
16444#define OP_FilterAdd 180 /* synopsis: filter(P1) += key(P3@P4) */
16445#define OP_Trace 181
16446#define OP_CursorHint 182
16447#define OP_ReleaseReg 183 /* synopsis: release r[P1@P2] mask P3 */
16448#define OP_Noop 184
16449#define OP_Explain 185
16450#define OP_Abortable 186
16451
16452/* Properties such as "out2" or "jump" that are specified in
16453** comments following the "case" for each opcode in the vdbe.c
16454** are encoded into bitvectors as follows:
16455*/
16456#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
16457#define OPFLG_IN1 0x02 /* in1: P1 is an input */
16458#define OPFLG_IN2 0x04 /* in2: P2 is an input */
16459#define OPFLG_IN3 0x08 /* in3: P3 is an input */
16460#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
16461#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
16462#define OPFLG_NCYCLE 0x40 /* ncycle:Cycles count against P1 */
16463#define OPFLG_INITIALIZER {\
16464/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
16465/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
16466/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\
16467/* 24 */ 0x49, 0x01, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,\
16468/* 32 */ 0x41, 0x01, 0x41, 0x41, 0x41, 0x01, 0x41, 0x41,\
16469/* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
16470/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16471/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\
16472/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
16473/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
16474/* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
16475/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\
16476/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x26, 0x26,\
16477/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
16478/* 112 */ 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40, 0x00,\
16479/* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\
16480/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,\
16481/* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
16482/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
16483/* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
16484/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
16485/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x50, 0x40,\
16486/* 176 */ 0x00, 0x10, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00,\
16487/* 184 */ 0x00, 0x00, 0x00,}
16488
16489/* The resolve3P2Values() routine is able to run faster if it knows
16490** the value of the largest JUMP opcode. The smaller the maximum
16491** JUMP opcode the better, so the mkopcodeh.tcl script that
16492** generated this include file strives to group all JUMP opcodes
16493** together near the beginning of the list.
16494*/
16495#define SQLITE_MX_JUMP_OPCODE 64 /* Maximum JUMP opcode */
16496
16497/************** End of opcodes.h *********************************************/
16498/************** Continuing where we left off in vdbe.h ***********************/
16499
16500/*
16501** Additional non-public SQLITE_PREPARE_* flags
16502*/
16503#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
16504#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
16505
16506/*
16507** Prototypes for the VDBE interface. See comments on the implementation
16508** for a description of what each of these routines does.
16509*/
16510SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
16511SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe*);
16512SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
16513SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
16514SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
16515SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
16516SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
16517SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
16518SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
16519SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
16520SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
16521SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
16522SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
16523SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
16524#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
16525SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
16526SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
16527#else
16528# define sqlite3VdbeVerifyNoMallocRequired(A,B)
16529# define sqlite3VdbeVerifyNoResultRow(A)
16530#endif
16531#if defined(SQLITE_DEBUG)
16532SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
16533SQLITE_PRIVATE void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe*,int,int,int);
16534#else
16535# define sqlite3VdbeVerifyAbortable(A,B)
16536# define sqlite3VdbeNoJumpsOutsideSubrtn(A,B,C,D)
16537#endif
16538SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
16539#ifndef SQLITE_OMIT_EXPLAIN
16540SQLITE_PRIVATE int sqlite3VdbeExplain(Parse*,u8,const char*,...);
16541SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
16542SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
16543# define ExplainQueryPlan(P) sqlite3VdbeExplain P
16544# ifdef SQLITE_ENABLE_STMT_SCANSTATUS
16545# define ExplainQueryPlan2(V,P) (V = sqlite3VdbeExplain P)
16546# else
16547# define ExplainQueryPlan2(V,P) ExplainQueryPlan(P)
16548# endif
16549# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
16550# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
16551#else
16552# define ExplainQueryPlan(P)
16553# define ExplainQueryPlan2(V,P)
16554# define ExplainQueryPlanPop(P)
16555# define ExplainQueryPlanParent(P) 0
16556# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16557#endif
16558#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
16559SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char*,const char*);
16560#else
16561# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16562#endif
16563SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
16564SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
16565SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
16566SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
16567SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
16568SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
16569SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe*, int);
16570SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
16571SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
16572SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
16573SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
16574#ifdef SQLITE_DEBUG
16575SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
16576#else
16577# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
16578#endif
16579SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
16580SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
16581SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
16582SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
16583SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
16584SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetLastOp(Vdbe*);
16585SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
16586SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
16587SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
16588SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
16589SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
16590SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
16591SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
16592SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
16593#ifdef SQLITE_DEBUG
16594SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
16595#endif
16596SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
16597SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
16598SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
16599SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
16600SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
16601SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
16602SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
16603SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
16604SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
16605#ifdef SQLITE_ENABLE_NORMALIZE
16606SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
16607SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
16608#endif
16609SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
16610SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
16611SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
16612SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
16613#ifndef SQLITE_OMIT_TRACE
16614SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
16615#endif
16616SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
16617SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
16618
16619SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
16620SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
16621SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
16622SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
16623
16624typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
16625SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
16626
16627SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
16628SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*);
16629
16630SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
16631#ifdef SQLITE_ENABLE_BYTECODE_VTAB
16632SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
16633#endif
16634
16635/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
16636** each VDBE opcode.
16637**
16638** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
16639** comments in VDBE programs that show key decision points in the code
16640** generator.
16641*/
16642#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
16643SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
16644# define VdbeComment(X) sqlite3VdbeComment X
16645SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
16646# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
16647# ifdef SQLITE_ENABLE_MODULE_COMMENTS
16648# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
16649# else
16650# define VdbeModuleComment(X)
16651# endif
16652#else
16653# define VdbeComment(X)
16654# define VdbeNoopComment(X)
16655# define VdbeModuleComment(X)
16656#endif
16657
16658/*
16659** The VdbeCoverage macros are used to set a coverage testing point
16660** for VDBE branch instructions. The coverage testing points are line
16661** numbers in the sqlite3.c source file. VDBE branch coverage testing
16662** only works with an amalgamation build. That's ok since a VDBE branch
16663** coverage build designed for testing the test suite only. No application
16664** should ever ship with VDBE branch coverage measuring turned on.
16665**
16666** VdbeCoverage(v) // Mark the previously coded instruction
16667** // as a branch
16668**
16669** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
16670**
16671** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
16672**
16673** VdbeCoverageNeverTaken(v) // Previous branch is never taken
16674**
16675** VdbeCoverageNeverNull(v) // Previous three-way branch is only
16676** // taken on the first two ways. The
16677** // NULL option is not possible
16678**
16679** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
16680** // in distinguishing equal and not-equal.
16681**
16682** Every VDBE branch operation must be tagged with one of the macros above.
16683** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
16684** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
16685** routine in vdbe.c, alerting the developer to the missed tag.
16686**
16687** During testing, the test application will invoke
16688** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
16689** routine that is invoked as each bytecode branch is taken. The callback
16690** contains the sqlite3.c source line number of the VdbeCoverage macro and
16691** flags to indicate whether or not the branch was taken. The test application
16692** is responsible for keeping track of this and reporting byte-code branches
16693** that are never taken.
16694**
16695** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
16696** vdbe.c source file for additional information.
16697*/
16698#ifdef SQLITE_VDBE_COVERAGE
16699SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int);
16700# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
16701# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
16702# define VdbeCoverageAlwaysTaken(v) \
16703 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
16704# define VdbeCoverageNeverTaken(v) \
16705 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
16706# define VdbeCoverageNeverNull(v) \
16707 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
16708# define VdbeCoverageNeverNullIf(v,x) \
16709 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
16710# define VdbeCoverageEqNe(v) \
16711 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
16712# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
16713#else
16714# define VdbeCoverage(v)
16715# define VdbeCoverageIf(v,x)
16716# define VdbeCoverageAlwaysTaken(v)
16717# define VdbeCoverageNeverTaken(v)
16718# define VdbeCoverageNeverNull(v)
16719# define VdbeCoverageNeverNullIf(v,x)
16720# define VdbeCoverageEqNe(v)
16721# define VDBE_OFFSET_LINENO(x) 0
16722#endif
16723
16724#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
16725SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
16726SQLITE_PRIVATE void sqlite3VdbeScanStatusRange(Vdbe*, int, int, int);
16727SQLITE_PRIVATE void sqlite3VdbeScanStatusCounters(Vdbe*, int, int, int);
16728#else
16729# define sqlite3VdbeScanStatus(a,b,c,d,e,f)
16730# define sqlite3VdbeScanStatusRange(a,b,c,d)
16731# define sqlite3VdbeScanStatusCounters(a,b,c,d)
16732#endif
16733
16734#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
16735SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
16736#endif
16737
16738#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
16739SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr);
16740#endif
16741
16742#endif /* SQLITE_VDBE_H */
16743
16744/************** End of vdbe.h ************************************************/
16745/************** Continuing where we left off in sqliteInt.h ******************/
16746/************** Include pcache.h in the middle of sqliteInt.h ****************/
16747/************** Begin file pcache.h ******************************************/
16748/*
16749** 2008 August 05
16750**
16751** The author disclaims copyright to this source code. In place of
16752** a legal notice, here is a blessing:
16753**
16754** May you do good and not evil.
16755** May you find forgiveness for yourself and forgive others.
16756** May you share freely, never taking more than you give.
16757**
16758*************************************************************************
16759** This header file defines the interface that the sqlite page cache
16760** subsystem.
16761*/
16762
16763#ifndef _PCACHE_H_
16764
16765typedef struct PgHdr PgHdr;
16766typedef struct PCache PCache;
16767
16768/*
16769** Every page in the cache is controlled by an instance of the following
16770** structure.
16771*/
16772struct PgHdr {
16773 sqlite3_pcache_page *pPage; /* Pcache object page handle */
16774 void *pData; /* Page data */
16775 void *pExtra; /* Extra content */
16776 PCache *pCache; /* PRIVATE: Cache that owns this page */
16777 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
16778 Pager *pPager; /* The pager this page is part of */
16779 Pgno pgno; /* Page number for this page */
16780#ifdef SQLITE_CHECK_PAGES
16781 u32 pageHash; /* Hash of page content */
16782#endif
16783 u16 flags; /* PGHDR flags defined below */
16784
16785 /**********************************************************************
16786 ** Elements above, except pCache, are public. All that follow are
16787 ** private to pcache.c and should not be accessed by other modules.
16788 ** pCache is grouped with the public elements for efficiency.
16789 */
16790 i64 nRef; /* Number of users of this page */
16791 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
16792 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
16793 /* NB: pDirtyNext and pDirtyPrev are undefined if the
16794 ** PgHdr object is not dirty */
16795};
16796
16797/* Bit values for PgHdr.flags */
16798#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
16799#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
16800#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
16801#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
16802 ** writing this page to the database */
16803#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
16804#define PGHDR_MMAP 0x020 /* This is an mmap page object */
16805
16806#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
16807
16808/* Initialize and shutdown the page cache subsystem */
16809SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
16810SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
16811
16812/* Page cache buffer management:
16813** These routines implement SQLITE_CONFIG_PAGECACHE.
16814*/
16815SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
16816
16817/* Create a new pager cache.
16818** Under memory stress, invoke xStress to try to make pages clean.
16819** Only clean and unpinned pages can be reclaimed.
16820*/
16821SQLITE_PRIVATE int sqlite3PcacheOpen(
16822 int szPage, /* Size of every page */
16823 int szExtra, /* Extra space associated with each page */
16824 int bPurgeable, /* True if pages are on backing store */
16825 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
16826 void *pStress, /* Argument to xStress */
16827 PCache *pToInit /* Preallocated space for the PCache */
16828);
16829
16830/* Modify the page-size after the cache has been created. */
16831SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
16832
16833/* Return the size in bytes of a PCache object. Used to preallocate
16834** storage space.
16835*/
16836SQLITE_PRIVATE int sqlite3PcacheSize(void);
16837
16838/* One release per successful fetch. Page is pinned until released.
16839** Reference counted.
16840*/
16841SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
16842SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
16843SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
16844SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
16845
16846SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
16847SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
16848SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
16849SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
16850SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
16851
16852/* Change a page number. Used by incr-vacuum. */
16853SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
16854
16855/* Remove all pages with pgno>x. Reset the cache if x==0 */
16856SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
16857
16858/* Get a list of all dirty pages in the cache, sorted by page number */
16859SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
16860
16861/* Reset and close the cache object */
16862SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
16863
16864/* Clear flags from pages of the page cache */
16865SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
16866
16867/* Discard the contents of the cache */
16868SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
16869
16870/* Return the total number of outstanding page references */
16871SQLITE_PRIVATE i64 sqlite3PcacheRefCount(PCache*);
16872
16873/* Increment the reference count of an existing page */
16874SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
16875
16876SQLITE_PRIVATE i64 sqlite3PcachePageRefcount(PgHdr*);
16877
16878/* Return the total number of pages stored in the cache */
16879SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
16880
16881#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
16882/* Iterate through all dirty pages currently stored in the cache. This
16883** interface is only available if SQLITE_CHECK_PAGES is defined when the
16884** library is built.
16885*/
16886SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
16887#endif
16888
16889#if defined(SQLITE_DEBUG)
16890/* Check invariants on a PgHdr object */
16891SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
16892#endif
16893
16894/* Set and get the suggested cache-size for the specified pager-cache.
16895**
16896** If no global maximum is configured, then the system attempts to limit
16897** the total number of pages cached by purgeable pager-caches to the sum
16898** of the suggested cache-sizes.
16899*/
16900SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
16901#ifdef SQLITE_TEST
16902SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
16903#endif
16904
16905/* Set or get the suggested spill-size for the specified pager-cache.
16906**
16907** The spill-size is the minimum number of pages in cache before the cache
16908** will attempt to spill dirty pages by calling xStress.
16909*/
16910SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
16911
16912/* Free up as much memory as possible from the page cache */
16913SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
16914
16915#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
16916/* Try to return memory used by the pcache module to the main memory heap */
16917SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
16918#endif
16919
16920#ifdef SQLITE_TEST
16921SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
16922#endif
16923
16924SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
16925
16926/* Return the header size */
16927SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
16928SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
16929
16930/* Number of dirty pages as a percentage of the configured cache size */
16931SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
16932
16933#ifdef SQLITE_DIRECT_OVERFLOW_READ
16934SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
16935#endif
16936
16937#endif /* _PCACHE_H_ */
16938
16939/************** End of pcache.h **********************************************/
16940/************** Continuing where we left off in sqliteInt.h ******************/
16941/************** Include mutex.h in the middle of sqliteInt.h *****************/
16942/************** Begin file mutex.h *******************************************/
16943/*
16944** 2007 August 28
16945**
16946** The author disclaims copyright to this source code. In place of
16947** a legal notice, here is a blessing:
16948**
16949** May you do good and not evil.
16950** May you find forgiveness for yourself and forgive others.
16951** May you share freely, never taking more than you give.
16952**
16953*************************************************************************
16954**
16955** This file contains the common header for all mutex implementations.
16956** The sqliteInt.h header #includes this file so that it is available
16957** to all source files. We break it out in an effort to keep the code
16958** better organized.
16959**
16960** NOTE: source files should *not* #include this header file directly.
16961** Source files should #include the sqliteInt.h file and let that file
16962** include this one indirectly.
16963*/
16964
16965
16966/*
16967** Figure out what version of the code to use. The choices are
16968**
16969** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
16970** mutexes implementation cannot be overridden
16971** at start-time.
16972**
16973** SQLITE_MUTEX_NOOP For single-threaded applications. No
16974** mutual exclusion is provided. But this
16975** implementation can be overridden at
16976** start-time.
16977**
16978** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16979**
16980** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16981*/
16982#if !SQLITE_THREADSAFE
16983# define SQLITE_MUTEX_OMIT
16984#endif
16985#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
16986# if SQLITE_OS_UNIX
16987# define SQLITE_MUTEX_PTHREADS
16988# elif SQLITE_OS_WIN
16989# define SQLITE_MUTEX_W32
16990# else
16991# define SQLITE_MUTEX_NOOP
16992# endif
16993#endif
16994
16995#ifdef SQLITE_MUTEX_OMIT
16996/*
16997** If this is a no-op implementation, implement everything as macros.
16998*/
16999#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
17000#define sqlite3_mutex_free(X)
17001#define sqlite3_mutex_enter(X)
17002#define sqlite3_mutex_try(X) SQLITE_OK
17003#define sqlite3_mutex_leave(X)
17004#define sqlite3_mutex_held(X) ((void)(X),1)
17005#define sqlite3_mutex_notheld(X) ((void)(X),1)
17006#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
17007#define sqlite3MutexInit() SQLITE_OK
17008#define sqlite3MutexEnd()
17009#define MUTEX_LOGIC(X)
17010#else
17011#define MUTEX_LOGIC(X) X
17012SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
17013#endif /* defined(SQLITE_MUTEX_OMIT) */
17014
17015/************** End of mutex.h ***********************************************/
17016/************** Continuing where we left off in sqliteInt.h ******************/
17017
17018/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17019** synchronous setting to EXTRA. It is no longer supported.
17020*/
17021#ifdef SQLITE_EXTRA_DURABLE
17022# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
17023# define SQLITE_DEFAULT_SYNCHRONOUS 3
17024#endif
17025
17026/*
17027** Default synchronous levels.
17028**
17029** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ
17030** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
17031**
17032** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
17033** OFF 1 0
17034** NORMAL 2 1
17035** FULL 3 2
17036** EXTRA 4 3
17037**
17038** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17039** In other words, the zero-based numbers are used for all external interfaces
17040** and the one-based values are used internally.
17041*/
17042#ifndef SQLITE_DEFAULT_SYNCHRONOUS
17043# define SQLITE_DEFAULT_SYNCHRONOUS 2
17044#endif
17045#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
17046# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
17047#endif
17048
17049/*
17050** Each database file to be accessed by the system is an instance
17051** of the following structure. There are normally two of these structures
17052** in the sqlite.aDb[] array. aDb[0] is the main database file and
17053** aDb[1] is the database file used to hold temporary tables. Additional
17054** databases may be attached.
17055*/
17056struct Db {
17057 char *zDbSName; /* Name of this database. (schema name, not filename) */
17058 Btree *pBt; /* The B*Tree structure for this database file */
17059 u8 safety_level; /* How aggressive at syncing data to disk */
17060 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
17061 Schema *pSchema; /* Pointer to database schema (possibly shared) */
17062};
17063
17064/*
17065** An instance of the following structure stores a database schema.
17066**
17067** Most Schema objects are associated with a Btree. The exception is
17068** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17069** In shared cache mode, a single Schema object can be shared by multiple
17070** Btrees that refer to the same underlying BtShared object.
17071**
17072** Schema objects are automatically deallocated when the last Btree that
17073** references them is destroyed. The TEMP Schema is manually freed by
17074** sqlite3_close().
17075*
17076** A thread must be holding a mutex on the corresponding Btree in order
17077** to access Schema content. This implies that the thread must also be
17078** holding a mutex on the sqlite3 connection pointer that owns the Btree.
17079** For a TEMP Schema, only the connection mutex is required.
17080*/
17081struct Schema {
17082 int schema_cookie; /* Database schema version number for this file */
17083 int iGeneration; /* Generation counter. Incremented with each change */
17084 Hash tblHash; /* All tables indexed by name */
17085 Hash idxHash; /* All (named) indices indexed by name */
17086 Hash trigHash; /* All triggers indexed by name */
17087 Hash fkeyHash; /* All foreign keys by referenced table name */
17088 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
17089 u8 file_format; /* Schema format version for this file */
17090 u8 enc; /* Text encoding used by this database */
17091 u16 schemaFlags; /* Flags associated with this schema */
17092 int cache_size; /* Number of pages to use in the cache */
17093};
17094
17095/*
17096** These macros can be used to test, set, or clear bits in the
17097** Db.pSchema->flags field.
17098*/
17099#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17100#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17101#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17102#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17103
17104/*
17105** Allowed values for the DB.pSchema->flags field.
17106**
17107** The DB_SchemaLoaded flag is set after the database schema has been
17108** read into internal hash tables.
17109**
17110** DB_UnresetViews means that one or more views have column names that
17111** have been filled out. If the schema changes, these column names might
17112** changes and so the view will need to be reset.
17113*/
17114#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
17115#define DB_UnresetViews 0x0002 /* Some views have defined column names */
17116#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
17117
17118/*
17119** The number of different kinds of things that can be limited
17120** using the sqlite3_limit() interface.
17121*/
17122#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
17123
17124/*
17125** Lookaside malloc is a set of fixed-size buffers that can be used
17126** to satisfy small transient memory allocation requests for objects
17127** associated with a particular database connection. The use of
17128** lookaside malloc provides a significant performance enhancement
17129** (approx 10%) by avoiding numerous malloc/free requests while parsing
17130** SQL statements.
17131**
17132** The Lookaside structure holds configuration information about the
17133** lookaside malloc subsystem. Each available memory allocation in
17134** the lookaside subsystem is stored on a linked list of LookasideSlot
17135** objects.
17136**
17137** Lookaside allocations are only allowed for objects that are associated
17138** with a particular database connection. Hence, schema information cannot
17139** be stored in lookaside because in shared cache mode the schema information
17140** is shared by multiple database connections. Therefore, while parsing
17141** schema information, the Lookaside.bEnabled flag is cleared so that
17142** lookaside allocations are not used to construct the schema objects.
17143**
17144** New lookaside allocations are only allowed if bDisable==0. When
17145** bDisable is greater than zero, sz is set to zero which effectively
17146** disables lookaside without adding a new test for the bDisable flag
17147** in a performance-critical path. sz should be set by to szTrue whenever
17148** bDisable changes back to zero.
17149**
17150** Lookaside buffers are initially held on the pInit list. As they are
17151** used and freed, they are added back to the pFree list. New allocations
17152** come off of pFree first, then pInit as a fallback. This dual-list
17153** allows use to compute a high-water mark - the maximum number of allocations
17154** outstanding at any point in the past - by subtracting the number of
17155** allocations on the pInit list from the total number of allocations.
17156**
17157** Enhancement on 2019-12-12: Two-size-lookaside
17158** The default lookaside configuration is 100 slots of 1200 bytes each.
17159** The larger slot sizes are important for performance, but they waste
17160** a lot of space, as most lookaside allocations are less than 128 bytes.
17161** The two-size-lookaside enhancement breaks up the lookaside allocation
17162** into two pools: One of 128-byte slots and the other of the default size
17163** (1200-byte) slots. Allocations are filled from the small-pool first,
17164** failing over to the full-size pool if that does not work. Thus more
17165** lookaside slots are available while also using less memory.
17166** This enhancement can be omitted by compiling with
17167** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
17168*/
17169struct Lookaside {
17170 u32 bDisable; /* Only operate the lookaside when zero */
17171 u16 sz; /* Size of each buffer in bytes */
17172 u16 szTrue; /* True value of sz, even if disabled */
17173 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
17174 u32 nSlot; /* Number of lookaside slots allocated */
17175 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
17176 LookasideSlot *pInit; /* List of buffers not previously used */
17177 LookasideSlot *pFree; /* List of available buffers */
17178#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17179 LookasideSlot *pSmallInit; /* List of small buffers not previously used */
17180 LookasideSlot *pSmallFree; /* List of available small buffers */
17181 void *pMiddle; /* First byte past end of full-size buffers and
17182 ** the first byte of LOOKASIDE_SMALL buffers */
17183#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
17184 void *pStart; /* First byte of available memory space */
17185 void *pEnd; /* First byte past end of available space */
17186 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17187};
17188struct LookasideSlot {
17189 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
17190};
17191
17192#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
17193#define EnableLookaside db->lookaside.bDisable--;\
17194 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17195
17196/* Size of the smaller allocations in two-size lookaside */
17197#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17198# define LOOKASIDE_SMALL 0
17199#else
17200# define LOOKASIDE_SMALL 128
17201#endif
17202
17203/*
17204** A hash table for built-in function definitions. (Application-defined
17205** functions use a regular table table from hash.h.)
17206**
17207** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
17208** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
17209** macro to compute a hash on the function name.
17210*/
17211#define SQLITE_FUNC_HASH_SZ 23
17212struct FuncDefHash {
17213 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
17214};
17215#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
17216
17217#ifdef SQLITE_USER_AUTHENTICATION
17218/*
17219** Information held in the "sqlite3" database connection object and used
17220** to manage user authentication.
17221*/
17222typedef struct sqlite3_userauth sqlite3_userauth;
17223struct sqlite3_userauth {
17224 u8 authLevel; /* Current authentication level */
17225 int nAuthPW; /* Size of the zAuthPW in bytes */
17226 char *zAuthPW; /* Password used to authenticate */
17227 char *zAuthUser; /* User name used to authenticate */
17228};
17229
17230/* Allowed values for sqlite3_userauth.authLevel */
17231#define UAUTH_Unknown 0 /* Authentication not yet checked */
17232#define UAUTH_Fail 1 /* User authentication failed */
17233#define UAUTH_User 2 /* Authenticated as a normal user */
17234#define UAUTH_Admin 3 /* Authenticated as an administrator */
17235
17236/* Functions used only by user authorization logic */
17237SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
17238SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
17239SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
17240SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
17241
17242#endif /* SQLITE_USER_AUTHENTICATION */
17243
17244/*
17245** typedef for the authorization callback function.
17246*/
17247#ifdef SQLITE_USER_AUTHENTICATION
17248 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17249 const char*, const char*);
17250#else
17251 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17252 const char*);
17253#endif
17254
17255#ifndef SQLITE_OMIT_DEPRECATED
17256/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
17257** in the style of sqlite3_trace()
17258*/
17259#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */
17260#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */
17261#else
17262#define SQLITE_TRACE_LEGACY 0
17263#define SQLITE_TRACE_XPROFILE 0
17264#endif /* SQLITE_OMIT_DEPRECATED */
17265#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */
17266
17267/*
17268** Maximum number of sqlite3.aDb[] entries. This is the number of attached
17269** databases plus 2 for "main" and "temp".
17270*/
17271#define SQLITE_MAX_DB (SQLITE_MAX_ATTACHED+2)
17272
17273/*
17274** Each database connection is an instance of the following structure.
17275*/
17276struct sqlite3 {
17277 sqlite3_vfs *pVfs; /* OS Interface */
17278 struct Vdbe *pVdbe; /* List of active virtual machines */
17279 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
17280 sqlite3_mutex *mutex; /* Connection mutex */
17281 Db *aDb; /* All backends */
17282 int nDb; /* Number of backends currently in use */
17283 u32 mDbFlags; /* flags recording internal state */
17284 u64 flags; /* flags settable by pragmas. See below */
17285 i64 lastRowid; /* ROWID of most recent insert (see above) */
17286 i64 szMmap; /* Default mmap_size setting */
17287 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17288 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
17289 int errCode; /* Most recent error code (SQLITE_*) */
17290 int errByteOffset; /* Byte offset of error in SQL statement */
17291 int errMask; /* & result codes with this before returning */
17292 int iSysErrno; /* Errno value from last system error */
17293 u32 dbOptFlags; /* Flags to enable/disable optimizations */
17294 u8 enc; /* Text encoding */
17295 u8 autoCommit; /* The auto-commit flag. */
17296 u8 temp_store; /* 1: file 2: memory 0: default */
17297 u8 mallocFailed; /* True if we have seen a malloc failure */
17298 u8 bBenignMalloc; /* Do not require OOMs if true */
17299 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17300 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
17301 u8 suppressErr; /* Do not issue error messages if true */
17302 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
17303 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
17304 u8 mTrace; /* zero or more SQLITE_TRACE flags */
17305 u8 noSharedCache; /* True if no shared-cache backends */
17306 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
17307 u8 eOpenState; /* Current condition of the connection */
17308 int nextPagesize; /* Pagesize after VACUUM if >0 */
17309 i64 nChange; /* Value returned by sqlite3_changes() */
17310 i64 nTotalChange; /* Value returned by sqlite3_total_changes() */
17311 int aLimit[SQLITE_N_LIMIT]; /* Limits */
17312 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
17313 struct sqlite3InitInfo { /* Information used during initialization */
17314 Pgno newTnum; /* Rootpage of table being initialized */
17315 u8 iDb; /* Which db file is being initialized */
17316 u8 busy; /* TRUE if currently initializing */
17317 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
17318 unsigned imposterTable : 1; /* Building an imposter table */
17319 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
17320 const char **azInit; /* "type", "name", and "tbl_name" columns */
17321 } init;
17322 int nVdbeActive; /* Number of VDBEs currently running */
17323 int nVdbeRead; /* Number of active VDBEs that read or write */
17324 int nVdbeWrite; /* Number of active VDBEs that read and write */
17325 int nVdbeExec; /* Number of nested calls to VdbeExec() */
17326 int nVDestroy; /* Number of active OP_VDestroy operations */
17327 int nExtension; /* Number of loaded extensions */
17328 void **aExtension; /* Array of shared library handles */
17329 union {
17330 void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */
17331 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
17332 } trace;
17333 void *pTraceArg; /* Argument to the trace function */
17334#ifndef SQLITE_OMIT_DEPRECATED
17335 void (*xProfile)(void*,const char*,u64); /* Profiling function */
17336 void *pProfileArg; /* Argument to profile function */
17337#endif
17338 void *pCommitArg; /* Argument to xCommitCallback() */
17339 int (*xCommitCallback)(void*); /* Invoked at every commit. */
17340 void *pRollbackArg; /* Argument to xRollbackCallback() */
17341 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
17342 void *pUpdateArg;
17343 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
17344 void *pAutovacPagesArg; /* Client argument to autovac_pages */
17345 void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
17346 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
17347 Parse *pParse; /* Current parse */
17348#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
17349 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
17350 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
17351 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
17352 );
17353 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17354#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
17355#ifndef SQLITE_OMIT_WAL
17356 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
17357 void *pWalArg;
17358#endif
17359 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
17360 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
17361 void *pCollNeededArg;
17362 sqlite3_value *pErr; /* Most recent error message */
17363 union {
17364 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
17365 double notUsed1; /* Spacer */
17366 } u1;
17367 Lookaside lookaside; /* Lookaside malloc configuration */
17368#ifndef SQLITE_OMIT_AUTHORIZATION
17369 sqlite3_xauth xAuth; /* Access authorization function */
17370 void *pAuthArg; /* 1st argument to the access auth function */
17371#endif
17372#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
17373 int (*xProgress)(void *); /* The progress callback */
17374 void *pProgressArg; /* Argument to the progress callback */
17375 unsigned nProgressOps; /* Number of opcodes for progress callback */
17376#endif
17377#ifndef SQLITE_OMIT_VIRTUALTABLE
17378 int nVTrans; /* Allocated size of aVTrans */
17379 Hash aModule; /* populated by sqlite3_create_module() */
17380 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
17381 VTable **aVTrans; /* Virtual tables with open transactions */
17382 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
17383#endif
17384 Hash aFunc; /* Hash table of connection functions */
17385 Hash aCollSeq; /* All collating sequences */
17386 BusyHandler busyHandler; /* Busy callback */
17387 Db aDbStatic[2]; /* Static space for the 2 default backends */
17388 Savepoint *pSavepoint; /* List of active savepoints */
17389 int nAnalysisLimit; /* Number of index rows to ANALYZE */
17390 int busyTimeout; /* Busy handler timeout, in msec */
17391 int nSavepoint; /* Number of non-transaction savepoints */
17392 int nStatement; /* Number of nested statement-transactions */
17393 i64 nDeferredCons; /* Net deferred constraints this transaction. */
17394 i64 nDeferredImmCons; /* Net deferred immediate constraints */
17395 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
17396#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
17397 /* The following variables are all protected by the STATIC_MAIN
17398 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
17399 **
17400 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
17401 ** unlock so that it can proceed.
17402 **
17403 ** When X.pBlockingConnection==Y, that means that something that X tried
17404 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
17405 ** held by Y.
17406 */
17407 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
17408 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
17409 void *pUnlockArg; /* Argument to xUnlockNotify */
17410 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
17411 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
17412#endif
17413#ifdef SQLITE_USER_AUTHENTICATION
17414 sqlite3_userauth auth; /* User authentication information */
17415#endif
17416};
17417
17418/*
17419** A macro to discover the encoding of a database.
17420*/
17421#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17422#define ENC(db) ((db)->enc)
17423
17424/*
17425** A u64 constant where the lower 32 bits are all zeros. Only the
17426** upper 32 bits are included in the argument. Necessary because some
17427** C-compilers still do not accept LL integer literals.
17428*/
17429#define HI(X) ((u64)(X)<<32)
17430
17431/*
17432** Possible values for the sqlite3.flags.
17433**
17434** Value constraints (enforced via assert()):
17435** SQLITE_FullFSync == PAGER_FULLFSYNC
17436** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
17437** SQLITE_CacheSpill == PAGER_CACHE_SPILL
17438*/
17439#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_SCHEMA */
17440#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
17441#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
17442#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
17443#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
17444#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
17445#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
17446#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and
17447 ** vtabs in the schema definition */
17448#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
17449 /* result set is empty */
17450#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
17451#define SQLITE_StmtScanStatus 0x00000400 /* Enable stmt_scanstats() counters */
17452#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
17453#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
17454#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
17455#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
17456#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
17457#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
17458#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
17459#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
17460#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
17461#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
17462#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
17463#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
17464#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
17465#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
17466#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
17467#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
17468#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
17469#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
17470#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17471#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17472#define SQLITE_EnableView 0x80000000 /* Enable the use of views */
17473#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */
17474 /* DELETE, or UPDATE and return */
17475 /* the count using a callback. */
17476#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */
17477#define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
17478
17479/* Flags used only if debugging */
17480#ifdef SQLITE_DEBUG
17481#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */
17482#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */
17483#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */
17484#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
17485#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
17486#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */
17487#endif
17488
17489/*
17490** Allowed values for sqlite3.mDbFlags
17491*/
17492#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
17493#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17494#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
17495#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
17496#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
17497#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
17498#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
17499
17500/*
17501** Bits of the sqlite3.dbOptFlags field that are used by the
17502** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
17503** selectively disable various optimizations.
17504*/
17505#define SQLITE_QueryFlattener 0x00000001 /* Query flattening */
17506#define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */
17507#define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */
17508#define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */
17509#define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */
17510#define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */
17511#define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */
17512#define SQLITE_Transitive 0x00000080 /* Transitive constraints */
17513#define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */
17514#define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17515#define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */
17516#define SQLITE_Stat4 0x00000800 /* Use STAT4 data */
17517 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
17518#define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17519#define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */
17520#define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17521#define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */
17522#define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */
17523#define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */
17524#define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */
17525 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
17526#define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */
17527#define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
17528#define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17529#define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */
17530#define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */
17531 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
17532#define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */
17533#define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
17534#define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */
17535#define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
17536#define SQLITE_AllOpts 0xffffffff /* All optimizations */
17537
17538/*
17539** Macros for testing whether or not optimizations are enabled or disabled.
17540*/
17541#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17542#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17543
17544/*
17545** Return true if it OK to factor constant expressions into the initialization
17546** code. The argument is a Parse object for the code generator.
17547*/
17548#define ConstFactorOk(P) ((P)->okConstFactor)
17549
17550/* Possible values for the sqlite3.eOpenState field.
17551** The numbers are randomly selected such that a minimum of three bits must
17552** change to convert any number to another or to zero
17553*/
17554#define SQLITE_STATE_OPEN 0x76 /* Database is open */
17555#define SQLITE_STATE_CLOSED 0xce /* Database is closed */
17556#define SQLITE_STATE_SICK 0xba /* Error and awaiting close */
17557#define SQLITE_STATE_BUSY 0x6d /* Database currently in use */
17558#define SQLITE_STATE_ERROR 0xd5 /* An SQLITE_MISUSE error occurred */
17559#define SQLITE_STATE_ZOMBIE 0xa7 /* Close with last statement close */
17560
17561/*
17562** Each SQL function is defined by an instance of the following
17563** structure. For global built-in functions (ex: substr(), max(), count())
17564** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
17565** For per-connection application-defined functions, a pointer to this
17566** structure is held in the db->aHash hash table.
17567**
17568** The u.pHash field is used by the global built-ins. The u.pDestructor
17569** field is used by per-connection app-def functions.
17570*/
17571struct FuncDef {
17572 i8 nArg; /* Number of arguments. -1 means unlimited */
17573 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
17574 void *pUserData; /* User data parameter */
17575 FuncDef *pNext; /* Next function with same name */
17576 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17577 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
17578 void (*xValue)(sqlite3_context*); /* Current agg value */
17579 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17580 const char *zName; /* SQL name of the function. */
17581 union {
17582 FuncDef *pHash; /* Next with a different name but the same hash */
17583 FuncDestructor *pDestructor; /* Reference counted destructor function */
17584 } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */
17585};
17586
17587/*
17588** This structure encapsulates a user-function destructor callback (as
17589** configured using create_function_v2()) and a reference counter. When
17590** create_function_v2() is called to create a function with a destructor,
17591** a single object of this type is allocated. FuncDestructor.nRef is set to
17592** the number of FuncDef objects created (either 1 or 3, depending on whether
17593** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
17594** member of each of the new FuncDef objects is set to point to the allocated
17595** FuncDestructor.
17596**
17597** Thereafter, when one of the FuncDef objects is deleted, the reference
17598** count on this object is decremented. When it reaches 0, the destructor
17599** is invoked and the FuncDestructor structure freed.
17600*/
17601struct FuncDestructor {
17602 int nRef;
17603 void (*xDestroy)(void *);
17604 void *pUserData;
17605};
17606
17607/*
17608** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
17609** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
17610** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
17611** are assert() statements in the code to verify this.
17612**
17613** Value constraints (enforced via assert()):
17614** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
17615** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
17616** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
17617** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
17618** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG
17619** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
17620** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
17621** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
17622** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
17623**
17624** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the
17625** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is
17626** used internally and if set means that the function has side effects.
17627** SQLITE_INNOCUOUS is used by application code and means "not unsafe".
17628** See multiple instances of tag-20230109-1.
17629*/
17630#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
17631#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
17632#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17633#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
17634#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
17635#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17636#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17637#define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
17638#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17639/* 0x0200 -- available for reuse */
17640#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17641#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
17642#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
17643#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17644 ** single query - might change over time */
17645#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17646/* 0x8000 -- available for reuse */
17647#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17648#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17649#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17650#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17651#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17652#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17653#define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17654#define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
17655
17656/* Identifier numbers for each in-line function */
17657#define INLINEFUNC_coalesce 0
17658#define INLINEFUNC_implies_nonnull_row 1
17659#define INLINEFUNC_expr_implies_expr 2
17660#define INLINEFUNC_expr_compare 3
17661#define INLINEFUNC_affinity 4
17662#define INLINEFUNC_iif 5
17663#define INLINEFUNC_sqlite_offset 6
17664#define INLINEFUNC_unlikely 99 /* Default case */
17665
17666/*
17667** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
17668** used to create the initializers for the FuncDef structures.
17669**
17670** FUNCTION(zName, nArg, iArg, bNC, xFunc)
17671** Used to create a scalar function definition of a function zName
17672** implemented by C function xFunc that accepts nArg arguments. The
17673** value passed as iArg is cast to a (void*) and made available
17674** as the user-data (sqlite3_user_data()) for the function. If
17675** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
17676**
17677** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
17678** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
17679**
17680** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
17681** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17682** adds the SQLITE_DIRECTONLY flag.
17683**
17684** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
17685** zName is the name of a function that is implemented by in-line
17686** byte code rather than by the usual callbacks. The iFuncId
17687** parameter determines the function id. The mFlags parameter is
17688** optional SQLITE_FUNC_ flags for this function.
17689**
17690** TEST_FUNC(zName, nArg, iFuncId, mFlags)
17691** zName is the name of a test-only function implemented by in-line
17692** byte code rather than by the usual callbacks. The iFuncId
17693** parameter determines the function id. The mFlags parameter is
17694** optional SQLITE_FUNC_ flags for this function.
17695**
17696** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
17697** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17698** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
17699** and functions like sqlite_version() that can change, but not during
17700** a single query. The iArg is ignored. The user-data is always set
17701** to a NULL pointer. The bNC parameter is not used.
17702**
17703** MFUNCTION(zName, nArg, xPtr, xFunc)
17704** For math-library functions. xPtr is an arbitrary pointer.
17705**
17706** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
17707** Used for "pure" date/time functions, this macro is like DFUNCTION
17708** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
17709** ignored and the user-data for these functions is set to an
17710** arbitrary non-NULL pointer. The bNC parameter is not used.
17711**
17712** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
17713** Used to create an aggregate function definition implemented by
17714** the C functions xStep and xFinal. The first four parameters
17715** are interpreted in the same way as the first 4 parameters to
17716** FUNCTION().
17717**
17718** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
17719** Used to create an aggregate function definition implemented by
17720** the C functions xStep and xFinal. The first four parameters
17721** are interpreted in the same way as the first 4 parameters to
17722** FUNCTION().
17723**
17724** LIKEFUNC(zName, nArg, pArg, flags)
17725** Used to create a scalar function definition of a function zName
17726** that accepts nArg arguments and is implemented by a call to C
17727** function likeFunc. Argument pArg is cast to a (void *) and made
17728** available as the function user-data (sqlite3_user_data()). The
17729** FuncDef.flags variable is set to the value passed as the flags
17730** parameter.
17731*/
17732#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
17733 {nArg, SQLITE_FUNC_BUILTIN|\
17734 SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17735 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17736#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17737 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17738 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17739#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17740 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
17741 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17742#define MFUNCTION(zName, nArg, xPtr, xFunc) \
17743 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
17744 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
17745#define JFUNCTION(zName, nArg, iArg, xFunc) \
17746 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|\
17747 SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
17748 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17749#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
17750 {nArg, SQLITE_FUNC_BUILTIN|\
17751 SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17752 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
17753#define TEST_FUNC(zName, nArg, iArg, mFlags) \
17754 {nArg, SQLITE_FUNC_BUILTIN|\
17755 SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
17756 SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17757 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
17758#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17759 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
17760 0, 0, xFunc, 0, 0, 0, #zName, {0} }
17761#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
17762 {nArg, SQLITE_FUNC_BUILTIN|\
17763 SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
17764 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
17765#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
17766 {nArg, SQLITE_FUNC_BUILTIN|\
17767 SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
17768 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17769#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
17770 {nArg, SQLITE_FUNC_BUILTIN|\
17771 SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17772 pArg, 0, xFunc, 0, 0, 0, #zName, }
17773#define LIKEFUNC(zName, nArg, arg, flags) \
17774 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
17775 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
17776#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
17777 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
17778 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
17779#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
17780 {nArg, SQLITE_FUNC_BUILTIN|\
17781 SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
17782 0, 0, xFunc, 0, 0, 0, #zName, {0} }
17783
17784
17785/*
17786** All current savepoints are stored in a linked list starting at
17787** sqlite3.pSavepoint. The first element in the list is the most recently
17788** opened savepoint. Savepoints are added to the list by the vdbe
17789** OP_Savepoint instruction.
17790*/
17791struct Savepoint {
17792 char *zName; /* Savepoint name (nul-terminated) */
17793 i64 nDeferredCons; /* Number of deferred fk violations */
17794 i64 nDeferredImmCons; /* Number of deferred imm fk. */
17795 Savepoint *pNext; /* Parent savepoint (if any) */
17796};
17797
17798/*
17799** The following are used as the second parameter to sqlite3Savepoint(),
17800** and as the P1 argument to the OP_Savepoint instruction.
17801*/
17802#define SAVEPOINT_BEGIN 0
17803#define SAVEPOINT_RELEASE 1
17804#define SAVEPOINT_ROLLBACK 2
17805
17806
17807/*
17808** Each SQLite module (virtual table definition) is defined by an
17809** instance of the following structure, stored in the sqlite3.aModule
17810** hash table.
17811*/
17812struct Module {
17813 const sqlite3_module *pModule; /* Callback pointers */
17814 const char *zName; /* Name passed to create_module() */
17815 int nRefModule; /* Number of pointers to this object */
17816 void *pAux; /* pAux passed to create_module() */
17817 void (*xDestroy)(void *); /* Module destructor function */
17818 Table *pEpoTab; /* Eponymous table for this module */
17819};
17820
17821/*
17822** Information about each column of an SQL table is held in an instance
17823** of the Column structure, in the Table.aCol[] array.
17824**
17825** Definitions:
17826**
17827** "table column index" This is the index of the column in the
17828** Table.aCol[] array, and also the index of
17829** the column in the original CREATE TABLE stmt.
17830**
17831** "storage column index" This is the index of the column in the
17832** record BLOB generated by the OP_MakeRecord
17833** opcode. The storage column index is less than
17834** or equal to the table column index. It is
17835** equal if and only if there are no VIRTUAL
17836** columns to the left.
17837**
17838** Notes on zCnName:
17839** The zCnName field stores the name of the column, the datatype of the
17840** column, and the collating sequence for the column, in that order, all in
17841** a single allocation. Each string is 0x00 terminated. The datatype
17842** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the
17843** collating sequence name is only included if the COLFLAG_HASCOLL bit is
17844** set.
17845*/
17846struct Column {
17847 char *zCnName; /* Name of this column */
17848 unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */
17849 unsigned eCType :4; /* One of the standard types */
17850 char affinity; /* One of the SQLITE_AFF_... values */
17851 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
17852 u8 hName; /* Column name hash for faster lookup */
17853 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17854 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
17855};
17856
17857/* Allowed values for Column.eCType.
17858**
17859** Values must match entries in the global constant arrays
17860** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
17861** than the offset into these arrays for the corresponding name.
17862** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
17863*/
17864#define COLTYPE_CUSTOM 0 /* Type appended to zName */
17865#define COLTYPE_ANY 1
17866#define COLTYPE_BLOB 2
17867#define COLTYPE_INT 3
17868#define COLTYPE_INTEGER 4
17869#define COLTYPE_REAL 5
17870#define COLTYPE_TEXT 6
17871#define SQLITE_N_STDTYPE 6 /* Number of standard types */
17872
17873/* Allowed values for Column.colFlags.
17874**
17875** Constraints:
17876** TF_HasVirtual == COLFLAG_VIRTUAL
17877** TF_HasStored == COLFLAG_STORED
17878** TF_HasHidden == COLFLAG_HIDDEN
17879*/
17880#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
17881#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
17882#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
17883#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
17884#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17885#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
17886#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */
17887#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */
17888#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */
17889#define COLFLAG_HASCOLL 0x0200 /* Has collating sequence name in zCnName */
17890#define COLFLAG_NOEXPAND 0x0400 /* Omit this column when expanding "*" */
17891#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */
17892#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
17893
17894/*
17895** A "Collating Sequence" is defined by an instance of the following
17896** structure. Conceptually, a collating sequence consists of a name and
17897** a comparison routine that defines the order of that sequence.
17898**
17899** If CollSeq.xCmp is NULL, it means that the
17900** collating sequence is undefined. Indices built on an undefined
17901** collating sequence may not be read or written.
17902*/
17903struct CollSeq {
17904 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17905 u8 enc; /* Text encoding handled by xCmp() */
17906 void *pUser; /* First argument to xCmp() */
17907 int (*xCmp)(void*,int, const void*, int, const void*);
17908 void (*xDel)(void*); /* Destructor for pUser */
17909};
17910
17911/*
17912** A sort order can be either ASC or DESC.
17913*/
17914#define SQLITE_SO_ASC 0 /* Sort in ascending order */
17915#define SQLITE_SO_DESC 1 /* Sort in ascending order */
17916#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17917
17918/*
17919** Column affinity types.
17920**
17921** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
17922** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
17923** the speed a little by numbering the values consecutively.
17924**
17925** But rather than start with 0 or 1, we begin with 'A'. That way,
17926** when multiple affinity types are concatenated into a string and
17927** used as the P4 operand, they will be more readable.
17928**
17929** Note also that the numeric types are grouped together so that testing
17930** for a numeric type is a single comparison. And the BLOB type is first.
17931*/
17932#define SQLITE_AFF_NONE 0x40 /* '@' */
17933#define SQLITE_AFF_BLOB 0x41 /* 'A' */
17934#define SQLITE_AFF_TEXT 0x42 /* 'B' */
17935#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */
17936#define SQLITE_AFF_INTEGER 0x44 /* 'D' */
17937#define SQLITE_AFF_REAL 0x45 /* 'E' */
17938#define SQLITE_AFF_FLEXNUM 0x46 /* 'F' */
17939
17940#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
17941
17942/*
17943** The SQLITE_AFF_MASK values masks off the significant bits of an
17944** affinity value.
17945*/
17946#define SQLITE_AFF_MASK 0x47
17947
17948/*
17949** Additional bit values that can be ORed with an affinity without
17950** changing the affinity.
17951**
17952** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
17953** It causes an assert() to fire if either operand to a comparison
17954** operator is NULL. It is added to certain comparison operators to
17955** prove that the operands are always NOT NULL.
17956*/
17957#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
17958#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
17959#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
17960
17961/*
17962** An object of this type is created for each virtual table present in
17963** the database schema.
17964**
17965** If the database schema is shared, then there is one instance of this
17966** structure for each database connection (sqlite3*) that uses the shared
17967** schema. This is because each database connection requires its own unique
17968** instance of the sqlite3_vtab* handle used to access the virtual table
17969** implementation. sqlite3_vtab* handles can not be shared between
17970** database connections, even when the rest of the in-memory database
17971** schema is shared, as the implementation often stores the database
17972** connection handle passed to it via the xConnect() or xCreate() method
17973** during initialization internally. This database connection handle may
17974** then be used by the virtual table implementation to access real tables
17975** within the database. So that they appear as part of the callers
17976** transaction, these accesses need to be made via the same database
17977** connection as that used to execute SQL operations on the virtual table.
17978**
17979** All VTable objects that correspond to a single table in a shared
17980** database schema are initially stored in a linked-list pointed to by
17981** the Table.pVTable member variable of the corresponding Table object.
17982** When an sqlite3_prepare() operation is required to access the virtual
17983** table, it searches the list for the VTable that corresponds to the
17984** database connection doing the preparing so as to use the correct
17985** sqlite3_vtab* handle in the compiled query.
17986**
17987** When an in-memory Table object is deleted (for example when the
17988** schema is being reloaded for some reason), the VTable objects are not
17989** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
17990** immediately. Instead, they are moved from the Table.pVTable list to
17991** another linked list headed by the sqlite3.pDisconnect member of the
17992** corresponding sqlite3 structure. They are then deleted/xDisconnected
17993** next time a statement is prepared using said sqlite3*. This is done
17994** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
17995** Refer to comments above function sqlite3VtabUnlockList() for an
17996** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
17997** list without holding the corresponding sqlite3.mutex mutex.
17998**
17999** The memory for objects of this type is always allocated by
18000** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
18001** the first argument.
18002*/
18003struct VTable {
18004 sqlite3 *db; /* Database connection associated with this table */
18005 Module *pMod; /* Pointer to module implementation */
18006 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
18007 int nRef; /* Number of pointers to this structure */
18008 u8 bConstraint; /* True if constraints are supported */
18009 u8 bAllSchemas; /* True if might use any attached schema */
18010 u8 eVtabRisk; /* Riskiness of allowing hacker access */
18011 int iSavepoint; /* Depth of the SAVEPOINT stack */
18012 VTable *pNext; /* Next in linked list (see above) */
18013};
18014
18015/* Allowed values for VTable.eVtabRisk
18016*/
18017#define SQLITE_VTABRISK_Low 0
18018#define SQLITE_VTABRISK_Normal 1
18019#define SQLITE_VTABRISK_High 2
18020
18021/*
18022** The schema for each SQL table, virtual table, and view is represented
18023** in memory by an instance of the following structure.
18024*/
18025struct Table {
18026 char *zName; /* Name of the table or view */
18027 Column *aCol; /* Information about each column */
18028 Index *pIndex; /* List of SQL indexes on this table. */
18029 char *zColAff; /* String defining the affinity of each column */
18030 ExprList *pCheck; /* All CHECK constraints */
18031 /* ... also used as column name list in a VIEW */
18032 Pgno tnum; /* Root BTree page for this table */
18033 u32 nTabRef; /* Number of pointers to this Table */
18034 u32 tabFlags; /* Mask of TF_* values */
18035 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
18036 i16 nCol; /* Number of columns in this table */
18037 i16 nNVCol; /* Number of columns that are not VIRTUAL */
18038 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18039 LogEst szTabRow; /* Estimated size of each table row in bytes */
18040#ifdef SQLITE_ENABLE_COSTMULT
18041 LogEst costMult; /* Cost multiplier for using this table */
18042#endif
18043 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
18044 u8 eTabType; /* 0: normal, 1: virtual, 2: view */
18045 union {
18046 struct { /* Used by ordinary tables: */
18047 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
18048 FKey *pFKey; /* Linked list of all foreign keys in this table */
18049 ExprList *pDfltList; /* DEFAULT clauses on various columns.
18050 ** Or the AS clause for generated columns. */
18051 } tab;
18052 struct { /* Used by views: */
18053 Select *pSelect; /* View definition */
18054 } view;
18055 struct { /* Used by virtual tables only: */
18056 int nArg; /* Number of arguments to the module */
18057 char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */
18058 VTable *p; /* List of VTable objects. */
18059 } vtab;
18060 } u;
18061 Trigger *pTrigger; /* List of triggers on this object */
18062 Schema *pSchema; /* Schema that contains this table */
18063};
18064
18065/*
18066** Allowed values for Table.tabFlags.
18067**
18068** TF_OOOHidden applies to tables or view that have hidden columns that are
18069** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18070** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18071** the TF_OOOHidden attribute would apply in this case. Such tables require
18072** special handling during INSERT processing. The "OOO" means "Out Of Order".
18073**
18074** Constraints:
18075**
18076** TF_HasVirtual == COLFLAG_VIRTUAL
18077** TF_HasStored == COLFLAG_STORED
18078** TF_HasHidden == COLFLAG_HIDDEN
18079*/
18080#define TF_Readonly 0x00000001 /* Read-only system table */
18081#define TF_HasHidden 0x00000002 /* Has one or more hidden columns */
18082#define TF_HasPrimaryKey 0x00000004 /* Table has a primary key */
18083#define TF_Autoincrement 0x00000008 /* Integer primary key is autoincrement */
18084#define TF_HasStat1 0x00000010 /* nRowLogEst set from sqlite_stat1 */
18085#define TF_HasVirtual 0x00000020 /* Has one or more VIRTUAL columns */
18086#define TF_HasStored 0x00000040 /* Has one or more STORED columns */
18087#define TF_HasGenerated 0x00000060 /* Combo: HasVirtual + HasStored */
18088#define TF_WithoutRowid 0x00000080 /* No rowid. PRIMARY KEY is the key */
18089#define TF_StatsUsed 0x00000100 /* Query planner decisions affected by
18090 ** Index.aiRowLogEst[] values */
18091#define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18092#define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18093#define TF_HasNotNull 0x00000800 /* Contains NOT NULL constraints */
18094#define TF_Shadow 0x00001000 /* True for a shadow table */
18095#define TF_HasStat4 0x00002000 /* STAT4 info available for this table */
18096#define TF_Ephemeral 0x00004000 /* An ephemeral table */
18097#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
18098#define TF_Strict 0x00010000 /* STRICT mode */
18099
18100/*
18101** Allowed values for Table.eTabType
18102*/
18103#define TABTYP_NORM 0 /* Ordinary table */
18104#define TABTYP_VTAB 1 /* Virtual table */
18105#define TABTYP_VIEW 2 /* A view */
18106
18107#define IsView(X) ((X)->eTabType==TABTYP_VIEW)
18108#define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
18109
18110/*
18111** Test to see whether or not a table is a virtual table. This is
18112** done as a macro so that it will be optimized out when virtual
18113** table support is omitted from the build.
18114*/
18115#ifndef SQLITE_OMIT_VIRTUALTABLE
18116# define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
18117# define ExprIsVtab(X) \
18118 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
18119#else
18120# define IsVirtual(X) 0
18121# define ExprIsVtab(X) 0
18122#endif
18123
18124/*
18125** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
18126** only works for non-virtual tables (ordinary tables and views) and is
18127** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
18128** IsHiddenColumn() macro is general purpose.
18129*/
18130#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
18131# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18132# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18133#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
18134# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18135# define IsOrdinaryHiddenColumn(X) 0
18136#else
18137# define IsHiddenColumn(X) 0
18138# define IsOrdinaryHiddenColumn(X) 0
18139#endif
18140
18141
18142/* Does the table have a rowid */
18143#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18144#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18145
18146/*
18147** Each foreign key constraint is an instance of the following structure.
18148**
18149** A foreign key is associated with two tables. The "from" table is
18150** the table that contains the REFERENCES clause that creates the foreign
18151** key. The "to" table is the table that is named in the REFERENCES clause.
18152** Consider this example:
18153**
18154** CREATE TABLE ex1(
18155** a INTEGER PRIMARY KEY,
18156** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
18157** );
18158**
18159** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18160** Equivalent names:
18161**
18162** from-table == child-table
18163** to-table == parent-table
18164**
18165** Each REFERENCES clause generates an instance of the following structure
18166** which is attached to the from-table. The to-table need not exist when
18167** the from-table is created. The existence of the to-table is not checked.
18168**
18169** The list of all parents for child Table X is held at X.pFKey.
18170**
18171** A list of all children for a table named Z (which might not even exist)
18172** is held in Schema.fkeyHash with a hash key of Z.
18173*/
18174struct FKey {
18175 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
18176 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
18177 char *zTo; /* Name of table that the key points to (aka: Parent) */
18178 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
18179 FKey *pPrevTo; /* Previous with the same zTo */
18180 int nCol; /* Number of columns in this key */
18181 /* EV: R-30323-21917 */
18182 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
18183 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
18184 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
18185 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
18186 int iFrom; /* Index of column in pFrom */
18187 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
18188 } aCol[1]; /* One entry for each of nCol columns */
18189};
18190
18191/*
18192** SQLite supports many different ways to resolve a constraint
18193** error. ROLLBACK processing means that a constraint violation
18194** causes the operation in process to fail and for the current transaction
18195** to be rolled back. ABORT processing means the operation in process
18196** fails and any prior changes from that one operation are backed out,
18197** but the transaction is not rolled back. FAIL processing means that
18198** the operation in progress stops and returns an error code. But prior
18199** changes due to the same operation are not backed out and no rollback
18200** occurs. IGNORE means that the particular row that caused the constraint
18201** error is not inserted or updated. Processing continues and no error
18202** is returned. REPLACE means that preexisting database rows that caused
18203** a UNIQUE constraint violation are removed so that the new insert or
18204** update can proceed. Processing continues and no error is reported.
18205** UPDATE applies to insert operations only and means that the insert
18206** is omitted and the DO UPDATE clause of an upsert is run instead.
18207**
18208** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys.
18209** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
18210** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
18211** key is set to NULL. SETDFLT means that the foreign key is set
18212** to its default value. CASCADE means that a DELETE or UPDATE of the
18213** referenced table row is propagated into the row that holds the
18214** foreign key.
18215**
18216** The OE_Default value is a place holder that means to use whatever
18217** conflict resolution algorithm is required from context.
18218**
18219** The following symbolic values are used to record which type
18220** of conflict resolution action to take.
18221*/
18222#define OE_None 0 /* There is no constraint to check */
18223#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
18224#define OE_Abort 2 /* Back out changes but do no rollback transaction */
18225#define OE_Fail 3 /* Stop the operation but leave all prior changes */
18226#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
18227#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
18228#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
18229#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
18230#define OE_SetNull 8 /* Set the foreign key value to NULL */
18231#define OE_SetDflt 9 /* Set the foreign key value to its default */
18232#define OE_Cascade 10 /* Cascade the changes */
18233#define OE_Default 11 /* Do whatever the default action is */
18234
18235
18236/*
18237** An instance of the following structure is passed as the first
18238** argument to sqlite3VdbeKeyCompare and is used to control the
18239** comparison of the two index keys.
18240**
18241** Note that aSortOrder[] and aColl[] have nField+1 slots. There
18242** are nField slots for the columns of an index then one extra slot
18243** for the rowid at the end.
18244*/
18245struct KeyInfo {
18246 u32 nRef; /* Number of references to this KeyInfo object */
18247 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18248 u16 nKeyField; /* Number of key columns in the index */
18249 u16 nAllField; /* Total columns, including key plus others */
18250 sqlite3 *db; /* The database connection */
18251 u8 *aSortFlags; /* Sort order for each column. */
18252 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
18253};
18254
18255/*
18256** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
18257*/
18258#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */
18259#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */
18260
18261/*
18262** This object holds a record which has been parsed out into individual
18263** fields, for the purposes of doing a comparison.
18264**
18265** A record is an object that contains one or more fields of data.
18266** Records are used to store the content of a table row and to store
18267** the key of an index. A blob encoding of a record is created by
18268** the OP_MakeRecord opcode of the VDBE and is disassembled by the
18269** OP_Column opcode.
18270**
18271** An instance of this object serves as a "key" for doing a search on
18272** an index b+tree. The goal of the search is to find the entry that
18273** is closed to the key described by this object. This object might hold
18274** just a prefix of the key. The number of fields is given by
18275** pKeyInfo->nField.
18276**
18277** The r1 and r2 fields are the values to return if this key is less than
18278** or greater than a key in the btree, respectively. These are normally
18279** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18280** is in DESC order.
18281**
18282** The key comparison functions actually return default_rc when they find
18283** an equals comparison. default_rc can be -1, 0, or +1. If there are
18284** multiple entries in the b-tree with the same key (when only looking
18285** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18286** cause the search to find the last match, or +1 to cause the search to
18287** find the first match.
18288**
18289** The key comparison functions will set eqSeen to true if they ever
18290** get and equal results when comparing this structure to a b-tree record.
18291** When default_rc!=0, the search might end up on the record immediately
18292** before the first match or immediately after the last match. The
18293** eqSeen field will indicate whether or not an exact match exists in the
18294** b-tree.
18295*/
18296struct UnpackedRecord {
18297 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18298 Mem *aMem; /* Values */
18299 union {
18300 char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */
18301 i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */
18302 } u;
18303 int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */
18304 u16 nField; /* Number of entries in apMem[] */
18305 i8 default_rc; /* Comparison result if keys are equal */
18306 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
18307 i8 r1; /* Value to return if (lhs < rhs) */
18308 i8 r2; /* Value to return if (lhs > rhs) */
18309 u8 eqSeen; /* True if an equality comparison has been seen */
18310};
18311
18312
18313/*
18314** Each SQL index is represented in memory by an
18315** instance of the following structure.
18316**
18317** The columns of the table that are to be indexed are described
18318** by the aiColumn[] field of this structure. For example, suppose
18319** we have the following table and index:
18320**
18321** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
18322** CREATE INDEX Ex2 ON Ex1(c3,c1);
18323**
18324** In the Table structure describing Ex1, nCol==3 because there are
18325** three columns in the table. In the Index structure describing
18326** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
18327** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
18328** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
18329** The second column to be indexed (c1) has an index of 0 in
18330** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
18331**
18332** The Index.onError field determines whether or not the indexed columns
18333** must be unique and what to do if they are not. When Index.onError=OE_None,
18334** it means this is not a unique index. Otherwise it is a unique index
18335** and the value of Index.onError indicates which conflict resolution
18336** algorithm to employ when an attempt is made to insert a non-unique
18337** element.
18338**
18339** The colNotIdxed bitmask is used in combination with SrcItem.colUsed
18340** for a fast test to see if an index can serve as a covering index.
18341** colNotIdxed has a 1 bit for every column of the original table that
18342** is *not* available in the index. Thus the expression
18343** "colUsed & colNotIdxed" will be non-zero if the index is not a
18344** covering index. The most significant bit of of colNotIdxed will always
18345** be true (note-20221022-a). If a column beyond the 63rd column of the
18346** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18347** and we have to assume either that the index is not covering, or use
18348** an alternative (slower) algorithm to determine whether or not
18349** the index is covering.
18350**
18351** While parsing a CREATE TABLE or CREATE INDEX statement in order to
18352** generate VDBE code (as opposed to parsing one read from an sqlite_schema
18353** table as part of parsing an existing database schema), transient instances
18354** of this structure may be created. In this case the Index.tnum variable is
18355** used to store the address of a VDBE instruction, not a database page
18356** number (it cannot - the database page is not allocated until the VDBE
18357** program is executed). See convertToWithoutRowidTable() for details.
18358*/
18359struct Index {
18360 char *zName; /* Name of this index */
18361 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
18362 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
18363 Table *pTable; /* The SQL table being indexed */
18364 char *zColAff; /* String defining the affinity of each column */
18365 Index *pNext; /* The next index associated with the same table */
18366 Schema *pSchema; /* Schema containing this index */
18367 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
18368 const char **azColl; /* Array of collation sequence names for index */
18369 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
18370 ExprList *aColExpr; /* Column expressions */
18371 Pgno tnum; /* DB Page containing root of this index */
18372 LogEst szIdxRow; /* Estimated average row size in bytes */
18373 u16 nKeyCol; /* Number of columns forming the key */
18374 u16 nColumn; /* Number of columns stored in the index */
18375 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
18376 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
18377 unsigned bUnordered:1; /* Use this index for == or IN queries only */
18378 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
18379 unsigned isResized:1; /* True if resizeIndexObject() has been called */
18380 unsigned isCovering:1; /* True if this is a covering index */
18381 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18382 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
18383 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
18384 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
18385 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
18386 unsigned bHasExpr:1; /* Index contains an expression, either a literal
18387 ** expression, or a reference to a VIRTUAL column */
18388#ifdef SQLITE_ENABLE_STAT4
18389 int nSample; /* Number of elements in aSample[] */
18390 int mxSample; /* Number of slots allocated to aSample[] */
18391 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
18392 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
18393 IndexSample *aSample; /* Samples of the left-most key */
18394 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18395 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18396#endif
18397 Bitmask colNotIdxed; /* Unindexed columns in pTab */
18398};
18399
18400/*
18401** Allowed values for Index.idxType
18402*/
18403#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
18404#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
18405#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
18406#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */
18407
18408/* Return true if index X is a PRIMARY KEY index */
18409#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18410
18411/* Return true if index X is a UNIQUE index */
18412#define IsUniqueIndex(X) ((X)->onError!=OE_None)
18413
18414/* The Index.aiColumn[] values are normally positive integer. But
18415** there are some negative values that have special meaning:
18416*/
18417#define XN_ROWID (-1) /* Indexed column is the rowid */
18418#define XN_EXPR (-2) /* Indexed column is an expression */
18419
18420/*
18421** Each sample stored in the sqlite_stat4 table is represented in memory
18422** using a structure of this type. See documentation at the top of the
18423** analyze.c source file for additional information.
18424*/
18425struct IndexSample {
18426 void *p; /* Pointer to sampled record */
18427 int n; /* Size of record in bytes */
18428 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
18429 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
18430 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
18431};
18432
18433/*
18434** Possible values to use within the flags argument to sqlite3GetToken().
18435*/
18436#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
18437#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
18438
18439/*
18440** Each token coming out of the lexer is an instance of
18441** this structure. Tokens are also used as part of an expression.
18442**
18443** The memory that "z" points to is owned by other objects. Take care
18444** that the owner of the "z" string does not deallocate the string before
18445** the Token goes out of scope! Very often, the "z" points to some place
18446** in the middle of the Parse.zSql text. But it might also point to a
18447** static string.
18448*/
18449struct Token {
18450 const char *z; /* Text of the token. Not NULL-terminated! */
18451 unsigned int n; /* Number of characters in this token */
18452};
18453
18454/*
18455** An instance of this structure contains information needed to generate
18456** code for a SELECT that contains aggregate functions.
18457**
18458** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
18459** pointer to this structure. The Expr.iAgg field is the index in
18460** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
18461** code for that node.
18462**
18463** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
18464** original Select structure that describes the SELECT statement. These
18465** fields do not need to be freed when deallocating the AggInfo structure.
18466*/
18467struct AggInfo {
18468 u8 directMode; /* Direct rendering mode means take data directly
18469 ** from source tables rather than from accumulators */
18470 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
18471 ** than the source table */
18472 u16 nSortingColumn; /* Number of columns in the sorting index */
18473 int sortingIdx; /* Cursor number of the sorting index */
18474 int sortingIdxPTab; /* Cursor number of pseudo-table */
18475 int iFirstReg; /* First register in range for aCol[] and aFunc[] */
18476 ExprList *pGroupBy; /* The group by clause */
18477 struct AggInfo_col { /* For each column used in source tables */
18478 Table *pTab; /* Source table */
18479 Expr *pCExpr; /* The original expression */
18480 int iTable; /* Cursor number of the source table */
18481 i16 iColumn; /* Column number within the source table */
18482 i16 iSorterColumn; /* Column number in the sorting index */
18483 } *aCol;
18484 int nColumn; /* Number of used entries in aCol[] */
18485 int nAccumulator; /* Number of columns that show through to the output.
18486 ** Additional columns are used only as parameters to
18487 ** aggregate functions */
18488 struct AggInfo_func { /* For each aggregate function */
18489 Expr *pFExpr; /* Expression encoding the function */
18490 FuncDef *pFunc; /* The aggregate function implementation */
18491 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
18492 int iDistAddr; /* Address of OP_OpenEphemeral */
18493 } *aFunc;
18494 int nFunc; /* Number of entries in aFunc[] */
18495 u32 selId; /* Select to which this AggInfo belongs */
18496#ifdef SQLITE_DEBUG
18497 Select *pSelect; /* SELECT statement that this AggInfo supports */
18498#endif
18499};
18500
18501/*
18502** Macros to compute aCol[] and aFunc[] register numbers.
18503**
18504** These macros should not be used prior to the call to
18505** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18506** The assert()s that are part of this macro verify that constraint.
18507*/
18508#define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18509#define AggInfoFuncReg(A,I) \
18510 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18511
18512/*
18513** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18514** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18515** than 32767 we have to make it 32-bit. 16-bit is preferred because
18516** it uses less memory in the Expr object, which is a big memory user
18517** in systems with lots of prepared statements. And few applications
18518** need more than about 10 or 20 variables. But some extreme users want
18519** to have prepared statements with over 32766 variables, and for them
18520** the option is available (at compile-time).
18521*/
18522#if SQLITE_MAX_VARIABLE_NUMBER<32767
18523typedef i16 ynVar;
18524#else
18525typedef int ynVar;
18526#endif
18527
18528/*
18529** Each node of an expression in the parse tree is an instance
18530** of this structure.
18531**
18532** Expr.op is the opcode. The integer parser token codes are reused
18533** as opcodes here. For example, the parser defines TK_GE to be an integer
18534** code representing the ">=" operator. This same integer code is reused
18535** to represent the greater-than-or-equal-to operator in the expression
18536** tree.
18537**
18538** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
18539** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If
18540** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the
18541** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
18542** then Expr.u.zToken contains the name of the function.
18543**
18544** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
18545** binary operator. Either or both may be NULL.
18546**
18547** Expr.x.pList is a list of arguments if the expression is an SQL function,
18548** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
18549** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18550** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
18551** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
18552** valid.
18553**
18554** An expression of the form ID or ID.ID refers to a column in a table.
18555** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
18556** the integer cursor number of a VDBE cursor pointing to that table and
18557** Expr.iColumn is the column number for the specific column. If the
18558** expression is used as a result in an aggregate SELECT, then the
18559** value is also stored in the Expr.iAgg column in the aggregate so that
18560** it can be accessed after all aggregates are computed.
18561**
18562** If the expression is an unbound variable marker (a question mark
18563** character '?' in the original SQL) then the Expr.iTable holds the index
18564** number for that variable.
18565**
18566** If the expression is a subquery then Expr.iColumn holds an integer
18567** register number containing the result of the subquery. If the
18568** subquery gives a constant result, then iTable is -1. If the subquery
18569** gives a different answer at different times during statement processing
18570** then iTable is the address of a subroutine that computes the subquery.
18571**
18572** If the Expr is of type OP_Column, and the table it is selecting from
18573** is a disk table or the "old.*" pseudo-table, then pTab points to the
18574** corresponding table definition.
18575**
18576** ALLOCATION NOTES:
18577**
18578** Expr objects can use a lot of memory space in database schema. To
18579** help reduce memory requirements, sometimes an Expr object will be
18580** truncated. And to reduce the number of memory allocations, sometimes
18581** two or more Expr objects will be stored in a single memory allocation,
18582** together with Expr.u.zToken strings.
18583**
18584** If the EP_Reduced and EP_TokenOnly flags are set when
18585** an Expr object is truncated. When EP_Reduced is set, then all
18586** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
18587** are contained within the same memory allocation. Note, however, that
18588** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
18589** allocated, regardless of whether or not EP_Reduced is set.
18590*/
18591struct Expr {
18592 u8 op; /* Operation performed by this node */
18593 char affExpr; /* affinity, or RAISE type */
18594 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
18595 ** TK_COLUMN: the value of p5 for OP_Column
18596 ** TK_AGG_FUNCTION: nesting depth
18597 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
18598#ifdef SQLITE_DEBUG
18599 u8 vvaFlags; /* Verification flags. */
18600#endif
18601 u32 flags; /* Various flags. EP_* See below */
18602 union {
18603 char *zToken; /* Token value. Zero terminated and dequoted */
18604 int iValue; /* Non-negative integer value if EP_IntValue */
18605 } u;
18606
18607 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
18608 ** space is allocated for the fields below this point. An attempt to
18609 ** access them will result in a segfault or malfunction.
18610 *********************************************************************/
18611
18612 Expr *pLeft; /* Left subnode */
18613 Expr *pRight; /* Right subnode */
18614 union {
18615 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
18616 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
18617 } x;
18618
18619 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
18620 ** space is allocated for the fields below this point. An attempt to
18621 ** access them will result in a segfault or malfunction.
18622 *********************************************************************/
18623
18624#if SQLITE_MAX_EXPR_DEPTH>0
18625 int nHeight; /* Height of the tree headed by this node */
18626#endif
18627 int iTable; /* TK_COLUMN: cursor number of table holding column
18628 ** TK_REGISTER: register number
18629 ** TK_TRIGGER: 1 -> new, 0 -> old
18630 ** EP_Unlikely: 134217728 times likelihood
18631 ** TK_IN: ephemeral table holding RHS
18632 ** TK_SELECT_COLUMN: Number of columns on the LHS
18633 ** TK_SELECT: 1st register of result vector */
18634 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18635 ** TK_VARIABLE: variable number (always >= 1).
18636 ** TK_SELECT_COLUMN: column of the result vector */
18637 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18638 union {
18639 int iJoin; /* If EP_OuterON or EP_InnerON, the right table */
18640 int iOfst; /* else: start of token from start of statement */
18641 } w;
18642 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
18643 union {
18644 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
18645 ** for a column of an index on an expression */
18646 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
18647 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
18648 int iAddr; /* Subroutine entry address */
18649 int regReturn; /* Register used to hold return address */
18650 } sub;
18651 } y;
18652};
18653
18654/* The following are the meanings of bits in the Expr.flags field.
18655** Value restrictions:
18656**
18657** EP_Agg == NC_HasAgg == SF_HasAgg
18658** EP_Win == NC_HasWin
18659*/
18660#define EP_OuterON 0x000001 /* Originates in ON/USING clause of outer join */
18661#define EP_InnerON 0x000002 /* Originates in ON/USING of an inner join */
18662#define EP_Distinct 0x000004 /* Aggregate function with DISTINCT keyword */
18663#define EP_HasFunc 0x000008 /* Contains one or more functions of any kind */
18664#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
18665#define EP_FixedCol 0x000020 /* TK_Column with a known fixed value */
18666#define EP_VarSelect 0x000040 /* pSelect is correlated, not constant */
18667#define EP_DblQuoted 0x000080 /* token.z was originally in "..." */
18668#define EP_InfixFunc 0x000100 /* True for an infix function: LIKE, GLOB, etc */
18669#define EP_Collate 0x000200 /* Tree contains a TK_COLLATE operator */
18670#define EP_Commuted 0x000400 /* Comparison operator has been commuted */
18671#define EP_IntValue 0x000800 /* Integer value contained in u.iValue */
18672#define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
18673#define EP_Skip 0x002000 /* Operator does not contribute to affinity */
18674#define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
18675#define EP_Win 0x008000 /* Contains window functions */
18676#define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18677 /* 0x020000 // Available for reuse */
18678#define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */
18679#define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */
18680#define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
18681#define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */
18682#define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */
18683#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
18684#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
18685#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
18686#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
18687#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
18688#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
18689#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
18690#define EP_FromDDL 0x40000000 /* Originates from sqlite_schema */
18691 /* 0x80000000 // Available */
18692
18693/* The EP_Propagate mask is a set of properties that automatically propagate
18694** upwards into parent nodes.
18695*/
18696#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
18697
18698/* Macros can be used to test, set, or clear bits in the
18699** Expr.flags field.
18700*/
18701#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18702#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18703#define ExprSetProperty(E,P) (E)->flags|=(P)
18704#define ExprClearProperty(E,P) (E)->flags&=~(P)
18705#define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
18706#define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
18707
18708/* Macros used to ensure that the correct members of unions are accessed
18709** in Expr.
18710*/
18711#define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
18712#define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
18713#define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
18714#define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
18715#define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
18716#define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
18717#define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
18718#define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
18719#define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
18720
18721/* Flags for use with Expr.vvaFlags
18722*/
18723#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
18724#define EP_Immutable 0x02 /* Do not change this Expr node */
18725
18726/* The ExprSetVVAProperty() macro is used for Verification, Validation,
18727** and Accreditation only. It works like ExprSetProperty() during VVA
18728** processes but is a no-op for delivery.
18729*/
18730#ifdef SQLITE_DEBUG
18731# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18732# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18733# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18734#else
18735# define ExprSetVVAProperty(E,P)
18736# define ExprHasVVAProperty(E,P) 0
18737# define ExprClearVVAProperties(E)
18738#endif
18739
18740/*
18741** Macros to determine the number of bytes required by a normal Expr
18742** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
18743** and an Expr struct with the EP_TokenOnly flag set.
18744*/
18745#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
18746#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
18747#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
18748
18749/*
18750** Flags passed to the sqlite3ExprDup() function. See the header comment
18751** above sqlite3ExprDup() for details.
18752*/
18753#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18754
18755/*
18756** True if the expression passed as an argument was a function with
18757** an OVER() clause (a window function).
18758*/
18759#ifdef SQLITE_OMIT_WINDOWFUNC
18760# define IsWindowFunc(p) 0
18761#else
18762# define IsWindowFunc(p) ( \
18763 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18764 )
18765#endif
18766
18767/*
18768** A list of expressions. Each expression may optionally have a
18769** name. An expr/name combination can be used in several ways, such
18770** as the list of "expr AS ID" fields following a "SELECT" or in the
18771** list of "ID = expr" items in an UPDATE. A list of expressions can
18772** also be used as the argument to a function, in which case the a.zName
18773** field is not used.
18774**
18775** In order to try to keep memory usage down, the Expr.a.zEName field
18776** is used for multiple purposes:
18777**
18778** eEName Usage
18779** ---------- -------------------------
18780** ENAME_NAME (1) the AS of result set column
18781** (2) COLUMN= of an UPDATE
18782**
18783** ENAME_TAB DB.TABLE.NAME used to resolve names
18784** of subqueries
18785**
18786** ENAME_SPAN Text of the original result set
18787** expression.
18788*/
18789struct ExprList {
18790 int nExpr; /* Number of expressions on the list */
18791 int nAlloc; /* Number of a[] slots allocated */
18792 struct ExprList_item { /* For each expression in the list */
18793 Expr *pExpr; /* The parse tree for this expression */
18794 char *zEName; /* Token associated with this expression */
18795 struct {
18796 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
18797 unsigned eEName :2; /* Meaning of zEName */
18798 unsigned done :1; /* Indicates when processing is finished */
18799 unsigned reusable :1; /* Constant expression is reusable */
18800 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
18801 unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */
18802 unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */
18803 unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */
18804 unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should
18805 ** not be expanded by "*" in parent queries */
18806 } fg;
18807 union {
18808 struct { /* Used by any ExprList other than Parse.pConsExpr */
18809 u16 iOrderByCol; /* For ORDER BY, column number in result set */
18810 u16 iAlias; /* Index into Parse.aAlias[] for zName */
18811 } x;
18812 int iConstExprReg; /* Register in which Expr value is cached. Used only
18813 ** by Parse.pConstExpr */
18814 } u;
18815 } a[1]; /* One slot for each expression in the list */
18816};
18817
18818/*
18819** Allowed values for Expr.a.eEName
18820*/
18821#define ENAME_NAME 0 /* The AS clause of a result set */
18822#define ENAME_SPAN 1 /* Complete text of the result set expression */
18823#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
18824
18825/*
18826** An instance of this structure can hold a simple list of identifiers,
18827** such as the list "a,b,c" in the following statements:
18828**
18829** INSERT INTO t(a,b,c) VALUES ...;
18830** CREATE INDEX idx ON t(a,b,c);
18831** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
18832**
18833** The IdList.a.idx field is used when the IdList represents the list of
18834** column names after a table name in an INSERT statement. In the statement
18835**
18836** INSERT INTO t(a,b,c) ...
18837**
18838** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18839*/
18840struct IdList {
18841 int nId; /* Number of identifiers on the list */
18842 u8 eU4; /* Which element of a.u4 is valid */
18843 struct IdList_item {
18844 char *zName; /* Name of the identifier */
18845 union {
18846 int idx; /* Index in some Table.aCol[] of a column named zName */
18847 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
18848 } u4;
18849 } a[1];
18850};
18851
18852/*
18853** Allowed values for IdList.eType, which determines which value of the a.u4
18854** is valid.
18855*/
18856#define EU4_NONE 0 /* Does not use IdList.a.u4 */
18857#define EU4_IDX 1 /* Uses IdList.a.u4.idx */
18858#define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
18859
18860/*
18861** The SrcItem object represents a single term in the FROM clause of a query.
18862** The SrcList object is mostly an array of SrcItems.
18863**
18864** The jointype starts out showing the join type between the current table
18865** and the next table on the list. The parser builds the list this way.
18866** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18867** jointype expresses the join between the table and the previous table.
18868**
18869** In the colUsed field, the high-order bit (bit 63) is set if the table
18870** contains more than 63 columns and the 64-th or later column is used.
18871**
18872** Union member validity:
18873**
18874** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
18875** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
18876** u2.pIBIndex fg.isIndexedBy && !fg.isCte
18877** u2.pCteUse fg.isCte && !fg.isIndexedBy
18878*/
18879struct SrcItem {
18880 Schema *pSchema; /* Schema to which this item is fixed */
18881 char *zDatabase; /* Name of database holding this table */
18882 char *zName; /* Name of the table */
18883 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
18884 Table *pTab; /* An SQL table corresponding to zName */
18885 Select *pSelect; /* A SELECT statement used in place of a table name */
18886 int addrFillSub; /* Address of subroutine to manifest a subquery */
18887 int regReturn; /* Register holding return address of addrFillSub */
18888 int regResult; /* Registers holding results of a co-routine */
18889 struct {
18890 u8 jointype; /* Type of join between this table and the previous */
18891 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
18892 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
18893 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18894 unsigned isCorrelated :1; /* True if sub-query is correlated */
18895 unsigned isMaterialized:1; /* This is a materialized view */
18896 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18897 unsigned isRecursive :1; /* True for recursive reference in WITH */
18898 unsigned fromDDL :1; /* Comes from sqlite_schema */
18899 unsigned isCte :1; /* This is a CTE */
18900 unsigned notCte :1; /* This item may not match a CTE */
18901 unsigned isUsing :1; /* u3.pUsing is valid */
18902 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18903 unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */
18904 unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
18905 } fg;
18906 int iCursor; /* The VDBE cursor number used to access this table */
18907 union {
18908 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
18909 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
18910 } u3;
18911 Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
18912 union {
18913 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
18914 ExprList *pFuncArg; /* Arguments to table-valued-function */
18915 } u1;
18916 union {
18917 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18918 CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */
18919 } u2;
18920};
18921
18922/*
18923** The OnOrUsing object represents either an ON clause or a USING clause.
18924** It can never be both at the same time, but it can be neither.
18925*/
18926struct OnOrUsing {
18927 Expr *pOn; /* The ON clause of a join */
18928 IdList *pUsing; /* The USING clause of a join */
18929};
18930
18931/*
18932** This object represents one or more tables that are the source of
18933** content for an SQL statement. For example, a single SrcList object
18934** is used to hold the FROM clause of a SELECT statement. SrcList also
18935** represents the target tables for DELETE, INSERT, and UPDATE statements.
18936**
18937*/
18938struct SrcList {
18939 int nSrc; /* Number of tables or subqueries in the FROM clause */
18940 u32 nAlloc; /* Number of entries allocated in a[] below */
18941 SrcItem a[1]; /* One entry for each identifier on the list */
18942};
18943
18944/*
18945** Permitted values of the SrcList.a.jointype field
18946*/
18947#define JT_INNER 0x01 /* Any kind of inner or cross join */
18948#define JT_CROSS 0x02 /* Explicit use of the CROSS keyword */
18949#define JT_NATURAL 0x04 /* True for a "natural" join */
18950#define JT_LEFT 0x08 /* Left outer join */
18951#define JT_RIGHT 0x10 /* Right outer join */
18952#define JT_OUTER 0x20 /* The "OUTER" keyword is present */
18953#define JT_LTORJ 0x40 /* One of the LEFT operands of a RIGHT JOIN
18954 ** Mnemonic: Left Table Of Right Join */
18955#define JT_ERROR 0x80 /* unknown or unsupported join type */
18956
18957/*
18958** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
18959** and the WhereInfo.wctrlFlags member.
18960**
18961** Value constraints (enforced via assert()):
18962** WHERE_USE_LIMIT == SF_FixedLimit
18963*/
18964#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18965#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
18966#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
18967#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18968#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
18969#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
18970#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18971 ** the OR optimization */
18972#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
18973#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
18974#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
18975#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
18976#define WHERE_AGG_DISTINCT 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
18977#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
18978#define WHERE_RIGHT_JOIN 0x1000 /* Processing a RIGHT JOIN */
18979 /* 0x2000 not currently used */
18980#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
18981 /* 0x8000 not currently used */
18982
18983/* Allowed return values from sqlite3WhereIsDistinct()
18984*/
18985#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
18986#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
18987#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
18988#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
18989
18990/*
18991** A NameContext defines a context in which to resolve table and column
18992** names. The context consists of a list of tables (the pSrcList) field and
18993** a list of named expression (pEList). The named expression list may
18994** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
18995** to the table being operated on by INSERT, UPDATE, or DELETE. The
18996** pEList corresponds to the result set of a SELECT and is NULL for
18997** other statements.
18998**
18999** NameContexts can be nested. When resolving names, the inner-most
19000** context is searched first. If no match is found, the next outer
19001** context is checked. If there is still no match, the next context
19002** is checked. This process continues until either a match is found
19003** or all contexts are check. When a match is found, the nRef member of
19004** the context containing the match is incremented.
19005**
19006** Each subquery gets a new NameContext. The pNext field points to the
19007** NameContext in the parent query. Thus the process of scanning the
19008** NameContext list corresponds to searching through successively outer
19009** subqueries looking for a match.
19010*/
19011struct NameContext {
19012 Parse *pParse; /* The parser */
19013 SrcList *pSrcList; /* One or more tables used to resolve names */
19014 union {
19015 ExprList *pEList; /* Optional list of result-set columns */
19016 AggInfo *pAggInfo; /* Information about aggregates at this level */
19017 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
19018 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
19019 } uNC;
19020 NameContext *pNext; /* Next outer name context. NULL for outermost */
19021 int nRef; /* Number of names resolved by this context */
19022 int nNcErr; /* Number of errors encountered while resolving names */
19023 int ncFlags; /* Zero or more NC_* flags defined below */
19024 Select *pWinSelect; /* SELECT statement for any window functions */
19025};
19026
19027/*
19028** Allowed values for the NameContext, ncFlags field.
19029**
19030** Value constraints (all checked via assert()):
19031** NC_HasAgg == SF_HasAgg == EP_Agg
19032** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
19033** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER
19034** NC_HasWin == EP_Win
19035**
19036*/
19037#define NC_AllowAgg 0x000001 /* Aggregate functions are allowed here */
19038#define NC_PartIdx 0x000002 /* True if resolving a partial index WHERE */
19039#define NC_IsCheck 0x000004 /* True if resolving a CHECK constraint */
19040#define NC_GenCol 0x000008 /* True for a GENERATED ALWAYS AS clause */
19041#define NC_HasAgg 0x000010 /* One or more aggregate functions seen */
19042#define NC_IdxExpr 0x000020 /* True if resolving columns of CREATE INDEX */
19043#define NC_SelfRef 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
19044#define NC_Subquery 0x000040 /* A subquery has been seen */
19045#define NC_UEList 0x000080 /* True if uNC.pEList is used */
19046#define NC_UAggInfo 0x000100 /* True if uNC.pAggInfo is used */
19047#define NC_UUpsert 0x000200 /* True if uNC.pUpsert is used */
19048#define NC_UBaseReg 0x000400 /* True if uNC.iBaseReg is used */
19049#define NC_MinMaxAgg 0x001000 /* min/max aggregates seen. See note above */
19050#define NC_Complex 0x002000 /* True if a function or subquery seen */
19051#define NC_AllowWin 0x004000 /* Window functions are allowed here */
19052#define NC_HasWin 0x008000 /* One or more window functions seen */
19053#define NC_IsDDL 0x010000 /* Resolving names in a CREATE statement */
19054#define NC_InAggFunc 0x020000 /* True if analyzing arguments to an agg func */
19055#define NC_FromDDL 0x040000 /* SQL text comes from sqlite_schema */
19056#define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
19057#define NC_OrderAgg 0x8000000 /* Has an aggregate other than count/min/max */
19058
19059/*
19060** An instance of the following object describes a single ON CONFLICT
19061** clause in an upsert.
19062**
19063** The pUpsertTarget field is only set if the ON CONFLICT clause includes
19064** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19065** conflict-target clause.) The pUpsertTargetWhere is the optional
19066** WHERE clause used to identify partial unique indexes.
19067**
19068** pUpsertSet is the list of column=expr terms of the UPDATE statement.
19069** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
19070** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
19071** WHERE clause is omitted.
19072*/
19073struct Upsert {
19074 ExprList *pUpsertTarget; /* Optional description of conflict target */
19075 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
19076 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
19077 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
19078 Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
19079 u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
19080 /* Above this point is the parse tree for the ON CONFLICT clauses.
19081 ** The next group of fields stores intermediate data. */
19082 void *pToFree; /* Free memory when deleting the Upsert object */
19083 /* All fields above are owned by the Upsert object and must be freed
19084 ** when the Upsert is destroyed. The fields below are used to transfer
19085 ** information from the INSERT processing down into the UPDATE processing
19086 ** while generating code. The fields below are owned by the INSERT
19087 ** statement and will be freed by INSERT processing. */
19088 Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */
19089 SrcList *pUpsertSrc; /* Table to be updated */
19090 int regData; /* First register holding array of VALUES */
19091 int iDataCur; /* Index of the data cursor */
19092 int iIdxCur; /* Index of the first index cursor */
19093};
19094
19095/*
19096** An instance of the following structure contains all information
19097** needed to generate code for a single SELECT statement.
19098**
19099** See the header comment on the computeLimitRegisters() routine for a
19100** detailed description of the meaning of the iLimit and iOffset fields.
19101**
19102** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
19103** These addresses must be stored so that we can go back and fill in
19104** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
19105** the number of columns in P2 can be computed at the same time
19106** as the OP_OpenEphm instruction is coded because not
19107** enough information about the compound query is known at that point.
19108** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
19109** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
19110** sequences for the ORDER BY clause.
19111*/
19112struct Select {
19113 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
19114 LogEst nSelectRow; /* Estimated number of result rows */
19115 u32 selFlags; /* Various SF_* values */
19116 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
19117 u32 selId; /* Unique identifier number for this SELECT */
19118 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
19119 ExprList *pEList; /* The fields of the result */
19120 SrcList *pSrc; /* The FROM clause */
19121 Expr *pWhere; /* The WHERE clause */
19122 ExprList *pGroupBy; /* The GROUP BY clause */
19123 Expr *pHaving; /* The HAVING clause */
19124 ExprList *pOrderBy; /* The ORDER BY clause */
19125 Select *pPrior; /* Prior select in a compound select statement */
19126 Select *pNext; /* Next select to the left in a compound */
19127 Expr *pLimit; /* LIMIT expression. NULL means not used. */
19128 With *pWith; /* WITH clause attached to this select. Or NULL. */
19129#ifndef SQLITE_OMIT_WINDOWFUNC
19130 Window *pWin; /* List of window functions */
19131 Window *pWinDefn; /* List of named window definitions */
19132#endif
19133};
19134
19135/*
19136** Allowed values for Select.selFlags. The "SF" prefix stands for
19137** "Select Flag".
19138**
19139** Value constraints (all checked via assert())
19140** SF_HasAgg == NC_HasAgg
19141** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
19142** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER
19143** SF_FixedLimit == WHERE_USE_LIMIT
19144*/
19145#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
19146#define SF_All 0x0000002 /* Includes the ALL keyword */
19147#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
19148#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
19149#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
19150#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
19151#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
19152#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
19153#define SF_Compound 0x0000100 /* Part of a compound query */
19154#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
19155#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
19156#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
19157#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
19158#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
19159#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
19160#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
19161#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
19162#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
19163#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
19164#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
19165#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
19166#define SF_View 0x0200000 /* SELECT statement is a view */
19167#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
19168#define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
19169#define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
19170#define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
19171#define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
19172#define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
19173#define SF_UpdateFrom 0x10000000 /* Query originates with UPDATE FROM */
19174
19175/* True if S exists and has SF_NestedFrom */
19176#define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
19177
19178/*
19179** The results of a SELECT can be distributed in several ways, as defined
19180** by one of the following macros. The "SRT" prefix means "SELECT Result
19181** Type".
19182**
19183** SRT_Union Store results as a key in a temporary index
19184** identified by pDest->iSDParm.
19185**
19186** SRT_Except Remove results from the temporary index pDest->iSDParm.
19187**
19188** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
19189** set is not empty.
19190**
19191** SRT_Discard Throw the results away. This is used by SELECT
19192** statements within triggers whose only purpose is
19193** the side-effects of functions.
19194**
19195** SRT_Output Generate a row of output (using the OP_ResultRow
19196** opcode) for each row in the result set.
19197**
19198** SRT_Mem Only valid if the result is a single column.
19199** Store the first column of the first result row
19200** in register pDest->iSDParm then abandon the rest
19201** of the query. This destination implies "LIMIT 1".
19202**
19203** SRT_Set The result must be a single column. Store each
19204** row of result as the key in table pDest->iSDParm.
19205** Apply the affinity pDest->affSdst before storing
19206** results. Used to implement "IN (SELECT ...)".
19207**
19208** SRT_EphemTab Create an temporary table pDest->iSDParm and store
19209** the result there. The cursor is left open after
19210** returning. This is like SRT_Table except that
19211** this destination uses OP_OpenEphemeral to create
19212** the table first.
19213**
19214** SRT_Coroutine Generate a co-routine that returns a new row of
19215** results each time it is invoked. The entry point
19216** of the co-routine is stored in register pDest->iSDParm
19217** and the result row is stored in pDest->nDest registers
19218** starting with pDest->iSdst.
19219**
19220** SRT_Table Store results in temporary table pDest->iSDParm.
19221** SRT_Fifo This is like SRT_EphemTab except that the table
19222** is assumed to already be open. SRT_Fifo has
19223** the additional property of being able to ignore
19224** the ORDER BY clause.
19225**
19226** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
19227** But also use temporary table pDest->iSDParm+1 as
19228** a record of all prior results and ignore any duplicate
19229** rows. Name means: "Distinct Fifo".
19230**
19231** SRT_Queue Store results in priority queue pDest->iSDParm (really
19232** an index). Append a sequence number so that all entries
19233** are distinct.
19234**
19235** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
19236** the same record has never been stored before. The
19237** index at pDest->iSDParm+1 hold all prior stores.
19238**
19239** SRT_Upfrom Store results in the temporary table already opened by
19240** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
19241** table is an intkey table - in this case the first
19242** column returned by the SELECT is used as the integer
19243** key. If (pDest->iSDParm>0), then the table is an index
19244** table. (pDest->iSDParm) is the number of key columns in
19245** each index record in this case.
19246*/
19247#define SRT_Union 1 /* Store result as keys in an index */
19248#define SRT_Except 2 /* Remove result from a UNION index */
19249#define SRT_Exists 3 /* Store 1 if the result is not empty */
19250#define SRT_Discard 4 /* Do not save the results anywhere */
19251#define SRT_DistFifo 5 /* Like SRT_Fifo, but unique results only */
19252#define SRT_DistQueue 6 /* Like SRT_Queue, but unique results only */
19253
19254/* The DISTINCT clause is ignored for all of the above. Not that
19255** IgnorableDistinct() implies IgnorableOrderby() */
19256#define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
19257
19258#define SRT_Queue 7 /* Store result in an queue */
19259#define SRT_Fifo 8 /* Store result as data with an automatic rowid */
19260
19261/* The ORDER BY clause is ignored for all of the above */
19262#define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19263
19264#define SRT_Output 9 /* Output each row of result */
19265#define SRT_Mem 10 /* Store result in a memory cell */
19266#define SRT_Set 11 /* Store results as keys in an index */
19267#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
19268#define SRT_Coroutine 13 /* Generate a single row of result */
19269#define SRT_Table 14 /* Store result as data with an automatic rowid */
19270#define SRT_Upfrom 15 /* Store result as data with rowid */
19271
19272/*
19273** An instance of this object describes where to put of the results of
19274** a SELECT statement.
19275*/
19276struct SelectDest {
19277 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
19278 int iSDParm; /* A parameter used by the eDest disposal method */
19279 int iSDParm2; /* A second parameter for the eDest disposal method */
19280 int iSdst; /* Base register where results are written */
19281 int nSdst; /* Number of registers allocated */
19282 char *zAffSdst; /* Affinity used for SRT_Set */
19283 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
19284};
19285
19286/*
19287** During code generation of statements that do inserts into AUTOINCREMENT
19288** tables, the following information is attached to the Table.u.autoInc.p
19289** pointer of each autoincrement table to record some side information that
19290** the code generator needs. We have to keep per-table autoincrement
19291** information in case inserts are done within triggers. Triggers do not
19292** normally coordinate their activities, but we do need to coordinate the
19293** loading and saving of autoincrement information.
19294*/
19295struct AutoincInfo {
19296 AutoincInfo *pNext; /* Next info block in a list of them all */
19297 Table *pTab; /* Table this info block refers to */
19298 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
19299 int regCtr; /* Memory register holding the rowid counter */
19300};
19301
19302/*
19303** At least one instance of the following structure is created for each
19304** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
19305** statement. All such objects are stored in the linked list headed at
19306** Parse.pTriggerPrg and deleted once statement compilation has been
19307** completed.
19308**
19309** A Vdbe sub-program that implements the body and WHEN clause of trigger
19310** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
19311** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
19312** The Parse.pTriggerPrg list never contains two entries with the same
19313** values for both pTrigger and orconf.
19314**
19315** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
19316** accessed (or set to 0 for triggers fired as a result of INSERT
19317** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
19318** a mask of new.* columns used by the program.
19319*/
19320struct TriggerPrg {
19321 Trigger *pTrigger; /* Trigger this program was coded from */
19322 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
19323 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
19324 int orconf; /* Default ON CONFLICT policy */
19325 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
19326};
19327
19328/*
19329** The yDbMask datatype for the bitmask of all attached databases.
19330*/
19331#if SQLITE_MAX_ATTACHED>30
19332 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
19333# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
19334# define DbMaskZero(M) memset((M),0,sizeof(M))
19335# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
19336# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
19337# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
19338#else
19339 typedef unsigned int yDbMask;
19340# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
19341# define DbMaskZero(M) ((M)=0)
19342# define DbMaskSet(M,I) ((M)|=(((yDbMask)1)<<(I)))
19343# define DbMaskAllZero(M) ((M)==0)
19344# define DbMaskNonZero(M) ((M)!=0)
19345#endif
19346
19347/*
19348** For each index X that has as one of its arguments either an expression
19349** or the name of a virtual generated column, and if X is in scope such that
19350** the value of the expression can simply be read from the index, then
19351** there is an instance of this object on the Parse.pIdxExpr list.
19352**
19353** During code generation, while generating code to evaluate expressions,
19354** this list is consulted and if a matching expression is found, the value
19355** is read from the index rather than being recomputed.
19356*/
19357struct IndexedExpr {
19358 Expr *pExpr; /* The expression contained in the index */
19359 int iDataCur; /* The data cursor associated with the index */
19360 int iIdxCur; /* The index cursor */
19361 int iIdxCol; /* The index column that contains value of pExpr */
19362 u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */
19363 u8 aff; /* Affinity of the pExpr expression */
19364 IndexedExpr *pIENext; /* Next in a list of all indexed expressions */
19365#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
19366 const char *zIdxName; /* Name of index, used only for bytecode comments */
19367#endif
19368};
19369
19370/*
19371** An instance of the ParseCleanup object specifies an operation that
19372** should be performed after parsing to deallocation resources obtained
19373** during the parse and which are no longer needed.
19374*/
19375struct ParseCleanup {
19376 ParseCleanup *pNext; /* Next cleanup task */
19377 void *pPtr; /* Pointer to object to deallocate */
19378 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
19379};
19380
19381/*
19382** An SQL parser context. A copy of this structure is passed through
19383** the parser and down into all the parser action routine in order to
19384** carry around information that is global to the entire parse.
19385**
19386** The structure is divided into two parts. When the parser and code
19387** generate call themselves recursively, the first part of the structure
19388** is constant but the second part is reset at the beginning and end of
19389** each recursion.
19390**
19391** The nTableLock and aTableLock variables are only used if the shared-cache
19392** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19393** used to store the set of table-locks required by the statement being
19394** compiled. Function sqlite3TableLock() is used to add entries to the
19395** list.
19396*/
19397struct Parse {
19398 sqlite3 *db; /* The main database structure */
19399 char *zErrMsg; /* An error message */
19400 Vdbe *pVdbe; /* An engine for executing database bytecode */
19401 int rc; /* Return code from execution */
19402 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
19403 u8 checkSchema; /* Causes schema cookie check after an error */
19404 u8 nested; /* Number of nested calls to the parser/code generator */
19405 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
19406 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
19407 u8 mayAbort; /* True if statement may throw an ABORT exception */
19408 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
19409 u8 okConstFactor; /* OK to factor out constants */
19410 u8 disableLookaside; /* Number of times lookaside has been disabled */
19411 u8 prepFlags; /* SQLITE_PREPARE_* flags */
19412 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
19413#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
19414 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
19415#endif
19416#ifdef SQLITE_DEBUG
19417 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
19418#endif
19419 int nRangeReg; /* Size of the temporary register block */
19420 int iRangeReg; /* First register in temporary register block */
19421 int nErr; /* Number of errors seen */
19422 int nTab; /* Number of previously allocated VDBE cursors */
19423 int nMem; /* Number of memory cells used so far */
19424 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
19425 int iSelfTab; /* Table associated with an index on expr, or negative
19426 ** of the base register during check-constraint eval */
19427 int nLabel; /* The *negative* of the number of labels used */
19428 int nLabelAlloc; /* Number of slots in aLabel */
19429 int *aLabel; /* Space to hold the labels */
19430 ExprList *pConstExpr;/* Constant expressions */
19431 IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
19432 Token constraintName;/* Name of the constraint currently being parsed */
19433 yDbMask writeMask; /* Start a write transaction on these databases */
19434 yDbMask cookieMask; /* Bitmask of schema verified databases */
19435 int regRowid; /* Register holding rowid of CREATE TABLE entry */
19436 int regRoot; /* Register holding root page number for new objects */
19437 int nMaxArg; /* Max args passed to user function by sub-program */
19438 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
19439#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19440 u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
19441#endif
19442#ifndef SQLITE_OMIT_SHARED_CACHE
19443 int nTableLock; /* Number of locks in aTableLock */
19444 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19445#endif
19446 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
19447 Parse *pToplevel; /* Parse structure for main program (or NULL) */
19448 Table *pTriggerTab; /* Table triggers are being coded for */
19449 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
19450 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
19451 union {
19452 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
19453 Returning *pReturning; /* The RETURNING clause */
19454 } u1;
19455 u32 oldmask; /* Mask of old.* columns referenced */
19456 u32 newmask; /* Mask of new.* columns referenced */
19457 LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
19458 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
19459 u8 bReturning; /* Coding a RETURNING trigger */
19460 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
19461 u8 disableTriggers; /* True to disable triggers */
19462
19463 /**************************************************************************
19464 ** Fields above must be initialized to zero. The fields that follow,
19465 ** down to the beginning of the recursive section, do not need to be
19466 ** initialized as they will be set before being used. The boundary is
19467 ** determined by offsetof(Parse,aTempReg).
19468 **************************************************************************/
19469
19470 int aTempReg[8]; /* Holding area for temporary registers */
19471 Parse *pOuterParse; /* Outer Parse object when nested */
19472 Token sNameToken; /* Token with unqualified schema object name */
19473
19474 /************************************************************************
19475 ** Above is constant between recursions. Below is reset before and after
19476 ** each recursion. The boundary between these two regions is determined
19477 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
19478 ** first field in the recursive region.
19479 ************************************************************************/
19480
19481 Token sLastToken; /* The last token parsed */
19482 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
19483 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
19484 u8 explain; /* True if the EXPLAIN flag is found on the query */
19485 u8 eParseMode; /* PARSE_MODE_XXX constant */
19486#ifndef SQLITE_OMIT_VIRTUALTABLE
19487 int nVtabLock; /* Number of virtual tables to lock */
19488#endif
19489 int nHeight; /* Expression tree height of current sub-select */
19490#ifndef SQLITE_OMIT_EXPLAIN
19491 int addrExplain; /* Address of current OP_Explain opcode */
19492#endif
19493 VList *pVList; /* Mapping between variable names and numbers */
19494 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
19495 const char *zTail; /* All SQL text past the last semicolon parsed */
19496 Table *pNewTable; /* A table being constructed by CREATE TABLE */
19497 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
19498 ** Also used to hold redundant UNIQUE constraints
19499 ** during a RENAME COLUMN */
19500 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
19501 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19502#ifndef SQLITE_OMIT_VIRTUALTABLE
19503 Token sArg; /* Complete text of a module argument */
19504 Table **apVtabLock; /* Pointer to virtual tables needing locking */
19505#endif
19506 With *pWith; /* Current WITH clause, or NULL */
19507#ifndef SQLITE_OMIT_ALTERTABLE
19508 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
19509#endif
19510};
19511
19512/* Allowed values for Parse.eParseMode
19513*/
19514#define PARSE_MODE_NORMAL 0
19515#define PARSE_MODE_DECLARE_VTAB 1
19516#define PARSE_MODE_RENAME 2
19517#define PARSE_MODE_UNMAP 3
19518
19519/*
19520** Sizes and pointers of various parts of the Parse object.
19521*/
19522#define PARSE_HDR(X) (((char*)(X))+offsetof(Parse,zErrMsg))
19523#define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColCache*/
19524#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
19525#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19526#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
19527
19528/*
19529** Return true if currently inside an sqlite3_declare_vtab() call.
19530*/
19531#ifdef SQLITE_OMIT_VIRTUALTABLE
19532 #define IN_DECLARE_VTAB 0
19533#else
19534 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19535#endif
19536
19537#if defined(SQLITE_OMIT_ALTERTABLE)
19538 #define IN_RENAME_OBJECT 0
19539#else
19540 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19541#endif
19542
19543#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
19544 #define IN_SPECIAL_PARSE 0
19545#else
19546 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19547#endif
19548
19549/*
19550** An instance of the following structure can be declared on a stack and used
19551** to save the Parse.zAuthContext value so that it can be restored later.
19552*/
19553struct AuthContext {
19554 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
19555 Parse *pParse; /* The Parse structure */
19556};
19557
19558/*
19559** Bitfield flags for P5 value in various opcodes.
19560**
19561** Value constraints (enforced via assert()):
19562** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
19563** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
19564** OPFLAG_BULKCSR == BTREE_BULKLOAD
19565** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
19566** OPFLAG_FORDELETE == BTREE_FORDELETE
19567** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
19568** OPFLAG_AUXDELETE == BTREE_AUXDELETE
19569*/
19570#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19571 /* Also used in P2 (not P5) of OP_Delete */
19572#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
19573#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
19574#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19575#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
19576#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
19577#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
19578#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19579#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
19580#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
19581#define OPFLAG_BYTELENARG 0xc0 /* OP_Column only for octet_length() */
19582#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
19583#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
19584#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
19585#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
19586#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
19587#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
19588#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
19589#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
19590#define OPFLAG_PREFORMAT 0x80 /* OP_Insert uses preformatted cell */
19591
19592/*
19593** Each trigger present in the database schema is stored as an instance of
19594** struct Trigger.
19595**
19596** Pointers to instances of struct Trigger are stored in two ways.
19597** 1. In the "trigHash" hash table (part of the sqlite3* that represents the
19598** database). This allows Trigger structures to be retrieved by name.
19599** 2. All triggers associated with a single table form a linked list, using the
19600** pNext member of struct Trigger. A pointer to the first element of the
19601** linked list is stored as the "pTrigger" member of the associated
19602** struct Table.
19603**
19604** The "step_list" member points to the first element of a linked list
19605** containing the SQL statements specified as the trigger program.
19606*/
19607struct Trigger {
19608 char *zName; /* The name of the trigger */
19609 char *table; /* The table or view to which the trigger applies */
19610 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
19611 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
19612 u8 bReturning; /* This trigger implements a RETURNING clause */
19613 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
19614 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19615 the <column-list> is stored here */
19616 Schema *pSchema; /* Schema containing the trigger */
19617 Schema *pTabSchema; /* Schema containing the table */
19618 TriggerStep *step_list; /* Link list of trigger program steps */
19619 Trigger *pNext; /* Next trigger associated with the table */
19620};
19621
19622/*
19623** A trigger is either a BEFORE or an AFTER trigger. The following constants
19624** determine which.
19625**
19626** If there are multiple triggers, you might of some BEFORE and some AFTER.
19627** In that cases, the constants below can be ORed together.
19628*/
19629#define TRIGGER_BEFORE 1
19630#define TRIGGER_AFTER 2
19631
19632/*
19633** An instance of struct TriggerStep is used to store a single SQL statement
19634** that is a part of a trigger-program.
19635**
19636** Instances of struct TriggerStep are stored in a singly linked list (linked
19637** using the "pNext" member) referenced by the "step_list" member of the
19638** associated struct Trigger instance. The first element of the linked list is
19639** the first step of the trigger-program.
19640**
19641** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
19642** "SELECT" statement. The meanings of the other members is determined by the
19643** value of "op" as follows:
19644**
19645** (op == TK_INSERT)
19646** orconf -> stores the ON CONFLICT algorithm
19647** pSelect -> The content to be inserted - either a SELECT statement or
19648** a VALUES clause.
19649** zTarget -> Dequoted name of the table to insert into.
19650** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19651** statement, then this stores the column-names to be
19652** inserted into.
19653** pUpsert -> The ON CONFLICT clauses for an Upsert
19654**
19655** (op == TK_DELETE)
19656** zTarget -> Dequoted name of the table to delete from.
19657** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19658** Otherwise NULL.
19659**
19660** (op == TK_UPDATE)
19661** zTarget -> Dequoted name of the table to update.
19662** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19663** Otherwise NULL.
19664** pExprList -> A list of the columns to update and the expressions to update
19665** them to. See sqlite3Update() documentation of "pChanges"
19666** argument.
19667**
19668** (op == TK_SELECT)
19669** pSelect -> The SELECT statement
19670**
19671** (op == TK_RETURNING)
19672** pExprList -> The list of expressions that follow the RETURNING keyword.
19673**
19674*/
19675struct TriggerStep {
19676 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT,
19677 ** or TK_RETURNING */
19678 u8 orconf; /* OE_Rollback etc. */
19679 Trigger *pTrig; /* The trigger that this step is a part of */
19680 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
19681 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
19682 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
19683 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
19684 ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */
19685 IdList *pIdList; /* Column names for INSERT */
19686 Upsert *pUpsert; /* Upsert clauses on an INSERT */
19687 char *zSpan; /* Original SQL text of this command */
19688 TriggerStep *pNext; /* Next in the link-list */
19689 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19690};
19691
19692/*
19693** Information about a RETURNING clause
19694*/
19695struct Returning {
19696 Parse *pParse; /* The parse that includes the RETURNING clause */
19697 ExprList *pReturnEL; /* List of expressions to return */
19698 Trigger retTrig; /* The transient trigger that implements RETURNING */
19699 TriggerStep retTStep; /* The trigger step */
19700 int iRetCur; /* Transient table holding RETURNING results */
19701 int nRetCol; /* Number of in pReturnEL after expansion */
19702 int iRetReg; /* Register array for holding a row of RETURNING */
19703};
19704
19705/*
19706** An objected used to accumulate the text of a string where we
19707** do not necessarily know how big the string will be in the end.
19708*/
19709struct sqlite3_str {
19710 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
19711 char *zText; /* The string collected so far */
19712 u32 nAlloc; /* Amount of space allocated in zText */
19713 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
19714 u32 nChar; /* Length of the string so far */
19715 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
19716 u8 printfFlags; /* SQLITE_PRINTF flags below */
19717};
19718#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19719#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
19720#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
19721
19722#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19723
19724/*
19725** The following object is the header for an "RCStr" or "reference-counted
19726** string". An RCStr is passed around and used like any other char*
19727** that has been dynamically allocated. The important interface
19728** differences:
19729**
19730** 1. RCStr strings are reference counted. They are deallocated
19731** when the reference count reaches zero.
19732**
19733** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than
19734** sqlite3_free()
19735**
19736** 3. Make a (read-only) copy of a read-only RCStr string using
19737** sqlite3RCStrRef().
19738*/
19739struct RCStr {
19740 u64 nRCRef; /* Number of references */
19741 /* Total structure size should be a multiple of 8 bytes for alignment */
19742};
19743
19744/*
19745** A pointer to this structure is used to communicate information
19746** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
19747*/
19748typedef struct {
19749 sqlite3 *db; /* The database being initialized */
19750 char **pzErrMsg; /* Error message stored here */
19751 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
19752 int rc; /* Result code stored here */
19753 u32 mInitFlags; /* Flags controlling error messages */
19754 u32 nInitRow; /* Number of rows processed */
19755 Pgno mxPage; /* Maximum page number. 0 for no limit. */
19756} InitData;
19757
19758/*
19759** Allowed values for mInitFlags
19760*/
19761#define INITFLAG_AlterMask 0x0003 /* Types of ALTER */
19762#define INITFLAG_AlterRename 0x0001 /* Reparse after a RENAME */
19763#define INITFLAG_AlterDrop 0x0002 /* Reparse after a DROP COLUMN */
19764#define INITFLAG_AlterAdd 0x0003 /* Reparse after an ADD COLUMN */
19765
19766/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
19767** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
19768** parameters are for temporary use during development, to help find
19769** optimal values for parameters in the query planner. The should not
19770** be used on trunk check-ins. They are a temporary mechanism available
19771** for transient development builds only.
19772**
19773** Tuning parameters are numbered starting with 1.
19774*/
19775#define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
19776#ifdef SQLITE_DEBUG
19777# define Tuning(X) (sqlite3Config.aTune[(X)-1])
19778#else
19779# define Tuning(X) 0
19780#endif
19781
19782/*
19783** Structure containing global configuration data for the SQLite library.
19784**
19785** This structure also contains some state information.
19786*/
19787struct Sqlite3Config {
19788 int bMemstat; /* True to enable memory status */
19789 u8 bCoreMutex; /* True to enable core mutexing */
19790 u8 bFullMutex; /* True to enable full mutexing */
19791 u8 bOpenUri; /* True to interpret filenames as URIs */
19792 u8 bUseCis; /* Use covering indices for full-scans */
19793 u8 bSmallMalloc; /* Avoid large memory allocations if true */
19794 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
19795 u8 bUseLongDouble; /* Make use of long double */
19796 int mxStrlen; /* Maximum string length */
19797 int neverCorrupt; /* Database is always well-formed */
19798 int szLookaside; /* Default lookaside buffer size */
19799 int nLookaside; /* Default lookaside buffer count */
19800 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19801 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19802 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19803 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19804 void *pHeap; /* Heap storage space */
19805 int nHeap; /* Size of pHeap[] */
19806 int mnReq, mxReq; /* Min and max heap requests sizes */
19807 sqlite3_int64 szMmap; /* mmap() space per open file */
19808 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
19809 void *pPage; /* Page cache memory */
19810 int szPage; /* Size of each page in pPage[] */
19811 int nPage; /* Number of pages in pPage[] */
19812 int mxParserStack; /* maximum depth of the parser stack */
19813 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19814 u32 szPma; /* Maximum Sorter PMA size */
19815 /* The above might be initialized to non-zero. The following need to always
19816 ** initially be zero, however. */
19817 int isInit; /* True after initialization has finished */
19818 int inProgress; /* True while initialization in progress */
19819 int isMutexInit; /* True after mutexes are initialized */
19820 int isMallocInit; /* True after malloc is initialized */
19821 int isPCacheInit; /* True after malloc is initialized */
19822 int nRefInitMutex; /* Number of users of pInitMutex */
19823 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
19824 void (*xLog)(void*,int,const char*); /* Function for logging */
19825 void *pLogArg; /* First argument to xLog() */
19826#ifdef SQLITE_ENABLE_SQLLOG
19827 void(*xSqllog)(void*,sqlite3*,const char*, int);
19828 void *pSqllogArg;
19829#endif
19830#ifdef SQLITE_VDBE_COVERAGE
19831 /* The following callback (if not NULL) is invoked on every VDBE branch
19832 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
19833 */
19834 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
19835 void *pVdbeBranchArg; /* 1st argument */
19836#endif
19837#ifndef SQLITE_OMIT_DESERIALIZE
19838 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
19839#endif
19840#ifndef SQLITE_UNTESTABLE
19841 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
19842#endif
19843 int bLocaltimeFault; /* True to fail localtime() calls */
19844 int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
19845 int iOnceResetThreshold; /* When to reset OP_Once counters */
19846 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19847 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
19848 /* vvvv--- must be last ---vvv */
19849#ifdef SQLITE_DEBUG
19850 sqlite3_int64 aTune[SQLITE_NTUNE]; /* Tuning parameters */
19851#endif
19852};
19853
19854/*
19855** This macro is used inside of assert() statements to indicate that
19856** the assert is only valid on a well-formed database. Instead of:
19857**
19858** assert( X );
19859**
19860** One writes:
19861**
19862** assert( X || CORRUPT_DB );
19863**
19864** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
19865** that the database is definitely corrupt, only that it might be corrupt.
19866** For most test cases, CORRUPT_DB is set to false using a special
19867** sqlite3_test_control(). This enables assert() statements to prove
19868** things that are always true for well-formed databases.
19869*/
19870#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
19871
19872/*
19873** Context pointer passed down through the tree-walk.
19874*/
19875struct Walker {
19876 Parse *pParse; /* Parser context. */
19877 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
19878 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
19879 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
19880 int walkerDepth; /* Number of subqueries */
19881 u16 eCode; /* A small processing code */
19882 u16 mWFlags; /* Use-dependent flags */
19883 union { /* Extra data for callback */
19884 NameContext *pNC; /* Naming context */
19885 int n; /* A counter */
19886 int iCur; /* A cursor number */
19887 SrcList *pSrcList; /* FROM clause */
19888 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
19889 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
19890 int *aiCol; /* array of column indexes */
19891 struct IdxCover *pIdxCover; /* Check for index coverage */
19892 ExprList *pGroupBy; /* GROUP BY clause */
19893 Select *pSelect; /* HAVING to WHERE clause ctx */
19894 struct WindowRewrite *pRewrite; /* Window rewrite context */
19895 struct WhereConst *pConst; /* WHERE clause constants */
19896 struct RenameCtx *pRename; /* RENAME COLUMN context */
19897 struct Table *pTab; /* Table of generated column */
19898 struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
19899 SrcItem *pSrcItem; /* A single FROM clause item */
19900 DbFixer *pFix; /* See sqlite3FixSelect() */
19901 Mem *aMem; /* See sqlite3BtreeCursorHint() */
19902 } u;
19903};
19904
19905/*
19906** The following structure contains information used by the sqliteFix...
19907** routines as they walk the parse tree to make database references
19908** explicit.
19909*/
19910struct DbFixer {
19911 Parse *pParse; /* The parsing context. Error messages written here */
19912 Walker w; /* Walker object */
19913 Schema *pSchema; /* Fix items to this schema */
19914 u8 bTemp; /* True for TEMP schema entries */
19915 const char *zDb; /* Make sure all objects are contained in this database */
19916 const char *zType; /* Type of the container - used for error messages */
19917 const Token *pName; /* Name of the container - used for error messages */
19918};
19919
19920/* Forward declarations */
19921SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
19922SQLITE_PRIVATE int sqlite3WalkExprNN(Walker*, Expr*);
19923SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
19924SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
19925SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
19926SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
19927SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
19928SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
19929SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
19930SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*);
19931SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*);
19932SQLITE_PRIVATE void sqlite3WalkWinDefnDummyCallback(Walker*,Select*);
19933
19934#ifdef SQLITE_DEBUG
19935SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
19936#endif
19937
19938#ifndef SQLITE_OMIT_CTE
19939SQLITE_PRIVATE void sqlite3SelectPopWith(Walker*, Select*);
19940#else
19941# define sqlite3SelectPopWith 0
19942#endif
19943
19944/*
19945** Return code from the parse-tree walking primitives and their
19946** callbacks.
19947*/
19948#define WRC_Continue 0 /* Continue down into children */
19949#define WRC_Prune 1 /* Omit children but continue walking siblings */
19950#define WRC_Abort 2 /* Abandon the tree walk */
19951
19952/*
19953** A single common table expression
19954*/
19955struct Cte {
19956 char *zName; /* Name of this CTE */
19957 ExprList *pCols; /* List of explicit column names, or NULL */
19958 Select *pSelect; /* The definition of this CTE */
19959 const char *zCteErr; /* Error message for circular references */
19960 CteUse *pUse; /* Usage information for this CTE */
19961 u8 eM10d; /* The MATERIALIZED flag */
19962};
19963
19964/*
19965** Allowed values for the materialized flag (eM10d):
19966*/
19967#define M10d_Yes 0 /* AS MATERIALIZED */
19968#define M10d_Any 1 /* Not specified. Query planner's choice */
19969#define M10d_No 2 /* AS NOT MATERIALIZED */
19970
19971/*
19972** An instance of the With object represents a WITH clause containing
19973** one or more CTEs (common table expressions).
19974*/
19975struct With {
19976 int nCte; /* Number of CTEs in the WITH clause */
19977 int bView; /* Belongs to the outermost Select of a view */
19978 With *pOuter; /* Containing WITH clause, or NULL */
19979 Cte a[1]; /* For each CTE in the WITH clause.... */
19980};
19981
19982/*
19983** The Cte object is not guaranteed to persist for the entire duration
19984** of code generation. (The query flattener or other parser tree
19985** edits might delete it.) The following object records information
19986** about each Common Table Expression that must be preserved for the
19987** duration of the parse.
19988**
19989** The CteUse objects are freed using sqlite3ParserAddCleanup() rather
19990** than sqlite3SelectDelete(), which is what enables them to persist
19991** until the end of code generation.
19992*/
19993struct CteUse {
19994 int nUse; /* Number of users of this CTE */
19995 int addrM9e; /* Start of subroutine to compute materialization */
19996 int regRtn; /* Return address register for addrM9e subroutine */
19997 int iCur; /* Ephemeral table holding the materialization */
19998 LogEst nRowEst; /* Estimated number of rows in the table */
19999 u8 eM10d; /* The MATERIALIZED flag */
20000};
20001
20002
20003#ifdef SQLITE_DEBUG
20004/*
20005** An instance of the TreeView object is used for printing the content of
20006** data structures on sqlite3DebugPrintf() using a tree-like view.
20007*/
20008struct TreeView {
20009 int iLevel; /* Which level of the tree we are on */
20010 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
20011};
20012#endif /* SQLITE_DEBUG */
20013
20014/*
20015** This object is used in various ways, most (but not all) related to window
20016** functions.
20017**
20018** (1) A single instance of this structure is attached to the
20019** the Expr.y.pWin field for each window function in an expression tree.
20020** This object holds the information contained in the OVER clause,
20021** plus additional fields used during code generation.
20022**
20023** (2) All window functions in a single SELECT form a linked-list
20024** attached to Select.pWin. The Window.pFunc and Window.pExpr
20025** fields point back to the expression that is the window function.
20026**
20027** (3) The terms of the WINDOW clause of a SELECT are instances of this
20028** object on a linked list attached to Select.pWinDefn.
20029**
20030** (4) For an aggregate function with a FILTER clause, an instance
20031** of this object is stored in Expr.y.pWin with eFrmType set to
20032** TK_FILTER. In this case the only field used is Window.pFilter.
20033**
20034** The uses (1) and (2) are really the same Window object that just happens
20035** to be accessible in two different ways. Use case (3) are separate objects.
20036*/
20037struct Window {
20038 char *zName; /* Name of window (may be NULL) */
20039 char *zBase; /* Name of base window for chaining (may be NULL) */
20040 ExprList *pPartition; /* PARTITION BY clause */
20041 ExprList *pOrderBy; /* ORDER BY clause */
20042 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
20043 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
20044 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
20045 u8 bImplicitFrame; /* True if frame was implicitly specified */
20046 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
20047 Expr *pStart; /* Expression for "<expr> PRECEDING" */
20048 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
20049 Window **ppThis; /* Pointer to this object in Select.pWin list */
20050 Window *pNextWin; /* Next window function belonging to this SELECT */
20051 Expr *pFilter; /* The FILTER expression */
20052 FuncDef *pWFunc; /* The function */
20053 int iEphCsr; /* Partition buffer or Peer buffer */
20054 int regAccum; /* Accumulator */
20055 int regResult; /* Interim result */
20056 int csrApp; /* Function cursor (used by min/max) */
20057 int regApp; /* Function register (also used by min/max) */
20058 int regPart; /* Array of registers for PARTITION BY values */
20059 Expr *pOwner; /* Expression object this window is attached to */
20060 int nBufferCol; /* Number of columns in buffer table */
20061 int iArgCol; /* Offset of first argument for this function */
20062 int regOne; /* Register containing constant value 1 */
20063 int regStartRowid;
20064 int regEndRowid;
20065 u8 bExprArgs; /* Defer evaluation of window function arguments
20066 ** due to the SQLITE_SUBTYPE flag */
20067};
20068
20069#ifndef SQLITE_OMIT_WINDOWFUNC
20070SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
20071SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
20072SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
20073SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
20074SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
20075SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
20076SQLITE_PRIVATE int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
20077SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
20078SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
20079SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
20080SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
20081SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
20082SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
20083SQLITE_PRIVATE void sqlite3WindowFunctions(void);
20084SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
20085SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
20086#else
20087# define sqlite3WindowDelete(a,b)
20088# define sqlite3WindowFunctions()
20089# define sqlite3WindowAttach(a,b,c)
20090#endif
20091
20092/*
20093** Assuming zIn points to the first byte of a UTF-8 character,
20094** advance zIn to point to the first byte of the next UTF-8 character.
20095*/
20096#define SQLITE_SKIP_UTF8(zIn) { \
20097 if( (*(zIn++))>=0xc0 ){ \
20098 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
20099 } \
20100}
20101
20102/*
20103** The SQLITE_*_BKPT macros are substitutes for the error codes with
20104** the same name but without the _BKPT suffix. These macros invoke
20105** routines that report the line-number on which the error originated
20106** using sqlite3_log(). The routines also provide a convenient place
20107** to set a debugger breakpoint.
20108*/
20109SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
20110SQLITE_PRIVATE int sqlite3CorruptError(int);
20111SQLITE_PRIVATE int sqlite3MisuseError(int);
20112SQLITE_PRIVATE int sqlite3CantopenError(int);
20113#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
20114#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
20115#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
20116#ifdef SQLITE_DEBUG
20117SQLITE_PRIVATE int sqlite3NomemError(int);
20118SQLITE_PRIVATE int sqlite3IoerrnomemError(int);
20119# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
20120# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
20121#else
20122# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
20123# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
20124#endif
20125#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
20126SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno);
20127# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
20128#else
20129# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
20130#endif
20131
20132/*
20133** FTS3 and FTS4 both require virtual table support
20134*/
20135#if defined(SQLITE_OMIT_VIRTUALTABLE)
20136# undef SQLITE_ENABLE_FTS3
20137# undef SQLITE_ENABLE_FTS4
20138#endif
20139
20140/*
20141** FTS4 is really an extension for FTS3. It is enabled using the
20142** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
20143** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
20144*/
20145#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
20146# define SQLITE_ENABLE_FTS3 1
20147#endif
20148
20149/*
20150** The ctype.h header is needed for non-ASCII systems. It is also
20151** needed by FTS3 when FTS3 is included in the amalgamation.
20152*/
20153#if !defined(SQLITE_ASCII) || \
20154 (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
20155# include <ctype.h>
20156#endif
20157
20158/*
20159** The following macros mimic the standard library functions toupper(),
20160** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
20161** sqlite versions only work for ASCII characters, regardless of locale.
20162*/
20163#ifdef SQLITE_ASCII
20164# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
20165# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
20166# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
20167# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
20168# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
20169# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
20170# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
20171# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
20172# define sqlite3JsonId1(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x42)
20173# define sqlite3JsonId2(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x46)
20174#else
20175# define sqlite3Toupper(x) toupper((unsigned char)(x))
20176# define sqlite3Isspace(x) isspace((unsigned char)(x))
20177# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
20178# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
20179# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
20180# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
20181# define sqlite3Tolower(x) tolower((unsigned char)(x))
20182# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
20183# define sqlite3JsonId1(x) (sqlite3IsIdChar(x)&&(x)<'0')
20184# define sqlite3JsonId2(x) sqlite3IsIdChar(x)
20185#endif
20186SQLITE_PRIVATE int sqlite3IsIdChar(u8);
20187
20188/*
20189** Internal function prototypes
20190*/
20191SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
20192SQLITE_PRIVATE int sqlite3Strlen30(const char*);
20193#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
20194SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
20195#define sqlite3StrNICmp sqlite3_strnicmp
20196
20197SQLITE_PRIVATE int sqlite3MallocInit(void);
20198SQLITE_PRIVATE void sqlite3MallocEnd(void);
20199SQLITE_PRIVATE void *sqlite3Malloc(u64);
20200SQLITE_PRIVATE void *sqlite3MallocZero(u64);
20201SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
20202SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
20203SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
20204SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
20205SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
20206SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
20207SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
20208SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
20209SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
20210SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
20211SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
20212SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3*, void*);
20213SQLITE_PRIVATE int sqlite3MallocSize(const void*);
20214SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*);
20215SQLITE_PRIVATE void *sqlite3PageMalloc(int);
20216SQLITE_PRIVATE void sqlite3PageFree(void*);
20217SQLITE_PRIVATE void sqlite3MemSetDefault(void);
20218#ifndef SQLITE_UNTESTABLE
20219SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
20220#endif
20221SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
20222
20223/*
20224** On systems with ample stack space and that support alloca(), make
20225** use of alloca() to obtain space for large automatic objects. By default,
20226** obtain space from malloc().
20227**
20228** The alloca() routine never returns NULL. This will cause code paths
20229** that deal with sqlite3StackAlloc() failures to be unreachable.
20230*/
20231#ifdef SQLITE_USE_ALLOCA
20232# define sqlite3StackAllocRaw(D,N) alloca(N)
20233# define sqlite3StackAllocRawNN(D,N) alloca(N)
20234# define sqlite3StackFree(D,P)
20235# define sqlite3StackFreeNN(D,P)
20236#else
20237# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
20238# define sqlite3StackAllocRawNN(D,N) sqlite3DbMallocRawNN(D,N)
20239# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
20240# define sqlite3StackFreeNN(D,P) sqlite3DbFreeNN(D,P)
20241#endif
20242
20243/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
20244** are, disable MEMSYS3
20245*/
20246#ifdef SQLITE_ENABLE_MEMSYS5
20247SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
20248#undef SQLITE_ENABLE_MEMSYS3
20249#endif
20250#ifdef SQLITE_ENABLE_MEMSYS3
20251SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
20252#endif
20253
20254
20255#ifndef SQLITE_MUTEX_OMIT
20256SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
20257SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
20258SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
20259SQLITE_PRIVATE int sqlite3MutexInit(void);
20260SQLITE_PRIVATE int sqlite3MutexEnd(void);
20261#endif
20262#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
20263SQLITE_PRIVATE void sqlite3MemoryBarrier(void);
20264#else
20265# define sqlite3MemoryBarrier()
20266#endif
20267
20268SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
20269SQLITE_PRIVATE void sqlite3StatusUp(int, int);
20270SQLITE_PRIVATE void sqlite3StatusDown(int, int);
20271SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
20272SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
20273
20274/* Access to mutexes used by sqlite3_status() */
20275SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
20276SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
20277
20278#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
20279SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
20280#else
20281# define sqlite3MutexWarnOnContention(x)
20282#endif
20283
20284#ifndef SQLITE_OMIT_FLOATING_POINT
20285# define EXP754 (((u64)0x7ff)<<52)
20286# define MAN754 ((((u64)1)<<52)-1)
20287# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
20288SQLITE_PRIVATE int sqlite3IsNaN(double);
20289#else
20290# define IsNaN(X) 0
20291# define sqlite3IsNaN(X) 0
20292#endif
20293
20294/*
20295** An instance of the following structure holds information about SQL
20296** functions arguments that are the parameters to the printf() function.
20297*/
20298struct PrintfArguments {
20299 int nArg; /* Total number of arguments */
20300 int nUsed; /* Number of arguments used so far */
20301 sqlite3_value **apArg; /* The argument values */
20302};
20303
20304/*
20305** An instance of this object receives the decoding of a floating point
20306** value into an approximate decimal representation.
20307*/
20308struct FpDecode {
20309 char sign; /* '+' or '-' */
20310 char isSpecial; /* 1: Infinity 2: NaN */
20311 int n; /* Significant digits in the decode */
20312 int iDP; /* Location of the decimal point */
20313 char *z; /* Start of significant digits */
20314 char zBuf[24]; /* Storage for significant digits */
20315};
20316
20317SQLITE_PRIVATE void sqlite3FpDecode(FpDecode*,double,int,int);
20318SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
20319SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
20320#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
20321SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
20322#endif
20323#if defined(SQLITE_TEST)
20324SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
20325#endif
20326
20327#if defined(SQLITE_DEBUG)
20328SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...);
20329SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
20330SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
20331SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
20332SQLITE_PRIVATE void sqlite3TreeViewBareIdList(TreeView*, const IdList*, const char*);
20333SQLITE_PRIVATE void sqlite3TreeViewIdList(TreeView*, const IdList*, u8, const char*);
20334SQLITE_PRIVATE void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8);
20335SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
20336SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
20337SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8);
20338SQLITE_PRIVATE void sqlite3TreeViewUpsert(TreeView*, const Upsert*, u8);
20339#if TREETRACE_ENABLED
20340SQLITE_PRIVATE void sqlite3TreeViewDelete(const With*, const SrcList*, const Expr*,
20341 const ExprList*,const Expr*, const Trigger*);
20342SQLITE_PRIVATE void sqlite3TreeViewInsert(const With*, const SrcList*,
20343 const IdList*, const Select*, const ExprList*,
20344 int, const Upsert*, const Trigger*);
20345SQLITE_PRIVATE void sqlite3TreeViewUpdate(const With*, const SrcList*, const ExprList*,
20346 const Expr*, int, const ExprList*, const Expr*,
20347 const Upsert*, const Trigger*);
20348#endif
20349#ifndef SQLITE_OMIT_TRIGGER
20350SQLITE_PRIVATE void sqlite3TreeViewTriggerStep(TreeView*, const TriggerStep*, u8, u8);
20351SQLITE_PRIVATE void sqlite3TreeViewTrigger(TreeView*, const Trigger*, u8, u8);
20352#endif
20353#ifndef SQLITE_OMIT_WINDOWFUNC
20354SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
20355SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
20356#endif
20357SQLITE_PRIVATE void sqlite3ShowExpr(const Expr*);
20358SQLITE_PRIVATE void sqlite3ShowExprList(const ExprList*);
20359SQLITE_PRIVATE void sqlite3ShowIdList(const IdList*);
20360SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList*);
20361SQLITE_PRIVATE void sqlite3ShowSelect(const Select*);
20362SQLITE_PRIVATE void sqlite3ShowWith(const With*);
20363SQLITE_PRIVATE void sqlite3ShowUpsert(const Upsert*);
20364#ifndef SQLITE_OMIT_TRIGGER
20365SQLITE_PRIVATE void sqlite3ShowTriggerStep(const TriggerStep*);
20366SQLITE_PRIVATE void sqlite3ShowTriggerStepList(const TriggerStep*);
20367SQLITE_PRIVATE void sqlite3ShowTrigger(const Trigger*);
20368SQLITE_PRIVATE void sqlite3ShowTriggerList(const Trigger*);
20369#endif
20370#ifndef SQLITE_OMIT_WINDOWFUNC
20371SQLITE_PRIVATE void sqlite3ShowWindow(const Window*);
20372SQLITE_PRIVATE void sqlite3ShowWinFunc(const Window*);
20373#endif
20374#endif
20375
20376SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
20377SQLITE_PRIVATE void sqlite3ProgressCheck(Parse*);
20378SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
20379SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
20380SQLITE_PRIVATE void sqlite3Dequote(char*);
20381SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
20382SQLITE_PRIVATE void sqlite3DequoteToken(Token*);
20383SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
20384SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
20385SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*);
20386SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
20387SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
20388SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
20389SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
20390SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
20391SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
20392SQLITE_PRIVATE void sqlite3TouchRegister(Parse*,int);
20393#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
20394SQLITE_PRIVATE int sqlite3FirstAvailableRegister(Parse*,int);
20395#endif
20396#ifdef SQLITE_DEBUG
20397SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
20398#endif
20399SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
20400SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
20401SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
20402SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
20403SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
20404SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
20405SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr*);
20406SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
20407SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
20408SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
20409SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
20410SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse*, Expr*);
20411SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
20412SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
20413SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
20414SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
20415SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int);
20416SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
20417SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
20418SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
20419SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
20420SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
20421SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
20422SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
20423SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
20424SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
20425#ifndef SQLITE_OMIT_VIRTUALTABLE
20426SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
20427#endif
20428SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
20429SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
20430SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
20431SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
20432SQLITE_PRIVATE void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*);
20433SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table*,Column*);
20434SQLITE_PRIVATE void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl);
20435SQLITE_PRIVATE const char *sqlite3ColumnColl(Column*);
20436SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
20437SQLITE_PRIVATE void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect);
20438SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
20439SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
20440SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
20441SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
20442SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
20443SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
20444#ifdef SQLITE_OMIT_GENERATED_COLUMNS
20445# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20446# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20447#else
20448SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16);
20449SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table*, i16);
20450#endif
20451SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
20452#if SQLITE_ENABLE_HIDDEN_COLUMNS
20453SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*);
20454#else
20455# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20456#endif
20457SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token,Token);
20458SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
20459SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
20460SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
20461SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
20462SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
20463SQLITE_PRIVATE void sqlite3AddGenerated(Parse*,Expr*,Token*);
20464SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
20465SQLITE_PRIVATE void sqlite3AddReturning(Parse*,ExprList*);
20466SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
20467 sqlite3_vfs**,char**,char **);
20468#define sqlite3CodecQueryParameters(A,B,C) 0
20469SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
20470
20471#ifdef SQLITE_UNTESTABLE
20472# define sqlite3FaultSim(X) SQLITE_OK
20473#else
20474SQLITE_PRIVATE int sqlite3FaultSim(int);
20475#endif
20476
20477SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
20478SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
20479SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
20480SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
20481SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
20482SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
20483SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
20484#ifndef SQLITE_UNTESTABLE
20485SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
20486#endif
20487
20488SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
20489SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
20490SQLITE_PRIVATE void sqlite3RowSetClear(void*);
20491SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
20492SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
20493SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
20494
20495SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
20496
20497#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
20498SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
20499#else
20500# define sqlite3ViewGetColumnNames(A,B) 0
20501#endif
20502
20503#if SQLITE_MAX_ATTACHED>30
20504SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
20505#endif
20506SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
20507SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
20508SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
20509SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
20510#ifndef SQLITE_OMIT_AUTOINCREMENT
20511SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
20512SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
20513#else
20514# define sqlite3AutoincrementBegin(X)
20515# define sqlite3AutoincrementEnd(X)
20516#endif
20517SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
20518#ifndef SQLITE_OMIT_GENERATED_COLUMNS
20519SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
20520#endif
20521SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
20522SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
20523SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
20524SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
20525SQLITE_PRIVATE SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
20526SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
20527SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
20528 Token*, Select*, OnOrUsing*);
20529SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
20530SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
20531SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, SrcItem *);
20532SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
20533SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
20534SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
20535SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
20536SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
20537SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
20538SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
20539 Expr*, int, int, u8);
20540SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
20541SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
20542SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
20543 Expr*,ExprList*,u32,Expr*);
20544SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
20545SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
20546SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
20547SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
20548#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
20549SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
20550#endif
20551SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*);
20552SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
20553SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
20554 Upsert*);
20555SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
20556 ExprList*,Select*,u16,int);
20557SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
20558SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
20559SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
20560SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
20561SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
20562SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
20563SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
20564SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
20565SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
20566SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
20567#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
20568#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
20569#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
20570SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo*);
20571SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
20572SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
20573SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
20574SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
20575SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
20576#ifndef SQLITE_OMIT_GENERATED_COLUMNS
20577SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
20578#endif
20579SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
20580SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
20581SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
20582SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
20583SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
20584SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
20585#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
20586#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
20587#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
20588#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
20589SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
20590SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
20591SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
20592SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
20593#define LOCATE_VIEW 0x01
20594#define LOCATE_NOERR 0x02
20595SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
20596SQLITE_PRIVATE const char *sqlite3PreferredTableName(const char*);
20597SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *);
20598SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
20599SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
20600SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
20601SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*);
20602SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
20603SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, const Token*);
20604SQLITE_PRIVATE int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
20605SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*,Expr*,int);
20606SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
20607SQLITE_PRIVATE int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
20608SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int,int);
20609SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
20610SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
20611SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
20612SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
20613SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
20614SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
20615#ifndef SQLITE_UNTESTABLE
20616SQLITE_PRIVATE void sqlite3PrngSaveState(void);
20617SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
20618#endif
20619SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
20620SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
20621SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
20622SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
20623SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
20624SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
20625SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
20626SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
20627SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char*);
20628SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
20629SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
20630SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
20631SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
20632SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
20633SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
20634SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
20635SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int);
20636#ifdef SQLITE_ENABLE_CURSOR_HINTS
20637SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
20638#endif
20639SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*);
20640SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
20641SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
20642SQLITE_PRIVATE int sqlite3IsRowid(const char*);
20643SQLITE_PRIVATE void sqlite3GenerateRowDelete(
20644 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
20645SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
20646SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
20647SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
20648SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
20649SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
20650 u8,u8,int,int*,int*,Upsert*);
20651#ifdef SQLITE_ENABLE_NULL_TRIM
20652SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
20653#else
20654# define sqlite3SetMakeRecordP5(A,B)
20655#endif
20656SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
20657SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
20658SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
20659SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
20660SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
20661SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
20662SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
20663SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
20664SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
20665SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
20666SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
20667SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
20668SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
20669SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
20670SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
20671SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
20672SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
20673SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
20674SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
20675SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void);
20676SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
20677#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
20678SQLITE_PRIVATE int sqlite3JsonTableFunctions(sqlite3*);
20679#endif
20680SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
20681SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
20682SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
20683SQLITE_PRIVATE With *sqlite3WithDup(sqlite3 *db, With *p);
20684
20685#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
20686SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
20687#endif
20688
20689#ifndef SQLITE_OMIT_TRIGGER
20690SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
20691 Expr*,int, int);
20692SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
20693SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
20694SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
20695SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
20696SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
20697SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
20698 int, int, int);
20699SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
20700 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
20701SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
20702SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
20703 const char*,const char*);
20704SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
20705 Select*,u8,Upsert*,
20706 const char*,const char*);
20707SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
20708 Expr*, u8, const char*,const char*);
20709SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
20710 const char*,const char*);
20711SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
20712SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
20713SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
20714SQLITE_PRIVATE SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
20715# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20716# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
20717#else
20718# define sqlite3TriggersExist(B,C,D,E,F) 0
20719# define sqlite3DeleteTrigger(A,B)
20720# define sqlite3DropTriggerPtr(A,B)
20721# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
20722# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
20723# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
20724# define sqlite3TriggerList(X, Y) 0
20725# define sqlite3ParseToplevel(p) p
20726# define sqlite3IsToplevel(p) 1
20727# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
20728# define sqlite3TriggerStepSrc(A,B) 0
20729#endif
20730
20731SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
20732SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol);
20733SQLITE_PRIVATE void sqlite3SrcItemColumnUsed(SrcItem*,int);
20734SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr*,int,u32);
20735SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
20736SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
20737#ifndef SQLITE_OMIT_AUTHORIZATION
20738SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
20739SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
20740SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
20741SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
20742SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
20743#else
20744# define sqlite3AuthRead(a,b,c,d)
20745# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
20746# define sqlite3AuthContextPush(a,b,c)
20747# define sqlite3AuthContextPop(a) ((void)(a))
20748#endif
20749SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
20750SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
20751SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
20752SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
20753SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
20754SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
20755SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
20756SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
20757
20758SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
20759SQLITE_PRIVATE i64 sqlite3RealToI64(double);
20760SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*);
20761SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
20762SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
20763SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
20764SQLITE_PRIVATE int sqlite3Atoi(const char*);
20765#ifndef SQLITE_OMIT_UTF16
20766SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
20767#endif
20768SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
20769SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
20770SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
20771SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
20772SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
20773SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
20774SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
20775SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
20776SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
20777
20778/*
20779** Routines to read and write variable-length integers. These used to
20780** be defined locally, but now we use the varint routines in the util.c
20781** file.
20782*/
20783SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
20784SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
20785SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
20786SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
20787
20788/*
20789** The common case is for a varint to be a single byte. They following
20790** macros handle the common case without a procedure call, but then call
20791** the procedure for larger varints.
20792*/
20793#define getVarint32(A,B) \
20794 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
20795#define getVarint32NR(A,B) \
20796 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
20797#define putVarint32(A,B) \
20798 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
20799 sqlite3PutVarint((A),(B)))
20800#define getVarint sqlite3GetVarint
20801#define putVarint sqlite3PutVarint
20802
20803
20804SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
20805SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3*,const Table*);
20806SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
20807SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
20808SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
20809SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int);
20810SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
20811SQLITE_PRIVATE int sqlite3ExprDataType(const Expr *pExpr);
20812SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
20813SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
20814SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
20815SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
20816SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3*);
20817SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
20818SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
20819SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
20820SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
20821
20822#if defined(SQLITE_NEED_ERR_NAME)
20823SQLITE_PRIVATE const char *sqlite3ErrName(int);
20824#endif
20825
20826#ifndef SQLITE_OMIT_DESERIALIZE
20827SQLITE_PRIVATE int sqlite3MemdbInit(void);
20828SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs*);
20829#else
20830# define sqlite3IsMemdb(X) 0
20831#endif
20832
20833SQLITE_PRIVATE const char *sqlite3ErrStr(int);
20834SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
20835SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
20836SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
20837SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
20838SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8);
20839SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
20840SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
20841SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
20842SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
20843SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*);
20844SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
20845SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
20846SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
20847SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
20848SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
20849SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, i64);
20850SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
20851SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
20852SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
20853SQLITE_PRIVATE int sqlite3AbsInt32(int);
20854#ifdef SQLITE_ENABLE_8_3_NAMES
20855SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
20856#else
20857# define sqlite3FileSuffix3(X,Y)
20858#endif
20859SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
20860
20861SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
20862SQLITE_PRIVATE int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*));
20863SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
20864SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
20865 void(*)(void*));
20866SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
20867SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
20868#ifndef SQLITE_UNTESTABLE
20869SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context*);
20870#endif
20871SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
20872#ifndef SQLITE_OMIT_UTF16
20873SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
20874#endif
20875SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
20876SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
20877#ifndef SQLITE_AMALGAMATION
20878SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
20879SQLITE_PRIVATE const char sqlite3StrBINARY[];
20880SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[];
20881SQLITE_PRIVATE const char sqlite3StdTypeAffinity[];
20882SQLITE_PRIVATE const char *sqlite3StdType[];
20883SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
20884SQLITE_PRIVATE const unsigned char *sqlite3aLTb;
20885SQLITE_PRIVATE const unsigned char *sqlite3aEQb;
20886SQLITE_PRIVATE const unsigned char *sqlite3aGTb;
20887SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
20888SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
20889SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
20890#ifndef SQLITE_OMIT_WSD
20891SQLITE_PRIVATE int sqlite3PendingByte;
20892#endif
20893#endif /* SQLITE_AMALGAMATION */
20894#ifdef VDBE_PROFILE
20895SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
20896#endif
20897SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
20898SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
20899SQLITE_PRIVATE void sqlite3AlterFunctions(void);
20900SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
20901SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
20902SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
20903SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
20904SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
20905SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
20906SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
20907SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
20908SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, SrcItem*);
20909SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
20910SQLITE_PRIVATE int sqlite3MatchEName(
20911 const struct ExprList_item*,
20912 const char*,
20913 const char*,
20914 const char*
20915);
20916SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
20917SQLITE_PRIVATE u8 sqlite3StrIHash(const char*);
20918SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
20919SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
20920SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
20921SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
20922SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
20923SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
20924SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
20925SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
20926SQLITE_PRIVATE void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*);
20927SQLITE_PRIVATE const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*);
20928SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom);
20929SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
20930SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
20931SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
20932SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
20933SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
20934SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
20935SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
20936SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
20937SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
20938SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
20939SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
20940SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
20941SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
20942SQLITE_PRIVATE void sqlite3SchemaClear(void *);
20943SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
20944SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
20945SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
20946SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
20947SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
20948SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
20949SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
20950SQLITE_PRIVATE const char *sqlite3SelectOpName(int);
20951SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse*, ExprList*);
20952
20953#ifdef SQLITE_DEBUG
20954SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
20955#endif
20956SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
20957 void (*)(sqlite3_context*,int,sqlite3_value **),
20958 void (*)(sqlite3_context*,int,sqlite3_value **),
20959 void (*)(sqlite3_context*),
20960 void (*)(sqlite3_context*),
20961 void (*)(sqlite3_context*,int,sqlite3_value **),
20962 FuncDestructor *pDestructor
20963);
20964SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
20965SQLITE_PRIVATE void *sqlite3OomFault(sqlite3*);
20966SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
20967SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
20968SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
20969
20970SQLITE_PRIVATE char *sqlite3RCStrRef(char*);
20971SQLITE_PRIVATE void sqlite3RCStrUnref(char*);
20972SQLITE_PRIVATE char *sqlite3RCStrNew(u64);
20973SQLITE_PRIVATE char *sqlite3RCStrResize(char*,u64);
20974
20975SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
20976SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum*, i64);
20977SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
20978SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8);
20979SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
20980SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
20981SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
20982SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
20983SQLITE_PRIVATE void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*);
20984
20985SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
20986SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
20987
20988#ifndef SQLITE_OMIT_SUBQUERY
20989SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
20990#else
20991# define sqlite3ExprCheckIN(x,y) SQLITE_OK
20992#endif
20993
20994#ifdef SQLITE_ENABLE_STAT4
20995SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
20996 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
20997SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
20998SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
20999SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
21000SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
21001#endif
21002
21003/*
21004** The interface to the LEMON-generated parser
21005*/
21006#ifndef SQLITE_AMALGAMATION
21007SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
21008SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
21009#endif
21010SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
21011SQLITE_PRIVATE int sqlite3ParserFallback(int);
21012#ifdef YYTRACKMAXSTACKDEPTH
21013SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
21014#endif
21015
21016SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
21017#ifndef SQLITE_OMIT_LOAD_EXTENSION
21018SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
21019#else
21020# define sqlite3CloseExtensions(X)
21021#endif
21022
21023#ifndef SQLITE_OMIT_SHARED_CACHE
21024SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
21025#else
21026 #define sqlite3TableLock(v,w,x,y,z)
21027#endif
21028
21029#ifdef SQLITE_TEST
21030SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
21031#endif
21032
21033#ifdef SQLITE_OMIT_VIRTUALTABLE
21034# define sqlite3VtabClear(D,T)
21035# define sqlite3VtabSync(X,Y) SQLITE_OK
21036# define sqlite3VtabRollback(X)
21037# define sqlite3VtabCommit(X)
21038# define sqlite3VtabInSync(db) 0
21039# define sqlite3VtabLock(X)
21040# define sqlite3VtabUnlock(X)
21041# define sqlite3VtabModuleUnref(D,X)
21042# define sqlite3VtabUnlockList(X)
21043# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
21044# define sqlite3GetVTable(X,Y) ((VTable*)0)
21045#else
21046SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
21047SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
21048SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
21049SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
21050SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
21051SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
21052SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
21053SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3*,Module*);
21054SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
21055SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
21056SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
21057SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
21058SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
21059 sqlite3*,
21060 const char*,
21061 const sqlite3_module*,
21062 void*,
21063 void(*)(void*)
21064 );
21065# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
21066#endif
21067SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db);
21068#ifndef SQLITE_OMIT_VIRTUALTABLE
21069SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
21070SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
21071SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*);
21072#else
21073# define sqlite3ShadowTableName(A,B) 0
21074# define sqlite3IsShadowTableOf(A,B,C) 0
21075# define sqlite3MarkAllShadowTablesOf(A,B)
21076#endif
21077SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
21078SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
21079SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
21080SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
21081SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
21082SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
21083SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
21084SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
21085SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
21086SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
21087SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
21088
21089SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
21090SQLITE_PRIVATE void sqlite3VtabUsesAllSchemas(Parse*);
21091SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
21092SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
21093SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
21094SQLITE_PRIVATE void sqlite3ParseObjectInit(Parse*,sqlite3*);
21095SQLITE_PRIVATE void sqlite3ParseObjectReset(Parse*);
21096SQLITE_PRIVATE void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
21097#ifdef SQLITE_ENABLE_NORMALIZE
21098SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
21099#endif
21100SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
21101SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
21102SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
21103SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
21104SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
21105SQLITE_PRIVATE const char *sqlite3JournalModename(int);
21106#ifndef SQLITE_OMIT_WAL
21107SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
21108SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
21109#endif
21110#ifndef SQLITE_OMIT_CTE
21111SQLITE_PRIVATE Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
21112SQLITE_PRIVATE void sqlite3CteDelete(sqlite3*,Cte*);
21113SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Cte*);
21114SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*);
21115SQLITE_PRIVATE With *sqlite3WithPush(Parse*, With*, u8);
21116#else
21117# define sqlite3CteNew(P,T,E,S) ((void*)0)
21118# define sqlite3CteDelete(D,C)
21119# define sqlite3CteWithAdd(P,W,C) ((void*)0)
21120# define sqlite3WithDelete(x,y)
21121# define sqlite3WithPush(x,y,z) ((void*)0)
21122#endif
21123#ifndef SQLITE_OMIT_UPSERT
21124SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
21125SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*);
21126SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
21127SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
21128SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
21129SQLITE_PRIVATE Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
21130SQLITE_PRIVATE int sqlite3UpsertNextIsIPK(Upsert*);
21131#else
21132#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0)
21133#define sqlite3UpsertDelete(x,y)
21134#define sqlite3UpsertDup(x,y) ((Upsert*)0)
21135#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0)
21136#define sqlite3UpsertNextIsIPK(x) 0
21137#endif
21138
21139
21140/* Declarations for functions in fkey.c. All of these are replaced by
21141** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
21142** key functionality is available. If OMIT_TRIGGER is defined but
21143** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
21144** this case foreign keys are parsed, but no other functionality is
21145** provided (enforcement of FK constraints requires the triggers sub-system).
21146*/
21147#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
21148SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
21149SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
21150SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
21151SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
21152SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
21153SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
21154SQLITE_PRIVATE void sqlite3FkClearTriggerCache(sqlite3*,int);
21155#else
21156 #define sqlite3FkActions(a,b,c,d,e,f)
21157 #define sqlite3FkCheck(a,b,c,d,e,f)
21158 #define sqlite3FkDropTable(a,b,c)
21159 #define sqlite3FkOldmask(a,b) 0
21160 #define sqlite3FkRequired(a,b,c,d) 0
21161 #define sqlite3FkReferences(a) 0
21162 #define sqlite3FkClearTriggerCache(a,b)
21163#endif
21164#ifndef SQLITE_OMIT_FOREIGN_KEY
21165SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
21166SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
21167#else
21168 #define sqlite3FkDelete(a,b)
21169 #define sqlite3FkLocateIndex(a,b,c,d,e)
21170#endif
21171
21172
21173/*
21174** Available fault injectors. Should be numbered beginning with 0.
21175*/
21176#define SQLITE_FAULTINJECTOR_MALLOC 0
21177#define SQLITE_FAULTINJECTOR_COUNT 1
21178
21179/*
21180** The interface to the code in fault.c used for identifying "benign"
21181** malloc failures. This is only present if SQLITE_UNTESTABLE
21182** is not defined.
21183*/
21184#ifndef SQLITE_UNTESTABLE
21185SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
21186SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
21187#else
21188 #define sqlite3BeginBenignMalloc()
21189 #define sqlite3EndBenignMalloc()
21190#endif
21191
21192/*
21193** Allowed return values from sqlite3FindInIndex()
21194*/
21195#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
21196#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
21197#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
21198#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
21199#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
21200/*
21201** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
21202*/
21203#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
21204#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
21205#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
21206SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
21207
21208SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
21209SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
21210#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
21211 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
21212SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
21213#endif
21214
21215SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
21216SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
21217
21218SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
21219#if SQLITE_MAX_EXPR_DEPTH>0
21220SQLITE_PRIVATE int sqlite3SelectExprHeight(const Select *);
21221SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
21222#else
21223 #define sqlite3SelectExprHeight(x) 0
21224 #define sqlite3ExprCheckHeight(x,y)
21225#endif
21226SQLITE_PRIVATE void sqlite3ExprSetErrorOffset(Expr*,int);
21227
21228SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
21229SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
21230
21231#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
21232SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
21233SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
21234SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
21235#else
21236 #define sqlite3ConnectionBlocked(x,y)
21237 #define sqlite3ConnectionUnlocked(x)
21238 #define sqlite3ConnectionClosed(x)
21239#endif
21240
21241#ifdef SQLITE_DEBUG
21242SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
21243#endif
21244#if defined(YYCOVERAGE)
21245SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*);
21246#endif
21247
21248/*
21249** If the SQLITE_ENABLE IOTRACE exists then the global variable
21250** sqlite3IoTrace is a pointer to a printf-like routine used to
21251** print I/O tracing messages.
21252*/
21253#ifdef SQLITE_ENABLE_IOTRACE
21254# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
21255SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
21256SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
21257#else
21258# define IOTRACE(A)
21259# define sqlite3VdbeIOTraceSql(X)
21260#endif
21261
21262/*
21263** These routines are available for the mem2.c debugging memory allocator
21264** only. They are used to verify that different "types" of memory
21265** allocations are properly tracked by the system.
21266**
21267** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
21268** the MEMTYPE_* macros defined below. The type must be a bitmask with
21269** a single bit set.
21270**
21271** sqlite3MemdebugHasType() returns true if any of the bits in its second
21272** argument match the type set by the previous sqlite3MemdebugSetType().
21273** sqlite3MemdebugHasType() is intended for use inside assert() statements.
21274**
21275** sqlite3MemdebugNoType() returns true if none of the bits in its second
21276** argument match the type set by the previous sqlite3MemdebugSetType().
21277**
21278** Perhaps the most important point is the difference between MEMTYPE_HEAP
21279** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
21280** it might have been allocated by lookaside, except the allocation was
21281** too large or lookaside was already full. It is important to verify
21282** that allocations that might have been satisfied by lookaside are not
21283** passed back to non-lookaside free() routines. Asserts such as the
21284** example above are placed on the non-lookaside free() routines to verify
21285** this constraint.
21286**
21287** All of this is no-op for a production build. It only comes into
21288** play when the SQLITE_MEMDEBUG compile-time option is used.
21289*/
21290#ifdef SQLITE_MEMDEBUG
21291SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8);
21292SQLITE_PRIVATE int sqlite3MemdebugHasType(const void*,u8);
21293SQLITE_PRIVATE int sqlite3MemdebugNoType(const void*,u8);
21294#else
21295# define sqlite3MemdebugSetType(X,Y) /* no-op */
21296# define sqlite3MemdebugHasType(X,Y) 1
21297# define sqlite3MemdebugNoType(X,Y) 1
21298#endif
21299#define MEMTYPE_HEAP 0x01 /* General heap allocations */
21300#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
21301#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
21302
21303/*
21304** Threading interface
21305*/
21306#if SQLITE_MAX_WORKER_THREADS>0
21307SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
21308SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
21309#endif
21310
21311#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
21312SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
21313#endif
21314#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
21315SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
21316#endif
21317
21318SQLITE_PRIVATE int sqlite3ExprVectorSize(const Expr *pExpr);
21319SQLITE_PRIVATE int sqlite3ExprIsVector(const Expr *pExpr);
21320SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
21321SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
21322SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
21323
21324#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
21325SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
21326#endif
21327
21328#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
21329SQLITE_PRIVATE int sqlite3KvvfsInit(void);
21330#endif
21331
21332#if defined(VDBE_PROFILE) \
21333 || defined(SQLITE_PERFORMANCE_TRACE) \
21334 || defined(SQLITE_ENABLE_STMT_SCANSTATUS)
21335SQLITE_PRIVATE sqlite3_uint64 sqlite3Hwtime(void);
21336#endif
21337
21338#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
21339# define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
21340#else
21341# define IS_STMT_SCANSTATUS(db) 0
21342#endif
21343
21344#endif /* SQLITEINT_H */
21345
21346/************** End of sqliteInt.h *******************************************/
21347/************** Begin file os_common.h ***************************************/
21348/*
21349** 2004 May 22
21350**
21351** The author disclaims copyright to this source code. In place of
21352** a legal notice, here is a blessing:
21353**
21354** May you do good and not evil.
21355** May you find forgiveness for yourself and forgive others.
21356** May you share freely, never taking more than you give.
21357**
21358******************************************************************************
21359**
21360** This file contains macros and a little bit of code that is common to
21361** all of the platform-specific files (os_*.c) and is #included into those
21362** files.
21363**
21364** This file should be #included by the os_*.c files only. It is not a
21365** general purpose header file.
21366*/
21367#ifndef _OS_COMMON_H_
21368#define _OS_COMMON_H_
21369
21370/*
21371** At least two bugs have slipped in because we changed the MEMORY_DEBUG
21372** macro to SQLITE_DEBUG and some older makefiles have not yet made the
21373** switch. The following code should catch this problem at compile-time.
21374*/
21375#ifdef MEMORY_DEBUG
21376# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
21377#endif
21378
21379/*
21380** Macros for performance tracing. Normally turned off. Only works
21381** on i486 hardware.
21382*/
21383#ifdef SQLITE_PERFORMANCE_TRACE
21384
21385static sqlite_uint64 g_start;
21386static sqlite_uint64 g_elapsed;
21387#define TIMER_START g_start=sqlite3Hwtime()
21388#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21389#define TIMER_ELAPSED g_elapsed
21390#else
21391#define TIMER_START
21392#define TIMER_END
21393#define TIMER_ELAPSED ((sqlite_uint64)0)
21394#endif
21395
21396/*
21397** If we compile with the SQLITE_TEST macro set, then the following block
21398** of code will give us the ability to simulate a disk I/O error. This
21399** is used for testing the I/O recovery logic.
21400*/
21401#if defined(SQLITE_TEST)
21402SQLITE_API extern int sqlite3_io_error_hit;
21403SQLITE_API extern int sqlite3_io_error_hardhit;
21404SQLITE_API extern int sqlite3_io_error_pending;
21405SQLITE_API extern int sqlite3_io_error_persist;
21406SQLITE_API extern int sqlite3_io_error_benign;
21407SQLITE_API extern int sqlite3_diskfull_pending;
21408SQLITE_API extern int sqlite3_diskfull;
21409#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
21410#define SimulateIOError(CODE) \
21411 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
21412 || sqlite3_io_error_pending-- == 1 ) \
21413 { local_ioerr(); CODE; }
21414static void local_ioerr(){
21415 IOTRACE(("IOERR\n"));
21416 sqlite3_io_error_hit++;
21417 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
21418}
21419#define SimulateDiskfullError(CODE) \
21420 if( sqlite3_diskfull_pending ){ \
21421 if( sqlite3_diskfull_pending == 1 ){ \
21422 local_ioerr(); \
21423 sqlite3_diskfull = 1; \
21424 sqlite3_io_error_hit = 1; \
21425 CODE; \
21426 }else{ \
21427 sqlite3_diskfull_pending--; \
21428 } \
21429 }
21430#else
21431#define SimulateIOErrorBenign(X)
21432#define SimulateIOError(A)
21433#define SimulateDiskfullError(A)
21434#endif /* defined(SQLITE_TEST) */
21435
21436/*
21437** When testing, keep a count of the number of open files.
21438*/
21439#if defined(SQLITE_TEST)
21440SQLITE_API extern int sqlite3_open_file_count;
21441#define OpenCounter(X) sqlite3_open_file_count+=(X)
21442#else
21443#define OpenCounter(X)
21444#endif /* defined(SQLITE_TEST) */
21445
21446#endif /* !defined(_OS_COMMON_H_) */
21447
21448/************** End of os_common.h *******************************************/
21449/************** Begin file ctime.c *******************************************/
21450/* DO NOT EDIT!
21451** This file is automatically generated by the script in the canonical
21452** SQLite source tree at tool/mkctimec.tcl.
21453**
21454** To modify this header, edit any of the various lists in that script
21455** which specify categories of generated conditionals in this file.
21456*/
21457
21458/*
21459** 2010 February 23
21460**
21461** The author disclaims copyright to this source code. In place of
21462** a legal notice, here is a blessing:
21463**
21464** May you do good and not evil.
21465** May you find forgiveness for yourself and forgive others.
21466** May you share freely, never taking more than you give.
21467**
21468*************************************************************************
21469**
21470** This file implements routines used to report what compile-time options
21471** SQLite was built with.
21472*/
21473#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21474
21475/*
21476** Include the configuration header output by 'configure' if we're using the
21477** autoconf-based build
21478*/
21479#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
21480/* #include "sqlite_cfg.h" */
21481#define SQLITECONFIG_H 1
21482#endif
21483
21484/* These macros are provided to "stringify" the value of the define
21485** for those options in which the value is meaningful. */
21486#define CTIMEOPT_VAL_(opt) #opt
21487#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
21488
21489/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
21490** option requires a separate macro because legal values contain a single
21491** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21492#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 "," #opt2
21493#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt)
21494/* #include "sqliteInt.h" */
21495
21496/*
21497** An array of names of all compile-time options. This array should
21498** be sorted A-Z.
21499**
21500** This array looks large, but in a typical installation actually uses
21501** only a handful of compile-time options, so most times this array is usually
21502** rather short and uses little memory space.
21503*/
21504static const char * const sqlite3azCompileOpt[] = {
21505
21506#ifdef SQLITE_32BIT_ROWID
21507 "32BIT_ROWID",
21508#endif
21509#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
21510 "4_BYTE_ALIGNED_MALLOC",
21511#endif
21512#ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN
21513# if SQLITE_ALLOW_COVERING_INDEX_SCAN != 1
21514 "ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN),
21515# endif
21516#endif
21517#ifdef SQLITE_ALLOW_URI_AUTHORITY
21518 "ALLOW_URI_AUTHORITY",
21519#endif
21520#ifdef SQLITE_ATOMIC_INTRINSICS
21521 "ATOMIC_INTRINSICS=" CTIMEOPT_VAL(SQLITE_ATOMIC_INTRINSICS),
21522#endif
21523#ifdef SQLITE_BITMASK_TYPE
21524 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE),
21525#endif
21526#ifdef SQLITE_BUG_COMPATIBLE_20160819
21527 "BUG_COMPATIBLE_20160819",
21528#endif
21529#ifdef SQLITE_CASE_SENSITIVE_LIKE
21530 "CASE_SENSITIVE_LIKE",
21531#endif
21532#ifdef SQLITE_CHECK_PAGES
21533 "CHECK_PAGES",
21534#endif
21535#if defined(__clang__) && defined(__clang_major__)
21536 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21537 CTIMEOPT_VAL(__clang_minor__) "."
21538 CTIMEOPT_VAL(__clang_patchlevel__),
21539#elif defined(_MSC_VER)
21540 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21541#elif defined(__GNUC__) && defined(__VERSION__)
21542 "COMPILER=gcc-" __VERSION__,
21543#endif
21544#ifdef SQLITE_COVERAGE_TEST
21545 "COVERAGE_TEST",
21546#endif
21547#ifdef SQLITE_DEBUG
21548 "DEBUG",
21549#endif
21550#ifdef SQLITE_DEFAULT_AUTOMATIC_INDEX
21551 "DEFAULT_AUTOMATIC_INDEX",
21552#endif
21553#ifdef SQLITE_DEFAULT_AUTOVACUUM
21554 "DEFAULT_AUTOVACUUM",
21555#endif
21556#ifdef SQLITE_DEFAULT_CACHE_SIZE
21557 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE),
21558#endif
21559#ifdef SQLITE_DEFAULT_CKPTFULLFSYNC
21560 "DEFAULT_CKPTFULLFSYNC",
21561#endif
21562#ifdef SQLITE_DEFAULT_FILE_FORMAT
21563 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT),
21564#endif
21565#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS
21566 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS),
21567#endif
21568#ifdef SQLITE_DEFAULT_FOREIGN_KEYS
21569 "DEFAULT_FOREIGN_KEYS",
21570#endif
21571#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
21572 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
21573#endif
21574#ifdef SQLITE_DEFAULT_LOCKING_MODE
21575 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
21576#endif
21577#ifdef SQLITE_DEFAULT_LOOKASIDE
21578 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
21579#endif
21580#ifdef SQLITE_DEFAULT_MEMSTATUS
21581# if SQLITE_DEFAULT_MEMSTATUS != 1
21582 "DEFAULT_MEMSTATUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_MEMSTATUS),
21583# endif
21584#endif
21585#ifdef SQLITE_DEFAULT_MMAP_SIZE
21586 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
21587#endif
21588#ifdef SQLITE_DEFAULT_PAGE_SIZE
21589 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE),
21590#endif
21591#ifdef SQLITE_DEFAULT_PCACHE_INITSZ
21592 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ),
21593#endif
21594#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
21595 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS),
21596#endif
21597#ifdef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
21598 "DEFAULT_RECURSIVE_TRIGGERS",
21599#endif
21600#ifdef SQLITE_DEFAULT_ROWEST
21601 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST),
21602#endif
21603#ifdef SQLITE_DEFAULT_SECTOR_SIZE
21604 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE),
21605#endif
21606#ifdef SQLITE_DEFAULT_SYNCHRONOUS
21607 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
21608#endif
21609#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
21610 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT),
21611#endif
21612#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS
21613 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
21614#endif
21615#ifdef SQLITE_DEFAULT_WORKER_THREADS
21616 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS),
21617#endif
21618#ifdef SQLITE_DIRECT_OVERFLOW_READ
21619 "DIRECT_OVERFLOW_READ",
21620#endif
21621#ifdef SQLITE_DISABLE_DIRSYNC
21622 "DISABLE_DIRSYNC",
21623#endif
21624#ifdef SQLITE_DISABLE_FTS3_UNICODE
21625 "DISABLE_FTS3_UNICODE",
21626#endif
21627#ifdef SQLITE_DISABLE_FTS4_DEFERRED
21628 "DISABLE_FTS4_DEFERRED",
21629#endif
21630#ifdef SQLITE_DISABLE_INTRINSIC
21631 "DISABLE_INTRINSIC",
21632#endif
21633#ifdef SQLITE_DISABLE_LFS
21634 "DISABLE_LFS",
21635#endif
21636#ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
21637 "DISABLE_PAGECACHE_OVERFLOW_STATS",
21638#endif
21639#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
21640 "DISABLE_SKIPAHEAD_DISTINCT",
21641#endif
21642#ifdef SQLITE_DQS
21643 "DQS=" CTIMEOPT_VAL(SQLITE_DQS),
21644#endif
21645#ifdef SQLITE_ENABLE_8_3_NAMES
21646 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
21647#endif
21648#ifdef SQLITE_ENABLE_API_ARMOR
21649 "ENABLE_API_ARMOR",
21650#endif
21651#ifdef SQLITE_ENABLE_ATOMIC_WRITE
21652 "ENABLE_ATOMIC_WRITE",
21653#endif
21654#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
21655 "ENABLE_BATCH_ATOMIC_WRITE",
21656#endif
21657#ifdef SQLITE_ENABLE_BYTECODE_VTAB
21658 "ENABLE_BYTECODE_VTAB",
21659#endif
21660#ifdef SQLITE_ENABLE_CEROD
21661 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
21662#endif
21663#ifdef SQLITE_ENABLE_COLUMN_METADATA
21664 "ENABLE_COLUMN_METADATA",
21665#endif
21666#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
21667 "ENABLE_COLUMN_USED_MASK",
21668#endif
21669#ifdef SQLITE_ENABLE_COSTMULT
21670 "ENABLE_COSTMULT",
21671#endif
21672#ifdef SQLITE_ENABLE_CURSOR_HINTS
21673 "ENABLE_CURSOR_HINTS",
21674#endif
21675#ifdef SQLITE_ENABLE_DBPAGE_VTAB
21676 "ENABLE_DBPAGE_VTAB",
21677#endif
21678#ifdef SQLITE_ENABLE_DBSTAT_VTAB
21679 "ENABLE_DBSTAT_VTAB",
21680#endif
21681#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
21682 "ENABLE_EXPENSIVE_ASSERT",
21683#endif
21684#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
21685 "ENABLE_EXPLAIN_COMMENTS",
21686#endif
21687#ifdef SQLITE_ENABLE_FTS3
21688 "ENABLE_FTS3",
21689#endif
21690#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
21691 "ENABLE_FTS3_PARENTHESIS",
21692#endif
21693#ifdef SQLITE_ENABLE_FTS3_TOKENIZER
21694 "ENABLE_FTS3_TOKENIZER",
21695#endif
21696#ifdef SQLITE_ENABLE_FTS4
21697 "ENABLE_FTS4",
21698#endif
21699#ifdef SQLITE_ENABLE_FTS5
21700 "ENABLE_FTS5",
21701#endif
21702#ifdef SQLITE_ENABLE_GEOPOLY
21703 "ENABLE_GEOPOLY",
21704#endif
21705#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
21706 "ENABLE_HIDDEN_COLUMNS",
21707#endif
21708#ifdef SQLITE_ENABLE_ICU
21709 "ENABLE_ICU",
21710#endif
21711#ifdef SQLITE_ENABLE_IOTRACE
21712 "ENABLE_IOTRACE",
21713#endif
21714#ifdef SQLITE_ENABLE_LOAD_EXTENSION
21715 "ENABLE_LOAD_EXTENSION",
21716#endif
21717#ifdef SQLITE_ENABLE_LOCKING_STYLE
21718 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
21719#endif
21720#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
21721 "ENABLE_MATH_FUNCTIONS",
21722#endif
21723#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
21724 "ENABLE_MEMORY_MANAGEMENT",
21725#endif
21726#ifdef SQLITE_ENABLE_MEMSYS3
21727 "ENABLE_MEMSYS3",
21728#endif
21729#ifdef SQLITE_ENABLE_MEMSYS5
21730 "ENABLE_MEMSYS5",
21731#endif
21732#ifdef SQLITE_ENABLE_MULTIPLEX
21733 "ENABLE_MULTIPLEX",
21734#endif
21735#ifdef SQLITE_ENABLE_NORMALIZE
21736 "ENABLE_NORMALIZE",
21737#endif
21738#ifdef SQLITE_ENABLE_NULL_TRIM
21739 "ENABLE_NULL_TRIM",
21740#endif
21741#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
21742 "ENABLE_OFFSET_SQL_FUNC",
21743#endif
21744#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
21745 "ENABLE_OVERSIZE_CELL_CHECK",
21746#endif
21747#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
21748 "ENABLE_PREUPDATE_HOOK",
21749#endif
21750#ifdef SQLITE_ENABLE_QPSG
21751 "ENABLE_QPSG",
21752#endif
21753#ifdef SQLITE_ENABLE_RBU
21754 "ENABLE_RBU",
21755#endif
21756#ifdef SQLITE_ENABLE_RTREE
21757 "ENABLE_RTREE",
21758#endif
21759#ifdef SQLITE_ENABLE_SESSION
21760 "ENABLE_SESSION",
21761#endif
21762#ifdef SQLITE_ENABLE_SNAPSHOT
21763 "ENABLE_SNAPSHOT",
21764#endif
21765#ifdef SQLITE_ENABLE_SORTER_REFERENCES
21766 "ENABLE_SORTER_REFERENCES",
21767#endif
21768#ifdef SQLITE_ENABLE_SQLLOG
21769 "ENABLE_SQLLOG",
21770#endif
21771#ifdef SQLITE_ENABLE_STAT4
21772 "ENABLE_STAT4",
21773#endif
21774#ifdef SQLITE_ENABLE_STMTVTAB
21775 "ENABLE_STMTVTAB",
21776#endif
21777#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
21778 "ENABLE_STMT_SCANSTATUS",
21779#endif
21780#ifdef SQLITE_ENABLE_TREETRACE
21781 "ENABLE_TREETRACE",
21782#endif
21783#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
21784 "ENABLE_UNKNOWN_SQL_FUNCTION",
21785#endif
21786#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
21787 "ENABLE_UNLOCK_NOTIFY",
21788#endif
21789#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
21790 "ENABLE_UPDATE_DELETE_LIMIT",
21791#endif
21792#ifdef SQLITE_ENABLE_URI_00_ERROR
21793 "ENABLE_URI_00_ERROR",
21794#endif
21795#ifdef SQLITE_ENABLE_VFSTRACE
21796 "ENABLE_VFSTRACE",
21797#endif
21798#ifdef SQLITE_ENABLE_WHERETRACE
21799 "ENABLE_WHERETRACE",
21800#endif
21801#ifdef SQLITE_ENABLE_ZIPVFS
21802 "ENABLE_ZIPVFS",
21803#endif
21804#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
21805 "EXPLAIN_ESTIMATED_ROWS",
21806#endif
21807#ifdef SQLITE_EXTRA_IFNULLROW
21808 "EXTRA_IFNULLROW",
21809#endif
21810#ifdef SQLITE_EXTRA_INIT
21811 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
21812#endif
21813#ifdef SQLITE_EXTRA_SHUTDOWN
21814 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
21815#endif
21816#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
21817 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
21818#endif
21819#ifdef SQLITE_FTS5_ENABLE_TEST_MI
21820 "FTS5_ENABLE_TEST_MI",
21821#endif
21822#ifdef SQLITE_FTS5_NO_WITHOUT_ROWID
21823 "FTS5_NO_WITHOUT_ROWID",
21824#endif
21825#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
21826 "HAVE_ISNAN",
21827#endif
21828#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
21829# if SQLITE_HOMEGROWN_RECURSIVE_MUTEX != 1
21830 "HOMEGROWN_RECURSIVE_MUTEX=" CTIMEOPT_VAL(SQLITE_HOMEGROWN_RECURSIVE_MUTEX),
21831# endif
21832#endif
21833#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
21834 "IGNORE_AFP_LOCK_ERRORS",
21835#endif
21836#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
21837 "IGNORE_FLOCK_LOCK_ERRORS",
21838#endif
21839#ifdef SQLITE_INLINE_MEMCPY
21840 "INLINE_MEMCPY",
21841#endif
21842#ifdef SQLITE_INT64_TYPE
21843 "INT64_TYPE",
21844#endif
21845#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
21846 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
21847#endif
21848#ifdef SQLITE_LEGACY_JSON_VALID
21849 "LEGACY_JSON_VALID",
21850#endif
21851#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
21852 "LIKE_DOESNT_MATCH_BLOBS",
21853#endif
21854#ifdef SQLITE_LOCK_TRACE
21855 "LOCK_TRACE",
21856#endif
21857#ifdef SQLITE_LOG_CACHE_SPILL
21858 "LOG_CACHE_SPILL",
21859#endif
21860#ifdef SQLITE_MALLOC_SOFT_LIMIT
21861 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
21862#endif
21863#ifdef SQLITE_MAX_ATTACHED
21864 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
21865#endif
21866#ifdef SQLITE_MAX_COLUMN
21867 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
21868#endif
21869#ifdef SQLITE_MAX_COMPOUND_SELECT
21870 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
21871#endif
21872#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
21873 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
21874#endif
21875#ifdef SQLITE_MAX_EXPR_DEPTH
21876 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
21877#endif
21878#ifdef SQLITE_MAX_FUNCTION_ARG
21879 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
21880#endif
21881#ifdef SQLITE_MAX_LENGTH
21882 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
21883#endif
21884#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
21885 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
21886#endif
21887#ifdef SQLITE_MAX_MEMORY
21888 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
21889#endif
21890#ifdef SQLITE_MAX_MMAP_SIZE
21891 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
21892#endif
21893#ifdef SQLITE_MAX_MMAP_SIZE_
21894 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
21895#endif
21896#ifdef SQLITE_MAX_PAGE_COUNT
21897 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
21898#endif
21899#ifdef SQLITE_MAX_PAGE_SIZE
21900 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
21901#endif
21902#ifdef SQLITE_MAX_SCHEMA_RETRY
21903 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
21904#endif
21905#ifdef SQLITE_MAX_SQL_LENGTH
21906 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
21907#endif
21908#ifdef SQLITE_MAX_TRIGGER_DEPTH
21909 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
21910#endif
21911#ifdef SQLITE_MAX_VARIABLE_NUMBER
21912 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
21913#endif
21914#ifdef SQLITE_MAX_VDBE_OP
21915 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
21916#endif
21917#ifdef SQLITE_MAX_WORKER_THREADS
21918 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
21919#endif
21920#ifdef SQLITE_MEMDEBUG
21921 "MEMDEBUG",
21922#endif
21923#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
21924 "MIXED_ENDIAN_64BIT_FLOAT",
21925#endif
21926#ifdef SQLITE_MMAP_READWRITE
21927 "MMAP_READWRITE",
21928#endif
21929#ifdef SQLITE_MUTEX_NOOP
21930 "MUTEX_NOOP",
21931#endif
21932#ifdef SQLITE_MUTEX_OMIT
21933 "MUTEX_OMIT",
21934#endif
21935#ifdef SQLITE_MUTEX_PTHREADS
21936 "MUTEX_PTHREADS",
21937#endif
21938#ifdef SQLITE_MUTEX_W32
21939 "MUTEX_W32",
21940#endif
21941#ifdef SQLITE_NEED_ERR_NAME
21942 "NEED_ERR_NAME",
21943#endif
21944#ifdef SQLITE_NO_SYNC
21945 "NO_SYNC",
21946#endif
21947#ifdef SQLITE_OMIT_ALTERTABLE
21948 "OMIT_ALTERTABLE",
21949#endif
21950#ifdef SQLITE_OMIT_ANALYZE
21951 "OMIT_ANALYZE",
21952#endif
21953#ifdef SQLITE_OMIT_ATTACH
21954 "OMIT_ATTACH",
21955#endif
21956#ifdef SQLITE_OMIT_AUTHORIZATION
21957 "OMIT_AUTHORIZATION",
21958#endif
21959#ifdef SQLITE_OMIT_AUTOINCREMENT
21960 "OMIT_AUTOINCREMENT",
21961#endif
21962#ifdef SQLITE_OMIT_AUTOINIT
21963 "OMIT_AUTOINIT",
21964#endif
21965#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
21966 "OMIT_AUTOMATIC_INDEX",
21967#endif
21968#ifdef SQLITE_OMIT_AUTORESET
21969 "OMIT_AUTORESET",
21970#endif
21971#ifdef SQLITE_OMIT_AUTOVACUUM
21972 "OMIT_AUTOVACUUM",
21973#endif
21974#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
21975 "OMIT_BETWEEN_OPTIMIZATION",
21976#endif
21977#ifdef SQLITE_OMIT_BLOB_LITERAL
21978 "OMIT_BLOB_LITERAL",
21979#endif
21980#ifdef SQLITE_OMIT_CAST
21981 "OMIT_CAST",
21982#endif
21983#ifdef SQLITE_OMIT_CHECK
21984 "OMIT_CHECK",
21985#endif
21986#ifdef SQLITE_OMIT_COMPLETE
21987 "OMIT_COMPLETE",
21988#endif
21989#ifdef SQLITE_OMIT_COMPOUND_SELECT
21990 "OMIT_COMPOUND_SELECT",
21991#endif
21992#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
21993 "OMIT_CONFLICT_CLAUSE",
21994#endif
21995#ifdef SQLITE_OMIT_CTE
21996 "OMIT_CTE",
21997#endif
21998#if defined(SQLITE_OMIT_DATETIME_FUNCS) || defined(SQLITE_OMIT_FLOATING_POINT)
21999 "OMIT_DATETIME_FUNCS",
22000#endif
22001#ifdef SQLITE_OMIT_DECLTYPE
22002 "OMIT_DECLTYPE",
22003#endif
22004#ifdef SQLITE_OMIT_DEPRECATED
22005 "OMIT_DEPRECATED",
22006#endif
22007#ifdef SQLITE_OMIT_DESERIALIZE
22008 "OMIT_DESERIALIZE",
22009#endif
22010#ifdef SQLITE_OMIT_DISKIO
22011 "OMIT_DISKIO",
22012#endif
22013#ifdef SQLITE_OMIT_EXPLAIN
22014 "OMIT_EXPLAIN",
22015#endif
22016#ifdef SQLITE_OMIT_FLAG_PRAGMAS
22017 "OMIT_FLAG_PRAGMAS",
22018#endif
22019#ifdef SQLITE_OMIT_FLOATING_POINT
22020 "OMIT_FLOATING_POINT",
22021#endif
22022#ifdef SQLITE_OMIT_FOREIGN_KEY
22023 "OMIT_FOREIGN_KEY",
22024#endif
22025#ifdef SQLITE_OMIT_GET_TABLE
22026 "OMIT_GET_TABLE",
22027#endif
22028#ifdef SQLITE_OMIT_HEX_INTEGER
22029 "OMIT_HEX_INTEGER",
22030#endif
22031#ifdef SQLITE_OMIT_INCRBLOB
22032 "OMIT_INCRBLOB",
22033#endif
22034#ifdef SQLITE_OMIT_INTEGRITY_CHECK
22035 "OMIT_INTEGRITY_CHECK",
22036#endif
22037#ifdef SQLITE_OMIT_INTROSPECTION_PRAGMAS
22038 "OMIT_INTROSPECTION_PRAGMAS",
22039#endif
22040#ifdef SQLITE_OMIT_JSON
22041 "OMIT_JSON",
22042#endif
22043#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
22044 "OMIT_LIKE_OPTIMIZATION",
22045#endif
22046#ifdef SQLITE_OMIT_LOAD_EXTENSION
22047 "OMIT_LOAD_EXTENSION",
22048#endif
22049#ifdef SQLITE_OMIT_LOCALTIME
22050 "OMIT_LOCALTIME",
22051#endif
22052#ifdef SQLITE_OMIT_LOOKASIDE
22053 "OMIT_LOOKASIDE",
22054#endif
22055#ifdef SQLITE_OMIT_MEMORYDB
22056 "OMIT_MEMORYDB",
22057#endif
22058#ifdef SQLITE_OMIT_OR_OPTIMIZATION
22059 "OMIT_OR_OPTIMIZATION",
22060#endif
22061#ifdef SQLITE_OMIT_PAGER_PRAGMAS
22062 "OMIT_PAGER_PRAGMAS",
22063#endif
22064#ifdef SQLITE_OMIT_PARSER_TRACE
22065 "OMIT_PARSER_TRACE",
22066#endif
22067#ifdef SQLITE_OMIT_POPEN
22068 "OMIT_POPEN",
22069#endif
22070#ifdef SQLITE_OMIT_PRAGMA
22071 "OMIT_PRAGMA",
22072#endif
22073#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
22074 "OMIT_PROGRESS_CALLBACK",
22075#endif
22076#ifdef SQLITE_OMIT_QUICKBALANCE
22077 "OMIT_QUICKBALANCE",
22078#endif
22079#ifdef SQLITE_OMIT_REINDEX
22080 "OMIT_REINDEX",
22081#endif
22082#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
22083 "OMIT_SCHEMA_PRAGMAS",
22084#endif
22085#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
22086 "OMIT_SCHEMA_VERSION_PRAGMAS",
22087#endif
22088#ifdef SQLITE_OMIT_SHARED_CACHE
22089 "OMIT_SHARED_CACHE",
22090#endif
22091#ifdef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
22092 "OMIT_SHUTDOWN_DIRECTORIES",
22093#endif
22094#ifdef SQLITE_OMIT_SUBQUERY
22095 "OMIT_SUBQUERY",
22096#endif
22097#ifdef SQLITE_OMIT_TCL_VARIABLE
22098 "OMIT_TCL_VARIABLE",
22099#endif
22100#ifdef SQLITE_OMIT_TEMPDB
22101 "OMIT_TEMPDB",
22102#endif
22103#ifdef SQLITE_OMIT_TEST_CONTROL
22104 "OMIT_TEST_CONTROL",
22105#endif
22106#ifdef SQLITE_OMIT_TRACE
22107# if SQLITE_OMIT_TRACE != 1
22108 "OMIT_TRACE=" CTIMEOPT_VAL(SQLITE_OMIT_TRACE),
22109# endif
22110#endif
22111#ifdef SQLITE_OMIT_TRIGGER
22112 "OMIT_TRIGGER",
22113#endif
22114#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
22115 "OMIT_TRUNCATE_OPTIMIZATION",
22116#endif
22117#ifdef SQLITE_OMIT_UTF16
22118 "OMIT_UTF16",
22119#endif
22120#ifdef SQLITE_OMIT_VACUUM
22121 "OMIT_VACUUM",
22122#endif
22123#ifdef SQLITE_OMIT_VIEW
22124 "OMIT_VIEW",
22125#endif
22126#ifdef SQLITE_OMIT_VIRTUALTABLE
22127 "OMIT_VIRTUALTABLE",
22128#endif
22129#ifdef SQLITE_OMIT_WAL
22130 "OMIT_WAL",
22131#endif
22132#ifdef SQLITE_OMIT_WSD
22133 "OMIT_WSD",
22134#endif
22135#ifdef SQLITE_OMIT_XFER_OPT
22136 "OMIT_XFER_OPT",
22137#endif
22138#ifdef SQLITE_PERFORMANCE_TRACE
22139 "PERFORMANCE_TRACE",
22140#endif
22141#ifdef SQLITE_POWERSAFE_OVERWRITE
22142# if SQLITE_POWERSAFE_OVERWRITE != 1
22143 "POWERSAFE_OVERWRITE=" CTIMEOPT_VAL(SQLITE_POWERSAFE_OVERWRITE),
22144# endif
22145#endif
22146#ifdef SQLITE_PREFER_PROXY_LOCKING
22147 "PREFER_PROXY_LOCKING",
22148#endif
22149#ifdef SQLITE_PROXY_DEBUG
22150 "PROXY_DEBUG",
22151#endif
22152#ifdef SQLITE_REVERSE_UNORDERED_SELECTS
22153 "REVERSE_UNORDERED_SELECTS",
22154#endif
22155#ifdef SQLITE_RTREE_INT_ONLY
22156 "RTREE_INT_ONLY",
22157#endif
22158#ifdef SQLITE_SECURE_DELETE
22159 "SECURE_DELETE",
22160#endif
22161#ifdef SQLITE_SMALL_STACK
22162 "SMALL_STACK",
22163#endif
22164#ifdef SQLITE_SORTER_PMASZ
22165 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
22166#endif
22167#ifdef SQLITE_SOUNDEX
22168 "SOUNDEX",
22169#endif
22170#ifdef SQLITE_STAT4_SAMPLES
22171 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
22172#endif
22173#ifdef SQLITE_STMTJRNL_SPILL
22174 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
22175#endif
22176#ifdef SQLITE_SUBSTR_COMPATIBILITY
22177 "SUBSTR_COMPATIBILITY",
22178#endif
22179#if (!defined(SQLITE_WIN32_MALLOC) \
22180 && !defined(SQLITE_ZERO_MALLOC) \
22181 && !defined(SQLITE_MEMDEBUG) \
22182 ) || defined(SQLITE_SYSTEM_MALLOC)
22183 "SYSTEM_MALLOC",
22184#endif
22185#ifdef SQLITE_TCL
22186 "TCL",
22187#endif
22188#ifdef SQLITE_TEMP_STORE
22189 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
22190#endif
22191#ifdef SQLITE_TEST
22192 "TEST",
22193#endif
22194#if defined(SQLITE_THREADSAFE)
22195 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
22196#elif defined(THREADSAFE)
22197 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
22198#else
22199 "THREADSAFE=1",
22200#endif
22201#ifdef SQLITE_UNLINK_AFTER_CLOSE
22202 "UNLINK_AFTER_CLOSE",
22203#endif
22204#ifdef SQLITE_UNTESTABLE
22205 "UNTESTABLE",
22206#endif
22207#ifdef SQLITE_USER_AUTHENTICATION
22208 "USER_AUTHENTICATION",
22209#endif
22210#ifdef SQLITE_USE_ALLOCA
22211 "USE_ALLOCA",
22212#endif
22213#ifdef SQLITE_USE_FCNTL_TRACE
22214 "USE_FCNTL_TRACE",
22215#endif
22216#ifdef SQLITE_USE_URI
22217 "USE_URI",
22218#endif
22219#ifdef SQLITE_VDBE_COVERAGE
22220 "VDBE_COVERAGE",
22221#endif
22222#ifdef SQLITE_WIN32_MALLOC
22223 "WIN32_MALLOC",
22224#endif
22225#ifdef SQLITE_ZERO_MALLOC
22226 "ZERO_MALLOC",
22227#endif
22228
22229} ;
22230
22231SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
22232 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
22233 return (const char**)sqlite3azCompileOpt;
22234}
22235
22236#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
22237
22238/************** End of ctime.c ***********************************************/
22239/************** Begin file global.c ******************************************/
22240/*
22241** 2008 June 13
22242**
22243** The author disclaims copyright to this source code. In place of
22244** a legal notice, here is a blessing:
22245**
22246** May you do good and not evil.
22247** May you find forgiveness for yourself and forgive others.
22248** May you share freely, never taking more than you give.
22249**
22250*************************************************************************
22251**
22252** This file contains definitions of global variables and constants.
22253*/
22254/* #include "sqliteInt.h" */
22255
22256/* An array to map all upper-case characters into their corresponding
22257** lower-case character.
22258**
22259** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22260** handle case conversions for the UTF character set since the tables
22261** involved are nearly as big or bigger than SQLite itself.
22262*/
22263SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
22264#ifdef SQLITE_ASCII
22265 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
22266 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
22267 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
22268 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
22269 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
22270 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
22271 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
22272 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
22273 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
22274 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
22275 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
22276 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
22277 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
22278 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
22279 252,253,254,255,
22280#endif
22281#ifdef SQLITE_EBCDIC
22282 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
22283 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
22284 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
22285 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
22286 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
22287 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
22288 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
22289 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
22290 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
22291 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
22292 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
22293 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
22294 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
22295 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
22296 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
22297 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
22298#endif
22299/* All of the upper-to-lower conversion data is above. The following
22300** 18 integers are completely unrelated. They are appended to the
22301** sqlite3UpperToLower[] array to avoid UBSAN warnings. Here's what is
22302** going on:
22303**
22304** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented
22305** by invoking sqlite3MemCompare(A,B) which compares values A and B and
22306** returns negative, zero, or positive if A is less then, equal to, or
22307** greater than B, respectively. Then the true false results is found by
22308** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or
22309** sqlite3aGTb[opcode] depending on whether the result of compare(A,B)
22310** is negative, zero, or positive, where opcode is the specific opcode.
22311** The only works because the comparison opcodes are consecutive and in
22312** this order: NE EQ GT LE LT GE. Various assert()s throughout the code
22313** ensure that is the case.
22314**
22315** These elements must be appended to another array. Otherwise the
22316** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22317** be undefined behavior. That's goofy, but the C-standards people thought
22318** it was a good idea, so here we are.
22319*/
22320/* NE EQ GT LE LT GE */
22321 1, 0, 0, 1, 1, 0, /* aLTb[]: Use when compare(A,B) less than zero */
22322 0, 1, 0, 1, 0, 1, /* aEQb[]: Use when compare(A,B) equals zero */
22323 1, 0, 1, 0, 0, 1 /* aGTb[]: Use when compare(A,B) greater than zero*/
22324};
22325SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22326SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22327SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22328
22329/*
22330** The following 256 byte lookup table is used to support SQLites built-in
22331** equivalents to the following standard library functions:
22332**
22333** isspace() 0x01
22334** isalpha() 0x02
22335** isdigit() 0x04
22336** isalnum() 0x06
22337** isxdigit() 0x08
22338** toupper() 0x20
22339** SQLite identifier character 0x40 $, _, or non-ascii
22340** Quote character 0x80
22341**
22342** Bit 0x20 is set if the mapped character requires translation to upper
22343** case. i.e. if the character is a lower-case ASCII character.
22344** If x is a lower-case ASCII character, then its upper-case equivalent
22345** is (x - 0x20). Therefore toupper() can be implemented as:
22346**
22347** (x & ~(map[x]&0x20))
22348**
22349** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
22350** array. tolower() is used more often than toupper() by SQLite.
22351**
22352** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22353** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
22354** non-ASCII UTF character. Hence the test for whether or not a character is
22355** part of an identifier is 0x46.
22356*/
22357SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
22358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
22359 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
22360 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
22361 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
22362 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
22363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22364 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
22365 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
22366
22367 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
22368 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
22369 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
22370 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
22371 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
22372 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
22373 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
22374 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
22375
22376 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
22377 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
22378 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
22379 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
22380 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
22381 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
22382 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
22383 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
22384
22385 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
22386 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
22387 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
22388 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
22389 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
22390 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
22391 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
22392 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
22393};
22394
22395/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22396** compatibility for legacy applications, the URI filename capability is
22397** disabled by default.
22398**
22399** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22400** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22401**
22402** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22403** disabled. The default value may be changed by compiling with the
22404** SQLITE_USE_URI symbol defined.
22405*/
22406#ifndef SQLITE_USE_URI
22407# define SQLITE_USE_URI 0
22408#endif
22409
22410/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22411** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22412** that compile-time option is omitted.
22413*/
22414#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN)
22415# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
22416#else
22417# if !SQLITE_ALLOW_COVERING_INDEX_SCAN
22418# error "Compile-time disabling of covering index scan using the\
22419 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22420 Contact SQLite developers if this is a problem for you, and\
22421 delete this #error macro to continue with your build."
22422# endif
22423#endif
22424
22425/* The minimum PMA size is set to this value multiplied by the database
22426** page size in bytes.
22427*/
22428#ifndef SQLITE_SORTER_PMASZ
22429# define SQLITE_SORTER_PMASZ 250
22430#endif
22431
22432/* Statement journals spill to disk when their size exceeds the following
22433** threshold (in bytes). 0 means that statement journals are created and
22434** written to disk immediately (the default behavior for SQLite versions
22435** before 3.12.0). -1 means always keep the entire statement journal in
22436** memory. (The statement journal is also always held entirely in memory
22437** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
22438** setting.)
22439*/
22440#ifndef SQLITE_STMTJRNL_SPILL
22441# define SQLITE_STMTJRNL_SPILL (64*1024)
22442#endif
22443
22444/*
22445** The default lookaside-configuration, the format "SZ,N". SZ is the
22446** number of bytes in each lookaside slot (should be a multiple of 8)
22447** and N is the number of slots. The lookaside-configuration can be
22448** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22449** or at run-time for an individual database connection using
22450** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
22451**
22452** With the two-size-lookaside enhancement, less lookaside is required.
22453** The default configuration of 1200,40 actually provides 30 1200-byte slots
22454** and 93 128-byte slots, which is more lookaside than is available
22455** using the older 1200,100 configuration without two-size-lookaside.
22456*/
22457#ifndef SQLITE_DEFAULT_LOOKASIDE
22458# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
22459# define SQLITE_DEFAULT_LOOKASIDE 1200,100 /* 120KB of memory */
22460# else
22461# define SQLITE_DEFAULT_LOOKASIDE 1200,40 /* 48KB of memory */
22462# endif
22463#endif
22464
22465
22466/* The default maximum size of an in-memory database created using
22467** sqlite3_deserialize()
22468*/
22469#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE
22470# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824
22471#endif
22472
22473/*
22474** The following singleton contains the global configuration for
22475** the SQLite library.
22476*/
22477SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
22478 SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
22479 1, /* bCoreMutex */
22480 SQLITE_THREADSAFE==1, /* bFullMutex */
22481 SQLITE_USE_URI, /* bOpenUri */
22482 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
22483 0, /* bSmallMalloc */
22484 1, /* bExtraSchemaChecks */
22485 sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */
22486 0x7ffffffe, /* mxStrlen */
22487 0, /* neverCorrupt */
22488 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
22489 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
22490 {0,0,0,0,0,0,0,0}, /* m */
22491 {0,0,0,0,0,0,0,0,0}, /* mutex */
22492 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
22493 (void*)0, /* pHeap */
22494 0, /* nHeap */
22495 0, 0, /* mnHeap, mxHeap */
22496 SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
22497 SQLITE_MAX_MMAP_SIZE, /* mxMmap */
22498 (void*)0, /* pPage */
22499 0, /* szPage */
22500 SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
22501 0, /* mxParserStack */
22502 0, /* sharedCacheEnabled */
22503 SQLITE_SORTER_PMASZ, /* szPma */
22504 /* All the rest should always be initialized to zero */
22505 0, /* isInit */
22506 0, /* inProgress */
22507 0, /* isMutexInit */
22508 0, /* isMallocInit */
22509 0, /* isPCacheInit */
22510 0, /* nRefInitMutex */
22511 0, /* pInitMutex */
22512 0, /* xLog */
22513 0, /* pLogArg */
22514#ifdef SQLITE_ENABLE_SQLLOG
22515 0, /* xSqllog */
22516 0, /* pSqllogArg */
22517#endif
22518#ifdef SQLITE_VDBE_COVERAGE
22519 0, /* xVdbeBranch */
22520 0, /* pVbeBranchArg */
22521#endif
22522#ifndef SQLITE_OMIT_DESERIALIZE
22523 SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */
22524#endif
22525#ifndef SQLITE_UNTESTABLE
22526 0, /* xTestCallback */
22527#endif
22528 0, /* bLocaltimeFault */
22529 0, /* xAltLocaltime */
22530 0x7ffffffe, /* iOnceResetThreshold */
22531 SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */
22532 0, /* iPrngSeed */
22533#ifdef SQLITE_DEBUG
22534 {0,0,0,0,0,0}, /* aTune */
22535#endif
22536};
22537
22538/*
22539** Hash table for global functions - functions common to all
22540** database connections. After initialization, this table is
22541** read-only.
22542*/
22543SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
22544
22545#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
22546/*
22547** Counter used for coverage testing. Does not come into play for
22548** release builds.
22549**
22550** Access to this global variable is not mutex protected. This might
22551** result in TSAN warnings. But as the variable does not exist in
22552** release builds, that should not be a concern.
22553*/
22554SQLITE_PRIVATE unsigned int sqlite3CoverageCounter;
22555#endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */
22556
22557#ifdef VDBE_PROFILE
22558/*
22559** The following performance counter can be used in place of
22560** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22561*/
22562SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
22563#endif
22564
22565/*
22566** The value of the "pending" byte must be 0x40000000 (1 byte past the
22567** 1-gibabyte boundary) in a compatible database. SQLite never uses
22568** the database page that contains the pending byte. It never attempts
22569** to read or write that page. The pending byte page is set aside
22570** for use by the VFS layers as space for managing file locks.
22571**
22572** During testing, it is often desirable to move the pending byte to
22573** a different position in the file. This allows code that has to
22574** deal with the pending byte to run on files that are much smaller
22575** than 1 GiB. The sqlite3_test_control() interface can be used to
22576** move the pending byte.
22577**
22578** IMPORTANT: Changing the pending byte to any value other than
22579** 0x40000000 results in an incompatible database file format!
22580** Changing the pending byte during operation will result in undefined
22581** and incorrect behavior.
22582*/
22583#ifndef SQLITE_OMIT_WSD
22584SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
22585#endif
22586
22587/*
22588** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS.
22589*/
22590SQLITE_PRIVATE u32 sqlite3TreeTrace = 0;
22591SQLITE_PRIVATE u32 sqlite3WhereTrace = 0;
22592
22593/* #include "opcodes.h" */
22594/*
22595** Properties of opcodes. The OPFLG_INITIALIZER macro is
22596** created by mkopcodeh.awk during compilation. Data is obtained
22597** from the comments following the "case OP_xxxx:" statements in
22598** the vdbe.c file.
22599*/
22600SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
22601
22602/*
22603** Name of the default collating sequence
22604*/
22605SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
22606
22607/*
22608** Standard typenames. These names must match the COLTYPE_* definitions.
22609** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
22610**
22611** sqlite3StdType[] The actual names of the datatypes.
22612**
22613** sqlite3StdTypeLen[] The length (in bytes) of each entry
22614** in sqlite3StdType[].
22615**
22616** sqlite3StdTypeAffinity[] The affinity associated with each entry
22617** in sqlite3StdType[].
22618*/
22619SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
22620SQLITE_PRIVATE const char sqlite3StdTypeAffinity[] = {
22621 SQLITE_AFF_NUMERIC,
22622 SQLITE_AFF_BLOB,
22623 SQLITE_AFF_INTEGER,
22624 SQLITE_AFF_INTEGER,
22625 SQLITE_AFF_REAL,
22626 SQLITE_AFF_TEXT
22627};
22628SQLITE_PRIVATE const char *sqlite3StdType[] = {
22629 "ANY",
22630 "BLOB",
22631 "INT",
22632 "INTEGER",
22633 "REAL",
22634 "TEXT"
22635};
22636
22637/************** End of global.c **********************************************/
22638/************** Begin file status.c ******************************************/
22639/*
22640** 2008 June 18
22641**
22642** The author disclaims copyright to this source code. In place of
22643** a legal notice, here is a blessing:
22644**
22645** May you do good and not evil.
22646** May you find forgiveness for yourself and forgive others.
22647** May you share freely, never taking more than you give.
22648**
22649*************************************************************************
22650**
22651** This module implements the sqlite3_status() interface and related
22652** functionality.
22653*/
22654/* #include "sqliteInt.h" */
22655/************** Include vdbeInt.h in the middle of status.c ******************/
22656/************** Begin file vdbeInt.h *****************************************/
22657/*
22658** 2003 September 6
22659**
22660** The author disclaims copyright to this source code. In place of
22661** a legal notice, here is a blessing:
22662**
22663** May you do good and not evil.
22664** May you find forgiveness for yourself and forgive others.
22665** May you share freely, never taking more than you give.
22666**
22667*************************************************************************
22668** This is the header file for information that is private to the
22669** VDBE. This information used to all be at the top of the single
22670** source code file "vdbe.c". When that file became too big (over
22671** 6000 lines long) it was split up into several smaller files and
22672** this header information was factored out.
22673*/
22674#ifndef SQLITE_VDBEINT_H
22675#define SQLITE_VDBEINT_H
22676
22677/*
22678** The maximum number of times that a statement will try to reparse
22679** itself before giving up and returning SQLITE_SCHEMA.
22680*/
22681#ifndef SQLITE_MAX_SCHEMA_RETRY
22682# define SQLITE_MAX_SCHEMA_RETRY 50
22683#endif
22684
22685/*
22686** VDBE_DISPLAY_P4 is true or false depending on whether or not the
22687** "explain" P4 display logic is enabled.
22688*/
22689#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
22690 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
22691 || defined(SQLITE_ENABLE_BYTECODE_VTAB)
22692# define VDBE_DISPLAY_P4 1
22693#else
22694# define VDBE_DISPLAY_P4 0
22695#endif
22696
22697/*
22698** SQL is translated into a sequence of instructions to be
22699** executed by a virtual machine. Each instruction is an instance
22700** of the following structure.
22701*/
22702typedef struct VdbeOp Op;
22703
22704/*
22705** Boolean values
22706*/
22707typedef unsigned Bool;
22708
22709/* Opaque type used by code in vdbesort.c */
22710typedef struct VdbeSorter VdbeSorter;
22711
22712/* Elements of the linked list at Vdbe.pAuxData */
22713typedef struct AuxData AuxData;
22714
22715/* A cache of large TEXT or BLOB values in a VdbeCursor */
22716typedef struct VdbeTxtBlbCache VdbeTxtBlbCache;
22717
22718/* Types of VDBE cursors */
22719#define CURTYPE_BTREE 0
22720#define CURTYPE_SORTER 1
22721#define CURTYPE_VTAB 2
22722#define CURTYPE_PSEUDO 3
22723
22724/*
22725** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
22726**
22727** * A b-tree cursor
22728** - In the main database or in an ephemeral database
22729** - On either an index or a table
22730** * A sorter
22731** * A virtual table
22732** * A one-row "pseudotable" stored in a single register
22733*/
22734typedef struct VdbeCursor VdbeCursor;
22735struct VdbeCursor {
22736 u8 eCurType; /* One of the CURTYPE_* values above */
22737 i8 iDb; /* Index of cursor database in db->aDb[] */
22738 u8 nullRow; /* True if pointing to a row with no data */
22739 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
22740 u8 isTable; /* True for rowid tables. False for indexes */
22741#ifdef SQLITE_DEBUG
22742 u8 seekOp; /* Most recent seek operation on this cursor */
22743 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
22744#endif
22745 Bool isEphemeral:1; /* True for an ephemeral table */
22746 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
22747 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
22748 Bool noReuse:1; /* OpenEphemeral may not reuse this cursor */
22749 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
22750 u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */
22751 union { /* pBtx for isEphermeral. pAltMap otherwise */
22752 Btree *pBtx; /* Separate file holding temporary table */
22753 u32 *aAltMap; /* Mapping from table to index column numbers */
22754 } ub;
22755 i64 seqCount; /* Sequence counter */
22756
22757 /* Cached OP_Column parse information is only valid if cacheStatus matches
22758 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
22759 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
22760 ** the cache is out of date. */
22761 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
22762 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
22763 ** if there have been no prior seeks on the cursor. */
22764 /* seekResult does not distinguish between "no seeks have ever occurred
22765 ** on this cursor" and "the most recent seek was an exact match".
22766 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
22767
22768 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
22769 ** The fields that follow are uninitialized, and must be individually
22770 ** initialized prior to first use. */
22771 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
22772 union {
22773 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
22774 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
22775 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
22776 } uc;
22777 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
22778 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
22779 Pgno pgnoRoot; /* Root page of the open btree cursor */
22780 i16 nField; /* Number of fields in the header */
22781 u16 nHdrParsed; /* Number of header fields parsed so far */
22782 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
22783 u32 *aOffset; /* Pointer to aType[nField] */
22784 const u8 *aRow; /* Data for the current row, if all on one page */
22785 u32 payloadSize; /* Total number of bytes in the record */
22786 u32 szRow; /* Byte available in aRow */
22787#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
22788 u64 maskUsed; /* Mask of columns used by this cursor */
22789#endif
22790 VdbeTxtBlbCache *pCache; /* Cache of large TEXT or BLOB values */
22791
22792 /* 2*nField extra array elements allocated for aType[], beyond the one
22793 ** static element declared in the structure. nField total array slots for
22794 ** aType[] and nField+1 array slots for aOffset[] */
22795 u32 aType[1]; /* Type values record decode. MUST BE LAST */
22796};
22797
22798/* Return true if P is a null-only cursor
22799*/
22800#define IsNullCursor(P) \
22801 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
22802
22803/*
22804** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
22805*/
22806#define CACHE_STALE 0
22807
22808/*
22809** Large TEXT or BLOB values can be slow to load, so we want to avoid
22810** loading them more than once. For that reason, large TEXT and BLOB values
22811** can be stored in a cache defined by this object, and attached to the
22812** VdbeCursor using the pCache field.
22813*/
22814struct VdbeTxtBlbCache {
22815 char *pCValue; /* A RCStr buffer to hold the value */
22816 i64 iOffset; /* File offset of the row being cached */
22817 int iCol; /* Column for which the cache is valid */
22818 u32 cacheStatus; /* Vdbe.cacheCtr value */
22819 u32 colCacheCtr; /* Column cache counter */
22820};
22821
22822/*
22823** When a sub-program is executed (OP_Program), a structure of this type
22824** is allocated to store the current value of the program counter, as
22825** well as the current memory cell array and various other frame specific
22826** values stored in the Vdbe struct. When the sub-program is finished,
22827** these values are copied back to the Vdbe from the VdbeFrame structure,
22828** restoring the state of the VM to as it was before the sub-program
22829** began executing.
22830**
22831** The memory for a VdbeFrame object is allocated and managed by a memory
22832** cell in the parent (calling) frame. When the memory cell is deleted or
22833** overwritten, the VdbeFrame object is not freed immediately. Instead, it
22834** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
22835** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
22836** this instead of deleting the VdbeFrame immediately is to avoid recursive
22837** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
22838** child frame are released.
22839**
22840** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
22841** set to NULL if the currently executing frame is the main program.
22842*/
22843typedef struct VdbeFrame VdbeFrame;
22844struct VdbeFrame {
22845 Vdbe *v; /* VM this frame belongs to */
22846 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
22847 Op *aOp; /* Program instructions for parent frame */
22848 Mem *aMem; /* Array of memory cells for parent frame */
22849 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
22850 u8 *aOnce; /* Bitmask used by OP_Once */
22851 void *token; /* Copy of SubProgram.token */
22852 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
22853 AuxData *pAuxData; /* Linked list of auxdata allocations */
22854#if SQLITE_DEBUG
22855 u32 iFrameMagic; /* magic number for sanity checking */
22856#endif
22857 int nCursor; /* Number of entries in apCsr */
22858 int pc; /* Program Counter in parent (calling) frame */
22859 int nOp; /* Size of aOp array */
22860 int nMem; /* Number of entries in aMem */
22861 int nChildMem; /* Number of memory cells for child frame */
22862 int nChildCsr; /* Number of cursors for child frame */
22863 i64 nChange; /* Statement changes (Vdbe.nChange) */
22864 i64 nDbChange; /* Value of db->nChange */
22865};
22866
22867/* Magic number for sanity checking on VdbeFrame objects */
22868#define SQLITE_FRAME_MAGIC 0x879fb71e
22869
22870/*
22871** Return a pointer to the array of registers allocated for use
22872** by a VdbeFrame.
22873*/
22874#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
22875
22876/*
22877** Internally, the vdbe manipulates nearly all SQL values as Mem
22878** structures. Each Mem struct may cache multiple representations (string,
22879** integer etc.) of the same value.
22880*/
22881struct sqlite3_value {
22882 union MemValue {
22883 double r; /* Real value used when MEM_Real is set in flags */
22884 i64 i; /* Integer value used when MEM_Int is set in flags */
22885 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
22886 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
22887 FuncDef *pDef; /* Used only when flags==MEM_Agg */
22888 } u;
22889 char *z; /* String or BLOB value */
22890 int n; /* Number of characters in string value, excluding '\0' */
22891 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
22892 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
22893 u8 eSubtype; /* Subtype for this value */
22894 /* ShallowCopy only needs to copy the information above */
22895 sqlite3 *db; /* The associated database connection */
22896 int szMalloc; /* Size of the zMalloc allocation */
22897 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
22898 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
22899 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
22900#ifdef SQLITE_DEBUG
22901 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
22902 u16 mScopyFlags; /* flags value immediately after the shallow copy */
22903#endif
22904};
22905
22906/*
22907** Size of struct Mem not including the Mem.zMalloc member or anything that
22908** follows.
22909*/
22910#define MEMCELLSIZE offsetof(Mem,db)
22911
22912/* One or more of the following flags are set to indicate the
22913** representations of the value stored in the Mem struct.
22914**
22915** * MEM_Null An SQL NULL value
22916**
22917** * MEM_Null|MEM_Zero An SQL NULL with the virtual table
22918** UPDATE no-change flag set
22919**
22920** * MEM_Null|MEM_Term| An SQL NULL, but also contains a
22921** MEM_Subtype pointer accessible using
22922** sqlite3_value_pointer().
22923**
22924** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
22925** to other NULLs even using the IS operator.
22926**
22927** * MEM_Str A string, stored in Mem.z with
22928** length Mem.n. Zero-terminated if
22929** MEM_Term is set. This flag is
22930** incompatible with MEM_Blob and
22931** MEM_Null, but can appear with MEM_Int,
22932** MEM_Real, and MEM_IntReal.
22933**
22934** * MEM_Blob A blob, stored in Mem.z length Mem.n.
22935** Incompatible with MEM_Str, MEM_Null,
22936** MEM_Int, MEM_Real, and MEM_IntReal.
22937**
22938** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus
22939** MEM.u.i extra 0x00 bytes at the end.
22940**
22941** * MEM_Int Integer stored in Mem.u.i.
22942**
22943** * MEM_Real Real stored in Mem.u.r.
22944**
22945** * MEM_IntReal Real stored as an integer in Mem.u.i.
22946**
22947** If the MEM_Null flag is set, then the value is an SQL NULL value.
22948** For a pointer type created using sqlite3_bind_pointer() or
22949** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
22950**
22951** If the MEM_Str flag is set then Mem.z points at a string representation.
22952** Usually this is encoded in the same unicode encoding as the main
22953** database (see below for exceptions). If the MEM_Term flag is also
22954** set, then the string is nul terminated. The MEM_Int and MEM_Real
22955** flags may coexist with the MEM_Str flag.
22956*/
22957#define MEM_Undefined 0x0000 /* Value is undefined */
22958#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */
22959#define MEM_Str 0x0002 /* Value is a string */
22960#define MEM_Int 0x0004 /* Value is an integer */
22961#define MEM_Real 0x0008 /* Value is a real number */
22962#define MEM_Blob 0x0010 /* Value is a BLOB */
22963#define MEM_IntReal 0x0020 /* MEM_Int that stringifies like MEM_Real */
22964#define MEM_AffMask 0x003f /* Mask of affinity bits */
22965
22966/* Extra bits that modify the meanings of the core datatypes above
22967*/
22968#define MEM_FromBind 0x0040 /* Value originates from sqlite3_bind() */
22969 /* 0x0080 // Available */
22970#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
22971#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */
22972#define MEM_Zero 0x0400 /* Mem.i contains count of 0s appended to blob */
22973#define MEM_Subtype 0x0800 /* Mem.eSubtype is valid */
22974#define MEM_TypeMask 0x0dbf /* Mask of type bits */
22975
22976/* Bits that determine the storage for Mem.z for a string or blob or
22977** aggregate accumulator.
22978*/
22979#define MEM_Dyn 0x1000 /* Need to call Mem.xDel() on Mem.z */
22980#define MEM_Static 0x2000 /* Mem.z points to a static string */
22981#define MEM_Ephem 0x4000 /* Mem.z points to an ephemeral string */
22982#define MEM_Agg 0x8000 /* Mem.z points to an agg function context */
22983
22984/* Return TRUE if Mem X contains dynamically allocated content - anything
22985** that needs to be deallocated to avoid a leak.
22986*/
22987#define VdbeMemDynamic(X) \
22988 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
22989
22990/*
22991** Clear any existing type flags from a Mem and replace them with f
22992*/
22993#define MemSetTypeFlag(p, f) \
22994 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
22995
22996/*
22997** True if Mem X is a NULL-nochng type.
22998*/
22999#define MemNullNochng(X) \
23000 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
23001 && (X)->n==0 && (X)->u.nZero==0)
23002
23003/*
23004** Return true if a memory cell has been initialized and is valid.
23005** is for use inside assert() statements only.
23006**
23007** A Memory cell is initialized if at least one of the
23008** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits
23009** is set. It is "undefined" if all those bits are zero.
23010*/
23011#ifdef SQLITE_DEBUG
23012#define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
23013#endif
23014
23015/*
23016** Each auxiliary data pointer stored by a user defined function
23017** implementation calling sqlite3_set_auxdata() is stored in an instance
23018** of this structure. All such structures associated with a single VM
23019** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
23020** when the VM is halted (if not before).
23021*/
23022struct AuxData {
23023 int iAuxOp; /* Instruction number of OP_Function opcode */
23024 int iAuxArg; /* Index of function argument. */
23025 void *pAux; /* Aux data pointer */
23026 void (*xDeleteAux)(void*); /* Destructor for the aux data */
23027 AuxData *pNextAux; /* Next element in list */
23028};
23029
23030/*
23031** The "context" argument for an installable function. A pointer to an
23032** instance of this structure is the first argument to the routines used
23033** implement the SQL functions.
23034**
23035** There is a typedef for this structure in sqlite.h. So all routines,
23036** even the public interface to SQLite, can use a pointer to this structure.
23037** But this file is the only place where the internal details of this
23038** structure are known.
23039**
23040** This structure is defined inside of vdbeInt.h because it uses substructures
23041** (Mem) which are only defined there.
23042*/
23043struct sqlite3_context {
23044 Mem *pOut; /* The return value is stored here */
23045 FuncDef *pFunc; /* Pointer to function information */
23046 Mem *pMem; /* Memory cell used to store aggregate context */
23047 Vdbe *pVdbe; /* The VM that owns this context */
23048 int iOp; /* Instruction number of OP_Function */
23049 int isError; /* Error code returned by the function. */
23050 u8 enc; /* Encoding to use for results */
23051 u8 skipFlag; /* Skip accumulator loading if true */
23052 u8 argc; /* Number of arguments */
23053 sqlite3_value *argv[1]; /* Argument set */
23054};
23055
23056/* A bitfield type for use inside of structures. Always follow with :N where
23057** N is the number of bits.
23058*/
23059typedef unsigned bft; /* Bit Field Type */
23060
23061/* The ScanStatus object holds a single value for the
23062** sqlite3_stmt_scanstatus() interface.
23063**
23064** aAddrRange[]:
23065** This array is used by ScanStatus elements associated with EQP
23066** notes that make an SQLITE_SCANSTAT_NCYCLE value available. It is
23067** an array of up to 3 ranges of VM addresses for which the Vdbe.anCycle[]
23068** values should be summed to calculate the NCYCLE value. Each pair of
23069** integer addresses is a start and end address (both inclusive) for a range
23070** instructions. A start value of 0 indicates an empty range.
23071*/
23072typedef struct ScanStatus ScanStatus;
23073struct ScanStatus {
23074 int addrExplain; /* OP_Explain for loop */
23075 int aAddrRange[6];
23076 int addrLoop; /* Address of "loops" counter */
23077 int addrVisit; /* Address of "rows visited" counter */
23078 int iSelectID; /* The "Select-ID" for this loop */
23079 LogEst nEst; /* Estimated output rows per loop */
23080 char *zName; /* Name of table or index */
23081};
23082
23083/* The DblquoteStr object holds the text of a double-quoted
23084** string for a prepared statement. A linked list of these objects
23085** is constructed during statement parsing and is held on Vdbe.pDblStr.
23086** When computing a normalized SQL statement for an SQL statement, that
23087** list is consulted for each double-quoted identifier to see if the
23088** identifier should really be a string literal.
23089*/
23090typedef struct DblquoteStr DblquoteStr;
23091struct DblquoteStr {
23092 DblquoteStr *pNextStr; /* Next string literal in the list */
23093 char z[8]; /* Dequoted value for the string */
23094};
23095
23096/*
23097** An instance of the virtual machine. This structure contains the complete
23098** state of the virtual machine.
23099**
23100** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
23101** is really a pointer to an instance of this structure.
23102*/
23103struct Vdbe {
23104 sqlite3 *db; /* The database connection that owns this statement */
23105 Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */
23106 Parse *pParse; /* Parsing context used to create this Vdbe */
23107 ynVar nVar; /* Number of entries in aVar[] */
23108 int nMem; /* Number of memory locations currently allocated */
23109 int nCursor; /* Number of slots in apCsr[] */
23110 u32 cacheCtr; /* VdbeCursor row cache generation counter */
23111 int pc; /* The program counter */
23112 int rc; /* Value to return */
23113 i64 nChange; /* Number of db changes made since last reset */
23114 int iStatement; /* Statement number (or 0 if has no opened stmt) */
23115 i64 iCurrentTime; /* Value of julianday('now') for this statement */
23116 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
23117 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
23118 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
23119 Mem *aMem; /* The memory locations */
23120 Mem **apArg; /* Arguments to currently executing user function */
23121 VdbeCursor **apCsr; /* One element of this array for each open cursor */
23122 Mem *aVar; /* Values for the OP_Variable opcode. */
23123
23124 /* When allocating a new Vdbe object, all of the fields below should be
23125 ** initialized to zero or NULL */
23126
23127 Op *aOp; /* Space to hold the virtual machine's program */
23128 int nOp; /* Number of instructions in the program */
23129 int nOpAlloc; /* Slots allocated for aOp[] */
23130 Mem *aColName; /* Column names to return */
23131 Mem *pResultRow; /* Current output row */
23132 char *zErrMsg; /* Error message written here */
23133 VList *pVList; /* Name of variables */
23134#ifndef SQLITE_OMIT_TRACE
23135 i64 startTime; /* Time when query started - used for profiling */
23136#endif
23137#ifdef SQLITE_DEBUG
23138 int rcApp; /* errcode set by sqlite3_result_error_code() */
23139 u32 nWrite; /* Number of write operations that have occurred */
23140#endif
23141 u16 nResColumn; /* Number of columns in one row of the result set */
23142 u16 nResAlloc; /* Column slots allocated to aColName[] */
23143 u8 errorAction; /* Recovery action to do in case of an error */
23144 u8 minWriteFileFormat; /* Minimum file format for writable database files */
23145 u8 prepFlags; /* SQLITE_PREPARE_* flags */
23146 u8 eVdbeState; /* On of the VDBE_*_STATE values */
23147 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
23148 bft explain:2; /* 0: normal, 1: EXPLAIN, 2: EXPLAIN QUERY PLAN */
23149 bft changeCntOn:1; /* True to update the change-counter */
23150 bft usesStmtJournal:1; /* True if uses a statement journal */
23151 bft readOnly:1; /* True for statements that do not write */
23152 bft bIsReader:1; /* True for statements that read */
23153 bft haveEqpOps:1; /* Bytecode supports EXPLAIN QUERY PLAN */
23154 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23155 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
23156 u32 aCounter[9]; /* Counters used by sqlite3_stmt_status() */
23157 char *zSql; /* Text of the SQL statement that generated this */
23158#ifdef SQLITE_ENABLE_NORMALIZE
23159 char *zNormSql; /* Normalization of the associated SQL statement */
23160 DblquoteStr *pDblStr; /* List of double-quoted string literals */
23161#endif
23162 void *pFree; /* Free this when deleting the vdbe */
23163 VdbeFrame *pFrame; /* Parent frame */
23164 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
23165 int nFrame; /* Number of frames in pFrame list */
23166 u32 expmask; /* Binding to these vars invalidates VM */
23167 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23168 AuxData *pAuxData; /* Linked list of auxdata allocations */
23169#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
23170 int nScan; /* Entries in aScan[] */
23171 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
23172#endif
23173};
23174
23175/*
23176** The following are allowed values for Vdbe.eVdbeState
23177*/
23178#define VDBE_INIT_STATE 0 /* Prepared statement under construction */
23179#define VDBE_READY_STATE 1 /* Ready to run but not yet started */
23180#define VDBE_RUN_STATE 2 /* Run in progress */
23181#define VDBE_HALT_STATE 3 /* Finished. Need reset() or finalize() */
23182
23183/*
23184** Structure used to store the context required by the
23185** sqlite3_preupdate_*() API functions.
23186*/
23187struct PreUpdate {
23188 Vdbe *v;
23189 VdbeCursor *pCsr; /* Cursor to read old values from */
23190 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
23191 u8 *aRecord; /* old.* database record */
23192 KeyInfo keyinfo;
23193 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
23194 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
23195 int iNewReg; /* Register for new.* values */
23196 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
23197 i64 iKey1; /* First key value passed to hook */
23198 i64 iKey2; /* Second key value passed to hook */
23199 Mem *aNew; /* Array of new.* values */
23200 Table *pTab; /* Schema object being updated */
23201 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
23202};
23203
23204/*
23205** An instance of this object is used to pass an vector of values into
23206** OP_VFilter, the xFilter method of a virtual table. The vector is the
23207** set of values on the right-hand side of an IN constraint.
23208**
23209** The value as passed into xFilter is an sqlite3_value with a "pointer"
23210** type, such as is generated by sqlite3_result_pointer() and read by
23211** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null
23212** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces
23213** know how to use this object to step through all the values in the
23214** right operand of the IN constraint.
23215*/
23216typedef struct ValueList ValueList;
23217struct ValueList {
23218 BtCursor *pCsr; /* An ephemeral table holding all values */
23219 sqlite3_value *pOut; /* Register to hold each decoded output value */
23220};
23221
23222/* Size of content associated with serial types that fit into a
23223** single-byte varint.
23224*/
23225#ifndef SQLITE_AMALGAMATION
23226SQLITE_PRIVATE const u8 sqlite3SmallTypeSizes[];
23227#endif
23228
23229/*
23230** Function prototypes
23231*/
23232SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
23233SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
23234SQLITE_PRIVATE void sqlite3VdbeFreeCursorNN(Vdbe*,VdbeCursor*);
23235void sqliteVdbePopStack(Vdbe*,int);
23236SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p);
23237SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*);
23238SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
23239SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
23240SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
23241#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
23242SQLITE_PRIVATE u64 sqlite3FloatSwap(u64 in);
23243# define swapMixedEndianFloat(X) X = sqlite3FloatSwap(X)
23244#else
23245# define swapMixedEndianFloat(X)
23246#endif
23247SQLITE_PRIVATE void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
23248SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
23249
23250int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
23251SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
23252SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
23253SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
23254#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
23255SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
23256SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
23257#endif
23258#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
23259SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
23260#endif
23261#if !defined(SQLITE_OMIT_EXPLAIN)
23262SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
23263#endif
23264SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
23265SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
23266SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
23267SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
23268SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
23269SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
23270SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
23271SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
23272SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
23273#ifdef SQLITE_OMIT_FLOATING_POINT
23274# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
23275#else
23276SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
23277#endif
23278SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
23279SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
23280SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
23281#ifndef SQLITE_OMIT_INCRBLOB
23282SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
23283#else
23284SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem*,int);
23285#endif
23286#ifdef SQLITE_DEBUG
23287SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
23288#endif
23289SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
23290SQLITE_PRIVATE void sqlite3VdbeMemZeroTerminateIfAble(Mem*);
23291SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
23292SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
23293SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
23294SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem*);
23295SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
23296SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
23297SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
23298SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
23299SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
23300SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
23301SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
23302SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
23303SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
23304SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
23305SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem*p);
23306SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
23307#ifndef SQLITE_OMIT_WINDOWFUNC
23308SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
23309#endif
23310#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
23311SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
23312#endif
23313SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
23314SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
23315SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
23316#ifdef SQLITE_DEBUG
23317SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
23318#endif
23319SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
23320SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
23321SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
23322#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
23323SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
23324 Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int);
23325#endif
23326SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
23327
23328SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
23329SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
23330SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
23331SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
23332SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
23333SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
23334SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
23335SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
23336
23337SQLITE_PRIVATE void sqlite3VdbeValueListFree(void*);
23338
23339#ifdef SQLITE_DEBUG
23340SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
23341SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
23342#else
23343# define sqlite3VdbeIncrWriteCounter(V,C)
23344# define sqlite3VdbeAssertAbortable(V)
23345#endif
23346
23347#if !defined(SQLITE_OMIT_SHARED_CACHE)
23348SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
23349#else
23350# define sqlite3VdbeEnter(X)
23351#endif
23352
23353#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
23354SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
23355#else
23356# define sqlite3VdbeLeave(X)
23357#endif
23358
23359#ifdef SQLITE_DEBUG
23360SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
23361SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
23362#endif
23363
23364#ifndef SQLITE_OMIT_FOREIGN_KEY
23365SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
23366#else
23367# define sqlite3VdbeCheckFk(p,i) 0
23368#endif
23369
23370#ifdef SQLITE_DEBUG
23371SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
23372SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
23373#endif
23374#ifndef SQLITE_OMIT_UTF16
23375SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
23376SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
23377#endif
23378
23379#ifndef SQLITE_OMIT_INCRBLOB
23380SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
23381 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23382#else
23383 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
23384 #define ExpandBlob(P) SQLITE_OK
23385#endif
23386
23387#endif /* !defined(SQLITE_VDBEINT_H) */
23388
23389/************** End of vdbeInt.h *********************************************/
23390/************** Continuing where we left off in status.c *********************/
23391
23392/*
23393** Variables in which to record status information.
23394*/
23395#if SQLITE_PTRSIZE>4
23396typedef sqlite3_int64 sqlite3StatValueType;
23397#else
23398typedef u32 sqlite3StatValueType;
23399#endif
23400typedef struct sqlite3StatType sqlite3StatType;
23401static SQLITE_WSD struct sqlite3StatType {
23402 sqlite3StatValueType nowValue[10]; /* Current value */
23403 sqlite3StatValueType mxValue[10]; /* Maximum value */
23404} sqlite3Stat = { {0,}, {0,} };
23405
23406/*
23407** Elements of sqlite3Stat[] are protected by either the memory allocator
23408** mutex, or by the pcache1 mutex. The following array determines which.
23409*/
23410static const char statMutex[] = {
23411 0, /* SQLITE_STATUS_MEMORY_USED */
23412 1, /* SQLITE_STATUS_PAGECACHE_USED */
23413 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
23414 0, /* SQLITE_STATUS_SCRATCH_USED */
23415 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
23416 0, /* SQLITE_STATUS_MALLOC_SIZE */
23417 0, /* SQLITE_STATUS_PARSER_STACK */
23418 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
23419 0, /* SQLITE_STATUS_SCRATCH_SIZE */
23420 0, /* SQLITE_STATUS_MALLOC_COUNT */
23421};
23422
23423
23424/* The "wsdStat" macro will resolve to the status information
23425** state vector. If writable static data is unsupported on the target,
23426** we have to locate the state vector at run-time. In the more common
23427** case where writable static data is supported, wsdStat can refer directly
23428** to the "sqlite3Stat" state vector declared above.
23429*/
23430#ifdef SQLITE_OMIT_WSD
23431# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
23432# define wsdStat x[0]
23433#else
23434# define wsdStatInit
23435# define wsdStat sqlite3Stat
23436#endif
23437
23438/*
23439** Return the current value of a status parameter. The caller must
23440** be holding the appropriate mutex.
23441*/
23442SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
23443 wsdStatInit;
23444 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
23445 assert( op>=0 && op<ArraySize(statMutex) );
23446 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
23447 : sqlite3MallocMutex()) );
23448 return wsdStat.nowValue[op];
23449}
23450
23451/*
23452** Add N to the value of a status record. The caller must hold the
23453** appropriate mutex. (Locking is checked by assert()).
23454**
23455** The StatusUp() routine can accept positive or negative values for N.
23456** The value of N is added to the current status value and the high-water
23457** mark is adjusted if necessary.
23458**
23459** The StatusDown() routine lowers the current value by N. The highwater
23460** mark is unchanged. N must be non-negative for StatusDown().
23461*/
23462SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
23463 wsdStatInit;
23464 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
23465 assert( op>=0 && op<ArraySize(statMutex) );
23466 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
23467 : sqlite3MallocMutex()) );
23468 wsdStat.nowValue[op] += N;
23469 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
23470 wsdStat.mxValue[op] = wsdStat.nowValue[op];
23471 }
23472}
23473SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
23474 wsdStatInit;
23475 assert( N>=0 );
23476 assert( op>=0 && op<ArraySize(statMutex) );
23477 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
23478 : sqlite3MallocMutex()) );
23479 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
23480 wsdStat.nowValue[op] -= N;
23481}
23482
23483/*
23484** Adjust the highwater mark if necessary.
23485** The caller must hold the appropriate mutex.
23486*/
23487SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
23488 sqlite3StatValueType newValue;
23489 wsdStatInit;
23490 assert( X>=0 );
23491 newValue = (sqlite3StatValueType)X;
23492 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
23493 assert( op>=0 && op<ArraySize(statMutex) );
23494 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
23495 : sqlite3MallocMutex()) );
23496 assert( op==SQLITE_STATUS_MALLOC_SIZE
23497 || op==SQLITE_STATUS_PAGECACHE_SIZE
23498 || op==SQLITE_STATUS_PARSER_STACK );
23499 if( newValue>wsdStat.mxValue[op] ){
23500 wsdStat.mxValue[op] = newValue;
23501 }
23502}
23503
23504/*
23505** Query status information.
23506*/
23507SQLITE_API int sqlite3_status64(
23508 int op,
23509 sqlite3_int64 *pCurrent,
23510 sqlite3_int64 *pHighwater,
23511 int resetFlag
23512){
23513 sqlite3_mutex *pMutex;
23514 wsdStatInit;
23515 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
23516 return SQLITE_MISUSE_BKPT;
23517 }
23518#ifdef SQLITE_ENABLE_API_ARMOR
23519 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
23520#endif
23521 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
23522 sqlite3_mutex_enter(pMutex);
23523 *pCurrent = wsdStat.nowValue[op];
23524 *pHighwater = wsdStat.mxValue[op];
23525 if( resetFlag ){
23526 wsdStat.mxValue[op] = wsdStat.nowValue[op];
23527 }
23528 sqlite3_mutex_leave(pMutex);
23529 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
23530 return SQLITE_OK;
23531}
23532SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
23533 sqlite3_int64 iCur = 0, iHwtr = 0;
23534 int rc;
23535#ifdef SQLITE_ENABLE_API_ARMOR
23536 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
23537#endif
23538 rc = sqlite3_status64(op, pCurrent: &iCur, pHighwater: &iHwtr, resetFlag);
23539 if( rc==0 ){
23540 *pCurrent = (int)iCur;
23541 *pHighwater = (int)iHwtr;
23542 }
23543 return rc;
23544}
23545
23546/*
23547** Return the number of LookasideSlot elements on the linked list
23548*/
23549static u32 countLookasideSlots(LookasideSlot *p){
23550 u32 cnt = 0;
23551 while( p ){
23552 p = p->pNext;
23553 cnt++;
23554 }
23555 return cnt;
23556}
23557
23558/*
23559** Count the number of slots of lookaside memory that are outstanding
23560*/
23561SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
23562 u32 nInit = countLookasideSlots(p: db->lookaside.pInit);
23563 u32 nFree = countLookasideSlots(p: db->lookaside.pFree);
23564#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
23565 nInit += countLookasideSlots(p: db->lookaside.pSmallInit);
23566 nFree += countLookasideSlots(p: db->lookaside.pSmallFree);
23567#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
23568 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
23569 return db->lookaside.nSlot - (nInit+nFree);
23570}
23571
23572/*
23573** Query status information for a single database connection
23574*/
23575SQLITE_API int sqlite3_db_status(
23576 sqlite3 *db, /* The database connection whose status is desired */
23577 int op, /* Status verb */
23578 int *pCurrent, /* Write current value here */
23579 int *pHighwater, /* Write high-water mark here */
23580 int resetFlag /* Reset high-water mark if true */
23581){
23582 int rc = SQLITE_OK; /* Return code */
23583#ifdef SQLITE_ENABLE_API_ARMOR
23584 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
23585 return SQLITE_MISUSE_BKPT;
23586 }
23587#endif
23588 sqlite3_mutex_enter(db->mutex);
23589 switch( op ){
23590 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
23591 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
23592 if( resetFlag ){
23593 LookasideSlot *p = db->lookaside.pFree;
23594 if( p ){
23595 while( p->pNext ) p = p->pNext;
23596 p->pNext = db->lookaside.pInit;
23597 db->lookaside.pInit = db->lookaside.pFree;
23598 db->lookaside.pFree = 0;
23599 }
23600#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
23601 p = db->lookaside.pSmallFree;
23602 if( p ){
23603 while( p->pNext ) p = p->pNext;
23604 p->pNext = db->lookaside.pSmallInit;
23605 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
23606 db->lookaside.pSmallFree = 0;
23607 }
23608#endif
23609 }
23610 break;
23611 }
23612
23613 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
23614 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
23615 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
23616 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
23617 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
23618 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
23619 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
23620 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
23621 *pCurrent = 0;
23622 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
23623 if( resetFlag ){
23624 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
23625 }
23626 break;
23627 }
23628
23629 /*
23630 ** Return an approximation for the amount of memory currently used
23631 ** by all pagers associated with the given database connection. The
23632 ** highwater mark is meaningless and is returned as zero.
23633 */
23634 case SQLITE_DBSTATUS_CACHE_USED_SHARED:
23635 case SQLITE_DBSTATUS_CACHE_USED: {
23636 int totalUsed = 0;
23637 int i;
23638 sqlite3BtreeEnterAll(db);
23639 for(i=0; i<db->nDb; i++){
23640 Btree *pBt = db->aDb[i].pBt;
23641 if( pBt ){
23642 Pager *pPager = sqlite3BtreePager(pBt);
23643 int nByte = sqlite3PagerMemUsed(pPager);
23644 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
23645 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
23646 }
23647 totalUsed += nByte;
23648 }
23649 }
23650 sqlite3BtreeLeaveAll(db);
23651 *pCurrent = totalUsed;
23652 *pHighwater = 0;
23653 break;
23654 }
23655
23656 /*
23657 ** *pCurrent gets an accurate estimate of the amount of memory used
23658 ** to store the schema for all databases (main, temp, and any ATTACHed
23659 ** databases. *pHighwater is set to zero.
23660 */
23661 case SQLITE_DBSTATUS_SCHEMA_USED: {
23662 int i; /* Used to iterate through schemas */
23663 int nByte = 0; /* Used to accumulate return value */
23664
23665 sqlite3BtreeEnterAll(db);
23666 db->pnBytesFreed = &nByte;
23667 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23668 db->lookaside.pEnd = db->lookaside.pStart;
23669 for(i=0; i<db->nDb; i++){
23670 Schema *pSchema = db->aDb[i].pSchema;
23671 if( ALWAYS(pSchema!=0) ){
23672 HashElem *p;
23673
23674 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
23675 pSchema->tblHash.count
23676 + pSchema->trigHash.count
23677 + pSchema->idxHash.count
23678 + pSchema->fkeyHash.count
23679 );
23680 nByte += sqlite3_msize(pSchema->tblHash.ht);
23681 nByte += sqlite3_msize(pSchema->trigHash.ht);
23682 nByte += sqlite3_msize(pSchema->idxHash.ht);
23683 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
23684
23685 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
23686 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
23687 }
23688 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
23689 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
23690 }
23691 }
23692 }
23693 db->pnBytesFreed = 0;
23694 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23695 sqlite3BtreeLeaveAll(db);
23696
23697 *pHighwater = 0;
23698 *pCurrent = nByte;
23699 break;
23700 }
23701
23702 /*
23703 ** *pCurrent gets an accurate estimate of the amount of memory used
23704 ** to store all prepared statements.
23705 ** *pHighwater is set to zero.
23706 */
23707 case SQLITE_DBSTATUS_STMT_USED: {
23708 struct Vdbe *pVdbe; /* Used to iterate through VMs */
23709 int nByte = 0; /* Used to accumulate return value */
23710
23711 db->pnBytesFreed = &nByte;
23712 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23713 db->lookaside.pEnd = db->lookaside.pStart;
23714 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
23715 sqlite3VdbeDelete(pVdbe);
23716 }
23717 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23718 db->pnBytesFreed = 0;
23719
23720 *pHighwater = 0; /* IMP: R-64479-57858 */
23721 *pCurrent = nByte;
23722
23723 break;
23724 }
23725
23726 /*
23727 ** Set *pCurrent to the total cache hits or misses encountered by all
23728 ** pagers the database handle is connected to. *pHighwater is always set
23729 ** to zero.
23730 */
23731 case SQLITE_DBSTATUS_CACHE_SPILL:
23732 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
23733 /* no break */ deliberate_fall_through
23734 case SQLITE_DBSTATUS_CACHE_HIT:
23735 case SQLITE_DBSTATUS_CACHE_MISS:
23736 case SQLITE_DBSTATUS_CACHE_WRITE:{
23737 int i;
23738 int nRet = 0;
23739 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
23740 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
23741
23742 for(i=0; i<db->nDb; i++){
23743 if( db->aDb[i].pBt ){
23744 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
23745 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
23746 }
23747 }
23748 *pHighwater = 0; /* IMP: R-42420-56072 */
23749 /* IMP: R-54100-20147 */
23750 /* IMP: R-29431-39229 */
23751 *pCurrent = nRet;
23752 break;
23753 }
23754
23755 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
23756 ** key constraints. Set *pCurrent to zero if all foreign key constraints
23757 ** have been satisfied. The *pHighwater is always set to zero.
23758 */
23759 case SQLITE_DBSTATUS_DEFERRED_FKS: {
23760 *pHighwater = 0; /* IMP: R-11967-56545 */
23761 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
23762 break;
23763 }
23764
23765 default: {
23766 rc = SQLITE_ERROR;
23767 }
23768 }
23769 sqlite3_mutex_leave(db->mutex);
23770 return rc;
23771}
23772
23773/************** End of status.c **********************************************/
23774/************** Begin file date.c ********************************************/
23775/*
23776** 2003 October 31
23777**
23778** The author disclaims copyright to this source code. In place of
23779** a legal notice, here is a blessing:
23780**
23781** May you do good and not evil.
23782** May you find forgiveness for yourself and forgive others.
23783** May you share freely, never taking more than you give.
23784**
23785*************************************************************************
23786** This file contains the C functions that implement date and time
23787** functions for SQLite.
23788**
23789** There is only one exported symbol in this file - the function
23790** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
23791** All other code has file scope.
23792**
23793** SQLite processes all times and dates as julian day numbers. The
23794** dates and times are stored as the number of days since noon
23795** in Greenwich on November 24, 4714 B.C. according to the Gregorian
23796** calendar system.
23797**
23798** 1970-01-01 00:00:00 is JD 2440587.5
23799** 2000-01-01 00:00:00 is JD 2451544.5
23800**
23801** This implementation requires years to be expressed as a 4-digit number
23802** which means that only dates between 0000-01-01 and 9999-12-31 can
23803** be represented, even though julian day numbers allow a much wider
23804** range of dates.
23805**
23806** The Gregorian calendar system is used for all dates and times,
23807** even those that predate the Gregorian calendar. Historians usually
23808** use the julian calendar for dates prior to 1582-10-15 and for some
23809** dates afterwards, depending on locale. Beware of this difference.
23810**
23811** The conversion algorithms are implemented based on descriptions
23812** in the following text:
23813**
23814** Jean Meeus
23815** Astronomical Algorithms, 2nd Edition, 1998
23816** ISBN 0-943396-61-1
23817** Willmann-Bell, Inc
23818** Richmond, Virginia (USA)
23819*/
23820/* #include "sqliteInt.h" */
23821/* #include <stdlib.h> */
23822/* #include <assert.h> */
23823#include <time.h>
23824
23825#ifndef SQLITE_OMIT_DATETIME_FUNCS
23826
23827/*
23828** The MSVC CRT on Windows CE may not have a localtime() function.
23829** So declare a substitute. The substitute function itself is
23830** defined in "os_win.c".
23831*/
23832#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
23833 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
23834struct tm *__cdecl localtime(const time_t *);
23835#endif
23836
23837/*
23838** A structure for holding a single date and time.
23839*/
23840typedef struct DateTime DateTime;
23841struct DateTime {
23842 sqlite3_int64 iJD; /* The julian day number times 86400000 */
23843 int Y, M, D; /* Year, month, and day */
23844 int h, m; /* Hour and minutes */
23845 int tz; /* Timezone offset in minutes */
23846 double s; /* Seconds */
23847 char validJD; /* True (1) if iJD is valid */
23848 char rawS; /* Raw numeric value stored in s */
23849 char validYMD; /* True (1) if Y,M,D are valid */
23850 char validHMS; /* True (1) if h,m,s are valid */
23851 char validTZ; /* True (1) if tz is valid */
23852 char tzSet; /* Timezone was set explicitly */
23853 char isError; /* An overflow has occurred */
23854 char useSubsec; /* Display subsecond precision */
23855};
23856
23857
23858/*
23859** Convert zDate into one or more integers according to the conversion
23860** specifier zFormat.
23861**
23862** zFormat[] contains 4 characters for each integer converted, except for
23863** the last integer which is specified by three characters. The meaning
23864** of a four-character format specifiers ABCD is:
23865**
23866** A: number of digits to convert. Always "2" or "4".
23867** B: minimum value. Always "0" or "1".
23868** C: maximum value, decoded as:
23869** a: 12
23870** b: 14
23871** c: 24
23872** d: 31
23873** e: 59
23874** f: 9999
23875** D: the separator character, or \000 to indicate this is the
23876** last number to convert.
23877**
23878** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
23879** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
23880** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
23881** the 2-digit day which is the last integer in the set.
23882**
23883** The function returns the number of successful conversions.
23884*/
23885static int getDigits(const char *zDate, const char *zFormat, ...){
23886 /* The aMx[] array translates the 3rd character of each format
23887 ** spec into a max size: a b c d e f */
23888 static const u16 aMx[] = { 12, 14, 24, 31, 59, 14712 };
23889 va_list ap;
23890 int cnt = 0;
23891 char nextC;
23892 va_start(ap, zFormat);
23893 do{
23894 char N = zFormat[0] - '0';
23895 char min = zFormat[1] - '0';
23896 int val = 0;
23897 u16 max;
23898
23899 assert( zFormat[2]>='a' && zFormat[2]<='f' );
23900 max = aMx[zFormat[2] - 'a'];
23901 nextC = zFormat[3];
23902 val = 0;
23903 while( N-- ){
23904 if( !sqlite3Isdigit(*zDate) ){
23905 goto end_getDigits;
23906 }
23907 val = val*10 + *zDate - '0';
23908 zDate++;
23909 }
23910 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
23911 goto end_getDigits;
23912 }
23913 *va_arg(ap,int*) = val;
23914 zDate++;
23915 cnt++;
23916 zFormat += 4;
23917 }while( nextC );
23918end_getDigits:
23919 va_end(ap);
23920 return cnt;
23921}
23922
23923/*
23924** Parse a timezone extension on the end of a date-time.
23925** The extension is of the form:
23926**
23927** (+/-)HH:MM
23928**
23929** Or the "zulu" notation:
23930**
23931** Z
23932**
23933** If the parse is successful, write the number of minutes
23934** of change in p->tz and return 0. If a parser error occurs,
23935** return non-zero.
23936**
23937** A missing specifier is not considered an error.
23938*/
23939static int parseTimezone(const char *zDate, DateTime *p){
23940 int sgn = 0;
23941 int nHr, nMn;
23942 int c;
23943 while( sqlite3Isspace(*zDate) ){ zDate++; }
23944 p->tz = 0;
23945 c = *zDate;
23946 if( c=='-' ){
23947 sgn = -1;
23948 }else if( c=='+' ){
23949 sgn = +1;
23950 }else if( c=='Z' || c=='z' ){
23951 zDate++;
23952 goto zulu_time;
23953 }else{
23954 return c!=0;
23955 }
23956 zDate++;
23957 if( getDigits(zDate, zFormat: "20b:20e", &nHr, &nMn)!=2 ){
23958 return 1;
23959 }
23960 zDate += 5;
23961 p->tz = sgn*(nMn + nHr*60);
23962zulu_time:
23963 while( sqlite3Isspace(*zDate) ){ zDate++; }
23964 p->tzSet = 1;
23965 return *zDate!=0;
23966}
23967
23968/*
23969** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
23970** The HH, MM, and SS must each be exactly 2 digits. The
23971** fractional seconds FFFF can be one or more digits.
23972**
23973** Return 1 if there is a parsing error and 0 on success.
23974*/
23975static int parseHhMmSs(const char *zDate, DateTime *p){
23976 int h, m, s;
23977 double ms = 0.0;
23978 if( getDigits(zDate, zFormat: "20c:20e", &h, &m)!=2 ){
23979 return 1;
23980 }
23981 zDate += 5;
23982 if( *zDate==':' ){
23983 zDate++;
23984 if( getDigits(zDate, zFormat: "20e", &s)!=1 ){
23985 return 1;
23986 }
23987 zDate += 2;
23988 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
23989 double rScale = 1.0;
23990 zDate++;
23991 while( sqlite3Isdigit(*zDate) ){
23992 ms = ms*10.0 + *zDate - '0';
23993 rScale *= 10.0;
23994 zDate++;
23995 }
23996 ms /= rScale;
23997 }
23998 }else{
23999 s = 0;
24000 }
24001 p->validJD = 0;
24002 p->rawS = 0;
24003 p->validHMS = 1;
24004 p->h = h;
24005 p->m = m;
24006 p->s = s + ms;
24007 if( parseTimezone(zDate, p) ) return 1;
24008 p->validTZ = (p->tz!=0)?1:0;
24009 return 0;
24010}
24011
24012/*
24013** Put the DateTime object into its error state.
24014*/
24015static void datetimeError(DateTime *p){
24016 memset(s: p, c: 0, n: sizeof(*p));
24017 p->isError = 1;
24018}
24019
24020/*
24021** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24022** that the YYYY-MM-DD is according to the Gregorian calendar.
24023**
24024** Reference: Meeus page 61
24025*/
24026static void computeJD(DateTime *p){
24027 int Y, M, D, A, B, X1, X2;
24028
24029 if( p->validJD ) return;
24030 if( p->validYMD ){
24031 Y = p->Y;
24032 M = p->M;
24033 D = p->D;
24034 }else{
24035 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
24036 M = 1;
24037 D = 1;
24038 }
24039 if( Y<-4713 || Y>9999 || p->rawS ){
24040 datetimeError(p);
24041 return;
24042 }
24043 if( M<=2 ){
24044 Y--;
24045 M += 12;
24046 }
24047 A = Y/100;
24048 B = 2 - A + (A/4);
24049 X1 = 36525*(Y+4716)/100;
24050 X2 = 306001*(M+1)/10000;
24051 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
24052 p->validJD = 1;
24053 if( p->validHMS ){
24054 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
24055 if( p->validTZ ){
24056 p->iJD -= p->tz*60000;
24057 p->validYMD = 0;
24058 p->validHMS = 0;
24059 p->validTZ = 0;
24060 }
24061 }
24062}
24063
24064/*
24065** Parse dates of the form
24066**
24067** YYYY-MM-DD HH:MM:SS.FFF
24068** YYYY-MM-DD HH:MM:SS
24069** YYYY-MM-DD HH:MM
24070** YYYY-MM-DD
24071**
24072** Write the result into the DateTime structure and return 0
24073** on success and 1 if the input string is not a well-formed
24074** date.
24075*/
24076static int parseYyyyMmDd(const char *zDate, DateTime *p){
24077 int Y, M, D, neg;
24078
24079 if( zDate[0]=='-' ){
24080 zDate++;
24081 neg = 1;
24082 }else{
24083 neg = 0;
24084 }
24085 if( getDigits(zDate, zFormat: "40f-21a-21d", &Y, &M, &D)!=3 ){
24086 return 1;
24087 }
24088 zDate += 10;
24089 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
24090 if( parseHhMmSs(zDate, p)==0 ){
24091 /* We got the time */
24092 }else if( *zDate==0 ){
24093 p->validHMS = 0;
24094 }else{
24095 return 1;
24096 }
24097 p->validJD = 0;
24098 p->validYMD = 1;
24099 p->Y = neg ? -Y : Y;
24100 p->M = M;
24101 p->D = D;
24102 if( p->validTZ ){
24103 computeJD(p);
24104 }
24105 return 0;
24106}
24107
24108/*
24109** Set the time to the current time reported by the VFS.
24110**
24111** Return the number of errors.
24112*/
24113static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
24114 p->iJD = sqlite3StmtCurrentTime(context);
24115 if( p->iJD>0 ){
24116 p->validJD = 1;
24117 return 0;
24118 }else{
24119 return 1;
24120 }
24121}
24122
24123/*
24124** Input "r" is a numeric quantity which might be a julian day number,
24125** or the number of seconds since 1970. If the value if r is within
24126** range of a julian day number, install it as such and set validJD.
24127** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
24128*/
24129static void setRawDateNumber(DateTime *p, double r){
24130 p->s = r;
24131 p->rawS = 1;
24132 if( r>=0.0 && r<5373484.5 ){
24133 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
24134 p->validJD = 1;
24135 }
24136}
24137
24138/*
24139** Attempt to parse the given string into a julian day number. Return
24140** the number of errors.
24141**
24142** The following are acceptable forms for the input string:
24143**
24144** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
24145** DDDD.DD
24146** now
24147**
24148** In the first form, the +/-HH:MM is always optional. The fractional
24149** seconds extension (the ".FFF") is optional. The seconds portion
24150** (":SS.FFF") is option. The year and date can be omitted as long
24151** as there is a time string. The time string can be omitted as long
24152** as there is a year and date.
24153*/
24154static int parseDateOrTime(
24155 sqlite3_context *context,
24156 const char *zDate,
24157 DateTime *p
24158){
24159 double r;
24160 if( parseYyyyMmDd(zDate,p)==0 ){
24161 return 0;
24162 }else if( parseHhMmSs(zDate, p)==0 ){
24163 return 0;
24164 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
24165 return setDateTimeToCurrent(context, p);
24166 }else if( sqlite3AtoF(z: zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8)>0 ){
24167 setRawDateNumber(p, r);
24168 return 0;
24169 }else if( (sqlite3StrICmp(zDate,"subsec")==0
24170 || sqlite3StrICmp(zDate,"subsecond")==0)
24171 && sqlite3NotPureFunc(context) ){
24172 p->useSubsec = 1;
24173 return setDateTimeToCurrent(context, p);
24174 }
24175 return 1;
24176}
24177
24178/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24179** Multiplying this by 86400000 gives 464269060799999 as the maximum value
24180** for DateTime.iJD.
24181**
24182** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
24183** such a large integer literal, so we have to encode it.
24184*/
24185#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff)
24186
24187/*
24188** Return TRUE if the given julian day number is within range.
24189**
24190** The input is the JulianDay times 86400000.
24191*/
24192static int validJulianDay(sqlite3_int64 iJD){
24193 return iJD>=0 && iJD<=INT_464269060799999;
24194}
24195
24196/*
24197** Compute the Year, Month, and Day from the julian day number.
24198*/
24199static void computeYMD(DateTime *p){
24200 int Z, A, B, C, D, E, X1;
24201 if( p->validYMD ) return;
24202 if( !p->validJD ){
24203 p->Y = 2000;
24204 p->M = 1;
24205 p->D = 1;
24206 }else if( !validJulianDay(iJD: p->iJD) ){
24207 datetimeError(p);
24208 return;
24209 }else{
24210 Z = (int)((p->iJD + 43200000)/86400000);
24211 A = (int)((Z - 1867216.25)/36524.25);
24212 A = Z + 1 + A - (A/4);
24213 B = A + 1524;
24214 C = (int)((B - 122.1)/365.25);
24215 D = (36525*(C&32767))/100;
24216 E = (int)((B-D)/30.6001);
24217 X1 = (int)(30.6001*E);
24218 p->D = B - D - X1;
24219 p->M = E<14 ? E-1 : E-13;
24220 p->Y = p->M>2 ? C - 4716 : C - 4715;
24221 }
24222 p->validYMD = 1;
24223}
24224
24225/*
24226** Compute the Hour, Minute, and Seconds from the julian day number.
24227*/
24228static void computeHMS(DateTime *p){
24229 int day_ms, day_min; /* milliseconds, minutes into the day */
24230 if( p->validHMS ) return;
24231 computeJD(p);
24232 day_ms = (int)((p->iJD + 43200000) % 86400000);
24233 p->s = (day_ms % 60000)/1000.0;
24234 day_min = day_ms/60000;
24235 p->m = day_min % 60;
24236 p->h = day_min / 60;
24237 p->rawS = 0;
24238 p->validHMS = 1;
24239}
24240
24241/*
24242** Compute both YMD and HMS
24243*/
24244static void computeYMD_HMS(DateTime *p){
24245 computeYMD(p);
24246 computeHMS(p);
24247}
24248
24249/*
24250** Clear the YMD and HMS and the TZ
24251*/
24252static void clearYMD_HMS_TZ(DateTime *p){
24253 p->validYMD = 0;
24254 p->validHMS = 0;
24255 p->validTZ = 0;
24256}
24257
24258#ifndef SQLITE_OMIT_LOCALTIME
24259/*
24260** On recent Windows platforms, the localtime_s() function is available
24261** as part of the "Secure CRT". It is essentially equivalent to
24262** localtime_r() available under most POSIX platforms, except that the
24263** order of the parameters is reversed.
24264**
24265** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24266**
24267** If the user has not indicated to use localtime_r() or localtime_s()
24268** already, check for an MSVC build environment that provides
24269** localtime_s().
24270*/
24271#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
24272 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
24273#undef HAVE_LOCALTIME_S
24274#define HAVE_LOCALTIME_S 1
24275#endif
24276
24277/*
24278** The following routine implements the rough equivalent of localtime_r()
24279** using whatever operating-system specific localtime facility that
24280** is available. This routine returns 0 on success and
24281** non-zero on any kind of error.
24282**
24283** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24284** routine will always fail. If bLocaltimeFault is nonzero and
24285** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is
24286** invoked in place of the OS-defined localtime() function.
24287**
24288** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24289** library function localtime_r() is used to assist in the calculation of
24290** local time.
24291*/
24292static int osLocaltime(time_t *t, struct tm *pTm){
24293 int rc;
24294#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
24295 struct tm *pX;
24296#if SQLITE_THREADSAFE>0
24297 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
24298#endif
24299 sqlite3_mutex_enter(mutex);
24300 pX = localtime(timer: t);
24301#ifndef SQLITE_UNTESTABLE
24302 if( sqlite3GlobalConfig.bLocaltimeFault ){
24303 if( sqlite3GlobalConfig.xAltLocaltime!=0
24304 && 0==sqlite3GlobalConfig.xAltLocaltime((const void*)t,(void*)pTm)
24305 ){
24306 pX = pTm;
24307 }else{
24308 pX = 0;
24309 }
24310 }
24311#endif
24312 if( pX ) *pTm = *pX;
24313#if SQLITE_THREADSAFE>0
24314 sqlite3_mutex_leave(mutex);
24315#endif
24316 rc = pX==0;
24317#else
24318#ifndef SQLITE_UNTESTABLE
24319 if( sqlite3GlobalConfig.bLocaltimeFault ){
24320 if( sqlite3GlobalConfig.xAltLocaltime!=0 ){
24321 return sqlite3GlobalConfig.xAltLocaltime((const void*)t,(void*)pTm);
24322 }else{
24323 return 1;
24324 }
24325 }
24326#endif
24327#if HAVE_LOCALTIME_R
24328 rc = localtime_r(t, pTm)==0;
24329#else
24330 rc = localtime_s(pTm, t);
24331#endif /* HAVE_LOCALTIME_R */
24332#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
24333 return rc;
24334}
24335#endif /* SQLITE_OMIT_LOCALTIME */
24336
24337
24338#ifndef SQLITE_OMIT_LOCALTIME
24339/*
24340** Assuming the input DateTime is UTC, move it to its localtime equivalent.
24341*/
24342static int toLocaltime(
24343 DateTime *p, /* Date at which to calculate offset */
24344 sqlite3_context *pCtx /* Write error here if one occurs */
24345){
24346 time_t t;
24347 struct tm sLocal;
24348 int iYearDiff;
24349
24350 /* Initialize the contents of sLocal to avoid a compiler warning. */
24351 memset(s: &sLocal, c: 0, n: sizeof(sLocal));
24352
24353 computeJD(p);
24354 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
24355 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
24356 ){
24357 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
24358 ** works for years between 1970 and 2037. For dates outside this range,
24359 ** SQLite attempts to map the year into an equivalent year within this
24360 ** range, do the calculation, then map the year back.
24361 */
24362 DateTime x = *p;
24363 computeYMD_HMS(p: &x);
24364 iYearDiff = (2000 + x.Y%4) - x.Y;
24365 x.Y += iYearDiff;
24366 x.validJD = 0;
24367 computeJD(p: &x);
24368 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
24369 }else{
24370 iYearDiff = 0;
24371 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
24372 }
24373 if( osLocaltime(t: &t, pTm: &sLocal) ){
24374 sqlite3_result_error(pCtx, "local time unavailable", -1);
24375 return SQLITE_ERROR;
24376 }
24377 p->Y = sLocal.tm_year + 1900 - iYearDiff;
24378 p->M = sLocal.tm_mon + 1;
24379 p->D = sLocal.tm_mday;
24380 p->h = sLocal.tm_hour;
24381 p->m = sLocal.tm_min;
24382 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
24383 p->validYMD = 1;
24384 p->validHMS = 1;
24385 p->validJD = 0;
24386 p->rawS = 0;
24387 p->validTZ = 0;
24388 p->isError = 0;
24389 return SQLITE_OK;
24390}
24391#endif /* SQLITE_OMIT_LOCALTIME */
24392
24393/*
24394** The following table defines various date transformations of the form
24395**
24396** 'NNN days'
24397**
24398** Where NNN is an arbitrary floating-point number and "days" can be one
24399** of several units of time.
24400*/
24401static const struct {
24402 u8 nName; /* Length of the name */
24403 char zName[7]; /* Name of the transformation */
24404 float rLimit; /* Maximum NNN value for this transform */
24405 float rXform; /* Constant used for this transform */
24406} aXformType[] = {
24407 { 6, "second", 4.6427e+14, 1.0 },
24408 { 6, "minute", 7.7379e+12, 60.0 },
24409 { 4, "hour", 1.2897e+11, 3600.0 },
24410 { 3, "day", 5373485.0, 86400.0 },
24411 { 5, "month", 176546.0, 2592000.0 },
24412 { 4, "year", 14713.0, 31536000.0 },
24413};
24414
24415/*
24416** If the DateTime p is raw number, try to figure out if it is
24417** a julian day number of a unix timestamp. Set the p value
24418** appropriately.
24419*/
24420static void autoAdjustDate(DateTime *p){
24421 if( !p->rawS || p->validJD ){
24422 p->rawS = 0;
24423 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
24424 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
24425 ){
24426 double r = p->s*1000.0 + 210866760000000.0;
24427 clearYMD_HMS_TZ(p);
24428 p->iJD = (sqlite3_int64)(r + 0.5);
24429 p->validJD = 1;
24430 p->rawS = 0;
24431 }
24432}
24433
24434/*
24435** Process a modifier to a date-time stamp. The modifiers are
24436** as follows:
24437**
24438** NNN days
24439** NNN hours
24440** NNN minutes
24441** NNN.NNNN seconds
24442** NNN months
24443** NNN years
24444** start of month
24445** start of year
24446** start of week
24447** start of day
24448** weekday N
24449** unixepoch
24450** localtime
24451** utc
24452**
24453** Return 0 on success and 1 if there is any kind of error. If the error
24454** is in a system call (i.e. localtime()), then an error message is written
24455** to context pCtx. If the error is an unrecognized modifier, no error is
24456** written to pCtx.
24457*/
24458static int parseModifier(
24459 sqlite3_context *pCtx, /* Function context */
24460 const char *z, /* The text of the modifier */
24461 int n, /* Length of zMod in bytes */
24462 DateTime *p, /* The date/time value to be modified */
24463 int idx /* Parameter index of the modifier */
24464){
24465 int rc = 1;
24466 double r;
24467 switch(sqlite3UpperToLower[(u8)z[0]] ){
24468 case 'a': {
24469 /*
24470 ** auto
24471 **
24472 ** If rawS is available, then interpret as a julian day number, or
24473 ** a unix timestamp, depending on its magnitude.
24474 */
24475 if( sqlite3_stricmp(z, "auto")==0 ){
24476 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
24477 autoAdjustDate(p);
24478 rc = 0;
24479 }
24480 break;
24481 }
24482 case 'j': {
24483 /*
24484 ** julianday
24485 **
24486 ** Always interpret the prior number as a julian-day value. If this
24487 ** is not the first modifier, or if the prior argument is not a numeric
24488 ** value in the allowed range of julian day numbers understood by
24489 ** SQLite (0..5373484.5) then the result will be NULL.
24490 */
24491 if( sqlite3_stricmp(z, "julianday")==0 ){
24492 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
24493 if( p->validJD && p->rawS ){
24494 rc = 0;
24495 p->rawS = 0;
24496 }
24497 }
24498 break;
24499 }
24500#ifndef SQLITE_OMIT_LOCALTIME
24501 case 'l': {
24502 /* localtime
24503 **
24504 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
24505 ** show local time.
24506 */
24507 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
24508 rc = toLocaltime(p, pCtx);
24509 }
24510 break;
24511 }
24512#endif
24513 case 'u': {
24514 /*
24515 ** unixepoch
24516 **
24517 ** Treat the current value of p->s as the number of
24518 ** seconds since 1970. Convert to a real julian day number.
24519 */
24520 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
24521 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
24522 r = p->s*1000.0 + 210866760000000.0;
24523 if( r>=0.0 && r<464269060800000.0 ){
24524 clearYMD_HMS_TZ(p);
24525 p->iJD = (sqlite3_int64)(r + 0.5);
24526 p->validJD = 1;
24527 p->rawS = 0;
24528 rc = 0;
24529 }
24530 }
24531#ifndef SQLITE_OMIT_LOCALTIME
24532 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
24533 if( p->tzSet==0 ){
24534 i64 iOrigJD; /* Original localtime */
24535 i64 iGuess; /* Guess at the corresponding utc time */
24536 int cnt = 0; /* Safety to prevent infinite loop */
24537 i64 iErr; /* Guess is off by this much */
24538
24539 computeJD(p);
24540 iGuess = iOrigJD = p->iJD;
24541 iErr = 0;
24542 do{
24543 DateTime new;
24544 memset(s: &new, c: 0, n: sizeof(new));
24545 iGuess -= iErr;
24546 new.iJD = iGuess;
24547 new.validJD = 1;
24548 rc = toLocaltime(p: &new, pCtx);
24549 if( rc ) return rc;
24550 computeJD(p: &new);
24551 iErr = new.iJD - iOrigJD;
24552 }while( iErr && cnt++<3 );
24553 memset(s: p, c: 0, n: sizeof(*p));
24554 p->iJD = iGuess;
24555 p->validJD = 1;
24556 p->tzSet = 1;
24557 }
24558 rc = SQLITE_OK;
24559 }
24560#endif
24561 break;
24562 }
24563 case 'w': {
24564 /*
24565 ** weekday N
24566 **
24567 ** Move the date to the same time on the next occurrence of
24568 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
24569 ** date is already on the appropriate weekday, this is a no-op.
24570 */
24571 if( sqlite3_strnicmp(z, "weekday ", 8)==0
24572 && sqlite3AtoF(z: &z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
24573 && r>=0.0 && r<7.0 && (n=(int)r)==r ){
24574 sqlite3_int64 Z;
24575 computeYMD_HMS(p);
24576 p->validTZ = 0;
24577 p->validJD = 0;
24578 computeJD(p);
24579 Z = ((p->iJD + 129600000)/86400000) % 7;
24580 if( Z>n ) Z -= 7;
24581 p->iJD += (n - Z)*86400000;
24582 clearYMD_HMS_TZ(p);
24583 rc = 0;
24584 }
24585 break;
24586 }
24587 case 's': {
24588 /*
24589 ** start of TTTTT
24590 **
24591 ** Move the date backwards to the beginning of the current day,
24592 ** or month or year.
24593 **
24594 ** subsecond
24595 ** subsec
24596 **
24597 ** Show subsecond precision in the output of datetime() and
24598 ** unixepoch() and strftime('%s').
24599 */
24600 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ){
24601 if( sqlite3_stricmp(z, "subsec")==0
24602 || sqlite3_stricmp(z, "subsecond")==0
24603 ){
24604 p->useSubsec = 1;
24605 rc = 0;
24606 }
24607 break;
24608 }
24609 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
24610 z += 9;
24611 computeYMD(p);
24612 p->validHMS = 1;
24613 p->h = p->m = 0;
24614 p->s = 0.0;
24615 p->rawS = 0;
24616 p->validTZ = 0;
24617 p->validJD = 0;
24618 if( sqlite3_stricmp(z,"month")==0 ){
24619 p->D = 1;
24620 rc = 0;
24621 }else if( sqlite3_stricmp(z,"year")==0 ){
24622 p->M = 1;
24623 p->D = 1;
24624 rc = 0;
24625 }else if( sqlite3_stricmp(z,"day")==0 ){
24626 rc = 0;
24627 }
24628 break;
24629 }
24630 case '+':
24631 case '-':
24632 case '0':
24633 case '1':
24634 case '2':
24635 case '3':
24636 case '4':
24637 case '5':
24638 case '6':
24639 case '7':
24640 case '8':
24641 case '9': {
24642 double rRounder;
24643 int i;
24644 int Y,M,D,h,m,x;
24645 const char *z2 = z;
24646 char z0 = z[0];
24647 for(n=1; z[n]; n++){
24648 if( z[n]==':' ) break;
24649 if( sqlite3Isspace(z[n]) ) break;
24650 if( z[n]=='-' ){
24651 if( n==5 && getDigits(zDate: &z[1], zFormat: "40f", &Y)==1 ) break;
24652 if( n==6 && getDigits(zDate: &z[1], zFormat: "50f", &Y)==1 ) break;
24653 }
24654 }
24655 if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){
24656 assert( rc==1 );
24657 break;
24658 }
24659 if( z[n]=='-' ){
24660 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the
24661 ** specified number of years, months, and days. MM is limited to
24662 ** the range 0-11 and DD is limited to 0-30.
24663 */
24664 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */
24665 if( n==5 ){
24666 if( getDigits(zDate: &z[1], zFormat: "40f-20a-20d", &Y, &M, &D)!=3 ) break;
24667 }else{
24668 assert( n==6 );
24669 if( getDigits(zDate: &z[1], zFormat: "50f-20a-20d", &Y, &M, &D)!=3 ) break;
24670 z++;
24671 }
24672 if( M>=12 ) break; /* M range 0..11 */
24673 if( D>=31 ) break; /* D range 0..30 */
24674 computeYMD_HMS(p);
24675 p->validJD = 0;
24676 if( z0=='-' ){
24677 p->Y -= Y;
24678 p->M -= M;
24679 D = -D;
24680 }else{
24681 p->Y += Y;
24682 p->M += M;
24683 }
24684 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
24685 p->Y += x;
24686 p->M -= x*12;
24687 computeJD(p);
24688 p->validHMS = 0;
24689 p->validYMD = 0;
24690 p->iJD += (i64)D*86400000;
24691 if( z[11]==0 ){
24692 rc = 0;
24693 break;
24694 }
24695 if( sqlite3Isspace(z[11])
24696 && getDigits(zDate: &z[12], zFormat: "20c:20e", &h, &m)==2
24697 ){
24698 z2 = &z[12];
24699 n = 2;
24700 }else{
24701 break;
24702 }
24703 }
24704 if( z2[n]==':' ){
24705 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
24706 ** specified number of hours, minutes, seconds, and fractional seconds
24707 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
24708 ** omitted.
24709 */
24710
24711 DateTime tx;
24712 sqlite3_int64 day;
24713 if( !sqlite3Isdigit(*z2) ) z2++;
24714 memset(s: &tx, c: 0, n: sizeof(tx));
24715 if( parseHhMmSs(zDate: z2, p: &tx) ) break;
24716 computeJD(p: &tx);
24717 tx.iJD -= 43200000;
24718 day = tx.iJD/86400000;
24719 tx.iJD -= day*86400000;
24720 if( z0=='-' ) tx.iJD = -tx.iJD;
24721 computeJD(p);
24722 clearYMD_HMS_TZ(p);
24723 p->iJD += tx.iJD;
24724 rc = 0;
24725 break;
24726 }
24727
24728 /* If control reaches this point, it means the transformation is
24729 ** one of the forms like "+NNN days". */
24730 z += n;
24731 while( sqlite3Isspace(*z) ) z++;
24732 n = sqlite3Strlen30(z);
24733 if( n>10 || n<3 ) break;
24734 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
24735 computeJD(p);
24736 assert( rc==1 );
24737 rRounder = r<0 ? -0.5 : +0.5;
24738 for(i=0; i<ArraySize(aXformType); i++){
24739 if( aXformType[i].nName==n
24740 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
24741 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
24742 ){
24743 switch( i ){
24744 case 4: { /* Special processing to add months */
24745 assert( strcmp(aXformType[i].zName,"month")==0 );
24746 computeYMD_HMS(p);
24747 p->M += (int)r;
24748 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
24749 p->Y += x;
24750 p->M -= x*12;
24751 p->validJD = 0;
24752 r -= (int)r;
24753 break;
24754 }
24755 case 5: { /* Special processing to add years */
24756 int y = (int)r;
24757 assert( strcmp(aXformType[i].zName,"year")==0 );
24758 computeYMD_HMS(p);
24759 p->Y += y;
24760 p->validJD = 0;
24761 r -= (int)r;
24762 break;
24763 }
24764 }
24765 computeJD(p);
24766 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
24767 rc = 0;
24768 break;
24769 }
24770 }
24771 clearYMD_HMS_TZ(p);
24772 break;
24773 }
24774 default: {
24775 break;
24776 }
24777 }
24778 return rc;
24779}
24780
24781/*
24782** Process time function arguments. argv[0] is a date-time stamp.
24783** argv[1] and following are modifiers. Parse them all and write
24784** the resulting time into the DateTime structure p. Return 0
24785** on success and 1 if there are any errors.
24786**
24787** If there are zero parameters (if even argv[0] is undefined)
24788** then assume a default value of "now" for argv[0].
24789*/
24790static int isDate(
24791 sqlite3_context *context,
24792 int argc,
24793 sqlite3_value **argv,
24794 DateTime *p
24795){
24796 int i, n;
24797 const unsigned char *z;
24798 int eType;
24799 memset(s: p, c: 0, n: sizeof(*p));
24800 if( argc==0 ){
24801 if( !sqlite3NotPureFunc(context) ) return 1;
24802 return setDateTimeToCurrent(context, p);
24803 }
24804 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
24805 || eType==SQLITE_INTEGER ){
24806 setRawDateNumber(p, r: sqlite3_value_double(argv[0]));
24807 }else{
24808 z = sqlite3_value_text(argv[0]);
24809 if( !z || parseDateOrTime(context, zDate: (char*)z, p) ){
24810 return 1;
24811 }
24812 }
24813 for(i=1; i<argc; i++){
24814 z = sqlite3_value_text(argv[i]);
24815 n = sqlite3_value_bytes(argv[i]);
24816 if( z==0 || parseModifier(pCtx: context, z: (char*)z, n, p, idx: i) ) return 1;
24817 }
24818 computeJD(p);
24819 if( p->isError || !validJulianDay(iJD: p->iJD) ) return 1;
24820 return 0;
24821}
24822
24823
24824/*
24825** The following routines implement the various date and time functions
24826** of SQLite.
24827*/
24828
24829/*
24830** julianday( TIMESTRING, MOD, MOD, ...)
24831**
24832** Return the julian day number of the date specified in the arguments
24833*/
24834static void juliandayFunc(
24835 sqlite3_context *context,
24836 int argc,
24837 sqlite3_value **argv
24838){
24839 DateTime x;
24840 if( isDate(context, argc, argv, p: &x)==0 ){
24841 computeJD(p: &x);
24842 sqlite3_result_double(context, x.iJD/86400000.0);
24843 }
24844}
24845
24846/*
24847** unixepoch( TIMESTRING, MOD, MOD, ...)
24848**
24849** Return the number of seconds (including fractional seconds) since
24850** the unix epoch of 1970-01-01 00:00:00 GMT.
24851*/
24852static void unixepochFunc(
24853 sqlite3_context *context,
24854 int argc,
24855 sqlite3_value **argv
24856){
24857 DateTime x;
24858 if( isDate(context, argc, argv, p: &x)==0 ){
24859 computeJD(p: &x);
24860 if( x.useSubsec ){
24861 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0);
24862 }else{
24863 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
24864 }
24865 }
24866}
24867
24868/*
24869** datetime( TIMESTRING, MOD, MOD, ...)
24870**
24871** Return YYYY-MM-DD HH:MM:SS
24872*/
24873static void datetimeFunc(
24874 sqlite3_context *context,
24875 int argc,
24876 sqlite3_value **argv
24877){
24878 DateTime x;
24879 if( isDate(context, argc, argv, p: &x)==0 ){
24880 int Y, s, n;
24881 char zBuf[32];
24882 computeYMD_HMS(p: &x);
24883 Y = x.Y;
24884 if( Y<0 ) Y = -Y;
24885 zBuf[1] = '0' + (Y/1000)%10;
24886 zBuf[2] = '0' + (Y/100)%10;
24887 zBuf[3] = '0' + (Y/10)%10;
24888 zBuf[4] = '0' + (Y)%10;
24889 zBuf[5] = '-';
24890 zBuf[6] = '0' + (x.M/10)%10;
24891 zBuf[7] = '0' + (x.M)%10;
24892 zBuf[8] = '-';
24893 zBuf[9] = '0' + (x.D/10)%10;
24894 zBuf[10] = '0' + (x.D)%10;
24895 zBuf[11] = ' ';
24896 zBuf[12] = '0' + (x.h/10)%10;
24897 zBuf[13] = '0' + (x.h)%10;
24898 zBuf[14] = ':';
24899 zBuf[15] = '0' + (x.m/10)%10;
24900 zBuf[16] = '0' + (x.m)%10;
24901 zBuf[17] = ':';
24902 if( x.useSubsec ){
24903 s = (int)(1000.0*x.s + 0.5);
24904 zBuf[18] = '0' + (s/10000)%10;
24905 zBuf[19] = '0' + (s/1000)%10;
24906 zBuf[20] = '.';
24907 zBuf[21] = '0' + (s/100)%10;
24908 zBuf[22] = '0' + (s/10)%10;
24909 zBuf[23] = '0' + (s)%10;
24910 zBuf[24] = 0;
24911 n = 24;
24912 }else{
24913 s = (int)x.s;
24914 zBuf[18] = '0' + (s/10)%10;
24915 zBuf[19] = '0' + (s)%10;
24916 zBuf[20] = 0;
24917 n = 20;
24918 }
24919 if( x.Y<0 ){
24920 zBuf[0] = '-';
24921 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT);
24922 }else{
24923 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT);
24924 }
24925 }
24926}
24927
24928/*
24929** time( TIMESTRING, MOD, MOD, ...)
24930**
24931** Return HH:MM:SS
24932*/
24933static void timeFunc(
24934 sqlite3_context *context,
24935 int argc,
24936 sqlite3_value **argv
24937){
24938 DateTime x;
24939 if( isDate(context, argc, argv, p: &x)==0 ){
24940 int s, n;
24941 char zBuf[16];
24942 computeHMS(p: &x);
24943 zBuf[0] = '0' + (x.h/10)%10;
24944 zBuf[1] = '0' + (x.h)%10;
24945 zBuf[2] = ':';
24946 zBuf[3] = '0' + (x.m/10)%10;
24947 zBuf[4] = '0' + (x.m)%10;
24948 zBuf[5] = ':';
24949 if( x.useSubsec ){
24950 s = (int)(1000.0*x.s + 0.5);
24951 zBuf[6] = '0' + (s/10000)%10;
24952 zBuf[7] = '0' + (s/1000)%10;
24953 zBuf[8] = '.';
24954 zBuf[9] = '0' + (s/100)%10;
24955 zBuf[10] = '0' + (s/10)%10;
24956 zBuf[11] = '0' + (s)%10;
24957 zBuf[12] = 0;
24958 n = 12;
24959 }else{
24960 s = (int)x.s;
24961 zBuf[6] = '0' + (s/10)%10;
24962 zBuf[7] = '0' + (s)%10;
24963 zBuf[8] = 0;
24964 n = 8;
24965 }
24966 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT);
24967 }
24968}
24969
24970/*
24971** date( TIMESTRING, MOD, MOD, ...)
24972**
24973** Return YYYY-MM-DD
24974*/
24975static void dateFunc(
24976 sqlite3_context *context,
24977 int argc,
24978 sqlite3_value **argv
24979){
24980 DateTime x;
24981 if( isDate(context, argc, argv, p: &x)==0 ){
24982 int Y;
24983 char zBuf[16];
24984 computeYMD(p: &x);
24985 Y = x.Y;
24986 if( Y<0 ) Y = -Y;
24987 zBuf[1] = '0' + (Y/1000)%10;
24988 zBuf[2] = '0' + (Y/100)%10;
24989 zBuf[3] = '0' + (Y/10)%10;
24990 zBuf[4] = '0' + (Y)%10;
24991 zBuf[5] = '-';
24992 zBuf[6] = '0' + (x.M/10)%10;
24993 zBuf[7] = '0' + (x.M)%10;
24994 zBuf[8] = '-';
24995 zBuf[9] = '0' + (x.D/10)%10;
24996 zBuf[10] = '0' + (x.D)%10;
24997 zBuf[11] = 0;
24998 if( x.Y<0 ){
24999 zBuf[0] = '-';
25000 sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT);
25001 }else{
25002 sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT);
25003 }
25004 }
25005}
25006
25007/*
25008** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
25009**
25010** Return a string described by FORMAT. Conversions as follows:
25011**
25012** %d day of month
25013** %f ** fractional seconds SS.SSS
25014** %H hour 00-24
25015** %j day of year 000-366
25016** %J ** julian day number
25017** %m month 01-12
25018** %M minute 00-59
25019** %s seconds since 1970-01-01
25020** %S seconds 00-59
25021** %w day of week 0-6 Sunday==0
25022** %W week of year 00-53
25023** %Y year 0000-9999
25024** %% %
25025*/
25026static void strftimeFunc(
25027 sqlite3_context *context,
25028 int argc,
25029 sqlite3_value **argv
25030){
25031 DateTime x;
25032 size_t i,j;
25033 sqlite3 *db;
25034 const char *zFmt;
25035 sqlite3_str sRes;
25036
25037
25038 if( argc==0 ) return;
25039 zFmt = (const char*)sqlite3_value_text(argv[0]);
25040 if( zFmt==0 || isDate(context, argc: argc-1, argv: argv+1, p: &x) ) return;
25041 db = sqlite3_context_db_handle(context);
25042 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
25043
25044 computeJD(p: &x);
25045 computeYMD_HMS(p: &x);
25046 for(i=j=0; zFmt[i]; i++){
25047 if( zFmt[i]!='%' ) continue;
25048 if( j<i ) sqlite3_str_append(&sRes, zIn: zFmt+j, N: (int)(i-j));
25049 i++;
25050 j = i + 1;
25051 switch( zFmt[i] ){
25052 case 'd': {
25053 sqlite3_str_appendf(&sRes, zFormat: "%02d", x.D);
25054 break;
25055 }
25056 case 'f': {
25057 double s = x.s;
25058 if( s>59.999 ) s = 59.999;
25059 sqlite3_str_appendf(&sRes, zFormat: "%06.3f", s);
25060 break;
25061 }
25062 case 'H': {
25063 sqlite3_str_appendf(&sRes, zFormat: "%02d", x.h);
25064 break;
25065 }
25066 case 'W': /* Fall thru */
25067 case 'j': {
25068 int nDay; /* Number of days since 1st day of year */
25069 DateTime y = x;
25070 y.validJD = 0;
25071 y.M = 1;
25072 y.D = 1;
25073 computeJD(p: &y);
25074 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
25075 if( zFmt[i]=='W' ){
25076 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
25077 wd = (int)(((x.iJD+43200000)/86400000)%7);
25078 sqlite3_str_appendf(&sRes,zFormat: "%02d",(nDay+7-wd)/7);
25079 }else{
25080 sqlite3_str_appendf(&sRes,zFormat: "%03d",nDay+1);
25081 }
25082 break;
25083 }
25084 case 'J': {
25085 sqlite3_str_appendf(&sRes,zFormat: "%.16g",x.iJD/86400000.0);
25086 break;
25087 }
25088 case 'm': {
25089 sqlite3_str_appendf(&sRes,zFormat: "%02d",x.M);
25090 break;
25091 }
25092 case 'M': {
25093 sqlite3_str_appendf(&sRes,zFormat: "%02d",x.m);
25094 break;
25095 }
25096 case 's': {
25097 if( x.useSubsec ){
25098 sqlite3_str_appendf(&sRes,zFormat: "%.3f",
25099 (x.iJD - 21086676*(i64)10000000)/1000.0);
25100 }else{
25101 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
25102 sqlite3_str_appendf(&sRes,zFormat: "%lld",iS);
25103 }
25104 break;
25105 }
25106 case 'S': {
25107 sqlite3_str_appendf(&sRes,zFormat: "%02d",(int)x.s);
25108 break;
25109 }
25110 case 'w': {
25111 sqlite3_str_appendchar(&sRes, N: 1,
25112 C: (char)(((x.iJD+129600000)/86400000) % 7) + '0');
25113 break;
25114 }
25115 case 'Y': {
25116 sqlite3_str_appendf(&sRes,zFormat: "%04d",x.Y);
25117 break;
25118 }
25119 case '%': {
25120 sqlite3_str_appendchar(&sRes, N: 1, C: '%');
25121 break;
25122 }
25123 default: {
25124 sqlite3_str_reset(&sRes);
25125 return;
25126 }
25127 }
25128 }
25129 if( j<i ) sqlite3_str_append(&sRes, zIn: zFmt+j, N: (int)(i-j));
25130 sqlite3ResultStrAccum(context, &sRes);
25131}
25132
25133/*
25134** current_time()
25135**
25136** This function returns the same value as time('now').
25137*/
25138static void ctimeFunc(
25139 sqlite3_context *context,
25140 int NotUsed,
25141 sqlite3_value **NotUsed2
25142){
25143 UNUSED_PARAMETER2(NotUsed, NotUsed2);
25144 timeFunc(context, argc: 0, argv: 0);
25145}
25146
25147/*
25148** current_date()
25149**
25150** This function returns the same value as date('now').
25151*/
25152static void cdateFunc(
25153 sqlite3_context *context,
25154 int NotUsed,
25155 sqlite3_value **NotUsed2
25156){
25157 UNUSED_PARAMETER2(NotUsed, NotUsed2);
25158 dateFunc(context, argc: 0, argv: 0);
25159}
25160
25161/*
25162** timediff(DATE1, DATE2)
25163**
25164** Return the amount of time that must be added to DATE2 in order to
25165** convert it into DATE2. The time difference format is:
25166**
25167** +YYYY-MM-DD HH:MM:SS.SSS
25168**
25169** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
25170** date/time values A and B, the following invariant should hold:
25171**
25172** datetime(A) == (datetime(B, timediff(A,B))
25173**
25174** Both DATE arguments must be either a julian day number, or an
25175** ISO-8601 string. The unix timestamps are not supported by this
25176** routine.
25177*/
25178static void timediffFunc(
25179 sqlite3_context *context,
25180 int NotUsed1,
25181 sqlite3_value **argv
25182){
25183 char sign;
25184 int Y, M;
25185 DateTime d1, d2;
25186 sqlite3_str sRes;
25187 UNUSED_PARAMETER(NotUsed1);
25188 if( isDate(context, argc: 1, argv: &argv[0], p: &d1) ) return;
25189 if( isDate(context, argc: 1, argv: &argv[1], p: &d2) ) return;
25190 computeYMD_HMS(p: &d1);
25191 computeYMD_HMS(p: &d2);
25192 if( d1.iJD>=d2.iJD ){
25193 sign = '+';
25194 Y = d1.Y - d2.Y;
25195 if( Y ){
25196 d2.Y = d1.Y;
25197 d2.validJD = 0;
25198 computeJD(p: &d2);
25199 }
25200 M = d1.M - d2.M;
25201 if( M<0 ){
25202 Y--;
25203 M += 12;
25204 }
25205 if( M!=0 ){
25206 d2.M = d1.M;
25207 d2.validJD = 0;
25208 computeJD(p: &d2);
25209 }
25210 while( d1.iJD<d2.iJD ){
25211 M--;
25212 if( M<0 ){
25213 M = 11;
25214 Y--;
25215 }
25216 d2.M--;
25217 if( d2.M<1 ){
25218 d2.M = 12;
25219 d2.Y--;
25220 }
25221 d2.validJD = 0;
25222 computeJD(p: &d2);
25223 }
25224 d1.iJD -= d2.iJD;
25225 d1.iJD += (u64)1486995408 * (u64)100000;
25226 }else /* d1<d2 */{
25227 sign = '-';
25228 Y = d2.Y - d1.Y;
25229 if( Y ){
25230 d2.Y = d1.Y;
25231 d2.validJD = 0;
25232 computeJD(p: &d2);
25233 }
25234 M = d2.M - d1.M;
25235 if( M<0 ){
25236 Y--;
25237 M += 12;
25238 }
25239 if( M!=0 ){
25240 d2.M = d1.M;
25241 d2.validJD = 0;
25242 computeJD(p: &d2);
25243 }
25244 while( d1.iJD>d2.iJD ){
25245 M--;
25246 if( M<0 ){
25247 M = 11;
25248 Y--;
25249 }
25250 d2.M++;
25251 if( d2.M>12 ){
25252 d2.M = 1;
25253 d2.Y++;
25254 }
25255 d2.validJD = 0;
25256 computeJD(p: &d2);
25257 }
25258 d1.iJD = d2.iJD - d1.iJD;
25259 d1.iJD += (u64)1486995408 * (u64)100000;
25260 }
25261 d1.validYMD = 0;
25262 d1.validHMS = 0;
25263 d1.validTZ = 0;
25264 computeYMD_HMS(p: &d1);
25265 sqlite3StrAccumInit(&sRes, 0, 0, 0, 100);
25266 sqlite3_str_appendf(&sRes, zFormat: "%c%04d-%02d-%02d %02d:%02d:%06.3f",
25267 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s);
25268 sqlite3ResultStrAccum(context, &sRes);
25269}
25270
25271
25272/*
25273** current_timestamp()
25274**
25275** This function returns the same value as datetime('now').
25276*/
25277static void ctimestampFunc(
25278 sqlite3_context *context,
25279 int NotUsed,
25280 sqlite3_value **NotUsed2
25281){
25282 UNUSED_PARAMETER2(NotUsed, NotUsed2);
25283 datetimeFunc(context, argc: 0, argv: 0);
25284}
25285#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
25286
25287#ifdef SQLITE_OMIT_DATETIME_FUNCS
25288/*
25289** If the library is compiled to omit the full-scale date and time
25290** handling (to get a smaller binary), the following minimal version
25291** of the functions current_time(), current_date() and current_timestamp()
25292** are included instead. This is to support column declarations that
25293** include "DEFAULT CURRENT_TIME" etc.
25294**
25295** This function uses the C-library functions time(), gmtime()
25296** and strftime(). The format string to pass to strftime() is supplied
25297** as the user-data for the function.
25298*/
25299static void currentTimeFunc(
25300 sqlite3_context *context,
25301 int argc,
25302 sqlite3_value **argv
25303){
25304 time_t t;
25305 char *zFormat = (char *)sqlite3_user_data(context);
25306 sqlite3_int64 iT;
25307 struct tm *pTm;
25308 struct tm sNow;
25309 char zBuf[20];
25310
25311 UNUSED_PARAMETER(argc);
25312 UNUSED_PARAMETER(argv);
25313
25314 iT = sqlite3StmtCurrentTime(context);
25315 if( iT<=0 ) return;
25316 t = iT/1000 - 10000*(sqlite3_int64)21086676;
25317#if HAVE_GMTIME_R
25318 pTm = gmtime_r(&t, &sNow);
25319#else
25320 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
25321 pTm = gmtime(&t);
25322 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
25323 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
25324#endif
25325 if( pTm ){
25326 strftime(zBuf, 20, zFormat, &sNow);
25327 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
25328 }
25329}
25330#endif
25331
25332/*
25333** This function registered all of the above C functions as SQL
25334** functions. This should be the only routine in this file with
25335** external linkage.
25336*/
25337SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
25338 static FuncDef aDateTimeFuncs[] = {
25339#ifndef SQLITE_OMIT_DATETIME_FUNCS
25340 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
25341 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ),
25342 PURE_DATE(date, -1, 0, 0, dateFunc ),
25343 PURE_DATE(time, -1, 0, 0, timeFunc ),
25344 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
25345 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
25346 PURE_DATE(timediff, 2, 0, 0, timediffFunc ),
25347 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
25348 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
25349 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
25350#else
25351 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
25352 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
25353 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
25354#endif
25355 };
25356 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
25357}
25358
25359/************** End of date.c ************************************************/
25360/************** Begin file os.c **********************************************/
25361/*
25362** 2005 November 29
25363**
25364** The author disclaims copyright to this source code. In place of
25365** a legal notice, here is a blessing:
25366**
25367** May you do good and not evil.
25368** May you find forgiveness for yourself and forgive others.
25369** May you share freely, never taking more than you give.
25370**
25371******************************************************************************
25372**
25373** This file contains OS interface code that is common to all
25374** architectures.
25375*/
25376/* #include "sqliteInt.h" */
25377
25378/*
25379** If we compile with the SQLITE_TEST macro set, then the following block
25380** of code will give us the ability to simulate a disk I/O error. This
25381** is used for testing the I/O recovery logic.
25382*/
25383#if defined(SQLITE_TEST)
25384SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
25385SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
25386SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
25387SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
25388SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
25389SQLITE_API int sqlite3_diskfull_pending = 0;
25390SQLITE_API int sqlite3_diskfull = 0;
25391#endif /* defined(SQLITE_TEST) */
25392
25393/*
25394** When testing, also keep a count of the number of open files.
25395*/
25396#if defined(SQLITE_TEST)
25397SQLITE_API int sqlite3_open_file_count = 0;
25398#endif /* defined(SQLITE_TEST) */
25399
25400/*
25401** The default SQLite sqlite3_vfs implementations do not allocate
25402** memory (actually, os_unix.c allocates a small amount of memory
25403** from within OsOpen()), but some third-party implementations may.
25404** So we test the effects of a malloc() failing and the sqlite3OsXXX()
25405** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
25406**
25407** The following functions are instrumented for malloc() failure
25408** testing:
25409**
25410** sqlite3OsRead()
25411** sqlite3OsWrite()
25412** sqlite3OsSync()
25413** sqlite3OsFileSize()
25414** sqlite3OsLock()
25415** sqlite3OsCheckReservedLock()
25416** sqlite3OsFileControl()
25417** sqlite3OsShmMap()
25418** sqlite3OsOpen()
25419** sqlite3OsDelete()
25420** sqlite3OsAccess()
25421** sqlite3OsFullPathname()
25422**
25423*/
25424#if defined(SQLITE_TEST)
25425SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
25426 #define DO_OS_MALLOC_TEST(x) \
25427 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
25428 void *pTstAlloc = sqlite3Malloc(10); \
25429 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \
25430 sqlite3_free(pTstAlloc); \
25431 }
25432#else
25433 #define DO_OS_MALLOC_TEST(x)
25434#endif
25435
25436/*
25437** The following routines are convenience wrappers around methods
25438** of the sqlite3_file object. This is mostly just syntactic sugar. All
25439** of this would be completely automatic if SQLite were coded using
25440** C++ instead of plain old C.
25441*/
25442SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
25443 if( pId->pMethods ){
25444 pId->pMethods->xClose(pId);
25445 pId->pMethods = 0;
25446 }
25447}
25448SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
25449 DO_OS_MALLOC_TEST(id);
25450 return id->pMethods->xRead(id, pBuf, amt, offset);
25451}
25452SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
25453 DO_OS_MALLOC_TEST(id);
25454 return id->pMethods->xWrite(id, pBuf, amt, offset);
25455}
25456SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
25457 return id->pMethods->xTruncate(id, size);
25458}
25459SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
25460 DO_OS_MALLOC_TEST(id);
25461 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
25462}
25463SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
25464 DO_OS_MALLOC_TEST(id);
25465 return id->pMethods->xFileSize(id, pSize);
25466}
25467SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
25468 DO_OS_MALLOC_TEST(id);
25469 assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE );
25470 return id->pMethods->xLock(id, lockType);
25471}
25472SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
25473 assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED );
25474 return id->pMethods->xUnlock(id, lockType);
25475}
25476SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
25477 DO_OS_MALLOC_TEST(id);
25478 return id->pMethods->xCheckReservedLock(id, pResOut);
25479}
25480
25481/*
25482** Use sqlite3OsFileControl() when we are doing something that might fail
25483** and we need to know about the failures. Use sqlite3OsFileControlHint()
25484** when simply tossing information over the wall to the VFS and we do not
25485** really care if the VFS receives and understands the information since it
25486** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
25487** routine has no return value since the return value would be meaningless.
25488*/
25489SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
25490 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
25491#ifdef SQLITE_TEST
25492 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
25493 && op!=SQLITE_FCNTL_LOCK_TIMEOUT
25494 && op!=SQLITE_FCNTL_CKPT_DONE
25495 && op!=SQLITE_FCNTL_CKPT_START
25496 ){
25497 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
25498 ** is using a regular VFS, it is called after the corresponding
25499 ** transaction has been committed. Injecting a fault at this point
25500 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM
25501 ** but the transaction is committed anyway.
25502 **
25503 ** The core must call OsFileControl() though, not OsFileControlHint(),
25504 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
25505 ** means the commit really has failed and an error should be returned
25506 ** to the user.
25507 **
25508 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
25509 ** to the cksumvfs. Their return code is meaningless and is ignored
25510 ** by the SQLite core, so there is no point in simulating OOMs for them.
25511 */
25512 DO_OS_MALLOC_TEST(id);
25513 }
25514#endif
25515 return id->pMethods->xFileControl(id, op, pArg);
25516}
25517SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
25518 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
25519}
25520
25521SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
25522 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
25523 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
25524}
25525SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
25526 if( NEVER(id->pMethods==0) ) return 0;
25527 return id->pMethods->xDeviceCharacteristics(id);
25528}
25529#ifndef SQLITE_OMIT_WAL
25530SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
25531 return id->pMethods->xShmLock(id, offset, n, flags);
25532}
25533SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
25534 id->pMethods->xShmBarrier(id);
25535}
25536SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
25537 return id->pMethods->xShmUnmap(id, deleteFlag);
25538}
25539SQLITE_PRIVATE int sqlite3OsShmMap(
25540 sqlite3_file *id, /* Database file handle */
25541 int iPage,
25542 int pgsz,
25543 int bExtend, /* True to extend file if necessary */
25544 void volatile **pp /* OUT: Pointer to mapping */
25545){
25546 DO_OS_MALLOC_TEST(id);
25547 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
25548}
25549#endif /* SQLITE_OMIT_WAL */
25550
25551#if SQLITE_MAX_MMAP_SIZE>0
25552/* The real implementation of xFetch and xUnfetch */
25553SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
25554 DO_OS_MALLOC_TEST(id);
25555 return id->pMethods->xFetch(id, iOff, iAmt, pp);
25556}
25557SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
25558 return id->pMethods->xUnfetch(id, iOff, p);
25559}
25560#else
25561/* No-op stubs to use when memory-mapped I/O is disabled */
25562SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
25563 *pp = 0;
25564 return SQLITE_OK;
25565}
25566SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
25567 return SQLITE_OK;
25568}
25569#endif
25570
25571/*
25572** The next group of routines are convenience wrappers around the
25573** VFS methods.
25574*/
25575SQLITE_PRIVATE int sqlite3OsOpen(
25576 sqlite3_vfs *pVfs,
25577 const char *zPath,
25578 sqlite3_file *pFile,
25579 int flags,
25580 int *pFlagsOut
25581){
25582 int rc;
25583 DO_OS_MALLOC_TEST(0);
25584 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
25585 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
25586 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
25587 ** reaching the VFS. */
25588 assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) );
25589 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
25590 assert( rc==SQLITE_OK || pFile->pMethods==0 );
25591 return rc;
25592}
25593SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
25594 DO_OS_MALLOC_TEST(0);
25595 assert( dirSync==0 || dirSync==1 );
25596 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK;
25597}
25598SQLITE_PRIVATE int sqlite3OsAccess(
25599 sqlite3_vfs *pVfs,
25600 const char *zPath,
25601 int flags,
25602 int *pResOut
25603){
25604 DO_OS_MALLOC_TEST(0);
25605 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
25606}
25607SQLITE_PRIVATE int sqlite3OsFullPathname(
25608 sqlite3_vfs *pVfs,
25609 const char *zPath,
25610 int nPathOut,
25611 char *zPathOut
25612){
25613 DO_OS_MALLOC_TEST(0);
25614 zPathOut[0] = 0;
25615 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
25616}
25617#ifndef SQLITE_OMIT_LOAD_EXTENSION
25618SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
25619 assert( zPath!=0 );
25620 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */
25621 return pVfs->xDlOpen(pVfs, zPath);
25622}
25623SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
25624 pVfs->xDlError(pVfs, nByte, zBufOut);
25625}
25626SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
25627 return pVfs->xDlSym(pVfs, pHdle, zSym);
25628}
25629SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
25630 pVfs->xDlClose(pVfs, pHandle);
25631}
25632#endif /* SQLITE_OMIT_LOAD_EXTENSION */
25633SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
25634 if( sqlite3Config.iPrngSeed ){
25635 memset(s: zBufOut, c: 0, n: nByte);
25636 if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
25637 memcpy(dest: zBufOut, src: &sqlite3Config.iPrngSeed, n: nByte);
25638 return SQLITE_OK;
25639 }else{
25640 return pVfs->xRandomness(pVfs, nByte, zBufOut);
25641 }
25642
25643}
25644SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
25645 return pVfs->xSleep(pVfs, nMicro);
25646}
25647SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
25648 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
25649}
25650SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
25651 int rc;
25652 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
25653 ** method to get the current date and time if that method is available
25654 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
25655 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
25656 ** unavailable.
25657 */
25658 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
25659 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
25660 }else{
25661 double r;
25662 rc = pVfs->xCurrentTime(pVfs, &r);
25663 *pTimeOut = (sqlite3_int64)(r*86400000.0);
25664 }
25665 return rc;
25666}
25667
25668SQLITE_PRIVATE int sqlite3OsOpenMalloc(
25669 sqlite3_vfs *pVfs,
25670 const char *zFile,
25671 sqlite3_file **ppFile,
25672 int flags,
25673 int *pOutFlags
25674){
25675 int rc;
25676 sqlite3_file *pFile;
25677 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
25678 if( pFile ){
25679 rc = sqlite3OsOpen(pVfs, zPath: zFile, pFile, flags, pFlagsOut: pOutFlags);
25680 if( rc!=SQLITE_OK ){
25681 sqlite3_free(pFile);
25682 *ppFile = 0;
25683 }else{
25684 *ppFile = pFile;
25685 }
25686 }else{
25687 *ppFile = 0;
25688 rc = SQLITE_NOMEM_BKPT;
25689 }
25690 assert( *ppFile!=0 || rc!=SQLITE_OK );
25691 return rc;
25692}
25693SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
25694 assert( pFile );
25695 sqlite3OsClose(pId: pFile);
25696 sqlite3_free(pFile);
25697}
25698
25699/*
25700** This function is a wrapper around the OS specific implementation of
25701** sqlite3_os_init(). The purpose of the wrapper is to provide the
25702** ability to simulate a malloc failure, so that the handling of an
25703** error in sqlite3_os_init() by the upper layers can be tested.
25704*/
25705SQLITE_PRIVATE int sqlite3OsInit(void){
25706 void *p = sqlite3_malloc(10);
25707 if( p==0 ) return SQLITE_NOMEM_BKPT;
25708 sqlite3_free(p);
25709 return sqlite3_os_init();
25710}
25711
25712/*
25713** The list of all registered VFS implementations.
25714*/
25715static sqlite3_vfs * SQLITE_WSD vfsList = 0;
25716#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
25717
25718/*
25719** Locate a VFS by name. If no name is given, simply return the
25720** first VFS on the list.
25721*/
25722SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
25723 sqlite3_vfs *pVfs = 0;
25724#if SQLITE_THREADSAFE
25725 sqlite3_mutex *mutex;
25726#endif
25727#ifndef SQLITE_OMIT_AUTOINIT
25728 int rc = sqlite3_initialize();
25729 if( rc ) return 0;
25730#endif
25731#if SQLITE_THREADSAFE
25732 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
25733#endif
25734 sqlite3_mutex_enter(mutex);
25735 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
25736 if( zVfs==0 ) break;
25737 if( strcmp(s1: zVfs, s2: pVfs->zName)==0 ) break;
25738 }
25739 sqlite3_mutex_leave(mutex);
25740 return pVfs;
25741}
25742
25743/*
25744** Unlink a VFS from the linked list
25745*/
25746static void vfsUnlink(sqlite3_vfs *pVfs){
25747 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) );
25748 if( pVfs==0 ){
25749 /* No-op */
25750 }else if( vfsList==pVfs ){
25751 vfsList = pVfs->pNext;
25752 }else if( vfsList ){
25753 sqlite3_vfs *p = vfsList;
25754 while( p->pNext && p->pNext!=pVfs ){
25755 p = p->pNext;
25756 }
25757 if( p->pNext==pVfs ){
25758 p->pNext = pVfs->pNext;
25759 }
25760 }
25761}
25762
25763/*
25764** Register a VFS with the system. It is harmless to register the same
25765** VFS multiple times. The new VFS becomes the default if makeDflt is
25766** true.
25767*/
25768SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
25769 MUTEX_LOGIC(sqlite3_mutex *mutex;)
25770#ifndef SQLITE_OMIT_AUTOINIT
25771 int rc = sqlite3_initialize();
25772 if( rc ) return rc;
25773#endif
25774#ifdef SQLITE_ENABLE_API_ARMOR
25775 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
25776#endif
25777
25778 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
25779 sqlite3_mutex_enter(mutex);
25780 vfsUnlink(pVfs);
25781 if( makeDflt || vfsList==0 ){
25782 pVfs->pNext = vfsList;
25783 vfsList = pVfs;
25784 }else{
25785 pVfs->pNext = vfsList->pNext;
25786 vfsList->pNext = pVfs;
25787 }
25788 assert(vfsList);
25789 sqlite3_mutex_leave(mutex);
25790 return SQLITE_OK;
25791}
25792
25793/*
25794** Unregister a VFS so that it is no longer accessible.
25795*/
25796SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
25797 MUTEX_LOGIC(sqlite3_mutex *mutex;)
25798#ifndef SQLITE_OMIT_AUTOINIT
25799 int rc = sqlite3_initialize();
25800 if( rc ) return rc;
25801#endif
25802 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
25803 sqlite3_mutex_enter(mutex);
25804 vfsUnlink(pVfs);
25805 sqlite3_mutex_leave(mutex);
25806 return SQLITE_OK;
25807}
25808
25809/************** End of os.c **************************************************/
25810/************** Begin file fault.c *******************************************/
25811/*
25812** 2008 Jan 22
25813**
25814** The author disclaims copyright to this source code. In place of
25815** a legal notice, here is a blessing:
25816**
25817** May you do good and not evil.
25818** May you find forgiveness for yourself and forgive others.
25819** May you share freely, never taking more than you give.
25820**
25821*************************************************************************
25822**
25823** This file contains code to support the concept of "benign"
25824** malloc failures (when the xMalloc() or xRealloc() method of the
25825** sqlite3_mem_methods structure fails to allocate a block of memory
25826** and returns 0).
25827**
25828** Most malloc failures are non-benign. After they occur, SQLite
25829** abandons the current operation and returns an error code (usually
25830** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
25831** fatal. For example, if a malloc fails while resizing a hash table, this
25832** is completely recoverable simply by not carrying out the resize. The
25833** hash table will continue to function normally. So a malloc failure
25834** during a hash table resize is a benign fault.
25835*/
25836
25837/* #include "sqliteInt.h" */
25838
25839#ifndef SQLITE_UNTESTABLE
25840
25841/*
25842** Global variables.
25843*/
25844typedef struct BenignMallocHooks BenignMallocHooks;
25845static SQLITE_WSD struct BenignMallocHooks {
25846 void (*xBenignBegin)(void);
25847 void (*xBenignEnd)(void);
25848} sqlite3Hooks = { 0, 0 };
25849
25850/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
25851** structure. If writable static data is unsupported on the target,
25852** we have to locate the state vector at run-time. In the more common
25853** case where writable static data is supported, wsdHooks can refer directly
25854** to the "sqlite3Hooks" state vector declared above.
25855*/
25856#ifdef SQLITE_OMIT_WSD
25857# define wsdHooksInit \
25858 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
25859# define wsdHooks x[0]
25860#else
25861# define wsdHooksInit
25862# define wsdHooks sqlite3Hooks
25863#endif
25864
25865
25866/*
25867** Register hooks to call when sqlite3BeginBenignMalloc() and
25868** sqlite3EndBenignMalloc() are called, respectively.
25869*/
25870SQLITE_PRIVATE void sqlite3BenignMallocHooks(
25871 void (*xBenignBegin)(void),
25872 void (*xBenignEnd)(void)
25873){
25874 wsdHooksInit;
25875 wsdHooks.xBenignBegin = xBenignBegin;
25876 wsdHooks.xBenignEnd = xBenignEnd;
25877}
25878
25879/*
25880** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
25881** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
25882** indicates that subsequent malloc failures are non-benign.
25883*/
25884SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
25885 wsdHooksInit;
25886 if( wsdHooks.xBenignBegin ){
25887 wsdHooks.xBenignBegin();
25888 }
25889}
25890SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
25891 wsdHooksInit;
25892 if( wsdHooks.xBenignEnd ){
25893 wsdHooks.xBenignEnd();
25894 }
25895}
25896
25897#endif /* #ifndef SQLITE_UNTESTABLE */
25898
25899/************** End of fault.c ***********************************************/
25900/************** Begin file mem0.c ********************************************/
25901/*
25902** 2008 October 28
25903**
25904** The author disclaims copyright to this source code. In place of
25905** a legal notice, here is a blessing:
25906**
25907** May you do good and not evil.
25908** May you find forgiveness for yourself and forgive others.
25909** May you share freely, never taking more than you give.
25910**
25911*************************************************************************
25912**
25913** This file contains a no-op memory allocation drivers for use when
25914** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
25915** here always fail. SQLite will not operate with these drivers. These
25916** are merely placeholders. Real drivers must be substituted using
25917** sqlite3_config() before SQLite will operate.
25918*/
25919/* #include "sqliteInt.h" */
25920
25921/*
25922** This version of the memory allocator is the default. It is
25923** used when no other memory allocator is specified using compile-time
25924** macros.
25925*/
25926#ifdef SQLITE_ZERO_MALLOC
25927
25928/*
25929** No-op versions of all memory allocation routines
25930*/
25931static void *sqlite3MemMalloc(int nByte){ return 0; }
25932static void sqlite3MemFree(void *pPrior){ return; }
25933static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
25934static int sqlite3MemSize(void *pPrior){ return 0; }
25935static int sqlite3MemRoundup(int n){ return n; }
25936static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
25937static void sqlite3MemShutdown(void *NotUsed){ return; }
25938
25939/*
25940** This routine is the only routine in this file with external linkage.
25941**
25942** Populate the low-level memory allocation function pointers in
25943** sqlite3GlobalConfig.m with pointers to the routines in this file.
25944*/
25945SQLITE_PRIVATE void sqlite3MemSetDefault(void){
25946 static const sqlite3_mem_methods defaultMethods = {
25947 sqlite3MemMalloc,
25948 sqlite3MemFree,
25949 sqlite3MemRealloc,
25950 sqlite3MemSize,
25951 sqlite3MemRoundup,
25952 sqlite3MemInit,
25953 sqlite3MemShutdown,
25954 0
25955 };
25956 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
25957}
25958
25959#endif /* SQLITE_ZERO_MALLOC */
25960
25961/************** End of mem0.c ************************************************/
25962/************** Begin file mem1.c ********************************************/
25963/*
25964** 2007 August 14
25965**
25966** The author disclaims copyright to this source code. In place of
25967** a legal notice, here is a blessing:
25968**
25969** May you do good and not evil.
25970** May you find forgiveness for yourself and forgive others.
25971** May you share freely, never taking more than you give.
25972**
25973*************************************************************************
25974**
25975** This file contains low-level memory allocation drivers for when
25976** SQLite will use the standard C-library malloc/realloc/free interface
25977** to obtain the memory it needs.
25978**
25979** This file contains implementations of the low-level memory allocation
25980** routines specified in the sqlite3_mem_methods object. The content of
25981** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
25982** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
25983** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
25984** default configuration is to use memory allocation routines in this
25985** file.
25986**
25987** C-preprocessor macro summary:
25988**
25989** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
25990** the malloc_usable_size() interface exists
25991** on the target platform. Or, this symbol
25992** can be set manually, if desired.
25993** If an equivalent interface exists by
25994** a different name, using a separate -D
25995** option to rename it.
25996**
25997** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
25998** memory allocator. Set this symbol to enable
25999** building on older macs.
26000**
26001** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
26002** _msize() on windows systems. This might
26003** be necessary when compiling for Delphi,
26004** for example.
26005*/
26006/* #include "sqliteInt.h" */
26007
26008/*
26009** This version of the memory allocator is the default. It is
26010** used when no other memory allocator is specified using compile-time
26011** macros.
26012*/
26013#ifdef SQLITE_SYSTEM_MALLOC
26014#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
26015
26016/*
26017** Use the zone allocator available on apple products unless the
26018** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
26019*/
26020#include <sys/sysctl.h>
26021#include <malloc/malloc.h>
26022#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
26023#include <libkern/OSAtomic.h>
26024#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
26025static malloc_zone_t* _sqliteZone_;
26026#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
26027#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
26028#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
26029#define SQLITE_MALLOCSIZE(x) \
26030 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
26031
26032#else /* if not __APPLE__ */
26033
26034/*
26035** Use standard C library malloc and free on non-Apple systems.
26036** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
26037*/
26038#define SQLITE_MALLOC(x) malloc(x)
26039#define SQLITE_FREE(x) free(x)
26040#define SQLITE_REALLOC(x,y) realloc((x),(y))
26041
26042/*
26043** The malloc.h header file is needed for malloc_usable_size() function
26044** on some systems (e.g. Linux).
26045*/
26046#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
26047# define SQLITE_USE_MALLOC_H 1
26048# define SQLITE_USE_MALLOC_USABLE_SIZE 1
26049/*
26050** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
26051** use of _msize() is automatic, but can be disabled by compiling with
26052** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
26053** the malloc.h header file.
26054*/
26055#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
26056# define SQLITE_USE_MALLOC_H
26057# define SQLITE_USE_MSIZE
26058#endif
26059
26060/*
26061** Include the malloc.h header file, if necessary. Also set define macro
26062** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
26063** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
26064** The memory size function can always be overridden manually by defining
26065** the macro SQLITE_MALLOCSIZE to the desired function name.
26066*/
26067#if defined(SQLITE_USE_MALLOC_H)
26068# include <malloc.h>
26069# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
26070# if !defined(SQLITE_MALLOCSIZE)
26071# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
26072# endif
26073# elif defined(SQLITE_USE_MSIZE)
26074# if !defined(SQLITE_MALLOCSIZE)
26075# define SQLITE_MALLOCSIZE _msize
26076# endif
26077# endif
26078#endif /* defined(SQLITE_USE_MALLOC_H) */
26079
26080#endif /* __APPLE__ or not __APPLE__ */
26081
26082/*
26083** Like malloc(), but remember the size of the allocation
26084** so that we can find it later using sqlite3MemSize().
26085**
26086** For this low-level routine, we are guaranteed that nByte>0 because
26087** cases of nByte<=0 will be intercepted and dealt with by higher level
26088** routines.
26089*/
26090static void *sqlite3MemMalloc(int nByte){
26091#ifdef SQLITE_MALLOCSIZE
26092 void *p;
26093 testcase( ROUND8(nByte)==nByte );
26094 p = SQLITE_MALLOC( nByte );
26095 if( p==0 ){
26096 testcase( sqlite3GlobalConfig.xLog!=0 );
26097 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
26098 }
26099 return p;
26100#else
26101 sqlite3_int64 *p;
26102 assert( nByte>0 );
26103 testcase( ROUND8(nByte)!=nByte );
26104 p = SQLITE_MALLOC( nByte+8 );
26105 if( p ){
26106 p[0] = nByte;
26107 p++;
26108 }else{
26109 testcase( sqlite3GlobalConfig.xLog!=0 );
26110 sqlite3_log(SQLITE_NOMEM, zFormat: "failed to allocate %u bytes of memory", nByte);
26111 }
26112 return (void *)p;
26113#endif
26114}
26115
26116/*
26117** Like free() but works for allocations obtained from sqlite3MemMalloc()
26118** or sqlite3MemRealloc().
26119**
26120** For this low-level routine, we already know that pPrior!=0 since
26121** cases where pPrior==0 will have been intercepted and dealt with
26122** by higher-level routines.
26123*/
26124static void sqlite3MemFree(void *pPrior){
26125#ifdef SQLITE_MALLOCSIZE
26126 SQLITE_FREE(pPrior);
26127#else
26128 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
26129 assert( pPrior!=0 );
26130 p--;
26131 SQLITE_FREE(p);
26132#endif
26133}
26134
26135/*
26136** Report the allocated size of a prior return from xMalloc()
26137** or xRealloc().
26138*/
26139static int sqlite3MemSize(void *pPrior){
26140#ifdef SQLITE_MALLOCSIZE
26141 assert( pPrior!=0 );
26142 return (int)SQLITE_MALLOCSIZE(pPrior);
26143#else
26144 sqlite3_int64 *p;
26145 assert( pPrior!=0 );
26146 p = (sqlite3_int64*)pPrior;
26147 p--;
26148 return (int)p[0];
26149#endif
26150}
26151
26152/*
26153** Like realloc(). Resize an allocation previously obtained from
26154** sqlite3MemMalloc().
26155**
26156** For this low-level interface, we know that pPrior!=0. Cases where
26157** pPrior==0 while have been intercepted by higher-level routine and
26158** redirected to xMalloc. Similarly, we know that nByte>0 because
26159** cases where nByte<=0 will have been intercepted by higher-level
26160** routines and redirected to xFree.
26161*/
26162static void *sqlite3MemRealloc(void *pPrior, int nByte){
26163#ifdef SQLITE_MALLOCSIZE
26164 void *p = SQLITE_REALLOC(pPrior, nByte);
26165 if( p==0 ){
26166 testcase( sqlite3GlobalConfig.xLog!=0 );
26167 sqlite3_log(SQLITE_NOMEM,
26168 "failed memory resize %u to %u bytes",
26169 SQLITE_MALLOCSIZE(pPrior), nByte);
26170 }
26171 return p;
26172#else
26173 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
26174 assert( pPrior!=0 && nByte>0 );
26175 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
26176 p--;
26177 p = SQLITE_REALLOC(p, nByte+8 );
26178 if( p ){
26179 p[0] = nByte;
26180 p++;
26181 }else{
26182 testcase( sqlite3GlobalConfig.xLog!=0 );
26183 sqlite3_log(SQLITE_NOMEM,
26184 zFormat: "failed memory resize %u to %u bytes",
26185 sqlite3MemSize(pPrior), nByte);
26186 }
26187 return (void*)p;
26188#endif
26189}
26190
26191/*
26192** Round up a request size to the next valid allocation size.
26193*/
26194static int sqlite3MemRoundup(int n){
26195 return ROUND8(n);
26196}
26197
26198/*
26199** Initialize this module.
26200*/
26201static int sqlite3MemInit(void *NotUsed){
26202#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
26203 int cpuCount;
26204 size_t len;
26205 if( _sqliteZone_ ){
26206 return SQLITE_OK;
26207 }
26208 len = sizeof(cpuCount);
26209 /* One usually wants to use hw.activecpu for MT decisions, but not here */
26210 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
26211 if( cpuCount>1 ){
26212 /* defer MT decisions to system malloc */
26213 _sqliteZone_ = malloc_default_zone();
26214 }else{
26215 /* only 1 core, use our own zone to contention over global locks,
26216 ** e.g. we have our own dedicated locks */
26217 _sqliteZone_ = malloc_create_zone(4096, 0);
26218 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
26219 }
26220#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
26221 UNUSED_PARAMETER(NotUsed);
26222 return SQLITE_OK;
26223}
26224
26225/*
26226** Deinitialize this module.
26227*/
26228static void sqlite3MemShutdown(void *NotUsed){
26229 UNUSED_PARAMETER(NotUsed);
26230 return;
26231}
26232
26233/*
26234** This routine is the only routine in this file with external linkage.
26235**
26236** Populate the low-level memory allocation function pointers in
26237** sqlite3GlobalConfig.m with pointers to the routines in this file.
26238*/
26239SQLITE_PRIVATE void sqlite3MemSetDefault(void){
26240 static const sqlite3_mem_methods defaultMethods = {
26241 sqlite3MemMalloc,
26242 sqlite3MemFree,
26243 sqlite3MemRealloc,
26244 sqlite3MemSize,
26245 sqlite3MemRoundup,
26246 sqlite3MemInit,
26247 sqlite3MemShutdown,
26248 0
26249 };
26250 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
26251}
26252
26253#endif /* SQLITE_SYSTEM_MALLOC */
26254
26255/************** End of mem1.c ************************************************/
26256/************** Begin file mem2.c ********************************************/
26257/*
26258** 2007 August 15
26259**
26260** The author disclaims copyright to this source code. In place of
26261** a legal notice, here is a blessing:
26262**
26263** May you do good and not evil.
26264** May you find forgiveness for yourself and forgive others.
26265** May you share freely, never taking more than you give.
26266**
26267*************************************************************************
26268**
26269** This file contains low-level memory allocation drivers for when
26270** SQLite will use the standard C-library malloc/realloc/free interface
26271** to obtain the memory it needs while adding lots of additional debugging
26272** information to each allocation in order to help detect and fix memory
26273** leaks and memory usage errors.
26274**
26275** This file contains implementations of the low-level memory allocation
26276** routines specified in the sqlite3_mem_methods object.
26277*/
26278/* #include "sqliteInt.h" */
26279
26280/*
26281** This version of the memory allocator is used only if the
26282** SQLITE_MEMDEBUG macro is defined
26283*/
26284#ifdef SQLITE_MEMDEBUG
26285
26286/*
26287** The backtrace functionality is only available with GLIBC
26288*/
26289#ifdef __GLIBC__
26290 extern int backtrace(void**,int);
26291 extern void backtrace_symbols_fd(void*const*,int,int);
26292#else
26293# define backtrace(A,B) 1
26294# define backtrace_symbols_fd(A,B,C)
26295#endif
26296/* #include <stdio.h> */
26297
26298/*
26299** Each memory allocation looks like this:
26300**
26301** ------------------------------------------------------------------------
26302** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
26303** ------------------------------------------------------------------------
26304**
26305** The application code sees only a pointer to the allocation. We have
26306** to back up from the allocation pointer to find the MemBlockHdr. The
26307** MemBlockHdr tells us the size of the allocation and the number of
26308** backtrace pointers. There is also a guard word at the end of the
26309** MemBlockHdr.
26310*/
26311struct MemBlockHdr {
26312 i64 iSize; /* Size of this allocation */
26313 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
26314 char nBacktrace; /* Number of backtraces on this alloc */
26315 char nBacktraceSlots; /* Available backtrace slots */
26316 u8 nTitle; /* Bytes of title; includes '\0' */
26317 u8 eType; /* Allocation type code */
26318 int iForeGuard; /* Guard word for sanity */
26319};
26320
26321/*
26322** Guard words
26323*/
26324#define FOREGUARD 0x80F5E153
26325#define REARGUARD 0xE4676B53
26326
26327/*
26328** Number of malloc size increments to track.
26329*/
26330#define NCSIZE 1000
26331
26332/*
26333** All of the static variables used by this module are collected
26334** into a single structure named "mem". This is to keep the
26335** static variables organized and to reduce namespace pollution
26336** when this module is combined with other in the amalgamation.
26337*/
26338static struct {
26339
26340 /*
26341 ** Mutex to control access to the memory allocation subsystem.
26342 */
26343 sqlite3_mutex *mutex;
26344
26345 /*
26346 ** Head and tail of a linked list of all outstanding allocations
26347 */
26348 struct MemBlockHdr *pFirst;
26349 struct MemBlockHdr *pLast;
26350
26351 /*
26352 ** The number of levels of backtrace to save in new allocations.
26353 */
26354 int nBacktrace;
26355 void (*xBacktrace)(int, int, void **);
26356
26357 /*
26358 ** Title text to insert in front of each block
26359 */
26360 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
26361 char zTitle[100]; /* The title text */
26362
26363 /*
26364 ** sqlite3MallocDisallow() increments the following counter.
26365 ** sqlite3MallocAllow() decrements it.
26366 */
26367 int disallow; /* Do not allow memory allocation */
26368
26369 /*
26370 ** Gather statistics on the sizes of memory allocations.
26371 ** nAlloc[i] is the number of allocation attempts of i*8
26372 ** bytes. i==NCSIZE is the number of allocation attempts for
26373 ** sizes more than NCSIZE*8 bytes.
26374 */
26375 int nAlloc[NCSIZE]; /* Total number of allocations */
26376 int nCurrent[NCSIZE]; /* Current number of allocations */
26377 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
26378
26379} mem;
26380
26381
26382/*
26383** Adjust memory usage statistics
26384*/
26385static void adjustStats(int iSize, int increment){
26386 int i = ROUND8(iSize)/8;
26387 if( i>NCSIZE-1 ){
26388 i = NCSIZE - 1;
26389 }
26390 if( increment>0 ){
26391 mem.nAlloc[i]++;
26392 mem.nCurrent[i]++;
26393 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
26394 mem.mxCurrent[i] = mem.nCurrent[i];
26395 }
26396 }else{
26397 mem.nCurrent[i]--;
26398 assert( mem.nCurrent[i]>=0 );
26399 }
26400}
26401
26402/*
26403** Given an allocation, find the MemBlockHdr for that allocation.
26404**
26405** This routine checks the guards at either end of the allocation and
26406** if they are incorrect it asserts.
26407*/
26408static struct MemBlockHdr *sqlite3MemsysGetHeader(const void *pAllocation){
26409 struct MemBlockHdr *p;
26410 int *pInt;
26411 u8 *pU8;
26412 int nReserve;
26413
26414 p = (struct MemBlockHdr*)pAllocation;
26415 p--;
26416 assert( p->iForeGuard==(int)FOREGUARD );
26417 nReserve = ROUND8(p->iSize);
26418 pInt = (int*)pAllocation;
26419 pU8 = (u8*)pAllocation;
26420 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
26421 /* This checks any of the "extra" bytes allocated due
26422 ** to rounding up to an 8 byte boundary to ensure
26423 ** they haven't been overwritten.
26424 */
26425 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
26426 return p;
26427}
26428
26429/*
26430** Return the number of bytes currently allocated at address p.
26431*/
26432static int sqlite3MemSize(void *p){
26433 struct MemBlockHdr *pHdr;
26434 if( !p ){
26435 return 0;
26436 }
26437 pHdr = sqlite3MemsysGetHeader(p);
26438 return (int)pHdr->iSize;
26439}
26440
26441/*
26442** Initialize the memory allocation subsystem.
26443*/
26444static int sqlite3MemInit(void *NotUsed){
26445 UNUSED_PARAMETER(NotUsed);
26446 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
26447 if( !sqlite3GlobalConfig.bMemstat ){
26448 /* If memory status is enabled, then the malloc.c wrapper will already
26449 ** hold the STATIC_MEM mutex when the routines here are invoked. */
26450 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
26451 }
26452 return SQLITE_OK;
26453}
26454
26455/*
26456** Deinitialize the memory allocation subsystem.
26457*/
26458static void sqlite3MemShutdown(void *NotUsed){
26459 UNUSED_PARAMETER(NotUsed);
26460 mem.mutex = 0;
26461}
26462
26463/*
26464** Round up a request size to the next valid allocation size.
26465*/
26466static int sqlite3MemRoundup(int n){
26467 return ROUND8(n);
26468}
26469
26470/*
26471** Fill a buffer with pseudo-random bytes. This is used to preset
26472** the content of a new memory allocation to unpredictable values and
26473** to clear the content of a freed allocation to unpredictable values.
26474*/
26475static void randomFill(char *pBuf, int nByte){
26476 unsigned int x, y, r;
26477 x = SQLITE_PTR_TO_INT(pBuf);
26478 y = nByte | 1;
26479 while( nByte >= 4 ){
26480 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
26481 y = y*1103515245 + 12345;
26482 r = x ^ y;
26483 *(int*)pBuf = r;
26484 pBuf += 4;
26485 nByte -= 4;
26486 }
26487 while( nByte-- > 0 ){
26488 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
26489 y = y*1103515245 + 12345;
26490 r = x ^ y;
26491 *(pBuf++) = r & 0xff;
26492 }
26493}
26494
26495/*
26496** Allocate nByte bytes of memory.
26497*/
26498static void *sqlite3MemMalloc(int nByte){
26499 struct MemBlockHdr *pHdr;
26500 void **pBt;
26501 char *z;
26502 int *pInt;
26503 void *p = 0;
26504 int totalSize;
26505 int nReserve;
26506 sqlite3_mutex_enter(mem.mutex);
26507 assert( mem.disallow==0 );
26508 nReserve = ROUND8(nByte);
26509 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
26510 mem.nBacktrace*sizeof(void*) + mem.nTitle;
26511 p = malloc(totalSize);
26512 if( p ){
26513 z = p;
26514 pBt = (void**)&z[mem.nTitle];
26515 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
26516 pHdr->pNext = 0;
26517 pHdr->pPrev = mem.pLast;
26518 if( mem.pLast ){
26519 mem.pLast->pNext = pHdr;
26520 }else{
26521 mem.pFirst = pHdr;
26522 }
26523 mem.pLast = pHdr;
26524 pHdr->iForeGuard = FOREGUARD;
26525 pHdr->eType = MEMTYPE_HEAP;
26526 pHdr->nBacktraceSlots = mem.nBacktrace;
26527 pHdr->nTitle = mem.nTitle;
26528 if( mem.nBacktrace ){
26529 void *aAddr[40];
26530 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
26531 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
26532 assert(pBt[0]);
26533 if( mem.xBacktrace ){
26534 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
26535 }
26536 }else{
26537 pHdr->nBacktrace = 0;
26538 }
26539 if( mem.nTitle ){
26540 memcpy(z, mem.zTitle, mem.nTitle);
26541 }
26542 pHdr->iSize = nByte;
26543 adjustStats(nByte, +1);
26544 pInt = (int*)&pHdr[1];
26545 pInt[nReserve/sizeof(int)] = REARGUARD;
26546 randomFill((char*)pInt, nByte);
26547 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
26548 p = (void*)pInt;
26549 }
26550 sqlite3_mutex_leave(mem.mutex);
26551 return p;
26552}
26553
26554/*
26555** Free memory.
26556*/
26557static void sqlite3MemFree(void *pPrior){
26558 struct MemBlockHdr *pHdr;
26559 void **pBt;
26560 char *z;
26561 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
26562 || mem.mutex!=0 );
26563 pHdr = sqlite3MemsysGetHeader(pPrior);
26564 pBt = (void**)pHdr;
26565 pBt -= pHdr->nBacktraceSlots;
26566 sqlite3_mutex_enter(mem.mutex);
26567 if( pHdr->pPrev ){
26568 assert( pHdr->pPrev->pNext==pHdr );
26569 pHdr->pPrev->pNext = pHdr->pNext;
26570 }else{
26571 assert( mem.pFirst==pHdr );
26572 mem.pFirst = pHdr->pNext;
26573 }
26574 if( pHdr->pNext ){
26575 assert( pHdr->pNext->pPrev==pHdr );
26576 pHdr->pNext->pPrev = pHdr->pPrev;
26577 }else{
26578 assert( mem.pLast==pHdr );
26579 mem.pLast = pHdr->pPrev;
26580 }
26581 z = (char*)pBt;
26582 z -= pHdr->nTitle;
26583 adjustStats((int)pHdr->iSize, -1);
26584 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
26585 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
26586 free(z);
26587 sqlite3_mutex_leave(mem.mutex);
26588}
26589
26590/*
26591** Change the size of an existing memory allocation.
26592**
26593** For this debugging implementation, we *always* make a copy of the
26594** allocation into a new place in memory. In this way, if the
26595** higher level code is using pointer to the old allocation, it is
26596** much more likely to break and we are much more liking to find
26597** the error.
26598*/
26599static void *sqlite3MemRealloc(void *pPrior, int nByte){
26600 struct MemBlockHdr *pOldHdr;
26601 void *pNew;
26602 assert( mem.disallow==0 );
26603 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
26604 pOldHdr = sqlite3MemsysGetHeader(pPrior);
26605 pNew = sqlite3MemMalloc(nByte);
26606 if( pNew ){
26607 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
26608 if( nByte>pOldHdr->iSize ){
26609 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
26610 }
26611 sqlite3MemFree(pPrior);
26612 }
26613 return pNew;
26614}
26615
26616/*
26617** Populate the low-level memory allocation function pointers in
26618** sqlite3GlobalConfig.m with pointers to the routines in this file.
26619*/
26620SQLITE_PRIVATE void sqlite3MemSetDefault(void){
26621 static const sqlite3_mem_methods defaultMethods = {
26622 sqlite3MemMalloc,
26623 sqlite3MemFree,
26624 sqlite3MemRealloc,
26625 sqlite3MemSize,
26626 sqlite3MemRoundup,
26627 sqlite3MemInit,
26628 sqlite3MemShutdown,
26629 0
26630 };
26631 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
26632}
26633
26634/*
26635** Set the "type" of an allocation.
26636*/
26637SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
26638 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
26639 struct MemBlockHdr *pHdr;
26640 pHdr = sqlite3MemsysGetHeader(p);
26641 assert( pHdr->iForeGuard==FOREGUARD );
26642 pHdr->eType = eType;
26643 }
26644}
26645
26646/*
26647** Return TRUE if the mask of type in eType matches the type of the
26648** allocation p. Also return true if p==NULL.
26649**
26650** This routine is designed for use within an assert() statement, to
26651** verify the type of an allocation. For example:
26652**
26653** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26654*/
26655SQLITE_PRIVATE int sqlite3MemdebugHasType(const void *p, u8 eType){
26656 int rc = 1;
26657 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
26658 struct MemBlockHdr *pHdr;
26659 pHdr = sqlite3MemsysGetHeader(p);
26660 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
26661 if( (pHdr->eType&eType)==0 ){
26662 rc = 0;
26663 }
26664 }
26665 return rc;
26666}
26667
26668/*
26669** Return TRUE if the mask of type in eType matches no bits of the type of the
26670** allocation p. Also return true if p==NULL.
26671**
26672** This routine is designed for use within an assert() statement, to
26673** verify the type of an allocation. For example:
26674**
26675** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
26676*/
26677SQLITE_PRIVATE int sqlite3MemdebugNoType(const void *p, u8 eType){
26678 int rc = 1;
26679 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
26680 struct MemBlockHdr *pHdr;
26681 pHdr = sqlite3MemsysGetHeader(p);
26682 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
26683 if( (pHdr->eType&eType)!=0 ){
26684 rc = 0;
26685 }
26686 }
26687 return rc;
26688}
26689
26690/*
26691** Set the number of backtrace levels kept for each allocation.
26692** A value of zero turns off backtracing. The number is always rounded
26693** up to a multiple of 2.
26694*/
26695SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
26696 if( depth<0 ){ depth = 0; }
26697 if( depth>20 ){ depth = 20; }
26698 depth = (depth+1)&0xfe;
26699 mem.nBacktrace = depth;
26700}
26701
26702SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
26703 mem.xBacktrace = xBacktrace;
26704}
26705
26706/*
26707** Set the title string for subsequent allocations.
26708*/
26709SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
26710 unsigned int n = sqlite3Strlen30(zTitle) + 1;
26711 sqlite3_mutex_enter(mem.mutex);
26712 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
26713 memcpy(mem.zTitle, zTitle, n);
26714 mem.zTitle[n] = 0;
26715 mem.nTitle = ROUND8(n);
26716 sqlite3_mutex_leave(mem.mutex);
26717}
26718
26719SQLITE_PRIVATE void sqlite3MemdebugSync(){
26720 struct MemBlockHdr *pHdr;
26721 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
26722 void **pBt = (void**)pHdr;
26723 pBt -= pHdr->nBacktraceSlots;
26724 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
26725 }
26726}
26727
26728/*
26729** Open the file indicated and write a log of all unfreed memory
26730** allocations into that log.
26731*/
26732SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
26733 FILE *out;
26734 struct MemBlockHdr *pHdr;
26735 void **pBt;
26736 int i;
26737 out = fopen(zFilename, "w");
26738 if( out==0 ){
26739 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
26740 zFilename);
26741 return;
26742 }
26743 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
26744 char *z = (char*)pHdr;
26745 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
26746 fprintf(out, "**** %lld bytes at %p from %s ****\n",
26747 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
26748 if( pHdr->nBacktrace ){
26749 fflush(out);
26750 pBt = (void**)pHdr;
26751 pBt -= pHdr->nBacktraceSlots;
26752 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
26753 fprintf(out, "\n");
26754 }
26755 }
26756 fprintf(out, "COUNTS:\n");
26757 for(i=0; i<NCSIZE-1; i++){
26758 if( mem.nAlloc[i] ){
26759 fprintf(out, " %5d: %10d %10d %10d\n",
26760 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
26761 }
26762 }
26763 if( mem.nAlloc[NCSIZE-1] ){
26764 fprintf(out, " %5d: %10d %10d %10d\n",
26765 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
26766 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
26767 }
26768 fclose(out);
26769}
26770
26771/*
26772** Return the number of times sqlite3MemMalloc() has been called.
26773*/
26774SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
26775 int i;
26776 int nTotal = 0;
26777 for(i=0; i<NCSIZE; i++){
26778 nTotal += mem.nAlloc[i];
26779 }
26780 return nTotal;
26781}
26782
26783
26784#endif /* SQLITE_MEMDEBUG */
26785
26786/************** End of mem2.c ************************************************/
26787/************** Begin file mem3.c ********************************************/
26788/*
26789** 2007 October 14
26790**
26791** The author disclaims copyright to this source code. In place of
26792** a legal notice, here is a blessing:
26793**
26794** May you do good and not evil.
26795** May you find forgiveness for yourself and forgive others.
26796** May you share freely, never taking more than you give.
26797**
26798*************************************************************************
26799** This file contains the C functions that implement a memory
26800** allocation subsystem for use by SQLite.
26801**
26802** This version of the memory allocation subsystem omits all
26803** use of malloc(). The SQLite user supplies a block of memory
26804** before calling sqlite3_initialize() from which allocations
26805** are made and returned by the xMalloc() and xRealloc()
26806** implementations. Once sqlite3_initialize() has been called,
26807** the amount of memory available to SQLite is fixed and cannot
26808** be changed.
26809**
26810** This version of the memory allocation subsystem is included
26811** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
26812*/
26813/* #include "sqliteInt.h" */
26814
26815/*
26816** This version of the memory allocator is only built into the library
26817** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
26818** mean that the library will use a memory-pool by default, just that
26819** it is available. The mempool allocator is activated by calling
26820** sqlite3_config().
26821*/
26822#ifdef SQLITE_ENABLE_MEMSYS3
26823
26824/*
26825** Maximum size (in Mem3Blocks) of a "small" chunk.
26826*/
26827#define MX_SMALL 10
26828
26829
26830/*
26831** Number of freelist hash slots
26832*/
26833#define N_HASH 61
26834
26835/*
26836** A memory allocation (also called a "chunk") consists of two or
26837** more blocks where each block is 8 bytes. The first 8 bytes are
26838** a header that is not returned to the user.
26839**
26840** A chunk is two or more blocks that is either checked out or
26841** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
26842** size of the allocation in blocks if the allocation is free.
26843** The u.hdr.size4x&1 bit is true if the chunk is checked out and
26844** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
26845** is true if the previous chunk is checked out and false if the
26846** previous chunk is free. The u.hdr.prevSize field is the size of
26847** the previous chunk in blocks if the previous chunk is on the
26848** freelist. If the previous chunk is checked out, then
26849** u.hdr.prevSize can be part of the data for that chunk and should
26850** not be read or written.
26851**
26852** We often identify a chunk by its index in mem3.aPool[]. When
26853** this is done, the chunk index refers to the second block of
26854** the chunk. In this way, the first chunk has an index of 1.
26855** A chunk index of 0 means "no such chunk" and is the equivalent
26856** of a NULL pointer.
26857**
26858** The second block of free chunks is of the form u.list. The
26859** two fields form a double-linked list of chunks of related sizes.
26860** Pointers to the head of the list are stored in mem3.aiSmall[]
26861** for smaller chunks and mem3.aiHash[] for larger chunks.
26862**
26863** The second block of a chunk is user data if the chunk is checked
26864** out. If a chunk is checked out, the user data may extend into
26865** the u.hdr.prevSize value of the following chunk.
26866*/
26867typedef struct Mem3Block Mem3Block;
26868struct Mem3Block {
26869 union {
26870 struct {
26871 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
26872 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
26873 } hdr;
26874 struct {
26875 u32 next; /* Index in mem3.aPool[] of next free chunk */
26876 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
26877 } list;
26878 } u;
26879};
26880
26881/*
26882** All of the static variables used by this module are collected
26883** into a single structure named "mem3". This is to keep the
26884** static variables organized and to reduce namespace pollution
26885** when this module is combined with other in the amalgamation.
26886*/
26887static SQLITE_WSD struct Mem3Global {
26888 /*
26889 ** Memory available for allocation. nPool is the size of the array
26890 ** (in Mem3Blocks) pointed to by aPool less 2.
26891 */
26892 u32 nPool;
26893 Mem3Block *aPool;
26894
26895 /*
26896 ** True if we are evaluating an out-of-memory callback.
26897 */
26898 int alarmBusy;
26899
26900 /*
26901 ** Mutex to control access to the memory allocation subsystem.
26902 */
26903 sqlite3_mutex *mutex;
26904
26905 /*
26906 ** The minimum amount of free space that we have seen.
26907 */
26908 u32 mnKeyBlk;
26909
26910 /*
26911 ** iKeyBlk is the index of the key chunk. Most new allocations
26912 ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks)
26913 ** of the current key chunk. iKeyBlk is 0 if there is no key chunk.
26914 ** The key chunk is not in either the aiHash[] or aiSmall[].
26915 */
26916 u32 iKeyBlk;
26917 u32 szKeyBlk;
26918
26919 /*
26920 ** Array of lists of free blocks according to the block size
26921 ** for smaller chunks, or a hash on the block size for larger
26922 ** chunks.
26923 */
26924 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
26925 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
26926} mem3 = { 97535575 };
26927
26928#define mem3 GLOBAL(struct Mem3Global, mem3)
26929
26930/*
26931** Unlink the chunk at mem3.aPool[i] from list it is currently
26932** on. *pRoot is the list that i is a member of.
26933*/
26934static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
26935 u32 next = mem3.aPool[i].u.list.next;
26936 u32 prev = mem3.aPool[i].u.list.prev;
26937 assert( sqlite3_mutex_held(mem3.mutex) );
26938 if( prev==0 ){
26939 *pRoot = next;
26940 }else{
26941 mem3.aPool[prev].u.list.next = next;
26942 }
26943 if( next ){
26944 mem3.aPool[next].u.list.prev = prev;
26945 }
26946 mem3.aPool[i].u.list.next = 0;
26947 mem3.aPool[i].u.list.prev = 0;
26948}
26949
26950/*
26951** Unlink the chunk at index i from
26952** whatever list is currently a member of.
26953*/
26954static void memsys3Unlink(u32 i){
26955 u32 size, hash;
26956 assert( sqlite3_mutex_held(mem3.mutex) );
26957 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26958 assert( i>=1 );
26959 size = mem3.aPool[i-1].u.hdr.size4x/4;
26960 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26961 assert( size>=2 );
26962 if( size <= MX_SMALL ){
26963 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
26964 }else{
26965 hash = size % N_HASH;
26966 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
26967 }
26968}
26969
26970/*
26971** Link the chunk at mem3.aPool[i] so that is on the list rooted
26972** at *pRoot.
26973*/
26974static void memsys3LinkIntoList(u32 i, u32 *pRoot){
26975 assert( sqlite3_mutex_held(mem3.mutex) );
26976 mem3.aPool[i].u.list.next = *pRoot;
26977 mem3.aPool[i].u.list.prev = 0;
26978 if( *pRoot ){
26979 mem3.aPool[*pRoot].u.list.prev = i;
26980 }
26981 *pRoot = i;
26982}
26983
26984/*
26985** Link the chunk at index i into either the appropriate
26986** small chunk list, or into the large chunk hash table.
26987*/
26988static void memsys3Link(u32 i){
26989 u32 size, hash;
26990 assert( sqlite3_mutex_held(mem3.mutex) );
26991 assert( i>=1 );
26992 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26993 size = mem3.aPool[i-1].u.hdr.size4x/4;
26994 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26995 assert( size>=2 );
26996 if( size <= MX_SMALL ){
26997 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
26998 }else{
26999 hash = size % N_HASH;
27000 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
27001 }
27002}
27003
27004/*
27005** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
27006** will already be held (obtained by code in malloc.c) if
27007** sqlite3GlobalConfig.bMemStat is true.
27008*/
27009static void memsys3Enter(void){
27010 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
27011 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
27012 }
27013 sqlite3_mutex_enter(mem3.mutex);
27014}
27015static void memsys3Leave(void){
27016 sqlite3_mutex_leave(mem3.mutex);
27017}
27018
27019/*
27020** Called when we are unable to satisfy an allocation of nBytes.
27021*/
27022static void memsys3OutOfMemory(int nByte){
27023 if( !mem3.alarmBusy ){
27024 mem3.alarmBusy = 1;
27025 assert( sqlite3_mutex_held(mem3.mutex) );
27026 sqlite3_mutex_leave(mem3.mutex);
27027 sqlite3_release_memory(nByte);
27028 sqlite3_mutex_enter(mem3.mutex);
27029 mem3.alarmBusy = 0;
27030 }
27031}
27032
27033
27034/*
27035** Chunk i is a free chunk that has been unlinked. Adjust its
27036** size parameters for check-out and return a pointer to the
27037** user portion of the chunk.
27038*/
27039static void *memsys3Checkout(u32 i, u32 nBlock){
27040 u32 x;
27041 assert( sqlite3_mutex_held(mem3.mutex) );
27042 assert( i>=1 );
27043 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
27044 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
27045 x = mem3.aPool[i-1].u.hdr.size4x;
27046 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
27047 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
27048 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
27049 return &mem3.aPool[i];
27050}
27051
27052/*
27053** Carve a piece off of the end of the mem3.iKeyBlk free chunk.
27054** Return a pointer to the new allocation. Or, if the key chunk
27055** is not large enough, return 0.
27056*/
27057static void *memsys3FromKeyBlk(u32 nBlock){
27058 assert( sqlite3_mutex_held(mem3.mutex) );
27059 assert( mem3.szKeyBlk>=nBlock );
27060 if( nBlock>=mem3.szKeyBlk-1 ){
27061 /* Use the entire key chunk */
27062 void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk);
27063 mem3.iKeyBlk = 0;
27064 mem3.szKeyBlk = 0;
27065 mem3.mnKeyBlk = 0;
27066 return p;
27067 }else{
27068 /* Split the key block. Return the tail. */
27069 u32 newi, x;
27070 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
27071 assert( newi > mem3.iKeyBlk+1 );
27072 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
27073 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
27074 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
27075 mem3.szKeyBlk -= nBlock;
27076 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
27077 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
27078 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
27079 if( mem3.szKeyBlk < mem3.mnKeyBlk ){
27080 mem3.mnKeyBlk = mem3.szKeyBlk;
27081 }
27082 return (void*)&mem3.aPool[newi];
27083 }
27084}
27085
27086/*
27087** *pRoot is the head of a list of free chunks of the same size
27088** or same size hash. In other words, *pRoot is an entry in either
27089** mem3.aiSmall[] or mem3.aiHash[].
27090**
27091** This routine examines all entries on the given list and tries
27092** to coalesce each entries with adjacent free chunks.
27093**
27094** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces
27095** the current mem3.iKeyBlk with the new larger chunk. In order for
27096** this mem3.iKeyBlk replacement to work, the key chunk must be
27097** linked into the hash tables. That is not the normal state of
27098** affairs, of course. The calling routine must link the key
27099** chunk before invoking this routine, then must unlink the (possibly
27100** changed) key chunk once this routine has finished.
27101*/
27102static void memsys3Merge(u32 *pRoot){
27103 u32 iNext, prev, size, i, x;
27104
27105 assert( sqlite3_mutex_held(mem3.mutex) );
27106 for(i=*pRoot; i>0; i=iNext){
27107 iNext = mem3.aPool[i].u.list.next;
27108 size = mem3.aPool[i-1].u.hdr.size4x;
27109 assert( (size&1)==0 );
27110 if( (size&2)==0 ){
27111 memsys3UnlinkFromList(i, pRoot);
27112 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
27113 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
27114 if( prev==iNext ){
27115 iNext = mem3.aPool[prev].u.list.next;
27116 }
27117 memsys3Unlink(prev);
27118 size = i + size/4 - prev;
27119 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
27120 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
27121 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
27122 memsys3Link(prev);
27123 i = prev;
27124 }else{
27125 size /= 4;
27126 }
27127 if( size>mem3.szKeyBlk ){
27128 mem3.iKeyBlk = i;
27129 mem3.szKeyBlk = size;
27130 }
27131 }
27132}
27133
27134/*
27135** Return a block of memory of at least nBytes in size.
27136** Return NULL if unable.
27137**
27138** This function assumes that the necessary mutexes, if any, are
27139** already held by the caller. Hence "Unsafe".
27140*/
27141static void *memsys3MallocUnsafe(int nByte){
27142 u32 i;
27143 u32 nBlock;
27144 u32 toFree;
27145
27146 assert( sqlite3_mutex_held(mem3.mutex) );
27147 assert( sizeof(Mem3Block)==8 );
27148 if( nByte<=12 ){
27149 nBlock = 2;
27150 }else{
27151 nBlock = (nByte + 11)/8;
27152 }
27153 assert( nBlock>=2 );
27154
27155 /* STEP 1:
27156 ** Look for an entry of the correct size in either the small
27157 ** chunk table or in the large chunk hash table. This is
27158 ** successful most of the time (about 9 times out of 10).
27159 */
27160 if( nBlock <= MX_SMALL ){
27161 i = mem3.aiSmall[nBlock-2];
27162 if( i>0 ){
27163 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
27164 return memsys3Checkout(i, nBlock);
27165 }
27166 }else{
27167 int hash = nBlock % N_HASH;
27168 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
27169 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
27170 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
27171 return memsys3Checkout(i, nBlock);
27172 }
27173 }
27174 }
27175
27176 /* STEP 2:
27177 ** Try to satisfy the allocation by carving a piece off of the end
27178 ** of the key chunk. This step usually works if step 1 fails.
27179 */
27180 if( mem3.szKeyBlk>=nBlock ){
27181 return memsys3FromKeyBlk(nBlock);
27182 }
27183
27184
27185 /* STEP 3:
27186 ** Loop through the entire memory pool. Coalesce adjacent free
27187 ** chunks. Recompute the key chunk as the largest free chunk.
27188 ** Then try again to satisfy the allocation by carving a piece off
27189 ** of the end of the key chunk. This step happens very
27190 ** rarely (we hope!)
27191 */
27192 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
27193 memsys3OutOfMemory(toFree);
27194 if( mem3.iKeyBlk ){
27195 memsys3Link(mem3.iKeyBlk);
27196 mem3.iKeyBlk = 0;
27197 mem3.szKeyBlk = 0;
27198 }
27199 for(i=0; i<N_HASH; i++){
27200 memsys3Merge(&mem3.aiHash[i]);
27201 }
27202 for(i=0; i<MX_SMALL-1; i++){
27203 memsys3Merge(&mem3.aiSmall[i]);
27204 }
27205 if( mem3.szKeyBlk ){
27206 memsys3Unlink(mem3.iKeyBlk);
27207 if( mem3.szKeyBlk>=nBlock ){
27208 return memsys3FromKeyBlk(nBlock);
27209 }
27210 }
27211 }
27212
27213 /* If none of the above worked, then we fail. */
27214 return 0;
27215}
27216
27217/*
27218** Free an outstanding memory allocation.
27219**
27220** This function assumes that the necessary mutexes, if any, are
27221** already held by the caller. Hence "Unsafe".
27222*/
27223static void memsys3FreeUnsafe(void *pOld){
27224 Mem3Block *p = (Mem3Block*)pOld;
27225 int i;
27226 u32 size, x;
27227 assert( sqlite3_mutex_held(mem3.mutex) );
27228 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
27229 i = p - mem3.aPool;
27230 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
27231 size = mem3.aPool[i-1].u.hdr.size4x/4;
27232 assert( i+size<=mem3.nPool+1 );
27233 mem3.aPool[i-1].u.hdr.size4x &= ~1;
27234 mem3.aPool[i+size-1].u.hdr.prevSize = size;
27235 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
27236 memsys3Link(i);
27237
27238 /* Try to expand the key using the newly freed chunk */
27239 if( mem3.iKeyBlk ){
27240 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
27241 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
27242 mem3.iKeyBlk -= size;
27243 mem3.szKeyBlk += size;
27244 memsys3Unlink(mem3.iKeyBlk);
27245 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
27246 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
27247 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
27248 }
27249 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
27250 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
27251 memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk);
27252 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
27253 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
27254 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
27255 }
27256 }
27257}
27258
27259/*
27260** Return the size of an outstanding allocation, in bytes. The
27261** size returned omits the 8-byte header overhead. This only
27262** works for chunks that are currently checked out.
27263*/
27264static int memsys3Size(void *p){
27265 Mem3Block *pBlock;
27266 assert( p!=0 );
27267 pBlock = (Mem3Block*)p;
27268 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
27269 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
27270}
27271
27272/*
27273** Round up a request size to the next valid allocation size.
27274*/
27275static int memsys3Roundup(int n){
27276 if( n<=12 ){
27277 return 12;
27278 }else{
27279 return ((n+11)&~7) - 4;
27280 }
27281}
27282
27283/*
27284** Allocate nBytes of memory.
27285*/
27286static void *memsys3Malloc(int nBytes){
27287 sqlite3_int64 *p;
27288 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
27289 memsys3Enter();
27290 p = memsys3MallocUnsafe(nBytes);
27291 memsys3Leave();
27292 return (void*)p;
27293}
27294
27295/*
27296** Free memory.
27297*/
27298static void memsys3Free(void *pPrior){
27299 assert( pPrior );
27300 memsys3Enter();
27301 memsys3FreeUnsafe(pPrior);
27302 memsys3Leave();
27303}
27304
27305/*
27306** Change the size of an existing memory allocation
27307*/
27308static void *memsys3Realloc(void *pPrior, int nBytes){
27309 int nOld;
27310 void *p;
27311 if( pPrior==0 ){
27312 return sqlite3_malloc(nBytes);
27313 }
27314 if( nBytes<=0 ){
27315 sqlite3_free(pPrior);
27316 return 0;
27317 }
27318 nOld = memsys3Size(pPrior);
27319 if( nBytes<=nOld && nBytes>=nOld-128 ){
27320 return pPrior;
27321 }
27322 memsys3Enter();
27323 p = memsys3MallocUnsafe(nBytes);
27324 if( p ){
27325 if( nOld<nBytes ){
27326 memcpy(p, pPrior, nOld);
27327 }else{
27328 memcpy(p, pPrior, nBytes);
27329 }
27330 memsys3FreeUnsafe(pPrior);
27331 }
27332 memsys3Leave();
27333 return p;
27334}
27335
27336/*
27337** Initialize this module.
27338*/
27339static int memsys3Init(void *NotUsed){
27340 UNUSED_PARAMETER(NotUsed);
27341 if( !sqlite3GlobalConfig.pHeap ){
27342 return SQLITE_ERROR;
27343 }
27344
27345 /* Store a pointer to the memory block in global structure mem3. */
27346 assert( sizeof(Mem3Block)==8 );
27347 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
27348 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
27349
27350 /* Initialize the key block. */
27351 mem3.szKeyBlk = mem3.nPool;
27352 mem3.mnKeyBlk = mem3.szKeyBlk;
27353 mem3.iKeyBlk = 1;
27354 mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2;
27355 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
27356 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
27357
27358 return SQLITE_OK;
27359}
27360
27361/*
27362** Deinitialize this module.
27363*/
27364static void memsys3Shutdown(void *NotUsed){
27365 UNUSED_PARAMETER(NotUsed);
27366 mem3.mutex = 0;
27367 return;
27368}
27369
27370
27371
27372/*
27373** Open the file indicated and write a log of all unfreed memory
27374** allocations into that log.
27375*/
27376SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
27377#ifdef SQLITE_DEBUG
27378 FILE *out;
27379 u32 i, j;
27380 u32 size;
27381 if( zFilename==0 || zFilename[0]==0 ){
27382 out = stdout;
27383 }else{
27384 out = fopen(zFilename, "w");
27385 if( out==0 ){
27386 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
27387 zFilename);
27388 return;
27389 }
27390 }
27391 memsys3Enter();
27392 fprintf(out, "CHUNKS:\n");
27393 for(i=1; i<=mem3.nPool; i+=size/4){
27394 size = mem3.aPool[i-1].u.hdr.size4x;
27395 if( size/4<=1 ){
27396 fprintf(out, "%p size error\n", &mem3.aPool[i]);
27397 assert( 0 );
27398 break;
27399 }
27400 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
27401 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
27402 assert( 0 );
27403 break;
27404 }
27405 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
27406 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
27407 assert( 0 );
27408 break;
27409 }
27410 if( size&1 ){
27411 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
27412 }else{
27413 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
27414 i==mem3.iKeyBlk ? " **key**" : "");
27415 }
27416 }
27417 for(i=0; i<MX_SMALL-1; i++){
27418 if( mem3.aiSmall[i]==0 ) continue;
27419 fprintf(out, "small(%2d):", i);
27420 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
27421 fprintf(out, " %p(%d)", &mem3.aPool[j],
27422 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
27423 }
27424 fprintf(out, "\n");
27425 }
27426 for(i=0; i<N_HASH; i++){
27427 if( mem3.aiHash[i]==0 ) continue;
27428 fprintf(out, "hash(%2d):", i);
27429 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
27430 fprintf(out, " %p(%d)", &mem3.aPool[j],
27431 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
27432 }
27433 fprintf(out, "\n");
27434 }
27435 fprintf(out, "key=%d\n", mem3.iKeyBlk);
27436 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
27437 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
27438 sqlite3_mutex_leave(mem3.mutex);
27439 if( out==stdout ){
27440 fflush(stdout);
27441 }else{
27442 fclose(out);
27443 }
27444#else
27445 UNUSED_PARAMETER(zFilename);
27446#endif
27447}
27448
27449/*
27450** This routine is the only routine in this file with external
27451** linkage.
27452**
27453** Populate the low-level memory allocation function pointers in
27454** sqlite3GlobalConfig.m with pointers to the routines in this file. The
27455** arguments specify the block of memory to manage.
27456**
27457** This routine is only called by sqlite3_config(), and therefore
27458** is not required to be threadsafe (it is not).
27459*/
27460SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
27461 static const sqlite3_mem_methods mempoolMethods = {
27462 memsys3Malloc,
27463 memsys3Free,
27464 memsys3Realloc,
27465 memsys3Size,
27466 memsys3Roundup,
27467 memsys3Init,
27468 memsys3Shutdown,
27469 0
27470 };
27471 return &mempoolMethods;
27472}
27473
27474#endif /* SQLITE_ENABLE_MEMSYS3 */
27475
27476/************** End of mem3.c ************************************************/
27477/************** Begin file mem5.c ********************************************/
27478/*
27479** 2007 October 14
27480**
27481** The author disclaims copyright to this source code. In place of
27482** a legal notice, here is a blessing:
27483**
27484** May you do good and not evil.
27485** May you find forgiveness for yourself and forgive others.
27486** May you share freely, never taking more than you give.
27487**
27488*************************************************************************
27489** This file contains the C functions that implement a memory
27490** allocation subsystem for use by SQLite.
27491**
27492** This version of the memory allocation subsystem omits all
27493** use of malloc(). The application gives SQLite a block of memory
27494** before calling sqlite3_initialize() from which allocations
27495** are made and returned by the xMalloc() and xRealloc()
27496** implementations. Once sqlite3_initialize() has been called,
27497** the amount of memory available to SQLite is fixed and cannot
27498** be changed.
27499**
27500** This version of the memory allocation subsystem is included
27501** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
27502**
27503** This memory allocator uses the following algorithm:
27504**
27505** 1. All memory allocation sizes are rounded up to a power of 2.
27506**
27507** 2. If two adjacent free blocks are the halves of a larger block,
27508** then the two blocks are coalesced into the single larger block.
27509**
27510** 3. New memory is allocated from the first available free block.
27511**
27512** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
27513** Concerning Dynamic Storage Allocation". Journal of the Association for
27514** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
27515**
27516** Let n be the size of the largest allocation divided by the minimum
27517** allocation size (after rounding all sizes up to a power of 2.) Let M
27518** be the maximum amount of memory ever outstanding at one time. Let
27519** N be the total amount of memory available for allocation. Robson
27520** proved that this memory allocator will never breakdown due to
27521** fragmentation as long as the following constraint holds:
27522**
27523** N >= M*(1 + log2(n)/2) - n + 1
27524**
27525** The sqlite3_status() logic tracks the maximum values of n and M so
27526** that an application can, at any time, verify this constraint.
27527*/
27528/* #include "sqliteInt.h" */
27529
27530/*
27531** This version of the memory allocator is used only when
27532** SQLITE_ENABLE_MEMSYS5 is defined.
27533*/
27534#ifdef SQLITE_ENABLE_MEMSYS5
27535
27536/*
27537** A minimum allocation is an instance of the following structure.
27538** Larger allocations are an array of these structures where the
27539** size of the array is a power of 2.
27540**
27541** The size of this object must be a power of two. That fact is
27542** verified in memsys5Init().
27543*/
27544typedef struct Mem5Link Mem5Link;
27545struct Mem5Link {
27546 int next; /* Index of next free chunk */
27547 int prev; /* Index of previous free chunk */
27548};
27549
27550/*
27551** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
27552** mem5.szAtom is always at least 8 and 32-bit integers are used,
27553** it is not actually possible to reach this limit.
27554*/
27555#define LOGMAX 30
27556
27557/*
27558** Masks used for mem5.aCtrl[] elements.
27559*/
27560#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
27561#define CTRL_FREE 0x20 /* True if not checked out */
27562
27563/*
27564** All of the static variables used by this module are collected
27565** into a single structure named "mem5". This is to keep the
27566** static variables organized and to reduce namespace pollution
27567** when this module is combined with other in the amalgamation.
27568*/
27569static SQLITE_WSD struct Mem5Global {
27570 /*
27571 ** Memory available for allocation
27572 */
27573 int szAtom; /* Smallest possible allocation in bytes */
27574 int nBlock; /* Number of szAtom sized blocks in zPool */
27575 u8 *zPool; /* Memory available to be allocated */
27576
27577 /*
27578 ** Mutex to control access to the memory allocation subsystem.
27579 */
27580 sqlite3_mutex *mutex;
27581
27582#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27583 /*
27584 ** Performance statistics
27585 */
27586 u64 nAlloc; /* Total number of calls to malloc */
27587 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
27588 u64 totalExcess; /* Total internal fragmentation */
27589 u32 currentOut; /* Current checkout, including internal fragmentation */
27590 u32 currentCount; /* Current number of distinct checkouts */
27591 u32 maxOut; /* Maximum instantaneous currentOut */
27592 u32 maxCount; /* Maximum instantaneous currentCount */
27593 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
27594#endif
27595
27596 /*
27597 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
27598 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
27599 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
27600 */
27601 int aiFreelist[LOGMAX+1];
27602
27603 /*
27604 ** Space for tracking which blocks are checked out and the size
27605 ** of each block. One byte per block.
27606 */
27607 u8 *aCtrl;
27608
27609} mem5;
27610
27611/*
27612** Access the static variable through a macro for SQLITE_OMIT_WSD.
27613*/
27614#define mem5 GLOBAL(struct Mem5Global, mem5)
27615
27616/*
27617** Assuming mem5.zPool is divided up into an array of Mem5Link
27618** structures, return a pointer to the idx-th such link.
27619*/
27620#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
27621
27622/*
27623** Unlink the chunk at mem5.aPool[i] from list it is currently
27624** on. It should be found on mem5.aiFreelist[iLogsize].
27625*/
27626static void memsys5Unlink(int i, int iLogsize){
27627 int next, prev;
27628 assert( i>=0 && i<mem5.nBlock );
27629 assert( iLogsize>=0 && iLogsize<=LOGMAX );
27630 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
27631
27632 next = MEM5LINK(i)->next;
27633 prev = MEM5LINK(i)->prev;
27634 if( prev<0 ){
27635 mem5.aiFreelist[iLogsize] = next;
27636 }else{
27637 MEM5LINK(prev)->next = next;
27638 }
27639 if( next>=0 ){
27640 MEM5LINK(next)->prev = prev;
27641 }
27642}
27643
27644/*
27645** Link the chunk at mem5.aPool[i] so that is on the iLogsize
27646** free list.
27647*/
27648static void memsys5Link(int i, int iLogsize){
27649 int x;
27650 assert( sqlite3_mutex_held(mem5.mutex) );
27651 assert( i>=0 && i<mem5.nBlock );
27652 assert( iLogsize>=0 && iLogsize<=LOGMAX );
27653 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
27654
27655 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
27656 MEM5LINK(i)->prev = -1;
27657 if( x>=0 ){
27658 assert( x<mem5.nBlock );
27659 MEM5LINK(x)->prev = i;
27660 }
27661 mem5.aiFreelist[iLogsize] = i;
27662}
27663
27664/*
27665** Obtain or release the mutex needed to access global data structures.
27666*/
27667static void memsys5Enter(void){
27668 sqlite3_mutex_enter(mem5.mutex);
27669}
27670static void memsys5Leave(void){
27671 sqlite3_mutex_leave(mem5.mutex);
27672}
27673
27674/*
27675** Return the size of an outstanding allocation, in bytes.
27676** This only works for chunks that are currently checked out.
27677*/
27678static int memsys5Size(void *p){
27679 int iSize, i;
27680 assert( p!=0 );
27681 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
27682 assert( i>=0 && i<mem5.nBlock );
27683 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
27684 return iSize;
27685}
27686
27687/*
27688** Return a block of memory of at least nBytes in size.
27689** Return NULL if unable. Return NULL if nBytes==0.
27690**
27691** The caller guarantees that nByte is positive.
27692**
27693** The caller has obtained a mutex prior to invoking this
27694** routine so there is never any chance that two or more
27695** threads can be in this routine at the same time.
27696*/
27697static void *memsys5MallocUnsafe(int nByte){
27698 int i; /* Index of a mem5.aPool[] slot */
27699 int iBin; /* Index into mem5.aiFreelist[] */
27700 int iFullSz; /* Size of allocation rounded up to power of 2 */
27701 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
27702
27703 /* nByte must be a positive */
27704 assert( nByte>0 );
27705
27706 /* No more than 1GiB per allocation */
27707 if( nByte > 0x40000000 ) return 0;
27708
27709#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27710 /* Keep track of the maximum allocation request. Even unfulfilled
27711 ** requests are counted */
27712 if( (u32)nByte>mem5.maxRequest ){
27713 mem5.maxRequest = nByte;
27714 }
27715#endif
27716
27717
27718 /* Round nByte up to the next valid power of two */
27719 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
27720
27721 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
27722 ** block. If not, then split a block of the next larger power of
27723 ** two in order to create a new free block of size iLogsize.
27724 */
27725 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
27726 if( iBin>LOGMAX ){
27727 testcase( sqlite3GlobalConfig.xLog!=0 );
27728 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
27729 return 0;
27730 }
27731 i = mem5.aiFreelist[iBin];
27732 memsys5Unlink(i, iBin);
27733 while( iBin>iLogsize ){
27734 int newSize;
27735
27736 iBin--;
27737 newSize = 1 << iBin;
27738 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
27739 memsys5Link(i+newSize, iBin);
27740 }
27741 mem5.aCtrl[i] = iLogsize;
27742
27743#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27744 /* Update allocator performance statistics. */
27745 mem5.nAlloc++;
27746 mem5.totalAlloc += iFullSz;
27747 mem5.totalExcess += iFullSz - nByte;
27748 mem5.currentCount++;
27749 mem5.currentOut += iFullSz;
27750 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
27751 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
27752#endif
27753
27754#ifdef SQLITE_DEBUG
27755 /* Make sure the allocated memory does not assume that it is set to zero
27756 ** or retains a value from a previous allocation */
27757 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
27758#endif
27759
27760 /* Return a pointer to the allocated memory. */
27761 return (void*)&mem5.zPool[i*mem5.szAtom];
27762}
27763
27764/*
27765** Free an outstanding memory allocation.
27766*/
27767static void memsys5FreeUnsafe(void *pOld){
27768 u32 size, iLogsize;
27769 int iBlock;
27770
27771 /* Set iBlock to the index of the block pointed to by pOld in
27772 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
27773 */
27774 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
27775
27776 /* Check that the pointer pOld points to a valid, non-free block. */
27777 assert( iBlock>=0 && iBlock<mem5.nBlock );
27778 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
27779 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
27780
27781 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
27782 size = 1<<iLogsize;
27783 assert( iBlock+size-1<(u32)mem5.nBlock );
27784
27785 mem5.aCtrl[iBlock] |= CTRL_FREE;
27786 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
27787
27788#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27789 assert( mem5.currentCount>0 );
27790 assert( mem5.currentOut>=(size*mem5.szAtom) );
27791 mem5.currentCount--;
27792 mem5.currentOut -= size*mem5.szAtom;
27793 assert( mem5.currentOut>0 || mem5.currentCount==0 );
27794 assert( mem5.currentCount>0 || mem5.currentOut==0 );
27795#endif
27796
27797 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
27798 while( ALWAYS(iLogsize<LOGMAX) ){
27799 int iBuddy;
27800 if( (iBlock>>iLogsize) & 1 ){
27801 iBuddy = iBlock - size;
27802 assert( iBuddy>=0 );
27803 }else{
27804 iBuddy = iBlock + size;
27805 if( iBuddy>=mem5.nBlock ) break;
27806 }
27807 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
27808 memsys5Unlink(iBuddy, iLogsize);
27809 iLogsize++;
27810 if( iBuddy<iBlock ){
27811 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
27812 mem5.aCtrl[iBlock] = 0;
27813 iBlock = iBuddy;
27814 }else{
27815 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
27816 mem5.aCtrl[iBuddy] = 0;
27817 }
27818 size *= 2;
27819 }
27820
27821#ifdef SQLITE_DEBUG
27822 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
27823 ** not used after being freed */
27824 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
27825#endif
27826
27827 memsys5Link(iBlock, iLogsize);
27828}
27829
27830/*
27831** Allocate nBytes of memory.
27832*/
27833static void *memsys5Malloc(int nBytes){
27834 sqlite3_int64 *p = 0;
27835 if( nBytes>0 ){
27836 memsys5Enter();
27837 p = memsys5MallocUnsafe(nBytes);
27838 memsys5Leave();
27839 }
27840 return (void*)p;
27841}
27842
27843/*
27844** Free memory.
27845**
27846** The outer layer memory allocator prevents this routine from
27847** being called with pPrior==0.
27848*/
27849static void memsys5Free(void *pPrior){
27850 assert( pPrior!=0 );
27851 memsys5Enter();
27852 memsys5FreeUnsafe(pPrior);
27853 memsys5Leave();
27854}
27855
27856/*
27857** Change the size of an existing memory allocation.
27858**
27859** The outer layer memory allocator prevents this routine from
27860** being called with pPrior==0.
27861**
27862** nBytes is always a value obtained from a prior call to
27863** memsys5Round(). Hence nBytes is always a non-negative power
27864** of two. If nBytes==0 that means that an oversize allocation
27865** (an allocation larger than 0x40000000) was requested and this
27866** routine should return 0 without freeing pPrior.
27867*/
27868static void *memsys5Realloc(void *pPrior, int nBytes){
27869 int nOld;
27870 void *p;
27871 assert( pPrior!=0 );
27872 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
27873 assert( nBytes>=0 );
27874 if( nBytes==0 ){
27875 return 0;
27876 }
27877 nOld = memsys5Size(pPrior);
27878 if( nBytes<=nOld ){
27879 return pPrior;
27880 }
27881 p = memsys5Malloc(nBytes);
27882 if( p ){
27883 memcpy(p, pPrior, nOld);
27884 memsys5Free(pPrior);
27885 }
27886 return p;
27887}
27888
27889/*
27890** Round up a request size to the next valid allocation size. If
27891** the allocation is too large to be handled by this allocation system,
27892** return 0.
27893**
27894** All allocations must be a power of two and must be expressed by a
27895** 32-bit signed integer. Hence the largest allocation is 0x40000000
27896** or 1073741824 bytes.
27897*/
27898static int memsys5Roundup(int n){
27899 int iFullSz;
27900 if( n<=mem5.szAtom*2 ){
27901 if( n<=mem5.szAtom ) return mem5.szAtom;
27902 return mem5.szAtom*2;
27903 }
27904 if( n>0x10000000 ){
27905 if( n>0x40000000 ) return 0;
27906 if( n>0x20000000 ) return 0x40000000;
27907 return 0x20000000;
27908 }
27909 for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4);
27910 if( (iFullSz/2)>=(i64)n ) return iFullSz/2;
27911 return iFullSz;
27912}
27913
27914/*
27915** Return the ceiling of the logarithm base 2 of iValue.
27916**
27917** Examples: memsys5Log(1) -> 0
27918** memsys5Log(2) -> 1
27919** memsys5Log(4) -> 2
27920** memsys5Log(5) -> 3
27921** memsys5Log(8) -> 3
27922** memsys5Log(9) -> 4
27923*/
27924static int memsys5Log(int iValue){
27925 int iLog;
27926 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
27927 return iLog;
27928}
27929
27930/*
27931** Initialize the memory allocator.
27932**
27933** This routine is not threadsafe. The caller must be holding a mutex
27934** to prevent multiple threads from entering at the same time.
27935*/
27936static int memsys5Init(void *NotUsed){
27937 int ii; /* Loop counter */
27938 int nByte; /* Number of bytes of memory available to this allocator */
27939 u8 *zByte; /* Memory usable by this allocator */
27940 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
27941 int iOffset; /* An offset into mem5.aCtrl[] */
27942
27943 UNUSED_PARAMETER(NotUsed);
27944
27945 /* For the purposes of this routine, disable the mutex */
27946 mem5.mutex = 0;
27947
27948 /* The size of a Mem5Link object must be a power of two. Verify that
27949 ** this is case.
27950 */
27951 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
27952
27953 nByte = sqlite3GlobalConfig.nHeap;
27954 zByte = (u8*)sqlite3GlobalConfig.pHeap;
27955 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
27956
27957 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
27958 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
27959 mem5.szAtom = (1<<nMinLog);
27960 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
27961 mem5.szAtom = mem5.szAtom << 1;
27962 }
27963
27964 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
27965 mem5.zPool = zByte;
27966 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
27967
27968 for(ii=0; ii<=LOGMAX; ii++){
27969 mem5.aiFreelist[ii] = -1;
27970 }
27971
27972 iOffset = 0;
27973 for(ii=LOGMAX; ii>=0; ii--){
27974 int nAlloc = (1<<ii);
27975 if( (iOffset+nAlloc)<=mem5.nBlock ){
27976 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
27977 memsys5Link(iOffset, ii);
27978 iOffset += nAlloc;
27979 }
27980 assert((iOffset+nAlloc)>mem5.nBlock);
27981 }
27982
27983 /* If a mutex is required for normal operation, allocate one */
27984 if( sqlite3GlobalConfig.bMemstat==0 ){
27985 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
27986 }
27987
27988 return SQLITE_OK;
27989}
27990
27991/*
27992** Deinitialize this module.
27993*/
27994static void memsys5Shutdown(void *NotUsed){
27995 UNUSED_PARAMETER(NotUsed);
27996 mem5.mutex = 0;
27997 return;
27998}
27999
28000#ifdef SQLITE_TEST
28001/*
28002** Open the file indicated and write a log of all unfreed memory
28003** allocations into that log.
28004*/
28005SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
28006 FILE *out;
28007 int i, j, n;
28008 int nMinLog;
28009
28010 if( zFilename==0 || zFilename[0]==0 ){
28011 out = stdout;
28012 }else{
28013 out = fopen(zFilename, "w");
28014 if( out==0 ){
28015 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
28016 zFilename);
28017 return;
28018 }
28019 }
28020 memsys5Enter();
28021 nMinLog = memsys5Log(mem5.szAtom);
28022 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
28023 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
28024 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
28025 }
28026 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
28027 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
28028 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
28029 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
28030 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
28031 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
28032 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
28033 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
28034 memsys5Leave();
28035 if( out==stdout ){
28036 fflush(stdout);
28037 }else{
28038 fclose(out);
28039 }
28040}
28041#endif
28042
28043/*
28044** This routine is the only routine in this file with external
28045** linkage. It returns a pointer to a static sqlite3_mem_methods
28046** struct populated with the memsys5 methods.
28047*/
28048SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
28049 static const sqlite3_mem_methods memsys5Methods = {
28050 memsys5Malloc,
28051 memsys5Free,
28052 memsys5Realloc,
28053 memsys5Size,
28054 memsys5Roundup,
28055 memsys5Init,
28056 memsys5Shutdown,
28057 0
28058 };
28059 return &memsys5Methods;
28060}
28061
28062#endif /* SQLITE_ENABLE_MEMSYS5 */
28063
28064/************** End of mem5.c ************************************************/
28065/************** Begin file mutex.c *******************************************/
28066/*
28067** 2007 August 14
28068**
28069** The author disclaims copyright to this source code. In place of
28070** a legal notice, here is a blessing:
28071**
28072** May you do good and not evil.
28073** May you find forgiveness for yourself and forgive others.
28074** May you share freely, never taking more than you give.
28075**
28076*************************************************************************
28077** This file contains the C functions that implement mutexes.
28078**
28079** This file contains code that is common across all mutex implementations.
28080*/
28081/* #include "sqliteInt.h" */
28082
28083#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
28084/*
28085** For debugging purposes, record when the mutex subsystem is initialized
28086** and uninitialized so that we can assert() if there is an attempt to
28087** allocate a mutex while the system is uninitialized.
28088*/
28089static SQLITE_WSD int mutexIsInit = 0;
28090#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
28091
28092
28093#ifndef SQLITE_MUTEX_OMIT
28094
28095#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
28096/*
28097** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
28098** the implementation of a wrapper around the system default mutex
28099** implementation (sqlite3DefaultMutex()).
28100**
28101** Most calls are passed directly through to the underlying default
28102** mutex implementation. Except, if a mutex is configured by calling
28103** sqlite3MutexWarnOnContention() on it, then if contention is ever
28104** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
28105**
28106** This type of mutex is used as the database handle mutex when testing
28107** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
28108*/
28109
28110/*
28111** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
28112** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
28113** allocated by the system mutex implementation. Variable iType is usually set
28114** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
28115** or one of the static mutex identifiers. Or, if this is a recursive mutex
28116** that has been configured using sqlite3MutexWarnOnContention(), it is
28117** set to SQLITE_MUTEX_WARNONCONTENTION.
28118*/
28119typedef struct CheckMutex CheckMutex;
28120struct CheckMutex {
28121 int iType;
28122 sqlite3_mutex *mutex;
28123};
28124
28125#define SQLITE_MUTEX_WARNONCONTENTION (-1)
28126
28127/*
28128** Pointer to real mutex methods object used by the CheckMutex
28129** implementation. Set by checkMutexInit().
28130*/
28131static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
28132
28133#ifdef SQLITE_DEBUG
28134static int checkMutexHeld(sqlite3_mutex *p){
28135 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
28136}
28137static int checkMutexNotheld(sqlite3_mutex *p){
28138 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
28139}
28140#endif
28141
28142/*
28143** Initialize and deinitialize the mutex subsystem.
28144*/
28145static int checkMutexInit(void){
28146 pGlobalMutexMethods = sqlite3DefaultMutex();
28147 return SQLITE_OK;
28148}
28149static int checkMutexEnd(void){
28150 pGlobalMutexMethods = 0;
28151 return SQLITE_OK;
28152}
28153
28154/*
28155** Allocate a mutex.
28156*/
28157static sqlite3_mutex *checkMutexAlloc(int iType){
28158 static CheckMutex staticMutexes[] = {
28159 {2, 0}, {3, 0}, {4, 0}, {5, 0},
28160 {6, 0}, {7, 0}, {8, 0}, {9, 0},
28161 {10, 0}, {11, 0}, {12, 0}, {13, 0}
28162 };
28163 CheckMutex *p = 0;
28164
28165 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
28166 if( iType<2 ){
28167 p = sqlite3MallocZero(sizeof(CheckMutex));
28168 if( p==0 ) return 0;
28169 p->iType = iType;
28170 }else{
28171#ifdef SQLITE_ENABLE_API_ARMOR
28172 if( iType-2>=ArraySize(staticMutexes) ){
28173 (void)SQLITE_MISUSE_BKPT;
28174 return 0;
28175 }
28176#endif
28177 p = &staticMutexes[iType-2];
28178 }
28179
28180 if( p->mutex==0 ){
28181 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
28182 if( p->mutex==0 ){
28183 if( iType<2 ){
28184 sqlite3_free(p);
28185 }
28186 p = 0;
28187 }
28188 }
28189
28190 return (sqlite3_mutex*)p;
28191}
28192
28193/*
28194** Free a mutex.
28195*/
28196static void checkMutexFree(sqlite3_mutex *p){
28197 assert( SQLITE_MUTEX_RECURSIVE<2 );
28198 assert( SQLITE_MUTEX_FAST<2 );
28199 assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
28200
28201#if SQLITE_ENABLE_API_ARMOR
28202 if( ((CheckMutex*)p)->iType<2 )
28203#endif
28204 {
28205 CheckMutex *pCheck = (CheckMutex*)p;
28206 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
28207 sqlite3_free(pCheck);
28208 }
28209#ifdef SQLITE_ENABLE_API_ARMOR
28210 else{
28211 (void)SQLITE_MISUSE_BKPT;
28212 }
28213#endif
28214}
28215
28216/*
28217** Enter the mutex.
28218*/
28219static void checkMutexEnter(sqlite3_mutex *p){
28220 CheckMutex *pCheck = (CheckMutex*)p;
28221 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
28222 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
28223 return;
28224 }
28225 sqlite3_log(SQLITE_MISUSE,
28226 "illegal multi-threaded access to database connection"
28227 );
28228 }
28229 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
28230}
28231
28232/*
28233** Enter the mutex (do not block).
28234*/
28235static int checkMutexTry(sqlite3_mutex *p){
28236 CheckMutex *pCheck = (CheckMutex*)p;
28237 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
28238}
28239
28240/*
28241** Leave the mutex.
28242*/
28243static void checkMutexLeave(sqlite3_mutex *p){
28244 CheckMutex *pCheck = (CheckMutex*)p;
28245 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
28246}
28247
28248sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
28249 static const sqlite3_mutex_methods sMutex = {
28250 checkMutexInit,
28251 checkMutexEnd,
28252 checkMutexAlloc,
28253 checkMutexFree,
28254 checkMutexEnter,
28255 checkMutexTry,
28256 checkMutexLeave,
28257#ifdef SQLITE_DEBUG
28258 checkMutexHeld,
28259 checkMutexNotheld
28260#else
28261 0,
28262 0
28263#endif
28264 };
28265 return &sMutex;
28266}
28267
28268/*
28269** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
28270** one on which there should be no contention.
28271*/
28272SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
28273 if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
28274 CheckMutex *pCheck = (CheckMutex*)p;
28275 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
28276 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
28277 }
28278}
28279#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
28280
28281/*
28282** Initialize the mutex system.
28283*/
28284SQLITE_PRIVATE int sqlite3MutexInit(void){
28285 int rc = SQLITE_OK;
28286 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
28287 /* If the xMutexAlloc method has not been set, then the user did not
28288 ** install a mutex implementation via sqlite3_config() prior to
28289 ** sqlite3_initialize() being called. This block copies pointers to
28290 ** the default implementation into the sqlite3GlobalConfig structure.
28291 */
28292 sqlite3_mutex_methods const *pFrom;
28293 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
28294
28295 if( sqlite3GlobalConfig.bCoreMutex ){
28296#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
28297 pFrom = multiThreadedCheckMutex();
28298#else
28299 pFrom = sqlite3DefaultMutex();
28300#endif
28301 }else{
28302 pFrom = sqlite3NoopMutex();
28303 }
28304 pTo->xMutexInit = pFrom->xMutexInit;
28305 pTo->xMutexEnd = pFrom->xMutexEnd;
28306 pTo->xMutexFree = pFrom->xMutexFree;
28307 pTo->xMutexEnter = pFrom->xMutexEnter;
28308 pTo->xMutexTry = pFrom->xMutexTry;
28309 pTo->xMutexLeave = pFrom->xMutexLeave;
28310 pTo->xMutexHeld = pFrom->xMutexHeld;
28311 pTo->xMutexNotheld = pFrom->xMutexNotheld;
28312 sqlite3MemoryBarrier();
28313 pTo->xMutexAlloc = pFrom->xMutexAlloc;
28314 }
28315 assert( sqlite3GlobalConfig.mutex.xMutexInit );
28316 rc = sqlite3GlobalConfig.mutex.xMutexInit();
28317
28318#ifdef SQLITE_DEBUG
28319 GLOBAL(int, mutexIsInit) = 1;
28320#endif
28321
28322 sqlite3MemoryBarrier();
28323 return rc;
28324}
28325
28326/*
28327** Shutdown the mutex system. This call frees resources allocated by
28328** sqlite3MutexInit().
28329*/
28330SQLITE_PRIVATE int sqlite3MutexEnd(void){
28331 int rc = SQLITE_OK;
28332 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
28333 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
28334 }
28335
28336#ifdef SQLITE_DEBUG
28337 GLOBAL(int, mutexIsInit) = 0;
28338#endif
28339
28340 return rc;
28341}
28342
28343/*
28344** Retrieve a pointer to a static mutex or allocate a new dynamic one.
28345*/
28346SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
28347#ifndef SQLITE_OMIT_AUTOINIT
28348 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
28349 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
28350#endif
28351 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
28352 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
28353}
28354
28355SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
28356 if( !sqlite3GlobalConfig.bCoreMutex ){
28357 return 0;
28358 }
28359 assert( GLOBAL(int, mutexIsInit) );
28360 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
28361 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
28362}
28363
28364/*
28365** Free a dynamic mutex.
28366*/
28367SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
28368 if( p ){
28369 assert( sqlite3GlobalConfig.mutex.xMutexFree );
28370 sqlite3GlobalConfig.mutex.xMutexFree(p);
28371 }
28372}
28373
28374/*
28375** Obtain the mutex p. If some other thread already has the mutex, block
28376** until it can be obtained.
28377*/
28378SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
28379 if( p ){
28380 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
28381 sqlite3GlobalConfig.mutex.xMutexEnter(p);
28382 }
28383}
28384
28385/*
28386** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
28387** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
28388*/
28389SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
28390 int rc = SQLITE_OK;
28391 if( p ){
28392 assert( sqlite3GlobalConfig.mutex.xMutexTry );
28393 return sqlite3GlobalConfig.mutex.xMutexTry(p);
28394 }
28395 return rc;
28396}
28397
28398/*
28399** The sqlite3_mutex_leave() routine exits a mutex that was previously
28400** entered by the same thread. The behavior is undefined if the mutex
28401** is not currently entered. If a NULL pointer is passed as an argument
28402** this function is a no-op.
28403*/
28404SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
28405 if( p ){
28406 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
28407 sqlite3GlobalConfig.mutex.xMutexLeave(p);
28408 }
28409}
28410
28411#ifndef NDEBUG
28412/*
28413** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
28414** intended for use inside assert() statements.
28415*/
28416SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
28417 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
28418 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
28419}
28420SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
28421 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
28422 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
28423}
28424#endif
28425
28426#endif /* !defined(SQLITE_MUTEX_OMIT) */
28427
28428/************** End of mutex.c ***********************************************/
28429/************** Begin file mutex_noop.c **************************************/
28430/*
28431** 2008 October 07
28432**
28433** The author disclaims copyright to this source code. In place of
28434** a legal notice, here is a blessing:
28435**
28436** May you do good and not evil.
28437** May you find forgiveness for yourself and forgive others.
28438** May you share freely, never taking more than you give.
28439**
28440*************************************************************************
28441** This file contains the C functions that implement mutexes.
28442**
28443** This implementation in this file does not provide any mutual
28444** exclusion and is thus suitable for use only in applications
28445** that use SQLite in a single thread. The routines defined
28446** here are place-holders. Applications can substitute working
28447** mutex routines at start-time using the
28448**
28449** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
28450**
28451** interface.
28452**
28453** If compiled with SQLITE_DEBUG, then additional logic is inserted
28454** that does error checking on mutexes to make sure they are being
28455** called correctly.
28456*/
28457/* #include "sqliteInt.h" */
28458
28459#ifndef SQLITE_MUTEX_OMIT
28460
28461#ifndef SQLITE_DEBUG
28462/*
28463** Stub routines for all mutex methods.
28464**
28465** This routines provide no mutual exclusion or error checking.
28466*/
28467static int noopMutexInit(void){ return SQLITE_OK; }
28468static int noopMutexEnd(void){ return SQLITE_OK; }
28469static sqlite3_mutex *noopMutexAlloc(int id){
28470 UNUSED_PARAMETER(id);
28471 return (sqlite3_mutex*)8;
28472}
28473static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
28474static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
28475static int noopMutexTry(sqlite3_mutex *p){
28476 UNUSED_PARAMETER(p);
28477 return SQLITE_OK;
28478}
28479static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
28480
28481SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
28482 static const sqlite3_mutex_methods sMutex = {
28483 noopMutexInit,
28484 noopMutexEnd,
28485 noopMutexAlloc,
28486 noopMutexFree,
28487 noopMutexEnter,
28488 noopMutexTry,
28489 noopMutexLeave,
28490
28491 0,
28492 0,
28493 };
28494
28495 return &sMutex;
28496}
28497#endif /* !SQLITE_DEBUG */
28498
28499#ifdef SQLITE_DEBUG
28500/*
28501** In this implementation, error checking is provided for testing
28502** and debugging purposes. The mutexes still do not provide any
28503** mutual exclusion.
28504*/
28505
28506/*
28507** The mutex object
28508*/
28509typedef struct sqlite3_debug_mutex {
28510 int id; /* The mutex type */
28511 int cnt; /* Number of entries without a matching leave */
28512} sqlite3_debug_mutex;
28513
28514/*
28515** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
28516** intended for use inside assert() statements.
28517*/
28518static int debugMutexHeld(sqlite3_mutex *pX){
28519 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
28520 return p==0 || p->cnt>0;
28521}
28522static int debugMutexNotheld(sqlite3_mutex *pX){
28523 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
28524 return p==0 || p->cnt==0;
28525}
28526
28527/*
28528** Initialize and deinitialize the mutex subsystem.
28529*/
28530static int debugMutexInit(void){ return SQLITE_OK; }
28531static int debugMutexEnd(void){ return SQLITE_OK; }
28532
28533/*
28534** The sqlite3_mutex_alloc() routine allocates a new
28535** mutex and returns a pointer to it. If it returns NULL
28536** that means that a mutex could not be allocated.
28537*/
28538static sqlite3_mutex *debugMutexAlloc(int id){
28539 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
28540 sqlite3_debug_mutex *pNew = 0;
28541 switch( id ){
28542 case SQLITE_MUTEX_FAST:
28543 case SQLITE_MUTEX_RECURSIVE: {
28544 pNew = sqlite3Malloc(sizeof(*pNew));
28545 if( pNew ){
28546 pNew->id = id;
28547 pNew->cnt = 0;
28548 }
28549 break;
28550 }
28551 default: {
28552#ifdef SQLITE_ENABLE_API_ARMOR
28553 if( id-2<0 || id-2>=ArraySize(aStatic) ){
28554 (void)SQLITE_MISUSE_BKPT;
28555 return 0;
28556 }
28557#endif
28558 pNew = &aStatic[id-2];
28559 pNew->id = id;
28560 break;
28561 }
28562 }
28563 return (sqlite3_mutex*)pNew;
28564}
28565
28566/*
28567** This routine deallocates a previously allocated mutex.
28568*/
28569static void debugMutexFree(sqlite3_mutex *pX){
28570 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
28571 assert( p->cnt==0 );
28572 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
28573 sqlite3_free(p);
28574 }else{
28575#ifdef SQLITE_ENABLE_API_ARMOR
28576 (void)SQLITE_MISUSE_BKPT;
28577#endif
28578 }
28579}
28580
28581/*
28582** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
28583** to enter a mutex. If another thread is already within the mutex,
28584** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
28585** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
28586** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
28587** be entered multiple times by the same thread. In such cases the,
28588** mutex must be exited an equal number of times before another thread
28589** can enter. If the same thread tries to enter any other kind of mutex
28590** more than once, the behavior is undefined.
28591*/
28592static void debugMutexEnter(sqlite3_mutex *pX){
28593 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
28594 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28595 p->cnt++;
28596}
28597static int debugMutexTry(sqlite3_mutex *pX){
28598 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
28599 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28600 p->cnt++;
28601 return SQLITE_OK;
28602}
28603
28604/*
28605** The sqlite3_mutex_leave() routine exits a mutex that was
28606** previously entered by the same thread. The behavior
28607** is undefined if the mutex is not currently entered or
28608** is not currently allocated. SQLite will never do either.
28609*/
28610static void debugMutexLeave(sqlite3_mutex *pX){
28611 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
28612 assert( debugMutexHeld(pX) );
28613 p->cnt--;
28614 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28615}
28616
28617SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
28618 static const sqlite3_mutex_methods sMutex = {
28619 debugMutexInit,
28620 debugMutexEnd,
28621 debugMutexAlloc,
28622 debugMutexFree,
28623 debugMutexEnter,
28624 debugMutexTry,
28625 debugMutexLeave,
28626
28627 debugMutexHeld,
28628 debugMutexNotheld
28629 };
28630
28631 return &sMutex;
28632}
28633#endif /* SQLITE_DEBUG */
28634
28635/*
28636** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
28637** is used regardless of the run-time threadsafety setting.
28638*/
28639#ifdef SQLITE_MUTEX_NOOP
28640SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
28641 return sqlite3NoopMutex();
28642}
28643#endif /* defined(SQLITE_MUTEX_NOOP) */
28644#endif /* !defined(SQLITE_MUTEX_OMIT) */
28645
28646/************** End of mutex_noop.c ******************************************/
28647/************** Begin file mutex_unix.c **************************************/
28648/*
28649** 2007 August 28
28650**
28651** The author disclaims copyright to this source code. In place of
28652** a legal notice, here is a blessing:
28653**
28654** May you do good and not evil.
28655** May you find forgiveness for yourself and forgive others.
28656** May you share freely, never taking more than you give.
28657**
28658*************************************************************************
28659** This file contains the C functions that implement mutexes for pthreads
28660*/
28661/* #include "sqliteInt.h" */
28662
28663/*
28664** The code in this file is only used if we are compiling threadsafe
28665** under unix with pthreads.
28666**
28667** Note that this implementation requires a version of pthreads that
28668** supports recursive mutexes.
28669*/
28670#ifdef SQLITE_MUTEX_PTHREADS
28671
28672#include <pthread.h>
28673
28674/*
28675** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
28676** are necessary under two conditions: (1) Debug builds and (2) using
28677** home-grown mutexes. Encapsulate these conditions into a single #define.
28678*/
28679#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
28680# define SQLITE_MUTEX_NREF 1
28681#else
28682# define SQLITE_MUTEX_NREF 0
28683#endif
28684
28685/*
28686** Each recursive mutex is an instance of the following structure.
28687*/
28688struct sqlite3_mutex {
28689 pthread_mutex_t mutex; /* Mutex controlling the lock */
28690#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
28691 int id; /* Mutex type */
28692#endif
28693#if SQLITE_MUTEX_NREF
28694 volatile int nRef; /* Number of entrances */
28695 volatile pthread_t owner; /* Thread that is within this mutex */
28696 int trace; /* True to trace changes */
28697#endif
28698};
28699#if SQLITE_MUTEX_NREF
28700# define SQLITE3_MUTEX_INITIALIZER(id) \
28701 {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
28702#elif defined(SQLITE_ENABLE_API_ARMOR)
28703# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
28704#else
28705#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
28706#endif
28707
28708/*
28709** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
28710** intended for use only inside assert() statements. On some platforms,
28711** there might be race conditions that can cause these routines to
28712** deliver incorrect results. In particular, if pthread_equal() is
28713** not an atomic operation, then these routines might delivery
28714** incorrect results. On most platforms, pthread_equal() is a
28715** comparison of two integers and is therefore atomic. But we are
28716** told that HPUX is not such a platform. If so, then these routines
28717** will not always work correctly on HPUX.
28718**
28719** On those platforms where pthread_equal() is not atomic, SQLite
28720** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
28721** make sure no assert() statements are evaluated and hence these
28722** routines are never called.
28723*/
28724#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
28725static int pthreadMutexHeld(sqlite3_mutex *p){
28726 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
28727}
28728static int pthreadMutexNotheld(sqlite3_mutex *p){
28729 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
28730}
28731#endif
28732
28733/*
28734** Try to provide a memory barrier operation, needed for initialization
28735** and also for the implementation of xShmBarrier in the VFS in cases
28736** where SQLite is compiled without mutexes.
28737*/
28738SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
28739#if defined(SQLITE_MEMORY_BARRIER)
28740 SQLITE_MEMORY_BARRIER;
28741#elif defined(__GNUC__) && GCC_VERSION>=4001000
28742 __sync_synchronize();
28743#endif
28744}
28745
28746/*
28747** Initialize and deinitialize the mutex subsystem.
28748*/
28749static int pthreadMutexInit(void){ return SQLITE_OK; }
28750static int pthreadMutexEnd(void){ return SQLITE_OK; }
28751
28752/*
28753** The sqlite3_mutex_alloc() routine allocates a new
28754** mutex and returns a pointer to it. If it returns NULL
28755** that means that a mutex could not be allocated. SQLite
28756** will unwind its stack and return an error. The argument
28757** to sqlite3_mutex_alloc() is one of these integer constants:
28758**
28759** <ul>
28760** <li> SQLITE_MUTEX_FAST
28761** <li> SQLITE_MUTEX_RECURSIVE
28762** <li> SQLITE_MUTEX_STATIC_MAIN
28763** <li> SQLITE_MUTEX_STATIC_MEM
28764** <li> SQLITE_MUTEX_STATIC_OPEN
28765** <li> SQLITE_MUTEX_STATIC_PRNG
28766** <li> SQLITE_MUTEX_STATIC_LRU
28767** <li> SQLITE_MUTEX_STATIC_PMEM
28768** <li> SQLITE_MUTEX_STATIC_APP1
28769** <li> SQLITE_MUTEX_STATIC_APP2
28770** <li> SQLITE_MUTEX_STATIC_APP3
28771** <li> SQLITE_MUTEX_STATIC_VFS1
28772** <li> SQLITE_MUTEX_STATIC_VFS2
28773** <li> SQLITE_MUTEX_STATIC_VFS3
28774** </ul>
28775**
28776** The first two constants cause sqlite3_mutex_alloc() to create
28777** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
28778** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
28779** The mutex implementation does not need to make a distinction
28780** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
28781** not want to. But SQLite will only request a recursive mutex in
28782** cases where it really needs one. If a faster non-recursive mutex
28783** implementation is available on the host platform, the mutex subsystem
28784** might return such a mutex in response to SQLITE_MUTEX_FAST.
28785**
28786** The other allowed parameters to sqlite3_mutex_alloc() each return
28787** a pointer to a static preexisting mutex. Six static mutexes are
28788** used by the current version of SQLite. Future versions of SQLite
28789** may add additional static mutexes. Static mutexes are for internal
28790** use by SQLite only. Applications that use SQLite mutexes should
28791** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
28792** SQLITE_MUTEX_RECURSIVE.
28793**
28794** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
28795** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
28796** returns a different mutex on every call. But for the static
28797** mutex types, the same mutex is returned on every call that has
28798** the same type number.
28799*/
28800static sqlite3_mutex *pthreadMutexAlloc(int iType){
28801 static sqlite3_mutex staticMutexes[] = {
28802 SQLITE3_MUTEX_INITIALIZER(2),
28803 SQLITE3_MUTEX_INITIALIZER(3),
28804 SQLITE3_MUTEX_INITIALIZER(4),
28805 SQLITE3_MUTEX_INITIALIZER(5),
28806 SQLITE3_MUTEX_INITIALIZER(6),
28807 SQLITE3_MUTEX_INITIALIZER(7),
28808 SQLITE3_MUTEX_INITIALIZER(8),
28809 SQLITE3_MUTEX_INITIALIZER(9),
28810 SQLITE3_MUTEX_INITIALIZER(10),
28811 SQLITE3_MUTEX_INITIALIZER(11),
28812 SQLITE3_MUTEX_INITIALIZER(12),
28813 SQLITE3_MUTEX_INITIALIZER(13)
28814 };
28815 sqlite3_mutex *p;
28816 switch( iType ){
28817 case SQLITE_MUTEX_RECURSIVE: {
28818 p = sqlite3MallocZero( sizeof(*p) );
28819 if( p ){
28820#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28821 /* If recursive mutexes are not available, we will have to
28822 ** build our own. See below. */
28823 pthread_mutex_init(&p->mutex, 0);
28824#else
28825 /* Use a recursive mutex if it is available */
28826 pthread_mutexattr_t recursiveAttr;
28827 pthread_mutexattr_init(attr: &recursiveAttr);
28828 pthread_mutexattr_settype(attr: &recursiveAttr, kind: PTHREAD_MUTEX_RECURSIVE);
28829 pthread_mutex_init(mutex: &p->mutex, mutexattr: &recursiveAttr);
28830 pthread_mutexattr_destroy(attr: &recursiveAttr);
28831#endif
28832#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
28833 p->id = SQLITE_MUTEX_RECURSIVE;
28834#endif
28835 }
28836 break;
28837 }
28838 case SQLITE_MUTEX_FAST: {
28839 p = sqlite3MallocZero( sizeof(*p) );
28840 if( p ){
28841 pthread_mutex_init(mutex: &p->mutex, mutexattr: 0);
28842#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
28843 p->id = SQLITE_MUTEX_FAST;
28844#endif
28845 }
28846 break;
28847 }
28848 default: {
28849#ifdef SQLITE_ENABLE_API_ARMOR
28850 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
28851 (void)SQLITE_MISUSE_BKPT;
28852 return 0;
28853 }
28854#endif
28855 p = &staticMutexes[iType-2];
28856 break;
28857 }
28858 }
28859#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
28860 assert( p==0 || p->id==iType );
28861#endif
28862 return p;
28863}
28864
28865
28866/*
28867** This routine deallocates a previously
28868** allocated mutex. SQLite is careful to deallocate every
28869** mutex that it allocates.
28870*/
28871static void pthreadMutexFree(sqlite3_mutex *p){
28872 assert( p->nRef==0 );
28873#if SQLITE_ENABLE_API_ARMOR
28874 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
28875#endif
28876 {
28877 pthread_mutex_destroy(mutex: &p->mutex);
28878 sqlite3_free(p);
28879 }
28880#ifdef SQLITE_ENABLE_API_ARMOR
28881 else{
28882 (void)SQLITE_MISUSE_BKPT;
28883 }
28884#endif
28885}
28886
28887/*
28888** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
28889** to enter a mutex. If another thread is already within the mutex,
28890** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
28891** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
28892** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
28893** be entered multiple times by the same thread. In such cases the,
28894** mutex must be exited an equal number of times before another thread
28895** can enter. If the same thread tries to enter any other kind of mutex
28896** more than once, the behavior is undefined.
28897*/
28898static void pthreadMutexEnter(sqlite3_mutex *p){
28899 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28900
28901#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28902 /* If recursive mutexes are not available, then we have to grow
28903 ** our own. This implementation assumes that pthread_equal()
28904 ** is atomic - that it cannot be deceived into thinking self
28905 ** and p->owner are equal if p->owner changes between two values
28906 ** that are not equal to self while the comparison is taking place.
28907 ** This implementation also assumes a coherent cache - that
28908 ** separate processes cannot read different values from the same
28909 ** address at the same time. If either of these two conditions
28910 ** are not met, then the mutexes will fail and problems will result.
28911 */
28912 {
28913 pthread_t self = pthread_self();
28914 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28915 p->nRef++;
28916 }else{
28917 pthread_mutex_lock(&p->mutex);
28918 assert( p->nRef==0 );
28919 p->owner = self;
28920 p->nRef = 1;
28921 }
28922 }
28923#else
28924 /* Use the built-in recursive mutexes if they are available.
28925 */
28926 pthread_mutex_lock(mutex: &p->mutex);
28927#if SQLITE_MUTEX_NREF
28928 assert( p->nRef>0 || p->owner==0 );
28929 p->owner = pthread_self();
28930 p->nRef++;
28931#endif
28932#endif
28933
28934#ifdef SQLITE_DEBUG
28935 if( p->trace ){
28936 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28937 }
28938#endif
28939}
28940static int pthreadMutexTry(sqlite3_mutex *p){
28941 int rc;
28942 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28943
28944#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28945 /* If recursive mutexes are not available, then we have to grow
28946 ** our own. This implementation assumes that pthread_equal()
28947 ** is atomic - that it cannot be deceived into thinking self
28948 ** and p->owner are equal if p->owner changes between two values
28949 ** that are not equal to self while the comparison is taking place.
28950 ** This implementation also assumes a coherent cache - that
28951 ** separate processes cannot read different values from the same
28952 ** address at the same time. If either of these two conditions
28953 ** are not met, then the mutexes will fail and problems will result.
28954 */
28955 {
28956 pthread_t self = pthread_self();
28957 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28958 p->nRef++;
28959 rc = SQLITE_OK;
28960 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
28961 assert( p->nRef==0 );
28962 p->owner = self;
28963 p->nRef = 1;
28964 rc = SQLITE_OK;
28965 }else{
28966 rc = SQLITE_BUSY;
28967 }
28968 }
28969#else
28970 /* Use the built-in recursive mutexes if they are available.
28971 */
28972 if( pthread_mutex_trylock(mutex: &p->mutex)==0 ){
28973#if SQLITE_MUTEX_NREF
28974 p->owner = pthread_self();
28975 p->nRef++;
28976#endif
28977 rc = SQLITE_OK;
28978 }else{
28979 rc = SQLITE_BUSY;
28980 }
28981#endif
28982
28983#ifdef SQLITE_DEBUG
28984 if( rc==SQLITE_OK && p->trace ){
28985 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28986 }
28987#endif
28988 return rc;
28989}
28990
28991/*
28992** The sqlite3_mutex_leave() routine exits a mutex that was
28993** previously entered by the same thread. The behavior
28994** is undefined if the mutex is not currently entered or
28995** is not currently allocated. SQLite will never do either.
28996*/
28997static void pthreadMutexLeave(sqlite3_mutex *p){
28998 assert( pthreadMutexHeld(p) );
28999#if SQLITE_MUTEX_NREF
29000 p->nRef--;
29001 if( p->nRef==0 ) p->owner = 0;
29002#endif
29003 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
29004
29005#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
29006 if( p->nRef==0 ){
29007 pthread_mutex_unlock(&p->mutex);
29008 }
29009#else
29010 pthread_mutex_unlock(mutex: &p->mutex);
29011#endif
29012
29013#ifdef SQLITE_DEBUG
29014 if( p->trace ){
29015 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
29016 }
29017#endif
29018}
29019
29020SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
29021 static const sqlite3_mutex_methods sMutex = {
29022 pthreadMutexInit,
29023 pthreadMutexEnd,
29024 pthreadMutexAlloc,
29025 pthreadMutexFree,
29026 pthreadMutexEnter,
29027 pthreadMutexTry,
29028 pthreadMutexLeave,
29029#ifdef SQLITE_DEBUG
29030 pthreadMutexHeld,
29031 pthreadMutexNotheld
29032#else
29033 0,
29034 0
29035#endif
29036 };
29037
29038 return &sMutex;
29039}
29040
29041#endif /* SQLITE_MUTEX_PTHREADS */
29042
29043/************** End of mutex_unix.c ******************************************/
29044/************** Begin file mutex_w32.c ***************************************/
29045/*
29046** 2007 August 14
29047**
29048** The author disclaims copyright to this source code. In place of
29049** a legal notice, here is a blessing:
29050**
29051** May you do good and not evil.
29052** May you find forgiveness for yourself and forgive others.
29053** May you share freely, never taking more than you give.
29054**
29055*************************************************************************
29056** This file contains the C functions that implement mutexes for Win32.
29057*/
29058/* #include "sqliteInt.h" */
29059
29060#if SQLITE_OS_WIN
29061/*
29062** Include code that is common to all os_*.c files
29063*/
29064/* #include "os_common.h" */
29065
29066/*
29067** Include the header file for the Windows VFS.
29068*/
29069/************** Include os_win.h in the middle of mutex_w32.c ****************/
29070/************** Begin file os_win.h ******************************************/
29071/*
29072** 2013 November 25
29073**
29074** The author disclaims copyright to this source code. In place of
29075** a legal notice, here is a blessing:
29076**
29077** May you do good and not evil.
29078** May you find forgiveness for yourself and forgive others.
29079** May you share freely, never taking more than you give.
29080**
29081******************************************************************************
29082**
29083** This file contains code that is specific to Windows.
29084*/
29085#ifndef SQLITE_OS_WIN_H
29086#define SQLITE_OS_WIN_H
29087
29088/*
29089** Include the primary Windows SDK header file.
29090*/
29091#include "windows.h"
29092
29093#ifdef __CYGWIN__
29094# include <sys/cygwin.h>
29095# include <errno.h> /* amalgamator: dontcache */
29096#endif
29097
29098/*
29099** Determine if we are dealing with Windows NT.
29100**
29101** We ought to be able to determine if we are compiling for Windows 9x or
29102** Windows NT using the _WIN32_WINNT macro as follows:
29103**
29104** #if defined(_WIN32_WINNT)
29105** # define SQLITE_OS_WINNT 1
29106** #else
29107** # define SQLITE_OS_WINNT 0
29108** #endif
29109**
29110** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
29111** it ought to, so the above test does not work. We'll just assume that
29112** everything is Windows NT unless the programmer explicitly says otherwise
29113** by setting SQLITE_OS_WINNT to 0.
29114*/
29115#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
29116# define SQLITE_OS_WINNT 1
29117#endif
29118
29119/*
29120** Determine if we are dealing with Windows CE - which has a much reduced
29121** API.
29122*/
29123#if defined(_WIN32_WCE)
29124# define SQLITE_OS_WINCE 1
29125#else
29126# define SQLITE_OS_WINCE 0
29127#endif
29128
29129/*
29130** Determine if we are dealing with WinRT, which provides only a subset of
29131** the full Win32 API.
29132*/
29133#if !defined(SQLITE_OS_WINRT)
29134# define SQLITE_OS_WINRT 0
29135#endif
29136
29137/*
29138** For WinCE, some API function parameters do not appear to be declared as
29139** volatile.
29140*/
29141#if SQLITE_OS_WINCE
29142# define SQLITE_WIN32_VOLATILE
29143#else
29144# define SQLITE_WIN32_VOLATILE volatile
29145#endif
29146
29147/*
29148** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
29149** functions are not available (e.g. those not using MSVC, Cygwin, etc).
29150*/
29151#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
29152 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
29153# define SQLITE_OS_WIN_THREADS 1
29154#else
29155# define SQLITE_OS_WIN_THREADS 0
29156#endif
29157
29158#endif /* SQLITE_OS_WIN_H */
29159
29160/************** End of os_win.h **********************************************/
29161/************** Continuing where we left off in mutex_w32.c ******************/
29162#endif
29163
29164/*
29165** The code in this file is only used if we are compiling multithreaded
29166** on a Win32 system.
29167*/
29168#ifdef SQLITE_MUTEX_W32
29169
29170/*
29171** Each recursive mutex is an instance of the following structure.
29172*/
29173struct sqlite3_mutex {
29174 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
29175 int id; /* Mutex type */
29176#ifdef SQLITE_DEBUG
29177 volatile int nRef; /* Number of entrances */
29178 volatile DWORD owner; /* Thread holding this mutex */
29179 volatile LONG trace; /* True to trace changes */
29180#endif
29181};
29182
29183/*
29184** These are the initializer values used when declaring a "static" mutex
29185** on Win32. It should be noted that all mutexes require initialization
29186** on the Win32 platform.
29187*/
29188#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
29189
29190#ifdef SQLITE_DEBUG
29191#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
29192 0L, (DWORD)0, 0 }
29193#else
29194#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
29195#endif
29196
29197#ifdef SQLITE_DEBUG
29198/*
29199** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29200** intended for use only inside assert() statements.
29201*/
29202static int winMutexHeld(sqlite3_mutex *p){
29203 return p->nRef!=0 && p->owner==GetCurrentThreadId();
29204}
29205
29206static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
29207 return p->nRef==0 || p->owner!=tid;
29208}
29209
29210static int winMutexNotheld(sqlite3_mutex *p){
29211 DWORD tid = GetCurrentThreadId();
29212 return winMutexNotheld2(p, tid);
29213}
29214#endif
29215
29216/*
29217** Try to provide a memory barrier operation, needed for initialization
29218** and also for the xShmBarrier method of the VFS in cases when SQLite is
29219** compiled without mutexes (SQLITE_THREADSAFE=0).
29220*/
29221SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
29222#if defined(SQLITE_MEMORY_BARRIER)
29223 SQLITE_MEMORY_BARRIER;
29224#elif defined(__GNUC__)
29225 __sync_synchronize();
29226#elif MSVC_VERSION>=1300
29227 _ReadWriteBarrier();
29228#elif defined(MemoryBarrier)
29229 MemoryBarrier();
29230#endif
29231}
29232
29233/*
29234** Initialize and deinitialize the mutex subsystem.
29235*/
29236static sqlite3_mutex winMutex_staticMutexes[] = {
29237 SQLITE3_MUTEX_INITIALIZER(2),
29238 SQLITE3_MUTEX_INITIALIZER(3),
29239 SQLITE3_MUTEX_INITIALIZER(4),
29240 SQLITE3_MUTEX_INITIALIZER(5),
29241 SQLITE3_MUTEX_INITIALIZER(6),
29242 SQLITE3_MUTEX_INITIALIZER(7),
29243 SQLITE3_MUTEX_INITIALIZER(8),
29244 SQLITE3_MUTEX_INITIALIZER(9),
29245 SQLITE3_MUTEX_INITIALIZER(10),
29246 SQLITE3_MUTEX_INITIALIZER(11),
29247 SQLITE3_MUTEX_INITIALIZER(12),
29248 SQLITE3_MUTEX_INITIALIZER(13)
29249};
29250
29251static int winMutex_isInit = 0;
29252static int winMutex_isNt = -1; /* <0 means "need to query" */
29253
29254/* As the winMutexInit() and winMutexEnd() functions are called as part
29255** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
29256** "interlocked" magic used here is probably not strictly necessary.
29257*/
29258static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
29259
29260SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
29261SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
29262
29263static int winMutexInit(void){
29264 /* The first to increment to 1 does actual initialization */
29265 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
29266 int i;
29267 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
29268#if SQLITE_OS_WINRT
29269 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
29270#else
29271 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
29272#endif
29273 }
29274 winMutex_isInit = 1;
29275 }else{
29276 /* Another thread is (in the process of) initializing the static
29277 ** mutexes */
29278 while( !winMutex_isInit ){
29279 sqlite3_win32_sleep(1);
29280 }
29281 }
29282 return SQLITE_OK;
29283}
29284
29285static int winMutexEnd(void){
29286 /* The first to decrement to 0 does actual shutdown
29287 ** (which should be the last to shutdown.) */
29288 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
29289 if( winMutex_isInit==1 ){
29290 int i;
29291 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
29292 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
29293 }
29294 winMutex_isInit = 0;
29295 }
29296 }
29297 return SQLITE_OK;
29298}
29299
29300/*
29301** The sqlite3_mutex_alloc() routine allocates a new
29302** mutex and returns a pointer to it. If it returns NULL
29303** that means that a mutex could not be allocated. SQLite
29304** will unwind its stack and return an error. The argument
29305** to sqlite3_mutex_alloc() is one of these integer constants:
29306**
29307** <ul>
29308** <li> SQLITE_MUTEX_FAST
29309** <li> SQLITE_MUTEX_RECURSIVE
29310** <li> SQLITE_MUTEX_STATIC_MAIN
29311** <li> SQLITE_MUTEX_STATIC_MEM
29312** <li> SQLITE_MUTEX_STATIC_OPEN
29313** <li> SQLITE_MUTEX_STATIC_PRNG
29314** <li> SQLITE_MUTEX_STATIC_LRU
29315** <li> SQLITE_MUTEX_STATIC_PMEM
29316** <li> SQLITE_MUTEX_STATIC_APP1
29317** <li> SQLITE_MUTEX_STATIC_APP2
29318** <li> SQLITE_MUTEX_STATIC_APP3
29319** <li> SQLITE_MUTEX_STATIC_VFS1
29320** <li> SQLITE_MUTEX_STATIC_VFS2
29321** <li> SQLITE_MUTEX_STATIC_VFS3
29322** </ul>
29323**
29324** The first two constants cause sqlite3_mutex_alloc() to create
29325** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
29326** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
29327** The mutex implementation does not need to make a distinction
29328** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
29329** not want to. But SQLite will only request a recursive mutex in
29330** cases where it really needs one. If a faster non-recursive mutex
29331** implementation is available on the host platform, the mutex subsystem
29332** might return such a mutex in response to SQLITE_MUTEX_FAST.
29333**
29334** The other allowed parameters to sqlite3_mutex_alloc() each return
29335** a pointer to a static preexisting mutex. Six static mutexes are
29336** used by the current version of SQLite. Future versions of SQLite
29337** may add additional static mutexes. Static mutexes are for internal
29338** use by SQLite only. Applications that use SQLite mutexes should
29339** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
29340** SQLITE_MUTEX_RECURSIVE.
29341**
29342** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
29343** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
29344** returns a different mutex on every call. But for the static
29345** mutex types, the same mutex is returned on every call that has
29346** the same type number.
29347*/
29348static sqlite3_mutex *winMutexAlloc(int iType){
29349 sqlite3_mutex *p;
29350
29351 switch( iType ){
29352 case SQLITE_MUTEX_FAST:
29353 case SQLITE_MUTEX_RECURSIVE: {
29354 p = sqlite3MallocZero( sizeof(*p) );
29355 if( p ){
29356 p->id = iType;
29357#ifdef SQLITE_DEBUG
29358#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
29359 p->trace = 1;
29360#endif
29361#endif
29362#if SQLITE_OS_WINRT
29363 InitializeCriticalSectionEx(&p->mutex, 0, 0);
29364#else
29365 InitializeCriticalSection(&p->mutex);
29366#endif
29367 }
29368 break;
29369 }
29370 default: {
29371#ifdef SQLITE_ENABLE_API_ARMOR
29372 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
29373 (void)SQLITE_MISUSE_BKPT;
29374 return 0;
29375 }
29376#endif
29377 p = &winMutex_staticMutexes[iType-2];
29378#ifdef SQLITE_DEBUG
29379#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
29380 InterlockedCompareExchange(&p->trace, 1, 0);
29381#endif
29382#endif
29383 break;
29384 }
29385 }
29386 assert( p==0 || p->id==iType );
29387 return p;
29388}
29389
29390
29391/*
29392** This routine deallocates a previously
29393** allocated mutex. SQLite is careful to deallocate every
29394** mutex that it allocates.
29395*/
29396static void winMutexFree(sqlite3_mutex *p){
29397 assert( p );
29398 assert( p->nRef==0 && p->owner==0 );
29399 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
29400 DeleteCriticalSection(&p->mutex);
29401 sqlite3_free(p);
29402 }else{
29403#ifdef SQLITE_ENABLE_API_ARMOR
29404 (void)SQLITE_MISUSE_BKPT;
29405#endif
29406 }
29407}
29408
29409/*
29410** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
29411** to enter a mutex. If another thread is already within the mutex,
29412** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
29413** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
29414** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
29415** be entered multiple times by the same thread. In such cases the,
29416** mutex must be exited an equal number of times before another thread
29417** can enter. If the same thread tries to enter any other kind of mutex
29418** more than once, the behavior is undefined.
29419*/
29420static void winMutexEnter(sqlite3_mutex *p){
29421#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
29422 DWORD tid = GetCurrentThreadId();
29423#endif
29424#ifdef SQLITE_DEBUG
29425 assert( p );
29426 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
29427#else
29428 assert( p );
29429#endif
29430 assert( winMutex_isInit==1 );
29431 EnterCriticalSection(&p->mutex);
29432#ifdef SQLITE_DEBUG
29433 assert( p->nRef>0 || p->owner==0 );
29434 p->owner = tid;
29435 p->nRef++;
29436 if( p->trace ){
29437 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
29438 tid, p->id, p, p->trace, p->nRef));
29439 }
29440#endif
29441}
29442
29443static int winMutexTry(sqlite3_mutex *p){
29444#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
29445 DWORD tid = GetCurrentThreadId();
29446#endif
29447 int rc = SQLITE_BUSY;
29448 assert( p );
29449 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
29450 /*
29451 ** The sqlite3_mutex_try() routine is very rarely used, and when it
29452 ** is used it is merely an optimization. So it is OK for it to always
29453 ** fail.
29454 **
29455 ** The TryEnterCriticalSection() interface is only available on WinNT.
29456 ** And some windows compilers complain if you try to use it without
29457 ** first doing some #defines that prevent SQLite from building on Win98.
29458 ** For that reason, we will omit this optimization for now. See
29459 ** ticket #2685.
29460 */
29461#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
29462 assert( winMutex_isInit==1 );
29463 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
29464 if( winMutex_isNt<0 ){
29465 winMutex_isNt = sqlite3_win32_is_nt();
29466 }
29467 assert( winMutex_isNt==0 || winMutex_isNt==1 );
29468 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
29469#ifdef SQLITE_DEBUG
29470 p->owner = tid;
29471 p->nRef++;
29472#endif
29473 rc = SQLITE_OK;
29474 }
29475#else
29476 UNUSED_PARAMETER(p);
29477#endif
29478#ifdef SQLITE_DEBUG
29479 if( p->trace ){
29480 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
29481 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
29482 }
29483#endif
29484 return rc;
29485}
29486
29487/*
29488** The sqlite3_mutex_leave() routine exits a mutex that was
29489** previously entered by the same thread. The behavior
29490** is undefined if the mutex is not currently entered or
29491** is not currently allocated. SQLite will never do either.
29492*/
29493static void winMutexLeave(sqlite3_mutex *p){
29494#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
29495 DWORD tid = GetCurrentThreadId();
29496#endif
29497 assert( p );
29498#ifdef SQLITE_DEBUG
29499 assert( p->nRef>0 );
29500 assert( p->owner==tid );
29501 p->nRef--;
29502 if( p->nRef==0 ) p->owner = 0;
29503 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
29504#endif
29505 assert( winMutex_isInit==1 );
29506 LeaveCriticalSection(&p->mutex);
29507#ifdef SQLITE_DEBUG
29508 if( p->trace ){
29509 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
29510 tid, p->id, p, p->trace, p->nRef));
29511 }
29512#endif
29513}
29514
29515SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
29516 static const sqlite3_mutex_methods sMutex = {
29517 winMutexInit,
29518 winMutexEnd,
29519 winMutexAlloc,
29520 winMutexFree,
29521 winMutexEnter,
29522 winMutexTry,
29523 winMutexLeave,
29524#ifdef SQLITE_DEBUG
29525 winMutexHeld,
29526 winMutexNotheld
29527#else
29528 0,
29529 0
29530#endif
29531 };
29532 return &sMutex;
29533}
29534
29535#endif /* SQLITE_MUTEX_W32 */
29536
29537/************** End of mutex_w32.c *******************************************/
29538/************** Begin file malloc.c ******************************************/
29539/*
29540** 2001 September 15
29541**
29542** The author disclaims copyright to this source code. In place of
29543** a legal notice, here is a blessing:
29544**
29545** May you do good and not evil.
29546** May you find forgiveness for yourself and forgive others.
29547** May you share freely, never taking more than you give.
29548**
29549*************************************************************************
29550**
29551** Memory allocation functions used throughout sqlite.
29552*/
29553/* #include "sqliteInt.h" */
29554/* #include <stdarg.h> */
29555
29556/*
29557** Attempt to release up to n bytes of non-essential memory currently
29558** held by SQLite. An example of non-essential memory is memory used to
29559** cache database pages that are not currently in use.
29560*/
29561SQLITE_API int sqlite3_release_memory(int n){
29562#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
29563 return sqlite3PcacheReleaseMemory(n);
29564#else
29565 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
29566 ** is a no-op returning zero if SQLite is not compiled with
29567 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
29568 UNUSED_PARAMETER(n);
29569 return 0;
29570#endif
29571}
29572
29573/*
29574** Default value of the hard heap limit. 0 means "no limit".
29575*/
29576#ifndef SQLITE_MAX_MEMORY
29577# define SQLITE_MAX_MEMORY 0
29578#endif
29579
29580/*
29581** State information local to the memory allocation subsystem.
29582*/
29583static SQLITE_WSD struct Mem0Global {
29584 sqlite3_mutex *mutex; /* Mutex to serialize access */
29585 sqlite3_int64 alarmThreshold; /* The soft heap limit */
29586 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
29587
29588 /*
29589 ** True if heap is nearly "full" where "full" is defined by the
29590 ** sqlite3_soft_heap_limit() setting.
29591 */
29592 int nearlyFull;
29593} mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
29594
29595#define mem0 GLOBAL(struct Mem0Global, mem0)
29596
29597/*
29598** Return the memory allocator mutex. sqlite3_status() needs it.
29599*/
29600SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
29601 return mem0.mutex;
29602}
29603
29604#ifndef SQLITE_OMIT_DEPRECATED
29605/*
29606** Deprecated external interface. It used to set an alarm callback
29607** that was invoked when memory usage grew too large. Now it is a
29608** no-op.
29609*/
29610SQLITE_API int sqlite3_memory_alarm(
29611 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
29612 void *pArg,
29613 sqlite3_int64 iThreshold
29614){
29615 (void)xCallback;
29616 (void)pArg;
29617 (void)iThreshold;
29618 return SQLITE_OK;
29619}
29620#endif
29621
29622/*
29623** Set the soft heap-size limit for the library. An argument of
29624** zero disables the limit. A negative argument is a no-op used to
29625** obtain the return value.
29626**
29627** The return value is the value of the heap limit just before this
29628** interface was called.
29629**
29630** If the hard heap limit is enabled, then the soft heap limit cannot
29631** be disabled nor raised above the hard heap limit.
29632*/
29633SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
29634 sqlite3_int64 priorLimit;
29635 sqlite3_int64 excess;
29636 sqlite3_int64 nUsed;
29637#ifndef SQLITE_OMIT_AUTOINIT
29638 int rc = sqlite3_initialize();
29639 if( rc ) return -1;
29640#endif
29641 sqlite3_mutex_enter(mem0.mutex);
29642 priorLimit = mem0.alarmThreshold;
29643 if( n<0 ){
29644 sqlite3_mutex_leave(mem0.mutex);
29645 return priorLimit;
29646 }
29647 if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
29648 n = mem0.hardLimit;
29649 }
29650 mem0.alarmThreshold = n;
29651 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
29652 AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed);
29653 sqlite3_mutex_leave(mem0.mutex);
29654 excess = sqlite3_memory_used() - n;
29655 if( excess>0 ) sqlite3_release_memory(n: (int)(excess & 0x7fffffff));
29656 return priorLimit;
29657}
29658SQLITE_API void sqlite3_soft_heap_limit(int n){
29659 if( n<0 ) n = 0;
29660 sqlite3_soft_heap_limit64(n);
29661}
29662
29663/*
29664** Set the hard heap-size limit for the library. An argument of zero
29665** disables the hard heap limit. A negative argument is a no-op used
29666** to obtain the return value without affecting the hard heap limit.
29667**
29668** The return value is the value of the hard heap limit just prior to
29669** calling this interface.
29670**
29671** Setting the hard heap limit will also activate the soft heap limit
29672** and constrain the soft heap limit to be no more than the hard heap
29673** limit.
29674*/
29675SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
29676 sqlite3_int64 priorLimit;
29677#ifndef SQLITE_OMIT_AUTOINIT
29678 int rc = sqlite3_initialize();
29679 if( rc ) return -1;
29680#endif
29681 sqlite3_mutex_enter(mem0.mutex);
29682 priorLimit = mem0.hardLimit;
29683 if( n>=0 ){
29684 mem0.hardLimit = n;
29685 if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
29686 mem0.alarmThreshold = n;
29687 }
29688 }
29689 sqlite3_mutex_leave(mem0.mutex);
29690 return priorLimit;
29691}
29692
29693
29694/*
29695** Initialize the memory allocation subsystem.
29696*/
29697SQLITE_PRIVATE int sqlite3MallocInit(void){
29698 int rc;
29699 if( sqlite3GlobalConfig.m.xMalloc==0 ){
29700 sqlite3MemSetDefault();
29701 }
29702 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
29703 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
29704 || sqlite3GlobalConfig.nPage<=0 ){
29705 sqlite3GlobalConfig.pPage = 0;
29706 sqlite3GlobalConfig.szPage = 0;
29707 }
29708 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
29709 if( rc!=SQLITE_OK ) memset(s: &mem0, c: 0, n: sizeof(mem0));
29710 return rc;
29711}
29712
29713/*
29714** Return true if the heap is currently under memory pressure - in other
29715** words if the amount of heap used is close to the limit set by
29716** sqlite3_soft_heap_limit().
29717*/
29718SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
29719 return AtomicLoad(&mem0.nearlyFull);
29720}
29721
29722/*
29723** Deinitialize the memory allocation subsystem.
29724*/
29725SQLITE_PRIVATE void sqlite3MallocEnd(void){
29726 if( sqlite3GlobalConfig.m.xShutdown ){
29727 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
29728 }
29729 memset(s: &mem0, c: 0, n: sizeof(mem0));
29730}
29731
29732/*
29733** Return the amount of memory currently checked out.
29734*/
29735SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
29736 sqlite3_int64 res, mx;
29737 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, pCurrent: &res, pHighwater: &mx, resetFlag: 0);
29738 return res;
29739}
29740
29741/*
29742** Return the maximum amount of memory that has ever been
29743** checked out since either the beginning of this process
29744** or since the most recent reset.
29745*/
29746SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
29747 sqlite3_int64 res, mx;
29748 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, pCurrent: &res, pHighwater: &mx, resetFlag);
29749 return mx;
29750}
29751
29752/*
29753** Trigger the alarm
29754*/
29755static void sqlite3MallocAlarm(int nByte){
29756 if( mem0.alarmThreshold<=0 ) return;
29757 sqlite3_mutex_leave(mem0.mutex);
29758 sqlite3_release_memory(n: nByte);
29759 sqlite3_mutex_enter(mem0.mutex);
29760}
29761
29762/*
29763** Do a memory allocation with statistics and alarms. Assume the
29764** lock is already held.
29765*/
29766static void mallocWithAlarm(int n, void **pp){
29767 void *p;
29768 int nFull;
29769 assert( sqlite3_mutex_held(mem0.mutex) );
29770 assert( n>0 );
29771
29772 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
29773 ** implementation of malloc_good_size(), which must be called in debug
29774 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
29775 ** or else a crash results. Hence, do not attempt to optimize out the
29776 ** following xRoundup() call. */
29777 nFull = sqlite3GlobalConfig.m.xRoundup(n);
29778
29779 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, X: n);
29780 if( mem0.alarmThreshold>0 ){
29781 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
29782 if( nUsed >= mem0.alarmThreshold - nFull ){
29783 AtomicStore(&mem0.nearlyFull, 1);
29784 sqlite3MallocAlarm(nByte: nFull);
29785 if( mem0.hardLimit ){
29786 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
29787 if( nUsed >= mem0.hardLimit - nFull ){
29788 *pp = 0;
29789 return;
29790 }
29791 }
29792 }else{
29793 AtomicStore(&mem0.nearlyFull, 0);
29794 }
29795 }
29796 p = sqlite3GlobalConfig.m.xMalloc(nFull);
29797#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
29798 if( p==0 && mem0.alarmThreshold>0 ){
29799 sqlite3MallocAlarm(nFull);
29800 p = sqlite3GlobalConfig.m.xMalloc(nFull);
29801 }
29802#endif
29803 if( p ){
29804 nFull = sqlite3MallocSize(p);
29805 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, N: nFull);
29806 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, N: 1);
29807 }
29808 *pp = p;
29809}
29810
29811/*
29812** Maximum size of any single memory allocation.
29813**
29814** This is not a limit on the total amount of memory used. This is
29815** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
29816**
29817** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
29818** This provides a 256-byte safety margin for defense against 32-bit
29819** signed integer overflow bugs when computing memory allocation sizes.
29820** Paranoid applications might want to reduce the maximum allocation size
29821** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
29822** or even smaller would be reasonable upper bounds on the size of a memory
29823** allocations for most applications.
29824*/
29825#ifndef SQLITE_MAX_ALLOCATION_SIZE
29826# define SQLITE_MAX_ALLOCATION_SIZE 2147483391
29827#endif
29828#if SQLITE_MAX_ALLOCATION_SIZE>2147483391
29829# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
29830#endif
29831
29832/*
29833** Allocate memory. This routine is like sqlite3_malloc() except that it
29834** assumes the memory subsystem has already been initialized.
29835*/
29836SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
29837 void *p;
29838 if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
29839 p = 0;
29840 }else if( sqlite3GlobalConfig.bMemstat ){
29841 sqlite3_mutex_enter(mem0.mutex);
29842 mallocWithAlarm(n: (int)n, pp: &p);
29843 sqlite3_mutex_leave(mem0.mutex);
29844 }else{
29845 p = sqlite3GlobalConfig.m.xMalloc((int)n);
29846 }
29847 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
29848 return p;
29849}
29850
29851/*
29852** This version of the memory allocation is for use by the application.
29853** First make sure the memory subsystem is initialized, then do the
29854** allocation.
29855*/
29856SQLITE_API void *sqlite3_malloc(int n){
29857#ifndef SQLITE_OMIT_AUTOINIT
29858 if( sqlite3_initialize() ) return 0;
29859#endif
29860 return n<=0 ? 0 : sqlite3Malloc(n);
29861}
29862SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
29863#ifndef SQLITE_OMIT_AUTOINIT
29864 if( sqlite3_initialize() ) return 0;
29865#endif
29866 return sqlite3Malloc(n);
29867}
29868
29869/*
29870** TRUE if p is a lookaside memory allocation from db
29871*/
29872#ifndef SQLITE_OMIT_LOOKASIDE
29873static int isLookaside(sqlite3 *db, const void *p){
29874 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
29875}
29876#else
29877#define isLookaside(A,B) 0
29878#endif
29879
29880/*
29881** Return the size of a memory allocation previously obtained from
29882** sqlite3Malloc() or sqlite3_malloc().
29883*/
29884SQLITE_PRIVATE int sqlite3MallocSize(const void *p){
29885 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29886 return sqlite3GlobalConfig.m.xSize((void*)p);
29887}
29888static int lookasideMallocSize(sqlite3 *db, const void *p){
29889#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29890 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
29891#else
29892 return db->lookaside.szTrue;
29893#endif
29894}
29895SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){
29896 assert( p!=0 );
29897#ifdef SQLITE_DEBUG
29898 if( db==0 ){
29899 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29900 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29901 }else if( !isLookaside(db,p) ){
29902 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29903 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29904 }
29905#endif
29906 if( db ){
29907 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
29908#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29909 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29910 assert( sqlite3_mutex_held(db->mutex) );
29911 return LOOKASIDE_SMALL;
29912 }
29913#endif
29914 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29915 assert( sqlite3_mutex_held(db->mutex) );
29916 return db->lookaside.szTrue;
29917 }
29918 }
29919 }
29920 return sqlite3GlobalConfig.m.xSize((void*)p);
29921}
29922SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
29923 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29924 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29925 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
29926}
29927
29928/*
29929** Free memory previously obtained from sqlite3Malloc().
29930*/
29931SQLITE_API void sqlite3_free(void *p){
29932 if( p==0 ) return; /* IMP: R-49053-54554 */
29933 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29934 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29935 if( sqlite3GlobalConfig.bMemstat ){
29936 sqlite3_mutex_enter(mem0.mutex);
29937 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, N: sqlite3MallocSize(p));
29938 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, N: 1);
29939 sqlite3GlobalConfig.m.xFree(p);
29940 sqlite3_mutex_leave(mem0.mutex);
29941 }else{
29942 sqlite3GlobalConfig.m.xFree(p);
29943 }
29944}
29945
29946/*
29947** Add the size of memory allocation "p" to the count in
29948** *db->pnBytesFreed.
29949*/
29950static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
29951 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
29952}
29953
29954/*
29955** Free memory that might be associated with a particular database
29956** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
29957** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
29958*/
29959SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
29960 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29961 assert( p!=0 );
29962 if( db ){
29963 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29964#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29965 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29966 LookasideSlot *pBuf = (LookasideSlot*)p;
29967 assert( db->pnBytesFreed==0 );
29968#ifdef SQLITE_DEBUG
29969 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
29970#endif
29971 pBuf->pNext = db->lookaside.pSmallFree;
29972 db->lookaside.pSmallFree = pBuf;
29973 return;
29974 }
29975#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
29976 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29977 LookasideSlot *pBuf = (LookasideSlot*)p;
29978 assert( db->pnBytesFreed==0 );
29979#ifdef SQLITE_DEBUG
29980 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29981#endif
29982 pBuf->pNext = db->lookaside.pFree;
29983 db->lookaside.pFree = pBuf;
29984 return;
29985 }
29986 }
29987 if( db->pnBytesFreed ){
29988 measureAllocationSize(db, p);
29989 return;
29990 }
29991 }
29992 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29993 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29994 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
29995 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29996 sqlite3_free(p);
29997}
29998SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
29999 assert( db!=0 );
30000 assert( sqlite3_mutex_held(db->mutex) );
30001 assert( p!=0 );
30002 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
30003#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30004 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
30005 LookasideSlot *pBuf = (LookasideSlot*)p;
30006 assert( db->pnBytesFreed==0 );
30007#ifdef SQLITE_DEBUG
30008 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
30009#endif
30010 pBuf->pNext = db->lookaside.pSmallFree;
30011 db->lookaside.pSmallFree = pBuf;
30012 return;
30013 }
30014#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
30015 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
30016 LookasideSlot *pBuf = (LookasideSlot*)p;
30017 assert( db->pnBytesFreed==0 );
30018#ifdef SQLITE_DEBUG
30019 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
30020#endif
30021 pBuf->pNext = db->lookaside.pFree;
30022 db->lookaside.pFree = pBuf;
30023 return;
30024 }
30025 }
30026 if( db->pnBytesFreed ){
30027 measureAllocationSize(db, p);
30028 return;
30029 }
30030 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
30031 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
30032 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
30033 sqlite3_free(p);
30034}
30035SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
30036 assert( db==0 || sqlite3_mutex_held(db->mutex) );
30037 if( p ) sqlite3DbFreeNN(db, p);
30038}
30039
30040/*
30041** Change the size of an existing memory allocation
30042*/
30043SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
30044 int nOld, nNew, nDiff;
30045 void *pNew;
30046 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
30047 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
30048 if( pOld==0 ){
30049 return sqlite3Malloc(n: nBytes); /* IMP: R-04300-56712 */
30050 }
30051 if( nBytes==0 ){
30052 sqlite3_free(p: pOld); /* IMP: R-26507-47431 */
30053 return 0;
30054 }
30055 if( nBytes>=0x7fffff00 ){
30056 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
30057 return 0;
30058 }
30059 nOld = sqlite3MallocSize(p: pOld);
30060 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
30061 ** argument to xRealloc is always a value returned by a prior call to
30062 ** xRoundup. */
30063 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
30064 if( nOld==nNew ){
30065 pNew = pOld;
30066 }else if( sqlite3GlobalConfig.bMemstat ){
30067 sqlite3_int64 nUsed;
30068 sqlite3_mutex_enter(mem0.mutex);
30069 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, X: (int)nBytes);
30070 nDiff = nNew - nOld;
30071 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
30072 mem0.alarmThreshold-nDiff ){
30073 sqlite3MallocAlarm(nByte: nDiff);
30074 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
30075 sqlite3_mutex_leave(mem0.mutex);
30076 return 0;
30077 }
30078 }
30079 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
30080#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
30081 if( pNew==0 && mem0.alarmThreshold>0 ){
30082 sqlite3MallocAlarm((int)nBytes);
30083 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
30084 }
30085#endif
30086 if( pNew ){
30087 nNew = sqlite3MallocSize(p: pNew);
30088 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, N: nNew-nOld);
30089 }
30090 sqlite3_mutex_leave(mem0.mutex);
30091 }else{
30092 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
30093 }
30094 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
30095 return pNew;
30096}
30097
30098/*
30099** The public interface to sqlite3Realloc. Make sure that the memory
30100** subsystem is initialized prior to invoking sqliteRealloc.
30101*/
30102SQLITE_API void *sqlite3_realloc(void *pOld, int n){
30103#ifndef SQLITE_OMIT_AUTOINIT
30104 if( sqlite3_initialize() ) return 0;
30105#endif
30106 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
30107 return sqlite3Realloc(pOld, nBytes: n);
30108}
30109SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
30110#ifndef SQLITE_OMIT_AUTOINIT
30111 if( sqlite3_initialize() ) return 0;
30112#endif
30113 return sqlite3Realloc(pOld, nBytes: n);
30114}
30115
30116
30117/*
30118** Allocate and zero memory.
30119*/
30120SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
30121 void *p = sqlite3Malloc(n);
30122 if( p ){
30123 memset(s: p, c: 0, n: (size_t)n);
30124 }
30125 return p;
30126}
30127
30128/*
30129** Allocate and zero memory. If the allocation fails, make
30130** the mallocFailed flag in the connection pointer.
30131*/
30132SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
30133 void *p;
30134 testcase( db==0 );
30135 p = sqlite3DbMallocRaw(db, n);
30136 if( p ) memset(s: p, c: 0, n: (size_t)n);
30137 return p;
30138}
30139
30140
30141/* Finish the work of sqlite3DbMallocRawNN for the unusual and
30142** slower case when the allocation cannot be fulfilled using lookaside.
30143*/
30144static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
30145 void *p;
30146 assert( db!=0 );
30147 p = sqlite3Malloc(n);
30148 if( !p ) sqlite3OomFault(db);
30149 sqlite3MemdebugSetType(p,
30150 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
30151 return p;
30152}
30153
30154/*
30155** Allocate memory, either lookaside (if possible) or heap.
30156** If the allocation fails, set the mallocFailed flag in
30157** the connection pointer.
30158**
30159** If db!=0 and db->mallocFailed is true (indicating a prior malloc
30160** failure on the same database connection) then always return 0.
30161** Hence for a particular database connection, once malloc starts
30162** failing, it fails consistently until mallocFailed is reset.
30163** This is an important assumption. There are many places in the
30164** code that do things like this:
30165**
30166** int *a = (int*)sqlite3DbMallocRaw(db, 100);
30167** int *b = (int*)sqlite3DbMallocRaw(db, 200);
30168** if( b ) a[10] = 9;
30169**
30170** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
30171** that all prior mallocs (ex: "a") worked too.
30172**
30173** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
30174** not a NULL pointer.
30175*/
30176SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
30177 void *p;
30178 if( db ) return sqlite3DbMallocRawNN(db, n);
30179 p = sqlite3Malloc(n);
30180 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
30181 return p;
30182}
30183SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
30184#ifndef SQLITE_OMIT_LOOKASIDE
30185 LookasideSlot *pBuf;
30186 assert( db!=0 );
30187 assert( sqlite3_mutex_held(db->mutex) );
30188 assert( db->pnBytesFreed==0 );
30189 if( n>db->lookaside.sz ){
30190 if( !db->lookaside.bDisable ){
30191 db->lookaside.anStat[1]++;
30192 }else if( db->mallocFailed ){
30193 return 0;
30194 }
30195 return dbMallocRawFinish(db, n);
30196 }
30197#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30198 if( n<=LOOKASIDE_SMALL ){
30199 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
30200 db->lookaside.pSmallFree = pBuf->pNext;
30201 db->lookaside.anStat[0]++;
30202 return (void*)pBuf;
30203 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
30204 db->lookaside.pSmallInit = pBuf->pNext;
30205 db->lookaside.anStat[0]++;
30206 return (void*)pBuf;
30207 }
30208 }
30209#endif
30210 if( (pBuf = db->lookaside.pFree)!=0 ){
30211 db->lookaside.pFree = pBuf->pNext;
30212 db->lookaside.anStat[0]++;
30213 return (void*)pBuf;
30214 }else if( (pBuf = db->lookaside.pInit)!=0 ){
30215 db->lookaside.pInit = pBuf->pNext;
30216 db->lookaside.anStat[0]++;
30217 return (void*)pBuf;
30218 }else{
30219 db->lookaside.anStat[2]++;
30220 }
30221#else
30222 assert( db!=0 );
30223 assert( sqlite3_mutex_held(db->mutex) );
30224 assert( db->pnBytesFreed==0 );
30225 if( db->mallocFailed ){
30226 return 0;
30227 }
30228#endif
30229 return dbMallocRawFinish(db, n);
30230}
30231
30232/* Forward declaration */
30233static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
30234
30235/*
30236** Resize the block of memory pointed to by p to n bytes. If the
30237** resize fails, set the mallocFailed flag in the connection object.
30238*/
30239SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
30240 assert( db!=0 );
30241 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
30242 assert( sqlite3_mutex_held(db->mutex) );
30243 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
30244#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30245 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
30246 if( n<=LOOKASIDE_SMALL ) return p;
30247 }else
30248#endif
30249 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
30250 if( n<=db->lookaside.szTrue ) return p;
30251 }
30252 }
30253 return dbReallocFinish(db, p, n);
30254}
30255static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
30256 void *pNew = 0;
30257 assert( db!=0 );
30258 assert( p!=0 );
30259 if( db->mallocFailed==0 ){
30260 if( isLookaside(db, p) ){
30261 pNew = sqlite3DbMallocRawNN(db, n);
30262 if( pNew ){
30263 memcpy(dest: pNew, src: p, n: lookasideMallocSize(db, p));
30264 sqlite3DbFree(db, p);
30265 }
30266 }else{
30267 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
30268 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
30269 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
30270 pNew = sqlite3Realloc(pOld: p, nBytes: n);
30271 if( !pNew ){
30272 sqlite3OomFault(db);
30273 }
30274 sqlite3MemdebugSetType(pNew,
30275 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
30276 }
30277 }
30278 return pNew;
30279}
30280
30281/*
30282** Attempt to reallocate p. If the reallocation fails, then free p
30283** and set the mallocFailed flag in the database connection.
30284*/
30285SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
30286 void *pNew;
30287 pNew = sqlite3DbRealloc(db, p, n);
30288 if( !pNew ){
30289 sqlite3DbFree(db, p);
30290 }
30291 return pNew;
30292}
30293
30294/*
30295** Make a copy of a string in memory obtained from sqliteMalloc(). These
30296** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
30297** is because when memory debugging is turned on, these two functions are
30298** called via macros that record the current file and line number in the
30299** ThreadData structure.
30300*/
30301SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
30302 char *zNew;
30303 size_t n;
30304 if( z==0 ){
30305 return 0;
30306 }
30307 n = strlen(s: z) + 1;
30308 zNew = sqlite3DbMallocRaw(db, n);
30309 if( zNew ){
30310 memcpy(dest: zNew, src: z, n: n);
30311 }
30312 return zNew;
30313}
30314SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
30315 char *zNew;
30316 assert( db!=0 );
30317 assert( z!=0 || n==0 );
30318 assert( (n&0x7fffffff)==n );
30319 zNew = z ? sqlite3DbMallocRawNN(db, n: n+1) : 0;
30320 if( zNew ){
30321 memcpy(dest: zNew, src: z, n: (size_t)n);
30322 zNew[n] = 0;
30323 }
30324 return zNew;
30325}
30326
30327/*
30328** The text between zStart and zEnd represents a phrase within a larger
30329** SQL statement. Make a copy of this phrase in space obtained form
30330** sqlite3DbMalloc(). Omit leading and trailing whitespace.
30331*/
30332SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
30333 int n;
30334#ifdef SQLITE_DEBUG
30335 /* Because of the way the parser works, the span is guaranteed to contain
30336 ** at least one non-space character */
30337 for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]<zEnd ); }
30338#endif
30339 while( sqlite3Isspace(zStart[0]) ) zStart++;
30340 n = (int)(zEnd - zStart);
30341 while( sqlite3Isspace(zStart[n-1]) ) n--;
30342 return sqlite3DbStrNDup(db, z: zStart, n);
30343}
30344
30345/*
30346** Free any prior content in *pz and replace it with a copy of zNew.
30347*/
30348SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
30349 char *z = sqlite3DbStrDup(db, z: zNew);
30350 sqlite3DbFree(db, p: *pz);
30351 *pz = z;
30352}
30353
30354/*
30355** Call this routine to record the fact that an OOM (out-of-memory) error
30356** has happened. This routine will set db->mallocFailed, and also
30357** temporarily disable the lookaside memory allocator and interrupt
30358** any running VDBEs.
30359**
30360** Always return a NULL pointer so that this routine can be invoked using
30361**
30362** return sqlite3OomFault(db);
30363**
30364** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
30365** common case where no OOM occurs.
30366*/
30367SQLITE_PRIVATE void *sqlite3OomFault(sqlite3 *db){
30368 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
30369 db->mallocFailed = 1;
30370 if( db->nVdbeExec>0 ){
30371 AtomicStore(&db->u1.isInterrupted, 1);
30372 }
30373 DisableLookaside;
30374 if( db->pParse ){
30375 Parse *pParse;
30376 sqlite3ErrorMsg(db->pParse, "out of memory");
30377 db->pParse->rc = SQLITE_NOMEM_BKPT;
30378 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
30379 pParse->nErr++;
30380 pParse->rc = SQLITE_NOMEM;
30381 }
30382 }
30383 }
30384 return 0;
30385}
30386
30387/*
30388** This routine reactivates the memory allocator and clears the
30389** db->mallocFailed flag as necessary.
30390**
30391** The memory allocator is not restarted if there are running
30392** VDBEs.
30393*/
30394SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
30395 if( db->mallocFailed && db->nVdbeExec==0 ){
30396 db->mallocFailed = 0;
30397 AtomicStore(&db->u1.isInterrupted, 0);
30398 assert( db->lookaside.bDisable>0 );
30399 EnableLookaside;
30400 }
30401}
30402
30403/*
30404** Take actions at the end of an API call to deal with error codes.
30405*/
30406static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
30407 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
30408 sqlite3OomClear(db);
30409 sqlite3Error(db, SQLITE_NOMEM);
30410 return SQLITE_NOMEM_BKPT;
30411 }
30412 return rc & db->errMask;
30413}
30414
30415/*
30416** This function must be called before exiting any API function (i.e.
30417** returning control to the user) that has called sqlite3_malloc or
30418** sqlite3_realloc.
30419**
30420** The returned value is normally a copy of the second argument to this
30421** function. However, if a malloc() failure has occurred since the previous
30422** invocation SQLITE_NOMEM is returned instead.
30423**
30424** If an OOM as occurred, then the connection error-code (the value
30425** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
30426*/
30427SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
30428 /* If the db handle must hold the connection handle mutex here.
30429 ** Otherwise the read (and possible write) of db->mallocFailed
30430 ** is unsafe, as is the call to sqlite3Error().
30431 */
30432 assert( db!=0 );
30433 assert( sqlite3_mutex_held(db->mutex) );
30434 if( db->mallocFailed || rc ){
30435 return apiHandleError(db, rc);
30436 }
30437 return rc & db->errMask;
30438}
30439
30440/************** End of malloc.c **********************************************/
30441/************** Begin file printf.c ******************************************/
30442/*
30443** The "printf" code that follows dates from the 1980's. It is in
30444** the public domain.
30445**
30446**************************************************************************
30447**
30448** This file contains code for a set of "printf"-like routines. These
30449** routines format strings much like the printf() from the standard C
30450** library, though the implementation here has enhancements to support
30451** SQLite.
30452*/
30453/* #include "sqliteInt.h" */
30454
30455/*
30456** Conversion types fall into various categories as defined by the
30457** following enumeration.
30458*/
30459#define etRADIX 0 /* non-decimal integer types. %x %o */
30460#define etFLOAT 1 /* Floating point. %f */
30461#define etEXP 2 /* Exponentional notation. %e and %E */
30462#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
30463#define etSIZE 4 /* Return number of characters processed so far. %n */
30464#define etSTRING 5 /* Strings. %s */
30465#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
30466#define etPERCENT 7 /* Percent symbol. %% */
30467#define etCHARX 8 /* Characters. %c */
30468/* The rest are extensions, not normally found in printf() */
30469#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
30470#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
30471 NULL pointers replaced by SQL NULL. %Q */
30472#define etTOKEN 11 /* a pointer to a Token structure */
30473#define etSRCITEM 12 /* a pointer to a SrcItem */
30474#define etPOINTER 13 /* The %p conversion */
30475#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
30476#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
30477#define etDECIMAL 16 /* %d or %u, but not %x, %o */
30478
30479#define etINVALID 17 /* Any unrecognized conversion type */
30480
30481
30482/*
30483** An "etByte" is an 8-bit unsigned value.
30484*/
30485typedef unsigned char etByte;
30486
30487/*
30488** Each builtin conversion character (ex: the 'd' in "%d") is described
30489** by an instance of the following structure
30490*/
30491typedef struct et_info { /* Information about each format field */
30492 char fmttype; /* The format field code letter */
30493 etByte base; /* The base for radix conversion */
30494 etByte flags; /* One or more of FLAG_ constants below */
30495 etByte type; /* Conversion paradigm */
30496 etByte charset; /* Offset into aDigits[] of the digits string */
30497 etByte prefix; /* Offset into aPrefix[] of the prefix string */
30498} et_info;
30499
30500/*
30501** Allowed values for et_info.flags
30502*/
30503#define FLAG_SIGNED 1 /* True if the value to convert is signed */
30504#define FLAG_STRING 4 /* Allow infinite precision */
30505
30506
30507/*
30508** The following table is searched linearly, so it is good to put the
30509** most frequently used conversion types first.
30510*/
30511static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
30512static const char aPrefix[] = "-x0\000X0";
30513static const et_info fmtinfo[] = {
30514 { 'd', 10, 1, etDECIMAL, 0, 0 },
30515 { 's', 0, 4, etSTRING, 0, 0 },
30516 { 'g', 0, 1, etGENERIC, 30, 0 },
30517 { 'z', 0, 4, etDYNSTRING, 0, 0 },
30518 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
30519 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
30520 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
30521 { 'c', 0, 0, etCHARX, 0, 0 },
30522 { 'o', 8, 0, etRADIX, 0, 2 },
30523 { 'u', 10, 0, etDECIMAL, 0, 0 },
30524 { 'x', 16, 0, etRADIX, 16, 1 },
30525 { 'X', 16, 0, etRADIX, 0, 4 },
30526#ifndef SQLITE_OMIT_FLOATING_POINT
30527 { 'f', 0, 1, etFLOAT, 0, 0 },
30528 { 'e', 0, 1, etEXP, 30, 0 },
30529 { 'E', 0, 1, etEXP, 14, 0 },
30530 { 'G', 0, 1, etGENERIC, 14, 0 },
30531#endif
30532 { 'i', 10, 1, etDECIMAL, 0, 0 },
30533 { 'n', 0, 0, etSIZE, 0, 0 },
30534 { '%', 0, 0, etPERCENT, 0, 0 },
30535 { 'p', 16, 0, etPOINTER, 0, 1 },
30536
30537 /* All the rest are undocumented and are for internal use only */
30538 { 'T', 0, 0, etTOKEN, 0, 0 },
30539 { 'S', 0, 0, etSRCITEM, 0, 0 },
30540 { 'r', 10, 1, etORDINAL, 0, 0 },
30541};
30542
30543/* Notes:
30544**
30545** %S Takes a pointer to SrcItem. Shows name or database.name
30546** %!S Like %S but prefer the zName over the zAlias
30547*/
30548
30549/*
30550** Set the StrAccum object to an error mode.
30551*/
30552SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
30553 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
30554 p->accError = eError;
30555 if( p->mxAlloc ) sqlite3_str_reset(p);
30556 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
30557}
30558
30559/*
30560** Extra argument values from a PrintfArguments object
30561*/
30562static sqlite3_int64 getIntArg(PrintfArguments *p){
30563 if( p->nArg<=p->nUsed ) return 0;
30564 return sqlite3_value_int64(p->apArg[p->nUsed++]);
30565}
30566static double getDoubleArg(PrintfArguments *p){
30567 if( p->nArg<=p->nUsed ) return 0.0;
30568 return sqlite3_value_double(p->apArg[p->nUsed++]);
30569}
30570static char *getTextArg(PrintfArguments *p){
30571 if( p->nArg<=p->nUsed ) return 0;
30572 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
30573}
30574
30575/*
30576** Allocate memory for a temporary buffer needed for printf rendering.
30577**
30578** If the requested size of the temp buffer is larger than the size
30579** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
30580** Do the size check before the memory allocation to prevent rogue
30581** SQL from requesting large allocations using the precision or width
30582** field of the printf() function.
30583*/
30584static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
30585 char *z;
30586 if( pAccum->accError ) return 0;
30587 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
30588 sqlite3StrAccumSetError(p: pAccum, SQLITE_TOOBIG);
30589 return 0;
30590 }
30591 z = sqlite3DbMallocRaw(db: pAccum->db, n);
30592 if( z==0 ){
30593 sqlite3StrAccumSetError(p: pAccum, SQLITE_NOMEM);
30594 }
30595 return z;
30596}
30597
30598/*
30599** On machines with a small stack size, you can redefine the
30600** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
30601*/
30602#ifndef SQLITE_PRINT_BUF_SIZE
30603# define SQLITE_PRINT_BUF_SIZE 70
30604#endif
30605#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
30606
30607/*
30608** Hard limit on the precision of floating-point conversions.
30609*/
30610#ifndef SQLITE_PRINTF_PRECISION_LIMIT
30611# define SQLITE_FP_PRECISION_LIMIT 100000000
30612#endif
30613
30614/*
30615** Render a string given by "fmt" into the StrAccum object.
30616*/
30617SQLITE_API void sqlite3_str_vappendf(
30618 sqlite3_str *pAccum, /* Accumulate results here */
30619 const char *fmt, /* Format string */
30620 va_list ap /* arguments */
30621){
30622 int c; /* Next character in the format string */
30623 char *bufpt; /* Pointer to the conversion buffer */
30624 int precision; /* Precision of the current field */
30625 int length; /* Length of the field */
30626 int idx; /* A general purpose loop counter */
30627 int width; /* Width of the current field */
30628 etByte flag_leftjustify; /* True if "-" flag is present */
30629 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
30630 etByte flag_alternateform; /* True if "#" flag is present */
30631 etByte flag_altform2; /* True if "!" flag is present */
30632 etByte flag_zeropad; /* True if field width constant starts with zero */
30633 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
30634 etByte done; /* Loop termination flag */
30635 etByte cThousand; /* Thousands separator for %d and %u */
30636 etByte xtype = etINVALID; /* Conversion paradigm */
30637 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
30638 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
30639 sqlite_uint64 longvalue; /* Value for integer types */
30640 double realvalue; /* Value for real types */
30641 const et_info *infop; /* Pointer to the appropriate info structure */
30642 char *zOut; /* Rendering buffer */
30643 int nOut; /* Size of the rendering buffer */
30644 char *zExtra = 0; /* Malloced memory used by some conversion */
30645 int exp, e2; /* exponent of real numbers */
30646 etByte flag_dp; /* True if decimal point should be shown */
30647 etByte flag_rtz; /* True if trailing zeros should be removed */
30648
30649 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
30650 char buf[etBUFSIZE]; /* Conversion buffer */
30651
30652 /* pAccum never starts out with an empty buffer that was obtained from
30653 ** malloc(). This precondition is required by the mprintf("%z...")
30654 ** optimization. */
30655 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
30656
30657 bufpt = 0;
30658 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
30659 pArgList = va_arg(ap, PrintfArguments*);
30660 bArgList = 1;
30661 }else{
30662 bArgList = 0;
30663 }
30664 for(; (c=(*fmt))!=0; ++fmt){
30665 if( c!='%' ){
30666 bufpt = (char *)fmt;
30667#if HAVE_STRCHRNUL
30668 fmt = strchrnul(fmt, '%');
30669#else
30670 do{ fmt++; }while( *fmt && *fmt != '%' );
30671#endif
30672 sqlite3_str_append(pAccum, zIn: bufpt, N: (int)(fmt - bufpt));
30673 if( *fmt==0 ) break;
30674 }
30675 if( (c=(*++fmt))==0 ){
30676 sqlite3_str_append(pAccum, zIn: "%", N: 1);
30677 break;
30678 }
30679 /* Find out what flags are present */
30680 flag_leftjustify = flag_prefix = cThousand =
30681 flag_alternateform = flag_altform2 = flag_zeropad = 0;
30682 done = 0;
30683 width = 0;
30684 flag_long = 0;
30685 precision = -1;
30686 do{
30687 switch( c ){
30688 case '-': flag_leftjustify = 1; break;
30689 case '+': flag_prefix = '+'; break;
30690 case ' ': flag_prefix = ' '; break;
30691 case '#': flag_alternateform = 1; break;
30692 case '!': flag_altform2 = 1; break;
30693 case '0': flag_zeropad = 1; break;
30694 case ',': cThousand = ','; break;
30695 default: done = 1; break;
30696 case 'l': {
30697 flag_long = 1;
30698 c = *++fmt;
30699 if( c=='l' ){
30700 c = *++fmt;
30701 flag_long = 2;
30702 }
30703 done = 1;
30704 break;
30705 }
30706 case '1': case '2': case '3': case '4': case '5':
30707 case '6': case '7': case '8': case '9': {
30708 unsigned wx = c - '0';
30709 while( (c = *++fmt)>='0' && c<='9' ){
30710 wx = wx*10 + c - '0';
30711 }
30712 testcase( wx>0x7fffffff );
30713 width = wx & 0x7fffffff;
30714#ifdef SQLITE_PRINTF_PRECISION_LIMIT
30715 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
30716 width = SQLITE_PRINTF_PRECISION_LIMIT;
30717 }
30718#endif
30719 if( c!='.' && c!='l' ){
30720 done = 1;
30721 }else{
30722 fmt--;
30723 }
30724 break;
30725 }
30726 case '*': {
30727 if( bArgList ){
30728 width = (int)getIntArg(p: pArgList);
30729 }else{
30730 width = va_arg(ap,int);
30731 }
30732 if( width<0 ){
30733 flag_leftjustify = 1;
30734 width = width >= -2147483647 ? -width : 0;
30735 }
30736#ifdef SQLITE_PRINTF_PRECISION_LIMIT
30737 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
30738 width = SQLITE_PRINTF_PRECISION_LIMIT;
30739 }
30740#endif
30741 if( (c = fmt[1])!='.' && c!='l' ){
30742 c = *++fmt;
30743 done = 1;
30744 }
30745 break;
30746 }
30747 case '.': {
30748 c = *++fmt;
30749 if( c=='*' ){
30750 if( bArgList ){
30751 precision = (int)getIntArg(p: pArgList);
30752 }else{
30753 precision = va_arg(ap,int);
30754 }
30755 if( precision<0 ){
30756 precision = precision >= -2147483647 ? -precision : -1;
30757 }
30758 c = *++fmt;
30759 }else{
30760 unsigned px = 0;
30761 while( c>='0' && c<='9' ){
30762 px = px*10 + c - '0';
30763 c = *++fmt;
30764 }
30765 testcase( px>0x7fffffff );
30766 precision = px & 0x7fffffff;
30767 }
30768#ifdef SQLITE_PRINTF_PRECISION_LIMIT
30769 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
30770 precision = SQLITE_PRINTF_PRECISION_LIMIT;
30771 }
30772#endif
30773 if( c=='l' ){
30774 --fmt;
30775 }else{
30776 done = 1;
30777 }
30778 break;
30779 }
30780 }
30781 }while( !done && (c=(*++fmt))!=0 );
30782
30783 /* Fetch the info entry for the field */
30784 infop = &fmtinfo[0];
30785 xtype = etINVALID;
30786 for(idx=0; idx<ArraySize(fmtinfo); idx++){
30787 if( c==fmtinfo[idx].fmttype ){
30788 infop = &fmtinfo[idx];
30789 xtype = infop->type;
30790 break;
30791 }
30792 }
30793
30794 /*
30795 ** At this point, variables are initialized as follows:
30796 **
30797 ** flag_alternateform TRUE if a '#' is present.
30798 ** flag_altform2 TRUE if a '!' is present.
30799 ** flag_prefix '+' or ' ' or zero
30800 ** flag_leftjustify TRUE if a '-' is present or if the
30801 ** field width was negative.
30802 ** flag_zeropad TRUE if the width began with 0.
30803 ** flag_long 1 for "l", 2 for "ll"
30804 ** width The specified field width. This is
30805 ** always non-negative. Zero is the default.
30806 ** precision The specified precision. The default
30807 ** is -1.
30808 ** xtype The class of the conversion.
30809 ** infop Pointer to the appropriate info struct.
30810 */
30811 assert( width>=0 );
30812 assert( precision>=(-1) );
30813 switch( xtype ){
30814 case etPOINTER:
30815 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
30816 sizeof(char*)==sizeof(long int) ? 1 : 0;
30817 /* no break */ deliberate_fall_through
30818 case etORDINAL:
30819 case etRADIX:
30820 cThousand = 0;
30821 /* no break */ deliberate_fall_through
30822 case etDECIMAL:
30823 if( infop->flags & FLAG_SIGNED ){
30824 i64 v;
30825 if( bArgList ){
30826 v = getIntArg(p: pArgList);
30827 }else if( flag_long ){
30828 if( flag_long==2 ){
30829 v = va_arg(ap,i64) ;
30830 }else{
30831 v = va_arg(ap,long int);
30832 }
30833 }else{
30834 v = va_arg(ap,int);
30835 }
30836 if( v<0 ){
30837 testcase( v==SMALLEST_INT64 );
30838 testcase( v==(-1) );
30839 longvalue = ~v;
30840 longvalue++;
30841 prefix = '-';
30842 }else{
30843 longvalue = v;
30844 prefix = flag_prefix;
30845 }
30846 }else{
30847 if( bArgList ){
30848 longvalue = (u64)getIntArg(p: pArgList);
30849 }else if( flag_long ){
30850 if( flag_long==2 ){
30851 longvalue = va_arg(ap,u64);
30852 }else{
30853 longvalue = va_arg(ap,unsigned long int);
30854 }
30855 }else{
30856 longvalue = va_arg(ap,unsigned int);
30857 }
30858 prefix = 0;
30859 }
30860 if( longvalue==0 ) flag_alternateform = 0;
30861 if( flag_zeropad && precision<width-(prefix!=0) ){
30862 precision = width-(prefix!=0);
30863 }
30864 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
30865 nOut = etBUFSIZE;
30866 zOut = buf;
30867 }else{
30868 u64 n;
30869 n = (u64)precision + 10;
30870 if( cThousand ) n += precision/3;
30871 zOut = zExtra = printfTempBuf(pAccum, n);
30872 if( zOut==0 ) return;
30873 nOut = (int)n;
30874 }
30875 bufpt = &zOut[nOut-1];
30876 if( xtype==etORDINAL ){
30877 static const char zOrd[] = "thstndrd";
30878 int x = (int)(longvalue % 10);
30879 if( x>=4 || (longvalue/10)%10==1 ){
30880 x = 0;
30881 }
30882 *(--bufpt) = zOrd[x*2+1];
30883 *(--bufpt) = zOrd[x*2];
30884 }
30885 {
30886 const char *cset = &aDigits[infop->charset];
30887 u8 base = infop->base;
30888 do{ /* Convert to ascii */
30889 *(--bufpt) = cset[longvalue%base];
30890 longvalue = longvalue/base;
30891 }while( longvalue>0 );
30892 }
30893 length = (int)(&zOut[nOut-1]-bufpt);
30894 while( precision>length ){
30895 *(--bufpt) = '0'; /* Zero pad */
30896 length++;
30897 }
30898 if( cThousand ){
30899 int nn = (length - 1)/3; /* Number of "," to insert */
30900 int ix = (length - 1)%3 + 1;
30901 bufpt -= nn;
30902 for(idx=0; nn>0; idx++){
30903 bufpt[idx] = bufpt[idx+nn];
30904 ix--;
30905 if( ix==0 ){
30906 bufpt[++idx] = cThousand;
30907 nn--;
30908 ix = 3;
30909 }
30910 }
30911 }
30912 if( prefix ) *(--bufpt) = prefix; /* Add sign */
30913 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
30914 const char *pre;
30915 char x;
30916 pre = &aPrefix[infop->prefix];
30917 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
30918 }
30919 length = (int)(&zOut[nOut-1]-bufpt);
30920 break;
30921 case etFLOAT:
30922 case etEXP:
30923 case etGENERIC: {
30924 FpDecode s;
30925 int iRound;
30926 int j;
30927
30928 if( bArgList ){
30929 realvalue = getDoubleArg(p: pArgList);
30930 }else{
30931 realvalue = va_arg(ap,double);
30932 }
30933 if( precision<0 ) precision = 6; /* Set default precision */
30934#ifdef SQLITE_FP_PRECISION_LIMIT
30935 if( precision>SQLITE_FP_PRECISION_LIMIT ){
30936 precision = SQLITE_FP_PRECISION_LIMIT;
30937 }
30938#endif
30939 if( xtype==etFLOAT ){
30940 iRound = -precision;
30941 }else if( xtype==etGENERIC ){
30942 iRound = precision;
30943 }else{
30944 iRound = precision+1;
30945 }
30946 sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16);
30947 if( s.isSpecial ){
30948 if( s.isSpecial==2 ){
30949 bufpt = flag_zeropad ? "null" : "NaN";
30950 length = sqlite3Strlen30(bufpt);
30951 break;
30952 }else if( flag_zeropad ){
30953 s.z[0] = '9';
30954 s.iDP = 1000;
30955 s.n = 1;
30956 }else{
30957 memcpy(dest: buf, src: "-Inf", n: 5);
30958 bufpt = buf;
30959 if( s.sign=='-' ){
30960 /* no-op */
30961 }else if( flag_prefix ){
30962 buf[0] = flag_prefix;
30963 }else{
30964 bufpt++;
30965 }
30966 length = sqlite3Strlen30(bufpt);
30967 break;
30968 }
30969 }
30970 if( s.sign=='-' ){
30971 prefix = '-';
30972 }else{
30973 prefix = flag_prefix;
30974 }
30975
30976 exp = s.iDP-1;
30977 if( xtype==etGENERIC && precision>0 ) precision--;
30978
30979 /*
30980 ** If the field type is etGENERIC, then convert to either etEXP
30981 ** or etFLOAT, as appropriate.
30982 */
30983 if( xtype==etGENERIC ){
30984 flag_rtz = !flag_alternateform;
30985 if( exp<-4 || exp>precision ){
30986 xtype = etEXP;
30987 }else{
30988 precision = precision - exp;
30989 xtype = etFLOAT;
30990 }
30991 }else{
30992 flag_rtz = flag_altform2;
30993 }
30994 if( xtype==etEXP ){
30995 e2 = 0;
30996 }else{
30997 e2 = s.iDP - 1;
30998 }
30999 bufpt = buf;
31000 {
31001 i64 szBufNeeded; /* Size of a temporary buffer needed */
31002 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
31003 if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
31004 if( szBufNeeded > etBUFSIZE ){
31005 bufpt = zExtra = printfTempBuf(pAccum, n: szBufNeeded);
31006 if( bufpt==0 ) return;
31007 }
31008 }
31009 zOut = bufpt;
31010 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
31011 /* The sign in front of the number */
31012 if( prefix ){
31013 *(bufpt++) = prefix;
31014 }
31015 /* Digits prior to the decimal point */
31016 j = 0;
31017 if( e2<0 ){
31018 *(bufpt++) = '0';
31019 }else{
31020 for(; e2>=0; e2--){
31021 *(bufpt++) = j<s.n ? s.z[j++] : '0';
31022 if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
31023 }
31024 }
31025 /* The decimal point */
31026 if( flag_dp ){
31027 *(bufpt++) = '.';
31028 }
31029 /* "0" digits after the decimal point but before the first
31030 ** significant digit of the number */
31031 for(e2++; e2<0 && precision>0; precision--, e2++){
31032 *(bufpt++) = '0';
31033 }
31034 /* Significant digits after the decimal point */
31035 while( (precision--)>0 ){
31036 *(bufpt++) = j<s.n ? s.z[j++] : '0';
31037 }
31038 /* Remove trailing zeros and the "." if no digits follow the "." */
31039 if( flag_rtz && flag_dp ){
31040 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
31041 assert( bufpt>zOut );
31042 if( bufpt[-1]=='.' ){
31043 if( flag_altform2 ){
31044 *(bufpt++) = '0';
31045 }else{
31046 *(--bufpt) = 0;
31047 }
31048 }
31049 }
31050 /* Add the "eNNN" suffix */
31051 if( xtype==etEXP ){
31052 exp = s.iDP - 1;
31053 *(bufpt++) = aDigits[infop->charset];
31054 if( exp<0 ){
31055 *(bufpt++) = '-'; exp = -exp;
31056 }else{
31057 *(bufpt++) = '+';
31058 }
31059 if( exp>=100 ){
31060 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
31061 exp %= 100;
31062 }
31063 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
31064 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
31065 }
31066 *bufpt = 0;
31067
31068 /* The converted number is in buf[] and zero terminated. Output it.
31069 ** Note that the number is in the usual order, not reversed as with
31070 ** integer conversions. */
31071 length = (int)(bufpt-zOut);
31072 bufpt = zOut;
31073
31074 /* Special case: Add leading zeros if the flag_zeropad flag is
31075 ** set and we are not left justified */
31076 if( flag_zeropad && !flag_leftjustify && length < width){
31077 int i;
31078 int nPad = width - length;
31079 for(i=width; i>=nPad; i--){
31080 bufpt[i] = bufpt[i-nPad];
31081 }
31082 i = prefix!=0;
31083 while( nPad-- ) bufpt[i++] = '0';
31084 length = width;
31085 }
31086 break;
31087 }
31088 case etSIZE:
31089 if( !bArgList ){
31090 *(va_arg(ap,int*)) = pAccum->nChar;
31091 }
31092 length = width = 0;
31093 break;
31094 case etPERCENT:
31095 buf[0] = '%';
31096 bufpt = buf;
31097 length = 1;
31098 break;
31099 case etCHARX:
31100 if( bArgList ){
31101 bufpt = getTextArg(p: pArgList);
31102 length = 1;
31103 if( bufpt ){
31104 buf[0] = c = *(bufpt++);
31105 if( (c&0xc0)==0xc0 ){
31106 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
31107 buf[length++] = *(bufpt++);
31108 }
31109 }
31110 }else{
31111 buf[0] = 0;
31112 }
31113 }else{
31114 unsigned int ch = va_arg(ap,unsigned int);
31115 if( ch<0x00080 ){
31116 buf[0] = ch & 0xff;
31117 length = 1;
31118 }else if( ch<0x00800 ){
31119 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
31120 buf[1] = 0x80 + (u8)(ch & 0x3f);
31121 length = 2;
31122 }else if( ch<0x10000 ){
31123 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
31124 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
31125 buf[2] = 0x80 + (u8)(ch & 0x3f);
31126 length = 3;
31127 }else{
31128 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
31129 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
31130 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
31131 buf[3] = 0x80 + (u8)(ch & 0x3f);
31132 length = 4;
31133 }
31134 }
31135 if( precision>1 ){
31136 i64 nPrior = 1;
31137 width -= precision-1;
31138 if( width>1 && !flag_leftjustify ){
31139 sqlite3_str_appendchar(pAccum, N: width-1, C: ' ');
31140 width = 0;
31141 }
31142 sqlite3_str_append(pAccum, zIn: buf, N: length);
31143 precision--;
31144 while( precision > 1 ){
31145 i64 nCopyBytes;
31146 if( nPrior > precision-1 ) nPrior = precision - 1;
31147 nCopyBytes = length*nPrior;
31148 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){
31149 sqlite3StrAccumEnlarge(pAccum, nCopyBytes);
31150 }
31151 if( pAccum->accError ) break;
31152 sqlite3_str_append(pAccum,
31153 zIn: &pAccum->zText[pAccum->nChar-nCopyBytes], N: nCopyBytes);
31154 precision -= nPrior;
31155 nPrior *= 2;
31156 }
31157 }
31158 bufpt = buf;
31159 flag_altform2 = 1;
31160 goto adjust_width_for_utf8;
31161 case etSTRING:
31162 case etDYNSTRING:
31163 if( bArgList ){
31164 bufpt = getTextArg(p: pArgList);
31165 xtype = etSTRING;
31166 }else{
31167 bufpt = va_arg(ap,char*);
31168 }
31169 if( bufpt==0 ){
31170 bufpt = "";
31171 }else if( xtype==etDYNSTRING ){
31172 if( pAccum->nChar==0
31173 && pAccum->mxAlloc
31174 && width==0
31175 && precision<0
31176 && pAccum->accError==0
31177 ){
31178 /* Special optimization for sqlite3_mprintf("%z..."):
31179 ** Extend an existing memory allocation rather than creating
31180 ** a new one. */
31181 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
31182 pAccum->zText = bufpt;
31183 pAccum->nAlloc = sqlite3DbMallocSize(db: pAccum->db, p: bufpt);
31184 pAccum->nChar = 0x7fffffff & (int)strlen(s: bufpt);
31185 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
31186 length = 0;
31187 break;
31188 }
31189 zExtra = bufpt;
31190 }
31191 if( precision>=0 ){
31192 if( flag_altform2 ){
31193 /* Set length to the number of bytes needed in order to display
31194 ** precision characters */
31195 unsigned char *z = (unsigned char*)bufpt;
31196 while( precision-- > 0 && z[0] ){
31197 SQLITE_SKIP_UTF8(z);
31198 }
31199 length = (int)(z - (unsigned char*)bufpt);
31200 }else{
31201 for(length=0; length<precision && bufpt[length]; length++){}
31202 }
31203 }else{
31204 length = 0x7fffffff & (int)strlen(s: bufpt);
31205 }
31206 adjust_width_for_utf8:
31207 if( flag_altform2 && width>0 ){
31208 /* Adjust width to account for extra bytes in UTF-8 characters */
31209 int ii = length - 1;
31210 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
31211 }
31212 break;
31213 case etSQLESCAPE: /* %q: Escape ' characters */
31214 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
31215 case etSQLESCAPE3: { /* %w: Escape " characters */
31216 i64 i, j, k, n;
31217 int needQuote, isnull;
31218 char ch;
31219 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
31220 char *escarg;
31221
31222 if( bArgList ){
31223 escarg = getTextArg(p: pArgList);
31224 }else{
31225 escarg = va_arg(ap,char*);
31226 }
31227 isnull = escarg==0;
31228 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
31229 /* For %q, %Q, and %w, the precision is the number of bytes (or
31230 ** characters if the ! flags is present) to use from the input.
31231 ** Because of the extra quoting characters inserted, the number
31232 ** of output characters may be larger than the precision.
31233 */
31234 k = precision;
31235 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
31236 if( ch==q ) n++;
31237 if( flag_altform2 && (ch&0xc0)==0xc0 ){
31238 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
31239 }
31240 }
31241 needQuote = !isnull && xtype==etSQLESCAPE2;
31242 n += i + 3;
31243 if( n>etBUFSIZE ){
31244 bufpt = zExtra = printfTempBuf(pAccum, n);
31245 if( bufpt==0 ) return;
31246 }else{
31247 bufpt = buf;
31248 }
31249 j = 0;
31250 if( needQuote ) bufpt[j++] = q;
31251 k = i;
31252 for(i=0; i<k; i++){
31253 bufpt[j++] = ch = escarg[i];
31254 if( ch==q ) bufpt[j++] = ch;
31255 }
31256 if( needQuote ) bufpt[j++] = q;
31257 bufpt[j] = 0;
31258 length = j;
31259 goto adjust_width_for_utf8;
31260 }
31261 case etTOKEN: {
31262 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
31263 if( flag_alternateform ){
31264 /* %#T means an Expr pointer that uses Expr.u.zToken */
31265 Expr *pExpr = va_arg(ap,Expr*);
31266 if( ALWAYS(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue)) ){
31267 sqlite3_str_appendall(pAccum, zIn: (const char*)pExpr->u.zToken);
31268 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
31269 }
31270 }else{
31271 /* %T means a Token pointer */
31272 Token *pToken = va_arg(ap, Token*);
31273 assert( bArgList==0 );
31274 if( pToken && pToken->n ){
31275 sqlite3_str_append(pAccum, zIn: (const char*)pToken->z, N: pToken->n);
31276 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
31277 }
31278 }
31279 length = width = 0;
31280 break;
31281 }
31282 case etSRCITEM: {
31283 SrcItem *pItem;
31284 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
31285 pItem = va_arg(ap, SrcItem*);
31286 assert( bArgList==0 );
31287 if( pItem->zAlias && !flag_altform2 ){
31288 sqlite3_str_appendall(pAccum, zIn: pItem->zAlias);
31289 }else if( pItem->zName ){
31290 if( pItem->zDatabase ){
31291 sqlite3_str_appendall(pAccum, zIn: pItem->zDatabase);
31292 sqlite3_str_append(pAccum, zIn: ".", N: 1);
31293 }
31294 sqlite3_str_appendall(pAccum, zIn: pItem->zName);
31295 }else if( pItem->zAlias ){
31296 sqlite3_str_appendall(pAccum, zIn: pItem->zAlias);
31297 }else{
31298 Select *pSel = pItem->pSelect;
31299 assert( pSel!=0 );
31300 if( pSel->selFlags & SF_NestedFrom ){
31301 sqlite3_str_appendf(pAccum, zFormat: "(join-%u)", pSel->selId);
31302 }else{
31303 sqlite3_str_appendf(pAccum, zFormat: "(subquery-%u)", pSel->selId);
31304 }
31305 }
31306 length = width = 0;
31307 break;
31308 }
31309 default: {
31310 assert( xtype==etINVALID );
31311 return;
31312 }
31313 }/* End switch over the format type */
31314 /*
31315 ** The text of the conversion is pointed to by "bufpt" and is
31316 ** "length" characters long. The field width is "width". Do
31317 ** the output. Both length and width are in bytes, not characters,
31318 ** at this point. If the "!" flag was present on string conversions
31319 ** indicating that width and precision should be expressed in characters,
31320 ** then the values have been translated prior to reaching this point.
31321 */
31322 width -= length;
31323 if( width>0 ){
31324 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, N: width, C: ' ');
31325 sqlite3_str_append(pAccum, zIn: bufpt, N: length);
31326 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, N: width, C: ' ');
31327 }else{
31328 sqlite3_str_append(pAccum, zIn: bufpt, N: length);
31329 }
31330
31331 if( zExtra ){
31332 sqlite3DbFree(db: pAccum->db, p: zExtra);
31333 zExtra = 0;
31334 }
31335 }/* End for loop over the format string */
31336} /* End of function */
31337
31338
31339/*
31340** The z string points to the first character of a token that is
31341** associated with an error. If db does not already have an error
31342** byte offset recorded, try to compute the error byte offset for
31343** z and set the error byte offset in db.
31344*/
31345SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
31346 const Parse *pParse;
31347 const char *zText;
31348 const char *zEnd;
31349 assert( z!=0 );
31350 if( NEVER(db==0) ) return;
31351 if( db->errByteOffset!=(-2) ) return;
31352 pParse = db->pParse;
31353 if( NEVER(pParse==0) ) return;
31354 zText =pParse->zTail;
31355 if( NEVER(zText==0) ) return;
31356 zEnd = &zText[strlen(s: zText)];
31357 if( SQLITE_WITHIN(z,zText,zEnd) ){
31358 db->errByteOffset = (int)(z-zText);
31359 }
31360}
31361
31362/*
31363** If pExpr has a byte offset for the start of a token, record that as
31364** as the error offset.
31365*/
31366SQLITE_PRIVATE void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){
31367 while( pExpr
31368 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
31369 ){
31370 pExpr = pExpr->pLeft;
31371 }
31372 if( pExpr==0 ) return;
31373 db->errByteOffset = pExpr->w.iOfst;
31374}
31375
31376/*
31377** Enlarge the memory allocation on a StrAccum object so that it is
31378** able to accept at least N more bytes of text.
31379**
31380** Return the number of bytes of text that StrAccum is able to accept
31381** after the attempted enlargement. The value returned might be zero.
31382*/
31383SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){
31384 char *zNew;
31385 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
31386 if( p->accError ){
31387 testcase(p->accError==SQLITE_TOOBIG);
31388 testcase(p->accError==SQLITE_NOMEM);
31389 return 0;
31390 }
31391 if( p->mxAlloc==0 ){
31392 sqlite3StrAccumSetError(p, SQLITE_TOOBIG);
31393 return p->nAlloc - p->nChar - 1;
31394 }else{
31395 char *zOld = isMalloced(p) ? p->zText : 0;
31396 i64 szNew = p->nChar + N + 1;
31397 if( szNew+p->nChar<=p->mxAlloc ){
31398 /* Force exponential buffer size growth as long as it does not overflow,
31399 ** to avoid having to call this routine too often */
31400 szNew += p->nChar;
31401 }
31402 if( szNew > p->mxAlloc ){
31403 sqlite3_str_reset(p);
31404 sqlite3StrAccumSetError(p, SQLITE_TOOBIG);
31405 return 0;
31406 }else{
31407 p->nAlloc = (int)szNew;
31408 }
31409 if( p->db ){
31410 zNew = sqlite3DbRealloc(db: p->db, p: zOld, n: p->nAlloc);
31411 }else{
31412 zNew = sqlite3Realloc(pOld: zOld, nBytes: p->nAlloc);
31413 }
31414 if( zNew ){
31415 assert( p->zText!=0 || p->nChar==0 );
31416 if( !isMalloced(p) && p->nChar>0 ) memcpy(dest: zNew, src: p->zText, n: p->nChar);
31417 p->zText = zNew;
31418 p->nAlloc = sqlite3DbMallocSize(db: p->db, p: zNew);
31419 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
31420 }else{
31421 sqlite3_str_reset(p);
31422 sqlite3StrAccumSetError(p, SQLITE_NOMEM);
31423 return 0;
31424 }
31425 }
31426 assert( N>=0 && N<=0x7fffffff );
31427 return (int)N;
31428}
31429
31430/*
31431** Append N copies of character c to the given string buffer.
31432*/
31433SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
31434 testcase( p->nChar + (i64)N > 0x7fffffff );
31435 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
31436 return;
31437 }
31438 while( (N--)>0 ) p->zText[p->nChar++] = c;
31439}
31440
31441/*
31442** The StrAccum "p" is not large enough to accept N new bytes of z[].
31443** So enlarge if first, then do the append.
31444**
31445** This is a helper routine to sqlite3_str_append() that does special-case
31446** work (enlarging the buffer) using tail recursion, so that the
31447** sqlite3_str_append() routine can use fast calling semantics.
31448*/
31449static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
31450 N = sqlite3StrAccumEnlarge(p, N);
31451 if( N>0 ){
31452 memcpy(dest: &p->zText[p->nChar], src: z, n: N);
31453 p->nChar += N;
31454 }
31455}
31456
31457/*
31458** Append N bytes of text from z to the StrAccum object. Increase the
31459** size of the memory allocation for StrAccum if necessary.
31460*/
31461SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
31462 assert( z!=0 || N==0 );
31463 assert( p->zText!=0 || p->nChar==0 || p->accError );
31464 assert( N>=0 );
31465 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
31466 if( p->nChar+N >= p->nAlloc ){
31467 enlargeAndAppend(p,z,N);
31468 }else if( N ){
31469 assert( p->zText );
31470 p->nChar += N;
31471 memcpy(dest: &p->zText[p->nChar-N], src: z, n: N);
31472 }
31473}
31474
31475/*
31476** Append the complete text of zero-terminated string z[] to the p string.
31477*/
31478SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
31479 sqlite3_str_append(p, z, N: sqlite3Strlen30(z));
31480}
31481
31482
31483/*
31484** Finish off a string by making sure it is zero-terminated.
31485** Return a pointer to the resulting string. Return a NULL
31486** pointer if any kind of error was encountered.
31487*/
31488static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
31489 char *zText;
31490 assert( p->mxAlloc>0 && !isMalloced(p) );
31491 zText = sqlite3DbMallocRaw(db: p->db, n: p->nChar+1 );
31492 if( zText ){
31493 memcpy(dest: zText, src: p->zText, n: p->nChar+1);
31494 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
31495 }else{
31496 sqlite3StrAccumSetError(p, SQLITE_NOMEM);
31497 }
31498 p->zText = zText;
31499 return zText;
31500}
31501SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
31502 if( p->zText ){
31503 p->zText[p->nChar] = 0;
31504 if( p->mxAlloc>0 && !isMalloced(p) ){
31505 return strAccumFinishRealloc(p);
31506 }
31507 }
31508 return p->zText;
31509}
31510
31511/*
31512** Use the content of the StrAccum passed as the second argument
31513** as the result of an SQL function.
31514*/
31515SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){
31516 if( p->accError ){
31517 sqlite3_result_error_code(pCtx, p->accError);
31518 sqlite3_str_reset(p);
31519 }else if( isMalloced(p) ){
31520 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC);
31521 }else{
31522 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
31523 sqlite3_str_reset(p);
31524 }
31525}
31526
31527/*
31528** This singleton is an sqlite3_str object that is returned if
31529** sqlite3_malloc() fails to provide space for a real one. This
31530** sqlite3_str object accepts no new text and always returns
31531** an SQLITE_NOMEM error.
31532*/
31533static sqlite3_str sqlite3OomStr = {
31534 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
31535};
31536
31537/* Finalize a string created using sqlite3_str_new().
31538*/
31539SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
31540 char *z;
31541 if( p!=0 && p!=&sqlite3OomStr ){
31542 z = sqlite3StrAccumFinish(p);
31543 sqlite3_free(p);
31544 }else{
31545 z = 0;
31546 }
31547 return z;
31548}
31549
31550/* Return any error code associated with p */
31551SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
31552 return p ? p->accError : SQLITE_NOMEM;
31553}
31554
31555/* Return the current length of p in bytes */
31556SQLITE_API int sqlite3_str_length(sqlite3_str *p){
31557 return p ? p->nChar : 0;
31558}
31559
31560/* Return the current value for p */
31561SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
31562 if( p==0 || p->nChar==0 ) return 0;
31563 p->zText[p->nChar] = 0;
31564 return p->zText;
31565}
31566
31567/*
31568** Reset an StrAccum string. Reclaim all malloced memory.
31569*/
31570SQLITE_API void sqlite3_str_reset(StrAccum *p){
31571 if( isMalloced(p) ){
31572 sqlite3DbFree(db: p->db, p: p->zText);
31573 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
31574 }
31575 p->nAlloc = 0;
31576 p->nChar = 0;
31577 p->zText = 0;
31578}
31579
31580/*
31581** Initialize a string accumulator.
31582**
31583** p: The accumulator to be initialized.
31584** db: Pointer to a database connection. May be NULL. Lookaside
31585** memory is used if not NULL. db->mallocFailed is set appropriately
31586** when not NULL.
31587** zBase: An initial buffer. May be NULL in which case the initial buffer
31588** is malloced.
31589** n: Size of zBase in bytes. If total space requirements never exceed
31590** n then no memory allocations ever occur.
31591** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
31592** allocations will ever occur.
31593*/
31594SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
31595 p->zText = zBase;
31596 p->db = db;
31597 p->nAlloc = n;
31598 p->mxAlloc = mx;
31599 p->nChar = 0;
31600 p->accError = 0;
31601 p->printfFlags = 0;
31602}
31603
31604/* Allocate and initialize a new dynamic string object */
31605SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
31606 sqlite3_str *p = sqlite3_malloc64(n: sizeof(*p));
31607 if( p ){
31608 sqlite3StrAccumInit(p, db: 0, zBase: 0, n: 0,
31609 mx: db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
31610 }else{
31611 p = &sqlite3OomStr;
31612 }
31613 return p;
31614}
31615
31616/*
31617** Print into memory obtained from sqliteMalloc(). Use the internal
31618** %-conversion extensions.
31619*/
31620SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
31621 char *z;
31622 char zBase[SQLITE_PRINT_BUF_SIZE];
31623 StrAccum acc;
31624 assert( db!=0 );
31625 sqlite3StrAccumInit(p: &acc, db, zBase, n: sizeof(zBase),
31626 mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
31627 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
31628 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
31629 z = sqlite3StrAccumFinish(p: &acc);
31630 if( acc.accError==SQLITE_NOMEM ){
31631 sqlite3OomFault(db);
31632 }
31633 return z;
31634}
31635
31636/*
31637** Print into memory obtained from sqliteMalloc(). Use the internal
31638** %-conversion extensions.
31639*/
31640SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
31641 va_list ap;
31642 char *z;
31643 va_start(ap, zFormat);
31644 z = sqlite3VMPrintf(db, zFormat, ap);
31645 va_end(ap);
31646 return z;
31647}
31648
31649/*
31650** Print into memory obtained from sqlite3_malloc(). Omit the internal
31651** %-conversion extensions.
31652*/
31653SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
31654 char *z;
31655 char zBase[SQLITE_PRINT_BUF_SIZE];
31656 StrAccum acc;
31657
31658#ifdef SQLITE_ENABLE_API_ARMOR
31659 if( zFormat==0 ){
31660 (void)SQLITE_MISUSE_BKPT;
31661 return 0;
31662 }
31663#endif
31664#ifndef SQLITE_OMIT_AUTOINIT
31665 if( sqlite3_initialize() ) return 0;
31666#endif
31667 sqlite3StrAccumInit(p: &acc, db: 0, zBase, n: sizeof(zBase), SQLITE_MAX_LENGTH);
31668 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
31669 z = sqlite3StrAccumFinish(p: &acc);
31670 return z;
31671}
31672
31673/*
31674** Print into memory obtained from sqlite3_malloc()(). Omit the internal
31675** %-conversion extensions.
31676*/
31677SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
31678 va_list ap;
31679 char *z;
31680#ifndef SQLITE_OMIT_AUTOINIT
31681 if( sqlite3_initialize() ) return 0;
31682#endif
31683 va_start(ap, zFormat);
31684 z = sqlite3_vmprintf(zFormat, ap);
31685 va_end(ap);
31686 return z;
31687}
31688
31689/*
31690** sqlite3_snprintf() works like snprintf() except that it ignores the
31691** current locale settings. This is important for SQLite because we
31692** are not able to use a "," as the decimal point in place of "." as
31693** specified by some locales.
31694**
31695** Oops: The first two arguments of sqlite3_snprintf() are backwards
31696** from the snprintf() standard. Unfortunately, it is too late to change
31697** this without breaking compatibility, so we just have to live with the
31698** mistake.
31699**
31700** sqlite3_vsnprintf() is the varargs version.
31701*/
31702SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
31703 StrAccum acc;
31704 if( n<=0 ) return zBuf;
31705#ifdef SQLITE_ENABLE_API_ARMOR
31706 if( zBuf==0 || zFormat==0 ) {
31707 (void)SQLITE_MISUSE_BKPT;
31708 if( zBuf ) zBuf[0] = 0;
31709 return zBuf;
31710 }
31711#endif
31712 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n, mx: 0);
31713 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
31714 zBuf[acc.nChar] = 0;
31715 return zBuf;
31716}
31717SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
31718 StrAccum acc;
31719 va_list ap;
31720 if( n<=0 ) return zBuf;
31721#ifdef SQLITE_ENABLE_API_ARMOR
31722 if( zBuf==0 || zFormat==0 ) {
31723 (void)SQLITE_MISUSE_BKPT;
31724 if( zBuf ) zBuf[0] = 0;
31725 return zBuf;
31726 }
31727#endif
31728 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n, mx: 0);
31729 va_start(ap,zFormat);
31730 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
31731 va_end(ap);
31732 zBuf[acc.nChar] = 0;
31733 return zBuf;
31734}
31735
31736/*
31737** This is the routine that actually formats the sqlite3_log() message.
31738** We house it in a separate routine from sqlite3_log() to avoid using
31739** stack space on small-stack systems when logging is disabled.
31740**
31741** sqlite3_log() must render into a static buffer. It cannot dynamically
31742** allocate memory because it might be called while the memory allocator
31743** mutex is held.
31744**
31745** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
31746** certain format characters (%q) or for very large precisions or widths.
31747** Care must be taken that any sqlite3_log() calls that occur while the
31748** memory mutex is held do not use these mechanisms.
31749*/
31750static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
31751 StrAccum acc; /* String accumulator */
31752 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
31753
31754 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zMsg, n: sizeof(zMsg), mx: 0);
31755 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
31756 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
31757 sqlite3StrAccumFinish(p: &acc));
31758}
31759
31760/*
31761** Format and write a message to the log if logging is enabled.
31762*/
31763SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
31764 va_list ap; /* Vararg list */
31765 if( sqlite3GlobalConfig.xLog ){
31766 va_start(ap, zFormat);
31767 renderLogMsg(iErrCode, zFormat, ap);
31768 va_end(ap);
31769 }
31770}
31771
31772#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
31773/*
31774** A version of printf() that understands %lld. Used for debugging.
31775** The printf() built into some versions of windows does not understand %lld
31776** and segfaults if you give it a long long int.
31777*/
31778SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
31779 va_list ap;
31780 StrAccum acc;
31781 char zBuf[SQLITE_PRINT_BUF_SIZE*10];
31782 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
31783 va_start(ap,zFormat);
31784 sqlite3_str_vappendf(&acc, zFormat, ap);
31785 va_end(ap);
31786 sqlite3StrAccumFinish(&acc);
31787#ifdef SQLITE_OS_TRACE_PROC
31788 {
31789 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
31790 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
31791 }
31792#else
31793 fprintf(stdout,"%s", zBuf);
31794 fflush(stdout);
31795#endif
31796}
31797#endif
31798
31799
31800/*
31801** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
31802** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
31803*/
31804SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
31805 va_list ap;
31806 va_start(ap,zFormat);
31807 sqlite3_str_vappendf(pAccum: p, fmt: zFormat, ap);
31808 va_end(ap);
31809}
31810
31811
31812/*****************************************************************************
31813** Reference counted string storage
31814*****************************************************************************/
31815
31816/*
31817** Increase the reference count of the string by one.
31818**
31819** The input parameter is returned.
31820*/
31821SQLITE_PRIVATE char *sqlite3RCStrRef(char *z){
31822 RCStr *p = (RCStr*)z;
31823 assert( p!=0 );
31824 p--;
31825 p->nRCRef++;
31826 return z;
31827}
31828
31829/*
31830** Decrease the reference count by one. Free the string when the
31831** reference count reaches zero.
31832*/
31833SQLITE_PRIVATE void sqlite3RCStrUnref(char *z){
31834 RCStr *p = (RCStr*)z;
31835 assert( p!=0 );
31836 p--;
31837 assert( p->nRCRef>0 );
31838 if( p->nRCRef>=2 ){
31839 p->nRCRef--;
31840 }else{
31841 sqlite3_free(p);
31842 }
31843}
31844
31845/*
31846** Create a new string that is capable of holding N bytes of text, not counting
31847** the zero byte at the end. The string is uninitialized.
31848**
31849** The reference count is initially 1. Call sqlite3RCStrUnref() to free the
31850** newly allocated string.
31851**
31852** This routine returns 0 on an OOM.
31853*/
31854SQLITE_PRIVATE char *sqlite3RCStrNew(u64 N){
31855 RCStr *p = sqlite3_malloc64( n: N + sizeof(*p) + 1 );
31856 if( p==0 ) return 0;
31857 p->nRCRef = 1;
31858 return (char*)&p[1];
31859}
31860
31861/*
31862** Change the size of the string so that it is able to hold N bytes.
31863** The string might be reallocated, so return the new allocation.
31864*/
31865SQLITE_PRIVATE char *sqlite3RCStrResize(char *z, u64 N){
31866 RCStr *p = (RCStr*)z;
31867 RCStr *pNew;
31868 assert( p!=0 );
31869 p--;
31870 assert( p->nRCRef==1 );
31871 pNew = sqlite3_realloc64(pOld: p, n: N+sizeof(RCStr)+1);
31872 if( pNew==0 ){
31873 sqlite3_free(p);
31874 return 0;
31875 }else{
31876 return (char*)&pNew[1];
31877 }
31878}
31879
31880/************** End of printf.c **********************************************/
31881/************** Begin file treeview.c ****************************************/
31882/*
31883** 2015-06-08
31884**
31885** The author disclaims copyright to this source code. In place of
31886** a legal notice, here is a blessing:
31887**
31888** May you do good and not evil.
31889** May you find forgiveness for yourself and forgive others.
31890** May you share freely, never taking more than you give.
31891**
31892*************************************************************************
31893**
31894** This file contains C code to implement the TreeView debugging routines.
31895** These routines print a parse tree to standard output for debugging and
31896** analysis.
31897**
31898** The interfaces in this file is only available when compiling
31899** with SQLITE_DEBUG.
31900*/
31901/* #include "sqliteInt.h" */
31902#ifdef SQLITE_DEBUG
31903
31904/*
31905** Add a new subitem to the tree. The moreToFollow flag indicates that this
31906** is not the last item in the tree.
31907*/
31908static void sqlite3TreeViewPush(TreeView **pp, u8 moreToFollow){
31909 TreeView *p = *pp;
31910 if( p==0 ){
31911 *pp = p = sqlite3_malloc64( sizeof(*p) );
31912 if( p==0 ) return;
31913 memset(p, 0, sizeof(*p));
31914 }else{
31915 p->iLevel++;
31916 }
31917 assert( moreToFollow==0 || moreToFollow==1 );
31918 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
31919}
31920
31921/*
31922** Finished with one layer of the tree
31923*/
31924static void sqlite3TreeViewPop(TreeView **pp){
31925 TreeView *p = *pp;
31926 if( p==0 ) return;
31927 p->iLevel--;
31928 if( p->iLevel<0 ){
31929 sqlite3_free(p);
31930 *pp = 0;
31931 }
31932}
31933
31934/*
31935** Generate a single line of output for the tree, with a prefix that contains
31936** all the appropriate tree lines
31937*/
31938SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
31939 va_list ap;
31940 int i;
31941 StrAccum acc;
31942 char zBuf[1000];
31943 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
31944 if( p ){
31945 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
31946 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
31947 }
31948 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
31949 }
31950 if( zFormat!=0 ){
31951 va_start(ap, zFormat);
31952 sqlite3_str_vappendf(&acc, zFormat, ap);
31953 va_end(ap);
31954 assert( acc.nChar>0 || acc.accError );
31955 sqlite3_str_append(&acc, "\n", 1);
31956 }
31957 sqlite3StrAccumFinish(&acc);
31958 fprintf(stdout,"%s", zBuf);
31959 fflush(stdout);
31960}
31961
31962/*
31963** Shorthand for starting a new tree item that consists of a single label
31964*/
31965static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
31966 sqlite3TreeViewPush(&p, moreFollows);
31967 sqlite3TreeViewLine(p, "%s", zLabel);
31968}
31969
31970/*
31971** Show a list of Column objects in tree format.
31972*/
31973SQLITE_PRIVATE void sqlite3TreeViewColumnList(
31974 TreeView *pView,
31975 const Column *aCol,
31976 int nCol,
31977 u8 moreToFollow
31978){
31979 int i;
31980 sqlite3TreeViewPush(&pView, moreToFollow);
31981 sqlite3TreeViewLine(pView, "COLUMNS");
31982 for(i=0; i<nCol; i++){
31983 u16 flg = aCol[i].colFlags;
31984 int colMoreToFollow = i<(nCol - 1);
31985 sqlite3TreeViewPush(&pView, colMoreToFollow);
31986 sqlite3TreeViewLine(pView, 0);
31987 printf(" %s", aCol[i].zCnName);
31988 switch( aCol[i].eCType ){
31989 case COLTYPE_ANY: printf(" ANY"); break;
31990 case COLTYPE_BLOB: printf(" BLOB"); break;
31991 case COLTYPE_INT: printf(" INT"); break;
31992 case COLTYPE_INTEGER: printf(" INTEGER"); break;
31993 case COLTYPE_REAL: printf(" REAL"); break;
31994 case COLTYPE_TEXT: printf(" TEXT"); break;
31995 case COLTYPE_CUSTOM: {
31996 if( flg & COLFLAG_HASTYPE ){
31997 const char *z = aCol[i].zCnName;
31998 z += strlen(z)+1;
31999 printf(" X-%s", z);
32000 break;
32001 }
32002 }
32003 }
32004 if( flg & COLFLAG_PRIMKEY ) printf(" PRIMARY KEY");
32005 if( flg & COLFLAG_HIDDEN ) printf(" HIDDEN");
32006#ifdef COLFLAG_NOEXPAND
32007 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND");
32008#endif
32009 if( flg ) printf(" flags=%04x", flg);
32010 printf("\n");
32011 fflush(stdout);
32012 sqlite3TreeViewPop(&pView);
32013 }
32014 sqlite3TreeViewPop(&pView);
32015}
32016
32017/*
32018** Generate a human-readable description of a WITH clause.
32019*/
32020SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
32021 int i;
32022 if( pWith==0 ) return;
32023 if( pWith->nCte==0 ) return;
32024 if( pWith->pOuter ){
32025 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
32026 }else{
32027 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
32028 }
32029 if( pWith->nCte>0 ){
32030 sqlite3TreeViewPush(&pView, moreToFollow);
32031 for(i=0; i<pWith->nCte; i++){
32032 StrAccum x;
32033 char zLine[1000];
32034 const struct Cte *pCte = &pWith->a[i];
32035 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
32036 sqlite3_str_appendf(&x, "%s", pCte->zName);
32037 if( pCte->pCols && pCte->pCols->nExpr>0 ){
32038 char cSep = '(';
32039 int j;
32040 for(j=0; j<pCte->pCols->nExpr; j++){
32041 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
32042 cSep = ',';
32043 }
32044 sqlite3_str_appendf(&x, ")");
32045 }
32046 if( pCte->eM10d!=M10d_Any ){
32047 sqlite3_str_appendf(&x, " %sMATERIALIZED",
32048 pCte->eM10d==M10d_No ? "NOT " : "");
32049 }
32050 if( pCte->pUse ){
32051 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
32052 pCte->pUse->nUse);
32053 }
32054 sqlite3StrAccumFinish(&x);
32055 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
32056 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
32057 sqlite3TreeViewPop(&pView);
32058 }
32059 sqlite3TreeViewPop(&pView);
32060 }
32061}
32062
32063/*
32064** Generate a human-readable description of a SrcList object.
32065*/
32066SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
32067 int i;
32068 if( pSrc==0 ) return;
32069 for(i=0; i<pSrc->nSrc; i++){
32070 const SrcItem *pItem = &pSrc->a[i];
32071 StrAccum x;
32072 int n = 0;
32073 char zLine[1000];
32074 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
32075 x.printfFlags |= SQLITE_PRINTF_INTERNAL;
32076 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
32077 if( pItem->pTab ){
32078 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
32079 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
32080 }
32081 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){
32082 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
32083 }else if( pItem->fg.jointype & JT_LEFT ){
32084 sqlite3_str_appendf(&x, " LEFT-JOIN");
32085 }else if( pItem->fg.jointype & JT_RIGHT ){
32086 sqlite3_str_appendf(&x, " RIGHT-JOIN");
32087 }else if( pItem->fg.jointype & JT_CROSS ){
32088 sqlite3_str_appendf(&x, " CROSS-JOIN");
32089 }
32090 if( pItem->fg.jointype & JT_LTORJ ){
32091 sqlite3_str_appendf(&x, " LTORJ");
32092 }
32093 if( pItem->fg.fromDDL ){
32094 sqlite3_str_appendf(&x, " DDL");
32095 }
32096 if( pItem->fg.isCte ){
32097 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
32098 }
32099 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
32100 sqlite3_str_appendf(&x, " ON");
32101 }
32102 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc");
32103 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated");
32104 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized");
32105 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine");
32106 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte");
32107 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom");
32108
32109 sqlite3StrAccumFinish(&x);
32110 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
32111 n = 0;
32112 if( pItem->pSelect ) n++;
32113 if( pItem->fg.isTabFunc ) n++;
32114 if( pItem->fg.isUsing ) n++;
32115 if( pItem->fg.isUsing ){
32116 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
32117 }
32118 if( pItem->pSelect ){
32119 if( pItem->pTab ){
32120 Table *pTab = pItem->pTab;
32121 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
32122 }
32123 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
32124 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
32125 }
32126 if( pItem->fg.isTabFunc ){
32127 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
32128 }
32129 sqlite3TreeViewPop(&pView);
32130 }
32131}
32132
32133/*
32134** Generate a human-readable description of a Select object.
32135*/
32136SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
32137 int n = 0;
32138 int cnt = 0;
32139 if( p==0 ){
32140 sqlite3TreeViewLine(pView, "nil-SELECT");
32141 return;
32142 }
32143 sqlite3TreeViewPush(&pView, moreToFollow);
32144 if( p->pWith ){
32145 sqlite3TreeViewWith(pView, p->pWith, 1);
32146 cnt = 1;
32147 sqlite3TreeViewPush(&pView, 1);
32148 }
32149 do{
32150 if( p->selFlags & SF_WhereBegin ){
32151 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
32152 }else{
32153 sqlite3TreeViewLine(pView,
32154 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
32155 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
32156 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
32157 p->selId, p, p->selFlags,
32158 (int)p->nSelectRow
32159 );
32160 }
32161 if( cnt++ ) sqlite3TreeViewPop(&pView);
32162 if( p->pPrior ){
32163 n = 1000;
32164 }else{
32165 n = 0;
32166 if( p->pSrc && p->pSrc->nSrc ) n++;
32167 if( p->pWhere ) n++;
32168 if( p->pGroupBy ) n++;
32169 if( p->pHaving ) n++;
32170 if( p->pOrderBy ) n++;
32171 if( p->pLimit ) n++;
32172#ifndef SQLITE_OMIT_WINDOWFUNC
32173 if( p->pWin ) n++;
32174 if( p->pWinDefn ) n++;
32175#endif
32176 }
32177 if( p->pEList ){
32178 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
32179 }
32180 n--;
32181#ifndef SQLITE_OMIT_WINDOWFUNC
32182 if( p->pWin ){
32183 Window *pX;
32184 sqlite3TreeViewPush(&pView, (n--)>0);
32185 sqlite3TreeViewLine(pView, "window-functions");
32186 for(pX=p->pWin; pX; pX=pX->pNextWin){
32187 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
32188 }
32189 sqlite3TreeViewPop(&pView);
32190 }
32191#endif
32192 if( p->pSrc && p->pSrc->nSrc ){
32193 sqlite3TreeViewPush(&pView, (n--)>0);
32194 sqlite3TreeViewLine(pView, "FROM");
32195 sqlite3TreeViewSrcList(pView, p->pSrc);
32196 sqlite3TreeViewPop(&pView);
32197 }
32198 if( p->pWhere ){
32199 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
32200 sqlite3TreeViewExpr(pView, p->pWhere, 0);
32201 sqlite3TreeViewPop(&pView);
32202 }
32203 if( p->pGroupBy ){
32204 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
32205 }
32206 if( p->pHaving ){
32207 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
32208 sqlite3TreeViewExpr(pView, p->pHaving, 0);
32209 sqlite3TreeViewPop(&pView);
32210 }
32211#ifndef SQLITE_OMIT_WINDOWFUNC
32212 if( p->pWinDefn ){
32213 Window *pX;
32214 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
32215 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
32216 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
32217 }
32218 sqlite3TreeViewPop(&pView);
32219 }
32220#endif
32221 if( p->pOrderBy ){
32222 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
32223 }
32224 if( p->pLimit ){
32225 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
32226 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
32227 if( p->pLimit->pRight ){
32228 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
32229 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
32230 sqlite3TreeViewPop(&pView);
32231 }
32232 sqlite3TreeViewPop(&pView);
32233 }
32234 if( p->pPrior ){
32235 const char *zOp = "UNION";
32236 switch( p->op ){
32237 case TK_ALL: zOp = "UNION ALL"; break;
32238 case TK_INTERSECT: zOp = "INTERSECT"; break;
32239 case TK_EXCEPT: zOp = "EXCEPT"; break;
32240 }
32241 sqlite3TreeViewItem(pView, zOp, 1);
32242 }
32243 p = p->pPrior;
32244 }while( p!=0 );
32245 sqlite3TreeViewPop(&pView);
32246}
32247
32248#ifndef SQLITE_OMIT_WINDOWFUNC
32249/*
32250** Generate a description of starting or stopping bounds
32251*/
32252SQLITE_PRIVATE void sqlite3TreeViewBound(
32253 TreeView *pView, /* View context */
32254 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
32255 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
32256 u8 moreToFollow /* True if more to follow */
32257){
32258 switch( eBound ){
32259 case TK_UNBOUNDED: {
32260 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
32261 sqlite3TreeViewPop(&pView);
32262 break;
32263 }
32264 case TK_CURRENT: {
32265 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
32266 sqlite3TreeViewPop(&pView);
32267 break;
32268 }
32269 case TK_PRECEDING: {
32270 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
32271 sqlite3TreeViewExpr(pView, pExpr, 0);
32272 sqlite3TreeViewPop(&pView);
32273 break;
32274 }
32275 case TK_FOLLOWING: {
32276 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
32277 sqlite3TreeViewExpr(pView, pExpr, 0);
32278 sqlite3TreeViewPop(&pView);
32279 break;
32280 }
32281 }
32282}
32283#endif /* SQLITE_OMIT_WINDOWFUNC */
32284
32285#ifndef SQLITE_OMIT_WINDOWFUNC
32286/*
32287** Generate a human-readable explanation for a Window object
32288*/
32289SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
32290 int nElement = 0;
32291 if( pWin==0 ) return;
32292 if( pWin->pFilter ){
32293 sqlite3TreeViewItem(pView, "FILTER", 1);
32294 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
32295 sqlite3TreeViewPop(&pView);
32296 }
32297 sqlite3TreeViewPush(&pView, more);
32298 if( pWin->zName ){
32299 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
32300 }else{
32301 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
32302 }
32303 if( pWin->zBase ) nElement++;
32304 if( pWin->pOrderBy ) nElement++;
32305 if( pWin->eFrmType ) nElement++;
32306 if( pWin->eExclude ) nElement++;
32307 if( pWin->zBase ){
32308 sqlite3TreeViewPush(&pView, (--nElement)>0);
32309 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
32310 sqlite3TreeViewPop(&pView);
32311 }
32312 if( pWin->pPartition ){
32313 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
32314 }
32315 if( pWin->pOrderBy ){
32316 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
32317 }
32318 if( pWin->eFrmType ){
32319 char zBuf[30];
32320 const char *zFrmType = "ROWS";
32321 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
32322 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
32323 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
32324 pWin->bImplicitFrame ? " (implied)" : "");
32325 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
32326 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
32327 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
32328 sqlite3TreeViewPop(&pView);
32329 }
32330 if( pWin->eExclude ){
32331 char zBuf[30];
32332 const char *zExclude;
32333 switch( pWin->eExclude ){
32334 case TK_NO: zExclude = "NO OTHERS"; break;
32335 case TK_CURRENT: zExclude = "CURRENT ROW"; break;
32336 case TK_GROUP: zExclude = "GROUP"; break;
32337 case TK_TIES: zExclude = "TIES"; break;
32338 default:
32339 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
32340 zExclude = zBuf;
32341 break;
32342 }
32343 sqlite3TreeViewPush(&pView, 0);
32344 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
32345 sqlite3TreeViewPop(&pView);
32346 }
32347 sqlite3TreeViewPop(&pView);
32348}
32349#endif /* SQLITE_OMIT_WINDOWFUNC */
32350
32351#ifndef SQLITE_OMIT_WINDOWFUNC
32352/*
32353** Generate a human-readable explanation for a Window Function object
32354*/
32355SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
32356 if( pWin==0 ) return;
32357 sqlite3TreeViewPush(&pView, more);
32358 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
32359 pWin->pWFunc->zName, pWin->pWFunc->nArg);
32360 sqlite3TreeViewWindow(pView, pWin, 0);
32361 sqlite3TreeViewPop(&pView);
32362}
32363#endif /* SQLITE_OMIT_WINDOWFUNC */
32364
32365/*
32366** Generate a human-readable explanation of an expression tree.
32367*/
32368SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
32369 const char *zBinOp = 0; /* Binary operator */
32370 const char *zUniOp = 0; /* Unary operator */
32371 char zFlgs[200];
32372 sqlite3TreeViewPush(&pView, moreToFollow);
32373 if( pExpr==0 ){
32374 sqlite3TreeViewLine(pView, "nil");
32375 sqlite3TreeViewPop(&pView);
32376 return;
32377 }
32378 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){
32379 StrAccum x;
32380 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
32381 sqlite3_str_appendf(&x, " fg.af=%x.%c",
32382 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
32383 if( ExprHasProperty(pExpr, EP_OuterON) ){
32384 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
32385 }
32386 if( ExprHasProperty(pExpr, EP_InnerON) ){
32387 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
32388 }
32389 if( ExprHasProperty(pExpr, EP_FromDDL) ){
32390 sqlite3_str_appendf(&x, " DDL");
32391 }
32392 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
32393 sqlite3_str_appendf(&x, " IMMUTABLE");
32394 }
32395 if( pExpr->pAggInfo!=0 ){
32396 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg);
32397 }
32398 sqlite3StrAccumFinish(&x);
32399 }else{
32400 zFlgs[0] = 0;
32401 }
32402 switch( pExpr->op ){
32403 case TK_AGG_COLUMN: {
32404 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
32405 pExpr->iTable, pExpr->iColumn, zFlgs);
32406 break;
32407 }
32408 case TK_COLUMN: {
32409 if( pExpr->iTable<0 ){
32410 /* This only happens when coding check constraints */
32411 char zOp2[16];
32412 if( pExpr->op2 ){
32413 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
32414 }else{
32415 zOp2[0] = 0;
32416 }
32417 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
32418 pExpr->iColumn, zFlgs, zOp2);
32419 }else{
32420 assert( ExprUseYTab(pExpr) );
32421 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
32422 pExpr->iTable, pExpr->iColumn,
32423 pExpr->y.pTab, zFlgs);
32424 }
32425 if( ExprHasProperty(pExpr, EP_FixedCol) ){
32426 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32427 }
32428 break;
32429 }
32430 case TK_INTEGER: {
32431 if( pExpr->flags & EP_IntValue ){
32432 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
32433 }else{
32434 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
32435 }
32436 break;
32437 }
32438#ifndef SQLITE_OMIT_FLOATING_POINT
32439 case TK_FLOAT: {
32440 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32441 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
32442 break;
32443 }
32444#endif
32445 case TK_STRING: {
32446 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32447 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
32448 break;
32449 }
32450 case TK_NULL: {
32451 sqlite3TreeViewLine(pView,"NULL");
32452 break;
32453 }
32454 case TK_TRUEFALSE: {
32455 sqlite3TreeViewLine(pView,"%s%s",
32456 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
32457 break;
32458 }
32459#ifndef SQLITE_OMIT_BLOB_LITERAL
32460 case TK_BLOB: {
32461 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32462 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
32463 break;
32464 }
32465#endif
32466 case TK_VARIABLE: {
32467 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32468 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
32469 pExpr->u.zToken, pExpr->iColumn);
32470 break;
32471 }
32472 case TK_REGISTER: {
32473 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
32474 break;
32475 }
32476 case TK_ID: {
32477 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32478 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
32479 break;
32480 }
32481#ifndef SQLITE_OMIT_CAST
32482 case TK_CAST: {
32483 /* Expressions of the form: CAST(pLeft AS token) */
32484 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32485 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
32486 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32487 break;
32488 }
32489#endif /* SQLITE_OMIT_CAST */
32490 case TK_LT: zBinOp = "LT"; break;
32491 case TK_LE: zBinOp = "LE"; break;
32492 case TK_GT: zBinOp = "GT"; break;
32493 case TK_GE: zBinOp = "GE"; break;
32494 case TK_NE: zBinOp = "NE"; break;
32495 case TK_EQ: zBinOp = "EQ"; break;
32496 case TK_IS: zBinOp = "IS"; break;
32497 case TK_ISNOT: zBinOp = "ISNOT"; break;
32498 case TK_AND: zBinOp = "AND"; break;
32499 case TK_OR: zBinOp = "OR"; break;
32500 case TK_PLUS: zBinOp = "ADD"; break;
32501 case TK_STAR: zBinOp = "MUL"; break;
32502 case TK_MINUS: zBinOp = "SUB"; break;
32503 case TK_REM: zBinOp = "REM"; break;
32504 case TK_BITAND: zBinOp = "BITAND"; break;
32505 case TK_BITOR: zBinOp = "BITOR"; break;
32506 case TK_SLASH: zBinOp = "DIV"; break;
32507 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
32508 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
32509 case TK_CONCAT: zBinOp = "CONCAT"; break;
32510 case TK_DOT: zBinOp = "DOT"; break;
32511 case TK_LIMIT: zBinOp = "LIMIT"; break;
32512
32513 case TK_UMINUS: zUniOp = "UMINUS"; break;
32514 case TK_UPLUS: zUniOp = "UPLUS"; break;
32515 case TK_BITNOT: zUniOp = "BITNOT"; break;
32516 case TK_NOT: zUniOp = "NOT"; break;
32517 case TK_ISNULL: zUniOp = "ISNULL"; break;
32518 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
32519
32520 case TK_TRUTH: {
32521 int x;
32522 const char *azOp[] = {
32523 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
32524 };
32525 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
32526 assert( pExpr->pRight );
32527 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op
32528 == TK_TRUEFALSE );
32529 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
32530 zUniOp = azOp[x];
32531 break;
32532 }
32533
32534 case TK_SPAN: {
32535 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32536 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
32537 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32538 break;
32539 }
32540
32541 case TK_COLLATE: {
32542 /* COLLATE operators without the EP_Collate flag are intended to
32543 ** emulate collation associated with a table column. These show
32544 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
32545 ** operators that appear in the original SQL always have the
32546 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
32547 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32548 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
32549 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
32550 pExpr->u.zToken, zFlgs);
32551 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32552 break;
32553 }
32554
32555 case TK_AGG_FUNCTION:
32556 case TK_FUNCTION: {
32557 ExprList *pFarg; /* List of function arguments */
32558 Window *pWin;
32559 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
32560 pFarg = 0;
32561 pWin = 0;
32562 }else{
32563 assert( ExprUseXList(pExpr) );
32564 pFarg = pExpr->x.pList;
32565#ifndef SQLITE_OMIT_WINDOWFUNC
32566 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
32567#else
32568 pWin = 0;
32569#endif
32570 }
32571 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32572 if( pExpr->op==TK_AGG_FUNCTION ){
32573 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
32574 pExpr->op2, pExpr->u.zToken, zFlgs,
32575 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
32576 pExpr->iAgg, pExpr->pAggInfo);
32577 }else if( pExpr->op2!=0 ){
32578 const char *zOp2;
32579 char zBuf[8];
32580 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
32581 zOp2 = zBuf;
32582 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
32583 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
32584 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
32585 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
32586 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
32587 pExpr->u.zToken, zFlgs, zOp2);
32588 }else{
32589 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
32590 }
32591 if( pFarg ){
32592 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
32593 }
32594#ifndef SQLITE_OMIT_WINDOWFUNC
32595 if( pWin ){
32596 sqlite3TreeViewWindow(pView, pWin, 0);
32597 }
32598#endif
32599 break;
32600 }
32601#ifndef SQLITE_OMIT_SUBQUERY
32602 case TK_EXISTS: {
32603 assert( ExprUseXSelect(pExpr) );
32604 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
32605 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32606 break;
32607 }
32608 case TK_SELECT: {
32609 assert( ExprUseXSelect(pExpr) );
32610 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
32611 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32612 break;
32613 }
32614 case TK_IN: {
32615 sqlite3_str *pStr = sqlite3_str_new(0);
32616 char *z;
32617 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
32618 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
32619 if( ExprHasProperty(pExpr, EP_Subrtn) ){
32620 sqlite3_str_appendf(pStr, " subrtn(%d,%d)",
32621 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
32622 }
32623 z = sqlite3_str_finish(pStr);
32624 sqlite3TreeViewLine(pView, z);
32625 sqlite3_free(z);
32626 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32627 if( ExprUseXSelect(pExpr) ){
32628 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32629 }else{
32630 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
32631 }
32632 break;
32633 }
32634#endif /* SQLITE_OMIT_SUBQUERY */
32635
32636 /*
32637 ** x BETWEEN y AND z
32638 **
32639 ** This is equivalent to
32640 **
32641 ** x>=y AND x<=z
32642 **
32643 ** X is stored in pExpr->pLeft.
32644 ** Y is stored in pExpr->pList->a[0].pExpr.
32645 ** Z is stored in pExpr->pList->a[1].pExpr.
32646 */
32647 case TK_BETWEEN: {
32648 const Expr *pX, *pY, *pZ;
32649 pX = pExpr->pLeft;
32650 assert( ExprUseXList(pExpr) );
32651 assert( pExpr->x.pList->nExpr==2 );
32652 pY = pExpr->x.pList->a[0].pExpr;
32653 pZ = pExpr->x.pList->a[1].pExpr;
32654 sqlite3TreeViewLine(pView, "BETWEEN");
32655 sqlite3TreeViewExpr(pView, pX, 1);
32656 sqlite3TreeViewExpr(pView, pY, 1);
32657 sqlite3TreeViewExpr(pView, pZ, 0);
32658 break;
32659 }
32660 case TK_TRIGGER: {
32661 /* If the opcode is TK_TRIGGER, then the expression is a reference
32662 ** to a column in the new.* or old.* pseudo-tables available to
32663 ** trigger programs. In this case Expr.iTable is set to 1 for the
32664 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
32665 ** is set to the column of the pseudo-table to read, or to -1 to
32666 ** read the rowid field.
32667 */
32668 sqlite3TreeViewLine(pView, "%s(%d)",
32669 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
32670 break;
32671 }
32672 case TK_CASE: {
32673 sqlite3TreeViewLine(pView, "CASE");
32674 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32675 assert( ExprUseXList(pExpr) );
32676 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
32677 break;
32678 }
32679#ifndef SQLITE_OMIT_TRIGGER
32680 case TK_RAISE: {
32681 const char *zType = "unk";
32682 switch( pExpr->affExpr ){
32683 case OE_Rollback: zType = "rollback"; break;
32684 case OE_Abort: zType = "abort"; break;
32685 case OE_Fail: zType = "fail"; break;
32686 case OE_Ignore: zType = "ignore"; break;
32687 }
32688 assert( !ExprHasProperty(pExpr, EP_IntValue) );
32689 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
32690 break;
32691 }
32692#endif
32693 case TK_MATCH: {
32694 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
32695 pExpr->iTable, pExpr->iColumn, zFlgs);
32696 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
32697 break;
32698 }
32699 case TK_VECTOR: {
32700 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
32701 assert( ExprUseXList(pExpr) );
32702 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
32703 sqlite3_free(z);
32704 break;
32705 }
32706 case TK_SELECT_COLUMN: {
32707 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
32708 pExpr->iColumn, pExpr->iTable-1,
32709 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
32710 assert( ExprUseXSelect(pExpr->pLeft) );
32711 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
32712 break;
32713 }
32714 case TK_IF_NULL_ROW: {
32715 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
32716 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32717 break;
32718 }
32719 case TK_ERROR: {
32720 Expr tmp;
32721 sqlite3TreeViewLine(pView, "ERROR");
32722 tmp = *pExpr;
32723 tmp.op = pExpr->op2;
32724 sqlite3TreeViewExpr(pView, &tmp, 0);
32725 break;
32726 }
32727 case TK_ROW: {
32728 if( pExpr->iColumn<=0 ){
32729 sqlite3TreeViewLine(pView, "First FROM table rowid");
32730 }else{
32731 sqlite3TreeViewLine(pView, "First FROM table column %d",
32732 pExpr->iColumn-1);
32733 }
32734 break;
32735 }
32736 default: {
32737 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
32738 break;
32739 }
32740 }
32741 if( zBinOp ){
32742 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
32743 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32744 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
32745 }else if( zUniOp ){
32746 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
32747 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32748 }
32749 sqlite3TreeViewPop(&pView);
32750}
32751
32752
32753/*
32754** Generate a human-readable explanation of an expression list.
32755*/
32756SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
32757 TreeView *pView,
32758 const ExprList *pList,
32759 const char *zLabel
32760){
32761 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
32762 if( pList==0 ){
32763 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
32764 }else{
32765 int i;
32766 sqlite3TreeViewLine(pView, "%s", zLabel);
32767 for(i=0; i<pList->nExpr; i++){
32768 int j = pList->a[i].u.x.iOrderByCol;
32769 char *zName = pList->a[i].zEName;
32770 int moreToFollow = i<pList->nExpr - 1;
32771 if( j || zName ){
32772 sqlite3TreeViewPush(&pView, moreToFollow);
32773 moreToFollow = 0;
32774 sqlite3TreeViewLine(pView, 0);
32775 if( zName ){
32776 switch( pList->a[i].fg.eEName ){
32777 default:
32778 fprintf(stdout, "AS %s ", zName);
32779 break;
32780 case ENAME_TAB:
32781 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
32782 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) ");
32783 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) ");
32784 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) ");
32785 break;
32786 case ENAME_SPAN:
32787 fprintf(stdout, "SPAN(\"%s\") ", zName);
32788 break;
32789 }
32790 }
32791 if( j ){
32792 fprintf(stdout, "iOrderByCol=%d", j);
32793 }
32794 fprintf(stdout, "\n");
32795 fflush(stdout);
32796 }
32797 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
32798 if( j || zName ){
32799 sqlite3TreeViewPop(&pView);
32800 }
32801 }
32802 }
32803}
32804SQLITE_PRIVATE void sqlite3TreeViewExprList(
32805 TreeView *pView,
32806 const ExprList *pList,
32807 u8 moreToFollow,
32808 const char *zLabel
32809){
32810 sqlite3TreeViewPush(&pView, moreToFollow);
32811 sqlite3TreeViewBareExprList(pView, pList, zLabel);
32812 sqlite3TreeViewPop(&pView);
32813}
32814
32815/*
32816** Generate a human-readable explanation of an id-list.
32817*/
32818SQLITE_PRIVATE void sqlite3TreeViewBareIdList(
32819 TreeView *pView,
32820 const IdList *pList,
32821 const char *zLabel
32822){
32823 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
32824 if( pList==0 ){
32825 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
32826 }else{
32827 int i;
32828 sqlite3TreeViewLine(pView, "%s", zLabel);
32829 for(i=0; i<pList->nId; i++){
32830 char *zName = pList->a[i].zName;
32831 int moreToFollow = i<pList->nId - 1;
32832 if( zName==0 ) zName = "(null)";
32833 sqlite3TreeViewPush(&pView, moreToFollow);
32834 sqlite3TreeViewLine(pView, 0);
32835 if( pList->eU4==EU4_NONE ){
32836 fprintf(stdout, "%s\n", zName);
32837 }else if( pList->eU4==EU4_IDX ){
32838 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
32839 }else{
32840 assert( pList->eU4==EU4_EXPR );
32841 if( pList->a[i].u4.pExpr==0 ){
32842 fprintf(stdout, "%s (pExpr=NULL)\n", zName);
32843 }else{
32844 fprintf(stdout, "%s\n", zName);
32845 sqlite3TreeViewPush(&pView, i<pList->nId-1);
32846 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
32847 sqlite3TreeViewPop(&pView);
32848 }
32849 }
32850 sqlite3TreeViewPop(&pView);
32851 }
32852 }
32853}
32854SQLITE_PRIVATE void sqlite3TreeViewIdList(
32855 TreeView *pView,
32856 const IdList *pList,
32857 u8 moreToFollow,
32858 const char *zLabel
32859){
32860 sqlite3TreeViewPush(&pView, moreToFollow);
32861 sqlite3TreeViewBareIdList(pView, pList, zLabel);
32862 sqlite3TreeViewPop(&pView);
32863}
32864
32865/*
32866** Generate a human-readable explanation of a list of Upsert objects
32867*/
32868SQLITE_PRIVATE void sqlite3TreeViewUpsert(
32869 TreeView *pView,
32870 const Upsert *pUpsert,
32871 u8 moreToFollow
32872){
32873 if( pUpsert==0 ) return;
32874 sqlite3TreeViewPush(&pView, moreToFollow);
32875 while( pUpsert ){
32876 int n;
32877 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
32878 sqlite3TreeViewLine(pView, "ON CONFLICT DO %s",
32879 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
32880 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
32881 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
32882 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
32883 if( pUpsert->pUpsertWhere ){
32884 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
32885 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
32886 sqlite3TreeViewPop(&pView);
32887 }
32888 sqlite3TreeViewPop(&pView);
32889 pUpsert = pUpsert->pNextUpsert;
32890 }
32891 sqlite3TreeViewPop(&pView);
32892}
32893
32894#if TREETRACE_ENABLED
32895/*
32896** Generate a human-readable diagram of the data structure that go
32897** into generating an DELETE statement.
32898*/
32899SQLITE_PRIVATE void sqlite3TreeViewDelete(
32900 const With *pWith,
32901 const SrcList *pTabList,
32902 const Expr *pWhere,
32903 const ExprList *pOrderBy,
32904 const Expr *pLimit,
32905 const Trigger *pTrigger
32906){
32907 int n = 0;
32908 TreeView *pView = 0;
32909 sqlite3TreeViewPush(&pView, 0);
32910 sqlite3TreeViewLine(pView, "DELETE");
32911 if( pWith ) n++;
32912 if( pTabList ) n++;
32913 if( pWhere ) n++;
32914 if( pOrderBy ) n++;
32915 if( pLimit ) n++;
32916 if( pTrigger ) n++;
32917 if( pWith ){
32918 sqlite3TreeViewPush(&pView, (--n)>0);
32919 sqlite3TreeViewWith(pView, pWith, 0);
32920 sqlite3TreeViewPop(&pView);
32921 }
32922 if( pTabList ){
32923 sqlite3TreeViewPush(&pView, (--n)>0);
32924 sqlite3TreeViewLine(pView, "FROM");
32925 sqlite3TreeViewSrcList(pView, pTabList);
32926 sqlite3TreeViewPop(&pView);
32927 }
32928 if( pWhere ){
32929 sqlite3TreeViewPush(&pView, (--n)>0);
32930 sqlite3TreeViewLine(pView, "WHERE");
32931 sqlite3TreeViewExpr(pView, pWhere, 0);
32932 sqlite3TreeViewPop(&pView);
32933 }
32934 if( pOrderBy ){
32935 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32936 }
32937 if( pLimit ){
32938 sqlite3TreeViewPush(&pView, (--n)>0);
32939 sqlite3TreeViewLine(pView, "LIMIT");
32940 sqlite3TreeViewExpr(pView, pLimit, 0);
32941 sqlite3TreeViewPop(&pView);
32942 }
32943 if( pTrigger ){
32944 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32945 }
32946 sqlite3TreeViewPop(&pView);
32947}
32948#endif /* TREETRACE_ENABLED */
32949
32950#if TREETRACE_ENABLED
32951/*
32952** Generate a human-readable diagram of the data structure that go
32953** into generating an INSERT statement.
32954*/
32955SQLITE_PRIVATE void sqlite3TreeViewInsert(
32956 const With *pWith,
32957 const SrcList *pTabList,
32958 const IdList *pColumnList,
32959 const Select *pSelect,
32960 const ExprList *pExprList,
32961 int onError,
32962 const Upsert *pUpsert,
32963 const Trigger *pTrigger
32964){
32965 TreeView *pView = 0;
32966 int n = 0;
32967 const char *zLabel = "INSERT";
32968 switch( onError ){
32969 case OE_Replace: zLabel = "REPLACE"; break;
32970 case OE_Ignore: zLabel = "INSERT OR IGNORE"; break;
32971 case OE_Rollback: zLabel = "INSERT OR ROLLBACK"; break;
32972 case OE_Abort: zLabel = "INSERT OR ABORT"; break;
32973 case OE_Fail: zLabel = "INSERT OR FAIL"; break;
32974 }
32975 sqlite3TreeViewPush(&pView, 0);
32976 sqlite3TreeViewLine(pView, zLabel);
32977 if( pWith ) n++;
32978 if( pTabList ) n++;
32979 if( pColumnList ) n++;
32980 if( pSelect ) n++;
32981 if( pExprList ) n++;
32982 if( pUpsert ) n++;
32983 if( pTrigger ) n++;
32984 if( pWith ){
32985 sqlite3TreeViewPush(&pView, (--n)>0);
32986 sqlite3TreeViewWith(pView, pWith, 0);
32987 sqlite3TreeViewPop(&pView);
32988 }
32989 if( pTabList ){
32990 sqlite3TreeViewPush(&pView, (--n)>0);
32991 sqlite3TreeViewLine(pView, "INTO");
32992 sqlite3TreeViewSrcList(pView, pTabList);
32993 sqlite3TreeViewPop(&pView);
32994 }
32995 if( pColumnList ){
32996 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
32997 }
32998 if( pSelect ){
32999 sqlite3TreeViewPush(&pView, (--n)>0);
33000 sqlite3TreeViewLine(pView, "DATA-SOURCE");
33001 sqlite3TreeViewSelect(pView, pSelect, 0);
33002 sqlite3TreeViewPop(&pView);
33003 }
33004 if( pExprList ){
33005 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
33006 }
33007 if( pUpsert ){
33008 sqlite3TreeViewPush(&pView, (--n)>0);
33009 sqlite3TreeViewLine(pView, "UPSERT");
33010 sqlite3TreeViewUpsert(pView, pUpsert, 0);
33011 sqlite3TreeViewPop(&pView);
33012 }
33013 if( pTrigger ){
33014 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
33015 }
33016 sqlite3TreeViewPop(&pView);
33017}
33018#endif /* TREETRACE_ENABLED */
33019
33020#if TREETRACE_ENABLED
33021/*
33022** Generate a human-readable diagram of the data structure that go
33023** into generating an UPDATE statement.
33024*/
33025SQLITE_PRIVATE void sqlite3TreeViewUpdate(
33026 const With *pWith,
33027 const SrcList *pTabList,
33028 const ExprList *pChanges,
33029 const Expr *pWhere,
33030 int onError,
33031 const ExprList *pOrderBy,
33032 const Expr *pLimit,
33033 const Upsert *pUpsert,
33034 const Trigger *pTrigger
33035){
33036 int n = 0;
33037 TreeView *pView = 0;
33038 const char *zLabel = "UPDATE";
33039 switch( onError ){
33040 case OE_Replace: zLabel = "UPDATE OR REPLACE"; break;
33041 case OE_Ignore: zLabel = "UPDATE OR IGNORE"; break;
33042 case OE_Rollback: zLabel = "UPDATE OR ROLLBACK"; break;
33043 case OE_Abort: zLabel = "UPDATE OR ABORT"; break;
33044 case OE_Fail: zLabel = "UPDATE OR FAIL"; break;
33045 }
33046 sqlite3TreeViewPush(&pView, 0);
33047 sqlite3TreeViewLine(pView, zLabel);
33048 if( pWith ) n++;
33049 if( pTabList ) n++;
33050 if( pChanges ) n++;
33051 if( pWhere ) n++;
33052 if( pOrderBy ) n++;
33053 if( pLimit ) n++;
33054 if( pUpsert ) n++;
33055 if( pTrigger ) n++;
33056 if( pWith ){
33057 sqlite3TreeViewPush(&pView, (--n)>0);
33058 sqlite3TreeViewWith(pView, pWith, 0);
33059 sqlite3TreeViewPop(&pView);
33060 }
33061 if( pTabList ){
33062 sqlite3TreeViewPush(&pView, (--n)>0);
33063 sqlite3TreeViewLine(pView, "FROM");
33064 sqlite3TreeViewSrcList(pView, pTabList);
33065 sqlite3TreeViewPop(&pView);
33066 }
33067 if( pChanges ){
33068 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
33069 }
33070 if( pWhere ){
33071 sqlite3TreeViewPush(&pView, (--n)>0);
33072 sqlite3TreeViewLine(pView, "WHERE");
33073 sqlite3TreeViewExpr(pView, pWhere, 0);
33074 sqlite3TreeViewPop(&pView);
33075 }
33076 if( pOrderBy ){
33077 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
33078 }
33079 if( pLimit ){
33080 sqlite3TreeViewPush(&pView, (--n)>0);
33081 sqlite3TreeViewLine(pView, "LIMIT");
33082 sqlite3TreeViewExpr(pView, pLimit, 0);
33083 sqlite3TreeViewPop(&pView);
33084 }
33085 if( pUpsert ){
33086 sqlite3TreeViewPush(&pView, (--n)>0);
33087 sqlite3TreeViewLine(pView, "UPSERT");
33088 sqlite3TreeViewUpsert(pView, pUpsert, 0);
33089 sqlite3TreeViewPop(&pView);
33090 }
33091 if( pTrigger ){
33092 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
33093 }
33094 sqlite3TreeViewPop(&pView);
33095}
33096#endif /* TREETRACE_ENABLED */
33097
33098#ifndef SQLITE_OMIT_TRIGGER
33099/*
33100** Show a human-readable graph of a TriggerStep
33101*/
33102SQLITE_PRIVATE void sqlite3TreeViewTriggerStep(
33103 TreeView *pView,
33104 const TriggerStep *pStep,
33105 u8 moreToFollow,
33106 u8 showFullList
33107){
33108 int cnt = 0;
33109 if( pStep==0 ) return;
33110 sqlite3TreeViewPush(&pView,
33111 moreToFollow || (showFullList && pStep->pNext!=0));
33112 do{
33113 if( cnt++ && pStep->pNext==0 ){
33114 sqlite3TreeViewPop(&pView);
33115 sqlite3TreeViewPush(&pView, 0);
33116 }
33117 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
33118 }while( showFullList && (pStep = pStep->pNext)!=0 );
33119 sqlite3TreeViewPop(&pView);
33120}
33121
33122/*
33123** Show a human-readable graph of a Trigger
33124*/
33125SQLITE_PRIVATE void sqlite3TreeViewTrigger(
33126 TreeView *pView,
33127 const Trigger *pTrigger,
33128 u8 moreToFollow,
33129 u8 showFullList
33130){
33131 int cnt = 0;
33132 if( pTrigger==0 ) return;
33133 sqlite3TreeViewPush(&pView,
33134 moreToFollow || (showFullList && pTrigger->pNext!=0));
33135 do{
33136 if( cnt++ && pTrigger->pNext==0 ){
33137 sqlite3TreeViewPop(&pView);
33138 sqlite3TreeViewPush(&pView, 0);
33139 }
33140 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
33141 sqlite3TreeViewPush(&pView, 0);
33142 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
33143 sqlite3TreeViewPop(&pView);
33144 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
33145 sqlite3TreeViewPop(&pView);
33146}
33147#endif /* SQLITE_OMIT_TRIGGER */
33148
33149
33150/*
33151** These simplified versions of the tree-view routines omit unnecessary
33152** parameters. These variants are intended to be used from a symbolic
33153** debugger, such as "gdb", during interactive debugging sessions.
33154**
33155** This routines are given external linkage so that they will always be
33156** accessible to the debugging, and to avoid warnings about unused
33157** functions. But these routines only exist in debugging builds, so they
33158** do not contaminate the interface.
33159*/
33160SQLITE_PRIVATE void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
33161SQLITE_PRIVATE void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
33162SQLITE_PRIVATE void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
33163SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
33164SQLITE_PRIVATE void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); }
33165SQLITE_PRIVATE void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); }
33166SQLITE_PRIVATE void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); }
33167#ifndef SQLITE_OMIT_TRIGGER
33168SQLITE_PRIVATE void sqlite3ShowTriggerStep(const TriggerStep *p){
33169 sqlite3TreeViewTriggerStep(0,p,0,0);
33170}
33171SQLITE_PRIVATE void sqlite3ShowTriggerStepList(const TriggerStep *p){
33172 sqlite3TreeViewTriggerStep(0,p,0,1);
33173}
33174SQLITE_PRIVATE void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); }
33175SQLITE_PRIVATE void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);}
33176#endif
33177#ifndef SQLITE_OMIT_WINDOWFUNC
33178SQLITE_PRIVATE void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); }
33179SQLITE_PRIVATE void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); }
33180#endif
33181
33182#endif /* SQLITE_DEBUG */
33183
33184/************** End of treeview.c ********************************************/
33185/************** Begin file random.c ******************************************/
33186/*
33187** 2001 September 15
33188**
33189** The author disclaims copyright to this source code. In place of
33190** a legal notice, here is a blessing:
33191**
33192** May you do good and not evil.
33193** May you find forgiveness for yourself and forgive others.
33194** May you share freely, never taking more than you give.
33195**
33196*************************************************************************
33197** This file contains code to implement a pseudo-random number
33198** generator (PRNG) for SQLite.
33199**
33200** Random numbers are used by some of the database backends in order
33201** to generate random integer keys for tables or random filenames.
33202*/
33203/* #include "sqliteInt.h" */
33204
33205
33206/* All threads share a single random number generator.
33207** This structure is the current state of the generator.
33208*/
33209static SQLITE_WSD struct sqlite3PrngType {
33210 u32 s[16]; /* 64 bytes of chacha20 state */
33211 u8 out[64]; /* Output bytes */
33212 u8 n; /* Output bytes remaining */
33213} sqlite3Prng;
33214
33215
33216/* The RFC-7539 ChaCha20 block function
33217*/
33218#define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
33219#define QR(a, b, c, d) ( \
33220 a += b, d ^= a, d = ROTL(d,16), \
33221 c += d, b ^= c, b = ROTL(b,12), \
33222 a += b, d ^= a, d = ROTL(d, 8), \
33223 c += d, b ^= c, b = ROTL(b, 7))
33224static void chacha_block(u32 *out, const u32 *in){
33225 int i;
33226 u32 x[16];
33227 memcpy(dest: x, src: in, n: 64);
33228 for(i=0; i<10; i++){
33229 QR(x[0], x[4], x[ 8], x[12]);
33230 QR(x[1], x[5], x[ 9], x[13]);
33231 QR(x[2], x[6], x[10], x[14]);
33232 QR(x[3], x[7], x[11], x[15]);
33233 QR(x[0], x[5], x[10], x[15]);
33234 QR(x[1], x[6], x[11], x[12]);
33235 QR(x[2], x[7], x[ 8], x[13]);
33236 QR(x[3], x[4], x[ 9], x[14]);
33237 }
33238 for(i=0; i<16; i++) out[i] = x[i]+in[i];
33239}
33240
33241/*
33242** Return N random bytes.
33243*/
33244SQLITE_API void sqlite3_randomness(int N, void *pBuf){
33245 unsigned char *zBuf = pBuf;
33246
33247 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
33248 ** state vector. If writable static data is unsupported on the target,
33249 ** we have to locate the state vector at run-time. In the more common
33250 ** case where writable static data is supported, wsdPrng can refer directly
33251 ** to the "sqlite3Prng" state vector declared above.
33252 */
33253#ifdef SQLITE_OMIT_WSD
33254 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
33255# define wsdPrng p[0]
33256#else
33257# define wsdPrng sqlite3Prng
33258#endif
33259
33260#if SQLITE_THREADSAFE
33261 sqlite3_mutex *mutex;
33262#endif
33263
33264#ifndef SQLITE_OMIT_AUTOINIT
33265 if( sqlite3_initialize() ) return;
33266#endif
33267
33268#if SQLITE_THREADSAFE
33269 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
33270#endif
33271
33272 sqlite3_mutex_enter(p: mutex);
33273 if( N<=0 || pBuf==0 ){
33274 wsdPrng.s[0] = 0;
33275 sqlite3_mutex_leave(p: mutex);
33276 return;
33277 }
33278
33279 /* Initialize the state of the random number generator once,
33280 ** the first time this routine is called.
33281 */
33282 if( wsdPrng.s[0]==0 ){
33283 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs: 0);
33284 static const u32 chacha20_init[] = {
33285 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
33286 };
33287 memcpy(dest: &wsdPrng.s[0], src: chacha20_init, n: 16);
33288 if( NEVER(pVfs==0) ){
33289 memset(s: &wsdPrng.s[4], c: 0, n: 44);
33290 }else{
33291 sqlite3OsRandomness(pVfs, nByte: 44, zBufOut: (char*)&wsdPrng.s[4]);
33292 }
33293 wsdPrng.s[15] = wsdPrng.s[12];
33294 wsdPrng.s[12] = 0;
33295 wsdPrng.n = 0;
33296 }
33297
33298 assert( N>0 );
33299 while( 1 /* exit by break */ ){
33300 if( N<=wsdPrng.n ){
33301 memcpy(dest: zBuf, src: &wsdPrng.out[wsdPrng.n-N], n: N);
33302 wsdPrng.n -= N;
33303 break;
33304 }
33305 if( wsdPrng.n>0 ){
33306 memcpy(dest: zBuf, wsdPrng.out, wsdPrng.n);
33307 N -= wsdPrng.n;
33308 zBuf += wsdPrng.n;
33309 }
33310 wsdPrng.s[12]++;
33311 chacha_block(out: (u32*)wsdPrng.out, wsdPrng.s);
33312 wsdPrng.n = 64;
33313 }
33314 sqlite3_mutex_leave(p: mutex);
33315}
33316
33317#ifndef SQLITE_UNTESTABLE
33318/*
33319** For testing purposes, we sometimes want to preserve the state of
33320** PRNG and restore the PRNG to its saved state at a later time, or
33321** to reset the PRNG to its initial state. These routines accomplish
33322** those tasks.
33323**
33324** The sqlite3_test_control() interface calls these routines to
33325** control the PRNG.
33326*/
33327static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
33328SQLITE_PRIVATE void sqlite3PrngSaveState(void){
33329 memcpy(
33330 dest: &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
33331 src: &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
33332 n: sizeof(sqlite3Prng)
33333 );
33334}
33335SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
33336 memcpy(
33337 dest: &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
33338 src: &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
33339 n: sizeof(sqlite3Prng)
33340 );
33341}
33342#endif /* SQLITE_UNTESTABLE */
33343
33344/************** End of random.c **********************************************/
33345/************** Begin file threads.c *****************************************/
33346/*
33347** 2012 July 21
33348**
33349** The author disclaims copyright to this source code. In place of
33350** a legal notice, here is a blessing:
33351**
33352** May you do good and not evil.
33353** May you find forgiveness for yourself and forgive others.
33354** May you share freely, never taking more than you give.
33355**
33356******************************************************************************
33357**
33358** This file presents a simple cross-platform threading interface for
33359** use internally by SQLite.
33360**
33361** A "thread" can be created using sqlite3ThreadCreate(). This thread
33362** runs independently of its creator until it is joined using
33363** sqlite3ThreadJoin(), at which point it terminates.
33364**
33365** Threads do not have to be real. It could be that the work of the
33366** "thread" is done by the main thread at either the sqlite3ThreadCreate()
33367** or sqlite3ThreadJoin() call. This is, in fact, what happens in
33368** single threaded systems. Nothing in SQLite requires multiple threads.
33369** This interface exists so that applications that want to take advantage
33370** of multiple cores can do so, while also allowing applications to stay
33371** single-threaded if desired.
33372*/
33373/* #include "sqliteInt.h" */
33374#if SQLITE_OS_WIN
33375/* # include "os_win.h" */
33376#endif
33377
33378#if SQLITE_MAX_WORKER_THREADS>0
33379
33380/********************************* Unix Pthreads ****************************/
33381#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
33382
33383#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33384/* #include <pthread.h> */
33385
33386/* A running thread */
33387struct SQLiteThread {
33388 pthread_t tid; /* Thread ID */
33389 int done; /* Set to true when thread finishes */
33390 void *pOut; /* Result returned by the thread */
33391 void *(*xTask)(void*); /* The thread routine */
33392 void *pIn; /* Argument to the thread */
33393};
33394
33395/* Create a new thread */
33396SQLITE_PRIVATE int sqlite3ThreadCreate(
33397 SQLiteThread **ppThread, /* OUT: Write the thread object here */
33398 void *(*xTask)(void*), /* Routine to run in a separate thread */
33399 void *pIn /* Argument passed into xTask() */
33400){
33401 SQLiteThread *p;
33402 int rc;
33403
33404 assert( ppThread!=0 );
33405 assert( xTask!=0 );
33406 /* This routine is never used in single-threaded mode */
33407 assert( sqlite3GlobalConfig.bCoreMutex!=0 );
33408
33409 *ppThread = 0;
33410 p = sqlite3Malloc(n: sizeof(*p));
33411 if( p==0 ) return SQLITE_NOMEM_BKPT;
33412 memset(s: p, c: 0, n: sizeof(*p));
33413 p->xTask = xTask;
33414 p->pIn = pIn;
33415 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
33416 ** function that returns SQLITE_ERROR when passed the argument 200, that
33417 ** forces worker threads to run sequentially and deterministically
33418 ** for testing purposes. */
33419 if( sqlite3FaultSim(200) ){
33420 rc = 1;
33421 }else{
33422 rc = pthread_create(newthread: &p->tid, attr: 0, start_routine: xTask, arg: pIn);
33423 }
33424 if( rc ){
33425 p->done = 1;
33426 p->pOut = xTask(pIn);
33427 }
33428 *ppThread = p;
33429 return SQLITE_OK;
33430}
33431
33432/* Get the results of the thread */
33433SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
33434 int rc;
33435
33436 assert( ppOut!=0 );
33437 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
33438 if( p->done ){
33439 *ppOut = p->pOut;
33440 rc = SQLITE_OK;
33441 }else{
33442 rc = pthread_join(th: p->tid, thread_return: ppOut) ? SQLITE_ERROR : SQLITE_OK;
33443 }
33444 sqlite3_free(p);
33445 return rc;
33446}
33447
33448#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
33449/******************************** End Unix Pthreads *************************/
33450
33451
33452/********************************* Win32 Threads ****************************/
33453#if SQLITE_OS_WIN_THREADS
33454
33455#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33456#include <process.h>
33457
33458/* A running thread */
33459struct SQLiteThread {
33460 void *tid; /* The thread handle */
33461 unsigned id; /* The thread identifier */
33462 void *(*xTask)(void*); /* The routine to run as a thread */
33463 void *pIn; /* Argument to xTask */
33464 void *pResult; /* Result of xTask */
33465};
33466
33467/* Thread procedure Win32 compatibility shim */
33468static unsigned __stdcall sqlite3ThreadProc(
33469 void *pArg /* IN: Pointer to the SQLiteThread structure */
33470){
33471 SQLiteThread *p = (SQLiteThread *)pArg;
33472
33473 assert( p!=0 );
33474#if 0
33475 /*
33476 ** This assert appears to trigger spuriously on certain
33477 ** versions of Windows, possibly due to _beginthreadex()
33478 ** and/or CreateThread() not fully setting their thread
33479 ** ID parameter before starting the thread.
33480 */
33481 assert( p->id==GetCurrentThreadId() );
33482#endif
33483 assert( p->xTask!=0 );
33484 p->pResult = p->xTask(p->pIn);
33485
33486 _endthreadex(0);
33487 return 0; /* NOT REACHED */
33488}
33489
33490/* Create a new thread */
33491SQLITE_PRIVATE int sqlite3ThreadCreate(
33492 SQLiteThread **ppThread, /* OUT: Write the thread object here */
33493 void *(*xTask)(void*), /* Routine to run in a separate thread */
33494 void *pIn /* Argument passed into xTask() */
33495){
33496 SQLiteThread *p;
33497
33498 assert( ppThread!=0 );
33499 assert( xTask!=0 );
33500 *ppThread = 0;
33501 p = sqlite3Malloc(sizeof(*p));
33502 if( p==0 ) return SQLITE_NOMEM_BKPT;
33503 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
33504 ** function that returns SQLITE_ERROR when passed the argument 200, that
33505 ** forces worker threads to run sequentially and deterministically
33506 ** (via the sqlite3FaultSim() term of the conditional) for testing
33507 ** purposes. */
33508 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
33509 memset(p, 0, sizeof(*p));
33510 }else{
33511 p->xTask = xTask;
33512 p->pIn = pIn;
33513 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
33514 if( p->tid==0 ){
33515 memset(p, 0, sizeof(*p));
33516 }
33517 }
33518 if( p->xTask==0 ){
33519 p->id = GetCurrentThreadId();
33520 p->pResult = xTask(pIn);
33521 }
33522 *ppThread = p;
33523 return SQLITE_OK;
33524}
33525
33526SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
33527
33528/* Get the results of the thread */
33529SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
33530 DWORD rc;
33531 BOOL bRc;
33532
33533 assert( ppOut!=0 );
33534 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
33535 if( p->xTask==0 ){
33536 /* assert( p->id==GetCurrentThreadId() ); */
33537 rc = WAIT_OBJECT_0;
33538 assert( p->tid==0 );
33539 }else{
33540 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
33541 rc = sqlite3Win32Wait((HANDLE)p->tid);
33542 assert( rc!=WAIT_IO_COMPLETION );
33543 bRc = CloseHandle((HANDLE)p->tid);
33544 assert( bRc );
33545 }
33546 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
33547 sqlite3_free(p);
33548 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
33549}
33550
33551#endif /* SQLITE_OS_WIN_THREADS */
33552/******************************** End Win32 Threads *************************/
33553
33554
33555/********************************* Single-Threaded **************************/
33556#ifndef SQLITE_THREADS_IMPLEMENTED
33557/*
33558** This implementation does not actually create a new thread. It does the
33559** work of the thread in the main thread, when either the thread is created
33560** or when it is joined
33561*/
33562
33563/* A running thread */
33564struct SQLiteThread {
33565 void *(*xTask)(void*); /* The routine to run as a thread */
33566 void *pIn; /* Argument to xTask */
33567 void *pResult; /* Result of xTask */
33568};
33569
33570/* Create a new thread */
33571SQLITE_PRIVATE int sqlite3ThreadCreate(
33572 SQLiteThread **ppThread, /* OUT: Write the thread object here */
33573 void *(*xTask)(void*), /* Routine to run in a separate thread */
33574 void *pIn /* Argument passed into xTask() */
33575){
33576 SQLiteThread *p;
33577
33578 assert( ppThread!=0 );
33579 assert( xTask!=0 );
33580 *ppThread = 0;
33581 p = sqlite3Malloc(sizeof(*p));
33582 if( p==0 ) return SQLITE_NOMEM_BKPT;
33583 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
33584 p->xTask = xTask;
33585 p->pIn = pIn;
33586 }else{
33587 p->xTask = 0;
33588 p->pResult = xTask(pIn);
33589 }
33590 *ppThread = p;
33591 return SQLITE_OK;
33592}
33593
33594/* Get the results of the thread */
33595SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
33596
33597 assert( ppOut!=0 );
33598 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
33599 if( p->xTask ){
33600 *ppOut = p->xTask(p->pIn);
33601 }else{
33602 *ppOut = p->pResult;
33603 }
33604 sqlite3_free(p);
33605
33606#if defined(SQLITE_TEST)
33607 {
33608 void *pTstAlloc = sqlite3Malloc(10);
33609 if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
33610 sqlite3_free(pTstAlloc);
33611 }
33612#endif
33613
33614 return SQLITE_OK;
33615}
33616
33617#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
33618/****************************** End Single-Threaded *************************/
33619#endif /* SQLITE_MAX_WORKER_THREADS>0 */
33620
33621/************** End of threads.c *********************************************/
33622/************** Begin file utf.c *********************************************/
33623/*
33624** 2004 April 13
33625**
33626** The author disclaims copyright to this source code. In place of
33627** a legal notice, here is a blessing:
33628**
33629** May you do good and not evil.
33630** May you find forgiveness for yourself and forgive others.
33631** May you share freely, never taking more than you give.
33632**
33633*************************************************************************
33634** This file contains routines used to translate between UTF-8,
33635** UTF-16, UTF-16BE, and UTF-16LE.
33636**
33637** Notes on UTF-8:
33638**
33639** Byte-0 Byte-1 Byte-2 Byte-3 Value
33640** 0xxxxxxx 00000000 00000000 0xxxxxxx
33641** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
33642** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
33643** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
33644**
33645**
33646** Notes on UTF-16: (with wwww+1==uuuuu)
33647**
33648** Word-0 Word-1 Value
33649** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
33650** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
33651**
33652**
33653** BOM or Byte Order Mark:
33654** 0xff 0xfe little-endian utf-16 follows
33655** 0xfe 0xff big-endian utf-16 follows
33656**
33657*/
33658/* #include "sqliteInt.h" */
33659/* #include <assert.h> */
33660/* #include "vdbeInt.h" */
33661
33662#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
33663/*
33664** The following constant value is used by the SQLITE_BIGENDIAN and
33665** SQLITE_LITTLEENDIAN macros.
33666*/
33667SQLITE_PRIVATE const int sqlite3one = 1;
33668#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
33669
33670/*
33671** This lookup table is used to help decode the first byte of
33672** a multi-byte UTF8 character.
33673*/
33674static const unsigned char sqlite3Utf8Trans1[] = {
33675 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33676 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
33677 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
33678 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
33679 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33680 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
33681 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33682 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
33683};
33684
33685
33686#define WRITE_UTF8(zOut, c) { \
33687 if( c<0x00080 ){ \
33688 *zOut++ = (u8)(c&0xFF); \
33689 } \
33690 else if( c<0x00800 ){ \
33691 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
33692 *zOut++ = 0x80 + (u8)(c & 0x3F); \
33693 } \
33694 else if( c<0x10000 ){ \
33695 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
33696 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
33697 *zOut++ = 0x80 + (u8)(c & 0x3F); \
33698 }else{ \
33699 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
33700 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
33701 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
33702 *zOut++ = 0x80 + (u8)(c & 0x3F); \
33703 } \
33704}
33705
33706#define WRITE_UTF16LE(zOut, c) { \
33707 if( c<=0xFFFF ){ \
33708 *zOut++ = (u8)(c&0x00FF); \
33709 *zOut++ = (u8)((c>>8)&0x00FF); \
33710 }else{ \
33711 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33712 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33713 *zOut++ = (u8)(c&0x00FF); \
33714 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
33715 } \
33716}
33717
33718#define WRITE_UTF16BE(zOut, c) { \
33719 if( c<=0xFFFF ){ \
33720 *zOut++ = (u8)((c>>8)&0x00FF); \
33721 *zOut++ = (u8)(c&0x00FF); \
33722 }else{ \
33723 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33724 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33725 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
33726 *zOut++ = (u8)(c&0x00FF); \
33727 } \
33728}
33729
33730/*
33731** Translate a single UTF-8 character. Return the unicode value.
33732**
33733** During translation, assume that the byte that zTerm points
33734** is a 0x00.
33735**
33736** Write a pointer to the next unread byte back into *pzNext.
33737**
33738** Notes On Invalid UTF-8:
33739**
33740** * This routine never allows a 7-bit character (0x00 through 0x7f) to
33741** be encoded as a multi-byte character. Any multi-byte character that
33742** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
33743**
33744** * This routine never allows a UTF16 surrogate value to be encoded.
33745** If a multi-byte character attempts to encode a value between
33746** 0xd800 and 0xe000 then it is rendered as 0xfffd.
33747**
33748** * Bytes in the range of 0x80 through 0xbf which occur as the first
33749** byte of a character are interpreted as single-byte characters
33750** and rendered as themselves even though they are technically
33751** invalid characters.
33752**
33753** * This routine accepts over-length UTF8 encodings
33754** for unicode values 0x80 and greater. It does not change over-length
33755** encodings to 0xfffd as some systems recommend.
33756*/
33757#define READ_UTF8(zIn, zTerm, c) \
33758 c = *(zIn++); \
33759 if( c>=0xc0 ){ \
33760 c = sqlite3Utf8Trans1[c-0xc0]; \
33761 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
33762 c = (c<<6) + (0x3f & *(zIn++)); \
33763 } \
33764 if( c<0x80 \
33765 || (c&0xFFFFF800)==0xD800 \
33766 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
33767 }
33768SQLITE_PRIVATE u32 sqlite3Utf8Read(
33769 const unsigned char **pz /* Pointer to string from which to read char */
33770){
33771 unsigned int c;
33772
33773 /* Same as READ_UTF8() above but without the zTerm parameter.
33774 ** For this routine, we assume the UTF8 string is always zero-terminated.
33775 */
33776 c = *((*pz)++);
33777 if( c>=0xc0 ){
33778 c = sqlite3Utf8Trans1[c-0xc0];
33779 while( (*(*pz) & 0xc0)==0x80 ){
33780 c = (c<<6) + (0x3f & *((*pz)++));
33781 }
33782 if( c<0x80
33783 || (c&0xFFFFF800)==0xD800
33784 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
33785 }
33786 return c;
33787}
33788
33789
33790
33791
33792/*
33793** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
33794** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
33795*/
33796/* #define TRANSLATE_TRACE 1 */
33797
33798#ifndef SQLITE_OMIT_UTF16
33799/*
33800** This routine transforms the internal text encoding used by pMem to
33801** desiredEnc. It is an error if the string is already of the desired
33802** encoding, or if *pMem does not contain a string value.
33803*/
33804SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
33805 sqlite3_int64 len; /* Maximum length of output string in bytes */
33806 unsigned char *zOut; /* Output buffer */
33807 unsigned char *zIn; /* Input iterator */
33808 unsigned char *zTerm; /* End of input */
33809 unsigned char *z; /* Output iterator */
33810 unsigned int c;
33811
33812 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
33813 assert( pMem->flags&MEM_Str );
33814 assert( pMem->enc!=desiredEnc );
33815 assert( pMem->enc!=0 );
33816 assert( pMem->n>=0 );
33817
33818#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
33819 {
33820 StrAccum acc;
33821 char zBuf[1000];
33822 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
33823 sqlite3VdbeMemPrettyPrint(pMem, &acc);
33824 fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
33825 }
33826#endif
33827
33828 /* If the translation is between UTF-16 little and big endian, then
33829 ** all that is required is to swap the byte order. This case is handled
33830 ** differently from the others.
33831 */
33832 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
33833 u8 temp;
33834 int rc;
33835 rc = sqlite3VdbeMemMakeWriteable(pMem);
33836 if( rc!=SQLITE_OK ){
33837 assert( rc==SQLITE_NOMEM );
33838 return SQLITE_NOMEM_BKPT;
33839 }
33840 zIn = (u8*)pMem->z;
33841 zTerm = &zIn[pMem->n&~1];
33842 while( zIn<zTerm ){
33843 temp = *zIn;
33844 *zIn = *(zIn+1);
33845 zIn++;
33846 *zIn++ = temp;
33847 }
33848 pMem->enc = desiredEnc;
33849 goto translate_out;
33850 }
33851
33852 /* Set len to the maximum number of bytes required in the output buffer. */
33853 if( desiredEnc==SQLITE_UTF8 ){
33854 /* When converting from UTF-16, the maximum growth results from
33855 ** translating a 2-byte character to a 4-byte UTF-8 character.
33856 ** A single byte is required for the output string
33857 ** nul-terminator.
33858 */
33859 pMem->n &= ~1;
33860 len = 2 * (sqlite3_int64)pMem->n + 1;
33861 }else{
33862 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
33863 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
33864 ** character. Two bytes are required in the output buffer for the
33865 ** nul-terminator.
33866 */
33867 len = 2 * (sqlite3_int64)pMem->n + 2;
33868 }
33869
33870 /* Set zIn to point at the start of the input buffer and zTerm to point 1
33871 ** byte past the end.
33872 **
33873 ** Variable zOut is set to point at the output buffer, space obtained
33874 ** from sqlite3_malloc().
33875 */
33876 zIn = (u8*)pMem->z;
33877 zTerm = &zIn[pMem->n];
33878 zOut = sqlite3DbMallocRaw(db: pMem->db, n: len);
33879 if( !zOut ){
33880 return SQLITE_NOMEM_BKPT;
33881 }
33882 z = zOut;
33883
33884 if( pMem->enc==SQLITE_UTF8 ){
33885 if( desiredEnc==SQLITE_UTF16LE ){
33886 /* UTF-8 -> UTF-16 Little-endian */
33887 while( zIn<zTerm ){
33888 READ_UTF8(zIn, zTerm, c);
33889 WRITE_UTF16LE(z, c);
33890 }
33891 }else{
33892 assert( desiredEnc==SQLITE_UTF16BE );
33893 /* UTF-8 -> UTF-16 Big-endian */
33894 while( zIn<zTerm ){
33895 READ_UTF8(zIn, zTerm, c);
33896 WRITE_UTF16BE(z, c);
33897 }
33898 }
33899 pMem->n = (int)(z - zOut);
33900 *z++ = 0;
33901 }else{
33902 assert( desiredEnc==SQLITE_UTF8 );
33903 if( pMem->enc==SQLITE_UTF16LE ){
33904 /* UTF-16 Little-endian -> UTF-8 */
33905 while( zIn<zTerm ){
33906 c = *(zIn++);
33907 c += (*(zIn++))<<8;
33908 if( c>=0xd800 && c<0xe000 ){
33909#ifdef SQLITE_REPLACE_INVALID_UTF
33910 if( c>=0xdc00 || zIn>=zTerm ){
33911 c = 0xfffd;
33912 }else{
33913 int c2 = *(zIn++);
33914 c2 += (*(zIn++))<<8;
33915 if( c2<0xdc00 || c2>=0xe000 ){
33916 zIn -= 2;
33917 c = 0xfffd;
33918 }else{
33919 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
33920 }
33921 }
33922#else
33923 if( zIn<zTerm ){
33924 int c2 = (*zIn++);
33925 c2 += ((*zIn++)<<8);
33926 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
33927 }
33928#endif
33929 }
33930 WRITE_UTF8(z, c);
33931 }
33932 }else{
33933 /* UTF-16 Big-endian -> UTF-8 */
33934 while( zIn<zTerm ){
33935 c = (*(zIn++))<<8;
33936 c += *(zIn++);
33937 if( c>=0xd800 && c<0xe000 ){
33938#ifdef SQLITE_REPLACE_INVALID_UTF
33939 if( c>=0xdc00 || zIn>=zTerm ){
33940 c = 0xfffd;
33941 }else{
33942 int c2 = (*(zIn++))<<8;
33943 c2 += *(zIn++);
33944 if( c2<0xdc00 || c2>=0xe000 ){
33945 zIn -= 2;
33946 c = 0xfffd;
33947 }else{
33948 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
33949 }
33950 }
33951#else
33952 if( zIn<zTerm ){
33953 int c2 = ((*zIn++)<<8);
33954 c2 += (*zIn++);
33955 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
33956 }
33957#endif
33958 }
33959 WRITE_UTF8(z, c);
33960 }
33961 }
33962 pMem->n = (int)(z - zOut);
33963 }
33964 *z = 0;
33965 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
33966
33967 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
33968 sqlite3VdbeMemRelease(p: pMem);
33969 pMem->flags = c;
33970 pMem->enc = desiredEnc;
33971 pMem->z = (char*)zOut;
33972 pMem->zMalloc = pMem->z;
33973 pMem->szMalloc = sqlite3DbMallocSize(db: pMem->db, p: pMem->z);
33974
33975translate_out:
33976#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
33977 {
33978 StrAccum acc;
33979 char zBuf[1000];
33980 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
33981 sqlite3VdbeMemPrettyPrint(pMem, &acc);
33982 fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
33983 }
33984#endif
33985 return SQLITE_OK;
33986}
33987#endif /* SQLITE_OMIT_UTF16 */
33988
33989#ifndef SQLITE_OMIT_UTF16
33990/*
33991** This routine checks for a byte-order mark at the beginning of the
33992** UTF-16 string stored in *pMem. If one is present, it is removed and
33993** the encoding of the Mem adjusted. This routine does not do any
33994** byte-swapping, it just sets Mem.enc appropriately.
33995**
33996** The allocation (static, dynamic etc.) and encoding of the Mem may be
33997** changed by this function.
33998*/
33999SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
34000 int rc = SQLITE_OK;
34001 u8 bom = 0;
34002
34003 assert( pMem->n>=0 );
34004 if( pMem->n>1 ){
34005 u8 b1 = *(u8 *)pMem->z;
34006 u8 b2 = *(((u8 *)pMem->z) + 1);
34007 if( b1==0xFE && b2==0xFF ){
34008 bom = SQLITE_UTF16BE;
34009 }
34010 if( b1==0xFF && b2==0xFE ){
34011 bom = SQLITE_UTF16LE;
34012 }
34013 }
34014
34015 if( bom ){
34016 rc = sqlite3VdbeMemMakeWriteable(pMem);
34017 if( rc==SQLITE_OK ){
34018 pMem->n -= 2;
34019 memmove(dest: pMem->z, src: &pMem->z[2], n: pMem->n);
34020 pMem->z[pMem->n] = '\0';
34021 pMem->z[pMem->n+1] = '\0';
34022 pMem->flags |= MEM_Term;
34023 pMem->enc = bom;
34024 }
34025 }
34026 return rc;
34027}
34028#endif /* SQLITE_OMIT_UTF16 */
34029
34030/*
34031** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
34032** return the number of unicode characters in pZ up to (but not including)
34033** the first 0x00 byte. If nByte is not less than zero, return the
34034** number of unicode characters in the first nByte of pZ (or up to
34035** the first 0x00, whichever comes first).
34036*/
34037SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
34038 int r = 0;
34039 const u8 *z = (const u8*)zIn;
34040 const u8 *zTerm;
34041 if( nByte>=0 ){
34042 zTerm = &z[nByte];
34043 }else{
34044 zTerm = (const u8*)(-1);
34045 }
34046 assert( z<=zTerm );
34047 while( *z!=0 && z<zTerm ){
34048 SQLITE_SKIP_UTF8(z);
34049 r++;
34050 }
34051 return r;
34052}
34053
34054/* This test function is not currently used by the automated test-suite.
34055** Hence it is only available in debug builds.
34056*/
34057#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
34058/*
34059** Translate UTF-8 to UTF-8.
34060**
34061** This has the effect of making sure that the string is well-formed
34062** UTF-8. Miscoded characters are removed.
34063**
34064** The translation is done in-place and aborted if the output
34065** overruns the input.
34066*/
34067SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
34068 unsigned char *zOut = zIn;
34069 unsigned char *zStart = zIn;
34070 u32 c;
34071
34072 while( zIn[0] && zOut<=zIn ){
34073 c = sqlite3Utf8Read((const u8**)&zIn);
34074 if( c!=0xfffd ){
34075 WRITE_UTF8(zOut, c);
34076 }
34077 }
34078 *zOut = 0;
34079 return (int)(zOut - zStart);
34080}
34081#endif
34082
34083#ifndef SQLITE_OMIT_UTF16
34084/*
34085** Convert a UTF-16 string in the native encoding into a UTF-8 string.
34086** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
34087** be freed by the calling function.
34088**
34089** NULL is returned if there is an allocation error.
34090*/
34091SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
34092 Mem m;
34093 memset(s: &m, c: 0, n: sizeof(m));
34094 m.db = db;
34095 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
34096 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
34097 if( db->mallocFailed ){
34098 sqlite3VdbeMemRelease(p: &m);
34099 m.z = 0;
34100 }
34101 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
34102 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
34103 assert( m.z || db->mallocFailed );
34104 return m.z;
34105}
34106
34107/*
34108** zIn is a UTF-16 encoded unicode string at least nChar characters long.
34109** Return the number of bytes in the first nChar unicode characters
34110** in pZ. nChar must be non-negative.
34111*/
34112SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
34113 int c;
34114 unsigned char const *z = zIn;
34115 int n = 0;
34116
34117 if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
34118 while( n<nChar ){
34119 c = z[0];
34120 z += 2;
34121 if( c>=0xd8 && c<0xdc && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
34122 n++;
34123 }
34124 return (int)(z-(unsigned char const *)zIn)
34125 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
34126}
34127
34128#if defined(SQLITE_TEST)
34129/*
34130** This routine is called from the TCL test function "translate_selftest".
34131** It checks that the primitives for serializing and deserializing
34132** characters in each encoding are inverses of each other.
34133*/
34134SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
34135 unsigned int i, t;
34136 unsigned char zBuf[20];
34137 unsigned char *z;
34138 int n;
34139 unsigned int c;
34140
34141 for(i=0; i<0x00110000; i++){
34142 z = zBuf;
34143 WRITE_UTF8(z, i);
34144 n = (int)(z-zBuf);
34145 assert( n>0 && n<=4 );
34146 z[0] = 0;
34147 z = zBuf;
34148 c = sqlite3Utf8Read((const u8**)&z);
34149 t = i;
34150 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
34151 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
34152 assert( c==t );
34153 assert( (z-zBuf)==n );
34154 }
34155}
34156#endif /* SQLITE_TEST */
34157#endif /* SQLITE_OMIT_UTF16 */
34158
34159/************** End of utf.c *************************************************/
34160/************** Begin file util.c ********************************************/
34161/*
34162** 2001 September 15
34163**
34164** The author disclaims copyright to this source code. In place of
34165** a legal notice, here is a blessing:
34166**
34167** May you do good and not evil.
34168** May you find forgiveness for yourself and forgive others.
34169** May you share freely, never taking more than you give.
34170**
34171*************************************************************************
34172** Utility functions used throughout sqlite.
34173**
34174** This file contains functions for allocating memory, comparing
34175** strings, and stuff like that.
34176**
34177*/
34178/* #include "sqliteInt.h" */
34179/* #include <stdarg.h> */
34180#ifndef SQLITE_OMIT_FLOATING_POINT
34181#include <math.h>
34182#endif
34183
34184/*
34185** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
34186** or to bypass normal error detection during testing in order to let
34187** execute proceed further downstream.
34188**
34189** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
34190** sqlite3FaultSim() function only returns non-zero during testing.
34191**
34192** During testing, if the test harness has set a fault-sim callback using
34193** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
34194** each call to sqlite3FaultSim() is relayed to that application-supplied
34195** callback and the integer return value form the application-supplied
34196** callback is returned by sqlite3FaultSim().
34197**
34198** The integer argument to sqlite3FaultSim() is a code to identify which
34199** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
34200** should have a unique code. To prevent legacy testing applications from
34201** breaking, the codes should not be changed or reused.
34202*/
34203#ifndef SQLITE_UNTESTABLE
34204SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
34205 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
34206 return xCallback ? xCallback(iTest) : SQLITE_OK;
34207}
34208#endif
34209
34210#ifndef SQLITE_OMIT_FLOATING_POINT
34211/*
34212** Return true if the floating point value is Not a Number (NaN).
34213**
34214** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
34215** Otherwise, we have our own implementation that works on most systems.
34216*/
34217SQLITE_PRIVATE int sqlite3IsNaN(double x){
34218 int rc; /* The value return */
34219#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
34220 u64 y;
34221 memcpy(dest: &y,src: &x,n: sizeof(y));
34222 rc = IsNaN(y);
34223#else
34224 rc = isnan(x);
34225#endif /* HAVE_ISNAN */
34226 testcase( rc );
34227 return rc;
34228}
34229#endif /* SQLITE_OMIT_FLOATING_POINT */
34230
34231/*
34232** Compute a string length that is limited to what can be stored in
34233** lower 30 bits of a 32-bit signed integer.
34234**
34235** The value returned will never be negative. Nor will it ever be greater
34236** than the actual length of the string. For very long strings (greater
34237** than 1GiB) the value returned might be less than the true string length.
34238*/
34239SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
34240 if( z==0 ) return 0;
34241 return 0x3fffffff & (int)strlen(s: z);
34242}
34243
34244/*
34245** Return the declared type of a column. Or return zDflt if the column
34246** has no declared type.
34247**
34248** The column type is an extra string stored after the zero-terminator on
34249** the column name if and only if the COLFLAG_HASTYPE flag is set.
34250*/
34251SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
34252 if( pCol->colFlags & COLFLAG_HASTYPE ){
34253 return pCol->zCnName + strlen(s: pCol->zCnName) + 1;
34254 }else if( pCol->eCType ){
34255 assert( pCol->eCType<=SQLITE_N_STDTYPE );
34256 return (char*)sqlite3StdType[pCol->eCType-1];
34257 }else{
34258 return zDflt;
34259 }
34260}
34261
34262/*
34263** Helper function for sqlite3Error() - called rarely. Broken out into
34264** a separate routine to avoid unnecessary register saves on entry to
34265** sqlite3Error().
34266*/
34267static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
34268 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
34269 sqlite3SystemError(db, err_code);
34270}
34271
34272/*
34273** Set the current error code to err_code and clear any prior error message.
34274** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
34275** that would be appropriate.
34276*/
34277SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
34278 assert( db!=0 );
34279 db->errCode = err_code;
34280 if( err_code || db->pErr ){
34281 sqlite3ErrorFinish(db, err_code);
34282 }else{
34283 db->errByteOffset = -1;
34284 }
34285}
34286
34287/*
34288** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
34289** and error message.
34290*/
34291SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){
34292 assert( db!=0 );
34293 db->errCode = SQLITE_OK;
34294 db->errByteOffset = -1;
34295 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
34296}
34297
34298/*
34299** Load the sqlite3.iSysErrno field if that is an appropriate thing
34300** to do based on the SQLite error code in rc.
34301*/
34302SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
34303 if( rc==SQLITE_IOERR_NOMEM ) return;
34304#ifdef SQLITE_USE_SEH
34305 if( rc==SQLITE_IOERR_IN_PAGE ){
34306 int ii;
34307 int iErr;
34308 sqlite3BtreeEnterAll(db);
34309 for(ii=0; ii<db->nDb; ii++){
34310 if( db->aDb[ii].pBt ){
34311 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt));
34312 if( iErr ){
34313 db->iSysErrno = iErr;
34314 }
34315 }
34316 }
34317 sqlite3BtreeLeaveAll(db);
34318 return;
34319 }
34320#endif
34321 rc &= 0xff;
34322 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
34323 db->iSysErrno = sqlite3OsGetLastError(pVfs: db->pVfs);
34324 }
34325}
34326
34327/*
34328** Set the most recent error code and error string for the sqlite
34329** handle "db". The error code is set to "err_code".
34330**
34331** If it is not NULL, string zFormat specifies the format of the
34332** error string. zFormat and any string tokens that follow it are
34333** assumed to be encoded in UTF-8.
34334**
34335** To clear the most recent error for sqlite handle "db", sqlite3Error
34336** should be called with err_code set to SQLITE_OK and zFormat set
34337** to NULL.
34338*/
34339SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
34340 assert( db!=0 );
34341 db->errCode = err_code;
34342 sqlite3SystemError(db, rc: err_code);
34343 if( zFormat==0 ){
34344 sqlite3Error(db, err_code);
34345 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
34346 char *z;
34347 va_list ap;
34348 va_start(ap, zFormat);
34349 z = sqlite3VMPrintf(db, zFormat, ap);
34350 va_end(ap);
34351 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
34352 }
34353}
34354
34355/*
34356** Check for interrupts and invoke progress callback.
34357*/
34358SQLITE_PRIVATE void sqlite3ProgressCheck(Parse *p){
34359 sqlite3 *db = p->db;
34360 if( AtomicLoad(&db->u1.isInterrupted) ){
34361 p->nErr++;
34362 p->rc = SQLITE_INTERRUPT;
34363 }
34364#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
34365 if( db->xProgress && (++p->nProgressSteps)>=db->nProgressOps ){
34366 if( db->xProgress(db->pProgressArg) ){
34367 p->nErr++;
34368 p->rc = SQLITE_INTERRUPT;
34369 }
34370 p->nProgressSteps = 0;
34371 }
34372#endif
34373}
34374
34375/*
34376** Add an error message to pParse->zErrMsg and increment pParse->nErr.
34377**
34378** This function should be used to report any error that occurs while
34379** compiling an SQL statement (i.e. within sqlite3_prepare()). The
34380** last thing the sqlite3_prepare() function does is copy the error
34381** stored by this function into the database handle using sqlite3Error().
34382** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
34383** during statement execution (sqlite3_step() etc.).
34384*/
34385SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
34386 char *zMsg;
34387 va_list ap;
34388 sqlite3 *db = pParse->db;
34389 assert( db!=0 );
34390 assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
34391 db->errByteOffset = -2;
34392 va_start(ap, zFormat);
34393 zMsg = sqlite3VMPrintf(db, zFormat, ap);
34394 va_end(ap);
34395 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
34396 if( db->suppressErr ){
34397 sqlite3DbFree(db, p: zMsg);
34398 if( db->mallocFailed ){
34399 pParse->nErr++;
34400 pParse->rc = SQLITE_NOMEM;
34401 }
34402 }else{
34403 pParse->nErr++;
34404 sqlite3DbFree(db, p: pParse->zErrMsg);
34405 pParse->zErrMsg = zMsg;
34406 pParse->rc = SQLITE_ERROR;
34407 pParse->pWith = 0;
34408 }
34409}
34410
34411/*
34412** If database connection db is currently parsing SQL, then transfer
34413** error code errCode to that parser if the parser has not already
34414** encountered some other kind of error.
34415*/
34416SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
34417 Parse *pParse;
34418 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
34419 pParse->rc = errCode;
34420 pParse->nErr++;
34421 return errCode;
34422}
34423
34424/*
34425** Convert an SQL-style quoted string into a normal string by removing
34426** the quote characters. The conversion is done in-place. If the
34427** input does not begin with a quote character, then this routine
34428** is a no-op.
34429**
34430** The input string must be zero-terminated. A new zero-terminator
34431** is added to the dequoted string.
34432**
34433** The return value is -1 if no dequoting occurs or the length of the
34434** dequoted string, exclusive of the zero terminator, if dequoting does
34435** occur.
34436**
34437** 2002-02-14: This routine is extended to remove MS-Access style
34438** brackets from around identifiers. For example: "[a-b-c]" becomes
34439** "a-b-c".
34440*/
34441SQLITE_PRIVATE void sqlite3Dequote(char *z){
34442 char quote;
34443 int i, j;
34444 if( z==0 ) return;
34445 quote = z[0];
34446 if( !sqlite3Isquote(quote) ) return;
34447 if( quote=='[' ) quote = ']';
34448 for(i=1, j=0;; i++){
34449 assert( z[i] );
34450 if( z[i]==quote ){
34451 if( z[i+1]==quote ){
34452 z[j++] = quote;
34453 i++;
34454 }else{
34455 break;
34456 }
34457 }else{
34458 z[j++] = z[i];
34459 }
34460 }
34461 z[j] = 0;
34462}
34463SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
34464 assert( !ExprHasProperty(p, EP_IntValue) );
34465 assert( sqlite3Isquote(p->u.zToken[0]) );
34466 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
34467 sqlite3Dequote(z: p->u.zToken);
34468}
34469
34470/*
34471** If the input token p is quoted, try to adjust the token to remove
34472** the quotes. This is not always possible:
34473**
34474** "abc" -> abc
34475** "ab""cd" -> (not possible because of the interior "")
34476**
34477** Remove the quotes if possible. This is a optimization. The overall
34478** system should still return the correct answer even if this routine
34479** is always a no-op.
34480*/
34481SQLITE_PRIVATE void sqlite3DequoteToken(Token *p){
34482 unsigned int i;
34483 if( p->n<2 ) return;
34484 if( !sqlite3Isquote(p->z[0]) ) return;
34485 for(i=1; i<p->n-1; i++){
34486 if( sqlite3Isquote(p->z[i]) ) return;
34487 }
34488 p->n -= 2;
34489 p->z++;
34490}
34491
34492/*
34493** Generate a Token object from a string
34494*/
34495SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
34496 p->z = z;
34497 p->n = sqlite3Strlen30(z);
34498}
34499
34500/* Convenient short-hand */
34501#define UpperToLower sqlite3UpperToLower
34502
34503/*
34504** Some systems have stricmp(). Others have strcasecmp(). Because
34505** there is no consistency, we will define our own.
34506**
34507** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
34508** sqlite3_strnicmp() APIs allow applications and extensions to compare
34509** the contents of two buffers containing UTF-8 strings in a
34510** case-independent fashion, using the same definition of "case
34511** independence" that SQLite uses internally when comparing identifiers.
34512*/
34513SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
34514 if( zLeft==0 ){
34515 return zRight ? -1 : 0;
34516 }else if( zRight==0 ){
34517 return 1;
34518 }
34519 return sqlite3StrICmp(zLeft, zRight);
34520}
34521SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
34522 unsigned char *a, *b;
34523 int c, x;
34524 a = (unsigned char *)zLeft;
34525 b = (unsigned char *)zRight;
34526 for(;;){
34527 c = *a;
34528 x = *b;
34529 if( c==x ){
34530 if( c==0 ) break;
34531 }else{
34532 c = (int)UpperToLower[c] - (int)UpperToLower[x];
34533 if( c ) break;
34534 }
34535 a++;
34536 b++;
34537 }
34538 return c;
34539}
34540SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
34541 register unsigned char *a, *b;
34542 if( zLeft==0 ){
34543 return zRight ? -1 : 0;
34544 }else if( zRight==0 ){
34545 return 1;
34546 }
34547 a = (unsigned char *)zLeft;
34548 b = (unsigned char *)zRight;
34549 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
34550 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
34551}
34552
34553/*
34554** Compute an 8-bit hash on a string that is insensitive to case differences
34555*/
34556SQLITE_PRIVATE u8 sqlite3StrIHash(const char *z){
34557 u8 h = 0;
34558 if( z==0 ) return 0;
34559 while( z[0] ){
34560 h += UpperToLower[(unsigned char)z[0]];
34561 z++;
34562 }
34563 return h;
34564}
34565
34566/* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
34567**
34568** Reference:
34569** T. J. Dekker, "A Floating-Point Technique for Extending the
34570** Available Precision". 1971-07-26.
34571*/
34572static void dekkerMul2(volatile double *x, double y, double yy){
34573 /*
34574 ** The "volatile" keywords on parameter x[] and on local variables
34575 ** below are needed force intermediate results to be truncated to
34576 ** binary64 rather than be carried around in an extended-precision
34577 ** format. The truncation is necessary for the Dekker algorithm to
34578 ** work. Intel x86 floating point might omit the truncation without
34579 ** the use of volatile.
34580 */
34581 volatile double tx, ty, p, q, c, cc;
34582 double hx, hy;
34583 u64 m;
34584 memcpy(dest: &m, src: (void*)&x[0], n: 8);
34585 m &= 0xfffffffffc000000LL;
34586 memcpy(dest: &hx, src: &m, n: 8);
34587 tx = x[0] - hx;
34588 memcpy(dest: &m, src: &y, n: 8);
34589 m &= 0xfffffffffc000000LL;
34590 memcpy(dest: &hy, src: &m, n: 8);
34591 ty = y - hy;
34592 p = hx*hy;
34593 q = hx*ty + tx*hy;
34594 c = p+q;
34595 cc = p - c + q + tx*ty;
34596 cc = x[0]*yy + x[1]*y + cc;
34597 x[0] = c + cc;
34598 x[1] = c - x[0];
34599 x[1] += cc;
34600}
34601
34602/*
34603** The string z[] is an text representation of a real number.
34604** Convert this string to a double and write it into *pResult.
34605**
34606** The string z[] is length bytes in length (bytes, not characters) and
34607** uses the encoding enc. The string is not necessarily zero-terminated.
34608**
34609** Return TRUE if the result is a valid real number (or integer) and FALSE
34610** if the string is empty or contains extraneous text. More specifically
34611** return
34612** 1 => The input string is a pure integer
34613** 2 or more => The input has a decimal point or eNNN clause
34614** 0 or less => The input string is not a valid number
34615** -1 => Not a valid number, but has a valid prefix which
34616** includes a decimal point and/or an eNNN clause
34617**
34618** Valid numbers are in one of these formats:
34619**
34620** [+-]digits[E[+-]digits]
34621** [+-]digits.[digits][E[+-]digits]
34622** [+-].digits[E[+-]digits]
34623**
34624** Leading and trailing whitespace is ignored for the purpose of determining
34625** validity.
34626**
34627** If some prefix of the input string is a valid number, this routine
34628** returns FALSE but it still converts the prefix and writes the result
34629** into *pResult.
34630*/
34631#if defined(_MSC_VER)
34632#pragma warning(disable : 4756)
34633#endif
34634SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
34635#ifndef SQLITE_OMIT_FLOATING_POINT
34636 int incr;
34637 const char *zEnd;
34638 /* sign * significand * (10 ^ (esign * exponent)) */
34639 int sign = 1; /* sign of significand */
34640 u64 s = 0; /* significand */
34641 int d = 0; /* adjust exponent for shifting decimal point */
34642 int esign = 1; /* sign of exponent */
34643 int e = 0; /* exponent */
34644 int eValid = 1; /* True exponent is either not used or is well-formed */
34645 int nDigit = 0; /* Number of digits processed */
34646 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
34647
34648 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
34649 *pResult = 0.0; /* Default return value, in case of an error */
34650 if( length==0 ) return 0;
34651
34652 if( enc==SQLITE_UTF8 ){
34653 incr = 1;
34654 zEnd = z + length;
34655 }else{
34656 int i;
34657 incr = 2;
34658 length &= ~1;
34659 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
34660 testcase( enc==SQLITE_UTF16LE );
34661 testcase( enc==SQLITE_UTF16BE );
34662 for(i=3-enc; i<length && z[i]==0; i+=2){}
34663 if( i<length ) eType = -100;
34664 zEnd = &z[i^1];
34665 z += (enc&1);
34666 }
34667
34668 /* skip leading spaces */
34669 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
34670 if( z>=zEnd ) return 0;
34671
34672 /* get sign of significand */
34673 if( *z=='-' ){
34674 sign = -1;
34675 z+=incr;
34676 }else if( *z=='+' ){
34677 z+=incr;
34678 }
34679
34680 /* copy max significant digits to significand */
34681 while( z<zEnd && sqlite3Isdigit(*z) ){
34682 s = s*10 + (*z - '0');
34683 z+=incr; nDigit++;
34684 if( s>=((LARGEST_UINT64-9)/10) ){
34685 /* skip non-significant significand digits
34686 ** (increase exponent by d to shift decimal left) */
34687 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
34688 }
34689 }
34690 if( z>=zEnd ) goto do_atof_calc;
34691
34692 /* if decimal point is present */
34693 if( *z=='.' ){
34694 z+=incr;
34695 eType++;
34696 /* copy digits from after decimal to significand
34697 ** (decrease exponent by d to shift decimal right) */
34698 while( z<zEnd && sqlite3Isdigit(*z) ){
34699 if( s<((LARGEST_UINT64-9)/10) ){
34700 s = s*10 + (*z - '0');
34701 d--;
34702 nDigit++;
34703 }
34704 z+=incr;
34705 }
34706 }
34707 if( z>=zEnd ) goto do_atof_calc;
34708
34709 /* if exponent is present */
34710 if( *z=='e' || *z=='E' ){
34711 z+=incr;
34712 eValid = 0;
34713 eType++;
34714
34715 /* This branch is needed to avoid a (harmless) buffer overread. The
34716 ** special comment alerts the mutation tester that the correct answer
34717 ** is obtained even if the branch is omitted */
34718 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
34719
34720 /* get sign of exponent */
34721 if( *z=='-' ){
34722 esign = -1;
34723 z+=incr;
34724 }else if( *z=='+' ){
34725 z+=incr;
34726 }
34727 /* copy digits to exponent */
34728 while( z<zEnd && sqlite3Isdigit(*z) ){
34729 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
34730 z+=incr;
34731 eValid = 1;
34732 }
34733 }
34734
34735 /* skip trailing spaces */
34736 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
34737
34738do_atof_calc:
34739 /* Zero is a special case */
34740 if( s==0 ){
34741 *pResult = sign<0 ? -0.0 : +0.0;
34742 goto atof_return;
34743 }
34744
34745 /* adjust exponent by d, and update sign */
34746 e = (e*esign) + d;
34747
34748 /* Try to adjust the exponent to make it smaller */
34749 while( e>0 && s<(LARGEST_UINT64/10) ){
34750 s *= 10;
34751 e--;
34752 }
34753 while( e<0 && (s%10)==0 ){
34754 s /= 10;
34755 e++;
34756 }
34757
34758 if( e==0 ){
34759 *pResult = s;
34760 }else if( sqlite3Config.bUseLongDouble ){
34761 LONGDOUBLE_TYPE r = (LONGDOUBLE_TYPE)s;
34762 if( e>0 ){
34763 while( e>=100 ){ e-=100; r *= 1.0e+100L; }
34764 while( e>=10 ){ e-=10; r *= 1.0e+10L; }
34765 while( e>=1 ){ e-=1; r *= 1.0e+01L; }
34766 }else{
34767 while( e<=-100 ){ e+=100; r *= 1.0e-100L; }
34768 while( e<=-10 ){ e+=10; r *= 1.0e-10L; }
34769 while( e<=-1 ){ e+=1; r *= 1.0e-01L; }
34770 }
34771 assert( r>=0.0 );
34772 if( r>+1.7976931348623157081452742373e+308L ){
34773#ifdef INFINITY
34774 *pResult = +INFINITY;
34775#else
34776 *pResult = 1.0e308*10.0;
34777#endif
34778 }else{
34779 *pResult = (double)r;
34780 }
34781 }else{
34782 double rr[2];
34783 u64 s2;
34784 rr[0] = (double)s;
34785 s2 = (u64)rr[0];
34786 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
34787 if( e>0 ){
34788 while( e>=100 ){
34789 e -= 100;
34790 dekkerMul2(x: rr, y: 1.0e+100, yy: -1.5902891109759918046e+83);
34791 }
34792 while( e>=10 ){
34793 e -= 10;
34794 dekkerMul2(x: rr, y: 1.0e+10, yy: 0.0);
34795 }
34796 while( e>=1 ){
34797 e -= 1;
34798 dekkerMul2(x: rr, y: 1.0e+01, yy: 0.0);
34799 }
34800 }else{
34801 while( e<=-100 ){
34802 e += 100;
34803 dekkerMul2(x: rr, y: 1.0e-100, yy: -1.99918998026028836196e-117);
34804 }
34805 while( e<=-10 ){
34806 e += 10;
34807 dekkerMul2(x: rr, y: 1.0e-10, yy: -3.6432197315497741579e-27);
34808 }
34809 while( e<=-1 ){
34810 e += 1;
34811 dekkerMul2(x: rr, y: 1.0e-01, yy: -5.5511151231257827021e-18);
34812 }
34813 }
34814 *pResult = rr[0]+rr[1];
34815 if( sqlite3IsNaN(x: *pResult) ) *pResult = 1e300*1e300;
34816 }
34817 if( sign<0 ) *pResult = -*pResult;
34818 assert( !sqlite3IsNaN(*pResult) );
34819
34820atof_return:
34821 /* return true if number and no extra non-whitespace characters after */
34822 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
34823 return eType;
34824 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
34825 return -1;
34826 }else{
34827 return 0;
34828 }
34829#else
34830 return !sqlite3Atoi64(z, pResult, length, enc);
34831#endif /* SQLITE_OMIT_FLOATING_POINT */
34832}
34833#if defined(_MSC_VER)
34834#pragma warning(default : 4756)
34835#endif
34836
34837/*
34838** Render an signed 64-bit integer as text. Store the result in zOut[] and
34839** return the length of the string that was stored, in bytes. The value
34840** returned does not include the zero terminator at the end of the output
34841** string.
34842**
34843** The caller must ensure that zOut[] is at least 21 bytes in size.
34844*/
34845SQLITE_PRIVATE int sqlite3Int64ToText(i64 v, char *zOut){
34846 int i;
34847 u64 x;
34848 char zTemp[22];
34849 if( v<0 ){
34850 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
34851 }else{
34852 x = v;
34853 }
34854 i = sizeof(zTemp)-2;
34855 zTemp[sizeof(zTemp)-1] = 0;
34856 while( 1 /*exit-by-break*/ ){
34857 zTemp[i] = (x%10) + '0';
34858 x = x/10;
34859 if( x==0 ) break;
34860 i--;
34861 };
34862 if( v<0 ) zTemp[--i] = '-';
34863 memcpy(dest: zOut, src: &zTemp[i], n: sizeof(zTemp)-i);
34864 return sizeof(zTemp)-1-i;
34865}
34866
34867/*
34868** Compare the 19-character string zNum against the text representation
34869** value 2^63: 9223372036854775808. Return negative, zero, or positive
34870** if zNum is less than, equal to, or greater than the string.
34871** Note that zNum must contain exactly 19 characters.
34872**
34873** Unlike memcmp() this routine is guaranteed to return the difference
34874** in the values of the last digit if the only difference is in the
34875** last digit. So, for example,
34876**
34877** compare2pow63("9223372036854775800", 1)
34878**
34879** will return -8.
34880*/
34881static int compare2pow63(const char *zNum, int incr){
34882 int c = 0;
34883 int i;
34884 /* 012345678901234567 */
34885 const char *pow63 = "922337203685477580";
34886 for(i=0; c==0 && i<18; i++){
34887 c = (zNum[i*incr]-pow63[i])*10;
34888 }
34889 if( c==0 ){
34890 c = zNum[18*incr] - '8';
34891 testcase( c==(-1) );
34892 testcase( c==0 );
34893 testcase( c==(+1) );
34894 }
34895 return c;
34896}
34897
34898/*
34899** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
34900** routine does *not* accept hexadecimal notation.
34901**
34902** Returns:
34903**
34904** -1 Not even a prefix of the input text looks like an integer
34905** 0 Successful transformation. Fits in a 64-bit signed integer.
34906** 1 Excess non-space text after the integer value
34907** 2 Integer too large for a 64-bit signed integer or is malformed
34908** 3 Special case of 9223372036854775808
34909**
34910** length is the number of bytes in the string (bytes, not characters).
34911** The string is not necessarily zero-terminated. The encoding is
34912** given by enc.
34913*/
34914SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
34915 int incr;
34916 u64 u = 0;
34917 int neg = 0; /* assume positive */
34918 int i;
34919 int c = 0;
34920 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
34921 int rc; /* Baseline return code */
34922 const char *zStart;
34923 const char *zEnd = zNum + length;
34924 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
34925 if( enc==SQLITE_UTF8 ){
34926 incr = 1;
34927 }else{
34928 incr = 2;
34929 length &= ~1;
34930 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
34931 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
34932 nonNum = i<length;
34933 zEnd = &zNum[i^1];
34934 zNum += (enc&1);
34935 }
34936 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
34937 if( zNum<zEnd ){
34938 if( *zNum=='-' ){
34939 neg = 1;
34940 zNum+=incr;
34941 }else if( *zNum=='+' ){
34942 zNum+=incr;
34943 }
34944 }
34945 zStart = zNum;
34946 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
34947 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
34948 u = u*10 + c - '0';
34949 }
34950 testcase( i==18*incr );
34951 testcase( i==19*incr );
34952 testcase( i==20*incr );
34953 if( u>LARGEST_INT64 ){
34954 /* This test and assignment is needed only to suppress UB warnings
34955 ** from clang and -fsanitize=undefined. This test and assignment make
34956 ** the code a little larger and slower, and no harm comes from omitting
34957 ** them, but we must appease the undefined-behavior pharisees. */
34958 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
34959 }else if( neg ){
34960 *pNum = -(i64)u;
34961 }else{
34962 *pNum = (i64)u;
34963 }
34964 rc = 0;
34965 if( i==0 && zStart==zNum ){ /* No digits */
34966 rc = -1;
34967 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
34968 rc = 1;
34969 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
34970 int jj = i;
34971 do{
34972 if( !sqlite3Isspace(zNum[jj]) ){
34973 rc = 1; /* Extra non-space text after the integer */
34974 break;
34975 }
34976 jj += incr;
34977 }while( &zNum[jj]<zEnd );
34978 }
34979 if( i<19*incr ){
34980 /* Less than 19 digits, so we know that it fits in 64 bits */
34981 assert( u<=LARGEST_INT64 );
34982 return rc;
34983 }else{
34984 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
34985 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
34986 if( c<0 ){
34987 /* zNum is less than 9223372036854775808 so it fits */
34988 assert( u<=LARGEST_INT64 );
34989 return rc;
34990 }else{
34991 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
34992 if( c>0 ){
34993 /* zNum is greater than 9223372036854775808 so it overflows */
34994 return 2;
34995 }else{
34996 /* zNum is exactly 9223372036854775808. Fits if negative. The
34997 ** special case 2 overflow if positive */
34998 assert( u-1==LARGEST_INT64 );
34999 return neg ? rc : 3;
35000 }
35001 }
35002 }
35003}
35004
35005/*
35006** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
35007** into a 64-bit signed integer. This routine accepts hexadecimal literals,
35008** whereas sqlite3Atoi64() does not.
35009**
35010** Returns:
35011**
35012** 0 Successful transformation. Fits in a 64-bit signed integer.
35013** 1 Excess text after the integer value
35014** 2 Integer too large for a 64-bit signed integer or is malformed
35015** 3 Special case of 9223372036854775808
35016*/
35017SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
35018#ifndef SQLITE_OMIT_HEX_INTEGER
35019 if( z[0]=='0'
35020 && (z[1]=='x' || z[1]=='X')
35021 ){
35022 u64 u = 0;
35023 int i, k;
35024 for(i=2; z[i]=='0'; i++){}
35025 for(k=i; sqlite3Isxdigit(z[k]); k++){
35026 u = u*16 + sqlite3HexToInt(h: z[k]);
35027 }
35028 memcpy(dest: pOut, src: &u, n: 8);
35029 if( k-i>16 ) return 2;
35030 if( z[k]!=0 ) return 1;
35031 return 0;
35032 }else
35033#endif /* SQLITE_OMIT_HEX_INTEGER */
35034 {
35035 int n = (int)(0x3fffffff&strspn(s: z,accept: "+- \n\t0123456789"));
35036 if( z[n] ) n++;
35037 return sqlite3Atoi64(zNum: z, pNum: pOut, length: n, SQLITE_UTF8);
35038 }
35039}
35040
35041/*
35042** If zNum represents an integer that will fit in 32-bits, then set
35043** *pValue to that integer and return true. Otherwise return false.
35044**
35045** This routine accepts both decimal and hexadecimal notation for integers.
35046**
35047** Any non-numeric characters that following zNum are ignored.
35048** This is different from sqlite3Atoi64() which requires the
35049** input number to be zero-terminated.
35050*/
35051SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
35052 sqlite_int64 v = 0;
35053 int i, c;
35054 int neg = 0;
35055 if( zNum[0]=='-' ){
35056 neg = 1;
35057 zNum++;
35058 }else if( zNum[0]=='+' ){
35059 zNum++;
35060 }
35061#ifndef SQLITE_OMIT_HEX_INTEGER
35062 else if( zNum[0]=='0'
35063 && (zNum[1]=='x' || zNum[1]=='X')
35064 && sqlite3Isxdigit(zNum[2])
35065 ){
35066 u32 u = 0;
35067 zNum += 2;
35068 while( zNum[0]=='0' ) zNum++;
35069 for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){
35070 u = u*16 + sqlite3HexToInt(h: zNum[i]);
35071 }
35072 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
35073 memcpy(dest: pValue, src: &u, n: 4);
35074 return 1;
35075 }else{
35076 return 0;
35077 }
35078 }
35079#endif
35080 if( !sqlite3Isdigit(zNum[0]) ) return 0;
35081 while( zNum[0]=='0' ) zNum++;
35082 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
35083 v = v*10 + c;
35084 }
35085
35086 /* The longest decimal representation of a 32 bit integer is 10 digits:
35087 **
35088 ** 1234567890
35089 ** 2^31 -> 2147483648
35090 */
35091 testcase( i==10 );
35092 if( i>10 ){
35093 return 0;
35094 }
35095 testcase( v-neg==2147483647 );
35096 if( v-neg>2147483647 ){
35097 return 0;
35098 }
35099 if( neg ){
35100 v = -v;
35101 }
35102 *pValue = (int)v;
35103 return 1;
35104}
35105
35106/*
35107** Return a 32-bit integer value extracted from a string. If the
35108** string is not an integer, just return 0.
35109*/
35110SQLITE_PRIVATE int sqlite3Atoi(const char *z){
35111 int x = 0;
35112 sqlite3GetInt32(zNum: z, pValue: &x);
35113 return x;
35114}
35115
35116/*
35117** Decode a floating-point value into an approximate decimal
35118** representation.
35119**
35120** Round the decimal representation to n significant digits if
35121** n is positive. Or round to -n signficant digits after the
35122** decimal point if n is negative. No rounding is performed if
35123** n is zero.
35124**
35125** The significant digits of the decimal representation are
35126** stored in p->z[] which is a often (but not always) a pointer
35127** into the middle of p->zBuf[]. There are p->n significant digits.
35128** The p->z[] array is *not* zero-terminated.
35129*/
35130SQLITE_PRIVATE void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){
35131 int i;
35132 u64 v;
35133 int e, exp = 0;
35134 p->isSpecial = 0;
35135 p->z = p->zBuf;
35136
35137 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
35138 ** NaN. */
35139 if( r<0.0 ){
35140 p->sign = '-';
35141 r = -r;
35142 }else if( r==0.0 ){
35143 p->sign = '+';
35144 p->n = 1;
35145 p->iDP = 1;
35146 p->z = "0";
35147 return;
35148 }else{
35149 p->sign = '+';
35150 }
35151 memcpy(dest: &v,src: &r,n: 8);
35152 e = v>>52;
35153 if( (e&0x7ff)==0x7ff ){
35154 p->isSpecial = 1 + (v!=0x7ff0000000000000LL);
35155 p->n = 0;
35156 p->iDP = 0;
35157 return;
35158 }
35159
35160 /* Multiply r by powers of ten until it lands somewhere in between
35161 ** 1.0e+19 and 1.0e+17.
35162 */
35163 if( sqlite3Config.bUseLongDouble ){
35164 LONGDOUBLE_TYPE rr = r;
35165 if( rr>=1.0e+19 ){
35166 while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; }
35167 while( rr>=1.0e+29L ){ exp+=10; rr *= 1.0e-10L; }
35168 while( rr>=1.0e+19L ){ exp++; rr *= 1.0e-1L; }
35169 }else{
35170 while( rr<1.0e-97L ){ exp-=100; rr *= 1.0e+100L; }
35171 while( rr<1.0e+07L ){ exp-=10; rr *= 1.0e+10L; }
35172 while( rr<1.0e+17L ){ exp--; rr *= 1.0e+1L; }
35173 }
35174 v = (u64)rr;
35175 }else{
35176 /* If high-precision floating point is not available using "long double",
35177 ** then use Dekker-style double-double computation to increase the
35178 ** precision.
35179 **
35180 ** The error terms on constants like 1.0e+100 computed using the
35181 ** decimal extension, for example as follows:
35182 **
35183 ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100)));
35184 */
35185 double rr[2];
35186 rr[0] = r;
35187 rr[1] = 0.0;
35188 if( rr[0]>1.84e+19 ){
35189 while( rr[0]>1.84e+119 ){
35190 exp += 100;
35191 dekkerMul2(x: rr, y: 1.0e-100, yy: -1.99918998026028836196e-117);
35192 }
35193 while( rr[0]>1.84e+29 ){
35194 exp += 10;
35195 dekkerMul2(x: rr, y: 1.0e-10, yy: -3.6432197315497741579e-27);
35196 }
35197 while( rr[0]>1.84e+19 ){
35198 exp += 1;
35199 dekkerMul2(x: rr, y: 1.0e-01, yy: -5.5511151231257827021e-18);
35200 }
35201 }else{
35202 while( rr[0]<1.84e-82 ){
35203 exp -= 100;
35204 dekkerMul2(x: rr, y: 1.0e+100, yy: -1.5902891109759918046e+83);
35205 }
35206 while( rr[0]<1.84e+08 ){
35207 exp -= 10;
35208 dekkerMul2(x: rr, y: 1.0e+10, yy: 0.0);
35209 }
35210 while( rr[0]<1.84e+18 ){
35211 exp -= 1;
35212 dekkerMul2(x: rr, y: 1.0e+01, yy: 0.0);
35213 }
35214 }
35215 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1];
35216 }
35217
35218
35219 /* Extract significant digits. */
35220 i = sizeof(p->zBuf)-1;
35221 assert( v>0 );
35222 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; }
35223 assert( i>=0 && i<sizeof(p->zBuf)-1 );
35224 p->n = sizeof(p->zBuf) - 1 - i;
35225 assert( p->n>0 );
35226 assert( p->n<sizeof(p->zBuf) );
35227 p->iDP = p->n + exp;
35228 if( iRound<0 ){
35229 iRound = p->iDP - iRound;
35230 if( iRound==0 && p->zBuf[i+1]>='5' ){
35231 iRound = 1;
35232 p->zBuf[i--] = '0';
35233 p->n++;
35234 p->iDP++;
35235 }
35236 }
35237 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){
35238 char *z = &p->zBuf[i+1];
35239 if( iRound>mxRound ) iRound = mxRound;
35240 p->n = iRound;
35241 if( z[iRound]>='5' ){
35242 int j = iRound-1;
35243 while( 1 /*exit-by-break*/ ){
35244 z[j]++;
35245 if( z[j]<='9' ) break;
35246 z[j] = '0';
35247 if( j==0 ){
35248 p->z[i--] = '1';
35249 p->n++;
35250 p->iDP++;
35251 break;
35252 }else{
35253 j--;
35254 }
35255 }
35256 }
35257 }
35258 p->z = &p->zBuf[i+1];
35259 assert( i+p->n < sizeof(p->zBuf) );
35260 while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; }
35261}
35262
35263/*
35264** Try to convert z into an unsigned 32-bit integer. Return true on
35265** success and false if there is an error.
35266**
35267** Only decimal notation is accepted.
35268*/
35269SQLITE_PRIVATE int sqlite3GetUInt32(const char *z, u32 *pI){
35270 u64 v = 0;
35271 int i;
35272 for(i=0; sqlite3Isdigit(z[i]); i++){
35273 v = v*10 + z[i] - '0';
35274 if( v>4294967296LL ){ *pI = 0; return 0; }
35275 }
35276 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
35277 *pI = (u32)v;
35278 return 1;
35279}
35280
35281/*
35282** The variable-length integer encoding is as follows:
35283**
35284** KEY:
35285** A = 0xxxxxxx 7 bits of data and one flag bit
35286** B = 1xxxxxxx 7 bits of data and one flag bit
35287** C = xxxxxxxx 8 bits of data
35288**
35289** 7 bits - A
35290** 14 bits - BA
35291** 21 bits - BBA
35292** 28 bits - BBBA
35293** 35 bits - BBBBA
35294** 42 bits - BBBBBA
35295** 49 bits - BBBBBBA
35296** 56 bits - BBBBBBBA
35297** 64 bits - BBBBBBBBC
35298*/
35299
35300/*
35301** Write a 64-bit variable-length integer to memory starting at p[0].
35302** The length of data write will be between 1 and 9 bytes. The number
35303** of bytes written is returned.
35304**
35305** A variable-length integer consists of the lower 7 bits of each byte
35306** for all bytes that have the 8th bit set and one byte with the 8th
35307** bit clear. Except, if we get to the 9th byte, it stores the full
35308** 8 bits and is the last byte.
35309*/
35310static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
35311 int i, j, n;
35312 u8 buf[10];
35313 if( v & (((u64)0xff000000)<<32) ){
35314 p[8] = (u8)v;
35315 v >>= 8;
35316 for(i=7; i>=0; i--){
35317 p[i] = (u8)((v & 0x7f) | 0x80);
35318 v >>= 7;
35319 }
35320 return 9;
35321 }
35322 n = 0;
35323 do{
35324 buf[n++] = (u8)((v & 0x7f) | 0x80);
35325 v >>= 7;
35326 }while( v!=0 );
35327 buf[0] &= 0x7f;
35328 assert( n<=9 );
35329 for(i=0, j=n-1; j>=0; j--, i++){
35330 p[i] = buf[j];
35331 }
35332 return n;
35333}
35334SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
35335 if( v<=0x7f ){
35336 p[0] = v&0x7f;
35337 return 1;
35338 }
35339 if( v<=0x3fff ){
35340 p[0] = ((v>>7)&0x7f)|0x80;
35341 p[1] = v&0x7f;
35342 return 2;
35343 }
35344 return putVarint64(p,v);
35345}
35346
35347/*
35348** Bitmasks used by sqlite3GetVarint(). These precomputed constants
35349** are defined here rather than simply putting the constant expressions
35350** inline in order to work around bugs in the RVT compiler.
35351**
35352** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
35353**
35354** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
35355*/
35356#define SLOT_2_0 0x001fc07f
35357#define SLOT_4_2_0 0xf01fc07f
35358
35359
35360/*
35361** Read a 64-bit variable-length integer from memory starting at p[0].
35362** Return the number of bytes read. The value is stored in *v.
35363*/
35364SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
35365 u32 a,b,s;
35366
35367 if( ((signed char*)p)[0]>=0 ){
35368 *v = *p;
35369 return 1;
35370 }
35371 if( ((signed char*)p)[1]>=0 ){
35372 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
35373 return 2;
35374 }
35375
35376 /* Verify that constants are precomputed correctly */
35377 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
35378 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
35379
35380 a = ((u32)p[0])<<14;
35381 b = p[1];
35382 p += 2;
35383 a |= *p;
35384 /* a: p0<<14 | p2 (unmasked) */
35385 if (!(a&0x80))
35386 {
35387 a &= SLOT_2_0;
35388 b &= 0x7f;
35389 b = b<<7;
35390 a |= b;
35391 *v = a;
35392 return 3;
35393 }
35394
35395 /* CSE1 from below */
35396 a &= SLOT_2_0;
35397 p++;
35398 b = b<<14;
35399 b |= *p;
35400 /* b: p1<<14 | p3 (unmasked) */
35401 if (!(b&0x80))
35402 {
35403 b &= SLOT_2_0;
35404 /* moved CSE1 up */
35405 /* a &= (0x7f<<14)|(0x7f); */
35406 a = a<<7;
35407 a |= b;
35408 *v = a;
35409 return 4;
35410 }
35411
35412 /* a: p0<<14 | p2 (masked) */
35413 /* b: p1<<14 | p3 (unmasked) */
35414 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
35415 /* moved CSE1 up */
35416 /* a &= (0x7f<<14)|(0x7f); */
35417 b &= SLOT_2_0;
35418 s = a;
35419 /* s: p0<<14 | p2 (masked) */
35420
35421 p++;
35422 a = a<<14;
35423 a |= *p;
35424 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
35425 if (!(a&0x80))
35426 {
35427 /* we can skip these cause they were (effectively) done above
35428 ** while calculating s */
35429 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
35430 /* b &= (0x7f<<14)|(0x7f); */
35431 b = b<<7;
35432 a |= b;
35433 s = s>>18;
35434 *v = ((u64)s)<<32 | a;
35435 return 5;
35436 }
35437
35438 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
35439 s = s<<7;
35440 s |= b;
35441 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
35442
35443 p++;
35444 b = b<<14;
35445 b |= *p;
35446 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
35447 if (!(b&0x80))
35448 {
35449 /* we can skip this cause it was (effectively) done above in calc'ing s */
35450 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
35451 a &= SLOT_2_0;
35452 a = a<<7;
35453 a |= b;
35454 s = s>>18;
35455 *v = ((u64)s)<<32 | a;
35456 return 6;
35457 }
35458
35459 p++;
35460 a = a<<14;
35461 a |= *p;
35462 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
35463 if (!(a&0x80))
35464 {
35465 a &= SLOT_4_2_0;
35466 b &= SLOT_2_0;
35467 b = b<<7;
35468 a |= b;
35469 s = s>>11;
35470 *v = ((u64)s)<<32 | a;
35471 return 7;
35472 }
35473
35474 /* CSE2 from below */
35475 a &= SLOT_2_0;
35476 p++;
35477 b = b<<14;
35478 b |= *p;
35479 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
35480 if (!(b&0x80))
35481 {
35482 b &= SLOT_4_2_0;
35483 /* moved CSE2 up */
35484 /* a &= (0x7f<<14)|(0x7f); */
35485 a = a<<7;
35486 a |= b;
35487 s = s>>4;
35488 *v = ((u64)s)<<32 | a;
35489 return 8;
35490 }
35491
35492 p++;
35493 a = a<<15;
35494 a |= *p;
35495 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
35496
35497 /* moved CSE2 up */
35498 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
35499 b &= SLOT_2_0;
35500 b = b<<8;
35501 a |= b;
35502
35503 s = s<<4;
35504 b = p[-4];
35505 b &= 0x7f;
35506 b = b>>3;
35507 s |= b;
35508
35509 *v = ((u64)s)<<32 | a;
35510
35511 return 9;
35512}
35513
35514/*
35515** Read a 32-bit variable-length integer from memory starting at p[0].
35516** Return the number of bytes read. The value is stored in *v.
35517**
35518** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
35519** integer, then set *v to 0xffffffff.
35520**
35521** A MACRO version, getVarint32, is provided which inlines the
35522** single-byte case. All code should use the MACRO version as
35523** this function assumes the single-byte case has already been handled.
35524*/
35525SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
35526 u32 a,b;
35527
35528 /* The 1-byte case. Overwhelmingly the most common. Handled inline
35529 ** by the getVarin32() macro */
35530 a = *p;
35531 /* a: p0 (unmasked) */
35532#ifndef getVarint32
35533 if (!(a&0x80))
35534 {
35535 /* Values between 0 and 127 */
35536 *v = a;
35537 return 1;
35538 }
35539#endif
35540
35541 /* The 2-byte case */
35542 p++;
35543 b = *p;
35544 /* b: p1 (unmasked) */
35545 if (!(b&0x80))
35546 {
35547 /* Values between 128 and 16383 */
35548 a &= 0x7f;
35549 a = a<<7;
35550 *v = a | b;
35551 return 2;
35552 }
35553
35554 /* The 3-byte case */
35555 p++;
35556 a = a<<14;
35557 a |= *p;
35558 /* a: p0<<14 | p2 (unmasked) */
35559 if (!(a&0x80))
35560 {
35561 /* Values between 16384 and 2097151 */
35562 a &= (0x7f<<14)|(0x7f);
35563 b &= 0x7f;
35564 b = b<<7;
35565 *v = a | b;
35566 return 3;
35567 }
35568
35569 /* A 32-bit varint is used to store size information in btrees.
35570 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
35571 ** A 3-byte varint is sufficient, for example, to record the size
35572 ** of a 1048569-byte BLOB or string.
35573 **
35574 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
35575 ** rare larger cases can be handled by the slower 64-bit varint
35576 ** routine.
35577 */
35578#if 1
35579 {
35580 u64 v64;
35581 u8 n;
35582
35583 n = sqlite3GetVarint(p: p-2, v: &v64);
35584 assert( n>3 && n<=9 );
35585 if( (v64 & SQLITE_MAX_U32)!=v64 ){
35586 *v = 0xffffffff;
35587 }else{
35588 *v = (u32)v64;
35589 }
35590 return n;
35591 }
35592
35593#else
35594 /* For following code (kept for historical record only) shows an
35595 ** unrolling for the 3- and 4-byte varint cases. This code is
35596 ** slightly faster, but it is also larger and much harder to test.
35597 */
35598 p++;
35599 b = b<<14;
35600 b |= *p;
35601 /* b: p1<<14 | p3 (unmasked) */
35602 if (!(b&0x80))
35603 {
35604 /* Values between 2097152 and 268435455 */
35605 b &= (0x7f<<14)|(0x7f);
35606 a &= (0x7f<<14)|(0x7f);
35607 a = a<<7;
35608 *v = a | b;
35609 return 4;
35610 }
35611
35612 p++;
35613 a = a<<14;
35614 a |= *p;
35615 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
35616 if (!(a&0x80))
35617 {
35618 /* Values between 268435456 and 34359738367 */
35619 a &= SLOT_4_2_0;
35620 b &= SLOT_4_2_0;
35621 b = b<<7;
35622 *v = a | b;
35623 return 5;
35624 }
35625
35626 /* We can only reach this point when reading a corrupt database
35627 ** file. In that case we are not in any hurry. Use the (relatively
35628 ** slow) general-purpose sqlite3GetVarint() routine to extract the
35629 ** value. */
35630 {
35631 u64 v64;
35632 u8 n;
35633
35634 p -= 4;
35635 n = sqlite3GetVarint(p, &v64);
35636 assert( n>5 && n<=9 );
35637 *v = (u32)v64;
35638 return n;
35639 }
35640#endif
35641}
35642
35643/*
35644** Return the number of bytes that will be needed to store the given
35645** 64-bit integer.
35646*/
35647SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
35648 int i;
35649 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
35650 return i;
35651}
35652
35653
35654/*
35655** Read or write a four-byte big-endian integer value.
35656*/
35657SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
35658#if SQLITE_BYTEORDER==4321
35659 u32 x;
35660 memcpy(&x,p,4);
35661 return x;
35662#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
35663 u32 x;
35664 memcpy(&x,p,4);
35665 return __builtin_bswap32(x);
35666#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
35667 u32 x;
35668 memcpy(&x,p,4);
35669 return _byteswap_ulong(x);
35670#else
35671 testcase( p[0]&0x80 );
35672 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
35673#endif
35674}
35675SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
35676#if SQLITE_BYTEORDER==4321
35677 memcpy(p,&v,4);
35678#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
35679 u32 x = __builtin_bswap32(v);
35680 memcpy(p,&x,4);
35681#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
35682 u32 x = _byteswap_ulong(v);
35683 memcpy(p,&x,4);
35684#else
35685 p[0] = (u8)(v>>24);
35686 p[1] = (u8)(v>>16);
35687 p[2] = (u8)(v>>8);
35688 p[3] = (u8)v;
35689#endif
35690}
35691
35692
35693
35694/*
35695** Translate a single byte of Hex into an integer.
35696** This routine only works if h really is a valid hexadecimal
35697** character: 0..9a..fA..F
35698*/
35699SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
35700 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
35701#ifdef SQLITE_ASCII
35702 h += 9*(1&(h>>6));
35703#endif
35704#ifdef SQLITE_EBCDIC
35705 h += 9*(1&~(h>>4));
35706#endif
35707 return (u8)(h & 0xf);
35708}
35709
35710#if !defined(SQLITE_OMIT_BLOB_LITERAL)
35711/*
35712** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
35713** value. Return a pointer to its binary value. Space to hold the
35714** binary value has been obtained from malloc and must be freed by
35715** the calling routine.
35716*/
35717SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
35718 char *zBlob;
35719 int i;
35720
35721 zBlob = (char *)sqlite3DbMallocRawNN(db, n: n/2 + 1);
35722 n--;
35723 if( zBlob ){
35724 for(i=0; i<n; i+=2){
35725 zBlob[i/2] = (sqlite3HexToInt(h: z[i])<<4) | sqlite3HexToInt(h: z[i+1]);
35726 }
35727 zBlob[i/2] = 0;
35728 }
35729 return zBlob;
35730}
35731#endif /* !SQLITE_OMIT_BLOB_LITERAL */
35732
35733/*
35734** Log an error that is an API call on a connection pointer that should
35735** not have been used. The "type" of connection pointer is given as the
35736** argument. The zType is a word like "NULL" or "closed" or "invalid".
35737*/
35738static void logBadConnection(const char *zType){
35739 sqlite3_log(SQLITE_MISUSE,
35740 zFormat: "API call with %s database connection pointer",
35741 zType
35742 );
35743}
35744
35745/*
35746** Check to make sure we have a valid db pointer. This test is not
35747** foolproof but it does provide some measure of protection against
35748** misuse of the interface such as passing in db pointers that are
35749** NULL or which have been previously closed. If this routine returns
35750** 1 it means that the db pointer is valid and 0 if it should not be
35751** dereferenced for any reason. The calling function should invoke
35752** SQLITE_MISUSE immediately.
35753**
35754** sqlite3SafetyCheckOk() requires that the db pointer be valid for
35755** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
35756** open properly and is not fit for general use but which can be
35757** used as an argument to sqlite3_errmsg() or sqlite3_close().
35758*/
35759SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
35760 u8 eOpenState;
35761 if( db==0 ){
35762 logBadConnection(zType: "NULL");
35763 return 0;
35764 }
35765 eOpenState = db->eOpenState;
35766 if( eOpenState!=SQLITE_STATE_OPEN ){
35767 if( sqlite3SafetyCheckSickOrOk(db) ){
35768 testcase( sqlite3GlobalConfig.xLog!=0 );
35769 logBadConnection(zType: "unopened");
35770 }
35771 return 0;
35772 }else{
35773 return 1;
35774 }
35775}
35776SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
35777 u8 eOpenState;
35778 eOpenState = db->eOpenState;
35779 if( eOpenState!=SQLITE_STATE_SICK &&
35780 eOpenState!=SQLITE_STATE_OPEN &&
35781 eOpenState!=SQLITE_STATE_BUSY ){
35782 testcase( sqlite3GlobalConfig.xLog!=0 );
35783 logBadConnection(zType: "invalid");
35784 return 0;
35785 }else{
35786 return 1;
35787 }
35788}
35789
35790/*
35791** Attempt to add, subtract, or multiply the 64-bit signed value iB against
35792** the other 64-bit signed integer at *pA and store the result in *pA.
35793** Return 0 on success. Or if the operation would have resulted in an
35794** overflow, leave *pA unchanged and return 1.
35795*/
35796SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
35797#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
35798 return __builtin_add_overflow(*pA, iB, pA);
35799#else
35800 i64 iA = *pA;
35801 testcase( iA==0 ); testcase( iA==1 );
35802 testcase( iB==-1 ); testcase( iB==0 );
35803 if( iB>=0 ){
35804 testcase( iA>0 && LARGEST_INT64 - iA == iB );
35805 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
35806 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
35807 }else{
35808 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
35809 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
35810 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
35811 }
35812 *pA += iB;
35813 return 0;
35814#endif
35815}
35816SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
35817#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
35818 return __builtin_sub_overflow(*pA, iB, pA);
35819#else
35820 testcase( iB==SMALLEST_INT64+1 );
35821 if( iB==SMALLEST_INT64 ){
35822 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
35823 if( (*pA)>=0 ) return 1;
35824 *pA -= iB;
35825 return 0;
35826 }else{
35827 return sqlite3AddInt64(pA, iB: -iB);
35828 }
35829#endif
35830}
35831SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
35832#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
35833 return __builtin_mul_overflow(*pA, iB, pA);
35834#else
35835 i64 iA = *pA;
35836 if( iB>0 ){
35837 if( iA>LARGEST_INT64/iB ) return 1;
35838 if( iA<SMALLEST_INT64/iB ) return 1;
35839 }else if( iB<0 ){
35840 if( iA>0 ){
35841 if( iB<SMALLEST_INT64/iA ) return 1;
35842 }else if( iA<0 ){
35843 if( iB==SMALLEST_INT64 ) return 1;
35844 if( iA==SMALLEST_INT64 ) return 1;
35845 if( -iA>LARGEST_INT64/-iB ) return 1;
35846 }
35847 }
35848 *pA = iA*iB;
35849 return 0;
35850#endif
35851}
35852
35853/*
35854** Compute the absolute value of a 32-bit signed integer, of possible. Or
35855** if the integer has a value of -2147483648, return +2147483647
35856*/
35857SQLITE_PRIVATE int sqlite3AbsInt32(int x){
35858 if( x>=0 ) return x;
35859 if( x==(int)0x80000000 ) return 0x7fffffff;
35860 return -x;
35861}
35862
35863#ifdef SQLITE_ENABLE_8_3_NAMES
35864/*
35865** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
35866** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
35867** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
35868** three characters, then shorten the suffix on z[] to be the last three
35869** characters of the original suffix.
35870**
35871** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
35872** do the suffix shortening regardless of URI parameter.
35873**
35874** Examples:
35875**
35876** test.db-journal => test.nal
35877** test.db-wal => test.wal
35878** test.db-shm => test.shm
35879** test.db-mj7f3319fa => test.9fa
35880*/
35881SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
35882#if SQLITE_ENABLE_8_3_NAMES<2
35883 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
35884#endif
35885 {
35886 int i, sz;
35887 sz = sqlite3Strlen30(z);
35888 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
35889 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
35890 }
35891}
35892#endif
35893
35894/*
35895** Find (an approximate) sum of two LogEst values. This computation is
35896** not a simple "+" operator because LogEst is stored as a logarithmic
35897** value.
35898**
35899*/
35900SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
35901 static const unsigned char x[] = {
35902 10, 10, /* 0,1 */
35903 9, 9, /* 2,3 */
35904 8, 8, /* 4,5 */
35905 7, 7, 7, /* 6,7,8 */
35906 6, 6, 6, /* 9,10,11 */
35907 5, 5, 5, /* 12-14 */
35908 4, 4, 4, 4, /* 15-18 */
35909 3, 3, 3, 3, 3, 3, /* 19-24 */
35910 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
35911 };
35912 if( a>=b ){
35913 if( a>b+49 ) return a;
35914 if( a>b+31 ) return a+1;
35915 return a+x[a-b];
35916 }else{
35917 if( b>a+49 ) return b;
35918 if( b>a+31 ) return b+1;
35919 return b+x[b-a];
35920 }
35921}
35922
35923/*
35924** Convert an integer into a LogEst. In other words, compute an
35925** approximation for 10*log2(x).
35926*/
35927SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
35928 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
35929 LogEst y = 40;
35930 if( x<8 ){
35931 if( x<2 ) return 0;
35932 while( x<8 ){ y -= 10; x <<= 1; }
35933 }else{
35934#if GCC_VERSION>=5004000
35935 int i = 60 - __builtin_clzll(x);
35936 y += i*10;
35937 x >>= i;
35938#else
35939 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
35940 while( x>15 ){ y += 10; x >>= 1; }
35941#endif
35942 }
35943 return a[x&7] + y - 10;
35944}
35945
35946/*
35947** Convert a double into a LogEst
35948** In other words, compute an approximation for 10*log2(x).
35949*/
35950SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
35951 u64 a;
35952 LogEst e;
35953 assert( sizeof(x)==8 && sizeof(a)==8 );
35954 if( x<=1 ) return 0;
35955 if( x<=2000000000 ) return sqlite3LogEst(x: (u64)x);
35956 memcpy(dest: &a, src: &x, n: 8);
35957 e = (a>>52) - 1022;
35958 return e*10;
35959}
35960
35961/*
35962** Convert a LogEst into an integer.
35963*/
35964SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
35965 u64 n;
35966 n = x%10;
35967 x /= 10;
35968 if( n>=5 ) n -= 2;
35969 else if( n>=1 ) n -= 1;
35970 if( x>60 ) return (u64)LARGEST_INT64;
35971 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
35972}
35973
35974/*
35975** Add a new name/number pair to a VList. This might require that the
35976** VList object be reallocated, so return the new VList. If an OOM
35977** error occurs, the original VList returned and the
35978** db->mallocFailed flag is set.
35979**
35980** A VList is really just an array of integers. To destroy a VList,
35981** simply pass it to sqlite3DbFree().
35982**
35983** The first integer is the number of integers allocated for the whole
35984** VList. The second integer is the number of integers actually used.
35985** Each name/number pair is encoded by subsequent groups of 3 or more
35986** integers.
35987**
35988** Each name/number pair starts with two integers which are the numeric
35989** value for the pair and the size of the name/number pair, respectively.
35990** The text name overlays one or more following integers. The text name
35991** is always zero-terminated.
35992**
35993** Conceptually:
35994**
35995** struct VList {
35996** int nAlloc; // Number of allocated slots
35997** int nUsed; // Number of used slots
35998** struct VListEntry {
35999** int iValue; // Value for this entry
36000** int nSlot; // Slots used by this entry
36001** // ... variable name goes here
36002** } a[0];
36003** }
36004**
36005** During code generation, pointers to the variable names within the
36006** VList are taken. When that happens, nAlloc is set to zero as an
36007** indication that the VList may never again be enlarged, since the
36008** accompanying realloc() would invalidate the pointers.
36009*/
36010SQLITE_PRIVATE VList *sqlite3VListAdd(
36011 sqlite3 *db, /* The database connection used for malloc() */
36012 VList *pIn, /* The input VList. Might be NULL */
36013 const char *zName, /* Name of symbol to add */
36014 int nName, /* Bytes of text in zName */
36015 int iVal /* Value to associate with zName */
36016){
36017 int nInt; /* number of sizeof(int) objects needed for zName */
36018 char *z; /* Pointer to where zName will be stored */
36019 int i; /* Index in pIn[] where zName is stored */
36020
36021 nInt = nName/4 + 3;
36022 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
36023 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
36024 /* Enlarge the allocation */
36025 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
36026 VList *pOut = sqlite3DbRealloc(db, p: pIn, n: nAlloc*sizeof(int));
36027 if( pOut==0 ) return pIn;
36028 if( pIn==0 ) pOut[1] = 2;
36029 pIn = pOut;
36030 pIn[0] = nAlloc;
36031 }
36032 i = pIn[1];
36033 pIn[i] = iVal;
36034 pIn[i+1] = nInt;
36035 z = (char*)&pIn[i+2];
36036 pIn[1] = i+nInt;
36037 assert( pIn[1]<=pIn[0] );
36038 memcpy(dest: z, src: zName, n: nName);
36039 z[nName] = 0;
36040 return pIn;
36041}
36042
36043/*
36044** Return a pointer to the name of a variable in the given VList that
36045** has the value iVal. Or return a NULL if there is no such variable in
36046** the list
36047*/
36048SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
36049 int i, mx;
36050 if( pIn==0 ) return 0;
36051 mx = pIn[1];
36052 i = 2;
36053 do{
36054 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
36055 i += pIn[i+1];
36056 }while( i<mx );
36057 return 0;
36058}
36059
36060/*
36061** Return the number of the variable named zName, if it is in VList.
36062** or return 0 if there is no such variable.
36063*/
36064SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
36065 int i, mx;
36066 if( pIn==0 ) return 0;
36067 mx = pIn[1];
36068 i = 2;
36069 do{
36070 const char *z = (const char*)&pIn[i+2];
36071 if( strncmp(s1: z,s2: zName,n: nName)==0 && z[nName]==0 ) return pIn[i];
36072 i += pIn[i+1];
36073 }while( i<mx );
36074 return 0;
36075}
36076
36077/*
36078** High-resolution hardware timer used for debugging and testing only.
36079*/
36080#if defined(VDBE_PROFILE) \
36081 || defined(SQLITE_PERFORMANCE_TRACE) \
36082 || defined(SQLITE_ENABLE_STMT_SCANSTATUS)
36083/************** Include hwtime.h in the middle of util.c *********************/
36084/************** Begin file hwtime.h ******************************************/
36085/*
36086** 2008 May 27
36087**
36088** The author disclaims copyright to this source code. In place of
36089** a legal notice, here is a blessing:
36090**
36091** May you do good and not evil.
36092** May you find forgiveness for yourself and forgive others.
36093** May you share freely, never taking more than you give.
36094**
36095******************************************************************************
36096**
36097** This file contains inline asm code for retrieving "high-performance"
36098** counters for x86 and x86_64 class CPUs.
36099*/
36100#ifndef SQLITE_HWTIME_H
36101#define SQLITE_HWTIME_H
36102
36103/*
36104** The following routine only works on Pentium-class (or newer) processors.
36105** It uses the RDTSC opcode to read the cycle count value out of the
36106** processor and returns that value. This can be used for high-res
36107** profiling.
36108*/
36109#if !defined(__STRICT_ANSI__) && \
36110 (defined(__GNUC__) || defined(_MSC_VER)) && \
36111 (defined(i386) || defined(__i386__) || defined(_M_IX86))
36112
36113 #if defined(__GNUC__)
36114
36115 __inline__ sqlite_uint64 sqlite3Hwtime(void){
36116 unsigned int lo, hi;
36117 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
36118 return (sqlite_uint64)hi << 32 | lo;
36119 }
36120
36121 #elif defined(_MSC_VER)
36122
36123 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
36124 __asm {
36125 rdtsc
36126 ret ; return value at EDX:EAX
36127 }
36128 }
36129
36130 #endif
36131
36132#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
36133
36134 __inline__ sqlite_uint64 sqlite3Hwtime(void){
36135 unsigned int lo, hi;
36136 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
36137 return (sqlite_uint64)hi << 32 | lo;
36138 }
36139
36140#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
36141
36142 __inline__ sqlite_uint64 sqlite3Hwtime(void){
36143 unsigned long long retval;
36144 unsigned long junk;
36145 __asm__ __volatile__ ("\n\
36146 1: mftbu %1\n\
36147 mftb %L0\n\
36148 mftbu %0\n\
36149 cmpw %0,%1\n\
36150 bne 1b"
36151 : "=r" (retval), "=r" (junk));
36152 return retval;
36153 }
36154
36155#else
36156
36157 /*
36158 ** asm() is needed for hardware timing support. Without asm(),
36159 ** disable the sqlite3Hwtime() routine.
36160 **
36161 ** sqlite3Hwtime() is only used for some obscure debugging
36162 ** and analysis configurations, not in any deliverable, so this
36163 ** should not be a great loss.
36164 */
36165SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
36166
36167#endif
36168
36169#endif /* !defined(SQLITE_HWTIME_H) */
36170
36171/************** End of hwtime.h **********************************************/
36172/************** Continuing where we left off in util.c ***********************/
36173#endif
36174
36175/************** End of util.c ************************************************/
36176/************** Begin file hash.c ********************************************/
36177/*
36178** 2001 September 22
36179**
36180** The author disclaims copyright to this source code. In place of
36181** a legal notice, here is a blessing:
36182**
36183** May you do good and not evil.
36184** May you find forgiveness for yourself and forgive others.
36185** May you share freely, never taking more than you give.
36186**
36187*************************************************************************
36188** This is the implementation of generic hash-tables
36189** used in SQLite.
36190*/
36191/* #include "sqliteInt.h" */
36192/* #include <assert.h> */
36193
36194/* Turn bulk memory into a hash table object by initializing the
36195** fields of the Hash structure.
36196**
36197** "pNew" is a pointer to the hash table that is to be initialized.
36198*/
36199SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
36200 assert( pNew!=0 );
36201 pNew->first = 0;
36202 pNew->count = 0;
36203 pNew->htsize = 0;
36204 pNew->ht = 0;
36205}
36206
36207/* Remove all entries from a hash table. Reclaim all memory.
36208** Call this routine to delete a hash table or to reset a hash table
36209** to the empty state.
36210*/
36211SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
36212 HashElem *elem; /* For looping over all elements of the table */
36213
36214 assert( pH!=0 );
36215 elem = pH->first;
36216 pH->first = 0;
36217 sqlite3_free(p: pH->ht);
36218 pH->ht = 0;
36219 pH->htsize = 0;
36220 while( elem ){
36221 HashElem *next_elem = elem->next;
36222 sqlite3_free(p: elem);
36223 elem = next_elem;
36224 }
36225 pH->count = 0;
36226}
36227
36228/*
36229** The hashing function.
36230*/
36231static unsigned int strHash(const char *z){
36232 unsigned int h = 0;
36233 unsigned char c;
36234 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
36235 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
36236 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
36237 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
36238 h += sqlite3UpperToLower[c];
36239 h *= 0x9e3779b1;
36240 }
36241 return h;
36242}
36243
36244
36245/* Link pNew element into the hash table pH. If pEntry!=0 then also
36246** insert pNew into the pEntry hash bucket.
36247*/
36248static void insertElement(
36249 Hash *pH, /* The complete hash table */
36250 struct _ht *pEntry, /* The entry into which pNew is inserted */
36251 HashElem *pNew /* The element to be inserted */
36252){
36253 HashElem *pHead; /* First element already in pEntry */
36254 if( pEntry ){
36255 pHead = pEntry->count ? pEntry->chain : 0;
36256 pEntry->count++;
36257 pEntry->chain = pNew;
36258 }else{
36259 pHead = 0;
36260 }
36261 if( pHead ){
36262 pNew->next = pHead;
36263 pNew->prev = pHead->prev;
36264 if( pHead->prev ){ pHead->prev->next = pNew; }
36265 else { pH->first = pNew; }
36266 pHead->prev = pNew;
36267 }else{
36268 pNew->next = pH->first;
36269 if( pH->first ){ pH->first->prev = pNew; }
36270 pNew->prev = 0;
36271 pH->first = pNew;
36272 }
36273}
36274
36275
36276/* Resize the hash table so that it contains "new_size" buckets.
36277**
36278** The hash table might fail to resize if sqlite3_malloc() fails or
36279** if the new size is the same as the prior size.
36280** Return TRUE if the resize occurs and false if not.
36281*/
36282static int rehash(Hash *pH, unsigned int new_size){
36283 struct _ht *new_ht; /* The new hash table */
36284 HashElem *elem, *next_elem; /* For looping over existing elements */
36285
36286#if SQLITE_MALLOC_SOFT_LIMIT>0
36287 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
36288 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
36289 }
36290 if( new_size==pH->htsize ) return 0;
36291#endif
36292
36293 /* The inability to allocates space for a larger hash table is
36294 ** a performance hit but it is not a fatal error. So mark the
36295 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
36296 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
36297 ** only zeroes the requested number of bytes whereas this module will
36298 ** use the actual amount of space allocated for the hash table (which
36299 ** may be larger than the requested amount).
36300 */
36301 sqlite3BeginBenignMalloc();
36302 new_ht = (struct _ht *)sqlite3Malloc( n: new_size*sizeof(struct _ht) );
36303 sqlite3EndBenignMalloc();
36304
36305 if( new_ht==0 ) return 0;
36306 sqlite3_free(p: pH->ht);
36307 pH->ht = new_ht;
36308 pH->htsize = new_size = sqlite3MallocSize(p: new_ht)/sizeof(struct _ht);
36309 memset(s: new_ht, c: 0, n: new_size*sizeof(struct _ht));
36310 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
36311 unsigned int h = strHash(z: elem->pKey) % new_size;
36312 next_elem = elem->next;
36313 insertElement(pH, pEntry: &new_ht[h], pNew: elem);
36314 }
36315 return 1;
36316}
36317
36318/* This function (for internal use only) locates an element in an
36319** hash table that matches the given key. If no element is found,
36320** a pointer to a static null element with HashElem.data==0 is returned.
36321** If pH is not NULL, then the hash for this key is written to *pH.
36322*/
36323static HashElem *findElementWithHash(
36324 const Hash *pH, /* The pH to be searched */
36325 const char *pKey, /* The key we are searching for */
36326 unsigned int *pHash /* Write the hash value here */
36327){
36328 HashElem *elem; /* Used to loop thru the element list */
36329 unsigned int count; /* Number of elements left to test */
36330 unsigned int h; /* The computed hash */
36331 static HashElem nullElement = { 0, 0, 0, 0 };
36332
36333 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
36334 struct _ht *pEntry;
36335 h = strHash(z: pKey) % pH->htsize;
36336 pEntry = &pH->ht[h];
36337 elem = pEntry->chain;
36338 count = pEntry->count;
36339 }else{
36340 h = 0;
36341 elem = pH->first;
36342 count = pH->count;
36343 }
36344 if( pHash ) *pHash = h;
36345 while( count ){
36346 assert( elem!=0 );
36347 if( sqlite3StrICmp(zLeft: elem->pKey,zRight: pKey)==0 ){
36348 return elem;
36349 }
36350 elem = elem->next;
36351 count--;
36352 }
36353 return &nullElement;
36354}
36355
36356/* Remove a single entry from the hash table given a pointer to that
36357** element and a hash on the element's key.
36358*/
36359static void removeElementGivenHash(
36360 Hash *pH, /* The pH containing "elem" */
36361 HashElem* elem, /* The element to be removed from the pH */
36362 unsigned int h /* Hash value for the element */
36363){
36364 struct _ht *pEntry;
36365 if( elem->prev ){
36366 elem->prev->next = elem->next;
36367 }else{
36368 pH->first = elem->next;
36369 }
36370 if( elem->next ){
36371 elem->next->prev = elem->prev;
36372 }
36373 if( pH->ht ){
36374 pEntry = &pH->ht[h];
36375 if( pEntry->chain==elem ){
36376 pEntry->chain = elem->next;
36377 }
36378 assert( pEntry->count>0 );
36379 pEntry->count--;
36380 }
36381 sqlite3_free( p: elem );
36382 pH->count--;
36383 if( pH->count==0 ){
36384 assert( pH->first==0 );
36385 assert( pH->count==0 );
36386 sqlite3HashClear(pH);
36387 }
36388}
36389
36390/* Attempt to locate an element of the hash table pH with a key
36391** that matches pKey. Return the data for this element if it is
36392** found, or NULL if there is no match.
36393*/
36394SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
36395 assert( pH!=0 );
36396 assert( pKey!=0 );
36397 return findElementWithHash(pH, pKey, pHash: 0)->data;
36398}
36399
36400/* Insert an element into the hash table pH. The key is pKey
36401** and the data is "data".
36402**
36403** If no element exists with a matching key, then a new
36404** element is created and NULL is returned.
36405**
36406** If another element already exists with the same key, then the
36407** new data replaces the old data and the old data is returned.
36408** The key is not copied in this instance. If a malloc fails, then
36409** the new data is returned and the hash table is unchanged.
36410**
36411** If the "data" parameter to this function is NULL, then the
36412** element corresponding to "key" is removed from the hash table.
36413*/
36414SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
36415 unsigned int h; /* the hash of the key modulo hash table size */
36416 HashElem *elem; /* Used to loop thru the element list */
36417 HashElem *new_elem; /* New element added to the pH */
36418
36419 assert( pH!=0 );
36420 assert( pKey!=0 );
36421 elem = findElementWithHash(pH,pKey,pHash: &h);
36422 if( elem->data ){
36423 void *old_data = elem->data;
36424 if( data==0 ){
36425 removeElementGivenHash(pH,elem,h);
36426 }else{
36427 elem->data = data;
36428 elem->pKey = pKey;
36429 }
36430 return old_data;
36431 }
36432 if( data==0 ) return 0;
36433 new_elem = (HashElem*)sqlite3Malloc( n: sizeof(HashElem) );
36434 if( new_elem==0 ) return data;
36435 new_elem->pKey = pKey;
36436 new_elem->data = data;
36437 pH->count++;
36438 if( pH->count>=10 && pH->count > 2*pH->htsize ){
36439 if( rehash(pH, new_size: pH->count*2) ){
36440 assert( pH->htsize>0 );
36441 h = strHash(z: pKey) % pH->htsize;
36442 }
36443 }
36444 insertElement(pH, pEntry: pH->ht ? &pH->ht[h] : 0, pNew: new_elem);
36445 return 0;
36446}
36447
36448/************** End of hash.c ************************************************/
36449/************** Begin file opcodes.c *****************************************/
36450/* Automatically generated. Do not edit */
36451/* See the tool/mkopcodec.tcl script for details. */
36452#if !defined(SQLITE_OMIT_EXPLAIN) \
36453 || defined(VDBE_PROFILE) \
36454 || defined(SQLITE_DEBUG)
36455#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
36456# define OpHelp(X) "\0" X
36457#else
36458# define OpHelp(X)
36459#endif
36460SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
36461 static const char *const azName[] = {
36462 /* 0 */ "Savepoint" OpHelp(""),
36463 /* 1 */ "AutoCommit" OpHelp(""),
36464 /* 2 */ "Transaction" OpHelp(""),
36465 /* 3 */ "Checkpoint" OpHelp(""),
36466 /* 4 */ "JournalMode" OpHelp(""),
36467 /* 5 */ "Vacuum" OpHelp(""),
36468 /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
36469 /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
36470 /* 8 */ "Init" OpHelp("Start at P2"),
36471 /* 9 */ "Goto" OpHelp(""),
36472 /* 10 */ "Gosub" OpHelp(""),
36473 /* 11 */ "InitCoroutine" OpHelp(""),
36474 /* 12 */ "Yield" OpHelp(""),
36475 /* 13 */ "MustBeInt" OpHelp(""),
36476 /* 14 */ "Jump" OpHelp(""),
36477 /* 15 */ "Once" OpHelp(""),
36478 /* 16 */ "If" OpHelp(""),
36479 /* 17 */ "IfNot" OpHelp(""),
36480 /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
36481 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
36482 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
36483 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
36484 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
36485 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
36486 /* 24 */ "SeekGT" OpHelp("key=r[P3@P4]"),
36487 /* 25 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
36488 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
36489 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
36490 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
36491 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
36492 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
36493 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
36494 /* 32 */ "Last" OpHelp(""),
36495 /* 33 */ "IfSmaller" OpHelp(""),
36496 /* 34 */ "SorterSort" OpHelp(""),
36497 /* 35 */ "Sort" OpHelp(""),
36498 /* 36 */ "Rewind" OpHelp(""),
36499 /* 37 */ "SorterNext" OpHelp(""),
36500 /* 38 */ "Prev" OpHelp(""),
36501 /* 39 */ "Next" OpHelp(""),
36502 /* 40 */ "IdxLE" OpHelp("key=r[P3@P4]"),
36503 /* 41 */ "IdxGT" OpHelp("key=r[P3@P4]"),
36504 /* 42 */ "IdxLT" OpHelp("key=r[P3@P4]"),
36505 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
36506 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
36507 /* 45 */ "IdxGE" OpHelp("key=r[P3@P4]"),
36508 /* 46 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
36509 /* 47 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
36510 /* 48 */ "Program" OpHelp(""),
36511 /* 49 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
36512 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
36513 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
36514 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
36515 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
36516 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
36517 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
36518 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
36519 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
36520 /* 58 */ "ElseEq" OpHelp(""),
36521 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
36522 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
36523 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
36524 /* 62 */ "IncrVacuum" OpHelp(""),
36525 /* 63 */ "VNext" OpHelp(""),
36526 /* 64 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
36527 /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
36528 /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
36529 /* 67 */ "Return" OpHelp(""),
36530 /* 68 */ "EndCoroutine" OpHelp(""),
36531 /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
36532 /* 70 */ "Halt" OpHelp(""),
36533 /* 71 */ "Integer" OpHelp("r[P2]=P1"),
36534 /* 72 */ "Int64" OpHelp("r[P2]=P4"),
36535 /* 73 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
36536 /* 74 */ "BeginSubrtn" OpHelp("r[P2]=NULL"),
36537 /* 75 */ "Null" OpHelp("r[P2..P3]=NULL"),
36538 /* 76 */ "SoftNull" OpHelp("r[P1]=NULL"),
36539 /* 77 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
36540 /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
36541 /* 79 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
36542 /* 80 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
36543 /* 81 */ "SCopy" OpHelp("r[P2]=r[P1]"),
36544 /* 82 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
36545 /* 83 */ "FkCheck" OpHelp(""),
36546 /* 84 */ "ResultRow" OpHelp("output=r[P1@P2]"),
36547 /* 85 */ "CollSeq" OpHelp(""),
36548 /* 86 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
36549 /* 87 */ "RealAffinity" OpHelp(""),
36550 /* 88 */ "Cast" OpHelp("affinity(r[P1])"),
36551 /* 89 */ "Permutation" OpHelp(""),
36552 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
36553 /* 91 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
36554 /* 92 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"),
36555 /* 93 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
36556 /* 94 */ "Column" OpHelp("r[P3]=PX cursor P1 column P2"),
36557 /* 95 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"),
36558 /* 96 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
36559 /* 97 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
36560 /* 98 */ "Count" OpHelp("r[P2]=count()"),
36561 /* 99 */ "ReadCookie" OpHelp(""),
36562 /* 100 */ "SetCookie" OpHelp(""),
36563 /* 101 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
36564 /* 102 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
36565 /* 103 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
36566 /* 104 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
36567 /* 105 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
36568 /* 106 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
36569 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
36570 /* 108 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
36571 /* 109 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
36572 /* 110 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
36573 /* 111 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
36574 /* 112 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
36575 /* 113 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
36576 /* 114 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
36577 /* 115 */ "OpenDup" OpHelp(""),
36578 /* 116 */ "OpenAutoindex" OpHelp("nColumn=P2"),
36579 /* 117 */ "String8" OpHelp("r[P2]='P4'"),
36580 /* 118 */ "OpenEphemeral" OpHelp("nColumn=P2"),
36581 /* 119 */ "SorterOpen" OpHelp(""),
36582 /* 120 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
36583 /* 121 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
36584 /* 122 */ "Close" OpHelp(""),
36585 /* 123 */ "ColumnsUsed" OpHelp(""),
36586 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
36587 /* 125 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"),
36588 /* 126 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
36589 /* 127 */ "NewRowid" OpHelp("r[P2]=rowid"),
36590 /* 128 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
36591 /* 129 */ "RowCell" OpHelp(""),
36592 /* 130 */ "Delete" OpHelp(""),
36593 /* 131 */ "ResetCount" OpHelp(""),
36594 /* 132 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
36595 /* 133 */ "SorterData" OpHelp("r[P2]=data"),
36596 /* 134 */ "RowData" OpHelp("r[P2]=data"),
36597 /* 135 */ "Rowid" OpHelp("r[P2]=PX rowid of P1"),
36598 /* 136 */ "NullRow" OpHelp(""),
36599 /* 137 */ "SeekEnd" OpHelp(""),
36600 /* 138 */ "IdxInsert" OpHelp("key=r[P2]"),
36601 /* 139 */ "SorterInsert" OpHelp("key=r[P2]"),
36602 /* 140 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
36603 /* 141 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
36604 /* 142 */ "IdxRowid" OpHelp("r[P2]=rowid"),
36605 /* 143 */ "FinishSeek" OpHelp(""),
36606 /* 144 */ "Destroy" OpHelp(""),
36607 /* 145 */ "Clear" OpHelp(""),
36608 /* 146 */ "ResetSorter" OpHelp(""),
36609 /* 147 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
36610 /* 148 */ "SqlExec" OpHelp(""),
36611 /* 149 */ "ParseSchema" OpHelp(""),
36612 /* 150 */ "LoadAnalysis" OpHelp(""),
36613 /* 151 */ "DropTable" OpHelp(""),
36614 /* 152 */ "DropIndex" OpHelp(""),
36615 /* 153 */ "Real" OpHelp("r[P2]=P4"),
36616 /* 154 */ "DropTrigger" OpHelp(""),
36617 /* 155 */ "IntegrityCk" OpHelp(""),
36618 /* 156 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
36619 /* 157 */ "Param" OpHelp(""),
36620 /* 158 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
36621 /* 159 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
36622 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
36623 /* 161 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
36624 /* 162 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
36625 /* 163 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
36626 /* 164 */ "AggValue" OpHelp("r[P3]=value N=P2"),
36627 /* 165 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
36628 /* 166 */ "Expire" OpHelp(""),
36629 /* 167 */ "CursorLock" OpHelp(""),
36630 /* 168 */ "CursorUnlock" OpHelp(""),
36631 /* 169 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
36632 /* 170 */ "VBegin" OpHelp(""),
36633 /* 171 */ "VCreate" OpHelp(""),
36634 /* 172 */ "VDestroy" OpHelp(""),
36635 /* 173 */ "VOpen" OpHelp(""),
36636 /* 174 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"),
36637 /* 175 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
36638 /* 176 */ "VRename" OpHelp(""),
36639 /* 177 */ "Pagecount" OpHelp(""),
36640 /* 178 */ "MaxPgcnt" OpHelp(""),
36641 /* 179 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"),
36642 /* 180 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"),
36643 /* 181 */ "Trace" OpHelp(""),
36644 /* 182 */ "CursorHint" OpHelp(""),
36645 /* 183 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
36646 /* 184 */ "Noop" OpHelp(""),
36647 /* 185 */ "Explain" OpHelp(""),
36648 /* 186 */ "Abortable" OpHelp(""),
36649 };
36650 return azName[i];
36651}
36652#endif
36653
36654/************** End of opcodes.c *********************************************/
36655/************** Begin file os_kv.c *******************************************/
36656/*
36657** 2022-09-06
36658**
36659** The author disclaims copyright to this source code. In place of
36660** a legal notice, here is a blessing:
36661**
36662** May you do good and not evil.
36663** May you find forgiveness for yourself and forgive others.
36664** May you share freely, never taking more than you give.
36665**
36666******************************************************************************
36667**
36668** This file contains an experimental VFS layer that operates on a
36669** Key/Value storage engine where both keys and values must be pure
36670** text.
36671*/
36672/* #include <sqliteInt.h> */
36673#if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL))
36674
36675/*****************************************************************************
36676** Debugging logic
36677*/
36678
36679/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
36680#if 0
36681#define SQLITE_KV_TRACE(X) printf X
36682#else
36683#define SQLITE_KV_TRACE(X)
36684#endif
36685
36686/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
36687#if 0
36688#define SQLITE_KV_LOG(X) printf X
36689#else
36690#define SQLITE_KV_LOG(X)
36691#endif
36692
36693
36694/*
36695** Forward declaration of objects used by this VFS implementation
36696*/
36697typedef struct KVVfsFile KVVfsFile;
36698
36699/* A single open file. There are only two files represented by this
36700** VFS - the database and the rollback journal.
36701*/
36702struct KVVfsFile {
36703 sqlite3_file base; /* IO methods */
36704 const char *zClass; /* Storage class */
36705 int isJournal; /* True if this is a journal file */
36706 unsigned int nJrnl; /* Space allocated for aJrnl[] */
36707 char *aJrnl; /* Journal content */
36708 int szPage; /* Last known page size */
36709 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
36710 char *aData; /* Buffer to hold page data */
36711};
36712#define SQLITE_KVOS_SZ 133073
36713
36714/*
36715** Methods for KVVfsFile
36716*/
36717static int kvvfsClose(sqlite3_file*);
36718static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
36719static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
36720static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
36721static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
36722static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
36723static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
36724static int kvvfsSyncDb(sqlite3_file*, int flags);
36725static int kvvfsSyncJrnl(sqlite3_file*, int flags);
36726static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
36727static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
36728static int kvvfsLock(sqlite3_file*, int);
36729static int kvvfsUnlock(sqlite3_file*, int);
36730static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
36731static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
36732static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
36733static int kvvfsSectorSize(sqlite3_file*);
36734static int kvvfsDeviceCharacteristics(sqlite3_file*);
36735
36736/*
36737** Methods for sqlite3_vfs
36738*/
36739static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
36740static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
36741static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
36742static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
36743static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
36744static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
36745static int kvvfsSleep(sqlite3_vfs*, int microseconds);
36746static int kvvfsCurrentTime(sqlite3_vfs*, double*);
36747static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
36748
36749static sqlite3_vfs sqlite3OsKvvfsObject = {
36750 1, /* iVersion */
36751 sizeof(KVVfsFile), /* szOsFile */
36752 1024, /* mxPathname */
36753 0, /* pNext */
36754 "kvvfs", /* zName */
36755 0, /* pAppData */
36756 kvvfsOpen, /* xOpen */
36757 kvvfsDelete, /* xDelete */
36758 kvvfsAccess, /* xAccess */
36759 kvvfsFullPathname, /* xFullPathname */
36760 kvvfsDlOpen, /* xDlOpen */
36761 0, /* xDlError */
36762 0, /* xDlSym */
36763 0, /* xDlClose */
36764 kvvfsRandomness, /* xRandomness */
36765 kvvfsSleep, /* xSleep */
36766 kvvfsCurrentTime, /* xCurrentTime */
36767 0, /* xGetLastError */
36768 kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
36769};
36770
36771/* Methods for sqlite3_file objects referencing a database file
36772*/
36773static sqlite3_io_methods kvvfs_db_io_methods = {
36774 1, /* iVersion */
36775 kvvfsClose, /* xClose */
36776 kvvfsReadDb, /* xRead */
36777 kvvfsWriteDb, /* xWrite */
36778 kvvfsTruncateDb, /* xTruncate */
36779 kvvfsSyncDb, /* xSync */
36780 kvvfsFileSizeDb, /* xFileSize */
36781 kvvfsLock, /* xLock */
36782 kvvfsUnlock, /* xUnlock */
36783 kvvfsCheckReservedLock, /* xCheckReservedLock */
36784 kvvfsFileControlDb, /* xFileControl */
36785 kvvfsSectorSize, /* xSectorSize */
36786 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
36787 0, /* xShmMap */
36788 0, /* xShmLock */
36789 0, /* xShmBarrier */
36790 0, /* xShmUnmap */
36791 0, /* xFetch */
36792 0 /* xUnfetch */
36793};
36794
36795/* Methods for sqlite3_file objects referencing a rollback journal
36796*/
36797static sqlite3_io_methods kvvfs_jrnl_io_methods = {
36798 1, /* iVersion */
36799 kvvfsClose, /* xClose */
36800 kvvfsReadJrnl, /* xRead */
36801 kvvfsWriteJrnl, /* xWrite */
36802 kvvfsTruncateJrnl, /* xTruncate */
36803 kvvfsSyncJrnl, /* xSync */
36804 kvvfsFileSizeJrnl, /* xFileSize */
36805 kvvfsLock, /* xLock */
36806 kvvfsUnlock, /* xUnlock */
36807 kvvfsCheckReservedLock, /* xCheckReservedLock */
36808 kvvfsFileControlJrnl, /* xFileControl */
36809 kvvfsSectorSize, /* xSectorSize */
36810 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
36811 0, /* xShmMap */
36812 0, /* xShmLock */
36813 0, /* xShmBarrier */
36814 0, /* xShmUnmap */
36815 0, /* xFetch */
36816 0 /* xUnfetch */
36817};
36818
36819/****** Storage subsystem **************************************************/
36820#include <sys/types.h>
36821#include <sys/stat.h>
36822#include <unistd.h>
36823
36824/* Forward declarations for the low-level storage engine
36825*/
36826static int kvstorageWrite(const char*, const char *zKey, const char *zData);
36827static int kvstorageDelete(const char*, const char *zKey);
36828static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
36829#define KVSTORAGE_KEY_SZ 32
36830
36831/* Expand the key name with an appropriate prefix and put the result
36832** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
36833** KVSTORAGE_KEY_SZ bytes.
36834*/
36835static void kvstorageMakeKey(
36836 const char *zClass,
36837 const char *zKeyIn,
36838 char *zKeyOut
36839){
36840 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
36841}
36842
36843/* Write content into a key. zClass is the particular namespace of the
36844** underlying key/value store to use - either "local" or "session".
36845**
36846** Both zKey and zData are zero-terminated pure text strings.
36847**
36848** Return the number of errors.
36849*/
36850static int kvstorageWrite(
36851 const char *zClass,
36852 const char *zKey,
36853 const char *zData
36854){
36855 FILE *fd;
36856 char zXKey[KVSTORAGE_KEY_SZ];
36857 kvstorageMakeKey(zClass, zKey, zXKey);
36858 fd = fopen(zXKey, "wb");
36859 if( fd ){
36860 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
36861 (int)strlen(zData), zData,
36862 strlen(zData)>50 ? "..." : ""));
36863 fputs(zData, fd);
36864 fclose(fd);
36865 return 0;
36866 }else{
36867 return 1;
36868 }
36869}
36870
36871/* Delete a key (with its corresponding data) from the key/value
36872** namespace given by zClass. If the key does not previously exist,
36873** this routine is a no-op.
36874*/
36875static int kvstorageDelete(const char *zClass, const char *zKey){
36876 char zXKey[KVSTORAGE_KEY_SZ];
36877 kvstorageMakeKey(zClass, zKey, zXKey);
36878 unlink(zXKey);
36879 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
36880 return 0;
36881}
36882
36883/* Read the value associated with a zKey from the key/value namespace given
36884** by zClass and put the text data associated with that key in the first
36885** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
36886** enough to hold it all. The value put into zBuf must always be zero
36887** terminated, even if it gets truncated because nBuf is not large enough.
36888**
36889** Return the total number of bytes in the data, without truncation, and
36890** not counting the final zero terminator. Return -1 if the key does
36891** not exist.
36892**
36893** If nBuf<=0 then this routine simply returns the size of the data without
36894** actually reading it.
36895*/
36896static int kvstorageRead(
36897 const char *zClass,
36898 const char *zKey,
36899 char *zBuf,
36900 int nBuf
36901){
36902 FILE *fd;
36903 struct stat buf;
36904 char zXKey[KVSTORAGE_KEY_SZ];
36905 kvstorageMakeKey(zClass, zKey, zXKey);
36906 if( access(zXKey, R_OK)!=0
36907 || stat(zXKey, &buf)!=0
36908 || !S_ISREG(buf.st_mode)
36909 ){
36910 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36911 return -1;
36912 }
36913 if( nBuf<=0 ){
36914 return (int)buf.st_size;
36915 }else if( nBuf==1 ){
36916 zBuf[0] = 0;
36917 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
36918 (int)buf.st_size));
36919 return (int)buf.st_size;
36920 }
36921 if( nBuf > buf.st_size + 1 ){
36922 nBuf = buf.st_size + 1;
36923 }
36924 fd = fopen(zXKey, "rb");
36925 if( fd==0 ){
36926 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36927 return -1;
36928 }else{
36929 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
36930 fclose(fd);
36931 zBuf[n] = 0;
36932 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
36933 n, zBuf, n>50 ? "..." : ""));
36934 return (int)n;
36935 }
36936}
36937
36938/*
36939** An internal level of indirection which enables us to replace the
36940** kvvfs i/o methods with JavaScript implementations in WASM builds.
36941** Maintenance reminder: if this struct changes in any way, the JSON
36942** rendering of its structure must be updated in
36943** sqlite3_wasm_enum_json(). There are no binary compatibility
36944** concerns, so it does not need an iVersion member. This file is
36945** necessarily always compiled together with sqlite3_wasm_enum_json(),
36946** and JS code dynamically creates the mapping of members based on
36947** that JSON description.
36948*/
36949typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
36950struct sqlite3_kvvfs_methods {
36951 int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
36952 int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
36953 int (*xDelete)(const char *zClass, const char *zKey);
36954 const int nKeySize;
36955};
36956
36957/*
36958** This object holds the kvvfs I/O methods which may be swapped out
36959** for JavaScript-side implementations in WASM builds. In such builds
36960** it cannot be const, but in native builds it should be so that
36961** the compiler can hopefully optimize this level of indirection out.
36962** That said, kvvfs is intended primarily for use in WASM builds.
36963**
36964** Note that this is not explicitly flagged as static because the
36965** amalgamation build will tag it with SQLITE_PRIVATE.
36966*/
36967#ifndef SQLITE_WASM
36968const
36969#endif
36970SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
36971kvstorageRead,
36972kvstorageWrite,
36973kvstorageDelete,
36974KVSTORAGE_KEY_SZ
36975};
36976
36977/****** Utility subroutines ************************************************/
36978
36979/*
36980** Encode binary into the text encoded used to persist on disk.
36981** The output text is stored in aOut[], which must be at least
36982** nData+1 bytes in length.
36983**
36984** Return the actual length of the encoded text, not counting the
36985** zero terminator at the end.
36986**
36987** Encoding format
36988** ---------------
36989**
36990** * Non-zero bytes are encoded as upper-case hexadecimal
36991**
36992** * A sequence of one or more zero-bytes that are not at the
36993** beginning of the buffer are encoded as a little-endian
36994** base-26 number using a..z. "a" means 0. "b" means 1,
36995** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
36996**
36997** * Because there is no overlap between the encoding characters
36998** of hexadecimal and base-26 numbers, it is always clear where
36999** one stops and the next begins.
37000*/
37001static int kvvfsEncode(const char *aData, int nData, char *aOut){
37002 int i, j;
37003 const unsigned char *a = (const unsigned char*)aData;
37004 for(i=j=0; i<nData; i++){
37005 unsigned char c = a[i];
37006 if( c!=0 ){
37007 aOut[j++] = "0123456789ABCDEF"[c>>4];
37008 aOut[j++] = "0123456789ABCDEF"[c&0xf];
37009 }else{
37010 /* A sequence of 1 or more zeros is stored as a little-endian
37011 ** base-26 number using a..z as the digits. So one zero is "b".
37012 ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
37013 ** and so forth.
37014 */
37015 int k;
37016 for(k=1; i+k<nData && a[i+k]==0; k++){}
37017 i += k-1;
37018 while( k>0 ){
37019 aOut[j++] = 'a'+(k%26);
37020 k /= 26;
37021 }
37022 }
37023 }
37024 aOut[j] = 0;
37025 return j;
37026}
37027
37028static const signed char kvvfsHexValue[256] = {
37029 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37030 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37031 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37032 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
37033 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37034 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37035 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37036 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37037
37038 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37039 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37040 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37041 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37042 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37043 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37044 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37045 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
37046};
37047
37048/*
37049** Decode the text encoding back to binary. The binary content is
37050** written into pOut, which must be at least nOut bytes in length.
37051**
37052** The return value is the number of bytes actually written into aOut[].
37053*/
37054static int kvvfsDecode(const char *a, char *aOut, int nOut){
37055 int i, j;
37056 int c;
37057 const unsigned char *aIn = (const unsigned char*)a;
37058 i = 0;
37059 j = 0;
37060 while( 1 ){
37061 c = kvvfsHexValue[aIn[i]];
37062 if( c<0 ){
37063 int n = 0;
37064 int mult = 1;
37065 c = aIn[i];
37066 if( c==0 ) break;
37067 while( c>='a' && c<='z' ){
37068 n += (c - 'a')*mult;
37069 mult *= 26;
37070 c = aIn[++i];
37071 }
37072 if( j+n>nOut ) return -1;
37073 memset(&aOut[j], 0, n);
37074 j += n;
37075 if( c==0 || mult==1 ) break; /* progress stalled if mult==1 */
37076 }else{
37077 aOut[j] = c<<4;
37078 c = kvvfsHexValue[aIn[++i]];
37079 if( c<0 ) break;
37080 aOut[j++] += c;
37081 i++;
37082 }
37083 }
37084 return j;
37085}
37086
37087/*
37088** Decode a complete journal file. Allocate space in pFile->aJrnl
37089** and store the decoding there. Or leave pFile->aJrnl set to NULL
37090** if an error is encountered.
37091**
37092** The first few characters of the text encoding will be a little-endian
37093** base-26 number (digits a..z) that is the total number of bytes
37094** in the decoded journal file image. This base-26 number is followed
37095** by a single space, then the encoding of the journal. The space
37096** separator is required to act as a terminator for the base-26 number.
37097*/
37098static void kvvfsDecodeJournal(
37099 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
37100 const char *zTxt, /* Text encoding. Zero-terminated */
37101 int nTxt /* Bytes in zTxt, excluding zero terminator */
37102){
37103 unsigned int n = 0;
37104 int c, i, mult;
37105 i = 0;
37106 mult = 1;
37107 while( (c = zTxt[i++])>='a' && c<='z' ){
37108 n += (zTxt[i] - 'a')*mult;
37109 mult *= 26;
37110 }
37111 sqlite3_free(pFile->aJrnl);
37112 pFile->aJrnl = sqlite3_malloc64( n );
37113 if( pFile->aJrnl==0 ){
37114 pFile->nJrnl = 0;
37115 return;
37116 }
37117 pFile->nJrnl = n;
37118 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
37119 if( n<pFile->nJrnl ){
37120 sqlite3_free(pFile->aJrnl);
37121 pFile->aJrnl = 0;
37122 pFile->nJrnl = 0;
37123 }
37124}
37125
37126/*
37127** Read or write the "sz" element, containing the database file size.
37128*/
37129static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
37130 char zData[50];
37131 zData[0] = 0;
37132 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
37133 return strtoll(zData, 0, 0);
37134}
37135static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
37136 char zData[50];
37137 sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
37138 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
37139}
37140
37141/****** sqlite3_io_methods methods ******************************************/
37142
37143/*
37144** Close an kvvfs-file.
37145*/
37146static int kvvfsClose(sqlite3_file *pProtoFile){
37147 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37148
37149 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
37150 pFile->isJournal ? "journal" : "db"));
37151 sqlite3_free(pFile->aJrnl);
37152 sqlite3_free(pFile->aData);
37153 return SQLITE_OK;
37154}
37155
37156/*
37157** Read from the -journal file.
37158*/
37159static int kvvfsReadJrnl(
37160 sqlite3_file *pProtoFile,
37161 void *zBuf,
37162 int iAmt,
37163 sqlite_int64 iOfst
37164){
37165 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
37166 assert( pFile->isJournal );
37167 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37168 if( pFile->aJrnl==0 ){
37169 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
37170 char *aTxt;
37171 if( szTxt<=4 ){
37172 return SQLITE_IOERR;
37173 }
37174 aTxt = sqlite3_malloc64( szTxt+1 );
37175 if( aTxt==0 ) return SQLITE_NOMEM;
37176 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
37177 kvvfsDecodeJournal(pFile, aTxt, szTxt);
37178 sqlite3_free(aTxt);
37179 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
37180 }
37181 if( iOfst+iAmt>pFile->nJrnl ){
37182 return SQLITE_IOERR_SHORT_READ;
37183 }
37184 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
37185 return SQLITE_OK;
37186}
37187
37188/*
37189** Read from the database file.
37190*/
37191static int kvvfsReadDb(
37192 sqlite3_file *pProtoFile,
37193 void *zBuf,
37194 int iAmt,
37195 sqlite_int64 iOfst
37196){
37197 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
37198 unsigned int pgno;
37199 int got, n;
37200 char zKey[30];
37201 char *aData = pFile->aData;
37202 assert( iOfst>=0 );
37203 assert( iAmt>=0 );
37204 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37205 if( iOfst+iAmt>=512 ){
37206 if( (iOfst % iAmt)!=0 ){
37207 return SQLITE_IOERR_READ;
37208 }
37209 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
37210 return SQLITE_IOERR_READ;
37211 }
37212 pFile->szPage = iAmt;
37213 pgno = 1 + iOfst/iAmt;
37214 }else{
37215 pgno = 1;
37216 }
37217 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
37218 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
37219 aData, SQLITE_KVOS_SZ-1);
37220 if( got<0 ){
37221 n = 0;
37222 }else{
37223 aData[got] = 0;
37224 if( iOfst+iAmt<512 ){
37225 int k = iOfst+iAmt;
37226 aData[k*2] = 0;
37227 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
37228 if( n>=iOfst+iAmt ){
37229 memcpy(zBuf, &aData[2000+iOfst], iAmt);
37230 n = iAmt;
37231 }else{
37232 n = 0;
37233 }
37234 }else{
37235 n = kvvfsDecode(aData, zBuf, iAmt);
37236 }
37237 }
37238 if( n<iAmt ){
37239 memset(zBuf+n, 0, iAmt-n);
37240 return SQLITE_IOERR_SHORT_READ;
37241 }
37242 return SQLITE_OK;
37243}
37244
37245
37246/*
37247** Write into the -journal file.
37248*/
37249static int kvvfsWriteJrnl(
37250 sqlite3_file *pProtoFile,
37251 const void *zBuf,
37252 int iAmt,
37253 sqlite_int64 iOfst
37254){
37255 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
37256 sqlite3_int64 iEnd = iOfst+iAmt;
37257 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37258 if( iEnd>=0x10000000 ) return SQLITE_FULL;
37259 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
37260 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
37261 if( aNew==0 ){
37262 return SQLITE_IOERR_NOMEM;
37263 }
37264 pFile->aJrnl = aNew;
37265 if( pFile->nJrnl<iOfst ){
37266 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
37267 }
37268 pFile->nJrnl = iEnd;
37269 }
37270 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
37271 return SQLITE_OK;
37272}
37273
37274/*
37275** Write into the database file.
37276*/
37277static int kvvfsWriteDb(
37278 sqlite3_file *pProtoFile,
37279 const void *zBuf,
37280 int iAmt,
37281 sqlite_int64 iOfst
37282){
37283 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
37284 unsigned int pgno;
37285 char zKey[30];
37286 char *aData = pFile->aData;
37287 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37288 assert( iAmt>=512 && iAmt<=65536 );
37289 assert( (iAmt & (iAmt-1))==0 );
37290 assert( pFile->szPage<0 || pFile->szPage==iAmt );
37291 pFile->szPage = iAmt;
37292 pgno = 1 + iOfst/iAmt;
37293 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
37294 kvvfsEncode(zBuf, iAmt, aData);
37295 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
37296 return SQLITE_IOERR;
37297 }
37298 if( iOfst+iAmt > pFile->szDb ){
37299 pFile->szDb = iOfst + iAmt;
37300 }
37301 return SQLITE_OK;
37302}
37303
37304/*
37305** Truncate an kvvfs-file.
37306*/
37307static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
37308 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37309 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
37310 assert( size==0 );
37311 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
37312 sqlite3_free(pFile->aJrnl);
37313 pFile->aJrnl = 0;
37314 pFile->nJrnl = 0;
37315 return SQLITE_OK;
37316}
37317static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
37318 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37319 if( pFile->szDb>size
37320 && pFile->szPage>0
37321 && (size % pFile->szPage)==0
37322 ){
37323 char zKey[50];
37324 unsigned int pgno, pgnoMax;
37325 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
37326 pgno = 1 + size/pFile->szPage;
37327 pgnoMax = 2 + pFile->szDb/pFile->szPage;
37328 while( pgno<=pgnoMax ){
37329 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
37330 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
37331 pgno++;
37332 }
37333 pFile->szDb = size;
37334 return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK;
37335 }
37336 return SQLITE_IOERR;
37337}
37338
37339/*
37340** Sync an kvvfs-file.
37341*/
37342static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
37343 int i, n;
37344 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37345 char *zOut;
37346 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
37347 if( pFile->nJrnl<=0 ){
37348 return kvvfsTruncateJrnl(pProtoFile, 0);
37349 }
37350 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
37351 if( zOut==0 ){
37352 return SQLITE_IOERR_NOMEM;
37353 }
37354 n = pFile->nJrnl;
37355 i = 0;
37356 do{
37357 zOut[i++] = 'a' + (n%26);
37358 n /= 26;
37359 }while( n>0 );
37360 zOut[i++] = ' ';
37361 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
37362 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
37363 sqlite3_free(zOut);
37364 return i ? SQLITE_IOERR : SQLITE_OK;
37365}
37366static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
37367 return SQLITE_OK;
37368}
37369
37370/*
37371** Return the current file-size of an kvvfs-file.
37372*/
37373static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
37374 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37375 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
37376 *pSize = pFile->nJrnl;
37377 return SQLITE_OK;
37378}
37379static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
37380 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37381 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
37382 if( pFile->szDb>=0 ){
37383 *pSize = pFile->szDb;
37384 }else{
37385 *pSize = kvvfsReadFileSize(pFile);
37386 }
37387 return SQLITE_OK;
37388}
37389
37390/*
37391** Lock an kvvfs-file.
37392*/
37393static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
37394 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37395 assert( !pFile->isJournal );
37396 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
37397
37398 if( eLock!=SQLITE_LOCK_NONE ){
37399 pFile->szDb = kvvfsReadFileSize(pFile);
37400 }
37401 return SQLITE_OK;
37402}
37403
37404/*
37405** Unlock an kvvfs-file.
37406*/
37407static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
37408 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37409 assert( !pFile->isJournal );
37410 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
37411 if( eLock==SQLITE_LOCK_NONE ){
37412 pFile->szDb = -1;
37413 }
37414 return SQLITE_OK;
37415}
37416
37417/*
37418** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
37419*/
37420static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
37421 SQLITE_KV_LOG(("xCheckReservedLock\n"));
37422 *pResOut = 0;
37423 return SQLITE_OK;
37424}
37425
37426/*
37427** File control method. For custom operations on an kvvfs-file.
37428*/
37429static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
37430 SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
37431 return SQLITE_NOTFOUND;
37432}
37433static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
37434 SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
37435 if( op==SQLITE_FCNTL_SYNC ){
37436 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37437 int rc = SQLITE_OK;
37438 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
37439 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
37440 rc = SQLITE_IOERR;
37441 }
37442 return rc;
37443 }
37444 return SQLITE_NOTFOUND;
37445}
37446
37447/*
37448** Return the sector-size in bytes for an kvvfs-file.
37449*/
37450static int kvvfsSectorSize(sqlite3_file *pFile){
37451 return 512;
37452}
37453
37454/*
37455** Return the device characteristic flags supported by an kvvfs-file.
37456*/
37457static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
37458 return 0;
37459}
37460
37461/****** sqlite3_vfs methods *************************************************/
37462
37463/*
37464** Open an kvvfs file handle.
37465*/
37466static int kvvfsOpen(
37467 sqlite3_vfs *pProtoVfs,
37468 const char *zName,
37469 sqlite3_file *pProtoFile,
37470 int flags,
37471 int *pOutFlags
37472){
37473 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
37474 if( zName==0 ) zName = "";
37475 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
37476 if( strcmp(zName, "local")==0
37477 || strcmp(zName, "session")==0
37478 ){
37479 pFile->isJournal = 0;
37480 pFile->base.pMethods = &kvvfs_db_io_methods;
37481 }else
37482 if( strcmp(zName, "local-journal")==0
37483 || strcmp(zName, "session-journal")==0
37484 ){
37485 pFile->isJournal = 1;
37486 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
37487 }else{
37488 return SQLITE_CANTOPEN;
37489 }
37490 if( zName[0]=='s' ){
37491 pFile->zClass = "session";
37492 }else{
37493 pFile->zClass = "local";
37494 }
37495 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
37496 if( pFile->aData==0 ){
37497 return SQLITE_NOMEM;
37498 }
37499 pFile->aJrnl = 0;
37500 pFile->nJrnl = 0;
37501 pFile->szPage = -1;
37502 pFile->szDb = -1;
37503 return SQLITE_OK;
37504}
37505
37506/*
37507** Delete the file located at zPath. If the dirSync argument is true,
37508** ensure the file-system modifications are synced to disk before
37509** returning.
37510*/
37511static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
37512 if( strcmp(zPath, "local-journal")==0 ){
37513 sqlite3KvvfsMethods.xDelete("local", "jrnl");
37514 }else
37515 if( strcmp(zPath, "session-journal")==0 ){
37516 sqlite3KvvfsMethods.xDelete("session", "jrnl");
37517 }
37518 return SQLITE_OK;
37519}
37520
37521/*
37522** Test for access permissions. Return true if the requested permission
37523** is available, or false otherwise.
37524*/
37525static int kvvfsAccess(
37526 sqlite3_vfs *pProtoVfs,
37527 const char *zPath,
37528 int flags,
37529 int *pResOut
37530){
37531 SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
37532 if( strcmp(zPath, "local-journal")==0 ){
37533 *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
37534 }else
37535 if( strcmp(zPath, "session-journal")==0 ){
37536 *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
37537 }else
37538 if( strcmp(zPath, "local")==0 ){
37539 *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
37540 }else
37541 if( strcmp(zPath, "session")==0 ){
37542 *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
37543 }else
37544 {
37545 *pResOut = 0;
37546 }
37547 SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
37548 return SQLITE_OK;
37549}
37550
37551/*
37552** Populate buffer zOut with the full canonical pathname corresponding
37553** to the pathname in zPath. zOut is guaranteed to point to a buffer
37554** of at least (INST_MAX_PATHNAME+1) bytes.
37555*/
37556static int kvvfsFullPathname(
37557 sqlite3_vfs *pVfs,
37558 const char *zPath,
37559 int nOut,
37560 char *zOut
37561){
37562 size_t nPath;
37563#ifdef SQLITE_OS_KV_ALWAYS_LOCAL
37564 zPath = "local";
37565#endif
37566 nPath = strlen(zPath);
37567 SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
37568 if( nOut<nPath+1 ) nPath = nOut - 1;
37569 memcpy(zOut, zPath, nPath);
37570 zOut[nPath] = 0;
37571 return SQLITE_OK;
37572}
37573
37574/*
37575** Open the dynamic library located at zPath and return a handle.
37576*/
37577static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
37578 return 0;
37579}
37580
37581/*
37582** Populate the buffer pointed to by zBufOut with nByte bytes of
37583** random data.
37584*/
37585static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
37586 memset(zBufOut, 0, nByte);
37587 return nByte;
37588}
37589
37590/*
37591** Sleep for nMicro microseconds. Return the number of microseconds
37592** actually slept.
37593*/
37594static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
37595 return SQLITE_OK;
37596}
37597
37598/*
37599** Return the current time as a Julian Day number in *pTimeOut.
37600*/
37601static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
37602 sqlite3_int64 i = 0;
37603 int rc;
37604 rc = kvvfsCurrentTimeInt64(0, &i);
37605 *pTimeOut = i/86400000.0;
37606 return rc;
37607}
37608#include <sys/time.h>
37609static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
37610 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
37611 struct timeval sNow;
37612 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
37613 *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
37614 return SQLITE_OK;
37615}
37616#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
37617
37618#if SQLITE_OS_KV
37619/*
37620** This routine is called initialize the KV-vfs as the default VFS.
37621*/
37622SQLITE_API int sqlite3_os_init(void){
37623 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
37624}
37625SQLITE_API int sqlite3_os_end(void){
37626 return SQLITE_OK;
37627}
37628#endif /* SQLITE_OS_KV */
37629
37630#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
37631SQLITE_PRIVATE int sqlite3KvvfsInit(void){
37632 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
37633}
37634#endif
37635
37636/************** End of os_kv.c ***********************************************/
37637/************** Begin file os_unix.c *****************************************/
37638/*
37639** 2004 May 22
37640**
37641** The author disclaims copyright to this source code. In place of
37642** a legal notice, here is a blessing:
37643**
37644** May you do good and not evil.
37645** May you find forgiveness for yourself and forgive others.
37646** May you share freely, never taking more than you give.
37647**
37648******************************************************************************
37649**
37650** This file contains the VFS implementation for unix-like operating systems
37651** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
37652**
37653** There are actually several different VFS implementations in this file.
37654** The differences are in the way that file locking is done. The default
37655** implementation uses Posix Advisory Locks. Alternative implementations
37656** use flock(), dot-files, various proprietary locking schemas, or simply
37657** skip locking all together.
37658**
37659** This source file is organized into divisions where the logic for various
37660** subfunctions is contained within the appropriate division. PLEASE
37661** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
37662** in the correct division and should be clearly labelled.
37663**
37664** The layout of divisions is as follows:
37665**
37666** * General-purpose declarations and utility functions.
37667** * Unique file ID logic used by VxWorks.
37668** * Various locking primitive implementations (all except proxy locking):
37669** + for Posix Advisory Locks
37670** + for no-op locks
37671** + for dot-file locks
37672** + for flock() locking
37673** + for named semaphore locks (VxWorks only)
37674** + for AFP filesystem locks (MacOSX only)
37675** * sqlite3_file methods not associated with locking.
37676** * Definitions of sqlite3_io_methods objects for all locking
37677** methods plus "finder" functions for each locking method.
37678** * sqlite3_vfs method implementations.
37679** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
37680** * Definitions of sqlite3_vfs objects for all locking methods
37681** plus implementations of sqlite3_os_init() and sqlite3_os_end().
37682*/
37683/* #include "sqliteInt.h" */
37684#if SQLITE_OS_UNIX /* This file is used on unix only */
37685
37686/*
37687** There are various methods for file locking used for concurrency
37688** control:
37689**
37690** 1. POSIX locking (the default),
37691** 2. No locking,
37692** 3. Dot-file locking,
37693** 4. flock() locking,
37694** 5. AFP locking (OSX only),
37695** 6. Named POSIX semaphores (VXWorks only),
37696** 7. proxy locking. (OSX only)
37697**
37698** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
37699** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
37700** selection of the appropriate locking style based on the filesystem
37701** where the database is located.
37702*/
37703#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
37704# if defined(__APPLE__)
37705# define SQLITE_ENABLE_LOCKING_STYLE 1
37706# else
37707# define SQLITE_ENABLE_LOCKING_STYLE 0
37708# endif
37709#endif
37710
37711/* Use pread() and pwrite() if they are available */
37712#if defined(__APPLE__) || defined(__linux__)
37713# define HAVE_PREAD 1
37714# define HAVE_PWRITE 1
37715#endif
37716#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
37717# undef USE_PREAD
37718# define USE_PREAD64 1
37719#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
37720# undef USE_PREAD64
37721# define USE_PREAD 1
37722#endif
37723
37724/*
37725** standard include files.
37726*/
37727#include <sys/types.h> /* amalgamator: keep */
37728#include <sys/stat.h> /* amalgamator: keep */
37729#include <fcntl.h>
37730#include <sys/ioctl.h>
37731#include <unistd.h> /* amalgamator: keep */
37732/* #include <time.h> */
37733#include <sys/time.h> /* amalgamator: keep */
37734#include <errno.h>
37735#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \
37736 && !defined(SQLITE_WASI)
37737# include <sys/mman.h>
37738#endif
37739
37740#if SQLITE_ENABLE_LOCKING_STYLE
37741/* # include <sys/ioctl.h> */
37742# include <sys/file.h>
37743# include <sys/param.h>
37744#endif /* SQLITE_ENABLE_LOCKING_STYLE */
37745
37746/*
37747** Try to determine if gethostuuid() is available based on standard
37748** macros. This might sometimes compute the wrong value for some
37749** obscure platforms. For those cases, simply compile with one of
37750** the following:
37751**
37752** -DHAVE_GETHOSTUUID=0
37753** -DHAVE_GETHOSTUUID=1
37754**
37755** None if this matters except when building on Apple products with
37756** -DSQLITE_ENABLE_LOCKING_STYLE.
37757*/
37758#ifndef HAVE_GETHOSTUUID
37759# define HAVE_GETHOSTUUID 0
37760# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
37761 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
37762# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
37763 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\
37764 && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0))
37765# undef HAVE_GETHOSTUUID
37766# define HAVE_GETHOSTUUID 1
37767# else
37768# warning "gethostuuid() is disabled."
37769# endif
37770# endif
37771#endif
37772
37773
37774#if OS_VXWORKS
37775/* # include <sys/ioctl.h> */
37776# include <semaphore.h>
37777# include <limits.h>
37778#endif /* OS_VXWORKS */
37779
37780#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
37781# include <sys/mount.h>
37782#endif
37783
37784#ifdef HAVE_UTIME
37785# include <utime.h>
37786#endif
37787
37788/*
37789** Allowed values of unixFile.fsFlags
37790*/
37791#define SQLITE_FSFLAGS_IS_MSDOS 0x1
37792
37793/*
37794** If we are to be thread-safe, include the pthreads header.
37795*/
37796#if SQLITE_THREADSAFE
37797/* # include <pthread.h> */
37798#endif
37799
37800/*
37801** Default permissions when creating a new file
37802*/
37803#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
37804# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
37805#endif
37806
37807/*
37808** Default permissions when creating auto proxy dir
37809*/
37810#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
37811# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
37812#endif
37813
37814/*
37815** Maximum supported path-length.
37816*/
37817#define MAX_PATHNAME 512
37818
37819/*
37820** Maximum supported symbolic links
37821*/
37822#define SQLITE_MAX_SYMLINKS 100
37823
37824/*
37825** Remove and stub certain info for WASI (WebAssembly System
37826** Interface) builds.
37827*/
37828#ifdef SQLITE_WASI
37829# undef HAVE_FCHMOD
37830# undef HAVE_FCHOWN
37831# undef HAVE_MREMAP
37832# define HAVE_MREMAP 0
37833# ifndef SQLITE_DEFAULT_UNIX_VFS
37834# define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
37835 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
37836# endif
37837# ifndef F_RDLCK
37838# define F_RDLCK 0
37839# define F_WRLCK 1
37840# define F_UNLCK 2
37841# if __LONG_MAX == 0x7fffffffL
37842# define F_GETLK 12
37843# define F_SETLK 13
37844# define F_SETLKW 14
37845# else
37846# define F_GETLK 5
37847# define F_SETLK 6
37848# define F_SETLKW 7
37849# endif
37850# endif
37851#else /* !SQLITE_WASI */
37852# ifndef HAVE_FCHMOD
37853# define HAVE_FCHMOD
37854# endif
37855#endif /* SQLITE_WASI */
37856
37857#ifdef SQLITE_WASI
37858# define osGetpid(X) (pid_t)1
37859#else
37860/* Always cast the getpid() return type for compatibility with
37861** kernel modules in VxWorks. */
37862# define osGetpid(X) (pid_t)getpid()
37863#endif
37864
37865/*
37866** Only set the lastErrno if the error code is a real error and not
37867** a normal expected return code of SQLITE_BUSY or SQLITE_OK
37868*/
37869#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
37870
37871/* Forward references */
37872typedef struct unixShm unixShm; /* Connection shared memory */
37873typedef struct unixShmNode unixShmNode; /* Shared memory instance */
37874typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
37875typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
37876
37877/*
37878** Sometimes, after a file handle is closed by SQLite, the file descriptor
37879** cannot be closed immediately. In these cases, instances of the following
37880** structure are used to store the file descriptor while waiting for an
37881** opportunity to either close or reuse it.
37882*/
37883struct UnixUnusedFd {
37884 int fd; /* File descriptor to close */
37885 int flags; /* Flags this file descriptor was opened with */
37886 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
37887};
37888
37889/*
37890** The unixFile structure is subclass of sqlite3_file specific to the unix
37891** VFS implementations.
37892*/
37893typedef struct unixFile unixFile;
37894struct unixFile {
37895 sqlite3_io_methods const *pMethod; /* Always the first entry */
37896 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
37897 unixInodeInfo *pInode; /* Info about locks on this inode */
37898 int h; /* The file descriptor */
37899 unsigned char eFileLock; /* The type of lock held on this fd */
37900 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
37901 int lastErrno; /* The unix errno from last I/O error */
37902 void *lockingContext; /* Locking style specific state */
37903 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
37904 const char *zPath; /* Name of the file */
37905 unixShm *pShm; /* Shared memory segment information */
37906 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
37907#if SQLITE_MAX_MMAP_SIZE>0
37908 int nFetchOut; /* Number of outstanding xFetch refs */
37909 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
37910 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
37911 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
37912 void *pMapRegion; /* Memory mapped region */
37913#endif
37914 int sectorSize; /* Device sector size */
37915 int deviceCharacteristics; /* Precomputed device characteristics */
37916#if SQLITE_ENABLE_LOCKING_STYLE
37917 int openFlags; /* The flags specified at open() */
37918#endif
37919#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
37920 unsigned fsFlags; /* cached details from statfs() */
37921#endif
37922#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
37923 unsigned iBusyTimeout; /* Wait this many millisec on locks */
37924#endif
37925#if OS_VXWORKS
37926 struct vxworksFileId *pId; /* Unique file ID */
37927#endif
37928#ifdef SQLITE_DEBUG
37929 /* The next group of variables are used to track whether or not the
37930 ** transaction counter in bytes 24-27 of database files are updated
37931 ** whenever any part of the database changes. An assertion fault will
37932 ** occur if a file is updated without also updating the transaction
37933 ** counter. This test is made to avoid new problems similar to the
37934 ** one described by ticket #3584.
37935 */
37936 unsigned char transCntrChng; /* True if the transaction counter changed */
37937 unsigned char dbUpdate; /* True if any part of database file changed */
37938 unsigned char inNormalWrite; /* True if in a normal write operation */
37939
37940#endif
37941
37942#ifdef SQLITE_TEST
37943 /* In test mode, increase the size of this structure a bit so that
37944 ** it is larger than the struct CrashFile defined in test6.c.
37945 */
37946 char aPadding[32];
37947#endif
37948};
37949
37950/* This variable holds the process id (pid) from when the xRandomness()
37951** method was called. If xOpen() is called from a different process id,
37952** indicating that a fork() has occurred, the PRNG will be reset.
37953*/
37954static pid_t randomnessPid = 0;
37955
37956/*
37957** Allowed values for the unixFile.ctrlFlags bitmask:
37958*/
37959#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
37960#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
37961#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
37962#ifndef SQLITE_DISABLE_DIRSYNC
37963# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
37964#else
37965# define UNIXFILE_DIRSYNC 0x00
37966#endif
37967#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
37968#define UNIXFILE_DELETE 0x20 /* Delete on close */
37969#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
37970#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
37971
37972/*
37973** Include code that is common to all os_*.c files
37974*/
37975/* #include "os_common.h" */
37976
37977/*
37978** Define various macros that are missing from some systems.
37979*/
37980#ifndef O_LARGEFILE
37981# define O_LARGEFILE 0
37982#endif
37983#ifdef SQLITE_DISABLE_LFS
37984# undef O_LARGEFILE
37985# define O_LARGEFILE 0
37986#endif
37987#ifndef O_NOFOLLOW
37988# define O_NOFOLLOW 0
37989#endif
37990#ifndef O_BINARY
37991# define O_BINARY 0
37992#endif
37993
37994/*
37995** The threadid macro resolves to the thread-id or to 0. Used for
37996** testing and debugging only.
37997*/
37998#if SQLITE_THREADSAFE
37999#define threadid pthread_self()
38000#else
38001#define threadid 0
38002#endif
38003
38004/*
38005** HAVE_MREMAP defaults to true on Linux and false everywhere else.
38006*/
38007#if !defined(HAVE_MREMAP)
38008# if defined(__linux__) && defined(_GNU_SOURCE)
38009# define HAVE_MREMAP 1
38010# else
38011# define HAVE_MREMAP 0
38012# endif
38013#endif
38014
38015/*
38016** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
38017** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
38018*/
38019#ifdef __ANDROID__
38020# define lseek lseek64
38021#endif
38022
38023#ifdef __linux__
38024/*
38025** Linux-specific IOCTL magic numbers used for controlling F2FS
38026*/
38027#define F2FS_IOCTL_MAGIC 0xf5
38028#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
38029#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
38030#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
38031#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
38032#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32)
38033#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
38034#endif /* __linux__ */
38035
38036
38037/*
38038** Different Unix systems declare open() in different ways. Same use
38039** open(const char*,int,mode_t). Others use open(const char*,int,...).
38040** The difference is important when using a pointer to the function.
38041**
38042** The safest way to deal with the problem is to always use this wrapper
38043** which always has the same well-defined interface.
38044*/
38045static int posixOpen(const char *zFile, int flags, int mode){
38046 return open(file: zFile, oflag: flags, mode);
38047}
38048
38049/* Forward reference */
38050static int openDirectory(const char*, int*);
38051static int unixGetpagesize(void);
38052
38053/*
38054** Many system calls are accessed through pointer-to-functions so that
38055** they may be overridden at runtime to facilitate fault injection during
38056** testing and sandboxing. The following array holds the names and pointers
38057** to all overrideable system calls.
38058*/
38059static struct unix_syscall {
38060 const char *zName; /* Name of the system call */
38061 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
38062 sqlite3_syscall_ptr pDefault; /* Default value */
38063} aSyscall[] = {
38064 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
38065#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
38066
38067 { "close", (sqlite3_syscall_ptr)close, 0 },
38068#define osClose ((int(*)(int))aSyscall[1].pCurrent)
38069
38070 { "access", (sqlite3_syscall_ptr)access, 0 },
38071#define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
38072
38073 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
38074#define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
38075
38076 { "stat", (sqlite3_syscall_ptr)stat, 0 },
38077#define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
38078
38079/*
38080** The DJGPP compiler environment looks mostly like Unix, but it
38081** lacks the fcntl() system call. So redefine fcntl() to be something
38082** that always succeeds. This means that locking does not occur under
38083** DJGPP. But it is DOS - what did you expect?
38084*/
38085#ifdef __DJGPP__
38086 { "fstat", 0, 0 },
38087#define osFstat(a,b,c) 0
38088#else
38089 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
38090#define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
38091#endif
38092
38093 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
38094#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
38095
38096 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
38097#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
38098
38099 { "read", (sqlite3_syscall_ptr)read, 0 },
38100#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
38101
38102#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
38103 { "pread", (sqlite3_syscall_ptr)pread, 0 },
38104#else
38105 { "pread", (sqlite3_syscall_ptr)0, 0 },
38106#endif
38107#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
38108
38109#if defined(USE_PREAD64)
38110 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
38111#else
38112 { "pread64", (sqlite3_syscall_ptr)0, 0 },
38113#endif
38114#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
38115
38116 { "write", (sqlite3_syscall_ptr)write, 0 },
38117#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
38118
38119#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
38120 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
38121#else
38122 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
38123#endif
38124#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
38125 aSyscall[12].pCurrent)
38126
38127#if defined(USE_PREAD64)
38128 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
38129#else
38130 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
38131#endif
38132#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
38133 aSyscall[13].pCurrent)
38134
38135#if defined(HAVE_FCHMOD)
38136 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
38137#else
38138 { "fchmod", (sqlite3_syscall_ptr)0, 0 },
38139#endif
38140#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
38141
38142#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
38143 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
38144#else
38145 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
38146#endif
38147#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
38148
38149 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
38150#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
38151
38152 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
38153#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
38154
38155 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
38156#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
38157
38158 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
38159#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
38160
38161#if defined(HAVE_FCHOWN)
38162 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
38163#else
38164 { "fchown", (sqlite3_syscall_ptr)0, 0 },
38165#endif
38166#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
38167
38168#if defined(HAVE_FCHOWN)
38169 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
38170#else
38171 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
38172#endif
38173#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
38174
38175#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \
38176 && !defined(SQLITE_WASI)
38177 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
38178#else
38179 { "mmap", (sqlite3_syscall_ptr)0, 0 },
38180#endif
38181#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
38182
38183#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \
38184 && !defined(SQLITE_WASI)
38185 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
38186#else
38187 { "munmap", (sqlite3_syscall_ptr)0, 0 },
38188#endif
38189#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
38190
38191#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
38192 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
38193#else
38194 { "mremap", (sqlite3_syscall_ptr)0, 0 },
38195#endif
38196#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
38197
38198#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
38199 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
38200#else
38201 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
38202#endif
38203#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
38204
38205#if defined(HAVE_READLINK)
38206 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
38207#else
38208 { "readlink", (sqlite3_syscall_ptr)0, 0 },
38209#endif
38210#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
38211
38212#if defined(HAVE_LSTAT)
38213 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
38214#else
38215 { "lstat", (sqlite3_syscall_ptr)0, 0 },
38216#endif
38217#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
38218
38219#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
38220# ifdef __ANDROID__
38221 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
38222#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
38223# else
38224 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
38225#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
38226# endif
38227#else
38228 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
38229#endif
38230
38231}; /* End of the overrideable system calls */
38232
38233
38234/*
38235** On some systems, calls to fchown() will trigger a message in a security
38236** log if they come from non-root processes. So avoid calling fchown() if
38237** we are not running as root.
38238*/
38239static int robustFchown(int fd, uid_t uid, gid_t gid){
38240#if defined(HAVE_FCHOWN)
38241 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
38242#else
38243 return 0;
38244#endif
38245}
38246
38247/*
38248** This is the xSetSystemCall() method of sqlite3_vfs for all of the
38249** "unix" VFSes. Return SQLITE_OK upon successfully updating the
38250** system call pointer, or SQLITE_NOTFOUND if there is no configurable
38251** system call named zName.
38252*/
38253static int unixSetSystemCall(
38254 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
38255 const char *zName, /* Name of system call to override */
38256 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
38257){
38258 unsigned int i;
38259 int rc = SQLITE_NOTFOUND;
38260
38261 UNUSED_PARAMETER(pNotUsed);
38262 if( zName==0 ){
38263 /* If no zName is given, restore all system calls to their default
38264 ** settings and return NULL
38265 */
38266 rc = SQLITE_OK;
38267 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
38268 if( aSyscall[i].pDefault ){
38269 aSyscall[i].pCurrent = aSyscall[i].pDefault;
38270 }
38271 }
38272 }else{
38273 /* If zName is specified, operate on only the one system call
38274 ** specified.
38275 */
38276 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
38277 if( strcmp(s1: zName, s2: aSyscall[i].zName)==0 ){
38278 if( aSyscall[i].pDefault==0 ){
38279 aSyscall[i].pDefault = aSyscall[i].pCurrent;
38280 }
38281 rc = SQLITE_OK;
38282 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
38283 aSyscall[i].pCurrent = pNewFunc;
38284 break;
38285 }
38286 }
38287 }
38288 return rc;
38289}
38290
38291/*
38292** Return the value of a system call. Return NULL if zName is not a
38293** recognized system call name. NULL is also returned if the system call
38294** is currently undefined.
38295*/
38296static sqlite3_syscall_ptr unixGetSystemCall(
38297 sqlite3_vfs *pNotUsed,
38298 const char *zName
38299){
38300 unsigned int i;
38301
38302 UNUSED_PARAMETER(pNotUsed);
38303 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
38304 if( strcmp(s1: zName, s2: aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
38305 }
38306 return 0;
38307}
38308
38309/*
38310** Return the name of the first system call after zName. If zName==NULL
38311** then return the name of the first system call. Return NULL if zName
38312** is the last system call or if zName is not the name of a valid
38313** system call.
38314*/
38315static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
38316 int i = -1;
38317
38318 UNUSED_PARAMETER(p);
38319 if( zName ){
38320 for(i=0; i<ArraySize(aSyscall)-1; i++){
38321 if( strcmp(s1: zName, s2: aSyscall[i].zName)==0 ) break;
38322 }
38323 }
38324 for(i++; i<ArraySize(aSyscall); i++){
38325 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
38326 }
38327 return 0;
38328}
38329
38330/*
38331** Do not accept any file descriptor less than this value, in order to avoid
38332** opening database file using file descriptors that are commonly used for
38333** standard input, output, and error.
38334*/
38335#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
38336# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
38337#endif
38338
38339/*
38340** Invoke open(). Do so multiple times, until it either succeeds or
38341** fails for some reason other than EINTR.
38342**
38343** If the file creation mode "m" is 0 then set it to the default for
38344** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
38345** 0644) as modified by the system umask. If m is not 0, then
38346** make the file creation mode be exactly m ignoring the umask.
38347**
38348** The m parameter will be non-zero only when creating -wal, -journal,
38349** and -shm files. We want those files to have *exactly* the same
38350** permissions as their original database, unadulterated by the umask.
38351** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
38352** transaction crashes and leaves behind hot journals, then any
38353** process that is able to write to the database will also be able to
38354** recover the hot journals.
38355*/
38356static int robust_open(const char *z, int f, mode_t m){
38357 int fd;
38358 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
38359 while(1){
38360#if defined(O_CLOEXEC)
38361 fd = osOpen(z,f|O_CLOEXEC,m2);
38362#else
38363 fd = osOpen(z,f,m2);
38364#endif
38365 if( fd<0 ){
38366 if( errno==EINTR ) continue;
38367 break;
38368 }
38369 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
38370 if( (f & (O_EXCL|O_CREAT))==(O_EXCL|O_CREAT) ){
38371 (void)osUnlink(z);
38372 }
38373 osClose(fd);
38374 sqlite3_log(SQLITE_WARNING,
38375 zFormat: "attempt to open \"%s\" as file descriptor %d", z, fd);
38376 fd = -1;
38377 if( osOpen("/dev/null", O_RDONLY, m)<0 ) break;
38378 }
38379 if( fd>=0 ){
38380 if( m!=0 ){
38381 struct stat statbuf;
38382 if( osFstat(fd, &statbuf)==0
38383 && statbuf.st_size==0
38384 && (statbuf.st_mode&0777)!=m
38385 ){
38386 osFchmod(fd, m);
38387 }
38388 }
38389#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
38390 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
38391#endif
38392 }
38393 return fd;
38394}
38395
38396/*
38397** Helper functions to obtain and relinquish the global mutex. The
38398** global mutex is used to protect the unixInodeInfo and
38399** vxworksFileId objects used by this file, all of which may be
38400** shared by multiple threads.
38401**
38402** Function unixMutexHeld() is used to assert() that the global mutex
38403** is held when required. This function is only used as part of assert()
38404** statements. e.g.
38405**
38406** unixEnterMutex()
38407** assert( unixMutexHeld() );
38408** unixEnterLeave()
38409**
38410** To prevent deadlock, the global unixBigLock must must be acquired
38411** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
38412** OK to get the pLockMutex without holding unixBigLock first, but if
38413** that happens, the unixBigLock mutex must not be acquired until after
38414** pLockMutex is released.
38415**
38416** OK: enter(unixBigLock), enter(pLockInfo)
38417** OK: enter(unixBigLock)
38418** OK: enter(pLockInfo)
38419** ERROR: enter(pLockInfo), enter(unixBigLock)
38420*/
38421static sqlite3_mutex *unixBigLock = 0;
38422static void unixEnterMutex(void){
38423 assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */
38424 sqlite3_mutex_enter(p: unixBigLock);
38425}
38426static void unixLeaveMutex(void){
38427 assert( sqlite3_mutex_held(unixBigLock) );
38428 sqlite3_mutex_leave(p: unixBigLock);
38429}
38430#ifdef SQLITE_DEBUG
38431static int unixMutexHeld(void) {
38432 return sqlite3_mutex_held(unixBigLock);
38433}
38434#endif
38435
38436
38437#ifdef SQLITE_HAVE_OS_TRACE
38438/*
38439** Helper function for printing out trace information from debugging
38440** binaries. This returns the string representation of the supplied
38441** integer lock-type.
38442*/
38443static const char *azFileLock(int eFileLock){
38444 switch( eFileLock ){
38445 case NO_LOCK: return "NONE";
38446 case SHARED_LOCK: return "SHARED";
38447 case RESERVED_LOCK: return "RESERVED";
38448 case PENDING_LOCK: return "PENDING";
38449 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
38450 }
38451 return "ERROR";
38452}
38453#endif
38454
38455#ifdef SQLITE_LOCK_TRACE
38456/*
38457** Print out information about all locking operations.
38458**
38459** This routine is used for troubleshooting locks on multithreaded
38460** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
38461** command-line option on the compiler. This code is normally
38462** turned off.
38463*/
38464static int lockTrace(int fd, int op, struct flock *p){
38465 char *zOpName, *zType;
38466 int s;
38467 int savedErrno;
38468 if( op==F_GETLK ){
38469 zOpName = "GETLK";
38470 }else if( op==F_SETLK ){
38471 zOpName = "SETLK";
38472 }else{
38473 s = osFcntl(fd, op, p);
38474 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
38475 return s;
38476 }
38477 if( p->l_type==F_RDLCK ){
38478 zType = "RDLCK";
38479 }else if( p->l_type==F_WRLCK ){
38480 zType = "WRLCK";
38481 }else if( p->l_type==F_UNLCK ){
38482 zType = "UNLCK";
38483 }else{
38484 assert( 0 );
38485 }
38486 assert( p->l_whence==SEEK_SET );
38487 s = osFcntl(fd, op, p);
38488 savedErrno = errno;
38489 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
38490 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
38491 (int)p->l_pid, s);
38492 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
38493 struct flock l2;
38494 l2 = *p;
38495 osFcntl(fd, F_GETLK, &l2);
38496 if( l2.l_type==F_RDLCK ){
38497 zType = "RDLCK";
38498 }else if( l2.l_type==F_WRLCK ){
38499 zType = "WRLCK";
38500 }else if( l2.l_type==F_UNLCK ){
38501 zType = "UNLCK";
38502 }else{
38503 assert( 0 );
38504 }
38505 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
38506 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
38507 }
38508 errno = savedErrno;
38509 return s;
38510}
38511#undef osFcntl
38512#define osFcntl lockTrace
38513#endif /* SQLITE_LOCK_TRACE */
38514
38515/*
38516** Retry ftruncate() calls that fail due to EINTR
38517**
38518** All calls to ftruncate() within this file should be made through
38519** this wrapper. On the Android platform, bypassing the logic below
38520** could lead to a corrupt database.
38521*/
38522static int robust_ftruncate(int h, sqlite3_int64 sz){
38523 int rc;
38524#ifdef __ANDROID__
38525 /* On Android, ftruncate() always uses 32-bit offsets, even if
38526 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
38527 ** truncate a file to any size larger than 2GiB. Silently ignore any
38528 ** such attempts. */
38529 if( sz>(sqlite3_int64)0x7FFFFFFF ){
38530 rc = SQLITE_OK;
38531 }else
38532#endif
38533 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
38534 return rc;
38535}
38536
38537/*
38538** This routine translates a standard POSIX errno code into something
38539** useful to the clients of the sqlite3 functions. Specifically, it is
38540** intended to translate a variety of "try again" errors into SQLITE_BUSY
38541** and a variety of "please close the file descriptor NOW" errors into
38542** SQLITE_IOERR
38543**
38544** Errors during initialization of locks, or file system support for locks,
38545** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
38546*/
38547static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
38548 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
38549 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
38550 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
38551 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
38552 switch (posixError) {
38553 case EACCES:
38554 case EAGAIN:
38555 case ETIMEDOUT:
38556 case EBUSY:
38557 case EINTR:
38558 case ENOLCK:
38559 /* random NFS retry error, unless during file system support
38560 * introspection, in which it actually means what it says */
38561 return SQLITE_BUSY;
38562
38563 case EPERM:
38564 return SQLITE_PERM;
38565
38566 default:
38567 return sqliteIOErr;
38568 }
38569}
38570
38571
38572/******************************************************************************
38573****************** Begin Unique File ID Utility Used By VxWorks ***************
38574**
38575** On most versions of unix, we can get a unique ID for a file by concatenating
38576** the device number and the inode number. But this does not work on VxWorks.
38577** On VxWorks, a unique file id must be based on the canonical filename.
38578**
38579** A pointer to an instance of the following structure can be used as a
38580** unique file ID in VxWorks. Each instance of this structure contains
38581** a copy of the canonical filename. There is also a reference count.
38582** The structure is reclaimed when the number of pointers to it drops to
38583** zero.
38584**
38585** There are never very many files open at one time and lookups are not
38586** a performance-critical path, so it is sufficient to put these
38587** structures on a linked list.
38588*/
38589struct vxworksFileId {
38590 struct vxworksFileId *pNext; /* Next in a list of them all */
38591 int nRef; /* Number of references to this one */
38592 int nName; /* Length of the zCanonicalName[] string */
38593 char *zCanonicalName; /* Canonical filename */
38594};
38595
38596#if OS_VXWORKS
38597/*
38598** All unique filenames are held on a linked list headed by this
38599** variable:
38600*/
38601static struct vxworksFileId *vxworksFileList = 0;
38602
38603/*
38604** Simplify a filename into its canonical form
38605** by making the following changes:
38606**
38607** * removing any trailing and duplicate /
38608** * convert /./ into just /
38609** * convert /A/../ where A is any simple name into just /
38610**
38611** Changes are made in-place. Return the new name length.
38612**
38613** The original filename is in z[0..n-1]. Return the number of
38614** characters in the simplified name.
38615*/
38616static int vxworksSimplifyName(char *z, int n){
38617 int i, j;
38618 while( n>1 && z[n-1]=='/' ){ n--; }
38619 for(i=j=0; i<n; i++){
38620 if( z[i]=='/' ){
38621 if( z[i+1]=='/' ) continue;
38622 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
38623 i += 1;
38624 continue;
38625 }
38626 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
38627 while( j>0 && z[j-1]!='/' ){ j--; }
38628 if( j>0 ){ j--; }
38629 i += 2;
38630 continue;
38631 }
38632 }
38633 z[j++] = z[i];
38634 }
38635 z[j] = 0;
38636 return j;
38637}
38638
38639/*
38640** Find a unique file ID for the given absolute pathname. Return
38641** a pointer to the vxworksFileId object. This pointer is the unique
38642** file ID.
38643**
38644** The nRef field of the vxworksFileId object is incremented before
38645** the object is returned. A new vxworksFileId object is created
38646** and added to the global list if necessary.
38647**
38648** If a memory allocation error occurs, return NULL.
38649*/
38650static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
38651 struct vxworksFileId *pNew; /* search key and new file ID */
38652 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
38653 int n; /* Length of zAbsoluteName string */
38654
38655 assert( zAbsoluteName[0]=='/' );
38656 n = (int)strlen(zAbsoluteName);
38657 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
38658 if( pNew==0 ) return 0;
38659 pNew->zCanonicalName = (char*)&pNew[1];
38660 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
38661 n = vxworksSimplifyName(pNew->zCanonicalName, n);
38662
38663 /* Search for an existing entry that matching the canonical name.
38664 ** If found, increment the reference count and return a pointer to
38665 ** the existing file ID.
38666 */
38667 unixEnterMutex();
38668 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
38669 if( pCandidate->nName==n
38670 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
38671 ){
38672 sqlite3_free(pNew);
38673 pCandidate->nRef++;
38674 unixLeaveMutex();
38675 return pCandidate;
38676 }
38677 }
38678
38679 /* No match was found. We will make a new file ID */
38680 pNew->nRef = 1;
38681 pNew->nName = n;
38682 pNew->pNext = vxworksFileList;
38683 vxworksFileList = pNew;
38684 unixLeaveMutex();
38685 return pNew;
38686}
38687
38688/*
38689** Decrement the reference count on a vxworksFileId object. Free
38690** the object when the reference count reaches zero.
38691*/
38692static void vxworksReleaseFileId(struct vxworksFileId *pId){
38693 unixEnterMutex();
38694 assert( pId->nRef>0 );
38695 pId->nRef--;
38696 if( pId->nRef==0 ){
38697 struct vxworksFileId **pp;
38698 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
38699 assert( *pp==pId );
38700 *pp = pId->pNext;
38701 sqlite3_free(pId);
38702 }
38703 unixLeaveMutex();
38704}
38705#endif /* OS_VXWORKS */
38706/*************** End of Unique File ID Utility Used By VxWorks ****************
38707******************************************************************************/
38708
38709
38710/******************************************************************************
38711*************************** Posix Advisory Locking ****************************
38712**
38713** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
38714** section 6.5.2.2 lines 483 through 490 specify that when a process
38715** sets or clears a lock, that operation overrides any prior locks set
38716** by the same process. It does not explicitly say so, but this implies
38717** that it overrides locks set by the same process using a different
38718** file descriptor. Consider this test case:
38719**
38720** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
38721** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
38722**
38723** Suppose ./file1 and ./file2 are really the same file (because
38724** one is a hard or symbolic link to the other) then if you set
38725** an exclusive lock on fd1, then try to get an exclusive lock
38726** on fd2, it works. I would have expected the second lock to
38727** fail since there was already a lock on the file due to fd1.
38728** But not so. Since both locks came from the same process, the
38729** second overrides the first, even though they were on different
38730** file descriptors opened on different file names.
38731**
38732** This means that we cannot use POSIX locks to synchronize file access
38733** among competing threads of the same process. POSIX locks will work fine
38734** to synchronize access for threads in separate processes, but not
38735** threads within the same process.
38736**
38737** To work around the problem, SQLite has to manage file locks internally
38738** on its own. Whenever a new database is opened, we have to find the
38739** specific inode of the database file (the inode is determined by the
38740** st_dev and st_ino fields of the stat structure that fstat() fills in)
38741** and check for locks already existing on that inode. When locks are
38742** created or removed, we have to look at our own internal record of the
38743** locks to see if another thread has previously set a lock on that same
38744** inode.
38745**
38746** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
38747** For VxWorks, we have to use the alternative unique ID system based on
38748** canonical filename and implemented in the previous division.)
38749**
38750** The sqlite3_file structure for POSIX is no longer just an integer file
38751** descriptor. It is now a structure that holds the integer file
38752** descriptor and a pointer to a structure that describes the internal
38753** locks on the corresponding inode. There is one locking structure
38754** per inode, so if the same inode is opened twice, both unixFile structures
38755** point to the same locking structure. The locking structure keeps
38756** a reference count (so we will know when to delete it) and a "cnt"
38757** field that tells us its internal lock status. cnt==0 means the
38758** file is unlocked. cnt==-1 means the file has an exclusive lock.
38759** cnt>0 means there are cnt shared locks on the file.
38760**
38761** Any attempt to lock or unlock a file first checks the locking
38762** structure. The fcntl() system call is only invoked to set a
38763** POSIX lock if the internal lock structure transitions between
38764** a locked and an unlocked state.
38765**
38766** But wait: there are yet more problems with POSIX advisory locks.
38767**
38768** If you close a file descriptor that points to a file that has locks,
38769** all locks on that file that are owned by the current process are
38770** released. To work around this problem, each unixInodeInfo object
38771** maintains a count of the number of pending locks on the inode.
38772** When an attempt is made to close an unixFile, if there are
38773** other unixFile open on the same inode that are holding locks, the call
38774** to close() the file descriptor is deferred until all of the locks clear.
38775** The unixInodeInfo structure keeps a list of file descriptors that need to
38776** be closed and that list is walked (and cleared) when the last lock
38777** clears.
38778**
38779** Yet another problem: LinuxThreads do not play well with posix locks.
38780**
38781** Many older versions of linux use the LinuxThreads library which is
38782** not posix compliant. Under LinuxThreads, a lock created by thread
38783** A cannot be modified or overridden by a different thread B.
38784** Only thread A can modify the lock. Locking behavior is correct
38785** if the application uses the newer Native Posix Thread Library (NPTL)
38786** on linux - with NPTL a lock created by thread A can override locks
38787** in thread B. But there is no way to know at compile-time which
38788** threading library is being used. So there is no way to know at
38789** compile-time whether or not thread A can override locks on thread B.
38790** One has to do a run-time check to discover the behavior of the
38791** current process.
38792**
38793** SQLite used to support LinuxThreads. But support for LinuxThreads
38794** was dropped beginning with version 3.7.0. SQLite will still work with
38795** LinuxThreads provided that (1) there is no more than one connection
38796** per database file in the same process and (2) database connections
38797** do not move across threads.
38798*/
38799
38800/*
38801** An instance of the following structure serves as the key used
38802** to locate a particular unixInodeInfo object.
38803*/
38804struct unixFileId {
38805 dev_t dev; /* Device number */
38806#if OS_VXWORKS
38807 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
38808#else
38809 /* We are told that some versions of Android contain a bug that
38810 ** sizes ino_t at only 32-bits instead of 64-bits. (See
38811 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
38812 ** To work around this, always allocate 64-bits for the inode number.
38813 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
38814 ** but that should not be a big deal. */
38815 /* WAS: ino_t ino; */
38816 u64 ino; /* Inode number */
38817#endif
38818};
38819
38820/*
38821** An instance of the following structure is allocated for each open
38822** inode.
38823**
38824** A single inode can have multiple file descriptors, so each unixFile
38825** structure contains a pointer to an instance of this object and this
38826** object keeps a count of the number of unixFile pointing to it.
38827**
38828** Mutex rules:
38829**
38830** (1) Only the pLockMutex mutex must be held in order to read or write
38831** any of the locking fields:
38832** nShared, nLock, eFileLock, bProcessLock, pUnused
38833**
38834** (2) When nRef>0, then the following fields are unchanging and can
38835** be read (but not written) without holding any mutex:
38836** fileId, pLockMutex
38837**
38838** (3) With the exceptions above, all the fields may only be read
38839** or written while holding the global unixBigLock mutex.
38840**
38841** Deadlock prevention: The global unixBigLock mutex may not
38842** be acquired while holding the pLockMutex mutex. If both unixBigLock
38843** and pLockMutex are needed, then unixBigLock must be acquired first.
38844*/
38845struct unixInodeInfo {
38846 struct unixFileId fileId; /* The lookup key */
38847 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
38848 int nShared; /* Number of SHARED locks held */
38849 int nLock; /* Number of outstanding file locks */
38850 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
38851 unsigned char bProcessLock; /* An exclusive process lock is held */
38852 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
38853 int nRef; /* Number of pointers to this structure */
38854 unixShmNode *pShmNode; /* Shared memory associated with this inode */
38855 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
38856 unixInodeInfo *pPrev; /* .... doubly linked */
38857#if SQLITE_ENABLE_LOCKING_STYLE
38858 unsigned long long sharedByte; /* for AFP simulated shared lock */
38859#endif
38860#if OS_VXWORKS
38861 sem_t *pSem; /* Named POSIX semaphore */
38862 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
38863#endif
38864};
38865
38866/*
38867** A lists of all unixInodeInfo objects.
38868**
38869** Must hold unixBigLock in order to read or write this variable.
38870*/
38871static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
38872
38873#ifdef SQLITE_DEBUG
38874/*
38875** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
38876** This routine is used only within assert() to help verify correct mutex
38877** usage.
38878*/
38879int unixFileMutexHeld(unixFile *pFile){
38880 assert( pFile->pInode );
38881 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
38882}
38883int unixFileMutexNotheld(unixFile *pFile){
38884 assert( pFile->pInode );
38885 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
38886}
38887#endif
38888
38889/*
38890**
38891** This function - unixLogErrorAtLine(), is only ever called via the macro
38892** unixLogError().
38893**
38894** It is invoked after an error occurs in an OS function and errno has been
38895** set. It logs a message using sqlite3_log() containing the current value of
38896** errno and, if possible, the human-readable equivalent from strerror() or
38897** strerror_r().
38898**
38899** The first argument passed to the macro should be the error code that
38900** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
38901** The two subsequent arguments should be the name of the OS function that
38902** failed (e.g. "unlink", "open") and the associated file-system path,
38903** if any.
38904*/
38905#define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
38906static int unixLogErrorAtLine(
38907 int errcode, /* SQLite error code */
38908 const char *zFunc, /* Name of OS function that failed */
38909 const char *zPath, /* File path associated with error */
38910 int iLine /* Source line number where error occurred */
38911){
38912 char *zErr; /* Message from strerror() or equivalent */
38913 int iErrno = errno; /* Saved syscall error number */
38914
38915 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
38916 ** the strerror() function to obtain the human-readable error message
38917 ** equivalent to errno. Otherwise, use strerror_r().
38918 */
38919#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
38920 char aErr[80];
38921 memset(aErr, 0, sizeof(aErr));
38922 zErr = aErr;
38923
38924 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
38925 ** assume that the system provides the GNU version of strerror_r() that
38926 ** returns a pointer to a buffer containing the error message. That pointer
38927 ** may point to aErr[], or it may point to some static storage somewhere.
38928 ** Otherwise, assume that the system provides the POSIX version of
38929 ** strerror_r(), which always writes an error message into aErr[].
38930 **
38931 ** If the code incorrectly assumes that it is the POSIX version that is
38932 ** available, the error message will often be an empty string. Not a
38933 ** huge problem. Incorrectly concluding that the GNU version is available
38934 ** could lead to a segfault though.
38935 */
38936#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
38937 zErr =
38938# endif
38939 strerror_r(iErrno, aErr, sizeof(aErr)-1);
38940
38941#elif SQLITE_THREADSAFE
38942 /* This is a threadsafe build, but strerror_r() is not available. */
38943 zErr = "";
38944#else
38945 /* Non-threadsafe build, use strerror(). */
38946 zErr = strerror(iErrno);
38947#endif
38948
38949 if( zPath==0 ) zPath = "";
38950 sqlite3_log(iErrCode: errcode,
38951 zFormat: "os_unix.c:%d: (%d) %s(%s) - %s",
38952 iLine, iErrno, zFunc, zPath, zErr
38953 );
38954
38955 return errcode;
38956}
38957
38958/*
38959** Close a file descriptor.
38960**
38961** We assume that close() almost always works, since it is only in a
38962** very sick application or on a very sick platform that it might fail.
38963** If it does fail, simply leak the file descriptor, but do log the
38964** error.
38965**
38966** Note that it is not safe to retry close() after EINTR since the
38967** file descriptor might have already been reused by another thread.
38968** So we don't even try to recover from an EINTR. Just log the error
38969** and move on.
38970*/
38971static void robust_close(unixFile *pFile, int h, int lineno){
38972 if( osClose(h) ){
38973 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, zFunc: "close",
38974 zPath: pFile ? pFile->zPath : 0, iLine: lineno);
38975 }
38976}
38977
38978/*
38979** Set the pFile->lastErrno. Do this in a subroutine as that provides
38980** a convenient place to set a breakpoint.
38981*/
38982static void storeLastErrno(unixFile *pFile, int error){
38983 pFile->lastErrno = error;
38984}
38985
38986/*
38987** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
38988*/
38989static void closePendingFds(unixFile *pFile){
38990 unixInodeInfo *pInode = pFile->pInode;
38991 UnixUnusedFd *p;
38992 UnixUnusedFd *pNext;
38993 assert( unixFileMutexHeld(pFile) );
38994 for(p=pInode->pUnused; p; p=pNext){
38995 pNext = p->pNext;
38996 robust_close(pFile, h: p->fd, __LINE__);
38997 sqlite3_free(p);
38998 }
38999 pInode->pUnused = 0;
39000}
39001
39002/*
39003** Release a unixInodeInfo structure previously allocated by findInodeInfo().
39004**
39005** The global mutex must be held when this routine is called, but the mutex
39006** on the inode being deleted must NOT be held.
39007*/
39008static void releaseInodeInfo(unixFile *pFile){
39009 unixInodeInfo *pInode = pFile->pInode;
39010 assert( unixMutexHeld() );
39011 assert( unixFileMutexNotheld(pFile) );
39012 if( ALWAYS(pInode) ){
39013 pInode->nRef--;
39014 if( pInode->nRef==0 ){
39015 assert( pInode->pShmNode==0 );
39016 sqlite3_mutex_enter(p: pInode->pLockMutex);
39017 closePendingFds(pFile);
39018 sqlite3_mutex_leave(p: pInode->pLockMutex);
39019 if( pInode->pPrev ){
39020 assert( pInode->pPrev->pNext==pInode );
39021 pInode->pPrev->pNext = pInode->pNext;
39022 }else{
39023 assert( inodeList==pInode );
39024 inodeList = pInode->pNext;
39025 }
39026 if( pInode->pNext ){
39027 assert( pInode->pNext->pPrev==pInode );
39028 pInode->pNext->pPrev = pInode->pPrev;
39029 }
39030 sqlite3_mutex_free(p: pInode->pLockMutex);
39031 sqlite3_free(p: pInode);
39032 }
39033 }
39034}
39035
39036/*
39037** Given a file descriptor, locate the unixInodeInfo object that
39038** describes that file descriptor. Create a new one if necessary. The
39039** return value might be uninitialized if an error occurs.
39040**
39041** The global mutex must held when calling this routine.
39042**
39043** Return an appropriate error code.
39044*/
39045static int findInodeInfo(
39046 unixFile *pFile, /* Unix file with file desc used in the key */
39047 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
39048){
39049 int rc; /* System call return code */
39050 int fd; /* The file descriptor for pFile */
39051 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
39052 struct stat statbuf; /* Low-level file information */
39053 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
39054
39055 assert( unixMutexHeld() );
39056
39057 /* Get low-level information about the file that we can used to
39058 ** create a unique name for the file.
39059 */
39060 fd = pFile->h;
39061 rc = osFstat(fd, &statbuf);
39062 if( rc!=0 ){
39063 storeLastErrno(pFile, errno);
39064#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
39065 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
39066#endif
39067 return SQLITE_IOERR;
39068 }
39069
39070#ifdef __APPLE__
39071 /* On OS X on an msdos filesystem, the inode number is reported
39072 ** incorrectly for zero-size files. See ticket #3260. To work
39073 ** around this problem (we consider it a bug in OS X, not SQLite)
39074 ** we always increase the file size to 1 by writing a single byte
39075 ** prior to accessing the inode number. The one byte written is
39076 ** an ASCII 'S' character which also happens to be the first byte
39077 ** in the header of every SQLite database. In this way, if there
39078 ** is a race condition such that another thread has already populated
39079 ** the first page of the database, no damage is done.
39080 */
39081 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
39082 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
39083 if( rc!=1 ){
39084 storeLastErrno(pFile, errno);
39085 return SQLITE_IOERR;
39086 }
39087 rc = osFstat(fd, &statbuf);
39088 if( rc!=0 ){
39089 storeLastErrno(pFile, errno);
39090 return SQLITE_IOERR;
39091 }
39092 }
39093#endif
39094
39095 memset(s: &fileId, c: 0, n: sizeof(fileId));
39096 fileId.dev = statbuf.st_dev;
39097#if OS_VXWORKS
39098 fileId.pId = pFile->pId;
39099#else
39100 fileId.ino = (u64)statbuf.st_ino;
39101#endif
39102 assert( unixMutexHeld() );
39103 pInode = inodeList;
39104 while( pInode && memcmp(s1: &fileId, s2: &pInode->fileId, n: sizeof(fileId)) ){
39105 pInode = pInode->pNext;
39106 }
39107 if( pInode==0 ){
39108 pInode = sqlite3_malloc64( n: sizeof(*pInode) );
39109 if( pInode==0 ){
39110 return SQLITE_NOMEM_BKPT;
39111 }
39112 memset(s: pInode, c: 0, n: sizeof(*pInode));
39113 memcpy(dest: &pInode->fileId, src: &fileId, n: sizeof(fileId));
39114 if( sqlite3GlobalConfig.bCoreMutex ){
39115 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
39116 if( pInode->pLockMutex==0 ){
39117 sqlite3_free(p: pInode);
39118 return SQLITE_NOMEM_BKPT;
39119 }
39120 }
39121 pInode->nRef = 1;
39122 assert( unixMutexHeld() );
39123 pInode->pNext = inodeList;
39124 pInode->pPrev = 0;
39125 if( inodeList ) inodeList->pPrev = pInode;
39126 inodeList = pInode;
39127 }else{
39128 pInode->nRef++;
39129 }
39130 *ppInode = pInode;
39131 return SQLITE_OK;
39132}
39133
39134/*
39135** Return TRUE if pFile has been renamed or unlinked since it was first opened.
39136*/
39137static int fileHasMoved(unixFile *pFile){
39138#if OS_VXWORKS
39139 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
39140#else
39141 struct stat buf;
39142 return pFile->pInode!=0 &&
39143 (osStat(pFile->zPath, &buf)!=0
39144 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
39145#endif
39146}
39147
39148
39149/*
39150** Check a unixFile that is a database. Verify the following:
39151**
39152** (1) There is exactly one hard link on the file
39153** (2) The file is not a symbolic link
39154** (3) The file has not been renamed or unlinked
39155**
39156** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
39157*/
39158static void verifyDbFile(unixFile *pFile){
39159 struct stat buf;
39160 int rc;
39161
39162 /* These verifications occurs for the main database only */
39163 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
39164
39165 rc = osFstat(pFile->h, &buf);
39166 if( rc!=0 ){
39167 sqlite3_log(SQLITE_WARNING, zFormat: "cannot fstat db file %s", pFile->zPath);
39168 return;
39169 }
39170 if( buf.st_nlink==0 ){
39171 sqlite3_log(SQLITE_WARNING, zFormat: "file unlinked while open: %s", pFile->zPath);
39172 return;
39173 }
39174 if( buf.st_nlink>1 ){
39175 sqlite3_log(SQLITE_WARNING, zFormat: "multiple links to file: %s", pFile->zPath);
39176 return;
39177 }
39178 if( fileHasMoved(pFile) ){
39179 sqlite3_log(SQLITE_WARNING, zFormat: "file renamed while open: %s", pFile->zPath);
39180 return;
39181 }
39182}
39183
39184
39185/*
39186** This routine checks if there is a RESERVED lock held on the specified
39187** file by this or any other process. If such a lock is held, set *pResOut
39188** to a non-zero value otherwise *pResOut is set to zero. The return value
39189** is set to SQLITE_OK unless an I/O error occurs during lock checking.
39190*/
39191static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
39192 int rc = SQLITE_OK;
39193 int reserved = 0;
39194 unixFile *pFile = (unixFile*)id;
39195
39196 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
39197
39198 assert( pFile );
39199 assert( pFile->eFileLock<=SHARED_LOCK );
39200 sqlite3_mutex_enter(p: pFile->pInode->pLockMutex);
39201
39202 /* Check if a thread in this process holds such a lock */
39203 if( pFile->pInode->eFileLock>SHARED_LOCK ){
39204 reserved = 1;
39205 }
39206
39207 /* Otherwise see if some other process holds it.
39208 */
39209#ifndef __DJGPP__
39210 if( !reserved && !pFile->pInode->bProcessLock ){
39211 struct flock lock;
39212 lock.l_whence = SEEK_SET;
39213 lock.l_start = RESERVED_BYTE;
39214 lock.l_len = 1;
39215 lock.l_type = F_WRLCK;
39216 if( osFcntl(pFile->h, F_GETLK, &lock) ){
39217 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
39218 storeLastErrno(pFile, errno);
39219 } else if( lock.l_type!=F_UNLCK ){
39220 reserved = 1;
39221 }
39222 }
39223#endif
39224
39225 sqlite3_mutex_leave(p: pFile->pInode->pLockMutex);
39226 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
39227
39228 *pResOut = reserved;
39229 return rc;
39230}
39231
39232/* Forward declaration*/
39233static int unixSleep(sqlite3_vfs*,int);
39234
39235/*
39236** Set a posix-advisory-lock.
39237**
39238** There are two versions of this routine. If compiled with
39239** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
39240** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
39241** value is set, then it is the number of milliseconds to wait before
39242** failing the lock. The iBusyTimeout value is always reset back to
39243** zero on each call.
39244**
39245** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
39246** attempt to set the lock.
39247*/
39248#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
39249# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
39250#else
39251static int osSetPosixAdvisoryLock(
39252 int h, /* The file descriptor on which to take the lock */
39253 struct flock *pLock, /* The description of the lock */
39254 unixFile *pFile /* Structure holding timeout value */
39255){
39256 int tm = pFile->iBusyTimeout;
39257 int rc = osFcntl(h,F_SETLK,pLock);
39258 while( rc<0 && tm>0 ){
39259 /* On systems that support some kind of blocking file lock with a timeout,
39260 ** make appropriate changes here to invoke that blocking file lock. On
39261 ** generic posix, however, there is no such API. So we simply try the
39262 ** lock once every millisecond until either the timeout expires, or until
39263 ** the lock is obtained. */
39264 unixSleep(0,1000);
39265 rc = osFcntl(h,F_SETLK,pLock);
39266 tm--;
39267 }
39268 return rc;
39269}
39270#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
39271
39272
39273/*
39274** Attempt to set a system-lock on the file pFile. The lock is
39275** described by pLock.
39276**
39277** If the pFile was opened read/write from unix-excl, then the only lock
39278** ever obtained is an exclusive lock, and it is obtained exactly once
39279** the first time any lock is attempted. All subsequent system locking
39280** operations become no-ops. Locking operations still happen internally,
39281** in order to coordinate access between separate database connections
39282** within this process, but all of that is handled in memory and the
39283** operating system does not participate.
39284**
39285** This function is a pass-through to fcntl(F_SETLK) if pFile is using
39286** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
39287** and is read-only.
39288**
39289** Zero is returned if the call completes successfully, or -1 if a call
39290** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
39291*/
39292static int unixFileLock(unixFile *pFile, struct flock *pLock){
39293 int rc;
39294 unixInodeInfo *pInode = pFile->pInode;
39295 assert( pInode!=0 );
39296 assert( sqlite3_mutex_held(pInode->pLockMutex) );
39297 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
39298 if( pInode->bProcessLock==0 ){
39299 struct flock lock;
39300 assert( pInode->nLock==0 );
39301 lock.l_whence = SEEK_SET;
39302 lock.l_start = SHARED_FIRST;
39303 lock.l_len = SHARED_SIZE;
39304 lock.l_type = F_WRLCK;
39305 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
39306 if( rc<0 ) return rc;
39307 pInode->bProcessLock = 1;
39308 pInode->nLock++;
39309 }else{
39310 rc = 0;
39311 }
39312 }else{
39313 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
39314 }
39315 return rc;
39316}
39317
39318/*
39319** Lock the file with the lock specified by parameter eFileLock - one
39320** of the following:
39321**
39322** (1) SHARED_LOCK
39323** (2) RESERVED_LOCK
39324** (3) PENDING_LOCK
39325** (4) EXCLUSIVE_LOCK
39326**
39327** Sometimes when requesting one lock state, additional lock states
39328** are inserted in between. The locking might fail on one of the later
39329** transitions leaving the lock state different from what it started but
39330** still short of its goal. The following chart shows the allowed
39331** transitions and the inserted intermediate states:
39332**
39333** UNLOCKED -> SHARED
39334** SHARED -> RESERVED
39335** SHARED -> EXCLUSIVE
39336** RESERVED -> (PENDING) -> EXCLUSIVE
39337** PENDING -> EXCLUSIVE
39338**
39339** This routine will only increase a lock. Use the sqlite3OsUnlock()
39340** routine to lower a locking level.
39341*/
39342static int unixLock(sqlite3_file *id, int eFileLock){
39343 /* The following describes the implementation of the various locks and
39344 ** lock transitions in terms of the POSIX advisory shared and exclusive
39345 ** lock primitives (called read-locks and write-locks below, to avoid
39346 ** confusion with SQLite lock names). The algorithms are complicated
39347 ** slightly in order to be compatible with Windows95 systems simultaneously
39348 ** accessing the same database file, in case that is ever required.
39349 **
39350 ** Symbols defined in os.h identify the 'pending byte' and the 'reserved
39351 ** byte', each single bytes at well known offsets, and the 'shared byte
39352 ** range', a range of 510 bytes at a well known offset.
39353 **
39354 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
39355 ** byte'. If this is successful, 'shared byte range' is read-locked
39356 ** and the lock on the 'pending byte' released. (Legacy note: When
39357 ** SQLite was first developed, Windows95 systems were still very common,
39358 ** and Windows95 lacks a shared-lock capability. So on Windows95, a
39359 ** single randomly selected by from the 'shared byte range' is locked.
39360 ** Windows95 is now pretty much extinct, but this work-around for the
39361 ** lack of shared-locks on Windows95 lives on, for backwards
39362 ** compatibility.)
39363 **
39364 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
39365 ** A RESERVED lock is implemented by grabbing a write-lock on the
39366 ** 'reserved byte'.
39367 **
39368 ** An EXCLUSIVE lock may only be requested after either a SHARED or
39369 ** RESERVED lock is held. An EXCLUSIVE lock is implemented by obtaining
39370 ** a write-lock on the entire 'shared byte range'. Since all other locks
39371 ** require a read-lock on one of the bytes within this range, this ensures
39372 ** that no other locks are held on the database.
39373 **
39374 ** If a process that holds a RESERVED lock requests an EXCLUSIVE, then
39375 ** a PENDING lock is obtained first. A PENDING lock is implemented by
39376 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
39377 ** SHARED locks can be obtained, but existing SHARED locks are allowed to
39378 ** persist. If the call to this function fails to obtain the EXCLUSIVE
39379 ** lock in this case, it holds the PENDING lock instead. The client may
39380 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
39381 ** locks have cleared.
39382 */
39383 int rc = SQLITE_OK;
39384 unixFile *pFile = (unixFile*)id;
39385 unixInodeInfo *pInode;
39386 struct flock lock;
39387 int tErrno = 0;
39388
39389 assert( pFile );
39390 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
39391 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
39392 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
39393 osGetpid(0)));
39394
39395 /* If there is already a lock of this type or more restrictive on the
39396 ** unixFile, do nothing. Don't use the end_lock: exit path, as
39397 ** unixEnterMutex() hasn't been called yet.
39398 */
39399 if( pFile->eFileLock>=eFileLock ){
39400 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
39401 azFileLock(eFileLock)));
39402 return SQLITE_OK;
39403 }
39404
39405 /* Make sure the locking sequence is correct.
39406 ** (1) We never move from unlocked to anything higher than shared lock.
39407 ** (2) SQLite never explicitly requests a pending lock.
39408 ** (3) A shared lock is always held when a reserve lock is requested.
39409 */
39410 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
39411 assert( eFileLock!=PENDING_LOCK );
39412 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
39413
39414 /* This mutex is needed because pFile->pInode is shared across threads
39415 */
39416 pInode = pFile->pInode;
39417 sqlite3_mutex_enter(p: pInode->pLockMutex);
39418
39419 /* If some thread using this PID has a lock via a different unixFile*
39420 ** handle that precludes the requested lock, return BUSY.
39421 */
39422 if( (pFile->eFileLock!=pInode->eFileLock &&
39423 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
39424 ){
39425 rc = SQLITE_BUSY;
39426 goto end_lock;
39427 }
39428
39429 /* If a SHARED lock is requested, and some thread using this PID already
39430 ** has a SHARED or RESERVED lock, then increment reference counts and
39431 ** return SQLITE_OK.
39432 */
39433 if( eFileLock==SHARED_LOCK &&
39434 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
39435 assert( eFileLock==SHARED_LOCK );
39436 assert( pFile->eFileLock==0 );
39437 assert( pInode->nShared>0 );
39438 pFile->eFileLock = SHARED_LOCK;
39439 pInode->nShared++;
39440 pInode->nLock++;
39441 goto end_lock;
39442 }
39443
39444
39445 /* A PENDING lock is needed before acquiring a SHARED lock and before
39446 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
39447 ** be released.
39448 */
39449 lock.l_len = 1L;
39450 lock.l_whence = SEEK_SET;
39451 if( eFileLock==SHARED_LOCK
39452 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock==RESERVED_LOCK)
39453 ){
39454 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
39455 lock.l_start = PENDING_BYTE;
39456 if( unixFileLock(pFile, pLock: &lock) ){
39457 tErrno = errno;
39458 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
39459 if( rc!=SQLITE_BUSY ){
39460 storeLastErrno(pFile, error: tErrno);
39461 }
39462 goto end_lock;
39463 }else if( eFileLock==EXCLUSIVE_LOCK ){
39464 pFile->eFileLock = PENDING_LOCK;
39465 pInode->eFileLock = PENDING_LOCK;
39466 }
39467 }
39468
39469
39470 /* If control gets to this point, then actually go ahead and make
39471 ** operating system calls for the specified lock.
39472 */
39473 if( eFileLock==SHARED_LOCK ){
39474 assert( pInode->nShared==0 );
39475 assert( pInode->eFileLock==0 );
39476 assert( rc==SQLITE_OK );
39477
39478 /* Now get the read-lock */
39479 lock.l_start = SHARED_FIRST;
39480 lock.l_len = SHARED_SIZE;
39481 if( unixFileLock(pFile, pLock: &lock) ){
39482 tErrno = errno;
39483 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
39484 }
39485
39486 /* Drop the temporary PENDING lock */
39487 lock.l_start = PENDING_BYTE;
39488 lock.l_len = 1L;
39489 lock.l_type = F_UNLCK;
39490 if( unixFileLock(pFile, pLock: &lock) && rc==SQLITE_OK ){
39491 /* This could happen with a network mount */
39492 tErrno = errno;
39493 rc = SQLITE_IOERR_UNLOCK;
39494 }
39495
39496 if( rc ){
39497 if( rc!=SQLITE_BUSY ){
39498 storeLastErrno(pFile, error: tErrno);
39499 }
39500 goto end_lock;
39501 }else{
39502 pFile->eFileLock = SHARED_LOCK;
39503 pInode->nLock++;
39504 pInode->nShared = 1;
39505 }
39506 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
39507 /* We are trying for an exclusive lock but another thread in this
39508 ** same process is still holding a shared lock. */
39509 rc = SQLITE_BUSY;
39510 }else{
39511 /* The request was for a RESERVED or EXCLUSIVE lock. It is
39512 ** assumed that there is a SHARED or greater lock on the file
39513 ** already.
39514 */
39515 assert( 0!=pFile->eFileLock );
39516 lock.l_type = F_WRLCK;
39517
39518 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
39519 if( eFileLock==RESERVED_LOCK ){
39520 lock.l_start = RESERVED_BYTE;
39521 lock.l_len = 1L;
39522 }else{
39523 lock.l_start = SHARED_FIRST;
39524 lock.l_len = SHARED_SIZE;
39525 }
39526
39527 if( unixFileLock(pFile, pLock: &lock) ){
39528 tErrno = errno;
39529 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
39530 if( rc!=SQLITE_BUSY ){
39531 storeLastErrno(pFile, error: tErrno);
39532 }
39533 }
39534 }
39535
39536
39537#ifdef SQLITE_DEBUG
39538 /* Set up the transaction-counter change checking flags when
39539 ** transitioning from a SHARED to a RESERVED lock. The change
39540 ** from SHARED to RESERVED marks the beginning of a normal
39541 ** write operation (not a hot journal rollback).
39542 */
39543 if( rc==SQLITE_OK
39544 && pFile->eFileLock<=SHARED_LOCK
39545 && eFileLock==RESERVED_LOCK
39546 ){
39547 pFile->transCntrChng = 0;
39548 pFile->dbUpdate = 0;
39549 pFile->inNormalWrite = 1;
39550 }
39551#endif
39552
39553 if( rc==SQLITE_OK ){
39554 pFile->eFileLock = eFileLock;
39555 pInode->eFileLock = eFileLock;
39556 }
39557
39558end_lock:
39559 sqlite3_mutex_leave(p: pInode->pLockMutex);
39560 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
39561 rc==SQLITE_OK ? "ok" : "failed"));
39562 return rc;
39563}
39564
39565/*
39566** Add the file descriptor used by file handle pFile to the corresponding
39567** pUnused list.
39568*/
39569static void setPendingFd(unixFile *pFile){
39570 unixInodeInfo *pInode = pFile->pInode;
39571 UnixUnusedFd *p = pFile->pPreallocatedUnused;
39572 assert( unixFileMutexHeld(pFile) );
39573 p->pNext = pInode->pUnused;
39574 pInode->pUnused = p;
39575 pFile->h = -1;
39576 pFile->pPreallocatedUnused = 0;
39577}
39578
39579/*
39580** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
39581** must be either NO_LOCK or SHARED_LOCK.
39582**
39583** If the locking level of the file descriptor is already at or below
39584** the requested locking level, this routine is a no-op.
39585**
39586** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
39587** the byte range is divided into 2 parts and the first part is unlocked then
39588** set to a read lock, then the other part is simply unlocked. This works
39589** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
39590** remove the write lock on a region when a read lock is set.
39591*/
39592static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
39593 unixFile *pFile = (unixFile*)id;
39594 unixInodeInfo *pInode;
39595 struct flock lock;
39596 int rc = SQLITE_OK;
39597
39598 assert( pFile );
39599 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
39600 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
39601 osGetpid(0)));
39602
39603 assert( eFileLock<=SHARED_LOCK );
39604 if( pFile->eFileLock<=eFileLock ){
39605 return SQLITE_OK;
39606 }
39607 pInode = pFile->pInode;
39608 sqlite3_mutex_enter(p: pInode->pLockMutex);
39609 assert( pInode->nShared!=0 );
39610 if( pFile->eFileLock>SHARED_LOCK ){
39611 assert( pInode->eFileLock==pFile->eFileLock );
39612
39613#ifdef SQLITE_DEBUG
39614 /* When reducing a lock such that other processes can start
39615 ** reading the database file again, make sure that the
39616 ** transaction counter was updated if any part of the database
39617 ** file changed. If the transaction counter is not updated,
39618 ** other connections to the same file might not realize that
39619 ** the file has changed and hence might not know to flush their
39620 ** cache. The use of a stale cache can lead to database corruption.
39621 */
39622 pFile->inNormalWrite = 0;
39623#endif
39624
39625 /* downgrading to a shared lock on NFS involves clearing the write lock
39626 ** before establishing the readlock - to avoid a race condition we downgrade
39627 ** the lock in 2 blocks, so that part of the range will be covered by a
39628 ** write lock until the rest is covered by a read lock:
39629 ** 1: [WWWWW]
39630 ** 2: [....W]
39631 ** 3: [RRRRW]
39632 ** 4: [RRRR.]
39633 */
39634 if( eFileLock==SHARED_LOCK ){
39635#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
39636 (void)handleNFSUnlock;
39637 assert( handleNFSUnlock==0 );
39638#endif
39639#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39640 if( handleNFSUnlock ){
39641 int tErrno; /* Error code from system call errors */
39642 off_t divSize = SHARED_SIZE - 1;
39643
39644 lock.l_type = F_UNLCK;
39645 lock.l_whence = SEEK_SET;
39646 lock.l_start = SHARED_FIRST;
39647 lock.l_len = divSize;
39648 if( unixFileLock(pFile, &lock)==(-1) ){
39649 tErrno = errno;
39650 rc = SQLITE_IOERR_UNLOCK;
39651 storeLastErrno(pFile, tErrno);
39652 goto end_unlock;
39653 }
39654 lock.l_type = F_RDLCK;
39655 lock.l_whence = SEEK_SET;
39656 lock.l_start = SHARED_FIRST;
39657 lock.l_len = divSize;
39658 if( unixFileLock(pFile, &lock)==(-1) ){
39659 tErrno = errno;
39660 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
39661 if( IS_LOCK_ERROR(rc) ){
39662 storeLastErrno(pFile, tErrno);
39663 }
39664 goto end_unlock;
39665 }
39666 lock.l_type = F_UNLCK;
39667 lock.l_whence = SEEK_SET;
39668 lock.l_start = SHARED_FIRST+divSize;
39669 lock.l_len = SHARED_SIZE-divSize;
39670 if( unixFileLock(pFile, &lock)==(-1) ){
39671 tErrno = errno;
39672 rc = SQLITE_IOERR_UNLOCK;
39673 storeLastErrno(pFile, tErrno);
39674 goto end_unlock;
39675 }
39676 }else
39677#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
39678 {
39679 lock.l_type = F_RDLCK;
39680 lock.l_whence = SEEK_SET;
39681 lock.l_start = SHARED_FIRST;
39682 lock.l_len = SHARED_SIZE;
39683 if( unixFileLock(pFile, pLock: &lock) ){
39684 /* In theory, the call to unixFileLock() cannot fail because another
39685 ** process is holding an incompatible lock. If it does, this
39686 ** indicates that the other process is not following the locking
39687 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
39688 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
39689 ** an assert to fail). */
39690 rc = SQLITE_IOERR_RDLOCK;
39691 storeLastErrno(pFile, errno);
39692 goto end_unlock;
39693 }
39694 }
39695 }
39696 lock.l_type = F_UNLCK;
39697 lock.l_whence = SEEK_SET;
39698 lock.l_start = PENDING_BYTE;
39699 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
39700 if( unixFileLock(pFile, pLock: &lock)==0 ){
39701 pInode->eFileLock = SHARED_LOCK;
39702 }else{
39703 rc = SQLITE_IOERR_UNLOCK;
39704 storeLastErrno(pFile, errno);
39705 goto end_unlock;
39706 }
39707 }
39708 if( eFileLock==NO_LOCK ){
39709 /* Decrement the shared lock counter. Release the lock using an
39710 ** OS call only when all threads in this same process have released
39711 ** the lock.
39712 */
39713 pInode->nShared--;
39714 if( pInode->nShared==0 ){
39715 lock.l_type = F_UNLCK;
39716 lock.l_whence = SEEK_SET;
39717 lock.l_start = lock.l_len = 0L;
39718 if( unixFileLock(pFile, pLock: &lock)==0 ){
39719 pInode->eFileLock = NO_LOCK;
39720 }else{
39721 rc = SQLITE_IOERR_UNLOCK;
39722 storeLastErrno(pFile, errno);
39723 pInode->eFileLock = NO_LOCK;
39724 pFile->eFileLock = NO_LOCK;
39725 }
39726 }
39727
39728 /* Decrement the count of locks against this same file. When the
39729 ** count reaches zero, close any other file descriptors whose close
39730 ** was deferred because of outstanding locks.
39731 */
39732 pInode->nLock--;
39733 assert( pInode->nLock>=0 );
39734 if( pInode->nLock==0 ) closePendingFds(pFile);
39735 }
39736
39737end_unlock:
39738 sqlite3_mutex_leave(p: pInode->pLockMutex);
39739 if( rc==SQLITE_OK ){
39740 pFile->eFileLock = eFileLock;
39741 }
39742 return rc;
39743}
39744
39745/*
39746** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
39747** must be either NO_LOCK or SHARED_LOCK.
39748**
39749** If the locking level of the file descriptor is already at or below
39750** the requested locking level, this routine is a no-op.
39751*/
39752static int unixUnlock(sqlite3_file *id, int eFileLock){
39753#if SQLITE_MAX_MMAP_SIZE>0
39754 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
39755#endif
39756 return posixUnlock(id, eFileLock, handleNFSUnlock: 0);
39757}
39758
39759#if SQLITE_MAX_MMAP_SIZE>0
39760static int unixMapfile(unixFile *pFd, i64 nByte);
39761static void unixUnmapfile(unixFile *pFd);
39762#endif
39763
39764/*
39765** This function performs the parts of the "close file" operation
39766** common to all locking schemes. It closes the directory and file
39767** handles, if they are valid, and sets all fields of the unixFile
39768** structure to 0.
39769**
39770** It is *not* necessary to hold the mutex when this routine is called,
39771** even on VxWorks. A mutex will be acquired on VxWorks by the
39772** vxworksReleaseFileId() routine.
39773*/
39774static int closeUnixFile(sqlite3_file *id){
39775 unixFile *pFile = (unixFile*)id;
39776#if SQLITE_MAX_MMAP_SIZE>0
39777 unixUnmapfile(pFd: pFile);
39778#endif
39779 if( pFile->h>=0 ){
39780 robust_close(pFile, h: pFile->h, __LINE__);
39781 pFile->h = -1;
39782 }
39783#if OS_VXWORKS
39784 if( pFile->pId ){
39785 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
39786 osUnlink(pFile->pId->zCanonicalName);
39787 }
39788 vxworksReleaseFileId(pFile->pId);
39789 pFile->pId = 0;
39790 }
39791#endif
39792#ifdef SQLITE_UNLINK_AFTER_CLOSE
39793 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
39794 osUnlink(pFile->zPath);
39795 sqlite3_free(*(char**)&pFile->zPath);
39796 pFile->zPath = 0;
39797 }
39798#endif
39799 OSTRACE(("CLOSE %-3d\n", pFile->h));
39800 OpenCounter(-1);
39801 sqlite3_free(p: pFile->pPreallocatedUnused);
39802 memset(s: pFile, c: 0, n: sizeof(unixFile));
39803 return SQLITE_OK;
39804}
39805
39806/*
39807** Close a file.
39808*/
39809static int unixClose(sqlite3_file *id){
39810 int rc = SQLITE_OK;
39811 unixFile *pFile = (unixFile *)id;
39812 unixInodeInfo *pInode = pFile->pInode;
39813
39814 assert( pInode!=0 );
39815 verifyDbFile(pFile);
39816 unixUnlock(id, NO_LOCK);
39817 assert( unixFileMutexNotheld(pFile) );
39818 unixEnterMutex();
39819
39820 /* unixFile.pInode is always valid here. Otherwise, a different close
39821 ** routine (e.g. nolockClose()) would be called instead.
39822 */
39823 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
39824 sqlite3_mutex_enter(p: pInode->pLockMutex);
39825 if( pInode->nLock ){
39826 /* If there are outstanding locks, do not actually close the file just
39827 ** yet because that would clear those locks. Instead, add the file
39828 ** descriptor to pInode->pUnused list. It will be automatically closed
39829 ** when the last lock is cleared.
39830 */
39831 setPendingFd(pFile);
39832 }
39833 sqlite3_mutex_leave(p: pInode->pLockMutex);
39834 releaseInodeInfo(pFile);
39835 assert( pFile->pShm==0 );
39836 rc = closeUnixFile(id);
39837 unixLeaveMutex();
39838 return rc;
39839}
39840
39841/************** End of the posix advisory lock implementation *****************
39842******************************************************************************/
39843
39844/******************************************************************************
39845****************************** No-op Locking **********************************
39846**
39847** Of the various locking implementations available, this is by far the
39848** simplest: locking is ignored. No attempt is made to lock the database
39849** file for reading or writing.
39850**
39851** This locking mode is appropriate for use on read-only databases
39852** (ex: databases that are burned into CD-ROM, for example.) It can
39853** also be used if the application employs some external mechanism to
39854** prevent simultaneous access of the same database by two or more
39855** database connections. But there is a serious risk of database
39856** corruption if this locking mode is used in situations where multiple
39857** database connections are accessing the same database file at the same
39858** time and one or more of those connections are writing.
39859*/
39860
39861static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
39862 UNUSED_PARAMETER(NotUsed);
39863 *pResOut = 0;
39864 return SQLITE_OK;
39865}
39866static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
39867 UNUSED_PARAMETER2(NotUsed, NotUsed2);
39868 return SQLITE_OK;
39869}
39870static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
39871 UNUSED_PARAMETER2(NotUsed, NotUsed2);
39872 return SQLITE_OK;
39873}
39874
39875/*
39876** Close the file.
39877*/
39878static int nolockClose(sqlite3_file *id) {
39879 return closeUnixFile(id);
39880}
39881
39882/******************* End of the no-op lock implementation *********************
39883******************************************************************************/
39884
39885/******************************************************************************
39886************************* Begin dot-file Locking ******************************
39887**
39888** The dotfile locking implementation uses the existence of separate lock
39889** files (really a directory) to control access to the database. This works
39890** on just about every filesystem imaginable. But there are serious downsides:
39891**
39892** (1) There is zero concurrency. A single reader blocks all other
39893** connections from reading or writing the database.
39894**
39895** (2) An application crash or power loss can leave stale lock files
39896** sitting around that need to be cleared manually.
39897**
39898** Nevertheless, a dotlock is an appropriate locking mode for use if no
39899** other locking strategy is available.
39900**
39901** Dotfile locking works by creating a subdirectory in the same directory as
39902** the database and with the same name but with a ".lock" extension added.
39903** The existence of a lock directory implies an EXCLUSIVE lock. All other
39904** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
39905*/
39906
39907/*
39908** The file suffix added to the data base filename in order to create the
39909** lock directory.
39910*/
39911#define DOTLOCK_SUFFIX ".lock"
39912
39913/*
39914** This routine checks if there is a RESERVED lock held on the specified
39915** file by this or any other process. If such a lock is held, set *pResOut
39916** to a non-zero value otherwise *pResOut is set to zero. The return value
39917** is set to SQLITE_OK unless an I/O error occurs during lock checking.
39918**
39919** In dotfile locking, either a lock exists or it does not. So in this
39920** variation of CheckReservedLock(), *pResOut is set to true if any lock
39921** is held on the file and false if the file is unlocked.
39922*/
39923static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
39924 int rc = SQLITE_OK;
39925 int reserved = 0;
39926 unixFile *pFile = (unixFile*)id;
39927
39928 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
39929
39930 assert( pFile );
39931 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
39932 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
39933 *pResOut = reserved;
39934 return rc;
39935}
39936
39937/*
39938** Lock the file with the lock specified by parameter eFileLock - one
39939** of the following:
39940**
39941** (1) SHARED_LOCK
39942** (2) RESERVED_LOCK
39943** (3) PENDING_LOCK
39944** (4) EXCLUSIVE_LOCK
39945**
39946** Sometimes when requesting one lock state, additional lock states
39947** are inserted in between. The locking might fail on one of the later
39948** transitions leaving the lock state different from what it started but
39949** still short of its goal. The following chart shows the allowed
39950** transitions and the inserted intermediate states:
39951**
39952** UNLOCKED -> SHARED
39953** SHARED -> RESERVED
39954** SHARED -> (PENDING) -> EXCLUSIVE
39955** RESERVED -> (PENDING) -> EXCLUSIVE
39956** PENDING -> EXCLUSIVE
39957**
39958** This routine will only increase a lock. Use the sqlite3OsUnlock()
39959** routine to lower a locking level.
39960**
39961** With dotfile locking, we really only support state (4): EXCLUSIVE.
39962** But we track the other locking levels internally.
39963*/
39964static int dotlockLock(sqlite3_file *id, int eFileLock) {
39965 unixFile *pFile = (unixFile*)id;
39966 char *zLockFile = (char *)pFile->lockingContext;
39967 int rc = SQLITE_OK;
39968
39969
39970 /* If we have any lock, then the lock file already exists. All we have
39971 ** to do is adjust our internal record of the lock level.
39972 */
39973 if( pFile->eFileLock > NO_LOCK ){
39974 pFile->eFileLock = eFileLock;
39975 /* Always update the timestamp on the old file */
39976#ifdef HAVE_UTIME
39977 utime(zLockFile, NULL);
39978#else
39979 utimes(file: zLockFile, NULL);
39980#endif
39981 return SQLITE_OK;
39982 }
39983
39984 /* grab an exclusive lock */
39985 rc = osMkdir(zLockFile, 0777);
39986 if( rc<0 ){
39987 /* failed to open/create the lock directory */
39988 int tErrno = errno;
39989 if( EEXIST == tErrno ){
39990 rc = SQLITE_BUSY;
39991 } else {
39992 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
39993 if( rc!=SQLITE_BUSY ){
39994 storeLastErrno(pFile, error: tErrno);
39995 }
39996 }
39997 return rc;
39998 }
39999
40000 /* got it, set the type and return ok */
40001 pFile->eFileLock = eFileLock;
40002 return rc;
40003}
40004
40005/*
40006** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40007** must be either NO_LOCK or SHARED_LOCK.
40008**
40009** If the locking level of the file descriptor is already at or below
40010** the requested locking level, this routine is a no-op.
40011**
40012** When the locking level reaches NO_LOCK, delete the lock file.
40013*/
40014static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
40015 unixFile *pFile = (unixFile*)id;
40016 char *zLockFile = (char *)pFile->lockingContext;
40017 int rc;
40018
40019 assert( pFile );
40020 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
40021 pFile->eFileLock, osGetpid(0)));
40022 assert( eFileLock<=SHARED_LOCK );
40023
40024 /* no-op if possible */
40025 if( pFile->eFileLock==eFileLock ){
40026 return SQLITE_OK;
40027 }
40028
40029 /* To downgrade to shared, simply update our internal notion of the
40030 ** lock state. No need to mess with the file on disk.
40031 */
40032 if( eFileLock==SHARED_LOCK ){
40033 pFile->eFileLock = SHARED_LOCK;
40034 return SQLITE_OK;
40035 }
40036
40037 /* To fully unlock the database, delete the lock file */
40038 assert( eFileLock==NO_LOCK );
40039 rc = osRmdir(zLockFile);
40040 if( rc<0 ){
40041 int tErrno = errno;
40042 if( tErrno==ENOENT ){
40043 rc = SQLITE_OK;
40044 }else{
40045 rc = SQLITE_IOERR_UNLOCK;
40046 storeLastErrno(pFile, error: tErrno);
40047 }
40048 return rc;
40049 }
40050 pFile->eFileLock = NO_LOCK;
40051 return SQLITE_OK;
40052}
40053
40054/*
40055** Close a file. Make sure the lock has been released before closing.
40056*/
40057static int dotlockClose(sqlite3_file *id) {
40058 unixFile *pFile = (unixFile*)id;
40059 assert( id!=0 );
40060 dotlockUnlock(id, NO_LOCK);
40061 sqlite3_free(p: pFile->lockingContext);
40062 return closeUnixFile(id);
40063}
40064/****************** End of the dot-file lock implementation *******************
40065******************************************************************************/
40066
40067/******************************************************************************
40068************************** Begin flock Locking ********************************
40069**
40070** Use the flock() system call to do file locking.
40071**
40072** flock() locking is like dot-file locking in that the various
40073** fine-grain locking levels supported by SQLite are collapsed into
40074** a single exclusive lock. In other words, SHARED, RESERVED, and
40075** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
40076** still works when you do this, but concurrency is reduced since
40077** only a single process can be reading the database at a time.
40078**
40079** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
40080*/
40081#if SQLITE_ENABLE_LOCKING_STYLE
40082
40083/*
40084** Retry flock() calls that fail with EINTR
40085*/
40086#ifdef EINTR
40087static int robust_flock(int fd, int op){
40088 int rc;
40089 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
40090 return rc;
40091}
40092#else
40093# define robust_flock(a,b) flock(a,b)
40094#endif
40095
40096
40097/*
40098** This routine checks if there is a RESERVED lock held on the specified
40099** file by this or any other process. If such a lock is held, set *pResOut
40100** to a non-zero value otherwise *pResOut is set to zero. The return value
40101** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40102*/
40103static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
40104 int rc = SQLITE_OK;
40105 int reserved = 0;
40106 unixFile *pFile = (unixFile*)id;
40107
40108 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40109
40110 assert( pFile );
40111
40112 /* Check if a thread in this process holds such a lock */
40113 if( pFile->eFileLock>SHARED_LOCK ){
40114 reserved = 1;
40115 }
40116
40117 /* Otherwise see if some other process holds it. */
40118 if( !reserved ){
40119 /* attempt to get the lock */
40120 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
40121 if( !lrc ){
40122 /* got the lock, unlock it */
40123 lrc = robust_flock(pFile->h, LOCK_UN);
40124 if ( lrc ) {
40125 int tErrno = errno;
40126 /* unlock failed with an error */
40127 lrc = SQLITE_IOERR_UNLOCK;
40128 storeLastErrno(pFile, tErrno);
40129 rc = lrc;
40130 }
40131 } else {
40132 int tErrno = errno;
40133 reserved = 1;
40134 /* someone else might have it reserved */
40135 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
40136 if( IS_LOCK_ERROR(lrc) ){
40137 storeLastErrno(pFile, tErrno);
40138 rc = lrc;
40139 }
40140 }
40141 }
40142 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
40143
40144#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
40145 if( (rc & 0xff) == SQLITE_IOERR ){
40146 rc = SQLITE_OK;
40147 reserved=1;
40148 }
40149#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
40150 *pResOut = reserved;
40151 return rc;
40152}
40153
40154/*
40155** Lock the file with the lock specified by parameter eFileLock - one
40156** of the following:
40157**
40158** (1) SHARED_LOCK
40159** (2) RESERVED_LOCK
40160** (3) PENDING_LOCK
40161** (4) EXCLUSIVE_LOCK
40162**
40163** Sometimes when requesting one lock state, additional lock states
40164** are inserted in between. The locking might fail on one of the later
40165** transitions leaving the lock state different from what it started but
40166** still short of its goal. The following chart shows the allowed
40167** transitions and the inserted intermediate states:
40168**
40169** UNLOCKED -> SHARED
40170** SHARED -> RESERVED
40171** SHARED -> (PENDING) -> EXCLUSIVE
40172** RESERVED -> (PENDING) -> EXCLUSIVE
40173** PENDING -> EXCLUSIVE
40174**
40175** flock() only really support EXCLUSIVE locks. We track intermediate
40176** lock states in the sqlite3_file structure, but all locks SHARED or
40177** above are really EXCLUSIVE locks and exclude all other processes from
40178** access the file.
40179**
40180** This routine will only increase a lock. Use the sqlite3OsUnlock()
40181** routine to lower a locking level.
40182*/
40183static int flockLock(sqlite3_file *id, int eFileLock) {
40184 int rc = SQLITE_OK;
40185 unixFile *pFile = (unixFile*)id;
40186
40187 assert( pFile );
40188
40189 /* if we already have a lock, it is exclusive.
40190 ** Just adjust level and punt on outta here. */
40191 if (pFile->eFileLock > NO_LOCK) {
40192 pFile->eFileLock = eFileLock;
40193 return SQLITE_OK;
40194 }
40195
40196 /* grab an exclusive lock */
40197
40198 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
40199 int tErrno = errno;
40200 /* didn't get, must be busy */
40201 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
40202 if( IS_LOCK_ERROR(rc) ){
40203 storeLastErrno(pFile, tErrno);
40204 }
40205 } else {
40206 /* got it, set the type and return ok */
40207 pFile->eFileLock = eFileLock;
40208 }
40209 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
40210 rc==SQLITE_OK ? "ok" : "failed"));
40211#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
40212 if( (rc & 0xff) == SQLITE_IOERR ){
40213 rc = SQLITE_BUSY;
40214 }
40215#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
40216 return rc;
40217}
40218
40219
40220/*
40221** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40222** must be either NO_LOCK or SHARED_LOCK.
40223**
40224** If the locking level of the file descriptor is already at or below
40225** the requested locking level, this routine is a no-op.
40226*/
40227static int flockUnlock(sqlite3_file *id, int eFileLock) {
40228 unixFile *pFile = (unixFile*)id;
40229
40230 assert( pFile );
40231 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
40232 pFile->eFileLock, osGetpid(0)));
40233 assert( eFileLock<=SHARED_LOCK );
40234
40235 /* no-op if possible */
40236 if( pFile->eFileLock==eFileLock ){
40237 return SQLITE_OK;
40238 }
40239
40240 /* shared can just be set because we always have an exclusive */
40241 if (eFileLock==SHARED_LOCK) {
40242 pFile->eFileLock = eFileLock;
40243 return SQLITE_OK;
40244 }
40245
40246 /* no, really, unlock. */
40247 if( robust_flock(pFile->h, LOCK_UN) ){
40248#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
40249 return SQLITE_OK;
40250#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
40251 return SQLITE_IOERR_UNLOCK;
40252 }else{
40253 pFile->eFileLock = NO_LOCK;
40254 return SQLITE_OK;
40255 }
40256}
40257
40258/*
40259** Close a file.
40260*/
40261static int flockClose(sqlite3_file *id) {
40262 assert( id!=0 );
40263 flockUnlock(id, NO_LOCK);
40264 return closeUnixFile(id);
40265}
40266
40267#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
40268
40269/******************* End of the flock lock implementation *********************
40270******************************************************************************/
40271
40272/******************************************************************************
40273************************ Begin Named Semaphore Locking ************************
40274**
40275** Named semaphore locking is only supported on VxWorks.
40276**
40277** Semaphore locking is like dot-lock and flock in that it really only
40278** supports EXCLUSIVE locking. Only a single process can read or write
40279** the database file at a time. This reduces potential concurrency, but
40280** makes the lock implementation much easier.
40281*/
40282#if OS_VXWORKS
40283
40284/*
40285** This routine checks if there is a RESERVED lock held on the specified
40286** file by this or any other process. If such a lock is held, set *pResOut
40287** to a non-zero value otherwise *pResOut is set to zero. The return value
40288** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40289*/
40290static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
40291 int rc = SQLITE_OK;
40292 int reserved = 0;
40293 unixFile *pFile = (unixFile*)id;
40294
40295 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40296
40297 assert( pFile );
40298
40299 /* Check if a thread in this process holds such a lock */
40300 if( pFile->eFileLock>SHARED_LOCK ){
40301 reserved = 1;
40302 }
40303
40304 /* Otherwise see if some other process holds it. */
40305 if( !reserved ){
40306 sem_t *pSem = pFile->pInode->pSem;
40307
40308 if( sem_trywait(pSem)==-1 ){
40309 int tErrno = errno;
40310 if( EAGAIN != tErrno ){
40311 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
40312 storeLastErrno(pFile, tErrno);
40313 } else {
40314 /* someone else has the lock when we are in NO_LOCK */
40315 reserved = (pFile->eFileLock < SHARED_LOCK);
40316 }
40317 }else{
40318 /* we could have it if we want it */
40319 sem_post(pSem);
40320 }
40321 }
40322 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
40323
40324 *pResOut = reserved;
40325 return rc;
40326}
40327
40328/*
40329** Lock the file with the lock specified by parameter eFileLock - one
40330** of the following:
40331**
40332** (1) SHARED_LOCK
40333** (2) RESERVED_LOCK
40334** (3) PENDING_LOCK
40335** (4) EXCLUSIVE_LOCK
40336**
40337** Sometimes when requesting one lock state, additional lock states
40338** are inserted in between. The locking might fail on one of the later
40339** transitions leaving the lock state different from what it started but
40340** still short of its goal. The following chart shows the allowed
40341** transitions and the inserted intermediate states:
40342**
40343** UNLOCKED -> SHARED
40344** SHARED -> RESERVED
40345** SHARED -> (PENDING) -> EXCLUSIVE
40346** RESERVED -> (PENDING) -> EXCLUSIVE
40347** PENDING -> EXCLUSIVE
40348**
40349** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
40350** lock states in the sqlite3_file structure, but all locks SHARED or
40351** above are really EXCLUSIVE locks and exclude all other processes from
40352** access the file.
40353**
40354** This routine will only increase a lock. Use the sqlite3OsUnlock()
40355** routine to lower a locking level.
40356*/
40357static int semXLock(sqlite3_file *id, int eFileLock) {
40358 unixFile *pFile = (unixFile*)id;
40359 sem_t *pSem = pFile->pInode->pSem;
40360 int rc = SQLITE_OK;
40361
40362 /* if we already have a lock, it is exclusive.
40363 ** Just adjust level and punt on outta here. */
40364 if (pFile->eFileLock > NO_LOCK) {
40365 pFile->eFileLock = eFileLock;
40366 rc = SQLITE_OK;
40367 goto sem_end_lock;
40368 }
40369
40370 /* lock semaphore now but bail out when already locked. */
40371 if( sem_trywait(pSem)==-1 ){
40372 rc = SQLITE_BUSY;
40373 goto sem_end_lock;
40374 }
40375
40376 /* got it, set the type and return ok */
40377 pFile->eFileLock = eFileLock;
40378
40379 sem_end_lock:
40380 return rc;
40381}
40382
40383/*
40384** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40385** must be either NO_LOCK or SHARED_LOCK.
40386**
40387** If the locking level of the file descriptor is already at or below
40388** the requested locking level, this routine is a no-op.
40389*/
40390static int semXUnlock(sqlite3_file *id, int eFileLock) {
40391 unixFile *pFile = (unixFile*)id;
40392 sem_t *pSem = pFile->pInode->pSem;
40393
40394 assert( pFile );
40395 assert( pSem );
40396 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
40397 pFile->eFileLock, osGetpid(0)));
40398 assert( eFileLock<=SHARED_LOCK );
40399
40400 /* no-op if possible */
40401 if( pFile->eFileLock==eFileLock ){
40402 return SQLITE_OK;
40403 }
40404
40405 /* shared can just be set because we always have an exclusive */
40406 if (eFileLock==SHARED_LOCK) {
40407 pFile->eFileLock = eFileLock;
40408 return SQLITE_OK;
40409 }
40410
40411 /* no, really unlock. */
40412 if ( sem_post(pSem)==-1 ) {
40413 int rc, tErrno = errno;
40414 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
40415 if( IS_LOCK_ERROR(rc) ){
40416 storeLastErrno(pFile, tErrno);
40417 }
40418 return rc;
40419 }
40420 pFile->eFileLock = NO_LOCK;
40421 return SQLITE_OK;
40422}
40423
40424/*
40425 ** Close a file.
40426 */
40427static int semXClose(sqlite3_file *id) {
40428 if( id ){
40429 unixFile *pFile = (unixFile*)id;
40430 semXUnlock(id, NO_LOCK);
40431 assert( pFile );
40432 assert( unixFileMutexNotheld(pFile) );
40433 unixEnterMutex();
40434 releaseInodeInfo(pFile);
40435 unixLeaveMutex();
40436 closeUnixFile(id);
40437 }
40438 return SQLITE_OK;
40439}
40440
40441#endif /* OS_VXWORKS */
40442/*
40443** Named semaphore locking is only available on VxWorks.
40444**
40445*************** End of the named semaphore lock implementation ****************
40446******************************************************************************/
40447
40448
40449/******************************************************************************
40450*************************** Begin AFP Locking *********************************
40451**
40452** AFP is the Apple Filing Protocol. AFP is a network filesystem found
40453** on Apple Macintosh computers - both OS9 and OSX.
40454**
40455** Third-party implementations of AFP are available. But this code here
40456** only works on OSX.
40457*/
40458
40459#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
40460/*
40461** The afpLockingContext structure contains all afp lock specific state
40462*/
40463typedef struct afpLockingContext afpLockingContext;
40464struct afpLockingContext {
40465 int reserved;
40466 const char *dbPath; /* Name of the open file */
40467};
40468
40469struct ByteRangeLockPB2
40470{
40471 unsigned long long offset; /* offset to first byte to lock */
40472 unsigned long long length; /* nbr of bytes to lock */
40473 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
40474 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
40475 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
40476 int fd; /* file desc to assoc this lock with */
40477};
40478
40479#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
40480
40481/*
40482** This is a utility for setting or clearing a bit-range lock on an
40483** AFP filesystem.
40484**
40485** Return SQLITE_OK on success, SQLITE_BUSY on failure.
40486*/
40487static int afpSetLock(
40488 const char *path, /* Name of the file to be locked or unlocked */
40489 unixFile *pFile, /* Open file descriptor on path */
40490 unsigned long long offset, /* First byte to be locked */
40491 unsigned long long length, /* Number of bytes to lock */
40492 int setLockFlag /* True to set lock. False to clear lock */
40493){
40494 struct ByteRangeLockPB2 pb;
40495 int err;
40496
40497 pb.unLockFlag = setLockFlag ? 0 : 1;
40498 pb.startEndFlag = 0;
40499 pb.offset = offset;
40500 pb.length = length;
40501 pb.fd = pFile->h;
40502
40503 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
40504 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
40505 offset, length));
40506 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
40507 if ( err==-1 ) {
40508 int rc;
40509 int tErrno = errno;
40510 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
40511 path, tErrno, strerror(tErrno)));
40512#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
40513 rc = SQLITE_BUSY;
40514#else
40515 rc = sqliteErrorFromPosixError(tErrno,
40516 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
40517#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
40518 if( IS_LOCK_ERROR(rc) ){
40519 storeLastErrno(pFile, tErrno);
40520 }
40521 return rc;
40522 } else {
40523 return SQLITE_OK;
40524 }
40525}
40526
40527/*
40528** This routine checks if there is a RESERVED lock held on the specified
40529** file by this or any other process. If such a lock is held, set *pResOut
40530** to a non-zero value otherwise *pResOut is set to zero. The return value
40531** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40532*/
40533static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
40534 int rc = SQLITE_OK;
40535 int reserved = 0;
40536 unixFile *pFile = (unixFile*)id;
40537 afpLockingContext *context;
40538
40539 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40540
40541 assert( pFile );
40542 context = (afpLockingContext *) pFile->lockingContext;
40543 if( context->reserved ){
40544 *pResOut = 1;
40545 return SQLITE_OK;
40546 }
40547 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
40548 /* Check if a thread in this process holds such a lock */
40549 if( pFile->pInode->eFileLock>SHARED_LOCK ){
40550 reserved = 1;
40551 }
40552
40553 /* Otherwise see if some other process holds it.
40554 */
40555 if( !reserved ){
40556 /* lock the RESERVED byte */
40557 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40558 if( SQLITE_OK==lrc ){
40559 /* if we succeeded in taking the reserved lock, unlock it to restore
40560 ** the original state */
40561 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
40562 } else {
40563 /* if we failed to get the lock then someone else must have it */
40564 reserved = 1;
40565 }
40566 if( IS_LOCK_ERROR(lrc) ){
40567 rc=lrc;
40568 }
40569 }
40570
40571 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
40572 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
40573
40574 *pResOut = reserved;
40575 return rc;
40576}
40577
40578/*
40579** Lock the file with the lock specified by parameter eFileLock - one
40580** of the following:
40581**
40582** (1) SHARED_LOCK
40583** (2) RESERVED_LOCK
40584** (3) PENDING_LOCK
40585** (4) EXCLUSIVE_LOCK
40586**
40587** Sometimes when requesting one lock state, additional lock states
40588** are inserted in between. The locking might fail on one of the later
40589** transitions leaving the lock state different from what it started but
40590** still short of its goal. The following chart shows the allowed
40591** transitions and the inserted intermediate states:
40592**
40593** UNLOCKED -> SHARED
40594** SHARED -> RESERVED
40595** SHARED -> (PENDING) -> EXCLUSIVE
40596** RESERVED -> (PENDING) -> EXCLUSIVE
40597** PENDING -> EXCLUSIVE
40598**
40599** This routine will only increase a lock. Use the sqlite3OsUnlock()
40600** routine to lower a locking level.
40601*/
40602static int afpLock(sqlite3_file *id, int eFileLock){
40603 int rc = SQLITE_OK;
40604 unixFile *pFile = (unixFile*)id;
40605 unixInodeInfo *pInode = pFile->pInode;
40606 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
40607
40608 assert( pFile );
40609 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
40610 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40611 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
40612
40613 /* If there is already a lock of this type or more restrictive on the
40614 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
40615 ** unixEnterMutex() hasn't been called yet.
40616 */
40617 if( pFile->eFileLock>=eFileLock ){
40618 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
40619 azFileLock(eFileLock)));
40620 return SQLITE_OK;
40621 }
40622
40623 /* Make sure the locking sequence is correct
40624 ** (1) We never move from unlocked to anything higher than shared lock.
40625 ** (2) SQLite never explicitly requests a pending lock.
40626 ** (3) A shared lock is always held when a reserve lock is requested.
40627 */
40628 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
40629 assert( eFileLock!=PENDING_LOCK );
40630 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
40631
40632 /* This mutex is needed because pFile->pInode is shared across threads
40633 */
40634 pInode = pFile->pInode;
40635 sqlite3_mutex_enter(pInode->pLockMutex);
40636
40637 /* If some thread using this PID has a lock via a different unixFile*
40638 ** handle that precludes the requested lock, return BUSY.
40639 */
40640 if( (pFile->eFileLock!=pInode->eFileLock &&
40641 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
40642 ){
40643 rc = SQLITE_BUSY;
40644 goto afp_end_lock;
40645 }
40646
40647 /* If a SHARED lock is requested, and some thread using this PID already
40648 ** has a SHARED or RESERVED lock, then increment reference counts and
40649 ** return SQLITE_OK.
40650 */
40651 if( eFileLock==SHARED_LOCK &&
40652 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
40653 assert( eFileLock==SHARED_LOCK );
40654 assert( pFile->eFileLock==0 );
40655 assert( pInode->nShared>0 );
40656 pFile->eFileLock = SHARED_LOCK;
40657 pInode->nShared++;
40658 pInode->nLock++;
40659 goto afp_end_lock;
40660 }
40661
40662 /* A PENDING lock is needed before acquiring a SHARED lock and before
40663 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
40664 ** be released.
40665 */
40666 if( eFileLock==SHARED_LOCK
40667 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
40668 ){
40669 int failed;
40670 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
40671 if (failed) {
40672 rc = failed;
40673 goto afp_end_lock;
40674 }
40675 }
40676
40677 /* If control gets to this point, then actually go ahead and make
40678 ** operating system calls for the specified lock.
40679 */
40680 if( eFileLock==SHARED_LOCK ){
40681 int lrc1, lrc2, lrc1Errno = 0;
40682 long lk, mask;
40683
40684 assert( pInode->nShared==0 );
40685 assert( pInode->eFileLock==0 );
40686
40687 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
40688 /* Now get the read-lock SHARED_LOCK */
40689 /* note that the quality of the randomness doesn't matter that much */
40690 lk = random();
40691 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
40692 lrc1 = afpSetLock(context->dbPath, pFile,
40693 SHARED_FIRST+pInode->sharedByte, 1, 1);
40694 if( IS_LOCK_ERROR(lrc1) ){
40695 lrc1Errno = pFile->lastErrno;
40696 }
40697 /* Drop the temporary PENDING lock */
40698 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40699
40700 if( IS_LOCK_ERROR(lrc1) ) {
40701 storeLastErrno(pFile, lrc1Errno);
40702 rc = lrc1;
40703 goto afp_end_lock;
40704 } else if( IS_LOCK_ERROR(lrc2) ){
40705 rc = lrc2;
40706 goto afp_end_lock;
40707 } else if( lrc1 != SQLITE_OK ) {
40708 rc = lrc1;
40709 } else {
40710 pFile->eFileLock = SHARED_LOCK;
40711 pInode->nLock++;
40712 pInode->nShared = 1;
40713 }
40714 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
40715 /* We are trying for an exclusive lock but another thread in this
40716 ** same process is still holding a shared lock. */
40717 rc = SQLITE_BUSY;
40718 }else{
40719 /* The request was for a RESERVED or EXCLUSIVE lock. It is
40720 ** assumed that there is a SHARED or greater lock on the file
40721 ** already.
40722 */
40723 int failed = 0;
40724 assert( 0!=pFile->eFileLock );
40725 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
40726 /* Acquire a RESERVED lock */
40727 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40728 if( !failed ){
40729 context->reserved = 1;
40730 }
40731 }
40732 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
40733 /* Acquire an EXCLUSIVE lock */
40734
40735 /* Remove the shared lock before trying the range. we'll need to
40736 ** reestablish the shared lock if we can't get the afpUnlock
40737 */
40738 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
40739 pInode->sharedByte, 1, 0)) ){
40740 int failed2 = SQLITE_OK;
40741 /* now attempt to get the exclusive lock range */
40742 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
40743 SHARED_SIZE, 1);
40744 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
40745 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
40746 /* Can't reestablish the shared lock. Sqlite can't deal, this is
40747 ** a critical I/O error
40748 */
40749 rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 :
40750 SQLITE_IOERR_LOCK;
40751 goto afp_end_lock;
40752 }
40753 }else{
40754 rc = failed;
40755 }
40756 }
40757 if( failed ){
40758 rc = failed;
40759 }
40760 }
40761
40762 if( rc==SQLITE_OK ){
40763 pFile->eFileLock = eFileLock;
40764 pInode->eFileLock = eFileLock;
40765 }else if( eFileLock==EXCLUSIVE_LOCK ){
40766 pFile->eFileLock = PENDING_LOCK;
40767 pInode->eFileLock = PENDING_LOCK;
40768 }
40769
40770afp_end_lock:
40771 sqlite3_mutex_leave(pInode->pLockMutex);
40772 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
40773 rc==SQLITE_OK ? "ok" : "failed"));
40774 return rc;
40775}
40776
40777/*
40778** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40779** must be either NO_LOCK or SHARED_LOCK.
40780**
40781** If the locking level of the file descriptor is already at or below
40782** the requested locking level, this routine is a no-op.
40783*/
40784static int afpUnlock(sqlite3_file *id, int eFileLock) {
40785 int rc = SQLITE_OK;
40786 unixFile *pFile = (unixFile*)id;
40787 unixInodeInfo *pInode;
40788 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
40789 int skipShared = 0;
40790#ifdef SQLITE_TEST
40791 int h = pFile->h;
40792#endif
40793
40794 assert( pFile );
40795 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
40796 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40797 osGetpid(0)));
40798
40799 assert( eFileLock<=SHARED_LOCK );
40800 if( pFile->eFileLock<=eFileLock ){
40801 return SQLITE_OK;
40802 }
40803 pInode = pFile->pInode;
40804 sqlite3_mutex_enter(pInode->pLockMutex);
40805 assert( pInode->nShared!=0 );
40806 if( pFile->eFileLock>SHARED_LOCK ){
40807 assert( pInode->eFileLock==pFile->eFileLock );
40808 SimulateIOErrorBenign(1);
40809 SimulateIOError( h=(-1) )
40810 SimulateIOErrorBenign(0);
40811
40812#ifdef SQLITE_DEBUG
40813 /* When reducing a lock such that other processes can start
40814 ** reading the database file again, make sure that the
40815 ** transaction counter was updated if any part of the database
40816 ** file changed. If the transaction counter is not updated,
40817 ** other connections to the same file might not realize that
40818 ** the file has changed and hence might not know to flush their
40819 ** cache. The use of a stale cache can lead to database corruption.
40820 */
40821 assert( pFile->inNormalWrite==0
40822 || pFile->dbUpdate==0
40823 || pFile->transCntrChng==1 );
40824 pFile->inNormalWrite = 0;
40825#endif
40826
40827 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
40828 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
40829 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
40830 /* only re-establish the shared lock if necessary */
40831 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40832 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
40833 } else {
40834 skipShared = 1;
40835 }
40836 }
40837 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
40838 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40839 }
40840 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
40841 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
40842 if( !rc ){
40843 context->reserved = 0;
40844 }
40845 }
40846 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
40847 pInode->eFileLock = SHARED_LOCK;
40848 }
40849 }
40850 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
40851
40852 /* Decrement the shared lock counter. Release the lock using an
40853 ** OS call only when all threads in this same process have released
40854 ** the lock.
40855 */
40856 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40857 pInode->nShared--;
40858 if( pInode->nShared==0 ){
40859 SimulateIOErrorBenign(1);
40860 SimulateIOError( h=(-1) )
40861 SimulateIOErrorBenign(0);
40862 if( !skipShared ){
40863 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
40864 }
40865 if( !rc ){
40866 pInode->eFileLock = NO_LOCK;
40867 pFile->eFileLock = NO_LOCK;
40868 }
40869 }
40870 if( rc==SQLITE_OK ){
40871 pInode->nLock--;
40872 assert( pInode->nLock>=0 );
40873 if( pInode->nLock==0 ) closePendingFds(pFile);
40874 }
40875 }
40876
40877 sqlite3_mutex_leave(pInode->pLockMutex);
40878 if( rc==SQLITE_OK ){
40879 pFile->eFileLock = eFileLock;
40880 }
40881 return rc;
40882}
40883
40884/*
40885** Close a file & cleanup AFP specific locking context
40886*/
40887static int afpClose(sqlite3_file *id) {
40888 int rc = SQLITE_OK;
40889 unixFile *pFile = (unixFile*)id;
40890 assert( id!=0 );
40891 afpUnlock(id, NO_LOCK);
40892 assert( unixFileMutexNotheld(pFile) );
40893 unixEnterMutex();
40894 if( pFile->pInode ){
40895 unixInodeInfo *pInode = pFile->pInode;
40896 sqlite3_mutex_enter(pInode->pLockMutex);
40897 if( pInode->nLock ){
40898 /* If there are outstanding locks, do not actually close the file just
40899 ** yet because that would clear those locks. Instead, add the file
40900 ** descriptor to pInode->aPending. It will be automatically closed when
40901 ** the last lock is cleared.
40902 */
40903 setPendingFd(pFile);
40904 }
40905 sqlite3_mutex_leave(pInode->pLockMutex);
40906 }
40907 releaseInodeInfo(pFile);
40908 sqlite3_free(pFile->lockingContext);
40909 rc = closeUnixFile(id);
40910 unixLeaveMutex();
40911 return rc;
40912}
40913
40914#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
40915/*
40916** The code above is the AFP lock implementation. The code is specific
40917** to MacOSX and does not work on other unix platforms. No alternative
40918** is available. If you don't compile for a mac, then the "unix-afp"
40919** VFS is not available.
40920**
40921********************* End of the AFP lock implementation **********************
40922******************************************************************************/
40923
40924/******************************************************************************
40925*************************** Begin NFS Locking ********************************/
40926
40927#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
40928/*
40929 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40930 ** must be either NO_LOCK or SHARED_LOCK.
40931 **
40932 ** If the locking level of the file descriptor is already at or below
40933 ** the requested locking level, this routine is a no-op.
40934 */
40935static int nfsUnlock(sqlite3_file *id, int eFileLock){
40936 return posixUnlock(id, eFileLock, 1);
40937}
40938
40939#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
40940/*
40941** The code above is the NFS lock implementation. The code is specific
40942** to MacOSX and does not work on other unix platforms. No alternative
40943** is available.
40944**
40945********************* End of the NFS lock implementation **********************
40946******************************************************************************/
40947
40948/******************************************************************************
40949**************** Non-locking sqlite3_file methods *****************************
40950**
40951** The next division contains implementations for all methods of the
40952** sqlite3_file object other than the locking methods. The locking
40953** methods were defined in divisions above (one locking method per
40954** division). Those methods that are common to all locking modes
40955** are gather together into this division.
40956*/
40957
40958/*
40959** Seek to the offset passed as the second argument, then read cnt
40960** bytes into pBuf. Return the number of bytes actually read.
40961**
40962** To avoid stomping the errno value on a failed read the lastErrno value
40963** is set before returning.
40964*/
40965static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
40966 int got;
40967 int prior = 0;
40968#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
40969 i64 newOffset;
40970#endif
40971 TIMER_START;
40972 assert( cnt==(cnt&0x1ffff) );
40973 assert( id->h>2 );
40974 do{
40975#if defined(USE_PREAD)
40976 got = osPread(id->h, pBuf, cnt, offset);
40977 SimulateIOError( got = -1 );
40978#elif defined(USE_PREAD64)
40979 got = osPread64(id->h, pBuf, cnt, offset);
40980 SimulateIOError( got = -1 );
40981#else
40982 newOffset = lseek(id->h, offset, SEEK_SET);
40983 SimulateIOError( newOffset = -1 );
40984 if( newOffset<0 ){
40985 storeLastErrno((unixFile*)id, errno);
40986 return -1;
40987 }
40988 got = osRead(id->h, pBuf, cnt);
40989#endif
40990 if( got==cnt ) break;
40991 if( got<0 ){
40992 if( errno==EINTR ){ got = 1; continue; }
40993 prior = 0;
40994 storeLastErrno(pFile: (unixFile*)id, errno);
40995 break;
40996 }else if( got>0 ){
40997 cnt -= got;
40998 offset += got;
40999 prior += got;
41000 pBuf = (void*)(got + (char*)pBuf);
41001 }
41002 }while( got>0 );
41003 TIMER_END;
41004 OSTRACE(("READ %-3d %5d %7lld %llu\n",
41005 id->h, got+prior, offset-prior, TIMER_ELAPSED));
41006 return got+prior;
41007}
41008
41009/*
41010** Read data from a file into a buffer. Return SQLITE_OK if all
41011** bytes were read successfully and SQLITE_IOERR if anything goes
41012** wrong.
41013*/
41014static int unixRead(
41015 sqlite3_file *id,
41016 void *pBuf,
41017 int amt,
41018 sqlite3_int64 offset
41019){
41020 unixFile *pFile = (unixFile *)id;
41021 int got;
41022 assert( id );
41023 assert( offset>=0 );
41024 assert( amt>0 );
41025
41026 /* If this is a database file (not a journal, super-journal or temp
41027 ** file), the bytes in the locking range should never be read or written. */
41028#if 0
41029 assert( pFile->pPreallocatedUnused==0
41030 || offset>=PENDING_BYTE+512
41031 || offset+amt<=PENDING_BYTE
41032 );
41033#endif
41034
41035#if SQLITE_MAX_MMAP_SIZE>0
41036 /* Deal with as much of this read request as possible by transferring
41037 ** data from the memory mapping using memcpy(). */
41038 if( offset<pFile->mmapSize ){
41039 if( offset+amt <= pFile->mmapSize ){
41040 memcpy(dest: pBuf, src: &((u8 *)(pFile->pMapRegion))[offset], n: amt);
41041 return SQLITE_OK;
41042 }else{
41043 int nCopy = pFile->mmapSize - offset;
41044 memcpy(dest: pBuf, src: &((u8 *)(pFile->pMapRegion))[offset], n: nCopy);
41045 pBuf = &((u8 *)pBuf)[nCopy];
41046 amt -= nCopy;
41047 offset += nCopy;
41048 }
41049 }
41050#endif
41051
41052 got = seekAndRead(id: pFile, offset, pBuf, cnt: amt);
41053 if( got==amt ){
41054 return SQLITE_OK;
41055 }else if( got<0 ){
41056 /* pFile->lastErrno has been set by seekAndRead().
41057 ** Usually we return SQLITE_IOERR_READ here, though for some
41058 ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The
41059 ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT
41060 ** prior to returning to the application by the sqlite3ApiExit()
41061 ** routine.
41062 */
41063 switch( pFile->lastErrno ){
41064 case ERANGE:
41065 case EIO:
41066#ifdef ENXIO
41067 case ENXIO:
41068#endif
41069#ifdef EDEVERR
41070 case EDEVERR:
41071#endif
41072 return SQLITE_IOERR_CORRUPTFS;
41073 }
41074 return SQLITE_IOERR_READ;
41075 }else{
41076 storeLastErrno(pFile, error: 0); /* not a system error */
41077 /* Unread parts of the buffer must be zero-filled */
41078 memset(s: &((char*)pBuf)[got], c: 0, n: amt-got);
41079 return SQLITE_IOERR_SHORT_READ;
41080 }
41081}
41082
41083/*
41084** Attempt to seek the file-descriptor passed as the first argument to
41085** absolute offset iOff, then attempt to write nBuf bytes of data from
41086** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
41087** return the actual number of bytes written (which may be less than
41088** nBuf).
41089*/
41090static int seekAndWriteFd(
41091 int fd, /* File descriptor to write to */
41092 i64 iOff, /* File offset to begin writing at */
41093 const void *pBuf, /* Copy data from this buffer to the file */
41094 int nBuf, /* Size of buffer pBuf in bytes */
41095 int *piErrno /* OUT: Error number if error occurs */
41096){
41097 int rc = 0; /* Value returned by system call */
41098
41099 assert( nBuf==(nBuf&0x1ffff) );
41100 assert( fd>2 );
41101 assert( piErrno!=0 );
41102 nBuf &= 0x1ffff;
41103 TIMER_START;
41104
41105#if defined(USE_PREAD)
41106 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
41107#elif defined(USE_PREAD64)
41108 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
41109#else
41110 do{
41111 i64 iSeek = lseek(fd, iOff, SEEK_SET);
41112 SimulateIOError( iSeek = -1 );
41113 if( iSeek<0 ){
41114 rc = -1;
41115 break;
41116 }
41117 rc = osWrite(fd, pBuf, nBuf);
41118 }while( rc<0 && errno==EINTR );
41119#endif
41120
41121 TIMER_END;
41122 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
41123
41124 if( rc<0 ) *piErrno = errno;
41125 return rc;
41126}
41127
41128
41129/*
41130** Seek to the offset in id->offset then read cnt bytes into pBuf.
41131** Return the number of bytes actually read. Update the offset.
41132**
41133** To avoid stomping the errno value on a failed write the lastErrno value
41134** is set before returning.
41135*/
41136static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
41137 return seekAndWriteFd(fd: id->h, iOff: offset, pBuf, nBuf: cnt, piErrno: &id->lastErrno);
41138}
41139
41140
41141/*
41142** Write data from a buffer into a file. Return SQLITE_OK on success
41143** or some other error code on failure.
41144*/
41145static int unixWrite(
41146 sqlite3_file *id,
41147 const void *pBuf,
41148 int amt,
41149 sqlite3_int64 offset
41150){
41151 unixFile *pFile = (unixFile*)id;
41152 int wrote = 0;
41153 assert( id );
41154 assert( amt>0 );
41155
41156 /* If this is a database file (not a journal, super-journal or temp
41157 ** file), the bytes in the locking range should never be read or written. */
41158#if 0
41159 assert( pFile->pPreallocatedUnused==0
41160 || offset>=PENDING_BYTE+512
41161 || offset+amt<=PENDING_BYTE
41162 );
41163#endif
41164
41165#ifdef SQLITE_DEBUG
41166 /* If we are doing a normal write to a database file (as opposed to
41167 ** doing a hot-journal rollback or a write to some file other than a
41168 ** normal database file) then record the fact that the database
41169 ** has changed. If the transaction counter is modified, record that
41170 ** fact too.
41171 */
41172 if( pFile->inNormalWrite ){
41173 pFile->dbUpdate = 1; /* The database has been modified */
41174 if( offset<=24 && offset+amt>=27 ){
41175 int rc;
41176 char oldCntr[4];
41177 SimulateIOErrorBenign(1);
41178 rc = seekAndRead(pFile, 24, oldCntr, 4);
41179 SimulateIOErrorBenign(0);
41180 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
41181 pFile->transCntrChng = 1; /* The transaction counter has changed */
41182 }
41183 }
41184 }
41185#endif
41186
41187#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
41188 /* Deal with as much of this write request as possible by transferring
41189 ** data from the memory mapping using memcpy(). */
41190 if( offset<pFile->mmapSize ){
41191 if( offset+amt <= pFile->mmapSize ){
41192 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41193 return SQLITE_OK;
41194 }else{
41195 int nCopy = pFile->mmapSize - offset;
41196 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
41197 pBuf = &((u8 *)pBuf)[nCopy];
41198 amt -= nCopy;
41199 offset += nCopy;
41200 }
41201 }
41202#endif
41203
41204 while( (wrote = seekAndWrite(id: pFile, offset, pBuf, cnt: amt))<amt && wrote>0 ){
41205 amt -= wrote;
41206 offset += wrote;
41207 pBuf = &((char*)pBuf)[wrote];
41208 }
41209 SimulateIOError(( wrote=(-1), amt=1 ));
41210 SimulateDiskfullError(( wrote=0, amt=1 ));
41211
41212 if( amt>wrote ){
41213 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
41214 /* lastErrno set by seekAndWrite */
41215 return SQLITE_IOERR_WRITE;
41216 }else{
41217 storeLastErrno(pFile, error: 0); /* not a system error */
41218 return SQLITE_FULL;
41219 }
41220 }
41221
41222 return SQLITE_OK;
41223}
41224
41225#ifdef SQLITE_TEST
41226/*
41227** Count the number of fullsyncs and normal syncs. This is used to test
41228** that syncs and fullsyncs are occurring at the right times.
41229*/
41230SQLITE_API int sqlite3_sync_count = 0;
41231SQLITE_API int sqlite3_fullsync_count = 0;
41232#endif
41233
41234/*
41235** We do not trust systems to provide a working fdatasync(). Some do.
41236** Others do no. To be safe, we will stick with the (slightly slower)
41237** fsync(). If you know that your system does support fdatasync() correctly,
41238** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
41239*/
41240#if !defined(fdatasync) && !HAVE_FDATASYNC
41241# define fdatasync fsync
41242#endif
41243
41244/*
41245** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
41246** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
41247** only available on Mac OS X. But that could change.
41248*/
41249#ifdef F_FULLFSYNC
41250# define HAVE_FULLFSYNC 1
41251#else
41252# define HAVE_FULLFSYNC 0
41253#endif
41254
41255
41256/*
41257** The fsync() system call does not work as advertised on many
41258** unix systems. The following procedure is an attempt to make
41259** it work better.
41260**
41261** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
41262** for testing when we want to run through the test suite quickly.
41263** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
41264** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
41265** or power failure will likely corrupt the database file.
41266**
41267** SQLite sets the dataOnly flag if the size of the file is unchanged.
41268** The idea behind dataOnly is that it should only write the file content
41269** to disk, not the inode. We only set dataOnly if the file size is
41270** unchanged since the file size is part of the inode. However,
41271** Ted Ts'o tells us that fdatasync() will also write the inode if the
41272** file size has changed. The only real difference between fdatasync()
41273** and fsync(), Ted tells us, is that fdatasync() will not flush the
41274** inode if the mtime or owner or other inode attributes have changed.
41275** We only care about the file size, not the other file attributes, so
41276** as far as SQLite is concerned, an fdatasync() is always adequate.
41277** So, we always use fdatasync() if it is available, regardless of
41278** the value of the dataOnly flag.
41279*/
41280static int full_fsync(int fd, int fullSync, int dataOnly){
41281 int rc;
41282
41283 /* The following "ifdef/elif/else/" block has the same structure as
41284 ** the one below. It is replicated here solely to avoid cluttering
41285 ** up the real code with the UNUSED_PARAMETER() macros.
41286 */
41287#ifdef SQLITE_NO_SYNC
41288 UNUSED_PARAMETER(fd);
41289 UNUSED_PARAMETER(fullSync);
41290 UNUSED_PARAMETER(dataOnly);
41291#elif HAVE_FULLFSYNC
41292 UNUSED_PARAMETER(dataOnly);
41293#else
41294 UNUSED_PARAMETER(fullSync);
41295 UNUSED_PARAMETER(dataOnly);
41296#endif
41297
41298 /* Record the number of times that we do a normal fsync() and
41299 ** FULLSYNC. This is used during testing to verify that this procedure
41300 ** gets called with the correct arguments.
41301 */
41302#ifdef SQLITE_TEST
41303 if( fullSync ) sqlite3_fullsync_count++;
41304 sqlite3_sync_count++;
41305#endif
41306
41307 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
41308 ** no-op. But go ahead and call fstat() to validate the file
41309 ** descriptor as we need a method to provoke a failure during
41310 ** coverage testing.
41311 */
41312#ifdef SQLITE_NO_SYNC
41313 {
41314 struct stat buf;
41315 rc = osFstat(fd, &buf);
41316 }
41317#elif HAVE_FULLFSYNC
41318 if( fullSync ){
41319 rc = osFcntl(fd, F_FULLFSYNC, 0);
41320 }else{
41321 rc = 1;
41322 }
41323 /* If the FULLFSYNC failed, fall back to attempting an fsync().
41324 ** It shouldn't be possible for fullfsync to fail on the local
41325 ** file system (on OSX), so failure indicates that FULLFSYNC
41326 ** isn't supported for this file system. So, attempt an fsync
41327 ** and (for now) ignore the overhead of a superfluous fcntl call.
41328 ** It'd be better to detect fullfsync support once and avoid
41329 ** the fcntl call every time sync is called.
41330 */
41331 if( rc ) rc = fsync(fd);
41332
41333#elif defined(__APPLE__)
41334 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
41335 ** so currently we default to the macro that redefines fdatasync to fsync
41336 */
41337 rc = fsync(fd);
41338#else
41339 rc = fdatasync(fd: fd);
41340#if OS_VXWORKS
41341 if( rc==-1 && errno==ENOTSUP ){
41342 rc = fsync(fd);
41343 }
41344#endif /* OS_VXWORKS */
41345#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
41346
41347 if( OS_VXWORKS && rc!= -1 ){
41348 rc = 0;
41349 }
41350 return rc;
41351}
41352
41353/*
41354** Open a file descriptor to the directory containing file zFilename.
41355** If successful, *pFd is set to the opened file descriptor and
41356** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
41357** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
41358** value.
41359**
41360** The directory file descriptor is used for only one thing - to
41361** fsync() a directory to make sure file creation and deletion events
41362** are flushed to disk. Such fsyncs are not needed on newer
41363** journaling filesystems, but are required on older filesystems.
41364**
41365** This routine can be overridden using the xSetSysCall interface.
41366** The ability to override this routine was added in support of the
41367** chromium sandbox. Opening a directory is a security risk (we are
41368** told) so making it overrideable allows the chromium sandbox to
41369** replace this routine with a harmless no-op. To make this routine
41370** a no-op, replace it with a stub that returns SQLITE_OK but leaves
41371** *pFd set to a negative number.
41372**
41373** If SQLITE_OK is returned, the caller is responsible for closing
41374** the file descriptor *pFd using close().
41375*/
41376static int openDirectory(const char *zFilename, int *pFd){
41377 int ii;
41378 int fd = -1;
41379 char zDirname[MAX_PATHNAME+1];
41380
41381 sqlite3_snprintf(MAX_PATHNAME, zBuf: zDirname, zFormat: "%s", zFilename);
41382 for(ii=(int)strlen(s: zDirname); ii>0 && zDirname[ii]!='/'; ii--);
41383 if( ii>0 ){
41384 zDirname[ii] = '\0';
41385 }else{
41386 if( zDirname[0]!='/' ) zDirname[0] = '.';
41387 zDirname[1] = 0;
41388 }
41389 fd = robust_open(z: zDirname, O_RDONLY|O_BINARY, m: 0);
41390 if( fd>=0 ){
41391 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
41392 }
41393 *pFd = fd;
41394 if( fd>=0 ) return SQLITE_OK;
41395 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
41396}
41397
41398/*
41399** Make sure all writes to a particular file are committed to disk.
41400**
41401** If dataOnly==0 then both the file itself and its metadata (file
41402** size, access time, etc) are synced. If dataOnly!=0 then only the
41403** file data is synced.
41404**
41405** Under Unix, also make sure that the directory entry for the file
41406** has been created by fsync-ing the directory that contains the file.
41407** If we do not do this and we encounter a power failure, the directory
41408** entry for the journal might not exist after we reboot. The next
41409** SQLite to access the file will not know that the journal exists (because
41410** the directory entry for the journal was never created) and the transaction
41411** will not roll back - possibly leading to database corruption.
41412*/
41413static int unixSync(sqlite3_file *id, int flags){
41414 int rc;
41415 unixFile *pFile = (unixFile*)id;
41416
41417 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
41418 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
41419
41420 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
41421 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
41422 || (flags&0x0F)==SQLITE_SYNC_FULL
41423 );
41424
41425 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
41426 ** line is to test that doing so does not cause any problems.
41427 */
41428 SimulateDiskfullError( return SQLITE_FULL );
41429
41430 assert( pFile );
41431 OSTRACE(("SYNC %-3d\n", pFile->h));
41432 rc = full_fsync(fd: pFile->h, fullSync: isFullsync, dataOnly: isDataOnly);
41433 SimulateIOError( rc=1 );
41434 if( rc ){
41435 storeLastErrno(pFile, errno);
41436 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
41437 }
41438
41439 /* Also fsync the directory containing the file if the DIRSYNC flag
41440 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
41441 ** are unable to fsync a directory, so ignore errors on the fsync.
41442 */
41443 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
41444 int dirfd;
41445 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
41446 HAVE_FULLFSYNC, isFullsync));
41447 rc = osOpenDirectory(pFile->zPath, &dirfd);
41448 if( rc==SQLITE_OK ){
41449 full_fsync(fd: dirfd, fullSync: 0, dataOnly: 0);
41450 robust_close(pFile, h: dirfd, __LINE__);
41451 }else{
41452 assert( rc==SQLITE_CANTOPEN );
41453 rc = SQLITE_OK;
41454 }
41455 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
41456 }
41457 return rc;
41458}
41459
41460/*
41461** Truncate an open file to a specified size
41462*/
41463static int unixTruncate(sqlite3_file *id, i64 nByte){
41464 unixFile *pFile = (unixFile *)id;
41465 int rc;
41466 assert( pFile );
41467 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
41468
41469 /* If the user has configured a chunk-size for this file, truncate the
41470 ** file so that it consists of an integer number of chunks (i.e. the
41471 ** actual file size after the operation may be larger than the requested
41472 ** size).
41473 */
41474 if( pFile->szChunk>0 ){
41475 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
41476 }
41477
41478 rc = robust_ftruncate(h: pFile->h, sz: nByte);
41479 if( rc ){
41480 storeLastErrno(pFile, errno);
41481 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
41482 }else{
41483#ifdef SQLITE_DEBUG
41484 /* If we are doing a normal write to a database file (as opposed to
41485 ** doing a hot-journal rollback or a write to some file other than a
41486 ** normal database file) and we truncate the file to zero length,
41487 ** that effectively updates the change counter. This might happen
41488 ** when restoring a database using the backup API from a zero-length
41489 ** source.
41490 */
41491 if( pFile->inNormalWrite && nByte==0 ){
41492 pFile->transCntrChng = 1;
41493 }
41494#endif
41495
41496#if SQLITE_MAX_MMAP_SIZE>0
41497 /* If the file was just truncated to a size smaller than the currently
41498 ** mapped region, reduce the effective mapping size as well. SQLite will
41499 ** use read() and write() to access data beyond this point from now on.
41500 */
41501 if( nByte<pFile->mmapSize ){
41502 pFile->mmapSize = nByte;
41503 }
41504#endif
41505
41506 return SQLITE_OK;
41507 }
41508}
41509
41510/*
41511** Determine the current size of a file in bytes
41512*/
41513static int unixFileSize(sqlite3_file *id, i64 *pSize){
41514 int rc;
41515 struct stat buf;
41516 assert( id );
41517 rc = osFstat(((unixFile*)id)->h, &buf);
41518 SimulateIOError( rc=1 );
41519 if( rc!=0 ){
41520 storeLastErrno(pFile: (unixFile*)id, errno);
41521 return SQLITE_IOERR_FSTAT;
41522 }
41523 *pSize = buf.st_size;
41524
41525 /* When opening a zero-size database, the findInodeInfo() procedure
41526 ** writes a single byte into that file in order to work around a bug
41527 ** in the OS-X msdos filesystem. In order to avoid problems with upper
41528 ** layers, we need to report this file size as zero even though it is
41529 ** really 1. Ticket #3260.
41530 */
41531 if( *pSize==1 ) *pSize = 0;
41532
41533
41534 return SQLITE_OK;
41535}
41536
41537#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41538/*
41539** Handler for proxy-locking file-control verbs. Defined below in the
41540** proxying locking division.
41541*/
41542static int proxyFileControl(sqlite3_file*,int,void*);
41543#endif
41544
41545/*
41546** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
41547** file-control operation. Enlarge the database to nBytes in size
41548** (rounded up to the next chunk-size). If the database is already
41549** nBytes or larger, this routine is a no-op.
41550*/
41551static int fcntlSizeHint(unixFile *pFile, i64 nByte){
41552 if( pFile->szChunk>0 ){
41553 i64 nSize; /* Required file size */
41554 struct stat buf; /* Used to hold return values of fstat() */
41555
41556 if( osFstat(pFile->h, &buf) ){
41557 return SQLITE_IOERR_FSTAT;
41558 }
41559
41560 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
41561 if( nSize>(i64)buf.st_size ){
41562
41563#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
41564 /* The code below is handling the return value of osFallocate()
41565 ** correctly. posix_fallocate() is defined to "returns zero on success,
41566 ** or an error number on failure". See the manpage for details. */
41567 int err;
41568 do{
41569 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
41570 }while( err==EINTR );
41571 if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
41572#else
41573 /* If the OS does not have posix_fallocate(), fake it. Write a
41574 ** single byte to the last byte in each block that falls entirely
41575 ** within the extended region. Then, if required, a single byte
41576 ** at offset (nSize-1), to set the size of the file correctly.
41577 ** This is a similar technique to that used by glibc on systems
41578 ** that do not have a real fallocate() call.
41579 */
41580 int nBlk = buf.st_blksize; /* File-system block size */
41581 int nWrite = 0; /* Number of bytes written by seekAndWrite */
41582 i64 iWrite; /* Next offset to write to */
41583
41584 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
41585 assert( iWrite>=buf.st_size );
41586 assert( ((iWrite+1)%nBlk)==0 );
41587 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
41588 if( iWrite>=nSize ) iWrite = nSize - 1;
41589 nWrite = seekAndWrite(pFile, iWrite, "", 1);
41590 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
41591 }
41592#endif
41593 }
41594 }
41595
41596#if SQLITE_MAX_MMAP_SIZE>0
41597 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
41598 int rc;
41599 if( pFile->szChunk<=0 ){
41600 if( robust_ftruncate(h: pFile->h, sz: nByte) ){
41601 storeLastErrno(pFile, errno);
41602 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
41603 }
41604 }
41605
41606 rc = unixMapfile(pFd: pFile, nByte);
41607 return rc;
41608 }
41609#endif
41610
41611 return SQLITE_OK;
41612}
41613
41614/*
41615** If *pArg is initially negative then this is a query. Set *pArg to
41616** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
41617**
41618** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
41619*/
41620static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
41621 if( *pArg<0 ){
41622 *pArg = (pFile->ctrlFlags & mask)!=0;
41623 }else if( (*pArg)==0 ){
41624 pFile->ctrlFlags &= ~mask;
41625 }else{
41626 pFile->ctrlFlags |= mask;
41627 }
41628}
41629
41630/* Forward declaration */
41631static int unixGetTempname(int nBuf, char *zBuf);
41632#ifndef SQLITE_OMIT_WAL
41633 static int unixFcntlExternalReader(unixFile*, int*);
41634#endif
41635
41636/*
41637** Information and control of an open file handle.
41638*/
41639static int unixFileControl(sqlite3_file *id, int op, void *pArg){
41640 unixFile *pFile = (unixFile*)id;
41641 switch( op ){
41642#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
41643 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
41644 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
41645 return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
41646 }
41647 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
41648 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
41649 return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
41650 }
41651 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
41652 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
41653 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
41654 }
41655#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
41656
41657 case SQLITE_FCNTL_LOCKSTATE: {
41658 *(int*)pArg = pFile->eFileLock;
41659 return SQLITE_OK;
41660 }
41661 case SQLITE_FCNTL_LAST_ERRNO: {
41662 *(int*)pArg = pFile->lastErrno;
41663 return SQLITE_OK;
41664 }
41665 case SQLITE_FCNTL_CHUNK_SIZE: {
41666 pFile->szChunk = *(int *)pArg;
41667 return SQLITE_OK;
41668 }
41669 case SQLITE_FCNTL_SIZE_HINT: {
41670 int rc;
41671 SimulateIOErrorBenign(1);
41672 rc = fcntlSizeHint(pFile, nByte: *(i64 *)pArg);
41673 SimulateIOErrorBenign(0);
41674 return rc;
41675 }
41676 case SQLITE_FCNTL_PERSIST_WAL: {
41677 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, pArg: (int*)pArg);
41678 return SQLITE_OK;
41679 }
41680 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
41681 unixModeBit(pFile, UNIXFILE_PSOW, pArg: (int*)pArg);
41682 return SQLITE_OK;
41683 }
41684 case SQLITE_FCNTL_VFSNAME: {
41685 *(char**)pArg = sqlite3_mprintf(zFormat: "%s", pFile->pVfs->zName);
41686 return SQLITE_OK;
41687 }
41688 case SQLITE_FCNTL_TEMPFILENAME: {
41689 char *zTFile = sqlite3_malloc64( n: pFile->pVfs->mxPathname );
41690 if( zTFile ){
41691 unixGetTempname(nBuf: pFile->pVfs->mxPathname, zBuf: zTFile);
41692 *(char**)pArg = zTFile;
41693 }
41694 return SQLITE_OK;
41695 }
41696 case SQLITE_FCNTL_HAS_MOVED: {
41697 *(int*)pArg = fileHasMoved(pFile);
41698 return SQLITE_OK;
41699 }
41700#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
41701 case SQLITE_FCNTL_LOCK_TIMEOUT: {
41702 int iOld = pFile->iBusyTimeout;
41703 pFile->iBusyTimeout = *(int*)pArg;
41704 *(int*)pArg = iOld;
41705 return SQLITE_OK;
41706 }
41707#endif
41708#if SQLITE_MAX_MMAP_SIZE>0
41709 case SQLITE_FCNTL_MMAP_SIZE: {
41710 i64 newLimit = *(i64*)pArg;
41711 int rc = SQLITE_OK;
41712 if( newLimit>sqlite3GlobalConfig.mxMmap ){
41713 newLimit = sqlite3GlobalConfig.mxMmap;
41714 }
41715
41716 /* The value of newLimit may be eventually cast to (size_t) and passed
41717 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
41718 ** 64-bit type. */
41719 if( newLimit>0 && sizeof(size_t)<8 ){
41720 newLimit = (newLimit & 0x7FFFFFFF);
41721 }
41722
41723 *(i64*)pArg = pFile->mmapSizeMax;
41724 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
41725 pFile->mmapSizeMax = newLimit;
41726 if( pFile->mmapSize>0 ){
41727 unixUnmapfile(pFd: pFile);
41728 rc = unixMapfile(pFd: pFile, nByte: -1);
41729 }
41730 }
41731 return rc;
41732 }
41733#endif
41734#ifdef SQLITE_DEBUG
41735 /* The pager calls this method to signal that it has done
41736 ** a rollback and that the database is therefore unchanged and
41737 ** it hence it is OK for the transaction change counter to be
41738 ** unchanged.
41739 */
41740 case SQLITE_FCNTL_DB_UNCHANGED: {
41741 ((unixFile*)id)->dbUpdate = 0;
41742 return SQLITE_OK;
41743 }
41744#endif
41745#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41746 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
41747 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
41748 return proxyFileControl(id,op,pArg);
41749 }
41750#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
41751
41752 case SQLITE_FCNTL_EXTERNAL_READER: {
41753#ifndef SQLITE_OMIT_WAL
41754 return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
41755#else
41756 *(int*)pArg = 0;
41757 return SQLITE_OK;
41758#endif
41759 }
41760 }
41761 return SQLITE_NOTFOUND;
41762}
41763
41764/*
41765** If pFd->sectorSize is non-zero when this function is called, it is a
41766** no-op. Otherwise, the values of pFd->sectorSize and
41767** pFd->deviceCharacteristics are set according to the file-system
41768** characteristics.
41769**
41770** There are two versions of this function. One for QNX and one for all
41771** other systems.
41772*/
41773#ifndef __QNXNTO__
41774static void setDeviceCharacteristics(unixFile *pFd){
41775 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
41776 if( pFd->sectorSize==0 ){
41777#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
41778 int res;
41779 u32 f = 0;
41780
41781 /* Check for support for F2FS atomic batch writes. */
41782 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
41783 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
41784 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
41785 }
41786#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
41787
41788 /* Set the POWERSAFE_OVERWRITE flag if requested. */
41789 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
41790 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
41791 }
41792
41793 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41794 }
41795}
41796#else
41797#include <sys/dcmd_blk.h>
41798#include <sys/statvfs.h>
41799static void setDeviceCharacteristics(unixFile *pFile){
41800 if( pFile->sectorSize == 0 ){
41801 struct statvfs fsInfo;
41802
41803 /* Set defaults for non-supported filesystems */
41804 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41805 pFile->deviceCharacteristics = 0;
41806 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
41807 return;
41808 }
41809
41810 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
41811 pFile->sectorSize = fsInfo.f_bsize;
41812 pFile->deviceCharacteristics =
41813 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
41814 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
41815 ** the write succeeds */
41816 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
41817 ** so it is ordered */
41818 0;
41819 }else if( strstr(fsInfo.f_basetype, "etfs") ){
41820 pFile->sectorSize = fsInfo.f_bsize;
41821 pFile->deviceCharacteristics =
41822 /* etfs cluster size writes are atomic */
41823 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
41824 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
41825 ** the write succeeds */
41826 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
41827 ** so it is ordered */
41828 0;
41829 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
41830 pFile->sectorSize = fsInfo.f_bsize;
41831 pFile->deviceCharacteristics =
41832 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
41833 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
41834 ** the write succeeds */
41835 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
41836 ** so it is ordered */
41837 0;
41838 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
41839 pFile->sectorSize = fsInfo.f_bsize;
41840 pFile->deviceCharacteristics =
41841 /* full bitset of atomics from max sector size and smaller */
41842 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41843 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
41844 ** so it is ordered */
41845 0;
41846 }else if( strstr(fsInfo.f_basetype, "dos") ){
41847 pFile->sectorSize = fsInfo.f_bsize;
41848 pFile->deviceCharacteristics =
41849 /* full bitset of atomics from max sector size and smaller */
41850 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41851 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
41852 ** so it is ordered */
41853 0;
41854 }else{
41855 pFile->deviceCharacteristics =
41856 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
41857 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
41858 ** the write succeeds */
41859 0;
41860 }
41861 }
41862 /* Last chance verification. If the sector size isn't a multiple of 512
41863 ** then it isn't valid.*/
41864 if( pFile->sectorSize % 512 != 0 ){
41865 pFile->deviceCharacteristics = 0;
41866 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41867 }
41868}
41869#endif
41870
41871/*
41872** Return the sector size in bytes of the underlying block device for
41873** the specified file. This is almost always 512 bytes, but may be
41874** larger for some devices.
41875**
41876** SQLite code assumes this function cannot fail. It also assumes that
41877** if two files are created in the same file-system directory (i.e.
41878** a database and its journal file) that the sector size will be the
41879** same for both.
41880*/
41881static int unixSectorSize(sqlite3_file *id){
41882 unixFile *pFd = (unixFile*)id;
41883 setDeviceCharacteristics(pFd);
41884 return pFd->sectorSize;
41885}
41886
41887/*
41888** Return the device characteristics for the file.
41889**
41890** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
41891** However, that choice is controversial since technically the underlying
41892** file system does not always provide powersafe overwrites. (In other
41893** words, after a power-loss event, parts of the file that were never
41894** written might end up being altered.) However, non-PSOW behavior is very,
41895** very rare. And asserting PSOW makes a large reduction in the amount
41896** of required I/O for journaling, since a lot of padding is eliminated.
41897** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
41898** available to turn it off and URI query parameter available to turn it off.
41899*/
41900static int unixDeviceCharacteristics(sqlite3_file *id){
41901 unixFile *pFd = (unixFile*)id;
41902 setDeviceCharacteristics(pFd);
41903 return pFd->deviceCharacteristics;
41904}
41905
41906#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
41907
41908/*
41909** Return the system page size.
41910**
41911** This function should not be called directly by other code in this file.
41912** Instead, it should be called via macro osGetpagesize().
41913*/
41914static int unixGetpagesize(void){
41915#if OS_VXWORKS
41916 return 1024;
41917#elif defined(_BSD_SOURCE)
41918 return getpagesize();
41919#else
41920 return (int)sysconf(_SC_PAGESIZE);
41921#endif
41922}
41923
41924#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
41925
41926#ifndef SQLITE_OMIT_WAL
41927
41928/*
41929** Object used to represent an shared memory buffer.
41930**
41931** When multiple threads all reference the same wal-index, each thread
41932** has its own unixShm object, but they all point to a single instance
41933** of this unixShmNode object. In other words, each wal-index is opened
41934** only once per process.
41935**
41936** Each unixShmNode object is connected to a single unixInodeInfo object.
41937** We could coalesce this object into unixInodeInfo, but that would mean
41938** every open file that does not use shared memory (in other words, most
41939** open files) would have to carry around this extra information. So
41940** the unixInodeInfo object contains a pointer to this unixShmNode object
41941** and the unixShmNode object is created only when needed.
41942**
41943** unixMutexHeld() must be true when creating or destroying
41944** this object or while reading or writing the following fields:
41945**
41946** nRef
41947**
41948** The following fields are read-only after the object is created:
41949**
41950** hShm
41951** zFilename
41952**
41953** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
41954** unixMutexHeld() is true when reading or writing any other field
41955** in this structure.
41956*/
41957struct unixShmNode {
41958 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
41959 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
41960 char *zFilename; /* Name of the mmapped file */
41961 int hShm; /* Open file descriptor */
41962 int szRegion; /* Size of shared-memory regions */
41963 u16 nRegion; /* Size of array apRegion */
41964 u8 isReadonly; /* True if read-only */
41965 u8 isUnlocked; /* True if no DMS lock held */
41966 char **apRegion; /* Array of mapped shared-memory regions */
41967 int nRef; /* Number of unixShm objects pointing to this */
41968 unixShm *pFirst; /* All unixShm objects pointing to this */
41969 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
41970#ifdef SQLITE_DEBUG
41971 u8 exclMask; /* Mask of exclusive locks held */
41972 u8 sharedMask; /* Mask of shared locks held */
41973 u8 nextShmId; /* Next available unixShm.id value */
41974#endif
41975};
41976
41977/*
41978** Structure used internally by this VFS to record the state of an
41979** open shared memory connection.
41980**
41981** The following fields are initialized when this object is created and
41982** are read-only thereafter:
41983**
41984** unixShm.pShmNode
41985** unixShm.id
41986**
41987** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
41988** be held while accessing any read/write fields.
41989*/
41990struct unixShm {
41991 unixShmNode *pShmNode; /* The underlying unixShmNode object */
41992 unixShm *pNext; /* Next unixShm with the same unixShmNode */
41993 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
41994 u8 id; /* Id of this connection within its unixShmNode */
41995 u16 sharedMask; /* Mask of shared locks held */
41996 u16 exclMask; /* Mask of exclusive locks held */
41997};
41998
41999/*
42000** Constants used for locking
42001*/
42002#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
42003#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
42004
42005/*
42006** Use F_GETLK to check whether or not there are any readers with open
42007** wal-mode transactions in other processes on database file pFile. If
42008** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are
42009** such transactions, or 0 otherwise. If an error occurs, return an
42010** SQLite error code. The final value of *piOut is undefined in this
42011** case.
42012*/
42013static int unixFcntlExternalReader(unixFile *pFile, int *piOut){
42014 int rc = SQLITE_OK;
42015 *piOut = 0;
42016 if( pFile->pShm){
42017 unixShmNode *pShmNode = pFile->pShm->pShmNode;
42018 struct flock f;
42019
42020 memset(s: &f, c: 0, n: sizeof(f));
42021 f.l_type = F_WRLCK;
42022 f.l_whence = SEEK_SET;
42023 f.l_start = UNIX_SHM_BASE + 3;
42024 f.l_len = SQLITE_SHM_NLOCK - 3;
42025
42026 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
42027 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
42028 rc = SQLITE_IOERR_LOCK;
42029 }else{
42030 *piOut = (f.l_type!=F_UNLCK);
42031 }
42032 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
42033 }
42034
42035 return rc;
42036}
42037
42038
42039/*
42040** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
42041**
42042** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
42043** otherwise.
42044*/
42045static int unixShmSystemLock(
42046 unixFile *pFile, /* Open connection to the WAL file */
42047 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
42048 int ofst, /* First byte of the locking range */
42049 int n /* Number of bytes to lock */
42050){
42051 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
42052 struct flock f; /* The posix advisory locking structure */
42053 int rc = SQLITE_OK; /* Result code form fcntl() */
42054
42055 /* Access to the unixShmNode object is serialized by the caller */
42056 pShmNode = pFile->pInode->pShmNode;
42057 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
42058 assert( pShmNode->nRef>0 || unixMutexHeld() );
42059
42060 /* Shared locks never span more than one byte */
42061 assert( n==1 || lockType!=F_RDLCK );
42062
42063 /* Locks are within range */
42064 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
42065
42066 if( pShmNode->hShm>=0 ){
42067 int res;
42068 /* Initialize the locking parameters */
42069 f.l_type = lockType;
42070 f.l_whence = SEEK_SET;
42071 f.l_start = ofst;
42072 f.l_len = n;
42073 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
42074 if( res==-1 ){
42075#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42076 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
42077#else
42078 rc = SQLITE_BUSY;
42079#endif
42080 }
42081 }
42082
42083 /* Update the global lock state and do debug tracing */
42084#ifdef SQLITE_DEBUG
42085 { u16 mask;
42086 OSTRACE(("SHM-LOCK "));
42087 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
42088 if( rc==SQLITE_OK ){
42089 if( lockType==F_UNLCK ){
42090 OSTRACE(("unlock %d ok", ofst));
42091 pShmNode->exclMask &= ~mask;
42092 pShmNode->sharedMask &= ~mask;
42093 }else if( lockType==F_RDLCK ){
42094 OSTRACE(("read-lock %d ok", ofst));
42095 pShmNode->exclMask &= ~mask;
42096 pShmNode->sharedMask |= mask;
42097 }else{
42098 assert( lockType==F_WRLCK );
42099 OSTRACE(("write-lock %d ok", ofst));
42100 pShmNode->exclMask |= mask;
42101 pShmNode->sharedMask &= ~mask;
42102 }
42103 }else{
42104 if( lockType==F_UNLCK ){
42105 OSTRACE(("unlock %d failed", ofst));
42106 }else if( lockType==F_RDLCK ){
42107 OSTRACE(("read-lock failed"));
42108 }else{
42109 assert( lockType==F_WRLCK );
42110 OSTRACE(("write-lock %d failed", ofst));
42111 }
42112 }
42113 OSTRACE((" - afterwards %03x,%03x\n",
42114 pShmNode->sharedMask, pShmNode->exclMask));
42115 }
42116#endif
42117
42118 return rc;
42119}
42120
42121/*
42122** Return the minimum number of 32KB shm regions that should be mapped at
42123** a time, assuming that each mapping must be an integer multiple of the
42124** current system page-size.
42125**
42126** Usually, this is 1. The exception seems to be systems that are configured
42127** to use 64KB pages - in this case each mapping must cover at least two
42128** shm regions.
42129*/
42130static int unixShmRegionPerMap(void){
42131 int shmsz = 32*1024; /* SHM region size */
42132 int pgsz = osGetpagesize(); /* System page size */
42133 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
42134 if( pgsz<shmsz ) return 1;
42135 return pgsz/shmsz;
42136}
42137
42138/*
42139** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
42140**
42141** This is not a VFS shared-memory method; it is a utility function called
42142** by VFS shared-memory methods.
42143*/
42144static void unixShmPurge(unixFile *pFd){
42145 unixShmNode *p = pFd->pInode->pShmNode;
42146 assert( unixMutexHeld() );
42147 if( p && ALWAYS(p->nRef==0) ){
42148 int nShmPerMap = unixShmRegionPerMap();
42149 int i;
42150 assert( p->pInode==pFd->pInode );
42151 sqlite3_mutex_free(p: p->pShmMutex);
42152 for(i=0; i<p->nRegion; i+=nShmPerMap){
42153 if( p->hShm>=0 ){
42154 osMunmap(p->apRegion[i], p->szRegion);
42155 }else{
42156 sqlite3_free(p: p->apRegion[i]);
42157 }
42158 }
42159 sqlite3_free(p: p->apRegion);
42160 if( p->hShm>=0 ){
42161 robust_close(pFile: pFd, h: p->hShm, __LINE__);
42162 p->hShm = -1;
42163 }
42164 p->pInode->pShmNode = 0;
42165 sqlite3_free(p);
42166 }
42167}
42168
42169/*
42170** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
42171** take it now. Return SQLITE_OK if successful, or an SQLite error
42172** code otherwise.
42173**
42174** If the DMS cannot be locked because this is a readonly_shm=1
42175** connection and no other process already holds a lock, return
42176** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
42177*/
42178static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
42179 struct flock lock;
42180 int rc = SQLITE_OK;
42181
42182 /* Use F_GETLK to determine the locks other processes are holding
42183 ** on the DMS byte. If it indicates that another process is holding
42184 ** a SHARED lock, then this process may also take a SHARED lock
42185 ** and proceed with opening the *-shm file.
42186 **
42187 ** Or, if no other process is holding any lock, then this process
42188 ** is the first to open it. In this case take an EXCLUSIVE lock on the
42189 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
42190 ** downgrade to a SHARED lock on the DMS byte.
42191 **
42192 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
42193 ** return SQLITE_BUSY to the caller (it will try again). An earlier
42194 ** version of this code attempted the SHARED lock at this point. But
42195 ** this introduced a subtle race condition: if the process holding
42196 ** EXCLUSIVE failed just before truncating the *-shm file, then this
42197 ** process might open and use the *-shm file without truncating it.
42198 ** And if the *-shm file has been corrupted by a power failure or
42199 ** system crash, the database itself may also become corrupt. */
42200 lock.l_whence = SEEK_SET;
42201 lock.l_start = UNIX_SHM_DMS;
42202 lock.l_len = 1;
42203 lock.l_type = F_WRLCK;
42204 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
42205 rc = SQLITE_IOERR_LOCK;
42206 }else if( lock.l_type==F_UNLCK ){
42207 if( pShmNode->isReadonly ){
42208 pShmNode->isUnlocked = 1;
42209 rc = SQLITE_READONLY_CANTINIT;
42210 }else{
42211 rc = unixShmSystemLock(pFile: pDbFd, F_WRLCK, UNIX_SHM_DMS, n: 1);
42212 /* The first connection to attach must truncate the -shm file. We
42213 ** truncate to 3 bytes (an arbitrary small number, less than the
42214 ** -shm header size) rather than 0 as a system debugging aid, to
42215 ** help detect if a -shm file truncation is legitimate or is the work
42216 ** or a rogue process. */
42217 if( rc==SQLITE_OK && robust_ftruncate(h: pShmNode->hShm, sz: 3) ){
42218 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
42219 }
42220 }
42221 }else if( lock.l_type==F_WRLCK ){
42222 rc = SQLITE_BUSY;
42223 }
42224
42225 if( rc==SQLITE_OK ){
42226 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
42227 rc = unixShmSystemLock(pFile: pDbFd, F_RDLCK, UNIX_SHM_DMS, n: 1);
42228 }
42229 return rc;
42230}
42231
42232/*
42233** Open a shared-memory area associated with open database file pDbFd.
42234** This particular implementation uses mmapped files.
42235**
42236** The file used to implement shared-memory is in the same directory
42237** as the open database file and has the same name as the open database
42238** file with the "-shm" suffix added. For example, if the database file
42239** is "/home/user1/config.db" then the file that is created and mmapped
42240** for shared memory will be called "/home/user1/config.db-shm".
42241**
42242** Another approach to is to use files in /dev/shm or /dev/tmp or an
42243** some other tmpfs mount. But if a file in a different directory
42244** from the database file is used, then differing access permissions
42245** or a chroot() might cause two different processes on the same
42246** database to end up using different files for shared memory -
42247** meaning that their memory would not really be shared - resulting
42248** in database corruption. Nevertheless, this tmpfs file usage
42249** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
42250** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
42251** option results in an incompatible build of SQLite; builds of SQLite
42252** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
42253** same database file at the same time, database corruption will likely
42254** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
42255** "unsupported" and may go away in a future SQLite release.
42256**
42257** When opening a new shared-memory file, if no other instances of that
42258** file are currently open, in this process or in other processes, then
42259** the file must be truncated to zero length or have its header cleared.
42260**
42261** If the original database file (pDbFd) is using the "unix-excl" VFS
42262** that means that an exclusive lock is held on the database file and
42263** that no other processes are able to read or write the database. In
42264** that case, we do not really need shared memory. No shared memory
42265** file is created. The shared memory will be simulated with heap memory.
42266*/
42267static int unixOpenSharedMemory(unixFile *pDbFd){
42268 struct unixShm *p = 0; /* The connection to be opened */
42269 struct unixShmNode *pShmNode; /* The underlying mmapped file */
42270 int rc = SQLITE_OK; /* Result code */
42271 unixInodeInfo *pInode; /* The inode of fd */
42272 char *zShm; /* Name of the file used for SHM */
42273 int nShmFilename; /* Size of the SHM filename in bytes */
42274
42275 /* Allocate space for the new unixShm object. */
42276 p = sqlite3_malloc64( n: sizeof(*p) );
42277 if( p==0 ) return SQLITE_NOMEM_BKPT;
42278 memset(s: p, c: 0, n: sizeof(*p));
42279 assert( pDbFd->pShm==0 );
42280
42281 /* Check to see if a unixShmNode object already exists. Reuse an existing
42282 ** one if present. Create a new one if necessary.
42283 */
42284 assert( unixFileMutexNotheld(pDbFd) );
42285 unixEnterMutex();
42286 pInode = pDbFd->pInode;
42287 pShmNode = pInode->pShmNode;
42288 if( pShmNode==0 ){
42289 struct stat sStat; /* fstat() info for database file */
42290#ifndef SQLITE_SHM_DIRECTORY
42291 const char *zBasePath = pDbFd->zPath;
42292#endif
42293
42294 /* Call fstat() to figure out the permissions on the database file. If
42295 ** a new *-shm file is created, an attempt will be made to create it
42296 ** with the same permissions.
42297 */
42298 if( osFstat(pDbFd->h, &sStat) ){
42299 rc = SQLITE_IOERR_FSTAT;
42300 goto shm_open_err;
42301 }
42302
42303#ifdef SQLITE_SHM_DIRECTORY
42304 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
42305#else
42306 nShmFilename = 6 + (int)strlen(s: zBasePath);
42307#endif
42308 pShmNode = sqlite3_malloc64( n: sizeof(*pShmNode) + nShmFilename );
42309 if( pShmNode==0 ){
42310 rc = SQLITE_NOMEM_BKPT;
42311 goto shm_open_err;
42312 }
42313 memset(s: pShmNode, c: 0, n: sizeof(*pShmNode)+nShmFilename);
42314 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
42315#ifdef SQLITE_SHM_DIRECTORY
42316 sqlite3_snprintf(nShmFilename, zShm,
42317 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
42318 (u32)sStat.st_ino, (u32)sStat.st_dev);
42319#else
42320 sqlite3_snprintf(n: nShmFilename, zBuf: zShm, zFormat: "%s-shm", zBasePath);
42321 sqlite3FileSuffix3(pDbFd->zPath, zShm);
42322#endif
42323 pShmNode->hShm = -1;
42324 pDbFd->pInode->pShmNode = pShmNode;
42325 pShmNode->pInode = pDbFd->pInode;
42326 if( sqlite3GlobalConfig.bCoreMutex ){
42327 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
42328 if( pShmNode->pShmMutex==0 ){
42329 rc = SQLITE_NOMEM_BKPT;
42330 goto shm_open_err;
42331 }
42332 }
42333
42334 if( pInode->bProcessLock==0 ){
42335 if( 0==sqlite3_uri_boolean(z: pDbFd->zPath, zParam: "readonly_shm", bDefault: 0) ){
42336 pShmNode->hShm = robust_open(z: zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
42337 m: (sStat.st_mode&0777));
42338 }
42339 if( pShmNode->hShm<0 ){
42340 pShmNode->hShm = robust_open(z: zShm, O_RDONLY|O_NOFOLLOW,
42341 m: (sStat.st_mode&0777));
42342 if( pShmNode->hShm<0 ){
42343 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
42344 goto shm_open_err;
42345 }
42346 pShmNode->isReadonly = 1;
42347 }
42348
42349 /* If this process is running as root, make sure that the SHM file
42350 ** is owned by the same user that owns the original database. Otherwise,
42351 ** the original owner will not be able to connect.
42352 */
42353 robustFchown(fd: pShmNode->hShm, uid: sStat.st_uid, gid: sStat.st_gid);
42354
42355 rc = unixLockSharedMemory(pDbFd, pShmNode);
42356 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
42357 }
42358 }
42359
42360 /* Make the new connection a child of the unixShmNode */
42361 p->pShmNode = pShmNode;
42362#ifdef SQLITE_DEBUG
42363 p->id = pShmNode->nextShmId++;
42364#endif
42365 pShmNode->nRef++;
42366 pDbFd->pShm = p;
42367 unixLeaveMutex();
42368
42369 /* The reference count on pShmNode has already been incremented under
42370 ** the cover of the unixEnterMutex() mutex and the pointer from the
42371 ** new (struct unixShm) object to the pShmNode has been set. All that is
42372 ** left to do is to link the new object into the linked list starting
42373 ** at pShmNode->pFirst. This must be done while holding the
42374 ** pShmNode->pShmMutex.
42375 */
42376 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
42377 p->pNext = pShmNode->pFirst;
42378 pShmNode->pFirst = p;
42379 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
42380 return rc;
42381
42382 /* Jump here on any error */
42383shm_open_err:
42384 unixShmPurge(pFd: pDbFd); /* This call frees pShmNode if required */
42385 sqlite3_free(p);
42386 unixLeaveMutex();
42387 return rc;
42388}
42389
42390/*
42391** This function is called to obtain a pointer to region iRegion of the
42392** shared-memory associated with the database file fd. Shared-memory regions
42393** are numbered starting from zero. Each shared-memory region is szRegion
42394** bytes in size.
42395**
42396** If an error occurs, an error code is returned and *pp is set to NULL.
42397**
42398** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
42399** region has not been allocated (by any client, including one running in a
42400** separate process), then *pp is set to NULL and SQLITE_OK returned. If
42401** bExtend is non-zero and the requested shared-memory region has not yet
42402** been allocated, it is allocated by this function.
42403**
42404** If the shared-memory region has already been allocated or is allocated by
42405** this call as described above, then it is mapped into this processes
42406** address space (if it is not already), *pp is set to point to the mapped
42407** memory and SQLITE_OK returned.
42408*/
42409static int unixShmMap(
42410 sqlite3_file *fd, /* Handle open on database file */
42411 int iRegion, /* Region to retrieve */
42412 int szRegion, /* Size of regions */
42413 int bExtend, /* True to extend file if necessary */
42414 void volatile **pp /* OUT: Mapped memory */
42415){
42416 unixFile *pDbFd = (unixFile*)fd;
42417 unixShm *p;
42418 unixShmNode *pShmNode;
42419 int rc = SQLITE_OK;
42420 int nShmPerMap = unixShmRegionPerMap();
42421 int nReqRegion;
42422
42423 /* If the shared-memory file has not yet been opened, open it now. */
42424 if( pDbFd->pShm==0 ){
42425 rc = unixOpenSharedMemory(pDbFd);
42426 if( rc!=SQLITE_OK ) return rc;
42427 }
42428
42429 p = pDbFd->pShm;
42430 pShmNode = p->pShmNode;
42431 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
42432 if( pShmNode->isUnlocked ){
42433 rc = unixLockSharedMemory(pDbFd, pShmNode);
42434 if( rc!=SQLITE_OK ) goto shmpage_out;
42435 pShmNode->isUnlocked = 0;
42436 }
42437 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
42438 assert( pShmNode->pInode==pDbFd->pInode );
42439 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
42440 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
42441
42442 /* Minimum number of regions required to be mapped. */
42443 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
42444
42445 if( pShmNode->nRegion<nReqRegion ){
42446 char **apNew; /* New apRegion[] array */
42447 int nByte = nReqRegion*szRegion; /* Minimum required file size */
42448 struct stat sStat; /* Used by fstat() */
42449
42450 pShmNode->szRegion = szRegion;
42451
42452 if( pShmNode->hShm>=0 ){
42453 /* The requested region is not mapped into this processes address space.
42454 ** Check to see if it has been allocated (i.e. if the wal-index file is
42455 ** large enough to contain the requested region).
42456 */
42457 if( osFstat(pShmNode->hShm, &sStat) ){
42458 rc = SQLITE_IOERR_SHMSIZE;
42459 goto shmpage_out;
42460 }
42461
42462 if( sStat.st_size<nByte ){
42463 /* The requested memory region does not exist. If bExtend is set to
42464 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
42465 */
42466 if( !bExtend ){
42467 goto shmpage_out;
42468 }
42469
42470 /* Alternatively, if bExtend is true, extend the file. Do this by
42471 ** writing a single byte to the end of each (OS) page being
42472 ** allocated or extended. Technically, we need only write to the
42473 ** last page in order to extend the file. But writing to all new
42474 ** pages forces the OS to allocate them immediately, which reduces
42475 ** the chances of SIGBUS while accessing the mapped region later on.
42476 */
42477 else{
42478 static const int pgsz = 4096;
42479 int iPg;
42480
42481 /* Write to the last byte of each newly allocated or extended page */
42482 assert( (nByte % pgsz)==0 );
42483 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
42484 int x = 0;
42485 if( seekAndWriteFd(fd: pShmNode->hShm, iOff: iPg*pgsz + pgsz-1,pBuf: "",nBuf: 1,piErrno: &x)!=1 ){
42486 const char *zFile = pShmNode->zFilename;
42487 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
42488 goto shmpage_out;
42489 }
42490 }
42491 }
42492 }
42493 }
42494
42495 /* Map the requested memory region into this processes address space. */
42496 apNew = (char **)sqlite3_realloc(
42497 pOld: pShmNode->apRegion, n: nReqRegion*sizeof(char *)
42498 );
42499 if( !apNew ){
42500 rc = SQLITE_IOERR_NOMEM_BKPT;
42501 goto shmpage_out;
42502 }
42503 pShmNode->apRegion = apNew;
42504 while( pShmNode->nRegion<nReqRegion ){
42505 int nMap = szRegion*nShmPerMap;
42506 int i;
42507 void *pMem;
42508 if( pShmNode->hShm>=0 ){
42509 pMem = osMmap(0, nMap,
42510 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
42511 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
42512 );
42513 if( pMem==MAP_FAILED ){
42514 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
42515 goto shmpage_out;
42516 }
42517 }else{
42518 pMem = sqlite3_malloc64(n: nMap);
42519 if( pMem==0 ){
42520 rc = SQLITE_NOMEM_BKPT;
42521 goto shmpage_out;
42522 }
42523 memset(s: pMem, c: 0, n: nMap);
42524 }
42525
42526 for(i=0; i<nShmPerMap; i++){
42527 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
42528 }
42529 pShmNode->nRegion += nShmPerMap;
42530 }
42531 }
42532
42533shmpage_out:
42534 if( pShmNode->nRegion>iRegion ){
42535 *pp = pShmNode->apRegion[iRegion];
42536 }else{
42537 *pp = 0;
42538 }
42539 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
42540 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
42541 return rc;
42542}
42543
42544/*
42545** Check that the pShmNode->aLock[] array comports with the locking bitmasks
42546** held by each client. Return true if it does, or false otherwise. This
42547** is to be used in an assert(). e.g.
42548**
42549** assert( assertLockingArrayOk(pShmNode) );
42550*/
42551#ifdef SQLITE_DEBUG
42552static int assertLockingArrayOk(unixShmNode *pShmNode){
42553 unixShm *pX;
42554 int aLock[SQLITE_SHM_NLOCK];
42555 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
42556
42557 memset(aLock, 0, sizeof(aLock));
42558 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42559 int i;
42560 for(i=0; i<SQLITE_SHM_NLOCK; i++){
42561 if( pX->exclMask & (1<<i) ){
42562 assert( aLock[i]==0 );
42563 aLock[i] = -1;
42564 }else if( pX->sharedMask & (1<<i) ){
42565 assert( aLock[i]>=0 );
42566 aLock[i]++;
42567 }
42568 }
42569 }
42570
42571 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
42572 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
42573}
42574#endif
42575
42576/*
42577** Change the lock state for a shared-memory segment.
42578**
42579** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
42580** different here than in posix. In xShmLock(), one can go from unlocked
42581** to shared and back or from unlocked to exclusive and back. But one may
42582** not go from shared to exclusive or from exclusive to shared.
42583*/
42584static int unixShmLock(
42585 sqlite3_file *fd, /* Database file holding the shared memory */
42586 int ofst, /* First lock to acquire or release */
42587 int n, /* Number of locks to acquire or release */
42588 int flags /* What to do with the lock */
42589){
42590 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
42591 unixShm *p; /* The shared memory being locked */
42592 unixShmNode *pShmNode; /* The underlying file iNode */
42593 int rc = SQLITE_OK; /* Result code */
42594 u16 mask; /* Mask of locks to take or release */
42595 int *aLock;
42596
42597 p = pDbFd->pShm;
42598 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
42599 pShmNode = p->pShmNode;
42600 if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
42601 aLock = pShmNode->aLock;
42602
42603 assert( pShmNode==pDbFd->pInode->pShmNode );
42604 assert( pShmNode->pInode==pDbFd->pInode );
42605 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
42606 assert( n>=1 );
42607 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
42608 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
42609 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
42610 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
42611 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
42612 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
42613 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
42614
42615 /* Check that, if this to be a blocking lock, no locks that occur later
42616 ** in the following list than the lock being obtained are already held:
42617 **
42618 ** 1. Checkpointer lock (ofst==1).
42619 ** 2. Write lock (ofst==0).
42620 ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
42621 **
42622 ** In other words, if this is a blocking lock, none of the locks that
42623 ** occur later in the above list than the lock being obtained may be
42624 ** held.
42625 **
42626 ** It is not permitted to block on the RECOVER lock.
42627 */
42628#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42629 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
42630 (ofst!=2) /* not RECOVER */
42631 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
42632 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
42633 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
42634 ));
42635#endif
42636
42637 mask = (1<<(ofst+n)) - (1<<ofst);
42638 assert( n>1 || mask==(1<<ofst) );
42639 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
42640 assert( assertLockingArrayOk(pShmNode) );
42641 if( flags & SQLITE_SHM_UNLOCK ){
42642 if( (p->exclMask|p->sharedMask) & mask ){
42643 int ii;
42644 int bUnlock = 1;
42645
42646 for(ii=ofst; ii<ofst+n; ii++){
42647 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
42648 bUnlock = 0;
42649 }
42650 }
42651
42652 if( bUnlock ){
42653 rc = unixShmSystemLock(pFile: pDbFd, F_UNLCK, ofst: ofst+UNIX_SHM_BASE, n);
42654 if( rc==SQLITE_OK ){
42655 memset(s: &aLock[ofst], c: 0, n: sizeof(int)*n);
42656 }
42657 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
42658 assert( n==1 && aLock[ofst]>1 );
42659 aLock[ofst]--;
42660 }
42661
42662 /* Undo the local locks */
42663 if( rc==SQLITE_OK ){
42664 p->exclMask &= ~mask;
42665 p->sharedMask &= ~mask;
42666 }
42667 }
42668 }else if( flags & SQLITE_SHM_SHARED ){
42669 assert( n==1 );
42670 assert( (p->exclMask & (1<<ofst))==0 );
42671 if( (p->sharedMask & mask)==0 ){
42672 if( aLock[ofst]<0 ){
42673 rc = SQLITE_BUSY;
42674 }else if( aLock[ofst]==0 ){
42675 rc = unixShmSystemLock(pFile: pDbFd, F_RDLCK, ofst: ofst+UNIX_SHM_BASE, n);
42676 }
42677
42678 /* Get the local shared locks */
42679 if( rc==SQLITE_OK ){
42680 p->sharedMask |= mask;
42681 aLock[ofst]++;
42682 }
42683 }
42684 }else{
42685 /* Make sure no sibling connections hold locks that will block this
42686 ** lock. If any do, return SQLITE_BUSY right away. */
42687 int ii;
42688 for(ii=ofst; ii<ofst+n; ii++){
42689 assert( (p->sharedMask & mask)==0 );
42690 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
42691 rc = SQLITE_BUSY;
42692 break;
42693 }
42694 }
42695
42696 /* Get the exclusive locks at the system level. Then if successful
42697 ** also update the in-memory values. */
42698 if( rc==SQLITE_OK ){
42699 rc = unixShmSystemLock(pFile: pDbFd, F_WRLCK, ofst: ofst+UNIX_SHM_BASE, n);
42700 if( rc==SQLITE_OK ){
42701 assert( (p->sharedMask & mask)==0 );
42702 p->exclMask |= mask;
42703 for(ii=ofst; ii<ofst+n; ii++){
42704 aLock[ii] = -1;
42705 }
42706 }
42707 }
42708 }
42709 assert( assertLockingArrayOk(pShmNode) );
42710 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
42711 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
42712 p->id, osGetpid(0), p->sharedMask, p->exclMask));
42713 return rc;
42714}
42715
42716/*
42717** Implement a memory barrier or memory fence on shared memory.
42718**
42719** All loads and stores begun before the barrier must complete before
42720** any load or store begun after the barrier.
42721*/
42722static void unixShmBarrier(
42723 sqlite3_file *fd /* Database file holding the shared memory */
42724){
42725 UNUSED_PARAMETER(fd);
42726 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42727 assert( fd->pMethods->xLock==nolockLock
42728 || unixFileMutexNotheld((unixFile*)fd)
42729 );
42730 unixEnterMutex(); /* Also mutex, for redundancy */
42731 unixLeaveMutex();
42732}
42733
42734/*
42735** Close a connection to shared-memory. Delete the underlying
42736** storage if deleteFlag is true.
42737**
42738** If there is no shared memory associated with the connection then this
42739** routine is a harmless no-op.
42740*/
42741static int unixShmUnmap(
42742 sqlite3_file *fd, /* The underlying database file */
42743 int deleteFlag /* Delete shared-memory if true */
42744){
42745 unixShm *p; /* The connection to be closed */
42746 unixShmNode *pShmNode; /* The underlying shared-memory file */
42747 unixShm **pp; /* For looping over sibling connections */
42748 unixFile *pDbFd; /* The underlying database file */
42749
42750 pDbFd = (unixFile*)fd;
42751 p = pDbFd->pShm;
42752 if( p==0 ) return SQLITE_OK;
42753 pShmNode = p->pShmNode;
42754
42755 assert( pShmNode==pDbFd->pInode->pShmNode );
42756 assert( pShmNode->pInode==pDbFd->pInode );
42757
42758 /* Remove connection p from the set of connections associated
42759 ** with pShmNode */
42760 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
42761 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
42762 *pp = p->pNext;
42763
42764 /* Free the connection p */
42765 sqlite3_free(p);
42766 pDbFd->pShm = 0;
42767 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
42768
42769 /* If pShmNode->nRef has reached 0, then close the underlying
42770 ** shared-memory file, too */
42771 assert( unixFileMutexNotheld(pDbFd) );
42772 unixEnterMutex();
42773 assert( pShmNode->nRef>0 );
42774 pShmNode->nRef--;
42775 if( pShmNode->nRef==0 ){
42776 if( deleteFlag && pShmNode->hShm>=0 ){
42777 osUnlink(pShmNode->zFilename);
42778 }
42779 unixShmPurge(pFd: pDbFd);
42780 }
42781 unixLeaveMutex();
42782
42783 return SQLITE_OK;
42784}
42785
42786
42787#else
42788# define unixShmMap 0
42789# define unixShmLock 0
42790# define unixShmBarrier 0
42791# define unixShmUnmap 0
42792#endif /* #ifndef SQLITE_OMIT_WAL */
42793
42794#if SQLITE_MAX_MMAP_SIZE>0
42795/*
42796** If it is currently memory mapped, unmap file pFd.
42797*/
42798static void unixUnmapfile(unixFile *pFd){
42799 assert( pFd->nFetchOut==0 );
42800 if( pFd->pMapRegion ){
42801 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
42802 pFd->pMapRegion = 0;
42803 pFd->mmapSize = 0;
42804 pFd->mmapSizeActual = 0;
42805 }
42806}
42807
42808/*
42809** Attempt to set the size of the memory mapping maintained by file
42810** descriptor pFd to nNew bytes. Any existing mapping is discarded.
42811**
42812** If successful, this function sets the following variables:
42813**
42814** unixFile.pMapRegion
42815** unixFile.mmapSize
42816** unixFile.mmapSizeActual
42817**
42818** If unsuccessful, an error message is logged via sqlite3_log() and
42819** the three variables above are zeroed. In this case SQLite should
42820** continue accessing the database using the xRead() and xWrite()
42821** methods.
42822*/
42823static void unixRemapfile(
42824 unixFile *pFd, /* File descriptor object */
42825 i64 nNew /* Required mapping size */
42826){
42827 const char *zErr = "mmap";
42828 int h = pFd->h; /* File descriptor open on db file */
42829 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
42830 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
42831 u8 *pNew = 0; /* Location of new mapping */
42832 int flags = PROT_READ; /* Flags to pass to mmap() */
42833
42834 assert( pFd->nFetchOut==0 );
42835 assert( nNew>pFd->mmapSize );
42836 assert( nNew<=pFd->mmapSizeMax );
42837 assert( nNew>0 );
42838 assert( pFd->mmapSizeActual>=pFd->mmapSize );
42839 assert( MAP_FAILED!=0 );
42840
42841#ifdef SQLITE_MMAP_READWRITE
42842 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
42843#endif
42844
42845 if( pOrig ){
42846#if HAVE_MREMAP
42847 i64 nReuse = pFd->mmapSize;
42848#else
42849 const int szSyspage = osGetpagesize();
42850 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
42851#endif
42852 u8 *pReq = &pOrig[nReuse];
42853
42854 /* Unmap any pages of the existing mapping that cannot be reused. */
42855 if( nReuse!=nOrig ){
42856 osMunmap(pReq, nOrig-nReuse);
42857 }
42858
42859#if HAVE_MREMAP
42860 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
42861 zErr = "mremap";
42862#else
42863 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
42864 if( pNew!=MAP_FAILED ){
42865 if( pNew!=pReq ){
42866 osMunmap(pNew, nNew - nReuse);
42867 pNew = 0;
42868 }else{
42869 pNew = pOrig;
42870 }
42871 }
42872#endif
42873
42874 /* The attempt to extend the existing mapping failed. Free it. */
42875 if( pNew==MAP_FAILED || pNew==0 ){
42876 osMunmap(pOrig, nReuse);
42877 }
42878 }
42879
42880 /* If pNew is still NULL, try to create an entirely new mapping. */
42881 if( pNew==0 ){
42882 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
42883 }
42884
42885 if( pNew==MAP_FAILED ){
42886 pNew = 0;
42887 nNew = 0;
42888 unixLogError(SQLITE_OK, zErr, pFd->zPath);
42889
42890 /* If the mmap() above failed, assume that all subsequent mmap() calls
42891 ** will probably fail too. Fall back to using xRead/xWrite exclusively
42892 ** in this case. */
42893 pFd->mmapSizeMax = 0;
42894 }
42895 pFd->pMapRegion = (void *)pNew;
42896 pFd->mmapSize = pFd->mmapSizeActual = nNew;
42897}
42898
42899/*
42900** Memory map or remap the file opened by file-descriptor pFd (if the file
42901** is already mapped, the existing mapping is replaced by the new). Or, if
42902** there already exists a mapping for this file, and there are still
42903** outstanding xFetch() references to it, this function is a no-op.
42904**
42905** If parameter nByte is non-negative, then it is the requested size of
42906** the mapping to create. Otherwise, if nByte is less than zero, then the
42907** requested size is the size of the file on disk. The actual size of the
42908** created mapping is either the requested size or the value configured
42909** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
42910**
42911** SQLITE_OK is returned if no error occurs (even if the mapping is not
42912** recreated as a result of outstanding references) or an SQLite error
42913** code otherwise.
42914*/
42915static int unixMapfile(unixFile *pFd, i64 nMap){
42916 assert( nMap>=0 || pFd->nFetchOut==0 );
42917 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42918 if( pFd->nFetchOut>0 ) return SQLITE_OK;
42919
42920 if( nMap<0 ){
42921 struct stat statbuf; /* Low-level file information */
42922 if( osFstat(pFd->h, &statbuf) ){
42923 return SQLITE_IOERR_FSTAT;
42924 }
42925 nMap = statbuf.st_size;
42926 }
42927 if( nMap>pFd->mmapSizeMax ){
42928 nMap = pFd->mmapSizeMax;
42929 }
42930
42931 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42932 if( nMap!=pFd->mmapSize ){
42933 unixRemapfile(pFd, nNew: nMap);
42934 }
42935
42936 return SQLITE_OK;
42937}
42938#endif /* SQLITE_MAX_MMAP_SIZE>0 */
42939
42940/*
42941** If possible, return a pointer to a mapping of file fd starting at offset
42942** iOff. The mapping must be valid for at least nAmt bytes.
42943**
42944** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
42945** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
42946** Finally, if an error does occur, return an SQLite error code. The final
42947** value of *pp is undefined in this case.
42948**
42949** If this function does return a pointer, the caller must eventually
42950** release the reference by calling unixUnfetch().
42951*/
42952static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
42953#if SQLITE_MAX_MMAP_SIZE>0
42954 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
42955#endif
42956 *pp = 0;
42957
42958#if SQLITE_MAX_MMAP_SIZE>0
42959 if( pFd->mmapSizeMax>0 ){
42960 if( pFd->pMapRegion==0 ){
42961 int rc = unixMapfile(pFd, nMap: -1);
42962 if( rc!=SQLITE_OK ) return rc;
42963 }
42964 if( pFd->mmapSize >= iOff+nAmt ){
42965 *pp = &((u8 *)pFd->pMapRegion)[iOff];
42966 pFd->nFetchOut++;
42967 }
42968 }
42969#endif
42970 return SQLITE_OK;
42971}
42972
42973/*
42974** If the third argument is non-NULL, then this function releases a
42975** reference obtained by an earlier call to unixFetch(). The second
42976** argument passed to this function must be the same as the corresponding
42977** argument that was passed to the unixFetch() invocation.
42978**
42979** Or, if the third argument is NULL, then this function is being called
42980** to inform the VFS layer that, according to POSIX, any existing mapping
42981** may now be invalid and should be unmapped.
42982*/
42983static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
42984#if SQLITE_MAX_MMAP_SIZE>0
42985 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
42986 UNUSED_PARAMETER(iOff);
42987
42988 /* If p==0 (unmap the entire file) then there must be no outstanding
42989 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
42990 ** then there must be at least one outstanding. */
42991 assert( (p==0)==(pFd->nFetchOut==0) );
42992
42993 /* If p!=0, it must match the iOff value. */
42994 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
42995
42996 if( p ){
42997 pFd->nFetchOut--;
42998 }else{
42999 unixUnmapfile(pFd);
43000 }
43001
43002 assert( pFd->nFetchOut>=0 );
43003#else
43004 UNUSED_PARAMETER(fd);
43005 UNUSED_PARAMETER(p);
43006 UNUSED_PARAMETER(iOff);
43007#endif
43008 return SQLITE_OK;
43009}
43010
43011/*
43012** Here ends the implementation of all sqlite3_file methods.
43013**
43014********************** End sqlite3_file Methods *******************************
43015******************************************************************************/
43016
43017/*
43018** This division contains definitions of sqlite3_io_methods objects that
43019** implement various file locking strategies. It also contains definitions
43020** of "finder" functions. A finder-function is used to locate the appropriate
43021** sqlite3_io_methods object for a particular database file. The pAppData
43022** field of the sqlite3_vfs VFS objects are initialized to be pointers to
43023** the correct finder-function for that VFS.
43024**
43025** Most finder functions return a pointer to a fixed sqlite3_io_methods
43026** object. The only interesting finder-function is autolockIoFinder, which
43027** looks at the filesystem type and tries to guess the best locking
43028** strategy from that.
43029**
43030** For finder-function F, two objects are created:
43031**
43032** (1) The real finder-function named "FImpt()".
43033**
43034** (2) A constant pointer to this function named just "F".
43035**
43036**
43037** A pointer to the F pointer is used as the pAppData value for VFS
43038** objects. We have to do this instead of letting pAppData point
43039** directly at the finder-function since C90 rules prevent a void*
43040** from be cast into a function pointer.
43041**
43042**
43043** Each instance of this macro generates two objects:
43044**
43045** * A constant sqlite3_io_methods object call METHOD that has locking
43046** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
43047**
43048** * An I/O method finder function called FINDER that returns a pointer
43049** to the METHOD object in the previous bullet.
43050*/
43051#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
43052static const sqlite3_io_methods METHOD = { \
43053 VERSION, /* iVersion */ \
43054 CLOSE, /* xClose */ \
43055 unixRead, /* xRead */ \
43056 unixWrite, /* xWrite */ \
43057 unixTruncate, /* xTruncate */ \
43058 unixSync, /* xSync */ \
43059 unixFileSize, /* xFileSize */ \
43060 LOCK, /* xLock */ \
43061 UNLOCK, /* xUnlock */ \
43062 CKLOCK, /* xCheckReservedLock */ \
43063 unixFileControl, /* xFileControl */ \
43064 unixSectorSize, /* xSectorSize */ \
43065 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
43066 SHMMAP, /* xShmMap */ \
43067 unixShmLock, /* xShmLock */ \
43068 unixShmBarrier, /* xShmBarrier */ \
43069 unixShmUnmap, /* xShmUnmap */ \
43070 unixFetch, /* xFetch */ \
43071 unixUnfetch, /* xUnfetch */ \
43072}; \
43073static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
43074 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
43075 return &METHOD; \
43076} \
43077static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
43078 = FINDER##Impl;
43079
43080/*
43081** Here are all of the sqlite3_io_methods objects for each of the
43082** locking strategies. Functions that return pointers to these methods
43083** are also created.
43084*/
43085IOMETHODS(
43086 posixIoFinder, /* Finder function name */
43087 posixIoMethods, /* sqlite3_io_methods object name */
43088 3, /* shared memory and mmap are enabled */
43089 unixClose, /* xClose method */
43090 unixLock, /* xLock method */
43091 unixUnlock, /* xUnlock method */
43092 unixCheckReservedLock, /* xCheckReservedLock method */
43093 unixShmMap /* xShmMap method */
43094)
43095IOMETHODS(
43096 nolockIoFinder, /* Finder function name */
43097 nolockIoMethods, /* sqlite3_io_methods object name */
43098 3, /* shared memory and mmap are enabled */
43099 nolockClose, /* xClose method */
43100 nolockLock, /* xLock method */
43101 nolockUnlock, /* xUnlock method */
43102 nolockCheckReservedLock, /* xCheckReservedLock method */
43103 0 /* xShmMap method */
43104)
43105IOMETHODS(
43106 dotlockIoFinder, /* Finder function name */
43107 dotlockIoMethods, /* sqlite3_io_methods object name */
43108 1, /* shared memory is disabled */
43109 dotlockClose, /* xClose method */
43110 dotlockLock, /* xLock method */
43111 dotlockUnlock, /* xUnlock method */
43112 dotlockCheckReservedLock, /* xCheckReservedLock method */
43113 0 /* xShmMap method */
43114)
43115
43116#if SQLITE_ENABLE_LOCKING_STYLE
43117IOMETHODS(
43118 flockIoFinder, /* Finder function name */
43119 flockIoMethods, /* sqlite3_io_methods object name */
43120 1, /* shared memory is disabled */
43121 flockClose, /* xClose method */
43122 flockLock, /* xLock method */
43123 flockUnlock, /* xUnlock method */
43124 flockCheckReservedLock, /* xCheckReservedLock method */
43125 0 /* xShmMap method */
43126)
43127#endif
43128
43129#if OS_VXWORKS
43130IOMETHODS(
43131 semIoFinder, /* Finder function name */
43132 semIoMethods, /* sqlite3_io_methods object name */
43133 1, /* shared memory is disabled */
43134 semXClose, /* xClose method */
43135 semXLock, /* xLock method */
43136 semXUnlock, /* xUnlock method */
43137 semXCheckReservedLock, /* xCheckReservedLock method */
43138 0 /* xShmMap method */
43139)
43140#endif
43141
43142#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
43143IOMETHODS(
43144 afpIoFinder, /* Finder function name */
43145 afpIoMethods, /* sqlite3_io_methods object name */
43146 1, /* shared memory is disabled */
43147 afpClose, /* xClose method */
43148 afpLock, /* xLock method */
43149 afpUnlock, /* xUnlock method */
43150 afpCheckReservedLock, /* xCheckReservedLock method */
43151 0 /* xShmMap method */
43152)
43153#endif
43154
43155/*
43156** The proxy locking method is a "super-method" in the sense that it
43157** opens secondary file descriptors for the conch and lock files and
43158** it uses proxy, dot-file, AFP, and flock() locking methods on those
43159** secondary files. For this reason, the division that implements
43160** proxy locking is located much further down in the file. But we need
43161** to go ahead and define the sqlite3_io_methods and finder function
43162** for proxy locking here. So we forward declare the I/O methods.
43163*/
43164#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
43165static int proxyClose(sqlite3_file*);
43166static int proxyLock(sqlite3_file*, int);
43167static int proxyUnlock(sqlite3_file*, int);
43168static int proxyCheckReservedLock(sqlite3_file*, int*);
43169IOMETHODS(
43170 proxyIoFinder, /* Finder function name */
43171 proxyIoMethods, /* sqlite3_io_methods object name */
43172 1, /* shared memory is disabled */
43173 proxyClose, /* xClose method */
43174 proxyLock, /* xLock method */
43175 proxyUnlock, /* xUnlock method */
43176 proxyCheckReservedLock, /* xCheckReservedLock method */
43177 0 /* xShmMap method */
43178)
43179#endif
43180
43181/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
43182#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
43183IOMETHODS(
43184 nfsIoFinder, /* Finder function name */
43185 nfsIoMethods, /* sqlite3_io_methods object name */
43186 1, /* shared memory is disabled */
43187 unixClose, /* xClose method */
43188 unixLock, /* xLock method */
43189 nfsUnlock, /* xUnlock method */
43190 unixCheckReservedLock, /* xCheckReservedLock method */
43191 0 /* xShmMap method */
43192)
43193#endif
43194
43195#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
43196/*
43197** This "finder" function attempts to determine the best locking strategy
43198** for the database file "filePath". It then returns the sqlite3_io_methods
43199** object that implements that strategy.
43200**
43201** This is for MacOSX only.
43202*/
43203static const sqlite3_io_methods *autolockIoFinderImpl(
43204 const char *filePath, /* name of the database file */
43205 unixFile *pNew /* open file object for the database file */
43206){
43207 static const struct Mapping {
43208 const char *zFilesystem; /* Filesystem type name */
43209 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
43210 } aMap[] = {
43211 { "hfs", &posixIoMethods },
43212 { "ufs", &posixIoMethods },
43213 { "afpfs", &afpIoMethods },
43214 { "smbfs", &afpIoMethods },
43215 { "webdav", &nolockIoMethods },
43216 { 0, 0 }
43217 };
43218 int i;
43219 struct statfs fsInfo;
43220 struct flock lockInfo;
43221
43222 if( !filePath ){
43223 /* If filePath==NULL that means we are dealing with a transient file
43224 ** that does not need to be locked. */
43225 return &nolockIoMethods;
43226 }
43227 if( statfs(filePath, &fsInfo) != -1 ){
43228 if( fsInfo.f_flags & MNT_RDONLY ){
43229 return &nolockIoMethods;
43230 }
43231 for(i=0; aMap[i].zFilesystem; i++){
43232 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
43233 return aMap[i].pMethods;
43234 }
43235 }
43236 }
43237
43238 /* Default case. Handles, amongst others, "nfs".
43239 ** Test byte-range lock using fcntl(). If the call succeeds,
43240 ** assume that the file-system supports POSIX style locks.
43241 */
43242 lockInfo.l_len = 1;
43243 lockInfo.l_start = 0;
43244 lockInfo.l_whence = SEEK_SET;
43245 lockInfo.l_type = F_RDLCK;
43246 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
43247 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
43248 return &nfsIoMethods;
43249 } else {
43250 return &posixIoMethods;
43251 }
43252 }else{
43253 return &dotlockIoMethods;
43254 }
43255}
43256static const sqlite3_io_methods
43257 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
43258
43259#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
43260
43261#if OS_VXWORKS
43262/*
43263** This "finder" function for VxWorks checks to see if posix advisory
43264** locking works. If it does, then that is what is used. If it does not
43265** work, then fallback to named semaphore locking.
43266*/
43267static const sqlite3_io_methods *vxworksIoFinderImpl(
43268 const char *filePath, /* name of the database file */
43269 unixFile *pNew /* the open file object */
43270){
43271 struct flock lockInfo;
43272
43273 if( !filePath ){
43274 /* If filePath==NULL that means we are dealing with a transient file
43275 ** that does not need to be locked. */
43276 return &nolockIoMethods;
43277 }
43278
43279 /* Test if fcntl() is supported and use POSIX style locks.
43280 ** Otherwise fall back to the named semaphore method.
43281 */
43282 lockInfo.l_len = 1;
43283 lockInfo.l_start = 0;
43284 lockInfo.l_whence = SEEK_SET;
43285 lockInfo.l_type = F_RDLCK;
43286 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
43287 return &posixIoMethods;
43288 }else{
43289 return &semIoMethods;
43290 }
43291}
43292static const sqlite3_io_methods
43293 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
43294
43295#endif /* OS_VXWORKS */
43296
43297/*
43298** An abstract type for a pointer to an IO method finder function:
43299*/
43300typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
43301
43302
43303/****************************************************************************
43304**************************** sqlite3_vfs methods ****************************
43305**
43306** This division contains the implementation of methods on the
43307** sqlite3_vfs object.
43308*/
43309
43310/*
43311** Initialize the contents of the unixFile structure pointed to by pId.
43312*/
43313static int fillInUnixFile(
43314 sqlite3_vfs *pVfs, /* Pointer to vfs object */
43315 int h, /* Open file descriptor of file being opened */
43316 sqlite3_file *pId, /* Write to the unixFile structure here */
43317 const char *zFilename, /* Name of the file being opened */
43318 int ctrlFlags /* Zero or more UNIXFILE_* values */
43319){
43320 const sqlite3_io_methods *pLockingStyle;
43321 unixFile *pNew = (unixFile *)pId;
43322 int rc = SQLITE_OK;
43323
43324 assert( pNew->pInode==NULL );
43325
43326 /* No locking occurs in temporary files */
43327 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
43328
43329 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
43330 pNew->h = h;
43331 pNew->pVfs = pVfs;
43332 pNew->zPath = zFilename;
43333 pNew->ctrlFlags = (u8)ctrlFlags;
43334#if SQLITE_MAX_MMAP_SIZE>0
43335 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
43336#endif
43337 if( sqlite3_uri_boolean(z: ((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
43338 zParam: "psow", SQLITE_POWERSAFE_OVERWRITE) ){
43339 pNew->ctrlFlags |= UNIXFILE_PSOW;
43340 }
43341 if( strcmp(s1: pVfs->zName,s2: "unix-excl")==0 ){
43342 pNew->ctrlFlags |= UNIXFILE_EXCL;
43343 }
43344
43345#if OS_VXWORKS
43346 pNew->pId = vxworksFindFileId(zFilename);
43347 if( pNew->pId==0 ){
43348 ctrlFlags |= UNIXFILE_NOLOCK;
43349 rc = SQLITE_NOMEM_BKPT;
43350 }
43351#endif
43352
43353 if( ctrlFlags & UNIXFILE_NOLOCK ){
43354 pLockingStyle = &nolockIoMethods;
43355 }else{
43356 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
43357#if SQLITE_ENABLE_LOCKING_STYLE
43358 /* Cache zFilename in the locking context (AFP and dotlock override) for
43359 ** proxyLock activation is possible (remote proxy is based on db name)
43360 ** zFilename remains valid until file is closed, to support */
43361 pNew->lockingContext = (void*)zFilename;
43362#endif
43363 }
43364
43365 if( pLockingStyle == &posixIoMethods
43366#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
43367 || pLockingStyle == &nfsIoMethods
43368#endif
43369 ){
43370 unixEnterMutex();
43371 rc = findInodeInfo(pFile: pNew, ppInode: &pNew->pInode);
43372 if( rc!=SQLITE_OK ){
43373 /* If an error occurred in findInodeInfo(), close the file descriptor
43374 ** immediately, before releasing the mutex. findInodeInfo() may fail
43375 ** in two scenarios:
43376 **
43377 ** (a) A call to fstat() failed.
43378 ** (b) A malloc failed.
43379 **
43380 ** Scenario (b) may only occur if the process is holding no other
43381 ** file descriptors open on the same file. If there were other file
43382 ** descriptors on this file, then no malloc would be required by
43383 ** findInodeInfo(). If this is the case, it is quite safe to close
43384 ** handle h - as it is guaranteed that no posix locks will be released
43385 ** by doing so.
43386 **
43387 ** If scenario (a) caused the error then things are not so safe. The
43388 ** implicit assumption here is that if fstat() fails, things are in
43389 ** such bad shape that dropping a lock or two doesn't matter much.
43390 */
43391 robust_close(pFile: pNew, h, __LINE__);
43392 h = -1;
43393 }
43394 unixLeaveMutex();
43395 }
43396
43397#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
43398 else if( pLockingStyle == &afpIoMethods ){
43399 /* AFP locking uses the file path so it needs to be included in
43400 ** the afpLockingContext.
43401 */
43402 afpLockingContext *pCtx;
43403 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
43404 if( pCtx==0 ){
43405 rc = SQLITE_NOMEM_BKPT;
43406 }else{
43407 /* NB: zFilename exists and remains valid until the file is closed
43408 ** according to requirement F11141. So we do not need to make a
43409 ** copy of the filename. */
43410 pCtx->dbPath = zFilename;
43411 pCtx->reserved = 0;
43412 srandomdev();
43413 unixEnterMutex();
43414 rc = findInodeInfo(pNew, &pNew->pInode);
43415 if( rc!=SQLITE_OK ){
43416 sqlite3_free(pNew->lockingContext);
43417 robust_close(pNew, h, __LINE__);
43418 h = -1;
43419 }
43420 unixLeaveMutex();
43421 }
43422 }
43423#endif
43424
43425 else if( pLockingStyle == &dotlockIoMethods ){
43426 /* Dotfile locking uses the file path so it needs to be included in
43427 ** the dotlockLockingContext
43428 */
43429 char *zLockFile;
43430 int nFilename;
43431 assert( zFilename!=0 );
43432 nFilename = (int)strlen(s: zFilename) + 6;
43433 zLockFile = (char *)sqlite3_malloc64(n: nFilename);
43434 if( zLockFile==0 ){
43435 rc = SQLITE_NOMEM_BKPT;
43436 }else{
43437 sqlite3_snprintf(n: nFilename, zBuf: zLockFile, zFormat: "%s" DOTLOCK_SUFFIX, zFilename);
43438 }
43439 pNew->lockingContext = zLockFile;
43440 }
43441
43442#if OS_VXWORKS
43443 else if( pLockingStyle == &semIoMethods ){
43444 /* Named semaphore locking uses the file path so it needs to be
43445 ** included in the semLockingContext
43446 */
43447 unixEnterMutex();
43448 rc = findInodeInfo(pNew, &pNew->pInode);
43449 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
43450 char *zSemName = pNew->pInode->aSemName;
43451 int n;
43452 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
43453 pNew->pId->zCanonicalName);
43454 for( n=1; zSemName[n]; n++ )
43455 if( zSemName[n]=='/' ) zSemName[n] = '_';
43456 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
43457 if( pNew->pInode->pSem == SEM_FAILED ){
43458 rc = SQLITE_NOMEM_BKPT;
43459 pNew->pInode->aSemName[0] = '\0';
43460 }
43461 }
43462 unixLeaveMutex();
43463 }
43464#endif
43465
43466 storeLastErrno(pFile: pNew, error: 0);
43467#if OS_VXWORKS
43468 if( rc!=SQLITE_OK ){
43469 if( h>=0 ) robust_close(pNew, h, __LINE__);
43470 h = -1;
43471 osUnlink(zFilename);
43472 pNew->ctrlFlags |= UNIXFILE_DELETE;
43473 }
43474#endif
43475 if( rc!=SQLITE_OK ){
43476 if( h>=0 ) robust_close(pFile: pNew, h, __LINE__);
43477 }else{
43478 pId->pMethods = pLockingStyle;
43479 OpenCounter(+1);
43480 verifyDbFile(pFile: pNew);
43481 }
43482 return rc;
43483}
43484
43485/*
43486** Directories to consider for temp files.
43487*/
43488static const char *azTempDirs[] = {
43489 0,
43490 0,
43491 "/var/tmp",
43492 "/usr/tmp",
43493 "/tmp",
43494 "."
43495};
43496
43497/*
43498** Initialize first two members of azTempDirs[] array.
43499*/
43500static void unixTempFileInit(void){
43501 azTempDirs[0] = getenv(name: "SQLITE_TMPDIR");
43502 azTempDirs[1] = getenv(name: "TMPDIR");
43503}
43504
43505/*
43506** Return the name of a directory in which to put temporary files.
43507** If no suitable temporary file directory can be found, return NULL.
43508*/
43509static const char *unixTempFileDir(void){
43510 unsigned int i = 0;
43511 struct stat buf;
43512 const char *zDir = sqlite3_temp_directory;
43513
43514 while(1){
43515 if( zDir!=0
43516 && osStat(zDir, &buf)==0
43517 && S_ISDIR(buf.st_mode)
43518 && osAccess(zDir, 03)==0
43519 ){
43520 return zDir;
43521 }
43522 if( i>=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break;
43523 zDir = azTempDirs[i++];
43524 }
43525 return 0;
43526}
43527
43528/*
43529** Create a temporary file name in zBuf. zBuf must be allocated
43530** by the calling process and must be big enough to hold at least
43531** pVfs->mxPathname bytes.
43532*/
43533static int unixGetTempname(int nBuf, char *zBuf){
43534 const char *zDir;
43535 int iLimit = 0;
43536 int rc = SQLITE_OK;
43537
43538 /* It's odd to simulate an io-error here, but really this is just
43539 ** using the io-error infrastructure to test that SQLite handles this
43540 ** function failing.
43541 */
43542 zBuf[0] = 0;
43543 SimulateIOError( return SQLITE_IOERR );
43544
43545 sqlite3_mutex_enter(p: sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
43546 zDir = unixTempFileDir();
43547 if( zDir==0 ){
43548 rc = SQLITE_IOERR_GETTEMPPATH;
43549 }else{
43550 do{
43551 u64 r;
43552 sqlite3_randomness(N: sizeof(r), pBuf: &r);
43553 assert( nBuf>2 );
43554 zBuf[nBuf-2] = 0;
43555 sqlite3_snprintf(n: nBuf, zBuf, zFormat: "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
43556 zDir, r, 0);
43557 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
43558 rc = SQLITE_ERROR;
43559 break;
43560 }
43561 }while( osAccess(zBuf,0)==0 );
43562 }
43563 sqlite3_mutex_leave(p: sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
43564 return rc;
43565}
43566
43567#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
43568/*
43569** Routine to transform a unixFile into a proxy-locking unixFile.
43570** Implementation in the proxy-lock division, but used by unixOpen()
43571** if SQLITE_PREFER_PROXY_LOCKING is defined.
43572*/
43573static int proxyTransformUnixFile(unixFile*, const char*);
43574#endif
43575
43576/*
43577** Search for an unused file descriptor that was opened on the database
43578** file (not a journal or super-journal file) identified by pathname
43579** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
43580** argument to this function.
43581**
43582** Such a file descriptor may exist if a database connection was closed
43583** but the associated file descriptor could not be closed because some
43584** other file descriptor open on the same file is holding a file-lock.
43585** Refer to comments in the unixClose() function and the lengthy comment
43586** describing "Posix Advisory Locking" at the start of this file for
43587** further details. Also, ticket #4018.
43588**
43589** If a suitable file descriptor is found, then it is returned. If no
43590** such file descriptor is located, -1 is returned.
43591*/
43592static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
43593 UnixUnusedFd *pUnused = 0;
43594
43595 /* Do not search for an unused file descriptor on vxworks. Not because
43596 ** vxworks would not benefit from the change (it might, we're not sure),
43597 ** but because no way to test it is currently available. It is better
43598 ** not to risk breaking vxworks support for the sake of such an obscure
43599 ** feature. */
43600#if !OS_VXWORKS
43601 struct stat sStat; /* Results of stat() call */
43602
43603 unixEnterMutex();
43604
43605 /* A stat() call may fail for various reasons. If this happens, it is
43606 ** almost certain that an open() call on the same path will also fail.
43607 ** For this reason, if an error occurs in the stat() call here, it is
43608 ** ignored and -1 is returned. The caller will try to open a new file
43609 ** descriptor on the same path, fail, and return an error to SQLite.
43610 **
43611 ** Even if a subsequent open() call does succeed, the consequences of
43612 ** not searching for a reusable file descriptor are not dire. */
43613 if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
43614 unixInodeInfo *pInode;
43615
43616 pInode = inodeList;
43617 while( pInode && (pInode->fileId.dev!=sStat.st_dev
43618 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
43619 pInode = pInode->pNext;
43620 }
43621 if( pInode ){
43622 UnixUnusedFd **pp;
43623 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
43624 sqlite3_mutex_enter(p: pInode->pLockMutex);
43625 flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
43626 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
43627 pUnused = *pp;
43628 if( pUnused ){
43629 *pp = pUnused->pNext;
43630 }
43631 sqlite3_mutex_leave(p: pInode->pLockMutex);
43632 }
43633 }
43634 unixLeaveMutex();
43635#endif /* if !OS_VXWORKS */
43636 return pUnused;
43637}
43638
43639/*
43640** Find the mode, uid and gid of file zFile.
43641*/
43642static int getFileMode(
43643 const char *zFile, /* File name */
43644 mode_t *pMode, /* OUT: Permissions of zFile */
43645 uid_t *pUid, /* OUT: uid of zFile. */
43646 gid_t *pGid /* OUT: gid of zFile. */
43647){
43648 struct stat sStat; /* Output of stat() on database file */
43649 int rc = SQLITE_OK;
43650 if( 0==osStat(zFile, &sStat) ){
43651 *pMode = sStat.st_mode & 0777;
43652 *pUid = sStat.st_uid;
43653 *pGid = sStat.st_gid;
43654 }else{
43655 rc = SQLITE_IOERR_FSTAT;
43656 }
43657 return rc;
43658}
43659
43660/*
43661** This function is called by unixOpen() to determine the unix permissions
43662** to create new files with. If no error occurs, then SQLITE_OK is returned
43663** and a value suitable for passing as the third argument to open(2) is
43664** written to *pMode. If an IO error occurs, an SQLite error code is
43665** returned and the value of *pMode is not modified.
43666**
43667** In most cases, this routine sets *pMode to 0, which will become
43668** an indication to robust_open() to create the file using
43669** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
43670** But if the file being opened is a WAL or regular journal file, then
43671** this function queries the file-system for the permissions on the
43672** corresponding database file and sets *pMode to this value. Whenever
43673** possible, WAL and journal files are created using the same permissions
43674** as the associated database file.
43675**
43676** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
43677** original filename is unavailable. But 8_3_NAMES is only used for
43678** FAT filesystems and permissions do not matter there, so just use
43679** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero.
43680*/
43681static int findCreateFileMode(
43682 const char *zPath, /* Path of file (possibly) being created */
43683 int flags, /* Flags passed as 4th argument to xOpen() */
43684 mode_t *pMode, /* OUT: Permissions to open file with */
43685 uid_t *pUid, /* OUT: uid to set on the file */
43686 gid_t *pGid /* OUT: gid to set on the file */
43687){
43688 int rc = SQLITE_OK; /* Return Code */
43689 *pMode = 0;
43690 *pUid = 0;
43691 *pGid = 0;
43692 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
43693 char zDb[MAX_PATHNAME+1]; /* Database file path */
43694 int nDb; /* Number of valid bytes in zDb */
43695
43696 /* zPath is a path to a WAL or journal file. The following block derives
43697 ** the path to the associated database file from zPath. This block handles
43698 ** the following naming conventions:
43699 **
43700 ** "<path to db>-journal"
43701 ** "<path to db>-wal"
43702 ** "<path to db>-journalNN"
43703 ** "<path to db>-walNN"
43704 **
43705 ** where NN is a decimal number. The NN naming schemes are
43706 ** used by the test_multiplex.c module.
43707 **
43708 ** In normal operation, the journal file name will always contain
43709 ** a '-' character. However in 8+3 filename mode, or if a corrupt
43710 ** rollback journal specifies a super-journal with a goofy name, then
43711 ** the '-' might be missing or the '-' might be the first character in
43712 ** the filename. In that case, just return SQLITE_OK with *pMode==0.
43713 */
43714 nDb = sqlite3Strlen30(z: zPath) - 1;
43715 while( nDb>0 && zPath[nDb]!='.' ){
43716 if( zPath[nDb]=='-' ){
43717 memcpy(dest: zDb, src: zPath, n: nDb);
43718 zDb[nDb] = '\0';
43719 rc = getFileMode(zFile: zDb, pMode, pUid, pGid);
43720 break;
43721 }
43722 nDb--;
43723 }
43724 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
43725 *pMode = 0600;
43726 }else if( flags & SQLITE_OPEN_URI ){
43727 /* If this is a main database file and the file was opened using a URI
43728 ** filename, check for the "modeof" parameter. If present, interpret
43729 ** its value as a filename and try to copy the mode, uid and gid from
43730 ** that file. */
43731 const char *z = sqlite3_uri_parameter(z: zPath, zParam: "modeof");
43732 if( z ){
43733 rc = getFileMode(zFile: z, pMode, pUid, pGid);
43734 }
43735 }
43736 return rc;
43737}
43738
43739/*
43740** Open the file zPath.
43741**
43742** Previously, the SQLite OS layer used three functions in place of this
43743** one:
43744**
43745** sqlite3OsOpenReadWrite();
43746** sqlite3OsOpenReadOnly();
43747** sqlite3OsOpenExclusive();
43748**
43749** These calls correspond to the following combinations of flags:
43750**
43751** ReadWrite() -> (READWRITE | CREATE)
43752** ReadOnly() -> (READONLY)
43753** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
43754**
43755** The old OpenExclusive() accepted a boolean argument - "delFlag". If
43756** true, the file was configured to be automatically deleted when the
43757** file handle closed. To achieve the same effect using this new
43758** interface, add the DELETEONCLOSE flag to those specified above for
43759** OpenExclusive().
43760*/
43761static int unixOpen(
43762 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
43763 const char *zPath, /* Pathname of file to be opened */
43764 sqlite3_file *pFile, /* The file descriptor to be filled in */
43765 int flags, /* Input flags to control the opening */
43766 int *pOutFlags /* Output flags returned to SQLite core */
43767){
43768 unixFile *p = (unixFile *)pFile;
43769 int fd = -1; /* File descriptor returned by open() */
43770 int openFlags = 0; /* Flags to pass to open() */
43771 int eType = flags&0x0FFF00; /* Type of file to open */
43772 int noLock; /* True to omit locking primitives */
43773 int rc = SQLITE_OK; /* Function Return Code */
43774 int ctrlFlags = 0; /* UNIXFILE_* flags */
43775
43776 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
43777 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
43778 int isCreate = (flags & SQLITE_OPEN_CREATE);
43779 int isReadonly = (flags & SQLITE_OPEN_READONLY);
43780 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
43781#if SQLITE_ENABLE_LOCKING_STYLE
43782 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
43783#endif
43784#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
43785 struct statfs fsInfo;
43786#endif
43787
43788 /* If creating a super- or main-file journal, this function will open
43789 ** a file-descriptor on the directory too. The first time unixSync()
43790 ** is called the directory file descriptor will be fsync()ed and close()d.
43791 */
43792 int isNewJrnl = (isCreate && (
43793 eType==SQLITE_OPEN_SUPER_JOURNAL
43794 || eType==SQLITE_OPEN_MAIN_JOURNAL
43795 || eType==SQLITE_OPEN_WAL
43796 ));
43797
43798 /* If argument zPath is a NULL pointer, this function is required to open
43799 ** a temporary file. Use this buffer to store the file name in.
43800 */
43801 char zTmpname[MAX_PATHNAME+2];
43802 const char *zName = zPath;
43803
43804 /* Check the following statements are true:
43805 **
43806 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
43807 ** (b) if CREATE is set, then READWRITE must also be set, and
43808 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
43809 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
43810 */
43811 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
43812 assert(isCreate==0 || isReadWrite);
43813 assert(isExclusive==0 || isCreate);
43814 assert(isDelete==0 || isCreate);
43815
43816 /* The main DB, main journal, WAL file and super-journal are never
43817 ** automatically deleted. Nor are they ever temporary files. */
43818 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
43819 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
43820 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
43821 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
43822
43823 /* Assert that the upper layer has set one of the "file-type" flags. */
43824 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
43825 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
43826 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
43827 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
43828 );
43829
43830 /* Detect a pid change and reset the PRNG. There is a race condition
43831 ** here such that two or more threads all trying to open databases at
43832 ** the same instant might all reset the PRNG. But multiple resets
43833 ** are harmless.
43834 */
43835 if( randomnessPid!=osGetpid(0) ){
43836 randomnessPid = osGetpid(0);
43837 sqlite3_randomness(N: 0,pBuf: 0);
43838 }
43839 memset(s: p, c: 0, n: sizeof(unixFile));
43840
43841#ifdef SQLITE_ASSERT_NO_FILES
43842 /* Applications that never read or write a persistent disk files */
43843 assert( zName==0 );
43844#endif
43845
43846 if( eType==SQLITE_OPEN_MAIN_DB ){
43847 UnixUnusedFd *pUnused;
43848 pUnused = findReusableFd(zPath: zName, flags);
43849 if( pUnused ){
43850 fd = pUnused->fd;
43851 }else{
43852 pUnused = sqlite3_malloc64(n: sizeof(*pUnused));
43853 if( !pUnused ){
43854 return SQLITE_NOMEM_BKPT;
43855 }
43856 }
43857 p->pPreallocatedUnused = pUnused;
43858
43859 /* Database filenames are double-zero terminated if they are not
43860 ** URIs with parameters. Hence, they can always be passed into
43861 ** sqlite3_uri_parameter(). */
43862 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
43863
43864 }else if( !zName ){
43865 /* If zName is NULL, the upper layer is requesting a temp file. */
43866 assert(isDelete && !isNewJrnl);
43867 rc = unixGetTempname(nBuf: pVfs->mxPathname, zBuf: zTmpname);
43868 if( rc!=SQLITE_OK ){
43869 return rc;
43870 }
43871 zName = zTmpname;
43872
43873 /* Generated temporary filenames are always double-zero terminated
43874 ** for use by sqlite3_uri_parameter(). */
43875 assert( zName[strlen(zName)+1]==0 );
43876 }
43877
43878 /* Determine the value of the flags parameter passed to POSIX function
43879 ** open(). These must be calculated even if open() is not called, as
43880 ** they may be stored as part of the file handle and used by the
43881 ** 'conch file' locking functions later on. */
43882 if( isReadonly ) openFlags |= O_RDONLY;
43883 if( isReadWrite ) openFlags |= O_RDWR;
43884 if( isCreate ) openFlags |= O_CREAT;
43885 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
43886 openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW);
43887
43888 if( fd<0 ){
43889 mode_t openMode; /* Permissions to create file with */
43890 uid_t uid; /* Userid for the file */
43891 gid_t gid; /* Groupid for the file */
43892 rc = findCreateFileMode(zPath: zName, flags, pMode: &openMode, pUid: &uid, pGid: &gid);
43893 if( rc!=SQLITE_OK ){
43894 assert( !p->pPreallocatedUnused );
43895 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
43896 return rc;
43897 }
43898 fd = robust_open(z: zName, f: openFlags, m: openMode);
43899 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
43900 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
43901 if( fd<0 ){
43902 if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
43903 /* If unable to create a journal because the directory is not
43904 ** writable, change the error code to indicate that. */
43905 rc = SQLITE_READONLY_DIRECTORY;
43906 }else if( errno!=EISDIR && isReadWrite ){
43907 /* Failed to open the file for read/write access. Try read-only. */
43908 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
43909 openFlags &= ~(O_RDWR|O_CREAT);
43910 flags |= SQLITE_OPEN_READONLY;
43911 openFlags |= O_RDONLY;
43912 isReadonly = 1;
43913 fd = robust_open(z: zName, f: openFlags, m: openMode);
43914 }
43915 }
43916 if( fd<0 ){
43917 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
43918 if( rc==SQLITE_OK ) rc = rc2;
43919 goto open_finished;
43920 }
43921
43922 /* The owner of the rollback journal or WAL file should always be the
43923 ** same as the owner of the database file. Try to ensure that this is
43924 ** the case. The chown() system call will be a no-op if the current
43925 ** process lacks root privileges, be we should at least try. Without
43926 ** this step, if a root process opens a database file, it can leave
43927 ** behinds a journal/WAL that is owned by root and hence make the
43928 ** database inaccessible to unprivileged processes.
43929 **
43930 ** If openMode==0, then that means uid and gid are not set correctly
43931 ** (probably because SQLite is configured to use 8+3 filename mode) and
43932 ** in that case we do not want to attempt the chown().
43933 */
43934 if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){
43935 robustFchown(fd, uid, gid);
43936 }
43937 }
43938 assert( fd>=0 );
43939 if( pOutFlags ){
43940 *pOutFlags = flags;
43941 }
43942
43943 if( p->pPreallocatedUnused ){
43944 p->pPreallocatedUnused->fd = fd;
43945 p->pPreallocatedUnused->flags =
43946 flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
43947 }
43948
43949 if( isDelete ){
43950#if OS_VXWORKS
43951 zPath = zName;
43952#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
43953 zPath = sqlite3_mprintf("%s", zName);
43954 if( zPath==0 ){
43955 robust_close(p, fd, __LINE__);
43956 return SQLITE_NOMEM_BKPT;
43957 }
43958#else
43959 osUnlink(zName);
43960#endif
43961 }
43962#if SQLITE_ENABLE_LOCKING_STYLE
43963 else{
43964 p->openFlags = openFlags;
43965 }
43966#endif
43967
43968#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
43969 if( fstatfs(fd, &fsInfo) == -1 ){
43970 storeLastErrno(p, errno);
43971 robust_close(p, fd, __LINE__);
43972 return SQLITE_IOERR_ACCESS;
43973 }
43974 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
43975 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43976 }
43977 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
43978 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43979 }
43980#endif
43981
43982 /* Set up appropriate ctrlFlags */
43983 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
43984 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
43985 noLock = eType!=SQLITE_OPEN_MAIN_DB;
43986 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
43987 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC;
43988 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
43989
43990#if SQLITE_ENABLE_LOCKING_STYLE
43991#if SQLITE_PREFER_PROXY_LOCKING
43992 isAutoProxy = 1;
43993#endif
43994 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
43995 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
43996 int useProxy = 0;
43997
43998 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
43999 ** never use proxy, NULL means use proxy for non-local files only. */
44000 if( envforce!=NULL ){
44001 useProxy = atoi(envforce)>0;
44002 }else{
44003 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
44004 }
44005 if( useProxy ){
44006 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
44007 if( rc==SQLITE_OK ){
44008 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
44009 if( rc!=SQLITE_OK ){
44010 /* Use unixClose to clean up the resources added in fillInUnixFile
44011 ** and clear all the structure's references. Specifically,
44012 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
44013 */
44014 unixClose(pFile);
44015 return rc;
44016 }
44017 }
44018 goto open_finished;
44019 }
44020 }
44021#endif
44022
44023 assert( zPath==0 || zPath[0]=='/'
44024 || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
44025 );
44026 rc = fillInUnixFile(pVfs, h: fd, pId: pFile, zFilename: zPath, ctrlFlags);
44027
44028open_finished:
44029 if( rc!=SQLITE_OK ){
44030 sqlite3_free(p: p->pPreallocatedUnused);
44031 }
44032 return rc;
44033}
44034
44035
44036/*
44037** Delete the file at zPath. If the dirSync argument is true, fsync()
44038** the directory after deleting the file.
44039*/
44040static int unixDelete(
44041 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
44042 const char *zPath, /* Name of file to be deleted */
44043 int dirSync /* If true, fsync() directory after deleting file */
44044){
44045 int rc = SQLITE_OK;
44046 UNUSED_PARAMETER(NotUsed);
44047 SimulateIOError(return SQLITE_IOERR_DELETE);
44048 if( osUnlink(zPath)==(-1) ){
44049 if( errno==ENOENT
44050#if OS_VXWORKS
44051 || osAccess(zPath,0)!=0
44052#endif
44053 ){
44054 rc = SQLITE_IOERR_DELETE_NOENT;
44055 }else{
44056 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
44057 }
44058 return rc;
44059 }
44060#ifndef SQLITE_DISABLE_DIRSYNC
44061 if( (dirSync & 1)!=0 ){
44062 int fd;
44063 rc = osOpenDirectory(zPath, &fd);
44064 if( rc==SQLITE_OK ){
44065 if( full_fsync(fd,fullSync: 0,dataOnly: 0) ){
44066 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
44067 }
44068 robust_close(pFile: 0, h: fd, __LINE__);
44069 }else{
44070 assert( rc==SQLITE_CANTOPEN );
44071 rc = SQLITE_OK;
44072 }
44073 }
44074#endif
44075 return rc;
44076}
44077
44078/*
44079** Test the existence of or access permissions of file zPath. The
44080** test performed depends on the value of flags:
44081**
44082** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
44083** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
44084** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
44085**
44086** Otherwise return 0.
44087*/
44088static int unixAccess(
44089 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
44090 const char *zPath, /* Path of the file to examine */
44091 int flags, /* What do we want to learn about the zPath file? */
44092 int *pResOut /* Write result boolean here */
44093){
44094 UNUSED_PARAMETER(NotUsed);
44095 SimulateIOError( return SQLITE_IOERR_ACCESS; );
44096 assert( pResOut!=0 );
44097
44098 /* The spec says there are three possible values for flags. But only
44099 ** two of them are actually used */
44100 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
44101
44102 if( flags==SQLITE_ACCESS_EXISTS ){
44103 struct stat buf;
44104 *pResOut = 0==osStat(zPath, &buf) &&
44105 (!S_ISREG(buf.st_mode) || buf.st_size>0);
44106 }else{
44107 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
44108 }
44109 return SQLITE_OK;
44110}
44111
44112/*
44113** A pathname under construction
44114*/
44115typedef struct DbPath DbPath;
44116struct DbPath {
44117 int rc; /* Non-zero following any error */
44118 int nSymlink; /* Number of symlinks resolved */
44119 char *zOut; /* Write the pathname here */
44120 int nOut; /* Bytes of space available to zOut[] */
44121 int nUsed; /* Bytes of zOut[] currently being used */
44122};
44123
44124/* Forward reference */
44125static void appendAllPathElements(DbPath*,const char*);
44126
44127/*
44128** Append a single path element to the DbPath under construction
44129*/
44130static void appendOnePathElement(
44131 DbPath *pPath, /* Path under construction, to which to append zName */
44132 const char *zName, /* Name to append to pPath. Not zero-terminated */
44133 int nName /* Number of significant bytes in zName */
44134){
44135 assert( nName>0 );
44136 assert( zName!=0 );
44137 if( zName[0]=='.' ){
44138 if( nName==1 ) return;
44139 if( zName[1]=='.' && nName==2 ){
44140 if( pPath->nUsed>1 ){
44141 assert( pPath->zOut[0]=='/' );
44142 while( pPath->zOut[--pPath->nUsed]!='/' ){}
44143 }
44144 return;
44145 }
44146 }
44147 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
44148 pPath->rc = SQLITE_ERROR;
44149 return;
44150 }
44151 pPath->zOut[pPath->nUsed++] = '/';
44152 memcpy(dest: &pPath->zOut[pPath->nUsed], src: zName, n: nName);
44153 pPath->nUsed += nName;
44154#if defined(HAVE_READLINK) && defined(HAVE_LSTAT)
44155 if( pPath->rc==SQLITE_OK ){
44156 const char *zIn;
44157 struct stat buf;
44158 pPath->zOut[pPath->nUsed] = 0;
44159 zIn = pPath->zOut;
44160 if( osLstat(zIn, &buf)!=0 ){
44161 if( errno!=ENOENT ){
44162 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
44163 }
44164 }else if( S_ISLNK(buf.st_mode) ){
44165 ssize_t got;
44166 char zLnk[SQLITE_MAX_PATHLEN+2];
44167 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
44168 pPath->rc = SQLITE_CANTOPEN_BKPT;
44169 return;
44170 }
44171 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
44172 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
44173 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
44174 return;
44175 }
44176 zLnk[got] = 0;
44177 if( zLnk[0]=='/' ){
44178 pPath->nUsed = 0;
44179 }else{
44180 pPath->nUsed -= nName + 1;
44181 }
44182 appendAllPathElements(pPath, zLnk);
44183 }
44184 }
44185#endif
44186}
44187
44188/*
44189** Append all path elements in zPath to the DbPath under construction.
44190*/
44191static void appendAllPathElements(
44192 DbPath *pPath, /* Path under construction, to which to append zName */
44193 const char *zPath /* Path to append to pPath. Is zero-terminated */
44194){
44195 int i = 0;
44196 int j = 0;
44197 do{
44198 while( zPath[i] && zPath[i]!='/' ){ i++; }
44199 if( i>j ){
44200 appendOnePathElement(pPath, zName: &zPath[j], nName: i-j);
44201 }
44202 j = i+1;
44203 }while( zPath[i++] );
44204}
44205
44206/*
44207** Turn a relative pathname into a full pathname. The relative path
44208** is stored as a nul-terminated string in the buffer pointed to by
44209** zPath.
44210**
44211** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
44212** (in this case, MAX_PATHNAME bytes). The full-path is written to
44213** this buffer before returning.
44214*/
44215static int unixFullPathname(
44216 sqlite3_vfs *pVfs, /* Pointer to vfs object */
44217 const char *zPath, /* Possibly relative input path */
44218 int nOut, /* Size of output buffer in bytes */
44219 char *zOut /* Output buffer */
44220){
44221 DbPath path;
44222 UNUSED_PARAMETER(pVfs);
44223 path.rc = 0;
44224 path.nUsed = 0;
44225 path.nSymlink = 0;
44226 path.nOut = nOut;
44227 path.zOut = zOut;
44228 if( zPath[0]!='/' ){
44229 char zPwd[SQLITE_MAX_PATHLEN+2];
44230 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
44231 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
44232 }
44233 appendAllPathElements(pPath: &path, zPath: zPwd);
44234 }
44235 appendAllPathElements(pPath: &path, zPath);
44236 zOut[path.nUsed] = 0;
44237 if( path.rc || path.nUsed<2 ) return SQLITE_CANTOPEN_BKPT;
44238 if( path.nSymlink ) return SQLITE_OK_SYMLINK;
44239 return SQLITE_OK;
44240}
44241
44242#ifndef SQLITE_OMIT_LOAD_EXTENSION
44243/*
44244** Interfaces for opening a shared library, finding entry points
44245** within the shared library, and closing the shared library.
44246*/
44247#include <dlfcn.h>
44248static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
44249 UNUSED_PARAMETER(NotUsed);
44250 return dlopen(file: zFilename, RTLD_NOW | RTLD_GLOBAL);
44251}
44252
44253/*
44254** SQLite calls this function immediately after a call to unixDlSym() or
44255** unixDlOpen() fails (returns a null pointer). If a more detailed error
44256** message is available, it is written to zBufOut. If no error message
44257** is available, zBufOut is left unmodified and SQLite uses a default
44258** error message.
44259*/
44260static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
44261 const char *zErr;
44262 UNUSED_PARAMETER(NotUsed);
44263 unixEnterMutex();
44264 zErr = dlerror();
44265 if( zErr ){
44266 sqlite3_snprintf(n: nBuf, zBuf: zBufOut, zFormat: "%s", zErr);
44267 }
44268 unixLeaveMutex();
44269}
44270static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
44271 /*
44272 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
44273 ** cast into a pointer to a function. And yet the library dlsym() routine
44274 ** returns a void* which is really a pointer to a function. So how do we
44275 ** use dlsym() with -pedantic-errors?
44276 **
44277 ** Variable x below is defined to be a pointer to a function taking
44278 ** parameters void* and const char* and returning a pointer to a function.
44279 ** We initialize x by assigning it a pointer to the dlsym() function.
44280 ** (That assignment requires a cast.) Then we call the function that
44281 ** x points to.
44282 **
44283 ** This work-around is unlikely to work correctly on any system where
44284 ** you really cannot cast a function pointer into void*. But then, on the
44285 ** other hand, dlsym() will not work on such a system either, so we have
44286 ** not really lost anything.
44287 */
44288 void (*(*x)(void*,const char*))(void);
44289 UNUSED_PARAMETER(NotUsed);
44290 x = (void(*(*)(void*,const char*))(void))dlsym;
44291 return (*x)(p, zSym);
44292}
44293static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
44294 UNUSED_PARAMETER(NotUsed);
44295 dlclose(handle: pHandle);
44296}
44297#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
44298 #define unixDlOpen 0
44299 #define unixDlError 0
44300 #define unixDlSym 0
44301 #define unixDlClose 0
44302#endif
44303
44304/*
44305** Write nBuf bytes of random data to the supplied buffer zBuf.
44306*/
44307static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
44308 UNUSED_PARAMETER(NotUsed);
44309 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
44310
44311 /* We have to initialize zBuf to prevent valgrind from reporting
44312 ** errors. The reports issued by valgrind are incorrect - we would
44313 ** prefer that the randomness be increased by making use of the
44314 ** uninitialized space in zBuf - but valgrind errors tend to worry
44315 ** some users. Rather than argue, it seems easier just to initialize
44316 ** the whole array and silence valgrind, even if that means less randomness
44317 ** in the random seed.
44318 **
44319 ** When testing, initializing zBuf[] to zero is all we do. That means
44320 ** that we always use the same random number sequence. This makes the
44321 ** tests repeatable.
44322 */
44323 memset(s: zBuf, c: 0, n: nBuf);
44324 randomnessPid = osGetpid(0);
44325#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
44326 {
44327 int fd, got;
44328 fd = robust_open(z: "/dev/urandom", O_RDONLY, m: 0);
44329 if( fd<0 ){
44330 time_t t;
44331 time(timer: &t);
44332 memcpy(dest: zBuf, src: &t, n: sizeof(t));
44333 memcpy(dest: &zBuf[sizeof(t)], src: &randomnessPid, n: sizeof(randomnessPid));
44334 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
44335 nBuf = sizeof(t) + sizeof(randomnessPid);
44336 }else{
44337 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
44338 robust_close(pFile: 0, h: fd, __LINE__);
44339 }
44340 }
44341#endif
44342 return nBuf;
44343}
44344
44345
44346/*
44347** Sleep for a little while. Return the amount of time slept.
44348** The argument is the number of microseconds we want to sleep.
44349** The return value is the number of microseconds of sleep actually
44350** requested from the underlying operating system, a number which
44351** might be greater than or equal to the argument, but not less
44352** than the argument.
44353*/
44354static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
44355#if !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP+0
44356 struct timespec sp;
44357 sp.tv_sec = microseconds / 1000000;
44358 sp.tv_nsec = (microseconds % 1000000) * 1000;
44359
44360 /* Almost all modern unix systems support nanosleep(). But if you are
44361 ** compiling for one of the rare exceptions, you can use
44362 ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
44363 ** usleep() is available) in order to bypass the use of nanosleep() */
44364 nanosleep(requested_time: &sp, NULL);
44365
44366 UNUSED_PARAMETER(NotUsed);
44367 return microseconds;
44368#elif defined(HAVE_USLEEP) && HAVE_USLEEP
44369 if( microseconds>=1000000 ) sleep(microseconds/1000000);
44370 if( microseconds%1000000 ) usleep(microseconds%1000000);
44371 UNUSED_PARAMETER(NotUsed);
44372 return microseconds;
44373#else
44374 int seconds = (microseconds+999999)/1000000;
44375 sleep(seconds);
44376 UNUSED_PARAMETER(NotUsed);
44377 return seconds*1000000;
44378#endif
44379}
44380
44381/*
44382** The following variable, if set to a non-zero value, is interpreted as
44383** the number of seconds since 1970 and is used to set the result of
44384** sqlite3OsCurrentTime() during testing.
44385*/
44386#ifdef SQLITE_TEST
44387SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
44388#endif
44389
44390/*
44391** Find the current time (in Universal Coordinated Time). Write into *piNow
44392** the current time and date as a Julian Day number times 86_400_000. In
44393** other words, write into *piNow the number of milliseconds since the Julian
44394** epoch of noon in Greenwich on November 24, 4714 B.C according to the
44395** proleptic Gregorian calendar.
44396**
44397** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
44398** cannot be found.
44399*/
44400static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
44401 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
44402 int rc = SQLITE_OK;
44403#if defined(NO_GETTOD)
44404 time_t t;
44405 time(&t);
44406 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
44407#elif OS_VXWORKS
44408 struct timespec sNow;
44409 clock_gettime(CLOCK_REALTIME, &sNow);
44410 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
44411#else
44412 struct timeval sNow;
44413 (void)gettimeofday(tv: &sNow, tz: 0); /* Cannot fail given valid arguments */
44414 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
44415#endif
44416
44417#ifdef SQLITE_TEST
44418 if( sqlite3_current_time ){
44419 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
44420 }
44421#endif
44422 UNUSED_PARAMETER(NotUsed);
44423 return rc;
44424}
44425
44426#ifndef SQLITE_OMIT_DEPRECATED
44427/*
44428** Find the current time (in Universal Coordinated Time). Write the
44429** current time and date as a Julian Day number into *prNow and
44430** return 0. Return 1 if the time and date cannot be found.
44431*/
44432static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
44433 sqlite3_int64 i = 0;
44434 int rc;
44435 UNUSED_PARAMETER(NotUsed);
44436 rc = unixCurrentTimeInt64(NotUsed: 0, piNow: &i);
44437 *prNow = i/86400000.0;
44438 return rc;
44439}
44440#else
44441# define unixCurrentTime 0
44442#endif
44443
44444/*
44445** The xGetLastError() method is designed to return a better
44446** low-level error message when operating-system problems come up
44447** during SQLite operation. Only the integer return code is currently
44448** used.
44449*/
44450static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
44451 UNUSED_PARAMETER(NotUsed);
44452 UNUSED_PARAMETER(NotUsed2);
44453 UNUSED_PARAMETER(NotUsed3);
44454 return errno;
44455}
44456
44457
44458/*
44459************************ End of sqlite3_vfs methods ***************************
44460******************************************************************************/
44461
44462/******************************************************************************
44463************************** Begin Proxy Locking ********************************
44464**
44465** Proxy locking is a "uber-locking-method" in this sense: It uses the
44466** other locking methods on secondary lock files. Proxy locking is a
44467** meta-layer over top of the primitive locking implemented above. For
44468** this reason, the division that implements of proxy locking is deferred
44469** until late in the file (here) after all of the other I/O methods have
44470** been defined - so that the primitive locking methods are available
44471** as services to help with the implementation of proxy locking.
44472**
44473****
44474**
44475** The default locking schemes in SQLite use byte-range locks on the
44476** database file to coordinate safe, concurrent access by multiple readers
44477** and writers [http://sqlite.org/lockingv3.html]. The five file locking
44478** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
44479** as POSIX read & write locks over fixed set of locations (via fsctl),
44480** on AFP and SMB only exclusive byte-range locks are available via fsctl
44481** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
44482** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
44483** address in the shared range is taken for a SHARED lock, the entire
44484** shared range is taken for an EXCLUSIVE lock):
44485**
44486** PENDING_BYTE 0x40000000
44487** RESERVED_BYTE 0x40000001
44488** SHARED_RANGE 0x40000002 -> 0x40000200
44489**
44490** This works well on the local file system, but shows a nearly 100x
44491** slowdown in read performance on AFP because the AFP client disables
44492** the read cache when byte-range locks are present. Enabling the read
44493** cache exposes a cache coherency problem that is present on all OS X
44494** supported network file systems. NFS and AFP both observe the
44495** close-to-open semantics for ensuring cache coherency
44496** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
44497** address the requirements for concurrent database access by multiple
44498** readers and writers
44499** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
44500**
44501** To address the performance and cache coherency issues, proxy file locking
44502** changes the way database access is controlled by limiting access to a
44503** single host at a time and moving file locks off of the database file
44504** and onto a proxy file on the local file system.
44505**
44506**
44507** Using proxy locks
44508** -----------------
44509**
44510** C APIs
44511**
44512** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
44513** <proxy_path> | ":auto:");
44514** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
44515** &<proxy_path>);
44516**
44517**
44518** SQL pragmas
44519**
44520** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
44521** PRAGMA [database.]lock_proxy_file
44522**
44523** Specifying ":auto:" means that if there is a conch file with a matching
44524** host ID in it, the proxy path in the conch file will be used, otherwise
44525** a proxy path based on the user's temp dir
44526** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
44527** actual proxy file name is generated from the name and path of the
44528** database file. For example:
44529**
44530** For database path "/Users/me/foo.db"
44531** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
44532**
44533** Once a lock proxy is configured for a database connection, it can not
44534** be removed, however it may be switched to a different proxy path via
44535** the above APIs (assuming the conch file is not being held by another
44536** connection or process).
44537**
44538**
44539** How proxy locking works
44540** -----------------------
44541**
44542** Proxy file locking relies primarily on two new supporting files:
44543**
44544** * conch file to limit access to the database file to a single host
44545** at a time
44546**
44547** * proxy file to act as a proxy for the advisory locks normally
44548** taken on the database
44549**
44550** The conch file - to use a proxy file, sqlite must first "hold the conch"
44551** by taking an sqlite-style shared lock on the conch file, reading the
44552** contents and comparing the host's unique host ID (see below) and lock
44553** proxy path against the values stored in the conch. The conch file is
44554** stored in the same directory as the database file and the file name
44555** is patterned after the database file name as ".<databasename>-conch".
44556** If the conch file does not exist, or its contents do not match the
44557** host ID and/or proxy path, then the lock is escalated to an exclusive
44558** lock and the conch file contents is updated with the host ID and proxy
44559** path and the lock is downgraded to a shared lock again. If the conch
44560** is held by another process (with a shared lock), the exclusive lock
44561** will fail and SQLITE_BUSY is returned.
44562**
44563** The proxy file - a single-byte file used for all advisory file locks
44564** normally taken on the database file. This allows for safe sharing
44565** of the database file for multiple readers and writers on the same
44566** host (the conch ensures that they all use the same local lock file).
44567**
44568** Requesting the lock proxy does not immediately take the conch, it is
44569** only taken when the first request to lock database file is made.
44570** This matches the semantics of the traditional locking behavior, where
44571** opening a connection to a database file does not take a lock on it.
44572** The shared lock and an open file descriptor are maintained until
44573** the connection to the database is closed.
44574**
44575** The proxy file and the lock file are never deleted so they only need
44576** to be created the first time they are used.
44577**
44578** Configuration options
44579** ---------------------
44580**
44581** SQLITE_PREFER_PROXY_LOCKING
44582**
44583** Database files accessed on non-local file systems are
44584** automatically configured for proxy locking, lock files are
44585** named automatically using the same logic as
44586** PRAGMA lock_proxy_file=":auto:"
44587**
44588** SQLITE_PROXY_DEBUG
44589**
44590** Enables the logging of error messages during host id file
44591** retrieval and creation
44592**
44593** LOCKPROXYDIR
44594**
44595** Overrides the default directory used for lock proxy files that
44596** are named automatically via the ":auto:" setting
44597**
44598** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
44599**
44600** Permissions to use when creating a directory for storing the
44601** lock proxy files, only used when LOCKPROXYDIR is not set.
44602**
44603**
44604** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
44605** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
44606** force proxy locking to be used for every database file opened, and 0
44607** will force automatic proxy locking to be disabled for all database
44608** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
44609** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
44610*/
44611
44612/*
44613** Proxy locking is only available on MacOSX
44614*/
44615#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
44616
44617/*
44618** The proxyLockingContext has the path and file structures for the remote
44619** and local proxy files in it
44620*/
44621typedef struct proxyLockingContext proxyLockingContext;
44622struct proxyLockingContext {
44623 unixFile *conchFile; /* Open conch file */
44624 char *conchFilePath; /* Name of the conch file */
44625 unixFile *lockProxy; /* Open proxy lock file */
44626 char *lockProxyPath; /* Name of the proxy lock file */
44627 char *dbPath; /* Name of the open file */
44628 int conchHeld; /* 1 if the conch is held, -1 if lockless */
44629 int nFails; /* Number of conch taking failures */
44630 void *oldLockingContext; /* Original lockingcontext to restore on close */
44631 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
44632};
44633
44634/*
44635** The proxy lock file path for the database at dbPath is written into lPath,
44636** which must point to valid, writable memory large enough for a maxLen length
44637** file path.
44638*/
44639static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
44640 int len;
44641 int dbLen;
44642 int i;
44643
44644#ifdef LOCKPROXYDIR
44645 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
44646#else
44647# ifdef _CS_DARWIN_USER_TEMP_DIR
44648 {
44649 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
44650 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
44651 lPath, errno, osGetpid(0)));
44652 return SQLITE_IOERR_LOCK;
44653 }
44654 len = strlcat(lPath, "sqliteplocks", maxLen);
44655 }
44656# else
44657 len = strlcpy(lPath, "/tmp/", maxLen);
44658# endif
44659#endif
44660
44661 if( lPath[len-1]!='/' ){
44662 len = strlcat(lPath, "/", maxLen);
44663 }
44664
44665 /* transform the db path to a unique cache name */
44666 dbLen = (int)strlen(dbPath);
44667 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
44668 char c = dbPath[i];
44669 lPath[i+len] = (c=='/')?'_':c;
44670 }
44671 lPath[i+len]='\0';
44672 strlcat(lPath, ":auto:", maxLen);
44673 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
44674 return SQLITE_OK;
44675}
44676
44677/*
44678 ** Creates the lock file and any missing directories in lockPath
44679 */
44680static int proxyCreateLockPath(const char *lockPath){
44681 int i, len;
44682 char buf[MAXPATHLEN];
44683 int start = 0;
44684
44685 assert(lockPath!=NULL);
44686 /* try to create all the intermediate directories */
44687 len = (int)strlen(lockPath);
44688 buf[0] = lockPath[0];
44689 for( i=1; i<len; i++ ){
44690 if( lockPath[i] == '/' && (i - start > 0) ){
44691 /* only mkdir if leaf dir != "." or "/" or ".." */
44692 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
44693 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
44694 buf[i]='\0';
44695 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
44696 int err=errno;
44697 if( err!=EEXIST ) {
44698 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
44699 "'%s' proxy lock path=%s pid=%d\n",
44700 buf, strerror(err), lockPath, osGetpid(0)));
44701 return err;
44702 }
44703 }
44704 }
44705 start=i+1;
44706 }
44707 buf[i] = lockPath[i];
44708 }
44709 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
44710 return 0;
44711}
44712
44713/*
44714** Create a new VFS file descriptor (stored in memory obtained from
44715** sqlite3_malloc) and open the file named "path" in the file descriptor.
44716**
44717** The caller is responsible not only for closing the file descriptor
44718** but also for freeing the memory associated with the file descriptor.
44719*/
44720static int proxyCreateUnixFile(
44721 const char *path, /* path for the new unixFile */
44722 unixFile **ppFile, /* unixFile created and returned by ref */
44723 int islockfile /* if non zero missing dirs will be created */
44724) {
44725 int fd = -1;
44726 unixFile *pNew;
44727 int rc = SQLITE_OK;
44728 int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW;
44729 sqlite3_vfs dummyVfs;
44730 int terrno = 0;
44731 UnixUnusedFd *pUnused = NULL;
44732
44733 /* 1. first try to open/create the file
44734 ** 2. if that fails, and this is a lock file (not-conch), try creating
44735 ** the parent directories and then try again.
44736 ** 3. if that fails, try to open the file read-only
44737 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
44738 */
44739 pUnused = findReusableFd(path, openFlags);
44740 if( pUnused ){
44741 fd = pUnused->fd;
44742 }else{
44743 pUnused = sqlite3_malloc64(sizeof(*pUnused));
44744 if( !pUnused ){
44745 return SQLITE_NOMEM_BKPT;
44746 }
44747 }
44748 if( fd<0 ){
44749 fd = robust_open(path, openFlags, 0);
44750 terrno = errno;
44751 if( fd<0 && errno==ENOENT && islockfile ){
44752 if( proxyCreateLockPath(path) == SQLITE_OK ){
44753 fd = robust_open(path, openFlags, 0);
44754 }
44755 }
44756 }
44757 if( fd<0 ){
44758 openFlags = O_RDONLY | O_NOFOLLOW;
44759 fd = robust_open(path, openFlags, 0);
44760 terrno = errno;
44761 }
44762 if( fd<0 ){
44763 if( islockfile ){
44764 return SQLITE_BUSY;
44765 }
44766 switch (terrno) {
44767 case EACCES:
44768 return SQLITE_PERM;
44769 case EIO:
44770 return SQLITE_IOERR_LOCK; /* even though it is the conch */
44771 default:
44772 return SQLITE_CANTOPEN_BKPT;
44773 }
44774 }
44775
44776 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
44777 if( pNew==NULL ){
44778 rc = SQLITE_NOMEM_BKPT;
44779 goto end_create_proxy;
44780 }
44781 memset(pNew, 0, sizeof(unixFile));
44782 pNew->openFlags = openFlags;
44783 memset(&dummyVfs, 0, sizeof(dummyVfs));
44784 dummyVfs.pAppData = (void*)&autolockIoFinder;
44785 dummyVfs.zName = "dummy";
44786 pUnused->fd = fd;
44787 pUnused->flags = openFlags;
44788 pNew->pPreallocatedUnused = pUnused;
44789
44790 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
44791 if( rc==SQLITE_OK ){
44792 *ppFile = pNew;
44793 return SQLITE_OK;
44794 }
44795end_create_proxy:
44796 robust_close(pNew, fd, __LINE__);
44797 sqlite3_free(pNew);
44798 sqlite3_free(pUnused);
44799 return rc;
44800}
44801
44802#ifdef SQLITE_TEST
44803/* simulate multiple hosts by creating unique hostid file paths */
44804SQLITE_API int sqlite3_hostid_num = 0;
44805#endif
44806
44807#define PROXY_HOSTIDLEN 16 /* conch file host id length */
44808
44809#if HAVE_GETHOSTUUID
44810/* Not always defined in the headers as it ought to be */
44811extern int gethostuuid(uuid_t id, const struct timespec *wait);
44812#endif
44813
44814/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
44815** bytes of writable memory.
44816*/
44817static int proxyGetHostID(unsigned char *pHostID, int *pError){
44818 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
44819 memset(pHostID, 0, PROXY_HOSTIDLEN);
44820#if HAVE_GETHOSTUUID
44821 {
44822 struct timespec timeout = {1, 0}; /* 1 sec timeout */
44823 if( gethostuuid(pHostID, &timeout) ){
44824 int err = errno;
44825 if( pError ){
44826 *pError = err;
44827 }
44828 return SQLITE_IOERR;
44829 }
44830 }
44831#else
44832 UNUSED_PARAMETER(pError);
44833#endif
44834#ifdef SQLITE_TEST
44835 /* simulate multiple hosts by creating unique hostid file paths */
44836 if( sqlite3_hostid_num != 0){
44837 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
44838 }
44839#endif
44840
44841 return SQLITE_OK;
44842}
44843
44844/* The conch file contains the header, host id and lock file path
44845 */
44846#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
44847#define PROXY_HEADERLEN 1 /* conch file header length */
44848#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
44849#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
44850
44851/*
44852** Takes an open conch file, copies the contents to a new path and then moves
44853** it back. The newly created file's file descriptor is assigned to the
44854** conch file structure and finally the original conch file descriptor is
44855** closed. Returns zero if successful.
44856*/
44857static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
44858 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44859 unixFile *conchFile = pCtx->conchFile;
44860 char tPath[MAXPATHLEN];
44861 char buf[PROXY_MAXCONCHLEN];
44862 char *cPath = pCtx->conchFilePath;
44863 size_t readLen = 0;
44864 size_t pathLen = 0;
44865 char errmsg[64] = "";
44866 int fd = -1;
44867 int rc = -1;
44868 UNUSED_PARAMETER(myHostID);
44869
44870 /* create a new path by replace the trailing '-conch' with '-break' */
44871 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
44872 if( pathLen>MAXPATHLEN || pathLen<6 ||
44873 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
44874 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
44875 goto end_breaklock;
44876 }
44877 /* read the conch content */
44878 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
44879 if( readLen<PROXY_PATHINDEX ){
44880 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
44881 goto end_breaklock;
44882 }
44883 /* write it out to the temporary break file */
44884 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW), 0);
44885 if( fd<0 ){
44886 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
44887 goto end_breaklock;
44888 }
44889 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
44890 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
44891 goto end_breaklock;
44892 }
44893 if( rename(tPath, cPath) ){
44894 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
44895 goto end_breaklock;
44896 }
44897 rc = 0;
44898 fprintf(stderr, "broke stale lock on %s\n", cPath);
44899 robust_close(pFile, conchFile->h, __LINE__);
44900 conchFile->h = fd;
44901 conchFile->openFlags = O_RDWR | O_CREAT;
44902
44903end_breaklock:
44904 if( rc ){
44905 if( fd>=0 ){
44906 osUnlink(tPath);
44907 robust_close(pFile, fd, __LINE__);
44908 }
44909 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
44910 }
44911 return rc;
44912}
44913
44914/* Take the requested lock on the conch file and break a stale lock if the
44915** host id matches.
44916*/
44917static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
44918 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44919 unixFile *conchFile = pCtx->conchFile;
44920 int rc = SQLITE_OK;
44921 int nTries = 0;
44922 struct timespec conchModTime;
44923
44924 memset(&conchModTime, 0, sizeof(conchModTime));
44925 do {
44926 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44927 nTries ++;
44928 if( rc==SQLITE_BUSY ){
44929 /* If the lock failed (busy):
44930 * 1st try: get the mod time of the conch, wait 0.5s and try again.
44931 * 2nd try: fail if the mod time changed or host id is different, wait
44932 * 10 sec and try again
44933 * 3rd try: break the lock unless the mod time has changed.
44934 */
44935 struct stat buf;
44936 if( osFstat(conchFile->h, &buf) ){
44937 storeLastErrno(pFile, errno);
44938 return SQLITE_IOERR_LOCK;
44939 }
44940
44941 if( nTries==1 ){
44942 conchModTime = buf.st_mtimespec;
44943 unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/
44944 continue;
44945 }
44946
44947 assert( nTries>1 );
44948 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
44949 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
44950 return SQLITE_BUSY;
44951 }
44952
44953 if( nTries==2 ){
44954 char tBuf[PROXY_MAXCONCHLEN];
44955 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
44956 if( len<0 ){
44957 storeLastErrno(pFile, errno);
44958 return SQLITE_IOERR_LOCK;
44959 }
44960 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
44961 /* don't break the lock if the host id doesn't match */
44962 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
44963 return SQLITE_BUSY;
44964 }
44965 }else{
44966 /* don't break the lock on short read or a version mismatch */
44967 return SQLITE_BUSY;
44968 }
44969 unixSleep(0,10000000); /* wait 10 sec and try the lock again */
44970 continue;
44971 }
44972
44973 assert( nTries==3 );
44974 if( 0==proxyBreakConchLock(pFile, myHostID) ){
44975 rc = SQLITE_OK;
44976 if( lockType==EXCLUSIVE_LOCK ){
44977 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
44978 }
44979 if( !rc ){
44980 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44981 }
44982 }
44983 }
44984 } while( rc==SQLITE_BUSY && nTries<3 );
44985
44986 return rc;
44987}
44988
44989/* Takes the conch by taking a shared lock and read the contents conch, if
44990** lockPath is non-NULL, the host ID and lock file path must match. A NULL
44991** lockPath means that the lockPath in the conch file will be used if the
44992** host IDs match, or a new lock path will be generated automatically
44993** and written to the conch file.
44994*/
44995static int proxyTakeConch(unixFile *pFile){
44996 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44997
44998 if( pCtx->conchHeld!=0 ){
44999 return SQLITE_OK;
45000 }else{
45001 unixFile *conchFile = pCtx->conchFile;
45002 uuid_t myHostID;
45003 int pError = 0;
45004 char readBuf[PROXY_MAXCONCHLEN];
45005 char lockPath[MAXPATHLEN];
45006 char *tempLockPath = NULL;
45007 int rc = SQLITE_OK;
45008 int createConch = 0;
45009 int hostIdMatch = 0;
45010 int readLen = 0;
45011 int tryOldLockPath = 0;
45012 int forceNewLockPath = 0;
45013
45014 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
45015 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45016 osGetpid(0)));
45017
45018 rc = proxyGetHostID(myHostID, &pError);
45019 if( (rc&0xff)==SQLITE_IOERR ){
45020 storeLastErrno(pFile, pError);
45021 goto end_takeconch;
45022 }
45023 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
45024 if( rc!=SQLITE_OK ){
45025 goto end_takeconch;
45026 }
45027 /* read the existing conch file */
45028 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
45029 if( readLen<0 ){
45030 /* I/O error: lastErrno set by seekAndRead */
45031 storeLastErrno(pFile, conchFile->lastErrno);
45032 rc = SQLITE_IOERR_READ;
45033 goto end_takeconch;
45034 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
45035 readBuf[0]!=(char)PROXY_CONCHVERSION ){
45036 /* a short read or version format mismatch means we need to create a new
45037 ** conch file.
45038 */
45039 createConch = 1;
45040 }
45041 /* if the host id matches and the lock path already exists in the conch
45042 ** we'll try to use the path there, if we can't open that path, we'll
45043 ** retry with a new auto-generated path
45044 */
45045 do { /* in case we need to try again for an :auto: named lock file */
45046
45047 if( !createConch && !forceNewLockPath ){
45048 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
45049 PROXY_HOSTIDLEN);
45050 /* if the conch has data compare the contents */
45051 if( !pCtx->lockProxyPath ){
45052 /* for auto-named local lock file, just check the host ID and we'll
45053 ** use the local lock file path that's already in there
45054 */
45055 if( hostIdMatch ){
45056 size_t pathLen = (readLen - PROXY_PATHINDEX);
45057
45058 if( pathLen>=MAXPATHLEN ){
45059 pathLen=MAXPATHLEN-1;
45060 }
45061 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
45062 lockPath[pathLen] = 0;
45063 tempLockPath = lockPath;
45064 tryOldLockPath = 1;
45065 /* create a copy of the lock path if the conch is taken */
45066 goto end_takeconch;
45067 }
45068 }else if( hostIdMatch
45069 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
45070 readLen-PROXY_PATHINDEX)
45071 ){
45072 /* conch host and lock path match */
45073 goto end_takeconch;
45074 }
45075 }
45076
45077 /* if the conch isn't writable and doesn't match, we can't take it */
45078 if( (conchFile->openFlags&O_RDWR) == 0 ){
45079 rc = SQLITE_BUSY;
45080 goto end_takeconch;
45081 }
45082
45083 /* either the conch didn't match or we need to create a new one */
45084 if( !pCtx->lockProxyPath ){
45085 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
45086 tempLockPath = lockPath;
45087 /* create a copy of the lock path _only_ if the conch is taken */
45088 }
45089
45090 /* update conch with host and path (this will fail if other process
45091 ** has a shared lock already), if the host id matches, use the big
45092 ** stick.
45093 */
45094 futimes(conchFile->h, NULL);
45095 if( hostIdMatch && !createConch ){
45096 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
45097 /* We are trying for an exclusive lock but another thread in this
45098 ** same process is still holding a shared lock. */
45099 rc = SQLITE_BUSY;
45100 } else {
45101 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
45102 }
45103 }else{
45104 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
45105 }
45106 if( rc==SQLITE_OK ){
45107 char writeBuffer[PROXY_MAXCONCHLEN];
45108 int writeSize = 0;
45109
45110 writeBuffer[0] = (char)PROXY_CONCHVERSION;
45111 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
45112 if( pCtx->lockProxyPath!=NULL ){
45113 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
45114 MAXPATHLEN);
45115 }else{
45116 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
45117 }
45118 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
45119 robust_ftruncate(conchFile->h, writeSize);
45120 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
45121 full_fsync(conchFile->h,0,0);
45122 /* If we created a new conch file (not just updated the contents of a
45123 ** valid conch file), try to match the permissions of the database
45124 */
45125 if( rc==SQLITE_OK && createConch ){
45126 struct stat buf;
45127 int err = osFstat(pFile->h, &buf);
45128 if( err==0 ){
45129 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
45130 S_IROTH|S_IWOTH);
45131 /* try to match the database file R/W permissions, ignore failure */
45132#ifndef SQLITE_PROXY_DEBUG
45133 osFchmod(conchFile->h, cmode);
45134#else
45135 do{
45136 rc = osFchmod(conchFile->h, cmode);
45137 }while( rc==(-1) && errno==EINTR );
45138 if( rc!=0 ){
45139 int code = errno;
45140 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
45141 cmode, code, strerror(code));
45142 } else {
45143 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
45144 }
45145 }else{
45146 int code = errno;
45147 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
45148 err, code, strerror(code));
45149#endif
45150 }
45151 }
45152 }
45153 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
45154
45155 end_takeconch:
45156 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
45157 if( rc==SQLITE_OK && pFile->openFlags ){
45158 int fd;
45159 if( pFile->h>=0 ){
45160 robust_close(pFile, pFile->h, __LINE__);
45161 }
45162 pFile->h = -1;
45163 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
45164 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
45165 if( fd>=0 ){
45166 pFile->h = fd;
45167 }else{
45168 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
45169 during locking */
45170 }
45171 }
45172 if( rc==SQLITE_OK && !pCtx->lockProxy ){
45173 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
45174 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
45175 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
45176 /* we couldn't create the proxy lock file with the old lock file path
45177 ** so try again via auto-naming
45178 */
45179 forceNewLockPath = 1;
45180 tryOldLockPath = 0;
45181 continue; /* go back to the do {} while start point, try again */
45182 }
45183 }
45184 if( rc==SQLITE_OK ){
45185 /* Need to make a copy of path if we extracted the value
45186 ** from the conch file or the path was allocated on the stack
45187 */
45188 if( tempLockPath ){
45189 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
45190 if( !pCtx->lockProxyPath ){
45191 rc = SQLITE_NOMEM_BKPT;
45192 }
45193 }
45194 }
45195 if( rc==SQLITE_OK ){
45196 pCtx->conchHeld = 1;
45197
45198 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
45199 afpLockingContext *afpCtx;
45200 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
45201 afpCtx->dbPath = pCtx->lockProxyPath;
45202 }
45203 } else {
45204 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
45205 }
45206 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
45207 rc==SQLITE_OK?"ok":"failed"));
45208 return rc;
45209 } while (1); /* in case we need to retry the :auto: lock file -
45210 ** we should never get here except via the 'continue' call. */
45211 }
45212}
45213
45214/*
45215** If pFile holds a lock on a conch file, then release that lock.
45216*/
45217static int proxyReleaseConch(unixFile *pFile){
45218 int rc = SQLITE_OK; /* Subroutine return code */
45219 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
45220 unixFile *conchFile; /* Name of the conch file */
45221
45222 pCtx = (proxyLockingContext *)pFile->lockingContext;
45223 conchFile = pCtx->conchFile;
45224 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
45225 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45226 osGetpid(0)));
45227 if( pCtx->conchHeld>0 ){
45228 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
45229 }
45230 pCtx->conchHeld = 0;
45231 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
45232 (rc==SQLITE_OK ? "ok" : "failed")));
45233 return rc;
45234}
45235
45236/*
45237** Given the name of a database file, compute the name of its conch file.
45238** Store the conch filename in memory obtained from sqlite3_malloc64().
45239** Make *pConchPath point to the new name. Return SQLITE_OK on success
45240** or SQLITE_NOMEM if unable to obtain memory.
45241**
45242** The caller is responsible for ensuring that the allocated memory
45243** space is eventually freed.
45244**
45245** *pConchPath is set to NULL if a memory allocation error occurs.
45246*/
45247static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
45248 int i; /* Loop counter */
45249 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
45250 char *conchPath; /* buffer in which to construct conch name */
45251
45252 /* Allocate space for the conch filename and initialize the name to
45253 ** the name of the original database file. */
45254 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
45255 if( conchPath==0 ){
45256 return SQLITE_NOMEM_BKPT;
45257 }
45258 memcpy(conchPath, dbPath, len+1);
45259
45260 /* now insert a "." before the last / character */
45261 for( i=(len-1); i>=0; i-- ){
45262 if( conchPath[i]=='/' ){
45263 i++;
45264 break;
45265 }
45266 }
45267 conchPath[i]='.';
45268 while ( i<len ){
45269 conchPath[i+1]=dbPath[i];
45270 i++;
45271 }
45272
45273 /* append the "-conch" suffix to the file */
45274 memcpy(&conchPath[i+1], "-conch", 7);
45275 assert( (int)strlen(conchPath) == len+7 );
45276
45277 return SQLITE_OK;
45278}
45279
45280
45281/* Takes a fully configured proxy locking-style unix file and switches
45282** the local lock file path
45283*/
45284static int switchLockProxyPath(unixFile *pFile, const char *path) {
45285 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
45286 char *oldPath = pCtx->lockProxyPath;
45287 int rc = SQLITE_OK;
45288
45289 if( pFile->eFileLock!=NO_LOCK ){
45290 return SQLITE_BUSY;
45291 }
45292
45293 /* nothing to do if the path is NULL, :auto: or matches the existing path */
45294 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
45295 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
45296 return SQLITE_OK;
45297 }else{
45298 unixFile *lockProxy = pCtx->lockProxy;
45299 pCtx->lockProxy=NULL;
45300 pCtx->conchHeld = 0;
45301 if( lockProxy!=NULL ){
45302 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
45303 if( rc ) return rc;
45304 sqlite3_free(lockProxy);
45305 }
45306 sqlite3_free(oldPath);
45307 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
45308 }
45309
45310 return rc;
45311}
45312
45313/*
45314** pFile is a file that has been opened by a prior xOpen call. dbPath
45315** is a string buffer at least MAXPATHLEN+1 characters in size.
45316**
45317** This routine find the filename associated with pFile and writes it
45318** int dbPath.
45319*/
45320static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
45321#if defined(__APPLE__)
45322 if( pFile->pMethod == &afpIoMethods ){
45323 /* afp style keeps a reference to the db path in the filePath field
45324 ** of the struct */
45325 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
45326 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
45327 MAXPATHLEN);
45328 } else
45329#endif
45330 if( pFile->pMethod == &dotlockIoMethods ){
45331 /* dot lock style uses the locking context to store the dot lock
45332 ** file path */
45333 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
45334 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
45335 }else{
45336 /* all other styles use the locking context to store the db file path */
45337 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
45338 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
45339 }
45340 return SQLITE_OK;
45341}
45342
45343/*
45344** Takes an already filled in unix file and alters it so all file locking
45345** will be performed on the local proxy lock file. The following fields
45346** are preserved in the locking context so that they can be restored and
45347** the unix structure properly cleaned up at close time:
45348** ->lockingContext
45349** ->pMethod
45350*/
45351static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
45352 proxyLockingContext *pCtx;
45353 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
45354 char *lockPath=NULL;
45355 int rc = SQLITE_OK;
45356
45357 if( pFile->eFileLock!=NO_LOCK ){
45358 return SQLITE_BUSY;
45359 }
45360 proxyGetDbPathForUnixFile(pFile, dbPath);
45361 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
45362 lockPath=NULL;
45363 }else{
45364 lockPath=(char *)path;
45365 }
45366
45367 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
45368 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
45369
45370 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
45371 if( pCtx==0 ){
45372 return SQLITE_NOMEM_BKPT;
45373 }
45374 memset(pCtx, 0, sizeof(*pCtx));
45375
45376 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
45377 if( rc==SQLITE_OK ){
45378 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
45379 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
45380 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
45381 ** (c) the file system is read-only, then enable no-locking access.
45382 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
45383 ** that openFlags will have only one of O_RDONLY or O_RDWR.
45384 */
45385 struct statfs fsInfo;
45386 struct stat conchInfo;
45387 int goLockless = 0;
45388
45389 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
45390 int err = errno;
45391 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
45392 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
45393 }
45394 }
45395 if( goLockless ){
45396 pCtx->conchHeld = -1; /* read only FS/ lockless */
45397 rc = SQLITE_OK;
45398 }
45399 }
45400 }
45401 if( rc==SQLITE_OK && lockPath ){
45402 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
45403 }
45404
45405 if( rc==SQLITE_OK ){
45406 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
45407 if( pCtx->dbPath==NULL ){
45408 rc = SQLITE_NOMEM_BKPT;
45409 }
45410 }
45411 if( rc==SQLITE_OK ){
45412 /* all memory is allocated, proxys are created and assigned,
45413 ** switch the locking context and pMethod then return.
45414 */
45415 pCtx->oldLockingContext = pFile->lockingContext;
45416 pFile->lockingContext = pCtx;
45417 pCtx->pOldMethod = pFile->pMethod;
45418 pFile->pMethod = &proxyIoMethods;
45419 }else{
45420 if( pCtx->conchFile ){
45421 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
45422 sqlite3_free(pCtx->conchFile);
45423 }
45424 sqlite3DbFree(0, pCtx->lockProxyPath);
45425 sqlite3_free(pCtx->conchFilePath);
45426 sqlite3_free(pCtx);
45427 }
45428 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
45429 (rc==SQLITE_OK ? "ok" : "failed")));
45430 return rc;
45431}
45432
45433
45434/*
45435** This routine handles sqlite3_file_control() calls that are specific
45436** to proxy locking.
45437*/
45438static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
45439 switch( op ){
45440 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
45441 unixFile *pFile = (unixFile*)id;
45442 if( pFile->pMethod == &proxyIoMethods ){
45443 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
45444 proxyTakeConch(pFile);
45445 if( pCtx->lockProxyPath ){
45446 *(const char **)pArg = pCtx->lockProxyPath;
45447 }else{
45448 *(const char **)pArg = ":auto: (not held)";
45449 }
45450 } else {
45451 *(const char **)pArg = NULL;
45452 }
45453 return SQLITE_OK;
45454 }
45455 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
45456 unixFile *pFile = (unixFile*)id;
45457 int rc = SQLITE_OK;
45458 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
45459 if( pArg==NULL || (const char *)pArg==0 ){
45460 if( isProxyStyle ){
45461 /* turn off proxy locking - not supported. If support is added for
45462 ** switching proxy locking mode off then it will need to fail if
45463 ** the journal mode is WAL mode.
45464 */
45465 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
45466 }else{
45467 /* turn off proxy locking - already off - NOOP */
45468 rc = SQLITE_OK;
45469 }
45470 }else{
45471 const char *proxyPath = (const char *)pArg;
45472 if( isProxyStyle ){
45473 proxyLockingContext *pCtx =
45474 (proxyLockingContext*)pFile->lockingContext;
45475 if( !strcmp(pArg, ":auto:")
45476 || (pCtx->lockProxyPath &&
45477 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
45478 ){
45479 rc = SQLITE_OK;
45480 }else{
45481 rc = switchLockProxyPath(pFile, proxyPath);
45482 }
45483 }else{
45484 /* turn on proxy file locking */
45485 rc = proxyTransformUnixFile(pFile, proxyPath);
45486 }
45487 }
45488 return rc;
45489 }
45490 default: {
45491 assert( 0 ); /* The call assures that only valid opcodes are sent */
45492 }
45493 }
45494 /*NOTREACHED*/ assert(0);
45495 return SQLITE_ERROR;
45496}
45497
45498/*
45499** Within this division (the proxying locking implementation) the procedures
45500** above this point are all utilities. The lock-related methods of the
45501** proxy-locking sqlite3_io_method object follow.
45502*/
45503
45504
45505/*
45506** This routine checks if there is a RESERVED lock held on the specified
45507** file by this or any other process. If such a lock is held, set *pResOut
45508** to a non-zero value otherwise *pResOut is set to zero. The return value
45509** is set to SQLITE_OK unless an I/O error occurs during lock checking.
45510*/
45511static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
45512 unixFile *pFile = (unixFile*)id;
45513 int rc = proxyTakeConch(pFile);
45514 if( rc==SQLITE_OK ){
45515 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45516 if( pCtx->conchHeld>0 ){
45517 unixFile *proxy = pCtx->lockProxy;
45518 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
45519 }else{ /* conchHeld < 0 is lockless */
45520 pResOut=0;
45521 }
45522 }
45523 return rc;
45524}
45525
45526/*
45527** Lock the file with the lock specified by parameter eFileLock - one
45528** of the following:
45529**
45530** (1) SHARED_LOCK
45531** (2) RESERVED_LOCK
45532** (3) PENDING_LOCK
45533** (4) EXCLUSIVE_LOCK
45534**
45535** Sometimes when requesting one lock state, additional lock states
45536** are inserted in between. The locking might fail on one of the later
45537** transitions leaving the lock state different from what it started but
45538** still short of its goal. The following chart shows the allowed
45539** transitions and the inserted intermediate states:
45540**
45541** UNLOCKED -> SHARED
45542** SHARED -> RESERVED
45543** SHARED -> (PENDING) -> EXCLUSIVE
45544** RESERVED -> (PENDING) -> EXCLUSIVE
45545** PENDING -> EXCLUSIVE
45546**
45547** This routine will only increase a lock. Use the sqlite3OsUnlock()
45548** routine to lower a locking level.
45549*/
45550static int proxyLock(sqlite3_file *id, int eFileLock) {
45551 unixFile *pFile = (unixFile*)id;
45552 int rc = proxyTakeConch(pFile);
45553 if( rc==SQLITE_OK ){
45554 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45555 if( pCtx->conchHeld>0 ){
45556 unixFile *proxy = pCtx->lockProxy;
45557 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
45558 pFile->eFileLock = proxy->eFileLock;
45559 }else{
45560 /* conchHeld < 0 is lockless */
45561 }
45562 }
45563 return rc;
45564}
45565
45566
45567/*
45568** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
45569** must be either NO_LOCK or SHARED_LOCK.
45570**
45571** If the locking level of the file descriptor is already at or below
45572** the requested locking level, this routine is a no-op.
45573*/
45574static int proxyUnlock(sqlite3_file *id, int eFileLock) {
45575 unixFile *pFile = (unixFile*)id;
45576 int rc = proxyTakeConch(pFile);
45577 if( rc==SQLITE_OK ){
45578 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45579 if( pCtx->conchHeld>0 ){
45580 unixFile *proxy = pCtx->lockProxy;
45581 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
45582 pFile->eFileLock = proxy->eFileLock;
45583 }else{
45584 /* conchHeld < 0 is lockless */
45585 }
45586 }
45587 return rc;
45588}
45589
45590/*
45591** Close a file that uses proxy locks.
45592*/
45593static int proxyClose(sqlite3_file *id) {
45594 if( ALWAYS(id) ){
45595 unixFile *pFile = (unixFile*)id;
45596 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45597 unixFile *lockProxy = pCtx->lockProxy;
45598 unixFile *conchFile = pCtx->conchFile;
45599 int rc = SQLITE_OK;
45600
45601 if( lockProxy ){
45602 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
45603 if( rc ) return rc;
45604 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
45605 if( rc ) return rc;
45606 sqlite3_free(lockProxy);
45607 pCtx->lockProxy = 0;
45608 }
45609 if( conchFile ){
45610 if( pCtx->conchHeld ){
45611 rc = proxyReleaseConch(pFile);
45612 if( rc ) return rc;
45613 }
45614 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
45615 if( rc ) return rc;
45616 sqlite3_free(conchFile);
45617 }
45618 sqlite3DbFree(0, pCtx->lockProxyPath);
45619 sqlite3_free(pCtx->conchFilePath);
45620 sqlite3DbFree(0, pCtx->dbPath);
45621 /* restore the original locking context and pMethod then close it */
45622 pFile->lockingContext = pCtx->oldLockingContext;
45623 pFile->pMethod = pCtx->pOldMethod;
45624 sqlite3_free(pCtx);
45625 return pFile->pMethod->xClose(id);
45626 }
45627 return SQLITE_OK;
45628}
45629
45630
45631
45632#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
45633/*
45634** The proxy locking style is intended for use with AFP filesystems.
45635** And since AFP is only supported on MacOSX, the proxy locking is also
45636** restricted to MacOSX.
45637**
45638**
45639******************* End of the proxy lock implementation **********************
45640******************************************************************************/
45641
45642/*
45643** Initialize the operating system interface.
45644**
45645** This routine registers all VFS implementations for unix-like operating
45646** systems. This routine, and the sqlite3_os_end() routine that follows,
45647** should be the only routines in this file that are visible from other
45648** files.
45649**
45650** This routine is called once during SQLite initialization and by a
45651** single thread. The memory allocation and mutex subsystems have not
45652** necessarily been initialized when this routine is called, and so they
45653** should not be used.
45654*/
45655SQLITE_API int sqlite3_os_init(void){
45656 /*
45657 ** The following macro defines an initializer for an sqlite3_vfs object.
45658 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
45659 ** to the "finder" function. (pAppData is a pointer to a pointer because
45660 ** silly C90 rules prohibit a void* from being cast to a function pointer
45661 ** and so we have to go through the intermediate pointer to avoid problems
45662 ** when compiling with -pedantic-errors on GCC.)
45663 **
45664 ** The FINDER parameter to this macro is the name of the pointer to the
45665 ** finder-function. The finder-function returns a pointer to the
45666 ** sqlite_io_methods object that implements the desired locking
45667 ** behaviors. See the division above that contains the IOMETHODS
45668 ** macro for addition information on finder-functions.
45669 **
45670 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
45671 ** object. But the "autolockIoFinder" available on MacOSX does a little
45672 ** more than that; it looks at the filesystem type that hosts the
45673 ** database file and tries to choose an locking method appropriate for
45674 ** that filesystem time.
45675 */
45676 #define UNIXVFS(VFSNAME, FINDER) { \
45677 3, /* iVersion */ \
45678 sizeof(unixFile), /* szOsFile */ \
45679 MAX_PATHNAME, /* mxPathname */ \
45680 0, /* pNext */ \
45681 VFSNAME, /* zName */ \
45682 (void*)&FINDER, /* pAppData */ \
45683 unixOpen, /* xOpen */ \
45684 unixDelete, /* xDelete */ \
45685 unixAccess, /* xAccess */ \
45686 unixFullPathname, /* xFullPathname */ \
45687 unixDlOpen, /* xDlOpen */ \
45688 unixDlError, /* xDlError */ \
45689 unixDlSym, /* xDlSym */ \
45690 unixDlClose, /* xDlClose */ \
45691 unixRandomness, /* xRandomness */ \
45692 unixSleep, /* xSleep */ \
45693 unixCurrentTime, /* xCurrentTime */ \
45694 unixGetLastError, /* xGetLastError */ \
45695 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
45696 unixSetSystemCall, /* xSetSystemCall */ \
45697 unixGetSystemCall, /* xGetSystemCall */ \
45698 unixNextSystemCall, /* xNextSystemCall */ \
45699 }
45700
45701 /*
45702 ** All default VFSes for unix are contained in the following array.
45703 **
45704 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
45705 ** by the SQLite core when the VFS is registered. So the following
45706 ** array cannot be const.
45707 */
45708 static sqlite3_vfs aVfs[] = {
45709#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
45710 UNIXVFS("unix", autolockIoFinder ),
45711#elif OS_VXWORKS
45712 UNIXVFS("unix", vxworksIoFinder ),
45713#else
45714 UNIXVFS("unix", posixIoFinder ),
45715#endif
45716 UNIXVFS("unix-none", nolockIoFinder ),
45717 UNIXVFS("unix-dotfile", dotlockIoFinder ),
45718 UNIXVFS("unix-excl", posixIoFinder ),
45719#if OS_VXWORKS
45720 UNIXVFS("unix-namedsem", semIoFinder ),
45721#endif
45722#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
45723 UNIXVFS("unix-posix", posixIoFinder ),
45724#endif
45725#if SQLITE_ENABLE_LOCKING_STYLE
45726 UNIXVFS("unix-flock", flockIoFinder ),
45727#endif
45728#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
45729 UNIXVFS("unix-afp", afpIoFinder ),
45730 UNIXVFS("unix-nfs", nfsIoFinder ),
45731 UNIXVFS("unix-proxy", proxyIoFinder ),
45732#endif
45733 };
45734 unsigned int i; /* Loop counter */
45735
45736 /* Double-check that the aSyscall[] array has been constructed
45737 ** correctly. See ticket [bb3a86e890c8e96ab] */
45738 assert( ArraySize(aSyscall)==29 );
45739
45740 /* Register all VFSes defined in the aVfs[] array */
45741 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
45742#ifdef SQLITE_DEFAULT_UNIX_VFS
45743 sqlite3_vfs_register(&aVfs[i],
45744 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
45745#else
45746 sqlite3_vfs_register(pVfs: &aVfs[i], makeDflt: i==0);
45747#endif
45748 }
45749#ifdef SQLITE_OS_KV_OPTIONAL
45750 sqlite3KvvfsInit();
45751#endif
45752 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
45753
45754#ifndef SQLITE_OMIT_WAL
45755 /* Validate lock assumptions */
45756 assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
45757 assert( UNIX_SHM_BASE==120 ); /* Start of locking area */
45758 /* Locks:
45759 ** WRITE UNIX_SHM_BASE 120
45760 ** CKPT UNIX_SHM_BASE+1 121
45761 ** RECOVER UNIX_SHM_BASE+2 122
45762 ** READ-0 UNIX_SHM_BASE+3 123
45763 ** READ-1 UNIX_SHM_BASE+4 124
45764 ** READ-2 UNIX_SHM_BASE+5 125
45765 ** READ-3 UNIX_SHM_BASE+6 126
45766 ** READ-4 UNIX_SHM_BASE+7 127
45767 ** DMS UNIX_SHM_BASE+8 128
45768 */
45769 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
45770#endif
45771
45772 /* Initialize temp file dir array. */
45773 unixTempFileInit();
45774
45775 return SQLITE_OK;
45776}
45777
45778/*
45779** Shutdown the operating system interface.
45780**
45781** Some operating systems might need to do some cleanup in this routine,
45782** to release dynamically allocated objects. But not on unix.
45783** This routine is a no-op for unix.
45784*/
45785SQLITE_API int sqlite3_os_end(void){
45786 unixBigLock = 0;
45787 return SQLITE_OK;
45788}
45789
45790#endif /* SQLITE_OS_UNIX */
45791
45792/************** End of os_unix.c *********************************************/
45793/************** Begin file os_win.c ******************************************/
45794/*
45795** 2004 May 22
45796**
45797** The author disclaims copyright to this source code. In place of
45798** a legal notice, here is a blessing:
45799**
45800** May you do good and not evil.
45801** May you find forgiveness for yourself and forgive others.
45802** May you share freely, never taking more than you give.
45803**
45804******************************************************************************
45805**
45806** This file contains code that is specific to Windows.
45807*/
45808/* #include "sqliteInt.h" */
45809#if SQLITE_OS_WIN /* This file is used for Windows only */
45810
45811/*
45812** Include code that is common to all os_*.c files
45813*/
45814/* #include "os_common.h" */
45815
45816/*
45817** Include the header file for the Windows VFS.
45818*/
45819/* #include "os_win.h" */
45820
45821/*
45822** Compiling and using WAL mode requires several APIs that are only
45823** available in Windows platforms based on the NT kernel.
45824*/
45825#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
45826# error "WAL mode requires support from the Windows NT kernel, compile\
45827 with SQLITE_OMIT_WAL."
45828#endif
45829
45830#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
45831# error "Memory mapped files require support from the Windows NT kernel,\
45832 compile with SQLITE_MAX_MMAP_SIZE=0."
45833#endif
45834
45835/*
45836** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
45837** based on the sub-platform)?
45838*/
45839#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
45840# define SQLITE_WIN32_HAS_ANSI
45841#endif
45842
45843/*
45844** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
45845** based on the sub-platform)?
45846*/
45847#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
45848 !defined(SQLITE_WIN32_NO_WIDE)
45849# define SQLITE_WIN32_HAS_WIDE
45850#endif
45851
45852/*
45853** Make sure at least one set of Win32 APIs is available.
45854*/
45855#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
45856# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
45857 must be defined."
45858#endif
45859
45860/*
45861** Define the required Windows SDK version constants if they are not
45862** already available.
45863*/
45864#ifndef NTDDI_WIN8
45865# define NTDDI_WIN8 0x06020000
45866#endif
45867
45868#ifndef NTDDI_WINBLUE
45869# define NTDDI_WINBLUE 0x06030000
45870#endif
45871
45872#ifndef NTDDI_WINTHRESHOLD
45873# define NTDDI_WINTHRESHOLD 0x06040000
45874#endif
45875
45876/*
45877** Check to see if the GetVersionEx[AW] functions are deprecated on the
45878** target system. GetVersionEx was first deprecated in Win8.1.
45879*/
45880#ifndef SQLITE_WIN32_GETVERSIONEX
45881# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
45882# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
45883# else
45884# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
45885# endif
45886#endif
45887
45888/*
45889** Check to see if the CreateFileMappingA function is supported on the
45890** target system. It is unavailable when using "mincore.lib" on Win10.
45891** When compiling for Windows 10, always assume "mincore.lib" is in use.
45892*/
45893#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
45894# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
45895# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
45896# else
45897# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
45898# endif
45899#endif
45900
45901/*
45902** This constant should already be defined (in the "WinDef.h" SDK file).
45903*/
45904#ifndef MAX_PATH
45905# define MAX_PATH (260)
45906#endif
45907
45908/*
45909** Maximum pathname length (in chars) for Win32. This should normally be
45910** MAX_PATH.
45911*/
45912#ifndef SQLITE_WIN32_MAX_PATH_CHARS
45913# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
45914#endif
45915
45916/*
45917** This constant should already be defined (in the "WinNT.h" SDK file).
45918*/
45919#ifndef UNICODE_STRING_MAX_CHARS
45920# define UNICODE_STRING_MAX_CHARS (32767)
45921#endif
45922
45923/*
45924** Maximum pathname length (in chars) for WinNT. This should normally be
45925** UNICODE_STRING_MAX_CHARS.
45926*/
45927#ifndef SQLITE_WINNT_MAX_PATH_CHARS
45928# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
45929#endif
45930
45931/*
45932** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
45933** characters, so we allocate 4 bytes per character assuming worst-case of
45934** 4-bytes-per-character for UTF8.
45935*/
45936#ifndef SQLITE_WIN32_MAX_PATH_BYTES
45937# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
45938#endif
45939
45940/*
45941** Maximum pathname length (in bytes) for WinNT. This should normally be
45942** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
45943*/
45944#ifndef SQLITE_WINNT_MAX_PATH_BYTES
45945# define SQLITE_WINNT_MAX_PATH_BYTES \
45946 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
45947#endif
45948
45949/*
45950** Maximum error message length (in chars) for WinRT.
45951*/
45952#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
45953# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
45954#endif
45955
45956/*
45957** Returns non-zero if the character should be treated as a directory
45958** separator.
45959*/
45960#ifndef winIsDirSep
45961# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
45962#endif
45963
45964/*
45965** This macro is used when a local variable is set to a value that is
45966** [sometimes] not used by the code (e.g. via conditional compilation).
45967*/
45968#ifndef UNUSED_VARIABLE_VALUE
45969# define UNUSED_VARIABLE_VALUE(x) (void)(x)
45970#endif
45971
45972/*
45973** Returns the character that should be used as the directory separator.
45974*/
45975#ifndef winGetDirSep
45976# define winGetDirSep() '\\'
45977#endif
45978
45979/*
45980** Do we need to manually define the Win32 file mapping APIs for use with WAL
45981** mode or memory mapped files (e.g. these APIs are available in the Windows
45982** CE SDK; however, they are not present in the header file)?
45983*/
45984#if SQLITE_WIN32_FILEMAPPING_API && \
45985 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
45986/*
45987** Two of the file mapping APIs are different under WinRT. Figure out which
45988** set we need.
45989*/
45990#if SQLITE_OS_WINRT
45991WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
45992 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
45993
45994WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
45995#else
45996#if defined(SQLITE_WIN32_HAS_ANSI)
45997WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
45998 DWORD, DWORD, DWORD, LPCSTR);
45999#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
46000
46001#if defined(SQLITE_WIN32_HAS_WIDE)
46002WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
46003 DWORD, DWORD, DWORD, LPCWSTR);
46004#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
46005
46006WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
46007#endif /* SQLITE_OS_WINRT */
46008
46009/*
46010** These file mapping APIs are common to both Win32 and WinRT.
46011*/
46012
46013WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
46014WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
46015#endif /* SQLITE_WIN32_FILEMAPPING_API */
46016
46017/*
46018** Some Microsoft compilers lack this definition.
46019*/
46020#ifndef INVALID_FILE_ATTRIBUTES
46021# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
46022#endif
46023
46024#ifndef FILE_FLAG_MASK
46025# define FILE_FLAG_MASK (0xFF3C0000)
46026#endif
46027
46028#ifndef FILE_ATTRIBUTE_MASK
46029# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
46030#endif
46031
46032#ifndef SQLITE_OMIT_WAL
46033/* Forward references to structures used for WAL */
46034typedef struct winShm winShm; /* A connection to shared-memory */
46035typedef struct winShmNode winShmNode; /* A region of shared-memory */
46036#endif
46037
46038/*
46039** WinCE lacks native support for file locking so we have to fake it
46040** with some code of our own.
46041*/
46042#if SQLITE_OS_WINCE
46043typedef struct winceLock {
46044 int nReaders; /* Number of reader locks obtained */
46045 BOOL bPending; /* Indicates a pending lock has been obtained */
46046 BOOL bReserved; /* Indicates a reserved lock has been obtained */
46047 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
46048} winceLock;
46049#endif
46050
46051/*
46052** The winFile structure is a subclass of sqlite3_file* specific to the win32
46053** portability layer.
46054*/
46055typedef struct winFile winFile;
46056struct winFile {
46057 const sqlite3_io_methods *pMethod; /*** Must be first ***/
46058 sqlite3_vfs *pVfs; /* The VFS used to open this file */
46059 HANDLE h; /* Handle for accessing the file */
46060 u8 locktype; /* Type of lock currently held on this file */
46061 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
46062 u8 ctrlFlags; /* Flags. See WINFILE_* below */
46063 DWORD lastErrno; /* The Windows errno from the last I/O error */
46064#ifndef SQLITE_OMIT_WAL
46065 winShm *pShm; /* Instance of shared memory on this file */
46066#endif
46067 const char *zPath; /* Full pathname of this file */
46068 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
46069#if SQLITE_OS_WINCE
46070 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
46071 HANDLE hMutex; /* Mutex used to control access to shared lock */
46072 HANDLE hShared; /* Shared memory segment used for locking */
46073 winceLock local; /* Locks obtained by this instance of winFile */
46074 winceLock *shared; /* Global shared lock memory for the file */
46075#endif
46076#if SQLITE_MAX_MMAP_SIZE>0
46077 int nFetchOut; /* Number of outstanding xFetch references */
46078 HANDLE hMap; /* Handle for accessing memory mapping */
46079 void *pMapRegion; /* Area memory mapped */
46080 sqlite3_int64 mmapSize; /* Size of mapped region */
46081 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
46082#endif
46083};
46084
46085/*
46086** The winVfsAppData structure is used for the pAppData member for all of the
46087** Win32 VFS variants.
46088*/
46089typedef struct winVfsAppData winVfsAppData;
46090struct winVfsAppData {
46091 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
46092 void *pAppData; /* The extra pAppData, if any. */
46093 BOOL bNoLock; /* Non-zero if locking is disabled. */
46094};
46095
46096/*
46097** Allowed values for winFile.ctrlFlags
46098*/
46099#define WINFILE_RDONLY 0x02 /* Connection is read only */
46100#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
46101#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
46102
46103/*
46104 * The size of the buffer used by sqlite3_win32_write_debug().
46105 */
46106#ifndef SQLITE_WIN32_DBG_BUF_SIZE
46107# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
46108#endif
46109
46110/*
46111 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
46112 * various Win32 API heap functions instead of our own.
46113 */
46114#ifdef SQLITE_WIN32_MALLOC
46115
46116/*
46117 * If this is non-zero, an isolated heap will be created by the native Win32
46118 * allocator subsystem; otherwise, the default process heap will be used. This
46119 * setting has no effect when compiling for WinRT. By default, this is enabled
46120 * and an isolated heap will be created to store all allocated data.
46121 *
46122 ******************************************************************************
46123 * WARNING: It is important to note that when this setting is non-zero and the
46124 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
46125 * function), all data that was allocated using the isolated heap will
46126 * be freed immediately and any attempt to access any of that freed
46127 * data will almost certainly result in an immediate access violation.
46128 ******************************************************************************
46129 */
46130#ifndef SQLITE_WIN32_HEAP_CREATE
46131# define SQLITE_WIN32_HEAP_CREATE (TRUE)
46132#endif
46133
46134/*
46135 * This is the maximum possible initial size of the Win32-specific heap, in
46136 * bytes.
46137 */
46138#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
46139# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
46140#endif
46141
46142/*
46143 * This is the extra space for the initial size of the Win32-specific heap,
46144 * in bytes. This value may be zero.
46145 */
46146#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
46147# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
46148#endif
46149
46150/*
46151 * Calculate the maximum legal cache size, in pages, based on the maximum
46152 * possible initial heap size and the default page size, setting aside the
46153 * needed extra space.
46154 */
46155#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
46156# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
46157 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
46158 (SQLITE_DEFAULT_PAGE_SIZE))
46159#endif
46160
46161/*
46162 * This is cache size used in the calculation of the initial size of the
46163 * Win32-specific heap. It cannot be negative.
46164 */
46165#ifndef SQLITE_WIN32_CACHE_SIZE
46166# if SQLITE_DEFAULT_CACHE_SIZE>=0
46167# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
46168# else
46169# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
46170# endif
46171#endif
46172
46173/*
46174 * Make sure that the calculated cache size, in pages, cannot cause the
46175 * initial size of the Win32-specific heap to exceed the maximum amount
46176 * of memory that can be specified in the call to HeapCreate.
46177 */
46178#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
46179# undef SQLITE_WIN32_CACHE_SIZE
46180# define SQLITE_WIN32_CACHE_SIZE (2000)
46181#endif
46182
46183/*
46184 * The initial size of the Win32-specific heap. This value may be zero.
46185 */
46186#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
46187# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
46188 (SQLITE_DEFAULT_PAGE_SIZE) + \
46189 (SQLITE_WIN32_HEAP_INIT_EXTRA))
46190#endif
46191
46192/*
46193 * The maximum size of the Win32-specific heap. This value may be zero.
46194 */
46195#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
46196# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
46197#endif
46198
46199/*
46200 * The extra flags to use in calls to the Win32 heap APIs. This value may be
46201 * zero for the default behavior.
46202 */
46203#ifndef SQLITE_WIN32_HEAP_FLAGS
46204# define SQLITE_WIN32_HEAP_FLAGS (0)
46205#endif
46206
46207
46208/*
46209** The winMemData structure stores information required by the Win32-specific
46210** sqlite3_mem_methods implementation.
46211*/
46212typedef struct winMemData winMemData;
46213struct winMemData {
46214#ifndef NDEBUG
46215 u32 magic1; /* Magic number to detect structure corruption. */
46216#endif
46217 HANDLE hHeap; /* The handle to our heap. */
46218 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
46219#ifndef NDEBUG
46220 u32 magic2; /* Magic number to detect structure corruption. */
46221#endif
46222};
46223
46224#ifndef NDEBUG
46225#define WINMEM_MAGIC1 0x42b2830b
46226#define WINMEM_MAGIC2 0xbd4d7cf4
46227#endif
46228
46229static struct winMemData win_mem_data = {
46230#ifndef NDEBUG
46231 WINMEM_MAGIC1,
46232#endif
46233 NULL, FALSE
46234#ifndef NDEBUG
46235 ,WINMEM_MAGIC2
46236#endif
46237};
46238
46239#ifndef NDEBUG
46240#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
46241#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
46242#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
46243#else
46244#define winMemAssertMagic()
46245#endif
46246
46247#define winMemGetDataPtr() &win_mem_data
46248#define winMemGetHeap() win_mem_data.hHeap
46249#define winMemGetOwned() win_mem_data.bOwned
46250
46251static void *winMemMalloc(int nBytes);
46252static void winMemFree(void *pPrior);
46253static void *winMemRealloc(void *pPrior, int nBytes);
46254static int winMemSize(void *p);
46255static int winMemRoundup(int n);
46256static int winMemInit(void *pAppData);
46257static void winMemShutdown(void *pAppData);
46258
46259SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
46260#endif /* SQLITE_WIN32_MALLOC */
46261
46262/*
46263** The following variable is (normally) set once and never changes
46264** thereafter. It records whether the operating system is Win9x
46265** or WinNT.
46266**
46267** 0: Operating system unknown.
46268** 1: Operating system is Win9x.
46269** 2: Operating system is WinNT.
46270**
46271** In order to facilitate testing on a WinNT system, the test fixture
46272** can manually set this value to 1 to emulate Win98 behavior.
46273*/
46274#ifdef SQLITE_TEST
46275SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
46276#else
46277static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
46278#endif
46279
46280#ifndef SYSCALL
46281# define SYSCALL sqlite3_syscall_ptr
46282#endif
46283
46284/*
46285** This function is not available on Windows CE or WinRT.
46286 */
46287
46288#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
46289# define osAreFileApisANSI() 1
46290#endif
46291
46292/*
46293** Many system calls are accessed through pointer-to-functions so that
46294** they may be overridden at runtime to facilitate fault injection during
46295** testing and sandboxing. The following array holds the names and pointers
46296** to all overrideable system calls.
46297*/
46298static struct win_syscall {
46299 const char *zName; /* Name of the system call */
46300 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
46301 sqlite3_syscall_ptr pDefault; /* Default value */
46302} aSyscall[] = {
46303#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
46304 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
46305#else
46306 { "AreFileApisANSI", (SYSCALL)0, 0 },
46307#endif
46308
46309#ifndef osAreFileApisANSI
46310#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
46311#endif
46312
46313#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
46314 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
46315#else
46316 { "CharLowerW", (SYSCALL)0, 0 },
46317#endif
46318
46319#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
46320
46321#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
46322 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
46323#else
46324 { "CharUpperW", (SYSCALL)0, 0 },
46325#endif
46326
46327#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
46328
46329 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
46330
46331#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
46332
46333#if defined(SQLITE_WIN32_HAS_ANSI)
46334 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
46335#else
46336 { "CreateFileA", (SYSCALL)0, 0 },
46337#endif
46338
46339#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
46340 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
46341
46342#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
46343 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
46344#else
46345 { "CreateFileW", (SYSCALL)0, 0 },
46346#endif
46347
46348#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
46349 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
46350
46351#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
46352 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
46353 SQLITE_WIN32_CREATEFILEMAPPINGA
46354 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
46355#else
46356 { "CreateFileMappingA", (SYSCALL)0, 0 },
46357#endif
46358
46359#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
46360 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
46361
46362#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
46363 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
46364 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
46365#else
46366 { "CreateFileMappingW", (SYSCALL)0, 0 },
46367#endif
46368
46369#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
46370 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
46371
46372#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
46373 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
46374#else
46375 { "CreateMutexW", (SYSCALL)0, 0 },
46376#endif
46377
46378#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
46379 LPCWSTR))aSyscall[8].pCurrent)
46380
46381#if defined(SQLITE_WIN32_HAS_ANSI)
46382 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
46383#else
46384 { "DeleteFileA", (SYSCALL)0, 0 },
46385#endif
46386
46387#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
46388
46389#if defined(SQLITE_WIN32_HAS_WIDE)
46390 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
46391#else
46392 { "DeleteFileW", (SYSCALL)0, 0 },
46393#endif
46394
46395#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
46396
46397#if SQLITE_OS_WINCE
46398 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
46399#else
46400 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
46401#endif
46402
46403#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
46404 LPFILETIME))aSyscall[11].pCurrent)
46405
46406#if SQLITE_OS_WINCE
46407 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
46408#else
46409 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
46410#endif
46411
46412#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
46413 LPSYSTEMTIME))aSyscall[12].pCurrent)
46414
46415 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
46416
46417#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
46418
46419#if defined(SQLITE_WIN32_HAS_ANSI)
46420 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
46421#else
46422 { "FormatMessageA", (SYSCALL)0, 0 },
46423#endif
46424
46425#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
46426 DWORD,va_list*))aSyscall[14].pCurrent)
46427
46428#if defined(SQLITE_WIN32_HAS_WIDE)
46429 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
46430#else
46431 { "FormatMessageW", (SYSCALL)0, 0 },
46432#endif
46433
46434#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
46435 DWORD,va_list*))aSyscall[15].pCurrent)
46436
46437#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
46438 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
46439#else
46440 { "FreeLibrary", (SYSCALL)0, 0 },
46441#endif
46442
46443#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
46444
46445 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
46446
46447#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
46448
46449#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
46450 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
46451#else
46452 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
46453#endif
46454
46455#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
46456 LPDWORD))aSyscall[18].pCurrent)
46457
46458#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
46459 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
46460#else
46461 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
46462#endif
46463
46464#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
46465 LPDWORD))aSyscall[19].pCurrent)
46466
46467#if defined(SQLITE_WIN32_HAS_ANSI)
46468 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
46469#else
46470 { "GetFileAttributesA", (SYSCALL)0, 0 },
46471#endif
46472
46473#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
46474
46475#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
46476 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
46477#else
46478 { "GetFileAttributesW", (SYSCALL)0, 0 },
46479#endif
46480
46481#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
46482
46483#if defined(SQLITE_WIN32_HAS_WIDE)
46484 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
46485#else
46486 { "GetFileAttributesExW", (SYSCALL)0, 0 },
46487#endif
46488
46489#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
46490 LPVOID))aSyscall[22].pCurrent)
46491
46492#if !SQLITE_OS_WINRT
46493 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
46494#else
46495 { "GetFileSize", (SYSCALL)0, 0 },
46496#endif
46497
46498#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
46499
46500#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
46501 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
46502#else
46503 { "GetFullPathNameA", (SYSCALL)0, 0 },
46504#endif
46505
46506#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
46507 LPSTR*))aSyscall[24].pCurrent)
46508
46509#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
46510 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
46511#else
46512 { "GetFullPathNameW", (SYSCALL)0, 0 },
46513#endif
46514
46515#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
46516 LPWSTR*))aSyscall[25].pCurrent)
46517
46518 { "GetLastError", (SYSCALL)GetLastError, 0 },
46519
46520#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
46521
46522#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
46523#if SQLITE_OS_WINCE
46524 /* The GetProcAddressA() routine is only available on Windows CE. */
46525 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
46526#else
46527 /* All other Windows platforms expect GetProcAddress() to take
46528 ** an ANSI string regardless of the _UNICODE setting */
46529 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
46530#endif
46531#else
46532 { "GetProcAddressA", (SYSCALL)0, 0 },
46533#endif
46534
46535#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
46536 LPCSTR))aSyscall[27].pCurrent)
46537
46538#if !SQLITE_OS_WINRT
46539 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
46540#else
46541 { "GetSystemInfo", (SYSCALL)0, 0 },
46542#endif
46543
46544#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
46545
46546 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
46547
46548#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
46549
46550#if !SQLITE_OS_WINCE
46551 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
46552#else
46553 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
46554#endif
46555
46556#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
46557 LPFILETIME))aSyscall[30].pCurrent)
46558
46559#if defined(SQLITE_WIN32_HAS_ANSI)
46560 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
46561#else
46562 { "GetTempPathA", (SYSCALL)0, 0 },
46563#endif
46564
46565#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
46566
46567#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
46568 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
46569#else
46570 { "GetTempPathW", (SYSCALL)0, 0 },
46571#endif
46572
46573#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
46574
46575#if !SQLITE_OS_WINRT
46576 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
46577#else
46578 { "GetTickCount", (SYSCALL)0, 0 },
46579#endif
46580
46581#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
46582
46583#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
46584 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
46585#else
46586 { "GetVersionExA", (SYSCALL)0, 0 },
46587#endif
46588
46589#define osGetVersionExA ((BOOL(WINAPI*)( \
46590 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
46591
46592#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
46593 SQLITE_WIN32_GETVERSIONEX
46594 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
46595#else
46596 { "GetVersionExW", (SYSCALL)0, 0 },
46597#endif
46598
46599#define osGetVersionExW ((BOOL(WINAPI*)( \
46600 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
46601
46602 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
46603
46604#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
46605 SIZE_T))aSyscall[36].pCurrent)
46606
46607#if !SQLITE_OS_WINRT
46608 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
46609#else
46610 { "HeapCreate", (SYSCALL)0, 0 },
46611#endif
46612
46613#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
46614 SIZE_T))aSyscall[37].pCurrent)
46615
46616#if !SQLITE_OS_WINRT
46617 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
46618#else
46619 { "HeapDestroy", (SYSCALL)0, 0 },
46620#endif
46621
46622#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
46623
46624 { "HeapFree", (SYSCALL)HeapFree, 0 },
46625
46626#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
46627
46628 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
46629
46630#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
46631 SIZE_T))aSyscall[40].pCurrent)
46632
46633 { "HeapSize", (SYSCALL)HeapSize, 0 },
46634
46635#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
46636 LPCVOID))aSyscall[41].pCurrent)
46637
46638#if !SQLITE_OS_WINRT
46639 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
46640#else
46641 { "HeapValidate", (SYSCALL)0, 0 },
46642#endif
46643
46644#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
46645 LPCVOID))aSyscall[42].pCurrent)
46646
46647#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
46648 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
46649#else
46650 { "HeapCompact", (SYSCALL)0, 0 },
46651#endif
46652
46653#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
46654
46655#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
46656 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
46657#else
46658 { "LoadLibraryA", (SYSCALL)0, 0 },
46659#endif
46660
46661#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
46662
46663#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
46664 !defined(SQLITE_OMIT_LOAD_EXTENSION)
46665 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
46666#else
46667 { "LoadLibraryW", (SYSCALL)0, 0 },
46668#endif
46669
46670#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
46671
46672#if !SQLITE_OS_WINRT
46673 { "LocalFree", (SYSCALL)LocalFree, 0 },
46674#else
46675 { "LocalFree", (SYSCALL)0, 0 },
46676#endif
46677
46678#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
46679
46680#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
46681 { "LockFile", (SYSCALL)LockFile, 0 },
46682#else
46683 { "LockFile", (SYSCALL)0, 0 },
46684#endif
46685
46686#ifndef osLockFile
46687#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
46688 DWORD))aSyscall[47].pCurrent)
46689#endif
46690
46691#if !SQLITE_OS_WINCE
46692 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
46693#else
46694 { "LockFileEx", (SYSCALL)0, 0 },
46695#endif
46696
46697#ifndef osLockFileEx
46698#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
46699 LPOVERLAPPED))aSyscall[48].pCurrent)
46700#endif
46701
46702#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
46703 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
46704 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
46705#else
46706 { "MapViewOfFile", (SYSCALL)0, 0 },
46707#endif
46708
46709#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
46710 SIZE_T))aSyscall[49].pCurrent)
46711
46712 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
46713
46714#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
46715 int))aSyscall[50].pCurrent)
46716
46717 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
46718
46719#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
46720 LARGE_INTEGER*))aSyscall[51].pCurrent)
46721
46722 { "ReadFile", (SYSCALL)ReadFile, 0 },
46723
46724#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
46725 LPOVERLAPPED))aSyscall[52].pCurrent)
46726
46727 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
46728
46729#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
46730
46731#if !SQLITE_OS_WINRT
46732 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
46733#else
46734 { "SetFilePointer", (SYSCALL)0, 0 },
46735#endif
46736
46737#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
46738 DWORD))aSyscall[54].pCurrent)
46739
46740#if !SQLITE_OS_WINRT
46741 { "Sleep", (SYSCALL)Sleep, 0 },
46742#else
46743 { "Sleep", (SYSCALL)0, 0 },
46744#endif
46745
46746#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
46747
46748 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
46749
46750#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
46751 LPFILETIME))aSyscall[56].pCurrent)
46752
46753#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
46754 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
46755#else
46756 { "UnlockFile", (SYSCALL)0, 0 },
46757#endif
46758
46759#ifndef osUnlockFile
46760#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
46761 DWORD))aSyscall[57].pCurrent)
46762#endif
46763
46764#if !SQLITE_OS_WINCE
46765 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
46766#else
46767 { "UnlockFileEx", (SYSCALL)0, 0 },
46768#endif
46769
46770#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
46771 LPOVERLAPPED))aSyscall[58].pCurrent)
46772
46773#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
46774 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
46775#else
46776 { "UnmapViewOfFile", (SYSCALL)0, 0 },
46777#endif
46778
46779#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
46780
46781 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
46782
46783#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
46784 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
46785
46786 { "WriteFile", (SYSCALL)WriteFile, 0 },
46787
46788#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
46789 LPOVERLAPPED))aSyscall[61].pCurrent)
46790
46791#if SQLITE_OS_WINRT
46792 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
46793#else
46794 { "CreateEventExW", (SYSCALL)0, 0 },
46795#endif
46796
46797#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
46798 DWORD,DWORD))aSyscall[62].pCurrent)
46799
46800#if !SQLITE_OS_WINRT
46801 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
46802#else
46803 { "WaitForSingleObject", (SYSCALL)0, 0 },
46804#endif
46805
46806#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
46807 DWORD))aSyscall[63].pCurrent)
46808
46809#if !SQLITE_OS_WINCE
46810 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
46811#else
46812 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
46813#endif
46814
46815#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
46816 BOOL))aSyscall[64].pCurrent)
46817
46818#if SQLITE_OS_WINRT
46819 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
46820#else
46821 { "SetFilePointerEx", (SYSCALL)0, 0 },
46822#endif
46823
46824#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
46825 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
46826
46827#if SQLITE_OS_WINRT
46828 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
46829#else
46830 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
46831#endif
46832
46833#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
46834 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
46835
46836#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
46837 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
46838#else
46839 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
46840#endif
46841
46842#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
46843 SIZE_T))aSyscall[67].pCurrent)
46844
46845#if SQLITE_OS_WINRT
46846 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
46847#else
46848 { "CreateFile2", (SYSCALL)0, 0 },
46849#endif
46850
46851#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
46852 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
46853
46854#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
46855 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
46856#else
46857 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
46858#endif
46859
46860#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
46861 DWORD))aSyscall[69].pCurrent)
46862
46863#if SQLITE_OS_WINRT
46864 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
46865#else
46866 { "GetTickCount64", (SYSCALL)0, 0 },
46867#endif
46868
46869#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
46870
46871#if SQLITE_OS_WINRT
46872 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
46873#else
46874 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
46875#endif
46876
46877#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
46878 LPSYSTEM_INFO))aSyscall[71].pCurrent)
46879
46880#if defined(SQLITE_WIN32_HAS_ANSI)
46881 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
46882#else
46883 { "OutputDebugStringA", (SYSCALL)0, 0 },
46884#endif
46885
46886#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
46887
46888#if defined(SQLITE_WIN32_HAS_WIDE)
46889 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
46890#else
46891 { "OutputDebugStringW", (SYSCALL)0, 0 },
46892#endif
46893
46894#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
46895
46896 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
46897
46898#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
46899
46900#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
46901 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
46902#else
46903 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
46904#endif
46905
46906#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
46907 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
46908
46909/*
46910** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
46911** is really just a macro that uses a compiler intrinsic (e.g. x64).
46912** So do not try to make this is into a redefinable interface.
46913*/
46914#if defined(InterlockedCompareExchange)
46915 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
46916
46917#define osInterlockedCompareExchange InterlockedCompareExchange
46918#else
46919 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
46920
46921#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
46922 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
46923#endif /* defined(InterlockedCompareExchange) */
46924
46925#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
46926 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
46927#else
46928 { "UuidCreate", (SYSCALL)0, 0 },
46929#endif
46930
46931#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
46932
46933#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
46934 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
46935#else
46936 { "UuidCreateSequential", (SYSCALL)0, 0 },
46937#endif
46938
46939#define osUuidCreateSequential \
46940 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
46941
46942#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
46943 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
46944#else
46945 { "FlushViewOfFile", (SYSCALL)0, 0 },
46946#endif
46947
46948#define osFlushViewOfFile \
46949 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
46950
46951}; /* End of the overrideable system calls */
46952
46953/*
46954** This is the xSetSystemCall() method of sqlite3_vfs for all of the
46955** "win32" VFSes. Return SQLITE_OK upon successfully updating the
46956** system call pointer, or SQLITE_NOTFOUND if there is no configurable
46957** system call named zName.
46958*/
46959static int winSetSystemCall(
46960 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
46961 const char *zName, /* Name of system call to override */
46962 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
46963){
46964 unsigned int i;
46965 int rc = SQLITE_NOTFOUND;
46966
46967 UNUSED_PARAMETER(pNotUsed);
46968 if( zName==0 ){
46969 /* If no zName is given, restore all system calls to their default
46970 ** settings and return NULL
46971 */
46972 rc = SQLITE_OK;
46973 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
46974 if( aSyscall[i].pDefault ){
46975 aSyscall[i].pCurrent = aSyscall[i].pDefault;
46976 }
46977 }
46978 }else{
46979 /* If zName is specified, operate on only the one system call
46980 ** specified.
46981 */
46982 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
46983 if( strcmp(zName, aSyscall[i].zName)==0 ){
46984 if( aSyscall[i].pDefault==0 ){
46985 aSyscall[i].pDefault = aSyscall[i].pCurrent;
46986 }
46987 rc = SQLITE_OK;
46988 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
46989 aSyscall[i].pCurrent = pNewFunc;
46990 break;
46991 }
46992 }
46993 }
46994 return rc;
46995}
46996
46997/*
46998** Return the value of a system call. Return NULL if zName is not a
46999** recognized system call name. NULL is also returned if the system call
47000** is currently undefined.
47001*/
47002static sqlite3_syscall_ptr winGetSystemCall(
47003 sqlite3_vfs *pNotUsed,
47004 const char *zName
47005){
47006 unsigned int i;
47007
47008 UNUSED_PARAMETER(pNotUsed);
47009 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
47010 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
47011 }
47012 return 0;
47013}
47014
47015/*
47016** Return the name of the first system call after zName. If zName==NULL
47017** then return the name of the first system call. Return NULL if zName
47018** is the last system call or if zName is not the name of a valid
47019** system call.
47020*/
47021static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
47022 int i = -1;
47023
47024 UNUSED_PARAMETER(p);
47025 if( zName ){
47026 for(i=0; i<ArraySize(aSyscall)-1; i++){
47027 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
47028 }
47029 }
47030 for(i++; i<ArraySize(aSyscall); i++){
47031 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
47032 }
47033 return 0;
47034}
47035
47036#ifdef SQLITE_WIN32_MALLOC
47037/*
47038** If a Win32 native heap has been configured, this function will attempt to
47039** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
47040** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
47041** "pnLargest" argument, if non-zero, will be used to return the size of the
47042** largest committed free block in the heap, in bytes.
47043*/
47044SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
47045 int rc = SQLITE_OK;
47046 UINT nLargest = 0;
47047 HANDLE hHeap;
47048
47049 winMemAssertMagic();
47050 hHeap = winMemGetHeap();
47051 assert( hHeap!=0 );
47052 assert( hHeap!=INVALID_HANDLE_VALUE );
47053#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47054 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
47055#endif
47056#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47057 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
47058 DWORD lastErrno = osGetLastError();
47059 if( lastErrno==NO_ERROR ){
47060 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
47061 (void*)hHeap);
47062 rc = SQLITE_NOMEM_BKPT;
47063 }else{
47064 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
47065 osGetLastError(), (void*)hHeap);
47066 rc = SQLITE_ERROR;
47067 }
47068 }
47069#else
47070 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
47071 (void*)hHeap);
47072 rc = SQLITE_NOTFOUND;
47073#endif
47074 if( pnLargest ) *pnLargest = nLargest;
47075 return rc;
47076}
47077
47078/*
47079** If a Win32 native heap has been configured, this function will attempt to
47080** destroy and recreate it. If the Win32 native heap is not isolated and/or
47081** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
47082** be returned and no changes will be made to the Win32 native heap.
47083*/
47084SQLITE_API int sqlite3_win32_reset_heap(){
47085 int rc;
47086 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
47087 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
47088 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
47089 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
47090 sqlite3_mutex_enter(pMainMtx);
47091 sqlite3_mutex_enter(pMem);
47092 winMemAssertMagic();
47093 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
47094 /*
47095 ** At this point, there should be no outstanding memory allocations on
47096 ** the heap. Also, since both the main and memsys locks are currently
47097 ** being held by us, no other function (i.e. from another thread) should
47098 ** be able to even access the heap. Attempt to destroy and recreate our
47099 ** isolated Win32 native heap now.
47100 */
47101 assert( winMemGetHeap()!=NULL );
47102 assert( winMemGetOwned() );
47103 assert( sqlite3_memory_used()==0 );
47104 winMemShutdown(winMemGetDataPtr());
47105 assert( winMemGetHeap()==NULL );
47106 assert( !winMemGetOwned() );
47107 assert( sqlite3_memory_used()==0 );
47108 rc = winMemInit(winMemGetDataPtr());
47109 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
47110 assert( rc!=SQLITE_OK || winMemGetOwned() );
47111 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
47112 }else{
47113 /*
47114 ** The Win32 native heap cannot be modified because it may be in use.
47115 */
47116 rc = SQLITE_BUSY;
47117 }
47118 sqlite3_mutex_leave(pMem);
47119 sqlite3_mutex_leave(pMainMtx);
47120 return rc;
47121}
47122#endif /* SQLITE_WIN32_MALLOC */
47123
47124/*
47125** This function outputs the specified (ANSI) string to the Win32 debugger
47126** (if available).
47127*/
47128
47129SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
47130 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
47131 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
47132 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
47133 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
47134#ifdef SQLITE_ENABLE_API_ARMOR
47135 if( !zBuf ){
47136 (void)SQLITE_MISUSE_BKPT;
47137 return;
47138 }
47139#endif
47140#if defined(SQLITE_WIN32_HAS_ANSI)
47141 if( nMin>0 ){
47142 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
47143 memcpy(zDbgBuf, zBuf, nMin);
47144 osOutputDebugStringA(zDbgBuf);
47145 }else{
47146 osOutputDebugStringA(zBuf);
47147 }
47148#elif defined(SQLITE_WIN32_HAS_WIDE)
47149 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
47150 if ( osMultiByteToWideChar(
47151 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
47152 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
47153 return;
47154 }
47155 osOutputDebugStringW((LPCWSTR)zDbgBuf);
47156#else
47157 if( nMin>0 ){
47158 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
47159 memcpy(zDbgBuf, zBuf, nMin);
47160 fprintf(stderr, "%s", zDbgBuf);
47161 }else{
47162 fprintf(stderr, "%s", zBuf);
47163 }
47164#endif
47165}
47166
47167/*
47168** The following routine suspends the current thread for at least ms
47169** milliseconds. This is equivalent to the Win32 Sleep() interface.
47170*/
47171#if SQLITE_OS_WINRT
47172static HANDLE sleepObj = NULL;
47173#endif
47174
47175SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
47176#if SQLITE_OS_WINRT
47177 if ( sleepObj==NULL ){
47178 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
47179 SYNCHRONIZE);
47180 }
47181 assert( sleepObj!=NULL );
47182 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
47183#else
47184 osSleep(milliseconds);
47185#endif
47186}
47187
47188#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
47189 SQLITE_THREADSAFE>0
47190SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
47191 DWORD rc;
47192 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
47193 TRUE))==WAIT_IO_COMPLETION ){}
47194 return rc;
47195}
47196#endif
47197
47198/*
47199** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
47200** or WinCE. Return false (zero) for Win95, Win98, or WinME.
47201**
47202** Here is an interesting observation: Win95, Win98, and WinME lack
47203** the LockFileEx() API. But we can still statically link against that
47204** API as long as we don't call it when running Win95/98/ME. A call to
47205** this routine is used to determine if the host is Win95/98/ME or
47206** WinNT/2K/XP so that we will know whether or not we can safely call
47207** the LockFileEx() API.
47208*/
47209
47210#if !SQLITE_WIN32_GETVERSIONEX
47211# define osIsNT() (1)
47212#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
47213# define osIsNT() (1)
47214#elif !defined(SQLITE_WIN32_HAS_WIDE)
47215# define osIsNT() (0)
47216#else
47217# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
47218#endif
47219
47220/*
47221** This function determines if the machine is running a version of Windows
47222** based on the NT kernel.
47223*/
47224SQLITE_API int sqlite3_win32_is_nt(void){
47225#if SQLITE_OS_WINRT
47226 /*
47227 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
47228 ** kernel.
47229 */
47230 return 1;
47231#elif SQLITE_WIN32_GETVERSIONEX
47232 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
47233#if defined(SQLITE_WIN32_HAS_ANSI)
47234 OSVERSIONINFOA sInfo;
47235 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
47236 osGetVersionExA(&sInfo);
47237 osInterlockedCompareExchange(&sqlite3_os_type,
47238 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
47239#elif defined(SQLITE_WIN32_HAS_WIDE)
47240 OSVERSIONINFOW sInfo;
47241 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
47242 osGetVersionExW(&sInfo);
47243 osInterlockedCompareExchange(&sqlite3_os_type,
47244 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
47245#endif
47246 }
47247 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
47248#elif SQLITE_TEST
47249 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
47250#else
47251 /*
47252 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
47253 ** deprecated are always assumed to be based on the NT kernel.
47254 */
47255 return 1;
47256#endif
47257}
47258
47259#ifdef SQLITE_WIN32_MALLOC
47260/*
47261** Allocate nBytes of memory.
47262*/
47263static void *winMemMalloc(int nBytes){
47264 HANDLE hHeap;
47265 void *p;
47266
47267 winMemAssertMagic();
47268 hHeap = winMemGetHeap();
47269 assert( hHeap!=0 );
47270 assert( hHeap!=INVALID_HANDLE_VALUE );
47271#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47272 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
47273#endif
47274 assert( nBytes>=0 );
47275 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
47276 if( !p ){
47277 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
47278 nBytes, osGetLastError(), (void*)hHeap);
47279 }
47280 return p;
47281}
47282
47283/*
47284** Free memory.
47285*/
47286static void winMemFree(void *pPrior){
47287 HANDLE hHeap;
47288
47289 winMemAssertMagic();
47290 hHeap = winMemGetHeap();
47291 assert( hHeap!=0 );
47292 assert( hHeap!=INVALID_HANDLE_VALUE );
47293#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47294 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
47295#endif
47296 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
47297 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
47298 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
47299 pPrior, osGetLastError(), (void*)hHeap);
47300 }
47301}
47302
47303/*
47304** Change the size of an existing memory allocation
47305*/
47306static void *winMemRealloc(void *pPrior, int nBytes){
47307 HANDLE hHeap;
47308 void *p;
47309
47310 winMemAssertMagic();
47311 hHeap = winMemGetHeap();
47312 assert( hHeap!=0 );
47313 assert( hHeap!=INVALID_HANDLE_VALUE );
47314#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47315 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
47316#endif
47317 assert( nBytes>=0 );
47318 if( !pPrior ){
47319 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
47320 }else{
47321 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
47322 }
47323 if( !p ){
47324 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
47325 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
47326 (void*)hHeap);
47327 }
47328 return p;
47329}
47330
47331/*
47332** Return the size of an outstanding allocation, in bytes.
47333*/
47334static int winMemSize(void *p){
47335 HANDLE hHeap;
47336 SIZE_T n;
47337
47338 winMemAssertMagic();
47339 hHeap = winMemGetHeap();
47340 assert( hHeap!=0 );
47341 assert( hHeap!=INVALID_HANDLE_VALUE );
47342#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47343 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
47344#endif
47345 if( !p ) return 0;
47346 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
47347 if( n==(SIZE_T)-1 ){
47348 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
47349 p, osGetLastError(), (void*)hHeap);
47350 return 0;
47351 }
47352 return (int)n;
47353}
47354
47355/*
47356** Round up a request size to the next valid allocation size.
47357*/
47358static int winMemRoundup(int n){
47359 return n;
47360}
47361
47362/*
47363** Initialize this module.
47364*/
47365static int winMemInit(void *pAppData){
47366 winMemData *pWinMemData = (winMemData *)pAppData;
47367
47368 if( !pWinMemData ) return SQLITE_ERROR;
47369 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
47370 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
47371
47372#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
47373 if( !pWinMemData->hHeap ){
47374 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
47375 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
47376 if( dwMaximumSize==0 ){
47377 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
47378 }else if( dwInitialSize>dwMaximumSize ){
47379 dwInitialSize = dwMaximumSize;
47380 }
47381 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
47382 dwInitialSize, dwMaximumSize);
47383 if( !pWinMemData->hHeap ){
47384 sqlite3_log(SQLITE_NOMEM,
47385 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
47386 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
47387 dwMaximumSize);
47388 return SQLITE_NOMEM_BKPT;
47389 }
47390 pWinMemData->bOwned = TRUE;
47391 assert( pWinMemData->bOwned );
47392 }
47393#else
47394 pWinMemData->hHeap = osGetProcessHeap();
47395 if( !pWinMemData->hHeap ){
47396 sqlite3_log(SQLITE_NOMEM,
47397 "failed to GetProcessHeap (%lu)", osGetLastError());
47398 return SQLITE_NOMEM_BKPT;
47399 }
47400 pWinMemData->bOwned = FALSE;
47401 assert( !pWinMemData->bOwned );
47402#endif
47403 assert( pWinMemData->hHeap!=0 );
47404 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
47405#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47406 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
47407#endif
47408 return SQLITE_OK;
47409}
47410
47411/*
47412** Deinitialize this module.
47413*/
47414static void winMemShutdown(void *pAppData){
47415 winMemData *pWinMemData = (winMemData *)pAppData;
47416
47417 if( !pWinMemData ) return;
47418 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
47419 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
47420
47421 if( pWinMemData->hHeap ){
47422 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
47423#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
47424 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
47425#endif
47426 if( pWinMemData->bOwned ){
47427 if( !osHeapDestroy(pWinMemData->hHeap) ){
47428 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
47429 osGetLastError(), (void*)pWinMemData->hHeap);
47430 }
47431 pWinMemData->bOwned = FALSE;
47432 }
47433 pWinMemData->hHeap = NULL;
47434 }
47435}
47436
47437/*
47438** Populate the low-level memory allocation function pointers in
47439** sqlite3GlobalConfig.m with pointers to the routines in this file. The
47440** arguments specify the block of memory to manage.
47441**
47442** This routine is only called by sqlite3_config(), and therefore
47443** is not required to be threadsafe (it is not).
47444*/
47445SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
47446 static const sqlite3_mem_methods winMemMethods = {
47447 winMemMalloc,
47448 winMemFree,
47449 winMemRealloc,
47450 winMemSize,
47451 winMemRoundup,
47452 winMemInit,
47453 winMemShutdown,
47454 &win_mem_data
47455 };
47456 return &winMemMethods;
47457}
47458
47459SQLITE_PRIVATE void sqlite3MemSetDefault(void){
47460 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
47461}
47462#endif /* SQLITE_WIN32_MALLOC */
47463
47464/*
47465** Convert a UTF-8 string to Microsoft Unicode.
47466**
47467** Space to hold the returned string is obtained from sqlite3_malloc().
47468*/
47469static LPWSTR winUtf8ToUnicode(const char *zText){
47470 int nChar;
47471 LPWSTR zWideText;
47472
47473 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
47474 if( nChar==0 ){
47475 return 0;
47476 }
47477 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
47478 if( zWideText==0 ){
47479 return 0;
47480 }
47481 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
47482 nChar);
47483 if( nChar==0 ){
47484 sqlite3_free(zWideText);
47485 zWideText = 0;
47486 }
47487 return zWideText;
47488}
47489
47490/*
47491** Convert a Microsoft Unicode string to UTF-8.
47492**
47493** Space to hold the returned string is obtained from sqlite3_malloc().
47494*/
47495static char *winUnicodeToUtf8(LPCWSTR zWideText){
47496 int nByte;
47497 char *zText;
47498
47499 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
47500 if( nByte == 0 ){
47501 return 0;
47502 }
47503 zText = sqlite3MallocZero( nByte );
47504 if( zText==0 ){
47505 return 0;
47506 }
47507 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
47508 0, 0);
47509 if( nByte == 0 ){
47510 sqlite3_free(zText);
47511 zText = 0;
47512 }
47513 return zText;
47514}
47515
47516/*
47517** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
47518** code page.
47519**
47520** Space to hold the returned string is obtained from sqlite3_malloc().
47521*/
47522static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
47523 int nByte;
47524 LPWSTR zMbcsText;
47525 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
47526
47527 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
47528 0)*sizeof(WCHAR);
47529 if( nByte==0 ){
47530 return 0;
47531 }
47532 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
47533 if( zMbcsText==0 ){
47534 return 0;
47535 }
47536 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
47537 nByte);
47538 if( nByte==0 ){
47539 sqlite3_free(zMbcsText);
47540 zMbcsText = 0;
47541 }
47542 return zMbcsText;
47543}
47544
47545/*
47546** Convert a Microsoft Unicode string to a multi-byte character string,
47547** using the ANSI or OEM code page.
47548**
47549** Space to hold the returned string is obtained from sqlite3_malloc().
47550*/
47551static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
47552 int nByte;
47553 char *zText;
47554 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
47555
47556 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
47557 if( nByte == 0 ){
47558 return 0;
47559 }
47560 zText = sqlite3MallocZero( nByte );
47561 if( zText==0 ){
47562 return 0;
47563 }
47564 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
47565 nByte, 0, 0);
47566 if( nByte == 0 ){
47567 sqlite3_free(zText);
47568 zText = 0;
47569 }
47570 return zText;
47571}
47572
47573/*
47574** Convert a multi-byte character string to UTF-8.
47575**
47576** Space to hold the returned string is obtained from sqlite3_malloc().
47577*/
47578static char *winMbcsToUtf8(const char *zText, int useAnsi){
47579 char *zTextUtf8;
47580 LPWSTR zTmpWide;
47581
47582 zTmpWide = winMbcsToUnicode(zText, useAnsi);
47583 if( zTmpWide==0 ){
47584 return 0;
47585 }
47586 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
47587 sqlite3_free(zTmpWide);
47588 return zTextUtf8;
47589}
47590
47591/*
47592** Convert a UTF-8 string to a multi-byte character string.
47593**
47594** Space to hold the returned string is obtained from sqlite3_malloc().
47595*/
47596static char *winUtf8ToMbcs(const char *zText, int useAnsi){
47597 char *zTextMbcs;
47598 LPWSTR zTmpWide;
47599
47600 zTmpWide = winUtf8ToUnicode(zText);
47601 if( zTmpWide==0 ){
47602 return 0;
47603 }
47604 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
47605 sqlite3_free(zTmpWide);
47606 return zTextMbcs;
47607}
47608
47609/*
47610** This is a public wrapper for the winUtf8ToUnicode() function.
47611*/
47612SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
47613#ifdef SQLITE_ENABLE_API_ARMOR
47614 if( !zText ){
47615 (void)SQLITE_MISUSE_BKPT;
47616 return 0;
47617 }
47618#endif
47619#ifndef SQLITE_OMIT_AUTOINIT
47620 if( sqlite3_initialize() ) return 0;
47621#endif
47622 return winUtf8ToUnicode(zText);
47623}
47624
47625/*
47626** This is a public wrapper for the winUnicodeToUtf8() function.
47627*/
47628SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
47629#ifdef SQLITE_ENABLE_API_ARMOR
47630 if( !zWideText ){
47631 (void)SQLITE_MISUSE_BKPT;
47632 return 0;
47633 }
47634#endif
47635#ifndef SQLITE_OMIT_AUTOINIT
47636 if( sqlite3_initialize() ) return 0;
47637#endif
47638 return winUnicodeToUtf8(zWideText);
47639}
47640
47641/*
47642** This is a public wrapper for the winMbcsToUtf8() function.
47643*/
47644SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
47645#ifdef SQLITE_ENABLE_API_ARMOR
47646 if( !zText ){
47647 (void)SQLITE_MISUSE_BKPT;
47648 return 0;
47649 }
47650#endif
47651#ifndef SQLITE_OMIT_AUTOINIT
47652 if( sqlite3_initialize() ) return 0;
47653#endif
47654 return winMbcsToUtf8(zText, osAreFileApisANSI());
47655}
47656
47657/*
47658** This is a public wrapper for the winMbcsToUtf8() function.
47659*/
47660SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
47661#ifdef SQLITE_ENABLE_API_ARMOR
47662 if( !zText ){
47663 (void)SQLITE_MISUSE_BKPT;
47664 return 0;
47665 }
47666#endif
47667#ifndef SQLITE_OMIT_AUTOINIT
47668 if( sqlite3_initialize() ) return 0;
47669#endif
47670 return winMbcsToUtf8(zText, useAnsi);
47671}
47672
47673/*
47674** This is a public wrapper for the winUtf8ToMbcs() function.
47675*/
47676SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
47677#ifdef SQLITE_ENABLE_API_ARMOR
47678 if( !zText ){
47679 (void)SQLITE_MISUSE_BKPT;
47680 return 0;
47681 }
47682#endif
47683#ifndef SQLITE_OMIT_AUTOINIT
47684 if( sqlite3_initialize() ) return 0;
47685#endif
47686 return winUtf8ToMbcs(zText, osAreFileApisANSI());
47687}
47688
47689/*
47690** This is a public wrapper for the winUtf8ToMbcs() function.
47691*/
47692SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
47693#ifdef SQLITE_ENABLE_API_ARMOR
47694 if( !zText ){
47695 (void)SQLITE_MISUSE_BKPT;
47696 return 0;
47697 }
47698#endif
47699#ifndef SQLITE_OMIT_AUTOINIT
47700 if( sqlite3_initialize() ) return 0;
47701#endif
47702 return winUtf8ToMbcs(zText, useAnsi);
47703}
47704
47705/*
47706** This function is the same as sqlite3_win32_set_directory (below); however,
47707** it accepts a UTF-8 string.
47708*/
47709SQLITE_API int sqlite3_win32_set_directory8(
47710 unsigned long type, /* Identifier for directory being set or reset */
47711 const char *zValue /* New value for directory being set or reset */
47712){
47713 char **ppDirectory = 0;
47714 int rc;
47715#ifndef SQLITE_OMIT_AUTOINIT
47716 rc = sqlite3_initialize();
47717 if( rc ) return rc;
47718#endif
47719 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
47720 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
47721 ppDirectory = &sqlite3_data_directory;
47722 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
47723 ppDirectory = &sqlite3_temp_directory;
47724 }
47725 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
47726 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
47727 );
47728 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
47729 if( ppDirectory ){
47730 char *zCopy = 0;
47731 if( zValue && zValue[0] ){
47732 zCopy = sqlite3_mprintf("%s", zValue);
47733 if ( zCopy==0 ){
47734 rc = SQLITE_NOMEM_BKPT;
47735 goto set_directory8_done;
47736 }
47737 }
47738 sqlite3_free(*ppDirectory);
47739 *ppDirectory = zCopy;
47740 rc = SQLITE_OK;
47741 }else{
47742 rc = SQLITE_ERROR;
47743 }
47744set_directory8_done:
47745 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
47746 return rc;
47747}
47748
47749/*
47750** This function is the same as sqlite3_win32_set_directory (below); however,
47751** it accepts a UTF-16 string.
47752*/
47753SQLITE_API int sqlite3_win32_set_directory16(
47754 unsigned long type, /* Identifier for directory being set or reset */
47755 const void *zValue /* New value for directory being set or reset */
47756){
47757 int rc;
47758 char *zUtf8 = 0;
47759 if( zValue ){
47760 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
47761 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
47762 }
47763 rc = sqlite3_win32_set_directory8(type, zUtf8);
47764 if( zUtf8 ) sqlite3_free(zUtf8);
47765 return rc;
47766}
47767
47768/*
47769** This function sets the data directory or the temporary directory based on
47770** the provided arguments. The type argument must be 1 in order to set the
47771** data directory or 2 in order to set the temporary directory. The zValue
47772** argument is the name of the directory to use. The return value will be
47773** SQLITE_OK if successful.
47774*/
47775SQLITE_API int sqlite3_win32_set_directory(
47776 unsigned long type, /* Identifier for directory being set or reset */
47777 void *zValue /* New value for directory being set or reset */
47778){
47779 return sqlite3_win32_set_directory16(type, zValue);
47780}
47781
47782/*
47783** The return value of winGetLastErrorMsg
47784** is zero if the error message fits in the buffer, or non-zero
47785** otherwise (if the message was truncated).
47786*/
47787static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
47788 /* FormatMessage returns 0 on failure. Otherwise it
47789 ** returns the number of TCHARs written to the output
47790 ** buffer, excluding the terminating null char.
47791 */
47792 DWORD dwLen = 0;
47793 char *zOut = 0;
47794
47795 if( osIsNT() ){
47796#if SQLITE_OS_WINRT
47797 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
47798 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
47799 FORMAT_MESSAGE_IGNORE_INSERTS,
47800 NULL,
47801 lastErrno,
47802 0,
47803 zTempWide,
47804 SQLITE_WIN32_MAX_ERRMSG_CHARS,
47805 0);
47806#else
47807 LPWSTR zTempWide = NULL;
47808 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
47809 FORMAT_MESSAGE_FROM_SYSTEM |
47810 FORMAT_MESSAGE_IGNORE_INSERTS,
47811 NULL,
47812 lastErrno,
47813 0,
47814 (LPWSTR) &zTempWide,
47815 0,
47816 0);
47817#endif
47818 if( dwLen > 0 ){
47819 /* allocate a buffer and convert to UTF8 */
47820 sqlite3BeginBenignMalloc();
47821 zOut = winUnicodeToUtf8(zTempWide);
47822 sqlite3EndBenignMalloc();
47823#if !SQLITE_OS_WINRT
47824 /* free the system buffer allocated by FormatMessage */
47825 osLocalFree(zTempWide);
47826#endif
47827 }
47828 }
47829#ifdef SQLITE_WIN32_HAS_ANSI
47830 else{
47831 char *zTemp = NULL;
47832 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
47833 FORMAT_MESSAGE_FROM_SYSTEM |
47834 FORMAT_MESSAGE_IGNORE_INSERTS,
47835 NULL,
47836 lastErrno,
47837 0,
47838 (LPSTR) &zTemp,
47839 0,
47840 0);
47841 if( dwLen > 0 ){
47842 /* allocate a buffer and convert to UTF8 */
47843 sqlite3BeginBenignMalloc();
47844 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
47845 sqlite3EndBenignMalloc();
47846 /* free the system buffer allocated by FormatMessage */
47847 osLocalFree(zTemp);
47848 }
47849 }
47850#endif
47851 if( 0 == dwLen ){
47852 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
47853 }else{
47854 /* copy a maximum of nBuf chars to output buffer */
47855 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
47856 /* free the UTF8 buffer */
47857 sqlite3_free(zOut);
47858 }
47859 return 0;
47860}
47861
47862/*
47863**
47864** This function - winLogErrorAtLine() - is only ever called via the macro
47865** winLogError().
47866**
47867** This routine is invoked after an error occurs in an OS function.
47868** It logs a message using sqlite3_log() containing the current value of
47869** error code and, if possible, the human-readable equivalent from
47870** FormatMessage.
47871**
47872** The first argument passed to the macro should be the error code that
47873** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
47874** The two subsequent arguments should be the name of the OS function that
47875** failed and the associated file-system path, if any.
47876*/
47877#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
47878static int winLogErrorAtLine(
47879 int errcode, /* SQLite error code */
47880 DWORD lastErrno, /* Win32 last error */
47881 const char *zFunc, /* Name of OS function that failed */
47882 const char *zPath, /* File path associated with error */
47883 int iLine /* Source line number where error occurred */
47884){
47885 char zMsg[500]; /* Human readable error text */
47886 int i; /* Loop counter */
47887
47888 zMsg[0] = 0;
47889 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
47890 assert( errcode!=SQLITE_OK );
47891 if( zPath==0 ) zPath = "";
47892 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
47893 zMsg[i] = 0;
47894 sqlite3_log(errcode,
47895 "os_win.c:%d: (%lu) %s(%s) - %s",
47896 iLine, lastErrno, zFunc, zPath, zMsg
47897 );
47898
47899 return errcode;
47900}
47901
47902/*
47903** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
47904** will be retried following a locking error - probably caused by
47905** antivirus software. Also the initial delay before the first retry.
47906** The delay increases linearly with each retry.
47907*/
47908#ifndef SQLITE_WIN32_IOERR_RETRY
47909# define SQLITE_WIN32_IOERR_RETRY 10
47910#endif
47911#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
47912# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
47913#endif
47914static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
47915static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
47916
47917/*
47918** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
47919** error code obtained via GetLastError() is eligible to be retried. It
47920** must accept the error code DWORD as its only argument and should return
47921** non-zero if the error code is transient in nature and the operation
47922** responsible for generating the original error might succeed upon being
47923** retried. The argument to this macro should be a variable.
47924**
47925** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
47926** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
47927** returns zero. The "winIoerrCanRetry2" macro is completely optional and
47928** may be used to include additional error codes in the set that should
47929** result in the failing I/O operation being retried by the caller. If
47930** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
47931** identical to those of the "winIoerrCanRetry1" macro.
47932*/
47933#if !defined(winIoerrCanRetry1)
47934#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
47935 ((a)==ERROR_SHARING_VIOLATION) || \
47936 ((a)==ERROR_LOCK_VIOLATION) || \
47937 ((a)==ERROR_DEV_NOT_EXIST) || \
47938 ((a)==ERROR_NETNAME_DELETED) || \
47939 ((a)==ERROR_SEM_TIMEOUT) || \
47940 ((a)==ERROR_NETWORK_UNREACHABLE))
47941#endif
47942
47943/*
47944** If a ReadFile() or WriteFile() error occurs, invoke this routine
47945** to see if it should be retried. Return TRUE to retry. Return FALSE
47946** to give up with an error.
47947*/
47948static int winRetryIoerr(int *pnRetry, DWORD *pError){
47949 DWORD e = osGetLastError();
47950 if( *pnRetry>=winIoerrRetry ){
47951 if( pError ){
47952 *pError = e;
47953 }
47954 return 0;
47955 }
47956 if( winIoerrCanRetry1(e) ){
47957 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
47958 ++*pnRetry;
47959 return 1;
47960 }
47961#if defined(winIoerrCanRetry2)
47962 else if( winIoerrCanRetry2(e) ){
47963 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
47964 ++*pnRetry;
47965 return 1;
47966 }
47967#endif
47968 if( pError ){
47969 *pError = e;
47970 }
47971 return 0;
47972}
47973
47974/*
47975** Log a I/O error retry episode.
47976*/
47977static void winLogIoerr(int nRetry, int lineno){
47978 if( nRetry ){
47979 sqlite3_log(SQLITE_NOTICE,
47980 "delayed %dms for lock/sharing conflict at line %d",
47981 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
47982 );
47983 }
47984}
47985
47986/*
47987** This #if does not rely on the SQLITE_OS_WINCE define because the
47988** corresponding section in "date.c" cannot use it.
47989*/
47990#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
47991 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
47992/*
47993** The MSVC CRT on Windows CE may not have a localtime() function.
47994** So define a substitute.
47995*/
47996/* # include <time.h> */
47997struct tm *__cdecl localtime(const time_t *t)
47998{
47999 static struct tm y;
48000 FILETIME uTm, lTm;
48001 SYSTEMTIME pTm;
48002 sqlite3_int64 t64;
48003 t64 = *t;
48004 t64 = (t64 + 11644473600)*10000000;
48005 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
48006 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
48007 osFileTimeToLocalFileTime(&uTm,&lTm);
48008 osFileTimeToSystemTime(&lTm,&pTm);
48009 y.tm_year = pTm.wYear - 1900;
48010 y.tm_mon = pTm.wMonth - 1;
48011 y.tm_wday = pTm.wDayOfWeek;
48012 y.tm_mday = pTm.wDay;
48013 y.tm_hour = pTm.wHour;
48014 y.tm_min = pTm.wMinute;
48015 y.tm_sec = pTm.wSecond;
48016 return &y;
48017}
48018#endif
48019
48020#if SQLITE_OS_WINCE
48021/*************************************************************************
48022** This section contains code for WinCE only.
48023*/
48024#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
48025
48026/*
48027** Acquire a lock on the handle h
48028*/
48029static void winceMutexAcquire(HANDLE h){
48030 DWORD dwErr;
48031 do {
48032 dwErr = osWaitForSingleObject(h, INFINITE);
48033 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
48034}
48035/*
48036** Release a lock acquired by winceMutexAcquire()
48037*/
48038#define winceMutexRelease(h) ReleaseMutex(h)
48039
48040/*
48041** Create the mutex and shared memory used for locking in the file
48042** descriptor pFile
48043*/
48044static int winceCreateLock(const char *zFilename, winFile *pFile){
48045 LPWSTR zTok;
48046 LPWSTR zName;
48047 DWORD lastErrno;
48048 BOOL bLogged = FALSE;
48049 BOOL bInit = TRUE;
48050
48051 zName = winUtf8ToUnicode(zFilename);
48052 if( zName==0 ){
48053 /* out of memory */
48054 return SQLITE_IOERR_NOMEM_BKPT;
48055 }
48056
48057 /* Initialize the local lockdata */
48058 memset(&pFile->local, 0, sizeof(pFile->local));
48059
48060 /* Replace the backslashes from the filename and lowercase it
48061 ** to derive a mutex name. */
48062 zTok = osCharLowerW(zName);
48063 for (;*zTok;zTok++){
48064 if (*zTok == '\\') *zTok = '_';
48065 }
48066
48067 /* Create/open the named mutex */
48068 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
48069 if (!pFile->hMutex){
48070 pFile->lastErrno = osGetLastError();
48071 sqlite3_free(zName);
48072 return winLogError(SQLITE_IOERR, pFile->lastErrno,
48073 "winceCreateLock1", zFilename);
48074 }
48075
48076 /* Acquire the mutex before continuing */
48077 winceMutexAcquire(pFile->hMutex);
48078
48079 /* Since the names of named mutexes, semaphores, file mappings etc are
48080 ** case-sensitive, take advantage of that by uppercasing the mutex name
48081 ** and using that as the shared filemapping name.
48082 */
48083 osCharUpperW(zName);
48084 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
48085 PAGE_READWRITE, 0, sizeof(winceLock),
48086 zName);
48087
48088 /* Set a flag that indicates we're the first to create the memory so it
48089 ** must be zero-initialized */
48090 lastErrno = osGetLastError();
48091 if (lastErrno == ERROR_ALREADY_EXISTS){
48092 bInit = FALSE;
48093 }
48094
48095 sqlite3_free(zName);
48096
48097 /* If we succeeded in making the shared memory handle, map it. */
48098 if( pFile->hShared ){
48099 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
48100 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
48101 /* If mapping failed, close the shared memory handle and erase it */
48102 if( !pFile->shared ){
48103 pFile->lastErrno = osGetLastError();
48104 winLogError(SQLITE_IOERR, pFile->lastErrno,
48105 "winceCreateLock2", zFilename);
48106 bLogged = TRUE;
48107 osCloseHandle(pFile->hShared);
48108 pFile->hShared = NULL;
48109 }
48110 }
48111
48112 /* If shared memory could not be created, then close the mutex and fail */
48113 if( pFile->hShared==NULL ){
48114 if( !bLogged ){
48115 pFile->lastErrno = lastErrno;
48116 winLogError(SQLITE_IOERR, pFile->lastErrno,
48117 "winceCreateLock3", zFilename);
48118 bLogged = TRUE;
48119 }
48120 winceMutexRelease(pFile->hMutex);
48121 osCloseHandle(pFile->hMutex);
48122 pFile->hMutex = NULL;
48123 return SQLITE_IOERR;
48124 }
48125
48126 /* Initialize the shared memory if we're supposed to */
48127 if( bInit ){
48128 memset(pFile->shared, 0, sizeof(winceLock));
48129 }
48130
48131 winceMutexRelease(pFile->hMutex);
48132 return SQLITE_OK;
48133}
48134
48135/*
48136** Destroy the part of winFile that deals with wince locks
48137*/
48138static void winceDestroyLock(winFile *pFile){
48139 if (pFile->hMutex){
48140 /* Acquire the mutex */
48141 winceMutexAcquire(pFile->hMutex);
48142
48143 /* The following blocks should probably assert in debug mode, but they
48144 are to cleanup in case any locks remained open */
48145 if (pFile->local.nReaders){
48146 pFile->shared->nReaders --;
48147 }
48148 if (pFile->local.bReserved){
48149 pFile->shared->bReserved = FALSE;
48150 }
48151 if (pFile->local.bPending){
48152 pFile->shared->bPending = FALSE;
48153 }
48154 if (pFile->local.bExclusive){
48155 pFile->shared->bExclusive = FALSE;
48156 }
48157
48158 /* De-reference and close our copy of the shared memory handle */
48159 osUnmapViewOfFile(pFile->shared);
48160 osCloseHandle(pFile->hShared);
48161
48162 /* Done with the mutex */
48163 winceMutexRelease(pFile->hMutex);
48164 osCloseHandle(pFile->hMutex);
48165 pFile->hMutex = NULL;
48166 }
48167}
48168
48169/*
48170** An implementation of the LockFile() API of Windows for CE
48171*/
48172static BOOL winceLockFile(
48173 LPHANDLE phFile,
48174 DWORD dwFileOffsetLow,
48175 DWORD dwFileOffsetHigh,
48176 DWORD nNumberOfBytesToLockLow,
48177 DWORD nNumberOfBytesToLockHigh
48178){
48179 winFile *pFile = HANDLE_TO_WINFILE(phFile);
48180 BOOL bReturn = FALSE;
48181
48182 UNUSED_PARAMETER(dwFileOffsetHigh);
48183 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
48184
48185 if (!pFile->hMutex) return TRUE;
48186 winceMutexAcquire(pFile->hMutex);
48187
48188 /* Wanting an exclusive lock? */
48189 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
48190 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
48191 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
48192 pFile->shared->bExclusive = TRUE;
48193 pFile->local.bExclusive = TRUE;
48194 bReturn = TRUE;
48195 }
48196 }
48197
48198 /* Want a read-only lock? */
48199 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
48200 nNumberOfBytesToLockLow == 1){
48201 if (pFile->shared->bExclusive == 0){
48202 pFile->local.nReaders ++;
48203 if (pFile->local.nReaders == 1){
48204 pFile->shared->nReaders ++;
48205 }
48206 bReturn = TRUE;
48207 }
48208 }
48209
48210 /* Want a pending lock? */
48211 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
48212 && nNumberOfBytesToLockLow == 1){
48213 /* If no pending lock has been acquired, then acquire it */
48214 if (pFile->shared->bPending == 0) {
48215 pFile->shared->bPending = TRUE;
48216 pFile->local.bPending = TRUE;
48217 bReturn = TRUE;
48218 }
48219 }
48220
48221 /* Want a reserved lock? */
48222 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
48223 && nNumberOfBytesToLockLow == 1){
48224 if (pFile->shared->bReserved == 0) {
48225 pFile->shared->bReserved = TRUE;
48226 pFile->local.bReserved = TRUE;
48227 bReturn = TRUE;
48228 }
48229 }
48230
48231 winceMutexRelease(pFile->hMutex);
48232 return bReturn;
48233}
48234
48235/*
48236** An implementation of the UnlockFile API of Windows for CE
48237*/
48238static BOOL winceUnlockFile(
48239 LPHANDLE phFile,
48240 DWORD dwFileOffsetLow,
48241 DWORD dwFileOffsetHigh,
48242 DWORD nNumberOfBytesToUnlockLow,
48243 DWORD nNumberOfBytesToUnlockHigh
48244){
48245 winFile *pFile = HANDLE_TO_WINFILE(phFile);
48246 BOOL bReturn = FALSE;
48247
48248 UNUSED_PARAMETER(dwFileOffsetHigh);
48249 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
48250
48251 if (!pFile->hMutex) return TRUE;
48252 winceMutexAcquire(pFile->hMutex);
48253
48254 /* Releasing a reader lock or an exclusive lock */
48255 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
48256 /* Did we have an exclusive lock? */
48257 if (pFile->local.bExclusive){
48258 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
48259 pFile->local.bExclusive = FALSE;
48260 pFile->shared->bExclusive = FALSE;
48261 bReturn = TRUE;
48262 }
48263
48264 /* Did we just have a reader lock? */
48265 else if (pFile->local.nReaders){
48266 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
48267 || nNumberOfBytesToUnlockLow == 1);
48268 pFile->local.nReaders --;
48269 if (pFile->local.nReaders == 0)
48270 {
48271 pFile->shared->nReaders --;
48272 }
48273 bReturn = TRUE;
48274 }
48275 }
48276
48277 /* Releasing a pending lock */
48278 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
48279 && nNumberOfBytesToUnlockLow == 1){
48280 if (pFile->local.bPending){
48281 pFile->local.bPending = FALSE;
48282 pFile->shared->bPending = FALSE;
48283 bReturn = TRUE;
48284 }
48285 }
48286 /* Releasing a reserved lock */
48287 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
48288 && nNumberOfBytesToUnlockLow == 1){
48289 if (pFile->local.bReserved) {
48290 pFile->local.bReserved = FALSE;
48291 pFile->shared->bReserved = FALSE;
48292 bReturn = TRUE;
48293 }
48294 }
48295
48296 winceMutexRelease(pFile->hMutex);
48297 return bReturn;
48298}
48299/*
48300** End of the special code for wince
48301*****************************************************************************/
48302#endif /* SQLITE_OS_WINCE */
48303
48304/*
48305** Lock a file region.
48306*/
48307static BOOL winLockFile(
48308 LPHANDLE phFile,
48309 DWORD flags,
48310 DWORD offsetLow,
48311 DWORD offsetHigh,
48312 DWORD numBytesLow,
48313 DWORD numBytesHigh
48314){
48315#if SQLITE_OS_WINCE
48316 /*
48317 ** NOTE: Windows CE is handled differently here due its lack of the Win32
48318 ** API LockFile.
48319 */
48320 return winceLockFile(phFile, offsetLow, offsetHigh,
48321 numBytesLow, numBytesHigh);
48322#else
48323 if( osIsNT() ){
48324 OVERLAPPED ovlp;
48325 memset(&ovlp, 0, sizeof(OVERLAPPED));
48326 ovlp.Offset = offsetLow;
48327 ovlp.OffsetHigh = offsetHigh;
48328 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
48329 }else{
48330 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
48331 numBytesHigh);
48332 }
48333#endif
48334}
48335
48336/*
48337** Unlock a file region.
48338 */
48339static BOOL winUnlockFile(
48340 LPHANDLE phFile,
48341 DWORD offsetLow,
48342 DWORD offsetHigh,
48343 DWORD numBytesLow,
48344 DWORD numBytesHigh
48345){
48346#if SQLITE_OS_WINCE
48347 /*
48348 ** NOTE: Windows CE is handled differently here due its lack of the Win32
48349 ** API UnlockFile.
48350 */
48351 return winceUnlockFile(phFile, offsetLow, offsetHigh,
48352 numBytesLow, numBytesHigh);
48353#else
48354 if( osIsNT() ){
48355 OVERLAPPED ovlp;
48356 memset(&ovlp, 0, sizeof(OVERLAPPED));
48357 ovlp.Offset = offsetLow;
48358 ovlp.OffsetHigh = offsetHigh;
48359 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
48360 }else{
48361 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
48362 numBytesHigh);
48363 }
48364#endif
48365}
48366
48367/*****************************************************************************
48368** The next group of routines implement the I/O methods specified
48369** by the sqlite3_io_methods object.
48370******************************************************************************/
48371
48372/*
48373** Some Microsoft compilers lack this definition.
48374*/
48375#ifndef INVALID_SET_FILE_POINTER
48376# define INVALID_SET_FILE_POINTER ((DWORD)-1)
48377#endif
48378
48379/*
48380** Move the current position of the file handle passed as the first
48381** argument to offset iOffset within the file. If successful, return 0.
48382** Otherwise, set pFile->lastErrno and return non-zero.
48383*/
48384static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
48385#if !SQLITE_OS_WINRT
48386 LONG upperBits; /* Most sig. 32 bits of new offset */
48387 LONG lowerBits; /* Least sig. 32 bits of new offset */
48388 DWORD dwRet; /* Value returned by SetFilePointer() */
48389 DWORD lastErrno; /* Value returned by GetLastError() */
48390
48391 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
48392
48393 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
48394 lowerBits = (LONG)(iOffset & 0xffffffff);
48395
48396 /* API oddity: If successful, SetFilePointer() returns a dword
48397 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
48398 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
48399 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
48400 ** whether an error has actually occurred, it is also necessary to call
48401 ** GetLastError().
48402 */
48403 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
48404
48405 if( (dwRet==INVALID_SET_FILE_POINTER
48406 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
48407 pFile->lastErrno = lastErrno;
48408 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
48409 "winSeekFile", pFile->zPath);
48410 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
48411 return 1;
48412 }
48413
48414 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
48415 return 0;
48416#else
48417 /*
48418 ** Same as above, except that this implementation works for WinRT.
48419 */
48420
48421 LARGE_INTEGER x; /* The new offset */
48422 BOOL bRet; /* Value returned by SetFilePointerEx() */
48423
48424 x.QuadPart = iOffset;
48425 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
48426
48427 if(!bRet){
48428 pFile->lastErrno = osGetLastError();
48429 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
48430 "winSeekFile", pFile->zPath);
48431 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
48432 return 1;
48433 }
48434
48435 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
48436 return 0;
48437#endif
48438}
48439
48440#if SQLITE_MAX_MMAP_SIZE>0
48441/* Forward references to VFS helper methods used for memory mapped files */
48442static int winMapfile(winFile*, sqlite3_int64);
48443static int winUnmapfile(winFile*);
48444#endif
48445
48446/*
48447** Close a file.
48448**
48449** It is reported that an attempt to close a handle might sometimes
48450** fail. This is a very unreasonable result, but Windows is notorious
48451** for being unreasonable so I do not doubt that it might happen. If
48452** the close fails, we pause for 100 milliseconds and try again. As
48453** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
48454** giving up and returning an error.
48455*/
48456#define MX_CLOSE_ATTEMPT 3
48457static int winClose(sqlite3_file *id){
48458 int rc, cnt = 0;
48459 winFile *pFile = (winFile*)id;
48460
48461 assert( id!=0 );
48462#ifndef SQLITE_OMIT_WAL
48463 assert( pFile->pShm==0 );
48464#endif
48465 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
48466 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
48467 osGetCurrentProcessId(), pFile, pFile->h));
48468
48469#if SQLITE_MAX_MMAP_SIZE>0
48470 winUnmapfile(pFile);
48471#endif
48472
48473 do{
48474 rc = osCloseHandle(pFile->h);
48475 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
48476 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
48477#if SQLITE_OS_WINCE
48478#define WINCE_DELETION_ATTEMPTS 3
48479 {
48480 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
48481 if( pAppData==NULL || !pAppData->bNoLock ){
48482 winceDestroyLock(pFile);
48483 }
48484 }
48485 if( pFile->zDeleteOnClose ){
48486 int cnt = 0;
48487 while(
48488 osDeleteFileW(pFile->zDeleteOnClose)==0
48489 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
48490 && cnt++ < WINCE_DELETION_ATTEMPTS
48491 ){
48492 sqlite3_win32_sleep(100); /* Wait a little before trying again */
48493 }
48494 sqlite3_free(pFile->zDeleteOnClose);
48495 }
48496#endif
48497 if( rc ){
48498 pFile->h = NULL;
48499 }
48500 OpenCounter(-1);
48501 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
48502 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
48503 return rc ? SQLITE_OK
48504 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
48505 "winClose", pFile->zPath);
48506}
48507
48508/*
48509** Read data from a file into a buffer. Return SQLITE_OK if all
48510** bytes were read successfully and SQLITE_IOERR if anything goes
48511** wrong.
48512*/
48513static int winRead(
48514 sqlite3_file *id, /* File to read from */
48515 void *pBuf, /* Write content into this buffer */
48516 int amt, /* Number of bytes to read */
48517 sqlite3_int64 offset /* Begin reading at this offset */
48518){
48519#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
48520 OVERLAPPED overlapped; /* The offset for ReadFile. */
48521#endif
48522 winFile *pFile = (winFile*)id; /* file handle */
48523 DWORD nRead; /* Number of bytes actually read from file */
48524 int nRetry = 0; /* Number of retrys */
48525
48526 assert( id!=0 );
48527 assert( amt>0 );
48528 assert( offset>=0 );
48529 SimulateIOError(return SQLITE_IOERR_READ);
48530 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
48531 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
48532 pFile->h, pBuf, amt, offset, pFile->locktype));
48533
48534#if SQLITE_MAX_MMAP_SIZE>0
48535 /* Deal with as much of this read request as possible by transferring
48536 ** data from the memory mapping using memcpy(). */
48537 if( offset<pFile->mmapSize ){
48538 if( offset+amt <= pFile->mmapSize ){
48539 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
48540 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48541 osGetCurrentProcessId(), pFile, pFile->h));
48542 return SQLITE_OK;
48543 }else{
48544 int nCopy = (int)(pFile->mmapSize - offset);
48545 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
48546 pBuf = &((u8 *)pBuf)[nCopy];
48547 amt -= nCopy;
48548 offset += nCopy;
48549 }
48550 }
48551#endif
48552
48553#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
48554 if( winSeekFile(pFile, offset) ){
48555 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
48556 osGetCurrentProcessId(), pFile, pFile->h));
48557 return SQLITE_FULL;
48558 }
48559 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
48560#else
48561 memset(&overlapped, 0, sizeof(OVERLAPPED));
48562 overlapped.Offset = (LONG)(offset & 0xffffffff);
48563 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
48564 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
48565 osGetLastError()!=ERROR_HANDLE_EOF ){
48566#endif
48567 DWORD lastErrno;
48568 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
48569 pFile->lastErrno = lastErrno;
48570 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
48571 osGetCurrentProcessId(), pFile, pFile->h));
48572 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
48573 "winRead", pFile->zPath);
48574 }
48575 winLogIoerr(nRetry, __LINE__);
48576 if( nRead<(DWORD)amt ){
48577 /* Unread parts of the buffer must be zero-filled */
48578 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
48579 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
48580 osGetCurrentProcessId(), pFile, pFile->h));
48581 return SQLITE_IOERR_SHORT_READ;
48582 }
48583
48584 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48585 osGetCurrentProcessId(), pFile, pFile->h));
48586 return SQLITE_OK;
48587}
48588
48589/*
48590** Write data from a buffer into a file. Return SQLITE_OK on success
48591** or some other error code on failure.
48592*/
48593static int winWrite(
48594 sqlite3_file *id, /* File to write into */
48595 const void *pBuf, /* The bytes to be written */
48596 int amt, /* Number of bytes to write */
48597 sqlite3_int64 offset /* Offset into the file to begin writing at */
48598){
48599 int rc = 0; /* True if error has occurred, else false */
48600 winFile *pFile = (winFile*)id; /* File handle */
48601 int nRetry = 0; /* Number of retries */
48602
48603 assert( amt>0 );
48604 assert( pFile );
48605 SimulateIOError(return SQLITE_IOERR_WRITE);
48606 SimulateDiskfullError(return SQLITE_FULL);
48607
48608 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
48609 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
48610 pFile->h, pBuf, amt, offset, pFile->locktype));
48611
48612#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
48613 /* Deal with as much of this write request as possible by transferring
48614 ** data from the memory mapping using memcpy(). */
48615 if( offset<pFile->mmapSize ){
48616 if( offset+amt <= pFile->mmapSize ){
48617 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
48618 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48619 osGetCurrentProcessId(), pFile, pFile->h));
48620 return SQLITE_OK;
48621 }else{
48622 int nCopy = (int)(pFile->mmapSize - offset);
48623 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
48624 pBuf = &((u8 *)pBuf)[nCopy];
48625 amt -= nCopy;
48626 offset += nCopy;
48627 }
48628 }
48629#endif
48630
48631#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
48632 rc = winSeekFile(pFile, offset);
48633 if( rc==0 ){
48634#else
48635 {
48636#endif
48637#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
48638 OVERLAPPED overlapped; /* The offset for WriteFile. */
48639#endif
48640 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
48641 int nRem = amt; /* Number of bytes yet to be written */
48642 DWORD nWrite; /* Bytes written by each WriteFile() call */
48643 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
48644
48645#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
48646 memset(&overlapped, 0, sizeof(OVERLAPPED));
48647 overlapped.Offset = (LONG)(offset & 0xffffffff);
48648 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
48649#endif
48650
48651 while( nRem>0 ){
48652#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
48653 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
48654#else
48655 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
48656#endif
48657 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
48658 break;
48659 }
48660 assert( nWrite==0 || nWrite<=(DWORD)nRem );
48661 if( nWrite==0 || nWrite>(DWORD)nRem ){
48662 lastErrno = osGetLastError();
48663 break;
48664 }
48665#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
48666 offset += nWrite;
48667 overlapped.Offset = (LONG)(offset & 0xffffffff);
48668 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
48669#endif
48670 aRem += nWrite;
48671 nRem -= nWrite;
48672 }
48673 if( nRem>0 ){
48674 pFile->lastErrno = lastErrno;
48675 rc = 1;
48676 }
48677 }
48678
48679 if( rc ){
48680 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
48681 || ( pFile->lastErrno==ERROR_DISK_FULL )){
48682 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
48683 osGetCurrentProcessId(), pFile, pFile->h));
48684 return winLogError(SQLITE_FULL, pFile->lastErrno,
48685 "winWrite1", pFile->zPath);
48686 }
48687 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
48688 osGetCurrentProcessId(), pFile, pFile->h));
48689 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
48690 "winWrite2", pFile->zPath);
48691 }else{
48692 winLogIoerr(nRetry, __LINE__);
48693 }
48694 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48695 osGetCurrentProcessId(), pFile, pFile->h));
48696 return SQLITE_OK;
48697}
48698
48699/*
48700** Truncate an open file to a specified size
48701*/
48702static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
48703 winFile *pFile = (winFile*)id; /* File handle object */
48704 int rc = SQLITE_OK; /* Return code for this function */
48705 DWORD lastErrno;
48706#if SQLITE_MAX_MMAP_SIZE>0
48707 sqlite3_int64 oldMmapSize;
48708 if( pFile->nFetchOut>0 ){
48709 /* File truncation is a no-op if there are outstanding memory mapped
48710 ** pages. This is because truncating the file means temporarily unmapping
48711 ** the file, and that might delete memory out from under existing cursors.
48712 **
48713 ** This can result in incremental vacuum not truncating the file,
48714 ** if there is an active read cursor when the incremental vacuum occurs.
48715 ** No real harm comes of this - the database file is not corrupted,
48716 ** though some folks might complain that the file is bigger than it
48717 ** needs to be.
48718 **
48719 ** The only feasible work-around is to defer the truncation until after
48720 ** all references to memory-mapped content are closed. That is doable,
48721 ** but involves adding a few branches in the common write code path which
48722 ** could slow down normal operations slightly. Hence, we have decided for
48723 ** now to simply make transactions a no-op if there are pending reads. We
48724 ** can maybe revisit this decision in the future.
48725 */
48726 return SQLITE_OK;
48727 }
48728#endif
48729
48730 assert( pFile );
48731 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
48732 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
48733 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
48734
48735 /* If the user has configured a chunk-size for this file, truncate the
48736 ** file so that it consists of an integer number of chunks (i.e. the
48737 ** actual file size after the operation may be larger than the requested
48738 ** size).
48739 */
48740 if( pFile->szChunk>0 ){
48741 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
48742 }
48743
48744#if SQLITE_MAX_MMAP_SIZE>0
48745 if( pFile->pMapRegion ){
48746 oldMmapSize = pFile->mmapSize;
48747 }else{
48748 oldMmapSize = 0;
48749 }
48750 winUnmapfile(pFile);
48751#endif
48752
48753 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
48754 if( winSeekFile(pFile, nByte) ){
48755 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48756 "winTruncate1", pFile->zPath);
48757 }else if( 0==osSetEndOfFile(pFile->h) &&
48758 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
48759 pFile->lastErrno = lastErrno;
48760 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48761 "winTruncate2", pFile->zPath);
48762 }
48763
48764#if SQLITE_MAX_MMAP_SIZE>0
48765 if( rc==SQLITE_OK && oldMmapSize>0 ){
48766 if( oldMmapSize>nByte ){
48767 winMapfile(pFile, -1);
48768 }else{
48769 winMapfile(pFile, oldMmapSize);
48770 }
48771 }
48772#endif
48773
48774 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
48775 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
48776 return rc;
48777}
48778
48779#ifdef SQLITE_TEST
48780/*
48781** Count the number of fullsyncs and normal syncs. This is used to test
48782** that syncs and fullsyncs are occurring at the right times.
48783*/
48784SQLITE_API int sqlite3_sync_count = 0;
48785SQLITE_API int sqlite3_fullsync_count = 0;
48786#endif
48787
48788/*
48789** Make sure all writes to a particular file are committed to disk.
48790*/
48791static int winSync(sqlite3_file *id, int flags){
48792#ifndef SQLITE_NO_SYNC
48793 /*
48794 ** Used only when SQLITE_NO_SYNC is not defined.
48795 */
48796 BOOL rc;
48797#endif
48798#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
48799 defined(SQLITE_HAVE_OS_TRACE)
48800 /*
48801 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
48802 ** OSTRACE() macros.
48803 */
48804 winFile *pFile = (winFile*)id;
48805#else
48806 UNUSED_PARAMETER(id);
48807#endif
48808
48809 assert( pFile );
48810 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
48811 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
48812 || (flags&0x0F)==SQLITE_SYNC_FULL
48813 );
48814
48815 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
48816 ** line is to test that doing so does not cause any problems.
48817 */
48818 SimulateDiskfullError( return SQLITE_FULL );
48819
48820 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
48821 osGetCurrentProcessId(), pFile, pFile->h, flags,
48822 pFile->locktype));
48823
48824#ifndef SQLITE_TEST
48825 UNUSED_PARAMETER(flags);
48826#else
48827 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
48828 sqlite3_fullsync_count++;
48829 }
48830 sqlite3_sync_count++;
48831#endif
48832
48833 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
48834 ** no-op
48835 */
48836#ifdef SQLITE_NO_SYNC
48837 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48838 osGetCurrentProcessId(), pFile, pFile->h));
48839 return SQLITE_OK;
48840#else
48841#if SQLITE_MAX_MMAP_SIZE>0
48842 if( pFile->pMapRegion ){
48843 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
48844 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48845 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
48846 pFile, pFile->pMapRegion));
48847 }else{
48848 pFile->lastErrno = osGetLastError();
48849 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48850 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
48851 pFile, pFile->pMapRegion));
48852 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
48853 "winSync1", pFile->zPath);
48854 }
48855 }
48856#endif
48857 rc = osFlushFileBuffers(pFile->h);
48858 SimulateIOError( rc=FALSE );
48859 if( rc ){
48860 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48861 osGetCurrentProcessId(), pFile, pFile->h));
48862 return SQLITE_OK;
48863 }else{
48864 pFile->lastErrno = osGetLastError();
48865 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
48866 osGetCurrentProcessId(), pFile, pFile->h));
48867 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
48868 "winSync2", pFile->zPath);
48869 }
48870#endif
48871}
48872
48873/*
48874** Determine the current size of a file in bytes
48875*/
48876static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
48877 winFile *pFile = (winFile*)id;
48878 int rc = SQLITE_OK;
48879
48880 assert( id!=0 );
48881 assert( pSize!=0 );
48882 SimulateIOError(return SQLITE_IOERR_FSTAT);
48883 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
48884
48885#if SQLITE_OS_WINRT
48886 {
48887 FILE_STANDARD_INFO info;
48888 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
48889 &info, sizeof(info)) ){
48890 *pSize = info.EndOfFile.QuadPart;
48891 }else{
48892 pFile->lastErrno = osGetLastError();
48893 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48894 "winFileSize", pFile->zPath);
48895 }
48896 }
48897#else
48898 {
48899 DWORD upperBits;
48900 DWORD lowerBits;
48901 DWORD lastErrno;
48902
48903 lowerBits = osGetFileSize(pFile->h, &upperBits);
48904 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
48905 if( (lowerBits == INVALID_FILE_SIZE)
48906 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
48907 pFile->lastErrno = lastErrno;
48908 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48909 "winFileSize", pFile->zPath);
48910 }
48911 }
48912#endif
48913 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
48914 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
48915 return rc;
48916}
48917
48918/*
48919** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
48920*/
48921#ifndef LOCKFILE_FAIL_IMMEDIATELY
48922# define LOCKFILE_FAIL_IMMEDIATELY 1
48923#endif
48924
48925#ifndef LOCKFILE_EXCLUSIVE_LOCK
48926# define LOCKFILE_EXCLUSIVE_LOCK 2
48927#endif
48928
48929/*
48930** Historically, SQLite has used both the LockFile and LockFileEx functions.
48931** When the LockFile function was used, it was always expected to fail
48932** immediately if the lock could not be obtained. Also, it always expected to
48933** obtain an exclusive lock. These flags are used with the LockFileEx function
48934** and reflect those expectations; therefore, they should not be changed.
48935*/
48936#ifndef SQLITE_LOCKFILE_FLAGS
48937# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
48938 LOCKFILE_EXCLUSIVE_LOCK)
48939#endif
48940
48941/*
48942** Currently, SQLite never calls the LockFileEx function without wanting the
48943** call to fail immediately if the lock cannot be obtained.
48944*/
48945#ifndef SQLITE_LOCKFILEEX_FLAGS
48946# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
48947#endif
48948
48949/*
48950** Acquire a reader lock.
48951** Different API routines are called depending on whether or not this
48952** is Win9x or WinNT.
48953*/
48954static int winGetReadLock(winFile *pFile){
48955 int res;
48956 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48957 if( osIsNT() ){
48958#if SQLITE_OS_WINCE
48959 /*
48960 ** NOTE: Windows CE is handled differently here due its lack of the Win32
48961 ** API LockFileEx.
48962 */
48963 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
48964#else
48965 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
48966 SHARED_SIZE, 0);
48967#endif
48968 }
48969#ifdef SQLITE_WIN32_HAS_ANSI
48970 else{
48971 int lk;
48972 sqlite3_randomness(sizeof(lk), &lk);
48973 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
48974 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48975 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48976 }
48977#endif
48978 if( res == 0 ){
48979 pFile->lastErrno = osGetLastError();
48980 /* No need to log a failure to lock */
48981 }
48982 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
48983 return res;
48984}
48985
48986/*
48987** Undo a readlock
48988*/
48989static int winUnlockReadLock(winFile *pFile){
48990 int res;
48991 DWORD lastErrno;
48992 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48993 if( osIsNT() ){
48994 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48995 }
48996#ifdef SQLITE_WIN32_HAS_ANSI
48997 else{
48998 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48999 }
49000#endif
49001 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
49002 pFile->lastErrno = lastErrno;
49003 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
49004 "winUnlockReadLock", pFile->zPath);
49005 }
49006 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
49007 return res;
49008}
49009
49010/*
49011** Lock the file with the lock specified by parameter locktype - one
49012** of the following:
49013**
49014** (1) SHARED_LOCK
49015** (2) RESERVED_LOCK
49016** (3) PENDING_LOCK
49017** (4) EXCLUSIVE_LOCK
49018**
49019** Sometimes when requesting one lock state, additional lock states
49020** are inserted in between. The locking might fail on one of the later
49021** transitions leaving the lock state different from what it started but
49022** still short of its goal. The following chart shows the allowed
49023** transitions and the inserted intermediate states:
49024**
49025** UNLOCKED -> SHARED
49026** SHARED -> RESERVED
49027** SHARED -> (PENDING) -> EXCLUSIVE
49028** RESERVED -> (PENDING) -> EXCLUSIVE
49029** PENDING -> EXCLUSIVE
49030**
49031** This routine will only increase a lock. The winUnlock() routine
49032** erases all locks at once and returns us immediately to locking level 0.
49033** It is not possible to lower the locking level one step at a time. You
49034** must go straight to locking level 0.
49035*/
49036static int winLock(sqlite3_file *id, int locktype){
49037 int rc = SQLITE_OK; /* Return code from subroutines */
49038 int res = 1; /* Result of a Windows lock call */
49039 int newLocktype; /* Set pFile->locktype to this value before exiting */
49040 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
49041 winFile *pFile = (winFile*)id;
49042 DWORD lastErrno = NO_ERROR;
49043
49044 assert( id!=0 );
49045 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
49046 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49047
49048 /* If there is already a lock of this type or more restrictive on the
49049 ** OsFile, do nothing. Don't use the end_lock: exit path, as
49050 ** sqlite3OsEnterMutex() hasn't been called yet.
49051 */
49052 if( pFile->locktype>=locktype ){
49053 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
49054 return SQLITE_OK;
49055 }
49056
49057 /* Do not allow any kind of write-lock on a read-only database
49058 */
49059 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
49060 return SQLITE_IOERR_LOCK;
49061 }
49062
49063 /* Make sure the locking sequence is correct
49064 */
49065 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
49066 assert( locktype!=PENDING_LOCK );
49067 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
49068
49069 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
49070 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
49071 ** the PENDING_LOCK byte is temporary.
49072 */
49073 newLocktype = pFile->locktype;
49074 if( pFile->locktype==NO_LOCK
49075 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
49076 ){
49077 int cnt = 3;
49078 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49079 PENDING_BYTE, 0, 1, 0))==0 ){
49080 /* Try 3 times to get the pending lock. This is needed to work
49081 ** around problems caused by indexing and/or anti-virus software on
49082 ** Windows systems.
49083 ** If you are using this code as a model for alternative VFSes, do not
49084 ** copy this retry logic. It is a hack intended for Windows only.
49085 */
49086 lastErrno = osGetLastError();
49087 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
49088 pFile->h, cnt, res));
49089 if( lastErrno==ERROR_INVALID_HANDLE ){
49090 pFile->lastErrno = lastErrno;
49091 rc = SQLITE_IOERR_LOCK;
49092 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
49093 pFile->h, cnt, sqlite3ErrName(rc)));
49094 return rc;
49095 }
49096 if( cnt ) sqlite3_win32_sleep(1);
49097 }
49098 gotPendingLock = res;
49099 if( !res ){
49100 lastErrno = osGetLastError();
49101 }
49102 }
49103
49104 /* Acquire a shared lock
49105 */
49106 if( locktype==SHARED_LOCK && res ){
49107 assert( pFile->locktype==NO_LOCK );
49108 res = winGetReadLock(pFile);
49109 if( res ){
49110 newLocktype = SHARED_LOCK;
49111 }else{
49112 lastErrno = osGetLastError();
49113 }
49114 }
49115
49116 /* Acquire a RESERVED lock
49117 */
49118 if( locktype==RESERVED_LOCK && res ){
49119 assert( pFile->locktype==SHARED_LOCK );
49120 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
49121 if( res ){
49122 newLocktype = RESERVED_LOCK;
49123 }else{
49124 lastErrno = osGetLastError();
49125 }
49126 }
49127
49128 /* Acquire a PENDING lock
49129 */
49130 if( locktype==EXCLUSIVE_LOCK && res ){
49131 newLocktype = PENDING_LOCK;
49132 gotPendingLock = 0;
49133 }
49134
49135 /* Acquire an EXCLUSIVE lock
49136 */
49137 if( locktype==EXCLUSIVE_LOCK && res ){
49138 assert( pFile->locktype>=SHARED_LOCK );
49139 (void)winUnlockReadLock(pFile);
49140 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
49141 SHARED_SIZE, 0);
49142 if( res ){
49143 newLocktype = EXCLUSIVE_LOCK;
49144 }else{
49145 lastErrno = osGetLastError();
49146 winGetReadLock(pFile);
49147 }
49148 }
49149
49150 /* If we are holding a PENDING lock that ought to be released, then
49151 ** release it now.
49152 */
49153 if( gotPendingLock && locktype==SHARED_LOCK ){
49154 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
49155 }
49156
49157 /* Update the state of the lock has held in the file descriptor then
49158 ** return the appropriate result code.
49159 */
49160 if( res ){
49161 rc = SQLITE_OK;
49162 }else{
49163 pFile->lastErrno = lastErrno;
49164 rc = SQLITE_BUSY;
49165 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
49166 pFile->h, locktype, newLocktype));
49167 }
49168 pFile->locktype = (u8)newLocktype;
49169 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
49170 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
49171 return rc;
49172}
49173
49174/*
49175** This routine checks if there is a RESERVED lock held on the specified
49176** file by this or any other process. If such a lock is held, return
49177** non-zero, otherwise zero.
49178*/
49179static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
49180 int res;
49181 winFile *pFile = (winFile*)id;
49182
49183 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
49184 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
49185
49186 assert( id!=0 );
49187 if( pFile->locktype>=RESERVED_LOCK ){
49188 res = 1;
49189 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
49190 }else{
49191 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
49192 if( res ){
49193 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
49194 }
49195 res = !res;
49196 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
49197 }
49198 *pResOut = res;
49199 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
49200 pFile->h, pResOut, *pResOut));
49201 return SQLITE_OK;
49202}
49203
49204/*
49205** Lower the locking level on file descriptor id to locktype. locktype
49206** must be either NO_LOCK or SHARED_LOCK.
49207**
49208** If the locking level of the file descriptor is already at or below
49209** the requested locking level, this routine is a no-op.
49210**
49211** It is not possible for this routine to fail if the second argument
49212** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
49213** might return SQLITE_IOERR;
49214*/
49215static int winUnlock(sqlite3_file *id, int locktype){
49216 int type;
49217 winFile *pFile = (winFile*)id;
49218 int rc = SQLITE_OK;
49219 assert( pFile!=0 );
49220 assert( locktype<=SHARED_LOCK );
49221 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
49222 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49223 type = pFile->locktype;
49224 if( type>=EXCLUSIVE_LOCK ){
49225 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
49226 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
49227 /* This should never happen. We should always be able to
49228 ** reacquire the read lock */
49229 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
49230 "winUnlock", pFile->zPath);
49231 }
49232 }
49233 if( type>=RESERVED_LOCK ){
49234 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
49235 }
49236 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
49237 winUnlockReadLock(pFile);
49238 }
49239 if( type>=PENDING_LOCK ){
49240 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
49241 }
49242 pFile->locktype = (u8)locktype;
49243 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
49244 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
49245 return rc;
49246}
49247
49248/******************************************************************************
49249****************************** No-op Locking **********************************
49250**
49251** Of the various locking implementations available, this is by far the
49252** simplest: locking is ignored. No attempt is made to lock the database
49253** file for reading or writing.
49254**
49255** This locking mode is appropriate for use on read-only databases
49256** (ex: databases that are burned into CD-ROM, for example.) It can
49257** also be used if the application employs some external mechanism to
49258** prevent simultaneous access of the same database by two or more
49259** database connections. But there is a serious risk of database
49260** corruption if this locking mode is used in situations where multiple
49261** database connections are accessing the same database file at the same
49262** time and one or more of those connections are writing.
49263*/
49264
49265static int winNolockLock(sqlite3_file *id, int locktype){
49266 UNUSED_PARAMETER(id);
49267 UNUSED_PARAMETER(locktype);
49268 return SQLITE_OK;
49269}
49270
49271static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
49272 UNUSED_PARAMETER(id);
49273 UNUSED_PARAMETER(pResOut);
49274 return SQLITE_OK;
49275}
49276
49277static int winNolockUnlock(sqlite3_file *id, int locktype){
49278 UNUSED_PARAMETER(id);
49279 UNUSED_PARAMETER(locktype);
49280 return SQLITE_OK;
49281}
49282
49283/******************* End of the no-op lock implementation *********************
49284******************************************************************************/
49285
49286/*
49287** If *pArg is initially negative then this is a query. Set *pArg to
49288** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
49289**
49290** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
49291*/
49292static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
49293 if( *pArg<0 ){
49294 *pArg = (pFile->ctrlFlags & mask)!=0;
49295 }else if( (*pArg)==0 ){
49296 pFile->ctrlFlags &= ~mask;
49297 }else{
49298 pFile->ctrlFlags |= mask;
49299 }
49300}
49301
49302/* Forward references to VFS helper methods used for temporary files */
49303static int winGetTempname(sqlite3_vfs *, char **);
49304static int winIsDir(const void *);
49305static BOOL winIsLongPathPrefix(const char *);
49306static BOOL winIsDriveLetterAndColon(const char *);
49307
49308/*
49309** Control and query of the open file handle.
49310*/
49311static int winFileControl(sqlite3_file *id, int op, void *pArg){
49312 winFile *pFile = (winFile*)id;
49313 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
49314 switch( op ){
49315 case SQLITE_FCNTL_LOCKSTATE: {
49316 *(int*)pArg = pFile->locktype;
49317 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49318 return SQLITE_OK;
49319 }
49320 case SQLITE_FCNTL_LAST_ERRNO: {
49321 *(int*)pArg = (int)pFile->lastErrno;
49322 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49323 return SQLITE_OK;
49324 }
49325 case SQLITE_FCNTL_CHUNK_SIZE: {
49326 pFile->szChunk = *(int *)pArg;
49327 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49328 return SQLITE_OK;
49329 }
49330 case SQLITE_FCNTL_SIZE_HINT: {
49331 if( pFile->szChunk>0 ){
49332 sqlite3_int64 oldSz;
49333 int rc = winFileSize(id, &oldSz);
49334 if( rc==SQLITE_OK ){
49335 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
49336 if( newSz>oldSz ){
49337 SimulateIOErrorBenign(1);
49338 rc = winTruncate(id, newSz);
49339 SimulateIOErrorBenign(0);
49340 }
49341 }
49342 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
49343 return rc;
49344 }
49345 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49346 return SQLITE_OK;
49347 }
49348 case SQLITE_FCNTL_PERSIST_WAL: {
49349 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
49350 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49351 return SQLITE_OK;
49352 }
49353 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
49354 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
49355 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49356 return SQLITE_OK;
49357 }
49358 case SQLITE_FCNTL_VFSNAME: {
49359 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
49360 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49361 return SQLITE_OK;
49362 }
49363 case SQLITE_FCNTL_WIN32_AV_RETRY: {
49364 int *a = (int*)pArg;
49365 if( a[0]>0 ){
49366 winIoerrRetry = a[0];
49367 }else{
49368 a[0] = winIoerrRetry;
49369 }
49370 if( a[1]>0 ){
49371 winIoerrRetryDelay = a[1];
49372 }else{
49373 a[1] = winIoerrRetryDelay;
49374 }
49375 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49376 return SQLITE_OK;
49377 }
49378 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
49379 LPHANDLE phFile = (LPHANDLE)pArg;
49380 *phFile = pFile->h;
49381 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49382 return SQLITE_OK;
49383 }
49384#ifdef SQLITE_TEST
49385 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
49386 LPHANDLE phFile = (LPHANDLE)pArg;
49387 HANDLE hOldFile = pFile->h;
49388 pFile->h = *phFile;
49389 *phFile = hOldFile;
49390 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
49391 hOldFile, pFile->h));
49392 return SQLITE_OK;
49393 }
49394#endif
49395 case SQLITE_FCNTL_TEMPFILENAME: {
49396 char *zTFile = 0;
49397 int rc = winGetTempname(pFile->pVfs, &zTFile);
49398 if( rc==SQLITE_OK ){
49399 *(char**)pArg = zTFile;
49400 }
49401 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
49402 return rc;
49403 }
49404#if SQLITE_MAX_MMAP_SIZE>0
49405 case SQLITE_FCNTL_MMAP_SIZE: {
49406 i64 newLimit = *(i64*)pArg;
49407 int rc = SQLITE_OK;
49408 if( newLimit>sqlite3GlobalConfig.mxMmap ){
49409 newLimit = sqlite3GlobalConfig.mxMmap;
49410 }
49411
49412 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
49413 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
49414 ** least a 64-bit type. */
49415 if( newLimit>0 && sizeof(SIZE_T)<8 ){
49416 newLimit = (newLimit & 0x7FFFFFFF);
49417 }
49418
49419 *(i64*)pArg = pFile->mmapSizeMax;
49420 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
49421 pFile->mmapSizeMax = newLimit;
49422 if( pFile->mmapSize>0 ){
49423 winUnmapfile(pFile);
49424 rc = winMapfile(pFile, -1);
49425 }
49426 }
49427 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
49428 return rc;
49429 }
49430#endif
49431 }
49432 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
49433 return SQLITE_NOTFOUND;
49434}
49435
49436/*
49437** Return the sector size in bytes of the underlying block device for
49438** the specified file. This is almost always 512 bytes, but may be
49439** larger for some devices.
49440**
49441** SQLite code assumes this function cannot fail. It also assumes that
49442** if two files are created in the same file-system directory (i.e.
49443** a database and its journal file) that the sector size will be the
49444** same for both.
49445*/
49446static int winSectorSize(sqlite3_file *id){
49447 (void)id;
49448 return SQLITE_DEFAULT_SECTOR_SIZE;
49449}
49450
49451/*
49452** Return a vector of device characteristics.
49453*/
49454static int winDeviceCharacteristics(sqlite3_file *id){
49455 winFile *p = (winFile*)id;
49456 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
49457 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
49458}
49459
49460/*
49461** Windows will only let you create file view mappings
49462** on allocation size granularity boundaries.
49463** During sqlite3_os_init() we do a GetSystemInfo()
49464** to get the granularity size.
49465*/
49466static SYSTEM_INFO winSysInfo;
49467
49468#ifndef SQLITE_OMIT_WAL
49469
49470/*
49471** Helper functions to obtain and relinquish the global mutex. The
49472** global mutex is used to protect the winLockInfo objects used by
49473** this file, all of which may be shared by multiple threads.
49474**
49475** Function winShmMutexHeld() is used to assert() that the global mutex
49476** is held when required. This function is only used as part of assert()
49477** statements. e.g.
49478**
49479** winShmEnterMutex()
49480** assert( winShmMutexHeld() );
49481** winShmLeaveMutex()
49482*/
49483static sqlite3_mutex *winBigLock = 0;
49484static void winShmEnterMutex(void){
49485 sqlite3_mutex_enter(winBigLock);
49486}
49487static void winShmLeaveMutex(void){
49488 sqlite3_mutex_leave(winBigLock);
49489}
49490#ifndef NDEBUG
49491static int winShmMutexHeld(void) {
49492 return sqlite3_mutex_held(winBigLock);
49493}
49494#endif
49495
49496/*
49497** Object used to represent a single file opened and mmapped to provide
49498** shared memory. When multiple threads all reference the same
49499** log-summary, each thread has its own winFile object, but they all
49500** point to a single instance of this object. In other words, each
49501** log-summary is opened only once per process.
49502**
49503** winShmMutexHeld() must be true when creating or destroying
49504** this object or while reading or writing the following fields:
49505**
49506** nRef
49507** pNext
49508**
49509** The following fields are read-only after the object is created:
49510**
49511** fid
49512** zFilename
49513**
49514** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
49515** winShmMutexHeld() is true when reading or writing any other field
49516** in this structure.
49517**
49518*/
49519struct winShmNode {
49520 sqlite3_mutex *mutex; /* Mutex to access this object */
49521 char *zFilename; /* Name of the file */
49522 winFile hFile; /* File handle from winOpen */
49523
49524 int szRegion; /* Size of shared-memory regions */
49525 int nRegion; /* Size of array apRegion */
49526 u8 isReadonly; /* True if read-only */
49527 u8 isUnlocked; /* True if no DMS lock held */
49528
49529 struct ShmRegion {
49530 HANDLE hMap; /* File handle from CreateFileMapping */
49531 void *pMap;
49532 } *aRegion;
49533 DWORD lastErrno; /* The Windows errno from the last I/O error */
49534
49535 int nRef; /* Number of winShm objects pointing to this */
49536 winShm *pFirst; /* All winShm objects pointing to this */
49537 winShmNode *pNext; /* Next in list of all winShmNode objects */
49538#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
49539 u8 nextShmId; /* Next available winShm.id value */
49540#endif
49541};
49542
49543/*
49544** A global array of all winShmNode objects.
49545**
49546** The winShmMutexHeld() must be true while reading or writing this list.
49547*/
49548static winShmNode *winShmNodeList = 0;
49549
49550/*
49551** Structure used internally by this VFS to record the state of an
49552** open shared memory connection.
49553**
49554** The following fields are initialized when this object is created and
49555** are read-only thereafter:
49556**
49557** winShm.pShmNode
49558** winShm.id
49559**
49560** All other fields are read/write. The winShm.pShmNode->mutex must be held
49561** while accessing any read/write fields.
49562*/
49563struct winShm {
49564 winShmNode *pShmNode; /* The underlying winShmNode object */
49565 winShm *pNext; /* Next winShm with the same winShmNode */
49566 u8 hasMutex; /* True if holding the winShmNode mutex */
49567 u16 sharedMask; /* Mask of shared locks held */
49568 u16 exclMask; /* Mask of exclusive locks held */
49569#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
49570 u8 id; /* Id of this connection with its winShmNode */
49571#endif
49572};
49573
49574/*
49575** Constants used for locking
49576*/
49577#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
49578#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
49579
49580/*
49581** Apply advisory locks for all n bytes beginning at ofst.
49582*/
49583#define WINSHM_UNLCK 1
49584#define WINSHM_RDLCK 2
49585#define WINSHM_WRLCK 3
49586static int winShmSystemLock(
49587 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
49588 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
49589 int ofst, /* Offset to first byte to be locked/unlocked */
49590 int nByte /* Number of bytes to lock or unlock */
49591){
49592 int rc = 0; /* Result code form Lock/UnlockFileEx() */
49593
49594 /* Access to the winShmNode object is serialized by the caller */
49595 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
49596
49597 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
49598 pFile->hFile.h, lockType, ofst, nByte));
49599
49600 /* Release/Acquire the system-level lock */
49601 if( lockType==WINSHM_UNLCK ){
49602 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
49603 }else{
49604 /* Initialize the locking parameters */
49605 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
49606 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
49607 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
49608 }
49609
49610 if( rc!= 0 ){
49611 rc = SQLITE_OK;
49612 }else{
49613 pFile->lastErrno = osGetLastError();
49614 rc = SQLITE_BUSY;
49615 }
49616
49617 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
49618 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
49619 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
49620
49621 return rc;
49622}
49623
49624/* Forward references to VFS methods */
49625static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
49626static int winDelete(sqlite3_vfs *,const char*,int);
49627
49628/*
49629** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
49630**
49631** This is not a VFS shared-memory method; it is a utility function called
49632** by VFS shared-memory methods.
49633*/
49634static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
49635 winShmNode **pp;
49636 winShmNode *p;
49637 assert( winShmMutexHeld() );
49638 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
49639 osGetCurrentProcessId(), deleteFlag));
49640 pp = &winShmNodeList;
49641 while( (p = *pp)!=0 ){
49642 if( p->nRef==0 ){
49643 int i;
49644 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
49645 for(i=0; i<p->nRegion; i++){
49646 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
49647 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
49648 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
49649 UNUSED_VARIABLE_VALUE(bRc);
49650 bRc = osCloseHandle(p->aRegion[i].hMap);
49651 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
49652 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
49653 UNUSED_VARIABLE_VALUE(bRc);
49654 }
49655 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
49656 SimulateIOErrorBenign(1);
49657 winClose((sqlite3_file *)&p->hFile);
49658 SimulateIOErrorBenign(0);
49659 }
49660 if( deleteFlag ){
49661 SimulateIOErrorBenign(1);
49662 sqlite3BeginBenignMalloc();
49663 winDelete(pVfs, p->zFilename, 0);
49664 sqlite3EndBenignMalloc();
49665 SimulateIOErrorBenign(0);
49666 }
49667 *pp = p->pNext;
49668 sqlite3_free(p->aRegion);
49669 sqlite3_free(p);
49670 }else{
49671 pp = &p->pNext;
49672 }
49673 }
49674}
49675
49676/*
49677** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
49678** take it now. Return SQLITE_OK if successful, or an SQLite error
49679** code otherwise.
49680**
49681** If the DMS cannot be locked because this is a readonly_shm=1
49682** connection and no other process already holds a lock, return
49683** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
49684*/
49685static int winLockSharedMemory(winShmNode *pShmNode){
49686 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
49687
49688 if( rc==SQLITE_OK ){
49689 if( pShmNode->isReadonly ){
49690 pShmNode->isUnlocked = 1;
49691 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
49692 return SQLITE_READONLY_CANTINIT;
49693 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
49694 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
49695 return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
49696 "winLockSharedMemory", pShmNode->zFilename);
49697 }
49698 }
49699
49700 if( rc==SQLITE_OK ){
49701 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
49702 }
49703
49704 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
49705}
49706
49707/*
49708** Open the shared-memory area associated with database file pDbFd.
49709**
49710** When opening a new shared-memory file, if no other instances of that
49711** file are currently open, in this process or in other processes, then
49712** the file must be truncated to zero length or have its header cleared.
49713*/
49714static int winOpenSharedMemory(winFile *pDbFd){
49715 struct winShm *p; /* The connection to be opened */
49716 winShmNode *pShmNode = 0; /* The underlying mmapped file */
49717 int rc = SQLITE_OK; /* Result code */
49718 winShmNode *pNew; /* Newly allocated winShmNode */
49719 int nName; /* Size of zName in bytes */
49720
49721 assert( pDbFd->pShm==0 ); /* Not previously opened */
49722
49723 /* Allocate space for the new sqlite3_shm object. Also speculatively
49724 ** allocate space for a new winShmNode and filename.
49725 */
49726 p = sqlite3MallocZero( sizeof(*p) );
49727 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
49728 nName = sqlite3Strlen30(pDbFd->zPath);
49729 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
49730 if( pNew==0 ){
49731 sqlite3_free(p);
49732 return SQLITE_IOERR_NOMEM_BKPT;
49733 }
49734 pNew->zFilename = (char*)&pNew[1];
49735 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
49736 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
49737
49738 /* Look to see if there is an existing winShmNode that can be used.
49739 ** If no matching winShmNode currently exists, create a new one.
49740 */
49741 winShmEnterMutex();
49742 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
49743 /* TBD need to come up with better match here. Perhaps
49744 ** use FILE_ID_BOTH_DIR_INFO Structure.
49745 */
49746 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
49747 }
49748 if( pShmNode ){
49749 sqlite3_free(pNew);
49750 }else{
49751 int inFlags = SQLITE_OPEN_WAL;
49752 int outFlags = 0;
49753
49754 pShmNode = pNew;
49755 pNew = 0;
49756 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
49757 pShmNode->pNext = winShmNodeList;
49758 winShmNodeList = pShmNode;
49759
49760 if( sqlite3GlobalConfig.bCoreMutex ){
49761 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
49762 if( pShmNode->mutex==0 ){
49763 rc = SQLITE_IOERR_NOMEM_BKPT;
49764 goto shm_open_err;
49765 }
49766 }
49767
49768 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
49769 inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
49770 }else{
49771 inFlags |= SQLITE_OPEN_READONLY;
49772 }
49773 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
49774 (sqlite3_file*)&pShmNode->hFile,
49775 inFlags, &outFlags);
49776 if( rc!=SQLITE_OK ){
49777 rc = winLogError(rc, osGetLastError(), "winOpenShm",
49778 pShmNode->zFilename);
49779 goto shm_open_err;
49780 }
49781 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
49782
49783 rc = winLockSharedMemory(pShmNode);
49784 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
49785 }
49786
49787 /* Make the new connection a child of the winShmNode */
49788 p->pShmNode = pShmNode;
49789#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
49790 p->id = pShmNode->nextShmId++;
49791#endif
49792 pShmNode->nRef++;
49793 pDbFd->pShm = p;
49794 winShmLeaveMutex();
49795
49796 /* The reference count on pShmNode has already been incremented under
49797 ** the cover of the winShmEnterMutex() mutex and the pointer from the
49798 ** new (struct winShm) object to the pShmNode has been set. All that is
49799 ** left to do is to link the new object into the linked list starting
49800 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
49801 ** mutex.
49802 */
49803 sqlite3_mutex_enter(pShmNode->mutex);
49804 p->pNext = pShmNode->pFirst;
49805 pShmNode->pFirst = p;
49806 sqlite3_mutex_leave(pShmNode->mutex);
49807 return rc;
49808
49809 /* Jump here on any error */
49810shm_open_err:
49811 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
49812 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
49813 sqlite3_free(p);
49814 sqlite3_free(pNew);
49815 winShmLeaveMutex();
49816 return rc;
49817}
49818
49819/*
49820** Close a connection to shared-memory. Delete the underlying
49821** storage if deleteFlag is true.
49822*/
49823static int winShmUnmap(
49824 sqlite3_file *fd, /* Database holding shared memory */
49825 int deleteFlag /* Delete after closing if true */
49826){
49827 winFile *pDbFd; /* Database holding shared-memory */
49828 winShm *p; /* The connection to be closed */
49829 winShmNode *pShmNode; /* The underlying shared-memory file */
49830 winShm **pp; /* For looping over sibling connections */
49831
49832 pDbFd = (winFile*)fd;
49833 p = pDbFd->pShm;
49834 if( p==0 ) return SQLITE_OK;
49835 pShmNode = p->pShmNode;
49836
49837 /* Remove connection p from the set of connections associated
49838 ** with pShmNode */
49839 sqlite3_mutex_enter(pShmNode->mutex);
49840 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
49841 *pp = p->pNext;
49842
49843 /* Free the connection p */
49844 sqlite3_free(p);
49845 pDbFd->pShm = 0;
49846 sqlite3_mutex_leave(pShmNode->mutex);
49847
49848 /* If pShmNode->nRef has reached 0, then close the underlying
49849 ** shared-memory file, too */
49850 winShmEnterMutex();
49851 assert( pShmNode->nRef>0 );
49852 pShmNode->nRef--;
49853 if( pShmNode->nRef==0 ){
49854 winShmPurge(pDbFd->pVfs, deleteFlag);
49855 }
49856 winShmLeaveMutex();
49857
49858 return SQLITE_OK;
49859}
49860
49861/*
49862** Change the lock state for a shared-memory segment.
49863*/
49864static int winShmLock(
49865 sqlite3_file *fd, /* Database file holding the shared memory */
49866 int ofst, /* First lock to acquire or release */
49867 int n, /* Number of locks to acquire or release */
49868 int flags /* What to do with the lock */
49869){
49870 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
49871 winShm *p = pDbFd->pShm; /* The shared memory being locked */
49872 winShm *pX; /* For looping over all siblings */
49873 winShmNode *pShmNode;
49874 int rc = SQLITE_OK; /* Result code */
49875 u16 mask; /* Mask of locks to take or release */
49876
49877 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
49878 pShmNode = p->pShmNode;
49879 if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
49880
49881 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
49882 assert( n>=1 );
49883 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
49884 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
49885 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
49886 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
49887 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
49888
49889 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
49890 assert( n>1 || mask==(1<<ofst) );
49891 sqlite3_mutex_enter(pShmNode->mutex);
49892 if( flags & SQLITE_SHM_UNLOCK ){
49893 u16 allMask = 0; /* Mask of locks held by siblings */
49894
49895 /* See if any siblings hold this same lock */
49896 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49897 if( pX==p ) continue;
49898 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
49899 allMask |= pX->sharedMask;
49900 }
49901
49902 /* Unlock the system-level locks */
49903 if( (mask & allMask)==0 ){
49904 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
49905 }else{
49906 rc = SQLITE_OK;
49907 }
49908
49909 /* Undo the local locks */
49910 if( rc==SQLITE_OK ){
49911 p->exclMask &= ~mask;
49912 p->sharedMask &= ~mask;
49913 }
49914 }else if( flags & SQLITE_SHM_SHARED ){
49915 u16 allShared = 0; /* Union of locks held by connections other than "p" */
49916
49917 /* Find out which shared locks are already held by sibling connections.
49918 ** If any sibling already holds an exclusive lock, go ahead and return
49919 ** SQLITE_BUSY.
49920 */
49921 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49922 if( (pX->exclMask & mask)!=0 ){
49923 rc = SQLITE_BUSY;
49924 break;
49925 }
49926 allShared |= pX->sharedMask;
49927 }
49928
49929 /* Get shared locks at the system level, if necessary */
49930 if( rc==SQLITE_OK ){
49931 if( (allShared & mask)==0 ){
49932 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
49933 }else{
49934 rc = SQLITE_OK;
49935 }
49936 }
49937
49938 /* Get the local shared locks */
49939 if( rc==SQLITE_OK ){
49940 p->sharedMask |= mask;
49941 }
49942 }else{
49943 /* Make sure no sibling connections hold locks that will block this
49944 ** lock. If any do, return SQLITE_BUSY right away.
49945 */
49946 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49947 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
49948 rc = SQLITE_BUSY;
49949 break;
49950 }
49951 }
49952
49953 /* Get the exclusive locks at the system level. Then if successful
49954 ** also mark the local connection as being locked.
49955 */
49956 if( rc==SQLITE_OK ){
49957 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
49958 if( rc==SQLITE_OK ){
49959 assert( (p->sharedMask & mask)==0 );
49960 p->exclMask |= mask;
49961 }
49962 }
49963 }
49964 sqlite3_mutex_leave(pShmNode->mutex);
49965 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
49966 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
49967 sqlite3ErrName(rc)));
49968 return rc;
49969}
49970
49971/*
49972** Implement a memory barrier or memory fence on shared memory.
49973**
49974** All loads and stores begun before the barrier must complete before
49975** any load or store begun after the barrier.
49976*/
49977static void winShmBarrier(
49978 sqlite3_file *fd /* Database holding the shared memory */
49979){
49980 UNUSED_PARAMETER(fd);
49981 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
49982 winShmEnterMutex(); /* Also mutex, for redundancy */
49983 winShmLeaveMutex();
49984}
49985
49986/*
49987** This function is called to obtain a pointer to region iRegion of the
49988** shared-memory associated with the database file fd. Shared-memory regions
49989** are numbered starting from zero. Each shared-memory region is szRegion
49990** bytes in size.
49991**
49992** If an error occurs, an error code is returned and *pp is set to NULL.
49993**
49994** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
49995** region has not been allocated (by any client, including one running in a
49996** separate process), then *pp is set to NULL and SQLITE_OK returned. If
49997** isWrite is non-zero and the requested shared-memory region has not yet
49998** been allocated, it is allocated by this function.
49999**
50000** If the shared-memory region has already been allocated or is allocated by
50001** this call as described above, then it is mapped into this processes
50002** address space (if it is not already), *pp is set to point to the mapped
50003** memory and SQLITE_OK returned.
50004*/
50005static int winShmMap(
50006 sqlite3_file *fd, /* Handle open on database file */
50007 int iRegion, /* Region to retrieve */
50008 int szRegion, /* Size of regions */
50009 int isWrite, /* True to extend file if necessary */
50010 void volatile **pp /* OUT: Mapped memory */
50011){
50012 winFile *pDbFd = (winFile*)fd;
50013 winShm *pShm = pDbFd->pShm;
50014 winShmNode *pShmNode;
50015 DWORD protect = PAGE_READWRITE;
50016 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
50017 int rc = SQLITE_OK;
50018
50019 if( !pShm ){
50020 rc = winOpenSharedMemory(pDbFd);
50021 if( rc!=SQLITE_OK ) return rc;
50022 pShm = pDbFd->pShm;
50023 assert( pShm!=0 );
50024 }
50025 pShmNode = pShm->pShmNode;
50026
50027 sqlite3_mutex_enter(pShmNode->mutex);
50028 if( pShmNode->isUnlocked ){
50029 rc = winLockSharedMemory(pShmNode);
50030 if( rc!=SQLITE_OK ) goto shmpage_out;
50031 pShmNode->isUnlocked = 0;
50032 }
50033 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
50034
50035 if( pShmNode->nRegion<=iRegion ){
50036 struct ShmRegion *apNew; /* New aRegion[] array */
50037 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
50038 sqlite3_int64 sz; /* Current size of wal-index file */
50039
50040 pShmNode->szRegion = szRegion;
50041
50042 /* The requested region is not mapped into this processes address space.
50043 ** Check to see if it has been allocated (i.e. if the wal-index file is
50044 ** large enough to contain the requested region).
50045 */
50046 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
50047 if( rc!=SQLITE_OK ){
50048 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
50049 "winShmMap1", pDbFd->zPath);
50050 goto shmpage_out;
50051 }
50052
50053 if( sz<nByte ){
50054 /* The requested memory region does not exist. If isWrite is set to
50055 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
50056 **
50057 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
50058 ** the requested memory region.
50059 */
50060 if( !isWrite ) goto shmpage_out;
50061 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
50062 if( rc!=SQLITE_OK ){
50063 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
50064 "winShmMap2", pDbFd->zPath);
50065 goto shmpage_out;
50066 }
50067 }
50068
50069 /* Map the requested memory region into this processes address space. */
50070 apNew = (struct ShmRegion *)sqlite3_realloc64(
50071 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
50072 );
50073 if( !apNew ){
50074 rc = SQLITE_IOERR_NOMEM_BKPT;
50075 goto shmpage_out;
50076 }
50077 pShmNode->aRegion = apNew;
50078
50079 if( pShmNode->isReadonly ){
50080 protect = PAGE_READONLY;
50081 flags = FILE_MAP_READ;
50082 }
50083
50084 while( pShmNode->nRegion<=iRegion ){
50085 HANDLE hMap = NULL; /* file-mapping handle */
50086 void *pMap = 0; /* Mapped memory region */
50087
50088#if SQLITE_OS_WINRT
50089 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
50090 NULL, protect, nByte, NULL
50091 );
50092#elif defined(SQLITE_WIN32_HAS_WIDE)
50093 hMap = osCreateFileMappingW(pShmNode->hFile.h,
50094 NULL, protect, 0, nByte, NULL
50095 );
50096#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
50097 hMap = osCreateFileMappingA(pShmNode->hFile.h,
50098 NULL, protect, 0, nByte, NULL
50099 );
50100#endif
50101 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
50102 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
50103 hMap ? "ok" : "failed"));
50104 if( hMap ){
50105 int iOffset = pShmNode->nRegion*szRegion;
50106 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
50107#if SQLITE_OS_WINRT
50108 pMap = osMapViewOfFileFromApp(hMap, flags,
50109 iOffset - iOffsetShift, szRegion + iOffsetShift
50110 );
50111#else
50112 pMap = osMapViewOfFile(hMap, flags,
50113 0, iOffset - iOffsetShift, szRegion + iOffsetShift
50114 );
50115#endif
50116 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
50117 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
50118 szRegion, pMap ? "ok" : "failed"));
50119 }
50120 if( !pMap ){
50121 pShmNode->lastErrno = osGetLastError();
50122 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
50123 "winShmMap3", pDbFd->zPath);
50124 if( hMap ) osCloseHandle(hMap);
50125 goto shmpage_out;
50126 }
50127
50128 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
50129 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
50130 pShmNode->nRegion++;
50131 }
50132 }
50133
50134shmpage_out:
50135 if( pShmNode->nRegion>iRegion ){
50136 int iOffset = iRegion*szRegion;
50137 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
50138 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
50139 *pp = (void *)&p[iOffsetShift];
50140 }else{
50141 *pp = 0;
50142 }
50143 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
50144 sqlite3_mutex_leave(pShmNode->mutex);
50145 return rc;
50146}
50147
50148#else
50149# define winShmMap 0
50150# define winShmLock 0
50151# define winShmBarrier 0
50152# define winShmUnmap 0
50153#endif /* #ifndef SQLITE_OMIT_WAL */
50154
50155/*
50156** Cleans up the mapped region of the specified file, if any.
50157*/
50158#if SQLITE_MAX_MMAP_SIZE>0
50159static int winUnmapfile(winFile *pFile){
50160 assert( pFile!=0 );
50161 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
50162 "mmapSize=%lld, mmapSizeMax=%lld\n",
50163 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
50164 pFile->mmapSize, pFile->mmapSizeMax));
50165 if( pFile->pMapRegion ){
50166 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
50167 pFile->lastErrno = osGetLastError();
50168 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
50169 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
50170 pFile->pMapRegion));
50171 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
50172 "winUnmapfile1", pFile->zPath);
50173 }
50174 pFile->pMapRegion = 0;
50175 pFile->mmapSize = 0;
50176 }
50177 if( pFile->hMap!=NULL ){
50178 if( !osCloseHandle(pFile->hMap) ){
50179 pFile->lastErrno = osGetLastError();
50180 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
50181 osGetCurrentProcessId(), pFile, pFile->hMap));
50182 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
50183 "winUnmapfile2", pFile->zPath);
50184 }
50185 pFile->hMap = NULL;
50186 }
50187 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
50188 osGetCurrentProcessId(), pFile));
50189 return SQLITE_OK;
50190}
50191
50192/*
50193** Memory map or remap the file opened by file-descriptor pFd (if the file
50194** is already mapped, the existing mapping is replaced by the new). Or, if
50195** there already exists a mapping for this file, and there are still
50196** outstanding xFetch() references to it, this function is a no-op.
50197**
50198** If parameter nByte is non-negative, then it is the requested size of
50199** the mapping to create. Otherwise, if nByte is less than zero, then the
50200** requested size is the size of the file on disk. The actual size of the
50201** created mapping is either the requested size or the value configured
50202** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
50203**
50204** SQLITE_OK is returned if no error occurs (even if the mapping is not
50205** recreated as a result of outstanding references) or an SQLite error
50206** code otherwise.
50207*/
50208static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
50209 sqlite3_int64 nMap = nByte;
50210 int rc;
50211
50212 assert( nMap>=0 || pFd->nFetchOut==0 );
50213 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
50214 osGetCurrentProcessId(), pFd, nByte));
50215
50216 if( pFd->nFetchOut>0 ) return SQLITE_OK;
50217
50218 if( nMap<0 ){
50219 rc = winFileSize((sqlite3_file*)pFd, &nMap);
50220 if( rc ){
50221 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
50222 osGetCurrentProcessId(), pFd));
50223 return SQLITE_IOERR_FSTAT;
50224 }
50225 }
50226 if( nMap>pFd->mmapSizeMax ){
50227 nMap = pFd->mmapSizeMax;
50228 }
50229 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
50230
50231 if( nMap==0 && pFd->mmapSize>0 ){
50232 winUnmapfile(pFd);
50233 }
50234 if( nMap!=pFd->mmapSize ){
50235 void *pNew = 0;
50236 DWORD protect = PAGE_READONLY;
50237 DWORD flags = FILE_MAP_READ;
50238
50239 winUnmapfile(pFd);
50240#ifdef SQLITE_MMAP_READWRITE
50241 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
50242 protect = PAGE_READWRITE;
50243 flags |= FILE_MAP_WRITE;
50244 }
50245#endif
50246#if SQLITE_OS_WINRT
50247 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
50248#elif defined(SQLITE_WIN32_HAS_WIDE)
50249 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
50250 (DWORD)((nMap>>32) & 0xffffffff),
50251 (DWORD)(nMap & 0xffffffff), NULL);
50252#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
50253 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
50254 (DWORD)((nMap>>32) & 0xffffffff),
50255 (DWORD)(nMap & 0xffffffff), NULL);
50256#endif
50257 if( pFd->hMap==NULL ){
50258 pFd->lastErrno = osGetLastError();
50259 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
50260 "winMapfile1", pFd->zPath);
50261 /* Log the error, but continue normal operation using xRead/xWrite */
50262 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
50263 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
50264 return SQLITE_OK;
50265 }
50266 assert( (nMap % winSysInfo.dwPageSize)==0 );
50267 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
50268#if SQLITE_OS_WINRT
50269 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
50270#else
50271 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
50272#endif
50273 if( pNew==NULL ){
50274 osCloseHandle(pFd->hMap);
50275 pFd->hMap = NULL;
50276 pFd->lastErrno = osGetLastError();
50277 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
50278 "winMapfile2", pFd->zPath);
50279 /* Log the error, but continue normal operation using xRead/xWrite */
50280 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
50281 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
50282 return SQLITE_OK;
50283 }
50284 pFd->pMapRegion = pNew;
50285 pFd->mmapSize = nMap;
50286 }
50287
50288 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
50289 osGetCurrentProcessId(), pFd));
50290 return SQLITE_OK;
50291}
50292#endif /* SQLITE_MAX_MMAP_SIZE>0 */
50293
50294/*
50295** If possible, return a pointer to a mapping of file fd starting at offset
50296** iOff. The mapping must be valid for at least nAmt bytes.
50297**
50298** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
50299** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
50300** Finally, if an error does occur, return an SQLite error code. The final
50301** value of *pp is undefined in this case.
50302**
50303** If this function does return a pointer, the caller must eventually
50304** release the reference by calling winUnfetch().
50305*/
50306static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
50307#if SQLITE_MAX_MMAP_SIZE>0
50308 winFile *pFd = (winFile*)fd; /* The underlying database file */
50309#endif
50310 *pp = 0;
50311
50312 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
50313 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
50314
50315#if SQLITE_MAX_MMAP_SIZE>0
50316 if( pFd->mmapSizeMax>0 ){
50317 if( pFd->pMapRegion==0 ){
50318 int rc = winMapfile(pFd, -1);
50319 if( rc!=SQLITE_OK ){
50320 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
50321 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
50322 return rc;
50323 }
50324 }
50325 if( pFd->mmapSize >= iOff+nAmt ){
50326 assert( pFd->pMapRegion!=0 );
50327 *pp = &((u8 *)pFd->pMapRegion)[iOff];
50328 pFd->nFetchOut++;
50329 }
50330 }
50331#endif
50332
50333 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
50334 osGetCurrentProcessId(), fd, pp, *pp));
50335 return SQLITE_OK;
50336}
50337
50338/*
50339** If the third argument is non-NULL, then this function releases a
50340** reference obtained by an earlier call to winFetch(). The second
50341** argument passed to this function must be the same as the corresponding
50342** argument that was passed to the winFetch() invocation.
50343**
50344** Or, if the third argument is NULL, then this function is being called
50345** to inform the VFS layer that, according to POSIX, any existing mapping
50346** may now be invalid and should be unmapped.
50347*/
50348static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
50349#if SQLITE_MAX_MMAP_SIZE>0
50350 winFile *pFd = (winFile*)fd; /* The underlying database file */
50351
50352 /* If p==0 (unmap the entire file) then there must be no outstanding
50353 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
50354 ** then there must be at least one outstanding. */
50355 assert( (p==0)==(pFd->nFetchOut==0) );
50356
50357 /* If p!=0, it must match the iOff value. */
50358 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
50359
50360 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
50361 osGetCurrentProcessId(), pFd, iOff, p));
50362
50363 if( p ){
50364 pFd->nFetchOut--;
50365 }else{
50366 /* FIXME: If Windows truly always prevents truncating or deleting a
50367 ** file while a mapping is held, then the following winUnmapfile() call
50368 ** is unnecessary can be omitted - potentially improving
50369 ** performance. */
50370 winUnmapfile(pFd);
50371 }
50372
50373 assert( pFd->nFetchOut>=0 );
50374#endif
50375
50376 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
50377 osGetCurrentProcessId(), fd));
50378 return SQLITE_OK;
50379}
50380
50381/*
50382** Here ends the implementation of all sqlite3_file methods.
50383**
50384********************** End sqlite3_file Methods *******************************
50385******************************************************************************/
50386
50387/*
50388** This vector defines all the methods that can operate on an
50389** sqlite3_file for win32.
50390*/
50391static const sqlite3_io_methods winIoMethod = {
50392 3, /* iVersion */
50393 winClose, /* xClose */
50394 winRead, /* xRead */
50395 winWrite, /* xWrite */
50396 winTruncate, /* xTruncate */
50397 winSync, /* xSync */
50398 winFileSize, /* xFileSize */
50399 winLock, /* xLock */
50400 winUnlock, /* xUnlock */
50401 winCheckReservedLock, /* xCheckReservedLock */
50402 winFileControl, /* xFileControl */
50403 winSectorSize, /* xSectorSize */
50404 winDeviceCharacteristics, /* xDeviceCharacteristics */
50405 winShmMap, /* xShmMap */
50406 winShmLock, /* xShmLock */
50407 winShmBarrier, /* xShmBarrier */
50408 winShmUnmap, /* xShmUnmap */
50409 winFetch, /* xFetch */
50410 winUnfetch /* xUnfetch */
50411};
50412
50413/*
50414** This vector defines all the methods that can operate on an
50415** sqlite3_file for win32 without performing any locking.
50416*/
50417static const sqlite3_io_methods winIoNolockMethod = {
50418 3, /* iVersion */
50419 winClose, /* xClose */
50420 winRead, /* xRead */
50421 winWrite, /* xWrite */
50422 winTruncate, /* xTruncate */
50423 winSync, /* xSync */
50424 winFileSize, /* xFileSize */
50425 winNolockLock, /* xLock */
50426 winNolockUnlock, /* xUnlock */
50427 winNolockCheckReservedLock, /* xCheckReservedLock */
50428 winFileControl, /* xFileControl */
50429 winSectorSize, /* xSectorSize */
50430 winDeviceCharacteristics, /* xDeviceCharacteristics */
50431 winShmMap, /* xShmMap */
50432 winShmLock, /* xShmLock */
50433 winShmBarrier, /* xShmBarrier */
50434 winShmUnmap, /* xShmUnmap */
50435 winFetch, /* xFetch */
50436 winUnfetch /* xUnfetch */
50437};
50438
50439static winVfsAppData winAppData = {
50440 &winIoMethod, /* pMethod */
50441 0, /* pAppData */
50442 0 /* bNoLock */
50443};
50444
50445static winVfsAppData winNolockAppData = {
50446 &winIoNolockMethod, /* pMethod */
50447 0, /* pAppData */
50448 1 /* bNoLock */
50449};
50450
50451/****************************************************************************
50452**************************** sqlite3_vfs methods ****************************
50453**
50454** This division contains the implementation of methods on the
50455** sqlite3_vfs object.
50456*/
50457
50458#if defined(__CYGWIN__)
50459/*
50460** Convert a filename from whatever the underlying operating system
50461** supports for filenames into UTF-8. Space to hold the result is
50462** obtained from malloc and must be freed by the calling function.
50463*/
50464static char *winConvertToUtf8Filename(const void *zFilename){
50465 char *zConverted = 0;
50466 if( osIsNT() ){
50467 zConverted = winUnicodeToUtf8(zFilename);
50468 }
50469#ifdef SQLITE_WIN32_HAS_ANSI
50470 else{
50471 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
50472 }
50473#endif
50474 /* caller will handle out of memory */
50475 return zConverted;
50476}
50477#endif
50478
50479/*
50480** Convert a UTF-8 filename into whatever form the underlying
50481** operating system wants filenames in. Space to hold the result
50482** is obtained from malloc and must be freed by the calling
50483** function.
50484*/
50485static void *winConvertFromUtf8Filename(const char *zFilename){
50486 void *zConverted = 0;
50487 if( osIsNT() ){
50488 zConverted = winUtf8ToUnicode(zFilename);
50489 }
50490#ifdef SQLITE_WIN32_HAS_ANSI
50491 else{
50492 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
50493 }
50494#endif
50495 /* caller will handle out of memory */
50496 return zConverted;
50497}
50498
50499/*
50500** This function returns non-zero if the specified UTF-8 string buffer
50501** ends with a directory separator character or one was successfully
50502** added to it.
50503*/
50504static int winMakeEndInDirSep(int nBuf, char *zBuf){
50505 if( zBuf ){
50506 int nLen = sqlite3Strlen30(zBuf);
50507 if( nLen>0 ){
50508 if( winIsDirSep(zBuf[nLen-1]) ){
50509 return 1;
50510 }else if( nLen+1<nBuf ){
50511 zBuf[nLen] = winGetDirSep();
50512 zBuf[nLen+1] = '\0';
50513 return 1;
50514 }
50515 }
50516 }
50517 return 0;
50518}
50519
50520/*
50521** If sqlite3_temp_directory is defined, take the mutex and return true.
50522**
50523** If sqlite3_temp_directory is NULL (undefined), omit the mutex and
50524** return false.
50525*/
50526static int winTempDirDefined(void){
50527 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
50528 if( sqlite3_temp_directory!=0 ) return 1;
50529 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
50530 return 0;
50531}
50532
50533/*
50534** Create a temporary file name and store the resulting pointer into pzBuf.
50535** The pointer returned in pzBuf must be freed via sqlite3_free().
50536*/
50537static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
50538 static char zChars[] =
50539 "abcdefghijklmnopqrstuvwxyz"
50540 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
50541 "0123456789";
50542 size_t i, j;
50543 DWORD pid;
50544 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
50545 int nMax, nBuf, nDir, nLen;
50546 char *zBuf;
50547
50548 /* It's odd to simulate an io-error here, but really this is just
50549 ** using the io-error infrastructure to test that SQLite handles this
50550 ** function failing.
50551 */
50552 SimulateIOError( return SQLITE_IOERR );
50553
50554 /* Allocate a temporary buffer to store the fully qualified file
50555 ** name for the temporary file. If this fails, we cannot continue.
50556 */
50557 nMax = pVfs->mxPathname; nBuf = nMax + 2;
50558 zBuf = sqlite3MallocZero( nBuf );
50559 if( !zBuf ){
50560 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50561 return SQLITE_IOERR_NOMEM_BKPT;
50562 }
50563
50564 /* Figure out the effective temporary directory. First, check if one
50565 ** has been explicitly set by the application; otherwise, use the one
50566 ** configured by the operating system.
50567 */
50568 nDir = nMax - (nPre + 15);
50569 assert( nDir>0 );
50570 if( winTempDirDefined() ){
50571 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
50572 if( nDirLen>0 ){
50573 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
50574 nDirLen++;
50575 }
50576 if( nDirLen>nDir ){
50577 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
50578 sqlite3_free(zBuf);
50579 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50580 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
50581 }
50582 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
50583 }
50584 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
50585 }
50586
50587#if defined(__CYGWIN__)
50588 else{
50589 static const char *azDirs[] = {
50590 0, /* getenv("SQLITE_TMPDIR") */
50591 0, /* getenv("TMPDIR") */
50592 0, /* getenv("TMP") */
50593 0, /* getenv("TEMP") */
50594 0, /* getenv("USERPROFILE") */
50595 "/var/tmp",
50596 "/usr/tmp",
50597 "/tmp",
50598 ".",
50599 0 /* List terminator */
50600 };
50601 unsigned int i;
50602 const char *zDir = 0;
50603
50604 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
50605 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
50606 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
50607 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
50608 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
50609 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
50610 void *zConverted;
50611 if( zDir==0 ) continue;
50612 /* If the path starts with a drive letter followed by the colon
50613 ** character, assume it is already a native Win32 path; otherwise,
50614 ** it must be converted to a native Win32 path via the Cygwin API
50615 ** prior to using it.
50616 */
50617 if( winIsDriveLetterAndColon(zDir) ){
50618 zConverted = winConvertFromUtf8Filename(zDir);
50619 if( !zConverted ){
50620 sqlite3_free(zBuf);
50621 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50622 return SQLITE_IOERR_NOMEM_BKPT;
50623 }
50624 if( winIsDir(zConverted) ){
50625 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
50626 sqlite3_free(zConverted);
50627 break;
50628 }
50629 sqlite3_free(zConverted);
50630 }else{
50631 zConverted = sqlite3MallocZero( nMax+1 );
50632 if( !zConverted ){
50633 sqlite3_free(zBuf);
50634 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50635 return SQLITE_IOERR_NOMEM_BKPT;
50636 }
50637 if( cygwin_conv_path(
50638 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
50639 zConverted, nMax+1)<0 ){
50640 sqlite3_free(zConverted);
50641 sqlite3_free(zBuf);
50642 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
50643 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
50644 "winGetTempname2", zDir);
50645 }
50646 if( winIsDir(zConverted) ){
50647 /* At this point, we know the candidate directory exists and should
50648 ** be used. However, we may need to convert the string containing
50649 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
50650 */
50651 char *zUtf8 = winConvertToUtf8Filename(zConverted);
50652 if( !zUtf8 ){
50653 sqlite3_free(zConverted);
50654 sqlite3_free(zBuf);
50655 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50656 return SQLITE_IOERR_NOMEM_BKPT;
50657 }
50658 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
50659 sqlite3_free(zUtf8);
50660 sqlite3_free(zConverted);
50661 break;
50662 }
50663 sqlite3_free(zConverted);
50664 }
50665 }
50666 }
50667#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
50668 else if( osIsNT() ){
50669 char *zMulti;
50670 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
50671 if( !zWidePath ){
50672 sqlite3_free(zBuf);
50673 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50674 return SQLITE_IOERR_NOMEM_BKPT;
50675 }
50676 if( osGetTempPathW(nMax, zWidePath)==0 ){
50677 sqlite3_free(zWidePath);
50678 sqlite3_free(zBuf);
50679 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50680 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
50681 "winGetTempname2", 0);
50682 }
50683 zMulti = winUnicodeToUtf8(zWidePath);
50684 if( zMulti ){
50685 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
50686 sqlite3_free(zMulti);
50687 sqlite3_free(zWidePath);
50688 }else{
50689 sqlite3_free(zWidePath);
50690 sqlite3_free(zBuf);
50691 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50692 return SQLITE_IOERR_NOMEM_BKPT;
50693 }
50694 }
50695#ifdef SQLITE_WIN32_HAS_ANSI
50696 else{
50697 char *zUtf8;
50698 char *zMbcsPath = sqlite3MallocZero( nMax );
50699 if( !zMbcsPath ){
50700 sqlite3_free(zBuf);
50701 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50702 return SQLITE_IOERR_NOMEM_BKPT;
50703 }
50704 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
50705 sqlite3_free(zBuf);
50706 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50707 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
50708 "winGetTempname3", 0);
50709 }
50710 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
50711 if( zUtf8 ){
50712 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
50713 sqlite3_free(zUtf8);
50714 }else{
50715 sqlite3_free(zBuf);
50716 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50717 return SQLITE_IOERR_NOMEM_BKPT;
50718 }
50719 }
50720#endif /* SQLITE_WIN32_HAS_ANSI */
50721#endif /* !SQLITE_OS_WINRT */
50722
50723 /*
50724 ** Check to make sure the temporary directory ends with an appropriate
50725 ** separator. If it does not and there is not enough space left to add
50726 ** one, fail.
50727 */
50728 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
50729 sqlite3_free(zBuf);
50730 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50731 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
50732 }
50733
50734 /*
50735 ** Check that the output buffer is large enough for the temporary file
50736 ** name in the following format:
50737 **
50738 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
50739 **
50740 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
50741 ** account for the space used by the 15 character random suffix and the
50742 ** two trailing NUL characters. The final directory separator character
50743 ** has already added if it was not already present.
50744 */
50745 nLen = sqlite3Strlen30(zBuf);
50746 if( (nLen + nPre + 17) > nBuf ){
50747 sqlite3_free(zBuf);
50748 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50749 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
50750 }
50751
50752 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
50753
50754 j = sqlite3Strlen30(zBuf);
50755 sqlite3_randomness(15, &zBuf[j]);
50756 pid = osGetCurrentProcessId();
50757 for(i=0; i<15; i++, j++){
50758 zBuf[j] += pid & 0xff;
50759 pid >>= 8;
50760 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
50761 }
50762 zBuf[j] = 0;
50763 zBuf[j+1] = 0;
50764 *pzBuf = zBuf;
50765
50766 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
50767 return SQLITE_OK;
50768}
50769
50770/*
50771** Return TRUE if the named file is really a directory. Return false if
50772** it is something other than a directory, or if there is any kind of memory
50773** allocation failure.
50774*/
50775static int winIsDir(const void *zConverted){
50776 DWORD attr;
50777 int rc = 0;
50778 DWORD lastErrno;
50779
50780 if( osIsNT() ){
50781 int cnt = 0;
50782 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
50783 memset(&sAttrData, 0, sizeof(sAttrData));
50784 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
50785 GetFileExInfoStandard,
50786 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
50787 if( !rc ){
50788 return 0; /* Invalid name? */
50789 }
50790 attr = sAttrData.dwFileAttributes;
50791#if SQLITE_OS_WINCE==0
50792 }else{
50793 attr = osGetFileAttributesA((char*)zConverted);
50794#endif
50795 }
50796 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
50797}
50798
50799/* forward reference */
50800static int winAccess(
50801 sqlite3_vfs *pVfs, /* Not used on win32 */
50802 const char *zFilename, /* Name of file to check */
50803 int flags, /* Type of test to make on this file */
50804 int *pResOut /* OUT: Result */
50805);
50806
50807/*
50808** Open a file.
50809*/
50810static int winOpen(
50811 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
50812 const char *zName, /* Name of the file (UTF-8) */
50813 sqlite3_file *id, /* Write the SQLite file handle here */
50814 int flags, /* Open mode flags */
50815 int *pOutFlags /* Status return flags */
50816){
50817 HANDLE h;
50818 DWORD lastErrno = 0;
50819 DWORD dwDesiredAccess;
50820 DWORD dwShareMode;
50821 DWORD dwCreationDisposition;
50822 DWORD dwFlagsAndAttributes = 0;
50823#if SQLITE_OS_WINCE
50824 int isTemp = 0;
50825#endif
50826 winVfsAppData *pAppData;
50827 winFile *pFile = (winFile*)id;
50828 void *zConverted; /* Filename in OS encoding */
50829 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
50830 int cnt = 0;
50831
50832 /* If argument zPath is a NULL pointer, this function is required to open
50833 ** a temporary file. Use this buffer to store the file name in.
50834 */
50835 char *zTmpname = 0; /* For temporary filename, if necessary. */
50836
50837 int rc = SQLITE_OK; /* Function Return Code */
50838#if !defined(NDEBUG) || SQLITE_OS_WINCE
50839 int eType = flags&0xFFFFFF00; /* Type of file to open */
50840#endif
50841
50842 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
50843 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
50844 int isCreate = (flags & SQLITE_OPEN_CREATE);
50845 int isReadonly = (flags & SQLITE_OPEN_READONLY);
50846 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
50847
50848#ifndef NDEBUG
50849 int isOpenJournal = (isCreate && (
50850 eType==SQLITE_OPEN_SUPER_JOURNAL
50851 || eType==SQLITE_OPEN_MAIN_JOURNAL
50852 || eType==SQLITE_OPEN_WAL
50853 ));
50854#endif
50855
50856 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
50857 zUtf8Name, id, flags, pOutFlags));
50858
50859 /* Check the following statements are true:
50860 **
50861 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
50862 ** (b) if CREATE is set, then READWRITE must also be set, and
50863 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
50864 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
50865 */
50866 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
50867 assert(isCreate==0 || isReadWrite);
50868 assert(isExclusive==0 || isCreate);
50869 assert(isDelete==0 || isCreate);
50870
50871 /* The main DB, main journal, WAL file and super-journal are never
50872 ** automatically deleted. Nor are they ever temporary files. */
50873 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
50874 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
50875 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
50876 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
50877
50878 /* Assert that the upper layer has set one of the "file-type" flags. */
50879 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
50880 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
50881 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
50882 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
50883 );
50884
50885 assert( pFile!=0 );
50886 memset(pFile, 0, sizeof(winFile));
50887 pFile->h = INVALID_HANDLE_VALUE;
50888
50889#if SQLITE_OS_WINRT
50890 if( !zUtf8Name && !sqlite3_temp_directory ){
50891 sqlite3_log(SQLITE_ERROR,
50892 "sqlite3_temp_directory variable should be set for WinRT");
50893 }
50894#endif
50895
50896 /* If the second argument to this function is NULL, generate a
50897 ** temporary file name to use
50898 */
50899 if( !zUtf8Name ){
50900 assert( isDelete && !isOpenJournal );
50901 rc = winGetTempname(pVfs, &zTmpname);
50902 if( rc!=SQLITE_OK ){
50903 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
50904 return rc;
50905 }
50906 zUtf8Name = zTmpname;
50907 }
50908
50909 /* Database filenames are double-zero terminated if they are not
50910 ** URIs with parameters. Hence, they can always be passed into
50911 ** sqlite3_uri_parameter().
50912 */
50913 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
50914 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
50915
50916 /* Convert the filename to the system encoding. */
50917 zConverted = winConvertFromUtf8Filename(zUtf8Name);
50918 if( zConverted==0 ){
50919 sqlite3_free(zTmpname);
50920 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
50921 return SQLITE_IOERR_NOMEM_BKPT;
50922 }
50923
50924 if( winIsDir(zConverted) ){
50925 sqlite3_free(zConverted);
50926 sqlite3_free(zTmpname);
50927 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
50928 return SQLITE_CANTOPEN_ISDIR;
50929 }
50930
50931 if( isReadWrite ){
50932 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
50933 }else{
50934 dwDesiredAccess = GENERIC_READ;
50935 }
50936
50937 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
50938 ** created. SQLite doesn't use it to indicate "exclusive access"
50939 ** as it is usually understood.
50940 */
50941 if( isExclusive ){
50942 /* Creates a new file, only if it does not already exist. */
50943 /* If the file exists, it fails. */
50944 dwCreationDisposition = CREATE_NEW;
50945 }else if( isCreate ){
50946 /* Open existing file, or create if it doesn't exist */
50947 dwCreationDisposition = OPEN_ALWAYS;
50948 }else{
50949 /* Opens a file, only if it exists. */
50950 dwCreationDisposition = OPEN_EXISTING;
50951 }
50952
50953 if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){
50954 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
50955 }else{
50956 dwShareMode = 0;
50957 }
50958
50959 if( isDelete ){
50960#if SQLITE_OS_WINCE
50961 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
50962 isTemp = 1;
50963#else
50964 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
50965 | FILE_ATTRIBUTE_HIDDEN
50966 | FILE_FLAG_DELETE_ON_CLOSE;
50967#endif
50968 }else{
50969 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
50970 }
50971 /* Reports from the internet are that performance is always
50972 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
50973#if SQLITE_OS_WINCE
50974 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
50975#endif
50976
50977 if( osIsNT() ){
50978#if SQLITE_OS_WINRT
50979 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
50980 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
50981 extendedParameters.dwFileAttributes =
50982 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
50983 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
50984 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
50985 extendedParameters.lpSecurityAttributes = NULL;
50986 extendedParameters.hTemplateFile = NULL;
50987 do{
50988 h = osCreateFile2((LPCWSTR)zConverted,
50989 dwDesiredAccess,
50990 dwShareMode,
50991 dwCreationDisposition,
50992 &extendedParameters);
50993 if( h!=INVALID_HANDLE_VALUE ) break;
50994 if( isReadWrite ){
50995 int rc2, isRO = 0;
50996 sqlite3BeginBenignMalloc();
50997 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ, &isRO);
50998 sqlite3EndBenignMalloc();
50999 if( rc2==SQLITE_OK && isRO ) break;
51000 }
51001 }while( winRetryIoerr(&cnt, &lastErrno) );
51002#else
51003 do{
51004 h = osCreateFileW((LPCWSTR)zConverted,
51005 dwDesiredAccess,
51006 dwShareMode, NULL,
51007 dwCreationDisposition,
51008 dwFlagsAndAttributes,
51009 NULL);
51010 if( h!=INVALID_HANDLE_VALUE ) break;
51011 if( isReadWrite ){
51012 int rc2, isRO = 0;
51013 sqlite3BeginBenignMalloc();
51014 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ, &isRO);
51015 sqlite3EndBenignMalloc();
51016 if( rc2==SQLITE_OK && isRO ) break;
51017 }
51018 }while( winRetryIoerr(&cnt, &lastErrno) );
51019#endif
51020 }
51021#ifdef SQLITE_WIN32_HAS_ANSI
51022 else{
51023 do{
51024 h = osCreateFileA((LPCSTR)zConverted,
51025 dwDesiredAccess,
51026 dwShareMode, NULL,
51027 dwCreationDisposition,
51028 dwFlagsAndAttributes,
51029 NULL);
51030 if( h!=INVALID_HANDLE_VALUE ) break;
51031 if( isReadWrite ){
51032 int rc2, isRO = 0;
51033 sqlite3BeginBenignMalloc();
51034 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ, &isRO);
51035 sqlite3EndBenignMalloc();
51036 if( rc2==SQLITE_OK && isRO ) break;
51037 }
51038 }while( winRetryIoerr(&cnt, &lastErrno) );
51039 }
51040#endif
51041 winLogIoerr(cnt, __LINE__);
51042
51043 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
51044 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
51045
51046 if( h==INVALID_HANDLE_VALUE ){
51047 sqlite3_free(zConverted);
51048 sqlite3_free(zTmpname);
51049 if( isReadWrite && !isExclusive ){
51050 return winOpen(pVfs, zName, id,
51051 ((flags|SQLITE_OPEN_READONLY) &
51052 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
51053 pOutFlags);
51054 }else{
51055 pFile->lastErrno = lastErrno;
51056 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
51057 return SQLITE_CANTOPEN_BKPT;
51058 }
51059 }
51060
51061 if( pOutFlags ){
51062 if( isReadWrite ){
51063 *pOutFlags = SQLITE_OPEN_READWRITE;
51064 }else{
51065 *pOutFlags = SQLITE_OPEN_READONLY;
51066 }
51067 }
51068
51069 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
51070 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
51071 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
51072
51073 pAppData = (winVfsAppData*)pVfs->pAppData;
51074
51075#if SQLITE_OS_WINCE
51076 {
51077 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
51078 && ((pAppData==NULL) || !pAppData->bNoLock)
51079 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
51080 ){
51081 osCloseHandle(h);
51082 sqlite3_free(zConverted);
51083 sqlite3_free(zTmpname);
51084 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
51085 return rc;
51086 }
51087 }
51088 if( isTemp ){
51089 pFile->zDeleteOnClose = zConverted;
51090 }else
51091#endif
51092 {
51093 sqlite3_free(zConverted);
51094 }
51095
51096 sqlite3_free(zTmpname);
51097 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
51098 pFile->pVfs = pVfs;
51099 pFile->h = h;
51100 if( isReadonly ){
51101 pFile->ctrlFlags |= WINFILE_RDONLY;
51102 }
51103 if( (flags & SQLITE_OPEN_MAIN_DB)
51104 && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE)
51105 ){
51106 pFile->ctrlFlags |= WINFILE_PSOW;
51107 }
51108 pFile->lastErrno = NO_ERROR;
51109 pFile->zPath = zName;
51110#if SQLITE_MAX_MMAP_SIZE>0
51111 pFile->hMap = NULL;
51112 pFile->pMapRegion = 0;
51113 pFile->mmapSize = 0;
51114 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
51115#endif
51116
51117 OpenCounter(+1);
51118 return rc;
51119}
51120
51121/*
51122** Delete the named file.
51123**
51124** Note that Windows does not allow a file to be deleted if some other
51125** process has it open. Sometimes a virus scanner or indexing program
51126** will open a journal file shortly after it is created in order to do
51127** whatever it does. While this other process is holding the
51128** file open, we will be unable to delete it. To work around this
51129** problem, we delay 100 milliseconds and try to delete again. Up
51130** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
51131** up and returning an error.
51132*/
51133static int winDelete(
51134 sqlite3_vfs *pVfs, /* Not used on win32 */
51135 const char *zFilename, /* Name of file to delete */
51136 int syncDir /* Not used on win32 */
51137){
51138 int cnt = 0;
51139 int rc;
51140 DWORD attr;
51141 DWORD lastErrno = 0;
51142 void *zConverted;
51143 UNUSED_PARAMETER(pVfs);
51144 UNUSED_PARAMETER(syncDir);
51145
51146 SimulateIOError(return SQLITE_IOERR_DELETE);
51147 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
51148
51149 zConverted = winConvertFromUtf8Filename(zFilename);
51150 if( zConverted==0 ){
51151 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
51152 return SQLITE_IOERR_NOMEM_BKPT;
51153 }
51154 if( osIsNT() ){
51155 do {
51156#if SQLITE_OS_WINRT
51157 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
51158 memset(&sAttrData, 0, sizeof(sAttrData));
51159 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
51160 &sAttrData) ){
51161 attr = sAttrData.dwFileAttributes;
51162 }else{
51163 lastErrno = osGetLastError();
51164 if( lastErrno==ERROR_FILE_NOT_FOUND
51165 || lastErrno==ERROR_PATH_NOT_FOUND ){
51166 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
51167 }else{
51168 rc = SQLITE_ERROR;
51169 }
51170 break;
51171 }
51172#else
51173 attr = osGetFileAttributesW(zConverted);
51174#endif
51175 if ( attr==INVALID_FILE_ATTRIBUTES ){
51176 lastErrno = osGetLastError();
51177 if( lastErrno==ERROR_FILE_NOT_FOUND
51178 || lastErrno==ERROR_PATH_NOT_FOUND ){
51179 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
51180 }else{
51181 rc = SQLITE_ERROR;
51182 }
51183 break;
51184 }
51185 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
51186 rc = SQLITE_ERROR; /* Files only. */
51187 break;
51188 }
51189 if ( osDeleteFileW(zConverted) ){
51190 rc = SQLITE_OK; /* Deleted OK. */
51191 break;
51192 }
51193 if ( !winRetryIoerr(&cnt, &lastErrno) ){
51194 rc = SQLITE_ERROR; /* No more retries. */
51195 break;
51196 }
51197 } while(1);
51198 }
51199#ifdef SQLITE_WIN32_HAS_ANSI
51200 else{
51201 do {
51202 attr = osGetFileAttributesA(zConverted);
51203 if ( attr==INVALID_FILE_ATTRIBUTES ){
51204 lastErrno = osGetLastError();
51205 if( lastErrno==ERROR_FILE_NOT_FOUND
51206 || lastErrno==ERROR_PATH_NOT_FOUND ){
51207 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
51208 }else{
51209 rc = SQLITE_ERROR;
51210 }
51211 break;
51212 }
51213 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
51214 rc = SQLITE_ERROR; /* Files only. */
51215 break;
51216 }
51217 if ( osDeleteFileA(zConverted) ){
51218 rc = SQLITE_OK; /* Deleted OK. */
51219 break;
51220 }
51221 if ( !winRetryIoerr(&cnt, &lastErrno) ){
51222 rc = SQLITE_ERROR; /* No more retries. */
51223 break;
51224 }
51225 } while(1);
51226 }
51227#endif
51228 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
51229 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
51230 }else{
51231 winLogIoerr(cnt, __LINE__);
51232 }
51233 sqlite3_free(zConverted);
51234 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
51235 return rc;
51236}
51237
51238/*
51239** Check the existence and status of a file.
51240*/
51241static int winAccess(
51242 sqlite3_vfs *pVfs, /* Not used on win32 */
51243 const char *zFilename, /* Name of file to check */
51244 int flags, /* Type of test to make on this file */
51245 int *pResOut /* OUT: Result */
51246){
51247 DWORD attr;
51248 int rc = 0;
51249 DWORD lastErrno = 0;
51250 void *zConverted;
51251 UNUSED_PARAMETER(pVfs);
51252
51253 SimulateIOError( return SQLITE_IOERR_ACCESS; );
51254 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
51255 zFilename, flags, pResOut));
51256
51257 if( zFilename==0 ){
51258 *pResOut = 0;
51259 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
51260 zFilename, pResOut, *pResOut));
51261 return SQLITE_OK;
51262 }
51263
51264 zConverted = winConvertFromUtf8Filename(zFilename);
51265 if( zConverted==0 ){
51266 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
51267 return SQLITE_IOERR_NOMEM_BKPT;
51268 }
51269 if( osIsNT() ){
51270 int cnt = 0;
51271 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
51272 memset(&sAttrData, 0, sizeof(sAttrData));
51273 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
51274 GetFileExInfoStandard,
51275 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
51276 if( rc ){
51277 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
51278 ** as if it does not exist.
51279 */
51280 if( flags==SQLITE_ACCESS_EXISTS
51281 && sAttrData.nFileSizeHigh==0
51282 && sAttrData.nFileSizeLow==0 ){
51283 attr = INVALID_FILE_ATTRIBUTES;
51284 }else{
51285 attr = sAttrData.dwFileAttributes;
51286 }
51287 }else{
51288 winLogIoerr(cnt, __LINE__);
51289 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
51290 sqlite3_free(zConverted);
51291 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
51292 zFilename);
51293 }else{
51294 attr = INVALID_FILE_ATTRIBUTES;
51295 }
51296 }
51297 }
51298#ifdef SQLITE_WIN32_HAS_ANSI
51299 else{
51300 attr = osGetFileAttributesA((char*)zConverted);
51301 }
51302#endif
51303 sqlite3_free(zConverted);
51304 switch( flags ){
51305 case SQLITE_ACCESS_READ:
51306 case SQLITE_ACCESS_EXISTS:
51307 rc = attr!=INVALID_FILE_ATTRIBUTES;
51308 break;
51309 case SQLITE_ACCESS_READWRITE:
51310 rc = attr!=INVALID_FILE_ATTRIBUTES &&
51311 (attr & FILE_ATTRIBUTE_READONLY)==0;
51312 break;
51313 default:
51314 assert(!"Invalid flags argument");
51315 }
51316 *pResOut = rc;
51317 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
51318 zFilename, pResOut, *pResOut));
51319 return SQLITE_OK;
51320}
51321
51322/*
51323** Returns non-zero if the specified path name starts with the "long path"
51324** prefix.
51325*/
51326static BOOL winIsLongPathPrefix(
51327 const char *zPathname
51328){
51329 return ( zPathname[0]=='\\' && zPathname[1]=='\\'
51330 && zPathname[2]=='?' && zPathname[3]=='\\' );
51331}
51332
51333/*
51334** Returns non-zero if the specified path name starts with a drive letter
51335** followed by a colon character.
51336*/
51337static BOOL winIsDriveLetterAndColon(
51338 const char *zPathname
51339){
51340 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
51341}
51342
51343/*
51344** Returns non-zero if the specified path name should be used verbatim. If
51345** non-zero is returned from this function, the calling function must simply
51346** use the provided path name verbatim -OR- resolve it into a full path name
51347** using the GetFullPathName Win32 API function (if available).
51348*/
51349static BOOL winIsVerbatimPathname(
51350 const char *zPathname
51351){
51352 /*
51353 ** If the path name starts with a forward slash or a backslash, it is either
51354 ** a legal UNC name, a volume relative path, or an absolute path name in the
51355 ** "Unix" format on Windows. There is no easy way to differentiate between
51356 ** the final two cases; therefore, we return the safer return value of TRUE
51357 ** so that callers of this function will simply use it verbatim.
51358 */
51359 if ( winIsDirSep(zPathname[0]) ){
51360 return TRUE;
51361 }
51362
51363 /*
51364 ** If the path name starts with a letter and a colon it is either a volume
51365 ** relative path or an absolute path. Callers of this function must not
51366 ** attempt to treat it as a relative path name (i.e. they should simply use
51367 ** it verbatim).
51368 */
51369 if ( winIsDriveLetterAndColon(zPathname) ){
51370 return TRUE;
51371 }
51372
51373 /*
51374 ** If we get to this point, the path name should almost certainly be a purely
51375 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
51376 */
51377 return FALSE;
51378}
51379
51380/*
51381** Turn a relative pathname into a full pathname. Write the full
51382** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
51383** bytes in size.
51384*/
51385static int winFullPathnameNoMutex(
51386 sqlite3_vfs *pVfs, /* Pointer to vfs object */
51387 const char *zRelative, /* Possibly relative input path */
51388 int nFull, /* Size of output buffer in bytes */
51389 char *zFull /* Output buffer */
51390){
51391#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
51392 DWORD nByte;
51393 void *zConverted;
51394 char *zOut;
51395#endif
51396
51397 /* If this path name begins with "/X:" or "\\?\", where "X" is any
51398 ** alphabetic character, discard the initial "/" from the pathname.
51399 */
51400 if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
51401 || winIsLongPathPrefix(zRelative+1)) ){
51402 zRelative++;
51403 }
51404
51405#if defined(__CYGWIN__)
51406 SimulateIOError( return SQLITE_ERROR );
51407 UNUSED_PARAMETER(nFull);
51408 assert( nFull>=pVfs->mxPathname );
51409 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
51410 /*
51411 ** NOTE: We are dealing with a relative path name and the data
51412 ** directory has been set. Therefore, use it as the basis
51413 ** for converting the relative path name to an absolute
51414 ** one by prepending the data directory and a slash.
51415 */
51416 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
51417 if( !zOut ){
51418 return SQLITE_IOERR_NOMEM_BKPT;
51419 }
51420 if( cygwin_conv_path(
51421 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
51422 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
51423 sqlite3_free(zOut);
51424 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
51425 "winFullPathname1", zRelative);
51426 }else{
51427 char *zUtf8 = winConvertToUtf8Filename(zOut);
51428 if( !zUtf8 ){
51429 sqlite3_free(zOut);
51430 return SQLITE_IOERR_NOMEM_BKPT;
51431 }
51432 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51433 sqlite3_data_directory, winGetDirSep(), zUtf8);
51434 sqlite3_free(zUtf8);
51435 sqlite3_free(zOut);
51436 }
51437 }else{
51438 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
51439 if( !zOut ){
51440 return SQLITE_IOERR_NOMEM_BKPT;
51441 }
51442 if( cygwin_conv_path(
51443 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
51444 zRelative, zOut, pVfs->mxPathname+1)<0 ){
51445 sqlite3_free(zOut);
51446 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
51447 "winFullPathname2", zRelative);
51448 }else{
51449 char *zUtf8 = winConvertToUtf8Filename(zOut);
51450 if( !zUtf8 ){
51451 sqlite3_free(zOut);
51452 return SQLITE_IOERR_NOMEM_BKPT;
51453 }
51454 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
51455 sqlite3_free(zUtf8);
51456 sqlite3_free(zOut);
51457 }
51458 }
51459 return SQLITE_OK;
51460#endif
51461
51462#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
51463 SimulateIOError( return SQLITE_ERROR );
51464 /* WinCE has no concept of a relative pathname, or so I am told. */
51465 /* WinRT has no way to convert a relative path to an absolute one. */
51466 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
51467 /*
51468 ** NOTE: We are dealing with a relative path name and the data
51469 ** directory has been set. Therefore, use it as the basis
51470 ** for converting the relative path name to an absolute
51471 ** one by prepending the data directory and a backslash.
51472 */
51473 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51474 sqlite3_data_directory, winGetDirSep(), zRelative);
51475 }else{
51476 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
51477 }
51478 return SQLITE_OK;
51479#endif
51480
51481#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
51482 /* It's odd to simulate an io-error here, but really this is just
51483 ** using the io-error infrastructure to test that SQLite handles this
51484 ** function failing. This function could fail if, for example, the
51485 ** current working directory has been unlinked.
51486 */
51487 SimulateIOError( return SQLITE_ERROR );
51488 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
51489 /*
51490 ** NOTE: We are dealing with a relative path name and the data
51491 ** directory has been set. Therefore, use it as the basis
51492 ** for converting the relative path name to an absolute
51493 ** one by prepending the data directory and a backslash.
51494 */
51495 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51496 sqlite3_data_directory, winGetDirSep(), zRelative);
51497 return SQLITE_OK;
51498 }
51499 zConverted = winConvertFromUtf8Filename(zRelative);
51500 if( zConverted==0 ){
51501 return SQLITE_IOERR_NOMEM_BKPT;
51502 }
51503 if( osIsNT() ){
51504 LPWSTR zTemp;
51505 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
51506 if( nByte==0 ){
51507 sqlite3_free(zConverted);
51508 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
51509 "winFullPathname1", zRelative);
51510 }
51511 nByte += 3;
51512 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
51513 if( zTemp==0 ){
51514 sqlite3_free(zConverted);
51515 return SQLITE_IOERR_NOMEM_BKPT;
51516 }
51517 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
51518 if( nByte==0 ){
51519 sqlite3_free(zConverted);
51520 sqlite3_free(zTemp);
51521 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
51522 "winFullPathname2", zRelative);
51523 }
51524 sqlite3_free(zConverted);
51525 zOut = winUnicodeToUtf8(zTemp);
51526 sqlite3_free(zTemp);
51527 }
51528#ifdef SQLITE_WIN32_HAS_ANSI
51529 else{
51530 char *zTemp;
51531 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
51532 if( nByte==0 ){
51533 sqlite3_free(zConverted);
51534 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
51535 "winFullPathname3", zRelative);
51536 }
51537 nByte += 3;
51538 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
51539 if( zTemp==0 ){
51540 sqlite3_free(zConverted);
51541 return SQLITE_IOERR_NOMEM_BKPT;
51542 }
51543 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
51544 if( nByte==0 ){
51545 sqlite3_free(zConverted);
51546 sqlite3_free(zTemp);
51547 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
51548 "winFullPathname4", zRelative);
51549 }
51550 sqlite3_free(zConverted);
51551 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
51552 sqlite3_free(zTemp);
51553 }
51554#endif
51555 if( zOut ){
51556 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
51557 sqlite3_free(zOut);
51558 return SQLITE_OK;
51559 }else{
51560 return SQLITE_IOERR_NOMEM_BKPT;
51561 }
51562#endif
51563}
51564static int winFullPathname(
51565 sqlite3_vfs *pVfs, /* Pointer to vfs object */
51566 const char *zRelative, /* Possibly relative input path */
51567 int nFull, /* Size of output buffer in bytes */
51568 char *zFull /* Output buffer */
51569){
51570 int rc;
51571 MUTEX_LOGIC( sqlite3_mutex *pMutex; )
51572 MUTEX_LOGIC( pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); )
51573 sqlite3_mutex_enter(pMutex);
51574 rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
51575 sqlite3_mutex_leave(pMutex);
51576 return rc;
51577}
51578
51579#ifndef SQLITE_OMIT_LOAD_EXTENSION
51580/*
51581** Interfaces for opening a shared library, finding entry points
51582** within the shared library, and closing the shared library.
51583*/
51584static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
51585 HANDLE h;
51586#if defined(__CYGWIN__)
51587 int nFull = pVfs->mxPathname+1;
51588 char *zFull = sqlite3MallocZero( nFull );
51589 void *zConverted = 0;
51590 if( zFull==0 ){
51591 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
51592 return 0;
51593 }
51594 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
51595 sqlite3_free(zFull);
51596 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
51597 return 0;
51598 }
51599 zConverted = winConvertFromUtf8Filename(zFull);
51600 sqlite3_free(zFull);
51601#else
51602 void *zConverted = winConvertFromUtf8Filename(zFilename);
51603 UNUSED_PARAMETER(pVfs);
51604#endif
51605 if( zConverted==0 ){
51606 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
51607 return 0;
51608 }
51609 if( osIsNT() ){
51610#if SQLITE_OS_WINRT
51611 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
51612#else
51613 h = osLoadLibraryW((LPCWSTR)zConverted);
51614#endif
51615 }
51616#ifdef SQLITE_WIN32_HAS_ANSI
51617 else{
51618 h = osLoadLibraryA((char*)zConverted);
51619 }
51620#endif
51621 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
51622 sqlite3_free(zConverted);
51623 return (void*)h;
51624}
51625static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
51626 UNUSED_PARAMETER(pVfs);
51627 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
51628}
51629static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
51630 FARPROC proc;
51631 UNUSED_PARAMETER(pVfs);
51632 proc = osGetProcAddressA((HANDLE)pH, zSym);
51633 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
51634 (void*)pH, zSym, (void*)proc));
51635 return (void(*)(void))proc;
51636}
51637static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
51638 UNUSED_PARAMETER(pVfs);
51639 osFreeLibrary((HANDLE)pHandle);
51640 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
51641}
51642#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
51643 #define winDlOpen 0
51644 #define winDlError 0
51645 #define winDlSym 0
51646 #define winDlClose 0
51647#endif
51648
51649/* State information for the randomness gatherer. */
51650typedef struct EntropyGatherer EntropyGatherer;
51651struct EntropyGatherer {
51652 unsigned char *a; /* Gather entropy into this buffer */
51653 int na; /* Size of a[] in bytes */
51654 int i; /* XOR next input into a[i] */
51655 int nXor; /* Number of XOR operations done */
51656};
51657
51658#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
51659/* Mix sz bytes of entropy into p. */
51660static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
51661 int j, k;
51662 for(j=0, k=p->i; j<sz; j++){
51663 p->a[k++] ^= x[j];
51664 if( k>=p->na ) k = 0;
51665 }
51666 p->i = k;
51667 p->nXor += sz;
51668}
51669#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
51670
51671/*
51672** Write up to nBuf bytes of randomness into zBuf.
51673*/
51674static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
51675#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
51676 UNUSED_PARAMETER(pVfs);
51677 memset(zBuf, 0, nBuf);
51678 return nBuf;
51679#else
51680 EntropyGatherer e;
51681 UNUSED_PARAMETER(pVfs);
51682 memset(zBuf, 0, nBuf);
51683 e.a = (unsigned char*)zBuf;
51684 e.na = nBuf;
51685 e.nXor = 0;
51686 e.i = 0;
51687 {
51688 SYSTEMTIME x;
51689 osGetSystemTime(&x);
51690 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
51691 }
51692 {
51693 DWORD pid = osGetCurrentProcessId();
51694 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
51695 }
51696#if SQLITE_OS_WINRT
51697 {
51698 ULONGLONG cnt = osGetTickCount64();
51699 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
51700 }
51701#else
51702 {
51703 DWORD cnt = osGetTickCount();
51704 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
51705 }
51706#endif /* SQLITE_OS_WINRT */
51707 {
51708 LARGE_INTEGER i;
51709 osQueryPerformanceCounter(&i);
51710 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
51711 }
51712#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
51713 {
51714 UUID id;
51715 memset(&id, 0, sizeof(UUID));
51716 osUuidCreate(&id);
51717 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
51718 memset(&id, 0, sizeof(UUID));
51719 osUuidCreateSequential(&id);
51720 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
51721 }
51722#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
51723 return e.nXor>nBuf ? nBuf : e.nXor;
51724#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
51725}
51726
51727
51728/*
51729** Sleep for a little while. Return the amount of time slept.
51730*/
51731static int winSleep(sqlite3_vfs *pVfs, int microsec){
51732 sqlite3_win32_sleep((microsec+999)/1000);
51733 UNUSED_PARAMETER(pVfs);
51734 return ((microsec+999)/1000)*1000;
51735}
51736
51737/*
51738** The following variable, if set to a non-zero value, is interpreted as
51739** the number of seconds since 1970 and is used to set the result of
51740** sqlite3OsCurrentTime() during testing.
51741*/
51742#ifdef SQLITE_TEST
51743SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
51744#endif
51745
51746/*
51747** Find the current time (in Universal Coordinated Time). Write into *piNow
51748** the current time and date as a Julian Day number times 86_400_000. In
51749** other words, write into *piNow the number of milliseconds since the Julian
51750** epoch of noon in Greenwich on November 24, 4714 B.C according to the
51751** proleptic Gregorian calendar.
51752**
51753** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
51754** cannot be found.
51755*/
51756static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
51757 /* FILETIME structure is a 64-bit value representing the number of
51758 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
51759 */
51760 FILETIME ft;
51761 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
51762#ifdef SQLITE_TEST
51763 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
51764#endif
51765 /* 2^32 - to avoid use of LL and warnings in gcc */
51766 static const sqlite3_int64 max32BitValue =
51767 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
51768 (sqlite3_int64)294967296;
51769
51770#if SQLITE_OS_WINCE
51771 SYSTEMTIME time;
51772 osGetSystemTime(&time);
51773 /* if SystemTimeToFileTime() fails, it returns zero. */
51774 if (!osSystemTimeToFileTime(&time,&ft)){
51775 return SQLITE_ERROR;
51776 }
51777#else
51778 osGetSystemTimeAsFileTime( &ft );
51779#endif
51780
51781 *piNow = winFiletimeEpoch +
51782 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
51783 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
51784
51785#ifdef SQLITE_TEST
51786 if( sqlite3_current_time ){
51787 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
51788 }
51789#endif
51790 UNUSED_PARAMETER(pVfs);
51791 return SQLITE_OK;
51792}
51793
51794/*
51795** Find the current time (in Universal Coordinated Time). Write the
51796** current time and date as a Julian Day number into *prNow and
51797** return 0. Return 1 if the time and date cannot be found.
51798*/
51799static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
51800 int rc;
51801 sqlite3_int64 i;
51802 rc = winCurrentTimeInt64(pVfs, &i);
51803 if( !rc ){
51804 *prNow = i/86400000.0;
51805 }
51806 return rc;
51807}
51808
51809/*
51810** The idea is that this function works like a combination of
51811** GetLastError() and FormatMessage() on Windows (or errno and
51812** strerror_r() on Unix). After an error is returned by an OS
51813** function, SQLite calls this function with zBuf pointing to
51814** a buffer of nBuf bytes. The OS layer should populate the
51815** buffer with a nul-terminated UTF-8 encoded error message
51816** describing the last IO error to have occurred within the calling
51817** thread.
51818**
51819** If the error message is too large for the supplied buffer,
51820** it should be truncated. The return value of xGetLastError
51821** is zero if the error message fits in the buffer, or non-zero
51822** otherwise (if the message was truncated). If non-zero is returned,
51823** then it is not necessary to include the nul-terminator character
51824** in the output buffer.
51825**
51826** Not supplying an error message will have no adverse effect
51827** on SQLite. It is fine to have an implementation that never
51828** returns an error message:
51829**
51830** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
51831** assert(zBuf[0]=='\0');
51832** return 0;
51833** }
51834**
51835** However if an error message is supplied, it will be incorporated
51836** by sqlite into the error message available to the user using
51837** sqlite3_errmsg(), possibly making IO errors easier to debug.
51838*/
51839static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
51840 DWORD e = osGetLastError();
51841 UNUSED_PARAMETER(pVfs);
51842 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
51843 return e;
51844}
51845
51846/*
51847** Initialize and deinitialize the operating system interface.
51848*/
51849SQLITE_API int sqlite3_os_init(void){
51850 static sqlite3_vfs winVfs = {
51851 3, /* iVersion */
51852 sizeof(winFile), /* szOsFile */
51853 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
51854 0, /* pNext */
51855 "win32", /* zName */
51856 &winAppData, /* pAppData */
51857 winOpen, /* xOpen */
51858 winDelete, /* xDelete */
51859 winAccess, /* xAccess */
51860 winFullPathname, /* xFullPathname */
51861 winDlOpen, /* xDlOpen */
51862 winDlError, /* xDlError */
51863 winDlSym, /* xDlSym */
51864 winDlClose, /* xDlClose */
51865 winRandomness, /* xRandomness */
51866 winSleep, /* xSleep */
51867 winCurrentTime, /* xCurrentTime */
51868 winGetLastError, /* xGetLastError */
51869 winCurrentTimeInt64, /* xCurrentTimeInt64 */
51870 winSetSystemCall, /* xSetSystemCall */
51871 winGetSystemCall, /* xGetSystemCall */
51872 winNextSystemCall, /* xNextSystemCall */
51873 };
51874#if defined(SQLITE_WIN32_HAS_WIDE)
51875 static sqlite3_vfs winLongPathVfs = {
51876 3, /* iVersion */
51877 sizeof(winFile), /* szOsFile */
51878 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
51879 0, /* pNext */
51880 "win32-longpath", /* zName */
51881 &winAppData, /* pAppData */
51882 winOpen, /* xOpen */
51883 winDelete, /* xDelete */
51884 winAccess, /* xAccess */
51885 winFullPathname, /* xFullPathname */
51886 winDlOpen, /* xDlOpen */
51887 winDlError, /* xDlError */
51888 winDlSym, /* xDlSym */
51889 winDlClose, /* xDlClose */
51890 winRandomness, /* xRandomness */
51891 winSleep, /* xSleep */
51892 winCurrentTime, /* xCurrentTime */
51893 winGetLastError, /* xGetLastError */
51894 winCurrentTimeInt64, /* xCurrentTimeInt64 */
51895 winSetSystemCall, /* xSetSystemCall */
51896 winGetSystemCall, /* xGetSystemCall */
51897 winNextSystemCall, /* xNextSystemCall */
51898 };
51899#endif
51900 static sqlite3_vfs winNolockVfs = {
51901 3, /* iVersion */
51902 sizeof(winFile), /* szOsFile */
51903 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
51904 0, /* pNext */
51905 "win32-none", /* zName */
51906 &winNolockAppData, /* pAppData */
51907 winOpen, /* xOpen */
51908 winDelete, /* xDelete */
51909 winAccess, /* xAccess */
51910 winFullPathname, /* xFullPathname */
51911 winDlOpen, /* xDlOpen */
51912 winDlError, /* xDlError */
51913 winDlSym, /* xDlSym */
51914 winDlClose, /* xDlClose */
51915 winRandomness, /* xRandomness */
51916 winSleep, /* xSleep */
51917 winCurrentTime, /* xCurrentTime */
51918 winGetLastError, /* xGetLastError */
51919 winCurrentTimeInt64, /* xCurrentTimeInt64 */
51920 winSetSystemCall, /* xSetSystemCall */
51921 winGetSystemCall, /* xGetSystemCall */
51922 winNextSystemCall, /* xNextSystemCall */
51923 };
51924#if defined(SQLITE_WIN32_HAS_WIDE)
51925 static sqlite3_vfs winLongPathNolockVfs = {
51926 3, /* iVersion */
51927 sizeof(winFile), /* szOsFile */
51928 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
51929 0, /* pNext */
51930 "win32-longpath-none", /* zName */
51931 &winNolockAppData, /* pAppData */
51932 winOpen, /* xOpen */
51933 winDelete, /* xDelete */
51934 winAccess, /* xAccess */
51935 winFullPathname, /* xFullPathname */
51936 winDlOpen, /* xDlOpen */
51937 winDlError, /* xDlError */
51938 winDlSym, /* xDlSym */
51939 winDlClose, /* xDlClose */
51940 winRandomness, /* xRandomness */
51941 winSleep, /* xSleep */
51942 winCurrentTime, /* xCurrentTime */
51943 winGetLastError, /* xGetLastError */
51944 winCurrentTimeInt64, /* xCurrentTimeInt64 */
51945 winSetSystemCall, /* xSetSystemCall */
51946 winGetSystemCall, /* xGetSystemCall */
51947 winNextSystemCall, /* xNextSystemCall */
51948 };
51949#endif
51950
51951 /* Double-check that the aSyscall[] array has been constructed
51952 ** correctly. See ticket [bb3a86e890c8e96ab] */
51953 assert( ArraySize(aSyscall)==80 );
51954
51955 /* get memory map allocation granularity */
51956 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
51957#if SQLITE_OS_WINRT
51958 osGetNativeSystemInfo(&winSysInfo);
51959#else
51960 osGetSystemInfo(&winSysInfo);
51961#endif
51962 assert( winSysInfo.dwAllocationGranularity>0 );
51963 assert( winSysInfo.dwPageSize>0 );
51964
51965 sqlite3_vfs_register(&winVfs, 1);
51966
51967#if defined(SQLITE_WIN32_HAS_WIDE)
51968 sqlite3_vfs_register(&winLongPathVfs, 0);
51969#endif
51970
51971 sqlite3_vfs_register(&winNolockVfs, 0);
51972
51973#if defined(SQLITE_WIN32_HAS_WIDE)
51974 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
51975#endif
51976
51977#ifndef SQLITE_OMIT_WAL
51978 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
51979#endif
51980
51981 return SQLITE_OK;
51982}
51983
51984SQLITE_API int sqlite3_os_end(void){
51985#if SQLITE_OS_WINRT
51986 if( sleepObj!=NULL ){
51987 osCloseHandle(sleepObj);
51988 sleepObj = NULL;
51989 }
51990#endif
51991
51992#ifndef SQLITE_OMIT_WAL
51993 winBigLock = 0;
51994#endif
51995
51996 return SQLITE_OK;
51997}
51998
51999#endif /* SQLITE_OS_WIN */
52000
52001/************** End of os_win.c **********************************************/
52002/************** Begin file memdb.c *******************************************/
52003/*
52004** 2016-09-07
52005**
52006** The author disclaims copyright to this source code. In place of
52007** a legal notice, here is a blessing:
52008**
52009** May you do good and not evil.
52010** May you find forgiveness for yourself and forgive others.
52011** May you share freely, never taking more than you give.
52012**
52013******************************************************************************
52014**
52015** This file implements an in-memory VFS. A database is held as a contiguous
52016** block of memory.
52017**
52018** This file also implements interface sqlite3_serialize() and
52019** sqlite3_deserialize().
52020*/
52021/* #include "sqliteInt.h" */
52022#ifndef SQLITE_OMIT_DESERIALIZE
52023
52024/*
52025** Forward declaration of objects used by this utility
52026*/
52027typedef struct sqlite3_vfs MemVfs;
52028typedef struct MemFile MemFile;
52029typedef struct MemStore MemStore;
52030
52031/* Access to a lower-level VFS that (might) implement dynamic loading,
52032** access to randomness, etc.
52033*/
52034#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
52035
52036/* Storage for a memdb file.
52037**
52038** An memdb object can be shared or separate. Shared memdb objects can be
52039** used by more than one database connection. Mutexes are used by shared
52040** memdb objects to coordinate access. Separate memdb objects are only
52041** connected to a single database connection and do not require additional
52042** mutexes.
52043**
52044** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created
52045** using "file:/name?vfs=memdb". The first character of the name must be
52046** "/" or else the object will be a separate memdb object. All shared
52047** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order.
52048**
52049** Separate memdb objects are created using a name that does not begin
52050** with "/" or using sqlite3_deserialize().
52051**
52052** Access rules for shared MemStore objects:
52053**
52054** * .zFName is initialized when the object is created and afterwards
52055** is unchanged until the object is destroyed. So it can be accessed
52056** at any time as long as we know the object is not being destroyed,
52057** which means while either the SQLITE_MUTEX_STATIC_VFS1 or
52058** .pMutex is held or the object is not part of memdb_g.apMemStore[].
52059**
52060** * Can .pMutex can only be changed while holding the
52061** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part
52062** of memdb_g.apMemStore[].
52063**
52064** * Other fields can only be changed while holding the .pMutex mutex
52065** or when the .nRef is less than zero and the object is not part of
52066** memdb_g.apMemStore[].
52067**
52068** * The .aData pointer has the added requirement that it can can only
52069** be changed (for resizing) when nMmap is zero.
52070**
52071*/
52072struct MemStore {
52073 sqlite3_int64 sz; /* Size of the file */
52074 sqlite3_int64 szAlloc; /* Space allocated to aData */
52075 sqlite3_int64 szMax; /* Maximum allowed size of the file */
52076 unsigned char *aData; /* content of the file */
52077 sqlite3_mutex *pMutex; /* Used by shared stores only */
52078 int nMmap; /* Number of memory mapped pages */
52079 unsigned mFlags; /* Flags */
52080 int nRdLock; /* Number of readers */
52081 int nWrLock; /* Number of writers. (Always 0 or 1) */
52082 int nRef; /* Number of users of this MemStore */
52083 char *zFName; /* The filename for shared stores */
52084};
52085
52086/* An open file */
52087struct MemFile {
52088 sqlite3_file base; /* IO methods */
52089 MemStore *pStore; /* The storage */
52090 int eLock; /* Most recent lock against this file */
52091};
52092
52093/*
52094** File-scope variables for holding the memdb files that are accessible
52095** to multiple database connections in separate threads.
52096**
52097** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object.
52098*/
52099static struct MemFS {
52100 int nMemStore; /* Number of shared MemStore objects */
52101 MemStore **apMemStore; /* Array of all shared MemStore objects */
52102} memdb_g;
52103
52104/*
52105** Methods for MemFile
52106*/
52107static int memdbClose(sqlite3_file*);
52108static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
52109static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
52110static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
52111static int memdbSync(sqlite3_file*, int flags);
52112static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
52113static int memdbLock(sqlite3_file*, int);
52114static int memdbUnlock(sqlite3_file*, int);
52115/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
52116static int memdbFileControl(sqlite3_file*, int op, void *pArg);
52117/* static int memdbSectorSize(sqlite3_file*); // not used */
52118static int memdbDeviceCharacteristics(sqlite3_file*);
52119static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
52120static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
52121
52122/*
52123** Methods for MemVfs
52124*/
52125static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
52126/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
52127static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
52128static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
52129static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
52130static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
52131static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
52132static void memdbDlClose(sqlite3_vfs*, void*);
52133static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
52134static int memdbSleep(sqlite3_vfs*, int microseconds);
52135/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
52136static int memdbGetLastError(sqlite3_vfs*, int, char *);
52137static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
52138
52139static sqlite3_vfs memdb_vfs = {
52140 2, /* iVersion */
52141 0, /* szOsFile (set when registered) */
52142 1024, /* mxPathname */
52143 0, /* pNext */
52144 "memdb", /* zName */
52145 0, /* pAppData (set when registered) */
52146 memdbOpen, /* xOpen */
52147 0, /* memdbDelete, */ /* xDelete */
52148 memdbAccess, /* xAccess */
52149 memdbFullPathname, /* xFullPathname */
52150 memdbDlOpen, /* xDlOpen */
52151 memdbDlError, /* xDlError */
52152 memdbDlSym, /* xDlSym */
52153 memdbDlClose, /* xDlClose */
52154 memdbRandomness, /* xRandomness */
52155 memdbSleep, /* xSleep */
52156 0, /* memdbCurrentTime, */ /* xCurrentTime */
52157 memdbGetLastError, /* xGetLastError */
52158 memdbCurrentTimeInt64, /* xCurrentTimeInt64 */
52159 0, /* xSetSystemCall */
52160 0, /* xGetSystemCall */
52161 0, /* xNextSystemCall */
52162};
52163
52164static const sqlite3_io_methods memdb_io_methods = {
52165 3, /* iVersion */
52166 memdbClose, /* xClose */
52167 memdbRead, /* xRead */
52168 memdbWrite, /* xWrite */
52169 memdbTruncate, /* xTruncate */
52170 memdbSync, /* xSync */
52171 memdbFileSize, /* xFileSize */
52172 memdbLock, /* xLock */
52173 memdbUnlock, /* xUnlock */
52174 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
52175 memdbFileControl, /* xFileControl */
52176 0, /* memdbSectorSize,*/ /* xSectorSize */
52177 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
52178 0, /* xShmMap */
52179 0, /* xShmLock */
52180 0, /* xShmBarrier */
52181 0, /* xShmUnmap */
52182 memdbFetch, /* xFetch */
52183 memdbUnfetch /* xUnfetch */
52184};
52185
52186/*
52187** Enter/leave the mutex on a MemStore
52188*/
52189#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
52190static void memdbEnter(MemStore *p){
52191 UNUSED_PARAMETER(p);
52192}
52193static void memdbLeave(MemStore *p){
52194 UNUSED_PARAMETER(p);
52195}
52196#else
52197static void memdbEnter(MemStore *p){
52198 sqlite3_mutex_enter(p: p->pMutex);
52199}
52200static void memdbLeave(MemStore *p){
52201 sqlite3_mutex_leave(p: p->pMutex);
52202}
52203#endif
52204
52205
52206
52207/*
52208** Close an memdb-file.
52209** Free the underlying MemStore object when its refcount drops to zero
52210** or less.
52211*/
52212static int memdbClose(sqlite3_file *pFile){
52213 MemStore *p = ((MemFile*)pFile)->pStore;
52214 if( p->zFName ){
52215 int i;
52216#ifndef SQLITE_MUTEX_OMIT
52217 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
52218#endif
52219 sqlite3_mutex_enter(p: pVfsMutex);
52220 for(i=0; ALWAYS(i<memdb_g.nMemStore); i++){
52221 if( memdb_g.apMemStore[i]==p ){
52222 memdbEnter(p);
52223 if( p->nRef==1 ){
52224 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
52225 if( memdb_g.nMemStore==0 ){
52226 sqlite3_free(p: memdb_g.apMemStore);
52227 memdb_g.apMemStore = 0;
52228 }
52229 }
52230 break;
52231 }
52232 }
52233 sqlite3_mutex_leave(p: pVfsMutex);
52234 }else{
52235 memdbEnter(p);
52236 }
52237 p->nRef--;
52238 if( p->nRef<=0 ){
52239 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
52240 sqlite3_free(p: p->aData);
52241 }
52242 memdbLeave(p);
52243 sqlite3_mutex_free(p: p->pMutex);
52244 sqlite3_free(p);
52245 }else{
52246 memdbLeave(p);
52247 }
52248 return SQLITE_OK;
52249}
52250
52251/*
52252** Read data from an memdb-file.
52253*/
52254static int memdbRead(
52255 sqlite3_file *pFile,
52256 void *zBuf,
52257 int iAmt,
52258 sqlite_int64 iOfst
52259){
52260 MemStore *p = ((MemFile*)pFile)->pStore;
52261 memdbEnter(p);
52262 if( iOfst+iAmt>p->sz ){
52263 memset(s: zBuf, c: 0, n: iAmt);
52264 if( iOfst<p->sz ) memcpy(dest: zBuf, src: p->aData+iOfst, n: p->sz - iOfst);
52265 memdbLeave(p);
52266 return SQLITE_IOERR_SHORT_READ;
52267 }
52268 memcpy(dest: zBuf, src: p->aData+iOfst, n: iAmt);
52269 memdbLeave(p);
52270 return SQLITE_OK;
52271}
52272
52273/*
52274** Try to enlarge the memory allocation to hold at least sz bytes
52275*/
52276static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){
52277 unsigned char *pNew;
52278 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
52279 return SQLITE_FULL;
52280 }
52281 if( newSz>p->szMax ){
52282 return SQLITE_FULL;
52283 }
52284 newSz *= 2;
52285 if( newSz>p->szMax ) newSz = p->szMax;
52286 pNew = sqlite3Realloc(pOld: p->aData, nBytes: newSz);
52287 if( pNew==0 ) return SQLITE_IOERR_NOMEM;
52288 p->aData = pNew;
52289 p->szAlloc = newSz;
52290 return SQLITE_OK;
52291}
52292
52293/*
52294** Write data to an memdb-file.
52295*/
52296static int memdbWrite(
52297 sqlite3_file *pFile,
52298 const void *z,
52299 int iAmt,
52300 sqlite_int64 iOfst
52301){
52302 MemStore *p = ((MemFile*)pFile)->pStore;
52303 memdbEnter(p);
52304 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
52305 /* Can't happen: memdbLock() will return SQLITE_READONLY before
52306 ** reaching this point */
52307 memdbLeave(p);
52308 return SQLITE_IOERR_WRITE;
52309 }
52310 if( iOfst+iAmt>p->sz ){
52311 int rc;
52312 if( iOfst+iAmt>p->szAlloc
52313 && (rc = memdbEnlarge(p, newSz: iOfst+iAmt))!=SQLITE_OK
52314 ){
52315 memdbLeave(p);
52316 return rc;
52317 }
52318 if( iOfst>p->sz ) memset(s: p->aData+p->sz, c: 0, n: iOfst-p->sz);
52319 p->sz = iOfst+iAmt;
52320 }
52321 memcpy(dest: p->aData+iOfst, src: z, n: iAmt);
52322 memdbLeave(p);
52323 return SQLITE_OK;
52324}
52325
52326/*
52327** Truncate an memdb-file.
52328**
52329** In rollback mode (which is always the case for memdb, as it does not
52330** support WAL mode) the truncate() method is only used to reduce
52331** the size of a file, never to increase the size.
52332*/
52333static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
52334 MemStore *p = ((MemFile*)pFile)->pStore;
52335 int rc = SQLITE_OK;
52336 memdbEnter(p);
52337 if( size>p->sz ){
52338 /* This can only happen with a corrupt wal mode db */
52339 rc = SQLITE_CORRUPT;
52340 }else{
52341 p->sz = size;
52342 }
52343 memdbLeave(p);
52344 return rc;
52345}
52346
52347/*
52348** Sync an memdb-file.
52349*/
52350static int memdbSync(sqlite3_file *pFile, int flags){
52351 UNUSED_PARAMETER(pFile);
52352 UNUSED_PARAMETER(flags);
52353 return SQLITE_OK;
52354}
52355
52356/*
52357** Return the current file-size of an memdb-file.
52358*/
52359static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
52360 MemStore *p = ((MemFile*)pFile)->pStore;
52361 memdbEnter(p);
52362 *pSize = p->sz;
52363 memdbLeave(p);
52364 return SQLITE_OK;
52365}
52366
52367/*
52368** Lock an memdb-file.
52369*/
52370static int memdbLock(sqlite3_file *pFile, int eLock){
52371 MemFile *pThis = (MemFile*)pFile;
52372 MemStore *p = pThis->pStore;
52373 int rc = SQLITE_OK;
52374 if( eLock<=pThis->eLock ) return SQLITE_OK;
52375 memdbEnter(p);
52376
52377 assert( p->nWrLock==0 || p->nWrLock==1 );
52378 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
52379 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
52380
52381 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
52382 rc = SQLITE_READONLY;
52383 }else{
52384 switch( eLock ){
52385 case SQLITE_LOCK_SHARED: {
52386 assert( pThis->eLock==SQLITE_LOCK_NONE );
52387 if( p->nWrLock>0 ){
52388 rc = SQLITE_BUSY;
52389 }else{
52390 p->nRdLock++;
52391 }
52392 break;
52393 };
52394
52395 case SQLITE_LOCK_RESERVED:
52396 case SQLITE_LOCK_PENDING: {
52397 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
52398 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
52399 if( p->nWrLock>0 ){
52400 rc = SQLITE_BUSY;
52401 }else{
52402 p->nWrLock = 1;
52403 }
52404 }
52405 break;
52406 }
52407
52408 default: {
52409 assert( eLock==SQLITE_LOCK_EXCLUSIVE );
52410 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
52411 if( p->nRdLock>1 ){
52412 rc = SQLITE_BUSY;
52413 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
52414 p->nWrLock = 1;
52415 }
52416 break;
52417 }
52418 }
52419 }
52420 if( rc==SQLITE_OK ) pThis->eLock = eLock;
52421 memdbLeave(p);
52422 return rc;
52423}
52424
52425/*
52426** Unlock an memdb-file.
52427*/
52428static int memdbUnlock(sqlite3_file *pFile, int eLock){
52429 MemFile *pThis = (MemFile*)pFile;
52430 MemStore *p = pThis->pStore;
52431 if( eLock>=pThis->eLock ) return SQLITE_OK;
52432 memdbEnter(p);
52433
52434 assert( eLock==SQLITE_LOCK_SHARED || eLock==SQLITE_LOCK_NONE );
52435 if( eLock==SQLITE_LOCK_SHARED ){
52436 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
52437 p->nWrLock--;
52438 }
52439 }else{
52440 if( pThis->eLock>SQLITE_LOCK_SHARED ){
52441 p->nWrLock--;
52442 }
52443 p->nRdLock--;
52444 }
52445
52446 pThis->eLock = eLock;
52447 memdbLeave(p);
52448 return SQLITE_OK;
52449}
52450
52451#if 0
52452/*
52453** This interface is only used for crash recovery, which does not
52454** occur on an in-memory database.
52455*/
52456static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
52457 *pResOut = 0;
52458 return SQLITE_OK;
52459}
52460#endif
52461
52462
52463/*
52464** File control method. For custom operations on an memdb-file.
52465*/
52466static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
52467 MemStore *p = ((MemFile*)pFile)->pStore;
52468 int rc = SQLITE_NOTFOUND;
52469 memdbEnter(p);
52470 if( op==SQLITE_FCNTL_VFSNAME ){
52471 *(char**)pArg = sqlite3_mprintf(zFormat: "memdb(%p,%lld)", p->aData, p->sz);
52472 rc = SQLITE_OK;
52473 }
52474 if( op==SQLITE_FCNTL_SIZE_LIMIT ){
52475 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
52476 if( iLimit<p->sz ){
52477 if( iLimit<0 ){
52478 iLimit = p->szMax;
52479 }else{
52480 iLimit = p->sz;
52481 }
52482 }
52483 p->szMax = iLimit;
52484 *(sqlite3_int64*)pArg = iLimit;
52485 rc = SQLITE_OK;
52486 }
52487 memdbLeave(p);
52488 return rc;
52489}
52490
52491#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
52492/*
52493** Return the sector-size in bytes for an memdb-file.
52494*/
52495static int memdbSectorSize(sqlite3_file *pFile){
52496 return 1024;
52497}
52498#endif
52499
52500/*
52501** Return the device characteristic flags supported by an memdb-file.
52502*/
52503static int memdbDeviceCharacteristics(sqlite3_file *pFile){
52504 UNUSED_PARAMETER(pFile);
52505 return SQLITE_IOCAP_ATOMIC |
52506 SQLITE_IOCAP_POWERSAFE_OVERWRITE |
52507 SQLITE_IOCAP_SAFE_APPEND |
52508 SQLITE_IOCAP_SEQUENTIAL;
52509}
52510
52511/* Fetch a page of a memory-mapped file */
52512static int memdbFetch(
52513 sqlite3_file *pFile,
52514 sqlite3_int64 iOfst,
52515 int iAmt,
52516 void **pp
52517){
52518 MemStore *p = ((MemFile*)pFile)->pStore;
52519 memdbEnter(p);
52520 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
52521 *pp = 0;
52522 }else{
52523 p->nMmap++;
52524 *pp = (void*)(p->aData + iOfst);
52525 }
52526 memdbLeave(p);
52527 return SQLITE_OK;
52528}
52529
52530/* Release a memory-mapped page */
52531static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
52532 MemStore *p = ((MemFile*)pFile)->pStore;
52533 UNUSED_PARAMETER(iOfst);
52534 UNUSED_PARAMETER(pPage);
52535 memdbEnter(p);
52536 p->nMmap--;
52537 memdbLeave(p);
52538 return SQLITE_OK;
52539}
52540
52541/*
52542** Open an mem file handle.
52543*/
52544static int memdbOpen(
52545 sqlite3_vfs *pVfs,
52546 const char *zName,
52547 sqlite3_file *pFd,
52548 int flags,
52549 int *pOutFlags
52550){
52551 MemFile *pFile = (MemFile*)pFd;
52552 MemStore *p = 0;
52553 int szName;
52554 UNUSED_PARAMETER(pVfs);
52555
52556 memset(s: pFile, c: 0, n: sizeof(*pFile));
52557 szName = sqlite3Strlen30(z: zName);
52558 if( szName>1 && (zName[0]=='/' || zName[0]=='\\') ){
52559 int i;
52560#ifndef SQLITE_MUTEX_OMIT
52561 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
52562#endif
52563 sqlite3_mutex_enter(p: pVfsMutex);
52564 for(i=0; i<memdb_g.nMemStore; i++){
52565 if( strcmp(s1: memdb_g.apMemStore[i]->zFName,s2: zName)==0 ){
52566 p = memdb_g.apMemStore[i];
52567 break;
52568 }
52569 }
52570 if( p==0 ){
52571 MemStore **apNew;
52572 p = sqlite3Malloc( n: sizeof(*p) + szName + 3 );
52573 if( p==0 ){
52574 sqlite3_mutex_leave(p: pVfsMutex);
52575 return SQLITE_NOMEM;
52576 }
52577 apNew = sqlite3Realloc(pOld: memdb_g.apMemStore,
52578 nBytes: sizeof(apNew[0])*(memdb_g.nMemStore+1) );
52579 if( apNew==0 ){
52580 sqlite3_free(p);
52581 sqlite3_mutex_leave(p: pVfsMutex);
52582 return SQLITE_NOMEM;
52583 }
52584 apNew[memdb_g.nMemStore++] = p;
52585 memdb_g.apMemStore = apNew;
52586 memset(s: p, c: 0, n: sizeof(*p));
52587 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
52588 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52589 p->zFName = (char*)&p[1];
52590 memcpy(dest: p->zFName, src: zName, n: szName+1);
52591 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
52592 if( p->pMutex==0 ){
52593 memdb_g.nMemStore--;
52594 sqlite3_free(p);
52595 sqlite3_mutex_leave(p: pVfsMutex);
52596 return SQLITE_NOMEM;
52597 }
52598 p->nRef = 1;
52599 memdbEnter(p);
52600 }else{
52601 memdbEnter(p);
52602 p->nRef++;
52603 }
52604 sqlite3_mutex_leave(p: pVfsMutex);
52605 }else{
52606 p = sqlite3Malloc( n: sizeof(*p) );
52607 if( p==0 ){
52608 return SQLITE_NOMEM;
52609 }
52610 memset(s: p, c: 0, n: sizeof(*p));
52611 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
52612 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52613 }
52614 pFile->pStore = p;
52615 if( pOutFlags!=0 ){
52616 *pOutFlags = flags | SQLITE_OPEN_MEMORY;
52617 }
52618 pFd->pMethods = &memdb_io_methods;
52619 memdbLeave(p);
52620 return SQLITE_OK;
52621}
52622
52623#if 0 /* Only used to delete rollback journals, super-journals, and WAL
52624 ** files, none of which exist in memdb. So this routine is never used */
52625/*
52626** Delete the file located at zPath. If the dirSync argument is true,
52627** ensure the file-system modifications are synced to disk before
52628** returning.
52629*/
52630static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
52631 return SQLITE_IOERR_DELETE;
52632}
52633#endif
52634
52635/*
52636** Test for access permissions. Return true if the requested permission
52637** is available, or false otherwise.
52638**
52639** With memdb, no files ever exist on disk. So always return false.
52640*/
52641static int memdbAccess(
52642 sqlite3_vfs *pVfs,
52643 const char *zPath,
52644 int flags,
52645 int *pResOut
52646){
52647 UNUSED_PARAMETER(pVfs);
52648 UNUSED_PARAMETER(zPath);
52649 UNUSED_PARAMETER(flags);
52650 *pResOut = 0;
52651 return SQLITE_OK;
52652}
52653
52654/*
52655** Populate buffer zOut with the full canonical pathname corresponding
52656** to the pathname in zPath. zOut is guaranteed to point to a buffer
52657** of at least (INST_MAX_PATHNAME+1) bytes.
52658*/
52659static int memdbFullPathname(
52660 sqlite3_vfs *pVfs,
52661 const char *zPath,
52662 int nOut,
52663 char *zOut
52664){
52665 UNUSED_PARAMETER(pVfs);
52666 sqlite3_snprintf(n: nOut, zBuf: zOut, zFormat: "%s", zPath);
52667 return SQLITE_OK;
52668}
52669
52670/*
52671** Open the dynamic library located at zPath and return a handle.
52672*/
52673static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
52674 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
52675}
52676
52677/*
52678** Populate the buffer zErrMsg (size nByte bytes) with a human readable
52679** utf-8 string describing the most recent error encountered associated
52680** with dynamic libraries.
52681*/
52682static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
52683 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
52684}
52685
52686/*
52687** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
52688*/
52689static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
52690 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
52691}
52692
52693/*
52694** Close the dynamic library handle pHandle.
52695*/
52696static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
52697 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
52698}
52699
52700/*
52701** Populate the buffer pointed to by zBufOut with nByte bytes of
52702** random data.
52703*/
52704static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
52705 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
52706}
52707
52708/*
52709** Sleep for nMicro microseconds. Return the number of microseconds
52710** actually slept.
52711*/
52712static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
52713 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
52714}
52715
52716#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
52717/*
52718** Return the current time as a Julian Day number in *pTimeOut.
52719*/
52720static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
52721 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
52722}
52723#endif
52724
52725static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
52726 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
52727}
52728static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
52729 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
52730}
52731
52732/*
52733** Translate a database connection pointer and schema name into a
52734** MemFile pointer.
52735*/
52736static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
52737 MemFile *p = 0;
52738 MemStore *pStore;
52739 int rc = sqlite3_file_control(db, zDbName: zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
52740 if( rc ) return 0;
52741 if( p->base.pMethods!=&memdb_io_methods ) return 0;
52742 pStore = p->pStore;
52743 memdbEnter(p: pStore);
52744 if( pStore->zFName!=0 ) p = 0;
52745 memdbLeave(p: pStore);
52746 return p;
52747}
52748
52749/*
52750** Return the serialization of a database
52751*/
52752SQLITE_API unsigned char *sqlite3_serialize(
52753 sqlite3 *db, /* The database connection */
52754 const char *zSchema, /* Which database within the connection */
52755 sqlite3_int64 *piSize, /* Write size here, if not NULL */
52756 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
52757){
52758 MemFile *p;
52759 int iDb;
52760 Btree *pBt;
52761 sqlite3_int64 sz;
52762 int szPage = 0;
52763 sqlite3_stmt *pStmt = 0;
52764 unsigned char *pOut;
52765 char *zSql;
52766 int rc;
52767
52768#ifdef SQLITE_ENABLE_API_ARMOR
52769 if( !sqlite3SafetyCheckOk(db) ){
52770 (void)SQLITE_MISUSE_BKPT;
52771 return 0;
52772 }
52773#endif
52774
52775 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52776 p = memdbFromDbSchema(db, zSchema);
52777 iDb = sqlite3FindDbName(db, zSchema);
52778 if( piSize ) *piSize = -1;
52779 if( iDb<0 ) return 0;
52780 if( p ){
52781 MemStore *pStore = p->pStore;
52782 assert( pStore->pMutex==0 );
52783 if( piSize ) *piSize = pStore->sz;
52784 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
52785 pOut = pStore->aData;
52786 }else{
52787 pOut = sqlite3_malloc64( n: pStore->sz );
52788 if( pOut ) memcpy(dest: pOut, src: pStore->aData, n: pStore->sz);
52789 }
52790 return pOut;
52791 }
52792 pBt = db->aDb[iDb].pBt;
52793 if( pBt==0 ) return 0;
52794 szPage = sqlite3BtreeGetPageSize(pBt);
52795 zSql = sqlite3_mprintf(zFormat: "PRAGMA \"%w\".page_count", zSchema);
52796 rc = zSql ? sqlite3_prepare_v2(db, zSql, nByte: -1, ppStmt: &pStmt, pzTail: 0) : SQLITE_NOMEM;
52797 sqlite3_free(p: zSql);
52798 if( rc ) return 0;
52799 rc = sqlite3_step(pStmt);
52800 if( rc!=SQLITE_ROW ){
52801 pOut = 0;
52802 }else{
52803 sz = sqlite3_column_int64(pStmt, iCol: 0)*szPage;
52804 if( piSize ) *piSize = sz;
52805 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
52806 pOut = 0;
52807 }else{
52808 pOut = sqlite3_malloc64( n: sz );
52809 if( pOut ){
52810 int nPage = sqlite3_column_int(pStmt, iCol: 0);
52811 Pager *pPager = sqlite3BtreePager(pBt);
52812 int pgno;
52813 for(pgno=1; pgno<=nPage; pgno++){
52814 DbPage *pPage = 0;
52815 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
52816 rc = sqlite3PagerGet(pPager, pgno, ppPage: (DbPage**)&pPage, clrFlag: 0);
52817 if( rc==SQLITE_OK ){
52818 memcpy(dest: pTo, src: sqlite3PagerGetData(pPage), n: szPage);
52819 }else{
52820 memset(s: pTo, c: 0, n: szPage);
52821 }
52822 sqlite3PagerUnref(pPage);
52823 }
52824 }
52825 }
52826 }
52827 sqlite3_finalize(pStmt);
52828 return pOut;
52829}
52830
52831/* Convert zSchema to a MemDB and initialize its content.
52832*/
52833SQLITE_API int sqlite3_deserialize(
52834 sqlite3 *db, /* The database connection */
52835 const char *zSchema, /* Which DB to reopen with the deserialization */
52836 unsigned char *pData, /* The serialized database content */
52837 sqlite3_int64 szDb, /* Number bytes in the deserialization */
52838 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
52839 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
52840){
52841 MemFile *p;
52842 char *zSql;
52843 sqlite3_stmt *pStmt = 0;
52844 int rc;
52845 int iDb;
52846
52847#ifdef SQLITE_ENABLE_API_ARMOR
52848 if( !sqlite3SafetyCheckOk(db) ){
52849 return SQLITE_MISUSE_BKPT;
52850 }
52851 if( szDb<0 ) return SQLITE_MISUSE_BKPT;
52852 if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
52853#endif
52854
52855 sqlite3_mutex_enter(p: db->mutex);
52856 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52857 iDb = sqlite3FindDbName(db, zSchema);
52858 testcase( iDb==1 );
52859 if( iDb<2 && iDb!=0 ){
52860 rc = SQLITE_ERROR;
52861 goto end_deserialize;
52862 }
52863 zSql = sqlite3_mprintf(zFormat: "ATTACH x AS %Q", zSchema);
52864 if( zSql==0 ){
52865 rc = SQLITE_NOMEM;
52866 }else{
52867 rc = sqlite3_prepare_v2(db, zSql, nByte: -1, ppStmt: &pStmt, pzTail: 0);
52868 sqlite3_free(p: zSql);
52869 }
52870 if( rc ) goto end_deserialize;
52871 db->init.iDb = (u8)iDb;
52872 db->init.reopenMemdb = 1;
52873 rc = sqlite3_step(pStmt);
52874 db->init.reopenMemdb = 0;
52875 if( rc!=SQLITE_DONE ){
52876 rc = SQLITE_ERROR;
52877 goto end_deserialize;
52878 }
52879 p = memdbFromDbSchema(db, zSchema);
52880 if( p==0 ){
52881 rc = SQLITE_ERROR;
52882 }else{
52883 MemStore *pStore = p->pStore;
52884 pStore->aData = pData;
52885 pData = 0;
52886 pStore->sz = szDb;
52887 pStore->szAlloc = szBuf;
52888 pStore->szMax = szBuf;
52889 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
52890 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
52891 }
52892 pStore->mFlags = mFlags;
52893 rc = SQLITE_OK;
52894 }
52895
52896end_deserialize:
52897 sqlite3_finalize(pStmt);
52898 if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE)!=0 ){
52899 sqlite3_free(p: pData);
52900 }
52901 sqlite3_mutex_leave(p: db->mutex);
52902 return rc;
52903}
52904
52905/*
52906** Return true if the VFS is the memvfs.
52907*/
52908SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
52909 return pVfs==&memdb_vfs;
52910}
52911
52912/*
52913** This routine is called when the extension is loaded.
52914** Register the new VFS.
52915*/
52916SQLITE_PRIVATE int sqlite3MemdbInit(void){
52917 sqlite3_vfs *pLower = sqlite3_vfs_find(zVfs: 0);
52918 unsigned int sz;
52919 if( NEVER(pLower==0) ) return SQLITE_ERROR;
52920 sz = pLower->szOsFile;
52921 memdb_vfs.pAppData = pLower;
52922 /* The following conditional can only be true when compiled for
52923 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave
52924 ** it in, to be safe, but it is marked as NO_TEST since there
52925 ** is no way to reach it under most builds. */
52926 if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
52927 memdb_vfs.szOsFile = sz;
52928 return sqlite3_vfs_register(pVfs: &memdb_vfs, makeDflt: 0);
52929}
52930#endif /* SQLITE_OMIT_DESERIALIZE */
52931
52932/************** End of memdb.c ***********************************************/
52933/************** Begin file bitvec.c ******************************************/
52934/*
52935** 2008 February 16
52936**
52937** The author disclaims copyright to this source code. In place of
52938** a legal notice, here is a blessing:
52939**
52940** May you do good and not evil.
52941** May you find forgiveness for yourself and forgive others.
52942** May you share freely, never taking more than you give.
52943**
52944*************************************************************************
52945** This file implements an object that represents a fixed-length
52946** bitmap. Bits are numbered starting with 1.
52947**
52948** A bitmap is used to record which pages of a database file have been
52949** journalled during a transaction, or which pages have the "dont-write"
52950** property. Usually only a few pages are meet either condition.
52951** So the bitmap is usually sparse and has low cardinality.
52952** But sometimes (for example when during a DROP of a large table) most
52953** or all of the pages in a database can get journalled. In those cases,
52954** the bitmap becomes dense with high cardinality. The algorithm needs
52955** to handle both cases well.
52956**
52957** The size of the bitmap is fixed when the object is created.
52958**
52959** All bits are clear when the bitmap is created. Individual bits
52960** may be set or cleared one at a time.
52961**
52962** Test operations are about 100 times more common that set operations.
52963** Clear operations are exceedingly rare. There are usually between
52964** 5 and 500 set operations per Bitvec object, though the number of sets can
52965** sometimes grow into tens of thousands or larger. The size of the
52966** Bitvec object is the number of pages in the database file at the
52967** start of a transaction, and is thus usually less than a few thousand,
52968** but can be as large as 2 billion for a really big database.
52969*/
52970/* #include "sqliteInt.h" */
52971
52972/* Size of the Bitvec structure in bytes. */
52973#define BITVEC_SZ 512
52974
52975/* Round the union size down to the nearest pointer boundary, since that's how
52976** it will be aligned within the Bitvec struct. */
52977#define BITVEC_USIZE \
52978 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
52979
52980/* Type of the array "element" for the bitmap representation.
52981** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
52982** Setting this to the "natural word" size of your CPU may improve
52983** performance. */
52984#define BITVEC_TELEM u8
52985/* Size, in bits, of the bitmap element. */
52986#define BITVEC_SZELEM 8
52987/* Number of elements in a bitmap array. */
52988#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
52989/* Number of bits in the bitmap array. */
52990#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
52991
52992/* Number of u32 values in hash table. */
52993#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
52994/* Maximum number of entries in hash table before
52995** sub-dividing and re-hashing. */
52996#define BITVEC_MXHASH (BITVEC_NINT/2)
52997/* Hashing function for the aHash representation.
52998** Empirical testing showed that the *37 multiplier
52999** (an arbitrary prime)in the hash function provided
53000** no fewer collisions than the no-op *1. */
53001#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
53002
53003#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
53004
53005
53006/*
53007** A bitmap is an instance of the following structure.
53008**
53009** This bitmap records the existence of zero or more bits
53010** with values between 1 and iSize, inclusive.
53011**
53012** There are three possible representations of the bitmap.
53013** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
53014** bitmap. The least significant bit is bit 1.
53015**
53016** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
53017** a hash table that will hold up to BITVEC_MXHASH distinct values.
53018**
53019** Otherwise, the value i is redirected into one of BITVEC_NPTR
53020** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
53021** handles up to iDivisor separate values of i. apSub[0] holds
53022** values between 1 and iDivisor. apSub[1] holds values between
53023** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
53024** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
53025** to hold deal with values between 1 and iDivisor.
53026*/
53027struct Bitvec {
53028 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
53029 u32 nSet; /* Number of bits that are set - only valid for aHash
53030 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
53031 ** this would be 125. */
53032 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
53033 /* Should >=0 for apSub element. */
53034 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
53035 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
53036 union {
53037 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
53038 u32 aHash[BITVEC_NINT]; /* Hash table representation */
53039 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
53040 } u;
53041};
53042
53043/*
53044** Create a new bitmap object able to handle bits between 0 and iSize,
53045** inclusive. Return a pointer to the new object. Return NULL if
53046** malloc fails.
53047*/
53048SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
53049 Bitvec *p;
53050 assert( sizeof(*p)==BITVEC_SZ );
53051 p = sqlite3MallocZero( n: sizeof(*p) );
53052 if( p ){
53053 p->iSize = iSize;
53054 }
53055 return p;
53056}
53057
53058/*
53059** Check to see if the i-th bit is set. Return true or false.
53060** If p is NULL (if the bitmap has not been created) or if
53061** i is out of range, then return false.
53062*/
53063SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
53064 assert( p!=0 );
53065 i--;
53066 if( i>=p->iSize ) return 0;
53067 while( p->iDivisor ){
53068 u32 bin = i/p->iDivisor;
53069 i = i%p->iDivisor;
53070 p = p->u.apSub[bin];
53071 if (!p) {
53072 return 0;
53073 }
53074 }
53075 if( p->iSize<=BITVEC_NBIT ){
53076 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
53077 } else{
53078 u32 h = BITVEC_HASH(i++);
53079 while( p->u.aHash[h] ){
53080 if( p->u.aHash[h]==i ) return 1;
53081 h = (h+1) % BITVEC_NINT;
53082 }
53083 return 0;
53084 }
53085}
53086SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
53087 return p!=0 && sqlite3BitvecTestNotNull(p,i);
53088}
53089
53090/*
53091** Set the i-th bit. Return 0 on success and an error code if
53092** anything goes wrong.
53093**
53094** This routine might cause sub-bitmaps to be allocated. Failing
53095** to get the memory needed to hold the sub-bitmap is the only
53096** that can go wrong with an insert, assuming p and i are valid.
53097**
53098** The calling function must ensure that p is a valid Bitvec object
53099** and that the value for "i" is within range of the Bitvec object.
53100** Otherwise the behavior is undefined.
53101*/
53102SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
53103 u32 h;
53104 if( p==0 ) return SQLITE_OK;
53105 assert( i>0 );
53106 assert( i<=p->iSize );
53107 i--;
53108 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
53109 u32 bin = i/p->iDivisor;
53110 i = i%p->iDivisor;
53111 if( p->u.apSub[bin]==0 ){
53112 p->u.apSub[bin] = sqlite3BitvecCreate( iSize: p->iDivisor );
53113 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
53114 }
53115 p = p->u.apSub[bin];
53116 }
53117 if( p->iSize<=BITVEC_NBIT ){
53118 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
53119 return SQLITE_OK;
53120 }
53121 h = BITVEC_HASH(i++);
53122 /* if there wasn't a hash collision, and this doesn't */
53123 /* completely fill the hash, then just add it without */
53124 /* worrying about sub-dividing and re-hashing. */
53125 if( !p->u.aHash[h] ){
53126 if (p->nSet<(BITVEC_NINT-1)) {
53127 goto bitvec_set_end;
53128 } else {
53129 goto bitvec_set_rehash;
53130 }
53131 }
53132 /* there was a collision, check to see if it's already */
53133 /* in hash, if not, try to find a spot for it */
53134 do {
53135 if( p->u.aHash[h]==i ) return SQLITE_OK;
53136 h++;
53137 if( h>=BITVEC_NINT ) h = 0;
53138 } while( p->u.aHash[h] );
53139 /* we didn't find it in the hash. h points to the first */
53140 /* available free spot. check to see if this is going to */
53141 /* make our hash too "full". */
53142bitvec_set_rehash:
53143 if( p->nSet>=BITVEC_MXHASH ){
53144 unsigned int j;
53145 int rc;
53146 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
53147 if( aiValues==0 ){
53148 return SQLITE_NOMEM_BKPT;
53149 }else{
53150 memcpy(dest: aiValues, src: p->u.aHash, n: sizeof(p->u.aHash));
53151 memset(s: p->u.apSub, c: 0, n: sizeof(p->u.apSub));
53152 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
53153 rc = sqlite3BitvecSet(p, i);
53154 for(j=0; j<BITVEC_NINT; j++){
53155 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, i: aiValues[j]);
53156 }
53157 sqlite3StackFree(0, aiValues);
53158 return rc;
53159 }
53160 }
53161bitvec_set_end:
53162 p->nSet++;
53163 p->u.aHash[h] = i;
53164 return SQLITE_OK;
53165}
53166
53167/*
53168** Clear the i-th bit.
53169**
53170** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
53171** that BitvecClear can use to rebuilt its hash table.
53172*/
53173SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
53174 if( p==0 ) return;
53175 assert( i>0 );
53176 i--;
53177 while( p->iDivisor ){
53178 u32 bin = i/p->iDivisor;
53179 i = i%p->iDivisor;
53180 p = p->u.apSub[bin];
53181 if (!p) {
53182 return;
53183 }
53184 }
53185 if( p->iSize<=BITVEC_NBIT ){
53186 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
53187 }else{
53188 unsigned int j;
53189 u32 *aiValues = pBuf;
53190 memcpy(dest: aiValues, src: p->u.aHash, n: sizeof(p->u.aHash));
53191 memset(s: p->u.aHash, c: 0, n: sizeof(p->u.aHash));
53192 p->nSet = 0;
53193 for(j=0; j<BITVEC_NINT; j++){
53194 if( aiValues[j] && aiValues[j]!=(i+1) ){
53195 u32 h = BITVEC_HASH(aiValues[j]-1);
53196 p->nSet++;
53197 while( p->u.aHash[h] ){
53198 h++;
53199 if( h>=BITVEC_NINT ) h = 0;
53200 }
53201 p->u.aHash[h] = aiValues[j];
53202 }
53203 }
53204 }
53205}
53206
53207/*
53208** Destroy a bitmap object. Reclaim all memory used.
53209*/
53210SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
53211 if( p==0 ) return;
53212 if( p->iDivisor ){
53213 unsigned int i;
53214 for(i=0; i<BITVEC_NPTR; i++){
53215 sqlite3BitvecDestroy(p: p->u.apSub[i]);
53216 }
53217 }
53218 sqlite3_free(p);
53219}
53220
53221/*
53222** Return the value of the iSize parameter specified when Bitvec *p
53223** was created.
53224*/
53225SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
53226 return p->iSize;
53227}
53228
53229#ifndef SQLITE_UNTESTABLE
53230/*
53231** Let V[] be an array of unsigned characters sufficient to hold
53232** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
53233** Then the following macros can be used to set, clear, or test
53234** individual bits within V.
53235*/
53236#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
53237#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
53238#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
53239
53240/*
53241** This routine runs an extensive test of the Bitvec code.
53242**
53243** The input is an array of integers that acts as a program
53244** to test the Bitvec. The integers are opcodes followed
53245** by 0, 1, or 3 operands, depending on the opcode. Another
53246** opcode follows immediately after the last operand.
53247**
53248** There are 6 opcodes numbered from 0 through 5. 0 is the
53249** "halt" opcode and causes the test to end.
53250**
53251** 0 Halt and return the number of errors
53252** 1 N S X Set N bits beginning with S and incrementing by X
53253** 2 N S X Clear N bits beginning with S and incrementing by X
53254** 3 N Set N randomly chosen bits
53255** 4 N Clear N randomly chosen bits
53256** 5 N S X Set N bits from S increment X in array only, not in bitvec
53257**
53258** The opcodes 1 through 4 perform set and clear operations are performed
53259** on both a Bitvec object and on a linear array of bits obtained from malloc.
53260** Opcode 5 works on the linear array only, not on the Bitvec.
53261** Opcode 5 is used to deliberately induce a fault in order to
53262** confirm that error detection works.
53263**
53264** At the conclusion of the test the linear array is compared
53265** against the Bitvec object. If there are any differences,
53266** an error is returned. If they are the same, zero is returned.
53267**
53268** If a memory allocation error occurs, return -1.
53269*/
53270SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
53271 Bitvec *pBitvec = 0;
53272 unsigned char *pV = 0;
53273 int rc = -1;
53274 int i, nx, pc, op;
53275 void *pTmpSpace;
53276
53277 /* Allocate the Bitvec to be tested and a linear array of
53278 ** bits to act as the reference */
53279 pBitvec = sqlite3BitvecCreate( iSize: sz );
53280 pV = sqlite3MallocZero( n: (sz+7)/8 + 1 );
53281 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
53282 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
53283
53284 /* NULL pBitvec tests */
53285 sqlite3BitvecSet(p: 0, i: 1);
53286 sqlite3BitvecClear(p: 0, i: 1, pBuf: pTmpSpace);
53287
53288 /* Run the program */
53289 pc = i = 0;
53290 while( (op = aOp[pc])!=0 ){
53291 switch( op ){
53292 case 1:
53293 case 2:
53294 case 5: {
53295 nx = 4;
53296 i = aOp[pc+2] - 1;
53297 aOp[pc+2] += aOp[pc+3];
53298 break;
53299 }
53300 case 3:
53301 case 4:
53302 default: {
53303 nx = 2;
53304 sqlite3_randomness(N: sizeof(i), pBuf: &i);
53305 break;
53306 }
53307 }
53308 if( (--aOp[pc+1]) > 0 ) nx = 0;
53309 pc += nx;
53310 i = (i & 0x7fffffff)%sz;
53311 if( (op & 1)!=0 ){
53312 SETBIT(pV, (i+1));
53313 if( op!=5 ){
53314 if( sqlite3BitvecSet(p: pBitvec, i: i+1) ) goto bitvec_end;
53315 }
53316 }else{
53317 CLEARBIT(pV, (i+1));
53318 sqlite3BitvecClear(p: pBitvec, i: i+1, pBuf: pTmpSpace);
53319 }
53320 }
53321
53322 /* Test to make sure the linear array exactly matches the
53323 ** Bitvec object. Start with the assumption that they do
53324 ** match (rc==0). Change rc to non-zero if a discrepancy
53325 ** is found.
53326 */
53327 rc = sqlite3BitvecTest(p: 0,i: 0) + sqlite3BitvecTest(p: pBitvec, i: sz+1)
53328 + sqlite3BitvecTest(p: pBitvec, i: 0)
53329 + (sqlite3BitvecSize(p: pBitvec) - sz);
53330 for(i=1; i<=sz; i++){
53331 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(p: pBitvec,i) ){
53332 rc = i;
53333 break;
53334 }
53335 }
53336
53337 /* Free allocated structure */
53338bitvec_end:
53339 sqlite3_free(p: pTmpSpace);
53340 sqlite3_free(p: pV);
53341 sqlite3BitvecDestroy(p: pBitvec);
53342 return rc;
53343}
53344#endif /* SQLITE_UNTESTABLE */
53345
53346/************** End of bitvec.c **********************************************/
53347/************** Begin file pcache.c ******************************************/
53348/*
53349** 2008 August 05
53350**
53351** The author disclaims copyright to this source code. In place of
53352** a legal notice, here is a blessing:
53353**
53354** May you do good and not evil.
53355** May you find forgiveness for yourself and forgive others.
53356** May you share freely, never taking more than you give.
53357**
53358*************************************************************************
53359** This file implements that page cache.
53360*/
53361/* #include "sqliteInt.h" */
53362
53363/*
53364** A complete page cache is an instance of this structure. Every
53365** entry in the cache holds a single page of the database file. The
53366** btree layer only operates on the cached copy of the database pages.
53367**
53368** A page cache entry is "clean" if it exactly matches what is currently
53369** on disk. A page is "dirty" if it has been modified and needs to be
53370** persisted to disk.
53371**
53372** pDirty, pDirtyTail, pSynced:
53373** All dirty pages are linked into the doubly linked list using
53374** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
53375** such that p was added to the list more recently than p->pDirtyNext.
53376** PCache.pDirty points to the first (newest) element in the list and
53377** pDirtyTail to the last (oldest).
53378**
53379** The PCache.pSynced variable is used to optimize searching for a dirty
53380** page to eject from the cache mid-transaction. It is better to eject
53381** a page that does not require a journal sync than one that does.
53382** Therefore, pSynced is maintained so that it *almost* always points
53383** to either the oldest page in the pDirty/pDirtyTail list that has a
53384** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
53385** (so that the right page to eject can be found by following pDirtyPrev
53386** pointers).
53387*/
53388struct PCache {
53389 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
53390 PgHdr *pSynced; /* Last synced page in dirty page list */
53391 i64 nRefSum; /* Sum of ref counts over all pages */
53392 int szCache; /* Configured cache size */
53393 int szSpill; /* Size before spilling occurs */
53394 int szPage; /* Size of every page in this cache */
53395 int szExtra; /* Size of extra space for each page */
53396 u8 bPurgeable; /* True if pages are on backing store */
53397 u8 eCreate; /* eCreate value for for xFetch() */
53398 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
53399 void *pStress; /* Argument to xStress */
53400 sqlite3_pcache *pCache; /* Pluggable cache module */
53401};
53402
53403/********************************** Test and Debug Logic **********************/
53404/*
53405** Debug tracing macros. Enable by by changing the "0" to "1" and
53406** recompiling.
53407**
53408** When sqlite3PcacheTrace is 1, single line trace messages are issued.
53409** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
53410** is displayed for many operations, resulting in a lot of output.
53411*/
53412#if defined(SQLITE_DEBUG) && 0
53413 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
53414 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
53415# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
53416 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
53417 PgHdr *pPg;
53418 unsigned char *a;
53419 int j;
53420 if( pLower==0 ){
53421 printf("%3d: NULL\n", i);
53422 }else{
53423 pPg = (PgHdr*)pLower->pExtra;
53424 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
53425 a = (unsigned char *)pLower->pBuf;
53426 for(j=0; j<12; j++) printf("%02x", a[j]);
53427 printf(" ptr %p\n", pPg);
53428 }
53429 }
53430 static void pcacheDump(PCache *pCache){
53431 int N;
53432 int i;
53433 sqlite3_pcache_page *pLower;
53434
53435 if( sqlite3PcacheTrace<2 ) return;
53436 if( pCache->pCache==0 ) return;
53437 N = sqlite3PcachePagecount(pCache);
53438 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
53439 for(i=1; i<=N; i++){
53440 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
53441 pcachePageTrace(i, pLower);
53442 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
53443 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
53444 }
53445 }
53446 }
53447#else
53448# define pcacheTrace(X)
53449# define pcachePageTrace(PGNO, X)
53450# define pcacheDump(X)
53451#endif
53452
53453/*
53454** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
53455** This routine runs inside of assert() statements only.
53456*/
53457#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
53458static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
53459 PgHdr *p;
53460 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53461 if( p==pPg ) return 1;
53462 }
53463 return 0;
53464}
53465static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg){
53466 PgHdr *p;
53467 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53468 if( p==pPg ) return 0;
53469 }
53470 return 1;
53471}
53472#else
53473# define pageOnDirtyList(A,B) 1
53474# define pageNotOnDirtyList(A,B) 1
53475#endif
53476
53477/*
53478** Check invariants on a PgHdr entry. Return true if everything is OK.
53479** Return false if any invariant is violated.
53480**
53481** This routine is for use inside of assert() statements only. For
53482** example:
53483**
53484** assert( sqlite3PcachePageSanity(pPg) );
53485*/
53486#ifdef SQLITE_DEBUG
53487SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
53488 PCache *pCache;
53489 assert( pPg!=0 );
53490 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
53491 pCache = pPg->pCache;
53492 assert( pCache!=0 ); /* Every page has an associated PCache */
53493 if( pPg->flags & PGHDR_CLEAN ){
53494 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
53495 assert( pageNotOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirtylist */
53496 }else{
53497 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
53498 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
53499 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
53500 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
53501 assert( pageOnDirtyList(pCache, pPg) );
53502 }
53503 /* WRITEABLE pages must also be DIRTY */
53504 if( pPg->flags & PGHDR_WRITEABLE ){
53505 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
53506 }
53507 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
53508 ** for example, when using the sqlite3PagerDontWrite() optimization:
53509 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
53510 ** (2) Page X moved to freelist, WRITEABLE is cleared
53511 ** (3) Page X reused, WRITEABLE is set again
53512 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
53513 ** in step 3, and page might be written into the database without first
53514 ** syncing the rollback journal, which might cause corruption on a power
53515 ** loss.
53516 **
53517 ** Another example is when the database page size is smaller than the
53518 ** disk sector size. When any page of a sector is journalled, all pages
53519 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
53520 ** in case they are later modified, since all pages in the same sector
53521 ** must be journalled and synced before any of those pages can be safely
53522 ** written.
53523 */
53524 return 1;
53525}
53526#endif /* SQLITE_DEBUG */
53527
53528
53529/********************************** Linked List Management ********************/
53530
53531/* Allowed values for second argument to pcacheManageDirtyList() */
53532#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
53533#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
53534#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
53535
53536/*
53537** Manage pPage's participation on the dirty list. Bits of the addRemove
53538** argument determines what operation to do. The 0x01 bit means first
53539** remove pPage from the dirty list. The 0x02 means add pPage back to
53540** the dirty list. Doing both moves pPage to the front of the dirty list.
53541*/
53542static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
53543 PCache *p = pPage->pCache;
53544
53545 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
53546 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
53547 pPage->pgno));
53548 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
53549 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
53550 assert( pPage->pDirtyPrev || pPage==p->pDirty );
53551
53552 /* Update the PCache1.pSynced variable if necessary. */
53553 if( p->pSynced==pPage ){
53554 p->pSynced = pPage->pDirtyPrev;
53555 }
53556
53557 if( pPage->pDirtyNext ){
53558 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
53559 }else{
53560 assert( pPage==p->pDirtyTail );
53561 p->pDirtyTail = pPage->pDirtyPrev;
53562 }
53563 if( pPage->pDirtyPrev ){
53564 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
53565 }else{
53566 /* If there are now no dirty pages in the cache, set eCreate to 2.
53567 ** This is an optimization that allows sqlite3PcacheFetch() to skip
53568 ** searching for a dirty page to eject from the cache when it might
53569 ** otherwise have to. */
53570 assert( pPage==p->pDirty );
53571 p->pDirty = pPage->pDirtyNext;
53572 assert( p->bPurgeable || p->eCreate==2 );
53573 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
53574 assert( p->bPurgeable==0 || p->eCreate==1 );
53575 p->eCreate = 2;
53576 }
53577 }
53578 }
53579 if( addRemove & PCACHE_DIRTYLIST_ADD ){
53580 pPage->pDirtyPrev = 0;
53581 pPage->pDirtyNext = p->pDirty;
53582 if( pPage->pDirtyNext ){
53583 assert( pPage->pDirtyNext->pDirtyPrev==0 );
53584 pPage->pDirtyNext->pDirtyPrev = pPage;
53585 }else{
53586 p->pDirtyTail = pPage;
53587 if( p->bPurgeable ){
53588 assert( p->eCreate==2 );
53589 p->eCreate = 1;
53590 }
53591 }
53592 p->pDirty = pPage;
53593
53594 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
53595 ** pSynced to point to it. Checking the NEED_SYNC flag is an
53596 ** optimization, as if pSynced points to a page with the NEED_SYNC
53597 ** flag set sqlite3PcacheFetchStress() searches through all newer
53598 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
53599 if( !p->pSynced
53600 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
53601 ){
53602 p->pSynced = pPage;
53603 }
53604 }
53605 pcacheDump(p);
53606}
53607
53608/*
53609** Wrapper around the pluggable caches xUnpin method. If the cache is
53610** being used for an in-memory database, this function is a no-op.
53611*/
53612static void pcacheUnpin(PgHdr *p){
53613 if( p->pCache->bPurgeable ){
53614 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
53615 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
53616 pcacheDump(p->pCache);
53617 }
53618}
53619
53620/*
53621** Compute the number of pages of cache requested. p->szCache is the
53622** cache size requested by the "PRAGMA cache_size" statement.
53623*/
53624static int numberOfCachePages(PCache *p){
53625 if( p->szCache>=0 ){
53626 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
53627 ** suggested cache size is set to N. */
53628 return p->szCache;
53629 }else{
53630 i64 n;
53631 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
53632 ** number of cache pages is adjusted to be a number of pages that would
53633 ** use approximately abs(N*1024) bytes of memory based on the current
53634 ** page size. */
53635 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
53636 if( n>1000000000 ) n = 1000000000;
53637 return (int)n;
53638 }
53639}
53640
53641/*************************************************** General Interfaces ******
53642**
53643** Initialize and shutdown the page cache subsystem. Neither of these
53644** functions are threadsafe.
53645*/
53646SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
53647 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
53648 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
53649 ** built-in default page cache is used instead of the application defined
53650 ** page cache. */
53651 sqlite3PCacheSetDefault();
53652 assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
53653 }
53654 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
53655}
53656SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
53657 if( sqlite3GlobalConfig.pcache2.xShutdown ){
53658 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
53659 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
53660 }
53661}
53662
53663/*
53664** Return the size in bytes of a PCache object.
53665*/
53666SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
53667
53668/*
53669** Create a new PCache object. Storage space to hold the object
53670** has already been allocated and is passed in as the p pointer.
53671** The caller discovers how much space needs to be allocated by
53672** calling sqlite3PcacheSize().
53673**
53674** szExtra is some extra space allocated for each page. The first
53675** 8 bytes of the extra space will be zeroed as the page is allocated,
53676** but remaining content will be uninitialized. Though it is opaque
53677** to this module, the extra space really ends up being the MemPage
53678** structure in the pager.
53679*/
53680SQLITE_PRIVATE int sqlite3PcacheOpen(
53681 int szPage, /* Size of every page */
53682 int szExtra, /* Extra space associated with each page */
53683 int bPurgeable, /* True if pages are on backing store */
53684 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
53685 void *pStress, /* Argument to xStress */
53686 PCache *p /* Preallocated space for the PCache */
53687){
53688 memset(s: p, c: 0, n: sizeof(PCache));
53689 p->szPage = 1;
53690 p->szExtra = szExtra;
53691 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
53692 p->bPurgeable = bPurgeable;
53693 p->eCreate = 2;
53694 p->xStress = xStress;
53695 p->pStress = pStress;
53696 p->szCache = 100;
53697 p->szSpill = 1;
53698 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
53699 return sqlite3PcacheSetPageSize(p, szPage);
53700}
53701
53702/*
53703** Change the page size for PCache object. The caller must ensure that there
53704** are no outstanding page references when this function is called.
53705*/
53706SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
53707 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
53708 if( pCache->szPage ){
53709 sqlite3_pcache *pNew;
53710 pNew = sqlite3GlobalConfig.pcache2.xCreate(
53711 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
53712 pCache->bPurgeable
53713 );
53714 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
53715 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(p: pCache));
53716 if( pCache->pCache ){
53717 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53718 }
53719 pCache->pCache = pNew;
53720 pCache->szPage = szPage;
53721 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
53722 }
53723 return SQLITE_OK;
53724}
53725
53726/*
53727** Try to obtain a page from the cache.
53728**
53729** This routine returns a pointer to an sqlite3_pcache_page object if
53730** such an object is already in cache, or if a new one is created.
53731** This routine returns a NULL pointer if the object was not in cache
53732** and could not be created.
53733**
53734** The createFlags should be 0 to check for existing pages and should
53735** be 3 (not 1, but 3) to try to create a new page.
53736**
53737** If the createFlag is 0, then NULL is always returned if the page
53738** is not already in the cache. If createFlag is 1, then a new page
53739** is created only if that can be done without spilling dirty pages
53740** and without exceeding the cache size limit.
53741**
53742** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
53743** initialize the sqlite3_pcache_page object and convert it into a
53744** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
53745** routines are split this way for performance reasons. When separated
53746** they can both (usually) operate without having to push values to
53747** the stack on entry and pop them back off on exit, which saves a
53748** lot of pushing and popping.
53749*/
53750SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
53751 PCache *pCache, /* Obtain the page from this cache */
53752 Pgno pgno, /* Page number to obtain */
53753 int createFlag /* If true, create page if it does not exist already */
53754){
53755 int eCreate;
53756 sqlite3_pcache_page *pRes;
53757
53758 assert( pCache!=0 );
53759 assert( pCache->pCache!=0 );
53760 assert( createFlag==3 || createFlag==0 );
53761 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
53762
53763 /* eCreate defines what to do if the page does not exist.
53764 ** 0 Do not allocate a new page. (createFlag==0)
53765 ** 1 Allocate a new page if doing so is inexpensive.
53766 ** (createFlag==1 AND bPurgeable AND pDirty)
53767 ** 2 Allocate a new page even it doing so is difficult.
53768 ** (createFlag==1 AND !(bPurgeable AND pDirty)
53769 */
53770 eCreate = createFlag & pCache->eCreate;
53771 assert( eCreate==0 || eCreate==1 || eCreate==2 );
53772 assert( createFlag==0 || pCache->eCreate==eCreate );
53773 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
53774 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
53775 pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,
53776 createFlag?" create":"",pRes));
53777 pcachePageTrace(pgno, pRes);
53778 return pRes;
53779}
53780
53781/*
53782** If the sqlite3PcacheFetch() routine is unable to allocate a new
53783** page because no clean pages are available for reuse and the cache
53784** size limit has been reached, then this routine can be invoked to
53785** try harder to allocate a page. This routine might invoke the stress
53786** callback to spill dirty pages to the journal. It will then try to
53787** allocate the new page and will only fail to allocate a new page on
53788** an OOM error.
53789**
53790** This routine should be invoked only after sqlite3PcacheFetch() fails.
53791*/
53792SQLITE_PRIVATE int sqlite3PcacheFetchStress(
53793 PCache *pCache, /* Obtain the page from this cache */
53794 Pgno pgno, /* Page number to obtain */
53795 sqlite3_pcache_page **ppPage /* Write result here */
53796){
53797 PgHdr *pPg;
53798 if( pCache->eCreate==2 ) return 0;
53799
53800 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
53801 /* Find a dirty page to write-out and recycle. First try to find a
53802 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
53803 ** cleared), but if that is not possible settle for any other
53804 ** unreferenced dirty page.
53805 **
53806 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
53807 ** flag is currently referenced, then the following may leave pSynced
53808 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
53809 ** cleared). This is Ok, as pSynced is just an optimization. */
53810 for(pPg=pCache->pSynced;
53811 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
53812 pPg=pPg->pDirtyPrev
53813 );
53814 pCache->pSynced = pPg;
53815 if( !pPg ){
53816 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
53817 }
53818 if( pPg ){
53819 int rc;
53820#ifdef SQLITE_LOG_CACHE_SPILL
53821 sqlite3_log(SQLITE_FULL,
53822 "spill page %d making room for %d - cache used: %d/%d",
53823 pPg->pgno, pgno,
53824 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
53825 numberOfCachePages(pCache));
53826#endif
53827 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
53828 rc = pCache->xStress(pCache->pStress, pPg);
53829 pcacheDump(pCache);
53830 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
53831 return rc;
53832 }
53833 }
53834 }
53835 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
53836 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
53837}
53838
53839/*
53840** This is a helper routine for sqlite3PcacheFetchFinish()
53841**
53842** In the uncommon case where the page being fetched has not been
53843** initialized, this routine is invoked to do the initialization.
53844** This routine is broken out into a separate function since it
53845** requires extra stack manipulation that can be avoided in the common
53846** case.
53847*/
53848static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
53849 PCache *pCache, /* Obtain the page from this cache */
53850 Pgno pgno, /* Page number obtained */
53851 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
53852){
53853 PgHdr *pPgHdr;
53854 assert( pPage!=0 );
53855 pPgHdr = (PgHdr*)pPage->pExtra;
53856 assert( pPgHdr->pPage==0 );
53857 memset(s: &pPgHdr->pDirty, c: 0, n: sizeof(PgHdr) - offsetof(PgHdr,pDirty));
53858 pPgHdr->pPage = pPage;
53859 pPgHdr->pData = pPage->pBuf;
53860 pPgHdr->pExtra = (void *)&pPgHdr[1];
53861 memset(s: pPgHdr->pExtra, c: 0, n: 8);
53862 pPgHdr->pCache = pCache;
53863 pPgHdr->pgno = pgno;
53864 pPgHdr->flags = PGHDR_CLEAN;
53865 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
53866}
53867
53868/*
53869** This routine converts the sqlite3_pcache_page object returned by
53870** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
53871** must be called after sqlite3PcacheFetch() in order to get a usable
53872** result.
53873*/
53874SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
53875 PCache *pCache, /* Obtain the page from this cache */
53876 Pgno pgno, /* Page number obtained */
53877 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
53878){
53879 PgHdr *pPgHdr;
53880
53881 assert( pPage!=0 );
53882 pPgHdr = (PgHdr *)pPage->pExtra;
53883
53884 if( !pPgHdr->pPage ){
53885 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
53886 }
53887 pCache->nRefSum++;
53888 pPgHdr->nRef++;
53889 assert( sqlite3PcachePageSanity(pPgHdr) );
53890 return pPgHdr;
53891}
53892
53893/*
53894** Decrement the reference count on a page. If the page is clean and the
53895** reference count drops to 0, then it is made eligible for recycling.
53896*/
53897SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
53898 assert( p->nRef>0 );
53899 p->pCache->nRefSum--;
53900 if( (--p->nRef)==0 ){
53901 if( p->flags&PGHDR_CLEAN ){
53902 pcacheUnpin(p);
53903 }else{
53904 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_FRONT);
53905 assert( sqlite3PcachePageSanity(p) );
53906 }
53907 }
53908}
53909
53910/*
53911** Increase the reference count of a supplied page by 1.
53912*/
53913SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
53914 assert(p->nRef>0);
53915 assert( sqlite3PcachePageSanity(p) );
53916 p->nRef++;
53917 p->pCache->nRefSum++;
53918}
53919
53920/*
53921** Drop a page from the cache. There must be exactly one reference to the
53922** page. This function deletes that reference, so after it returns the
53923** page pointed to by p is invalid.
53924*/
53925SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
53926 assert( p->nRef==1 );
53927 assert( sqlite3PcachePageSanity(p) );
53928 if( p->flags&PGHDR_DIRTY ){
53929 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_REMOVE);
53930 }
53931 p->pCache->nRefSum--;
53932 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
53933}
53934
53935/*
53936** Make sure the page is marked as dirty. If it isn't dirty already,
53937** make it so.
53938*/
53939SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
53940 assert( p->nRef>0 );
53941 assert( sqlite3PcachePageSanity(p) );
53942 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
53943 p->flags &= ~PGHDR_DONT_WRITE;
53944 if( p->flags & PGHDR_CLEAN ){
53945 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
53946 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
53947 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
53948 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_ADD);
53949 assert( sqlite3PcachePageSanity(p) );
53950 }
53951 assert( sqlite3PcachePageSanity(p) );
53952 }
53953}
53954
53955/*
53956** Make sure the page is marked as clean. If it isn't clean already,
53957** make it so.
53958*/
53959SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
53960 assert( sqlite3PcachePageSanity(p) );
53961 assert( (p->flags & PGHDR_DIRTY)!=0 );
53962 assert( (p->flags & PGHDR_CLEAN)==0 );
53963 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_REMOVE);
53964 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53965 p->flags |= PGHDR_CLEAN;
53966 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
53967 assert( sqlite3PcachePageSanity(p) );
53968 if( p->nRef==0 ){
53969 pcacheUnpin(p);
53970 }
53971}
53972
53973/*
53974** Make every page in the cache clean.
53975*/
53976SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
53977 PgHdr *p;
53978 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
53979 while( (p = pCache->pDirty)!=0 ){
53980 sqlite3PcacheMakeClean(p);
53981 }
53982}
53983
53984/*
53985** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
53986*/
53987SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
53988 PgHdr *p;
53989 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
53990 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53991 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53992 }
53993 pCache->pSynced = pCache->pDirtyTail;
53994}
53995
53996/*
53997** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
53998*/
53999SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
54000 PgHdr *p;
54001 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54002 p->flags &= ~PGHDR_NEED_SYNC;
54003 }
54004 pCache->pSynced = pCache->pDirtyTail;
54005}
54006
54007/*
54008** Change the page number of page p to newPgno.
54009*/
54010SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
54011 PCache *pCache = p->pCache;
54012 sqlite3_pcache_page *pOther;
54013 assert( p->nRef>0 );
54014 assert( newPgno>0 );
54015 assert( sqlite3PcachePageSanity(p) );
54016 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
54017 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
54018 if( pOther ){
54019 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
54020 assert( pXPage->nRef==0 );
54021 pXPage->nRef++;
54022 pCache->nRefSum++;
54023 sqlite3PcacheDrop(p: pXPage);
54024 }
54025 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
54026 p->pgno = newPgno;
54027 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
54028 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_FRONT);
54029 assert( sqlite3PcachePageSanity(p) );
54030 }
54031}
54032
54033/*
54034** Drop every cache entry whose page number is greater than "pgno". The
54035** caller must ensure that there are no outstanding references to any pages
54036** other than page 1 with a page number greater than pgno.
54037**
54038** If there is a reference to page 1 and the pgno parameter passed to this
54039** function is 0, then the data area associated with page 1 is zeroed, but
54040** the page object is not dropped.
54041*/
54042SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
54043 if( pCache->pCache ){
54044 PgHdr *p;
54045 PgHdr *pNext;
54046 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
54047 for(p=pCache->pDirty; p; p=pNext){
54048 pNext = p->pDirtyNext;
54049 /* This routine never gets call with a positive pgno except right
54050 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
54051 ** it must be that pgno==0.
54052 */
54053 assert( p->pgno>0 );
54054 if( p->pgno>pgno ){
54055 assert( p->flags&PGHDR_DIRTY );
54056 sqlite3PcacheMakeClean(p);
54057 }
54058 }
54059 if( pgno==0 && pCache->nRefSum ){
54060 sqlite3_pcache_page *pPage1;
54061 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
54062 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
54063 ** pCache->nRefSum>0 */
54064 memset(s: pPage1->pBuf, c: 0, n: pCache->szPage);
54065 pgno = 1;
54066 }
54067 }
54068 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
54069 }
54070}
54071
54072/*
54073** Close a cache.
54074*/
54075SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
54076 assert( pCache->pCache!=0 );
54077 pcacheTrace(("%p.CLOSE\n",pCache));
54078 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
54079}
54080
54081/*
54082** Discard the contents of the cache.
54083*/
54084SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
54085 sqlite3PcacheTruncate(pCache, pgno: 0);
54086}
54087
54088/*
54089** Merge two lists of pages connected by pDirty and in pgno order.
54090** Do not bother fixing the pDirtyPrev pointers.
54091*/
54092static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
54093 PgHdr result, *pTail;
54094 pTail = &result;
54095 assert( pA!=0 && pB!=0 );
54096 for(;;){
54097 if( pA->pgno<pB->pgno ){
54098 pTail->pDirty = pA;
54099 pTail = pA;
54100 pA = pA->pDirty;
54101 if( pA==0 ){
54102 pTail->pDirty = pB;
54103 break;
54104 }
54105 }else{
54106 pTail->pDirty = pB;
54107 pTail = pB;
54108 pB = pB->pDirty;
54109 if( pB==0 ){
54110 pTail->pDirty = pA;
54111 break;
54112 }
54113 }
54114 }
54115 return result.pDirty;
54116}
54117
54118/*
54119** Sort the list of pages in ascending order by pgno. Pages are
54120** connected by pDirty pointers. The pDirtyPrev pointers are
54121** corrupted by this sort.
54122**
54123** Since there cannot be more than 2^31 distinct pages in a database,
54124** there cannot be more than 31 buckets required by the merge sorter.
54125** One extra bucket is added to catch overflow in case something
54126** ever changes to make the previous sentence incorrect.
54127*/
54128#define N_SORT_BUCKET 32
54129static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
54130 PgHdr *a[N_SORT_BUCKET], *p;
54131 int i;
54132 memset(s: a, c: 0, n: sizeof(a));
54133 while( pIn ){
54134 p = pIn;
54135 pIn = p->pDirty;
54136 p->pDirty = 0;
54137 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
54138 if( a[i]==0 ){
54139 a[i] = p;
54140 break;
54141 }else{
54142 p = pcacheMergeDirtyList(pA: a[i], pB: p);
54143 a[i] = 0;
54144 }
54145 }
54146 if( NEVER(i==N_SORT_BUCKET-1) ){
54147 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
54148 ** the input list. But that is impossible.
54149 */
54150 a[i] = pcacheMergeDirtyList(pA: a[i], pB: p);
54151 }
54152 }
54153 p = a[0];
54154 for(i=1; i<N_SORT_BUCKET; i++){
54155 if( a[i]==0 ) continue;
54156 p = p ? pcacheMergeDirtyList(pA: p, pB: a[i]) : a[i];
54157 }
54158 return p;
54159}
54160
54161/*
54162** Return a list of all dirty pages in the cache, sorted by page number.
54163*/
54164SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
54165 PgHdr *p;
54166 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54167 p->pDirty = p->pDirtyNext;
54168 }
54169 return pcacheSortDirtyList(pIn: pCache->pDirty);
54170}
54171
54172/*
54173** Return the total number of references to all pages held by the cache.
54174**
54175** This is not the total number of pages referenced, but the sum of the
54176** reference count for all pages.
54177*/
54178SQLITE_PRIVATE i64 sqlite3PcacheRefCount(PCache *pCache){
54179 return pCache->nRefSum;
54180}
54181
54182/*
54183** Return the number of references to the page supplied as an argument.
54184*/
54185SQLITE_PRIVATE i64 sqlite3PcachePageRefcount(PgHdr *p){
54186 return p->nRef;
54187}
54188
54189/*
54190** Return the total number of pages in the cache.
54191*/
54192SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
54193 assert( pCache->pCache!=0 );
54194 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
54195}
54196
54197#ifdef SQLITE_TEST
54198/*
54199** Get the suggested cache-size value.
54200*/
54201SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
54202 return numberOfCachePages(pCache);
54203}
54204#endif
54205
54206/*
54207** Set the suggested cache-size value.
54208*/
54209SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
54210 assert( pCache->pCache!=0 );
54211 pCache->szCache = mxPage;
54212 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
54213 numberOfCachePages(p: pCache));
54214}
54215
54216/*
54217** Set the suggested cache-spill value. Make no changes if if the
54218** argument is zero. Return the effective cache-spill size, which will
54219** be the larger of the szSpill and szCache.
54220*/
54221SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
54222 int res;
54223 assert( p->pCache!=0 );
54224 if( mxPage ){
54225 if( mxPage<0 ){
54226 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
54227 }
54228 p->szSpill = mxPage;
54229 }
54230 res = numberOfCachePages(p);
54231 if( res<p->szSpill ) res = p->szSpill;
54232 return res;
54233}
54234
54235/*
54236** Free up as much memory as possible from the page cache.
54237*/
54238SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
54239 assert( pCache->pCache!=0 );
54240 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
54241}
54242
54243/*
54244** Return the size of the header added by this middleware layer
54245** in the page-cache hierarchy.
54246*/
54247SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
54248
54249/*
54250** Return the number of dirty pages currently in the cache, as a percentage
54251** of the configured cache size.
54252*/
54253SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
54254 PgHdr *pDirty;
54255 int nDirty = 0;
54256 int nCache = numberOfCachePages(p: pCache);
54257 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
54258 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
54259}
54260
54261#ifdef SQLITE_DIRECT_OVERFLOW_READ
54262/*
54263** Return true if there are one or more dirty pages in the cache. Else false.
54264*/
54265SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
54266 return (pCache->pDirty!=0);
54267}
54268#endif
54269
54270#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
54271/*
54272** For all dirty pages currently in the cache, invoke the specified
54273** callback. This is only used if the SQLITE_CHECK_PAGES macro is
54274** defined.
54275*/
54276SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
54277 PgHdr *pDirty;
54278 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
54279 xIter(pDirty);
54280 }
54281}
54282#endif
54283
54284/************** End of pcache.c **********************************************/
54285/************** Begin file pcache1.c *****************************************/
54286/*
54287** 2008 November 05
54288**
54289** The author disclaims copyright to this source code. In place of
54290** a legal notice, here is a blessing:
54291**
54292** May you do good and not evil.
54293** May you find forgiveness for yourself and forgive others.
54294** May you share freely, never taking more than you give.
54295**
54296*************************************************************************
54297**
54298** This file implements the default page cache implementation (the
54299** sqlite3_pcache interface). It also contains part of the implementation
54300** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
54301** If the default page cache implementation is overridden, then neither of
54302** these two features are available.
54303**
54304** A Page cache line looks like this:
54305**
54306** -------------------------------------------------------------
54307** | database page content | PgHdr1 | MemPage | PgHdr |
54308** -------------------------------------------------------------
54309**
54310** The database page content is up front (so that buffer overreads tend to
54311** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
54312** is the extension added by the btree.c module containing information such
54313** as the database page number and how that database page is used. PgHdr
54314** is added by the pcache.c layer and contains information used to keep track
54315** of which pages are "dirty". PgHdr1 is an extension added by this
54316** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
54317** PgHdr1 contains information needed to look up a page by its page number.
54318** The superclass sqlite3_pcache_page.pBuf points to the start of the
54319** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
54320**
54321** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
54322** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
54323** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
54324** size can vary according to architecture, compile-time options, and
54325** SQLite library version number.
54326**
54327** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
54328** was defined, then the page content would be held in a separate memory
54329** allocation from the PgHdr1. This was intended to avoid clownshoe memory
54330** allocations. However, the btree layer needs a small (16-byte) overrun
54331** area after the page content buffer. The header serves as that overrun
54332** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
54333** any possibility of a memory error.
54334**
54335** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
54336** with this module. Information is passed back and forth as PgHdr1 pointers.
54337**
54338** The pcache.c and pager.c modules deal pointers to PgHdr objects.
54339** The btree.c module deals with pointers to MemPage objects.
54340**
54341** SOURCE OF PAGE CACHE MEMORY:
54342**
54343** Memory for a page might come from any of three sources:
54344**
54345** (1) The general-purpose memory allocator - sqlite3Malloc()
54346** (2) Global page-cache memory provided using sqlite3_config() with
54347** SQLITE_CONFIG_PAGECACHE.
54348** (3) PCache-local bulk allocation.
54349**
54350** The third case is a chunk of heap memory (defaulting to 100 pages worth)
54351** that is allocated when the page cache is created. The size of the local
54352** bulk allocation can be adjusted using
54353**
54354** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
54355**
54356** If N is positive, then N pages worth of memory are allocated using a single
54357** sqlite3Malloc() call and that memory is used for the first N pages allocated.
54358** Or if N is negative, then -1024*N bytes of memory are allocated and used
54359** for as many pages as can be accommodated.
54360**
54361** Only one of (2) or (3) can be used. Once the memory available to (2) or
54362** (3) is exhausted, subsequent allocations fail over to the general-purpose
54363** memory allocator (1).
54364**
54365** Earlier versions of SQLite used only methods (1) and (2). But experiments
54366** show that method (3) with N==100 provides about a 5% performance boost for
54367** common workloads.
54368*/
54369/* #include "sqliteInt.h" */
54370
54371typedef struct PCache1 PCache1;
54372typedef struct PgHdr1 PgHdr1;
54373typedef struct PgFreeslot PgFreeslot;
54374typedef struct PGroup PGroup;
54375
54376/*
54377** Each cache entry is represented by an instance of the following
54378** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
54379** directly before this structure and is used to cache the page content.
54380**
54381** When reading a corrupt database file, it is possible that SQLite might
54382** read a few bytes (no more than 16 bytes) past the end of the page buffer.
54383** It will only read past the end of the page buffer, never write. This
54384** object is positioned immediately after the page buffer to serve as an
54385** overrun area, so that overreads are harmless.
54386**
54387** Variables isBulkLocal and isAnchor were once type "u8". That works,
54388** but causes a 2-byte gap in the structure for most architectures (since
54389** pointers must be either 4 or 8-byte aligned). As this structure is located
54390** in memory directly after the associated page data, if the database is
54391** corrupt, code at the b-tree layer may overread the page buffer and
54392** read part of this structure before the corruption is detected. This
54393** can cause a valgrind error if the uninitialized gap is accessed. Using u16
54394** ensures there is no such gap, and therefore no bytes of uninitialized
54395** memory in the structure.
54396**
54397** The pLruNext and pLruPrev pointers form a double-linked circular list
54398** of all pages that are unpinned. The PGroup.lru element (which should be
54399** the only element on the list with PgHdr1.isAnchor set to 1) forms the
54400** beginning and the end of the list.
54401*/
54402struct PgHdr1 {
54403 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
54404 unsigned int iKey; /* Key value (page number) */
54405 u16 isBulkLocal; /* This page from bulk local storage */
54406 u16 isAnchor; /* This is the PGroup.lru element */
54407 PgHdr1 *pNext; /* Next in hash table chain */
54408 PCache1 *pCache; /* Cache that currently owns this page */
54409 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
54410 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
54411 /* NB: pLruPrev is only valid if pLruNext!=0 */
54412};
54413
54414/*
54415** A page is pinned if it is not on the LRU list. To be "pinned" means
54416** that the page is in active use and must not be deallocated.
54417*/
54418#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
54419#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
54420
54421/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
54422** of one or more PCaches that are able to recycle each other's unpinned
54423** pages when they are under memory pressure. A PGroup is an instance of
54424** the following object.
54425**
54426** This page cache implementation works in one of two modes:
54427**
54428** (1) Every PCache is the sole member of its own PGroup. There is
54429** one PGroup per PCache.
54430**
54431** (2) There is a single global PGroup that all PCaches are a member
54432** of.
54433**
54434** Mode 1 uses more memory (since PCache instances are not able to rob
54435** unused pages from other PCaches) but it also operates without a mutex,
54436** and is therefore often faster. Mode 2 requires a mutex in order to be
54437** threadsafe, but recycles pages more efficiently.
54438**
54439** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
54440** PGroup which is the pcache1.grp global variable and its mutex is
54441** SQLITE_MUTEX_STATIC_LRU.
54442*/
54443struct PGroup {
54444 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
54445 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
54446 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
54447 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
54448 unsigned int nPurgeable; /* Number of purgeable pages allocated */
54449 PgHdr1 lru; /* The beginning and end of the LRU list */
54450};
54451
54452/* Each page cache is an instance of the following object. Every
54453** open database file (including each in-memory database and each
54454** temporary or transient database) has a single page cache which
54455** is an instance of this object.
54456**
54457** Pointers to structures of this type are cast and returned as
54458** opaque sqlite3_pcache* handles.
54459*/
54460struct PCache1 {
54461 /* Cache configuration parameters. Page size (szPage) and the purgeable
54462 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
54463 ** cache is created and are never changed thereafter. nMax may be
54464 ** modified at any time by a call to the pcache1Cachesize() method.
54465 ** The PGroup mutex must be held when accessing nMax.
54466 */
54467 PGroup *pGroup; /* PGroup this cache belongs to */
54468 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
54469 int szPage; /* Size of database content section */
54470 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
54471 int szAlloc; /* Total size of one pcache line */
54472 int bPurgeable; /* True if cache is purgeable */
54473 unsigned int nMin; /* Minimum number of pages reserved */
54474 unsigned int nMax; /* Configured "cache_size" value */
54475 unsigned int n90pct; /* nMax*9/10 */
54476 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
54477 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
54478
54479 /* Hash table of all pages. The following variables may only be accessed
54480 ** when the accessor is holding the PGroup mutex.
54481 */
54482 unsigned int nRecyclable; /* Number of pages in the LRU list */
54483 unsigned int nPage; /* Total number of pages in apHash */
54484 unsigned int nHash; /* Number of slots in apHash[] */
54485 PgHdr1 **apHash; /* Hash table for fast lookup by key */
54486 PgHdr1 *pFree; /* List of unused pcache-local pages */
54487 void *pBulk; /* Bulk memory used by pcache-local */
54488};
54489
54490/*
54491** Free slots in the allocator used to divide up the global page cache
54492** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
54493*/
54494struct PgFreeslot {
54495 PgFreeslot *pNext; /* Next free slot */
54496};
54497
54498/*
54499** Global data used by this cache.
54500*/
54501static SQLITE_WSD struct PCacheGlobal {
54502 PGroup grp; /* The global PGroup for mode (2) */
54503
54504 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
54505 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
54506 ** fixed at sqlite3_initialize() time and do not require mutex protection.
54507 ** The nFreeSlot and pFree values do require mutex protection.
54508 */
54509 int isInit; /* True if initialized */
54510 int separateCache; /* Use a new PGroup for each PCache */
54511 int nInitPage; /* Initial bulk allocation size */
54512 int szSlot; /* Size of each free slot */
54513 int nSlot; /* The number of pcache slots */
54514 int nReserve; /* Try to keep nFreeSlot above this */
54515 void *pStart, *pEnd; /* Bounds of global page cache memory */
54516 /* Above requires no mutex. Use mutex below for variable that follow. */
54517 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
54518 PgFreeslot *pFree; /* Free page blocks */
54519 int nFreeSlot; /* Number of unused pcache slots */
54520 /* The following value requires a mutex to change. We skip the mutex on
54521 ** reading because (1) most platforms read a 32-bit integer atomically and
54522 ** (2) even if an incorrect value is read, no great harm is done since this
54523 ** is really just an optimization. */
54524 int bUnderPressure; /* True if low on PAGECACHE memory */
54525} pcache1_g;
54526
54527/*
54528** All code in this file should access the global structure above via the
54529** alias "pcache1". This ensures that the WSD emulation is used when
54530** compiling for systems that do not support real WSD.
54531*/
54532#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
54533
54534/*
54535** Macros to enter and leave the PCache LRU mutex.
54536*/
54537#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
54538# define pcache1EnterMutex(X) assert((X)->mutex==0)
54539# define pcache1LeaveMutex(X) assert((X)->mutex==0)
54540# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
54541#else
54542# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
54543# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
54544# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
54545#endif
54546
54547/******************************************************************************/
54548/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
54549
54550
54551/*
54552** This function is called during initialization if a static buffer is
54553** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
54554** verb to sqlite3_config(). Parameter pBuf points to an allocation large
54555** enough to contain 'n' buffers of 'sz' bytes each.
54556**
54557** This routine is called from sqlite3_initialize() and so it is guaranteed
54558** to be serialized already. There is no need for further mutexing.
54559*/
54560SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
54561 if( pcache1.isInit ){
54562 PgFreeslot *p;
54563 if( pBuf==0 ) sz = n = 0;
54564 if( n==0 ) sz = 0;
54565 sz = ROUNDDOWN8(sz);
54566 pcache1.szSlot = sz;
54567 pcache1.nSlot = pcache1.nFreeSlot = n;
54568 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
54569 pcache1.pStart = pBuf;
54570 pcache1.pFree = 0;
54571 pcache1.bUnderPressure = 0;
54572 while( n-- ){
54573 p = (PgFreeslot*)pBuf;
54574 p->pNext = pcache1.pFree;
54575 pcache1.pFree = p;
54576 pBuf = (void*)&((char*)pBuf)[sz];
54577 }
54578 pcache1.pEnd = pBuf;
54579 }
54580}
54581
54582/*
54583** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
54584** true if pCache->pFree ends up containing one or more free pages.
54585*/
54586static int pcache1InitBulk(PCache1 *pCache){
54587 i64 szBulk;
54588 char *zBulk;
54589 if( pcache1.nInitPage==0 ) return 0;
54590 /* Do not bother with a bulk allocation if the cache size very small */
54591 if( pCache->nMax<3 ) return 0;
54592 sqlite3BeginBenignMalloc();
54593 if( pcache1.nInitPage>0 ){
54594 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
54595 }else{
54596 szBulk = -1024 * (i64)pcache1.nInitPage;
54597 }
54598 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
54599 szBulk = pCache->szAlloc*(i64)pCache->nMax;
54600 }
54601 zBulk = pCache->pBulk = sqlite3Malloc( n: szBulk );
54602 sqlite3EndBenignMalloc();
54603 if( zBulk ){
54604 int nBulk = sqlite3MallocSize(p: zBulk)/pCache->szAlloc;
54605 do{
54606 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
54607 pX->page.pBuf = zBulk;
54608 pX->page.pExtra = &pX[1];
54609 pX->isBulkLocal = 1;
54610 pX->isAnchor = 0;
54611 pX->pNext = pCache->pFree;
54612 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
54613 pCache->pFree = pX;
54614 zBulk += pCache->szAlloc;
54615 }while( --nBulk );
54616 }
54617 return pCache->pFree!=0;
54618}
54619
54620/*
54621** Malloc function used within this file to allocate space from the buffer
54622** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
54623** such buffer exists or there is no space left in it, this function falls
54624** back to sqlite3Malloc().
54625**
54626** Multiple threads can run this routine at the same time. Global variables
54627** in pcache1 need to be protected via mutex.
54628*/
54629static void *pcache1Alloc(int nByte){
54630 void *p = 0;
54631 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
54632 if( nByte<=pcache1.szSlot ){
54633 sqlite3_mutex_enter(pcache1.mutex);
54634 p = (PgHdr1 *)pcache1.pFree;
54635 if( p ){
54636 pcache1.pFree = pcache1.pFree->pNext;
54637 pcache1.nFreeSlot--;
54638 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
54639 assert( pcache1.nFreeSlot>=0 );
54640 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, X: nByte);
54641 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, N: 1);
54642 }
54643 sqlite3_mutex_leave(pcache1.mutex);
54644 }
54645 if( p==0 ){
54646 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
54647 ** it from sqlite3Malloc instead.
54648 */
54649 p = sqlite3Malloc(n: nByte);
54650#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
54651 if( p ){
54652 int sz = sqlite3MallocSize(p);
54653 sqlite3_mutex_enter(pcache1.mutex);
54654 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, X: nByte);
54655 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, N: sz);
54656 sqlite3_mutex_leave(pcache1.mutex);
54657 }
54658#endif
54659 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
54660 }
54661 return p;
54662}
54663
54664/*
54665** Free an allocated buffer obtained from pcache1Alloc().
54666*/
54667static void pcache1Free(void *p){
54668 if( p==0 ) return;
54669 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
54670 PgFreeslot *pSlot;
54671 sqlite3_mutex_enter(pcache1.mutex);
54672 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, N: 1);
54673 pSlot = (PgFreeslot*)p;
54674 pSlot->pNext = pcache1.pFree;
54675 pcache1.pFree = pSlot;
54676 pcache1.nFreeSlot++;
54677 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
54678 assert( pcache1.nFreeSlot<=pcache1.nSlot );
54679 sqlite3_mutex_leave(pcache1.mutex);
54680 }else{
54681 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
54682 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
54683#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
54684 {
54685 int nFreed = 0;
54686 nFreed = sqlite3MallocSize(p);
54687 sqlite3_mutex_enter(pcache1.mutex);
54688 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, N: nFreed);
54689 sqlite3_mutex_leave(pcache1.mutex);
54690 }
54691#endif
54692 sqlite3_free(p);
54693 }
54694}
54695
54696#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
54697/*
54698** Return the size of a pcache allocation
54699*/
54700static int pcache1MemSize(void *p){
54701 if( p>=pcache1.pStart && p<pcache1.pEnd ){
54702 return pcache1.szSlot;
54703 }else{
54704 int iSize;
54705 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
54706 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
54707 iSize = sqlite3MallocSize(p);
54708 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
54709 return iSize;
54710 }
54711}
54712#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
54713
54714/*
54715** Allocate a new page object initially associated with cache pCache.
54716*/
54717static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
54718 PgHdr1 *p = 0;
54719 void *pPg;
54720
54721 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54722 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
54723 assert( pCache->pFree!=0 );
54724 p = pCache->pFree;
54725 pCache->pFree = p->pNext;
54726 p->pNext = 0;
54727 }else{
54728#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
54729 /* The group mutex must be released before pcache1Alloc() is called. This
54730 ** is because it might call sqlite3_release_memory(), which assumes that
54731 ** this mutex is not held. */
54732 assert( pcache1.separateCache==0 );
54733 assert( pCache->pGroup==&pcache1.grp );
54734 pcache1LeaveMutex(pCache->pGroup);
54735#endif
54736 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
54737 pPg = pcache1Alloc(nByte: pCache->szAlloc);
54738 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
54739#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
54740 pcache1EnterMutex(pCache->pGroup);
54741#endif
54742 if( pPg==0 ) return 0;
54743 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
54744 p->page.pBuf = pPg;
54745 p->page.pExtra = &p[1];
54746 p->isBulkLocal = 0;
54747 p->isAnchor = 0;
54748 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
54749 }
54750 (*pCache->pnPurgeable)++;
54751 return p;
54752}
54753
54754/*
54755** Free a page object allocated by pcache1AllocPage().
54756*/
54757static void pcache1FreePage(PgHdr1 *p){
54758 PCache1 *pCache;
54759 assert( p!=0 );
54760 pCache = p->pCache;
54761 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
54762 if( p->isBulkLocal ){
54763 p->pNext = pCache->pFree;
54764 pCache->pFree = p;
54765 }else{
54766 pcache1Free(p: p->page.pBuf);
54767 }
54768 (*pCache->pnPurgeable)--;
54769}
54770
54771/*
54772** Malloc function used by SQLite to obtain space from the buffer configured
54773** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
54774** exists, this function falls back to sqlite3Malloc().
54775*/
54776SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
54777 assert( sz<=65536+8 ); /* These allocations are never very large */
54778 return pcache1Alloc(nByte: sz);
54779}
54780
54781/*
54782** Free an allocated buffer obtained from sqlite3PageMalloc().
54783*/
54784SQLITE_PRIVATE void sqlite3PageFree(void *p){
54785 pcache1Free(p);
54786}
54787
54788
54789/*
54790** Return true if it desirable to avoid allocating a new page cache
54791** entry.
54792**
54793** If memory was allocated specifically to the page cache using
54794** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
54795** it is desirable to avoid allocating a new page cache entry because
54796** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
54797** for all page cache needs and we should not need to spill the
54798** allocation onto the heap.
54799**
54800** Or, the heap is used for all page cache memory but the heap is
54801** under memory pressure, then again it is desirable to avoid
54802** allocating a new page cache entry in order to avoid stressing
54803** the heap even further.
54804*/
54805static int pcache1UnderMemoryPressure(PCache1 *pCache){
54806 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
54807 return pcache1.bUnderPressure;
54808 }else{
54809 return sqlite3HeapNearlyFull();
54810 }
54811}
54812
54813/******************************************************************************/
54814/******** General Implementation Functions ************************************/
54815
54816/*
54817** This function is used to resize the hash table used by the cache passed
54818** as the first argument.
54819**
54820** The PCache mutex must be held when this function is called.
54821*/
54822static void pcache1ResizeHash(PCache1 *p){
54823 PgHdr1 **apNew;
54824 unsigned int nNew;
54825 unsigned int i;
54826
54827 assert( sqlite3_mutex_held(p->pGroup->mutex) );
54828
54829 nNew = p->nHash*2;
54830 if( nNew<256 ){
54831 nNew = 256;
54832 }
54833
54834 pcache1LeaveMutex(p->pGroup);
54835 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
54836 apNew = (PgHdr1 **)sqlite3MallocZero(n: sizeof(PgHdr1 *)*nNew);
54837 if( p->nHash ){ sqlite3EndBenignMalloc(); }
54838 pcache1EnterMutex(p->pGroup);
54839 if( apNew ){
54840 for(i=0; i<p->nHash; i++){
54841 PgHdr1 *pPage;
54842 PgHdr1 *pNext = p->apHash[i];
54843 while( (pPage = pNext)!=0 ){
54844 unsigned int h = pPage->iKey % nNew;
54845 pNext = pPage->pNext;
54846 pPage->pNext = apNew[h];
54847 apNew[h] = pPage;
54848 }
54849 }
54850 sqlite3_free(p: p->apHash);
54851 p->apHash = apNew;
54852 p->nHash = nNew;
54853 }
54854}
54855
54856/*
54857** This function is used internally to remove the page pPage from the
54858** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
54859** LRU list, then this function is a no-op.
54860**
54861** The PGroup mutex must be held when this function is called.
54862*/
54863static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
54864 assert( pPage!=0 );
54865 assert( PAGE_IS_UNPINNED(pPage) );
54866 assert( pPage->pLruNext );
54867 assert( pPage->pLruPrev );
54868 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
54869 pPage->pLruPrev->pLruNext = pPage->pLruNext;
54870 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
54871 pPage->pLruNext = 0;
54872 /* pPage->pLruPrev = 0;
54873 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
54874 assert( pPage->isAnchor==0 );
54875 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
54876 pPage->pCache->nRecyclable--;
54877 return pPage;
54878}
54879
54880
54881/*
54882** Remove the page supplied as an argument from the hash table
54883** (PCache1.apHash structure) that it is currently stored in.
54884** Also free the page if freePage is true.
54885**
54886** The PGroup mutex must be held when this function is called.
54887*/
54888static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
54889 unsigned int h;
54890 PCache1 *pCache = pPage->pCache;
54891 PgHdr1 **pp;
54892
54893 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54894 h = pPage->iKey % pCache->nHash;
54895 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
54896 *pp = (*pp)->pNext;
54897
54898 pCache->nPage--;
54899 if( freeFlag ) pcache1FreePage(p: pPage);
54900}
54901
54902/*
54903** If there are currently more than nMaxPage pages allocated, try
54904** to recycle pages to reduce the number allocated to nMaxPage.
54905*/
54906static void pcache1EnforceMaxPage(PCache1 *pCache){
54907 PGroup *pGroup = pCache->pGroup;
54908 PgHdr1 *p;
54909 assert( sqlite3_mutex_held(pGroup->mutex) );
54910 while( pGroup->nPurgeable>pGroup->nMaxPage
54911 && (p=pGroup->lru.pLruPrev)->isAnchor==0
54912 ){
54913 assert( p->pCache->pGroup==pGroup );
54914 assert( PAGE_IS_UNPINNED(p) );
54915 pcache1PinPage(pPage: p);
54916 pcache1RemoveFromHash(pPage: p, freeFlag: 1);
54917 }
54918 if( pCache->nPage==0 && pCache->pBulk ){
54919 sqlite3_free(p: pCache->pBulk);
54920 pCache->pBulk = pCache->pFree = 0;
54921 }
54922}
54923
54924/*
54925** Discard all pages from cache pCache with a page number (key value)
54926** greater than or equal to iLimit. Any pinned pages that meet this
54927** criteria are unpinned before they are discarded.
54928**
54929** The PCache mutex must be held when this function is called.
54930*/
54931static void pcache1TruncateUnsafe(
54932 PCache1 *pCache, /* The cache to truncate */
54933 unsigned int iLimit /* Drop pages with this pgno or larger */
54934){
54935 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
54936 unsigned int h, iStop;
54937 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54938 assert( pCache->iMaxKey >= iLimit );
54939 assert( pCache->nHash > 0 );
54940 if( pCache->iMaxKey - iLimit < pCache->nHash ){
54941 /* If we are just shaving the last few pages off the end of the
54942 ** cache, then there is no point in scanning the entire hash table.
54943 ** Only scan those hash slots that might contain pages that need to
54944 ** be removed. */
54945 h = iLimit % pCache->nHash;
54946 iStop = pCache->iMaxKey % pCache->nHash;
54947 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
54948 }else{
54949 /* This is the general case where many pages are being removed.
54950 ** It is necessary to scan the entire hash table */
54951 h = pCache->nHash/2;
54952 iStop = h - 1;
54953 }
54954 for(;;){
54955 PgHdr1 **pp;
54956 PgHdr1 *pPage;
54957 assert( h<pCache->nHash );
54958 pp = &pCache->apHash[h];
54959 while( (pPage = *pp)!=0 ){
54960 if( pPage->iKey>=iLimit ){
54961 pCache->nPage--;
54962 *pp = pPage->pNext;
54963 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
54964 pcache1FreePage(p: pPage);
54965 }else{
54966 pp = &pPage->pNext;
54967 TESTONLY( if( nPage>=0 ) nPage++; )
54968 }
54969 }
54970 if( h==iStop ) break;
54971 h = (h+1) % pCache->nHash;
54972 }
54973 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
54974}
54975
54976/******************************************************************************/
54977/******** sqlite3_pcache Methods **********************************************/
54978
54979/*
54980** Implementation of the sqlite3_pcache.xInit method.
54981*/
54982static int pcache1Init(void *NotUsed){
54983 UNUSED_PARAMETER(NotUsed);
54984 assert( pcache1.isInit==0 );
54985 memset(s: &pcache1, c: 0, n: sizeof(pcache1));
54986
54987
54988 /*
54989 ** The pcache1.separateCache variable is true if each PCache has its own
54990 ** private PGroup (mode-1). pcache1.separateCache is false if the single
54991 ** PGroup in pcache1.grp is used for all page caches (mode-2).
54992 **
54993 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
54994 **
54995 ** * Use a unified cache in single-threaded applications that have
54996 ** configured a start-time buffer for use as page-cache memory using
54997 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
54998 ** pBuf argument.
54999 **
55000 ** * Otherwise use separate caches (mode-1)
55001 */
55002#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
55003 pcache1.separateCache = 0;
55004#elif SQLITE_THREADSAFE
55005 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
55006 || sqlite3GlobalConfig.bCoreMutex>0;
55007#else
55008 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
55009#endif
55010
55011#if SQLITE_THREADSAFE
55012 if( sqlite3GlobalConfig.bCoreMutex ){
55013 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
55014 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
55015 }
55016#endif
55017 if( pcache1.separateCache
55018 && sqlite3GlobalConfig.nPage!=0
55019 && sqlite3GlobalConfig.pPage==0
55020 ){
55021 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
55022 }else{
55023 pcache1.nInitPage = 0;
55024 }
55025 pcache1.grp.mxPinned = 10;
55026 pcache1.isInit = 1;
55027 return SQLITE_OK;
55028}
55029
55030/*
55031** Implementation of the sqlite3_pcache.xShutdown method.
55032** Note that the static mutex allocated in xInit does
55033** not need to be freed.
55034*/
55035static void pcache1Shutdown(void *NotUsed){
55036 UNUSED_PARAMETER(NotUsed);
55037 assert( pcache1.isInit!=0 );
55038 memset(s: &pcache1, c: 0, n: sizeof(pcache1));
55039}
55040
55041/* forward declaration */
55042static void pcache1Destroy(sqlite3_pcache *p);
55043
55044/*
55045** Implementation of the sqlite3_pcache.xCreate method.
55046**
55047** Allocate a new cache.
55048*/
55049static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
55050 PCache1 *pCache; /* The newly created page cache */
55051 PGroup *pGroup; /* The group the new page cache will belong to */
55052 int sz; /* Bytes of memory required to allocate the new cache */
55053
55054 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
55055 assert( szExtra < 300 );
55056
55057 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
55058 pCache = (PCache1 *)sqlite3MallocZero(n: sz);
55059 if( pCache ){
55060 if( pcache1.separateCache ){
55061 pGroup = (PGroup*)&pCache[1];
55062 pGroup->mxPinned = 10;
55063 }else{
55064 pGroup = &pcache1.grp;
55065 }
55066 pcache1EnterMutex(pGroup);
55067 if( pGroup->lru.isAnchor==0 ){
55068 pGroup->lru.isAnchor = 1;
55069 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
55070 }
55071 pCache->pGroup = pGroup;
55072 pCache->szPage = szPage;
55073 pCache->szExtra = szExtra;
55074 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
55075 pCache->bPurgeable = (bPurgeable ? 1 : 0);
55076 pcache1ResizeHash(p: pCache);
55077 if( bPurgeable ){
55078 pCache->nMin = 10;
55079 pGroup->nMinPage += pCache->nMin;
55080 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55081 pCache->pnPurgeable = &pGroup->nPurgeable;
55082 }else{
55083 pCache->pnPurgeable = &pCache->nPurgeableDummy;
55084 }
55085 pcache1LeaveMutex(pGroup);
55086 if( pCache->nHash==0 ){
55087 pcache1Destroy(p: (sqlite3_pcache*)pCache);
55088 pCache = 0;
55089 }
55090 }
55091 return (sqlite3_pcache *)pCache;
55092}
55093
55094/*
55095** Implementation of the sqlite3_pcache.xCachesize method.
55096**
55097** Configure the cache_size limit for a cache.
55098*/
55099static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
55100 PCache1 *pCache = (PCache1 *)p;
55101 u32 n;
55102 assert( nMax>=0 );
55103 if( pCache->bPurgeable ){
55104 PGroup *pGroup = pCache->pGroup;
55105 pcache1EnterMutex(pGroup);
55106 n = (u32)nMax;
55107 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
55108 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
55109 }
55110 pGroup->nMaxPage += (n - pCache->nMax);
55111 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55112 pCache->nMax = n;
55113 pCache->n90pct = pCache->nMax*9/10;
55114 pcache1EnforceMaxPage(pCache);
55115 pcache1LeaveMutex(pGroup);
55116 }
55117}
55118
55119/*
55120** Implementation of the sqlite3_pcache.xShrink method.
55121**
55122** Free up as much memory as possible.
55123*/
55124static void pcache1Shrink(sqlite3_pcache *p){
55125 PCache1 *pCache = (PCache1*)p;
55126 if( pCache->bPurgeable ){
55127 PGroup *pGroup = pCache->pGroup;
55128 unsigned int savedMaxPage;
55129 pcache1EnterMutex(pGroup);
55130 savedMaxPage = pGroup->nMaxPage;
55131 pGroup->nMaxPage = 0;
55132 pcache1EnforceMaxPage(pCache);
55133 pGroup->nMaxPage = savedMaxPage;
55134 pcache1LeaveMutex(pGroup);
55135 }
55136}
55137
55138/*
55139** Implementation of the sqlite3_pcache.xPagecount method.
55140*/
55141static int pcache1Pagecount(sqlite3_pcache *p){
55142 int n;
55143 PCache1 *pCache = (PCache1*)p;
55144 pcache1EnterMutex(pCache->pGroup);
55145 n = pCache->nPage;
55146 pcache1LeaveMutex(pCache->pGroup);
55147 return n;
55148}
55149
55150
55151/*
55152** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
55153** in the header of the pcache1Fetch() procedure.
55154**
55155** This steps are broken out into a separate procedure because they are
55156** usually not needed, and by avoiding the stack initialization required
55157** for these steps, the main pcache1Fetch() procedure can run faster.
55158*/
55159static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
55160 PCache1 *pCache,
55161 unsigned int iKey,
55162 int createFlag
55163){
55164 unsigned int nPinned;
55165 PGroup *pGroup = pCache->pGroup;
55166 PgHdr1 *pPage = 0;
55167
55168 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
55169 assert( pCache->nPage >= pCache->nRecyclable );
55170 nPinned = pCache->nPage - pCache->nRecyclable;
55171 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
55172 assert( pCache->n90pct == pCache->nMax*9/10 );
55173 if( createFlag==1 && (
55174 nPinned>=pGroup->mxPinned
55175 || nPinned>=pCache->n90pct
55176 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
55177 )){
55178 return 0;
55179 }
55180
55181 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(p: pCache);
55182 assert( pCache->nHash>0 && pCache->apHash );
55183
55184 /* Step 4. Try to recycle a page. */
55185 if( pCache->bPurgeable
55186 && !pGroup->lru.pLruPrev->isAnchor
55187 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
55188 ){
55189 PCache1 *pOther;
55190 pPage = pGroup->lru.pLruPrev;
55191 assert( PAGE_IS_UNPINNED(pPage) );
55192 pcache1RemoveFromHash(pPage, freeFlag: 0);
55193 pcache1PinPage(pPage);
55194 pOther = pPage->pCache;
55195 if( pOther->szAlloc != pCache->szAlloc ){
55196 pcache1FreePage(p: pPage);
55197 pPage = 0;
55198 }else{
55199 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
55200 }
55201 }
55202
55203 /* Step 5. If a usable page buffer has still not been found,
55204 ** attempt to allocate a new one.
55205 */
55206 if( !pPage ){
55207 pPage = pcache1AllocPage(pCache, benignMalloc: createFlag==1);
55208 }
55209
55210 if( pPage ){
55211 unsigned int h = iKey % pCache->nHash;
55212 pCache->nPage++;
55213 pPage->iKey = iKey;
55214 pPage->pNext = pCache->apHash[h];
55215 pPage->pCache = pCache;
55216 pPage->pLruNext = 0;
55217 /* pPage->pLruPrev = 0;
55218 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
55219 *(void **)pPage->page.pExtra = 0;
55220 pCache->apHash[h] = pPage;
55221 if( iKey>pCache->iMaxKey ){
55222 pCache->iMaxKey = iKey;
55223 }
55224 }
55225 return pPage;
55226}
55227
55228/*
55229** Implementation of the sqlite3_pcache.xFetch method.
55230**
55231** Fetch a page by key value.
55232**
55233** Whether or not a new page may be allocated by this function depends on
55234** the value of the createFlag argument. 0 means do not allocate a new
55235** page. 1 means allocate a new page if space is easily available. 2
55236** means to try really hard to allocate a new page.
55237**
55238** For a non-purgeable cache (a cache used as the storage for an in-memory
55239** database) there is really no difference between createFlag 1 and 2. So
55240** the calling function (pcache.c) will never have a createFlag of 1 on
55241** a non-purgeable cache.
55242**
55243** There are three different approaches to obtaining space for a page,
55244** depending on the value of parameter createFlag (which may be 0, 1 or 2).
55245**
55246** 1. Regardless of the value of createFlag, the cache is searched for a
55247** copy of the requested page. If one is found, it is returned.
55248**
55249** 2. If createFlag==0 and the page is not already in the cache, NULL is
55250** returned.
55251**
55252** 3. If createFlag is 1, and the page is not already in the cache, then
55253** return NULL (do not allocate a new page) if any of the following
55254** conditions are true:
55255**
55256** (a) the number of pages pinned by the cache is greater than
55257** PCache1.nMax, or
55258**
55259** (b) the number of pages pinned by the cache is greater than
55260** the sum of nMax for all purgeable caches, less the sum of
55261** nMin for all other purgeable caches, or
55262**
55263** 4. If none of the first three conditions apply and the cache is marked
55264** as purgeable, and if one of the following is true:
55265**
55266** (a) The number of pages allocated for the cache is already
55267** PCache1.nMax, or
55268**
55269** (b) The number of pages allocated for all purgeable caches is
55270** already equal to or greater than the sum of nMax for all
55271** purgeable caches,
55272**
55273** (c) The system is under memory pressure and wants to avoid
55274** unnecessary pages cache entry allocations
55275**
55276** then attempt to recycle a page from the LRU list. If it is the right
55277** size, return the recycled buffer. Otherwise, free the buffer and
55278** proceed to step 5.
55279**
55280** 5. Otherwise, allocate and return a new page buffer.
55281**
55282** There are two versions of this routine. pcache1FetchWithMutex() is
55283** the general case. pcache1FetchNoMutex() is a faster implementation for
55284** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
55285** invokes the appropriate routine.
55286*/
55287static PgHdr1 *pcache1FetchNoMutex(
55288 sqlite3_pcache *p,
55289 unsigned int iKey,
55290 int createFlag
55291){
55292 PCache1 *pCache = (PCache1 *)p;
55293 PgHdr1 *pPage = 0;
55294
55295 /* Step 1: Search the hash table for an existing entry. */
55296 pPage = pCache->apHash[iKey % pCache->nHash];
55297 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
55298
55299 /* Step 2: If the page was found in the hash table, then return it.
55300 ** If the page was not in the hash table and createFlag is 0, abort.
55301 ** Otherwise (page not in hash and createFlag!=0) continue with
55302 ** subsequent steps to try to create the page. */
55303 if( pPage ){
55304 if( PAGE_IS_UNPINNED(pPage) ){
55305 return pcache1PinPage(pPage);
55306 }else{
55307 return pPage;
55308 }
55309 }else if( createFlag ){
55310 /* Steps 3, 4, and 5 implemented by this subroutine */
55311 return pcache1FetchStage2(pCache, iKey, createFlag);
55312 }else{
55313 return 0;
55314 }
55315}
55316#if PCACHE1_MIGHT_USE_GROUP_MUTEX
55317static PgHdr1 *pcache1FetchWithMutex(
55318 sqlite3_pcache *p,
55319 unsigned int iKey,
55320 int createFlag
55321){
55322 PCache1 *pCache = (PCache1 *)p;
55323 PgHdr1 *pPage;
55324
55325 pcache1EnterMutex(pCache->pGroup);
55326 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
55327 assert( pPage==0 || pCache->iMaxKey>=iKey );
55328 pcache1LeaveMutex(pCache->pGroup);
55329 return pPage;
55330}
55331#endif
55332static sqlite3_pcache_page *pcache1Fetch(
55333 sqlite3_pcache *p,
55334 unsigned int iKey,
55335 int createFlag
55336){
55337#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
55338 PCache1 *pCache = (PCache1 *)p;
55339#endif
55340
55341 assert( offsetof(PgHdr1,page)==0 );
55342 assert( pCache->bPurgeable || createFlag!=1 );
55343 assert( pCache->bPurgeable || pCache->nMin==0 );
55344 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
55345 assert( pCache->nMin==0 || pCache->bPurgeable );
55346 assert( pCache->nHash>0 );
55347#if PCACHE1_MIGHT_USE_GROUP_MUTEX
55348 if( pCache->pGroup->mutex ){
55349 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
55350 }else
55351#endif
55352 {
55353 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
55354 }
55355}
55356
55357
55358/*
55359** Implementation of the sqlite3_pcache.xUnpin method.
55360**
55361** Mark a page as unpinned (eligible for asynchronous recycling).
55362*/
55363static void pcache1Unpin(
55364 sqlite3_pcache *p,
55365 sqlite3_pcache_page *pPg,
55366 int reuseUnlikely
55367){
55368 PCache1 *pCache = (PCache1 *)p;
55369 PgHdr1 *pPage = (PgHdr1 *)pPg;
55370 PGroup *pGroup = pCache->pGroup;
55371
55372 assert( pPage->pCache==pCache );
55373 pcache1EnterMutex(pGroup);
55374
55375 /* It is an error to call this function if the page is already
55376 ** part of the PGroup LRU list.
55377 */
55378 assert( pPage->pLruNext==0 );
55379 assert( PAGE_IS_PINNED(pPage) );
55380
55381 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
55382 pcache1RemoveFromHash(pPage, freeFlag: 1);
55383 }else{
55384 /* Add the page to the PGroup LRU list. */
55385 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
55386 pPage->pLruPrev = &pGroup->lru;
55387 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
55388 *ppFirst = pPage;
55389 pCache->nRecyclable++;
55390 }
55391
55392 pcache1LeaveMutex(pCache->pGroup);
55393}
55394
55395/*
55396** Implementation of the sqlite3_pcache.xRekey method.
55397*/
55398static void pcache1Rekey(
55399 sqlite3_pcache *p,
55400 sqlite3_pcache_page *pPg,
55401 unsigned int iOld,
55402 unsigned int iNew
55403){
55404 PCache1 *pCache = (PCache1 *)p;
55405 PgHdr1 *pPage = (PgHdr1 *)pPg;
55406 PgHdr1 **pp;
55407 unsigned int hOld, hNew;
55408 assert( pPage->iKey==iOld );
55409 assert( pPage->pCache==pCache );
55410 assert( iOld!=iNew ); /* The page number really is changing */
55411
55412 pcache1EnterMutex(pCache->pGroup);
55413
55414 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
55415 hOld = iOld%pCache->nHash;
55416 pp = &pCache->apHash[hOld];
55417 while( (*pp)!=pPage ){
55418 pp = &(*pp)->pNext;
55419 }
55420 *pp = pPage->pNext;
55421
55422 assert( pcache1FetchNoMutex(p, iNew, 0)==0 ); /* iNew not in cache */
55423 hNew = iNew%pCache->nHash;
55424 pPage->iKey = iNew;
55425 pPage->pNext = pCache->apHash[hNew];
55426 pCache->apHash[hNew] = pPage;
55427 if( iNew>pCache->iMaxKey ){
55428 pCache->iMaxKey = iNew;
55429 }
55430
55431 pcache1LeaveMutex(pCache->pGroup);
55432}
55433
55434/*
55435** Implementation of the sqlite3_pcache.xTruncate method.
55436**
55437** Discard all unpinned pages in the cache with a page number equal to
55438** or greater than parameter iLimit. Any pinned pages with a page number
55439** equal to or greater than iLimit are implicitly unpinned.
55440*/
55441static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
55442 PCache1 *pCache = (PCache1 *)p;
55443 pcache1EnterMutex(pCache->pGroup);
55444 if( iLimit<=pCache->iMaxKey ){
55445 pcache1TruncateUnsafe(pCache, iLimit);
55446 pCache->iMaxKey = iLimit-1;
55447 }
55448 pcache1LeaveMutex(pCache->pGroup);
55449}
55450
55451/*
55452** Implementation of the sqlite3_pcache.xDestroy method.
55453**
55454** Destroy a cache allocated using pcache1Create().
55455*/
55456static void pcache1Destroy(sqlite3_pcache *p){
55457 PCache1 *pCache = (PCache1 *)p;
55458 PGroup *pGroup = pCache->pGroup;
55459 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
55460 pcache1EnterMutex(pGroup);
55461 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, iLimit: 0);
55462 assert( pGroup->nMaxPage >= pCache->nMax );
55463 pGroup->nMaxPage -= pCache->nMax;
55464 assert( pGroup->nMinPage >= pCache->nMin );
55465 pGroup->nMinPage -= pCache->nMin;
55466 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55467 pcache1EnforceMaxPage(pCache);
55468 pcache1LeaveMutex(pGroup);
55469 sqlite3_free(p: pCache->pBulk);
55470 sqlite3_free(p: pCache->apHash);
55471 sqlite3_free(p: pCache);
55472}
55473
55474/*
55475** This function is called during initialization (sqlite3_initialize()) to
55476** install the default pluggable cache module, assuming the user has not
55477** already provided an alternative.
55478*/
55479SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
55480 static const sqlite3_pcache_methods2 defaultMethods = {
55481 1, /* iVersion */
55482 0, /* pArg */
55483 pcache1Init, /* xInit */
55484 pcache1Shutdown, /* xShutdown */
55485 pcache1Create, /* xCreate */
55486 pcache1Cachesize, /* xCachesize */
55487 pcache1Pagecount, /* xPagecount */
55488 pcache1Fetch, /* xFetch */
55489 pcache1Unpin, /* xUnpin */
55490 pcache1Rekey, /* xRekey */
55491 pcache1Truncate, /* xTruncate */
55492 pcache1Destroy, /* xDestroy */
55493 pcache1Shrink /* xShrink */
55494 };
55495 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
55496}
55497
55498/*
55499** Return the size of the header on each page of this PCACHE implementation.
55500*/
55501SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
55502
55503/*
55504** Return the global mutex used by this PCACHE implementation. The
55505** sqlite3_status() routine needs access to this mutex.
55506*/
55507SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
55508 return pcache1.mutex;
55509}
55510
55511#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
55512/*
55513** This function is called to free superfluous dynamically allocated memory
55514** held by the pager system. Memory in use by any SQLite pager allocated
55515** by the current thread may be sqlite3_free()ed.
55516**
55517** nReq is the number of bytes of memory required. Once this much has
55518** been released, the function returns. The return value is the total number
55519** of bytes of memory released.
55520*/
55521SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
55522 int nFree = 0;
55523 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
55524 assert( sqlite3_mutex_notheld(pcache1.mutex) );
55525 if( sqlite3GlobalConfig.pPage==0 ){
55526 PgHdr1 *p;
55527 pcache1EnterMutex(&pcache1.grp);
55528 while( (nReq<0 || nFree<nReq)
55529 && (p=pcache1.grp.lru.pLruPrev)!=0
55530 && p->isAnchor==0
55531 ){
55532 nFree += pcache1MemSize(p->page.pBuf);
55533 assert( PAGE_IS_UNPINNED(p) );
55534 pcache1PinPage(p);
55535 pcache1RemoveFromHash(p, 1);
55536 }
55537 pcache1LeaveMutex(&pcache1.grp);
55538 }
55539 return nFree;
55540}
55541#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
55542
55543#ifdef SQLITE_TEST
55544/*
55545** This function is used by test procedures to inspect the internal state
55546** of the global cache.
55547*/
55548SQLITE_PRIVATE void sqlite3PcacheStats(
55549 int *pnCurrent, /* OUT: Total number of pages cached */
55550 int *pnMax, /* OUT: Global maximum cache size */
55551 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
55552 int *pnRecyclable /* OUT: Total number of pages available for recycling */
55553){
55554 PgHdr1 *p;
55555 int nRecyclable = 0;
55556 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
55557 assert( PAGE_IS_UNPINNED(p) );
55558 nRecyclable++;
55559 }
55560 *pnCurrent = pcache1.grp.nPurgeable;
55561 *pnMax = (int)pcache1.grp.nMaxPage;
55562 *pnMin = (int)pcache1.grp.nMinPage;
55563 *pnRecyclable = nRecyclable;
55564}
55565#endif
55566
55567/************** End of pcache1.c *********************************************/
55568/************** Begin file rowset.c ******************************************/
55569/*
55570** 2008 December 3
55571**
55572** The author disclaims copyright to this source code. In place of
55573** a legal notice, here is a blessing:
55574**
55575** May you do good and not evil.
55576** May you find forgiveness for yourself and forgive others.
55577** May you share freely, never taking more than you give.
55578**
55579*************************************************************************
55580**
55581** This module implements an object we call a "RowSet".
55582**
55583** The RowSet object is a collection of rowids. Rowids
55584** are inserted into the RowSet in an arbitrary order. Inserts
55585** can be intermixed with tests to see if a given rowid has been
55586** previously inserted into the RowSet.
55587**
55588** After all inserts are finished, it is possible to extract the
55589** elements of the RowSet in sorted order. Once this extraction
55590** process has started, no new elements may be inserted.
55591**
55592** Hence, the primitive operations for a RowSet are:
55593**
55594** CREATE
55595** INSERT
55596** TEST
55597** SMALLEST
55598** DESTROY
55599**
55600** The CREATE and DESTROY primitives are the constructor and destructor,
55601** obviously. The INSERT primitive adds a new element to the RowSet.
55602** TEST checks to see if an element is already in the RowSet. SMALLEST
55603** extracts the least value from the RowSet.
55604**
55605** The INSERT primitive might allocate additional memory. Memory is
55606** allocated in chunks so most INSERTs do no allocation. There is an
55607** upper bound on the size of allocated memory. No memory is freed
55608** until DESTROY.
55609**
55610** The TEST primitive includes a "batch" number. The TEST primitive
55611** will only see elements that were inserted before the last change
55612** in the batch number. In other words, if an INSERT occurs between
55613** two TESTs where the TESTs have the same batch number, then the
55614** value added by the INSERT will not be visible to the second TEST.
55615** The initial batch number is zero, so if the very first TEST contains
55616** a non-zero batch number, it will see all prior INSERTs.
55617**
55618** No INSERTs may occurs after a SMALLEST. An assertion will fail if
55619** that is attempted.
55620**
55621** The cost of an INSERT is roughly constant. (Sometimes new memory
55622** has to be allocated on an INSERT.) The cost of a TEST with a new
55623** batch number is O(NlogN) where N is the number of elements in the RowSet.
55624** The cost of a TEST using the same batch number is O(logN). The cost
55625** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
55626** primitives are constant time. The cost of DESTROY is O(N).
55627**
55628** TEST and SMALLEST may not be used by the same RowSet. This used to
55629** be possible, but the feature was not used, so it was removed in order
55630** to simplify the code.
55631*/
55632/* #include "sqliteInt.h" */
55633
55634
55635/*
55636** Target size for allocation chunks.
55637*/
55638#define ROWSET_ALLOCATION_SIZE 1024
55639
55640/*
55641** The number of rowset entries per allocation chunk.
55642*/
55643#define ROWSET_ENTRY_PER_CHUNK \
55644 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
55645
55646/*
55647** Each entry in a RowSet is an instance of the following object.
55648**
55649** This same object is reused to store a linked list of trees of RowSetEntry
55650** objects. In that alternative use, pRight points to the next entry
55651** in the list, pLeft points to the tree, and v is unused. The
55652** RowSet.pForest value points to the head of this forest list.
55653*/
55654struct RowSetEntry {
55655 i64 v; /* ROWID value for this entry */
55656 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
55657 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
55658};
55659
55660/*
55661** RowSetEntry objects are allocated in large chunks (instances of the
55662** following structure) to reduce memory allocation overhead. The
55663** chunks are kept on a linked list so that they can be deallocated
55664** when the RowSet is destroyed.
55665*/
55666struct RowSetChunk {
55667 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
55668 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
55669};
55670
55671/*
55672** A RowSet in an instance of the following structure.
55673**
55674** A typedef of this structure if found in sqliteInt.h.
55675*/
55676struct RowSet {
55677 struct RowSetChunk *pChunk; /* List of all chunk allocations */
55678 sqlite3 *db; /* The database connection */
55679 struct RowSetEntry *pEntry; /* List of entries using pRight */
55680 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
55681 struct RowSetEntry *pFresh; /* Source of new entry objects */
55682 struct RowSetEntry *pForest; /* List of binary trees of entries */
55683 u16 nFresh; /* Number of objects on pFresh */
55684 u16 rsFlags; /* Various flags */
55685 int iBatch; /* Current insert batch */
55686};
55687
55688/*
55689** Allowed values for RowSet.rsFlags
55690*/
55691#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
55692#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
55693
55694/*
55695** Allocate a RowSet object. Return NULL if a memory allocation
55696** error occurs.
55697*/
55698SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
55699 RowSet *p = sqlite3DbMallocRawNN(db, n: sizeof(*p));
55700 if( p ){
55701 int N = sqlite3DbMallocSize(db, p);
55702 p->pChunk = 0;
55703 p->db = db;
55704 p->pEntry = 0;
55705 p->pLast = 0;
55706 p->pForest = 0;
55707 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
55708 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
55709 p->rsFlags = ROWSET_SORTED;
55710 p->iBatch = 0;
55711 }
55712 return p;
55713}
55714
55715/*
55716** Deallocate all chunks from a RowSet. This frees all memory that
55717** the RowSet has allocated over its lifetime. This routine is
55718** the destructor for the RowSet.
55719*/
55720SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
55721 RowSet *p = (RowSet*)pArg;
55722 struct RowSetChunk *pChunk, *pNextChunk;
55723 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
55724 pNextChunk = pChunk->pNextChunk;
55725 sqlite3DbFree(db: p->db, p: pChunk);
55726 }
55727 p->pChunk = 0;
55728 p->nFresh = 0;
55729 p->pEntry = 0;
55730 p->pLast = 0;
55731 p->pForest = 0;
55732 p->rsFlags = ROWSET_SORTED;
55733}
55734
55735/*
55736** Deallocate all chunks from a RowSet. This frees all memory that
55737** the RowSet has allocated over its lifetime. This routine is
55738** the destructor for the RowSet.
55739*/
55740SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
55741 sqlite3RowSetClear(pArg);
55742 sqlite3DbFree(db: ((RowSet*)pArg)->db, p: pArg);
55743}
55744
55745/*
55746** Allocate a new RowSetEntry object that is associated with the
55747** given RowSet. Return a pointer to the new and completely uninitialized
55748** object.
55749**
55750** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
55751** routine returns NULL.
55752*/
55753static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
55754 assert( p!=0 );
55755 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
55756 /* We could allocate a fresh RowSetEntry each time one is needed, but it
55757 ** is more efficient to pull a preallocated entry from the pool */
55758 struct RowSetChunk *pNew;
55759 pNew = sqlite3DbMallocRawNN(db: p->db, n: sizeof(*pNew));
55760 if( pNew==0 ){
55761 return 0;
55762 }
55763 pNew->pNextChunk = p->pChunk;
55764 p->pChunk = pNew;
55765 p->pFresh = pNew->aEntry;
55766 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
55767 }
55768 p->nFresh--;
55769 return p->pFresh++;
55770}
55771
55772/*
55773** Insert a new value into a RowSet.
55774**
55775** The mallocFailed flag of the database connection is set if a
55776** memory allocation fails.
55777*/
55778SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
55779 struct RowSetEntry *pEntry; /* The new entry */
55780 struct RowSetEntry *pLast; /* The last prior entry */
55781
55782 /* This routine is never called after sqlite3RowSetNext() */
55783 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
55784
55785 pEntry = rowSetEntryAlloc(p);
55786 if( pEntry==0 ) return;
55787 pEntry->v = rowid;
55788 pEntry->pRight = 0;
55789 pLast = p->pLast;
55790 if( pLast ){
55791 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
55792 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
55793 ** where possible */
55794 p->rsFlags &= ~ROWSET_SORTED;
55795 }
55796 pLast->pRight = pEntry;
55797 }else{
55798 p->pEntry = pEntry;
55799 }
55800 p->pLast = pEntry;
55801}
55802
55803/*
55804** Merge two lists of RowSetEntry objects. Remove duplicates.
55805**
55806** The input lists are connected via pRight pointers and are
55807** assumed to each already be in sorted order.
55808*/
55809static struct RowSetEntry *rowSetEntryMerge(
55810 struct RowSetEntry *pA, /* First sorted list to be merged */
55811 struct RowSetEntry *pB /* Second sorted list to be merged */
55812){
55813 struct RowSetEntry head;
55814 struct RowSetEntry *pTail;
55815
55816 pTail = &head;
55817 assert( pA!=0 && pB!=0 );
55818 for(;;){
55819 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
55820 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
55821 if( pA->v<=pB->v ){
55822 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
55823 pA = pA->pRight;
55824 if( pA==0 ){
55825 pTail->pRight = pB;
55826 break;
55827 }
55828 }else{
55829 pTail = pTail->pRight = pB;
55830 pB = pB->pRight;
55831 if( pB==0 ){
55832 pTail->pRight = pA;
55833 break;
55834 }
55835 }
55836 }
55837 return head.pRight;
55838}
55839
55840/*
55841** Sort all elements on the list of RowSetEntry objects into order of
55842** increasing v.
55843*/
55844static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
55845 unsigned int i;
55846 struct RowSetEntry *pNext, *aBucket[40];
55847
55848 memset(s: aBucket, c: 0, n: sizeof(aBucket));
55849 while( pIn ){
55850 pNext = pIn->pRight;
55851 pIn->pRight = 0;
55852 for(i=0; aBucket[i]; i++){
55853 pIn = rowSetEntryMerge(pA: aBucket[i], pB: pIn);
55854 aBucket[i] = 0;
55855 }
55856 aBucket[i] = pIn;
55857 pIn = pNext;
55858 }
55859 pIn = aBucket[0];
55860 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
55861 if( aBucket[i]==0 ) continue;
55862 pIn = pIn ? rowSetEntryMerge(pA: pIn, pB: aBucket[i]) : aBucket[i];
55863 }
55864 return pIn;
55865}
55866
55867
55868/*
55869** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
55870** Convert this tree into a linked list connected by the pRight pointers
55871** and return pointers to the first and last elements of the new list.
55872*/
55873static void rowSetTreeToList(
55874 struct RowSetEntry *pIn, /* Root of the input tree */
55875 struct RowSetEntry **ppFirst, /* Write head of the output list here */
55876 struct RowSetEntry **ppLast /* Write tail of the output list here */
55877){
55878 assert( pIn!=0 );
55879 if( pIn->pLeft ){
55880 struct RowSetEntry *p;
55881 rowSetTreeToList(pIn: pIn->pLeft, ppFirst, ppLast: &p);
55882 p->pRight = pIn;
55883 }else{
55884 *ppFirst = pIn;
55885 }
55886 if( pIn->pRight ){
55887 rowSetTreeToList(pIn: pIn->pRight, ppFirst: &pIn->pRight, ppLast);
55888 }else{
55889 *ppLast = pIn;
55890 }
55891 assert( (*ppLast)->pRight==0 );
55892}
55893
55894
55895/*
55896** Convert a sorted list of elements (connected by pRight) into a binary
55897** tree with depth of iDepth. A depth of 1 means the tree contains a single
55898** node taken from the head of *ppList. A depth of 2 means a tree with
55899** three nodes. And so forth.
55900**
55901** Use as many entries from the input list as required and update the
55902** *ppList to point to the unused elements of the list. If the input
55903** list contains too few elements, then construct an incomplete tree
55904** and leave *ppList set to NULL.
55905**
55906** Return a pointer to the root of the constructed binary tree.
55907*/
55908static struct RowSetEntry *rowSetNDeepTree(
55909 struct RowSetEntry **ppList,
55910 int iDepth
55911){
55912 struct RowSetEntry *p; /* Root of the new tree */
55913 struct RowSetEntry *pLeft; /* Left subtree */
55914 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
55915 /* Prevent unnecessary deep recursion when we run out of entries */
55916 return 0;
55917 }
55918 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
55919 /* This branch causes a *balanced* tree to be generated. A valid tree
55920 ** is still generated without this branch, but the tree is wildly
55921 ** unbalanced and inefficient. */
55922 pLeft = rowSetNDeepTree(ppList, iDepth: iDepth-1);
55923 p = *ppList;
55924 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
55925 /* It is safe to always return here, but the resulting tree
55926 ** would be unbalanced */
55927 return pLeft;
55928 }
55929 p->pLeft = pLeft;
55930 *ppList = p->pRight;
55931 p->pRight = rowSetNDeepTree(ppList, iDepth: iDepth-1);
55932 }else{
55933 p = *ppList;
55934 *ppList = p->pRight;
55935 p->pLeft = p->pRight = 0;
55936 }
55937 return p;
55938}
55939
55940/*
55941** Convert a sorted list of elements into a binary tree. Make the tree
55942** as deep as it needs to be in order to contain the entire list.
55943*/
55944static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
55945 int iDepth; /* Depth of the tree so far */
55946 struct RowSetEntry *p; /* Current tree root */
55947 struct RowSetEntry *pLeft; /* Left subtree */
55948
55949 assert( pList!=0 );
55950 p = pList;
55951 pList = p->pRight;
55952 p->pLeft = p->pRight = 0;
55953 for(iDepth=1; pList; iDepth++){
55954 pLeft = p;
55955 p = pList;
55956 pList = p->pRight;
55957 p->pLeft = pLeft;
55958 p->pRight = rowSetNDeepTree(ppList: &pList, iDepth);
55959 }
55960 return p;
55961}
55962
55963/*
55964** Extract the smallest element from the RowSet.
55965** Write the element into *pRowid. Return 1 on success. Return
55966** 0 if the RowSet is already empty.
55967**
55968** After this routine has been called, the sqlite3RowSetInsert()
55969** routine may not be called again.
55970**
55971** This routine may not be called after sqlite3RowSetTest() has
55972** been used. Older versions of RowSet allowed that, but as the
55973** capability was not used by the code generator, it was removed
55974** for code economy.
55975*/
55976SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
55977 assert( p!=0 );
55978 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
55979
55980 /* Merge the forest into a single sorted list on first call */
55981 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55982 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55983 p->pEntry = rowSetEntrySort(pIn: p->pEntry);
55984 }
55985 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
55986 }
55987
55988 /* Return the next entry on the list */
55989 if( p->pEntry ){
55990 *pRowid = p->pEntry->v;
55991 p->pEntry = p->pEntry->pRight;
55992 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
55993 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
55994 sqlite3RowSetClear(pArg: p);
55995 }
55996 return 1;
55997 }else{
55998 return 0;
55999 }
56000}
56001
56002/*
56003** Check to see if element iRowid was inserted into the rowset as
56004** part of any insert batch prior to iBatch. Return 1 or 0.
56005**
56006** If this is the first test of a new batch and if there exist entries
56007** on pRowSet->pEntry, then sort those entries into the forest at
56008** pRowSet->pForest so that they can be tested.
56009*/
56010SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
56011 struct RowSetEntry *p, *pTree;
56012
56013 /* This routine is never called after sqlite3RowSetNext() */
56014 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
56015
56016 /* Sort entries into the forest on the first test of a new batch.
56017 ** To save unnecessary work, only do this when the batch number changes.
56018 */
56019 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
56020 p = pRowSet->pEntry;
56021 if( p ){
56022 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
56023 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56024 /* Only sort the current set of entries if they need it */
56025 p = rowSetEntrySort(pIn: p);
56026 }
56027 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56028 ppPrevTree = &pTree->pRight;
56029 if( pTree->pLeft==0 ){
56030 pTree->pLeft = rowSetListToTree(pList: p);
56031 break;
56032 }else{
56033 struct RowSetEntry *pAux, *pTail;
56034 rowSetTreeToList(pIn: pTree->pLeft, ppFirst: &pAux, ppLast: &pTail);
56035 pTree->pLeft = 0;
56036 p = rowSetEntryMerge(pA: pAux, pB: p);
56037 }
56038 }
56039 if( pTree==0 ){
56040 *ppPrevTree = pTree = rowSetEntryAlloc(p: pRowSet);
56041 if( pTree ){
56042 pTree->v = 0;
56043 pTree->pRight = 0;
56044 pTree->pLeft = rowSetListToTree(pList: p);
56045 }
56046 }
56047 pRowSet->pEntry = 0;
56048 pRowSet->pLast = 0;
56049 pRowSet->rsFlags |= ROWSET_SORTED;
56050 }
56051 pRowSet->iBatch = iBatch;
56052 }
56053
56054 /* Test to see if the iRowid value appears anywhere in the forest.
56055 ** Return 1 if it does and 0 if not.
56056 */
56057 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56058 p = pTree->pLeft;
56059 while( p ){
56060 if( p->v<iRowid ){
56061 p = p->pRight;
56062 }else if( p->v>iRowid ){
56063 p = p->pLeft;
56064 }else{
56065 return 1;
56066 }
56067 }
56068 }
56069 return 0;
56070}
56071
56072/************** End of rowset.c **********************************************/
56073/************** Begin file pager.c *******************************************/
56074/*
56075** 2001 September 15
56076**
56077** The author disclaims copyright to this source code. In place of
56078** a legal notice, here is a blessing:
56079**
56080** May you do good and not evil.
56081** May you find forgiveness for yourself and forgive others.
56082** May you share freely, never taking more than you give.
56083**
56084*************************************************************************
56085** This is the implementation of the page cache subsystem or "pager".
56086**
56087** The pager is used to access a database disk file. It implements
56088** atomic commit and rollback through the use of a journal file that
56089** is separate from the database file. The pager also implements file
56090** locking to prevent two processes from writing the same database
56091** file simultaneously, or one process from reading the database while
56092** another is writing.
56093*/
56094#ifndef SQLITE_OMIT_DISKIO
56095/* #include "sqliteInt.h" */
56096/************** Include wal.h in the middle of pager.c ***********************/
56097/************** Begin file wal.h *********************************************/
56098/*
56099** 2010 February 1
56100**
56101** The author disclaims copyright to this source code. In place of
56102** a legal notice, here is a blessing:
56103**
56104** May you do good and not evil.
56105** May you find forgiveness for yourself and forgive others.
56106** May you share freely, never taking more than you give.
56107**
56108*************************************************************************
56109** This header file defines the interface to the write-ahead logging
56110** system. Refer to the comments below and the header comment attached to
56111** the implementation of each function in log.c for further details.
56112*/
56113
56114#ifndef SQLITE_WAL_H
56115#define SQLITE_WAL_H
56116
56117/* #include "sqliteInt.h" */
56118
56119/* Macros for extracting appropriate sync flags for either transaction
56120** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
56121*/
56122#define WAL_SYNC_FLAGS(X) ((X)&0x03)
56123#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03)
56124
56125#ifdef SQLITE_OMIT_WAL
56126# define sqlite3WalOpen(x,y,z) 0
56127# define sqlite3WalLimit(x,y)
56128# define sqlite3WalClose(v,w,x,y,z) 0
56129# define sqlite3WalBeginReadTransaction(y,z) 0
56130# define sqlite3WalEndReadTransaction(z)
56131# define sqlite3WalDbsize(y) 0
56132# define sqlite3WalBeginWriteTransaction(y) 0
56133# define sqlite3WalEndWriteTransaction(x) 0
56134# define sqlite3WalUndo(x,y,z) 0
56135# define sqlite3WalSavepoint(y,z)
56136# define sqlite3WalSavepointUndo(y,z) 0
56137# define sqlite3WalFrames(u,v,w,x,y,z) 0
56138# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
56139# define sqlite3WalCallback(z) 0
56140# define sqlite3WalExclusiveMode(y,z) 0
56141# define sqlite3WalHeapMemory(z) 0
56142# define sqlite3WalFramesize(z) 0
56143# define sqlite3WalFindFrame(x,y,z) 0
56144# define sqlite3WalFile(x) 0
56145# undef SQLITE_USE_SEH
56146#else
56147
56148#define WAL_SAVEPOINT_NDATA 4
56149
56150/* Connection to a write-ahead log (WAL) file.
56151** There is one object of this type for each pager.
56152*/
56153typedef struct Wal Wal;
56154
56155/* Open and close a connection to a write-ahead log. */
56156SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
56157SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
56158
56159/* Set the limiting size of a WAL file. */
56160SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
56161
56162/* Used by readers to open (lock) and close (unlock) a snapshot. A
56163** snapshot is like a read-transaction. It is the state of the database
56164** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
56165** preserves the current state even if the other threads or processes
56166** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
56167** transaction and releases the lock.
56168*/
56169SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
56170SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
56171
56172/* Read a page from the write-ahead log, if it is present. */
56173SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
56174SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
56175
56176/* If the WAL is not empty, return the size of the database. */
56177SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
56178
56179/* Obtain or release the WRITER lock. */
56180SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
56181SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
56182
56183/* Undo any frames written (but not committed) to the log */
56184SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
56185
56186/* Return an integer that records the current (uncommitted) write
56187** position in the WAL */
56188SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
56189
56190/* Move the write position of the WAL back to iFrame. Called in
56191** response to a ROLLBACK TO command. */
56192SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
56193
56194/* Write a frame or frames to the log. */
56195SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
56196
56197/* Copy pages from the log to the database file */
56198SQLITE_PRIVATE int sqlite3WalCheckpoint(
56199 Wal *pWal, /* Write-ahead log connection */
56200 sqlite3 *db, /* Check this handle's interrupt flag */
56201 int eMode, /* One of PASSIVE, FULL and RESTART */
56202 int (*xBusy)(void*), /* Function to call when busy */
56203 void *pBusyArg, /* Context argument for xBusyHandler */
56204 int sync_flags, /* Flags to sync db file with (or 0) */
56205 int nBuf, /* Size of buffer nBuf */
56206 u8 *zBuf, /* Temporary buffer to use */
56207 int *pnLog, /* OUT: Number of frames in WAL */
56208 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
56209);
56210
56211/* Return the value to pass to a sqlite3_wal_hook callback, the
56212** number of frames in the WAL at the point of the last commit since
56213** sqlite3WalCallback() was called. If no commits have occurred since
56214** the last call, then return 0.
56215*/
56216SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
56217
56218/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
56219** by the pager layer on the database file.
56220*/
56221SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
56222
56223/* Return true if the argument is non-NULL and the WAL module is using
56224** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
56225** WAL module is using shared-memory, return false.
56226*/
56227SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
56228
56229#ifdef SQLITE_ENABLE_SNAPSHOT
56230SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
56231SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
56232SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
56233SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
56234SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
56235#endif
56236
56237#ifdef SQLITE_ENABLE_ZIPVFS
56238/* If the WAL file is not empty, return the number of bytes of content
56239** stored in each frame (i.e. the db page-size when the WAL was created).
56240*/
56241SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
56242#endif
56243
56244/* Return the sqlite3_file object for the WAL file */
56245SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
56246
56247#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
56248SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock);
56249SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db);
56250#endif
56251
56252#ifdef SQLITE_USE_SEH
56253SQLITE_PRIVATE int sqlite3WalSystemErrno(Wal*);
56254#endif
56255
56256#endif /* ifndef SQLITE_OMIT_WAL */
56257#endif /* SQLITE_WAL_H */
56258
56259/************** End of wal.h *************************************************/
56260/************** Continuing where we left off in pager.c **********************/
56261
56262
56263/******************* NOTES ON THE DESIGN OF THE PAGER ************************
56264**
56265** This comment block describes invariants that hold when using a rollback
56266** journal. These invariants do not apply for journal_mode=WAL,
56267** journal_mode=MEMORY, or journal_mode=OFF.
56268**
56269** Within this comment block, a page is deemed to have been synced
56270** automatically as soon as it is written when PRAGMA synchronous=OFF.
56271** Otherwise, the page is not synced until the xSync method of the VFS
56272** is called successfully on the file containing the page.
56273**
56274** Definition: A page of the database file is said to be "overwriteable" if
56275** one or more of the following are true about the page:
56276**
56277** (a) The original content of the page as it was at the beginning of
56278** the transaction has been written into the rollback journal and
56279** synced.
56280**
56281** (b) The page was a freelist leaf page at the start of the transaction.
56282**
56283** (c) The page number is greater than the largest page that existed in
56284** the database file at the start of the transaction.
56285**
56286** (1) A page of the database file is never overwritten unless one of the
56287** following are true:
56288**
56289** (a) The page and all other pages on the same sector are overwriteable.
56290**
56291** (b) The atomic page write optimization is enabled, and the entire
56292** transaction other than the update of the transaction sequence
56293** number consists of a single page change.
56294**
56295** (2) The content of a page written into the rollback journal exactly matches
56296** both the content in the database when the rollback journal was written
56297** and the content in the database at the beginning of the current
56298** transaction.
56299**
56300** (3) Writes to the database file are an integer multiple of the page size
56301** in length and are aligned on a page boundary.
56302**
56303** (4) Reads from the database file are either aligned on a page boundary and
56304** an integer multiple of the page size in length or are taken from the
56305** first 100 bytes of the database file.
56306**
56307** (5) All writes to the database file are synced prior to the rollback journal
56308** being deleted, truncated, or zeroed.
56309**
56310** (6) If a super-journal file is used, then all writes to the database file
56311** are synced prior to the super-journal being deleted.
56312**
56313** Definition: Two databases (or the same database at two points it time)
56314** are said to be "logically equivalent" if they give the same answer to
56315** all queries. Note in particular the content of freelist leaf
56316** pages can be changed arbitrarily without affecting the logical equivalence
56317** of the database.
56318**
56319** (7) At any time, if any subset, including the empty set and the total set,
56320** of the unsynced changes to a rollback journal are removed and the
56321** journal is rolled back, the resulting database file will be logically
56322** equivalent to the database file at the beginning of the transaction.
56323**
56324** (8) When a transaction is rolled back, the xTruncate method of the VFS
56325** is called to restore the database file to the same size it was at
56326** the beginning of the transaction. (In some VFSes, the xTruncate
56327** method is a no-op, but that does not change the fact the SQLite will
56328** invoke it.)
56329**
56330** (9) Whenever the database file is modified, at least one bit in the range
56331** of bytes from 24 through 39 inclusive will be changed prior to releasing
56332** the EXCLUSIVE lock, thus signaling other connections on the same
56333** database to flush their caches.
56334**
56335** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
56336** than one billion transactions.
56337**
56338** (11) A database file is well-formed at the beginning and at the conclusion
56339** of every transaction.
56340**
56341** (12) An EXCLUSIVE lock is held on the database file when writing to
56342** the database file.
56343**
56344** (13) A SHARED lock is held on the database file while reading any
56345** content out of the database file.
56346**
56347******************************************************************************/
56348
56349/*
56350** Macros for troubleshooting. Normally turned off
56351*/
56352#if 0
56353int sqlite3PagerTrace=1; /* True to enable tracing */
56354#define sqlite3DebugPrintf printf
56355#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
56356#else
56357#define PAGERTRACE(X)
56358#endif
56359
56360/*
56361** The following two macros are used within the PAGERTRACE() macros above
56362** to print out file-descriptors.
56363**
56364** PAGERID() takes a pointer to a Pager struct as its argument. The
56365** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
56366** struct as its argument.
56367*/
56368#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
56369#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
56370
56371/*
56372** The Pager.eState variable stores the current 'state' of a pager. A
56373** pager may be in any one of the seven states shown in the following
56374** state diagram.
56375**
56376** OPEN <------+------+
56377** | | |
56378** V | |
56379** +---------> READER-------+ |
56380** | | |
56381** | V |
56382** |<-------WRITER_LOCKED------> ERROR
56383** | | ^
56384** | V |
56385** |<------WRITER_CACHEMOD-------->|
56386** | | |
56387** | V |
56388** |<-------WRITER_DBMOD---------->|
56389** | | |
56390** | V |
56391** +<------WRITER_FINISHED-------->+
56392**
56393**
56394** List of state transitions and the C [function] that performs each:
56395**
56396** OPEN -> READER [sqlite3PagerSharedLock]
56397** READER -> OPEN [pager_unlock]
56398**
56399** READER -> WRITER_LOCKED [sqlite3PagerBegin]
56400** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
56401** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
56402** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
56403** WRITER_*** -> READER [pager_end_transaction]
56404**
56405** WRITER_*** -> ERROR [pager_error]
56406** ERROR -> OPEN [pager_unlock]
56407**
56408**
56409** OPEN:
56410**
56411** The pager starts up in this state. Nothing is guaranteed in this
56412** state - the file may or may not be locked and the database size is
56413** unknown. The database may not be read or written.
56414**
56415** * No read or write transaction is active.
56416** * Any lock, or no lock at all, may be held on the database file.
56417** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
56418**
56419** READER:
56420**
56421** In this state all the requirements for reading the database in
56422** rollback (non-WAL) mode are met. Unless the pager is (or recently
56423** was) in exclusive-locking mode, a user-level read transaction is
56424** open. The database size is known in this state.
56425**
56426** A connection running with locking_mode=normal enters this state when
56427** it opens a read-transaction on the database and returns to state
56428** OPEN after the read-transaction is completed. However a connection
56429** running in locking_mode=exclusive (including temp databases) remains in
56430** this state even after the read-transaction is closed. The only way
56431** a locking_mode=exclusive connection can transition from READER to OPEN
56432** is via the ERROR state (see below).
56433**
56434** * A read transaction may be active (but a write-transaction cannot).
56435** * A SHARED or greater lock is held on the database file.
56436** * The dbSize variable may be trusted (even if a user-level read
56437** transaction is not active). The dbOrigSize and dbFileSize variables
56438** may not be trusted at this point.
56439** * If the database is a WAL database, then the WAL connection is open.
56440** * Even if a read-transaction is not open, it is guaranteed that
56441** there is no hot-journal in the file-system.
56442**
56443** WRITER_LOCKED:
56444**
56445** The pager moves to this state from READER when a write-transaction
56446** is first opened on the database. In WRITER_LOCKED state, all locks
56447** required to start a write-transaction are held, but no actual
56448** modifications to the cache or database have taken place.
56449**
56450** In rollback mode, a RESERVED or (if the transaction was opened with
56451** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
56452** moving to this state, but the journal file is not written to or opened
56453** to in this state. If the transaction is committed or rolled back while
56454** in WRITER_LOCKED state, all that is required is to unlock the database
56455** file.
56456**
56457** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
56458** If the connection is running with locking_mode=exclusive, an attempt
56459** is made to obtain an EXCLUSIVE lock on the database file.
56460**
56461** * A write transaction is active.
56462** * If the connection is open in rollback-mode, a RESERVED or greater
56463** lock is held on the database file.
56464** * If the connection is open in WAL-mode, a WAL write transaction
56465** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
56466** called).
56467** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
56468** * The contents of the pager cache have not been modified.
56469** * The journal file may or may not be open.
56470** * Nothing (not even the first header) has been written to the journal.
56471**
56472** WRITER_CACHEMOD:
56473**
56474** A pager moves from WRITER_LOCKED state to this state when a page is
56475** first modified by the upper layer. In rollback mode the journal file
56476** is opened (if it is not already open) and a header written to the
56477** start of it. The database file on disk has not been modified.
56478**
56479** * A write transaction is active.
56480** * A RESERVED or greater lock is held on the database file.
56481** * The journal file is open and the first header has been written
56482** to it, but the header has not been synced to disk.
56483** * The contents of the page cache have been modified.
56484**
56485** WRITER_DBMOD:
56486**
56487** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
56488** when it modifies the contents of the database file. WAL connections
56489** never enter this state (since they do not modify the database file,
56490** just the log file).
56491**
56492** * A write transaction is active.
56493** * An EXCLUSIVE or greater lock is held on the database file.
56494** * The journal file is open and the first header has been written
56495** and synced to disk.
56496** * The contents of the page cache have been modified (and possibly
56497** written to disk).
56498**
56499** WRITER_FINISHED:
56500**
56501** It is not possible for a WAL connection to enter this state.
56502**
56503** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
56504** state after the entire transaction has been successfully written into the
56505** database file. In this state the transaction may be committed simply
56506** by finalizing the journal file. Once in WRITER_FINISHED state, it is
56507** not possible to modify the database further. At this point, the upper
56508** layer must either commit or rollback the transaction.
56509**
56510** * A write transaction is active.
56511** * An EXCLUSIVE or greater lock is held on the database file.
56512** * All writing and syncing of journal and database data has finished.
56513** If no error occurred, all that remains is to finalize the journal to
56514** commit the transaction. If an error did occur, the caller will need
56515** to rollback the transaction.
56516**
56517** ERROR:
56518**
56519** The ERROR state is entered when an IO or disk-full error (including
56520** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
56521** difficult to be sure that the in-memory pager state (cache contents,
56522** db size etc.) are consistent with the contents of the file-system.
56523**
56524** Temporary pager files may enter the ERROR state, but in-memory pagers
56525** cannot.
56526**
56527** For example, if an IO error occurs while performing a rollback,
56528** the contents of the page-cache may be left in an inconsistent state.
56529** At this point it would be dangerous to change back to READER state
56530** (as usually happens after a rollback). Any subsequent readers might
56531** report database corruption (due to the inconsistent cache), and if
56532** they upgrade to writers, they may inadvertently corrupt the database
56533** file. To avoid this hazard, the pager switches into the ERROR state
56534** instead of READER following such an error.
56535**
56536** Once it has entered the ERROR state, any attempt to use the pager
56537** to read or write data returns an error. Eventually, once all
56538** outstanding transactions have been abandoned, the pager is able to
56539** transition back to OPEN state, discarding the contents of the
56540** page-cache and any other in-memory state at the same time. Everything
56541** is reloaded from disk (and, if necessary, hot-journal rollback performed)
56542** when a read-transaction is next opened on the pager (transitioning
56543** the pager into READER state). At that point the system has recovered
56544** from the error.
56545**
56546** Specifically, the pager jumps into the ERROR state if:
56547**
56548** 1. An error occurs while attempting a rollback. This happens in
56549** function sqlite3PagerRollback().
56550**
56551** 2. An error occurs while attempting to finalize a journal file
56552** following a commit in function sqlite3PagerCommitPhaseTwo().
56553**
56554** 3. An error occurs while attempting to write to the journal or
56555** database file in function pagerStress() in order to free up
56556** memory.
56557**
56558** In other cases, the error is returned to the b-tree layer. The b-tree
56559** layer then attempts a rollback operation. If the error condition
56560** persists, the pager enters the ERROR state via condition (1) above.
56561**
56562** Condition (3) is necessary because it can be triggered by a read-only
56563** statement executed within a transaction. In this case, if the error
56564** code were simply returned to the user, the b-tree layer would not
56565** automatically attempt a rollback, as it assumes that an error in a
56566** read-only statement cannot leave the pager in an internally inconsistent
56567** state.
56568**
56569** * The Pager.errCode variable is set to something other than SQLITE_OK.
56570** * There are one or more outstanding references to pages (after the
56571** last reference is dropped the pager should move back to OPEN state).
56572** * The pager is not an in-memory pager.
56573**
56574**
56575** Notes:
56576**
56577** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
56578** connection is open in WAL mode. A WAL connection is always in one
56579** of the first four states.
56580**
56581** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
56582** state. There are two exceptions: immediately after exclusive-mode has
56583** been turned on (and before any read or write transactions are
56584** executed), and when the pager is leaving the "error state".
56585**
56586** * See also: assert_pager_state().
56587*/
56588#define PAGER_OPEN 0
56589#define PAGER_READER 1
56590#define PAGER_WRITER_LOCKED 2
56591#define PAGER_WRITER_CACHEMOD 3
56592#define PAGER_WRITER_DBMOD 4
56593#define PAGER_WRITER_FINISHED 5
56594#define PAGER_ERROR 6
56595
56596/*
56597** The Pager.eLock variable is almost always set to one of the
56598** following locking-states, according to the lock currently held on
56599** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
56600** This variable is kept up to date as locks are taken and released by
56601** the pagerLockDb() and pagerUnlockDb() wrappers.
56602**
56603** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
56604** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
56605** the operation was successful. In these circumstances pagerLockDb() and
56606** pagerUnlockDb() take a conservative approach - eLock is always updated
56607** when unlocking the file, and only updated when locking the file if the
56608** VFS call is successful. This way, the Pager.eLock variable may be set
56609** to a less exclusive (lower) value than the lock that is actually held
56610** at the system level, but it is never set to a more exclusive value.
56611**
56612** This is usually safe. If an xUnlock fails or appears to fail, there may
56613** be a few redundant xLock() calls or a lock may be held for longer than
56614** required, but nothing really goes wrong.
56615**
56616** The exception is when the database file is unlocked as the pager moves
56617** from ERROR to OPEN state. At this point there may be a hot-journal file
56618** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
56619** transition, by the same pager or any other). If the call to xUnlock()
56620** fails at this point and the pager is left holding an EXCLUSIVE lock, this
56621** can confuse the call to xCheckReservedLock() call made later as part
56622** of hot-journal detection.
56623**
56624** xCheckReservedLock() is defined as returning true "if there is a RESERVED
56625** lock held by this process or any others". So xCheckReservedLock may
56626** return true because the caller itself is holding an EXCLUSIVE lock (but
56627** doesn't know it because of a previous error in xUnlock). If this happens
56628** a hot-journal may be mistaken for a journal being created by an active
56629** transaction in another process, causing SQLite to read from the database
56630** without rolling it back.
56631**
56632** To work around this, if a call to xUnlock() fails when unlocking the
56633** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
56634** is only changed back to a real locking state after a successful call
56635** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
56636** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
56637** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
56638** lock on the database file before attempting to roll it back. See function
56639** PagerSharedLock() for more detail.
56640**
56641** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
56642** PAGER_OPEN state.
56643*/
56644#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
56645
56646/*
56647** The maximum allowed sector size. 64KiB. If the xSectorsize() method
56648** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
56649** This could conceivably cause corruption following a power failure on
56650** such a system. This is currently an undocumented limit.
56651*/
56652#define MAX_SECTOR_SIZE 0x10000
56653
56654
56655/*
56656** An instance of the following structure is allocated for each active
56657** savepoint and statement transaction in the system. All such structures
56658** are stored in the Pager.aSavepoint[] array, which is allocated and
56659** resized using sqlite3Realloc().
56660**
56661** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
56662** set to 0. If a journal-header is written into the main journal while
56663** the savepoint is active, then iHdrOffset is set to the byte offset
56664** immediately following the last journal record written into the main
56665** journal before the journal-header. This is required during savepoint
56666** rollback (see pagerPlaybackSavepoint()).
56667*/
56668typedef struct PagerSavepoint PagerSavepoint;
56669struct PagerSavepoint {
56670 i64 iOffset; /* Starting offset in main journal */
56671 i64 iHdrOffset; /* See above */
56672 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
56673 Pgno nOrig; /* Original number of pages in file */
56674 Pgno iSubRec; /* Index of first record in sub-journal */
56675 int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */
56676#ifndef SQLITE_OMIT_WAL
56677 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
56678#endif
56679};
56680
56681/*
56682** Bits of the Pager.doNotSpill flag. See further description below.
56683*/
56684#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
56685#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
56686#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
56687
56688/*
56689** An open page cache is an instance of struct Pager. A description of
56690** some of the more important member variables follows:
56691**
56692** eState
56693**
56694** The current 'state' of the pager object. See the comment and state
56695** diagram above for a description of the pager state.
56696**
56697** eLock
56698**
56699** For a real on-disk database, the current lock held on the database file -
56700** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
56701**
56702** For a temporary or in-memory database (neither of which require any
56703** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
56704** databases always have Pager.exclusiveMode==1, this tricks the pager
56705** logic into thinking that it already has all the locks it will ever
56706** need (and no reason to release them).
56707**
56708** In some (obscure) circumstances, this variable may also be set to
56709** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
56710** details.
56711**
56712** changeCountDone
56713**
56714** This boolean variable is used to make sure that the change-counter
56715** (the 4-byte header field at byte offset 24 of the database file) is
56716** not updated more often than necessary.
56717**
56718** It is set to true when the change-counter field is updated, which
56719** can only happen if an exclusive lock is held on the database file.
56720** It is cleared (set to false) whenever an exclusive lock is
56721** relinquished on the database file. Each time a transaction is committed,
56722** The changeCountDone flag is inspected. If it is true, the work of
56723** updating the change-counter is omitted for the current transaction.
56724**
56725** This mechanism means that when running in exclusive mode, a connection
56726** need only update the change-counter once, for the first transaction
56727** committed.
56728**
56729** setSuper
56730**
56731** When PagerCommitPhaseOne() is called to commit a transaction, it may
56732** (or may not) specify a super-journal name to be written into the
56733** journal file before it is synced to disk.
56734**
56735** Whether or not a journal file contains a super-journal pointer affects
56736** the way in which the journal file is finalized after the transaction is
56737** committed or rolled back when running in "journal_mode=PERSIST" mode.
56738** If a journal file does not contain a super-journal pointer, it is
56739** finalized by overwriting the first journal header with zeroes. If
56740** it does contain a super-journal pointer the journal file is finalized
56741** by truncating it to zero bytes, just as if the connection were
56742** running in "journal_mode=truncate" mode.
56743**
56744** Journal files that contain super-journal pointers cannot be finalized
56745** simply by overwriting the first journal-header with zeroes, as the
56746** super-journal pointer could interfere with hot-journal rollback of any
56747** subsequently interrupted transaction that reuses the journal file.
56748**
56749** The flag is cleared as soon as the journal file is finalized (either
56750** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
56751** journal file from being successfully finalized, the setSuper flag
56752** is cleared anyway (and the pager will move to ERROR state).
56753**
56754** doNotSpill
56755**
56756** This variables control the behavior of cache-spills (calls made by
56757** the pcache module to the pagerStress() routine to write cached data
56758** to the file-system in order to free up memory).
56759**
56760** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
56761** writing to the database from pagerStress() is disabled altogether.
56762** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
56763** comes up during savepoint rollback that requires the pcache module
56764** to allocate a new page to prevent the journal file from being written
56765** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
56766** case is a user preference.
56767**
56768** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
56769** pagerStress() is permitted, but syncing the journal file is not.
56770** This flag is set by sqlite3PagerWrite() when the file-system sector-size
56771** is larger than the database page-size in order to prevent a journal sync
56772** from happening in between the journalling of two pages on the same sector.
56773**
56774** subjInMemory
56775**
56776** This is a boolean variable. If true, then any required sub-journal
56777** is opened as an in-memory journal file. If false, then in-memory
56778** sub-journals are only used for in-memory pager files.
56779**
56780** This variable is updated by the upper layer each time a new
56781** write-transaction is opened.
56782**
56783** dbSize, dbOrigSize, dbFileSize
56784**
56785** Variable dbSize is set to the number of pages in the database file.
56786** It is valid in PAGER_READER and higher states (all states except for
56787** OPEN and ERROR).
56788**
56789** dbSize is set based on the size of the database file, which may be
56790** larger than the size of the database (the value stored at offset
56791** 28 of the database header by the btree). If the size of the file
56792** is not an integer multiple of the page-size, the value stored in
56793** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
56794** Except, any file that is greater than 0 bytes in size is considered
56795** to have at least one page. (i.e. a 1KB file with 2K page-size leads
56796** to dbSize==1).
56797**
56798** During a write-transaction, if pages with page-numbers greater than
56799** dbSize are modified in the cache, dbSize is updated accordingly.
56800** Similarly, if the database is truncated using PagerTruncateImage(),
56801** dbSize is updated.
56802**
56803** Variables dbOrigSize and dbFileSize are valid in states
56804** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
56805** variable at the start of the transaction. It is used during rollback,
56806** and to determine whether or not pages need to be journalled before
56807** being modified.
56808**
56809** Throughout a write-transaction, dbFileSize contains the size of
56810** the file on disk in pages. It is set to a copy of dbSize when the
56811** write-transaction is first opened, and updated when VFS calls are made
56812** to write or truncate the database file on disk.
56813**
56814** The only reason the dbFileSize variable is required is to suppress
56815** unnecessary calls to xTruncate() after committing a transaction. If,
56816** when a transaction is committed, the dbFileSize variable indicates
56817** that the database file is larger than the database image (Pager.dbSize),
56818** pager_truncate() is called. The pager_truncate() call uses xFilesize()
56819** to measure the database file on disk, and then truncates it if required.
56820** dbFileSize is not used when rolling back a transaction. In this case
56821** pager_truncate() is called unconditionally (which means there may be
56822** a call to xFilesize() that is not strictly required). In either case,
56823** pager_truncate() may cause the file to become smaller or larger.
56824**
56825** dbHintSize
56826**
56827** The dbHintSize variable is used to limit the number of calls made to
56828** the VFS xFileControl(FCNTL_SIZE_HINT) method.
56829**
56830** dbHintSize is set to a copy of the dbSize variable when a
56831** write-transaction is opened (at the same time as dbFileSize and
56832** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
56833** dbHintSize is increased to the number of pages that correspond to the
56834** size-hint passed to the method call. See pager_write_pagelist() for
56835** details.
56836**
56837** errCode
56838**
56839** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
56840** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
56841** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
56842** sub-codes.
56843**
56844** syncFlags, walSyncFlags
56845**
56846** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
56847** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
56848** and contains the flags used to sync the checkpoint operations in the
56849** lower two bits, and sync flags used for transaction commits in the WAL
56850** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
56851** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
56852** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
56853** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
56854** meaning that the 0x04 and 0x08 bits are both zero.
56855*/
56856struct Pager {
56857 sqlite3_vfs *pVfs; /* OS functions to use for IO */
56858 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
56859 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
56860 u8 useJournal; /* Use a rollback journal on this file */
56861 u8 noSync; /* Do not sync the journal if true */
56862 u8 fullSync; /* Do extra syncs of the journal for robustness */
56863 u8 extraSync; /* sync directory after journal delete */
56864 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
56865 u8 walSyncFlags; /* See description above */
56866 u8 tempFile; /* zFilename is a temporary or immutable file */
56867 u8 noLock; /* Do not lock (except in WAL mode) */
56868 u8 readOnly; /* True for a read-only database */
56869 u8 memDb; /* True to inhibit all file I/O */
56870 u8 memVfs; /* VFS-implemented memory database */
56871
56872 /**************************************************************************
56873 ** The following block contains those class members that change during
56874 ** routine operation. Class members not in this block are either fixed
56875 ** when the pager is first created or else only change when there is a
56876 ** significant mode change (such as changing the page_size, locking_mode,
56877 ** or the journal_mode). From another view, these class members describe
56878 ** the "state" of the pager, while other class members describe the
56879 ** "configuration" of the pager.
56880 */
56881 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
56882 u8 eLock; /* Current lock held on database file */
56883 u8 changeCountDone; /* Set after incrementing the change-counter */
56884 u8 setSuper; /* Super-jrnl name is written into jrnl */
56885 u8 doNotSpill; /* Do not spill the cache when non-zero */
56886 u8 subjInMemory; /* True to use in-memory sub-journals */
56887 u8 bUseFetch; /* True to use xFetch() */
56888 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
56889 Pgno dbSize; /* Number of pages in the database */
56890 Pgno dbOrigSize; /* dbSize before the current transaction */
56891 Pgno dbFileSize; /* Number of pages in the database file */
56892 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
56893 int errCode; /* One of several kinds of errors */
56894 int nRec; /* Pages journalled since last j-header written */
56895 u32 cksumInit; /* Quasi-random value added to every checksum */
56896 u32 nSubRec; /* Number of records written to sub-journal */
56897 Bitvec *pInJournal; /* One bit for each page in the database file */
56898 sqlite3_file *fd; /* File descriptor for database */
56899 sqlite3_file *jfd; /* File descriptor for main journal */
56900 sqlite3_file *sjfd; /* File descriptor for sub-journal */
56901 i64 journalOff; /* Current write offset in the journal file */
56902 i64 journalHdr; /* Byte offset to previous journal header */
56903 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
56904 PagerSavepoint *aSavepoint; /* Array of active savepoints */
56905 int nSavepoint; /* Number of elements in aSavepoint[] */
56906 u32 iDataVersion; /* Changes whenever database content changes */
56907 char dbFileVers[16]; /* Changes whenever database file changes */
56908
56909 int nMmapOut; /* Number of mmap pages currently outstanding */
56910 sqlite3_int64 szMmap; /* Desired maximum mmap size */
56911 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
56912 /*
56913 ** End of the routinely-changing class members
56914 ***************************************************************************/
56915
56916 u16 nExtra; /* Add this many bytes to each in-memory page */
56917 i16 nReserve; /* Number of unused bytes at end of each page */
56918 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
56919 u32 sectorSize; /* Assumed sector size during rollback */
56920 Pgno mxPgno; /* Maximum allowed size of the database */
56921 Pgno lckPgno; /* Page number for the locking page */
56922 i64 pageSize; /* Number of bytes in a page */
56923 i64 journalSizeLimit; /* Size limit for persistent journal files */
56924 char *zFilename; /* Name of the database file */
56925 char *zJournal; /* Name of the journal file */
56926 int (*xBusyHandler)(void*); /* Function to call when busy */
56927 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
56928 int aStat[4]; /* Total cache hits, misses, writes, spills */
56929#ifdef SQLITE_TEST
56930 int nRead; /* Database pages read */
56931#endif
56932 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
56933 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
56934 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
56935 PCache *pPCache; /* Pointer to page cache object */
56936#ifndef SQLITE_OMIT_WAL
56937 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
56938 char *zWal; /* File name for write-ahead log */
56939#endif
56940};
56941
56942/*
56943** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
56944** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
56945** or CACHE_WRITE to sqlite3_db_status().
56946*/
56947#define PAGER_STAT_HIT 0
56948#define PAGER_STAT_MISS 1
56949#define PAGER_STAT_WRITE 2
56950#define PAGER_STAT_SPILL 3
56951
56952/*
56953** The following global variables hold counters used for
56954** testing purposes only. These variables do not exist in
56955** a non-testing build. These variables are not thread-safe.
56956*/
56957#ifdef SQLITE_TEST
56958SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
56959SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
56960SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
56961# define PAGER_INCR(v) v++
56962#else
56963# define PAGER_INCR(v)
56964#endif
56965
56966
56967
56968/*
56969** Journal files begin with the following magic string. The data
56970** was obtained from /dev/random. It is used only as a sanity check.
56971**
56972** Since version 2.8.0, the journal format contains additional sanity
56973** checking information. If the power fails while the journal is being
56974** written, semi-random garbage data might appear in the journal
56975** file after power is restored. If an attempt is then made
56976** to roll the journal back, the database could be corrupted. The additional
56977** sanity checking data is an attempt to discover the garbage in the
56978** journal and ignore it.
56979**
56980** The sanity checking information for the new journal format consists
56981** of a 32-bit checksum on each page of data. The checksum covers both
56982** the page number and the pPager->pageSize bytes of data for the page.
56983** This cksum is initialized to a 32-bit random value that appears in the
56984** journal file right after the header. The random initializer is important,
56985** because garbage data that appears at the end of a journal is likely
56986** data that was once in other files that have now been deleted. If the
56987** garbage data came from an obsolete journal file, the checksums might
56988** be correct. But by initializing the checksum to random value which
56989** is different for every journal, we minimize that risk.
56990*/
56991static const unsigned char aJournalMagic[] = {
56992 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
56993};
56994
56995/*
56996** The size of the of each page record in the journal is given by
56997** the following macro.
56998*/
56999#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
57000
57001/*
57002** The journal header size for this pager. This is usually the same
57003** size as a single disk sector. See also setSectorSize().
57004*/
57005#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
57006
57007/*
57008** The macro MEMDB is true if we are dealing with an in-memory database.
57009** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
57010** the value of MEMDB will be a constant and the compiler will optimize
57011** out code that would never execute.
57012*/
57013#ifdef SQLITE_OMIT_MEMORYDB
57014# define MEMDB 0
57015#else
57016# define MEMDB pPager->memDb
57017#endif
57018
57019/*
57020** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
57021** interfaces to access the database using memory-mapped I/O.
57022*/
57023#if SQLITE_MAX_MMAP_SIZE>0
57024# define USEFETCH(x) ((x)->bUseFetch)
57025#else
57026# define USEFETCH(x) 0
57027#endif
57028
57029/*
57030** The argument to this macro is a file descriptor (type sqlite3_file*).
57031** Return 0 if it is not open, or non-zero (but not 1) if it is.
57032**
57033** This is so that expressions can be written as:
57034**
57035** if( isOpen(pPager->jfd) ){ ...
57036**
57037** instead of
57038**
57039** if( pPager->jfd->pMethods ){ ...
57040*/
57041#define isOpen(pFd) ((pFd)->pMethods!=0)
57042
57043#ifdef SQLITE_DIRECT_OVERFLOW_READ
57044/*
57045** Return true if page pgno can be read directly from the database file
57046** by the b-tree layer. This is the case if:
57047**
57048** * the database file is open,
57049** * there are no dirty pages in the cache, and
57050** * the desired page is not currently in the wal file.
57051*/
57052SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
57053 if( pPager->fd->pMethods==0 ) return 0;
57054 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
57055#ifndef SQLITE_OMIT_WAL
57056 if( pPager->pWal ){
57057 u32 iRead = 0;
57058 int rc;
57059 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
57060 return (rc==SQLITE_OK && iRead==0);
57061 }
57062#endif
57063 return 1;
57064}
57065#endif
57066
57067#ifndef SQLITE_OMIT_WAL
57068# define pagerUseWal(x) ((x)->pWal!=0)
57069#else
57070# define pagerUseWal(x) 0
57071# define pagerRollbackWal(x) 0
57072# define pagerWalFrames(v,w,x,y) 0
57073# define pagerOpenWalIfPresent(z) SQLITE_OK
57074# define pagerBeginReadTransaction(z) SQLITE_OK
57075#endif
57076
57077#ifndef NDEBUG
57078/*
57079** Usage:
57080**
57081** assert( assert_pager_state(pPager) );
57082**
57083** This function runs many asserts to try to find inconsistencies in
57084** the internal state of the Pager object.
57085*/
57086static int assert_pager_state(Pager *p){
57087 Pager *pPager = p;
57088
57089 /* State must be valid. */
57090 assert( p->eState==PAGER_OPEN
57091 || p->eState==PAGER_READER
57092 || p->eState==PAGER_WRITER_LOCKED
57093 || p->eState==PAGER_WRITER_CACHEMOD
57094 || p->eState==PAGER_WRITER_DBMOD
57095 || p->eState==PAGER_WRITER_FINISHED
57096 || p->eState==PAGER_ERROR
57097 );
57098
57099 /* Regardless of the current state, a temp-file connection always behaves
57100 ** as if it has an exclusive lock on the database file. It never updates
57101 ** the change-counter field, so the changeCountDone flag is always set.
57102 */
57103 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
57104 assert( p->tempFile==0 || pPager->changeCountDone );
57105
57106 /* If the useJournal flag is clear, the journal-mode must be "OFF".
57107 ** And if the journal-mode is "OFF", the journal file must not be open.
57108 */
57109 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
57110 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
57111
57112 /* Check that MEMDB implies noSync. And an in-memory journal. Since
57113 ** this means an in-memory pager performs no IO at all, it cannot encounter
57114 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
57115 ** a journal file. (although the in-memory journal implementation may
57116 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
57117 ** is therefore not possible for an in-memory pager to enter the ERROR
57118 ** state.
57119 */
57120 if( MEMDB ){
57121 assert( !isOpen(p->fd) );
57122 assert( p->noSync );
57123 assert( p->journalMode==PAGER_JOURNALMODE_OFF
57124 || p->journalMode==PAGER_JOURNALMODE_MEMORY
57125 );
57126 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
57127 assert( pagerUseWal(p)==0 );
57128 }
57129
57130 /* If changeCountDone is set, a RESERVED lock or greater must be held
57131 ** on the file.
57132 */
57133 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
57134 assert( p->eLock!=PENDING_LOCK );
57135
57136 switch( p->eState ){
57137 case PAGER_OPEN:
57138 assert( !MEMDB );
57139 assert( pPager->errCode==SQLITE_OK );
57140 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
57141 break;
57142
57143 case PAGER_READER:
57144 assert( pPager->errCode==SQLITE_OK );
57145 assert( p->eLock!=UNKNOWN_LOCK );
57146 assert( p->eLock>=SHARED_LOCK );
57147 break;
57148
57149 case PAGER_WRITER_LOCKED:
57150 assert( p->eLock!=UNKNOWN_LOCK );
57151 assert( pPager->errCode==SQLITE_OK );
57152 if( !pagerUseWal(pPager) ){
57153 assert( p->eLock>=RESERVED_LOCK );
57154 }
57155 assert( pPager->dbSize==pPager->dbOrigSize );
57156 assert( pPager->dbOrigSize==pPager->dbFileSize );
57157 assert( pPager->dbOrigSize==pPager->dbHintSize );
57158 assert( pPager->setSuper==0 );
57159 break;
57160
57161 case PAGER_WRITER_CACHEMOD:
57162 assert( p->eLock!=UNKNOWN_LOCK );
57163 assert( pPager->errCode==SQLITE_OK );
57164 if( !pagerUseWal(pPager) ){
57165 /* It is possible that if journal_mode=wal here that neither the
57166 ** journal file nor the WAL file are open. This happens during
57167 ** a rollback transaction that switches from journal_mode=off
57168 ** to journal_mode=wal.
57169 */
57170 assert( p->eLock>=RESERVED_LOCK );
57171 assert( isOpen(p->jfd)
57172 || p->journalMode==PAGER_JOURNALMODE_OFF
57173 || p->journalMode==PAGER_JOURNALMODE_WAL
57174 );
57175 }
57176 assert( pPager->dbOrigSize==pPager->dbFileSize );
57177 assert( pPager->dbOrigSize==pPager->dbHintSize );
57178 break;
57179
57180 case PAGER_WRITER_DBMOD:
57181 assert( p->eLock==EXCLUSIVE_LOCK );
57182 assert( pPager->errCode==SQLITE_OK );
57183 assert( !pagerUseWal(pPager) );
57184 assert( p->eLock>=EXCLUSIVE_LOCK );
57185 assert( isOpen(p->jfd)
57186 || p->journalMode==PAGER_JOURNALMODE_OFF
57187 || p->journalMode==PAGER_JOURNALMODE_WAL
57188 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57189 );
57190 assert( pPager->dbOrigSize<=pPager->dbHintSize );
57191 break;
57192
57193 case PAGER_WRITER_FINISHED:
57194 assert( p->eLock==EXCLUSIVE_LOCK );
57195 assert( pPager->errCode==SQLITE_OK );
57196 assert( !pagerUseWal(pPager) );
57197 assert( isOpen(p->jfd)
57198 || p->journalMode==PAGER_JOURNALMODE_OFF
57199 || p->journalMode==PAGER_JOURNALMODE_WAL
57200 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57201 );
57202 break;
57203
57204 case PAGER_ERROR:
57205 /* There must be at least one outstanding reference to the pager if
57206 ** in ERROR state. Otherwise the pager should have already dropped
57207 ** back to OPEN state.
57208 */
57209 assert( pPager->errCode!=SQLITE_OK );
57210 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
57211 break;
57212 }
57213
57214 return 1;
57215}
57216#endif /* ifndef NDEBUG */
57217
57218#ifdef SQLITE_DEBUG
57219/*
57220** Return a pointer to a human readable string in a static buffer
57221** containing the state of the Pager object passed as an argument. This
57222** is intended to be used within debuggers. For example, as an alternative
57223** to "print *pPager" in gdb:
57224**
57225** (gdb) printf "%s", print_pager_state(pPager)
57226**
57227** This routine has external linkage in order to suppress compiler warnings
57228** about an unused function. It is enclosed within SQLITE_DEBUG and so does
57229** not appear in normal builds.
57230*/
57231char *print_pager_state(Pager *p){
57232 static char zRet[1024];
57233
57234 sqlite3_snprintf(1024, zRet,
57235 "Filename: %s\n"
57236 "State: %s errCode=%d\n"
57237 "Lock: %s\n"
57238 "Locking mode: locking_mode=%s\n"
57239 "Journal mode: journal_mode=%s\n"
57240 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
57241 "Journal: journalOff=%lld journalHdr=%lld\n"
57242 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
57243 , p->zFilename
57244 , p->eState==PAGER_OPEN ? "OPEN" :
57245 p->eState==PAGER_READER ? "READER" :
57246 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
57247 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
57248 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
57249 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
57250 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
57251 , (int)p->errCode
57252 , p->eLock==NO_LOCK ? "NO_LOCK" :
57253 p->eLock==RESERVED_LOCK ? "RESERVED" :
57254 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
57255 p->eLock==SHARED_LOCK ? "SHARED" :
57256 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
57257 , p->exclusiveMode ? "exclusive" : "normal"
57258 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
57259 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
57260 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
57261 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
57262 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
57263 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
57264 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
57265 , p->journalOff, p->journalHdr
57266 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
57267 );
57268
57269 return zRet;
57270}
57271#endif
57272
57273/* Forward references to the various page getters */
57274static int getPageNormal(Pager*,Pgno,DbPage**,int);
57275static int getPageError(Pager*,Pgno,DbPage**,int);
57276#if SQLITE_MAX_MMAP_SIZE>0
57277static int getPageMMap(Pager*,Pgno,DbPage**,int);
57278#endif
57279
57280/*
57281** Set the Pager.xGet method for the appropriate routine used to fetch
57282** content from the pager.
57283*/
57284static void setGetterMethod(Pager *pPager){
57285 if( pPager->errCode ){
57286 pPager->xGet = getPageError;
57287#if SQLITE_MAX_MMAP_SIZE>0
57288 }else if( USEFETCH(pPager) ){
57289 pPager->xGet = getPageMMap;
57290#endif /* SQLITE_MAX_MMAP_SIZE>0 */
57291 }else{
57292 pPager->xGet = getPageNormal;
57293 }
57294}
57295
57296/*
57297** Return true if it is necessary to write page *pPg into the sub-journal.
57298** A page needs to be written into the sub-journal if there exists one
57299** or more open savepoints for which:
57300**
57301** * The page-number is less than or equal to PagerSavepoint.nOrig, and
57302** * The bit corresponding to the page-number is not set in
57303** PagerSavepoint.pInSavepoint.
57304*/
57305static int subjRequiresPage(PgHdr *pPg){
57306 Pager *pPager = pPg->pPager;
57307 PagerSavepoint *p;
57308 Pgno pgno = pPg->pgno;
57309 int i;
57310 for(i=0; i<pPager->nSavepoint; i++){
57311 p = &pPager->aSavepoint[i];
57312 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p: p->pInSavepoint, i: pgno) ){
57313 for(i=i+1; i<pPager->nSavepoint; i++){
57314 pPager->aSavepoint[i].bTruncateOnRelease = 0;
57315 }
57316 return 1;
57317 }
57318 }
57319 return 0;
57320}
57321
57322#ifdef SQLITE_DEBUG
57323/*
57324** Return true if the page is already in the journal file.
57325*/
57326static int pageInJournal(Pager *pPager, PgHdr *pPg){
57327 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
57328}
57329#endif
57330
57331/*
57332** Read a 32-bit integer from the given file descriptor. Store the integer
57333** that is read in *pRes. Return SQLITE_OK if everything worked, or an
57334** error code is something goes wrong.
57335**
57336** All values are stored on disk as big-endian.
57337*/
57338static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
57339 unsigned char ac[4];
57340 int rc = sqlite3OsRead(id: fd, pBuf: ac, amt: sizeof(ac), offset);
57341 if( rc==SQLITE_OK ){
57342 *pRes = sqlite3Get4byte(p: ac);
57343 }
57344 return rc;
57345}
57346
57347/*
57348** Write a 32-bit integer into a string buffer in big-endian byte order.
57349*/
57350#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
57351
57352
57353/*
57354** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
57355** on success or an error code is something goes wrong.
57356*/
57357static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
57358 char ac[4];
57359 put32bits(ac, val);
57360 return sqlite3OsWrite(id: fd, pBuf: ac, amt: 4, offset);
57361}
57362
57363/*
57364** Unlock the database file to level eLock, which must be either NO_LOCK
57365** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
57366** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
57367**
57368** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
57369** called, do not modify it. See the comment above the #define of
57370** UNKNOWN_LOCK for an explanation of this.
57371*/
57372static int pagerUnlockDb(Pager *pPager, int eLock){
57373 int rc = SQLITE_OK;
57374
57375 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
57376 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
57377 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
57378 if( isOpen(pPager->fd) ){
57379 assert( pPager->eLock>=eLock );
57380 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(id: pPager->fd, lockType: eLock);
57381 if( pPager->eLock!=UNKNOWN_LOCK ){
57382 pPager->eLock = (u8)eLock;
57383 }
57384 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
57385 }
57386 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
57387 return rc;
57388}
57389
57390/*
57391** Lock the database file to level eLock, which must be either SHARED_LOCK,
57392** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
57393** Pager.eLock variable to the new locking state.
57394**
57395** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
57396** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
57397** See the comment above the #define of UNKNOWN_LOCK for an explanation
57398** of this.
57399*/
57400static int pagerLockDb(Pager *pPager, int eLock){
57401 int rc = SQLITE_OK;
57402
57403 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
57404 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
57405 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(id: pPager->fd, lockType: eLock);
57406 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
57407 pPager->eLock = (u8)eLock;
57408 IOTRACE(("LOCK %p %d\n", pPager, eLock))
57409 }
57410 }
57411 return rc;
57412}
57413
57414/*
57415** This function determines whether or not the atomic-write or
57416** atomic-batch-write optimizations can be used with this pager. The
57417** atomic-write optimization can be used if:
57418**
57419** (a) the value returned by OsDeviceCharacteristics() indicates that
57420** a database page may be written atomically, and
57421** (b) the value returned by OsSectorSize() is less than or equal
57422** to the page size.
57423**
57424** If it can be used, then the value returned is the size of the journal
57425** file when it contains rollback data for exactly one page.
57426**
57427** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
57428** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
57429** returned in this case.
57430**
57431** If neither optimization can be used, 0 is returned.
57432*/
57433static int jrnlBufferSize(Pager *pPager){
57434 assert( !MEMDB );
57435
57436#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
57437 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
57438 int dc; /* Device characteristics */
57439
57440 assert( isOpen(pPager->fd) );
57441 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
57442#else
57443 UNUSED_PARAMETER(pPager);
57444#endif
57445
57446#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
57447 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
57448 return -1;
57449 }
57450#endif
57451
57452#ifdef SQLITE_ENABLE_ATOMIC_WRITE
57453 {
57454 int nSector = pPager->sectorSize;
57455 int szPage = pPager->pageSize;
57456
57457 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
57458 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
57459 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
57460 return 0;
57461 }
57462 }
57463
57464 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
57465#endif
57466
57467 return 0;
57468}
57469
57470/*
57471** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
57472** on the cache using a hash function. This is used for testing
57473** and debugging only.
57474*/
57475#ifdef SQLITE_CHECK_PAGES
57476/*
57477** Return a 32-bit hash of the page data for pPage.
57478*/
57479static u32 pager_datahash(int nByte, unsigned char *pData){
57480 u32 hash = 0;
57481 int i;
57482 for(i=0; i<nByte; i++){
57483 hash = (hash*1039) + pData[i];
57484 }
57485 return hash;
57486}
57487static u32 pager_pagehash(PgHdr *pPage){
57488 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
57489}
57490static void pager_set_pagehash(PgHdr *pPage){
57491 pPage->pageHash = pager_pagehash(pPage);
57492}
57493
57494/*
57495** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
57496** is defined, and NDEBUG is not defined, an assert() statement checks
57497** that the page is either dirty or still matches the calculated page-hash.
57498*/
57499#define CHECK_PAGE(x) checkPage(x)
57500static void checkPage(PgHdr *pPg){
57501 Pager *pPager = pPg->pPager;
57502 assert( pPager->eState!=PAGER_ERROR );
57503 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
57504}
57505
57506#else
57507#define pager_datahash(X,Y) 0
57508#define pager_pagehash(X) 0
57509#define pager_set_pagehash(X)
57510#define CHECK_PAGE(x)
57511#endif /* SQLITE_CHECK_PAGES */
57512
57513/*
57514** When this is called the journal file for pager pPager must be open.
57515** This function attempts to read a super-journal file name from the
57516** end of the file and, if successful, copies it into memory supplied
57517** by the caller. See comments above writeSuperJournal() for the format
57518** used to store a super-journal file name at the end of a journal file.
57519**
57520** zSuper must point to a buffer of at least nSuper bytes allocated by
57521** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
57522** enough space to write the super-journal name). If the super-journal
57523** name in the journal is longer than nSuper bytes (including a
57524** nul-terminator), then this is handled as if no super-journal name
57525** were present in the journal.
57526**
57527** If a super-journal file name is present at the end of the journal
57528** file, then it is copied into the buffer pointed to by zSuper. A
57529** nul-terminator byte is appended to the buffer following the
57530** super-journal file name.
57531**
57532** If it is determined that no super-journal file name is present
57533** zSuper[0] is set to 0 and SQLITE_OK returned.
57534**
57535** If an error occurs while reading from the journal file, an SQLite
57536** error code is returned.
57537*/
57538static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
57539 int rc; /* Return code */
57540 u32 len; /* Length in bytes of super-journal name */
57541 i64 szJ; /* Total size in bytes of journal file pJrnl */
57542 u32 cksum; /* MJ checksum value read from journal */
57543 u32 u; /* Unsigned loop counter */
57544 unsigned char aMagic[8]; /* A buffer to hold the magic header */
57545 zSuper[0] = '\0';
57546
57547 if( SQLITE_OK!=(rc = sqlite3OsFileSize(id: pJrnl, pSize: &szJ))
57548 || szJ<16
57549 || SQLITE_OK!=(rc = read32bits(fd: pJrnl, offset: szJ-16, pRes: &len))
57550 || len>=nSuper
57551 || len>szJ-16
57552 || len==0
57553 || SQLITE_OK!=(rc = read32bits(fd: pJrnl, offset: szJ-12, pRes: &cksum))
57554 || SQLITE_OK!=(rc = sqlite3OsRead(id: pJrnl, pBuf: aMagic, amt: 8, offset: szJ-8))
57555 || memcmp(s1: aMagic, s2: aJournalMagic, n: 8)
57556 || SQLITE_OK!=(rc = sqlite3OsRead(id: pJrnl, pBuf: zSuper, amt: len, offset: szJ-16-len))
57557 ){
57558 return rc;
57559 }
57560
57561 /* See if the checksum matches the super-journal name */
57562 for(u=0; u<len; u++){
57563 cksum -= zSuper[u];
57564 }
57565 if( cksum ){
57566 /* If the checksum doesn't add up, then one or more of the disk sectors
57567 ** containing the super-journal filename is corrupted. This means
57568 ** definitely roll back, so just return SQLITE_OK and report a (nul)
57569 ** super-journal filename.
57570 */
57571 len = 0;
57572 }
57573 zSuper[len] = '\0';
57574 zSuper[len+1] = '\0';
57575
57576 return SQLITE_OK;
57577}
57578
57579/*
57580** Return the offset of the sector boundary at or immediately
57581** following the value in pPager->journalOff, assuming a sector
57582** size of pPager->sectorSize bytes.
57583**
57584** i.e for a sector size of 512:
57585**
57586** Pager.journalOff Return value
57587** ---------------------------------------
57588** 0 0
57589** 512 512
57590** 100 512
57591** 2000 2048
57592**
57593*/
57594static i64 journalHdrOffset(Pager *pPager){
57595 i64 offset = 0;
57596 i64 c = pPager->journalOff;
57597 if( c ){
57598 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
57599 }
57600 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
57601 assert( offset>=c );
57602 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
57603 return offset;
57604}
57605
57606/*
57607** The journal file must be open when this function is called.
57608**
57609** This function is a no-op if the journal file has not been written to
57610** within the current transaction (i.e. if Pager.journalOff==0).
57611**
57612** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
57613** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
57614** zero the 28-byte header at the start of the journal file. In either case,
57615** if the pager is not in no-sync mode, sync the journal file immediately
57616** after writing or truncating it.
57617**
57618** If Pager.journalSizeLimit is set to a positive, non-zero value, and
57619** following the truncation or zeroing described above the size of the
57620** journal file in bytes is larger than this value, then truncate the
57621** journal file to Pager.journalSizeLimit bytes. The journal file does
57622** not need to be synced following this operation.
57623**
57624** If an IO error occurs, abandon processing and return the IO error code.
57625** Otherwise, return SQLITE_OK.
57626*/
57627static int zeroJournalHdr(Pager *pPager, int doTruncate){
57628 int rc = SQLITE_OK; /* Return code */
57629 assert( isOpen(pPager->jfd) );
57630 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
57631 if( pPager->journalOff ){
57632 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
57633
57634 IOTRACE(("JZEROHDR %p\n", pPager))
57635 if( doTruncate || iLimit==0 ){
57636 rc = sqlite3OsTruncate(id: pPager->jfd, size: 0);
57637 }else{
57638 static const char zeroHdr[28] = {0};
57639 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: zeroHdr, amt: sizeof(zeroHdr), offset: 0);
57640 }
57641 if( rc==SQLITE_OK && !pPager->noSync ){
57642 rc = sqlite3OsSync(id: pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
57643 }
57644
57645 /* At this point the transaction is committed but the write lock
57646 ** is still held on the file. If there is a size limit configured for
57647 ** the persistent journal and the journal file currently consumes more
57648 ** space than that limit allows for, truncate it now. There is no need
57649 ** to sync the file following this operation.
57650 */
57651 if( rc==SQLITE_OK && iLimit>0 ){
57652 i64 sz;
57653 rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &sz);
57654 if( rc==SQLITE_OK && sz>iLimit ){
57655 rc = sqlite3OsTruncate(id: pPager->jfd, size: iLimit);
57656 }
57657 }
57658 }
57659 return rc;
57660}
57661
57662/*
57663** The journal file must be open when this routine is called. A journal
57664** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
57665** current location.
57666**
57667** The format for the journal header is as follows:
57668** - 8 bytes: Magic identifying journal format.
57669** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
57670** - 4 bytes: Random number used for page hash.
57671** - 4 bytes: Initial database page count.
57672** - 4 bytes: Sector size used by the process that wrote this journal.
57673** - 4 bytes: Database page size.
57674**
57675** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
57676*/
57677static int writeJournalHdr(Pager *pPager){
57678 int rc = SQLITE_OK; /* Return code */
57679 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
57680 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
57681 u32 nWrite; /* Bytes of header sector written */
57682 int ii; /* Loop counter */
57683
57684 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57685
57686 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
57687 nHeader = JOURNAL_HDR_SZ(pPager);
57688 }
57689
57690 /* If there are active savepoints and any of them were created
57691 ** since the most recent journal header was written, update the
57692 ** PagerSavepoint.iHdrOffset fields now.
57693 */
57694 for(ii=0; ii<pPager->nSavepoint; ii++){
57695 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
57696 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
57697 }
57698 }
57699
57700 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
57701
57702 /*
57703 ** Write the nRec Field - the number of page records that follow this
57704 ** journal header. Normally, zero is written to this value at this time.
57705 ** After the records are added to the journal (and the journal synced,
57706 ** if in full-sync mode), the zero is overwritten with the true number
57707 ** of records (see syncJournal()).
57708 **
57709 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
57710 ** reading the journal this value tells SQLite to assume that the
57711 ** rest of the journal file contains valid page records. This assumption
57712 ** is dangerous, as if a failure occurred whilst writing to the journal
57713 ** file it may contain some garbage data. There are two scenarios
57714 ** where this risk can be ignored:
57715 **
57716 ** * When the pager is in no-sync mode. Corruption can follow a
57717 ** power failure in this case anyway.
57718 **
57719 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
57720 ** that garbage data is never appended to the journal file.
57721 */
57722 assert( isOpen(pPager->fd) || pPager->noSync );
57723 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
57724 || (sqlite3OsDeviceCharacteristics(id: pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
57725 ){
57726 memcpy(dest: zHeader, src: aJournalMagic, n: sizeof(aJournalMagic));
57727 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
57728 }else{
57729 memset(s: zHeader, c: 0, n: sizeof(aJournalMagic)+4);
57730 }
57731
57732 /* The random check-hash initializer */
57733 sqlite3_randomness(N: sizeof(pPager->cksumInit), pBuf: &pPager->cksumInit);
57734 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
57735 /* The initial database size */
57736 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
57737 /* The assumed sector size for this process */
57738 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
57739
57740 /* The page size */
57741 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
57742
57743 /* Initializing the tail of the buffer is not necessary. Everything
57744 ** works find if the following memset() is omitted. But initializing
57745 ** the memory prevents valgrind from complaining, so we are willing to
57746 ** take the performance hit.
57747 */
57748 memset(s: &zHeader[sizeof(aJournalMagic)+20], c: 0,
57749 n: nHeader-(sizeof(aJournalMagic)+20));
57750
57751 /* In theory, it is only necessary to write the 28 bytes that the
57752 ** journal header consumes to the journal file here. Then increment the
57753 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
57754 ** record is written to the following sector (leaving a gap in the file
57755 ** that will be implicitly filled in by the OS).
57756 **
57757 ** However it has been discovered that on some systems this pattern can
57758 ** be significantly slower than contiguously writing data to the file,
57759 ** even if that means explicitly writing data to the block of
57760 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
57761 ** is done.
57762 **
57763 ** The loop is required here in case the sector-size is larger than the
57764 ** database page size. Since the zHeader buffer is only Pager.pageSize
57765 ** bytes in size, more than one call to sqlite3OsWrite() may be required
57766 ** to populate the entire journal header sector.
57767 */
57768 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
57769 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
57770 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: zHeader, amt: nHeader, offset: pPager->journalOff);
57771 assert( pPager->journalHdr <= pPager->journalOff );
57772 pPager->journalOff += nHeader;
57773 }
57774
57775 return rc;
57776}
57777
57778/*
57779** The journal file must be open when this is called. A journal header file
57780** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
57781** file. The current location in the journal file is given by
57782** pPager->journalOff. See comments above function writeJournalHdr() for
57783** a description of the journal header format.
57784**
57785** If the header is read successfully, *pNRec is set to the number of
57786** page records following this header and *pDbSize is set to the size of the
57787** database before the transaction began, in pages. Also, pPager->cksumInit
57788** is set to the value read from the journal header. SQLITE_OK is returned
57789** in this case.
57790**
57791** If the journal header file appears to be corrupted, SQLITE_DONE is
57792** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
57793** cannot be read from the journal file an error code is returned.
57794*/
57795static int readJournalHdr(
57796 Pager *pPager, /* Pager object */
57797 int isHot,
57798 i64 journalSize, /* Size of the open journal file in bytes */
57799 u32 *pNRec, /* OUT: Value read from the nRec field */
57800 u32 *pDbSize /* OUT: Value of original database size field */
57801){
57802 int rc; /* Return code */
57803 unsigned char aMagic[8]; /* A buffer to hold the magic header */
57804 i64 iHdrOff; /* Offset of journal header being read */
57805
57806 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57807
57808 /* Advance Pager.journalOff to the start of the next sector. If the
57809 ** journal file is too small for there to be a header stored at this
57810 ** point, return SQLITE_DONE.
57811 */
57812 pPager->journalOff = journalHdrOffset(pPager);
57813 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
57814 return SQLITE_DONE;
57815 }
57816 iHdrOff = pPager->journalOff;
57817
57818 /* Read in the first 8 bytes of the journal header. If they do not match
57819 ** the magic string found at the start of each journal header, return
57820 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
57821 ** proceed.
57822 */
57823 if( isHot || iHdrOff!=pPager->journalHdr ){
57824 rc = sqlite3OsRead(id: pPager->jfd, pBuf: aMagic, amt: sizeof(aMagic), offset: iHdrOff);
57825 if( rc ){
57826 return rc;
57827 }
57828 if( memcmp(s1: aMagic, s2: aJournalMagic, n: sizeof(aMagic))!=0 ){
57829 return SQLITE_DONE;
57830 }
57831 }
57832
57833 /* Read the first three 32-bit fields of the journal header: The nRec
57834 ** field, the checksum-initializer and the database size at the start
57835 ** of the transaction. Return an error code if anything goes wrong.
57836 */
57837 if( SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+8, pRes: pNRec))
57838 || SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+12, pRes: &pPager->cksumInit))
57839 || SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+16, pRes: pDbSize))
57840 ){
57841 return rc;
57842 }
57843
57844 if( pPager->journalOff==0 ){
57845 u32 iPageSize; /* Page-size field of journal header */
57846 u32 iSectorSize; /* Sector-size field of journal header */
57847
57848 /* Read the page-size and sector-size journal header fields. */
57849 if( SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+20, pRes: &iSectorSize))
57850 || SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+24, pRes: &iPageSize))
57851 ){
57852 return rc;
57853 }
57854
57855 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
57856 ** journal header to zero. In this case, assume that the Pager.pageSize
57857 ** variable is already set to the correct page size.
57858 */
57859 if( iPageSize==0 ){
57860 iPageSize = pPager->pageSize;
57861 }
57862
57863 /* Check that the values read from the page-size and sector-size fields
57864 ** are within range. To be 'in range', both values need to be a power
57865 ** of two greater than or equal to 512 or 32, and not greater than their
57866 ** respective compile time maximum limits.
57867 */
57868 if( iPageSize<512 || iSectorSize<32
57869 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
57870 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
57871 ){
57872 /* If the either the page-size or sector-size in the journal-header is
57873 ** invalid, then the process that wrote the journal-header must have
57874 ** crashed before the header was synced. In this case stop reading
57875 ** the journal file here.
57876 */
57877 return SQLITE_DONE;
57878 }
57879
57880 /* Update the page-size to match the value read from the journal.
57881 ** Use a testcase() macro to make sure that malloc failure within
57882 ** PagerSetPagesize() is tested.
57883 */
57884 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
57885 testcase( rc!=SQLITE_OK );
57886
57887 /* Update the assumed sector-size to match the value used by
57888 ** the process that created this journal. If this journal was
57889 ** created by a process other than this one, then this routine
57890 ** is being called from within pager_playback(). The local value
57891 ** of Pager.sectorSize is restored at the end of that routine.
57892 */
57893 pPager->sectorSize = iSectorSize;
57894 }
57895
57896 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
57897 return rc;
57898}
57899
57900
57901/*
57902** Write the supplied super-journal name into the journal file for pager
57903** pPager at the current location. The super-journal name must be the last
57904** thing written to a journal file. If the pager is in full-sync mode, the
57905** journal file descriptor is advanced to the next sector boundary before
57906** anything is written. The format is:
57907**
57908** + 4 bytes: PAGER_SJ_PGNO.
57909** + N bytes: super-journal filename in utf-8.
57910** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
57911** + 4 bytes: super-journal name checksum.
57912** + 8 bytes: aJournalMagic[].
57913**
57914** The super-journal page checksum is the sum of the bytes in the super-journal
57915** name, where each byte is interpreted as a signed 8-bit integer.
57916**
57917** If zSuper is a NULL pointer (occurs for a single database transaction),
57918** this call is a no-op.
57919*/
57920static int writeSuperJournal(Pager *pPager, const char *zSuper){
57921 int rc; /* Return code */
57922 int nSuper; /* Length of string zSuper */
57923 i64 iHdrOff; /* Offset of header in journal file */
57924 i64 jrnlSize; /* Size of journal file on disk */
57925 u32 cksum = 0; /* Checksum of string zSuper */
57926
57927 assert( pPager->setSuper==0 );
57928 assert( !pagerUseWal(pPager) );
57929
57930 if( !zSuper
57931 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57932 || !isOpen(pPager->jfd)
57933 ){
57934 return SQLITE_OK;
57935 }
57936 pPager->setSuper = 1;
57937 assert( pPager->journalHdr <= pPager->journalOff );
57938
57939 /* Calculate the length in bytes and the checksum of zSuper */
57940 for(nSuper=0; zSuper[nSuper]; nSuper++){
57941 cksum += zSuper[nSuper];
57942 }
57943
57944 /* If in full-sync mode, advance to the next disk sector before writing
57945 ** the super-journal name. This is in case the previous page written to
57946 ** the journal has already been synced.
57947 */
57948 if( pPager->fullSync ){
57949 pPager->journalOff = journalHdrOffset(pPager);
57950 }
57951 iHdrOff = pPager->journalOff;
57952
57953 /* Write the super-journal data to the end of the journal file. If
57954 ** an error occurs, return the error code to the caller.
57955 */
57956 if( (0 != (rc = write32bits(fd: pPager->jfd, offset: iHdrOff, PAGER_SJ_PGNO(pPager))))
57957 || (0 != (rc = sqlite3OsWrite(id: pPager->jfd, pBuf: zSuper, amt: nSuper, offset: iHdrOff+4)))
57958 || (0 != (rc = write32bits(fd: pPager->jfd, offset: iHdrOff+4+nSuper, val: nSuper)))
57959 || (0 != (rc = write32bits(fd: pPager->jfd, offset: iHdrOff+4+nSuper+4, val: cksum)))
57960 || (0 != (rc = sqlite3OsWrite(id: pPager->jfd, pBuf: aJournalMagic, amt: 8,
57961 offset: iHdrOff+4+nSuper+8)))
57962 ){
57963 return rc;
57964 }
57965 pPager->journalOff += (nSuper+20);
57966
57967 /* If the pager is in persistent-journal mode, then the physical
57968 ** journal-file may extend past the end of the super-journal name
57969 ** and 8 bytes of magic data just written to the file. This is
57970 ** dangerous because the code to rollback a hot-journal file
57971 ** will not be able to find the super-journal name to determine
57972 ** whether or not the journal is hot.
57973 **
57974 ** Easiest thing to do in this scenario is to truncate the journal
57975 ** file to the required size.
57976 */
57977 if( SQLITE_OK==(rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &jrnlSize))
57978 && jrnlSize>pPager->journalOff
57979 ){
57980 rc = sqlite3OsTruncate(id: pPager->jfd, size: pPager->journalOff);
57981 }
57982 return rc;
57983}
57984
57985/*
57986** Discard the entire contents of the in-memory page-cache.
57987*/
57988static void pager_reset(Pager *pPager){
57989 pPager->iDataVersion++;
57990 sqlite3BackupRestart(pPager->pBackup);
57991 sqlite3PcacheClear(pCache: pPager->pPCache);
57992}
57993
57994/*
57995** Return the pPager->iDataVersion value
57996*/
57997SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
57998 return pPager->iDataVersion;
57999}
58000
58001/*
58002** Free all structures in the Pager.aSavepoint[] array and set both
58003** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
58004** if it is open and the pager is not in exclusive mode.
58005*/
58006static void releaseAllSavepoints(Pager *pPager){
58007 int ii; /* Iterator for looping through Pager.aSavepoint */
58008 for(ii=0; ii<pPager->nSavepoint; ii++){
58009 sqlite3BitvecDestroy(p: pPager->aSavepoint[ii].pInSavepoint);
58010 }
58011 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(p: pPager->sjfd) ){
58012 sqlite3OsClose(pId: pPager->sjfd);
58013 }
58014 sqlite3_free(p: pPager->aSavepoint);
58015 pPager->aSavepoint = 0;
58016 pPager->nSavepoint = 0;
58017 pPager->nSubRec = 0;
58018}
58019
58020/*
58021** Set the bit number pgno in the PagerSavepoint.pInSavepoint
58022** bitvecs of all open savepoints. Return SQLITE_OK if successful
58023** or SQLITE_NOMEM if a malloc failure occurs.
58024*/
58025static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
58026 int ii; /* Loop counter */
58027 int rc = SQLITE_OK; /* Result code */
58028
58029 for(ii=0; ii<pPager->nSavepoint; ii++){
58030 PagerSavepoint *p = &pPager->aSavepoint[ii];
58031 if( pgno<=p->nOrig ){
58032 rc |= sqlite3BitvecSet(p: p->pInSavepoint, i: pgno);
58033 testcase( rc==SQLITE_NOMEM );
58034 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
58035 }
58036 }
58037 return rc;
58038}
58039
58040/*
58041** This function is a no-op if the pager is in exclusive mode and not
58042** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
58043** state.
58044**
58045** If the pager is not in exclusive-access mode, the database file is
58046** completely unlocked. If the file is unlocked and the file-system does
58047** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
58048** closed (if it is open).
58049**
58050** If the pager is in ERROR state when this function is called, the
58051** contents of the pager cache are discarded before switching back to
58052** the OPEN state. Regardless of whether the pager is in exclusive-mode
58053** or not, any journal file left in the file-system will be treated
58054** as a hot-journal and rolled back the next time a read-transaction
58055** is opened (by this or by any other connection).
58056*/
58057static void pager_unlock(Pager *pPager){
58058
58059 assert( pPager->eState==PAGER_READER
58060 || pPager->eState==PAGER_OPEN
58061 || pPager->eState==PAGER_ERROR
58062 );
58063
58064 sqlite3BitvecDestroy(p: pPager->pInJournal);
58065 pPager->pInJournal = 0;
58066 releaseAllSavepoints(pPager);
58067
58068 if( pagerUseWal(pPager) ){
58069 assert( !isOpen(pPager->jfd) );
58070 sqlite3WalEndReadTransaction(pWal: pPager->pWal);
58071 pPager->eState = PAGER_OPEN;
58072 }else if( !pPager->exclusiveMode ){
58073 int rc; /* Error code returned by pagerUnlockDb() */
58074 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(id: pPager->fd):0;
58075
58076 /* If the operating system support deletion of open files, then
58077 ** close the journal file when dropping the database lock. Otherwise
58078 ** another connection with journal_mode=delete might delete the file
58079 ** out from under us.
58080 */
58081 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
58082 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
58083 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
58084 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
58085 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
58086 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
58087 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
58088 || 1!=(pPager->journalMode & 5)
58089 ){
58090 sqlite3OsClose(pId: pPager->jfd);
58091 }
58092
58093 /* If the pager is in the ERROR state and the call to unlock the database
58094 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
58095 ** above the #define for UNKNOWN_LOCK for an explanation of why this
58096 ** is necessary.
58097 */
58098 rc = pagerUnlockDb(pPager, NO_LOCK);
58099 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
58100 pPager->eLock = UNKNOWN_LOCK;
58101 }
58102
58103 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
58104 ** without clearing the error code. This is intentional - the error
58105 ** code is cleared and the cache reset in the block below.
58106 */
58107 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
58108 pPager->eState = PAGER_OPEN;
58109 }
58110
58111 /* If Pager.errCode is set, the contents of the pager cache cannot be
58112 ** trusted. Now that there are no outstanding references to the pager,
58113 ** it can safely move back to PAGER_OPEN state. This happens in both
58114 ** normal and exclusive-locking mode.
58115 */
58116 assert( pPager->errCode==SQLITE_OK || !MEMDB );
58117 if( pPager->errCode ){
58118 if( pPager->tempFile==0 ){
58119 pager_reset(pPager);
58120 pPager->changeCountDone = 0;
58121 pPager->eState = PAGER_OPEN;
58122 }else{
58123 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
58124 }
58125 if( USEFETCH(pPager) ) sqlite3OsUnfetch(id: pPager->fd, iOff: 0, p: 0);
58126 pPager->errCode = SQLITE_OK;
58127 setGetterMethod(pPager);
58128 }
58129
58130 pPager->journalOff = 0;
58131 pPager->journalHdr = 0;
58132 pPager->setSuper = 0;
58133}
58134
58135/*
58136** This function is called whenever an IOERR or FULL error that requires
58137** the pager to transition into the ERROR state may have occurred.
58138** The first argument is a pointer to the pager structure, the second
58139** the error-code about to be returned by a pager API function. The
58140** value returned is a copy of the second argument to this function.
58141**
58142** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
58143** IOERR sub-codes, the pager enters the ERROR state and the error code
58144** is stored in Pager.errCode. While the pager remains in the ERROR state,
58145** all major API calls on the Pager will immediately return Pager.errCode.
58146**
58147** The ERROR state indicates that the contents of the pager-cache
58148** cannot be trusted. This state can be cleared by completely discarding
58149** the contents of the pager-cache. If a transaction was active when
58150** the persistent error occurred, then the rollback journal may need
58151** to be replayed to restore the contents of the database file (as if
58152** it were a hot-journal).
58153*/
58154static int pager_error(Pager *pPager, int rc){
58155 int rc2 = rc & 0xff;
58156 assert( rc==SQLITE_OK || !MEMDB );
58157 assert(
58158 pPager->errCode==SQLITE_FULL ||
58159 pPager->errCode==SQLITE_OK ||
58160 (pPager->errCode & 0xff)==SQLITE_IOERR
58161 );
58162 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
58163 pPager->errCode = rc;
58164 pPager->eState = PAGER_ERROR;
58165 setGetterMethod(pPager);
58166 }
58167 return rc;
58168}
58169
58170static int pager_truncate(Pager *pPager, Pgno nPage);
58171
58172/*
58173** The write transaction open on pPager is being committed (bCommit==1)
58174** or rolled back (bCommit==0).
58175**
58176** Return TRUE if and only if all dirty pages should be flushed to disk.
58177**
58178** Rules:
58179**
58180** * For non-TEMP databases, always sync to disk. This is necessary
58181** for transactions to be durable.
58182**
58183** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
58184** file has been created already (via a spill on pagerStress()) and
58185** when the number of dirty pages in memory exceeds 25% of the total
58186** cache size.
58187*/
58188static int pagerFlushOnCommit(Pager *pPager, int bCommit){
58189 if( pPager->tempFile==0 ) return 1;
58190 if( !bCommit ) return 0;
58191 if( !isOpen(pPager->fd) ) return 0;
58192 return (sqlite3PCachePercentDirty(pCache: pPager->pPCache)>=25);
58193}
58194
58195/*
58196** This routine ends a transaction. A transaction is usually ended by
58197** either a COMMIT or a ROLLBACK operation. This routine may be called
58198** after rollback of a hot-journal, or if an error occurs while opening
58199** the journal file or writing the very first journal-header of a
58200** database transaction.
58201**
58202** This routine is never called in PAGER_ERROR state. If it is called
58203** in PAGER_NONE or PAGER_SHARED state and the lock held is less
58204** exclusive than a RESERVED lock, it is a no-op.
58205**
58206** Otherwise, any active savepoints are released.
58207**
58208** If the journal file is open, then it is "finalized". Once a journal
58209** file has been finalized it is not possible to use it to roll back a
58210** transaction. Nor will it be considered to be a hot-journal by this
58211** or any other database connection. Exactly how a journal is finalized
58212** depends on whether or not the pager is running in exclusive mode and
58213** the current journal-mode (Pager.journalMode value), as follows:
58214**
58215** journalMode==MEMORY
58216** Journal file descriptor is simply closed. This destroys an
58217** in-memory journal.
58218**
58219** journalMode==TRUNCATE
58220** Journal file is truncated to zero bytes in size.
58221**
58222** journalMode==PERSIST
58223** The first 28 bytes of the journal file are zeroed. This invalidates
58224** the first journal header in the file, and hence the entire journal
58225** file. An invalid journal file cannot be rolled back.
58226**
58227** journalMode==DELETE
58228** The journal file is closed and deleted using sqlite3OsDelete().
58229**
58230** If the pager is running in exclusive mode, this method of finalizing
58231** the journal file is never used. Instead, if the journalMode is
58232** DELETE and the pager is in exclusive mode, the method described under
58233** journalMode==PERSIST is used instead.
58234**
58235** After the journal is finalized, the pager moves to PAGER_READER state.
58236** If running in non-exclusive rollback mode, the lock on the file is
58237** downgraded to a SHARED_LOCK.
58238**
58239** SQLITE_OK is returned if no error occurs. If an error occurs during
58240** any of the IO operations to finalize the journal file or unlock the
58241** database then the IO error code is returned to the user. If the
58242** operation to finalize the journal file fails, then the code still
58243** tries to unlock the database file if not in exclusive mode. If the
58244** unlock operation fails as well, then the first error code related
58245** to the first error encountered (the journal finalization one) is
58246** returned.
58247*/
58248static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
58249 int rc = SQLITE_OK; /* Error code from journal finalization operation */
58250 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
58251
58252 /* Do nothing if the pager does not have an open write transaction
58253 ** or at least a RESERVED lock. This function may be called when there
58254 ** is no write-transaction active but a RESERVED or greater lock is
58255 ** held under two circumstances:
58256 **
58257 ** 1. After a successful hot-journal rollback, it is called with
58258 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
58259 **
58260 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
58261 ** lock switches back to locking_mode=normal and then executes a
58262 ** read-transaction, this function is called with eState==PAGER_READER
58263 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
58264 */
58265 assert( assert_pager_state(pPager) );
58266 assert( pPager->eState!=PAGER_ERROR );
58267 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
58268 return SQLITE_OK;
58269 }
58270
58271 releaseAllSavepoints(pPager);
58272 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
58273 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
58274 );
58275 if( isOpen(pPager->jfd) ){
58276 assert( !pagerUseWal(pPager) );
58277
58278 /* Finalize the journal file. */
58279 if( sqlite3JournalIsInMemory(p: pPager->jfd) ){
58280 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
58281 sqlite3OsClose(pId: pPager->jfd);
58282 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
58283 if( pPager->journalOff==0 ){
58284 rc = SQLITE_OK;
58285 }else{
58286 rc = sqlite3OsTruncate(id: pPager->jfd, size: 0);
58287 if( rc==SQLITE_OK && pPager->fullSync ){
58288 /* Make sure the new file size is written into the inode right away.
58289 ** Otherwise the journal might resurrect following a power loss and
58290 ** cause the last transaction to roll back. See
58291 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
58292 */
58293 rc = sqlite3OsSync(id: pPager->jfd, flags: pPager->syncFlags);
58294 }
58295 }
58296 pPager->journalOff = 0;
58297 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
58298 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
58299 ){
58300 rc = zeroJournalHdr(pPager, doTruncate: hasSuper||pPager->tempFile);
58301 pPager->journalOff = 0;
58302 }else{
58303 /* This branch may be executed with Pager.journalMode==MEMORY if
58304 ** a hot-journal was just rolled back. In this case the journal
58305 ** file should be closed and deleted. If this connection writes to
58306 ** the database file, it will do so using an in-memory journal.
58307 */
58308 int bDelete = !pPager->tempFile;
58309 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
58310 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
58311 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58312 || pPager->journalMode==PAGER_JOURNALMODE_WAL
58313 );
58314 sqlite3OsClose(pId: pPager->jfd);
58315 if( bDelete ){
58316 rc = sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zJournal, dirSync: pPager->extraSync);
58317 }
58318 }
58319 }
58320
58321#ifdef SQLITE_CHECK_PAGES
58322 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
58323 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
58324 PgHdr *p = sqlite3PagerLookup(pPager, 1);
58325 if( p ){
58326 p->pageHash = 0;
58327 sqlite3PagerUnrefNotNull(p);
58328 }
58329 }
58330#endif
58331
58332 sqlite3BitvecDestroy(p: pPager->pInJournal);
58333 pPager->pInJournal = 0;
58334 pPager->nRec = 0;
58335 if( rc==SQLITE_OK ){
58336 if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
58337 sqlite3PcacheCleanAll(pCache: pPager->pPCache);
58338 }else{
58339 sqlite3PcacheClearWritable(pCache: pPager->pPCache);
58340 }
58341 sqlite3PcacheTruncate(pCache: pPager->pPCache, pgno: pPager->dbSize);
58342 }
58343
58344 if( pagerUseWal(pPager) ){
58345 /* Drop the WAL write-lock, if any. Also, if the connection was in
58346 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
58347 ** lock held on the database file.
58348 */
58349 rc2 = sqlite3WalEndWriteTransaction(pWal: pPager->pWal);
58350 assert( rc2==SQLITE_OK );
58351 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
58352 /* This branch is taken when committing a transaction in rollback-journal
58353 ** mode if the database file on disk is larger than the database image.
58354 ** At this point the journal has been finalized and the transaction
58355 ** successfully committed, but the EXCLUSIVE lock is still held on the
58356 ** file. So it is safe to truncate the database file to its minimum
58357 ** required size. */
58358 assert( pPager->eLock==EXCLUSIVE_LOCK );
58359 rc = pager_truncate(pPager, nPage: pPager->dbSize);
58360 }
58361
58362 if( rc==SQLITE_OK && bCommit ){
58363 rc = sqlite3OsFileControl(id: pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, pArg: 0);
58364 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
58365 }
58366
58367 if( !pPager->exclusiveMode
58368 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pWal: pPager->pWal, op: 0))
58369 ){
58370 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
58371 }
58372 pPager->eState = PAGER_READER;
58373 pPager->setSuper = 0;
58374
58375 return (rc==SQLITE_OK?rc2:rc);
58376}
58377
58378/*
58379** Execute a rollback if a transaction is active and unlock the
58380** database file.
58381**
58382** If the pager has already entered the ERROR state, do not attempt
58383** the rollback at this time. Instead, pager_unlock() is called. The
58384** call to pager_unlock() will discard all in-memory pages, unlock
58385** the database file and move the pager back to OPEN state. If this
58386** means that there is a hot-journal left in the file-system, the next
58387** connection to obtain a shared lock on the pager (which may be this one)
58388** will roll it back.
58389**
58390** If the pager has not already entered the ERROR state, but an IO or
58391** malloc error occurs during a rollback, then this will itself cause
58392** the pager to enter the ERROR state. Which will be cleared by the
58393** call to pager_unlock(), as described above.
58394*/
58395static void pagerUnlockAndRollback(Pager *pPager){
58396 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
58397 assert( assert_pager_state(pPager) );
58398 if( pPager->eState>=PAGER_WRITER_LOCKED ){
58399 sqlite3BeginBenignMalloc();
58400 sqlite3PagerRollback(pPager);
58401 sqlite3EndBenignMalloc();
58402 }else if( !pPager->exclusiveMode ){
58403 assert( pPager->eState==PAGER_READER );
58404 pager_end_transaction(pPager, hasSuper: 0, bCommit: 0);
58405 }
58406 }
58407 pager_unlock(pPager);
58408}
58409
58410/*
58411** Parameter aData must point to a buffer of pPager->pageSize bytes
58412** of data. Compute and return a checksum based on the contents of the
58413** page of data and the current value of pPager->cksumInit.
58414**
58415** This is not a real checksum. It is really just the sum of the
58416** random initial value (pPager->cksumInit) and every 200th byte
58417** of the page data, starting with byte offset (pPager->pageSize%200).
58418** Each byte is interpreted as an 8-bit unsigned integer.
58419**
58420** Changing the formula used to compute this checksum results in an
58421** incompatible journal file format.
58422**
58423** If journal corruption occurs due to a power failure, the most likely
58424** scenario is that one end or the other of the record will be changed.
58425** It is much less likely that the two ends of the journal record will be
58426** correct and the middle be corrupt. Thus, this "checksum" scheme,
58427** though fast and simple, catches the mostly likely kind of corruption.
58428*/
58429static u32 pager_cksum(Pager *pPager, const u8 *aData){
58430 u32 cksum = pPager->cksumInit; /* Checksum value to return */
58431 int i = pPager->pageSize-200; /* Loop counter */
58432 while( i>0 ){
58433 cksum += aData[i];
58434 i -= 200;
58435 }
58436 return cksum;
58437}
58438
58439/*
58440** Read a single page from either the journal file (if isMainJrnl==1) or
58441** from the sub-journal (if isMainJrnl==0) and playback that page.
58442** The page begins at offset *pOffset into the file. The *pOffset
58443** value is increased to the start of the next page in the journal.
58444**
58445** The main rollback journal uses checksums - the statement journal does
58446** not.
58447**
58448** If the page number of the page record read from the (sub-)journal file
58449** is greater than the current value of Pager.dbSize, then playback is
58450** skipped and SQLITE_OK is returned.
58451**
58452** If pDone is not NULL, then it is a record of pages that have already
58453** been played back. If the page at *pOffset has already been played back
58454** (if the corresponding pDone bit is set) then skip the playback.
58455** Make sure the pDone bit corresponding to the *pOffset page is set
58456** prior to returning.
58457**
58458** If the page record is successfully read from the (sub-)journal file
58459** and played back, then SQLITE_OK is returned. If an IO error occurs
58460** while reading the record from the (sub-)journal file or while writing
58461** to the database file, then the IO error code is returned. If data
58462** is successfully read from the (sub-)journal file but appears to be
58463** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
58464** two circumstances:
58465**
58466** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
58467** * If the record is being rolled back from the main journal file
58468** and the checksum field does not match the record content.
58469**
58470** Neither of these two scenarios are possible during a savepoint rollback.
58471**
58472** If this is a savepoint rollback, then memory may have to be dynamically
58473** allocated by this function. If this is the case and an allocation fails,
58474** SQLITE_NOMEM is returned.
58475*/
58476static int pager_playback_one_page(
58477 Pager *pPager, /* The pager being played back */
58478 i64 *pOffset, /* Offset of record to playback */
58479 Bitvec *pDone, /* Bitvec of pages already played back */
58480 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
58481 int isSavepnt /* True for a savepoint rollback */
58482){
58483 int rc;
58484 PgHdr *pPg; /* An existing page in the cache */
58485 Pgno pgno; /* The page number of a page in journal */
58486 u32 cksum; /* Checksum used for sanity checking */
58487 char *aData; /* Temporary storage for the page */
58488 sqlite3_file *jfd; /* The file descriptor for the journal file */
58489 int isSynced; /* True if journal page is synced */
58490
58491 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
58492 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
58493 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
58494 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
58495
58496 aData = pPager->pTmpSpace;
58497 assert( aData ); /* Temp storage must have already been allocated */
58498 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
58499
58500 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
58501 ** or savepoint rollback done at the request of the caller) or this is
58502 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
58503 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
58504 ** only reads from the main journal, not the sub-journal.
58505 */
58506 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
58507 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
58508 );
58509 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
58510
58511 /* Read the page number and page data from the journal or sub-journal
58512 ** file. Return an error code to the caller if an IO error occurs.
58513 */
58514 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
58515 rc = read32bits(fd: jfd, offset: *pOffset, pRes: &pgno);
58516 if( rc!=SQLITE_OK ) return rc;
58517 rc = sqlite3OsRead(id: jfd, pBuf: (u8*)aData, amt: pPager->pageSize, offset: (*pOffset)+4);
58518 if( rc!=SQLITE_OK ) return rc;
58519 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
58520
58521 /* Sanity checking on the page. This is more important that I originally
58522 ** thought. If a power failure occurs while the journal is being written,
58523 ** it could cause invalid data to be written into the journal. We need to
58524 ** detect this invalid data (with high probability) and ignore it.
58525 */
58526 if( pgno==0 || pgno==PAGER_SJ_PGNO(pPager) ){
58527 assert( !isSavepnt );
58528 return SQLITE_DONE;
58529 }
58530 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(p: pDone, i: pgno) ){
58531 return SQLITE_OK;
58532 }
58533 if( isMainJrnl ){
58534 rc = read32bits(fd: jfd, offset: (*pOffset)-4, pRes: &cksum);
58535 if( rc ) return rc;
58536 if( !isSavepnt && pager_cksum(pPager, aData: (u8*)aData)!=cksum ){
58537 return SQLITE_DONE;
58538 }
58539 }
58540
58541 /* If this page has already been played back before during the current
58542 ** rollback, then don't bother to play it back again.
58543 */
58544 if( pDone && (rc = sqlite3BitvecSet(p: pDone, i: pgno))!=SQLITE_OK ){
58545 return rc;
58546 }
58547
58548 /* When playing back page 1, restore the nReserve setting
58549 */
58550 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
58551 pPager->nReserve = ((u8*)aData)[20];
58552 }
58553
58554 /* If the pager is in CACHEMOD state, then there must be a copy of this
58555 ** page in the pager cache. In this case just update the pager cache,
58556 ** not the database file. The page is left marked dirty in this case.
58557 **
58558 ** An exception to the above rule: If the database is in no-sync mode
58559 ** and a page is moved during an incremental vacuum then the page may
58560 ** not be in the pager cache. Later: if a malloc() or IO error occurs
58561 ** during a Movepage() call, then the page may not be in the cache
58562 ** either. So the condition described in the above paragraph is not
58563 ** assert()able.
58564 **
58565 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
58566 ** pager cache if it exists and the main file. The page is then marked
58567 ** not dirty. Since this code is only executed in PAGER_OPEN state for
58568 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
58569 ** if the pager is in OPEN state.
58570 **
58571 ** Ticket #1171: The statement journal might contain page content that is
58572 ** different from the page content at the start of the transaction.
58573 ** This occurs when a page is changed prior to the start of a statement
58574 ** then changed again within the statement. When rolling back such a
58575 ** statement we must not write to the original database unless we know
58576 ** for certain that original page contents are synced into the main rollback
58577 ** journal. Otherwise, a power loss might leave modified data in the
58578 ** database file without an entry in the rollback journal that can
58579 ** restore the database to its original form. Two conditions must be
58580 ** met before writing to the database files. (1) the database must be
58581 ** locked. (2) we know that the original page content is fully synced
58582 ** in the main journal either because the page is not in cache or else
58583 ** the page is marked as needSync==0.
58584 **
58585 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
58586 ** is possible to fail a statement on a database that does not yet exist.
58587 ** Do not attempt to write if database file has never been opened.
58588 */
58589 if( pagerUseWal(pPager) ){
58590 pPg = 0;
58591 }else{
58592 pPg = sqlite3PagerLookup(pPager, pgno);
58593 }
58594 assert( pPg || !MEMDB );
58595 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
58596 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
58597 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
58598 (isMainJrnl?"main-journal":"sub-journal")
58599 ));
58600 if( isMainJrnl ){
58601 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
58602 }else{
58603 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
58604 }
58605 if( isOpen(pPager->fd)
58606 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58607 && isSynced
58608 ){
58609 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
58610 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
58611 assert( !pagerUseWal(pPager) );
58612
58613 /* Write the data read from the journal back into the database file.
58614 ** This is usually safe even for an encrypted database - as the data
58615 ** was encrypted before it was written to the journal file. The exception
58616 ** is if the data was just read from an in-memory sub-journal. In that
58617 ** case it must be encrypted here before it is copied into the database
58618 ** file. */
58619 rc = sqlite3OsWrite(id: pPager->fd, pBuf: (u8 *)aData, amt: pPager->pageSize, offset: ofst);
58620
58621 if( pgno>pPager->dbFileSize ){
58622 pPager->dbFileSize = pgno;
58623 }
58624 if( pPager->pBackup ){
58625 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
58626 }
58627 }else if( !isMainJrnl && pPg==0 ){
58628 /* If this is a rollback of a savepoint and data was not written to
58629 ** the database and the page is not in-memory, there is a potential
58630 ** problem. When the page is next fetched by the b-tree layer, it
58631 ** will be read from the database file, which may or may not be
58632 ** current.
58633 **
58634 ** There are a couple of different ways this can happen. All are quite
58635 ** obscure. When running in synchronous mode, this can only happen
58636 ** if the page is on the free-list at the start of the transaction, then
58637 ** populated, then moved using sqlite3PagerMovepage().
58638 **
58639 ** The solution is to add an in-memory page to the cache containing
58640 ** the data just read from the sub-journal. Mark the page as dirty
58641 ** and if the pager requires a journal-sync, then mark the page as
58642 ** requiring a journal-sync before it is written.
58643 */
58644 assert( isSavepnt );
58645 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
58646 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
58647 rc = sqlite3PagerGet(pPager, pgno, ppPage: &pPg, clrFlag: 1);
58648 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
58649 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
58650 if( rc!=SQLITE_OK ) return rc;
58651 sqlite3PcacheMakeDirty(p: pPg);
58652 }
58653 if( pPg ){
58654 /* No page should ever be explicitly rolled back that is in use, except
58655 ** for page 1 which is held in use in order to keep the lock on the
58656 ** database active. However such a page may be rolled back as a result
58657 ** of an internal error resulting in an automatic call to
58658 ** sqlite3PagerRollback().
58659 */
58660 void *pData;
58661 pData = pPg->pData;
58662 memcpy(dest: pData, src: (u8*)aData, n: pPager->pageSize);
58663 pPager->xReiniter(pPg);
58664 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
58665 ** that call was dangerous and had no detectable benefit since the cache
58666 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
58667 ** has been removed. */
58668 pager_set_pagehash(pPg);
58669
58670 /* If this was page 1, then restore the value of Pager.dbFileVers.
58671 ** Do this before any decoding. */
58672 if( pgno==1 ){
58673 memcpy(dest: &pPager->dbFileVers, src: &((u8*)pData)[24],n: sizeof(pPager->dbFileVers));
58674 }
58675 sqlite3PcacheRelease(p: pPg);
58676 }
58677 return rc;
58678}
58679
58680/*
58681** Parameter zSuper is the name of a super-journal file. A single journal
58682** file that referred to the super-journal file has just been rolled back.
58683** This routine checks if it is possible to delete the super-journal file,
58684** and does so if it is.
58685**
58686** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
58687** available for use within this function.
58688**
58689** When a super-journal file is created, it is populated with the names
58690** of all of its child journals, one after another, formatted as utf-8
58691** encoded text. The end of each child journal file is marked with a
58692** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
58693** file for a transaction involving two databases might be:
58694**
58695** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
58696**
58697** A super-journal file may only be deleted once all of its child
58698** journals have been rolled back.
58699**
58700** This function reads the contents of the super-journal file into
58701** memory and loops through each of the child journal names. For
58702** each child journal, it checks if:
58703**
58704** * if the child journal exists, and if so
58705** * if the child journal contains a reference to super-journal
58706** file zSuper
58707**
58708** If a child journal can be found that matches both of the criteria
58709** above, this function returns without doing anything. Otherwise, if
58710** no such child journal can be found, file zSuper is deleted from
58711** the file-system using sqlite3OsDelete().
58712**
58713** If an IO error within this function, an error code is returned. This
58714** function allocates memory by calling sqlite3Malloc(). If an allocation
58715** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
58716** occur, SQLITE_OK is returned.
58717**
58718** TODO: This function allocates a single block of memory to load
58719** the entire contents of the super-journal file. This could be
58720** a couple of kilobytes or so - potentially larger than the page
58721** size.
58722*/
58723static int pager_delsuper(Pager *pPager, const char *zSuper){
58724 sqlite3_vfs *pVfs = pPager->pVfs;
58725 int rc; /* Return code */
58726 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
58727 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
58728 char *zSuperJournal = 0; /* Contents of super-journal file */
58729 i64 nSuperJournal; /* Size of super-journal file */
58730 char *zJournal; /* Pointer to one journal within MJ file */
58731 char *zSuperPtr; /* Space to hold super-journal filename */
58732 char *zFree = 0; /* Free this buffer */
58733 int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
58734
58735 /* Allocate space for both the pJournal and pSuper file descriptors.
58736 ** If successful, open the super-journal file for reading.
58737 */
58738 pSuper = (sqlite3_file *)sqlite3MallocZero(n: pVfs->szOsFile * 2);
58739 if( !pSuper ){
58740 rc = SQLITE_NOMEM_BKPT;
58741 pJournal = 0;
58742 }else{
58743 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
58744 rc = sqlite3OsOpen(pVfs, zPath: zSuper, pFile: pSuper, flags, pFlagsOut: 0);
58745 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
58746 }
58747 if( rc!=SQLITE_OK ) goto delsuper_out;
58748
58749 /* Load the entire super-journal file into space obtained from
58750 ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain
58751 ** sufficient space (in zSuperPtr) to hold the names of super-journal
58752 ** files extracted from regular rollback-journals.
58753 */
58754 rc = sqlite3OsFileSize(id: pSuper, pSize: &nSuperJournal);
58755 if( rc!=SQLITE_OK ) goto delsuper_out;
58756 nSuperPtr = pVfs->mxPathname+1;
58757 zFree = sqlite3Malloc(n: 4 + nSuperJournal + nSuperPtr + 2);
58758 if( !zFree ){
58759 rc = SQLITE_NOMEM_BKPT;
58760 goto delsuper_out;
58761 }
58762 zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0;
58763 zSuperJournal = &zFree[4];
58764 zSuperPtr = &zSuperJournal[nSuperJournal+2];
58765 rc = sqlite3OsRead(id: pSuper, pBuf: zSuperJournal, amt: (int)nSuperJournal, offset: 0);
58766 if( rc!=SQLITE_OK ) goto delsuper_out;
58767 zSuperJournal[nSuperJournal] = 0;
58768 zSuperJournal[nSuperJournal+1] = 0;
58769
58770 zJournal = zSuperJournal;
58771 while( (zJournal-zSuperJournal)<nSuperJournal ){
58772 int exists;
58773 rc = sqlite3OsAccess(pVfs, zPath: zJournal, SQLITE_ACCESS_EXISTS, pResOut: &exists);
58774 if( rc!=SQLITE_OK ){
58775 goto delsuper_out;
58776 }
58777 if( exists ){
58778 /* One of the journals pointed to by the super-journal exists.
58779 ** Open it and check if it points at the super-journal. If
58780 ** so, return without deleting the super-journal file.
58781 ** NB: zJournal is really a MAIN_JOURNAL. But call it a
58782 ** SUPER_JOURNAL here so that the VFS will not send the zJournal
58783 ** name into sqlite3_database_file_object().
58784 */
58785 int c;
58786 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
58787 rc = sqlite3OsOpen(pVfs, zPath: zJournal, pFile: pJournal, flags, pFlagsOut: 0);
58788 if( rc!=SQLITE_OK ){
58789 goto delsuper_out;
58790 }
58791
58792 rc = readSuperJournal(pJrnl: pJournal, zSuper: zSuperPtr, nSuper: nSuperPtr);
58793 sqlite3OsClose(pId: pJournal);
58794 if( rc!=SQLITE_OK ){
58795 goto delsuper_out;
58796 }
58797
58798 c = zSuperPtr[0]!=0 && strcmp(s1: zSuperPtr, s2: zSuper)==0;
58799 if( c ){
58800 /* We have a match. Do not delete the super-journal file. */
58801 goto delsuper_out;
58802 }
58803 }
58804 zJournal += (sqlite3Strlen30(z: zJournal)+1);
58805 }
58806
58807 sqlite3OsClose(pId: pSuper);
58808 rc = sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
58809
58810delsuper_out:
58811 sqlite3_free(p: zFree);
58812 if( pSuper ){
58813 sqlite3OsClose(pId: pSuper);
58814 assert( !isOpen(pJournal) );
58815 sqlite3_free(p: pSuper);
58816 }
58817 return rc;
58818}
58819
58820
58821/*
58822** This function is used to change the actual size of the database
58823** file in the file-system. This only happens when committing a transaction,
58824** or rolling back a transaction (including rolling back a hot-journal).
58825**
58826** If the main database file is not open, or the pager is not in either
58827** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
58828** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
58829** If the file on disk is currently larger than nPage pages, then use the VFS
58830** xTruncate() method to truncate it.
58831**
58832** Or, it might be the case that the file on disk is smaller than
58833** nPage pages. Some operating system implementations can get confused if
58834** you try to truncate a file to some size that is larger than it
58835** currently is, so detect this case and write a single zero byte to
58836** the end of the new file instead.
58837**
58838** If successful, return SQLITE_OK. If an IO error occurs while modifying
58839** the database file, return the error code to the caller.
58840*/
58841static int pager_truncate(Pager *pPager, Pgno nPage){
58842 int rc = SQLITE_OK;
58843 assert( pPager->eState!=PAGER_ERROR );
58844 assert( pPager->eState!=PAGER_READER );
58845 PAGERTRACE(("Truncate %d npage %u\n", PAGERID(pPager), nPage));
58846
58847
58848 if( isOpen(pPager->fd)
58849 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58850 ){
58851 i64 currentSize, newSize;
58852 int szPage = pPager->pageSize;
58853 assert( pPager->eLock==EXCLUSIVE_LOCK );
58854 /* TODO: Is it safe to use Pager.dbFileSize here? */
58855 rc = sqlite3OsFileSize(id: pPager->fd, pSize: &currentSize);
58856 newSize = szPage*(i64)nPage;
58857 if( rc==SQLITE_OK && currentSize!=newSize ){
58858 if( currentSize>newSize ){
58859 rc = sqlite3OsTruncate(id: pPager->fd, size: newSize);
58860 }else if( (currentSize+szPage)<=newSize ){
58861 char *pTmp = pPager->pTmpSpace;
58862 memset(s: pTmp, c: 0, n: szPage);
58863 testcase( (newSize-szPage) == currentSize );
58864 testcase( (newSize-szPage) > currentSize );
58865 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_SIZE_HINT, pArg: &newSize);
58866 rc = sqlite3OsWrite(id: pPager->fd, pBuf: pTmp, amt: szPage, offset: newSize-szPage);
58867 }
58868 if( rc==SQLITE_OK ){
58869 pPager->dbFileSize = nPage;
58870 }
58871 }
58872 }
58873 return rc;
58874}
58875
58876/*
58877** Return a sanitized version of the sector-size of OS file pFile. The
58878** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
58879*/
58880SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
58881 int iRet = sqlite3OsSectorSize(id: pFile);
58882 if( iRet<32 ){
58883 iRet = 512;
58884 }else if( iRet>MAX_SECTOR_SIZE ){
58885 assert( MAX_SECTOR_SIZE>=512 );
58886 iRet = MAX_SECTOR_SIZE;
58887 }
58888 return iRet;
58889}
58890
58891/*
58892** Set the value of the Pager.sectorSize variable for the given
58893** pager based on the value returned by the xSectorSize method
58894** of the open database file. The sector size will be used
58895** to determine the size and alignment of journal header and
58896** super-journal pointers within created journal files.
58897**
58898** For temporary files the effective sector size is always 512 bytes.
58899**
58900** Otherwise, for non-temporary files, the effective sector size is
58901** the value returned by the xSectorSize() method rounded up to 32 if
58902** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
58903** is greater than MAX_SECTOR_SIZE.
58904**
58905** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
58906** the effective sector size to its minimum value (512). The purpose of
58907** pPager->sectorSize is to define the "blast radius" of bytes that
58908** might change if a crash occurs while writing to a single byte in
58909** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
58910** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
58911** size. For backwards compatibility of the rollback journal file format,
58912** we cannot reduce the effective sector size below 512.
58913*/
58914static void setSectorSize(Pager *pPager){
58915 assert( isOpen(pPager->fd) || pPager->tempFile );
58916
58917 if( pPager->tempFile
58918 || (sqlite3OsDeviceCharacteristics(id: pPager->fd) &
58919 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
58920 ){
58921 /* Sector size doesn't matter for temporary files. Also, the file
58922 ** may not have been opened yet, in which case the OsSectorSize()
58923 ** call will segfault. */
58924 pPager->sectorSize = 512;
58925 }else{
58926 pPager->sectorSize = sqlite3SectorSize(pFile: pPager->fd);
58927 }
58928}
58929
58930/*
58931** Playback the journal and thus restore the database file to
58932** the state it was in before we started making changes.
58933**
58934** The journal file format is as follows:
58935**
58936** (1) 8 byte prefix. A copy of aJournalMagic[].
58937** (2) 4 byte big-endian integer which is the number of valid page records
58938** in the journal. If this value is 0xffffffff, then compute the
58939** number of page records from the journal size.
58940** (3) 4 byte big-endian integer which is the initial value for the
58941** sanity checksum.
58942** (4) 4 byte integer which is the number of pages to truncate the
58943** database to during a rollback.
58944** (5) 4 byte big-endian integer which is the sector size. The header
58945** is this many bytes in size.
58946** (6) 4 byte big-endian integer which is the page size.
58947** (7) zero padding out to the next sector size.
58948** (8) Zero or more pages instances, each as follows:
58949** + 4 byte page number.
58950** + pPager->pageSize bytes of data.
58951** + 4 byte checksum
58952**
58953** When we speak of the journal header, we mean the first 7 items above.
58954** Each entry in the journal is an instance of the 8th item.
58955**
58956** Call the value from the second bullet "nRec". nRec is the number of
58957** valid page entries in the journal. In most cases, you can compute the
58958** value of nRec from the size of the journal file. But if a power
58959** failure occurred while the journal was being written, it could be the
58960** case that the size of the journal file had already been increased but
58961** the extra entries had not yet made it safely to disk. In such a case,
58962** the value of nRec computed from the file size would be too large. For
58963** that reason, we always use the nRec value in the header.
58964**
58965** If the nRec value is 0xffffffff it means that nRec should be computed
58966** from the file size. This value is used when the user selects the
58967** no-sync option for the journal. A power failure could lead to corruption
58968** in this case. But for things like temporary table (which will be
58969** deleted when the power is restored) we don't care.
58970**
58971** If the file opened as the journal file is not a well-formed
58972** journal file then all pages up to the first corrupted page are rolled
58973** back (or no pages if the journal header is corrupted). The journal file
58974** is then deleted and SQLITE_OK returned, just as if no corruption had
58975** been encountered.
58976**
58977** If an I/O or malloc() error occurs, the journal-file is not deleted
58978** and an error code is returned.
58979**
58980** The isHot parameter indicates that we are trying to rollback a journal
58981** that might be a hot journal. Or, it could be that the journal is
58982** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
58983** If the journal really is hot, reset the pager cache prior rolling
58984** back any content. If the journal is merely persistent, no reset is
58985** needed.
58986*/
58987static int pager_playback(Pager *pPager, int isHot){
58988 sqlite3_vfs *pVfs = pPager->pVfs;
58989 i64 szJ; /* Size of the journal file in bytes */
58990 u32 nRec; /* Number of Records in the journal */
58991 u32 u; /* Unsigned loop counter */
58992 Pgno mxPg = 0; /* Size of the original file in pages */
58993 int rc; /* Result code of a subroutine */
58994 int res = 1; /* Value returned by sqlite3OsAccess() */
58995 char *zSuper = 0; /* Name of super-journal file if any */
58996 int needPagerReset; /* True to reset page prior to first page rollback */
58997 int nPlayback = 0; /* Total number of pages restored from journal */
58998 u32 savedPageSize = pPager->pageSize;
58999
59000 /* Figure out how many records are in the journal. Abort early if
59001 ** the journal is empty.
59002 */
59003 assert( isOpen(pPager->jfd) );
59004 rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &szJ);
59005 if( rc!=SQLITE_OK ){
59006 goto end_playback;
59007 }
59008
59009 /* Read the super-journal name from the journal, if it is present.
59010 ** If a super-journal file name is specified, but the file is not
59011 ** present on disk, then the journal is not hot and does not need to be
59012 ** played back.
59013 **
59014 ** TODO: Technically the following is an error because it assumes that
59015 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
59016 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
59017 ** mxPathname is 512, which is the same as the minimum allowable value
59018 ** for pageSize.
59019 */
59020 zSuper = pPager->pTmpSpace;
59021 rc = readSuperJournal(pJrnl: pPager->jfd, zSuper, nSuper: pPager->pVfs->mxPathname+1);
59022 if( rc==SQLITE_OK && zSuper[0] ){
59023 rc = sqlite3OsAccess(pVfs, zPath: zSuper, SQLITE_ACCESS_EXISTS, pResOut: &res);
59024 }
59025 zSuper = 0;
59026 if( rc!=SQLITE_OK || !res ){
59027 goto end_playback;
59028 }
59029 pPager->journalOff = 0;
59030 needPagerReset = isHot;
59031
59032 /* This loop terminates either when a readJournalHdr() or
59033 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
59034 ** occurs.
59035 */
59036 while( 1 ){
59037 /* Read the next journal header from the journal file. If there are
59038 ** not enough bytes left in the journal file for a complete header, or
59039 ** it is corrupted, then a process must have failed while writing it.
59040 ** This indicates nothing more needs to be rolled back.
59041 */
59042 rc = readJournalHdr(pPager, isHot, journalSize: szJ, pNRec: &nRec, pDbSize: &mxPg);
59043 if( rc!=SQLITE_OK ){
59044 if( rc==SQLITE_DONE ){
59045 rc = SQLITE_OK;
59046 }
59047 goto end_playback;
59048 }
59049
59050 /* If nRec is 0xffffffff, then this journal was created by a process
59051 ** working in no-sync mode. This means that the rest of the journal
59052 ** file consists of pages, there are no more journal headers. Compute
59053 ** the value of nRec based on this assumption.
59054 */
59055 if( nRec==0xffffffff ){
59056 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
59057 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
59058 }
59059
59060 /* If nRec is 0 and this rollback is of a transaction created by this
59061 ** process and if this is the final header in the journal, then it means
59062 ** that this part of the journal was being filled but has not yet been
59063 ** synced to disk. Compute the number of pages based on the remaining
59064 ** size of the file.
59065 **
59066 ** The third term of the test was added to fix ticket #2565.
59067 ** When rolling back a hot journal, nRec==0 always means that the next
59068 ** chunk of the journal contains zero pages to be rolled back. But
59069 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
59070 ** the journal, it means that the journal might contain additional
59071 ** pages that need to be rolled back and that the number of pages
59072 ** should be computed based on the journal file size.
59073 */
59074 if( nRec==0 && !isHot &&
59075 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
59076 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
59077 }
59078
59079 /* If this is the first header read from the journal, truncate the
59080 ** database file back to its original size.
59081 */
59082 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
59083 rc = pager_truncate(pPager, nPage: mxPg);
59084 if( rc!=SQLITE_OK ){
59085 goto end_playback;
59086 }
59087 pPager->dbSize = mxPg;
59088 if( pPager->mxPgno<mxPg ){
59089 pPager->mxPgno = mxPg;
59090 }
59091 }
59092
59093 /* Copy original pages out of the journal and back into the
59094 ** database file and/or page cache.
59095 */
59096 for(u=0; u<nRec; u++){
59097 if( needPagerReset ){
59098 pager_reset(pPager);
59099 needPagerReset = 0;
59100 }
59101 rc = pager_playback_one_page(pPager,pOffset: &pPager->journalOff,pDone: 0,isMainJrnl: 1,isSavepnt: 0);
59102 if( rc==SQLITE_OK ){
59103 nPlayback++;
59104 }else{
59105 if( rc==SQLITE_DONE ){
59106 pPager->journalOff = szJ;
59107 break;
59108 }else if( rc==SQLITE_IOERR_SHORT_READ ){
59109 /* If the journal has been truncated, simply stop reading and
59110 ** processing the journal. This might happen if the journal was
59111 ** not completely written and synced prior to a crash. In that
59112 ** case, the database should have never been written in the
59113 ** first place so it is OK to simply abandon the rollback. */
59114 rc = SQLITE_OK;
59115 goto end_playback;
59116 }else{
59117 /* If we are unable to rollback, quit and return the error
59118 ** code. This will cause the pager to enter the error state
59119 ** so that no further harm will be done. Perhaps the next
59120 ** process to come along will be able to rollback the database.
59121 */
59122 goto end_playback;
59123 }
59124 }
59125 }
59126 }
59127 /*NOTREACHED*/
59128 assert( 0 );
59129
59130end_playback:
59131 if( rc==SQLITE_OK ){
59132 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
59133 }
59134 /* Following a rollback, the database file should be back in its original
59135 ** state prior to the start of the transaction, so invoke the
59136 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
59137 ** assertion that the transaction counter was modified.
59138 */
59139#ifdef SQLITE_DEBUG
59140 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
59141#endif
59142
59143 /* If this playback is happening automatically as a result of an IO or
59144 ** malloc error that occurred after the change-counter was updated but
59145 ** before the transaction was committed, then the change-counter
59146 ** modification may just have been reverted. If this happens in exclusive
59147 ** mode, then subsequent transactions performed by the connection will not
59148 ** update the change-counter at all. This may lead to cache inconsistency
59149 ** problems for other processes at some point in the future. So, just
59150 ** in case this has happened, clear the changeCountDone flag now.
59151 */
59152 pPager->changeCountDone = pPager->tempFile;
59153
59154 if( rc==SQLITE_OK ){
59155 /* Leave 4 bytes of space before the super-journal filename in memory.
59156 ** This is because it may end up being passed to sqlite3OsOpen(), in
59157 ** which case it requires 4 0x00 bytes in memory immediately before
59158 ** the filename. */
59159 zSuper = &pPager->pTmpSpace[4];
59160 rc = readSuperJournal(pJrnl: pPager->jfd, zSuper, nSuper: pPager->pVfs->mxPathname+1);
59161 testcase( rc!=SQLITE_OK );
59162 }
59163 if( rc==SQLITE_OK
59164 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59165 ){
59166 rc = sqlite3PagerSync(pPager, zSuper: 0);
59167 }
59168 if( rc==SQLITE_OK ){
59169 rc = pager_end_transaction(pPager, hasSuper: zSuper[0]!='\0', bCommit: 0);
59170 testcase( rc!=SQLITE_OK );
59171 }
59172 if( rc==SQLITE_OK && zSuper[0] && res ){
59173 /* If there was a super-journal and this routine will return success,
59174 ** see if it is possible to delete the super-journal.
59175 */
59176 assert( zSuper==&pPager->pTmpSpace[4] );
59177 memset(s: pPager->pTmpSpace, c: 0, n: 4);
59178 rc = pager_delsuper(pPager, zSuper);
59179 testcase( rc!=SQLITE_OK );
59180 }
59181 if( isHot && nPlayback ){
59182 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, zFormat: "recovered %d pages from %s",
59183 nPlayback, pPager->zJournal);
59184 }
59185
59186 /* The Pager.sectorSize variable may have been updated while rolling
59187 ** back a journal created by a process with a different sector size
59188 ** value. Reset it to the correct value for this process.
59189 */
59190 setSectorSize(pPager);
59191 return rc;
59192}
59193
59194
59195/*
59196** Read the content for page pPg out of the database file (or out of
59197** the WAL if that is where the most recent copy if found) into
59198** pPg->pData. A shared lock or greater must be held on the database
59199** file before this function is called.
59200**
59201** If page 1 is read, then the value of Pager.dbFileVers[] is set to
59202** the value read from the database file.
59203**
59204** If an IO error occurs, then the IO error is returned to the caller.
59205** Otherwise, SQLITE_OK is returned.
59206*/
59207static int readDbPage(PgHdr *pPg){
59208 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
59209 int rc = SQLITE_OK; /* Return code */
59210
59211#ifndef SQLITE_OMIT_WAL
59212 u32 iFrame = 0; /* Frame of WAL containing pgno */
59213
59214 assert( pPager->eState>=PAGER_READER && !MEMDB );
59215 assert( isOpen(pPager->fd) );
59216
59217 if( pagerUseWal(pPager) ){
59218 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
59219 if( rc ) return rc;
59220 }
59221 if( iFrame ){
59222 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
59223 }else
59224#endif
59225 {
59226 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
59227 rc = sqlite3OsRead(id: pPager->fd, pBuf: pPg->pData, amt: pPager->pageSize, offset: iOffset);
59228 if( rc==SQLITE_IOERR_SHORT_READ ){
59229 rc = SQLITE_OK;
59230 }
59231 }
59232
59233 if( pPg->pgno==1 ){
59234 if( rc ){
59235 /* If the read is unsuccessful, set the dbFileVers[] to something
59236 ** that will never be a valid file version. dbFileVers[] is a copy
59237 ** of bytes 24..39 of the database. Bytes 28..31 should always be
59238 ** zero or the size of the database in page. Bytes 32..35 and 35..39
59239 ** should be page numbers which are never 0xffffffff. So filling
59240 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
59241 **
59242 ** For an encrypted database, the situation is more complex: bytes
59243 ** 24..39 of the database are white noise. But the probability of
59244 ** white noise equaling 16 bytes of 0xff is vanishingly small so
59245 ** we should still be ok.
59246 */
59247 memset(s: pPager->dbFileVers, c: 0xff, n: sizeof(pPager->dbFileVers));
59248 }else{
59249 u8 *dbFileVers = &((u8*)pPg->pData)[24];
59250 memcpy(dest: &pPager->dbFileVers, src: dbFileVers, n: sizeof(pPager->dbFileVers));
59251 }
59252 }
59253 PAGER_INCR(sqlite3_pager_readdb_count);
59254 PAGER_INCR(pPager->nRead);
59255 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
59256 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
59257 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
59258
59259 return rc;
59260}
59261
59262/*
59263** Update the value of the change-counter at offsets 24 and 92 in
59264** the header and the sqlite version number at offset 96.
59265**
59266** This is an unconditional update. See also the pager_incr_changecounter()
59267** routine which only updates the change-counter if the update is actually
59268** needed, as determined by the pPager->changeCountDone state variable.
59269*/
59270static void pager_write_changecounter(PgHdr *pPg){
59271 u32 change_counter;
59272 if( NEVER(pPg==0) ) return;
59273
59274 /* Increment the value just read and write it back to byte 24. */
59275 change_counter = sqlite3Get4byte(p: (u8*)pPg->pPager->dbFileVers)+1;
59276 put32bits(((char*)pPg->pData)+24, change_counter);
59277
59278 /* Also store the SQLite version number in bytes 96..99 and in
59279 ** bytes 92..95 store the change counter for which the version number
59280 ** is valid. */
59281 put32bits(((char*)pPg->pData)+92, change_counter);
59282 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
59283}
59284
59285#ifndef SQLITE_OMIT_WAL
59286/*
59287** This function is invoked once for each page that has already been
59288** written into the log file when a WAL transaction is rolled back.
59289** Parameter iPg is the page number of said page. The pCtx argument
59290** is actually a pointer to the Pager structure.
59291**
59292** If page iPg is present in the cache, and has no outstanding references,
59293** it is discarded. Otherwise, if there are one or more outstanding
59294** references, the page content is reloaded from the database. If the
59295** attempt to reload content from the database is required and fails,
59296** return an SQLite error code. Otherwise, SQLITE_OK.
59297*/
59298static int pagerUndoCallback(void *pCtx, Pgno iPg){
59299 int rc = SQLITE_OK;
59300 Pager *pPager = (Pager *)pCtx;
59301 PgHdr *pPg;
59302
59303 assert( pagerUseWal(pPager) );
59304 pPg = sqlite3PagerLookup(pPager, pgno: iPg);
59305 if( pPg ){
59306 if( sqlite3PcachePageRefcount(p: pPg)==1 ){
59307 sqlite3PcacheDrop(p: pPg);
59308 }else{
59309 rc = readDbPage(pPg);
59310 if( rc==SQLITE_OK ){
59311 pPager->xReiniter(pPg);
59312 }
59313 sqlite3PagerUnrefNotNull(pPg);
59314 }
59315 }
59316
59317 /* Normally, if a transaction is rolled back, any backup processes are
59318 ** updated as data is copied out of the rollback journal and into the
59319 ** database. This is not generally possible with a WAL database, as
59320 ** rollback involves simply truncating the log file. Therefore, if one
59321 ** or more frames have already been written to the log (and therefore
59322 ** also copied into the backup databases) as part of this transaction,
59323 ** the backups must be restarted.
59324 */
59325 sqlite3BackupRestart(pPager->pBackup);
59326
59327 return rc;
59328}
59329
59330/*
59331** This function is called to rollback a transaction on a WAL database.
59332*/
59333static int pagerRollbackWal(Pager *pPager){
59334 int rc; /* Return Code */
59335 PgHdr *pList; /* List of dirty pages to revert */
59336
59337 /* For all pages in the cache that are currently dirty or have already
59338 ** been written (but not committed) to the log file, do one of the
59339 ** following:
59340 **
59341 ** + Discard the cached page (if refcount==0), or
59342 ** + Reload page content from the database (if refcount>0).
59343 */
59344 pPager->dbSize = pPager->dbOrigSize;
59345 rc = sqlite3WalUndo(pWal: pPager->pWal, xUndo: pagerUndoCallback, pUndoCtx: (void *)pPager);
59346 pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
59347 while( pList && rc==SQLITE_OK ){
59348 PgHdr *pNext = pList->pDirty;
59349 rc = pagerUndoCallback(pCtx: (void *)pPager, iPg: pList->pgno);
59350 pList = pNext;
59351 }
59352
59353 return rc;
59354}
59355
59356/*
59357** This function is a wrapper around sqlite3WalFrames(). As well as logging
59358** the contents of the list of pages headed by pList (connected by pDirty),
59359** this function notifies any active backup processes that the pages have
59360** changed.
59361**
59362** The list of pages passed into this routine is always sorted by page number.
59363** Hence, if page 1 appears anywhere on the list, it will be the first page.
59364*/
59365static int pagerWalFrames(
59366 Pager *pPager, /* Pager object */
59367 PgHdr *pList, /* List of frames to log */
59368 Pgno nTruncate, /* Database size after this commit */
59369 int isCommit /* True if this is a commit */
59370){
59371 int rc; /* Return code */
59372 int nList; /* Number of pages in pList */
59373 PgHdr *p; /* For looping over pages */
59374
59375 assert( pPager->pWal );
59376 assert( pList );
59377#ifdef SQLITE_DEBUG
59378 /* Verify that the page list is in ascending order */
59379 for(p=pList; p && p->pDirty; p=p->pDirty){
59380 assert( p->pgno < p->pDirty->pgno );
59381 }
59382#endif
59383
59384 assert( pList->pDirty==0 || isCommit );
59385 if( isCommit ){
59386 /* If a WAL transaction is being committed, there is no point in writing
59387 ** any pages with page numbers greater than nTruncate into the WAL file.
59388 ** They will never be read by any client. So remove them from the pDirty
59389 ** list here. */
59390 PgHdr **ppNext = &pList;
59391 nList = 0;
59392 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
59393 if( p->pgno<=nTruncate ){
59394 ppNext = &p->pDirty;
59395 nList++;
59396 }
59397 }
59398 assert( pList );
59399 }else{
59400 nList = 1;
59401 }
59402 pPager->aStat[PAGER_STAT_WRITE] += nList;
59403
59404 if( pList->pgno==1 ) pager_write_changecounter(pPg: pList);
59405 rc = sqlite3WalFrames(pWal: pPager->pWal,
59406 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
59407 );
59408 if( rc==SQLITE_OK && pPager->pBackup ){
59409 for(p=pList; p; p=p->pDirty){
59410 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
59411 }
59412 }
59413
59414#ifdef SQLITE_CHECK_PAGES
59415 pList = sqlite3PcacheDirtyList(pPager->pPCache);
59416 for(p=pList; p; p=p->pDirty){
59417 pager_set_pagehash(p);
59418 }
59419#endif
59420
59421 return rc;
59422}
59423
59424/*
59425** Begin a read transaction on the WAL.
59426**
59427** This routine used to be called "pagerOpenSnapshot()" because it essentially
59428** makes a snapshot of the database at the current point in time and preserves
59429** that snapshot for use by the reader in spite of concurrently changes by
59430** other writers or checkpointers.
59431*/
59432static int pagerBeginReadTransaction(Pager *pPager){
59433 int rc; /* Return code */
59434 int changed = 0; /* True if cache must be reset */
59435
59436 assert( pagerUseWal(pPager) );
59437 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
59438
59439 /* sqlite3WalEndReadTransaction() was not called for the previous
59440 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
59441 ** are in locking_mode=NORMAL and EndRead() was previously called,
59442 ** the duplicate call is harmless.
59443 */
59444 sqlite3WalEndReadTransaction(pWal: pPager->pWal);
59445
59446 rc = sqlite3WalBeginReadTransaction(pWal: pPager->pWal, &changed);
59447 if( rc!=SQLITE_OK || changed ){
59448 pager_reset(pPager);
59449 if( USEFETCH(pPager) ) sqlite3OsUnfetch(id: pPager->fd, iOff: 0, p: 0);
59450 }
59451
59452 return rc;
59453}
59454#endif
59455
59456/*
59457** This function is called as part of the transition from PAGER_OPEN
59458** to PAGER_READER state to determine the size of the database file
59459** in pages (assuming the page size currently stored in Pager.pageSize).
59460**
59461** If no error occurs, SQLITE_OK is returned and the size of the database
59462** in pages is stored in *pnPage. Otherwise, an error code (perhaps
59463** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
59464*/
59465static int pagerPagecount(Pager *pPager, Pgno *pnPage){
59466 Pgno nPage; /* Value to return via *pnPage */
59467
59468 /* Query the WAL sub-system for the database size. The WalDbsize()
59469 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
59470 ** if the database size is not available. The database size is not
59471 ** available from the WAL sub-system if the log file is empty or
59472 ** contains no valid committed transactions.
59473 */
59474 assert( pPager->eState==PAGER_OPEN );
59475 assert( pPager->eLock>=SHARED_LOCK );
59476 assert( isOpen(pPager->fd) );
59477 assert( pPager->tempFile==0 );
59478 nPage = sqlite3WalDbsize(pWal: pPager->pWal);
59479
59480 /* If the number of pages in the database is not available from the
59481 ** WAL sub-system, determine the page count based on the size of
59482 ** the database file. If the size of the database file is not an
59483 ** integer multiple of the page-size, round up the result.
59484 */
59485 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
59486 i64 n = 0; /* Size of db file in bytes */
59487 int rc = sqlite3OsFileSize(id: pPager->fd, pSize: &n);
59488 if( rc!=SQLITE_OK ){
59489 return rc;
59490 }
59491 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
59492 }
59493
59494 /* If the current number of pages in the file is greater than the
59495 ** configured maximum pager number, increase the allowed limit so
59496 ** that the file can be read.
59497 */
59498 if( nPage>pPager->mxPgno ){
59499 pPager->mxPgno = (Pgno)nPage;
59500 }
59501
59502 *pnPage = nPage;
59503 return SQLITE_OK;
59504}
59505
59506#ifndef SQLITE_OMIT_WAL
59507/*
59508** Check if the *-wal file that corresponds to the database opened by pPager
59509** exists if the database is not empty, or verify that the *-wal file does
59510** not exist (by deleting it) if the database file is empty.
59511**
59512** If the database is not empty and the *-wal file exists, open the pager
59513** in WAL mode. If the database is empty or if no *-wal file exists and
59514** if no error occurs, make sure Pager.journalMode is not set to
59515** PAGER_JOURNALMODE_WAL.
59516**
59517** Return SQLITE_OK or an error code.
59518**
59519** The caller must hold a SHARED lock on the database file to call this
59520** function. Because an EXCLUSIVE lock on the db file is required to delete
59521** a WAL on a none-empty database, this ensures there is no race condition
59522** between the xAccess() below and an xDelete() being executed by some
59523** other connection.
59524*/
59525static int pagerOpenWalIfPresent(Pager *pPager){
59526 int rc = SQLITE_OK;
59527 assert( pPager->eState==PAGER_OPEN );
59528 assert( pPager->eLock>=SHARED_LOCK );
59529
59530 if( !pPager->tempFile ){
59531 int isWal; /* True if WAL file exists */
59532 rc = sqlite3OsAccess(
59533 pVfs: pPager->pVfs, zPath: pPager->zWal, SQLITE_ACCESS_EXISTS, pResOut: &isWal
59534 );
59535 if( rc==SQLITE_OK ){
59536 if( isWal ){
59537 Pgno nPage; /* Size of the database file */
59538
59539 rc = pagerPagecount(pPager, pnPage: &nPage);
59540 if( rc ) return rc;
59541 if( nPage==0 ){
59542 rc = sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zWal, dirSync: 0);
59543 }else{
59544 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
59545 rc = sqlite3PagerOpenWal(pPager, pisOpen: 0);
59546 }
59547 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
59548 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
59549 }
59550 }
59551 }
59552 return rc;
59553}
59554#endif
59555
59556/*
59557** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
59558** the entire super-journal file. The case pSavepoint==NULL occurs when
59559** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
59560** savepoint.
59561**
59562** When pSavepoint is not NULL (meaning a non-transaction savepoint is
59563** being rolled back), then the rollback consists of up to three stages,
59564** performed in the order specified:
59565**
59566** * Pages are played back from the main journal starting at byte
59567** offset PagerSavepoint.iOffset and continuing to
59568** PagerSavepoint.iHdrOffset, or to the end of the main journal
59569** file if PagerSavepoint.iHdrOffset is zero.
59570**
59571** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
59572** back starting from the journal header immediately following
59573** PagerSavepoint.iHdrOffset to the end of the main journal file.
59574**
59575** * Pages are then played back from the sub-journal file, starting
59576** with the PagerSavepoint.iSubRec and continuing to the end of
59577** the journal file.
59578**
59579** Throughout the rollback process, each time a page is rolled back, the
59580** corresponding bit is set in a bitvec structure (variable pDone in the
59581** implementation below). This is used to ensure that a page is only
59582** rolled back the first time it is encountered in either journal.
59583**
59584** If pSavepoint is NULL, then pages are only played back from the main
59585** journal file. There is no need for a bitvec in this case.
59586**
59587** In either case, before playback commences the Pager.dbSize variable
59588** is reset to the value that it held at the start of the savepoint
59589** (or transaction). No page with a page-number greater than this value
59590** is played back. If one is encountered it is simply skipped.
59591*/
59592static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
59593 i64 szJ; /* Effective size of the main journal */
59594 i64 iHdrOff; /* End of first segment of main-journal records */
59595 int rc = SQLITE_OK; /* Return code */
59596 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
59597
59598 assert( pPager->eState!=PAGER_ERROR );
59599 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59600
59601 /* Allocate a bitvec to use to store the set of pages rolled back */
59602 if( pSavepoint ){
59603 pDone = sqlite3BitvecCreate(iSize: pSavepoint->nOrig);
59604 if( !pDone ){
59605 return SQLITE_NOMEM_BKPT;
59606 }
59607 }
59608
59609 /* Set the database size back to the value it was before the savepoint
59610 ** being reverted was opened.
59611 */
59612 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
59613 pPager->changeCountDone = pPager->tempFile;
59614
59615 if( !pSavepoint && pagerUseWal(pPager) ){
59616 return pagerRollbackWal(pPager);
59617 }
59618
59619 /* Use pPager->journalOff as the effective size of the main rollback
59620 ** journal. The actual file might be larger than this in
59621 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
59622 ** past pPager->journalOff is off-limits to us.
59623 */
59624 szJ = pPager->journalOff;
59625 assert( pagerUseWal(pPager)==0 || szJ==0 );
59626
59627 /* Begin by rolling back records from the main journal starting at
59628 ** PagerSavepoint.iOffset and continuing to the next journal header.
59629 ** There might be records in the main journal that have a page number
59630 ** greater than the current database size (pPager->dbSize) but those
59631 ** will be skipped automatically. Pages are added to pDone as they
59632 ** are played back.
59633 */
59634 if( pSavepoint && !pagerUseWal(pPager) ){
59635 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
59636 pPager->journalOff = pSavepoint->iOffset;
59637 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
59638 rc = pager_playback_one_page(pPager, pOffset: &pPager->journalOff, pDone, isMainJrnl: 1, isSavepnt: 1);
59639 }
59640 assert( rc!=SQLITE_DONE );
59641 }else{
59642 pPager->journalOff = 0;
59643 }
59644
59645 /* Continue rolling back records out of the main journal starting at
59646 ** the first journal header seen and continuing until the effective end
59647 ** of the main journal file. Continue to skip out-of-range pages and
59648 ** continue adding pages rolled back to pDone.
59649 */
59650 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
59651 u32 ii; /* Loop counter */
59652 u32 nJRec = 0; /* Number of Journal Records */
59653 u32 dummy;
59654 rc = readJournalHdr(pPager, isHot: 0, journalSize: szJ, pNRec: &nJRec, pDbSize: &dummy);
59655 assert( rc!=SQLITE_DONE );
59656
59657 /*
59658 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
59659 ** test is related to ticket #2565. See the discussion in the
59660 ** pager_playback() function for additional information.
59661 */
59662 if( nJRec==0
59663 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
59664 ){
59665 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
59666 }
59667 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
59668 rc = pager_playback_one_page(pPager, pOffset: &pPager->journalOff, pDone, isMainJrnl: 1, isSavepnt: 1);
59669 }
59670 assert( rc!=SQLITE_DONE );
59671 }
59672 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
59673
59674 /* Finally, rollback pages from the sub-journal. Page that were
59675 ** previously rolled back out of the main journal (and are hence in pDone)
59676 ** will be skipped. Out-of-range pages are also skipped.
59677 */
59678 if( pSavepoint ){
59679 u32 ii; /* Loop counter */
59680 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
59681
59682 if( pagerUseWal(pPager) ){
59683 rc = sqlite3WalSavepointUndo(pWal: pPager->pWal, aWalData: pSavepoint->aWalData);
59684 }
59685 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
59686 assert( offset==(i64)ii*(4+pPager->pageSize) );
59687 rc = pager_playback_one_page(pPager, pOffset: &offset, pDone, isMainJrnl: 0, isSavepnt: 1);
59688 }
59689 assert( rc!=SQLITE_DONE );
59690 }
59691
59692 sqlite3BitvecDestroy(p: pDone);
59693 if( rc==SQLITE_OK ){
59694 pPager->journalOff = szJ;
59695 }
59696
59697 return rc;
59698}
59699
59700/*
59701** Change the maximum number of in-memory pages that are allowed
59702** before attempting to recycle clean and unused pages.
59703*/
59704SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
59705 sqlite3PcacheSetCachesize(pCache: pPager->pPCache, mxPage);
59706}
59707
59708/*
59709** Change the maximum number of in-memory pages that are allowed
59710** before attempting to spill pages to journal.
59711*/
59712SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
59713 return sqlite3PcacheSetSpillsize(p: pPager->pPCache, mxPage);
59714}
59715
59716/*
59717** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
59718*/
59719static void pagerFixMaplimit(Pager *pPager){
59720#if SQLITE_MAX_MMAP_SIZE>0
59721 sqlite3_file *fd = pPager->fd;
59722 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
59723 sqlite3_int64 sz;
59724 sz = pPager->szMmap;
59725 pPager->bUseFetch = (sz>0);
59726 setGetterMethod(pPager);
59727 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_MMAP_SIZE, pArg: &sz);
59728 }
59729#endif
59730}
59731
59732/*
59733** Change the maximum size of any memory mapping made of the database file.
59734*/
59735SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
59736 pPager->szMmap = szMmap;
59737 pagerFixMaplimit(pPager);
59738}
59739
59740/*
59741** Free as much memory as possible from the pager.
59742*/
59743SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
59744 sqlite3PcacheShrink(pCache: pPager->pPCache);
59745}
59746
59747/*
59748** Adjust settings of the pager to those specified in the pgFlags parameter.
59749**
59750** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
59751** of the database to damage due to OS crashes or power failures by
59752** changing the number of syncs()s when writing the journals.
59753** There are four levels:
59754**
59755** OFF sqlite3OsSync() is never called. This is the default
59756** for temporary and transient files.
59757**
59758** NORMAL The journal is synced once before writes begin on the
59759** database. This is normally adequate protection, but
59760** it is theoretically possible, though very unlikely,
59761** that an inopertune power failure could leave the journal
59762** in a state which would cause damage to the database
59763** when it is rolled back.
59764**
59765** FULL The journal is synced twice before writes begin on the
59766** database (with some additional information - the nRec field
59767** of the journal header - being written in between the two
59768** syncs). If we assume that writing a
59769** single disk sector is atomic, then this mode provides
59770** assurance that the journal will not be corrupted to the
59771** point of causing damage to the database during rollback.
59772**
59773** EXTRA This is like FULL except that is also syncs the directory
59774** that contains the rollback journal after the rollback
59775** journal is unlinked.
59776**
59777** The above is for a rollback-journal mode. For WAL mode, OFF continues
59778** to mean that no syncs ever occur. NORMAL means that the WAL is synced
59779** prior to the start of checkpoint and that the database file is synced
59780** at the conclusion of the checkpoint if the entire content of the WAL
59781** was written back into the database. But no sync operations occur for
59782** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
59783** file is synced following each commit operation, in addition to the
59784** syncs associated with NORMAL. There is no difference between FULL
59785** and EXTRA for WAL mode.
59786**
59787** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
59788** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
59789** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
59790** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
59791** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
59792** synchronous=FULL versus synchronous=NORMAL setting determines when
59793** the xSync primitive is called and is relevant to all platforms.
59794**
59795** Numeric values associated with these states are OFF==1, NORMAL=2,
59796** and FULL=3.
59797*/
59798SQLITE_PRIVATE void sqlite3PagerSetFlags(
59799 Pager *pPager, /* The pager to set safety level for */
59800 unsigned pgFlags /* Various flags */
59801){
59802 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
59803 if( pPager->tempFile ){
59804 pPager->noSync = 1;
59805 pPager->fullSync = 0;
59806 pPager->extraSync = 0;
59807 }else{
59808 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
59809 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
59810 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
59811 }
59812 if( pPager->noSync ){
59813 pPager->syncFlags = 0;
59814 }else if( pgFlags & PAGER_FULLFSYNC ){
59815 pPager->syncFlags = SQLITE_SYNC_FULL;
59816 }else{
59817 pPager->syncFlags = SQLITE_SYNC_NORMAL;
59818 }
59819 pPager->walSyncFlags = (pPager->syncFlags<<2);
59820 if( pPager->fullSync ){
59821 pPager->walSyncFlags |= pPager->syncFlags;
59822 }
59823 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
59824 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
59825 }
59826 if( pgFlags & PAGER_CACHESPILL ){
59827 pPager->doNotSpill &= ~SPILLFLAG_OFF;
59828 }else{
59829 pPager->doNotSpill |= SPILLFLAG_OFF;
59830 }
59831}
59832
59833/*
59834** The following global variable is incremented whenever the library
59835** attempts to open a temporary file. This information is used for
59836** testing and analysis only.
59837*/
59838#ifdef SQLITE_TEST
59839SQLITE_API int sqlite3_opentemp_count = 0;
59840#endif
59841
59842/*
59843** Open a temporary file.
59844**
59845** Write the file descriptor into *pFile. Return SQLITE_OK on success
59846** or some other error code if we fail. The OS will automatically
59847** delete the temporary file when it is closed.
59848**
59849** The flags passed to the VFS layer xOpen() call are those specified
59850** by parameter vfsFlags ORed with the following:
59851**
59852** SQLITE_OPEN_READWRITE
59853** SQLITE_OPEN_CREATE
59854** SQLITE_OPEN_EXCLUSIVE
59855** SQLITE_OPEN_DELETEONCLOSE
59856*/
59857static int pagerOpentemp(
59858 Pager *pPager, /* The pager object */
59859 sqlite3_file *pFile, /* Write the file descriptor here */
59860 int vfsFlags /* Flags passed through to the VFS */
59861){
59862 int rc; /* Return code */
59863
59864#ifdef SQLITE_TEST
59865 sqlite3_opentemp_count++; /* Used for testing and analysis only */
59866#endif
59867
59868 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
59869 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
59870 rc = sqlite3OsOpen(pVfs: pPager->pVfs, zPath: 0, pFile, flags: vfsFlags, pFlagsOut: 0);
59871 assert( rc!=SQLITE_OK || isOpen(pFile) );
59872 return rc;
59873}
59874
59875/*
59876** Set the busy handler function.
59877**
59878** The pager invokes the busy-handler if sqlite3OsLock() returns
59879** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
59880** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
59881** lock. It does *not* invoke the busy handler when upgrading from
59882** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
59883** (which occurs during hot-journal rollback). Summary:
59884**
59885** Transition | Invokes xBusyHandler
59886** --------------------------------------------------------
59887** NO_LOCK -> SHARED_LOCK | Yes
59888** SHARED_LOCK -> RESERVED_LOCK | No
59889** SHARED_LOCK -> EXCLUSIVE_LOCK | No
59890** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
59891**
59892** If the busy-handler callback returns non-zero, the lock is
59893** retried. If it returns zero, then the SQLITE_BUSY error is
59894** returned to the caller of the pager API function.
59895*/
59896SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
59897 Pager *pPager, /* Pager object */
59898 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
59899 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
59900){
59901 void **ap;
59902 pPager->xBusyHandler = xBusyHandler;
59903 pPager->pBusyHandlerArg = pBusyHandlerArg;
59904 ap = (void **)&pPager->xBusyHandler;
59905 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
59906 assert( ap[1]==pBusyHandlerArg );
59907 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_BUSYHANDLER, pArg: (void *)ap);
59908}
59909
59910/*
59911** Change the page size used by the Pager object. The new page size
59912** is passed in *pPageSize.
59913**
59914** If the pager is in the error state when this function is called, it
59915** is a no-op. The value returned is the error state error code (i.e.
59916** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
59917**
59918** Otherwise, if all of the following are true:
59919**
59920** * the new page size (value of *pPageSize) is valid (a power
59921** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
59922**
59923** * there are no outstanding page references, and
59924**
59925** * the database is either not an in-memory database or it is
59926** an in-memory database that currently consists of zero pages.
59927**
59928** then the pager object page size is set to *pPageSize.
59929**
59930** If the page size is changed, then this function uses sqlite3PagerMalloc()
59931** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
59932** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
59933** In all other cases, SQLITE_OK is returned.
59934**
59935** If the page size is not changed, either because one of the enumerated
59936** conditions above is not true, the pager was in error state when this
59937** function was called, or because the memory allocation attempt failed,
59938** then *pPageSize is set to the old, retained page size before returning.
59939*/
59940SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
59941 int rc = SQLITE_OK;
59942
59943 /* It is not possible to do a full assert_pager_state() here, as this
59944 ** function may be called from within PagerOpen(), before the state
59945 ** of the Pager object is internally consistent.
59946 **
59947 ** At one point this function returned an error if the pager was in
59948 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
59949 ** there is at least one outstanding page reference, this function
59950 ** is a no-op for that case anyhow.
59951 */
59952
59953 u32 pageSize = *pPageSize;
59954 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
59955 if( (pPager->memDb==0 || pPager->dbSize==0)
59956 && sqlite3PcacheRefCount(pCache: pPager->pPCache)==0
59957 && pageSize && pageSize!=(u32)pPager->pageSize
59958 ){
59959 char *pNew = NULL; /* New temp space */
59960 i64 nByte = 0;
59961
59962 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
59963 rc = sqlite3OsFileSize(id: pPager->fd, pSize: &nByte);
59964 }
59965 if( rc==SQLITE_OK ){
59966 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
59967 * cell header parser will never run off the end of the allocation */
59968 pNew = (char *)sqlite3PageMalloc(sz: pageSize+8);
59969 if( !pNew ){
59970 rc = SQLITE_NOMEM_BKPT;
59971 }else{
59972 memset(s: pNew+pageSize, c: 0, n: 8);
59973 }
59974 }
59975
59976 if( rc==SQLITE_OK ){
59977 pager_reset(pPager);
59978 rc = sqlite3PcacheSetPageSize(pCache: pPager->pPCache, szPage: pageSize);
59979 }
59980 if( rc==SQLITE_OK ){
59981 sqlite3PageFree(p: pPager->pTmpSpace);
59982 pPager->pTmpSpace = pNew;
59983 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
59984 pPager->pageSize = pageSize;
59985 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
59986 }else{
59987 sqlite3PageFree(p: pNew);
59988 }
59989 }
59990
59991 *pPageSize = pPager->pageSize;
59992 if( rc==SQLITE_OK ){
59993 if( nReserve<0 ) nReserve = pPager->nReserve;
59994 assert( nReserve>=0 && nReserve<1000 );
59995 pPager->nReserve = (i16)nReserve;
59996 pagerFixMaplimit(pPager);
59997 }
59998 return rc;
59999}
60000
60001/*
60002** Return a pointer to the "temporary page" buffer held internally
60003** by the pager. This is a buffer that is big enough to hold the
60004** entire content of a database page. This buffer is used internally
60005** during rollback and will be overwritten whenever a rollback
60006** occurs. But other modules are free to use it too, as long as
60007** no rollbacks are happening.
60008*/
60009SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
60010 return pPager->pTmpSpace;
60011}
60012
60013/*
60014** Attempt to set the maximum database page count if mxPage is positive.
60015** Make no changes if mxPage is zero or negative. And never reduce the
60016** maximum page count below the current size of the database.
60017**
60018** Regardless of mxPage, return the current maximum page count.
60019*/
60020SQLITE_PRIVATE Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){
60021 if( mxPage>0 ){
60022 pPager->mxPgno = mxPage;
60023 }
60024 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
60025 /* assert( pPager->mxPgno>=pPager->dbSize ); */
60026 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
60027 ** less than the total number of valid pages in the database. But this
60028 ** may be less than Pager.dbSize, and so the assert() above is not valid */
60029 return pPager->mxPgno;
60030}
60031
60032/*
60033** The following set of routines are used to disable the simulated
60034** I/O error mechanism. These routines are used to avoid simulated
60035** errors in places where we do not care about errors.
60036**
60037** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
60038** and generate no code.
60039*/
60040#ifdef SQLITE_TEST
60041SQLITE_API extern int sqlite3_io_error_pending;
60042SQLITE_API extern int sqlite3_io_error_hit;
60043static int saved_cnt;
60044void disable_simulated_io_errors(void){
60045 saved_cnt = sqlite3_io_error_pending;
60046 sqlite3_io_error_pending = -1;
60047}
60048void enable_simulated_io_errors(void){
60049 sqlite3_io_error_pending = saved_cnt;
60050}
60051#else
60052# define disable_simulated_io_errors()
60053# define enable_simulated_io_errors()
60054#endif
60055
60056/*
60057** Read the first N bytes from the beginning of the file into memory
60058** that pDest points to.
60059**
60060** If the pager was opened on a transient file (zFilename==""), or
60061** opened on a file less than N bytes in size, the output buffer is
60062** zeroed and SQLITE_OK returned. The rationale for this is that this
60063** function is used to read database headers, and a new transient or
60064** zero sized database has a header than consists entirely of zeroes.
60065**
60066** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
60067** the error code is returned to the caller and the contents of the
60068** output buffer undefined.
60069*/
60070SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
60071 int rc = SQLITE_OK;
60072 memset(s: pDest, c: 0, n: N);
60073 assert( isOpen(pPager->fd) || pPager->tempFile );
60074
60075 /* This routine is only called by btree immediately after creating
60076 ** the Pager object. There has not been an opportunity to transition
60077 ** to WAL mode yet.
60078 */
60079 assert( !pagerUseWal(pPager) );
60080
60081 if( isOpen(pPager->fd) ){
60082 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
60083 rc = sqlite3OsRead(id: pPager->fd, pBuf: pDest, amt: N, offset: 0);
60084 if( rc==SQLITE_IOERR_SHORT_READ ){
60085 rc = SQLITE_OK;
60086 }
60087 }
60088 return rc;
60089}
60090
60091/*
60092** This function may only be called when a read-transaction is open on
60093** the pager. It returns the total number of pages in the database.
60094**
60095** However, if the file is between 1 and <page-size> bytes in size, then
60096** this is considered a 1 page file.
60097*/
60098SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
60099 assert( pPager->eState>=PAGER_READER );
60100 assert( pPager->eState!=PAGER_WRITER_FINISHED );
60101 *pnPage = (int)pPager->dbSize;
60102}
60103
60104
60105/*
60106** Try to obtain a lock of type locktype on the database file. If
60107** a similar or greater lock is already held, this function is a no-op
60108** (returning SQLITE_OK immediately).
60109**
60110** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
60111** the busy callback if the lock is currently not available. Repeat
60112** until the busy callback returns false or until the attempt to
60113** obtain the lock succeeds.
60114**
60115** Return SQLITE_OK on success and an error code if we cannot obtain
60116** the lock. If the lock is obtained successfully, set the Pager.state
60117** variable to locktype before returning.
60118*/
60119static int pager_wait_on_lock(Pager *pPager, int locktype){
60120 int rc; /* Return code */
60121
60122 /* Check that this is either a no-op (because the requested lock is
60123 ** already held), or one of the transitions that the busy-handler
60124 ** may be invoked during, according to the comment above
60125 ** sqlite3PagerSetBusyhandler().
60126 */
60127 assert( (pPager->eLock>=locktype)
60128 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
60129 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
60130 );
60131
60132 do {
60133 rc = pagerLockDb(pPager, eLock: locktype);
60134 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
60135 return rc;
60136}
60137
60138/*
60139** Function assertTruncateConstraint(pPager) checks that one of the
60140** following is true for all dirty pages currently in the page-cache:
60141**
60142** a) The page number is less than or equal to the size of the
60143** current database image, in pages, OR
60144**
60145** b) if the page content were written at this time, it would not
60146** be necessary to write the current content out to the sub-journal.
60147**
60148** If the condition asserted by this function were not true, and the
60149** dirty page were to be discarded from the cache via the pagerStress()
60150** routine, pagerStress() would not write the current page content to
60151** the database file. If a savepoint transaction were rolled back after
60152** this happened, the correct behavior would be to restore the current
60153** content of the page. However, since this content is not present in either
60154** the database file or the portion of the rollback journal and
60155** sub-journal rolled back the content could not be restored and the
60156** database image would become corrupt. It is therefore fortunate that
60157** this circumstance cannot arise.
60158*/
60159#if defined(SQLITE_DEBUG)
60160static void assertTruncateConstraintCb(PgHdr *pPg){
60161 Pager *pPager = pPg->pPager;
60162 assert( pPg->flags&PGHDR_DIRTY );
60163 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
60164 Pgno pgno = pPg->pgno;
60165 int i;
60166 for(i=0; i<pPg->pPager->nSavepoint; i++){
60167 PagerSavepoint *p = &pPager->aSavepoint[i];
60168 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
60169 }
60170 }
60171}
60172static void assertTruncateConstraint(Pager *pPager){
60173 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
60174}
60175#else
60176# define assertTruncateConstraint(pPager)
60177#endif
60178
60179/*
60180** Truncate the in-memory database file image to nPage pages. This
60181** function does not actually modify the database file on disk. It
60182** just sets the internal state of the pager object so that the
60183** truncation will be done when the current transaction is committed.
60184**
60185** This function is only called right before committing a transaction.
60186** Once this function has been called, the transaction must either be
60187** rolled back or committed. It is not safe to call this function and
60188** then continue writing to the database.
60189*/
60190SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
60191 assert( pPager->dbSize>=nPage || CORRUPT_DB );
60192 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
60193 pPager->dbSize = nPage;
60194
60195 /* At one point the code here called assertTruncateConstraint() to
60196 ** ensure that all pages being truncated away by this operation are,
60197 ** if one or more savepoints are open, present in the savepoint
60198 ** journal so that they can be restored if the savepoint is rolled
60199 ** back. This is no longer necessary as this function is now only
60200 ** called right before committing a transaction. So although the
60201 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
60202 ** they cannot be rolled back. So the assertTruncateConstraint() call
60203 ** is no longer correct. */
60204}
60205
60206
60207/*
60208** This function is called before attempting a hot-journal rollback. It
60209** syncs the journal file to disk, then sets pPager->journalHdr to the
60210** size of the journal file so that the pager_playback() routine knows
60211** that the entire journal file has been synced.
60212**
60213** Syncing a hot-journal to disk before attempting to roll it back ensures
60214** that if a power-failure occurs during the rollback, the process that
60215** attempts rollback following system recovery sees the same journal
60216** content as this process.
60217**
60218** If everything goes as planned, SQLITE_OK is returned. Otherwise,
60219** an SQLite error code.
60220*/
60221static int pagerSyncHotJournal(Pager *pPager){
60222 int rc = SQLITE_OK;
60223 if( !pPager->noSync ){
60224 rc = sqlite3OsSync(id: pPager->jfd, SQLITE_SYNC_NORMAL);
60225 }
60226 if( rc==SQLITE_OK ){
60227 rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &pPager->journalHdr);
60228 }
60229 return rc;
60230}
60231
60232#if SQLITE_MAX_MMAP_SIZE>0
60233/*
60234** Obtain a reference to a memory mapped page object for page number pgno.
60235** The new object will use the pointer pData, obtained from xFetch().
60236** If successful, set *ppPage to point to the new page reference
60237** and return SQLITE_OK. Otherwise, return an SQLite error code and set
60238** *ppPage to zero.
60239**
60240** Page references obtained by calling this function should be released
60241** by calling pagerReleaseMapPage().
60242*/
60243static int pagerAcquireMapPage(
60244 Pager *pPager, /* Pager object */
60245 Pgno pgno, /* Page number */
60246 void *pData, /* xFetch()'d data for this page */
60247 PgHdr **ppPage /* OUT: Acquired page object */
60248){
60249 PgHdr *p; /* Memory mapped page to return */
60250
60251 if( pPager->pMmapFreelist ){
60252 *ppPage = p = pPager->pMmapFreelist;
60253 pPager->pMmapFreelist = p->pDirty;
60254 p->pDirty = 0;
60255 assert( pPager->nExtra>=8 );
60256 memset(s: p->pExtra, c: 0, n: 8);
60257 }else{
60258 *ppPage = p = (PgHdr *)sqlite3MallocZero(n: sizeof(PgHdr) + pPager->nExtra);
60259 if( p==0 ){
60260 sqlite3OsUnfetch(id: pPager->fd, iOff: (i64)(pgno-1) * pPager->pageSize, p: pData);
60261 return SQLITE_NOMEM_BKPT;
60262 }
60263 p->pExtra = (void *)&p[1];
60264 p->flags = PGHDR_MMAP;
60265 p->nRef = 1;
60266 p->pPager = pPager;
60267 }
60268
60269 assert( p->pExtra==(void *)&p[1] );
60270 assert( p->pPage==0 );
60271 assert( p->flags==PGHDR_MMAP );
60272 assert( p->pPager==pPager );
60273 assert( p->nRef==1 );
60274
60275 p->pgno = pgno;
60276 p->pData = pData;
60277 pPager->nMmapOut++;
60278
60279 return SQLITE_OK;
60280}
60281#endif
60282
60283/*
60284** Release a reference to page pPg. pPg must have been returned by an
60285** earlier call to pagerAcquireMapPage().
60286*/
60287static void pagerReleaseMapPage(PgHdr *pPg){
60288 Pager *pPager = pPg->pPager;
60289 pPager->nMmapOut--;
60290 pPg->pDirty = pPager->pMmapFreelist;
60291 pPager->pMmapFreelist = pPg;
60292
60293 assert( pPager->fd->pMethods->iVersion>=3 );
60294 sqlite3OsUnfetch(id: pPager->fd, iOff: (i64)(pPg->pgno-1)*pPager->pageSize, p: pPg->pData);
60295}
60296
60297/*
60298** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
60299*/
60300static void pagerFreeMapHdrs(Pager *pPager){
60301 PgHdr *p;
60302 PgHdr *pNext;
60303 for(p=pPager->pMmapFreelist; p; p=pNext){
60304 pNext = p->pDirty;
60305 sqlite3_free(p);
60306 }
60307}
60308
60309/* Verify that the database file has not be deleted or renamed out from
60310** under the pager. Return SQLITE_OK if the database is still where it ought
60311** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
60312** code from sqlite3OsAccess()) if the database has gone missing.
60313*/
60314static int databaseIsUnmoved(Pager *pPager){
60315 int bHasMoved = 0;
60316 int rc;
60317
60318 if( pPager->tempFile ) return SQLITE_OK;
60319 if( pPager->dbSize==0 ) return SQLITE_OK;
60320 assert( pPager->zFilename && pPager->zFilename[0] );
60321 rc = sqlite3OsFileControl(id: pPager->fd, SQLITE_FCNTL_HAS_MOVED, pArg: &bHasMoved);
60322 if( rc==SQLITE_NOTFOUND ){
60323 /* If the HAS_MOVED file-control is unimplemented, assume that the file
60324 ** has not been moved. That is the historical behavior of SQLite: prior to
60325 ** version 3.8.3, it never checked */
60326 rc = SQLITE_OK;
60327 }else if( rc==SQLITE_OK && bHasMoved ){
60328 rc = SQLITE_READONLY_DBMOVED;
60329 }
60330 return rc;
60331}
60332
60333
60334/*
60335** Shutdown the page cache. Free all memory and close all files.
60336**
60337** If a transaction was in progress when this routine is called, that
60338** transaction is rolled back. All outstanding pages are invalidated
60339** and their memory is freed. Any attempt to use a page associated
60340** with this page cache after this function returns will likely
60341** result in a coredump.
60342**
60343** This function always succeeds. If a transaction is active an attempt
60344** is made to roll it back. If an error occurs during the rollback
60345** a hot journal may be left in the filesystem but no error is returned
60346** to the caller.
60347*/
60348SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
60349 u8 *pTmp = (u8*)pPager->pTmpSpace;
60350 assert( db || pagerUseWal(pPager)==0 );
60351 assert( assert_pager_state(pPager) );
60352 disable_simulated_io_errors();
60353 sqlite3BeginBenignMalloc();
60354 pagerFreeMapHdrs(pPager);
60355 /* pPager->errCode = 0; */
60356 pPager->exclusiveMode = 0;
60357#ifndef SQLITE_OMIT_WAL
60358 {
60359 u8 *a = 0;
60360 assert( db || pPager->pWal==0 );
60361 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
60362 && SQLITE_OK==databaseIsUnmoved(pPager)
60363 ){
60364 a = pTmp;
60365 }
60366 sqlite3WalClose(pWal: pPager->pWal, db, sync_flags: pPager->walSyncFlags, pPager->pageSize,a);
60367 pPager->pWal = 0;
60368 }
60369#endif
60370 pager_reset(pPager);
60371 if( MEMDB ){
60372 pager_unlock(pPager);
60373 }else{
60374 /* If it is open, sync the journal file before calling UnlockAndRollback.
60375 ** If this is not done, then an unsynced portion of the open journal
60376 ** file may be played back into the database. If a power failure occurs
60377 ** while this is happening, the database could become corrupt.
60378 **
60379 ** If an error occurs while trying to sync the journal, shift the pager
60380 ** into the ERROR state. This causes UnlockAndRollback to unlock the
60381 ** database and close the journal file without attempting to roll it
60382 ** back or finalize it. The next database user will have to do hot-journal
60383 ** rollback before accessing the database file.
60384 */
60385 if( isOpen(pPager->jfd) ){
60386 pager_error(pPager, rc: pagerSyncHotJournal(pPager));
60387 }
60388 pagerUnlockAndRollback(pPager);
60389 }
60390 sqlite3EndBenignMalloc();
60391 enable_simulated_io_errors();
60392 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
60393 IOTRACE(("CLOSE %p\n", pPager))
60394 sqlite3OsClose(pId: pPager->jfd);
60395 sqlite3OsClose(pId: pPager->fd);
60396 sqlite3PageFree(p: pTmp);
60397 sqlite3PcacheClose(pCache: pPager->pPCache);
60398 assert( !pPager->aSavepoint && !pPager->pInJournal );
60399 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
60400
60401 sqlite3_free(p: pPager);
60402 return SQLITE_OK;
60403}
60404
60405#if !defined(NDEBUG) || defined(SQLITE_TEST)
60406/*
60407** Return the page number for page pPg.
60408*/
60409SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
60410 return pPg->pgno;
60411}
60412#endif
60413
60414/*
60415** Increment the reference count for page pPg.
60416*/
60417SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
60418 sqlite3PcacheRef(p: pPg);
60419}
60420
60421/*
60422** Sync the journal. In other words, make sure all the pages that have
60423** been written to the journal have actually reached the surface of the
60424** disk and can be restored in the event of a hot-journal rollback.
60425**
60426** If the Pager.noSync flag is set, then this function is a no-op.
60427** Otherwise, the actions required depend on the journal-mode and the
60428** device characteristics of the file-system, as follows:
60429**
60430** * If the journal file is an in-memory journal file, no action need
60431** be taken.
60432**
60433** * Otherwise, if the device does not support the SAFE_APPEND property,
60434** then the nRec field of the most recently written journal header
60435** is updated to contain the number of journal records that have
60436** been written following it. If the pager is operating in full-sync
60437** mode, then the journal file is synced before this field is updated.
60438**
60439** * If the device does not support the SEQUENTIAL property, then
60440** journal file is synced.
60441**
60442** Or, in pseudo-code:
60443**
60444** if( NOT <in-memory journal> ){
60445** if( NOT SAFE_APPEND ){
60446** if( <full-sync mode> ) xSync(<journal file>);
60447** <update nRec field>
60448** }
60449** if( NOT SEQUENTIAL ) xSync(<journal file>);
60450** }
60451**
60452** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
60453** page currently held in memory before returning SQLITE_OK. If an IO
60454** error is encountered, then the IO error code is returned to the caller.
60455*/
60456static int syncJournal(Pager *pPager, int newHdr){
60457 int rc; /* Return code */
60458
60459 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60460 || pPager->eState==PAGER_WRITER_DBMOD
60461 );
60462 assert( assert_pager_state(pPager) );
60463 assert( !pagerUseWal(pPager) );
60464
60465 rc = sqlite3PagerExclusiveLock(pPager);
60466 if( rc!=SQLITE_OK ) return rc;
60467
60468 if( !pPager->noSync ){
60469 assert( !pPager->tempFile );
60470 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
60471 const int iDc = sqlite3OsDeviceCharacteristics(id: pPager->fd);
60472 assert( isOpen(pPager->jfd) );
60473
60474 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
60475 /* This block deals with an obscure problem. If the last connection
60476 ** that wrote to this database was operating in persistent-journal
60477 ** mode, then the journal file may at this point actually be larger
60478 ** than Pager.journalOff bytes. If the next thing in the journal
60479 ** file happens to be a journal-header (written as part of the
60480 ** previous connection's transaction), and a crash or power-failure
60481 ** occurs after nRec is updated but before this connection writes
60482 ** anything else to the journal file (or commits/rolls back its
60483 ** transaction), then SQLite may become confused when doing the
60484 ** hot-journal rollback following recovery. It may roll back all
60485 ** of this connections data, then proceed to rolling back the old,
60486 ** out-of-date data that follows it. Database corruption.
60487 **
60488 ** To work around this, if the journal file does appear to contain
60489 ** a valid header following Pager.journalOff, then write a 0x00
60490 ** byte to the start of it to prevent it from being recognized.
60491 **
60492 ** Variable iNextHdrOffset is set to the offset at which this
60493 ** problematic header will occur, if it exists. aMagic is used
60494 ** as a temporary buffer to inspect the first couple of bytes of
60495 ** the potential journal header.
60496 */
60497 i64 iNextHdrOffset;
60498 u8 aMagic[8];
60499 u8 zHeader[sizeof(aJournalMagic)+4];
60500
60501 memcpy(dest: zHeader, src: aJournalMagic, n: sizeof(aJournalMagic));
60502 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
60503
60504 iNextHdrOffset = journalHdrOffset(pPager);
60505 rc = sqlite3OsRead(id: pPager->jfd, pBuf: aMagic, amt: 8, offset: iNextHdrOffset);
60506 if( rc==SQLITE_OK && 0==memcmp(s1: aMagic, s2: aJournalMagic, n: 8) ){
60507 static const u8 zerobyte = 0;
60508 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: &zerobyte, amt: 1, offset: iNextHdrOffset);
60509 }
60510 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
60511 return rc;
60512 }
60513
60514 /* Write the nRec value into the journal file header. If in
60515 ** full-synchronous mode, sync the journal first. This ensures that
60516 ** all data has really hit the disk before nRec is updated to mark
60517 ** it as a candidate for rollback.
60518 **
60519 ** This is not required if the persistent media supports the
60520 ** SAFE_APPEND property. Because in this case it is not possible
60521 ** for garbage data to be appended to the file, the nRec field
60522 ** is populated with 0xFFFFFFFF when the journal header is written
60523 ** and never needs to be updated.
60524 */
60525 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
60526 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
60527 IOTRACE(("JSYNC %p\n", pPager))
60528 rc = sqlite3OsSync(id: pPager->jfd, flags: pPager->syncFlags);
60529 if( rc!=SQLITE_OK ) return rc;
60530 }
60531 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
60532 rc = sqlite3OsWrite(
60533 id: pPager->jfd, pBuf: zHeader, amt: sizeof(zHeader), offset: pPager->journalHdr
60534 );
60535 if( rc!=SQLITE_OK ) return rc;
60536 }
60537 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
60538 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
60539 IOTRACE(("JSYNC %p\n", pPager))
60540 rc = sqlite3OsSync(id: pPager->jfd, flags: pPager->syncFlags|
60541 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
60542 );
60543 if( rc!=SQLITE_OK ) return rc;
60544 }
60545
60546 pPager->journalHdr = pPager->journalOff;
60547 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
60548 pPager->nRec = 0;
60549 rc = writeJournalHdr(pPager);
60550 if( rc!=SQLITE_OK ) return rc;
60551 }
60552 }else{
60553 pPager->journalHdr = pPager->journalOff;
60554 }
60555 }
60556
60557 /* Unless the pager is in noSync mode, the journal file was just
60558 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
60559 ** all pages.
60560 */
60561 sqlite3PcacheClearSyncFlags(pCache: pPager->pPCache);
60562 pPager->eState = PAGER_WRITER_DBMOD;
60563 assert( assert_pager_state(pPager) );
60564 return SQLITE_OK;
60565}
60566
60567/*
60568** The argument is the first in a linked list of dirty pages connected
60569** by the PgHdr.pDirty pointer. This function writes each one of the
60570** in-memory pages in the list to the database file. The argument may
60571** be NULL, representing an empty list. In this case this function is
60572** a no-op.
60573**
60574** The pager must hold at least a RESERVED lock when this function
60575** is called. Before writing anything to the database file, this lock
60576** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
60577** SQLITE_BUSY is returned and no data is written to the database file.
60578**
60579** If the pager is a temp-file pager and the actual file-system file
60580** is not yet open, it is created and opened before any data is
60581** written out.
60582**
60583** Once the lock has been upgraded and, if necessary, the file opened,
60584** the pages are written out to the database file in list order. Writing
60585** a page is skipped if it meets either of the following criteria:
60586**
60587** * The page number is greater than Pager.dbSize, or
60588** * The PGHDR_DONT_WRITE flag is set on the page.
60589**
60590** If writing out a page causes the database file to grow, Pager.dbFileSize
60591** is updated accordingly. If page 1 is written out, then the value cached
60592** in Pager.dbFileVers[] is updated to match the new value stored in
60593** the database file.
60594**
60595** If everything is successful, SQLITE_OK is returned. If an IO error
60596** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
60597** be obtained, SQLITE_BUSY is returned.
60598*/
60599static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
60600 int rc = SQLITE_OK; /* Return code */
60601
60602 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
60603 assert( !pagerUseWal(pPager) );
60604 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
60605 assert( pPager->eLock==EXCLUSIVE_LOCK );
60606 assert( isOpen(pPager->fd) || pList->pDirty==0 );
60607
60608 /* If the file is a temp-file has not yet been opened, open it now. It
60609 ** is not possible for rc to be other than SQLITE_OK if this branch
60610 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
60611 */
60612 if( !isOpen(pPager->fd) ){
60613 assert( pPager->tempFile && rc==SQLITE_OK );
60614 rc = pagerOpentemp(pPager, pFile: pPager->fd, vfsFlags: pPager->vfsFlags);
60615 }
60616
60617 /* Before the first write, give the VFS a hint of what the final
60618 ** file size will be.
60619 */
60620 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
60621 if( rc==SQLITE_OK
60622 && pPager->dbHintSize<pPager->dbSize
60623 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
60624 ){
60625 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
60626 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_SIZE_HINT, pArg: &szFile);
60627 pPager->dbHintSize = pPager->dbSize;
60628 }
60629
60630 while( rc==SQLITE_OK && pList ){
60631 Pgno pgno = pList->pgno;
60632
60633 /* If there are dirty pages in the page cache with page numbers greater
60634 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
60635 ** make the file smaller (presumably by auto-vacuum code). Do not write
60636 ** any such pages to the file.
60637 **
60638 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
60639 ** set (set by sqlite3PagerDontWrite()).
60640 */
60641 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
60642 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
60643 char *pData; /* Data to write */
60644
60645 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
60646 if( pList->pgno==1 ) pager_write_changecounter(pPg: pList);
60647
60648 pData = pList->pData;
60649
60650 /* Write out the page data. */
60651 rc = sqlite3OsWrite(id: pPager->fd, pBuf: pData, amt: pPager->pageSize, offset);
60652
60653 /* If page 1 was just written, update Pager.dbFileVers to match
60654 ** the value now stored in the database file. If writing this
60655 ** page caused the database file to grow, update dbFileSize.
60656 */
60657 if( pgno==1 ){
60658 memcpy(dest: &pPager->dbFileVers, src: &pData[24], n: sizeof(pPager->dbFileVers));
60659 }
60660 if( pgno>pPager->dbFileSize ){
60661 pPager->dbFileSize = pgno;
60662 }
60663 pPager->aStat[PAGER_STAT_WRITE]++;
60664
60665 /* Update any backup objects copying the contents of this pager. */
60666 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
60667
60668 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
60669 PAGERID(pPager), pgno, pager_pagehash(pList)));
60670 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
60671 PAGER_INCR(sqlite3_pager_writedb_count);
60672 }else{
60673 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
60674 }
60675 pager_set_pagehash(pList);
60676 pList = pList->pDirty;
60677 }
60678
60679 return rc;
60680}
60681
60682/*
60683** Ensure that the sub-journal file is open. If it is already open, this
60684** function is a no-op.
60685**
60686** SQLITE_OK is returned if everything goes according to plan. An
60687** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
60688** fails.
60689*/
60690static int openSubJournal(Pager *pPager){
60691 int rc = SQLITE_OK;
60692 if( !isOpen(pPager->sjfd) ){
60693 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
60694 | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE
60695 | SQLITE_OPEN_DELETEONCLOSE;
60696 int nStmtSpill = sqlite3Config.nStmtSpill;
60697 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
60698 nStmtSpill = -1;
60699 }
60700 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
60701 }
60702 return rc;
60703}
60704
60705/*
60706** Append a record of the current state of page pPg to the sub-journal.
60707**
60708** If successful, set the bit corresponding to pPg->pgno in the bitvecs
60709** for all open savepoints before returning.
60710**
60711** This function returns SQLITE_OK if everything is successful, an IO
60712** error code if the attempt to write to the sub-journal fails, or
60713** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
60714** bitvec.
60715*/
60716static int subjournalPage(PgHdr *pPg){
60717 int rc = SQLITE_OK;
60718 Pager *pPager = pPg->pPager;
60719 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
60720
60721 /* Open the sub-journal, if it has not already been opened */
60722 assert( pPager->useJournal );
60723 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
60724 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
60725 assert( pagerUseWal(pPager)
60726 || pageInJournal(pPager, pPg)
60727 || pPg->pgno>pPager->dbOrigSize
60728 );
60729 rc = openSubJournal(pPager);
60730
60731 /* If the sub-journal was opened successfully (or was already open),
60732 ** write the journal record into the file. */
60733 if( rc==SQLITE_OK ){
60734 void *pData = pPg->pData;
60735 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
60736 char *pData2;
60737 pData2 = pData;
60738 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
60739 rc = write32bits(fd: pPager->sjfd, offset, val: pPg->pgno);
60740 if( rc==SQLITE_OK ){
60741 rc = sqlite3OsWrite(id: pPager->sjfd, pBuf: pData2, amt: pPager->pageSize, offset: offset+4);
60742 }
60743 }
60744 }
60745 if( rc==SQLITE_OK ){
60746 pPager->nSubRec++;
60747 assert( pPager->nSavepoint>0 );
60748 rc = addToSavepointBitvecs(pPager, pgno: pPg->pgno);
60749 }
60750 return rc;
60751}
60752static int subjournalPageIfRequired(PgHdr *pPg){
60753 if( subjRequiresPage(pPg) ){
60754 return subjournalPage(pPg);
60755 }else{
60756 return SQLITE_OK;
60757 }
60758}
60759
60760/*
60761** This function is called by the pcache layer when it has reached some
60762** soft memory limit. The first argument is a pointer to a Pager object
60763** (cast as a void*). The pager is always 'purgeable' (not an in-memory
60764** database). The second argument is a reference to a page that is
60765** currently dirty but has no outstanding references. The page
60766** is always associated with the Pager object passed as the first
60767** argument.
60768**
60769** The job of this function is to make pPg clean by writing its contents
60770** out to the database file, if possible. This may involve syncing the
60771** journal file.
60772**
60773** If successful, sqlite3PcacheMakeClean() is called on the page and
60774** SQLITE_OK returned. If an IO error occurs while trying to make the
60775** page clean, the IO error code is returned. If the page cannot be
60776** made clean for some other reason, but no error occurs, then SQLITE_OK
60777** is returned by sqlite3PcacheMakeClean() is not called.
60778*/
60779static int pagerStress(void *p, PgHdr *pPg){
60780 Pager *pPager = (Pager *)p;
60781 int rc = SQLITE_OK;
60782
60783 assert( pPg->pPager==pPager );
60784 assert( pPg->flags&PGHDR_DIRTY );
60785
60786 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
60787 ** journal (and adding a new header) is not allowed. This occurs
60788 ** during calls to sqlite3PagerWrite() while trying to journal multiple
60789 ** pages belonging to the same sector.
60790 **
60791 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
60792 ** regardless of whether or not a sync is required. This is set during
60793 ** a rollback or by user request, respectively.
60794 **
60795 ** Spilling is also prohibited when in an error state since that could
60796 ** lead to database corruption. In the current implementation it
60797 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
60798 ** while in the error state, hence it is impossible for this routine to
60799 ** be called in the error state. Nevertheless, we include a NEVER()
60800 ** test for the error state as a safeguard against future changes.
60801 */
60802 if( NEVER(pPager->errCode) ) return SQLITE_OK;
60803 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
60804 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
60805 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
60806 if( pPager->doNotSpill
60807 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
60808 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
60809 ){
60810 return SQLITE_OK;
60811 }
60812
60813 pPager->aStat[PAGER_STAT_SPILL]++;
60814 pPg->pDirty = 0;
60815 if( pagerUseWal(pPager) ){
60816 /* Write a single frame for this page to the log. */
60817 rc = subjournalPageIfRequired(pPg);
60818 if( rc==SQLITE_OK ){
60819 rc = pagerWalFrames(pPager, pList: pPg, nTruncate: 0, isCommit: 0);
60820 }
60821 }else{
60822
60823#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
60824 if( pPager->tempFile==0 ){
60825 rc = sqlite3JournalCreate(pPager->jfd);
60826 if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
60827 }
60828#endif
60829
60830 /* Sync the journal file if required. */
60831 if( pPg->flags&PGHDR_NEED_SYNC
60832 || pPager->eState==PAGER_WRITER_CACHEMOD
60833 ){
60834 rc = syncJournal(pPager, newHdr: 1);
60835 }
60836
60837 /* Write the contents of the page out to the database file. */
60838 if( rc==SQLITE_OK ){
60839 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
60840 rc = pager_write_pagelist(pPager, pList: pPg);
60841 }
60842 }
60843
60844 /* Mark the page as clean. */
60845 if( rc==SQLITE_OK ){
60846 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
60847 sqlite3PcacheMakeClean(p: pPg);
60848 }
60849
60850 return pager_error(pPager, rc);
60851}
60852
60853/*
60854** Flush all unreferenced dirty pages to disk.
60855*/
60856SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
60857 int rc = pPager->errCode;
60858 if( !MEMDB ){
60859 PgHdr *pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
60860 assert( assert_pager_state(pPager) );
60861 while( rc==SQLITE_OK && pList ){
60862 PgHdr *pNext = pList->pDirty;
60863 if( pList->nRef==0 ){
60864 rc = pagerStress(p: (void*)pPager, pPg: pList);
60865 }
60866 pList = pNext;
60867 }
60868 }
60869
60870 return rc;
60871}
60872
60873/*
60874** Allocate and initialize a new Pager object and put a pointer to it
60875** in *ppPager. The pager should eventually be freed by passing it
60876** to sqlite3PagerClose().
60877**
60878** The zFilename argument is the path to the database file to open.
60879** If zFilename is NULL then a randomly-named temporary file is created
60880** and used as the file to be cached. Temporary files are be deleted
60881** automatically when they are closed. If zFilename is ":memory:" then
60882** all information is held in cache. It is never written to disk.
60883** This can be used to implement an in-memory database.
60884**
60885** The nExtra parameter specifies the number of bytes of space allocated
60886** along with each page reference. This space is available to the user
60887** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
60888** first 8 bytes of this space are zeroed but the remainder is uninitialized.
60889** (The extra space is used by btree as the MemPage object.)
60890**
60891** The flags argument is used to specify properties that affect the
60892** operation of the pager. It should be passed some bitwise combination
60893** of the PAGER_* flags.
60894**
60895** The vfsFlags parameter is a bitmask to pass to the flags parameter
60896** of the xOpen() method of the supplied VFS when opening files.
60897**
60898** If the pager object is allocated and the specified file opened
60899** successfully, SQLITE_OK is returned and *ppPager set to point to
60900** the new pager object. If an error occurs, *ppPager is set to NULL
60901** and error code returned. This function may return SQLITE_NOMEM
60902** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
60903** various SQLITE_IO_XXX errors.
60904*/
60905SQLITE_PRIVATE int sqlite3PagerOpen(
60906 sqlite3_vfs *pVfs, /* The virtual file system to use */
60907 Pager **ppPager, /* OUT: Return the Pager structure here */
60908 const char *zFilename, /* Name of the database file to open */
60909 int nExtra, /* Extra bytes append to each in-memory page */
60910 int flags, /* flags controlling this file */
60911 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
60912 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
60913){
60914 u8 *pPtr;
60915 Pager *pPager = 0; /* Pager object to allocate and return */
60916 int rc = SQLITE_OK; /* Return code */
60917 int tempFile = 0; /* True for temp files (incl. in-memory files) */
60918 int memDb = 0; /* True if this is an in-memory file */
60919 int memJM = 0; /* Memory journal mode */
60920 int readOnly = 0; /* True if this is a read-only file */
60921 int journalFileSize; /* Bytes to allocate for each journal fd */
60922 char *zPathname = 0; /* Full path to database file */
60923 int nPathname = 0; /* Number of bytes in zPathname */
60924 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
60925 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
60926 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
60927 const char *zUri = 0; /* URI args to copy */
60928 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
60929
60930 /* Figure out how much space is required for each journal file-handle
60931 ** (there are two of them, the main journal and the sub-journal). */
60932 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
60933
60934 /* Set the output variable to NULL in case an error occurs. */
60935 *ppPager = 0;
60936
60937#ifndef SQLITE_OMIT_MEMORYDB
60938 if( flags & PAGER_MEMORY ){
60939 memDb = 1;
60940 if( zFilename && zFilename[0] ){
60941 zPathname = sqlite3DbStrDup(db: 0, z: zFilename);
60942 if( zPathname==0 ) return SQLITE_NOMEM_BKPT;
60943 nPathname = sqlite3Strlen30(z: zPathname);
60944 zFilename = 0;
60945 }
60946 }
60947#endif
60948
60949 /* Compute and store the full pathname in an allocated buffer pointed
60950 ** to by zPathname, length nPathname. Or, if this is a temporary file,
60951 ** leave both nPathname and zPathname set to 0.
60952 */
60953 if( zFilename && zFilename[0] ){
60954 const char *z;
60955 nPathname = pVfs->mxPathname+1;
60956 zPathname = sqlite3DbMallocRaw(db: 0, n: nPathname*2);
60957 if( zPathname==0 ){
60958 return SQLITE_NOMEM_BKPT;
60959 }
60960 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
60961 rc = sqlite3OsFullPathname(pVfs, zPath: zFilename, nPathOut: nPathname, zPathOut: zPathname);
60962 if( rc!=SQLITE_OK ){
60963 if( rc==SQLITE_OK_SYMLINK ){
60964 if( vfsFlags & SQLITE_OPEN_NOFOLLOW ){
60965 rc = SQLITE_CANTOPEN_SYMLINK;
60966 }else{
60967 rc = SQLITE_OK;
60968 }
60969 }
60970 }
60971 nPathname = sqlite3Strlen30(z: zPathname);
60972 z = zUri = &zFilename[sqlite3Strlen30(z: zFilename)+1];
60973 while( *z ){
60974 z += strlen(s: z)+1;
60975 z += strlen(s: z)+1;
60976 }
60977 nUriByte = (int)(&z[1] - zUri);
60978 assert( nUriByte>=1 );
60979 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
60980 /* This branch is taken when the journal path required by
60981 ** the database being opened will be more than pVfs->mxPathname
60982 ** bytes in length. This means the database cannot be opened,
60983 ** as it will not be possible to open the journal file or even
60984 ** check for a hot-journal before reading.
60985 */
60986 rc = SQLITE_CANTOPEN_BKPT;
60987 }
60988 if( rc!=SQLITE_OK ){
60989 sqlite3DbFree(db: 0, p: zPathname);
60990 return rc;
60991 }
60992 }
60993
60994 /* Allocate memory for the Pager structure, PCache object, the
60995 ** three file descriptors, the database file name and the journal
60996 ** file name. The layout in memory is as follows:
60997 **
60998 ** Pager object (sizeof(Pager) bytes)
60999 ** PCache object (sqlite3PcacheSize() bytes)
61000 ** Database file handle (pVfs->szOsFile bytes)
61001 ** Sub-journal file handle (journalFileSize bytes)
61002 ** Main journal file handle (journalFileSize bytes)
61003 ** Ptr back to the Pager (sizeof(Pager*) bytes)
61004 ** \0\0\0\0 database prefix (4 bytes)
61005 ** Database file name (nPathname+1 bytes)
61006 ** URI query parameters (nUriByte bytes)
61007 ** Journal filename (nPathname+8+1 bytes)
61008 ** WAL filename (nPathname+4+1 bytes)
61009 ** \0\0\0 terminator (3 bytes)
61010 **
61011 ** Some 3rd-party software, over which we have no control, depends on
61012 ** the specific order of the filenames and the \0 separators between them
61013 ** so that it can (for example) find the database filename given the WAL
61014 ** filename without using the sqlite3_filename_database() API. This is a
61015 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
61016 ** software is in widespread use, so we try to avoid changing the filename
61017 ** order and formatting if possible. In particular, the details of the
61018 ** filename format expected by 3rd-party software should be as follows:
61019 **
61020 ** - Main Database Path
61021 ** - \0
61022 ** - Multiple URI components consisting of:
61023 ** - Key
61024 ** - \0
61025 ** - Value
61026 ** - \0
61027 ** - \0
61028 ** - Journal Path
61029 ** - \0
61030 ** - WAL Path (zWALName)
61031 ** - \0
61032 **
61033 ** The sqlite3_create_filename() interface and the databaseFilename() utility
61034 ** that is used by sqlite3_filename_database() and kin also depend on the
61035 ** specific formatting and order of the various filenames, so if the format
61036 ** changes here, be sure to change it there as well.
61037 */
61038 assert( SQLITE_PTRSIZE==sizeof(Pager*) );
61039 pPtr = (u8 *)sqlite3MallocZero(
61040 ROUND8(sizeof(*pPager)) + /* Pager structure */
61041 ROUND8(pcacheSize) + /* PCache object */
61042 ROUND8(pVfs->szOsFile) + /* The main db file */
61043 journalFileSize * 2 + /* The two journal files */
61044 SQLITE_PTRSIZE + /* Space to hold a pointer */
61045 4 + /* Database prefix */
61046 nPathname + 1 + /* database filename */
61047 nUriByte + /* query parameters */
61048 nPathname + 8 + 1 + /* Journal filename */
61049#ifndef SQLITE_OMIT_WAL
61050 nPathname + 4 + 1 + /* WAL filename */
61051#endif
61052 3 /* Terminator */
61053 );
61054 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
61055 if( !pPtr ){
61056 sqlite3DbFree(db: 0, p: zPathname);
61057 return SQLITE_NOMEM_BKPT;
61058 }
61059 pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager));
61060 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
61061 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
61062 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61063 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61064 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
61065 memcpy(dest: pPtr, src: &pPager, SQLITE_PTRSIZE); pPtr += SQLITE_PTRSIZE;
61066
61067 /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
61068 pPtr += 4; /* Skip zero prefix */
61069 pPager->zFilename = (char*)pPtr;
61070 if( nPathname>0 ){
61071 memcpy(dest: pPtr, src: zPathname, n: nPathname); pPtr += nPathname + 1;
61072 if( zUri ){
61073 memcpy(dest: pPtr, src: zUri, n: nUriByte); pPtr += nUriByte;
61074 }else{
61075 pPtr++;
61076 }
61077 }
61078
61079
61080 /* Fill in Pager.zJournal */
61081 if( nPathname>0 ){
61082 pPager->zJournal = (char*)pPtr;
61083 memcpy(dest: pPtr, src: zPathname, n: nPathname); pPtr += nPathname;
61084 memcpy(dest: pPtr, src: "-journal",n: 8); pPtr += 8 + 1;
61085#ifdef SQLITE_ENABLE_8_3_NAMES
61086 sqlite3FileSuffix3(zFilename,pPager->zJournal);
61087 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
61088#endif
61089 }else{
61090 pPager->zJournal = 0;
61091 }
61092
61093#ifndef SQLITE_OMIT_WAL
61094 /* Fill in Pager.zWal */
61095 if( nPathname>0 ){
61096 pPager->zWal = (char*)pPtr;
61097 memcpy(dest: pPtr, src: zPathname, n: nPathname); pPtr += nPathname;
61098 memcpy(dest: pPtr, src: "-wal", n: 4); pPtr += 4 + 1;
61099#ifdef SQLITE_ENABLE_8_3_NAMES
61100 sqlite3FileSuffix3(zFilename, pPager->zWal);
61101 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
61102#endif
61103 }else{
61104 pPager->zWal = 0;
61105 }
61106#endif
61107 (void)pPtr; /* Suppress warning about unused pPtr value */
61108
61109 if( nPathname ) sqlite3DbFree(db: 0, p: zPathname);
61110 pPager->pVfs = pVfs;
61111 pPager->vfsFlags = vfsFlags;
61112
61113 /* Open the pager file.
61114 */
61115 if( zFilename && zFilename[0] ){
61116 int fout = 0; /* VFS flags returned by xOpen() */
61117 rc = sqlite3OsOpen(pVfs, zPath: pPager->zFilename, pFile: pPager->fd, flags: vfsFlags, pFlagsOut: &fout);
61118 assert( !memDb );
61119 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
61120 readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
61121
61122 /* If the file was successfully opened for read/write access,
61123 ** choose a default page size in case we have to create the
61124 ** database file. The default page size is the maximum of:
61125 **
61126 ** + SQLITE_DEFAULT_PAGE_SIZE,
61127 ** + The value returned by sqlite3OsSectorSize()
61128 ** + The largest page size that can be written atomically.
61129 */
61130 if( rc==SQLITE_OK ){
61131 int iDc = sqlite3OsDeviceCharacteristics(id: pPager->fd);
61132 if( !readOnly ){
61133 setSectorSize(pPager);
61134 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
61135 if( szPageDflt<pPager->sectorSize ){
61136 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
61137 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
61138 }else{
61139 szPageDflt = (u32)pPager->sectorSize;
61140 }
61141 }
61142#ifdef SQLITE_ENABLE_ATOMIC_WRITE
61143 {
61144 int ii;
61145 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
61146 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
61147 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
61148 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
61149 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
61150 szPageDflt = ii;
61151 }
61152 }
61153 }
61154#endif
61155 }
61156 pPager->noLock = sqlite3_uri_boolean(z: pPager->zFilename, zParam: "nolock", bDefault: 0);
61157 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
61158 || sqlite3_uri_boolean(z: pPager->zFilename, zParam: "immutable", bDefault: 0) ){
61159 vfsFlags |= SQLITE_OPEN_READONLY;
61160 goto act_like_temp_file;
61161 }
61162 }
61163 }else{
61164 /* If a temporary file is requested, it is not opened immediately.
61165 ** In this case we accept the default page size and delay actually
61166 ** opening the file until the first call to OsWrite().
61167 **
61168 ** This branch is also run for an in-memory database. An in-memory
61169 ** database is the same as a temp-file that is never written out to
61170 ** disk and uses an in-memory rollback journal.
61171 **
61172 ** This branch also runs for files marked as immutable.
61173 */
61174act_like_temp_file:
61175 tempFile = 1;
61176 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
61177 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
61178 pPager->noLock = 1; /* Do no locking */
61179 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
61180 }
61181
61182 /* The following call to PagerSetPagesize() serves to set the value of
61183 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
61184 */
61185 if( rc==SQLITE_OK ){
61186 assert( pPager->memDb==0 );
61187 rc = sqlite3PagerSetPagesize(pPager, pPageSize: &szPageDflt, nReserve: -1);
61188 testcase( rc!=SQLITE_OK );
61189 }
61190
61191 /* Initialize the PCache object. */
61192 if( rc==SQLITE_OK ){
61193 nExtra = ROUND8(nExtra);
61194 assert( nExtra>=8 && nExtra<1000 );
61195 rc = sqlite3PcacheOpen(szPage: szPageDflt, szExtra: nExtra, bPurgeable: !memDb,
61196 xStress: !memDb?pagerStress:0, pStress: (void *)pPager, p: pPager->pPCache);
61197 }
61198
61199 /* If an error occurred above, free the Pager structure and close the file.
61200 */
61201 if( rc!=SQLITE_OK ){
61202 sqlite3OsClose(pId: pPager->fd);
61203 sqlite3PageFree(p: pPager->pTmpSpace);
61204 sqlite3_free(p: pPager);
61205 return rc;
61206 }
61207
61208 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
61209 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
61210
61211 pPager->useJournal = (u8)useJournal;
61212 /* pPager->stmtOpen = 0; */
61213 /* pPager->stmtInUse = 0; */
61214 /* pPager->nRef = 0; */
61215 /* pPager->stmtSize = 0; */
61216 /* pPager->stmtJSize = 0; */
61217 /* pPager->nPage = 0; */
61218 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
61219 /* pPager->state = PAGER_UNLOCK; */
61220 /* pPager->errMask = 0; */
61221 pPager->tempFile = (u8)tempFile;
61222 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
61223 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
61224 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
61225 pPager->exclusiveMode = (u8)tempFile;
61226 pPager->changeCountDone = pPager->tempFile;
61227 pPager->memDb = (u8)memDb;
61228 pPager->readOnly = (u8)readOnly;
61229 assert( useJournal || pPager->tempFile );
61230 sqlite3PagerSetFlags(pPager, pgFlags: (SQLITE_DEFAULT_SYNCHRONOUS+1)|PAGER_CACHESPILL);
61231 /* pPager->pFirst = 0; */
61232 /* pPager->pFirstSynced = 0; */
61233 /* pPager->pLast = 0; */
61234 pPager->nExtra = (u16)nExtra;
61235 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
61236 assert( isOpen(pPager->fd) || tempFile );
61237 setSectorSize(pPager);
61238 if( !useJournal ){
61239 pPager->journalMode = PAGER_JOURNALMODE_OFF;
61240 }else if( memDb || memJM ){
61241 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
61242 }
61243 /* pPager->xBusyHandler = 0; */
61244 /* pPager->pBusyHandlerArg = 0; */
61245 pPager->xReiniter = xReinit;
61246 setGetterMethod(pPager);
61247 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
61248 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
61249
61250 *ppPager = pPager;
61251 return SQLITE_OK;
61252}
61253
61254/*
61255** Return the sqlite3_file for the main database given the name
61256** of the corresponding WAL or Journal name as passed into
61257** xOpen.
61258*/
61259SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
61260 Pager *pPager;
61261 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61262 zName--;
61263 }
61264 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
61265 return pPager->fd;
61266}
61267
61268
61269/*
61270** This function is called after transitioning from PAGER_UNLOCK to
61271** PAGER_SHARED state. It tests if there is a hot journal present in
61272** the file-system for the given pager. A hot journal is one that
61273** needs to be played back. According to this function, a hot-journal
61274** file exists if the following criteria are met:
61275**
61276** * The journal file exists in the file system, and
61277** * No process holds a RESERVED or greater lock on the database file, and
61278** * The database file itself is greater than 0 bytes in size, and
61279** * The first byte of the journal file exists and is not 0x00.
61280**
61281** If the current size of the database file is 0 but a journal file
61282** exists, that is probably an old journal left over from a prior
61283** database with the same name. In this case the journal file is
61284** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
61285** is returned.
61286**
61287** This routine does not check if there is a super-journal filename
61288** at the end of the file. If there is, and that super-journal file
61289** does not exist, then the journal file is not really hot. In this
61290** case this routine will return a false-positive. The pager_playback()
61291** routine will discover that the journal file is not really hot and
61292** will not roll it back.
61293**
61294** If a hot-journal file is found to exist, *pExists is set to 1 and
61295** SQLITE_OK returned. If no hot-journal file is present, *pExists is
61296** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
61297** to determine whether or not a hot-journal file exists, the IO error
61298** code is returned and the value of *pExists is undefined.
61299*/
61300static int hasHotJournal(Pager *pPager, int *pExists){
61301 sqlite3_vfs * const pVfs = pPager->pVfs;
61302 int rc = SQLITE_OK; /* Return code */
61303 int exists = 1; /* True if a journal file is present */
61304 int jrnlOpen = !!isOpen(pPager->jfd);
61305
61306 assert( pPager->useJournal );
61307 assert( isOpen(pPager->fd) );
61308 assert( pPager->eState==PAGER_OPEN );
61309
61310 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
61311 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
61312 ));
61313
61314 *pExists = 0;
61315 if( !jrnlOpen ){
61316 rc = sqlite3OsAccess(pVfs, zPath: pPager->zJournal, SQLITE_ACCESS_EXISTS, pResOut: &exists);
61317 }
61318 if( rc==SQLITE_OK && exists ){
61319 int locked = 0; /* True if some process holds a RESERVED lock */
61320
61321 /* Race condition here: Another process might have been holding the
61322 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
61323 ** call above, but then delete the journal and drop the lock before
61324 ** we get to the following sqlite3OsCheckReservedLock() call. If that
61325 ** is the case, this routine might think there is a hot journal when
61326 ** in fact there is none. This results in a false-positive which will
61327 ** be dealt with by the playback routine. Ticket #3883.
61328 */
61329 rc = sqlite3OsCheckReservedLock(id: pPager->fd, pResOut: &locked);
61330 if( rc==SQLITE_OK && !locked ){
61331 Pgno nPage; /* Number of pages in database file */
61332
61333 assert( pPager->tempFile==0 );
61334 rc = pagerPagecount(pPager, pnPage: &nPage);
61335 if( rc==SQLITE_OK ){
61336 /* If the database is zero pages in size, that means that either (1) the
61337 ** journal is a remnant from a prior database with the same name where
61338 ** the database file but not the journal was deleted, or (2) the initial
61339 ** transaction that populates a new database is being rolled back.
61340 ** In either case, the journal file can be deleted. However, take care
61341 ** not to delete the journal file if it is already open due to
61342 ** journal_mode=PERSIST.
61343 */
61344 if( nPage==0 && !jrnlOpen ){
61345 sqlite3BeginBenignMalloc();
61346 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
61347 sqlite3OsDelete(pVfs, zPath: pPager->zJournal, dirSync: 0);
61348 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
61349 }
61350 sqlite3EndBenignMalloc();
61351 }else{
61352 /* The journal file exists and no other connection has a reserved
61353 ** or greater lock on the database file. Now check that there is
61354 ** at least one non-zero bytes at the start of the journal file.
61355 ** If there is, then we consider this journal to be hot. If not,
61356 ** it can be ignored.
61357 */
61358 if( !jrnlOpen ){
61359 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
61360 rc = sqlite3OsOpen(pVfs, zPath: pPager->zJournal, pFile: pPager->jfd, flags: f, pFlagsOut: &f);
61361 }
61362 if( rc==SQLITE_OK ){
61363 u8 first = 0;
61364 rc = sqlite3OsRead(id: pPager->jfd, pBuf: (void *)&first, amt: 1, offset: 0);
61365 if( rc==SQLITE_IOERR_SHORT_READ ){
61366 rc = SQLITE_OK;
61367 }
61368 if( !jrnlOpen ){
61369 sqlite3OsClose(pId: pPager->jfd);
61370 }
61371 *pExists = (first!=0);
61372 }else if( rc==SQLITE_CANTOPEN ){
61373 /* If we cannot open the rollback journal file in order to see if
61374 ** it has a zero header, that might be due to an I/O error, or
61375 ** it might be due to the race condition described above and in
61376 ** ticket #3883. Either way, assume that the journal is hot.
61377 ** This might be a false positive. But if it is, then the
61378 ** automatic journal playback and recovery mechanism will deal
61379 ** with it under an EXCLUSIVE lock where we do not need to
61380 ** worry so much with race conditions.
61381 */
61382 *pExists = 1;
61383 rc = SQLITE_OK;
61384 }
61385 }
61386 }
61387 }
61388 }
61389
61390 return rc;
61391}
61392
61393/*
61394** This function is called to obtain a shared lock on the database file.
61395** It is illegal to call sqlite3PagerGet() until after this function
61396** has been successfully called. If a shared-lock is already held when
61397** this function is called, it is a no-op.
61398**
61399** The following operations are also performed by this function.
61400**
61401** 1) If the pager is currently in PAGER_OPEN state (no lock held
61402** on the database file), then an attempt is made to obtain a
61403** SHARED lock on the database file. Immediately after obtaining
61404** the SHARED lock, the file-system is checked for a hot-journal,
61405** which is played back if present. Following any hot-journal
61406** rollback, the contents of the cache are validated by checking
61407** the 'change-counter' field of the database file header and
61408** discarded if they are found to be invalid.
61409**
61410** 2) If the pager is running in exclusive-mode, and there are currently
61411** no outstanding references to any pages, and is in the error state,
61412** then an attempt is made to clear the error state by discarding
61413** the contents of the page cache and rolling back any open journal
61414** file.
61415**
61416** If everything is successful, SQLITE_OK is returned. If an IO error
61417** occurs while locking the database, checking for a hot-journal file or
61418** rolling back a journal file, the IO error code is returned.
61419*/
61420SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
61421 int rc = SQLITE_OK; /* Return code */
61422
61423 /* This routine is only called from b-tree and only when there are no
61424 ** outstanding pages. This implies that the pager state should either
61425 ** be OPEN or READER. READER is only possible if the pager is or was in
61426 ** exclusive access mode. */
61427 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
61428 assert( assert_pager_state(pPager) );
61429 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
61430 assert( pPager->errCode==SQLITE_OK );
61431
61432 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
61433 int bHotJournal = 1; /* True if there exists a hot journal-file */
61434
61435 assert( !MEMDB );
61436 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
61437
61438 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
61439 if( rc!=SQLITE_OK ){
61440 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
61441 goto failed;
61442 }
61443
61444 /* If a journal file exists, and there is no RESERVED lock on the
61445 ** database file, then it either needs to be played back or deleted.
61446 */
61447 if( pPager->eLock<=SHARED_LOCK ){
61448 rc = hasHotJournal(pPager, pExists: &bHotJournal);
61449 }
61450 if( rc!=SQLITE_OK ){
61451 goto failed;
61452 }
61453 if( bHotJournal ){
61454 if( pPager->readOnly ){
61455 rc = SQLITE_READONLY_ROLLBACK;
61456 goto failed;
61457 }
61458
61459 /* Get an EXCLUSIVE lock on the database file. At this point it is
61460 ** important that a RESERVED lock is not obtained on the way to the
61461 ** EXCLUSIVE lock. If it were, another process might open the
61462 ** database file, detect the RESERVED lock, and conclude that the
61463 ** database is safe to read while this process is still rolling the
61464 ** hot-journal back.
61465 **
61466 ** Because the intermediate RESERVED lock is not requested, any
61467 ** other process attempting to access the database file will get to
61468 ** this point in the code and fail to obtain its own EXCLUSIVE lock
61469 ** on the database file.
61470 **
61471 ** Unless the pager is in locking_mode=exclusive mode, the lock is
61472 ** downgraded to SHARED_LOCK before this function returns.
61473 */
61474 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
61475 if( rc!=SQLITE_OK ){
61476 goto failed;
61477 }
61478
61479 /* If it is not already open and the file exists on disk, open the
61480 ** journal for read/write access. Write access is required because
61481 ** in exclusive-access mode the file descriptor will be kept open
61482 ** and possibly used for a transaction later on. Also, write-access
61483 ** is usually required to finalize the journal in journal_mode=persist
61484 ** mode (and also for journal_mode=truncate on some systems).
61485 **
61486 ** If the journal does not exist, it usually means that some
61487 ** other connection managed to get in and roll it back before
61488 ** this connection obtained the exclusive lock above. Or, it
61489 ** may mean that the pager was in the error-state when this
61490 ** function was called and the journal file does not exist.
61491 */
61492 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61493 sqlite3_vfs * const pVfs = pPager->pVfs;
61494 int bExists; /* True if journal file exists */
61495 rc = sqlite3OsAccess(
61496 pVfs, zPath: pPager->zJournal, SQLITE_ACCESS_EXISTS, pResOut: &bExists);
61497 if( rc==SQLITE_OK && bExists ){
61498 int fout = 0;
61499 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
61500 assert( !pPager->tempFile );
61501 rc = sqlite3OsOpen(pVfs, zPath: pPager->zJournal, pFile: pPager->jfd, flags: f, pFlagsOut: &fout);
61502 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61503 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
61504 rc = SQLITE_CANTOPEN_BKPT;
61505 sqlite3OsClose(pId: pPager->jfd);
61506 }
61507 }
61508 }
61509
61510 /* Playback and delete the journal. Drop the database write
61511 ** lock and reacquire the read lock. Purge the cache before
61512 ** playing back the hot-journal so that we don't end up with
61513 ** an inconsistent cache. Sync the hot journal before playing
61514 ** it back since the process that crashed and left the hot journal
61515 ** probably did not sync it and we are required to always sync
61516 ** the journal before playing it back.
61517 */
61518 if( isOpen(pPager->jfd) ){
61519 assert( rc==SQLITE_OK );
61520 rc = pagerSyncHotJournal(pPager);
61521 if( rc==SQLITE_OK ){
61522 rc = pager_playback(pPager, isHot: !pPager->tempFile);
61523 pPager->eState = PAGER_OPEN;
61524 }
61525 }else if( !pPager->exclusiveMode ){
61526 pagerUnlockDb(pPager, SHARED_LOCK);
61527 }
61528
61529 if( rc!=SQLITE_OK ){
61530 /* This branch is taken if an error occurs while trying to open
61531 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
61532 ** pager_unlock() routine will be called before returning to unlock
61533 ** the file. If the unlock attempt fails, then Pager.eLock must be
61534 ** set to UNKNOWN_LOCK (see the comment above the #define for
61535 ** UNKNOWN_LOCK above for an explanation).
61536 **
61537 ** In order to get pager_unlock() to do this, set Pager.eState to
61538 ** PAGER_ERROR now. This is not actually counted as a transition
61539 ** to ERROR state in the state diagram at the top of this file,
61540 ** since we know that the same call to pager_unlock() will very
61541 ** shortly transition the pager object to the OPEN state. Calling
61542 ** assert_pager_state() would fail now, as it should not be possible
61543 ** to be in ERROR state when there are zero outstanding page
61544 ** references.
61545 */
61546 pager_error(pPager, rc);
61547 goto failed;
61548 }
61549
61550 assert( pPager->eState==PAGER_OPEN );
61551 assert( (pPager->eLock==SHARED_LOCK)
61552 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
61553 );
61554 }
61555
61556 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
61557 /* The shared-lock has just been acquired then check to
61558 ** see if the database has been modified. If the database has changed,
61559 ** flush the cache. The hasHeldSharedLock flag prevents this from
61560 ** occurring on the very first access to a file, in order to save a
61561 ** single unnecessary sqlite3OsRead() call at the start-up.
61562 **
61563 ** Database changes are detected by looking at 15 bytes beginning
61564 ** at offset 24 into the file. The first 4 of these 16 bytes are
61565 ** a 32-bit counter that is incremented with each change. The
61566 ** other bytes change randomly with each file change when
61567 ** a codec is in use.
61568 **
61569 ** There is a vanishingly small chance that a change will not be
61570 ** detected. The chance of an undetected change is so small that
61571 ** it can be neglected.
61572 */
61573 char dbFileVers[sizeof(pPager->dbFileVers)];
61574
61575 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
61576 rc = sqlite3OsRead(id: pPager->fd, pBuf: &dbFileVers, amt: sizeof(dbFileVers), offset: 24);
61577 if( rc!=SQLITE_OK ){
61578 if( rc!=SQLITE_IOERR_SHORT_READ ){
61579 goto failed;
61580 }
61581 memset(s: dbFileVers, c: 0, n: sizeof(dbFileVers));
61582 }
61583
61584 if( memcmp(s1: pPager->dbFileVers, s2: dbFileVers, n: sizeof(dbFileVers))!=0 ){
61585 pager_reset(pPager);
61586
61587 /* Unmap the database file. It is possible that external processes
61588 ** may have truncated the database file and then extended it back
61589 ** to its original size while this process was not holding a lock.
61590 ** In this case there may exist a Pager.pMap mapping that appears
61591 ** to be the right size but is not actually valid. Avoid this
61592 ** possibility by unmapping the db here. */
61593 if( USEFETCH(pPager) ){
61594 sqlite3OsUnfetch(id: pPager->fd, iOff: 0, p: 0);
61595 }
61596 }
61597 }
61598
61599 /* If there is a WAL file in the file-system, open this database in WAL
61600 ** mode. Otherwise, the following function call is a no-op.
61601 */
61602 rc = pagerOpenWalIfPresent(pPager);
61603#ifndef SQLITE_OMIT_WAL
61604 assert( pPager->pWal==0 || rc==SQLITE_OK );
61605#endif
61606 }
61607
61608 if( pagerUseWal(pPager) ){
61609 assert( rc==SQLITE_OK );
61610 rc = pagerBeginReadTransaction(pPager);
61611 }
61612
61613 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
61614 rc = pagerPagecount(pPager, pnPage: &pPager->dbSize);
61615 }
61616
61617 failed:
61618 if( rc!=SQLITE_OK ){
61619 assert( !MEMDB );
61620 pager_unlock(pPager);
61621 assert( pPager->eState==PAGER_OPEN );
61622 }else{
61623 pPager->eState = PAGER_READER;
61624 pPager->hasHeldSharedLock = 1;
61625 }
61626 return rc;
61627}
61628
61629/*
61630** If the reference count has reached zero, rollback any active
61631** transaction and unlock the pager.
61632**
61633** Except, in locking_mode=EXCLUSIVE when there is nothing to in
61634** the rollback journal, the unlock is not performed and there is
61635** nothing to rollback, so this routine is a no-op.
61636*/
61637static void pagerUnlockIfUnused(Pager *pPager){
61638 if( sqlite3PcacheRefCount(pCache: pPager->pPCache)==0 ){
61639 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
61640 pagerUnlockAndRollback(pPager);
61641 }
61642}
61643
61644/*
61645** The page getter methods each try to acquire a reference to a
61646** page with page number pgno. If the requested reference is
61647** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
61648**
61649** There are different implementations of the getter method depending
61650** on the current state of the pager.
61651**
61652** getPageNormal() -- The normal getter
61653** getPageError() -- Used if the pager is in an error state
61654** getPageMmap() -- Used if memory-mapped I/O is enabled
61655**
61656** If the requested page is already in the cache, it is returned.
61657** Otherwise, a new page object is allocated and populated with data
61658** read from the database file. In some cases, the pcache module may
61659** choose not to allocate a new page object and may reuse an existing
61660** object with no outstanding references.
61661**
61662** The extra data appended to a page is always initialized to zeros the
61663** first time a page is loaded into memory. If the page requested is
61664** already in the cache when this function is called, then the extra
61665** data is left as it was when the page object was last used.
61666**
61667** If the database image is smaller than the requested page or if
61668** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
61669** requested page is not already stored in the cache, then no
61670** actual disk read occurs. In this case the memory image of the
61671** page is initialized to all zeros.
61672**
61673** If PAGER_GET_NOCONTENT is true, it means that we do not care about
61674** the contents of the page. This occurs in two scenarios:
61675**
61676** a) When reading a free-list leaf page from the database, and
61677**
61678** b) When a savepoint is being rolled back and we need to load
61679** a new page into the cache to be filled with the data read
61680** from the savepoint journal.
61681**
61682** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
61683** of being read from the database. Additionally, the bits corresponding
61684** to pgno in Pager.pInJournal (bitvec of pages already written to the
61685** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
61686** savepoints are set. This means if the page is made writable at any
61687** point in the future, using a call to sqlite3PagerWrite(), its contents
61688** will not be journaled. This saves IO.
61689**
61690** The acquisition might fail for several reasons. In all cases,
61691** an appropriate error code is returned and *ppPage is set to NULL.
61692**
61693** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
61694** to find a page in the in-memory cache first. If the page is not already
61695** in memory, this routine goes to disk to read it in whereas Lookup()
61696** just returns 0. This routine acquires a read-lock the first time it
61697** has to go to disk, and could also playback an old journal if necessary.
61698** Since Lookup() never goes to disk, it never has to deal with locks
61699** or journal files.
61700*/
61701static int getPageNormal(
61702 Pager *pPager, /* The pager open on the database file */
61703 Pgno pgno, /* Page number to fetch */
61704 DbPage **ppPage, /* Write a pointer to the page here */
61705 int flags /* PAGER_GET_XXX flags */
61706){
61707 int rc = SQLITE_OK;
61708 PgHdr *pPg;
61709 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
61710 sqlite3_pcache_page *pBase;
61711
61712 assert( pPager->errCode==SQLITE_OK );
61713 assert( pPager->eState>=PAGER_READER );
61714 assert( assert_pager_state(pPager) );
61715 assert( pPager->hasHeldSharedLock==1 );
61716
61717 if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
61718 pBase = sqlite3PcacheFetch(pCache: pPager->pPCache, pgno, createFlag: 3);
61719 if( pBase==0 ){
61720 pPg = 0;
61721 rc = sqlite3PcacheFetchStress(pCache: pPager->pPCache, pgno, ppPage: &pBase);
61722 if( rc!=SQLITE_OK ) goto pager_acquire_err;
61723 if( pBase==0 ){
61724 rc = SQLITE_NOMEM_BKPT;
61725 goto pager_acquire_err;
61726 }
61727 }
61728 pPg = *ppPage = sqlite3PcacheFetchFinish(pCache: pPager->pPCache, pgno, pPage: pBase);
61729 assert( pPg==(*ppPage) );
61730 assert( pPg->pgno==pgno );
61731 assert( pPg->pPager==pPager || pPg->pPager==0 );
61732
61733 noContent = (flags & PAGER_GET_NOCONTENT)!=0;
61734 if( pPg->pPager && !noContent ){
61735 /* In this case the pcache already contains an initialized copy of
61736 ** the page. Return without further ado. */
61737 assert( pgno!=PAGER_SJ_PGNO(pPager) );
61738 pPager->aStat[PAGER_STAT_HIT]++;
61739 return SQLITE_OK;
61740
61741 }else{
61742 /* The pager cache has created a new page. Its content needs to
61743 ** be initialized. But first some error checks:
61744 **
61745 ** (*) obsolete. Was: maximum page number is 2^31
61746 ** (2) Never try to fetch the locking page
61747 */
61748 if( pgno==PAGER_SJ_PGNO(pPager) ){
61749 rc = SQLITE_CORRUPT_BKPT;
61750 goto pager_acquire_err;
61751 }
61752
61753 pPg->pPager = pPager;
61754
61755 assert( !isOpen(pPager->fd) || !MEMDB );
61756 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
61757 if( pgno>pPager->mxPgno ){
61758 rc = SQLITE_FULL;
61759 if( pgno<=pPager->dbSize ){
61760 sqlite3PcacheRelease(p: pPg);
61761 pPg = 0;
61762 }
61763 goto pager_acquire_err;
61764 }
61765 if( noContent ){
61766 /* Failure to set the bits in the InJournal bit-vectors is benign.
61767 ** It merely means that we might do some extra work to journal a
61768 ** page that does not need to be journaled. Nevertheless, be sure
61769 ** to test the case where a malloc error occurs while trying to set
61770 ** a bit in a bit vector.
61771 */
61772 sqlite3BeginBenignMalloc();
61773 if( pgno<=pPager->dbOrigSize ){
61774 TESTONLY( rc = ) sqlite3BitvecSet(p: pPager->pInJournal, i: pgno);
61775 testcase( rc==SQLITE_NOMEM );
61776 }
61777 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
61778 testcase( rc==SQLITE_NOMEM );
61779 sqlite3EndBenignMalloc();
61780 }
61781 memset(s: pPg->pData, c: 0, n: pPager->pageSize);
61782 IOTRACE(("ZERO %p %d\n", pPager, pgno));
61783 }else{
61784 assert( pPg->pPager==pPager );
61785 pPager->aStat[PAGER_STAT_MISS]++;
61786 rc = readDbPage(pPg);
61787 if( rc!=SQLITE_OK ){
61788 goto pager_acquire_err;
61789 }
61790 }
61791 pager_set_pagehash(pPg);
61792 }
61793 return SQLITE_OK;
61794
61795pager_acquire_err:
61796 assert( rc!=SQLITE_OK );
61797 if( pPg ){
61798 sqlite3PcacheDrop(p: pPg);
61799 }
61800 pagerUnlockIfUnused(pPager);
61801 *ppPage = 0;
61802 return rc;
61803}
61804
61805#if SQLITE_MAX_MMAP_SIZE>0
61806/* The page getter for when memory-mapped I/O is enabled */
61807static int getPageMMap(
61808 Pager *pPager, /* The pager open on the database file */
61809 Pgno pgno, /* Page number to fetch */
61810 DbPage **ppPage, /* Write a pointer to the page here */
61811 int flags /* PAGER_GET_XXX flags */
61812){
61813 int rc = SQLITE_OK;
61814 PgHdr *pPg = 0;
61815 u32 iFrame = 0; /* Frame to read from WAL file */
61816
61817 /* It is acceptable to use a read-only (mmap) page for any page except
61818 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
61819 ** flag was specified by the caller. And so long as the db is not a
61820 ** temporary or in-memory database. */
61821 const int bMmapOk = (pgno>1
61822 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
61823 );
61824
61825 assert( USEFETCH(pPager) );
61826
61827 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
61828 ** allows the compiler optimizer to reuse the results of the "pgno>1"
61829 ** test in the previous statement, and avoid testing pgno==0 in the
61830 ** common case where pgno is large. */
61831 if( pgno<=1 && pgno==0 ){
61832 return SQLITE_CORRUPT_BKPT;
61833 }
61834 assert( pPager->eState>=PAGER_READER );
61835 assert( assert_pager_state(pPager) );
61836 assert( pPager->hasHeldSharedLock==1 );
61837 assert( pPager->errCode==SQLITE_OK );
61838
61839 if( bMmapOk && pagerUseWal(pPager) ){
61840 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
61841 if( rc!=SQLITE_OK ){
61842 *ppPage = 0;
61843 return rc;
61844 }
61845 }
61846 if( bMmapOk && iFrame==0 ){
61847 void *pData = 0;
61848 rc = sqlite3OsFetch(id: pPager->fd,
61849 iOff: (i64)(pgno-1) * pPager->pageSize, iAmt: pPager->pageSize, pp: &pData
61850 );
61851 if( rc==SQLITE_OK && pData ){
61852 if( pPager->eState>PAGER_READER || pPager->tempFile ){
61853 pPg = sqlite3PagerLookup(pPager, pgno);
61854 }
61855 if( pPg==0 ){
61856 rc = pagerAcquireMapPage(pPager, pgno, pData, ppPage: &pPg);
61857 }else{
61858 sqlite3OsUnfetch(id: pPager->fd, iOff: (i64)(pgno-1)*pPager->pageSize, p: pData);
61859 }
61860 if( pPg ){
61861 assert( rc==SQLITE_OK );
61862 *ppPage = pPg;
61863 return SQLITE_OK;
61864 }
61865 }
61866 if( rc!=SQLITE_OK ){
61867 *ppPage = 0;
61868 return rc;
61869 }
61870 }
61871 return getPageNormal(pPager, pgno, ppPage, flags);
61872}
61873#endif /* SQLITE_MAX_MMAP_SIZE>0 */
61874
61875/* The page getter method for when the pager is an error state */
61876static int getPageError(
61877 Pager *pPager, /* The pager open on the database file */
61878 Pgno pgno, /* Page number to fetch */
61879 DbPage **ppPage, /* Write a pointer to the page here */
61880 int flags /* PAGER_GET_XXX flags */
61881){
61882 UNUSED_PARAMETER(pgno);
61883 UNUSED_PARAMETER(flags);
61884 assert( pPager->errCode!=SQLITE_OK );
61885 *ppPage = 0;
61886 return pPager->errCode;
61887}
61888
61889
61890/* Dispatch all page fetch requests to the appropriate getter method.
61891*/
61892SQLITE_PRIVATE int sqlite3PagerGet(
61893 Pager *pPager, /* The pager open on the database file */
61894 Pgno pgno, /* Page number to fetch */
61895 DbPage **ppPage, /* Write a pointer to the page here */
61896 int flags /* PAGER_GET_XXX flags */
61897){
61898 /* printf("PAGE %u\n", pgno); fflush(stdout); */
61899 return pPager->xGet(pPager, pgno, ppPage, flags);
61900}
61901
61902/*
61903** Acquire a page if it is already in the in-memory cache. Do
61904** not read the page from disk. Return a pointer to the page,
61905** or 0 if the page is not in cache.
61906**
61907** See also sqlite3PagerGet(). The difference between this routine
61908** and sqlite3PagerGet() is that _get() will go to the disk and read
61909** in the page if the page is not already in cache. This routine
61910** returns NULL if the page is not in cache or if a disk I/O error
61911** has ever happened.
61912*/
61913SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
61914 sqlite3_pcache_page *pPage;
61915 assert( pPager!=0 );
61916 assert( pgno!=0 );
61917 assert( pPager->pPCache!=0 );
61918 pPage = sqlite3PcacheFetch(pCache: pPager->pPCache, pgno, createFlag: 0);
61919 assert( pPage==0 || pPager->hasHeldSharedLock );
61920 if( pPage==0 ) return 0;
61921 return sqlite3PcacheFetchFinish(pCache: pPager->pPCache, pgno, pPage);
61922}
61923
61924/*
61925** Release a page reference.
61926**
61927** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be used
61928** if we know that the page being released is not the last reference to page1.
61929** The btree layer always holds page1 open until the end, so these first
61930** two routines can be used to release any page other than BtShared.pPage1.
61931** The assert() at tag-20230419-2 proves that this constraint is always
61932** honored.
61933**
61934** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
61935** checks the total number of outstanding pages and if the number of
61936** pages reaches zero it drops the database lock.
61937*/
61938SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
61939 TESTONLY( Pager *pPager = pPg->pPager; )
61940 assert( pPg!=0 );
61941 if( pPg->flags & PGHDR_MMAP ){
61942 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
61943 pagerReleaseMapPage(pPg);
61944 }else{
61945 sqlite3PcacheRelease(p: pPg);
61946 }
61947 /* Do not use this routine to release the last reference to page1 */
61948 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */
61949}
61950SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
61951 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
61952}
61953SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
61954 Pager *pPager;
61955 assert( pPg!=0 );
61956 assert( pPg->pgno==1 );
61957 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
61958 pPager = pPg->pPager;
61959 sqlite3PcacheRelease(p: pPg);
61960 pagerUnlockIfUnused(pPager);
61961}
61962
61963/*
61964** This function is called at the start of every write transaction.
61965** There must already be a RESERVED or EXCLUSIVE lock on the database
61966** file when this routine is called.
61967**
61968** Open the journal file for pager pPager and write a journal header
61969** to the start of it. If there are active savepoints, open the sub-journal
61970** as well. This function is only used when the journal file is being
61971** opened to write a rollback log for a transaction. It is not used
61972** when opening a hot journal file to roll it back.
61973**
61974** If the journal file is already open (as it may be in exclusive mode),
61975** then this function just writes a journal header to the start of the
61976** already open file.
61977**
61978** Whether or not the journal file is opened by this function, the
61979** Pager.pInJournal bitvec structure is allocated.
61980**
61981** Return SQLITE_OK if everything is successful. Otherwise, return
61982** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
61983** an IO error code if opening or writing the journal file fails.
61984*/
61985static int pager_open_journal(Pager *pPager){
61986 int rc = SQLITE_OK; /* Return code */
61987 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
61988
61989 assert( pPager->eState==PAGER_WRITER_LOCKED );
61990 assert( assert_pager_state(pPager) );
61991 assert( pPager->pInJournal==0 );
61992
61993 /* If already in the error state, this function is a no-op. But on
61994 ** the other hand, this routine is never called if we are already in
61995 ** an error state. */
61996 if( NEVER(pPager->errCode) ) return pPager->errCode;
61997
61998 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61999 pPager->pInJournal = sqlite3BitvecCreate(iSize: pPager->dbSize);
62000 if( pPager->pInJournal==0 ){
62001 return SQLITE_NOMEM_BKPT;
62002 }
62003
62004 /* Open the journal file if it is not already open. */
62005 if( !isOpen(pPager->jfd) ){
62006 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
62007 sqlite3MemJournalOpen(pPager->jfd);
62008 }else{
62009 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
62010 int nSpill;
62011
62012 if( pPager->tempFile ){
62013 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
62014 flags |= SQLITE_OPEN_EXCLUSIVE;
62015 nSpill = sqlite3Config.nStmtSpill;
62016 }else{
62017 flags |= SQLITE_OPEN_MAIN_JOURNAL;
62018 nSpill = jrnlBufferSize(pPager);
62019 }
62020
62021 /* Verify that the database still has the same name as it did when
62022 ** it was originally opened. */
62023 rc = databaseIsUnmoved(pPager);
62024 if( rc==SQLITE_OK ){
62025 rc = sqlite3JournalOpen (
62026 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
62027 );
62028 }
62029 }
62030 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
62031 }
62032
62033
62034 /* Write the first journal header to the journal file and open
62035 ** the sub-journal if necessary.
62036 */
62037 if( rc==SQLITE_OK ){
62038 /* TODO: Check if all of these are really required. */
62039 pPager->nRec = 0;
62040 pPager->journalOff = 0;
62041 pPager->setSuper = 0;
62042 pPager->journalHdr = 0;
62043 rc = writeJournalHdr(pPager);
62044 }
62045 }
62046
62047 if( rc!=SQLITE_OK ){
62048 sqlite3BitvecDestroy(p: pPager->pInJournal);
62049 pPager->pInJournal = 0;
62050 pPager->journalOff = 0;
62051 }else{
62052 assert( pPager->eState==PAGER_WRITER_LOCKED );
62053 pPager->eState = PAGER_WRITER_CACHEMOD;
62054 }
62055
62056 return rc;
62057}
62058
62059/*
62060** Begin a write-transaction on the specified pager object. If a
62061** write-transaction has already been opened, this function is a no-op.
62062**
62063** If the exFlag argument is false, then acquire at least a RESERVED
62064** lock on the database file. If exFlag is true, then acquire at least
62065** an EXCLUSIVE lock. If such a lock is already held, no locking
62066** functions need be called.
62067**
62068** If the subjInMemory argument is non-zero, then any sub-journal opened
62069** within this transaction will be opened as an in-memory file. This
62070** has no effect if the sub-journal is already opened (as it may be when
62071** running in exclusive mode) or if the transaction does not require a
62072** sub-journal. If the subjInMemory argument is zero, then any required
62073** sub-journal is implemented in-memory if pPager is an in-memory database,
62074** or using a temporary file otherwise.
62075*/
62076SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
62077 int rc = SQLITE_OK;
62078
62079 if( pPager->errCode ) return pPager->errCode;
62080 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
62081 pPager->subjInMemory = (u8)subjInMemory;
62082
62083 if( pPager->eState==PAGER_READER ){
62084 assert( pPager->pInJournal==0 );
62085
62086 if( pagerUseWal(pPager) ){
62087 /* If the pager is configured to use locking_mode=exclusive, and an
62088 ** exclusive lock on the database is not already held, obtain it now.
62089 */
62090 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pWal: pPager->pWal, op: -1) ){
62091 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
62092 if( rc!=SQLITE_OK ){
62093 return rc;
62094 }
62095 (void)sqlite3WalExclusiveMode(pWal: pPager->pWal, op: 1);
62096 }
62097
62098 /* Grab the write lock on the log file. If successful, upgrade to
62099 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
62100 ** The busy-handler is not invoked if another connection already
62101 ** holds the write-lock. If possible, the upper layer will call it.
62102 */
62103 rc = sqlite3WalBeginWriteTransaction(pWal: pPager->pWal);
62104 }else{
62105 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
62106 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
62107 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
62108 ** lock, but not when obtaining the RESERVED lock.
62109 */
62110 rc = pagerLockDb(pPager, RESERVED_LOCK);
62111 if( rc==SQLITE_OK && exFlag ){
62112 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
62113 }
62114 }
62115
62116 if( rc==SQLITE_OK ){
62117 /* Change to WRITER_LOCKED state.
62118 **
62119 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
62120 ** when it has an open transaction, but never to DBMOD or FINISHED.
62121 ** This is because in those states the code to roll back savepoint
62122 ** transactions may copy data from the sub-journal into the database
62123 ** file as well as into the page cache. Which would be incorrect in
62124 ** WAL mode.
62125 */
62126 pPager->eState = PAGER_WRITER_LOCKED;
62127 pPager->dbHintSize = pPager->dbSize;
62128 pPager->dbFileSize = pPager->dbSize;
62129 pPager->dbOrigSize = pPager->dbSize;
62130 pPager->journalOff = 0;
62131 }
62132
62133 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
62134 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
62135 assert( assert_pager_state(pPager) );
62136 }
62137
62138 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
62139 return rc;
62140}
62141
62142/*
62143** Write page pPg onto the end of the rollback journal.
62144*/
62145static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
62146 Pager *pPager = pPg->pPager;
62147 int rc;
62148 u32 cksum;
62149 char *pData2;
62150 i64 iOff = pPager->journalOff;
62151
62152 /* We should never write to the journal file the page that
62153 ** contains the database locks. The following assert verifies
62154 ** that we do not. */
62155 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
62156
62157 assert( pPager->journalHdr<=pPager->journalOff );
62158 pData2 = pPg->pData;
62159 cksum = pager_cksum(pPager, aData: (u8*)pData2);
62160
62161 /* Even if an IO or diskfull error occurs while journalling the
62162 ** page in the block above, set the need-sync flag for the page.
62163 ** Otherwise, when the transaction is rolled back, the logic in
62164 ** playback_one_page() will think that the page needs to be restored
62165 ** in the database file. And if an IO error occurs while doing so,
62166 ** then corruption may follow.
62167 */
62168 pPg->flags |= PGHDR_NEED_SYNC;
62169
62170 rc = write32bits(fd: pPager->jfd, offset: iOff, val: pPg->pgno);
62171 if( rc!=SQLITE_OK ) return rc;
62172 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: pData2, amt: pPager->pageSize, offset: iOff+4);
62173 if( rc!=SQLITE_OK ) return rc;
62174 rc = write32bits(fd: pPager->jfd, offset: iOff+pPager->pageSize+4, val: cksum);
62175 if( rc!=SQLITE_OK ) return rc;
62176
62177 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
62178 pPager->journalOff, pPager->pageSize));
62179 PAGER_INCR(sqlite3_pager_writej_count);
62180 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
62181 PAGERID(pPager), pPg->pgno,
62182 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
62183
62184 pPager->journalOff += 8 + pPager->pageSize;
62185 pPager->nRec++;
62186 assert( pPager->pInJournal!=0 );
62187 rc = sqlite3BitvecSet(p: pPager->pInJournal, i: pPg->pgno);
62188 testcase( rc==SQLITE_NOMEM );
62189 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
62190 rc |= addToSavepointBitvecs(pPager, pgno: pPg->pgno);
62191 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
62192 return rc;
62193}
62194
62195/*
62196** Mark a single data page as writeable. The page is written into the
62197** main journal or sub-journal as required. If the page is written into
62198** one of the journals, the corresponding bit is set in the
62199** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
62200** of any open savepoints as appropriate.
62201*/
62202static int pager_write(PgHdr *pPg){
62203 Pager *pPager = pPg->pPager;
62204 int rc = SQLITE_OK;
62205
62206 /* This routine is not called unless a write-transaction has already
62207 ** been started. The journal file may or may not be open at this point.
62208 ** It is never called in the ERROR state.
62209 */
62210 assert( pPager->eState==PAGER_WRITER_LOCKED
62211 || pPager->eState==PAGER_WRITER_CACHEMOD
62212 || pPager->eState==PAGER_WRITER_DBMOD
62213 );
62214 assert( assert_pager_state(pPager) );
62215 assert( pPager->errCode==0 );
62216 assert( pPager->readOnly==0 );
62217 CHECK_PAGE(pPg);
62218
62219 /* The journal file needs to be opened. Higher level routines have already
62220 ** obtained the necessary locks to begin the write-transaction, but the
62221 ** rollback journal might not yet be open. Open it now if this is the case.
62222 **
62223 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
62224 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
62225 ** an error might occur and the pager would end up in WRITER_LOCKED state
62226 ** with pages marked as dirty in the cache.
62227 */
62228 if( pPager->eState==PAGER_WRITER_LOCKED ){
62229 rc = pager_open_journal(pPager);
62230 if( rc!=SQLITE_OK ) return rc;
62231 }
62232 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
62233 assert( assert_pager_state(pPager) );
62234
62235 /* Mark the page that is about to be modified as dirty. */
62236 sqlite3PcacheMakeDirty(p: pPg);
62237
62238 /* If a rollback journal is in use, them make sure the page that is about
62239 ** to change is in the rollback journal, or if the page is a new page off
62240 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
62241 */
62242 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
62243 if( pPager->pInJournal!=0
62244 && sqlite3BitvecTestNotNull(p: pPager->pInJournal, i: pPg->pgno)==0
62245 ){
62246 assert( pagerUseWal(pPager)==0 );
62247 if( pPg->pgno<=pPager->dbOrigSize ){
62248 rc = pagerAddPageToRollbackJournal(pPg);
62249 if( rc!=SQLITE_OK ){
62250 return rc;
62251 }
62252 }else{
62253 if( pPager->eState!=PAGER_WRITER_DBMOD ){
62254 pPg->flags |= PGHDR_NEED_SYNC;
62255 }
62256 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
62257 PAGERID(pPager), pPg->pgno,
62258 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
62259 }
62260 }
62261
62262 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
62263 ** and before writing the page into the rollback journal. Wait until now,
62264 ** after the page has been successfully journalled, before setting the
62265 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
62266 */
62267 pPg->flags |= PGHDR_WRITEABLE;
62268
62269 /* If the statement journal is open and the page is not in it,
62270 ** then write the page into the statement journal.
62271 */
62272 if( pPager->nSavepoint>0 ){
62273 rc = subjournalPageIfRequired(pPg);
62274 }
62275
62276 /* Update the database size and return. */
62277 if( pPager->dbSize<pPg->pgno ){
62278 pPager->dbSize = pPg->pgno;
62279 }
62280 return rc;
62281}
62282
62283/*
62284** This is a variant of sqlite3PagerWrite() that runs when the sector size
62285** is larger than the page size. SQLite makes the (reasonable) assumption that
62286** all bytes of a sector are written together by hardware. Hence, all bytes of
62287** a sector need to be journalled in case of a power loss in the middle of
62288** a write.
62289**
62290** Usually, the sector size is less than or equal to the page size, in which
62291** case pages can be individually written. This routine only runs in the
62292** exceptional case where the page size is smaller than the sector size.
62293*/
62294static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
62295 int rc = SQLITE_OK; /* Return code */
62296 Pgno nPageCount; /* Total number of pages in database file */
62297 Pgno pg1; /* First page of the sector pPg is located on. */
62298 int nPage = 0; /* Number of pages starting at pg1 to journal */
62299 int ii; /* Loop counter */
62300 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
62301 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
62302 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
62303
62304 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
62305 ** a journal header to be written between the pages journaled by
62306 ** this function.
62307 */
62308 assert( !MEMDB );
62309 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
62310 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
62311
62312 /* This trick assumes that both the page-size and sector-size are
62313 ** an integer power of 2. It sets variable pg1 to the identifier
62314 ** of the first page of the sector pPg is located on.
62315 */
62316 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
62317
62318 nPageCount = pPager->dbSize;
62319 if( pPg->pgno>nPageCount ){
62320 nPage = (pPg->pgno - pg1)+1;
62321 }else if( (pg1+nPagePerSector-1)>nPageCount ){
62322 nPage = nPageCount+1-pg1;
62323 }else{
62324 nPage = nPagePerSector;
62325 }
62326 assert(nPage>0);
62327 assert(pg1<=pPg->pgno);
62328 assert((pg1+nPage)>pPg->pgno);
62329
62330 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
62331 Pgno pg = pg1+ii;
62332 PgHdr *pPage;
62333 if( pg==pPg->pgno || !sqlite3BitvecTest(p: pPager->pInJournal, i: pg) ){
62334 if( pg!=PAGER_SJ_PGNO(pPager) ){
62335 rc = sqlite3PagerGet(pPager, pgno: pg, ppPage: &pPage, flags: 0);
62336 if( rc==SQLITE_OK ){
62337 rc = pager_write(pPg: pPage);
62338 if( pPage->flags&PGHDR_NEED_SYNC ){
62339 needSync = 1;
62340 }
62341 sqlite3PagerUnrefNotNull(pPg: pPage);
62342 }
62343 }
62344 }else if( (pPage = sqlite3PagerLookup(pPager, pgno: pg))!=0 ){
62345 if( pPage->flags&PGHDR_NEED_SYNC ){
62346 needSync = 1;
62347 }
62348 sqlite3PagerUnrefNotNull(pPg: pPage);
62349 }
62350 }
62351
62352 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
62353 ** starting at pg1, then it needs to be set for all of them. Because
62354 ** writing to any of these nPage pages may damage the others, the
62355 ** journal file must contain sync()ed copies of all of them
62356 ** before any of them can be written out to the database file.
62357 */
62358 if( rc==SQLITE_OK && needSync ){
62359 assert( !MEMDB );
62360 for(ii=0; ii<nPage; ii++){
62361 PgHdr *pPage = sqlite3PagerLookup(pPager, pgno: pg1+ii);
62362 if( pPage ){
62363 pPage->flags |= PGHDR_NEED_SYNC;
62364 sqlite3PagerUnrefNotNull(pPg: pPage);
62365 }
62366 }
62367 }
62368
62369 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
62370 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
62371 return rc;
62372}
62373
62374/*
62375** Mark a data page as writeable. This routine must be called before
62376** making changes to a page. The caller must check the return value
62377** of this function and be careful not to change any page data unless
62378** this routine returns SQLITE_OK.
62379**
62380** The difference between this function and pager_write() is that this
62381** function also deals with the special case where 2 or more pages
62382** fit on a single disk sector. In this case all co-resident pages
62383** must have been written to the journal file before returning.
62384**
62385** If an error occurs, SQLITE_NOMEM or an IO error code is returned
62386** as appropriate. Otherwise, SQLITE_OK.
62387*/
62388SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
62389 Pager *pPager = pPg->pPager;
62390 assert( (pPg->flags & PGHDR_MMAP)==0 );
62391 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62392 assert( assert_pager_state(pPager) );
62393 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
62394 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
62395 return SQLITE_OK;
62396 }else if( pPager->errCode ){
62397 return pPager->errCode;
62398 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
62399 assert( pPager->tempFile==0 );
62400 return pagerWriteLargeSector(pPg);
62401 }else{
62402 return pager_write(pPg);
62403 }
62404}
62405
62406/*
62407** Return TRUE if the page given in the argument was previously passed
62408** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
62409** to change the content of the page.
62410*/
62411#ifndef NDEBUG
62412SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
62413 return pPg->flags & PGHDR_WRITEABLE;
62414}
62415#endif
62416
62417/*
62418** A call to this routine tells the pager that it is not necessary to
62419** write the information on page pPg back to the disk, even though
62420** that page might be marked as dirty. This happens, for example, when
62421** the page has been added as a leaf of the freelist and so its
62422** content no longer matters.
62423**
62424** The overlying software layer calls this routine when all of the data
62425** on the given page is unused. The pager marks the page as clean so
62426** that it does not get written to disk.
62427**
62428** Tests show that this optimization can quadruple the speed of large
62429** DELETE operations.
62430**
62431** This optimization cannot be used with a temp-file, as the page may
62432** have been dirty at the start of the transaction. In that case, if
62433** memory pressure forces page pPg out of the cache, the data does need
62434** to be written out to disk so that it may be read back in if the
62435** current transaction is rolled back.
62436*/
62437SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
62438 Pager *pPager = pPg->pPager;
62439 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
62440 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
62441 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
62442 pPg->flags |= PGHDR_DONT_WRITE;
62443 pPg->flags &= ~PGHDR_WRITEABLE;
62444 testcase( pPg->flags & PGHDR_NEED_SYNC );
62445 pager_set_pagehash(pPg);
62446 }
62447}
62448
62449/*
62450** This routine is called to increment the value of the database file
62451** change-counter, stored as a 4-byte big-endian integer starting at
62452** byte offset 24 of the pager file. The secondary change counter at
62453** 92 is also updated, as is the SQLite version number at offset 96.
62454**
62455** But this only happens if the pPager->changeCountDone flag is false.
62456** To avoid excess churning of page 1, the update only happens once.
62457** See also the pager_write_changecounter() routine that does an
62458** unconditional update of the change counters.
62459**
62460** If the isDirectMode flag is zero, then this is done by calling
62461** sqlite3PagerWrite() on page 1, then modifying the contents of the
62462** page data. In this case the file will be updated when the current
62463** transaction is committed.
62464**
62465** The isDirectMode flag may only be non-zero if the library was compiled
62466** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
62467** if isDirect is non-zero, then the database file is updated directly
62468** by writing an updated version of page 1 using a call to the
62469** sqlite3OsWrite() function.
62470*/
62471static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
62472 int rc = SQLITE_OK;
62473
62474 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62475 || pPager->eState==PAGER_WRITER_DBMOD
62476 );
62477 assert( assert_pager_state(pPager) );
62478
62479 /* Declare and initialize constant integer 'isDirect'. If the
62480 ** atomic-write optimization is enabled in this build, then isDirect
62481 ** is initialized to the value passed as the isDirectMode parameter
62482 ** to this function. Otherwise, it is always set to zero.
62483 **
62484 ** The idea is that if the atomic-write optimization is not
62485 ** enabled at compile time, the compiler can omit the tests of
62486 ** 'isDirect' below, as well as the block enclosed in the
62487 ** "if( isDirect )" condition.
62488 */
62489#ifndef SQLITE_ENABLE_ATOMIC_WRITE
62490# define DIRECT_MODE 0
62491 assert( isDirectMode==0 );
62492 UNUSED_PARAMETER(isDirectMode);
62493#else
62494# define DIRECT_MODE isDirectMode
62495#endif
62496
62497 if( !pPager->changeCountDone && pPager->dbSize>0 ){
62498 PgHdr *pPgHdr; /* Reference to page 1 */
62499
62500 assert( !pPager->tempFile && isOpen(pPager->fd) );
62501
62502 /* Open page 1 of the file for writing. */
62503 rc = sqlite3PagerGet(pPager, pgno: 1, ppPage: &pPgHdr, flags: 0);
62504 assert( pPgHdr==0 || rc==SQLITE_OK );
62505
62506 /* If page one was fetched successfully, and this function is not
62507 ** operating in direct-mode, make page 1 writable. When not in
62508 ** direct mode, page 1 is always held in cache and hence the PagerGet()
62509 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
62510 */
62511 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
62512 rc = sqlite3PagerWrite(pPg: pPgHdr);
62513 }
62514
62515 if( rc==SQLITE_OK ){
62516 /* Actually do the update of the change counter */
62517 pager_write_changecounter(pPg: pPgHdr);
62518
62519 /* If running in direct mode, write the contents of page 1 to the file. */
62520 if( DIRECT_MODE ){
62521 const void *zBuf;
62522 assert( pPager->dbFileSize>0 );
62523 zBuf = pPgHdr->pData;
62524 if( rc==SQLITE_OK ){
62525 rc = sqlite3OsWrite(id: pPager->fd, pBuf: zBuf, amt: pPager->pageSize, offset: 0);
62526 pPager->aStat[PAGER_STAT_WRITE]++;
62527 }
62528 if( rc==SQLITE_OK ){
62529 /* Update the pager's copy of the change-counter. Otherwise, the
62530 ** next time a read transaction is opened the cache will be
62531 ** flushed (as the change-counter values will not match). */
62532 const void *pCopy = (const void *)&((const char *)zBuf)[24];
62533 memcpy(dest: &pPager->dbFileVers, src: pCopy, n: sizeof(pPager->dbFileVers));
62534 pPager->changeCountDone = 1;
62535 }
62536 }else{
62537 pPager->changeCountDone = 1;
62538 }
62539 }
62540
62541 /* Release the page reference. */
62542 sqlite3PagerUnref(pPg: pPgHdr);
62543 }
62544 return rc;
62545}
62546
62547/*
62548** Sync the database file to disk. This is a no-op for in-memory databases
62549** or pages with the Pager.noSync flag set.
62550**
62551** If successful, or if called on a pager for which it is a no-op, this
62552** function returns SQLITE_OK. Otherwise, an IO error code is returned.
62553*/
62554SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper){
62555 int rc = SQLITE_OK;
62556 void *pArg = (void*)zSuper;
62557 rc = sqlite3OsFileControl(id: pPager->fd, SQLITE_FCNTL_SYNC, pArg);
62558 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
62559 if( rc==SQLITE_OK && !pPager->noSync ){
62560 assert( !MEMDB );
62561 rc = sqlite3OsSync(id: pPager->fd, flags: pPager->syncFlags);
62562 }
62563 return rc;
62564}
62565
62566/*
62567** This function may only be called while a write-transaction is active in
62568** rollback. If the connection is in WAL mode, this call is a no-op.
62569** Otherwise, if the connection does not already have an EXCLUSIVE lock on
62570** the database file, an attempt is made to obtain one.
62571**
62572** If the EXCLUSIVE lock is already held or the attempt to obtain it is
62573** successful, or the connection is in WAL mode, SQLITE_OK is returned.
62574** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
62575** returned.
62576*/
62577SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
62578 int rc = pPager->errCode;
62579 assert( assert_pager_state(pPager) );
62580 if( rc==SQLITE_OK ){
62581 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62582 || pPager->eState==PAGER_WRITER_DBMOD
62583 || pPager->eState==PAGER_WRITER_LOCKED
62584 );
62585 assert( assert_pager_state(pPager) );
62586 if( 0==pagerUseWal(pPager) ){
62587 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
62588 }
62589 }
62590 return rc;
62591}
62592
62593/*
62594** Sync the database file for the pager pPager. zSuper points to the name
62595** of a super-journal file that should be written into the individual
62596** journal file. zSuper may be NULL, which is interpreted as no
62597** super-journal (a single database transaction).
62598**
62599** This routine ensures that:
62600**
62601** * The database file change-counter is updated,
62602** * the journal is synced (unless the atomic-write optimization is used),
62603** * all dirty pages are written to the database file,
62604** * the database file is truncated (if required), and
62605** * the database file synced.
62606**
62607** The only thing that remains to commit the transaction is to finalize
62608** (delete, truncate or zero the first part of) the journal file (or
62609** delete the super-journal file if specified).
62610**
62611** Note that if zSuper==NULL, this does not overwrite a previous value
62612** passed to an sqlite3PagerCommitPhaseOne() call.
62613**
62614** If the final parameter - noSync - is true, then the database file itself
62615** is not synced. The caller must call sqlite3PagerSync() directly to
62616** sync the database file before calling CommitPhaseTwo() to delete the
62617** journal file in this case.
62618*/
62619SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
62620 Pager *pPager, /* Pager object */
62621 const char *zSuper, /* If not NULL, the super-journal name */
62622 int noSync /* True to omit the xSync on the db file */
62623){
62624 int rc = SQLITE_OK; /* Return code */
62625
62626 assert( pPager->eState==PAGER_WRITER_LOCKED
62627 || pPager->eState==PAGER_WRITER_CACHEMOD
62628 || pPager->eState==PAGER_WRITER_DBMOD
62629 || pPager->eState==PAGER_ERROR
62630 );
62631 assert( assert_pager_state(pPager) );
62632
62633 /* If a prior error occurred, report that error again. */
62634 if( NEVER(pPager->errCode) ) return pPager->errCode;
62635
62636 /* Provide the ability to easily simulate an I/O error during testing */
62637 if( sqlite3FaultSim(iTest: 400) ) return SQLITE_IOERR;
62638
62639 PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
62640 pPager->zFilename, zSuper, pPager->dbSize));
62641
62642 /* If no database changes have been made, return early. */
62643 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
62644
62645 assert( MEMDB==0 || pPager->tempFile );
62646 assert( isOpen(pPager->fd) || pPager->tempFile );
62647 if( 0==pagerFlushOnCommit(pPager, bCommit: 1) ){
62648 /* If this is an in-memory db, or no pages have been written to, or this
62649 ** function has already been called, it is mostly a no-op. However, any
62650 ** backup in progress needs to be restarted. */
62651 sqlite3BackupRestart(pPager->pBackup);
62652 }else{
62653 PgHdr *pList;
62654 if( pagerUseWal(pPager) ){
62655 PgHdr *pPageOne = 0;
62656 pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
62657 if( pList==0 ){
62658 /* Must have at least one page for the WAL commit flag.
62659 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
62660 rc = sqlite3PagerGet(pPager, pgno: 1, ppPage: &pPageOne, flags: 0);
62661 pList = pPageOne;
62662 pList->pDirty = 0;
62663 }
62664 assert( rc==SQLITE_OK );
62665 if( ALWAYS(pList) ){
62666 rc = pagerWalFrames(pPager, pList, nTruncate: pPager->dbSize, isCommit: 1);
62667 }
62668 sqlite3PagerUnref(pPg: pPageOne);
62669 if( rc==SQLITE_OK ){
62670 sqlite3PcacheCleanAll(pCache: pPager->pPCache);
62671 }
62672 }else{
62673 /* The bBatch boolean is true if the batch-atomic-write commit method
62674 ** should be used. No rollback journal is created if batch-atomic-write
62675 ** is enabled.
62676 */
62677#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
62678 sqlite3_file *fd = pPager->fd;
62679 int bBatch = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
62680 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
62681 && !pPager->noSync
62682 && sqlite3JournalIsInMemory(pPager->jfd);
62683#else
62684# define bBatch 0
62685#endif
62686
62687#ifdef SQLITE_ENABLE_ATOMIC_WRITE
62688 /* The following block updates the change-counter. Exactly how it
62689 ** does this depends on whether or not the atomic-update optimization
62690 ** was enabled at compile time, and if this transaction meets the
62691 ** runtime criteria to use the operation:
62692 **
62693 ** * The file-system supports the atomic-write property for
62694 ** blocks of size page-size, and
62695 ** * This commit is not part of a multi-file transaction, and
62696 ** * Exactly one page has been modified and store in the journal file.
62697 **
62698 ** If the optimization was not enabled at compile time, then the
62699 ** pager_incr_changecounter() function is called to update the change
62700 ** counter in 'indirect-mode'. If the optimization is compiled in but
62701 ** is not applicable to this transaction, call sqlite3JournalCreate()
62702 ** to make sure the journal file has actually been created, then call
62703 ** pager_incr_changecounter() to update the change-counter in indirect
62704 ** mode.
62705 **
62706 ** Otherwise, if the optimization is both enabled and applicable,
62707 ** then call pager_incr_changecounter() to update the change-counter
62708 ** in 'direct' mode. In this case the journal file will never be
62709 ** created for this transaction.
62710 */
62711 if( bBatch==0 ){
62712 PgHdr *pPg;
62713 assert( isOpen(pPager->jfd)
62714 || pPager->journalMode==PAGER_JOURNALMODE_OFF
62715 || pPager->journalMode==PAGER_JOURNALMODE_WAL
62716 );
62717 if( !zSuper && isOpen(pPager->jfd)
62718 && pPager->journalOff==jrnlBufferSize(pPager)
62719 && pPager->dbSize>=pPager->dbOrigSize
62720 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
62721 ){
62722 /* Update the db file change counter via the direct-write method. The
62723 ** following call will modify the in-memory representation of page 1
62724 ** to include the updated change counter and then write page 1
62725 ** directly to the database file. Because of the atomic-write
62726 ** property of the host file-system, this is safe.
62727 */
62728 rc = pager_incr_changecounter(pPager, 1);
62729 }else{
62730 rc = sqlite3JournalCreate(pPager->jfd);
62731 if( rc==SQLITE_OK ){
62732 rc = pager_incr_changecounter(pPager, 0);
62733 }
62734 }
62735 }
62736#else /* SQLITE_ENABLE_ATOMIC_WRITE */
62737#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
62738 if( zSuper ){
62739 rc = sqlite3JournalCreate(pPager->jfd);
62740 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
62741 assert( bBatch==0 );
62742 }
62743#endif
62744 rc = pager_incr_changecounter(pPager, isDirectMode: 0);
62745#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
62746 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
62747
62748 /* Write the super-journal name into the journal file. If a
62749 ** super-journal file name has already been written to the journal file,
62750 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
62751 */
62752 rc = writeSuperJournal(pPager, zSuper);
62753 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
62754
62755 /* Sync the journal file and write all dirty pages to the database.
62756 ** If the atomic-update optimization is being used, this sync will not
62757 ** create the journal file or perform any real IO.
62758 **
62759 ** Because the change-counter page was just modified, unless the
62760 ** atomic-update optimization is used it is almost certain that the
62761 ** journal requires a sync here. However, in locking_mode=exclusive
62762 ** on a system under memory pressure it is just possible that this is
62763 ** not the case. In this case it is likely enough that the redundant
62764 ** xSync() call will be changed to a no-op by the OS anyhow.
62765 */
62766 rc = syncJournal(pPager, newHdr: 0);
62767 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
62768
62769 pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
62770#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
62771 if( bBatch ){
62772 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
62773 if( rc==SQLITE_OK ){
62774 rc = pager_write_pagelist(pPager, pList);
62775 if( rc==SQLITE_OK ){
62776 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
62777 }
62778 if( rc!=SQLITE_OK ){
62779 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
62780 }
62781 }
62782
62783 if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
62784 rc = sqlite3JournalCreate(pPager->jfd);
62785 if( rc!=SQLITE_OK ){
62786 sqlite3OsClose(pPager->jfd);
62787 goto commit_phase_one_exit;
62788 }
62789 bBatch = 0;
62790 }else{
62791 sqlite3OsClose(pPager->jfd);
62792 }
62793 }
62794#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
62795
62796 if( bBatch==0 ){
62797 rc = pager_write_pagelist(pPager, pList);
62798 }
62799 if( rc!=SQLITE_OK ){
62800 assert( rc!=SQLITE_IOERR_BLOCKED );
62801 goto commit_phase_one_exit;
62802 }
62803 sqlite3PcacheCleanAll(pCache: pPager->pPCache);
62804
62805 /* If the file on disk is smaller than the database image, use
62806 ** pager_truncate to grow the file here. This can happen if the database
62807 ** image was extended as part of the current transaction and then the
62808 ** last page in the db image moved to the free-list. In this case the
62809 ** last page is never written out to disk, leaving the database file
62810 ** undersized. Fix this now if it is the case. */
62811 if( pPager->dbSize>pPager->dbFileSize ){
62812 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
62813 assert( pPager->eState==PAGER_WRITER_DBMOD );
62814 rc = pager_truncate(pPager, nPage: nNew);
62815 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
62816 }
62817
62818 /* Finally, sync the database file. */
62819 if( !noSync ){
62820 rc = sqlite3PagerSync(pPager, zSuper);
62821 }
62822 IOTRACE(("DBSYNC %p\n", pPager))
62823 }
62824 }
62825
62826commit_phase_one_exit:
62827 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
62828 pPager->eState = PAGER_WRITER_FINISHED;
62829 }
62830 return rc;
62831}
62832
62833
62834/*
62835** When this function is called, the database file has been completely
62836** updated to reflect the changes made by the current transaction and
62837** synced to disk. The journal file still exists in the file-system
62838** though, and if a failure occurs at this point it will eventually
62839** be used as a hot-journal and the current transaction rolled back.
62840**
62841** This function finalizes the journal file, either by deleting,
62842** truncating or partially zeroing it, so that it cannot be used
62843** for hot-journal rollback. Once this is done the transaction is
62844** irrevocably committed.
62845**
62846** If an error occurs, an IO error code is returned and the pager
62847** moves into the error state. Otherwise, SQLITE_OK is returned.
62848*/
62849SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
62850 int rc = SQLITE_OK; /* Return code */
62851
62852 /* This routine should not be called if a prior error has occurred.
62853 ** But if (due to a coding error elsewhere in the system) it does get
62854 ** called, just return the same error code without doing anything. */
62855 if( NEVER(pPager->errCode) ) return pPager->errCode;
62856 pPager->iDataVersion++;
62857
62858 assert( pPager->eState==PAGER_WRITER_LOCKED
62859 || pPager->eState==PAGER_WRITER_FINISHED
62860 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
62861 );
62862 assert( assert_pager_state(pPager) );
62863
62864 /* An optimization. If the database was not actually modified during
62865 ** this transaction, the pager is running in exclusive-mode and is
62866 ** using persistent journals, then this function is a no-op.
62867 **
62868 ** The start of the journal file currently contains a single journal
62869 ** header with the nRec field set to 0. If such a journal is used as
62870 ** a hot-journal during hot-journal rollback, 0 changes will be made
62871 ** to the database file. So there is no need to zero the journal
62872 ** header. Since the pager is in exclusive mode, there is no need
62873 ** to drop any locks either.
62874 */
62875 if( pPager->eState==PAGER_WRITER_LOCKED
62876 && pPager->exclusiveMode
62877 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
62878 ){
62879 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
62880 pPager->eState = PAGER_READER;
62881 return SQLITE_OK;
62882 }
62883
62884 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
62885 rc = pager_end_transaction(pPager, hasSuper: pPager->setSuper, bCommit: 1);
62886 return pager_error(pPager, rc);
62887}
62888
62889/*
62890** If a write transaction is open, then all changes made within the
62891** transaction are reverted and the current write-transaction is closed.
62892** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
62893** state if an error occurs.
62894**
62895** If the pager is already in PAGER_ERROR state when this function is called,
62896** it returns Pager.errCode immediately. No work is performed in this case.
62897**
62898** Otherwise, in rollback mode, this function performs two functions:
62899**
62900** 1) It rolls back the journal file, restoring all database file and
62901** in-memory cache pages to the state they were in when the transaction
62902** was opened, and
62903**
62904** 2) It finalizes the journal file, so that it is not used for hot
62905** rollback at any point in the future.
62906**
62907** Finalization of the journal file (task 2) is only performed if the
62908** rollback is successful.
62909**
62910** In WAL mode, all cache-entries containing data modified within the
62911** current transaction are either expelled from the cache or reverted to
62912** their pre-transaction state by re-reading data from the database or
62913** WAL files. The WAL transaction is then closed.
62914*/
62915SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
62916 int rc = SQLITE_OK; /* Return code */
62917 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
62918
62919 /* PagerRollback() is a no-op if called in READER or OPEN state. If
62920 ** the pager is already in the ERROR state, the rollback is not
62921 ** attempted here. Instead, the error code is returned to the caller.
62922 */
62923 assert( assert_pager_state(pPager) );
62924 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
62925 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
62926
62927 if( pagerUseWal(pPager) ){
62928 int rc2;
62929 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, iSavepoint: -1);
62930 rc2 = pager_end_transaction(pPager, hasSuper: pPager->setSuper, bCommit: 0);
62931 if( rc==SQLITE_OK ) rc = rc2;
62932 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
62933 int eState = pPager->eState;
62934 rc = pager_end_transaction(pPager, hasSuper: 0, bCommit: 0);
62935 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
62936 /* This can happen using journal_mode=off. Move the pager to the error
62937 ** state to indicate that the contents of the cache may not be trusted.
62938 ** Any active readers will get SQLITE_ABORT.
62939 */
62940 pPager->errCode = SQLITE_ABORT;
62941 pPager->eState = PAGER_ERROR;
62942 setGetterMethod(pPager);
62943 return rc;
62944 }
62945 }else{
62946 rc = pager_playback(pPager, isHot: 0);
62947 }
62948
62949 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
62950 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
62951 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
62952 || rc==SQLITE_CANTOPEN
62953 );
62954
62955 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
62956 ** cache. So call pager_error() on the way out to make any error persistent.
62957 */
62958 return pager_error(pPager, rc);
62959}
62960
62961/*
62962** Return TRUE if the database file is opened read-only. Return FALSE
62963** if the database is (in theory) writable.
62964*/
62965SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
62966 return pPager->readOnly;
62967}
62968
62969#ifdef SQLITE_DEBUG
62970/*
62971** Return the sum of the reference counts for all pages held by pPager.
62972*/
62973SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
62974 return sqlite3PcacheRefCount(pPager->pPCache);
62975}
62976#endif
62977
62978/*
62979** Return the approximate number of bytes of memory currently
62980** used by the pager and its associated cache.
62981*/
62982SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
62983 int perPageSize = pPager->pageSize + pPager->nExtra
62984 + (int)(sizeof(PgHdr) + 5*sizeof(void*));
62985 return perPageSize*sqlite3PcachePagecount(pCache: pPager->pPCache)
62986 + sqlite3MallocSize(p: pPager)
62987 + pPager->pageSize;
62988}
62989
62990/*
62991** Return the number of references to the specified page.
62992*/
62993SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
62994 return sqlite3PcachePageRefcount(p: pPage);
62995}
62996
62997#ifdef SQLITE_TEST
62998/*
62999** This routine is used for testing and analysis only.
63000*/
63001SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
63002 static int a[11];
63003 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
63004 a[1] = sqlite3PcachePagecount(pPager->pPCache);
63005 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
63006 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63007 a[4] = pPager->eState;
63008 a[5] = pPager->errCode;
63009 a[6] = pPager->aStat[PAGER_STAT_HIT];
63010 a[7] = pPager->aStat[PAGER_STAT_MISS];
63011 a[8] = 0; /* Used to be pPager->nOvfl */
63012 a[9] = pPager->nRead;
63013 a[10] = pPager->aStat[PAGER_STAT_WRITE];
63014 return a;
63015}
63016#endif
63017
63018/*
63019** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
63020** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
63021** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
63022** it was added later.
63023**
63024** Before returning, *pnVal is incremented by the
63025** current cache hit or miss count, according to the value of eStat. If the
63026** reset parameter is non-zero, the cache hit or miss count is zeroed before
63027** returning.
63028*/
63029SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
63030
63031 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
63032 || eStat==SQLITE_DBSTATUS_CACHE_MISS
63033 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
63034 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
63035 );
63036
63037 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
63038 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
63039 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
63040 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
63041
63042 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
63043 *pnVal += pPager->aStat[eStat];
63044 if( reset ){
63045 pPager->aStat[eStat] = 0;
63046 }
63047}
63048
63049/*
63050** Return true if this is an in-memory or temp-file backed pager.
63051*/
63052SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
63053 return pPager->tempFile || pPager->memVfs;
63054}
63055
63056/*
63057** Check that there are at least nSavepoint savepoints open. If there are
63058** currently less than nSavepoints open, then open one or more savepoints
63059** to make up the difference. If the number of savepoints is already
63060** equal to nSavepoint, then this function is a no-op.
63061**
63062** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
63063** occurs while opening the sub-journal file, then an IO error code is
63064** returned. Otherwise, SQLITE_OK.
63065*/
63066static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
63067 int rc = SQLITE_OK; /* Return code */
63068 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
63069 int ii; /* Iterator variable */
63070 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
63071
63072 assert( pPager->eState>=PAGER_WRITER_LOCKED );
63073 assert( assert_pager_state(pPager) );
63074 assert( nSavepoint>nCurrent && pPager->useJournal );
63075
63076 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
63077 ** if the allocation fails. Otherwise, zero the new portion in case a
63078 ** malloc failure occurs while populating it in the for(...) loop below.
63079 */
63080 aNew = (PagerSavepoint *)sqlite3Realloc(
63081 pOld: pPager->aSavepoint, nBytes: sizeof(PagerSavepoint)*nSavepoint
63082 );
63083 if( !aNew ){
63084 return SQLITE_NOMEM_BKPT;
63085 }
63086 memset(s: &aNew[nCurrent], c: 0, n: (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
63087 pPager->aSavepoint = aNew;
63088
63089 /* Populate the PagerSavepoint structures just allocated. */
63090 for(ii=nCurrent; ii<nSavepoint; ii++){
63091 aNew[ii].nOrig = pPager->dbSize;
63092 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
63093 aNew[ii].iOffset = pPager->journalOff;
63094 }else{
63095 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
63096 }
63097 aNew[ii].iSubRec = pPager->nSubRec;
63098 aNew[ii].pInSavepoint = sqlite3BitvecCreate(iSize: pPager->dbSize);
63099 aNew[ii].bTruncateOnRelease = 1;
63100 if( !aNew[ii].pInSavepoint ){
63101 return SQLITE_NOMEM_BKPT;
63102 }
63103 if( pagerUseWal(pPager) ){
63104 sqlite3WalSavepoint(pWal: pPager->pWal, aWalData: aNew[ii].aWalData);
63105 }
63106 pPager->nSavepoint = ii+1;
63107 }
63108 assert( pPager->nSavepoint==nSavepoint );
63109 assertTruncateConstraint(pPager);
63110 return rc;
63111}
63112SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
63113 assert( pPager->eState>=PAGER_WRITER_LOCKED );
63114 assert( assert_pager_state(pPager) );
63115
63116 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
63117 return pagerOpenSavepoint(pPager, nSavepoint);
63118 }else{
63119 return SQLITE_OK;
63120 }
63121}
63122
63123
63124/*
63125** This function is called to rollback or release (commit) a savepoint.
63126** The savepoint to release or rollback need not be the most recently
63127** created savepoint.
63128**
63129** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
63130** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
63131** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
63132** that have occurred since the specified savepoint was created.
63133**
63134** The savepoint to rollback or release is identified by parameter
63135** iSavepoint. A value of 0 means to operate on the outermost savepoint
63136** (the first created). A value of (Pager.nSavepoint-1) means operate
63137** on the most recently created savepoint. If iSavepoint is greater than
63138** (Pager.nSavepoint-1), then this function is a no-op.
63139**
63140** If a negative value is passed to this function, then the current
63141** transaction is rolled back. This is different to calling
63142** sqlite3PagerRollback() because this function does not terminate
63143** the transaction or unlock the database, it just restores the
63144** contents of the database to its original state.
63145**
63146** In any case, all savepoints with an index greater than iSavepoint
63147** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
63148** then savepoint iSavepoint is also destroyed.
63149**
63150** This function may return SQLITE_NOMEM if a memory allocation fails,
63151** or an IO error code if an IO error occurs while rolling back a
63152** savepoint. If no errors occur, SQLITE_OK is returned.
63153*/
63154SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
63155 int rc = pPager->errCode;
63156
63157#ifdef SQLITE_ENABLE_ZIPVFS
63158 if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
63159#endif
63160
63161 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
63162 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
63163
63164 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
63165 int ii; /* Iterator variable */
63166 int nNew; /* Number of remaining savepoints after this op. */
63167
63168 /* Figure out how many savepoints will still be active after this
63169 ** operation. Store this value in nNew. Then free resources associated
63170 ** with any savepoints that are destroyed by this operation.
63171 */
63172 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
63173 for(ii=nNew; ii<pPager->nSavepoint; ii++){
63174 sqlite3BitvecDestroy(p: pPager->aSavepoint[ii].pInSavepoint);
63175 }
63176 pPager->nSavepoint = nNew;
63177
63178 /* Truncate the sub-journal so that it only includes the parts
63179 ** that are still in use. */
63180 if( op==SAVEPOINT_RELEASE ){
63181 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
63182 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
63183 /* Only truncate if it is an in-memory sub-journal. */
63184 if( sqlite3JournalIsInMemory(p: pPager->sjfd) ){
63185 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
63186 rc = sqlite3OsTruncate(id: pPager->sjfd, size: sz);
63187 assert( rc==SQLITE_OK );
63188 }
63189 pPager->nSubRec = pRel->iSubRec;
63190 }
63191 }
63192 /* Else this is a rollback operation, playback the specified savepoint.
63193 ** If this is a temp-file, it is possible that the journal file has
63194 ** not yet been opened. In this case there have been no changes to
63195 ** the database file, so the playback operation can be skipped.
63196 */
63197 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
63198 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
63199 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
63200 assert(rc!=SQLITE_DONE);
63201 }
63202
63203#ifdef SQLITE_ENABLE_ZIPVFS
63204 /* If the cache has been modified but the savepoint cannot be rolled
63205 ** back journal_mode=off, put the pager in the error state. This way,
63206 ** if the VFS used by this pager includes ZipVFS, the entire transaction
63207 ** can be rolled back at the ZipVFS level. */
63208 else if(
63209 pPager->journalMode==PAGER_JOURNALMODE_OFF
63210 && pPager->eState>=PAGER_WRITER_CACHEMOD
63211 ){
63212 pPager->errCode = SQLITE_ABORT;
63213 pPager->eState = PAGER_ERROR;
63214 setGetterMethod(pPager);
63215 }
63216#endif
63217 }
63218
63219 return rc;
63220}
63221
63222/*
63223** Return the full pathname of the database file.
63224**
63225** Except, if the pager is in-memory only, then return an empty string if
63226** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
63227** used to report the filename to the user, for compatibility with legacy
63228** behavior. But when the Btree needs to know the filename for matching to
63229** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
63230** participate in shared-cache.
63231**
63232** The return value to this routine is always safe to use with
63233** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
63234*/
63235SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
63236 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
63237 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pVfs: pPager->pVfs)) ){
63238 return &zFake[4];
63239 }else{
63240 return pPager->zFilename;
63241 }
63242}
63243
63244/*
63245** Return the VFS structure for the pager.
63246*/
63247SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
63248 return pPager->pVfs;
63249}
63250
63251/*
63252** Return the file handle for the database file associated
63253** with the pager. This might return NULL if the file has
63254** not yet been opened.
63255*/
63256SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
63257 return pPager->fd;
63258}
63259
63260/*
63261** Return the file handle for the journal file (if it exists).
63262** This will be either the rollback journal or the WAL file.
63263*/
63264SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
63265#if SQLITE_OMIT_WAL
63266 return pPager->jfd;
63267#else
63268 return pPager->pWal ? sqlite3WalFile(pWal: pPager->pWal) : pPager->jfd;
63269#endif
63270}
63271
63272/*
63273** Return the full pathname of the journal file.
63274*/
63275SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
63276 return pPager->zJournal;
63277}
63278
63279#ifndef SQLITE_OMIT_AUTOVACUUM
63280/*
63281** Move the page pPg to location pgno in the file.
63282**
63283** There must be no references to the page previously located at
63284** pgno (which we call pPgOld) though that page is allowed to be
63285** in cache. If the page previously located at pgno is not already
63286** in the rollback journal, it is not put there by by this routine.
63287**
63288** References to the page pPg remain valid. Updating any
63289** meta-data associated with pPg (i.e. data stored in the nExtra bytes
63290** allocated along with the page) is the responsibility of the caller.
63291**
63292** A transaction must be active when this routine is called. It used to be
63293** required that a statement transaction was not active, but this restriction
63294** has been removed (CREATE INDEX needs to move a page when a statement
63295** transaction is active).
63296**
63297** If the fourth argument, isCommit, is non-zero, then this page is being
63298** moved as part of a database reorganization just before the transaction
63299** is being committed. In this case, it is guaranteed that the database page
63300** pPg refers to will not be written to again within this transaction.
63301**
63302** This function may return SQLITE_NOMEM or an IO error code if an error
63303** occurs. Otherwise, it returns SQLITE_OK.
63304*/
63305SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
63306 PgHdr *pPgOld; /* The page being overwritten. */
63307 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
63308 int rc; /* Return code */
63309 Pgno origPgno; /* The original page number */
63310
63311 assert( pPg->nRef>0 );
63312 assert( pPager->eState==PAGER_WRITER_CACHEMOD
63313 || pPager->eState==PAGER_WRITER_DBMOD
63314 );
63315 assert( assert_pager_state(pPager) );
63316
63317 /* In order to be able to rollback, an in-memory database must journal
63318 ** the page we are moving from.
63319 */
63320 assert( pPager->tempFile || !MEMDB );
63321 if( pPager->tempFile ){
63322 rc = sqlite3PagerWrite(pPg);
63323 if( rc ) return rc;
63324 }
63325
63326 /* If the page being moved is dirty and has not been saved by the latest
63327 ** savepoint, then save the current contents of the page into the
63328 ** sub-journal now. This is required to handle the following scenario:
63329 **
63330 ** BEGIN;
63331 ** <journal page X, then modify it in memory>
63332 ** SAVEPOINT one;
63333 ** <Move page X to location Y>
63334 ** ROLLBACK TO one;
63335 **
63336 ** If page X were not written to the sub-journal here, it would not
63337 ** be possible to restore its contents when the "ROLLBACK TO one"
63338 ** statement were is processed.
63339 **
63340 ** subjournalPage() may need to allocate space to store pPg->pgno into
63341 ** one or more savepoint bitvecs. This is the reason this function
63342 ** may return SQLITE_NOMEM.
63343 */
63344 if( (pPg->flags & PGHDR_DIRTY)!=0
63345 && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
63346 ){
63347 return rc;
63348 }
63349
63350 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
63351 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
63352 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
63353
63354 /* If the journal needs to be sync()ed before page pPg->pgno can
63355 ** be written to, store pPg->pgno in local variable needSyncPgno.
63356 **
63357 ** If the isCommit flag is set, there is no need to remember that
63358 ** the journal needs to be sync()ed before database page pPg->pgno
63359 ** can be written to. The caller has already promised not to write to it.
63360 */
63361 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
63362 needSyncPgno = pPg->pgno;
63363 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
63364 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
63365 assert( pPg->flags&PGHDR_DIRTY );
63366 }
63367
63368 /* If the cache contains a page with page-number pgno, remove it
63369 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
63370 ** page pgno before the 'move' operation, it needs to be retained
63371 ** for the page moved there.
63372 */
63373 pPg->flags &= ~PGHDR_NEED_SYNC;
63374 pPgOld = sqlite3PagerLookup(pPager, pgno);
63375 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
63376 if( pPgOld ){
63377 if( NEVER(pPgOld->nRef>1) ){
63378 sqlite3PagerUnrefNotNull(pPg: pPgOld);
63379 return SQLITE_CORRUPT_BKPT;
63380 }
63381 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
63382 if( pPager->tempFile ){
63383 /* Do not discard pages from an in-memory database since we might
63384 ** need to rollback later. Just move the page out of the way. */
63385 sqlite3PcacheMove(p: pPgOld, newPgno: pPager->dbSize+1);
63386 }else{
63387 sqlite3PcacheDrop(p: pPgOld);
63388 }
63389 }
63390
63391 origPgno = pPg->pgno;
63392 sqlite3PcacheMove(p: pPg, newPgno: pgno);
63393 sqlite3PcacheMakeDirty(p: pPg);
63394
63395 /* For an in-memory database, make sure the original page continues
63396 ** to exist, in case the transaction needs to roll back. Use pPgOld
63397 ** as the original page since it has already been allocated.
63398 */
63399 if( pPager->tempFile && pPgOld ){
63400 sqlite3PcacheMove(p: pPgOld, newPgno: origPgno);
63401 sqlite3PagerUnrefNotNull(pPg: pPgOld);
63402 }
63403
63404 if( needSyncPgno ){
63405 /* If needSyncPgno is non-zero, then the journal file needs to be
63406 ** sync()ed before any data is written to database file page needSyncPgno.
63407 ** Currently, no such page exists in the page-cache and the
63408 ** "is journaled" bitvec flag has been set. This needs to be remedied by
63409 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
63410 ** flag.
63411 **
63412 ** If the attempt to load the page into the page-cache fails, (due
63413 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
63414 ** array. Otherwise, if the page is loaded and written again in
63415 ** this transaction, it may be written to the database file before
63416 ** it is synced into the journal file. This way, it may end up in
63417 ** the journal file twice, but that is not a problem.
63418 */
63419 PgHdr *pPgHdr;
63420 rc = sqlite3PagerGet(pPager, pgno: needSyncPgno, ppPage: &pPgHdr, flags: 0);
63421 if( rc!=SQLITE_OK ){
63422 if( needSyncPgno<=pPager->dbOrigSize ){
63423 assert( pPager->pTmpSpace!=0 );
63424 sqlite3BitvecClear(p: pPager->pInJournal, i: needSyncPgno, pBuf: pPager->pTmpSpace);
63425 }
63426 return rc;
63427 }
63428 pPgHdr->flags |= PGHDR_NEED_SYNC;
63429 sqlite3PcacheMakeDirty(p: pPgHdr);
63430 sqlite3PagerUnrefNotNull(pPg: pPgHdr);
63431 }
63432
63433 return SQLITE_OK;
63434}
63435#endif
63436
63437/*
63438** The page handle passed as the first argument refers to a dirty page
63439** with a page number other than iNew. This function changes the page's
63440** page number to iNew and sets the value of the PgHdr.flags field to
63441** the value passed as the third parameter.
63442*/
63443SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
63444 assert( pPg->pgno!=iNew );
63445 pPg->flags = flags;
63446 sqlite3PcacheMove(p: pPg, newPgno: iNew);
63447}
63448
63449/*
63450** Return a pointer to the data for the specified page.
63451*/
63452SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
63453 assert( pPg->nRef>0 || pPg->pPager->memDb );
63454 return pPg->pData;
63455}
63456
63457/*
63458** Return a pointer to the Pager.nExtra bytes of "extra" space
63459** allocated along with the specified page.
63460*/
63461SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
63462 return pPg->pExtra;
63463}
63464
63465/*
63466** Get/set the locking-mode for this pager. Parameter eMode must be one
63467** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
63468** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
63469** the locking-mode is set to the value specified.
63470**
63471** The returned value is either PAGER_LOCKINGMODE_NORMAL or
63472** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
63473** locking-mode.
63474*/
63475SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
63476 assert( eMode==PAGER_LOCKINGMODE_QUERY
63477 || eMode==PAGER_LOCKINGMODE_NORMAL
63478 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
63479 assert( PAGER_LOCKINGMODE_QUERY<0 );
63480 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
63481 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
63482 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pWal: pPager->pWal) ){
63483 pPager->exclusiveMode = (u8)eMode;
63484 }
63485 return (int)pPager->exclusiveMode;
63486}
63487
63488/*
63489** Set the journal-mode for this pager. Parameter eMode must be one of:
63490**
63491** PAGER_JOURNALMODE_DELETE
63492** PAGER_JOURNALMODE_TRUNCATE
63493** PAGER_JOURNALMODE_PERSIST
63494** PAGER_JOURNALMODE_OFF
63495** PAGER_JOURNALMODE_MEMORY
63496** PAGER_JOURNALMODE_WAL
63497**
63498** The journalmode is set to the value specified if the change is allowed.
63499** The change may be disallowed for the following reasons:
63500**
63501** * An in-memory database can only have its journal_mode set to _OFF
63502** or _MEMORY.
63503**
63504** * Temporary databases cannot have _WAL journalmode.
63505**
63506** The returned indicate the current (possibly updated) journal-mode.
63507*/
63508SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
63509 u8 eOld = pPager->journalMode; /* Prior journalmode */
63510
63511 /* The eMode parameter is always valid */
63512 assert( eMode==PAGER_JOURNALMODE_DELETE /* 0 */
63513 || eMode==PAGER_JOURNALMODE_PERSIST /* 1 */
63514 || eMode==PAGER_JOURNALMODE_OFF /* 2 */
63515 || eMode==PAGER_JOURNALMODE_TRUNCATE /* 3 */
63516 || eMode==PAGER_JOURNALMODE_MEMORY /* 4 */
63517 || eMode==PAGER_JOURNALMODE_WAL /* 5 */ );
63518
63519 /* This routine is only called from the OP_JournalMode opcode, and
63520 ** the logic there will never allow a temporary file to be changed
63521 ** to WAL mode.
63522 */
63523 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
63524
63525 /* Do allow the journalmode of an in-memory database to be set to
63526 ** anything other than MEMORY or OFF
63527 */
63528 if( MEMDB ){
63529 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
63530 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
63531 eMode = eOld;
63532 }
63533 }
63534
63535 if( eMode!=eOld ){
63536
63537 /* Change the journal mode. */
63538 assert( pPager->eState!=PAGER_ERROR );
63539 pPager->journalMode = (u8)eMode;
63540
63541 /* When transitioning from TRUNCATE or PERSIST to any other journal
63542 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
63543 ** delete the journal file.
63544 */
63545 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
63546 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
63547 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
63548 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
63549 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
63550 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
63551
63552 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
63553 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
63554 /* In this case we would like to delete the journal file. If it is
63555 ** not possible, then that is not a problem. Deleting the journal file
63556 ** here is an optimization only.
63557 **
63558 ** Before deleting the journal file, obtain a RESERVED lock on the
63559 ** database file. This ensures that the journal file is not deleted
63560 ** while it is in use by some other client.
63561 */
63562 sqlite3OsClose(pId: pPager->jfd);
63563 if( pPager->eLock>=RESERVED_LOCK ){
63564 sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zJournal, dirSync: 0);
63565 }else{
63566 int rc = SQLITE_OK;
63567 int state = pPager->eState;
63568 assert( state==PAGER_OPEN || state==PAGER_READER );
63569 if( state==PAGER_OPEN ){
63570 rc = sqlite3PagerSharedLock(pPager);
63571 }
63572 if( pPager->eState==PAGER_READER ){
63573 assert( rc==SQLITE_OK );
63574 rc = pagerLockDb(pPager, RESERVED_LOCK);
63575 }
63576 if( rc==SQLITE_OK ){
63577 sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zJournal, dirSync: 0);
63578 }
63579 if( rc==SQLITE_OK && state==PAGER_READER ){
63580 pagerUnlockDb(pPager, SHARED_LOCK);
63581 }else if( state==PAGER_OPEN ){
63582 pager_unlock(pPager);
63583 }
63584 assert( state==pPager->eState );
63585 }
63586 }else if( eMode==PAGER_JOURNALMODE_OFF ){
63587 sqlite3OsClose(pId: pPager->jfd);
63588 }
63589 }
63590
63591 /* Return the new journal mode */
63592 return (int)pPager->journalMode;
63593}
63594
63595/*
63596** Return the current journal mode.
63597*/
63598SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
63599 return (int)pPager->journalMode;
63600}
63601
63602/*
63603** Return TRUE if the pager is in a state where it is OK to change the
63604** journalmode. Journalmode changes can only happen when the database
63605** is unmodified.
63606*/
63607SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
63608 assert( assert_pager_state(pPager) );
63609 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
63610 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
63611 return 1;
63612}
63613
63614/*
63615** Get/set the size-limit used for persistent journal files.
63616**
63617** Setting the size limit to -1 means no limit is enforced.
63618** An attempt to set a limit smaller than -1 is a no-op.
63619*/
63620SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
63621 if( iLimit>=-1 ){
63622 pPager->journalSizeLimit = iLimit;
63623 sqlite3WalLimit(pPager->pWal, iLimit);
63624 }
63625 return pPager->journalSizeLimit;
63626}
63627
63628/*
63629** Return a pointer to the pPager->pBackup variable. The backup module
63630** in backup.c maintains the content of this variable. This module
63631** uses it opaquely as an argument to sqlite3BackupRestart() and
63632** sqlite3BackupUpdate() only.
63633*/
63634SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
63635 return &pPager->pBackup;
63636}
63637
63638#ifndef SQLITE_OMIT_VACUUM
63639/*
63640** Unless this is an in-memory or temporary database, clear the pager cache.
63641*/
63642SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
63643 assert( MEMDB==0 || pPager->tempFile );
63644 if( pPager->tempFile==0 ) pager_reset(pPager);
63645}
63646#endif
63647
63648
63649#ifndef SQLITE_OMIT_WAL
63650/*
63651** This function is called when the user invokes "PRAGMA wal_checkpoint",
63652** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
63653** or wal_blocking_checkpoint() API functions.
63654**
63655** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
63656*/
63657SQLITE_PRIVATE int sqlite3PagerCheckpoint(
63658 Pager *pPager, /* Checkpoint on this pager */
63659 sqlite3 *db, /* Db handle used to check for interrupts */
63660 int eMode, /* Type of checkpoint */
63661 int *pnLog, /* OUT: Final number of frames in log */
63662 int *pnCkpt /* OUT: Final number of checkpointed frames */
63663){
63664 int rc = SQLITE_OK;
63665 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
63666 /* This only happens when a database file is zero bytes in size opened and
63667 ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint()
63668 ** is invoked without any intervening transactions. We need to start
63669 ** a transaction to initialize pWal. The PRAGMA table_list statement is
63670 ** used for this since it starts transactions on every database file,
63671 ** including all ATTACHed databases. This seems expensive for a single
63672 ** sqlite3_wal_checkpoint() call, but it happens very rarely.
63673 ** https://sqlite.org/forum/forumpost/fd0f19d229156939
63674 */
63675 sqlite3_exec(db, sql: "PRAGMA table_list",callback: 0,0,errmsg: 0);
63676 }
63677 if( pPager->pWal ){
63678 rc = sqlite3WalCheckpoint(pWal: pPager->pWal, db, eMode,
63679 xBusy: (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
63680 pBusyArg: pPager->pBusyHandlerArg,
63681 sync_flags: pPager->walSyncFlags, nBuf: pPager->pageSize, zBuf: (u8 *)pPager->pTmpSpace,
63682 pnLog, pnCkpt
63683 );
63684 }
63685 return rc;
63686}
63687
63688SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
63689 return sqlite3WalCallback(pWal: pPager->pWal);
63690}
63691
63692/*
63693** Return true if the underlying VFS for the given pager supports the
63694** primitives necessary for write-ahead logging.
63695*/
63696SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
63697 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
63698 if( pPager->noLock ) return 0;
63699 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
63700}
63701
63702/*
63703** Attempt to take an exclusive lock on the database file. If a PENDING lock
63704** is obtained instead, immediately release it.
63705*/
63706static int pagerExclusiveLock(Pager *pPager){
63707 int rc; /* Return code */
63708 u8 eOrigLock; /* Original lock */
63709
63710 assert( pPager->eLock>=SHARED_LOCK );
63711 eOrigLock = pPager->eLock;
63712 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
63713 if( rc!=SQLITE_OK ){
63714 /* If the attempt to grab the exclusive lock failed, release the
63715 ** pending lock that may have been obtained instead. */
63716 pagerUnlockDb(pPager, eLock: eOrigLock);
63717 }
63718
63719 return rc;
63720}
63721
63722/*
63723** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
63724** exclusive-locking mode when this function is called, take an EXCLUSIVE
63725** lock on the database file and use heap-memory to store the wal-index
63726** in. Otherwise, use the normal shared-memory.
63727*/
63728static int pagerOpenWal(Pager *pPager){
63729 int rc = SQLITE_OK;
63730
63731 assert( pPager->pWal==0 && pPager->tempFile==0 );
63732 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
63733
63734 /* If the pager is already in exclusive-mode, the WAL module will use
63735 ** heap-memory for the wal-index instead of the VFS shared-memory
63736 ** implementation. Take the exclusive lock now, before opening the WAL
63737 ** file, to make sure this is safe.
63738 */
63739 if( pPager->exclusiveMode ){
63740 rc = pagerExclusiveLock(pPager);
63741 }
63742
63743 /* Open the connection to the log file. If this operation fails,
63744 ** (e.g. due to malloc() failure), return an error code.
63745 */
63746 if( rc==SQLITE_OK ){
63747 rc = sqlite3WalOpen(pPager->pVfs,
63748 pPager->fd, pPager->zWal, pPager->exclusiveMode,
63749 pPager->journalSizeLimit, &pPager->pWal
63750 );
63751 }
63752 pagerFixMaplimit(pPager);
63753
63754 return rc;
63755}
63756
63757
63758/*
63759** The caller must be holding a SHARED lock on the database file to call
63760** this function.
63761**
63762** If the pager passed as the first argument is open on a real database
63763** file (not a temp file or an in-memory database), and the WAL file
63764** is not already open, make an attempt to open it now. If successful,
63765** return SQLITE_OK. If an error occurs or the VFS used by the pager does
63766** not support the xShmXXX() methods, return an error code. *pbOpen is
63767** not modified in either case.
63768**
63769** If the pager is open on a temp-file (or in-memory database), or if
63770** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
63771** without doing anything.
63772*/
63773SQLITE_PRIVATE int sqlite3PagerOpenWal(
63774 Pager *pPager, /* Pager object */
63775 int *pbOpen /* OUT: Set to true if call is a no-op */
63776){
63777 int rc = SQLITE_OK; /* Return code */
63778
63779 assert( assert_pager_state(pPager) );
63780 assert( pPager->eState==PAGER_OPEN || pbOpen );
63781 assert( pPager->eState==PAGER_READER || !pbOpen );
63782 assert( pbOpen==0 || *pbOpen==0 );
63783 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
63784
63785 if( !pPager->tempFile && !pPager->pWal ){
63786 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
63787
63788 /* Close any rollback journal previously open */
63789 sqlite3OsClose(pId: pPager->jfd);
63790
63791 rc = pagerOpenWal(pPager);
63792 if( rc==SQLITE_OK ){
63793 pPager->journalMode = PAGER_JOURNALMODE_WAL;
63794 pPager->eState = PAGER_OPEN;
63795 }
63796 }else{
63797 *pbOpen = 1;
63798 }
63799
63800 return rc;
63801}
63802
63803/*
63804** This function is called to close the connection to the log file prior
63805** to switching from WAL to rollback mode.
63806**
63807** Before closing the log file, this function attempts to take an
63808** EXCLUSIVE lock on the database file. If this cannot be obtained, an
63809** error (SQLITE_BUSY) is returned and the log connection is not closed.
63810** If successful, the EXCLUSIVE lock is not released before returning.
63811*/
63812SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
63813 int rc = SQLITE_OK;
63814
63815 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
63816
63817 /* If the log file is not already open, but does exist in the file-system,
63818 ** it may need to be checkpointed before the connection can switch to
63819 ** rollback mode. Open it now so this can happen.
63820 */
63821 if( !pPager->pWal ){
63822 int logexists = 0;
63823 rc = pagerLockDb(pPager, SHARED_LOCK);
63824 if( rc==SQLITE_OK ){
63825 rc = sqlite3OsAccess(
63826 pVfs: pPager->pVfs, zPath: pPager->zWal, SQLITE_ACCESS_EXISTS, pResOut: &logexists
63827 );
63828 }
63829 if( rc==SQLITE_OK && logexists ){
63830 rc = pagerOpenWal(pPager);
63831 }
63832 }
63833
63834 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
63835 ** the database file, the log and log-summary files will be deleted.
63836 */
63837 if( rc==SQLITE_OK && pPager->pWal ){
63838 rc = pagerExclusiveLock(pPager);
63839 if( rc==SQLITE_OK ){
63840 rc = sqlite3WalClose(pWal: pPager->pWal, db, sync_flags: pPager->walSyncFlags,
63841 pPager->pageSize, (u8*)pPager->pTmpSpace);
63842 pPager->pWal = 0;
63843 pagerFixMaplimit(pPager);
63844 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
63845 }
63846 }
63847 return rc;
63848}
63849
63850#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
63851/*
63852** If pager pPager is a wal-mode database not in exclusive locking mode,
63853** invoke the sqlite3WalWriteLock() function on the associated Wal object
63854** with the same db and bLock parameters as were passed to this function.
63855** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
63856*/
63857SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){
63858 int rc = SQLITE_OK;
63859 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
63860 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
63861 }
63862 return rc;
63863}
63864
63865/*
63866** Set the database handle used by the wal layer to determine if
63867** blocking locks are required.
63868*/
63869SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
63870 if( pagerUseWal(pPager) ){
63871 sqlite3WalDb(pPager->pWal, db);
63872 }
63873}
63874#endif
63875
63876#ifdef SQLITE_ENABLE_SNAPSHOT
63877/*
63878** If this is a WAL database, obtain a snapshot handle for the snapshot
63879** currently open. Otherwise, return an error.
63880*/
63881SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
63882 int rc = SQLITE_ERROR;
63883 if( pPager->pWal ){
63884 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
63885 }
63886 return rc;
63887}
63888
63889/*
63890** If this is a WAL database, store a pointer to pSnapshot. Next time a
63891** read transaction is opened, attempt to read from the snapshot it
63892** identifies. If this is not a WAL database, return an error.
63893*/
63894SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(
63895 Pager *pPager,
63896 sqlite3_snapshot *pSnapshot
63897){
63898 int rc = SQLITE_OK;
63899 if( pPager->pWal ){
63900 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
63901 }else{
63902 rc = SQLITE_ERROR;
63903 }
63904 return rc;
63905}
63906
63907/*
63908** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
63909** is not a WAL database, return an error.
63910*/
63911SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
63912 int rc;
63913 if( pPager->pWal ){
63914 rc = sqlite3WalSnapshotRecover(pPager->pWal);
63915 }else{
63916 rc = SQLITE_ERROR;
63917 }
63918 return rc;
63919}
63920
63921/*
63922** The caller currently has a read transaction open on the database.
63923** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
63924** this function takes a SHARED lock on the CHECKPOINTER slot and then
63925** checks if the snapshot passed as the second argument is still
63926** available. If so, SQLITE_OK is returned.
63927**
63928** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
63929** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
63930** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
63931** lock is released before returning.
63932*/
63933SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
63934 int rc;
63935 if( pPager->pWal ){
63936 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
63937 }else{
63938 rc = SQLITE_ERROR;
63939 }
63940 return rc;
63941}
63942
63943/*
63944** Release a lock obtained by an earlier successful call to
63945** sqlite3PagerSnapshotCheck().
63946*/
63947SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
63948 assert( pPager->pWal );
63949 sqlite3WalSnapshotUnlock(pPager->pWal);
63950}
63951
63952#endif /* SQLITE_ENABLE_SNAPSHOT */
63953#endif /* !SQLITE_OMIT_WAL */
63954
63955#ifdef SQLITE_ENABLE_ZIPVFS
63956/*
63957** A read-lock must be held on the pager when this function is called. If
63958** the pager is in WAL mode and the WAL file currently contains one or more
63959** frames, return the size in bytes of the page images stored within the
63960** WAL frames. Otherwise, if this is not a WAL database or the WAL file
63961** is empty, return 0.
63962*/
63963SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
63964 assert( pPager->eState>=PAGER_READER );
63965 return sqlite3WalFramesize(pPager->pWal);
63966}
63967#endif
63968
63969#ifdef SQLITE_USE_SEH
63970SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){
63971 return sqlite3WalSystemErrno(pPager->pWal);
63972}
63973#endif
63974
63975#endif /* SQLITE_OMIT_DISKIO */
63976
63977/************** End of pager.c ***********************************************/
63978/************** Begin file wal.c *********************************************/
63979/*
63980** 2010 February 1
63981**
63982** The author disclaims copyright to this source code. In place of
63983** a legal notice, here is a blessing:
63984**
63985** May you do good and not evil.
63986** May you find forgiveness for yourself and forgive others.
63987** May you share freely, never taking more than you give.
63988**
63989*************************************************************************
63990**
63991** This file contains the implementation of a write-ahead log (WAL) used in
63992** "journal_mode=WAL" mode.
63993**
63994** WRITE-AHEAD LOG (WAL) FILE FORMAT
63995**
63996** A WAL file consists of a header followed by zero or more "frames".
63997** Each frame records the revised content of a single page from the
63998** database file. All changes to the database are recorded by writing
63999** frames into the WAL. Transactions commit when a frame is written that
64000** contains a commit marker. A single WAL can and usually does record
64001** multiple transactions. Periodically, the content of the WAL is
64002** transferred back into the database file in an operation called a
64003** "checkpoint".
64004**
64005** A single WAL file can be used multiple times. In other words, the
64006** WAL can fill up with frames and then be checkpointed and then new
64007** frames can overwrite the old ones. A WAL always grows from beginning
64008** toward the end. Checksums and counters attached to each frame are
64009** used to determine which frames within the WAL are valid and which
64010** are leftovers from prior checkpoints.
64011**
64012** The WAL header is 32 bytes in size and consists of the following eight
64013** big-endian 32-bit unsigned integer values:
64014**
64015** 0: Magic number. 0x377f0682 or 0x377f0683
64016** 4: File format version. Currently 3007000
64017** 8: Database page size. Example: 1024
64018** 12: Checkpoint sequence number
64019** 16: Salt-1, random integer incremented with each checkpoint
64020** 20: Salt-2, a different random integer changing with each ckpt
64021** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
64022** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
64023**
64024** Immediately following the wal-header are zero or more frames. Each
64025** frame consists of a 24-byte frame-header followed by a <page-size> bytes
64026** of page data. The frame-header is six big-endian 32-bit unsigned
64027** integer values, as follows:
64028**
64029** 0: Page number.
64030** 4: For commit records, the size of the database image in pages
64031** after the commit. For all other records, zero.
64032** 8: Salt-1 (copied from the header)
64033** 12: Salt-2 (copied from the header)
64034** 16: Checksum-1.
64035** 20: Checksum-2.
64036**
64037** A frame is considered valid if and only if the following conditions are
64038** true:
64039**
64040** (1) The salt-1 and salt-2 values in the frame-header match
64041** salt values in the wal-header
64042**
64043** (2) The checksum values in the final 8 bytes of the frame-header
64044** exactly match the checksum computed consecutively on the
64045** WAL header and the first 8 bytes and the content of all frames
64046** up to and including the current frame.
64047**
64048** The checksum is computed using 32-bit big-endian integers if the
64049** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
64050** is computed using little-endian if the magic number is 0x377f0682.
64051** The checksum values are always stored in the frame header in a
64052** big-endian format regardless of which byte order is used to compute
64053** the checksum. The checksum is computed by interpreting the input as
64054** an even number of unsigned 32-bit integers: x[0] through x[N]. The
64055** algorithm used for the checksum is as follows:
64056**
64057** for i from 0 to n-1 step 2:
64058** s0 += x[i] + s1;
64059** s1 += x[i+1] + s0;
64060** endfor
64061**
64062** Note that s0 and s1 are both weighted checksums using fibonacci weights
64063** in reverse order (the largest fibonacci weight occurs on the first element
64064** of the sequence being summed.) The s1 value spans all 32-bit
64065** terms of the sequence whereas s0 omits the final term.
64066**
64067** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
64068** WAL is transferred into the database, then the database is VFS.xSync-ed.
64069** The VFS.xSync operations serve as write barriers - all writes launched
64070** before the xSync must complete before any write that launches after the
64071** xSync begins.
64072**
64073** After each checkpoint, the salt-1 value is incremented and the salt-2
64074** value is randomized. This prevents old and new frames in the WAL from
64075** being considered valid at the same time and being checkpointing together
64076** following a crash.
64077**
64078** READER ALGORITHM
64079**
64080** To read a page from the database (call it page number P), a reader
64081** first checks the WAL to see if it contains page P. If so, then the
64082** last valid instance of page P that is a followed by a commit frame
64083** or is a commit frame itself becomes the value read. If the WAL
64084** contains no copies of page P that are valid and which are a commit
64085** frame or are followed by a commit frame, then page P is read from
64086** the database file.
64087**
64088** To start a read transaction, the reader records the index of the last
64089** valid frame in the WAL. The reader uses this recorded "mxFrame" value
64090** for all subsequent read operations. New transactions can be appended
64091** to the WAL, but as long as the reader uses its original mxFrame value
64092** and ignores the newly appended content, it will see a consistent snapshot
64093** of the database from a single point in time. This technique allows
64094** multiple concurrent readers to view different versions of the database
64095** content simultaneously.
64096**
64097** The reader algorithm in the previous paragraphs works correctly, but
64098** because frames for page P can appear anywhere within the WAL, the
64099** reader has to scan the entire WAL looking for page P frames. If the
64100** WAL is large (multiple megabytes is typical) that scan can be slow,
64101** and read performance suffers. To overcome this problem, a separate
64102** data structure called the wal-index is maintained to expedite the
64103** search for frames of a particular page.
64104**
64105** WAL-INDEX FORMAT
64106**
64107** Conceptually, the wal-index is shared memory, though VFS implementations
64108** might choose to implement the wal-index using a mmapped file. Because
64109** the wal-index is shared memory, SQLite does not support journal_mode=WAL
64110** on a network filesystem. All users of the database must be able to
64111** share memory.
64112**
64113** In the default unix and windows implementation, the wal-index is a mmapped
64114** file whose name is the database name with a "-shm" suffix added. For that
64115** reason, the wal-index is sometimes called the "shm" file.
64116**
64117** The wal-index is transient. After a crash, the wal-index can (and should
64118** be) reconstructed from the original WAL file. In fact, the VFS is required
64119** to either truncate or zero the header of the wal-index when the last
64120** connection to it closes. Because the wal-index is transient, it can
64121** use an architecture-specific format; it does not have to be cross-platform.
64122** Hence, unlike the database and WAL file formats which store all values
64123** as big endian, the wal-index can store multi-byte values in the native
64124** byte order of the host computer.
64125**
64126** The purpose of the wal-index is to answer this question quickly: Given
64127** a page number P and a maximum frame index M, return the index of the
64128** last frame in the wal before frame M for page P in the WAL, or return
64129** NULL if there are no frames for page P in the WAL prior to M.
64130**
64131** The wal-index consists of a header region, followed by an one or
64132** more index blocks.
64133**
64134** The wal-index header contains the total number of frames within the WAL
64135** in the mxFrame field.
64136**
64137** Each index block except for the first contains information on
64138** HASHTABLE_NPAGE frames. The first index block contains information on
64139** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
64140** HASHTABLE_NPAGE are selected so that together the wal-index header and
64141** first index block are the same size as all other index blocks in the
64142** wal-index. The values are:
64143**
64144** HASHTABLE_NPAGE 4096
64145** HASHTABLE_NPAGE_ONE 4062
64146**
64147** Each index block contains two sections, a page-mapping that contains the
64148** database page number associated with each wal frame, and a hash-table
64149** that allows readers to query an index block for a specific page number.
64150** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
64151** for the first index block) 32-bit page numbers. The first entry in the
64152** first index-block contains the database page number corresponding to the
64153** first frame in the WAL file. The first entry in the second index block
64154** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
64155** the log, and so on.
64156**
64157** The last index block in a wal-index usually contains less than the full
64158** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
64159** depending on the contents of the WAL file. This does not change the
64160** allocated size of the page-mapping array - the page-mapping array merely
64161** contains unused entries.
64162**
64163** Even without using the hash table, the last frame for page P
64164** can be found by scanning the page-mapping sections of each index block
64165** starting with the last index block and moving toward the first, and
64166** within each index block, starting at the end and moving toward the
64167** beginning. The first entry that equals P corresponds to the frame
64168** holding the content for that page.
64169**
64170** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
64171** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
64172** hash table for each page number in the mapping section, so the hash
64173** table is never more than half full. The expected number of collisions
64174** prior to finding a match is 1. Each entry of the hash table is an
64175** 1-based index of an entry in the mapping section of the same
64176** index block. Let K be the 1-based index of the largest entry in
64177** the mapping section. (For index blocks other than the last, K will
64178** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
64179** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
64180** contain a value of 0.
64181**
64182** To look for page P in the hash table, first compute a hash iKey on
64183** P as follows:
64184**
64185** iKey = (P * 383) % HASHTABLE_NSLOT
64186**
64187** Then start scanning entries of the hash table, starting with iKey
64188** (wrapping around to the beginning when the end of the hash table is
64189** reached) until an unused hash slot is found. Let the first unused slot
64190** be at index iUnused. (iUnused might be less than iKey if there was
64191** wrap-around.) Because the hash table is never more than half full,
64192** the search is guaranteed to eventually hit an unused entry. Let
64193** iMax be the value between iKey and iUnused, closest to iUnused,
64194** where aHash[iMax]==P. If there is no iMax entry (if there exists
64195** no hash slot such that aHash[i]==p) then page P is not in the
64196** current index block. Otherwise the iMax-th mapping entry of the
64197** current index block corresponds to the last entry that references
64198** page P.
64199**
64200** A hash search begins with the last index block and moves toward the
64201** first index block, looking for entries corresponding to page P. On
64202** average, only two or three slots in each index block need to be
64203** examined in order to either find the last entry for page P, or to
64204** establish that no such entry exists in the block. Each index block
64205** holds over 4000 entries. So two or three index blocks are sufficient
64206** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
64207** comparisons (on average) suffice to either locate a frame in the
64208** WAL or to establish that the frame does not exist in the WAL. This
64209** is much faster than scanning the entire 10MB WAL.
64210**
64211** Note that entries are added in order of increasing K. Hence, one
64212** reader might be using some value K0 and a second reader that started
64213** at a later time (after additional transactions were added to the WAL
64214** and to the wal-index) might be using a different value K1, where K1>K0.
64215** Both readers can use the same hash table and mapping section to get
64216** the correct result. There may be entries in the hash table with
64217** K>K0 but to the first reader, those entries will appear to be unused
64218** slots in the hash table and so the first reader will get an answer as
64219** if no values greater than K0 had ever been inserted into the hash table
64220** in the first place - which is what reader one wants. Meanwhile, the
64221** second reader using K1 will see additional values that were inserted
64222** later, which is exactly what reader two wants.
64223**
64224** When a rollback occurs, the value of K is decreased. Hash table entries
64225** that correspond to frames greater than the new K value are removed
64226** from the hash table at this point.
64227*/
64228#ifndef SQLITE_OMIT_WAL
64229
64230/* #include "wal.h" */
64231
64232/*
64233** Trace output macros
64234*/
64235#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
64236SQLITE_PRIVATE int sqlite3WalTrace = 0;
64237# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
64238#else
64239# define WALTRACE(X)
64240#endif
64241
64242/*
64243** The maximum (and only) versions of the wal and wal-index formats
64244** that may be interpreted by this version of SQLite.
64245**
64246** If a client begins recovering a WAL file and finds that (a) the checksum
64247** values in the wal-header are correct and (b) the version field is not
64248** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
64249**
64250** Similarly, if a client successfully reads a wal-index header (i.e. the
64251** checksum test is successful) and finds that the version field is not
64252** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
64253** returns SQLITE_CANTOPEN.
64254*/
64255#define WAL_MAX_VERSION 3007000
64256#define WALINDEX_MAX_VERSION 3007000
64257
64258/*
64259** Index numbers for various locking bytes. WAL_NREADER is the number
64260** of available reader locks and should be at least 3. The default
64261** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
64262**
64263** Technically, the various VFSes are free to implement these locks however
64264** they see fit. However, compatibility is encouraged so that VFSes can
64265** interoperate. The standard implementation used on both unix and windows
64266** is for the index number to indicate a byte offset into the
64267** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
64268** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
64269** should be 120) is the location in the shm file for the first locking
64270** byte.
64271*/
64272#define WAL_WRITE_LOCK 0
64273#define WAL_ALL_BUT_WRITE 1
64274#define WAL_CKPT_LOCK 1
64275#define WAL_RECOVER_LOCK 2
64276#define WAL_READ_LOCK(I) (3+(I))
64277#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
64278
64279
64280/* Object declarations */
64281typedef struct WalIndexHdr WalIndexHdr;
64282typedef struct WalIterator WalIterator;
64283typedef struct WalCkptInfo WalCkptInfo;
64284
64285
64286/*
64287** The following object holds a copy of the wal-index header content.
64288**
64289** The actual header in the wal-index consists of two copies of this
64290** object followed by one instance of the WalCkptInfo object.
64291** For all versions of SQLite through 3.10.0 and probably beyond,
64292** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
64293** the total header size is 136 bytes.
64294**
64295** The szPage value can be any power of 2 between 512 and 32768, inclusive.
64296** Or it can be 1 to represent a 65536-byte page. The latter case was
64297** added in 3.7.1 when support for 64K pages was added.
64298*/
64299struct WalIndexHdr {
64300 u32 iVersion; /* Wal-index version */
64301 u32 unused; /* Unused (padding) field */
64302 u32 iChange; /* Counter incremented each transaction */
64303 u8 isInit; /* 1 when initialized */
64304 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
64305 u16 szPage; /* Database page size in bytes. 1==64K */
64306 u32 mxFrame; /* Index of last valid frame in the WAL */
64307 u32 nPage; /* Size of database in pages */
64308 u32 aFrameCksum[2]; /* Checksum of last frame in log */
64309 u32 aSalt[2]; /* Two salt values copied from WAL header */
64310 u32 aCksum[2]; /* Checksum over all prior fields */
64311};
64312
64313/*
64314** A copy of the following object occurs in the wal-index immediately
64315** following the second copy of the WalIndexHdr. This object stores
64316** information used by checkpoint.
64317**
64318** nBackfill is the number of frames in the WAL that have been written
64319** back into the database. (We call the act of moving content from WAL to
64320** database "backfilling".) The nBackfill number is never greater than
64321** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
64322** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
64323** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
64324** mxFrame back to zero when the WAL is reset.
64325**
64326** nBackfillAttempted is the largest value of nBackfill that a checkpoint
64327** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
64328** the nBackfillAttempted is set before any backfilling is done and the
64329** nBackfill is only set after all backfilling completes. So if a checkpoint
64330** crashes, nBackfillAttempted might be larger than nBackfill. The
64331** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
64332**
64333** The aLock[] field is a set of bytes used for locking. These bytes should
64334** never be read or written.
64335**
64336** There is one entry in aReadMark[] for each reader lock. If a reader
64337** holds read-lock K, then the value in aReadMark[K] is no greater than
64338** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
64339** for any aReadMark[] means that entry is unused. aReadMark[0] is
64340** a special case; its value is never used and it exists as a place-holder
64341** to avoid having to offset aReadMark[] indexes by one. Readers holding
64342** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
64343** directly from the database.
64344**
64345** The value of aReadMark[K] may only be changed by a thread that
64346** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
64347** aReadMark[K] cannot changed while there is a reader is using that mark
64348** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
64349**
64350** The checkpointer may only transfer frames from WAL to database where
64351** the frame numbers are less than or equal to every aReadMark[] that is
64352** in use (that is, every aReadMark[j] for which there is a corresponding
64353** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
64354** largest value and will increase an unused aReadMark[] to mxFrame if there
64355** is not already an aReadMark[] equal to mxFrame. The exception to the
64356** previous sentence is when nBackfill equals mxFrame (meaning that everything
64357** in the WAL has been backfilled into the database) then new readers
64358** will choose aReadMark[0] which has value 0 and hence such reader will
64359** get all their all content directly from the database file and ignore
64360** the WAL.
64361**
64362** Writers normally append new frames to the end of the WAL. However,
64363** if nBackfill equals mxFrame (meaning that all WAL content has been
64364** written back into the database) and if no readers are using the WAL
64365** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
64366** the writer will first "reset" the WAL back to the beginning and start
64367** writing new content beginning at frame 1.
64368**
64369** We assume that 32-bit loads are atomic and so no locks are needed in
64370** order to read from any aReadMark[] entries.
64371*/
64372struct WalCkptInfo {
64373 u32 nBackfill; /* Number of WAL frames backfilled into DB */
64374 u32 aReadMark[WAL_NREADER]; /* Reader marks */
64375 u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */
64376 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
64377 u32 notUsed0; /* Available for future enhancements */
64378};
64379#define READMARK_NOT_USED 0xffffffff
64380
64381/*
64382** This is a schematic view of the complete 136-byte header of the
64383** wal-index file (also known as the -shm file):
64384**
64385** +-----------------------------+
64386** 0: | iVersion | \
64387** +-----------------------------+ |
64388** 4: | (unused padding) | |
64389** +-----------------------------+ |
64390** 8: | iChange | |
64391** +-------+-------+-------------+ |
64392** 12: | bInit | bBig | szPage | |
64393** +-------+-------+-------------+ |
64394** 16: | mxFrame | | First copy of the
64395** +-----------------------------+ | WalIndexHdr object
64396** 20: | nPage | |
64397** +-----------------------------+ |
64398** 24: | aFrameCksum | |
64399** | | |
64400** +-----------------------------+ |
64401** 32: | aSalt | |
64402** | | |
64403** +-----------------------------+ |
64404** 40: | aCksum | |
64405** | | /
64406** +-----------------------------+
64407** 48: | iVersion | \
64408** +-----------------------------+ |
64409** 52: | (unused padding) | |
64410** +-----------------------------+ |
64411** 56: | iChange | |
64412** +-------+-------+-------------+ |
64413** 60: | bInit | bBig | szPage | |
64414** +-------+-------+-------------+ | Second copy of the
64415** 64: | mxFrame | | WalIndexHdr
64416** +-----------------------------+ |
64417** 68: | nPage | |
64418** +-----------------------------+ |
64419** 72: | aFrameCksum | |
64420** | | |
64421** +-----------------------------+ |
64422** 80: | aSalt | |
64423** | | |
64424** +-----------------------------+ |
64425** 88: | aCksum | |
64426** | | /
64427** +-----------------------------+
64428** 96: | nBackfill |
64429** +-----------------------------+
64430** 100: | 5 read marks |
64431** | |
64432** | |
64433** | |
64434** | |
64435** +-------+-------+------+------+
64436** 120: | Write | Ckpt | Rcvr | Rd0 | \
64437** +-------+-------+------+------+ ) 8 lock bytes
64438** | Read1 | Read2 | Rd3 | Rd4 | /
64439** +-------+-------+------+------+
64440** 128: | nBackfillAttempted |
64441** +-----------------------------+
64442** 132: | (unused padding) |
64443** +-----------------------------+
64444*/
64445
64446/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
64447** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
64448** only support mandatory file-locks, we do not read or write data
64449** from the region of the file on which locks are applied.
64450*/
64451#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
64452#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
64453
64454/* Size of header before each frame in wal */
64455#define WAL_FRAME_HDRSIZE 24
64456
64457/* Size of write ahead log header, including checksum. */
64458#define WAL_HDRSIZE 32
64459
64460/* WAL magic value. Either this value, or the same value with the least
64461** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
64462** big-endian format in the first 4 bytes of a WAL file.
64463**
64464** If the LSB is set, then the checksums for each frame within the WAL
64465** file are calculated by treating all data as an array of 32-bit
64466** big-endian words. Otherwise, they are calculated by interpreting
64467** all data as 32-bit little-endian words.
64468*/
64469#define WAL_MAGIC 0x377f0682
64470
64471/*
64472** Return the offset of frame iFrame in the write-ahead log file,
64473** assuming a database page size of szPage bytes. The offset returned
64474** is to the start of the write-ahead log frame-header.
64475*/
64476#define walFrameOffset(iFrame, szPage) ( \
64477 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
64478)
64479
64480/*
64481** An open write-ahead log file is represented by an instance of the
64482** following object.
64483*/
64484struct Wal {
64485 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
64486 sqlite3_file *pDbFd; /* File handle for the database file */
64487 sqlite3_file *pWalFd; /* File handle for WAL file */
64488 u32 iCallback; /* Value to pass to log callback (or 0) */
64489 i64 mxWalSize; /* Truncate WAL to this size upon reset */
64490 int nWiData; /* Size of array apWiData */
64491 int szFirstBlock; /* Size of first block written to WAL file */
64492 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
64493 u32 szPage; /* Database page size */
64494 i16 readLock; /* Which read lock is being held. -1 for none */
64495 u8 syncFlags; /* Flags to use to sync header writes */
64496 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
64497 u8 writeLock; /* True if in a write transaction */
64498 u8 ckptLock; /* True if holding a checkpoint lock */
64499 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
64500 u8 truncateOnCommit; /* True to truncate WAL file on commit */
64501 u8 syncHeader; /* Fsync the WAL header if true */
64502 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
64503 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
64504 WalIndexHdr hdr; /* Wal-index header for current transaction */
64505 u32 minFrame; /* Ignore wal frames before this one */
64506 u32 iReCksum; /* On commit, recalculate checksums from here */
64507 const char *zWalName; /* Name of WAL file */
64508 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
64509#ifdef SQLITE_USE_SEH
64510 u32 lockMask; /* Mask of locks held */
64511 void *pFree; /* Pointer to sqlite3_free() if exception thrown */
64512 u32 *pWiValue; /* Value to write into apWiData[iWiPg] */
64513 int iWiPg; /* Write pWiValue into apWiData[iWiPg] */
64514 int iSysErrno; /* System error code following exception */
64515#endif
64516#ifdef SQLITE_DEBUG
64517 int nSehTry; /* Number of nested SEH_TRY{} blocks */
64518 u8 lockError; /* True if a locking error has occurred */
64519#endif
64520#ifdef SQLITE_ENABLE_SNAPSHOT
64521 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
64522#endif
64523#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
64524 sqlite3 *db;
64525#endif
64526};
64527
64528/*
64529** Candidate values for Wal.exclusiveMode.
64530*/
64531#define WAL_NORMAL_MODE 0
64532#define WAL_EXCLUSIVE_MODE 1
64533#define WAL_HEAPMEMORY_MODE 2
64534
64535/*
64536** Possible values for WAL.readOnly
64537*/
64538#define WAL_RDWR 0 /* Normal read/write connection */
64539#define WAL_RDONLY 1 /* The WAL file is readonly */
64540#define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
64541
64542/*
64543** Each page of the wal-index mapping contains a hash-table made up of
64544** an array of HASHTABLE_NSLOT elements of the following type.
64545*/
64546typedef u16 ht_slot;
64547
64548/*
64549** This structure is used to implement an iterator that loops through
64550** all frames in the WAL in database page order. Where two or more frames
64551** correspond to the same database page, the iterator visits only the
64552** frame most recently written to the WAL (in other words, the frame with
64553** the largest index).
64554**
64555** The internals of this structure are only accessed by:
64556**
64557** walIteratorInit() - Create a new iterator,
64558** walIteratorNext() - Step an iterator,
64559** walIteratorFree() - Free an iterator.
64560**
64561** This functionality is used by the checkpoint code (see walCheckpoint()).
64562*/
64563struct WalIterator {
64564 u32 iPrior; /* Last result returned from the iterator */
64565 int nSegment; /* Number of entries in aSegment[] */
64566 struct WalSegment {
64567 int iNext; /* Next slot in aIndex[] not yet returned */
64568 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
64569 u32 *aPgno; /* Array of page numbers. */
64570 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
64571 int iZero; /* Frame number associated with aPgno[0] */
64572 } aSegment[1]; /* One for every 32KB page in the wal-index */
64573};
64574
64575/*
64576** Define the parameters of the hash tables in the wal-index file. There
64577** is a hash-table following every HASHTABLE_NPAGE page numbers in the
64578** wal-index.
64579**
64580** Changing any of these constants will alter the wal-index format and
64581** create incompatibilities.
64582*/
64583#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
64584#define HASHTABLE_HASH_1 383 /* Should be prime */
64585#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
64586
64587/*
64588** The block of page numbers associated with the first hash-table in a
64589** wal-index is smaller than usual. This is so that there is a complete
64590** hash-table on each aligned 32KB page of the wal-index.
64591*/
64592#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
64593
64594/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
64595#define WALINDEX_PGSZ ( \
64596 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
64597)
64598
64599/*
64600** Structured Exception Handling (SEH) is a Windows-specific technique
64601** for catching exceptions raised while accessing memory-mapped files.
64602**
64603** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
64604** deal with system-level errors that arise during WAL -shm file processing.
64605** Without this compile-time option, any system-level faults that appear
64606** while accessing the memory-mapped -shm file will cause a process-wide
64607** signal to be deliver, which will more than likely cause the entire
64608** process to exit.
64609*/
64610#ifdef SQLITE_USE_SEH
64611#include <Windows.h>
64612
64613/* Beginning of a block of code in which an exception might occur */
64614# define SEH_TRY __try { \
64615 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
64616 VVA_ONLY(pWal->nSehTry++);
64617
64618/* The end of a block of code in which an exception might occur */
64619# define SEH_EXCEPT(X) \
64620 VVA_ONLY(pWal->nSehTry--); \
64621 assert( pWal->nSehTry==0 ); \
64622 } __except( sehExceptionFilter(pWal, GetExceptionCode(), GetExceptionInformation() ) ){ X }
64623
64624/* Simulate a memory-mapping fault in the -shm file for testing purposes */
64625# define SEH_INJECT_FAULT sehInjectFault(pWal)
64626
64627/*
64628** The second argument is the return value of GetExceptionCode() for the
64629** current exception. Return EXCEPTION_EXECUTE_HANDLER if the exception code
64630** indicates that the exception may have been caused by accessing the *-shm
64631** file mapping. Or EXCEPTION_CONTINUE_SEARCH otherwise.
64632*/
64633static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){
64634 VVA_ONLY(pWal->nSehTry--);
64635 if( eCode==EXCEPTION_IN_PAGE_ERROR ){
64636 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
64637 /* From MSDN: For this type of exception, the first element of the
64638 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
64639 ** was thrown while reading, 1 if while writing. The second element is
64640 ** the virtual address being accessed. The "third array element specifies
64641 ** the underlying NTSTATUS code that resulted in the exception". */
64642 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
64643 }
64644 return EXCEPTION_EXECUTE_HANDLER;
64645 }
64646 return EXCEPTION_CONTINUE_SEARCH;
64647}
64648
64649/*
64650** If one is configured, invoke the xTestCallback callback with 650 as
64651** the argument. If it returns true, throw the same exception that is
64652** thrown by the system if the *-shm file mapping is accessed after it
64653** has been invalidated.
64654*/
64655static void sehInjectFault(Wal *pWal){
64656 int res;
64657 assert( pWal->nSehTry>0 );
64658
64659 res = sqlite3FaultSim(650);
64660 if( res!=0 ){
64661 ULONG_PTR aArg[3];
64662 aArg[0] = 0;
64663 aArg[1] = 0;
64664 aArg[2] = (ULONG_PTR)res;
64665 RaiseException(EXCEPTION_IN_PAGE_ERROR, 0, 3, (const ULONG_PTR*)aArg);
64666 }
64667}
64668
64669/*
64670** There are two ways to use this macro. To set a pointer to be freed
64671** if an exception is thrown:
64672**
64673** SEH_FREE_ON_ERROR(0, pPtr);
64674**
64675** and to cancel the same:
64676**
64677** SEH_FREE_ON_ERROR(pPtr, 0);
64678**
64679** In the first case, there must not already be a pointer registered to
64680** be freed. In the second case, pPtr must be the registered pointer.
64681*/
64682#define SEH_FREE_ON_ERROR(X,Y) \
64683 assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
64684
64685/*
64686** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
64687** to be set to pValue if an exception is thrown:
64688**
64689** SEH_SET_ON_ERROR(iPg, pValue);
64690**
64691** and to cancel the same:
64692**
64693** SEH_SET_ON_ERROR(0, 0);
64694*/
64695#define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
64696
64697#else
64698# define SEH_TRY VVA_ONLY(pWal->nSehTry++);
64699# define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
64700# define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
64701# define SEH_FREE_ON_ERROR(X,Y)
64702# define SEH_SET_ON_ERROR(X,Y)
64703#endif /* ifdef SQLITE_USE_SEH */
64704
64705
64706/*
64707** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
64708** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
64709** numbered from zero.
64710**
64711** If the wal-index is currently smaller the iPage pages then the size
64712** of the wal-index might be increased, but only if it is safe to do
64713** so. It is safe to enlarge the wal-index if pWal->writeLock is true
64714** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
64715**
64716** Three possible result scenarios:
64717**
64718** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
64719** (2) rc>=SQLITE_ERROR and *ppPage==NULL
64720** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0
64721**
64722** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
64723*/
64724static SQLITE_NOINLINE int walIndexPageRealloc(
64725 Wal *pWal, /* The WAL context */
64726 int iPage, /* The page we seek */
64727 volatile u32 **ppPage /* Write the page pointer here */
64728){
64729 int rc = SQLITE_OK;
64730
64731 /* Enlarge the pWal->apWiData[] array if required */
64732 if( pWal->nWiData<=iPage ){
64733 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
64734 volatile u32 **apNew;
64735 apNew = (volatile u32 **)sqlite3Realloc(pOld: (void *)pWal->apWiData, nBytes: nByte);
64736 if( !apNew ){
64737 *ppPage = 0;
64738 return SQLITE_NOMEM_BKPT;
64739 }
64740 memset(s: (void*)&apNew[pWal->nWiData], c: 0,
64741 n: sizeof(u32*)*(iPage+1-pWal->nWiData));
64742 pWal->apWiData = apNew;
64743 pWal->nWiData = iPage+1;
64744 }
64745
64746 /* Request a pointer to the required page from the VFS */
64747 assert( pWal->apWiData[iPage]==0 );
64748 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
64749 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
64750 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
64751 }else{
64752 rc = sqlite3OsShmMap(id: pWal->pDbFd, iPage, WALINDEX_PGSZ,
64753 bExtend: pWal->writeLock, pp: (void volatile **)&pWal->apWiData[iPage]
64754 );
64755 assert( pWal->apWiData[iPage]!=0
64756 || rc!=SQLITE_OK
64757 || (pWal->writeLock==0 && iPage==0) );
64758 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
64759 if( rc==SQLITE_OK ){
64760 if( iPage>0 && sqlite3FaultSim(iTest: 600) ) rc = SQLITE_NOMEM;
64761 }else if( (rc&0xff)==SQLITE_READONLY ){
64762 pWal->readOnly |= WAL_SHM_RDONLY;
64763 if( rc==SQLITE_READONLY ){
64764 rc = SQLITE_OK;
64765 }
64766 }
64767 }
64768
64769 *ppPage = pWal->apWiData[iPage];
64770 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
64771 return rc;
64772}
64773static int walIndexPage(
64774 Wal *pWal, /* The WAL context */
64775 int iPage, /* The page we seek */
64776 volatile u32 **ppPage /* Write the page pointer here */
64777){
64778 SEH_INJECT_FAULT;
64779 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
64780 return walIndexPageRealloc(pWal, iPage, ppPage);
64781 }
64782 return SQLITE_OK;
64783}
64784
64785/*
64786** Return a pointer to the WalCkptInfo structure in the wal-index.
64787*/
64788static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
64789 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64790 SEH_INJECT_FAULT;
64791 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
64792}
64793
64794/*
64795** Return a pointer to the WalIndexHdr structure in the wal-index.
64796*/
64797static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
64798 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64799 SEH_INJECT_FAULT;
64800 return (volatile WalIndexHdr*)pWal->apWiData[0];
64801}
64802
64803/*
64804** The argument to this macro must be of type u32. On a little-endian
64805** architecture, it returns the u32 value that results from interpreting
64806** the 4 bytes as a big-endian value. On a big-endian architecture, it
64807** returns the value that would be produced by interpreting the 4 bytes
64808** of the input value as a little-endian integer.
64809*/
64810#define BYTESWAP32(x) ( \
64811 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
64812 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
64813)
64814
64815/*
64816** Generate or extend an 8 byte checksum based on the data in
64817** array aByte[] and the initial values of aIn[0] and aIn[1] (or
64818** initial values of 0 and 0 if aIn==NULL).
64819**
64820** The checksum is written back into aOut[] before returning.
64821**
64822** nByte must be a positive multiple of 8.
64823*/
64824static void walChecksumBytes(
64825 int nativeCksum, /* True for native byte-order, false for non-native */
64826 u8 *a, /* Content to be checksummed */
64827 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
64828 const u32 *aIn, /* Initial checksum value input */
64829 u32 *aOut /* OUT: Final checksum value output */
64830){
64831 u32 s1, s2;
64832 u32 *aData = (u32 *)a;
64833 u32 *aEnd = (u32 *)&a[nByte];
64834
64835 if( aIn ){
64836 s1 = aIn[0];
64837 s2 = aIn[1];
64838 }else{
64839 s1 = s2 = 0;
64840 }
64841
64842 assert( nByte>=8 );
64843 assert( (nByte&0x00000007)==0 );
64844 assert( nByte<=65536 );
64845 assert( nByte%4==0 );
64846
64847 if( !nativeCksum ){
64848 do {
64849 s1 += BYTESWAP32(aData[0]) + s2;
64850 s2 += BYTESWAP32(aData[1]) + s1;
64851 aData += 2;
64852 }while( aData<aEnd );
64853 }else if( nByte%64==0 ){
64854 do {
64855 s1 += *aData++ + s2;
64856 s2 += *aData++ + s1;
64857 s1 += *aData++ + s2;
64858 s2 += *aData++ + s1;
64859 s1 += *aData++ + s2;
64860 s2 += *aData++ + s1;
64861 s1 += *aData++ + s2;
64862 s2 += *aData++ + s1;
64863 s1 += *aData++ + s2;
64864 s2 += *aData++ + s1;
64865 s1 += *aData++ + s2;
64866 s2 += *aData++ + s1;
64867 s1 += *aData++ + s2;
64868 s2 += *aData++ + s1;
64869 s1 += *aData++ + s2;
64870 s2 += *aData++ + s1;
64871 }while( aData<aEnd );
64872 }else{
64873 do {
64874 s1 += *aData++ + s2;
64875 s2 += *aData++ + s1;
64876 }while( aData<aEnd );
64877 }
64878 assert( aData==aEnd );
64879
64880 aOut[0] = s1;
64881 aOut[1] = s2;
64882}
64883
64884/*
64885** If there is the possibility of concurrent access to the SHM file
64886** from multiple threads and/or processes, then do a memory barrier.
64887*/
64888static void walShmBarrier(Wal *pWal){
64889 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
64890 sqlite3OsShmBarrier(id: pWal->pDbFd);
64891 }
64892}
64893
64894/*
64895** Add the SQLITE_NO_TSAN as part of the return-type of a function
64896** definition as a hint that the function contains constructs that
64897** might give false-positive TSAN warnings.
64898**
64899** See tag-20200519-1.
64900*/
64901#if defined(__clang__) && !defined(SQLITE_NO_TSAN)
64902# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread))
64903#else
64904# define SQLITE_NO_TSAN
64905#endif
64906
64907/*
64908** Write the header information in pWal->hdr into the wal-index.
64909**
64910** The checksum on pWal->hdr is updated before it is written.
64911*/
64912static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){
64913 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
64914 const int nCksum = offsetof(WalIndexHdr, aCksum);
64915
64916 assert( pWal->writeLock );
64917 pWal->hdr.isInit = 1;
64918 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
64919 walChecksumBytes(nativeCksum: 1, a: (u8*)&pWal->hdr, nByte: nCksum, aIn: 0, aOut: pWal->hdr.aCksum);
64920 /* Possible TSAN false-positive. See tag-20200519-1 */
64921 memcpy(dest: (void*)&aHdr[1], src: (const void*)&pWal->hdr, n: sizeof(WalIndexHdr));
64922 walShmBarrier(pWal);
64923 memcpy(dest: (void*)&aHdr[0], src: (const void*)&pWal->hdr, n: sizeof(WalIndexHdr));
64924}
64925
64926/*
64927** This function encodes a single frame header and writes it to a buffer
64928** supplied by the caller. A frame-header is made up of a series of
64929** 4-byte big-endian integers, as follows:
64930**
64931** 0: Page number.
64932** 4: For commit records, the size of the database image in pages
64933** after the commit. For all other records, zero.
64934** 8: Salt-1 (copied from the wal-header)
64935** 12: Salt-2 (copied from the wal-header)
64936** 16: Checksum-1.
64937** 20: Checksum-2.
64938*/
64939static void walEncodeFrame(
64940 Wal *pWal, /* The write-ahead log */
64941 u32 iPage, /* Database page number for frame */
64942 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
64943 u8 *aData, /* Pointer to page data */
64944 u8 *aFrame /* OUT: Write encoded frame here */
64945){
64946 int nativeCksum; /* True for native byte-order checksums */
64947 u32 *aCksum = pWal->hdr.aFrameCksum;
64948 assert( WAL_FRAME_HDRSIZE==24 );
64949 sqlite3Put4byte(p: &aFrame[0], v: iPage);
64950 sqlite3Put4byte(p: &aFrame[4], v: nTruncate);
64951 if( pWal->iReCksum==0 ){
64952 memcpy(dest: &aFrame[8], src: pWal->hdr.aSalt, n: 8);
64953
64954 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64955 walChecksumBytes(nativeCksum, a: aFrame, nByte: 8, aIn: aCksum, aOut: aCksum);
64956 walChecksumBytes(nativeCksum, a: aData, nByte: pWal->szPage, aIn: aCksum, aOut: aCksum);
64957
64958 sqlite3Put4byte(p: &aFrame[16], v: aCksum[0]);
64959 sqlite3Put4byte(p: &aFrame[20], v: aCksum[1]);
64960 }else{
64961 memset(s: &aFrame[8], c: 0, n: 16);
64962 }
64963}
64964
64965/*
64966** Check to see if the frame with header in aFrame[] and content
64967** in aData[] is valid. If it is a valid frame, fill *piPage and
64968** *pnTruncate and return true. Return if the frame is not valid.
64969*/
64970static int walDecodeFrame(
64971 Wal *pWal, /* The write-ahead log */
64972 u32 *piPage, /* OUT: Database page number for frame */
64973 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
64974 u8 *aData, /* Pointer to page data (for checksum) */
64975 u8 *aFrame /* Frame data */
64976){
64977 int nativeCksum; /* True for native byte-order checksums */
64978 u32 *aCksum = pWal->hdr.aFrameCksum;
64979 u32 pgno; /* Page number of the frame */
64980 assert( WAL_FRAME_HDRSIZE==24 );
64981
64982 /* A frame is only valid if the salt values in the frame-header
64983 ** match the salt values in the wal-header.
64984 */
64985 if( memcmp(s1: &pWal->hdr.aSalt, s2: &aFrame[8], n: 8)!=0 ){
64986 return 0;
64987 }
64988
64989 /* A frame is only valid if the page number is greater than zero.
64990 */
64991 pgno = sqlite3Get4byte(p: &aFrame[0]);
64992 if( pgno==0 ){
64993 return 0;
64994 }
64995
64996 /* A frame is only valid if a checksum of the WAL header,
64997 ** all prior frames, the first 16 bytes of this frame-header,
64998 ** and the frame-data matches the checksum in the last 8
64999 ** bytes of this frame-header.
65000 */
65001 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65002 walChecksumBytes(nativeCksum, a: aFrame, nByte: 8, aIn: aCksum, aOut: aCksum);
65003 walChecksumBytes(nativeCksum, a: aData, nByte: pWal->szPage, aIn: aCksum, aOut: aCksum);
65004 if( aCksum[0]!=sqlite3Get4byte(p: &aFrame[16])
65005 || aCksum[1]!=sqlite3Get4byte(p: &aFrame[20])
65006 ){
65007 /* Checksum failed. */
65008 return 0;
65009 }
65010
65011 /* If we reach this point, the frame is valid. Return the page number
65012 ** and the new database size.
65013 */
65014 *piPage = pgno;
65015 *pnTruncate = sqlite3Get4byte(p: &aFrame[4]);
65016 return 1;
65017}
65018
65019
65020#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
65021/*
65022** Names of locks. This routine is used to provide debugging output and is not
65023** a part of an ordinary build.
65024*/
65025static const char *walLockName(int lockIdx){
65026 if( lockIdx==WAL_WRITE_LOCK ){
65027 return "WRITE-LOCK";
65028 }else if( lockIdx==WAL_CKPT_LOCK ){
65029 return "CKPT-LOCK";
65030 }else if( lockIdx==WAL_RECOVER_LOCK ){
65031 return "RECOVER-LOCK";
65032 }else{
65033 static char zName[15];
65034 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
65035 lockIdx-WAL_READ_LOCK(0));
65036 return zName;
65037 }
65038}
65039#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
65040
65041
65042/*
65043** Set or release locks on the WAL. Locks are either shared or exclusive.
65044** A lock cannot be moved directly between shared and exclusive - it must go
65045** through the unlocked state first.
65046**
65047** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
65048*/
65049static int walLockShared(Wal *pWal, int lockIdx){
65050 int rc;
65051 if( pWal->exclusiveMode ) return SQLITE_OK;
65052 rc = sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n: 1,
65053 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
65054 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
65055 walLockName(lockIdx), rc ? "failed" : "ok"));
65056 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
65057#ifdef SQLITE_USE_SEH
65058 if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx);
65059#endif
65060 return rc;
65061}
65062static void walUnlockShared(Wal *pWal, int lockIdx){
65063 if( pWal->exclusiveMode ) return;
65064 (void)sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n: 1,
65065 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
65066#ifdef SQLITE_USE_SEH
65067 pWal->lockMask &= ~(1 << lockIdx);
65068#endif
65069 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
65070}
65071static int walLockExclusive(Wal *pWal, int lockIdx, int n){
65072 int rc;
65073 if( pWal->exclusiveMode ) return SQLITE_OK;
65074 rc = sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n,
65075 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
65076 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
65077 walLockName(lockIdx), n, rc ? "failed" : "ok"));
65078 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
65079#ifdef SQLITE_USE_SEH
65080 if( rc==SQLITE_OK ){
65081 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
65082 }
65083#endif
65084 return rc;
65085}
65086static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
65087 if( pWal->exclusiveMode ) return;
65088 (void)sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n,
65089 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
65090#ifdef SQLITE_USE_SEH
65091 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
65092#endif
65093 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
65094 walLockName(lockIdx), n));
65095}
65096
65097/*
65098** Compute a hash on a page number. The resulting hash value must land
65099** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
65100** the hash to the next value in the event of a collision.
65101*/
65102static int walHash(u32 iPage){
65103 assert( iPage>0 );
65104 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
65105 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
65106}
65107static int walNextHash(int iPriorHash){
65108 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
65109}
65110
65111/*
65112** An instance of the WalHashLoc object is used to describe the location
65113** of a page hash table in the wal-index. This becomes the return value
65114** from walHashGet().
65115*/
65116typedef struct WalHashLoc WalHashLoc;
65117struct WalHashLoc {
65118 volatile ht_slot *aHash; /* Start of the wal-index hash table */
65119 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
65120 u32 iZero; /* One less than the frame number of first indexed*/
65121};
65122
65123/*
65124** Return pointers to the hash table and page number array stored on
65125** page iHash of the wal-index. The wal-index is broken into 32KB pages
65126** numbered starting from 0.
65127**
65128** Set output variable pLoc->aHash to point to the start of the hash table
65129** in the wal-index file. Set pLoc->iZero to one less than the frame
65130** number of the first frame indexed by this hash table. If a
65131** slot in the hash table is set to N, it refers to frame number
65132** (pLoc->iZero+N) in the log.
65133**
65134** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
65135** first frame indexed by the hash table, frame (pLoc->iZero).
65136*/
65137static int walHashGet(
65138 Wal *pWal, /* WAL handle */
65139 int iHash, /* Find the iHash'th table */
65140 WalHashLoc *pLoc /* OUT: Hash table location */
65141){
65142 int rc; /* Return code */
65143
65144 rc = walIndexPage(pWal, iPage: iHash, ppPage: &pLoc->aPgno);
65145 assert( rc==SQLITE_OK || iHash>0 );
65146
65147 if( pLoc->aPgno ){
65148 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
65149 if( iHash==0 ){
65150 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
65151 pLoc->iZero = 0;
65152 }else{
65153 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
65154 }
65155 }else if( NEVER(rc==SQLITE_OK) ){
65156 rc = SQLITE_ERROR;
65157 }
65158 return rc;
65159}
65160
65161/*
65162** Return the number of the wal-index page that contains the hash-table
65163** and page-number array that contain entries corresponding to WAL frame
65164** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
65165** are numbered starting from 0.
65166*/
65167static int walFramePage(u32 iFrame){
65168 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
65169 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
65170 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
65171 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
65172 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
65173 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
65174 );
65175 assert( iHash>=0 );
65176 return iHash;
65177}
65178
65179/*
65180** Return the page number associated with frame iFrame in this WAL.
65181*/
65182static u32 walFramePgno(Wal *pWal, u32 iFrame){
65183 int iHash = walFramePage(iFrame);
65184 SEH_INJECT_FAULT;
65185 if( iHash==0 ){
65186 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
65187 }
65188 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
65189}
65190
65191/*
65192** Remove entries from the hash table that point to WAL slots greater
65193** than pWal->hdr.mxFrame.
65194**
65195** This function is called whenever pWal->hdr.mxFrame is decreased due
65196** to a rollback or savepoint.
65197**
65198** At most only the hash table containing pWal->hdr.mxFrame needs to be
65199** updated. Any later hash tables will be automatically cleared when
65200** pWal->hdr.mxFrame advances to the point where those hash tables are
65201** actually needed.
65202*/
65203static void walCleanupHash(Wal *pWal){
65204 WalHashLoc sLoc; /* Hash table location */
65205 int iLimit = 0; /* Zero values greater than this */
65206 int nByte; /* Number of bytes to zero in aPgno[] */
65207 int i; /* Used to iterate through aHash[] */
65208
65209 assert( pWal->writeLock );
65210 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
65211 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
65212 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
65213
65214 if( pWal->hdr.mxFrame==0 ) return;
65215
65216 /* Obtain pointers to the hash-table and page-number array containing
65217 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
65218 ** that the page said hash-table and array reside on is already mapped.(1)
65219 */
65220 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
65221 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
65222 i = walHashGet(pWal, iHash: walFramePage(iFrame: pWal->hdr.mxFrame), pLoc: &sLoc);
65223 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
65224
65225 /* Zero all hash-table entries that correspond to frame numbers greater
65226 ** than pWal->hdr.mxFrame.
65227 */
65228 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
65229 assert( iLimit>0 );
65230 for(i=0; i<HASHTABLE_NSLOT; i++){
65231 if( sLoc.aHash[i]>iLimit ){
65232 sLoc.aHash[i] = 0;
65233 }
65234 }
65235
65236 /* Zero the entries in the aPgno array that correspond to frames with
65237 ** frame numbers greater than pWal->hdr.mxFrame.
65238 */
65239 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
65240 assert( nByte>=0 );
65241 memset(s: (void *)&sLoc.aPgno[iLimit], c: 0, n: nByte);
65242
65243#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
65244 /* Verify that the every entry in the mapping region is still reachable
65245 ** via the hash table even after the cleanup.
65246 */
65247 if( iLimit ){
65248 int j; /* Loop counter */
65249 int iKey; /* Hash key */
65250 for(j=0; j<iLimit; j++){
65251 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
65252 if( sLoc.aHash[iKey]==j+1 ) break;
65253 }
65254 assert( sLoc.aHash[iKey]==j+1 );
65255 }
65256 }
65257#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
65258}
65259
65260
65261/*
65262** Set an entry in the wal-index that will map database page number
65263** pPage into WAL frame iFrame.
65264*/
65265static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
65266 int rc; /* Return code */
65267 WalHashLoc sLoc; /* Wal-index hash table location */
65268
65269 rc = walHashGet(pWal, iHash: walFramePage(iFrame), pLoc: &sLoc);
65270
65271 /* Assuming the wal-index file was successfully mapped, populate the
65272 ** page number array and hash table entry.
65273 */
65274 if( rc==SQLITE_OK ){
65275 int iKey; /* Hash table key */
65276 int idx; /* Value to write to hash-table slot */
65277 int nCollide; /* Number of hash collisions */
65278
65279 idx = iFrame - sLoc.iZero;
65280 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
65281
65282 /* If this is the first entry to be added to this hash-table, zero the
65283 ** entire hash table and aPgno[] array before proceeding.
65284 */
65285 if( idx==1 ){
65286 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
65287 assert( nByte>=0 );
65288 memset(s: (void*)sLoc.aPgno, c: 0, n: nByte);
65289 }
65290
65291 /* If the entry in aPgno[] is already set, then the previous writer
65292 ** must have exited unexpectedly in the middle of a transaction (after
65293 ** writing one or more dirty pages to the WAL to free up memory).
65294 ** Remove the remnants of that writers uncommitted transaction from
65295 ** the hash-table before writing any new entries.
65296 */
65297 if( sLoc.aPgno[idx-1] ){
65298 walCleanupHash(pWal);
65299 assert( !sLoc.aPgno[idx-1] );
65300 }
65301
65302 /* Write the aPgno[] array entry and the hash-table slot. */
65303 nCollide = idx;
65304 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iPriorHash: iKey)){
65305 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
65306 }
65307 sLoc.aPgno[idx-1] = iPage;
65308 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx);
65309
65310#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
65311 /* Verify that the number of entries in the hash table exactly equals
65312 ** the number of entries in the mapping region.
65313 */
65314 {
65315 int i; /* Loop counter */
65316 int nEntry = 0; /* Number of entries in the hash table */
65317 for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
65318 assert( nEntry==idx );
65319 }
65320
65321 /* Verify that the every entry in the mapping region is reachable
65322 ** via the hash table. This turns out to be a really, really expensive
65323 ** thing to check, so only do this occasionally - not on every
65324 ** iteration.
65325 */
65326 if( (idx&0x3ff)==0 ){
65327 int i; /* Loop counter */
65328 for(i=0; i<idx; i++){
65329 for(iKey=walHash(sLoc.aPgno[i]);
65330 sLoc.aHash[iKey];
65331 iKey=walNextHash(iKey)){
65332 if( sLoc.aHash[iKey]==i+1 ) break;
65333 }
65334 assert( sLoc.aHash[iKey]==i+1 );
65335 }
65336 }
65337#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
65338 }
65339
65340 return rc;
65341}
65342
65343
65344/*
65345** Recover the wal-index by reading the write-ahead log file.
65346**
65347** This routine first tries to establish an exclusive lock on the
65348** wal-index to prevent other threads/processes from doing anything
65349** with the WAL or wal-index while recovery is running. The
65350** WAL_RECOVER_LOCK is also held so that other threads will know
65351** that this thread is running recovery. If unable to establish
65352** the necessary locks, this routine returns SQLITE_BUSY.
65353*/
65354static int walIndexRecover(Wal *pWal){
65355 int rc; /* Return Code */
65356 i64 nSize; /* Size of log file */
65357 u32 aFrameCksum[2] = {0, 0};
65358 int iLock; /* Lock offset to lock for checkpoint */
65359
65360 /* Obtain an exclusive lock on all byte in the locking range not already
65361 ** locked by the caller. The caller is guaranteed to have locked the
65362 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
65363 ** If successful, the same bytes that are locked here are unlocked before
65364 ** this function returns.
65365 */
65366 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
65367 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
65368 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
65369 assert( pWal->writeLock );
65370 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
65371 rc = walLockExclusive(pWal, lockIdx: iLock, WAL_READ_LOCK(0)-iLock);
65372 if( rc ){
65373 return rc;
65374 }
65375
65376 WALTRACE(("WAL%p: recovery begin...\n", pWal));
65377
65378 memset(s: &pWal->hdr, c: 0, n: sizeof(WalIndexHdr));
65379
65380 rc = sqlite3OsFileSize(id: pWal->pWalFd, pSize: &nSize);
65381 if( rc!=SQLITE_OK ){
65382 goto recovery_error;
65383 }
65384
65385 if( nSize>WAL_HDRSIZE ){
65386 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
65387 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
65388 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
65389 int szFrame; /* Number of bytes in buffer aFrame[] */
65390 u8 *aData; /* Pointer to data part of aFrame buffer */
65391 int szPage; /* Page size according to the log */
65392 u32 magic; /* Magic value read from WAL header */
65393 u32 version; /* Magic value read from WAL header */
65394 int isValid; /* True if this frame is valid */
65395 u32 iPg; /* Current 32KB wal-index page */
65396 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
65397
65398 /* Read in the WAL header. */
65399 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, WAL_HDRSIZE, offset: 0);
65400 if( rc!=SQLITE_OK ){
65401 goto recovery_error;
65402 }
65403
65404 /* If the database page size is not a power of two, or is greater than
65405 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
65406 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
65407 ** WAL file.
65408 */
65409 magic = sqlite3Get4byte(p: &aBuf[0]);
65410 szPage = sqlite3Get4byte(p: &aBuf[8]);
65411 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
65412 || szPage&(szPage-1)
65413 || szPage>SQLITE_MAX_PAGE_SIZE
65414 || szPage<512
65415 ){
65416 goto finished;
65417 }
65418 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
65419 pWal->szPage = szPage;
65420 pWal->nCkpt = sqlite3Get4byte(p: &aBuf[12]);
65421 memcpy(dest: &pWal->hdr.aSalt, src: &aBuf[16], n: 8);
65422
65423 /* Verify that the WAL header checksum is correct */
65424 walChecksumBytes(nativeCksum: pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
65425 a: aBuf, WAL_HDRSIZE-2*4, aIn: 0, aOut: pWal->hdr.aFrameCksum
65426 );
65427 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(p: &aBuf[24])
65428 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(p: &aBuf[28])
65429 ){
65430 goto finished;
65431 }
65432
65433 /* Verify that the version number on the WAL format is one that
65434 ** are able to understand */
65435 version = sqlite3Get4byte(p: &aBuf[4]);
65436 if( version!=WAL_MAX_VERSION ){
65437 rc = SQLITE_CANTOPEN_BKPT;
65438 goto finished;
65439 }
65440
65441 /* Malloc a buffer to read frames into. */
65442 szFrame = szPage + WAL_FRAME_HDRSIZE;
65443 aFrame = (u8 *)sqlite3_malloc64(n: szFrame + WALINDEX_PGSZ);
65444 SEH_FREE_ON_ERROR(0, aFrame);
65445 if( !aFrame ){
65446 rc = SQLITE_NOMEM_BKPT;
65447 goto recovery_error;
65448 }
65449 aData = &aFrame[WAL_FRAME_HDRSIZE];
65450 aPrivate = (u32*)&aData[szPage];
65451
65452 /* Read all frames from the log file. */
65453 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
65454 for(iPg=0; iPg<=(u32)walFramePage(iFrame: iLastFrame); iPg++){
65455 u32 *aShare;
65456 u32 iFrame; /* Index of last frame read */
65457 u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE);
65458 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
65459 u32 nHdr, nHdr32;
65460 rc = walIndexPage(pWal, iPage: iPg, ppPage: (volatile u32**)&aShare);
65461 assert( aShare!=0 || rc!=SQLITE_OK );
65462 if( aShare==0 ) break;
65463 SEH_SET_ON_ERROR(iPg, aShare);
65464 pWal->apWiData[iPg] = aPrivate;
65465
65466 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
65467 i64 iOffset = walFrameOffset(iFrame, szPage);
65468 u32 pgno; /* Database page number for frame */
65469 u32 nTruncate; /* dbsize field from frame header */
65470
65471 /* Read and decode the next log frame. */
65472 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aFrame, amt: szFrame, offset: iOffset);
65473 if( rc!=SQLITE_OK ) break;
65474 isValid = walDecodeFrame(pWal, piPage: &pgno, pnTruncate: &nTruncate, aData, aFrame);
65475 if( !isValid ) break;
65476 rc = walIndexAppend(pWal, iFrame, iPage: pgno);
65477 if( NEVER(rc!=SQLITE_OK) ) break;
65478
65479 /* If nTruncate is non-zero, this is a commit record. */
65480 if( nTruncate ){
65481 pWal->hdr.mxFrame = iFrame;
65482 pWal->hdr.nPage = nTruncate;
65483 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
65484 testcase( szPage<=32768 );
65485 testcase( szPage>=65536 );
65486 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
65487 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
65488 }
65489 }
65490 pWal->apWiData[iPg] = aShare;
65491 SEH_SET_ON_ERROR(0,0);
65492 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE : 0);
65493 nHdr32 = nHdr / sizeof(u32);
65494#ifndef SQLITE_SAFER_WALINDEX_RECOVERY
65495 /* Memcpy() should work fine here, on all reasonable implementations.
65496 ** Technically, memcpy() might change the destination to some
65497 ** intermediate value before setting to the final value, and that might
65498 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
65499 ** do that, according to the spec, but no memcpy() implementation that
65500 ** we know of actually does that, which is why we say that memcpy()
65501 ** is safe for this. Memcpy() is certainly a lot faster.
65502 */
65503 memcpy(dest: &aShare[nHdr32], src: &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
65504#else
65505 /* In the event that some platform is found for which memcpy()
65506 ** changes the destination to some intermediate value before
65507 ** setting the final value, this alternative copy routine is
65508 ** provided.
65509 */
65510 {
65511 int i;
65512 for(i=nHdr32; i<WALINDEX_PGSZ/sizeof(u32); i++){
65513 if( aShare[i]!=aPrivate[i] ){
65514 /* Atomic memory operations are not required here because if
65515 ** the value needs to be changed, that means it is not being
65516 ** accessed concurrently. */
65517 aShare[i] = aPrivate[i];
65518 }
65519 }
65520 }
65521#endif
65522 SEH_INJECT_FAULT;
65523 if( iFrame<=iLast ) break;
65524 }
65525
65526 SEH_FREE_ON_ERROR(aFrame, 0);
65527 sqlite3_free(p: aFrame);
65528 }
65529
65530finished:
65531 if( rc==SQLITE_OK ){
65532 volatile WalCkptInfo *pInfo;
65533 int i;
65534 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
65535 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
65536 walIndexWriteHdr(pWal);
65537
65538 /* Reset the checkpoint-header. This is safe because this thread is
65539 ** currently holding locks that exclude all other writers and
65540 ** checkpointers. Then set the values of read-mark slots 1 through N.
65541 */
65542 pInfo = walCkptInfo(pWal);
65543 pInfo->nBackfill = 0;
65544 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
65545 pInfo->aReadMark[0] = 0;
65546 for(i=1; i<WAL_NREADER; i++){
65547 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
65548 if( rc==SQLITE_OK ){
65549 if( i==1 && pWal->hdr.mxFrame ){
65550 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
65551 }else{
65552 pInfo->aReadMark[i] = READMARK_NOT_USED;
65553 }
65554 SEH_INJECT_FAULT;
65555 walUnlockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
65556 }else if( rc!=SQLITE_BUSY ){
65557 goto recovery_error;
65558 }
65559 }
65560
65561 /* If more than one frame was recovered from the log file, report an
65562 ** event via sqlite3_log(). This is to help with identifying performance
65563 ** problems caused by applications routinely shutting down without
65564 ** checkpointing the log file.
65565 */
65566 if( pWal->hdr.nPage ){
65567 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
65568 zFormat: "recovered %d frames from WAL file %s",
65569 pWal->hdr.mxFrame, pWal->zWalName
65570 );
65571 }
65572 }
65573
65574recovery_error:
65575 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
65576 walUnlockExclusive(pWal, lockIdx: iLock, WAL_READ_LOCK(0)-iLock);
65577 return rc;
65578}
65579
65580/*
65581** Close an open wal-index.
65582*/
65583static void walIndexClose(Wal *pWal, int isDelete){
65584 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
65585 int i;
65586 for(i=0; i<pWal->nWiData; i++){
65587 sqlite3_free(p: (void *)pWal->apWiData[i]);
65588 pWal->apWiData[i] = 0;
65589 }
65590 }
65591 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
65592 sqlite3OsShmUnmap(id: pWal->pDbFd, deleteFlag: isDelete);
65593 }
65594}
65595
65596/*
65597** Open a connection to the WAL file zWalName. The database file must
65598** already be opened on connection pDbFd. The buffer that zWalName points
65599** to must remain valid for the lifetime of the returned Wal* handle.
65600**
65601** A SHARED lock should be held on the database file when this function
65602** is called. The purpose of this SHARED lock is to prevent any other
65603** client from unlinking the WAL or wal-index file. If another process
65604** were to do this just after this client opened one of these files, the
65605** system would be badly broken.
65606**
65607** If the log file is successfully opened, SQLITE_OK is returned and
65608** *ppWal is set to point to a new WAL handle. If an error occurs,
65609** an SQLite error code is returned and *ppWal is left unmodified.
65610*/
65611SQLITE_PRIVATE int sqlite3WalOpen(
65612 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
65613 sqlite3_file *pDbFd, /* The open database file */
65614 const char *zWalName, /* Name of the WAL file */
65615 int bNoShm, /* True to run in heap-memory mode */
65616 i64 mxWalSize, /* Truncate WAL to this size on reset */
65617 Wal **ppWal /* OUT: Allocated Wal handle */
65618){
65619 int rc; /* Return Code */
65620 Wal *pRet; /* Object to allocate and return */
65621 int flags; /* Flags passed to OsOpen() */
65622
65623 assert( zWalName && zWalName[0] );
65624 assert( pDbFd );
65625
65626 /* Verify the values of various constants. Any changes to the values
65627 ** of these constants would result in an incompatible on-disk format
65628 ** for the -shm file. Any change that causes one of these asserts to
65629 ** fail is a backward compatibility problem, even if the change otherwise
65630 ** works.
65631 **
65632 ** This table also serves as a helpful cross-reference when trying to
65633 ** interpret hex dumps of the -shm file.
65634 */
65635 assert( 48 == sizeof(WalIndexHdr) );
65636 assert( 40 == sizeof(WalCkptInfo) );
65637 assert( 120 == WALINDEX_LOCK_OFFSET );
65638 assert( 136 == WALINDEX_HDR_SIZE );
65639 assert( 4096 == HASHTABLE_NPAGE );
65640 assert( 4062 == HASHTABLE_NPAGE_ONE );
65641 assert( 8192 == HASHTABLE_NSLOT );
65642 assert( 383 == HASHTABLE_HASH_1 );
65643 assert( 32768 == WALINDEX_PGSZ );
65644 assert( 8 == SQLITE_SHM_NLOCK );
65645 assert( 5 == WAL_NREADER );
65646 assert( 24 == WAL_FRAME_HDRSIZE );
65647 assert( 32 == WAL_HDRSIZE );
65648 assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK );
65649 assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK );
65650 assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK );
65651 assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) );
65652 assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) );
65653 assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) );
65654 assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) );
65655 assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) );
65656
65657 /* In the amalgamation, the os_unix.c and os_win.c source files come before
65658 ** this source file. Verify that the #defines of the locking byte offsets
65659 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
65660 ** For that matter, if the lock offset ever changes from its initial design
65661 ** value of 120, we need to know that so there is an assert() to check it.
65662 */
65663#ifdef WIN_SHM_BASE
65664 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
65665#endif
65666#ifdef UNIX_SHM_BASE
65667 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
65668#endif
65669
65670
65671 /* Allocate an instance of struct Wal to return. */
65672 *ppWal = 0;
65673 pRet = (Wal*)sqlite3MallocZero(n: sizeof(Wal) + pVfs->szOsFile);
65674 if( !pRet ){
65675 return SQLITE_NOMEM_BKPT;
65676 }
65677
65678 pRet->pVfs = pVfs;
65679 pRet->pWalFd = (sqlite3_file *)&pRet[1];
65680 pRet->pDbFd = pDbFd;
65681 pRet->readLock = -1;
65682 pRet->mxWalSize = mxWalSize;
65683 pRet->zWalName = zWalName;
65684 pRet->syncHeader = 1;
65685 pRet->padToSectorBoundary = 1;
65686 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
65687
65688 /* Open file handle on the write-ahead log file. */
65689 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
65690 rc = sqlite3OsOpen(pVfs, zPath: zWalName, pFile: pRet->pWalFd, flags, pFlagsOut: &flags);
65691 if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
65692 pRet->readOnly = WAL_RDONLY;
65693 }
65694
65695 if( rc!=SQLITE_OK ){
65696 walIndexClose(pWal: pRet, isDelete: 0);
65697 sqlite3OsClose(pId: pRet->pWalFd);
65698 sqlite3_free(p: pRet);
65699 }else{
65700 int iDC = sqlite3OsDeviceCharacteristics(id: pDbFd);
65701 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
65702 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
65703 pRet->padToSectorBoundary = 0;
65704 }
65705 *ppWal = pRet;
65706 WALTRACE(("WAL%d: opened\n", pRet));
65707 }
65708 return rc;
65709}
65710
65711/*
65712** Change the size to which the WAL file is truncated on each reset.
65713*/
65714SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
65715 if( pWal ) pWal->mxWalSize = iLimit;
65716}
65717
65718/*
65719** Find the smallest page number out of all pages held in the WAL that
65720** has not been returned by any prior invocation of this method on the
65721** same WalIterator object. Write into *piFrame the frame index where
65722** that page was last written into the WAL. Write into *piPage the page
65723** number.
65724**
65725** Return 0 on success. If there are no pages in the WAL with a page
65726** number larger than *piPage, then return 1.
65727*/
65728static int walIteratorNext(
65729 WalIterator *p, /* Iterator */
65730 u32 *piPage, /* OUT: The page number of the next page */
65731 u32 *piFrame /* OUT: Wal frame index of next page */
65732){
65733 u32 iMin; /* Result pgno must be greater than iMin */
65734 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
65735 int i; /* For looping through segments */
65736
65737 iMin = p->iPrior;
65738 assert( iMin<0xffffffff );
65739 for(i=p->nSegment-1; i>=0; i--){
65740 struct WalSegment *pSegment = &p->aSegment[i];
65741 while( pSegment->iNext<pSegment->nEntry ){
65742 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
65743 if( iPg>iMin ){
65744 if( iPg<iRet ){
65745 iRet = iPg;
65746 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
65747 }
65748 break;
65749 }
65750 pSegment->iNext++;
65751 }
65752 }
65753
65754 *piPage = p->iPrior = iRet;
65755 return (iRet==0xFFFFFFFF);
65756}
65757
65758/*
65759** This function merges two sorted lists into a single sorted list.
65760**
65761** aLeft[] and aRight[] are arrays of indices. The sort key is
65762** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
65763** is guaranteed for all J<K:
65764**
65765** aContent[aLeft[J]] < aContent[aLeft[K]]
65766** aContent[aRight[J]] < aContent[aRight[K]]
65767**
65768** This routine overwrites aRight[] with a new (probably longer) sequence
65769** of indices such that the aRight[] contains every index that appears in
65770** either aLeft[] or the old aRight[] and such that the second condition
65771** above is still met.
65772**
65773** The aContent[aLeft[X]] values will be unique for all X. And the
65774** aContent[aRight[X]] values will be unique too. But there might be
65775** one or more combinations of X and Y such that
65776**
65777** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
65778**
65779** When that happens, omit the aLeft[X] and use the aRight[Y] index.
65780*/
65781static void walMerge(
65782 const u32 *aContent, /* Pages in wal - keys for the sort */
65783 ht_slot *aLeft, /* IN: Left hand input list */
65784 int nLeft, /* IN: Elements in array *paLeft */
65785 ht_slot **paRight, /* IN/OUT: Right hand input list */
65786 int *pnRight, /* IN/OUT: Elements in *paRight */
65787 ht_slot *aTmp /* Temporary buffer */
65788){
65789 int iLeft = 0; /* Current index in aLeft */
65790 int iRight = 0; /* Current index in aRight */
65791 int iOut = 0; /* Current index in output buffer */
65792 int nRight = *pnRight;
65793 ht_slot *aRight = *paRight;
65794
65795 assert( nLeft>0 && nRight>0 );
65796 while( iRight<nRight || iLeft<nLeft ){
65797 ht_slot logpage;
65798 Pgno dbpage;
65799
65800 if( (iLeft<nLeft)
65801 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
65802 ){
65803 logpage = aLeft[iLeft++];
65804 }else{
65805 logpage = aRight[iRight++];
65806 }
65807 dbpage = aContent[logpage];
65808
65809 aTmp[iOut++] = logpage;
65810 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
65811
65812 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
65813 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
65814 }
65815
65816 *paRight = aLeft;
65817 *pnRight = iOut;
65818 memcpy(dest: aLeft, src: aTmp, n: sizeof(aTmp[0])*iOut);
65819}
65820
65821/*
65822** Sort the elements in list aList using aContent[] as the sort key.
65823** Remove elements with duplicate keys, preferring to keep the
65824** larger aList[] values.
65825**
65826** The aList[] entries are indices into aContent[]. The values in
65827** aList[] are to be sorted so that for all J<K:
65828**
65829** aContent[aList[J]] < aContent[aList[K]]
65830**
65831** For any X and Y such that
65832**
65833** aContent[aList[X]] == aContent[aList[Y]]
65834**
65835** Keep the larger of the two values aList[X] and aList[Y] and discard
65836** the smaller.
65837*/
65838static void walMergesort(
65839 const u32 *aContent, /* Pages in wal */
65840 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
65841 ht_slot *aList, /* IN/OUT: List to sort */
65842 int *pnList /* IN/OUT: Number of elements in aList[] */
65843){
65844 struct Sublist {
65845 int nList; /* Number of elements in aList */
65846 ht_slot *aList; /* Pointer to sub-list content */
65847 };
65848
65849 const int nList = *pnList; /* Size of input list */
65850 int nMerge = 0; /* Number of elements in list aMerge */
65851 ht_slot *aMerge = 0; /* List to be merged */
65852 int iList; /* Index into input list */
65853 u32 iSub = 0; /* Index into aSub array */
65854 struct Sublist aSub[13]; /* Array of sub-lists */
65855
65856 memset(s: aSub, c: 0, n: sizeof(aSub));
65857 assert( nList<=HASHTABLE_NPAGE && nList>0 );
65858 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
65859
65860 for(iList=0; iList<nList; iList++){
65861 nMerge = 1;
65862 aMerge = &aList[iList];
65863 for(iSub=0; iList & (1<<iSub); iSub++){
65864 struct Sublist *p;
65865 assert( iSub<ArraySize(aSub) );
65866 p = &aSub[iSub];
65867 assert( p->aList && p->nList<=(1<<iSub) );
65868 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
65869 walMerge(aContent, aLeft: p->aList, nLeft: p->nList, paRight: &aMerge, pnRight: &nMerge, aTmp: aBuffer);
65870 }
65871 aSub[iSub].aList = aMerge;
65872 aSub[iSub].nList = nMerge;
65873 }
65874
65875 for(iSub++; iSub<ArraySize(aSub); iSub++){
65876 if( nList & (1<<iSub) ){
65877 struct Sublist *p;
65878 assert( iSub<ArraySize(aSub) );
65879 p = &aSub[iSub];
65880 assert( p->nList<=(1<<iSub) );
65881 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
65882 walMerge(aContent, aLeft: p->aList, nLeft: p->nList, paRight: &aMerge, pnRight: &nMerge, aTmp: aBuffer);
65883 }
65884 }
65885 assert( aMerge==aList );
65886 *pnList = nMerge;
65887
65888#ifdef SQLITE_DEBUG
65889 {
65890 int i;
65891 for(i=1; i<*pnList; i++){
65892 assert( aContent[aList[i]] > aContent[aList[i-1]] );
65893 }
65894 }
65895#endif
65896}
65897
65898/*
65899** Free an iterator allocated by walIteratorInit().
65900*/
65901static void walIteratorFree(WalIterator *p){
65902 sqlite3_free(p);
65903}
65904
65905/*
65906** Construct a WalInterator object that can be used to loop over all
65907** pages in the WAL following frame nBackfill in ascending order. Frames
65908** nBackfill or earlier may be included - excluding them is an optimization
65909** only. The caller must hold the checkpoint lock.
65910**
65911** On success, make *pp point to the newly allocated WalInterator object
65912** return SQLITE_OK. Otherwise, return an error code. If this routine
65913** returns an error, the value of *pp is undefined.
65914**
65915** The calling routine should invoke walIteratorFree() to destroy the
65916** WalIterator object when it has finished with it.
65917*/
65918static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
65919 WalIterator *p; /* Return value */
65920 int nSegment; /* Number of segments to merge */
65921 u32 iLast; /* Last frame in log */
65922 sqlite3_int64 nByte; /* Number of bytes to allocate */
65923 int i; /* Iterator variable */
65924 ht_slot *aTmp; /* Temp space used by merge-sort */
65925 int rc = SQLITE_OK; /* Return Code */
65926
65927 /* This routine only runs while holding the checkpoint lock. And
65928 ** it only runs if there is actually content in the log (mxFrame>0).
65929 */
65930 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
65931 iLast = pWal->hdr.mxFrame;
65932
65933 /* Allocate space for the WalIterator object. */
65934 nSegment = walFramePage(iFrame: iLast) + 1;
65935 nByte = sizeof(WalIterator)
65936 + (nSegment-1)*sizeof(struct WalSegment)
65937 + iLast*sizeof(ht_slot);
65938 p = (WalIterator *)sqlite3_malloc64(n: nByte
65939 + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
65940 );
65941 if( !p ){
65942 return SQLITE_NOMEM_BKPT;
65943 }
65944 memset(s: p, c: 0, n: nByte);
65945 p->nSegment = nSegment;
65946 aTmp = (ht_slot*)&(((u8*)p)[nByte]);
65947 SEH_FREE_ON_ERROR(0, p);
65948 for(i=walFramePage(iFrame: nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
65949 WalHashLoc sLoc;
65950
65951 rc = walHashGet(pWal, iHash: i, pLoc: &sLoc);
65952 if( rc==SQLITE_OK ){
65953 int j; /* Counter variable */
65954 int nEntry; /* Number of entries in this segment */
65955 ht_slot *aIndex; /* Sorted index for this segment */
65956
65957 if( (i+1)==nSegment ){
65958 nEntry = (int)(iLast - sLoc.iZero);
65959 }else{
65960 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
65961 }
65962 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
65963 sLoc.iZero++;
65964
65965 for(j=0; j<nEntry; j++){
65966 aIndex[j] = (ht_slot)j;
65967 }
65968 walMergesort(aContent: (u32 *)sLoc.aPgno, aBuffer: aTmp, aList: aIndex, pnList: &nEntry);
65969 p->aSegment[i].iZero = sLoc.iZero;
65970 p->aSegment[i].nEntry = nEntry;
65971 p->aSegment[i].aIndex = aIndex;
65972 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
65973 }
65974 }
65975 if( rc!=SQLITE_OK ){
65976 SEH_FREE_ON_ERROR(p, 0);
65977 walIteratorFree(p);
65978 p = 0;
65979 }
65980 *pp = p;
65981 return rc;
65982}
65983
65984#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
65985/*
65986** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
65987** they are supported by the VFS, and (b) the database handle is configured
65988** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
65989** or 0 otherwise.
65990*/
65991static int walEnableBlocking(Wal *pWal){
65992 int res = 0;
65993 if( pWal->db ){
65994 int tmout = pWal->db->busyTimeout;
65995 if( tmout ){
65996 int rc;
65997 rc = sqlite3OsFileControl(
65998 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
65999 );
66000 res = (rc==SQLITE_OK);
66001 }
66002 }
66003 return res;
66004}
66005
66006/*
66007** Disable blocking locks.
66008*/
66009static void walDisableBlocking(Wal *pWal){
66010 int tmout = 0;
66011 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
66012}
66013
66014/*
66015** If parameter bLock is true, attempt to enable blocking locks, take
66016** the WRITER lock, and then disable blocking locks. If blocking locks
66017** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
66018** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
66019** an error if blocking locks can not be enabled.
66020**
66021** If the bLock parameter is false and the WRITER lock is held, release it.
66022*/
66023SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){
66024 int rc = SQLITE_OK;
66025 assert( pWal->readLock<0 || bLock==0 );
66026 if( bLock ){
66027 assert( pWal->db );
66028 if( walEnableBlocking(pWal) ){
66029 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
66030 if( rc==SQLITE_OK ){
66031 pWal->writeLock = 1;
66032 }
66033 walDisableBlocking(pWal);
66034 }
66035 }else if( pWal->writeLock ){
66036 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
66037 pWal->writeLock = 0;
66038 }
66039 return rc;
66040}
66041
66042/*
66043** Set the database handle used to determine if blocking locks are required.
66044*/
66045SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){
66046 pWal->db = db;
66047}
66048
66049/*
66050** Take an exclusive WRITE lock. Blocking if so configured.
66051*/
66052static int walLockWriter(Wal *pWal){
66053 int rc;
66054 walEnableBlocking(pWal);
66055 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
66056 walDisableBlocking(pWal);
66057 return rc;
66058}
66059#else
66060# define walEnableBlocking(x) 0
66061# define walDisableBlocking(x)
66062# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1)
66063# define sqlite3WalDb(pWal, db)
66064#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
66065
66066
66067/*
66068** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
66069** n. If the attempt fails and parameter xBusy is not NULL, then it is a
66070** busy-handler function. Invoke it and retry the lock until either the
66071** lock is successfully obtained or the busy-handler returns 0.
66072*/
66073static int walBusyLock(
66074 Wal *pWal, /* WAL connection */
66075 int (*xBusy)(void*), /* Function to call when busy */
66076 void *pBusyArg, /* Context argument for xBusyHandler */
66077 int lockIdx, /* Offset of first byte to lock */
66078 int n /* Number of bytes to lock */
66079){
66080 int rc;
66081 do {
66082 rc = walLockExclusive(pWal, lockIdx, n);
66083 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
66084#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
66085 if( rc==SQLITE_BUSY_TIMEOUT ){
66086 walDisableBlocking(pWal);
66087 rc = SQLITE_BUSY;
66088 }
66089#endif
66090 return rc;
66091}
66092
66093/*
66094** The cache of the wal-index header must be valid to call this function.
66095** Return the page-size in bytes used by the database.
66096*/
66097static int walPagesize(Wal *pWal){
66098 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
66099}
66100
66101/*
66102** The following is guaranteed when this function is called:
66103**
66104** a) the WRITER lock is held,
66105** b) the entire log file has been checkpointed, and
66106** c) any existing readers are reading exclusively from the database
66107** file - there are no readers that may attempt to read a frame from
66108** the log file.
66109**
66110** This function updates the shared-memory structures so that the next
66111** client to write to the database (which may be this one) does so by
66112** writing frames into the start of the log file.
66113**
66114** The value of parameter salt1 is used as the aSalt[1] value in the
66115** new wal-index header. It should be passed a pseudo-random value (i.e.
66116** one obtained from sqlite3_randomness()).
66117*/
66118static void walRestartHdr(Wal *pWal, u32 salt1){
66119 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
66120 int i; /* Loop counter */
66121 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
66122 pWal->nCkpt++;
66123 pWal->hdr.mxFrame = 0;
66124 sqlite3Put4byte(p: (u8*)&aSalt[0], v: 1 + sqlite3Get4byte(p: (u8*)&aSalt[0]));
66125 memcpy(dest: &pWal->hdr.aSalt[1], src: &salt1, n: 4);
66126 walIndexWriteHdr(pWal);
66127 AtomicStore(&pInfo->nBackfill, 0);
66128 pInfo->nBackfillAttempted = 0;
66129 pInfo->aReadMark[1] = 0;
66130 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
66131 assert( pInfo->aReadMark[0]==0 );
66132}
66133
66134/*
66135** Copy as much content as we can from the WAL back into the database file
66136** in response to an sqlite3_wal_checkpoint() request or the equivalent.
66137**
66138** The amount of information copies from WAL to database might be limited
66139** by active readers. This routine will never overwrite a database page
66140** that a concurrent reader might be using.
66141**
66142** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
66143** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
66144** checkpoints are always run by a background thread or background
66145** process, foreground threads will never block on a lengthy fsync call.
66146**
66147** Fsync is called on the WAL before writing content out of the WAL and
66148** into the database. This ensures that if the new content is persistent
66149** in the WAL and can be recovered following a power-loss or hard reset.
66150**
66151** Fsync is also called on the database file if (and only if) the entire
66152** WAL content is copied into the database file. This second fsync makes
66153** it safe to delete the WAL since the new content will persist in the
66154** database file.
66155**
66156** This routine uses and updates the nBackfill field of the wal-index header.
66157** This is the only routine that will increase the value of nBackfill.
66158** (A WAL reset or recovery will revert nBackfill to zero, but not increase
66159** its value.)
66160**
66161** The caller must be holding sufficient locks to ensure that no other
66162** checkpoint is running (in any other thread or process) at the same
66163** time.
66164*/
66165static int walCheckpoint(
66166 Wal *pWal, /* Wal connection */
66167 sqlite3 *db, /* Check for interrupts on this handle */
66168 int eMode, /* One of PASSIVE, FULL or RESTART */
66169 int (*xBusy)(void*), /* Function to call when busy */
66170 void *pBusyArg, /* Context argument for xBusyHandler */
66171 int sync_flags, /* Flags for OsSync() (or 0) */
66172 u8 *zBuf /* Temporary buffer to use */
66173){
66174 int rc = SQLITE_OK; /* Return code */
66175 int szPage; /* Database page-size */
66176 WalIterator *pIter = 0; /* Wal iterator context */
66177 u32 iDbpage = 0; /* Next database page to write */
66178 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
66179 u32 mxSafeFrame; /* Max frame that can be backfilled */
66180 u32 mxPage; /* Max database page to write */
66181 int i; /* Loop counter */
66182 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
66183
66184 szPage = walPagesize(pWal);
66185 testcase( szPage<=32768 );
66186 testcase( szPage>=65536 );
66187 pInfo = walCkptInfo(pWal);
66188 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
66189
66190 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
66191 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
66192 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
66193
66194 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
66195 ** safe to write into the database. Frames beyond mxSafeFrame might
66196 ** overwrite database pages that are in use by active readers and thus
66197 ** cannot be backfilled from the WAL.
66198 */
66199 mxSafeFrame = pWal->hdr.mxFrame;
66200 mxPage = pWal->hdr.nPage;
66201 for(i=1; i<WAL_NREADER; i++){
66202 u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
66203 if( mxSafeFrame>y ){
66204 assert( y<=pWal->hdr.mxFrame );
66205 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), n: 1);
66206 if( rc==SQLITE_OK ){
66207 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
66208 AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT;
66209 walUnlockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
66210 }else if( rc==SQLITE_BUSY ){
66211 mxSafeFrame = y;
66212 xBusy = 0;
66213 }else{
66214 goto walcheckpoint_out;
66215 }
66216 }
66217 }
66218
66219 /* Allocate the iterator */
66220 if( pInfo->nBackfill<mxSafeFrame ){
66221 rc = walIteratorInit(pWal, nBackfill: pInfo->nBackfill, pp: &pIter);
66222 assert( rc==SQLITE_OK || pIter==0 );
66223 }
66224
66225 if( pIter
66226 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),n: 1))==SQLITE_OK
66227 ){
66228 u32 nBackfill = pInfo->nBackfill;
66229 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT;
66230
66231 /* Sync the WAL to disk */
66232 rc = sqlite3OsSync(id: pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
66233
66234 /* If the database may grow as a result of this checkpoint, hint
66235 ** about the eventual size of the db file to the VFS layer.
66236 */
66237 if( rc==SQLITE_OK ){
66238 i64 nReq = ((i64)mxPage * szPage);
66239 i64 nSize; /* Current size of database file */
66240 sqlite3OsFileControl(id: pWal->pDbFd, SQLITE_FCNTL_CKPT_START, pArg: 0);
66241 rc = sqlite3OsFileSize(id: pWal->pDbFd, pSize: &nSize);
66242 if( rc==SQLITE_OK && nSize<nReq ){
66243 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
66244 /* If the size of the final database is larger than the current
66245 ** database plus the amount of data in the wal file, plus the
66246 ** maximum size of the pending-byte page (65536 bytes), then
66247 ** must be corruption somewhere. */
66248 rc = SQLITE_CORRUPT_BKPT;
66249 }else{
66250 sqlite3OsFileControlHint(id: pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,pArg: &nReq);
66251 }
66252 }
66253
66254 }
66255
66256 /* Iterate through the contents of the WAL, copying data to the db file */
66257 while( rc==SQLITE_OK && 0==walIteratorNext(p: pIter, piPage: &iDbpage, piFrame: &iFrame) ){
66258 i64 iOffset;
66259 assert( walFramePgno(pWal, iFrame)==iDbpage );
66260 SEH_INJECT_FAULT;
66261 if( AtomicLoad(&db->u1.isInterrupted) ){
66262 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
66263 break;
66264 }
66265 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
66266 continue;
66267 }
66268 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
66269 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
66270 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: zBuf, amt: szPage, offset: iOffset);
66271 if( rc!=SQLITE_OK ) break;
66272 iOffset = (iDbpage-1)*(i64)szPage;
66273 testcase( IS_BIG_INT(iOffset) );
66274 rc = sqlite3OsWrite(id: pWal->pDbFd, pBuf: zBuf, amt: szPage, offset: iOffset);
66275 if( rc!=SQLITE_OK ) break;
66276 }
66277 sqlite3OsFileControl(id: pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, pArg: 0);
66278
66279 /* If work was actually accomplished... */
66280 if( rc==SQLITE_OK ){
66281 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
66282 i64 szDb = pWal->hdr.nPage*(i64)szPage;
66283 testcase( IS_BIG_INT(szDb) );
66284 rc = sqlite3OsTruncate(id: pWal->pDbFd, size: szDb);
66285 if( rc==SQLITE_OK ){
66286 rc = sqlite3OsSync(id: pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
66287 }
66288 }
66289 if( rc==SQLITE_OK ){
66290 AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT;
66291 }
66292 }
66293
66294 /* Release the reader lock held while backfilling */
66295 walUnlockExclusive(pWal, WAL_READ_LOCK(0), n: 1);
66296 }
66297
66298 if( rc==SQLITE_BUSY ){
66299 /* Reset the return code so as not to report a checkpoint failure
66300 ** just because there are active readers. */
66301 rc = SQLITE_OK;
66302 }
66303 }
66304
66305 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
66306 ** entire wal file has been copied into the database file, then block
66307 ** until all readers have finished using the wal file. This ensures that
66308 ** the next process to write to the database restarts the wal file.
66309 */
66310 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
66311 assert( pWal->writeLock );
66312 SEH_INJECT_FAULT;
66313 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
66314 rc = SQLITE_BUSY;
66315 }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
66316 u32 salt1;
66317 sqlite3_randomness(N: 4, pBuf: &salt1);
66318 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
66319 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
66320 if( rc==SQLITE_OK ){
66321 if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
66322 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
66323 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
66324 ** truncates the log file to zero bytes just prior to a
66325 ** successful return.
66326 **
66327 ** In theory, it might be safe to do this without updating the
66328 ** wal-index header in shared memory, as all subsequent reader or
66329 ** writer clients should see that the entire log file has been
66330 ** checkpointed and behave accordingly. This seems unsafe though,
66331 ** as it would leave the system in a state where the contents of
66332 ** the wal-index header do not match the contents of the
66333 ** file-system. To avoid this, update the wal-index header to
66334 ** indicate that the log file contains zero valid frames. */
66335 walRestartHdr(pWal, salt1);
66336 rc = sqlite3OsTruncate(id: pWal->pWalFd, size: 0);
66337 }
66338 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
66339 }
66340 }
66341 }
66342
66343 walcheckpoint_out:
66344 SEH_FREE_ON_ERROR(pIter, 0);
66345 walIteratorFree(p: pIter);
66346 return rc;
66347}
66348
66349/*
66350** If the WAL file is currently larger than nMax bytes in size, truncate
66351** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
66352*/
66353static void walLimitSize(Wal *pWal, i64 nMax){
66354 i64 sz;
66355 int rx;
66356 sqlite3BeginBenignMalloc();
66357 rx = sqlite3OsFileSize(id: pWal->pWalFd, pSize: &sz);
66358 if( rx==SQLITE_OK && (sz > nMax ) ){
66359 rx = sqlite3OsTruncate(id: pWal->pWalFd, size: nMax);
66360 }
66361 sqlite3EndBenignMalloc();
66362 if( rx ){
66363 sqlite3_log(iErrCode: rx, zFormat: "cannot limit WAL size: %s", pWal->zWalName);
66364 }
66365}
66366
66367#ifdef SQLITE_USE_SEH
66368/*
66369** This is the "standard" exception handler used in a few places to handle
66370** an exception thrown by reading from the *-shm mapping after it has become
66371** invalid in SQLITE_USE_SEH builds. It is used as follows:
66372**
66373** SEH_TRY { ... }
66374** SEH_EXCEPT( rc = walHandleException(pWal); )
66375**
66376** This function does three things:
66377**
66378** 1) Determines the locks that should be held, based on the contents of
66379** the Wal.readLock, Wal.writeLock and Wal.ckptLock variables. All other
66380** held locks are assumed to be transient locks that would have been
66381** released had the exception not been thrown and are dropped.
66382**
66383** 2) Frees the pointer at Wal.pFree, if any, using sqlite3_free().
66384**
66385** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
66386**
66387** 4) Returns SQLITE_IOERR.
66388*/
66389static int walHandleException(Wal *pWal){
66390 if( pWal->exclusiveMode==0 ){
66391 static const int S = 1;
66392 static const int E = (1<<SQLITE_SHM_NLOCK);
66393 int ii;
66394 u32 mUnlock = pWal->lockMask & ~(
66395 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
66396 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
66397 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
66398 );
66399 for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){
66400 if( (S<<ii) & mUnlock ) walUnlockShared(pWal, ii);
66401 if( (E<<ii) & mUnlock ) walUnlockExclusive(pWal, ii, 1);
66402 }
66403 }
66404 sqlite3_free(pWal->pFree);
66405 pWal->pFree = 0;
66406 if( pWal->pWiValue ){
66407 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
66408 pWal->pWiValue = 0;
66409 }
66410 return SQLITE_IOERR_IN_PAGE;
66411}
66412
66413/*
66414** Assert that the Wal.lockMask mask, which indicates the locks held
66415** by the connenction, is consistent with the Wal.readLock, Wal.writeLock
66416** and Wal.ckptLock variables. To be used as:
66417**
66418** assert( walAssertLockmask(pWal) );
66419*/
66420static int walAssertLockmask(Wal *pWal){
66421 if( pWal->exclusiveMode==0 ){
66422 static const int S = 1;
66423 static const int E = (1<<SQLITE_SHM_NLOCK);
66424 u32 mExpect = (
66425 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
66426 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
66427 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
66428#ifdef SQLITE_ENABLE_SNAPSHOT
66429 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0)
66430#endif
66431 );
66432 assert( mExpect==pWal->lockMask );
66433 }
66434 return 1;
66435}
66436
66437/*
66438** Return and zero the "system error" field set when an
66439** EXCEPTION_IN_PAGE_ERROR exception is caught.
66440*/
66441SQLITE_PRIVATE int sqlite3WalSystemErrno(Wal *pWal){
66442 int iRet = 0;
66443 if( pWal ){
66444 iRet = pWal->iSysErrno;
66445 pWal->iSysErrno = 0;
66446 }
66447 return iRet;
66448}
66449
66450#else
66451# define walAssertLockmask(x) 1
66452#endif /* ifdef SQLITE_USE_SEH */
66453
66454/*
66455** Close a connection to a log file.
66456*/
66457SQLITE_PRIVATE int sqlite3WalClose(
66458 Wal *pWal, /* Wal to close */
66459 sqlite3 *db, /* For interrupt flag */
66460 int sync_flags, /* Flags to pass to OsSync() (or 0) */
66461 int nBuf,
66462 u8 *zBuf /* Buffer of at least nBuf bytes */
66463){
66464 int rc = SQLITE_OK;
66465 if( pWal ){
66466 int isDelete = 0; /* True to unlink wal and wal-index files */
66467
66468 assert( walAssertLockmask(pWal) );
66469
66470 /* If an EXCLUSIVE lock can be obtained on the database file (using the
66471 ** ordinary, rollback-mode locking methods, this guarantees that the
66472 ** connection associated with this log file is the only connection to
66473 ** the database. In this case checkpoint the database and unlink both
66474 ** the wal and wal-index files.
66475 **
66476 ** The EXCLUSIVE lock is not released before returning.
66477 */
66478 if( zBuf!=0
66479 && SQLITE_OK==(rc = sqlite3OsLock(id: pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
66480 ){
66481 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
66482 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
66483 }
66484 rc = sqlite3WalCheckpoint(pWal, db,
66485 SQLITE_CHECKPOINT_PASSIVE, xBusy: 0, pBusyArg: 0, sync_flags, nBuf, zBuf, pnLog: 0, pnCkpt: 0
66486 );
66487 if( rc==SQLITE_OK ){
66488 int bPersist = -1;
66489 sqlite3OsFileControlHint(
66490 id: pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, pArg: &bPersist
66491 );
66492 if( bPersist!=1 ){
66493 /* Try to delete the WAL file if the checkpoint completed and
66494 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
66495 ** mode (!bPersist) */
66496 isDelete = 1;
66497 }else if( pWal->mxWalSize>=0 ){
66498 /* Try to truncate the WAL file to zero bytes if the checkpoint
66499 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
66500 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
66501 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
66502 ** to zero bytes as truncating to the journal_size_limit might
66503 ** leave a corrupt WAL file on disk. */
66504 walLimitSize(pWal, nMax: 0);
66505 }
66506 }
66507 }
66508
66509 walIndexClose(pWal, isDelete);
66510 sqlite3OsClose(pId: pWal->pWalFd);
66511 if( isDelete ){
66512 sqlite3BeginBenignMalloc();
66513 sqlite3OsDelete(pVfs: pWal->pVfs, zPath: pWal->zWalName, dirSync: 0);
66514 sqlite3EndBenignMalloc();
66515 }
66516 WALTRACE(("WAL%p: closed\n", pWal));
66517 sqlite3_free(p: (void *)pWal->apWiData);
66518 sqlite3_free(p: pWal);
66519 }
66520 return rc;
66521}
66522
66523/*
66524** Try to read the wal-index header. Return 0 on success and 1 if
66525** there is a problem.
66526**
66527** The wal-index is in shared memory. Another thread or process might
66528** be writing the header at the same time this procedure is trying to
66529** read it, which might result in inconsistency. A dirty read is detected
66530** by verifying that both copies of the header are the same and also by
66531** a checksum on the header.
66532**
66533** If and only if the read is consistent and the header is different from
66534** pWal->hdr, then pWal->hdr is updated to the content of the new header
66535** and *pChanged is set to 1.
66536**
66537** If the checksum cannot be verified return non-zero. If the header
66538** is read successfully and the checksum verified, return zero.
66539*/
66540static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
66541 u32 aCksum[2]; /* Checksum on the header content */
66542 WalIndexHdr h1, h2; /* Two copies of the header content */
66543 WalIndexHdr volatile *aHdr; /* Header in shared memory */
66544
66545 /* The first page of the wal-index must be mapped at this point. */
66546 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66547
66548 /* Read the header. This might happen concurrently with a write to the
66549 ** same area of shared memory on a different CPU in a SMP,
66550 ** meaning it is possible that an inconsistent snapshot is read
66551 ** from the file. If this happens, return non-zero.
66552 **
66553 ** tag-20200519-1:
66554 ** There are two copies of the header at the beginning of the wal-index.
66555 ** When reading, read [0] first then [1]. Writes are in the reverse order.
66556 ** Memory barriers are used to prevent the compiler or the hardware from
66557 ** reordering the reads and writes. TSAN and similar tools can sometimes
66558 ** give false-positive warnings about these accesses because the tools do not
66559 ** account for the double-read and the memory barrier. The use of mutexes
66560 ** here would be problematic as the memory being accessed is potentially
66561 ** shared among multiple processes and not all mutex implementations work
66562 ** reliably in that environment.
66563 */
66564 aHdr = walIndexHdr(pWal);
66565 memcpy(dest: &h1, src: (void *)&aHdr[0], n: sizeof(h1)); /* Possible TSAN false-positive */
66566 walShmBarrier(pWal);
66567 memcpy(dest: &h2, src: (void *)&aHdr[1], n: sizeof(h2));
66568
66569 if( memcmp(s1: &h1, s2: &h2, n: sizeof(h1))!=0 ){
66570 return 1; /* Dirty read */
66571 }
66572 if( h1.isInit==0 ){
66573 return 1; /* Malformed header - probably all zeros */
66574 }
66575 walChecksumBytes(nativeCksum: 1, a: (u8*)&h1, nByte: sizeof(h1)-sizeof(h1.aCksum), aIn: 0, aOut: aCksum);
66576 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
66577 return 1; /* Checksum does not match */
66578 }
66579
66580 if( memcmp(s1: &pWal->hdr, s2: &h1, n: sizeof(WalIndexHdr)) ){
66581 *pChanged = 1;
66582 memcpy(dest: &pWal->hdr, src: &h1, n: sizeof(WalIndexHdr));
66583 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
66584 testcase( pWal->szPage<=32768 );
66585 testcase( pWal->szPage>=65536 );
66586 }
66587
66588 /* The header was successfully read. Return zero. */
66589 return 0;
66590}
66591
66592/*
66593** This is the value that walTryBeginRead returns when it needs to
66594** be retried.
66595*/
66596#define WAL_RETRY (-1)
66597
66598/*
66599** Read the wal-index header from the wal-index and into pWal->hdr.
66600** If the wal-header appears to be corrupt, try to reconstruct the
66601** wal-index from the WAL before returning.
66602**
66603** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
66604** changed by this operation. If pWal->hdr is unchanged, set *pChanged
66605** to 0.
66606**
66607** If the wal-index header is successfully read, return SQLITE_OK.
66608** Otherwise an SQLite error code.
66609*/
66610static int walIndexReadHdr(Wal *pWal, int *pChanged){
66611 int rc; /* Return code */
66612 int badHdr; /* True if a header read failed */
66613 volatile u32 *page0; /* Chunk of wal-index containing header */
66614
66615 /* Ensure that page 0 of the wal-index (the page that contains the
66616 ** wal-index header) is mapped. Return early if an error occurs here.
66617 */
66618 assert( pChanged );
66619 rc = walIndexPage(pWal, iPage: 0, ppPage: &page0);
66620 if( rc!=SQLITE_OK ){
66621 assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
66622 if( rc==SQLITE_READONLY_CANTINIT ){
66623 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
66624 ** was openable but is not writable, and this thread is unable to
66625 ** confirm that another write-capable connection has the shared-memory
66626 ** open, and hence the content of the shared-memory is unreliable,
66627 ** since the shared-memory might be inconsistent with the WAL file
66628 ** and there is no writer on hand to fix it. */
66629 assert( page0==0 );
66630 assert( pWal->writeLock==0 );
66631 assert( pWal->readOnly & WAL_SHM_RDONLY );
66632 pWal->bShmUnreliable = 1;
66633 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
66634 *pChanged = 1;
66635 }else{
66636 return rc; /* Any other non-OK return is just an error */
66637 }
66638 }else{
66639 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
66640 ** is zero, which prevents the SHM from growing */
66641 testcase( page0!=0 );
66642 }
66643 assert( page0!=0 || pWal->writeLock==0 );
66644
66645 /* If the first page of the wal-index has been mapped, try to read the
66646 ** wal-index header immediately, without holding any lock. This usually
66647 ** works, but may fail if the wal-index header is corrupt or currently
66648 ** being modified by another thread or process.
66649 */
66650 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
66651
66652 /* If the first attempt failed, it might have been due to a race
66653 ** with a writer. So get a WRITE lock and try again.
66654 */
66655 if( badHdr ){
66656 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
66657 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
66658 walUnlockShared(pWal, WAL_WRITE_LOCK);
66659 rc = SQLITE_READONLY_RECOVERY;
66660 }
66661 }else{
66662 int bWriteLock = pWal->writeLock;
66663 if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){
66664 pWal->writeLock = 1;
66665 if( SQLITE_OK==(rc = walIndexPage(pWal, iPage: 0, ppPage: &page0)) ){
66666 badHdr = walIndexTryHdr(pWal, pChanged);
66667 if( badHdr ){
66668 /* If the wal-index header is still malformed even while holding
66669 ** a WRITE lock, it can only mean that the header is corrupted and
66670 ** needs to be reconstructed. So run recovery to do exactly that.
66671 */
66672 rc = walIndexRecover(pWal);
66673 *pChanged = 1;
66674 }
66675 }
66676 if( bWriteLock==0 ){
66677 pWal->writeLock = 0;
66678 walUnlockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
66679 }
66680 }
66681 }
66682 }
66683
66684 /* If the header is read successfully, check the version number to make
66685 ** sure the wal-index was not constructed with some future format that
66686 ** this version of SQLite cannot understand.
66687 */
66688 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
66689 rc = SQLITE_CANTOPEN_BKPT;
66690 }
66691 if( pWal->bShmUnreliable ){
66692 if( rc!=SQLITE_OK ){
66693 walIndexClose(pWal, isDelete: 0);
66694 pWal->bShmUnreliable = 0;
66695 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
66696 /* walIndexRecover() might have returned SHORT_READ if a concurrent
66697 ** writer truncated the WAL out from under it. If that happens, it
66698 ** indicates that a writer has fixed the SHM file for us, so retry */
66699 if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
66700 }
66701 pWal->exclusiveMode = WAL_NORMAL_MODE;
66702 }
66703
66704 return rc;
66705}
66706
66707/*
66708** Open a transaction in a connection where the shared-memory is read-only
66709** and where we cannot verify that there is a separate write-capable connection
66710** on hand to keep the shared-memory up-to-date with the WAL file.
66711**
66712** This can happen, for example, when the shared-memory is implemented by
66713** memory-mapping a *-shm file, where a prior writer has shut down and
66714** left the *-shm file on disk, and now the present connection is trying
66715** to use that database but lacks write permission on the *-shm file.
66716** Other scenarios are also possible, depending on the VFS implementation.
66717**
66718** Precondition:
66719**
66720** The *-wal file has been read and an appropriate wal-index has been
66721** constructed in pWal->apWiData[] using heap memory instead of shared
66722** memory.
66723**
66724** If this function returns SQLITE_OK, then the read transaction has
66725** been successfully opened. In this case output variable (*pChanged)
66726** is set to true before returning if the caller should discard the
66727** contents of the page cache before proceeding. Or, if it returns
66728** WAL_RETRY, then the heap memory wal-index has been discarded and
66729** the caller should retry opening the read transaction from the
66730** beginning (including attempting to map the *-shm file).
66731**
66732** If an error occurs, an SQLite error code is returned.
66733*/
66734static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
66735 i64 szWal; /* Size of wal file on disk in bytes */
66736 i64 iOffset; /* Current offset when reading wal file */
66737 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
66738 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
66739 int szFrame; /* Number of bytes in buffer aFrame[] */
66740 u8 *aData; /* Pointer to data part of aFrame buffer */
66741 volatile void *pDummy; /* Dummy argument for xShmMap */
66742 int rc; /* Return code */
66743 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
66744
66745 assert( pWal->bShmUnreliable );
66746 assert( pWal->readOnly & WAL_SHM_RDONLY );
66747 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66748
66749 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
66750 ** writers from running a checkpoint, but does not stop them
66751 ** from running recovery. */
66752 rc = walLockShared(pWal, WAL_READ_LOCK(0));
66753 if( rc!=SQLITE_OK ){
66754 if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
66755 goto begin_unreliable_shm_out;
66756 }
66757 pWal->readLock = 0;
66758
66759 /* Check to see if a separate writer has attached to the shared-memory area,
66760 ** thus making the shared-memory "reliable" again. Do this by invoking
66761 ** the xShmMap() routine of the VFS and looking to see if the return
66762 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
66763 **
66764 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
66765 ** cause the heap-memory WAL-index to be discarded and the actual
66766 ** shared memory to be used in its place.
66767 **
66768 ** This step is important because, even though this connection is holding
66769 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
66770 ** have already checkpointed the WAL file and, while the current
66771 ** is active, wrap the WAL and start overwriting frames that this
66772 ** process wants to use.
66773 **
66774 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
66775 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
66776 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
66777 ** even if some external agent does a "chmod" to make the shared-memory
66778 ** writable by us, until sqlite3OsShmUnmap() has been called.
66779 ** This is a requirement on the VFS implementation.
66780 */
66781 rc = sqlite3OsShmMap(id: pWal->pDbFd, iPage: 0, WALINDEX_PGSZ, bExtend: 0, pp: &pDummy);
66782 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
66783 if( rc!=SQLITE_READONLY_CANTINIT ){
66784 rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
66785 goto begin_unreliable_shm_out;
66786 }
66787
66788 /* We reach this point only if the real shared-memory is still unreliable.
66789 ** Assume the in-memory WAL-index substitute is correct and load it
66790 ** into pWal->hdr.
66791 */
66792 memcpy(dest: &pWal->hdr, src: (void*)walIndexHdr(pWal), n: sizeof(WalIndexHdr));
66793
66794 /* Make sure some writer hasn't come in and changed the WAL file out
66795 ** from under us, then disconnected, while we were not looking.
66796 */
66797 rc = sqlite3OsFileSize(id: pWal->pWalFd, pSize: &szWal);
66798 if( rc!=SQLITE_OK ){
66799 goto begin_unreliable_shm_out;
66800 }
66801 if( szWal<WAL_HDRSIZE ){
66802 /* If the wal file is too small to contain a wal-header and the
66803 ** wal-index header has mxFrame==0, then it must be safe to proceed
66804 ** reading the database file only. However, the page cache cannot
66805 ** be trusted, as a read/write connection may have connected, written
66806 ** the db, run a checkpoint, truncated the wal file and disconnected
66807 ** since this client's last read transaction. */
66808 *pChanged = 1;
66809 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
66810 goto begin_unreliable_shm_out;
66811 }
66812
66813 /* Check the salt keys at the start of the wal file still match. */
66814 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, WAL_HDRSIZE, offset: 0);
66815 if( rc!=SQLITE_OK ){
66816 goto begin_unreliable_shm_out;
66817 }
66818 if( memcmp(s1: &pWal->hdr.aSalt, s2: &aBuf[16], n: 8) ){
66819 /* Some writer has wrapped the WAL file while we were not looking.
66820 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
66821 ** rebuilt. */
66822 rc = WAL_RETRY;
66823 goto begin_unreliable_shm_out;
66824 }
66825
66826 /* Allocate a buffer to read frames into */
66827 assert( (pWal->szPage & (pWal->szPage-1))==0 );
66828 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
66829 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
66830 aFrame = (u8 *)sqlite3_malloc64(n: szFrame);
66831 if( aFrame==0 ){
66832 rc = SQLITE_NOMEM_BKPT;
66833 goto begin_unreliable_shm_out;
66834 }
66835 aData = &aFrame[WAL_FRAME_HDRSIZE];
66836
66837 /* Check to see if a complete transaction has been appended to the
66838 ** wal file since the heap-memory wal-index was created. If so, the
66839 ** heap-memory wal-index is discarded and WAL_RETRY returned to
66840 ** the caller. */
66841 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
66842 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
66843 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
66844 iOffset+szFrame<=szWal;
66845 iOffset+=szFrame
66846 ){
66847 u32 pgno; /* Database page number for frame */
66848 u32 nTruncate; /* dbsize field from frame header */
66849
66850 /* Read and decode the next log frame. */
66851 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aFrame, amt: szFrame, offset: iOffset);
66852 if( rc!=SQLITE_OK ) break;
66853 if( !walDecodeFrame(pWal, piPage: &pgno, pnTruncate: &nTruncate, aData, aFrame) ) break;
66854
66855 /* If nTruncate is non-zero, then a complete transaction has been
66856 ** appended to this wal file. Set rc to WAL_RETRY and break out of
66857 ** the loop. */
66858 if( nTruncate ){
66859 rc = WAL_RETRY;
66860 break;
66861 }
66862 }
66863 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
66864 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
66865
66866 begin_unreliable_shm_out:
66867 sqlite3_free(p: aFrame);
66868 if( rc!=SQLITE_OK ){
66869 int i;
66870 for(i=0; i<pWal->nWiData; i++){
66871 sqlite3_free(p: (void*)pWal->apWiData[i]);
66872 pWal->apWiData[i] = 0;
66873 }
66874 pWal->bShmUnreliable = 0;
66875 sqlite3WalEndReadTransaction(pWal);
66876 *pChanged = 1;
66877 }
66878 return rc;
66879}
66880
66881/*
66882** Attempt to start a read transaction. This might fail due to a race or
66883** other transient condition. When that happens, it returns WAL_RETRY to
66884** indicate to the caller that it is safe to retry immediately.
66885**
66886** On success return SQLITE_OK. On a permanent failure (such an
66887** I/O error or an SQLITE_BUSY because another process is running
66888** recovery) return a positive error code.
66889**
66890** The useWal parameter is true to force the use of the WAL and disable
66891** the case where the WAL is bypassed because it has been completely
66892** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
66893** to make a copy of the wal-index header into pWal->hdr. If the
66894** wal-index header has changed, *pChanged is set to 1 (as an indication
66895** to the caller that the local page cache is obsolete and needs to be
66896** flushed.) When useWal==1, the wal-index header is assumed to already
66897** be loaded and the pChanged parameter is unused.
66898**
66899** The caller must set the cnt parameter to the number of prior calls to
66900** this routine during the current read attempt that returned WAL_RETRY.
66901** This routine will start taking more aggressive measures to clear the
66902** race conditions after multiple WAL_RETRY returns, and after an excessive
66903** number of errors will ultimately return SQLITE_PROTOCOL. The
66904** SQLITE_PROTOCOL return indicates that some other process has gone rogue
66905** and is not honoring the locking protocol. There is a vanishingly small
66906** chance that SQLITE_PROTOCOL could be returned because of a run of really
66907** bad luck when there is lots of contention for the wal-index, but that
66908** possibility is so small that it can be safely neglected, we believe.
66909**
66910** On success, this routine obtains a read lock on
66911** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
66912** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
66913** that means the Wal does not hold any read lock. The reader must not
66914** access any database page that is modified by a WAL frame up to and
66915** including frame number aReadMark[pWal->readLock]. The reader will
66916** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
66917** Or if pWal->readLock==0, then the reader will ignore the WAL
66918** completely and get all content directly from the database file.
66919** If the useWal parameter is 1 then the WAL will never be ignored and
66920** this routine will always set pWal->readLock>0 on success.
66921** When the read transaction is completed, the caller must release the
66922** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
66923**
66924** This routine uses the nBackfill and aReadMark[] fields of the header
66925** to select a particular WAL_READ_LOCK() that strives to let the
66926** checkpoint process do as much work as possible. This routine might
66927** update values of the aReadMark[] array in the header, but if it does
66928** so it takes care to hold an exclusive lock on the corresponding
66929** WAL_READ_LOCK() while changing values.
66930*/
66931static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
66932 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
66933 u32 mxReadMark; /* Largest aReadMark[] value */
66934 int mxI; /* Index of largest aReadMark[] value */
66935 int i; /* Loop counter */
66936 int rc = SQLITE_OK; /* Return code */
66937 u32 mxFrame; /* Wal frame to lock to */
66938
66939 assert( pWal->readLock<0 ); /* Not currently locked */
66940
66941 /* useWal may only be set for read/write connections */
66942 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
66943
66944 /* Take steps to avoid spinning forever if there is a protocol error.
66945 **
66946 ** Circumstances that cause a RETRY should only last for the briefest
66947 ** instances of time. No I/O or other system calls are done while the
66948 ** locks are held, so the locks should not be held for very long. But
66949 ** if we are unlucky, another process that is holding a lock might get
66950 ** paged out or take a page-fault that is time-consuming to resolve,
66951 ** during the few nanoseconds that it is holding the lock. In that case,
66952 ** it might take longer than normal for the lock to free.
66953 **
66954 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
66955 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
66956 ** is more of a scheduler yield than an actual delay. But on the 10th
66957 ** an subsequent retries, the delays start becoming longer and longer,
66958 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
66959 ** The total delay time before giving up is less than 10 seconds.
66960 */
66961 if( cnt>5 ){
66962 int nDelay = 1; /* Pause time in microseconds */
66963 if( cnt>100 ){
66964 VVA_ONLY( pWal->lockError = 1; )
66965 return SQLITE_PROTOCOL;
66966 }
66967 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
66968 sqlite3OsSleep(pVfs: pWal->pVfs, nMicro: nDelay);
66969 }
66970
66971 if( !useWal ){
66972 assert( rc==SQLITE_OK );
66973 if( pWal->bShmUnreliable==0 ){
66974 rc = walIndexReadHdr(pWal, pChanged);
66975 }
66976 if( rc==SQLITE_BUSY ){
66977 /* If there is not a recovery running in another thread or process
66978 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
66979 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
66980 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
66981 ** would be technically correct. But the race is benign since with
66982 ** WAL_RETRY this routine will be called again and will probably be
66983 ** right on the second iteration.
66984 */
66985 if( pWal->apWiData[0]==0 ){
66986 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
66987 ** We assume this is a transient condition, so return WAL_RETRY. The
66988 ** xShmMap() implementation used by the default unix and win32 VFS
66989 ** modules may return SQLITE_BUSY due to a race condition in the
66990 ** code that determines whether or not the shared-memory region
66991 ** must be zeroed before the requested page is returned.
66992 */
66993 rc = WAL_RETRY;
66994 }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
66995 walUnlockShared(pWal, WAL_RECOVER_LOCK);
66996 rc = WAL_RETRY;
66997 }else if( rc==SQLITE_BUSY ){
66998 rc = SQLITE_BUSY_RECOVERY;
66999 }
67000 }
67001 if( rc!=SQLITE_OK ){
67002 return rc;
67003 }
67004 else if( pWal->bShmUnreliable ){
67005 return walBeginShmUnreliable(pWal, pChanged);
67006 }
67007 }
67008
67009 assert( pWal->nWiData>0 );
67010 assert( pWal->apWiData[0]!=0 );
67011 pInfo = walCkptInfo(pWal);
67012 SEH_INJECT_FAULT;
67013 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
67014#ifdef SQLITE_ENABLE_SNAPSHOT
67015 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
67016#endif
67017 ){
67018 /* The WAL has been completely backfilled (or it is empty).
67019 ** and can be safely ignored.
67020 */
67021 rc = walLockShared(pWal, WAL_READ_LOCK(0));
67022 walShmBarrier(pWal);
67023 if( rc==SQLITE_OK ){
67024 if( memcmp(s1: (void *)walIndexHdr(pWal), s2: &pWal->hdr, n: sizeof(WalIndexHdr)) ){
67025 /* It is not safe to allow the reader to continue here if frames
67026 ** may have been appended to the log before READ_LOCK(0) was obtained.
67027 ** When holding READ_LOCK(0), the reader ignores the entire log file,
67028 ** which implies that the database file contains a trustworthy
67029 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
67030 ** happening, this is usually correct.
67031 **
67032 ** However, if frames have been appended to the log (or if the log
67033 ** is wrapped and written for that matter) before the READ_LOCK(0)
67034 ** is obtained, that is not necessarily true. A checkpointer may
67035 ** have started to backfill the appended frames but crashed before
67036 ** it finished. Leaving a corrupt image in the database file.
67037 */
67038 walUnlockShared(pWal, WAL_READ_LOCK(0));
67039 return WAL_RETRY;
67040 }
67041 pWal->readLock = 0;
67042 return SQLITE_OK;
67043 }else if( rc!=SQLITE_BUSY ){
67044 return rc;
67045 }
67046 }
67047
67048 /* If we get this far, it means that the reader will want to use
67049 ** the WAL to get at content from recent commits. The job now is
67050 ** to select one of the aReadMark[] entries that is closest to
67051 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
67052 */
67053 mxReadMark = 0;
67054 mxI = 0;
67055 mxFrame = pWal->hdr.mxFrame;
67056#ifdef SQLITE_ENABLE_SNAPSHOT
67057 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
67058 mxFrame = pWal->pSnapshot->mxFrame;
67059 }
67060#endif
67061 for(i=1; i<WAL_NREADER; i++){
67062 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
67063 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
67064 assert( thisMark!=READMARK_NOT_USED );
67065 mxReadMark = thisMark;
67066 mxI = i;
67067 }
67068 }
67069 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
67070 && (mxReadMark<mxFrame || mxI==0)
67071 ){
67072 for(i=1; i<WAL_NREADER; i++){
67073 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
67074 if( rc==SQLITE_OK ){
67075 AtomicStore(pInfo->aReadMark+i,mxFrame);
67076 mxReadMark = mxFrame;
67077 mxI = i;
67078 walUnlockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
67079 break;
67080 }else if( rc!=SQLITE_BUSY ){
67081 return rc;
67082 }
67083 }
67084 }
67085 if( mxI==0 ){
67086 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
67087 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
67088 }
67089
67090 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
67091 if( rc ){
67092 return rc==SQLITE_BUSY ? WAL_RETRY : rc;
67093 }
67094 /* Now that the read-lock has been obtained, check that neither the
67095 ** value in the aReadMark[] array or the contents of the wal-index
67096 ** header have changed.
67097 **
67098 ** It is necessary to check that the wal-index header did not change
67099 ** between the time it was read and when the shared-lock was obtained
67100 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
67101 ** that the log file may have been wrapped by a writer, or that frames
67102 ** that occur later in the log than pWal->hdr.mxFrame may have been
67103 ** copied into the database by a checkpointer. If either of these things
67104 ** happened, then reading the database with the current value of
67105 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
67106 ** instead.
67107 **
67108 ** Before checking that the live wal-index header has not changed
67109 ** since it was read, set Wal.minFrame to the first frame in the wal
67110 ** file that has not yet been checkpointed. This client will not need
67111 ** to read any frames earlier than minFrame from the wal file - they
67112 ** can be safely read directly from the database file.
67113 **
67114 ** Because a ShmBarrier() call is made between taking the copy of
67115 ** nBackfill and checking that the wal-header in shared-memory still
67116 ** matches the one cached in pWal->hdr, it is guaranteed that the
67117 ** checkpointer that set nBackfill was not working with a wal-index
67118 ** header newer than that cached in pWal->hdr. If it were, that could
67119 ** cause a problem. The checkpointer could omit to checkpoint
67120 ** a version of page X that lies before pWal->minFrame (call that version
67121 ** A) on the basis that there is a newer version (version B) of the same
67122 ** page later in the wal file. But if version B happens to like past
67123 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
67124 ** that it can read version A from the database file. However, since
67125 ** we can guarantee that the checkpointer that set nBackfill could not
67126 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
67127 */
67128 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
67129 walShmBarrier(pWal);
67130 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
67131 || memcmp(s1: (void *)walIndexHdr(pWal), s2: &pWal->hdr, n: sizeof(WalIndexHdr))
67132 ){
67133 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
67134 return WAL_RETRY;
67135 }else{
67136 assert( mxReadMark<=pWal->hdr.mxFrame );
67137 pWal->readLock = (i16)mxI;
67138 }
67139 return rc;
67140}
67141
67142#ifdef SQLITE_ENABLE_SNAPSHOT
67143/*
67144** This function does the work of sqlite3WalSnapshotRecover().
67145*/
67146static int walSnapshotRecover(
67147 Wal *pWal, /* WAL handle */
67148 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
67149 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
67150){
67151 int szPage = (int)pWal->szPage;
67152 int rc;
67153 i64 szDb; /* Size of db file in bytes */
67154
67155 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
67156 if( rc==SQLITE_OK ){
67157 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
67158 u32 i = pInfo->nBackfillAttempted;
67159 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
67160 WalHashLoc sLoc; /* Hash table location */
67161 u32 pgno; /* Page number in db file */
67162 i64 iDbOff; /* Offset of db file entry */
67163 i64 iWalOff; /* Offset of wal file entry */
67164
67165 rc = walHashGet(pWal, walFramePage(i), &sLoc);
67166 if( rc!=SQLITE_OK ) break;
67167 assert( i - sLoc.iZero - 1 >=0 );
67168 pgno = sLoc.aPgno[i-sLoc.iZero-1];
67169 iDbOff = (i64)(pgno-1) * szPage;
67170
67171 if( iDbOff+szPage<=szDb ){
67172 iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
67173 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
67174
67175 if( rc==SQLITE_OK ){
67176 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
67177 }
67178
67179 if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
67180 break;
67181 }
67182 }
67183
67184 pInfo->nBackfillAttempted = i-1;
67185 }
67186 }
67187
67188 return rc;
67189}
67190
67191/*
67192** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
67193** variable so that older snapshots can be accessed. To do this, loop
67194** through all wal frames from nBackfillAttempted to (nBackfill+1),
67195** comparing their content to the corresponding page with the database
67196** file, if any. Set nBackfillAttempted to the frame number of the
67197** first frame for which the wal file content matches the db file.
67198**
67199** This is only really safe if the file-system is such that any page
67200** writes made by earlier checkpointers were atomic operations, which
67201** is not always true. It is also possible that nBackfillAttempted
67202** may be left set to a value larger than expected, if a wal frame
67203** contains content that duplicate of an earlier version of the same
67204** page.
67205**
67206** SQLITE_OK is returned if successful, or an SQLite error code if an
67207** error occurs. It is not an error if nBackfillAttempted cannot be
67208** decreased at all.
67209*/
67210SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
67211 int rc;
67212
67213 assert( pWal->readLock>=0 );
67214 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
67215 if( rc==SQLITE_OK ){
67216 void *pBuf1 = sqlite3_malloc(pWal->szPage);
67217 void *pBuf2 = sqlite3_malloc(pWal->szPage);
67218 if( pBuf1==0 || pBuf2==0 ){
67219 rc = SQLITE_NOMEM;
67220 }else{
67221 pWal->ckptLock = 1;
67222 SEH_TRY {
67223 rc = walSnapshotRecover(pWal, pBuf1, pBuf2);
67224 }
67225 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
67226 pWal->ckptLock = 0;
67227 }
67228
67229 sqlite3_free(pBuf1);
67230 sqlite3_free(pBuf2);
67231 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
67232 }
67233
67234 return rc;
67235}
67236#endif /* SQLITE_ENABLE_SNAPSHOT */
67237
67238/*
67239** This function does the work of sqlite3WalBeginReadTransaction() (see
67240** below). That function simply calls this one inside an SEH_TRY{...} block.
67241*/
67242static int walBeginReadTransaction(Wal *pWal, int *pChanged){
67243 int rc; /* Return code */
67244 int cnt = 0; /* Number of TryBeginRead attempts */
67245#ifdef SQLITE_ENABLE_SNAPSHOT
67246 int ckptLock = 0;
67247 int bChanged = 0;
67248 WalIndexHdr *pSnapshot = pWal->pSnapshot;
67249#endif
67250
67251 assert( pWal->ckptLock==0 );
67252 assert( pWal->nSehTry>0 );
67253
67254#ifdef SQLITE_ENABLE_SNAPSHOT
67255 if( pSnapshot ){
67256 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
67257 bChanged = 1;
67258 }
67259
67260 /* It is possible that there is a checkpointer thread running
67261 ** concurrent with this code. If this is the case, it may be that the
67262 ** checkpointer has already determined that it will checkpoint
67263 ** snapshot X, where X is later in the wal file than pSnapshot, but
67264 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
67265 ** its intent. To avoid the race condition this leads to, ensure that
67266 ** there is no checkpointer process by taking a shared CKPT lock
67267 ** before checking pInfo->nBackfillAttempted. */
67268 (void)walEnableBlocking(pWal);
67269 rc = walLockShared(pWal, WAL_CKPT_LOCK);
67270 walDisableBlocking(pWal);
67271
67272 if( rc!=SQLITE_OK ){
67273 return rc;
67274 }
67275 ckptLock = 1;
67276 }
67277#endif
67278
67279 do{
67280 rc = walTryBeginRead(pWal, pChanged, useWal: 0, cnt: ++cnt);
67281 }while( rc==WAL_RETRY );
67282 testcase( (rc&0xff)==SQLITE_BUSY );
67283 testcase( (rc&0xff)==SQLITE_IOERR );
67284 testcase( rc==SQLITE_PROTOCOL );
67285 testcase( rc==SQLITE_OK );
67286
67287#ifdef SQLITE_ENABLE_SNAPSHOT
67288 if( rc==SQLITE_OK ){
67289 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
67290 /* At this point the client has a lock on an aReadMark[] slot holding
67291 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
67292 ** is populated with the wal-index header corresponding to the head
67293 ** of the wal file. Verify that pSnapshot is still valid before
67294 ** continuing. Reasons why pSnapshot might no longer be valid:
67295 **
67296 ** (1) The WAL file has been reset since the snapshot was taken.
67297 ** In this case, the salt will have changed.
67298 **
67299 ** (2) A checkpoint as been attempted that wrote frames past
67300 ** pSnapshot->mxFrame into the database file. Note that the
67301 ** checkpoint need not have completed for this to cause problems.
67302 */
67303 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
67304
67305 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
67306 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
67307
67308 /* Check that the wal file has not been wrapped. Assuming that it has
67309 ** not, also check that no checkpointer has attempted to checkpoint any
67310 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
67311 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
67312 ** with *pSnapshot and set *pChanged as appropriate for opening the
67313 ** snapshot. */
67314 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
67315 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
67316 ){
67317 assert( pWal->readLock>0 );
67318 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
67319 *pChanged = bChanged;
67320 }else{
67321 rc = SQLITE_ERROR_SNAPSHOT;
67322 }
67323
67324 /* A client using a non-current snapshot may not ignore any frames
67325 ** from the start of the wal file. This is because, for a system
67326 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
67327 ** have omitted to checkpoint a frame earlier than minFrame in
67328 ** the file because there exists a frame after iSnapshot that
67329 ** is the same database page. */
67330 pWal->minFrame = 1;
67331
67332 if( rc!=SQLITE_OK ){
67333 sqlite3WalEndReadTransaction(pWal);
67334 }
67335 }
67336 }
67337
67338 /* Release the shared CKPT lock obtained above. */
67339 if( ckptLock ){
67340 assert( pSnapshot );
67341 walUnlockShared(pWal, WAL_CKPT_LOCK);
67342 }
67343#endif
67344 return rc;
67345}
67346
67347/*
67348** Begin a read transaction on the database.
67349**
67350** This routine used to be called sqlite3OpenSnapshot() and with good reason:
67351** it takes a snapshot of the state of the WAL and wal-index for the current
67352** instant in time. The current thread will continue to use this snapshot.
67353** Other threads might append new content to the WAL and wal-index but
67354** that extra content is ignored by the current thread.
67355**
67356** If the database contents have changes since the previous read
67357** transaction, then *pChanged is set to 1 before returning. The
67358** Pager layer will use this to know that its cache is stale and
67359** needs to be flushed.
67360*/
67361SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
67362 int rc;
67363 SEH_TRY {
67364 rc = walBeginReadTransaction(pWal, pChanged);
67365 }
67366 SEH_EXCEPT( rc = walHandleException(pWal); )
67367 return rc;
67368}
67369
67370/*
67371** Finish with a read transaction. All this does is release the
67372** read-lock.
67373*/
67374SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
67375 sqlite3WalEndWriteTransaction(pWal);
67376 if( pWal->readLock>=0 ){
67377 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
67378 pWal->readLock = -1;
67379 }
67380}
67381
67382/*
67383** Search the wal file for page pgno. If found, set *piRead to the frame that
67384** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
67385** to zero.
67386**
67387** Return SQLITE_OK if successful, or an error code if an error occurs. If an
67388** error does occur, the final value of *piRead is undefined.
67389*/
67390static int walFindFrame(
67391 Wal *pWal, /* WAL handle */
67392 Pgno pgno, /* Database page number to read data for */
67393 u32 *piRead /* OUT: Frame number (or zero) */
67394){
67395 u32 iRead = 0; /* If !=0, WAL frame to return data from */
67396 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
67397 int iHash; /* Used to loop through N hash tables */
67398 int iMinHash;
67399
67400 /* This routine is only be called from within a read transaction. */
67401 assert( pWal->readLock>=0 || pWal->lockError );
67402
67403 /* If the "last page" field of the wal-index header snapshot is 0, then
67404 ** no data will be read from the wal under any circumstances. Return early
67405 ** in this case as an optimization. Likewise, if pWal->readLock==0,
67406 ** then the WAL is ignored by the reader so return early, as if the
67407 ** WAL were empty.
67408 */
67409 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
67410 *piRead = 0;
67411 return SQLITE_OK;
67412 }
67413
67414 /* Search the hash table or tables for an entry matching page number
67415 ** pgno. Each iteration of the following for() loop searches one
67416 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
67417 **
67418 ** This code might run concurrently to the code in walIndexAppend()
67419 ** that adds entries to the wal-index (and possibly to this hash
67420 ** table). This means the value just read from the hash
67421 ** slot (aHash[iKey]) may have been added before or after the
67422 ** current read transaction was opened. Values added after the
67423 ** read transaction was opened may have been written incorrectly -
67424 ** i.e. these slots may contain garbage data. However, we assume
67425 ** that any slots written before the current read transaction was
67426 ** opened remain unmodified.
67427 **
67428 ** For the reasons above, the if(...) condition featured in the inner
67429 ** loop of the following block is more stringent that would be required
67430 ** if we had exclusive access to the hash-table:
67431 **
67432 ** (aPgno[iFrame]==pgno):
67433 ** This condition filters out normal hash-table collisions.
67434 **
67435 ** (iFrame<=iLast):
67436 ** This condition filters out entries that were added to the hash
67437 ** table after the current read-transaction had started.
67438 */
67439 iMinHash = walFramePage(iFrame: pWal->minFrame);
67440 for(iHash=walFramePage(iFrame: iLast); iHash>=iMinHash; iHash--){
67441 WalHashLoc sLoc; /* Hash table location */
67442 int iKey; /* Hash slot index */
67443 int nCollide; /* Number of hash collisions remaining */
67444 int rc; /* Error code */
67445 u32 iH;
67446
67447 rc = walHashGet(pWal, iHash, pLoc: &sLoc);
67448 if( rc!=SQLITE_OK ){
67449 return rc;
67450 }
67451 nCollide = HASHTABLE_NSLOT;
67452 iKey = walHash(iPage: pgno);
67453 SEH_INJECT_FAULT;
67454 while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){
67455 u32 iFrame = iH + sLoc.iZero;
67456 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
67457 assert( iFrame>iRead || CORRUPT_DB );
67458 iRead = iFrame;
67459 }
67460 if( (nCollide--)==0 ){
67461 return SQLITE_CORRUPT_BKPT;
67462 }
67463 iKey = walNextHash(iPriorHash: iKey);
67464 }
67465 if( iRead ) break;
67466 }
67467
67468#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
67469 /* If expensive assert() statements are available, do a linear search
67470 ** of the wal-index file content. Make sure the results agree with the
67471 ** result obtained using the hash indexes above. */
67472 {
67473 u32 iRead2 = 0;
67474 u32 iTest;
67475 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
67476 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
67477 if( walFramePgno(pWal, iTest)==pgno ){
67478 iRead2 = iTest;
67479 break;
67480 }
67481 }
67482 assert( iRead==iRead2 );
67483 }
67484#endif
67485
67486 *piRead = iRead;
67487 return SQLITE_OK;
67488}
67489
67490/*
67491** Search the wal file for page pgno. If found, set *piRead to the frame that
67492** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
67493** to zero.
67494**
67495** Return SQLITE_OK if successful, or an error code if an error occurs. If an
67496** error does occur, the final value of *piRead is undefined.
67497**
67498** The difference between this function and walFindFrame() is that this
67499** function wraps walFindFrame() in an SEH_TRY{...} block.
67500*/
67501SQLITE_PRIVATE int sqlite3WalFindFrame(
67502 Wal *pWal, /* WAL handle */
67503 Pgno pgno, /* Database page number to read data for */
67504 u32 *piRead /* OUT: Frame number (or zero) */
67505){
67506 int rc;
67507 SEH_TRY {
67508 rc = walFindFrame(pWal, pgno, piRead);
67509 }
67510 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
67511 return rc;
67512}
67513
67514/*
67515** Read the contents of frame iRead from the wal file into buffer pOut
67516** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
67517** error code otherwise.
67518*/
67519SQLITE_PRIVATE int sqlite3WalReadFrame(
67520 Wal *pWal, /* WAL handle */
67521 u32 iRead, /* Frame to read */
67522 int nOut, /* Size of buffer pOut in bytes */
67523 u8 *pOut /* Buffer to write page data to */
67524){
67525 int sz;
67526 i64 iOffset;
67527 sz = pWal->hdr.szPage;
67528 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
67529 testcase( sz<=32768 );
67530 testcase( sz>=65536 );
67531 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
67532 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
67533 return sqlite3OsRead(id: pWal->pWalFd, pBuf: pOut, amt: (nOut>sz ? sz : nOut), offset: iOffset);
67534}
67535
67536/*
67537** Return the size of the database in pages (or zero, if unknown).
67538*/
67539SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
67540 if( pWal && ALWAYS(pWal->readLock>=0) ){
67541 return pWal->hdr.nPage;
67542 }
67543 return 0;
67544}
67545
67546
67547/*
67548** This function starts a write transaction on the WAL.
67549**
67550** A read transaction must have already been started by a prior call
67551** to sqlite3WalBeginReadTransaction().
67552**
67553** If another thread or process has written into the database since
67554** the read transaction was started, then it is not possible for this
67555** thread to write as doing so would cause a fork. So this routine
67556** returns SQLITE_BUSY in that case and no write transaction is started.
67557**
67558** There can only be a single writer active at a time.
67559*/
67560SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
67561 int rc;
67562
67563#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67564 /* If the write-lock is already held, then it was obtained before the
67565 ** read-transaction was even opened, making this call a no-op.
67566 ** Return early. */
67567 if( pWal->writeLock ){
67568 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
67569 return SQLITE_OK;
67570 }
67571#endif
67572
67573 /* Cannot start a write transaction without first holding a read
67574 ** transaction. */
67575 assert( pWal->readLock>=0 );
67576 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
67577
67578 if( pWal->readOnly ){
67579 return SQLITE_READONLY;
67580 }
67581
67582 /* Only one writer allowed at a time. Get the write lock. Return
67583 ** SQLITE_BUSY if unable.
67584 */
67585 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
67586 if( rc ){
67587 return rc;
67588 }
67589 pWal->writeLock = 1;
67590
67591 /* If another connection has written to the database file since the
67592 ** time the read transaction on this connection was started, then
67593 ** the write is disallowed.
67594 */
67595 SEH_TRY {
67596 if( memcmp(s1: &pWal->hdr, s2: (void *)walIndexHdr(pWal), n: sizeof(WalIndexHdr))!=0 ){
67597 rc = SQLITE_BUSY_SNAPSHOT;
67598 }
67599 }
67600 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
67601
67602 if( rc!=SQLITE_OK ){
67603 walUnlockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
67604 pWal->writeLock = 0;
67605 }
67606 return rc;
67607}
67608
67609/*
67610** End a write transaction. The commit has already been done. This
67611** routine merely releases the lock.
67612*/
67613SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
67614 if( pWal->writeLock ){
67615 walUnlockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
67616 pWal->writeLock = 0;
67617 pWal->iReCksum = 0;
67618 pWal->truncateOnCommit = 0;
67619 }
67620 return SQLITE_OK;
67621}
67622
67623/*
67624** If any data has been written (but not committed) to the log file, this
67625** function moves the write-pointer back to the start of the transaction.
67626**
67627** Additionally, the callback function is invoked for each frame written
67628** to the WAL since the start of the transaction. If the callback returns
67629** other than SQLITE_OK, it is not invoked again and the error code is
67630** returned to the caller.
67631**
67632** Otherwise, if the callback function does not return an error, this
67633** function returns SQLITE_OK.
67634*/
67635SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
67636 int rc = SQLITE_OK;
67637 if( ALWAYS(pWal->writeLock) ){
67638 Pgno iMax = pWal->hdr.mxFrame;
67639 Pgno iFrame;
67640
67641 SEH_TRY {
67642 /* Restore the clients cache of the wal-index header to the state it
67643 ** was in before the client began writing to the database.
67644 */
67645 memcpy(dest: &pWal->hdr, src: (void *)walIndexHdr(pWal), n: sizeof(WalIndexHdr));
67646
67647 for(iFrame=pWal->hdr.mxFrame+1;
67648 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
67649 iFrame++
67650 ){
67651 /* This call cannot fail. Unless the page for which the page number
67652 ** is passed as the second argument is (a) in the cache and
67653 ** (b) has an outstanding reference, then xUndo is either a no-op
67654 ** (if (a) is false) or simply expels the page from the cache (if (b)
67655 ** is false).
67656 **
67657 ** If the upper layer is doing a rollback, it is guaranteed that there
67658 ** are no outstanding references to any page other than page 1. And
67659 ** page 1 is never written to the log until the transaction is
67660 ** committed. As a result, the call to xUndo may not fail.
67661 */
67662 assert( walFramePgno(pWal, iFrame)!=1 );
67663 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
67664 }
67665 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
67666 }
67667 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
67668 }
67669 return rc;
67670}
67671
67672/*
67673** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
67674** values. This function populates the array with values required to
67675** "rollback" the write position of the WAL handle back to the current
67676** point in the event of a savepoint rollback (via WalSavepointUndo()).
67677*/
67678SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
67679 assert( pWal->writeLock );
67680 aWalData[0] = pWal->hdr.mxFrame;
67681 aWalData[1] = pWal->hdr.aFrameCksum[0];
67682 aWalData[2] = pWal->hdr.aFrameCksum[1];
67683 aWalData[3] = pWal->nCkpt;
67684}
67685
67686/*
67687** Move the write position of the WAL back to the point identified by
67688** the values in the aWalData[] array. aWalData must point to an array
67689** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
67690** by a call to WalSavepoint().
67691*/
67692SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
67693 int rc = SQLITE_OK;
67694
67695 assert( pWal->writeLock );
67696 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
67697
67698 if( aWalData[3]!=pWal->nCkpt ){
67699 /* This savepoint was opened immediately after the write-transaction
67700 ** was started. Right after that, the writer decided to wrap around
67701 ** to the start of the log. Update the savepoint values to match.
67702 */
67703 aWalData[0] = 0;
67704 aWalData[3] = pWal->nCkpt;
67705 }
67706
67707 if( aWalData[0]<pWal->hdr.mxFrame ){
67708 pWal->hdr.mxFrame = aWalData[0];
67709 pWal->hdr.aFrameCksum[0] = aWalData[1];
67710 pWal->hdr.aFrameCksum[1] = aWalData[2];
67711 SEH_TRY {
67712 walCleanupHash(pWal);
67713 }
67714 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; )
67715 }
67716
67717 return rc;
67718}
67719
67720/*
67721** This function is called just before writing a set of frames to the log
67722** file (see sqlite3WalFrames()). It checks to see if, instead of appending
67723** to the current log file, it is possible to overwrite the start of the
67724** existing log file with the new frames (i.e. "reset" the log). If so,
67725** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
67726** unchanged.
67727**
67728** SQLITE_OK is returned if no error is encountered (regardless of whether
67729** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
67730** if an error occurs.
67731*/
67732static int walRestartLog(Wal *pWal){
67733 int rc = SQLITE_OK;
67734 int cnt;
67735
67736 if( pWal->readLock==0 ){
67737 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
67738 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
67739 if( pInfo->nBackfill>0 ){
67740 u32 salt1;
67741 sqlite3_randomness(N: 4, pBuf: &salt1);
67742 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
67743 if( rc==SQLITE_OK ){
67744 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
67745 ** readers are currently using the WAL), then the transactions
67746 ** frames will overwrite the start of the existing log. Update the
67747 ** wal-index header to reflect this.
67748 **
67749 ** In theory it would be Ok to update the cache of the header only
67750 ** at this point. But updating the actual wal-index header is also
67751 ** safe and means there is no special case for sqlite3WalUndo()
67752 ** to handle if this transaction is rolled back. */
67753 walRestartHdr(pWal, salt1);
67754 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
67755 }else if( rc!=SQLITE_BUSY ){
67756 return rc;
67757 }
67758 }
67759 walUnlockShared(pWal, WAL_READ_LOCK(0));
67760 pWal->readLock = -1;
67761 cnt = 0;
67762 do{
67763 int notUsed;
67764 rc = walTryBeginRead(pWal, pChanged: &notUsed, useWal: 1, cnt: ++cnt);
67765 }while( rc==WAL_RETRY );
67766 assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
67767 testcase( (rc&0xff)==SQLITE_IOERR );
67768 testcase( rc==SQLITE_PROTOCOL );
67769 testcase( rc==SQLITE_OK );
67770 }
67771 return rc;
67772}
67773
67774/*
67775** Information about the current state of the WAL file and where
67776** the next fsync should occur - passed from sqlite3WalFrames() into
67777** walWriteToLog().
67778*/
67779typedef struct WalWriter {
67780 Wal *pWal; /* The complete WAL information */
67781 sqlite3_file *pFd; /* The WAL file to which we write */
67782 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
67783 int syncFlags; /* Flags for the fsync */
67784 int szPage; /* Size of one page */
67785} WalWriter;
67786
67787/*
67788** Write iAmt bytes of content into the WAL file beginning at iOffset.
67789** Do a sync when crossing the p->iSyncPoint boundary.
67790**
67791** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
67792** first write the part before iSyncPoint, then sync, then write the
67793** rest.
67794*/
67795static int walWriteToLog(
67796 WalWriter *p, /* WAL to write to */
67797 void *pContent, /* Content to be written */
67798 int iAmt, /* Number of bytes to write */
67799 sqlite3_int64 iOffset /* Start writing at this offset */
67800){
67801 int rc;
67802 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
67803 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
67804 rc = sqlite3OsWrite(id: p->pFd, pBuf: pContent, amt: iFirstAmt, offset: iOffset);
67805 if( rc ) return rc;
67806 iOffset += iFirstAmt;
67807 iAmt -= iFirstAmt;
67808 pContent = (void*)(iFirstAmt + (char*)pContent);
67809 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
67810 rc = sqlite3OsSync(id: p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
67811 if( iAmt==0 || rc ) return rc;
67812 }
67813 rc = sqlite3OsWrite(id: p->pFd, pBuf: pContent, amt: iAmt, offset: iOffset);
67814 return rc;
67815}
67816
67817/*
67818** Write out a single frame of the WAL
67819*/
67820static int walWriteOneFrame(
67821 WalWriter *p, /* Where to write the frame */
67822 PgHdr *pPage, /* The page of the frame to be written */
67823 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
67824 sqlite3_int64 iOffset /* Byte offset at which to write */
67825){
67826 int rc; /* Result code from subfunctions */
67827 void *pData; /* Data actually written */
67828 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
67829 pData = pPage->pData;
67830 walEncodeFrame(pWal: p->pWal, iPage: pPage->pgno, nTruncate, aData: pData, aFrame);
67831 rc = walWriteToLog(p, pContent: aFrame, iAmt: sizeof(aFrame), iOffset);
67832 if( rc ) return rc;
67833 /* Write the page data */
67834 rc = walWriteToLog(p, pContent: pData, iAmt: p->szPage, iOffset: iOffset+sizeof(aFrame));
67835 return rc;
67836}
67837
67838/*
67839** This function is called as part of committing a transaction within which
67840** one or more frames have been overwritten. It updates the checksums for
67841** all frames written to the wal file by the current transaction starting
67842** with the earliest to have been overwritten.
67843**
67844** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
67845*/
67846static int walRewriteChecksums(Wal *pWal, u32 iLast){
67847 const int szPage = pWal->szPage;/* Database page size */
67848 int rc = SQLITE_OK; /* Return code */
67849 u8 *aBuf; /* Buffer to load data from wal file into */
67850 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
67851 u32 iRead; /* Next frame to read from wal file */
67852 i64 iCksumOff;
67853
67854 aBuf = sqlite3_malloc(n: szPage + WAL_FRAME_HDRSIZE);
67855 if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
67856
67857 /* Find the checksum values to use as input for the recalculating the
67858 ** first checksum. If the first frame is frame 1 (implying that the current
67859 ** transaction restarted the wal file), these values must be read from the
67860 ** wal-file header. Otherwise, read them from the frame header of the
67861 ** previous frame. */
67862 assert( pWal->iReCksum>0 );
67863 if( pWal->iReCksum==1 ){
67864 iCksumOff = 24;
67865 }else{
67866 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
67867 }
67868 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, amt: sizeof(u32)*2, offset: iCksumOff);
67869 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(p: aBuf);
67870 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(p: &aBuf[sizeof(u32)]);
67871
67872 iRead = pWal->iReCksum;
67873 pWal->iReCksum = 0;
67874 for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
67875 i64 iOff = walFrameOffset(iRead, szPage);
67876 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, amt: szPage+WAL_FRAME_HDRSIZE, offset: iOff);
67877 if( rc==SQLITE_OK ){
67878 u32 iPgno, nDbSize;
67879 iPgno = sqlite3Get4byte(p: aBuf);
67880 nDbSize = sqlite3Get4byte(p: &aBuf[4]);
67881
67882 walEncodeFrame(pWal, iPage: iPgno, nTruncate: nDbSize, aData: &aBuf[WAL_FRAME_HDRSIZE], aFrame);
67883 rc = sqlite3OsWrite(id: pWal->pWalFd, pBuf: aFrame, amt: sizeof(aFrame), offset: iOff);
67884 }
67885 }
67886
67887 sqlite3_free(p: aBuf);
67888 return rc;
67889}
67890
67891/*
67892** Write a set of frames to the log. The caller must hold the write-lock
67893** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
67894*/
67895static int walFrames(
67896 Wal *pWal, /* Wal handle to write to */
67897 int szPage, /* Database page-size in bytes */
67898 PgHdr *pList, /* List of dirty pages to write */
67899 Pgno nTruncate, /* Database size after this commit */
67900 int isCommit, /* True if this is a commit */
67901 int sync_flags /* Flags to pass to OsSync() (or 0) */
67902){
67903 int rc; /* Used to catch return codes */
67904 u32 iFrame; /* Next frame address */
67905 PgHdr *p; /* Iterator to run through pList with. */
67906 PgHdr *pLast = 0; /* Last frame in list */
67907 int nExtra = 0; /* Number of extra copies of last page */
67908 int szFrame; /* The size of a single frame */
67909 i64 iOffset; /* Next byte to write in WAL file */
67910 WalWriter w; /* The writer */
67911 u32 iFirst = 0; /* First frame that may be overwritten */
67912 WalIndexHdr *pLive; /* Pointer to shared header */
67913
67914 assert( pList );
67915 assert( pWal->writeLock );
67916
67917 /* If this frame set completes a transaction, then nTruncate>0. If
67918 ** nTruncate==0 then this frame set does not complete the transaction. */
67919 assert( (isCommit!=0)==(nTruncate!=0) );
67920
67921#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
67922 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
67923 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
67924 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
67925 }
67926#endif
67927
67928 pLive = (WalIndexHdr*)walIndexHdr(pWal);
67929 if( memcmp(s1: &pWal->hdr, s2: (void *)pLive, n: sizeof(WalIndexHdr))!=0 ){
67930 iFirst = pLive->mxFrame+1;
67931 }
67932
67933 /* See if it is possible to write these frames into the start of the
67934 ** log file, instead of appending to it at pWal->hdr.mxFrame.
67935 */
67936 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
67937 return rc;
67938 }
67939
67940 /* If this is the first frame written into the log, write the WAL
67941 ** header to the start of the WAL file. See comments at the top of
67942 ** this source file for a description of the WAL header format.
67943 */
67944 iFrame = pWal->hdr.mxFrame;
67945 if( iFrame==0 ){
67946 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
67947 u32 aCksum[2]; /* Checksum for wal-header */
67948
67949 sqlite3Put4byte(p: &aWalHdr[0], v: (WAL_MAGIC | SQLITE_BIGENDIAN));
67950 sqlite3Put4byte(p: &aWalHdr[4], WAL_MAX_VERSION);
67951 sqlite3Put4byte(p: &aWalHdr[8], v: szPage);
67952 sqlite3Put4byte(p: &aWalHdr[12], v: pWal->nCkpt);
67953 if( pWal->nCkpt==0 ) sqlite3_randomness(N: 8, pBuf: pWal->hdr.aSalt);
67954 memcpy(dest: &aWalHdr[16], src: pWal->hdr.aSalt, n: 8);
67955 walChecksumBytes(nativeCksum: 1, a: aWalHdr, WAL_HDRSIZE-2*4, aIn: 0, aOut: aCksum);
67956 sqlite3Put4byte(p: &aWalHdr[24], v: aCksum[0]);
67957 sqlite3Put4byte(p: &aWalHdr[28], v: aCksum[1]);
67958
67959 pWal->szPage = szPage;
67960 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
67961 pWal->hdr.aFrameCksum[0] = aCksum[0];
67962 pWal->hdr.aFrameCksum[1] = aCksum[1];
67963 pWal->truncateOnCommit = 1;
67964
67965 rc = sqlite3OsWrite(id: pWal->pWalFd, pBuf: aWalHdr, amt: sizeof(aWalHdr), offset: 0);
67966 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
67967 if( rc!=SQLITE_OK ){
67968 return rc;
67969 }
67970
67971 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
67972 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
67973 ** an out-of-order write following a WAL restart could result in
67974 ** database corruption. See the ticket:
67975 **
67976 ** https://sqlite.org/src/info/ff5be73dee
67977 */
67978 if( pWal->syncHeader ){
67979 rc = sqlite3OsSync(id: pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
67980 if( rc ) return rc;
67981 }
67982 }
67983 if( (int)pWal->szPage!=szPage ){
67984 return SQLITE_CORRUPT_BKPT; /* TH3 test case: cov1/corrupt155.test */
67985 }
67986
67987 /* Setup information needed to write frames into the WAL */
67988 w.pWal = pWal;
67989 w.pFd = pWal->pWalFd;
67990 w.iSyncPoint = 0;
67991 w.syncFlags = sync_flags;
67992 w.szPage = szPage;
67993 iOffset = walFrameOffset(iFrame+1, szPage);
67994 szFrame = szPage + WAL_FRAME_HDRSIZE;
67995
67996 /* Write all frames into the log file exactly once */
67997 for(p=pList; p; p=p->pDirty){
67998 int nDbSize; /* 0 normally. Positive == commit flag */
67999
68000 /* Check if this page has already been written into the wal file by
68001 ** the current transaction. If so, overwrite the existing frame and
68002 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
68003 ** checksums must be recomputed when the transaction is committed. */
68004 if( iFirst && (p->pDirty || isCommit==0) ){
68005 u32 iWrite = 0;
68006 VVA_ONLY(rc =) walFindFrame(pWal, pgno: p->pgno, piRead: &iWrite);
68007 assert( rc==SQLITE_OK || iWrite==0 );
68008 if( iWrite>=iFirst ){
68009 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
68010 void *pData;
68011 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
68012 pWal->iReCksum = iWrite;
68013 }
68014 pData = p->pData;
68015 rc = sqlite3OsWrite(id: pWal->pWalFd, pBuf: pData, amt: szPage, offset: iOff);
68016 if( rc ) return rc;
68017 p->flags &= ~PGHDR_WAL_APPEND;
68018 continue;
68019 }
68020 }
68021
68022 iFrame++;
68023 assert( iOffset==walFrameOffset(iFrame, szPage) );
68024 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
68025 rc = walWriteOneFrame(p: &w, pPage: p, nTruncate: nDbSize, iOffset);
68026 if( rc ) return rc;
68027 pLast = p;
68028 iOffset += szFrame;
68029 p->flags |= PGHDR_WAL_APPEND;
68030 }
68031
68032 /* Recalculate checksums within the wal file if required. */
68033 if( isCommit && pWal->iReCksum ){
68034 rc = walRewriteChecksums(pWal, iLast: iFrame);
68035 if( rc ) return rc;
68036 }
68037
68038 /* If this is the end of a transaction, then we might need to pad
68039 ** the transaction and/or sync the WAL file.
68040 **
68041 ** Padding and syncing only occur if this set of frames complete a
68042 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
68043 ** or synchronous==OFF, then no padding or syncing are needed.
68044 **
68045 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
68046 ** needed and only the sync is done. If padding is needed, then the
68047 ** final frame is repeated (with its commit mark) until the next sector
68048 ** boundary is crossed. Only the part of the WAL prior to the last
68049 ** sector boundary is synced; the part of the last frame that extends
68050 ** past the sector boundary is written after the sync.
68051 */
68052 if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
68053 int bSync = 1;
68054 if( pWal->padToSectorBoundary ){
68055 int sectorSize = sqlite3SectorSize(pFile: pWal->pWalFd);
68056 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
68057 bSync = (w.iSyncPoint==iOffset);
68058 testcase( bSync );
68059 while( iOffset<w.iSyncPoint ){
68060 rc = walWriteOneFrame(p: &w, pPage: pLast, nTruncate, iOffset);
68061 if( rc ) return rc;
68062 iOffset += szFrame;
68063 nExtra++;
68064 assert( pLast!=0 );
68065 }
68066 }
68067 if( bSync ){
68068 assert( rc==SQLITE_OK );
68069 rc = sqlite3OsSync(id: w.pFd, WAL_SYNC_FLAGS(sync_flags));
68070 }
68071 }
68072
68073 /* If this frame set completes the first transaction in the WAL and
68074 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
68075 ** journal size limit, if possible.
68076 */
68077 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
68078 i64 sz = pWal->mxWalSize;
68079 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
68080 sz = walFrameOffset(iFrame+nExtra+1, szPage);
68081 }
68082 walLimitSize(pWal, nMax: sz);
68083 pWal->truncateOnCommit = 0;
68084 }
68085
68086 /* Append data to the wal-index. It is not necessary to lock the
68087 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
68088 ** guarantees that there are no other writers, and no data that may
68089 ** be in use by existing readers is being overwritten.
68090 */
68091 iFrame = pWal->hdr.mxFrame;
68092 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
68093 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
68094 iFrame++;
68095 rc = walIndexAppend(pWal, iFrame, iPage: p->pgno);
68096 }
68097 assert( pLast!=0 || nExtra==0 );
68098 while( rc==SQLITE_OK && nExtra>0 ){
68099 iFrame++;
68100 nExtra--;
68101 rc = walIndexAppend(pWal, iFrame, iPage: pLast->pgno);
68102 }
68103
68104 if( rc==SQLITE_OK ){
68105 /* Update the private copy of the header. */
68106 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
68107 testcase( szPage<=32768 );
68108 testcase( szPage>=65536 );
68109 pWal->hdr.mxFrame = iFrame;
68110 if( isCommit ){
68111 pWal->hdr.iChange++;
68112 pWal->hdr.nPage = nTruncate;
68113 }
68114 /* If this is a commit, update the wal-index header too. */
68115 if( isCommit ){
68116 walIndexWriteHdr(pWal);
68117 pWal->iCallback = iFrame;
68118 }
68119 }
68120
68121 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
68122 return rc;
68123}
68124
68125/*
68126** Write a set of frames to the log. The caller must hold the write-lock
68127** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
68128**
68129** The difference between this function and walFrames() is that this
68130** function wraps walFrames() in an SEH_TRY{...} block.
68131*/
68132SQLITE_PRIVATE int sqlite3WalFrames(
68133 Wal *pWal, /* Wal handle to write to */
68134 int szPage, /* Database page-size in bytes */
68135 PgHdr *pList, /* List of dirty pages to write */
68136 Pgno nTruncate, /* Database size after this commit */
68137 int isCommit, /* True if this is a commit */
68138 int sync_flags /* Flags to pass to OsSync() (or 0) */
68139){
68140 int rc;
68141 SEH_TRY {
68142 rc = walFrames(pWal, szPage, pList, nTruncate, isCommit, sync_flags);
68143 }
68144 SEH_EXCEPT( rc = walHandleException(pWal); )
68145 return rc;
68146}
68147
68148/*
68149** This routine is called to implement sqlite3_wal_checkpoint() and
68150** related interfaces.
68151**
68152** Obtain a CHECKPOINT lock and then backfill as much information as
68153** we can from WAL into the database.
68154**
68155** If parameter xBusy is not NULL, it is a pointer to a busy-handler
68156** callback. In this case this function runs a blocking checkpoint.
68157*/
68158SQLITE_PRIVATE int sqlite3WalCheckpoint(
68159 Wal *pWal, /* Wal connection */
68160 sqlite3 *db, /* Check this handle's interrupt flag */
68161 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
68162 int (*xBusy)(void*), /* Function to call when busy */
68163 void *pBusyArg, /* Context argument for xBusyHandler */
68164 int sync_flags, /* Flags to sync db file with (or 0) */
68165 int nBuf, /* Size of temporary buffer */
68166 u8 *zBuf, /* Temporary buffer to use */
68167 int *pnLog, /* OUT: Number of frames in WAL */
68168 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
68169){
68170 int rc; /* Return code */
68171 int isChanged = 0; /* True if a new wal-index header is loaded */
68172 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
68173 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
68174
68175 assert( pWal->ckptLock==0 );
68176 assert( pWal->writeLock==0 );
68177
68178 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
68179 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
68180 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
68181
68182 if( pWal->readOnly ) return SQLITE_READONLY;
68183 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
68184
68185 /* Enable blocking locks, if possible. If blocking locks are successfully
68186 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
68187 sqlite3WalDb(pWal, db);
68188 (void)walEnableBlocking(pWal);
68189
68190 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
68191 ** "checkpoint" lock on the database file.
68192 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
68193 ** checkpoint operation at the same time, the lock cannot be obtained and
68194 ** SQLITE_BUSY is returned.
68195 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
68196 ** it will not be invoked in this case.
68197 */
68198 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, n: 1);
68199 testcase( rc==SQLITE_BUSY );
68200 testcase( rc!=SQLITE_OK && xBusy2!=0 );
68201 if( rc==SQLITE_OK ){
68202 pWal->ckptLock = 1;
68203
68204 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
68205 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
68206 ** file.
68207 **
68208 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
68209 ** immediately, and a busy-handler is configured, it is invoked and the
68210 ** writer lock retried until either the busy-handler returns 0 or the
68211 ** lock is successfully obtained.
68212 */
68213 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
68214 rc = walBusyLock(pWal, xBusy: xBusy2, pBusyArg, WAL_WRITE_LOCK, n: 1);
68215 if( rc==SQLITE_OK ){
68216 pWal->writeLock = 1;
68217 }else if( rc==SQLITE_BUSY ){
68218 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
68219 xBusy2 = 0;
68220 rc = SQLITE_OK;
68221 }
68222 }
68223 }
68224
68225
68226 /* Read the wal-index header. */
68227 SEH_TRY {
68228 if( rc==SQLITE_OK ){
68229 walDisableBlocking(pWal);
68230 rc = walIndexReadHdr(pWal, pChanged: &isChanged);
68231 (void)walEnableBlocking(pWal);
68232 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
68233 sqlite3OsUnfetch(id: pWal->pDbFd, iOff: 0, p: 0);
68234 }
68235 }
68236
68237 /* Copy data from the log to the database file. */
68238 if( rc==SQLITE_OK ){
68239 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
68240 rc = SQLITE_CORRUPT_BKPT;
68241 }else{
68242 rc = walCheckpoint(pWal, db, eMode: eMode2, xBusy: xBusy2, pBusyArg, sync_flags,zBuf);
68243 }
68244
68245 /* If no error occurred, set the output variables. */
68246 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
68247 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
68248 SEH_INJECT_FAULT;
68249 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
68250 }
68251 }
68252 }
68253 SEH_EXCEPT( rc = walHandleException(pWal); )
68254
68255 if( isChanged ){
68256 /* If a new wal-index header was loaded before the checkpoint was
68257 ** performed, then the pager-cache associated with pWal is now
68258 ** out of date. So zero the cached wal-index header to ensure that
68259 ** next time the pager opens a snapshot on this database it knows that
68260 ** the cache needs to be reset.
68261 */
68262 memset(s: &pWal->hdr, c: 0, n: sizeof(WalIndexHdr));
68263 }
68264
68265 walDisableBlocking(pWal);
68266 sqlite3WalDb(pWal, 0);
68267
68268 /* Release the locks. */
68269 sqlite3WalEndWriteTransaction(pWal);
68270 if( pWal->ckptLock ){
68271 walUnlockExclusive(pWal, WAL_CKPT_LOCK, n: 1);
68272 pWal->ckptLock = 0;
68273 }
68274 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
68275#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68276 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
68277#endif
68278 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
68279}
68280
68281/* Return the value to pass to a sqlite3_wal_hook callback, the
68282** number of frames in the WAL at the point of the last commit since
68283** sqlite3WalCallback() was called. If no commits have occurred since
68284** the last call, then return 0.
68285*/
68286SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
68287 u32 ret = 0;
68288 if( pWal ){
68289 ret = pWal->iCallback;
68290 pWal->iCallback = 0;
68291 }
68292 return (int)ret;
68293}
68294
68295/*
68296** This function is called to change the WAL subsystem into or out
68297** of locking_mode=EXCLUSIVE.
68298**
68299** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
68300** into locking_mode=NORMAL. This means that we must acquire a lock
68301** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
68302** or if the acquisition of the lock fails, then return 0. If the
68303** transition out of exclusive-mode is successful, return 1. This
68304** operation must occur while the pager is still holding the exclusive
68305** lock on the main database file.
68306**
68307** If op is one, then change from locking_mode=NORMAL into
68308** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
68309** be released. Return 1 if the transition is made and 0 if the
68310** WAL is already in exclusive-locking mode - meaning that this
68311** routine is a no-op. The pager must already hold the exclusive lock
68312** on the main database file before invoking this operation.
68313**
68314** If op is negative, then do a dry-run of the op==1 case but do
68315** not actually change anything. The pager uses this to see if it
68316** should acquire the database exclusive lock prior to invoking
68317** the op==1 case.
68318*/
68319SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
68320 int rc;
68321 assert( pWal->writeLock==0 );
68322 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
68323
68324 /* pWal->readLock is usually set, but might be -1 if there was a
68325 ** prior error while attempting to acquire are read-lock. This cannot
68326 ** happen if the connection is actually in exclusive mode (as no xShmLock
68327 ** locks are taken in this case). Nor should the pager attempt to
68328 ** upgrade to exclusive-mode following such an error.
68329 */
68330#ifndef SQLITE_USE_SEH
68331 assert( pWal->readLock>=0 || pWal->lockError );
68332#endif
68333 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
68334
68335 if( op==0 ){
68336 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
68337 pWal->exclusiveMode = WAL_NORMAL_MODE;
68338 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
68339 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
68340 }
68341 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
68342 }else{
68343 /* Already in locking_mode=NORMAL */
68344 rc = 0;
68345 }
68346 }else if( op>0 ){
68347 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
68348 assert( pWal->readLock>=0 );
68349 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
68350 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
68351 rc = 1;
68352 }else{
68353 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
68354 }
68355 return rc;
68356}
68357
68358/*
68359** Return true if the argument is non-NULL and the WAL module is using
68360** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
68361** WAL module is using shared-memory, return false.
68362*/
68363SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
68364 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
68365}
68366
68367#ifdef SQLITE_ENABLE_SNAPSHOT
68368/* Create a snapshot object. The content of a snapshot is opaque to
68369** every other subsystem, so the WAL module can put whatever it needs
68370** in the object.
68371*/
68372SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
68373 int rc = SQLITE_OK;
68374 WalIndexHdr *pRet;
68375 static const u32 aZero[4] = { 0, 0, 0, 0 };
68376
68377 assert( pWal->readLock>=0 && pWal->writeLock==0 );
68378
68379 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
68380 *ppSnapshot = 0;
68381 return SQLITE_ERROR;
68382 }
68383 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
68384 if( pRet==0 ){
68385 rc = SQLITE_NOMEM_BKPT;
68386 }else{
68387 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
68388 *ppSnapshot = (sqlite3_snapshot*)pRet;
68389 }
68390
68391 return rc;
68392}
68393
68394/* Try to open on pSnapshot when the next read-transaction starts
68395*/
68396SQLITE_PRIVATE void sqlite3WalSnapshotOpen(
68397 Wal *pWal,
68398 sqlite3_snapshot *pSnapshot
68399){
68400 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
68401}
68402
68403/*
68404** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
68405** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
68406*/
68407SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
68408 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
68409 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
68410
68411 /* aSalt[0] is a copy of the value stored in the wal file header. It
68412 ** is incremented each time the wal file is restarted. */
68413 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
68414 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
68415 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
68416 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
68417 return 0;
68418}
68419
68420/*
68421** The caller currently has a read transaction open on the database.
68422** This function takes a SHARED lock on the CHECKPOINTER slot and then
68423** checks if the snapshot passed as the second argument is still
68424** available. If so, SQLITE_OK is returned.
68425**
68426** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
68427** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
68428** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
68429** lock is released before returning.
68430*/
68431SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
68432 int rc;
68433 SEH_TRY {
68434 rc = walLockShared(pWal, WAL_CKPT_LOCK);
68435 if( rc==SQLITE_OK ){
68436 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
68437 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
68438 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
68439 ){
68440 rc = SQLITE_ERROR_SNAPSHOT;
68441 walUnlockShared(pWal, WAL_CKPT_LOCK);
68442 }
68443 }
68444 }
68445 SEH_EXCEPT( rc = walHandleException(pWal); )
68446 return rc;
68447}
68448
68449/*
68450** Release a lock obtained by an earlier successful call to
68451** sqlite3WalSnapshotCheck().
68452*/
68453SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
68454 assert( pWal );
68455 walUnlockShared(pWal, WAL_CKPT_LOCK);
68456}
68457
68458
68459#endif /* SQLITE_ENABLE_SNAPSHOT */
68460
68461#ifdef SQLITE_ENABLE_ZIPVFS
68462/*
68463** If the argument is not NULL, it points to a Wal object that holds a
68464** read-lock. This function returns the database page-size if it is known,
68465** or zero if it is not (or if pWal is NULL).
68466*/
68467SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
68468 assert( pWal==0 || pWal->readLock>=0 );
68469 return (pWal ? pWal->szPage : 0);
68470}
68471#endif
68472
68473/* Return the sqlite3_file object for the WAL file
68474*/
68475SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
68476 return pWal->pWalFd;
68477}
68478
68479#endif /* #ifndef SQLITE_OMIT_WAL */
68480
68481/************** End of wal.c *************************************************/
68482/************** Begin file btmutex.c *****************************************/
68483/*
68484** 2007 August 27
68485**
68486** The author disclaims copyright to this source code. In place of
68487** a legal notice, here is a blessing:
68488**
68489** May you do good and not evil.
68490** May you find forgiveness for yourself and forgive others.
68491** May you share freely, never taking more than you give.
68492**
68493*************************************************************************
68494**
68495** This file contains code used to implement mutexes on Btree objects.
68496** This code really belongs in btree.c. But btree.c is getting too
68497** big and we want to break it down some. This packaged seemed like
68498** a good breakout.
68499*/
68500/************** Include btreeInt.h in the middle of btmutex.c ****************/
68501/************** Begin file btreeInt.h ****************************************/
68502/*
68503** 2004 April 6
68504**
68505** The author disclaims copyright to this source code. In place of
68506** a legal notice, here is a blessing:
68507**
68508** May you do good and not evil.
68509** May you find forgiveness for yourself and forgive others.
68510** May you share freely, never taking more than you give.
68511**
68512*************************************************************************
68513** This file implements an external (disk-based) database using BTrees.
68514** For a detailed discussion of BTrees, refer to
68515**
68516** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
68517** "Sorting And Searching", pages 473-480. Addison-Wesley
68518** Publishing Company, Reading, Massachusetts.
68519**
68520** The basic idea is that each page of the file contains N database
68521** entries and N+1 pointers to subpages.
68522**
68523** ----------------------------------------------------------------
68524** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
68525** ----------------------------------------------------------------
68526**
68527** All of the keys on the page that Ptr(0) points to have values less
68528** than Key(0). All of the keys on page Ptr(1) and its subpages have
68529** values greater than Key(0) and less than Key(1). All of the keys
68530** on Ptr(N) and its subpages have values greater than Key(N-1). And
68531** so forth.
68532**
68533** Finding a particular key requires reading O(log(M)) pages from the
68534** disk where M is the number of entries in the tree.
68535**
68536** In this implementation, a single file can hold one or more separate
68537** BTrees. Each BTree is identified by the index of its root page. The
68538** key and data for any entry are combined to form the "payload". A
68539** fixed amount of payload can be carried directly on the database
68540** page. If the payload is larger than the preset amount then surplus
68541** bytes are stored on overflow pages. The payload for an entry
68542** and the preceding pointer are combined to form a "Cell". Each
68543** page has a small header which contains the Ptr(N) pointer and other
68544** information such as the size of key and data.
68545**
68546** FORMAT DETAILS
68547**
68548** The file is divided into pages. The first page is called page 1,
68549** the second is page 2, and so forth. A page number of zero indicates
68550** "no such page". The page size can be any power of 2 between 512 and 65536.
68551** Each page can be either a btree page, a freelist page, an overflow
68552** page, or a pointer-map page.
68553**
68554** The first page is always a btree page. The first 100 bytes of the first
68555** page contain a special header (the "file header") that describes the file.
68556** The format of the file header is as follows:
68557**
68558** OFFSET SIZE DESCRIPTION
68559** 0 16 Header string: "SQLite format 3\000"
68560** 16 2 Page size in bytes. (1 means 65536)
68561** 18 1 File format write version
68562** 19 1 File format read version
68563** 20 1 Bytes of unused space at the end of each page
68564** 21 1 Max embedded payload fraction (must be 64)
68565** 22 1 Min embedded payload fraction (must be 32)
68566** 23 1 Min leaf payload fraction (must be 32)
68567** 24 4 File change counter
68568** 28 4 Reserved for future use
68569** 32 4 First freelist page
68570** 36 4 Number of freelist pages in the file
68571** 40 60 15 4-byte meta values passed to higher layers
68572**
68573** 40 4 Schema cookie
68574** 44 4 File format of schema layer
68575** 48 4 Size of page cache
68576** 52 4 Largest root-page (auto/incr_vacuum)
68577** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
68578** 60 4 User version
68579** 64 4 Incremental vacuum mode
68580** 68 4 Application-ID
68581** 72 20 unused
68582** 92 4 The version-valid-for number
68583** 96 4 SQLITE_VERSION_NUMBER
68584**
68585** All of the integer values are big-endian (most significant byte first).
68586**
68587** The file change counter is incremented when the database is changed
68588** This counter allows other processes to know when the file has changed
68589** and thus when they need to flush their cache.
68590**
68591** The max embedded payload fraction is the amount of the total usable
68592** space in a page that can be consumed by a single cell for standard
68593** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
68594** is to limit the maximum cell size so that at least 4 cells will fit
68595** on one page. Thus the default max embedded payload fraction is 64.
68596**
68597** If the payload for a cell is larger than the max payload, then extra
68598** payload is spilled to overflow pages. Once an overflow page is allocated,
68599** as many bytes as possible are moved into the overflow pages without letting
68600** the cell size drop below the min embedded payload fraction.
68601**
68602** The min leaf payload fraction is like the min embedded payload fraction
68603** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
68604** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
68605** not specified in the header.
68606**
68607** Each btree pages is divided into three sections: The header, the
68608** cell pointer array, and the cell content area. Page 1 also has a 100-byte
68609** file header that occurs before the page header.
68610**
68611** |----------------|
68612** | file header | 100 bytes. Page 1 only.
68613** |----------------|
68614** | page header | 8 bytes for leaves. 12 bytes for interior nodes
68615** |----------------|
68616** | cell pointer | | 2 bytes per cell. Sorted order.
68617** | array | | Grows downward
68618** | | v
68619** |----------------|
68620** | unallocated |
68621** | space |
68622** |----------------| ^ Grows upwards
68623** | cell content | | Arbitrary order interspersed with freeblocks.
68624** | area | | and free space fragments.
68625** |----------------|
68626**
68627** The page headers looks like this:
68628**
68629** OFFSET SIZE DESCRIPTION
68630** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
68631** 1 2 byte offset to the first freeblock
68632** 3 2 number of cells on this page
68633** 5 2 first byte of the cell content area
68634** 7 1 number of fragmented free bytes
68635** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
68636**
68637** The flags define the format of this btree page. The leaf flag means that
68638** this page has no children. The zerodata flag means that this page carries
68639** only keys and no data. The intkey flag means that the key is an integer
68640** which is stored in the key size entry of the cell header rather than in
68641** the payload area.
68642**
68643** The cell pointer array begins on the first byte after the page header.
68644** The cell pointer array contains zero or more 2-byte numbers which are
68645** offsets from the beginning of the page to the cell content in the cell
68646** content area. The cell pointers occur in sorted order. The system strives
68647** to keep free space after the last cell pointer so that new cells can
68648** be easily added without having to defragment the page.
68649**
68650** Cell content is stored at the very end of the page and grows toward the
68651** beginning of the page.
68652**
68653** Unused space within the cell content area is collected into a linked list of
68654** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
68655** to the first freeblock is given in the header. Freeblocks occur in
68656** increasing order. Because a freeblock must be at least 4 bytes in size,
68657** any group of 3 or fewer unused bytes in the cell content area cannot
68658** exist on the freeblock chain. A group of 3 or fewer free bytes is called
68659** a fragment. The total number of bytes in all fragments is recorded.
68660** in the page header at offset 7.
68661**
68662** SIZE DESCRIPTION
68663** 2 Byte offset of the next freeblock
68664** 2 Bytes in this freeblock
68665**
68666** Cells are of variable length. Cells are stored in the cell content area at
68667** the end of the page. Pointers to the cells are in the cell pointer array
68668** that immediately follows the page header. Cells is not necessarily
68669** contiguous or in order, but cell pointers are contiguous and in order.
68670**
68671** Cell content makes use of variable length integers. A variable
68672** length integer is 1 to 9 bytes where the lower 7 bits of each
68673** byte are used. The integer consists of all bytes that have bit 8 set and
68674** the first byte with bit 8 clear. The most significant byte of the integer
68675** appears first. A variable-length integer may not be more than 9 bytes long.
68676** As a special case, all 8 bits of the 9th byte are used as data. This
68677** allows a 64-bit integer to be encoded in 9 bytes.
68678**
68679** 0x00 becomes 0x00000000
68680** 0x7f becomes 0x0000007f
68681** 0x81 0x00 becomes 0x00000080
68682** 0x82 0x00 becomes 0x00000100
68683** 0x80 0x7f becomes 0x0000007f
68684** 0x81 0x91 0xd1 0xac 0x78 becomes 0x12345678
68685** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
68686**
68687** Variable length integers are used for rowids and to hold the number of
68688** bytes of key and data in a btree cell.
68689**
68690** The content of a cell looks like this:
68691**
68692** SIZE DESCRIPTION
68693** 4 Page number of the left child. Omitted if leaf flag is set.
68694** var Number of bytes of data. Omitted if the zerodata flag is set.
68695** var Number of bytes of key. Or the key itself if intkey flag is set.
68696** * Payload
68697** 4 First page of the overflow chain. Omitted if no overflow
68698**
68699** Overflow pages form a linked list. Each page except the last is completely
68700** filled with data (pagesize - 4 bytes). The last page can have as little
68701** as 1 byte of data.
68702**
68703** SIZE DESCRIPTION
68704** 4 Page number of next overflow page
68705** * Data
68706**
68707** Freelist pages come in two subtypes: trunk pages and leaf pages. The
68708** file header points to the first in a linked list of trunk page. Each trunk
68709** page points to multiple leaf pages. The content of a leaf page is
68710** unspecified. A trunk page looks like this:
68711**
68712** SIZE DESCRIPTION
68713** 4 Page number of next trunk page
68714** 4 Number of leaf pointers on this page
68715** * zero or more pages numbers of leaves
68716*/
68717/* #include "sqliteInt.h" */
68718
68719
68720/* The following value is the maximum cell size assuming a maximum page
68721** size give above.
68722*/
68723#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
68724
68725/* The maximum number of cells on a single page of the database. This
68726** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
68727** plus 2 bytes for the index to the cell in the page header). Such
68728** small cells will be rare, but they are possible.
68729*/
68730#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
68731
68732/* Forward declarations */
68733typedef struct MemPage MemPage;
68734typedef struct BtLock BtLock;
68735typedef struct CellInfo CellInfo;
68736
68737/*
68738** This is a magic string that appears at the beginning of every
68739** SQLite database in order to identify the file as a real database.
68740**
68741** You can change this value at compile-time by specifying a
68742** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
68743** header must be exactly 16 bytes including the zero-terminator so
68744** the string itself should be 15 characters long. If you change
68745** the header, then your custom library will not be able to read
68746** databases generated by the standard tools and the standard tools
68747** will not be able to read databases created by your custom library.
68748*/
68749#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
68750# define SQLITE_FILE_HEADER "SQLite format 3"
68751#endif
68752
68753/*
68754** Page type flags. An ORed combination of these flags appear as the
68755** first byte of on-disk image of every BTree page.
68756*/
68757#define PTF_INTKEY 0x01
68758#define PTF_ZERODATA 0x02
68759#define PTF_LEAFDATA 0x04
68760#define PTF_LEAF 0x08
68761
68762/*
68763** An instance of this object stores information about each a single database
68764** page that has been loaded into memory. The information in this object
68765** is derived from the raw on-disk page content.
68766**
68767** As each database page is loaded into memory, the pager allocates an
68768** instance of this object and zeros the first 8 bytes. (This is the
68769** "extra" information associated with each page of the pager.)
68770**
68771** Access to all fields of this structure is controlled by the mutex
68772** stored in MemPage.pBt->mutex.
68773*/
68774struct MemPage {
68775 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
68776 u8 intKey; /* True if table b-trees. False for index b-trees */
68777 u8 intKeyLeaf; /* True if the leaf of an intKey table */
68778 Pgno pgno; /* Page number for this page */
68779 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
68780 ** is allocated. All fields that follow must be initialized before use */
68781 u8 leaf; /* True if a leaf page */
68782 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
68783 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
68784 u8 max1bytePayload; /* min(maxLocal,127) */
68785 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
68786 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
68787 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
68788 u16 cellOffset; /* Index in aData of first cell pointer */
68789 int nFree; /* Number of free bytes on the page. -1 for unknown */
68790 u16 nCell; /* Number of cells on this page, local and ovfl */
68791 u16 maskPage; /* Mask for page offset */
68792 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
68793 ** non-overflow cell */
68794 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
68795 BtShared *pBt; /* Pointer to BtShared that this page is part of */
68796 u8 *aData; /* Pointer to disk image of the page data */
68797 u8 *aDataEnd; /* One byte past the end of the entire page - not just
68798 ** the usable space, the entire page. Used to prevent
68799 ** corruption-induced buffer overflow. */
68800 u8 *aCellIdx; /* The cell index area */
68801 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
68802 DbPage *pDbPage; /* Pager page handle */
68803 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
68804 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
68805};
68806
68807/*
68808** A linked list of the following structures is stored at BtShared.pLock.
68809** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
68810** is opened on the table with root page BtShared.iTable. Locks are removed
68811** from this list when a transaction is committed or rolled back, or when
68812** a btree handle is closed.
68813*/
68814struct BtLock {
68815 Btree *pBtree; /* Btree handle holding this lock */
68816 Pgno iTable; /* Root page of table */
68817 u8 eLock; /* READ_LOCK or WRITE_LOCK */
68818 BtLock *pNext; /* Next in BtShared.pLock list */
68819};
68820
68821/* Candidate values for BtLock.eLock */
68822#define READ_LOCK 1
68823#define WRITE_LOCK 2
68824
68825/* A Btree handle
68826**
68827** A database connection contains a pointer to an instance of
68828** this object for every database file that it has open. This structure
68829** is opaque to the database connection. The database connection cannot
68830** see the internals of this structure and only deals with pointers to
68831** this structure.
68832**
68833** For some database files, the same underlying database cache might be
68834** shared between multiple connections. In that case, each connection
68835** has it own instance of this object. But each instance of this object
68836** points to the same BtShared object. The database cache and the
68837** schema associated with the database file are all contained within
68838** the BtShared object.
68839**
68840** All fields in this structure are accessed under sqlite3.mutex.
68841** The pBt pointer itself may not be changed while there exists cursors
68842** in the referenced BtShared that point back to this Btree since those
68843** cursors have to go through this Btree to find their BtShared and
68844** they often do so without holding sqlite3.mutex.
68845*/
68846struct Btree {
68847 sqlite3 *db; /* The database connection holding this btree */
68848 BtShared *pBt; /* Sharable content of this btree */
68849 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
68850 u8 sharable; /* True if we can share pBt with another db */
68851 u8 locked; /* True if db currently has pBt locked */
68852 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
68853 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
68854 int nBackup; /* Number of backup operations reading this btree */
68855 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
68856 Btree *pNext; /* List of other sharable Btrees from the same db */
68857 Btree *pPrev; /* Back pointer of the same list */
68858#ifdef SQLITE_DEBUG
68859 u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */
68860#endif
68861#ifndef SQLITE_OMIT_SHARED_CACHE
68862 BtLock lock; /* Object used to lock page 1 */
68863#endif
68864};
68865
68866/*
68867** Btree.inTrans may take one of the following values.
68868**
68869** If the shared-data extension is enabled, there may be multiple users
68870** of the Btree structure. At most one of these may open a write transaction,
68871** but any number may have active read transactions.
68872**
68873** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and
68874** SQLITE_TXN_WRITE
68875*/
68876#define TRANS_NONE 0
68877#define TRANS_READ 1
68878#define TRANS_WRITE 2
68879
68880#if TRANS_NONE!=SQLITE_TXN_NONE
68881# error wrong numeric code for no-transaction
68882#endif
68883#if TRANS_READ!=SQLITE_TXN_READ
68884# error wrong numeric code for read-transaction
68885#endif
68886#if TRANS_WRITE!=SQLITE_TXN_WRITE
68887# error wrong numeric code for write-transaction
68888#endif
68889
68890
68891/*
68892** An instance of this object represents a single database file.
68893**
68894** A single database file can be in use at the same time by two
68895** or more database connections. When two or more connections are
68896** sharing the same database file, each connection has it own
68897** private Btree object for the file and each of those Btrees points
68898** to this one BtShared object. BtShared.nRef is the number of
68899** connections currently sharing this database file.
68900**
68901** Fields in this structure are accessed under the BtShared.mutex
68902** mutex, except for nRef and pNext which are accessed under the
68903** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field
68904** may not be modified once it is initially set as long as nRef>0.
68905** The pSchema field may be set once under BtShared.mutex and
68906** thereafter is unchanged as long as nRef>0.
68907**
68908** isPending:
68909**
68910** If a BtShared client fails to obtain a write-lock on a database
68911** table (because there exists one or more read-locks on the table),
68912** the shared-cache enters 'pending-lock' state and isPending is
68913** set to true.
68914**
68915** The shared-cache leaves the 'pending lock' state when either of
68916** the following occur:
68917**
68918** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
68919** 2) The number of locks held by other connections drops to zero.
68920**
68921** while in the 'pending-lock' state, no connection may start a new
68922** transaction.
68923**
68924** This feature is included to help prevent writer-starvation.
68925*/
68926struct BtShared {
68927 Pager *pPager; /* The page cache */
68928 sqlite3 *db; /* Database connection currently using this Btree */
68929 BtCursor *pCursor; /* A list of all open cursors */
68930 MemPage *pPage1; /* First page of the database */
68931 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
68932#ifndef SQLITE_OMIT_AUTOVACUUM
68933 u8 autoVacuum; /* True if auto-vacuum is enabled */
68934 u8 incrVacuum; /* True if incr-vacuum is enabled */
68935 u8 bDoTruncate; /* True to truncate db on commit */
68936#endif
68937 u8 inTransaction; /* Transaction state */
68938 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
68939 u8 nReserveWanted; /* Desired number of extra bytes per page */
68940 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
68941 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
68942 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
68943 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
68944 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
68945 u32 pageSize; /* Total number of bytes on a page */
68946 u32 usableSize; /* Number of usable bytes on each page */
68947 int nTransaction; /* Number of open transactions (read + write) */
68948 u32 nPage; /* Number of pages in the database */
68949 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
68950 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
68951 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
68952 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
68953#ifndef SQLITE_OMIT_SHARED_CACHE
68954 int nRef; /* Number of references to this structure */
68955 BtShared *pNext; /* Next on a list of sharable BtShared structs */
68956 BtLock *pLock; /* List of locks held on this shared-btree struct */
68957 Btree *pWriter; /* Btree with currently open write transaction */
68958#endif
68959 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
68960 int nPreformatSize; /* Size of last cell written by TransferRow() */
68961};
68962
68963/*
68964** Allowed values for BtShared.btsFlags
68965*/
68966#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
68967#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
68968#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
68969#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */
68970#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */
68971#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */
68972#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
68973#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */
68974#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
68975
68976/*
68977** An instance of the following structure is used to hold information
68978** about a cell. The parseCellPtr() function fills in this structure
68979** based on information extract from the raw disk page.
68980*/
68981struct CellInfo {
68982 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
68983 u8 *pPayload; /* Pointer to the start of payload */
68984 u32 nPayload; /* Bytes of payload */
68985 u16 nLocal; /* Amount of payload held locally, not on overflow */
68986 u16 nSize; /* Size of the cell content on the main b-tree page */
68987};
68988
68989/*
68990** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
68991** this will be declared corrupt. This value is calculated based on a
68992** maximum database size of 2^31 pages a minimum fanout of 2 for a
68993** root-node and 3 for all other internal nodes.
68994**
68995** If a tree that appears to be taller than this is encountered, it is
68996** assumed that the database is corrupt.
68997*/
68998#define BTCURSOR_MAX_DEPTH 20
68999
69000/*
69001** A cursor is a pointer to a particular entry within a particular
69002** b-tree within a database file.
69003**
69004** The entry is identified by its MemPage and the index in
69005** MemPage.aCell[] of the entry.
69006**
69007** A single database file can be shared by two more database connections,
69008** but cursors cannot be shared. Each cursor is associated with a
69009** particular database connection identified BtCursor.pBtree.db.
69010**
69011** Fields in this structure are accessed under the BtShared.mutex
69012** found at self->pBt->mutex.
69013**
69014** skipNext meaning:
69015** The meaning of skipNext depends on the value of eState:
69016**
69017** eState Meaning of skipNext
69018** VALID skipNext is meaningless and is ignored
69019** INVALID skipNext is meaningless and is ignored
69020** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
69021** sqlite3BtreePrevious() is no-op if skipNext<0.
69022** REQUIRESEEK restoreCursorPosition() restores the cursor to
69023** eState=SKIPNEXT if skipNext!=0
69024** FAULT skipNext holds the cursor fault error code.
69025*/
69026struct BtCursor {
69027 u8 eState; /* One of the CURSOR_XXX constants (see below) */
69028 u8 curFlags; /* zero or more BTCF_* flags defined below */
69029 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
69030 u8 hints; /* As configured by CursorSetHints() */
69031 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
69032 ** Error code if eState==CURSOR_FAULT */
69033 Btree *pBtree; /* The Btree to which this cursor belongs */
69034 Pgno *aOverflow; /* Cache of overflow page locations */
69035 void *pKey; /* Saved key that was cursor last known position */
69036 /* All fields above are zeroed when the cursor is allocated. See
69037 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
69038 ** initialized. */
69039#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
69040 BtShared *pBt; /* The BtShared this cursor points to */
69041 BtCursor *pNext; /* Forms a linked list of all cursors */
69042 CellInfo info; /* A parse of the cell we are pointing at */
69043 i64 nKey; /* Size of pKey, or last integer key */
69044 Pgno pgnoRoot; /* The root page of this tree */
69045 i8 iPage; /* Index of current page in apPage */
69046 u8 curIntKey; /* Value of apPage[0]->intKey */
69047 u16 ix; /* Current index for apPage[iPage] */
69048 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
69049 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
69050 MemPage *pPage; /* Current page */
69051 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
69052};
69053
69054/*
69055** Legal values for BtCursor.curFlags
69056*/
69057#define BTCF_WriteFlag 0x01 /* True if a write cursor */
69058#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
69059#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
69060#define BTCF_AtLast 0x08 /* Cursor is pointing to the last entry */
69061#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
69062#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
69063#define BTCF_Pinned 0x40 /* Cursor is busy and cannot be moved */
69064
69065/*
69066** Potential values for BtCursor.eState.
69067**
69068** CURSOR_INVALID:
69069** Cursor does not point to a valid entry. This can happen (for example)
69070** because the table is empty or because BtreeCursorFirst() has not been
69071** called.
69072**
69073** CURSOR_VALID:
69074** Cursor points to a valid entry. getPayload() etc. may be called.
69075**
69076** CURSOR_SKIPNEXT:
69077** Cursor is valid except that the Cursor.skipNext field is non-zero
69078** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
69079** operation should be a no-op.
69080**
69081** CURSOR_REQUIRESEEK:
69082** The table that this cursor was opened on still exists, but has been
69083** modified since the cursor was last used. The cursor position is saved
69084** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
69085** this state, restoreCursorPosition() can be called to attempt to
69086** seek the cursor to the saved position.
69087**
69088** CURSOR_FAULT:
69089** An unrecoverable error (an I/O error or a malloc failure) has occurred
69090** on a different connection that shares the BtShared cache with this
69091** cursor. The error has left the cache in an inconsistent state.
69092** Do nothing else with this cursor. Any attempt to use the cursor
69093** should return the error code stored in BtCursor.skipNext
69094*/
69095#define CURSOR_VALID 0
69096#define CURSOR_INVALID 1
69097#define CURSOR_SKIPNEXT 2
69098#define CURSOR_REQUIRESEEK 3
69099#define CURSOR_FAULT 4
69100
69101/*
69102** The database page the PENDING_BYTE occupies. This page is never used.
69103*/
69104#define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
69105
69106/*
69107** These macros define the location of the pointer-map entry for a
69108** database page. The first argument to each is the number of usable
69109** bytes on each page of the database (often 1024). The second is the
69110** page number to look up in the pointer map.
69111**
69112** PTRMAP_PAGENO returns the database page number of the pointer-map
69113** page that stores the required pointer. PTRMAP_PTROFFSET returns
69114** the offset of the requested map entry.
69115**
69116** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
69117** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
69118** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
69119** this test.
69120*/
69121#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
69122#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
69123#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
69124
69125/*
69126** The pointer map is a lookup table that identifies the parent page for
69127** each child page in the database file. The parent page is the page that
69128** contains a pointer to the child. Every page in the database contains
69129** 0 or 1 parent pages. (In this context 'database page' refers
69130** to any page that is not part of the pointer map itself.) Each pointer map
69131** entry consists of a single byte 'type' and a 4 byte parent page number.
69132** The PTRMAP_XXX identifiers below are the valid types.
69133**
69134** The purpose of the pointer map is to facility moving pages from one
69135** position in the file to another as part of autovacuum. When a page
69136** is moved, the pointer in its parent must be updated to point to the
69137** new location. The pointer map is used to locate the parent page quickly.
69138**
69139** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
69140** used in this case.
69141**
69142** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
69143** is not used in this case.
69144**
69145** PTRMAP_OVERFLOW1: The database page is the first page in a list of
69146** overflow pages. The page number identifies the page that
69147** contains the cell with a pointer to this overflow page.
69148**
69149** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
69150** overflow pages. The page-number identifies the previous
69151** page in the overflow page list.
69152**
69153** PTRMAP_BTREE: The database page is a non-root btree page. The page number
69154** identifies the parent page in the btree.
69155*/
69156#define PTRMAP_ROOTPAGE 1
69157#define PTRMAP_FREEPAGE 2
69158#define PTRMAP_OVERFLOW1 3
69159#define PTRMAP_OVERFLOW2 4
69160#define PTRMAP_BTREE 5
69161
69162/* A bunch of assert() statements to check the transaction state variables
69163** of handle p (type Btree*) are internally consistent.
69164*/
69165#define btreeIntegrity(p) \
69166 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
69167 assert( p->pBt->inTransaction>=p->inTrans );
69168
69169
69170/*
69171** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
69172** if the database supports auto-vacuum or not. Because it is used
69173** within an expression that is an argument to another macro
69174** (sqliteMallocRaw), it is not possible to use conditional compilation.
69175** So, this macro is defined instead.
69176*/
69177#ifndef SQLITE_OMIT_AUTOVACUUM
69178#define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
69179#else
69180#define ISAUTOVACUUM(pBt) 0
69181#endif
69182
69183
69184/*
69185** This structure is passed around through all the PRAGMA integrity_check
69186** checking routines in order to keep track of some global state information.
69187**
69188** The aRef[] array is allocated so that there is 1 bit for each page in
69189** the database. As the integrity-check proceeds, for each page used in
69190** the database the corresponding bit is set. This allows integrity-check to
69191** detect pages that are used twice and orphaned pages (both of which
69192** indicate corruption).
69193*/
69194typedef struct IntegrityCk IntegrityCk;
69195struct IntegrityCk {
69196 BtShared *pBt; /* The tree being checked out */
69197 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
69198 u8 *aPgRef; /* 1 bit per page in the db (see above) */
69199 Pgno nPage; /* Number of pages in the database */
69200 int mxErr; /* Stop accumulating errors when this reaches zero */
69201 int nErr; /* Number of messages written to zErrMsg so far */
69202 int rc; /* SQLITE_OK, SQLITE_NOMEM, or SQLITE_INTERRUPT */
69203 u32 nStep; /* Number of steps into the integrity_check process */
69204 const char *zPfx; /* Error message prefix */
69205 Pgno v0; /* Value for first %u substitution in zPfx (root page) */
69206 Pgno v1; /* Value for second %u substitution in zPfx (current pg) */
69207 int v2; /* Value for third %d substitution in zPfx */
69208 StrAccum errMsg; /* Accumulate the error message text here */
69209 u32 *heap; /* Min-heap used for analyzing cell coverage */
69210 sqlite3 *db; /* Database connection running the check */
69211};
69212
69213/*
69214** Routines to read or write a two- and four-byte big-endian integer values.
69215*/
69216#define get2byte(x) ((x)[0]<<8 | (x)[1])
69217#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
69218#define get4byte sqlite3Get4byte
69219#define put4byte sqlite3Put4byte
69220
69221/*
69222** get2byteAligned(), unlike get2byte(), requires that its argument point to a
69223** two-byte aligned address. get2byteAligned() is only used for accessing the
69224** cell addresses in a btree header.
69225*/
69226#if SQLITE_BYTEORDER==4321
69227# define get2byteAligned(x) (*(u16*)(x))
69228#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
69229# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
69230#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
69231# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
69232#else
69233# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
69234#endif
69235
69236/************** End of btreeInt.h ********************************************/
69237/************** Continuing where we left off in btmutex.c ********************/
69238#ifndef SQLITE_OMIT_SHARED_CACHE
69239#if SQLITE_THREADSAFE
69240
69241/*
69242** Obtain the BtShared mutex associated with B-Tree handle p. Also,
69243** set BtShared.db to the database handle associated with p and the
69244** p->locked boolean to true.
69245*/
69246static void lockBtreeMutex(Btree *p){
69247 assert( p->locked==0 );
69248 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
69249 assert( sqlite3_mutex_held(p->db->mutex) );
69250
69251 sqlite3_mutex_enter(p: p->pBt->mutex);
69252 p->pBt->db = p->db;
69253 p->locked = 1;
69254}
69255
69256/*
69257** Release the BtShared mutex associated with B-Tree handle p and
69258** clear the p->locked boolean.
69259*/
69260static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
69261 BtShared *pBt = p->pBt;
69262 assert( p->locked==1 );
69263 assert( sqlite3_mutex_held(pBt->mutex) );
69264 assert( sqlite3_mutex_held(p->db->mutex) );
69265 assert( p->db==pBt->db );
69266
69267 sqlite3_mutex_leave(p: pBt->mutex);
69268 p->locked = 0;
69269}
69270
69271/* Forward reference */
69272static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
69273
69274/*
69275** Enter a mutex on the given BTree object.
69276**
69277** If the object is not sharable, then no mutex is ever required
69278** and this routine is a no-op. The underlying mutex is non-recursive.
69279** But we keep a reference count in Btree.wantToLock so the behavior
69280** of this interface is recursive.
69281**
69282** To avoid deadlocks, multiple Btrees are locked in the same order
69283** by all database connections. The p->pNext is a list of other
69284** Btrees belonging to the same database connection as the p Btree
69285** which need to be locked after p. If we cannot get a lock on
69286** p, then first unlock all of the others on p->pNext, then wait
69287** for the lock to become available on p, then relock all of the
69288** subsequent Btrees that desire a lock.
69289*/
69290SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
69291 /* Some basic sanity checking on the Btree. The list of Btrees
69292 ** connected by pNext and pPrev should be in sorted order by
69293 ** Btree.pBt value. All elements of the list should belong to
69294 ** the same connection. Only shared Btrees are on the list. */
69295 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
69296 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
69297 assert( p->pNext==0 || p->pNext->db==p->db );
69298 assert( p->pPrev==0 || p->pPrev->db==p->db );
69299 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
69300
69301 /* Check for locking consistency */
69302 assert( !p->locked || p->wantToLock>0 );
69303 assert( p->sharable || p->wantToLock==0 );
69304
69305 /* We should already hold a lock on the database connection */
69306 assert( sqlite3_mutex_held(p->db->mutex) );
69307
69308 /* Unless the database is sharable and unlocked, then BtShared.db
69309 ** should already be set correctly. */
69310 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
69311
69312 if( !p->sharable ) return;
69313 p->wantToLock++;
69314 if( p->locked ) return;
69315 btreeLockCarefully(p);
69316}
69317
69318/* This is a helper function for sqlite3BtreeLock(). By moving
69319** complex, but seldom used logic, out of sqlite3BtreeLock() and
69320** into this routine, we avoid unnecessary stack pointer changes
69321** and thus help the sqlite3BtreeLock() routine to run much faster
69322** in the common case.
69323*/
69324static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
69325 Btree *pLater;
69326
69327 /* In most cases, we should be able to acquire the lock we
69328 ** want without having to go through the ascending lock
69329 ** procedure that follows. Just be sure not to block.
69330 */
69331 if( sqlite3_mutex_try(p: p->pBt->mutex)==SQLITE_OK ){
69332 p->pBt->db = p->db;
69333 p->locked = 1;
69334 return;
69335 }
69336
69337 /* To avoid deadlock, first release all locks with a larger
69338 ** BtShared address. Then acquire our lock. Then reacquire
69339 ** the other BtShared locks that we used to hold in ascending
69340 ** order.
69341 */
69342 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
69343 assert( pLater->sharable );
69344 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
69345 assert( !pLater->locked || pLater->wantToLock>0 );
69346 if( pLater->locked ){
69347 unlockBtreeMutex(p: pLater);
69348 }
69349 }
69350 lockBtreeMutex(p);
69351 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
69352 if( pLater->wantToLock ){
69353 lockBtreeMutex(p: pLater);
69354 }
69355 }
69356}
69357
69358
69359/*
69360** Exit the recursive mutex on a Btree.
69361*/
69362SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
69363 assert( sqlite3_mutex_held(p->db->mutex) );
69364 if( p->sharable ){
69365 assert( p->wantToLock>0 );
69366 p->wantToLock--;
69367 if( p->wantToLock==0 ){
69368 unlockBtreeMutex(p);
69369 }
69370 }
69371}
69372
69373#ifndef NDEBUG
69374/*
69375** Return true if the BtShared mutex is held on the btree, or if the
69376** B-Tree is not marked as sharable.
69377**
69378** This routine is used only from within assert() statements.
69379*/
69380SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
69381 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
69382 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
69383 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
69384 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
69385
69386 return (p->sharable==0 || p->locked);
69387}
69388#endif
69389
69390
69391/*
69392** Enter the mutex on every Btree associated with a database
69393** connection. This is needed (for example) prior to parsing
69394** a statement since we will be comparing table and column names
69395** against all schemas and we do not want those schemas being
69396** reset out from under us.
69397**
69398** There is a corresponding leave-all procedures.
69399**
69400** Enter the mutexes in ascending order by BtShared pointer address
69401** to avoid the possibility of deadlock when two threads with
69402** two or more btrees in common both try to lock all their btrees
69403** at the same instant.
69404*/
69405static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
69406 int i;
69407 int skipOk = 1;
69408 Btree *p;
69409 assert( sqlite3_mutex_held(db->mutex) );
69410 for(i=0; i<db->nDb; i++){
69411 p = db->aDb[i].pBt;
69412 if( p && p->sharable ){
69413 sqlite3BtreeEnter(p);
69414 skipOk = 0;
69415 }
69416 }
69417 db->noSharedCache = skipOk;
69418}
69419SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
69420 if( db->noSharedCache==0 ) btreeEnterAll(db);
69421}
69422static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
69423 int i;
69424 Btree *p;
69425 assert( sqlite3_mutex_held(db->mutex) );
69426 for(i=0; i<db->nDb; i++){
69427 p = db->aDb[i].pBt;
69428 if( p ) sqlite3BtreeLeave(p);
69429 }
69430}
69431SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
69432 if( db->noSharedCache==0 ) btreeLeaveAll(db);
69433}
69434
69435#ifndef NDEBUG
69436/*
69437** Return true if the current thread holds the database connection
69438** mutex and all required BtShared mutexes.
69439**
69440** This routine is used inside assert() statements only.
69441*/
69442SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
69443 int i;
69444 if( !sqlite3_mutex_held(db->mutex) ){
69445 return 0;
69446 }
69447 for(i=0; i<db->nDb; i++){
69448 Btree *p;
69449 p = db->aDb[i].pBt;
69450 if( p && p->sharable &&
69451 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
69452 return 0;
69453 }
69454 }
69455 return 1;
69456}
69457#endif /* NDEBUG */
69458
69459#ifndef NDEBUG
69460/*
69461** Return true if the correct mutexes are held for accessing the
69462** db->aDb[iDb].pSchema structure. The mutexes required for schema
69463** access are:
69464**
69465** (1) The mutex on db
69466** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
69467**
69468** If pSchema is not NULL, then iDb is computed from pSchema and
69469** db using sqlite3SchemaToIndex().
69470*/
69471SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
69472 Btree *p;
69473 assert( db!=0 );
69474 if( db->pVfs==0 && db->nDb==0 ) return 1;
69475 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
69476 assert( iDb>=0 && iDb<db->nDb );
69477 if( !sqlite3_mutex_held(db->mutex) ) return 0;
69478 if( iDb==1 ) return 1;
69479 p = db->aDb[iDb].pBt;
69480 assert( p!=0 );
69481 return p->sharable==0 || p->locked==1;
69482}
69483#endif /* NDEBUG */
69484
69485#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
69486/*
69487** The following are special cases for mutex enter routines for use
69488** in single threaded applications that use shared cache. Except for
69489** these two routines, all mutex operations are no-ops in that case and
69490** are null #defines in btree.h.
69491**
69492** If shared cache is disabled, then all btree mutex routines, including
69493** the ones below, are no-ops and are null #defines in btree.h.
69494*/
69495
69496SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
69497 p->pBt->db = p->db;
69498}
69499SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
69500 int i;
69501 for(i=0; i<db->nDb; i++){
69502 Btree *p = db->aDb[i].pBt;
69503 if( p ){
69504 p->pBt->db = p->db;
69505 }
69506 }
69507}
69508#endif /* if SQLITE_THREADSAFE */
69509
69510#ifndef SQLITE_OMIT_INCRBLOB
69511/*
69512** Enter a mutex on a Btree given a cursor owned by that Btree.
69513**
69514** These entry points are used by incremental I/O only. Enter() is required
69515** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
69516** the build is threadsafe. Leave() is only required by threadsafe builds.
69517*/
69518SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
69519 sqlite3BtreeEnter(p: pCur->pBtree);
69520}
69521# if SQLITE_THREADSAFE
69522SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
69523 sqlite3BtreeLeave(p: pCur->pBtree);
69524}
69525# endif
69526#endif /* ifndef SQLITE_OMIT_INCRBLOB */
69527
69528#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
69529
69530/************** End of btmutex.c *********************************************/
69531/************** Begin file btree.c *******************************************/
69532
69533/*
69534** 2004 April 6
69535**
69536** The author disclaims copyright to this source code. In place of
69537** a legal notice, here is a blessing:
69538**
69539** May you do good and not evil.
69540** May you find forgiveness for yourself and forgive others.
69541** May you share freely, never taking more than you give.
69542**
69543*************************************************************************
69544** This file implements an external (disk-based) database using BTrees.
69545** See the header comment on "btreeInt.h" for additional information.
69546** Including a description of file format and an overview of operation.
69547*/
69548/* #include "btreeInt.h" */
69549
69550/*
69551** The header string that appears at the beginning of every
69552** SQLite database.
69553*/
69554static const char zMagicHeader[] = SQLITE_FILE_HEADER;
69555
69556/*
69557** Set this global variable to 1 to enable tracing using the TRACE
69558** macro.
69559*/
69560#if 0
69561int sqlite3BtreeTrace=1; /* True to enable tracing */
69562# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
69563#else
69564# define TRACE(X)
69565#endif
69566
69567/*
69568** Extract a 2-byte big-endian integer from an array of unsigned bytes.
69569** But if the value is zero, make it 65536.
69570**
69571** This routine is used to extract the "offset to cell content area" value
69572** from the header of a btree page. If the page size is 65536 and the page
69573** is empty, the offset should be 65536, but the 2-byte value stores zero.
69574** This routine makes the necessary adjustment to 65536.
69575*/
69576#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
69577
69578/*
69579** Values passed as the 5th argument to allocateBtreePage()
69580*/
69581#define BTALLOC_ANY 0 /* Allocate any page */
69582#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
69583#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
69584
69585/*
69586** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
69587** defined, or 0 if it is. For example:
69588**
69589** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
69590*/
69591#ifndef SQLITE_OMIT_AUTOVACUUM
69592#define IfNotOmitAV(expr) (expr)
69593#else
69594#define IfNotOmitAV(expr) 0
69595#endif
69596
69597#ifndef SQLITE_OMIT_SHARED_CACHE
69598/*
69599** A list of BtShared objects that are eligible for participation
69600** in shared cache. This variable has file scope during normal builds,
69601** but the test harness needs to access it so we make it global for
69602** test builds.
69603**
69604** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
69605*/
69606#ifdef SQLITE_TEST
69607SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
69608#else
69609static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
69610#endif
69611#endif /* SQLITE_OMIT_SHARED_CACHE */
69612
69613#ifndef SQLITE_OMIT_SHARED_CACHE
69614/*
69615** Enable or disable the shared pager and schema features.
69616**
69617** This routine has no effect on existing database connections.
69618** The shared cache setting effects only future calls to
69619** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
69620*/
69621SQLITE_API int sqlite3_enable_shared_cache(int enable){
69622 sqlite3GlobalConfig.sharedCacheEnabled = enable;
69623 return SQLITE_OK;
69624}
69625#endif
69626
69627
69628
69629#ifdef SQLITE_OMIT_SHARED_CACHE
69630 /*
69631 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
69632 ** and clearAllSharedCacheTableLocks()
69633 ** manipulate entries in the BtShared.pLock linked list used to store
69634 ** shared-cache table level locks. If the library is compiled with the
69635 ** shared-cache feature disabled, then there is only ever one user
69636 ** of each BtShared structure and so this locking is not necessary.
69637 ** So define the lock related functions as no-ops.
69638 */
69639 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
69640 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
69641 #define clearAllSharedCacheTableLocks(a)
69642 #define downgradeAllSharedCacheTableLocks(a)
69643 #define hasSharedCacheTableLock(a,b,c,d) 1
69644 #define hasReadConflicts(a, b) 0
69645#endif
69646
69647#ifdef SQLITE_DEBUG
69648/*
69649** Return and reset the seek counter for a Btree object.
69650*/
69651SQLITE_PRIVATE sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){
69652 u64 n = pBt->nSeek;
69653 pBt->nSeek = 0;
69654 return n;
69655}
69656#endif
69657
69658/*
69659** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
69660** (MemPage*) as an argument. The (MemPage*) must not be NULL.
69661**
69662** If SQLITE_DEBUG is not defined, then this macro is equivalent to
69663** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
69664** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
69665** with the page number and filename associated with the (MemPage*).
69666*/
69667#ifdef SQLITE_DEBUG
69668int corruptPageError(int lineno, MemPage *p){
69669 char *zMsg;
69670 sqlite3BeginBenignMalloc();
69671 zMsg = sqlite3_mprintf("database corruption page %u of %s",
69672 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
69673 );
69674 sqlite3EndBenignMalloc();
69675 if( zMsg ){
69676 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
69677 }
69678 sqlite3_free(zMsg);
69679 return SQLITE_CORRUPT_BKPT;
69680}
69681# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
69682#else
69683# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
69684#endif
69685
69686#ifndef SQLITE_OMIT_SHARED_CACHE
69687
69688#ifdef SQLITE_DEBUG
69689/*
69690**** This function is only used as part of an assert() statement. ***
69691**
69692** Check to see if pBtree holds the required locks to read or write to the
69693** table with root page iRoot. Return 1 if it does and 0 if not.
69694**
69695** For example, when writing to a table with root-page iRoot via
69696** Btree connection pBtree:
69697**
69698** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
69699**
69700** When writing to an index that resides in a sharable database, the
69701** caller should have first obtained a lock specifying the root page of
69702** the corresponding table. This makes things a bit more complicated,
69703** as this module treats each table as a separate structure. To determine
69704** the table corresponding to the index being written, this
69705** function has to search through the database schema.
69706**
69707** Instead of a lock on the table/index rooted at page iRoot, the caller may
69708** hold a write-lock on the schema table (root page 1). This is also
69709** acceptable.
69710*/
69711static int hasSharedCacheTableLock(
69712 Btree *pBtree, /* Handle that must hold lock */
69713 Pgno iRoot, /* Root page of b-tree */
69714 int isIndex, /* True if iRoot is the root of an index b-tree */
69715 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
69716){
69717 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
69718 Pgno iTab = 0;
69719 BtLock *pLock;
69720
69721 /* If this database is not shareable, or if the client is reading
69722 ** and has the read-uncommitted flag set, then no lock is required.
69723 ** Return true immediately.
69724 */
69725 if( (pBtree->sharable==0)
69726 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
69727 ){
69728 return 1;
69729 }
69730
69731 /* If the client is reading or writing an index and the schema is
69732 ** not loaded, then it is too difficult to actually check to see if
69733 ** the correct locks are held. So do not bother - just return true.
69734 ** This case does not come up very often anyhow.
69735 */
69736 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
69737 return 1;
69738 }
69739
69740 /* Figure out the root-page that the lock should be held on. For table
69741 ** b-trees, this is just the root page of the b-tree being read or
69742 ** written. For index b-trees, it is the root page of the associated
69743 ** table. */
69744 if( isIndex ){
69745 HashElem *p;
69746 int bSeen = 0;
69747 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
69748 Index *pIdx = (Index *)sqliteHashData(p);
69749 if( pIdx->tnum==iRoot ){
69750 if( bSeen ){
69751 /* Two or more indexes share the same root page. There must
69752 ** be imposter tables. So just return true. The assert is not
69753 ** useful in that case. */
69754 return 1;
69755 }
69756 iTab = pIdx->pTable->tnum;
69757 bSeen = 1;
69758 }
69759 }
69760 }else{
69761 iTab = iRoot;
69762 }
69763
69764 /* Search for the required lock. Either a write-lock on root-page iTab, a
69765 ** write-lock on the schema table, or (if the client is reading) a
69766 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
69767 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
69768 if( pLock->pBtree==pBtree
69769 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
69770 && pLock->eLock>=eLockType
69771 ){
69772 return 1;
69773 }
69774 }
69775
69776 /* Failed to find the required lock. */
69777 return 0;
69778}
69779#endif /* SQLITE_DEBUG */
69780
69781#ifdef SQLITE_DEBUG
69782/*
69783**** This function may be used as part of assert() statements only. ****
69784**
69785** Return true if it would be illegal for pBtree to write into the
69786** table or index rooted at iRoot because other shared connections are
69787** simultaneously reading that same table or index.
69788**
69789** It is illegal for pBtree to write if some other Btree object that
69790** shares the same BtShared object is currently reading or writing
69791** the iRoot table. Except, if the other Btree object has the
69792** read-uncommitted flag set, then it is OK for the other object to
69793** have a read cursor.
69794**
69795** For example, before writing to any part of the table or index
69796** rooted at page iRoot, one should call:
69797**
69798** assert( !hasReadConflicts(pBtree, iRoot) );
69799*/
69800static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
69801 BtCursor *p;
69802 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69803 if( p->pgnoRoot==iRoot
69804 && p->pBtree!=pBtree
69805 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
69806 ){
69807 return 1;
69808 }
69809 }
69810 return 0;
69811}
69812#endif /* #ifdef SQLITE_DEBUG */
69813
69814/*
69815** Query to see if Btree handle p may obtain a lock of type eLock
69816** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
69817** SQLITE_OK if the lock may be obtained (by calling
69818** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
69819*/
69820static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
69821 BtShared *pBt = p->pBt;
69822 BtLock *pIter;
69823
69824 assert( sqlite3BtreeHoldsMutex(p) );
69825 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
69826 assert( p->db!=0 );
69827 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
69828
69829 /* If requesting a write-lock, then the Btree must have an open write
69830 ** transaction on this file. And, obviously, for this to be so there
69831 ** must be an open write transaction on the file itself.
69832 */
69833 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
69834 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
69835
69836 /* This routine is a no-op if the shared-cache is not enabled */
69837 if( !p->sharable ){
69838 return SQLITE_OK;
69839 }
69840
69841 /* If some other connection is holding an exclusive lock, the
69842 ** requested lock may not be obtained.
69843 */
69844 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
69845 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
69846 return SQLITE_LOCKED_SHAREDCACHE;
69847 }
69848
69849 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69850 /* The condition (pIter->eLock!=eLock) in the following if(...)
69851 ** statement is a simplification of:
69852 **
69853 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
69854 **
69855 ** since we know that if eLock==WRITE_LOCK, then no other connection
69856 ** may hold a WRITE_LOCK on any table in this file (since there can
69857 ** only be a single writer).
69858 */
69859 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
69860 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
69861 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
69862 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
69863 if( eLock==WRITE_LOCK ){
69864 assert( p==pBt->pWriter );
69865 pBt->btsFlags |= BTS_PENDING;
69866 }
69867 return SQLITE_LOCKED_SHAREDCACHE;
69868 }
69869 }
69870 return SQLITE_OK;
69871}
69872#endif /* !SQLITE_OMIT_SHARED_CACHE */
69873
69874#ifndef SQLITE_OMIT_SHARED_CACHE
69875/*
69876** Add a lock on the table with root-page iTable to the shared-btree used
69877** by Btree handle p. Parameter eLock must be either READ_LOCK or
69878** WRITE_LOCK.
69879**
69880** This function assumes the following:
69881**
69882** (a) The specified Btree object p is connected to a sharable
69883** database (one with the BtShared.sharable flag set), and
69884**
69885** (b) No other Btree objects hold a lock that conflicts
69886** with the requested lock (i.e. querySharedCacheTableLock() has
69887** already been called and returned SQLITE_OK).
69888**
69889** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
69890** is returned if a malloc attempt fails.
69891*/
69892static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
69893 BtShared *pBt = p->pBt;
69894 BtLock *pLock = 0;
69895 BtLock *pIter;
69896
69897 assert( sqlite3BtreeHoldsMutex(p) );
69898 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
69899 assert( p->db!=0 );
69900
69901 /* A connection with the read-uncommitted flag set will never try to
69902 ** obtain a read-lock using this function. The only read-lock obtained
69903 ** by a connection in read-uncommitted mode is on the sqlite_schema
69904 ** table, and that lock is obtained in BtreeBeginTrans(). */
69905 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
69906
69907 /* This function should only be called on a sharable b-tree after it
69908 ** has been determined that no other b-tree holds a conflicting lock. */
69909 assert( p->sharable );
69910 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
69911
69912 /* First search the list for an existing lock on this table. */
69913 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69914 if( pIter->iTable==iTable && pIter->pBtree==p ){
69915 pLock = pIter;
69916 break;
69917 }
69918 }
69919
69920 /* If the above search did not find a BtLock struct associating Btree p
69921 ** with table iTable, allocate one and link it into the list.
69922 */
69923 if( !pLock ){
69924 pLock = (BtLock *)sqlite3MallocZero(n: sizeof(BtLock));
69925 if( !pLock ){
69926 return SQLITE_NOMEM_BKPT;
69927 }
69928 pLock->iTable = iTable;
69929 pLock->pBtree = p;
69930 pLock->pNext = pBt->pLock;
69931 pBt->pLock = pLock;
69932 }
69933
69934 /* Set the BtLock.eLock variable to the maximum of the current lock
69935 ** and the requested lock. This means if a write-lock was already held
69936 ** and a read-lock requested, we don't incorrectly downgrade the lock.
69937 */
69938 assert( WRITE_LOCK>READ_LOCK );
69939 if( eLock>pLock->eLock ){
69940 pLock->eLock = eLock;
69941 }
69942
69943 return SQLITE_OK;
69944}
69945#endif /* !SQLITE_OMIT_SHARED_CACHE */
69946
69947#ifndef SQLITE_OMIT_SHARED_CACHE
69948/*
69949** Release all the table locks (locks obtained via calls to
69950** the setSharedCacheTableLock() procedure) held by Btree object p.
69951**
69952** This function assumes that Btree p has an open read or write
69953** transaction. If it does not, then the BTS_PENDING flag
69954** may be incorrectly cleared.
69955*/
69956static void clearAllSharedCacheTableLocks(Btree *p){
69957 BtShared *pBt = p->pBt;
69958 BtLock **ppIter = &pBt->pLock;
69959
69960 assert( sqlite3BtreeHoldsMutex(p) );
69961 assert( p->sharable || 0==*ppIter );
69962 assert( p->inTrans>0 );
69963
69964 while( *ppIter ){
69965 BtLock *pLock = *ppIter;
69966 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
69967 assert( pLock->pBtree->inTrans>=pLock->eLock );
69968 if( pLock->pBtree==p ){
69969 *ppIter = pLock->pNext;
69970 assert( pLock->iTable!=1 || pLock==&p->lock );
69971 if( pLock->iTable!=1 ){
69972 sqlite3_free(p: pLock);
69973 }
69974 }else{
69975 ppIter = &pLock->pNext;
69976 }
69977 }
69978
69979 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
69980 if( pBt->pWriter==p ){
69981 pBt->pWriter = 0;
69982 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69983 }else if( pBt->nTransaction==2 ){
69984 /* This function is called when Btree p is concluding its
69985 ** transaction. If there currently exists a writer, and p is not
69986 ** that writer, then the number of locks held by connections other
69987 ** than the writer must be about to drop to zero. In this case
69988 ** set the BTS_PENDING flag to 0.
69989 **
69990 ** If there is not currently a writer, then BTS_PENDING must
69991 ** be zero already. So this next line is harmless in that case.
69992 */
69993 pBt->btsFlags &= ~BTS_PENDING;
69994 }
69995}
69996
69997/*
69998** This function changes all write-locks held by Btree p into read-locks.
69999*/
70000static void downgradeAllSharedCacheTableLocks(Btree *p){
70001 BtShared *pBt = p->pBt;
70002 if( pBt->pWriter==p ){
70003 BtLock *pLock;
70004 pBt->pWriter = 0;
70005 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
70006 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
70007 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
70008 pLock->eLock = READ_LOCK;
70009 }
70010 }
70011}
70012
70013#endif /* SQLITE_OMIT_SHARED_CACHE */
70014
70015static void releasePage(MemPage *pPage); /* Forward reference */
70016static void releasePageOne(MemPage *pPage); /* Forward reference */
70017static void releasePageNotNull(MemPage *pPage); /* Forward reference */
70018
70019/*
70020***** This routine is used inside of assert() only ****
70021**
70022** Verify that the cursor holds the mutex on its BtShared
70023*/
70024#ifdef SQLITE_DEBUG
70025static int cursorHoldsMutex(BtCursor *p){
70026 return sqlite3_mutex_held(p->pBt->mutex);
70027}
70028
70029/* Verify that the cursor and the BtShared agree about what is the current
70030** database connetion. This is important in shared-cache mode. If the database
70031** connection pointers get out-of-sync, it is possible for routines like
70032** btreeInitPage() to reference an stale connection pointer that references a
70033** a connection that has already closed. This routine is used inside assert()
70034** statements only and for the purpose of double-checking that the btree code
70035** does keep the database connection pointers up-to-date.
70036*/
70037static int cursorOwnsBtShared(BtCursor *p){
70038 assert( cursorHoldsMutex(p) );
70039 return (p->pBtree->db==p->pBt->db);
70040}
70041#endif
70042
70043/*
70044** Invalidate the overflow cache of the cursor passed as the first argument.
70045** on the shared btree structure pBt.
70046*/
70047#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
70048
70049/*
70050** Invalidate the overflow page-list cache for all cursors opened
70051** on the shared btree structure pBt.
70052*/
70053static void invalidateAllOverflowCache(BtShared *pBt){
70054 BtCursor *p;
70055 assert( sqlite3_mutex_held(pBt->mutex) );
70056 for(p=pBt->pCursor; p; p=p->pNext){
70057 invalidateOverflowCache(p);
70058 }
70059}
70060
70061#ifndef SQLITE_OMIT_INCRBLOB
70062/*
70063** This function is called before modifying the contents of a table
70064** to invalidate any incrblob cursors that are open on the
70065** row or one of the rows being modified.
70066**
70067** If argument isClearTable is true, then the entire contents of the
70068** table is about to be deleted. In this case invalidate all incrblob
70069** cursors open on any row within the table with root-page pgnoRoot.
70070**
70071** Otherwise, if argument isClearTable is false, then the row with
70072** rowid iRow is being replaced or deleted. In this case invalidate
70073** only those incrblob cursors open on that specific row.
70074*/
70075static void invalidateIncrblobCursors(
70076 Btree *pBtree, /* The database file to check */
70077 Pgno pgnoRoot, /* The table that might be changing */
70078 i64 iRow, /* The rowid that might be changing */
70079 int isClearTable /* True if all rows are being deleted */
70080){
70081 BtCursor *p;
70082 assert( pBtree->hasIncrblobCur );
70083 assert( sqlite3BtreeHoldsMutex(pBtree) );
70084 pBtree->hasIncrblobCur = 0;
70085 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
70086 if( (p->curFlags & BTCF_Incrblob)!=0 ){
70087 pBtree->hasIncrblobCur = 1;
70088 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
70089 p->eState = CURSOR_INVALID;
70090 }
70091 }
70092 }
70093}
70094
70095#else
70096 /* Stub function when INCRBLOB is omitted */
70097 #define invalidateIncrblobCursors(w,x,y,z)
70098#endif /* SQLITE_OMIT_INCRBLOB */
70099
70100/*
70101** Set bit pgno of the BtShared.pHasContent bitvec. This is called
70102** when a page that previously contained data becomes a free-list leaf
70103** page.
70104**
70105** The BtShared.pHasContent bitvec exists to work around an obscure
70106** bug caused by the interaction of two useful IO optimizations surrounding
70107** free-list leaf pages:
70108**
70109** 1) When all data is deleted from a page and the page becomes
70110** a free-list leaf page, the page is not written to the database
70111** (as free-list leaf pages contain no meaningful data). Sometimes
70112** such a page is not even journalled (as it will not be modified,
70113** why bother journalling it?).
70114**
70115** 2) When a free-list leaf page is reused, its content is not read
70116** from the database or written to the journal file (why should it
70117** be, if it is not at all meaningful?).
70118**
70119** By themselves, these optimizations work fine and provide a handy
70120** performance boost to bulk delete or insert operations. However, if
70121** a page is moved to the free-list and then reused within the same
70122** transaction, a problem comes up. If the page is not journalled when
70123** it is moved to the free-list and it is also not journalled when it
70124** is extracted from the free-list and reused, then the original data
70125** may be lost. In the event of a rollback, it may not be possible
70126** to restore the database to its original configuration.
70127**
70128** The solution is the BtShared.pHasContent bitvec. Whenever a page is
70129** moved to become a free-list leaf page, the corresponding bit is
70130** set in the bitvec. Whenever a leaf page is extracted from the free-list,
70131** optimization 2 above is omitted if the corresponding bit is already
70132** set in BtShared.pHasContent. The contents of the bitvec are cleared
70133** at the end of every transaction.
70134*/
70135static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
70136 int rc = SQLITE_OK;
70137 if( !pBt->pHasContent ){
70138 assert( pgno<=pBt->nPage );
70139 pBt->pHasContent = sqlite3BitvecCreate(iSize: pBt->nPage);
70140 if( !pBt->pHasContent ){
70141 rc = SQLITE_NOMEM_BKPT;
70142 }
70143 }
70144 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(p: pBt->pHasContent) ){
70145 rc = sqlite3BitvecSet(p: pBt->pHasContent, i: pgno);
70146 }
70147 return rc;
70148}
70149
70150/*
70151** Query the BtShared.pHasContent vector.
70152**
70153** This function is called when a free-list leaf page is removed from the
70154** free-list for reuse. It returns false if it is safe to retrieve the
70155** page from the pager layer with the 'no-content' flag set. True otherwise.
70156*/
70157static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
70158 Bitvec *p = pBt->pHasContent;
70159 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, i: pgno));
70160}
70161
70162/*
70163** Clear (destroy) the BtShared.pHasContent bitvec. This should be
70164** invoked at the conclusion of each write-transaction.
70165*/
70166static void btreeClearHasContent(BtShared *pBt){
70167 sqlite3BitvecDestroy(p: pBt->pHasContent);
70168 pBt->pHasContent = 0;
70169}
70170
70171/*
70172** Release all of the apPage[] pages for a cursor.
70173*/
70174static void btreeReleaseAllCursorPages(BtCursor *pCur){
70175 int i;
70176 if( pCur->iPage>=0 ){
70177 for(i=0; i<pCur->iPage; i++){
70178 releasePageNotNull(pPage: pCur->apPage[i]);
70179 }
70180 releasePageNotNull(pPage: pCur->pPage);
70181 pCur->iPage = -1;
70182 }
70183}
70184
70185/*
70186** The cursor passed as the only argument must point to a valid entry
70187** when this function is called (i.e. have eState==CURSOR_VALID). This
70188** function saves the current cursor key in variables pCur->nKey and
70189** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
70190** code otherwise.
70191**
70192** If the cursor is open on an intkey table, then the integer key
70193** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
70194** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
70195** set to point to a malloced buffer pCur->nKey bytes in size containing
70196** the key.
70197*/
70198static int saveCursorKey(BtCursor *pCur){
70199 int rc = SQLITE_OK;
70200 assert( CURSOR_VALID==pCur->eState );
70201 assert( 0==pCur->pKey );
70202 assert( cursorHoldsMutex(pCur) );
70203
70204 if( pCur->curIntKey ){
70205 /* Only the rowid is required for a table btree */
70206 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
70207 }else{
70208 /* For an index btree, save the complete key content. It is possible
70209 ** that the current key is corrupt. In that case, it is possible that
70210 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
70211 ** up to the size of 1 varint plus 1 8-byte value when the cursor
70212 ** position is restored. Hence the 17 bytes of padding allocated
70213 ** below. */
70214 void *pKey;
70215 pCur->nKey = sqlite3BtreePayloadSize(pCur);
70216 pKey = sqlite3Malloc( n: pCur->nKey + 9 + 8 );
70217 if( pKey ){
70218 rc = sqlite3BtreePayload(pCur, offset: 0, amt: (int)pCur->nKey, pKey);
70219 if( rc==SQLITE_OK ){
70220 memset(s: ((u8*)pKey)+pCur->nKey, c: 0, n: 9+8);
70221 pCur->pKey = pKey;
70222 }else{
70223 sqlite3_free(p: pKey);
70224 }
70225 }else{
70226 rc = SQLITE_NOMEM_BKPT;
70227 }
70228 }
70229 assert( !pCur->curIntKey || !pCur->pKey );
70230 return rc;
70231}
70232
70233/*
70234** Save the current cursor position in the variables BtCursor.nKey
70235** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
70236**
70237** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
70238** prior to calling this routine.
70239*/
70240static int saveCursorPosition(BtCursor *pCur){
70241 int rc;
70242
70243 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
70244 assert( 0==pCur->pKey );
70245 assert( cursorHoldsMutex(pCur) );
70246
70247 if( pCur->curFlags & BTCF_Pinned ){
70248 return SQLITE_CONSTRAINT_PINNED;
70249 }
70250 if( pCur->eState==CURSOR_SKIPNEXT ){
70251 pCur->eState = CURSOR_VALID;
70252 }else{
70253 pCur->skipNext = 0;
70254 }
70255
70256 rc = saveCursorKey(pCur);
70257 if( rc==SQLITE_OK ){
70258 btreeReleaseAllCursorPages(pCur);
70259 pCur->eState = CURSOR_REQUIRESEEK;
70260 }
70261
70262 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
70263 return rc;
70264}
70265
70266/* Forward reference */
70267static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
70268
70269/*
70270** Save the positions of all cursors (except pExcept) that are open on
70271** the table with root-page iRoot. "Saving the cursor position" means that
70272** the location in the btree is remembered in such a way that it can be
70273** moved back to the same spot after the btree has been modified. This
70274** routine is called just before cursor pExcept is used to modify the
70275** table, for example in BtreeDelete() or BtreeInsert().
70276**
70277** If there are two or more cursors on the same btree, then all such
70278** cursors should have their BTCF_Multiple flag set. The btreeCursor()
70279** routine enforces that rule. This routine only needs to be called in
70280** the uncommon case when pExpect has the BTCF_Multiple flag set.
70281**
70282** If pExpect!=NULL and if no other cursors are found on the same root-page,
70283** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
70284** pointless call to this routine.
70285**
70286** Implementation note: This routine merely checks to see if any cursors
70287** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
70288** event that cursors are in need to being saved.
70289*/
70290static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
70291 BtCursor *p;
70292 assert( sqlite3_mutex_held(pBt->mutex) );
70293 assert( pExcept==0 || pExcept->pBt==pBt );
70294 for(p=pBt->pCursor; p; p=p->pNext){
70295 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
70296 }
70297 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
70298 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
70299 return SQLITE_OK;
70300}
70301
70302/* This helper routine to saveAllCursors does the actual work of saving
70303** the cursors if and when a cursor is found that actually requires saving.
70304** The common case is that no cursors need to be saved, so this routine is
70305** broken out from its caller to avoid unnecessary stack pointer movement.
70306*/
70307static int SQLITE_NOINLINE saveCursorsOnList(
70308 BtCursor *p, /* The first cursor that needs saving */
70309 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
70310 BtCursor *pExcept /* Do not save this cursor */
70311){
70312 do{
70313 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
70314 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
70315 int rc = saveCursorPosition(pCur: p);
70316 if( SQLITE_OK!=rc ){
70317 return rc;
70318 }
70319 }else{
70320 testcase( p->iPage>=0 );
70321 btreeReleaseAllCursorPages(pCur: p);
70322 }
70323 }
70324 p = p->pNext;
70325 }while( p );
70326 return SQLITE_OK;
70327}
70328
70329/*
70330** Clear the current cursor position.
70331*/
70332SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
70333 assert( cursorHoldsMutex(pCur) );
70334 sqlite3_free(p: pCur->pKey);
70335 pCur->pKey = 0;
70336 pCur->eState = CURSOR_INVALID;
70337}
70338
70339/*
70340** In this version of BtreeMoveto, pKey is a packed index record
70341** such as is generated by the OP_MakeRecord opcode. Unpack the
70342** record and then call sqlite3BtreeIndexMoveto() to do the work.
70343*/
70344static int btreeMoveto(
70345 BtCursor *pCur, /* Cursor open on the btree to be searched */
70346 const void *pKey, /* Packed key if the btree is an index */
70347 i64 nKey, /* Integer key for tables. Size of pKey for indices */
70348 int bias, /* Bias search to the high end */
70349 int *pRes /* Write search results here */
70350){
70351 int rc; /* Status code */
70352 UnpackedRecord *pIdxKey; /* Unpacked index key */
70353
70354 if( pKey ){
70355 KeyInfo *pKeyInfo = pCur->pKeyInfo;
70356 assert( nKey==(i64)(int)nKey );
70357 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
70358 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
70359 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
70360 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
70361 rc = SQLITE_CORRUPT_BKPT;
70362 }else{
70363 rc = sqlite3BtreeIndexMoveto(pCur, pUnKey: pIdxKey, pRes);
70364 }
70365 sqlite3DbFree(db: pCur->pKeyInfo->db, p: pIdxKey);
70366 }else{
70367 pIdxKey = 0;
70368 rc = sqlite3BtreeTableMoveto(pCur, intKey: nKey, bias, pRes);
70369 }
70370 return rc;
70371}
70372
70373/*
70374** Restore the cursor to the position it was in (or as close to as possible)
70375** when saveCursorPosition() was called. Note that this call deletes the
70376** saved position info stored by saveCursorPosition(), so there can be
70377** at most one effective restoreCursorPosition() call after each
70378** saveCursorPosition().
70379*/
70380static int btreeRestoreCursorPosition(BtCursor *pCur){
70381 int rc;
70382 int skipNext = 0;
70383 assert( cursorOwnsBtShared(pCur) );
70384 assert( pCur->eState>=CURSOR_REQUIRESEEK );
70385 if( pCur->eState==CURSOR_FAULT ){
70386 return pCur->skipNext;
70387 }
70388 pCur->eState = CURSOR_INVALID;
70389 if( sqlite3FaultSim(iTest: 410) ){
70390 rc = SQLITE_IOERR;
70391 }else{
70392 rc = btreeMoveto(pCur, pKey: pCur->pKey, nKey: pCur->nKey, bias: 0, pRes: &skipNext);
70393 }
70394 if( rc==SQLITE_OK ){
70395 sqlite3_free(p: pCur->pKey);
70396 pCur->pKey = 0;
70397 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
70398 if( skipNext ) pCur->skipNext = skipNext;
70399 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
70400 pCur->eState = CURSOR_SKIPNEXT;
70401 }
70402 }
70403 return rc;
70404}
70405
70406#define restoreCursorPosition(p) \
70407 (p->eState>=CURSOR_REQUIRESEEK ? \
70408 btreeRestoreCursorPosition(p) : \
70409 SQLITE_OK)
70410
70411/*
70412** Determine whether or not a cursor has moved from the position where
70413** it was last placed, or has been invalidated for any other reason.
70414** Cursors can move when the row they are pointing at is deleted out
70415** from under them, for example. Cursor might also move if a btree
70416** is rebalanced.
70417**
70418** Calling this routine with a NULL cursor pointer returns false.
70419**
70420** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
70421** back to where it ought to be if this routine returns true.
70422*/
70423SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
70424 assert( EIGHT_BYTE_ALIGNMENT(pCur)
70425 || pCur==sqlite3BtreeFakeValidCursor() );
70426 assert( offsetof(BtCursor, eState)==0 );
70427 assert( sizeof(pCur->eState)==1 );
70428 return CURSOR_VALID != *(u8*)pCur;
70429}
70430
70431/*
70432** Return a pointer to a fake BtCursor object that will always answer
70433** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
70434** cursor returned must not be used with any other Btree interface.
70435*/
70436SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
70437 static u8 fakeCursor = CURSOR_VALID;
70438 assert( offsetof(BtCursor, eState)==0 );
70439 return (BtCursor*)&fakeCursor;
70440}
70441
70442/*
70443** This routine restores a cursor back to its original position after it
70444** has been moved by some outside activity (such as a btree rebalance or
70445** a row having been deleted out from under the cursor).
70446**
70447** On success, the *pDifferentRow parameter is false if the cursor is left
70448** pointing at exactly the same row. *pDifferntRow is the row the cursor
70449** was pointing to has been deleted, forcing the cursor to point to some
70450** nearby row.
70451**
70452** This routine should only be called for a cursor that just returned
70453** TRUE from sqlite3BtreeCursorHasMoved().
70454*/
70455SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
70456 int rc;
70457
70458 assert( pCur!=0 );
70459 assert( pCur->eState!=CURSOR_VALID );
70460 rc = restoreCursorPosition(pCur);
70461 if( rc ){
70462 *pDifferentRow = 1;
70463 return rc;
70464 }
70465 if( pCur->eState!=CURSOR_VALID ){
70466 *pDifferentRow = 1;
70467 }else{
70468 *pDifferentRow = 0;
70469 }
70470 return SQLITE_OK;
70471}
70472
70473#ifdef SQLITE_ENABLE_CURSOR_HINTS
70474/*
70475** Provide hints to the cursor. The particular hint given (and the type
70476** and number of the varargs parameters) is determined by the eHintType
70477** parameter. See the definitions of the BTREE_HINT_* macros for details.
70478*/
70479SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
70480 /* Used only by system that substitute their own storage engine */
70481#ifdef SQLITE_DEBUG
70482 if( ALWAYS(eHintType==BTREE_HINT_RANGE) ){
70483 va_list ap;
70484 Expr *pExpr;
70485 Walker w;
70486 memset(&w, 0, sizeof(w));
70487 w.xExprCallback = sqlite3CursorRangeHintExprCheck;
70488 va_start(ap, eHintType);
70489 pExpr = va_arg(ap, Expr*);
70490 w.u.aMem = va_arg(ap, Mem*);
70491 va_end(ap);
70492 assert( pExpr!=0 );
70493 assert( w.u.aMem!=0 );
70494 sqlite3WalkExpr(&w, pExpr);
70495 }
70496#endif /* SQLITE_DEBUG */
70497}
70498#endif /* SQLITE_ENABLE_CURSOR_HINTS */
70499
70500
70501/*
70502** Provide flag hints to the cursor.
70503*/
70504SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
70505 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
70506 pCur->hints = x;
70507}
70508
70509
70510#ifndef SQLITE_OMIT_AUTOVACUUM
70511/*
70512** Given a page number of a regular database page, return the page
70513** number for the pointer-map page that contains the entry for the
70514** input page number.
70515**
70516** Return 0 (not a valid page) for pgno==1 since there is
70517** no pointer map associated with page 1. The integrity_check logic
70518** requires that ptrmapPageno(*,1)!=1.
70519*/
70520static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
70521 int nPagesPerMapPage;
70522 Pgno iPtrMap, ret;
70523 assert( sqlite3_mutex_held(pBt->mutex) );
70524 if( pgno<2 ) return 0;
70525 nPagesPerMapPage = (pBt->usableSize/5)+1;
70526 iPtrMap = (pgno-2)/nPagesPerMapPage;
70527 ret = (iPtrMap*nPagesPerMapPage) + 2;
70528 if( ret==PENDING_BYTE_PAGE(pBt) ){
70529 ret++;
70530 }
70531 return ret;
70532}
70533
70534/*
70535** Write an entry into the pointer map.
70536**
70537** This routine updates the pointer map entry for page number 'key'
70538** so that it maps to type 'eType' and parent page number 'pgno'.
70539**
70540** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
70541** a no-op. If an error occurs, the appropriate error code is written
70542** into *pRC.
70543*/
70544static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
70545 DbPage *pDbPage; /* The pointer map page */
70546 u8 *pPtrmap; /* The pointer map data */
70547 Pgno iPtrmap; /* The pointer map page number */
70548 int offset; /* Offset in pointer map page */
70549 int rc; /* Return code from subfunctions */
70550
70551 if( *pRC ) return;
70552
70553 assert( sqlite3_mutex_held(pBt->mutex) );
70554 /* The super-journal page number must never be used as a pointer map page */
70555 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
70556
70557 assert( pBt->autoVacuum );
70558 if( key==0 ){
70559 *pRC = SQLITE_CORRUPT_BKPT;
70560 return;
70561 }
70562 iPtrmap = PTRMAP_PAGENO(pBt, key);
70563 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno: iPtrmap, ppPage: &pDbPage, flags: 0);
70564 if( rc!=SQLITE_OK ){
70565 *pRC = rc;
70566 return;
70567 }
70568 if( ((char*)sqlite3PagerGetExtra(pPg: pDbPage))[0]!=0 ){
70569 /* The first byte of the extra data is the MemPage.isInit byte.
70570 ** If that byte is set, it means this page is also being used
70571 ** as a btree page. */
70572 *pRC = SQLITE_CORRUPT_BKPT;
70573 goto ptrmap_exit;
70574 }
70575 offset = PTRMAP_PTROFFSET(iPtrmap, key);
70576 if( offset<0 ){
70577 *pRC = SQLITE_CORRUPT_BKPT;
70578 goto ptrmap_exit;
70579 }
70580 assert( offset <= (int)pBt->usableSize-5 );
70581 pPtrmap = (u8 *)sqlite3PagerGetData(pPg: pDbPage);
70582
70583 if( eType!=pPtrmap[offset] || get4byte(p: &pPtrmap[offset+1])!=parent ){
70584 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
70585 *pRC= rc = sqlite3PagerWrite(pPg: pDbPage);
70586 if( rc==SQLITE_OK ){
70587 pPtrmap[offset] = eType;
70588 put4byte(p: &pPtrmap[offset+1], v: parent);
70589 }
70590 }
70591
70592ptrmap_exit:
70593 sqlite3PagerUnref(pPg: pDbPage);
70594}
70595
70596/*
70597** Read an entry from the pointer map.
70598**
70599** This routine retrieves the pointer map entry for page 'key', writing
70600** the type and parent page number to *pEType and *pPgno respectively.
70601** An error code is returned if something goes wrong, otherwise SQLITE_OK.
70602*/
70603static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
70604 DbPage *pDbPage; /* The pointer map page */
70605 int iPtrmap; /* Pointer map page index */
70606 u8 *pPtrmap; /* Pointer map page data */
70607 int offset; /* Offset of entry in pointer map */
70608 int rc;
70609
70610 assert( sqlite3_mutex_held(pBt->mutex) );
70611
70612 iPtrmap = PTRMAP_PAGENO(pBt, key);
70613 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno: iPtrmap, ppPage: &pDbPage, flags: 0);
70614 if( rc!=0 ){
70615 return rc;
70616 }
70617 pPtrmap = (u8 *)sqlite3PagerGetData(pPg: pDbPage);
70618
70619 offset = PTRMAP_PTROFFSET(iPtrmap, key);
70620 if( offset<0 ){
70621 sqlite3PagerUnref(pPg: pDbPage);
70622 return SQLITE_CORRUPT_BKPT;
70623 }
70624 assert( offset <= (int)pBt->usableSize-5 );
70625 assert( pEType!=0 );
70626 *pEType = pPtrmap[offset];
70627 if( pPgno ) *pPgno = get4byte(p: &pPtrmap[offset+1]);
70628
70629 sqlite3PagerUnref(pPg: pDbPage);
70630 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
70631 return SQLITE_OK;
70632}
70633
70634#else /* if defined SQLITE_OMIT_AUTOVACUUM */
70635 #define ptrmapPut(w,x,y,z,rc)
70636 #define ptrmapGet(w,x,y,z) SQLITE_OK
70637 #define ptrmapPutOvflPtr(x, y, z, rc)
70638#endif
70639
70640/*
70641** Given a btree page and a cell index (0 means the first cell on
70642** the page, 1 means the second cell, and so forth) return a pointer
70643** to the cell content.
70644**
70645** findCellPastPtr() does the same except it skips past the initial
70646** 4-byte child pointer found on interior pages, if there is one.
70647**
70648** This routine works only for pages that do not contain overflow cells.
70649*/
70650#define findCell(P,I) \
70651 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
70652#define findCellPastPtr(P,I) \
70653 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
70654
70655
70656/*
70657** This is common tail processing for btreeParseCellPtr() and
70658** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
70659** on a single B-tree page. Make necessary adjustments to the CellInfo
70660** structure.
70661*/
70662static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
70663 MemPage *pPage, /* Page containing the cell */
70664 u8 *pCell, /* Pointer to the cell text. */
70665 CellInfo *pInfo /* Fill in this structure */
70666){
70667 /* If the payload will not fit completely on the local page, we have
70668 ** to decide how much to store locally and how much to spill onto
70669 ** overflow pages. The strategy is to minimize the amount of unused
70670 ** space on overflow pages while keeping the amount of local storage
70671 ** in between minLocal and maxLocal.
70672 **
70673 ** Warning: changing the way overflow payload is distributed in any
70674 ** way will result in an incompatible file format.
70675 */
70676 int minLocal; /* Minimum amount of payload held locally */
70677 int maxLocal; /* Maximum amount of payload held locally */
70678 int surplus; /* Overflow payload available for local storage */
70679
70680 minLocal = pPage->minLocal;
70681 maxLocal = pPage->maxLocal;
70682 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
70683 testcase( surplus==maxLocal );
70684 testcase( surplus==maxLocal+1 );
70685 if( surplus <= maxLocal ){
70686 pInfo->nLocal = (u16)surplus;
70687 }else{
70688 pInfo->nLocal = (u16)minLocal;
70689 }
70690 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
70691}
70692
70693/*
70694** Given a record with nPayload bytes of payload stored within btree
70695** page pPage, return the number of bytes of payload stored locally.
70696*/
70697static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
70698 int maxLocal; /* Maximum amount of payload held locally */
70699 maxLocal = pPage->maxLocal;
70700 if( nPayload<=maxLocal ){
70701 return nPayload;
70702 }else{
70703 int minLocal; /* Minimum amount of payload held locally */
70704 int surplus; /* Overflow payload available for local storage */
70705 minLocal = pPage->minLocal;
70706 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
70707 return ( surplus <= maxLocal ) ? surplus : minLocal;
70708 }
70709}
70710
70711/*
70712** The following routines are implementations of the MemPage.xParseCell()
70713** method.
70714**
70715** Parse a cell content block and fill in the CellInfo structure.
70716**
70717** btreeParseCellPtr() => table btree leaf nodes
70718** btreeParseCellNoPayload() => table btree internal nodes
70719** btreeParseCellPtrIndex() => index btree nodes
70720**
70721** There is also a wrapper function btreeParseCell() that works for
70722** all MemPage types and that references the cell by index rather than
70723** by pointer.
70724*/
70725static void btreeParseCellPtrNoPayload(
70726 MemPage *pPage, /* Page containing the cell */
70727 u8 *pCell, /* Pointer to the cell text. */
70728 CellInfo *pInfo /* Fill in this structure */
70729){
70730 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70731 assert( pPage->leaf==0 );
70732 assert( pPage->childPtrSize==4 );
70733#ifndef SQLITE_DEBUG
70734 UNUSED_PARAMETER(pPage);
70735#endif
70736 pInfo->nSize = 4 + getVarint(p: &pCell[4], v: (u64*)&pInfo->nKey);
70737 pInfo->nPayload = 0;
70738 pInfo->nLocal = 0;
70739 pInfo->pPayload = 0;
70740 return;
70741}
70742static void btreeParseCellPtr(
70743 MemPage *pPage, /* Page containing the cell */
70744 u8 *pCell, /* Pointer to the cell text. */
70745 CellInfo *pInfo /* Fill in this structure */
70746){
70747 u8 *pIter; /* For scanning through pCell */
70748 u32 nPayload; /* Number of bytes of cell payload */
70749 u64 iKey; /* Extracted Key value */
70750
70751 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70752 assert( pPage->leaf==0 || pPage->leaf==1 );
70753 assert( pPage->intKeyLeaf );
70754 assert( pPage->childPtrSize==0 );
70755 pIter = pCell;
70756
70757 /* The next block of code is equivalent to:
70758 **
70759 ** pIter += getVarint32(pIter, nPayload);
70760 **
70761 ** The code is inlined to avoid a function call.
70762 */
70763 nPayload = *pIter;
70764 if( nPayload>=0x80 ){
70765 u8 *pEnd = &pIter[8];
70766 nPayload &= 0x7f;
70767 do{
70768 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
70769 }while( (*pIter)>=0x80 && pIter<pEnd );
70770 }
70771 pIter++;
70772
70773 /* The next block of code is equivalent to:
70774 **
70775 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
70776 **
70777 ** The code is inlined and the loop is unrolled for performance.
70778 ** This routine is a high-runner.
70779 */
70780 iKey = *pIter;
70781 if( iKey>=0x80 ){
70782 u8 x;
70783 iKey = (iKey<<7) ^ (x = *++pIter);
70784 if( x>=0x80 ){
70785 iKey = (iKey<<7) ^ (x = *++pIter);
70786 if( x>=0x80 ){
70787 iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter);
70788 if( x>=0x80 ){
70789 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
70790 if( x>=0x80 ){
70791 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
70792 if( x>=0x80 ){
70793 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
70794 if( x>=0x80 ){
70795 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
70796 if( x>=0x80 ){
70797 iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter);
70798 }
70799 }
70800 }
70801 }
70802 }
70803 }else{
70804 iKey ^= 0x204000;
70805 }
70806 }else{
70807 iKey ^= 0x4000;
70808 }
70809 }
70810 pIter++;
70811
70812 pInfo->nKey = *(i64*)&iKey;
70813 pInfo->nPayload = nPayload;
70814 pInfo->pPayload = pIter;
70815 testcase( nPayload==pPage->maxLocal );
70816 testcase( nPayload==(u32)pPage->maxLocal+1 );
70817 if( nPayload<=pPage->maxLocal ){
70818 /* This is the (easy) common case where the entire payload fits
70819 ** on the local page. No overflow is required.
70820 */
70821 pInfo->nSize = nPayload + (u16)(pIter - pCell);
70822 if( pInfo->nSize<4 ) pInfo->nSize = 4;
70823 pInfo->nLocal = (u16)nPayload;
70824 }else{
70825 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
70826 }
70827}
70828static void btreeParseCellPtrIndex(
70829 MemPage *pPage, /* Page containing the cell */
70830 u8 *pCell, /* Pointer to the cell text. */
70831 CellInfo *pInfo /* Fill in this structure */
70832){
70833 u8 *pIter; /* For scanning through pCell */
70834 u32 nPayload; /* Number of bytes of cell payload */
70835
70836 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70837 assert( pPage->leaf==0 || pPage->leaf==1 );
70838 assert( pPage->intKeyLeaf==0 );
70839 pIter = pCell + pPage->childPtrSize;
70840 nPayload = *pIter;
70841 if( nPayload>=0x80 ){
70842 u8 *pEnd = &pIter[8];
70843 nPayload &= 0x7f;
70844 do{
70845 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
70846 }while( *(pIter)>=0x80 && pIter<pEnd );
70847 }
70848 pIter++;
70849 pInfo->nKey = nPayload;
70850 pInfo->nPayload = nPayload;
70851 pInfo->pPayload = pIter;
70852 testcase( nPayload==pPage->maxLocal );
70853 testcase( nPayload==(u32)pPage->maxLocal+1 );
70854 if( nPayload<=pPage->maxLocal ){
70855 /* This is the (easy) common case where the entire payload fits
70856 ** on the local page. No overflow is required.
70857 */
70858 pInfo->nSize = nPayload + (u16)(pIter - pCell);
70859 if( pInfo->nSize<4 ) pInfo->nSize = 4;
70860 pInfo->nLocal = (u16)nPayload;
70861 }else{
70862 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
70863 }
70864}
70865static void btreeParseCell(
70866 MemPage *pPage, /* Page containing the cell */
70867 int iCell, /* The cell index. First cell is 0 */
70868 CellInfo *pInfo /* Fill in this structure */
70869){
70870 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
70871}
70872
70873/*
70874** The following routines are implementations of the MemPage.xCellSize
70875** method.
70876**
70877** Compute the total number of bytes that a Cell needs in the cell
70878** data area of the btree-page. The return number includes the cell
70879** data header and the local payload, but not any overflow page or
70880** the space used by the cell pointer.
70881**
70882** cellSizePtrNoPayload() => table internal nodes
70883** cellSizePtrTableLeaf() => table leaf nodes
70884** cellSizePtr() => index internal nodes
70885** cellSizeIdxLeaf() => index leaf nodes
70886*/
70887static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
70888 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
70889 u8 *pEnd; /* End mark for a varint */
70890 u32 nSize; /* Size value to return */
70891
70892#ifdef SQLITE_DEBUG
70893 /* The value returned by this function should always be the same as
70894 ** the (CellInfo.nSize) value found by doing a full parse of the
70895 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
70896 ** this function verifies that this invariant is not violated. */
70897 CellInfo debuginfo;
70898 pPage->xParseCell(pPage, pCell, &debuginfo);
70899#endif
70900
70901 assert( pPage->childPtrSize==4 );
70902 nSize = *pIter;
70903 if( nSize>=0x80 ){
70904 pEnd = &pIter[8];
70905 nSize &= 0x7f;
70906 do{
70907 nSize = (nSize<<7) | (*++pIter & 0x7f);
70908 }while( *(pIter)>=0x80 && pIter<pEnd );
70909 }
70910 pIter++;
70911 testcase( nSize==pPage->maxLocal );
70912 testcase( nSize==(u32)pPage->maxLocal+1 );
70913 if( nSize<=pPage->maxLocal ){
70914 nSize += (u32)(pIter - pCell);
70915 assert( nSize>4 );
70916 }else{
70917 int minLocal = pPage->minLocal;
70918 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70919 testcase( nSize==pPage->maxLocal );
70920 testcase( nSize==(u32)pPage->maxLocal+1 );
70921 if( nSize>pPage->maxLocal ){
70922 nSize = minLocal;
70923 }
70924 nSize += 4 + (u16)(pIter - pCell);
70925 }
70926 assert( nSize==debuginfo.nSize || CORRUPT_DB );
70927 return (u16)nSize;
70928}
70929static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){
70930 u8 *pIter = pCell; /* For looping over bytes of pCell */
70931 u8 *pEnd; /* End mark for a varint */
70932 u32 nSize; /* Size value to return */
70933
70934#ifdef SQLITE_DEBUG
70935 /* The value returned by this function should always be the same as
70936 ** the (CellInfo.nSize) value found by doing a full parse of the
70937 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
70938 ** this function verifies that this invariant is not violated. */
70939 CellInfo debuginfo;
70940 pPage->xParseCell(pPage, pCell, &debuginfo);
70941#endif
70942
70943 assert( pPage->childPtrSize==0 );
70944 nSize = *pIter;
70945 if( nSize>=0x80 ){
70946 pEnd = &pIter[8];
70947 nSize &= 0x7f;
70948 do{
70949 nSize = (nSize<<7) | (*++pIter & 0x7f);
70950 }while( *(pIter)>=0x80 && pIter<pEnd );
70951 }
70952 pIter++;
70953 testcase( nSize==pPage->maxLocal );
70954 testcase( nSize==(u32)pPage->maxLocal+1 );
70955 if( nSize<=pPage->maxLocal ){
70956 nSize += (u32)(pIter - pCell);
70957 if( nSize<4 ) nSize = 4;
70958 }else{
70959 int minLocal = pPage->minLocal;
70960 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70961 testcase( nSize==pPage->maxLocal );
70962 testcase( nSize==(u32)pPage->maxLocal+1 );
70963 if( nSize>pPage->maxLocal ){
70964 nSize = minLocal;
70965 }
70966 nSize += 4 + (u16)(pIter - pCell);
70967 }
70968 assert( nSize==debuginfo.nSize || CORRUPT_DB );
70969 return (u16)nSize;
70970}
70971static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
70972 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
70973 u8 *pEnd; /* End mark for a varint */
70974
70975#ifdef SQLITE_DEBUG
70976 /* The value returned by this function should always be the same as
70977 ** the (CellInfo.nSize) value found by doing a full parse of the
70978 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
70979 ** this function verifies that this invariant is not violated. */
70980 CellInfo debuginfo;
70981 pPage->xParseCell(pPage, pCell, &debuginfo);
70982#else
70983 UNUSED_PARAMETER(pPage);
70984#endif
70985
70986 assert( pPage->childPtrSize==4 );
70987 pEnd = pIter + 9;
70988 while( (*pIter++)&0x80 && pIter<pEnd );
70989 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
70990 return (u16)(pIter - pCell);
70991}
70992static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
70993 u8 *pIter = pCell; /* For looping over bytes of pCell */
70994 u8 *pEnd; /* End mark for a varint */
70995 u32 nSize; /* Size value to return */
70996
70997#ifdef SQLITE_DEBUG
70998 /* The value returned by this function should always be the same as
70999 ** the (CellInfo.nSize) value found by doing a full parse of the
71000 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
71001 ** this function verifies that this invariant is not violated. */
71002 CellInfo debuginfo;
71003 pPage->xParseCell(pPage, pCell, &debuginfo);
71004#endif
71005
71006 nSize = *pIter;
71007 if( nSize>=0x80 ){
71008 pEnd = &pIter[8];
71009 nSize &= 0x7f;
71010 do{
71011 nSize = (nSize<<7) | (*++pIter & 0x7f);
71012 }while( *(pIter)>=0x80 && pIter<pEnd );
71013 }
71014 pIter++;
71015 /* pIter now points at the 64-bit integer key value, a variable length
71016 ** integer. The following block moves pIter to point at the first byte
71017 ** past the end of the key value. */
71018 if( (*pIter++)&0x80
71019 && (*pIter++)&0x80
71020 && (*pIter++)&0x80
71021 && (*pIter++)&0x80
71022 && (*pIter++)&0x80
71023 && (*pIter++)&0x80
71024 && (*pIter++)&0x80
71025 && (*pIter++)&0x80 ){ pIter++; }
71026 testcase( nSize==pPage->maxLocal );
71027 testcase( nSize==(u32)pPage->maxLocal+1 );
71028 if( nSize<=pPage->maxLocal ){
71029 nSize += (u32)(pIter - pCell);
71030 if( nSize<4 ) nSize = 4;
71031 }else{
71032 int minLocal = pPage->minLocal;
71033 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71034 testcase( nSize==pPage->maxLocal );
71035 testcase( nSize==(u32)pPage->maxLocal+1 );
71036 if( nSize>pPage->maxLocal ){
71037 nSize = minLocal;
71038 }
71039 nSize += 4 + (u16)(pIter - pCell);
71040 }
71041 assert( nSize==debuginfo.nSize || CORRUPT_DB );
71042 return (u16)nSize;
71043}
71044
71045
71046#ifdef SQLITE_DEBUG
71047/* This variation on cellSizePtr() is used inside of assert() statements
71048** only. */
71049static u16 cellSize(MemPage *pPage, int iCell){
71050 return pPage->xCellSize(pPage, findCell(pPage, iCell));
71051}
71052#endif
71053
71054#ifndef SQLITE_OMIT_AUTOVACUUM
71055/*
71056** The cell pCell is currently part of page pSrc but will ultimately be part
71057** of pPage. (pSrc and pPage are often the same.) If pCell contains a
71058** pointer to an overflow page, insert an entry into the pointer-map for
71059** the overflow page that will be valid after pCell has been moved to pPage.
71060*/
71061static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
71062 CellInfo info;
71063 if( *pRC ) return;
71064 assert( pCell!=0 );
71065 pPage->xParseCell(pPage, pCell, &info);
71066 if( info.nLocal<info.nPayload ){
71067 Pgno ovfl;
71068 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
71069 testcase( pSrc!=pPage );
71070 *pRC = SQLITE_CORRUPT_BKPT;
71071 return;
71072 }
71073 ovfl = get4byte(p: &pCell[info.nSize-4]);
71074 ptrmapPut(pBt: pPage->pBt, key: ovfl, PTRMAP_OVERFLOW1, parent: pPage->pgno, pRC);
71075 }
71076}
71077#endif
71078
71079
71080/*
71081** Defragment the page given. This routine reorganizes cells within the
71082** page so that there are no free-blocks on the free-block list.
71083**
71084** Parameter nMaxFrag is the maximum amount of fragmented space that may be
71085** present in the page after this routine returns.
71086**
71087** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
71088** b-tree page so that there are no freeblocks or fragment bytes, all
71089** unused bytes are contained in the unallocated space region, and all
71090** cells are packed tightly at the end of the page.
71091*/
71092static int defragmentPage(MemPage *pPage, int nMaxFrag){
71093 int i; /* Loop counter */
71094 int pc; /* Address of the i-th cell */
71095 int hdr; /* Offset to the page header */
71096 int size; /* Size of a cell */
71097 int usableSize; /* Number of usable bytes on a page */
71098 int cellOffset; /* Offset to the cell pointer array */
71099 int cbrk; /* Offset to the cell content area */
71100 int nCell; /* Number of cells on the page */
71101 unsigned char *data; /* The page data */
71102 unsigned char *temp; /* Temp area for cell content */
71103 unsigned char *src; /* Source of content */
71104 int iCellFirst; /* First allowable cell index */
71105 int iCellLast; /* Last possible cell index */
71106 int iCellStart; /* First cell offset in input */
71107
71108 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71109 assert( pPage->pBt!=0 );
71110 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
71111 assert( pPage->nOverflow==0 );
71112 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71113 data = pPage->aData;
71114 hdr = pPage->hdrOffset;
71115 cellOffset = pPage->cellOffset;
71116 nCell = pPage->nCell;
71117 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
71118 iCellFirst = cellOffset + 2*nCell;
71119 usableSize = pPage->pBt->usableSize;
71120
71121 /* This block handles pages with two or fewer free blocks and nMaxFrag
71122 ** or fewer fragmented bytes. In this case it is faster to move the
71123 ** two (or one) blocks of cells using memmove() and add the required
71124 ** offsets to each pointer in the cell-pointer array than it is to
71125 ** reconstruct the entire page. */
71126 if( (int)data[hdr+7]<=nMaxFrag ){
71127 int iFree = get2byte(&data[hdr+1]);
71128 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
71129 if( iFree ){
71130 int iFree2 = get2byte(&data[iFree]);
71131 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
71132 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
71133 u8 *pEnd = &data[cellOffset + nCell*2];
71134 u8 *pAddr;
71135 int sz2 = 0;
71136 int sz = get2byte(&data[iFree+2]);
71137 int top = get2byte(&data[hdr+5]);
71138 if( top>=iFree ){
71139 return SQLITE_CORRUPT_PAGE(pPage);
71140 }
71141 if( iFree2 ){
71142 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
71143 sz2 = get2byte(&data[iFree2+2]);
71144 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
71145 memmove(dest: &data[iFree+sz+sz2], src: &data[iFree+sz], n: iFree2-(iFree+sz));
71146 sz += sz2;
71147 }else if( iFree+sz>usableSize ){
71148 return SQLITE_CORRUPT_PAGE(pPage);
71149 }
71150
71151 cbrk = top+sz;
71152 assert( cbrk+(iFree-top) <= usableSize );
71153 memmove(dest: &data[cbrk], src: &data[top], n: iFree-top);
71154 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
71155 pc = get2byte(pAddr);
71156 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
71157 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
71158 }
71159 goto defragment_out;
71160 }
71161 }
71162 }
71163
71164 cbrk = usableSize;
71165 iCellLast = usableSize - 4;
71166 iCellStart = get2byte(&data[hdr+5]);
71167 if( nCell>0 ){
71168 temp = sqlite3PagerTempSpace(pPager: pPage->pBt->pPager);
71169 memcpy(dest: temp, src: data, n: usableSize);
71170 src = temp;
71171 for(i=0; i<nCell; i++){
71172 u8 *pAddr; /* The i-th cell pointer */
71173 pAddr = &data[cellOffset + i*2];
71174 pc = get2byte(pAddr);
71175 testcase( pc==iCellFirst );
71176 testcase( pc==iCellLast );
71177 /* These conditions have already been verified in btreeInitPage()
71178 ** if PRAGMA cell_size_check=ON.
71179 */
71180 if( pc>iCellLast ){
71181 return SQLITE_CORRUPT_PAGE(pPage);
71182 }
71183 assert( pc>=0 && pc<=iCellLast );
71184 size = pPage->xCellSize(pPage, &src[pc]);
71185 cbrk -= size;
71186 if( cbrk<iCellStart || pc+size>usableSize ){
71187 return SQLITE_CORRUPT_PAGE(pPage);
71188 }
71189 assert( cbrk+size<=usableSize && cbrk>=iCellStart );
71190 testcase( cbrk+size==usableSize );
71191 testcase( pc+size==usableSize );
71192 put2byte(pAddr, cbrk);
71193 memcpy(dest: &data[cbrk], src: &src[pc], n: size);
71194 }
71195 }
71196 data[hdr+7] = 0;
71197
71198defragment_out:
71199 assert( pPage->nFree>=0 );
71200 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
71201 return SQLITE_CORRUPT_PAGE(pPage);
71202 }
71203 assert( cbrk>=iCellFirst );
71204 put2byte(&data[hdr+5], cbrk);
71205 data[hdr+1] = 0;
71206 data[hdr+2] = 0;
71207 memset(s: &data[iCellFirst], c: 0, n: cbrk-iCellFirst);
71208 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71209 return SQLITE_OK;
71210}
71211
71212/*
71213** Search the free-list on page pPg for space to store a cell nByte bytes in
71214** size. If one can be found, return a pointer to the space and remove it
71215** from the free-list.
71216**
71217** If no suitable space can be found on the free-list, return NULL.
71218**
71219** This function may detect corruption within pPg. If corruption is
71220** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
71221**
71222** Slots on the free list that are between 1 and 3 bytes larger than nByte
71223** will be ignored if adding the extra space to the fragmentation count
71224** causes the fragmentation count to exceed 60.
71225*/
71226static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
71227 const int hdr = pPg->hdrOffset; /* Offset to page header */
71228 u8 * const aData = pPg->aData; /* Page data */
71229 int iAddr = hdr + 1; /* Address of ptr to pc */
71230 u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */
71231 int pc = get2byte(pTmp); /* Address of a free slot */
71232 int x; /* Excess size of the slot */
71233 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
71234 int size; /* Size of the free slot */
71235
71236 assert( pc>0 );
71237 while( pc<=maxPC ){
71238 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
71239 ** freeblock form a big-endian integer which is the size of the freeblock
71240 ** in bytes, including the 4-byte header. */
71241 pTmp = &aData[pc+2];
71242 size = get2byte(pTmp);
71243 if( (x = size - nByte)>=0 ){
71244 testcase( x==4 );
71245 testcase( x==3 );
71246 if( x<4 ){
71247 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
71248 ** number of bytes in fragments may not exceed 60. */
71249 if( aData[hdr+7]>57 ) return 0;
71250
71251 /* Remove the slot from the free-list. Update the number of
71252 ** fragmented bytes within the page. */
71253 memcpy(dest: &aData[iAddr], src: &aData[pc], n: 2);
71254 aData[hdr+7] += (u8)x;
71255 return &aData[pc];
71256 }else if( x+pc > maxPC ){
71257 /* This slot extends off the end of the usable part of the page */
71258 *pRc = SQLITE_CORRUPT_PAGE(pPg);
71259 return 0;
71260 }else{
71261 /* The slot remains on the free-list. Reduce its size to account
71262 ** for the portion used by the new allocation. */
71263 put2byte(&aData[pc+2], x);
71264 }
71265 return &aData[pc + x];
71266 }
71267 iAddr = pc;
71268 pTmp = &aData[pc];
71269 pc = get2byte(pTmp);
71270 if( pc<=iAddr ){
71271 if( pc ){
71272 /* The next slot in the chain comes before the current slot */
71273 *pRc = SQLITE_CORRUPT_PAGE(pPg);
71274 }
71275 return 0;
71276 }
71277 }
71278 if( pc>maxPC+nByte-4 ){
71279 /* The free slot chain extends off the end of the page */
71280 *pRc = SQLITE_CORRUPT_PAGE(pPg);
71281 }
71282 return 0;
71283}
71284
71285/*
71286** Allocate nByte bytes of space from within the B-Tree page passed
71287** as the first argument. Write into *pIdx the index into pPage->aData[]
71288** of the first byte of allocated space. Return either SQLITE_OK or
71289** an error code (usually SQLITE_CORRUPT).
71290**
71291** The caller guarantees that there is sufficient space to make the
71292** allocation. This routine might need to defragment in order to bring
71293** all the space together, however. This routine will avoid using
71294** the first two bytes past the cell pointer area since presumably this
71295** allocation is being made in order to insert a new cell, so we will
71296** also end up needing a new cell pointer.
71297*/
71298static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
71299 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
71300 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
71301 int top; /* First byte of cell content area */
71302 int rc = SQLITE_OK; /* Integer return code */
71303 u8 *pTmp; /* Temp ptr into data[] */
71304 int gap; /* First byte of gap between cell pointers and cell content */
71305
71306 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71307 assert( pPage->pBt );
71308 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71309 assert( nByte>=0 ); /* Minimum cell size is 4 */
71310 assert( pPage->nFree>=nByte );
71311 assert( pPage->nOverflow==0 );
71312 assert( nByte < (int)(pPage->pBt->usableSize-8) );
71313
71314 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
71315 gap = pPage->cellOffset + 2*pPage->nCell;
71316 assert( gap<=65536 );
71317 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
71318 ** and the reserved space is zero (the usual value for reserved space)
71319 ** then the cell content offset of an empty page wants to be 65536.
71320 ** However, that integer is too large to be stored in a 2-byte unsigned
71321 ** integer, so a value of 0 is used in its place. */
71322 pTmp = &data[hdr+5];
71323 top = get2byte(pTmp);
71324 if( gap>top ){
71325 if( top==0 && pPage->pBt->usableSize==65536 ){
71326 top = 65536;
71327 }else{
71328 return SQLITE_CORRUPT_PAGE(pPage);
71329 }
71330 }else if( top>(int)pPage->pBt->usableSize ){
71331 return SQLITE_CORRUPT_PAGE(pPage);
71332 }
71333
71334 /* If there is enough space between gap and top for one more cell pointer,
71335 ** and if the freelist is not empty, then search the
71336 ** freelist looking for a slot big enough to satisfy the request.
71337 */
71338 testcase( gap+2==top );
71339 testcase( gap+1==top );
71340 testcase( gap==top );
71341 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
71342 u8 *pSpace = pageFindSlot(pPg: pPage, nByte, pRc: &rc);
71343 if( pSpace ){
71344 int g2;
71345 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
71346 *pIdx = g2 = (int)(pSpace-data);
71347 if( g2<=gap ){
71348 return SQLITE_CORRUPT_PAGE(pPage);
71349 }else{
71350 return SQLITE_OK;
71351 }
71352 }else if( rc ){
71353 return rc;
71354 }
71355 }
71356
71357 /* The request could not be fulfilled using a freelist slot. Check
71358 ** to see if defragmentation is necessary.
71359 */
71360 testcase( gap+2+nByte==top );
71361 if( gap+2+nByte>top ){
71362 assert( pPage->nCell>0 || CORRUPT_DB );
71363 assert( pPage->nFree>=0 );
71364 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
71365 if( rc ) return rc;
71366 top = get2byteNotZero(&data[hdr+5]);
71367 assert( gap+2+nByte<=top );
71368 }
71369
71370
71371 /* Allocate memory from the gap in between the cell pointer array
71372 ** and the cell content area. The btreeComputeFreeSpace() call has already
71373 ** validated the freelist. Given that the freelist is valid, there
71374 ** is no way that the allocation can extend off the end of the page.
71375 ** The assert() below verifies the previous sentence.
71376 */
71377 top -= nByte;
71378 put2byte(&data[hdr+5], top);
71379 assert( top+nByte <= (int)pPage->pBt->usableSize );
71380 *pIdx = top;
71381 return SQLITE_OK;
71382}
71383
71384/*
71385** Return a section of the pPage->aData to the freelist.
71386** The first byte of the new free block is pPage->aData[iStart]
71387** and the size of the block is iSize bytes.
71388**
71389** Adjacent freeblocks are coalesced.
71390**
71391** Even though the freeblock list was checked by btreeComputeFreeSpace(),
71392** that routine will not detect overlap between cells or freeblocks. Nor
71393** does it detect cells or freeblocks that encroach into the reserved bytes
71394** at the end of the page. So do additional corruption checks inside this
71395** routine and return SQLITE_CORRUPT if any problems are found.
71396*/
71397static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
71398 u16 iPtr; /* Address of ptr to next freeblock */
71399 u16 iFreeBlk; /* Address of the next freeblock */
71400 u8 hdr; /* Page header size. 0 or 100 */
71401 u8 nFrag = 0; /* Reduction in fragmentation */
71402 u16 iOrigSize = iSize; /* Original value of iSize */
71403 u16 x; /* Offset to cell content area */
71404 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
71405 unsigned char *data = pPage->aData; /* Page content */
71406 u8 *pTmp; /* Temporary ptr into data[] */
71407
71408 assert( pPage->pBt!=0 );
71409 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71410 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
71411 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
71412 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71413 assert( iSize>=4 ); /* Minimum cell size is 4 */
71414 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
71415
71416 /* The list of freeblocks must be in ascending order. Find the
71417 ** spot on the list where iStart should be inserted.
71418 */
71419 hdr = pPage->hdrOffset;
71420 iPtr = hdr + 1;
71421 if( data[iPtr+1]==0 && data[iPtr]==0 ){
71422 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
71423 }else{
71424 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
71425 if( iFreeBlk<=iPtr ){
71426 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
71427 return SQLITE_CORRUPT_PAGE(pPage);
71428 }
71429 iPtr = iFreeBlk;
71430 }
71431 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
71432 return SQLITE_CORRUPT_PAGE(pPage);
71433 }
71434 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
71435
71436 /* At this point:
71437 ** iFreeBlk: First freeblock after iStart, or zero if none
71438 ** iPtr: The address of a pointer to iFreeBlk
71439 **
71440 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
71441 */
71442 if( iFreeBlk && iEnd+3>=iFreeBlk ){
71443 nFrag = iFreeBlk - iEnd;
71444 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
71445 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
71446 if( iEnd > pPage->pBt->usableSize ){
71447 return SQLITE_CORRUPT_PAGE(pPage);
71448 }
71449 iSize = iEnd - iStart;
71450 iFreeBlk = get2byte(&data[iFreeBlk]);
71451 }
71452
71453 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
71454 ** pointer in the page header) then check to see if iStart should be
71455 ** coalesced onto the end of iPtr.
71456 */
71457 if( iPtr>hdr+1 ){
71458 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
71459 if( iPtrEnd+3>=iStart ){
71460 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
71461 nFrag += iStart - iPtrEnd;
71462 iSize = iEnd - iPtr;
71463 iStart = iPtr;
71464 }
71465 }
71466 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
71467 data[hdr+7] -= nFrag;
71468 }
71469 pTmp = &data[hdr+5];
71470 x = get2byte(pTmp);
71471 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
71472 /* Overwrite deleted information with zeros when the secure_delete
71473 ** option is enabled */
71474 memset(s: &data[iStart], c: 0, n: iSize);
71475 }
71476 if( iStart<=x ){
71477 /* The new freeblock is at the beginning of the cell content area,
71478 ** so just extend the cell content area rather than create another
71479 ** freelist entry */
71480 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
71481 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
71482 put2byte(&data[hdr+1], iFreeBlk);
71483 put2byte(&data[hdr+5], iEnd);
71484 }else{
71485 /* Insert the new freeblock into the freelist */
71486 put2byte(&data[iPtr], iStart);
71487 put2byte(&data[iStart], iFreeBlk);
71488 put2byte(&data[iStart+2], iSize);
71489 }
71490 pPage->nFree += iOrigSize;
71491 return SQLITE_OK;
71492}
71493
71494/*
71495** Decode the flags byte (the first byte of the header) for a page
71496** and initialize fields of the MemPage structure accordingly.
71497**
71498** Only the following combinations are supported. Anything different
71499** indicates a corrupt database files:
71500**
71501** PTF_ZERODATA (0x02, 2)
71502** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
71503** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
71504** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
71505*/
71506static int decodeFlags(MemPage *pPage, int flagByte){
71507 BtShared *pBt; /* A copy of pPage->pBt */
71508
71509 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
71510 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71511 pBt = pPage->pBt;
71512 pPage->max1bytePayload = pBt->max1bytePayload;
71513 if( flagByte>=(PTF_ZERODATA | PTF_LEAF) ){
71514 pPage->childPtrSize = 0;
71515 pPage->leaf = 1;
71516 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF) ){
71517 pPage->intKeyLeaf = 1;
71518 pPage->xCellSize = cellSizePtrTableLeaf;
71519 pPage->xParseCell = btreeParseCellPtr;
71520 pPage->intKey = 1;
71521 pPage->maxLocal = pBt->maxLeaf;
71522 pPage->minLocal = pBt->minLeaf;
71523 }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){
71524 pPage->intKey = 0;
71525 pPage->intKeyLeaf = 0;
71526 pPage->xCellSize = cellSizePtrIdxLeaf;
71527 pPage->xParseCell = btreeParseCellPtrIndex;
71528 pPage->maxLocal = pBt->maxLocal;
71529 pPage->minLocal = pBt->minLocal;
71530 }else{
71531 pPage->intKey = 0;
71532 pPage->intKeyLeaf = 0;
71533 pPage->xCellSize = cellSizePtrIdxLeaf;
71534 pPage->xParseCell = btreeParseCellPtrIndex;
71535 return SQLITE_CORRUPT_PAGE(pPage);
71536 }
71537 }else{
71538 pPage->childPtrSize = 4;
71539 pPage->leaf = 0;
71540 if( flagByte==(PTF_ZERODATA) ){
71541 pPage->intKey = 0;
71542 pPage->intKeyLeaf = 0;
71543 pPage->xCellSize = cellSizePtr;
71544 pPage->xParseCell = btreeParseCellPtrIndex;
71545 pPage->maxLocal = pBt->maxLocal;
71546 pPage->minLocal = pBt->minLocal;
71547 }else if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
71548 pPage->intKeyLeaf = 0;
71549 pPage->xCellSize = cellSizePtrNoPayload;
71550 pPage->xParseCell = btreeParseCellPtrNoPayload;
71551 pPage->intKey = 1;
71552 pPage->maxLocal = pBt->maxLeaf;
71553 pPage->minLocal = pBt->minLeaf;
71554 }else{
71555 pPage->intKey = 0;
71556 pPage->intKeyLeaf = 0;
71557 pPage->xCellSize = cellSizePtr;
71558 pPage->xParseCell = btreeParseCellPtrIndex;
71559 return SQLITE_CORRUPT_PAGE(pPage);
71560 }
71561 }
71562 return SQLITE_OK;
71563}
71564
71565/*
71566** Compute the amount of freespace on the page. In other words, fill
71567** in the pPage->nFree field.
71568*/
71569static int btreeComputeFreeSpace(MemPage *pPage){
71570 int pc; /* Address of a freeblock within pPage->aData[] */
71571 u8 hdr; /* Offset to beginning of page header */
71572 u8 *data; /* Equal to pPage->aData */
71573 int usableSize; /* Amount of usable space on each page */
71574 int nFree; /* Number of unused bytes on the page */
71575 int top; /* First byte of the cell content area */
71576 int iCellFirst; /* First allowable cell or freeblock offset */
71577 int iCellLast; /* Last possible cell or freeblock offset */
71578
71579 assert( pPage->pBt!=0 );
71580 assert( pPage->pBt->db!=0 );
71581 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71582 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
71583 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
71584 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
71585 assert( pPage->isInit==1 );
71586 assert( pPage->nFree<0 );
71587
71588 usableSize = pPage->pBt->usableSize;
71589 hdr = pPage->hdrOffset;
71590 data = pPage->aData;
71591 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
71592 ** the start of the cell content area. A zero value for this integer is
71593 ** interpreted as 65536. */
71594 top = get2byteNotZero(&data[hdr+5]);
71595 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
71596 iCellLast = usableSize - 4;
71597
71598 /* Compute the total free space on the page
71599 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
71600 ** start of the first freeblock on the page, or is zero if there are no
71601 ** freeblocks. */
71602 pc = get2byte(&data[hdr+1]);
71603 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
71604 if( pc>0 ){
71605 u32 next, size;
71606 if( pc<top ){
71607 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
71608 ** always be at least one cell before the first freeblock.
71609 */
71610 return SQLITE_CORRUPT_PAGE(pPage);
71611 }
71612 while( 1 ){
71613 if( pc>iCellLast ){
71614 /* Freeblock off the end of the page */
71615 return SQLITE_CORRUPT_PAGE(pPage);
71616 }
71617 next = get2byte(&data[pc]);
71618 size = get2byte(&data[pc+2]);
71619 nFree = nFree + size;
71620 if( next<=pc+size+3 ) break;
71621 pc = next;
71622 }
71623 if( next>0 ){
71624 /* Freeblock not in ascending order */
71625 return SQLITE_CORRUPT_PAGE(pPage);
71626 }
71627 if( pc+size>(unsigned int)usableSize ){
71628 /* Last freeblock extends past page end */
71629 return SQLITE_CORRUPT_PAGE(pPage);
71630 }
71631 }
71632
71633 /* At this point, nFree contains the sum of the offset to the start
71634 ** of the cell-content area plus the number of free bytes within
71635 ** the cell-content area. If this is greater than the usable-size
71636 ** of the page, then the page must be corrupted. This check also
71637 ** serves to verify that the offset to the start of the cell-content
71638 ** area, according to the page header, lies within the page.
71639 */
71640 if( nFree>usableSize || nFree<iCellFirst ){
71641 return SQLITE_CORRUPT_PAGE(pPage);
71642 }
71643 pPage->nFree = (u16)(nFree - iCellFirst);
71644 return SQLITE_OK;
71645}
71646
71647/*
71648** Do additional sanity check after btreeInitPage() if
71649** PRAGMA cell_size_check=ON
71650*/
71651static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
71652 int iCellFirst; /* First allowable cell or freeblock offset */
71653 int iCellLast; /* Last possible cell or freeblock offset */
71654 int i; /* Index into the cell pointer array */
71655 int sz; /* Size of a cell */
71656 int pc; /* Address of a freeblock within pPage->aData[] */
71657 u8 *data; /* Equal to pPage->aData */
71658 int usableSize; /* Maximum usable space on the page */
71659 int cellOffset; /* Start of cell content area */
71660
71661 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
71662 usableSize = pPage->pBt->usableSize;
71663 iCellLast = usableSize - 4;
71664 data = pPage->aData;
71665 cellOffset = pPage->cellOffset;
71666 if( !pPage->leaf ) iCellLast--;
71667 for(i=0; i<pPage->nCell; i++){
71668 pc = get2byteAligned(&data[cellOffset+i*2]);
71669 testcase( pc==iCellFirst );
71670 testcase( pc==iCellLast );
71671 if( pc<iCellFirst || pc>iCellLast ){
71672 return SQLITE_CORRUPT_PAGE(pPage);
71673 }
71674 sz = pPage->xCellSize(pPage, &data[pc]);
71675 testcase( pc+sz==usableSize );
71676 if( pc+sz>usableSize ){
71677 return SQLITE_CORRUPT_PAGE(pPage);
71678 }
71679 }
71680 return SQLITE_OK;
71681}
71682
71683/*
71684** Initialize the auxiliary information for a disk block.
71685**
71686** Return SQLITE_OK on success. If we see that the page does
71687** not contain a well-formed database page, then return
71688** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
71689** guarantee that the page is well-formed. It only shows that
71690** we failed to detect any corruption.
71691*/
71692static int btreeInitPage(MemPage *pPage){
71693 u8 *data; /* Equal to pPage->aData */
71694 BtShared *pBt; /* The main btree structure */
71695
71696 assert( pPage->pBt!=0 );
71697 assert( pPage->pBt->db!=0 );
71698 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71699 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
71700 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
71701 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
71702 assert( pPage->isInit==0 );
71703
71704 pBt = pPage->pBt;
71705 data = pPage->aData + pPage->hdrOffset;
71706 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
71707 ** the b-tree page type. */
71708 if( decodeFlags(pPage, flagByte: data[0]) ){
71709 return SQLITE_CORRUPT_PAGE(pPage);
71710 }
71711 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
71712 pPage->maskPage = (u16)(pBt->pageSize - 1);
71713 pPage->nOverflow = 0;
71714 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
71715 pPage->aCellIdx = data + pPage->childPtrSize + 8;
71716 pPage->aDataEnd = pPage->aData + pBt->pageSize;
71717 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
71718 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
71719 ** number of cells on the page. */
71720 pPage->nCell = get2byte(&data[3]);
71721 if( pPage->nCell>MX_CELL(pBt) ){
71722 /* To many cells for a single page. The page must be corrupt */
71723 return SQLITE_CORRUPT_PAGE(pPage);
71724 }
71725 testcase( pPage->nCell==MX_CELL(pBt) );
71726 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
71727 ** possible for a root page of a table that contains no rows) then the
71728 ** offset to the cell content area will equal the page size minus the
71729 ** bytes of reserved space. */
71730 assert( pPage->nCell>0
71731 || get2byteNotZero(&data[5])==(int)pBt->usableSize
71732 || CORRUPT_DB );
71733 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
71734 pPage->isInit = 1;
71735 if( pBt->db->flags & SQLITE_CellSizeCk ){
71736 return btreeCellSizeCheck(pPage);
71737 }
71738 return SQLITE_OK;
71739}
71740
71741/*
71742** Set up a raw page so that it looks like a database page holding
71743** no entries.
71744*/
71745static void zeroPage(MemPage *pPage, int flags){
71746 unsigned char *data = pPage->aData;
71747 BtShared *pBt = pPage->pBt;
71748 u8 hdr = pPage->hdrOffset;
71749 u16 first;
71750
71751 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
71752 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71753 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
71754 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71755 assert( sqlite3_mutex_held(pBt->mutex) );
71756 if( pBt->btsFlags & BTS_FAST_SECURE ){
71757 memset(s: &data[hdr], c: 0, n: pBt->usableSize - hdr);
71758 }
71759 data[hdr] = (char)flags;
71760 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
71761 memset(s: &data[hdr+1], c: 0, n: 4);
71762 data[hdr+7] = 0;
71763 put2byte(&data[hdr+5], pBt->usableSize);
71764 pPage->nFree = (u16)(pBt->usableSize - first);
71765 decodeFlags(pPage, flagByte: flags);
71766 pPage->cellOffset = first;
71767 pPage->aDataEnd = &data[pBt->pageSize];
71768 pPage->aCellIdx = &data[first];
71769 pPage->aDataOfst = &data[pPage->childPtrSize];
71770 pPage->nOverflow = 0;
71771 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
71772 pPage->maskPage = (u16)(pBt->pageSize - 1);
71773 pPage->nCell = 0;
71774 pPage->isInit = 1;
71775}
71776
71777
71778/*
71779** Convert a DbPage obtained from the pager into a MemPage used by
71780** the btree layer.
71781*/
71782static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
71783 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pPg: pDbPage);
71784 if( pgno!=pPage->pgno ){
71785 pPage->aData = sqlite3PagerGetData(pPg: pDbPage);
71786 pPage->pDbPage = pDbPage;
71787 pPage->pBt = pBt;
71788 pPage->pgno = pgno;
71789 pPage->hdrOffset = pgno==1 ? 100 : 0;
71790 }
71791 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
71792 return pPage;
71793}
71794
71795/*
71796** Get a page from the pager. Initialize the MemPage.pBt and
71797** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
71798**
71799** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
71800** about the content of the page at this time. So do not go to the disk
71801** to fetch the content. Just fill in the content with zeros for now.
71802** If in the future we call sqlite3PagerWrite() on this page, that
71803** means we have started to be concerned about content and the disk
71804** read should occur at that point.
71805*/
71806static int btreeGetPage(
71807 BtShared *pBt, /* The btree */
71808 Pgno pgno, /* Number of the page to fetch */
71809 MemPage **ppPage, /* Return the page in this parameter */
71810 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
71811){
71812 int rc;
71813 DbPage *pDbPage;
71814
71815 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
71816 assert( sqlite3_mutex_held(pBt->mutex) );
71817 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno, ppPage: (DbPage**)&pDbPage, flags);
71818 if( rc ) return rc;
71819 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
71820 return SQLITE_OK;
71821}
71822
71823/*
71824** Retrieve a page from the pager cache. If the requested page is not
71825** already in the pager cache return NULL. Initialize the MemPage.pBt and
71826** MemPage.aData elements if needed.
71827*/
71828static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
71829 DbPage *pDbPage;
71830 assert( sqlite3_mutex_held(pBt->mutex) );
71831 pDbPage = sqlite3PagerLookup(pPager: pBt->pPager, pgno);
71832 if( pDbPage ){
71833 return btreePageFromDbPage(pDbPage, pgno, pBt);
71834 }
71835 return 0;
71836}
71837
71838/*
71839** Return the size of the database file in pages. If there is any kind of
71840** error, return ((unsigned int)-1).
71841*/
71842static Pgno btreePagecount(BtShared *pBt){
71843 return pBt->nPage;
71844}
71845SQLITE_PRIVATE Pgno sqlite3BtreeLastPage(Btree *p){
71846 assert( sqlite3BtreeHoldsMutex(p) );
71847 return btreePagecount(pBt: p->pBt);
71848}
71849
71850/*
71851** Get a page from the pager and initialize it.
71852*/
71853static int getAndInitPage(
71854 BtShared *pBt, /* The database file */
71855 Pgno pgno, /* Number of the page to get */
71856 MemPage **ppPage, /* Write the page pointer here */
71857 int bReadOnly /* True for a read-only page */
71858){
71859 int rc;
71860 DbPage *pDbPage;
71861 MemPage *pPage;
71862 assert( sqlite3_mutex_held(pBt->mutex) );
71863
71864 if( pgno>btreePagecount(pBt) ){
71865 *ppPage = 0;
71866 return SQLITE_CORRUPT_BKPT;
71867 }
71868 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno, ppPage: (DbPage**)&pDbPage, flags: bReadOnly);
71869 if( rc ){
71870 *ppPage = 0;
71871 return rc;
71872 }
71873 pPage = (MemPage*)sqlite3PagerGetExtra(pPg: pDbPage);
71874 if( pPage->isInit==0 ){
71875 btreePageFromDbPage(pDbPage, pgno, pBt);
71876 rc = btreeInitPage(pPage);
71877 if( rc!=SQLITE_OK ){
71878 releasePage(pPage);
71879 *ppPage = 0;
71880 return rc;
71881 }
71882 }
71883 assert( pPage->pgno==pgno || CORRUPT_DB );
71884 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
71885 *ppPage = pPage;
71886 return SQLITE_OK;
71887}
71888
71889/*
71890** Release a MemPage. This should be called once for each prior
71891** call to btreeGetPage.
71892**
71893** Page1 is a special case and must be released using releasePageOne().
71894*/
71895static void releasePageNotNull(MemPage *pPage){
71896 assert( pPage->aData );
71897 assert( pPage->pBt );
71898 assert( pPage->pDbPage!=0 );
71899 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71900 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
71901 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71902 sqlite3PagerUnrefNotNull(pPg: pPage->pDbPage);
71903}
71904static void releasePage(MemPage *pPage){
71905 if( pPage ) releasePageNotNull(pPage);
71906}
71907static void releasePageOne(MemPage *pPage){
71908 assert( pPage!=0 );
71909 assert( pPage->aData );
71910 assert( pPage->pBt );
71911 assert( pPage->pDbPage!=0 );
71912 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71913 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
71914 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71915 sqlite3PagerUnrefPageOne(pPg: pPage->pDbPage);
71916}
71917
71918/*
71919** Get an unused page.
71920**
71921** This works just like btreeGetPage() with the addition:
71922**
71923** * If the page is already in use for some other purpose, immediately
71924** release it and return an SQLITE_CURRUPT error.
71925** * Make sure the isInit flag is clear
71926*/
71927static int btreeGetUnusedPage(
71928 BtShared *pBt, /* The btree */
71929 Pgno pgno, /* Number of the page to fetch */
71930 MemPage **ppPage, /* Return the page in this parameter */
71931 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
71932){
71933 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
71934 if( rc==SQLITE_OK ){
71935 if( sqlite3PagerPageRefcount(pPage: (*ppPage)->pDbPage)>1 ){
71936 releasePage(pPage: *ppPage);
71937 *ppPage = 0;
71938 return SQLITE_CORRUPT_BKPT;
71939 }
71940 (*ppPage)->isInit = 0;
71941 }else{
71942 *ppPage = 0;
71943 }
71944 return rc;
71945}
71946
71947
71948/*
71949** During a rollback, when the pager reloads information into the cache
71950** so that the cache is restored to its original state at the start of
71951** the transaction, for each page restored this routine is called.
71952**
71953** This routine needs to reset the extra data section at the end of the
71954** page to agree with the restored data.
71955*/
71956static void pageReinit(DbPage *pData){
71957 MemPage *pPage;
71958 pPage = (MemPage *)sqlite3PagerGetExtra(pPg: pData);
71959 assert( sqlite3PagerPageRefcount(pData)>0 );
71960 if( pPage->isInit ){
71961 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71962 pPage->isInit = 0;
71963 if( sqlite3PagerPageRefcount(pPage: pData)>1 ){
71964 /* pPage might not be a btree page; it might be an overflow page
71965 ** or ptrmap page or a free page. In those cases, the following
71966 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
71967 ** But no harm is done by this. And it is very important that
71968 ** btreeInitPage() be called on every btree page so we make
71969 ** the call for every page that comes in for re-initializing. */
71970 btreeInitPage(pPage);
71971 }
71972 }
71973}
71974
71975/*
71976** Invoke the busy handler for a btree.
71977*/
71978static int btreeInvokeBusyHandler(void *pArg){
71979 BtShared *pBt = (BtShared*)pArg;
71980 assert( pBt->db );
71981 assert( sqlite3_mutex_held(pBt->db->mutex) );
71982 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
71983}
71984
71985/*
71986** Open a database file.
71987**
71988** zFilename is the name of the database file. If zFilename is NULL
71989** then an ephemeral database is created. The ephemeral database might
71990** be exclusively in memory, or it might use a disk-based memory cache.
71991** Either way, the ephemeral database will be automatically deleted
71992** when sqlite3BtreeClose() is called.
71993**
71994** If zFilename is ":memory:" then an in-memory database is created
71995** that is automatically destroyed when it is closed.
71996**
71997** The "flags" parameter is a bitmask that might contain bits like
71998** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
71999**
72000** If the database is already opened in the same database connection
72001** and we are in shared cache mode, then the open will fail with an
72002** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
72003** objects in the same database connection since doing so will lead
72004** to problems with locking.
72005*/
72006SQLITE_PRIVATE int sqlite3BtreeOpen(
72007 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
72008 const char *zFilename, /* Name of the file containing the BTree database */
72009 sqlite3 *db, /* Associated database handle */
72010 Btree **ppBtree, /* Pointer to new Btree object written here */
72011 int flags, /* Options */
72012 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
72013){
72014 BtShared *pBt = 0; /* Shared part of btree structure */
72015 Btree *p; /* Handle to return */
72016 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
72017 int rc = SQLITE_OK; /* Result code from this function */
72018 u8 nReserve; /* Byte of unused space on each page */
72019 unsigned char zDbHeader[100]; /* Database header content */
72020
72021 /* True if opening an ephemeral, temporary database */
72022 const int isTempDb = zFilename==0 || zFilename[0]==0;
72023
72024 /* Set the variable isMemdb to true for an in-memory database, or
72025 ** false for a file-based database.
72026 */
72027#ifdef SQLITE_OMIT_MEMORYDB
72028 const int isMemdb = 0;
72029#else
72030 const int isMemdb = (zFilename && strcmp(s1: zFilename, s2: ":memory:")==0)
72031 || (isTempDb && sqlite3TempInMemory(db))
72032 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
72033#endif
72034
72035 assert( db!=0 );
72036 assert( pVfs!=0 );
72037 assert( sqlite3_mutex_held(db->mutex) );
72038 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
72039
72040 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
72041 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
72042
72043 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
72044 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
72045
72046 if( isMemdb ){
72047 flags |= BTREE_MEMORY;
72048 }
72049 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
72050 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
72051 }
72052 p = sqlite3MallocZero(n: sizeof(Btree));
72053 if( !p ){
72054 return SQLITE_NOMEM_BKPT;
72055 }
72056 p->inTrans = TRANS_NONE;
72057 p->db = db;
72058#ifndef SQLITE_OMIT_SHARED_CACHE
72059 p->lock.pBtree = p;
72060 p->lock.iTable = 1;
72061#endif
72062
72063#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
72064 /*
72065 ** If this Btree is a candidate for shared cache, try to find an
72066 ** existing BtShared object that we can share with
72067 */
72068 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
72069 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
72070 int nFilename = sqlite3Strlen30(z: zFilename)+1;
72071 int nFullPathname = pVfs->mxPathname+1;
72072 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
72073 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
72074
72075 p->sharable = 1;
72076 if( !zFullPathname ){
72077 sqlite3_free(p);
72078 return SQLITE_NOMEM_BKPT;
72079 }
72080 if( isMemdb ){
72081 memcpy(dest: zFullPathname, src: zFilename, n: nFilename);
72082 }else{
72083 rc = sqlite3OsFullPathname(pVfs, zPath: zFilename,
72084 nPathOut: nFullPathname, zPathOut: zFullPathname);
72085 if( rc ){
72086 if( rc==SQLITE_OK_SYMLINK ){
72087 rc = SQLITE_OK;
72088 }else{
72089 sqlite3_free(p: zFullPathname);
72090 sqlite3_free(p);
72091 return rc;
72092 }
72093 }
72094 }
72095#if SQLITE_THREADSAFE
72096 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
72097 sqlite3_mutex_enter(p: mutexOpen);
72098 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
72099 sqlite3_mutex_enter(p: mutexShared);
72100#endif
72101 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
72102 assert( pBt->nRef>0 );
72103 if( 0==strcmp(s1: zFullPathname, s2: sqlite3PagerFilename(pPager: pBt->pPager, nullIfMemDb: 0))
72104 && sqlite3PagerVfs(pPager: pBt->pPager)==pVfs ){
72105 int iDb;
72106 for(iDb=db->nDb-1; iDb>=0; iDb--){
72107 Btree *pExisting = db->aDb[iDb].pBt;
72108 if( pExisting && pExisting->pBt==pBt ){
72109 sqlite3_mutex_leave(p: mutexShared);
72110 sqlite3_mutex_leave(p: mutexOpen);
72111 sqlite3_free(p: zFullPathname);
72112 sqlite3_free(p);
72113 return SQLITE_CONSTRAINT;
72114 }
72115 }
72116 p->pBt = pBt;
72117 pBt->nRef++;
72118 break;
72119 }
72120 }
72121 sqlite3_mutex_leave(p: mutexShared);
72122 sqlite3_free(p: zFullPathname);
72123 }
72124#ifdef SQLITE_DEBUG
72125 else{
72126 /* In debug mode, we mark all persistent databases as sharable
72127 ** even when they are not. This exercises the locking code and
72128 ** gives more opportunity for asserts(sqlite3_mutex_held())
72129 ** statements to find locking problems.
72130 */
72131 p->sharable = 1;
72132 }
72133#endif
72134 }
72135#endif
72136 if( pBt==0 ){
72137 /*
72138 ** The following asserts make sure that structures used by the btree are
72139 ** the right size. This is to guard against size changes that result
72140 ** when compiling on a different architecture.
72141 */
72142 assert( sizeof(i64)==8 );
72143 assert( sizeof(u64)==8 );
72144 assert( sizeof(u32)==4 );
72145 assert( sizeof(u16)==2 );
72146 assert( sizeof(Pgno)==4 );
72147
72148 /* Suppress false-positive compiler warning from PVS-Studio */
72149 memset(s: &zDbHeader[16], c: 0, n: 8);
72150
72151 pBt = sqlite3MallocZero( n: sizeof(*pBt) );
72152 if( pBt==0 ){
72153 rc = SQLITE_NOMEM_BKPT;
72154 goto btree_open_out;
72155 }
72156 rc = sqlite3PagerOpen(pVfs, ppPager: &pBt->pPager, zFilename,
72157 nExtra: sizeof(MemPage), flags, vfsFlags, xReinit: pageReinit);
72158 if( rc==SQLITE_OK ){
72159 sqlite3PagerSetMmapLimit(pPager: pBt->pPager, szMmap: db->szMmap);
72160 rc = sqlite3PagerReadFileheader(pPager: pBt->pPager,N: sizeof(zDbHeader),pDest: zDbHeader);
72161 }
72162 if( rc!=SQLITE_OK ){
72163 goto btree_open_out;
72164 }
72165 pBt->openFlags = (u8)flags;
72166 pBt->db = db;
72167 sqlite3PagerSetBusyHandler(pPager: pBt->pPager, xBusyHandler: btreeInvokeBusyHandler, pBusyHandlerArg: pBt);
72168 p->pBt = pBt;
72169
72170 pBt->pCursor = 0;
72171 pBt->pPage1 = 0;
72172 if( sqlite3PagerIsreadonly(pPager: pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
72173#if defined(SQLITE_SECURE_DELETE)
72174 pBt->btsFlags |= BTS_SECURE_DELETE;
72175#elif defined(SQLITE_FAST_SECURE_DELETE)
72176 pBt->btsFlags |= BTS_OVERWRITE;
72177#endif
72178 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
72179 ** determined by the 2-byte integer located at an offset of 16 bytes from
72180 ** the beginning of the database file. */
72181 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
72182 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
72183 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
72184 pBt->pageSize = 0;
72185#ifndef SQLITE_OMIT_AUTOVACUUM
72186 /* If the magic name ":memory:" will create an in-memory database, then
72187 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
72188 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
72189 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
72190 ** regular file-name. In this case the auto-vacuum applies as per normal.
72191 */
72192 if( zFilename && !isMemdb ){
72193 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
72194 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
72195 }
72196#endif
72197 nReserve = 0;
72198 }else{
72199 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
72200 ** determined by the one-byte unsigned integer found at an offset of 20
72201 ** into the database file header. */
72202 nReserve = zDbHeader[20];
72203 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72204#ifndef SQLITE_OMIT_AUTOVACUUM
72205 pBt->autoVacuum = (get4byte(p: &zDbHeader[36 + 4*4])?1:0);
72206 pBt->incrVacuum = (get4byte(p: &zDbHeader[36 + 7*4])?1:0);
72207#endif
72208 }
72209 rc = sqlite3PagerSetPagesize(pPager: pBt->pPager, pPageSize: &pBt->pageSize, nReserve);
72210 if( rc ) goto btree_open_out;
72211 pBt->usableSize = pBt->pageSize - nReserve;
72212 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
72213
72214#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
72215 /* Add the new BtShared object to the linked list sharable BtShareds.
72216 */
72217 pBt->nRef = 1;
72218 if( p->sharable ){
72219 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
72220 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)
72221 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
72222 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
72223 if( pBt->mutex==0 ){
72224 rc = SQLITE_NOMEM_BKPT;
72225 goto btree_open_out;
72226 }
72227 }
72228 sqlite3_mutex_enter(p: mutexShared);
72229 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
72230 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
72231 sqlite3_mutex_leave(p: mutexShared);
72232 }
72233#endif
72234 }
72235
72236#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
72237 /* If the new Btree uses a sharable pBtShared, then link the new
72238 ** Btree into the list of all sharable Btrees for the same connection.
72239 ** The list is kept in ascending order by pBt address.
72240 */
72241 if( p->sharable ){
72242 int i;
72243 Btree *pSib;
72244 for(i=0; i<db->nDb; i++){
72245 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
72246 while( pSib->pPrev ){ pSib = pSib->pPrev; }
72247 if( (uptr)p->pBt<(uptr)pSib->pBt ){
72248 p->pNext = pSib;
72249 p->pPrev = 0;
72250 pSib->pPrev = p;
72251 }else{
72252 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
72253 pSib = pSib->pNext;
72254 }
72255 p->pNext = pSib->pNext;
72256 p->pPrev = pSib;
72257 if( p->pNext ){
72258 p->pNext->pPrev = p;
72259 }
72260 pSib->pNext = p;
72261 }
72262 break;
72263 }
72264 }
72265 }
72266#endif
72267 *ppBtree = p;
72268
72269btree_open_out:
72270 if( rc!=SQLITE_OK ){
72271 if( pBt && pBt->pPager ){
72272 sqlite3PagerClose(pPager: pBt->pPager, db: 0);
72273 }
72274 sqlite3_free(p: pBt);
72275 sqlite3_free(p);
72276 *ppBtree = 0;
72277 }else{
72278 sqlite3_file *pFile;
72279
72280 /* If the B-Tree was successfully opened, set the pager-cache size to the
72281 ** default value. Except, when opening on an existing shared pager-cache,
72282 ** do not change the pager-cache size.
72283 */
72284 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
72285 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE);
72286 }
72287
72288 pFile = sqlite3PagerFile(pPager: pBt->pPager);
72289 if( pFile->pMethods ){
72290 sqlite3OsFileControlHint(id: pFile, SQLITE_FCNTL_PDB, pArg: (void*)&pBt->db);
72291 }
72292 }
72293 if( mutexOpen ){
72294 assert( sqlite3_mutex_held(mutexOpen) );
72295 sqlite3_mutex_leave(p: mutexOpen);
72296 }
72297 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
72298 return rc;
72299}
72300
72301/*
72302** Decrement the BtShared.nRef counter. When it reaches zero,
72303** remove the BtShared structure from the sharing list. Return
72304** true if the BtShared.nRef counter reaches zero and return
72305** false if it is still positive.
72306*/
72307static int removeFromSharingList(BtShared *pBt){
72308#ifndef SQLITE_OMIT_SHARED_CACHE
72309 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )
72310 BtShared *pList;
72311 int removed = 0;
72312
72313 assert( sqlite3_mutex_notheld(pBt->mutex) );
72314 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
72315 sqlite3_mutex_enter(p: pMainMtx);
72316 pBt->nRef--;
72317 if( pBt->nRef<=0 ){
72318 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
72319 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
72320 }else{
72321 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
72322 while( ALWAYS(pList) && pList->pNext!=pBt ){
72323 pList=pList->pNext;
72324 }
72325 if( ALWAYS(pList) ){
72326 pList->pNext = pBt->pNext;
72327 }
72328 }
72329 if( SQLITE_THREADSAFE ){
72330 sqlite3_mutex_free(p: pBt->mutex);
72331 }
72332 removed = 1;
72333 }
72334 sqlite3_mutex_leave(p: pMainMtx);
72335 return removed;
72336#else
72337 return 1;
72338#endif
72339}
72340
72341/*
72342** Make sure pBt->pTmpSpace points to an allocation of
72343** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
72344** pointer.
72345*/
72346static SQLITE_NOINLINE int allocateTempSpace(BtShared *pBt){
72347 assert( pBt!=0 );
72348 assert( pBt->pTmpSpace==0 );
72349 /* This routine is called only by btreeCursor() when allocating the
72350 ** first write cursor for the BtShared object */
72351 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
72352 pBt->pTmpSpace = sqlite3PageMalloc( sz: pBt->pageSize );
72353 if( pBt->pTmpSpace==0 ){
72354 BtCursor *pCur = pBt->pCursor;
72355 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
72356 memset(s: pCur, c: 0, n: sizeof(*pCur));
72357 return SQLITE_NOMEM_BKPT;
72358 }
72359
72360 /* One of the uses of pBt->pTmpSpace is to format cells before
72361 ** inserting them into a leaf page (function fillInCell()). If
72362 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
72363 ** by the various routines that manipulate binary cells. Which
72364 ** can mean that fillInCell() only initializes the first 2 or 3
72365 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
72366 ** it into a database page. This is not actually a problem, but it
72367 ** does cause a valgrind error when the 1 or 2 bytes of uninitialized
72368 ** data is passed to system call write(). So to avoid this error,
72369 ** zero the first 4 bytes of temp space here.
72370 **
72371 ** Also: Provide four bytes of initialized space before the
72372 ** beginning of pTmpSpace as an area available to prepend the
72373 ** left-child pointer to the beginning of a cell.
72374 */
72375 memset(s: pBt->pTmpSpace, c: 0, n: 8);
72376 pBt->pTmpSpace += 4;
72377 return SQLITE_OK;
72378}
72379
72380/*
72381** Free the pBt->pTmpSpace allocation
72382*/
72383static void freeTempSpace(BtShared *pBt){
72384 if( pBt->pTmpSpace ){
72385 pBt->pTmpSpace -= 4;
72386 sqlite3PageFree(p: pBt->pTmpSpace);
72387 pBt->pTmpSpace = 0;
72388 }
72389}
72390
72391/*
72392** Close an open database and invalidate all cursors.
72393*/
72394SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
72395 BtShared *pBt = p->pBt;
72396
72397 /* Close all cursors opened via this handle. */
72398 assert( sqlite3_mutex_held(p->db->mutex) );
72399 sqlite3BtreeEnter(p);
72400
72401 /* Verify that no other cursors have this Btree open */
72402#ifdef SQLITE_DEBUG
72403 {
72404 BtCursor *pCur = pBt->pCursor;
72405 while( pCur ){
72406 BtCursor *pTmp = pCur;
72407 pCur = pCur->pNext;
72408 assert( pTmp->pBtree!=p );
72409
72410 }
72411 }
72412#endif
72413
72414 /* Rollback any active transaction and free the handle structure.
72415 ** The call to sqlite3BtreeRollback() drops any table-locks held by
72416 ** this handle.
72417 */
72418 sqlite3BtreeRollback(p, SQLITE_OK, 0);
72419 sqlite3BtreeLeave(p);
72420
72421 /* If there are still other outstanding references to the shared-btree
72422 ** structure, return now. The remainder of this procedure cleans
72423 ** up the shared-btree.
72424 */
72425 assert( p->wantToLock==0 && p->locked==0 );
72426 if( !p->sharable || removeFromSharingList(pBt) ){
72427 /* The pBt is no longer on the sharing list, so we can access
72428 ** it without having to hold the mutex.
72429 **
72430 ** Clean out and delete the BtShared object.
72431 */
72432 assert( !pBt->pCursor );
72433 sqlite3PagerClose(pPager: pBt->pPager, db: p->db);
72434 if( pBt->xFreeSchema && pBt->pSchema ){
72435 pBt->xFreeSchema(pBt->pSchema);
72436 }
72437 sqlite3DbFree(db: 0, p: pBt->pSchema);
72438 freeTempSpace(pBt);
72439 sqlite3_free(p: pBt);
72440 }
72441
72442#ifndef SQLITE_OMIT_SHARED_CACHE
72443 assert( p->wantToLock==0 );
72444 assert( p->locked==0 );
72445 if( p->pPrev ) p->pPrev->pNext = p->pNext;
72446 if( p->pNext ) p->pNext->pPrev = p->pPrev;
72447#endif
72448
72449 sqlite3_free(p);
72450 return SQLITE_OK;
72451}
72452
72453/*
72454** Change the "soft" limit on the number of pages in the cache.
72455** Unused and unmodified pages will be recycled when the number of
72456** pages in the cache exceeds this soft limit. But the size of the
72457** cache is allowed to grow larger than this limit if it contains
72458** dirty pages or pages still in active use.
72459*/
72460SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
72461 BtShared *pBt = p->pBt;
72462 assert( sqlite3_mutex_held(p->db->mutex) );
72463 sqlite3BtreeEnter(p);
72464 sqlite3PagerSetCachesize(pPager: pBt->pPager, mxPage);
72465 sqlite3BtreeLeave(p);
72466 return SQLITE_OK;
72467}
72468
72469/*
72470** Change the "spill" limit on the number of pages in the cache.
72471** If the number of pages exceeds this limit during a write transaction,
72472** the pager might attempt to "spill" pages to the journal early in
72473** order to free up memory.
72474**
72475** The value returned is the current spill size. If zero is passed
72476** as an argument, no changes are made to the spill size setting, so
72477** using mxPage of 0 is a way to query the current spill size.
72478*/
72479SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
72480 BtShared *pBt = p->pBt;
72481 int res;
72482 assert( sqlite3_mutex_held(p->db->mutex) );
72483 sqlite3BtreeEnter(p);
72484 res = sqlite3PagerSetSpillsize(pPager: pBt->pPager, mxPage);
72485 sqlite3BtreeLeave(p);
72486 return res;
72487}
72488
72489#if SQLITE_MAX_MMAP_SIZE>0
72490/*
72491** Change the limit on the amount of the database file that may be
72492** memory mapped.
72493*/
72494SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
72495 BtShared *pBt = p->pBt;
72496 assert( sqlite3_mutex_held(p->db->mutex) );
72497 sqlite3BtreeEnter(p);
72498 sqlite3PagerSetMmapLimit(pPager: pBt->pPager, szMmap);
72499 sqlite3BtreeLeave(p);
72500 return SQLITE_OK;
72501}
72502#endif /* SQLITE_MAX_MMAP_SIZE>0 */
72503
72504/*
72505** Change the way data is synced to disk in order to increase or decrease
72506** how well the database resists damage due to OS crashes and power
72507** failures. Level 1 is the same as asynchronous (no syncs() occur and
72508** there is a high probability of damage) Level 2 is the default. There
72509** is a very low but non-zero probability of damage. Level 3 reduces the
72510** probability of damage to near zero but with a write performance reduction.
72511*/
72512#ifndef SQLITE_OMIT_PAGER_PRAGMAS
72513SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
72514 Btree *p, /* The btree to set the safety level on */
72515 unsigned pgFlags /* Various PAGER_* flags */
72516){
72517 BtShared *pBt = p->pBt;
72518 assert( sqlite3_mutex_held(p->db->mutex) );
72519 sqlite3BtreeEnter(p);
72520 sqlite3PagerSetFlags(pPager: pBt->pPager, pgFlags);
72521 sqlite3BtreeLeave(p);
72522 return SQLITE_OK;
72523}
72524#endif
72525
72526/*
72527** Change the default pages size and the number of reserved bytes per page.
72528** Or, if the page size has already been fixed, return SQLITE_READONLY
72529** without changing anything.
72530**
72531** The page size must be a power of 2 between 512 and 65536. If the page
72532** size supplied does not meet this constraint then the page size is not
72533** changed.
72534**
72535** Page sizes are constrained to be a power of two so that the region
72536** of the database file used for locking (beginning at PENDING_BYTE,
72537** the first byte past the 1GB boundary, 0x40000000) needs to occur
72538** at the beginning of a page.
72539**
72540** If parameter nReserve is less than zero, then the number of reserved
72541** bytes per page is left unchanged.
72542**
72543** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
72544** and autovacuum mode can no longer be changed.
72545*/
72546SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
72547 int rc = SQLITE_OK;
72548 int x;
72549 BtShared *pBt = p->pBt;
72550 assert( nReserve>=0 && nReserve<=255 );
72551 sqlite3BtreeEnter(p);
72552 pBt->nReserveWanted = nReserve;
72553 x = pBt->pageSize - pBt->usableSize;
72554 if( nReserve<x ) nReserve = x;
72555 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
72556 sqlite3BtreeLeave(p);
72557 return SQLITE_READONLY;
72558 }
72559 assert( nReserve>=0 && nReserve<=255 );
72560 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
72561 ((pageSize-1)&pageSize)==0 ){
72562 assert( (pageSize & 7)==0 );
72563 assert( !pBt->pCursor );
72564 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
72565 pBt->pageSize = (u32)pageSize;
72566 freeTempSpace(pBt);
72567 }
72568 rc = sqlite3PagerSetPagesize(pPager: pBt->pPager, pPageSize: &pBt->pageSize, nReserve);
72569 pBt->usableSize = pBt->pageSize - (u16)nReserve;
72570 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72571 sqlite3BtreeLeave(p);
72572 return rc;
72573}
72574
72575/*
72576** Return the currently defined page size
72577*/
72578SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
72579 return p->pBt->pageSize;
72580}
72581
72582/*
72583** This function is similar to sqlite3BtreeGetReserve(), except that it
72584** may only be called if it is guaranteed that the b-tree mutex is already
72585** held.
72586**
72587** This is useful in one special case in the backup API code where it is
72588** known that the shared b-tree mutex is held, but the mutex on the
72589** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
72590** were to be called, it might collide with some other operation on the
72591** database handle that owns *p, causing undefined behavior.
72592*/
72593SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
72594 int n;
72595 assert( sqlite3_mutex_held(p->pBt->mutex) );
72596 n = p->pBt->pageSize - p->pBt->usableSize;
72597 return n;
72598}
72599
72600/*
72601** Return the number of bytes of space at the end of every page that
72602** are intentionally left unused. This is the "reserved" space that is
72603** sometimes used by extensions.
72604**
72605** The value returned is the larger of the current reserve size and
72606** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
72607** The amount of reserve can only grow - never shrink.
72608*/
72609SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){
72610 int n1, n2;
72611 sqlite3BtreeEnter(p);
72612 n1 = (int)p->pBt->nReserveWanted;
72613 n2 = sqlite3BtreeGetReserveNoMutex(p);
72614 sqlite3BtreeLeave(p);
72615 return n1>n2 ? n1 : n2;
72616}
72617
72618
72619/*
72620** Set the maximum page count for a database if mxPage is positive.
72621** No changes are made if mxPage is 0 or negative.
72622** Regardless of the value of mxPage, return the maximum page count.
72623*/
72624SQLITE_PRIVATE Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
72625 Pgno n;
72626 sqlite3BtreeEnter(p);
72627 n = sqlite3PagerMaxPageCount(pPager: p->pBt->pPager, mxPage);
72628 sqlite3BtreeLeave(p);
72629 return n;
72630}
72631
72632/*
72633** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
72634**
72635** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
72636** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
72637** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
72638** newFlag==(-1) No changes
72639**
72640** This routine acts as a query if newFlag is less than zero
72641**
72642** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
72643** freelist leaf pages are not written back to the database. Thus in-page
72644** deleted content is cleared, but freelist deleted content is not.
72645**
72646** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
72647** that freelist leaf pages are written back into the database, increasing
72648** the amount of disk I/O.
72649*/
72650SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
72651 int b;
72652 if( p==0 ) return 0;
72653 sqlite3BtreeEnter(p);
72654 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
72655 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
72656 if( newFlag>=0 ){
72657 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
72658 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
72659 }
72660 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
72661 sqlite3BtreeLeave(p);
72662 return b;
72663}
72664
72665/*
72666** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
72667** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
72668** is disabled. The default value for the auto-vacuum property is
72669** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
72670*/
72671SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
72672#ifdef SQLITE_OMIT_AUTOVACUUM
72673 return SQLITE_READONLY;
72674#else
72675 BtShared *pBt = p->pBt;
72676 int rc = SQLITE_OK;
72677 u8 av = (u8)autoVacuum;
72678
72679 sqlite3BtreeEnter(p);
72680 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
72681 rc = SQLITE_READONLY;
72682 }else{
72683 pBt->autoVacuum = av ?1:0;
72684 pBt->incrVacuum = av==2 ?1:0;
72685 }
72686 sqlite3BtreeLeave(p);
72687 return rc;
72688#endif
72689}
72690
72691/*
72692** Return the value of the 'auto-vacuum' property. If auto-vacuum is
72693** enabled 1 is returned. Otherwise 0.
72694*/
72695SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
72696#ifdef SQLITE_OMIT_AUTOVACUUM
72697 return BTREE_AUTOVACUUM_NONE;
72698#else
72699 int rc;
72700 sqlite3BtreeEnter(p);
72701 rc = (
72702 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
72703 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
72704 BTREE_AUTOVACUUM_INCR
72705 );
72706 sqlite3BtreeLeave(p);
72707 return rc;
72708#endif
72709}
72710
72711/*
72712** If the user has not set the safety-level for this database connection
72713** using "PRAGMA synchronous", and if the safety-level is not already
72714** set to the value passed to this function as the second parameter,
72715** set it so.
72716*/
72717#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
72718 && !defined(SQLITE_OMIT_WAL)
72719static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
72720 sqlite3 *db;
72721 Db *pDb;
72722 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
72723 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
72724 if( pDb->bSyncSet==0
72725 && pDb->safety_level!=safety_level
72726 && pDb!=&db->aDb[1]
72727 ){
72728 pDb->safety_level = safety_level;
72729 sqlite3PagerSetFlags(pBt->pPager,
72730 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
72731 }
72732 }
72733}
72734#else
72735# define setDefaultSyncFlag(pBt,safety_level)
72736#endif
72737
72738/* Forward declaration */
72739static int newDatabase(BtShared*);
72740
72741
72742/*
72743** Get a reference to pPage1 of the database file. This will
72744** also acquire a readlock on that file.
72745**
72746** SQLITE_OK is returned on success. If the file is not a
72747** well-formed database file, then SQLITE_CORRUPT is returned.
72748** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
72749** is returned if we run out of memory.
72750*/
72751static int lockBtree(BtShared *pBt){
72752 int rc; /* Result code from subfunctions */
72753 MemPage *pPage1; /* Page 1 of the database file */
72754 u32 nPage; /* Number of pages in the database */
72755 u32 nPageFile = 0; /* Number of pages in the database file */
72756
72757 assert( sqlite3_mutex_held(pBt->mutex) );
72758 assert( pBt->pPage1==0 );
72759 rc = sqlite3PagerSharedLock(pPager: pBt->pPager);
72760 if( rc!=SQLITE_OK ) return rc;
72761 rc = btreeGetPage(pBt, pgno: 1, ppPage: &pPage1, flags: 0);
72762 if( rc!=SQLITE_OK ) return rc;
72763
72764 /* Do some checking to help insure the file we opened really is
72765 ** a valid database file.
72766 */
72767 nPage = get4byte(p: 28+(u8*)pPage1->aData);
72768 sqlite3PagerPagecount(pPager: pBt->pPager, pnPage: (int*)&nPageFile);
72769 if( nPage==0 || memcmp(s1: 24+(u8*)pPage1->aData, s2: 92+(u8*)pPage1->aData,n: 4)!=0 ){
72770 nPage = nPageFile;
72771 }
72772 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
72773 nPage = 0;
72774 }
72775 if( nPage>0 ){
72776 u32 pageSize;
72777 u32 usableSize;
72778 u8 *page1 = pPage1->aData;
72779 rc = SQLITE_NOTADB;
72780 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
72781 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
72782 ** 61 74 20 33 00. */
72783 if( memcmp(s1: page1, s2: zMagicHeader, n: 16)!=0 ){
72784 goto page1_init_failed;
72785 }
72786
72787#ifdef SQLITE_OMIT_WAL
72788 if( page1[18]>1 ){
72789 pBt->btsFlags |= BTS_READ_ONLY;
72790 }
72791 if( page1[19]>1 ){
72792 goto page1_init_failed;
72793 }
72794#else
72795 if( page1[18]>2 ){
72796 pBt->btsFlags |= BTS_READ_ONLY;
72797 }
72798 if( page1[19]>2 ){
72799 goto page1_init_failed;
72800 }
72801
72802 /* If the read version is set to 2, this database should be accessed
72803 ** in WAL mode. If the log is not already open, open it now. Then
72804 ** return SQLITE_OK and return without populating BtShared.pPage1.
72805 ** The caller detects this and calls this function again. This is
72806 ** required as the version of page 1 currently in the page1 buffer
72807 ** may not be the latest version - there may be a newer one in the log
72808 ** file.
72809 */
72810 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
72811 int isOpen = 0;
72812 rc = sqlite3PagerOpenWal(pPager: pBt->pPager, pbOpen: &isOpen);
72813 if( rc!=SQLITE_OK ){
72814 goto page1_init_failed;
72815 }else{
72816 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
72817 if( isOpen==0 ){
72818 releasePageOne(pPage: pPage1);
72819 return SQLITE_OK;
72820 }
72821 }
72822 rc = SQLITE_NOTADB;
72823 }else{
72824 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
72825 }
72826#endif
72827
72828 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
72829 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
72830 **
72831 ** The original design allowed these amounts to vary, but as of
72832 ** version 3.6.0, we require them to be fixed.
72833 */
72834 if( memcmp(s1: &page1[21], s2: "\100\040\040",n: 3)!=0 ){
72835 goto page1_init_failed;
72836 }
72837 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
72838 ** determined by the 2-byte integer located at an offset of 16 bytes from
72839 ** the beginning of the database file. */
72840 pageSize = (page1[16]<<8) | (page1[17]<<16);
72841 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
72842 ** between 512 and 65536 inclusive. */
72843 if( ((pageSize-1)&pageSize)!=0
72844 || pageSize>SQLITE_MAX_PAGE_SIZE
72845 || pageSize<=256
72846 ){
72847 goto page1_init_failed;
72848 }
72849 assert( (pageSize & 7)==0 );
72850 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
72851 ** integer at offset 20 is the number of bytes of space at the end of
72852 ** each page to reserve for extensions.
72853 **
72854 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
72855 ** determined by the one-byte unsigned integer found at an offset of 20
72856 ** into the database file header. */
72857 usableSize = pageSize - page1[20];
72858 if( (u32)pageSize!=pBt->pageSize ){
72859 /* After reading the first page of the database assuming a page size
72860 ** of BtShared.pageSize, we have discovered that the page-size is
72861 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
72862 ** zero and return SQLITE_OK. The caller will call this function
72863 ** again with the correct page-size.
72864 */
72865 releasePageOne(pPage: pPage1);
72866 pBt->usableSize = usableSize;
72867 pBt->pageSize = pageSize;
72868 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72869 freeTempSpace(pBt);
72870 rc = sqlite3PagerSetPagesize(pPager: pBt->pPager, pPageSize: &pBt->pageSize,
72871 nReserve: pageSize-usableSize);
72872 return rc;
72873 }
72874 if( nPage>nPageFile ){
72875 if( sqlite3WritableSchema(pBt->db)==0 ){
72876 rc = SQLITE_CORRUPT_BKPT;
72877 goto page1_init_failed;
72878 }else{
72879 nPage = nPageFile;
72880 }
72881 }
72882 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
72883 ** be less than 480. In other words, if the page size is 512, then the
72884 ** reserved space size cannot exceed 32. */
72885 if( usableSize<480 ){
72886 goto page1_init_failed;
72887 }
72888 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72889 pBt->pageSize = pageSize;
72890 pBt->usableSize = usableSize;
72891#ifndef SQLITE_OMIT_AUTOVACUUM
72892 pBt->autoVacuum = (get4byte(p: &page1[36 + 4*4])?1:0);
72893 pBt->incrVacuum = (get4byte(p: &page1[36 + 7*4])?1:0);
72894#endif
72895 }
72896
72897 /* maxLocal is the maximum amount of payload to store locally for
72898 ** a cell. Make sure it is small enough so that at least minFanout
72899 ** cells can will fit on one page. We assume a 10-byte page header.
72900 ** Besides the payload, the cell must store:
72901 ** 2-byte pointer to the cell
72902 ** 4-byte child pointer
72903 ** 9-byte nKey value
72904 ** 4-byte nData value
72905 ** 4-byte overflow page pointer
72906 ** So a cell consists of a 2-byte pointer, a header which is as much as
72907 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
72908 ** page pointer.
72909 */
72910 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
72911 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
72912 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
72913 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
72914 if( pBt->maxLocal>127 ){
72915 pBt->max1bytePayload = 127;
72916 }else{
72917 pBt->max1bytePayload = (u8)pBt->maxLocal;
72918 }
72919 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
72920 pBt->pPage1 = pPage1;
72921 pBt->nPage = nPage;
72922 return SQLITE_OK;
72923
72924page1_init_failed:
72925 releasePageOne(pPage: pPage1);
72926 pBt->pPage1 = 0;
72927 return rc;
72928}
72929
72930#ifndef NDEBUG
72931/*
72932** Return the number of cursors open on pBt. This is for use
72933** in assert() expressions, so it is only compiled if NDEBUG is not
72934** defined.
72935**
72936** Only write cursors are counted if wrOnly is true. If wrOnly is
72937** false then all cursors are counted.
72938**
72939** For the purposes of this routine, a cursor is any cursor that
72940** is capable of reading or writing to the database. Cursors that
72941** have been tripped into the CURSOR_FAULT state are not counted.
72942*/
72943static int countValidCursors(BtShared *pBt, int wrOnly){
72944 BtCursor *pCur;
72945 int r = 0;
72946 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
72947 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
72948 && pCur->eState!=CURSOR_FAULT ) r++;
72949 }
72950 return r;
72951}
72952#endif
72953
72954/*
72955** If there are no outstanding cursors and we are not in the middle
72956** of a transaction but there is a read lock on the database, then
72957** this routine unrefs the first page of the database file which
72958** has the effect of releasing the read lock.
72959**
72960** If there is a transaction in progress, this routine is a no-op.
72961*/
72962static void unlockBtreeIfUnused(BtShared *pBt){
72963 assert( sqlite3_mutex_held(pBt->mutex) );
72964 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
72965 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
72966 MemPage *pPage1 = pBt->pPage1;
72967 assert( pPage1->aData );
72968 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
72969 pBt->pPage1 = 0;
72970 releasePageOne(pPage: pPage1);
72971 }
72972}
72973
72974/*
72975** If pBt points to an empty file then convert that empty file
72976** into a new empty database by initializing the first page of
72977** the database.
72978*/
72979static int newDatabase(BtShared *pBt){
72980 MemPage *pP1;
72981 unsigned char *data;
72982 int rc;
72983
72984 assert( sqlite3_mutex_held(pBt->mutex) );
72985 if( pBt->nPage>0 ){
72986 return SQLITE_OK;
72987 }
72988 pP1 = pBt->pPage1;
72989 assert( pP1!=0 );
72990 data = pP1->aData;
72991 rc = sqlite3PagerWrite(pPg: pP1->pDbPage);
72992 if( rc ) return rc;
72993 memcpy(dest: data, src: zMagicHeader, n: sizeof(zMagicHeader));
72994 assert( sizeof(zMagicHeader)==16 );
72995 data[16] = (u8)((pBt->pageSize>>8)&0xff);
72996 data[17] = (u8)((pBt->pageSize>>16)&0xff);
72997 data[18] = 1;
72998 data[19] = 1;
72999 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
73000 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
73001 data[21] = 64;
73002 data[22] = 32;
73003 data[23] = 32;
73004 memset(s: &data[24], c: 0, n: 100-24);
73005 zeroPage(pPage: pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
73006 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73007#ifndef SQLITE_OMIT_AUTOVACUUM
73008 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
73009 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
73010 put4byte(p: &data[36 + 4*4], v: pBt->autoVacuum);
73011 put4byte(p: &data[36 + 7*4], v: pBt->incrVacuum);
73012#endif
73013 pBt->nPage = 1;
73014 data[31] = 1;
73015 return SQLITE_OK;
73016}
73017
73018/*
73019** Initialize the first page of the database file (creating a database
73020** consisting of a single page and no schema objects). Return SQLITE_OK
73021** if successful, or an SQLite error code otherwise.
73022*/
73023SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
73024 int rc;
73025 sqlite3BtreeEnter(p);
73026 p->pBt->nPage = 0;
73027 rc = newDatabase(pBt: p->pBt);
73028 sqlite3BtreeLeave(p);
73029 return rc;
73030}
73031
73032/*
73033** Attempt to start a new transaction. A write-transaction
73034** is started if the second argument is nonzero, otherwise a read-
73035** transaction. If the second argument is 2 or more and exclusive
73036** transaction is started, meaning that no other process is allowed
73037** to access the database. A preexisting transaction may not be
73038** upgraded to exclusive by calling this routine a second time - the
73039** exclusivity flag only works for a new transaction.
73040**
73041** A write-transaction must be started before attempting any
73042** changes to the database. None of the following routines
73043** will work unless a transaction is started first:
73044**
73045** sqlite3BtreeCreateTable()
73046** sqlite3BtreeCreateIndex()
73047** sqlite3BtreeClearTable()
73048** sqlite3BtreeDropTable()
73049** sqlite3BtreeInsert()
73050** sqlite3BtreeDelete()
73051** sqlite3BtreeUpdateMeta()
73052**
73053** If an initial attempt to acquire the lock fails because of lock contention
73054** and the database was previously unlocked, then invoke the busy handler
73055** if there is one. But if there was previously a read-lock, do not
73056** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
73057** returned when there is already a read-lock in order to avoid a deadlock.
73058**
73059** Suppose there are two processes A and B. A has a read lock and B has
73060** a reserved lock. B tries to promote to exclusive but is blocked because
73061** of A's read lock. A tries to promote to reserved but is blocked by B.
73062** One or the other of the two processes must give way or there can be
73063** no progress. By returning SQLITE_BUSY and not invoking the busy callback
73064** when A already has a read lock, we encourage A to give up and let B
73065** proceed.
73066*/
73067static SQLITE_NOINLINE int btreeBeginTrans(
73068 Btree *p, /* The btree in which to start the transaction */
73069 int wrflag, /* True to start a write transaction */
73070 int *pSchemaVersion /* Put schema version number here, if not NULL */
73071){
73072 BtShared *pBt = p->pBt;
73073 Pager *pPager = pBt->pPager;
73074 int rc = SQLITE_OK;
73075
73076 sqlite3BtreeEnter(p);
73077 btreeIntegrity(p);
73078
73079 /* If the btree is already in a write-transaction, or it
73080 ** is already in a read-transaction and a read-transaction
73081 ** is requested, this is a no-op.
73082 */
73083 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
73084 goto trans_begun;
73085 }
73086 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
73087
73088 if( (p->db->flags & SQLITE_ResetDatabase)
73089 && sqlite3PagerIsreadonly(pPager)==0
73090 ){
73091 pBt->btsFlags &= ~BTS_READ_ONLY;
73092 }
73093
73094 /* Write transactions are not possible on a read-only database */
73095 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
73096 rc = SQLITE_READONLY;
73097 goto trans_begun;
73098 }
73099
73100#ifndef SQLITE_OMIT_SHARED_CACHE
73101 {
73102 sqlite3 *pBlock = 0;
73103 /* If another database handle has already opened a write transaction
73104 ** on this shared-btree structure and a second write transaction is
73105 ** requested, return SQLITE_LOCKED.
73106 */
73107 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
73108 || (pBt->btsFlags & BTS_PENDING)!=0
73109 ){
73110 pBlock = pBt->pWriter->db;
73111 }else if( wrflag>1 ){
73112 BtLock *pIter;
73113 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
73114 if( pIter->pBtree!=p ){
73115 pBlock = pIter->pBtree->db;
73116 break;
73117 }
73118 }
73119 }
73120 if( pBlock ){
73121 sqlite3ConnectionBlocked(p->db, pBlock);
73122 rc = SQLITE_LOCKED_SHAREDCACHE;
73123 goto trans_begun;
73124 }
73125 }
73126#endif
73127
73128 /* Any read-only or read-write transaction implies a read-lock on
73129 ** page 1. So if some other shared-cache client already has a write-lock
73130 ** on page 1, the transaction cannot be opened. */
73131 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
73132 if( SQLITE_OK!=rc ) goto trans_begun;
73133
73134 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
73135 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
73136 do {
73137 sqlite3PagerWalDb(pPager, p->db);
73138
73139#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
73140 /* If transitioning from no transaction directly to a write transaction,
73141 ** block for the WRITER lock first if possible. */
73142 if( pBt->pPage1==0 && wrflag ){
73143 assert( pBt->inTransaction==TRANS_NONE );
73144 rc = sqlite3PagerWalWriteLock(pPager, 1);
73145 if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
73146 }
73147#endif
73148
73149 /* Call lockBtree() until either pBt->pPage1 is populated or
73150 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
73151 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
73152 ** reading page 1 it discovers that the page-size of the database
73153 ** file is not pBt->pageSize. In this case lockBtree() will update
73154 ** pBt->pageSize to the page-size of the file on disk.
73155 */
73156 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
73157
73158 if( rc==SQLITE_OK && wrflag ){
73159 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
73160 rc = SQLITE_READONLY;
73161 }else{
73162 rc = sqlite3PagerBegin(pPager, exFlag: wrflag>1, subjInMemory: sqlite3TempInMemory(p->db));
73163 if( rc==SQLITE_OK ){
73164 rc = newDatabase(pBt);
73165 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
73166 /* if there was no transaction opened when this function was
73167 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
73168 ** code to SQLITE_BUSY. */
73169 rc = SQLITE_BUSY;
73170 }
73171 }
73172 }
73173
73174 if( rc!=SQLITE_OK ){
73175 (void)sqlite3PagerWalWriteLock(pPager, 0);
73176 unlockBtreeIfUnused(pBt);
73177 }
73178 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
73179 btreeInvokeBusyHandler(pArg: pBt) );
73180 sqlite3PagerWalDb(pPager, 0);
73181#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
73182 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
73183#endif
73184
73185 if( rc==SQLITE_OK ){
73186 if( p->inTrans==TRANS_NONE ){
73187 pBt->nTransaction++;
73188#ifndef SQLITE_OMIT_SHARED_CACHE
73189 if( p->sharable ){
73190 assert( p->lock.pBtree==p && p->lock.iTable==1 );
73191 p->lock.eLock = READ_LOCK;
73192 p->lock.pNext = pBt->pLock;
73193 pBt->pLock = &p->lock;
73194 }
73195#endif
73196 }
73197 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
73198 if( p->inTrans>pBt->inTransaction ){
73199 pBt->inTransaction = p->inTrans;
73200 }
73201 if( wrflag ){
73202 MemPage *pPage1 = pBt->pPage1;
73203#ifndef SQLITE_OMIT_SHARED_CACHE
73204 assert( !pBt->pWriter );
73205 pBt->pWriter = p;
73206 pBt->btsFlags &= ~BTS_EXCLUSIVE;
73207 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
73208#endif
73209
73210 /* If the db-size header field is incorrect (as it may be if an old
73211 ** client has been writing the database file), update it now. Doing
73212 ** this sooner rather than later means the database size can safely
73213 ** re-read the database size from page 1 if a savepoint or transaction
73214 ** rollback occurs within the transaction.
73215 */
73216 if( pBt->nPage!=get4byte(p: &pPage1->aData[28]) ){
73217 rc = sqlite3PagerWrite(pPg: pPage1->pDbPage);
73218 if( rc==SQLITE_OK ){
73219 put4byte(p: &pPage1->aData[28], v: pBt->nPage);
73220 }
73221 }
73222 }
73223 }
73224
73225trans_begun:
73226 if( rc==SQLITE_OK ){
73227 if( pSchemaVersion ){
73228 *pSchemaVersion = get4byte(p: &pBt->pPage1->aData[40]);
73229 }
73230 if( wrflag ){
73231 /* This call makes sure that the pager has the correct number of
73232 ** open savepoints. If the second parameter is greater than 0 and
73233 ** the sub-journal is not already open, then it will be opened here.
73234 */
73235 rc = sqlite3PagerOpenSavepoint(pPager, nSavepoint: p->db->nSavepoint);
73236 }
73237 }
73238
73239 btreeIntegrity(p);
73240 sqlite3BtreeLeave(p);
73241 return rc;
73242}
73243SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
73244 BtShared *pBt;
73245 if( p->sharable
73246 || p->inTrans==TRANS_NONE
73247 || (p->inTrans==TRANS_READ && wrflag!=0)
73248 ){
73249 return btreeBeginTrans(p,wrflag,pSchemaVersion);
73250 }
73251 pBt = p->pBt;
73252 if( pSchemaVersion ){
73253 *pSchemaVersion = get4byte(p: &pBt->pPage1->aData[40]);
73254 }
73255 if( wrflag ){
73256 /* This call makes sure that the pager has the correct number of
73257 ** open savepoints. If the second parameter is greater than 0 and
73258 ** the sub-journal is not already open, then it will be opened here.
73259 */
73260 return sqlite3PagerOpenSavepoint(pPager: pBt->pPager, nSavepoint: p->db->nSavepoint);
73261 }else{
73262 return SQLITE_OK;
73263 }
73264}
73265
73266#ifndef SQLITE_OMIT_AUTOVACUUM
73267
73268/*
73269** Set the pointer-map entries for all children of page pPage. Also, if
73270** pPage contains cells that point to overflow pages, set the pointer
73271** map entries for the overflow pages as well.
73272*/
73273static int setChildPtrmaps(MemPage *pPage){
73274 int i; /* Counter variable */
73275 int nCell; /* Number of cells in page pPage */
73276 int rc; /* Return code */
73277 BtShared *pBt = pPage->pBt;
73278 Pgno pgno = pPage->pgno;
73279
73280 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73281 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
73282 if( rc!=SQLITE_OK ) return rc;
73283 nCell = pPage->nCell;
73284
73285 for(i=0; i<nCell; i++){
73286 u8 *pCell = findCell(pPage, i);
73287
73288 ptrmapPutOvflPtr(pPage, pSrc: pPage, pCell, pRC: &rc);
73289
73290 if( !pPage->leaf ){
73291 Pgno childPgno = get4byte(p: pCell);
73292 ptrmapPut(pBt, key: childPgno, PTRMAP_BTREE, parent: pgno, pRC: &rc);
73293 }
73294 }
73295
73296 if( !pPage->leaf ){
73297 Pgno childPgno = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
73298 ptrmapPut(pBt, key: childPgno, PTRMAP_BTREE, parent: pgno, pRC: &rc);
73299 }
73300
73301 return rc;
73302}
73303
73304/*
73305** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
73306** that it points to iTo. Parameter eType describes the type of pointer to
73307** be modified, as follows:
73308**
73309** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
73310** page of pPage.
73311**
73312** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
73313** page pointed to by one of the cells on pPage.
73314**
73315** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
73316** overflow page in the list.
73317*/
73318static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
73319 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73320 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73321 if( eType==PTRMAP_OVERFLOW2 ){
73322 /* The pointer is always the first 4 bytes of the page in this case. */
73323 if( get4byte(p: pPage->aData)!=iFrom ){
73324 return SQLITE_CORRUPT_PAGE(pPage);
73325 }
73326 put4byte(p: pPage->aData, v: iTo);
73327 }else{
73328 int i;
73329 int nCell;
73330 int rc;
73331
73332 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
73333 if( rc ) return rc;
73334 nCell = pPage->nCell;
73335
73336 for(i=0; i<nCell; i++){
73337 u8 *pCell = findCell(pPage, i);
73338 if( eType==PTRMAP_OVERFLOW1 ){
73339 CellInfo info;
73340 pPage->xParseCell(pPage, pCell, &info);
73341 if( info.nLocal<info.nPayload ){
73342 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
73343 return SQLITE_CORRUPT_PAGE(pPage);
73344 }
73345 if( iFrom==get4byte(p: pCell+info.nSize-4) ){
73346 put4byte(p: pCell+info.nSize-4, v: iTo);
73347 break;
73348 }
73349 }
73350 }else{
73351 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
73352 return SQLITE_CORRUPT_PAGE(pPage);
73353 }
73354 if( get4byte(p: pCell)==iFrom ){
73355 put4byte(p: pCell, v: iTo);
73356 break;
73357 }
73358 }
73359 }
73360
73361 if( i==nCell ){
73362 if( eType!=PTRMAP_BTREE ||
73363 get4byte(p: &pPage->aData[pPage->hdrOffset+8])!=iFrom ){
73364 return SQLITE_CORRUPT_PAGE(pPage);
73365 }
73366 put4byte(p: &pPage->aData[pPage->hdrOffset+8], v: iTo);
73367 }
73368 }
73369 return SQLITE_OK;
73370}
73371
73372
73373/*
73374** Move the open database page pDbPage to location iFreePage in the
73375** database. The pDbPage reference remains valid.
73376**
73377** The isCommit flag indicates that there is no need to remember that
73378** the journal needs to be sync()ed before database page pDbPage->pgno
73379** can be written to. The caller has already promised not to write to that
73380** page.
73381*/
73382static int relocatePage(
73383 BtShared *pBt, /* Btree */
73384 MemPage *pDbPage, /* Open page to move */
73385 u8 eType, /* Pointer map 'type' entry for pDbPage */
73386 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
73387 Pgno iFreePage, /* The location to move pDbPage to */
73388 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
73389){
73390 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
73391 Pgno iDbPage = pDbPage->pgno;
73392 Pager *pPager = pBt->pPager;
73393 int rc;
73394
73395 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
73396 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
73397 assert( sqlite3_mutex_held(pBt->mutex) );
73398 assert( pDbPage->pBt==pBt );
73399 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
73400
73401 /* Move page iDbPage from its current location to page number iFreePage */
73402 TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n",
73403 iDbPage, iFreePage, iPtrPage, eType));
73404 rc = sqlite3PagerMovepage(pPager, pPg: pDbPage->pDbPage, pgno: iFreePage, isCommit);
73405 if( rc!=SQLITE_OK ){
73406 return rc;
73407 }
73408 pDbPage->pgno = iFreePage;
73409
73410 /* If pDbPage was a btree-page, then it may have child pages and/or cells
73411 ** that point to overflow pages. The pointer map entries for all these
73412 ** pages need to be changed.
73413 **
73414 ** If pDbPage is an overflow page, then the first 4 bytes may store a
73415 ** pointer to a subsequent overflow page. If this is the case, then
73416 ** the pointer map needs to be updated for the subsequent overflow page.
73417 */
73418 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
73419 rc = setChildPtrmaps(pDbPage);
73420 if( rc!=SQLITE_OK ){
73421 return rc;
73422 }
73423 }else{
73424 Pgno nextOvfl = get4byte(p: pDbPage->aData);
73425 if( nextOvfl!=0 ){
73426 ptrmapPut(pBt, key: nextOvfl, PTRMAP_OVERFLOW2, parent: iFreePage, pRC: &rc);
73427 if( rc!=SQLITE_OK ){
73428 return rc;
73429 }
73430 }
73431 }
73432
73433 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
73434 ** that it points at iFreePage. Also fix the pointer map entry for
73435 ** iPtrPage.
73436 */
73437 if( eType!=PTRMAP_ROOTPAGE ){
73438 rc = btreeGetPage(pBt, pgno: iPtrPage, ppPage: &pPtrPage, flags: 0);
73439 if( rc!=SQLITE_OK ){
73440 return rc;
73441 }
73442 rc = sqlite3PagerWrite(pPg: pPtrPage->pDbPage);
73443 if( rc!=SQLITE_OK ){
73444 releasePage(pPage: pPtrPage);
73445 return rc;
73446 }
73447 rc = modifyPagePointer(pPage: pPtrPage, iFrom: iDbPage, iTo: iFreePage, eType);
73448 releasePage(pPage: pPtrPage);
73449 if( rc==SQLITE_OK ){
73450 ptrmapPut(pBt, key: iFreePage, eType, parent: iPtrPage, pRC: &rc);
73451 }
73452 }
73453 return rc;
73454}
73455
73456/* Forward declaration required by incrVacuumStep(). */
73457static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
73458
73459/*
73460** Perform a single step of an incremental-vacuum. If successful, return
73461** SQLITE_OK. If there is no work to do (and therefore no point in
73462** calling this function again), return SQLITE_DONE. Or, if an error
73463** occurs, return some other error code.
73464**
73465** More specifically, this function attempts to re-organize the database so
73466** that the last page of the file currently in use is no longer in use.
73467**
73468** Parameter nFin is the number of pages that this database would contain
73469** were this function called until it returns SQLITE_DONE.
73470**
73471** If the bCommit parameter is non-zero, this function assumes that the
73472** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
73473** or an error. bCommit is passed true for an auto-vacuum-on-commit
73474** operation, or false for an incremental vacuum.
73475*/
73476static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
73477 Pgno nFreeList; /* Number of pages still on the free-list */
73478 int rc;
73479
73480 assert( sqlite3_mutex_held(pBt->mutex) );
73481 assert( iLastPg>nFin );
73482
73483 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
73484 u8 eType;
73485 Pgno iPtrPage;
73486
73487 nFreeList = get4byte(p: &pBt->pPage1->aData[36]);
73488 if( nFreeList==0 ){
73489 return SQLITE_DONE;
73490 }
73491
73492 rc = ptrmapGet(pBt, key: iLastPg, pEType: &eType, pPgno: &iPtrPage);
73493 if( rc!=SQLITE_OK ){
73494 return rc;
73495 }
73496 if( eType==PTRMAP_ROOTPAGE ){
73497 return SQLITE_CORRUPT_BKPT;
73498 }
73499
73500 if( eType==PTRMAP_FREEPAGE ){
73501 if( bCommit==0 ){
73502 /* Remove the page from the files free-list. This is not required
73503 ** if bCommit is non-zero. In that case, the free-list will be
73504 ** truncated to zero after this function returns, so it doesn't
73505 ** matter if it still contains some garbage entries.
73506 */
73507 Pgno iFreePg;
73508 MemPage *pFreePg;
73509 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
73510 if( rc!=SQLITE_OK ){
73511 return rc;
73512 }
73513 assert( iFreePg==iLastPg );
73514 releasePage(pPage: pFreePg);
73515 }
73516 } else {
73517 Pgno iFreePg; /* Index of free page to move pLastPg to */
73518 MemPage *pLastPg;
73519 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
73520 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
73521
73522 rc = btreeGetPage(pBt, pgno: iLastPg, ppPage: &pLastPg, flags: 0);
73523 if( rc!=SQLITE_OK ){
73524 return rc;
73525 }
73526
73527 /* If bCommit is zero, this loop runs exactly once and page pLastPg
73528 ** is swapped with the first free page pulled off the free list.
73529 **
73530 ** On the other hand, if bCommit is greater than zero, then keep
73531 ** looping until a free-page located within the first nFin pages
73532 ** of the file is found.
73533 */
73534 if( bCommit==0 ){
73535 eMode = BTALLOC_LE;
73536 iNear = nFin;
73537 }
73538 do {
73539 MemPage *pFreePg;
73540 Pgno dbSize = btreePagecount(pBt);
73541 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
73542 if( rc!=SQLITE_OK ){
73543 releasePage(pPage: pLastPg);
73544 return rc;
73545 }
73546 releasePage(pPage: pFreePg);
73547 if( iFreePg>dbSize ){
73548 releasePage(pPage: pLastPg);
73549 return SQLITE_CORRUPT_BKPT;
73550 }
73551 }while( bCommit && iFreePg>nFin );
73552 assert( iFreePg<iLastPg );
73553
73554 rc = relocatePage(pBt, pDbPage: pLastPg, eType, iPtrPage, iFreePage: iFreePg, isCommit: bCommit);
73555 releasePage(pPage: pLastPg);
73556 if( rc!=SQLITE_OK ){
73557 return rc;
73558 }
73559 }
73560 }
73561
73562 if( bCommit==0 ){
73563 do {
73564 iLastPg--;
73565 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
73566 pBt->bDoTruncate = 1;
73567 pBt->nPage = iLastPg;
73568 }
73569 return SQLITE_OK;
73570}
73571
73572/*
73573** The database opened by the first argument is an auto-vacuum database
73574** nOrig pages in size containing nFree free pages. Return the expected
73575** size of the database in pages following an auto-vacuum operation.
73576*/
73577static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
73578 int nEntry; /* Number of entries on one ptrmap page */
73579 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
73580 Pgno nFin; /* Return value */
73581
73582 nEntry = pBt->usableSize/5;
73583 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
73584 nFin = nOrig - nFree - nPtrmap;
73585 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
73586 nFin--;
73587 }
73588 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
73589 nFin--;
73590 }
73591
73592 return nFin;
73593}
73594
73595/*
73596** A write-transaction must be opened before calling this function.
73597** It performs a single unit of work towards an incremental vacuum.
73598**
73599** If the incremental vacuum is finished after this function has run,
73600** SQLITE_DONE is returned. If it is not finished, but no error occurred,
73601** SQLITE_OK is returned. Otherwise an SQLite error code.
73602*/
73603SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
73604 int rc;
73605 BtShared *pBt = p->pBt;
73606
73607 sqlite3BtreeEnter(p);
73608 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
73609 if( !pBt->autoVacuum ){
73610 rc = SQLITE_DONE;
73611 }else{
73612 Pgno nOrig = btreePagecount(pBt);
73613 Pgno nFree = get4byte(p: &pBt->pPage1->aData[36]);
73614 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
73615
73616 if( nOrig<nFin || nFree>=nOrig ){
73617 rc = SQLITE_CORRUPT_BKPT;
73618 }else if( nFree>0 ){
73619 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
73620 if( rc==SQLITE_OK ){
73621 invalidateAllOverflowCache(pBt);
73622 rc = incrVacuumStep(pBt, nFin, iLastPg: nOrig, bCommit: 0);
73623 }
73624 if( rc==SQLITE_OK ){
73625 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
73626 put4byte(p: &pBt->pPage1->aData[28], v: pBt->nPage);
73627 }
73628 }else{
73629 rc = SQLITE_DONE;
73630 }
73631 }
73632 sqlite3BtreeLeave(p);
73633 return rc;
73634}
73635
73636/*
73637** This routine is called prior to sqlite3PagerCommit when a transaction
73638** is committed for an auto-vacuum database.
73639*/
73640static int autoVacuumCommit(Btree *p){
73641 int rc = SQLITE_OK;
73642 Pager *pPager;
73643 BtShared *pBt;
73644 sqlite3 *db;
73645 VVA_ONLY( int nRef );
73646
73647 assert( p!=0 );
73648 pBt = p->pBt;
73649 pPager = pBt->pPager;
73650 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
73651
73652 assert( sqlite3_mutex_held(pBt->mutex) );
73653 invalidateAllOverflowCache(pBt);
73654 assert(pBt->autoVacuum);
73655 if( !pBt->incrVacuum ){
73656 Pgno nFin; /* Number of pages in database after autovacuuming */
73657 Pgno nFree; /* Number of pages on the freelist initially */
73658 Pgno nVac; /* Number of pages to vacuum */
73659 Pgno iFree; /* The next page to be freed */
73660 Pgno nOrig; /* Database size before freeing */
73661
73662 nOrig = btreePagecount(pBt);
73663 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
73664 /* It is not possible to create a database for which the final page
73665 ** is either a pointer-map page or the pending-byte page. If one
73666 ** is encountered, this indicates corruption.
73667 */
73668 return SQLITE_CORRUPT_BKPT;
73669 }
73670
73671 nFree = get4byte(p: &pBt->pPage1->aData[36]);
73672 db = p->db;
73673 if( db->xAutovacPages ){
73674 int iDb;
73675 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
73676 if( db->aDb[iDb].pBt==p ) break;
73677 }
73678 nVac = db->xAutovacPages(
73679 db->pAutovacPagesArg,
73680 db->aDb[iDb].zDbSName,
73681 nOrig,
73682 nFree,
73683 pBt->pageSize
73684 );
73685 if( nVac>nFree ){
73686 nVac = nFree;
73687 }
73688 if( nVac==0 ){
73689 return SQLITE_OK;
73690 }
73691 }else{
73692 nVac = nFree;
73693 }
73694 nFin = finalDbSize(pBt, nOrig, nFree: nVac);
73695 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
73696 if( nFin<nOrig ){
73697 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
73698 }
73699 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
73700 rc = incrVacuumStep(pBt, nFin, iLastPg: iFree, bCommit: nVac==nFree);
73701 }
73702 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
73703 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
73704 if( nVac==nFree ){
73705 put4byte(p: &pBt->pPage1->aData[32], v: 0);
73706 put4byte(p: &pBt->pPage1->aData[36], v: 0);
73707 }
73708 put4byte(p: &pBt->pPage1->aData[28], v: nFin);
73709 pBt->bDoTruncate = 1;
73710 pBt->nPage = nFin;
73711 }
73712 if( rc!=SQLITE_OK ){
73713 sqlite3PagerRollback(pPager);
73714 }
73715 }
73716
73717 assert( nRef>=sqlite3PagerRefcount(pPager) );
73718 return rc;
73719}
73720
73721#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
73722# define setChildPtrmaps(x) SQLITE_OK
73723#endif
73724
73725/*
73726** This routine does the first phase of a two-phase commit. This routine
73727** causes a rollback journal to be created (if it does not already exist)
73728** and populated with enough information so that if a power loss occurs
73729** the database can be restored to its original state by playing back
73730** the journal. Then the contents of the journal are flushed out to
73731** the disk. After the journal is safely on oxide, the changes to the
73732** database are written into the database file and flushed to oxide.
73733** At the end of this call, the rollback journal still exists on the
73734** disk and we are still holding all locks, so the transaction has not
73735** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
73736** commit process.
73737**
73738** This call is a no-op if no write-transaction is currently active on pBt.
73739**
73740** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
73741** the name of a super-journal file that should be written into the
73742** individual journal file, or is NULL, indicating no super-journal file
73743** (single database transaction).
73744**
73745** When this is called, the super-journal should already have been
73746** created, populated with this journal pointer and synced to disk.
73747**
73748** Once this is routine has returned, the only thing required to commit
73749** the write-transaction for this database file is to delete the journal.
73750*/
73751SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
73752 int rc = SQLITE_OK;
73753 if( p->inTrans==TRANS_WRITE ){
73754 BtShared *pBt = p->pBt;
73755 sqlite3BtreeEnter(p);
73756#ifndef SQLITE_OMIT_AUTOVACUUM
73757 if( pBt->autoVacuum ){
73758 rc = autoVacuumCommit(p);
73759 if( rc!=SQLITE_OK ){
73760 sqlite3BtreeLeave(p);
73761 return rc;
73762 }
73763 }
73764 if( pBt->bDoTruncate ){
73765 sqlite3PagerTruncateImage(pPager: pBt->pPager, nPage: pBt->nPage);
73766 }
73767#endif
73768 rc = sqlite3PagerCommitPhaseOne(pPager: pBt->pPager, zSuper: zSuperJrnl, noSync: 0);
73769 sqlite3BtreeLeave(p);
73770 }
73771 return rc;
73772}
73773
73774/*
73775** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
73776** at the conclusion of a transaction.
73777*/
73778static void btreeEndTransaction(Btree *p){
73779 BtShared *pBt = p->pBt;
73780 sqlite3 *db = p->db;
73781 assert( sqlite3BtreeHoldsMutex(p) );
73782
73783#ifndef SQLITE_OMIT_AUTOVACUUM
73784 pBt->bDoTruncate = 0;
73785#endif
73786 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
73787 /* If there are other active statements that belong to this database
73788 ** handle, downgrade to a read-only transaction. The other statements
73789 ** may still be reading from the database. */
73790 downgradeAllSharedCacheTableLocks(p);
73791 p->inTrans = TRANS_READ;
73792 }else{
73793 /* If the handle had any kind of transaction open, decrement the
73794 ** transaction count of the shared btree. If the transaction count
73795 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
73796 ** call below will unlock the pager. */
73797 if( p->inTrans!=TRANS_NONE ){
73798 clearAllSharedCacheTableLocks(p);
73799 pBt->nTransaction--;
73800 if( 0==pBt->nTransaction ){
73801 pBt->inTransaction = TRANS_NONE;
73802 }
73803 }
73804
73805 /* Set the current transaction state to TRANS_NONE and unlock the
73806 ** pager if this call closed the only read or write transaction. */
73807 p->inTrans = TRANS_NONE;
73808 unlockBtreeIfUnused(pBt);
73809 }
73810
73811 btreeIntegrity(p);
73812}
73813
73814/*
73815** Commit the transaction currently in progress.
73816**
73817** This routine implements the second phase of a 2-phase commit. The
73818** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
73819** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
73820** routine did all the work of writing information out to disk and flushing the
73821** contents so that they are written onto the disk platter. All this
73822** routine has to do is delete or truncate or zero the header in the
73823** the rollback journal (which causes the transaction to commit) and
73824** drop locks.
73825**
73826** Normally, if an error occurs while the pager layer is attempting to
73827** finalize the underlying journal file, this function returns an error and
73828** the upper layer will attempt a rollback. However, if the second argument
73829** is non-zero then this b-tree transaction is part of a multi-file
73830** transaction. In this case, the transaction has already been committed
73831** (by deleting a super-journal file) and the caller will ignore this
73832** functions return code. So, even if an error occurs in the pager layer,
73833** reset the b-tree objects internal state to indicate that the write
73834** transaction has been closed. This is quite safe, as the pager will have
73835** transitioned to the error state.
73836**
73837** This will release the write lock on the database file. If there
73838** are no active cursors, it also releases the read lock.
73839*/
73840SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
73841
73842 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
73843 sqlite3BtreeEnter(p);
73844 btreeIntegrity(p);
73845
73846 /* If the handle has a write-transaction open, commit the shared-btrees
73847 ** transaction and set the shared state to TRANS_READ.
73848 */
73849 if( p->inTrans==TRANS_WRITE ){
73850 int rc;
73851 BtShared *pBt = p->pBt;
73852 assert( pBt->inTransaction==TRANS_WRITE );
73853 assert( pBt->nTransaction>0 );
73854 rc = sqlite3PagerCommitPhaseTwo(pPager: pBt->pPager);
73855 if( rc!=SQLITE_OK && bCleanup==0 ){
73856 sqlite3BtreeLeave(p);
73857 return rc;
73858 }
73859 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
73860 pBt->inTransaction = TRANS_READ;
73861 btreeClearHasContent(pBt);
73862 }
73863
73864 btreeEndTransaction(p);
73865 sqlite3BtreeLeave(p);
73866 return SQLITE_OK;
73867}
73868
73869/*
73870** Do both phases of a commit.
73871*/
73872SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
73873 int rc;
73874 sqlite3BtreeEnter(p);
73875 rc = sqlite3BtreeCommitPhaseOne(p, zSuperJrnl: 0);
73876 if( rc==SQLITE_OK ){
73877 rc = sqlite3BtreeCommitPhaseTwo(p, bCleanup: 0);
73878 }
73879 sqlite3BtreeLeave(p);
73880 return rc;
73881}
73882
73883/*
73884** This routine sets the state to CURSOR_FAULT and the error
73885** code to errCode for every cursor on any BtShared that pBtree
73886** references. Or if the writeOnly flag is set to 1, then only
73887** trip write cursors and leave read cursors unchanged.
73888**
73889** Every cursor is a candidate to be tripped, including cursors
73890** that belong to other database connections that happen to be
73891** sharing the cache with pBtree.
73892**
73893** This routine gets called when a rollback occurs. If the writeOnly
73894** flag is true, then only write-cursors need be tripped - read-only
73895** cursors save their current positions so that they may continue
73896** following the rollback. Or, if writeOnly is false, all cursors are
73897** tripped. In general, writeOnly is false if the transaction being
73898** rolled back modified the database schema. In this case b-tree root
73899** pages may be moved or deleted from the database altogether, making
73900** it unsafe for read cursors to continue.
73901**
73902** If the writeOnly flag is true and an error is encountered while
73903** saving the current position of a read-only cursor, all cursors,
73904** including all read-cursors are tripped.
73905**
73906** SQLITE_OK is returned if successful, or if an error occurs while
73907** saving a cursor position, an SQLite error code.
73908*/
73909SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
73910 BtCursor *p;
73911 int rc = SQLITE_OK;
73912
73913 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
73914 if( pBtree ){
73915 sqlite3BtreeEnter(p: pBtree);
73916 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
73917 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
73918 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
73919 rc = saveCursorPosition(pCur: p);
73920 if( rc!=SQLITE_OK ){
73921 (void)sqlite3BtreeTripAllCursors(pBtree, errCode: rc, writeOnly: 0);
73922 break;
73923 }
73924 }
73925 }else{
73926 sqlite3BtreeClearCursor(pCur: p);
73927 p->eState = CURSOR_FAULT;
73928 p->skipNext = errCode;
73929 }
73930 btreeReleaseAllCursorPages(pCur: p);
73931 }
73932 sqlite3BtreeLeave(p: pBtree);
73933 }
73934 return rc;
73935}
73936
73937/*
73938** Set the pBt->nPage field correctly, according to the current
73939** state of the database. Assume pBt->pPage1 is valid.
73940*/
73941static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
73942 int nPage = get4byte(p: &pPage1->aData[28]);
73943 testcase( nPage==0 );
73944 if( nPage==0 ) sqlite3PagerPagecount(pPager: pBt->pPager, pnPage: &nPage);
73945 testcase( pBt->nPage!=(u32)nPage );
73946 pBt->nPage = nPage;
73947}
73948
73949/*
73950** Rollback the transaction in progress.
73951**
73952** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
73953** Only write cursors are tripped if writeOnly is true but all cursors are
73954** tripped if writeOnly is false. Any attempt to use
73955** a tripped cursor will result in an error.
73956**
73957** This will release the write lock on the database file. If there
73958** are no active cursors, it also releases the read lock.
73959*/
73960SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
73961 int rc;
73962 BtShared *pBt = p->pBt;
73963 MemPage *pPage1;
73964
73965 assert( writeOnly==1 || writeOnly==0 );
73966 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
73967 sqlite3BtreeEnter(p);
73968 if( tripCode==SQLITE_OK ){
73969 rc = tripCode = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
73970 if( rc ) writeOnly = 0;
73971 }else{
73972 rc = SQLITE_OK;
73973 }
73974 if( tripCode ){
73975 int rc2 = sqlite3BtreeTripAllCursors(pBtree: p, errCode: tripCode, writeOnly);
73976 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
73977 if( rc2!=SQLITE_OK ) rc = rc2;
73978 }
73979 btreeIntegrity(p);
73980
73981 if( p->inTrans==TRANS_WRITE ){
73982 int rc2;
73983
73984 assert( TRANS_WRITE==pBt->inTransaction );
73985 rc2 = sqlite3PagerRollback(pPager: pBt->pPager);
73986 if( rc2!=SQLITE_OK ){
73987 rc = rc2;
73988 }
73989
73990 /* The rollback may have destroyed the pPage1->aData value. So
73991 ** call btreeGetPage() on page 1 again to make
73992 ** sure pPage1->aData is set correctly. */
73993 if( btreeGetPage(pBt, pgno: 1, ppPage: &pPage1, flags: 0)==SQLITE_OK ){
73994 btreeSetNPage(pBt, pPage1);
73995 releasePageOne(pPage: pPage1);
73996 }
73997 assert( countValidCursors(pBt, 1)==0 );
73998 pBt->inTransaction = TRANS_READ;
73999 btreeClearHasContent(pBt);
74000 }
74001
74002 btreeEndTransaction(p);
74003 sqlite3BtreeLeave(p);
74004 return rc;
74005}
74006
74007/*
74008** Start a statement subtransaction. The subtransaction can be rolled
74009** back independently of the main transaction. You must start a transaction
74010** before starting a subtransaction. The subtransaction is ended automatically
74011** if the main transaction commits or rolls back.
74012**
74013** Statement subtransactions are used around individual SQL statements
74014** that are contained within a BEGIN...COMMIT block. If a constraint
74015** error occurs within the statement, the effect of that one statement
74016** can be rolled back without having to rollback the entire transaction.
74017**
74018** A statement sub-transaction is implemented as an anonymous savepoint. The
74019** value passed as the second parameter is the total number of savepoints,
74020** including the new anonymous savepoint, open on the B-Tree. i.e. if there
74021** are no active savepoints and no other statement-transactions open,
74022** iStatement is 1. This anonymous savepoint can be released or rolled back
74023** using the sqlite3BtreeSavepoint() function.
74024*/
74025SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
74026 int rc;
74027 BtShared *pBt = p->pBt;
74028 sqlite3BtreeEnter(p);
74029 assert( p->inTrans==TRANS_WRITE );
74030 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
74031 assert( iStatement>0 );
74032 assert( iStatement>p->db->nSavepoint );
74033 assert( pBt->inTransaction==TRANS_WRITE );
74034 /* At the pager level, a statement transaction is a savepoint with
74035 ** an index greater than all savepoints created explicitly using
74036 ** SQL statements. It is illegal to open, release or rollback any
74037 ** such savepoints while the statement transaction savepoint is active.
74038 */
74039 rc = sqlite3PagerOpenSavepoint(pPager: pBt->pPager, nSavepoint: iStatement);
74040 sqlite3BtreeLeave(p);
74041 return rc;
74042}
74043
74044/*
74045** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
74046** or SAVEPOINT_RELEASE. This function either releases or rolls back the
74047** savepoint identified by parameter iSavepoint, depending on the value
74048** of op.
74049**
74050** Normally, iSavepoint is greater than or equal to zero. However, if op is
74051** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
74052** contents of the entire transaction are rolled back. This is different
74053** from a normal transaction rollback, as no locks are released and the
74054** transaction remains open.
74055*/
74056SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
74057 int rc = SQLITE_OK;
74058 if( p && p->inTrans==TRANS_WRITE ){
74059 BtShared *pBt = p->pBt;
74060 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
74061 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
74062 sqlite3BtreeEnter(p);
74063 if( op==SAVEPOINT_ROLLBACK ){
74064 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
74065 }
74066 if( rc==SQLITE_OK ){
74067 rc = sqlite3PagerSavepoint(pPager: pBt->pPager, op, iSavepoint);
74068 }
74069 if( rc==SQLITE_OK ){
74070 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
74071 pBt->nPage = 0;
74072 }
74073 rc = newDatabase(pBt);
74074 btreeSetNPage(pBt, pPage1: pBt->pPage1);
74075
74076 /* pBt->nPage might be zero if the database was corrupt when
74077 ** the transaction was started. Otherwise, it must be at least 1. */
74078 assert( CORRUPT_DB || pBt->nPage>0 );
74079 }
74080 sqlite3BtreeLeave(p);
74081 }
74082 return rc;
74083}
74084
74085/*
74086** Create a new cursor for the BTree whose root is on the page
74087** iTable. If a read-only cursor is requested, it is assumed that
74088** the caller already has at least a read-only transaction open
74089** on the database already. If a write-cursor is requested, then
74090** the caller is assumed to have an open write transaction.
74091**
74092** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
74093** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
74094** can be used for reading or for writing if other conditions for writing
74095** are also met. These are the conditions that must be met in order
74096** for writing to be allowed:
74097**
74098** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
74099**
74100** 2: Other database connections that share the same pager cache
74101** but which are not in the READ_UNCOMMITTED state may not have
74102** cursors open with wrFlag==0 on the same table. Otherwise
74103** the changes made by this write cursor would be visible to
74104** the read cursors in the other database connection.
74105**
74106** 3: The database must be writable (not on read-only media)
74107**
74108** 4: There must be an active transaction.
74109**
74110** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
74111** is set. If FORDELETE is set, that is a hint to the implementation that
74112** this cursor will only be used to seek to and delete entries of an index
74113** as part of a larger DELETE statement. The FORDELETE hint is not used by
74114** this implementation. But in a hypothetical alternative storage engine
74115** in which index entries are automatically deleted when corresponding table
74116** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
74117** operations on this cursor can be no-ops and all READ operations can
74118** return a null row (2-bytes: 0x01 0x00).
74119**
74120** No checking is done to make sure that page iTable really is the
74121** root page of a b-tree. If it is not, then the cursor acquired
74122** will not work correctly.
74123**
74124** It is assumed that the sqlite3BtreeCursorZero() has been called
74125** on pCur to initialize the memory space prior to invoking this routine.
74126*/
74127static int btreeCursor(
74128 Btree *p, /* The btree */
74129 Pgno iTable, /* Root page of table to open */
74130 int wrFlag, /* 1 to write. 0 read-only */
74131 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
74132 BtCursor *pCur /* Space for new cursor */
74133){
74134 BtShared *pBt = p->pBt; /* Shared b-tree handle */
74135 BtCursor *pX; /* Looping over other all cursors */
74136
74137 assert( sqlite3BtreeHoldsMutex(p) );
74138 assert( wrFlag==0
74139 || wrFlag==BTREE_WRCSR
74140 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
74141 );
74142
74143 /* The following assert statements verify that if this is a sharable
74144 ** b-tree database, the connection is holding the required table locks,
74145 ** and that no other connection has any open cursor that conflicts with
74146 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
74147 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))
74148 || iTable<1 );
74149 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
74150
74151 /* Assert that the caller has opened the required transaction. */
74152 assert( p->inTrans>TRANS_NONE );
74153 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
74154 assert( pBt->pPage1 && pBt->pPage1->aData );
74155 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
74156
74157 if( iTable<=1 ){
74158 if( iTable<1 ){
74159 return SQLITE_CORRUPT_BKPT;
74160 }else if( btreePagecount(pBt)==0 ){
74161 assert( wrFlag==0 );
74162 iTable = 0;
74163 }
74164 }
74165
74166 /* Now that no other errors can occur, finish filling in the BtCursor
74167 ** variables and link the cursor into the BtShared list. */
74168 pCur->pgnoRoot = iTable;
74169 pCur->iPage = -1;
74170 pCur->pKeyInfo = pKeyInfo;
74171 pCur->pBtree = p;
74172 pCur->pBt = pBt;
74173 pCur->curFlags = 0;
74174 /* If there are two or more cursors on the same btree, then all such
74175 ** cursors *must* have the BTCF_Multiple flag set. */
74176 for(pX=pBt->pCursor; pX; pX=pX->pNext){
74177 if( pX->pgnoRoot==iTable ){
74178 pX->curFlags |= BTCF_Multiple;
74179 pCur->curFlags = BTCF_Multiple;
74180 }
74181 }
74182 pCur->eState = CURSOR_INVALID;
74183 pCur->pNext = pBt->pCursor;
74184 pBt->pCursor = pCur;
74185 if( wrFlag ){
74186 pCur->curFlags |= BTCF_WriteFlag;
74187 pCur->curPagerFlags = 0;
74188 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
74189 }else{
74190 pCur->curPagerFlags = PAGER_GET_READONLY;
74191 }
74192 return SQLITE_OK;
74193}
74194static int btreeCursorWithLock(
74195 Btree *p, /* The btree */
74196 Pgno iTable, /* Root page of table to open */
74197 int wrFlag, /* 1 to write. 0 read-only */
74198 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
74199 BtCursor *pCur /* Space for new cursor */
74200){
74201 int rc;
74202 sqlite3BtreeEnter(p);
74203 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
74204 sqlite3BtreeLeave(p);
74205 return rc;
74206}
74207SQLITE_PRIVATE int sqlite3BtreeCursor(
74208 Btree *p, /* The btree */
74209 Pgno iTable, /* Root page of table to open */
74210 int wrFlag, /* 1 to write. 0 read-only */
74211 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
74212 BtCursor *pCur /* Write new cursor here */
74213){
74214 if( p->sharable ){
74215 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
74216 }else{
74217 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
74218 }
74219}
74220
74221/*
74222** Return the size of a BtCursor object in bytes.
74223**
74224** This interfaces is needed so that users of cursors can preallocate
74225** sufficient storage to hold a cursor. The BtCursor object is opaque
74226** to users so they cannot do the sizeof() themselves - they must call
74227** this routine.
74228*/
74229SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
74230 return ROUND8(sizeof(BtCursor));
74231}
74232
74233/*
74234** Initialize memory that will be converted into a BtCursor object.
74235**
74236** The simple approach here would be to memset() the entire object
74237** to zero. But it turns out that the apPage[] and aiIdx[] arrays
74238** do not need to be zeroed and they are large, so we can save a lot
74239** of run-time by skipping the initialization of those elements.
74240*/
74241SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
74242 memset(s: p, c: 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
74243}
74244
74245/*
74246** Close a cursor. The read lock on the database file is released
74247** when the last cursor is closed.
74248*/
74249SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
74250 Btree *pBtree = pCur->pBtree;
74251 if( pBtree ){
74252 BtShared *pBt = pCur->pBt;
74253 sqlite3BtreeEnter(p: pBtree);
74254 assert( pBt->pCursor!=0 );
74255 if( pBt->pCursor==pCur ){
74256 pBt->pCursor = pCur->pNext;
74257 }else{
74258 BtCursor *pPrev = pBt->pCursor;
74259 do{
74260 if( pPrev->pNext==pCur ){
74261 pPrev->pNext = pCur->pNext;
74262 break;
74263 }
74264 pPrev = pPrev->pNext;
74265 }while( ALWAYS(pPrev) );
74266 }
74267 btreeReleaseAllCursorPages(pCur);
74268 unlockBtreeIfUnused(pBt);
74269 sqlite3_free(p: pCur->aOverflow);
74270 sqlite3_free(p: pCur->pKey);
74271 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
74272 /* Since the BtShared is not sharable, there is no need to
74273 ** worry about the missing sqlite3BtreeLeave() call here. */
74274 assert( pBtree->sharable==0 );
74275 sqlite3BtreeClose(p: pBtree);
74276 }else{
74277 sqlite3BtreeLeave(p: pBtree);
74278 }
74279 pCur->pBtree = 0;
74280 }
74281 return SQLITE_OK;
74282}
74283
74284/*
74285** Make sure the BtCursor* given in the argument has a valid
74286** BtCursor.info structure. If it is not already valid, call
74287** btreeParseCell() to fill it in.
74288**
74289** BtCursor.info is a cache of the information in the current cell.
74290** Using this cache reduces the number of calls to btreeParseCell().
74291*/
74292#ifndef NDEBUG
74293 static int cellInfoEqual(CellInfo *a, CellInfo *b){
74294 if( a->nKey!=b->nKey ) return 0;
74295 if( a->pPayload!=b->pPayload ) return 0;
74296 if( a->nPayload!=b->nPayload ) return 0;
74297 if( a->nLocal!=b->nLocal ) return 0;
74298 if( a->nSize!=b->nSize ) return 0;
74299 return 1;
74300 }
74301 static void assertCellInfo(BtCursor *pCur){
74302 CellInfo info;
74303 memset(&info, 0, sizeof(info));
74304 btreeParseCell(pCur->pPage, pCur->ix, &info);
74305 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
74306 }
74307#else
74308 #define assertCellInfo(x)
74309#endif
74310static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
74311 if( pCur->info.nSize==0 ){
74312 pCur->curFlags |= BTCF_ValidNKey;
74313 btreeParseCell(pPage: pCur->pPage,iCell: pCur->ix,pInfo: &pCur->info);
74314 }else{
74315 assertCellInfo(pCur);
74316 }
74317}
74318
74319#ifndef NDEBUG /* The next routine used only within assert() statements */
74320/*
74321** Return true if the given BtCursor is valid. A valid cursor is one
74322** that is currently pointing to a row in a (non-empty) table.
74323** This is a verification routine is used only within assert() statements.
74324*/
74325SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
74326 return pCur && pCur->eState==CURSOR_VALID;
74327}
74328#endif /* NDEBUG */
74329SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
74330 assert( pCur!=0 );
74331 return pCur->eState==CURSOR_VALID;
74332}
74333
74334/*
74335** Return the value of the integer key or "rowid" for a table btree.
74336** This routine is only valid for a cursor that is pointing into a
74337** ordinary table btree. If the cursor points to an index btree or
74338** is invalid, the result of this routine is undefined.
74339*/
74340SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
74341 assert( cursorHoldsMutex(pCur) );
74342 assert( pCur->eState==CURSOR_VALID );
74343 assert( pCur->curIntKey );
74344 getCellInfo(pCur);
74345 return pCur->info.nKey;
74346}
74347
74348/*
74349** Pin or unpin a cursor.
74350*/
74351SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor *pCur){
74352 assert( (pCur->curFlags & BTCF_Pinned)==0 );
74353 pCur->curFlags |= BTCF_Pinned;
74354}
74355SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor *pCur){
74356 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
74357 pCur->curFlags &= ~BTCF_Pinned;
74358}
74359
74360/*
74361** Return the offset into the database file for the start of the
74362** payload to which the cursor is pointing.
74363*/
74364SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
74365 assert( cursorHoldsMutex(pCur) );
74366 assert( pCur->eState==CURSOR_VALID );
74367 getCellInfo(pCur);
74368 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
74369 (i64)(pCur->info.pPayload - pCur->pPage->aData);
74370}
74371
74372/*
74373** Return the number of bytes of payload for the entry that pCur is
74374** currently pointing to. For table btrees, this will be the amount
74375** of data. For index btrees, this will be the size of the key.
74376**
74377** The caller must guarantee that the cursor is pointing to a non-NULL
74378** valid entry. In other words, the calling procedure must guarantee
74379** that the cursor has Cursor.eState==CURSOR_VALID.
74380*/
74381SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
74382 assert( cursorHoldsMutex(pCur) );
74383 assert( pCur->eState==CURSOR_VALID );
74384 getCellInfo(pCur);
74385 return pCur->info.nPayload;
74386}
74387
74388/*
74389** Return an upper bound on the size of any record for the table
74390** that the cursor is pointing into.
74391**
74392** This is an optimization. Everything will still work if this
74393** routine always returns 2147483647 (which is the largest record
74394** that SQLite can handle) or more. But returning a smaller value might
74395** prevent large memory allocations when trying to interpret a
74396** corrupt database.
74397**
74398** The current implementation merely returns the size of the underlying
74399** database file.
74400*/
74401SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
74402 assert( cursorHoldsMutex(pCur) );
74403 assert( pCur->eState==CURSOR_VALID );
74404 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
74405}
74406
74407/*
74408** Given the page number of an overflow page in the database (parameter
74409** ovfl), this function finds the page number of the next page in the
74410** linked list of overflow pages. If possible, it uses the auto-vacuum
74411** pointer-map data instead of reading the content of page ovfl to do so.
74412**
74413** If an error occurs an SQLite error code is returned. Otherwise:
74414**
74415** The page number of the next overflow page in the linked list is
74416** written to *pPgnoNext. If page ovfl is the last page in its linked
74417** list, *pPgnoNext is set to zero.
74418**
74419** If ppPage is not NULL, and a reference to the MemPage object corresponding
74420** to page number pOvfl was obtained, then *ppPage is set to point to that
74421** reference. It is the responsibility of the caller to call releasePage()
74422** on *ppPage to free the reference. In no reference was obtained (because
74423** the pointer-map was used to obtain the value for *pPgnoNext), then
74424** *ppPage is set to zero.
74425*/
74426static int getOverflowPage(
74427 BtShared *pBt, /* The database file */
74428 Pgno ovfl, /* Current overflow page number */
74429 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
74430 Pgno *pPgnoNext /* OUT: Next overflow page number */
74431){
74432 Pgno next = 0;
74433 MemPage *pPage = 0;
74434 int rc = SQLITE_OK;
74435
74436 assert( sqlite3_mutex_held(pBt->mutex) );
74437 assert(pPgnoNext);
74438
74439#ifndef SQLITE_OMIT_AUTOVACUUM
74440 /* Try to find the next page in the overflow list using the
74441 ** autovacuum pointer-map pages. Guess that the next page in
74442 ** the overflow list is page number (ovfl+1). If that guess turns
74443 ** out to be wrong, fall back to loading the data of page
74444 ** number ovfl to determine the next page number.
74445 */
74446 if( pBt->autoVacuum ){
74447 Pgno pgno;
74448 Pgno iGuess = ovfl+1;
74449 u8 eType;
74450
74451 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
74452 iGuess++;
74453 }
74454
74455 if( iGuess<=btreePagecount(pBt) ){
74456 rc = ptrmapGet(pBt, key: iGuess, pEType: &eType, pPgno: &pgno);
74457 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
74458 next = iGuess;
74459 rc = SQLITE_DONE;
74460 }
74461 }
74462 }
74463#endif
74464
74465 assert( next==0 || rc==SQLITE_DONE );
74466 if( rc==SQLITE_OK ){
74467 rc = btreeGetPage(pBt, pgno: ovfl, ppPage: &pPage, flags: (ppPage==0) ? PAGER_GET_READONLY : 0);
74468 assert( rc==SQLITE_OK || pPage==0 );
74469 if( rc==SQLITE_OK ){
74470 next = get4byte(p: pPage->aData);
74471 }
74472 }
74473
74474 *pPgnoNext = next;
74475 if( ppPage ){
74476 *ppPage = pPage;
74477 }else{
74478 releasePage(pPage);
74479 }
74480 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
74481}
74482
74483/*
74484** Copy data from a buffer to a page, or from a page to a buffer.
74485**
74486** pPayload is a pointer to data stored on database page pDbPage.
74487** If argument eOp is false, then nByte bytes of data are copied
74488** from pPayload to the buffer pointed at by pBuf. If eOp is true,
74489** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
74490** of data are copied from the buffer pBuf to pPayload.
74491**
74492** SQLITE_OK is returned on success, otherwise an error code.
74493*/
74494static int copyPayload(
74495 void *pPayload, /* Pointer to page data */
74496 void *pBuf, /* Pointer to buffer */
74497 int nByte, /* Number of bytes to copy */
74498 int eOp, /* 0 -> copy from page, 1 -> copy to page */
74499 DbPage *pDbPage /* Page containing pPayload */
74500){
74501 if( eOp ){
74502 /* Copy data from buffer to page (a write operation) */
74503 int rc = sqlite3PagerWrite(pPg: pDbPage);
74504 if( rc!=SQLITE_OK ){
74505 return rc;
74506 }
74507 memcpy(dest: pPayload, src: pBuf, n: nByte);
74508 }else{
74509 /* Copy data from page to buffer (a read operation) */
74510 memcpy(dest: pBuf, src: pPayload, n: nByte);
74511 }
74512 return SQLITE_OK;
74513}
74514
74515/*
74516** This function is used to read or overwrite payload information
74517** for the entry that the pCur cursor is pointing to. The eOp
74518** argument is interpreted as follows:
74519**
74520** 0: The operation is a read. Populate the overflow cache.
74521** 1: The operation is a write. Populate the overflow cache.
74522**
74523** A total of "amt" bytes are read or written beginning at "offset".
74524** Data is read to or from the buffer pBuf.
74525**
74526** The content being read or written might appear on the main page
74527** or be scattered out on multiple overflow pages.
74528**
74529** If the current cursor entry uses one or more overflow pages
74530** this function may allocate space for and lazily populate
74531** the overflow page-list cache array (BtCursor.aOverflow).
74532** Subsequent calls use this cache to make seeking to the supplied offset
74533** more efficient.
74534**
74535** Once an overflow page-list cache has been allocated, it must be
74536** invalidated if some other cursor writes to the same table, or if
74537** the cursor is moved to a different row. Additionally, in auto-vacuum
74538** mode, the following events may invalidate an overflow page-list cache.
74539**
74540** * An incremental vacuum,
74541** * A commit in auto_vacuum="full" mode,
74542** * Creating a table (may require moving an overflow page).
74543*/
74544static int accessPayload(
74545 BtCursor *pCur, /* Cursor pointing to entry to read from */
74546 u32 offset, /* Begin reading this far into payload */
74547 u32 amt, /* Read this many bytes */
74548 unsigned char *pBuf, /* Write the bytes into this buffer */
74549 int eOp /* zero to read. non-zero to write. */
74550){
74551 unsigned char *aPayload;
74552 int rc = SQLITE_OK;
74553 int iIdx = 0;
74554 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
74555 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
74556#ifdef SQLITE_DIRECT_OVERFLOW_READ
74557 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
74558#endif
74559
74560 assert( pPage );
74561 assert( eOp==0 || eOp==1 );
74562 assert( pCur->eState==CURSOR_VALID );
74563 if( pCur->ix>=pPage->nCell ){
74564 return SQLITE_CORRUPT_PAGE(pPage);
74565 }
74566 assert( cursorHoldsMutex(pCur) );
74567
74568 getCellInfo(pCur);
74569 aPayload = pCur->info.pPayload;
74570 assert( offset+amt <= pCur->info.nPayload );
74571
74572 assert( aPayload > pPage->aData );
74573 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
74574 /* Trying to read or write past the end of the data is an error. The
74575 ** conditional above is really:
74576 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
74577 ** but is recast into its current form to avoid integer overflow problems
74578 */
74579 return SQLITE_CORRUPT_PAGE(pPage);
74580 }
74581
74582 /* Check if data must be read/written to/from the btree page itself. */
74583 if( offset<pCur->info.nLocal ){
74584 int a = amt;
74585 if( a+offset>pCur->info.nLocal ){
74586 a = pCur->info.nLocal - offset;
74587 }
74588 rc = copyPayload(pPayload: &aPayload[offset], pBuf, nByte: a, eOp, pDbPage: pPage->pDbPage);
74589 offset = 0;
74590 pBuf += a;
74591 amt -= a;
74592 }else{
74593 offset -= pCur->info.nLocal;
74594 }
74595
74596
74597 if( rc==SQLITE_OK && amt>0 ){
74598 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
74599 Pgno nextPage;
74600
74601 nextPage = get4byte(p: &aPayload[pCur->info.nLocal]);
74602
74603 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
74604 **
74605 ** The aOverflow[] array is sized at one entry for each overflow page
74606 ** in the overflow chain. The page number of the first overflow page is
74607 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
74608 ** means "not yet known" (the cache is lazily populated).
74609 */
74610 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
74611 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
74612 if( pCur->aOverflow==0
74613 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(p: pCur->aOverflow)
74614 ){
74615 Pgno *aNew = (Pgno*)sqlite3Realloc(
74616 pOld: pCur->aOverflow, nBytes: nOvfl*2*sizeof(Pgno)
74617 );
74618 if( aNew==0 ){
74619 return SQLITE_NOMEM_BKPT;
74620 }else{
74621 pCur->aOverflow = aNew;
74622 }
74623 }
74624 memset(s: pCur->aOverflow, c: 0, n: nOvfl*sizeof(Pgno));
74625 pCur->curFlags |= BTCF_ValidOvfl;
74626 }else{
74627 /* If the overflow page-list cache has been allocated and the
74628 ** entry for the first required overflow page is valid, skip
74629 ** directly to it.
74630 */
74631 if( pCur->aOverflow[offset/ovflSize] ){
74632 iIdx = (offset/ovflSize);
74633 nextPage = pCur->aOverflow[iIdx];
74634 offset = (offset%ovflSize);
74635 }
74636 }
74637
74638 assert( rc==SQLITE_OK && amt>0 );
74639 while( nextPage ){
74640 /* If required, populate the overflow page-list cache. */
74641 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
74642 assert( pCur->aOverflow[iIdx]==0
74643 || pCur->aOverflow[iIdx]==nextPage
74644 || CORRUPT_DB );
74645 pCur->aOverflow[iIdx] = nextPage;
74646
74647 if( offset>=ovflSize ){
74648 /* The only reason to read this page is to obtain the page
74649 ** number for the next page in the overflow chain. The page
74650 ** data is not required. So first try to lookup the overflow
74651 ** page-list cache, if any, then fall back to the getOverflowPage()
74652 ** function.
74653 */
74654 assert( pCur->curFlags & BTCF_ValidOvfl );
74655 assert( pCur->pBtree->db==pBt->db );
74656 if( pCur->aOverflow[iIdx+1] ){
74657 nextPage = pCur->aOverflow[iIdx+1];
74658 }else{
74659 rc = getOverflowPage(pBt, ovfl: nextPage, ppPage: 0, pPgnoNext: &nextPage);
74660 }
74661 offset -= ovflSize;
74662 }else{
74663 /* Need to read this page properly. It contains some of the
74664 ** range of data that is being read (eOp==0) or written (eOp!=0).
74665 */
74666 int a = amt;
74667 if( a + offset > ovflSize ){
74668 a = ovflSize - offset;
74669 }
74670
74671#ifdef SQLITE_DIRECT_OVERFLOW_READ
74672 /* If all the following are true:
74673 **
74674 ** 1) this is a read operation, and
74675 ** 2) data is required from the start of this overflow page, and
74676 ** 3) there are no dirty pages in the page-cache
74677 ** 4) the database is file-backed, and
74678 ** 5) the page is not in the WAL file
74679 ** 6) at least 4 bytes have already been read into the output buffer
74680 **
74681 ** then data can be read directly from the database file into the
74682 ** output buffer, bypassing the page-cache altogether. This speeds
74683 ** up loading large records that span many overflow pages.
74684 */
74685 if( eOp==0 /* (1) */
74686 && offset==0 /* (2) */
74687 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
74688 && &pBuf[-4]>=pBufStart /* (6) */
74689 ){
74690 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
74691 u8 aSave[4];
74692 u8 *aWrite = &pBuf[-4];
74693 assert( aWrite>=pBufStart ); /* due to (6) */
74694 memcpy(aSave, aWrite, 4);
74695 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
74696 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
74697 nextPage = get4byte(aWrite);
74698 memcpy(aWrite, aSave, 4);
74699 }else
74700#endif
74701
74702 {
74703 DbPage *pDbPage;
74704 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno: nextPage, ppPage: &pDbPage,
74705 flags: (eOp==0 ? PAGER_GET_READONLY : 0)
74706 );
74707 if( rc==SQLITE_OK ){
74708 aPayload = sqlite3PagerGetData(pPg: pDbPage);
74709 nextPage = get4byte(p: aPayload);
74710 rc = copyPayload(pPayload: &aPayload[offset+4], pBuf, nByte: a, eOp, pDbPage);
74711 sqlite3PagerUnref(pPg: pDbPage);
74712 offset = 0;
74713 }
74714 }
74715 amt -= a;
74716 if( amt==0 ) return rc;
74717 pBuf += a;
74718 }
74719 if( rc ) break;
74720 iIdx++;
74721 }
74722 }
74723
74724 if( rc==SQLITE_OK && amt>0 ){
74725 /* Overflow chain ends prematurely */
74726 return SQLITE_CORRUPT_PAGE(pPage);
74727 }
74728 return rc;
74729}
74730
74731/*
74732** Read part of the payload for the row at which that cursor pCur is currently
74733** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
74734** begins at "offset".
74735**
74736** pCur can be pointing to either a table or an index b-tree.
74737** If pointing to a table btree, then the content section is read. If
74738** pCur is pointing to an index b-tree then the key section is read.
74739**
74740** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
74741** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
74742** cursor might be invalid or might need to be restored before being read.
74743**
74744** Return SQLITE_OK on success or an error code if anything goes
74745** wrong. An error is returned if "offset+amt" is larger than
74746** the available payload.
74747*/
74748SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
74749 assert( cursorHoldsMutex(pCur) );
74750 assert( pCur->eState==CURSOR_VALID );
74751 assert( pCur->iPage>=0 && pCur->pPage );
74752 return accessPayload(pCur, offset, amt, pBuf: (unsigned char*)pBuf, eOp: 0);
74753}
74754
74755/*
74756** This variant of sqlite3BtreePayload() works even if the cursor has not
74757** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
74758** interface.
74759*/
74760#ifndef SQLITE_OMIT_INCRBLOB
74761static SQLITE_NOINLINE int accessPayloadChecked(
74762 BtCursor *pCur,
74763 u32 offset,
74764 u32 amt,
74765 void *pBuf
74766){
74767 int rc;
74768 if ( pCur->eState==CURSOR_INVALID ){
74769 return SQLITE_ABORT;
74770 }
74771 assert( cursorOwnsBtShared(pCur) );
74772 rc = btreeRestoreCursorPosition(pCur);
74773 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, eOp: 0);
74774}
74775SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
74776 if( pCur->eState==CURSOR_VALID ){
74777 assert( cursorOwnsBtShared(pCur) );
74778 return accessPayload(pCur, offset, amt, pBuf, eOp: 0);
74779 }else{
74780 return accessPayloadChecked(pCur, offset, amt, pBuf);
74781 }
74782}
74783#endif /* SQLITE_OMIT_INCRBLOB */
74784
74785/*
74786** Return a pointer to payload information from the entry that the
74787** pCur cursor is pointing to. The pointer is to the beginning of
74788** the key if index btrees (pPage->intKey==0) and is the data for
74789** table btrees (pPage->intKey==1). The number of bytes of available
74790** key/data is written into *pAmt. If *pAmt==0, then the value
74791** returned will not be a valid pointer.
74792**
74793** This routine is an optimization. It is common for the entire key
74794** and data to fit on the local page and for there to be no overflow
74795** pages. When that is so, this routine can be used to access the
74796** key and data without making a copy. If the key and/or data spills
74797** onto overflow pages, then accessPayload() must be used to reassemble
74798** the key/data and copy it into a preallocated buffer.
74799**
74800** The pointer returned by this routine looks directly into the cached
74801** page of the database. The data might change or move the next time
74802** any btree routine is called.
74803*/
74804static const void *fetchPayload(
74805 BtCursor *pCur, /* Cursor pointing to entry to read from */
74806 u32 *pAmt /* Write the number of available bytes here */
74807){
74808 int amt;
74809 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
74810 assert( pCur->eState==CURSOR_VALID );
74811 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74812 assert( cursorOwnsBtShared(pCur) );
74813 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
74814 assert( pCur->info.nSize>0 );
74815 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
74816 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
74817 amt = pCur->info.nLocal;
74818 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
74819 /* There is too little space on the page for the expected amount
74820 ** of local content. Database must be corrupt. */
74821 assert( CORRUPT_DB );
74822 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
74823 }
74824 *pAmt = (u32)amt;
74825 return (void*)pCur->info.pPayload;
74826}
74827
74828
74829/*
74830** For the entry that cursor pCur is point to, return as
74831** many bytes of the key or data as are available on the local
74832** b-tree page. Write the number of available bytes into *pAmt.
74833**
74834** The pointer returned is ephemeral. The key/data may move
74835** or be destroyed on the next call to any Btree routine,
74836** including calls from other threads against the same cache.
74837** Hence, a mutex on the BtShared should be held prior to calling
74838** this routine.
74839**
74840** These routines is used to get quick access to key and data
74841** in the common case where no overflow pages are used.
74842*/
74843SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
74844 return fetchPayload(pCur, pAmt);
74845}
74846
74847
74848/*
74849** Move the cursor down to a new child page. The newPgno argument is the
74850** page number of the child page to move to.
74851**
74852** This function returns SQLITE_CORRUPT if the page-header flags field of
74853** the new child page does not match the flags field of the parent (i.e.
74854** if an intkey page appears to be the parent of a non-intkey page, or
74855** vice-versa).
74856*/
74857static int moveToChild(BtCursor *pCur, u32 newPgno){
74858 int rc;
74859 assert( cursorOwnsBtShared(pCur) );
74860 assert( pCur->eState==CURSOR_VALID );
74861 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
74862 assert( pCur->iPage>=0 );
74863 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
74864 return SQLITE_CORRUPT_BKPT;
74865 }
74866 pCur->info.nSize = 0;
74867 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74868 pCur->aiIdx[pCur->iPage] = pCur->ix;
74869 pCur->apPage[pCur->iPage] = pCur->pPage;
74870 pCur->ix = 0;
74871 pCur->iPage++;
74872 rc = getAndInitPage(pBt: pCur->pBt, pgno: newPgno, ppPage: &pCur->pPage, bReadOnly: pCur->curPagerFlags);
74873 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
74874 if( rc==SQLITE_OK
74875 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
74876 ){
74877 releasePage(pPage: pCur->pPage);
74878 rc = SQLITE_CORRUPT_PGNO(newPgno);
74879 }
74880 if( rc ){
74881 pCur->pPage = pCur->apPage[--pCur->iPage];
74882 }
74883 return rc;
74884}
74885
74886#ifdef SQLITE_DEBUG
74887/*
74888** Page pParent is an internal (non-leaf) tree page. This function
74889** asserts that page number iChild is the left-child if the iIdx'th
74890** cell in page pParent. Or, if iIdx is equal to the total number of
74891** cells in pParent, that page number iChild is the right-child of
74892** the page.
74893*/
74894static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
74895 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
74896 ** in a corrupt database */
74897 assert( iIdx<=pParent->nCell );
74898 if( iIdx==pParent->nCell ){
74899 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
74900 }else{
74901 assert( get4byte(findCell(pParent, iIdx))==iChild );
74902 }
74903}
74904#else
74905# define assertParentIndex(x,y,z)
74906#endif
74907
74908/*
74909** Move the cursor up to the parent page.
74910**
74911** pCur->idx is set to the cell index that contains the pointer
74912** to the page we are coming from. If we are coming from the
74913** right-most child page then pCur->idx is set to one more than
74914** the largest cell index.
74915*/
74916static void moveToParent(BtCursor *pCur){
74917 MemPage *pLeaf;
74918 assert( cursorOwnsBtShared(pCur) );
74919 assert( pCur->eState==CURSOR_VALID );
74920 assert( pCur->iPage>0 );
74921 assert( pCur->pPage );
74922 assertParentIndex(
74923 pCur->apPage[pCur->iPage-1],
74924 pCur->aiIdx[pCur->iPage-1],
74925 pCur->pPage->pgno
74926 );
74927 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
74928 pCur->info.nSize = 0;
74929 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74930 pCur->ix = pCur->aiIdx[pCur->iPage-1];
74931 pLeaf = pCur->pPage;
74932 pCur->pPage = pCur->apPage[--pCur->iPage];
74933 releasePageNotNull(pPage: pLeaf);
74934}
74935
74936/*
74937** Move the cursor to point to the root page of its b-tree structure.
74938**
74939** If the table has a virtual root page, then the cursor is moved to point
74940** to the virtual root page instead of the actual root page. A table has a
74941** virtual root page when the actual root page contains no cells and a
74942** single child page. This can only happen with the table rooted at page 1.
74943**
74944** If the b-tree structure is empty, the cursor state is set to
74945** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
74946** the cursor is set to point to the first cell located on the root
74947** (or virtual root) page and the cursor state is set to CURSOR_VALID.
74948**
74949** If this function returns successfully, it may be assumed that the
74950** page-header flags indicate that the [virtual] root-page is the expected
74951** kind of b-tree page (i.e. if when opening the cursor the caller did not
74952** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
74953** indicating a table b-tree, or if the caller did specify a KeyInfo
74954** structure the flags byte is set to 0x02 or 0x0A, indicating an index
74955** b-tree).
74956*/
74957static int moveToRoot(BtCursor *pCur){
74958 MemPage *pRoot;
74959 int rc = SQLITE_OK;
74960
74961 assert( cursorOwnsBtShared(pCur) );
74962 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
74963 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
74964 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
74965 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
74966 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
74967
74968 if( pCur->iPage>=0 ){
74969 if( pCur->iPage ){
74970 releasePageNotNull(pPage: pCur->pPage);
74971 while( --pCur->iPage ){
74972 releasePageNotNull(pPage: pCur->apPage[pCur->iPage]);
74973 }
74974 pRoot = pCur->pPage = pCur->apPage[0];
74975 goto skip_init;
74976 }
74977 }else if( pCur->pgnoRoot==0 ){
74978 pCur->eState = CURSOR_INVALID;
74979 return SQLITE_EMPTY;
74980 }else{
74981 assert( pCur->iPage==(-1) );
74982 if( pCur->eState>=CURSOR_REQUIRESEEK ){
74983 if( pCur->eState==CURSOR_FAULT ){
74984 assert( pCur->skipNext!=SQLITE_OK );
74985 return pCur->skipNext;
74986 }
74987 sqlite3BtreeClearCursor(pCur);
74988 }
74989 rc = getAndInitPage(pBt: pCur->pBt, pgno: pCur->pgnoRoot, ppPage: &pCur->pPage,
74990 bReadOnly: pCur->curPagerFlags);
74991 if( rc!=SQLITE_OK ){
74992 pCur->eState = CURSOR_INVALID;
74993 return rc;
74994 }
74995 pCur->iPage = 0;
74996 pCur->curIntKey = pCur->pPage->intKey;
74997 }
74998 pRoot = pCur->pPage;
74999 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
75000
75001 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
75002 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
75003 ** NULL, the caller expects a table b-tree. If this is not the case,
75004 ** return an SQLITE_CORRUPT error.
75005 **
75006 ** Earlier versions of SQLite assumed that this test could not fail
75007 ** if the root page was already loaded when this function was called (i.e.
75008 ** if pCur->iPage>=0). But this is not so if the database is corrupted
75009 ** in such a way that page pRoot is linked into a second b-tree table
75010 ** (or the freelist). */
75011 assert( pRoot->intKey==1 || pRoot->intKey==0 );
75012 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
75013 return SQLITE_CORRUPT_PAGE(pCur->pPage);
75014 }
75015
75016skip_init:
75017 pCur->ix = 0;
75018 pCur->info.nSize = 0;
75019 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
75020
75021 if( pRoot->nCell>0 ){
75022 pCur->eState = CURSOR_VALID;
75023 }else if( !pRoot->leaf ){
75024 Pgno subpage;
75025 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
75026 subpage = get4byte(p: &pRoot->aData[pRoot->hdrOffset+8]);
75027 pCur->eState = CURSOR_VALID;
75028 rc = moveToChild(pCur, newPgno: subpage);
75029 }else{
75030 pCur->eState = CURSOR_INVALID;
75031 rc = SQLITE_EMPTY;
75032 }
75033 return rc;
75034}
75035
75036/*
75037** Move the cursor down to the left-most leaf entry beneath the
75038** entry to which it is currently pointing.
75039**
75040** The left-most leaf is the one with the smallest key - the first
75041** in ascending order.
75042*/
75043static int moveToLeftmost(BtCursor *pCur){
75044 Pgno pgno;
75045 int rc = SQLITE_OK;
75046 MemPage *pPage;
75047
75048 assert( cursorOwnsBtShared(pCur) );
75049 assert( pCur->eState==CURSOR_VALID );
75050 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
75051 assert( pCur->ix<pPage->nCell );
75052 pgno = get4byte(findCell(pPage, pCur->ix));
75053 rc = moveToChild(pCur, newPgno: pgno);
75054 }
75055 return rc;
75056}
75057
75058/*
75059** Move the cursor down to the right-most leaf entry beneath the
75060** page to which it is currently pointing. Notice the difference
75061** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
75062** finds the left-most entry beneath the *entry* whereas moveToRightmost()
75063** finds the right-most entry beneath the *page*.
75064**
75065** The right-most entry is the one with the largest key - the last
75066** key in ascending order.
75067*/
75068static int moveToRightmost(BtCursor *pCur){
75069 Pgno pgno;
75070 int rc = SQLITE_OK;
75071 MemPage *pPage = 0;
75072
75073 assert( cursorOwnsBtShared(pCur) );
75074 assert( pCur->eState==CURSOR_VALID );
75075 while( !(pPage = pCur->pPage)->leaf ){
75076 pgno = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
75077 pCur->ix = pPage->nCell;
75078 rc = moveToChild(pCur, newPgno: pgno);
75079 if( rc ) return rc;
75080 }
75081 pCur->ix = pPage->nCell-1;
75082 assert( pCur->info.nSize==0 );
75083 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
75084 return SQLITE_OK;
75085}
75086
75087/* Move the cursor to the first entry in the table. Return SQLITE_OK
75088** on success. Set *pRes to 0 if the cursor actually points to something
75089** or set *pRes to 1 if the table is empty.
75090*/
75091SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
75092 int rc;
75093
75094 assert( cursorOwnsBtShared(pCur) );
75095 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75096 rc = moveToRoot(pCur);
75097 if( rc==SQLITE_OK ){
75098 assert( pCur->pPage->nCell>0 );
75099 *pRes = 0;
75100 rc = moveToLeftmost(pCur);
75101 }else if( rc==SQLITE_EMPTY ){
75102 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
75103 *pRes = 1;
75104 rc = SQLITE_OK;
75105 }
75106 return rc;
75107}
75108
75109/* Move the cursor to the last entry in the table. Return SQLITE_OK
75110** on success. Set *pRes to 0 if the cursor actually points to something
75111** or set *pRes to 1 if the table is empty.
75112*/
75113static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){
75114 int rc = moveToRoot(pCur);
75115 if( rc==SQLITE_OK ){
75116 assert( pCur->eState==CURSOR_VALID );
75117 *pRes = 0;
75118 rc = moveToRightmost(pCur);
75119 if( rc==SQLITE_OK ){
75120 pCur->curFlags |= BTCF_AtLast;
75121 }else{
75122 pCur->curFlags &= ~BTCF_AtLast;
75123 }
75124 }else if( rc==SQLITE_EMPTY ){
75125 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75126 *pRes = 1;
75127 rc = SQLITE_OK;
75128 }
75129 return rc;
75130}
75131SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
75132 assert( cursorOwnsBtShared(pCur) );
75133 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75134
75135 /* If the cursor already points to the last entry, this is a no-op. */
75136 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
75137#ifdef SQLITE_DEBUG
75138 /* This block serves to assert() that the cursor really does point
75139 ** to the last entry in the b-tree. */
75140 int ii;
75141 for(ii=0; ii<pCur->iPage; ii++){
75142 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
75143 }
75144 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
75145 testcase( pCur->ix!=pCur->pPage->nCell-1 );
75146 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
75147 assert( pCur->pPage->leaf );
75148#endif
75149 *pRes = 0;
75150 return SQLITE_OK;
75151 }
75152 return btreeLast(pCur, pRes);
75153}
75154
75155/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
75156** table near the key intKey. Return a success code.
75157**
75158** If an exact match is not found, then the cursor is always
75159** left pointing at a leaf page which would hold the entry if it
75160** were present. The cursor might point to an entry that comes
75161** before or after the key.
75162**
75163** An integer is written into *pRes which is the result of
75164** comparing the key with the entry to which the cursor is
75165** pointing. The meaning of the integer written into
75166** *pRes is as follows:
75167**
75168** *pRes<0 The cursor is left pointing at an entry that
75169** is smaller than intKey or if the table is empty
75170** and the cursor is therefore left point to nothing.
75171**
75172** *pRes==0 The cursor is left pointing at an entry that
75173** exactly matches intKey.
75174**
75175** *pRes>0 The cursor is left pointing at an entry that
75176** is larger than intKey.
75177*/
75178SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
75179 BtCursor *pCur, /* The cursor to be moved */
75180 i64 intKey, /* The table key */
75181 int biasRight, /* If true, bias the search to the high end */
75182 int *pRes /* Write search results here */
75183){
75184 int rc;
75185
75186 assert( cursorOwnsBtShared(pCur) );
75187 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75188 assert( pRes );
75189 assert( pCur->pKeyInfo==0 );
75190 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
75191
75192 /* If the cursor is already positioned at the point we are trying
75193 ** to move to, then just return without doing any work */
75194 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
75195 if( pCur->info.nKey==intKey ){
75196 *pRes = 0;
75197 return SQLITE_OK;
75198 }
75199 if( pCur->info.nKey<intKey ){
75200 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
75201 *pRes = -1;
75202 return SQLITE_OK;
75203 }
75204 /* If the requested key is one more than the previous key, then
75205 ** try to get there using sqlite3BtreeNext() rather than a full
75206 ** binary search. This is an optimization only. The correct answer
75207 ** is still obtained without this case, only a little more slowly. */
75208 if( pCur->info.nKey+1==intKey ){
75209 *pRes = 0;
75210 rc = sqlite3BtreeNext(pCur, flags: 0);
75211 if( rc==SQLITE_OK ){
75212 getCellInfo(pCur);
75213 if( pCur->info.nKey==intKey ){
75214 return SQLITE_OK;
75215 }
75216 }else if( rc!=SQLITE_DONE ){
75217 return rc;
75218 }
75219 }
75220 }
75221 }
75222
75223#ifdef SQLITE_DEBUG
75224 pCur->pBtree->nSeek++; /* Performance measurement during testing */
75225#endif
75226
75227 rc = moveToRoot(pCur);
75228 if( rc ){
75229 if( rc==SQLITE_EMPTY ){
75230 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75231 *pRes = -1;
75232 return SQLITE_OK;
75233 }
75234 return rc;
75235 }
75236 assert( pCur->pPage );
75237 assert( pCur->pPage->isInit );
75238 assert( pCur->eState==CURSOR_VALID );
75239 assert( pCur->pPage->nCell > 0 );
75240 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
75241 assert( pCur->curIntKey );
75242
75243 for(;;){
75244 int lwr, upr, idx, c;
75245 Pgno chldPg;
75246 MemPage *pPage = pCur->pPage;
75247 u8 *pCell; /* Pointer to current cell in pPage */
75248
75249 /* pPage->nCell must be greater than zero. If this is the root-page
75250 ** the cursor would have been INVALID above and this for(;;) loop
75251 ** not run. If this is not the root-page, then the moveToChild() routine
75252 ** would have already detected db corruption. Similarly, pPage must
75253 ** be the right kind (index or table) of b-tree page. Otherwise
75254 ** a moveToChild() or moveToRoot() call would have detected corruption. */
75255 assert( pPage->nCell>0 );
75256 assert( pPage->intKey );
75257 lwr = 0;
75258 upr = pPage->nCell-1;
75259 assert( biasRight==0 || biasRight==1 );
75260 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
75261 for(;;){
75262 i64 nCellKey;
75263 pCell = findCellPastPtr(pPage, idx);
75264 if( pPage->intKeyLeaf ){
75265 while( 0x80 <= *(pCell++) ){
75266 if( pCell>=pPage->aDataEnd ){
75267 return SQLITE_CORRUPT_PAGE(pPage);
75268 }
75269 }
75270 }
75271 getVarint(p: pCell, v: (u64*)&nCellKey);
75272 if( nCellKey<intKey ){
75273 lwr = idx+1;
75274 if( lwr>upr ){ c = -1; break; }
75275 }else if( nCellKey>intKey ){
75276 upr = idx-1;
75277 if( lwr>upr ){ c = +1; break; }
75278 }else{
75279 assert( nCellKey==intKey );
75280 pCur->ix = (u16)idx;
75281 if( !pPage->leaf ){
75282 lwr = idx;
75283 goto moveto_table_next_layer;
75284 }else{
75285 pCur->curFlags |= BTCF_ValidNKey;
75286 pCur->info.nKey = nCellKey;
75287 pCur->info.nSize = 0;
75288 *pRes = 0;
75289 return SQLITE_OK;
75290 }
75291 }
75292 assert( lwr+upr>=0 );
75293 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
75294 }
75295 assert( lwr==upr+1 || !pPage->leaf );
75296 assert( pPage->isInit );
75297 if( pPage->leaf ){
75298 assert( pCur->ix<pCur->pPage->nCell );
75299 pCur->ix = (u16)idx;
75300 *pRes = c;
75301 rc = SQLITE_OK;
75302 goto moveto_table_finish;
75303 }
75304moveto_table_next_layer:
75305 if( lwr>=pPage->nCell ){
75306 chldPg = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
75307 }else{
75308 chldPg = get4byte(findCell(pPage, lwr));
75309 }
75310 pCur->ix = (u16)lwr;
75311 rc = moveToChild(pCur, newPgno: chldPg);
75312 if( rc ) break;
75313 }
75314moveto_table_finish:
75315 pCur->info.nSize = 0;
75316 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75317 return rc;
75318}
75319
75320/*
75321** Compare the "idx"-th cell on the page the cursor pCur is currently
75322** pointing to to pIdxKey using xRecordCompare. Return negative or
75323** zero if the cell is less than or equal pIdxKey. Return positive
75324** if unknown.
75325**
75326** Return value negative: Cell at pCur[idx] less than pIdxKey
75327**
75328** Return value is zero: Cell at pCur[idx] equals pIdxKey
75329**
75330** Return value positive: Nothing is known about the relationship
75331** of the cell at pCur[idx] and pIdxKey.
75332**
75333** This routine is part of an optimization. It is always safe to return
75334** a positive value as that will cause the optimization to be skipped.
75335*/
75336static int indexCellCompare(
75337 BtCursor *pCur,
75338 int idx,
75339 UnpackedRecord *pIdxKey,
75340 RecordCompare xRecordCompare
75341){
75342 MemPage *pPage = pCur->pPage;
75343 int c;
75344 int nCell; /* Size of the pCell cell in bytes */
75345 u8 *pCell = findCellPastPtr(pPage, idx);
75346
75347 nCell = pCell[0];
75348 if( nCell<=pPage->max1bytePayload ){
75349 /* This branch runs if the record-size field of the cell is a
75350 ** single byte varint and the record fits entirely on the main
75351 ** b-tree page. */
75352 testcase( pCell+nCell+1==pPage->aDataEnd );
75353 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
75354 }else if( !(pCell[1] & 0x80)
75355 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
75356 ){
75357 /* The record-size field is a 2 byte varint and the record
75358 ** fits entirely on the main b-tree page. */
75359 testcase( pCell+nCell+2==pPage->aDataEnd );
75360 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
75361 }else{
75362 /* If the record extends into overflow pages, do not attempt
75363 ** the optimization. */
75364 c = 99;
75365 }
75366 return c;
75367}
75368
75369/*
75370** Return true (non-zero) if pCur is current pointing to the last
75371** page of a table.
75372*/
75373static int cursorOnLastPage(BtCursor *pCur){
75374 int i;
75375 assert( pCur->eState==CURSOR_VALID );
75376 for(i=0; i<pCur->iPage; i++){
75377 MemPage *pPage = pCur->apPage[i];
75378 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
75379 }
75380 return 1;
75381}
75382
75383/* Move the cursor so that it points to an entry in an index table
75384** near the key pIdxKey. Return a success code.
75385**
75386** If an exact match is not found, then the cursor is always
75387** left pointing at a leaf page which would hold the entry if it
75388** were present. The cursor might point to an entry that comes
75389** before or after the key.
75390**
75391** An integer is written into *pRes which is the result of
75392** comparing the key with the entry to which the cursor is
75393** pointing. The meaning of the integer written into
75394** *pRes is as follows:
75395**
75396** *pRes<0 The cursor is left pointing at an entry that
75397** is smaller than pIdxKey or if the table is empty
75398** and the cursor is therefore left point to nothing.
75399**
75400** *pRes==0 The cursor is left pointing at an entry that
75401** exactly matches pIdxKey.
75402**
75403** *pRes>0 The cursor is left pointing at an entry that
75404** is larger than pIdxKey.
75405**
75406** The pIdxKey->eqSeen field is set to 1 if there
75407** exists an entry in the table that exactly matches pIdxKey.
75408*/
75409SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
75410 BtCursor *pCur, /* The cursor to be moved */
75411 UnpackedRecord *pIdxKey, /* Unpacked index key */
75412 int *pRes /* Write search results here */
75413){
75414 int rc;
75415 RecordCompare xRecordCompare;
75416
75417 assert( cursorOwnsBtShared(pCur) );
75418 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75419 assert( pRes );
75420 assert( pCur->pKeyInfo!=0 );
75421
75422#ifdef SQLITE_DEBUG
75423 pCur->pBtree->nSeek++; /* Performance measurement during testing */
75424#endif
75425
75426 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
75427 pIdxKey->errCode = 0;
75428 assert( pIdxKey->default_rc==1
75429 || pIdxKey->default_rc==0
75430 || pIdxKey->default_rc==-1
75431 );
75432
75433
75434 /* Check to see if we can skip a lot of work. Two cases:
75435 **
75436 ** (1) If the cursor is already pointing to the very last cell
75437 ** in the table and the pIdxKey search key is greater than or
75438 ** equal to that last cell, then no movement is required.
75439 **
75440 ** (2) If the cursor is on the last page of the table and the first
75441 ** cell on that last page is less than or equal to the pIdxKey
75442 ** search key, then we can start the search on the current page
75443 ** without needing to go back to root.
75444 */
75445 if( pCur->eState==CURSOR_VALID
75446 && pCur->pPage->leaf
75447 && cursorOnLastPage(pCur)
75448 ){
75449 int c;
75450 if( pCur->ix==pCur->pPage->nCell-1
75451 && (c = indexCellCompare(pCur, idx: pCur->ix, pIdxKey, xRecordCompare))<=0
75452 && pIdxKey->errCode==SQLITE_OK
75453 ){
75454 *pRes = c;
75455 return SQLITE_OK; /* Cursor already pointing at the correct spot */
75456 }
75457 if( pCur->iPage>0
75458 && indexCellCompare(pCur, idx: 0, pIdxKey, xRecordCompare)<=0
75459 && pIdxKey->errCode==SQLITE_OK
75460 ){
75461 pCur->curFlags &= ~BTCF_ValidOvfl;
75462 if( !pCur->pPage->isInit ){
75463 return SQLITE_CORRUPT_BKPT;
75464 }
75465 goto bypass_moveto_root; /* Start search on the current page */
75466 }
75467 pIdxKey->errCode = SQLITE_OK;
75468 }
75469
75470 rc = moveToRoot(pCur);
75471 if( rc ){
75472 if( rc==SQLITE_EMPTY ){
75473 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75474 *pRes = -1;
75475 return SQLITE_OK;
75476 }
75477 return rc;
75478 }
75479
75480bypass_moveto_root:
75481 assert( pCur->pPage );
75482 assert( pCur->pPage->isInit );
75483 assert( pCur->eState==CURSOR_VALID );
75484 assert( pCur->pPage->nCell > 0 );
75485 assert( pCur->curIntKey==0 );
75486 assert( pIdxKey!=0 );
75487 for(;;){
75488 int lwr, upr, idx, c;
75489 Pgno chldPg;
75490 MemPage *pPage = pCur->pPage;
75491 u8 *pCell; /* Pointer to current cell in pPage */
75492
75493 /* pPage->nCell must be greater than zero. If this is the root-page
75494 ** the cursor would have been INVALID above and this for(;;) loop
75495 ** not run. If this is not the root-page, then the moveToChild() routine
75496 ** would have already detected db corruption. Similarly, pPage must
75497 ** be the right kind (index or table) of b-tree page. Otherwise
75498 ** a moveToChild() or moveToRoot() call would have detected corruption. */
75499 assert( pPage->nCell>0 );
75500 assert( pPage->intKey==0 );
75501 lwr = 0;
75502 upr = pPage->nCell-1;
75503 idx = upr>>1; /* idx = (lwr+upr)/2; */
75504 for(;;){
75505 int nCell; /* Size of the pCell cell in bytes */
75506 pCell = findCellPastPtr(pPage, idx);
75507
75508 /* The maximum supported page-size is 65536 bytes. This means that
75509 ** the maximum number of record bytes stored on an index B-Tree
75510 ** page is less than 16384 bytes and may be stored as a 2-byte
75511 ** varint. This information is used to attempt to avoid parsing
75512 ** the entire cell by checking for the cases where the record is
75513 ** stored entirely within the b-tree page by inspecting the first
75514 ** 2 bytes of the cell.
75515 */
75516 nCell = pCell[0];
75517 if( nCell<=pPage->max1bytePayload ){
75518 /* This branch runs if the record-size field of the cell is a
75519 ** single byte varint and the record fits entirely on the main
75520 ** b-tree page. */
75521 testcase( pCell+nCell+1==pPage->aDataEnd );
75522 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
75523 }else if( !(pCell[1] & 0x80)
75524 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
75525 ){
75526 /* The record-size field is a 2 byte varint and the record
75527 ** fits entirely on the main b-tree page. */
75528 testcase( pCell+nCell+2==pPage->aDataEnd );
75529 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
75530 }else{
75531 /* The record flows over onto one or more overflow pages. In
75532 ** this case the whole cell needs to be parsed, a buffer allocated
75533 ** and accessPayload() used to retrieve the record into the
75534 ** buffer before VdbeRecordCompare() can be called.
75535 **
75536 ** If the record is corrupt, the xRecordCompare routine may read
75537 ** up to two varints past the end of the buffer. An extra 18
75538 ** bytes of padding is allocated at the end of the buffer in
75539 ** case this happens. */
75540 void *pCellKey;
75541 u8 * const pCellBody = pCell - pPage->childPtrSize;
75542 const int nOverrun = 18; /* Size of the overrun padding */
75543 pPage->xParseCell(pPage, pCellBody, &pCur->info);
75544 nCell = (int)pCur->info.nKey;
75545 testcase( nCell<0 ); /* True if key size is 2^32 or more */
75546 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
75547 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
75548 testcase( nCell==2 ); /* Minimum legal index key size */
75549 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
75550 rc = SQLITE_CORRUPT_PAGE(pPage);
75551 goto moveto_index_finish;
75552 }
75553 pCellKey = sqlite3Malloc( n: nCell+nOverrun );
75554 if( pCellKey==0 ){
75555 rc = SQLITE_NOMEM_BKPT;
75556 goto moveto_index_finish;
75557 }
75558 pCur->ix = (u16)idx;
75559 rc = accessPayload(pCur, offset: 0, amt: nCell, pBuf: (unsigned char*)pCellKey, eOp: 0);
75560 memset(s: ((u8*)pCellKey)+nCell,c: 0,n: nOverrun); /* Fix uninit warnings */
75561 pCur->curFlags &= ~BTCF_ValidOvfl;
75562 if( rc ){
75563 sqlite3_free(p: pCellKey);
75564 goto moveto_index_finish;
75565 }
75566 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
75567 sqlite3_free(p: pCellKey);
75568 }
75569 assert(
75570 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
75571 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
75572 );
75573 if( c<0 ){
75574 lwr = idx+1;
75575 }else if( c>0 ){
75576 upr = idx-1;
75577 }else{
75578 assert( c==0 );
75579 *pRes = 0;
75580 rc = SQLITE_OK;
75581 pCur->ix = (u16)idx;
75582 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
75583 goto moveto_index_finish;
75584 }
75585 if( lwr>upr ) break;
75586 assert( lwr+upr>=0 );
75587 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
75588 }
75589 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
75590 assert( pPage->isInit );
75591 if( pPage->leaf ){
75592 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75593 pCur->ix = (u16)idx;
75594 *pRes = c;
75595 rc = SQLITE_OK;
75596 goto moveto_index_finish;
75597 }
75598 if( lwr>=pPage->nCell ){
75599 chldPg = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
75600 }else{
75601 chldPg = get4byte(findCell(pPage, lwr));
75602 }
75603
75604 /* This block is similar to an in-lined version of:
75605 **
75606 ** pCur->ix = (u16)lwr;
75607 ** rc = moveToChild(pCur, chldPg);
75608 ** if( rc ) break;
75609 */
75610 pCur->info.nSize = 0;
75611 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75612 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
75613 return SQLITE_CORRUPT_BKPT;
75614 }
75615 pCur->aiIdx[pCur->iPage] = (u16)lwr;
75616 pCur->apPage[pCur->iPage] = pCur->pPage;
75617 pCur->ix = 0;
75618 pCur->iPage++;
75619 rc = getAndInitPage(pBt: pCur->pBt, pgno: chldPg, ppPage: &pCur->pPage, bReadOnly: pCur->curPagerFlags);
75620 if( rc==SQLITE_OK
75621 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
75622 ){
75623 releasePage(pPage: pCur->pPage);
75624 rc = SQLITE_CORRUPT_PGNO(chldPg);
75625 }
75626 if( rc ){
75627 pCur->pPage = pCur->apPage[--pCur->iPage];
75628 break;
75629 }
75630 /*
75631 ***** End of in-lined moveToChild() call */
75632 }
75633moveto_index_finish:
75634 pCur->info.nSize = 0;
75635 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75636 return rc;
75637}
75638
75639
75640/*
75641** Return TRUE if the cursor is not pointing at an entry of the table.
75642**
75643** TRUE will be returned after a call to sqlite3BtreeNext() moves
75644** past the last entry in the table or sqlite3BtreePrev() moves past
75645** the first entry. TRUE is also returned if the table is empty.
75646*/
75647SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
75648 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
75649 ** have been deleted? This API will need to change to return an error code
75650 ** as well as the boolean result value.
75651 */
75652 return (CURSOR_VALID!=pCur->eState);
75653}
75654
75655/*
75656** Return an estimate for the number of rows in the table that pCur is
75657** pointing to. Return a negative number if no estimate is currently
75658** available.
75659*/
75660SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
75661 i64 n;
75662 u8 i;
75663
75664 assert( cursorOwnsBtShared(pCur) );
75665 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75666
75667 /* Currently this interface is only called by the OP_IfSmaller
75668 ** opcode, and it that case the cursor will always be valid and
75669 ** will always point to a leaf node. */
75670 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
75671 if( NEVER(pCur->pPage->leaf==0) ) return -1;
75672
75673 n = pCur->pPage->nCell;
75674 for(i=0; i<pCur->iPage; i++){
75675 n *= pCur->apPage[i]->nCell;
75676 }
75677 return n;
75678}
75679
75680/*
75681** Advance the cursor to the next entry in the database.
75682** Return value:
75683**
75684** SQLITE_OK success
75685** SQLITE_DONE cursor is already pointing at the last element
75686** otherwise some kind of error occurred
75687**
75688** The main entry point is sqlite3BtreeNext(). That routine is optimized
75689** for the common case of merely incrementing the cell counter BtCursor.aiIdx
75690** to the next cell on the current page. The (slower) btreeNext() helper
75691** routine is called when it is necessary to move to a different page or
75692** to restore the cursor.
75693**
75694** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
75695** cursor corresponds to an SQL index and this routine could have been
75696** skipped if the SQL index had been a unique index. The F argument
75697** is a hint to the implement. SQLite btree implementation does not use
75698** this hint, but COMDB2 does.
75699*/
75700static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
75701 int rc;
75702 int idx;
75703 MemPage *pPage;
75704
75705 assert( cursorOwnsBtShared(pCur) );
75706 if( pCur->eState!=CURSOR_VALID ){
75707 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75708 rc = restoreCursorPosition(pCur);
75709 if( rc!=SQLITE_OK ){
75710 return rc;
75711 }
75712 if( CURSOR_INVALID==pCur->eState ){
75713 return SQLITE_DONE;
75714 }
75715 if( pCur->eState==CURSOR_SKIPNEXT ){
75716 pCur->eState = CURSOR_VALID;
75717 if( pCur->skipNext>0 ) return SQLITE_OK;
75718 }
75719 }
75720
75721 pPage = pCur->pPage;
75722 idx = ++pCur->ix;
75723 if( sqlite3FaultSim(iTest: 412) ) pPage->isInit = 0;
75724 if( !pPage->isInit ){
75725 return SQLITE_CORRUPT_BKPT;
75726 }
75727
75728 if( idx>=pPage->nCell ){
75729 if( !pPage->leaf ){
75730 rc = moveToChild(pCur, get4byte(p: &pPage->aData[pPage->hdrOffset+8]));
75731 if( rc ) return rc;
75732 return moveToLeftmost(pCur);
75733 }
75734 do{
75735 if( pCur->iPage==0 ){
75736 pCur->eState = CURSOR_INVALID;
75737 return SQLITE_DONE;
75738 }
75739 moveToParent(pCur);
75740 pPage = pCur->pPage;
75741 }while( pCur->ix>=pPage->nCell );
75742 if( pPage->intKey ){
75743 return sqlite3BtreeNext(pCur, flags: 0);
75744 }else{
75745 return SQLITE_OK;
75746 }
75747 }
75748 if( pPage->leaf ){
75749 return SQLITE_OK;
75750 }else{
75751 return moveToLeftmost(pCur);
75752 }
75753}
75754SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
75755 MemPage *pPage;
75756 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
75757 assert( cursorOwnsBtShared(pCur) );
75758 assert( flags==0 || flags==1 );
75759 pCur->info.nSize = 0;
75760 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75761 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
75762 pPage = pCur->pPage;
75763 if( (++pCur->ix)>=pPage->nCell ){
75764 pCur->ix--;
75765 return btreeNext(pCur);
75766 }
75767 if( pPage->leaf ){
75768 return SQLITE_OK;
75769 }else{
75770 return moveToLeftmost(pCur);
75771 }
75772}
75773
75774/*
75775** Step the cursor to the back to the previous entry in the database.
75776** Return values:
75777**
75778** SQLITE_OK success
75779** SQLITE_DONE the cursor is already on the first element of the table
75780** otherwise some kind of error occurred
75781**
75782** The main entry point is sqlite3BtreePrevious(). That routine is optimized
75783** for the common case of merely decrementing the cell counter BtCursor.aiIdx
75784** to the previous cell on the current page. The (slower) btreePrevious()
75785** helper routine is called when it is necessary to move to a different page
75786** or to restore the cursor.
75787**
75788** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
75789** the cursor corresponds to an SQL index and this routine could have been
75790** skipped if the SQL index had been a unique index. The F argument is a
75791** hint to the implement. The native SQLite btree implementation does not
75792** use this hint, but COMDB2 does.
75793*/
75794static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
75795 int rc;
75796 MemPage *pPage;
75797
75798 assert( cursorOwnsBtShared(pCur) );
75799 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
75800 assert( pCur->info.nSize==0 );
75801 if( pCur->eState!=CURSOR_VALID ){
75802 rc = restoreCursorPosition(pCur);
75803 if( rc!=SQLITE_OK ){
75804 return rc;
75805 }
75806 if( CURSOR_INVALID==pCur->eState ){
75807 return SQLITE_DONE;
75808 }
75809 if( CURSOR_SKIPNEXT==pCur->eState ){
75810 pCur->eState = CURSOR_VALID;
75811 if( pCur->skipNext<0 ) return SQLITE_OK;
75812 }
75813 }
75814
75815 pPage = pCur->pPage;
75816 assert( pPage->isInit );
75817 if( !pPage->leaf ){
75818 int idx = pCur->ix;
75819 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
75820 if( rc ) return rc;
75821 rc = moveToRightmost(pCur);
75822 }else{
75823 while( pCur->ix==0 ){
75824 if( pCur->iPage==0 ){
75825 pCur->eState = CURSOR_INVALID;
75826 return SQLITE_DONE;
75827 }
75828 moveToParent(pCur);
75829 }
75830 assert( pCur->info.nSize==0 );
75831 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
75832
75833 pCur->ix--;
75834 pPage = pCur->pPage;
75835 if( pPage->intKey && !pPage->leaf ){
75836 rc = sqlite3BtreePrevious(pCur, flags: 0);
75837 }else{
75838 rc = SQLITE_OK;
75839 }
75840 }
75841 return rc;
75842}
75843SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
75844 assert( cursorOwnsBtShared(pCur) );
75845 assert( flags==0 || flags==1 );
75846 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
75847 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
75848 pCur->info.nSize = 0;
75849 if( pCur->eState!=CURSOR_VALID
75850 || pCur->ix==0
75851 || pCur->pPage->leaf==0
75852 ){
75853 return btreePrevious(pCur);
75854 }
75855 pCur->ix--;
75856 return SQLITE_OK;
75857}
75858
75859/*
75860** Allocate a new page from the database file.
75861**
75862** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
75863** has already been called on the new page.) The new page has also
75864** been referenced and the calling routine is responsible for calling
75865** sqlite3PagerUnref() on the new page when it is done.
75866**
75867** SQLITE_OK is returned on success. Any other return value indicates
75868** an error. *ppPage is set to NULL in the event of an error.
75869**
75870** If the "nearby" parameter is not 0, then an effort is made to
75871** locate a page close to the page number "nearby". This can be used in an
75872** attempt to keep related pages close to each other in the database file,
75873** which in turn can make database access faster.
75874**
75875** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
75876** anywhere on the free-list, then it is guaranteed to be returned. If
75877** eMode is BTALLOC_LT then the page returned will be less than or equal
75878** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
75879** are no restrictions on which page is returned.
75880*/
75881static int allocateBtreePage(
75882 BtShared *pBt, /* The btree */
75883 MemPage **ppPage, /* Store pointer to the allocated page here */
75884 Pgno *pPgno, /* Store the page number here */
75885 Pgno nearby, /* Search for a page near this one */
75886 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
75887){
75888 MemPage *pPage1;
75889 int rc;
75890 u32 n; /* Number of pages on the freelist */
75891 u32 k; /* Number of leaves on the trunk of the freelist */
75892 MemPage *pTrunk = 0;
75893 MemPage *pPrevTrunk = 0;
75894 Pgno mxPage; /* Total size of the database file */
75895
75896 assert( sqlite3_mutex_held(pBt->mutex) );
75897 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
75898 pPage1 = pBt->pPage1;
75899 mxPage = btreePagecount(pBt);
75900 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
75901 ** stores the total number of pages on the freelist. */
75902 n = get4byte(p: &pPage1->aData[36]);
75903 testcase( n==mxPage-1 );
75904 if( n>=mxPage ){
75905 return SQLITE_CORRUPT_BKPT;
75906 }
75907 if( n>0 ){
75908 /* There are pages on the freelist. Reuse one of those pages. */
75909 Pgno iTrunk;
75910 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
75911 u32 nSearch = 0; /* Count of the number of search attempts */
75912
75913 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
75914 ** shows that the page 'nearby' is somewhere on the free-list, then
75915 ** the entire-list will be searched for that page.
75916 */
75917#ifndef SQLITE_OMIT_AUTOVACUUM
75918 if( eMode==BTALLOC_EXACT ){
75919 if( nearby<=mxPage ){
75920 u8 eType;
75921 assert( nearby>0 );
75922 assert( pBt->autoVacuum );
75923 rc = ptrmapGet(pBt, key: nearby, pEType: &eType, pPgno: 0);
75924 if( rc ) return rc;
75925 if( eType==PTRMAP_FREEPAGE ){
75926 searchList = 1;
75927 }
75928 }
75929 }else if( eMode==BTALLOC_LE ){
75930 searchList = 1;
75931 }
75932#endif
75933
75934 /* Decrement the free-list count by 1. Set iTrunk to the index of the
75935 ** first free-list trunk page. iPrevTrunk is initially 1.
75936 */
75937 rc = sqlite3PagerWrite(pPg: pPage1->pDbPage);
75938 if( rc ) return rc;
75939 put4byte(p: &pPage1->aData[36], v: n-1);
75940
75941 /* The code within this loop is run only once if the 'searchList' variable
75942 ** is not true. Otherwise, it runs once for each trunk-page on the
75943 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
75944 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
75945 */
75946 do {
75947 pPrevTrunk = pTrunk;
75948 if( pPrevTrunk ){
75949 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
75950 ** is the page number of the next freelist trunk page in the list or
75951 ** zero if this is the last freelist trunk page. */
75952 iTrunk = get4byte(p: &pPrevTrunk->aData[0]);
75953 }else{
75954 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
75955 ** stores the page number of the first page of the freelist, or zero if
75956 ** the freelist is empty. */
75957 iTrunk = get4byte(p: &pPage1->aData[32]);
75958 }
75959 testcase( iTrunk==mxPage );
75960 if( iTrunk>mxPage || nSearch++ > n ){
75961 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
75962 }else{
75963 rc = btreeGetUnusedPage(pBt, pgno: iTrunk, ppPage: &pTrunk, flags: 0);
75964 }
75965 if( rc ){
75966 pTrunk = 0;
75967 goto end_allocate_page;
75968 }
75969 assert( pTrunk!=0 );
75970 assert( pTrunk->aData!=0 );
75971 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
75972 ** is the number of leaf page pointers to follow. */
75973 k = get4byte(p: &pTrunk->aData[4]);
75974 if( k==0 && !searchList ){
75975 /* The trunk has no leaves and the list is not being searched.
75976 ** So extract the trunk page itself and use it as the newly
75977 ** allocated page */
75978 assert( pPrevTrunk==0 );
75979 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
75980 if( rc ){
75981 goto end_allocate_page;
75982 }
75983 *pPgno = iTrunk;
75984 memcpy(dest: &pPage1->aData[32], src: &pTrunk->aData[0], n: 4);
75985 *ppPage = pTrunk;
75986 pTrunk = 0;
75987 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
75988 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
75989 /* Value of k is out of range. Database corruption */
75990 rc = SQLITE_CORRUPT_PGNO(iTrunk);
75991 goto end_allocate_page;
75992#ifndef SQLITE_OMIT_AUTOVACUUM
75993 }else if( searchList
75994 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
75995 ){
75996 /* The list is being searched and this trunk page is the page
75997 ** to allocate, regardless of whether it has leaves.
75998 */
75999 *pPgno = iTrunk;
76000 *ppPage = pTrunk;
76001 searchList = 0;
76002 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
76003 if( rc ){
76004 goto end_allocate_page;
76005 }
76006 if( k==0 ){
76007 if( !pPrevTrunk ){
76008 memcpy(dest: &pPage1->aData[32], src: &pTrunk->aData[0], n: 4);
76009 }else{
76010 rc = sqlite3PagerWrite(pPg: pPrevTrunk->pDbPage);
76011 if( rc!=SQLITE_OK ){
76012 goto end_allocate_page;
76013 }
76014 memcpy(dest: &pPrevTrunk->aData[0], src: &pTrunk->aData[0], n: 4);
76015 }
76016 }else{
76017 /* The trunk page is required by the caller but it contains
76018 ** pointers to free-list leaves. The first leaf becomes a trunk
76019 ** page in this case.
76020 */
76021 MemPage *pNewTrunk;
76022 Pgno iNewTrunk = get4byte(p: &pTrunk->aData[8]);
76023 if( iNewTrunk>mxPage ){
76024 rc = SQLITE_CORRUPT_PGNO(iTrunk);
76025 goto end_allocate_page;
76026 }
76027 testcase( iNewTrunk==mxPage );
76028 rc = btreeGetUnusedPage(pBt, pgno: iNewTrunk, ppPage: &pNewTrunk, flags: 0);
76029 if( rc!=SQLITE_OK ){
76030 goto end_allocate_page;
76031 }
76032 rc = sqlite3PagerWrite(pPg: pNewTrunk->pDbPage);
76033 if( rc!=SQLITE_OK ){
76034 releasePage(pPage: pNewTrunk);
76035 goto end_allocate_page;
76036 }
76037 memcpy(dest: &pNewTrunk->aData[0], src: &pTrunk->aData[0], n: 4);
76038 put4byte(p: &pNewTrunk->aData[4], v: k-1);
76039 memcpy(dest: &pNewTrunk->aData[8], src: &pTrunk->aData[12], n: (k-1)*4);
76040 releasePage(pPage: pNewTrunk);
76041 if( !pPrevTrunk ){
76042 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
76043 put4byte(p: &pPage1->aData[32], v: iNewTrunk);
76044 }else{
76045 rc = sqlite3PagerWrite(pPg: pPrevTrunk->pDbPage);
76046 if( rc ){
76047 goto end_allocate_page;
76048 }
76049 put4byte(p: &pPrevTrunk->aData[0], v: iNewTrunk);
76050 }
76051 }
76052 pTrunk = 0;
76053 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
76054#endif
76055 }else if( k>0 ){
76056 /* Extract a leaf from the trunk */
76057 u32 closest;
76058 Pgno iPage;
76059 unsigned char *aData = pTrunk->aData;
76060 if( nearby>0 ){
76061 u32 i;
76062 closest = 0;
76063 if( eMode==BTALLOC_LE ){
76064 for(i=0; i<k; i++){
76065 iPage = get4byte(p: &aData[8+i*4]);
76066 if( iPage<=nearby ){
76067 closest = i;
76068 break;
76069 }
76070 }
76071 }else{
76072 int dist;
76073 dist = sqlite3AbsInt32(get4byte(p: &aData[8]) - nearby);
76074 for(i=1; i<k; i++){
76075 int d2 = sqlite3AbsInt32(get4byte(p: &aData[8+i*4]) - nearby);
76076 if( d2<dist ){
76077 closest = i;
76078 dist = d2;
76079 }
76080 }
76081 }
76082 }else{
76083 closest = 0;
76084 }
76085
76086 iPage = get4byte(p: &aData[8+closest*4]);
76087 testcase( iPage==mxPage );
76088 if( iPage>mxPage || iPage<2 ){
76089 rc = SQLITE_CORRUPT_PGNO(iTrunk);
76090 goto end_allocate_page;
76091 }
76092 testcase( iPage==mxPage );
76093 if( !searchList
76094 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
76095 ){
76096 int noContent;
76097 *pPgno = iPage;
76098 TRACE(("ALLOCATE: %u was leaf %u of %u on trunk %u"
76099 ": %u more free pages\n",
76100 *pPgno, closest+1, k, pTrunk->pgno, n-1));
76101 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
76102 if( rc ) goto end_allocate_page;
76103 if( closest<k-1 ){
76104 memcpy(dest: &aData[8+closest*4], src: &aData[4+k*4], n: 4);
76105 }
76106 put4byte(p: &aData[4], v: k-1);
76107 noContent = !btreeGetHasContent(pBt, pgno: *pPgno)? PAGER_GET_NOCONTENT : 0;
76108 rc = btreeGetUnusedPage(pBt, pgno: *pPgno, ppPage, flags: noContent);
76109 if( rc==SQLITE_OK ){
76110 rc = sqlite3PagerWrite(pPg: (*ppPage)->pDbPage);
76111 if( rc!=SQLITE_OK ){
76112 releasePage(pPage: *ppPage);
76113 *ppPage = 0;
76114 }
76115 }
76116 searchList = 0;
76117 }
76118 }
76119 releasePage(pPage: pPrevTrunk);
76120 pPrevTrunk = 0;
76121 }while( searchList );
76122 }else{
76123 /* There are no pages on the freelist, so append a new page to the
76124 ** database image.
76125 **
76126 ** Normally, new pages allocated by this block can be requested from the
76127 ** pager layer with the 'no-content' flag set. This prevents the pager
76128 ** from trying to read the pages content from disk. However, if the
76129 ** current transaction has already run one or more incremental-vacuum
76130 ** steps, then the page we are about to allocate may contain content
76131 ** that is required in the event of a rollback. In this case, do
76132 ** not set the no-content flag. This causes the pager to load and journal
76133 ** the current page content before overwriting it.
76134 **
76135 ** Note that the pager will not actually attempt to load or journal
76136 ** content for any page that really does lie past the end of the database
76137 ** file on disk. So the effects of disabling the no-content optimization
76138 ** here are confined to those pages that lie between the end of the
76139 ** database image and the end of the database file.
76140 */
76141 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
76142
76143 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
76144 if( rc ) return rc;
76145 pBt->nPage++;
76146 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
76147
76148#ifndef SQLITE_OMIT_AUTOVACUUM
76149 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
76150 /* If *pPgno refers to a pointer-map page, allocate two new pages
76151 ** at the end of the file instead of one. The first allocated page
76152 ** becomes a new pointer-map page, the second is used by the caller.
76153 */
76154 MemPage *pPg = 0;
76155 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
76156 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
76157 rc = btreeGetUnusedPage(pBt, pgno: pBt->nPage, ppPage: &pPg, flags: bNoContent);
76158 if( rc==SQLITE_OK ){
76159 rc = sqlite3PagerWrite(pPg: pPg->pDbPage);
76160 releasePage(pPage: pPg);
76161 }
76162 if( rc ) return rc;
76163 pBt->nPage++;
76164 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
76165 }
76166#endif
76167 put4byte(p: 28 + (u8*)pBt->pPage1->aData, v: pBt->nPage);
76168 *pPgno = pBt->nPage;
76169
76170 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
76171 rc = btreeGetUnusedPage(pBt, pgno: *pPgno, ppPage, flags: bNoContent);
76172 if( rc ) return rc;
76173 rc = sqlite3PagerWrite(pPg: (*ppPage)->pDbPage);
76174 if( rc!=SQLITE_OK ){
76175 releasePage(pPage: *ppPage);
76176 *ppPage = 0;
76177 }
76178 TRACE(("ALLOCATE: %u from end of file\n", *pPgno));
76179 }
76180
76181 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
76182
76183end_allocate_page:
76184 releasePage(pPage: pTrunk);
76185 releasePage(pPage: pPrevTrunk);
76186 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
76187 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
76188 return rc;
76189}
76190
76191/*
76192** This function is used to add page iPage to the database file free-list.
76193** It is assumed that the page is not already a part of the free-list.
76194**
76195** The value passed as the second argument to this function is optional.
76196** If the caller happens to have a pointer to the MemPage object
76197** corresponding to page iPage handy, it may pass it as the second value.
76198** Otherwise, it may pass NULL.
76199**
76200** If a pointer to a MemPage object is passed as the second argument,
76201** its reference count is not altered by this function.
76202*/
76203static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
76204 MemPage *pTrunk = 0; /* Free-list trunk page */
76205 Pgno iTrunk = 0; /* Page number of free-list trunk page */
76206 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
76207 MemPage *pPage; /* Page being freed. May be NULL. */
76208 int rc; /* Return Code */
76209 u32 nFree; /* Initial number of pages on free-list */
76210
76211 assert( sqlite3_mutex_held(pBt->mutex) );
76212 assert( CORRUPT_DB || iPage>1 );
76213 assert( !pMemPage || pMemPage->pgno==iPage );
76214
76215 if( iPage<2 || iPage>pBt->nPage ){
76216 return SQLITE_CORRUPT_BKPT;
76217 }
76218 if( pMemPage ){
76219 pPage = pMemPage;
76220 sqlite3PagerRef(pPg: pPage->pDbPage);
76221 }else{
76222 pPage = btreePageLookup(pBt, pgno: iPage);
76223 }
76224
76225 /* Increment the free page count on pPage1 */
76226 rc = sqlite3PagerWrite(pPg: pPage1->pDbPage);
76227 if( rc ) goto freepage_out;
76228 nFree = get4byte(p: &pPage1->aData[36]);
76229 put4byte(p: &pPage1->aData[36], v: nFree+1);
76230
76231 if( pBt->btsFlags & BTS_SECURE_DELETE ){
76232 /* If the secure_delete option is enabled, then
76233 ** always fully overwrite deleted information with zeros.
76234 */
76235 if( (!pPage && ((rc = btreeGetPage(pBt, pgno: iPage, ppPage: &pPage, flags: 0))!=0) )
76236 || ((rc = sqlite3PagerWrite(pPg: pPage->pDbPage))!=0)
76237 ){
76238 goto freepage_out;
76239 }
76240 memset(s: pPage->aData, c: 0, n: pPage->pBt->pageSize);
76241 }
76242
76243 /* If the database supports auto-vacuum, write an entry in the pointer-map
76244 ** to indicate that the page is free.
76245 */
76246 if( ISAUTOVACUUM(pBt) ){
76247 ptrmapPut(pBt, key: iPage, PTRMAP_FREEPAGE, parent: 0, pRC: &rc);
76248 if( rc ) goto freepage_out;
76249 }
76250
76251 /* Now manipulate the actual database free-list structure. There are two
76252 ** possibilities. If the free-list is currently empty, or if the first
76253 ** trunk page in the free-list is full, then this page will become a
76254 ** new free-list trunk page. Otherwise, it will become a leaf of the
76255 ** first trunk page in the current free-list. This block tests if it
76256 ** is possible to add the page as a new free-list leaf.
76257 */
76258 if( nFree!=0 ){
76259 u32 nLeaf; /* Initial number of leaf cells on trunk page */
76260
76261 iTrunk = get4byte(p: &pPage1->aData[32]);
76262 if( iTrunk>btreePagecount(pBt) ){
76263 rc = SQLITE_CORRUPT_BKPT;
76264 goto freepage_out;
76265 }
76266 rc = btreeGetPage(pBt, pgno: iTrunk, ppPage: &pTrunk, flags: 0);
76267 if( rc!=SQLITE_OK ){
76268 goto freepage_out;
76269 }
76270
76271 nLeaf = get4byte(p: &pTrunk->aData[4]);
76272 assert( pBt->usableSize>32 );
76273 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
76274 rc = SQLITE_CORRUPT_BKPT;
76275 goto freepage_out;
76276 }
76277 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
76278 /* In this case there is room on the trunk page to insert the page
76279 ** being freed as a new leaf.
76280 **
76281 ** Note that the trunk page is not really full until it contains
76282 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
76283 ** coded. But due to a coding error in versions of SQLite prior to
76284 ** 3.6.0, databases with freelist trunk pages holding more than
76285 ** usableSize/4 - 8 entries will be reported as corrupt. In order
76286 ** to maintain backwards compatibility with older versions of SQLite,
76287 ** we will continue to restrict the number of entries to usableSize/4 - 8
76288 ** for now. At some point in the future (once everyone has upgraded
76289 ** to 3.6.0 or later) we should consider fixing the conditional above
76290 ** to read "usableSize/4-2" instead of "usableSize/4-8".
76291 **
76292 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
76293 ** avoid using the last six entries in the freelist trunk page array in
76294 ** order that database files created by newer versions of SQLite can be
76295 ** read by older versions of SQLite.
76296 */
76297 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
76298 if( rc==SQLITE_OK ){
76299 put4byte(p: &pTrunk->aData[4], v: nLeaf+1);
76300 put4byte(p: &pTrunk->aData[8+nLeaf*4], v: iPage);
76301 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
76302 sqlite3PagerDontWrite(pPg: pPage->pDbPage);
76303 }
76304 rc = btreeSetHasContent(pBt, pgno: iPage);
76305 }
76306 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
76307 goto freepage_out;
76308 }
76309 }
76310
76311 /* If control flows to this point, then it was not possible to add the
76312 ** the page being freed as a leaf page of the first trunk in the free-list.
76313 ** Possibly because the free-list is empty, or possibly because the
76314 ** first trunk in the free-list is full. Either way, the page being freed
76315 ** will become the new first trunk page in the free-list.
76316 */
76317 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, pgno: iPage, ppPage: &pPage, flags: 0)) ){
76318 goto freepage_out;
76319 }
76320 rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
76321 if( rc!=SQLITE_OK ){
76322 goto freepage_out;
76323 }
76324 put4byte(p: pPage->aData, v: iTrunk);
76325 put4byte(p: &pPage->aData[4], v: 0);
76326 put4byte(p: &pPage1->aData[32], v: iPage);
76327 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
76328
76329freepage_out:
76330 if( pPage ){
76331 pPage->isInit = 0;
76332 }
76333 releasePage(pPage);
76334 releasePage(pPage: pTrunk);
76335 return rc;
76336}
76337static void freePage(MemPage *pPage, int *pRC){
76338 if( (*pRC)==SQLITE_OK ){
76339 *pRC = freePage2(pBt: pPage->pBt, pMemPage: pPage, iPage: pPage->pgno);
76340 }
76341}
76342
76343/*
76344** Free the overflow pages associated with the given Cell.
76345*/
76346static SQLITE_NOINLINE int clearCellOverflow(
76347 MemPage *pPage, /* The page that contains the Cell */
76348 unsigned char *pCell, /* First byte of the Cell */
76349 CellInfo *pInfo /* Size information about the cell */
76350){
76351 BtShared *pBt;
76352 Pgno ovflPgno;
76353 int rc;
76354 int nOvfl;
76355 u32 ovflPageSize;
76356
76357 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76358 assert( pInfo->nLocal!=pInfo->nPayload );
76359 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
76360 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
76361 if( pCell + pInfo->nSize > pPage->aDataEnd ){
76362 /* Cell extends past end of page */
76363 return SQLITE_CORRUPT_PAGE(pPage);
76364 }
76365 ovflPgno = get4byte(p: pCell + pInfo->nSize - 4);
76366 pBt = pPage->pBt;
76367 assert( pBt->usableSize > 4 );
76368 ovflPageSize = pBt->usableSize - 4;
76369 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
76370 assert( nOvfl>0 ||
76371 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
76372 );
76373 while( nOvfl-- ){
76374 Pgno iNext = 0;
76375 MemPage *pOvfl = 0;
76376 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
76377 /* 0 is not a legal page number and page 1 cannot be an
76378 ** overflow page. Therefore if ovflPgno<2 or past the end of the
76379 ** file the database must be corrupt. */
76380 return SQLITE_CORRUPT_BKPT;
76381 }
76382 if( nOvfl ){
76383 rc = getOverflowPage(pBt, ovfl: ovflPgno, ppPage: &pOvfl, pPgnoNext: &iNext);
76384 if( rc ) return rc;
76385 }
76386
76387 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, pgno: ovflPgno))!=0) )
76388 && sqlite3PagerPageRefcount(pPage: pOvfl->pDbPage)!=1
76389 ){
76390 /* There is no reason any cursor should have an outstanding reference
76391 ** to an overflow page belonging to a cell that is being deleted/updated.
76392 ** So if there exists more than one reference to this page, then it
76393 ** must not really be an overflow page and the database must be corrupt.
76394 ** It is helpful to detect this before calling freePage2(), as
76395 ** freePage2() may zero the page contents if secure-delete mode is
76396 ** enabled. If this 'overflow' page happens to be a page that the
76397 ** caller is iterating through or using in some other way, this
76398 ** can be problematic.
76399 */
76400 rc = SQLITE_CORRUPT_BKPT;
76401 }else{
76402 rc = freePage2(pBt, pMemPage: pOvfl, iPage: ovflPgno);
76403 }
76404
76405 if( pOvfl ){
76406 sqlite3PagerUnref(pPg: pOvfl->pDbPage);
76407 }
76408 if( rc ) return rc;
76409 ovflPgno = iNext;
76410 }
76411 return SQLITE_OK;
76412}
76413
76414/* Call xParseCell to compute the size of a cell. If the cell contains
76415** overflow, then invoke cellClearOverflow to clear out that overflow.
76416** Store the result code (SQLITE_OK or some error code) in rc.
76417**
76418** Implemented as macro to force inlining for performance.
76419*/
76420#define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \
76421 pPage->xParseCell(pPage, pCell, &sInfo); \
76422 if( sInfo.nLocal!=sInfo.nPayload ){ \
76423 rc = clearCellOverflow(pPage, pCell, &sInfo); \
76424 }else{ \
76425 rc = SQLITE_OK; \
76426 }
76427
76428
76429/*
76430** Create the byte sequence used to represent a cell on page pPage
76431** and write that byte sequence into pCell[]. Overflow pages are
76432** allocated and filled in as necessary. The calling procedure
76433** is responsible for making sure sufficient space has been allocated
76434** for pCell[].
76435**
76436** Note that pCell does not necessary need to point to the pPage->aData
76437** area. pCell might point to some temporary storage. The cell will
76438** be constructed in this temporary area then copied into pPage->aData
76439** later.
76440*/
76441static int fillInCell(
76442 MemPage *pPage, /* The page that contains the cell */
76443 unsigned char *pCell, /* Complete text of the cell */
76444 const BtreePayload *pX, /* Payload with which to construct the cell */
76445 int *pnSize /* Write cell size here */
76446){
76447 int nPayload;
76448 const u8 *pSrc;
76449 int nSrc, n, rc, mn;
76450 int spaceLeft;
76451 MemPage *pToRelease;
76452 unsigned char *pPrior;
76453 unsigned char *pPayload;
76454 BtShared *pBt;
76455 Pgno pgnoOvfl;
76456 int nHeader;
76457
76458 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76459
76460 /* pPage is not necessarily writeable since pCell might be auxiliary
76461 ** buffer space that is separate from the pPage buffer area */
76462 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
76463 || sqlite3PagerIswriteable(pPage->pDbPage) );
76464
76465 /* Fill in the header. */
76466 nHeader = pPage->childPtrSize;
76467 if( pPage->intKey ){
76468 nPayload = pX->nData + pX->nZero;
76469 pSrc = pX->pData;
76470 nSrc = pX->nData;
76471 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
76472 nHeader += putVarint32(&pCell[nHeader], nPayload);
76473 nHeader += putVarint(p: &pCell[nHeader], v: *(u64*)&pX->nKey);
76474 }else{
76475 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
76476 nSrc = nPayload = (int)pX->nKey;
76477 pSrc = pX->pKey;
76478 nHeader += putVarint32(&pCell[nHeader], nPayload);
76479 }
76480
76481 /* Fill in the payload */
76482 pPayload = &pCell[nHeader];
76483 if( nPayload<=pPage->maxLocal ){
76484 /* This is the common case where everything fits on the btree page
76485 ** and no overflow pages are required. */
76486 n = nHeader + nPayload;
76487 testcase( n==3 );
76488 testcase( n==4 );
76489 if( n<4 ) n = 4;
76490 *pnSize = n;
76491 assert( nSrc<=nPayload );
76492 testcase( nSrc<nPayload );
76493 memcpy(dest: pPayload, src: pSrc, n: nSrc);
76494 memset(s: pPayload+nSrc, c: 0, n: nPayload-nSrc);
76495 return SQLITE_OK;
76496 }
76497
76498 /* If we reach this point, it means that some of the content will need
76499 ** to spill onto overflow pages.
76500 */
76501 mn = pPage->minLocal;
76502 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
76503 testcase( n==pPage->maxLocal );
76504 testcase( n==pPage->maxLocal+1 );
76505 if( n > pPage->maxLocal ) n = mn;
76506 spaceLeft = n;
76507 *pnSize = n + nHeader + 4;
76508 pPrior = &pCell[nHeader+n];
76509 pToRelease = 0;
76510 pgnoOvfl = 0;
76511 pBt = pPage->pBt;
76512
76513 /* At this point variables should be set as follows:
76514 **
76515 ** nPayload Total payload size in bytes
76516 ** pPayload Begin writing payload here
76517 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
76518 ** that means content must spill into overflow pages.
76519 ** *pnSize Size of the local cell (not counting overflow pages)
76520 ** pPrior Where to write the pgno of the first overflow page
76521 **
76522 ** Use a call to btreeParseCellPtr() to verify that the values above
76523 ** were computed correctly.
76524 */
76525#ifdef SQLITE_DEBUG
76526 {
76527 CellInfo info;
76528 pPage->xParseCell(pPage, pCell, &info);
76529 assert( nHeader==(int)(info.pPayload - pCell) );
76530 assert( info.nKey==pX->nKey );
76531 assert( *pnSize == info.nSize );
76532 assert( spaceLeft == info.nLocal );
76533 }
76534#endif
76535
76536 /* Write the payload into the local Cell and any extra into overflow pages */
76537 while( 1 ){
76538 n = nPayload;
76539 if( n>spaceLeft ) n = spaceLeft;
76540
76541 /* If pToRelease is not zero than pPayload points into the data area
76542 ** of pToRelease. Make sure pToRelease is still writeable. */
76543 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
76544
76545 /* If pPayload is part of the data area of pPage, then make sure pPage
76546 ** is still writeable */
76547 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
76548 || sqlite3PagerIswriteable(pPage->pDbPage) );
76549
76550 if( nSrc>=n ){
76551 memcpy(dest: pPayload, src: pSrc, n: n);
76552 }else if( nSrc>0 ){
76553 n = nSrc;
76554 memcpy(dest: pPayload, src: pSrc, n: n);
76555 }else{
76556 memset(s: pPayload, c: 0, n: n);
76557 }
76558 nPayload -= n;
76559 if( nPayload<=0 ) break;
76560 pPayload += n;
76561 pSrc += n;
76562 nSrc -= n;
76563 spaceLeft -= n;
76564 if( spaceLeft==0 ){
76565 MemPage *pOvfl = 0;
76566#ifndef SQLITE_OMIT_AUTOVACUUM
76567 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
76568 if( pBt->autoVacuum ){
76569 do{
76570 pgnoOvfl++;
76571 } while(
76572 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
76573 );
76574 }
76575#endif
76576 rc = allocateBtreePage(pBt, ppPage: &pOvfl, pPgno: &pgnoOvfl, nearby: pgnoOvfl, eMode: 0);
76577#ifndef SQLITE_OMIT_AUTOVACUUM
76578 /* If the database supports auto-vacuum, and the second or subsequent
76579 ** overflow page is being allocated, add an entry to the pointer-map
76580 ** for that page now.
76581 **
76582 ** If this is the first overflow page, then write a partial entry
76583 ** to the pointer-map. If we write nothing to this pointer-map slot,
76584 ** then the optimistic overflow chain processing in clearCell()
76585 ** may misinterpret the uninitialized values and delete the
76586 ** wrong pages from the database.
76587 */
76588 if( pBt->autoVacuum && rc==SQLITE_OK ){
76589 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
76590 ptrmapPut(pBt, key: pgnoOvfl, eType, parent: pgnoPtrmap, pRC: &rc);
76591 if( rc ){
76592 releasePage(pPage: pOvfl);
76593 }
76594 }
76595#endif
76596 if( rc ){
76597 releasePage(pPage: pToRelease);
76598 return rc;
76599 }
76600
76601 /* If pToRelease is not zero than pPrior points into the data area
76602 ** of pToRelease. Make sure pToRelease is still writeable. */
76603 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
76604
76605 /* If pPrior is part of the data area of pPage, then make sure pPage
76606 ** is still writeable */
76607 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
76608 || sqlite3PagerIswriteable(pPage->pDbPage) );
76609
76610 put4byte(p: pPrior, v: pgnoOvfl);
76611 releasePage(pPage: pToRelease);
76612 pToRelease = pOvfl;
76613 pPrior = pOvfl->aData;
76614 put4byte(p: pPrior, v: 0);
76615 pPayload = &pOvfl->aData[4];
76616 spaceLeft = pBt->usableSize - 4;
76617 }
76618 }
76619 releasePage(pPage: pToRelease);
76620 return SQLITE_OK;
76621}
76622
76623/*
76624** Remove the i-th cell from pPage. This routine effects pPage only.
76625** The cell content is not freed or deallocated. It is assumed that
76626** the cell content has been copied someplace else. This routine just
76627** removes the reference to the cell from pPage.
76628**
76629** "sz" must be the number of bytes in the cell.
76630*/
76631static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
76632 u32 pc; /* Offset to cell content of cell being deleted */
76633 u8 *data; /* pPage->aData */
76634 u8 *ptr; /* Used to move bytes around within data[] */
76635 int rc; /* The return code */
76636 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
76637
76638 if( *pRC ) return;
76639 assert( idx>=0 );
76640 assert( idx<pPage->nCell );
76641 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
76642 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
76643 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76644 assert( pPage->nFree>=0 );
76645 data = pPage->aData;
76646 ptr = &pPage->aCellIdx[2*idx];
76647 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
76648 pc = get2byte(ptr);
76649 hdr = pPage->hdrOffset;
76650 testcase( pc==(u32)get2byte(&data[hdr+5]) );
76651 testcase( pc+sz==pPage->pBt->usableSize );
76652 if( pc+sz > pPage->pBt->usableSize ){
76653 *pRC = SQLITE_CORRUPT_BKPT;
76654 return;
76655 }
76656 rc = freeSpace(pPage, iStart: pc, iSize: sz);
76657 if( rc ){
76658 *pRC = rc;
76659 return;
76660 }
76661 pPage->nCell--;
76662 if( pPage->nCell==0 ){
76663 memset(s: &data[hdr+1], c: 0, n: 4);
76664 data[hdr+7] = 0;
76665 put2byte(&data[hdr+5], pPage->pBt->usableSize);
76666 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
76667 - pPage->childPtrSize - 8;
76668 }else{
76669 memmove(dest: ptr, src: ptr+2, n: 2*(pPage->nCell - idx));
76670 put2byte(&data[hdr+3], pPage->nCell);
76671 pPage->nFree += 2;
76672 }
76673}
76674
76675/*
76676** Insert a new cell on pPage at cell index "i". pCell points to the
76677** content of the cell.
76678**
76679** If the cell content will fit on the page, then put it there. If it
76680** will not fit, then make a copy of the cell content into pTemp if
76681** pTemp is not null. Regardless of pTemp, allocate a new entry
76682** in pPage->apOvfl[] and make it point to the cell content (either
76683** in pTemp or the original pCell) and also record its index.
76684** Allocating a new entry in pPage->aCell[] implies that
76685** pPage->nOverflow is incremented.
76686**
76687** The insertCellFast() routine below works exactly the same as
76688** insertCell() except that it lacks the pTemp and iChild parameters
76689** which are assumed zero. Other than that, the two routines are the
76690** same.
76691**
76692** Fixes or enhancements to this routine should be reflected in
76693** insertCellFast()!
76694*/
76695static int insertCell(
76696 MemPage *pPage, /* Page into which we are copying */
76697 int i, /* New cell becomes the i-th cell of the page */
76698 u8 *pCell, /* Content of the new cell */
76699 int sz, /* Bytes of content in pCell */
76700 u8 *pTemp, /* Temp storage space for pCell, if needed */
76701 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
76702){
76703 int idx = 0; /* Where to write new cell content in data[] */
76704 int j; /* Loop counter */
76705 u8 *data; /* The content of the whole page */
76706 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
76707
76708 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
76709 assert( MX_CELL(pPage->pBt)<=10921 );
76710 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
76711 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
76712 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
76713 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76714 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
76715 assert( pPage->nFree>=0 );
76716 assert( iChild>0 );
76717 if( pPage->nOverflow || sz+2>pPage->nFree ){
76718 if( pTemp ){
76719 memcpy(dest: pTemp, src: pCell, n: sz);
76720 pCell = pTemp;
76721 }
76722 put4byte(p: pCell, v: iChild);
76723 j = pPage->nOverflow++;
76724 /* Comparison against ArraySize-1 since we hold back one extra slot
76725 ** as a contingency. In other words, never need more than 3 overflow
76726 ** slots but 4 are allocated, just to be safe. */
76727 assert( j < ArraySize(pPage->apOvfl)-1 );
76728 pPage->apOvfl[j] = pCell;
76729 pPage->aiOvfl[j] = (u16)i;
76730
76731 /* When multiple overflows occur, they are always sequential and in
76732 ** sorted order. This invariants arise because multiple overflows can
76733 ** only occur when inserting divider cells into the parent page during
76734 ** balancing, and the dividers are adjacent and sorted.
76735 */
76736 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
76737 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
76738 }else{
76739 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
76740 if( NEVER(rc!=SQLITE_OK) ){
76741 return rc;
76742 }
76743 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
76744 data = pPage->aData;
76745 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
76746 rc = allocateSpace(pPage, nByte: sz, pIdx: &idx);
76747 if( rc ){ return rc; }
76748 /* The allocateSpace() routine guarantees the following properties
76749 ** if it returns successfully */
76750 assert( idx >= 0 );
76751 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
76752 assert( idx+sz <= (int)pPage->pBt->usableSize );
76753 pPage->nFree -= (u16)(2 + sz);
76754 /* In a corrupt database where an entry in the cell index section of
76755 ** a btree page has a value of 3 or less, the pCell value might point
76756 ** as many as 4 bytes in front of the start of the aData buffer for
76757 ** the source page. Make sure this does not cause problems by not
76758 ** reading the first 4 bytes */
76759 memcpy(dest: &data[idx+4], src: pCell+4, n: sz-4);
76760 put4byte(p: &data[idx], v: iChild);
76761 pIns = pPage->aCellIdx + i*2;
76762 memmove(dest: pIns+2, src: pIns, n: 2*(pPage->nCell - i));
76763 put2byte(pIns, idx);
76764 pPage->nCell++;
76765 /* increment the cell count */
76766 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
76767 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
76768#ifndef SQLITE_OMIT_AUTOVACUUM
76769 if( pPage->pBt->autoVacuum ){
76770 int rc2 = SQLITE_OK;
76771 /* The cell may contain a pointer to an overflow page. If so, write
76772 ** the entry for the overflow page into the pointer map.
76773 */
76774 ptrmapPutOvflPtr(pPage, pSrc: pPage, pCell, pRC: &rc2);
76775 if( rc2 ) return rc2;
76776 }
76777#endif
76778 }
76779 return SQLITE_OK;
76780}
76781
76782/*
76783** This variant of insertCell() assumes that the pTemp and iChild
76784** parameters are both zero. Use this variant in sqlite3BtreeInsert()
76785** for performance improvement, and also so that this variant is only
76786** called from that one place, and is thus inlined, and thus runs must
76787** faster.
76788**
76789** Fixes or enhancements to this routine should be reflected into
76790** the insertCell() routine.
76791*/
76792static int insertCellFast(
76793 MemPage *pPage, /* Page into which we are copying */
76794 int i, /* New cell becomes the i-th cell of the page */
76795 u8 *pCell, /* Content of the new cell */
76796 int sz /* Bytes of content in pCell */
76797){
76798 int idx = 0; /* Where to write new cell content in data[] */
76799 int j; /* Loop counter */
76800 u8 *data; /* The content of the whole page */
76801 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
76802
76803 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
76804 assert( MX_CELL(pPage->pBt)<=10921 );
76805 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
76806 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
76807 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
76808 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76809 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
76810 assert( pPage->nFree>=0 );
76811 assert( pPage->nOverflow==0 );
76812 if( sz+2>pPage->nFree ){
76813 j = pPage->nOverflow++;
76814 /* Comparison against ArraySize-1 since we hold back one extra slot
76815 ** as a contingency. In other words, never need more than 3 overflow
76816 ** slots but 4 are allocated, just to be safe. */
76817 assert( j < ArraySize(pPage->apOvfl)-1 );
76818 pPage->apOvfl[j] = pCell;
76819 pPage->aiOvfl[j] = (u16)i;
76820
76821 /* When multiple overflows occur, they are always sequential and in
76822 ** sorted order. This invariants arise because multiple overflows can
76823 ** only occur when inserting divider cells into the parent page during
76824 ** balancing, and the dividers are adjacent and sorted.
76825 */
76826 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
76827 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
76828 }else{
76829 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
76830 if( rc!=SQLITE_OK ){
76831 return rc;
76832 }
76833 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
76834 data = pPage->aData;
76835 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
76836 rc = allocateSpace(pPage, nByte: sz, pIdx: &idx);
76837 if( rc ){ return rc; }
76838 /* The allocateSpace() routine guarantees the following properties
76839 ** if it returns successfully */
76840 assert( idx >= 0 );
76841 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
76842 assert( idx+sz <= (int)pPage->pBt->usableSize );
76843 pPage->nFree -= (u16)(2 + sz);
76844 memcpy(dest: &data[idx], src: pCell, n: sz);
76845 pIns = pPage->aCellIdx + i*2;
76846 memmove(dest: pIns+2, src: pIns, n: 2*(pPage->nCell - i));
76847 put2byte(pIns, idx);
76848 pPage->nCell++;
76849 /* increment the cell count */
76850 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
76851 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
76852#ifndef SQLITE_OMIT_AUTOVACUUM
76853 if( pPage->pBt->autoVacuum ){
76854 int rc2 = SQLITE_OK;
76855 /* The cell may contain a pointer to an overflow page. If so, write
76856 ** the entry for the overflow page into the pointer map.
76857 */
76858 ptrmapPutOvflPtr(pPage, pSrc: pPage, pCell, pRC: &rc2);
76859 if( rc2 ) return rc2;
76860 }
76861#endif
76862 }
76863 return SQLITE_OK;
76864}
76865
76866/*
76867** The following parameters determine how many adjacent pages get involved
76868** in a balancing operation. NN is the number of neighbors on either side
76869** of the page that participate in the balancing operation. NB is the
76870** total number of pages that participate, including the target page and
76871** NN neighbors on either side.
76872**
76873** The minimum value of NN is 1 (of course). Increasing NN above 1
76874** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
76875** in exchange for a larger degradation in INSERT and UPDATE performance.
76876** The value of NN appears to give the best results overall.
76877**
76878** (Later:) The description above makes it seem as if these values are
76879** tunable - as if you could change them and recompile and it would all work.
76880** But that is unlikely. NB has been 3 since the inception of SQLite and
76881** we have never tested any other value.
76882*/
76883#define NN 1 /* Number of neighbors on either side of pPage */
76884#define NB 3 /* (NN*2+1): Total pages involved in the balance */
76885
76886/*
76887** A CellArray object contains a cache of pointers and sizes for a
76888** consecutive sequence of cells that might be held on multiple pages.
76889**
76890** The cells in this array are the divider cell or cells from the pParent
76891** page plus up to three child pages. There are a total of nCell cells.
76892**
76893** pRef is a pointer to one of the pages that contributes cells. This is
76894** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
76895** which should be common to all pages that contribute cells to this array.
76896**
76897** apCell[] and szCell[] hold, respectively, pointers to the start of each
76898** cell and the size of each cell. Some of the apCell[] pointers might refer
76899** to overflow cells. In other words, some apCel[] pointers might not point
76900** to content area of the pages.
76901**
76902** A szCell[] of zero means the size of that cell has not yet been computed.
76903**
76904** The cells come from as many as four different pages:
76905**
76906** -----------
76907** | Parent |
76908** -----------
76909** / | \
76910** / | \
76911** --------- --------- ---------
76912** |Child-1| |Child-2| |Child-3|
76913** --------- --------- ---------
76914**
76915** The order of cells is in the array is for an index btree is:
76916**
76917** 1. All cells from Child-1 in order
76918** 2. The first divider cell from Parent
76919** 3. All cells from Child-2 in order
76920** 4. The second divider cell from Parent
76921** 5. All cells from Child-3 in order
76922**
76923** For a table-btree (with rowids) the items 2 and 4 are empty because
76924** content exists only in leaves and there are no divider cells.
76925**
76926** For an index btree, the apEnd[] array holds pointer to the end of page
76927** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
76928** respectively. The ixNx[] array holds the number of cells contained in
76929** each of these 5 stages, and all stages to the left. Hence:
76930**
76931** ixNx[0] = Number of cells in Child-1.
76932** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
76933** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
76934** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
76935** ixNx[4] = Total number of cells.
76936**
76937** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
76938** are used and they point to the leaf pages only, and the ixNx value are:
76939**
76940** ixNx[0] = Number of cells in Child-1.
76941** ixNx[1] = Number of cells in Child-1 and Child-2.
76942** ixNx[2] = Total number of cells.
76943**
76944** Sometimes when deleting, a child page can have zero cells. In those
76945** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
76946** entries, shift down. The end result is that each ixNx[] entry should
76947** be larger than the previous
76948*/
76949typedef struct CellArray CellArray;
76950struct CellArray {
76951 int nCell; /* Number of cells in apCell[] */
76952 MemPage *pRef; /* Reference page */
76953 u8 **apCell; /* All cells begin balanced */
76954 u16 *szCell; /* Local size of all cells in apCell[] */
76955 u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */
76956 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */
76957};
76958
76959/*
76960** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
76961** computed.
76962*/
76963static void populateCellCache(CellArray *p, int idx, int N){
76964 MemPage *pRef = p->pRef;
76965 u16 *szCell = p->szCell;
76966 assert( idx>=0 && idx+N<=p->nCell );
76967 while( N>0 ){
76968 assert( p->apCell[idx]!=0 );
76969 if( szCell[idx]==0 ){
76970 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
76971 }else{
76972 assert( CORRUPT_DB ||
76973 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
76974 }
76975 idx++;
76976 N--;
76977 }
76978}
76979
76980/*
76981** Return the size of the Nth element of the cell array
76982*/
76983static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
76984 assert( N>=0 && N<p->nCell );
76985 assert( p->szCell[N]==0 );
76986 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
76987 return p->szCell[N];
76988}
76989static u16 cachedCellSize(CellArray *p, int N){
76990 assert( N>=0 && N<p->nCell );
76991 if( p->szCell[N] ) return p->szCell[N];
76992 return computeCellSize(p, N);
76993}
76994
76995/*
76996** Array apCell[] contains pointers to nCell b-tree page cells. The
76997** szCell[] array contains the size in bytes of each cell. This function
76998** replaces the current contents of page pPg with the contents of the cell
76999** array.
77000**
77001** Some of the cells in apCell[] may currently be stored in pPg. This
77002** function works around problems caused by this by making a copy of any
77003** such cells before overwriting the page data.
77004**
77005** The MemPage.nFree field is invalidated by this function. It is the
77006** responsibility of the caller to set it correctly.
77007*/
77008static int rebuildPage(
77009 CellArray *pCArray, /* Content to be added to page pPg */
77010 int iFirst, /* First cell in pCArray to use */
77011 int nCell, /* Final number of cells on page */
77012 MemPage *pPg /* The page to be reconstructed */
77013){
77014 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
77015 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
77016 const int usableSize = pPg->pBt->usableSize;
77017 u8 * const pEnd = &aData[usableSize];
77018 int i = iFirst; /* Which cell to copy from pCArray*/
77019 u32 j; /* Start of cell content area */
77020 int iEnd = i+nCell; /* Loop terminator */
77021 u8 *pCellptr = pPg->aCellIdx;
77022 u8 *pTmp = sqlite3PagerTempSpace(pPager: pPg->pBt->pPager);
77023 u8 *pData;
77024 int k; /* Current slot in pCArray->apEnd[] */
77025 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
77026
77027 assert( i<iEnd );
77028 j = get2byte(&aData[hdr+5]);
77029 if( NEVER(j>(u32)usableSize) ){ j = 0; }
77030 memcpy(dest: &pTmp[j], src: &aData[j], n: usableSize - j);
77031
77032 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i; k++){}
77033 pSrcEnd = pCArray->apEnd[k];
77034
77035 pData = pEnd;
77036 while( 1/*exit by break*/ ){
77037 u8 *pCell = pCArray->apCell[i];
77038 u16 sz = pCArray->szCell[i];
77039 assert( sz>0 );
77040 if( SQLITE_WITHIN(pCell,aData+j,pEnd) ){
77041 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
77042 pCell = &pTmp[pCell - aData];
77043 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
77044 && (uptr)(pCell)<(uptr)pSrcEnd
77045 ){
77046 return SQLITE_CORRUPT_BKPT;
77047 }
77048
77049 pData -= sz;
77050 put2byte(pCellptr, (pData - aData));
77051 pCellptr += 2;
77052 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
77053 memmove(dest: pData, src: pCell, n: sz);
77054 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
77055 i++;
77056 if( i>=iEnd ) break;
77057 if( pCArray->ixNx[k]<=i ){
77058 k++;
77059 pSrcEnd = pCArray->apEnd[k];
77060 }
77061 }
77062
77063 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
77064 pPg->nCell = nCell;
77065 pPg->nOverflow = 0;
77066
77067 put2byte(&aData[hdr+1], 0);
77068 put2byte(&aData[hdr+3], pPg->nCell);
77069 put2byte(&aData[hdr+5], pData - aData);
77070 aData[hdr+7] = 0x00;
77071 return SQLITE_OK;
77072}
77073
77074/*
77075** The pCArray objects contains pointers to b-tree cells and the cell sizes.
77076** This function attempts to add the cells stored in the array to page pPg.
77077** If it cannot (because the page needs to be defragmented before the cells
77078** will fit), non-zero is returned. Otherwise, if the cells are added
77079** successfully, zero is returned.
77080**
77081** Argument pCellptr points to the first entry in the cell-pointer array
77082** (part of page pPg) to populate. After cell apCell[0] is written to the
77083** page body, a 16-bit offset is written to pCellptr. And so on, for each
77084** cell in the array. It is the responsibility of the caller to ensure
77085** that it is safe to overwrite this part of the cell-pointer array.
77086**
77087** When this function is called, *ppData points to the start of the
77088** content area on page pPg. If the size of the content area is extended,
77089** *ppData is updated to point to the new start of the content area
77090** before returning.
77091**
77092** Finally, argument pBegin points to the byte immediately following the
77093** end of the space required by this page for the cell-pointer area (for
77094** all cells - not just those inserted by the current call). If the content
77095** area must be extended to before this point in order to accommodate all
77096** cells in apCell[], then the cells do not fit and non-zero is returned.
77097*/
77098static int pageInsertArray(
77099 MemPage *pPg, /* Page to add cells to */
77100 u8 *pBegin, /* End of cell-pointer array */
77101 u8 **ppData, /* IN/OUT: Page content-area pointer */
77102 u8 *pCellptr, /* Pointer to cell-pointer area */
77103 int iFirst, /* Index of first cell to add */
77104 int nCell, /* Number of cells to add to pPg */
77105 CellArray *pCArray /* Array of cells */
77106){
77107 int i = iFirst; /* Loop counter - cell index to insert */
77108 u8 *aData = pPg->aData; /* Complete page */
77109 u8 *pData = *ppData; /* Content area. A subset of aData[] */
77110 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
77111 int k; /* Current slot in pCArray->apEnd[] */
77112 u8 *pEnd; /* Maximum extent of cell data */
77113 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
77114 if( iEnd<=iFirst ) return 0;
77115 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i ; k++){}
77116 pEnd = pCArray->apEnd[k];
77117 while( 1 /*Exit by break*/ ){
77118 int sz, rc;
77119 u8 *pSlot;
77120 assert( pCArray->szCell[i]!=0 );
77121 sz = pCArray->szCell[i];
77122 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,nByte: sz,pRc: &rc))==0 ){
77123 if( (pData - pBegin)<sz ) return 1;
77124 pData -= sz;
77125 pSlot = pData;
77126 }
77127 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
77128 ** database. But they might for a corrupt database. Hence use memmove()
77129 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
77130 assert( (pSlot+sz)<=pCArray->apCell[i]
77131 || pSlot>=(pCArray->apCell[i]+sz)
77132 || CORRUPT_DB );
77133 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
77134 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
77135 ){
77136 assert( CORRUPT_DB );
77137 (void)SQLITE_CORRUPT_BKPT;
77138 return 1;
77139 }
77140 memmove(dest: pSlot, src: pCArray->apCell[i], n: sz);
77141 put2byte(pCellptr, (pSlot - aData));
77142 pCellptr += 2;
77143 i++;
77144 if( i>=iEnd ) break;
77145 if( pCArray->ixNx[k]<=i ){
77146 k++;
77147 pEnd = pCArray->apEnd[k];
77148 }
77149 }
77150 *ppData = pData;
77151 return 0;
77152}
77153
77154/*
77155** The pCArray object contains pointers to b-tree cells and their sizes.
77156**
77157** This function adds the space associated with each cell in the array
77158** that is currently stored within the body of pPg to the pPg free-list.
77159** The cell-pointers and other fields of the page are not updated.
77160**
77161** This function returns the total number of cells added to the free-list.
77162*/
77163static int pageFreeArray(
77164 MemPage *pPg, /* Page to edit */
77165 int iFirst, /* First cell to delete */
77166 int nCell, /* Cells to delete */
77167 CellArray *pCArray /* Array of cells */
77168){
77169 u8 * const aData = pPg->aData;
77170 u8 * const pEnd = &aData[pPg->pBt->usableSize];
77171 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
77172 int nRet = 0;
77173 int i, j;
77174 int iEnd = iFirst + nCell;
77175 int nFree = 0;
77176 int aOfst[10];
77177 int aAfter[10];
77178
77179 for(i=iFirst; i<iEnd; i++){
77180 u8 *pCell = pCArray->apCell[i];
77181 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
77182 int sz;
77183 int iAfter;
77184 int iOfst;
77185 /* No need to use cachedCellSize() here. The sizes of all cells that
77186 ** are to be freed have already been computing while deciding which
77187 ** cells need freeing */
77188 sz = pCArray->szCell[i]; assert( sz>0 );
77189 iOfst = (u16)(pCell - aData);
77190 iAfter = iOfst+sz;
77191 for(j=0; j<nFree; j++){
77192 if( aOfst[j]==iAfter ){
77193 aOfst[j] = iOfst;
77194 break;
77195 }else if( aAfter[j]==iOfst ){
77196 aAfter[j] = iAfter;
77197 break;
77198 }
77199 }
77200 if( j>=nFree ){
77201 if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){
77202 for(j=0; j<nFree; j++){
77203 freeSpace(pPage: pPg, iStart: aOfst[j], iSize: aAfter[j]-aOfst[j]);
77204 }
77205 nFree = 0;
77206 }
77207 aOfst[nFree] = iOfst;
77208 aAfter[nFree] = iAfter;
77209 if( &aData[iAfter]>pEnd ) return 0;
77210 nFree++;
77211 }
77212 nRet++;
77213 }
77214 }
77215 for(j=0; j<nFree; j++){
77216 freeSpace(pPage: pPg, iStart: aOfst[j], iSize: aAfter[j]-aOfst[j]);
77217 }
77218 return nRet;
77219}
77220
77221/*
77222** pCArray contains pointers to and sizes of all cells in the page being
77223** balanced. The current page, pPg, has pPg->nCell cells starting with
77224** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
77225** starting at apCell[iNew].
77226**
77227** This routine makes the necessary adjustments to pPg so that it contains
77228** the correct cells after being balanced.
77229**
77230** The pPg->nFree field is invalid when this function returns. It is the
77231** responsibility of the caller to set it correctly.
77232*/
77233static int editPage(
77234 MemPage *pPg, /* Edit this page */
77235 int iOld, /* Index of first cell currently on page */
77236 int iNew, /* Index of new first cell on page */
77237 int nNew, /* Final number of cells on page */
77238 CellArray *pCArray /* Array of cells and sizes */
77239){
77240 u8 * const aData = pPg->aData;
77241 const int hdr = pPg->hdrOffset;
77242 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
77243 int nCell = pPg->nCell; /* Cells stored on pPg */
77244 u8 *pData;
77245 u8 *pCellptr;
77246 int i;
77247 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
77248 int iNewEnd = iNew + nNew;
77249
77250#ifdef SQLITE_DEBUG
77251 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
77252 memcpy(pTmp, aData, pPg->pBt->usableSize);
77253#endif
77254
77255 /* Remove cells from the start and end of the page */
77256 assert( nCell>=0 );
77257 if( iOld<iNew ){
77258 int nShift = pageFreeArray(pPg, iFirst: iOld, nCell: iNew-iOld, pCArray);
77259 if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
77260 memmove(dest: pPg->aCellIdx, src: &pPg->aCellIdx[nShift*2], n: nCell*2);
77261 nCell -= nShift;
77262 }
77263 if( iNewEnd < iOldEnd ){
77264 int nTail = pageFreeArray(pPg, iFirst: iNewEnd, nCell: iOldEnd - iNewEnd, pCArray);
77265 assert( nCell>=nTail );
77266 nCell -= nTail;
77267 }
77268
77269 pData = &aData[get2byte(&aData[hdr+5])];
77270 if( pData<pBegin ) goto editpage_fail;
77271 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
77272
77273 /* Add cells to the start of the page */
77274 if( iNew<iOld ){
77275 int nAdd = MIN(nNew,iOld-iNew);
77276 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
77277 assert( nAdd>=0 );
77278 pCellptr = pPg->aCellIdx;
77279 memmove(dest: &pCellptr[nAdd*2], src: pCellptr, n: nCell*2);
77280 if( pageInsertArray(
77281 pPg, pBegin, ppData: &pData, pCellptr,
77282 iFirst: iNew, nCell: nAdd, pCArray
77283 ) ) goto editpage_fail;
77284 nCell += nAdd;
77285 }
77286
77287 /* Add any overflow cells */
77288 for(i=0; i<pPg->nOverflow; i++){
77289 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
77290 if( iCell>=0 && iCell<nNew ){
77291 pCellptr = &pPg->aCellIdx[iCell * 2];
77292 if( nCell>iCell ){
77293 memmove(dest: &pCellptr[2], src: pCellptr, n: (nCell - iCell) * 2);
77294 }
77295 nCell++;
77296 cachedCellSize(p: pCArray, N: iCell+iNew);
77297 if( pageInsertArray(
77298 pPg, pBegin, ppData: &pData, pCellptr,
77299 iFirst: iCell+iNew, nCell: 1, pCArray
77300 ) ) goto editpage_fail;
77301 }
77302 }
77303
77304 /* Append cells to the end of the page */
77305 assert( nCell>=0 );
77306 pCellptr = &pPg->aCellIdx[nCell*2];
77307 if( pageInsertArray(
77308 pPg, pBegin, ppData: &pData, pCellptr,
77309 iFirst: iNew+nCell, nCell: nNew-nCell, pCArray
77310 ) ) goto editpage_fail;
77311
77312 pPg->nCell = nNew;
77313 pPg->nOverflow = 0;
77314
77315 put2byte(&aData[hdr+3], pPg->nCell);
77316 put2byte(&aData[hdr+5], pData - aData);
77317
77318#ifdef SQLITE_DEBUG
77319 for(i=0; i<nNew && !CORRUPT_DB; i++){
77320 u8 *pCell = pCArray->apCell[i+iNew];
77321 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
77322 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
77323 pCell = &pTmp[pCell - aData];
77324 }
77325 assert( 0==memcmp(pCell, &aData[iOff],
77326 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
77327 }
77328#endif
77329
77330 return SQLITE_OK;
77331 editpage_fail:
77332 /* Unable to edit this page. Rebuild it from scratch instead. */
77333 populateCellCache(p: pCArray, idx: iNew, N: nNew);
77334 return rebuildPage(pCArray, iFirst: iNew, nCell: nNew, pPg);
77335}
77336
77337
77338#ifndef SQLITE_OMIT_QUICKBALANCE
77339/*
77340** This version of balance() handles the common special case where
77341** a new entry is being inserted on the extreme right-end of the
77342** tree, in other words, when the new entry will become the largest
77343** entry in the tree.
77344**
77345** Instead of trying to balance the 3 right-most leaf pages, just add
77346** a new page to the right-hand side and put the one new entry in
77347** that page. This leaves the right side of the tree somewhat
77348** unbalanced. But odds are that we will be inserting new entries
77349** at the end soon afterwards so the nearly empty page will quickly
77350** fill up. On average.
77351**
77352** pPage is the leaf page which is the right-most page in the tree.
77353** pParent is its parent. pPage must have a single overflow entry
77354** which is also the right-most entry on the page.
77355**
77356** The pSpace buffer is used to store a temporary copy of the divider
77357** cell that will be inserted into pParent. Such a cell consists of a 4
77358** byte page number followed by a variable length integer. In other
77359** words, at most 13 bytes. Hence the pSpace buffer must be at
77360** least 13 bytes in size.
77361*/
77362static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
77363 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
77364 MemPage *pNew; /* Newly allocated page */
77365 int rc; /* Return Code */
77366 Pgno pgnoNew; /* Page number of pNew */
77367
77368 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77369 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77370 assert( pPage->nOverflow==1 );
77371
77372 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
77373 assert( pPage->nFree>=0 );
77374 assert( pParent->nFree>=0 );
77375
77376 /* Allocate a new page. This page will become the right-sibling of
77377 ** pPage. Make the parent page writable, so that the new divider cell
77378 ** may be inserted. If both these operations are successful, proceed.
77379 */
77380 rc = allocateBtreePage(pBt, ppPage: &pNew, pPgno: &pgnoNew, nearby: 0, eMode: 0);
77381
77382 if( rc==SQLITE_OK ){
77383
77384 u8 *pOut = &pSpace[4];
77385 u8 *pCell = pPage->apOvfl[0];
77386 u16 szCell = pPage->xCellSize(pPage, pCell);
77387 u8 *pStop;
77388 CellArray b;
77389
77390 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
77391 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
77392 zeroPage(pPage: pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
77393 b.nCell = 1;
77394 b.pRef = pPage;
77395 b.apCell = &pCell;
77396 b.szCell = &szCell;
77397 b.apEnd[0] = pPage->aDataEnd;
77398 b.ixNx[0] = 2;
77399 rc = rebuildPage(pCArray: &b, iFirst: 0, nCell: 1, pPg: pNew);
77400 if( NEVER(rc) ){
77401 releasePage(pPage: pNew);
77402 return rc;
77403 }
77404 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
77405
77406 /* If this is an auto-vacuum database, update the pointer map
77407 ** with entries for the new page, and any pointer from the
77408 ** cell on the page to an overflow page. If either of these
77409 ** operations fails, the return code is set, but the contents
77410 ** of the parent page are still manipulated by the code below.
77411 ** That is Ok, at this point the parent page is guaranteed to
77412 ** be marked as dirty. Returning an error code will cause a
77413 ** rollback, undoing any changes made to the parent page.
77414 */
77415 if( ISAUTOVACUUM(pBt) ){
77416 ptrmapPut(pBt, key: pgnoNew, PTRMAP_BTREE, parent: pParent->pgno, pRC: &rc);
77417 if( szCell>pNew->minLocal ){
77418 ptrmapPutOvflPtr(pPage: pNew, pSrc: pNew, pCell, pRC: &rc);
77419 }
77420 }
77421
77422 /* Create a divider cell to insert into pParent. The divider cell
77423 ** consists of a 4-byte page number (the page number of pPage) and
77424 ** a variable length key value (which must be the same value as the
77425 ** largest key on pPage).
77426 **
77427 ** To find the largest key value on pPage, first find the right-most
77428 ** cell on pPage. The first two fields of this cell are the
77429 ** record-length (a variable length integer at most 32-bits in size)
77430 ** and the key value (a variable length integer, may have any value).
77431 ** The first of the while(...) loops below skips over the record-length
77432 ** field. The second while(...) loop copies the key value from the
77433 ** cell on pPage into the pSpace buffer.
77434 */
77435 pCell = findCell(pPage, pPage->nCell-1);
77436 pStop = &pCell[9];
77437 while( (*(pCell++)&0x80) && pCell<pStop );
77438 pStop = &pCell[9];
77439 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
77440
77441 /* Insert the new divider cell into pParent. */
77442 if( rc==SQLITE_OK ){
77443 rc = insertCell(pPage: pParent, i: pParent->nCell, pCell: pSpace, sz: (int)(pOut-pSpace),
77444 pTemp: 0, iChild: pPage->pgno);
77445 }
77446
77447 /* Set the right-child pointer of pParent to point to the new page. */
77448 put4byte(p: &pParent->aData[pParent->hdrOffset+8], v: pgnoNew);
77449
77450 /* Release the reference to the new page. */
77451 releasePage(pPage: pNew);
77452 }
77453
77454 return rc;
77455}
77456#endif /* SQLITE_OMIT_QUICKBALANCE */
77457
77458#if 0
77459/*
77460** This function does not contribute anything to the operation of SQLite.
77461** it is sometimes activated temporarily while debugging code responsible
77462** for setting pointer-map entries.
77463*/
77464static int ptrmapCheckPages(MemPage **apPage, int nPage){
77465 int i, j;
77466 for(i=0; i<nPage; i++){
77467 Pgno n;
77468 u8 e;
77469 MemPage *pPage = apPage[i];
77470 BtShared *pBt = pPage->pBt;
77471 assert( pPage->isInit );
77472
77473 for(j=0; j<pPage->nCell; j++){
77474 CellInfo info;
77475 u8 *z;
77476
77477 z = findCell(pPage, j);
77478 pPage->xParseCell(pPage, z, &info);
77479 if( info.nLocal<info.nPayload ){
77480 Pgno ovfl = get4byte(&z[info.nSize-4]);
77481 ptrmapGet(pBt, ovfl, &e, &n);
77482 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
77483 }
77484 if( !pPage->leaf ){
77485 Pgno child = get4byte(z);
77486 ptrmapGet(pBt, child, &e, &n);
77487 assert( n==pPage->pgno && e==PTRMAP_BTREE );
77488 }
77489 }
77490 if( !pPage->leaf ){
77491 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
77492 ptrmapGet(pBt, child, &e, &n);
77493 assert( n==pPage->pgno && e==PTRMAP_BTREE );
77494 }
77495 }
77496 return 1;
77497}
77498#endif
77499
77500/*
77501** This function is used to copy the contents of the b-tree node stored
77502** on page pFrom to page pTo. If page pFrom was not a leaf page, then
77503** the pointer-map entries for each child page are updated so that the
77504** parent page stored in the pointer map is page pTo. If pFrom contained
77505** any cells with overflow page pointers, then the corresponding pointer
77506** map entries are also updated so that the parent page is page pTo.
77507**
77508** If pFrom is currently carrying any overflow cells (entries in the
77509** MemPage.apOvfl[] array), they are not copied to pTo.
77510**
77511** Before returning, page pTo is reinitialized using btreeInitPage().
77512**
77513** The performance of this function is not critical. It is only used by
77514** the balance_shallower() and balance_deeper() procedures, neither of
77515** which are called often under normal circumstances.
77516*/
77517static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
77518 if( (*pRC)==SQLITE_OK ){
77519 BtShared * const pBt = pFrom->pBt;
77520 u8 * const aFrom = pFrom->aData;
77521 u8 * const aTo = pTo->aData;
77522 int const iFromHdr = pFrom->hdrOffset;
77523 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
77524 int rc;
77525 int iData;
77526
77527
77528 assert( pFrom->isInit );
77529 assert( pFrom->nFree>=iToHdr );
77530 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
77531
77532 /* Copy the b-tree node content from page pFrom to page pTo. */
77533 iData = get2byte(&aFrom[iFromHdr+5]);
77534 memcpy(dest: &aTo[iData], src: &aFrom[iData], n: pBt->usableSize-iData);
77535 memcpy(dest: &aTo[iToHdr], src: &aFrom[iFromHdr], n: pFrom->cellOffset + 2*pFrom->nCell);
77536
77537 /* Reinitialize page pTo so that the contents of the MemPage structure
77538 ** match the new data. The initialization of pTo can actually fail under
77539 ** fairly obscure circumstances, even though it is a copy of initialized
77540 ** page pFrom.
77541 */
77542 pTo->isInit = 0;
77543 rc = btreeInitPage(pPage: pTo);
77544 if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pPage: pTo);
77545 if( rc!=SQLITE_OK ){
77546 *pRC = rc;
77547 return;
77548 }
77549
77550 /* If this is an auto-vacuum database, update the pointer-map entries
77551 ** for any b-tree or overflow pages that pTo now contains the pointers to.
77552 */
77553 if( ISAUTOVACUUM(pBt) ){
77554 *pRC = setChildPtrmaps(pTo);
77555 }
77556 }
77557}
77558
77559/*
77560** This routine redistributes cells on the iParentIdx'th child of pParent
77561** (hereafter "the page") and up to 2 siblings so that all pages have about the
77562** same amount of free space. Usually a single sibling on either side of the
77563** page are used in the balancing, though both siblings might come from one
77564** side if the page is the first or last child of its parent. If the page
77565** has fewer than 2 siblings (something which can only happen if the page
77566** is a root page or a child of a root page) then all available siblings
77567** participate in the balancing.
77568**
77569** The number of siblings of the page might be increased or decreased by
77570** one or two in an effort to keep pages nearly full but not over full.
77571**
77572** Note that when this routine is called, some of the cells on the page
77573** might not actually be stored in MemPage.aData[]. This can happen
77574** if the page is overfull. This routine ensures that all cells allocated
77575** to the page and its siblings fit into MemPage.aData[] before returning.
77576**
77577** In the course of balancing the page and its siblings, cells may be
77578** inserted into or removed from the parent page (pParent). Doing so
77579** may cause the parent page to become overfull or underfull. If this
77580** happens, it is the responsibility of the caller to invoke the correct
77581** balancing routine to fix this problem (see the balance() routine).
77582**
77583** If this routine fails for any reason, it might leave the database
77584** in a corrupted state. So if this routine fails, the database should
77585** be rolled back.
77586**
77587** The third argument to this function, aOvflSpace, is a pointer to a
77588** buffer big enough to hold one page. If while inserting cells into the parent
77589** page (pParent) the parent page becomes overfull, this buffer is
77590** used to store the parent's overflow cells. Because this function inserts
77591** a maximum of four divider cells into the parent page, and the maximum
77592** size of a cell stored within an internal node is always less than 1/4
77593** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
77594** enough for all overflow cells.
77595**
77596** If aOvflSpace is set to a null pointer, this function returns
77597** SQLITE_NOMEM.
77598*/
77599static int balance_nonroot(
77600 MemPage *pParent, /* Parent page of siblings being balanced */
77601 int iParentIdx, /* Index of "the page" in pParent */
77602 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
77603 int isRoot, /* True if pParent is a root-page */
77604 int bBulk /* True if this call is part of a bulk load */
77605){
77606 BtShared *pBt; /* The whole database */
77607 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
77608 int nNew = 0; /* Number of pages in apNew[] */
77609 int nOld; /* Number of pages in apOld[] */
77610 int i, j, k; /* Loop counters */
77611 int nxDiv; /* Next divider slot in pParent->aCell[] */
77612 int rc = SQLITE_OK; /* The return code */
77613 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
77614 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
77615 int usableSpace; /* Bytes in pPage beyond the header */
77616 int pageFlags; /* Value of pPage->aData[0] */
77617 int iSpace1 = 0; /* First unused byte of aSpace1[] */
77618 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
77619 int szScratch; /* Size of scratch memory requested */
77620 MemPage *apOld[NB]; /* pPage and up to two siblings */
77621 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
77622 u8 *pRight; /* Location in parent of right-sibling pointer */
77623 u8 *apDiv[NB-1]; /* Divider cells in pParent */
77624 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
77625 int cntOld[NB+2]; /* Old index in b.apCell[] */
77626 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
77627 u8 *aSpace1; /* Space for copies of dividers cells */
77628 Pgno pgno; /* Temp var to store a page number in */
77629 u8 abDone[NB+2]; /* True after i'th new page is populated */
77630 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
77631 CellArray b; /* Parsed information on cells being balanced */
77632
77633 memset(s: abDone, c: 0, n: sizeof(abDone));
77634 memset(s: &b, c: 0, n: sizeof(b));
77635 pBt = pParent->pBt;
77636 assert( sqlite3_mutex_held(pBt->mutex) );
77637 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77638
77639 /* At this point pParent may have at most one overflow cell. And if
77640 ** this overflow cell is present, it must be the cell with
77641 ** index iParentIdx. This scenario comes about when this function
77642 ** is called (indirectly) from sqlite3BtreeDelete().
77643 */
77644 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
77645 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
77646
77647 if( !aOvflSpace ){
77648 return SQLITE_NOMEM_BKPT;
77649 }
77650 assert( pParent->nFree>=0 );
77651
77652 /* Find the sibling pages to balance. Also locate the cells in pParent
77653 ** that divide the siblings. An attempt is made to find NN siblings on
77654 ** either side of pPage. More siblings are taken from one side, however,
77655 ** if there are fewer than NN siblings on the other side. If pParent
77656 ** has NB or fewer children then all children of pParent are taken.
77657 **
77658 ** This loop also drops the divider cells from the parent page. This
77659 ** way, the remainder of the function does not have to deal with any
77660 ** overflow cells in the parent page, since if any existed they will
77661 ** have already been removed.
77662 */
77663 i = pParent->nOverflow + pParent->nCell;
77664 if( i<2 ){
77665 nxDiv = 0;
77666 }else{
77667 assert( bBulk==0 || bBulk==1 );
77668 if( iParentIdx==0 ){
77669 nxDiv = 0;
77670 }else if( iParentIdx==i ){
77671 nxDiv = i-2+bBulk;
77672 }else{
77673 nxDiv = iParentIdx-1;
77674 }
77675 i = 2-bBulk;
77676 }
77677 nOld = i+1;
77678 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
77679 pRight = &pParent->aData[pParent->hdrOffset+8];
77680 }else{
77681 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
77682 }
77683 pgno = get4byte(p: pRight);
77684 while( 1 ){
77685 if( rc==SQLITE_OK ){
77686 rc = getAndInitPage(pBt, pgno, ppPage: &apOld[i], bReadOnly: 0);
77687 }
77688 if( rc ){
77689 memset(s: apOld, c: 0, n: (i+1)*sizeof(MemPage*));
77690 goto balance_cleanup;
77691 }
77692 if( apOld[i]->nFree<0 ){
77693 rc = btreeComputeFreeSpace(pPage: apOld[i]);
77694 if( rc ){
77695 memset(s: apOld, c: 0, n: (i)*sizeof(MemPage*));
77696 goto balance_cleanup;
77697 }
77698 }
77699 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
77700 if( (i--)==0 ) break;
77701
77702 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
77703 apDiv[i] = pParent->apOvfl[0];
77704 pgno = get4byte(p: apDiv[i]);
77705 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
77706 pParent->nOverflow = 0;
77707 }else{
77708 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
77709 pgno = get4byte(p: apDiv[i]);
77710 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
77711
77712 /* Drop the cell from the parent page. apDiv[i] still points to
77713 ** the cell within the parent, even though it has been dropped.
77714 ** This is safe because dropping a cell only overwrites the first
77715 ** four bytes of it, and this function does not need the first
77716 ** four bytes of the divider cell. So the pointer is safe to use
77717 ** later on.
77718 **
77719 ** But not if we are in secure-delete mode. In secure-delete mode,
77720 ** the dropCell() routine will overwrite the entire cell with zeroes.
77721 ** In this case, temporarily copy the cell into the aOvflSpace[]
77722 ** buffer. It will be copied out again as soon as the aSpace[] buffer
77723 ** is allocated. */
77724 if( pBt->btsFlags & BTS_FAST_SECURE ){
77725 int iOff;
77726
77727 /* If the following if() condition is not true, the db is corrupted.
77728 ** The call to dropCell() below will detect this. */
77729 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
77730 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
77731 memcpy(dest: &aOvflSpace[iOff], src: apDiv[i], n: szNew[i]);
77732 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
77733 }
77734 }
77735 dropCell(pPage: pParent, idx: i+nxDiv-pParent->nOverflow, sz: szNew[i], pRC: &rc);
77736 }
77737 }
77738
77739 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
77740 ** alignment */
77741 nMaxCells = (nMaxCells + 3)&~3;
77742
77743 /*
77744 ** Allocate space for memory structures
77745 */
77746 szScratch =
77747 nMaxCells*sizeof(u8*) /* b.apCell */
77748 + nMaxCells*sizeof(u16) /* b.szCell */
77749 + pBt->pageSize; /* aSpace1 */
77750
77751 assert( szScratch<=7*(int)pBt->pageSize );
77752 b.apCell = sqlite3StackAllocRaw(0, szScratch );
77753 if( b.apCell==0 ){
77754 rc = SQLITE_NOMEM_BKPT;
77755 goto balance_cleanup;
77756 }
77757 b.szCell = (u16*)&b.apCell[nMaxCells];
77758 aSpace1 = (u8*)&b.szCell[nMaxCells];
77759 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
77760
77761 /*
77762 ** Load pointers to all cells on sibling pages and the divider cells
77763 ** into the local b.apCell[] array. Make copies of the divider cells
77764 ** into space obtained from aSpace1[]. The divider cells have already
77765 ** been removed from pParent.
77766 **
77767 ** If the siblings are on leaf pages, then the child pointers of the
77768 ** divider cells are stripped from the cells before they are copied
77769 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
77770 ** child pointers. If siblings are not leaves, then all cell in
77771 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
77772 ** are alike.
77773 **
77774 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
77775 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
77776 */
77777 b.pRef = apOld[0];
77778 leafCorrection = b.pRef->leaf*4;
77779 leafData = b.pRef->intKeyLeaf;
77780 for(i=0; i<nOld; i++){
77781 MemPage *pOld = apOld[i];
77782 int limit = pOld->nCell;
77783 u8 *aData = pOld->aData;
77784 u16 maskPage = pOld->maskPage;
77785 u8 *piCell = aData + pOld->cellOffset;
77786 u8 *piEnd;
77787 VVA_ONLY( int nCellAtStart = b.nCell; )
77788
77789 /* Verify that all sibling pages are of the same "type" (table-leaf,
77790 ** table-interior, index-leaf, or index-interior).
77791 */
77792 if( pOld->aData[0]!=apOld[0]->aData[0] ){
77793 rc = SQLITE_CORRUPT_BKPT;
77794 goto balance_cleanup;
77795 }
77796
77797 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
77798 ** contains overflow cells, include them in the b.apCell[] array
77799 ** in the correct spot.
77800 **
77801 ** Note that when there are multiple overflow cells, it is always the
77802 ** case that they are sequential and adjacent. This invariant arises
77803 ** because multiple overflows can only occurs when inserting divider
77804 ** cells into a parent on a prior balance, and divider cells are always
77805 ** adjacent and are inserted in order. There is an assert() tagged
77806 ** with "NOTE 1" in the overflow cell insertion loop to prove this
77807 ** invariant.
77808 **
77809 ** This must be done in advance. Once the balance starts, the cell
77810 ** offset section of the btree page will be overwritten and we will no
77811 ** long be able to find the cells if a pointer to each cell is not saved
77812 ** first.
77813 */
77814 memset(s: &b.szCell[b.nCell], c: 0, n: sizeof(b.szCell[0])*(limit+pOld->nOverflow));
77815 if( pOld->nOverflow>0 ){
77816 if( NEVER(limit<pOld->aiOvfl[0]) ){
77817 rc = SQLITE_CORRUPT_BKPT;
77818 goto balance_cleanup;
77819 }
77820 limit = pOld->aiOvfl[0];
77821 for(j=0; j<limit; j++){
77822 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
77823 piCell += 2;
77824 b.nCell++;
77825 }
77826 for(k=0; k<pOld->nOverflow; k++){
77827 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
77828 b.apCell[b.nCell] = pOld->apOvfl[k];
77829 b.nCell++;
77830 }
77831 }
77832 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
77833 while( piCell<piEnd ){
77834 assert( b.nCell<nMaxCells );
77835 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
77836 piCell += 2;
77837 b.nCell++;
77838 }
77839 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
77840
77841 cntOld[i] = b.nCell;
77842 if( i<nOld-1 && !leafData){
77843 u16 sz = (u16)szNew[i];
77844 u8 *pTemp;
77845 assert( b.nCell<nMaxCells );
77846 b.szCell[b.nCell] = sz;
77847 pTemp = &aSpace1[iSpace1];
77848 iSpace1 += sz;
77849 assert( sz<=pBt->maxLocal+23 );
77850 assert( iSpace1 <= (int)pBt->pageSize );
77851 memcpy(dest: pTemp, src: apDiv[i], n: sz);
77852 b.apCell[b.nCell] = pTemp+leafCorrection;
77853 assert( leafCorrection==0 || leafCorrection==4 );
77854 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
77855 if( !pOld->leaf ){
77856 assert( leafCorrection==0 );
77857 assert( pOld->hdrOffset==0 || CORRUPT_DB );
77858 /* The right pointer of the child page pOld becomes the left
77859 ** pointer of the divider cell */
77860 memcpy(dest: b.apCell[b.nCell], src: &pOld->aData[8], n: 4);
77861 }else{
77862 assert( leafCorrection==4 );
77863 while( b.szCell[b.nCell]<4 ){
77864 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
77865 ** does exist, pad it with 0x00 bytes. */
77866 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
77867 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
77868 aSpace1[iSpace1++] = 0x00;
77869 b.szCell[b.nCell]++;
77870 }
77871 }
77872 b.nCell++;
77873 }
77874 }
77875
77876 /*
77877 ** Figure out the number of pages needed to hold all b.nCell cells.
77878 ** Store this number in "k". Also compute szNew[] which is the total
77879 ** size of all cells on the i-th page and cntNew[] which is the index
77880 ** in b.apCell[] of the cell that divides page i from page i+1.
77881 ** cntNew[k] should equal b.nCell.
77882 **
77883 ** Values computed by this block:
77884 **
77885 ** k: The total number of sibling pages
77886 ** szNew[i]: Spaced used on the i-th sibling page.
77887 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
77888 ** the right of the i-th sibling page.
77889 ** usableSpace: Number of bytes of space available on each sibling.
77890 **
77891 */
77892 usableSpace = pBt->usableSize - 12 + leafCorrection;
77893 for(i=k=0; i<nOld; i++, k++){
77894 MemPage *p = apOld[i];
77895 b.apEnd[k] = p->aDataEnd;
77896 b.ixNx[k] = cntOld[i];
77897 if( k && b.ixNx[k]==b.ixNx[k-1] ){
77898 k--; /* Omit b.ixNx[] entry for child pages with no cells */
77899 }
77900 if( !leafData ){
77901 k++;
77902 b.apEnd[k] = pParent->aDataEnd;
77903 b.ixNx[k] = cntOld[i]+1;
77904 }
77905 assert( p->nFree>=0 );
77906 szNew[i] = usableSpace - p->nFree;
77907 for(j=0; j<p->nOverflow; j++){
77908 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
77909 }
77910 cntNew[i] = cntOld[i];
77911 }
77912 k = nOld;
77913 for(i=0; i<k; i++){
77914 int sz;
77915 while( szNew[i]>usableSpace ){
77916 if( i+1>=k ){
77917 k = i+2;
77918 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
77919 szNew[k-1] = 0;
77920 cntNew[k-1] = b.nCell;
77921 }
77922 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]-1);
77923 szNew[i] -= sz;
77924 if( !leafData ){
77925 if( cntNew[i]<b.nCell ){
77926 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]);
77927 }else{
77928 sz = 0;
77929 }
77930 }
77931 szNew[i+1] += sz;
77932 cntNew[i]--;
77933 }
77934 while( cntNew[i]<b.nCell ){
77935 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]);
77936 if( szNew[i]+sz>usableSpace ) break;
77937 szNew[i] += sz;
77938 cntNew[i]++;
77939 if( !leafData ){
77940 if( cntNew[i]<b.nCell ){
77941 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]);
77942 }else{
77943 sz = 0;
77944 }
77945 }
77946 szNew[i+1] -= sz;
77947 }
77948 if( cntNew[i]>=b.nCell ){
77949 k = i+1;
77950 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
77951 rc = SQLITE_CORRUPT_BKPT;
77952 goto balance_cleanup;
77953 }
77954 }
77955
77956 /*
77957 ** The packing computed by the previous block is biased toward the siblings
77958 ** on the left side (siblings with smaller keys). The left siblings are
77959 ** always nearly full, while the right-most sibling might be nearly empty.
77960 ** The next block of code attempts to adjust the packing of siblings to
77961 ** get a better balance.
77962 **
77963 ** This adjustment is more than an optimization. The packing above might
77964 ** be so out of balance as to be illegal. For example, the right-most
77965 ** sibling might be completely empty. This adjustment is not optional.
77966 */
77967 for(i=k-1; i>0; i--){
77968 int szRight = szNew[i]; /* Size of sibling on the right */
77969 int szLeft = szNew[i-1]; /* Size of sibling on the left */
77970 int r; /* Index of right-most cell in left sibling */
77971 int d; /* Index of first cell to the left of right sibling */
77972
77973 r = cntNew[i-1] - 1;
77974 d = r + 1 - leafData;
77975 (void)cachedCellSize(p: &b, N: d);
77976 do{
77977 int szR, szD;
77978 assert( d<nMaxCells );
77979 assert( r<nMaxCells );
77980 szR = cachedCellSize(p: &b, N: r);
77981 szD = b.szCell[d];
77982 if( szRight!=0
77983 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
77984 break;
77985 }
77986 szRight += szD + 2;
77987 szLeft -= szR + 2;
77988 cntNew[i-1] = r;
77989 r--;
77990 d--;
77991 }while( r>=0 );
77992 szNew[i] = szRight;
77993 szNew[i-1] = szLeft;
77994 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
77995 rc = SQLITE_CORRUPT_BKPT;
77996 goto balance_cleanup;
77997 }
77998 }
77999
78000 /* Sanity check: For a non-corrupt database file one of the following
78001 ** must be true:
78002 ** (1) We found one or more cells (cntNew[0])>0), or
78003 ** (2) pPage is a virtual root page. A virtual root page is when
78004 ** the real root page is page 1 and we are the only child of
78005 ** that page.
78006 */
78007 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
78008 TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n",
78009 apOld[0]->pgno, apOld[0]->nCell,
78010 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
78011 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
78012 ));
78013
78014 /*
78015 ** Allocate k new pages. Reuse old pages where possible.
78016 */
78017 pageFlags = apOld[0]->aData[0];
78018 for(i=0; i<k; i++){
78019 MemPage *pNew;
78020 if( i<nOld ){
78021 pNew = apNew[i] = apOld[i];
78022 apOld[i] = 0;
78023 rc = sqlite3PagerWrite(pPg: pNew->pDbPage);
78024 nNew++;
78025 if( sqlite3PagerPageRefcount(pPage: pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
78026 && rc==SQLITE_OK
78027 ){
78028 rc = SQLITE_CORRUPT_BKPT;
78029 }
78030 if( rc ) goto balance_cleanup;
78031 }else{
78032 assert( i>0 );
78033 rc = allocateBtreePage(pBt, ppPage: &pNew, pPgno: &pgno, nearby: (bBulk ? 1 : pgno), eMode: 0);
78034 if( rc ) goto balance_cleanup;
78035 zeroPage(pPage: pNew, flags: pageFlags);
78036 apNew[i] = pNew;
78037 nNew++;
78038 cntOld[i] = b.nCell;
78039
78040 /* Set the pointer-map entry for the new sibling page. */
78041 if( ISAUTOVACUUM(pBt) ){
78042 ptrmapPut(pBt, key: pNew->pgno, PTRMAP_BTREE, parent: pParent->pgno, pRC: &rc);
78043 if( rc!=SQLITE_OK ){
78044 goto balance_cleanup;
78045 }
78046 }
78047 }
78048 }
78049
78050 /*
78051 ** Reassign page numbers so that the new pages are in ascending order.
78052 ** This helps to keep entries in the disk file in order so that a scan
78053 ** of the table is closer to a linear scan through the file. That in turn
78054 ** helps the operating system to deliver pages from the disk more rapidly.
78055 **
78056 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
78057 ** (5), that is not a performance concern.
78058 **
78059 ** When NB==3, this one optimization makes the database about 25% faster
78060 ** for large insertions and deletions.
78061 */
78062 for(i=0; i<nNew; i++){
78063 aPgno[i] = apNew[i]->pgno;
78064 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
78065 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
78066 }
78067 for(i=0; i<nNew-1; i++){
78068 int iB = i;
78069 for(j=i+1; j<nNew; j++){
78070 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
78071 }
78072
78073 /* If apNew[i] has a page number that is bigger than any of the
78074 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
78075 ** entry that has the smallest page number (which we know to be
78076 ** entry apNew[iB]).
78077 */
78078 if( iB!=i ){
78079 Pgno pgnoA = apNew[i]->pgno;
78080 Pgno pgnoB = apNew[iB]->pgno;
78081 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
78082 u16 fgA = apNew[i]->pDbPage->flags;
78083 u16 fgB = apNew[iB]->pDbPage->flags;
78084 sqlite3PagerRekey(pPg: apNew[i]->pDbPage, iNew: pgnoTemp, flags: fgB);
78085 sqlite3PagerRekey(pPg: apNew[iB]->pDbPage, iNew: pgnoA, flags: fgA);
78086 sqlite3PagerRekey(pPg: apNew[i]->pDbPage, iNew: pgnoB, flags: fgB);
78087 apNew[i]->pgno = pgnoB;
78088 apNew[iB]->pgno = pgnoA;
78089 }
78090 }
78091
78092 TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) "
78093 "%u(%u nc=%u) %u(%u nc=%u)\n",
78094 apNew[0]->pgno, szNew[0], cntNew[0],
78095 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
78096 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
78097 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
78098 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
78099 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
78100 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
78101 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
78102 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
78103 ));
78104
78105 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78106 assert( nNew>=1 && nNew<=ArraySize(apNew) );
78107 assert( apNew[nNew-1]!=0 );
78108 put4byte(p: pRight, v: apNew[nNew-1]->pgno);
78109
78110 /* If the sibling pages are not leaves, ensure that the right-child pointer
78111 ** of the right-most new sibling page is set to the value that was
78112 ** originally in the same field of the right-most old sibling page. */
78113 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
78114 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
78115 memcpy(dest: &apNew[nNew-1]->aData[8], src: &pOld->aData[8], n: 4);
78116 }
78117
78118 /* Make any required updates to pointer map entries associated with
78119 ** cells stored on sibling pages following the balance operation. Pointer
78120 ** map entries associated with divider cells are set by the insertCell()
78121 ** routine. The associated pointer map entries are:
78122 **
78123 ** a) if the cell contains a reference to an overflow chain, the
78124 ** entry associated with the first page in the overflow chain, and
78125 **
78126 ** b) if the sibling pages are not leaves, the child page associated
78127 ** with the cell.
78128 **
78129 ** If the sibling pages are not leaves, then the pointer map entry
78130 ** associated with the right-child of each sibling may also need to be
78131 ** updated. This happens below, after the sibling pages have been
78132 ** populated, not here.
78133 */
78134 if( ISAUTOVACUUM(pBt) ){
78135 MemPage *pOld;
78136 MemPage *pNew = pOld = apNew[0];
78137 int cntOldNext = pNew->nCell + pNew->nOverflow;
78138 int iNew = 0;
78139 int iOld = 0;
78140
78141 for(i=0; i<b.nCell; i++){
78142 u8 *pCell = b.apCell[i];
78143 while( i==cntOldNext ){
78144 iOld++;
78145 assert( iOld<nNew || iOld<nOld );
78146 assert( iOld>=0 && iOld<NB );
78147 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
78148 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
78149 }
78150 if( i==cntNew[iNew] ){
78151 pNew = apNew[++iNew];
78152 if( !leafData ) continue;
78153 }
78154
78155 /* Cell pCell is destined for new sibling page pNew. Originally, it
78156 ** was either part of sibling page iOld (possibly an overflow cell),
78157 ** or else the divider cell to the left of sibling page iOld. So,
78158 ** if sibling page iOld had the same page number as pNew, and if
78159 ** pCell really was a part of sibling page iOld (not a divider or
78160 ** overflow cell), we can skip updating the pointer map entries. */
78161 if( iOld>=nNew
78162 || pNew->pgno!=aPgno[iOld]
78163 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
78164 ){
78165 if( !leafCorrection ){
78166 ptrmapPut(pBt, get4byte(p: pCell), PTRMAP_BTREE, parent: pNew->pgno, pRC: &rc);
78167 }
78168 if( cachedCellSize(p: &b,N: i)>pNew->minLocal ){
78169 ptrmapPutOvflPtr(pPage: pNew, pSrc: pOld, pCell, pRC: &rc);
78170 }
78171 if( rc ) goto balance_cleanup;
78172 }
78173 }
78174 }
78175
78176 /* Insert new divider cells into pParent. */
78177 for(i=0; i<nNew-1; i++){
78178 u8 *pCell;
78179 u8 *pTemp;
78180 int sz;
78181 u8 *pSrcEnd;
78182 MemPage *pNew = apNew[i];
78183 j = cntNew[i];
78184
78185 assert( j<nMaxCells );
78186 assert( b.apCell[j]!=0 );
78187 pCell = b.apCell[j];
78188 sz = b.szCell[j] + leafCorrection;
78189 pTemp = &aOvflSpace[iOvflSpace];
78190 if( !pNew->leaf ){
78191 memcpy(dest: &pNew->aData[8], src: pCell, n: 4);
78192 }else if( leafData ){
78193 /* If the tree is a leaf-data tree, and the siblings are leaves,
78194 ** then there is no divider cell in b.apCell[]. Instead, the divider
78195 ** cell consists of the integer key for the right-most cell of
78196 ** the sibling-page assembled above only.
78197 */
78198 CellInfo info;
78199 j--;
78200 pNew->xParseCell(pNew, b.apCell[j], &info);
78201 pCell = pTemp;
78202 sz = 4 + putVarint(p: &pCell[4], v: info.nKey);
78203 pTemp = 0;
78204 }else{
78205 pCell -= 4;
78206 /* Obscure case for non-leaf-data trees: If the cell at pCell was
78207 ** previously stored on a leaf node, and its reported size was 4
78208 ** bytes, then it may actually be smaller than this
78209 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
78210 ** any cell). But it is important to pass the correct size to
78211 ** insertCell(), so reparse the cell now.
78212 **
78213 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
78214 ** and WITHOUT ROWID tables with exactly one column which is the
78215 ** primary key.
78216 */
78217 if( b.szCell[j]==4 ){
78218 assert(leafCorrection==4);
78219 sz = pParent->xCellSize(pParent, pCell);
78220 }
78221 }
78222 iOvflSpace += sz;
78223 assert( sz<=pBt->maxLocal+23 );
78224 assert( iOvflSpace <= (int)pBt->pageSize );
78225 for(k=0; ALWAYS(k<NB*2) && b.ixNx[k]<=j; k++){}
78226 pSrcEnd = b.apEnd[k];
78227 if( SQLITE_OVERFLOW(pSrcEnd, pCell, pCell+sz) ){
78228 rc = SQLITE_CORRUPT_BKPT;
78229 goto balance_cleanup;
78230 }
78231 rc = insertCell(pPage: pParent, i: nxDiv+i, pCell, sz, pTemp, iChild: pNew->pgno);
78232 if( rc!=SQLITE_OK ) goto balance_cleanup;
78233 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78234 }
78235
78236 /* Now update the actual sibling pages. The order in which they are updated
78237 ** is important, as this code needs to avoid disrupting any page from which
78238 ** cells may still to be read. In practice, this means:
78239 **
78240 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
78241 ** then it is not safe to update page apNew[iPg] until after
78242 ** the left-hand sibling apNew[iPg-1] has been updated.
78243 **
78244 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
78245 ** then it is not safe to update page apNew[iPg] until after
78246 ** the right-hand sibling apNew[iPg+1] has been updated.
78247 **
78248 ** If neither of the above apply, the page is safe to update.
78249 **
78250 ** The iPg value in the following loop starts at nNew-1 goes down
78251 ** to 0, then back up to nNew-1 again, thus making two passes over
78252 ** the pages. On the initial downward pass, only condition (1) above
78253 ** needs to be tested because (2) will always be true from the previous
78254 ** step. On the upward pass, both conditions are always true, so the
78255 ** upwards pass simply processes pages that were missed on the downward
78256 ** pass.
78257 */
78258 for(i=1-nNew; i<nNew; i++){
78259 int iPg = i<0 ? -i : i;
78260 assert( iPg>=0 && iPg<nNew );
78261 assert( iPg>=1 || i>=0 );
78262 assert( iPg<ArraySize(cntOld) );
78263 if( abDone[iPg] ) continue; /* Skip pages already processed */
78264 if( i>=0 /* On the upwards pass, or... */
78265 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
78266 ){
78267 int iNew;
78268 int iOld;
78269 int nNewCell;
78270
78271 /* Verify condition (1): If cells are moving left, update iPg
78272 ** only after iPg-1 has already been updated. */
78273 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
78274
78275 /* Verify condition (2): If cells are moving right, update iPg
78276 ** only after iPg+1 has already been updated. */
78277 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
78278
78279 if( iPg==0 ){
78280 iNew = iOld = 0;
78281 nNewCell = cntNew[0];
78282 }else{
78283 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
78284 iNew = cntNew[iPg-1] + !leafData;
78285 nNewCell = cntNew[iPg] - iNew;
78286 }
78287
78288 rc = editPage(pPg: apNew[iPg], iOld, iNew, nNew: nNewCell, pCArray: &b);
78289 if( rc ) goto balance_cleanup;
78290 abDone[iPg]++;
78291 apNew[iPg]->nFree = usableSpace-szNew[iPg];
78292 assert( apNew[iPg]->nOverflow==0 );
78293 assert( apNew[iPg]->nCell==nNewCell );
78294 }
78295 }
78296
78297 /* All pages have been processed exactly once */
78298 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
78299
78300 assert( nOld>0 );
78301 assert( nNew>0 );
78302
78303 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
78304 /* The root page of the b-tree now contains no cells. The only sibling
78305 ** page is the right-child of the parent. Copy the contents of the
78306 ** child page into the parent, decreasing the overall height of the
78307 ** b-tree structure by one. This is described as the "balance-shallower"
78308 ** sub-algorithm in some documentation.
78309 **
78310 ** If this is an auto-vacuum database, the call to copyNodeContent()
78311 ** sets all pointer-map entries corresponding to database image pages
78312 ** for which the pointer is stored within the content being copied.
78313 **
78314 ** It is critical that the child page be defragmented before being
78315 ** copied into the parent, because if the parent is page 1 then it will
78316 ** by smaller than the child due to the database header, and so all the
78317 ** free space needs to be up front.
78318 */
78319 assert( nNew==1 || CORRUPT_DB );
78320 rc = defragmentPage(pPage: apNew[0], nMaxFrag: -1);
78321 testcase( rc!=SQLITE_OK );
78322 assert( apNew[0]->nFree ==
78323 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
78324 - apNew[0]->nCell*2)
78325 || rc!=SQLITE_OK
78326 );
78327 copyNodeContent(pFrom: apNew[0], pTo: pParent, pRC: &rc);
78328 freePage(pPage: apNew[0], pRC: &rc);
78329 }else if( ISAUTOVACUUM(pBt) && !leafCorrection ){
78330 /* Fix the pointer map entries associated with the right-child of each
78331 ** sibling page. All other pointer map entries have already been taken
78332 ** care of. */
78333 for(i=0; i<nNew; i++){
78334 u32 key = get4byte(p: &apNew[i]->aData[8]);
78335 ptrmapPut(pBt, key, PTRMAP_BTREE, parent: apNew[i]->pgno, pRC: &rc);
78336 }
78337 }
78338
78339 assert( pParent->isInit );
78340 TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n",
78341 nOld, nNew, b.nCell));
78342
78343 /* Free any old pages that were not reused as new pages.
78344 */
78345 for(i=nNew; i<nOld; i++){
78346 freePage(pPage: apOld[i], pRC: &rc);
78347 }
78348
78349#if 0
78350 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
78351 /* The ptrmapCheckPages() contains assert() statements that verify that
78352 ** all pointer map pages are set correctly. This is helpful while
78353 ** debugging. This is usually disabled because a corrupt database may
78354 ** cause an assert() statement to fail. */
78355 ptrmapCheckPages(apNew, nNew);
78356 ptrmapCheckPages(&pParent, 1);
78357 }
78358#endif
78359
78360 /*
78361 ** Cleanup before returning.
78362 */
78363balance_cleanup:
78364 sqlite3StackFree(0, b.apCell);
78365 for(i=0; i<nOld; i++){
78366 releasePage(pPage: apOld[i]);
78367 }
78368 for(i=0; i<nNew; i++){
78369 releasePage(pPage: apNew[i]);
78370 }
78371
78372 return rc;
78373}
78374
78375
78376/*
78377** This function is called when the root page of a b-tree structure is
78378** overfull (has one or more overflow pages).
78379**
78380** A new child page is allocated and the contents of the current root
78381** page, including overflow cells, are copied into the child. The root
78382** page is then overwritten to make it an empty page with the right-child
78383** pointer pointing to the new page.
78384**
78385** Before returning, all pointer-map entries corresponding to pages
78386** that the new child-page now contains pointers to are updated. The
78387** entry corresponding to the new right-child pointer of the root
78388** page is also updated.
78389**
78390** If successful, *ppChild is set to contain a reference to the child
78391** page and SQLITE_OK is returned. In this case the caller is required
78392** to call releasePage() on *ppChild exactly once. If an error occurs,
78393** an error code is returned and *ppChild is set to 0.
78394*/
78395static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
78396 int rc; /* Return value from subprocedures */
78397 MemPage *pChild = 0; /* Pointer to a new child page */
78398 Pgno pgnoChild = 0; /* Page number of the new child page */
78399 BtShared *pBt = pRoot->pBt; /* The BTree */
78400
78401 assert( pRoot->nOverflow>0 );
78402 assert( sqlite3_mutex_held(pBt->mutex) );
78403
78404 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
78405 ** page that will become the new right-child of pPage. Copy the contents
78406 ** of the node stored on pRoot into the new child page.
78407 */
78408 rc = sqlite3PagerWrite(pPg: pRoot->pDbPage);
78409 if( rc==SQLITE_OK ){
78410 rc = allocateBtreePage(pBt,ppPage: &pChild,pPgno: &pgnoChild,nearby: pRoot->pgno,eMode: 0);
78411 copyNodeContent(pFrom: pRoot, pTo: pChild, pRC: &rc);
78412 if( ISAUTOVACUUM(pBt) ){
78413 ptrmapPut(pBt, key: pgnoChild, PTRMAP_BTREE, parent: pRoot->pgno, pRC: &rc);
78414 }
78415 }
78416 if( rc ){
78417 *ppChild = 0;
78418 releasePage(pPage: pChild);
78419 return rc;
78420 }
78421 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
78422 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
78423 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
78424
78425 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
78426
78427 /* Copy the overflow cells from pRoot to pChild */
78428 memcpy(dest: pChild->aiOvfl, src: pRoot->aiOvfl,
78429 n: pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
78430 memcpy(dest: pChild->apOvfl, src: pRoot->apOvfl,
78431 n: pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
78432 pChild->nOverflow = pRoot->nOverflow;
78433
78434 /* Zero the contents of pRoot. Then install pChild as the right-child. */
78435 zeroPage(pPage: pRoot, flags: pChild->aData[0] & ~PTF_LEAF);
78436 put4byte(p: &pRoot->aData[pRoot->hdrOffset+8], v: pgnoChild);
78437
78438 *ppChild = pChild;
78439 return SQLITE_OK;
78440}
78441
78442/*
78443** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
78444** on the same B-tree as pCur.
78445**
78446** This can occur if a database is corrupt with two or more SQL tables
78447** pointing to the same b-tree. If an insert occurs on one SQL table
78448** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
78449** table linked to the same b-tree. If the secondary insert causes a
78450** rebalance, that can change content out from under the cursor on the
78451** first SQL table, violating invariants on the first insert.
78452*/
78453static int anotherValidCursor(BtCursor *pCur){
78454 BtCursor *pOther;
78455 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
78456 if( pOther!=pCur
78457 && pOther->eState==CURSOR_VALID
78458 && pOther->pPage==pCur->pPage
78459 ){
78460 return SQLITE_CORRUPT_BKPT;
78461 }
78462 }
78463 return SQLITE_OK;
78464}
78465
78466/*
78467** The page that pCur currently points to has just been modified in
78468** some way. This function figures out if this modification means the
78469** tree needs to be balanced, and if so calls the appropriate balancing
78470** routine. Balancing routines are:
78471**
78472** balance_quick()
78473** balance_deeper()
78474** balance_nonroot()
78475*/
78476static int balance(BtCursor *pCur){
78477 int rc = SQLITE_OK;
78478 u8 aBalanceQuickSpace[13];
78479 u8 *pFree = 0;
78480
78481 VVA_ONLY( int balance_quick_called = 0 );
78482 VVA_ONLY( int balance_deeper_called = 0 );
78483
78484 do {
78485 int iPage;
78486 MemPage *pPage = pCur->pPage;
78487
78488 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
78489 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
78490 /* No rebalance required as long as:
78491 ** (1) There are no overflow cells
78492 ** (2) The amount of free space on the page is less than 2/3rds of
78493 ** the total usable space on the page. */
78494 break;
78495 }else if( (iPage = pCur->iPage)==0 ){
78496 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
78497 /* The root page of the b-tree is overfull. In this case call the
78498 ** balance_deeper() function to create a new child for the root-page
78499 ** and copy the current contents of the root-page to it. The
78500 ** next iteration of the do-loop will balance the child page.
78501 */
78502 assert( balance_deeper_called==0 );
78503 VVA_ONLY( balance_deeper_called++ );
78504 rc = balance_deeper(pRoot: pPage, ppChild: &pCur->apPage[1]);
78505 if( rc==SQLITE_OK ){
78506 pCur->iPage = 1;
78507 pCur->ix = 0;
78508 pCur->aiIdx[0] = 0;
78509 pCur->apPage[0] = pPage;
78510 pCur->pPage = pCur->apPage[1];
78511 assert( pCur->pPage->nOverflow );
78512 }
78513 }else{
78514 break;
78515 }
78516 }else if( sqlite3PagerPageRefcount(pPage: pPage->pDbPage)>1 ){
78517 /* The page being written is not a root page, and there is currently
78518 ** more than one reference to it. This only happens if the page is one
78519 ** of its own ancestor pages. Corruption. */
78520 rc = SQLITE_CORRUPT_BKPT;
78521 }else{
78522 MemPage * const pParent = pCur->apPage[iPage-1];
78523 int const iIdx = pCur->aiIdx[iPage-1];
78524
78525 rc = sqlite3PagerWrite(pPg: pParent->pDbPage);
78526 if( rc==SQLITE_OK && pParent->nFree<0 ){
78527 rc = btreeComputeFreeSpace(pPage: pParent);
78528 }
78529 if( rc==SQLITE_OK ){
78530#ifndef SQLITE_OMIT_QUICKBALANCE
78531 if( pPage->intKeyLeaf
78532 && pPage->nOverflow==1
78533 && pPage->aiOvfl[0]==pPage->nCell
78534 && pParent->pgno!=1
78535 && pParent->nCell==iIdx
78536 ){
78537 /* Call balance_quick() to create a new sibling of pPage on which
78538 ** to store the overflow cell. balance_quick() inserts a new cell
78539 ** into pParent, which may cause pParent overflow. If this
78540 ** happens, the next iteration of the do-loop will balance pParent
78541 ** use either balance_nonroot() or balance_deeper(). Until this
78542 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
78543 ** buffer.
78544 **
78545 ** The purpose of the following assert() is to check that only a
78546 ** single call to balance_quick() is made for each call to this
78547 ** function. If this were not verified, a subtle bug involving reuse
78548 ** of the aBalanceQuickSpace[] might sneak in.
78549 */
78550 assert( balance_quick_called==0 );
78551 VVA_ONLY( balance_quick_called++ );
78552 rc = balance_quick(pParent, pPage, pSpace: aBalanceQuickSpace);
78553 }else
78554#endif
78555 {
78556 /* In this case, call balance_nonroot() to redistribute cells
78557 ** between pPage and up to 2 of its sibling pages. This involves
78558 ** modifying the contents of pParent, which may cause pParent to
78559 ** become overfull or underfull. The next iteration of the do-loop
78560 ** will balance the parent page to correct this.
78561 **
78562 ** If the parent page becomes overfull, the overflow cell or cells
78563 ** are stored in the pSpace buffer allocated immediately below.
78564 ** A subsequent iteration of the do-loop will deal with this by
78565 ** calling balance_nonroot() (balance_deeper() may be called first,
78566 ** but it doesn't deal with overflow cells - just moves them to a
78567 ** different page). Once this subsequent call to balance_nonroot()
78568 ** has completed, it is safe to release the pSpace buffer used by
78569 ** the previous call, as the overflow cell data will have been
78570 ** copied either into the body of a database page or into the new
78571 ** pSpace buffer passed to the latter call to balance_nonroot().
78572 */
78573 u8 *pSpace = sqlite3PageMalloc(sz: pCur->pBt->pageSize);
78574 rc = balance_nonroot(pParent, iParentIdx: iIdx, aOvflSpace: pSpace, isRoot: iPage==1,
78575 bBulk: pCur->hints&BTREE_BULKLOAD);
78576 if( pFree ){
78577 /* If pFree is not NULL, it points to the pSpace buffer used
78578 ** by a previous call to balance_nonroot(). Its contents are
78579 ** now stored either on real database pages or within the
78580 ** new pSpace buffer, so it may be safely freed here. */
78581 sqlite3PageFree(p: pFree);
78582 }
78583
78584 /* The pSpace buffer will be freed after the next call to
78585 ** balance_nonroot(), or just before this function returns, whichever
78586 ** comes first. */
78587 pFree = pSpace;
78588 }
78589 }
78590
78591 pPage->nOverflow = 0;
78592
78593 /* The next iteration of the do-loop balances the parent page. */
78594 releasePage(pPage);
78595 pCur->iPage--;
78596 assert( pCur->iPage>=0 );
78597 pCur->pPage = pCur->apPage[pCur->iPage];
78598 }
78599 }while( rc==SQLITE_OK );
78600
78601 if( pFree ){
78602 sqlite3PageFree(p: pFree);
78603 }
78604 return rc;
78605}
78606
78607/* Overwrite content from pX into pDest. Only do the write if the
78608** content is different from what is already there.
78609*/
78610static int btreeOverwriteContent(
78611 MemPage *pPage, /* MemPage on which writing will occur */
78612 u8 *pDest, /* Pointer to the place to start writing */
78613 const BtreePayload *pX, /* Source of data to write */
78614 int iOffset, /* Offset of first byte to write */
78615 int iAmt /* Number of bytes to be written */
78616){
78617 int nData = pX->nData - iOffset;
78618 if( nData<=0 ){
78619 /* Overwriting with zeros */
78620 int i;
78621 for(i=0; i<iAmt && pDest[i]==0; i++){}
78622 if( i<iAmt ){
78623 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
78624 if( rc ) return rc;
78625 memset(s: pDest + i, c: 0, n: iAmt - i);
78626 }
78627 }else{
78628 if( nData<iAmt ){
78629 /* Mixed read data and zeros at the end. Make a recursive call
78630 ** to write the zeros then fall through to write the real data */
78631 int rc = btreeOverwriteContent(pPage, pDest: pDest+nData, pX, iOffset: iOffset+nData,
78632 iAmt: iAmt-nData);
78633 if( rc ) return rc;
78634 iAmt = nData;
78635 }
78636 if( memcmp(s1: pDest, s2: ((u8*)pX->pData) + iOffset, n: iAmt)!=0 ){
78637 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
78638 if( rc ) return rc;
78639 /* In a corrupt database, it is possible for the source and destination
78640 ** buffers to overlap. This is harmless since the database is already
78641 ** corrupt but it does cause valgrind and ASAN warnings. So use
78642 ** memmove(). */
78643 memmove(dest: pDest, src: ((u8*)pX->pData) + iOffset, n: iAmt);
78644 }
78645 }
78646 return SQLITE_OK;
78647}
78648
78649/*
78650** Overwrite the cell that cursor pCur is pointing to with fresh content
78651** contained in pX. In this variant, pCur is pointing to an overflow
78652** cell.
78653*/
78654static SQLITE_NOINLINE int btreeOverwriteOverflowCell(
78655 BtCursor *pCur, /* Cursor pointing to cell to overwrite */
78656 const BtreePayload *pX /* Content to write into the cell */
78657){
78658 int iOffset; /* Next byte of pX->pData to write */
78659 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
78660 int rc; /* Return code */
78661 MemPage *pPage = pCur->pPage; /* Page being written */
78662 BtShared *pBt; /* Btree */
78663 Pgno ovflPgno; /* Next overflow page to write */
78664 u32 ovflPageSize; /* Size to write on overflow page */
78665
78666 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
78667
78668 /* Overwrite the local portion first */
78669 rc = btreeOverwriteContent(pPage, pDest: pCur->info.pPayload, pX,
78670 iOffset: 0, iAmt: pCur->info.nLocal);
78671 if( rc ) return rc;
78672
78673 /* Now overwrite the overflow pages */
78674 iOffset = pCur->info.nLocal;
78675 assert( nTotal>=0 );
78676 assert( iOffset>=0 );
78677 ovflPgno = get4byte(p: pCur->info.pPayload + iOffset);
78678 pBt = pPage->pBt;
78679 ovflPageSize = pBt->usableSize - 4;
78680 do{
78681 rc = btreeGetPage(pBt, pgno: ovflPgno, ppPage: &pPage, flags: 0);
78682 if( rc ) return rc;
78683 if( sqlite3PagerPageRefcount(pPage: pPage->pDbPage)!=1 || pPage->isInit ){
78684 rc = SQLITE_CORRUPT_BKPT;
78685 }else{
78686 if( iOffset+ovflPageSize<(u32)nTotal ){
78687 ovflPgno = get4byte(p: pPage->aData);
78688 }else{
78689 ovflPageSize = nTotal - iOffset;
78690 }
78691 rc = btreeOverwriteContent(pPage, pDest: pPage->aData+4, pX,
78692 iOffset, iAmt: ovflPageSize);
78693 }
78694 sqlite3PagerUnref(pPg: pPage->pDbPage);
78695 if( rc ) return rc;
78696 iOffset += ovflPageSize;
78697 }while( iOffset<nTotal );
78698 return SQLITE_OK;
78699}
78700
78701/*
78702** Overwrite the cell that cursor pCur is pointing to with fresh content
78703** contained in pX.
78704*/
78705static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
78706 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
78707 MemPage *pPage = pCur->pPage; /* Page being written */
78708
78709 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
78710 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
78711 ){
78712 return SQLITE_CORRUPT_BKPT;
78713 }
78714 if( pCur->info.nLocal==nTotal ){
78715 /* The entire cell is local */
78716 return btreeOverwriteContent(pPage, pDest: pCur->info.pPayload, pX,
78717 iOffset: 0, iAmt: pCur->info.nLocal);
78718 }else{
78719 /* The cell contains overflow content */
78720 return btreeOverwriteOverflowCell(pCur, pX);
78721 }
78722}
78723
78724
78725/*
78726** Insert a new record into the BTree. The content of the new record
78727** is described by the pX object. The pCur cursor is used only to
78728** define what table the record should be inserted into, and is left
78729** pointing at a random location.
78730**
78731** For a table btree (used for rowid tables), only the pX.nKey value of
78732** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
78733** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
78734** hold the content of the row.
78735**
78736** For an index btree (used for indexes and WITHOUT ROWID tables), the
78737** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
78738** pX.pData,nData,nZero fields must be zero.
78739**
78740** If the seekResult parameter is non-zero, then a successful call to
78741** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
78742** been performed. In other words, if seekResult!=0 then the cursor
78743** is currently pointing to a cell that will be adjacent to the cell
78744** to be inserted. If seekResult<0 then pCur points to a cell that is
78745** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
78746** that is larger than (pKey,nKey).
78747**
78748** If seekResult==0, that means pCur is pointing at some unknown location.
78749** In that case, this routine must seek the cursor to the correct insertion
78750** point for (pKey,nKey) before doing the insertion. For index btrees,
78751** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
78752** key values and pX->aMem can be used instead of pX->pKey to avoid having
78753** to decode the key.
78754*/
78755SQLITE_PRIVATE int sqlite3BtreeInsert(
78756 BtCursor *pCur, /* Insert data into the table of this cursor */
78757 const BtreePayload *pX, /* Content of the row to be inserted */
78758 int flags, /* True if this is likely an append */
78759 int seekResult /* Result of prior IndexMoveto() call */
78760){
78761 int rc;
78762 int loc = seekResult; /* -1: before desired location +1: after */
78763 int szNew = 0;
78764 int idx;
78765 MemPage *pPage;
78766 Btree *p = pCur->pBtree;
78767 unsigned char *oldCell;
78768 unsigned char *newCell = 0;
78769
78770 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
78771 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
78772
78773 /* Save the positions of any other cursors open on this table.
78774 **
78775 ** In some cases, the call to btreeMoveto() below is a no-op. For
78776 ** example, when inserting data into a table with auto-generated integer
78777 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
78778 ** integer key to use. It then calls this function to actually insert the
78779 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
78780 ** that the cursor is already where it needs to be and returns without
78781 ** doing any work. To avoid thwarting these optimizations, it is important
78782 ** not to clear the cursor here.
78783 */
78784 if( pCur->curFlags & BTCF_Multiple ){
78785 rc = saveAllCursors(pBt: p->pBt, iRoot: pCur->pgnoRoot, pExcept: pCur);
78786 if( rc ) return rc;
78787 if( loc && pCur->iPage<0 ){
78788 /* This can only happen if the schema is corrupt such that there is more
78789 ** than one table or index with the same root page as used by the cursor.
78790 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
78791 ** the schema was loaded. This cannot be asserted though, as a user might
78792 ** set the flag, load the schema, and then unset the flag. */
78793 return SQLITE_CORRUPT_BKPT;
78794 }
78795 }
78796
78797 /* Ensure that the cursor is not in the CURSOR_FAULT state and that it
78798 ** points to a valid cell.
78799 */
78800 if( pCur->eState>=CURSOR_REQUIRESEEK ){
78801 testcase( pCur->eState==CURSOR_REQUIRESEEK );
78802 testcase( pCur->eState==CURSOR_FAULT );
78803 rc = moveToRoot(pCur);
78804 if( rc && rc!=SQLITE_EMPTY ) return rc;
78805 }
78806
78807 assert( cursorOwnsBtShared(pCur) );
78808 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
78809 && p->pBt->inTransaction==TRANS_WRITE
78810 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
78811 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
78812
78813 /* Assert that the caller has been consistent. If this cursor was opened
78814 ** expecting an index b-tree, then the caller should be inserting blob
78815 ** keys with no associated data. If the cursor was opened expecting an
78816 ** intkey table, the caller should be inserting integer keys with a
78817 ** blob of associated data. */
78818 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
78819
78820 if( pCur->pKeyInfo==0 ){
78821 assert( pX->pKey==0 );
78822 /* If this is an insert into a table b-tree, invalidate any incrblob
78823 ** cursors open on the row being replaced */
78824 if( p->hasIncrblobCur ){
78825 invalidateIncrblobCursors(pBtree: p, pgnoRoot: pCur->pgnoRoot, iRow: pX->nKey, isClearTable: 0);
78826 }
78827
78828 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
78829 ** to a row with the same key as the new entry being inserted.
78830 */
78831#ifdef SQLITE_DEBUG
78832 if( flags & BTREE_SAVEPOSITION ){
78833 assert( pCur->curFlags & BTCF_ValidNKey );
78834 assert( pX->nKey==pCur->info.nKey );
78835 assert( loc==0 );
78836 }
78837#endif
78838
78839 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
78840 ** that the cursor is not pointing to a row to be overwritten.
78841 ** So do a complete check.
78842 */
78843 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
78844 /* The cursor is pointing to the entry that is to be
78845 ** overwritten */
78846 assert( pX->nData>=0 && pX->nZero>=0 );
78847 if( pCur->info.nSize!=0
78848 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
78849 ){
78850 /* New entry is the same size as the old. Do an overwrite */
78851 return btreeOverwriteCell(pCur, pX);
78852 }
78853 assert( loc==0 );
78854 }else if( loc==0 ){
78855 /* The cursor is *not* pointing to the cell to be overwritten, nor
78856 ** to an adjacent cell. Move the cursor so that it is pointing either
78857 ** to the cell to be overwritten or an adjacent cell.
78858 */
78859 rc = sqlite3BtreeTableMoveto(pCur, intKey: pX->nKey,
78860 biasRight: (flags & BTREE_APPEND)!=0, pRes: &loc);
78861 if( rc ) return rc;
78862 }
78863 }else{
78864 /* This is an index or a WITHOUT ROWID table */
78865
78866 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
78867 ** to a row with the same key as the new entry being inserted.
78868 */
78869 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
78870
78871 /* If the cursor is not already pointing either to the cell to be
78872 ** overwritten, or if a new cell is being inserted, if the cursor is
78873 ** not pointing to an immediately adjacent cell, then move the cursor
78874 ** so that it does.
78875 */
78876 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
78877 if( pX->nMem ){
78878 UnpackedRecord r;
78879 r.pKeyInfo = pCur->pKeyInfo;
78880 r.aMem = pX->aMem;
78881 r.nField = pX->nMem;
78882 r.default_rc = 0;
78883 r.eqSeen = 0;
78884 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey: &r, pRes: &loc);
78885 }else{
78886 rc = btreeMoveto(pCur, pKey: pX->pKey, nKey: pX->nKey,
78887 bias: (flags & BTREE_APPEND)!=0, pRes: &loc);
78888 }
78889 if( rc ) return rc;
78890 }
78891
78892 /* If the cursor is currently pointing to an entry to be overwritten
78893 ** and the new content is the same as as the old, then use the
78894 ** overwrite optimization.
78895 */
78896 if( loc==0 ){
78897 getCellInfo(pCur);
78898 if( pCur->info.nKey==pX->nKey ){
78899 BtreePayload x2;
78900 x2.pData = pX->pKey;
78901 x2.nData = pX->nKey;
78902 x2.nZero = 0;
78903 return btreeOverwriteCell(pCur, pX: &x2);
78904 }
78905 }
78906 }
78907 assert( pCur->eState==CURSOR_VALID
78908 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
78909
78910 pPage = pCur->pPage;
78911 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
78912 assert( pPage->leaf || !pPage->intKey );
78913 if( pPage->nFree<0 ){
78914 if( NEVER(pCur->eState>CURSOR_INVALID) ){
78915 /* ^^^^^--- due to the moveToRoot() call above */
78916 rc = SQLITE_CORRUPT_BKPT;
78917 }else{
78918 rc = btreeComputeFreeSpace(pPage);
78919 }
78920 if( rc ) return rc;
78921 }
78922
78923 TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n",
78924 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
78925 loc==0 ? "overwrite" : "new entry"));
78926 assert( pPage->isInit || CORRUPT_DB );
78927 newCell = p->pBt->pTmpSpace;
78928 assert( newCell!=0 );
78929 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
78930 if( flags & BTREE_PREFORMAT ){
78931 rc = SQLITE_OK;
78932 szNew = p->pBt->nPreformatSize;
78933 if( szNew<4 ) szNew = 4;
78934 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
78935 CellInfo info;
78936 pPage->xParseCell(pPage, newCell, &info);
78937 if( info.nPayload!=info.nLocal ){
78938 Pgno ovfl = get4byte(p: &newCell[szNew-4]);
78939 ptrmapPut(pBt: p->pBt, key: ovfl, PTRMAP_OVERFLOW1, parent: pPage->pgno, pRC: &rc);
78940 if( NEVER(rc) ) goto end_insert;
78941 }
78942 }
78943 }else{
78944 rc = fillInCell(pPage, pCell: newCell, pX, pnSize: &szNew);
78945 if( rc ) goto end_insert;
78946 }
78947 assert( szNew==pPage->xCellSize(pPage, newCell) );
78948 assert( szNew <= MX_CELL_SIZE(p->pBt) );
78949 idx = pCur->ix;
78950 pCur->info.nSize = 0;
78951 if( loc==0 ){
78952 CellInfo info;
78953 assert( idx>=0 );
78954 if( idx>=pPage->nCell ){
78955 return SQLITE_CORRUPT_BKPT;
78956 }
78957 rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
78958 if( rc ){
78959 goto end_insert;
78960 }
78961 oldCell = findCell(pPage, idx);
78962 if( !pPage->leaf ){
78963 memcpy(dest: newCell, src: oldCell, n: 4);
78964 }
78965 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
78966 testcase( pCur->curFlags & BTCF_ValidOvfl );
78967 invalidateOverflowCache(pCur);
78968 if( info.nSize==szNew && info.nLocal==info.nPayload
78969 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
78970 ){
78971 /* Overwrite the old cell with the new if they are the same size.
78972 ** We could also try to do this if the old cell is smaller, then add
78973 ** the leftover space to the free list. But experiments show that
78974 ** doing that is no faster then skipping this optimization and just
78975 ** calling dropCell() and insertCell().
78976 **
78977 ** This optimization cannot be used on an autovacuum database if the
78978 ** new entry uses overflow pages, as the insertCell() call below is
78979 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
78980 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
78981 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
78982 return SQLITE_CORRUPT_BKPT;
78983 }
78984 if( oldCell+szNew > pPage->aDataEnd ){
78985 return SQLITE_CORRUPT_BKPT;
78986 }
78987 memcpy(dest: oldCell, src: newCell, n: szNew);
78988 return SQLITE_OK;
78989 }
78990 dropCell(pPage, idx, sz: info.nSize, pRC: &rc);
78991 if( rc ) goto end_insert;
78992 }else if( loc<0 && pPage->nCell>0 ){
78993 assert( pPage->leaf );
78994 idx = ++pCur->ix;
78995 pCur->curFlags &= ~BTCF_ValidNKey;
78996 }else{
78997 assert( pPage->leaf );
78998 }
78999 rc = insertCellFast(pPage, i: idx, pCell: newCell, sz: szNew);
79000 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
79001 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
79002
79003 /* If no error has occurred and pPage has an overflow cell, call balance()
79004 ** to redistribute the cells within the tree. Since balance() may move
79005 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
79006 ** variables.
79007 **
79008 ** Previous versions of SQLite called moveToRoot() to move the cursor
79009 ** back to the root page as balance() used to invalidate the contents
79010 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
79011 ** set the cursor state to "invalid". This makes common insert operations
79012 ** slightly faster.
79013 **
79014 ** There is a subtle but important optimization here too. When inserting
79015 ** multiple records into an intkey b-tree using a single cursor (as can
79016 ** happen while processing an "INSERT INTO ... SELECT" statement), it
79017 ** is advantageous to leave the cursor pointing to the last entry in
79018 ** the b-tree if possible. If the cursor is left pointing to the last
79019 ** entry in the table, and the next row inserted has an integer key
79020 ** larger than the largest existing key, it is possible to insert the
79021 ** row without seeking the cursor. This can be a big performance boost.
79022 */
79023 if( pPage->nOverflow ){
79024 assert( rc==SQLITE_OK );
79025 pCur->curFlags &= ~(BTCF_ValidNKey);
79026 rc = balance(pCur);
79027
79028 /* Must make sure nOverflow is reset to zero even if the balance()
79029 ** fails. Internal data structure corruption will result otherwise.
79030 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
79031 ** from trying to save the current position of the cursor. */
79032 pCur->pPage->nOverflow = 0;
79033 pCur->eState = CURSOR_INVALID;
79034 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
79035 btreeReleaseAllCursorPages(pCur);
79036 if( pCur->pKeyInfo ){
79037 assert( pCur->pKey==0 );
79038 pCur->pKey = sqlite3Malloc( n: pX->nKey );
79039 if( pCur->pKey==0 ){
79040 rc = SQLITE_NOMEM;
79041 }else{
79042 memcpy(dest: pCur->pKey, src: pX->pKey, n: pX->nKey);
79043 }
79044 }
79045 pCur->eState = CURSOR_REQUIRESEEK;
79046 pCur->nKey = pX->nKey;
79047 }
79048 }
79049 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
79050
79051end_insert:
79052 return rc;
79053}
79054
79055/*
79056** This function is used as part of copying the current row from cursor
79057** pSrc into cursor pDest. If the cursors are open on intkey tables, then
79058** parameter iKey is used as the rowid value when the record is copied
79059** into pDest. Otherwise, the record is copied verbatim.
79060**
79061** This function does not actually write the new value to cursor pDest.
79062** Instead, it creates and populates any required overflow pages and
79063** writes the data for the new cell into the BtShared.pTmpSpace buffer
79064** for the destination database. The size of the cell, in bytes, is left
79065** in BtShared.nPreformatSize. The caller completes the insertion by
79066** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
79067**
79068** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
79069*/
79070SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
79071 BtShared *pBt = pDest->pBt;
79072 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
79073 const u8 *aIn; /* Pointer to next input buffer */
79074 u32 nIn; /* Size of input buffer aIn[] */
79075 u32 nRem; /* Bytes of data still to copy */
79076
79077 getCellInfo(pCur: pSrc);
79078 if( pSrc->info.nPayload<0x80 ){
79079 *(aOut++) = pSrc->info.nPayload;
79080 }else{
79081 aOut += sqlite3PutVarint(p: aOut, v: pSrc->info.nPayload);
79082 }
79083 if( pDest->pKeyInfo==0 ) aOut += putVarint(p: aOut, v: iKey);
79084 nIn = pSrc->info.nLocal;
79085 aIn = pSrc->info.pPayload;
79086 if( aIn+nIn>pSrc->pPage->aDataEnd ){
79087 return SQLITE_CORRUPT_BKPT;
79088 }
79089 nRem = pSrc->info.nPayload;
79090 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
79091 memcpy(dest: aOut, src: aIn, n: nIn);
79092 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
79093 return SQLITE_OK;
79094 }else{
79095 int rc = SQLITE_OK;
79096 Pager *pSrcPager = pSrc->pBt->pPager;
79097 u8 *pPgnoOut = 0;
79098 Pgno ovflIn = 0;
79099 DbPage *pPageIn = 0;
79100 MemPage *pPageOut = 0;
79101 u32 nOut; /* Size of output buffer aOut[] */
79102
79103 nOut = btreePayloadToLocal(pPage: pDest->pPage, nPayload: pSrc->info.nPayload);
79104 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
79105 if( nOut<pSrc->info.nPayload ){
79106 pPgnoOut = &aOut[nOut];
79107 pBt->nPreformatSize += 4;
79108 }
79109
79110 if( nRem>nIn ){
79111 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
79112 return SQLITE_CORRUPT_BKPT;
79113 }
79114 ovflIn = get4byte(p: &pSrc->info.pPayload[nIn]);
79115 }
79116
79117 do {
79118 nRem -= nOut;
79119 do{
79120 assert( nOut>0 );
79121 if( nIn>0 ){
79122 int nCopy = MIN(nOut, nIn);
79123 memcpy(dest: aOut, src: aIn, n: nCopy);
79124 nOut -= nCopy;
79125 nIn -= nCopy;
79126 aOut += nCopy;
79127 aIn += nCopy;
79128 }
79129 if( nOut>0 ){
79130 sqlite3PagerUnref(pPg: pPageIn);
79131 pPageIn = 0;
79132 rc = sqlite3PagerGet(pPager: pSrcPager, pgno: ovflIn, ppPage: &pPageIn, PAGER_GET_READONLY);
79133 if( rc==SQLITE_OK ){
79134 aIn = (const u8*)sqlite3PagerGetData(pPg: pPageIn);
79135 ovflIn = get4byte(p: aIn);
79136 aIn += 4;
79137 nIn = pSrc->pBt->usableSize - 4;
79138 }
79139 }
79140 }while( rc==SQLITE_OK && nOut>0 );
79141
79142 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
79143 Pgno pgnoNew;
79144 MemPage *pNew = 0;
79145 rc = allocateBtreePage(pBt, ppPage: &pNew, pPgno: &pgnoNew, nearby: 0, eMode: 0);
79146 put4byte(p: pPgnoOut, v: pgnoNew);
79147 if( ISAUTOVACUUM(pBt) && pPageOut ){
79148 ptrmapPut(pBt, key: pgnoNew, PTRMAP_OVERFLOW2, parent: pPageOut->pgno, pRC: &rc);
79149 }
79150 releasePage(pPage: pPageOut);
79151 pPageOut = pNew;
79152 if( pPageOut ){
79153 pPgnoOut = pPageOut->aData;
79154 put4byte(p: pPgnoOut, v: 0);
79155 aOut = &pPgnoOut[4];
79156 nOut = MIN(pBt->usableSize - 4, nRem);
79157 }
79158 }
79159 }while( nRem>0 && rc==SQLITE_OK );
79160
79161 releasePage(pPage: pPageOut);
79162 sqlite3PagerUnref(pPg: pPageIn);
79163 return rc;
79164 }
79165}
79166
79167/*
79168** Delete the entry that the cursor is pointing to.
79169**
79170** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
79171** the cursor is left pointing at an arbitrary location after the delete.
79172** But if that bit is set, then the cursor is left in a state such that
79173** the next call to BtreeNext() or BtreePrev() moves it to the same row
79174** as it would have been on if the call to BtreeDelete() had been omitted.
79175**
79176** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
79177** associated with a single table entry and its indexes. Only one of those
79178** deletes is considered the "primary" delete. The primary delete occurs
79179** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
79180** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
79181** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
79182** but which might be used by alternative storage engines.
79183*/
79184SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
79185 Btree *p = pCur->pBtree;
79186 BtShared *pBt = p->pBt;
79187 int rc; /* Return code */
79188 MemPage *pPage; /* Page to delete cell from */
79189 unsigned char *pCell; /* Pointer to cell to delete */
79190 int iCellIdx; /* Index of cell to delete */
79191 int iCellDepth; /* Depth of node containing pCell */
79192 CellInfo info; /* Size of the cell being deleted */
79193 u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */
79194
79195 assert( cursorOwnsBtShared(pCur) );
79196 assert( pBt->inTransaction==TRANS_WRITE );
79197 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
79198 assert( pCur->curFlags & BTCF_WriteFlag );
79199 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
79200 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
79201 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
79202 if( pCur->eState!=CURSOR_VALID ){
79203 if( pCur->eState>=CURSOR_REQUIRESEEK ){
79204 rc = btreeRestoreCursorPosition(pCur);
79205 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
79206 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
79207 }else{
79208 return SQLITE_CORRUPT_BKPT;
79209 }
79210 }
79211 assert( pCur->eState==CURSOR_VALID );
79212
79213 iCellDepth = pCur->iPage;
79214 iCellIdx = pCur->ix;
79215 pPage = pCur->pPage;
79216 if( pPage->nCell<=iCellIdx ){
79217 return SQLITE_CORRUPT_BKPT;
79218 }
79219 pCell = findCell(pPage, iCellIdx);
79220 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
79221 return SQLITE_CORRUPT_BKPT;
79222 }
79223 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
79224 return SQLITE_CORRUPT_BKPT;
79225 }
79226
79227 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
79228 ** be preserved following this delete operation. If the current delete
79229 ** will cause a b-tree rebalance, then this is done by saving the cursor
79230 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
79231 ** returning.
79232 **
79233 ** If the current delete will not cause a rebalance, then the cursor
79234 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
79235 ** before or after the deleted entry.
79236 **
79237 ** The bPreserve value records which path is required:
79238 **
79239 ** bPreserve==0 Not necessary to save the cursor position
79240 ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position
79241 ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT.
79242 */
79243 bPreserve = (flags & BTREE_SAVEPOSITION)!=0;
79244 if( bPreserve ){
79245 if( !pPage->leaf
79246 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
79247 (int)(pBt->usableSize*2/3)
79248 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
79249 ){
79250 /* A b-tree rebalance will be required after deleting this entry.
79251 ** Save the cursor key. */
79252 rc = saveCursorKey(pCur);
79253 if( rc ) return rc;
79254 }else{
79255 bPreserve = 2;
79256 }
79257 }
79258
79259 /* If the page containing the entry to delete is not a leaf page, move
79260 ** the cursor to the largest entry in the tree that is smaller than
79261 ** the entry being deleted. This cell will replace the cell being deleted
79262 ** from the internal node. The 'previous' entry is used for this instead
79263 ** of the 'next' entry, as the previous entry is always a part of the
79264 ** sub-tree headed by the child page of the cell being deleted. This makes
79265 ** balancing the tree following the delete operation easier. */
79266 if( !pPage->leaf ){
79267 rc = sqlite3BtreePrevious(pCur, flags: 0);
79268 assert( rc!=SQLITE_DONE );
79269 if( rc ) return rc;
79270 }
79271
79272 /* Save the positions of any other cursors open on this table before
79273 ** making any modifications. */
79274 if( pCur->curFlags & BTCF_Multiple ){
79275 rc = saveAllCursors(pBt, iRoot: pCur->pgnoRoot, pExcept: pCur);
79276 if( rc ) return rc;
79277 }
79278
79279 /* If this is a delete operation to remove a row from a table b-tree,
79280 ** invalidate any incrblob cursors open on the row being deleted. */
79281 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
79282 invalidateIncrblobCursors(pBtree: p, pgnoRoot: pCur->pgnoRoot, iRow: pCur->info.nKey, isClearTable: 0);
79283 }
79284
79285 /* Make the page containing the entry to be deleted writable. Then free any
79286 ** overflow pages associated with the entry and finally remove the cell
79287 ** itself from within the page. */
79288 rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
79289 if( rc ) return rc;
79290 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
79291 dropCell(pPage, idx: iCellIdx, sz: info.nSize, pRC: &rc);
79292 if( rc ) return rc;
79293
79294 /* If the cell deleted was not located on a leaf page, then the cursor
79295 ** is currently pointing to the largest entry in the sub-tree headed
79296 ** by the child-page of the cell that was just deleted from an internal
79297 ** node. The cell from the leaf node needs to be moved to the internal
79298 ** node to replace the deleted cell. */
79299 if( !pPage->leaf ){
79300 MemPage *pLeaf = pCur->pPage;
79301 int nCell;
79302 Pgno n;
79303 unsigned char *pTmp;
79304
79305 if( pLeaf->nFree<0 ){
79306 rc = btreeComputeFreeSpace(pPage: pLeaf);
79307 if( rc ) return rc;
79308 }
79309 if( iCellDepth<pCur->iPage-1 ){
79310 n = pCur->apPage[iCellDepth+1]->pgno;
79311 }else{
79312 n = pCur->pPage->pgno;
79313 }
79314 pCell = findCell(pLeaf, pLeaf->nCell-1);
79315 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
79316 nCell = pLeaf->xCellSize(pLeaf, pCell);
79317 assert( MX_CELL_SIZE(pBt) >= nCell );
79318 pTmp = pBt->pTmpSpace;
79319 assert( pTmp!=0 );
79320 rc = sqlite3PagerWrite(pPg: pLeaf->pDbPage);
79321 if( rc==SQLITE_OK ){
79322 rc = insertCell(pPage, i: iCellIdx, pCell: pCell-4, sz: nCell+4, pTemp: pTmp, iChild: n);
79323 }
79324 dropCell(pPage: pLeaf, idx: pLeaf->nCell-1, sz: nCell, pRC: &rc);
79325 if( rc ) return rc;
79326 }
79327
79328 /* Balance the tree. If the entry deleted was located on a leaf page,
79329 ** then the cursor still points to that page. In this case the first
79330 ** call to balance() repairs the tree, and the if(...) condition is
79331 ** never true.
79332 **
79333 ** Otherwise, if the entry deleted was on an internal node page, then
79334 ** pCur is pointing to the leaf page from which a cell was removed to
79335 ** replace the cell deleted from the internal node. This is slightly
79336 ** tricky as the leaf node may be underfull, and the internal node may
79337 ** be either under or overfull. In this case run the balancing algorithm
79338 ** on the leaf node first. If the balance proceeds far enough up the
79339 ** tree that we can be sure that any problem in the internal node has
79340 ** been corrected, so be it. Otherwise, after balancing the leaf node,
79341 ** walk the cursor up the tree to the internal node and balance it as
79342 ** well. */
79343 assert( pCur->pPage->nOverflow==0 );
79344 assert( pCur->pPage->nFree>=0 );
79345 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
79346 /* Optimization: If the free space is less than 2/3rds of the page,
79347 ** then balance() will always be a no-op. No need to invoke it. */
79348 rc = SQLITE_OK;
79349 }else{
79350 rc = balance(pCur);
79351 }
79352 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
79353 releasePageNotNull(pPage: pCur->pPage);
79354 pCur->iPage--;
79355 while( pCur->iPage>iCellDepth ){
79356 releasePage(pPage: pCur->apPage[pCur->iPage--]);
79357 }
79358 pCur->pPage = pCur->apPage[pCur->iPage];
79359 rc = balance(pCur);
79360 }
79361
79362 if( rc==SQLITE_OK ){
79363 if( bPreserve>1 ){
79364 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
79365 assert( pPage==pCur->pPage || CORRUPT_DB );
79366 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
79367 pCur->eState = CURSOR_SKIPNEXT;
79368 if( iCellIdx>=pPage->nCell ){
79369 pCur->skipNext = -1;
79370 pCur->ix = pPage->nCell-1;
79371 }else{
79372 pCur->skipNext = 1;
79373 }
79374 }else{
79375 rc = moveToRoot(pCur);
79376 if( bPreserve ){
79377 btreeReleaseAllCursorPages(pCur);
79378 pCur->eState = CURSOR_REQUIRESEEK;
79379 }
79380 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
79381 }
79382 }
79383 return rc;
79384}
79385
79386/*
79387** Create a new BTree table. Write into *piTable the page
79388** number for the root page of the new table.
79389**
79390** The type of type is determined by the flags parameter. Only the
79391** following values of flags are currently in use. Other values for
79392** flags might not work:
79393**
79394** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
79395** BTREE_ZERODATA Used for SQL indices
79396*/
79397static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
79398 BtShared *pBt = p->pBt;
79399 MemPage *pRoot;
79400 Pgno pgnoRoot;
79401 int rc;
79402 int ptfFlags; /* Page-type flags for the root page of new table */
79403
79404 assert( sqlite3BtreeHoldsMutex(p) );
79405 assert( pBt->inTransaction==TRANS_WRITE );
79406 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
79407
79408#ifdef SQLITE_OMIT_AUTOVACUUM
79409 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
79410 if( rc ){
79411 return rc;
79412 }
79413#else
79414 if( pBt->autoVacuum ){
79415 Pgno pgnoMove; /* Move a page here to make room for the root-page */
79416 MemPage *pPageMove; /* The page to move to. */
79417
79418 /* Creating a new table may probably require moving an existing database
79419 ** to make room for the new tables root page. In case this page turns
79420 ** out to be an overflow page, delete all overflow page-map caches
79421 ** held by open cursors.
79422 */
79423 invalidateAllOverflowCache(pBt);
79424
79425 /* Read the value of meta[3] from the database to determine where the
79426 ** root page of the new table should go. meta[3] is the largest root-page
79427 ** created so far, so the new root-page is (meta[3]+1).
79428 */
79429 sqlite3BtreeGetMeta(pBtree: p, BTREE_LARGEST_ROOT_PAGE, pValue: &pgnoRoot);
79430 if( pgnoRoot>btreePagecount(pBt) ){
79431 return SQLITE_CORRUPT_BKPT;
79432 }
79433 pgnoRoot++;
79434
79435 /* The new root-page may not be allocated on a pointer-map page, or the
79436 ** PENDING_BYTE page.
79437 */
79438 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
79439 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
79440 pgnoRoot++;
79441 }
79442 assert( pgnoRoot>=3 );
79443
79444 /* Allocate a page. The page that currently resides at pgnoRoot will
79445 ** be moved to the allocated page (unless the allocated page happens
79446 ** to reside at pgnoRoot).
79447 */
79448 rc = allocateBtreePage(pBt, ppPage: &pPageMove, pPgno: &pgnoMove, nearby: pgnoRoot, BTALLOC_EXACT);
79449 if( rc!=SQLITE_OK ){
79450 return rc;
79451 }
79452
79453 if( pgnoMove!=pgnoRoot ){
79454 /* pgnoRoot is the page that will be used for the root-page of
79455 ** the new table (assuming an error did not occur). But we were
79456 ** allocated pgnoMove. If required (i.e. if it was not allocated
79457 ** by extending the file), the current page at position pgnoMove
79458 ** is already journaled.
79459 */
79460 u8 eType = 0;
79461 Pgno iPtrPage = 0;
79462
79463 /* Save the positions of any open cursors. This is required in
79464 ** case they are holding a reference to an xFetch reference
79465 ** corresponding to page pgnoRoot. */
79466 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
79467 releasePage(pPage: pPageMove);
79468 if( rc!=SQLITE_OK ){
79469 return rc;
79470 }
79471
79472 /* Move the page currently at pgnoRoot to pgnoMove. */
79473 rc = btreeGetPage(pBt, pgno: pgnoRoot, ppPage: &pRoot, flags: 0);
79474 if( rc!=SQLITE_OK ){
79475 return rc;
79476 }
79477 rc = ptrmapGet(pBt, key: pgnoRoot, pEType: &eType, pPgno: &iPtrPage);
79478 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
79479 rc = SQLITE_CORRUPT_BKPT;
79480 }
79481 if( rc!=SQLITE_OK ){
79482 releasePage(pPage: pRoot);
79483 return rc;
79484 }
79485 assert( eType!=PTRMAP_ROOTPAGE );
79486 assert( eType!=PTRMAP_FREEPAGE );
79487 rc = relocatePage(pBt, pDbPage: pRoot, eType, iPtrPage, iFreePage: pgnoMove, isCommit: 0);
79488 releasePage(pPage: pRoot);
79489
79490 /* Obtain the page at pgnoRoot */
79491 if( rc!=SQLITE_OK ){
79492 return rc;
79493 }
79494 rc = btreeGetPage(pBt, pgno: pgnoRoot, ppPage: &pRoot, flags: 0);
79495 if( rc!=SQLITE_OK ){
79496 return rc;
79497 }
79498 rc = sqlite3PagerWrite(pPg: pRoot->pDbPage);
79499 if( rc!=SQLITE_OK ){
79500 releasePage(pPage: pRoot);
79501 return rc;
79502 }
79503 }else{
79504 pRoot = pPageMove;
79505 }
79506
79507 /* Update the pointer-map and meta-data with the new root-page number. */
79508 ptrmapPut(pBt, key: pgnoRoot, PTRMAP_ROOTPAGE, parent: 0, pRC: &rc);
79509 if( rc ){
79510 releasePage(pPage: pRoot);
79511 return rc;
79512 }
79513
79514 /* When the new root page was allocated, page 1 was made writable in
79515 ** order either to increase the database filesize, or to decrement the
79516 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
79517 */
79518 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
79519 rc = sqlite3BtreeUpdateMeta(p, idx: 4, value: pgnoRoot);
79520 if( NEVER(rc) ){
79521 releasePage(pPage: pRoot);
79522 return rc;
79523 }
79524
79525 }else{
79526 rc = allocateBtreePage(pBt, ppPage: &pRoot, pPgno: &pgnoRoot, nearby: 1, eMode: 0);
79527 if( rc ) return rc;
79528 }
79529#endif
79530 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
79531 if( createTabFlags & BTREE_INTKEY ){
79532 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
79533 }else{
79534 ptfFlags = PTF_ZERODATA | PTF_LEAF;
79535 }
79536 zeroPage(pPage: pRoot, flags: ptfFlags);
79537 sqlite3PagerUnref(pPg: pRoot->pDbPage);
79538 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
79539 *piTable = pgnoRoot;
79540 return SQLITE_OK;
79541}
79542SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
79543 int rc;
79544 sqlite3BtreeEnter(p);
79545 rc = btreeCreateTable(p, piTable, createTabFlags: flags);
79546 sqlite3BtreeLeave(p);
79547 return rc;
79548}
79549
79550/*
79551** Erase the given database page and all its children. Return
79552** the page to the freelist.
79553*/
79554static int clearDatabasePage(
79555 BtShared *pBt, /* The BTree that contains the table */
79556 Pgno pgno, /* Page number to clear */
79557 int freePageFlag, /* Deallocate page if true */
79558 i64 *pnChange /* Add number of Cells freed to this counter */
79559){
79560 MemPage *pPage;
79561 int rc;
79562 unsigned char *pCell;
79563 int i;
79564 int hdr;
79565 CellInfo info;
79566
79567 assert( sqlite3_mutex_held(pBt->mutex) );
79568 if( pgno>btreePagecount(pBt) ){
79569 return SQLITE_CORRUPT_BKPT;
79570 }
79571 rc = getAndInitPage(pBt, pgno, ppPage: &pPage, bReadOnly: 0);
79572 if( rc ) return rc;
79573 if( (pBt->openFlags & BTREE_SINGLE)==0
79574 && sqlite3PagerPageRefcount(pPage: pPage->pDbPage) != (1 + (pgno==1))
79575 ){
79576 rc = SQLITE_CORRUPT_BKPT;
79577 goto cleardatabasepage_out;
79578 }
79579 hdr = pPage->hdrOffset;
79580 for(i=0; i<pPage->nCell; i++){
79581 pCell = findCell(pPage, i);
79582 if( !pPage->leaf ){
79583 rc = clearDatabasePage(pBt, get4byte(p: pCell), freePageFlag: 1, pnChange);
79584 if( rc ) goto cleardatabasepage_out;
79585 }
79586 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
79587 if( rc ) goto cleardatabasepage_out;
79588 }
79589 if( !pPage->leaf ){
79590 rc = clearDatabasePage(pBt, get4byte(p: &pPage->aData[hdr+8]), freePageFlag: 1, pnChange);
79591 if( rc ) goto cleardatabasepage_out;
79592 if( pPage->intKey ) pnChange = 0;
79593 }
79594 if( pnChange ){
79595 testcase( !pPage->intKey );
79596 *pnChange += pPage->nCell;
79597 }
79598 if( freePageFlag ){
79599 freePage(pPage, pRC: &rc);
79600 }else if( (rc = sqlite3PagerWrite(pPg: pPage->pDbPage))==0 ){
79601 zeroPage(pPage, flags: pPage->aData[hdr] | PTF_LEAF);
79602 }
79603
79604cleardatabasepage_out:
79605 releasePage(pPage);
79606 return rc;
79607}
79608
79609/*
79610** Delete all information from a single table in the database. iTable is
79611** the page number of the root of the table. After this routine returns,
79612** the root page is empty, but still exists.
79613**
79614** This routine will fail with SQLITE_LOCKED if there are any open
79615** read cursors on the table. Open write cursors are moved to the
79616** root of the table.
79617**
79618** If pnChange is not NULL, then the integer value pointed to by pnChange
79619** is incremented by the number of entries in the table.
79620*/
79621SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
79622 int rc;
79623 BtShared *pBt = p->pBt;
79624 sqlite3BtreeEnter(p);
79625 assert( p->inTrans==TRANS_WRITE );
79626
79627 rc = saveAllCursors(pBt, iRoot: (Pgno)iTable, pExcept: 0);
79628
79629 if( SQLITE_OK==rc ){
79630 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
79631 ** is the root of a table b-tree - if it is not, the following call is
79632 ** a no-op). */
79633 if( p->hasIncrblobCur ){
79634 invalidateIncrblobCursors(pBtree: p, pgnoRoot: (Pgno)iTable, iRow: 0, isClearTable: 1);
79635 }
79636 rc = clearDatabasePage(pBt, pgno: (Pgno)iTable, freePageFlag: 0, pnChange);
79637 }
79638 sqlite3BtreeLeave(p);
79639 return rc;
79640}
79641
79642/*
79643** Delete all information from the single table that pCur is open on.
79644**
79645** This routine only work for pCur on an ephemeral table.
79646*/
79647SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
79648 return sqlite3BtreeClearTable(p: pCur->pBtree, iTable: pCur->pgnoRoot, pnChange: 0);
79649}
79650
79651/*
79652** Erase all information in a table and add the root of the table to
79653** the freelist. Except, the root of the principle table (the one on
79654** page 1) is never added to the freelist.
79655**
79656** This routine will fail with SQLITE_LOCKED if there are any open
79657** cursors on the table.
79658**
79659** If AUTOVACUUM is enabled and the page at iTable is not the last
79660** root page in the database file, then the last root page
79661** in the database file is moved into the slot formerly occupied by
79662** iTable and that last slot formerly occupied by the last root page
79663** is added to the freelist instead of iTable. In this say, all
79664** root pages are kept at the beginning of the database file, which
79665** is necessary for AUTOVACUUM to work right. *piMoved is set to the
79666** page number that used to be the last root page in the file before
79667** the move. If no page gets moved, *piMoved is set to 0.
79668** The last root page is recorded in meta[3] and the value of
79669** meta[3] is updated by this procedure.
79670*/
79671static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
79672 int rc;
79673 MemPage *pPage = 0;
79674 BtShared *pBt = p->pBt;
79675
79676 assert( sqlite3BtreeHoldsMutex(p) );
79677 assert( p->inTrans==TRANS_WRITE );
79678 assert( iTable>=2 );
79679 if( iTable>btreePagecount(pBt) ){
79680 return SQLITE_CORRUPT_BKPT;
79681 }
79682
79683 rc = sqlite3BtreeClearTable(p, iTable, pnChange: 0);
79684 if( rc ) return rc;
79685 rc = btreeGetPage(pBt, pgno: (Pgno)iTable, ppPage: &pPage, flags: 0);
79686 if( NEVER(rc) ){
79687 releasePage(pPage);
79688 return rc;
79689 }
79690
79691 *piMoved = 0;
79692
79693#ifdef SQLITE_OMIT_AUTOVACUUM
79694 freePage(pPage, &rc);
79695 releasePage(pPage);
79696#else
79697 if( pBt->autoVacuum ){
79698 Pgno maxRootPgno;
79699 sqlite3BtreeGetMeta(pBtree: p, BTREE_LARGEST_ROOT_PAGE, pValue: &maxRootPgno);
79700
79701 if( iTable==maxRootPgno ){
79702 /* If the table being dropped is the table with the largest root-page
79703 ** number in the database, put the root page on the free list.
79704 */
79705 freePage(pPage, pRC: &rc);
79706 releasePage(pPage);
79707 if( rc!=SQLITE_OK ){
79708 return rc;
79709 }
79710 }else{
79711 /* The table being dropped does not have the largest root-page
79712 ** number in the database. So move the page that does into the
79713 ** gap left by the deleted root-page.
79714 */
79715 MemPage *pMove;
79716 releasePage(pPage);
79717 rc = btreeGetPage(pBt, pgno: maxRootPgno, ppPage: &pMove, flags: 0);
79718 if( rc!=SQLITE_OK ){
79719 return rc;
79720 }
79721 rc = relocatePage(pBt, pDbPage: pMove, PTRMAP_ROOTPAGE, iPtrPage: 0, iFreePage: iTable, isCommit: 0);
79722 releasePage(pPage: pMove);
79723 if( rc!=SQLITE_OK ){
79724 return rc;
79725 }
79726 pMove = 0;
79727 rc = btreeGetPage(pBt, pgno: maxRootPgno, ppPage: &pMove, flags: 0);
79728 freePage(pPage: pMove, pRC: &rc);
79729 releasePage(pPage: pMove);
79730 if( rc!=SQLITE_OK ){
79731 return rc;
79732 }
79733 *piMoved = maxRootPgno;
79734 }
79735
79736 /* Set the new 'max-root-page' value in the database header. This
79737 ** is the old value less one, less one more if that happens to
79738 ** be a root-page number, less one again if that is the
79739 ** PENDING_BYTE_PAGE.
79740 */
79741 maxRootPgno--;
79742 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
79743 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
79744 maxRootPgno--;
79745 }
79746 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
79747
79748 rc = sqlite3BtreeUpdateMeta(p, idx: 4, value: maxRootPgno);
79749 }else{
79750 freePage(pPage, pRC: &rc);
79751 releasePage(pPage);
79752 }
79753#endif
79754 return rc;
79755}
79756SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
79757 int rc;
79758 sqlite3BtreeEnter(p);
79759 rc = btreeDropTable(p, iTable, piMoved);
79760 sqlite3BtreeLeave(p);
79761 return rc;
79762}
79763
79764
79765/*
79766** This function may only be called if the b-tree connection already
79767** has a read or write transaction open on the database.
79768**
79769** Read the meta-information out of a database file. Meta[0]
79770** is the number of free pages currently in the database. Meta[1]
79771** through meta[15] are available for use by higher layers. Meta[0]
79772** is read-only, the others are read/write.
79773**
79774** The schema layer numbers meta values differently. At the schema
79775** layer (and the SetCookie and ReadCookie opcodes) the number of
79776** free pages is not visible. So Cookie[0] is the same as Meta[1].
79777**
79778** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
79779** of reading the value out of the header, it instead loads the "DataVersion"
79780** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
79781** database file. It is a number computed by the pager. But its access
79782** pattern is the same as header meta values, and so it is convenient to
79783** read it from this routine.
79784*/
79785SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
79786 BtShared *pBt = p->pBt;
79787
79788 sqlite3BtreeEnter(p);
79789 assert( p->inTrans>TRANS_NONE );
79790 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) );
79791 assert( pBt->pPage1 );
79792 assert( idx>=0 && idx<=15 );
79793
79794 if( idx==BTREE_DATA_VERSION ){
79795 *pMeta = sqlite3PagerDataVersion(pPager: pBt->pPager) + p->iBDataVersion;
79796 }else{
79797 *pMeta = get4byte(p: &pBt->pPage1->aData[36 + idx*4]);
79798 }
79799
79800 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
79801 ** database, mark the database as read-only. */
79802#ifdef SQLITE_OMIT_AUTOVACUUM
79803 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
79804 pBt->btsFlags |= BTS_READ_ONLY;
79805 }
79806#endif
79807
79808 sqlite3BtreeLeave(p);
79809}
79810
79811/*
79812** Write meta-information back into the database. Meta[0] is
79813** read-only and may not be written.
79814*/
79815SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
79816 BtShared *pBt = p->pBt;
79817 unsigned char *pP1;
79818 int rc;
79819 assert( idx>=1 && idx<=15 );
79820 sqlite3BtreeEnter(p);
79821 assert( p->inTrans==TRANS_WRITE );
79822 assert( pBt->pPage1!=0 );
79823 pP1 = pBt->pPage1->aData;
79824 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
79825 if( rc==SQLITE_OK ){
79826 put4byte(p: &pP1[36 + idx*4], v: iMeta);
79827#ifndef SQLITE_OMIT_AUTOVACUUM
79828 if( idx==BTREE_INCR_VACUUM ){
79829 assert( pBt->autoVacuum || iMeta==0 );
79830 assert( iMeta==0 || iMeta==1 );
79831 pBt->incrVacuum = (u8)iMeta;
79832 }
79833#endif
79834 }
79835 sqlite3BtreeLeave(p);
79836 return rc;
79837}
79838
79839/*
79840** The first argument, pCur, is a cursor opened on some b-tree. Count the
79841** number of entries in the b-tree and write the result to *pnEntry.
79842**
79843** SQLITE_OK is returned if the operation is successfully executed.
79844** Otherwise, if an error is encountered (i.e. an IO error or database
79845** corruption) an SQLite error code is returned.
79846*/
79847SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
79848 i64 nEntry = 0; /* Value to return in *pnEntry */
79849 int rc; /* Return code */
79850
79851 rc = moveToRoot(pCur);
79852 if( rc==SQLITE_EMPTY ){
79853 *pnEntry = 0;
79854 return SQLITE_OK;
79855 }
79856
79857 /* Unless an error occurs, the following loop runs one iteration for each
79858 ** page in the B-Tree structure (not including overflow pages).
79859 */
79860 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
79861 int iIdx; /* Index of child node in parent */
79862 MemPage *pPage; /* Current page of the b-tree */
79863
79864 /* If this is a leaf page or the tree is not an int-key tree, then
79865 ** this page contains countable entries. Increment the entry counter
79866 ** accordingly.
79867 */
79868 pPage = pCur->pPage;
79869 if( pPage->leaf || !pPage->intKey ){
79870 nEntry += pPage->nCell;
79871 }
79872
79873 /* pPage is a leaf node. This loop navigates the cursor so that it
79874 ** points to the first interior cell that it points to the parent of
79875 ** the next page in the tree that has not yet been visited. The
79876 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
79877 ** of the page, or to the number of cells in the page if the next page
79878 ** to visit is the right-child of its parent.
79879 **
79880 ** If all pages in the tree have been visited, return SQLITE_OK to the
79881 ** caller.
79882 */
79883 if( pPage->leaf ){
79884 do {
79885 if( pCur->iPage==0 ){
79886 /* All pages of the b-tree have been visited. Return successfully. */
79887 *pnEntry = nEntry;
79888 return moveToRoot(pCur);
79889 }
79890 moveToParent(pCur);
79891 }while ( pCur->ix>=pCur->pPage->nCell );
79892
79893 pCur->ix++;
79894 pPage = pCur->pPage;
79895 }
79896
79897 /* Descend to the child node of the cell that the cursor currently
79898 ** points at. This is the right-child if (iIdx==pPage->nCell).
79899 */
79900 iIdx = pCur->ix;
79901 if( iIdx==pPage->nCell ){
79902 rc = moveToChild(pCur, get4byte(p: &pPage->aData[pPage->hdrOffset+8]));
79903 }else{
79904 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
79905 }
79906 }
79907
79908 /* An error has occurred. Return an error code. */
79909 return rc;
79910}
79911
79912/*
79913** Return the pager associated with a BTree. This routine is used for
79914** testing and debugging only.
79915*/
79916SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
79917 return p->pBt->pPager;
79918}
79919
79920#ifndef SQLITE_OMIT_INTEGRITY_CHECK
79921/*
79922** Record an OOM error during integrity_check
79923*/
79924static void checkOom(IntegrityCk *pCheck){
79925 pCheck->rc = SQLITE_NOMEM;
79926 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
79927 if( pCheck->nErr==0 ) pCheck->nErr++;
79928}
79929
79930/*
79931** Invoke the progress handler, if appropriate. Also check for an
79932** interrupt.
79933*/
79934static void checkProgress(IntegrityCk *pCheck){
79935 sqlite3 *db = pCheck->db;
79936 if( AtomicLoad(&db->u1.isInterrupted) ){
79937 pCheck->rc = SQLITE_INTERRUPT;
79938 pCheck->nErr++;
79939 pCheck->mxErr = 0;
79940 }
79941#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
79942 if( db->xProgress ){
79943 assert( db->nProgressOps>0 );
79944 pCheck->nStep++;
79945 if( (pCheck->nStep % db->nProgressOps)==0
79946 && db->xProgress(db->pProgressArg)
79947 ){
79948 pCheck->rc = SQLITE_INTERRUPT;
79949 pCheck->nErr++;
79950 pCheck->mxErr = 0;
79951 }
79952 }
79953#endif
79954}
79955
79956/*
79957** Append a message to the error message string.
79958*/
79959static void checkAppendMsg(
79960 IntegrityCk *pCheck,
79961 const char *zFormat,
79962 ...
79963){
79964 va_list ap;
79965 checkProgress(pCheck);
79966 if( !pCheck->mxErr ) return;
79967 pCheck->mxErr--;
79968 pCheck->nErr++;
79969 va_start(ap, zFormat);
79970 if( pCheck->errMsg.nChar ){
79971 sqlite3_str_append(p: &pCheck->errMsg, z: "\n", N: 1);
79972 }
79973 if( pCheck->zPfx ){
79974 sqlite3_str_appendf(p: &pCheck->errMsg, zFormat: pCheck->zPfx,
79975 pCheck->v0, pCheck->v1, pCheck->v2);
79976 }
79977 sqlite3_str_vappendf(pAccum: &pCheck->errMsg, fmt: zFormat, ap);
79978 va_end(ap);
79979 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
79980 checkOom(pCheck);
79981 }
79982}
79983#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
79984
79985#ifndef SQLITE_OMIT_INTEGRITY_CHECK
79986
79987/*
79988** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
79989** corresponds to page iPg is already set.
79990*/
79991static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
79992 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
79993 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
79994}
79995
79996/*
79997** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
79998*/
79999static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
80000 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
80001 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
80002}
80003
80004
80005/*
80006** Add 1 to the reference count for page iPage. If this is the second
80007** reference to the page, add an error message to pCheck->zErrMsg.
80008** Return 1 if there are 2 or more references to the page and 0 if
80009** if this is the first reference to the page.
80010**
80011** Also check that the page number is in bounds.
80012*/
80013static int checkRef(IntegrityCk *pCheck, Pgno iPage){
80014 if( iPage>pCheck->nPage || iPage==0 ){
80015 checkAppendMsg(pCheck, zFormat: "invalid page number %u", iPage);
80016 return 1;
80017 }
80018 if( getPageReferenced(pCheck, iPg: iPage) ){
80019 checkAppendMsg(pCheck, zFormat: "2nd reference to page %u", iPage);
80020 return 1;
80021 }
80022 setPageReferenced(pCheck, iPg: iPage);
80023 return 0;
80024}
80025
80026#ifndef SQLITE_OMIT_AUTOVACUUM
80027/*
80028** Check that the entry in the pointer-map for page iChild maps to
80029** page iParent, pointer type ptrType. If not, append an error message
80030** to pCheck.
80031*/
80032static void checkPtrmap(
80033 IntegrityCk *pCheck, /* Integrity check context */
80034 Pgno iChild, /* Child page number */
80035 u8 eType, /* Expected pointer map type */
80036 Pgno iParent /* Expected pointer map parent page number */
80037){
80038 int rc;
80039 u8 ePtrmapType;
80040 Pgno iPtrmapParent;
80041
80042 rc = ptrmapGet(pBt: pCheck->pBt, key: iChild, pEType: &ePtrmapType, pPgno: &iPtrmapParent);
80043 if( rc!=SQLITE_OK ){
80044 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) checkOom(pCheck);
80045 checkAppendMsg(pCheck, zFormat: "Failed to read ptrmap key=%u", iChild);
80046 return;
80047 }
80048
80049 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
80050 checkAppendMsg(pCheck,
80051 zFormat: "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)",
80052 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
80053 }
80054}
80055#endif
80056
80057/*
80058** Check the integrity of the freelist or of an overflow page list.
80059** Verify that the number of pages on the list is N.
80060*/
80061static void checkList(
80062 IntegrityCk *pCheck, /* Integrity checking context */
80063 int isFreeList, /* True for a freelist. False for overflow page list */
80064 Pgno iPage, /* Page number for first page in the list */
80065 u32 N /* Expected number of pages in the list */
80066){
80067 int i;
80068 u32 expected = N;
80069 int nErrAtStart = pCheck->nErr;
80070 while( iPage!=0 && pCheck->mxErr ){
80071 DbPage *pOvflPage;
80072 unsigned char *pOvflData;
80073 if( checkRef(pCheck, iPage) ) break;
80074 N--;
80075 if( sqlite3PagerGet(pPager: pCheck->pPager, pgno: (Pgno)iPage, ppPage: &pOvflPage, flags: 0) ){
80076 checkAppendMsg(pCheck, zFormat: "failed to get page %u", iPage);
80077 break;
80078 }
80079 pOvflData = (unsigned char *)sqlite3PagerGetData(pPg: pOvflPage);
80080 if( isFreeList ){
80081 u32 n = (u32)get4byte(p: &pOvflData[4]);
80082#ifndef SQLITE_OMIT_AUTOVACUUM
80083 if( pCheck->pBt->autoVacuum ){
80084 checkPtrmap(pCheck, iChild: iPage, PTRMAP_FREEPAGE, iParent: 0);
80085 }
80086#endif
80087 if( n>pCheck->pBt->usableSize/4-2 ){
80088 checkAppendMsg(pCheck,
80089 zFormat: "freelist leaf count too big on page %u", iPage);
80090 N--;
80091 }else{
80092 for(i=0; i<(int)n; i++){
80093 Pgno iFreePage = get4byte(p: &pOvflData[8+i*4]);
80094#ifndef SQLITE_OMIT_AUTOVACUUM
80095 if( pCheck->pBt->autoVacuum ){
80096 checkPtrmap(pCheck, iChild: iFreePage, PTRMAP_FREEPAGE, iParent: 0);
80097 }
80098#endif
80099 checkRef(pCheck, iPage: iFreePage);
80100 }
80101 N -= n;
80102 }
80103 }
80104#ifndef SQLITE_OMIT_AUTOVACUUM
80105 else{
80106 /* If this database supports auto-vacuum and iPage is not the last
80107 ** page in this overflow list, check that the pointer-map entry for
80108 ** the following page matches iPage.
80109 */
80110 if( pCheck->pBt->autoVacuum && N>0 ){
80111 i = get4byte(p: pOvflData);
80112 checkPtrmap(pCheck, iChild: i, PTRMAP_OVERFLOW2, iParent: iPage);
80113 }
80114 }
80115#endif
80116 iPage = get4byte(p: pOvflData);
80117 sqlite3PagerUnref(pPg: pOvflPage);
80118 }
80119 if( N && nErrAtStart==pCheck->nErr ){
80120 checkAppendMsg(pCheck,
80121 zFormat: "%s is %u but should be %u",
80122 isFreeList ? "size" : "overflow list length",
80123 expected-N, expected);
80124 }
80125}
80126#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
80127
80128/*
80129** An implementation of a min-heap.
80130**
80131** aHeap[0] is the number of elements on the heap. aHeap[1] is the
80132** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
80133** and aHeap[N*2+1].
80134**
80135** The heap property is this: Every node is less than or equal to both
80136** of its daughter nodes. A consequence of the heap property is that the
80137** root node aHeap[1] is always the minimum value currently in the heap.
80138**
80139** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
80140** the heap, preserving the heap property. The btreeHeapPull() routine
80141** removes the root element from the heap (the minimum value in the heap)
80142** and then moves other nodes around as necessary to preserve the heap
80143** property.
80144**
80145** This heap is used for cell overlap and coverage testing. Each u32
80146** entry represents the span of a cell or freeblock on a btree page.
80147** The upper 16 bits are the index of the first byte of a range and the
80148** lower 16 bits are the index of the last byte of that range.
80149*/
80150static void btreeHeapInsert(u32 *aHeap, u32 x){
80151 u32 j, i;
80152 assert( aHeap!=0 );
80153 i = ++aHeap[0];
80154 aHeap[i] = x;
80155 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
80156 x = aHeap[j];
80157 aHeap[j] = aHeap[i];
80158 aHeap[i] = x;
80159 i = j;
80160 }
80161}
80162static int btreeHeapPull(u32 *aHeap, u32 *pOut){
80163 u32 j, i, x;
80164 if( (x = aHeap[0])==0 ) return 0;
80165 *pOut = aHeap[1];
80166 aHeap[1] = aHeap[x];
80167 aHeap[x] = 0xffffffff;
80168 aHeap[0]--;
80169 i = 1;
80170 while( (j = i*2)<=aHeap[0] ){
80171 if( aHeap[j]>aHeap[j+1] ) j++;
80172 if( aHeap[i]<aHeap[j] ) break;
80173 x = aHeap[i];
80174 aHeap[i] = aHeap[j];
80175 aHeap[j] = x;
80176 i = j;
80177 }
80178 return 1;
80179}
80180
80181#ifndef SQLITE_OMIT_INTEGRITY_CHECK
80182/*
80183** Do various sanity checks on a single page of a tree. Return
80184** the tree depth. Root pages return 0. Parents of root pages
80185** return 1, and so forth.
80186**
80187** These checks are done:
80188**
80189** 1. Make sure that cells and freeblocks do not overlap
80190** but combine to completely cover the page.
80191** 2. Make sure integer cell keys are in order.
80192** 3. Check the integrity of overflow pages.
80193** 4. Recursively call checkTreePage on all children.
80194** 5. Verify that the depth of all children is the same.
80195*/
80196static int checkTreePage(
80197 IntegrityCk *pCheck, /* Context for the sanity check */
80198 Pgno iPage, /* Page number of the page to check */
80199 i64 *piMinKey, /* Write minimum integer primary key here */
80200 i64 maxKey /* Error if integer primary key greater than this */
80201){
80202 MemPage *pPage = 0; /* The page being analyzed */
80203 int i; /* Loop counter */
80204 int rc; /* Result code from subroutine call */
80205 int depth = -1, d2; /* Depth of a subtree */
80206 int pgno; /* Page number */
80207 int nFrag; /* Number of fragmented bytes on the page */
80208 int hdr; /* Offset to the page header */
80209 int cellStart; /* Offset to the start of the cell pointer array */
80210 int nCell; /* Number of cells */
80211 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
80212 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
80213 ** False if IPK must be strictly less than maxKey */
80214 u8 *data; /* Page content */
80215 u8 *pCell; /* Cell content */
80216 u8 *pCellIdx; /* Next element of the cell pointer array */
80217 BtShared *pBt; /* The BtShared object that owns pPage */
80218 u32 pc; /* Address of a cell */
80219 u32 usableSize; /* Usable size of the page */
80220 u32 contentOffset; /* Offset to the start of the cell content area */
80221 u32 *heap = 0; /* Min-heap used for checking cell coverage */
80222 u32 x, prev = 0; /* Next and previous entry on the min-heap */
80223 const char *saved_zPfx = pCheck->zPfx;
80224 int saved_v1 = pCheck->v1;
80225 int saved_v2 = pCheck->v2;
80226 u8 savedIsInit = 0;
80227
80228 /* Check that the page exists
80229 */
80230 checkProgress(pCheck);
80231 if( pCheck->mxErr==0 ) goto end_of_check;
80232 pBt = pCheck->pBt;
80233 usableSize = pBt->usableSize;
80234 if( iPage==0 ) return 0;
80235 if( checkRef(pCheck, iPage) ) return 0;
80236 pCheck->zPfx = "Tree %u page %u: ";
80237 pCheck->v1 = iPage;
80238 if( (rc = btreeGetPage(pBt, pgno: iPage, ppPage: &pPage, flags: 0))!=0 ){
80239 checkAppendMsg(pCheck,
80240 zFormat: "unable to get the page. error code=%d", rc);
80241 goto end_of_check;
80242 }
80243
80244 /* Clear MemPage.isInit to make sure the corruption detection code in
80245 ** btreeInitPage() is executed. */
80246 savedIsInit = pPage->isInit;
80247 pPage->isInit = 0;
80248 if( (rc = btreeInitPage(pPage))!=0 ){
80249 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
80250 checkAppendMsg(pCheck,
80251 zFormat: "btreeInitPage() returns error code %d", rc);
80252 goto end_of_check;
80253 }
80254 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
80255 assert( rc==SQLITE_CORRUPT );
80256 checkAppendMsg(pCheck, zFormat: "free space corruption", rc);
80257 goto end_of_check;
80258 }
80259 data = pPage->aData;
80260 hdr = pPage->hdrOffset;
80261
80262 /* Set up for cell analysis */
80263 pCheck->zPfx = "Tree %u page %u cell %u: ";
80264 contentOffset = get2byteNotZero(&data[hdr+5]);
80265 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
80266
80267 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
80268 ** number of cells on the page. */
80269 nCell = get2byte(&data[hdr+3]);
80270 assert( pPage->nCell==nCell );
80271
80272 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
80273 ** immediately follows the b-tree page header. */
80274 cellStart = hdr + 12 - 4*pPage->leaf;
80275 assert( pPage->aCellIdx==&data[cellStart] );
80276 pCellIdx = &data[cellStart + 2*(nCell-1)];
80277
80278 if( !pPage->leaf ){
80279 /* Analyze the right-child page of internal pages */
80280 pgno = get4byte(p: &data[hdr+8]);
80281#ifndef SQLITE_OMIT_AUTOVACUUM
80282 if( pBt->autoVacuum ){
80283 pCheck->zPfx = "Tree %u page %u right child: ";
80284 checkPtrmap(pCheck, iChild: pgno, PTRMAP_BTREE, iParent: iPage);
80285 }
80286#endif
80287 depth = checkTreePage(pCheck, iPage: pgno, piMinKey: &maxKey, maxKey);
80288 keyCanBeEqual = 0;
80289 }else{
80290 /* For leaf pages, the coverage check will occur in the same loop
80291 ** as the other cell checks, so initialize the heap. */
80292 heap = pCheck->heap;
80293 heap[0] = 0;
80294 }
80295
80296 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
80297 ** integer offsets to the cell contents. */
80298 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
80299 CellInfo info;
80300
80301 /* Check cell size */
80302 pCheck->v2 = i;
80303 assert( pCellIdx==&data[cellStart + i*2] );
80304 pc = get2byteAligned(pCellIdx);
80305 pCellIdx -= 2;
80306 if( pc<contentOffset || pc>usableSize-4 ){
80307 checkAppendMsg(pCheck, zFormat: "Offset %u out of range %u..%u",
80308 pc, contentOffset, usableSize-4);
80309 doCoverageCheck = 0;
80310 continue;
80311 }
80312 pCell = &data[pc];
80313 pPage->xParseCell(pPage, pCell, &info);
80314 if( pc+info.nSize>usableSize ){
80315 checkAppendMsg(pCheck, zFormat: "Extends off end of page");
80316 doCoverageCheck = 0;
80317 continue;
80318 }
80319
80320 /* Check for integer primary key out of range */
80321 if( pPage->intKey ){
80322 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
80323 checkAppendMsg(pCheck, zFormat: "Rowid %lld out of order", info.nKey);
80324 }
80325 maxKey = info.nKey;
80326 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
80327 }
80328
80329 /* Check the content overflow list */
80330 if( info.nPayload>info.nLocal ){
80331 u32 nPage; /* Number of pages on the overflow chain */
80332 Pgno pgnoOvfl; /* First page of the overflow chain */
80333 assert( pc + info.nSize - 4 <= usableSize );
80334 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
80335 pgnoOvfl = get4byte(p: &pCell[info.nSize - 4]);
80336#ifndef SQLITE_OMIT_AUTOVACUUM
80337 if( pBt->autoVacuum ){
80338 checkPtrmap(pCheck, iChild: pgnoOvfl, PTRMAP_OVERFLOW1, iParent: iPage);
80339 }
80340#endif
80341 checkList(pCheck, isFreeList: 0, iPage: pgnoOvfl, N: nPage);
80342 }
80343
80344 if( !pPage->leaf ){
80345 /* Check sanity of left child page for internal pages */
80346 pgno = get4byte(p: pCell);
80347#ifndef SQLITE_OMIT_AUTOVACUUM
80348 if( pBt->autoVacuum ){
80349 checkPtrmap(pCheck, iChild: pgno, PTRMAP_BTREE, iParent: iPage);
80350 }
80351#endif
80352 d2 = checkTreePage(pCheck, iPage: pgno, piMinKey: &maxKey, maxKey);
80353 keyCanBeEqual = 0;
80354 if( d2!=depth ){
80355 checkAppendMsg(pCheck, zFormat: "Child page depth differs");
80356 depth = d2;
80357 }
80358 }else{
80359 /* Populate the coverage-checking heap for leaf pages */
80360 btreeHeapInsert(aHeap: heap, x: (pc<<16)|(pc+info.nSize-1));
80361 }
80362 }
80363 *piMinKey = maxKey;
80364
80365 /* Check for complete coverage of the page
80366 */
80367 pCheck->zPfx = 0;
80368 if( doCoverageCheck && pCheck->mxErr>0 ){
80369 /* For leaf pages, the min-heap has already been initialized and the
80370 ** cells have already been inserted. But for internal pages, that has
80371 ** not yet been done, so do it now */
80372 if( !pPage->leaf ){
80373 heap = pCheck->heap;
80374 heap[0] = 0;
80375 for(i=nCell-1; i>=0; i--){
80376 u32 size;
80377 pc = get2byteAligned(&data[cellStart+i*2]);
80378 size = pPage->xCellSize(pPage, &data[pc]);
80379 btreeHeapInsert(aHeap: heap, x: (pc<<16)|(pc+size-1));
80380 }
80381 }
80382 /* Add the freeblocks to the min-heap
80383 **
80384 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
80385 ** is the offset of the first freeblock, or zero if there are no
80386 ** freeblocks on the page.
80387 */
80388 i = get2byte(&data[hdr+1]);
80389 while( i>0 ){
80390 int size, j;
80391 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
80392 size = get2byte(&data[i+2]);
80393 assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
80394 btreeHeapInsert(aHeap: heap, x: (((u32)i)<<16)|(i+size-1));
80395 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
80396 ** big-endian integer which is the offset in the b-tree page of the next
80397 ** freeblock in the chain, or zero if the freeblock is the last on the
80398 ** chain. */
80399 j = get2byte(&data[i]);
80400 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
80401 ** increasing offset. */
80402 assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */
80403 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
80404 i = j;
80405 }
80406 /* Analyze the min-heap looking for overlap between cells and/or
80407 ** freeblocks, and counting the number of untracked bytes in nFrag.
80408 **
80409 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
80410 ** There is an implied first entry the covers the page header, the cell
80411 ** pointer index, and the gap between the cell pointer index and the start
80412 ** of cell content.
80413 **
80414 ** The loop below pulls entries from the min-heap in order and compares
80415 ** the start_address against the previous end_address. If there is an
80416 ** overlap, that means bytes are used multiple times. If there is a gap,
80417 ** that gap is added to the fragmentation count.
80418 */
80419 nFrag = 0;
80420 prev = contentOffset - 1; /* Implied first min-heap entry */
80421 while( btreeHeapPull(aHeap: heap,pOut: &x) ){
80422 if( (prev&0xffff)>=(x>>16) ){
80423 checkAppendMsg(pCheck,
80424 zFormat: "Multiple uses for byte %u of page %u", x>>16, iPage);
80425 break;
80426 }else{
80427 nFrag += (x>>16) - (prev&0xffff) - 1;
80428 prev = x;
80429 }
80430 }
80431 nFrag += usableSize - (prev&0xffff) - 1;
80432 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
80433 ** is stored in the fifth field of the b-tree page header.
80434 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
80435 ** number of fragmented free bytes within the cell content area.
80436 */
80437 if( heap[0]==0 && nFrag!=data[hdr+7] ){
80438 checkAppendMsg(pCheck,
80439 zFormat: "Fragmentation of %u bytes reported as %u on page %u",
80440 nFrag, data[hdr+7], iPage);
80441 }
80442 }
80443
80444end_of_check:
80445 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
80446 releasePage(pPage);
80447 pCheck->zPfx = saved_zPfx;
80448 pCheck->v1 = saved_v1;
80449 pCheck->v2 = saved_v2;
80450 return depth+1;
80451}
80452#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
80453
80454#ifndef SQLITE_OMIT_INTEGRITY_CHECK
80455/*
80456** This routine does a complete check of the given BTree file. aRoot[] is
80457** an array of pages numbers were each page number is the root page of
80458** a table. nRoot is the number of entries in aRoot.
80459**
80460** A read-only or read-write transaction must be opened before calling
80461** this function.
80462**
80463** Write the number of error seen in *pnErr. Except for some memory
80464** allocation errors, an error message held in memory obtained from
80465** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
80466** returned. If a memory allocation error occurs, NULL is returned.
80467**
80468** If the first entry in aRoot[] is 0, that indicates that the list of
80469** root pages is incomplete. This is a "partial integrity-check". This
80470** happens when performing an integrity check on a single table. The
80471** zero is skipped, of course. But in addition, the freelist checks
80472** and the checks to make sure every page is referenced are also skipped,
80473** since obviously it is not possible to know which pages are covered by
80474** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
80475** checks are still performed.
80476*/
80477SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
80478 sqlite3 *db, /* Database connection that is running the check */
80479 Btree *p, /* The btree to be checked */
80480 Pgno *aRoot, /* An array of root pages numbers for individual trees */
80481 int nRoot, /* Number of entries in aRoot[] */
80482 int mxErr, /* Stop reporting errors after this many */
80483 int *pnErr, /* OUT: Write number of errors seen to this variable */
80484 char **pzOut /* OUT: Write the error message string here */
80485){
80486 Pgno i;
80487 IntegrityCk sCheck;
80488 BtShared *pBt = p->pBt;
80489 u64 savedDbFlags = pBt->db->flags;
80490 char zErr[100];
80491 int bPartial = 0; /* True if not checking all btrees */
80492 int bCkFreelist = 1; /* True to scan the freelist */
80493 VVA_ONLY( int nRef );
80494 assert( nRoot>0 );
80495
80496 /* aRoot[0]==0 means this is a partial check */
80497 if( aRoot[0]==0 ){
80498 assert( nRoot>1 );
80499 bPartial = 1;
80500 if( aRoot[1]!=1 ) bCkFreelist = 0;
80501 }
80502
80503 sqlite3BtreeEnter(p);
80504 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
80505 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
80506 assert( nRef>=0 );
80507 memset(s: &sCheck, c: 0, n: sizeof(sCheck));
80508 sCheck.db = db;
80509 sCheck.pBt = pBt;
80510 sCheck.pPager = pBt->pPager;
80511 sCheck.nPage = btreePagecount(pBt: sCheck.pBt);
80512 sCheck.mxErr = mxErr;
80513 sqlite3StrAccumInit(p: &sCheck.errMsg, db: 0, zBase: zErr, n: sizeof(zErr), SQLITE_MAX_LENGTH);
80514 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
80515 if( sCheck.nPage==0 ){
80516 goto integrity_ck_cleanup;
80517 }
80518
80519 sCheck.aPgRef = sqlite3MallocZero(n: (sCheck.nPage / 8)+ 1);
80520 if( !sCheck.aPgRef ){
80521 checkOom(pCheck: &sCheck);
80522 goto integrity_ck_cleanup;
80523 }
80524 sCheck.heap = (u32*)sqlite3PageMalloc( sz: pBt->pageSize );
80525 if( sCheck.heap==0 ){
80526 checkOom(pCheck: &sCheck);
80527 goto integrity_ck_cleanup;
80528 }
80529
80530 i = PENDING_BYTE_PAGE(pBt);
80531 if( i<=sCheck.nPage ) setPageReferenced(pCheck: &sCheck, iPg: i);
80532
80533 /* Check the integrity of the freelist
80534 */
80535 if( bCkFreelist ){
80536 sCheck.zPfx = "Freelist: ";
80537 checkList(pCheck: &sCheck, isFreeList: 1, get4byte(p: &pBt->pPage1->aData[32]),
80538 get4byte(p: &pBt->pPage1->aData[36]));
80539 sCheck.zPfx = 0;
80540 }
80541
80542 /* Check all the tables.
80543 */
80544#ifndef SQLITE_OMIT_AUTOVACUUM
80545 if( !bPartial ){
80546 if( pBt->autoVacuum ){
80547 Pgno mx = 0;
80548 Pgno mxInHdr;
80549 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
80550 mxInHdr = get4byte(p: &pBt->pPage1->aData[52]);
80551 if( mx!=mxInHdr ){
80552 checkAppendMsg(pCheck: &sCheck,
80553 zFormat: "max rootpage (%u) disagrees with header (%u)",
80554 mx, mxInHdr
80555 );
80556 }
80557 }else if( get4byte(p: &pBt->pPage1->aData[64])!=0 ){
80558 checkAppendMsg(pCheck: &sCheck,
80559 zFormat: "incremental_vacuum enabled with a max rootpage of zero"
80560 );
80561 }
80562 }
80563#endif
80564 testcase( pBt->db->flags & SQLITE_CellSizeCk );
80565 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
80566 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
80567 i64 notUsed;
80568 if( aRoot[i]==0 ) continue;
80569#ifndef SQLITE_OMIT_AUTOVACUUM
80570 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
80571 checkPtrmap(pCheck: &sCheck, iChild: aRoot[i], PTRMAP_ROOTPAGE, iParent: 0);
80572 }
80573#endif
80574 sCheck.v0 = aRoot[i];
80575 checkTreePage(pCheck: &sCheck, iPage: aRoot[i], piMinKey: &notUsed, LARGEST_INT64);
80576 }
80577 pBt->db->flags = savedDbFlags;
80578
80579 /* Make sure every page in the file is referenced
80580 */
80581 if( !bPartial ){
80582 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
80583#ifdef SQLITE_OMIT_AUTOVACUUM
80584 if( getPageReferenced(&sCheck, i)==0 ){
80585 checkAppendMsg(&sCheck, "Page %u: never used", i);
80586 }
80587#else
80588 /* If the database supports auto-vacuum, make sure no tables contain
80589 ** references to pointer-map pages.
80590 */
80591 if( getPageReferenced(pCheck: &sCheck, iPg: i)==0 &&
80592 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
80593 checkAppendMsg(pCheck: &sCheck, zFormat: "Page %u: never used", i);
80594 }
80595 if( getPageReferenced(pCheck: &sCheck, iPg: i)!=0 &&
80596 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
80597 checkAppendMsg(pCheck: &sCheck, zFormat: "Page %u: pointer map referenced", i);
80598 }
80599#endif
80600 }
80601 }
80602
80603 /* Clean up and report errors.
80604 */
80605integrity_ck_cleanup:
80606 sqlite3PageFree(p: sCheck.heap);
80607 sqlite3_free(p: sCheck.aPgRef);
80608 *pnErr = sCheck.nErr;
80609 if( sCheck.nErr==0 ){
80610 sqlite3_str_reset(p: &sCheck.errMsg);
80611 *pzOut = 0;
80612 }else{
80613 *pzOut = sqlite3StrAccumFinish(p: &sCheck.errMsg);
80614 }
80615 /* Make sure this analysis did not leave any unref() pages. */
80616 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
80617 sqlite3BtreeLeave(p);
80618 return sCheck.rc;
80619}
80620#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
80621
80622/*
80623** Return the full pathname of the underlying database file. Return
80624** an empty string if the database is in-memory or a TEMP database.
80625**
80626** The pager filename is invariant as long as the pager is
80627** open so it is safe to access without the BtShared mutex.
80628*/
80629SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
80630 assert( p->pBt->pPager!=0 );
80631 return sqlite3PagerFilename(pPager: p->pBt->pPager, nullIfMemDb: 1);
80632}
80633
80634/*
80635** Return the pathname of the journal file for this database. The return
80636** value of this routine is the same regardless of whether the journal file
80637** has been created or not.
80638**
80639** The pager journal filename is invariant as long as the pager is
80640** open so it is safe to access without the BtShared mutex.
80641*/
80642SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
80643 assert( p->pBt->pPager!=0 );
80644 return sqlite3PagerJournalname(pPager: p->pBt->pPager);
80645}
80646
80647/*
80648** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
80649** to describe the current transaction state of Btree p.
80650*/
80651SQLITE_PRIVATE int sqlite3BtreeTxnState(Btree *p){
80652 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
80653 return p ? p->inTrans : 0;
80654}
80655
80656#ifndef SQLITE_OMIT_WAL
80657/*
80658** Run a checkpoint on the Btree passed as the first argument.
80659**
80660** Return SQLITE_LOCKED if this or any other connection has an open
80661** transaction on the shared-cache the argument Btree is connected to.
80662**
80663** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
80664*/
80665SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
80666 int rc = SQLITE_OK;
80667 if( p ){
80668 BtShared *pBt = p->pBt;
80669 sqlite3BtreeEnter(p);
80670 if( pBt->inTransaction!=TRANS_NONE ){
80671 rc = SQLITE_LOCKED;
80672 }else{
80673 rc = sqlite3PagerCheckpoint(pPager: pBt->pPager, db: p->db, eMode, pnLog, pnCkpt);
80674 }
80675 sqlite3BtreeLeave(p);
80676 }
80677 return rc;
80678}
80679#endif
80680
80681/*
80682** Return true if there is currently a backup running on Btree p.
80683*/
80684SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
80685 assert( p );
80686 assert( sqlite3_mutex_held(p->db->mutex) );
80687 return p->nBackup!=0;
80688}
80689
80690/*
80691** This function returns a pointer to a blob of memory associated with
80692** a single shared-btree. The memory is used by client code for its own
80693** purposes (for example, to store a high-level schema associated with
80694** the shared-btree). The btree layer manages reference counting issues.
80695**
80696** The first time this is called on a shared-btree, nBytes bytes of memory
80697** are allocated, zeroed, and returned to the caller. For each subsequent
80698** call the nBytes parameter is ignored and a pointer to the same blob
80699** of memory returned.
80700**
80701** If the nBytes parameter is 0 and the blob of memory has not yet been
80702** allocated, a null pointer is returned. If the blob has already been
80703** allocated, it is returned as normal.
80704**
80705** Just before the shared-btree is closed, the function passed as the
80706** xFree argument when the memory allocation was made is invoked on the
80707** blob of allocated memory. The xFree function should not call sqlite3_free()
80708** on the memory, the btree layer does that.
80709*/
80710SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
80711 BtShared *pBt = p->pBt;
80712 sqlite3BtreeEnter(p);
80713 if( !pBt->pSchema && nBytes ){
80714 pBt->pSchema = sqlite3DbMallocZero(db: 0, n: nBytes);
80715 pBt->xFreeSchema = xFree;
80716 }
80717 sqlite3BtreeLeave(p);
80718 return pBt->pSchema;
80719}
80720
80721/*
80722** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
80723** btree as the argument handle holds an exclusive lock on the
80724** sqlite_schema table. Otherwise SQLITE_OK.
80725*/
80726SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
80727 int rc;
80728 assert( sqlite3_mutex_held(p->db->mutex) );
80729 sqlite3BtreeEnter(p);
80730 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
80731 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
80732 sqlite3BtreeLeave(p);
80733 return rc;
80734}
80735
80736
80737#ifndef SQLITE_OMIT_SHARED_CACHE
80738/*
80739** Obtain a lock on the table whose root page is iTab. The
80740** lock is a write lock if isWritelock is true or a read lock
80741** if it is false.
80742*/
80743SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
80744 int rc = SQLITE_OK;
80745 assert( p->inTrans!=TRANS_NONE );
80746 if( p->sharable ){
80747 u8 lockType = READ_LOCK + isWriteLock;
80748 assert( READ_LOCK+1==WRITE_LOCK );
80749 assert( isWriteLock==0 || isWriteLock==1 );
80750
80751 sqlite3BtreeEnter(p);
80752 rc = querySharedCacheTableLock(p, iTab, eLock: lockType);
80753 if( rc==SQLITE_OK ){
80754 rc = setSharedCacheTableLock(p, iTable: iTab, eLock: lockType);
80755 }
80756 sqlite3BtreeLeave(p);
80757 }
80758 return rc;
80759}
80760#endif
80761
80762#ifndef SQLITE_OMIT_INCRBLOB
80763/*
80764** Argument pCsr must be a cursor opened for writing on an
80765** INTKEY table currently pointing at a valid table entry.
80766** This function modifies the data stored as part of that entry.
80767**
80768** Only the data content may only be modified, it is not possible to
80769** change the length of the data stored. If this function is called with
80770** parameters that attempt to write past the end of the existing data,
80771** no modifications are made and SQLITE_CORRUPT is returned.
80772*/
80773SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
80774 int rc;
80775 assert( cursorOwnsBtShared(pCsr) );
80776 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
80777 assert( pCsr->curFlags & BTCF_Incrblob );
80778
80779 rc = restoreCursorPosition(pCsr);
80780 if( rc!=SQLITE_OK ){
80781 return rc;
80782 }
80783 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
80784 if( pCsr->eState!=CURSOR_VALID ){
80785 return SQLITE_ABORT;
80786 }
80787
80788 /* Save the positions of all other cursors open on this table. This is
80789 ** required in case any of them are holding references to an xFetch
80790 ** version of the b-tree page modified by the accessPayload call below.
80791 **
80792 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
80793 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
80794 ** saveAllCursors can only return SQLITE_OK.
80795 */
80796 VVA_ONLY(rc =) saveAllCursors(pBt: pCsr->pBt, iRoot: pCsr->pgnoRoot, pExcept: pCsr);
80797 assert( rc==SQLITE_OK );
80798
80799 /* Check some assumptions:
80800 ** (a) the cursor is open for writing,
80801 ** (b) there is a read/write transaction open,
80802 ** (c) the connection holds a write-lock on the table (if required),
80803 ** (d) there are no conflicting read-locks, and
80804 ** (e) the cursor points at a valid row of an intKey table.
80805 */
80806 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
80807 return SQLITE_READONLY;
80808 }
80809 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
80810 && pCsr->pBt->inTransaction==TRANS_WRITE );
80811 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
80812 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
80813 assert( pCsr->pPage->intKey );
80814
80815 return accessPayload(pCur: pCsr, offset, amt, pBuf: (unsigned char *)z, eOp: 1);
80816}
80817
80818/*
80819** Mark this cursor as an incremental blob cursor.
80820*/
80821SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
80822 pCur->curFlags |= BTCF_Incrblob;
80823 pCur->pBtree->hasIncrblobCur = 1;
80824}
80825#endif
80826
80827/*
80828** Set both the "read version" (single byte at byte offset 18) and
80829** "write version" (single byte at byte offset 19) fields in the database
80830** header to iVersion.
80831*/
80832SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
80833 BtShared *pBt = pBtree->pBt;
80834 int rc; /* Return code */
80835
80836 assert( iVersion==1 || iVersion==2 );
80837
80838 /* If setting the version fields to 1, do not automatically open the
80839 ** WAL connection, even if the version fields are currently set to 2.
80840 */
80841 pBt->btsFlags &= ~BTS_NO_WAL;
80842 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
80843
80844 rc = sqlite3BtreeBeginTrans(p: pBtree, wrflag: 0, pSchemaVersion: 0);
80845 if( rc==SQLITE_OK ){
80846 u8 *aData = pBt->pPage1->aData;
80847 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
80848 rc = sqlite3BtreeBeginTrans(p: pBtree, wrflag: 2, pSchemaVersion: 0);
80849 if( rc==SQLITE_OK ){
80850 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
80851 if( rc==SQLITE_OK ){
80852 aData[18] = (u8)iVersion;
80853 aData[19] = (u8)iVersion;
80854 }
80855 }
80856 }
80857 }
80858
80859 pBt->btsFlags &= ~BTS_NO_WAL;
80860 return rc;
80861}
80862
80863/*
80864** Return true if the cursor has a hint specified. This routine is
80865** only used from within assert() statements
80866*/
80867SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
80868 return (pCsr->hints & mask)!=0;
80869}
80870
80871/*
80872** Return true if the given Btree is read-only.
80873*/
80874SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
80875 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
80876}
80877
80878/*
80879** Return the size of the header added to each page by this module.
80880*/
80881SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
80882
80883/*
80884** If no transaction is active and the database is not a temp-db, clear
80885** the in-memory pager cache.
80886*/
80887SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree *p){
80888 BtShared *pBt = p->pBt;
80889 if( pBt->inTransaction==TRANS_NONE ){
80890 sqlite3PagerClearCache(pPager: pBt->pPager);
80891 }
80892}
80893
80894#if !defined(SQLITE_OMIT_SHARED_CACHE)
80895/*
80896** Return true if the Btree passed as the only argument is sharable.
80897*/
80898SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
80899 return p->sharable;
80900}
80901
80902/*
80903** Return the number of connections to the BtShared object accessed by
80904** the Btree handle passed as the only argument. For private caches
80905** this is always 1. For shared caches it may be 1 or greater.
80906*/
80907SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
80908 testcase( p->sharable );
80909 return p->pBt->nRef;
80910}
80911#endif
80912
80913/************** End of btree.c ***********************************************/
80914/************** Begin file backup.c ******************************************/
80915/*
80916** 2009 January 28
80917**
80918** The author disclaims copyright to this source code. In place of
80919** a legal notice, here is a blessing:
80920**
80921** May you do good and not evil.
80922** May you find forgiveness for yourself and forgive others.
80923** May you share freely, never taking more than you give.
80924**
80925*************************************************************************
80926** This file contains the implementation of the sqlite3_backup_XXX()
80927** API functions and the related features.
80928*/
80929/* #include "sqliteInt.h" */
80930/* #include "btreeInt.h" */
80931
80932/*
80933** Structure allocated for each backup operation.
80934*/
80935struct sqlite3_backup {
80936 sqlite3* pDestDb; /* Destination database handle */
80937 Btree *pDest; /* Destination b-tree file */
80938 u32 iDestSchema; /* Original schema cookie in destination */
80939 int bDestLocked; /* True once a write-transaction is open on pDest */
80940
80941 Pgno iNext; /* Page number of the next source page to copy */
80942 sqlite3* pSrcDb; /* Source database handle */
80943 Btree *pSrc; /* Source b-tree file */
80944
80945 int rc; /* Backup process error code */
80946
80947 /* These two variables are set by every call to backup_step(). They are
80948 ** read by calls to backup_remaining() and backup_pagecount().
80949 */
80950 Pgno nRemaining; /* Number of pages left to copy */
80951 Pgno nPagecount; /* Total number of pages to copy */
80952
80953 int isAttached; /* True once backup has been registered with pager */
80954 sqlite3_backup *pNext; /* Next backup associated with source pager */
80955};
80956
80957/*
80958** THREAD SAFETY NOTES:
80959**
80960** Once it has been created using backup_init(), a single sqlite3_backup
80961** structure may be accessed via two groups of thread-safe entry points:
80962**
80963** * Via the sqlite3_backup_XXX() API function backup_step() and
80964** backup_finish(). Both these functions obtain the source database
80965** handle mutex and the mutex associated with the source BtShared
80966** structure, in that order.
80967**
80968** * Via the BackupUpdate() and BackupRestart() functions, which are
80969** invoked by the pager layer to report various state changes in
80970** the page cache associated with the source database. The mutex
80971** associated with the source database BtShared structure will always
80972** be held when either of these functions are invoked.
80973**
80974** The other sqlite3_backup_XXX() API functions, backup_remaining() and
80975** backup_pagecount() are not thread-safe functions. If they are called
80976** while some other thread is calling backup_step() or backup_finish(),
80977** the values returned may be invalid. There is no way for a call to
80978** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
80979** or backup_pagecount().
80980**
80981** Depending on the SQLite configuration, the database handles and/or
80982** the Btree objects may have their own mutexes that require locking.
80983** Non-sharable Btrees (in-memory databases for example), do not have
80984** associated mutexes.
80985*/
80986
80987/*
80988** Return a pointer corresponding to database zDb (i.e. "main", "temp")
80989** in connection handle pDb. If such a database cannot be found, return
80990** a NULL pointer and write an error message to pErrorDb.
80991**
80992** If the "temp" database is requested, it may need to be opened by this
80993** function. If an error occurs while doing so, return 0 and write an
80994** error message to pErrorDb.
80995*/
80996static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
80997 int i = sqlite3FindDbName(pDb, zDb);
80998
80999 if( i==1 ){
81000 Parse sParse;
81001 int rc = 0;
81002 sqlite3ParseObjectInit(&sParse,pDb);
81003 if( sqlite3OpenTempDatabase(&sParse) ){
81004 sqlite3ErrorWithMsg(db: pErrorDb, err_code: sParse.rc, zFormat: "%s", sParse.zErrMsg);
81005 rc = SQLITE_ERROR;
81006 }
81007 sqlite3DbFree(db: pErrorDb, p: sParse.zErrMsg);
81008 sqlite3ParseObjectReset(&sParse);
81009 if( rc ){
81010 return 0;
81011 }
81012 }
81013
81014 if( i<0 ){
81015 sqlite3ErrorWithMsg(db: pErrorDb, SQLITE_ERROR, zFormat: "unknown database %s", zDb);
81016 return 0;
81017 }
81018
81019 return pDb->aDb[i].pBt;
81020}
81021
81022/*
81023** Attempt to set the page size of the destination to match the page size
81024** of the source.
81025*/
81026static int setDestPgsz(sqlite3_backup *p){
81027 int rc;
81028 rc = sqlite3BtreeSetPageSize(p: p->pDest,pageSize: sqlite3BtreeGetPageSize(p: p->pSrc),nReserve: 0,iFix: 0);
81029 return rc;
81030}
81031
81032/*
81033** Check that there is no open read-transaction on the b-tree passed as the
81034** second argument. If there is not, return SQLITE_OK. Otherwise, if there
81035** is an open read-transaction, return SQLITE_ERROR and leave an error
81036** message in database handle db.
81037*/
81038static int checkReadTransaction(sqlite3 *db, Btree *p){
81039 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE ){
81040 sqlite3ErrorWithMsg(db, SQLITE_ERROR, zFormat: "destination database is in use");
81041 return SQLITE_ERROR;
81042 }
81043 return SQLITE_OK;
81044}
81045
81046/*
81047** Create an sqlite3_backup process to copy the contents of zSrcDb from
81048** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
81049** a pointer to the new sqlite3_backup object.
81050**
81051** If an error occurs, NULL is returned and an error code and error message
81052** stored in database handle pDestDb.
81053*/
81054SQLITE_API sqlite3_backup *sqlite3_backup_init(
81055 sqlite3* pDestDb, /* Database to write to */
81056 const char *zDestDb, /* Name of database within pDestDb */
81057 sqlite3* pSrcDb, /* Database connection to read from */
81058 const char *zSrcDb /* Name of database within pSrcDb */
81059){
81060 sqlite3_backup *p; /* Value to return */
81061
81062#ifdef SQLITE_ENABLE_API_ARMOR
81063 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
81064 (void)SQLITE_MISUSE_BKPT;
81065 return 0;
81066 }
81067#endif
81068
81069 /* Lock the source database handle. The destination database
81070 ** handle is not locked in this routine, but it is locked in
81071 ** sqlite3_backup_step(). The user is required to ensure that no
81072 ** other thread accesses the destination handle for the duration
81073 ** of the backup operation. Any attempt to use the destination
81074 ** database connection while a backup is in progress may cause
81075 ** a malfunction or a deadlock.
81076 */
81077 sqlite3_mutex_enter(p: pSrcDb->mutex);
81078 sqlite3_mutex_enter(p: pDestDb->mutex);
81079
81080 if( pSrcDb==pDestDb ){
81081 sqlite3ErrorWithMsg(
81082 db: pDestDb, SQLITE_ERROR, zFormat: "source and destination must be distinct"
81083 );
81084 p = 0;
81085 }else {
81086 /* Allocate space for a new sqlite3_backup object...
81087 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
81088 ** call to sqlite3_backup_init() and is destroyed by a call to
81089 ** sqlite3_backup_finish(). */
81090 p = (sqlite3_backup *)sqlite3MallocZero(n: sizeof(sqlite3_backup));
81091 if( !p ){
81092 sqlite3Error(db: pDestDb, SQLITE_NOMEM_BKPT);
81093 }
81094 }
81095
81096 /* If the allocation succeeded, populate the new object. */
81097 if( p ){
81098 p->pSrc = findBtree(pErrorDb: pDestDb, pDb: pSrcDb, zDb: zSrcDb);
81099 p->pDest = findBtree(pErrorDb: pDestDb, pDb: pDestDb, zDb: zDestDb);
81100 p->pDestDb = pDestDb;
81101 p->pSrcDb = pSrcDb;
81102 p->iNext = 1;
81103 p->isAttached = 0;
81104
81105 if( 0==p->pSrc || 0==p->pDest
81106 || checkReadTransaction(db: pDestDb, p: p->pDest)!=SQLITE_OK
81107 ){
81108 /* One (or both) of the named databases did not exist or an OOM
81109 ** error was hit. Or there is a transaction open on the destination
81110 ** database. The error has already been written into the pDestDb
81111 ** handle. All that is left to do here is free the sqlite3_backup
81112 ** structure. */
81113 sqlite3_free(p);
81114 p = 0;
81115 }
81116 }
81117 if( p ){
81118 p->pSrc->nBackup++;
81119 }
81120
81121 sqlite3_mutex_leave(p: pDestDb->mutex);
81122 sqlite3_mutex_leave(p: pSrcDb->mutex);
81123 return p;
81124}
81125
81126/*
81127** Argument rc is an SQLite error code. Return true if this error is
81128** considered fatal if encountered during a backup operation. All errors
81129** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
81130*/
81131static int isFatalError(int rc){
81132 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
81133}
81134
81135/*
81136** Parameter zSrcData points to a buffer containing the data for
81137** page iSrcPg from the source database. Copy this data into the
81138** destination database.
81139*/
81140static int backupOnePage(
81141 sqlite3_backup *p, /* Backup handle */
81142 Pgno iSrcPg, /* Source database page to backup */
81143 const u8 *zSrcData, /* Source database page data */
81144 int bUpdate /* True for an update, false otherwise */
81145){
81146 Pager * const pDestPager = sqlite3BtreePager(p: p->pDest);
81147 const int nSrcPgsz = sqlite3BtreeGetPageSize(p: p->pSrc);
81148 int nDestPgsz = sqlite3BtreeGetPageSize(p: p->pDest);
81149 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
81150 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
81151 int rc = SQLITE_OK;
81152 i64 iOff;
81153
81154 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
81155 assert( p->bDestLocked );
81156 assert( !isFatalError(p->rc) );
81157 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
81158 assert( zSrcData );
81159 assert( nSrcPgsz==nDestPgsz || sqlite3PagerIsMemdb(pDestPager)==0 );
81160
81161 /* This loop runs once for each destination page spanned by the source
81162 ** page. For each iteration, variable iOff is set to the byte offset
81163 ** of the destination page.
81164 */
81165 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
81166 DbPage *pDestPg = 0;
81167 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
81168 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
81169 if( SQLITE_OK==(rc = sqlite3PagerGet(pPager: pDestPager, pgno: iDest, ppPage: &pDestPg, flags: 0))
81170 && SQLITE_OK==(rc = sqlite3PagerWrite(pPg: pDestPg))
81171 ){
81172 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
81173 u8 *zDestData = sqlite3PagerGetData(pPg: pDestPg);
81174 u8 *zOut = &zDestData[iOff%nDestPgsz];
81175
81176 /* Copy the data from the source page into the destination page.
81177 ** Then clear the Btree layer MemPage.isInit flag. Both this module
81178 ** and the pager code use this trick (clearing the first byte
81179 ** of the page 'extra' space to invalidate the Btree layers
81180 ** cached parse of the page). MemPage.isInit is marked
81181 ** "MUST BE FIRST" for this purpose.
81182 */
81183 memcpy(dest: zOut, src: zIn, n: nCopy);
81184 ((u8 *)sqlite3PagerGetExtra(pPg: pDestPg))[0] = 0;
81185 if( iOff==0 && bUpdate==0 ){
81186 sqlite3Put4byte(p: &zOut[28], v: sqlite3BtreeLastPage(p: p->pSrc));
81187 }
81188 }
81189 sqlite3PagerUnref(pPg: pDestPg);
81190 }
81191
81192 return rc;
81193}
81194
81195/*
81196** If pFile is currently larger than iSize bytes, then truncate it to
81197** exactly iSize bytes. If pFile is not larger than iSize bytes, then
81198** this function is a no-op.
81199**
81200** Return SQLITE_OK if everything is successful, or an SQLite error
81201** code if an error occurs.
81202*/
81203static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
81204 i64 iCurrent;
81205 int rc = sqlite3OsFileSize(id: pFile, pSize: &iCurrent);
81206 if( rc==SQLITE_OK && iCurrent>iSize ){
81207 rc = sqlite3OsTruncate(id: pFile, size: iSize);
81208 }
81209 return rc;
81210}
81211
81212/*
81213** Register this backup object with the associated source pager for
81214** callbacks when pages are changed or the cache invalidated.
81215*/
81216static void attachBackupObject(sqlite3_backup *p){
81217 sqlite3_backup **pp;
81218 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
81219 pp = sqlite3PagerBackupPtr(pPager: sqlite3BtreePager(p: p->pSrc));
81220 p->pNext = *pp;
81221 *pp = p;
81222 p->isAttached = 1;
81223}
81224
81225/*
81226** Copy nPage pages from the source b-tree to the destination.
81227*/
81228SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
81229 int rc;
81230 int destMode; /* Destination journal mode */
81231 int pgszSrc = 0; /* Source page size */
81232 int pgszDest = 0; /* Destination page size */
81233
81234#ifdef SQLITE_ENABLE_API_ARMOR
81235 if( p==0 ) return SQLITE_MISUSE_BKPT;
81236#endif
81237 sqlite3_mutex_enter(p: p->pSrcDb->mutex);
81238 sqlite3BtreeEnter(p: p->pSrc);
81239 if( p->pDestDb ){
81240 sqlite3_mutex_enter(p: p->pDestDb->mutex);
81241 }
81242
81243 rc = p->rc;
81244 if( !isFatalError(rc) ){
81245 Pager * const pSrcPager = sqlite3BtreePager(p: p->pSrc); /* Source pager */
81246 Pager * const pDestPager = sqlite3BtreePager(p: p->pDest); /* Dest pager */
81247 int ii; /* Iterator variable */
81248 int nSrcPage = -1; /* Size of source db in pages */
81249 int bCloseTrans = 0; /* True if src db requires unlocking */
81250
81251 /* If the source pager is currently in a write-transaction, return
81252 ** SQLITE_BUSY immediately.
81253 */
81254 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
81255 rc = SQLITE_BUSY;
81256 }else{
81257 rc = SQLITE_OK;
81258 }
81259
81260 /* If there is no open read-transaction on the source database, open
81261 ** one now. If a transaction is opened here, then it will be closed
81262 ** before this function exits.
81263 */
81264 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p: p->pSrc) ){
81265 rc = sqlite3BtreeBeginTrans(p: p->pSrc, wrflag: 0, pSchemaVersion: 0);
81266 bCloseTrans = 1;
81267 }
81268
81269 /* If the destination database has not yet been locked (i.e. if this
81270 ** is the first call to backup_step() for the current backup operation),
81271 ** try to set its page size to the same as the source database. This
81272 ** is especially important on ZipVFS systems, as in that case it is
81273 ** not possible to create a database file that uses one page size by
81274 ** writing to it with another. */
81275 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
81276 rc = SQLITE_NOMEM;
81277 }
81278
81279 /* Lock the destination database, if it is not locked already. */
81280 if( SQLITE_OK==rc && p->bDestLocked==0
81281 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p: p->pDest, wrflag: 2,
81282 pSchemaVersion: (int*)&p->iDestSchema))
81283 ){
81284 p->bDestLocked = 1;
81285 }
81286
81287 /* Do not allow backup if the destination database is in WAL mode
81288 ** and the page sizes are different between source and destination */
81289 pgszSrc = sqlite3BtreeGetPageSize(p: p->pSrc);
81290 pgszDest = sqlite3BtreeGetPageSize(p: p->pDest);
81291 destMode = sqlite3PagerGetJournalMode(pPager: sqlite3BtreePager(p: p->pDest));
81292 if( SQLITE_OK==rc
81293 && (destMode==PAGER_JOURNALMODE_WAL || sqlite3PagerIsMemdb(pPager: pDestPager))
81294 && pgszSrc!=pgszDest
81295 ){
81296 rc = SQLITE_READONLY;
81297 }
81298
81299 /* Now that there is a read-lock on the source database, query the
81300 ** source pager for the number of pages in the database.
81301 */
81302 nSrcPage = (int)sqlite3BtreeLastPage(p: p->pSrc);
81303 assert( nSrcPage>=0 );
81304 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
81305 const Pgno iSrcPg = p->iNext; /* Source page number */
81306 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
81307 DbPage *pSrcPg; /* Source page object */
81308 rc = sqlite3PagerGet(pPager: pSrcPager, pgno: iSrcPg, ppPage: &pSrcPg,PAGER_GET_READONLY);
81309 if( rc==SQLITE_OK ){
81310 rc = backupOnePage(p, iSrcPg, zSrcData: sqlite3PagerGetData(pPg: pSrcPg), bUpdate: 0);
81311 sqlite3PagerUnref(pPg: pSrcPg);
81312 }
81313 }
81314 p->iNext++;
81315 }
81316 if( rc==SQLITE_OK ){
81317 p->nPagecount = nSrcPage;
81318 p->nRemaining = nSrcPage+1-p->iNext;
81319 if( p->iNext>(Pgno)nSrcPage ){
81320 rc = SQLITE_DONE;
81321 }else if( !p->isAttached ){
81322 attachBackupObject(p);
81323 }
81324 }
81325
81326 /* Update the schema version field in the destination database. This
81327 ** is to make sure that the schema-version really does change in
81328 ** the case where the source and destination databases have the
81329 ** same schema version.
81330 */
81331 if( rc==SQLITE_DONE ){
81332 if( nSrcPage==0 ){
81333 rc = sqlite3BtreeNewDb(p: p->pDest);
81334 nSrcPage = 1;
81335 }
81336 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
81337 rc = sqlite3BtreeUpdateMeta(p: p->pDest,idx: 1,iMeta: p->iDestSchema+1);
81338 }
81339 if( rc==SQLITE_OK ){
81340 if( p->pDestDb ){
81341 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
81342 }
81343 if( destMode==PAGER_JOURNALMODE_WAL ){
81344 rc = sqlite3BtreeSetVersion(pBtree: p->pDest, iVersion: 2);
81345 }
81346 }
81347 if( rc==SQLITE_OK ){
81348 int nDestTruncate;
81349 /* Set nDestTruncate to the final number of pages in the destination
81350 ** database. The complication here is that the destination page
81351 ** size may be different to the source page size.
81352 **
81353 ** If the source page size is smaller than the destination page size,
81354 ** round up. In this case the call to sqlite3OsTruncate() below will
81355 ** fix the size of the file. However it is important to call
81356 ** sqlite3PagerTruncateImage() here so that any pages in the
81357 ** destination file that lie beyond the nDestTruncate page mark are
81358 ** journalled by PagerCommitPhaseOne() before they are destroyed
81359 ** by the file truncation.
81360 */
81361 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
81362 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
81363 if( pgszSrc<pgszDest ){
81364 int ratio = pgszDest/pgszSrc;
81365 nDestTruncate = (nSrcPage+ratio-1)/ratio;
81366 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
81367 nDestTruncate--;
81368 }
81369 }else{
81370 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
81371 }
81372 assert( nDestTruncate>0 );
81373
81374 if( pgszSrc<pgszDest ){
81375 /* If the source page-size is smaller than the destination page-size,
81376 ** two extra things may need to happen:
81377 **
81378 ** * The destination may need to be truncated, and
81379 **
81380 ** * Data stored on the pages immediately following the
81381 ** pending-byte page in the source database may need to be
81382 ** copied into the destination database.
81383 */
81384 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
81385 sqlite3_file * const pFile = sqlite3PagerFile(pPager: pDestPager);
81386 Pgno iPg;
81387 int nDstPage;
81388 i64 iOff;
81389 i64 iEnd;
81390
81391 assert( pFile );
81392 assert( nDestTruncate==0
81393 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
81394 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
81395 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
81396 ));
81397
81398 /* This block ensures that all data required to recreate the original
81399 ** database has been stored in the journal for pDestPager and the
81400 ** journal synced to disk. So at this point we may safely modify
81401 ** the database file in any way, knowing that if a power failure
81402 ** occurs, the original database will be reconstructed from the
81403 ** journal file. */
81404 sqlite3PagerPagecount(pPager: pDestPager, pnPage: &nDstPage);
81405 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
81406 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
81407 DbPage *pPg;
81408 rc = sqlite3PagerGet(pPager: pDestPager, pgno: iPg, ppPage: &pPg, flags: 0);
81409 if( rc==SQLITE_OK ){
81410 rc = sqlite3PagerWrite(pPg);
81411 sqlite3PagerUnref(pPg);
81412 }
81413 }
81414 }
81415 if( rc==SQLITE_OK ){
81416 rc = sqlite3PagerCommitPhaseOne(pPager: pDestPager, zSuper: 0, noSync: 1);
81417 }
81418
81419 /* Write the extra pages and truncate the database file as required */
81420 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
81421 for(
81422 iOff=PENDING_BYTE+pgszSrc;
81423 rc==SQLITE_OK && iOff<iEnd;
81424 iOff+=pgszSrc
81425 ){
81426 PgHdr *pSrcPg = 0;
81427 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
81428 rc = sqlite3PagerGet(pPager: pSrcPager, pgno: iSrcPg, ppPage: &pSrcPg, flags: 0);
81429 if( rc==SQLITE_OK ){
81430 u8 *zData = sqlite3PagerGetData(pPg: pSrcPg);
81431 rc = sqlite3OsWrite(id: pFile, pBuf: zData, amt: pgszSrc, offset: iOff);
81432 }
81433 sqlite3PagerUnref(pPg: pSrcPg);
81434 }
81435 if( rc==SQLITE_OK ){
81436 rc = backupTruncateFile(pFile, iSize);
81437 }
81438
81439 /* Sync the database file to disk. */
81440 if( rc==SQLITE_OK ){
81441 rc = sqlite3PagerSync(pPager: pDestPager, zSuper: 0);
81442 }
81443 }else{
81444 sqlite3PagerTruncateImage(pPager: pDestPager, nPage: nDestTruncate);
81445 rc = sqlite3PagerCommitPhaseOne(pPager: pDestPager, zSuper: 0, noSync: 0);
81446 }
81447
81448 /* Finish committing the transaction to the destination database. */
81449 if( SQLITE_OK==rc
81450 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p: p->pDest, bCleanup: 0))
81451 ){
81452 rc = SQLITE_DONE;
81453 }
81454 }
81455 }
81456
81457 /* If bCloseTrans is true, then this function opened a read transaction
81458 ** on the source database. Close the read transaction here. There is
81459 ** no need to check the return values of the btree methods here, as
81460 ** "committing" a read-only transaction cannot fail.
81461 */
81462 if( bCloseTrans ){
81463 TESTONLY( int rc2 );
81464 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p: p->pSrc, zSuperJrnl: 0);
81465 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p: p->pSrc, bCleanup: 0);
81466 assert( rc2==SQLITE_OK );
81467 }
81468
81469 if( rc==SQLITE_IOERR_NOMEM ){
81470 rc = SQLITE_NOMEM_BKPT;
81471 }
81472 p->rc = rc;
81473 }
81474 if( p->pDestDb ){
81475 sqlite3_mutex_leave(p: p->pDestDb->mutex);
81476 }
81477 sqlite3BtreeLeave(p: p->pSrc);
81478 sqlite3_mutex_leave(p: p->pSrcDb->mutex);
81479 return rc;
81480}
81481
81482/*
81483** Release all resources associated with an sqlite3_backup* handle.
81484*/
81485SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
81486 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
81487 sqlite3 *pSrcDb; /* Source database connection */
81488 int rc; /* Value to return */
81489
81490 /* Enter the mutexes */
81491 if( p==0 ) return SQLITE_OK;
81492 pSrcDb = p->pSrcDb;
81493 sqlite3_mutex_enter(p: pSrcDb->mutex);
81494 sqlite3BtreeEnter(p: p->pSrc);
81495 if( p->pDestDb ){
81496 sqlite3_mutex_enter(p: p->pDestDb->mutex);
81497 }
81498
81499 /* Detach this backup from the source pager. */
81500 if( p->pDestDb ){
81501 p->pSrc->nBackup--;
81502 }
81503 if( p->isAttached ){
81504 pp = sqlite3PagerBackupPtr(pPager: sqlite3BtreePager(p: p->pSrc));
81505 assert( pp!=0 );
81506 while( *pp!=p ){
81507 pp = &(*pp)->pNext;
81508 assert( pp!=0 );
81509 }
81510 *pp = p->pNext;
81511 }
81512
81513 /* If a transaction is still open on the Btree, roll it back. */
81514 sqlite3BtreeRollback(p: p->pDest, SQLITE_OK, writeOnly: 0);
81515
81516 /* Set the error code of the destination database handle. */
81517 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
81518 if( p->pDestDb ){
81519 sqlite3Error(db: p->pDestDb, err_code: rc);
81520
81521 /* Exit the mutexes and free the backup context structure. */
81522 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
81523 }
81524 sqlite3BtreeLeave(p: p->pSrc);
81525 if( p->pDestDb ){
81526 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
81527 ** call to sqlite3_backup_init() and is destroyed by a call to
81528 ** sqlite3_backup_finish(). */
81529 sqlite3_free(p);
81530 }
81531 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
81532 return rc;
81533}
81534
81535/*
81536** Return the number of pages still to be backed up as of the most recent
81537** call to sqlite3_backup_step().
81538*/
81539SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
81540#ifdef SQLITE_ENABLE_API_ARMOR
81541 if( p==0 ){
81542 (void)SQLITE_MISUSE_BKPT;
81543 return 0;
81544 }
81545#endif
81546 return p->nRemaining;
81547}
81548
81549/*
81550** Return the total number of pages in the source database as of the most
81551** recent call to sqlite3_backup_step().
81552*/
81553SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
81554#ifdef SQLITE_ENABLE_API_ARMOR
81555 if( p==0 ){
81556 (void)SQLITE_MISUSE_BKPT;
81557 return 0;
81558 }
81559#endif
81560 return p->nPagecount;
81561}
81562
81563/*
81564** This function is called after the contents of page iPage of the
81565** source database have been modified. If page iPage has already been
81566** copied into the destination database, then the data written to the
81567** destination is now invalidated. The destination copy of iPage needs
81568** to be updated with the new data before the backup operation is
81569** complete.
81570**
81571** It is assumed that the mutex associated with the BtShared object
81572** corresponding to the source database is held when this function is
81573** called.
81574*/
81575static SQLITE_NOINLINE void backupUpdate(
81576 sqlite3_backup *p,
81577 Pgno iPage,
81578 const u8 *aData
81579){
81580 assert( p!=0 );
81581 do{
81582 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
81583 if( !isFatalError(rc: p->rc) && iPage<p->iNext ){
81584 /* The backup process p has already copied page iPage. But now it
81585 ** has been modified by a transaction on the source pager. Copy
81586 ** the new data into the backup.
81587 */
81588 int rc;
81589 assert( p->pDestDb );
81590 sqlite3_mutex_enter(p: p->pDestDb->mutex);
81591 rc = backupOnePage(p, iSrcPg: iPage, zSrcData: aData, bUpdate: 1);
81592 sqlite3_mutex_leave(p: p->pDestDb->mutex);
81593 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
81594 if( rc!=SQLITE_OK ){
81595 p->rc = rc;
81596 }
81597 }
81598 }while( (p = p->pNext)!=0 );
81599}
81600SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
81601 if( pBackup ) backupUpdate(p: pBackup, iPage, aData);
81602}
81603
81604/*
81605** Restart the backup process. This is called when the pager layer
81606** detects that the database has been modified by an external database
81607** connection. In this case there is no way of knowing which of the
81608** pages that have been copied into the destination database are still
81609** valid and which are not, so the entire process needs to be restarted.
81610**
81611** It is assumed that the mutex associated with the BtShared object
81612** corresponding to the source database is held when this function is
81613** called.
81614*/
81615SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
81616 sqlite3_backup *p; /* Iterator variable */
81617 for(p=pBackup; p; p=p->pNext){
81618 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
81619 p->iNext = 1;
81620 }
81621}
81622
81623#ifndef SQLITE_OMIT_VACUUM
81624/*
81625** Copy the complete content of pBtFrom into pBtTo. A transaction
81626** must be active for both files.
81627**
81628** The size of file pTo may be reduced by this operation. If anything
81629** goes wrong, the transaction on pTo is rolled back. If successful, the
81630** transaction is committed before returning.
81631*/
81632SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
81633 int rc;
81634 sqlite3_file *pFd; /* File descriptor for database pTo */
81635 sqlite3_backup b;
81636 sqlite3BtreeEnter(p: pTo);
81637 sqlite3BtreeEnter(p: pFrom);
81638
81639 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE );
81640 pFd = sqlite3PagerFile(pPager: sqlite3BtreePager(p: pTo));
81641 if( pFd->pMethods ){
81642 i64 nByte = sqlite3BtreeGetPageSize(p: pFrom)*(i64)sqlite3BtreeLastPage(p: pFrom);
81643 rc = sqlite3OsFileControl(id: pFd, SQLITE_FCNTL_OVERWRITE, pArg: &nByte);
81644 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
81645 if( rc ) goto copy_finished;
81646 }
81647
81648 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
81649 ** to 0. This is used by the implementations of sqlite3_backup_step()
81650 ** and sqlite3_backup_finish() to detect that they are being called
81651 ** from this function, not directly by the user.
81652 */
81653 memset(s: &b, c: 0, n: sizeof(b));
81654 b.pSrcDb = pFrom->db;
81655 b.pSrc = pFrom;
81656 b.pDest = pTo;
81657 b.iNext = 1;
81658
81659 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
81660 ** file. By passing this as the number of pages to copy to
81661 ** sqlite3_backup_step(), we can guarantee that the copy finishes
81662 ** within a single call (unless an error occurs). The assert() statement
81663 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
81664 ** or an error code. */
81665 sqlite3_backup_step(p: &b, nPage: 0x7FFFFFFF);
81666 assert( b.rc!=SQLITE_OK );
81667
81668 rc = sqlite3_backup_finish(p: &b);
81669 if( rc==SQLITE_OK ){
81670 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
81671 }else{
81672 sqlite3PagerClearCache(pPager: sqlite3BtreePager(p: b.pDest));
81673 }
81674
81675 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE );
81676copy_finished:
81677 sqlite3BtreeLeave(p: pFrom);
81678 sqlite3BtreeLeave(p: pTo);
81679 return rc;
81680}
81681#endif /* SQLITE_OMIT_VACUUM */
81682
81683/************** End of backup.c **********************************************/
81684/************** Begin file vdbemem.c *****************************************/
81685/*
81686** 2004 May 26
81687**
81688** The author disclaims copyright to this source code. In place of
81689** a legal notice, here is a blessing:
81690**
81691** May you do good and not evil.
81692** May you find forgiveness for yourself and forgive others.
81693** May you share freely, never taking more than you give.
81694**
81695*************************************************************************
81696**
81697** This file contains code use to manipulate "Mem" structure. A "Mem"
81698** stores a single value in the VDBE. Mem is an opaque structure visible
81699** only within the VDBE. Interface routines refer to a Mem using the
81700** name sqlite_value
81701*/
81702/* #include "sqliteInt.h" */
81703/* #include "vdbeInt.h" */
81704
81705/* True if X is a power of two. 0 is considered a power of two here.
81706** In other words, return true if X has at most one bit set.
81707*/
81708#define ISPOWEROF2(X) (((X)&((X)-1))==0)
81709
81710#ifdef SQLITE_DEBUG
81711/*
81712** Check invariants on a Mem object.
81713**
81714** This routine is intended for use inside of assert() statements, like
81715** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
81716*/
81717SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
81718 /* If MEM_Dyn is set then Mem.xDel!=0.
81719 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
81720 */
81721 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
81722
81723 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
81724 ** ensure that if Mem.szMalloc>0 then it is safe to do
81725 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
81726 ** That saves a few cycles in inner loops. */
81727 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
81728
81729 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
81730 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
81731
81732 if( p->flags & MEM_Null ){
81733 /* Cannot be both MEM_Null and some other type */
81734 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
81735
81736 /* If MEM_Null is set, then either the value is a pure NULL (the usual
81737 ** case) or it is a pointer set using sqlite3_bind_pointer() or
81738 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
81739 ** set.
81740 */
81741 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
81742 /* This is a pointer type. There may be a flag to indicate what to
81743 ** do with the pointer. */
81744 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
81745 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
81746 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
81747
81748 /* No other bits set */
81749 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
81750 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
81751 }else{
81752 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
81753 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
81754 }
81755 }else{
81756 /* The MEM_Cleared bit is only allowed on NULLs */
81757 assert( (p->flags & MEM_Cleared)==0 );
81758 }
81759
81760 /* The szMalloc field holds the correct memory allocation size */
81761 assert( p->szMalloc==0
81762 || (p->flags==MEM_Undefined
81763 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
81764 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
81765
81766 /* If p holds a string or blob, the Mem.z must point to exactly
81767 ** one of the following:
81768 **
81769 ** (1) Memory in Mem.zMalloc and managed by the Mem object
81770 ** (2) Memory to be freed using Mem.xDel
81771 ** (3) An ephemeral string or blob
81772 ** (4) A static string or blob
81773 */
81774 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
81775 assert(
81776 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
81777 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
81778 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
81779 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
81780 );
81781 }
81782 return 1;
81783}
81784#endif
81785
81786/*
81787** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
81788** into a buffer.
81789*/
81790static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
81791 StrAccum acc;
81792 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
81793 assert( sz>22 );
81794 if( p->flags & MEM_Int ){
81795#if GCC_VERSION>=7000000
81796 /* Work-around for GCC bug
81797 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
81798 i64 x;
81799 assert( (p->flags&MEM_Int)*2==sizeof(x) );
81800 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
81801 p->n = sqlite3Int64ToText(x, zBuf);
81802#else
81803 p->n = sqlite3Int64ToText(v: p->u.i, zOut: zBuf);
81804#endif
81805 }else{
81806 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n: sz, mx: 0);
81807 sqlite3_str_appendf(p: &acc, zFormat: "%!.15g",
81808 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
81809 assert( acc.zText==zBuf && acc.mxAlloc<=0 );
81810 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
81811 p->n = acc.nChar;
81812 }
81813}
81814
81815#ifdef SQLITE_DEBUG
81816/*
81817** Validity checks on pMem. pMem holds a string.
81818**
81819** (1) Check that string value of pMem agrees with its integer or real value.
81820** (2) Check that the string is correctly zero terminated
81821**
81822** A single int or real value always converts to the same strings. But
81823** many different strings can be converted into the same int or real.
81824** If a table contains a numeric value and an index is based on the
81825** corresponding string value, then it is important that the string be
81826** derived from the numeric value, not the other way around, to ensure
81827** that the index and table are consistent. See ticket
81828** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
81829** an example.
81830**
81831** This routine looks at pMem to verify that if it has both a numeric
81832** representation and a string representation then the string rep has
81833** been derived from the numeric and not the other way around. It returns
81834** true if everything is ok and false if there is a problem.
81835**
81836** This routine is for use inside of assert() statements only.
81837*/
81838SQLITE_PRIVATE int sqlite3VdbeMemValidStrRep(Mem *p){
81839 Mem tmp;
81840 char zBuf[100];
81841 char *z;
81842 int i, j, incr;
81843 if( (p->flags & MEM_Str)==0 ) return 1;
81844 if( p->db && p->db->mallocFailed ) return 1;
81845 if( p->flags & MEM_Term ){
81846 /* Insure that the string is properly zero-terminated. Pay particular
81847 ** attention to the case where p->n is odd */
81848 if( p->szMalloc>0 && p->z==p->zMalloc ){
81849 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
81850 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
81851 }
81852 assert( p->z[p->n]==0 );
81853 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
81854 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
81855 }
81856 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
81857 memcpy(&tmp, p, sizeof(tmp));
81858 vdbeMemRenderNum(sizeof(zBuf), zBuf, &tmp);
81859 z = p->z;
81860 i = j = 0;
81861 incr = 1;
81862 if( p->enc!=SQLITE_UTF8 ){
81863 incr = 2;
81864 if( p->enc==SQLITE_UTF16BE ) z++;
81865 }
81866 while( zBuf[j] ){
81867 if( zBuf[j++]!=z[i] ) return 0;
81868 i += incr;
81869 }
81870 return 1;
81871}
81872#endif /* SQLITE_DEBUG */
81873
81874/*
81875** If pMem is an object with a valid string representation, this routine
81876** ensures the internal encoding for the string representation is
81877** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
81878**
81879** If pMem is not a string object, or the encoding of the string
81880** representation is already stored using the requested encoding, then this
81881** routine is a no-op.
81882**
81883** SQLITE_OK is returned if the conversion is successful (or not required).
81884** SQLITE_NOMEM may be returned if a malloc() fails during conversion
81885** between formats.
81886*/
81887SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
81888#ifndef SQLITE_OMIT_UTF16
81889 int rc;
81890#endif
81891 assert( pMem!=0 );
81892 assert( !sqlite3VdbeMemIsRowSet(pMem) );
81893 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
81894 || desiredEnc==SQLITE_UTF16BE );
81895 if( !(pMem->flags&MEM_Str) ){
81896 pMem->enc = desiredEnc;
81897 return SQLITE_OK;
81898 }
81899 if( pMem->enc==desiredEnc ){
81900 return SQLITE_OK;
81901 }
81902 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81903#ifdef SQLITE_OMIT_UTF16
81904 return SQLITE_ERROR;
81905#else
81906
81907 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
81908 ** then the encoding of the value may not have changed.
81909 */
81910 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc: (u8)desiredEnc);
81911 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
81912 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
81913 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
81914 return rc;
81915#endif
81916}
81917
81918/*
81919** Make sure pMem->z points to a writable allocation of at least n bytes.
81920**
81921** If the bPreserve argument is true, then copy of the content of
81922** pMem->z into the new allocation. pMem must be either a string or
81923** blob if bPreserve is true. If bPreserve is false, any prior content
81924** in pMem->z is discarded.
81925*/
81926SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
81927 assert( sqlite3VdbeCheckMemInvariants(pMem) );
81928 assert( !sqlite3VdbeMemIsRowSet(pMem) );
81929 testcase( pMem->db==0 );
81930
81931 /* If the bPreserve flag is set to true, then the memory cell must already
81932 ** contain a valid string or blob value. */
81933 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
81934 testcase( bPreserve && pMem->z==0 );
81935
81936 assert( pMem->szMalloc==0
81937 || (pMem->flags==MEM_Undefined
81938 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
81939 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
81940 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
81941 if( pMem->db ){
81942 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(db: pMem->db, p: pMem->z, n);
81943 }else{
81944 pMem->zMalloc = sqlite3Realloc(pOld: pMem->z, nBytes: n);
81945 if( pMem->zMalloc==0 ) sqlite3_free(p: pMem->z);
81946 pMem->z = pMem->zMalloc;
81947 }
81948 bPreserve = 0;
81949 }else{
81950 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(db: pMem->db, p: pMem->zMalloc);
81951 pMem->zMalloc = sqlite3DbMallocRaw(db: pMem->db, n);
81952 }
81953 if( pMem->zMalloc==0 ){
81954 sqlite3VdbeMemSetNull(pMem);
81955 pMem->z = 0;
81956 pMem->szMalloc = 0;
81957 return SQLITE_NOMEM_BKPT;
81958 }else{
81959 pMem->szMalloc = sqlite3DbMallocSize(db: pMem->db, p: pMem->zMalloc);
81960 }
81961
81962 if( bPreserve && pMem->z ){
81963 assert( pMem->z!=pMem->zMalloc );
81964 memcpy(dest: pMem->zMalloc, src: pMem->z, n: pMem->n);
81965 }
81966 if( (pMem->flags&MEM_Dyn)!=0 ){
81967 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
81968 pMem->xDel((void *)(pMem->z));
81969 }
81970
81971 pMem->z = pMem->zMalloc;
81972 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
81973 return SQLITE_OK;
81974}
81975
81976/*
81977** Change the pMem->zMalloc allocation to be at least szNew bytes.
81978** If pMem->zMalloc already meets or exceeds the requested size, this
81979** routine is a no-op.
81980**
81981** Any prior string or blob content in the pMem object may be discarded.
81982** The pMem->xDel destructor is called, if it exists. Though MEM_Str
81983** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
81984** and MEM_Null values are preserved.
81985**
81986** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
81987** if unable to complete the resizing.
81988*/
81989SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
81990 assert( CORRUPT_DB || szNew>0 );
81991 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
81992 if( pMem->szMalloc<szNew ){
81993 return sqlite3VdbeMemGrow(pMem, n: szNew, bPreserve: 0);
81994 }
81995 assert( (pMem->flags & MEM_Dyn)==0 );
81996 pMem->z = pMem->zMalloc;
81997 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
81998 return SQLITE_OK;
81999}
82000
82001/*
82002** If pMem is already a string, detect if it is a zero-terminated
82003** string, or make it into one if possible, and mark it as such.
82004**
82005** This is an optimization. Correct operation continues even if
82006** this routine is a no-op.
82007*/
82008SQLITE_PRIVATE void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){
82009 if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){
82010 /* pMem must be a string, and it cannot be an ephemeral or static string */
82011 return;
82012 }
82013 if( pMem->enc!=SQLITE_UTF8 ) return;
82014 if( NEVER(pMem->z==0) ) return;
82015 if( pMem->flags & MEM_Dyn ){
82016 if( pMem->xDel==sqlite3_free
82017 && sqlite3_msize(p: pMem->z) >= (u64)(pMem->n+1)
82018 ){
82019 pMem->z[pMem->n] = 0;
82020 pMem->flags |= MEM_Term;
82021 return;
82022 }
82023 if( pMem->xDel==(void(*)(void*))sqlite3RCStrUnref ){
82024 /* Blindly assume that all RCStr objects are zero-terminated */
82025 pMem->flags |= MEM_Term;
82026 return;
82027 }
82028 }else if( pMem->szMalloc >= pMem->n+1 ){
82029 pMem->z[pMem->n] = 0;
82030 pMem->flags |= MEM_Term;
82031 return;
82032 }
82033}
82034
82035/*
82036** It is already known that pMem contains an unterminated string.
82037** Add the zero terminator.
82038**
82039** Three bytes of zero are added. In this way, there is guaranteed
82040** to be a double-zero byte at an even byte boundary in order to
82041** terminate a UTF16 string, even if the initial size of the buffer
82042** is an odd number of bytes.
82043*/
82044static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
82045 if( sqlite3VdbeMemGrow(pMem, n: pMem->n+3, bPreserve: 1) ){
82046 return SQLITE_NOMEM_BKPT;
82047 }
82048 pMem->z[pMem->n] = 0;
82049 pMem->z[pMem->n+1] = 0;
82050 pMem->z[pMem->n+2] = 0;
82051 pMem->flags |= MEM_Term;
82052 return SQLITE_OK;
82053}
82054
82055/*
82056** Change pMem so that its MEM_Str or MEM_Blob value is stored in
82057** MEM.zMalloc, where it can be safely written.
82058**
82059** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
82060*/
82061SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
82062 assert( pMem!=0 );
82063 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82064 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82065 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
82066 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
82067 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
82068 int rc = vdbeMemAddTerminator(pMem);
82069 if( rc ) return rc;
82070 }
82071 }
82072 pMem->flags &= ~MEM_Ephem;
82073#ifdef SQLITE_DEBUG
82074 pMem->pScopyFrom = 0;
82075#endif
82076
82077 return SQLITE_OK;
82078}
82079
82080/*
82081** If the given Mem* has a zero-filled tail, turn it into an ordinary
82082** blob stored in dynamically allocated space.
82083*/
82084#ifndef SQLITE_OMIT_INCRBLOB
82085SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
82086 int nByte;
82087 assert( pMem!=0 );
82088 assert( pMem->flags & MEM_Zero );
82089 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
82090 testcase( sqlite3_value_nochange(pMem) );
82091 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82092 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82093
82094 /* Set nByte to the number of bytes required to store the expanded blob. */
82095 nByte = pMem->n + pMem->u.nZero;
82096 if( nByte<=0 ){
82097 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
82098 nByte = 1;
82099 }
82100 if( sqlite3VdbeMemGrow(pMem, n: nByte, bPreserve: 1) ){
82101 return SQLITE_NOMEM_BKPT;
82102 }
82103 assert( pMem->z!=0 );
82104 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
82105
82106 memset(s: &pMem->z[pMem->n], c: 0, n: pMem->u.nZero);
82107 pMem->n += pMem->u.nZero;
82108 pMem->flags &= ~(MEM_Zero|MEM_Term);
82109 return SQLITE_OK;
82110}
82111#endif
82112
82113/*
82114** Make sure the given Mem is \u0000 terminated.
82115*/
82116SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
82117 assert( pMem!=0 );
82118 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82119 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
82120 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
82121 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
82122 return SQLITE_OK; /* Nothing to do */
82123 }else{
82124 return vdbeMemAddTerminator(pMem);
82125 }
82126}
82127
82128/*
82129** Add MEM_Str to the set of representations for the given Mem. This
82130** routine is only called if pMem is a number of some kind, not a NULL
82131** or a BLOB.
82132**
82133** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
82134** if bForce is true but are retained if bForce is false.
82135**
82136** A MEM_Null value will never be passed to this function. This function is
82137** used for converting values to text for returning to the user (i.e. via
82138** sqlite3_value_text()), or for ensuring that values to be used as btree
82139** keys are strings. In the former case a NULL pointer is returned the
82140** user and the latter is an internal programming error.
82141*/
82142SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
82143 const int nByte = 32;
82144
82145 assert( pMem!=0 );
82146 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82147 assert( !(pMem->flags&MEM_Zero) );
82148 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
82149 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
82150 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82151 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
82152
82153
82154 if( sqlite3VdbeMemClearAndResize(pMem, szNew: nByte) ){
82155 pMem->enc = 0;
82156 return SQLITE_NOMEM_BKPT;
82157 }
82158
82159 vdbeMemRenderNum(sz: nByte, zBuf: pMem->z, p: pMem);
82160 assert( pMem->z!=0 );
82161 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );
82162 pMem->enc = SQLITE_UTF8;
82163 pMem->flags |= MEM_Str|MEM_Term;
82164 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
82165 sqlite3VdbeChangeEncoding(pMem, desiredEnc: enc);
82166 return SQLITE_OK;
82167}
82168
82169/*
82170** Memory cell pMem contains the context of an aggregate function.
82171** This routine calls the finalize method for that function. The
82172** result of the aggregate is stored back into pMem.
82173**
82174** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
82175** otherwise.
82176*/
82177SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
82178 sqlite3_context ctx;
82179 Mem t;
82180 assert( pFunc!=0 );
82181 assert( pMem!=0 );
82182 assert( pMem->db!=0 );
82183 assert( pFunc->xFinalize!=0 );
82184 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
82185 assert( sqlite3_mutex_held(pMem->db->mutex) );
82186 memset(s: &ctx, c: 0, n: sizeof(ctx));
82187 memset(s: &t, c: 0, n: sizeof(t));
82188 t.flags = MEM_Null;
82189 t.db = pMem->db;
82190 ctx.pOut = &t;
82191 ctx.pMem = pMem;
82192 ctx.pFunc = pFunc;
82193 ctx.enc = ENC(t.db);
82194 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
82195 assert( (pMem->flags & MEM_Dyn)==0 );
82196 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(db: pMem->db, p: pMem->zMalloc);
82197 memcpy(dest: pMem, src: &t, n: sizeof(t));
82198 return ctx.isError;
82199}
82200
82201/*
82202** Memory cell pAccum contains the context of an aggregate function.
82203** This routine calls the xValue method for that function and stores
82204** the results in memory cell pMem.
82205**
82206** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
82207** otherwise.
82208*/
82209#ifndef SQLITE_OMIT_WINDOWFUNC
82210SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
82211 sqlite3_context ctx;
82212 assert( pFunc!=0 );
82213 assert( pFunc->xValue!=0 );
82214 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
82215 assert( pAccum->db!=0 );
82216 assert( sqlite3_mutex_held(pAccum->db->mutex) );
82217 memset(s: &ctx, c: 0, n: sizeof(ctx));
82218 sqlite3VdbeMemSetNull(pOut);
82219 ctx.pOut = pOut;
82220 ctx.pMem = pAccum;
82221 ctx.pFunc = pFunc;
82222 ctx.enc = ENC(pAccum->db);
82223 pFunc->xValue(&ctx);
82224 return ctx.isError;
82225}
82226#endif /* SQLITE_OMIT_WINDOWFUNC */
82227
82228/*
82229** If the memory cell contains a value that must be freed by
82230** invoking the external callback in Mem.xDel, then this routine
82231** will free that value. It also sets Mem.flags to MEM_Null.
82232**
82233** This is a helper routine for sqlite3VdbeMemSetNull() and
82234** for sqlite3VdbeMemRelease(). Use those other routines as the
82235** entry point for releasing Mem resources.
82236*/
82237static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
82238 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
82239 assert( VdbeMemDynamic(p) );
82240 if( p->flags&MEM_Agg ){
82241 sqlite3VdbeMemFinalize(pMem: p, pFunc: p->u.pDef);
82242 assert( (p->flags & MEM_Agg)==0 );
82243 testcase( p->flags & MEM_Dyn );
82244 }
82245 if( p->flags&MEM_Dyn ){
82246 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
82247 p->xDel((void *)p->z);
82248 }
82249 p->flags = MEM_Null;
82250}
82251
82252/*
82253** Release memory held by the Mem p, both external memory cleared
82254** by p->xDel and memory in p->zMalloc.
82255**
82256** This is a helper routine invoked by sqlite3VdbeMemRelease() in
82257** the unusual case where there really is memory in p that needs
82258** to be freed.
82259*/
82260static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
82261 if( VdbeMemDynamic(p) ){
82262 vdbeMemClearExternAndSetNull(p);
82263 }
82264 if( p->szMalloc ){
82265 sqlite3DbFreeNN(db: p->db, p: p->zMalloc);
82266 p->szMalloc = 0;
82267 }
82268 p->z = 0;
82269}
82270
82271/*
82272** Release any memory resources held by the Mem. Both the memory that is
82273** free by Mem.xDel and the Mem.zMalloc allocation are freed.
82274**
82275** Use this routine prior to clean up prior to abandoning a Mem, or to
82276** reset a Mem back to its minimum memory utilization.
82277**
82278** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
82279** prior to inserting new content into the Mem.
82280*/
82281SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
82282 assert( sqlite3VdbeCheckMemInvariants(p) );
82283 if( VdbeMemDynamic(p) || p->szMalloc ){
82284 vdbeMemClear(p);
82285 }
82286}
82287
82288/* Like sqlite3VdbeMemRelease() but faster for cases where we
82289** know in advance that the Mem is not MEM_Dyn or MEM_Agg.
82290*/
82291SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem *p){
82292 assert( !VdbeMemDynamic(p) );
82293 if( p->szMalloc ) vdbeMemClear(p);
82294}
82295
82296/*
82297** Return some kind of integer value which is the best we can do
82298** at representing the value that *pMem describes as an integer.
82299** If pMem is an integer, then the value is exact. If pMem is
82300** a floating-point then the value returned is the integer part.
82301** If pMem is a string or blob, then we make an attempt to convert
82302** it into an integer and return that. If pMem represents an
82303** an SQL-NULL value, return 0.
82304**
82305** If pMem represents a string value, its encoding might be changed.
82306*/
82307static SQLITE_NOINLINE i64 memIntValue(const Mem *pMem){
82308 i64 value = 0;
82309 sqlite3Atoi64(zNum: pMem->z, pNum: &value, length: pMem->n, enc: pMem->enc);
82310 return value;
82311}
82312SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem *pMem){
82313 int flags;
82314 assert( pMem!=0 );
82315 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82316 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
82317 flags = pMem->flags;
82318 if( flags & (MEM_Int|MEM_IntReal) ){
82319 testcase( flags & MEM_IntReal );
82320 return pMem->u.i;
82321 }else if( flags & MEM_Real ){
82322 return sqlite3RealToI64(pMem->u.r);
82323 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
82324 return memIntValue(pMem);
82325 }else{
82326 return 0;
82327 }
82328}
82329
82330/*
82331** Return the best representation of pMem that we can get into a
82332** double. If pMem is already a double or an integer, return its
82333** value. If it is a string or blob, try to convert it to a double.
82334** If it is a NULL, return 0.0.
82335*/
82336static SQLITE_NOINLINE double memRealValue(Mem *pMem){
82337 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
82338 double val = (double)0;
82339 sqlite3AtoF(z: pMem->z, pResult: &val, length: pMem->n, enc: pMem->enc);
82340 return val;
82341}
82342SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
82343 assert( pMem!=0 );
82344 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82345 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
82346 if( pMem->flags & MEM_Real ){
82347 return pMem->u.r;
82348 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
82349 testcase( pMem->flags & MEM_IntReal );
82350 return (double)pMem->u.i;
82351 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
82352 return memRealValue(pMem);
82353 }else{
82354 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
82355 return (double)0;
82356 }
82357}
82358
82359/*
82360** Return 1 if pMem represents true, and return 0 if pMem represents false.
82361** Return the value ifNull if pMem is NULL.
82362*/
82363SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
82364 testcase( pMem->flags & MEM_IntReal );
82365 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
82366 if( pMem->flags & MEM_Null ) return ifNull;
82367 return sqlite3VdbeRealValue(pMem)!=0.0;
82368}
82369
82370/*
82371** The MEM structure is already a MEM_Real or MEM_IntReal. Try to
82372** make it a MEM_Int if we can.
82373*/
82374SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
82375 assert( pMem!=0 );
82376 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
82377 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82378 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82379 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
82380
82381 if( pMem->flags & MEM_IntReal ){
82382 MemSetTypeFlag(pMem, MEM_Int);
82383 }else{
82384 i64 ix = sqlite3RealToI64(pMem->u.r);
82385
82386 /* Only mark the value as an integer if
82387 **
82388 ** (1) the round-trip conversion real->int->real is a no-op, and
82389 ** (2) The integer is neither the largest nor the smallest
82390 ** possible integer (ticket #3922)
82391 **
82392 ** The second and third terms in the following conditional enforces
82393 ** the second condition under the assumption that addition overflow causes
82394 ** values to wrap around.
82395 */
82396 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
82397 pMem->u.i = ix;
82398 MemSetTypeFlag(pMem, MEM_Int);
82399 }
82400 }
82401}
82402
82403/*
82404** Convert pMem to type integer. Invalidate any prior representations.
82405*/
82406SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
82407 assert( pMem!=0 );
82408 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82409 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82410 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
82411
82412 pMem->u.i = sqlite3VdbeIntValue(pMem);
82413 MemSetTypeFlag(pMem, MEM_Int);
82414 return SQLITE_OK;
82415}
82416
82417/*
82418** Convert pMem so that it is of type MEM_Real.
82419** Invalidate any prior representations.
82420*/
82421SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
82422 assert( pMem!=0 );
82423 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82424 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
82425
82426 pMem->u.r = sqlite3VdbeRealValue(pMem);
82427 MemSetTypeFlag(pMem, MEM_Real);
82428 return SQLITE_OK;
82429}
82430
82431/* Compare a floating point value to an integer. Return true if the two
82432** values are the same within the precision of the floating point value.
82433**
82434** This function assumes that i was obtained by assignment from r1.
82435**
82436** For some versions of GCC on 32-bit machines, if you do the more obvious
82437** comparison of "r1==(double)i" you sometimes get an answer of false even
82438** though the r1 and (double)i values are bit-for-bit the same.
82439*/
82440SQLITE_PRIVATE int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
82441 double r2 = (double)i;
82442 return r1==0.0
82443 || (memcmp(s1: &r1, s2: &r2, n: sizeof(r1))==0
82444 && i >= -2251799813685248LL && i < 2251799813685248LL);
82445}
82446
82447/* Convert a floating point value to its closest integer. Do so in
82448** a way that avoids 'outside the range of representable values' warnings
82449** from UBSAN.
82450*/
82451SQLITE_PRIVATE i64 sqlite3RealToI64(double r){
82452 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;
82453 if( r>+9223372036854774784.0 ) return LARGEST_INT64;
82454 return (i64)r;
82455}
82456
82457/*
82458** Convert pMem so that it has type MEM_Real or MEM_Int.
82459** Invalidate any prior representations.
82460**
82461** Every effort is made to force the conversion, even if the input
82462** is a string that does not look completely like a number. Convert
82463** as much of the string as we can and ignore the rest.
82464*/
82465SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
82466 assert( pMem!=0 );
82467 testcase( pMem->flags & MEM_Int );
82468 testcase( pMem->flags & MEM_Real );
82469 testcase( pMem->flags & MEM_IntReal );
82470 testcase( pMem->flags & MEM_Null );
82471 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
82472 int rc;
82473 sqlite3_int64 ix;
82474 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
82475 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82476 rc = sqlite3AtoF(z: pMem->z, pResult: &pMem->u.r, length: pMem->n, enc: pMem->enc);
82477 if( ((rc==0 || rc==1) && sqlite3Atoi64(zNum: pMem->z, pNum: &ix, length: pMem->n, enc: pMem->enc)<=1)
82478 || sqlite3RealSameAsInt(r1: pMem->u.r, i: (ix = sqlite3RealToI64(r: pMem->u.r)))
82479 ){
82480 pMem->u.i = ix;
82481 MemSetTypeFlag(pMem, MEM_Int);
82482 }else{
82483 MemSetTypeFlag(pMem, MEM_Real);
82484 }
82485 }
82486 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
82487 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
82488 return SQLITE_OK;
82489}
82490
82491/*
82492** Cast the datatype of the value in pMem according to the affinity
82493** "aff". Casting is different from applying affinity in that a cast
82494** is forced. In other words, the value is converted into the desired
82495** affinity even if that results in loss of data. This routine is
82496** used (for example) to implement the SQL "cast()" operator.
82497*/
82498SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
82499 if( pMem->flags & MEM_Null ) return SQLITE_OK;
82500 switch( aff ){
82501 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
82502 if( (pMem->flags & MEM_Blob)==0 ){
82503 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
82504 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
82505 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
82506 }else{
82507 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
82508 }
82509 break;
82510 }
82511 case SQLITE_AFF_NUMERIC: {
82512 sqlite3VdbeMemNumerify(pMem);
82513 break;
82514 }
82515 case SQLITE_AFF_INTEGER: {
82516 sqlite3VdbeMemIntegerify(pMem);
82517 break;
82518 }
82519 case SQLITE_AFF_REAL: {
82520 sqlite3VdbeMemRealify(pMem);
82521 break;
82522 }
82523 default: {
82524 int rc;
82525 assert( aff==SQLITE_AFF_TEXT );
82526 assert( MEM_Str==(MEM_Blob>>3) );
82527 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
82528 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
82529 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
82530 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
82531 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
82532 rc = sqlite3VdbeChangeEncoding(pMem, desiredEnc: encoding);
82533 if( rc ) return rc;
82534 sqlite3VdbeMemZeroTerminateIfAble(pMem);
82535 }
82536 }
82537 return SQLITE_OK;
82538}
82539
82540/*
82541** Initialize bulk memory to be a consistent Mem object.
82542**
82543** The minimum amount of initialization feasible is performed.
82544*/
82545SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
82546 assert( (flags & ~MEM_TypeMask)==0 );
82547 pMem->flags = flags;
82548 pMem->db = db;
82549 pMem->szMalloc = 0;
82550}
82551
82552
82553/*
82554** Delete any previous value and set the value stored in *pMem to NULL.
82555**
82556** This routine calls the Mem.xDel destructor to dispose of values that
82557** require the destructor. But it preserves the Mem.zMalloc memory allocation.
82558** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
82559** routine to invoke the destructor and deallocates Mem.zMalloc.
82560**
82561** Use this routine to reset the Mem prior to insert a new value.
82562**
82563** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
82564*/
82565SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
82566 if( VdbeMemDynamic(pMem) ){
82567 vdbeMemClearExternAndSetNull(p: pMem);
82568 }else{
82569 pMem->flags = MEM_Null;
82570 }
82571}
82572SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
82573 sqlite3VdbeMemSetNull(pMem: (Mem*)p);
82574}
82575
82576/*
82577** Delete any previous value and set the value to be a BLOB of length
82578** n containing all zeros.
82579*/
82580#ifndef SQLITE_OMIT_INCRBLOB
82581SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
82582 sqlite3VdbeMemRelease(p: pMem);
82583 pMem->flags = MEM_Blob|MEM_Zero;
82584 pMem->n = 0;
82585 if( n<0 ) n = 0;
82586 pMem->u.nZero = n;
82587 pMem->enc = SQLITE_UTF8;
82588 pMem->z = 0;
82589}
82590#else
82591SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
82592 int nByte = n>0?n:1;
82593 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
82594 return SQLITE_NOMEM_BKPT;
82595 }
82596 assert( pMem->z!=0 );
82597 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
82598 memset(pMem->z, 0, nByte);
82599 pMem->n = n>0?n:0;
82600 pMem->flags = MEM_Blob;
82601 pMem->enc = SQLITE_UTF8;
82602 return SQLITE_OK;
82603}
82604#endif
82605
82606/*
82607** The pMem is known to contain content that needs to be destroyed prior
82608** to a value change. So invoke the destructor, then set the value to
82609** a 64-bit integer.
82610*/
82611static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
82612 sqlite3VdbeMemSetNull(pMem);
82613 pMem->u.i = val;
82614 pMem->flags = MEM_Int;
82615}
82616
82617/*
82618** Delete any previous value and set the value stored in *pMem to val,
82619** manifest type INTEGER.
82620*/
82621SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
82622 if( VdbeMemDynamic(pMem) ){
82623 vdbeReleaseAndSetInt64(pMem, val);
82624 }else{
82625 pMem->u.i = val;
82626 pMem->flags = MEM_Int;
82627 }
82628}
82629
82630/* A no-op destructor */
82631SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
82632
82633/*
82634** Set the value stored in *pMem should already be a NULL.
82635** Also store a pointer to go with it.
82636*/
82637SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
82638 Mem *pMem,
82639 void *pPtr,
82640 const char *zPType,
82641 void (*xDestructor)(void*)
82642){
82643 assert( pMem->flags==MEM_Null );
82644 vdbeMemClear(p: pMem);
82645 pMem->u.zPType = zPType ? zPType : "";
82646 pMem->z = pPtr;
82647 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
82648 pMem->eSubtype = 'p';
82649 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
82650}
82651
82652#ifndef SQLITE_OMIT_FLOATING_POINT
82653/*
82654** Delete any previous value and set the value stored in *pMem to val,
82655** manifest type REAL.
82656*/
82657SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
82658 sqlite3VdbeMemSetNull(pMem);
82659 if( !sqlite3IsNaN(x: val) ){
82660 pMem->u.r = val;
82661 pMem->flags = MEM_Real;
82662 }
82663}
82664#endif
82665
82666#ifdef SQLITE_DEBUG
82667/*
82668** Return true if the Mem holds a RowSet object. This routine is intended
82669** for use inside of assert() statements.
82670*/
82671SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
82672 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
82673 && pMem->xDel==sqlite3RowSetDelete;
82674}
82675#endif
82676
82677/*
82678** Delete any previous value and set the value of pMem to be an
82679** empty boolean index.
82680**
82681** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
82682** error occurs.
82683*/
82684SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
82685 sqlite3 *db = pMem->db;
82686 RowSet *p;
82687 assert( db!=0 );
82688 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82689 sqlite3VdbeMemRelease(p: pMem);
82690 p = sqlite3RowSetInit(db);
82691 if( p==0 ) return SQLITE_NOMEM;
82692 pMem->z = (char*)p;
82693 pMem->flags = MEM_Blob|MEM_Dyn;
82694 pMem->xDel = sqlite3RowSetDelete;
82695 return SQLITE_OK;
82696}
82697
82698/*
82699** Return true if the Mem object contains a TEXT or BLOB that is
82700** too large - whose size exceeds SQLITE_MAX_LENGTH.
82701*/
82702SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
82703 assert( p->db!=0 );
82704 if( p->flags & (MEM_Str|MEM_Blob) ){
82705 int n = p->n;
82706 if( p->flags & MEM_Zero ){
82707 n += p->u.nZero;
82708 }
82709 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
82710 }
82711 return 0;
82712}
82713
82714#ifdef SQLITE_DEBUG
82715/*
82716** This routine prepares a memory cell for modification by breaking
82717** its link to a shallow copy and by marking any current shallow
82718** copies of this cell as invalid.
82719**
82720** This is used for testing and debugging only - to help ensure that shallow
82721** copies (created by OP_SCopy) are not misused.
82722*/
82723SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
82724 int i;
82725 Mem *pX;
82726 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
82727 if( pX->pScopyFrom==pMem ){
82728 u16 mFlags;
82729 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
82730 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
82731 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
82732 }
82733 /* If pX is marked as a shallow copy of pMem, then try to verify that
82734 ** no significant changes have been made to pX since the OP_SCopy.
82735 ** A significant change would indicated a missed call to this
82736 ** function for pX. Minor changes, such as adding or removing a
82737 ** dual type, are allowed, as long as the underlying value is the
82738 ** same. */
82739 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
82740 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
82741
82742 /* pMem is the register that is changing. But also mark pX as
82743 ** undefined so that we can quickly detect the shallow-copy error */
82744 pX->flags = MEM_Undefined;
82745 pX->pScopyFrom = 0;
82746 }
82747 }
82748 pMem->pScopyFrom = 0;
82749}
82750#endif /* SQLITE_DEBUG */
82751
82752/*
82753** Make an shallow copy of pFrom into pTo. Prior contents of
82754** pTo are freed. The pFrom->z field is not duplicated. If
82755** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
82756** and flags gets srcType (either MEM_Ephem or MEM_Static).
82757*/
82758static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
82759 vdbeMemClearExternAndSetNull(p: pTo);
82760 assert( !VdbeMemDynamic(pTo) );
82761 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
82762}
82763SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
82764 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
82765 assert( pTo->db==pFrom->db );
82766 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,eType: srcType); return; }
82767 memcpy(dest: pTo, src: pFrom, MEMCELLSIZE);
82768 if( (pFrom->flags&MEM_Static)==0 ){
82769 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
82770 assert( srcType==MEM_Ephem || srcType==MEM_Static );
82771 pTo->flags |= srcType;
82772 }
82773}
82774
82775/*
82776** Make a full copy of pFrom into pTo. Prior contents of pTo are
82777** freed before the copy is made.
82778*/
82779SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
82780 int rc = SQLITE_OK;
82781
82782 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
82783 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(p: pTo);
82784 memcpy(dest: pTo, src: pFrom, MEMCELLSIZE);
82785 pTo->flags &= ~MEM_Dyn;
82786 if( pTo->flags&(MEM_Str|MEM_Blob) ){
82787 if( 0==(pFrom->flags&MEM_Static) ){
82788 pTo->flags |= MEM_Ephem;
82789 rc = sqlite3VdbeMemMakeWriteable(pMem: pTo);
82790 }
82791 }
82792
82793 return rc;
82794}
82795
82796/*
82797** Transfer the contents of pFrom to pTo. Any existing value in pTo is
82798** freed. If pFrom contains ephemeral data, a copy is made.
82799**
82800** pFrom contains an SQL NULL when this routine returns.
82801*/
82802SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
82803 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
82804 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
82805 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
82806
82807 sqlite3VdbeMemRelease(p: pTo);
82808 memcpy(dest: pTo, src: pFrom, n: sizeof(Mem));
82809 pFrom->flags = MEM_Null;
82810 pFrom->szMalloc = 0;
82811}
82812
82813/*
82814** Change the value of a Mem to be a string or a BLOB.
82815**
82816** The memory management strategy depends on the value of the xDel
82817** parameter. If the value passed is SQLITE_TRANSIENT, then the
82818** string is copied into a (possibly existing) buffer managed by the
82819** Mem structure. Otherwise, any existing buffer is freed and the
82820** pointer copied.
82821**
82822** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
82823** size limit) then no memory allocation occurs. If the string can be
82824** stored without allocating memory, then it is. If a memory allocation
82825** is required to store the string, then value of pMem is unchanged. In
82826** either case, SQLITE_TOOBIG is returned.
82827**
82828** The "enc" parameter is the text encoding for the string, or zero
82829** to store a blob.
82830**
82831** If n is negative, then the string consists of all bytes up to but
82832** excluding the first zero character. The n parameter must be
82833** non-negative for blobs.
82834*/
82835SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
82836 Mem *pMem, /* Memory cell to set to string value */
82837 const char *z, /* String pointer */
82838 i64 n, /* Bytes in string, or negative */
82839 u8 enc, /* Encoding of z. 0 for BLOBs */
82840 void (*xDel)(void*) /* Destructor function */
82841){
82842 i64 nByte = n; /* New value for pMem->n */
82843 int iLimit; /* Maximum allowed string or blob size */
82844 u16 flags; /* New value for pMem->flags */
82845
82846 assert( pMem!=0 );
82847 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82848 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82849 assert( enc!=0 || n>=0 );
82850
82851 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
82852 if( !z ){
82853 sqlite3VdbeMemSetNull(pMem);
82854 return SQLITE_OK;
82855 }
82856
82857 if( pMem->db ){
82858 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
82859 }else{
82860 iLimit = SQLITE_MAX_LENGTH;
82861 }
82862 if( nByte<0 ){
82863 assert( enc!=0 );
82864 if( enc==SQLITE_UTF8 ){
82865 nByte = strlen(s: z);
82866 }else{
82867 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
82868 }
82869 flags= MEM_Str|MEM_Term;
82870 }else if( enc==0 ){
82871 flags = MEM_Blob;
82872 enc = SQLITE_UTF8;
82873 }else{
82874 flags = MEM_Str;
82875 }
82876 if( nByte>iLimit ){
82877 if( xDel && xDel!=SQLITE_TRANSIENT ){
82878 if( xDel==SQLITE_DYNAMIC ){
82879 sqlite3DbFree(db: pMem->db, p: (void*)z);
82880 }else{
82881 xDel((void*)z);
82882 }
82883 }
82884 sqlite3VdbeMemSetNull(pMem);
82885 return sqlite3ErrorToParser(db: pMem->db, SQLITE_TOOBIG);
82886 }
82887
82888 /* The following block sets the new values of Mem.z and Mem.xDel. It
82889 ** also sets a flag in local variable "flags" to indicate the memory
82890 ** management (one of MEM_Dyn or MEM_Static).
82891 */
82892 if( xDel==SQLITE_TRANSIENT ){
82893 i64 nAlloc = nByte;
82894 if( flags&MEM_Term ){
82895 nAlloc += (enc==SQLITE_UTF8?1:2);
82896 }
82897 testcase( nAlloc==0 );
82898 testcase( nAlloc==31 );
82899 testcase( nAlloc==32 );
82900 if( sqlite3VdbeMemClearAndResize(pMem, szNew: (int)MAX(nAlloc,32)) ){
82901 return SQLITE_NOMEM_BKPT;
82902 }
82903 memcpy(dest: pMem->z, src: z, n: nAlloc);
82904 }else{
82905 sqlite3VdbeMemRelease(p: pMem);
82906 pMem->z = (char *)z;
82907 if( xDel==SQLITE_DYNAMIC ){
82908 pMem->zMalloc = pMem->z;
82909 pMem->szMalloc = sqlite3DbMallocSize(db: pMem->db, p: pMem->zMalloc);
82910 }else{
82911 pMem->xDel = xDel;
82912 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
82913 }
82914 }
82915
82916 pMem->n = (int)(nByte & 0x7fffffff);
82917 pMem->flags = flags;
82918 pMem->enc = enc;
82919
82920#ifndef SQLITE_OMIT_UTF16
82921 if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
82922 return SQLITE_NOMEM_BKPT;
82923 }
82924#endif
82925
82926
82927 return SQLITE_OK;
82928}
82929
82930/*
82931** Move data out of a btree key or data field and into a Mem structure.
82932** The data is payload from the entry that pCur is currently pointing
82933** to. offset and amt determine what portion of the data or key to retrieve.
82934** The result is written into the pMem element.
82935**
82936** The pMem object must have been initialized. This routine will use
82937** pMem->zMalloc to hold the content from the btree, if possible. New
82938** pMem->zMalloc space will be allocated if necessary. The calling routine
82939** is responsible for making sure that the pMem object is eventually
82940** destroyed.
82941**
82942** If this routine fails for any reason (malloc returns NULL or unable
82943** to read from the disk) then the pMem is left in an inconsistent state.
82944*/
82945SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
82946 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
82947 u32 offset, /* Offset from the start of data to return bytes from. */
82948 u32 amt, /* Number of bytes to return. */
82949 Mem *pMem /* OUT: Return data in this Mem structure. */
82950){
82951 int rc;
82952 pMem->flags = MEM_Null;
82953 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
82954 return SQLITE_CORRUPT_BKPT;
82955 }
82956 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, szNew: amt+1)) ){
82957 rc = sqlite3BtreePayload(pCur, offset, amt, pBuf: pMem->z);
82958 if( rc==SQLITE_OK ){
82959 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
82960 pMem->flags = MEM_Blob;
82961 pMem->n = (int)amt;
82962 }else{
82963 sqlite3VdbeMemRelease(p: pMem);
82964 }
82965 }
82966 return rc;
82967}
82968SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(
82969 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
82970 u32 amt, /* Number of bytes to return. */
82971 Mem *pMem /* OUT: Return data in this Mem structure. */
82972){
82973 u32 available = 0; /* Number of bytes available on the local btree page */
82974 int rc = SQLITE_OK; /* Return code */
82975
82976 assert( sqlite3BtreeCursorIsValid(pCur) );
82977 assert( !VdbeMemDynamic(pMem) );
82978
82979 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
82980 ** that both the BtShared and database handle mutexes are held. */
82981 assert( !sqlite3VdbeMemIsRowSet(pMem) );
82982 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, pAmt: &available);
82983 assert( pMem->z!=0 );
82984
82985 if( amt<=available ){
82986 pMem->flags = MEM_Blob|MEM_Ephem;
82987 pMem->n = (int)amt;
82988 }else{
82989 rc = sqlite3VdbeMemFromBtree(pCur, offset: 0, amt, pMem);
82990 }
82991
82992 return rc;
82993}
82994
82995/*
82996** The pVal argument is known to be a value other than NULL.
82997** Convert it into a string with encoding enc and return a pointer
82998** to a zero-terminated version of that string.
82999*/
83000static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
83001 assert( pVal!=0 );
83002 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
83003 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
83004 assert( !sqlite3VdbeMemIsRowSet(pVal) );
83005 assert( (pVal->flags & (MEM_Null))==0 );
83006 if( pVal->flags & (MEM_Blob|MEM_Str) ){
83007 if( ExpandBlob(pVal) ) return 0;
83008 pVal->flags |= MEM_Str;
83009 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
83010 sqlite3VdbeChangeEncoding(pMem: pVal, desiredEnc: enc & ~SQLITE_UTF16_ALIGNED);
83011 }
83012 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
83013 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
83014 if( sqlite3VdbeMemMakeWriteable(pMem: pVal)!=SQLITE_OK ){
83015 return 0;
83016 }
83017 }
83018 sqlite3VdbeMemNulTerminate(pMem: pVal); /* IMP: R-31275-44060 */
83019 }else{
83020 sqlite3VdbeMemStringify(pMem: pVal, enc, bForce: 0);
83021 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
83022 }
83023 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
83024 || pVal->db->mallocFailed );
83025 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
83026 assert( sqlite3VdbeMemValidStrRep(pVal) );
83027 return pVal->z;
83028 }else{
83029 return 0;
83030 }
83031}
83032
83033/* This function is only available internally, it is not part of the
83034** external API. It works in a similar way to sqlite3_value_text(),
83035** except the data returned is in the encoding specified by the second
83036** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
83037** SQLITE_UTF8.
83038**
83039** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
83040** If that is the case, then the result must be aligned on an even byte
83041** boundary.
83042*/
83043SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
83044 if( !pVal ) return 0;
83045 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
83046 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
83047 assert( !sqlite3VdbeMemIsRowSet(pVal) );
83048 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
83049 assert( sqlite3VdbeMemValidStrRep(pVal) );
83050 return pVal->z;
83051 }
83052 if( pVal->flags&MEM_Null ){
83053 return 0;
83054 }
83055 return valueToText(pVal, enc);
83056}
83057
83058/* Return true if sqlit3_value object pVal is a string or blob value
83059** that uses the destructor specified in the second argument.
83060**
83061** TODO: Maybe someday promote this interface into a published API so
83062** that third-party extensions can get access to it?
83063*/
83064SQLITE_PRIVATE int sqlite3ValueIsOfClass(const sqlite3_value *pVal, void(*xFree)(void*)){
83065 if( ALWAYS(pVal!=0)
83066 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)
83067 && (pVal->flags & MEM_Dyn)!=0
83068 && pVal->xDel==xFree
83069 ){
83070 return 1;
83071 }else{
83072 return 0;
83073 }
83074}
83075
83076/*
83077** Create a new sqlite3_value object.
83078*/
83079SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
83080 Mem *p = sqlite3DbMallocZero(db, n: sizeof(*p));
83081 if( p ){
83082 p->flags = MEM_Null;
83083 p->db = db;
83084 }
83085 return p;
83086}
83087
83088/*
83089** Context object passed by sqlite3Stat4ProbeSetValue() through to
83090** valueNew(). See comments above valueNew() for details.
83091*/
83092struct ValueNewStat4Ctx {
83093 Parse *pParse;
83094 Index *pIdx;
83095 UnpackedRecord **ppRec;
83096 int iVal;
83097};
83098
83099/*
83100** Allocate and return a pointer to a new sqlite3_value object. If
83101** the second argument to this function is NULL, the object is allocated
83102** by calling sqlite3ValueNew().
83103**
83104** Otherwise, if the second argument is non-zero, then this function is
83105** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
83106** already been allocated, allocate the UnpackedRecord structure that
83107** that function will return to its caller here. Then return a pointer to
83108** an sqlite3_value within the UnpackedRecord.a[] array.
83109*/
83110static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
83111#ifdef SQLITE_ENABLE_STAT4
83112 if( p ){
83113 UnpackedRecord *pRec = p->ppRec[0];
83114
83115 if( pRec==0 ){
83116 Index *pIdx = p->pIdx; /* Index being probed */
83117 int nByte; /* Bytes of space to allocate */
83118 int i; /* Counter variable */
83119 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
83120
83121 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
83122 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
83123 if( pRec ){
83124 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
83125 if( pRec->pKeyInfo ){
83126 assert( pRec->pKeyInfo->nAllField==nCol );
83127 assert( pRec->pKeyInfo->enc==ENC(db) );
83128 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
83129 for(i=0; i<nCol; i++){
83130 pRec->aMem[i].flags = MEM_Null;
83131 pRec->aMem[i].db = db;
83132 }
83133 }else{
83134 sqlite3DbFreeNN(db, pRec);
83135 pRec = 0;
83136 }
83137 }
83138 if( pRec==0 ) return 0;
83139 p->ppRec[0] = pRec;
83140 }
83141
83142 pRec->nField = p->iVal+1;
83143 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
83144 return &pRec->aMem[p->iVal];
83145 }
83146#else
83147 UNUSED_PARAMETER(p);
83148#endif /* defined(SQLITE_ENABLE_STAT4) */
83149 return sqlite3ValueNew(db);
83150}
83151
83152/*
83153** The expression object indicated by the second argument is guaranteed
83154** to be a scalar SQL function. If
83155**
83156** * all function arguments are SQL literals,
83157** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
83158** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
83159**
83160** then this routine attempts to invoke the SQL function. Assuming no
83161** error occurs, output parameter (*ppVal) is set to point to a value
83162** object containing the result before returning SQLITE_OK.
83163**
83164** Affinity aff is applied to the result of the function before returning.
83165** If the result is a text value, the sqlite3_value object uses encoding
83166** enc.
83167**
83168** If the conditions above are not met, this function returns SQLITE_OK
83169** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
83170** NULL and an SQLite error code returned.
83171*/
83172#ifdef SQLITE_ENABLE_STAT4
83173static int valueFromFunction(
83174 sqlite3 *db, /* The database connection */
83175 const Expr *p, /* The expression to evaluate */
83176 u8 enc, /* Encoding to use */
83177 u8 aff, /* Affinity to use */
83178 sqlite3_value **ppVal, /* Write the new value here */
83179 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
83180){
83181 sqlite3_context ctx; /* Context object for function invocation */
83182 sqlite3_value **apVal = 0; /* Function arguments */
83183 int nVal = 0; /* Size of apVal[] array */
83184 FuncDef *pFunc = 0; /* Function definition */
83185 sqlite3_value *pVal = 0; /* New value */
83186 int rc = SQLITE_OK; /* Return code */
83187 ExprList *pList = 0; /* Function arguments */
83188 int i; /* Iterator variable */
83189
83190 assert( pCtx!=0 );
83191 assert( (p->flags & EP_TokenOnly)==0 );
83192 assert( ExprUseXList(p) );
83193 pList = p->x.pList;
83194 if( pList ) nVal = pList->nExpr;
83195 assert( !ExprHasProperty(p, EP_IntValue) );
83196 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
83197#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
83198 if( pFunc==0 ) return SQLITE_OK;
83199#endif
83200 assert( pFunc );
83201 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83202 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
83203 ){
83204 return SQLITE_OK;
83205 }
83206
83207 if( pList ){
83208 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
83209 if( apVal==0 ){
83210 rc = SQLITE_NOMEM_BKPT;
83211 goto value_from_function_out;
83212 }
83213 for(i=0; i<nVal; i++){
83214 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
83215 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
83216 }
83217 }
83218
83219 pVal = valueNew(db, pCtx);
83220 if( pVal==0 ){
83221 rc = SQLITE_NOMEM_BKPT;
83222 goto value_from_function_out;
83223 }
83224
83225 memset(&ctx, 0, sizeof(ctx));
83226 ctx.pOut = pVal;
83227 ctx.pFunc = pFunc;
83228 ctx.enc = ENC(db);
83229 pFunc->xSFunc(&ctx, nVal, apVal);
83230 if( ctx.isError ){
83231 rc = ctx.isError;
83232 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
83233 }else{
83234 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
83235 assert( rc==SQLITE_OK );
83236 rc = sqlite3VdbeChangeEncoding(pVal, enc);
83237 if( NEVER(rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal)) ){
83238 rc = SQLITE_TOOBIG;
83239 pCtx->pParse->nErr++;
83240 }
83241 }
83242
83243 value_from_function_out:
83244 if( rc!=SQLITE_OK ){
83245 pVal = 0;
83246 pCtx->pParse->rc = rc;
83247 }
83248 if( apVal ){
83249 for(i=0; i<nVal; i++){
83250 sqlite3ValueFree(apVal[i]);
83251 }
83252 sqlite3DbFreeNN(db, apVal);
83253 }
83254
83255 *ppVal = pVal;
83256 return rc;
83257}
83258#else
83259# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
83260#endif /* defined(SQLITE_ENABLE_STAT4) */
83261
83262/*
83263** Extract a value from the supplied expression in the manner described
83264** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
83265** using valueNew().
83266**
83267** If pCtx is NULL and an error occurs after the sqlite3_value object
83268** has been allocated, it is freed before returning. Or, if pCtx is not
83269** NULL, it is assumed that the caller will free any allocated object
83270** in all cases.
83271*/
83272static int valueFromExpr(
83273 sqlite3 *db, /* The database connection */
83274 const Expr *pExpr, /* The expression to evaluate */
83275 u8 enc, /* Encoding to use */
83276 u8 affinity, /* Affinity to use */
83277 sqlite3_value **ppVal, /* Write the new value here */
83278 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
83279){
83280 int op;
83281 char *zVal = 0;
83282 sqlite3_value *pVal = 0;
83283 int negInt = 1;
83284 const char *zNeg = "";
83285 int rc = SQLITE_OK;
83286
83287 assert( pExpr!=0 );
83288 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
83289 if( op==TK_REGISTER ) op = pExpr->op2;
83290
83291 /* Compressed expressions only appear when parsing the DEFAULT clause
83292 ** on a table column definition, and hence only when pCtx==0. This
83293 ** check ensures that an EP_TokenOnly expression is never passed down
83294 ** into valueFromFunction(). */
83295 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
83296
83297 if( op==TK_CAST ){
83298 u8 aff;
83299 assert( !ExprHasProperty(pExpr, EP_IntValue) );
83300 aff = sqlite3AffinityType(pExpr->u.zToken,0);
83301 rc = valueFromExpr(db, pExpr: pExpr->pLeft, enc, affinity: aff, ppVal, pCtx);
83302 testcase( rc!=SQLITE_OK );
83303 if( *ppVal ){
83304#ifdef SQLITE_ENABLE_STAT4
83305 rc = ExpandBlob(*ppVal);
83306#else
83307 /* zero-blobs only come from functions, not literal values. And
83308 ** functions are only processed under STAT4 */
83309 assert( (ppVal[0][0].flags & MEM_Zero)==0 );
83310#endif
83311 sqlite3VdbeMemCast(pMem: *ppVal, aff, encoding: enc);
83312 sqlite3ValueApplyAffinity(*ppVal, affinity, enc);
83313 }
83314 return rc;
83315 }
83316
83317 /* Handle negative integers in a single step. This is needed in the
83318 ** case when the value is -9223372036854775808.
83319 */
83320 if( op==TK_UMINUS
83321 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
83322 pExpr = pExpr->pLeft;
83323 op = pExpr->op;
83324 negInt = -1;
83325 zNeg = "-";
83326 }
83327
83328 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
83329 pVal = valueNew(db, p: pCtx);
83330 if( pVal==0 ) goto no_mem;
83331 if( ExprHasProperty(pExpr, EP_IntValue) ){
83332 sqlite3VdbeMemSetInt64(pMem: pVal, val: (i64)pExpr->u.iValue*negInt);
83333 }else{
83334 zVal = sqlite3MPrintf(db, zFormat: "%s%s", zNeg, pExpr->u.zToken);
83335 if( zVal==0 ) goto no_mem;
83336 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
83337 }
83338 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
83339 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
83340 }else{
83341 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
83342 }
83343 assert( (pVal->flags & MEM_IntReal)==0 );
83344 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
83345 testcase( pVal->flags & MEM_Int );
83346 testcase( pVal->flags & MEM_Real );
83347 pVal->flags &= ~MEM_Str;
83348 }
83349 if( enc!=SQLITE_UTF8 ){
83350 rc = sqlite3VdbeChangeEncoding(pMem: pVal, desiredEnc: enc);
83351 }
83352 }else if( op==TK_UMINUS ) {
83353 /* This branch happens for multiple negative signs. Ex: -(-5) */
83354 if( SQLITE_OK==valueFromExpr(db,pExpr: pExpr->pLeft,enc,affinity,ppVal: &pVal,pCtx)
83355 && pVal!=0
83356 ){
83357 sqlite3VdbeMemNumerify(pMem: pVal);
83358 if( pVal->flags & MEM_Real ){
83359 pVal->u.r = -pVal->u.r;
83360 }else if( pVal->u.i==SMALLEST_INT64 ){
83361#ifndef SQLITE_OMIT_FLOATING_POINT
83362 pVal->u.r = -(double)SMALLEST_INT64;
83363#else
83364 pVal->u.r = LARGEST_INT64;
83365#endif
83366 MemSetTypeFlag(pVal, MEM_Real);
83367 }else{
83368 pVal->u.i = -pVal->u.i;
83369 }
83370 sqlite3ValueApplyAffinity(pVal, affinity, enc);
83371 }
83372 }else if( op==TK_NULL ){
83373 pVal = valueNew(db, p: pCtx);
83374 if( pVal==0 ) goto no_mem;
83375 sqlite3VdbeMemSetNull(pMem: pVal);
83376 }
83377#ifndef SQLITE_OMIT_BLOB_LITERAL
83378 else if( op==TK_BLOB ){
83379 int nVal;
83380 assert( !ExprHasProperty(pExpr, EP_IntValue) );
83381 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
83382 assert( pExpr->u.zToken[1]=='\'' );
83383 pVal = valueNew(db, p: pCtx);
83384 if( !pVal ) goto no_mem;
83385 zVal = &pExpr->u.zToken[2];
83386 nVal = sqlite3Strlen30(z: zVal)-1;
83387 assert( zVal[nVal]=='\'' );
83388 sqlite3VdbeMemSetStr(pMem: pVal, z: sqlite3HexToBlob(db, z: zVal, n: nVal), n: nVal/2,
83389 enc: 0, SQLITE_DYNAMIC);
83390 }
83391#endif
83392#ifdef SQLITE_ENABLE_STAT4
83393 else if( op==TK_FUNCTION && pCtx!=0 ){
83394 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
83395 }
83396#endif
83397 else if( op==TK_TRUEFALSE ){
83398 assert( !ExprHasProperty(pExpr, EP_IntValue) );
83399 pVal = valueNew(db, p: pCtx);
83400 if( pVal ){
83401 pVal->flags = MEM_Int;
83402 pVal->u.i = pExpr->u.zToken[4]==0;
83403 }
83404 }
83405
83406 *ppVal = pVal;
83407 return rc;
83408
83409no_mem:
83410#ifdef SQLITE_ENABLE_STAT4
83411 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
83412#endif
83413 sqlite3OomFault(db);
83414 sqlite3DbFree(db, p: zVal);
83415 assert( *ppVal==0 );
83416#ifdef SQLITE_ENABLE_STAT4
83417 if( pCtx==0 ) sqlite3ValueFree(pVal);
83418#else
83419 assert( pCtx==0 ); sqlite3ValueFree(pVal);
83420#endif
83421 return SQLITE_NOMEM_BKPT;
83422}
83423
83424/*
83425** Create a new sqlite3_value object, containing the value of pExpr.
83426**
83427** This only works for very simple expressions that consist of one constant
83428** token (i.e. "5", "5.1", "'a string'"). If the expression can
83429** be converted directly into a value, then the value is allocated and
83430** a pointer written to *ppVal. The caller is responsible for deallocating
83431** the value by passing it to sqlite3ValueFree() later on. If the expression
83432** cannot be converted to a value, then *ppVal is set to NULL.
83433*/
83434SQLITE_PRIVATE int sqlite3ValueFromExpr(
83435 sqlite3 *db, /* The database connection */
83436 const Expr *pExpr, /* The expression to evaluate */
83437 u8 enc, /* Encoding to use */
83438 u8 affinity, /* Affinity to use */
83439 sqlite3_value **ppVal /* Write the new value here */
83440){
83441 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, pCtx: 0) : 0;
83442}
83443
83444#ifdef SQLITE_ENABLE_STAT4
83445/*
83446** Attempt to extract a value from pExpr and use it to construct *ppVal.
83447**
83448** If pAlloc is not NULL, then an UnpackedRecord object is created for
83449** pAlloc if one does not exist and the new value is added to the
83450** UnpackedRecord object.
83451**
83452** A value is extracted in the following cases:
83453**
83454** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
83455**
83456** * The expression is a bound variable, and this is a reprepare, or
83457**
83458** * The expression is a literal value.
83459**
83460** On success, *ppVal is made to point to the extracted value. The caller
83461** is responsible for ensuring that the value is eventually freed.
83462*/
83463static int stat4ValueFromExpr(
83464 Parse *pParse, /* Parse context */
83465 Expr *pExpr, /* The expression to extract a value from */
83466 u8 affinity, /* Affinity to use */
83467 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
83468 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
83469){
83470 int rc = SQLITE_OK;
83471 sqlite3_value *pVal = 0;
83472 sqlite3 *db = pParse->db;
83473
83474 /* Skip over any TK_COLLATE nodes */
83475 pExpr = sqlite3ExprSkipCollate(pExpr);
83476
83477 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
83478 if( !pExpr ){
83479 pVal = valueNew(db, pAlloc);
83480 if( pVal ){
83481 sqlite3VdbeMemSetNull((Mem*)pVal);
83482 }
83483 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
83484 Vdbe *v;
83485 int iBindVar = pExpr->iColumn;
83486 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
83487 if( (v = pParse->pReprepare)!=0 ){
83488 pVal = valueNew(db, pAlloc);
83489 if( pVal ){
83490 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
83491 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
83492 pVal->db = pParse->db;
83493 }
83494 }
83495 }else{
83496 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
83497 }
83498
83499 assert( pVal==0 || pVal->db==db );
83500 *ppVal = pVal;
83501 return rc;
83502}
83503
83504/*
83505** This function is used to allocate and populate UnpackedRecord
83506** structures intended to be compared against sample index keys stored
83507** in the sqlite_stat4 table.
83508**
83509** A single call to this function populates zero or more fields of the
83510** record starting with field iVal (fields are numbered from left to
83511** right starting with 0). A single field is populated if:
83512**
83513** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
83514**
83515** * The expression is a bound variable, and this is a reprepare, or
83516**
83517** * The sqlite3ValueFromExpr() function is able to extract a value
83518** from the expression (i.e. the expression is a literal value).
83519**
83520** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
83521** vector components that match either of the two latter criteria listed
83522** above.
83523**
83524** Before any value is appended to the record, the affinity of the
83525** corresponding column within index pIdx is applied to it. Before
83526** this function returns, output parameter *pnExtract is set to the
83527** number of values appended to the record.
83528**
83529** When this function is called, *ppRec must either point to an object
83530** allocated by an earlier call to this function, or must be NULL. If it
83531** is NULL and a value can be successfully extracted, a new UnpackedRecord
83532** is allocated (and *ppRec set to point to it) before returning.
83533**
83534** Unless an error is encountered, SQLITE_OK is returned. It is not an
83535** error if a value cannot be extracted from pExpr. If an error does
83536** occur, an SQLite error code is returned.
83537*/
83538SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
83539 Parse *pParse, /* Parse context */
83540 Index *pIdx, /* Index being probed */
83541 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
83542 Expr *pExpr, /* The expression to extract a value from */
83543 int nElem, /* Maximum number of values to append */
83544 int iVal, /* Array element to populate */
83545 int *pnExtract /* OUT: Values appended to the record */
83546){
83547 int rc = SQLITE_OK;
83548 int nExtract = 0;
83549
83550 if( pExpr==0 || pExpr->op!=TK_SELECT ){
83551 int i;
83552 struct ValueNewStat4Ctx alloc;
83553
83554 alloc.pParse = pParse;
83555 alloc.pIdx = pIdx;
83556 alloc.ppRec = ppRec;
83557
83558 for(i=0; i<nElem; i++){
83559 sqlite3_value *pVal = 0;
83560 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
83561 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
83562 alloc.iVal = iVal+i;
83563 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
83564 if( !pVal ) break;
83565 nExtract++;
83566 }
83567 }
83568
83569 *pnExtract = nExtract;
83570 return rc;
83571}
83572
83573/*
83574** Attempt to extract a value from expression pExpr using the methods
83575** as described for sqlite3Stat4ProbeSetValue() above.
83576**
83577** If successful, set *ppVal to point to a new value object and return
83578** SQLITE_OK. If no value can be extracted, but no other error occurs
83579** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
83580** does occur, return an SQLite error code. The final value of *ppVal
83581** is undefined in this case.
83582*/
83583SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
83584 Parse *pParse, /* Parse context */
83585 Expr *pExpr, /* The expression to extract a value from */
83586 u8 affinity, /* Affinity to use */
83587 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
83588){
83589 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
83590}
83591
83592/*
83593** Extract the iCol-th column from the nRec-byte record in pRec. Write
83594** the column value into *ppVal. If *ppVal is initially NULL then a new
83595** sqlite3_value object is allocated.
83596**
83597** If *ppVal is initially NULL then the caller is responsible for
83598** ensuring that the value written into *ppVal is eventually freed.
83599*/
83600SQLITE_PRIVATE int sqlite3Stat4Column(
83601 sqlite3 *db, /* Database handle */
83602 const void *pRec, /* Pointer to buffer containing record */
83603 int nRec, /* Size of buffer pRec in bytes */
83604 int iCol, /* Column to extract */
83605 sqlite3_value **ppVal /* OUT: Extracted value */
83606){
83607 u32 t = 0; /* a column type code */
83608 int nHdr; /* Size of the header in the record */
83609 int iHdr; /* Next unread header byte */
83610 int iField; /* Next unread data byte */
83611 int szField = 0; /* Size of the current data field */
83612 int i; /* Column index */
83613 u8 *a = (u8*)pRec; /* Typecast byte array */
83614 Mem *pMem = *ppVal; /* Write result into this Mem object */
83615
83616 assert( iCol>0 );
83617 iHdr = getVarint32(a, nHdr);
83618 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
83619 iField = nHdr;
83620 for(i=0; i<=iCol; i++){
83621 iHdr += getVarint32(&a[iHdr], t);
83622 testcase( iHdr==nHdr );
83623 testcase( iHdr==nHdr+1 );
83624 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
83625 szField = sqlite3VdbeSerialTypeLen(t);
83626 iField += szField;
83627 }
83628 testcase( iField==nRec );
83629 testcase( iField==nRec+1 );
83630 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
83631 if( pMem==0 ){
83632 pMem = *ppVal = sqlite3ValueNew(db);
83633 if( pMem==0 ) return SQLITE_NOMEM_BKPT;
83634 }
83635 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
83636 pMem->enc = ENC(db);
83637 return SQLITE_OK;
83638}
83639
83640/*
83641** Unless it is NULL, the argument must be an UnpackedRecord object returned
83642** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
83643** the object.
83644*/
83645SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
83646 if( pRec ){
83647 int i;
83648 int nCol = pRec->pKeyInfo->nAllField;
83649 Mem *aMem = pRec->aMem;
83650 sqlite3 *db = aMem[0].db;
83651 for(i=0; i<nCol; i++){
83652 sqlite3VdbeMemRelease(&aMem[i]);
83653 }
83654 sqlite3KeyInfoUnref(pRec->pKeyInfo);
83655 sqlite3DbFreeNN(db, pRec);
83656 }
83657}
83658#endif /* ifdef SQLITE_ENABLE_STAT4 */
83659
83660/*
83661** Change the string value of an sqlite3_value object
83662*/
83663SQLITE_PRIVATE void sqlite3ValueSetStr(
83664 sqlite3_value *v, /* Value to be set */
83665 int n, /* Length of string z */
83666 const void *z, /* Text of the new string */
83667 u8 enc, /* Encoding to use */
83668 void (*xDel)(void*) /* Destructor for the string */
83669){
83670 if( v ) sqlite3VdbeMemSetStr(pMem: (Mem *)v, z, n, enc, xDel);
83671}
83672
83673/*
83674** Free an sqlite3_value object
83675*/
83676SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
83677 if( !v ) return;
83678 sqlite3VdbeMemRelease(p: (Mem *)v);
83679 sqlite3DbFreeNN(db: ((Mem*)v)->db, p: v);
83680}
83681
83682/*
83683** The sqlite3ValueBytes() routine returns the number of bytes in the
83684** sqlite3_value object assuming that it uses the encoding "enc".
83685** The valueBytes() routine is a helper function.
83686*/
83687static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
83688 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
83689}
83690SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
83691 Mem *p = (Mem*)pVal;
83692 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
83693 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
83694 return p->n;
83695 }
83696 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
83697 return p->n;
83698 }
83699 if( (p->flags & MEM_Blob)!=0 ){
83700 if( p->flags & MEM_Zero ){
83701 return p->n + p->u.nZero;
83702 }else{
83703 return p->n;
83704 }
83705 }
83706 if( p->flags & MEM_Null ) return 0;
83707 return valueBytes(pVal, enc);
83708}
83709
83710/************** End of vdbemem.c *********************************************/
83711/************** Begin file vdbeaux.c *****************************************/
83712/*
83713** 2003 September 6
83714**
83715** The author disclaims copyright to this source code. In place of
83716** a legal notice, here is a blessing:
83717**
83718** May you do good and not evil.
83719** May you find forgiveness for yourself and forgive others.
83720** May you share freely, never taking more than you give.
83721**
83722*************************************************************************
83723** This file contains code used for creating, destroying, and populating
83724** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
83725*/
83726/* #include "sqliteInt.h" */
83727/* #include "vdbeInt.h" */
83728
83729/* Forward references */
83730static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
83731static void vdbeFreeOpArray(sqlite3 *, Op *, int);
83732
83733/*
83734** Create a new virtual database engine.
83735*/
83736SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
83737 sqlite3 *db = pParse->db;
83738 Vdbe *p;
83739 p = sqlite3DbMallocRawNN(db, n: sizeof(Vdbe) );
83740 if( p==0 ) return 0;
83741 memset(s: &p->aOp, c: 0, n: sizeof(Vdbe)-offsetof(Vdbe,aOp));
83742 p->db = db;
83743 if( db->pVdbe ){
83744 db->pVdbe->ppVPrev = &p->pVNext;
83745 }
83746 p->pVNext = db->pVdbe;
83747 p->ppVPrev = &db->pVdbe;
83748 db->pVdbe = p;
83749 assert( p->eVdbeState==VDBE_INIT_STATE );
83750 p->pParse = pParse;
83751 pParse->pVdbe = p;
83752 assert( pParse->aLabel==0 );
83753 assert( pParse->nLabel==0 );
83754 assert( p->nOpAlloc==0 );
83755 assert( pParse->szOpAlloc==0 );
83756 sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
83757 return p;
83758}
83759
83760/*
83761** Return the Parse object that owns a Vdbe object.
83762*/
83763SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe *p){
83764 return p->pParse;
83765}
83766
83767/*
83768** Change the error string stored in Vdbe.zErrMsg
83769*/
83770SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
83771 va_list ap;
83772 sqlite3DbFree(db: p->db, p: p->zErrMsg);
83773 va_start(ap, zFormat);
83774 p->zErrMsg = sqlite3VMPrintf(db: p->db, zFormat, ap);
83775 va_end(ap);
83776}
83777
83778/*
83779** Remember the SQL string for a prepared statement.
83780*/
83781SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
83782 if( p==0 ) return;
83783 p->prepFlags = prepFlags;
83784 if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
83785 p->expmask = 0;
83786 }
83787 assert( p->zSql==0 );
83788 p->zSql = sqlite3DbStrNDup(db: p->db, z, n);
83789}
83790
83791#ifdef SQLITE_ENABLE_NORMALIZE
83792/*
83793** Add a new element to the Vdbe->pDblStr list.
83794*/
83795SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
83796 if( p ){
83797 int n = sqlite3Strlen30(z);
83798 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
83799 sizeof(*pStr)+n+1-sizeof(pStr->z));
83800 if( pStr ){
83801 pStr->pNextStr = p->pDblStr;
83802 p->pDblStr = pStr;
83803 memcpy(pStr->z, z, n+1);
83804 }
83805 }
83806}
83807#endif
83808
83809#ifdef SQLITE_ENABLE_NORMALIZE
83810/*
83811** zId of length nId is a double-quoted identifier. Check to see if
83812** that identifier is really used as a string literal.
83813*/
83814SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(
83815 Vdbe *pVdbe, /* The prepared statement */
83816 const char *zId /* The double-quoted identifier, already dequoted */
83817){
83818 DblquoteStr *pStr;
83819 assert( zId!=0 );
83820 if( pVdbe->pDblStr==0 ) return 0;
83821 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
83822 if( strcmp(zId, pStr->z)==0 ) return 1;
83823 }
83824 return 0;
83825}
83826#endif
83827
83828/*
83829** Swap byte-code between two VDBE structures.
83830**
83831** This happens after pB was previously run and returned
83832** SQLITE_SCHEMA. The statement was then reprepared in pA.
83833** This routine transfers the new bytecode in pA over to pB
83834** so that pB can be run again. The old pB byte code is
83835** moved back to pA so that it will be cleaned up when pA is
83836** finalized.
83837*/
83838SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
83839 Vdbe tmp, *pTmp, **ppTmp;
83840 char *zTmp;
83841 assert( pA->db==pB->db );
83842 tmp = *pA;
83843 *pA = *pB;
83844 *pB = tmp;
83845 pTmp = pA->pVNext;
83846 pA->pVNext = pB->pVNext;
83847 pB->pVNext = pTmp;
83848 ppTmp = pA->ppVPrev;
83849 pA->ppVPrev = pB->ppVPrev;
83850 pB->ppVPrev = ppTmp;
83851 zTmp = pA->zSql;
83852 pA->zSql = pB->zSql;
83853 pB->zSql = zTmp;
83854#ifdef SQLITE_ENABLE_NORMALIZE
83855 zTmp = pA->zNormSql;
83856 pA->zNormSql = pB->zNormSql;
83857 pB->zNormSql = zTmp;
83858#endif
83859 pB->expmask = pA->expmask;
83860 pB->prepFlags = pA->prepFlags;
83861 memcpy(dest: pB->aCounter, src: pA->aCounter, n: sizeof(pB->aCounter));
83862 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
83863}
83864
83865/*
83866** Resize the Vdbe.aOp array so that it is at least nOp elements larger
83867** than its current size. nOp is guaranteed to be less than or equal
83868** to 1024/sizeof(Op).
83869**
83870** If an out-of-memory error occurs while resizing the array, return
83871** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
83872** unchanged (this is so that any opcodes already allocated can be
83873** correctly deallocated along with the rest of the Vdbe).
83874*/
83875static int growOpArray(Vdbe *v, int nOp){
83876 VdbeOp *pNew;
83877 Parse *p = v->pParse;
83878
83879 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
83880 ** more frequent reallocs and hence provide more opportunities for
83881 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
83882 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
83883 ** by the minimum* amount required until the size reaches 512. Normal
83884 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
83885 ** size of the op array or add 1KB of space, whichever is smaller. */
83886#ifdef SQLITE_TEST_REALLOC_STRESS
83887 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
83888 : (sqlite3_int64)v->nOpAlloc+nOp);
83889#else
83890 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
83891 : (sqlite3_int64)(1024/sizeof(Op)));
83892 UNUSED_PARAMETER(nOp);
83893#endif
83894
83895 /* Ensure that the size of a VDBE does not grow too large */
83896 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
83897 sqlite3OomFault(db: p->db);
83898 return SQLITE_NOMEM;
83899 }
83900
83901 assert( nOp<=(int)(1024/sizeof(Op)) );
83902 assert( nNew>=(v->nOpAlloc+nOp) );
83903 pNew = sqlite3DbRealloc(db: p->db, p: v->aOp, n: nNew*sizeof(Op));
83904 if( pNew ){
83905 p->szOpAlloc = sqlite3DbMallocSize(db: p->db, p: pNew);
83906 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
83907 v->aOp = pNew;
83908 }
83909 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
83910}
83911
83912#ifdef SQLITE_DEBUG
83913/* This routine is just a convenient place to set a breakpoint that will
83914** fire after each opcode is inserted and displayed using
83915** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and
83916** pOp are available to make the breakpoint conditional.
83917**
83918** Other useful labels for breakpoints include:
83919** test_trace_breakpoint(pc,pOp)
83920** sqlite3CorruptError(lineno)
83921** sqlite3MisuseError(lineno)
83922** sqlite3CantopenError(lineno)
83923*/
83924static void test_addop_breakpoint(int pc, Op *pOp){
83925 static int n = 0;
83926 (void)pc;
83927 (void)pOp;
83928 n++;
83929}
83930#endif
83931
83932/*
83933** Slow paths for sqlite3VdbeAddOp3() and sqlite3VdbeAddOp4Int() for the
83934** unusual case when we need to increase the size of the Vdbe.aOp[] array
83935** before adding the new opcode.
83936*/
83937static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
83938 assert( p->nOpAlloc<=p->nOp );
83939 if( growOpArray(v: p, nOp: 1) ) return 1;
83940 assert( p->nOpAlloc>p->nOp );
83941 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
83942}
83943static SQLITE_NOINLINE int addOp4IntSlow(
83944 Vdbe *p, /* Add the opcode to this VM */
83945 int op, /* The new opcode */
83946 int p1, /* The P1 operand */
83947 int p2, /* The P2 operand */
83948 int p3, /* The P3 operand */
83949 int p4 /* The P4 operand as an integer */
83950){
83951 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
83952 if( p->db->mallocFailed==0 ){
83953 VdbeOp *pOp = &p->aOp[addr];
83954 pOp->p4type = P4_INT32;
83955 pOp->p4.i = p4;
83956 }
83957 return addr;
83958}
83959
83960
83961/*
83962** Add a new instruction to the list of instructions current in the
83963** VDBE. Return the address of the new instruction.
83964**
83965** Parameters:
83966**
83967** p Pointer to the VDBE
83968**
83969** op The opcode for this instruction
83970**
83971** p1, p2, p3, p4 Operands
83972*/
83973SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
83974 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
83975}
83976SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
83977 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
83978}
83979SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
83980 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
83981}
83982SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
83983 int i;
83984 VdbeOp *pOp;
83985
83986 i = p->nOp;
83987 assert( p->eVdbeState==VDBE_INIT_STATE );
83988 assert( op>=0 && op<0xff );
83989 if( p->nOpAlloc<=i ){
83990 return growOp3(p, op, p1, p2, p3);
83991 }
83992 assert( p->aOp!=0 );
83993 p->nOp++;
83994 pOp = &p->aOp[i];
83995 assert( pOp!=0 );
83996 pOp->opcode = (u8)op;
83997 pOp->p5 = 0;
83998 pOp->p1 = p1;
83999 pOp->p2 = p2;
84000 pOp->p3 = p3;
84001 pOp->p4.p = 0;
84002 pOp->p4type = P4_NOTUSED;
84003
84004 /* Replicate this logic in sqlite3VdbeAddOp4Int()
84005 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
84006#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
84007 pOp->zComment = 0;
84008#endif
84009#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
84010 pOp->nExec = 0;
84011 pOp->nCycle = 0;
84012#endif
84013#ifdef SQLITE_DEBUG
84014 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84015 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
84016 test_addop_breakpoint(i, &p->aOp[i]);
84017 }
84018#endif
84019#ifdef SQLITE_VDBE_COVERAGE
84020 pOp->iSrcLine = 0;
84021#endif
84022 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84023 ** Replicate in sqlite3VdbeAddOp4Int() */
84024
84025 return i;
84026}
84027SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
84028 Vdbe *p, /* Add the opcode to this VM */
84029 int op, /* The new opcode */
84030 int p1, /* The P1 operand */
84031 int p2, /* The P2 operand */
84032 int p3, /* The P3 operand */
84033 int p4 /* The P4 operand as an integer */
84034){
84035 int i;
84036 VdbeOp *pOp;
84037
84038 i = p->nOp;
84039 if( p->nOpAlloc<=i ){
84040 return addOp4IntSlow(p, op, p1, p2, p3, p4);
84041 }
84042 p->nOp++;
84043 pOp = &p->aOp[i];
84044 assert( pOp!=0 );
84045 pOp->opcode = (u8)op;
84046 pOp->p5 = 0;
84047 pOp->p1 = p1;
84048 pOp->p2 = p2;
84049 pOp->p3 = p3;
84050 pOp->p4.i = p4;
84051 pOp->p4type = P4_INT32;
84052
84053 /* Replicate this logic in sqlite3VdbeAddOp3()
84054 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
84055#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
84056 pOp->zComment = 0;
84057#endif
84058#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
84059 pOp->nExec = 0;
84060 pOp->nCycle = 0;
84061#endif
84062#ifdef SQLITE_DEBUG
84063 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84064 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
84065 test_addop_breakpoint(i, &p->aOp[i]);
84066 }
84067#endif
84068#ifdef SQLITE_VDBE_COVERAGE
84069 pOp->iSrcLine = 0;
84070#endif
84071 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84072 ** Replicate in sqlite3VdbeAddOp3() */
84073
84074 return i;
84075}
84076
84077/* Generate code for an unconditional jump to instruction iDest
84078*/
84079SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
84080 return sqlite3VdbeAddOp3(p, OP_Goto, p1: 0, p2: iDest, p3: 0);
84081}
84082
84083/* Generate code to cause the string zStr to be loaded into
84084** register iDest
84085*/
84086SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
84087 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zP4: zStr, 0);
84088}
84089
84090/*
84091** Generate code that initializes multiple registers to string or integer
84092** constants. The registers begin with iDest and increase consecutively.
84093** One register is initialized for each characgter in zTypes[]. For each
84094** "s" character in zTypes[], the register is a string if the argument is
84095** not NULL, or OP_Null if the value is a null pointer. For each "i" character
84096** in zTypes[], the register is initialized to an integer.
84097**
84098** If the input string does not end with "X" then an OP_ResultRow instruction
84099** is generated for the values inserted.
84100*/
84101SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
84102 va_list ap;
84103 int i;
84104 char c;
84105 va_start(ap, zTypes);
84106 for(i=0; (c = zTypes[i])!=0; i++){
84107 if( c=='s' ){
84108 const char *z = va_arg(ap, const char*);
84109 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, zP4: z, 0);
84110 }else if( c=='i' ){
84111 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), p2: iDest+i);
84112 }else{
84113 goto skip_op_resultrow;
84114 }
84115 }
84116 sqlite3VdbeAddOp2(p, OP_ResultRow, p1: iDest, p2: i);
84117skip_op_resultrow:
84118 va_end(ap);
84119}
84120
84121/*
84122** Add an opcode that includes the p4 value as a pointer.
84123*/
84124SQLITE_PRIVATE int sqlite3VdbeAddOp4(
84125 Vdbe *p, /* Add the opcode to this VM */
84126 int op, /* The new opcode */
84127 int p1, /* The P1 operand */
84128 int p2, /* The P2 operand */
84129 int p3, /* The P3 operand */
84130 const char *zP4, /* The P4 operand */
84131 int p4type /* P4 operand type */
84132){
84133 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
84134 sqlite3VdbeChangeP4(p, addr, zP4, N: p4type);
84135 return addr;
84136}
84137
84138/*
84139** Add an OP_Function or OP_PureFunc opcode.
84140**
84141** The eCallCtx argument is information (typically taken from Expr.op2)
84142** that describes the calling context of the function. 0 means a general
84143** function call. NC_IsCheck means called by a check constraint,
84144** NC_IdxExpr means called as part of an index expression. NC_PartIdx
84145** means in the WHERE clause of a partial index. NC_GenCol means called
84146** while computing a generated column value. 0 is the usual case.
84147*/
84148SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
84149 Parse *pParse, /* Parsing context */
84150 int p1, /* Constant argument mask */
84151 int p2, /* First argument register */
84152 int p3, /* Register into which results are written */
84153 int nArg, /* Number of argument */
84154 const FuncDef *pFunc, /* The function to be invoked */
84155 int eCallCtx /* Calling context */
84156){
84157 Vdbe *v = pParse->pVdbe;
84158 int nByte;
84159 int addr;
84160 sqlite3_context *pCtx;
84161 assert( v );
84162 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
84163 pCtx = sqlite3DbMallocRawNN(db: pParse->db, n: nByte);
84164 if( pCtx==0 ){
84165 assert( pParse->db->mallocFailed );
84166 freeEphemeralFunction(db: pParse->db, pDef: (FuncDef*)pFunc);
84167 return 0;
84168 }
84169 pCtx->pOut = 0;
84170 pCtx->pFunc = (FuncDef*)pFunc;
84171 pCtx->pVdbe = 0;
84172 pCtx->isError = 0;
84173 pCtx->argc = nArg;
84174 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
84175 addr = sqlite3VdbeAddOp4(p: v, op: eCallCtx ? OP_PureFunc : OP_Function,
84176 p1, p2, p3, zP4: (char*)pCtx, P4_FUNCCTX);
84177 sqlite3VdbeChangeP5(v, P5: eCallCtx & NC_SelfRef);
84178 sqlite3MayAbort(pParse);
84179 return addr;
84180}
84181
84182/*
84183** Add an opcode that includes the p4 value with a P4_INT64 or
84184** P4_REAL type.
84185*/
84186SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
84187 Vdbe *p, /* Add the opcode to this VM */
84188 int op, /* The new opcode */
84189 int p1, /* The P1 operand */
84190 int p2, /* The P2 operand */
84191 int p3, /* The P3 operand */
84192 const u8 *zP4, /* The P4 operand */
84193 int p4type /* P4 operand type */
84194){
84195 char *p4copy = sqlite3DbMallocRawNN(db: sqlite3VdbeDb(p), n: 8);
84196 if( p4copy ) memcpy(dest: p4copy, src: zP4, n: 8);
84197 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, zP4: p4copy, p4type);
84198}
84199
84200#ifndef SQLITE_OMIT_EXPLAIN
84201/*
84202** Return the address of the current EXPLAIN QUERY PLAN baseline.
84203** 0 means "none".
84204*/
84205SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
84206 VdbeOp *pOp;
84207 if( pParse->addrExplain==0 ) return 0;
84208 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
84209 return pOp->p2;
84210}
84211
84212/*
84213** Set a debugger breakpoint on the following routine in order to
84214** monitor the EXPLAIN QUERY PLAN code generation.
84215*/
84216#if defined(SQLITE_DEBUG)
84217SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
84218 (void)z1;
84219 (void)z2;
84220}
84221#endif
84222
84223/*
84224** Add a new OP_Explain opcode.
84225**
84226** If the bPush flag is true, then make this opcode the parent for
84227** subsequent Explains until sqlite3VdbeExplainPop() is called.
84228*/
84229SQLITE_PRIVATE int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
84230 int addr = 0;
84231#if !defined(SQLITE_DEBUG)
84232 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
84233 ** But omit them (for performance) during production builds */
84234 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
84235#endif
84236 {
84237 char *zMsg;
84238 Vdbe *v;
84239 va_list ap;
84240 int iThis;
84241 va_start(ap, zFmt);
84242 zMsg = sqlite3VMPrintf(db: pParse->db, zFormat: zFmt, ap);
84243 va_end(ap);
84244 v = pParse->pVdbe;
84245 iThis = v->nOp;
84246 addr = sqlite3VdbeAddOp4(p: v, OP_Explain, p1: iThis, p2: pParse->addrExplain, p3: 0,
84247 zP4: zMsg, P4_DYNAMIC);
84248 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
84249 if( bPush){
84250 pParse->addrExplain = iThis;
84251 }
84252 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
84253 }
84254 return addr;
84255}
84256
84257/*
84258** Pop the EXPLAIN QUERY PLAN stack one level.
84259*/
84260SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
84261 sqlite3ExplainBreakpoint("POP", 0);
84262 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
84263}
84264#endif /* SQLITE_OMIT_EXPLAIN */
84265
84266/*
84267** Add an OP_ParseSchema opcode. This routine is broken out from
84268** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
84269** as having been used.
84270**
84271** The zWhere string must have been obtained from sqlite3_malloc().
84272** This routine will take ownership of the allocated memory.
84273*/
84274SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
84275 int j;
84276 sqlite3VdbeAddOp4(p, OP_ParseSchema, p1: iDb, p2: 0, p3: 0, zP4: zWhere, P4_DYNAMIC);
84277 sqlite3VdbeChangeP5(p, P5: p5);
84278 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
84279 sqlite3MayAbort(p->pParse);
84280}
84281
84282/* Insert the end of a co-routine
84283*/
84284SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
84285 sqlite3VdbeAddOp1(p: v, OP_EndCoroutine, p1: regYield);
84286
84287 /* Clear the temporary register cache, thereby ensuring that each
84288 ** co-routine has its own independent set of registers, because co-routines
84289 ** might expect their registers to be preserved across an OP_Yield, and
84290 ** that could cause problems if two or more co-routines are using the same
84291 ** temporary register.
84292 */
84293 v->pParse->nTempReg = 0;
84294 v->pParse->nRangeReg = 0;
84295}
84296
84297/*
84298** Create a new symbolic label for an instruction that has yet to be
84299** coded. The symbolic label is really just a negative number. The
84300** label can be used as the P2 value of an operation. Later, when
84301** the label is resolved to a specific address, the VDBE will scan
84302** through its operation list and change all values of P2 which match
84303** the label into the resolved address.
84304**
84305** The VDBE knows that a P2 value is a label because labels are
84306** always negative and P2 values are suppose to be non-negative.
84307** Hence, a negative P2 value is a label that has yet to be resolved.
84308** (Later:) This is only true for opcodes that have the OPFLG_JUMP
84309** property.
84310**
84311** Variable usage notes:
84312**
84313** Parse.aLabel[x] Stores the address that the x-th label resolves
84314** into. For testing (SQLITE_DEBUG), unresolved
84315** labels stores -1, but that is not required.
84316** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
84317** Parse.nLabel The *negative* of the number of labels that have
84318** been issued. The negative is stored because
84319** that gives a performance improvement over storing
84320** the equivalent positive value.
84321*/
84322SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse *pParse){
84323 return --pParse->nLabel;
84324}
84325
84326/*
84327** Resolve label "x" to be the address of the next instruction to
84328** be inserted. The parameter "x" must have been obtained from
84329** a prior call to sqlite3VdbeMakeLabel().
84330*/
84331static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
84332 int nNewSize = 10 - p->nLabel;
84333 p->aLabel = sqlite3DbReallocOrFree(db: p->db, p: p->aLabel,
84334 n: nNewSize*sizeof(p->aLabel[0]));
84335 if( p->aLabel==0 ){
84336 p->nLabelAlloc = 0;
84337 }else{
84338#ifdef SQLITE_DEBUG
84339 int i;
84340 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
84341#endif
84342 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
84343 sqlite3ProgressCheck(p);
84344 }
84345 p->nLabelAlloc = nNewSize;
84346 p->aLabel[j] = v->nOp;
84347 }
84348}
84349SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
84350 Parse *p = v->pParse;
84351 int j = ADDR(x);
84352 assert( v->eVdbeState==VDBE_INIT_STATE );
84353 assert( j<-p->nLabel );
84354 assert( j>=0 );
84355#ifdef SQLITE_DEBUG
84356 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84357 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
84358 }
84359#endif
84360 if( p->nLabelAlloc + p->nLabel < 0 ){
84361 resizeResolveLabel(p,v,j);
84362 }else{
84363 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
84364 p->aLabel[j] = v->nOp;
84365 }
84366}
84367
84368/*
84369** Mark the VDBE as one that can only be run one time.
84370*/
84371SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
84372 sqlite3VdbeAddOp2(p, OP_Expire, p1: 1, p2: 1);
84373}
84374
84375/*
84376** Mark the VDBE as one that can be run multiple times.
84377*/
84378SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
84379 int i;
84380 for(i=1; ALWAYS(i<p->nOp); i++){
84381 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
84382 p->aOp[1].opcode = OP_Noop;
84383 break;
84384 }
84385 }
84386}
84387
84388#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
84389
84390/*
84391** The following type and function are used to iterate through all opcodes
84392** in a Vdbe main program and each of the sub-programs (triggers) it may
84393** invoke directly or indirectly. It should be used as follows:
84394**
84395** Op *pOp;
84396** VdbeOpIter sIter;
84397**
84398** memset(&sIter, 0, sizeof(sIter));
84399** sIter.v = v; // v is of type Vdbe*
84400** while( (pOp = opIterNext(&sIter)) ){
84401** // Do something with pOp
84402** }
84403** sqlite3DbFree(v->db, sIter.apSub);
84404**
84405*/
84406typedef struct VdbeOpIter VdbeOpIter;
84407struct VdbeOpIter {
84408 Vdbe *v; /* Vdbe to iterate through the opcodes of */
84409 SubProgram **apSub; /* Array of subprograms */
84410 int nSub; /* Number of entries in apSub */
84411 int iAddr; /* Address of next instruction to return */
84412 int iSub; /* 0 = main program, 1 = first sub-program etc. */
84413};
84414static Op *opIterNext(VdbeOpIter *p){
84415 Vdbe *v = p->v;
84416 Op *pRet = 0;
84417 Op *aOp;
84418 int nOp;
84419
84420 if( p->iSub<=p->nSub ){
84421
84422 if( p->iSub==0 ){
84423 aOp = v->aOp;
84424 nOp = v->nOp;
84425 }else{
84426 aOp = p->apSub[p->iSub-1]->aOp;
84427 nOp = p->apSub[p->iSub-1]->nOp;
84428 }
84429 assert( p->iAddr<nOp );
84430
84431 pRet = &aOp[p->iAddr];
84432 p->iAddr++;
84433 if( p->iAddr==nOp ){
84434 p->iSub++;
84435 p->iAddr = 0;
84436 }
84437
84438 if( pRet->p4type==P4_SUBPROGRAM ){
84439 int nByte = (p->nSub+1)*sizeof(SubProgram*);
84440 int j;
84441 for(j=0; j<p->nSub; j++){
84442 if( p->apSub[j]==pRet->p4.pProgram ) break;
84443 }
84444 if( j==p->nSub ){
84445 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
84446 if( !p->apSub ){
84447 pRet = 0;
84448 }else{
84449 p->apSub[p->nSub++] = pRet->p4.pProgram;
84450 }
84451 }
84452 }
84453 }
84454
84455 return pRet;
84456}
84457
84458/*
84459** Check if the program stored in the VM associated with pParse may
84460** throw an ABORT exception (causing the statement, but not entire transaction
84461** to be rolled back). This condition is true if the main program or any
84462** sub-programs contains any of the following:
84463**
84464** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
84465** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
84466** * OP_Destroy
84467** * OP_VUpdate
84468** * OP_VCreate
84469** * OP_VRename
84470** * OP_FkCounter with P2==0 (immediate foreign key constraint)
84471** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
84472** (for CREATE TABLE AS SELECT ...)
84473**
84474** Then check that the value of Parse.mayAbort is true if an
84475** ABORT may be thrown, or false otherwise. Return true if it does
84476** match, or false otherwise. This function is intended to be used as
84477** part of an assert statement in the compiler. Similar to:
84478**
84479** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
84480*/
84481SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
84482 int hasAbort = 0;
84483 int hasFkCounter = 0;
84484 int hasCreateTable = 0;
84485 int hasCreateIndex = 0;
84486 int hasInitCoroutine = 0;
84487 Op *pOp;
84488 VdbeOpIter sIter;
84489
84490 if( v==0 ) return 0;
84491 memset(&sIter, 0, sizeof(sIter));
84492 sIter.v = v;
84493
84494 while( (pOp = opIterNext(&sIter))!=0 ){
84495 int opcode = pOp->opcode;
84496 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
84497 || opcode==OP_VDestroy
84498 || opcode==OP_VCreate
84499 || opcode==OP_ParseSchema
84500 || opcode==OP_Function || opcode==OP_PureFunc
84501 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
84502 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
84503 ){
84504 hasAbort = 1;
84505 break;
84506 }
84507 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
84508 if( mayAbort ){
84509 /* hasCreateIndex may also be set for some DELETE statements that use
84510 ** OP_Clear. So this routine may end up returning true in the case
84511 ** where a "DELETE FROM tbl" has a statement-journal but does not
84512 ** require one. This is not so bad - it is an inefficiency, not a bug. */
84513 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
84514 if( opcode==OP_Clear ) hasCreateIndex = 1;
84515 }
84516 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
84517#ifndef SQLITE_OMIT_FOREIGN_KEY
84518 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
84519 hasFkCounter = 1;
84520 }
84521#endif
84522 }
84523 sqlite3DbFree(v->db, sIter.apSub);
84524
84525 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
84526 ** If malloc failed, then the while() loop above may not have iterated
84527 ** through all opcodes and hasAbort may be set incorrectly. Return
84528 ** true for this case to prevent the assert() in the callers frame
84529 ** from failing. */
84530 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
84531 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
84532 );
84533}
84534#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
84535
84536#ifdef SQLITE_DEBUG
84537/*
84538** Increment the nWrite counter in the VDBE if the cursor is not an
84539** ephemeral cursor, or if the cursor argument is NULL.
84540*/
84541SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
84542 if( pC==0
84543 || (pC->eCurType!=CURTYPE_SORTER
84544 && pC->eCurType!=CURTYPE_PSEUDO
84545 && !pC->isEphemeral)
84546 ){
84547 p->nWrite++;
84548 }
84549}
84550#endif
84551
84552#ifdef SQLITE_DEBUG
84553/*
84554** Assert if an Abort at this point in time might result in a corrupt
84555** database.
84556*/
84557SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
84558 assert( p->nWrite==0 || p->usesStmtJournal );
84559}
84560#endif
84561
84562/*
84563** This routine is called after all opcodes have been inserted. It loops
84564** through all the opcodes and fixes up some details.
84565**
84566** (1) For each jump instruction with a negative P2 value (a label)
84567** resolve the P2 value to an actual address.
84568**
84569** (2) Compute the maximum number of arguments used by any SQL function
84570** and store that value in *pMaxFuncArgs.
84571**
84572** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
84573** indicate what the prepared statement actually does.
84574**
84575** (4) (discontinued)
84576**
84577** (5) Reclaim the memory allocated for storing labels.
84578**
84579** This routine will only function correctly if the mkopcodeh.tcl generator
84580** script numbers the opcodes correctly. Changes to this routine must be
84581** coordinated with changes to mkopcodeh.tcl.
84582*/
84583static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
84584 int nMaxArgs = *pMaxFuncArgs;
84585 Op *pOp;
84586 Parse *pParse = p->pParse;
84587 int *aLabel = pParse->aLabel;
84588
84589 assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */
84590 p->readOnly = 1;
84591 p->bIsReader = 0;
84592 pOp = &p->aOp[p->nOp-1];
84593 assert( p->aOp[0].opcode==OP_Init );
84594 while( 1 /* Loop terminates when it reaches the OP_Init opcode */ ){
84595 /* Only JUMP opcodes and the short list of special opcodes in the switch
84596 ** below need to be considered. The mkopcodeh.tcl generator script groups
84597 ** all these opcodes together near the front of the opcode list. Skip
84598 ** any opcode that does not need processing by virtual of the fact that
84599 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
84600 */
84601 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
84602 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
84603 ** cases from this switch! */
84604 switch( pOp->opcode ){
84605 case OP_Transaction: {
84606 if( pOp->p2!=0 ) p->readOnly = 0;
84607 /* no break */ deliberate_fall_through
84608 }
84609 case OP_AutoCommit:
84610 case OP_Savepoint: {
84611 p->bIsReader = 1;
84612 break;
84613 }
84614#ifndef SQLITE_OMIT_WAL
84615 case OP_Checkpoint:
84616#endif
84617 case OP_Vacuum:
84618 case OP_JournalMode: {
84619 p->readOnly = 0;
84620 p->bIsReader = 1;
84621 break;
84622 }
84623 case OP_Init: {
84624 assert( pOp->p2>=0 );
84625 goto resolve_p2_values_loop_exit;
84626 }
84627#ifndef SQLITE_OMIT_VIRTUALTABLE
84628 case OP_VUpdate: {
84629 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
84630 break;
84631 }
84632 case OP_VFilter: {
84633 int n;
84634 assert( (pOp - p->aOp) >= 3 );
84635 assert( pOp[-1].opcode==OP_Integer );
84636 n = pOp[-1].p1;
84637 if( n>nMaxArgs ) nMaxArgs = n;
84638 /* Fall through into the default case */
84639 /* no break */ deliberate_fall_through
84640 }
84641#endif
84642 default: {
84643 if( pOp->p2<0 ){
84644 /* The mkopcodeh.tcl script has so arranged things that the only
84645 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
84646 ** have non-negative values for P2. */
84647 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
84648 assert( ADDR(pOp->p2)<-pParse->nLabel );
84649 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
84650 pOp->p2 = aLabel[ADDR(pOp->p2)];
84651 }
84652 break;
84653 }
84654 }
84655 /* The mkopcodeh.tcl script has so arranged things that the only
84656 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
84657 ** have non-negative values for P2. */
84658 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
84659 }
84660 assert( pOp>p->aOp );
84661 pOp--;
84662 }
84663resolve_p2_values_loop_exit:
84664 if( aLabel ){
84665 sqlite3DbNNFreeNN(db: p->db, p: pParse->aLabel);
84666 pParse->aLabel = 0;
84667 }
84668 pParse->nLabel = 0;
84669 *pMaxFuncArgs = nMaxArgs;
84670 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
84671}
84672
84673#ifdef SQLITE_DEBUG
84674/*
84675** Check to see if a subroutine contains a jump to a location outside of
84676** the subroutine. If a jump outside the subroutine is detected, add code
84677** that will cause the program to halt with an error message.
84678**
84679** The subroutine consists of opcodes between iFirst and iLast. Jumps to
84680** locations within the subroutine are acceptable. iRetReg is a register
84681** that contains the return address. Jumps to outside the range of iFirst
84682** through iLast are also acceptable as long as the jump destination is
84683** an OP_Return to iReturnAddr.
84684**
84685** A jump to an unresolved label means that the jump destination will be
84686** beyond the current address. That is normally a jump to an early
84687** termination and is consider acceptable.
84688**
84689** This routine only runs during debug builds. The purpose is (of course)
84690** to detect invalid escapes out of a subroutine. The OP_Halt opcode
84691** is generated rather than an assert() or other error, so that ".eqp full"
84692** will still work to show the original bytecode, to aid in debugging.
84693*/
84694SQLITE_PRIVATE void sqlite3VdbeNoJumpsOutsideSubrtn(
84695 Vdbe *v, /* The byte-code program under construction */
84696 int iFirst, /* First opcode of the subroutine */
84697 int iLast, /* Last opcode of the subroutine */
84698 int iRetReg /* Subroutine return address register */
84699){
84700 VdbeOp *pOp;
84701 Parse *pParse;
84702 int i;
84703 sqlite3_str *pErr = 0;
84704 assert( v!=0 );
84705 pParse = v->pParse;
84706 assert( pParse!=0 );
84707 if( pParse->nErr ) return;
84708 assert( iLast>=iFirst );
84709 assert( iLast<v->nOp );
84710 pOp = &v->aOp[iFirst];
84711 for(i=iFirst; i<=iLast; i++, pOp++){
84712 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
84713 int iDest = pOp->p2; /* Jump destination */
84714 if( iDest==0 ) continue;
84715 if( pOp->opcode==OP_Gosub ) continue;
84716 if( iDest<0 ){
84717 int j = ADDR(iDest);
84718 assert( j>=0 );
84719 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
84720 continue;
84721 }
84722 iDest = pParse->aLabel[j];
84723 }
84724 if( iDest<iFirst || iDest>iLast ){
84725 int j = iDest;
84726 for(; j<v->nOp; j++){
84727 VdbeOp *pX = &v->aOp[j];
84728 if( pX->opcode==OP_Return ){
84729 if( pX->p1==iRetReg ) break;
84730 continue;
84731 }
84732 if( pX->opcode==OP_Noop ) continue;
84733 if( pX->opcode==OP_Explain ) continue;
84734 if( pErr==0 ){
84735 pErr = sqlite3_str_new(0);
84736 }else{
84737 sqlite3_str_appendchar(pErr, 1, '\n');
84738 }
84739 sqlite3_str_appendf(pErr,
84740 "Opcode at %d jumps to %d which is outside the "
84741 "subroutine at %d..%d",
84742 i, iDest, iFirst, iLast);
84743 break;
84744 }
84745 }
84746 }
84747 }
84748 if( pErr ){
84749 char *zErr = sqlite3_str_finish(pErr);
84750 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_INTERNAL, OE_Abort, 0, zErr, 0);
84751 sqlite3_free(zErr);
84752 sqlite3MayAbort(pParse);
84753 }
84754}
84755#endif /* SQLITE_DEBUG */
84756
84757/*
84758** Return the address of the next instruction to be inserted.
84759*/
84760SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
84761 assert( p->eVdbeState==VDBE_INIT_STATE );
84762 return p->nOp;
84763}
84764
84765/*
84766** Verify that at least N opcode slots are available in p without
84767** having to malloc for more space (except when compiled using
84768** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
84769** to verify that certain calls to sqlite3VdbeAddOpList() can never
84770** fail due to a OOM fault and hence that the return value from
84771** sqlite3VdbeAddOpList() will always be non-NULL.
84772*/
84773#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
84774SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
84775 assert( p->nOp + N <= p->nOpAlloc );
84776}
84777#endif
84778
84779/*
84780** Verify that the VM passed as the only argument does not contain
84781** an OP_ResultRow opcode. Fail an assert() if it does. This is used
84782** by code in pragma.c to ensure that the implementation of certain
84783** pragmas comports with the flags specified in the mkpragmatab.tcl
84784** script.
84785*/
84786#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
84787SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
84788 int i;
84789 for(i=0; i<p->nOp; i++){
84790 assert( p->aOp[i].opcode!=OP_ResultRow );
84791 }
84792}
84793#endif
84794
84795/*
84796** Generate code (a single OP_Abortable opcode) that will
84797** verify that the VDBE program can safely call Abort in the current
84798** context.
84799*/
84800#if defined(SQLITE_DEBUG)
84801SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
84802 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
84803}
84804#endif
84805
84806/*
84807** This function returns a pointer to the array of opcodes associated with
84808** the Vdbe passed as the first argument. It is the callers responsibility
84809** to arrange for the returned array to be eventually freed using the
84810** vdbeFreeOpArray() function.
84811**
84812** Before returning, *pnOp is set to the number of entries in the returned
84813** array. Also, *pnMaxArg is set to the larger of its current value and
84814** the number of entries in the Vdbe.apArg[] array required to execute the
84815** returned program.
84816*/
84817SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
84818 VdbeOp *aOp = p->aOp;
84819 assert( aOp && !p->db->mallocFailed );
84820
84821 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
84822 assert( DbMaskAllZero(p->btreeMask) );
84823
84824 resolveP2Values(p, pMaxFuncArgs: pnMaxArg);
84825 *pnOp = p->nOp;
84826 p->aOp = 0;
84827 return aOp;
84828}
84829
84830/*
84831** Add a whole list of operations to the operation stack. Return a
84832** pointer to the first operation inserted.
84833**
84834** Non-zero P2 arguments to jump instructions are automatically adjusted
84835** so that the jump target is relative to the first operation inserted.
84836*/
84837SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
84838 Vdbe *p, /* Add opcodes to the prepared statement */
84839 int nOp, /* Number of opcodes to add */
84840 VdbeOpList const *aOp, /* The opcodes to be added */
84841 int iLineno /* Source-file line number of first opcode */
84842){
84843 int i;
84844 VdbeOp *pOut, *pFirst;
84845 assert( nOp>0 );
84846 assert( p->eVdbeState==VDBE_INIT_STATE );
84847 if( p->nOp + nOp > p->nOpAlloc && growOpArray(v: p, nOp) ){
84848 return 0;
84849 }
84850 pFirst = pOut = &p->aOp[p->nOp];
84851 for(i=0; i<nOp; i++, aOp++, pOut++){
84852 pOut->opcode = aOp->opcode;
84853 pOut->p1 = aOp->p1;
84854 pOut->p2 = aOp->p2;
84855 assert( aOp->p2>=0 );
84856 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
84857 pOut->p2 += p->nOp;
84858 }
84859 pOut->p3 = aOp->p3;
84860 pOut->p4type = P4_NOTUSED;
84861 pOut->p4.p = 0;
84862 pOut->p5 = 0;
84863#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
84864 pOut->zComment = 0;
84865#endif
84866#ifdef SQLITE_VDBE_COVERAGE
84867 pOut->iSrcLine = iLineno+i;
84868#else
84869 (void)iLineno;
84870#endif
84871#ifdef SQLITE_DEBUG
84872 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84873 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
84874 }
84875#endif
84876 }
84877 p->nOp += nOp;
84878 return pFirst;
84879}
84880
84881#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
84882/*
84883** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
84884*/
84885SQLITE_PRIVATE void sqlite3VdbeScanStatus(
84886 Vdbe *p, /* VM to add scanstatus() to */
84887 int addrExplain, /* Address of OP_Explain (or 0) */
84888 int addrLoop, /* Address of loop counter */
84889 int addrVisit, /* Address of rows visited counter */
84890 LogEst nEst, /* Estimated number of output rows */
84891 const char *zName /* Name of table or index being scanned */
84892){
84893 if( IS_STMT_SCANSTATUS(p->db) ){
84894 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
84895 ScanStatus *aNew;
84896 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
84897 if( aNew ){
84898 ScanStatus *pNew = &aNew[p->nScan++];
84899 memset(pNew, 0, sizeof(ScanStatus));
84900 pNew->addrExplain = addrExplain;
84901 pNew->addrLoop = addrLoop;
84902 pNew->addrVisit = addrVisit;
84903 pNew->nEst = nEst;
84904 pNew->zName = sqlite3DbStrDup(p->db, zName);
84905 p->aScan = aNew;
84906 }
84907 }
84908}
84909
84910/*
84911** Add the range of instructions from addrStart to addrEnd (inclusive) to
84912** the set of those corresponding to the sqlite3_stmt_scanstatus() counters
84913** associated with the OP_Explain instruction at addrExplain. The
84914** sum of the sqlite3Hwtime() values for each of these instructions
84915** will be returned for SQLITE_SCANSTAT_NCYCLE requests.
84916*/
84917SQLITE_PRIVATE void sqlite3VdbeScanStatusRange(
84918 Vdbe *p,
84919 int addrExplain,
84920 int addrStart,
84921 int addrEnd
84922){
84923 if( IS_STMT_SCANSTATUS(p->db) ){
84924 ScanStatus *pScan = 0;
84925 int ii;
84926 for(ii=p->nScan-1; ii>=0; ii--){
84927 pScan = &p->aScan[ii];
84928 if( pScan->addrExplain==addrExplain ) break;
84929 pScan = 0;
84930 }
84931 if( pScan ){
84932 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
84933 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
84934 if( pScan->aAddrRange[ii]==0 ){
84935 pScan->aAddrRange[ii] = addrStart;
84936 pScan->aAddrRange[ii+1] = addrEnd;
84937 break;
84938 }
84939 }
84940 }
84941 }
84942}
84943
84944/*
84945** Set the addresses for the SQLITE_SCANSTAT_NLOOP and SQLITE_SCANSTAT_NROW
84946** counters for the query element associated with the OP_Explain at
84947** addrExplain.
84948*/
84949SQLITE_PRIVATE void sqlite3VdbeScanStatusCounters(
84950 Vdbe *p,
84951 int addrExplain,
84952 int addrLoop,
84953 int addrVisit
84954){
84955 if( IS_STMT_SCANSTATUS(p->db) ){
84956 ScanStatus *pScan = 0;
84957 int ii;
84958 for(ii=p->nScan-1; ii>=0; ii--){
84959 pScan = &p->aScan[ii];
84960 if( pScan->addrExplain==addrExplain ) break;
84961 pScan = 0;
84962 }
84963 if( pScan ){
84964 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
84965 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
84966 }
84967 }
84968}
84969#endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */
84970
84971
84972/*
84973** Change the value of the opcode, or P1, P2, P3, or P5 operands
84974** for a specific instruction.
84975*/
84976SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
84977 assert( addr>=0 );
84978 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
84979}
84980SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
84981 assert( addr>=0 );
84982 sqlite3VdbeGetOp(p,addr)->p1 = val;
84983}
84984SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
84985 assert( addr>=0 || p->db->mallocFailed );
84986 sqlite3VdbeGetOp(p,addr)->p2 = val;
84987}
84988SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
84989 assert( addr>=0 );
84990 sqlite3VdbeGetOp(p,addr)->p3 = val;
84991}
84992SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
84993 assert( p->nOp>0 || p->db->mallocFailed );
84994 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
84995}
84996
84997/*
84998** If the previous opcode is an OP_Column that delivers results
84999** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
85000** opcode.
85001*/
85002SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
85003 VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
85004 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
85005 pOp->p5 |= OPFLAG_TYPEOFARG;
85006 }
85007}
85008
85009/*
85010** Change the P2 operand of instruction addr so that it points to
85011** the address of the next instruction to be coded.
85012*/
85013SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
85014 sqlite3VdbeChangeP2(p, addr, val: p->nOp);
85015}
85016
85017/*
85018** Change the P2 operand of the jump instruction at addr so that
85019** the jump lands on the next opcode. Or if the jump instruction was
85020** the previous opcode (and is thus a no-op) then simply back up
85021** the next instruction counter by one slot so that the jump is
85022** overwritten by the next inserted opcode.
85023**
85024** This routine is an optimization of sqlite3VdbeJumpHere() that
85025** strives to omit useless byte-code like this:
85026**
85027** 7 Once 0 8 0
85028** 8 ...
85029*/
85030SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
85031 if( addr==p->nOp-1 ){
85032 assert( p->aOp[addr].opcode==OP_Once
85033 || p->aOp[addr].opcode==OP_If
85034 || p->aOp[addr].opcode==OP_FkIfZero );
85035 assert( p->aOp[addr].p4type==0 );
85036#ifdef SQLITE_VDBE_COVERAGE
85037 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
85038#endif
85039 p->nOp--;
85040 }else{
85041 sqlite3VdbeChangeP2(p, addr, val: p->nOp);
85042 }
85043}
85044
85045
85046/*
85047** If the input FuncDef structure is ephemeral, then free it. If
85048** the FuncDef is not ephemeral, then do nothing.
85049*/
85050static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
85051 assert( db!=0 );
85052 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
85053 sqlite3DbNNFreeNN(db, p: pDef);
85054 }
85055}
85056
85057/*
85058** Delete a P4 value if necessary.
85059*/
85060static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
85061 if( p->szMalloc ) sqlite3DbFree(db, p: p->zMalloc);
85062 sqlite3DbNNFreeNN(db, p);
85063}
85064static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
85065 assert( db!=0 );
85066 freeEphemeralFunction(db, pDef: p->pFunc);
85067 sqlite3DbNNFreeNN(db, p);
85068}
85069static void freeP4(sqlite3 *db, int p4type, void *p4){
85070 assert( db );
85071 switch( p4type ){
85072 case P4_FUNCCTX: {
85073 freeP4FuncCtx(db, p: (sqlite3_context*)p4);
85074 break;
85075 }
85076 case P4_REAL:
85077 case P4_INT64:
85078 case P4_DYNAMIC:
85079 case P4_INTARRAY: {
85080 if( p4 ) sqlite3DbNNFreeNN(db, p: p4);
85081 break;
85082 }
85083 case P4_KEYINFO: {
85084 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
85085 break;
85086 }
85087#ifdef SQLITE_ENABLE_CURSOR_HINTS
85088 case P4_EXPR: {
85089 sqlite3ExprDelete(db, (Expr*)p4);
85090 break;
85091 }
85092#endif
85093 case P4_FUNCDEF: {
85094 freeEphemeralFunction(db, pDef: (FuncDef*)p4);
85095 break;
85096 }
85097 case P4_MEM: {
85098 if( db->pnBytesFreed==0 ){
85099 sqlite3ValueFree(v: (sqlite3_value*)p4);
85100 }else{
85101 freeP4Mem(db, p: (Mem*)p4);
85102 }
85103 break;
85104 }
85105 case P4_VTAB : {
85106 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
85107 break;
85108 }
85109 }
85110}
85111
85112/*
85113** Free the space allocated for aOp and any p4 values allocated for the
85114** opcodes contained within. If aOp is not NULL it is assumed to contain
85115** nOp entries.
85116*/
85117static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
85118 assert( nOp>=0 );
85119 assert( db!=0 );
85120 if( aOp ){
85121 Op *pOp = &aOp[nOp-1];
85122 while(1){ /* Exit via break */
85123 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, p4type: pOp->p4type, p4: pOp->p4.p);
85124#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
85125 sqlite3DbFree(db, pOp->zComment);
85126#endif
85127 if( pOp==aOp ) break;
85128 pOp--;
85129 }
85130 sqlite3DbNNFreeNN(db, p: aOp);
85131 }
85132}
85133
85134/*
85135** Link the SubProgram object passed as the second argument into the linked
85136** list at Vdbe.pSubProgram. This list is used to delete all sub-program
85137** objects when the VM is no longer required.
85138*/
85139SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
85140 p->pNext = pVdbe->pProgram;
85141 pVdbe->pProgram = p;
85142}
85143
85144/*
85145** Return true if the given Vdbe has any SubPrograms.
85146*/
85147SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
85148 return pVdbe->pProgram!=0;
85149}
85150
85151/*
85152** Change the opcode at addr into OP_Noop
85153*/
85154SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
85155 VdbeOp *pOp;
85156 if( p->db->mallocFailed ) return 0;
85157 assert( addr>=0 && addr<p->nOp );
85158 pOp = &p->aOp[addr];
85159 freeP4(db: p->db, p4type: pOp->p4type, p4: pOp->p4.p);
85160 pOp->p4type = P4_NOTUSED;
85161 pOp->p4.z = 0;
85162 pOp->opcode = OP_Noop;
85163 return 1;
85164}
85165
85166/*
85167** If the last opcode is "op" and it is not a jump destination,
85168** then remove it. Return true if and only if an opcode was removed.
85169*/
85170SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
85171 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
85172 return sqlite3VdbeChangeToNoop(p, addr: p->nOp-1);
85173 }else{
85174 return 0;
85175 }
85176}
85177
85178#ifdef SQLITE_DEBUG
85179/*
85180** Generate an OP_ReleaseReg opcode to indicate that a range of
85181** registers, except any identified by mask, are no longer in use.
85182*/
85183SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(
85184 Parse *pParse, /* Parsing context */
85185 int iFirst, /* Index of first register to be released */
85186 int N, /* Number of registers to release */
85187 u32 mask, /* Mask of registers to NOT release */
85188 int bUndefine /* If true, mark registers as undefined */
85189){
85190 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
85191 assert( pParse->pVdbe );
85192 assert( iFirst>=1 );
85193 assert( iFirst+N-1<=pParse->nMem );
85194 if( N<=31 && mask!=0 ){
85195 while( N>0 && (mask&1)!=0 ){
85196 mask >>= 1;
85197 iFirst++;
85198 N--;
85199 }
85200 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
85201 mask &= ~MASKBIT32(N-1);
85202 N--;
85203 }
85204 }
85205 if( N>0 ){
85206 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
85207 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
85208 }
85209}
85210#endif /* SQLITE_DEBUG */
85211
85212/*
85213** Change the value of the P4 operand for a specific instruction.
85214** This routine is useful when a large program is loaded from a
85215** static array using sqlite3VdbeAddOpList but we want to make a
85216** few minor changes to the program.
85217**
85218** If n>=0 then the P4 operand is dynamic, meaning that a copy of
85219** the string is made into memory obtained from sqlite3_malloc().
85220** A value of n==0 means copy bytes of zP4 up to and including the
85221** first null byte. If n>0 then copy n+1 bytes of zP4.
85222**
85223** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
85224** to a string or structure that is guaranteed to exist for the lifetime of
85225** the Vdbe. In these cases we can just copy the pointer.
85226**
85227** If addr<0 then change P4 on the most recently inserted instruction.
85228*/
85229static void SQLITE_NOINLINE vdbeChangeP4Full(
85230 Vdbe *p,
85231 Op *pOp,
85232 const char *zP4,
85233 int n
85234){
85235 if( pOp->p4type ){
85236 freeP4(db: p->db, p4type: pOp->p4type, p4: pOp->p4.p);
85237 pOp->p4type = 0;
85238 pOp->p4.p = 0;
85239 }
85240 if( n<0 ){
85241 sqlite3VdbeChangeP4(p, addr: (int)(pOp - p->aOp), zP4, N: n);
85242 }else{
85243 if( n==0 ) n = sqlite3Strlen30(z: zP4);
85244 pOp->p4.z = sqlite3DbStrNDup(db: p->db, z: zP4, n);
85245 pOp->p4type = P4_DYNAMIC;
85246 }
85247}
85248SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
85249 Op *pOp;
85250 sqlite3 *db;
85251 assert( p!=0 );
85252 db = p->db;
85253 assert( p->eVdbeState==VDBE_INIT_STATE );
85254 assert( p->aOp!=0 || db->mallocFailed );
85255 if( db->mallocFailed ){
85256 if( n!=P4_VTAB ) freeP4(db, p4type: n, p4: (void*)*(char**)&zP4);
85257 return;
85258 }
85259 assert( p->nOp>0 );
85260 assert( addr<p->nOp );
85261 if( addr<0 ){
85262 addr = p->nOp - 1;
85263 }
85264 pOp = &p->aOp[addr];
85265 if( n>=0 || pOp->p4type ){
85266 vdbeChangeP4Full(p, pOp, zP4, n);
85267 return;
85268 }
85269 if( n==P4_INT32 ){
85270 /* Note: this cast is safe, because the origin data point was an int
85271 ** that was cast to a (const char *). */
85272 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
85273 pOp->p4type = P4_INT32;
85274 }else if( zP4!=0 ){
85275 assert( n<0 );
85276 pOp->p4.p = (void*)zP4;
85277 pOp->p4type = (signed char)n;
85278 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
85279 }
85280}
85281
85282/*
85283** Change the P4 operand of the most recently coded instruction
85284** to the value defined by the arguments. This is a high-speed
85285** version of sqlite3VdbeChangeP4().
85286**
85287** The P4 operand must not have been previously defined. And the new
85288** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
85289** those cases.
85290*/
85291SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
85292 VdbeOp *pOp;
85293 assert( n!=P4_INT32 && n!=P4_VTAB );
85294 assert( n<=0 );
85295 if( p->db->mallocFailed ){
85296 freeP4(db: p->db, p4type: n, p4: pP4);
85297 }else{
85298 assert( pP4!=0 || n==P4_DYNAMIC );
85299 assert( p->nOp>0 );
85300 pOp = &p->aOp[p->nOp-1];
85301 assert( pOp->p4type==P4_NOTUSED );
85302 pOp->p4type = n;
85303 pOp->p4.p = pP4;
85304 }
85305}
85306
85307/*
85308** Set the P4 on the most recently added opcode to the KeyInfo for the
85309** index given.
85310*/
85311SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
85312 Vdbe *v = pParse->pVdbe;
85313 KeyInfo *pKeyInfo;
85314 assert( v!=0 );
85315 assert( pIdx!=0 );
85316 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
85317 if( pKeyInfo ) sqlite3VdbeAppendP4(p: v, pP4: pKeyInfo, P4_KEYINFO);
85318}
85319
85320#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
85321/*
85322** Change the comment on the most recently coded instruction. Or
85323** insert a No-op and add the comment to that new instruction. This
85324** makes the code easier to read during debugging. None of this happens
85325** in a production build.
85326*/
85327static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
85328 assert( p->nOp>0 || p->aOp==0 );
85329 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
85330 if( p->nOp ){
85331 assert( p->aOp );
85332 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
85333 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
85334 }
85335}
85336SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
85337 va_list ap;
85338 if( p ){
85339 va_start(ap, zFormat);
85340 vdbeVComment(p, zFormat, ap);
85341 va_end(ap);
85342 }
85343}
85344SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
85345 va_list ap;
85346 if( p ){
85347 sqlite3VdbeAddOp0(p, OP_Noop);
85348 va_start(ap, zFormat);
85349 vdbeVComment(p, zFormat, ap);
85350 va_end(ap);
85351 }
85352}
85353#endif /* NDEBUG */
85354
85355#ifdef SQLITE_VDBE_COVERAGE
85356/*
85357** Set the value if the iSrcLine field for the previously coded instruction.
85358*/
85359SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
85360 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
85361}
85362#endif /* SQLITE_VDBE_COVERAGE */
85363
85364/*
85365** Return the opcode for a given address. The address must be non-negative.
85366** See sqlite3VdbeGetLastOp() to get the most recently added opcode.
85367**
85368** If a memory allocation error has occurred prior to the calling of this
85369** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
85370** is readable but not writable, though it is cast to a writable value.
85371** The return of a dummy opcode allows the call to continue functioning
85372** after an OOM fault without having to check to see if the return from
85373** this routine is a valid pointer. But because the dummy.opcode is 0,
85374** dummy will never be written to. This is verified by code inspection and
85375** by running with Valgrind.
85376*/
85377SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
85378 /* C89 specifies that the constant "dummy" will be initialized to all
85379 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
85380 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
85381 assert( p->eVdbeState==VDBE_INIT_STATE );
85382 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
85383 if( p->db->mallocFailed ){
85384 return (VdbeOp*)&dummy;
85385 }else{
85386 return &p->aOp[addr];
85387 }
85388}
85389
85390/* Return the most recently added opcode
85391*/
85392SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetLastOp(Vdbe *p){
85393 return sqlite3VdbeGetOp(p, addr: p->nOp - 1);
85394}
85395
85396#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
85397/*
85398** Return an integer value for one of the parameters to the opcode pOp
85399** determined by character c.
85400*/
85401static int translateP(char c, const Op *pOp){
85402 if( c=='1' ) return pOp->p1;
85403 if( c=='2' ) return pOp->p2;
85404 if( c=='3' ) return pOp->p3;
85405 if( c=='4' ) return pOp->p4.i;
85406 return pOp->p5;
85407}
85408
85409/*
85410** Compute a string for the "comment" field of a VDBE opcode listing.
85411**
85412** The Synopsis: field in comments in the vdbe.c source file gets converted
85413** to an extra string that is appended to the sqlite3OpcodeName(). In the
85414** absence of other comments, this synopsis becomes the comment on the opcode.
85415** Some translation occurs:
85416**
85417** "PX" -> "r[X]"
85418** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
85419** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
85420** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
85421*/
85422SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(
85423 sqlite3 *db, /* Optional - Oom error reporting only */
85424 const Op *pOp, /* The opcode to be commented */
85425 const char *zP4 /* Previously obtained value for P4 */
85426){
85427 const char *zOpName;
85428 const char *zSynopsis;
85429 int nOpName;
85430 int ii;
85431 char zAlt[50];
85432 StrAccum x;
85433
85434 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
85435 zOpName = sqlite3OpcodeName(pOp->opcode);
85436 nOpName = sqlite3Strlen30(zOpName);
85437 if( zOpName[nOpName+1] ){
85438 int seenCom = 0;
85439 char c;
85440 zSynopsis = zOpName + nOpName + 1;
85441 if( strncmp(zSynopsis,"IF ",3)==0 ){
85442 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
85443 zSynopsis = zAlt;
85444 }
85445 for(ii=0; (c = zSynopsis[ii])!=0; ii++){
85446 if( c=='P' ){
85447 c = zSynopsis[++ii];
85448 if( c=='4' ){
85449 sqlite3_str_appendall(&x, zP4);
85450 }else if( c=='X' ){
85451 if( pOp->zComment && pOp->zComment[0] ){
85452 sqlite3_str_appendall(&x, pOp->zComment);
85453 seenCom = 1;
85454 break;
85455 }
85456 }else{
85457 int v1 = translateP(c, pOp);
85458 int v2;
85459 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
85460 ii += 3;
85461 v2 = translateP(zSynopsis[ii], pOp);
85462 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
85463 ii += 2;
85464 v2++;
85465 }
85466 if( v2<2 ){
85467 sqlite3_str_appendf(&x, "%d", v1);
85468 }else{
85469 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
85470 }
85471 }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
85472 sqlite3_context *pCtx = pOp->p4.pCtx;
85473 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
85474 sqlite3_str_appendf(&x, "%d", v1);
85475 }else if( pCtx->argc>1 ){
85476 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
85477 }else if( x.accError==0 ){
85478 assert( x.nChar>2 );
85479 x.nChar -= 2;
85480 ii++;
85481 }
85482 ii += 3;
85483 }else{
85484 sqlite3_str_appendf(&x, "%d", v1);
85485 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
85486 ii += 4;
85487 }
85488 }
85489 }
85490 }else{
85491 sqlite3_str_appendchar(&x, 1, c);
85492 }
85493 }
85494 if( !seenCom && pOp->zComment ){
85495 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
85496 }
85497 }else if( pOp->zComment ){
85498 sqlite3_str_appendall(&x, pOp->zComment);
85499 }
85500 if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){
85501 sqlite3OomFault(db);
85502 }
85503 return sqlite3StrAccumFinish(&x);
85504}
85505#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
85506
85507#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
85508/*
85509** Translate the P4.pExpr value for an OP_CursorHint opcode into text
85510** that can be displayed in the P4 column of EXPLAIN output.
85511*/
85512static void displayP4Expr(StrAccum *p, Expr *pExpr){
85513 const char *zOp = 0;
85514 switch( pExpr->op ){
85515 case TK_STRING:
85516 assert( !ExprHasProperty(pExpr, EP_IntValue) );
85517 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
85518 break;
85519 case TK_INTEGER:
85520 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
85521 break;
85522 case TK_NULL:
85523 sqlite3_str_appendf(p, "NULL");
85524 break;
85525 case TK_REGISTER: {
85526 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
85527 break;
85528 }
85529 case TK_COLUMN: {
85530 if( pExpr->iColumn<0 ){
85531 sqlite3_str_appendf(p, "rowid");
85532 }else{
85533 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
85534 }
85535 break;
85536 }
85537 case TK_LT: zOp = "LT"; break;
85538 case TK_LE: zOp = "LE"; break;
85539 case TK_GT: zOp = "GT"; break;
85540 case TK_GE: zOp = "GE"; break;
85541 case TK_NE: zOp = "NE"; break;
85542 case TK_EQ: zOp = "EQ"; break;
85543 case TK_IS: zOp = "IS"; break;
85544 case TK_ISNOT: zOp = "ISNOT"; break;
85545 case TK_AND: zOp = "AND"; break;
85546 case TK_OR: zOp = "OR"; break;
85547 case TK_PLUS: zOp = "ADD"; break;
85548 case TK_STAR: zOp = "MUL"; break;
85549 case TK_MINUS: zOp = "SUB"; break;
85550 case TK_REM: zOp = "REM"; break;
85551 case TK_BITAND: zOp = "BITAND"; break;
85552 case TK_BITOR: zOp = "BITOR"; break;
85553 case TK_SLASH: zOp = "DIV"; break;
85554 case TK_LSHIFT: zOp = "LSHIFT"; break;
85555 case TK_RSHIFT: zOp = "RSHIFT"; break;
85556 case TK_CONCAT: zOp = "CONCAT"; break;
85557 case TK_UMINUS: zOp = "MINUS"; break;
85558 case TK_UPLUS: zOp = "PLUS"; break;
85559 case TK_BITNOT: zOp = "BITNOT"; break;
85560 case TK_NOT: zOp = "NOT"; break;
85561 case TK_ISNULL: zOp = "ISNULL"; break;
85562 case TK_NOTNULL: zOp = "NOTNULL"; break;
85563
85564 default:
85565 sqlite3_str_appendf(p, "%s", "expr");
85566 break;
85567 }
85568
85569 if( zOp ){
85570 sqlite3_str_appendf(p, "%s(", zOp);
85571 displayP4Expr(p, pExpr->pLeft);
85572 if( pExpr->pRight ){
85573 sqlite3_str_append(p, ",", 1);
85574 displayP4Expr(p, pExpr->pRight);
85575 }
85576 sqlite3_str_append(p, ")", 1);
85577 }
85578}
85579#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
85580
85581
85582#if VDBE_DISPLAY_P4
85583/*
85584** Compute a string that describes the P4 parameter for an opcode.
85585** Use zTemp for any required temporary buffer space.
85586*/
85587SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
85588 char *zP4 = 0;
85589 StrAccum x;
85590
85591 sqlite3StrAccumInit(p: &x, db: 0, zBase: 0, n: 0, SQLITE_MAX_LENGTH);
85592 switch( pOp->p4type ){
85593 case P4_KEYINFO: {
85594 int j;
85595 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
85596 assert( pKeyInfo->aSortFlags!=0 );
85597 sqlite3_str_appendf(p: &x, zFormat: "k(%d", pKeyInfo->nKeyField);
85598 for(j=0; j<pKeyInfo->nKeyField; j++){
85599 CollSeq *pColl = pKeyInfo->aColl[j];
85600 const char *zColl = pColl ? pColl->zName : "";
85601 if( strcmp(s1: zColl, s2: "BINARY")==0 ) zColl = "B";
85602 sqlite3_str_appendf(p: &x, zFormat: ",%s%s%s",
85603 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
85604 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
85605 zColl);
85606 }
85607 sqlite3_str_append(p: &x, z: ")", N: 1);
85608 break;
85609 }
85610#ifdef SQLITE_ENABLE_CURSOR_HINTS
85611 case P4_EXPR: {
85612 displayP4Expr(&x, pOp->p4.pExpr);
85613 break;
85614 }
85615#endif
85616 case P4_COLLSEQ: {
85617 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
85618 CollSeq *pColl = pOp->p4.pColl;
85619 assert( pColl->enc<4 );
85620 sqlite3_str_appendf(p: &x, zFormat: "%.18s-%s", pColl->zName,
85621 encnames[pColl->enc]);
85622 break;
85623 }
85624 case P4_FUNCDEF: {
85625 FuncDef *pDef = pOp->p4.pFunc;
85626 sqlite3_str_appendf(p: &x, zFormat: "%s(%d)", pDef->zName, pDef->nArg);
85627 break;
85628 }
85629 case P4_FUNCCTX: {
85630 FuncDef *pDef = pOp->p4.pCtx->pFunc;
85631 sqlite3_str_appendf(p: &x, zFormat: "%s(%d)", pDef->zName, pDef->nArg);
85632 break;
85633 }
85634 case P4_INT64: {
85635 sqlite3_str_appendf(p: &x, zFormat: "%lld", *pOp->p4.pI64);
85636 break;
85637 }
85638 case P4_INT32: {
85639 sqlite3_str_appendf(p: &x, zFormat: "%d", pOp->p4.i);
85640 break;
85641 }
85642 case P4_REAL: {
85643 sqlite3_str_appendf(p: &x, zFormat: "%.16g", *pOp->p4.pReal);
85644 break;
85645 }
85646 case P4_MEM: {
85647 Mem *pMem = pOp->p4.pMem;
85648 if( pMem->flags & MEM_Str ){
85649 zP4 = pMem->z;
85650 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
85651 sqlite3_str_appendf(p: &x, zFormat: "%lld", pMem->u.i);
85652 }else if( pMem->flags & MEM_Real ){
85653 sqlite3_str_appendf(p: &x, zFormat: "%.16g", pMem->u.r);
85654 }else if( pMem->flags & MEM_Null ){
85655 zP4 = "NULL";
85656 }else{
85657 assert( pMem->flags & MEM_Blob );
85658 zP4 = "(blob)";
85659 }
85660 break;
85661 }
85662#ifndef SQLITE_OMIT_VIRTUALTABLE
85663 case P4_VTAB: {
85664 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
85665 sqlite3_str_appendf(p: &x, zFormat: "vtab:%p", pVtab);
85666 break;
85667 }
85668#endif
85669 case P4_INTARRAY: {
85670 u32 i;
85671 u32 *ai = pOp->p4.ai;
85672 u32 n = ai[0]; /* The first element of an INTARRAY is always the
85673 ** count of the number of elements to follow */
85674 for(i=1; i<=n; i++){
85675 sqlite3_str_appendf(p: &x, zFormat: "%c%u", (i==1 ? '[' : ','), ai[i]);
85676 }
85677 sqlite3_str_append(p: &x, z: "]", N: 1);
85678 break;
85679 }
85680 case P4_SUBPROGRAM: {
85681 zP4 = "program";
85682 break;
85683 }
85684 case P4_TABLE: {
85685 zP4 = pOp->p4.pTab->zName;
85686 break;
85687 }
85688 default: {
85689 zP4 = pOp->p4.z;
85690 }
85691 }
85692 if( zP4 ) sqlite3_str_appendall(p: &x, z: zP4);
85693 if( (x.accError & SQLITE_NOMEM)!=0 ){
85694 sqlite3OomFault(db);
85695 }
85696 return sqlite3StrAccumFinish(p: &x);
85697}
85698#endif /* VDBE_DISPLAY_P4 */
85699
85700/*
85701** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
85702**
85703** The prepared statements need to know in advance the complete set of
85704** attached databases that will be use. A mask of these databases
85705** is maintained in p->btreeMask. The p->lockMask value is the subset of
85706** p->btreeMask of databases that will require a lock.
85707*/
85708SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
85709 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
85710 assert( i<(int)sizeof(p->btreeMask)*8 );
85711 DbMaskSet(p->btreeMask, i);
85712 if( i!=1 && sqlite3BtreeSharable(p: p->db->aDb[i].pBt) ){
85713 DbMaskSet(p->lockMask, i);
85714 }
85715}
85716
85717#if !defined(SQLITE_OMIT_SHARED_CACHE)
85718/*
85719** If SQLite is compiled to support shared-cache mode and to be threadsafe,
85720** this routine obtains the mutex associated with each BtShared structure
85721** that may be accessed by the VM passed as an argument. In doing so it also
85722** sets the BtShared.db member of each of the BtShared structures, ensuring
85723** that the correct busy-handler callback is invoked if required.
85724**
85725** If SQLite is not threadsafe but does support shared-cache mode, then
85726** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
85727** of all of BtShared structures accessible via the database handle
85728** associated with the VM.
85729**
85730** If SQLite is not threadsafe and does not support shared-cache mode, this
85731** function is a no-op.
85732**
85733** The p->btreeMask field is a bitmask of all btrees that the prepared
85734** statement p will ever use. Let N be the number of bits in p->btreeMask
85735** corresponding to btrees that use shared cache. Then the runtime of
85736** this routine is N*N. But as N is rarely more than 1, this should not
85737** be a problem.
85738*/
85739SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
85740 int i;
85741 sqlite3 *db;
85742 Db *aDb;
85743 int nDb;
85744 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
85745 db = p->db;
85746 aDb = db->aDb;
85747 nDb = db->nDb;
85748 for(i=0; i<nDb; i++){
85749 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
85750 sqlite3BtreeEnter(p: aDb[i].pBt);
85751 }
85752 }
85753}
85754#endif
85755
85756#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
85757/*
85758** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
85759*/
85760static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
85761 int i;
85762 sqlite3 *db;
85763 Db *aDb;
85764 int nDb;
85765 db = p->db;
85766 aDb = db->aDb;
85767 nDb = db->nDb;
85768 for(i=0; i<nDb; i++){
85769 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
85770 sqlite3BtreeLeave(p: aDb[i].pBt);
85771 }
85772 }
85773}
85774SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
85775 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
85776 vdbeLeave(p);
85777}
85778#endif
85779
85780#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
85781/*
85782** Print a single opcode. This routine is used for debugging only.
85783*/
85784SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
85785 char *zP4;
85786 char *zCom;
85787 sqlite3 dummyDb;
85788 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
85789 if( pOut==0 ) pOut = stdout;
85790 sqlite3BeginBenignMalloc();
85791 dummyDb.mallocFailed = 1;
85792 zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
85793#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
85794 zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
85795#else
85796 zCom = 0;
85797#endif
85798 /* NB: The sqlite3OpcodeName() function is implemented by code created
85799 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
85800 ** information from the vdbe.c source text */
85801 fprintf(pOut, zFormat1, pc,
85802 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
85803 zP4 ? zP4 : "", pOp->p5,
85804 zCom ? zCom : ""
85805 );
85806 fflush(pOut);
85807 sqlite3_free(zP4);
85808 sqlite3_free(zCom);
85809 sqlite3EndBenignMalloc();
85810}
85811#endif
85812
85813/*
85814** Initialize an array of N Mem element.
85815**
85816** This is a high-runner, so only those fields that really do need to
85817** be initialized are set. The Mem structure is organized so that
85818** the fields that get initialized are nearby and hopefully on the same
85819** cache line.
85820**
85821** Mem.flags = flags
85822** Mem.db = db
85823** Mem.szMalloc = 0
85824**
85825** All other fields of Mem can safely remain uninitialized for now. They
85826** will be initialized before use.
85827*/
85828static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
85829 if( N>0 ){
85830 do{
85831 p->flags = flags;
85832 p->db = db;
85833 p->szMalloc = 0;
85834#ifdef SQLITE_DEBUG
85835 p->pScopyFrom = 0;
85836#endif
85837 p++;
85838 }while( (--N)>0 );
85839 }
85840}
85841
85842/*
85843** Release auxiliary memory held in an array of N Mem elements.
85844**
85845** After this routine returns, all Mem elements in the array will still
85846** be valid. Those Mem elements that were not holding auxiliary resources
85847** will be unchanged. Mem elements which had something freed will be
85848** set to MEM_Undefined.
85849*/
85850static void releaseMemArray(Mem *p, int N){
85851 if( p && N ){
85852 Mem *pEnd = &p[N];
85853 sqlite3 *db = p->db;
85854 if( db->pnBytesFreed ){
85855 do{
85856 if( p->szMalloc ) sqlite3DbFree(db, p: p->zMalloc);
85857 }while( (++p)<pEnd );
85858 return;
85859 }
85860 do{
85861 assert( (&p[1])==pEnd || p[0].db==p[1].db );
85862 assert( sqlite3VdbeCheckMemInvariants(p) );
85863
85864 /* This block is really an inlined version of sqlite3VdbeMemRelease()
85865 ** that takes advantage of the fact that the memory cell value is
85866 ** being set to NULL after releasing any dynamic resources.
85867 **
85868 ** The justification for duplicating code is that according to
85869 ** callgrind, this causes a certain test case to hit the CPU 4.7
85870 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
85871 ** sqlite3MemRelease() were called from here. With -O2, this jumps
85872 ** to 6.6 percent. The test case is inserting 1000 rows into a table
85873 ** with no indexes using a single prepared INSERT statement, bind()
85874 ** and reset(). Inserts are grouped into a transaction.
85875 */
85876 testcase( p->flags & MEM_Agg );
85877 testcase( p->flags & MEM_Dyn );
85878 if( p->flags&(MEM_Agg|MEM_Dyn) ){
85879 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
85880 sqlite3VdbeMemRelease(p);
85881 p->flags = MEM_Undefined;
85882 }else if( p->szMalloc ){
85883 sqlite3DbNNFreeNN(db, p: p->zMalloc);
85884 p->szMalloc = 0;
85885 p->flags = MEM_Undefined;
85886 }
85887#ifdef SQLITE_DEBUG
85888 else{
85889 p->flags = MEM_Undefined;
85890 }
85891#endif
85892 }while( (++p)<pEnd );
85893 }
85894}
85895
85896#ifdef SQLITE_DEBUG
85897/*
85898** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
85899** and false if something is wrong.
85900**
85901** This routine is intended for use inside of assert() statements only.
85902*/
85903SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
85904 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
85905 return 1;
85906}
85907#endif
85908
85909
85910/*
85911** This is a destructor on a Mem object (which is really an sqlite3_value)
85912** that deletes the Frame object that is attached to it as a blob.
85913**
85914** This routine does not delete the Frame right away. It merely adds the
85915** frame to a list of frames to be deleted when the Vdbe halts.
85916*/
85917SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
85918 VdbeFrame *pFrame = (VdbeFrame*)pArg;
85919 assert( sqlite3VdbeFrameIsValid(pFrame) );
85920 pFrame->pParent = pFrame->v->pDelFrame;
85921 pFrame->v->pDelFrame = pFrame;
85922}
85923
85924#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
85925/*
85926** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
85927** QUERY PLAN output.
85928**
85929** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no
85930** more opcodes to be displayed.
85931*/
85932SQLITE_PRIVATE int sqlite3VdbeNextOpcode(
85933 Vdbe *p, /* The statement being explained */
85934 Mem *pSub, /* Storage for keeping track of subprogram nesting */
85935 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
85936 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
85937 int *piAddr, /* OUT: Write index into (*paOp)[] here */
85938 Op **paOp /* OUT: Write the opcode array here */
85939){
85940 int nRow; /* Stop when row count reaches this */
85941 int nSub = 0; /* Number of sub-vdbes seen so far */
85942 SubProgram **apSub = 0; /* Array of sub-vdbes */
85943 int i; /* Next instruction address */
85944 int rc = SQLITE_OK; /* Result code */
85945 Op *aOp = 0; /* Opcode array */
85946 int iPc; /* Rowid. Copy of value in *piPc */
85947
85948 /* When the number of output rows reaches nRow, that means the
85949 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
85950 ** nRow is the sum of the number of rows in the main program, plus
85951 ** the sum of the number of rows in all trigger subprograms encountered
85952 ** so far. The nRow value will increase as new trigger subprograms are
85953 ** encountered, but p->pc will eventually catch up to nRow.
85954 */
85955 nRow = p->nOp;
85956 if( pSub!=0 ){
85957 if( pSub->flags&MEM_Blob ){
85958 /* pSub is initiallly NULL. It is initialized to a BLOB by
85959 ** the P4_SUBPROGRAM processing logic below */
85960 nSub = pSub->n/sizeof(Vdbe*);
85961 apSub = (SubProgram **)pSub->z;
85962 }
85963 for(i=0; i<nSub; i++){
85964 nRow += apSub[i]->nOp;
85965 }
85966 }
85967 iPc = *piPc;
85968 while(1){ /* Loop exits via break */
85969 i = iPc++;
85970 if( i>=nRow ){
85971 p->rc = SQLITE_OK;
85972 rc = SQLITE_DONE;
85973 break;
85974 }
85975 if( i<p->nOp ){
85976 /* The rowid is small enough that we are still in the
85977 ** main program. */
85978 aOp = p->aOp;
85979 }else{
85980 /* We are currently listing subprograms. Figure out which one and
85981 ** pick up the appropriate opcode. */
85982 int j;
85983 i -= p->nOp;
85984 assert( apSub!=0 );
85985 assert( nSub>0 );
85986 for(j=0; i>=apSub[j]->nOp; j++){
85987 i -= apSub[j]->nOp;
85988 assert( i<apSub[j]->nOp || j+1<nSub );
85989 }
85990 aOp = apSub[j]->aOp;
85991 }
85992
85993 /* When an OP_Program opcode is encounter (the only opcode that has
85994 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
85995 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
85996 ** has not already been seen.
85997 */
85998 if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){
85999 int nByte = (nSub+1)*sizeof(SubProgram*);
86000 int j;
86001 for(j=0; j<nSub; j++){
86002 if( apSub[j]==aOp[i].p4.pProgram ) break;
86003 }
86004 if( j==nSub ){
86005 p->rc = sqlite3VdbeMemGrow(pMem: pSub, n: nByte, bPreserve: nSub!=0);
86006 if( p->rc!=SQLITE_OK ){
86007 rc = SQLITE_ERROR;
86008 break;
86009 }
86010 apSub = (SubProgram **)pSub->z;
86011 apSub[nSub++] = aOp[i].p4.pProgram;
86012 MemSetTypeFlag(pSub, MEM_Blob);
86013 pSub->n = nSub*sizeof(SubProgram*);
86014 nRow += aOp[i].p4.pProgram->nOp;
86015 }
86016 }
86017 if( eMode==0 ) break;
86018#ifdef SQLITE_ENABLE_BYTECODE_VTAB
86019 if( eMode==2 ){
86020 Op *pOp = aOp + i;
86021 if( pOp->opcode==OP_OpenRead ) break;
86022 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
86023 if( pOp->opcode==OP_ReopenIdx ) break;
86024 }else
86025#endif
86026 {
86027 assert( eMode==1 );
86028 if( aOp[i].opcode==OP_Explain ) break;
86029 if( aOp[i].opcode==OP_Init && iPc>1 ) break;
86030 }
86031 }
86032 *piPc = iPc;
86033 *piAddr = i;
86034 *paOp = aOp;
86035 return rc;
86036}
86037#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
86038
86039
86040/*
86041** Delete a VdbeFrame object and its contents. VdbeFrame objects are
86042** allocated by the OP_Program opcode in sqlite3VdbeExec().
86043*/
86044SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
86045 int i;
86046 Mem *aMem = VdbeFrameMem(p);
86047 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
86048 assert( sqlite3VdbeFrameIsValid(p) );
86049 for(i=0; i<p->nChildCsr; i++){
86050 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
86051 }
86052 releaseMemArray(p: aMem, N: p->nChildMem);
86053 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
86054 sqlite3DbFree(db: p->v->db, p);
86055}
86056
86057#ifndef SQLITE_OMIT_EXPLAIN
86058/*
86059** Give a listing of the program in the virtual machine.
86060**
86061** The interface is the same as sqlite3VdbeExec(). But instead of
86062** running the code, it invokes the callback once for each instruction.
86063** This feature is used to implement "EXPLAIN".
86064**
86065** When p->explain==1, each instruction is listed. When
86066** p->explain==2, only OP_Explain instructions are listed and these
86067** are shown in a different format. p->explain==2 is used to implement
86068** EXPLAIN QUERY PLAN.
86069** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
86070** are also shown, so that the boundaries between the main program and
86071** each trigger are clear.
86072**
86073** When p->explain==1, first the main program is listed, then each of
86074** the trigger subprograms are listed one by one.
86075*/
86076SQLITE_PRIVATE int sqlite3VdbeList(
86077 Vdbe *p /* The VDBE */
86078){
86079 Mem *pSub = 0; /* Memory cell hold array of subprogs */
86080 sqlite3 *db = p->db; /* The database connection */
86081 int i; /* Loop counter */
86082 int rc = SQLITE_OK; /* Return code */
86083 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
86084 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
86085 Op *aOp; /* Array of opcodes */
86086 Op *pOp; /* Current opcode */
86087
86088 assert( p->explain );
86089 assert( p->eVdbeState==VDBE_RUN_STATE );
86090 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
86091
86092 /* Even though this opcode does not use dynamic strings for
86093 ** the result, result columns may become dynamic if the user calls
86094 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
86095 */
86096 releaseMemArray(p: pMem, N: 8);
86097
86098 if( p->rc==SQLITE_NOMEM ){
86099 /* This happens if a malloc() inside a call to sqlite3_column_text() or
86100 ** sqlite3_column_text16() failed. */
86101 sqlite3OomFault(db);
86102 return SQLITE_ERROR;
86103 }
86104
86105 if( bListSubprogs ){
86106 /* The first 8 memory cells are used for the result set. So we will
86107 ** commandeer the 9th cell to use as storage for an array of pointers
86108 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
86109 ** cells. */
86110 assert( p->nMem>9 );
86111 pSub = &p->aMem[9];
86112 }else{
86113 pSub = 0;
86114 }
86115
86116 /* Figure out which opcode is next to display */
86117 rc = sqlite3VdbeNextOpcode(p, pSub, eMode: p->explain==2, piPc: &p->pc, piAddr: &i, paOp: &aOp);
86118
86119 if( rc==SQLITE_OK ){
86120 pOp = aOp + i;
86121 if( AtomicLoad(&db->u1.isInterrupted) ){
86122 p->rc = SQLITE_INTERRUPT;
86123 rc = SQLITE_ERROR;
86124 sqlite3VdbeError(p, zFormat: sqlite3ErrStr(p->rc));
86125 }else{
86126 char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
86127 if( p->explain==2 ){
86128 sqlite3VdbeMemSetInt64(pMem, val: pOp->p1);
86129 sqlite3VdbeMemSetInt64(pMem: pMem+1, val: pOp->p2);
86130 sqlite3VdbeMemSetInt64(pMem: pMem+2, val: pOp->p3);
86131 sqlite3VdbeMemSetStr(pMem: pMem+3, z: zP4, n: -1, SQLITE_UTF8, xDel: sqlite3_free);
86132 assert( p->nResColumn==4 );
86133 }else{
86134 sqlite3VdbeMemSetInt64(pMem: pMem+0, val: i);
86135 sqlite3VdbeMemSetStr(pMem: pMem+1, z: (char*)sqlite3OpcodeName(i: pOp->opcode),
86136 n: -1, SQLITE_UTF8, SQLITE_STATIC);
86137 sqlite3VdbeMemSetInt64(pMem: pMem+2, val: pOp->p1);
86138 sqlite3VdbeMemSetInt64(pMem: pMem+3, val: pOp->p2);
86139 sqlite3VdbeMemSetInt64(pMem: pMem+4, val: pOp->p3);
86140 /* pMem+5 for p4 is done last */
86141 sqlite3VdbeMemSetInt64(pMem: pMem+6, val: pOp->p5);
86142#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
86143 {
86144 char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
86145 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
86146 }
86147#else
86148 sqlite3VdbeMemSetNull(pMem: pMem+7);
86149#endif
86150 sqlite3VdbeMemSetStr(pMem: pMem+5, z: zP4, n: -1, SQLITE_UTF8, xDel: sqlite3_free);
86151 assert( p->nResColumn==8 );
86152 }
86153 p->pResultRow = pMem;
86154 if( db->mallocFailed ){
86155 p->rc = SQLITE_NOMEM;
86156 rc = SQLITE_ERROR;
86157 }else{
86158 p->rc = SQLITE_OK;
86159 rc = SQLITE_ROW;
86160 }
86161 }
86162 }
86163 return rc;
86164}
86165#endif /* SQLITE_OMIT_EXPLAIN */
86166
86167#ifdef SQLITE_DEBUG
86168/*
86169** Print the SQL that was used to generate a VDBE program.
86170*/
86171SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
86172 const char *z = 0;
86173 if( p->zSql ){
86174 z = p->zSql;
86175 }else if( p->nOp>=1 ){
86176 const VdbeOp *pOp = &p->aOp[0];
86177 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
86178 z = pOp->p4.z;
86179 while( sqlite3Isspace(*z) ) z++;
86180 }
86181 }
86182 if( z ) printf("SQL: [%s]\n", z);
86183}
86184#endif
86185
86186#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
86187/*
86188** Print an IOTRACE message showing SQL content.
86189*/
86190SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
86191 int nOp = p->nOp;
86192 VdbeOp *pOp;
86193 if( sqlite3IoTrace==0 ) return;
86194 if( nOp<1 ) return;
86195 pOp = &p->aOp[0];
86196 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
86197 int i, j;
86198 char z[1000];
86199 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
86200 for(i=0; sqlite3Isspace(z[i]); i++){}
86201 for(j=0; z[i]; i++){
86202 if( sqlite3Isspace(z[i]) ){
86203 if( z[i-1]!=' ' ){
86204 z[j++] = ' ';
86205 }
86206 }else{
86207 z[j++] = z[i];
86208 }
86209 }
86210 z[j] = 0;
86211 sqlite3IoTrace("SQL %s\n", z);
86212 }
86213}
86214#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
86215
86216/* An instance of this object describes bulk memory available for use
86217** by subcomponents of a prepared statement. Space is allocated out
86218** of a ReusableSpace object by the allocSpace() routine below.
86219*/
86220struct ReusableSpace {
86221 u8 *pSpace; /* Available memory */
86222 sqlite3_int64 nFree; /* Bytes of available memory */
86223 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
86224};
86225
86226/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
86227** from the ReusableSpace object. Return a pointer to the allocated
86228** memory on success. If insufficient memory is available in the
86229** ReusableSpace object, increase the ReusableSpace.nNeeded
86230** value by the amount needed and return NULL.
86231**
86232** If pBuf is not initially NULL, that means that the memory has already
86233** been allocated by a prior call to this routine, so just return a copy
86234** of pBuf and leave ReusableSpace unchanged.
86235**
86236** This allocator is employed to repurpose unused slots at the end of the
86237** opcode array of prepared state for other memory needs of the prepared
86238** statement.
86239*/
86240static void *allocSpace(
86241 struct ReusableSpace *p, /* Bulk memory available for allocation */
86242 void *pBuf, /* Pointer to a prior allocation */
86243 sqlite3_int64 nByte /* Bytes of memory needed. */
86244){
86245 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
86246 if( pBuf==0 ){
86247 nByte = ROUND8P(nByte);
86248 if( nByte <= p->nFree ){
86249 p->nFree -= nByte;
86250 pBuf = &p->pSpace[p->nFree];
86251 }else{
86252 p->nNeeded += nByte;
86253 }
86254 }
86255 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
86256 return pBuf;
86257}
86258
86259/*
86260** Rewind the VDBE back to the beginning in preparation for
86261** running it.
86262*/
86263SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
86264#if defined(SQLITE_DEBUG)
86265 int i;
86266#endif
86267 assert( p!=0 );
86268 assert( p->eVdbeState==VDBE_INIT_STATE
86269 || p->eVdbeState==VDBE_READY_STATE
86270 || p->eVdbeState==VDBE_HALT_STATE );
86271
86272 /* There should be at least one opcode.
86273 */
86274 assert( p->nOp>0 );
86275
86276 p->eVdbeState = VDBE_READY_STATE;
86277
86278#ifdef SQLITE_DEBUG
86279 for(i=0; i<p->nMem; i++){
86280 assert( p->aMem[i].db==p->db );
86281 }
86282#endif
86283 p->pc = -1;
86284 p->rc = SQLITE_OK;
86285 p->errorAction = OE_Abort;
86286 p->nChange = 0;
86287 p->cacheCtr = 1;
86288 p->minWriteFileFormat = 255;
86289 p->iStatement = 0;
86290 p->nFkConstraint = 0;
86291#ifdef VDBE_PROFILE
86292 for(i=0; i<p->nOp; i++){
86293 p->aOp[i].nExec = 0;
86294 p->aOp[i].nCycle = 0;
86295 }
86296#endif
86297}
86298
86299/*
86300** Prepare a virtual machine for execution for the first time after
86301** creating the virtual machine. This involves things such
86302** as allocating registers and initializing the program counter.
86303** After the VDBE has be prepped, it can be executed by one or more
86304** calls to sqlite3VdbeExec().
86305**
86306** This function may be called exactly once on each virtual machine.
86307** After this routine is called the VM has been "packaged" and is ready
86308** to run. After this routine is called, further calls to
86309** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
86310** the Vdbe from the Parse object that helped generate it so that the
86311** the Vdbe becomes an independent entity and the Parse object can be
86312** destroyed.
86313**
86314** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
86315** to its initial state after it has been run.
86316*/
86317SQLITE_PRIVATE void sqlite3VdbeMakeReady(
86318 Vdbe *p, /* The VDBE */
86319 Parse *pParse /* Parsing context */
86320){
86321 sqlite3 *db; /* The database connection */
86322 int nVar; /* Number of parameters */
86323 int nMem; /* Number of VM memory registers */
86324 int nCursor; /* Number of cursors required */
86325 int nArg; /* Number of arguments in subprograms */
86326 int n; /* Loop counter */
86327 struct ReusableSpace x; /* Reusable bulk memory */
86328
86329 assert( p!=0 );
86330 assert( p->nOp>0 );
86331 assert( pParse!=0 );
86332 assert( p->eVdbeState==VDBE_INIT_STATE );
86333 assert( pParse==p->pParse );
86334 p->pVList = pParse->pVList;
86335 pParse->pVList = 0;
86336 db = p->db;
86337 assert( db->mallocFailed==0 );
86338 nVar = pParse->nVar;
86339 nMem = pParse->nMem;
86340 nCursor = pParse->nTab;
86341 nArg = pParse->nMaxArg;
86342
86343 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
86344 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
86345 ** space at the end of aMem[] for cursors 1 and greater.
86346 ** See also: allocateCursor().
86347 */
86348 nMem += nCursor;
86349 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
86350
86351 /* Figure out how much reusable memory is available at the end of the
86352 ** opcode array. This extra memory will be reallocated for other elements
86353 ** of the prepared statement.
86354 */
86355 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
86356 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
86357 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
86358 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
86359 assert( x.nFree>=0 );
86360 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
86361
86362 resolveP2Values(p, pMaxFuncArgs: &nArg);
86363 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
86364 if( pParse->explain ){
86365 if( nMem<10 ) nMem = 10;
86366 p->explain = pParse->explain;
86367 p->nResColumn = 12 - 4*p->explain;
86368 }
86369 p->expired = 0;
86370
86371 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
86372 ** passes. On the first pass, we try to reuse unused memory at the
86373 ** end of the opcode array. If we are unable to satisfy all memory
86374 ** requirements by reusing the opcode array tail, then the second
86375 ** pass will fill in the remainder using a fresh memory allocation.
86376 **
86377 ** This two-pass approach that reuses as much memory as possible from
86378 ** the leftover memory at the end of the opcode array. This can significantly
86379 ** reduce the amount of memory held by a prepared statement.
86380 */
86381 x.nNeeded = 0;
86382 p->aMem = allocSpace(p: &x, pBuf: 0, nByte: nMem*sizeof(Mem));
86383 p->aVar = allocSpace(p: &x, pBuf: 0, nByte: nVar*sizeof(Mem));
86384 p->apArg = allocSpace(p: &x, pBuf: 0, nByte: nArg*sizeof(Mem*));
86385 p->apCsr = allocSpace(p: &x, pBuf: 0, nByte: nCursor*sizeof(VdbeCursor*));
86386 if( x.nNeeded ){
86387 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, n: x.nNeeded);
86388 x.nFree = x.nNeeded;
86389 if( !db->mallocFailed ){
86390 p->aMem = allocSpace(p: &x, pBuf: p->aMem, nByte: nMem*sizeof(Mem));
86391 p->aVar = allocSpace(p: &x, pBuf: p->aVar, nByte: nVar*sizeof(Mem));
86392 p->apArg = allocSpace(p: &x, pBuf: p->apArg, nByte: nArg*sizeof(Mem*));
86393 p->apCsr = allocSpace(p: &x, pBuf: p->apCsr, nByte: nCursor*sizeof(VdbeCursor*));
86394 }
86395 }
86396
86397 if( db->mallocFailed ){
86398 p->nVar = 0;
86399 p->nCursor = 0;
86400 p->nMem = 0;
86401 }else{
86402 p->nCursor = nCursor;
86403 p->nVar = (ynVar)nVar;
86404 initMemArray(p: p->aVar, N: nVar, db, MEM_Null);
86405 p->nMem = nMem;
86406 initMemArray(p: p->aMem, N: nMem, db, MEM_Undefined);
86407 memset(s: p->apCsr, c: 0, n: nCursor*sizeof(VdbeCursor*));
86408 }
86409 sqlite3VdbeRewind(p);
86410}
86411
86412/*
86413** Close a VDBE cursor and release all the resources that cursor
86414** happens to hold.
86415*/
86416SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
86417 if( pCx ) sqlite3VdbeFreeCursorNN(p,pCx);
86418}
86419static SQLITE_NOINLINE void freeCursorWithCache(Vdbe *p, VdbeCursor *pCx){
86420 VdbeTxtBlbCache *pCache = pCx->pCache;
86421 assert( pCx->colCache );
86422 pCx->colCache = 0;
86423 pCx->pCache = 0;
86424 if( pCache->pCValue ){
86425 sqlite3RCStrUnref(z: pCache->pCValue);
86426 pCache->pCValue = 0;
86427 }
86428 sqlite3DbFree(db: p->db, p: pCache);
86429 sqlite3VdbeFreeCursorNN(p, pCx);
86430}
86431SQLITE_PRIVATE void sqlite3VdbeFreeCursorNN(Vdbe *p, VdbeCursor *pCx){
86432 if( pCx->colCache ){
86433 freeCursorWithCache(p, pCx);
86434 return;
86435 }
86436 switch( pCx->eCurType ){
86437 case CURTYPE_SORTER: {
86438 sqlite3VdbeSorterClose(p->db, pCx);
86439 break;
86440 }
86441 case CURTYPE_BTREE: {
86442 assert( pCx->uc.pCursor!=0 );
86443 sqlite3BtreeCloseCursor(pCur: pCx->uc.pCursor);
86444 break;
86445 }
86446#ifndef SQLITE_OMIT_VIRTUALTABLE
86447 case CURTYPE_VTAB: {
86448 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
86449 const sqlite3_module *pModule = pVCur->pVtab->pModule;
86450 assert( pVCur->pVtab->nRef>0 );
86451 pVCur->pVtab->nRef--;
86452 pModule->xClose(pVCur);
86453 break;
86454 }
86455#endif
86456 }
86457}
86458
86459/*
86460** Close all cursors in the current frame.
86461*/
86462static void closeCursorsInFrame(Vdbe *p){
86463 int i;
86464 for(i=0; i<p->nCursor; i++){
86465 VdbeCursor *pC = p->apCsr[i];
86466 if( pC ){
86467 sqlite3VdbeFreeCursorNN(p, pCx: pC);
86468 p->apCsr[i] = 0;
86469 }
86470 }
86471}
86472
86473/*
86474** Copy the values stored in the VdbeFrame structure to its Vdbe. This
86475** is used, for example, when a trigger sub-program is halted to restore
86476** control to the main program.
86477*/
86478SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
86479 Vdbe *v = pFrame->v;
86480 closeCursorsInFrame(p: v);
86481 v->aOp = pFrame->aOp;
86482 v->nOp = pFrame->nOp;
86483 v->aMem = pFrame->aMem;
86484 v->nMem = pFrame->nMem;
86485 v->apCsr = pFrame->apCsr;
86486 v->nCursor = pFrame->nCursor;
86487 v->db->lastRowid = pFrame->lastRowid;
86488 v->nChange = pFrame->nChange;
86489 v->db->nChange = pFrame->nDbChange;
86490 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
86491 v->pAuxData = pFrame->pAuxData;
86492 pFrame->pAuxData = 0;
86493 return pFrame->pc;
86494}
86495
86496/*
86497** Close all cursors.
86498**
86499** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
86500** cell array. This is necessary as the memory cell array may contain
86501** pointers to VdbeFrame objects, which may in turn contain pointers to
86502** open cursors.
86503*/
86504static void closeAllCursors(Vdbe *p){
86505 if( p->pFrame ){
86506 VdbeFrame *pFrame;
86507 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
86508 sqlite3VdbeFrameRestore(pFrame);
86509 p->pFrame = 0;
86510 p->nFrame = 0;
86511 }
86512 assert( p->nFrame==0 );
86513 closeCursorsInFrame(p);
86514 releaseMemArray(p: p->aMem, N: p->nMem);
86515 while( p->pDelFrame ){
86516 VdbeFrame *pDel = p->pDelFrame;
86517 p->pDelFrame = pDel->pParent;
86518 sqlite3VdbeFrameDelete(p: pDel);
86519 }
86520
86521 /* Delete any auxdata allocations made by the VM */
86522 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
86523 assert( p->pAuxData==0 );
86524}
86525
86526/*
86527** Set the number of result columns that will be returned by this SQL
86528** statement. This is now set at compile time, rather than during
86529** execution of the vdbe program so that sqlite3_column_count() can
86530** be called on an SQL statement before sqlite3_step().
86531*/
86532SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
86533 int n;
86534 sqlite3 *db = p->db;
86535
86536 if( p->nResAlloc ){
86537 releaseMemArray(p: p->aColName, N: p->nResAlloc*COLNAME_N);
86538 sqlite3DbFree(db, p: p->aColName);
86539 }
86540 n = nResColumn*COLNAME_N;
86541 p->nResColumn = p->nResAlloc = (u16)nResColumn;
86542 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, n: sizeof(Mem)*n );
86543 if( p->aColName==0 ) return;
86544 initMemArray(p: p->aColName, N: n, db, MEM_Null);
86545}
86546
86547/*
86548** Set the name of the idx'th column to be returned by the SQL statement.
86549** zName must be a pointer to a nul terminated string.
86550**
86551** This call must be made after a call to sqlite3VdbeSetNumCols().
86552**
86553** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
86554** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
86555** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
86556*/
86557SQLITE_PRIVATE int sqlite3VdbeSetColName(
86558 Vdbe *p, /* Vdbe being configured */
86559 int idx, /* Index of column zName applies to */
86560 int var, /* One of the COLNAME_* constants */
86561 const char *zName, /* Pointer to buffer containing name */
86562 void (*xDel)(void*) /* Memory management strategy for zName */
86563){
86564 int rc;
86565 Mem *pColName;
86566 assert( idx<p->nResAlloc );
86567 assert( var<COLNAME_N );
86568 if( p->db->mallocFailed ){
86569 assert( !zName || xDel!=SQLITE_DYNAMIC );
86570 return SQLITE_NOMEM_BKPT;
86571 }
86572 assert( p->aColName!=0 );
86573 pColName = &(p->aColName[idx+var*p->nResAlloc]);
86574 rc = sqlite3VdbeMemSetStr(pMem: pColName, z: zName, n: -1, SQLITE_UTF8, xDel);
86575 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
86576 return rc;
86577}
86578
86579/*
86580** A read or write transaction may or may not be active on database handle
86581** db. If a transaction is active, commit it. If there is a
86582** write-transaction spanning more than one database file, this routine
86583** takes care of the super-journal trickery.
86584*/
86585static int vdbeCommit(sqlite3 *db, Vdbe *p){
86586 int i;
86587 int nTrans = 0; /* Number of databases with an active write-transaction
86588 ** that are candidates for a two-phase commit using a
86589 ** super-journal */
86590 int rc = SQLITE_OK;
86591 int needXcommit = 0;
86592
86593#ifdef SQLITE_OMIT_VIRTUALTABLE
86594 /* With this option, sqlite3VtabSync() is defined to be simply
86595 ** SQLITE_OK so p is not used.
86596 */
86597 UNUSED_PARAMETER(p);
86598#endif
86599
86600 /* Before doing anything else, call the xSync() callback for any
86601 ** virtual module tables written in this transaction. This has to
86602 ** be done before determining whether a super-journal file is
86603 ** required, as an xSync() callback may add an attached database
86604 ** to the transaction.
86605 */
86606 rc = sqlite3VtabSync(db, p);
86607
86608 /* This loop determines (a) if the commit hook should be invoked and
86609 ** (b) how many database files have open write transactions, not
86610 ** including the temp database. (b) is important because if more than
86611 ** one database file has an open write transaction, a super-journal
86612 ** file is required for an atomic commit.
86613 */
86614 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
86615 Btree *pBt = db->aDb[i].pBt;
86616 if( sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_WRITE ){
86617 /* Whether or not a database might need a super-journal depends upon
86618 ** its journal mode (among other things). This matrix determines which
86619 ** journal modes use a super-journal and which do not */
86620 static const u8 aMJNeeded[] = {
86621 /* DELETE */ 1,
86622 /* PERSIST */ 1,
86623 /* OFF */ 0,
86624 /* TRUNCATE */ 1,
86625 /* MEMORY */ 0,
86626 /* WAL */ 0
86627 };
86628 Pager *pPager; /* Pager associated with pBt */
86629 needXcommit = 1;
86630 sqlite3BtreeEnter(p: pBt);
86631 pPager = sqlite3BtreePager(p: pBt);
86632 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
86633 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
86634 && sqlite3PagerIsMemdb(pPager)==0
86635 ){
86636 assert( i!=1 );
86637 nTrans++;
86638 }
86639 rc = sqlite3PagerExclusiveLock(pPager);
86640 sqlite3BtreeLeave(p: pBt);
86641 }
86642 }
86643 if( rc!=SQLITE_OK ){
86644 return rc;
86645 }
86646
86647 /* If there are any write-transactions at all, invoke the commit hook */
86648 if( needXcommit && db->xCommitCallback ){
86649 rc = db->xCommitCallback(db->pCommitArg);
86650 if( rc ){
86651 return SQLITE_CONSTRAINT_COMMITHOOK;
86652 }
86653 }
86654
86655 /* The simple case - no more than one database file (not counting the
86656 ** TEMP database) has a transaction active. There is no need for the
86657 ** super-journal.
86658 **
86659 ** If the return value of sqlite3BtreeGetFilename() is a zero length
86660 ** string, it means the main database is :memory: or a temp file. In
86661 ** that case we do not support atomic multi-file commits, so use the
86662 ** simple case then too.
86663 */
86664 if( 0==sqlite3Strlen30(z: sqlite3BtreeGetFilename(p: db->aDb[0].pBt))
86665 || nTrans<=1
86666 ){
86667 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
86668 Btree *pBt = db->aDb[i].pBt;
86669 if( pBt ){
86670 rc = sqlite3BtreeCommitPhaseOne(p: pBt, zSuperJrnl: 0);
86671 }
86672 }
86673
86674 /* Do the commit only if all databases successfully complete phase 1.
86675 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
86676 ** IO error while deleting or truncating a journal file. It is unlikely,
86677 ** but could happen. In this case abandon processing and return the error.
86678 */
86679 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
86680 Btree *pBt = db->aDb[i].pBt;
86681 if( pBt ){
86682 rc = sqlite3BtreeCommitPhaseTwo(p: pBt, bCleanup: 0);
86683 }
86684 }
86685 if( rc==SQLITE_OK ){
86686 sqlite3VtabCommit(db);
86687 }
86688 }
86689
86690 /* The complex case - There is a multi-file write-transaction active.
86691 ** This requires a super-journal file to ensure the transaction is
86692 ** committed atomically.
86693 */
86694#ifndef SQLITE_OMIT_DISKIO
86695 else{
86696 sqlite3_vfs *pVfs = db->pVfs;
86697 char *zSuper = 0; /* File-name for the super-journal */
86698 char const *zMainFile = sqlite3BtreeGetFilename(p: db->aDb[0].pBt);
86699 sqlite3_file *pSuperJrnl = 0;
86700 i64 offset = 0;
86701 int res;
86702 int retryCount = 0;
86703 int nMainFile;
86704
86705 /* Select a super-journal file name */
86706 nMainFile = sqlite3Strlen30(z: zMainFile);
86707 zSuper = sqlite3MPrintf(db, zFormat: "%.4c%s%.16c", 0,zMainFile,0);
86708 if( zSuper==0 ) return SQLITE_NOMEM_BKPT;
86709 zSuper += 4;
86710 do {
86711 u32 iRandom;
86712 if( retryCount ){
86713 if( retryCount>100 ){
86714 sqlite3_log(SQLITE_FULL, zFormat: "MJ delete: %s", zSuper);
86715 sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
86716 break;
86717 }else if( retryCount==1 ){
86718 sqlite3_log(SQLITE_FULL, zFormat: "MJ collide: %s", zSuper);
86719 }
86720 }
86721 retryCount++;
86722 sqlite3_randomness(N: sizeof(iRandom), pBuf: &iRandom);
86723 sqlite3_snprintf(n: 13, zBuf: &zSuper[nMainFile], zFormat: "-mj%06X9%02X",
86724 (iRandom>>8)&0xffffff, iRandom&0xff);
86725 /* The antipenultimate character of the super-journal name must
86726 ** be "9" to avoid name collisions when using 8+3 filenames. */
86727 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
86728 sqlite3FileSuffix3(zMainFile, zSuper);
86729 rc = sqlite3OsAccess(pVfs, zPath: zSuper, SQLITE_ACCESS_EXISTS, pResOut: &res);
86730 }while( rc==SQLITE_OK && res );
86731 if( rc==SQLITE_OK ){
86732 /* Open the super-journal. */
86733 rc = sqlite3OsOpenMalloc(pVfs, zFile: zSuper, ppFile: &pSuperJrnl,
86734 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
86735 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_SUPER_JOURNAL, pOutFlags: 0
86736 );
86737 }
86738 if( rc!=SQLITE_OK ){
86739 sqlite3DbFree(db, p: zSuper-4);
86740 return rc;
86741 }
86742
86743 /* Write the name of each database file in the transaction into the new
86744 ** super-journal file. If an error occurs at this point close
86745 ** and delete the super-journal file. All the individual journal files
86746 ** still have 'null' as the super-journal pointer, so they will roll
86747 ** back independently if a failure occurs.
86748 */
86749 for(i=0; i<db->nDb; i++){
86750 Btree *pBt = db->aDb[i].pBt;
86751 if( sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_WRITE ){
86752 char const *zFile = sqlite3BtreeGetJournalname(p: pBt);
86753 if( zFile==0 ){
86754 continue; /* Ignore TEMP and :memory: databases */
86755 }
86756 assert( zFile[0]!=0 );
86757 rc = sqlite3OsWrite(id: pSuperJrnl, pBuf: zFile, amt: sqlite3Strlen30(z: zFile)+1,offset);
86758 offset += sqlite3Strlen30(z: zFile)+1;
86759 if( rc!=SQLITE_OK ){
86760 sqlite3OsCloseFree(pFile: pSuperJrnl);
86761 sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
86762 sqlite3DbFree(db, p: zSuper-4);
86763 return rc;
86764 }
86765 }
86766 }
86767
86768 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
86769 ** flag is set this is not required.
86770 */
86771 if( 0==(sqlite3OsDeviceCharacteristics(id: pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL)
86772 && SQLITE_OK!=(rc = sqlite3OsSync(id: pSuperJrnl, SQLITE_SYNC_NORMAL))
86773 ){
86774 sqlite3OsCloseFree(pFile: pSuperJrnl);
86775 sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
86776 sqlite3DbFree(db, p: zSuper-4);
86777 return rc;
86778 }
86779
86780 /* Sync all the db files involved in the transaction. The same call
86781 ** sets the super-journal pointer in each individual journal. If
86782 ** an error occurs here, do not delete the super-journal file.
86783 **
86784 ** If the error occurs during the first call to
86785 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
86786 ** super-journal file will be orphaned. But we cannot delete it,
86787 ** in case the super-journal file name was written into the journal
86788 ** file before the failure occurred.
86789 */
86790 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
86791 Btree *pBt = db->aDb[i].pBt;
86792 if( pBt ){
86793 rc = sqlite3BtreeCommitPhaseOne(p: pBt, zSuperJrnl: zSuper);
86794 }
86795 }
86796 sqlite3OsCloseFree(pFile: pSuperJrnl);
86797 assert( rc!=SQLITE_BUSY );
86798 if( rc!=SQLITE_OK ){
86799 sqlite3DbFree(db, p: zSuper-4);
86800 return rc;
86801 }
86802
86803 /* Delete the super-journal file. This commits the transaction. After
86804 ** doing this the directory is synced again before any individual
86805 ** transaction files are deleted.
86806 */
86807 rc = sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 1);
86808 sqlite3DbFree(db, p: zSuper-4);
86809 zSuper = 0;
86810 if( rc ){
86811 return rc;
86812 }
86813
86814 /* All files and directories have already been synced, so the following
86815 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
86816 ** deleting or truncating journals. If something goes wrong while
86817 ** this is happening we don't really care. The integrity of the
86818 ** transaction is already guaranteed, but some stray 'cold' journals
86819 ** may be lying around. Returning an error code won't help matters.
86820 */
86821 disable_simulated_io_errors();
86822 sqlite3BeginBenignMalloc();
86823 for(i=0; i<db->nDb; i++){
86824 Btree *pBt = db->aDb[i].pBt;
86825 if( pBt ){
86826 sqlite3BtreeCommitPhaseTwo(p: pBt, bCleanup: 1);
86827 }
86828 }
86829 sqlite3EndBenignMalloc();
86830 enable_simulated_io_errors();
86831
86832 sqlite3VtabCommit(db);
86833 }
86834#endif
86835
86836 return rc;
86837}
86838
86839/*
86840** This routine checks that the sqlite3.nVdbeActive count variable
86841** matches the number of vdbe's in the list sqlite3.pVdbe that are
86842** currently active. An assertion fails if the two counts do not match.
86843** This is an internal self-check only - it is not an essential processing
86844** step.
86845**
86846** This is a no-op if NDEBUG is defined.
86847*/
86848#ifndef NDEBUG
86849static void checkActiveVdbeCnt(sqlite3 *db){
86850 Vdbe *p;
86851 int cnt = 0;
86852 int nWrite = 0;
86853 int nRead = 0;
86854 p = db->pVdbe;
86855 while( p ){
86856 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
86857 cnt++;
86858 if( p->readOnly==0 ) nWrite++;
86859 if( p->bIsReader ) nRead++;
86860 }
86861 p = p->pVNext;
86862 }
86863 assert( cnt==db->nVdbeActive );
86864 assert( nWrite==db->nVdbeWrite );
86865 assert( nRead==db->nVdbeRead );
86866}
86867#else
86868#define checkActiveVdbeCnt(x)
86869#endif
86870
86871/*
86872** If the Vdbe passed as the first argument opened a statement-transaction,
86873** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
86874** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
86875** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
86876** statement transaction is committed.
86877**
86878** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
86879** Otherwise SQLITE_OK.
86880*/
86881static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
86882 sqlite3 *const db = p->db;
86883 int rc = SQLITE_OK;
86884 int i;
86885 const int iSavepoint = p->iStatement-1;
86886
86887 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
86888 assert( db->nStatement>0 );
86889 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
86890
86891 for(i=0; i<db->nDb; i++){
86892 int rc2 = SQLITE_OK;
86893 Btree *pBt = db->aDb[i].pBt;
86894 if( pBt ){
86895 if( eOp==SAVEPOINT_ROLLBACK ){
86896 rc2 = sqlite3BtreeSavepoint(p: pBt, SAVEPOINT_ROLLBACK, iSavepoint);
86897 }
86898 if( rc2==SQLITE_OK ){
86899 rc2 = sqlite3BtreeSavepoint(p: pBt, SAVEPOINT_RELEASE, iSavepoint);
86900 }
86901 if( rc==SQLITE_OK ){
86902 rc = rc2;
86903 }
86904 }
86905 }
86906 db->nStatement--;
86907 p->iStatement = 0;
86908
86909 if( rc==SQLITE_OK ){
86910 if( eOp==SAVEPOINT_ROLLBACK ){
86911 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
86912 }
86913 if( rc==SQLITE_OK ){
86914 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
86915 }
86916 }
86917
86918 /* If the statement transaction is being rolled back, also restore the
86919 ** database handles deferred constraint counter to the value it had when
86920 ** the statement transaction was opened. */
86921 if( eOp==SAVEPOINT_ROLLBACK ){
86922 db->nDeferredCons = p->nStmtDefCons;
86923 db->nDeferredImmCons = p->nStmtDefImmCons;
86924 }
86925 return rc;
86926}
86927SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
86928 if( p->db->nStatement && p->iStatement ){
86929 return vdbeCloseStatement(p, eOp);
86930 }
86931 return SQLITE_OK;
86932}
86933
86934
86935/*
86936** This function is called when a transaction opened by the database
86937** handle associated with the VM passed as an argument is about to be
86938** committed. If there are outstanding deferred foreign key constraint
86939** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
86940**
86941** If there are outstanding FK violations and this function returns
86942** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
86943** and write an error message to it. Then return SQLITE_ERROR.
86944*/
86945#ifndef SQLITE_OMIT_FOREIGN_KEY
86946SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
86947 sqlite3 *db = p->db;
86948 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
86949 || (!deferred && p->nFkConstraint>0)
86950 ){
86951 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
86952 p->errorAction = OE_Abort;
86953 sqlite3VdbeError(p, zFormat: "FOREIGN KEY constraint failed");
86954 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
86955 return SQLITE_CONSTRAINT_FOREIGNKEY;
86956 }
86957 return SQLITE_OK;
86958}
86959#endif
86960
86961/*
86962** This routine is called the when a VDBE tries to halt. If the VDBE
86963** has made changes and is in autocommit mode, then commit those
86964** changes. If a rollback is needed, then do the rollback.
86965**
86966** This routine is the only way to move the sqlite3eOpenState of a VM from
86967** SQLITE_STATE_RUN to SQLITE_STATE_HALT. It is harmless to
86968** call this on a VM that is in the SQLITE_STATE_HALT state.
86969**
86970** Return an error code. If the commit could not complete because of
86971** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
86972** means the close did not happen and needs to be repeated.
86973*/
86974SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
86975 int rc; /* Used to store transient return codes */
86976 sqlite3 *db = p->db;
86977
86978 /* This function contains the logic that determines if a statement or
86979 ** transaction will be committed or rolled back as a result of the
86980 ** execution of this virtual machine.
86981 **
86982 ** If any of the following errors occur:
86983 **
86984 ** SQLITE_NOMEM
86985 ** SQLITE_IOERR
86986 ** SQLITE_FULL
86987 ** SQLITE_INTERRUPT
86988 **
86989 ** Then the internal cache might have been left in an inconsistent
86990 ** state. We need to rollback the statement transaction, if there is
86991 ** one, or the complete transaction if there is no statement transaction.
86992 */
86993
86994 assert( p->eVdbeState==VDBE_RUN_STATE );
86995 if( db->mallocFailed ){
86996 p->rc = SQLITE_NOMEM_BKPT;
86997 }
86998 closeAllCursors(p);
86999 checkActiveVdbeCnt(db);
87000
87001 /* No commit or rollback needed if the program never started or if the
87002 ** SQL statement does not read or write a database file. */
87003 if( p->bIsReader ){
87004 int mrc; /* Primary error code from p->rc */
87005 int eStatementOp = 0;
87006 int isSpecialError; /* Set to true if a 'special' error */
87007
87008 /* Lock all btrees used by the statement */
87009 sqlite3VdbeEnter(p);
87010
87011 /* Check for one of the special errors */
87012 if( p->rc ){
87013 mrc = p->rc & 0xff;
87014 isSpecialError = mrc==SQLITE_NOMEM
87015 || mrc==SQLITE_IOERR
87016 || mrc==SQLITE_INTERRUPT
87017 || mrc==SQLITE_FULL;
87018 }else{
87019 mrc = isSpecialError = 0;
87020 }
87021 if( isSpecialError ){
87022 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
87023 ** no rollback is necessary. Otherwise, at least a savepoint
87024 ** transaction must be rolled back to restore the database to a
87025 ** consistent state.
87026 **
87027 ** Even if the statement is read-only, it is important to perform
87028 ** a statement or transaction rollback operation. If the error
87029 ** occurred while writing to the journal, sub-journal or database
87030 ** file as part of an effort to free up cache space (see function
87031 ** pagerStress() in pager.c), the rollback is required to restore
87032 ** the pager to a consistent state.
87033 */
87034 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
87035 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
87036 eStatementOp = SAVEPOINT_ROLLBACK;
87037 }else{
87038 /* We are forced to roll back the active transaction. Before doing
87039 ** so, abort any other statements this handle currently has active.
87040 */
87041 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
87042 sqlite3CloseSavepoints(db);
87043 db->autoCommit = 1;
87044 p->nChange = 0;
87045 }
87046 }
87047 }
87048
87049 /* Check for immediate foreign key violations. */
87050 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
87051 sqlite3VdbeCheckFk(p, deferred: 0);
87052 }
87053
87054 /* If the auto-commit flag is set and this is the only active writer
87055 ** VM, then we do either a commit or rollback of the current transaction.
87056 **
87057 ** Note: This block also runs if one of the special errors handled
87058 ** above has occurred.
87059 */
87060 if( !sqlite3VtabInSync(db)
87061 && db->autoCommit
87062 && db->nVdbeWrite==(p->readOnly==0)
87063 ){
87064 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
87065 rc = sqlite3VdbeCheckFk(p, deferred: 1);
87066 if( rc!=SQLITE_OK ){
87067 if( NEVER(p->readOnly) ){
87068 sqlite3VdbeLeave(p);
87069 return SQLITE_ERROR;
87070 }
87071 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
87072 }else if( db->flags & SQLITE_CorruptRdOnly ){
87073 rc = SQLITE_CORRUPT;
87074 db->flags &= ~SQLITE_CorruptRdOnly;
87075 }else{
87076 /* The auto-commit flag is true, the vdbe program was successful
87077 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
87078 ** key constraints to hold up the transaction. This means a commit
87079 ** is required. */
87080 rc = vdbeCommit(db, p);
87081 }
87082 if( rc==SQLITE_BUSY && p->readOnly ){
87083 sqlite3VdbeLeave(p);
87084 return SQLITE_BUSY;
87085 }else if( rc!=SQLITE_OK ){
87086 sqlite3SystemError(db, rc);
87087 p->rc = rc;
87088 sqlite3RollbackAll(db, SQLITE_OK);
87089 p->nChange = 0;
87090 }else{
87091 db->nDeferredCons = 0;
87092 db->nDeferredImmCons = 0;
87093 db->flags &= ~(u64)SQLITE_DeferFKs;
87094 sqlite3CommitInternalChanges(db);
87095 }
87096 }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
87097 p->nChange = 0;
87098 }else{
87099 sqlite3RollbackAll(db, SQLITE_OK);
87100 p->nChange = 0;
87101 }
87102 db->nStatement = 0;
87103 }else if( eStatementOp==0 ){
87104 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
87105 eStatementOp = SAVEPOINT_RELEASE;
87106 }else if( p->errorAction==OE_Abort ){
87107 eStatementOp = SAVEPOINT_ROLLBACK;
87108 }else{
87109 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
87110 sqlite3CloseSavepoints(db);
87111 db->autoCommit = 1;
87112 p->nChange = 0;
87113 }
87114 }
87115
87116 /* If eStatementOp is non-zero, then a statement transaction needs to
87117 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
87118 ** do so. If this operation returns an error, and the current statement
87119 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
87120 ** current statement error code.
87121 */
87122 if( eStatementOp ){
87123 rc = sqlite3VdbeCloseStatement(p, eOp: eStatementOp);
87124 if( rc ){
87125 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
87126 p->rc = rc;
87127 sqlite3DbFree(db, p: p->zErrMsg);
87128 p->zErrMsg = 0;
87129 }
87130 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
87131 sqlite3CloseSavepoints(db);
87132 db->autoCommit = 1;
87133 p->nChange = 0;
87134 }
87135 }
87136
87137 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
87138 ** has been rolled back, update the database connection change-counter.
87139 */
87140 if( p->changeCntOn ){
87141 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
87142 sqlite3VdbeSetChanges(db, p->nChange);
87143 }else{
87144 sqlite3VdbeSetChanges(db, 0);
87145 }
87146 p->nChange = 0;
87147 }
87148
87149 /* Release the locks */
87150 sqlite3VdbeLeave(p);
87151 }
87152
87153 /* We have successfully halted and closed the VM. Record this fact. */
87154 db->nVdbeActive--;
87155 if( !p->readOnly ) db->nVdbeWrite--;
87156 if( p->bIsReader ) db->nVdbeRead--;
87157 assert( db->nVdbeActive>=db->nVdbeRead );
87158 assert( db->nVdbeRead>=db->nVdbeWrite );
87159 assert( db->nVdbeWrite>=0 );
87160 p->eVdbeState = VDBE_HALT_STATE;
87161 checkActiveVdbeCnt(db);
87162 if( db->mallocFailed ){
87163 p->rc = SQLITE_NOMEM_BKPT;
87164 }
87165
87166 /* If the auto-commit flag is set to true, then any locks that were held
87167 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
87168 ** to invoke any required unlock-notify callbacks.
87169 */
87170 if( db->autoCommit ){
87171 sqlite3ConnectionUnlocked(db);
87172 }
87173
87174 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
87175 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
87176}
87177
87178
87179/*
87180** Each VDBE holds the result of the most recent sqlite3_step() call
87181** in p->rc. This routine sets that result back to SQLITE_OK.
87182*/
87183SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
87184 p->rc = SQLITE_OK;
87185}
87186
87187/*
87188** Copy the error code and error message belonging to the VDBE passed
87189** as the first argument to its database handle (so that they will be
87190** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
87191**
87192** This function does not clear the VDBE error code or message, just
87193** copies them to the database handle.
87194*/
87195SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
87196 sqlite3 *db = p->db;
87197 int rc = p->rc;
87198 if( p->zErrMsg ){
87199 db->bBenignMalloc++;
87200 sqlite3BeginBenignMalloc();
87201 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
87202 sqlite3ValueSetStr(v: db->pErr, n: -1, z: p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
87203 sqlite3EndBenignMalloc();
87204 db->bBenignMalloc--;
87205 }else if( db->pErr ){
87206 sqlite3ValueSetNull(p: db->pErr);
87207 }
87208 db->errCode = rc;
87209 db->errByteOffset = -1;
87210 return rc;
87211}
87212
87213#ifdef SQLITE_ENABLE_SQLLOG
87214/*
87215** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
87216** invoke it.
87217*/
87218static void vdbeInvokeSqllog(Vdbe *v){
87219 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
87220 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
87221 assert( v->db->init.busy==0 );
87222 if( zExpanded ){
87223 sqlite3GlobalConfig.xSqllog(
87224 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
87225 );
87226 sqlite3DbFree(v->db, zExpanded);
87227 }
87228 }
87229}
87230#else
87231# define vdbeInvokeSqllog(x)
87232#endif
87233
87234/*
87235** Clean up a VDBE after execution but do not delete the VDBE just yet.
87236** Write any error messages into *pzErrMsg. Return the result code.
87237**
87238** After this routine is run, the VDBE should be ready to be executed
87239** again.
87240**
87241** To look at it another way, this routine resets the state of the
87242** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to
87243** VDBE_READY_STATE.
87244*/
87245SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
87246#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
87247 int i;
87248#endif
87249
87250 sqlite3 *db;
87251 db = p->db;
87252
87253 /* If the VM did not run to completion or if it encountered an
87254 ** error, then it might not have been halted properly. So halt
87255 ** it now.
87256 */
87257 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
87258
87259 /* If the VDBE has been run even partially, then transfer the error code
87260 ** and error message from the VDBE into the main database structure. But
87261 ** if the VDBE has just been set to run but has not actually executed any
87262 ** instructions yet, leave the main database error information unchanged.
87263 */
87264 if( p->pc>=0 ){
87265 vdbeInvokeSqllog(p);
87266 if( db->pErr || p->zErrMsg ){
87267 sqlite3VdbeTransferError(p);
87268 }else{
87269 db->errCode = p->rc;
87270 }
87271 }
87272
87273 /* Reset register contents and reclaim error message memory.
87274 */
87275#ifdef SQLITE_DEBUG
87276 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
87277 ** Vdbe.aMem[] arrays have already been cleaned up. */
87278 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
87279 if( p->aMem ){
87280 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
87281 }
87282#endif
87283 if( p->zErrMsg ){
87284 sqlite3DbFree(db, p: p->zErrMsg);
87285 p->zErrMsg = 0;
87286 }
87287 p->pResultRow = 0;
87288#ifdef SQLITE_DEBUG
87289 p->nWrite = 0;
87290#endif
87291
87292 /* Save profiling information from this VDBE run.
87293 */
87294#ifdef VDBE_PROFILE
87295 {
87296 FILE *out = fopen("vdbe_profile.out", "a");
87297 if( out ){
87298 fprintf(out, "---- ");
87299 for(i=0; i<p->nOp; i++){
87300 fprintf(out, "%02x", p->aOp[i].opcode);
87301 }
87302 fprintf(out, "\n");
87303 if( p->zSql ){
87304 char c, pc = 0;
87305 fprintf(out, "-- ");
87306 for(i=0; (c = p->zSql[i])!=0; i++){
87307 if( pc=='\n' ) fprintf(out, "-- ");
87308 putc(c, out);
87309 pc = c;
87310 }
87311 if( pc!='\n' ) fprintf(out, "\n");
87312 }
87313 for(i=0; i<p->nOp; i++){
87314 char zHdr[100];
87315 i64 cnt = p->aOp[i].nExec;
87316 i64 cycles = p->aOp[i].nCycle;
87317 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
87318 cnt,
87319 cycles,
87320 cnt>0 ? cycles/cnt : 0
87321 );
87322 fprintf(out, "%s", zHdr);
87323 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
87324 }
87325 fclose(out);
87326 }
87327 }
87328#endif
87329 return p->rc & db->errMask;
87330}
87331
87332/*
87333** Clean up and delete a VDBE after execution. Return an integer which is
87334** the result code. Write any error message text into *pzErrMsg.
87335*/
87336SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
87337 int rc = SQLITE_OK;
87338 assert( VDBE_RUN_STATE>VDBE_READY_STATE );
87339 assert( VDBE_HALT_STATE>VDBE_READY_STATE );
87340 assert( VDBE_INIT_STATE<VDBE_READY_STATE );
87341 if( p->eVdbeState>=VDBE_READY_STATE ){
87342 rc = sqlite3VdbeReset(p);
87343 assert( (rc & p->db->errMask)==rc );
87344 }
87345 sqlite3VdbeDelete(p);
87346 return rc;
87347}
87348
87349/*
87350** If parameter iOp is less than zero, then invoke the destructor for
87351** all auxiliary data pointers currently cached by the VM passed as
87352** the first argument.
87353**
87354** Or, if iOp is greater than or equal to zero, then the destructor is
87355** only invoked for those auxiliary data pointers created by the user
87356** function invoked by the OP_Function opcode at instruction iOp of
87357** VM pVdbe, and only then if:
87358**
87359** * the associated function parameter is the 32nd or later (counting
87360** from left to right), or
87361**
87362** * the corresponding bit in argument mask is clear (where the first
87363** function parameter corresponds to bit 0 etc.).
87364*/
87365SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
87366 while( *pp ){
87367 AuxData *pAux = *pp;
87368 if( (iOp<0)
87369 || (pAux->iAuxOp==iOp
87370 && pAux->iAuxArg>=0
87371 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
87372 ){
87373 testcase( pAux->iAuxArg==31 );
87374 if( pAux->xDeleteAux ){
87375 pAux->xDeleteAux(pAux->pAux);
87376 }
87377 *pp = pAux->pNextAux;
87378 sqlite3DbFree(db, p: pAux);
87379 }else{
87380 pp= &pAux->pNextAux;
87381 }
87382 }
87383}
87384
87385/*
87386** Free all memory associated with the Vdbe passed as the second argument,
87387** except for object itself, which is preserved.
87388**
87389** The difference between this function and sqlite3VdbeDelete() is that
87390** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
87391** the database connection and frees the object itself.
87392*/
87393static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
87394 SubProgram *pSub, *pNext;
87395 assert( db!=0 );
87396 assert( p->db==0 || p->db==db );
87397 if( p->aColName ){
87398 releaseMemArray(p: p->aColName, N: p->nResAlloc*COLNAME_N);
87399 sqlite3DbNNFreeNN(db, p: p->aColName);
87400 }
87401 for(pSub=p->pProgram; pSub; pSub=pNext){
87402 pNext = pSub->pNext;
87403 vdbeFreeOpArray(db, aOp: pSub->aOp, nOp: pSub->nOp);
87404 sqlite3DbFree(db, p: pSub);
87405 }
87406 if( p->eVdbeState!=VDBE_INIT_STATE ){
87407 releaseMemArray(p: p->aVar, N: p->nVar);
87408 if( p->pVList ) sqlite3DbNNFreeNN(db, p: p->pVList);
87409 if( p->pFree ) sqlite3DbNNFreeNN(db, p: p->pFree);
87410 }
87411 vdbeFreeOpArray(db, aOp: p->aOp, nOp: p->nOp);
87412 if( p->zSql ) sqlite3DbNNFreeNN(db, p: p->zSql);
87413#ifdef SQLITE_ENABLE_NORMALIZE
87414 sqlite3DbFree(db, p->zNormSql);
87415 {
87416 DblquoteStr *pThis, *pNxt;
87417 for(pThis=p->pDblStr; pThis; pThis=pNxt){
87418 pNxt = pThis->pNextStr;
87419 sqlite3DbFree(db, pThis);
87420 }
87421 }
87422#endif
87423#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
87424 {
87425 int i;
87426 for(i=0; i<p->nScan; i++){
87427 sqlite3DbFree(db, p->aScan[i].zName);
87428 }
87429 sqlite3DbFree(db, p->aScan);
87430 }
87431#endif
87432}
87433
87434/*
87435** Delete an entire VDBE.
87436*/
87437SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
87438 sqlite3 *db;
87439
87440 assert( p!=0 );
87441 db = p->db;
87442 assert( db!=0 );
87443 assert( sqlite3_mutex_held(db->mutex) );
87444 sqlite3VdbeClearObject(db, p);
87445 if( db->pnBytesFreed==0 ){
87446 assert( p->ppVPrev!=0 );
87447 *p->ppVPrev = p->pVNext;
87448 if( p->pVNext ){
87449 p->pVNext->ppVPrev = p->ppVPrev;
87450 }
87451 }
87452 sqlite3DbNNFreeNN(db, p);
87453}
87454
87455/*
87456** The cursor "p" has a pending seek operation that has not yet been
87457** carried out. Seek the cursor now. If an error occurs, return
87458** the appropriate error code.
87459*/
87460SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){
87461 int res, rc;
87462#ifdef SQLITE_TEST
87463 extern int sqlite3_search_count;
87464#endif
87465 assert( p->deferredMoveto );
87466 assert( p->isTable );
87467 assert( p->eCurType==CURTYPE_BTREE );
87468 rc = sqlite3BtreeTableMoveto(pCur: p->uc.pCursor, intKey: p->movetoTarget, biasRight: 0, pRes: &res);
87469 if( rc ) return rc;
87470 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
87471#ifdef SQLITE_TEST
87472 sqlite3_search_count++;
87473#endif
87474 p->deferredMoveto = 0;
87475 p->cacheStatus = CACHE_STALE;
87476 return SQLITE_OK;
87477}
87478
87479/*
87480** Something has moved cursor "p" out of place. Maybe the row it was
87481** pointed to was deleted out from under it. Or maybe the btree was
87482** rebalanced. Whatever the cause, try to restore "p" to the place it
87483** is supposed to be pointing. If the row was deleted out from under the
87484** cursor, set the cursor to point to a NULL row.
87485*/
87486SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p){
87487 int isDifferentRow, rc;
87488 assert( p->eCurType==CURTYPE_BTREE );
87489 assert( p->uc.pCursor!=0 );
87490 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
87491 rc = sqlite3BtreeCursorRestore(pCur: p->uc.pCursor, pDifferentRow: &isDifferentRow);
87492 p->cacheStatus = CACHE_STALE;
87493 if( isDifferentRow ) p->nullRow = 1;
87494 return rc;
87495}
87496
87497/*
87498** Check to ensure that the cursor is valid. Restore the cursor
87499** if need be. Return any I/O error from the restore operation.
87500*/
87501SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
87502 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
87503 if( sqlite3BtreeCursorHasMoved(pCur: p->uc.pCursor) ){
87504 return sqlite3VdbeHandleMovedCursor(p);
87505 }
87506 return SQLITE_OK;
87507}
87508
87509/*
87510** The following functions:
87511**
87512** sqlite3VdbeSerialType()
87513** sqlite3VdbeSerialTypeLen()
87514** sqlite3VdbeSerialLen()
87515** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
87516** sqlite3VdbeSerialGet()
87517**
87518** encapsulate the code that serializes values for storage in SQLite
87519** data and index records. Each serialized value consists of a
87520** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
87521** integer, stored as a varint.
87522**
87523** In an SQLite index record, the serial type is stored directly before
87524** the blob of data that it corresponds to. In a table record, all serial
87525** types are stored at the start of the record, and the blobs of data at
87526** the end. Hence these functions allow the caller to handle the
87527** serial-type and data blob separately.
87528**
87529** The following table describes the various storage classes for data:
87530**
87531** serial type bytes of data type
87532** -------------- --------------- ---------------
87533** 0 0 NULL
87534** 1 1 signed integer
87535** 2 2 signed integer
87536** 3 3 signed integer
87537** 4 4 signed integer
87538** 5 6 signed integer
87539** 6 8 signed integer
87540** 7 8 IEEE float
87541** 8 0 Integer constant 0
87542** 9 0 Integer constant 1
87543** 10,11 reserved for expansion
87544** N>=12 and even (N-12)/2 BLOB
87545** N>=13 and odd (N-13)/2 text
87546**
87547** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
87548** of SQLite will not understand those serial types.
87549*/
87550
87551#if 0 /* Inlined into the OP_MakeRecord opcode */
87552/*
87553** Return the serial-type for the value stored in pMem.
87554**
87555** This routine might convert a large MEM_IntReal value into MEM_Real.
87556**
87557** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
87558** opcode in the byte-code engine. But by moving this routine in-line, we
87559** can omit some redundant tests and make that opcode a lot faster. So
87560** this routine is now only used by the STAT3 logic and STAT3 support has
87561** ended. The code is kept here for historical reference only.
87562*/
87563SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
87564 int flags = pMem->flags;
87565 u32 n;
87566
87567 assert( pLen!=0 );
87568 if( flags&MEM_Null ){
87569 *pLen = 0;
87570 return 0;
87571 }
87572 if( flags&(MEM_Int|MEM_IntReal) ){
87573 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
87574# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
87575 i64 i = pMem->u.i;
87576 u64 u;
87577 testcase( flags & MEM_Int );
87578 testcase( flags & MEM_IntReal );
87579 if( i<0 ){
87580 u = ~i;
87581 }else{
87582 u = i;
87583 }
87584 if( u<=127 ){
87585 if( (i&1)==i && file_format>=4 ){
87586 *pLen = 0;
87587 return 8+(u32)u;
87588 }else{
87589 *pLen = 1;
87590 return 1;
87591 }
87592 }
87593 if( u<=32767 ){ *pLen = 2; return 2; }
87594 if( u<=8388607 ){ *pLen = 3; return 3; }
87595 if( u<=2147483647 ){ *pLen = 4; return 4; }
87596 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
87597 *pLen = 8;
87598 if( flags&MEM_IntReal ){
87599 /* If the value is IntReal and is going to take up 8 bytes to store
87600 ** as an integer, then we might as well make it an 8-byte floating
87601 ** point value */
87602 pMem->u.r = (double)pMem->u.i;
87603 pMem->flags &= ~MEM_IntReal;
87604 pMem->flags |= MEM_Real;
87605 return 7;
87606 }
87607 return 6;
87608 }
87609 if( flags&MEM_Real ){
87610 *pLen = 8;
87611 return 7;
87612 }
87613 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
87614 assert( pMem->n>=0 );
87615 n = (u32)pMem->n;
87616 if( flags & MEM_Zero ){
87617 n += pMem->u.nZero;
87618 }
87619 *pLen = n;
87620 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
87621}
87622#endif /* inlined into OP_MakeRecord */
87623
87624/*
87625** The sizes for serial types less than 128
87626*/
87627SQLITE_PRIVATE const u8 sqlite3SmallTypeSizes[128] = {
87628 /* 0 1 2 3 4 5 6 7 8 9 */
87629/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
87630/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
87631/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
87632/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
87633/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
87634/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
87635/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
87636/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
87637/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
87638/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
87639/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
87640/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
87641/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
87642};
87643
87644/*
87645** Return the length of the data corresponding to the supplied serial-type.
87646*/
87647SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
87648 if( serial_type>=128 ){
87649 return (serial_type-12)/2;
87650 }else{
87651 assert( serial_type<12
87652 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
87653 return sqlite3SmallTypeSizes[serial_type];
87654 }
87655}
87656SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
87657 assert( serial_type<128 );
87658 return sqlite3SmallTypeSizes[serial_type];
87659}
87660
87661/*
87662** If we are on an architecture with mixed-endian floating
87663** points (ex: ARM7) then swap the lower 4 bytes with the
87664** upper 4 bytes. Return the result.
87665**
87666** For most architectures, this is a no-op.
87667**
87668** (later): It is reported to me that the mixed-endian problem
87669** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
87670** that early versions of GCC stored the two words of a 64-bit
87671** float in the wrong order. And that error has been propagated
87672** ever since. The blame is not necessarily with GCC, though.
87673** GCC might have just copying the problem from a prior compiler.
87674** I am also told that newer versions of GCC that follow a different
87675** ABI get the byte order right.
87676**
87677** Developers using SQLite on an ARM7 should compile and run their
87678** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
87679** enabled, some asserts below will ensure that the byte order of
87680** floating point values is correct.
87681**
87682** (2007-08-30) Frank van Vugt has studied this problem closely
87683** and has send his findings to the SQLite developers. Frank
87684** writes that some Linux kernels offer floating point hardware
87685** emulation that uses only 32-bit mantissas instead of a full
87686** 48-bits as required by the IEEE standard. (This is the
87687** CONFIG_FPE_FASTFPE option.) On such systems, floating point
87688** byte swapping becomes very complicated. To avoid problems,
87689** the necessary byte swapping is carried out using a 64-bit integer
87690** rather than a 64-bit float. Frank assures us that the code here
87691** works for him. We, the developers, have no way to independently
87692** verify this, but Frank seems to know what he is talking about
87693** so we trust him.
87694*/
87695#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
87696SQLITE_PRIVATE u64 sqlite3FloatSwap(u64 in){
87697 union {
87698 u64 r;
87699 u32 i[2];
87700 } u;
87701 u32 t;
87702
87703 u.r = in;
87704 t = u.i[0];
87705 u.i[0] = u.i[1];
87706 u.i[1] = t;
87707 return u.r;
87708}
87709#endif /* SQLITE_MIXED_ENDIAN_64BIT_FLOAT */
87710
87711
87712/* Input "x" is a sequence of unsigned characters that represent a
87713** big-endian integer. Return the equivalent native integer
87714*/
87715#define ONE_BYTE_INT(x) ((i8)(x)[0])
87716#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
87717#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
87718#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
87719#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
87720
87721/*
87722** Deserialize the data blob pointed to by buf as serial type serial_type
87723** and store the result in pMem.
87724**
87725** This function is implemented as two separate routines for performance.
87726** The few cases that require local variables are broken out into a separate
87727** routine so that in most cases the overhead of moving the stack pointer
87728** is avoided.
87729*/
87730static void serialGet(
87731 const unsigned char *buf, /* Buffer to deserialize from */
87732 u32 serial_type, /* Serial type to deserialize */
87733 Mem *pMem /* Memory cell to write value into */
87734){
87735 u64 x = FOUR_BYTE_UINT(buf);
87736 u32 y = FOUR_BYTE_UINT(buf+4);
87737 x = (x<<32) + y;
87738 if( serial_type==6 ){
87739 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
87740 ** twos-complement integer. */
87741 pMem->u.i = *(i64*)&x;
87742 pMem->flags = MEM_Int;
87743 testcase( pMem->u.i<0 );
87744 }else{
87745 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
87746 ** floating point number. */
87747#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
87748 /* Verify that integers and floating point values use the same
87749 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
87750 ** defined that 64-bit floating point values really are mixed
87751 ** endian.
87752 */
87753 static const u64 t1 = ((u64)0x3ff00000)<<32;
87754 static const double r1 = 1.0;
87755 u64 t2 = t1;
87756 swapMixedEndianFloat(t2);
87757 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
87758#endif
87759 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
87760 swapMixedEndianFloat(x);
87761 memcpy(dest: &pMem->u.r, src: &x, n: sizeof(x));
87762 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
87763 }
87764}
87765SQLITE_PRIVATE void sqlite3VdbeSerialGet(
87766 const unsigned char *buf, /* Buffer to deserialize from */
87767 u32 serial_type, /* Serial type to deserialize */
87768 Mem *pMem /* Memory cell to write value into */
87769){
87770 switch( serial_type ){
87771 case 10: { /* Internal use only: NULL with virtual table
87772 ** UPDATE no-change flag set */
87773 pMem->flags = MEM_Null|MEM_Zero;
87774 pMem->n = 0;
87775 pMem->u.nZero = 0;
87776 return;
87777 }
87778 case 11: /* Reserved for future use */
87779 case 0: { /* Null */
87780 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
87781 pMem->flags = MEM_Null;
87782 return;
87783 }
87784 case 1: {
87785 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
87786 ** integer. */
87787 pMem->u.i = ONE_BYTE_INT(buf);
87788 pMem->flags = MEM_Int;
87789 testcase( pMem->u.i<0 );
87790 return;
87791 }
87792 case 2: { /* 2-byte signed integer */
87793 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
87794 ** twos-complement integer. */
87795 pMem->u.i = TWO_BYTE_INT(buf);
87796 pMem->flags = MEM_Int;
87797 testcase( pMem->u.i<0 );
87798 return;
87799 }
87800 case 3: { /* 3-byte signed integer */
87801 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
87802 ** twos-complement integer. */
87803 pMem->u.i = THREE_BYTE_INT(buf);
87804 pMem->flags = MEM_Int;
87805 testcase( pMem->u.i<0 );
87806 return;
87807 }
87808 case 4: { /* 4-byte signed integer */
87809 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
87810 ** twos-complement integer. */
87811 pMem->u.i = FOUR_BYTE_INT(buf);
87812#ifdef __HP_cc
87813 /* Work around a sign-extension bug in the HP compiler for HP/UX */
87814 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
87815#endif
87816 pMem->flags = MEM_Int;
87817 testcase( pMem->u.i<0 );
87818 return;
87819 }
87820 case 5: { /* 6-byte signed integer */
87821 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
87822 ** twos-complement integer. */
87823 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
87824 pMem->flags = MEM_Int;
87825 testcase( pMem->u.i<0 );
87826 return;
87827 }
87828 case 6: /* 8-byte signed integer */
87829 case 7: { /* IEEE floating point */
87830 /* These use local variables, so do them in a separate routine
87831 ** to avoid having to move the frame pointer in the common case */
87832 serialGet(buf,serial_type,pMem);
87833 return;
87834 }
87835 case 8: /* Integer 0 */
87836 case 9: { /* Integer 1 */
87837 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
87838 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
87839 pMem->u.i = serial_type-8;
87840 pMem->flags = MEM_Int;
87841 return;
87842 }
87843 default: {
87844 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
87845 ** length.
87846 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
87847 ** (N-13)/2 bytes in length. */
87848 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
87849 pMem->z = (char *)buf;
87850 pMem->n = (serial_type-12)/2;
87851 pMem->flags = aFlag[serial_type&1];
87852 return;
87853 }
87854 }
87855 return;
87856}
87857/*
87858** This routine is used to allocate sufficient space for an UnpackedRecord
87859** structure large enough to be used with sqlite3VdbeRecordUnpack() if
87860** the first argument is a pointer to KeyInfo structure pKeyInfo.
87861**
87862** The space is either allocated using sqlite3DbMallocRaw() or from within
87863** the unaligned buffer passed via the second and third arguments (presumably
87864** stack space). If the former, then *ppFree is set to a pointer that should
87865** be eventually freed by the caller using sqlite3DbFree(). Or, if the
87866** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
87867** before returning.
87868**
87869** If an OOM error occurs, NULL is returned.
87870*/
87871SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
87872 KeyInfo *pKeyInfo /* Description of the record */
87873){
87874 UnpackedRecord *p; /* Unpacked record to return */
87875 int nByte; /* Number of bytes required for *p */
87876 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
87877 p = (UnpackedRecord *)sqlite3DbMallocRaw(db: pKeyInfo->db, n: nByte);
87878 if( !p ) return 0;
87879 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
87880 assert( pKeyInfo->aSortFlags!=0 );
87881 p->pKeyInfo = pKeyInfo;
87882 p->nField = pKeyInfo->nKeyField + 1;
87883 return p;
87884}
87885
87886/*
87887** Given the nKey-byte encoding of a record in pKey[], populate the
87888** UnpackedRecord structure indicated by the fourth argument with the
87889** contents of the decoded record.
87890*/
87891SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
87892 KeyInfo *pKeyInfo, /* Information about the record format */
87893 int nKey, /* Size of the binary record */
87894 const void *pKey, /* The binary record */
87895 UnpackedRecord *p /* Populate this structure before returning. */
87896){
87897 const unsigned char *aKey = (const unsigned char *)pKey;
87898 u32 d;
87899 u32 idx; /* Offset in aKey[] to read from */
87900 u16 u; /* Unsigned loop counter */
87901 u32 szHdr;
87902 Mem *pMem = p->aMem;
87903
87904 p->default_rc = 0;
87905 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
87906 idx = getVarint32(aKey, szHdr);
87907 d = szHdr;
87908 u = 0;
87909 while( idx<szHdr && d<=(u32)nKey ){
87910 u32 serial_type;
87911
87912 idx += getVarint32(&aKey[idx], serial_type);
87913 pMem->enc = pKeyInfo->enc;
87914 pMem->db = pKeyInfo->db;
87915 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
87916 pMem->szMalloc = 0;
87917 pMem->z = 0;
87918 sqlite3VdbeSerialGet(buf: &aKey[d], serial_type, pMem);
87919 d += sqlite3VdbeSerialTypeLen(serial_type);
87920 pMem++;
87921 if( (++u)>=p->nField ) break;
87922 }
87923 if( d>(u32)nKey && u ){
87924 assert( CORRUPT_DB );
87925 /* In a corrupt record entry, the last pMem might have been set up using
87926 ** uninitialized memory. Overwrite its value with NULL, to prevent
87927 ** warnings from MSAN. */
87928 sqlite3VdbeMemSetNull(pMem: pMem-1);
87929 }
87930 assert( u<=pKeyInfo->nKeyField + 1 );
87931 p->nField = u;
87932}
87933
87934#ifdef SQLITE_DEBUG
87935/*
87936** This function compares two index or table record keys in the same way
87937** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
87938** this function deserializes and compares values using the
87939** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
87940** in assert() statements to ensure that the optimized code in
87941** sqlite3VdbeRecordCompare() returns results with these two primitives.
87942**
87943** Return true if the result of comparison is equivalent to desiredResult.
87944** Return false if there is a disagreement.
87945*/
87946static int vdbeRecordCompareDebug(
87947 int nKey1, const void *pKey1, /* Left key */
87948 const UnpackedRecord *pPKey2, /* Right key */
87949 int desiredResult /* Correct answer */
87950){
87951 u32 d1; /* Offset into aKey[] of next data element */
87952 u32 idx1; /* Offset into aKey[] of next header element */
87953 u32 szHdr1; /* Number of bytes in header */
87954 int i = 0;
87955 int rc = 0;
87956 const unsigned char *aKey1 = (const unsigned char *)pKey1;
87957 KeyInfo *pKeyInfo;
87958 Mem mem1;
87959
87960 pKeyInfo = pPKey2->pKeyInfo;
87961 if( pKeyInfo->db==0 ) return 1;
87962 mem1.enc = pKeyInfo->enc;
87963 mem1.db = pKeyInfo->db;
87964 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
87965 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
87966
87967 /* Compilers may complain that mem1.u.i is potentially uninitialized.
87968 ** We could initialize it, as shown here, to silence those complaints.
87969 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
87970 ** the unnecessary initialization has a measurable negative performance
87971 ** impact, since this routine is a very high runner. And so, we choose
87972 ** to ignore the compiler warnings and leave this variable uninitialized.
87973 */
87974 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
87975
87976 idx1 = getVarint32(aKey1, szHdr1);
87977 if( szHdr1>98307 ) return SQLITE_CORRUPT;
87978 d1 = szHdr1;
87979 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
87980 assert( pKeyInfo->aSortFlags!=0 );
87981 assert( pKeyInfo->nKeyField>0 );
87982 assert( idx1<=szHdr1 || CORRUPT_DB );
87983 do{
87984 u32 serial_type1;
87985
87986 /* Read the serial types for the next element in each key. */
87987 idx1 += getVarint32( aKey1+idx1, serial_type1 );
87988
87989 /* Verify that there is enough key space remaining to avoid
87990 ** a buffer overread. The "d1+serial_type1+2" subexpression will
87991 ** always be greater than or equal to the amount of required key space.
87992 ** Use that approximation to avoid the more expensive call to
87993 ** sqlite3VdbeSerialTypeLen() in the common case.
87994 */
87995 if( d1+(u64)serial_type1+2>(u64)nKey1
87996 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
87997 ){
87998 if( serial_type1>=1
87999 && serial_type1<=7
88000 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)<=(u64)nKey1+8
88001 && CORRUPT_DB
88002 ){
88003 return 1; /* corrupt record not detected by
88004 ** sqlite3VdbeRecordCompareWithSkip(). Return true
88005 ** to avoid firing the assert() */
88006 }
88007 break;
88008 }
88009
88010 /* Extract the values to be compared.
88011 */
88012 sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
88013 d1 += sqlite3VdbeSerialTypeLen(serial_type1);
88014
88015 /* Do the comparison
88016 */
88017 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
88018 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
88019 if( rc!=0 ){
88020 assert( mem1.szMalloc==0 ); /* See comment below */
88021 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
88022 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
88023 ){
88024 rc = -rc;
88025 }
88026 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
88027 rc = -rc; /* Invert the result for DESC sort order. */
88028 }
88029 goto debugCompareEnd;
88030 }
88031 i++;
88032 }while( idx1<szHdr1 && i<pPKey2->nField );
88033
88034 /* No memory allocation is ever used on mem1. Prove this using
88035 ** the following assert(). If the assert() fails, it indicates a
88036 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
88037 */
88038 assert( mem1.szMalloc==0 );
88039
88040 /* rc==0 here means that one of the keys ran out of fields and
88041 ** all the fields up to that point were equal. Return the default_rc
88042 ** value. */
88043 rc = pPKey2->default_rc;
88044
88045debugCompareEnd:
88046 if( desiredResult==0 && rc==0 ) return 1;
88047 if( desiredResult<0 && rc<0 ) return 1;
88048 if( desiredResult>0 && rc>0 ) return 1;
88049 if( CORRUPT_DB ) return 1;
88050 if( pKeyInfo->db->mallocFailed ) return 1;
88051 return 0;
88052}
88053#endif
88054
88055#ifdef SQLITE_DEBUG
88056/*
88057** Count the number of fields (a.k.a. columns) in the record given by
88058** pKey,nKey. The verify that this count is less than or equal to the
88059** limit given by pKeyInfo->nAllField.
88060**
88061** If this constraint is not satisfied, it means that the high-speed
88062** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
88063** not work correctly. If this assert() ever fires, it probably means
88064** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
88065** incorrectly.
88066*/
88067static void vdbeAssertFieldCountWithinLimits(
88068 int nKey, const void *pKey, /* The record to verify */
88069 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
88070){
88071 int nField = 0;
88072 u32 szHdr;
88073 u32 idx;
88074 u32 notUsed;
88075 const unsigned char *aKey = (const unsigned char*)pKey;
88076
88077 if( CORRUPT_DB ) return;
88078 idx = getVarint32(aKey, szHdr);
88079 assert( nKey>=0 );
88080 assert( szHdr<=(u32)nKey );
88081 while( idx<szHdr ){
88082 idx += getVarint32(aKey+idx, notUsed);
88083 nField++;
88084 }
88085 assert( nField <= pKeyInfo->nAllField );
88086}
88087#else
88088# define vdbeAssertFieldCountWithinLimits(A,B,C)
88089#endif
88090
88091/*
88092** Both *pMem1 and *pMem2 contain string values. Compare the two values
88093** using the collation sequence pColl. As usual, return a negative , zero
88094** or positive value if *pMem1 is less than, equal to or greater than
88095** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
88096*/
88097static int vdbeCompareMemString(
88098 const Mem *pMem1,
88099 const Mem *pMem2,
88100 const CollSeq *pColl,
88101 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
88102){
88103 if( pMem1->enc==pColl->enc ){
88104 /* The strings are already in the correct encoding. Call the
88105 ** comparison function directly */
88106 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
88107 }else{
88108 int rc;
88109 const void *v1, *v2;
88110 Mem c1;
88111 Mem c2;
88112 sqlite3VdbeMemInit(pMem: &c1, db: pMem1->db, MEM_Null);
88113 sqlite3VdbeMemInit(pMem: &c2, db: pMem1->db, MEM_Null);
88114 sqlite3VdbeMemShallowCopy(pTo: &c1, pFrom: pMem1, MEM_Ephem);
88115 sqlite3VdbeMemShallowCopy(pTo: &c2, pFrom: pMem2, MEM_Ephem);
88116 v1 = sqlite3ValueText(pVal: (sqlite3_value*)&c1, enc: pColl->enc);
88117 v2 = sqlite3ValueText(pVal: (sqlite3_value*)&c2, enc: pColl->enc);
88118 if( (v1==0 || v2==0) ){
88119 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
88120 rc = 0;
88121 }else{
88122 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
88123 }
88124 sqlite3VdbeMemReleaseMalloc(p: &c1);
88125 sqlite3VdbeMemReleaseMalloc(p: &c2);
88126 return rc;
88127 }
88128}
88129
88130/*
88131** The input pBlob is guaranteed to be a Blob that is not marked
88132** with MEM_Zero. Return true if it could be a zero-blob.
88133*/
88134static int isAllZero(const char *z, int n){
88135 int i;
88136 for(i=0; i<n; i++){
88137 if( z[i] ) return 0;
88138 }
88139 return 1;
88140}
88141
88142/*
88143** Compare two blobs. Return negative, zero, or positive if the first
88144** is less than, equal to, or greater than the second, respectively.
88145** If one blob is a prefix of the other, then the shorter is the lessor.
88146*/
88147SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
88148 int c;
88149 int n1 = pB1->n;
88150 int n2 = pB2->n;
88151
88152 /* It is possible to have a Blob value that has some non-zero content
88153 ** followed by zero content. But that only comes up for Blobs formed
88154 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
88155 ** sqlite3MemCompare(). */
88156 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
88157 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
88158
88159 if( (pB1->flags|pB2->flags) & MEM_Zero ){
88160 if( pB1->flags & pB2->flags & MEM_Zero ){
88161 return pB1->u.nZero - pB2->u.nZero;
88162 }else if( pB1->flags & MEM_Zero ){
88163 if( !isAllZero(z: pB2->z, n: pB2->n) ) return -1;
88164 return pB1->u.nZero - n2;
88165 }else{
88166 if( !isAllZero(z: pB1->z, n: pB1->n) ) return +1;
88167 return n1 - pB2->u.nZero;
88168 }
88169 }
88170 c = memcmp(s1: pB1->z, s2: pB2->z, n: n1>n2 ? n2 : n1);
88171 if( c ) return c;
88172 return n1 - n2;
88173}
88174
88175/*
88176** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
88177** number. Return negative, zero, or positive if the first (i64) is less than,
88178** equal to, or greater than the second (double).
88179*/
88180SQLITE_PRIVATE int sqlite3IntFloatCompare(i64 i, double r){
88181 if( sizeof(LONGDOUBLE_TYPE)>8 ){
88182 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
88183 testcase( x<r );
88184 testcase( x>r );
88185 testcase( x==r );
88186 if( x<r ) return -1;
88187 if( x>r ) return +1; /*NO_TEST*/ /* work around bugs in gcov */
88188 return 0; /*NO_TEST*/ /* work around bugs in gcov */
88189 }else{
88190 i64 y;
88191 double s;
88192 if( r<-9223372036854775808.0 ) return +1;
88193 if( r>=9223372036854775808.0 ) return -1;
88194 y = (i64)r;
88195 if( i<y ) return -1;
88196 if( i>y ) return +1;
88197 s = (double)i;
88198 if( s<r ) return -1;
88199 if( s>r ) return +1;
88200 return 0;
88201 }
88202}
88203
88204/*
88205** Compare the values contained by the two memory cells, returning
88206** negative, zero or positive if pMem1 is less than, equal to, or greater
88207** than pMem2. Sorting order is NULL's first, followed by numbers (integers
88208** and reals) sorted numerically, followed by text ordered by the collating
88209** sequence pColl and finally blob's ordered by memcmp().
88210**
88211** Two NULL values are considered equal by this function.
88212*/
88213SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
88214 int f1, f2;
88215 int combined_flags;
88216
88217 f1 = pMem1->flags;
88218 f2 = pMem2->flags;
88219 combined_flags = f1|f2;
88220 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
88221
88222 /* If one value is NULL, it is less than the other. If both values
88223 ** are NULL, return 0.
88224 */
88225 if( combined_flags&MEM_Null ){
88226 return (f2&MEM_Null) - (f1&MEM_Null);
88227 }
88228
88229 /* At least one of the two values is a number
88230 */
88231 if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
88232 testcase( combined_flags & MEM_Int );
88233 testcase( combined_flags & MEM_Real );
88234 testcase( combined_flags & MEM_IntReal );
88235 if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
88236 testcase( f1 & f2 & MEM_Int );
88237 testcase( f1 & f2 & MEM_IntReal );
88238 if( pMem1->u.i < pMem2->u.i ) return -1;
88239 if( pMem1->u.i > pMem2->u.i ) return +1;
88240 return 0;
88241 }
88242 if( (f1 & f2 & MEM_Real)!=0 ){
88243 if( pMem1->u.r < pMem2->u.r ) return -1;
88244 if( pMem1->u.r > pMem2->u.r ) return +1;
88245 return 0;
88246 }
88247 if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
88248 testcase( f1 & MEM_Int );
88249 testcase( f1 & MEM_IntReal );
88250 if( (f2&MEM_Real)!=0 ){
88251 return sqlite3IntFloatCompare(i: pMem1->u.i, r: pMem2->u.r);
88252 }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
88253 if( pMem1->u.i < pMem2->u.i ) return -1;
88254 if( pMem1->u.i > pMem2->u.i ) return +1;
88255 return 0;
88256 }else{
88257 return -1;
88258 }
88259 }
88260 if( (f1&MEM_Real)!=0 ){
88261 if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
88262 testcase( f2 & MEM_Int );
88263 testcase( f2 & MEM_IntReal );
88264 return -sqlite3IntFloatCompare(i: pMem2->u.i, r: pMem1->u.r);
88265 }else{
88266 return -1;
88267 }
88268 }
88269 return +1;
88270 }
88271
88272 /* If one value is a string and the other is a blob, the string is less.
88273 ** If both are strings, compare using the collating functions.
88274 */
88275 if( combined_flags&MEM_Str ){
88276 if( (f1 & MEM_Str)==0 ){
88277 return 1;
88278 }
88279 if( (f2 & MEM_Str)==0 ){
88280 return -1;
88281 }
88282
88283 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
88284 assert( pMem1->enc==SQLITE_UTF8 ||
88285 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
88286
88287 /* The collation sequence must be defined at this point, even if
88288 ** the user deletes the collation sequence after the vdbe program is
88289 ** compiled (this was not always the case).
88290 */
88291 assert( !pColl || pColl->xCmp );
88292
88293 if( pColl ){
88294 return vdbeCompareMemString(pMem1, pMem2, pColl, prcErr: 0);
88295 }
88296 /* If a NULL pointer was passed as the collate function, fall through
88297 ** to the blob case and use memcmp(). */
88298 }
88299
88300 /* Both values must be blobs. Compare using memcmp(). */
88301 return sqlite3BlobCompare(pB1: pMem1, pB2: pMem2);
88302}
88303
88304
88305/*
88306** The first argument passed to this function is a serial-type that
88307** corresponds to an integer - all values between 1 and 9 inclusive
88308** except 7. The second points to a buffer containing an integer value
88309** serialized according to serial_type. This function deserializes
88310** and returns the value.
88311*/
88312static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
88313 u32 y;
88314 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
88315 switch( serial_type ){
88316 case 0:
88317 case 1:
88318 testcase( aKey[0]&0x80 );
88319 return ONE_BYTE_INT(aKey);
88320 case 2:
88321 testcase( aKey[0]&0x80 );
88322 return TWO_BYTE_INT(aKey);
88323 case 3:
88324 testcase( aKey[0]&0x80 );
88325 return THREE_BYTE_INT(aKey);
88326 case 4: {
88327 testcase( aKey[0]&0x80 );
88328 y = FOUR_BYTE_UINT(aKey);
88329 return (i64)*(int*)&y;
88330 }
88331 case 5: {
88332 testcase( aKey[0]&0x80 );
88333 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
88334 }
88335 case 6: {
88336 u64 x = FOUR_BYTE_UINT(aKey);
88337 testcase( aKey[0]&0x80 );
88338 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
88339 return (i64)*(i64*)&x;
88340 }
88341 }
88342
88343 return (serial_type - 8);
88344}
88345
88346/*
88347** This function compares the two table rows or index records
88348** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
88349** or positive integer if key1 is less than, equal to or
88350** greater than key2. The {nKey1, pKey1} key must be a blob
88351** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
88352** key must be a parsed key such as obtained from
88353** sqlite3VdbeParseRecord.
88354**
88355** If argument bSkip is non-zero, it is assumed that the caller has already
88356** determined that the first fields of the keys are equal.
88357**
88358** Key1 and Key2 do not have to contain the same number of fields. If all
88359** fields that appear in both keys are equal, then pPKey2->default_rc is
88360** returned.
88361**
88362** If database corruption is discovered, set pPKey2->errCode to
88363** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
88364** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
88365** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
88366*/
88367SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
88368 int nKey1, const void *pKey1, /* Left key */
88369 UnpackedRecord *pPKey2, /* Right key */
88370 int bSkip /* If true, skip the first field */
88371){
88372 u32 d1; /* Offset into aKey[] of next data element */
88373 int i; /* Index of next field to compare */
88374 u32 szHdr1; /* Size of record header in bytes */
88375 u32 idx1; /* Offset of first type in header */
88376 int rc = 0; /* Return value */
88377 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
88378 KeyInfo *pKeyInfo;
88379 const unsigned char *aKey1 = (const unsigned char *)pKey1;
88380 Mem mem1;
88381
88382 /* If bSkip is true, then the caller has already determined that the first
88383 ** two elements in the keys are equal. Fix the various stack variables so
88384 ** that this routine begins comparing at the second field. */
88385 if( bSkip ){
88386 u32 s1 = aKey1[1];
88387 if( s1<0x80 ){
88388 idx1 = 2;
88389 }else{
88390 idx1 = 1 + sqlite3GetVarint32(p: &aKey1[1], v: &s1);
88391 }
88392 szHdr1 = aKey1[0];
88393 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(serial_type: s1);
88394 i = 1;
88395 pRhs++;
88396 }else{
88397 if( (szHdr1 = aKey1[0])<0x80 ){
88398 idx1 = 1;
88399 }else{
88400 idx1 = sqlite3GetVarint32(p: aKey1, v: &szHdr1);
88401 }
88402 d1 = szHdr1;
88403 i = 0;
88404 }
88405 if( d1>(unsigned)nKey1 ){
88406 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88407 return 0; /* Corruption */
88408 }
88409
88410 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
88411 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
88412 || CORRUPT_DB );
88413 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
88414 assert( pPKey2->pKeyInfo->nKeyField>0 );
88415 assert( idx1<=szHdr1 || CORRUPT_DB );
88416 while( 1 /*exit-by-break*/ ){
88417 u32 serial_type;
88418
88419 /* RHS is an integer */
88420 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
88421 testcase( pRhs->flags & MEM_Int );
88422 testcase( pRhs->flags & MEM_IntReal );
88423 serial_type = aKey1[idx1];
88424 testcase( serial_type==12 );
88425 if( serial_type>=10 ){
88426 rc = serial_type==10 ? -1 : +1;
88427 }else if( serial_type==0 ){
88428 rc = -1;
88429 }else if( serial_type==7 ){
88430 sqlite3VdbeSerialGet(buf: &aKey1[d1], serial_type, pMem: &mem1);
88431 rc = -sqlite3IntFloatCompare(i: pRhs->u.i, r: mem1.u.r);
88432 }else{
88433 i64 lhs = vdbeRecordDecodeInt(serial_type, aKey: &aKey1[d1]);
88434 i64 rhs = pRhs->u.i;
88435 if( lhs<rhs ){
88436 rc = -1;
88437 }else if( lhs>rhs ){
88438 rc = +1;
88439 }
88440 }
88441 }
88442
88443 /* RHS is real */
88444 else if( pRhs->flags & MEM_Real ){
88445 serial_type = aKey1[idx1];
88446 if( serial_type>=10 ){
88447 /* Serial types 12 or greater are strings and blobs (greater than
88448 ** numbers). Types 10 and 11 are currently "reserved for future
88449 ** use", so it doesn't really matter what the results of comparing
88450 ** them to numeric values are. */
88451 rc = serial_type==10 ? -1 : +1;
88452 }else if( serial_type==0 ){
88453 rc = -1;
88454 }else{
88455 sqlite3VdbeSerialGet(buf: &aKey1[d1], serial_type, pMem: &mem1);
88456 if( serial_type==7 ){
88457 if( mem1.u.r<pRhs->u.r ){
88458 rc = -1;
88459 }else if( mem1.u.r>pRhs->u.r ){
88460 rc = +1;
88461 }
88462 }else{
88463 rc = sqlite3IntFloatCompare(i: mem1.u.i, r: pRhs->u.r);
88464 }
88465 }
88466 }
88467
88468 /* RHS is a string */
88469 else if( pRhs->flags & MEM_Str ){
88470 getVarint32NR(&aKey1[idx1], serial_type);
88471 testcase( serial_type==12 );
88472 if( serial_type<12 ){
88473 rc = -1;
88474 }else if( !(serial_type & 0x01) ){
88475 rc = +1;
88476 }else{
88477 mem1.n = (serial_type - 12) / 2;
88478 testcase( (d1+mem1.n)==(unsigned)nKey1 );
88479 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
88480 if( (d1+mem1.n) > (unsigned)nKey1
88481 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
88482 ){
88483 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88484 return 0; /* Corruption */
88485 }else if( pKeyInfo->aColl[i] ){
88486 mem1.enc = pKeyInfo->enc;
88487 mem1.db = pKeyInfo->db;
88488 mem1.flags = MEM_Str;
88489 mem1.z = (char*)&aKey1[d1];
88490 rc = vdbeCompareMemString(
88491 pMem1: &mem1, pMem2: pRhs, pColl: pKeyInfo->aColl[i], prcErr: &pPKey2->errCode
88492 );
88493 }else{
88494 int nCmp = MIN(mem1.n, pRhs->n);
88495 rc = memcmp(s1: &aKey1[d1], s2: pRhs->z, n: nCmp);
88496 if( rc==0 ) rc = mem1.n - pRhs->n;
88497 }
88498 }
88499 }
88500
88501 /* RHS is a blob */
88502 else if( pRhs->flags & MEM_Blob ){
88503 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
88504 getVarint32NR(&aKey1[idx1], serial_type);
88505 testcase( serial_type==12 );
88506 if( serial_type<12 || (serial_type & 0x01) ){
88507 rc = -1;
88508 }else{
88509 int nStr = (serial_type - 12) / 2;
88510 testcase( (d1+nStr)==(unsigned)nKey1 );
88511 testcase( (d1+nStr+1)==(unsigned)nKey1 );
88512 if( (d1+nStr) > (unsigned)nKey1 ){
88513 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88514 return 0; /* Corruption */
88515 }else if( pRhs->flags & MEM_Zero ){
88516 if( !isAllZero(z: (const char*)&aKey1[d1],n: nStr) ){
88517 rc = 1;
88518 }else{
88519 rc = nStr - pRhs->u.nZero;
88520 }
88521 }else{
88522 int nCmp = MIN(nStr, pRhs->n);
88523 rc = memcmp(s1: &aKey1[d1], s2: pRhs->z, n: nCmp);
88524 if( rc==0 ) rc = nStr - pRhs->n;
88525 }
88526 }
88527 }
88528
88529 /* RHS is null */
88530 else{
88531 serial_type = aKey1[idx1];
88532 rc = (serial_type!=0 && serial_type!=10);
88533 }
88534
88535 if( rc!=0 ){
88536 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
88537 if( sortFlags ){
88538 if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0
88539 || ((sortFlags & KEYINFO_ORDER_DESC)
88540 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
88541 ){
88542 rc = -rc;
88543 }
88544 }
88545 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
88546 assert( mem1.szMalloc==0 ); /* See comment below */
88547 return rc;
88548 }
88549
88550 i++;
88551 if( i==pPKey2->nField ) break;
88552 pRhs++;
88553 d1 += sqlite3VdbeSerialTypeLen(serial_type);
88554 if( d1>(unsigned)nKey1 ) break;
88555 idx1 += sqlite3VarintLen(v: serial_type);
88556 if( idx1>=(unsigned)szHdr1 ){
88557 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88558 return 0; /* Corrupt index */
88559 }
88560 }
88561
88562 /* No memory allocation is ever used on mem1. Prove this using
88563 ** the following assert(). If the assert() fails, it indicates a
88564 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
88565 assert( mem1.szMalloc==0 );
88566
88567 /* rc==0 here means that one or both of the keys ran out of fields and
88568 ** all the fields up to that point were equal. Return the default_rc
88569 ** value. */
88570 assert( CORRUPT_DB
88571 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
88572 || pPKey2->pKeyInfo->db->mallocFailed
88573 );
88574 pPKey2->eqSeen = 1;
88575 return pPKey2->default_rc;
88576}
88577SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
88578 int nKey1, const void *pKey1, /* Left key */
88579 UnpackedRecord *pPKey2 /* Right key */
88580){
88581 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, bSkip: 0);
88582}
88583
88584
88585/*
88586** This function is an optimized version of sqlite3VdbeRecordCompare()
88587** that (a) the first field of pPKey2 is an integer, and (b) the
88588** size-of-header varint at the start of (pKey1/nKey1) fits in a single
88589** byte (i.e. is less than 128).
88590**
88591** To avoid concerns about buffer overreads, this routine is only used
88592** on schemas where the maximum valid header size is 63 bytes or less.
88593*/
88594static int vdbeRecordCompareInt(
88595 int nKey1, const void *pKey1, /* Left key */
88596 UnpackedRecord *pPKey2 /* Right key */
88597){
88598 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
88599 int serial_type = ((const u8*)pKey1)[1];
88600 int res;
88601 u32 y;
88602 u64 x;
88603 i64 v;
88604 i64 lhs;
88605
88606 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
88607 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
88608 switch( serial_type ){
88609 case 1: { /* 1-byte signed integer */
88610 lhs = ONE_BYTE_INT(aKey);
88611 testcase( lhs<0 );
88612 break;
88613 }
88614 case 2: { /* 2-byte signed integer */
88615 lhs = TWO_BYTE_INT(aKey);
88616 testcase( lhs<0 );
88617 break;
88618 }
88619 case 3: { /* 3-byte signed integer */
88620 lhs = THREE_BYTE_INT(aKey);
88621 testcase( lhs<0 );
88622 break;
88623 }
88624 case 4: { /* 4-byte signed integer */
88625 y = FOUR_BYTE_UINT(aKey);
88626 lhs = (i64)*(int*)&y;
88627 testcase( lhs<0 );
88628 break;
88629 }
88630 case 5: { /* 6-byte signed integer */
88631 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
88632 testcase( lhs<0 );
88633 break;
88634 }
88635 case 6: { /* 8-byte signed integer */
88636 x = FOUR_BYTE_UINT(aKey);
88637 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
88638 lhs = *(i64*)&x;
88639 testcase( lhs<0 );
88640 break;
88641 }
88642 case 8:
88643 lhs = 0;
88644 break;
88645 case 9:
88646 lhs = 1;
88647 break;
88648
88649 /* This case could be removed without changing the results of running
88650 ** this code. Including it causes gcc to generate a faster switch
88651 ** statement (since the range of switch targets now starts at zero and
88652 ** is contiguous) but does not cause any duplicate code to be generated
88653 ** (as gcc is clever enough to combine the two like cases). Other
88654 ** compilers might be similar. */
88655 case 0: case 7:
88656 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
88657
88658 default:
88659 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
88660 }
88661
88662 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
88663 v = pPKey2->u.i;
88664 if( v>lhs ){
88665 res = pPKey2->r1;
88666 }else if( v<lhs ){
88667 res = pPKey2->r2;
88668 }else if( pPKey2->nField>1 ){
88669 /* The first fields of the two keys are equal. Compare the trailing
88670 ** fields. */
88671 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, bSkip: 1);
88672 }else{
88673 /* The first fields of the two keys are equal and there are no trailing
88674 ** fields. Return pPKey2->default_rc in this case. */
88675 res = pPKey2->default_rc;
88676 pPKey2->eqSeen = 1;
88677 }
88678
88679 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
88680 return res;
88681}
88682
88683/*
88684** This function is an optimized version of sqlite3VdbeRecordCompare()
88685** that (a) the first field of pPKey2 is a string, that (b) the first field
88686** uses the collation sequence BINARY and (c) that the size-of-header varint
88687** at the start of (pKey1/nKey1) fits in a single byte.
88688*/
88689static int vdbeRecordCompareString(
88690 int nKey1, const void *pKey1, /* Left key */
88691 UnpackedRecord *pPKey2 /* Right key */
88692){
88693 const u8 *aKey1 = (const u8*)pKey1;
88694 int serial_type;
88695 int res;
88696
88697 assert( pPKey2->aMem[0].flags & MEM_Str );
88698 assert( pPKey2->aMem[0].n == pPKey2->n );
88699 assert( pPKey2->aMem[0].z == pPKey2->u.z );
88700 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
88701 serial_type = (signed char)(aKey1[1]);
88702
88703vrcs_restart:
88704 if( serial_type<12 ){
88705 if( serial_type<0 ){
88706 sqlite3GetVarint32(p: &aKey1[1], v: (u32*)&serial_type);
88707 if( serial_type>=12 ) goto vrcs_restart;
88708 assert( CORRUPT_DB );
88709 }
88710 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
88711 }else if( !(serial_type & 0x01) ){
88712 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
88713 }else{
88714 int nCmp;
88715 int nStr;
88716 int szHdr = aKey1[0];
88717
88718 nStr = (serial_type-12) / 2;
88719 if( (szHdr + nStr) > nKey1 ){
88720 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88721 return 0; /* Corruption */
88722 }
88723 nCmp = MIN( pPKey2->n, nStr );
88724 res = memcmp(s1: &aKey1[szHdr], s2: pPKey2->u.z, n: nCmp);
88725
88726 if( res>0 ){
88727 res = pPKey2->r2;
88728 }else if( res<0 ){
88729 res = pPKey2->r1;
88730 }else{
88731 res = nStr - pPKey2->n;
88732 if( res==0 ){
88733 if( pPKey2->nField>1 ){
88734 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, bSkip: 1);
88735 }else{
88736 res = pPKey2->default_rc;
88737 pPKey2->eqSeen = 1;
88738 }
88739 }else if( res>0 ){
88740 res = pPKey2->r2;
88741 }else{
88742 res = pPKey2->r1;
88743 }
88744 }
88745 }
88746
88747 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
88748 || CORRUPT_DB
88749 || pPKey2->pKeyInfo->db->mallocFailed
88750 );
88751 return res;
88752}
88753
88754/*
88755** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
88756** suitable for comparing serialized records to the unpacked record passed
88757** as the only argument.
88758*/
88759SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
88760 /* varintRecordCompareInt() and varintRecordCompareString() both assume
88761 ** that the size-of-header varint that occurs at the start of each record
88762 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
88763 ** also assumes that it is safe to overread a buffer by at least the
88764 ** maximum possible legal header size plus 8 bytes. Because there is
88765 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
88766 ** buffer passed to varintRecordCompareInt() this makes it convenient to
88767 ** limit the size of the header to 64 bytes in cases where the first field
88768 ** is an integer.
88769 **
88770 ** The easiest way to enforce this limit is to consider only records with
88771 ** 13 fields or less. If the first field is an integer, the maximum legal
88772 ** header size is (12*5 + 1 + 1) bytes. */
88773 if( p->pKeyInfo->nAllField<=13 ){
88774 int flags = p->aMem[0].flags;
88775 if( p->pKeyInfo->aSortFlags[0] ){
88776 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
88777 return sqlite3VdbeRecordCompare;
88778 }
88779 p->r1 = 1;
88780 p->r2 = -1;
88781 }else{
88782 p->r1 = -1;
88783 p->r2 = 1;
88784 }
88785 if( (flags & MEM_Int) ){
88786 p->u.i = p->aMem[0].u.i;
88787 return vdbeRecordCompareInt;
88788 }
88789 testcase( flags & MEM_Real );
88790 testcase( flags & MEM_Null );
88791 testcase( flags & MEM_Blob );
88792 if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
88793 && p->pKeyInfo->aColl[0]==0
88794 ){
88795 assert( flags & MEM_Str );
88796 p->u.z = p->aMem[0].z;
88797 p->n = p->aMem[0].n;
88798 return vdbeRecordCompareString;
88799 }
88800 }
88801
88802 return sqlite3VdbeRecordCompare;
88803}
88804
88805/*
88806** pCur points at an index entry created using the OP_MakeRecord opcode.
88807** Read the rowid (the last field in the record) and store it in *rowid.
88808** Return SQLITE_OK if everything works, or an error code otherwise.
88809**
88810** pCur might be pointing to text obtained from a corrupt database file.
88811** So the content cannot be trusted. Do appropriate checks on the content.
88812*/
88813SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
88814 i64 nCellKey = 0;
88815 int rc;
88816 u32 szHdr; /* Size of the header */
88817 u32 typeRowid; /* Serial type of the rowid */
88818 u32 lenRowid; /* Size of the rowid */
88819 Mem m, v;
88820
88821 /* Get the size of the index entry. Only indices entries of less
88822 ** than 2GiB are support - anything large must be database corruption.
88823 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
88824 ** this code can safely assume that nCellKey is 32-bits
88825 */
88826 assert( sqlite3BtreeCursorIsValid(pCur) );
88827 nCellKey = sqlite3BtreePayloadSize(pCur);
88828 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
88829
88830 /* Read in the complete content of the index entry */
88831 sqlite3VdbeMemInit(pMem: &m, db, flags: 0);
88832 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, amt: (u32)nCellKey, pMem: &m);
88833 if( rc ){
88834 return rc;
88835 }
88836
88837 /* The index entry must begin with a header size */
88838 getVarint32NR((u8*)m.z, szHdr);
88839 testcase( szHdr==3 );
88840 testcase( szHdr==(u32)m.n );
88841 testcase( szHdr>0x7fffffff );
88842 assert( m.n>=0 );
88843 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
88844 goto idx_rowid_corruption;
88845 }
88846
88847 /* The last field of the index should be an integer - the ROWID.
88848 ** Verify that the last entry really is an integer. */
88849 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
88850 testcase( typeRowid==1 );
88851 testcase( typeRowid==2 );
88852 testcase( typeRowid==3 );
88853 testcase( typeRowid==4 );
88854 testcase( typeRowid==5 );
88855 testcase( typeRowid==6 );
88856 testcase( typeRowid==8 );
88857 testcase( typeRowid==9 );
88858 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
88859 goto idx_rowid_corruption;
88860 }
88861 lenRowid = sqlite3SmallTypeSizes[typeRowid];
88862 testcase( (u32)m.n==szHdr+lenRowid );
88863 if( unlikely((u32)m.n<szHdr+lenRowid) ){
88864 goto idx_rowid_corruption;
88865 }
88866
88867 /* Fetch the integer off the end of the index record */
88868 sqlite3VdbeSerialGet(buf: (u8*)&m.z[m.n-lenRowid], serial_type: typeRowid, pMem: &v);
88869 *rowid = v.u.i;
88870 sqlite3VdbeMemReleaseMalloc(p: &m);
88871 return SQLITE_OK;
88872
88873 /* Jump here if database corruption is detected after m has been
88874 ** allocated. Free the m object and return SQLITE_CORRUPT. */
88875idx_rowid_corruption:
88876 testcase( m.szMalloc!=0 );
88877 sqlite3VdbeMemReleaseMalloc(p: &m);
88878 return SQLITE_CORRUPT_BKPT;
88879}
88880
88881/*
88882** Compare the key of the index entry that cursor pC is pointing to against
88883** the key string in pUnpacked. Write into *pRes a number
88884** that is negative, zero, or positive if pC is less than, equal to,
88885** or greater than pUnpacked. Return SQLITE_OK on success.
88886**
88887** pUnpacked is either created without a rowid or is truncated so that it
88888** omits the rowid at the end. The rowid at the end of the index entry
88889** is ignored as well. Hence, this routine only compares the prefixes
88890** of the keys prior to the final rowid, not the entire key.
88891*/
88892SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
88893 sqlite3 *db, /* Database connection */
88894 VdbeCursor *pC, /* The cursor to compare against */
88895 UnpackedRecord *pUnpacked, /* Unpacked version of key */
88896 int *res /* Write the comparison result here */
88897){
88898 i64 nCellKey = 0;
88899 int rc;
88900 BtCursor *pCur;
88901 Mem m;
88902
88903 assert( pC->eCurType==CURTYPE_BTREE );
88904 pCur = pC->uc.pCursor;
88905 assert( sqlite3BtreeCursorIsValid(pCur) );
88906 nCellKey = sqlite3BtreePayloadSize(pCur);
88907 /* nCellKey will always be between 0 and 0xffffffff because of the way
88908 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
88909 if( nCellKey<=0 || nCellKey>0x7fffffff ){
88910 *res = 0;
88911 return SQLITE_CORRUPT_BKPT;
88912 }
88913 sqlite3VdbeMemInit(pMem: &m, db, flags: 0);
88914 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, amt: (u32)nCellKey, pMem: &m);
88915 if( rc ){
88916 return rc;
88917 }
88918 *res = sqlite3VdbeRecordCompareWithSkip(nKey1: m.n, pKey1: m.z, pPKey2: pUnpacked, bSkip: 0);
88919 sqlite3VdbeMemReleaseMalloc(p: &m);
88920 return SQLITE_OK;
88921}
88922
88923/*
88924** This routine sets the value to be returned by subsequent calls to
88925** sqlite3_changes() on the database handle 'db'.
88926*/
88927SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){
88928 assert( sqlite3_mutex_held(db->mutex) );
88929 db->nChange = nChange;
88930 db->nTotalChange += nChange;
88931}
88932
88933/*
88934** Set a flag in the vdbe to update the change counter when it is finalised
88935** or reset.
88936*/
88937SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
88938 v->changeCntOn = 1;
88939}
88940
88941/*
88942** Mark every prepared statement associated with a database connection
88943** as expired.
88944**
88945** An expired statement means that recompilation of the statement is
88946** recommend. Statements expire when things happen that make their
88947** programs obsolete. Removing user-defined functions or collating
88948** sequences, or changing an authorization function are the types of
88949** things that make prepared statements obsolete.
88950**
88951** If iCode is 1, then expiration is advisory. The statement should
88952** be reprepared before being restarted, but if it is already running
88953** it is allowed to run to completion.
88954**
88955** Internally, this function just sets the Vdbe.expired flag on all
88956** prepared statements. The flag is set to 1 for an immediate expiration
88957** and set to 2 for an advisory expiration.
88958*/
88959SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
88960 Vdbe *p;
88961 for(p = db->pVdbe; p; p=p->pVNext){
88962 p->expired = iCode+1;
88963 }
88964}
88965
88966/*
88967** Return the database associated with the Vdbe.
88968*/
88969SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
88970 return v->db;
88971}
88972
88973/*
88974** Return the SQLITE_PREPARE flags for a Vdbe.
88975*/
88976SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
88977 return v->prepFlags;
88978}
88979
88980/*
88981** Return a pointer to an sqlite3_value structure containing the value bound
88982** parameter iVar of VM v. Except, if the value is an SQL NULL, return
88983** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
88984** constants) to the value before returning it.
88985**
88986** The returned value must be freed by the caller using sqlite3ValueFree().
88987*/
88988SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
88989 assert( iVar>0 );
88990 if( v ){
88991 Mem *pMem = &v->aVar[iVar-1];
88992 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
88993 if( 0==(pMem->flags & MEM_Null) ){
88994 sqlite3_value *pRet = sqlite3ValueNew(db: v->db);
88995 if( pRet ){
88996 sqlite3VdbeMemCopy(pTo: (Mem *)pRet, pFrom: pMem);
88997 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
88998 }
88999 return pRet;
89000 }
89001 }
89002 return 0;
89003}
89004
89005/*
89006** Configure SQL variable iVar so that binding a new value to it signals
89007** to sqlite3_reoptimize() that re-preparing the statement may result
89008** in a better query plan.
89009*/
89010SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
89011 assert( iVar>0 );
89012 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
89013 if( iVar>=32 ){
89014 v->expmask |= 0x80000000;
89015 }else{
89016 v->expmask |= ((u32)1 << (iVar-1));
89017 }
89018}
89019
89020/*
89021** Cause a function to throw an error if it was call from OP_PureFunc
89022** rather than OP_Function.
89023**
89024** OP_PureFunc means that the function must be deterministic, and should
89025** throw an error if it is given inputs that would make it non-deterministic.
89026** This routine is invoked by date/time functions that use non-deterministic
89027** features such as 'now'.
89028*/
89029SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
89030 const VdbeOp *pOp;
89031#ifdef SQLITE_ENABLE_STAT4
89032 if( pCtx->pVdbe==0 ) return 1;
89033#endif
89034 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
89035 if( pOp->opcode==OP_PureFunc ){
89036 const char *zContext;
89037 char *zMsg;
89038 if( pOp->p5 & NC_IsCheck ){
89039 zContext = "a CHECK constraint";
89040 }else if( pOp->p5 & NC_GenCol ){
89041 zContext = "a generated column";
89042 }else{
89043 zContext = "an index";
89044 }
89045 zMsg = sqlite3_mprintf(zFormat: "non-deterministic use of %s() in %s",
89046 pCtx->pFunc->zName, zContext);
89047 sqlite3_result_error(pCtx, zMsg, -1);
89048 sqlite3_free(p: zMsg);
89049 return 0;
89050 }
89051 return 1;
89052}
89053
89054#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
89055/*
89056** This Walker callback is used to help verify that calls to
89057** sqlite3BtreeCursorHint() with opcode BTREE_HINT_RANGE have
89058** byte-code register values correctly initialized.
89059*/
89060SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){
89061 if( pExpr->op==TK_REGISTER ){
89062 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
89063 }
89064 return WRC_Continue;
89065}
89066#endif /* SQLITE_ENABLE_CURSOR_HINTS && SQLITE_DEBUG */
89067
89068#ifndef SQLITE_OMIT_VIRTUALTABLE
89069/*
89070** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
89071** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
89072** in memory obtained from sqlite3DbMalloc).
89073*/
89074SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
89075 if( pVtab->zErrMsg ){
89076 sqlite3 *db = p->db;
89077 sqlite3DbFree(db, p: p->zErrMsg);
89078 p->zErrMsg = sqlite3DbStrDup(db, z: pVtab->zErrMsg);
89079 sqlite3_free(p: pVtab->zErrMsg);
89080 pVtab->zErrMsg = 0;
89081 }
89082}
89083#endif /* SQLITE_OMIT_VIRTUALTABLE */
89084
89085#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
89086
89087/*
89088** If the second argument is not NULL, release any allocations associated
89089** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
89090** structure itself, using sqlite3DbFree().
89091**
89092** This function is used to free UnpackedRecord structures allocated by
89093** the vdbeUnpackRecord() function found in vdbeapi.c.
89094*/
89095static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
89096 assert( db!=0 );
89097 if( p ){
89098 int i;
89099 for(i=0; i<nField; i++){
89100 Mem *pMem = &p->aMem[i];
89101 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
89102 }
89103 sqlite3DbNNFreeNN(db, p);
89104 }
89105}
89106#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
89107
89108#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
89109/*
89110** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
89111** then cursor passed as the second argument should point to the row about
89112** to be update or deleted. If the application calls sqlite3_preupdate_old(),
89113** the required value will be read from the row the cursor points to.
89114*/
89115SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
89116 Vdbe *v, /* Vdbe pre-update hook is invoked by */
89117 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
89118 int op, /* SQLITE_INSERT, UPDATE or DELETE */
89119 const char *zDb, /* Database name */
89120 Table *pTab, /* Modified table */
89121 i64 iKey1, /* Initial key value */
89122 int iReg, /* Register for new.* record */
89123 int iBlobWrite
89124){
89125 sqlite3 *db = v->db;
89126 i64 iKey2;
89127 PreUpdate preupdate;
89128 const char *zTbl = pTab->zName;
89129 static const u8 fakeSortOrder = 0;
89130#ifdef SQLITE_DEBUG
89131 int nRealCol;
89132 if( pTab->tabFlags & TF_WithoutRowid ){
89133 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
89134 }else if( pTab->tabFlags & TF_HasVirtual ){
89135 nRealCol = pTab->nNVCol;
89136 }else{
89137 nRealCol = pTab->nCol;
89138 }
89139#endif
89140
89141 assert( db->pPreUpdate==0 );
89142 memset(&preupdate, 0, sizeof(PreUpdate));
89143 if( HasRowid(pTab)==0 ){
89144 iKey1 = iKey2 = 0;
89145 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
89146 }else{
89147 if( op==SQLITE_UPDATE ){
89148 iKey2 = v->aMem[iReg].u.i;
89149 }else{
89150 iKey2 = iKey1;
89151 }
89152 }
89153
89154 assert( pCsr!=0 );
89155 assert( pCsr->eCurType==CURTYPE_BTREE );
89156 assert( pCsr->nField==nRealCol
89157 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
89158 );
89159
89160 preupdate.v = v;
89161 preupdate.pCsr = pCsr;
89162 preupdate.op = op;
89163 preupdate.iNewReg = iReg;
89164 preupdate.keyinfo.db = db;
89165 preupdate.keyinfo.enc = ENC(db);
89166 preupdate.keyinfo.nKeyField = pTab->nCol;
89167 preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
89168 preupdate.iKey1 = iKey1;
89169 preupdate.iKey2 = iKey2;
89170 preupdate.pTab = pTab;
89171 preupdate.iBlobWrite = iBlobWrite;
89172
89173 db->pPreUpdate = &preupdate;
89174 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
89175 db->pPreUpdate = 0;
89176 sqlite3DbFree(db, preupdate.aRecord);
89177 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
89178 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
89179 if( preupdate.aNew ){
89180 int i;
89181 for(i=0; i<pCsr->nField; i++){
89182 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
89183 }
89184 sqlite3DbNNFreeNN(db, preupdate.aNew);
89185 }
89186}
89187#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
89188
89189/************** End of vdbeaux.c *********************************************/
89190/************** Begin file vdbeapi.c *****************************************/
89191/*
89192** 2004 May 26
89193**
89194** The author disclaims copyright to this source code. In place of
89195** a legal notice, here is a blessing:
89196**
89197** May you do good and not evil.
89198** May you find forgiveness for yourself and forgive others.
89199** May you share freely, never taking more than you give.
89200**
89201*************************************************************************
89202**
89203** This file contains code use to implement APIs that are part of the
89204** VDBE.
89205*/
89206/* #include "sqliteInt.h" */
89207/* #include "vdbeInt.h" */
89208/* #include "opcodes.h" */
89209
89210#ifndef SQLITE_OMIT_DEPRECATED
89211/*
89212** Return TRUE (non-zero) of the statement supplied as an argument needs
89213** to be recompiled. A statement needs to be recompiled whenever the
89214** execution environment changes in a way that would alter the program
89215** that sqlite3_prepare() generates. For example, if new functions or
89216** collating sequences are registered or if an authorizer function is
89217** added or changed.
89218*/
89219SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
89220 Vdbe *p = (Vdbe*)pStmt;
89221 return p==0 || p->expired;
89222}
89223#endif
89224
89225/*
89226** Check on a Vdbe to make sure it has not been finalized. Log
89227** an error and return true if it has been finalized (or is otherwise
89228** invalid). Return false if it is ok.
89229*/
89230static int vdbeSafety(Vdbe *p){
89231 if( p->db==0 ){
89232 sqlite3_log(SQLITE_MISUSE, zFormat: "API called with finalized prepared statement");
89233 return 1;
89234 }else{
89235 return 0;
89236 }
89237}
89238static int vdbeSafetyNotNull(Vdbe *p){
89239 if( p==0 ){
89240 sqlite3_log(SQLITE_MISUSE, zFormat: "API called with NULL prepared statement");
89241 return 1;
89242 }else{
89243 return vdbeSafety(p);
89244 }
89245}
89246
89247#ifndef SQLITE_OMIT_TRACE
89248/*
89249** Invoke the profile callback. This routine is only called if we already
89250** know that the profile callback is defined and needs to be invoked.
89251*/
89252static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
89253 sqlite3_int64 iNow;
89254 sqlite3_int64 iElapse;
89255 assert( p->startTime>0 );
89256 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
89257 assert( db->init.busy==0 );
89258 assert( p->zSql!=0 );
89259 sqlite3OsCurrentTimeInt64(pVfs: db->pVfs, pTimeOut: &iNow);
89260 iElapse = (iNow - p->startTime)*1000000;
89261#ifndef SQLITE_OMIT_DEPRECATED
89262 if( db->xProfile ){
89263 db->xProfile(db->pProfileArg, p->zSql, iElapse);
89264 }
89265#endif
89266 if( db->mTrace & SQLITE_TRACE_PROFILE ){
89267 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
89268 }
89269 p->startTime = 0;
89270}
89271/*
89272** The checkProfileCallback(DB,P) macro checks to see if a profile callback
89273** is needed, and it invokes the callback if it is needed.
89274*/
89275# define checkProfileCallback(DB,P) \
89276 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
89277#else
89278# define checkProfileCallback(DB,P) /*no-op*/
89279#endif
89280
89281/*
89282** The following routine destroys a virtual machine that is created by
89283** the sqlite3_compile() routine. The integer returned is an SQLITE_
89284** success/failure code that describes the result of executing the virtual
89285** machine.
89286**
89287** This routine sets the error code and string returned by
89288** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
89289*/
89290SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
89291 int rc;
89292 if( pStmt==0 ){
89293 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
89294 ** pointer is a harmless no-op. */
89295 rc = SQLITE_OK;
89296 }else{
89297 Vdbe *v = (Vdbe*)pStmt;
89298 sqlite3 *db = v->db;
89299 if( vdbeSafety(p: v) ) return SQLITE_MISUSE_BKPT;
89300 sqlite3_mutex_enter(p: db->mutex);
89301 checkProfileCallback(db, v);
89302 assert( v->eVdbeState>=VDBE_READY_STATE );
89303 rc = sqlite3VdbeReset(p: v);
89304 sqlite3VdbeDelete(p: v);
89305 rc = sqlite3ApiExit(db, rc);
89306 sqlite3LeaveMutexAndCloseZombie(db);
89307 }
89308 return rc;
89309}
89310
89311/*
89312** Terminate the current execution of an SQL statement and reset it
89313** back to its starting state so that it can be reused. A success code from
89314** the prior execution is returned.
89315**
89316** This routine sets the error code and string returned by
89317** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
89318*/
89319SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
89320 int rc;
89321 if( pStmt==0 ){
89322 rc = SQLITE_OK;
89323 }else{
89324 Vdbe *v = (Vdbe*)pStmt;
89325 sqlite3 *db = v->db;
89326 sqlite3_mutex_enter(p: db->mutex);
89327 checkProfileCallback(db, v);
89328 rc = sqlite3VdbeReset(p: v);
89329 sqlite3VdbeRewind(p: v);
89330 assert( (rc & (db->errMask))==rc );
89331 rc = sqlite3ApiExit(db, rc);
89332 sqlite3_mutex_leave(p: db->mutex);
89333 }
89334 return rc;
89335}
89336
89337/*
89338** Set all the parameters in the compiled SQL statement to NULL.
89339*/
89340SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
89341 int i;
89342 int rc = SQLITE_OK;
89343 Vdbe *p = (Vdbe*)pStmt;
89344#if SQLITE_THREADSAFE
89345 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
89346#endif
89347 sqlite3_mutex_enter(p: mutex);
89348 for(i=0; i<p->nVar; i++){
89349 sqlite3VdbeMemRelease(p: &p->aVar[i]);
89350 p->aVar[i].flags = MEM_Null;
89351 }
89352 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
89353 if( p->expmask ){
89354 p->expired = 1;
89355 }
89356 sqlite3_mutex_leave(p: mutex);
89357 return rc;
89358}
89359
89360
89361/**************************** sqlite3_value_ *******************************
89362** The following routines extract information from a Mem or sqlite3_value
89363** structure.
89364*/
89365SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
89366 Mem *p = (Mem*)pVal;
89367 if( p->flags & (MEM_Blob|MEM_Str) ){
89368 if( ExpandBlob(p)!=SQLITE_OK ){
89369 assert( p->flags==MEM_Null && p->z==0 );
89370 return 0;
89371 }
89372 p->flags |= MEM_Blob;
89373 return p->n ? p->z : 0;
89374 }else{
89375 return sqlite3_value_text(pVal);
89376 }
89377}
89378SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
89379 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
89380}
89381SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
89382 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
89383}
89384SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
89385 return sqlite3VdbeRealValue(pMem: (Mem*)pVal);
89386}
89387SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
89388 return (int)sqlite3VdbeIntValue(pMem: (Mem*)pVal);
89389}
89390SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
89391 return sqlite3VdbeIntValue(pMem: (Mem*)pVal);
89392}
89393SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
89394 Mem *pMem = (Mem*)pVal;
89395 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
89396}
89397SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
89398 Mem *p = (Mem*)pVal;
89399 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
89400 (MEM_Null|MEM_Term|MEM_Subtype)
89401 && zPType!=0
89402 && p->eSubtype=='p'
89403 && strcmp(s1: p->u.zPType, s2: zPType)==0
89404 ){
89405 return (void*)p->z;
89406 }else{
89407 return 0;
89408 }
89409}
89410SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
89411 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
89412}
89413#ifndef SQLITE_OMIT_UTF16
89414SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
89415 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
89416}
89417SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
89418 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
89419}
89420SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
89421 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
89422}
89423#endif /* SQLITE_OMIT_UTF16 */
89424/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
89425** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
89426** point number string BLOB NULL
89427*/
89428SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
89429 static const u8 aType[] = {
89430 SQLITE_BLOB, /* 0x00 (not possible) */
89431 SQLITE_NULL, /* 0x01 NULL */
89432 SQLITE_TEXT, /* 0x02 TEXT */
89433 SQLITE_NULL, /* 0x03 (not possible) */
89434 SQLITE_INTEGER, /* 0x04 INTEGER */
89435 SQLITE_NULL, /* 0x05 (not possible) */
89436 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */
89437 SQLITE_NULL, /* 0x07 (not possible) */
89438 SQLITE_FLOAT, /* 0x08 FLOAT */
89439 SQLITE_NULL, /* 0x09 (not possible) */
89440 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */
89441 SQLITE_NULL, /* 0x0b (not possible) */
89442 SQLITE_INTEGER, /* 0x0c (not possible) */
89443 SQLITE_NULL, /* 0x0d (not possible) */
89444 SQLITE_INTEGER, /* 0x0e (not possible) */
89445 SQLITE_NULL, /* 0x0f (not possible) */
89446 SQLITE_BLOB, /* 0x10 BLOB */
89447 SQLITE_NULL, /* 0x11 (not possible) */
89448 SQLITE_TEXT, /* 0x12 (not possible) */
89449 SQLITE_NULL, /* 0x13 (not possible) */
89450 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */
89451 SQLITE_NULL, /* 0x15 (not possible) */
89452 SQLITE_INTEGER, /* 0x16 (not possible) */
89453 SQLITE_NULL, /* 0x17 (not possible) */
89454 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */
89455 SQLITE_NULL, /* 0x19 (not possible) */
89456 SQLITE_FLOAT, /* 0x1a (not possible) */
89457 SQLITE_NULL, /* 0x1b (not possible) */
89458 SQLITE_INTEGER, /* 0x1c (not possible) */
89459 SQLITE_NULL, /* 0x1d (not possible) */
89460 SQLITE_INTEGER, /* 0x1e (not possible) */
89461 SQLITE_NULL, /* 0x1f (not possible) */
89462 SQLITE_FLOAT, /* 0x20 INTREAL */
89463 SQLITE_NULL, /* 0x21 (not possible) */
89464 SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */
89465 SQLITE_NULL, /* 0x23 (not possible) */
89466 SQLITE_FLOAT, /* 0x24 (not possible) */
89467 SQLITE_NULL, /* 0x25 (not possible) */
89468 SQLITE_FLOAT, /* 0x26 (not possible) */
89469 SQLITE_NULL, /* 0x27 (not possible) */
89470 SQLITE_FLOAT, /* 0x28 (not possible) */
89471 SQLITE_NULL, /* 0x29 (not possible) */
89472 SQLITE_FLOAT, /* 0x2a (not possible) */
89473 SQLITE_NULL, /* 0x2b (not possible) */
89474 SQLITE_FLOAT, /* 0x2c (not possible) */
89475 SQLITE_NULL, /* 0x2d (not possible) */
89476 SQLITE_FLOAT, /* 0x2e (not possible) */
89477 SQLITE_NULL, /* 0x2f (not possible) */
89478 SQLITE_BLOB, /* 0x30 (not possible) */
89479 SQLITE_NULL, /* 0x31 (not possible) */
89480 SQLITE_TEXT, /* 0x32 (not possible) */
89481 SQLITE_NULL, /* 0x33 (not possible) */
89482 SQLITE_FLOAT, /* 0x34 (not possible) */
89483 SQLITE_NULL, /* 0x35 (not possible) */
89484 SQLITE_FLOAT, /* 0x36 (not possible) */
89485 SQLITE_NULL, /* 0x37 (not possible) */
89486 SQLITE_FLOAT, /* 0x38 (not possible) */
89487 SQLITE_NULL, /* 0x39 (not possible) */
89488 SQLITE_FLOAT, /* 0x3a (not possible) */
89489 SQLITE_NULL, /* 0x3b (not possible) */
89490 SQLITE_FLOAT, /* 0x3c (not possible) */
89491 SQLITE_NULL, /* 0x3d (not possible) */
89492 SQLITE_FLOAT, /* 0x3e (not possible) */
89493 SQLITE_NULL, /* 0x3f (not possible) */
89494 };
89495#ifdef SQLITE_DEBUG
89496 {
89497 int eType = SQLITE_BLOB;
89498 if( pVal->flags & MEM_Null ){
89499 eType = SQLITE_NULL;
89500 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
89501 eType = SQLITE_FLOAT;
89502 }else if( pVal->flags & MEM_Int ){
89503 eType = SQLITE_INTEGER;
89504 }else if( pVal->flags & MEM_Str ){
89505 eType = SQLITE_TEXT;
89506 }
89507 assert( eType == aType[pVal->flags&MEM_AffMask] );
89508 }
89509#endif
89510 return aType[pVal->flags&MEM_AffMask];
89511}
89512SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
89513 return pVal->enc;
89514}
89515
89516/* Return true if a parameter to xUpdate represents an unchanged column */
89517SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
89518 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
89519}
89520
89521/* Return true if a parameter value originated from an sqlite3_bind() */
89522SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
89523 return (pVal->flags&MEM_FromBind)!=0;
89524}
89525
89526/* Make a copy of an sqlite3_value object
89527*/
89528SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
89529 sqlite3_value *pNew;
89530 if( pOrig==0 ) return 0;
89531 pNew = sqlite3_malloc( n: sizeof(*pNew) );
89532 if( pNew==0 ) return 0;
89533 memset(s: pNew, c: 0, n: sizeof(*pNew));
89534 memcpy(dest: pNew, src: pOrig, MEMCELLSIZE);
89535 pNew->flags &= ~MEM_Dyn;
89536 pNew->db = 0;
89537 if( pNew->flags&(MEM_Str|MEM_Blob) ){
89538 pNew->flags &= ~(MEM_Static|MEM_Dyn);
89539 pNew->flags |= MEM_Ephem;
89540 if( sqlite3VdbeMemMakeWriteable(pMem: pNew)!=SQLITE_OK ){
89541 sqlite3ValueFree(v: pNew);
89542 pNew = 0;
89543 }
89544 }else if( pNew->flags & MEM_Null ){
89545 /* Do not duplicate pointer values */
89546 pNew->flags &= ~(MEM_Term|MEM_Subtype);
89547 }
89548 return pNew;
89549}
89550
89551/* Destroy an sqlite3_value object previously obtained from
89552** sqlite3_value_dup().
89553*/
89554SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
89555 sqlite3ValueFree(v: pOld);
89556}
89557
89558
89559/**************************** sqlite3_result_ *******************************
89560** The following routines are used by user-defined functions to specify
89561** the function result.
89562**
89563** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
89564** result as a string or blob. Appropriate errors are set if the string/blob
89565** is too big or if an OOM occurs.
89566**
89567** The invokeValueDestructor(P,X) routine invokes destructor function X()
89568** on value P is not going to be used and need to be destroyed.
89569*/
89570static void setResultStrOrError(
89571 sqlite3_context *pCtx, /* Function context */
89572 const char *z, /* String pointer */
89573 int n, /* Bytes in string, or negative */
89574 u8 enc, /* Encoding of z. 0 for BLOBs */
89575 void (*xDel)(void*) /* Destructor function */
89576){
89577 Mem *pOut = pCtx->pOut;
89578 int rc = sqlite3VdbeMemSetStr(pMem: pOut, z, n, enc, xDel);
89579 if( rc ){
89580 if( rc==SQLITE_TOOBIG ){
89581 sqlite3_result_error_toobig(pCtx);
89582 }else{
89583 /* The only errors possible from sqlite3VdbeMemSetStr are
89584 ** SQLITE_TOOBIG and SQLITE_NOMEM */
89585 assert( rc==SQLITE_NOMEM );
89586 sqlite3_result_error_nomem(pCtx);
89587 }
89588 return;
89589 }
89590 sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: pCtx->enc);
89591 if( sqlite3VdbeMemTooBig(p: pOut) ){
89592 sqlite3_result_error_toobig(pCtx);
89593 }
89594}
89595static int invokeValueDestructor(
89596 const void *p, /* Value to destroy */
89597 void (*xDel)(void*), /* The destructor */
89598 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
89599){
89600 assert( xDel!=SQLITE_DYNAMIC );
89601 if( xDel==0 ){
89602 /* noop */
89603 }else if( xDel==SQLITE_TRANSIENT ){
89604 /* noop */
89605 }else{
89606 xDel((void*)p);
89607 }
89608 sqlite3_result_error_toobig(pCtx);
89609 return SQLITE_TOOBIG;
89610}
89611SQLITE_API void sqlite3_result_blob(
89612 sqlite3_context *pCtx,
89613 const void *z,
89614 int n,
89615 void (*xDel)(void *)
89616){
89617 assert( n>=0 );
89618 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89619 setResultStrOrError(pCtx, z, n, enc: 0, xDel);
89620}
89621SQLITE_API void sqlite3_result_blob64(
89622 sqlite3_context *pCtx,
89623 const void *z,
89624 sqlite3_uint64 n,
89625 void (*xDel)(void *)
89626){
89627 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89628 assert( xDel!=SQLITE_DYNAMIC );
89629 if( n>0x7fffffff ){
89630 (void)invokeValueDestructor(p: z, xDel, pCtx);
89631 }else{
89632 setResultStrOrError(pCtx, z, n: (int)n, enc: 0, xDel);
89633 }
89634}
89635SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
89636 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89637 sqlite3VdbeMemSetDouble(pMem: pCtx->pOut, val: rVal);
89638}
89639SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
89640 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89641 pCtx->isError = SQLITE_ERROR;
89642 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
89643}
89644#ifndef SQLITE_OMIT_UTF16
89645SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
89646 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89647 pCtx->isError = SQLITE_ERROR;
89648 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
89649}
89650#endif
89651SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
89652 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89653 sqlite3VdbeMemSetInt64(pMem: pCtx->pOut, val: (i64)iVal);
89654}
89655SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
89656 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89657 sqlite3VdbeMemSetInt64(pMem: pCtx->pOut, val: iVal);
89658}
89659SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
89660 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89661 sqlite3VdbeMemSetNull(pMem: pCtx->pOut);
89662}
89663SQLITE_API void sqlite3_result_pointer(
89664 sqlite3_context *pCtx,
89665 void *pPtr,
89666 const char *zPType,
89667 void (*xDestructor)(void*)
89668){
89669 Mem *pOut = pCtx->pOut;
89670 assert( sqlite3_mutex_held(pOut->db->mutex) );
89671 sqlite3VdbeMemRelease(p: pOut);
89672 pOut->flags = MEM_Null;
89673 sqlite3VdbeMemSetPointer(pMem: pOut, pPtr, zPType, xDestructor);
89674}
89675SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
89676 Mem *pOut = pCtx->pOut;
89677 assert( sqlite3_mutex_held(pOut->db->mutex) );
89678 pOut->eSubtype = eSubtype & 0xff;
89679 pOut->flags |= MEM_Subtype;
89680}
89681SQLITE_API void sqlite3_result_text(
89682 sqlite3_context *pCtx,
89683 const char *z,
89684 int n,
89685 void (*xDel)(void *)
89686){
89687 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89688 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
89689}
89690SQLITE_API void sqlite3_result_text64(
89691 sqlite3_context *pCtx,
89692 const char *z,
89693 sqlite3_uint64 n,
89694 void (*xDel)(void *),
89695 unsigned char enc
89696){
89697 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89698 assert( xDel!=SQLITE_DYNAMIC );
89699 if( enc!=SQLITE_UTF8 ){
89700 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
89701 n &= ~(u64)1;
89702 }
89703 if( n>0x7fffffff ){
89704 (void)invokeValueDestructor(p: z, xDel, pCtx);
89705 }else{
89706 setResultStrOrError(pCtx, z, n: (int)n, enc, xDel);
89707 sqlite3VdbeMemZeroTerminateIfAble(pMem: pCtx->pOut);
89708 }
89709}
89710#ifndef SQLITE_OMIT_UTF16
89711SQLITE_API void sqlite3_result_text16(
89712 sqlite3_context *pCtx,
89713 const void *z,
89714 int n,
89715 void (*xDel)(void *)
89716){
89717 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89718 setResultStrOrError(pCtx, z, n: n & ~(u64)1, SQLITE_UTF16NATIVE, xDel);
89719}
89720SQLITE_API void sqlite3_result_text16be(
89721 sqlite3_context *pCtx,
89722 const void *z,
89723 int n,
89724 void (*xDel)(void *)
89725){
89726 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89727 setResultStrOrError(pCtx, z, n: n & ~(u64)1, SQLITE_UTF16BE, xDel);
89728}
89729SQLITE_API void sqlite3_result_text16le(
89730 sqlite3_context *pCtx,
89731 const void *z,
89732 int n,
89733 void (*xDel)(void *)
89734){
89735 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89736 setResultStrOrError(pCtx, z, n: n & ~(u64)1, SQLITE_UTF16LE, xDel);
89737}
89738#endif /* SQLITE_OMIT_UTF16 */
89739SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
89740 Mem *pOut = pCtx->pOut;
89741 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89742 sqlite3VdbeMemCopy(pTo: pOut, pFrom: pValue);
89743 sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: pCtx->enc);
89744 if( sqlite3VdbeMemTooBig(p: pOut) ){
89745 sqlite3_result_error_toobig(pCtx);
89746 }
89747}
89748SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
89749 sqlite3_result_zeroblob64(pCtx, n: n>0 ? n : 0);
89750}
89751SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
89752 Mem *pOut = pCtx->pOut;
89753 assert( sqlite3_mutex_held(pOut->db->mutex) );
89754 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
89755 sqlite3_result_error_toobig(pCtx);
89756 return SQLITE_TOOBIG;
89757 }
89758#ifndef SQLITE_OMIT_INCRBLOB
89759 sqlite3VdbeMemSetZeroBlob(pMem: pCtx->pOut, n: (int)n);
89760 return SQLITE_OK;
89761#else
89762 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
89763#endif
89764}
89765SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
89766 pCtx->isError = errCode ? errCode : -1;
89767#ifdef SQLITE_DEBUG
89768 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
89769#endif
89770 if( pCtx->pOut->flags & MEM_Null ){
89771 setResultStrOrError(pCtx, z: sqlite3ErrStr(errCode), n: -1, SQLITE_UTF8,
89772 SQLITE_STATIC);
89773 }
89774}
89775
89776/* Force an SQLITE_TOOBIG error. */
89777SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
89778 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89779 pCtx->isError = SQLITE_TOOBIG;
89780 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z: "string or blob too big", n: -1,
89781 SQLITE_UTF8, SQLITE_STATIC);
89782}
89783
89784/* An SQLITE_NOMEM error. */
89785SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
89786 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89787 sqlite3VdbeMemSetNull(pMem: pCtx->pOut);
89788 pCtx->isError = SQLITE_NOMEM_BKPT;
89789 sqlite3OomFault(db: pCtx->pOut->db);
89790}
89791
89792#ifndef SQLITE_UNTESTABLE
89793/* Force the INT64 value currently stored as the result to be
89794** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
89795** test-control.
89796*/
89797SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context *pCtx){
89798 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89799 if( pCtx->pOut->flags & MEM_Int ){
89800 pCtx->pOut->flags &= ~MEM_Int;
89801 pCtx->pOut->flags |= MEM_IntReal;
89802 }
89803}
89804#endif
89805
89806
89807/*
89808** This function is called after a transaction has been committed. It
89809** invokes callbacks registered with sqlite3_wal_hook() as required.
89810*/
89811static int doWalCallbacks(sqlite3 *db){
89812 int rc = SQLITE_OK;
89813#ifndef SQLITE_OMIT_WAL
89814 int i;
89815 for(i=0; i<db->nDb; i++){
89816 Btree *pBt = db->aDb[i].pBt;
89817 if( pBt ){
89818 int nEntry;
89819 sqlite3BtreeEnter(p: pBt);
89820 nEntry = sqlite3PagerWalCallback(pPager: sqlite3BtreePager(p: pBt));
89821 sqlite3BtreeLeave(p: pBt);
89822 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
89823 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
89824 }
89825 }
89826 }
89827#endif
89828 return rc;
89829}
89830
89831
89832/*
89833** Execute the statement pStmt, either until a row of data is ready, the
89834** statement is completely executed or an error occurs.
89835**
89836** This routine implements the bulk of the logic behind the sqlite_step()
89837** API. The only thing omitted is the automatic recompile if a
89838** schema change has occurred. That detail is handled by the
89839** outer sqlite3_step() wrapper procedure.
89840*/
89841static int sqlite3Step(Vdbe *p){
89842 sqlite3 *db;
89843 int rc;
89844
89845 assert(p);
89846 db = p->db;
89847 if( p->eVdbeState!=VDBE_RUN_STATE ){
89848 restart_step:
89849 if( p->eVdbeState==VDBE_READY_STATE ){
89850 if( p->expired ){
89851 p->rc = SQLITE_SCHEMA;
89852 rc = SQLITE_ERROR;
89853 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
89854 /* If this statement was prepared using saved SQL and an
89855 ** error has occurred, then return the error code in p->rc to the
89856 ** caller. Set the error code in the database handle to the same
89857 ** value.
89858 */
89859 rc = sqlite3VdbeTransferError(p);
89860 }
89861 goto end_of_step;
89862 }
89863
89864 /* If there are no other statements currently running, then
89865 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
89866 ** from interrupting a statement that has not yet started.
89867 */
89868 if( db->nVdbeActive==0 ){
89869 AtomicStore(&db->u1.isInterrupted, 0);
89870 }
89871
89872 assert( db->nVdbeWrite>0 || db->autoCommit==0
89873 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
89874 );
89875
89876#ifndef SQLITE_OMIT_TRACE
89877 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
89878 && !db->init.busy && p->zSql ){
89879 sqlite3OsCurrentTimeInt64(pVfs: db->pVfs, pTimeOut: &p->startTime);
89880 }else{
89881 assert( p->startTime==0 );
89882 }
89883#endif
89884
89885 db->nVdbeActive++;
89886 if( p->readOnly==0 ) db->nVdbeWrite++;
89887 if( p->bIsReader ) db->nVdbeRead++;
89888 p->pc = 0;
89889 p->eVdbeState = VDBE_RUN_STATE;
89890 }else
89891
89892 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
89893 /* We used to require that sqlite3_reset() be called before retrying
89894 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
89895 ** with version 3.7.0, we changed this so that sqlite3_reset() would
89896 ** be called automatically instead of throwing the SQLITE_MISUSE error.
89897 ** This "automatic-reset" change is not technically an incompatibility,
89898 ** since any application that receives an SQLITE_MISUSE is broken by
89899 ** definition.
89900 **
89901 ** Nevertheless, some published applications that were originally written
89902 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
89903 ** returns, and those were broken by the automatic-reset change. As a
89904 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
89905 ** legacy behavior of returning SQLITE_MISUSE for cases where the
89906 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
89907 ** or SQLITE_BUSY error.
89908 */
89909#ifdef SQLITE_OMIT_AUTORESET
89910 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
89911 sqlite3_reset((sqlite3_stmt*)p);
89912 }else{
89913 return SQLITE_MISUSE_BKPT;
89914 }
89915#else
89916 sqlite3_reset(pStmt: (sqlite3_stmt*)p);
89917#endif
89918 assert( p->eVdbeState==VDBE_READY_STATE );
89919 goto restart_step;
89920 }
89921 }
89922
89923#ifdef SQLITE_DEBUG
89924 p->rcApp = SQLITE_OK;
89925#endif
89926#ifndef SQLITE_OMIT_EXPLAIN
89927 if( p->explain ){
89928 rc = sqlite3VdbeList(p);
89929 }else
89930#endif /* SQLITE_OMIT_EXPLAIN */
89931 {
89932 db->nVdbeExec++;
89933 rc = sqlite3VdbeExec(p);
89934 db->nVdbeExec--;
89935 }
89936
89937 if( rc==SQLITE_ROW ){
89938 assert( p->rc==SQLITE_OK );
89939 assert( db->mallocFailed==0 );
89940 db->errCode = SQLITE_ROW;
89941 return SQLITE_ROW;
89942 }else{
89943#ifndef SQLITE_OMIT_TRACE
89944 /* If the statement completed successfully, invoke the profile callback */
89945 checkProfileCallback(db, p);
89946#endif
89947 p->pResultRow = 0;
89948 if( rc==SQLITE_DONE && db->autoCommit ){
89949 assert( p->rc==SQLITE_OK );
89950 p->rc = doWalCallbacks(db);
89951 if( p->rc!=SQLITE_OK ){
89952 rc = SQLITE_ERROR;
89953 }
89954 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
89955 /* If this statement was prepared using saved SQL and an
89956 ** error has occurred, then return the error code in p->rc to the
89957 ** caller. Set the error code in the database handle to the same value.
89958 */
89959 rc = sqlite3VdbeTransferError(p);
89960 }
89961 }
89962
89963 db->errCode = rc;
89964 if( SQLITE_NOMEM==sqlite3ApiExit(db: p->db, rc: p->rc) ){
89965 p->rc = SQLITE_NOMEM_BKPT;
89966 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
89967 }
89968end_of_step:
89969 /* There are only a limited number of result codes allowed from the
89970 ** statements prepared using the legacy sqlite3_prepare() interface */
89971 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
89972 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
89973 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
89974 );
89975 return (rc&db->errMask);
89976}
89977
89978/*
89979** This is the top-level implementation of sqlite3_step(). Call
89980** sqlite3Step() to do most of the work. If a schema error occurs,
89981** call sqlite3Reprepare() and try again.
89982*/
89983SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
89984 int rc = SQLITE_OK; /* Result from sqlite3Step() */
89985 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
89986 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
89987 sqlite3 *db; /* The database connection */
89988
89989 if( vdbeSafetyNotNull(p: v) ){
89990 return SQLITE_MISUSE_BKPT;
89991 }
89992 db = v->db;
89993 sqlite3_mutex_enter(p: db->mutex);
89994 while( (rc = sqlite3Step(p: v))==SQLITE_SCHEMA
89995 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
89996 int savedPc = v->pc;
89997 rc = sqlite3Reprepare(v);
89998 if( rc!=SQLITE_OK ){
89999 /* This case occurs after failing to recompile an sql statement.
90000 ** The error message from the SQL compiler has already been loaded
90001 ** into the database handle. This block copies the error message
90002 ** from the database handle into the statement and sets the statement
90003 ** program counter to 0 to ensure that when the statement is
90004 ** finalized or reset the parser error message is available via
90005 ** sqlite3_errmsg() and sqlite3_errcode().
90006 */
90007 const char *zErr = (const char *)sqlite3_value_text(pVal: db->pErr);
90008 sqlite3DbFree(db, p: v->zErrMsg);
90009 if( !db->mallocFailed ){
90010 v->zErrMsg = sqlite3DbStrDup(db, z: zErr);
90011 v->rc = rc = sqlite3ApiExit(db, rc);
90012 } else {
90013 v->zErrMsg = 0;
90014 v->rc = rc = SQLITE_NOMEM_BKPT;
90015 }
90016 break;
90017 }
90018 sqlite3_reset(pStmt);
90019 if( savedPc>=0 ){
90020 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
90021 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
90022 ** already been done once on a prior invocation that failed due to
90023 ** SQLITE_SCHEMA. tag-20220401a */
90024 v->minWriteFileFormat = 254;
90025 }
90026 assert( v->expired==0 );
90027 }
90028 sqlite3_mutex_leave(p: db->mutex);
90029 return rc;
90030}
90031
90032
90033/*
90034** Extract the user data from a sqlite3_context structure and return a
90035** pointer to it.
90036*/
90037SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
90038 assert( p && p->pFunc );
90039 return p->pFunc->pUserData;
90040}
90041
90042/*
90043** Extract the user data from a sqlite3_context structure and return a
90044** pointer to it.
90045**
90046** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
90047** returns a copy of the pointer to the database connection (the 1st
90048** parameter) of the sqlite3_create_function() and
90049** sqlite3_create_function16() routines that originally registered the
90050** application defined function.
90051*/
90052SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
90053 assert( p && p->pOut );
90054 return p->pOut->db;
90055}
90056
90057/*
90058** If this routine is invoked from within an xColumn method of a virtual
90059** table, then it returns true if and only if the the call is during an
90060** UPDATE operation and the value of the column will not be modified
90061** by the UPDATE.
90062**
90063** If this routine is called from any context other than within the
90064** xColumn method of a virtual table, then the return value is meaningless
90065** and arbitrary.
90066**
90067** Virtual table implements might use this routine to optimize their
90068** performance by substituting a NULL result, or some other light-weight
90069** value, as a signal to the xUpdate routine that the column is unchanged.
90070*/
90071SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
90072 assert( p );
90073 return sqlite3_value_nochange(pVal: p->pOut);
90074}
90075
90076/*
90077** The destructor function for a ValueList object. This needs to be
90078** a separate function, unknowable to the application, to ensure that
90079** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not
90080** preceded by activation of IN processing via sqlite3_vtab_int() do not
90081** try to access a fake ValueList object inserted by a hostile extension.
90082*/
90083SQLITE_PRIVATE void sqlite3VdbeValueListFree(void *pToDelete){
90084 sqlite3_free(p: pToDelete);
90085}
90086
90087/*
90088** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
90089** sqlite3_vtab_in_next() (if bNext!=0).
90090*/
90091static int valueFromValueList(
90092 sqlite3_value *pVal, /* Pointer to the ValueList object */
90093 sqlite3_value **ppOut, /* Store the next value from the list here */
90094 int bNext /* 1 for _next(). 0 for _first() */
90095){
90096 int rc;
90097 ValueList *pRhs;
90098
90099 *ppOut = 0;
90100 if( pVal==0 ) return SQLITE_MISUSE;
90101 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
90102 return SQLITE_ERROR;
90103 }else{
90104 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
90105 (MEM_Null|MEM_Term|MEM_Subtype) );
90106 assert( pVal->eSubtype=='p' );
90107 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
90108 pRhs = (ValueList*)pVal->z;
90109 }
90110 if( bNext ){
90111 rc = sqlite3BtreeNext(pCur: pRhs->pCsr, flags: 0);
90112 }else{
90113 int dummy = 0;
90114 rc = sqlite3BtreeFirst(pCur: pRhs->pCsr, pRes: &dummy);
90115 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
90116 if( sqlite3BtreeEof(pCur: pRhs->pCsr) ) rc = SQLITE_DONE;
90117 }
90118 if( rc==SQLITE_OK ){
90119 u32 sz; /* Size of current row in bytes */
90120 Mem sMem; /* Raw content of current row */
90121 memset(s: &sMem, c: 0, n: sizeof(sMem));
90122 sz = sqlite3BtreePayloadSize(pCur: pRhs->pCsr);
90123 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur: pRhs->pCsr,amt: (int)sz,pMem: &sMem);
90124 if( rc==SQLITE_OK ){
90125 u8 *zBuf = (u8*)sMem.z;
90126 u32 iSerial;
90127 sqlite3_value *pOut = pRhs->pOut;
90128 int iOff = 1 + getVarint32(&zBuf[1], iSerial);
90129 sqlite3VdbeSerialGet(buf: &zBuf[iOff], serial_type: iSerial, pMem: pOut);
90130 pOut->enc = ENC(pOut->db);
90131 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pMem: pOut) ){
90132 rc = SQLITE_NOMEM;
90133 }else{
90134 *ppOut = pOut;
90135 }
90136 }
90137 sqlite3VdbeMemRelease(p: &sMem);
90138 }
90139 return rc;
90140}
90141
90142/*
90143** Set the iterator value pVal to point to the first value in the set.
90144** Set (*ppOut) to point to this value before returning.
90145*/
90146SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
90147 return valueFromValueList(pVal, ppOut, bNext: 0);
90148}
90149
90150/*
90151** Set the iterator value pVal to point to the next value in the set.
90152** Set (*ppOut) to point to this value before returning.
90153*/
90154SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
90155 return valueFromValueList(pVal, ppOut, bNext: 1);
90156}
90157
90158/*
90159** Return the current time for a statement. If the current time
90160** is requested more than once within the same run of a single prepared
90161** statement, the exact same time is returned for each invocation regardless
90162** of the amount of time that elapses between invocations. In other words,
90163** the time returned is always the time of the first call.
90164*/
90165SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
90166 int rc;
90167#ifndef SQLITE_ENABLE_STAT4
90168 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
90169 assert( p->pVdbe!=0 );
90170#else
90171 sqlite3_int64 iTime = 0;
90172 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
90173#endif
90174 if( *piTime==0 ){
90175 rc = sqlite3OsCurrentTimeInt64(pVfs: p->pOut->db->pVfs, pTimeOut: piTime);
90176 if( rc ) *piTime = 0;
90177 }
90178 return *piTime;
90179}
90180
90181/*
90182** Create a new aggregate context for p and return a pointer to
90183** its pMem->z element.
90184*/
90185static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
90186 Mem *pMem = p->pMem;
90187 assert( (pMem->flags & MEM_Agg)==0 );
90188 if( nByte<=0 ){
90189 sqlite3VdbeMemSetNull(pMem);
90190 pMem->z = 0;
90191 }else{
90192 sqlite3VdbeMemClearAndResize(pMem, szNew: nByte);
90193 pMem->flags = MEM_Agg;
90194 pMem->u.pDef = p->pFunc;
90195 if( pMem->z ){
90196 memset(s: pMem->z, c: 0, n: nByte);
90197 }
90198 }
90199 return (void*)pMem->z;
90200}
90201
90202/*
90203** Allocate or return the aggregate context for a user function. A new
90204** context is allocated on the first call. Subsequent calls return the
90205** same context that was returned on prior calls.
90206*/
90207SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
90208 assert( p && p->pFunc && p->pFunc->xFinalize );
90209 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
90210 testcase( nByte<0 );
90211 if( (p->pMem->flags & MEM_Agg)==0 ){
90212 return createAggContext(p, nByte);
90213 }else{
90214 return (void*)p->pMem->z;
90215 }
90216}
90217
90218/*
90219** Return the auxiliary data pointer, if any, for the iArg'th argument to
90220** the user-function defined by pCtx.
90221**
90222** The left-most argument is 0.
90223**
90224** Undocumented behavior: If iArg is negative then access a cache of
90225** auxiliary data pointers that is available to all functions within a
90226** single prepared statement. The iArg values must match.
90227*/
90228SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
90229 AuxData *pAuxData;
90230
90231 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90232#if SQLITE_ENABLE_STAT4
90233 if( pCtx->pVdbe==0 ) return 0;
90234#else
90235 assert( pCtx->pVdbe!=0 );
90236#endif
90237 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
90238 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
90239 return pAuxData->pAux;
90240 }
90241 }
90242 return 0;
90243}
90244
90245/*
90246** Set the auxiliary data pointer and delete function, for the iArg'th
90247** argument to the user-function defined by pCtx. Any previous value is
90248** deleted by calling the delete function specified when it was set.
90249**
90250** The left-most argument is 0.
90251**
90252** Undocumented behavior: If iArg is negative then make the data available
90253** to all functions within the current prepared statement using iArg as an
90254** access code.
90255*/
90256SQLITE_API void sqlite3_set_auxdata(
90257 sqlite3_context *pCtx,
90258 int iArg,
90259 void *pAux,
90260 void (*xDelete)(void*)
90261){
90262 AuxData *pAuxData;
90263 Vdbe *pVdbe = pCtx->pVdbe;
90264
90265 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90266#ifdef SQLITE_ENABLE_STAT4
90267 if( pVdbe==0 ) goto failed;
90268#else
90269 assert( pVdbe!=0 );
90270#endif
90271
90272 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
90273 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
90274 break;
90275 }
90276 }
90277 if( pAuxData==0 ){
90278 pAuxData = sqlite3DbMallocZero(db: pVdbe->db, n: sizeof(AuxData));
90279 if( !pAuxData ) goto failed;
90280 pAuxData->iAuxOp = pCtx->iOp;
90281 pAuxData->iAuxArg = iArg;
90282 pAuxData->pNextAux = pVdbe->pAuxData;
90283 pVdbe->pAuxData = pAuxData;
90284 if( pCtx->isError==0 ) pCtx->isError = -1;
90285 }else if( pAuxData->xDeleteAux ){
90286 pAuxData->xDeleteAux(pAuxData->pAux);
90287 }
90288
90289 pAuxData->pAux = pAux;
90290 pAuxData->xDeleteAux = xDelete;
90291 return;
90292
90293failed:
90294 if( xDelete ){
90295 xDelete(pAux);
90296 }
90297}
90298
90299#ifndef SQLITE_OMIT_DEPRECATED
90300/*
90301** Return the number of times the Step function of an aggregate has been
90302** called.
90303**
90304** This function is deprecated. Do not use it for new code. It is
90305** provide only to avoid breaking legacy code. New aggregate function
90306** implementations should keep their own counts within their aggregate
90307** context.
90308*/
90309SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
90310 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
90311 return p->pMem->n;
90312}
90313#endif
90314
90315/*
90316** Return the number of columns in the result set for the statement pStmt.
90317*/
90318SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
90319 Vdbe *pVm = (Vdbe *)pStmt;
90320 if( pVm==0 ) return 0;
90321 return pVm->nResColumn;
90322}
90323
90324/*
90325** Return the number of values available from the current row of the
90326** currently executing statement pStmt.
90327*/
90328SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
90329 Vdbe *pVm = (Vdbe *)pStmt;
90330 if( pVm==0 || pVm->pResultRow==0 ) return 0;
90331 return pVm->nResColumn;
90332}
90333
90334/*
90335** Return a pointer to static memory containing an SQL NULL value.
90336*/
90337static const Mem *columnNullValue(void){
90338 /* Even though the Mem structure contains an element
90339 ** of type i64, on certain architectures (x86) with certain compiler
90340 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
90341 ** instead of an 8-byte one. This all works fine, except that when
90342 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
90343 ** that a Mem structure is located on an 8-byte boundary. To prevent
90344 ** these assert()s from failing, when building with SQLITE_DEBUG defined
90345 ** using gcc, we force nullMem to be 8-byte aligned using the magical
90346 ** __attribute__((aligned(8))) macro. */
90347 static const Mem nullMem
90348#if defined(SQLITE_DEBUG) && defined(__GNUC__)
90349 __attribute__((aligned(8)))
90350#endif
90351 = {
90352 /* .u = */ {0},
90353 /* .z = */ (char*)0,
90354 /* .n = */ (int)0,
90355 /* .flags = */ (u16)MEM_Null,
90356 /* .enc = */ (u8)0,
90357 /* .eSubtype = */ (u8)0,
90358 /* .db = */ (sqlite3*)0,
90359 /* .szMalloc = */ (int)0,
90360 /* .uTemp = */ (u32)0,
90361 /* .zMalloc = */ (char*)0,
90362 /* .xDel = */ (void(*)(void*))0,
90363#ifdef SQLITE_DEBUG
90364 /* .pScopyFrom = */ (Mem*)0,
90365 /* .mScopyFlags= */ 0,
90366#endif
90367 };
90368 return &nullMem;
90369}
90370
90371/*
90372** Check to see if column iCol of the given statement is valid. If
90373** it is, return a pointer to the Mem for the value of that column.
90374** If iCol is not valid, return a pointer to a Mem which has a value
90375** of NULL.
90376*/
90377static Mem *columnMem(sqlite3_stmt *pStmt, int i){
90378 Vdbe *pVm;
90379 Mem *pOut;
90380
90381 pVm = (Vdbe *)pStmt;
90382 if( pVm==0 ) return (Mem*)columnNullValue();
90383 assert( pVm->db );
90384 sqlite3_mutex_enter(p: pVm->db->mutex);
90385 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
90386 pOut = &pVm->pResultRow[i];
90387 }else{
90388 sqlite3Error(db: pVm->db, SQLITE_RANGE);
90389 pOut = (Mem*)columnNullValue();
90390 }
90391 return pOut;
90392}
90393
90394/*
90395** This function is called after invoking an sqlite3_value_XXX function on a
90396** column value (i.e. a value returned by evaluating an SQL expression in the
90397** select list of a SELECT statement) that may cause a malloc() failure. If
90398** malloc() has failed, the threads mallocFailed flag is cleared and the result
90399** code of statement pStmt set to SQLITE_NOMEM.
90400**
90401** Specifically, this is called from within:
90402**
90403** sqlite3_column_int()
90404** sqlite3_column_int64()
90405** sqlite3_column_text()
90406** sqlite3_column_text16()
90407** sqlite3_column_real()
90408** sqlite3_column_bytes()
90409** sqlite3_column_bytes16()
90410** sqlite3_column_blob()
90411*/
90412static void columnMallocFailure(sqlite3_stmt *pStmt)
90413{
90414 /* If malloc() failed during an encoding conversion within an
90415 ** sqlite3_column_XXX API, then set the return code of the statement to
90416 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
90417 ** and _finalize() will return NOMEM.
90418 */
90419 Vdbe *p = (Vdbe *)pStmt;
90420 if( p ){
90421 assert( p->db!=0 );
90422 assert( sqlite3_mutex_held(p->db->mutex) );
90423 p->rc = sqlite3ApiExit(db: p->db, rc: p->rc);
90424 sqlite3_mutex_leave(p: p->db->mutex);
90425 }
90426}
90427
90428/**************************** sqlite3_column_ *******************************
90429** The following routines are used to access elements of the current row
90430** in the result set.
90431*/
90432SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
90433 const void *val;
90434 val = sqlite3_value_blob( pVal: columnMem(pStmt,i) );
90435 /* Even though there is no encoding conversion, value_blob() might
90436 ** need to call malloc() to expand the result of a zeroblob()
90437 ** expression.
90438 */
90439 columnMallocFailure(pStmt);
90440 return val;
90441}
90442SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
90443 int val = sqlite3_value_bytes( pVal: columnMem(pStmt,i) );
90444 columnMallocFailure(pStmt);
90445 return val;
90446}
90447SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
90448 int val = sqlite3_value_bytes16( pVal: columnMem(pStmt,i) );
90449 columnMallocFailure(pStmt);
90450 return val;
90451}
90452SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
90453 double val = sqlite3_value_double( pVal: columnMem(pStmt,i) );
90454 columnMallocFailure(pStmt);
90455 return val;
90456}
90457SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
90458 int val = sqlite3_value_int( pVal: columnMem(pStmt,i) );
90459 columnMallocFailure(pStmt);
90460 return val;
90461}
90462SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
90463 sqlite_int64 val = sqlite3_value_int64( pVal: columnMem(pStmt,i) );
90464 columnMallocFailure(pStmt);
90465 return val;
90466}
90467SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
90468 const unsigned char *val = sqlite3_value_text( pVal: columnMem(pStmt,i) );
90469 columnMallocFailure(pStmt);
90470 return val;
90471}
90472SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
90473 Mem *pOut = columnMem(pStmt, i);
90474 if( pOut->flags&MEM_Static ){
90475 pOut->flags &= ~MEM_Static;
90476 pOut->flags |= MEM_Ephem;
90477 }
90478 columnMallocFailure(pStmt);
90479 return (sqlite3_value *)pOut;
90480}
90481#ifndef SQLITE_OMIT_UTF16
90482SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
90483 const void *val = sqlite3_value_text16( pVal: columnMem(pStmt,i) );
90484 columnMallocFailure(pStmt);
90485 return val;
90486}
90487#endif /* SQLITE_OMIT_UTF16 */
90488SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
90489 int iType = sqlite3_value_type( pVal: columnMem(pStmt,i) );
90490 columnMallocFailure(pStmt);
90491 return iType;
90492}
90493
90494/*
90495** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN.
90496*/
90497static const char * const azExplainColNames8[] = {
90498 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */
90499 "id", "parent", "notused", "detail" /* EQP */
90500};
90501static const u16 azExplainColNames16data[] = {
90502 /* 0 */ 'a', 'd', 'd', 'r', 0,
90503 /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0,
90504 /* 12 */ 'p', '1', 0,
90505 /* 15 */ 'p', '2', 0,
90506 /* 18 */ 'p', '3', 0,
90507 /* 21 */ 'p', '4', 0,
90508 /* 24 */ 'p', '5', 0,
90509 /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0,
90510 /* 35 */ 'i', 'd', 0,
90511 /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0,
90512 /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0,
90513 /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0
90514};
90515static const u8 iExplainColNames16[] = {
90516 0, 5, 12, 15, 18, 21, 24, 27,
90517 35, 38, 45, 53
90518};
90519
90520/*
90521** Convert the N-th element of pStmt->pColName[] into a string using
90522** xFunc() then return that string. If N is out of range, return 0.
90523**
90524** There are up to 5 names for each column. useType determines which
90525** name is returned. Here are the names:
90526**
90527** 0 The column name as it should be displayed for output
90528** 1 The datatype name for the column
90529** 2 The name of the database that the column derives from
90530** 3 The name of the table that the column derives from
90531** 4 The name of the table column that the result column derives from
90532**
90533** If the result is not a simple column reference (if it is an expression
90534** or a constant) then useTypes 2, 3, and 4 return NULL.
90535*/
90536static const void *columnName(
90537 sqlite3_stmt *pStmt, /* The statement */
90538 int N, /* Which column to get the name for */
90539 int useUtf16, /* True to return the name as UTF16 */
90540 int useType /* What type of name */
90541){
90542 const void *ret;
90543 Vdbe *p;
90544 int n;
90545 sqlite3 *db;
90546#ifdef SQLITE_ENABLE_API_ARMOR
90547 if( pStmt==0 ){
90548 (void)SQLITE_MISUSE_BKPT;
90549 return 0;
90550 }
90551#endif
90552 if( N<0 ) return 0;
90553 ret = 0;
90554 p = (Vdbe *)pStmt;
90555 db = p->db;
90556 assert( db!=0 );
90557 sqlite3_mutex_enter(p: db->mutex);
90558
90559 if( p->explain ){
90560 if( useType>0 ) goto columnName_end;
90561 n = p->explain==1 ? 8 : 4;
90562 if( N>=n ) goto columnName_end;
90563 if( useUtf16 ){
90564 int i = iExplainColNames16[N + 8*p->explain - 8];
90565 ret = (void*)&azExplainColNames16data[i];
90566 }else{
90567 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
90568 }
90569 goto columnName_end;
90570 }
90571 n = p->nResColumn;
90572 if( N<n ){
90573 u8 prior_mallocFailed = db->mallocFailed;
90574 N += useType*n;
90575#ifndef SQLITE_OMIT_UTF16
90576 if( useUtf16 ){
90577 ret = sqlite3_value_text16(pVal: (sqlite3_value*)&p->aColName[N]);
90578 }else
90579#endif
90580 {
90581 ret = sqlite3_value_text(pVal: (sqlite3_value*)&p->aColName[N]);
90582 }
90583 /* A malloc may have failed inside of the _text() call. If this
90584 ** is the case, clear the mallocFailed flag and return NULL.
90585 */
90586 assert( db->mallocFailed==0 || db->mallocFailed==1 );
90587 if( db->mallocFailed > prior_mallocFailed ){
90588 sqlite3OomClear(db);
90589 ret = 0;
90590 }
90591 }
90592columnName_end:
90593 sqlite3_mutex_leave(p: db->mutex);
90594 return ret;
90595}
90596
90597/*
90598** Return the name of the Nth column of the result set returned by SQL
90599** statement pStmt.
90600*/
90601SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
90602 return columnName(pStmt, N, useUtf16: 0, COLNAME_NAME);
90603}
90604#ifndef SQLITE_OMIT_UTF16
90605SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
90606 return columnName(pStmt, N, useUtf16: 1, COLNAME_NAME);
90607}
90608#endif
90609
90610/*
90611** Constraint: If you have ENABLE_COLUMN_METADATA then you must
90612** not define OMIT_DECLTYPE.
90613*/
90614#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
90615# error "Must not define both SQLITE_OMIT_DECLTYPE \
90616 and SQLITE_ENABLE_COLUMN_METADATA"
90617#endif
90618
90619#ifndef SQLITE_OMIT_DECLTYPE
90620/*
90621** Return the column declaration type (if applicable) of the 'i'th column
90622** of the result set of SQL statement pStmt.
90623*/
90624SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
90625 return columnName(pStmt, N, useUtf16: 0, COLNAME_DECLTYPE);
90626}
90627#ifndef SQLITE_OMIT_UTF16
90628SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
90629 return columnName(pStmt, N, useUtf16: 1, COLNAME_DECLTYPE);
90630}
90631#endif /* SQLITE_OMIT_UTF16 */
90632#endif /* SQLITE_OMIT_DECLTYPE */
90633
90634#ifdef SQLITE_ENABLE_COLUMN_METADATA
90635/*
90636** Return the name of the database from which a result column derives.
90637** NULL is returned if the result column is an expression or constant or
90638** anything else which is not an unambiguous reference to a database column.
90639*/
90640SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
90641 return columnName(pStmt, N, useUtf16: 0, COLNAME_DATABASE);
90642}
90643#ifndef SQLITE_OMIT_UTF16
90644SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
90645 return columnName(pStmt, N, useUtf16: 1, COLNAME_DATABASE);
90646}
90647#endif /* SQLITE_OMIT_UTF16 */
90648
90649/*
90650** Return the name of the table from which a result column derives.
90651** NULL is returned if the result column is an expression or constant or
90652** anything else which is not an unambiguous reference to a database column.
90653*/
90654SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
90655 return columnName(pStmt, N, useUtf16: 0, COLNAME_TABLE);
90656}
90657#ifndef SQLITE_OMIT_UTF16
90658SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
90659 return columnName(pStmt, N, useUtf16: 1, COLNAME_TABLE);
90660}
90661#endif /* SQLITE_OMIT_UTF16 */
90662
90663/*
90664** Return the name of the table column from which a result column derives.
90665** NULL is returned if the result column is an expression or constant or
90666** anything else which is not an unambiguous reference to a database column.
90667*/
90668SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
90669 return columnName(pStmt, N, useUtf16: 0, COLNAME_COLUMN);
90670}
90671#ifndef SQLITE_OMIT_UTF16
90672SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
90673 return columnName(pStmt, N, useUtf16: 1, COLNAME_COLUMN);
90674}
90675#endif /* SQLITE_OMIT_UTF16 */
90676#endif /* SQLITE_ENABLE_COLUMN_METADATA */
90677
90678
90679/******************************* sqlite3_bind_ ***************************
90680**
90681** Routines used to attach values to wildcards in a compiled SQL statement.
90682*/
90683/*
90684** Unbind the value bound to variable i in virtual machine p. This is the
90685** the same as binding a NULL value to the column. If the "i" parameter is
90686** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK.
90687**
90688** A successful evaluation of this routine acquires the mutex on p.
90689** the mutex is released if any kind of error occurs.
90690**
90691** The error code stored in database p->db is overwritten with the return
90692** value in any case.
90693*/
90694static int vdbeUnbind(Vdbe *p, unsigned int i){
90695 Mem *pVar;
90696 if( vdbeSafetyNotNull(p) ){
90697 return SQLITE_MISUSE_BKPT;
90698 }
90699 sqlite3_mutex_enter(p: p->db->mutex);
90700 if( p->eVdbeState!=VDBE_READY_STATE ){
90701 sqlite3Error(db: p->db, SQLITE_MISUSE);
90702 sqlite3_mutex_leave(p: p->db->mutex);
90703 sqlite3_log(SQLITE_MISUSE,
90704 zFormat: "bind on a busy prepared statement: [%s]", p->zSql);
90705 return SQLITE_MISUSE_BKPT;
90706 }
90707 if( i>=(unsigned int)p->nVar ){
90708 sqlite3Error(db: p->db, SQLITE_RANGE);
90709 sqlite3_mutex_leave(p: p->db->mutex);
90710 return SQLITE_RANGE;
90711 }
90712 pVar = &p->aVar[i];
90713 sqlite3VdbeMemRelease(p: pVar);
90714 pVar->flags = MEM_Null;
90715 p->db->errCode = SQLITE_OK;
90716
90717 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
90718 ** binding a new value to this variable invalidates the current query plan.
90719 **
90720 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
90721 ** parameter in the WHERE clause might influence the choice of query plan
90722 ** for a statement, then the statement will be automatically recompiled,
90723 ** as if there had been a schema change, on the first sqlite3_step() call
90724 ** following any change to the bindings of that parameter.
90725 */
90726 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
90727 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
90728 p->expired = 1;
90729 }
90730 return SQLITE_OK;
90731}
90732
90733/*
90734** Bind a text or BLOB value.
90735*/
90736static int bindText(
90737 sqlite3_stmt *pStmt, /* The statement to bind against */
90738 int i, /* Index of the parameter to bind */
90739 const void *zData, /* Pointer to the data to be bound */
90740 i64 nData, /* Number of bytes of data to be bound */
90741 void (*xDel)(void*), /* Destructor for the data */
90742 u8 encoding /* Encoding for the data */
90743){
90744 Vdbe *p = (Vdbe *)pStmt;
90745 Mem *pVar;
90746 int rc;
90747
90748 rc = vdbeUnbind(p, i: (u32)(i-1));
90749 if( rc==SQLITE_OK ){
90750 if( zData!=0 ){
90751 pVar = &p->aVar[i-1];
90752 rc = sqlite3VdbeMemSetStr(pMem: pVar, z: zData, n: nData, enc: encoding, xDel);
90753 if( rc==SQLITE_OK && encoding!=0 ){
90754 rc = sqlite3VdbeChangeEncoding(pMem: pVar, ENC(p->db));
90755 }
90756 if( rc ){
90757 sqlite3Error(db: p->db, err_code: rc);
90758 rc = sqlite3ApiExit(db: p->db, rc);
90759 }
90760 }
90761 sqlite3_mutex_leave(p: p->db->mutex);
90762 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
90763 xDel((void*)zData);
90764 }
90765 return rc;
90766}
90767
90768
90769/*
90770** Bind a blob value to an SQL statement variable.
90771*/
90772SQLITE_API int sqlite3_bind_blob(
90773 sqlite3_stmt *pStmt,
90774 int i,
90775 const void *zData,
90776 int nData,
90777 void (*xDel)(void*)
90778){
90779#ifdef SQLITE_ENABLE_API_ARMOR
90780 if( nData<0 ) return SQLITE_MISUSE_BKPT;
90781#endif
90782 return bindText(pStmt, i, zData, nData, xDel, encoding: 0);
90783}
90784SQLITE_API int sqlite3_bind_blob64(
90785 sqlite3_stmt *pStmt,
90786 int i,
90787 const void *zData,
90788 sqlite3_uint64 nData,
90789 void (*xDel)(void*)
90790){
90791 assert( xDel!=SQLITE_DYNAMIC );
90792 return bindText(pStmt, i, zData, nData, xDel, encoding: 0);
90793}
90794SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
90795 int rc;
90796 Vdbe *p = (Vdbe *)pStmt;
90797 rc = vdbeUnbind(p, i: (u32)(i-1));
90798 if( rc==SQLITE_OK ){
90799 sqlite3VdbeMemSetDouble(pMem: &p->aVar[i-1], val: rValue);
90800 sqlite3_mutex_leave(p: p->db->mutex);
90801 }
90802 return rc;
90803}
90804SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
90805 return sqlite3_bind_int64(p, i, (i64)iValue);
90806}
90807SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
90808 int rc;
90809 Vdbe *p = (Vdbe *)pStmt;
90810 rc = vdbeUnbind(p, i: (u32)(i-1));
90811 if( rc==SQLITE_OK ){
90812 sqlite3VdbeMemSetInt64(pMem: &p->aVar[i-1], val: iValue);
90813 sqlite3_mutex_leave(p: p->db->mutex);
90814 }
90815 return rc;
90816}
90817SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
90818 int rc;
90819 Vdbe *p = (Vdbe*)pStmt;
90820 rc = vdbeUnbind(p, i: (u32)(i-1));
90821 if( rc==SQLITE_OK ){
90822 sqlite3_mutex_leave(p: p->db->mutex);
90823 }
90824 return rc;
90825}
90826SQLITE_API int sqlite3_bind_pointer(
90827 sqlite3_stmt *pStmt,
90828 int i,
90829 void *pPtr,
90830 const char *zPTtype,
90831 void (*xDestructor)(void*)
90832){
90833 int rc;
90834 Vdbe *p = (Vdbe*)pStmt;
90835 rc = vdbeUnbind(p, i: (u32)(i-1));
90836 if( rc==SQLITE_OK ){
90837 sqlite3VdbeMemSetPointer(pMem: &p->aVar[i-1], pPtr, zPType: zPTtype, xDestructor);
90838 sqlite3_mutex_leave(p: p->db->mutex);
90839 }else if( xDestructor ){
90840 xDestructor(pPtr);
90841 }
90842 return rc;
90843}
90844SQLITE_API int sqlite3_bind_text(
90845 sqlite3_stmt *pStmt,
90846 int i,
90847 const char *zData,
90848 int nData,
90849 void (*xDel)(void*)
90850){
90851 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
90852}
90853SQLITE_API int sqlite3_bind_text64(
90854 sqlite3_stmt *pStmt,
90855 int i,
90856 const char *zData,
90857 sqlite3_uint64 nData,
90858 void (*xDel)(void*),
90859 unsigned char enc
90860){
90861 assert( xDel!=SQLITE_DYNAMIC );
90862 if( enc!=SQLITE_UTF8 ){
90863 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
90864 nData &= ~(u16)1;
90865 }
90866 return bindText(pStmt, i, zData, nData, xDel, encoding: enc);
90867}
90868#ifndef SQLITE_OMIT_UTF16
90869SQLITE_API int sqlite3_bind_text16(
90870 sqlite3_stmt *pStmt,
90871 int i,
90872 const void *zData,
90873 int n,
90874 void (*xDel)(void*)
90875){
90876 return bindText(pStmt, i, zData, nData: n & ~(u64)1, xDel, SQLITE_UTF16NATIVE);
90877}
90878#endif /* SQLITE_OMIT_UTF16 */
90879SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
90880 int rc;
90881 switch( sqlite3_value_type(pVal: (sqlite3_value*)pValue) ){
90882 case SQLITE_INTEGER: {
90883 rc = sqlite3_bind_int64(pStmt, i, iValue: pValue->u.i);
90884 break;
90885 }
90886 case SQLITE_FLOAT: {
90887 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
90888 rc = sqlite3_bind_double(pStmt, i,
90889 rValue: (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
90890 );
90891 break;
90892 }
90893 case SQLITE_BLOB: {
90894 if( pValue->flags & MEM_Zero ){
90895 rc = sqlite3_bind_zeroblob(pStmt, i, n: pValue->u.nZero);
90896 }else{
90897 rc = sqlite3_bind_blob(pStmt, i, zData: pValue->z, nData: pValue->n,SQLITE_TRANSIENT);
90898 }
90899 break;
90900 }
90901 case SQLITE_TEXT: {
90902 rc = bindText(pStmt,i, zData: pValue->z, nData: pValue->n, SQLITE_TRANSIENT,
90903 encoding: pValue->enc);
90904 break;
90905 }
90906 default: {
90907 rc = sqlite3_bind_null(pStmt, i);
90908 break;
90909 }
90910 }
90911 return rc;
90912}
90913SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
90914 int rc;
90915 Vdbe *p = (Vdbe *)pStmt;
90916 rc = vdbeUnbind(p, i: (u32)(i-1));
90917 if( rc==SQLITE_OK ){
90918#ifndef SQLITE_OMIT_INCRBLOB
90919 sqlite3VdbeMemSetZeroBlob(pMem: &p->aVar[i-1], n);
90920#else
90921 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
90922#endif
90923 sqlite3_mutex_leave(p: p->db->mutex);
90924 }
90925 return rc;
90926}
90927SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
90928 int rc;
90929 Vdbe *p = (Vdbe *)pStmt;
90930 sqlite3_mutex_enter(p: p->db->mutex);
90931 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
90932 rc = SQLITE_TOOBIG;
90933 }else{
90934 assert( (n & 0x7FFFFFFF)==n );
90935 rc = sqlite3_bind_zeroblob(pStmt, i, n);
90936 }
90937 rc = sqlite3ApiExit(db: p->db, rc);
90938 sqlite3_mutex_leave(p: p->db->mutex);
90939 return rc;
90940}
90941
90942/*
90943** Return the number of wildcards that can be potentially bound to.
90944** This routine is added to support DBD::SQLite.
90945*/
90946SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
90947 Vdbe *p = (Vdbe*)pStmt;
90948 return p ? p->nVar : 0;
90949}
90950
90951/*
90952** Return the name of a wildcard parameter. Return NULL if the index
90953** is out of range or if the wildcard is unnamed.
90954**
90955** The result is always UTF-8.
90956*/
90957SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
90958 Vdbe *p = (Vdbe*)pStmt;
90959 if( p==0 ) return 0;
90960 return sqlite3VListNumToName(pIn: p->pVList, iVal: i);
90961}
90962
90963/*
90964** Given a wildcard parameter name, return the index of the variable
90965** with that name. If there is no variable with the given name,
90966** return 0.
90967*/
90968SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
90969 if( p==0 || zName==0 ) return 0;
90970 return sqlite3VListNameToNum(pIn: p->pVList, zName, nName);
90971}
90972SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
90973 return sqlite3VdbeParameterIndex(p: (Vdbe*)pStmt, zName, nName: sqlite3Strlen30(z: zName));
90974}
90975
90976/*
90977** Transfer all bindings from the first statement over to the second.
90978*/
90979SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
90980 Vdbe *pFrom = (Vdbe*)pFromStmt;
90981 Vdbe *pTo = (Vdbe*)pToStmt;
90982 int i;
90983 assert( pTo->db==pFrom->db );
90984 assert( pTo->nVar==pFrom->nVar );
90985 sqlite3_mutex_enter(p: pTo->db->mutex);
90986 for(i=0; i<pFrom->nVar; i++){
90987 sqlite3VdbeMemMove(pTo: &pTo->aVar[i], pFrom: &pFrom->aVar[i]);
90988 }
90989 sqlite3_mutex_leave(p: pTo->db->mutex);
90990 return SQLITE_OK;
90991}
90992
90993#ifndef SQLITE_OMIT_DEPRECATED
90994/*
90995** Deprecated external interface. Internal/core SQLite code
90996** should call sqlite3TransferBindings.
90997**
90998** It is misuse to call this routine with statements from different
90999** database connections. But as this is a deprecated interface, we
91000** will not bother to check for that condition.
91001**
91002** If the two statements contain a different number of bindings, then
91003** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
91004** SQLITE_OK is returned.
91005*/
91006SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
91007 Vdbe *pFrom = (Vdbe*)pFromStmt;
91008 Vdbe *pTo = (Vdbe*)pToStmt;
91009 if( pFrom->nVar!=pTo->nVar ){
91010 return SQLITE_ERROR;
91011 }
91012 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
91013 if( pTo->expmask ){
91014 pTo->expired = 1;
91015 }
91016 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
91017 if( pFrom->expmask ){
91018 pFrom->expired = 1;
91019 }
91020 return sqlite3TransferBindings(pFromStmt, pToStmt);
91021}
91022#endif
91023
91024/*
91025** Return the sqlite3* database handle to which the prepared statement given
91026** in the argument belongs. This is the same database handle that was
91027** the first argument to the sqlite3_prepare() that was used to create
91028** the statement in the first place.
91029*/
91030SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
91031 return pStmt ? ((Vdbe*)pStmt)->db : 0;
91032}
91033
91034/*
91035** Return true if the prepared statement is guaranteed to not modify the
91036** database.
91037*/
91038SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
91039 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
91040}
91041
91042/*
91043** Return 1 if the statement is an EXPLAIN and return 2 if the
91044** statement is an EXPLAIN QUERY PLAN
91045*/
91046SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
91047 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
91048}
91049
91050/*
91051** Set the explain mode for a statement.
91052*/
91053SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){
91054 Vdbe *v = (Vdbe*)pStmt;
91055 int rc;
91056 sqlite3_mutex_enter(p: v->db->mutex);
91057 if( ((int)v->explain)==eMode ){
91058 rc = SQLITE_OK;
91059 }else if( eMode<0 || eMode>2 ){
91060 rc = SQLITE_ERROR;
91061 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
91062 rc = SQLITE_ERROR;
91063 }else if( v->eVdbeState!=VDBE_READY_STATE ){
91064 rc = SQLITE_BUSY;
91065 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
91066 /* No reprepare necessary */
91067 v->explain = eMode;
91068 rc = SQLITE_OK;
91069 }else{
91070 v->explain = eMode;
91071 rc = sqlite3Reprepare(v);
91072 v->haveEqpOps = eMode==2;
91073 }
91074 if( v->explain ){
91075 v->nResColumn = 12 - 4*v->explain;
91076 }else{
91077 v->nResColumn = v->nResAlloc;
91078 }
91079 sqlite3_mutex_leave(p: v->db->mutex);
91080 return rc;
91081}
91082
91083/*
91084** Return true if the prepared statement is in need of being reset.
91085*/
91086SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
91087 Vdbe *v = (Vdbe*)pStmt;
91088 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
91089}
91090
91091/*
91092** Return a pointer to the next prepared statement after pStmt associated
91093** with database connection pDb. If pStmt is NULL, return the first
91094** prepared statement for the database connection. Return NULL if there
91095** are no more.
91096*/
91097SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
91098 sqlite3_stmt *pNext;
91099#ifdef SQLITE_ENABLE_API_ARMOR
91100 if( !sqlite3SafetyCheckOk(pDb) ){
91101 (void)SQLITE_MISUSE_BKPT;
91102 return 0;
91103 }
91104#endif
91105 sqlite3_mutex_enter(p: pDb->mutex);
91106 if( pStmt==0 ){
91107 pNext = (sqlite3_stmt*)pDb->pVdbe;
91108 }else{
91109 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
91110 }
91111 sqlite3_mutex_leave(p: pDb->mutex);
91112 return pNext;
91113}
91114
91115/*
91116** Return the value of a status counter for a prepared statement
91117*/
91118SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
91119 Vdbe *pVdbe = (Vdbe*)pStmt;
91120 u32 v;
91121#ifdef SQLITE_ENABLE_API_ARMOR
91122 if( !pStmt
91123 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
91124 ){
91125 (void)SQLITE_MISUSE_BKPT;
91126 return 0;
91127 }
91128#endif
91129 if( op==SQLITE_STMTSTATUS_MEMUSED ){
91130 sqlite3 *db = pVdbe->db;
91131 sqlite3_mutex_enter(p: db->mutex);
91132 v = 0;
91133 db->pnBytesFreed = (int*)&v;
91134 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
91135 db->lookaside.pEnd = db->lookaside.pStart;
91136 sqlite3VdbeDelete(p: pVdbe);
91137 db->pnBytesFreed = 0;
91138 db->lookaside.pEnd = db->lookaside.pTrueEnd;
91139 sqlite3_mutex_leave(p: db->mutex);
91140 }else{
91141 v = pVdbe->aCounter[op];
91142 if( resetFlag ) pVdbe->aCounter[op] = 0;
91143 }
91144 return (int)v;
91145}
91146
91147/*
91148** Return the SQL associated with a prepared statement
91149*/
91150SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
91151 Vdbe *p = (Vdbe *)pStmt;
91152 return p ? p->zSql : 0;
91153}
91154
91155/*
91156** Return the SQL associated with a prepared statement with
91157** bound parameters expanded. Space to hold the returned string is
91158** obtained from sqlite3_malloc(). The caller is responsible for
91159** freeing the returned string by passing it to sqlite3_free().
91160**
91161** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
91162** expanded bound parameters.
91163*/
91164SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
91165#ifdef SQLITE_OMIT_TRACE
91166 return 0;
91167#else
91168 char *z = 0;
91169 const char *zSql = sqlite3_sql(pStmt);
91170 if( zSql ){
91171 Vdbe *p = (Vdbe *)pStmt;
91172 sqlite3_mutex_enter(p: p->db->mutex);
91173 z = sqlite3VdbeExpandSql(p, zSql);
91174 sqlite3_mutex_leave(p: p->db->mutex);
91175 }
91176 return z;
91177#endif
91178}
91179
91180#ifdef SQLITE_ENABLE_NORMALIZE
91181/*
91182** Return the normalized SQL associated with a prepared statement.
91183*/
91184SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
91185 Vdbe *p = (Vdbe *)pStmt;
91186 if( p==0 ) return 0;
91187 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
91188 sqlite3_mutex_enter(p->db->mutex);
91189 p->zNormSql = sqlite3Normalize(p, p->zSql);
91190 sqlite3_mutex_leave(p->db->mutex);
91191 }
91192 return p->zNormSql;
91193}
91194#endif /* SQLITE_ENABLE_NORMALIZE */
91195
91196#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91197/*
91198** Allocate and populate an UnpackedRecord structure based on the serialized
91199** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
91200** if successful, or a NULL pointer if an OOM error is encountered.
91201*/
91202static UnpackedRecord *vdbeUnpackRecord(
91203 KeyInfo *pKeyInfo,
91204 int nKey,
91205 const void *pKey
91206){
91207 UnpackedRecord *pRet; /* Return value */
91208
91209 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
91210 if( pRet ){
91211 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
91212 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
91213 }
91214 return pRet;
91215}
91216
91217/*
91218** This function is called from within a pre-update callback to retrieve
91219** a field of the row currently being updated or deleted.
91220*/
91221SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
91222 PreUpdate *p = db->pPreUpdate;
91223 Mem *pMem;
91224 int rc = SQLITE_OK;
91225
91226 /* Test that this call is being made from within an SQLITE_DELETE or
91227 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
91228 if( !p || p->op==SQLITE_INSERT ){
91229 rc = SQLITE_MISUSE_BKPT;
91230 goto preupdate_old_out;
91231 }
91232 if( p->pPk ){
91233 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
91234 }
91235 if( iIdx>=p->pCsr->nField || iIdx<0 ){
91236 rc = SQLITE_RANGE;
91237 goto preupdate_old_out;
91238 }
91239
91240 /* If the old.* record has not yet been loaded into memory, do so now. */
91241 if( p->pUnpacked==0 ){
91242 u32 nRec;
91243 u8 *aRec;
91244
91245 assert( p->pCsr->eCurType==CURTYPE_BTREE );
91246 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
91247 aRec = sqlite3DbMallocRaw(db, nRec);
91248 if( !aRec ) goto preupdate_old_out;
91249 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
91250 if( rc==SQLITE_OK ){
91251 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
91252 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
91253 }
91254 if( rc!=SQLITE_OK ){
91255 sqlite3DbFree(db, aRec);
91256 goto preupdate_old_out;
91257 }
91258 p->aRecord = aRec;
91259 }
91260
91261 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
91262 if( iIdx==p->pTab->iPKey ){
91263 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
91264 }else if( iIdx>=p->pUnpacked->nField ){
91265 *ppValue = (sqlite3_value *)columnNullValue();
91266 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
91267 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
91268 testcase( pMem->flags & MEM_Int );
91269 testcase( pMem->flags & MEM_IntReal );
91270 sqlite3VdbeMemRealify(pMem);
91271 }
91272 }
91273
91274 preupdate_old_out:
91275 sqlite3Error(db, rc);
91276 return sqlite3ApiExit(db, rc);
91277}
91278#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
91279
91280#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91281/*
91282** This function is called from within a pre-update callback to retrieve
91283** the number of columns in the row being updated, deleted or inserted.
91284*/
91285SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
91286 PreUpdate *p = db->pPreUpdate;
91287 return (p ? p->keyinfo.nKeyField : 0);
91288}
91289#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
91290
91291#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91292/*
91293** This function is designed to be called from within a pre-update callback
91294** only. It returns zero if the change that caused the callback was made
91295** immediately by a user SQL statement. Or, if the change was made by a
91296** trigger program, it returns the number of trigger programs currently
91297** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
91298** top-level trigger etc.).
91299**
91300** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
91301** or SET DEFAULT action is considered a trigger.
91302*/
91303SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
91304 PreUpdate *p = db->pPreUpdate;
91305 return (p ? p->v->nFrame : 0);
91306}
91307#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
91308
91309#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91310/*
91311** This function is designed to be called from within a pre-update callback
91312** only.
91313*/
91314SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *db){
91315 PreUpdate *p = db->pPreUpdate;
91316 return (p ? p->iBlobWrite : -1);
91317}
91318#endif
91319
91320#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91321/*
91322** This function is called from within a pre-update callback to retrieve
91323** a field of the row currently being updated or inserted.
91324*/
91325SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
91326 PreUpdate *p = db->pPreUpdate;
91327 int rc = SQLITE_OK;
91328 Mem *pMem;
91329
91330 if( !p || p->op==SQLITE_DELETE ){
91331 rc = SQLITE_MISUSE_BKPT;
91332 goto preupdate_new_out;
91333 }
91334 if( p->pPk && p->op!=SQLITE_UPDATE ){
91335 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
91336 }
91337 if( iIdx>=p->pCsr->nField || iIdx<0 ){
91338 rc = SQLITE_RANGE;
91339 goto preupdate_new_out;
91340 }
91341
91342 if( p->op==SQLITE_INSERT ){
91343 /* For an INSERT, memory cell p->iNewReg contains the serialized record
91344 ** that is being inserted. Deserialize it. */
91345 UnpackedRecord *pUnpack = p->pNewUnpacked;
91346 if( !pUnpack ){
91347 Mem *pData = &p->v->aMem[p->iNewReg];
91348 rc = ExpandBlob(pData);
91349 if( rc!=SQLITE_OK ) goto preupdate_new_out;
91350 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
91351 if( !pUnpack ){
91352 rc = SQLITE_NOMEM;
91353 goto preupdate_new_out;
91354 }
91355 p->pNewUnpacked = pUnpack;
91356 }
91357 pMem = &pUnpack->aMem[iIdx];
91358 if( iIdx==p->pTab->iPKey ){
91359 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
91360 }else if( iIdx>=pUnpack->nField ){
91361 pMem = (sqlite3_value *)columnNullValue();
91362 }
91363 }else{
91364 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
91365 ** value. Make a copy of the cell contents and return a pointer to it.
91366 ** It is not safe to return a pointer to the memory cell itself as the
91367 ** caller may modify the value text encoding.
91368 */
91369 assert( p->op==SQLITE_UPDATE );
91370 if( !p->aNew ){
91371 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
91372 if( !p->aNew ){
91373 rc = SQLITE_NOMEM;
91374 goto preupdate_new_out;
91375 }
91376 }
91377 assert( iIdx>=0 && iIdx<p->pCsr->nField );
91378 pMem = &p->aNew[iIdx];
91379 if( pMem->flags==0 ){
91380 if( iIdx==p->pTab->iPKey ){
91381 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
91382 }else{
91383 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
91384 if( rc!=SQLITE_OK ) goto preupdate_new_out;
91385 }
91386 }
91387 }
91388 *ppValue = pMem;
91389
91390 preupdate_new_out:
91391 sqlite3Error(db, rc);
91392 return sqlite3ApiExit(db, rc);
91393}
91394#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
91395
91396#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
91397/*
91398** Return status data for a single loop within query pStmt.
91399*/
91400SQLITE_API int sqlite3_stmt_scanstatus_v2(
91401 sqlite3_stmt *pStmt, /* Prepared statement being queried */
91402 int iScan, /* Index of loop to report on */
91403 int iScanStatusOp, /* Which metric to return */
91404 int flags,
91405 void *pOut /* OUT: Write the answer here */
91406){
91407 Vdbe *p = (Vdbe*)pStmt;
91408 VdbeOp *aOp = p->aOp;
91409 int nOp = p->nOp;
91410 ScanStatus *pScan = 0;
91411 int idx;
91412
91413 if( p->pFrame ){
91414 VdbeFrame *pFrame;
91415 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
91416 aOp = pFrame->aOp;
91417 nOp = pFrame->nOp;
91418 }
91419
91420 if( iScan<0 ){
91421 int ii;
91422 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){
91423 i64 res = 0;
91424 for(ii=0; ii<nOp; ii++){
91425 res += aOp[ii].nCycle;
91426 }
91427 *(i64*)pOut = res;
91428 return 0;
91429 }
91430 return 1;
91431 }
91432 if( flags & SQLITE_SCANSTAT_COMPLEX ){
91433 idx = iScan;
91434 pScan = &p->aScan[idx];
91435 }else{
91436 /* If the COMPLEX flag is clear, then this function must ignore any
91437 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */
91438 for(idx=0; idx<p->nScan; idx++){
91439 pScan = &p->aScan[idx];
91440 if( pScan->zName ){
91441 iScan--;
91442 if( iScan<0 ) break;
91443 }
91444 }
91445 }
91446 if( idx>=p->nScan ) return 1;
91447
91448 switch( iScanStatusOp ){
91449 case SQLITE_SCANSTAT_NLOOP: {
91450 if( pScan->addrLoop>0 ){
91451 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
91452 }else{
91453 *(sqlite3_int64*)pOut = -1;
91454 }
91455 break;
91456 }
91457 case SQLITE_SCANSTAT_NVISIT: {
91458 if( pScan->addrVisit>0 ){
91459 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
91460 }else{
91461 *(sqlite3_int64*)pOut = -1;
91462 }
91463 break;
91464 }
91465 case SQLITE_SCANSTAT_EST: {
91466 double r = 1.0;
91467 LogEst x = pScan->nEst;
91468 while( x<100 ){
91469 x += 10;
91470 r *= 0.5;
91471 }
91472 *(double*)pOut = r*sqlite3LogEstToInt(x);
91473 break;
91474 }
91475 case SQLITE_SCANSTAT_NAME: {
91476 *(const char**)pOut = pScan->zName;
91477 break;
91478 }
91479 case SQLITE_SCANSTAT_EXPLAIN: {
91480 if( pScan->addrExplain ){
91481 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
91482 }else{
91483 *(const char**)pOut = 0;
91484 }
91485 break;
91486 }
91487 case SQLITE_SCANSTAT_SELECTID: {
91488 if( pScan->addrExplain ){
91489 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
91490 }else{
91491 *(int*)pOut = -1;
91492 }
91493 break;
91494 }
91495 case SQLITE_SCANSTAT_PARENTID: {
91496 if( pScan->addrExplain ){
91497 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
91498 }else{
91499 *(int*)pOut = -1;
91500 }
91501 break;
91502 }
91503 case SQLITE_SCANSTAT_NCYCLE: {
91504 i64 res = 0;
91505 if( pScan->aAddrRange[0]==0 ){
91506 res = -1;
91507 }else{
91508 int ii;
91509 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
91510 int iIns = pScan->aAddrRange[ii];
91511 int iEnd = pScan->aAddrRange[ii+1];
91512 if( iIns==0 ) break;
91513 if( iIns>0 ){
91514 while( iIns<=iEnd ){
91515 res += aOp[iIns].nCycle;
91516 iIns++;
91517 }
91518 }else{
91519 int iOp;
91520 for(iOp=0; iOp<nOp; iOp++){
91521 Op *pOp = &aOp[iOp];
91522 if( pOp->p1!=iEnd ) continue;
91523 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
91524 continue;
91525 }
91526 res += aOp[iOp].nCycle;
91527 }
91528 }
91529 }
91530 }
91531 *(i64*)pOut = res;
91532 break;
91533 }
91534 default: {
91535 return 1;
91536 }
91537 }
91538 return 0;
91539}
91540
91541/*
91542** Return status data for a single loop within query pStmt.
91543*/
91544SQLITE_API int sqlite3_stmt_scanstatus(
91545 sqlite3_stmt *pStmt, /* Prepared statement being queried */
91546 int iScan, /* Index of loop to report on */
91547 int iScanStatusOp, /* Which metric to return */
91548 void *pOut /* OUT: Write the answer here */
91549){
91550 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut);
91551}
91552
91553/*
91554** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
91555*/
91556SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
91557 Vdbe *p = (Vdbe*)pStmt;
91558 int ii;
91559 for(ii=0; ii<p->nOp; ii++){
91560 Op *pOp = &p->aOp[ii];
91561 pOp->nExec = 0;
91562 pOp->nCycle = 0;
91563 }
91564}
91565#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
91566
91567/************** End of vdbeapi.c *********************************************/
91568/************** Begin file vdbetrace.c ***************************************/
91569/*
91570** 2009 November 25
91571**
91572** The author disclaims copyright to this source code. In place of
91573** a legal notice, here is a blessing:
91574**
91575** May you do good and not evil.
91576** May you find forgiveness for yourself and forgive others.
91577** May you share freely, never taking more than you give.
91578**
91579*************************************************************************
91580**
91581** This file contains code used to insert the values of host parameters
91582** (aka "wildcards") into the SQL text output by sqlite3_trace().
91583**
91584** The Vdbe parse-tree explainer is also found here.
91585*/
91586/* #include "sqliteInt.h" */
91587/* #include "vdbeInt.h" */
91588
91589#ifndef SQLITE_OMIT_TRACE
91590
91591/*
91592** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
91593** bytes in this text up to but excluding the first character in
91594** a host parameter. If the text contains no host parameters, return
91595** the total number of bytes in the text.
91596*/
91597static int findNextHostParameter(const char *zSql, int *pnToken){
91598 int tokenType;
91599 int nTotal = 0;
91600 int n;
91601
91602 *pnToken = 0;
91603 while( zSql[0] ){
91604 n = sqlite3GetToken((u8*)zSql, &tokenType);
91605 assert( n>0 && tokenType!=TK_ILLEGAL );
91606 if( tokenType==TK_VARIABLE ){
91607 *pnToken = n;
91608 break;
91609 }
91610 nTotal += n;
91611 zSql += n;
91612 }
91613 return nTotal;
91614}
91615
91616/*
91617** This function returns a pointer to a nul-terminated string in memory
91618** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
91619** string contains a copy of zRawSql but with host parameters expanded to
91620** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
91621** then the returned string holds a copy of zRawSql with "-- " prepended
91622** to each line of text.
91623**
91624** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
91625** then long strings and blobs are truncated to that many bytes. This
91626** can be used to prevent unreasonably large trace strings when dealing
91627** with large (multi-megabyte) strings and blobs.
91628**
91629** The calling function is responsible for making sure the memory returned
91630** is eventually freed.
91631**
91632** ALGORITHM: Scan the input string looking for host parameters in any of
91633** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
91634** string literals, quoted identifier names, and comments. For text forms,
91635** the host parameter index is found by scanning the prepared
91636** statement for the corresponding OP_Variable opcode. Once the host
91637** parameter index is known, locate the value in p->aVar[]. Then render
91638** the value as a literal in place of the host parameter name.
91639*/
91640SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
91641 Vdbe *p, /* The prepared statement being evaluated */
91642 const char *zRawSql /* Raw text of the SQL statement */
91643){
91644 sqlite3 *db; /* The database connection */
91645 int idx = 0; /* Index of a host parameter */
91646 int nextIndex = 1; /* Index of next ? host parameter */
91647 int n; /* Length of a token prefix */
91648 int nToken; /* Length of the parameter token */
91649 int i; /* Loop counter */
91650 Mem *pVar; /* Value of a host parameter */
91651 StrAccum out; /* Accumulate the output here */
91652#ifndef SQLITE_OMIT_UTF16
91653 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
91654#endif
91655
91656 db = p->db;
91657 sqlite3StrAccumInit(p: &out, db: 0, zBase: 0, n: 0, mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
91658 if( db->nVdbeExec>1 ){
91659 while( *zRawSql ){
91660 const char *zStart = zRawSql;
91661 while( *(zRawSql++)!='\n' && *zRawSql );
91662 sqlite3_str_append(p: &out, z: "-- ", N: 3);
91663 assert( (zRawSql - zStart) > 0 );
91664 sqlite3_str_append(p: &out, z: zStart, N: (int)(zRawSql-zStart));
91665 }
91666 }else if( p->nVar==0 ){
91667 sqlite3_str_append(p: &out, z: zRawSql, N: sqlite3Strlen30(z: zRawSql));
91668 }else{
91669 while( zRawSql[0] ){
91670 n = findNextHostParameter(zSql: zRawSql, pnToken: &nToken);
91671 assert( n>0 );
91672 sqlite3_str_append(p: &out, z: zRawSql, N: n);
91673 zRawSql += n;
91674 assert( zRawSql[0] || nToken==0 );
91675 if( nToken==0 ) break;
91676 if( zRawSql[0]=='?' ){
91677 if( nToken>1 ){
91678 assert( sqlite3Isdigit(zRawSql[1]) );
91679 sqlite3GetInt32(zNum: &zRawSql[1], pValue: &idx);
91680 }else{
91681 idx = nextIndex;
91682 }
91683 }else{
91684 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
91685 zRawSql[0]=='@' || zRawSql[0]=='#' );
91686 testcase( zRawSql[0]==':' );
91687 testcase( zRawSql[0]=='$' );
91688 testcase( zRawSql[0]=='@' );
91689 testcase( zRawSql[0]=='#' );
91690 idx = sqlite3VdbeParameterIndex(p, zName: zRawSql, nName: nToken);
91691 assert( idx>0 );
91692 }
91693 zRawSql += nToken;
91694 nextIndex = MAX(idx + 1, nextIndex);
91695 assert( idx>0 && idx<=p->nVar );
91696 pVar = &p->aVar[idx-1];
91697 if( pVar->flags & MEM_Null ){
91698 sqlite3_str_append(p: &out, z: "NULL", N: 4);
91699 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
91700 sqlite3_str_appendf(p: &out, zFormat: "%lld", pVar->u.i);
91701 }else if( pVar->flags & MEM_Real ){
91702 sqlite3_str_appendf(p: &out, zFormat: "%!.15g", pVar->u.r);
91703 }else if( pVar->flags & MEM_Str ){
91704 int nOut; /* Number of bytes of the string text to include in output */
91705#ifndef SQLITE_OMIT_UTF16
91706 u8 enc = ENC(db);
91707 if( enc!=SQLITE_UTF8 ){
91708 memset(s: &utf8, c: 0, n: sizeof(utf8));
91709 utf8.db = db;
91710 sqlite3VdbeMemSetStr(pMem: &utf8, z: pVar->z, n: pVar->n, enc, SQLITE_STATIC);
91711 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(pMem: &utf8, SQLITE_UTF8) ){
91712 out.accError = SQLITE_NOMEM;
91713 out.nAlloc = 0;
91714 }
91715 pVar = &utf8;
91716 }
91717#endif
91718 nOut = pVar->n;
91719#ifdef SQLITE_TRACE_SIZE_LIMIT
91720 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
91721 nOut = SQLITE_TRACE_SIZE_LIMIT;
91722 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
91723 }
91724#endif
91725 sqlite3_str_appendf(p: &out, zFormat: "'%.*q'", nOut, pVar->z);
91726#ifdef SQLITE_TRACE_SIZE_LIMIT
91727 if( nOut<pVar->n ){
91728 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
91729 }
91730#endif
91731#ifndef SQLITE_OMIT_UTF16
91732 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(p: &utf8);
91733#endif
91734 }else if( pVar->flags & MEM_Zero ){
91735 sqlite3_str_appendf(p: &out, zFormat: "zeroblob(%d)", pVar->u.nZero);
91736 }else{
91737 int nOut; /* Number of bytes of the blob to include in output */
91738 assert( pVar->flags & MEM_Blob );
91739 sqlite3_str_append(p: &out, z: "x'", N: 2);
91740 nOut = pVar->n;
91741#ifdef SQLITE_TRACE_SIZE_LIMIT
91742 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
91743#endif
91744 for(i=0; i<nOut; i++){
91745 sqlite3_str_appendf(p: &out, zFormat: "%02x", pVar->z[i]&0xff);
91746 }
91747 sqlite3_str_append(p: &out, z: "'", N: 1);
91748#ifdef SQLITE_TRACE_SIZE_LIMIT
91749 if( nOut<pVar->n ){
91750 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
91751 }
91752#endif
91753 }
91754 }
91755 }
91756 if( out.accError ) sqlite3_str_reset(p: &out);
91757 return sqlite3StrAccumFinish(p: &out);
91758}
91759
91760#endif /* #ifndef SQLITE_OMIT_TRACE */
91761
91762/************** End of vdbetrace.c *******************************************/
91763/************** Begin file vdbe.c ********************************************/
91764/*
91765** 2001 September 15
91766**
91767** The author disclaims copyright to this source code. In place of
91768** a legal notice, here is a blessing:
91769**
91770** May you do good and not evil.
91771** May you find forgiveness for yourself and forgive others.
91772** May you share freely, never taking more than you give.
91773**
91774*************************************************************************
91775** The code in this file implements the function that runs the
91776** bytecode of a prepared statement.
91777**
91778** Various scripts scan this source file in order to generate HTML
91779** documentation, headers files, or other derived files. The formatting
91780** of the code in this file is, therefore, important. See other comments
91781** in this file for details. If in doubt, do not deviate from existing
91782** commenting and indentation practices when changing or adding code.
91783*/
91784/* #include "sqliteInt.h" */
91785/* #include "vdbeInt.h" */
91786
91787/*
91788** Invoke this macro on memory cells just prior to changing the
91789** value of the cell. This macro verifies that shallow copies are
91790** not misused. A shallow copy of a string or blob just copies a
91791** pointer to the string or blob, not the content. If the original
91792** is changed while the copy is still in use, the string or blob might
91793** be changed out from under the copy. This macro verifies that nothing
91794** like that ever happens.
91795*/
91796#ifdef SQLITE_DEBUG
91797# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
91798#else
91799# define memAboutToChange(P,M)
91800#endif
91801
91802/*
91803** The following global variable is incremented every time a cursor
91804** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
91805** procedures use this information to make sure that indices are
91806** working correctly. This variable has no function other than to
91807** help verify the correct operation of the library.
91808*/
91809#ifdef SQLITE_TEST
91810SQLITE_API int sqlite3_search_count = 0;
91811#endif
91812
91813/*
91814** When this global variable is positive, it gets decremented once before
91815** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
91816** field of the sqlite3 structure is set in order to simulate an interrupt.
91817**
91818** This facility is used for testing purposes only. It does not function
91819** in an ordinary build.
91820*/
91821#ifdef SQLITE_TEST
91822SQLITE_API int sqlite3_interrupt_count = 0;
91823#endif
91824
91825/*
91826** The next global variable is incremented each type the OP_Sort opcode
91827** is executed. The test procedures use this information to make sure that
91828** sorting is occurring or not occurring at appropriate times. This variable
91829** has no function other than to help verify the correct operation of the
91830** library.
91831*/
91832#ifdef SQLITE_TEST
91833SQLITE_API int sqlite3_sort_count = 0;
91834#endif
91835
91836/*
91837** The next global variable records the size of the largest MEM_Blob
91838** or MEM_Str that has been used by a VDBE opcode. The test procedures
91839** use this information to make sure that the zero-blob functionality
91840** is working correctly. This variable has no function other than to
91841** help verify the correct operation of the library.
91842*/
91843#ifdef SQLITE_TEST
91844SQLITE_API int sqlite3_max_blobsize = 0;
91845static void updateMaxBlobsize(Mem *p){
91846 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
91847 sqlite3_max_blobsize = p->n;
91848 }
91849}
91850#endif
91851
91852/*
91853** This macro evaluates to true if either the update hook or the preupdate
91854** hook are enabled for database connect DB.
91855*/
91856#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91857# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
91858#else
91859# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
91860#endif
91861
91862/*
91863** The next global variable is incremented each time the OP_Found opcode
91864** is executed. This is used to test whether or not the foreign key
91865** operation implemented using OP_FkIsZero is working. This variable
91866** has no function other than to help verify the correct operation of the
91867** library.
91868*/
91869#ifdef SQLITE_TEST
91870SQLITE_API int sqlite3_found_count = 0;
91871#endif
91872
91873/*
91874** Test a register to see if it exceeds the current maximum blob size.
91875** If it does, record the new maximum blob size.
91876*/
91877#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
91878# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
91879#else
91880# define UPDATE_MAX_BLOBSIZE(P)
91881#endif
91882
91883#ifdef SQLITE_DEBUG
91884/* This routine provides a convenient place to set a breakpoint during
91885** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
91886** each opcode is printed. Variables "pc" (program counter) and pOp are
91887** available to add conditionals to the breakpoint. GDB example:
91888**
91889** break test_trace_breakpoint if pc=22
91890**
91891** Other useful labels for breakpoints include:
91892** test_addop_breakpoint(pc,pOp)
91893** sqlite3CorruptError(lineno)
91894** sqlite3MisuseError(lineno)
91895** sqlite3CantopenError(lineno)
91896*/
91897static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
91898 static int n = 0;
91899 (void)pc;
91900 (void)pOp;
91901 (void)v;
91902 n++;
91903}
91904#endif
91905
91906/*
91907** Invoke the VDBE coverage callback, if that callback is defined. This
91908** feature is used for test suite validation only and does not appear an
91909** production builds.
91910**
91911** M is the type of branch. I is the direction taken for this instance of
91912** the branch.
91913**
91914** M: 2 - two-way branch (I=0: fall-thru 1: jump )
91915** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
91916** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
91917**
91918** In other words, if M is 2, then I is either 0 (for fall-through) or
91919** 1 (for when the branch is taken). If M is 3, the I is 0 for an
91920** ordinary fall-through, I is 1 if the branch was taken, and I is 2
91921** if the result of comparison is NULL. For M=3, I=2 the jump may or
91922** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
91923** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
91924** depending on if the operands are less than, equal, or greater than.
91925**
91926** iSrcLine is the source code line (from the __LINE__ macro) that
91927** generated the VDBE instruction combined with flag bits. The source
91928** code line number is in the lower 24 bits of iSrcLine and the upper
91929** 8 bytes are flags. The lower three bits of the flags indicate
91930** values for I that should never occur. For example, if the branch is
91931** always taken, the flags should be 0x05 since the fall-through and
91932** alternate branch are never taken. If a branch is never taken then
91933** flags should be 0x06 since only the fall-through approach is allowed.
91934**
91935** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
91936** interested in equal or not-equal. In other words, I==0 and I==2
91937** should be treated as equivalent
91938**
91939** Since only a line number is retained, not the filename, this macro
91940** only works for amalgamation builds. But that is ok, since these macros
91941** should be no-ops except for special builds used to measure test coverage.
91942*/
91943#if !defined(SQLITE_VDBE_COVERAGE)
91944# define VdbeBranchTaken(I,M)
91945#else
91946# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
91947 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
91948 u8 mNever;
91949 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
91950 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
91951 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
91952 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
91953 I = 1<<I;
91954 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
91955 ** the flags indicate directions that the branch can never go. If
91956 ** a branch really does go in one of those directions, assert right
91957 ** away. */
91958 mNever = iSrcLine >> 24;
91959 assert( (I & mNever)==0 );
91960 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
91961 /* Invoke the branch coverage callback with three arguments:
91962 ** iSrcLine - the line number of the VdbeCoverage() macro, with
91963 ** flags removed.
91964 ** I - Mask of bits 0x07 indicating which cases are are
91965 ** fulfilled by this instance of the jump. 0x01 means
91966 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
91967 ** impossible cases (ex: if the comparison is never NULL)
91968 ** are filled in automatically so that the coverage
91969 ** measurement logic does not flag those impossible cases
91970 ** as missed coverage.
91971 ** M - Type of jump. Same as M argument above
91972 */
91973 I |= mNever;
91974 if( M==2 ) I |= 0x04;
91975 if( M==4 ){
91976 I |= 0x08;
91977 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
91978 }
91979 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
91980 iSrcLine&0xffffff, I, M);
91981 }
91982#endif
91983
91984/*
91985** An ephemeral string value (signified by the MEM_Ephem flag) contains
91986** a pointer to a dynamically allocated string where some other entity
91987** is responsible for deallocating that string. Because the register
91988** does not control the string, it might be deleted without the register
91989** knowing it.
91990**
91991** This routine converts an ephemeral string into a dynamically allocated
91992** string that the register itself controls. In other words, it
91993** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
91994*/
91995#define Deephemeralize(P) \
91996 if( ((P)->flags&MEM_Ephem)!=0 \
91997 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
91998
91999/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
92000#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
92001
92002/*
92003** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
92004** if we run out of memory.
92005*/
92006static VdbeCursor *allocateCursor(
92007 Vdbe *p, /* The virtual machine */
92008 int iCur, /* Index of the new VdbeCursor */
92009 int nField, /* Number of fields in the table or index */
92010 u8 eCurType /* Type of the new cursor */
92011){
92012 /* Find the memory cell that will be used to store the blob of memory
92013 ** required for this VdbeCursor structure. It is convenient to use a
92014 ** vdbe memory cell to manage the memory allocation required for a
92015 ** VdbeCursor structure for the following reasons:
92016 **
92017 ** * Sometimes cursor numbers are used for a couple of different
92018 ** purposes in a vdbe program. The different uses might require
92019 ** different sized allocations. Memory cells provide growable
92020 ** allocations.
92021 **
92022 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
92023 ** be freed lazily via the sqlite3_release_memory() API. This
92024 ** minimizes the number of malloc calls made by the system.
92025 **
92026 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
92027 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
92028 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
92029 */
92030 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
92031
92032 int nByte;
92033 VdbeCursor *pCx = 0;
92034 nByte =
92035 ROUND8P(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
92036 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
92037
92038 assert( iCur>=0 && iCur<p->nCursor );
92039 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
92040 sqlite3VdbeFreeCursorNN(p, pCx: p->apCsr[iCur]);
92041 p->apCsr[iCur] = 0;
92042 }
92043
92044 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
92045 ** the pMem used to hold space for the cursor has enough storage available
92046 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
92047 ** to hold cursors, it is faster to in-line the logic. */
92048 assert( pMem->flags==MEM_Undefined );
92049 assert( (pMem->flags & MEM_Dyn)==0 );
92050 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
92051 if( pMem->szMalloc<nByte ){
92052 if( pMem->szMalloc>0 ){
92053 sqlite3DbFreeNN(db: pMem->db, p: pMem->zMalloc);
92054 }
92055 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(db: pMem->db, n: nByte);
92056 if( pMem->zMalloc==0 ){
92057 pMem->szMalloc = 0;
92058 return 0;
92059 }
92060 pMem->szMalloc = nByte;
92061 }
92062
92063 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
92064 memset(s: pCx, c: 0, offsetof(VdbeCursor,pAltCursor));
92065 pCx->eCurType = eCurType;
92066 pCx->nField = nField;
92067 pCx->aOffset = &pCx->aType[nField];
92068 if( eCurType==CURTYPE_BTREE ){
92069 pCx->uc.pCursor = (BtCursor*)
92070 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
92071 sqlite3BtreeCursorZero(p: pCx->uc.pCursor);
92072 }
92073 return pCx;
92074}
92075
92076/*
92077** The string in pRec is known to look like an integer and to have a
92078** floating point value of rValue. Return true and set *piValue to the
92079** integer value if the string is in range to be an integer. Otherwise,
92080** return false.
92081*/
92082static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
92083 i64 iValue;
92084 iValue = sqlite3RealToI64(r: rValue);
92085 if( sqlite3RealSameAsInt(r1: rValue,i: iValue) ){
92086 *piValue = iValue;
92087 return 1;
92088 }
92089 return 0==sqlite3Atoi64(zNum: pRec->z, pNum: piValue, length: pRec->n, enc: pRec->enc);
92090}
92091
92092/*
92093** Try to convert a value into a numeric representation if we can
92094** do so without loss of information. In other words, if the string
92095** looks like a number, convert it into a number. If it does not
92096** look like a number, leave it alone.
92097**
92098** If the bTryForInt flag is true, then extra effort is made to give
92099** an integer representation. Strings that look like floating point
92100** values but which have no fractional component (example: '48.00')
92101** will have a MEM_Int representation when bTryForInt is true.
92102**
92103** If bTryForInt is false, then if the input string contains a decimal
92104** point or exponential notation, the result is only MEM_Real, even
92105** if there is an exact integer representation of the quantity.
92106*/
92107static void applyNumericAffinity(Mem *pRec, int bTryForInt){
92108 double rValue;
92109 u8 enc = pRec->enc;
92110 int rc;
92111 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
92112 rc = sqlite3AtoF(z: pRec->z, pResult: &rValue, length: pRec->n, enc);
92113 if( rc<=0 ) return;
92114 if( rc==1 && alsoAnInt(pRec, rValue, piValue: &pRec->u.i) ){
92115 pRec->flags |= MEM_Int;
92116 }else{
92117 pRec->u.r = rValue;
92118 pRec->flags |= MEM_Real;
92119 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pMem: pRec);
92120 }
92121 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
92122 ** string representation after computing a numeric equivalent, because the
92123 ** string representation might not be the canonical representation for the
92124 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
92125 pRec->flags &= ~MEM_Str;
92126}
92127
92128/*
92129** Processing is determine by the affinity parameter:
92130**
92131** SQLITE_AFF_INTEGER:
92132** SQLITE_AFF_REAL:
92133** SQLITE_AFF_NUMERIC:
92134** Try to convert pRec to an integer representation or a
92135** floating-point representation if an integer representation
92136** is not possible. Note that the integer representation is
92137** always preferred, even if the affinity is REAL, because
92138** an integer representation is more space efficient on disk.
92139**
92140** SQLITE_AFF_FLEXNUM:
92141** If the value is text, then try to convert it into a number of
92142** some kind (integer or real) but do not make any other changes.
92143**
92144** SQLITE_AFF_TEXT:
92145** Convert pRec to a text representation.
92146**
92147** SQLITE_AFF_BLOB:
92148** SQLITE_AFF_NONE:
92149** No-op. pRec is unchanged.
92150*/
92151static void applyAffinity(
92152 Mem *pRec, /* The value to apply affinity to */
92153 char affinity, /* The affinity to be applied */
92154 u8 enc /* Use this text encoding */
92155){
92156 if( affinity>=SQLITE_AFF_NUMERIC ){
92157 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
92158 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM );
92159 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
92160 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
92161 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,bTryForInt: 1);
92162 }else if( affinity<=SQLITE_AFF_REAL ){
92163 sqlite3VdbeIntegerAffinity(pMem: pRec);
92164 }
92165 }
92166 }else if( affinity==SQLITE_AFF_TEXT ){
92167 /* Only attempt the conversion to TEXT if there is an integer or real
92168 ** representation (blob and NULL do not get converted) but no string
92169 ** representation. It would be harmless to repeat the conversion if
92170 ** there is already a string rep, but it is pointless to waste those
92171 ** CPU cycles. */
92172 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
92173 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
92174 testcase( pRec->flags & MEM_Int );
92175 testcase( pRec->flags & MEM_Real );
92176 testcase( pRec->flags & MEM_IntReal );
92177 sqlite3VdbeMemStringify(pMem: pRec, enc, bForce: 1);
92178 }
92179 }
92180 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
92181 }
92182}
92183
92184/*
92185** Try to convert the type of a function argument or a result column
92186** into a numeric representation. Use either INTEGER or REAL whichever
92187** is appropriate. But only do the conversion if it is possible without
92188** loss of information and return the revised type of the argument.
92189*/
92190SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
92191 int eType = sqlite3_value_type(pVal);
92192 if( eType==SQLITE_TEXT ){
92193 Mem *pMem = (Mem*)pVal;
92194 applyNumericAffinity(pRec: pMem, bTryForInt: 0);
92195 eType = sqlite3_value_type(pVal);
92196 }
92197 return eType;
92198}
92199
92200/*
92201** Exported version of applyAffinity(). This one works on sqlite3_value*,
92202** not the internal Mem* type.
92203*/
92204SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
92205 sqlite3_value *pVal,
92206 u8 affinity,
92207 u8 enc
92208){
92209 applyAffinity(pRec: (Mem *)pVal, affinity, enc);
92210}
92211
92212/*
92213** pMem currently only holds a string type (or maybe a BLOB that we can
92214** interpret as a string if we want to). Compute its corresponding
92215** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
92216** accordingly.
92217*/
92218static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
92219 int rc;
92220 sqlite3_int64 ix;
92221 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
92222 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
92223 if( ExpandBlob(pMem) ){
92224 pMem->u.i = 0;
92225 return MEM_Int;
92226 }
92227 rc = sqlite3AtoF(z: pMem->z, pResult: &pMem->u.r, length: pMem->n, enc: pMem->enc);
92228 if( rc<=0 ){
92229 if( rc==0 && sqlite3Atoi64(zNum: pMem->z, pNum: &ix, length: pMem->n, enc: pMem->enc)<=1 ){
92230 pMem->u.i = ix;
92231 return MEM_Int;
92232 }else{
92233 return MEM_Real;
92234 }
92235 }else if( rc==1 && sqlite3Atoi64(zNum: pMem->z, pNum: &ix, length: pMem->n, enc: pMem->enc)==0 ){
92236 pMem->u.i = ix;
92237 return MEM_Int;
92238 }
92239 return MEM_Real;
92240}
92241
92242/*
92243** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
92244** none.
92245**
92246** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
92247** But it does set pMem->u.r and pMem->u.i appropriately.
92248*/
92249static u16 numericType(Mem *pMem){
92250 assert( (pMem->flags & MEM_Null)==0
92251 || pMem->db==0 || pMem->db->mallocFailed );
92252 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
92253 testcase( pMem->flags & MEM_Int );
92254 testcase( pMem->flags & MEM_Real );
92255 testcase( pMem->flags & MEM_IntReal );
92256 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
92257 }
92258 assert( pMem->flags & (MEM_Str|MEM_Blob) );
92259 testcase( pMem->flags & MEM_Str );
92260 testcase( pMem->flags & MEM_Blob );
92261 return computeNumericType(pMem);
92262 return 0;
92263}
92264
92265#ifdef SQLITE_DEBUG
92266/*
92267** Write a nice string representation of the contents of cell pMem
92268** into buffer zBuf, length nBuf.
92269*/
92270SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
92271 int f = pMem->flags;
92272 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
92273 if( f&MEM_Blob ){
92274 int i;
92275 char c;
92276 if( f & MEM_Dyn ){
92277 c = 'z';
92278 assert( (f & (MEM_Static|MEM_Ephem))==0 );
92279 }else if( f & MEM_Static ){
92280 c = 't';
92281 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
92282 }else if( f & MEM_Ephem ){
92283 c = 'e';
92284 assert( (f & (MEM_Static|MEM_Dyn))==0 );
92285 }else{
92286 c = 's';
92287 }
92288 sqlite3_str_appendf(pStr, "%cx[", c);
92289 for(i=0; i<25 && i<pMem->n; i++){
92290 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
92291 }
92292 sqlite3_str_appendf(pStr, "|");
92293 for(i=0; i<25 && i<pMem->n; i++){
92294 char z = pMem->z[i];
92295 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
92296 }
92297 sqlite3_str_appendf(pStr,"]");
92298 if( f & MEM_Zero ){
92299 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
92300 }
92301 }else if( f & MEM_Str ){
92302 int j;
92303 u8 c;
92304 if( f & MEM_Dyn ){
92305 c = 'z';
92306 assert( (f & (MEM_Static|MEM_Ephem))==0 );
92307 }else if( f & MEM_Static ){
92308 c = 't';
92309 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
92310 }else if( f & MEM_Ephem ){
92311 c = 'e';
92312 assert( (f & (MEM_Static|MEM_Dyn))==0 );
92313 }else{
92314 c = 's';
92315 }
92316 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
92317 for(j=0; j<25 && j<pMem->n; j++){
92318 c = pMem->z[j];
92319 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
92320 }
92321 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
92322 if( f & MEM_Term ){
92323 sqlite3_str_appendf(pStr, "(0-term)");
92324 }
92325 }
92326}
92327#endif
92328
92329#ifdef SQLITE_DEBUG
92330/*
92331** Print the value of a register for tracing purposes:
92332*/
92333static void memTracePrint(Mem *p){
92334 if( p->flags & MEM_Undefined ){
92335 printf(" undefined");
92336 }else if( p->flags & MEM_Null ){
92337 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
92338 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
92339 printf(" si:%lld", p->u.i);
92340 }else if( (p->flags & (MEM_IntReal))!=0 ){
92341 printf(" ir:%lld", p->u.i);
92342 }else if( p->flags & MEM_Int ){
92343 printf(" i:%lld", p->u.i);
92344#ifndef SQLITE_OMIT_FLOATING_POINT
92345 }else if( p->flags & MEM_Real ){
92346 printf(" r:%.17g", p->u.r);
92347#endif
92348 }else if( sqlite3VdbeMemIsRowSet(p) ){
92349 printf(" (rowset)");
92350 }else{
92351 StrAccum acc;
92352 char zBuf[1000];
92353 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
92354 sqlite3VdbeMemPrettyPrint(p, &acc);
92355 printf(" %s", sqlite3StrAccumFinish(&acc));
92356 }
92357 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
92358}
92359static void registerTrace(int iReg, Mem *p){
92360 printf("R[%d] = ", iReg);
92361 memTracePrint(p);
92362 if( p->pScopyFrom ){
92363 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
92364 }
92365 printf("\n");
92366 sqlite3VdbeCheckMemInvariants(p);
92367}
92368/**/ void sqlite3PrintMem(Mem *pMem){
92369 memTracePrint(pMem);
92370 printf("\n");
92371 fflush(stdout);
92372}
92373#endif
92374
92375#ifdef SQLITE_DEBUG
92376/*
92377** Show the values of all registers in the virtual machine. Used for
92378** interactive debugging.
92379*/
92380SQLITE_PRIVATE void sqlite3VdbeRegisterDump(Vdbe *v){
92381 int i;
92382 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
92383}
92384#endif /* SQLITE_DEBUG */
92385
92386
92387#ifdef SQLITE_DEBUG
92388# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
92389#else
92390# define REGISTER_TRACE(R,M)
92391#endif
92392
92393#ifndef NDEBUG
92394/*
92395** This function is only called from within an assert() expression. It
92396** checks that the sqlite3.nTransaction variable is correctly set to
92397** the number of non-transaction savepoints currently in the
92398** linked list starting at sqlite3.pSavepoint.
92399**
92400** Usage:
92401**
92402** assert( checkSavepointCount(db) );
92403*/
92404static int checkSavepointCount(sqlite3 *db){
92405 int n = 0;
92406 Savepoint *p;
92407 for(p=db->pSavepoint; p; p=p->pNext) n++;
92408 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
92409 return 1;
92410}
92411#endif
92412
92413/*
92414** Return the register of pOp->p2 after first preparing it to be
92415** overwritten with an integer value.
92416*/
92417static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
92418 sqlite3VdbeMemSetNull(pMem: pOut);
92419 pOut->flags = MEM_Int;
92420 return pOut;
92421}
92422static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
92423 Mem *pOut;
92424 assert( pOp->p2>0 );
92425 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
92426 pOut = &p->aMem[pOp->p2];
92427 memAboutToChange(p, pOut);
92428 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
92429 return out2PrereleaseWithClear(pOut);
92430 }else{
92431 pOut->flags = MEM_Int;
92432 return pOut;
92433 }
92434}
92435
92436/*
92437** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
92438** with pOp->p3. Return the hash.
92439*/
92440static u64 filterHash(const Mem *aMem, const Op *pOp){
92441 int i, mx;
92442 u64 h = 0;
92443
92444 assert( pOp->p4type==P4_INT32 );
92445 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
92446 const Mem *p = &aMem[i];
92447 if( p->flags & (MEM_Int|MEM_IntReal) ){
92448 h += p->u.i;
92449 }else if( p->flags & MEM_Real ){
92450 h += sqlite3VdbeIntValue(pMem: p);
92451 }else if( p->flags & (MEM_Str|MEM_Blob) ){
92452 /* All strings have the same hash and all blobs have the same hash,
92453 ** though, at least, those hashes are different from each other and
92454 ** from NULL. */
92455 h += 4093 + (p->flags & (MEM_Str|MEM_Blob));
92456 }
92457 }
92458 return h;
92459}
92460
92461
92462/*
92463** For OP_Column, factor out the case where content is loaded from
92464** overflow pages, so that the code to implement this case is separate
92465** the common case where all content fits on the page. Factoring out
92466** the code reduces register pressure and helps the common case
92467** to run faster.
92468*/
92469static SQLITE_NOINLINE int vdbeColumnFromOverflow(
92470 VdbeCursor *pC, /* The BTree cursor from which we are reading */
92471 int iCol, /* The column to read */
92472 int t, /* The serial-type code for the column value */
92473 i64 iOffset, /* Offset to the start of the content value */
92474 u32 cacheStatus, /* Current Vdbe.cacheCtr value */
92475 u32 colCacheCtr, /* Current value of the column cache counter */
92476 Mem *pDest /* Store the value into this register. */
92477){
92478 int rc;
92479 sqlite3 *db = pDest->db;
92480 int encoding = pDest->enc;
92481 int len = sqlite3VdbeSerialTypeLen(serial_type: t);
92482 assert( pC->eCurType==CURTYPE_BTREE );
92483 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG;
92484 if( len > 4000 && pC->pKeyInfo==0 ){
92485 /* Cache large column values that are on overflow pages using
92486 ** an RCStr (reference counted string) so that if they are reloaded,
92487 ** that do not have to be copied a second time. The overhead of
92488 ** creating and managing the cache is such that this is only
92489 ** profitable for larger TEXT and BLOB values.
92490 **
92491 ** Only do this on table-btrees so that writes to index-btrees do not
92492 ** need to clear the cache. This buys performance in the common case
92493 ** in exchange for generality.
92494 */
92495 VdbeTxtBlbCache *pCache;
92496 char *pBuf;
92497 if( pC->colCache==0 ){
92498 pC->pCache = sqlite3DbMallocZero(db, n: sizeof(VdbeTxtBlbCache) );
92499 if( pC->pCache==0 ) return SQLITE_NOMEM;
92500 pC->colCache = 1;
92501 }
92502 pCache = pC->pCache;
92503 if( pCache->pCValue==0
92504 || pCache->iCol!=iCol
92505 || pCache->cacheStatus!=cacheStatus
92506 || pCache->colCacheCtr!=colCacheCtr
92507 || pCache->iOffset!=sqlite3BtreeOffset(pCur: pC->uc.pCursor)
92508 ){
92509 if( pCache->pCValue ) sqlite3RCStrUnref(z: pCache->pCValue);
92510 pBuf = pCache->pCValue = sqlite3RCStrNew( N: len+3 );
92511 if( pBuf==0 ) return SQLITE_NOMEM;
92512 rc = sqlite3BtreePayload(pCur: pC->uc.pCursor, offset: iOffset, amt: len, pBuf);
92513 if( rc ) return rc;
92514 pBuf[len] = 0;
92515 pBuf[len+1] = 0;
92516 pBuf[len+2] = 0;
92517 pCache->iCol = iCol;
92518 pCache->cacheStatus = cacheStatus;
92519 pCache->colCacheCtr = colCacheCtr;
92520 pCache->iOffset = sqlite3BtreeOffset(pCur: pC->uc.pCursor);
92521 }else{
92522 pBuf = pCache->pCValue;
92523 }
92524 assert( t>=12 );
92525 sqlite3RCStrRef(z: pBuf);
92526 if( t&1 ){
92527 rc = sqlite3VdbeMemSetStr(pMem: pDest, z: pBuf, n: len, enc: encoding,
92528 xDel: (void(*)(void*))sqlite3RCStrUnref);
92529 pDest->flags |= MEM_Term;
92530 }else{
92531 rc = sqlite3VdbeMemSetStr(pMem: pDest, z: pBuf, n: len, enc: 0,
92532 xDel: (void(*)(void*))sqlite3RCStrUnref);
92533 }
92534 }else{
92535 rc = sqlite3VdbeMemFromBtree(pCur: pC->uc.pCursor, offset: iOffset, amt: len, pMem: pDest);
92536 if( rc ) return rc;
92537 sqlite3VdbeSerialGet(buf: (const u8*)pDest->z, serial_type: t, pMem: pDest);
92538 if( (t&1)!=0 && encoding==SQLITE_UTF8 ){
92539 pDest->z[len] = 0;
92540 pDest->flags |= MEM_Term;
92541 }
92542 }
92543 pDest->flags &= ~MEM_Ephem;
92544 return rc;
92545}
92546
92547
92548/*
92549** Return the symbolic name for the data type of a pMem
92550*/
92551static const char *vdbeMemTypeName(Mem *pMem){
92552 static const char *azTypes[] = {
92553 /* SQLITE_INTEGER */ "INT",
92554 /* SQLITE_FLOAT */ "REAL",
92555 /* SQLITE_TEXT */ "TEXT",
92556 /* SQLITE_BLOB */ "BLOB",
92557 /* SQLITE_NULL */ "NULL"
92558 };
92559 return azTypes[sqlite3_value_type(pVal: pMem)-1];
92560}
92561
92562/*
92563** Execute as much of a VDBE program as we can.
92564** This is the core of sqlite3_step().
92565*/
92566SQLITE_PRIVATE int sqlite3VdbeExec(
92567 Vdbe *p /* The VDBE */
92568){
92569 Op *aOp = p->aOp; /* Copy of p->aOp */
92570 Op *pOp = aOp; /* Current operation */
92571#ifdef SQLITE_DEBUG
92572 Op *pOrigOp; /* Value of pOp at the top of the loop */
92573 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
92574 u8 iCompareIsInit = 0; /* iCompare is initialized */
92575#endif
92576 int rc = SQLITE_OK; /* Value to return */
92577 sqlite3 *db = p->db; /* The database */
92578 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
92579 u8 encoding = ENC(db); /* The database encoding */
92580 int iCompare = 0; /* Result of last comparison */
92581 u64 nVmStep = 0; /* Number of virtual machine steps */
92582#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
92583 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
92584#endif
92585 Mem *aMem = p->aMem; /* Copy of p->aMem */
92586 Mem *pIn1 = 0; /* 1st input operand */
92587 Mem *pIn2 = 0; /* 2nd input operand */
92588 Mem *pIn3 = 0; /* 3rd input operand */
92589 Mem *pOut = 0; /* Output operand */
92590 u32 colCacheCtr = 0; /* Column cache counter */
92591#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
92592 u64 *pnCycle = 0;
92593 int bStmtScanStatus = IS_STMT_SCANSTATUS(db)!=0;
92594#endif
92595 /*** INSERT STACK UNION HERE ***/
92596
92597 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
92598 if( DbMaskNonZero(p->lockMask) ){
92599 sqlite3VdbeEnter(p);
92600 }
92601#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
92602 if( db->xProgress ){
92603 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
92604 assert( 0 < db->nProgressOps );
92605 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
92606 }else{
92607 nProgressLimit = LARGEST_UINT64;
92608 }
92609#endif
92610 if( p->rc==SQLITE_NOMEM ){
92611 /* This happens if a malloc() inside a call to sqlite3_column_text() or
92612 ** sqlite3_column_text16() failed. */
92613 goto no_mem;
92614 }
92615 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
92616 testcase( p->rc!=SQLITE_OK );
92617 p->rc = SQLITE_OK;
92618 assert( p->bIsReader || p->readOnly!=0 );
92619 p->iCurrentTime = 0;
92620 assert( p->explain==0 );
92621 db->busyHandler.nBusy = 0;
92622 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
92623 sqlite3VdbeIOTraceSql(p);
92624#ifdef SQLITE_DEBUG
92625 sqlite3BeginBenignMalloc();
92626 if( p->pc==0
92627 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
92628 ){
92629 int i;
92630 int once = 1;
92631 sqlite3VdbePrintSql(p);
92632 if( p->db->flags & SQLITE_VdbeListing ){
92633 printf("VDBE Program Listing:\n");
92634 for(i=0; i<p->nOp; i++){
92635 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
92636 }
92637 }
92638 if( p->db->flags & SQLITE_VdbeEQP ){
92639 for(i=0; i<p->nOp; i++){
92640 if( aOp[i].opcode==OP_Explain ){
92641 if( once ) printf("VDBE Query Plan:\n");
92642 printf("%s\n", aOp[i].p4.z);
92643 once = 0;
92644 }
92645 }
92646 }
92647 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
92648 }
92649 sqlite3EndBenignMalloc();
92650#endif
92651 for(pOp=&aOp[p->pc]; 1; pOp++){
92652 /* Errors are detected by individual opcodes, with an immediate
92653 ** jumps to abort_due_to_error. */
92654 assert( rc==SQLITE_OK );
92655
92656 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
92657 nVmStep++;
92658
92659#if defined(VDBE_PROFILE)
92660 pOp->nExec++;
92661 pnCycle = &pOp->nCycle;
92662 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
92663#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
92664 if( bStmtScanStatus ){
92665 pOp->nExec++;
92666 pnCycle = &pOp->nCycle;
92667 *pnCycle -= sqlite3Hwtime();
92668 }
92669#endif
92670
92671 /* Only allow tracing if SQLITE_DEBUG is defined.
92672 */
92673#ifdef SQLITE_DEBUG
92674 if( db->flags & SQLITE_VdbeTrace ){
92675 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
92676 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
92677 }
92678#endif
92679
92680
92681 /* Check to see if we need to simulate an interrupt. This only happens
92682 ** if we have a special test build.
92683 */
92684#ifdef SQLITE_TEST
92685 if( sqlite3_interrupt_count>0 ){
92686 sqlite3_interrupt_count--;
92687 if( sqlite3_interrupt_count==0 ){
92688 sqlite3_interrupt(db);
92689 }
92690 }
92691#endif
92692
92693 /* Sanity checking on other operands */
92694#ifdef SQLITE_DEBUG
92695 {
92696 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
92697 if( (opProperty & OPFLG_IN1)!=0 ){
92698 assert( pOp->p1>0 );
92699 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
92700 assert( memIsValid(&aMem[pOp->p1]) );
92701 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
92702 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
92703 }
92704 if( (opProperty & OPFLG_IN2)!=0 ){
92705 assert( pOp->p2>0 );
92706 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
92707 assert( memIsValid(&aMem[pOp->p2]) );
92708 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
92709 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
92710 }
92711 if( (opProperty & OPFLG_IN3)!=0 ){
92712 assert( pOp->p3>0 );
92713 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
92714 assert( memIsValid(&aMem[pOp->p3]) );
92715 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
92716 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
92717 }
92718 if( (opProperty & OPFLG_OUT2)!=0 ){
92719 assert( pOp->p2>0 );
92720 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
92721 memAboutToChange(p, &aMem[pOp->p2]);
92722 }
92723 if( (opProperty & OPFLG_OUT3)!=0 ){
92724 assert( pOp->p3>0 );
92725 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
92726 memAboutToChange(p, &aMem[pOp->p3]);
92727 }
92728 }
92729#endif
92730#ifdef SQLITE_DEBUG
92731 pOrigOp = pOp;
92732#endif
92733
92734 switch( pOp->opcode ){
92735
92736/*****************************************************************************
92737** What follows is a massive switch statement where each case implements a
92738** separate instruction in the virtual machine. If we follow the usual
92739** indentation conventions, each case should be indented by 6 spaces. But
92740** that is a lot of wasted space on the left margin. So the code within
92741** the switch statement will break with convention and be flush-left. Another
92742** big comment (similar to this one) will mark the point in the code where
92743** we transition back to normal indentation.
92744**
92745** The formatting of each case is important. The makefile for SQLite
92746** generates two C files "opcodes.h" and "opcodes.c" by scanning this
92747** file looking for lines that begin with "case OP_". The opcodes.h files
92748** will be filled with #defines that give unique integer values to each
92749** opcode and the opcodes.c file is filled with an array of strings where
92750** each string is the symbolic name for the corresponding opcode. If the
92751** case statement is followed by a comment of the form "/# same as ... #/"
92752** that comment is used to determine the particular value of the opcode.
92753**
92754** Other keywords in the comment that follows each case are used to
92755** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
92756** Keywords include: in1, in2, in3, out2, out3. See
92757** the mkopcodeh.awk script for additional information.
92758**
92759** Documentation about VDBE opcodes is generated by scanning this file
92760** for lines of that contain "Opcode:". That line and all subsequent
92761** comment lines are used in the generation of the opcode.html documentation
92762** file.
92763**
92764** SUMMARY:
92765**
92766** Formatting is important to scripts that scan this file.
92767** Do not deviate from the formatting style currently in use.
92768**
92769*****************************************************************************/
92770
92771/* Opcode: Goto * P2 * * *
92772**
92773** An unconditional jump to address P2.
92774** The next instruction executed will be
92775** the one at index P2 from the beginning of
92776** the program.
92777**
92778** The P1 parameter is not actually used by this opcode. However, it
92779** is sometimes set to 1 instead of 0 as a hint to the command-line shell
92780** that this Goto is the bottom of a loop and that the lines from P2 down
92781** to the current line should be indented for EXPLAIN output.
92782*/
92783case OP_Goto: { /* jump */
92784
92785#ifdef SQLITE_DEBUG
92786 /* In debugging mode, when the p5 flags is set on an OP_Goto, that
92787 ** means we should really jump back to the preceding OP_ReleaseReg
92788 ** instruction. */
92789 if( pOp->p5 ){
92790 assert( pOp->p2 < (int)(pOp - aOp) );
92791 assert( pOp->p2 > 1 );
92792 pOp = &aOp[pOp->p2 - 2];
92793 assert( pOp[1].opcode==OP_ReleaseReg );
92794 goto check_for_interrupt;
92795 }
92796#endif
92797
92798jump_to_p2_and_check_for_interrupt:
92799 pOp = &aOp[pOp->p2 - 1];
92800
92801 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
92802 ** OP_VNext, or OP_SorterNext) all jump here upon
92803 ** completion. Check to see if sqlite3_interrupt() has been called
92804 ** or if the progress callback needs to be invoked.
92805 **
92806 ** This code uses unstructured "goto" statements and does not look clean.
92807 ** But that is not due to sloppy coding habits. The code is written this
92808 ** way for performance, to avoid having to run the interrupt and progress
92809 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
92810 ** faster according to "valgrind --tool=cachegrind" */
92811check_for_interrupt:
92812 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
92813#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
92814 /* Call the progress callback if it is configured and the required number
92815 ** of VDBE ops have been executed (either since this invocation of
92816 ** sqlite3VdbeExec() or since last time the progress callback was called).
92817 ** If the progress callback returns non-zero, exit the virtual machine with
92818 ** a return code SQLITE_ABORT.
92819 */
92820 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
92821 assert( db->nProgressOps!=0 );
92822 nProgressLimit += db->nProgressOps;
92823 if( db->xProgress(db->pProgressArg) ){
92824 nProgressLimit = LARGEST_UINT64;
92825 rc = SQLITE_INTERRUPT;
92826 goto abort_due_to_error;
92827 }
92828 }
92829#endif
92830
92831 break;
92832}
92833
92834/* Opcode: Gosub P1 P2 * * *
92835**
92836** Write the current address onto register P1
92837** and then jump to address P2.
92838*/
92839case OP_Gosub: { /* jump */
92840 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
92841 pIn1 = &aMem[pOp->p1];
92842 assert( VdbeMemDynamic(pIn1)==0 );
92843 memAboutToChange(p, pIn1);
92844 pIn1->flags = MEM_Int;
92845 pIn1->u.i = (int)(pOp-aOp);
92846 REGISTER_TRACE(pOp->p1, pIn1);
92847 goto jump_to_p2_and_check_for_interrupt;
92848}
92849
92850/* Opcode: Return P1 P2 P3 * *
92851**
92852** Jump to the address stored in register P1. If P1 is a return address
92853** register, then this accomplishes a return from a subroutine.
92854**
92855** If P3 is 1, then the jump is only taken if register P1 holds an integer
92856** values, otherwise execution falls through to the next opcode, and the
92857** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
92858** integer or else an assert() is raised. P3 should be set to 1 when
92859** this opcode is used in combination with OP_BeginSubrtn, and set to 0
92860** otherwise.
92861**
92862** The value in register P1 is unchanged by this opcode.
92863**
92864** P2 is not used by the byte-code engine. However, if P2 is positive
92865** and also less than the current address, then the "EXPLAIN" output
92866** formatter in the CLI will indent all opcodes from the P2 opcode up
92867** to be not including the current Return. P2 should be the first opcode
92868** in the subroutine from which this opcode is returning. Thus the P2
92869** value is a byte-code indentation hint. See tag-20220407a in
92870** wherecode.c and shell.c.
92871*/
92872case OP_Return: { /* in1 */
92873 pIn1 = &aMem[pOp->p1];
92874 if( pIn1->flags & MEM_Int ){
92875 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
92876 pOp = &aOp[pIn1->u.i];
92877 }else if( ALWAYS(pOp->p3) ){
92878 VdbeBranchTaken(0, 2);
92879 }
92880 break;
92881}
92882
92883/* Opcode: InitCoroutine P1 P2 P3 * *
92884**
92885** Set up register P1 so that it will Yield to the coroutine
92886** located at address P3.
92887**
92888** If P2!=0 then the coroutine implementation immediately follows
92889** this opcode. So jump over the coroutine implementation to
92890** address P2.
92891**
92892** See also: EndCoroutine
92893*/
92894case OP_InitCoroutine: { /* jump */
92895 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
92896 assert( pOp->p2>=0 && pOp->p2<p->nOp );
92897 assert( pOp->p3>=0 && pOp->p3<p->nOp );
92898 pOut = &aMem[pOp->p1];
92899 assert( !VdbeMemDynamic(pOut) );
92900 pOut->u.i = pOp->p3 - 1;
92901 pOut->flags = MEM_Int;
92902 if( pOp->p2==0 ) break;
92903
92904 /* Most jump operations do a goto to this spot in order to update
92905 ** the pOp pointer. */
92906jump_to_p2:
92907 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
92908 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
92909 pOp = &aOp[pOp->p2 - 1];
92910 break;
92911}
92912
92913/* Opcode: EndCoroutine P1 * * * *
92914**
92915** The instruction at the address in register P1 is a Yield.
92916** Jump to the P2 parameter of that Yield.
92917** After the jump, register P1 becomes undefined.
92918**
92919** See also: InitCoroutine
92920*/
92921case OP_EndCoroutine: { /* in1 */
92922 VdbeOp *pCaller;
92923 pIn1 = &aMem[pOp->p1];
92924 assert( pIn1->flags==MEM_Int );
92925 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
92926 pCaller = &aOp[pIn1->u.i];
92927 assert( pCaller->opcode==OP_Yield );
92928 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
92929 pOp = &aOp[pCaller->p2 - 1];
92930 pIn1->flags = MEM_Undefined;
92931 break;
92932}
92933
92934/* Opcode: Yield P1 P2 * * *
92935**
92936** Swap the program counter with the value in register P1. This
92937** has the effect of yielding to a coroutine.
92938**
92939** If the coroutine that is launched by this instruction ends with
92940** Yield or Return then continue to the next instruction. But if
92941** the coroutine launched by this instruction ends with
92942** EndCoroutine, then jump to P2 rather than continuing with the
92943** next instruction.
92944**
92945** See also: InitCoroutine
92946*/
92947case OP_Yield: { /* in1, jump */
92948 int pcDest;
92949 pIn1 = &aMem[pOp->p1];
92950 assert( VdbeMemDynamic(pIn1)==0 );
92951 pIn1->flags = MEM_Int;
92952 pcDest = (int)pIn1->u.i;
92953 pIn1->u.i = (int)(pOp - aOp);
92954 REGISTER_TRACE(pOp->p1, pIn1);
92955 pOp = &aOp[pcDest];
92956 break;
92957}
92958
92959/* Opcode: HaltIfNull P1 P2 P3 P4 P5
92960** Synopsis: if r[P3]=null halt
92961**
92962** Check the value in register P3. If it is NULL then Halt using
92963** parameter P1, P2, and P4 as if this were a Halt instruction. If the
92964** value in register P3 is not NULL, then this routine is a no-op.
92965** The P5 parameter should be 1.
92966*/
92967case OP_HaltIfNull: { /* in3 */
92968 pIn3 = &aMem[pOp->p3];
92969#ifdef SQLITE_DEBUG
92970 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
92971#endif
92972 if( (pIn3->flags & MEM_Null)==0 ) break;
92973 /* Fall through into OP_Halt */
92974 /* no break */ deliberate_fall_through
92975}
92976
92977/* Opcode: Halt P1 P2 * P4 P5
92978**
92979** Exit immediately. All open cursors, etc are closed
92980** automatically.
92981**
92982** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
92983** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
92984** For errors, it can be some other value. If P1!=0 then P2 will determine
92985** whether or not to rollback the current transaction. Do not rollback
92986** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
92987** then back out all changes that have occurred during this execution of the
92988** VDBE, but do not rollback the transaction.
92989**
92990** If P4 is not null then it is an error message string.
92991**
92992** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
92993**
92994** 0: (no change)
92995** 1: NOT NULL constraint failed: P4
92996** 2: UNIQUE constraint failed: P4
92997** 3: CHECK constraint failed: P4
92998** 4: FOREIGN KEY constraint failed: P4
92999**
93000** If P5 is not zero and P4 is NULL, then everything after the ":" is
93001** omitted.
93002**
93003** There is an implied "Halt 0 0 0" instruction inserted at the very end of
93004** every program. So a jump past the last instruction of the program
93005** is the same as executing Halt.
93006*/
93007case OP_Halt: {
93008 VdbeFrame *pFrame;
93009 int pcx;
93010
93011#ifdef SQLITE_DEBUG
93012 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
93013#endif
93014
93015 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates
93016 ** something is wrong with the code generator. Raise an assertion in order
93017 ** to bring this to the attention of fuzzers and other testing tools. */
93018 assert( pOp->p1!=SQLITE_INTERNAL );
93019
93020 if( p->pFrame && pOp->p1==SQLITE_OK ){
93021 /* Halt the sub-program. Return control to the parent frame. */
93022 pFrame = p->pFrame;
93023 p->pFrame = pFrame->pParent;
93024 p->nFrame--;
93025 sqlite3VdbeSetChanges(db, nChange: p->nChange);
93026 pcx = sqlite3VdbeFrameRestore(pFrame);
93027 if( pOp->p2==OE_Ignore ){
93028 /* Instruction pcx is the OP_Program that invoked the sub-program
93029 ** currently being halted. If the p2 instruction of this OP_Halt
93030 ** instruction is set to OE_Ignore, then the sub-program is throwing
93031 ** an IGNORE exception. In this case jump to the address specified
93032 ** as the p2 of the calling OP_Program. */
93033 pcx = p->aOp[pcx].p2-1;
93034 }
93035 aOp = p->aOp;
93036 aMem = p->aMem;
93037 pOp = &aOp[pcx];
93038 break;
93039 }
93040 p->rc = pOp->p1;
93041 p->errorAction = (u8)pOp->p2;
93042 assert( pOp->p5<=4 );
93043 if( p->rc ){
93044 if( pOp->p5 ){
93045 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
93046 "FOREIGN KEY" };
93047 testcase( pOp->p5==1 );
93048 testcase( pOp->p5==2 );
93049 testcase( pOp->p5==3 );
93050 testcase( pOp->p5==4 );
93051 sqlite3VdbeError(p, zFormat: "%s constraint failed", azType[pOp->p5-1]);
93052 if( pOp->p4.z ){
93053 p->zErrMsg = sqlite3MPrintf(db, zFormat: "%z: %s", p->zErrMsg, pOp->p4.z);
93054 }
93055 }else{
93056 sqlite3VdbeError(p, zFormat: "%s", pOp->p4.z);
93057 }
93058 pcx = (int)(pOp - aOp);
93059 sqlite3_log(iErrCode: pOp->p1, zFormat: "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
93060 }
93061 rc = sqlite3VdbeHalt(p);
93062 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
93063 if( rc==SQLITE_BUSY ){
93064 p->rc = SQLITE_BUSY;
93065 }else{
93066 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
93067 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
93068 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
93069 }
93070 goto vdbe_return;
93071}
93072
93073/* Opcode: Integer P1 P2 * * *
93074** Synopsis: r[P2]=P1
93075**
93076** The 32-bit integer value P1 is written into register P2.
93077*/
93078case OP_Integer: { /* out2 */
93079 pOut = out2Prerelease(p, pOp);
93080 pOut->u.i = pOp->p1;
93081 break;
93082}
93083
93084/* Opcode: Int64 * P2 * P4 *
93085** Synopsis: r[P2]=P4
93086**
93087** P4 is a pointer to a 64-bit integer value.
93088** Write that value into register P2.
93089*/
93090case OP_Int64: { /* out2 */
93091 pOut = out2Prerelease(p, pOp);
93092 assert( pOp->p4.pI64!=0 );
93093 pOut->u.i = *pOp->p4.pI64;
93094 break;
93095}
93096
93097#ifndef SQLITE_OMIT_FLOATING_POINT
93098/* Opcode: Real * P2 * P4 *
93099** Synopsis: r[P2]=P4
93100**
93101** P4 is a pointer to a 64-bit floating point value.
93102** Write that value into register P2.
93103*/
93104case OP_Real: { /* same as TK_FLOAT, out2 */
93105 pOut = out2Prerelease(p, pOp);
93106 pOut->flags = MEM_Real;
93107 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
93108 pOut->u.r = *pOp->p4.pReal;
93109 break;
93110}
93111#endif
93112
93113/* Opcode: String8 * P2 * P4 *
93114** Synopsis: r[P2]='P4'
93115**
93116** P4 points to a nul terminated UTF-8 string. This opcode is transformed
93117** into a String opcode before it is executed for the first time. During
93118** this transformation, the length of string P4 is computed and stored
93119** as the P1 parameter.
93120*/
93121case OP_String8: { /* same as TK_STRING, out2 */
93122 assert( pOp->p4.z!=0 );
93123 pOut = out2Prerelease(p, pOp);
93124 pOp->p1 = sqlite3Strlen30(z: pOp->p4.z);
93125
93126#ifndef SQLITE_OMIT_UTF16
93127 if( encoding!=SQLITE_UTF8 ){
93128 rc = sqlite3VdbeMemSetStr(pMem: pOut, z: pOp->p4.z, n: -1, SQLITE_UTF8, SQLITE_STATIC);
93129 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
93130 if( rc ) goto too_big;
93131 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: encoding) ) goto no_mem;
93132 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
93133 assert( VdbeMemDynamic(pOut)==0 );
93134 pOut->szMalloc = 0;
93135 pOut->flags |= MEM_Static;
93136 if( pOp->p4type==P4_DYNAMIC ){
93137 sqlite3DbFree(db, p: pOp->p4.z);
93138 }
93139 pOp->p4type = P4_DYNAMIC;
93140 pOp->p4.z = pOut->z;
93141 pOp->p1 = pOut->n;
93142 }
93143#endif
93144 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
93145 goto too_big;
93146 }
93147 pOp->opcode = OP_String;
93148 assert( rc==SQLITE_OK );
93149 /* Fall through to the next case, OP_String */
93150 /* no break */ deliberate_fall_through
93151}
93152
93153/* Opcode: String P1 P2 P3 P4 P5
93154** Synopsis: r[P2]='P4' (len=P1)
93155**
93156** The string value P4 of length P1 (bytes) is stored in register P2.
93157**
93158** If P3 is not zero and the content of register P3 is equal to P5, then
93159** the datatype of the register P2 is converted to BLOB. The content is
93160** the same sequence of bytes, it is merely interpreted as a BLOB instead
93161** of a string, as if it had been CAST. In other words:
93162**
93163** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
93164*/
93165case OP_String: { /* out2 */
93166 assert( pOp->p4.z!=0 );
93167 pOut = out2Prerelease(p, pOp);
93168 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
93169 pOut->z = pOp->p4.z;
93170 pOut->n = pOp->p1;
93171 pOut->enc = encoding;
93172 UPDATE_MAX_BLOBSIZE(pOut);
93173#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
93174 if( pOp->p3>0 ){
93175 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93176 pIn3 = &aMem[pOp->p3];
93177 assert( pIn3->flags & MEM_Int );
93178 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
93179 }
93180#endif
93181 break;
93182}
93183
93184/* Opcode: BeginSubrtn * P2 * * *
93185** Synopsis: r[P2]=NULL
93186**
93187** Mark the beginning of a subroutine that can be entered in-line
93188** or that can be called using OP_Gosub. The subroutine should
93189** be terminated by an OP_Return instruction that has a P1 operand that
93190** is the same as the P2 operand to this opcode and that has P3 set to 1.
93191** If the subroutine is entered in-line, then the OP_Return will simply
93192** fall through. But if the subroutine is entered using OP_Gosub, then
93193** the OP_Return will jump back to the first instruction after the OP_Gosub.
93194**
93195** This routine works by loading a NULL into the P2 register. When the
93196** return address register contains a NULL, the OP_Return instruction is
93197** a no-op that simply falls through to the next instruction (assuming that
93198** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
93199** entered in-line, then the OP_Return will cause in-line execution to
93200** continue. But if the subroutine is entered via OP_Gosub, then the
93201** OP_Return will cause a return to the address following the OP_Gosub.
93202**
93203** This opcode is identical to OP_Null. It has a different name
93204** only to make the byte code easier to read and verify.
93205*/
93206/* Opcode: Null P1 P2 P3 * *
93207** Synopsis: r[P2..P3]=NULL
93208**
93209** Write a NULL into registers P2. If P3 greater than P2, then also write
93210** NULL into register P3 and every register in between P2 and P3. If P3
93211** is less than P2 (typically P3 is zero) then only register P2 is
93212** set to NULL.
93213**
93214** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
93215** NULL values will not compare equal even if SQLITE_NULLEQ is set on
93216** OP_Ne or OP_Eq.
93217*/
93218case OP_BeginSubrtn:
93219case OP_Null: { /* out2 */
93220 int cnt;
93221 u16 nullFlag;
93222 pOut = out2Prerelease(p, pOp);
93223 cnt = pOp->p3-pOp->p2;
93224 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93225 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
93226 pOut->n = 0;
93227#ifdef SQLITE_DEBUG
93228 pOut->uTemp = 0;
93229#endif
93230 while( cnt>0 ){
93231 pOut++;
93232 memAboutToChange(p, pOut);
93233 sqlite3VdbeMemSetNull(pMem: pOut);
93234 pOut->flags = nullFlag;
93235 pOut->n = 0;
93236 cnt--;
93237 }
93238 break;
93239}
93240
93241/* Opcode: SoftNull P1 * * * *
93242** Synopsis: r[P1]=NULL
93243**
93244** Set register P1 to have the value NULL as seen by the OP_MakeRecord
93245** instruction, but do not free any string or blob memory associated with
93246** the register, so that if the value was a string or blob that was
93247** previously copied using OP_SCopy, the copies will continue to be valid.
93248*/
93249case OP_SoftNull: {
93250 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
93251 pOut = &aMem[pOp->p1];
93252 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
93253 break;
93254}
93255
93256/* Opcode: Blob P1 P2 * P4 *
93257** Synopsis: r[P2]=P4 (len=P1)
93258**
93259** P4 points to a blob of data P1 bytes long. Store this
93260** blob in register P2. If P4 is a NULL pointer, then construct
93261** a zero-filled blob that is P1 bytes long in P2.
93262*/
93263case OP_Blob: { /* out2 */
93264 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
93265 pOut = out2Prerelease(p, pOp);
93266 if( pOp->p4.z==0 ){
93267 sqlite3VdbeMemSetZeroBlob(pMem: pOut, n: pOp->p1);
93268 if( sqlite3VdbeMemExpandBlob(pMem: pOut) ) goto no_mem;
93269 }else{
93270 sqlite3VdbeMemSetStr(pMem: pOut, z: pOp->p4.z, n: pOp->p1, enc: 0, xDel: 0);
93271 }
93272 pOut->enc = encoding;
93273 UPDATE_MAX_BLOBSIZE(pOut);
93274 break;
93275}
93276
93277/* Opcode: Variable P1 P2 * P4 *
93278** Synopsis: r[P2]=parameter(P1,P4)
93279**
93280** Transfer the values of bound parameter P1 into register P2
93281**
93282** If the parameter is named, then its name appears in P4.
93283** The P4 value is used by sqlite3_bind_parameter_name().
93284*/
93285case OP_Variable: { /* out2 */
93286 Mem *pVar; /* Value being transferred */
93287
93288 assert( pOp->p1>0 && pOp->p1<=p->nVar );
93289 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
93290 pVar = &p->aVar[pOp->p1 - 1];
93291 if( sqlite3VdbeMemTooBig(p: pVar) ){
93292 goto too_big;
93293 }
93294 pOut = &aMem[pOp->p2];
93295 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pMem: pOut);
93296 memcpy(dest: pOut, src: pVar, MEMCELLSIZE);
93297 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
93298 pOut->flags |= MEM_Static|MEM_FromBind;
93299 UPDATE_MAX_BLOBSIZE(pOut);
93300 break;
93301}
93302
93303/* Opcode: Move P1 P2 P3 * *
93304** Synopsis: r[P2@P3]=r[P1@P3]
93305**
93306** Move the P3 values in register P1..P1+P3-1 over into
93307** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
93308** left holding a NULL. It is an error for register ranges
93309** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
93310** for P3 to be less than 1.
93311*/
93312case OP_Move: {
93313 int n; /* Number of registers left to copy */
93314 int p1; /* Register to copy from */
93315 int p2; /* Register to copy to */
93316
93317 n = pOp->p3;
93318 p1 = pOp->p1;
93319 p2 = pOp->p2;
93320 assert( n>0 && p1>0 && p2>0 );
93321 assert( p1+n<=p2 || p2+n<=p1 );
93322
93323 pIn1 = &aMem[p1];
93324 pOut = &aMem[p2];
93325 do{
93326 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
93327 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
93328 assert( memIsValid(pIn1) );
93329 memAboutToChange(p, pOut);
93330 sqlite3VdbeMemMove(pTo: pOut, pFrom: pIn1);
93331#ifdef SQLITE_DEBUG
93332 pIn1->pScopyFrom = 0;
93333 { int i;
93334 for(i=1; i<p->nMem; i++){
93335 if( aMem[i].pScopyFrom==pIn1 ){
93336 aMem[i].pScopyFrom = pOut;
93337 }
93338 }
93339 }
93340#endif
93341 Deephemeralize(pOut);
93342 REGISTER_TRACE(p2++, pOut);
93343 pIn1++;
93344 pOut++;
93345 }while( --n );
93346 break;
93347}
93348
93349/* Opcode: Copy P1 P2 P3 * P5
93350** Synopsis: r[P2@P3+1]=r[P1@P3+1]
93351**
93352** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
93353**
93354** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
93355** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
93356** be merged. The 0x0001 bit is used by the query planner and does not
93357** come into play during query execution.
93358**
93359** This instruction makes a deep copy of the value. A duplicate
93360** is made of any string or blob constant. See also OP_SCopy.
93361*/
93362case OP_Copy: {
93363 int n;
93364
93365 n = pOp->p3;
93366 pIn1 = &aMem[pOp->p1];
93367 pOut = &aMem[pOp->p2];
93368 assert( pOut!=pIn1 );
93369 while( 1 ){
93370 memAboutToChange(p, pOut);
93371 sqlite3VdbeMemShallowCopy(pTo: pOut, pFrom: pIn1, MEM_Ephem);
93372 Deephemeralize(pOut);
93373 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
93374 pOut->flags &= ~MEM_Subtype;
93375 }
93376#ifdef SQLITE_DEBUG
93377 pOut->pScopyFrom = 0;
93378#endif
93379 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
93380 if( (n--)==0 ) break;
93381 pOut++;
93382 pIn1++;
93383 }
93384 break;
93385}
93386
93387/* Opcode: SCopy P1 P2 * * *
93388** Synopsis: r[P2]=r[P1]
93389**
93390** Make a shallow copy of register P1 into register P2.
93391**
93392** This instruction makes a shallow copy of the value. If the value
93393** is a string or blob, then the copy is only a pointer to the
93394** original and hence if the original changes so will the copy.
93395** Worse, if the original is deallocated, the copy becomes invalid.
93396** Thus the program must guarantee that the original will not change
93397** during the lifetime of the copy. Use OP_Copy to make a complete
93398** copy.
93399*/
93400case OP_SCopy: { /* out2 */
93401 pIn1 = &aMem[pOp->p1];
93402 pOut = &aMem[pOp->p2];
93403 assert( pOut!=pIn1 );
93404 sqlite3VdbeMemShallowCopy(pTo: pOut, pFrom: pIn1, MEM_Ephem);
93405#ifdef SQLITE_DEBUG
93406 pOut->pScopyFrom = pIn1;
93407 pOut->mScopyFlags = pIn1->flags;
93408#endif
93409 break;
93410}
93411
93412/* Opcode: IntCopy P1 P2 * * *
93413** Synopsis: r[P2]=r[P1]
93414**
93415** Transfer the integer value held in register P1 into register P2.
93416**
93417** This is an optimized version of SCopy that works only for integer
93418** values.
93419*/
93420case OP_IntCopy: { /* out2 */
93421 pIn1 = &aMem[pOp->p1];
93422 assert( (pIn1->flags & MEM_Int)!=0 );
93423 pOut = &aMem[pOp->p2];
93424 sqlite3VdbeMemSetInt64(pMem: pOut, val: pIn1->u.i);
93425 break;
93426}
93427
93428/* Opcode: FkCheck * * * * *
93429**
93430** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
93431** foreign key constraint violations. If there are no foreign key
93432** constraint violations, this is a no-op.
93433**
93434** FK constraint violations are also checked when the prepared statement
93435** exits. This opcode is used to raise foreign key constraint errors prior
93436** to returning results such as a row change count or the result of a
93437** RETURNING clause.
93438*/
93439case OP_FkCheck: {
93440 if( (rc = sqlite3VdbeCheckFk(p,deferred: 0))!=SQLITE_OK ){
93441 goto abort_due_to_error;
93442 }
93443 break;
93444}
93445
93446/* Opcode: ResultRow P1 P2 * * *
93447** Synopsis: output=r[P1@P2]
93448**
93449** The registers P1 through P1+P2-1 contain a single row of
93450** results. This opcode causes the sqlite3_step() call to terminate
93451** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
93452** structure to provide access to the r(P1)..r(P1+P2-1) values as
93453** the result row.
93454*/
93455case OP_ResultRow: {
93456 assert( p->nResColumn==pOp->p2 );
93457 assert( pOp->p1>0 || CORRUPT_DB );
93458 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
93459
93460 p->cacheCtr = (p->cacheCtr + 2)|1;
93461 p->pResultRow = &aMem[pOp->p1];
93462#ifdef SQLITE_DEBUG
93463 {
93464 Mem *pMem = p->pResultRow;
93465 int i;
93466 for(i=0; i<pOp->p2; i++){
93467 assert( memIsValid(&pMem[i]) );
93468 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
93469 /* The registers in the result will not be used again when the
93470 ** prepared statement restarts. This is because sqlite3_column()
93471 ** APIs might have caused type conversions of made other changes to
93472 ** the register values. Therefore, we can go ahead and break any
93473 ** OP_SCopy dependencies. */
93474 pMem[i].pScopyFrom = 0;
93475 }
93476 }
93477#endif
93478 if( db->mallocFailed ) goto no_mem;
93479 if( db->mTrace & SQLITE_TRACE_ROW ){
93480 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
93481 }
93482 p->pc = (int)(pOp - aOp) + 1;
93483 rc = SQLITE_ROW;
93484 goto vdbe_return;
93485}
93486
93487/* Opcode: Concat P1 P2 P3 * *
93488** Synopsis: r[P3]=r[P2]+r[P1]
93489**
93490** Add the text in register P1 onto the end of the text in
93491** register P2 and store the result in register P3.
93492** If either the P1 or P2 text are NULL then store NULL in P3.
93493**
93494** P3 = P2 || P1
93495**
93496** It is illegal for P1 and P3 to be the same register. Sometimes,
93497** if P3 is the same register as P2, the implementation is able
93498** to avoid a memcpy().
93499*/
93500case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
93501 i64 nByte; /* Total size of the output string or blob */
93502 u16 flags1; /* Initial flags for P1 */
93503 u16 flags2; /* Initial flags for P2 */
93504
93505 pIn1 = &aMem[pOp->p1];
93506 pIn2 = &aMem[pOp->p2];
93507 pOut = &aMem[pOp->p3];
93508 testcase( pOut==pIn2 );
93509 assert( pIn1!=pOut );
93510 flags1 = pIn1->flags;
93511 testcase( flags1 & MEM_Null );
93512 testcase( pIn2->flags & MEM_Null );
93513 if( (flags1 | pIn2->flags) & MEM_Null ){
93514 sqlite3VdbeMemSetNull(pMem: pOut);
93515 break;
93516 }
93517 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
93518 if( sqlite3VdbeMemStringify(pMem: pIn1,enc: encoding,bForce: 0) ) goto no_mem;
93519 flags1 = pIn1->flags & ~MEM_Str;
93520 }else if( (flags1 & MEM_Zero)!=0 ){
93521 if( sqlite3VdbeMemExpandBlob(pMem: pIn1) ) goto no_mem;
93522 flags1 = pIn1->flags & ~MEM_Str;
93523 }
93524 flags2 = pIn2->flags;
93525 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
93526 if( sqlite3VdbeMemStringify(pMem: pIn2,enc: encoding,bForce: 0) ) goto no_mem;
93527 flags2 = pIn2->flags & ~MEM_Str;
93528 }else if( (flags2 & MEM_Zero)!=0 ){
93529 if( sqlite3VdbeMemExpandBlob(pMem: pIn2) ) goto no_mem;
93530 flags2 = pIn2->flags & ~MEM_Str;
93531 }
93532 nByte = pIn1->n + pIn2->n;
93533 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
93534 goto too_big;
93535 }
93536 if( sqlite3VdbeMemGrow(pMem: pOut, n: (int)nByte+2, bPreserve: pOut==pIn2) ){
93537 goto no_mem;
93538 }
93539 MemSetTypeFlag(pOut, MEM_Str);
93540 if( pOut!=pIn2 ){
93541 memcpy(dest: pOut->z, src: pIn2->z, n: pIn2->n);
93542 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
93543 pIn2->flags = flags2;
93544 }
93545 memcpy(dest: &pOut->z[pIn2->n], src: pIn1->z, n: pIn1->n);
93546 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
93547 pIn1->flags = flags1;
93548 if( encoding>SQLITE_UTF8 ) nByte &= ~1;
93549 pOut->z[nByte]=0;
93550 pOut->z[nByte+1] = 0;
93551 pOut->flags |= MEM_Term;
93552 pOut->n = (int)nByte;
93553 pOut->enc = encoding;
93554 UPDATE_MAX_BLOBSIZE(pOut);
93555 break;
93556}
93557
93558/* Opcode: Add P1 P2 P3 * *
93559** Synopsis: r[P3]=r[P1]+r[P2]
93560**
93561** Add the value in register P1 to the value in register P2
93562** and store the result in register P3.
93563** If either input is NULL, the result is NULL.
93564*/
93565/* Opcode: Multiply P1 P2 P3 * *
93566** Synopsis: r[P3]=r[P1]*r[P2]
93567**
93568**
93569** Multiply the value in register P1 by the value in register P2
93570** and store the result in register P3.
93571** If either input is NULL, the result is NULL.
93572*/
93573/* Opcode: Subtract P1 P2 P3 * *
93574** Synopsis: r[P3]=r[P2]-r[P1]
93575**
93576** Subtract the value in register P1 from the value in register P2
93577** and store the result in register P3.
93578** If either input is NULL, the result is NULL.
93579*/
93580/* Opcode: Divide P1 P2 P3 * *
93581** Synopsis: r[P3]=r[P2]/r[P1]
93582**
93583** Divide the value in register P1 by the value in register P2
93584** and store the result in register P3 (P3=P2/P1). If the value in
93585** register P1 is zero, then the result is NULL. If either input is
93586** NULL, the result is NULL.
93587*/
93588/* Opcode: Remainder P1 P2 P3 * *
93589** Synopsis: r[P3]=r[P2]%r[P1]
93590**
93591** Compute the remainder after integer register P2 is divided by
93592** register P1 and store the result in register P3.
93593** If the value in register P1 is zero the result is NULL.
93594** If either operand is NULL, the result is NULL.
93595*/
93596case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
93597case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
93598case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
93599case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
93600case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
93601 u16 type1; /* Numeric type of left operand */
93602 u16 type2; /* Numeric type of right operand */
93603 i64 iA; /* Integer value of left operand */
93604 i64 iB; /* Integer value of right operand */
93605 double rA; /* Real value of left operand */
93606 double rB; /* Real value of right operand */
93607
93608 pIn1 = &aMem[pOp->p1];
93609 type1 = pIn1->flags;
93610 pIn2 = &aMem[pOp->p2];
93611 type2 = pIn2->flags;
93612 pOut = &aMem[pOp->p3];
93613 if( (type1 & type2 & MEM_Int)!=0 ){
93614int_math:
93615 iA = pIn1->u.i;
93616 iB = pIn2->u.i;
93617 switch( pOp->opcode ){
93618 case OP_Add: if( sqlite3AddInt64(pA: &iB,iB: iA) ) goto fp_math; break;
93619 case OP_Subtract: if( sqlite3SubInt64(pA: &iB,iB: iA) ) goto fp_math; break;
93620 case OP_Multiply: if( sqlite3MulInt64(pA: &iB,iB: iA) ) goto fp_math; break;
93621 case OP_Divide: {
93622 if( iA==0 ) goto arithmetic_result_is_null;
93623 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
93624 iB /= iA;
93625 break;
93626 }
93627 default: {
93628 if( iA==0 ) goto arithmetic_result_is_null;
93629 if( iA==-1 ) iA = 1;
93630 iB %= iA;
93631 break;
93632 }
93633 }
93634 pOut->u.i = iB;
93635 MemSetTypeFlag(pOut, MEM_Int);
93636 }else if( ((type1 | type2) & MEM_Null)!=0 ){
93637 goto arithmetic_result_is_null;
93638 }else{
93639 type1 = numericType(pMem: pIn1);
93640 type2 = numericType(pMem: pIn2);
93641 if( (type1 & type2 & MEM_Int)!=0 ) goto int_math;
93642fp_math:
93643 rA = sqlite3VdbeRealValue(pMem: pIn1);
93644 rB = sqlite3VdbeRealValue(pMem: pIn2);
93645 switch( pOp->opcode ){
93646 case OP_Add: rB += rA; break;
93647 case OP_Subtract: rB -= rA; break;
93648 case OP_Multiply: rB *= rA; break;
93649 case OP_Divide: {
93650 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
93651 if( rA==(double)0 ) goto arithmetic_result_is_null;
93652 rB /= rA;
93653 break;
93654 }
93655 default: {
93656 iA = sqlite3VdbeIntValue(pMem: pIn1);
93657 iB = sqlite3VdbeIntValue(pMem: pIn2);
93658 if( iA==0 ) goto arithmetic_result_is_null;
93659 if( iA==-1 ) iA = 1;
93660 rB = (double)(iB % iA);
93661 break;
93662 }
93663 }
93664#ifdef SQLITE_OMIT_FLOATING_POINT
93665 pOut->u.i = rB;
93666 MemSetTypeFlag(pOut, MEM_Int);
93667#else
93668 if( sqlite3IsNaN(x: rB) ){
93669 goto arithmetic_result_is_null;
93670 }
93671 pOut->u.r = rB;
93672 MemSetTypeFlag(pOut, MEM_Real);
93673#endif
93674 }
93675 break;
93676
93677arithmetic_result_is_null:
93678 sqlite3VdbeMemSetNull(pMem: pOut);
93679 break;
93680}
93681
93682/* Opcode: CollSeq P1 * * P4
93683**
93684** P4 is a pointer to a CollSeq object. If the next call to a user function
93685** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
93686** be returned. This is used by the built-in min(), max() and nullif()
93687** functions.
93688**
93689** If P1 is not zero, then it is a register that a subsequent min() or
93690** max() aggregate will set to 1 if the current row is not the minimum or
93691** maximum. The P1 register is initialized to 0 by this instruction.
93692**
93693** The interface used by the implementation of the aforementioned functions
93694** to retrieve the collation sequence set by this opcode is not available
93695** publicly. Only built-in functions have access to this feature.
93696*/
93697case OP_CollSeq: {
93698 assert( pOp->p4type==P4_COLLSEQ );
93699 if( pOp->p1 ){
93700 sqlite3VdbeMemSetInt64(pMem: &aMem[pOp->p1], val: 0);
93701 }
93702 break;
93703}
93704
93705/* Opcode: BitAnd P1 P2 P3 * *
93706** Synopsis: r[P3]=r[P1]&r[P2]
93707**
93708** Take the bit-wise AND of the values in register P1 and P2 and
93709** store the result in register P3.
93710** If either input is NULL, the result is NULL.
93711*/
93712/* Opcode: BitOr P1 P2 P3 * *
93713** Synopsis: r[P3]=r[P1]|r[P2]
93714**
93715** Take the bit-wise OR of the values in register P1 and P2 and
93716** store the result in register P3.
93717** If either input is NULL, the result is NULL.
93718*/
93719/* Opcode: ShiftLeft P1 P2 P3 * *
93720** Synopsis: r[P3]=r[P2]<<r[P1]
93721**
93722** Shift the integer value in register P2 to the left by the
93723** number of bits specified by the integer in register P1.
93724** Store the result in register P3.
93725** If either input is NULL, the result is NULL.
93726*/
93727/* Opcode: ShiftRight P1 P2 P3 * *
93728** Synopsis: r[P3]=r[P2]>>r[P1]
93729**
93730** Shift the integer value in register P2 to the right by the
93731** number of bits specified by the integer in register P1.
93732** Store the result in register P3.
93733** If either input is NULL, the result is NULL.
93734*/
93735case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
93736case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
93737case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
93738case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
93739 i64 iA;
93740 u64 uA;
93741 i64 iB;
93742 u8 op;
93743
93744 pIn1 = &aMem[pOp->p1];
93745 pIn2 = &aMem[pOp->p2];
93746 pOut = &aMem[pOp->p3];
93747 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
93748 sqlite3VdbeMemSetNull(pMem: pOut);
93749 break;
93750 }
93751 iA = sqlite3VdbeIntValue(pMem: pIn2);
93752 iB = sqlite3VdbeIntValue(pMem: pIn1);
93753 op = pOp->opcode;
93754 if( op==OP_BitAnd ){
93755 iA &= iB;
93756 }else if( op==OP_BitOr ){
93757 iA |= iB;
93758 }else if( iB!=0 ){
93759 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
93760
93761 /* If shifting by a negative amount, shift in the other direction */
93762 if( iB<0 ){
93763 assert( OP_ShiftRight==OP_ShiftLeft+1 );
93764 op = 2*OP_ShiftLeft + 1 - op;
93765 iB = iB>(-64) ? -iB : 64;
93766 }
93767
93768 if( iB>=64 ){
93769 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
93770 }else{
93771 memcpy(dest: &uA, src: &iA, n: sizeof(uA));
93772 if( op==OP_ShiftLeft ){
93773 uA <<= iB;
93774 }else{
93775 uA >>= iB;
93776 /* Sign-extend on a right shift of a negative number */
93777 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
93778 }
93779 memcpy(dest: &iA, src: &uA, n: sizeof(iA));
93780 }
93781 }
93782 pOut->u.i = iA;
93783 MemSetTypeFlag(pOut, MEM_Int);
93784 break;
93785}
93786
93787/* Opcode: AddImm P1 P2 * * *
93788** Synopsis: r[P1]=r[P1]+P2
93789**
93790** Add the constant P2 to the value in register P1.
93791** The result is always an integer.
93792**
93793** To force any register to be an integer, just add 0.
93794*/
93795case OP_AddImm: { /* in1 */
93796 pIn1 = &aMem[pOp->p1];
93797 memAboutToChange(p, pIn1);
93798 sqlite3VdbeMemIntegerify(pMem: pIn1);
93799 pIn1->u.i += pOp->p2;
93800 break;
93801}
93802
93803/* Opcode: MustBeInt P1 P2 * * *
93804**
93805** Force the value in register P1 to be an integer. If the value
93806** in P1 is not an integer and cannot be converted into an integer
93807** without data loss, then jump immediately to P2, or if P2==0
93808** raise an SQLITE_MISMATCH exception.
93809*/
93810case OP_MustBeInt: { /* jump, in1 */
93811 pIn1 = &aMem[pOp->p1];
93812 if( (pIn1->flags & MEM_Int)==0 ){
93813 applyAffinity(pRec: pIn1, SQLITE_AFF_NUMERIC, enc: encoding);
93814 if( (pIn1->flags & MEM_Int)==0 ){
93815 VdbeBranchTaken(1, 2);
93816 if( pOp->p2==0 ){
93817 rc = SQLITE_MISMATCH;
93818 goto abort_due_to_error;
93819 }else{
93820 goto jump_to_p2;
93821 }
93822 }
93823 }
93824 VdbeBranchTaken(0, 2);
93825 MemSetTypeFlag(pIn1, MEM_Int);
93826 break;
93827}
93828
93829#ifndef SQLITE_OMIT_FLOATING_POINT
93830/* Opcode: RealAffinity P1 * * * *
93831**
93832** If register P1 holds an integer convert it to a real value.
93833**
93834** This opcode is used when extracting information from a column that
93835** has REAL affinity. Such column values may still be stored as
93836** integers, for space efficiency, but after extraction we want them
93837** to have only a real value.
93838*/
93839case OP_RealAffinity: { /* in1 */
93840 pIn1 = &aMem[pOp->p1];
93841 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
93842 testcase( pIn1->flags & MEM_Int );
93843 testcase( pIn1->flags & MEM_IntReal );
93844 sqlite3VdbeMemRealify(pMem: pIn1);
93845 REGISTER_TRACE(pOp->p1, pIn1);
93846 }
93847 break;
93848}
93849#endif
93850
93851#ifndef SQLITE_OMIT_CAST
93852/* Opcode: Cast P1 P2 * * *
93853** Synopsis: affinity(r[P1])
93854**
93855** Force the value in register P1 to be the type defined by P2.
93856**
93857** <ul>
93858** <li> P2=='A' &rarr; BLOB
93859** <li> P2=='B' &rarr; TEXT
93860** <li> P2=='C' &rarr; NUMERIC
93861** <li> P2=='D' &rarr; INTEGER
93862** <li> P2=='E' &rarr; REAL
93863** </ul>
93864**
93865** A NULL value is not changed by this routine. It remains NULL.
93866*/
93867case OP_Cast: { /* in1 */
93868 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
93869 testcase( pOp->p2==SQLITE_AFF_TEXT );
93870 testcase( pOp->p2==SQLITE_AFF_BLOB );
93871 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
93872 testcase( pOp->p2==SQLITE_AFF_INTEGER );
93873 testcase( pOp->p2==SQLITE_AFF_REAL );
93874 pIn1 = &aMem[pOp->p1];
93875 memAboutToChange(p, pIn1);
93876 rc = ExpandBlob(pIn1);
93877 if( rc ) goto abort_due_to_error;
93878 rc = sqlite3VdbeMemCast(pMem: pIn1, aff: pOp->p2, encoding);
93879 if( rc ) goto abort_due_to_error;
93880 UPDATE_MAX_BLOBSIZE(pIn1);
93881 REGISTER_TRACE(pOp->p1, pIn1);
93882 break;
93883}
93884#endif /* SQLITE_OMIT_CAST */
93885
93886/* Opcode: Eq P1 P2 P3 P4 P5
93887** Synopsis: IF r[P3]==r[P1]
93888**
93889** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
93890** jump to address P2.
93891**
93892** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
93893** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
93894** to coerce both inputs according to this affinity before the
93895** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
93896** affinity is used. Note that the affinity conversions are stored
93897** back into the input registers P1 and P3. So this opcode can cause
93898** persistent changes to registers P1 and P3.
93899**
93900** Once any conversions have taken place, and neither value is NULL,
93901** the values are compared. If both values are blobs then memcmp() is
93902** used to determine the results of the comparison. If both values
93903** are text, then the appropriate collating function specified in
93904** P4 is used to do the comparison. If P4 is not specified then
93905** memcmp() is used to compare text string. If both values are
93906** numeric, then a numeric comparison is used. If the two values
93907** are of different types, then numbers are considered less than
93908** strings and strings are considered less than blobs.
93909**
93910** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
93911** true or false and is never NULL. If both operands are NULL then the result
93912** of comparison is true. If either operand is NULL then the result is false.
93913** If neither operand is NULL the result is the same as it would be if
93914** the SQLITE_NULLEQ flag were omitted from P5.
93915**
93916** This opcode saves the result of comparison for use by the new
93917** OP_Jump opcode.
93918*/
93919/* Opcode: Ne P1 P2 P3 P4 P5
93920** Synopsis: IF r[P3]!=r[P1]
93921**
93922** This works just like the Eq opcode except that the jump is taken if
93923** the operands in registers P1 and P3 are not equal. See the Eq opcode for
93924** additional information.
93925*/
93926/* Opcode: Lt P1 P2 P3 P4 P5
93927** Synopsis: IF r[P3]<r[P1]
93928**
93929** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
93930** jump to address P2.
93931**
93932** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
93933** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
93934** bit is clear then fall through if either operand is NULL.
93935**
93936** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
93937** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
93938** to coerce both inputs according to this affinity before the
93939** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
93940** affinity is used. Note that the affinity conversions are stored
93941** back into the input registers P1 and P3. So this opcode can cause
93942** persistent changes to registers P1 and P3.
93943**
93944** Once any conversions have taken place, and neither value is NULL,
93945** the values are compared. If both values are blobs then memcmp() is
93946** used to determine the results of the comparison. If both values
93947** are text, then the appropriate collating function specified in
93948** P4 is used to do the comparison. If P4 is not specified then
93949** memcmp() is used to compare text string. If both values are
93950** numeric, then a numeric comparison is used. If the two values
93951** are of different types, then numbers are considered less than
93952** strings and strings are considered less than blobs.
93953**
93954** This opcode saves the result of comparison for use by the new
93955** OP_Jump opcode.
93956*/
93957/* Opcode: Le P1 P2 P3 P4 P5
93958** Synopsis: IF r[P3]<=r[P1]
93959**
93960** This works just like the Lt opcode except that the jump is taken if
93961** the content of register P3 is less than or equal to the content of
93962** register P1. See the Lt opcode for additional information.
93963*/
93964/* Opcode: Gt P1 P2 P3 P4 P5
93965** Synopsis: IF r[P3]>r[P1]
93966**
93967** This works just like the Lt opcode except that the jump is taken if
93968** the content of register P3 is greater than the content of
93969** register P1. See the Lt opcode for additional information.
93970*/
93971/* Opcode: Ge P1 P2 P3 P4 P5
93972** Synopsis: IF r[P3]>=r[P1]
93973**
93974** This works just like the Lt opcode except that the jump is taken if
93975** the content of register P3 is greater than or equal to the content of
93976** register P1. See the Lt opcode for additional information.
93977*/
93978case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
93979case OP_Ne: /* same as TK_NE, jump, in1, in3 */
93980case OP_Lt: /* same as TK_LT, jump, in1, in3 */
93981case OP_Le: /* same as TK_LE, jump, in1, in3 */
93982case OP_Gt: /* same as TK_GT, jump, in1, in3 */
93983case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
93984 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
93985 char affinity; /* Affinity to use for comparison */
93986 u16 flags1; /* Copy of initial value of pIn1->flags */
93987 u16 flags3; /* Copy of initial value of pIn3->flags */
93988
93989 pIn1 = &aMem[pOp->p1];
93990 pIn3 = &aMem[pOp->p3];
93991 flags1 = pIn1->flags;
93992 flags3 = pIn3->flags;
93993 if( (flags1 & flags3 & MEM_Int)!=0 ){
93994 /* Common case of comparison of two integers */
93995 if( pIn3->u.i > pIn1->u.i ){
93996 if( sqlite3aGTb[pOp->opcode] ){
93997 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93998 goto jump_to_p2;
93999 }
94000 iCompare = +1;
94001 VVA_ONLY( iCompareIsInit = 1; )
94002 }else if( pIn3->u.i < pIn1->u.i ){
94003 if( sqlite3aLTb[pOp->opcode] ){
94004 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94005 goto jump_to_p2;
94006 }
94007 iCompare = -1;
94008 VVA_ONLY( iCompareIsInit = 1; )
94009 }else{
94010 if( sqlite3aEQb[pOp->opcode] ){
94011 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94012 goto jump_to_p2;
94013 }
94014 iCompare = 0;
94015 VVA_ONLY( iCompareIsInit = 1; )
94016 }
94017 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94018 break;
94019 }
94020 if( (flags1 | flags3)&MEM_Null ){
94021 /* One or both operands are NULL */
94022 if( pOp->p5 & SQLITE_NULLEQ ){
94023 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
94024 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
94025 ** or not both operands are null.
94026 */
94027 assert( (flags1 & MEM_Cleared)==0 );
94028 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
94029 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
94030 if( (flags1&flags3&MEM_Null)!=0
94031 && (flags3&MEM_Cleared)==0
94032 ){
94033 res = 0; /* Operands are equal */
94034 }else{
94035 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
94036 }
94037 }else{
94038 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
94039 ** then the result is always NULL.
94040 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
94041 */
94042 VdbeBranchTaken(2,3);
94043 if( pOp->p5 & SQLITE_JUMPIFNULL ){
94044 goto jump_to_p2;
94045 }
94046 iCompare = 1; /* Operands are not equal */
94047 VVA_ONLY( iCompareIsInit = 1; )
94048 break;
94049 }
94050 }else{
94051 /* Neither operand is NULL and we couldn't do the special high-speed
94052 ** integer comparison case. So do a general-case comparison. */
94053 affinity = pOp->p5 & SQLITE_AFF_MASK;
94054 if( affinity>=SQLITE_AFF_NUMERIC ){
94055 if( (flags1 | flags3)&MEM_Str ){
94056 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
94057 applyNumericAffinity(pRec: pIn1,bTryForInt: 0);
94058 assert( flags3==pIn3->flags || CORRUPT_DB );
94059 flags3 = pIn3->flags;
94060 }
94061 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
94062 applyNumericAffinity(pRec: pIn3,bTryForInt: 0);
94063 }
94064 }
94065 }else if( affinity==SQLITE_AFF_TEXT && ((flags1 | flags3) & MEM_Str)!=0 ){
94066 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
94067 testcase( pIn1->flags & MEM_Int );
94068 testcase( pIn1->flags & MEM_Real );
94069 testcase( pIn1->flags & MEM_IntReal );
94070 sqlite3VdbeMemStringify(pMem: pIn1, enc: encoding, bForce: 1);
94071 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
94072 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
94073 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
94074 }
94075 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
94076 testcase( pIn3->flags & MEM_Int );
94077 testcase( pIn3->flags & MEM_Real );
94078 testcase( pIn3->flags & MEM_IntReal );
94079 sqlite3VdbeMemStringify(pMem: pIn3, enc: encoding, bForce: 1);
94080 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
94081 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
94082 }
94083 }
94084 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
94085 res = sqlite3MemCompare(pMem1: pIn3, pMem2: pIn1, pColl: pOp->p4.pColl);
94086 }
94087
94088 /* At this point, res is negative, zero, or positive if reg[P1] is
94089 ** less than, equal to, or greater than reg[P3], respectively. Compute
94090 ** the answer to this operator in res2, depending on what the comparison
94091 ** operator actually is. The next block of code depends on the fact
94092 ** that the 6 comparison operators are consecutive integers in this
94093 ** order: NE, EQ, GT, LE, LT, GE */
94094 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
94095 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
94096 if( res<0 ){
94097 res2 = sqlite3aLTb[pOp->opcode];
94098 }else if( res==0 ){
94099 res2 = sqlite3aEQb[pOp->opcode];
94100 }else{
94101 res2 = sqlite3aGTb[pOp->opcode];
94102 }
94103 iCompare = res;
94104 VVA_ONLY( iCompareIsInit = 1; )
94105
94106 /* Undo any changes made by applyAffinity() to the input registers. */
94107 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
94108 pIn3->flags = flags3;
94109 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
94110 pIn1->flags = flags1;
94111
94112 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94113 if( res2 ){
94114 goto jump_to_p2;
94115 }
94116 break;
94117}
94118
94119/* Opcode: ElseEq * P2 * * *
94120**
94121** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
94122** can be zero or more OP_ReleaseReg opcodes intervening, but no other
94123** opcodes are allowed to occur between this instruction and the previous
94124** OP_Lt or OP_Gt.
94125**
94126** If the result of an OP_Eq comparison on the same two operands as
94127** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If
94128** the result of an OP_Eq comparison on the two previous operands
94129** would have been false or NULL, then fall through.
94130*/
94131case OP_ElseEq: { /* same as TK_ESCAPE, jump */
94132
94133#ifdef SQLITE_DEBUG
94134 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
94135 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
94136 int iAddr;
94137 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
94138 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
94139 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
94140 break;
94141 }
94142#endif /* SQLITE_DEBUG */
94143 assert( iCompareIsInit );
94144 VdbeBranchTaken(iCompare==0, 2);
94145 if( iCompare==0 ) goto jump_to_p2;
94146 break;
94147}
94148
94149
94150/* Opcode: Permutation * * * P4 *
94151**
94152** Set the permutation used by the OP_Compare operator in the next
94153** instruction. The permutation is stored in the P4 operand.
94154**
94155** The permutation is only valid for the next opcode which must be
94156** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
94157**
94158** The first integer in the P4 integer array is the length of the array
94159** and does not become part of the permutation.
94160*/
94161case OP_Permutation: {
94162 assert( pOp->p4type==P4_INTARRAY );
94163 assert( pOp->p4.ai );
94164 assert( pOp[1].opcode==OP_Compare );
94165 assert( pOp[1].p5 & OPFLAG_PERMUTE );
94166 break;
94167}
94168
94169/* Opcode: Compare P1 P2 P3 P4 P5
94170** Synopsis: r[P1@P3] <-> r[P2@P3]
94171**
94172** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
94173** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
94174** the comparison for use by the next OP_Jump instruct.
94175**
94176** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
94177** determined by the most recent OP_Permutation operator. If the
94178** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
94179** order.
94180**
94181** P4 is a KeyInfo structure that defines collating sequences and sort
94182** orders for the comparison. The permutation applies to registers
94183** only. The KeyInfo elements are used sequentially.
94184**
94185** The comparison is a sort comparison, so NULLs compare equal,
94186** NULLs are less than numbers, numbers are less than strings,
94187** and strings are less than blobs.
94188**
94189** This opcode must be immediately followed by an OP_Jump opcode.
94190*/
94191case OP_Compare: {
94192 int n;
94193 int i;
94194 int p1;
94195 int p2;
94196 const KeyInfo *pKeyInfo;
94197 u32 idx;
94198 CollSeq *pColl; /* Collating sequence to use on this term */
94199 int bRev; /* True for DESCENDING sort order */
94200 u32 *aPermute; /* The permutation */
94201
94202 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
94203 aPermute = 0;
94204 }else{
94205 assert( pOp>aOp );
94206 assert( pOp[-1].opcode==OP_Permutation );
94207 assert( pOp[-1].p4type==P4_INTARRAY );
94208 aPermute = pOp[-1].p4.ai + 1;
94209 assert( aPermute!=0 );
94210 }
94211 n = pOp->p3;
94212 pKeyInfo = pOp->p4.pKeyInfo;
94213 assert( n>0 );
94214 assert( pKeyInfo!=0 );
94215 p1 = pOp->p1;
94216 p2 = pOp->p2;
94217#ifdef SQLITE_DEBUG
94218 if( aPermute ){
94219 int k, mx = 0;
94220 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
94221 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
94222 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
94223 }else{
94224 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
94225 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
94226 }
94227#endif /* SQLITE_DEBUG */
94228 for(i=0; i<n; i++){
94229 idx = aPermute ? aPermute[i] : (u32)i;
94230 assert( memIsValid(&aMem[p1+idx]) );
94231 assert( memIsValid(&aMem[p2+idx]) );
94232 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
94233 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
94234 assert( i<pKeyInfo->nKeyField );
94235 pColl = pKeyInfo->aColl[i];
94236 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
94237 iCompare = sqlite3MemCompare(pMem1: &aMem[p1+idx], pMem2: &aMem[p2+idx], pColl);
94238 VVA_ONLY( iCompareIsInit = 1; )
94239 if( iCompare ){
94240 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
94241 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
94242 ){
94243 iCompare = -iCompare;
94244 }
94245 if( bRev ) iCompare = -iCompare;
94246 break;
94247 }
94248 }
94249 assert( pOp[1].opcode==OP_Jump );
94250 break;
94251}
94252
94253/* Opcode: Jump P1 P2 P3 * *
94254**
94255** Jump to the instruction at address P1, P2, or P3 depending on whether
94256** in the most recent OP_Compare instruction the P1 vector was less than,
94257** equal to, or greater than the P2 vector, respectively.
94258**
94259** This opcode must immediately follow an OP_Compare opcode.
94260*/
94261case OP_Jump: { /* jump */
94262 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
94263 assert( iCompareIsInit );
94264 if( iCompare<0 ){
94265 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
94266 }else if( iCompare==0 ){
94267 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
94268 }else{
94269 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
94270 }
94271 break;
94272}
94273
94274/* Opcode: And P1 P2 P3 * *
94275** Synopsis: r[P3]=(r[P1] && r[P2])
94276**
94277** Take the logical AND of the values in registers P1 and P2 and
94278** write the result into register P3.
94279**
94280** If either P1 or P2 is 0 (false) then the result is 0 even if
94281** the other input is NULL. A NULL and true or two NULLs give
94282** a NULL output.
94283*/
94284/* Opcode: Or P1 P2 P3 * *
94285** Synopsis: r[P3]=(r[P1] || r[P2])
94286**
94287** Take the logical OR of the values in register P1 and P2 and
94288** store the answer in register P3.
94289**
94290** If either P1 or P2 is nonzero (true) then the result is 1 (true)
94291** even if the other input is NULL. A NULL and false or two NULLs
94292** give a NULL output.
94293*/
94294case OP_And: /* same as TK_AND, in1, in2, out3 */
94295case OP_Or: { /* same as TK_OR, in1, in2, out3 */
94296 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
94297 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
94298
94299 v1 = sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: 2);
94300 v2 = sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p2], ifNull: 2);
94301 if( pOp->opcode==OP_And ){
94302 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
94303 v1 = and_logic[v1*3+v2];
94304 }else{
94305 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
94306 v1 = or_logic[v1*3+v2];
94307 }
94308 pOut = &aMem[pOp->p3];
94309 if( v1==2 ){
94310 MemSetTypeFlag(pOut, MEM_Null);
94311 }else{
94312 pOut->u.i = v1;
94313 MemSetTypeFlag(pOut, MEM_Int);
94314 }
94315 break;
94316}
94317
94318/* Opcode: IsTrue P1 P2 P3 P4 *
94319** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
94320**
94321** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
94322** IS NOT FALSE operators.
94323**
94324** Interpret the value in register P1 as a boolean value. Store that
94325** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
94326** NULL, then the P3 is stored in register P2. Invert the answer if P4
94327** is 1.
94328**
94329** The logic is summarized like this:
94330**
94331** <ul>
94332** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
94333** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
94334** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
94335** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
94336** </ul>
94337*/
94338case OP_IsTrue: { /* in1, out2 */
94339 assert( pOp->p4type==P4_INT32 );
94340 assert( pOp->p4.i==0 || pOp->p4.i==1 );
94341 assert( pOp->p3==0 || pOp->p3==1 );
94342 sqlite3VdbeMemSetInt64(pMem: &aMem[pOp->p2],
94343 val: sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: pOp->p3) ^ pOp->p4.i);
94344 break;
94345}
94346
94347/* Opcode: Not P1 P2 * * *
94348** Synopsis: r[P2]= !r[P1]
94349**
94350** Interpret the value in register P1 as a boolean value. Store the
94351** boolean complement in register P2. If the value in register P1 is
94352** NULL, then a NULL is stored in P2.
94353*/
94354case OP_Not: { /* same as TK_NOT, in1, out2 */
94355 pIn1 = &aMem[pOp->p1];
94356 pOut = &aMem[pOp->p2];
94357 if( (pIn1->flags & MEM_Null)==0 ){
94358 sqlite3VdbeMemSetInt64(pMem: pOut, val: !sqlite3VdbeBooleanValue(pMem: pIn1,ifNull: 0));
94359 }else{
94360 sqlite3VdbeMemSetNull(pMem: pOut);
94361 }
94362 break;
94363}
94364
94365/* Opcode: BitNot P1 P2 * * *
94366** Synopsis: r[P2]= ~r[P1]
94367**
94368** Interpret the content of register P1 as an integer. Store the
94369** ones-complement of the P1 value into register P2. If P1 holds
94370** a NULL then store a NULL in P2.
94371*/
94372case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
94373 pIn1 = &aMem[pOp->p1];
94374 pOut = &aMem[pOp->p2];
94375 sqlite3VdbeMemSetNull(pMem: pOut);
94376 if( (pIn1->flags & MEM_Null)==0 ){
94377 pOut->flags = MEM_Int;
94378 pOut->u.i = ~sqlite3VdbeIntValue(pMem: pIn1);
94379 }
94380 break;
94381}
94382
94383/* Opcode: Once P1 P2 * * *
94384**
94385** Fall through to the next instruction the first time this opcode is
94386** encountered on each invocation of the byte-code program. Jump to P2
94387** on the second and all subsequent encounters during the same invocation.
94388**
94389** Top-level programs determine first invocation by comparing the P1
94390** operand against the P1 operand on the OP_Init opcode at the beginning
94391** of the program. If the P1 values differ, then fall through and make
94392** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
94393** the same then take the jump.
94394**
94395** For subprograms, there is a bitmask in the VdbeFrame that determines
94396** whether or not the jump should be taken. The bitmask is necessary
94397** because the self-altering code trick does not work for recursive
94398** triggers.
94399*/
94400case OP_Once: { /* jump */
94401 u32 iAddr; /* Address of this instruction */
94402 assert( p->aOp[0].opcode==OP_Init );
94403 if( p->pFrame ){
94404 iAddr = (int)(pOp - p->aOp);
94405 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
94406 VdbeBranchTaken(1, 2);
94407 goto jump_to_p2;
94408 }
94409 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
94410 }else{
94411 if( p->aOp[0].p1==pOp->p1 ){
94412 VdbeBranchTaken(1, 2);
94413 goto jump_to_p2;
94414 }
94415 }
94416 VdbeBranchTaken(0, 2);
94417 pOp->p1 = p->aOp[0].p1;
94418 break;
94419}
94420
94421/* Opcode: If P1 P2 P3 * *
94422**
94423** Jump to P2 if the value in register P1 is true. The value
94424** is considered true if it is numeric and non-zero. If the value
94425** in P1 is NULL then take the jump if and only if P3 is non-zero.
94426*/
94427case OP_If: { /* jump, in1 */
94428 int c;
94429 c = sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: pOp->p3);
94430 VdbeBranchTaken(c!=0, 2);
94431 if( c ) goto jump_to_p2;
94432 break;
94433}
94434
94435/* Opcode: IfNot P1 P2 P3 * *
94436**
94437** Jump to P2 if the value in register P1 is False. The value
94438** is considered false if it has a numeric value of zero. If the value
94439** in P1 is NULL then take the jump if and only if P3 is non-zero.
94440*/
94441case OP_IfNot: { /* jump, in1 */
94442 int c;
94443 c = !sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: !pOp->p3);
94444 VdbeBranchTaken(c!=0, 2);
94445 if( c ) goto jump_to_p2;
94446 break;
94447}
94448
94449/* Opcode: IsNull P1 P2 * * *
94450** Synopsis: if r[P1]==NULL goto P2
94451**
94452** Jump to P2 if the value in register P1 is NULL.
94453*/
94454case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
94455 pIn1 = &aMem[pOp->p1];
94456 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
94457 if( (pIn1->flags & MEM_Null)!=0 ){
94458 goto jump_to_p2;
94459 }
94460 break;
94461}
94462
94463/* Opcode: IsType P1 P2 P3 P4 P5
94464** Synopsis: if typeof(P1.P3) in P5 goto P2
94465**
94466** Jump to P2 if the type of a column in a btree is one of the types specified
94467** by the P5 bitmask.
94468**
94469** P1 is normally a cursor on a btree for which the row decode cache is
94470** valid through at least column P3. In other words, there should have been
94471** a prior OP_Column for column P3 or greater. If the cursor is not valid,
94472** then this opcode might give spurious results.
94473** The the btree row has fewer than P3 columns, then use P4 as the
94474** datatype.
94475**
94476** If P1 is -1, then P3 is a register number and the datatype is taken
94477** from the value in that register.
94478**
94479** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
94480** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
94481** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
94482**
94483** WARNING: This opcode does not reliably distinguish between NULL and REAL
94484** when P1>=0. If the database contains a NaN value, this opcode will think
94485** that the datatype is REAL when it should be NULL. When P1<0 and the value
94486** is already stored in register P3, then this opcode does reliably
94487** distinguish between NULL and REAL. The problem only arises then P1>=0.
94488**
94489** Take the jump to address P2 if and only if the datatype of the
94490** value determined by P1 and P3 corresponds to one of the bits in the
94491** P5 bitmask.
94492**
94493*/
94494case OP_IsType: { /* jump */
94495 VdbeCursor *pC;
94496 u16 typeMask;
94497 u32 serialType;
94498
94499 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
94500 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
94501 if( pOp->p1>=0 ){
94502 pC = p->apCsr[pOp->p1];
94503 assert( pC!=0 );
94504 assert( pOp->p3>=0 );
94505 if( pOp->p3<pC->nHdrParsed ){
94506 serialType = pC->aType[pOp->p3];
94507 if( serialType>=12 ){
94508 if( serialType&1 ){
94509 typeMask = 0x04; /* SQLITE_TEXT */
94510 }else{
94511 typeMask = 0x08; /* SQLITE_BLOB */
94512 }
94513 }else{
94514 static const unsigned char aMask[] = {
94515 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
94516 0x01, 0x01, 0x10, 0x10
94517 };
94518 testcase( serialType==0 );
94519 testcase( serialType==1 );
94520 testcase( serialType==2 );
94521 testcase( serialType==3 );
94522 testcase( serialType==4 );
94523 testcase( serialType==5 );
94524 testcase( serialType==6 );
94525 testcase( serialType==7 );
94526 testcase( serialType==8 );
94527 testcase( serialType==9 );
94528 testcase( serialType==10 );
94529 testcase( serialType==11 );
94530 typeMask = aMask[serialType];
94531 }
94532 }else{
94533 typeMask = 1 << (pOp->p4.i - 1);
94534 testcase( typeMask==0x01 );
94535 testcase( typeMask==0x02 );
94536 testcase( typeMask==0x04 );
94537 testcase( typeMask==0x08 );
94538 testcase( typeMask==0x10 );
94539 }
94540 }else{
94541 assert( memIsValid(&aMem[pOp->p3]) );
94542 typeMask = 1 << (sqlite3_value_type(pVal: (sqlite3_value*)&aMem[pOp->p3])-1);
94543 testcase( typeMask==0x01 );
94544 testcase( typeMask==0x02 );
94545 testcase( typeMask==0x04 );
94546 testcase( typeMask==0x08 );
94547 testcase( typeMask==0x10 );
94548 }
94549 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
94550 if( typeMask & pOp->p5 ){
94551 goto jump_to_p2;
94552 }
94553 break;
94554}
94555
94556/* Opcode: ZeroOrNull P1 P2 P3 * *
94557** Synopsis: r[P2] = 0 OR NULL
94558**
94559** If both registers P1 and P3 are NOT NULL, then store a zero in
94560** register P2. If either registers P1 or P3 are NULL then put
94561** a NULL in register P2.
94562*/
94563case OP_ZeroOrNull: { /* in1, in2, out2, in3 */
94564 if( (aMem[pOp->p1].flags & MEM_Null)!=0
94565 || (aMem[pOp->p3].flags & MEM_Null)!=0
94566 ){
94567 sqlite3VdbeMemSetNull(pMem: aMem + pOp->p2);
94568 }else{
94569 sqlite3VdbeMemSetInt64(pMem: aMem + pOp->p2, val: 0);
94570 }
94571 break;
94572}
94573
94574/* Opcode: NotNull P1 P2 * * *
94575** Synopsis: if r[P1]!=NULL goto P2
94576**
94577** Jump to P2 if the value in register P1 is not NULL.
94578*/
94579case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
94580 pIn1 = &aMem[pOp->p1];
94581 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
94582 if( (pIn1->flags & MEM_Null)==0 ){
94583 goto jump_to_p2;
94584 }
94585 break;
94586}
94587
94588/* Opcode: IfNullRow P1 P2 P3 * *
94589** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
94590**
94591** Check the cursor P1 to see if it is currently pointing at a NULL row.
94592** If it is, then set register P3 to NULL and jump immediately to P2.
94593** If P1 is not on a NULL row, then fall through without making any
94594** changes.
94595**
94596** If P1 is not an open cursor, then this opcode is a no-op.
94597*/
94598case OP_IfNullRow: { /* jump */
94599 VdbeCursor *pC;
94600 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94601 pC = p->apCsr[pOp->p1];
94602 if( pC && pC->nullRow ){
94603 sqlite3VdbeMemSetNull(pMem: aMem + pOp->p3);
94604 goto jump_to_p2;
94605 }
94606 break;
94607}
94608
94609#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
94610/* Opcode: Offset P1 P2 P3 * *
94611** Synopsis: r[P3] = sqlite_offset(P1)
94612**
94613** Store in register r[P3] the byte offset into the database file that is the
94614** start of the payload for the record at which that cursor P1 is currently
94615** pointing.
94616**
94617** P2 is the column number for the argument to the sqlite_offset() function.
94618** This opcode does not use P2 itself, but the P2 value is used by the
94619** code generator. The P1, P2, and P3 operands to this opcode are the
94620** same as for OP_Column.
94621**
94622** This opcode is only available if SQLite is compiled with the
94623** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
94624*/
94625case OP_Offset: { /* out3 */
94626 VdbeCursor *pC; /* The VDBE cursor */
94627 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94628 pC = p->apCsr[pOp->p1];
94629 pOut = &p->aMem[pOp->p3];
94630 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
94631 sqlite3VdbeMemSetNull(pOut);
94632 }else{
94633 if( pC->deferredMoveto ){
94634 rc = sqlite3VdbeFinishMoveto(pC);
94635 if( rc ) goto abort_due_to_error;
94636 }
94637 if( sqlite3BtreeEof(pC->uc.pCursor) ){
94638 sqlite3VdbeMemSetNull(pOut);
94639 }else{
94640 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
94641 }
94642 }
94643 break;
94644}
94645#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
94646
94647/* Opcode: Column P1 P2 P3 P4 P5
94648** Synopsis: r[P3]=PX cursor P1 column P2
94649**
94650** Interpret the data that cursor P1 points to as a structure built using
94651** the MakeRecord instruction. (See the MakeRecord opcode for additional
94652** information about the format of the data.) Extract the P2-th column
94653** from this record. If there are less than (P2+1)
94654** values in the record, extract a NULL.
94655**
94656** The value extracted is stored in register P3.
94657**
94658** If the record contains fewer than P2 fields, then extract a NULL. Or,
94659** if the P4 argument is a P4_MEM use the value of the P4 argument as
94660** the result.
94661**
94662** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
94663** to only be used by the length() function or the equivalent. The content
94664** of large blobs is not loaded, thus saving CPU cycles. If the
94665** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
94666** typeof() function or the IS NULL or IS NOT NULL operators or the
94667** equivalent. In this case, all content loading can be omitted.
94668*/
94669case OP_Column: { /* ncycle */
94670 u32 p2; /* column number to retrieve */
94671 VdbeCursor *pC; /* The VDBE cursor */
94672 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
94673 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
94674 int len; /* The length of the serialized data for the column */
94675 int i; /* Loop counter */
94676 Mem *pDest; /* Where to write the extracted value */
94677 Mem sMem; /* For storing the record being decoded */
94678 const u8 *zData; /* Part of the record being decoded */
94679 const u8 *zHdr; /* Next unparsed byte of the header */
94680 const u8 *zEndHdr; /* Pointer to first byte after the header */
94681 u64 offset64; /* 64-bit offset */
94682 u32 t; /* A type code from the record header */
94683 Mem *pReg; /* PseudoTable input register */
94684
94685 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94686 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
94687 pC = p->apCsr[pOp->p1];
94688 p2 = (u32)pOp->p2;
94689
94690op_column_restart:
94691 assert( pC!=0 );
94692 assert( p2<(u32)pC->nField
94693 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
94694 aOffset = pC->aOffset;
94695 assert( aOffset==pC->aType+pC->nField );
94696 assert( pC->eCurType!=CURTYPE_VTAB );
94697 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
94698 assert( pC->eCurType!=CURTYPE_SORTER );
94699
94700 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
94701 if( pC->nullRow ){
94702 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
94703 /* For the special case of as pseudo-cursor, the seekResult field
94704 ** identifies the register that holds the record */
94705 pReg = &aMem[pC->seekResult];
94706 assert( pReg->flags & MEM_Blob );
94707 assert( memIsValid(pReg) );
94708 pC->payloadSize = pC->szRow = pReg->n;
94709 pC->aRow = (u8*)pReg->z;
94710 }else{
94711 pDest = &aMem[pOp->p3];
94712 memAboutToChange(p, pDest);
94713 sqlite3VdbeMemSetNull(pMem: pDest);
94714 goto op_column_out;
94715 }
94716 }else{
94717 pCrsr = pC->uc.pCursor;
94718 if( pC->deferredMoveto ){
94719 u32 iMap;
94720 assert( !pC->isEphemeral );
94721 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
94722 pC = pC->pAltCursor;
94723 p2 = iMap - 1;
94724 goto op_column_restart;
94725 }
94726 rc = sqlite3VdbeFinishMoveto(p: pC);
94727 if( rc ) goto abort_due_to_error;
94728 }else if( sqlite3BtreeCursorHasMoved(pCur: pCrsr) ){
94729 rc = sqlite3VdbeHandleMovedCursor(p: pC);
94730 if( rc ) goto abort_due_to_error;
94731 goto op_column_restart;
94732 }
94733 assert( pC->eCurType==CURTYPE_BTREE );
94734 assert( pCrsr );
94735 assert( sqlite3BtreeCursorIsValid(pCrsr) );
94736 pC->payloadSize = sqlite3BtreePayloadSize(pCur: pCrsr);
94737 pC->aRow = sqlite3BtreePayloadFetch(pCur: pCrsr, pAmt: &pC->szRow);
94738 assert( pC->szRow<=pC->payloadSize );
94739 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
94740 }
94741 pC->cacheStatus = p->cacheCtr;
94742 if( (aOffset[0] = pC->aRow[0])<0x80 ){
94743 pC->iHdrOffset = 1;
94744 }else{
94745 pC->iHdrOffset = sqlite3GetVarint32(p: pC->aRow, v: aOffset);
94746 }
94747 pC->nHdrParsed = 0;
94748
94749 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
94750 /* pC->aRow does not have to hold the entire row, but it does at least
94751 ** need to cover the header of the record. If pC->aRow does not contain
94752 ** the complete header, then set it to zero, forcing the header to be
94753 ** dynamically allocated. */
94754 pC->aRow = 0;
94755 pC->szRow = 0;
94756
94757 /* Make sure a corrupt database has not given us an oversize header.
94758 ** Do this now to avoid an oversize memory allocation.
94759 **
94760 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
94761 ** types use so much data space that there can only be 4096 and 32 of
94762 ** them, respectively. So the maximum header length results from a
94763 ** 3-byte type for each of the maximum of 32768 columns plus three
94764 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
94765 */
94766 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
94767 goto op_column_corrupt;
94768 }
94769 }else{
94770 /* This is an optimization. By skipping over the first few tests
94771 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
94772 ** measurable performance gain.
94773 **
94774 ** This branch is taken even if aOffset[0]==0. Such a record is never
94775 ** generated by SQLite, and could be considered corruption, but we
94776 ** accept it for historical reasons. When aOffset[0]==0, the code this
94777 ** branch jumps to reads past the end of the record, but never more
94778 ** than a few bytes. Even if the record occurs at the end of the page
94779 ** content area, the "page header" comes after the page content and so
94780 ** this overread is harmless. Similar overreads can occur for a corrupt
94781 ** database file.
94782 */
94783 zData = pC->aRow;
94784 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
94785 testcase( aOffset[0]==0 );
94786 goto op_column_read_header;
94787 }
94788 }else if( sqlite3BtreeCursorHasMoved(pCur: pC->uc.pCursor) ){
94789 rc = sqlite3VdbeHandleMovedCursor(p: pC);
94790 if( rc ) goto abort_due_to_error;
94791 goto op_column_restart;
94792 }
94793
94794 /* Make sure at least the first p2+1 entries of the header have been
94795 ** parsed and valid information is in aOffset[] and pC->aType[].
94796 */
94797 if( pC->nHdrParsed<=p2 ){
94798 /* If there is more header available for parsing in the record, try
94799 ** to extract additional fields up through the p2+1-th field
94800 */
94801 if( pC->iHdrOffset<aOffset[0] ){
94802 /* Make sure zData points to enough of the record to cover the header. */
94803 if( pC->aRow==0 ){
94804 memset(s: &sMem, c: 0, n: sizeof(sMem));
94805 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur: pC->uc.pCursor,amt: aOffset[0],pMem: &sMem);
94806 if( rc!=SQLITE_OK ) goto abort_due_to_error;
94807 zData = (u8*)sMem.z;
94808 }else{
94809 zData = pC->aRow;
94810 }
94811
94812 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
94813 op_column_read_header:
94814 i = pC->nHdrParsed;
94815 offset64 = aOffset[i];
94816 zHdr = zData + pC->iHdrOffset;
94817 zEndHdr = zData + aOffset[0];
94818 testcase( zHdr>=zEndHdr );
94819 do{
94820 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
94821 zHdr++;
94822 offset64 += sqlite3VdbeOneByteSerialTypeLen(serial_type: t);
94823 }else{
94824 zHdr += sqlite3GetVarint32(p: zHdr, v: &t);
94825 pC->aType[i] = t;
94826 offset64 += sqlite3VdbeSerialTypeLen(serial_type: t);
94827 }
94828 aOffset[++i] = (u32)(offset64 & 0xffffffff);
94829 }while( (u32)i<=p2 && zHdr<zEndHdr );
94830
94831 /* The record is corrupt if any of the following are true:
94832 ** (1) the bytes of the header extend past the declared header size
94833 ** (2) the entire header was used but not all data was used
94834 ** (3) the end of the data extends beyond the end of the record.
94835 */
94836 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
94837 || (offset64 > pC->payloadSize)
94838 ){
94839 if( aOffset[0]==0 ){
94840 i = 0;
94841 zHdr = zEndHdr;
94842 }else{
94843 if( pC->aRow==0 ) sqlite3VdbeMemRelease(p: &sMem);
94844 goto op_column_corrupt;
94845 }
94846 }
94847
94848 pC->nHdrParsed = i;
94849 pC->iHdrOffset = (u32)(zHdr - zData);
94850 if( pC->aRow==0 ) sqlite3VdbeMemRelease(p: &sMem);
94851 }else{
94852 t = 0;
94853 }
94854
94855 /* If after trying to extract new entries from the header, nHdrParsed is
94856 ** still not up to p2, that means that the record has fewer than p2
94857 ** columns. So the result will be either the default value or a NULL.
94858 */
94859 if( pC->nHdrParsed<=p2 ){
94860 pDest = &aMem[pOp->p3];
94861 memAboutToChange(p, pDest);
94862 if( pOp->p4type==P4_MEM ){
94863 sqlite3VdbeMemShallowCopy(pTo: pDest, pFrom: pOp->p4.pMem, MEM_Static);
94864 }else{
94865 sqlite3VdbeMemSetNull(pMem: pDest);
94866 }
94867 goto op_column_out;
94868 }
94869 }else{
94870 t = pC->aType[p2];
94871 }
94872
94873 /* Extract the content for the p2+1-th column. Control can only
94874 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
94875 ** all valid.
94876 */
94877 assert( p2<pC->nHdrParsed );
94878 assert( rc==SQLITE_OK );
94879 pDest = &aMem[pOp->p3];
94880 memAboutToChange(p, pDest);
94881 assert( sqlite3VdbeCheckMemInvariants(pDest) );
94882 if( VdbeMemDynamic(pDest) ){
94883 sqlite3VdbeMemSetNull(pMem: pDest);
94884 }
94885 assert( t==pC->aType[p2] );
94886 if( pC->szRow>=aOffset[p2+1] ){
94887 /* This is the common case where the desired content fits on the original
94888 ** page - where the content is not on an overflow page */
94889 zData = pC->aRow + aOffset[p2];
94890 if( t<12 ){
94891 sqlite3VdbeSerialGet(buf: zData, serial_type: t, pMem: pDest);
94892 }else{
94893 /* If the column value is a string, we need a persistent value, not
94894 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
94895 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
94896 */
94897 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
94898 pDest->n = len = (t-12)/2;
94899 pDest->enc = encoding;
94900 if( pDest->szMalloc < len+2 ){
94901 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
94902 pDest->flags = MEM_Null;
94903 if( sqlite3VdbeMemGrow(pMem: pDest, n: len+2, bPreserve: 0) ) goto no_mem;
94904 }else{
94905 pDest->z = pDest->zMalloc;
94906 }
94907 memcpy(dest: pDest->z, src: zData, n: len);
94908 pDest->z[len] = 0;
94909 pDest->z[len+1] = 0;
94910 pDest->flags = aFlag[t&1];
94911 }
94912 }else{
94913 u8 p5;
94914 pDest->enc = encoding;
94915 assert( pDest->db==db );
94916 /* This branch happens only when content is on overflow pages */
94917 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0
94918 && (p5==OPFLAG_TYPEOFARG
94919 || (t>=12 && ((t&1)==0 || p5==OPFLAG_BYTELENARG))
94920 )
94921 )
94922 || sqlite3VdbeSerialTypeLen(serial_type: t)==0
94923 ){
94924 /* Content is irrelevant for
94925 ** 1. the typeof() function,
94926 ** 2. the length(X) function if X is a blob, and
94927 ** 3. if the content length is zero.
94928 ** So we might as well use bogus content rather than reading
94929 ** content from disk.
94930 **
94931 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
94932 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
94933 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
94934 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
94935 ** and it begins with a bunch of zeros.
94936 */
94937 sqlite3VdbeSerialGet(buf: (u8*)sqlite3CtypeMap, serial_type: t, pMem: pDest);
94938 }else{
94939 rc = vdbeColumnFromOverflow(pC, iCol: p2, t, iOffset: aOffset[p2],
94940 cacheStatus: p->cacheCtr, colCacheCtr, pDest);
94941 if( rc ){
94942 if( rc==SQLITE_NOMEM ) goto no_mem;
94943 if( rc==SQLITE_TOOBIG ) goto too_big;
94944 goto abort_due_to_error;
94945 }
94946 }
94947 }
94948
94949op_column_out:
94950 UPDATE_MAX_BLOBSIZE(pDest);
94951 REGISTER_TRACE(pOp->p3, pDest);
94952 break;
94953
94954op_column_corrupt:
94955 if( aOp[0].p3>0 ){
94956 pOp = &aOp[aOp[0].p3-1];
94957 break;
94958 }else{
94959 rc = SQLITE_CORRUPT_BKPT;
94960 goto abort_due_to_error;
94961 }
94962}
94963
94964/* Opcode: TypeCheck P1 P2 P3 P4 *
94965** Synopsis: typecheck(r[P1@P2])
94966**
94967** Apply affinities to the range of P2 registers beginning with P1.
94968** Take the affinities from the Table object in P4. If any value
94969** cannot be coerced into the correct type, then raise an error.
94970**
94971** This opcode is similar to OP_Affinity except that this opcode
94972** forces the register type to the Table column type. This is used
94973** to implement "strict affinity".
94974**
94975** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
94976** is zero. When P3 is non-zero, no type checking occurs for
94977** static generated columns. Virtual columns are computed at query time
94978** and so they are never checked.
94979**
94980** Preconditions:
94981**
94982** <ul>
94983** <li> P2 should be the number of non-virtual columns in the
94984** table of P4.
94985** <li> Table P4 should be a STRICT table.
94986** </ul>
94987**
94988** If any precondition is false, an assertion fault occurs.
94989*/
94990case OP_TypeCheck: {
94991 Table *pTab;
94992 Column *aCol;
94993 int i;
94994
94995 assert( pOp->p4type==P4_TABLE );
94996 pTab = pOp->p4.pTab;
94997 assert( pTab->tabFlags & TF_Strict );
94998 assert( pTab->nNVCol==pOp->p2 );
94999 aCol = pTab->aCol;
95000 pIn1 = &aMem[pOp->p1];
95001 for(i=0; i<pTab->nCol; i++){
95002 if( aCol[i].colFlags & COLFLAG_GENERATED ){
95003 if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue;
95004 if( pOp->p3 ){ pIn1++; continue; }
95005 }
95006 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
95007 applyAffinity(pRec: pIn1, affinity: aCol[i].affinity, enc: encoding);
95008 if( (pIn1->flags & MEM_Null)==0 ){
95009 switch( aCol[i].eCType ){
95010 case COLTYPE_BLOB: {
95011 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
95012 break;
95013 }
95014 case COLTYPE_INTEGER:
95015 case COLTYPE_INT: {
95016 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
95017 break;
95018 }
95019 case COLTYPE_TEXT: {
95020 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
95021 break;
95022 }
95023 case COLTYPE_REAL: {
95024 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
95025 assert( (pIn1->flags & MEM_IntReal)==0 );
95026 if( pIn1->flags & MEM_Int ){
95027 /* When applying REAL affinity, if the result is still an MEM_Int
95028 ** that will fit in 6 bytes, then change the type to MEM_IntReal
95029 ** so that we keep the high-resolution integer value but know that
95030 ** the type really wants to be REAL. */
95031 testcase( pIn1->u.i==140737488355328LL );
95032 testcase( pIn1->u.i==140737488355327LL );
95033 testcase( pIn1->u.i==-140737488355328LL );
95034 testcase( pIn1->u.i==-140737488355329LL );
95035 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
95036 pIn1->flags |= MEM_IntReal;
95037 pIn1->flags &= ~MEM_Int;
95038 }else{
95039 pIn1->u.r = (double)pIn1->u.i;
95040 pIn1->flags |= MEM_Real;
95041 pIn1->flags &= ~MEM_Int;
95042 }
95043 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
95044 goto vdbe_type_error;
95045 }
95046 break;
95047 }
95048 default: {
95049 /* COLTYPE_ANY. Accept anything. */
95050 break;
95051 }
95052 }
95053 }
95054 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
95055 pIn1++;
95056 }
95057 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
95058 break;
95059
95060vdbe_type_error:
95061 sqlite3VdbeError(p, zFormat: "cannot store %s value in %s column %s.%s",
95062 vdbeMemTypeName(pMem: pIn1), sqlite3StdType[aCol[i].eCType-1],
95063 pTab->zName, aCol[i].zCnName);
95064 rc = SQLITE_CONSTRAINT_DATATYPE;
95065 goto abort_due_to_error;
95066}
95067
95068/* Opcode: Affinity P1 P2 * P4 *
95069** Synopsis: affinity(r[P1@P2])
95070**
95071** Apply affinities to a range of P2 registers starting with P1.
95072**
95073** P4 is a string that is P2 characters long. The N-th character of the
95074** string indicates the column affinity that should be used for the N-th
95075** memory cell in the range.
95076*/
95077case OP_Affinity: {
95078 const char *zAffinity; /* The affinity to be applied */
95079
95080 zAffinity = pOp->p4.z;
95081 assert( zAffinity!=0 );
95082 assert( pOp->p2>0 );
95083 assert( zAffinity[pOp->p2]==0 );
95084 pIn1 = &aMem[pOp->p1];
95085 while( 1 /*exit-by-break*/ ){
95086 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
95087 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
95088 applyAffinity(pRec: pIn1, affinity: zAffinity[0], enc: encoding);
95089 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
95090 /* When applying REAL affinity, if the result is still an MEM_Int
95091 ** that will fit in 6 bytes, then change the type to MEM_IntReal
95092 ** so that we keep the high-resolution integer value but know that
95093 ** the type really wants to be REAL. */
95094 testcase( pIn1->u.i==140737488355328LL );
95095 testcase( pIn1->u.i==140737488355327LL );
95096 testcase( pIn1->u.i==-140737488355328LL );
95097 testcase( pIn1->u.i==-140737488355329LL );
95098 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
95099 pIn1->flags |= MEM_IntReal;
95100 pIn1->flags &= ~MEM_Int;
95101 }else{
95102 pIn1->u.r = (double)pIn1->u.i;
95103 pIn1->flags |= MEM_Real;
95104 pIn1->flags &= ~(MEM_Int|MEM_Str);
95105 }
95106 }
95107 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
95108 zAffinity++;
95109 if( zAffinity[0]==0 ) break;
95110 pIn1++;
95111 }
95112 break;
95113}
95114
95115/* Opcode: MakeRecord P1 P2 P3 P4 *
95116** Synopsis: r[P3]=mkrec(r[P1@P2])
95117**
95118** Convert P2 registers beginning with P1 into the [record format]
95119** use as a data record in a database table or as a key
95120** in an index. The OP_Column opcode can decode the record later.
95121**
95122** P4 may be a string that is P2 characters long. The N-th character of the
95123** string indicates the column affinity that should be used for the N-th
95124** field of the index key.
95125**
95126** The mapping from character to affinity is given by the SQLITE_AFF_
95127** macros defined in sqliteInt.h.
95128**
95129** If P4 is NULL then all index fields have the affinity BLOB.
95130**
95131** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
95132** compile-time option is enabled:
95133**
95134** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
95135** of the right-most table that can be null-trimmed.
95136**
95137** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
95138** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
95139** accept no-change records with serial_type 10. This value is
95140** only used inside an assert() and does not affect the end result.
95141*/
95142case OP_MakeRecord: {
95143 Mem *pRec; /* The new record */
95144 u64 nData; /* Number of bytes of data space */
95145 int nHdr; /* Number of bytes of header space */
95146 i64 nByte; /* Data space required for this record */
95147 i64 nZero; /* Number of zero bytes at the end of the record */
95148 int nVarint; /* Number of bytes in a varint */
95149 u32 serial_type; /* Type field */
95150 Mem *pData0; /* First field to be combined into the record */
95151 Mem *pLast; /* Last field of the record */
95152 int nField; /* Number of fields in the record */
95153 char *zAffinity; /* The affinity string for the record */
95154 u32 len; /* Length of a field */
95155 u8 *zHdr; /* Where to write next byte of the header */
95156 u8 *zPayload; /* Where to write next byte of the payload */
95157
95158 /* Assuming the record contains N fields, the record format looks
95159 ** like this:
95160 **
95161 ** ------------------------------------------------------------------------
95162 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
95163 ** ------------------------------------------------------------------------
95164 **
95165 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
95166 ** and so forth.
95167 **
95168 ** Each type field is a varint representing the serial type of the
95169 ** corresponding data element (see sqlite3VdbeSerialType()). The
95170 ** hdr-size field is also a varint which is the offset from the beginning
95171 ** of the record to data0.
95172 */
95173 nData = 0; /* Number of bytes of data space */
95174 nHdr = 0; /* Number of bytes of header space */
95175 nZero = 0; /* Number of zero bytes at the end of the record */
95176 nField = pOp->p1;
95177 zAffinity = pOp->p4.z;
95178 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
95179 pData0 = &aMem[nField];
95180 nField = pOp->p2;
95181 pLast = &pData0[nField-1];
95182
95183 /* Identify the output register */
95184 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
95185 pOut = &aMem[pOp->p3];
95186 memAboutToChange(p, pOut);
95187
95188 /* Apply the requested affinity to all inputs
95189 */
95190 assert( pData0<=pLast );
95191 if( zAffinity ){
95192 pRec = pData0;
95193 do{
95194 applyAffinity(pRec, affinity: zAffinity[0], enc: encoding);
95195 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
95196 pRec->flags |= MEM_IntReal;
95197 pRec->flags &= ~(MEM_Int);
95198 }
95199 REGISTER_TRACE((int)(pRec-aMem), pRec);
95200 zAffinity++;
95201 pRec++;
95202 assert( zAffinity[0]==0 || pRec<=pLast );
95203 }while( zAffinity[0] );
95204 }
95205
95206#ifdef SQLITE_ENABLE_NULL_TRIM
95207 /* NULLs can be safely trimmed from the end of the record, as long as
95208 ** as the schema format is 2 or more and none of the omitted columns
95209 ** have a non-NULL default value. Also, the record must be left with
95210 ** at least one field. If P5>0 then it will be one more than the
95211 ** index of the right-most column with a non-NULL default value */
95212 if( pOp->p5 ){
95213 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
95214 pLast--;
95215 nField--;
95216 }
95217 }
95218#endif
95219
95220 /* Loop through the elements that will make up the record to figure
95221 ** out how much space is required for the new record. After this loop,
95222 ** the Mem.uTemp field of each term should hold the serial-type that will
95223 ** be used for that term in the generated record:
95224 **
95225 ** Mem.uTemp value type
95226 ** --------------- ---------------
95227 ** 0 NULL
95228 ** 1 1-byte signed integer
95229 ** 2 2-byte signed integer
95230 ** 3 3-byte signed integer
95231 ** 4 4-byte signed integer
95232 ** 5 6-byte signed integer
95233 ** 6 8-byte signed integer
95234 ** 7 IEEE float
95235 ** 8 Integer constant 0
95236 ** 9 Integer constant 1
95237 ** 10,11 reserved for expansion
95238 ** N>=12 and even BLOB
95239 ** N>=13 and odd text
95240 **
95241 ** The following additional values are computed:
95242 ** nHdr Number of bytes needed for the record header
95243 ** nData Number of bytes of data space needed for the record
95244 ** nZero Zero bytes at the end of the record
95245 */
95246 pRec = pLast;
95247 do{
95248 assert( memIsValid(pRec) );
95249 if( pRec->flags & MEM_Null ){
95250 if( pRec->flags & MEM_Zero ){
95251 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
95252 ** table methods that never invoke sqlite3_result_xxxxx() while
95253 ** computing an unchanging column value in an UPDATE statement.
95254 ** Give such values a special internal-use-only serial-type of 10
95255 ** so that they can be passed through to xUpdate and have
95256 ** a true sqlite3_value_nochange(). */
95257#ifndef SQLITE_ENABLE_NULL_TRIM
95258 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
95259#endif
95260 pRec->uTemp = 10;
95261 }else{
95262 pRec->uTemp = 0;
95263 }
95264 nHdr++;
95265 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
95266 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
95267 i64 i = pRec->u.i;
95268 u64 uu;
95269 testcase( pRec->flags & MEM_Int );
95270 testcase( pRec->flags & MEM_IntReal );
95271 if( i<0 ){
95272 uu = ~i;
95273 }else{
95274 uu = i;
95275 }
95276 nHdr++;
95277 testcase( uu==127 ); testcase( uu==128 );
95278 testcase( uu==32767 ); testcase( uu==32768 );
95279 testcase( uu==8388607 ); testcase( uu==8388608 );
95280 testcase( uu==2147483647 ); testcase( uu==2147483648LL );
95281 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
95282 if( uu<=127 ){
95283 if( (i&1)==i && p->minWriteFileFormat>=4 ){
95284 pRec->uTemp = 8+(u32)uu;
95285 }else{
95286 nData++;
95287 pRec->uTemp = 1;
95288 }
95289 }else if( uu<=32767 ){
95290 nData += 2;
95291 pRec->uTemp = 2;
95292 }else if( uu<=8388607 ){
95293 nData += 3;
95294 pRec->uTemp = 3;
95295 }else if( uu<=2147483647 ){
95296 nData += 4;
95297 pRec->uTemp = 4;
95298 }else if( uu<=140737488355327LL ){
95299 nData += 6;
95300 pRec->uTemp = 5;
95301 }else{
95302 nData += 8;
95303 if( pRec->flags & MEM_IntReal ){
95304 /* If the value is IntReal and is going to take up 8 bytes to store
95305 ** as an integer, then we might as well make it an 8-byte floating
95306 ** point value */
95307 pRec->u.r = (double)pRec->u.i;
95308 pRec->flags &= ~MEM_IntReal;
95309 pRec->flags |= MEM_Real;
95310 pRec->uTemp = 7;
95311 }else{
95312 pRec->uTemp = 6;
95313 }
95314 }
95315 }else if( pRec->flags & MEM_Real ){
95316 nHdr++;
95317 nData += 8;
95318 pRec->uTemp = 7;
95319 }else{
95320 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
95321 assert( pRec->n>=0 );
95322 len = (u32)pRec->n;
95323 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
95324 if( pRec->flags & MEM_Zero ){
95325 serial_type += pRec->u.nZero*2;
95326 if( nData ){
95327 if( sqlite3VdbeMemExpandBlob(pMem: pRec) ) goto no_mem;
95328 len += pRec->u.nZero;
95329 }else{
95330 nZero += pRec->u.nZero;
95331 }
95332 }
95333 nData += len;
95334 nHdr += sqlite3VarintLen(v: serial_type);
95335 pRec->uTemp = serial_type;
95336 }
95337 if( pRec==pData0 ) break;
95338 pRec--;
95339 }while(1);
95340
95341 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
95342 ** which determines the total number of bytes in the header. The varint
95343 ** value is the size of the header in bytes including the size varint
95344 ** itself. */
95345 testcase( nHdr==126 );
95346 testcase( nHdr==127 );
95347 if( nHdr<=126 ){
95348 /* The common case */
95349 nHdr += 1;
95350 }else{
95351 /* Rare case of a really large header */
95352 nVarint = sqlite3VarintLen(v: nHdr);
95353 nHdr += nVarint;
95354 if( nVarint<sqlite3VarintLen(v: nHdr) ) nHdr++;
95355 }
95356 nByte = nHdr+nData;
95357
95358 /* Make sure the output register has a buffer large enough to store
95359 ** the new record. The output register (pOp->p3) is not allowed to
95360 ** be one of the input registers (because the following call to
95361 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
95362 */
95363 if( nByte+nZero<=pOut->szMalloc ){
95364 /* The output register is already large enough to hold the record.
95365 ** No error checks or buffer enlargement is required */
95366 pOut->z = pOut->zMalloc;
95367 }else{
95368 /* Need to make sure that the output is not too big and then enlarge
95369 ** the output register to hold the full result */
95370 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
95371 goto too_big;
95372 }
95373 if( sqlite3VdbeMemClearAndResize(pMem: pOut, szNew: (int)nByte) ){
95374 goto no_mem;
95375 }
95376 }
95377 pOut->n = (int)nByte;
95378 pOut->flags = MEM_Blob;
95379 if( nZero ){
95380 pOut->u.nZero = nZero;
95381 pOut->flags |= MEM_Zero;
95382 }
95383 UPDATE_MAX_BLOBSIZE(pOut);
95384 zHdr = (u8 *)pOut->z;
95385 zPayload = zHdr + nHdr;
95386
95387 /* Write the record */
95388 if( nHdr<0x80 ){
95389 *(zHdr++) = nHdr;
95390 }else{
95391 zHdr += sqlite3PutVarint(p: zHdr,v: nHdr);
95392 }
95393 assert( pData0<=pLast );
95394 pRec = pData0;
95395 while( 1 /*exit-by-break*/ ){
95396 serial_type = pRec->uTemp;
95397 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
95398 ** additional varints, one per column.
95399 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
95400 ** immediately follow the header. */
95401 if( serial_type<=7 ){
95402 *(zHdr++) = serial_type;
95403 if( serial_type==0 ){
95404 /* NULL value. No change in zPayload */
95405 }else{
95406 u64 v;
95407 u32 i;
95408 if( serial_type==7 ){
95409 assert( sizeof(v)==sizeof(pRec->u.r) );
95410 memcpy(dest: &v, src: &pRec->u.r, n: sizeof(v));
95411 swapMixedEndianFloat(v);
95412 }else{
95413 v = pRec->u.i;
95414 }
95415 len = i = sqlite3SmallTypeSizes[serial_type];
95416 assert( i>0 );
95417 while( 1 /*exit-by-break*/ ){
95418 zPayload[--i] = (u8)(v&0xFF);
95419 if( i==0 ) break;
95420 v >>= 8;
95421 }
95422 zPayload += len;
95423 }
95424 }else if( serial_type<0x80 ){
95425 *(zHdr++) = serial_type;
95426 if( serial_type>=14 && pRec->n>0 ){
95427 assert( pRec->z!=0 );
95428 memcpy(dest: zPayload, src: pRec->z, n: pRec->n);
95429 zPayload += pRec->n;
95430 }
95431 }else{
95432 zHdr += sqlite3PutVarint(p: zHdr, v: serial_type);
95433 if( pRec->n ){
95434 assert( pRec->z!=0 );
95435 memcpy(dest: zPayload, src: pRec->z, n: pRec->n);
95436 zPayload += pRec->n;
95437 }
95438 }
95439 if( pRec==pLast ) break;
95440 pRec++;
95441 }
95442 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
95443 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
95444
95445 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
95446 REGISTER_TRACE(pOp->p3, pOut);
95447 break;
95448}
95449
95450/* Opcode: Count P1 P2 P3 * *
95451** Synopsis: r[P2]=count()
95452**
95453** Store the number of entries (an integer value) in the table or index
95454** opened by cursor P1 in register P2.
95455**
95456** If P3==0, then an exact count is obtained, which involves visiting
95457** every btree page of the table. But if P3 is non-zero, an estimate
95458** is returned based on the current cursor position.
95459*/
95460case OP_Count: { /* out2 */
95461 i64 nEntry;
95462 BtCursor *pCrsr;
95463
95464 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
95465 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
95466 assert( pCrsr );
95467 if( pOp->p3 ){
95468 nEntry = sqlite3BtreeRowCountEst(pCur: pCrsr);
95469 }else{
95470 nEntry = 0; /* Not needed. Only used to silence a warning. */
95471 rc = sqlite3BtreeCount(db, pCur: pCrsr, pnEntry: &nEntry);
95472 if( rc ) goto abort_due_to_error;
95473 }
95474 pOut = out2Prerelease(p, pOp);
95475 pOut->u.i = nEntry;
95476 goto check_for_interrupt;
95477}
95478
95479/* Opcode: Savepoint P1 * * P4 *
95480**
95481** Open, release or rollback the savepoint named by parameter P4, depending
95482** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
95483** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
95484** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
95485*/
95486case OP_Savepoint: {
95487 int p1; /* Value of P1 operand */
95488 char *zName; /* Name of savepoint */
95489 int nName;
95490 Savepoint *pNew;
95491 Savepoint *pSavepoint;
95492 Savepoint *pTmp;
95493 int iSavepoint;
95494 int ii;
95495
95496 p1 = pOp->p1;
95497 zName = pOp->p4.z;
95498
95499 /* Assert that the p1 parameter is valid. Also that if there is no open
95500 ** transaction, then there cannot be any savepoints.
95501 */
95502 assert( db->pSavepoint==0 || db->autoCommit==0 );
95503 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
95504 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
95505 assert( checkSavepointCount(db) );
95506 assert( p->bIsReader );
95507
95508 if( p1==SAVEPOINT_BEGIN ){
95509 if( db->nVdbeWrite>0 ){
95510 /* A new savepoint cannot be created if there are active write
95511 ** statements (i.e. open read/write incremental blob handles).
95512 */
95513 sqlite3VdbeError(p, zFormat: "cannot open savepoint - SQL statements in progress");
95514 rc = SQLITE_BUSY;
95515 }else{
95516 nName = sqlite3Strlen30(z: zName);
95517
95518#ifndef SQLITE_OMIT_VIRTUALTABLE
95519 /* This call is Ok even if this savepoint is actually a transaction
95520 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
95521 ** If this is a transaction savepoint being opened, it is guaranteed
95522 ** that the db->aVTrans[] array is empty. */
95523 assert( db->autoCommit==0 || db->nVTrans==0 );
95524 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
95525 db->nStatement+db->nSavepoint);
95526 if( rc!=SQLITE_OK ) goto abort_due_to_error;
95527#endif
95528
95529 /* Create a new savepoint structure. */
95530 pNew = sqlite3DbMallocRawNN(db, n: sizeof(Savepoint)+nName+1);
95531 if( pNew ){
95532 pNew->zName = (char *)&pNew[1];
95533 memcpy(dest: pNew->zName, src: zName, n: nName+1);
95534
95535 /* If there is no open transaction, then mark this as a special
95536 ** "transaction savepoint". */
95537 if( db->autoCommit ){
95538 db->autoCommit = 0;
95539 db->isTransactionSavepoint = 1;
95540 }else{
95541 db->nSavepoint++;
95542 }
95543
95544 /* Link the new savepoint into the database handle's list. */
95545 pNew->pNext = db->pSavepoint;
95546 db->pSavepoint = pNew;
95547 pNew->nDeferredCons = db->nDeferredCons;
95548 pNew->nDeferredImmCons = db->nDeferredImmCons;
95549 }
95550 }
95551 }else{
95552 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
95553 iSavepoint = 0;
95554
95555 /* Find the named savepoint. If there is no such savepoint, then an
95556 ** an error is returned to the user. */
95557 for(
95558 pSavepoint = db->pSavepoint;
95559 pSavepoint && sqlite3StrICmp(zLeft: pSavepoint->zName, zRight: zName);
95560 pSavepoint = pSavepoint->pNext
95561 ){
95562 iSavepoint++;
95563 }
95564 if( !pSavepoint ){
95565 sqlite3VdbeError(p, zFormat: "no such savepoint: %s", zName);
95566 rc = SQLITE_ERROR;
95567 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
95568 /* It is not possible to release (commit) a savepoint if there are
95569 ** active write statements.
95570 */
95571 sqlite3VdbeError(p, zFormat: "cannot release savepoint - "
95572 "SQL statements in progress");
95573 rc = SQLITE_BUSY;
95574 }else{
95575
95576 /* Determine whether or not this is a transaction savepoint. If so,
95577 ** and this is a RELEASE command, then the current transaction
95578 ** is committed.
95579 */
95580 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
95581 if( isTransaction && p1==SAVEPOINT_RELEASE ){
95582 if( (rc = sqlite3VdbeCheckFk(p, deferred: 1))!=SQLITE_OK ){
95583 goto vdbe_return;
95584 }
95585 db->autoCommit = 1;
95586 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
95587 p->pc = (int)(pOp - aOp);
95588 db->autoCommit = 0;
95589 p->rc = rc = SQLITE_BUSY;
95590 goto vdbe_return;
95591 }
95592 rc = p->rc;
95593 if( rc ){
95594 db->autoCommit = 0;
95595 }else{
95596 db->isTransactionSavepoint = 0;
95597 }
95598 }else{
95599 int isSchemaChange;
95600 iSavepoint = db->nSavepoint - iSavepoint - 1;
95601 if( p1==SAVEPOINT_ROLLBACK ){
95602 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
95603 for(ii=0; ii<db->nDb; ii++){
95604 rc = sqlite3BtreeTripAllCursors(pBtree: db->aDb[ii].pBt,
95605 SQLITE_ABORT_ROLLBACK,
95606 writeOnly: isSchemaChange==0);
95607 if( rc!=SQLITE_OK ) goto abort_due_to_error;
95608 }
95609 }else{
95610 assert( p1==SAVEPOINT_RELEASE );
95611 isSchemaChange = 0;
95612 }
95613 for(ii=0; ii<db->nDb; ii++){
95614 rc = sqlite3BtreeSavepoint(p: db->aDb[ii].pBt, op: p1, iSavepoint);
95615 if( rc!=SQLITE_OK ){
95616 goto abort_due_to_error;
95617 }
95618 }
95619 if( isSchemaChange ){
95620 sqlite3ExpirePreparedStatements(db, iCode: 0);
95621 sqlite3ResetAllSchemasOfConnection(db);
95622 db->mDbFlags |= DBFLAG_SchemaChange;
95623 }
95624 }
95625 if( rc ) goto abort_due_to_error;
95626
95627 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
95628 ** savepoints nested inside of the savepoint being operated on. */
95629 while( db->pSavepoint!=pSavepoint ){
95630 pTmp = db->pSavepoint;
95631 db->pSavepoint = pTmp->pNext;
95632 sqlite3DbFree(db, p: pTmp);
95633 db->nSavepoint--;
95634 }
95635
95636 /* If it is a RELEASE, then destroy the savepoint being operated on
95637 ** too. If it is a ROLLBACK TO, then set the number of deferred
95638 ** constraint violations present in the database to the value stored
95639 ** when the savepoint was created. */
95640 if( p1==SAVEPOINT_RELEASE ){
95641 assert( pSavepoint==db->pSavepoint );
95642 db->pSavepoint = pSavepoint->pNext;
95643 sqlite3DbFree(db, p: pSavepoint);
95644 if( !isTransaction ){
95645 db->nSavepoint--;
95646 }
95647 }else{
95648 assert( p1==SAVEPOINT_ROLLBACK );
95649 db->nDeferredCons = pSavepoint->nDeferredCons;
95650 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
95651 }
95652
95653 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
95654 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
95655 if( rc!=SQLITE_OK ) goto abort_due_to_error;
95656 }
95657 }
95658 }
95659 if( rc ) goto abort_due_to_error;
95660 if( p->eVdbeState==VDBE_HALT_STATE ){
95661 rc = SQLITE_DONE;
95662 goto vdbe_return;
95663 }
95664 break;
95665}
95666
95667/* Opcode: AutoCommit P1 P2 * * *
95668**
95669** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
95670** back any currently active btree transactions. If there are any active
95671** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
95672** there are active writing VMs or active VMs that use shared cache.
95673**
95674** This instruction causes the VM to halt.
95675*/
95676case OP_AutoCommit: {
95677 int desiredAutoCommit;
95678 int iRollback;
95679
95680 desiredAutoCommit = pOp->p1;
95681 iRollback = pOp->p2;
95682 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
95683 assert( desiredAutoCommit==1 || iRollback==0 );
95684 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
95685 assert( p->bIsReader );
95686
95687 if( desiredAutoCommit!=db->autoCommit ){
95688 if( iRollback ){
95689 assert( desiredAutoCommit==1 );
95690 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
95691 db->autoCommit = 1;
95692 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
95693 /* If this instruction implements a COMMIT and other VMs are writing
95694 ** return an error indicating that the other VMs must complete first.
95695 */
95696 sqlite3VdbeError(p, zFormat: "cannot commit transaction - "
95697 "SQL statements in progress");
95698 rc = SQLITE_BUSY;
95699 goto abort_due_to_error;
95700 }else if( (rc = sqlite3VdbeCheckFk(p, deferred: 1))!=SQLITE_OK ){
95701 goto vdbe_return;
95702 }else{
95703 db->autoCommit = (u8)desiredAutoCommit;
95704 }
95705 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
95706 p->pc = (int)(pOp - aOp);
95707 db->autoCommit = (u8)(1-desiredAutoCommit);
95708 p->rc = rc = SQLITE_BUSY;
95709 goto vdbe_return;
95710 }
95711 sqlite3CloseSavepoints(db);
95712 if( p->rc==SQLITE_OK ){
95713 rc = SQLITE_DONE;
95714 }else{
95715 rc = SQLITE_ERROR;
95716 }
95717 goto vdbe_return;
95718 }else{
95719 sqlite3VdbeError(p,
95720 zFormat: (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
95721 (iRollback)?"cannot rollback - no transaction is active":
95722 "cannot commit - no transaction is active"));
95723
95724 rc = SQLITE_ERROR;
95725 goto abort_due_to_error;
95726 }
95727 /*NOTREACHED*/ assert(0);
95728}
95729
95730/* Opcode: Transaction P1 P2 P3 P4 P5
95731**
95732** Begin a transaction on database P1 if a transaction is not already
95733** active.
95734** If P2 is non-zero, then a write-transaction is started, or if a
95735** read-transaction is already active, it is upgraded to a write-transaction.
95736** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
95737** then an exclusive transaction is started.
95738**
95739** P1 is the index of the database file on which the transaction is
95740** started. Index 0 is the main database file and index 1 is the
95741** file used for temporary tables. Indices of 2 or more are used for
95742** attached databases.
95743**
95744** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
95745** true (this flag is set if the Vdbe may modify more than one row and may
95746** throw an ABORT exception), a statement transaction may also be opened.
95747** More specifically, a statement transaction is opened iff the database
95748** connection is currently not in autocommit mode, or if there are other
95749** active statements. A statement transaction allows the changes made by this
95750** VDBE to be rolled back after an error without having to roll back the
95751** entire transaction. If no error is encountered, the statement transaction
95752** will automatically commit when the VDBE halts.
95753**
95754** If P5!=0 then this opcode also checks the schema cookie against P3
95755** and the schema generation counter against P4.
95756** The cookie changes its value whenever the database schema changes.
95757** This operation is used to detect when that the cookie has changed
95758** and that the current process needs to reread the schema. If the schema
95759** cookie in P3 differs from the schema cookie in the database header or
95760** if the schema generation counter in P4 differs from the current
95761** generation counter, then an SQLITE_SCHEMA error is raised and execution
95762** halts. The sqlite3_step() wrapper function might then reprepare the
95763** statement and rerun it from the beginning.
95764*/
95765case OP_Transaction: {
95766 Btree *pBt;
95767 Db *pDb;
95768 int iMeta = 0;
95769
95770 assert( p->bIsReader );
95771 assert( p->readOnly==0 || pOp->p2==0 );
95772 assert( pOp->p2>=0 && pOp->p2<=2 );
95773 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95774 assert( DbMaskTest(p->btreeMask, pOp->p1) );
95775 assert( rc==SQLITE_OK );
95776 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
95777 if( db->flags & SQLITE_QueryOnly ){
95778 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
95779 rc = SQLITE_READONLY;
95780 }else{
95781 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
95782 ** transaction */
95783 rc = SQLITE_CORRUPT;
95784 }
95785 goto abort_due_to_error;
95786 }
95787 pDb = &db->aDb[pOp->p1];
95788 pBt = pDb->pBt;
95789
95790 if( pBt ){
95791 rc = sqlite3BtreeBeginTrans(p: pBt, wrflag: pOp->p2, pSchemaVersion: &iMeta);
95792 testcase( rc==SQLITE_BUSY_SNAPSHOT );
95793 testcase( rc==SQLITE_BUSY_RECOVERY );
95794 if( rc!=SQLITE_OK ){
95795 if( (rc&0xff)==SQLITE_BUSY ){
95796 p->pc = (int)(pOp - aOp);
95797 p->rc = rc;
95798 goto vdbe_return;
95799 }
95800 goto abort_due_to_error;
95801 }
95802
95803 if( p->usesStmtJournal
95804 && pOp->p2
95805 && (db->autoCommit==0 || db->nVdbeRead>1)
95806 ){
95807 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE );
95808 if( p->iStatement==0 ){
95809 assert( db->nStatement>=0 && db->nSavepoint>=0 );
95810 db->nStatement++;
95811 p->iStatement = db->nSavepoint + db->nStatement;
95812 }
95813
95814 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
95815 if( rc==SQLITE_OK ){
95816 rc = sqlite3BtreeBeginStmt(p: pBt, iStatement: p->iStatement);
95817 }
95818
95819 /* Store the current value of the database handles deferred constraint
95820 ** counter. If the statement transaction needs to be rolled back,
95821 ** the value of this counter needs to be restored too. */
95822 p->nStmtDefCons = db->nDeferredCons;
95823 p->nStmtDefImmCons = db->nDeferredImmCons;
95824 }
95825 }
95826 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
95827 if( rc==SQLITE_OK
95828 && pOp->p5
95829 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
95830 ){
95831 /*
95832 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
95833 ** version is checked to ensure that the schema has not changed since the
95834 ** SQL statement was prepared.
95835 */
95836 sqlite3DbFree(db, p: p->zErrMsg);
95837 p->zErrMsg = sqlite3DbStrDup(db, z: "database schema has changed");
95838 /* If the schema-cookie from the database file matches the cookie
95839 ** stored with the in-memory representation of the schema, do
95840 ** not reload the schema from the database file.
95841 **
95842 ** If virtual-tables are in use, this is not just an optimization.
95843 ** Often, v-tables store their data in other SQLite tables, which
95844 ** are queried from within xNext() and other v-table methods using
95845 ** prepared queries. If such a query is out-of-date, we do not want to
95846 ** discard the database schema, as the user code implementing the
95847 ** v-table would have to be ready for the sqlite3_vtab structure itself
95848 ** to be invalidated whenever sqlite3_step() is called from within
95849 ** a v-table method.
95850 */
95851 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
95852 sqlite3ResetOneSchema(db, pOp->p1);
95853 }
95854 p->expired = 1;
95855 rc = SQLITE_SCHEMA;
95856
95857 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
95858 ** from being modified in sqlite3VdbeHalt(). If this statement is
95859 ** reprepared, changeCntOn will be set again. */
95860 p->changeCntOn = 0;
95861 }
95862 if( rc ) goto abort_due_to_error;
95863 break;
95864}
95865
95866/* Opcode: ReadCookie P1 P2 P3 * *
95867**
95868** Read cookie number P3 from database P1 and write it into register P2.
95869** P3==1 is the schema version. P3==2 is the database format.
95870** P3==3 is the recommended pager cache size, and so forth. P1==0 is
95871** the main database file and P1==1 is the database file used to store
95872** temporary tables.
95873**
95874** There must be a read-lock on the database (either a transaction
95875** must be started or there must be an open cursor) before
95876** executing this instruction.
95877*/
95878case OP_ReadCookie: { /* out2 */
95879 int iMeta;
95880 int iDb;
95881 int iCookie;
95882
95883 assert( p->bIsReader );
95884 iDb = pOp->p1;
95885 iCookie = pOp->p3;
95886 assert( pOp->p3<SQLITE_N_BTREE_META );
95887 assert( iDb>=0 && iDb<db->nDb );
95888 assert( db->aDb[iDb].pBt!=0 );
95889 assert( DbMaskTest(p->btreeMask, iDb) );
95890
95891 sqlite3BtreeGetMeta(p: db->aDb[iDb].pBt, idx: iCookie, pMeta: (u32 *)&iMeta);
95892 pOut = out2Prerelease(p, pOp);
95893 pOut->u.i = iMeta;
95894 break;
95895}
95896
95897/* Opcode: SetCookie P1 P2 P3 * P5
95898**
95899** Write the integer value P3 into cookie number P2 of database P1.
95900** P2==1 is the schema version. P2==2 is the database format.
95901** P2==3 is the recommended pager cache
95902** size, and so forth. P1==0 is the main database file and P1==1 is the
95903** database file used to store temporary tables.
95904**
95905** A transaction must be started before executing this opcode.
95906**
95907** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
95908** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
95909** has P5 set to 1, so that the internal schema version will be different
95910** from the database schema version, resulting in a schema reset.
95911*/
95912case OP_SetCookie: {
95913 Db *pDb;
95914
95915 sqlite3VdbeIncrWriteCounter(p, 0);
95916 assert( pOp->p2<SQLITE_N_BTREE_META );
95917 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95918 assert( DbMaskTest(p->btreeMask, pOp->p1) );
95919 assert( p->readOnly==0 );
95920 pDb = &db->aDb[pOp->p1];
95921 assert( pDb->pBt!=0 );
95922 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
95923 /* See note about index shifting on OP_ReadCookie */
95924 rc = sqlite3BtreeUpdateMeta(p: pDb->pBt, idx: pOp->p2, iMeta: pOp->p3);
95925 if( pOp->p2==BTREE_SCHEMA_VERSION ){
95926 /* When the schema cookie changes, record the new cookie internally */
95927 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
95928 db->mDbFlags |= DBFLAG_SchemaChange;
95929 sqlite3FkClearTriggerCache(db, pOp->p1);
95930 }else if( pOp->p2==BTREE_FILE_FORMAT ){
95931 /* Record changes in the file format */
95932 pDb->pSchema->file_format = pOp->p3;
95933 }
95934 if( pOp->p1==1 ){
95935 /* Invalidate all prepared statements whenever the TEMP database
95936 ** schema is changed. Ticket #1644 */
95937 sqlite3ExpirePreparedStatements(db, iCode: 0);
95938 p->expired = 0;
95939 }
95940 if( rc ) goto abort_due_to_error;
95941 break;
95942}
95943
95944/* Opcode: OpenRead P1 P2 P3 P4 P5
95945** Synopsis: root=P2 iDb=P3
95946**
95947** Open a read-only cursor for the database table whose root page is
95948** P2 in a database file. The database file is determined by P3.
95949** P3==0 means the main database, P3==1 means the database used for
95950** temporary tables, and P3>1 means used the corresponding attached
95951** database. Give the new cursor an identifier of P1. The P1
95952** values need not be contiguous but all P1 values should be small integers.
95953** It is an error for P1 to be negative.
95954**
95955** Allowed P5 bits:
95956** <ul>
95957** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
95958** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
95959** of OP_SeekLE/OP_IdxLT)
95960** </ul>
95961**
95962** The P4 value may be either an integer (P4_INT32) or a pointer to
95963** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
95964** object, then table being opened must be an [index b-tree] where the
95965** KeyInfo object defines the content and collating
95966** sequence of that index b-tree. Otherwise, if P4 is an integer
95967** value, then the table being opened must be a [table b-tree] with a
95968** number of columns no less than the value of P4.
95969**
95970** See also: OpenWrite, ReopenIdx
95971*/
95972/* Opcode: ReopenIdx P1 P2 P3 P4 P5
95973** Synopsis: root=P2 iDb=P3
95974**
95975** The ReopenIdx opcode works like OP_OpenRead except that it first
95976** checks to see if the cursor on P1 is already open on the same
95977** b-tree and if it is this opcode becomes a no-op. In other words,
95978** if the cursor is already open, do not reopen it.
95979**
95980** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
95981** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
95982** be the same as every other ReopenIdx or OpenRead for the same cursor
95983** number.
95984**
95985** Allowed P5 bits:
95986** <ul>
95987** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
95988** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
95989** of OP_SeekLE/OP_IdxLT)
95990** </ul>
95991**
95992** See also: OP_OpenRead, OP_OpenWrite
95993*/
95994/* Opcode: OpenWrite P1 P2 P3 P4 P5
95995** Synopsis: root=P2 iDb=P3
95996**
95997** Open a read/write cursor named P1 on the table or index whose root
95998** page is P2 (or whose root page is held in register P2 if the
95999** OPFLAG_P2ISREG bit is set in P5 - see below).
96000**
96001** The P4 value may be either an integer (P4_INT32) or a pointer to
96002** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
96003** object, then table being opened must be an [index b-tree] where the
96004** KeyInfo object defines the content and collating
96005** sequence of that index b-tree. Otherwise, if P4 is an integer
96006** value, then the table being opened must be a [table b-tree] with a
96007** number of columns no less than the value of P4.
96008**
96009** Allowed P5 bits:
96010** <ul>
96011** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
96012** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
96013** of OP_SeekLE/OP_IdxLT)
96014** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
96015** and subsequently delete entries in an index btree. This is a
96016** hint to the storage engine that the storage engine is allowed to
96017** ignore. The hint is not used by the official SQLite b*tree storage
96018** engine, but is used by COMDB2.
96019** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
96020** as the root page, not the value of P2 itself.
96021** </ul>
96022**
96023** This instruction works like OpenRead except that it opens the cursor
96024** in read/write mode.
96025**
96026** See also: OP_OpenRead, OP_ReopenIdx
96027*/
96028case OP_ReopenIdx: { /* ncycle */
96029 int nField;
96030 KeyInfo *pKeyInfo;
96031 u32 p2;
96032 int iDb;
96033 int wrFlag;
96034 Btree *pX;
96035 VdbeCursor *pCur;
96036 Db *pDb;
96037
96038 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
96039 assert( pOp->p4type==P4_KEYINFO );
96040 pCur = p->apCsr[pOp->p1];
96041 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
96042 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
96043 assert( pCur->eCurType==CURTYPE_BTREE );
96044 sqlite3BtreeClearCursor(pCur: pCur->uc.pCursor);
96045 goto open_cursor_set_hints;
96046 }
96047 /* If the cursor is not currently open or is open on a different
96048 ** index, then fall through into OP_OpenRead to force a reopen */
96049case OP_OpenRead: /* ncycle */
96050case OP_OpenWrite:
96051
96052 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
96053 assert( p->bIsReader );
96054 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
96055 || p->readOnly==0 );
96056
96057 if( p->expired==1 ){
96058 rc = SQLITE_ABORT_ROLLBACK;
96059 goto abort_due_to_error;
96060 }
96061
96062 nField = 0;
96063 pKeyInfo = 0;
96064 p2 = (u32)pOp->p2;
96065 iDb = pOp->p3;
96066 assert( iDb>=0 && iDb<db->nDb );
96067 assert( DbMaskTest(p->btreeMask, iDb) );
96068 pDb = &db->aDb[iDb];
96069 pX = pDb->pBt;
96070 assert( pX!=0 );
96071 if( pOp->opcode==OP_OpenWrite ){
96072 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
96073 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
96074 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
96075 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
96076 p->minWriteFileFormat = pDb->pSchema->file_format;
96077 }
96078 }else{
96079 wrFlag = 0;
96080 }
96081 if( pOp->p5 & OPFLAG_P2ISREG ){
96082 assert( p2>0 );
96083 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
96084 assert( pOp->opcode==OP_OpenWrite );
96085 pIn2 = &aMem[p2];
96086 assert( memIsValid(pIn2) );
96087 assert( (pIn2->flags & MEM_Int)!=0 );
96088 sqlite3VdbeMemIntegerify(pMem: pIn2);
96089 p2 = (int)pIn2->u.i;
96090 /* The p2 value always comes from a prior OP_CreateBtree opcode and
96091 ** that opcode will always set the p2 value to 2 or more or else fail.
96092 ** If there were a failure, the prepared statement would have halted
96093 ** before reaching this instruction. */
96094 assert( p2>=2 );
96095 }
96096 if( pOp->p4type==P4_KEYINFO ){
96097 pKeyInfo = pOp->p4.pKeyInfo;
96098 assert( pKeyInfo->enc==ENC(db) );
96099 assert( pKeyInfo->db==db );
96100 nField = pKeyInfo->nAllField;
96101 }else if( pOp->p4type==P4_INT32 ){
96102 nField = pOp->p4.i;
96103 }
96104 assert( pOp->p1>=0 );
96105 assert( nField>=0 );
96106 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
96107 pCur = allocateCursor(p, iCur: pOp->p1, nField, CURTYPE_BTREE);
96108 if( pCur==0 ) goto no_mem;
96109 pCur->iDb = iDb;
96110 pCur->nullRow = 1;
96111 pCur->isOrdered = 1;
96112 pCur->pgnoRoot = p2;
96113#ifdef SQLITE_DEBUG
96114 pCur->wrFlag = wrFlag;
96115#endif
96116 rc = sqlite3BtreeCursor(p: pX, iTable: p2, wrFlag, pKeyInfo, pCur: pCur->uc.pCursor);
96117 pCur->pKeyInfo = pKeyInfo;
96118 /* Set the VdbeCursor.isTable variable. Previous versions of
96119 ** SQLite used to check if the root-page flags were sane at this point
96120 ** and report database corruption if they were not, but this check has
96121 ** since moved into the btree layer. */
96122 pCur->isTable = pOp->p4type!=P4_KEYINFO;
96123
96124open_cursor_set_hints:
96125 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
96126 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
96127 testcase( pOp->p5 & OPFLAG_BULKCSR );
96128 testcase( pOp->p2 & OPFLAG_SEEKEQ );
96129 sqlite3BtreeCursorHintFlags(pCur: pCur->uc.pCursor,
96130 x: (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
96131 if( rc ) goto abort_due_to_error;
96132 break;
96133}
96134
96135/* Opcode: OpenDup P1 P2 * * *
96136**
96137** Open a new cursor P1 that points to the same ephemeral table as
96138** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
96139** opcode. Only ephemeral cursors may be duplicated.
96140**
96141** Duplicate ephemeral cursors are used for self-joins of materialized views.
96142*/
96143case OP_OpenDup: { /* ncycle */
96144 VdbeCursor *pOrig; /* The original cursor to be duplicated */
96145 VdbeCursor *pCx; /* The new cursor */
96146
96147 pOrig = p->apCsr[pOp->p2];
96148 assert( pOrig );
96149 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
96150
96151 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOrig->nField, CURTYPE_BTREE);
96152 if( pCx==0 ) goto no_mem;
96153 pCx->nullRow = 1;
96154 pCx->isEphemeral = 1;
96155 pCx->pKeyInfo = pOrig->pKeyInfo;
96156 pCx->isTable = pOrig->isTable;
96157 pCx->pgnoRoot = pOrig->pgnoRoot;
96158 pCx->isOrdered = pOrig->isOrdered;
96159 pCx->ub.pBtx = pOrig->ub.pBtx;
96160 pCx->noReuse = 1;
96161 pOrig->noReuse = 1;
96162 rc = sqlite3BtreeCursor(p: pCx->ub.pBtx, iTable: pCx->pgnoRoot, BTREE_WRCSR,
96163 pKeyInfo: pCx->pKeyInfo, pCur: pCx->uc.pCursor);
96164 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
96165 ** opened for a database. Since there is already an open cursor when this
96166 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
96167 assert( rc==SQLITE_OK );
96168 break;
96169}
96170
96171
96172/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
96173** Synopsis: nColumn=P2
96174**
96175** Open a new cursor P1 to a transient table.
96176** The cursor is always opened read/write even if
96177** the main database is read-only. The ephemeral
96178** table is deleted automatically when the cursor is closed.
96179**
96180** If the cursor P1 is already opened on an ephemeral table, the table
96181** is cleared (all content is erased).
96182**
96183** P2 is the number of columns in the ephemeral table.
96184** The cursor points to a BTree table if P4==0 and to a BTree index
96185** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
96186** that defines the format of keys in the index.
96187**
96188** The P5 parameter can be a mask of the BTREE_* flags defined
96189** in btree.h. These flags control aspects of the operation of
96190** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
96191** added automatically.
96192**
96193** If P3 is positive, then reg[P3] is modified slightly so that it
96194** can be used as zero-length data for OP_Insert. This is an optimization
96195** that avoids an extra OP_Blob opcode to initialize that register.
96196*/
96197/* Opcode: OpenAutoindex P1 P2 * P4 *
96198** Synopsis: nColumn=P2
96199**
96200** This opcode works the same as OP_OpenEphemeral. It has a
96201** different name to distinguish its use. Tables created using
96202** by this opcode will be used for automatically created transient
96203** indices in joins.
96204*/
96205case OP_OpenAutoindex: /* ncycle */
96206case OP_OpenEphemeral: { /* ncycle */
96207 VdbeCursor *pCx;
96208 KeyInfo *pKeyInfo;
96209
96210 static const int vfsFlags =
96211 SQLITE_OPEN_READWRITE |
96212 SQLITE_OPEN_CREATE |
96213 SQLITE_OPEN_EXCLUSIVE |
96214 SQLITE_OPEN_DELETEONCLOSE |
96215 SQLITE_OPEN_TRANSIENT_DB;
96216 assert( pOp->p1>=0 );
96217 assert( pOp->p2>=0 );
96218 if( pOp->p3>0 ){
96219 /* Make register reg[P3] into a value that can be used as the data
96220 ** form sqlite3BtreeInsert() where the length of the data is zero. */
96221 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
96222 assert( pOp->opcode==OP_OpenEphemeral );
96223 assert( aMem[pOp->p3].flags & MEM_Null );
96224 aMem[pOp->p3].n = 0;
96225 aMem[pOp->p3].z = "";
96226 }
96227 pCx = p->apCsr[pOp->p1];
96228 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
96229 /* If the ephemeral table is already open and has no duplicates from
96230 ** OP_OpenDup, then erase all existing content so that the table is
96231 ** empty again, rather than creating a new table. */
96232 assert( pCx->isEphemeral );
96233 pCx->seqCount = 0;
96234 pCx->cacheStatus = CACHE_STALE;
96235 rc = sqlite3BtreeClearTable(p: pCx->ub.pBtx, iTable: pCx->pgnoRoot, pnChange: 0);
96236 }else{
96237 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOp->p2, CURTYPE_BTREE);
96238 if( pCx==0 ) goto no_mem;
96239 pCx->isEphemeral = 1;
96240 rc = sqlite3BtreeOpen(pVfs: db->pVfs, zFilename: 0, db, ppBtree: &pCx->ub.pBtx,
96241 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
96242 vfsFlags);
96243 if( rc==SQLITE_OK ){
96244 rc = sqlite3BtreeBeginTrans(p: pCx->ub.pBtx, wrflag: 1, pSchemaVersion: 0);
96245 if( rc==SQLITE_OK ){
96246 /* If a transient index is required, create it by calling
96247 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
96248 ** opening it. If a transient table is required, just use the
96249 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
96250 */
96251 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
96252 assert( pOp->p4type==P4_KEYINFO );
96253 rc = sqlite3BtreeCreateTable(p: pCx->ub.pBtx, piTable: &pCx->pgnoRoot,
96254 BTREE_BLOBKEY | pOp->p5);
96255 if( rc==SQLITE_OK ){
96256 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
96257 assert( pKeyInfo->db==db );
96258 assert( pKeyInfo->enc==ENC(db) );
96259 rc = sqlite3BtreeCursor(p: pCx->ub.pBtx, iTable: pCx->pgnoRoot, BTREE_WRCSR,
96260 pKeyInfo, pCur: pCx->uc.pCursor);
96261 }
96262 pCx->isTable = 0;
96263 }else{
96264 pCx->pgnoRoot = SCHEMA_ROOT;
96265 rc = sqlite3BtreeCursor(p: pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
96266 pKeyInfo: 0, pCur: pCx->uc.pCursor);
96267 pCx->isTable = 1;
96268 }
96269 }
96270 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
96271 if( rc ){
96272 sqlite3BtreeClose(p: pCx->ub.pBtx);
96273 }
96274 }
96275 }
96276 if( rc ) goto abort_due_to_error;
96277 pCx->nullRow = 1;
96278 break;
96279}
96280
96281/* Opcode: SorterOpen P1 P2 P3 P4 *
96282**
96283** This opcode works like OP_OpenEphemeral except that it opens
96284** a transient index that is specifically designed to sort large
96285** tables using an external merge-sort algorithm.
96286**
96287** If argument P3 is non-zero, then it indicates that the sorter may
96288** assume that a stable sort considering the first P3 fields of each
96289** key is sufficient to produce the required results.
96290*/
96291case OP_SorterOpen: {
96292 VdbeCursor *pCx;
96293
96294 assert( pOp->p1>=0 );
96295 assert( pOp->p2>=0 );
96296 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOp->p2, CURTYPE_SORTER);
96297 if( pCx==0 ) goto no_mem;
96298 pCx->pKeyInfo = pOp->p4.pKeyInfo;
96299 assert( pCx->pKeyInfo->db==db );
96300 assert( pCx->pKeyInfo->enc==ENC(db) );
96301 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
96302 if( rc ) goto abort_due_to_error;
96303 break;
96304}
96305
96306/* Opcode: SequenceTest P1 P2 * * *
96307** Synopsis: if( cursor[P1].ctr++ ) pc = P2
96308**
96309** P1 is a sorter cursor. If the sequence counter is currently zero, jump
96310** to P2. Regardless of whether or not the jump is taken, increment the
96311** the sequence value.
96312*/
96313case OP_SequenceTest: {
96314 VdbeCursor *pC;
96315 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96316 pC = p->apCsr[pOp->p1];
96317 assert( isSorter(pC) );
96318 if( (pC->seqCount++)==0 ){
96319 goto jump_to_p2;
96320 }
96321 break;
96322}
96323
96324/* Opcode: OpenPseudo P1 P2 P3 * *
96325** Synopsis: P3 columns in r[P2]
96326**
96327** Open a new cursor that points to a fake table that contains a single
96328** row of data. The content of that one row is the content of memory
96329** register P2. In other words, cursor P1 becomes an alias for the
96330** MEM_Blob content contained in register P2.
96331**
96332** A pseudo-table created by this opcode is used to hold a single
96333** row output from the sorter so that the row can be decomposed into
96334** individual columns using the OP_Column opcode. The OP_Column opcode
96335** is the only cursor opcode that works with a pseudo-table.
96336**
96337** P3 is the number of fields in the records that will be stored by
96338** the pseudo-table.
96339*/
96340case OP_OpenPseudo: {
96341 VdbeCursor *pCx;
96342
96343 assert( pOp->p1>=0 );
96344 assert( pOp->p3>=0 );
96345 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOp->p3, CURTYPE_PSEUDO);
96346 if( pCx==0 ) goto no_mem;
96347 pCx->nullRow = 1;
96348 pCx->seekResult = pOp->p2;
96349 pCx->isTable = 1;
96350 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
96351 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
96352 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
96353 ** which is a performance optimization */
96354 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
96355 assert( pOp->p5==0 );
96356 break;
96357}
96358
96359/* Opcode: Close P1 * * * *
96360**
96361** Close a cursor previously opened as P1. If P1 is not
96362** currently open, this instruction is a no-op.
96363*/
96364case OP_Close: { /* ncycle */
96365 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96366 sqlite3VdbeFreeCursor(p, pCx: p->apCsr[pOp->p1]);
96367 p->apCsr[pOp->p1] = 0;
96368 break;
96369}
96370
96371#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
96372/* Opcode: ColumnsUsed P1 * * P4 *
96373**
96374** This opcode (which only exists if SQLite was compiled with
96375** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
96376** table or index for cursor P1 are used. P4 is a 64-bit integer
96377** (P4_INT64) in which the first 63 bits are one for each of the
96378** first 63 columns of the table or index that are actually used
96379** by the cursor. The high-order bit is set if any column after
96380** the 64th is used.
96381*/
96382case OP_ColumnsUsed: {
96383 VdbeCursor *pC;
96384 pC = p->apCsr[pOp->p1];
96385 assert( pC->eCurType==CURTYPE_BTREE );
96386 pC->maskUsed = *(u64*)pOp->p4.pI64;
96387 break;
96388}
96389#endif
96390
96391/* Opcode: SeekGE P1 P2 P3 P4 *
96392** Synopsis: key=r[P3@P4]
96393**
96394** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
96395** use the value in register P3 as the key. If cursor P1 refers
96396** to an SQL index, then P3 is the first in an array of P4 registers
96397** that are used as an unpacked index key.
96398**
96399** Reposition cursor P1 so that it points to the smallest entry that
96400** is greater than or equal to the key value. If there are no records
96401** greater than or equal to the key and P2 is not zero, then jump to P2.
96402**
96403** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
96404** opcode will either land on a record that exactly matches the key, or
96405** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
96406** this opcode must be followed by an IdxLE opcode with the same arguments.
96407** The IdxGT opcode will be skipped if this opcode succeeds, but the
96408** IdxGT opcode will be used on subsequent loop iterations. The
96409** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
96410** is an equality search.
96411**
96412** This opcode leaves the cursor configured to move in forward order,
96413** from the beginning toward the end. In other words, the cursor is
96414** configured to use Next, not Prev.
96415**
96416** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
96417*/
96418/* Opcode: SeekGT P1 P2 P3 P4 *
96419** Synopsis: key=r[P3@P4]
96420**
96421** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
96422** use the value in register P3 as a key. If cursor P1 refers
96423** to an SQL index, then P3 is the first in an array of P4 registers
96424** that are used as an unpacked index key.
96425**
96426** Reposition cursor P1 so that it points to the smallest entry that
96427** is greater than the key value. If there are no records greater than
96428** the key and P2 is not zero, then jump to P2.
96429**
96430** This opcode leaves the cursor configured to move in forward order,
96431** from the beginning toward the end. In other words, the cursor is
96432** configured to use Next, not Prev.
96433**
96434** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
96435*/
96436/* Opcode: SeekLT P1 P2 P3 P4 *
96437** Synopsis: key=r[P3@P4]
96438**
96439** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
96440** use the value in register P3 as a key. If cursor P1 refers
96441** to an SQL index, then P3 is the first in an array of P4 registers
96442** that are used as an unpacked index key.
96443**
96444** Reposition cursor P1 so that it points to the largest entry that
96445** is less than the key value. If there are no records less than
96446** the key and P2 is not zero, then jump to P2.
96447**
96448** This opcode leaves the cursor configured to move in reverse order,
96449** from the end toward the beginning. In other words, the cursor is
96450** configured to use Prev, not Next.
96451**
96452** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
96453*/
96454/* Opcode: SeekLE P1 P2 P3 P4 *
96455** Synopsis: key=r[P3@P4]
96456**
96457** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
96458** use the value in register P3 as a key. If cursor P1 refers
96459** to an SQL index, then P3 is the first in an array of P4 registers
96460** that are used as an unpacked index key.
96461**
96462** Reposition cursor P1 so that it points to the largest entry that
96463** is less than or equal to the key value. If there are no records
96464** less than or equal to the key and P2 is not zero, then jump to P2.
96465**
96466** This opcode leaves the cursor configured to move in reverse order,
96467** from the end toward the beginning. In other words, the cursor is
96468** configured to use Prev, not Next.
96469**
96470** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
96471** opcode will either land on a record that exactly matches the key, or
96472** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
96473** this opcode must be followed by an IdxLE opcode with the same arguments.
96474** The IdxGE opcode will be skipped if this opcode succeeds, but the
96475** IdxGE opcode will be used on subsequent loop iterations. The
96476** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
96477** is an equality search.
96478**
96479** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
96480*/
96481case OP_SeekLT: /* jump, in3, group, ncycle */
96482case OP_SeekLE: /* jump, in3, group, ncycle */
96483case OP_SeekGE: /* jump, in3, group, ncycle */
96484case OP_SeekGT: { /* jump, in3, group, ncycle */
96485 int res; /* Comparison result */
96486 int oc; /* Opcode */
96487 VdbeCursor *pC; /* The cursor to seek */
96488 UnpackedRecord r; /* The key to seek for */
96489 int nField; /* Number of columns or fields in the key */
96490 i64 iKey; /* The rowid we are to seek to */
96491 int eqOnly; /* Only interested in == results */
96492
96493 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96494 assert( pOp->p2!=0 );
96495 pC = p->apCsr[pOp->p1];
96496 assert( pC!=0 );
96497 assert( pC->eCurType==CURTYPE_BTREE );
96498 assert( OP_SeekLE == OP_SeekLT+1 );
96499 assert( OP_SeekGE == OP_SeekLT+2 );
96500 assert( OP_SeekGT == OP_SeekLT+3 );
96501 assert( pC->isOrdered );
96502 assert( pC->uc.pCursor!=0 );
96503 oc = pOp->opcode;
96504 eqOnly = 0;
96505 pC->nullRow = 0;
96506#ifdef SQLITE_DEBUG
96507 pC->seekOp = pOp->opcode;
96508#endif
96509
96510 pC->deferredMoveto = 0;
96511 pC->cacheStatus = CACHE_STALE;
96512 if( pC->isTable ){
96513 u16 flags3, newType;
96514 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
96515 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
96516 || CORRUPT_DB );
96517
96518 /* The input value in P3 might be of any type: integer, real, string,
96519 ** blob, or NULL. But it needs to be an integer before we can do
96520 ** the seek, so convert it. */
96521 pIn3 = &aMem[pOp->p3];
96522 flags3 = pIn3->flags;
96523 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
96524 applyNumericAffinity(pRec: pIn3, bTryForInt: 0);
96525 }
96526 iKey = sqlite3VdbeIntValue(pMem: pIn3); /* Get the integer key value */
96527 newType = pIn3->flags; /* Record the type after applying numeric affinity */
96528 pIn3->flags = flags3; /* But convert the type back to its original */
96529
96530 /* If the P3 value could not be converted into an integer without
96531 ** loss of information, then special processing is required... */
96532 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
96533 int c;
96534 if( (newType & MEM_Real)==0 ){
96535 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
96536 VdbeBranchTaken(1,2);
96537 goto jump_to_p2;
96538 }else{
96539 rc = sqlite3BtreeLast(pCur: pC->uc.pCursor, pRes: &res);
96540 if( rc!=SQLITE_OK ) goto abort_due_to_error;
96541 goto seek_not_found;
96542 }
96543 }
96544 c = sqlite3IntFloatCompare(i: iKey, r: pIn3->u.r);
96545
96546 /* If the approximation iKey is larger than the actual real search
96547 ** term, substitute >= for > and < for <=. e.g. if the search term
96548 ** is 4.9 and the integer approximation 5:
96549 **
96550 ** (x > 4.9) -> (x >= 5)
96551 ** (x <= 4.9) -> (x < 5)
96552 */
96553 if( c>0 ){
96554 assert( OP_SeekGE==(OP_SeekGT-1) );
96555 assert( OP_SeekLT==(OP_SeekLE-1) );
96556 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
96557 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
96558 }
96559
96560 /* If the approximation iKey is smaller than the actual real search
96561 ** term, substitute <= for < and > for >=. */
96562 else if( c<0 ){
96563 assert( OP_SeekLE==(OP_SeekLT+1) );
96564 assert( OP_SeekGT==(OP_SeekGE+1) );
96565 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
96566 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
96567 }
96568 }
96569 rc = sqlite3BtreeTableMoveto(pCur: pC->uc.pCursor, intKey: (u64)iKey, biasRight: 0, pRes: &res);
96570 pC->movetoTarget = iKey; /* Used by OP_Delete */
96571 if( rc!=SQLITE_OK ){
96572 goto abort_due_to_error;
96573 }
96574 }else{
96575 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
96576 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
96577 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
96578 ** with the same key.
96579 */
96580 if( sqlite3BtreeCursorHasHint(pCsr: pC->uc.pCursor, BTREE_SEEK_EQ) ){
96581 eqOnly = 1;
96582 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
96583 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
96584 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
96585 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
96586 assert( pOp[1].p1==pOp[0].p1 );
96587 assert( pOp[1].p2==pOp[0].p2 );
96588 assert( pOp[1].p3==pOp[0].p3 );
96589 assert( pOp[1].p4.i==pOp[0].p4.i );
96590 }
96591
96592 nField = pOp->p4.i;
96593 assert( pOp->p4type==P4_INT32 );
96594 assert( nField>0 );
96595 r.pKeyInfo = pC->pKeyInfo;
96596 r.nField = (u16)nField;
96597
96598 /* The next line of code computes as follows, only faster:
96599 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
96600 ** r.default_rc = -1;
96601 ** }else{
96602 ** r.default_rc = +1;
96603 ** }
96604 */
96605 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
96606 assert( oc!=OP_SeekGT || r.default_rc==-1 );
96607 assert( oc!=OP_SeekLE || r.default_rc==-1 );
96608 assert( oc!=OP_SeekGE || r.default_rc==+1 );
96609 assert( oc!=OP_SeekLT || r.default_rc==+1 );
96610
96611 r.aMem = &aMem[pOp->p3];
96612#ifdef SQLITE_DEBUG
96613 {
96614 int i;
96615 for(i=0; i<r.nField; i++){
96616 assert( memIsValid(&r.aMem[i]) );
96617 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
96618 }
96619 }
96620#endif
96621 r.eqSeen = 0;
96622 rc = sqlite3BtreeIndexMoveto(pCur: pC->uc.pCursor, pIdxKey: &r, pRes: &res);
96623 if( rc!=SQLITE_OK ){
96624 goto abort_due_to_error;
96625 }
96626 if( eqOnly && r.eqSeen==0 ){
96627 assert( res!=0 );
96628 goto seek_not_found;
96629 }
96630 }
96631#ifdef SQLITE_TEST
96632 sqlite3_search_count++;
96633#endif
96634 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
96635 if( res<0 || (res==0 && oc==OP_SeekGT) ){
96636 res = 0;
96637 rc = sqlite3BtreeNext(pCur: pC->uc.pCursor, flags: 0);
96638 if( rc!=SQLITE_OK ){
96639 if( rc==SQLITE_DONE ){
96640 rc = SQLITE_OK;
96641 res = 1;
96642 }else{
96643 goto abort_due_to_error;
96644 }
96645 }
96646 }else{
96647 res = 0;
96648 }
96649 }else{
96650 assert( oc==OP_SeekLT || oc==OP_SeekLE );
96651 if( res>0 || (res==0 && oc==OP_SeekLT) ){
96652 res = 0;
96653 rc = sqlite3BtreePrevious(pCur: pC->uc.pCursor, flags: 0);
96654 if( rc!=SQLITE_OK ){
96655 if( rc==SQLITE_DONE ){
96656 rc = SQLITE_OK;
96657 res = 1;
96658 }else{
96659 goto abort_due_to_error;
96660 }
96661 }
96662 }else{
96663 /* res might be negative because the table is empty. Check to
96664 ** see if this is the case.
96665 */
96666 res = sqlite3BtreeEof(pCur: pC->uc.pCursor);
96667 }
96668 }
96669seek_not_found:
96670 assert( pOp->p2>0 );
96671 VdbeBranchTaken(res!=0,2);
96672 if( res ){
96673 goto jump_to_p2;
96674 }else if( eqOnly ){
96675 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
96676 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
96677 }
96678 break;
96679}
96680
96681
96682/* Opcode: SeekScan P1 P2 * * P5
96683** Synopsis: Scan-ahead up to P1 rows
96684**
96685** This opcode is a prefix opcode to OP_SeekGE. In other words, this
96686** opcode must be immediately followed by OP_SeekGE. This constraint is
96687** checked by assert() statements.
96688**
96689** This opcode uses the P1 through P4 operands of the subsequent
96690** OP_SeekGE. In the text that follows, the operands of the subsequent
96691** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
96692** the P1, P2 and P5 operands of this opcode are also used, and are called
96693** This.P1, This.P2 and This.P5.
96694**
96695** This opcode helps to optimize IN operators on a multi-column index
96696** where the IN operator is on the later terms of the index by avoiding
96697** unnecessary seeks on the btree, substituting steps to the next row
96698** of the b-tree instead. A correct answer is obtained if this opcode
96699** is omitted or is a no-op.
96700**
96701** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
96702** is the desired entry that we want the cursor SeekGE.P1 to be pointing
96703** to. Call this SeekGE.P3/P4 row the "target".
96704**
96705** If the SeekGE.P1 cursor is not currently pointing to a valid row,
96706** then this opcode is a no-op and control passes through into the OP_SeekGE.
96707**
96708** If the SeekGE.P1 cursor is pointing to a valid row, then that row
96709** might be the target row, or it might be near and slightly before the
96710** target row, or it might be after the target row. If the cursor is
96711** currently before the target row, then this opcode attempts to position
96712** the cursor on or after the target row by invoking sqlite3BtreeStep()
96713** on the cursor between 1 and This.P1 times.
96714**
96715** The This.P5 parameter is a flag that indicates what to do if the
96716** cursor ends up pointing at a valid row that is past the target
96717** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
96718** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
96719** case occurs when there are no inequality constraints to the right of
96720** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case
96721** occurs when there are inequality constraints to the right of the IN
96722** operator. In that case, the This.P2 will point either directly to or
96723** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
96724** loop terminate.
96725**
96726** Possible outcomes from this opcode:<ol>
96727**
96728** <li> If the cursor is initially not pointed to any valid row, then
96729** fall through into the subsequent OP_SeekGE opcode.
96730**
96731** <li> If the cursor is left pointing to a row that is before the target
96732** row, even after making as many as This.P1 calls to
96733** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
96734**
96735** <li> If the cursor is left pointing at the target row, either because it
96736** was at the target row to begin with or because one or more
96737** sqlite3BtreeNext() calls moved the cursor to the target row,
96738** then jump to This.P2..,
96739**
96740** <li> If the cursor started out before the target row and a call to
96741** to sqlite3BtreeNext() moved the cursor off the end of the index
96742** (indicating that the target row definitely does not exist in the
96743** btree) then jump to SeekGE.P2, ending the loop.
96744**
96745** <li> If the cursor ends up on a valid row that is past the target row
96746** (indicating that the target row does not exist in the btree) then
96747** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
96748** </ol>
96749*/
96750case OP_SeekScan: { /* ncycle */
96751 VdbeCursor *pC;
96752 int res;
96753 int nStep;
96754 UnpackedRecord r;
96755
96756 assert( pOp[1].opcode==OP_SeekGE );
96757
96758 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
96759 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
96760 ** opcode past the OP_SeekGE itself. */
96761 assert( pOp->p2>=(int)(pOp-aOp)+2 );
96762#ifdef SQLITE_DEBUG
96763 if( pOp->p5==0 ){
96764 /* There are no inequality constraints following the IN constraint. */
96765 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
96766 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
96767 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
96768 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
96769 || aOp[pOp->p2-1].opcode==OP_IdxGE );
96770 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
96771 }else{
96772 /* There are inequality constraints. */
96773 assert( pOp->p2==(int)(pOp-aOp)+2 );
96774 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
96775 }
96776#endif
96777
96778 assert( pOp->p1>0 );
96779 pC = p->apCsr[pOp[1].p1];
96780 assert( pC!=0 );
96781 assert( pC->eCurType==CURTYPE_BTREE );
96782 assert( !pC->isTable );
96783 if( !sqlite3BtreeCursorIsValidNN(pCur: pC->uc.pCursor) ){
96784#ifdef SQLITE_DEBUG
96785 if( db->flags&SQLITE_VdbeTrace ){
96786 printf("... cursor not valid - fall through\n");
96787 }
96788#endif
96789 break;
96790 }
96791 nStep = pOp->p1;
96792 assert( nStep>=1 );
96793 r.pKeyInfo = pC->pKeyInfo;
96794 r.nField = (u16)pOp[1].p4.i;
96795 r.default_rc = 0;
96796 r.aMem = &aMem[pOp[1].p3];
96797#ifdef SQLITE_DEBUG
96798 {
96799 int i;
96800 for(i=0; i<r.nField; i++){
96801 assert( memIsValid(&r.aMem[i]) );
96802 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
96803 }
96804 }
96805#endif
96806 res = 0; /* Not needed. Only used to silence a warning. */
96807 while(1){
96808 rc = sqlite3VdbeIdxKeyCompare(db, pC, pUnpacked: &r, res: &res);
96809 if( rc ) goto abort_due_to_error;
96810 if( res>0 && pOp->p5==0 ){
96811 seekscan_search_fail:
96812 /* Jump to SeekGE.P2, ending the loop */
96813#ifdef SQLITE_DEBUG
96814 if( db->flags&SQLITE_VdbeTrace ){
96815 printf("... %d steps and then skip\n", pOp->p1 - nStep);
96816 }
96817#endif
96818 VdbeBranchTaken(1,3);
96819 pOp++;
96820 goto jump_to_p2;
96821 }
96822 if( res>=0 ){
96823 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
96824#ifdef SQLITE_DEBUG
96825 if( db->flags&SQLITE_VdbeTrace ){
96826 printf("... %d steps and then success\n", pOp->p1 - nStep);
96827 }
96828#endif
96829 VdbeBranchTaken(2,3);
96830 goto jump_to_p2;
96831 break;
96832 }
96833 if( nStep<=0 ){
96834#ifdef SQLITE_DEBUG
96835 if( db->flags&SQLITE_VdbeTrace ){
96836 printf("... fall through after %d steps\n", pOp->p1);
96837 }
96838#endif
96839 VdbeBranchTaken(0,3);
96840 break;
96841 }
96842 nStep--;
96843 pC->cacheStatus = CACHE_STALE;
96844 rc = sqlite3BtreeNext(pCur: pC->uc.pCursor, flags: 0);
96845 if( rc ){
96846 if( rc==SQLITE_DONE ){
96847 rc = SQLITE_OK;
96848 goto seekscan_search_fail;
96849 }else{
96850 goto abort_due_to_error;
96851 }
96852 }
96853 }
96854
96855 break;
96856}
96857
96858
96859/* Opcode: SeekHit P1 P2 P3 * *
96860** Synopsis: set P2<=seekHit<=P3
96861**
96862** Increase or decrease the seekHit value for cursor P1, if necessary,
96863** so that it is no less than P2 and no greater than P3.
96864**
96865** The seekHit integer represents the maximum of terms in an index for which
96866** there is known to be at least one match. If the seekHit value is smaller
96867** than the total number of equality terms in an index lookup, then the
96868** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
96869** early, thus saving work. This is part of the IN-early-out optimization.
96870**
96871** P1 must be a valid b-tree cursor.
96872*/
96873case OP_SeekHit: { /* ncycle */
96874 VdbeCursor *pC;
96875 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96876 pC = p->apCsr[pOp->p1];
96877 assert( pC!=0 );
96878 assert( pOp->p3>=pOp->p2 );
96879 if( pC->seekHit<pOp->p2 ){
96880#ifdef SQLITE_DEBUG
96881 if( db->flags&SQLITE_VdbeTrace ){
96882 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
96883 }
96884#endif
96885 pC->seekHit = pOp->p2;
96886 }else if( pC->seekHit>pOp->p3 ){
96887#ifdef SQLITE_DEBUG
96888 if( db->flags&SQLITE_VdbeTrace ){
96889 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
96890 }
96891#endif
96892 pC->seekHit = pOp->p3;
96893 }
96894 break;
96895}
96896
96897/* Opcode: IfNotOpen P1 P2 * * *
96898** Synopsis: if( !csr[P1] ) goto P2
96899**
96900** If cursor P1 is not open or if P1 is set to a NULL row using the
96901** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
96902*/
96903case OP_IfNotOpen: { /* jump */
96904 VdbeCursor *pCur;
96905
96906 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96907 pCur = p->apCsr[pOp->p1];
96908 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
96909 if( pCur==0 || pCur->nullRow ){
96910 goto jump_to_p2_and_check_for_interrupt;
96911 }
96912 break;
96913}
96914
96915/* Opcode: Found P1 P2 P3 P4 *
96916** Synopsis: key=r[P3@P4]
96917**
96918** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
96919** P4>0 then register P3 is the first of P4 registers that form an unpacked
96920** record.
96921**
96922** Cursor P1 is on an index btree. If the record identified by P3 and P4
96923** is a prefix of any entry in P1 then a jump is made to P2 and
96924** P1 is left pointing at the matching entry.
96925**
96926** This operation leaves the cursor in a state where it can be
96927** advanced in the forward direction. The Next instruction will work,
96928** but not the Prev instruction.
96929**
96930** See also: NotFound, NoConflict, NotExists. SeekGe
96931*/
96932/* Opcode: NotFound P1 P2 P3 P4 *
96933** Synopsis: key=r[P3@P4]
96934**
96935** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
96936** P4>0 then register P3 is the first of P4 registers that form an unpacked
96937** record.
96938**
96939** Cursor P1 is on an index btree. If the record identified by P3 and P4
96940** is not the prefix of any entry in P1 then a jump is made to P2. If P1
96941** does contain an entry whose prefix matches the P3/P4 record then control
96942** falls through to the next instruction and P1 is left pointing at the
96943** matching entry.
96944**
96945** This operation leaves the cursor in a state where it cannot be
96946** advanced in either direction. In other words, the Next and Prev
96947** opcodes do not work after this operation.
96948**
96949** See also: Found, NotExists, NoConflict, IfNoHope
96950*/
96951/* Opcode: IfNoHope P1 P2 P3 P4 *
96952** Synopsis: key=r[P3@P4]
96953**
96954** Register P3 is the first of P4 registers that form an unpacked
96955** record. Cursor P1 is an index btree. P2 is a jump destination.
96956** In other words, the operands to this opcode are the same as the
96957** operands to OP_NotFound and OP_IdxGT.
96958**
96959** This opcode is an optimization attempt only. If this opcode always
96960** falls through, the correct answer is still obtained, but extra work
96961** is performed.
96962**
96963** A value of N in the seekHit flag of cursor P1 means that there exists
96964** a key P3:N that will match some record in the index. We want to know
96965** if it is possible for a record P3:P4 to match some record in the
96966** index. If it is not possible, we can skip some work. So if seekHit
96967** is less than P4, attempt to find out if a match is possible by running
96968** OP_NotFound.
96969**
96970** This opcode is used in IN clause processing for a multi-column key.
96971** If an IN clause is attached to an element of the key other than the
96972** left-most element, and if there are no matches on the most recent
96973** seek over the whole key, then it might be that one of the key element
96974** to the left is prohibiting a match, and hence there is "no hope" of
96975** any match regardless of how many IN clause elements are checked.
96976** In such a case, we abandon the IN clause search early, using this
96977** opcode. The opcode name comes from the fact that the
96978** jump is taken if there is "no hope" of achieving a match.
96979**
96980** See also: NotFound, SeekHit
96981*/
96982/* Opcode: NoConflict P1 P2 P3 P4 *
96983** Synopsis: key=r[P3@P4]
96984**
96985** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
96986** P4>0 then register P3 is the first of P4 registers that form an unpacked
96987** record.
96988**
96989** Cursor P1 is on an index btree. If the record identified by P3 and P4
96990** contains any NULL value, jump immediately to P2. If all terms of the
96991** record are not-NULL then a check is done to determine if any row in the
96992** P1 index btree has a matching key prefix. If there are no matches, jump
96993** immediately to P2. If there is a match, fall through and leave the P1
96994** cursor pointing to the matching row.
96995**
96996** This opcode is similar to OP_NotFound with the exceptions that the
96997** branch is always taken if any part of the search key input is NULL.
96998**
96999** This operation leaves the cursor in a state where it cannot be
97000** advanced in either direction. In other words, the Next and Prev
97001** opcodes do not work after this operation.
97002**
97003** See also: NotFound, Found, NotExists
97004*/
97005case OP_IfNoHope: { /* jump, in3, ncycle */
97006 VdbeCursor *pC;
97007 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97008 pC = p->apCsr[pOp->p1];
97009 assert( pC!=0 );
97010#ifdef SQLITE_DEBUG
97011 if( db->flags&SQLITE_VdbeTrace ){
97012 printf("seekHit is %d\n", pC->seekHit);
97013 }
97014#endif
97015 if( pC->seekHit>=pOp->p4.i ) break;
97016 /* Fall through into OP_NotFound */
97017 /* no break */ deliberate_fall_through
97018}
97019case OP_NoConflict: /* jump, in3, ncycle */
97020case OP_NotFound: /* jump, in3, ncycle */
97021case OP_Found: { /* jump, in3, ncycle */
97022 int alreadyExists;
97023 int ii;
97024 VdbeCursor *pC;
97025 UnpackedRecord *pIdxKey;
97026 UnpackedRecord r;
97027
97028#ifdef SQLITE_TEST
97029 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
97030#endif
97031
97032 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97033 assert( pOp->p4type==P4_INT32 );
97034 pC = p->apCsr[pOp->p1];
97035 assert( pC!=0 );
97036#ifdef SQLITE_DEBUG
97037 pC->seekOp = pOp->opcode;
97038#endif
97039 r.aMem = &aMem[pOp->p3];
97040 assert( pC->eCurType==CURTYPE_BTREE );
97041 assert( pC->uc.pCursor!=0 );
97042 assert( pC->isTable==0 );
97043 r.nField = (u16)pOp->p4.i;
97044 if( r.nField>0 ){
97045 /* Key values in an array of registers */
97046 r.pKeyInfo = pC->pKeyInfo;
97047 r.default_rc = 0;
97048#ifdef SQLITE_DEBUG
97049 for(ii=0; ii<r.nField; ii++){
97050 assert( memIsValid(&r.aMem[ii]) );
97051 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
97052 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
97053 }
97054#endif
97055 rc = sqlite3BtreeIndexMoveto(pCur: pC->uc.pCursor, pIdxKey: &r, pRes: &pC->seekResult);
97056 }else{
97057 /* Composite key generated by OP_MakeRecord */
97058 assert( r.aMem->flags & MEM_Blob );
97059 assert( pOp->opcode!=OP_NoConflict );
97060 rc = ExpandBlob(r.aMem);
97061 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
97062 if( rc ) goto no_mem;
97063 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo: pC->pKeyInfo);
97064 if( pIdxKey==0 ) goto no_mem;
97065 sqlite3VdbeRecordUnpack(pKeyInfo: pC->pKeyInfo, nKey: r.aMem->n, pKey: r.aMem->z, p: pIdxKey);
97066 pIdxKey->default_rc = 0;
97067 rc = sqlite3BtreeIndexMoveto(pCur: pC->uc.pCursor, pIdxKey, pRes: &pC->seekResult);
97068 sqlite3DbFreeNN(db, p: pIdxKey);
97069 }
97070 if( rc!=SQLITE_OK ){
97071 goto abort_due_to_error;
97072 }
97073 alreadyExists = (pC->seekResult==0);
97074 pC->nullRow = 1-alreadyExists;
97075 pC->deferredMoveto = 0;
97076 pC->cacheStatus = CACHE_STALE;
97077 if( pOp->opcode==OP_Found ){
97078 VdbeBranchTaken(alreadyExists!=0,2);
97079 if( alreadyExists ) goto jump_to_p2;
97080 }else{
97081 if( !alreadyExists ){
97082 VdbeBranchTaken(1,2);
97083 goto jump_to_p2;
97084 }
97085 if( pOp->opcode==OP_NoConflict ){
97086 /* For the OP_NoConflict opcode, take the jump if any of the
97087 ** input fields are NULL, since any key with a NULL will not
97088 ** conflict */
97089 for(ii=0; ii<r.nField; ii++){
97090 if( r.aMem[ii].flags & MEM_Null ){
97091 VdbeBranchTaken(1,2);
97092 goto jump_to_p2;
97093 }
97094 }
97095 }
97096 VdbeBranchTaken(0,2);
97097 if( pOp->opcode==OP_IfNoHope ){
97098 pC->seekHit = pOp->p4.i;
97099 }
97100 }
97101 break;
97102}
97103
97104/* Opcode: SeekRowid P1 P2 P3 * *
97105** Synopsis: intkey=r[P3]
97106**
97107** P1 is the index of a cursor open on an SQL table btree (with integer
97108** keys). If register P3 does not contain an integer or if P1 does not
97109** contain a record with rowid P3 then jump immediately to P2.
97110** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
97111** a record with rowid P3 then
97112** leave the cursor pointing at that record and fall through to the next
97113** instruction.
97114**
97115** The OP_NotExists opcode performs the same operation, but with OP_NotExists
97116** the P3 register must be guaranteed to contain an integer value. With this
97117** opcode, register P3 might not contain an integer.
97118**
97119** The OP_NotFound opcode performs the same operation on index btrees
97120** (with arbitrary multi-value keys).
97121**
97122** This opcode leaves the cursor in a state where it cannot be advanced
97123** in either direction. In other words, the Next and Prev opcodes will
97124** not work following this opcode.
97125**
97126** See also: Found, NotFound, NoConflict, SeekRowid
97127*/
97128/* Opcode: NotExists P1 P2 P3 * *
97129** Synopsis: intkey=r[P3]
97130**
97131** P1 is the index of a cursor open on an SQL table btree (with integer
97132** keys). P3 is an integer rowid. If P1 does not contain a record with
97133** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
97134** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
97135** leave the cursor pointing at that record and fall through to the next
97136** instruction.
97137**
97138** The OP_SeekRowid opcode performs the same operation but also allows the
97139** P3 register to contain a non-integer value, in which case the jump is
97140** always taken. This opcode requires that P3 always contain an integer.
97141**
97142** The OP_NotFound opcode performs the same operation on index btrees
97143** (with arbitrary multi-value keys).
97144**
97145** This opcode leaves the cursor in a state where it cannot be advanced
97146** in either direction. In other words, the Next and Prev opcodes will
97147** not work following this opcode.
97148**
97149** See also: Found, NotFound, NoConflict, SeekRowid
97150*/
97151case OP_SeekRowid: { /* jump, in3, ncycle */
97152 VdbeCursor *pC;
97153 BtCursor *pCrsr;
97154 int res;
97155 u64 iKey;
97156
97157 pIn3 = &aMem[pOp->p3];
97158 testcase( pIn3->flags & MEM_Int );
97159 testcase( pIn3->flags & MEM_IntReal );
97160 testcase( pIn3->flags & MEM_Real );
97161 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
97162 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
97163 /* If pIn3->u.i does not contain an integer, compute iKey as the
97164 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
97165 ** into an integer without loss of information. Take care to avoid
97166 ** changing the datatype of pIn3, however, as it is used by other
97167 ** parts of the prepared statement. */
97168 Mem x = pIn3[0];
97169 applyAffinity(pRec: &x, SQLITE_AFF_NUMERIC, enc: encoding);
97170 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
97171 iKey = x.u.i;
97172 goto notExistsWithKey;
97173 }
97174 /* Fall through into OP_NotExists */
97175 /* no break */ deliberate_fall_through
97176case OP_NotExists: /* jump, in3, ncycle */
97177 pIn3 = &aMem[pOp->p3];
97178 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
97179 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97180 iKey = pIn3->u.i;
97181notExistsWithKey:
97182 pC = p->apCsr[pOp->p1];
97183 assert( pC!=0 );
97184#ifdef SQLITE_DEBUG
97185 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
97186#endif
97187 assert( pC->isTable );
97188 assert( pC->eCurType==CURTYPE_BTREE );
97189 pCrsr = pC->uc.pCursor;
97190 assert( pCrsr!=0 );
97191 res = 0;
97192 rc = sqlite3BtreeTableMoveto(pCur: pCrsr, intKey: iKey, biasRight: 0, pRes: &res);
97193 assert( rc==SQLITE_OK || res==0 );
97194 pC->movetoTarget = iKey; /* Used by OP_Delete */
97195 pC->nullRow = 0;
97196 pC->cacheStatus = CACHE_STALE;
97197 pC->deferredMoveto = 0;
97198 VdbeBranchTaken(res!=0,2);
97199 pC->seekResult = res;
97200 if( res!=0 ){
97201 assert( rc==SQLITE_OK );
97202 if( pOp->p2==0 ){
97203 rc = SQLITE_CORRUPT_BKPT;
97204 }else{
97205 goto jump_to_p2;
97206 }
97207 }
97208 if( rc ) goto abort_due_to_error;
97209 break;
97210}
97211
97212/* Opcode: Sequence P1 P2 * * *
97213** Synopsis: r[P2]=cursor[P1].ctr++
97214**
97215** Find the next available sequence number for cursor P1.
97216** Write the sequence number into register P2.
97217** The sequence number on the cursor is incremented after this
97218** instruction.
97219*/
97220case OP_Sequence: { /* out2 */
97221 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97222 assert( p->apCsr[pOp->p1]!=0 );
97223 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
97224 pOut = out2Prerelease(p, pOp);
97225 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
97226 break;
97227}
97228
97229
97230/* Opcode: NewRowid P1 P2 P3 * *
97231** Synopsis: r[P2]=rowid
97232**
97233** Get a new integer record number (a.k.a "rowid") used as the key to a table.
97234** The record number is not previously used as a key in the database
97235** table that cursor P1 points to. The new record number is written
97236** written to register P2.
97237**
97238** If P3>0 then P3 is a register in the root frame of this VDBE that holds
97239** the largest previously generated record number. No new record numbers are
97240** allowed to be less than this value. When this value reaches its maximum,
97241** an SQLITE_FULL error is generated. The P3 register is updated with the '
97242** generated record number. This P3 mechanism is used to help implement the
97243** AUTOINCREMENT feature.
97244*/
97245case OP_NewRowid: { /* out2 */
97246 i64 v; /* The new rowid */
97247 VdbeCursor *pC; /* Cursor of table to get the new rowid */
97248 int res; /* Result of an sqlite3BtreeLast() */
97249 int cnt; /* Counter to limit the number of searches */
97250#ifndef SQLITE_OMIT_AUTOINCREMENT
97251 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
97252 VdbeFrame *pFrame; /* Root frame of VDBE */
97253#endif
97254
97255 v = 0;
97256 res = 0;
97257 pOut = out2Prerelease(p, pOp);
97258 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97259 pC = p->apCsr[pOp->p1];
97260 assert( pC!=0 );
97261 assert( pC->isTable );
97262 assert( pC->eCurType==CURTYPE_BTREE );
97263 assert( pC->uc.pCursor!=0 );
97264 {
97265 /* The next rowid or record number (different terms for the same
97266 ** thing) is obtained in a two-step algorithm.
97267 **
97268 ** First we attempt to find the largest existing rowid and add one
97269 ** to that. But if the largest existing rowid is already the maximum
97270 ** positive integer, we have to fall through to the second
97271 ** probabilistic algorithm
97272 **
97273 ** The second algorithm is to select a rowid at random and see if
97274 ** it already exists in the table. If it does not exist, we have
97275 ** succeeded. If the random rowid does exist, we select a new one
97276 ** and try again, up to 100 times.
97277 */
97278 assert( pC->isTable );
97279
97280#ifdef SQLITE_32BIT_ROWID
97281# define MAX_ROWID 0x7fffffff
97282#else
97283 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
97284 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
97285 ** to provide the constant while making all compilers happy.
97286 */
97287# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
97288#endif
97289
97290 if( !pC->useRandomRowid ){
97291 rc = sqlite3BtreeLast(pCur: pC->uc.pCursor, pRes: &res);
97292 if( rc!=SQLITE_OK ){
97293 goto abort_due_to_error;
97294 }
97295 if( res ){
97296 v = 1; /* IMP: R-61914-48074 */
97297 }else{
97298 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
97299 v = sqlite3BtreeIntegerKey(pCur: pC->uc.pCursor);
97300 if( v>=MAX_ROWID ){
97301 pC->useRandomRowid = 1;
97302 }else{
97303 v++; /* IMP: R-29538-34987 */
97304 }
97305 }
97306 }
97307
97308#ifndef SQLITE_OMIT_AUTOINCREMENT
97309 if( pOp->p3 ){
97310 /* Assert that P3 is a valid memory cell. */
97311 assert( pOp->p3>0 );
97312 if( p->pFrame ){
97313 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
97314 /* Assert that P3 is a valid memory cell. */
97315 assert( pOp->p3<=pFrame->nMem );
97316 pMem = &pFrame->aMem[pOp->p3];
97317 }else{
97318 /* Assert that P3 is a valid memory cell. */
97319 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
97320 pMem = &aMem[pOp->p3];
97321 memAboutToChange(p, pMem);
97322 }
97323 assert( memIsValid(pMem) );
97324
97325 REGISTER_TRACE(pOp->p3, pMem);
97326 sqlite3VdbeMemIntegerify(pMem);
97327 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
97328 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
97329 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
97330 goto abort_due_to_error;
97331 }
97332 if( v<pMem->u.i+1 ){
97333 v = pMem->u.i + 1;
97334 }
97335 pMem->u.i = v;
97336 }
97337#endif
97338 if( pC->useRandomRowid ){
97339 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
97340 ** largest possible integer (9223372036854775807) then the database
97341 ** engine starts picking positive candidate ROWIDs at random until
97342 ** it finds one that is not previously used. */
97343 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
97344 ** an AUTOINCREMENT table. */
97345 cnt = 0;
97346 do{
97347 sqlite3_randomness(N: sizeof(v), pBuf: &v);
97348 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
97349 }while( ((rc = sqlite3BtreeTableMoveto(pCur: pC->uc.pCursor, intKey: (u64)v,
97350 biasRight: 0, pRes: &res))==SQLITE_OK)
97351 && (res==0)
97352 && (++cnt<100));
97353 if( rc ) goto abort_due_to_error;
97354 if( res==0 ){
97355 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
97356 goto abort_due_to_error;
97357 }
97358 assert( v>0 ); /* EV: R-40812-03570 */
97359 }
97360 pC->deferredMoveto = 0;
97361 pC->cacheStatus = CACHE_STALE;
97362 }
97363 pOut->u.i = v;
97364 break;
97365}
97366
97367/* Opcode: Insert P1 P2 P3 P4 P5
97368** Synopsis: intkey=r[P3] data=r[P2]
97369**
97370** Write an entry into the table of cursor P1. A new entry is
97371** created if it doesn't already exist or the data for an existing
97372** entry is overwritten. The data is the value MEM_Blob stored in register
97373** number P2. The key is stored in register P3. The key must
97374** be a MEM_Int.
97375**
97376** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
97377** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
97378** then rowid is stored for subsequent return by the
97379** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
97380**
97381** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
97382** run faster by avoiding an unnecessary seek on cursor P1. However,
97383** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
97384** seeks on the cursor or if the most recent seek used a key equal to P3.
97385**
97386** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
97387** UPDATE operation. Otherwise (if the flag is clear) then this opcode
97388** is part of an INSERT operation. The difference is only important to
97389** the update hook.
97390**
97391** Parameter P4 may point to a Table structure, or may be NULL. If it is
97392** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
97393** following a successful insert.
97394**
97395** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
97396** allocated, then ownership of P2 is transferred to the pseudo-cursor
97397** and register P2 becomes ephemeral. If the cursor is changed, the
97398** value of register P2 will then change. Make sure this does not
97399** cause any problems.)
97400**
97401** This instruction only works on tables. The equivalent instruction
97402** for indices is OP_IdxInsert.
97403*/
97404case OP_Insert: {
97405 Mem *pData; /* MEM cell holding data for the record to be inserted */
97406 Mem *pKey; /* MEM cell holding key for the record */
97407 VdbeCursor *pC; /* Cursor to table into which insert is written */
97408 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
97409 const char *zDb; /* database name - used by the update hook */
97410 Table *pTab; /* Table structure - used by update and pre-update hooks */
97411 BtreePayload x; /* Payload to be inserted */
97412
97413 pData = &aMem[pOp->p2];
97414 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97415 assert( memIsValid(pData) );
97416 pC = p->apCsr[pOp->p1];
97417 assert( pC!=0 );
97418 assert( pC->eCurType==CURTYPE_BTREE );
97419 assert( pC->deferredMoveto==0 );
97420 assert( pC->uc.pCursor!=0 );
97421 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
97422 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
97423 REGISTER_TRACE(pOp->p2, pData);
97424 sqlite3VdbeIncrWriteCounter(p, pC);
97425
97426 pKey = &aMem[pOp->p3];
97427 assert( pKey->flags & MEM_Int );
97428 assert( memIsValid(pKey) );
97429 REGISTER_TRACE(pOp->p3, pKey);
97430 x.nKey = pKey->u.i;
97431
97432 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
97433 assert( pC->iDb>=0 );
97434 zDb = db->aDb[pC->iDb].zDbSName;
97435 pTab = pOp->p4.pTab;
97436 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
97437 }else{
97438 pTab = 0;
97439 zDb = 0;
97440 }
97441
97442#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
97443 /* Invoke the pre-update hook, if any */
97444 if( pTab ){
97445 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
97446 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
97447 }
97448 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
97449 /* Prevent post-update hook from running in cases when it should not */
97450 pTab = 0;
97451 }
97452 }
97453 if( pOp->p5 & OPFLAG_ISNOOP ) break;
97454#endif
97455
97456 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
97457 if( pOp->p5 & OPFLAG_NCHANGE ){
97458 p->nChange++;
97459 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
97460 }
97461 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
97462 x.pData = pData->z;
97463 x.nData = pData->n;
97464 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
97465 if( pData->flags & MEM_Zero ){
97466 x.nZero = pData->u.nZero;
97467 }else{
97468 x.nZero = 0;
97469 }
97470 x.pKey = 0;
97471 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
97472 rc = sqlite3BtreeInsert(pCur: pC->uc.pCursor, pX: &x,
97473 flags: (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
97474 seekResult
97475 );
97476 pC->deferredMoveto = 0;
97477 pC->cacheStatus = CACHE_STALE;
97478 colCacheCtr++;
97479
97480 /* Invoke the update-hook if required. */
97481 if( rc ) goto abort_due_to_error;
97482 if( pTab ){
97483 assert( db->xUpdateCallback!=0 );
97484 assert( pTab->aCol!=0 );
97485 db->xUpdateCallback(db->pUpdateArg,
97486 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
97487 zDb, pTab->zName, x.nKey);
97488 }
97489 break;
97490}
97491
97492/* Opcode: RowCell P1 P2 P3 * *
97493**
97494** P1 and P2 are both open cursors. Both must be opened on the same type
97495** of table - intkey or index. This opcode is used as part of copying
97496** the current row from P2 into P1. If the cursors are opened on intkey
97497** tables, register P3 contains the rowid to use with the new record in
97498** P1. If they are opened on index tables, P3 is not used.
97499**
97500** This opcode must be followed by either an Insert or InsertIdx opcode
97501** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
97502*/
97503case OP_RowCell: {
97504 VdbeCursor *pDest; /* Cursor to write to */
97505 VdbeCursor *pSrc; /* Cursor to read from */
97506 i64 iKey; /* Rowid value to insert with */
97507 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
97508 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
97509 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
97510 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
97511 pDest = p->apCsr[pOp->p1];
97512 pSrc = p->apCsr[pOp->p2];
97513 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
97514 rc = sqlite3BtreeTransferRow(pDest: pDest->uc.pCursor, pSrc: pSrc->uc.pCursor, iKey);
97515 if( rc!=SQLITE_OK ) goto abort_due_to_error;
97516 break;
97517};
97518
97519/* Opcode: Delete P1 P2 P3 P4 P5
97520**
97521** Delete the record at which the P1 cursor is currently pointing.
97522**
97523** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
97524** the cursor will be left pointing at either the next or the previous
97525** record in the table. If it is left pointing at the next record, then
97526** the next Next instruction will be a no-op. As a result, in this case
97527** it is ok to delete a record from within a Next loop. If
97528** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
97529** left in an undefined state.
97530**
97531** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
97532** delete is one of several associated with deleting a table row and
97533** all its associated index entries. Exactly one of those deletes is
97534** the "primary" delete. The others are all on OPFLAG_FORDELETE
97535** cursors or else are marked with the AUXDELETE flag.
97536**
97537** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
97538** change count is incremented (otherwise not).
97539**
97540** P1 must not be pseudo-table. It has to be a real table with
97541** multiple rows.
97542**
97543** If P4 is not NULL then it points to a Table object. In this case either
97544** the update or pre-update hook, or both, may be invoked. The P1 cursor must
97545** have been positioned using OP_NotFound prior to invoking this opcode in
97546** this case. Specifically, if one is configured, the pre-update hook is
97547** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
97548** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
97549**
97550** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
97551** of the memory cell that contains the value that the rowid of the row will
97552** be set to by the update.
97553*/
97554case OP_Delete: {
97555 VdbeCursor *pC;
97556 const char *zDb;
97557 Table *pTab;
97558 int opflags;
97559
97560 opflags = pOp->p2;
97561 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97562 pC = p->apCsr[pOp->p1];
97563 assert( pC!=0 );
97564 assert( pC->eCurType==CURTYPE_BTREE );
97565 assert( pC->uc.pCursor!=0 );
97566 assert( pC->deferredMoveto==0 );
97567 sqlite3VdbeIncrWriteCounter(p, pC);
97568
97569#ifdef SQLITE_DEBUG
97570 if( pOp->p4type==P4_TABLE
97571 && HasRowid(pOp->p4.pTab)
97572 && pOp->p5==0
97573 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
97574 ){
97575 /* If p5 is zero, the seek operation that positioned the cursor prior to
97576 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
97577 ** the row that is being deleted */
97578 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
97579 assert( CORRUPT_DB || pC->movetoTarget==iKey );
97580 }
97581#endif
97582
97583 /* If the update-hook or pre-update-hook will be invoked, set zDb to
97584 ** the name of the db to pass as to it. Also set local pTab to a copy
97585 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
97586 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
97587 ** VdbeCursor.movetoTarget to the current rowid. */
97588 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
97589 assert( pC->iDb>=0 );
97590 assert( pOp->p4.pTab!=0 );
97591 zDb = db->aDb[pC->iDb].zDbSName;
97592 pTab = pOp->p4.pTab;
97593 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
97594 pC->movetoTarget = sqlite3BtreeIntegerKey(pCur: pC->uc.pCursor);
97595 }
97596 }else{
97597 zDb = 0;
97598 pTab = 0;
97599 }
97600
97601#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
97602 /* Invoke the pre-update-hook if required. */
97603 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
97604 if( db->xPreUpdateCallback && pTab ){
97605 assert( !(opflags & OPFLAG_ISUPDATE)
97606 || HasRowid(pTab)==0
97607 || (aMem[pOp->p3].flags & MEM_Int)
97608 );
97609 sqlite3VdbePreUpdateHook(p, pC,
97610 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
97611 zDb, pTab, pC->movetoTarget,
97612 pOp->p3, -1
97613 );
97614 }
97615 if( opflags & OPFLAG_ISNOOP ) break;
97616#endif
97617
97618 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
97619 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
97620 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
97621 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
97622
97623#ifdef SQLITE_DEBUG
97624 if( p->pFrame==0 ){
97625 if( pC->isEphemeral==0
97626 && (pOp->p5 & OPFLAG_AUXDELETE)==0
97627 && (pC->wrFlag & OPFLAG_FORDELETE)==0
97628 ){
97629 nExtraDelete++;
97630 }
97631 if( pOp->p2 & OPFLAG_NCHANGE ){
97632 nExtraDelete--;
97633 }
97634 }
97635#endif
97636
97637 rc = sqlite3BtreeDelete(pCur: pC->uc.pCursor, flags: pOp->p5);
97638 pC->cacheStatus = CACHE_STALE;
97639 colCacheCtr++;
97640 pC->seekResult = 0;
97641 if( rc ) goto abort_due_to_error;
97642
97643 /* Invoke the update-hook if required. */
97644 if( opflags & OPFLAG_NCHANGE ){
97645 p->nChange++;
97646 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
97647 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
97648 pC->movetoTarget);
97649 assert( pC->iDb>=0 );
97650 }
97651 }
97652
97653 break;
97654}
97655/* Opcode: ResetCount * * * * *
97656**
97657** The value of the change counter is copied to the database handle
97658** change counter (returned by subsequent calls to sqlite3_changes()).
97659** Then the VMs internal change counter resets to 0.
97660** This is used by trigger programs.
97661*/
97662case OP_ResetCount: {
97663 sqlite3VdbeSetChanges(db, nChange: p->nChange);
97664 p->nChange = 0;
97665 break;
97666}
97667
97668/* Opcode: SorterCompare P1 P2 P3 P4
97669** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
97670**
97671** P1 is a sorter cursor. This instruction compares a prefix of the
97672** record blob in register P3 against a prefix of the entry that
97673** the sorter cursor currently points to. Only the first P4 fields
97674** of r[P3] and the sorter record are compared.
97675**
97676** If either P3 or the sorter contains a NULL in one of their significant
97677** fields (not counting the P4 fields at the end which are ignored) then
97678** the comparison is assumed to be equal.
97679**
97680** Fall through to next instruction if the two records compare equal to
97681** each other. Jump to P2 if they are different.
97682*/
97683case OP_SorterCompare: {
97684 VdbeCursor *pC;
97685 int res;
97686 int nKeyCol;
97687
97688 pC = p->apCsr[pOp->p1];
97689 assert( isSorter(pC) );
97690 assert( pOp->p4type==P4_INT32 );
97691 pIn3 = &aMem[pOp->p3];
97692 nKeyCol = pOp->p4.i;
97693 res = 0;
97694 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
97695 VdbeBranchTaken(res!=0,2);
97696 if( rc ) goto abort_due_to_error;
97697 if( res ) goto jump_to_p2;
97698 break;
97699};
97700
97701/* Opcode: SorterData P1 P2 P3 * *
97702** Synopsis: r[P2]=data
97703**
97704** Write into register P2 the current sorter data for sorter cursor P1.
97705** Then clear the column header cache on cursor P3.
97706**
97707** This opcode is normally used to move a record out of the sorter and into
97708** a register that is the source for a pseudo-table cursor created using
97709** OpenPseudo. That pseudo-table cursor is the one that is identified by
97710** parameter P3. Clearing the P3 column cache as part of this opcode saves
97711** us from having to issue a separate NullRow instruction to clear that cache.
97712*/
97713case OP_SorterData: { /* ncycle */
97714 VdbeCursor *pC;
97715
97716 pOut = &aMem[pOp->p2];
97717 pC = p->apCsr[pOp->p1];
97718 assert( isSorter(pC) );
97719 rc = sqlite3VdbeSorterRowkey(pC, pOut);
97720 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
97721 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97722 if( rc ) goto abort_due_to_error;
97723 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
97724 break;
97725}
97726
97727/* Opcode: RowData P1 P2 P3 * *
97728** Synopsis: r[P2]=data
97729**
97730** Write into register P2 the complete row content for the row at
97731** which cursor P1 is currently pointing.
97732** There is no interpretation of the data.
97733** It is just copied onto the P2 register exactly as
97734** it is found in the database file.
97735**
97736** If cursor P1 is an index, then the content is the key of the row.
97737** If cursor P2 is a table, then the content extracted is the data.
97738**
97739** If the P1 cursor must be pointing to a valid row (not a NULL row)
97740** of a real table, not a pseudo-table.
97741**
97742** If P3!=0 then this opcode is allowed to make an ephemeral pointer
97743** into the database page. That means that the content of the output
97744** register will be invalidated as soon as the cursor moves - including
97745** moves caused by other cursors that "save" the current cursors
97746** position in order that they can write to the same table. If P3==0
97747** then a copy of the data is made into memory. P3!=0 is faster, but
97748** P3==0 is safer.
97749**
97750** If P3!=0 then the content of the P2 register is unsuitable for use
97751** in OP_Result and any OP_Result will invalidate the P2 register content.
97752** The P2 register content is invalidated by opcodes like OP_Function or
97753** by any use of another cursor pointing to the same table.
97754*/
97755case OP_RowData: {
97756 VdbeCursor *pC;
97757 BtCursor *pCrsr;
97758 u32 n;
97759
97760 pOut = out2Prerelease(p, pOp);
97761
97762 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97763 pC = p->apCsr[pOp->p1];
97764 assert( pC!=0 );
97765 assert( pC->eCurType==CURTYPE_BTREE );
97766 assert( isSorter(pC)==0 );
97767 assert( pC->nullRow==0 );
97768 assert( pC->uc.pCursor!=0 );
97769 pCrsr = pC->uc.pCursor;
97770
97771 /* The OP_RowData opcodes always follow OP_NotExists or
97772 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
97773 ** that might invalidate the cursor.
97774 ** If this where not the case, on of the following assert()s
97775 ** would fail. Should this ever change (because of changes in the code
97776 ** generator) then the fix would be to insert a call to
97777 ** sqlite3VdbeCursorMoveto().
97778 */
97779 assert( pC->deferredMoveto==0 );
97780 assert( sqlite3BtreeCursorIsValid(pCrsr) );
97781
97782 n = sqlite3BtreePayloadSize(pCur: pCrsr);
97783 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
97784 goto too_big;
97785 }
97786 testcase( n==0 );
97787 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur: pCrsr, amt: n, pMem: pOut);
97788 if( rc ) goto abort_due_to_error;
97789 if( !pOp->p3 ) Deephemeralize(pOut);
97790 UPDATE_MAX_BLOBSIZE(pOut);
97791 REGISTER_TRACE(pOp->p2, pOut);
97792 break;
97793}
97794
97795/* Opcode: Rowid P1 P2 * * *
97796** Synopsis: r[P2]=PX rowid of P1
97797**
97798** Store in register P2 an integer which is the key of the table entry that
97799** P1 is currently point to.
97800**
97801** P1 can be either an ordinary table or a virtual table. There used to
97802** be a separate OP_VRowid opcode for use with virtual tables, but this
97803** one opcode now works for both table types.
97804*/
97805case OP_Rowid: { /* out2, ncycle */
97806 VdbeCursor *pC;
97807 i64 v;
97808 sqlite3_vtab *pVtab;
97809 const sqlite3_module *pModule;
97810
97811 pOut = out2Prerelease(p, pOp);
97812 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97813 pC = p->apCsr[pOp->p1];
97814 assert( pC!=0 );
97815 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
97816 if( pC->nullRow ){
97817 pOut->flags = MEM_Null;
97818 break;
97819 }else if( pC->deferredMoveto ){
97820 v = pC->movetoTarget;
97821#ifndef SQLITE_OMIT_VIRTUALTABLE
97822 }else if( pC->eCurType==CURTYPE_VTAB ){
97823 assert( pC->uc.pVCur!=0 );
97824 pVtab = pC->uc.pVCur->pVtab;
97825 pModule = pVtab->pModule;
97826 assert( pModule->xRowid );
97827 rc = pModule->xRowid(pC->uc.pVCur, &v);
97828 sqlite3VtabImportErrmsg(p, pVtab);
97829 if( rc ) goto abort_due_to_error;
97830#endif /* SQLITE_OMIT_VIRTUALTABLE */
97831 }else{
97832 assert( pC->eCurType==CURTYPE_BTREE );
97833 assert( pC->uc.pCursor!=0 );
97834 rc = sqlite3VdbeCursorRestore(p: pC);
97835 if( rc ) goto abort_due_to_error;
97836 if( pC->nullRow ){
97837 pOut->flags = MEM_Null;
97838 break;
97839 }
97840 v = sqlite3BtreeIntegerKey(pCur: pC->uc.pCursor);
97841 }
97842 pOut->u.i = v;
97843 break;
97844}
97845
97846/* Opcode: NullRow P1 * * * *
97847**
97848** Move the cursor P1 to a null row. Any OP_Column operations
97849** that occur while the cursor is on the null row will always
97850** write a NULL.
97851**
97852** If cursor P1 is not previously opened, open it now to a special
97853** pseudo-cursor that always returns NULL for every column.
97854*/
97855case OP_NullRow: {
97856 VdbeCursor *pC;
97857
97858 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97859 pC = p->apCsr[pOp->p1];
97860 if( pC==0 ){
97861 /* If the cursor is not already open, create a special kind of
97862 ** pseudo-cursor that always gives null rows. */
97863 pC = allocateCursor(p, iCur: pOp->p1, nField: 1, CURTYPE_PSEUDO);
97864 if( pC==0 ) goto no_mem;
97865 pC->seekResult = 0;
97866 pC->isTable = 1;
97867 pC->noReuse = 1;
97868 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
97869 }
97870 pC->nullRow = 1;
97871 pC->cacheStatus = CACHE_STALE;
97872 if( pC->eCurType==CURTYPE_BTREE ){
97873 assert( pC->uc.pCursor!=0 );
97874 sqlite3BtreeClearCursor(pCur: pC->uc.pCursor);
97875 }
97876#ifdef SQLITE_DEBUG
97877 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
97878#endif
97879 break;
97880}
97881
97882/* Opcode: SeekEnd P1 * * * *
97883**
97884** Position cursor P1 at the end of the btree for the purpose of
97885** appending a new entry onto the btree.
97886**
97887** It is assumed that the cursor is used only for appending and so
97888** if the cursor is valid, then the cursor must already be pointing
97889** at the end of the btree and so no changes are made to
97890** the cursor.
97891*/
97892/* Opcode: Last P1 P2 * * *
97893**
97894** The next use of the Rowid or Column or Prev instruction for P1
97895** will refer to the last entry in the database table or index.
97896** If the table or index is empty and P2>0, then jump immediately to P2.
97897** If P2 is 0 or if the table or index is not empty, fall through
97898** to the following instruction.
97899**
97900** This opcode leaves the cursor configured to move in reverse order,
97901** from the end toward the beginning. In other words, the cursor is
97902** configured to use Prev, not Next.
97903*/
97904case OP_SeekEnd: /* ncycle */
97905case OP_Last: { /* jump, ncycle */
97906 VdbeCursor *pC;
97907 BtCursor *pCrsr;
97908 int res;
97909
97910 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97911 pC = p->apCsr[pOp->p1];
97912 assert( pC!=0 );
97913 assert( pC->eCurType==CURTYPE_BTREE );
97914 pCrsr = pC->uc.pCursor;
97915 res = 0;
97916 assert( pCrsr!=0 );
97917#ifdef SQLITE_DEBUG
97918 pC->seekOp = pOp->opcode;
97919#endif
97920 if( pOp->opcode==OP_SeekEnd ){
97921 assert( pOp->p2==0 );
97922 pC->seekResult = -1;
97923 if( sqlite3BtreeCursorIsValidNN(pCur: pCrsr) ){
97924 break;
97925 }
97926 }
97927 rc = sqlite3BtreeLast(pCur: pCrsr, pRes: &res);
97928 pC->nullRow = (u8)res;
97929 pC->deferredMoveto = 0;
97930 pC->cacheStatus = CACHE_STALE;
97931 if( rc ) goto abort_due_to_error;
97932 if( pOp->p2>0 ){
97933 VdbeBranchTaken(res!=0,2);
97934 if( res ) goto jump_to_p2;
97935 }
97936 break;
97937}
97938
97939/* Opcode: IfSmaller P1 P2 P3 * *
97940**
97941** Estimate the number of rows in the table P1. Jump to P2 if that
97942** estimate is less than approximately 2**(0.1*P3).
97943*/
97944case OP_IfSmaller: { /* jump */
97945 VdbeCursor *pC;
97946 BtCursor *pCrsr;
97947 int res;
97948 i64 sz;
97949
97950 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97951 pC = p->apCsr[pOp->p1];
97952 assert( pC!=0 );
97953 pCrsr = pC->uc.pCursor;
97954 assert( pCrsr );
97955 rc = sqlite3BtreeFirst(pCur: pCrsr, pRes: &res);
97956 if( rc ) goto abort_due_to_error;
97957 if( res==0 ){
97958 sz = sqlite3BtreeRowCountEst(pCur: pCrsr);
97959 if( ALWAYS(sz>=0) && sqlite3LogEst(x: (u64)sz)<pOp->p3 ) res = 1;
97960 }
97961 VdbeBranchTaken(res!=0,2);
97962 if( res ) goto jump_to_p2;
97963 break;
97964}
97965
97966
97967/* Opcode: SorterSort P1 P2 * * *
97968**
97969** After all records have been inserted into the Sorter object
97970** identified by P1, invoke this opcode to actually do the sorting.
97971** Jump to P2 if there are no records to be sorted.
97972**
97973** This opcode is an alias for OP_Sort and OP_Rewind that is used
97974** for Sorter objects.
97975*/
97976/* Opcode: Sort P1 P2 * * *
97977**
97978** This opcode does exactly the same thing as OP_Rewind except that
97979** it increments an undocumented global variable used for testing.
97980**
97981** Sorting is accomplished by writing records into a sorting index,
97982** then rewinding that index and playing it back from beginning to
97983** end. We use the OP_Sort opcode instead of OP_Rewind to do the
97984** rewinding so that the global variable will be incremented and
97985** regression tests can determine whether or not the optimizer is
97986** correctly optimizing out sorts.
97987*/
97988case OP_SorterSort: /* jump ncycle */
97989case OP_Sort: { /* jump ncycle */
97990#ifdef SQLITE_TEST
97991 sqlite3_sort_count++;
97992 sqlite3_search_count--;
97993#endif
97994 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
97995 /* Fall through into OP_Rewind */
97996 /* no break */ deliberate_fall_through
97997}
97998/* Opcode: Rewind P1 P2 * * *
97999**
98000** The next use of the Rowid or Column or Next instruction for P1
98001** will refer to the first entry in the database table or index.
98002** If the table or index is empty, jump immediately to P2.
98003** If the table or index is not empty, fall through to the following
98004** instruction.
98005**
98006** If P2 is zero, that is an assertion that the P1 table is never
98007** empty and hence the jump will never be taken.
98008**
98009** This opcode leaves the cursor configured to move in forward order,
98010** from the beginning toward the end. In other words, the cursor is
98011** configured to use Next, not Prev.
98012*/
98013case OP_Rewind: { /* jump, ncycle */
98014 VdbeCursor *pC;
98015 BtCursor *pCrsr;
98016 int res;
98017
98018 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98019 assert( pOp->p5==0 );
98020 assert( pOp->p2>=0 && pOp->p2<p->nOp );
98021
98022 pC = p->apCsr[pOp->p1];
98023 assert( pC!=0 );
98024 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
98025 res = 1;
98026#ifdef SQLITE_DEBUG
98027 pC->seekOp = OP_Rewind;
98028#endif
98029 if( isSorter(pC) ){
98030 rc = sqlite3VdbeSorterRewind(pC, &res);
98031 }else{
98032 assert( pC->eCurType==CURTYPE_BTREE );
98033 pCrsr = pC->uc.pCursor;
98034 assert( pCrsr );
98035 rc = sqlite3BtreeFirst(pCur: pCrsr, pRes: &res);
98036 pC->deferredMoveto = 0;
98037 pC->cacheStatus = CACHE_STALE;
98038 }
98039 if( rc ) goto abort_due_to_error;
98040 pC->nullRow = (u8)res;
98041 if( pOp->p2>0 ){
98042 VdbeBranchTaken(res!=0,2);
98043 if( res ) goto jump_to_p2;
98044 }
98045 break;
98046}
98047
98048/* Opcode: Next P1 P2 P3 * P5
98049**
98050** Advance cursor P1 so that it points to the next key/data pair in its
98051** table or index. If there are no more key/value pairs then fall through
98052** to the following instruction. But if the cursor advance was successful,
98053** jump immediately to P2.
98054**
98055** The Next opcode is only valid following an SeekGT, SeekGE, or
98056** OP_Rewind opcode used to position the cursor. Next is not allowed
98057** to follow SeekLT, SeekLE, or OP_Last.
98058**
98059** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
98060** been opened prior to this opcode or the program will segfault.
98061**
98062** The P3 value is a hint to the btree implementation. If P3==1, that
98063** means P1 is an SQL index and that this instruction could have been
98064** omitted if that index had been unique. P3 is usually 0. P3 is
98065** always either 0 or 1.
98066**
98067** If P5 is positive and the jump is taken, then event counter
98068** number P5-1 in the prepared statement is incremented.
98069**
98070** See also: Prev
98071*/
98072/* Opcode: Prev P1 P2 P3 * P5
98073**
98074** Back up cursor P1 so that it points to the previous key/data pair in its
98075** table or index. If there is no previous key/value pairs then fall through
98076** to the following instruction. But if the cursor backup was successful,
98077** jump immediately to P2.
98078**
98079**
98080** The Prev opcode is only valid following an SeekLT, SeekLE, or
98081** OP_Last opcode used to position the cursor. Prev is not allowed
98082** to follow SeekGT, SeekGE, or OP_Rewind.
98083**
98084** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
98085** not open then the behavior is undefined.
98086**
98087** The P3 value is a hint to the btree implementation. If P3==1, that
98088** means P1 is an SQL index and that this instruction could have been
98089** omitted if that index had been unique. P3 is usually 0. P3 is
98090** always either 0 or 1.
98091**
98092** If P5 is positive and the jump is taken, then event counter
98093** number P5-1 in the prepared statement is incremented.
98094*/
98095/* Opcode: SorterNext P1 P2 * * P5
98096**
98097** This opcode works just like OP_Next except that P1 must be a
98098** sorter object for which the OP_SorterSort opcode has been
98099** invoked. This opcode advances the cursor to the next sorted
98100** record, or jumps to P2 if there are no more sorted records.
98101*/
98102case OP_SorterNext: { /* jump */
98103 VdbeCursor *pC;
98104
98105 pC = p->apCsr[pOp->p1];
98106 assert( isSorter(pC) );
98107 rc = sqlite3VdbeSorterNext(db, pC);
98108 goto next_tail;
98109
98110case OP_Prev: /* jump, ncycle */
98111 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98112 assert( pOp->p5==0
98113 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
98114 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
98115 pC = p->apCsr[pOp->p1];
98116 assert( pC!=0 );
98117 assert( pC->deferredMoveto==0 );
98118 assert( pC->eCurType==CURTYPE_BTREE );
98119 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
98120 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
98121 || pC->seekOp==OP_NullRow);
98122 rc = sqlite3BtreePrevious(pCur: pC->uc.pCursor, flags: pOp->p3);
98123 goto next_tail;
98124
98125case OP_Next: /* jump, ncycle */
98126 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98127 assert( pOp->p5==0
98128 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
98129 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
98130 pC = p->apCsr[pOp->p1];
98131 assert( pC!=0 );
98132 assert( pC->deferredMoveto==0 );
98133 assert( pC->eCurType==CURTYPE_BTREE );
98134 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
98135 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
98136 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
98137 || pC->seekOp==OP_IfNoHope);
98138 rc = sqlite3BtreeNext(pCur: pC->uc.pCursor, flags: pOp->p3);
98139
98140next_tail:
98141 pC->cacheStatus = CACHE_STALE;
98142 VdbeBranchTaken(rc==SQLITE_OK,2);
98143 if( rc==SQLITE_OK ){
98144 pC->nullRow = 0;
98145 p->aCounter[pOp->p5]++;
98146#ifdef SQLITE_TEST
98147 sqlite3_search_count++;
98148#endif
98149 goto jump_to_p2_and_check_for_interrupt;
98150 }
98151 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
98152 rc = SQLITE_OK;
98153 pC->nullRow = 1;
98154 goto check_for_interrupt;
98155}
98156
98157/* Opcode: IdxInsert P1 P2 P3 P4 P5
98158** Synopsis: key=r[P2]
98159**
98160** Register P2 holds an SQL index key made using the
98161** MakeRecord instructions. This opcode writes that key
98162** into the index P1. Data for the entry is nil.
98163**
98164** If P4 is not zero, then it is the number of values in the unpacked
98165** key of reg(P2). In that case, P3 is the index of the first register
98166** for the unpacked key. The availability of the unpacked key can sometimes
98167** be an optimization.
98168**
98169** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
98170** that this insert is likely to be an append.
98171**
98172** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
98173** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
98174** then the change counter is unchanged.
98175**
98176** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
98177** run faster by avoiding an unnecessary seek on cursor P1. However,
98178** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
98179** seeks on the cursor or if the most recent seek used a key equivalent
98180** to P2.
98181**
98182** This instruction only works for indices. The equivalent instruction
98183** for tables is OP_Insert.
98184*/
98185case OP_IdxInsert: { /* in2 */
98186 VdbeCursor *pC;
98187 BtreePayload x;
98188
98189 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98190 pC = p->apCsr[pOp->p1];
98191 sqlite3VdbeIncrWriteCounter(p, pC);
98192 assert( pC!=0 );
98193 assert( !isSorter(pC) );
98194 pIn2 = &aMem[pOp->p2];
98195 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
98196 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
98197 assert( pC->eCurType==CURTYPE_BTREE );
98198 assert( pC->isTable==0 );
98199 rc = ExpandBlob(pIn2);
98200 if( rc ) goto abort_due_to_error;
98201 x.nKey = pIn2->n;
98202 x.pKey = pIn2->z;
98203 x.aMem = aMem + pOp->p3;
98204 x.nMem = (u16)pOp->p4.i;
98205 rc = sqlite3BtreeInsert(pCur: pC->uc.pCursor, pX: &x,
98206 flags: (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
98207 seekResult: ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
98208 );
98209 assert( pC->deferredMoveto==0 );
98210 pC->cacheStatus = CACHE_STALE;
98211 if( rc) goto abort_due_to_error;
98212 break;
98213}
98214
98215/* Opcode: SorterInsert P1 P2 * * *
98216** Synopsis: key=r[P2]
98217**
98218** Register P2 holds an SQL index key made using the
98219** MakeRecord instructions. This opcode writes that key
98220** into the sorter P1. Data for the entry is nil.
98221*/
98222case OP_SorterInsert: { /* in2 */
98223 VdbeCursor *pC;
98224
98225 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98226 pC = p->apCsr[pOp->p1];
98227 sqlite3VdbeIncrWriteCounter(p, pC);
98228 assert( pC!=0 );
98229 assert( isSorter(pC) );
98230 pIn2 = &aMem[pOp->p2];
98231 assert( pIn2->flags & MEM_Blob );
98232 assert( pC->isTable==0 );
98233 rc = ExpandBlob(pIn2);
98234 if( rc ) goto abort_due_to_error;
98235 rc = sqlite3VdbeSorterWrite(pC, pIn2);
98236 if( rc) goto abort_due_to_error;
98237 break;
98238}
98239
98240/* Opcode: IdxDelete P1 P2 P3 * P5
98241** Synopsis: key=r[P2@P3]
98242**
98243** The content of P3 registers starting at register P2 form
98244** an unpacked index key. This opcode removes that entry from the
98245** index opened by cursor P1.
98246**
98247** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
98248** if no matching index entry is found. This happens when running
98249** an UPDATE or DELETE statement and the index entry to be updated
98250** or deleted is not found. For some uses of IdxDelete
98251** (example: the EXCEPT operator) it does not matter that no matching
98252** entry is found. For those cases, P5 is zero. Also, do not raise
98253** this (self-correcting and non-critical) error if in writable_schema mode.
98254*/
98255case OP_IdxDelete: {
98256 VdbeCursor *pC;
98257 BtCursor *pCrsr;
98258 int res;
98259 UnpackedRecord r;
98260
98261 assert( pOp->p3>0 );
98262 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
98263 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98264 pC = p->apCsr[pOp->p1];
98265 assert( pC!=0 );
98266 assert( pC->eCurType==CURTYPE_BTREE );
98267 sqlite3VdbeIncrWriteCounter(p, pC);
98268 pCrsr = pC->uc.pCursor;
98269 assert( pCrsr!=0 );
98270 r.pKeyInfo = pC->pKeyInfo;
98271 r.nField = (u16)pOp->p3;
98272 r.default_rc = 0;
98273 r.aMem = &aMem[pOp->p2];
98274 rc = sqlite3BtreeIndexMoveto(pCur: pCrsr, pIdxKey: &r, pRes: &res);
98275 if( rc ) goto abort_due_to_error;
98276 if( res==0 ){
98277 rc = sqlite3BtreeDelete(pCur: pCrsr, BTREE_AUXDELETE);
98278 if( rc ) goto abort_due_to_error;
98279 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
98280 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, zType: "index corruption");
98281 goto abort_due_to_error;
98282 }
98283 assert( pC->deferredMoveto==0 );
98284 pC->cacheStatus = CACHE_STALE;
98285 pC->seekResult = 0;
98286 break;
98287}
98288
98289/* Opcode: DeferredSeek P1 * P3 P4 *
98290** Synopsis: Move P3 to P1.rowid if needed
98291**
98292** P1 is an open index cursor and P3 is a cursor on the corresponding
98293** table. This opcode does a deferred seek of the P3 table cursor
98294** to the row that corresponds to the current row of P1.
98295**
98296** This is a deferred seek. Nothing actually happens until
98297** the cursor is used to read a record. That way, if no reads
98298** occur, no unnecessary I/O happens.
98299**
98300** P4 may be an array of integers (type P4_INTARRAY) containing
98301** one entry for each column in the P3 table. If array entry a(i)
98302** is non-zero, then reading column a(i)-1 from cursor P3 is
98303** equivalent to performing the deferred seek and then reading column i
98304** from P1. This information is stored in P3 and used to redirect
98305** reads against P3 over to P1, thus possibly avoiding the need to
98306** seek and read cursor P3.
98307*/
98308/* Opcode: IdxRowid P1 P2 * * *
98309** Synopsis: r[P2]=rowid
98310**
98311** Write into register P2 an integer which is the last entry in the record at
98312** the end of the index key pointed to by cursor P1. This integer should be
98313** the rowid of the table entry to which this index entry points.
98314**
98315** See also: Rowid, MakeRecord.
98316*/
98317case OP_DeferredSeek: /* ncycle */
98318case OP_IdxRowid: { /* out2, ncycle */
98319 VdbeCursor *pC; /* The P1 index cursor */
98320 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
98321 i64 rowid; /* Rowid that P1 current points to */
98322
98323 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98324 pC = p->apCsr[pOp->p1];
98325 assert( pC!=0 );
98326 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
98327 assert( pC->uc.pCursor!=0 );
98328 assert( pC->isTable==0 || IsNullCursor(pC) );
98329 assert( pC->deferredMoveto==0 );
98330 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
98331
98332 /* The IdxRowid and Seek opcodes are combined because of the commonality
98333 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
98334 rc = sqlite3VdbeCursorRestore(p: pC);
98335
98336 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
98337 ** since it was last positioned and an error (e.g. OOM or an IO error)
98338 ** occurs while trying to reposition it. */
98339 if( rc!=SQLITE_OK ) goto abort_due_to_error;
98340
98341 if( !pC->nullRow ){
98342 rowid = 0; /* Not needed. Only used to silence a warning. */
98343 rc = sqlite3VdbeIdxRowid(db, pCur: pC->uc.pCursor, rowid: &rowid);
98344 if( rc!=SQLITE_OK ){
98345 goto abort_due_to_error;
98346 }
98347 if( pOp->opcode==OP_DeferredSeek ){
98348 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
98349 pTabCur = p->apCsr[pOp->p3];
98350 assert( pTabCur!=0 );
98351 assert( pTabCur->eCurType==CURTYPE_BTREE );
98352 assert( pTabCur->uc.pCursor!=0 );
98353 assert( pTabCur->isTable );
98354 pTabCur->nullRow = 0;
98355 pTabCur->movetoTarget = rowid;
98356 pTabCur->deferredMoveto = 1;
98357 pTabCur->cacheStatus = CACHE_STALE;
98358 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
98359 assert( !pTabCur->isEphemeral );
98360 pTabCur->ub.aAltMap = pOp->p4.ai;
98361 assert( !pC->isEphemeral );
98362 pTabCur->pAltCursor = pC;
98363 }else{
98364 pOut = out2Prerelease(p, pOp);
98365 pOut->u.i = rowid;
98366 }
98367 }else{
98368 assert( pOp->opcode==OP_IdxRowid );
98369 sqlite3VdbeMemSetNull(pMem: &aMem[pOp->p2]);
98370 }
98371 break;
98372}
98373
98374/* Opcode: FinishSeek P1 * * * *
98375**
98376** If cursor P1 was previously moved via OP_DeferredSeek, complete that
98377** seek operation now, without further delay. If the cursor seek has
98378** already occurred, this instruction is a no-op.
98379*/
98380case OP_FinishSeek: { /* ncycle */
98381 VdbeCursor *pC; /* The P1 index cursor */
98382
98383 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98384 pC = p->apCsr[pOp->p1];
98385 if( pC->deferredMoveto ){
98386 rc = sqlite3VdbeFinishMoveto(p: pC);
98387 if( rc ) goto abort_due_to_error;
98388 }
98389 break;
98390}
98391
98392/* Opcode: IdxGE P1 P2 P3 P4 *
98393** Synopsis: key=r[P3@P4]
98394**
98395** The P4 register values beginning with P3 form an unpacked index
98396** key that omits the PRIMARY KEY. Compare this key value against the index
98397** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
98398** fields at the end.
98399**
98400** If the P1 index entry is greater than or equal to the key value
98401** then jump to P2. Otherwise fall through to the next instruction.
98402*/
98403/* Opcode: IdxGT P1 P2 P3 P4 *
98404** Synopsis: key=r[P3@P4]
98405**
98406** The P4 register values beginning with P3 form an unpacked index
98407** key that omits the PRIMARY KEY. Compare this key value against the index
98408** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
98409** fields at the end.
98410**
98411** If the P1 index entry is greater than the key value
98412** then jump to P2. Otherwise fall through to the next instruction.
98413*/
98414/* Opcode: IdxLT P1 P2 P3 P4 *
98415** Synopsis: key=r[P3@P4]
98416**
98417** The P4 register values beginning with P3 form an unpacked index
98418** key that omits the PRIMARY KEY or ROWID. Compare this key value against
98419** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
98420** ROWID on the P1 index.
98421**
98422** If the P1 index entry is less than the key value then jump to P2.
98423** Otherwise fall through to the next instruction.
98424*/
98425/* Opcode: IdxLE P1 P2 P3 P4 *
98426** Synopsis: key=r[P3@P4]
98427**
98428** The P4 register values beginning with P3 form an unpacked index
98429** key that omits the PRIMARY KEY or ROWID. Compare this key value against
98430** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
98431** ROWID on the P1 index.
98432**
98433** If the P1 index entry is less than or equal to the key value then jump
98434** to P2. Otherwise fall through to the next instruction.
98435*/
98436case OP_IdxLE: /* jump, ncycle */
98437case OP_IdxGT: /* jump, ncycle */
98438case OP_IdxLT: /* jump, ncycle */
98439case OP_IdxGE: { /* jump, ncycle */
98440 VdbeCursor *pC;
98441 int res;
98442 UnpackedRecord r;
98443
98444 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98445 pC = p->apCsr[pOp->p1];
98446 assert( pC!=0 );
98447 assert( pC->isOrdered );
98448 assert( pC->eCurType==CURTYPE_BTREE );
98449 assert( pC->uc.pCursor!=0);
98450 assert( pC->deferredMoveto==0 );
98451 assert( pOp->p4type==P4_INT32 );
98452 r.pKeyInfo = pC->pKeyInfo;
98453 r.nField = (u16)pOp->p4.i;
98454 if( pOp->opcode<OP_IdxLT ){
98455 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
98456 r.default_rc = -1;
98457 }else{
98458 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
98459 r.default_rc = 0;
98460 }
98461 r.aMem = &aMem[pOp->p3];
98462#ifdef SQLITE_DEBUG
98463 {
98464 int i;
98465 for(i=0; i<r.nField; i++){
98466 assert( memIsValid(&r.aMem[i]) );
98467 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
98468 }
98469 }
98470#endif
98471
98472 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
98473 {
98474 i64 nCellKey = 0;
98475 BtCursor *pCur;
98476 Mem m;
98477
98478 assert( pC->eCurType==CURTYPE_BTREE );
98479 pCur = pC->uc.pCursor;
98480 assert( sqlite3BtreeCursorIsValid(pCur) );
98481 nCellKey = sqlite3BtreePayloadSize(pCur);
98482 /* nCellKey will always be between 0 and 0xffffffff because of the way
98483 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
98484 if( nCellKey<=0 || nCellKey>0x7fffffff ){
98485 rc = SQLITE_CORRUPT_BKPT;
98486 goto abort_due_to_error;
98487 }
98488 sqlite3VdbeMemInit(pMem: &m, db, flags: 0);
98489 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, amt: (u32)nCellKey, pMem: &m);
98490 if( rc ) goto abort_due_to_error;
98491 res = sqlite3VdbeRecordCompareWithSkip(nKey1: m.n, pKey1: m.z, pPKey2: &r, bSkip: 0);
98492 sqlite3VdbeMemReleaseMalloc(p: &m);
98493 }
98494 /* End of inlined sqlite3VdbeIdxKeyCompare() */
98495
98496 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
98497 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
98498 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
98499 res = -res;
98500 }else{
98501 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
98502 res++;
98503 }
98504 VdbeBranchTaken(res>0,2);
98505 assert( rc==SQLITE_OK );
98506 if( res>0 ) goto jump_to_p2;
98507 break;
98508}
98509
98510/* Opcode: Destroy P1 P2 P3 * *
98511**
98512** Delete an entire database table or index whose root page in the database
98513** file is given by P1.
98514**
98515** The table being destroyed is in the main database file if P3==0. If
98516** P3==1 then the table to be destroyed is in the auxiliary database file
98517** that is used to store tables create using CREATE TEMPORARY TABLE.
98518**
98519** If AUTOVACUUM is enabled then it is possible that another root page
98520** might be moved into the newly deleted root page in order to keep all
98521** root pages contiguous at the beginning of the database. The former
98522** value of the root page that moved - its value before the move occurred -
98523** is stored in register P2. If no page movement was required (because the
98524** table being dropped was already the last one in the database) then a
98525** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
98526** is stored in register P2.
98527**
98528** This opcode throws an error if there are any active reader VMs when
98529** it is invoked. This is done to avoid the difficulty associated with
98530** updating existing cursors when a root page is moved in an AUTOVACUUM
98531** database. This error is thrown even if the database is not an AUTOVACUUM
98532** db in order to avoid introducing an incompatibility between autovacuum
98533** and non-autovacuum modes.
98534**
98535** See also: Clear
98536*/
98537case OP_Destroy: { /* out2 */
98538 int iMoved;
98539 int iDb;
98540
98541 sqlite3VdbeIncrWriteCounter(p, 0);
98542 assert( p->readOnly==0 );
98543 assert( pOp->p1>1 );
98544 pOut = out2Prerelease(p, pOp);
98545 pOut->flags = MEM_Null;
98546 if( db->nVdbeRead > db->nVDestroy+1 ){
98547 rc = SQLITE_LOCKED;
98548 p->errorAction = OE_Abort;
98549 goto abort_due_to_error;
98550 }else{
98551 iDb = pOp->p3;
98552 assert( DbMaskTest(p->btreeMask, iDb) );
98553 iMoved = 0; /* Not needed. Only to silence a warning. */
98554 rc = sqlite3BtreeDropTable(p: db->aDb[iDb].pBt, iTable: pOp->p1, piMoved: &iMoved);
98555 pOut->flags = MEM_Int;
98556 pOut->u.i = iMoved;
98557 if( rc ) goto abort_due_to_error;
98558#ifndef SQLITE_OMIT_AUTOVACUUM
98559 if( iMoved!=0 ){
98560 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
98561 /* All OP_Destroy operations occur on the same btree */
98562 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
98563 resetSchemaOnFault = iDb+1;
98564 }
98565#endif
98566 }
98567 break;
98568}
98569
98570/* Opcode: Clear P1 P2 P3
98571**
98572** Delete all contents of the database table or index whose root page
98573** in the database file is given by P1. But, unlike Destroy, do not
98574** remove the table or index from the database file.
98575**
98576** The table being cleared is in the main database file if P2==0. If
98577** P2==1 then the table to be cleared is in the auxiliary database file
98578** that is used to store tables create using CREATE TEMPORARY TABLE.
98579**
98580** If the P3 value is non-zero, then the row change count is incremented
98581** by the number of rows in the table being cleared. If P3 is greater
98582** than zero, then the value stored in register P3 is also incremented
98583** by the number of rows in the table being cleared.
98584**
98585** See also: Destroy
98586*/
98587case OP_Clear: {
98588 i64 nChange;
98589
98590 sqlite3VdbeIncrWriteCounter(p, 0);
98591 nChange = 0;
98592 assert( p->readOnly==0 );
98593 assert( DbMaskTest(p->btreeMask, pOp->p2) );
98594 rc = sqlite3BtreeClearTable(p: db->aDb[pOp->p2].pBt, iTable: (u32)pOp->p1, pnChange: &nChange);
98595 if( pOp->p3 ){
98596 p->nChange += nChange;
98597 if( pOp->p3>0 ){
98598 assert( memIsValid(&aMem[pOp->p3]) );
98599 memAboutToChange(p, &aMem[pOp->p3]);
98600 aMem[pOp->p3].u.i += nChange;
98601 }
98602 }
98603 if( rc ) goto abort_due_to_error;
98604 break;
98605}
98606
98607/* Opcode: ResetSorter P1 * * * *
98608**
98609** Delete all contents from the ephemeral table or sorter
98610** that is open on cursor P1.
98611**
98612** This opcode only works for cursors used for sorting and
98613** opened with OP_OpenEphemeral or OP_SorterOpen.
98614*/
98615case OP_ResetSorter: {
98616 VdbeCursor *pC;
98617
98618 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98619 pC = p->apCsr[pOp->p1];
98620 assert( pC!=0 );
98621 if( isSorter(pC) ){
98622 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
98623 }else{
98624 assert( pC->eCurType==CURTYPE_BTREE );
98625 assert( pC->isEphemeral );
98626 rc = sqlite3BtreeClearTableOfCursor(pCur: pC->uc.pCursor);
98627 if( rc ) goto abort_due_to_error;
98628 }
98629 break;
98630}
98631
98632/* Opcode: CreateBtree P1 P2 P3 * *
98633** Synopsis: r[P2]=root iDb=P1 flags=P3
98634**
98635** Allocate a new b-tree in the main database file if P1==0 or in the
98636** TEMP database file if P1==1 or in an attached database if
98637** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
98638** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
98639** The root page number of the new b-tree is stored in register P2.
98640*/
98641case OP_CreateBtree: { /* out2 */
98642 Pgno pgno;
98643 Db *pDb;
98644
98645 sqlite3VdbeIncrWriteCounter(p, 0);
98646 pOut = out2Prerelease(p, pOp);
98647 pgno = 0;
98648 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
98649 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98650 assert( DbMaskTest(p->btreeMask, pOp->p1) );
98651 assert( p->readOnly==0 );
98652 pDb = &db->aDb[pOp->p1];
98653 assert( pDb->pBt!=0 );
98654 rc = sqlite3BtreeCreateTable(p: pDb->pBt, piTable: &pgno, flags: pOp->p3);
98655 if( rc ) goto abort_due_to_error;
98656 pOut->u.i = pgno;
98657 break;
98658}
98659
98660/* Opcode: SqlExec * * * P4 *
98661**
98662** Run the SQL statement or statements specified in the P4 string.
98663*/
98664case OP_SqlExec: {
98665 sqlite3VdbeIncrWriteCounter(p, 0);
98666 db->nSqlExec++;
98667 rc = sqlite3_exec(db, sql: pOp->p4.z, callback: 0, 0, errmsg: 0);
98668 db->nSqlExec--;
98669 if( rc ) goto abort_due_to_error;
98670 break;
98671}
98672
98673/* Opcode: ParseSchema P1 * * P4 *
98674**
98675** Read and parse all entries from the schema table of database P1
98676** that match the WHERE clause P4. If P4 is a NULL pointer, then the
98677** entire schema for P1 is reparsed.
98678**
98679** This opcode invokes the parser to create a new virtual machine,
98680** then runs the new virtual machine. It is thus a re-entrant opcode.
98681*/
98682case OP_ParseSchema: {
98683 int iDb;
98684 const char *zSchema;
98685 char *zSql;
98686 InitData initData;
98687
98688 /* Any prepared statement that invokes this opcode will hold mutexes
98689 ** on every btree. This is a prerequisite for invoking
98690 ** sqlite3InitCallback().
98691 */
98692#ifdef SQLITE_DEBUG
98693 for(iDb=0; iDb<db->nDb; iDb++){
98694 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
98695 }
98696#endif
98697
98698 iDb = pOp->p1;
98699 assert( iDb>=0 && iDb<db->nDb );
98700 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)
98701 || db->mallocFailed
98702 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
98703
98704#ifndef SQLITE_OMIT_ALTERTABLE
98705 if( pOp->p4.z==0 ){
98706 sqlite3SchemaClear(db->aDb[iDb].pSchema);
98707 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
98708 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
98709 db->mDbFlags |= DBFLAG_SchemaChange;
98710 p->expired = 0;
98711 }else
98712#endif
98713 {
98714 zSchema = LEGACY_SCHEMA_TABLE;
98715 initData.db = db;
98716 initData.iDb = iDb;
98717 initData.pzErrMsg = &p->zErrMsg;
98718 initData.mInitFlags = 0;
98719 initData.mxPage = sqlite3BtreeLastPage(p: db->aDb[iDb].pBt);
98720 zSql = sqlite3MPrintf(db,
98721 zFormat: "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
98722 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
98723 if( zSql==0 ){
98724 rc = SQLITE_NOMEM_BKPT;
98725 }else{
98726 assert( db->init.busy==0 );
98727 db->init.busy = 1;
98728 initData.rc = SQLITE_OK;
98729 initData.nInitRow = 0;
98730 assert( !db->mallocFailed );
98731 rc = sqlite3_exec(db, sql: zSql, callback: sqlite3InitCallback, &initData, errmsg: 0);
98732 if( rc==SQLITE_OK ) rc = initData.rc;
98733 if( rc==SQLITE_OK && initData.nInitRow==0 ){
98734 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
98735 ** at least one SQL statement. Any less than that indicates that
98736 ** the sqlite_schema table is corrupt. */
98737 rc = SQLITE_CORRUPT_BKPT;
98738 }
98739 sqlite3DbFreeNN(db, p: zSql);
98740 db->init.busy = 0;
98741 }
98742 }
98743 if( rc ){
98744 sqlite3ResetAllSchemasOfConnection(db);
98745 if( rc==SQLITE_NOMEM ){
98746 goto no_mem;
98747 }
98748 goto abort_due_to_error;
98749 }
98750 break;
98751}
98752
98753#if !defined(SQLITE_OMIT_ANALYZE)
98754/* Opcode: LoadAnalysis P1 * * * *
98755**
98756** Read the sqlite_stat1 table for database P1 and load the content
98757** of that table into the internal index hash table. This will cause
98758** the analysis to be used when preparing all subsequent queries.
98759*/
98760case OP_LoadAnalysis: {
98761 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98762 rc = sqlite3AnalysisLoad(db, iDB: pOp->p1);
98763 if( rc ) goto abort_due_to_error;
98764 break;
98765}
98766#endif /* !defined(SQLITE_OMIT_ANALYZE) */
98767
98768/* Opcode: DropTable P1 * * P4 *
98769**
98770** Remove the internal (in-memory) data structures that describe
98771** the table named P4 in database P1. This is called after a table
98772** is dropped from disk (using the Destroy opcode) in order to keep
98773** the internal representation of the
98774** schema consistent with what is on disk.
98775*/
98776case OP_DropTable: {
98777 sqlite3VdbeIncrWriteCounter(p, 0);
98778 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
98779 break;
98780}
98781
98782/* Opcode: DropIndex P1 * * P4 *
98783**
98784** Remove the internal (in-memory) data structures that describe
98785** the index named P4 in database P1. This is called after an index
98786** is dropped from disk (using the Destroy opcode)
98787** in order to keep the internal representation of the
98788** schema consistent with what is on disk.
98789*/
98790case OP_DropIndex: {
98791 sqlite3VdbeIncrWriteCounter(p, 0);
98792 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
98793 break;
98794}
98795
98796/* Opcode: DropTrigger P1 * * P4 *
98797**
98798** Remove the internal (in-memory) data structures that describe
98799** the trigger named P4 in database P1. This is called after a trigger
98800** is dropped from disk (using the Destroy opcode) in order to keep
98801** the internal representation of the
98802** schema consistent with what is on disk.
98803*/
98804case OP_DropTrigger: {
98805 sqlite3VdbeIncrWriteCounter(p, 0);
98806 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
98807 break;
98808}
98809
98810
98811#ifndef SQLITE_OMIT_INTEGRITY_CHECK
98812/* Opcode: IntegrityCk P1 P2 P3 P4 P5
98813**
98814** Do an analysis of the currently open database. Store in
98815** register P1 the text of an error message describing any problems.
98816** If no problems are found, store a NULL in register P1.
98817**
98818** The register P3 contains one less than the maximum number of allowed errors.
98819** At most reg(P3) errors will be reported.
98820** In other words, the analysis stops as soon as reg(P1) errors are
98821** seen. Reg(P1) is updated with the number of errors remaining.
98822**
98823** The root page numbers of all tables in the database are integers
98824** stored in P4_INTARRAY argument.
98825**
98826** If P5 is not zero, the check is done on the auxiliary database
98827** file, not the main database file.
98828**
98829** This opcode is used to implement the integrity_check pragma.
98830*/
98831case OP_IntegrityCk: {
98832 int nRoot; /* Number of tables to check. (Number of root pages.) */
98833 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
98834 int nErr; /* Number of errors reported */
98835 char *z; /* Text of the error report */
98836 Mem *pnErr; /* Register keeping track of errors remaining */
98837
98838 assert( p->bIsReader );
98839 nRoot = pOp->p2;
98840 aRoot = pOp->p4.ai;
98841 assert( nRoot>0 );
98842 assert( aRoot[0]==(Pgno)nRoot );
98843 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
98844 pnErr = &aMem[pOp->p3];
98845 assert( (pnErr->flags & MEM_Int)!=0 );
98846 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
98847 pIn1 = &aMem[pOp->p1];
98848 assert( pOp->p5<db->nDb );
98849 assert( DbMaskTest(p->btreeMask, pOp->p5) );
98850 rc = sqlite3BtreeIntegrityCheck(db, p: db->aDb[pOp->p5].pBt, aRoot: &aRoot[1], nRoot,
98851 mxErr: (int)pnErr->u.i+1, pnErr: &nErr, pzOut: &z);
98852 sqlite3VdbeMemSetNull(pMem: pIn1);
98853 if( nErr==0 ){
98854 assert( z==0 );
98855 }else if( rc ){
98856 sqlite3_free(p: z);
98857 goto abort_due_to_error;
98858 }else{
98859 pnErr->u.i -= nErr-1;
98860 sqlite3VdbeMemSetStr(pMem: pIn1, z, n: -1, SQLITE_UTF8, xDel: sqlite3_free);
98861 }
98862 UPDATE_MAX_BLOBSIZE(pIn1);
98863 sqlite3VdbeChangeEncoding(pMem: pIn1, desiredEnc: encoding);
98864 goto check_for_interrupt;
98865}
98866#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
98867
98868/* Opcode: RowSetAdd P1 P2 * * *
98869** Synopsis: rowset(P1)=r[P2]
98870**
98871** Insert the integer value held by register P2 into a RowSet object
98872** held in register P1.
98873**
98874** An assertion fails if P2 is not an integer.
98875*/
98876case OP_RowSetAdd: { /* in1, in2 */
98877 pIn1 = &aMem[pOp->p1];
98878 pIn2 = &aMem[pOp->p2];
98879 assert( (pIn2->flags & MEM_Int)!=0 );
98880 if( (pIn1->flags & MEM_Blob)==0 ){
98881 if( sqlite3VdbeMemSetRowSet(pMem: pIn1) ) goto no_mem;
98882 }
98883 assert( sqlite3VdbeMemIsRowSet(pIn1) );
98884 sqlite3RowSetInsert(p: (RowSet*)pIn1->z, rowid: pIn2->u.i);
98885 break;
98886}
98887
98888/* Opcode: RowSetRead P1 P2 P3 * *
98889** Synopsis: r[P3]=rowset(P1)
98890**
98891** Extract the smallest value from the RowSet object in P1
98892** and put that value into register P3.
98893** Or, if RowSet object P1 is initially empty, leave P3
98894** unchanged and jump to instruction P2.
98895*/
98896case OP_RowSetRead: { /* jump, in1, out3 */
98897 i64 val;
98898
98899 pIn1 = &aMem[pOp->p1];
98900 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
98901 if( (pIn1->flags & MEM_Blob)==0
98902 || sqlite3RowSetNext(p: (RowSet*)pIn1->z, pRowid: &val)==0
98903 ){
98904 /* The boolean index is empty */
98905 sqlite3VdbeMemSetNull(pMem: pIn1);
98906 VdbeBranchTaken(1,2);
98907 goto jump_to_p2_and_check_for_interrupt;
98908 }else{
98909 /* A value was pulled from the index */
98910 VdbeBranchTaken(0,2);
98911 sqlite3VdbeMemSetInt64(pMem: &aMem[pOp->p3], val);
98912 }
98913 goto check_for_interrupt;
98914}
98915
98916/* Opcode: RowSetTest P1 P2 P3 P4
98917** Synopsis: if r[P3] in rowset(P1) goto P2
98918**
98919** Register P3 is assumed to hold a 64-bit integer value. If register P1
98920** contains a RowSet object and that RowSet object contains
98921** the value held in P3, jump to register P2. Otherwise, insert the
98922** integer in P3 into the RowSet and continue on to the
98923** next opcode.
98924**
98925** The RowSet object is optimized for the case where sets of integers
98926** are inserted in distinct phases, which each set contains no duplicates.
98927** Each set is identified by a unique P4 value. The first set
98928** must have P4==0, the final set must have P4==-1, and for all other sets
98929** must have P4>0.
98930**
98931** This allows optimizations: (a) when P4==0 there is no need to test
98932** the RowSet object for P3, as it is guaranteed not to contain it,
98933** (b) when P4==-1 there is no need to insert the value, as it will
98934** never be tested for, and (c) when a value that is part of set X is
98935** inserted, there is no need to search to see if the same value was
98936** previously inserted as part of set X (only if it was previously
98937** inserted as part of some other set).
98938*/
98939case OP_RowSetTest: { /* jump, in1, in3 */
98940 int iSet;
98941 int exists;
98942
98943 pIn1 = &aMem[pOp->p1];
98944 pIn3 = &aMem[pOp->p3];
98945 iSet = pOp->p4.i;
98946 assert( pIn3->flags&MEM_Int );
98947
98948 /* If there is anything other than a rowset object in memory cell P1,
98949 ** delete it now and initialize P1 with an empty rowset
98950 */
98951 if( (pIn1->flags & MEM_Blob)==0 ){
98952 if( sqlite3VdbeMemSetRowSet(pMem: pIn1) ) goto no_mem;
98953 }
98954 assert( sqlite3VdbeMemIsRowSet(pIn1) );
98955 assert( pOp->p4type==P4_INT32 );
98956 assert( iSet==-1 || iSet>=0 );
98957 if( iSet ){
98958 exists = sqlite3RowSetTest(pRowSet: (RowSet*)pIn1->z, iBatch: iSet, iRowid: pIn3->u.i);
98959 VdbeBranchTaken(exists!=0,2);
98960 if( exists ) goto jump_to_p2;
98961 }
98962 if( iSet>=0 ){
98963 sqlite3RowSetInsert(p: (RowSet*)pIn1->z, rowid: pIn3->u.i);
98964 }
98965 break;
98966}
98967
98968
98969#ifndef SQLITE_OMIT_TRIGGER
98970
98971/* Opcode: Program P1 P2 P3 P4 P5
98972**
98973** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
98974**
98975** P1 contains the address of the memory cell that contains the first memory
98976** cell in an array of values used as arguments to the sub-program. P2
98977** contains the address to jump to if the sub-program throws an IGNORE
98978** exception using the RAISE() function. Register P3 contains the address
98979** of a memory cell in this (the parent) VM that is used to allocate the
98980** memory required by the sub-vdbe at runtime.
98981**
98982** P4 is a pointer to the VM containing the trigger program.
98983**
98984** If P5 is non-zero, then recursive program invocation is enabled.
98985*/
98986case OP_Program: { /* jump */
98987 int nMem; /* Number of memory registers for sub-program */
98988 int nByte; /* Bytes of runtime space required for sub-program */
98989 Mem *pRt; /* Register to allocate runtime space */
98990 Mem *pMem; /* Used to iterate through memory cells */
98991 Mem *pEnd; /* Last memory cell in new array */
98992 VdbeFrame *pFrame; /* New vdbe frame to execute in */
98993 SubProgram *pProgram; /* Sub-program to execute */
98994 void *t; /* Token identifying trigger */
98995
98996 pProgram = pOp->p4.pProgram;
98997 pRt = &aMem[pOp->p3];
98998 assert( pProgram->nOp>0 );
98999
99000 /* If the p5 flag is clear, then recursive invocation of triggers is
99001 ** disabled for backwards compatibility (p5 is set if this sub-program
99002 ** is really a trigger, not a foreign key action, and the flag set
99003 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
99004 **
99005 ** It is recursive invocation of triggers, at the SQL level, that is
99006 ** disabled. In some cases a single trigger may generate more than one
99007 ** SubProgram (if the trigger may be executed with more than one different
99008 ** ON CONFLICT algorithm). SubProgram structures associated with a
99009 ** single trigger all have the same value for the SubProgram.token
99010 ** variable. */
99011 if( pOp->p5 ){
99012 t = pProgram->token;
99013 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
99014 if( pFrame ) break;
99015 }
99016
99017 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
99018 rc = SQLITE_ERROR;
99019 sqlite3VdbeError(p, zFormat: "too many levels of trigger recursion");
99020 goto abort_due_to_error;
99021 }
99022
99023 /* Register pRt is used to store the memory required to save the state
99024 ** of the current program, and the memory required at runtime to execute
99025 ** the trigger program. If this trigger has been fired before, then pRt
99026 ** is already allocated. Otherwise, it must be initialized. */
99027 if( (pRt->flags&MEM_Blob)==0 ){
99028 /* SubProgram.nMem is set to the number of memory cells used by the
99029 ** program stored in SubProgram.aOp. As well as these, one memory
99030 ** cell is required for each cursor used by the program. Set local
99031 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
99032 */
99033 nMem = pProgram->nMem + pProgram->nCsr;
99034 assert( nMem>0 );
99035 if( pProgram->nCsr==0 ) nMem++;
99036 nByte = ROUND8(sizeof(VdbeFrame))
99037 + nMem * sizeof(Mem)
99038 + pProgram->nCsr * sizeof(VdbeCursor*)
99039 + (pProgram->nOp + 7)/8;
99040 pFrame = sqlite3DbMallocZero(db, n: nByte);
99041 if( !pFrame ){
99042 goto no_mem;
99043 }
99044 sqlite3VdbeMemRelease(p: pRt);
99045 pRt->flags = MEM_Blob|MEM_Dyn;
99046 pRt->z = (char*)pFrame;
99047 pRt->n = nByte;
99048 pRt->xDel = sqlite3VdbeFrameMemDel;
99049
99050 pFrame->v = p;
99051 pFrame->nChildMem = nMem;
99052 pFrame->nChildCsr = pProgram->nCsr;
99053 pFrame->pc = (int)(pOp - aOp);
99054 pFrame->aMem = p->aMem;
99055 pFrame->nMem = p->nMem;
99056 pFrame->apCsr = p->apCsr;
99057 pFrame->nCursor = p->nCursor;
99058 pFrame->aOp = p->aOp;
99059 pFrame->nOp = p->nOp;
99060 pFrame->token = pProgram->token;
99061#ifdef SQLITE_DEBUG
99062 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
99063#endif
99064
99065 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
99066 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
99067 pMem->flags = MEM_Undefined;
99068 pMem->db = db;
99069 }
99070 }else{
99071 pFrame = (VdbeFrame*)pRt->z;
99072 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
99073 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
99074 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
99075 assert( pProgram->nCsr==pFrame->nChildCsr );
99076 assert( (int)(pOp - aOp)==pFrame->pc );
99077 }
99078
99079 p->nFrame++;
99080 pFrame->pParent = p->pFrame;
99081 pFrame->lastRowid = db->lastRowid;
99082 pFrame->nChange = p->nChange;
99083 pFrame->nDbChange = p->db->nChange;
99084 assert( pFrame->pAuxData==0 );
99085 pFrame->pAuxData = p->pAuxData;
99086 p->pAuxData = 0;
99087 p->nChange = 0;
99088 p->pFrame = pFrame;
99089 p->aMem = aMem = VdbeFrameMem(pFrame);
99090 p->nMem = pFrame->nChildMem;
99091 p->nCursor = (u16)pFrame->nChildCsr;
99092 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
99093 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
99094 memset(s: pFrame->aOnce, c: 0, n: (pProgram->nOp + 7)/8);
99095 p->aOp = aOp = pProgram->aOp;
99096 p->nOp = pProgram->nOp;
99097#ifdef SQLITE_DEBUG
99098 /* Verify that second and subsequent executions of the same trigger do not
99099 ** try to reuse register values from the first use. */
99100 {
99101 int i;
99102 for(i=0; i<p->nMem; i++){
99103 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
99104 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
99105 }
99106 }
99107#endif
99108 pOp = &aOp[-1];
99109 goto check_for_interrupt;
99110}
99111
99112/* Opcode: Param P1 P2 * * *
99113**
99114** This opcode is only ever present in sub-programs called via the
99115** OP_Program instruction. Copy a value currently stored in a memory
99116** cell of the calling (parent) frame to cell P2 in the current frames
99117** address space. This is used by trigger programs to access the new.*
99118** and old.* values.
99119**
99120** The address of the cell in the parent frame is determined by adding
99121** the value of the P1 argument to the value of the P1 argument to the
99122** calling OP_Program instruction.
99123*/
99124case OP_Param: { /* out2 */
99125 VdbeFrame *pFrame;
99126 Mem *pIn;
99127 pOut = out2Prerelease(p, pOp);
99128 pFrame = p->pFrame;
99129 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
99130 sqlite3VdbeMemShallowCopy(pTo: pOut, pFrom: pIn, MEM_Ephem);
99131 break;
99132}
99133
99134#endif /* #ifndef SQLITE_OMIT_TRIGGER */
99135
99136#ifndef SQLITE_OMIT_FOREIGN_KEY
99137/* Opcode: FkCounter P1 P2 * * *
99138** Synopsis: fkctr[P1]+=P2
99139**
99140** Increment a "constraint counter" by P2 (P2 may be negative or positive).
99141** If P1 is non-zero, the database constraint counter is incremented
99142** (deferred foreign key constraints). Otherwise, if P1 is zero, the
99143** statement counter is incremented (immediate foreign key constraints).
99144*/
99145case OP_FkCounter: {
99146 if( db->flags & SQLITE_DeferFKs ){
99147 db->nDeferredImmCons += pOp->p2;
99148 }else if( pOp->p1 ){
99149 db->nDeferredCons += pOp->p2;
99150 }else{
99151 p->nFkConstraint += pOp->p2;
99152 }
99153 break;
99154}
99155
99156/* Opcode: FkIfZero P1 P2 * * *
99157** Synopsis: if fkctr[P1]==0 goto P2
99158**
99159** This opcode tests if a foreign key constraint-counter is currently zero.
99160** If so, jump to instruction P2. Otherwise, fall through to the next
99161** instruction.
99162**
99163** If P1 is non-zero, then the jump is taken if the database constraint-counter
99164** is zero (the one that counts deferred constraint violations). If P1 is
99165** zero, the jump is taken if the statement constraint-counter is zero
99166** (immediate foreign key constraint violations).
99167*/
99168case OP_FkIfZero: { /* jump */
99169 if( pOp->p1 ){
99170 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
99171 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
99172 }else{
99173 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
99174 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
99175 }
99176 break;
99177}
99178#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
99179
99180#ifndef SQLITE_OMIT_AUTOINCREMENT
99181/* Opcode: MemMax P1 P2 * * *
99182** Synopsis: r[P1]=max(r[P1],r[P2])
99183**
99184** P1 is a register in the root frame of this VM (the root frame is
99185** different from the current frame if this instruction is being executed
99186** within a sub-program). Set the value of register P1 to the maximum of
99187** its current value and the value in register P2.
99188**
99189** This instruction throws an error if the memory cell is not initially
99190** an integer.
99191*/
99192case OP_MemMax: { /* in2 */
99193 VdbeFrame *pFrame;
99194 if( p->pFrame ){
99195 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
99196 pIn1 = &pFrame->aMem[pOp->p1];
99197 }else{
99198 pIn1 = &aMem[pOp->p1];
99199 }
99200 assert( memIsValid(pIn1) );
99201 sqlite3VdbeMemIntegerify(pMem: pIn1);
99202 pIn2 = &aMem[pOp->p2];
99203 sqlite3VdbeMemIntegerify(pMem: pIn2);
99204 if( pIn1->u.i<pIn2->u.i){
99205 pIn1->u.i = pIn2->u.i;
99206 }
99207 break;
99208}
99209#endif /* SQLITE_OMIT_AUTOINCREMENT */
99210
99211/* Opcode: IfPos P1 P2 P3 * *
99212** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
99213**
99214** Register P1 must contain an integer.
99215** If the value of register P1 is 1 or greater, subtract P3 from the
99216** value in P1 and jump to P2.
99217**
99218** If the initial value of register P1 is less than 1, then the
99219** value is unchanged and control passes through to the next instruction.
99220*/
99221case OP_IfPos: { /* jump, in1 */
99222 pIn1 = &aMem[pOp->p1];
99223 assert( pIn1->flags&MEM_Int );
99224 VdbeBranchTaken( pIn1->u.i>0, 2);
99225 if( pIn1->u.i>0 ){
99226 pIn1->u.i -= pOp->p3;
99227 goto jump_to_p2;
99228 }
99229 break;
99230}
99231
99232/* Opcode: OffsetLimit P1 P2 P3 * *
99233** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
99234**
99235** This opcode performs a commonly used computation associated with
99236** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
99237** holds the offset counter. The opcode computes the combined value
99238** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
99239** value computed is the total number of rows that will need to be
99240** visited in order to complete the query.
99241**
99242** If r[P3] is zero or negative, that means there is no OFFSET
99243** and r[P2] is set to be the value of the LIMIT, r[P1].
99244**
99245** if r[P1] is zero or negative, that means there is no LIMIT
99246** and r[P2] is set to -1.
99247**
99248** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
99249*/
99250case OP_OffsetLimit: { /* in1, out2, in3 */
99251 i64 x;
99252 pIn1 = &aMem[pOp->p1];
99253 pIn3 = &aMem[pOp->p3];
99254 pOut = out2Prerelease(p, pOp);
99255 assert( pIn1->flags & MEM_Int );
99256 assert( pIn3->flags & MEM_Int );
99257 x = pIn1->u.i;
99258 if( x<=0 || sqlite3AddInt64(pA: &x, iB: pIn3->u.i>0?pIn3->u.i:0) ){
99259 /* If the LIMIT is less than or equal to zero, loop forever. This
99260 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
99261 ** also loop forever. This is undocumented. In fact, one could argue
99262 ** that the loop should terminate. But assuming 1 billion iterations
99263 ** per second (far exceeding the capabilities of any current hardware)
99264 ** it would take nearly 300 years to actually reach the limit. So
99265 ** looping forever is a reasonable approximation. */
99266 pOut->u.i = -1;
99267 }else{
99268 pOut->u.i = x;
99269 }
99270 break;
99271}
99272
99273/* Opcode: IfNotZero P1 P2 * * *
99274** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
99275**
99276** Register P1 must contain an integer. If the content of register P1 is
99277** initially greater than zero, then decrement the value in register P1.
99278** If it is non-zero (negative or positive) and then also jump to P2.
99279** If register P1 is initially zero, leave it unchanged and fall through.
99280*/
99281case OP_IfNotZero: { /* jump, in1 */
99282 pIn1 = &aMem[pOp->p1];
99283 assert( pIn1->flags&MEM_Int );
99284 VdbeBranchTaken(pIn1->u.i<0, 2);
99285 if( pIn1->u.i ){
99286 if( pIn1->u.i>0 ) pIn1->u.i--;
99287 goto jump_to_p2;
99288 }
99289 break;
99290}
99291
99292/* Opcode: DecrJumpZero P1 P2 * * *
99293** Synopsis: if (--r[P1])==0 goto P2
99294**
99295** Register P1 must hold an integer. Decrement the value in P1
99296** and jump to P2 if the new value is exactly zero.
99297*/
99298case OP_DecrJumpZero: { /* jump, in1 */
99299 pIn1 = &aMem[pOp->p1];
99300 assert( pIn1->flags&MEM_Int );
99301 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
99302 VdbeBranchTaken(pIn1->u.i==0, 2);
99303 if( pIn1->u.i==0 ) goto jump_to_p2;
99304 break;
99305}
99306
99307
99308/* Opcode: AggStep * P2 P3 P4 P5
99309** Synopsis: accum=r[P3] step(r[P2@P5])
99310**
99311** Execute the xStep function for an aggregate.
99312** The function has P5 arguments. P4 is a pointer to the
99313** FuncDef structure that specifies the function. Register P3 is the
99314** accumulator.
99315**
99316** The P5 arguments are taken from register P2 and its
99317** successors.
99318*/
99319/* Opcode: AggInverse * P2 P3 P4 P5
99320** Synopsis: accum=r[P3] inverse(r[P2@P5])
99321**
99322** Execute the xInverse function for an aggregate.
99323** The function has P5 arguments. P4 is a pointer to the
99324** FuncDef structure that specifies the function. Register P3 is the
99325** accumulator.
99326**
99327** The P5 arguments are taken from register P2 and its
99328** successors.
99329*/
99330/* Opcode: AggStep1 P1 P2 P3 P4 P5
99331** Synopsis: accum=r[P3] step(r[P2@P5])
99332**
99333** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
99334** aggregate. The function has P5 arguments. P4 is a pointer to the
99335** FuncDef structure that specifies the function. Register P3 is the
99336** accumulator.
99337**
99338** The P5 arguments are taken from register P2 and its
99339** successors.
99340**
99341** This opcode is initially coded as OP_AggStep0. On first evaluation,
99342** the FuncDef stored in P4 is converted into an sqlite3_context and
99343** the opcode is changed. In this way, the initialization of the
99344** sqlite3_context only happens once, instead of on each call to the
99345** step function.
99346*/
99347case OP_AggInverse:
99348case OP_AggStep: {
99349 int n;
99350 sqlite3_context *pCtx;
99351
99352 assert( pOp->p4type==P4_FUNCDEF );
99353 n = pOp->p5;
99354 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
99355 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
99356 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
99357 pCtx = sqlite3DbMallocRawNN(db, n: n*sizeof(sqlite3_value*) +
99358 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
99359 if( pCtx==0 ) goto no_mem;
99360 pCtx->pMem = 0;
99361 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
99362 sqlite3VdbeMemInit(pMem: pCtx->pOut, db, MEM_Null);
99363 pCtx->pFunc = pOp->p4.pFunc;
99364 pCtx->iOp = (int)(pOp - aOp);
99365 pCtx->pVdbe = p;
99366 pCtx->skipFlag = 0;
99367 pCtx->isError = 0;
99368 pCtx->enc = encoding;
99369 pCtx->argc = n;
99370 pOp->p4type = P4_FUNCCTX;
99371 pOp->p4.pCtx = pCtx;
99372
99373 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
99374 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
99375
99376 pOp->opcode = OP_AggStep1;
99377 /* Fall through into OP_AggStep */
99378 /* no break */ deliberate_fall_through
99379}
99380case OP_AggStep1: {
99381 int i;
99382 sqlite3_context *pCtx;
99383 Mem *pMem;
99384
99385 assert( pOp->p4type==P4_FUNCCTX );
99386 pCtx = pOp->p4.pCtx;
99387 pMem = &aMem[pOp->p3];
99388
99389#ifdef SQLITE_DEBUG
99390 if( pOp->p1 ){
99391 /* This is an OP_AggInverse call. Verify that xStep has always
99392 ** been called at least once prior to any xInverse call. */
99393 assert( pMem->uTemp==0x1122e0e3 );
99394 }else{
99395 /* This is an OP_AggStep call. Mark it as such. */
99396 pMem->uTemp = 0x1122e0e3;
99397 }
99398#endif
99399
99400 /* If this function is inside of a trigger, the register array in aMem[]
99401 ** might change from one evaluation to the next. The next block of code
99402 ** checks to see if the register array has changed, and if so it
99403 ** reinitializes the relevant parts of the sqlite3_context object */
99404 if( pCtx->pMem != pMem ){
99405 pCtx->pMem = pMem;
99406 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
99407 }
99408
99409#ifdef SQLITE_DEBUG
99410 for(i=0; i<pCtx->argc; i++){
99411 assert( memIsValid(pCtx->argv[i]) );
99412 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
99413 }
99414#endif
99415
99416 pMem->n++;
99417 assert( pCtx->pOut->flags==MEM_Null );
99418 assert( pCtx->isError==0 );
99419 assert( pCtx->skipFlag==0 );
99420#ifndef SQLITE_OMIT_WINDOWFUNC
99421 if( pOp->p1 ){
99422 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
99423 }else
99424#endif
99425 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
99426
99427 if( pCtx->isError ){
99428 if( pCtx->isError>0 ){
99429 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pCtx->pOut));
99430 rc = pCtx->isError;
99431 }
99432 if( pCtx->skipFlag ){
99433 assert( pOp[-1].opcode==OP_CollSeq );
99434 i = pOp[-1].p1;
99435 if( i ) sqlite3VdbeMemSetInt64(pMem: &aMem[i], val: 1);
99436 pCtx->skipFlag = 0;
99437 }
99438 sqlite3VdbeMemRelease(p: pCtx->pOut);
99439 pCtx->pOut->flags = MEM_Null;
99440 pCtx->isError = 0;
99441 if( rc ) goto abort_due_to_error;
99442 }
99443 assert( pCtx->pOut->flags==MEM_Null );
99444 assert( pCtx->skipFlag==0 );
99445 break;
99446}
99447
99448/* Opcode: AggFinal P1 P2 * P4 *
99449** Synopsis: accum=r[P1] N=P2
99450**
99451** P1 is the memory location that is the accumulator for an aggregate
99452** or window function. Execute the finalizer function
99453** for an aggregate and store the result in P1.
99454**
99455** P2 is the number of arguments that the step function takes and
99456** P4 is a pointer to the FuncDef for this function. The P2
99457** argument is not used by this opcode. It is only there to disambiguate
99458** functions that can take varying numbers of arguments. The
99459** P4 argument is only needed for the case where
99460** the step function was not previously called.
99461*/
99462/* Opcode: AggValue * P2 P3 P4 *
99463** Synopsis: r[P3]=value N=P2
99464**
99465** Invoke the xValue() function and store the result in register P3.
99466**
99467** P2 is the number of arguments that the step function takes and
99468** P4 is a pointer to the FuncDef for this function. The P2
99469** argument is not used by this opcode. It is only there to disambiguate
99470** functions that can take varying numbers of arguments. The
99471** P4 argument is only needed for the case where
99472** the step function was not previously called.
99473*/
99474case OP_AggValue:
99475case OP_AggFinal: {
99476 Mem *pMem;
99477 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
99478 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
99479 pMem = &aMem[pOp->p1];
99480 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
99481#ifndef SQLITE_OMIT_WINDOWFUNC
99482 if( pOp->p3 ){
99483 memAboutToChange(p, &aMem[pOp->p3]);
99484 rc = sqlite3VdbeMemAggValue(pAccum: pMem, pOut: &aMem[pOp->p3], pFunc: pOp->p4.pFunc);
99485 pMem = &aMem[pOp->p3];
99486 }else
99487#endif
99488 {
99489 rc = sqlite3VdbeMemFinalize(pMem, pFunc: pOp->p4.pFunc);
99490 }
99491
99492 if( rc ){
99493 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pMem));
99494 goto abort_due_to_error;
99495 }
99496 sqlite3VdbeChangeEncoding(pMem, desiredEnc: encoding);
99497 UPDATE_MAX_BLOBSIZE(pMem);
99498 REGISTER_TRACE((int)(pMem-aMem), pMem);
99499 break;
99500}
99501
99502#ifndef SQLITE_OMIT_WAL
99503/* Opcode: Checkpoint P1 P2 P3 * *
99504**
99505** Checkpoint database P1. This is a no-op if P1 is not currently in
99506** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
99507** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
99508** SQLITE_BUSY or not, respectively. Write the number of pages in the
99509** WAL after the checkpoint into mem[P3+1] and the number of pages
99510** in the WAL that have been checkpointed after the checkpoint
99511** completes into mem[P3+2]. However on an error, mem[P3+1] and
99512** mem[P3+2] are initialized to -1.
99513*/
99514case OP_Checkpoint: {
99515 int i; /* Loop counter */
99516 int aRes[3]; /* Results */
99517 Mem *pMem; /* Write results here */
99518
99519 assert( p->readOnly==0 );
99520 aRes[0] = 0;
99521 aRes[1] = aRes[2] = -1;
99522 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
99523 || pOp->p2==SQLITE_CHECKPOINT_FULL
99524 || pOp->p2==SQLITE_CHECKPOINT_RESTART
99525 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
99526 );
99527 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
99528 if( rc ){
99529 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
99530 rc = SQLITE_OK;
99531 aRes[0] = 1;
99532 }
99533 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
99534 sqlite3VdbeMemSetInt64(pMem, val: (i64)aRes[i]);
99535 }
99536 break;
99537};
99538#endif
99539
99540#ifndef SQLITE_OMIT_PRAGMA
99541/* Opcode: JournalMode P1 P2 P3 * *
99542**
99543** Change the journal mode of database P1 to P3. P3 must be one of the
99544** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
99545** modes (delete, truncate, persist, off and memory), this is a simple
99546** operation. No IO is required.
99547**
99548** If changing into or out of WAL mode the procedure is more complicated.
99549**
99550** Write a string containing the final journal-mode to register P2.
99551*/
99552case OP_JournalMode: { /* out2 */
99553 Btree *pBt; /* Btree to change journal mode of */
99554 Pager *pPager; /* Pager associated with pBt */
99555 int eNew; /* New journal mode */
99556 int eOld; /* The old journal mode */
99557#ifndef SQLITE_OMIT_WAL
99558 const char *zFilename; /* Name of database file for pPager */
99559#endif
99560
99561 pOut = out2Prerelease(p, pOp);
99562 eNew = pOp->p3;
99563 assert( eNew==PAGER_JOURNALMODE_DELETE
99564 || eNew==PAGER_JOURNALMODE_TRUNCATE
99565 || eNew==PAGER_JOURNALMODE_PERSIST
99566 || eNew==PAGER_JOURNALMODE_OFF
99567 || eNew==PAGER_JOURNALMODE_MEMORY
99568 || eNew==PAGER_JOURNALMODE_WAL
99569 || eNew==PAGER_JOURNALMODE_QUERY
99570 );
99571 assert( pOp->p1>=0 && pOp->p1<db->nDb );
99572 assert( p->readOnly==0 );
99573
99574 pBt = db->aDb[pOp->p1].pBt;
99575 pPager = sqlite3BtreePager(p: pBt);
99576 eOld = sqlite3PagerGetJournalMode(pPager);
99577 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
99578 assert( sqlite3BtreeHoldsMutex(pBt) );
99579 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
99580
99581#ifndef SQLITE_OMIT_WAL
99582 zFilename = sqlite3PagerFilename(pPager, nullIfMemDb: 1);
99583
99584 /* Do not allow a transition to journal_mode=WAL for a database
99585 ** in temporary storage or if the VFS does not support shared memory
99586 */
99587 if( eNew==PAGER_JOURNALMODE_WAL
99588 && (sqlite3Strlen30(z: zFilename)==0 /* Temp file */
99589 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
99590 ){
99591 eNew = eOld;
99592 }
99593
99594 if( (eNew!=eOld)
99595 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
99596 ){
99597 if( !db->autoCommit || db->nVdbeRead>1 ){
99598 rc = SQLITE_ERROR;
99599 sqlite3VdbeError(p,
99600 zFormat: "cannot change %s wal mode from within a transaction",
99601 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
99602 );
99603 goto abort_due_to_error;
99604 }else{
99605
99606 if( eOld==PAGER_JOURNALMODE_WAL ){
99607 /* If leaving WAL mode, close the log file. If successful, the call
99608 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
99609 ** file. An EXCLUSIVE lock may still be held on the database file
99610 ** after a successful return.
99611 */
99612 rc = sqlite3PagerCloseWal(pPager, db);
99613 if( rc==SQLITE_OK ){
99614 sqlite3PagerSetJournalMode(pPager, eMode: eNew);
99615 }
99616 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
99617 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
99618 ** as an intermediate */
99619 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
99620 }
99621
99622 /* Open a transaction on the database file. Regardless of the journal
99623 ** mode, this transaction always uses a rollback journal.
99624 */
99625 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE );
99626 if( rc==SQLITE_OK ){
99627 rc = sqlite3BtreeSetVersion(pBtree: pBt, iVersion: (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
99628 }
99629 }
99630 }
99631#endif /* ifndef SQLITE_OMIT_WAL */
99632
99633 if( rc ) eNew = eOld;
99634 eNew = sqlite3PagerSetJournalMode(pPager, eMode: eNew);
99635
99636 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
99637 pOut->z = (char *)sqlite3JournalModename(eNew);
99638 pOut->n = sqlite3Strlen30(z: pOut->z);
99639 pOut->enc = SQLITE_UTF8;
99640 sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: encoding);
99641 if( rc ) goto abort_due_to_error;
99642 break;
99643};
99644#endif /* SQLITE_OMIT_PRAGMA */
99645
99646#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
99647/* Opcode: Vacuum P1 P2 * * *
99648**
99649** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
99650** for an attached database. The "temp" database may not be vacuumed.
99651**
99652** If P2 is not zero, then it is a register holding a string which is
99653** the file into which the result of vacuum should be written. When
99654** P2 is zero, the vacuum overwrites the original database.
99655*/
99656case OP_Vacuum: {
99657 assert( p->readOnly==0 );
99658 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
99659 pOp->p2 ? &aMem[pOp->p2] : 0);
99660 if( rc ) goto abort_due_to_error;
99661 break;
99662}
99663#endif
99664
99665#if !defined(SQLITE_OMIT_AUTOVACUUM)
99666/* Opcode: IncrVacuum P1 P2 * * *
99667**
99668** Perform a single step of the incremental vacuum procedure on
99669** the P1 database. If the vacuum has finished, jump to instruction
99670** P2. Otherwise, fall through to the next instruction.
99671*/
99672case OP_IncrVacuum: { /* jump */
99673 Btree *pBt;
99674
99675 assert( pOp->p1>=0 && pOp->p1<db->nDb );
99676 assert( DbMaskTest(p->btreeMask, pOp->p1) );
99677 assert( p->readOnly==0 );
99678 pBt = db->aDb[pOp->p1].pBt;
99679 rc = sqlite3BtreeIncrVacuum(p: pBt);
99680 VdbeBranchTaken(rc==SQLITE_DONE,2);
99681 if( rc ){
99682 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
99683 rc = SQLITE_OK;
99684 goto jump_to_p2;
99685 }
99686 break;
99687}
99688#endif
99689
99690/* Opcode: Expire P1 P2 * * *
99691**
99692** Cause precompiled statements to expire. When an expired statement
99693** is executed using sqlite3_step() it will either automatically
99694** reprepare itself (if it was originally created using sqlite3_prepare_v2())
99695** or it will fail with SQLITE_SCHEMA.
99696**
99697** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
99698** then only the currently executing statement is expired.
99699**
99700** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
99701** then running SQL statements are allowed to continue to run to completion.
99702** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
99703** that might help the statement run faster but which does not affect the
99704** correctness of operation.
99705*/
99706case OP_Expire: {
99707 assert( pOp->p2==0 || pOp->p2==1 );
99708 if( !pOp->p1 ){
99709 sqlite3ExpirePreparedStatements(db, iCode: pOp->p2);
99710 }else{
99711 p->expired = pOp->p2+1;
99712 }
99713 break;
99714}
99715
99716/* Opcode: CursorLock P1 * * * *
99717**
99718** Lock the btree to which cursor P1 is pointing so that the btree cannot be
99719** written by an other cursor.
99720*/
99721case OP_CursorLock: {
99722 VdbeCursor *pC;
99723 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99724 pC = p->apCsr[pOp->p1];
99725 assert( pC!=0 );
99726 assert( pC->eCurType==CURTYPE_BTREE );
99727 sqlite3BtreeCursorPin(pCur: pC->uc.pCursor);
99728 break;
99729}
99730
99731/* Opcode: CursorUnlock P1 * * * *
99732**
99733** Unlock the btree to which cursor P1 is pointing so that it can be
99734** written by other cursors.
99735*/
99736case OP_CursorUnlock: {
99737 VdbeCursor *pC;
99738 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99739 pC = p->apCsr[pOp->p1];
99740 assert( pC!=0 );
99741 assert( pC->eCurType==CURTYPE_BTREE );
99742 sqlite3BtreeCursorUnpin(pCur: pC->uc.pCursor);
99743 break;
99744}
99745
99746#ifndef SQLITE_OMIT_SHARED_CACHE
99747/* Opcode: TableLock P1 P2 P3 P4 *
99748** Synopsis: iDb=P1 root=P2 write=P3
99749**
99750** Obtain a lock on a particular table. This instruction is only used when
99751** the shared-cache feature is enabled.
99752**
99753** P1 is the index of the database in sqlite3.aDb[] of the database
99754** on which the lock is acquired. A readlock is obtained if P3==0 or
99755** a write lock if P3==1.
99756**
99757** P2 contains the root-page of the table to lock.
99758**
99759** P4 contains a pointer to the name of the table being locked. This is only
99760** used to generate an error message if the lock cannot be obtained.
99761*/
99762case OP_TableLock: {
99763 u8 isWriteLock = (u8)pOp->p3;
99764 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
99765 int p1 = pOp->p1;
99766 assert( p1>=0 && p1<db->nDb );
99767 assert( DbMaskTest(p->btreeMask, p1) );
99768 assert( isWriteLock==0 || isWriteLock==1 );
99769 rc = sqlite3BtreeLockTable(p: db->aDb[p1].pBt, iTab: pOp->p2, isWriteLock);
99770 if( rc ){
99771 if( (rc&0xFF)==SQLITE_LOCKED ){
99772 const char *z = pOp->p4.z;
99773 sqlite3VdbeError(p, zFormat: "database table is locked: %s", z);
99774 }
99775 goto abort_due_to_error;
99776 }
99777 }
99778 break;
99779}
99780#endif /* SQLITE_OMIT_SHARED_CACHE */
99781
99782#ifndef SQLITE_OMIT_VIRTUALTABLE
99783/* Opcode: VBegin * * * P4 *
99784**
99785** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
99786** xBegin method for that table.
99787**
99788** Also, whether or not P4 is set, check that this is not being called from
99789** within a callback to a virtual table xSync() method. If it is, the error
99790** code will be set to SQLITE_LOCKED.
99791*/
99792case OP_VBegin: {
99793 VTable *pVTab;
99794 pVTab = pOp->p4.pVtab;
99795 rc = sqlite3VtabBegin(db, pVTab);
99796 if( pVTab ) sqlite3VtabImportErrmsg(p, pVtab: pVTab->pVtab);
99797 if( rc ) goto abort_due_to_error;
99798 break;
99799}
99800#endif /* SQLITE_OMIT_VIRTUALTABLE */
99801
99802#ifndef SQLITE_OMIT_VIRTUALTABLE
99803/* Opcode: VCreate P1 P2 * * *
99804**
99805** P2 is a register that holds the name of a virtual table in database
99806** P1. Call the xCreate method for that table.
99807*/
99808case OP_VCreate: {
99809 Mem sMem; /* For storing the record being decoded */
99810 const char *zTab; /* Name of the virtual table */
99811
99812 memset(s: &sMem, c: 0, n: sizeof(sMem));
99813 sMem.db = db;
99814 /* Because P2 is always a static string, it is impossible for the
99815 ** sqlite3VdbeMemCopy() to fail */
99816 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
99817 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
99818 rc = sqlite3VdbeMemCopy(pTo: &sMem, pFrom: &aMem[pOp->p2]);
99819 assert( rc==SQLITE_OK );
99820 zTab = (const char*)sqlite3_value_text(pVal: &sMem);
99821 assert( zTab || db->mallocFailed );
99822 if( zTab ){
99823 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
99824 }
99825 sqlite3VdbeMemRelease(p: &sMem);
99826 if( rc ) goto abort_due_to_error;
99827 break;
99828}
99829#endif /* SQLITE_OMIT_VIRTUALTABLE */
99830
99831#ifndef SQLITE_OMIT_VIRTUALTABLE
99832/* Opcode: VDestroy P1 * * P4 *
99833**
99834** P4 is the name of a virtual table in database P1. Call the xDestroy method
99835** of that table.
99836*/
99837case OP_VDestroy: {
99838 db->nVDestroy++;
99839 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
99840 db->nVDestroy--;
99841 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
99842 if( rc ) goto abort_due_to_error;
99843 break;
99844}
99845#endif /* SQLITE_OMIT_VIRTUALTABLE */
99846
99847#ifndef SQLITE_OMIT_VIRTUALTABLE
99848/* Opcode: VOpen P1 * * P4 *
99849**
99850** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
99851** P1 is a cursor number. This opcode opens a cursor to the virtual
99852** table and stores that cursor in P1.
99853*/
99854case OP_VOpen: { /* ncycle */
99855 VdbeCursor *pCur;
99856 sqlite3_vtab_cursor *pVCur;
99857 sqlite3_vtab *pVtab;
99858 const sqlite3_module *pModule;
99859
99860 assert( p->bIsReader );
99861 pCur = 0;
99862 pVCur = 0;
99863 pVtab = pOp->p4.pVtab->pVtab;
99864 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
99865 rc = SQLITE_LOCKED;
99866 goto abort_due_to_error;
99867 }
99868 pModule = pVtab->pModule;
99869 rc = pModule->xOpen(pVtab, &pVCur);
99870 sqlite3VtabImportErrmsg(p, pVtab);
99871 if( rc ) goto abort_due_to_error;
99872
99873 /* Initialize sqlite3_vtab_cursor base class */
99874 pVCur->pVtab = pVtab;
99875
99876 /* Initialize vdbe cursor object */
99877 pCur = allocateCursor(p, iCur: pOp->p1, nField: 0, CURTYPE_VTAB);
99878 if( pCur ){
99879 pCur->uc.pVCur = pVCur;
99880 pVtab->nRef++;
99881 }else{
99882 assert( db->mallocFailed );
99883 pModule->xClose(pVCur);
99884 goto no_mem;
99885 }
99886 break;
99887}
99888#endif /* SQLITE_OMIT_VIRTUALTABLE */
99889
99890#ifndef SQLITE_OMIT_VIRTUALTABLE
99891/* Opcode: VInitIn P1 P2 P3 * *
99892** Synopsis: r[P2]=ValueList(P1,P3)
99893**
99894** Set register P2 to be a pointer to a ValueList object for cursor P1
99895** with cache register P3 and output register P3+1. This ValueList object
99896** can be used as the first argument to sqlite3_vtab_in_first() and
99897** sqlite3_vtab_in_next() to extract all of the values stored in the P1
99898** cursor. Register P3 is used to hold the values returned by
99899** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
99900*/
99901case OP_VInitIn: { /* out2, ncycle */
99902 VdbeCursor *pC; /* The cursor containing the RHS values */
99903 ValueList *pRhs; /* New ValueList object to put in reg[P2] */
99904
99905 pC = p->apCsr[pOp->p1];
99906 pRhs = sqlite3_malloc64( n: sizeof(*pRhs) );
99907 if( pRhs==0 ) goto no_mem;
99908 pRhs->pCsr = pC->uc.pCursor;
99909 pRhs->pOut = &aMem[pOp->p3];
99910 pOut = out2Prerelease(p, pOp);
99911 pOut->flags = MEM_Null;
99912 sqlite3VdbeMemSetPointer(pMem: pOut, pPtr: pRhs, zPType: "ValueList", xDestructor: sqlite3VdbeValueListFree);
99913 break;
99914}
99915#endif /* SQLITE_OMIT_VIRTUALTABLE */
99916
99917
99918#ifndef SQLITE_OMIT_VIRTUALTABLE
99919/* Opcode: VFilter P1 P2 P3 P4 *
99920** Synopsis: iplan=r[P3] zplan='P4'
99921**
99922** P1 is a cursor opened using VOpen. P2 is an address to jump to if
99923** the filtered result set is empty.
99924**
99925** P4 is either NULL or a string that was generated by the xBestIndex
99926** method of the module. The interpretation of the P4 string is left
99927** to the module implementation.
99928**
99929** This opcode invokes the xFilter method on the virtual table specified
99930** by P1. The integer query plan parameter to xFilter is stored in register
99931** P3. Register P3+1 stores the argc parameter to be passed to the
99932** xFilter method. Registers P3+2..P3+1+argc are the argc
99933** additional parameters which are passed to
99934** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
99935**
99936** A jump is made to P2 if the result set after filtering would be empty.
99937*/
99938case OP_VFilter: { /* jump, ncycle */
99939 int nArg;
99940 int iQuery;
99941 const sqlite3_module *pModule;
99942 Mem *pQuery;
99943 Mem *pArgc;
99944 sqlite3_vtab_cursor *pVCur;
99945 sqlite3_vtab *pVtab;
99946 VdbeCursor *pCur;
99947 int res;
99948 int i;
99949 Mem **apArg;
99950
99951 pQuery = &aMem[pOp->p3];
99952 pArgc = &pQuery[1];
99953 pCur = p->apCsr[pOp->p1];
99954 assert( memIsValid(pQuery) );
99955 REGISTER_TRACE(pOp->p3, pQuery);
99956 assert( pCur!=0 );
99957 assert( pCur->eCurType==CURTYPE_VTAB );
99958 pVCur = pCur->uc.pVCur;
99959 pVtab = pVCur->pVtab;
99960 pModule = pVtab->pModule;
99961
99962 /* Grab the index number and argc parameters */
99963 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
99964 nArg = (int)pArgc->u.i;
99965 iQuery = (int)pQuery->u.i;
99966
99967 /* Invoke the xFilter method */
99968 apArg = p->apArg;
99969 for(i = 0; i<nArg; i++){
99970 apArg[i] = &pArgc[i+1];
99971 }
99972 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
99973 sqlite3VtabImportErrmsg(p, pVtab);
99974 if( rc ) goto abort_due_to_error;
99975 res = pModule->xEof(pVCur);
99976 pCur->nullRow = 0;
99977 VdbeBranchTaken(res!=0,2);
99978 if( res ) goto jump_to_p2;
99979 break;
99980}
99981#endif /* SQLITE_OMIT_VIRTUALTABLE */
99982
99983#ifndef SQLITE_OMIT_VIRTUALTABLE
99984/* Opcode: VColumn P1 P2 P3 * P5
99985** Synopsis: r[P3]=vcolumn(P2)
99986**
99987** Store in register P3 the value of the P2-th column of
99988** the current row of the virtual-table of cursor P1.
99989**
99990** If the VColumn opcode is being used to fetch the value of
99991** an unchanging column during an UPDATE operation, then the P5
99992** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
99993** function to return true inside the xColumn method of the virtual
99994** table implementation. The P5 column might also contain other
99995** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
99996** unused by OP_VColumn.
99997*/
99998case OP_VColumn: { /* ncycle */
99999 sqlite3_vtab *pVtab;
100000 const sqlite3_module *pModule;
100001 Mem *pDest;
100002 sqlite3_context sContext;
100003
100004 VdbeCursor *pCur = p->apCsr[pOp->p1];
100005 assert( pCur!=0 );
100006 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100007 pDest = &aMem[pOp->p3];
100008 memAboutToChange(p, pDest);
100009 if( pCur->nullRow ){
100010 sqlite3VdbeMemSetNull(pMem: pDest);
100011 break;
100012 }
100013 assert( pCur->eCurType==CURTYPE_VTAB );
100014 pVtab = pCur->uc.pVCur->pVtab;
100015 pModule = pVtab->pModule;
100016 assert( pModule->xColumn );
100017 memset(s: &sContext, c: 0, n: sizeof(sContext));
100018 sContext.pOut = pDest;
100019 sContext.enc = encoding;
100020 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
100021 if( pOp->p5 & OPFLAG_NOCHNG ){
100022 sqlite3VdbeMemSetNull(pMem: pDest);
100023 pDest->flags = MEM_Null|MEM_Zero;
100024 pDest->u.nZero = 0;
100025 }else{
100026 MemSetTypeFlag(pDest, MEM_Null);
100027 }
100028 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
100029 sqlite3VtabImportErrmsg(p, pVtab);
100030 if( sContext.isError>0 ){
100031 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pDest));
100032 rc = sContext.isError;
100033 }
100034 sqlite3VdbeChangeEncoding(pMem: pDest, desiredEnc: encoding);
100035 REGISTER_TRACE(pOp->p3, pDest);
100036 UPDATE_MAX_BLOBSIZE(pDest);
100037
100038 if( rc ) goto abort_due_to_error;
100039 break;
100040}
100041#endif /* SQLITE_OMIT_VIRTUALTABLE */
100042
100043#ifndef SQLITE_OMIT_VIRTUALTABLE
100044/* Opcode: VNext P1 P2 * * *
100045**
100046** Advance virtual table P1 to the next row in its result set and
100047** jump to instruction P2. Or, if the virtual table has reached
100048** the end of its result set, then fall through to the next instruction.
100049*/
100050case OP_VNext: { /* jump, ncycle */
100051 sqlite3_vtab *pVtab;
100052 const sqlite3_module *pModule;
100053 int res;
100054 VdbeCursor *pCur;
100055
100056 pCur = p->apCsr[pOp->p1];
100057 assert( pCur!=0 );
100058 assert( pCur->eCurType==CURTYPE_VTAB );
100059 if( pCur->nullRow ){
100060 break;
100061 }
100062 pVtab = pCur->uc.pVCur->pVtab;
100063 pModule = pVtab->pModule;
100064 assert( pModule->xNext );
100065
100066 /* Invoke the xNext() method of the module. There is no way for the
100067 ** underlying implementation to return an error if one occurs during
100068 ** xNext(). Instead, if an error occurs, true is returned (indicating that
100069 ** data is available) and the error code returned when xColumn or
100070 ** some other method is next invoked on the save virtual table cursor.
100071 */
100072 rc = pModule->xNext(pCur->uc.pVCur);
100073 sqlite3VtabImportErrmsg(p, pVtab);
100074 if( rc ) goto abort_due_to_error;
100075 res = pModule->xEof(pCur->uc.pVCur);
100076 VdbeBranchTaken(!res,2);
100077 if( !res ){
100078 /* If there is data, jump to P2 */
100079 goto jump_to_p2_and_check_for_interrupt;
100080 }
100081 goto check_for_interrupt;
100082}
100083#endif /* SQLITE_OMIT_VIRTUALTABLE */
100084
100085#ifndef SQLITE_OMIT_VIRTUALTABLE
100086/* Opcode: VRename P1 * * P4 *
100087**
100088** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
100089** This opcode invokes the corresponding xRename method. The value
100090** in register P1 is passed as the zName argument to the xRename method.
100091*/
100092case OP_VRename: {
100093 sqlite3_vtab *pVtab;
100094 Mem *pName;
100095 int isLegacy;
100096
100097 isLegacy = (db->flags & SQLITE_LegacyAlter);
100098 db->flags |= SQLITE_LegacyAlter;
100099 pVtab = pOp->p4.pVtab->pVtab;
100100 pName = &aMem[pOp->p1];
100101 assert( pVtab->pModule->xRename );
100102 assert( memIsValid(pName) );
100103 assert( p->readOnly==0 );
100104 REGISTER_TRACE(pOp->p1, pName);
100105 assert( pName->flags & MEM_Str );
100106 testcase( pName->enc==SQLITE_UTF8 );
100107 testcase( pName->enc==SQLITE_UTF16BE );
100108 testcase( pName->enc==SQLITE_UTF16LE );
100109 rc = sqlite3VdbeChangeEncoding(pMem: pName, SQLITE_UTF8);
100110 if( rc ) goto abort_due_to_error;
100111 rc = pVtab->pModule->xRename(pVtab, pName->z);
100112 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
100113 sqlite3VtabImportErrmsg(p, pVtab);
100114 p->expired = 0;
100115 if( rc ) goto abort_due_to_error;
100116 break;
100117}
100118#endif
100119
100120#ifndef SQLITE_OMIT_VIRTUALTABLE
100121/* Opcode: VUpdate P1 P2 P3 P4 P5
100122** Synopsis: data=r[P3@P2]
100123**
100124** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
100125** This opcode invokes the corresponding xUpdate method. P2 values
100126** are contiguous memory cells starting at P3 to pass to the xUpdate
100127** invocation. The value in register (P3+P2-1) corresponds to the
100128** p2th element of the argv array passed to xUpdate.
100129**
100130** The xUpdate method will do a DELETE or an INSERT or both.
100131** The argv[0] element (which corresponds to memory cell P3)
100132** is the rowid of a row to delete. If argv[0] is NULL then no
100133** deletion occurs. The argv[1] element is the rowid of the new
100134** row. This can be NULL to have the virtual table select the new
100135** rowid for itself. The subsequent elements in the array are
100136** the values of columns in the new row.
100137**
100138** If P2==1 then no insert is performed. argv[0] is the rowid of
100139** a row to delete.
100140**
100141** P1 is a boolean flag. If it is set to true and the xUpdate call
100142** is successful, then the value returned by sqlite3_last_insert_rowid()
100143** is set to the value of the rowid for the row just inserted.
100144**
100145** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
100146** apply in the case of a constraint failure on an insert or update.
100147*/
100148case OP_VUpdate: {
100149 sqlite3_vtab *pVtab;
100150 const sqlite3_module *pModule;
100151 int nArg;
100152 int i;
100153 sqlite_int64 rowid = 0;
100154 Mem **apArg;
100155 Mem *pX;
100156
100157 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
100158 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
100159 );
100160 assert( p->readOnly==0 );
100161 if( db->mallocFailed ) goto no_mem;
100162 sqlite3VdbeIncrWriteCounter(p, 0);
100163 pVtab = pOp->p4.pVtab->pVtab;
100164 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
100165 rc = SQLITE_LOCKED;
100166 goto abort_due_to_error;
100167 }
100168 pModule = pVtab->pModule;
100169 nArg = pOp->p2;
100170 assert( pOp->p4type==P4_VTAB );
100171 if( ALWAYS(pModule->xUpdate) ){
100172 u8 vtabOnConflict = db->vtabOnConflict;
100173 apArg = p->apArg;
100174 pX = &aMem[pOp->p3];
100175 for(i=0; i<nArg; i++){
100176 assert( memIsValid(pX) );
100177 memAboutToChange(p, pX);
100178 apArg[i] = pX;
100179 pX++;
100180 }
100181 db->vtabOnConflict = pOp->p5;
100182 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
100183 db->vtabOnConflict = vtabOnConflict;
100184 sqlite3VtabImportErrmsg(p, pVtab);
100185 if( rc==SQLITE_OK && pOp->p1 ){
100186 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
100187 db->lastRowid = rowid;
100188 }
100189 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
100190 if( pOp->p5==OE_Ignore ){
100191 rc = SQLITE_OK;
100192 }else{
100193 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
100194 }
100195 }else{
100196 p->nChange++;
100197 }
100198 if( rc ) goto abort_due_to_error;
100199 }
100200 break;
100201}
100202#endif /* SQLITE_OMIT_VIRTUALTABLE */
100203
100204#ifndef SQLITE_OMIT_PAGER_PRAGMAS
100205/* Opcode: Pagecount P1 P2 * * *
100206**
100207** Write the current number of pages in database P1 to memory cell P2.
100208*/
100209case OP_Pagecount: { /* out2 */
100210 pOut = out2Prerelease(p, pOp);
100211 pOut->u.i = sqlite3BtreeLastPage(p: db->aDb[pOp->p1].pBt);
100212 break;
100213}
100214#endif
100215
100216
100217#ifndef SQLITE_OMIT_PAGER_PRAGMAS
100218/* Opcode: MaxPgcnt P1 P2 P3 * *
100219**
100220** Try to set the maximum page count for database P1 to the value in P3.
100221** Do not let the maximum page count fall below the current page count and
100222** do not change the maximum page count value if P3==0.
100223**
100224** Store the maximum page count after the change in register P2.
100225*/
100226case OP_MaxPgcnt: { /* out2 */
100227 unsigned int newMax;
100228 Btree *pBt;
100229
100230 pOut = out2Prerelease(p, pOp);
100231 pBt = db->aDb[pOp->p1].pBt;
100232 newMax = 0;
100233 if( pOp->p3 ){
100234 newMax = sqlite3BtreeLastPage(p: pBt);
100235 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
100236 }
100237 pOut->u.i = sqlite3BtreeMaxPageCount(p: pBt, mxPage: newMax);
100238 break;
100239}
100240#endif
100241
100242/* Opcode: Function P1 P2 P3 P4 *
100243** Synopsis: r[P3]=func(r[P2@NP])
100244**
100245** Invoke a user function (P4 is a pointer to an sqlite3_context object that
100246** contains a pointer to the function to be run) with arguments taken
100247** from register P2 and successors. The number of arguments is in
100248** the sqlite3_context object that P4 points to.
100249** The result of the function is stored
100250** in register P3. Register P3 must not be one of the function inputs.
100251**
100252** P1 is a 32-bit bitmask indicating whether or not each argument to the
100253** function was determined to be constant at compile time. If the first
100254** argument was constant then bit 0 of P1 is set. This is used to determine
100255** whether meta data associated with a user function argument using the
100256** sqlite3_set_auxdata() API may be safely retained until the next
100257** invocation of this opcode.
100258**
100259** See also: AggStep, AggFinal, PureFunc
100260*/
100261/* Opcode: PureFunc P1 P2 P3 P4 *
100262** Synopsis: r[P3]=func(r[P2@NP])
100263**
100264** Invoke a user function (P4 is a pointer to an sqlite3_context object that
100265** contains a pointer to the function to be run) with arguments taken
100266** from register P2 and successors. The number of arguments is in
100267** the sqlite3_context object that P4 points to.
100268** The result of the function is stored
100269** in register P3. Register P3 must not be one of the function inputs.
100270**
100271** P1 is a 32-bit bitmask indicating whether or not each argument to the
100272** function was determined to be constant at compile time. If the first
100273** argument was constant then bit 0 of P1 is set. This is used to determine
100274** whether meta data associated with a user function argument using the
100275** sqlite3_set_auxdata() API may be safely retained until the next
100276** invocation of this opcode.
100277**
100278** This opcode works exactly like OP_Function. The only difference is in
100279** its name. This opcode is used in places where the function must be
100280** purely non-deterministic. Some built-in date/time functions can be
100281** either deterministic of non-deterministic, depending on their arguments.
100282** When those function are used in a non-deterministic way, they will check
100283** to see if they were called using OP_PureFunc instead of OP_Function, and
100284** if they were, they throw an error.
100285**
100286** See also: AggStep, AggFinal, Function
100287*/
100288case OP_PureFunc: /* group */
100289case OP_Function: { /* group */
100290 int i;
100291 sqlite3_context *pCtx;
100292
100293 assert( pOp->p4type==P4_FUNCCTX );
100294 pCtx = pOp->p4.pCtx;
100295
100296 /* If this function is inside of a trigger, the register array in aMem[]
100297 ** might change from one evaluation to the next. The next block of code
100298 ** checks to see if the register array has changed, and if so it
100299 ** reinitializes the relevant parts of the sqlite3_context object */
100300 pOut = &aMem[pOp->p3];
100301 if( pCtx->pOut != pOut ){
100302 pCtx->pVdbe = p;
100303 pCtx->pOut = pOut;
100304 pCtx->enc = encoding;
100305 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
100306 }
100307 assert( pCtx->pVdbe==p );
100308
100309 memAboutToChange(p, pOut);
100310#ifdef SQLITE_DEBUG
100311 for(i=0; i<pCtx->argc; i++){
100312 assert( memIsValid(pCtx->argv[i]) );
100313 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
100314 }
100315#endif
100316 MemSetTypeFlag(pOut, MEM_Null);
100317 assert( pCtx->isError==0 );
100318 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
100319
100320 /* If the function returned an error, throw an exception */
100321 if( pCtx->isError ){
100322 if( pCtx->isError>0 ){
100323 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pOut));
100324 rc = pCtx->isError;
100325 }
100326 sqlite3VdbeDeleteAuxData(db, pp: &p->pAuxData, iOp: pCtx->iOp, mask: pOp->p1);
100327 pCtx->isError = 0;
100328 if( rc ) goto abort_due_to_error;
100329 }
100330
100331 assert( (pOut->flags&MEM_Str)==0
100332 || pOut->enc==encoding
100333 || db->mallocFailed );
100334 assert( !sqlite3VdbeMemTooBig(pOut) );
100335
100336 REGISTER_TRACE(pOp->p3, pOut);
100337 UPDATE_MAX_BLOBSIZE(pOut);
100338 break;
100339}
100340
100341/* Opcode: ClrSubtype P1 * * * *
100342** Synopsis: r[P1].subtype = 0
100343**
100344** Clear the subtype from register P1.
100345*/
100346case OP_ClrSubtype: { /* in1 */
100347 pIn1 = &aMem[pOp->p1];
100348 pIn1->flags &= ~MEM_Subtype;
100349 break;
100350}
100351
100352/* Opcode: FilterAdd P1 * P3 P4 *
100353** Synopsis: filter(P1) += key(P3@P4)
100354**
100355** Compute a hash on the P4 registers starting with r[P3] and
100356** add that hash to the bloom filter contained in r[P1].
100357*/
100358case OP_FilterAdd: {
100359 u64 h;
100360
100361 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
100362 pIn1 = &aMem[pOp->p1];
100363 assert( pIn1->flags & MEM_Blob );
100364 assert( pIn1->n>0 );
100365 h = filterHash(aMem, pOp);
100366#ifdef SQLITE_DEBUG
100367 if( db->flags&SQLITE_VdbeTrace ){
100368 int ii;
100369 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
100370 registerTrace(ii, &aMem[ii]);
100371 }
100372 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
100373 }
100374#endif
100375 h %= (pIn1->n*8);
100376 pIn1->z[h/8] |= 1<<(h&7);
100377 break;
100378}
100379
100380/* Opcode: Filter P1 P2 P3 P4 *
100381** Synopsis: if key(P3@P4) not in filter(P1) goto P2
100382**
100383** Compute a hash on the key contained in the P4 registers starting
100384** with r[P3]. Check to see if that hash is found in the
100385** bloom filter hosted by register P1. If it is not present then
100386** maybe jump to P2. Otherwise fall through.
100387**
100388** False negatives are harmless. It is always safe to fall through,
100389** even if the value is in the bloom filter. A false negative causes
100390** more CPU cycles to be used, but it should still yield the correct
100391** answer. However, an incorrect answer may well arise from a
100392** false positive - if the jump is taken when it should fall through.
100393*/
100394case OP_Filter: { /* jump */
100395 u64 h;
100396
100397 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
100398 pIn1 = &aMem[pOp->p1];
100399 assert( (pIn1->flags & MEM_Blob)!=0 );
100400 assert( pIn1->n >= 1 );
100401 h = filterHash(aMem, pOp);
100402#ifdef SQLITE_DEBUG
100403 if( db->flags&SQLITE_VdbeTrace ){
100404 int ii;
100405 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
100406 registerTrace(ii, &aMem[ii]);
100407 }
100408 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
100409 }
100410#endif
100411 h %= (pIn1->n*8);
100412 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
100413 VdbeBranchTaken(1, 2);
100414 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
100415 goto jump_to_p2;
100416 }else{
100417 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
100418 VdbeBranchTaken(0, 2);
100419 }
100420 break;
100421}
100422
100423/* Opcode: Trace P1 P2 * P4 *
100424**
100425** Write P4 on the statement trace output if statement tracing is
100426** enabled.
100427**
100428** Operand P1 must be 0x7fffffff and P2 must positive.
100429*/
100430/* Opcode: Init P1 P2 P3 P4 *
100431** Synopsis: Start at P2
100432**
100433** Programs contain a single instance of this opcode as the very first
100434** opcode.
100435**
100436** If tracing is enabled (by the sqlite3_trace()) interface, then
100437** the UTF-8 string contained in P4 is emitted on the trace callback.
100438** Or if P4 is blank, use the string returned by sqlite3_sql().
100439**
100440** If P2 is not zero, jump to instruction P2.
100441**
100442** Increment the value of P1 so that OP_Once opcodes will jump the
100443** first time they are evaluated for this run.
100444**
100445** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
100446** error is encountered.
100447*/
100448case OP_Trace:
100449case OP_Init: { /* jump */
100450 int i;
100451#ifndef SQLITE_OMIT_TRACE
100452 char *zTrace;
100453#endif
100454
100455 /* If the P4 argument is not NULL, then it must be an SQL comment string.
100456 ** The "--" string is broken up to prevent false-positives with srcck1.c.
100457 **
100458 ** This assert() provides evidence for:
100459 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
100460 ** would have been returned by the legacy sqlite3_trace() interface by
100461 ** using the X argument when X begins with "--" and invoking
100462 ** sqlite3_expanded_sql(P) otherwise.
100463 */
100464 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
100465
100466 /* OP_Init is always instruction 0 */
100467 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
100468
100469#ifndef SQLITE_OMIT_TRACE
100470 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
100471 && p->minWriteFileFormat!=254 /* tag-20220401a */
100472 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
100473 ){
100474#ifndef SQLITE_OMIT_DEPRECATED
100475 if( db->mTrace & SQLITE_TRACE_LEGACY ){
100476 char *z = sqlite3VdbeExpandSql(p, zRawSql: zTrace);
100477 db->trace.xLegacy(db->pTraceArg, z);
100478 sqlite3_free(p: z);
100479 }else
100480#endif
100481 if( db->nVdbeExec>1 ){
100482 char *z = sqlite3MPrintf(db, zFormat: "-- %s", zTrace);
100483 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
100484 sqlite3DbFree(db, p: z);
100485 }else{
100486 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
100487 }
100488 }
100489#ifdef SQLITE_USE_FCNTL_TRACE
100490 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
100491 if( zTrace ){
100492 int j;
100493 for(j=0; j<db->nDb; j++){
100494 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
100495 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
100496 }
100497 }
100498#endif /* SQLITE_USE_FCNTL_TRACE */
100499#ifdef SQLITE_DEBUG
100500 if( (db->flags & SQLITE_SqlTrace)!=0
100501 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
100502 ){
100503 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
100504 }
100505#endif /* SQLITE_DEBUG */
100506#endif /* SQLITE_OMIT_TRACE */
100507 assert( pOp->p2>0 );
100508 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
100509 if( pOp->opcode==OP_Trace ) break;
100510 for(i=1; i<p->nOp; i++){
100511 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
100512 }
100513 pOp->p1 = 0;
100514 }
100515 pOp->p1++;
100516 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
100517 goto jump_to_p2;
100518}
100519
100520#ifdef SQLITE_ENABLE_CURSOR_HINTS
100521/* Opcode: CursorHint P1 * * P4 *
100522**
100523** Provide a hint to cursor P1 that it only needs to return rows that
100524** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
100525** to values currently held in registers. TK_COLUMN terms in the P4
100526** expression refer to columns in the b-tree to which cursor P1 is pointing.
100527*/
100528case OP_CursorHint: {
100529 VdbeCursor *pC;
100530
100531 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100532 assert( pOp->p4type==P4_EXPR );
100533 pC = p->apCsr[pOp->p1];
100534 if( pC ){
100535 assert( pC->eCurType==CURTYPE_BTREE );
100536 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
100537 pOp->p4.pExpr, aMem);
100538 }
100539 break;
100540}
100541#endif /* SQLITE_ENABLE_CURSOR_HINTS */
100542
100543#ifdef SQLITE_DEBUG
100544/* Opcode: Abortable * * * * *
100545**
100546** Verify that an Abort can happen. Assert if an Abort at this point
100547** might cause database corruption. This opcode only appears in debugging
100548** builds.
100549**
100550** An Abort is safe if either there have been no writes, or if there is
100551** an active statement journal.
100552*/
100553case OP_Abortable: {
100554 sqlite3VdbeAssertAbortable(p);
100555 break;
100556}
100557#endif
100558
100559#ifdef SQLITE_DEBUG
100560/* Opcode: ReleaseReg P1 P2 P3 * P5
100561** Synopsis: release r[P1@P2] mask P3
100562**
100563** Release registers from service. Any content that was in the
100564** the registers is unreliable after this opcode completes.
100565**
100566** The registers released will be the P2 registers starting at P1,
100567** except if bit ii of P3 set, then do not release register P1+ii.
100568** In other words, P3 is a mask of registers to preserve.
100569**
100570** Releasing a register clears the Mem.pScopyFrom pointer. That means
100571** that if the content of the released register was set using OP_SCopy,
100572** a change to the value of the source register for the OP_SCopy will no longer
100573** generate an assertion fault in sqlite3VdbeMemAboutToChange().
100574**
100575** If P5 is set, then all released registers have their type set
100576** to MEM_Undefined so that any subsequent attempt to read the released
100577** register (before it is reinitialized) will generate an assertion fault.
100578**
100579** P5 ought to be set on every call to this opcode.
100580** However, there are places in the code generator will release registers
100581** before their are used, under the (valid) assumption that the registers
100582** will not be reallocated for some other purpose before they are used and
100583** hence are safe to release.
100584**
100585** This opcode is only available in testing and debugging builds. It is
100586** not generated for release builds. The purpose of this opcode is to help
100587** validate the generated bytecode. This opcode does not actually contribute
100588** to computing an answer.
100589*/
100590case OP_ReleaseReg: {
100591 Mem *pMem;
100592 int i;
100593 u32 constMask;
100594 assert( pOp->p1>0 );
100595 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
100596 pMem = &aMem[pOp->p1];
100597 constMask = pOp->p3;
100598 for(i=0; i<pOp->p2; i++, pMem++){
100599 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
100600 pMem->pScopyFrom = 0;
100601 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
100602 }
100603 }
100604 break;
100605}
100606#endif
100607
100608/* Opcode: Noop * * * * *
100609**
100610** Do nothing. This instruction is often useful as a jump
100611** destination.
100612*/
100613/*
100614** The magic Explain opcode are only inserted when explain==2 (which
100615** is to say when the EXPLAIN QUERY PLAN syntax is used.)
100616** This opcode records information from the optimizer. It is the
100617** the same as a no-op. This opcodesnever appears in a real VM program.
100618*/
100619default: { /* This is really OP_Noop, OP_Explain */
100620 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
100621
100622 break;
100623}
100624
100625/*****************************************************************************
100626** The cases of the switch statement above this line should all be indented
100627** by 6 spaces. But the left-most 6 spaces have been removed to improve the
100628** readability. From this point on down, the normal indentation rules are
100629** restored.
100630*****************************************************************************/
100631 }
100632
100633#if defined(VDBE_PROFILE)
100634 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
100635 pnCycle = 0;
100636#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
100637 if( pnCycle ){
100638 *pnCycle += sqlite3Hwtime();
100639 pnCycle = 0;
100640 }
100641#endif
100642
100643 /* The following code adds nothing to the actual functionality
100644 ** of the program. It is only here for testing and debugging.
100645 ** On the other hand, it does burn CPU cycles every time through
100646 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
100647 */
100648#ifndef NDEBUG
100649 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
100650
100651#ifdef SQLITE_DEBUG
100652 if( db->flags & SQLITE_VdbeTrace ){
100653 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
100654 if( rc!=0 ) printf("rc=%d\n",rc);
100655 if( opProperty & (OPFLG_OUT2) ){
100656 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
100657 }
100658 if( opProperty & OPFLG_OUT3 ){
100659 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
100660 }
100661 if( opProperty==0xff ){
100662 /* Never happens. This code exists to avoid a harmless linkage
100663 ** warning about sqlite3VdbeRegisterDump() being defined but not
100664 ** used. */
100665 sqlite3VdbeRegisterDump(p);
100666 }
100667 }
100668#endif /* SQLITE_DEBUG */
100669#endif /* NDEBUG */
100670 } /* The end of the for(;;) loop the loops through opcodes */
100671
100672 /* If we reach this point, it means that execution is finished with
100673 ** an error of some kind.
100674 */
100675abort_due_to_error:
100676 if( db->mallocFailed ){
100677 rc = SQLITE_NOMEM_BKPT;
100678 }else if( rc==SQLITE_IOERR_CORRUPTFS ){
100679 rc = SQLITE_CORRUPT_BKPT;
100680 }
100681 assert( rc );
100682#ifdef SQLITE_DEBUG
100683 if( db->flags & SQLITE_VdbeTrace ){
100684 const char *zTrace = p->zSql;
100685 if( zTrace==0 ){
100686 if( aOp[0].opcode==OP_Trace ){
100687 zTrace = aOp[0].p4.z;
100688 }
100689 if( zTrace==0 ) zTrace = "???";
100690 }
100691 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
100692 }
100693#endif
100694 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
100695 sqlite3VdbeError(p, zFormat: "%s", sqlite3ErrStr(rc));
100696 }
100697 p->rc = rc;
100698 sqlite3SystemError(db, rc);
100699 testcase( sqlite3GlobalConfig.xLog!=0 );
100700 sqlite3_log(iErrCode: rc, zFormat: "statement aborts at %d: [%s] %s",
100701 (int)(pOp - aOp), p->zSql, p->zErrMsg);
100702 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
100703 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
100704 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
100705 db->flags |= SQLITE_CorruptRdOnly;
100706 }
100707 rc = SQLITE_ERROR;
100708 if( resetSchemaOnFault>0 ){
100709 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
100710 }
100711
100712 /* This is the only way out of this procedure. We have to
100713 ** release the mutexes on btrees that were acquired at the
100714 ** top. */
100715vdbe_return:
100716#if defined(VDBE_PROFILE)
100717 if( pnCycle ){
100718 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
100719 pnCycle = 0;
100720 }
100721#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
100722 if( pnCycle ){
100723 *pnCycle += sqlite3Hwtime();
100724 pnCycle = 0;
100725 }
100726#endif
100727
100728#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
100729 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
100730 nProgressLimit += db->nProgressOps;
100731 if( db->xProgress(db->pProgressArg) ){
100732 nProgressLimit = LARGEST_UINT64;
100733 rc = SQLITE_INTERRUPT;
100734 goto abort_due_to_error;
100735 }
100736 }
100737#endif
100738 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
100739 if( DbMaskNonZero(p->lockMask) ){
100740 sqlite3VdbeLeave(p);
100741 }
100742 assert( rc!=SQLITE_OK || nExtraDelete==0
100743 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
100744 );
100745 return rc;
100746
100747 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
100748 ** is encountered.
100749 */
100750too_big:
100751 sqlite3VdbeError(p, zFormat: "string or blob too big");
100752 rc = SQLITE_TOOBIG;
100753 goto abort_due_to_error;
100754
100755 /* Jump to here if a malloc() fails.
100756 */
100757no_mem:
100758 sqlite3OomFault(db);
100759 sqlite3VdbeError(p, zFormat: "out of memory");
100760 rc = SQLITE_NOMEM_BKPT;
100761 goto abort_due_to_error;
100762
100763 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
100764 ** flag.
100765 */
100766abort_due_to_interrupt:
100767 assert( AtomicLoad(&db->u1.isInterrupted) );
100768 rc = SQLITE_INTERRUPT;
100769 goto abort_due_to_error;
100770}
100771
100772
100773/************** End of vdbe.c ************************************************/
100774/************** Begin file vdbeblob.c ****************************************/
100775/*
100776** 2007 May 1
100777**
100778** The author disclaims copyright to this source code. In place of
100779** a legal notice, here is a blessing:
100780**
100781** May you do good and not evil.
100782** May you find forgiveness for yourself and forgive others.
100783** May you share freely, never taking more than you give.
100784**
100785*************************************************************************
100786**
100787** This file contains code used to implement incremental BLOB I/O.
100788*/
100789
100790/* #include "sqliteInt.h" */
100791/* #include "vdbeInt.h" */
100792
100793#ifndef SQLITE_OMIT_INCRBLOB
100794
100795/*
100796** Valid sqlite3_blob* handles point to Incrblob structures.
100797*/
100798typedef struct Incrblob Incrblob;
100799struct Incrblob {
100800 int nByte; /* Size of open blob, in bytes */
100801 int iOffset; /* Byte offset of blob in cursor data */
100802 u16 iCol; /* Table column this handle is open on */
100803 BtCursor *pCsr; /* Cursor pointing at blob row */
100804 sqlite3_stmt *pStmt; /* Statement holding cursor open */
100805 sqlite3 *db; /* The associated database */
100806 char *zDb; /* Database name */
100807 Table *pTab; /* Table object */
100808};
100809
100810
100811/*
100812** This function is used by both blob_open() and blob_reopen(). It seeks
100813** the b-tree cursor associated with blob handle p to point to row iRow.
100814** If successful, SQLITE_OK is returned and subsequent calls to
100815** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
100816**
100817** If an error occurs, or if the specified row does not exist or does not
100818** contain a value of type TEXT or BLOB in the column nominated when the
100819** blob handle was opened, then an error code is returned and *pzErr may
100820** be set to point to a buffer containing an error message. It is the
100821** responsibility of the caller to free the error message buffer using
100822** sqlite3DbFree().
100823**
100824** If an error does occur, then the b-tree cursor is closed. All subsequent
100825** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
100826** immediately return SQLITE_ABORT.
100827*/
100828static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
100829 int rc; /* Error code */
100830 char *zErr = 0; /* Error message */
100831 Vdbe *v = (Vdbe *)p->pStmt;
100832
100833 /* Set the value of register r[1] in the SQL statement to integer iRow.
100834 ** This is done directly as a performance optimization
100835 */
100836 v->aMem[1].flags = MEM_Int;
100837 v->aMem[1].u.i = iRow;
100838
100839 /* If the statement has been run before (and is paused at the OP_ResultRow)
100840 ** then back it up to the point where it does the OP_NotExists. This could
100841 ** have been down with an extra OP_Goto, but simply setting the program
100842 ** counter is faster. */
100843 if( v->pc>4 ){
100844 v->pc = 4;
100845 assert( v->aOp[v->pc].opcode==OP_NotExists );
100846 rc = sqlite3VdbeExec(p: v);
100847 }else{
100848 rc = sqlite3_step(pStmt: p->pStmt);
100849 }
100850 if( rc==SQLITE_ROW ){
100851 VdbeCursor *pC = v->apCsr[0];
100852 u32 type;
100853 assert( pC!=0 );
100854 assert( pC->eCurType==CURTYPE_BTREE );
100855 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
100856 testcase( pC->nHdrParsed==p->iCol );
100857 testcase( pC->nHdrParsed==p->iCol+1 );
100858 if( type<12 ){
100859 zErr = sqlite3MPrintf(db: p->db, zFormat: "cannot open value of type %s",
100860 type==0?"null": type==7?"real": "integer"
100861 );
100862 rc = SQLITE_ERROR;
100863 sqlite3_finalize(pStmt: p->pStmt);
100864 p->pStmt = 0;
100865 }else{
100866 p->iOffset = pC->aType[p->iCol + pC->nField];
100867 p->nByte = sqlite3VdbeSerialTypeLen(serial_type: type);
100868 p->pCsr = pC->uc.pCursor;
100869 sqlite3BtreeIncrblobCursor(pCur: p->pCsr);
100870 }
100871 }
100872
100873 if( rc==SQLITE_ROW ){
100874 rc = SQLITE_OK;
100875 }else if( p->pStmt ){
100876 rc = sqlite3_finalize(pStmt: p->pStmt);
100877 p->pStmt = 0;
100878 if( rc==SQLITE_OK ){
100879 zErr = sqlite3MPrintf(db: p->db, zFormat: "no such rowid: %lld", iRow);
100880 rc = SQLITE_ERROR;
100881 }else{
100882 zErr = sqlite3MPrintf(db: p->db, zFormat: "%s", sqlite3_errmsg(p->db));
100883 }
100884 }
100885
100886 assert( rc!=SQLITE_OK || zErr==0 );
100887 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
100888
100889 *pzErr = zErr;
100890 return rc;
100891}
100892
100893/*
100894** Open a blob handle.
100895*/
100896SQLITE_API int sqlite3_blob_open(
100897 sqlite3* db, /* The database connection */
100898 const char *zDb, /* The attached database containing the blob */
100899 const char *zTable, /* The table containing the blob */
100900 const char *zColumn, /* The column containing the blob */
100901 sqlite_int64 iRow, /* The row containing the glob */
100902 int wrFlag, /* True -> read/write access, false -> read-only */
100903 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
100904){
100905 int nAttempt = 0;
100906 int iCol; /* Index of zColumn in row-record */
100907 int rc = SQLITE_OK;
100908 char *zErr = 0;
100909 Table *pTab;
100910 Incrblob *pBlob = 0;
100911 Parse sParse;
100912
100913#ifdef SQLITE_ENABLE_API_ARMOR
100914 if( ppBlob==0 ){
100915 return SQLITE_MISUSE_BKPT;
100916 }
100917#endif
100918 *ppBlob = 0;
100919#ifdef SQLITE_ENABLE_API_ARMOR
100920 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
100921 return SQLITE_MISUSE_BKPT;
100922 }
100923#endif
100924 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
100925
100926 sqlite3_mutex_enter(p: db->mutex);
100927
100928 pBlob = (Incrblob *)sqlite3DbMallocZero(db, n: sizeof(Incrblob));
100929 while(1){
100930 sqlite3ParseObjectInit(&sParse,db);
100931 if( !pBlob ) goto blob_open_out;
100932 sqlite3DbFree(db, p: zErr);
100933 zErr = 0;
100934
100935 sqlite3BtreeEnterAll(db);
100936 pTab = sqlite3LocateTable(&sParse, flags: 0, zTable, zDb);
100937 if( pTab && IsVirtual(pTab) ){
100938 pTab = 0;
100939 sqlite3ErrorMsg(pParse: &sParse, zFormat: "cannot open virtual table: %s", zTable);
100940 }
100941 if( pTab && !HasRowid(pTab) ){
100942 pTab = 0;
100943 sqlite3ErrorMsg(pParse: &sParse, zFormat: "cannot open table without rowid: %s", zTable);
100944 }
100945#ifndef SQLITE_OMIT_VIEW
100946 if( pTab && IsView(pTab) ){
100947 pTab = 0;
100948 sqlite3ErrorMsg(pParse: &sParse, zFormat: "cannot open view: %s", zTable);
100949 }
100950#endif
100951 if( !pTab ){
100952 if( sParse.zErrMsg ){
100953 sqlite3DbFree(db, p: zErr);
100954 zErr = sParse.zErrMsg;
100955 sParse.zErrMsg = 0;
100956 }
100957 rc = SQLITE_ERROR;
100958 sqlite3BtreeLeaveAll(db);
100959 goto blob_open_out;
100960 }
100961 pBlob->pTab = pTab;
100962 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
100963
100964 /* Now search pTab for the exact column. */
100965 for(iCol=0; iCol<pTab->nCol; iCol++) {
100966 if( sqlite3StrICmp(zLeft: pTab->aCol[iCol].zCnName, zRight: zColumn)==0 ){
100967 break;
100968 }
100969 }
100970 if( iCol==pTab->nCol ){
100971 sqlite3DbFree(db, p: zErr);
100972 zErr = sqlite3MPrintf(db, zFormat: "no such column: \"%s\"", zColumn);
100973 rc = SQLITE_ERROR;
100974 sqlite3BtreeLeaveAll(db);
100975 goto blob_open_out;
100976 }
100977
100978 /* If the value is being opened for writing, check that the
100979 ** column is not indexed, and that it is not part of a foreign key.
100980 */
100981 if( wrFlag ){
100982 const char *zFault = 0;
100983 Index *pIdx;
100984#ifndef SQLITE_OMIT_FOREIGN_KEY
100985 if( db->flags&SQLITE_ForeignKeys ){
100986 /* Check that the column is not part of an FK child key definition. It
100987 ** is not necessary to check if it is part of a parent key, as parent
100988 ** key columns must be indexed. The check below will pick up this
100989 ** case. */
100990 FKey *pFKey;
100991 assert( IsOrdinaryTable(pTab) );
100992 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
100993 int j;
100994 for(j=0; j<pFKey->nCol; j++){
100995 if( pFKey->aCol[j].iFrom==iCol ){
100996 zFault = "foreign key";
100997 }
100998 }
100999 }
101000 }
101001#endif
101002 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
101003 int j;
101004 for(j=0; j<pIdx->nKeyCol; j++){
101005 /* FIXME: Be smarter about indexes that use expressions */
101006 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
101007 zFault = "indexed";
101008 }
101009 }
101010 }
101011 if( zFault ){
101012 sqlite3DbFree(db, p: zErr);
101013 zErr = sqlite3MPrintf(db, zFormat: "cannot open %s column for writing", zFault);
101014 rc = SQLITE_ERROR;
101015 sqlite3BtreeLeaveAll(db);
101016 goto blob_open_out;
101017 }
101018 }
101019
101020 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse: &sParse);
101021 assert( pBlob->pStmt || db->mallocFailed );
101022 if( pBlob->pStmt ){
101023
101024 /* This VDBE program seeks a btree cursor to the identified
101025 ** db/table/row entry. The reason for using a vdbe program instead
101026 ** of writing code to use the b-tree layer directly is that the
101027 ** vdbe program will take advantage of the various transaction,
101028 ** locking and error handling infrastructure built into the vdbe.
101029 **
101030 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
101031 ** Code external to the Vdbe then "borrows" the b-tree cursor and
101032 ** uses it to implement the blob_read(), blob_write() and
101033 ** blob_bytes() functions.
101034 **
101035 ** The sqlite3_blob_close() function finalizes the vdbe program,
101036 ** which closes the b-tree cursor and (possibly) commits the
101037 ** transaction.
101038 */
101039 static const int iLn = VDBE_OFFSET_LINENO(2);
101040 static const VdbeOpList openBlob[] = {
101041 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
101042 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
101043 /* blobSeekToRow() will initialize r[1] to the desired rowid */
101044 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
101045 {OP_Column, 0, 0, 1}, /* 3 */
101046 {OP_ResultRow, 1, 0, 0}, /* 4 */
101047 {OP_Halt, 0, 0, 0}, /* 5 */
101048 };
101049 Vdbe *v = (Vdbe *)pBlob->pStmt;
101050 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
101051 VdbeOp *aOp;
101052
101053 sqlite3VdbeAddOp4Int(p: v, OP_Transaction, p1: iDb, p2: wrFlag,
101054 p3: pTab->pSchema->schema_cookie,
101055 p4: pTab->pSchema->iGeneration);
101056 sqlite3VdbeChangeP5(p: v, p5: 1);
101057 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
101058 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(openBlob), aOp: openBlob, iLineno: iLn);
101059
101060 /* Make sure a mutex is held on the table to be accessed */
101061 sqlite3VdbeUsesBtree(p: v, i: iDb);
101062
101063 if( db->mallocFailed==0 ){
101064 assert( aOp!=0 );
101065 /* Configure the OP_TableLock instruction */
101066#ifdef SQLITE_OMIT_SHARED_CACHE
101067 aOp[0].opcode = OP_Noop;
101068#else
101069 aOp[0].p1 = iDb;
101070 aOp[0].p2 = pTab->tnum;
101071 aOp[0].p3 = wrFlag;
101072 sqlite3VdbeChangeP4(p: v, addr: 2, zP4: pTab->zName, P4_TRANSIENT);
101073 }
101074 if( db->mallocFailed==0 ){
101075#endif
101076
101077 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
101078 ** parameter of the other to pTab->tnum. */
101079 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
101080 aOp[1].p2 = pTab->tnum;
101081 aOp[1].p3 = iDb;
101082
101083 /* Configure the number of columns. Configure the cursor to
101084 ** think that the table has one more column than it really
101085 ** does. An OP_Column to retrieve this imaginary column will
101086 ** always return an SQL NULL. This is useful because it means
101087 ** we can invoke OP_Column to fill in the vdbe cursors type
101088 ** and offset cache without causing any IO.
101089 */
101090 aOp[1].p4type = P4_INT32;
101091 aOp[1].p4.i = pTab->nCol+1;
101092 aOp[3].p2 = pTab->nCol;
101093
101094 sParse.nVar = 0;
101095 sParse.nMem = 1;
101096 sParse.nTab = 1;
101097 sqlite3VdbeMakeReady(p: v, pParse: &sParse);
101098 }
101099 }
101100
101101 pBlob->iCol = iCol;
101102 pBlob->db = db;
101103 sqlite3BtreeLeaveAll(db);
101104 if( db->mallocFailed ){
101105 goto blob_open_out;
101106 }
101107 rc = blobSeekToRow(p: pBlob, iRow, pzErr: &zErr);
101108 if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY || rc!=SQLITE_SCHEMA ) break;
101109 sqlite3ParseObjectReset(&sParse);
101110 }
101111
101112blob_open_out:
101113 if( rc==SQLITE_OK && db->mallocFailed==0 ){
101114 *ppBlob = (sqlite3_blob *)pBlob;
101115 }else{
101116 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize(p: (Vdbe *)pBlob->pStmt);
101117 sqlite3DbFree(db, p: pBlob);
101118 }
101119 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: (zErr ? "%s" : (char*)0), zErr);
101120 sqlite3DbFree(db, p: zErr);
101121 sqlite3ParseObjectReset(&sParse);
101122 rc = sqlite3ApiExit(db, rc);
101123 sqlite3_mutex_leave(p: db->mutex);
101124 return rc;
101125}
101126
101127/*
101128** Close a blob handle that was previously created using
101129** sqlite3_blob_open().
101130*/
101131SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
101132 Incrblob *p = (Incrblob *)pBlob;
101133 int rc;
101134 sqlite3 *db;
101135
101136 if( p ){
101137 sqlite3_stmt *pStmt = p->pStmt;
101138 db = p->db;
101139 sqlite3_mutex_enter(p: db->mutex);
101140 sqlite3DbFree(db, p);
101141 sqlite3_mutex_leave(p: db->mutex);
101142 rc = sqlite3_finalize(pStmt);
101143 }else{
101144 rc = SQLITE_OK;
101145 }
101146 return rc;
101147}
101148
101149/*
101150** Perform a read or write operation on a blob
101151*/
101152static int blobReadWrite(
101153 sqlite3_blob *pBlob,
101154 void *z,
101155 int n,
101156 int iOffset,
101157 int (*xCall)(BtCursor*, u32, u32, void*)
101158){
101159 int rc;
101160 Incrblob *p = (Incrblob *)pBlob;
101161 Vdbe *v;
101162 sqlite3 *db;
101163
101164 if( p==0 ) return SQLITE_MISUSE_BKPT;
101165 db = p->db;
101166 sqlite3_mutex_enter(p: db->mutex);
101167 v = (Vdbe*)p->pStmt;
101168
101169 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
101170 /* Request is out of range. Return a transient error. */
101171 rc = SQLITE_ERROR;
101172 }else if( v==0 ){
101173 /* If there is no statement handle, then the blob-handle has
101174 ** already been invalidated. Return SQLITE_ABORT in this case.
101175 */
101176 rc = SQLITE_ABORT;
101177 }else{
101178 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
101179 ** returned, clean-up the statement handle.
101180 */
101181 assert( db == v->db );
101182 sqlite3BtreeEnterCursor(pCur: p->pCsr);
101183
101184#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
101185 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
101186 /* If a pre-update hook is registered and this is a write cursor,
101187 ** invoke it here.
101188 **
101189 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
101190 ** operation should really be an SQLITE_UPDATE. This is probably
101191 ** incorrect, but is convenient because at this point the new.* values
101192 ** are not easily obtainable. And for the sessions module, an
101193 ** SQLITE_UPDATE where the PK columns do not change is handled in the
101194 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
101195 ** slightly more efficient). Since you cannot write to a PK column
101196 ** using the incremental-blob API, this works. For the sessions module
101197 ** anyhow.
101198 */
101199 sqlite3_int64 iKey;
101200 iKey = sqlite3BtreeIntegerKey(p->pCsr);
101201 assert( v->apCsr[0]!=0 );
101202 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
101203 sqlite3VdbePreUpdateHook(
101204 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
101205 );
101206 }
101207#endif
101208
101209 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
101210 sqlite3BtreeLeaveCursor(pCur: p->pCsr);
101211 if( rc==SQLITE_ABORT ){
101212 sqlite3VdbeFinalize(p: v);
101213 p->pStmt = 0;
101214 }else{
101215 v->rc = rc;
101216 }
101217 }
101218 sqlite3Error(db, err_code: rc);
101219 rc = sqlite3ApiExit(db, rc);
101220 sqlite3_mutex_leave(p: db->mutex);
101221 return rc;
101222}
101223
101224/*
101225** Read data from a blob handle.
101226*/
101227SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
101228 return blobReadWrite(pBlob, z, n, iOffset, xCall: sqlite3BtreePayloadChecked);
101229}
101230
101231/*
101232** Write data to a blob handle.
101233*/
101234SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
101235 return blobReadWrite(pBlob, z: (void *)z, n, iOffset, xCall: sqlite3BtreePutData);
101236}
101237
101238/*
101239** Query a blob handle for the size of the data.
101240**
101241** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
101242** so no mutex is required for access.
101243*/
101244SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
101245 Incrblob *p = (Incrblob *)pBlob;
101246 return (p && p->pStmt) ? p->nByte : 0;
101247}
101248
101249/*
101250** Move an existing blob handle to point to a different row of the same
101251** database table.
101252**
101253** If an error occurs, or if the specified row does not exist or does not
101254** contain a blob or text value, then an error code is returned and the
101255** database handle error code and message set. If this happens, then all
101256** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
101257** immediately return SQLITE_ABORT.
101258*/
101259SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
101260 int rc;
101261 Incrblob *p = (Incrblob *)pBlob;
101262 sqlite3 *db;
101263
101264 if( p==0 ) return SQLITE_MISUSE_BKPT;
101265 db = p->db;
101266 sqlite3_mutex_enter(p: db->mutex);
101267
101268 if( p->pStmt==0 ){
101269 /* If there is no statement handle, then the blob-handle has
101270 ** already been invalidated. Return SQLITE_ABORT in this case.
101271 */
101272 rc = SQLITE_ABORT;
101273 }else{
101274 char *zErr;
101275 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
101276 rc = blobSeekToRow(p, iRow, pzErr: &zErr);
101277 if( rc!=SQLITE_OK ){
101278 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: (zErr ? "%s" : (char*)0), zErr);
101279 sqlite3DbFree(db, p: zErr);
101280 }
101281 assert( rc!=SQLITE_SCHEMA );
101282 }
101283
101284 rc = sqlite3ApiExit(db, rc);
101285 assert( rc==SQLITE_OK || p->pStmt==0 );
101286 sqlite3_mutex_leave(p: db->mutex);
101287 return rc;
101288}
101289
101290#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
101291
101292/************** End of vdbeblob.c ********************************************/
101293/************** Begin file vdbesort.c ****************************************/
101294/*
101295** 2011-07-09
101296**
101297** The author disclaims copyright to this source code. In place of
101298** a legal notice, here is a blessing:
101299**
101300** May you do good and not evil.
101301** May you find forgiveness for yourself and forgive others.
101302** May you share freely, never taking more than you give.
101303**
101304*************************************************************************
101305** This file contains code for the VdbeSorter object, used in concert with
101306** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
101307** or by SELECT statements with ORDER BY clauses that cannot be satisfied
101308** using indexes and without LIMIT clauses.
101309**
101310** The VdbeSorter object implements a multi-threaded external merge sort
101311** algorithm that is efficient even if the number of elements being sorted
101312** exceeds the available memory.
101313**
101314** Here is the (internal, non-API) interface between this module and the
101315** rest of the SQLite system:
101316**
101317** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
101318**
101319** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
101320** object. The row is a binary blob in the
101321** OP_MakeRecord format that contains both
101322** the ORDER BY key columns and result columns
101323** in the case of a SELECT w/ ORDER BY, or
101324** the complete record for an index entry
101325** in the case of a CREATE INDEX.
101326**
101327** sqlite3VdbeSorterRewind() Sort all content previously added.
101328** Position the read cursor on the
101329** first sorted element.
101330**
101331** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
101332** element.
101333**
101334** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
101335** row currently under the read cursor.
101336**
101337** sqlite3VdbeSorterCompare() Compare the binary blob for the row
101338** currently under the read cursor against
101339** another binary blob X and report if
101340** X is strictly less than the read cursor.
101341** Used to enforce uniqueness in a
101342** CREATE UNIQUE INDEX statement.
101343**
101344** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
101345** all resources.
101346**
101347** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
101348** is like Close() followed by Init() only
101349** much faster.
101350**
101351** The interfaces above must be called in a particular order. Write() can
101352** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
101353** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
101354**
101355** Init()
101356** for each record: Write()
101357** Rewind()
101358** Rowkey()/Compare()
101359** Next()
101360** Close()
101361**
101362** Algorithm:
101363**
101364** Records passed to the sorter via calls to Write() are initially held
101365** unsorted in main memory. Assuming the amount of memory used never exceeds
101366** a threshold, when Rewind() is called the set of records is sorted using
101367** an in-memory merge sort. In this case, no temporary files are required
101368** and subsequent calls to Rowkey(), Next() and Compare() read records
101369** directly from main memory.
101370**
101371** If the amount of space used to store records in main memory exceeds the
101372** threshold, then the set of records currently in memory are sorted and
101373** written to a temporary file in "Packed Memory Array" (PMA) format.
101374** A PMA created at this point is known as a "level-0 PMA". Higher levels
101375** of PMAs may be created by merging existing PMAs together - for example
101376** merging two or more level-0 PMAs together creates a level-1 PMA.
101377**
101378** The threshold for the amount of main memory to use before flushing
101379** records to a PMA is roughly the same as the limit configured for the
101380** page-cache of the main database. Specifically, the threshold is set to
101381** the value returned by "PRAGMA main.page_size" multiplied by
101382** that returned by "PRAGMA main.cache_size", in bytes.
101383**
101384** If the sorter is running in single-threaded mode, then all PMAs generated
101385** are appended to a single temporary file. Or, if the sorter is running in
101386** multi-threaded mode then up to (N+1) temporary files may be opened, where
101387** N is the configured number of worker threads. In this case, instead of
101388** sorting the records and writing the PMA to a temporary file itself, the
101389** calling thread usually launches a worker thread to do so. Except, if
101390** there are already N worker threads running, the main thread does the work
101391** itself.
101392**
101393** The sorter is running in multi-threaded mode if (a) the library was built
101394** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
101395** than zero, and (b) worker threads have been enabled at runtime by calling
101396** "PRAGMA threads=N" with some value of N greater than 0.
101397**
101398** When Rewind() is called, any data remaining in memory is flushed to a
101399** final PMA. So at this point the data is stored in some number of sorted
101400** PMAs within temporary files on disk.
101401**
101402** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
101403** sorter is running in single-threaded mode, then these PMAs are merged
101404** incrementally as keys are retrieved from the sorter by the VDBE. The
101405** MergeEngine object, described in further detail below, performs this
101406** merge.
101407**
101408** Or, if running in multi-threaded mode, then a background thread is
101409** launched to merge the existing PMAs. Once the background thread has
101410** merged T bytes of data into a single sorted PMA, the main thread
101411** begins reading keys from that PMA while the background thread proceeds
101412** with merging the next T bytes of data. And so on.
101413**
101414** Parameter T is set to half the value of the memory threshold used
101415** by Write() above to determine when to create a new PMA.
101416**
101417** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
101418** Rewind() is called, then a hierarchy of incremental-merges is used.
101419** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
101420** disk are merged together. Then T bytes of data from the second set, and
101421** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
101422** PMAs at a time. This done is to improve locality.
101423**
101424** If running in multi-threaded mode and there are more than
101425** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
101426** than one background thread may be created. Specifically, there may be
101427** one background thread for each temporary file on disk, and one background
101428** thread to merge the output of each of the others to a single PMA for
101429** the main thread to read from.
101430*/
101431/* #include "sqliteInt.h" */
101432/* #include "vdbeInt.h" */
101433
101434/*
101435** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
101436** messages to stderr that may be helpful in understanding the performance
101437** characteristics of the sorter in multi-threaded mode.
101438*/
101439#if 0
101440# define SQLITE_DEBUG_SORTER_THREADS 1
101441#endif
101442
101443/*
101444** Hard-coded maximum amount of data to accumulate in memory before flushing
101445** to a level 0 PMA. The purpose of this limit is to prevent various integer
101446** overflows. 512MiB.
101447*/
101448#define SQLITE_MAX_PMASZ (1<<29)
101449
101450/*
101451** Private objects used by the sorter
101452*/
101453typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
101454typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
101455typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
101456typedef struct SorterRecord SorterRecord; /* A record being sorted */
101457typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
101458typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
101459typedef struct SorterList SorterList; /* In-memory list of records */
101460typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
101461
101462/*
101463** A container for a temp file handle and the current amount of data
101464** stored in the file.
101465*/
101466struct SorterFile {
101467 sqlite3_file *pFd; /* File handle */
101468 i64 iEof; /* Bytes of data stored in pFd */
101469};
101470
101471/*
101472** An in-memory list of objects to be sorted.
101473**
101474** If aMemory==0 then each object is allocated separately and the objects
101475** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
101476** are stored in the aMemory[] bulk memory, one right after the other, and
101477** are connected using SorterRecord.u.iNext.
101478*/
101479struct SorterList {
101480 SorterRecord *pList; /* Linked list of records */
101481 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
101482 int szPMA; /* Size of pList as PMA in bytes */
101483};
101484
101485/*
101486** The MergeEngine object is used to combine two or more smaller PMAs into
101487** one big PMA using a merge operation. Separate PMAs all need to be
101488** combined into one big PMA in order to be able to step through the sorted
101489** records in order.
101490**
101491** The aReadr[] array contains a PmaReader object for each of the PMAs being
101492** merged. An aReadr[] object either points to a valid key or else is at EOF.
101493** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
101494** For the purposes of the paragraphs below, we assume that the array is
101495** actually N elements in size, where N is the smallest power of 2 greater
101496** to or equal to the number of PMAs being merged. The extra aReadr[] elements
101497** are treated as if they are empty (always at EOF).
101498**
101499** The aTree[] array is also N elements in size. The value of N is stored in
101500** the MergeEngine.nTree variable.
101501**
101502** The final (N/2) elements of aTree[] contain the results of comparing
101503** pairs of PMA keys together. Element i contains the result of
101504** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
101505** aTree element is set to the index of it.
101506**
101507** For the purposes of this comparison, EOF is considered greater than any
101508** other key value. If the keys are equal (only possible with two EOF
101509** values), it doesn't matter which index is stored.
101510**
101511** The (N/4) elements of aTree[] that precede the final (N/2) described
101512** above contains the index of the smallest of each block of 4 PmaReaders
101513** And so on. So that aTree[1] contains the index of the PmaReader that
101514** currently points to the smallest key value. aTree[0] is unused.
101515**
101516** Example:
101517**
101518** aReadr[0] -> Banana
101519** aReadr[1] -> Feijoa
101520** aReadr[2] -> Elderberry
101521** aReadr[3] -> Currant
101522** aReadr[4] -> Grapefruit
101523** aReadr[5] -> Apple
101524** aReadr[6] -> Durian
101525** aReadr[7] -> EOF
101526**
101527** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
101528**
101529** The current element is "Apple" (the value of the key indicated by
101530** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
101531** be advanced to the next key in its segment. Say the next key is
101532** "Eggplant":
101533**
101534** aReadr[5] -> Eggplant
101535**
101536** The contents of aTree[] are updated first by comparing the new PmaReader
101537** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
101538** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
101539** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
101540** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
101541** so the value written into element 1 of the array is 0. As follows:
101542**
101543** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
101544**
101545** In other words, each time we advance to the next sorter element, log2(N)
101546** key comparison operations are required, where N is the number of segments
101547** being merged (rounded up to the next power of 2).
101548*/
101549struct MergeEngine {
101550 int nTree; /* Used size of aTree/aReadr (power of 2) */
101551 SortSubtask *pTask; /* Used by this thread only */
101552 int *aTree; /* Current state of incremental merge */
101553 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
101554};
101555
101556/*
101557** This object represents a single thread of control in a sort operation.
101558** Exactly VdbeSorter.nTask instances of this object are allocated
101559** as part of each VdbeSorter object. Instances are never allocated any
101560** other way. VdbeSorter.nTask is set to the number of worker threads allowed
101561** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
101562** single-threaded operation, there is exactly one instance of this object
101563** and for multi-threaded operation there are two or more instances.
101564**
101565** Essentially, this structure contains all those fields of the VdbeSorter
101566** structure for which each thread requires a separate instance. For example,
101567** each thread requeries its own UnpackedRecord object to unpack records in
101568** as part of comparison operations.
101569**
101570** Before a background thread is launched, variable bDone is set to 0. Then,
101571** right before it exits, the thread itself sets bDone to 1. This is used for
101572** two purposes:
101573**
101574** 1. When flushing the contents of memory to a level-0 PMA on disk, to
101575** attempt to select a SortSubtask for which there is not already an
101576** active background thread (since doing so causes the main thread
101577** to block until it finishes).
101578**
101579** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
101580** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
101581** block provoke debugging output.
101582**
101583** In both cases, the effects of the main thread seeing (bDone==0) even
101584** after the thread has finished are not dire. So we don't worry about
101585** memory barriers and such here.
101586*/
101587typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
101588struct SortSubtask {
101589 SQLiteThread *pThread; /* Background thread, if any */
101590 int bDone; /* Set if thread is finished but not joined */
101591 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
101592 UnpackedRecord *pUnpacked; /* Space to unpack a record */
101593 SorterList list; /* List for thread to write to a PMA */
101594 int nPMA; /* Number of PMAs currently in file */
101595 SorterCompare xCompare; /* Compare function to use */
101596 SorterFile file; /* Temp file for level-0 PMAs */
101597 SorterFile file2; /* Space for other PMAs */
101598};
101599
101600
101601/*
101602** Main sorter structure. A single instance of this is allocated for each
101603** sorter cursor created by the VDBE.
101604**
101605** mxKeysize:
101606** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
101607** this variable is updated so as to be set to the size on disk of the
101608** largest record in the sorter.
101609*/
101610struct VdbeSorter {
101611 int mnPmaSize; /* Minimum PMA size, in bytes */
101612 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
101613 int mxKeysize; /* Largest serialized key seen so far */
101614 int pgsz; /* Main database page size */
101615 PmaReader *pReader; /* Readr data from here after Rewind() */
101616 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
101617 sqlite3 *db; /* Database connection */
101618 KeyInfo *pKeyInfo; /* How to compare records */
101619 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
101620 SorterList list; /* List of in-memory records */
101621 int iMemory; /* Offset of free space in list.aMemory */
101622 int nMemory; /* Size of list.aMemory allocation in bytes */
101623 u8 bUsePMA; /* True if one or more PMAs created */
101624 u8 bUseThreads; /* True to use background threads */
101625 u8 iPrev; /* Previous thread used to flush PMA */
101626 u8 nTask; /* Size of aTask[] array */
101627 u8 typeMask;
101628 SortSubtask aTask[1]; /* One or more subtasks */
101629};
101630
101631#define SORTER_TYPE_INTEGER 0x01
101632#define SORTER_TYPE_TEXT 0x02
101633
101634/*
101635** An instance of the following object is used to read records out of a
101636** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
101637** aKey might point into aMap or into aBuffer. If neither of those locations
101638** contain a contiguous representation of the key, then aAlloc is allocated
101639** and the key is copied into aAlloc and aKey is made to point to aAlloc.
101640**
101641** pFd==0 at EOF.
101642*/
101643struct PmaReader {
101644 i64 iReadOff; /* Current read offset */
101645 i64 iEof; /* 1 byte past EOF for this PmaReader */
101646 int nAlloc; /* Bytes of space at aAlloc */
101647 int nKey; /* Number of bytes in key */
101648 sqlite3_file *pFd; /* File handle we are reading from */
101649 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
101650 u8 *aKey; /* Pointer to current key */
101651 u8 *aBuffer; /* Current read buffer */
101652 int nBuffer; /* Size of read buffer in bytes */
101653 u8 *aMap; /* Pointer to mapping of entire file */
101654 IncrMerger *pIncr; /* Incremental merger */
101655};
101656
101657/*
101658** Normally, a PmaReader object iterates through an existing PMA stored
101659** within a temp file. However, if the PmaReader.pIncr variable points to
101660** an object of the following type, it may be used to iterate/merge through
101661** multiple PMAs simultaneously.
101662**
101663** There are two types of IncrMerger object - single (bUseThread==0) and
101664** multi-threaded (bUseThread==1).
101665**
101666** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
101667** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
101668** size. When the IncrMerger is initialized, it reads enough data from
101669** pMerger to populate aFile[0]. It then sets variables within the
101670** corresponding PmaReader object to read from that file and kicks off
101671** a background thread to populate aFile[1] with the next mxSz bytes of
101672** sorted record data from pMerger.
101673**
101674** When the PmaReader reaches the end of aFile[0], it blocks until the
101675** background thread has finished populating aFile[1]. It then exchanges
101676** the contents of the aFile[0] and aFile[1] variables within this structure,
101677** sets the PmaReader fields to read from the new aFile[0] and kicks off
101678** another background thread to populate the new aFile[1]. And so on, until
101679** the contents of pMerger are exhausted.
101680**
101681** A single-threaded IncrMerger does not open any temporary files of its
101682** own. Instead, it has exclusive access to mxSz bytes of space beginning
101683** at offset iStartOff of file pTask->file2. And instead of using a
101684** background thread to prepare data for the PmaReader, with a single
101685** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
101686** keys from pMerger by the calling thread whenever the PmaReader runs out
101687** of data.
101688*/
101689struct IncrMerger {
101690 SortSubtask *pTask; /* Task that owns this merger */
101691 MergeEngine *pMerger; /* Merge engine thread reads data from */
101692 i64 iStartOff; /* Offset to start writing file at */
101693 int mxSz; /* Maximum bytes of data to store */
101694 int bEof; /* Set to true when merge is finished */
101695 int bUseThread; /* True to use a bg thread for this object */
101696 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
101697};
101698
101699/*
101700** An instance of this object is used for writing a PMA.
101701**
101702** The PMA is written one record at a time. Each record is of an arbitrary
101703** size. But I/O is more efficient if it occurs in page-sized blocks where
101704** each block is aligned on a page boundary. This object caches writes to
101705** the PMA so that aligned, page-size blocks are written.
101706*/
101707struct PmaWriter {
101708 int eFWErr; /* Non-zero if in an error state */
101709 u8 *aBuffer; /* Pointer to write buffer */
101710 int nBuffer; /* Size of write buffer in bytes */
101711 int iBufStart; /* First byte of buffer to write */
101712 int iBufEnd; /* Last byte of buffer to write */
101713 i64 iWriteOff; /* Offset of start of buffer in file */
101714 sqlite3_file *pFd; /* File handle to write to */
101715};
101716
101717/*
101718** This object is the header on a single record while that record is being
101719** held in memory and prior to being written out as part of a PMA.
101720**
101721** How the linked list is connected depends on how memory is being managed
101722** by this module. If using a separate allocation for each in-memory record
101723** (VdbeSorter.list.aMemory==0), then the list is always connected using the
101724** SorterRecord.u.pNext pointers.
101725**
101726** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
101727** then while records are being accumulated the list is linked using the
101728** SorterRecord.u.iNext offset. This is because the aMemory[] array may
101729** be sqlite3Realloc()ed while records are being accumulated. Once the VM
101730** has finished passing records to the sorter, or when the in-memory buffer
101731** is full, the list is sorted. As part of the sorting process, it is
101732** converted to use the SorterRecord.u.pNext pointers. See function
101733** vdbeSorterSort() for details.
101734*/
101735struct SorterRecord {
101736 int nVal; /* Size of the record in bytes */
101737 union {
101738 SorterRecord *pNext; /* Pointer to next record in list */
101739 int iNext; /* Offset within aMemory of next record */
101740 } u;
101741 /* The data for the record immediately follows this header */
101742};
101743
101744/* Return a pointer to the buffer containing the record data for SorterRecord
101745** object p. Should be used as if:
101746**
101747** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
101748*/
101749#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
101750
101751
101752/* Maximum number of PMAs that a single MergeEngine can merge */
101753#define SORTER_MAX_MERGE_COUNT 16
101754
101755static int vdbeIncrSwap(IncrMerger*);
101756static void vdbeIncrFree(IncrMerger *);
101757
101758/*
101759** Free all memory belonging to the PmaReader object passed as the
101760** argument. All structure fields are set to zero before returning.
101761*/
101762static void vdbePmaReaderClear(PmaReader *pReadr){
101763 sqlite3_free(p: pReadr->aAlloc);
101764 sqlite3_free(p: pReadr->aBuffer);
101765 if( pReadr->aMap ) sqlite3OsUnfetch(id: pReadr->pFd, iOff: 0, p: pReadr->aMap);
101766 vdbeIncrFree(pReadr->pIncr);
101767 memset(s: pReadr, c: 0, n: sizeof(PmaReader));
101768}
101769
101770/*
101771** Read the next nByte bytes of data from the PMA p.
101772** If successful, set *ppOut to point to a buffer containing the data
101773** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
101774** error code.
101775**
101776** The buffer returned in *ppOut is only valid until the
101777** next call to this function.
101778*/
101779static int vdbePmaReadBlob(
101780 PmaReader *p, /* PmaReader from which to take the blob */
101781 int nByte, /* Bytes of data to read */
101782 u8 **ppOut /* OUT: Pointer to buffer containing data */
101783){
101784 int iBuf; /* Offset within buffer to read from */
101785 int nAvail; /* Bytes of data available in buffer */
101786
101787 if( p->aMap ){
101788 *ppOut = &p->aMap[p->iReadOff];
101789 p->iReadOff += nByte;
101790 return SQLITE_OK;
101791 }
101792
101793 assert( p->aBuffer );
101794
101795 /* If there is no more data to be read from the buffer, read the next
101796 ** p->nBuffer bytes of data from the file into it. Or, if there are less
101797 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
101798 iBuf = p->iReadOff % p->nBuffer;
101799 if( iBuf==0 ){
101800 int nRead; /* Bytes to read from disk */
101801 int rc; /* sqlite3OsRead() return code */
101802
101803 /* Determine how many bytes of data to read. */
101804 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
101805 nRead = p->nBuffer;
101806 }else{
101807 nRead = (int)(p->iEof - p->iReadOff);
101808 }
101809 assert( nRead>0 );
101810
101811 /* Readr data from the file. Return early if an error occurs. */
101812 rc = sqlite3OsRead(id: p->pFd, pBuf: p->aBuffer, amt: nRead, offset: p->iReadOff);
101813 assert( rc!=SQLITE_IOERR_SHORT_READ );
101814 if( rc!=SQLITE_OK ) return rc;
101815 }
101816 nAvail = p->nBuffer - iBuf;
101817
101818 if( nByte<=nAvail ){
101819 /* The requested data is available in the in-memory buffer. In this
101820 ** case there is no need to make a copy of the data, just return a
101821 ** pointer into the buffer to the caller. */
101822 *ppOut = &p->aBuffer[iBuf];
101823 p->iReadOff += nByte;
101824 }else{
101825 /* The requested data is not all available in the in-memory buffer.
101826 ** In this case, allocate space at p->aAlloc[] to copy the requested
101827 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
101828 int nRem; /* Bytes remaining to copy */
101829
101830 /* Extend the p->aAlloc[] allocation if required. */
101831 if( p->nAlloc<nByte ){
101832 u8 *aNew;
101833 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
101834 while( nByte>nNew ) nNew = nNew*2;
101835 aNew = sqlite3Realloc(pOld: p->aAlloc, nBytes: nNew);
101836 if( !aNew ) return SQLITE_NOMEM_BKPT;
101837 p->nAlloc = nNew;
101838 p->aAlloc = aNew;
101839 }
101840
101841 /* Copy as much data as is available in the buffer into the start of
101842 ** p->aAlloc[]. */
101843 memcpy(dest: p->aAlloc, src: &p->aBuffer[iBuf], n: nAvail);
101844 p->iReadOff += nAvail;
101845 nRem = nByte - nAvail;
101846
101847 /* The following loop copies up to p->nBuffer bytes per iteration into
101848 ** the p->aAlloc[] buffer. */
101849 while( nRem>0 ){
101850 int rc; /* vdbePmaReadBlob() return code */
101851 int nCopy; /* Number of bytes to copy */
101852 u8 *aNext; /* Pointer to buffer to copy data from */
101853
101854 nCopy = nRem;
101855 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
101856 rc = vdbePmaReadBlob(p, nByte: nCopy, ppOut: &aNext);
101857 if( rc!=SQLITE_OK ) return rc;
101858 assert( aNext!=p->aAlloc );
101859 memcpy(dest: &p->aAlloc[nByte - nRem], src: aNext, n: nCopy);
101860 nRem -= nCopy;
101861 }
101862
101863 *ppOut = p->aAlloc;
101864 }
101865
101866 return SQLITE_OK;
101867}
101868
101869/*
101870** Read a varint from the stream of data accessed by p. Set *pnOut to
101871** the value read.
101872*/
101873static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
101874 int iBuf;
101875
101876 if( p->aMap ){
101877 p->iReadOff += sqlite3GetVarint(p: &p->aMap[p->iReadOff], v: pnOut);
101878 }else{
101879 iBuf = p->iReadOff % p->nBuffer;
101880 if( iBuf && (p->nBuffer-iBuf)>=9 ){
101881 p->iReadOff += sqlite3GetVarint(p: &p->aBuffer[iBuf], v: pnOut);
101882 }else{
101883 u8 aVarint[16], *a;
101884 int i = 0, rc;
101885 do{
101886 rc = vdbePmaReadBlob(p, nByte: 1, ppOut: &a);
101887 if( rc ) return rc;
101888 aVarint[(i++)&0xf] = a[0];
101889 }while( (a[0]&0x80)!=0 );
101890 sqlite3GetVarint(p: aVarint, v: pnOut);
101891 }
101892 }
101893
101894 return SQLITE_OK;
101895}
101896
101897/*
101898** Attempt to memory map file pFile. If successful, set *pp to point to the
101899** new mapping and return SQLITE_OK. If the mapping is not attempted
101900** (because the file is too large or the VFS layer is configured not to use
101901** mmap), return SQLITE_OK and set *pp to NULL.
101902**
101903** Or, if an error occurs, return an SQLite error code. The final value of
101904** *pp is undefined in this case.
101905*/
101906static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
101907 int rc = SQLITE_OK;
101908 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
101909 sqlite3_file *pFd = pFile->pFd;
101910 if( pFd->pMethods->iVersion>=3 ){
101911 rc = sqlite3OsFetch(id: pFd, iOff: 0, iAmt: (int)pFile->iEof, pp: (void**)pp);
101912 testcase( rc!=SQLITE_OK );
101913 }
101914 }
101915 return rc;
101916}
101917
101918/*
101919** Attach PmaReader pReadr to file pFile (if it is not already attached to
101920** that file) and seek it to offset iOff within the file. Return SQLITE_OK
101921** if successful, or an SQLite error code if an error occurs.
101922*/
101923static int vdbePmaReaderSeek(
101924 SortSubtask *pTask, /* Task context */
101925 PmaReader *pReadr, /* Reader whose cursor is to be moved */
101926 SorterFile *pFile, /* Sorter file to read from */
101927 i64 iOff /* Offset in pFile */
101928){
101929 int rc = SQLITE_OK;
101930
101931 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
101932
101933 if( sqlite3FaultSim(iTest: 201) ) return SQLITE_IOERR_READ;
101934 if( pReadr->aMap ){
101935 sqlite3OsUnfetch(id: pReadr->pFd, iOff: 0, p: pReadr->aMap);
101936 pReadr->aMap = 0;
101937 }
101938 pReadr->iReadOff = iOff;
101939 pReadr->iEof = pFile->iEof;
101940 pReadr->pFd = pFile->pFd;
101941
101942 rc = vdbeSorterMapFile(pTask, pFile, pp: &pReadr->aMap);
101943 if( rc==SQLITE_OK && pReadr->aMap==0 ){
101944 int pgsz = pTask->pSorter->pgsz;
101945 int iBuf = pReadr->iReadOff % pgsz;
101946 if( pReadr->aBuffer==0 ){
101947 pReadr->aBuffer = (u8*)sqlite3Malloc(n: pgsz);
101948 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
101949 pReadr->nBuffer = pgsz;
101950 }
101951 if( rc==SQLITE_OK && iBuf ){
101952 int nRead = pgsz - iBuf;
101953 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
101954 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
101955 }
101956 rc = sqlite3OsRead(
101957 id: pReadr->pFd, pBuf: &pReadr->aBuffer[iBuf], amt: nRead, offset: pReadr->iReadOff
101958 );
101959 testcase( rc!=SQLITE_OK );
101960 }
101961 }
101962
101963 return rc;
101964}
101965
101966/*
101967** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
101968** no error occurs, or an SQLite error code if one does.
101969*/
101970static int vdbePmaReaderNext(PmaReader *pReadr){
101971 int rc = SQLITE_OK; /* Return Code */
101972 u64 nRec = 0; /* Size of record in bytes */
101973
101974
101975 if( pReadr->iReadOff>=pReadr->iEof ){
101976 IncrMerger *pIncr = pReadr->pIncr;
101977 int bEof = 1;
101978 if( pIncr ){
101979 rc = vdbeIncrSwap(pIncr);
101980 if( rc==SQLITE_OK && pIncr->bEof==0 ){
101981 rc = vdbePmaReaderSeek(
101982 pTask: pIncr->pTask, pReadr, pFile: &pIncr->aFile[0], iOff: pIncr->iStartOff
101983 );
101984 bEof = 0;
101985 }
101986 }
101987
101988 if( bEof ){
101989 /* This is an EOF condition */
101990 vdbePmaReaderClear(pReadr);
101991 testcase( rc!=SQLITE_OK );
101992 return rc;
101993 }
101994 }
101995
101996 if( rc==SQLITE_OK ){
101997 rc = vdbePmaReadVarint(p: pReadr, pnOut: &nRec);
101998 }
101999 if( rc==SQLITE_OK ){
102000 pReadr->nKey = (int)nRec;
102001 rc = vdbePmaReadBlob(p: pReadr, nByte: (int)nRec, ppOut: &pReadr->aKey);
102002 testcase( rc!=SQLITE_OK );
102003 }
102004
102005 return rc;
102006}
102007
102008/*
102009** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
102010** starting at offset iStart and ending at offset iEof-1. This function
102011** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
102012** PMA is empty).
102013**
102014** If the pnByte parameter is NULL, then it is assumed that the file
102015** contains a single PMA, and that that PMA omits the initial length varint.
102016*/
102017static int vdbePmaReaderInit(
102018 SortSubtask *pTask, /* Task context */
102019 SorterFile *pFile, /* Sorter file to read from */
102020 i64 iStart, /* Start offset in pFile */
102021 PmaReader *pReadr, /* PmaReader to populate */
102022 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
102023){
102024 int rc;
102025
102026 assert( pFile->iEof>iStart );
102027 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
102028 assert( pReadr->aBuffer==0 );
102029 assert( pReadr->aMap==0 );
102030
102031 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iOff: iStart);
102032 if( rc==SQLITE_OK ){
102033 u64 nByte = 0; /* Size of PMA in bytes */
102034 rc = vdbePmaReadVarint(p: pReadr, pnOut: &nByte);
102035 pReadr->iEof = pReadr->iReadOff + nByte;
102036 *pnByte += nByte;
102037 }
102038
102039 if( rc==SQLITE_OK ){
102040 rc = vdbePmaReaderNext(pReadr);
102041 }
102042 return rc;
102043}
102044
102045/*
102046** A version of vdbeSorterCompare() that assumes that it has already been
102047** determined that the first field of key1 is equal to the first field of
102048** key2.
102049*/
102050static int vdbeSorterCompareTail(
102051 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
102052 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102053 const void *pKey1, int nKey1, /* Left side of comparison */
102054 const void *pKey2, int nKey2 /* Right side of comparison */
102055){
102056 UnpackedRecord *r2 = pTask->pUnpacked;
102057 if( *pbKey2Cached==0 ){
102058 sqlite3VdbeRecordUnpack(pKeyInfo: pTask->pSorter->pKeyInfo, nKey: nKey2, pKey: pKey2, p: r2);
102059 *pbKey2Cached = 1;
102060 }
102061 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2: r2, bSkip: 1);
102062}
102063
102064/*
102065** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
102066** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
102067** used by the comparison. Return the result of the comparison.
102068**
102069** If IN/OUT parameter *pbKey2Cached is true when this function is called,
102070** it is assumed that (pTask->pUnpacked) contains the unpacked version
102071** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
102072** version of key2 and *pbKey2Cached set to true before returning.
102073**
102074** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
102075** to SQLITE_NOMEM.
102076*/
102077static int vdbeSorterCompare(
102078 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
102079 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102080 const void *pKey1, int nKey1, /* Left side of comparison */
102081 const void *pKey2, int nKey2 /* Right side of comparison */
102082){
102083 UnpackedRecord *r2 = pTask->pUnpacked;
102084 if( !*pbKey2Cached ){
102085 sqlite3VdbeRecordUnpack(pKeyInfo: pTask->pSorter->pKeyInfo, nKey: nKey2, pKey: pKey2, p: r2);
102086 *pbKey2Cached = 1;
102087 }
102088 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2: r2);
102089}
102090
102091/*
102092** A specially optimized version of vdbeSorterCompare() that assumes that
102093** the first field of each key is a TEXT value and that the collation
102094** sequence to compare them with is BINARY.
102095*/
102096static int vdbeSorterCompareText(
102097 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
102098 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102099 const void *pKey1, int nKey1, /* Left side of comparison */
102100 const void *pKey2, int nKey2 /* Right side of comparison */
102101){
102102 const u8 * const p1 = (const u8 * const)pKey1;
102103 const u8 * const p2 = (const u8 * const)pKey2;
102104 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
102105 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
102106
102107 int n1;
102108 int n2;
102109 int res;
102110
102111 getVarint32NR(&p1[1], n1);
102112 getVarint32NR(&p2[1], n2);
102113 res = memcmp(s1: v1, s2: v2, n: (MIN(n1, n2) - 13)/2);
102114 if( res==0 ){
102115 res = n1 - n2;
102116 }
102117
102118 if( res==0 ){
102119 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
102120 res = vdbeSorterCompareTail(
102121 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
102122 );
102123 }
102124 }else{
102125 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
102126 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
102127 res = res * -1;
102128 }
102129 }
102130
102131 return res;
102132}
102133
102134/*
102135** A specially optimized version of vdbeSorterCompare() that assumes that
102136** the first field of each key is an INTEGER value.
102137*/
102138static int vdbeSorterCompareInt(
102139 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
102140 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102141 const void *pKey1, int nKey1, /* Left side of comparison */
102142 const void *pKey2, int nKey2 /* Right side of comparison */
102143){
102144 const u8 * const p1 = (const u8 * const)pKey1;
102145 const u8 * const p2 = (const u8 * const)pKey2;
102146 const int s1 = p1[1]; /* Left hand serial type */
102147 const int s2 = p2[1]; /* Right hand serial type */
102148 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
102149 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
102150 int res; /* Return value */
102151
102152 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
102153 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
102154
102155 if( s1==s2 ){
102156 /* The two values have the same sign. Compare using memcmp(). */
102157 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
102158 const u8 n = aLen[s1];
102159 int i;
102160 res = 0;
102161 for(i=0; i<n; i++){
102162 if( (res = v1[i] - v2[i])!=0 ){
102163 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
102164 res = v1[0] & 0x80 ? -1 : +1;
102165 }
102166 break;
102167 }
102168 }
102169 }else if( s1>7 && s2>7 ){
102170 res = s1 - s2;
102171 }else{
102172 if( s2>7 ){
102173 res = +1;
102174 }else if( s1>7 ){
102175 res = -1;
102176 }else{
102177 res = s1 - s2;
102178 }
102179 assert( res!=0 );
102180
102181 if( res>0 ){
102182 if( *v1 & 0x80 ) res = -1;
102183 }else{
102184 if( *v2 & 0x80 ) res = +1;
102185 }
102186 }
102187
102188 if( res==0 ){
102189 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
102190 res = vdbeSorterCompareTail(
102191 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
102192 );
102193 }
102194 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
102195 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
102196 res = res * -1;
102197 }
102198
102199 return res;
102200}
102201
102202/*
102203** Initialize the temporary index cursor just opened as a sorter cursor.
102204**
102205** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
102206** to determine the number of fields that should be compared from the
102207** records being sorted. However, if the value passed as argument nField
102208** is non-zero and the sorter is able to guarantee a stable sort, nField
102209** is used instead. This is used when sorting records for a CREATE INDEX
102210** statement. In this case, keys are always delivered to the sorter in
102211** order of the primary key, which happens to be make up the final part
102212** of the records being sorted. So if the sort is stable, there is never
102213** any reason to compare PK fields and they can be ignored for a small
102214** performance boost.
102215**
102216** The sorter can guarantee a stable sort when running in single-threaded
102217** mode, but not in multi-threaded mode.
102218**
102219** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
102220*/
102221SQLITE_PRIVATE int sqlite3VdbeSorterInit(
102222 sqlite3 *db, /* Database connection (for malloc()) */
102223 int nField, /* Number of key fields in each record */
102224 VdbeCursor *pCsr /* Cursor that holds the new sorter */
102225){
102226 int pgsz; /* Page size of main database */
102227 int i; /* Used to iterate through aTask[] */
102228 VdbeSorter *pSorter; /* The new sorter */
102229 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
102230 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
102231 int sz; /* Size of pSorter in bytes */
102232 int rc = SQLITE_OK;
102233#if SQLITE_MAX_WORKER_THREADS==0
102234# define nWorker 0
102235#else
102236 int nWorker;
102237#endif
102238
102239 /* Initialize the upper limit on the number of worker threads */
102240#if SQLITE_MAX_WORKER_THREADS>0
102241 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
102242 nWorker = 0;
102243 }else{
102244 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
102245 }
102246#endif
102247
102248 /* Do not allow the total number of threads (main thread + all workers)
102249 ** to exceed the maximum merge count */
102250#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
102251 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
102252 nWorker = SORTER_MAX_MERGE_COUNT-1;
102253 }
102254#endif
102255
102256 assert( pCsr->pKeyInfo );
102257 assert( !pCsr->isEphemeral );
102258 assert( pCsr->eCurType==CURTYPE_SORTER );
102259 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
102260 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
102261
102262 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, n: sz + szKeyInfo);
102263 pCsr->uc.pSorter = pSorter;
102264 if( pSorter==0 ){
102265 rc = SQLITE_NOMEM_BKPT;
102266 }else{
102267 Btree *pBt = db->aDb[0].pBt;
102268 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
102269 memcpy(dest: pKeyInfo, src: pCsr->pKeyInfo, n: szKeyInfo);
102270 pKeyInfo->db = 0;
102271 if( nField && nWorker==0 ){
102272 pKeyInfo->nKeyField = nField;
102273 }
102274 sqlite3BtreeEnter(p: pBt);
102275 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(p: pBt);
102276 sqlite3BtreeLeave(p: pBt);
102277 pSorter->nTask = nWorker + 1;
102278 pSorter->iPrev = (u8)(nWorker - 1);
102279 pSorter->bUseThreads = (pSorter->nTask>1);
102280 pSorter->db = db;
102281 for(i=0; i<pSorter->nTask; i++){
102282 SortSubtask *pTask = &pSorter->aTask[i];
102283 pTask->pSorter = pSorter;
102284 }
102285
102286 if( !sqlite3TempInMemory(db) ){
102287 i64 mxCache; /* Cache size in bytes*/
102288 u32 szPma = sqlite3GlobalConfig.szPma;
102289 pSorter->mnPmaSize = szPma * pgsz;
102290
102291 mxCache = db->aDb[0].pSchema->cache_size;
102292 if( mxCache<0 ){
102293 /* A negative cache-size value C indicates that the cache is abs(C)
102294 ** KiB in size. */
102295 mxCache = mxCache * -1024;
102296 }else{
102297 mxCache = mxCache * pgsz;
102298 }
102299 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
102300 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
102301
102302 /* Avoid large memory allocations if the application has requested
102303 ** SQLITE_CONFIG_SMALL_MALLOC. */
102304 if( sqlite3GlobalConfig.bSmallMalloc==0 ){
102305 assert( pSorter->iMemory==0 );
102306 pSorter->nMemory = pgsz;
102307 pSorter->list.aMemory = (u8*)sqlite3Malloc(n: pgsz);
102308 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
102309 }
102310 }
102311
102312 if( pKeyInfo->nAllField<13
102313 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
102314 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
102315 ){
102316 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
102317 }
102318 }
102319
102320 return rc;
102321}
102322#undef nWorker /* Defined at the top of this function */
102323
102324/*
102325** Free the list of sorted records starting at pRecord.
102326*/
102327static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
102328 SorterRecord *p;
102329 SorterRecord *pNext;
102330 for(p=pRecord; p; p=pNext){
102331 pNext = p->u.pNext;
102332 sqlite3DbFree(db, p);
102333 }
102334}
102335
102336/*
102337** Free all resources owned by the object indicated by argument pTask. All
102338** fields of *pTask are zeroed before returning.
102339*/
102340static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
102341 sqlite3DbFree(db, p: pTask->pUnpacked);
102342#if SQLITE_MAX_WORKER_THREADS>0
102343 /* pTask->list.aMemory can only be non-zero if it was handed memory
102344 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
102345 if( pTask->list.aMemory ){
102346 sqlite3_free(p: pTask->list.aMemory);
102347 }else
102348#endif
102349 {
102350 assert( pTask->list.aMemory==0 );
102351 vdbeSorterRecordFree(db: 0, pRecord: pTask->list.pList);
102352 }
102353 if( pTask->file.pFd ){
102354 sqlite3OsCloseFree(pFile: pTask->file.pFd);
102355 }
102356 if( pTask->file2.pFd ){
102357 sqlite3OsCloseFree(pFile: pTask->file2.pFd);
102358 }
102359 memset(s: pTask, c: 0, n: sizeof(SortSubtask));
102360}
102361
102362#ifdef SQLITE_DEBUG_SORTER_THREADS
102363static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
102364 i64 t;
102365 int iTask = (pTask - pTask->pSorter->aTask);
102366 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
102367 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
102368}
102369static void vdbeSorterRewindDebug(const char *zEvent){
102370 i64 t = 0;
102371 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
102372 if( ALWAYS(pVfs) ) sqlite3OsCurrentTimeInt64(pVfs, &t);
102373 fprintf(stderr, "%lld:X %s\n", t, zEvent);
102374}
102375static void vdbeSorterPopulateDebug(
102376 SortSubtask *pTask,
102377 const char *zEvent
102378){
102379 i64 t;
102380 int iTask = (pTask - pTask->pSorter->aTask);
102381 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
102382 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
102383}
102384static void vdbeSorterBlockDebug(
102385 SortSubtask *pTask,
102386 int bBlocked,
102387 const char *zEvent
102388){
102389 if( bBlocked ){
102390 i64 t;
102391 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
102392 fprintf(stderr, "%lld:main %s\n", t, zEvent);
102393 }
102394}
102395#else
102396# define vdbeSorterWorkDebug(x,y)
102397# define vdbeSorterRewindDebug(y)
102398# define vdbeSorterPopulateDebug(x,y)
102399# define vdbeSorterBlockDebug(x,y,z)
102400#endif
102401
102402#if SQLITE_MAX_WORKER_THREADS>0
102403/*
102404** Join thread pTask->thread.
102405*/
102406static int vdbeSorterJoinThread(SortSubtask *pTask){
102407 int rc = SQLITE_OK;
102408 if( pTask->pThread ){
102409#ifdef SQLITE_DEBUG_SORTER_THREADS
102410 int bDone = pTask->bDone;
102411#endif
102412 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
102413 vdbeSorterBlockDebug(pTask, !bDone, "enter");
102414 (void)sqlite3ThreadJoin(p: pTask->pThread, ppOut: &pRet);
102415 vdbeSorterBlockDebug(pTask, !bDone, "exit");
102416 rc = SQLITE_PTR_TO_INT(pRet);
102417 assert( pTask->bDone==1 );
102418 pTask->bDone = 0;
102419 pTask->pThread = 0;
102420 }
102421 return rc;
102422}
102423
102424/*
102425** Launch a background thread to run xTask(pIn).
102426*/
102427static int vdbeSorterCreateThread(
102428 SortSubtask *pTask, /* Thread will use this task object */
102429 void *(*xTask)(void*), /* Routine to run in a separate thread */
102430 void *pIn /* Argument passed into xTask() */
102431){
102432 assert( pTask->pThread==0 && pTask->bDone==0 );
102433 return sqlite3ThreadCreate(ppThread: &pTask->pThread, xTask, pIn);
102434}
102435
102436/*
102437** Join all outstanding threads launched by SorterWrite() to create
102438** level-0 PMAs.
102439*/
102440static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
102441 int rc = rcin;
102442 int i;
102443
102444 /* This function is always called by the main user thread.
102445 **
102446 ** If this function is being called after SorterRewind() has been called,
102447 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
102448 ** is currently attempt to join one of the other threads. To avoid a race
102449 ** condition where this thread also attempts to join the same object, join
102450 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
102451 for(i=pSorter->nTask-1; i>=0; i--){
102452 SortSubtask *pTask = &pSorter->aTask[i];
102453 int rc2 = vdbeSorterJoinThread(pTask);
102454 if( rc==SQLITE_OK ) rc = rc2;
102455 }
102456 return rc;
102457}
102458#else
102459# define vdbeSorterJoinAll(x,rcin) (rcin)
102460# define vdbeSorterJoinThread(pTask) SQLITE_OK
102461#endif
102462
102463/*
102464** Allocate a new MergeEngine object capable of handling up to
102465** nReader PmaReader inputs.
102466**
102467** nReader is automatically rounded up to the next power of two.
102468** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
102469*/
102470static MergeEngine *vdbeMergeEngineNew(int nReader){
102471 int N = 2; /* Smallest power of two >= nReader */
102472 int nByte; /* Total bytes of space to allocate */
102473 MergeEngine *pNew; /* Pointer to allocated object to return */
102474
102475 assert( nReader<=SORTER_MAX_MERGE_COUNT );
102476
102477 while( N<nReader ) N += N;
102478 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
102479
102480 pNew = sqlite3FaultSim(iTest: 100) ? 0 : (MergeEngine*)sqlite3MallocZero(n: nByte);
102481 if( pNew ){
102482 pNew->nTree = N;
102483 pNew->pTask = 0;
102484 pNew->aReadr = (PmaReader*)&pNew[1];
102485 pNew->aTree = (int*)&pNew->aReadr[N];
102486 }
102487 return pNew;
102488}
102489
102490/*
102491** Free the MergeEngine object passed as the only argument.
102492*/
102493static void vdbeMergeEngineFree(MergeEngine *pMerger){
102494 int i;
102495 if( pMerger ){
102496 for(i=0; i<pMerger->nTree; i++){
102497 vdbePmaReaderClear(pReadr: &pMerger->aReadr[i]);
102498 }
102499 }
102500 sqlite3_free(p: pMerger);
102501}
102502
102503/*
102504** Free all resources associated with the IncrMerger object indicated by
102505** the first argument.
102506*/
102507static void vdbeIncrFree(IncrMerger *pIncr){
102508 if( pIncr ){
102509#if SQLITE_MAX_WORKER_THREADS>0
102510 if( pIncr->bUseThread ){
102511 vdbeSorterJoinThread(pTask: pIncr->pTask);
102512 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pFile: pIncr->aFile[0].pFd);
102513 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pFile: pIncr->aFile[1].pFd);
102514 }
102515#endif
102516 vdbeMergeEngineFree(pMerger: pIncr->pMerger);
102517 sqlite3_free(p: pIncr);
102518 }
102519}
102520
102521/*
102522** Reset a sorting cursor back to its original empty state.
102523*/
102524SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
102525 int i;
102526 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
102527 assert( pSorter->bUseThreads || pSorter->pReader==0 );
102528#if SQLITE_MAX_WORKER_THREADS>0
102529 if( pSorter->pReader ){
102530 vdbePmaReaderClear(pReadr: pSorter->pReader);
102531 sqlite3DbFree(db, p: pSorter->pReader);
102532 pSorter->pReader = 0;
102533 }
102534#endif
102535 vdbeMergeEngineFree(pMerger: pSorter->pMerger);
102536 pSorter->pMerger = 0;
102537 for(i=0; i<pSorter->nTask; i++){
102538 SortSubtask *pTask = &pSorter->aTask[i];
102539 vdbeSortSubtaskCleanup(db, pTask);
102540 pTask->pSorter = pSorter;
102541 }
102542 if( pSorter->list.aMemory==0 ){
102543 vdbeSorterRecordFree(db: 0, pRecord: pSorter->list.pList);
102544 }
102545 pSorter->list.pList = 0;
102546 pSorter->list.szPMA = 0;
102547 pSorter->bUsePMA = 0;
102548 pSorter->iMemory = 0;
102549 pSorter->mxKeysize = 0;
102550 sqlite3DbFree(db, p: pSorter->pUnpacked);
102551 pSorter->pUnpacked = 0;
102552}
102553
102554/*
102555** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
102556*/
102557SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
102558 VdbeSorter *pSorter;
102559 assert( pCsr->eCurType==CURTYPE_SORTER );
102560 pSorter = pCsr->uc.pSorter;
102561 if( pSorter ){
102562 sqlite3VdbeSorterReset(db, pSorter);
102563 sqlite3_free(p: pSorter->list.aMemory);
102564 sqlite3DbFree(db, p: pSorter);
102565 pCsr->uc.pSorter = 0;
102566 }
102567}
102568
102569#if SQLITE_MAX_MMAP_SIZE>0
102570/*
102571** The first argument is a file-handle open on a temporary file. The file
102572** is guaranteed to be nByte bytes or smaller in size. This function
102573** attempts to extend the file to nByte bytes in size and to ensure that
102574** the VFS has memory mapped it.
102575**
102576** Whether or not the file does end up memory mapped of course depends on
102577** the specific VFS implementation.
102578*/
102579static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
102580 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
102581 void *p = 0;
102582 int chunksize = 4*1024;
102583 sqlite3OsFileControlHint(id: pFd, SQLITE_FCNTL_CHUNK_SIZE, pArg: &chunksize);
102584 sqlite3OsFileControlHint(id: pFd, SQLITE_FCNTL_SIZE_HINT, pArg: &nByte);
102585 sqlite3OsFetch(id: pFd, iOff: 0, iAmt: (int)nByte, pp: &p);
102586 if( p ) sqlite3OsUnfetch(id: pFd, iOff: 0, p);
102587 }
102588}
102589#else
102590# define vdbeSorterExtendFile(x,y,z)
102591#endif
102592
102593/*
102594** Allocate space for a file-handle and open a temporary file. If successful,
102595** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
102596** Otherwise, set *ppFd to 0 and return an SQLite error code.
102597*/
102598static int vdbeSorterOpenTempFile(
102599 sqlite3 *db, /* Database handle doing sort */
102600 i64 nExtend, /* Attempt to extend file to this size */
102601 sqlite3_file **ppFd
102602){
102603 int rc;
102604 if( sqlite3FaultSim(iTest: 202) ) return SQLITE_IOERR_ACCESS;
102605 rc = sqlite3OsOpenMalloc(pVfs: db->pVfs, zFile: 0, ppFile: ppFd,
102606 SQLITE_OPEN_TEMP_JOURNAL |
102607 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
102608 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, pOutFlags: &rc
102609 );
102610 if( rc==SQLITE_OK ){
102611 i64 max = SQLITE_MAX_MMAP_SIZE;
102612 sqlite3OsFileControlHint(id: *ppFd, SQLITE_FCNTL_MMAP_SIZE, pArg: (void*)&max);
102613 if( nExtend>0 ){
102614 vdbeSorterExtendFile(db, pFd: *ppFd, nByte: nExtend);
102615 }
102616 }
102617 return rc;
102618}
102619
102620/*
102621** If it has not already been allocated, allocate the UnpackedRecord
102622** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
102623** if no allocation was required), or SQLITE_NOMEM otherwise.
102624*/
102625static int vdbeSortAllocUnpacked(SortSubtask *pTask){
102626 if( pTask->pUnpacked==0 ){
102627 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo: pTask->pSorter->pKeyInfo);
102628 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
102629 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
102630 pTask->pUnpacked->errCode = 0;
102631 }
102632 return SQLITE_OK;
102633}
102634
102635
102636/*
102637** Merge the two sorted lists p1 and p2 into a single list.
102638*/
102639static SorterRecord *vdbeSorterMerge(
102640 SortSubtask *pTask, /* Calling thread context */
102641 SorterRecord *p1, /* First list to merge */
102642 SorterRecord *p2 /* Second list to merge */
102643){
102644 SorterRecord *pFinal = 0;
102645 SorterRecord **pp = &pFinal;
102646 int bCached = 0;
102647
102648 assert( p1!=0 && p2!=0 );
102649 for(;;){
102650 int res;
102651 res = pTask->xCompare(
102652 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
102653 );
102654
102655 if( res<=0 ){
102656 *pp = p1;
102657 pp = &p1->u.pNext;
102658 p1 = p1->u.pNext;
102659 if( p1==0 ){
102660 *pp = p2;
102661 break;
102662 }
102663 }else{
102664 *pp = p2;
102665 pp = &p2->u.pNext;
102666 p2 = p2->u.pNext;
102667 bCached = 0;
102668 if( p2==0 ){
102669 *pp = p1;
102670 break;
102671 }
102672 }
102673 }
102674 return pFinal;
102675}
102676
102677/*
102678** Return the SorterCompare function to compare values collected by the
102679** sorter object passed as the only argument.
102680*/
102681static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
102682 if( p->typeMask==SORTER_TYPE_INTEGER ){
102683 return vdbeSorterCompareInt;
102684 }else if( p->typeMask==SORTER_TYPE_TEXT ){
102685 return vdbeSorterCompareText;
102686 }
102687 return vdbeSorterCompare;
102688}
102689
102690/*
102691** Sort the linked list of records headed at pTask->pList. Return
102692** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
102693** an error occurs.
102694*/
102695static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
102696 int i;
102697 SorterRecord *p;
102698 int rc;
102699 SorterRecord *aSlot[64];
102700
102701 rc = vdbeSortAllocUnpacked(pTask);
102702 if( rc!=SQLITE_OK ) return rc;
102703
102704 p = pList->pList;
102705 pTask->xCompare = vdbeSorterGetCompare(p: pTask->pSorter);
102706 memset(s: aSlot, c: 0, n: sizeof(aSlot));
102707
102708 while( p ){
102709 SorterRecord *pNext;
102710 if( pList->aMemory ){
102711 if( (u8*)p==pList->aMemory ){
102712 pNext = 0;
102713 }else{
102714 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
102715 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
102716 }
102717 }else{
102718 pNext = p->u.pNext;
102719 }
102720
102721 p->u.pNext = 0;
102722 for(i=0; aSlot[i]; i++){
102723 p = vdbeSorterMerge(pTask, p1: p, p2: aSlot[i]);
102724 aSlot[i] = 0;
102725 }
102726 aSlot[i] = p;
102727 p = pNext;
102728 }
102729
102730 p = 0;
102731 for(i=0; i<ArraySize(aSlot); i++){
102732 if( aSlot[i]==0 ) continue;
102733 p = p ? vdbeSorterMerge(pTask, p1: p, p2: aSlot[i]) : aSlot[i];
102734 }
102735 pList->pList = p;
102736
102737 assert( pTask->pUnpacked->errCode==SQLITE_OK
102738 || pTask->pUnpacked->errCode==SQLITE_NOMEM
102739 );
102740 return pTask->pUnpacked->errCode;
102741}
102742
102743/*
102744** Initialize a PMA-writer object.
102745*/
102746static void vdbePmaWriterInit(
102747 sqlite3_file *pFd, /* File handle to write to */
102748 PmaWriter *p, /* Object to populate */
102749 int nBuf, /* Buffer size */
102750 i64 iStart /* Offset of pFd to begin writing at */
102751){
102752 memset(s: p, c: 0, n: sizeof(PmaWriter));
102753 p->aBuffer = (u8*)sqlite3Malloc(n: nBuf);
102754 if( !p->aBuffer ){
102755 p->eFWErr = SQLITE_NOMEM_BKPT;
102756 }else{
102757 p->iBufEnd = p->iBufStart = (iStart % nBuf);
102758 p->iWriteOff = iStart - p->iBufStart;
102759 p->nBuffer = nBuf;
102760 p->pFd = pFd;
102761 }
102762}
102763
102764/*
102765** Write nData bytes of data to the PMA. Return SQLITE_OK
102766** if successful, or an SQLite error code if an error occurs.
102767*/
102768static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
102769 int nRem = nData;
102770 while( nRem>0 && p->eFWErr==0 ){
102771 int nCopy = nRem;
102772 if( nCopy>(p->nBuffer - p->iBufEnd) ){
102773 nCopy = p->nBuffer - p->iBufEnd;
102774 }
102775
102776 memcpy(dest: &p->aBuffer[p->iBufEnd], src: &pData[nData-nRem], n: nCopy);
102777 p->iBufEnd += nCopy;
102778 if( p->iBufEnd==p->nBuffer ){
102779 p->eFWErr = sqlite3OsWrite(id: p->pFd,
102780 pBuf: &p->aBuffer[p->iBufStart], amt: p->iBufEnd - p->iBufStart,
102781 offset: p->iWriteOff + p->iBufStart
102782 );
102783 p->iBufStart = p->iBufEnd = 0;
102784 p->iWriteOff += p->nBuffer;
102785 }
102786 assert( p->iBufEnd<p->nBuffer );
102787
102788 nRem -= nCopy;
102789 }
102790}
102791
102792/*
102793** Flush any buffered data to disk and clean up the PMA-writer object.
102794** The results of using the PMA-writer after this call are undefined.
102795** Return SQLITE_OK if flushing the buffered data succeeds or is not
102796** required. Otherwise, return an SQLite error code.
102797**
102798** Before returning, set *piEof to the offset immediately following the
102799** last byte written to the file.
102800*/
102801static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
102802 int rc;
102803 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
102804 p->eFWErr = sqlite3OsWrite(id: p->pFd,
102805 pBuf: &p->aBuffer[p->iBufStart], amt: p->iBufEnd - p->iBufStart,
102806 offset: p->iWriteOff + p->iBufStart
102807 );
102808 }
102809 *piEof = (p->iWriteOff + p->iBufEnd);
102810 sqlite3_free(p: p->aBuffer);
102811 rc = p->eFWErr;
102812 memset(s: p, c: 0, n: sizeof(PmaWriter));
102813 return rc;
102814}
102815
102816/*
102817** Write value iVal encoded as a varint to the PMA. Return
102818** SQLITE_OK if successful, or an SQLite error code if an error occurs.
102819*/
102820static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
102821 int nByte;
102822 u8 aByte[10];
102823 nByte = sqlite3PutVarint(p: aByte, v: iVal);
102824 vdbePmaWriteBlob(p, pData: aByte, nData: nByte);
102825}
102826
102827/*
102828** Write the current contents of in-memory linked-list pList to a level-0
102829** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
102830** successful, or an SQLite error code otherwise.
102831**
102832** The format of a PMA is:
102833**
102834** * A varint. This varint contains the total number of bytes of content
102835** in the PMA (not including the varint itself).
102836**
102837** * One or more records packed end-to-end in order of ascending keys.
102838** Each record consists of a varint followed by a blob of data (the
102839** key). The varint is the number of bytes in the blob of data.
102840*/
102841static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
102842 sqlite3 *db = pTask->pSorter->db;
102843 int rc = SQLITE_OK; /* Return code */
102844 PmaWriter writer; /* Object used to write to the file */
102845
102846#ifdef SQLITE_DEBUG
102847 /* Set iSz to the expected size of file pTask->file after writing the PMA.
102848 ** This is used by an assert() statement at the end of this function. */
102849 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
102850#endif
102851
102852 vdbeSorterWorkDebug(pTask, "enter");
102853 memset(s: &writer, c: 0, n: sizeof(PmaWriter));
102854 assert( pList->szPMA>0 );
102855
102856 /* If the first temporary PMA file has not been opened, open it now. */
102857 if( pTask->file.pFd==0 ){
102858 rc = vdbeSorterOpenTempFile(db, nExtend: 0, ppFd: &pTask->file.pFd);
102859 assert( rc!=SQLITE_OK || pTask->file.pFd );
102860 assert( pTask->file.iEof==0 );
102861 assert( pTask->nPMA==0 );
102862 }
102863
102864 /* Try to get the file to memory map */
102865 if( rc==SQLITE_OK ){
102866 vdbeSorterExtendFile(db, pFd: pTask->file.pFd, nByte: pTask->file.iEof+pList->szPMA+9);
102867 }
102868
102869 /* Sort the list */
102870 if( rc==SQLITE_OK ){
102871 rc = vdbeSorterSort(pTask, pList);
102872 }
102873
102874 if( rc==SQLITE_OK ){
102875 SorterRecord *p;
102876 SorterRecord *pNext = 0;
102877
102878 vdbePmaWriterInit(pFd: pTask->file.pFd, p: &writer, nBuf: pTask->pSorter->pgsz,
102879 iStart: pTask->file.iEof);
102880 pTask->nPMA++;
102881 vdbePmaWriteVarint(p: &writer, iVal: pList->szPMA);
102882 for(p=pList->pList; p; p=pNext){
102883 pNext = p->u.pNext;
102884 vdbePmaWriteVarint(p: &writer, iVal: p->nVal);
102885 vdbePmaWriteBlob(p: &writer, SRVAL(p), nData: p->nVal);
102886 if( pList->aMemory==0 ) sqlite3_free(p);
102887 }
102888 pList->pList = p;
102889 rc = vdbePmaWriterFinish(p: &writer, piEof: &pTask->file.iEof);
102890 }
102891
102892 vdbeSorterWorkDebug(pTask, "exit");
102893 assert( rc!=SQLITE_OK || pList->pList==0 );
102894 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
102895 return rc;
102896}
102897
102898/*
102899** Advance the MergeEngine to its next entry.
102900** Set *pbEof to true there is no next entry because
102901** the MergeEngine has reached the end of all its inputs.
102902**
102903** Return SQLITE_OK if successful or an error code if an error occurs.
102904*/
102905static int vdbeMergeEngineStep(
102906 MergeEngine *pMerger, /* The merge engine to advance to the next row */
102907 int *pbEof /* Set TRUE at EOF. Set false for more content */
102908){
102909 int rc;
102910 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
102911 SortSubtask *pTask = pMerger->pTask;
102912
102913 /* Advance the current PmaReader */
102914 rc = vdbePmaReaderNext(pReadr: &pMerger->aReadr[iPrev]);
102915
102916 /* Update contents of aTree[] */
102917 if( rc==SQLITE_OK ){
102918 int i; /* Index of aTree[] to recalculate */
102919 PmaReader *pReadr1; /* First PmaReader to compare */
102920 PmaReader *pReadr2; /* Second PmaReader to compare */
102921 int bCached = 0;
102922
102923 /* Find the first two PmaReaders to compare. The one that was just
102924 ** advanced (iPrev) and the one next to it in the array. */
102925 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
102926 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
102927
102928 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
102929 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
102930 int iRes;
102931 if( pReadr1->pFd==0 ){
102932 iRes = +1;
102933 }else if( pReadr2->pFd==0 ){
102934 iRes = -1;
102935 }else{
102936 iRes = pTask->xCompare(pTask, &bCached,
102937 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
102938 );
102939 }
102940
102941 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
102942 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
102943 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
102944 ** pKey2 to point to the record belonging to pReadr2.
102945 **
102946 ** Alternatively, if pReadr2 contains the smaller of the two values,
102947 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
102948 ** was actually called above, then pTask->pUnpacked now contains
102949 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
102950 ** vdbeSorterCompare() from decoding pReadr2 again.
102951 **
102952 ** If the two values were equal, then the value from the oldest
102953 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
102954 ** is sorted from oldest to newest, so pReadr1 contains older values
102955 ** than pReadr2 iff (pReadr1<pReadr2). */
102956 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
102957 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
102958 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
102959 bCached = 0;
102960 }else{
102961 if( pReadr1->pFd ) bCached = 0;
102962 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
102963 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
102964 }
102965 }
102966 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
102967 }
102968
102969 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
102970}
102971
102972#if SQLITE_MAX_WORKER_THREADS>0
102973/*
102974** The main routine for background threads that write level-0 PMAs.
102975*/
102976static void *vdbeSorterFlushThread(void *pCtx){
102977 SortSubtask *pTask = (SortSubtask*)pCtx;
102978 int rc; /* Return code */
102979 assert( pTask->bDone==0 );
102980 rc = vdbeSorterListToPMA(pTask, pList: &pTask->list);
102981 pTask->bDone = 1;
102982 return SQLITE_INT_TO_PTR(rc);
102983}
102984#endif /* SQLITE_MAX_WORKER_THREADS>0 */
102985
102986/*
102987** Flush the current contents of VdbeSorter.list to a new PMA, possibly
102988** using a background thread.
102989*/
102990static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
102991#if SQLITE_MAX_WORKER_THREADS==0
102992 pSorter->bUsePMA = 1;
102993 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
102994#else
102995 int rc = SQLITE_OK;
102996 int i;
102997 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
102998 int nWorker = (pSorter->nTask-1);
102999
103000 /* Set the flag to indicate that at least one PMA has been written.
103001 ** Or will be, anyhow. */
103002 pSorter->bUsePMA = 1;
103003
103004 /* Select a sub-task to sort and flush the current list of in-memory
103005 ** records to disk. If the sorter is running in multi-threaded mode,
103006 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
103007 ** the background thread from a sub-tasks previous turn is still running,
103008 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
103009 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
103010 ** sub-tasks are preferred as they use background threads - the final
103011 ** sub-task uses the main thread. */
103012 for(i=0; i<nWorker; i++){
103013 int iTest = (pSorter->iPrev + i + 1) % nWorker;
103014 pTask = &pSorter->aTask[iTest];
103015 if( pTask->bDone ){
103016 rc = vdbeSorterJoinThread(pTask);
103017 }
103018 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
103019 }
103020
103021 if( rc==SQLITE_OK ){
103022 if( i==nWorker ){
103023 /* Use the foreground thread for this operation */
103024 rc = vdbeSorterListToPMA(pTask: &pSorter->aTask[nWorker], pList: &pSorter->list);
103025 }else{
103026 /* Launch a background thread for this operation */
103027 u8 *aMem;
103028 void *pCtx;
103029
103030 assert( pTask!=0 );
103031 assert( pTask->pThread==0 && pTask->bDone==0 );
103032 assert( pTask->list.pList==0 );
103033 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
103034
103035 aMem = pTask->list.aMemory;
103036 pCtx = (void*)pTask;
103037 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
103038 pTask->list = pSorter->list;
103039 pSorter->list.pList = 0;
103040 pSorter->list.szPMA = 0;
103041 if( aMem ){
103042 pSorter->list.aMemory = aMem;
103043 pSorter->nMemory = sqlite3MallocSize(p: aMem);
103044 }else if( pSorter->list.aMemory ){
103045 pSorter->list.aMemory = sqlite3Malloc(n: pSorter->nMemory);
103046 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
103047 }
103048
103049 rc = vdbeSorterCreateThread(pTask, xTask: vdbeSorterFlushThread, pIn: pCtx);
103050 }
103051 }
103052
103053 return rc;
103054#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
103055}
103056
103057/*
103058** Add a record to the sorter.
103059*/
103060SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
103061 const VdbeCursor *pCsr, /* Sorter cursor */
103062 Mem *pVal /* Memory cell containing record */
103063){
103064 VdbeSorter *pSorter;
103065 int rc = SQLITE_OK; /* Return Code */
103066 SorterRecord *pNew; /* New list element */
103067 int bFlush; /* True to flush contents of memory to PMA */
103068 int nReq; /* Bytes of memory required */
103069 int nPMA; /* Bytes of PMA space required */
103070 int t; /* serial type of first record field */
103071
103072 assert( pCsr->eCurType==CURTYPE_SORTER );
103073 pSorter = pCsr->uc.pSorter;
103074 getVarint32NR((const u8*)&pVal->z[1], t);
103075 if( t>0 && t<10 && t!=7 ){
103076 pSorter->typeMask &= SORTER_TYPE_INTEGER;
103077 }else if( t>10 && (t & 0x01) ){
103078 pSorter->typeMask &= SORTER_TYPE_TEXT;
103079 }else{
103080 pSorter->typeMask = 0;
103081 }
103082
103083 assert( pSorter );
103084
103085 /* Figure out whether or not the current contents of memory should be
103086 ** flushed to a PMA before continuing. If so, do so.
103087 **
103088 ** If using the single large allocation mode (pSorter->aMemory!=0), then
103089 ** flush the contents of memory to a new PMA if (a) at least one value is
103090 ** already in memory and (b) the new value will not fit in memory.
103091 **
103092 ** Or, if using separate allocations for each record, flush the contents
103093 ** of memory to a PMA if either of the following are true:
103094 **
103095 ** * The total memory allocated for the in-memory list is greater
103096 ** than (page-size * cache-size), or
103097 **
103098 ** * The total memory allocated for the in-memory list is greater
103099 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
103100 */
103101 nReq = pVal->n + sizeof(SorterRecord);
103102 nPMA = pVal->n + sqlite3VarintLen(v: pVal->n);
103103 if( pSorter->mxPmaSize ){
103104 if( pSorter->list.aMemory ){
103105 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
103106 }else{
103107 bFlush = (
103108 (pSorter->list.szPMA > pSorter->mxPmaSize)
103109 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
103110 );
103111 }
103112 if( bFlush ){
103113 rc = vdbeSorterFlushPMA(pSorter);
103114 pSorter->list.szPMA = 0;
103115 pSorter->iMemory = 0;
103116 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
103117 }
103118 }
103119
103120 pSorter->list.szPMA += nPMA;
103121 if( nPMA>pSorter->mxKeysize ){
103122 pSorter->mxKeysize = nPMA;
103123 }
103124
103125 if( pSorter->list.aMemory ){
103126 int nMin = pSorter->iMemory + nReq;
103127
103128 if( nMin>pSorter->nMemory ){
103129 u8 *aNew;
103130 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
103131 int iListOff = -1;
103132 if( pSorter->list.pList ){
103133 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
103134 }
103135 while( nNew < nMin ) nNew = nNew*2;
103136 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
103137 if( nNew < nMin ) nNew = nMin;
103138 aNew = sqlite3Realloc(pOld: pSorter->list.aMemory, nBytes: nNew);
103139 if( !aNew ) return SQLITE_NOMEM_BKPT;
103140 if( iListOff>=0 ){
103141 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
103142 }
103143 pSorter->list.aMemory = aNew;
103144 pSorter->nMemory = nNew;
103145 }
103146
103147 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
103148 pSorter->iMemory += ROUND8(nReq);
103149 if( pSorter->list.pList ){
103150 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
103151 }
103152 }else{
103153 pNew = (SorterRecord *)sqlite3Malloc(n: nReq);
103154 if( pNew==0 ){
103155 return SQLITE_NOMEM_BKPT;
103156 }
103157 pNew->u.pNext = pSorter->list.pList;
103158 }
103159
103160 memcpy(SRVAL(pNew), src: pVal->z, n: pVal->n);
103161 pNew->nVal = pVal->n;
103162 pSorter->list.pList = pNew;
103163
103164 return rc;
103165}
103166
103167/*
103168** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
103169** of the data stored in aFile[1] is the same as that used by regular PMAs,
103170** except that the number-of-bytes varint is omitted from the start.
103171*/
103172static int vdbeIncrPopulate(IncrMerger *pIncr){
103173 int rc = SQLITE_OK;
103174 int rc2;
103175 i64 iStart = pIncr->iStartOff;
103176 SorterFile *pOut = &pIncr->aFile[1];
103177 SortSubtask *pTask = pIncr->pTask;
103178 MergeEngine *pMerger = pIncr->pMerger;
103179 PmaWriter writer;
103180 assert( pIncr->bEof==0 );
103181
103182 vdbeSorterPopulateDebug(pTask, "enter");
103183
103184 vdbePmaWriterInit(pFd: pOut->pFd, p: &writer, nBuf: pTask->pSorter->pgsz, iStart);
103185 while( rc==SQLITE_OK ){
103186 int dummy;
103187 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
103188 int nKey = pReader->nKey;
103189 i64 iEof = writer.iWriteOff + writer.iBufEnd;
103190
103191 /* Check if the output file is full or if the input has been exhausted.
103192 ** In either case exit the loop. */
103193 if( pReader->pFd==0 ) break;
103194 if( (iEof + nKey + sqlite3VarintLen(v: nKey))>(iStart + pIncr->mxSz) ) break;
103195
103196 /* Write the next key to the output. */
103197 vdbePmaWriteVarint(p: &writer, iVal: nKey);
103198 vdbePmaWriteBlob(p: &writer, pData: pReader->aKey, nData: nKey);
103199 assert( pIncr->pMerger->pTask==pTask );
103200 rc = vdbeMergeEngineStep(pMerger: pIncr->pMerger, pbEof: &dummy);
103201 }
103202
103203 rc2 = vdbePmaWriterFinish(p: &writer, piEof: &pOut->iEof);
103204 if( rc==SQLITE_OK ) rc = rc2;
103205 vdbeSorterPopulateDebug(pTask, "exit");
103206 return rc;
103207}
103208
103209#if SQLITE_MAX_WORKER_THREADS>0
103210/*
103211** The main routine for background threads that populate aFile[1] of
103212** multi-threaded IncrMerger objects.
103213*/
103214static void *vdbeIncrPopulateThread(void *pCtx){
103215 IncrMerger *pIncr = (IncrMerger*)pCtx;
103216 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
103217 pIncr->pTask->bDone = 1;
103218 return pRet;
103219}
103220
103221/*
103222** Launch a background thread to populate aFile[1] of pIncr.
103223*/
103224static int vdbeIncrBgPopulate(IncrMerger *pIncr){
103225 void *p = (void*)pIncr;
103226 assert( pIncr->bUseThread );
103227 return vdbeSorterCreateThread(pTask: pIncr->pTask, xTask: vdbeIncrPopulateThread, pIn: p);
103228}
103229#endif
103230
103231/*
103232** This function is called when the PmaReader corresponding to pIncr has
103233** finished reading the contents of aFile[0]. Its purpose is to "refill"
103234** aFile[0] such that the PmaReader should start rereading it from the
103235** beginning.
103236**
103237** For single-threaded objects, this is accomplished by literally reading
103238** keys from pIncr->pMerger and repopulating aFile[0].
103239**
103240** For multi-threaded objects, all that is required is to wait until the
103241** background thread is finished (if it is not already) and then swap
103242** aFile[0] and aFile[1] in place. If the contents of pMerger have not
103243** been exhausted, this function also launches a new background thread
103244** to populate the new aFile[1].
103245**
103246** SQLITE_OK is returned on success, or an SQLite error code otherwise.
103247*/
103248static int vdbeIncrSwap(IncrMerger *pIncr){
103249 int rc = SQLITE_OK;
103250
103251#if SQLITE_MAX_WORKER_THREADS>0
103252 if( pIncr->bUseThread ){
103253 rc = vdbeSorterJoinThread(pTask: pIncr->pTask);
103254
103255 if( rc==SQLITE_OK ){
103256 SorterFile f0 = pIncr->aFile[0];
103257 pIncr->aFile[0] = pIncr->aFile[1];
103258 pIncr->aFile[1] = f0;
103259 }
103260
103261 if( rc==SQLITE_OK ){
103262 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
103263 pIncr->bEof = 1;
103264 }else{
103265 rc = vdbeIncrBgPopulate(pIncr);
103266 }
103267 }
103268 }else
103269#endif
103270 {
103271 rc = vdbeIncrPopulate(pIncr);
103272 pIncr->aFile[0] = pIncr->aFile[1];
103273 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
103274 pIncr->bEof = 1;
103275 }
103276 }
103277
103278 return rc;
103279}
103280
103281/*
103282** Allocate and return a new IncrMerger object to read data from pMerger.
103283**
103284** If an OOM condition is encountered, return NULL. In this case free the
103285** pMerger argument before returning.
103286*/
103287static int vdbeIncrMergerNew(
103288 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
103289 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
103290 IncrMerger **ppOut /* Write the new IncrMerger here */
103291){
103292 int rc = SQLITE_OK;
103293 IncrMerger *pIncr = *ppOut = (IncrMerger*)
103294 (sqlite3FaultSim(iTest: 100) ? 0 : sqlite3MallocZero(n: sizeof(*pIncr)));
103295 if( pIncr ){
103296 pIncr->pMerger = pMerger;
103297 pIncr->pTask = pTask;
103298 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
103299 pTask->file2.iEof += pIncr->mxSz;
103300 }else{
103301 vdbeMergeEngineFree(pMerger);
103302 rc = SQLITE_NOMEM_BKPT;
103303 }
103304 assert( *ppOut!=0 || rc!=SQLITE_OK );
103305 return rc;
103306}
103307
103308#if SQLITE_MAX_WORKER_THREADS>0
103309/*
103310** Set the "use-threads" flag on object pIncr.
103311*/
103312static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
103313 pIncr->bUseThread = 1;
103314 pIncr->pTask->file2.iEof -= pIncr->mxSz;
103315}
103316#endif /* SQLITE_MAX_WORKER_THREADS>0 */
103317
103318
103319
103320/*
103321** Recompute pMerger->aTree[iOut] by comparing the next keys on the
103322** two PmaReaders that feed that entry. Neither of the PmaReaders
103323** are advanced. This routine merely does the comparison.
103324*/
103325static void vdbeMergeEngineCompare(
103326 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
103327 int iOut /* Store the result in pMerger->aTree[iOut] */
103328){
103329 int i1;
103330 int i2;
103331 int iRes;
103332 PmaReader *p1;
103333 PmaReader *p2;
103334
103335 assert( iOut<pMerger->nTree && iOut>0 );
103336
103337 if( iOut>=(pMerger->nTree/2) ){
103338 i1 = (iOut - pMerger->nTree/2) * 2;
103339 i2 = i1 + 1;
103340 }else{
103341 i1 = pMerger->aTree[iOut*2];
103342 i2 = pMerger->aTree[iOut*2+1];
103343 }
103344
103345 p1 = &pMerger->aReadr[i1];
103346 p2 = &pMerger->aReadr[i2];
103347
103348 if( p1->pFd==0 ){
103349 iRes = i2;
103350 }else if( p2->pFd==0 ){
103351 iRes = i1;
103352 }else{
103353 SortSubtask *pTask = pMerger->pTask;
103354 int bCached = 0;
103355 int res;
103356 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
103357 res = pTask->xCompare(
103358 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
103359 );
103360 if( res<=0 ){
103361 iRes = i1;
103362 }else{
103363 iRes = i2;
103364 }
103365 }
103366
103367 pMerger->aTree[iOut] = iRes;
103368}
103369
103370/*
103371** Allowed values for the eMode parameter to vdbeMergeEngineInit()
103372** and vdbePmaReaderIncrMergeInit().
103373**
103374** Only INCRINIT_NORMAL is valid in single-threaded builds (when
103375** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
103376** when there exists one or more separate worker threads.
103377*/
103378#define INCRINIT_NORMAL 0
103379#define INCRINIT_TASK 1
103380#define INCRINIT_ROOT 2
103381
103382/*
103383** Forward reference required as the vdbeIncrMergeInit() and
103384** vdbePmaReaderIncrInit() routines are called mutually recursively when
103385** building a merge tree.
103386*/
103387static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
103388
103389/*
103390** Initialize the MergeEngine object passed as the second argument. Once this
103391** function returns, the first key of merged data may be read from the
103392** MergeEngine object in the usual fashion.
103393**
103394** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
103395** objects attached to the PmaReader objects that the merger reads from have
103396** already been populated, but that they have not yet populated aFile[0] and
103397** set the PmaReader objects up to read from it. In this case all that is
103398** required is to call vdbePmaReaderNext() on each PmaReader to point it at
103399** its first key.
103400**
103401** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
103402** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
103403** to pMerger.
103404**
103405** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
103406*/
103407static int vdbeMergeEngineInit(
103408 SortSubtask *pTask, /* Thread that will run pMerger */
103409 MergeEngine *pMerger, /* MergeEngine to initialize */
103410 int eMode /* One of the INCRINIT_XXX constants */
103411){
103412 int rc = SQLITE_OK; /* Return code */
103413 int i; /* For looping over PmaReader objects */
103414 int nTree; /* Number of subtrees to merge */
103415
103416 /* Failure to allocate the merge would have been detected prior to
103417 ** invoking this routine */
103418 assert( pMerger!=0 );
103419
103420 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
103421 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
103422
103423 /* Verify that the MergeEngine is assigned to a single thread */
103424 assert( pMerger->pTask==0 );
103425 pMerger->pTask = pTask;
103426
103427 nTree = pMerger->nTree;
103428 for(i=0; i<nTree; i++){
103429 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
103430 /* PmaReaders should be normally initialized in order, as if they are
103431 ** reading from the same temp file this makes for more linear file IO.
103432 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
103433 ** in use it will block the vdbePmaReaderNext() call while it uses
103434 ** the main thread to fill its buffer. So calling PmaReaderNext()
103435 ** on this PmaReader before any of the multi-threaded PmaReaders takes
103436 ** better advantage of multi-processor hardware. */
103437 rc = vdbePmaReaderNext(pReadr: &pMerger->aReadr[nTree-i-1]);
103438 }else{
103439 rc = vdbePmaReaderIncrInit(pReadr: &pMerger->aReadr[i], INCRINIT_NORMAL);
103440 }
103441 if( rc!=SQLITE_OK ) return rc;
103442 }
103443
103444 for(i=pMerger->nTree-1; i>0; i--){
103445 vdbeMergeEngineCompare(pMerger, iOut: i);
103446 }
103447 return pTask->pUnpacked->errCode;
103448}
103449
103450/*
103451** The PmaReader passed as the first argument is guaranteed to be an
103452** incremental-reader (pReadr->pIncr!=0). This function serves to open
103453** and/or initialize the temp file related fields of the IncrMerge
103454** object at (pReadr->pIncr).
103455**
103456** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
103457** in the sub-tree headed by pReadr are also initialized. Data is then
103458** loaded into the buffers belonging to pReadr and it is set to point to
103459** the first key in its range.
103460**
103461** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
103462** to be a multi-threaded PmaReader and this function is being called in a
103463** background thread. In this case all PmaReaders in the sub-tree are
103464** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
103465** pReadr is populated. However, pReadr itself is not set up to point
103466** to its first key. A call to vdbePmaReaderNext() is still required to do
103467** that.
103468**
103469** The reason this function does not call vdbePmaReaderNext() immediately
103470** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
103471** to block on thread (pTask->thread) before accessing aFile[1]. But, since
103472** this entire function is being run by thread (pTask->thread), that will
103473** lead to the current background thread attempting to join itself.
103474**
103475** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
103476** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
103477** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
103478** In this case vdbePmaReaderNext() is called on all child PmaReaders and
103479** the current PmaReader set to point to the first key in its range.
103480**
103481** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
103482*/
103483static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
103484 int rc = SQLITE_OK;
103485 IncrMerger *pIncr = pReadr->pIncr;
103486 SortSubtask *pTask = pIncr->pTask;
103487 sqlite3 *db = pTask->pSorter->db;
103488
103489 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
103490 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
103491
103492 rc = vdbeMergeEngineInit(pTask, pMerger: pIncr->pMerger, eMode);
103493
103494 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
103495 ** requires two temp files to itself, whereas a single-threaded object
103496 ** only requires a region of pTask->file2. */
103497 if( rc==SQLITE_OK ){
103498 int mxSz = pIncr->mxSz;
103499#if SQLITE_MAX_WORKER_THREADS>0
103500 if( pIncr->bUseThread ){
103501 rc = vdbeSorterOpenTempFile(db, nExtend: mxSz, ppFd: &pIncr->aFile[0].pFd);
103502 if( rc==SQLITE_OK ){
103503 rc = vdbeSorterOpenTempFile(db, nExtend: mxSz, ppFd: &pIncr->aFile[1].pFd);
103504 }
103505 }else
103506#endif
103507 /*if( !pIncr->bUseThread )*/{
103508 if( pTask->file2.pFd==0 ){
103509 assert( pTask->file2.iEof>0 );
103510 rc = vdbeSorterOpenTempFile(db, nExtend: pTask->file2.iEof, ppFd: &pTask->file2.pFd);
103511 pTask->file2.iEof = 0;
103512 }
103513 if( rc==SQLITE_OK ){
103514 pIncr->aFile[1].pFd = pTask->file2.pFd;
103515 pIncr->iStartOff = pTask->file2.iEof;
103516 pTask->file2.iEof += mxSz;
103517 }
103518 }
103519 }
103520
103521#if SQLITE_MAX_WORKER_THREADS>0
103522 if( rc==SQLITE_OK && pIncr->bUseThread ){
103523 /* Use the current thread to populate aFile[1], even though this
103524 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
103525 ** then this function is already running in background thread
103526 ** pIncr->pTask->thread.
103527 **
103528 ** If this is the INCRINIT_ROOT object, then it is running in the
103529 ** main VDBE thread. But that is Ok, as that thread cannot return
103530 ** control to the VDBE or proceed with anything useful until the
103531 ** first results are ready from this merger object anyway.
103532 */
103533 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
103534 rc = vdbeIncrPopulate(pIncr);
103535 }
103536#endif
103537
103538 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
103539 rc = vdbePmaReaderNext(pReadr);
103540 }
103541
103542 return rc;
103543}
103544
103545#if SQLITE_MAX_WORKER_THREADS>0
103546/*
103547** The main routine for vdbePmaReaderIncrMergeInit() operations run in
103548** background threads.
103549*/
103550static void *vdbePmaReaderBgIncrInit(void *pCtx){
103551 PmaReader *pReader = (PmaReader*)pCtx;
103552 void *pRet = SQLITE_INT_TO_PTR(
103553 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
103554 );
103555 pReader->pIncr->pTask->bDone = 1;
103556 return pRet;
103557}
103558#endif
103559
103560/*
103561** If the PmaReader passed as the first argument is not an incremental-reader
103562** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
103563** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
103564** this routine to initialize the incremental merge.
103565**
103566** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
103567** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
103568** Or, if the IncrMerger is single threaded, the same function is called
103569** using the current thread.
103570*/
103571static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
103572 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
103573 int rc = SQLITE_OK; /* Return code */
103574 if( pIncr ){
103575#if SQLITE_MAX_WORKER_THREADS>0
103576 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
103577 if( pIncr->bUseThread ){
103578 void *pCtx = (void*)pReadr;
103579 rc = vdbeSorterCreateThread(pTask: pIncr->pTask, xTask: vdbePmaReaderBgIncrInit, pIn: pCtx);
103580 }else
103581#endif
103582 {
103583 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
103584 }
103585 }
103586 return rc;
103587}
103588
103589/*
103590** Allocate a new MergeEngine object to merge the contents of nPMA level-0
103591** PMAs from pTask->file. If no error occurs, set *ppOut to point to
103592** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
103593** to NULL and return an SQLite error code.
103594**
103595** When this function is called, *piOffset is set to the offset of the
103596** first PMA to read from pTask->file. Assuming no error occurs, it is
103597** set to the offset immediately following the last byte of the last
103598** PMA before returning. If an error does occur, then the final value of
103599** *piOffset is undefined.
103600*/
103601static int vdbeMergeEngineLevel0(
103602 SortSubtask *pTask, /* Sorter task to read from */
103603 int nPMA, /* Number of PMAs to read */
103604 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
103605 MergeEngine **ppOut /* OUT: New merge-engine */
103606){
103607 MergeEngine *pNew; /* Merge engine to return */
103608 i64 iOff = *piOffset;
103609 int i;
103610 int rc = SQLITE_OK;
103611
103612 *ppOut = pNew = vdbeMergeEngineNew(nReader: nPMA);
103613 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
103614
103615 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
103616 i64 nDummy = 0;
103617 PmaReader *pReadr = &pNew->aReadr[i];
103618 rc = vdbePmaReaderInit(pTask, pFile: &pTask->file, iStart: iOff, pReadr, pnByte: &nDummy);
103619 iOff = pReadr->iEof;
103620 }
103621
103622 if( rc!=SQLITE_OK ){
103623 vdbeMergeEngineFree(pMerger: pNew);
103624 *ppOut = 0;
103625 }
103626 *piOffset = iOff;
103627 return rc;
103628}
103629
103630/*
103631** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
103632** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
103633**
103634** i.e.
103635**
103636** nPMA<=16 -> TreeDepth() == 0
103637** nPMA<=256 -> TreeDepth() == 1
103638** nPMA<=65536 -> TreeDepth() == 2
103639*/
103640static int vdbeSorterTreeDepth(int nPMA){
103641 int nDepth = 0;
103642 i64 nDiv = SORTER_MAX_MERGE_COUNT;
103643 while( nDiv < (i64)nPMA ){
103644 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
103645 nDepth++;
103646 }
103647 return nDepth;
103648}
103649
103650/*
103651** pRoot is the root of an incremental merge-tree with depth nDepth (according
103652** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
103653** tree, counting from zero. This function adds pLeaf to the tree.
103654**
103655** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
103656** code is returned and pLeaf is freed.
103657*/
103658static int vdbeSorterAddToTree(
103659 SortSubtask *pTask, /* Task context */
103660 int nDepth, /* Depth of tree according to TreeDepth() */
103661 int iSeq, /* Sequence number of leaf within tree */
103662 MergeEngine *pRoot, /* Root of tree */
103663 MergeEngine *pLeaf /* Leaf to add to tree */
103664){
103665 int rc = SQLITE_OK;
103666 int nDiv = 1;
103667 int i;
103668 MergeEngine *p = pRoot;
103669 IncrMerger *pIncr;
103670
103671 rc = vdbeIncrMergerNew(pTask, pMerger: pLeaf, ppOut: &pIncr);
103672
103673 for(i=1; i<nDepth; i++){
103674 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
103675 }
103676
103677 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
103678 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
103679 PmaReader *pReadr = &p->aReadr[iIter];
103680
103681 if( pReadr->pIncr==0 ){
103682 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
103683 if( pNew==0 ){
103684 rc = SQLITE_NOMEM_BKPT;
103685 }else{
103686 rc = vdbeIncrMergerNew(pTask, pMerger: pNew, ppOut: &pReadr->pIncr);
103687 }
103688 }
103689 if( rc==SQLITE_OK ){
103690 p = pReadr->pIncr->pMerger;
103691 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
103692 }
103693 }
103694
103695 if( rc==SQLITE_OK ){
103696 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
103697 }else{
103698 vdbeIncrFree(pIncr);
103699 }
103700 return rc;
103701}
103702
103703/*
103704** This function is called as part of a SorterRewind() operation on a sorter
103705** that has already written two or more level-0 PMAs to one or more temp
103706** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
103707** can be used to incrementally merge all PMAs on disk.
103708**
103709** If successful, SQLITE_OK is returned and *ppOut set to point to the
103710** MergeEngine object at the root of the tree before returning. Or, if an
103711** error occurs, an SQLite error code is returned and the final value
103712** of *ppOut is undefined.
103713*/
103714static int vdbeSorterMergeTreeBuild(
103715 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
103716 MergeEngine **ppOut /* Write the MergeEngine here */
103717){
103718 MergeEngine *pMain = 0;
103719 int rc = SQLITE_OK;
103720 int iTask;
103721
103722#if SQLITE_MAX_WORKER_THREADS>0
103723 /* If the sorter uses more than one task, then create the top-level
103724 ** MergeEngine here. This MergeEngine will read data from exactly
103725 ** one PmaReader per sub-task. */
103726 assert( pSorter->bUseThreads || pSorter->nTask==1 );
103727 if( pSorter->nTask>1 ){
103728 pMain = vdbeMergeEngineNew(nReader: pSorter->nTask);
103729 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
103730 }
103731#endif
103732
103733 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
103734 SortSubtask *pTask = &pSorter->aTask[iTask];
103735 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
103736 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
103737 MergeEngine *pRoot = 0; /* Root node of tree for this task */
103738 int nDepth = vdbeSorterTreeDepth(nPMA: pTask->nPMA);
103739 i64 iReadOff = 0;
103740
103741 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
103742 rc = vdbeMergeEngineLevel0(pTask, nPMA: pTask->nPMA, piOffset: &iReadOff, ppOut: &pRoot);
103743 }else{
103744 int i;
103745 int iSeq = 0;
103746 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
103747 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
103748 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
103749 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
103750 int nReader; /* Number of level-0 PMAs to merge */
103751
103752 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
103753 rc = vdbeMergeEngineLevel0(pTask, nPMA: nReader, piOffset: &iReadOff, ppOut: &pMerger);
103754 if( rc==SQLITE_OK ){
103755 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq: iSeq++, pRoot, pLeaf: pMerger);
103756 }
103757 }
103758 }
103759
103760 if( rc==SQLITE_OK ){
103761#if SQLITE_MAX_WORKER_THREADS>0
103762 if( pMain!=0 ){
103763 rc = vdbeIncrMergerNew(pTask, pMerger: pRoot, ppOut: &pMain->aReadr[iTask].pIncr);
103764 }else
103765#endif
103766 {
103767 assert( pMain==0 );
103768 pMain = pRoot;
103769 }
103770 }else{
103771 vdbeMergeEngineFree(pMerger: pRoot);
103772 }
103773 }
103774 }
103775
103776 if( rc!=SQLITE_OK ){
103777 vdbeMergeEngineFree(pMerger: pMain);
103778 pMain = 0;
103779 }
103780 *ppOut = pMain;
103781 return rc;
103782}
103783
103784/*
103785** This function is called as part of an sqlite3VdbeSorterRewind() operation
103786** on a sorter that has written two or more PMAs to temporary files. It sets
103787** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
103788** (for multi-threaded sorters) so that it can be used to iterate through
103789** all records stored in the sorter.
103790**
103791** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
103792*/
103793static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
103794 int rc; /* Return code */
103795 SortSubtask *pTask0 = &pSorter->aTask[0];
103796 MergeEngine *pMain = 0;
103797#if SQLITE_MAX_WORKER_THREADS
103798 sqlite3 *db = pTask0->pSorter->db;
103799 int i;
103800 SorterCompare xCompare = vdbeSorterGetCompare(p: pSorter);
103801 for(i=0; i<pSorter->nTask; i++){
103802 pSorter->aTask[i].xCompare = xCompare;
103803 }
103804#endif
103805
103806 rc = vdbeSorterMergeTreeBuild(pSorter, ppOut: &pMain);
103807 if( rc==SQLITE_OK ){
103808#if SQLITE_MAX_WORKER_THREADS
103809 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
103810 if( pSorter->bUseThreads ){
103811 int iTask;
103812 PmaReader *pReadr = 0;
103813 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
103814 rc = vdbeSortAllocUnpacked(pTask: pLast);
103815 if( rc==SQLITE_OK ){
103816 pReadr = (PmaReader*)sqlite3DbMallocZero(db, n: sizeof(PmaReader));
103817 pSorter->pReader = pReadr;
103818 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
103819 }
103820 if( rc==SQLITE_OK ){
103821 rc = vdbeIncrMergerNew(pTask: pLast, pMerger: pMain, ppOut: &pReadr->pIncr);
103822 if( rc==SQLITE_OK ){
103823 vdbeIncrMergerSetThreads(pIncr: pReadr->pIncr);
103824 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
103825 IncrMerger *pIncr;
103826 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
103827 vdbeIncrMergerSetThreads(pIncr);
103828 assert( pIncr->pTask!=pLast );
103829 }
103830 }
103831 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
103832 /* Check that:
103833 **
103834 ** a) The incremental merge object is configured to use the
103835 ** right task, and
103836 ** b) If it is using task (nTask-1), it is configured to run
103837 ** in single-threaded mode. This is important, as the
103838 ** root merge (INCRINIT_ROOT) will be using the same task
103839 ** object.
103840 */
103841 PmaReader *p = &pMain->aReadr[iTask];
103842 assert( p->pIncr==0 || (
103843 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
103844 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
103845 ));
103846 rc = vdbePmaReaderIncrInit(pReadr: p, INCRINIT_TASK);
103847 }
103848 }
103849 pMain = 0;
103850 }
103851 if( rc==SQLITE_OK ){
103852 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
103853 }
103854 }else
103855#endif
103856 {
103857 rc = vdbeMergeEngineInit(pTask: pTask0, pMerger: pMain, INCRINIT_NORMAL);
103858 pSorter->pMerger = pMain;
103859 pMain = 0;
103860 }
103861 }
103862
103863 if( rc!=SQLITE_OK ){
103864 vdbeMergeEngineFree(pMerger: pMain);
103865 }
103866 return rc;
103867}
103868
103869
103870/*
103871** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
103872** this function is called to prepare for iterating through the records
103873** in sorted order.
103874*/
103875SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
103876 VdbeSorter *pSorter;
103877 int rc = SQLITE_OK; /* Return code */
103878
103879 assert( pCsr->eCurType==CURTYPE_SORTER );
103880 pSorter = pCsr->uc.pSorter;
103881 assert( pSorter );
103882
103883 /* If no data has been written to disk, then do not do so now. Instead,
103884 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
103885 ** from the in-memory list. */
103886 if( pSorter->bUsePMA==0 ){
103887 if( pSorter->list.pList ){
103888 *pbEof = 0;
103889 rc = vdbeSorterSort(pTask: &pSorter->aTask[0], pList: &pSorter->list);
103890 }else{
103891 *pbEof = 1;
103892 }
103893 return rc;
103894 }
103895
103896 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
103897 ** function flushes the contents of memory to disk, it immediately always
103898 ** creates a new list consisting of a single key immediately afterwards.
103899 ** So the list is never empty at this point. */
103900 assert( pSorter->list.pList );
103901 rc = vdbeSorterFlushPMA(pSorter);
103902
103903 /* Join all threads */
103904 rc = vdbeSorterJoinAll(pSorter, rcin: rc);
103905
103906 vdbeSorterRewindDebug("rewind");
103907
103908 /* Assuming no errors have occurred, set up a merger structure to
103909 ** incrementally read and merge all remaining PMAs. */
103910 assert( pSorter->pReader==0 );
103911 if( rc==SQLITE_OK ){
103912 rc = vdbeSorterSetupMerge(pSorter);
103913 *pbEof = 0;
103914 }
103915
103916 vdbeSorterRewindDebug("rewinddone");
103917 return rc;
103918}
103919
103920/*
103921** Advance to the next element in the sorter. Return value:
103922**
103923** SQLITE_OK success
103924** SQLITE_DONE end of data
103925** otherwise some kind of error.
103926*/
103927SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
103928 VdbeSorter *pSorter;
103929 int rc; /* Return code */
103930
103931 assert( pCsr->eCurType==CURTYPE_SORTER );
103932 pSorter = pCsr->uc.pSorter;
103933 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
103934 if( pSorter->bUsePMA ){
103935 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
103936 assert( pSorter->bUseThreads==0 || pSorter->pReader );
103937 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
103938#if SQLITE_MAX_WORKER_THREADS>0
103939 if( pSorter->bUseThreads ){
103940 rc = vdbePmaReaderNext(pReadr: pSorter->pReader);
103941 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
103942 }else
103943#endif
103944 /*if( !pSorter->bUseThreads )*/ {
103945 int res = 0;
103946 assert( pSorter->pMerger!=0 );
103947 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
103948 rc = vdbeMergeEngineStep(pMerger: pSorter->pMerger, pbEof: &res);
103949 if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
103950 }
103951 }else{
103952 SorterRecord *pFree = pSorter->list.pList;
103953 pSorter->list.pList = pFree->u.pNext;
103954 pFree->u.pNext = 0;
103955 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pRecord: pFree);
103956 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
103957 }
103958 return rc;
103959}
103960
103961/*
103962** Return a pointer to a buffer owned by the sorter that contains the
103963** current key.
103964*/
103965static void *vdbeSorterRowkey(
103966 const VdbeSorter *pSorter, /* Sorter object */
103967 int *pnKey /* OUT: Size of current key in bytes */
103968){
103969 void *pKey;
103970 if( pSorter->bUsePMA ){
103971 PmaReader *pReader;
103972#if SQLITE_MAX_WORKER_THREADS>0
103973 if( pSorter->bUseThreads ){
103974 pReader = pSorter->pReader;
103975 }else
103976#endif
103977 /*if( !pSorter->bUseThreads )*/{
103978 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
103979 }
103980 *pnKey = pReader->nKey;
103981 pKey = pReader->aKey;
103982 }else{
103983 *pnKey = pSorter->list.pList->nVal;
103984 pKey = SRVAL(pSorter->list.pList);
103985 }
103986 return pKey;
103987}
103988
103989/*
103990** Copy the current sorter key into the memory cell pOut.
103991*/
103992SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
103993 VdbeSorter *pSorter;
103994 void *pKey; int nKey; /* Sorter key to copy into pOut */
103995
103996 assert( pCsr->eCurType==CURTYPE_SORTER );
103997 pSorter = pCsr->uc.pSorter;
103998 pKey = vdbeSorterRowkey(pSorter, pnKey: &nKey);
103999 if( sqlite3VdbeMemClearAndResize(pMem: pOut, szNew: nKey) ){
104000 return SQLITE_NOMEM_BKPT;
104001 }
104002 pOut->n = nKey;
104003 MemSetTypeFlag(pOut, MEM_Blob);
104004 memcpy(dest: pOut->z, src: pKey, n: nKey);
104005
104006 return SQLITE_OK;
104007}
104008
104009/*
104010** Compare the key in memory cell pVal with the key that the sorter cursor
104011** passed as the first argument currently points to. For the purposes of
104012** the comparison, ignore the rowid field at the end of each record.
104013**
104014** If the sorter cursor key contains any NULL values, consider it to be
104015** less than pVal. Even if pVal also contains NULL values.
104016**
104017** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
104018** Otherwise, set *pRes to a negative, zero or positive value if the
104019** key in pVal is smaller than, equal to or larger than the current sorter
104020** key.
104021**
104022** This routine forms the core of the OP_SorterCompare opcode, which in
104023** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
104024*/
104025SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
104026 const VdbeCursor *pCsr, /* Sorter cursor */
104027 Mem *pVal, /* Value to compare to current sorter key */
104028 int nKeyCol, /* Compare this many columns */
104029 int *pRes /* OUT: Result of comparison */
104030){
104031 VdbeSorter *pSorter;
104032 UnpackedRecord *r2;
104033 KeyInfo *pKeyInfo;
104034 int i;
104035 void *pKey; int nKey; /* Sorter key to compare pVal with */
104036
104037 assert( pCsr->eCurType==CURTYPE_SORTER );
104038 pSorter = pCsr->uc.pSorter;
104039 r2 = pSorter->pUnpacked;
104040 pKeyInfo = pCsr->pKeyInfo;
104041 if( r2==0 ){
104042 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
104043 if( r2==0 ) return SQLITE_NOMEM_BKPT;
104044 r2->nField = nKeyCol;
104045 }
104046 assert( r2->nField==nKeyCol );
104047
104048 pKey = vdbeSorterRowkey(pSorter, pnKey: &nKey);
104049 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, p: r2);
104050 for(i=0; i<nKeyCol; i++){
104051 if( r2->aMem[i].flags & MEM_Null ){
104052 *pRes = -1;
104053 return SQLITE_OK;
104054 }
104055 }
104056
104057 *pRes = sqlite3VdbeRecordCompare(nKey1: pVal->n, pKey1: pVal->z, pPKey2: r2);
104058 return SQLITE_OK;
104059}
104060
104061/************** End of vdbesort.c ********************************************/
104062/************** Begin file vdbevtab.c ****************************************/
104063/*
104064** 2020-03-23
104065**
104066** The author disclaims copyright to this source code. In place of
104067** a legal notice, here is a blessing:
104068**
104069** May you do good and not evil.
104070** May you find forgiveness for yourself and forgive others.
104071** May you share freely, never taking more than you give.
104072**
104073*************************************************************************
104074**
104075** This file implements virtual-tables for examining the bytecode content
104076** of a prepared statement.
104077*/
104078/* #include "sqliteInt.h" */
104079#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
104080/* #include "vdbeInt.h" */
104081
104082/* An instance of the bytecode() table-valued function.
104083*/
104084typedef struct bytecodevtab bytecodevtab;
104085struct bytecodevtab {
104086 sqlite3_vtab base; /* Base class - must be first */
104087 sqlite3 *db; /* Database connection */
104088 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
104089};
104090
104091/* A cursor for scanning through the bytecode
104092*/
104093typedef struct bytecodevtab_cursor bytecodevtab_cursor;
104094struct bytecodevtab_cursor {
104095 sqlite3_vtab_cursor base; /* Base class - must be first */
104096 sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */
104097 int iRowid; /* The rowid of the output table */
104098 int iAddr; /* Address */
104099 int needFinalize; /* Cursors owns pStmt and must finalize it */
104100 int showSubprograms; /* Provide a listing of subprograms */
104101 Op *aOp; /* Operand array */
104102 char *zP4; /* Rendered P4 value */
104103 const char *zType; /* tables_used.type */
104104 const char *zSchema; /* tables_used.schema */
104105 const char *zName; /* tables_used.name */
104106 Mem sub; /* Subprograms */
104107};
104108
104109/*
104110** Create a new bytecode() table-valued function.
104111*/
104112static int bytecodevtabConnect(
104113 sqlite3 *db,
104114 void *pAux,
104115 int argc, const char *const*argv,
104116 sqlite3_vtab **ppVtab,
104117 char **pzErr
104118){
104119 bytecodevtab *pNew;
104120 int rc;
104121 int isTabUsed = pAux!=0;
104122 const char *azSchema[2] = {
104123 /* bytecode() schema */
104124 "CREATE TABLE x("
104125 "addr INT,"
104126 "opcode TEXT,"
104127 "p1 INT,"
104128 "p2 INT,"
104129 "p3 INT,"
104130 "p4 TEXT,"
104131 "p5 INT,"
104132 "comment TEXT,"
104133 "subprog TEXT,"
104134 "nexec INT,"
104135 "ncycle INT,"
104136 "stmt HIDDEN"
104137 ");",
104138
104139 /* Tables_used() schema */
104140 "CREATE TABLE x("
104141 "type TEXT,"
104142 "schema TEXT,"
104143 "name TEXT,"
104144 "wr INT,"
104145 "subprog TEXT,"
104146 "stmt HIDDEN"
104147 ");"
104148 };
104149
104150 (void)argc;
104151 (void)argv;
104152 (void)pzErr;
104153 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
104154 if( rc==SQLITE_OK ){
104155 pNew = sqlite3_malloc( sizeof(*pNew) );
104156 *ppVtab = (sqlite3_vtab*)pNew;
104157 if( pNew==0 ) return SQLITE_NOMEM;
104158 memset(pNew, 0, sizeof(*pNew));
104159 pNew->db = db;
104160 pNew->bTablesUsed = isTabUsed*2;
104161 }
104162 return rc;
104163}
104164
104165/*
104166** This method is the destructor for bytecodevtab objects.
104167*/
104168static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
104169 bytecodevtab *p = (bytecodevtab*)pVtab;
104170 sqlite3_free(p);
104171 return SQLITE_OK;
104172}
104173
104174/*
104175** Constructor for a new bytecodevtab_cursor object.
104176*/
104177static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
104178 bytecodevtab *pVTab = (bytecodevtab*)p;
104179 bytecodevtab_cursor *pCur;
104180 pCur = sqlite3_malloc( sizeof(*pCur) );
104181 if( pCur==0 ) return SQLITE_NOMEM;
104182 memset(pCur, 0, sizeof(*pCur));
104183 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
104184 *ppCursor = &pCur->base;
104185 return SQLITE_OK;
104186}
104187
104188/*
104189** Clear all internal content from a bytecodevtab cursor.
104190*/
104191static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
104192 sqlite3_free(pCur->zP4);
104193 pCur->zP4 = 0;
104194 sqlite3VdbeMemRelease(&pCur->sub);
104195 sqlite3VdbeMemSetNull(&pCur->sub);
104196 if( pCur->needFinalize ){
104197 sqlite3_finalize(pCur->pStmt);
104198 }
104199 pCur->pStmt = 0;
104200 pCur->needFinalize = 0;
104201 pCur->zType = 0;
104202 pCur->zSchema = 0;
104203 pCur->zName = 0;
104204}
104205
104206/*
104207** Destructor for a bytecodevtab_cursor.
104208*/
104209static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
104210 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
104211 bytecodevtabCursorClear(pCur);
104212 sqlite3_free(pCur);
104213 return SQLITE_OK;
104214}
104215
104216
104217/*
104218** Advance a bytecodevtab_cursor to its next row of output.
104219*/
104220static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
104221 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
104222 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
104223 int rc;
104224 if( pCur->zP4 ){
104225 sqlite3_free(pCur->zP4);
104226 pCur->zP4 = 0;
104227 }
104228 if( pCur->zName ){
104229 pCur->zName = 0;
104230 pCur->zType = 0;
104231 pCur->zSchema = 0;
104232 }
104233 rc = sqlite3VdbeNextOpcode(
104234 (Vdbe*)pCur->pStmt,
104235 pCur->showSubprograms ? &pCur->sub : 0,
104236 pTab->bTablesUsed,
104237 &pCur->iRowid,
104238 &pCur->iAddr,
104239 &pCur->aOp);
104240 if( rc!=SQLITE_OK ){
104241 sqlite3VdbeMemSetNull(&pCur->sub);
104242 pCur->aOp = 0;
104243 }
104244 return SQLITE_OK;
104245}
104246
104247/*
104248** Return TRUE if the cursor has been moved off of the last
104249** row of output.
104250*/
104251static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
104252 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
104253 return pCur->aOp==0;
104254}
104255
104256/*
104257** Return values of columns for the row at which the bytecodevtab_cursor
104258** is currently pointing.
104259*/
104260static int bytecodevtabColumn(
104261 sqlite3_vtab_cursor *cur, /* The cursor */
104262 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
104263 int i /* Which column to return */
104264){
104265 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
104266 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
104267 Op *pOp = pCur->aOp + pCur->iAddr;
104268 if( pVTab->bTablesUsed ){
104269 if( i==4 ){
104270 i = 8;
104271 }else{
104272 if( i<=2 && pCur->zType==0 ){
104273 Schema *pSchema;
104274 HashElem *k;
104275 int iDb = pOp->p3;
104276 Pgno iRoot = (Pgno)pOp->p2;
104277 sqlite3 *db = pVTab->db;
104278 pSchema = db->aDb[iDb].pSchema;
104279 pCur->zSchema = db->aDb[iDb].zDbSName;
104280 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
104281 Table *pTab = (Table*)sqliteHashData(k);
104282 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
104283 pCur->zName = pTab->zName;
104284 pCur->zType = "table";
104285 break;
104286 }
104287 }
104288 if( pCur->zName==0 ){
104289 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
104290 Index *pIdx = (Index*)sqliteHashData(k);
104291 if( pIdx->tnum==iRoot ){
104292 pCur->zName = pIdx->zName;
104293 pCur->zType = "index";
104294 }
104295 }
104296 }
104297 }
104298 i += 20;
104299 }
104300 }
104301 switch( i ){
104302 case 0: /* addr */
104303 sqlite3_result_int(ctx, pCur->iAddr);
104304 break;
104305 case 1: /* opcode */
104306 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
104307 -1, SQLITE_STATIC);
104308 break;
104309 case 2: /* p1 */
104310 sqlite3_result_int(ctx, pOp->p1);
104311 break;
104312 case 3: /* p2 */
104313 sqlite3_result_int(ctx, pOp->p2);
104314 break;
104315 case 4: /* p3 */
104316 sqlite3_result_int(ctx, pOp->p3);
104317 break;
104318 case 5: /* p4 */
104319 case 7: /* comment */
104320 if( pCur->zP4==0 ){
104321 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
104322 }
104323 if( i==5 ){
104324 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
104325 }else{
104326#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
104327 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
104328 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
104329#endif
104330 }
104331 break;
104332 case 6: /* p5 */
104333 sqlite3_result_int(ctx, pOp->p5);
104334 break;
104335 case 8: { /* subprog */
104336 Op *aOp = pCur->aOp;
104337 assert( aOp[0].opcode==OP_Init );
104338 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
104339 if( pCur->iRowid==pCur->iAddr+1 ){
104340 break; /* Result is NULL for the main program */
104341 }else if( aOp[0].p4.z!=0 ){
104342 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
104343 }else{
104344 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC);
104345 }
104346 break;
104347 }
104348
104349#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
104350 case 9: /* nexec */
104351 sqlite3_result_int(ctx, pOp->nExec);
104352 break;
104353 case 10: /* ncycle */
104354 sqlite3_result_int(ctx, pOp->nCycle);
104355 break;
104356#else
104357 case 9: /* nexec */
104358 case 10: /* ncycle */
104359 sqlite3_result_int(ctx, 0);
104360 break;
104361#endif
104362
104363 case 20: /* tables_used.type */
104364 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
104365 break;
104366 case 21: /* tables_used.schema */
104367 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
104368 break;
104369 case 22: /* tables_used.name */
104370 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
104371 break;
104372 case 23: /* tables_used.wr */
104373 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
104374 break;
104375 }
104376 return SQLITE_OK;
104377}
104378
104379/*
104380** Return the rowid for the current row. In this implementation, the
104381** rowid is the same as the output value.
104382*/
104383static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
104384 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
104385 *pRowid = pCur->iRowid;
104386 return SQLITE_OK;
104387}
104388
104389/*
104390** Initialize a cursor.
104391**
104392** idxNum==0 means show all subprograms
104393** idxNum==1 means show only the main bytecode and omit subprograms.
104394*/
104395static int bytecodevtabFilter(
104396 sqlite3_vtab_cursor *pVtabCursor,
104397 int idxNum, const char *idxStr,
104398 int argc, sqlite3_value **argv
104399){
104400 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
104401 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
104402 int rc = SQLITE_OK;
104403 (void)idxStr;
104404
104405 bytecodevtabCursorClear(pCur);
104406 pCur->iRowid = 0;
104407 pCur->iAddr = 0;
104408 pCur->showSubprograms = idxNum==0;
104409 assert( argc==1 );
104410 if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
104411 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
104412 if( zSql==0 ){
104413 rc = SQLITE_NOMEM;
104414 }else{
104415 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
104416 pCur->needFinalize = 1;
104417 }
104418 }else{
104419 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
104420 }
104421 if( pCur->pStmt==0 ){
104422 pVTab->base.zErrMsg = sqlite3_mprintf(
104423 "argument to %s() is not a valid SQL statement",
104424 pVTab->bTablesUsed ? "tables_used" : "bytecode"
104425 );
104426 rc = SQLITE_ERROR;
104427 }else{
104428 bytecodevtabNext(pVtabCursor);
104429 }
104430 return rc;
104431}
104432
104433/*
104434** We must have a single stmt=? constraint that will be passed through
104435** into the xFilter method. If there is no valid stmt=? constraint,
104436** then return an SQLITE_CONSTRAINT error.
104437*/
104438static int bytecodevtabBestIndex(
104439 sqlite3_vtab *tab,
104440 sqlite3_index_info *pIdxInfo
104441){
104442 int i;
104443 int rc = SQLITE_CONSTRAINT;
104444 struct sqlite3_index_constraint *p;
104445 bytecodevtab *pVTab = (bytecodevtab*)tab;
104446 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
104447 pIdxInfo->estimatedCost = (double)100;
104448 pIdxInfo->estimatedRows = 100;
104449 pIdxInfo->idxNum = 0;
104450 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
104451 if( p->usable==0 ) continue;
104452 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
104453 rc = SQLITE_OK;
104454 pIdxInfo->aConstraintUsage[i].omit = 1;
104455 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
104456 }
104457 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
104458 pIdxInfo->aConstraintUsage[i].omit = 1;
104459 pIdxInfo->idxNum = 1;
104460 }
104461 }
104462 return rc;
104463}
104464
104465/*
104466** This following structure defines all the methods for the
104467** virtual table.
104468*/
104469static sqlite3_module bytecodevtabModule = {
104470 /* iVersion */ 0,
104471 /* xCreate */ 0,
104472 /* xConnect */ bytecodevtabConnect,
104473 /* xBestIndex */ bytecodevtabBestIndex,
104474 /* xDisconnect */ bytecodevtabDisconnect,
104475 /* xDestroy */ 0,
104476 /* xOpen */ bytecodevtabOpen,
104477 /* xClose */ bytecodevtabClose,
104478 /* xFilter */ bytecodevtabFilter,
104479 /* xNext */ bytecodevtabNext,
104480 /* xEof */ bytecodevtabEof,
104481 /* xColumn */ bytecodevtabColumn,
104482 /* xRowid */ bytecodevtabRowid,
104483 /* xUpdate */ 0,
104484 /* xBegin */ 0,
104485 /* xSync */ 0,
104486 /* xCommit */ 0,
104487 /* xRollback */ 0,
104488 /* xFindMethod */ 0,
104489 /* xRename */ 0,
104490 /* xSavepoint */ 0,
104491 /* xRelease */ 0,
104492 /* xRollbackTo */ 0,
104493 /* xShadowName */ 0
104494};
104495
104496
104497SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
104498 int rc;
104499 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
104500 if( rc==SQLITE_OK ){
104501 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
104502 }
104503 return rc;
104504}
104505#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
104506SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
104507#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
104508
104509/************** End of vdbevtab.c ********************************************/
104510/************** Begin file memjournal.c **************************************/
104511/*
104512** 2008 October 7
104513**
104514** The author disclaims copyright to this source code. In place of
104515** a legal notice, here is a blessing:
104516**
104517** May you do good and not evil.
104518** May you find forgiveness for yourself and forgive others.
104519** May you share freely, never taking more than you give.
104520**
104521*************************************************************************
104522**
104523** This file contains code use to implement an in-memory rollback journal.
104524** The in-memory rollback journal is used to journal transactions for
104525** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
104526**
104527** Update: The in-memory journal is also used to temporarily cache
104528** smaller journals that are not critical for power-loss recovery.
104529** For example, statement journals that are not too big will be held
104530** entirely in memory, thus reducing the number of file I/O calls, and
104531** more importantly, reducing temporary file creation events. If these
104532** journals become too large for memory, they are spilled to disk. But
104533** in the common case, they are usually small and no file I/O needs to
104534** occur.
104535*/
104536/* #include "sqliteInt.h" */
104537
104538/* Forward references to internal structures */
104539typedef struct MemJournal MemJournal;
104540typedef struct FilePoint FilePoint;
104541typedef struct FileChunk FileChunk;
104542
104543/*
104544** The rollback journal is composed of a linked list of these structures.
104545**
104546** The zChunk array is always at least 8 bytes in size - usually much more.
104547** Its actual size is stored in the MemJournal.nChunkSize variable.
104548*/
104549struct FileChunk {
104550 FileChunk *pNext; /* Next chunk in the journal */
104551 u8 zChunk[8]; /* Content of this chunk */
104552};
104553
104554/*
104555** By default, allocate this many bytes of memory for each FileChunk object.
104556*/
104557#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
104558
104559/*
104560** For chunk size nChunkSize, return the number of bytes that should
104561** be allocated for each FileChunk structure.
104562*/
104563#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
104564
104565/*
104566** An instance of this object serves as a cursor into the rollback journal.
104567** The cursor can be either for reading or writing.
104568*/
104569struct FilePoint {
104570 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
104571 FileChunk *pChunk; /* Specific chunk into which cursor points */
104572};
104573
104574/*
104575** This structure is a subclass of sqlite3_file. Each open memory-journal
104576** is an instance of this class.
104577*/
104578struct MemJournal {
104579 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
104580 int nChunkSize; /* In-memory chunk-size */
104581
104582 int nSpill; /* Bytes of data before flushing */
104583 FileChunk *pFirst; /* Head of in-memory chunk-list */
104584 FilePoint endpoint; /* Pointer to the end of the file */
104585 FilePoint readpoint; /* Pointer to the end of the last xRead() */
104586
104587 int flags; /* xOpen flags */
104588 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
104589 const char *zJournal; /* Name of the journal file */
104590};
104591
104592/*
104593** Read data from the in-memory journal file. This is the implementation
104594** of the sqlite3_vfs.xRead method.
104595*/
104596static int memjrnlRead(
104597 sqlite3_file *pJfd, /* The journal file from which to read */
104598 void *zBuf, /* Put the results here */
104599 int iAmt, /* Number of bytes to read */
104600 sqlite_int64 iOfst /* Begin reading at this offset */
104601){
104602 MemJournal *p = (MemJournal *)pJfd;
104603 u8 *zOut = zBuf;
104604 int nRead = iAmt;
104605 int iChunkOffset;
104606 FileChunk *pChunk;
104607
104608 if( (iAmt+iOfst)>p->endpoint.iOffset ){
104609 return SQLITE_IOERR_SHORT_READ;
104610 }
104611 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
104612 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
104613 sqlite3_int64 iOff = 0;
104614 for(pChunk=p->pFirst;
104615 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
104616 pChunk=pChunk->pNext
104617 ){
104618 iOff += p->nChunkSize;
104619 }
104620 }else{
104621 pChunk = p->readpoint.pChunk;
104622 assert( pChunk!=0 );
104623 }
104624
104625 iChunkOffset = (int)(iOfst%p->nChunkSize);
104626 do {
104627 int iSpace = p->nChunkSize - iChunkOffset;
104628 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
104629 memcpy(dest: zOut, src: (u8*)pChunk->zChunk + iChunkOffset, n: nCopy);
104630 zOut += nCopy;
104631 nRead -= iSpace;
104632 iChunkOffset = 0;
104633 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
104634 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
104635 p->readpoint.pChunk = pChunk;
104636
104637 return SQLITE_OK;
104638}
104639
104640/*
104641** Free the list of FileChunk structures headed at MemJournal.pFirst.
104642*/
104643static void memjrnlFreeChunks(FileChunk *pFirst){
104644 FileChunk *pIter;
104645 FileChunk *pNext;
104646 for(pIter=pFirst; pIter; pIter=pNext){
104647 pNext = pIter->pNext;
104648 sqlite3_free(p: pIter);
104649 }
104650}
104651
104652/*
104653** Flush the contents of memory to a real file on disk.
104654*/
104655static int memjrnlCreateFile(MemJournal *p){
104656 int rc;
104657 sqlite3_file *pReal = (sqlite3_file*)p;
104658 MemJournal copy = *p;
104659
104660 memset(s: p, c: 0, n: sizeof(MemJournal));
104661 rc = sqlite3OsOpen(pVfs: copy.pVfs, zPath: copy.zJournal, pFile: pReal, flags: copy.flags, pFlagsOut: 0);
104662 if( rc==SQLITE_OK ){
104663 int nChunk = copy.nChunkSize;
104664 i64 iOff = 0;
104665 FileChunk *pIter;
104666 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
104667 if( iOff + nChunk > copy.endpoint.iOffset ){
104668 nChunk = copy.endpoint.iOffset - iOff;
104669 }
104670 rc = sqlite3OsWrite(id: pReal, pBuf: (u8*)pIter->zChunk, amt: nChunk, offset: iOff);
104671 if( rc ) break;
104672 iOff += nChunk;
104673 }
104674 if( rc==SQLITE_OK ){
104675 /* No error has occurred. Free the in-memory buffers. */
104676 memjrnlFreeChunks(pFirst: copy.pFirst);
104677 }
104678 }
104679 if( rc!=SQLITE_OK ){
104680 /* If an error occurred while creating or writing to the file, restore
104681 ** the original before returning. This way, SQLite uses the in-memory
104682 ** journal data to roll back changes made to the internal page-cache
104683 ** before this function was called. */
104684 sqlite3OsClose(pId: pReal);
104685 *p = copy;
104686 }
104687 return rc;
104688}
104689
104690
104691/* Forward reference */
104692static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size);
104693
104694/*
104695** Write data to the file.
104696*/
104697static int memjrnlWrite(
104698 sqlite3_file *pJfd, /* The journal file into which to write */
104699 const void *zBuf, /* Take data to be written from here */
104700 int iAmt, /* Number of bytes to write */
104701 sqlite_int64 iOfst /* Begin writing at this offset into the file */
104702){
104703 MemJournal *p = (MemJournal *)pJfd;
104704 int nWrite = iAmt;
104705 u8 *zWrite = (u8 *)zBuf;
104706
104707 /* If the file should be created now, create it and write the new data
104708 ** into the file on disk. */
104709 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
104710 int rc = memjrnlCreateFile(p);
104711 if( rc==SQLITE_OK ){
104712 rc = sqlite3OsWrite(id: pJfd, pBuf: zBuf, amt: iAmt, offset: iOfst);
104713 }
104714 return rc;
104715 }
104716
104717 /* If the contents of this write should be stored in memory */
104718 else{
104719 /* An in-memory journal file should only ever be appended to. Random
104720 ** access writes are not required. The only exception to this is when
104721 ** the in-memory journal is being used by a connection using the
104722 ** atomic-write optimization. In this case the first 28 bytes of the
104723 ** journal file may be written as part of committing the transaction. */
104724 assert( iOfst<=p->endpoint.iOffset );
104725 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
104726 memjrnlTruncate(pJfd, size: iOfst);
104727 }
104728 if( iOfst==0 && p->pFirst ){
104729 assert( p->nChunkSize>iAmt );
104730 memcpy(dest: (u8*)p->pFirst->zChunk, src: zBuf, n: iAmt);
104731 }else{
104732 while( nWrite>0 ){
104733 FileChunk *pChunk = p->endpoint.pChunk;
104734 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
104735 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
104736
104737 assert( pChunk!=0 || iChunkOffset==0 );
104738 if( iChunkOffset==0 ){
104739 /* New chunk is required to extend the file. */
104740 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
104741 if( !pNew ){
104742 return SQLITE_IOERR_NOMEM_BKPT;
104743 }
104744 pNew->pNext = 0;
104745 if( pChunk ){
104746 assert( p->pFirst );
104747 pChunk->pNext = pNew;
104748 }else{
104749 assert( !p->pFirst );
104750 p->pFirst = pNew;
104751 }
104752 pChunk = p->endpoint.pChunk = pNew;
104753 }
104754
104755 assert( pChunk!=0 );
104756 memcpy(dest: (u8*)pChunk->zChunk + iChunkOffset, src: zWrite, n: iSpace);
104757 zWrite += iSpace;
104758 nWrite -= iSpace;
104759 p->endpoint.iOffset += iSpace;
104760 }
104761 }
104762 }
104763
104764 return SQLITE_OK;
104765}
104766
104767/*
104768** Truncate the in-memory file.
104769*/
104770static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
104771 MemJournal *p = (MemJournal *)pJfd;
104772 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
104773 if( size<p->endpoint.iOffset ){
104774 FileChunk *pIter = 0;
104775 if( size==0 ){
104776 memjrnlFreeChunks(pFirst: p->pFirst);
104777 p->pFirst = 0;
104778 }else{
104779 i64 iOff = p->nChunkSize;
104780 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
104781 iOff += p->nChunkSize;
104782 }
104783 if( ALWAYS(pIter) ){
104784 memjrnlFreeChunks(pFirst: pIter->pNext);
104785 pIter->pNext = 0;
104786 }
104787 }
104788
104789 p->endpoint.pChunk = pIter;
104790 p->endpoint.iOffset = size;
104791 p->readpoint.pChunk = 0;
104792 p->readpoint.iOffset = 0;
104793 }
104794 return SQLITE_OK;
104795}
104796
104797/*
104798** Close the file.
104799*/
104800static int memjrnlClose(sqlite3_file *pJfd){
104801 MemJournal *p = (MemJournal *)pJfd;
104802 memjrnlFreeChunks(pFirst: p->pFirst);
104803 return SQLITE_OK;
104804}
104805
104806/*
104807** Sync the file.
104808**
104809** If the real file has been created, call its xSync method. Otherwise,
104810** syncing an in-memory journal is a no-op.
104811*/
104812static int memjrnlSync(sqlite3_file *pJfd, int flags){
104813 UNUSED_PARAMETER2(pJfd, flags);
104814 return SQLITE_OK;
104815}
104816
104817/*
104818** Query the size of the file in bytes.
104819*/
104820static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
104821 MemJournal *p = (MemJournal *)pJfd;
104822 *pSize = (sqlite_int64) p->endpoint.iOffset;
104823 return SQLITE_OK;
104824}
104825
104826/*
104827** Table of methods for MemJournal sqlite3_file object.
104828*/
104829static const struct sqlite3_io_methods MemJournalMethods = {
104830 1, /* iVersion */
104831 memjrnlClose, /* xClose */
104832 memjrnlRead, /* xRead */
104833 memjrnlWrite, /* xWrite */
104834 memjrnlTruncate, /* xTruncate */
104835 memjrnlSync, /* xSync */
104836 memjrnlFileSize, /* xFileSize */
104837 0, /* xLock */
104838 0, /* xUnlock */
104839 0, /* xCheckReservedLock */
104840 0, /* xFileControl */
104841 0, /* xSectorSize */
104842 0, /* xDeviceCharacteristics */
104843 0, /* xShmMap */
104844 0, /* xShmLock */
104845 0, /* xShmBarrier */
104846 0, /* xShmUnmap */
104847 0, /* xFetch */
104848 0 /* xUnfetch */
104849};
104850
104851/*
104852** Open a journal file.
104853**
104854** The behaviour of the journal file depends on the value of parameter
104855** nSpill. If nSpill is 0, then the journal file is always create and
104856** accessed using the underlying VFS. If nSpill is less than zero, then
104857** all content is always stored in main-memory. Finally, if nSpill is a
104858** positive value, then the journal file is initially created in-memory
104859** but may be flushed to disk later on. In this case the journal file is
104860** flushed to disk either when it grows larger than nSpill bytes in size,
104861** or when sqlite3JournalCreate() is called.
104862*/
104863SQLITE_PRIVATE int sqlite3JournalOpen(
104864 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
104865 const char *zName, /* Name of the journal file */
104866 sqlite3_file *pJfd, /* Preallocated, blank file handle */
104867 int flags, /* Opening flags */
104868 int nSpill /* Bytes buffered before opening the file */
104869){
104870 MemJournal *p = (MemJournal*)pJfd;
104871
104872 assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) );
104873
104874 /* Zero the file-handle object. If nSpill was passed zero, initialize
104875 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
104876 ** case none of the code in this module is executed as a result of calls
104877 ** made on the journal file-handle. */
104878 memset(s: p, c: 0, n: sizeof(MemJournal));
104879 if( nSpill==0 ){
104880 return sqlite3OsOpen(pVfs, zPath: zName, pFile: pJfd, flags, pFlagsOut: 0);
104881 }
104882
104883 if( nSpill>0 ){
104884 p->nChunkSize = nSpill;
104885 }else{
104886 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
104887 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
104888 }
104889
104890 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
104891 p->nSpill = nSpill;
104892 p->flags = flags;
104893 p->zJournal = zName;
104894 p->pVfs = pVfs;
104895 return SQLITE_OK;
104896}
104897
104898/*
104899** Open an in-memory journal file.
104900*/
104901SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
104902 sqlite3JournalOpen(pVfs: 0, zName: 0, pJfd, flags: 0, nSpill: -1);
104903}
104904
104905#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
104906 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
104907/*
104908** If the argument p points to a MemJournal structure that is not an
104909** in-memory-only journal file (i.e. is one that was opened with a +ve
104910** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
104911** file has not yet been created, create it now.
104912*/
104913SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
104914 int rc = SQLITE_OK;
104915 MemJournal *p = (MemJournal*)pJfd;
104916 if( pJfd->pMethods==&MemJournalMethods && (
104917#ifdef SQLITE_ENABLE_ATOMIC_WRITE
104918 p->nSpill>0
104919#else
104920 /* While this appears to not be possible without ATOMIC_WRITE, the
104921 ** paths are complex, so it seems prudent to leave the test in as
104922 ** a NEVER(), in case our analysis is subtly flawed. */
104923 NEVER(p->nSpill>0)
104924#endif
104925#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
104926 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
104927#endif
104928 )){
104929 rc = memjrnlCreateFile(p);
104930 }
104931 return rc;
104932}
104933#endif
104934
104935/*
104936** The file-handle passed as the only argument is open on a journal file.
104937** Return true if this "journal file" is currently stored in heap memory,
104938** or false otherwise.
104939*/
104940SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
104941 return p->pMethods==&MemJournalMethods;
104942}
104943
104944/*
104945** Return the number of bytes required to store a JournalFile that uses vfs
104946** pVfs to create the underlying on-disk files.
104947*/
104948SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
104949 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
104950}
104951
104952/************** End of memjournal.c ******************************************/
104953/************** Begin file walker.c ******************************************/
104954/*
104955** 2008 August 16
104956**
104957** The author disclaims copyright to this source code. In place of
104958** a legal notice, here is a blessing:
104959**
104960** May you do good and not evil.
104961** May you find forgiveness for yourself and forgive others.
104962** May you share freely, never taking more than you give.
104963**
104964*************************************************************************
104965** This file contains routines used for walking the parser tree for
104966** an SQL statement.
104967*/
104968/* #include "sqliteInt.h" */
104969/* #include <stdlib.h> */
104970/* #include <string.h> */
104971
104972
104973#if !defined(SQLITE_OMIT_WINDOWFUNC)
104974/*
104975** Walk all expressions linked into the list of Window objects passed
104976** as the second argument.
104977*/
104978static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
104979 Window *pWin;
104980 for(pWin=pList; pWin; pWin=pWin->pNextWin){
104981 int rc;
104982 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
104983 if( rc ) return WRC_Abort;
104984 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
104985 if( rc ) return WRC_Abort;
104986 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
104987 if( rc ) return WRC_Abort;
104988 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
104989 if( rc ) return WRC_Abort;
104990 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
104991 if( rc ) return WRC_Abort;
104992 if( bOneOnly ) break;
104993 }
104994 return WRC_Continue;
104995}
104996#endif
104997
104998/*
104999** Walk an expression tree. Invoke the callback once for each node
105000** of the expression, while descending. (In other words, the callback
105001** is invoked before visiting children.)
105002**
105003** The return value from the callback should be one of the WRC_*
105004** constants to specify how to proceed with the walk.
105005**
105006** WRC_Continue Continue descending down the tree.
105007**
105008** WRC_Prune Do not descend into child nodes, but allow
105009** the walk to continue with sibling nodes.
105010**
105011** WRC_Abort Do no more callbacks. Unwind the stack and
105012** return from the top-level walk call.
105013**
105014** The return value from this routine is WRC_Abort to abandon the tree walk
105015** and WRC_Continue to continue.
105016*/
105017SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3WalkExprNN(Walker *pWalker, Expr *pExpr){
105018 int rc;
105019 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
105020 testcase( ExprHasProperty(pExpr, EP_Reduced) );
105021 while(1){
105022 rc = pWalker->xExprCallback(pWalker, pExpr);
105023 if( rc ) return rc & WRC_Abort;
105024 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
105025 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
105026 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr: pExpr->pLeft) ){
105027 return WRC_Abort;
105028 }
105029 if( pExpr->pRight ){
105030 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
105031 pExpr = pExpr->pRight;
105032 continue;
105033 }else if( ExprUseXSelect(pExpr) ){
105034 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
105035 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
105036 }else{
105037 if( pExpr->x.pList ){
105038 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
105039 }
105040#ifndef SQLITE_OMIT_WINDOWFUNC
105041 if( ExprHasProperty(pExpr, EP_WinFunc) ){
105042 if( walkWindowList(pWalker, pList: pExpr->y.pWin, bOneOnly: 1) ) return WRC_Abort;
105043 }
105044#endif
105045 }
105046 }
105047 break;
105048 }
105049 return WRC_Continue;
105050}
105051SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
105052 return pExpr ? sqlite3WalkExprNN(pWalker,pExpr) : WRC_Continue;
105053}
105054
105055/*
105056** Call sqlite3WalkExpr() for every expression in list p or until
105057** an abort request is seen.
105058*/
105059SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
105060 int i;
105061 struct ExprList_item *pItem;
105062 if( p ){
105063 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
105064 if( sqlite3WalkExpr(pWalker, pExpr: pItem->pExpr) ) return WRC_Abort;
105065 }
105066 }
105067 return WRC_Continue;
105068}
105069
105070/*
105071** This is a no-op callback for Walker->xSelectCallback2. If this
105072** callback is set, then the Select->pWinDefn list is traversed.
105073*/
105074SQLITE_PRIVATE void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){
105075 UNUSED_PARAMETER(pWalker);
105076 UNUSED_PARAMETER(p);
105077 /* No-op */
105078}
105079
105080/*
105081** Walk all expressions associated with SELECT statement p. Do
105082** not invoke the SELECT callback on p, but do (of course) invoke
105083** any expr callbacks and SELECT callbacks that come from subqueries.
105084** Return WRC_Abort or WRC_Continue.
105085*/
105086SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
105087 if( sqlite3WalkExprList(pWalker, p: p->pEList) ) return WRC_Abort;
105088 if( sqlite3WalkExpr(pWalker, pExpr: p->pWhere) ) return WRC_Abort;
105089 if( sqlite3WalkExprList(pWalker, p: p->pGroupBy) ) return WRC_Abort;
105090 if( sqlite3WalkExpr(pWalker, pExpr: p->pHaving) ) return WRC_Abort;
105091 if( sqlite3WalkExprList(pWalker, p: p->pOrderBy) ) return WRC_Abort;
105092 if( sqlite3WalkExpr(pWalker, pExpr: p->pLimit) ) return WRC_Abort;
105093#if !defined(SQLITE_OMIT_WINDOWFUNC)
105094 if( p->pWinDefn ){
105095 Parse *pParse;
105096 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
105097 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
105098#ifndef SQLITE_OMIT_CTE
105099 || pWalker->xSelectCallback2==sqlite3SelectPopWith
105100#endif
105101 ){
105102 /* The following may return WRC_Abort if there are unresolvable
105103 ** symbols (e.g. a table that does not exist) in a window definition. */
105104 int rc = walkWindowList(pWalker, pList: p->pWinDefn, bOneOnly: 0);
105105 return rc;
105106 }
105107 }
105108#endif
105109 return WRC_Continue;
105110}
105111
105112/*
105113** Walk the parse trees associated with all subqueries in the
105114** FROM clause of SELECT statement p. Do not invoke the select
105115** callback on p, but do invoke it on each FROM clause subquery
105116** and on any subqueries further down in the tree. Return
105117** WRC_Abort or WRC_Continue;
105118*/
105119SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
105120 SrcList *pSrc;
105121 int i;
105122 SrcItem *pItem;
105123
105124 pSrc = p->pSrc;
105125 if( ALWAYS(pSrc) ){
105126 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
105127 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
105128 return WRC_Abort;
105129 }
105130 if( pItem->fg.isTabFunc
105131 && sqlite3WalkExprList(pWalker, p: pItem->u1.pFuncArg)
105132 ){
105133 return WRC_Abort;
105134 }
105135 }
105136 }
105137 return WRC_Continue;
105138}
105139
105140/*
105141** Call sqlite3WalkExpr() for every expression in Select statement p.
105142** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
105143** on the compound select chain, p->pPrior.
105144**
105145** If it is not NULL, the xSelectCallback() callback is invoked before
105146** the walk of the expressions and FROM clause. The xSelectCallback2()
105147** method is invoked following the walk of the expressions and FROM clause,
105148** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
105149** and if the expressions and FROM clause both return WRC_Continue;
105150**
105151** Return WRC_Continue under normal conditions. Return WRC_Abort if
105152** there is an abort request.
105153**
105154** If the Walker does not have an xSelectCallback() then this routine
105155** is a no-op returning WRC_Continue.
105156*/
105157SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
105158 int rc;
105159 if( p==0 ) return WRC_Continue;
105160 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
105161 do{
105162 rc = pWalker->xSelectCallback(pWalker, p);
105163 if( rc ) return rc & WRC_Abort;
105164 if( sqlite3WalkSelectExpr(pWalker, p)
105165 || sqlite3WalkSelectFrom(pWalker, p)
105166 ){
105167 return WRC_Abort;
105168 }
105169 if( pWalker->xSelectCallback2 ){
105170 pWalker->xSelectCallback2(pWalker, p);
105171 }
105172 p = p->pPrior;
105173 }while( p!=0 );
105174 return WRC_Continue;
105175}
105176
105177/* Increase the walkerDepth when entering a subquery, and
105178** decrease when leaving the subquery.
105179*/
105180SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
105181 UNUSED_PARAMETER(pSelect);
105182 pWalker->walkerDepth++;
105183 return WRC_Continue;
105184}
105185SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
105186 UNUSED_PARAMETER(pSelect);
105187 pWalker->walkerDepth--;
105188}
105189
105190
105191/*
105192** No-op routine for the parse-tree walker.
105193**
105194** When this routine is the Walker.xExprCallback then expression trees
105195** are walked without any actions being taken at each node. Presumably,
105196** when this routine is used for Walker.xExprCallback then
105197** Walker.xSelectCallback is set to do something useful for every
105198** subquery in the parser tree.
105199*/
105200SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
105201 UNUSED_PARAMETER2(NotUsed, NotUsed2);
105202 return WRC_Continue;
105203}
105204
105205/*
105206** No-op routine for the parse-tree walker for SELECT statements.
105207** subquery in the parser tree.
105208*/
105209SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
105210 UNUSED_PARAMETER2(NotUsed, NotUsed2);
105211 return WRC_Continue;
105212}
105213
105214/************** End of walker.c **********************************************/
105215/************** Begin file resolve.c *****************************************/
105216/*
105217** 2008 August 18
105218**
105219** The author disclaims copyright to this source code. In place of
105220** a legal notice, here is a blessing:
105221**
105222** May you do good and not evil.
105223** May you find forgiveness for yourself and forgive others.
105224** May you share freely, never taking more than you give.
105225**
105226*************************************************************************
105227**
105228** This file contains routines used for walking the parser tree and
105229** resolve all identifiers by associating them with a particular
105230** table and column.
105231*/
105232/* #include "sqliteInt.h" */
105233
105234/*
105235** Magic table number to mean the EXCLUDED table in an UPSERT statement.
105236*/
105237#define EXCLUDED_TABLE_NUMBER 2
105238
105239/*
105240** Walk the expression tree pExpr and increase the aggregate function
105241** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
105242** This needs to occur when copying a TK_AGG_FUNCTION node from an
105243** outer query into an inner subquery.
105244**
105245** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
105246** is a helper function - a callback for the tree walker.
105247**
105248** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
105249*/
105250static int incrAggDepth(Walker *pWalker, Expr *pExpr){
105251 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
105252 return WRC_Continue;
105253}
105254static void incrAggFunctionDepth(Expr *pExpr, int N){
105255 if( N>0 ){
105256 Walker w;
105257 memset(s: &w, c: 0, n: sizeof(w));
105258 w.xExprCallback = incrAggDepth;
105259 w.u.n = N;
105260 sqlite3WalkExpr(pWalker: &w, pExpr);
105261 }
105262}
105263
105264/*
105265** Turn the pExpr expression into an alias for the iCol-th column of the
105266** result set in pEList.
105267**
105268** If the reference is followed by a COLLATE operator, then make sure
105269** the COLLATE operator is preserved. For example:
105270**
105271** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
105272**
105273** Should be transformed into:
105274**
105275** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
105276**
105277** The nSubquery parameter specifies how many levels of subquery the
105278** alias is removed from the original expression. The usual value is
105279** zero but it might be more if the alias is contained within a subquery
105280** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
105281** structures must be increased by the nSubquery amount.
105282*/
105283static void resolveAlias(
105284 Parse *pParse, /* Parsing context */
105285 ExprList *pEList, /* A result set */
105286 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
105287 Expr *pExpr, /* Transform this into an alias to the result set */
105288 int nSubquery /* Number of subqueries that the label is moving */
105289){
105290 Expr *pOrig; /* The iCol-th column of the result set */
105291 Expr *pDup; /* Copy of pOrig */
105292 sqlite3 *db; /* The database connection */
105293
105294 assert( iCol>=0 && iCol<pEList->nExpr );
105295 pOrig = pEList->a[iCol].pExpr;
105296 assert( pOrig!=0 );
105297 db = pParse->db;
105298 pDup = sqlite3ExprDup(db, pOrig, 0);
105299 if( db->mallocFailed ){
105300 sqlite3ExprDelete(db, pDup);
105301 pDup = 0;
105302 }else{
105303 Expr temp;
105304 incrAggFunctionDepth(pExpr: pDup, N: nSubquery);
105305 if( pExpr->op==TK_COLLATE ){
105306 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105307 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
105308 }
105309 memcpy(dest: &temp, src: pDup, n: sizeof(Expr));
105310 memcpy(dest: pDup, src: pExpr, n: sizeof(Expr));
105311 memcpy(dest: pExpr, src: &temp, n: sizeof(Expr));
105312 if( ExprHasProperty(pExpr, EP_WinFunc) ){
105313 if( ALWAYS(pExpr->y.pWin!=0) ){
105314 pExpr->y.pWin->pOwner = pExpr;
105315 }
105316 }
105317 sqlite3ExprDeferredDelete(pParse, pDup);
105318 }
105319}
105320
105321/*
105322** Subqueries stores the original database, table and column names for their
105323** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
105324** Check to see if the zSpan given to this routine matches the zDb, zTab,
105325** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
105326** match anything.
105327*/
105328SQLITE_PRIVATE int sqlite3MatchEName(
105329 const struct ExprList_item *pItem,
105330 const char *zCol,
105331 const char *zTab,
105332 const char *zDb
105333){
105334 int n;
105335 const char *zSpan;
105336 if( pItem->fg.eEName!=ENAME_TAB ) return 0;
105337 zSpan = pItem->zEName;
105338 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
105339 if( zDb && (sqlite3StrNICmp(zLeft: zSpan, zRight: zDb, N: n)!=0 || zDb[n]!=0) ){
105340 return 0;
105341 }
105342 zSpan += n+1;
105343 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
105344 if( zTab && (sqlite3StrNICmp(zLeft: zSpan, zRight: zTab, N: n)!=0 || zTab[n]!=0) ){
105345 return 0;
105346 }
105347 zSpan += n+1;
105348 if( zCol && sqlite3StrICmp(zLeft: zSpan, zRight: zCol)!=0 ){
105349 return 0;
105350 }
105351 return 1;
105352}
105353
105354/*
105355** Return TRUE if the double-quoted string mis-feature should be supported.
105356*/
105357static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
105358 if( db->init.busy ) return 1; /* Always support for legacy schemas */
105359 if( pTopNC->ncFlags & NC_IsDDL ){
105360 /* Currently parsing a DDL statement */
105361 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
105362 return 1;
105363 }
105364 return (db->flags & SQLITE_DqsDDL)!=0;
105365 }else{
105366 /* Currently parsing a DML statement */
105367 return (db->flags & SQLITE_DqsDML)!=0;
105368 }
105369}
105370
105371/*
105372** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
105373** return the appropriate colUsed mask.
105374*/
105375SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr *pExpr){
105376 int n;
105377 Table *pExTab;
105378
105379 n = pExpr->iColumn;
105380 assert( ExprUseYTab(pExpr) );
105381 pExTab = pExpr->y.pTab;
105382 assert( pExTab!=0 );
105383 if( (pExTab->tabFlags & TF_HasGenerated)!=0
105384 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
105385 ){
105386 testcase( pExTab->nCol==BMS-1 );
105387 testcase( pExTab->nCol==BMS );
105388 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
105389 }else{
105390 testcase( n==BMS-1 );
105391 testcase( n==BMS );
105392 if( n>=BMS ) n = BMS-1;
105393 return ((Bitmask)1)<<n;
105394 }
105395}
105396
105397/*
105398** Create a new expression term for the column specified by pMatch and
105399** iColumn. Append this new expression term to the FULL JOIN Match set
105400** in *ppList. Create a new *ppList if this is the first term in the
105401** set.
105402*/
105403static void extendFJMatch(
105404 Parse *pParse, /* Parsing context */
105405 ExprList **ppList, /* ExprList to extend */
105406 SrcItem *pMatch, /* Source table containing the column */
105407 i16 iColumn /* The column number */
105408){
105409 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
105410 if( pNew ){
105411 pNew->iTable = pMatch->iCursor;
105412 pNew->iColumn = iColumn;
105413 pNew->y.pTab = pMatch->pTab;
105414 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
105415 ExprSetProperty(pNew, EP_CanBeNull);
105416 *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew);
105417 }
105418}
105419
105420/*
105421** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
105422*/
105423static SQLITE_NOINLINE int isValidSchemaTableName(
105424 const char *zTab, /* Name as it appears in the SQL */
105425 Table *pTab, /* The schema table we are trying to match */
105426 Schema *pSchema /* non-NULL if a database qualifier is present */
105427){
105428 const char *zLegacy;
105429 assert( pTab!=0 );
105430 assert( pTab->tnum==1 );
105431 if( sqlite3StrNICmp(zLeft: zTab, zRight: "sqlite_", N: 7)!=0 ) return 0;
105432 zLegacy = pTab->zName;
105433 if( strcmp(s1: zLegacy+7, s2: &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){
105434 if( sqlite3StrICmp(zLeft: zTab+7, zRight: &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){
105435 return 1;
105436 }
105437 if( pSchema==0 ) return 0;
105438 if( sqlite3StrICmp(zLeft: zTab+7, zRight: &LEGACY_SCHEMA_TABLE[7])==0 ) return 1;
105439 if( sqlite3StrICmp(zLeft: zTab+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1;
105440 }else{
105441 if( sqlite3StrICmp(zLeft: zTab+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1;
105442 }
105443 return 0;
105444}
105445
105446/*
105447** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
105448** that name in the set of source tables in pSrcList and make the pExpr
105449** expression node refer back to that source column. The following changes
105450** are made to pExpr:
105451**
105452** pExpr->iDb Set the index in db->aDb[] of the database X
105453** (even if X is implied).
105454** pExpr->iTable Set to the cursor number for the table obtained
105455** from pSrcList.
105456** pExpr->y.pTab Points to the Table structure of X.Y (even if
105457** X and/or Y are implied.)
105458** pExpr->iColumn Set to the column number within the table.
105459** pExpr->op Set to TK_COLUMN.
105460** pExpr->pLeft Any expression this points to is deleted
105461** pExpr->pRight Any expression this points to is deleted.
105462**
105463** The zDb variable is the name of the database (the "X"). This value may be
105464** NULL meaning that name is of the form Y.Z or Z. Any available database
105465** can be used. The zTable variable is the name of the table (the "Y"). This
105466** value can be NULL if zDb is also NULL. If zTable is NULL it
105467** means that the form of the name is Z and that columns from any table
105468** can be used.
105469**
105470** If the name cannot be resolved unambiguously, leave an error message
105471** in pParse and return WRC_Abort. Return WRC_Prune on success.
105472*/
105473static int lookupName(
105474 Parse *pParse, /* The parsing context */
105475 const char *zDb, /* Name of the database containing table, or NULL */
105476 const char *zTab, /* Name of table containing column, or NULL */
105477 const char *zCol, /* Name of the column. */
105478 NameContext *pNC, /* The name context used to resolve the name */
105479 Expr *pExpr /* Make this EXPR node point to the selected column */
105480){
105481 int i, j; /* Loop counters */
105482 int cnt = 0; /* Number of matching column names */
105483 int cntTab = 0; /* Number of matching table names */
105484 int nSubquery = 0; /* How many levels of subquery */
105485 sqlite3 *db = pParse->db; /* The database connection */
105486 SrcItem *pItem; /* Use for looping over pSrcList items */
105487 SrcItem *pMatch = 0; /* The matching pSrcList item */
105488 NameContext *pTopNC = pNC; /* First namecontext in the list */
105489 Schema *pSchema = 0; /* Schema of the expression */
105490 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
105491 Table *pTab = 0; /* Table holding the row */
105492 Column *pCol; /* A column of pTab */
105493 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
105494
105495 assert( pNC ); /* the name context cannot be NULL. */
105496 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
105497 assert( zDb==0 || zTab!=0 );
105498 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
105499
105500 /* Initialize the node to no-match */
105501 pExpr->iTable = -1;
105502 ExprSetVVAProperty(pExpr, EP_NoReduce);
105503
105504 /* Translate the schema name in zDb into a pointer to the corresponding
105505 ** schema. If not found, pSchema will remain NULL and nothing will match
105506 ** resulting in an appropriate error message toward the end of this routine
105507 */
105508 if( zDb ){
105509 testcase( pNC->ncFlags & NC_PartIdx );
105510 testcase( pNC->ncFlags & NC_IsCheck );
105511 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
105512 /* Silently ignore database qualifiers inside CHECK constraints and
105513 ** partial indices. Do not raise errors because that might break
105514 ** legacy and because it does not hurt anything to just ignore the
105515 ** database name. */
105516 zDb = 0;
105517 }else{
105518 for(i=0; i<db->nDb; i++){
105519 assert( db->aDb[i].zDbSName );
105520 if( sqlite3StrICmp(zLeft: db->aDb[i].zDbSName,zRight: zDb)==0 ){
105521 pSchema = db->aDb[i].pSchema;
105522 break;
105523 }
105524 }
105525 if( i==db->nDb && sqlite3StrICmp(zLeft: "main", zRight: zDb)==0 ){
105526 /* This branch is taken when the main database has been renamed
105527 ** using SQLITE_DBCONFIG_MAINDBNAME. */
105528 pSchema = db->aDb[0].pSchema;
105529 zDb = db->aDb[0].zDbSName;
105530 }
105531 }
105532 }
105533
105534 /* Start at the inner-most context and move outward until a match is found */
105535 assert( pNC && cnt==0 );
105536 do{
105537 ExprList *pEList;
105538 SrcList *pSrcList = pNC->pSrcList;
105539
105540 if( pSrcList ){
105541 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
105542 u8 hCol;
105543 pTab = pItem->pTab;
105544 assert( pTab!=0 && pTab->zName!=0 );
105545 assert( pTab->nCol>0 || pParse->nErr );
105546 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
105547 if( pItem->fg.isNestedFrom ){
105548 /* In this case, pItem is a subquery that has been formed from a
105549 ** parenthesized subset of the FROM clause terms. Example:
105550 ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ...
105551 ** \_________________________/
105552 ** This pItem -------------^
105553 */
105554 int hit = 0;
105555 assert( pItem->pSelect!=0 );
105556 pEList = pItem->pSelect->pEList;
105557 assert( pEList!=0 );
105558 assert( pEList->nExpr==pTab->nCol );
105559 for(j=0; j<pEList->nExpr; j++){
105560 if( !sqlite3MatchEName(pItem: &pEList->a[j], zCol, zTab, zDb) ){
105561 continue;
105562 }
105563 if( cnt>0 ){
105564 if( pItem->fg.isUsing==0
105565 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
105566 ){
105567 /* Two or more tables have the same column name which is
105568 ** not joined by USING. This is an error. Signal as much
105569 ** by clearing pFJMatch and letting cnt go above 1. */
105570 sqlite3ExprListDelete(db, pFJMatch);
105571 pFJMatch = 0;
105572 }else
105573 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
105574 /* An INNER or LEFT JOIN. Use the left-most table */
105575 continue;
105576 }else
105577 if( (pItem->fg.jointype & JT_LEFT)==0 ){
105578 /* A RIGHT JOIN. Use the right-most table */
105579 cnt = 0;
105580 sqlite3ExprListDelete(db, pFJMatch);
105581 pFJMatch = 0;
105582 }else{
105583 /* For a FULL JOIN, we must construct a coalesce() func */
105584 extendFJMatch(pParse, ppList: &pFJMatch, pMatch, iColumn: pExpr->iColumn);
105585 }
105586 }
105587 cnt++;
105588 cntTab = 2;
105589 pMatch = pItem;
105590 pExpr->iColumn = j;
105591 pEList->a[j].fg.bUsed = 1;
105592 hit = 1;
105593 if( pEList->a[j].fg.bUsingTerm ) break;
105594 }
105595 if( hit || zTab==0 ) continue;
105596 }
105597 assert( zDb==0 || zTab!=0 );
105598 if( zTab ){
105599 if( zDb ){
105600 if( pTab->pSchema!=pSchema ) continue;
105601 if( pSchema==0 && strcmp(s1: zDb,s2: "*")!=0 ) continue;
105602 }
105603 if( pItem->zAlias!=0 ){
105604 if( sqlite3StrICmp(zLeft: zTab, zRight: pItem->zAlias)!=0 ){
105605 continue;
105606 }
105607 }else if( sqlite3StrICmp(zLeft: zTab, zRight: pTab->zName)!=0 ){
105608 if( pTab->tnum!=1 ) continue;
105609 if( !isValidSchemaTableName(zTab, pTab, pSchema) ) continue;
105610 }
105611 assert( ExprUseYTab(pExpr) );
105612 if( IN_RENAME_OBJECT && pItem->zAlias ){
105613 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)&pExpr->y.pTab);
105614 }
105615 }
105616 hCol = sqlite3StrIHash(z: zCol);
105617 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
105618 if( pCol->hName==hCol
105619 && sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zCol)==0
105620 ){
105621 if( cnt>0 ){
105622 if( pItem->fg.isUsing==0
105623 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
105624 ){
105625 /* Two or more tables have the same column name which is
105626 ** not joined by USING. This is an error. Signal as much
105627 ** by clearing pFJMatch and letting cnt go above 1. */
105628 sqlite3ExprListDelete(db, pFJMatch);
105629 pFJMatch = 0;
105630 }else
105631 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
105632 /* An INNER or LEFT JOIN. Use the left-most table */
105633 continue;
105634 }else
105635 if( (pItem->fg.jointype & JT_LEFT)==0 ){
105636 /* A RIGHT JOIN. Use the right-most table */
105637 cnt = 0;
105638 sqlite3ExprListDelete(db, pFJMatch);
105639 pFJMatch = 0;
105640 }else{
105641 /* For a FULL JOIN, we must construct a coalesce() func */
105642 extendFJMatch(pParse, ppList: &pFJMatch, pMatch, iColumn: pExpr->iColumn);
105643 }
105644 }
105645 cnt++;
105646 pMatch = pItem;
105647 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
105648 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
105649 if( pItem->fg.isNestedFrom ){
105650 sqlite3SrcItemColumnUsed(pItem, j);
105651 }
105652 break;
105653 }
105654 }
105655 if( 0==cnt && VisibleRowid(pTab) ){
105656 cntTab++;
105657 pMatch = pItem;
105658 }
105659 }
105660 if( pMatch ){
105661 pExpr->iTable = pMatch->iCursor;
105662 assert( ExprUseYTab(pExpr) );
105663 pExpr->y.pTab = pMatch->pTab;
105664 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
105665 ExprSetProperty(pExpr, EP_CanBeNull);
105666 }
105667 pSchema = pExpr->y.pTab->pSchema;
105668 }
105669 } /* if( pSrcList ) */
105670
105671#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
105672 /* If we have not already resolved the name, then maybe
105673 ** it is a new.* or old.* trigger argument reference. Or
105674 ** maybe it is an excluded.* from an upsert. Or maybe it is
105675 ** a reference in the RETURNING clause to a table being modified.
105676 */
105677 if( cnt==0 && zDb==0 ){
105678 pTab = 0;
105679#ifndef SQLITE_OMIT_TRIGGER
105680 if( pParse->pTriggerTab!=0 ){
105681 int op = pParse->eTriggerOp;
105682 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
105683 if( pParse->bReturning ){
105684 if( (pNC->ncFlags & NC_UBaseReg)!=0
105685 && ALWAYS(zTab==0
105686 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
105687 ){
105688 pExpr->iTable = op!=TK_DELETE;
105689 pTab = pParse->pTriggerTab;
105690 }
105691 }else if( op!=TK_DELETE && zTab && sqlite3StrICmp(zLeft: "new",zRight: zTab) == 0 ){
105692 pExpr->iTable = 1;
105693 pTab = pParse->pTriggerTab;
105694 }else if( op!=TK_INSERT && zTab && sqlite3StrICmp(zLeft: "old",zRight: zTab)==0 ){
105695 pExpr->iTable = 0;
105696 pTab = pParse->pTriggerTab;
105697 }
105698 }
105699#endif /* SQLITE_OMIT_TRIGGER */
105700#ifndef SQLITE_OMIT_UPSERT
105701 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
105702 Upsert *pUpsert = pNC->uNC.pUpsert;
105703 if( pUpsert && sqlite3StrICmp(zLeft: "excluded",zRight: zTab)==0 ){
105704 pTab = pUpsert->pUpsertSrc->a[0].pTab;
105705 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
105706 }
105707 }
105708#endif /* SQLITE_OMIT_UPSERT */
105709
105710 if( pTab ){
105711 int iCol;
105712 u8 hCol = sqlite3StrIHash(z: zCol);
105713 pSchema = pTab->pSchema;
105714 cntTab++;
105715 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
105716 if( pCol->hName==hCol
105717 && sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zCol)==0
105718 ){
105719 if( iCol==pTab->iPKey ){
105720 iCol = -1;
105721 }
105722 break;
105723 }
105724 }
105725 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
105726 /* IMP: R-51414-32910 */
105727 iCol = -1;
105728 }
105729 if( iCol<pTab->nCol ){
105730 cnt++;
105731 pMatch = 0;
105732#ifndef SQLITE_OMIT_UPSERT
105733 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
105734 testcase( iCol==(-1) );
105735 assert( ExprUseYTab(pExpr) );
105736 if( IN_RENAME_OBJECT ){
105737 pExpr->iColumn = iCol;
105738 pExpr->y.pTab = pTab;
105739 eNewExprOp = TK_COLUMN;
105740 }else{
105741 pExpr->iTable = pNC->uNC.pUpsert->regData +
105742 sqlite3TableColumnToStorage(pTab, iCol);
105743 eNewExprOp = TK_REGISTER;
105744 }
105745 }else
105746#endif /* SQLITE_OMIT_UPSERT */
105747 {
105748 assert( ExprUseYTab(pExpr) );
105749 pExpr->y.pTab = pTab;
105750 if( pParse->bReturning ){
105751 eNewExprOp = TK_REGISTER;
105752 pExpr->op2 = TK_COLUMN;
105753 pExpr->iColumn = iCol;
105754 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
105755 sqlite3TableColumnToStorage(pTab, iCol) + 1;
105756 }else{
105757 pExpr->iColumn = (i16)iCol;
105758 eNewExprOp = TK_TRIGGER;
105759#ifndef SQLITE_OMIT_TRIGGER
105760 if( iCol<0 ){
105761 pExpr->affExpr = SQLITE_AFF_INTEGER;
105762 }else if( pExpr->iTable==0 ){
105763 testcase( iCol==31 );
105764 testcase( iCol==32 );
105765 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
105766 }else{
105767 testcase( iCol==31 );
105768 testcase( iCol==32 );
105769 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
105770 }
105771#endif /* SQLITE_OMIT_TRIGGER */
105772 }
105773 }
105774 }
105775 }
105776 }
105777#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
105778
105779 /*
105780 ** Perhaps the name is a reference to the ROWID
105781 */
105782 if( cnt==0
105783 && cntTab==1
105784 && pMatch
105785 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
105786 && sqlite3IsRowid(zCol)
105787 && ALWAYS(VisibleRowid(pMatch->pTab))
105788 ){
105789 cnt = 1;
105790 pExpr->iColumn = -1;
105791 pExpr->affExpr = SQLITE_AFF_INTEGER;
105792 }
105793
105794 /*
105795 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
105796 ** might refer to an result-set alias. This happens, for example, when
105797 ** we are resolving names in the WHERE clause of the following command:
105798 **
105799 ** SELECT a+b AS x FROM table WHERE x<10;
105800 **
105801 ** In cases like this, replace pExpr with a copy of the expression that
105802 ** forms the result set entry ("a+b" in the example) and return immediately.
105803 ** Note that the expression in the result set should have already been
105804 ** resolved by the time the WHERE clause is resolved.
105805 **
105806 ** The ability to use an output result-set column in the WHERE, GROUP BY,
105807 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
105808 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
105809 ** is supported for backwards compatibility only. Hence, we issue a warning
105810 ** on sqlite3_log() whenever the capability is used.
105811 */
105812 if( cnt==0
105813 && (pNC->ncFlags & NC_UEList)!=0
105814 && zTab==0
105815 ){
105816 pEList = pNC->uNC.pEList;
105817 assert( pEList!=0 );
105818 for(j=0; j<pEList->nExpr; j++){
105819 char *zAs = pEList->a[j].zEName;
105820 if( pEList->a[j].fg.eEName==ENAME_NAME
105821 && sqlite3_stricmp(zLeft: zAs, zRight: zCol)==0
105822 ){
105823 Expr *pOrig;
105824 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
105825 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
105826 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
105827 pOrig = pEList->a[j].pExpr;
105828 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
105829 sqlite3ErrorMsg(pParse, zFormat: "misuse of aliased aggregate %s", zAs);
105830 return WRC_Abort;
105831 }
105832 if( ExprHasProperty(pOrig, EP_Win)
105833 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
105834 ){
105835 sqlite3ErrorMsg(pParse, zFormat: "misuse of aliased window function %s",zAs);
105836 return WRC_Abort;
105837 }
105838 if( sqlite3ExprVectorSize(pExpr: pOrig)!=1 ){
105839 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
105840 return WRC_Abort;
105841 }
105842 resolveAlias(pParse, pEList, iCol: j, pExpr, nSubquery);
105843 cnt = 1;
105844 pMatch = 0;
105845 assert( zTab==0 && zDb==0 );
105846 if( IN_RENAME_OBJECT ){
105847 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pExpr);
105848 }
105849 goto lookupname_end;
105850 }
105851 }
105852 }
105853
105854 /* Advance to the next name context. The loop will exit when either
105855 ** we have a match (cnt>0) or when we run out of name contexts.
105856 */
105857 if( cnt ) break;
105858 pNC = pNC->pNext;
105859 nSubquery++;
105860 }while( pNC );
105861
105862
105863 /*
105864 ** If X and Y are NULL (in other words if only the column name Z is
105865 ** supplied) and the value of Z is enclosed in double-quotes, then
105866 ** Z is a string literal if it doesn't match any column names. In that
105867 ** case, we need to return right away and not make any changes to
105868 ** pExpr.
105869 **
105870 ** Because no reference was made to outer contexts, the pNC->nRef
105871 ** fields are not changed in any context.
105872 */
105873 if( cnt==0 && zTab==0 ){
105874 assert( pExpr->op==TK_ID );
105875 if( ExprHasProperty(pExpr,EP_DblQuoted)
105876 && areDoubleQuotedStringsEnabled(db, pTopNC)
105877 ){
105878 /* If a double-quoted identifier does not match any known column name,
105879 ** then treat it as a string.
105880 **
105881 ** This hack was added in the early days of SQLite in a misguided attempt
105882 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
105883 ** I now sorely regret putting in this hack. The effect of this hack is
105884 ** that misspelled identifier names are silently converted into strings
105885 ** rather than causing an error, to the frustration of countless
105886 ** programmers. To all those frustrated programmers, my apologies.
105887 **
105888 ** Someday, I hope to get rid of this hack. Unfortunately there is
105889 ** a huge amount of legacy SQL that uses it. So for now, we just
105890 ** issue a warning.
105891 */
105892 sqlite3_log(SQLITE_WARNING,
105893 zFormat: "double-quoted string literal: \"%w\"", zCol);
105894#ifdef SQLITE_ENABLE_NORMALIZE
105895 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
105896#endif
105897 pExpr->op = TK_STRING;
105898 memset(s: &pExpr->y, c: 0, n: sizeof(pExpr->y));
105899 return WRC_Prune;
105900 }
105901 if( sqlite3ExprIdToTrueFalse(pExpr) ){
105902 return WRC_Prune;
105903 }
105904 }
105905
105906 /*
105907 ** cnt==0 means there was not match.
105908 ** cnt>1 means there were two or more matches.
105909 **
105910 ** cnt==0 is always an error. cnt>1 is often an error, but might
105911 ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
105912 */
105913 assert( pFJMatch==0 || cnt>0 );
105914 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );
105915 if( cnt!=1 ){
105916 const char *zErr;
105917 if( pFJMatch ){
105918 if( pFJMatch->nExpr==cnt-1 ){
105919 if( ExprHasProperty(pExpr,EP_Leaf) ){
105920 ExprClearProperty(pExpr,EP_Leaf);
105921 }else{
105922 sqlite3ExprDelete(db, pExpr->pLeft);
105923 pExpr->pLeft = 0;
105924 sqlite3ExprDelete(db, pExpr->pRight);
105925 pExpr->pRight = 0;
105926 }
105927 extendFJMatch(pParse, ppList: &pFJMatch, pMatch, iColumn: pExpr->iColumn);
105928 pExpr->op = TK_FUNCTION;
105929 pExpr->u.zToken = "coalesce";
105930 pExpr->x.pList = pFJMatch;
105931 cnt = 1;
105932 goto lookupname_end;
105933 }else{
105934 sqlite3ExprListDelete(db, pFJMatch);
105935 pFJMatch = 0;
105936 }
105937 }
105938 zErr = cnt==0 ? "no such column" : "ambiguous column name";
105939 if( zDb ){
105940 sqlite3ErrorMsg(pParse, zFormat: "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
105941 }else if( zTab ){
105942 sqlite3ErrorMsg(pParse, zFormat: "%s: %s.%s", zErr, zTab, zCol);
105943 }else{
105944 sqlite3ErrorMsg(pParse, zFormat: "%s: %s", zErr, zCol);
105945 }
105946 sqlite3RecordErrorOffsetOfExpr(db: pParse->db, pExpr);
105947 pParse->checkSchema = 1;
105948 pTopNC->nNcErr++;
105949 }
105950 assert( pFJMatch==0 );
105951
105952 /* Remove all substructure from pExpr */
105953 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
105954 sqlite3ExprDelete(db, pExpr->pLeft);
105955 pExpr->pLeft = 0;
105956 sqlite3ExprDelete(db, pExpr->pRight);
105957 pExpr->pRight = 0;
105958 ExprSetProperty(pExpr, EP_Leaf);
105959 }
105960
105961 /* If a column from a table in pSrcList is referenced, then record
105962 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
105963 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
105964 ** set if the 63rd or any subsequent column is used.
105965 **
105966 ** The colUsed mask is an optimization used to help determine if an
105967 ** index is a covering index. The correct answer is still obtained
105968 ** if the mask contains extra set bits. However, it is important to
105969 ** avoid setting bits beyond the maximum column number of the table.
105970 ** (See ticket [b92e5e8ec2cdbaa1]).
105971 **
105972 ** If a generated column is referenced, set bits for every column
105973 ** of the table.
105974 */
105975 if( pExpr->iColumn>=0 && pMatch!=0 ){
105976 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
105977 }
105978
105979 pExpr->op = eNewExprOp;
105980lookupname_end:
105981 if( cnt==1 ){
105982 assert( pNC!=0 );
105983#ifndef SQLITE_OMIT_AUTHORIZATION
105984 if( pParse->db->xAuth
105985 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
105986 ){
105987 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
105988 }
105989#endif
105990 /* Increment the nRef value on all name contexts from TopNC up to
105991 ** the point where the name matched. */
105992 for(;;){
105993 assert( pTopNC!=0 );
105994 pTopNC->nRef++;
105995 if( pTopNC==pNC ) break;
105996 pTopNC = pTopNC->pNext;
105997 }
105998 return WRC_Prune;
105999 } else {
106000 return WRC_Abort;
106001 }
106002}
106003
106004/*
106005** Allocate and return a pointer to an expression to load the column iCol
106006** from datasource iSrc in SrcList pSrc.
106007*/
106008SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
106009 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
106010 if( p ){
106011 SrcItem *pItem = &pSrc->a[iSrc];
106012 Table *pTab;
106013 assert( ExprUseYTab(p) );
106014 pTab = p->y.pTab = pItem->pTab;
106015 p->iTable = pItem->iCursor;
106016 if( p->y.pTab->iPKey==iCol ){
106017 p->iColumn = -1;
106018 }else{
106019 p->iColumn = (ynVar)iCol;
106020 if( (pTab->tabFlags & TF_HasGenerated)!=0
106021 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
106022 ){
106023 testcase( pTab->nCol==63 );
106024 testcase( pTab->nCol==64 );
106025 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
106026 }else{
106027 testcase( iCol==BMS );
106028 testcase( iCol==BMS-1 );
106029 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
106030 }
106031 }
106032 }
106033 return p;
106034}
106035
106036/*
106037** Report an error that an expression is not valid for some set of
106038** pNC->ncFlags values determined by validMask.
106039**
106040** static void notValid(
106041** Parse *pParse, // Leave error message here
106042** NameContext *pNC, // The name context
106043** const char *zMsg, // Type of error
106044** int validMask, // Set of contexts for which prohibited
106045** Expr *pExpr // Invalidate this expression on error
106046** ){...}
106047**
106048** As an optimization, since the conditional is almost always false
106049** (because errors are rare), the conditional is moved outside of the
106050** function call using a macro.
106051*/
106052static void notValidImpl(
106053 Parse *pParse, /* Leave error message here */
106054 NameContext *pNC, /* The name context */
106055 const char *zMsg, /* Type of error */
106056 Expr *pExpr, /* Invalidate this expression on error */
106057 Expr *pError /* Associate error with this expression */
106058){
106059 const char *zIn = "partial index WHERE clauses";
106060 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
106061#ifndef SQLITE_OMIT_CHECK
106062 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
106063#endif
106064#ifndef SQLITE_OMIT_GENERATED_COLUMNS
106065 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
106066#endif
106067 sqlite3ErrorMsg(pParse, zFormat: "%s prohibited in %s", zMsg, zIn);
106068 if( pExpr ) pExpr->op = TK_NULL;
106069 sqlite3RecordErrorOffsetOfExpr(db: pParse->db, pExpr: pError);
106070}
106071#define sqlite3ResolveNotValid(P,N,M,X,E,R) \
106072 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \
106073 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
106074
106075/*
106076** Expression p should encode a floating point value between 1.0 and 0.0.
106077** Return 1024 times this value. Or return -1 if p is not a floating point
106078** value between 1.0 and 0.0.
106079*/
106080static int exprProbability(Expr *p){
106081 double r = -1.0;
106082 if( p->op!=TK_FLOAT ) return -1;
106083 assert( !ExprHasProperty(p, EP_IntValue) );
106084 sqlite3AtoF(z: p->u.zToken, pResult: &r, length: sqlite3Strlen30(z: p->u.zToken), SQLITE_UTF8);
106085 assert( r>=0.0 );
106086 if( r>1.0 ) return -1;
106087 return (int)(r*134217728.0);
106088}
106089
106090/*
106091** This routine is callback for sqlite3WalkExpr().
106092**
106093** Resolve symbolic names into TK_COLUMN operators for the current
106094** node in the expression tree. Return 0 to continue the search down
106095** the tree or 2 to abort the tree walk.
106096**
106097** This routine also does error checking and name resolution for
106098** function names. The operator for aggregate functions is changed
106099** to TK_AGG_FUNCTION.
106100*/
106101static int resolveExprStep(Walker *pWalker, Expr *pExpr){
106102 NameContext *pNC;
106103 Parse *pParse;
106104
106105 pNC = pWalker->u.pNC;
106106 assert( pNC!=0 );
106107 pParse = pNC->pParse;
106108 assert( pParse==pWalker->pParse );
106109
106110#ifndef NDEBUG
106111 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
106112 SrcList *pSrcList = pNC->pSrcList;
106113 int i;
106114 for(i=0; i<pNC->pSrcList->nSrc; i++){
106115 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
106116 }
106117 }
106118#endif
106119 switch( pExpr->op ){
106120
106121 /* The special operator TK_ROW means use the rowid for the first
106122 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
106123 ** clause processing on UPDATE and DELETE statements, and by
106124 ** UPDATE ... FROM statement processing.
106125 */
106126 case TK_ROW: {
106127 SrcList *pSrcList = pNC->pSrcList;
106128 SrcItem *pItem;
106129 assert( pSrcList && pSrcList->nSrc>=1 );
106130 pItem = pSrcList->a;
106131 pExpr->op = TK_COLUMN;
106132 assert( ExprUseYTab(pExpr) );
106133 pExpr->y.pTab = pItem->pTab;
106134 pExpr->iTable = pItem->iCursor;
106135 pExpr->iColumn--;
106136 pExpr->affExpr = SQLITE_AFF_INTEGER;
106137 break;
106138 }
106139
106140 /* An optimization: Attempt to convert
106141 **
106142 ** "expr IS NOT NULL" --> "TRUE"
106143 ** "expr IS NULL" --> "FALSE"
106144 **
106145 ** if we can prove that "expr" is never NULL. Call this the
106146 ** "NOT NULL strength reduction optimization".
106147 **
106148 ** If this optimization occurs, also restore the NameContext ref-counts
106149 ** to the state they where in before the "column" LHS expression was
106150 ** resolved. This prevents "column" from being counted as having been
106151 ** referenced, which might prevent a SELECT from being erroneously
106152 ** marked as correlated.
106153 */
106154 case TK_NOTNULL:
106155 case TK_ISNULL: {
106156 int anRef[8];
106157 NameContext *p;
106158 int i;
106159 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
106160 anRef[i] = p->nRef;
106161 }
106162 sqlite3WalkExpr(pWalker, pExpr: pExpr->pLeft);
106163 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
106164 testcase( ExprHasProperty(pExpr, EP_OuterON) );
106165 assert( !ExprHasProperty(pExpr, EP_IntValue) );
106166 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
106167 pExpr->flags |= EP_IntValue;
106168 pExpr->op = TK_INTEGER;
106169
106170 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
106171 p->nRef = anRef[i];
106172 }
106173 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
106174 pExpr->pLeft = 0;
106175 }
106176 return WRC_Prune;
106177 }
106178
106179 /* A column name: ID
106180 ** Or table name and column name: ID.ID
106181 ** Or a database, table and column: ID.ID.ID
106182 **
106183 ** The TK_ID and TK_OUT cases are combined so that there will only
106184 ** be one call to lookupName(). Then the compiler will in-line
106185 ** lookupName() for a size reduction and performance increase.
106186 */
106187 case TK_ID:
106188 case TK_DOT: {
106189 const char *zColumn;
106190 const char *zTable;
106191 const char *zDb;
106192 Expr *pRight;
106193
106194 if( pExpr->op==TK_ID ){
106195 zDb = 0;
106196 zTable = 0;
106197 assert( !ExprHasProperty(pExpr, EP_IntValue) );
106198 zColumn = pExpr->u.zToken;
106199 }else{
106200 Expr *pLeft = pExpr->pLeft;
106201 testcase( pNC->ncFlags & NC_IdxExpr );
106202 testcase( pNC->ncFlags & NC_GenCol );
106203 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",
106204 NC_IdxExpr|NC_GenCol, 0, pExpr);
106205 pRight = pExpr->pRight;
106206 if( pRight->op==TK_ID ){
106207 zDb = 0;
106208 }else{
106209 assert( pRight->op==TK_DOT );
106210 assert( !ExprHasProperty(pRight, EP_IntValue) );
106211 zDb = pLeft->u.zToken;
106212 pLeft = pRight->pLeft;
106213 pRight = pRight->pRight;
106214 }
106215 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) );
106216 zTable = pLeft->u.zToken;
106217 zColumn = pRight->u.zToken;
106218 assert( ExprUseYTab(pExpr) );
106219 if( IN_RENAME_OBJECT ){
106220 sqlite3RenameTokenRemap(pParse, pTo: (void*)pExpr, pFrom: (void*)pRight);
106221 sqlite3RenameTokenRemap(pParse, pTo: (void*)&pExpr->y.pTab, pFrom: (void*)pLeft);
106222 }
106223 }
106224 return lookupName(pParse, zDb, zTab: zTable, zCol: zColumn, pNC, pExpr);
106225 }
106226
106227 /* Resolve function names
106228 */
106229 case TK_FUNCTION: {
106230 ExprList *pList = pExpr->x.pList; /* The argument list */
106231 int n = pList ? pList->nExpr : 0; /* Number of arguments */
106232 int no_such_func = 0; /* True if no such function exists */
106233 int wrong_num_args = 0; /* True if wrong number of arguments */
106234 int is_agg = 0; /* True if is an aggregate function */
106235 const char *zId; /* The function name. */
106236 FuncDef *pDef; /* Information about the function */
106237 u8 enc = ENC(pParse->db); /* The database encoding */
106238 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
106239#ifndef SQLITE_OMIT_WINDOWFUNC
106240 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
106241#endif
106242 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );
106243 zId = pExpr->u.zToken;
106244 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
106245 if( pDef==0 ){
106246 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
106247 if( pDef==0 ){
106248 no_such_func = 1;
106249 }else{
106250 wrong_num_args = 1;
106251 }
106252 }else{
106253 is_agg = pDef->xFinalize!=0;
106254 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
106255 ExprSetProperty(pExpr, EP_Unlikely);
106256 if( n==2 ){
106257 pExpr->iTable = exprProbability(p: pList->a[1].pExpr);
106258 if( pExpr->iTable<0 ){
106259 sqlite3ErrorMsg(pParse,
106260 zFormat: "second argument to %#T() must be a "
106261 "constant between 0.0 and 1.0", pExpr);
106262 pNC->nNcErr++;
106263 }
106264 }else{
106265 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
106266 ** equivalent to likelihood(X, 0.0625).
106267 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
106268 ** short-hand for likelihood(X,0.0625).
106269 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
106270 ** for likelihood(X,0.9375).
106271 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
106272 ** to likelihood(X,0.9375). */
106273 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
106274 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
106275 }
106276 }
106277#ifndef SQLITE_OMIT_AUTHORIZATION
106278 {
106279 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
106280 if( auth!=SQLITE_OK ){
106281 if( auth==SQLITE_DENY ){
106282 sqlite3ErrorMsg(pParse, zFormat: "not authorized to use function: %#T",
106283 pExpr);
106284 pNC->nNcErr++;
106285 }
106286 pExpr->op = TK_NULL;
106287 return WRC_Prune;
106288 }
106289 }
106290#endif
106291 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
106292 /* For the purposes of the EP_ConstFunc flag, date and time
106293 ** functions and other functions that change slowly are considered
106294 ** constant because they are constant for the duration of one query.
106295 ** This allows them to be factored out of inner loops. */
106296 ExprSetProperty(pExpr,EP_ConstFunc);
106297 }
106298 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
106299 /* Clearly non-deterministic functions like random(), but also
106300 ** date/time functions that use 'now', and other functions like
106301 ** sqlite_version() that might change over time cannot be used
106302 ** in an index or generated column. Curiously, they can be used
106303 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
106304 ** all this. */
106305 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
106306 NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr);
106307 }else{
106308 assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */
106309 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
106310 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
106311 }
106312 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
106313 && pParse->nested==0
106314 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
106315 ){
106316 /* Internal-use-only functions are disallowed unless the
106317 ** SQL is being compiled using sqlite3NestedParse() or
106318 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
106319 ** used to activate internal functions for testing purposes */
106320 no_such_func = 1;
106321 pDef = 0;
106322 }else
106323 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
106324 && !IN_RENAME_OBJECT
106325 ){
106326 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
106327 }
106328 }
106329
106330 if( 0==IN_RENAME_OBJECT ){
106331#ifndef SQLITE_OMIT_WINDOWFUNC
106332 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
106333 || (pDef->xValue==0 && pDef->xInverse==0)
106334 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
106335 );
106336 if( pDef && pDef->xValue==0 && pWin ){
106337 sqlite3ErrorMsg(pParse,
106338 zFormat: "%#T() may not be used as a window function", pExpr
106339 );
106340 pNC->nNcErr++;
106341 }else if(
106342 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
106343 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
106344 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
106345 ){
106346 const char *zType;
106347 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
106348 zType = "window";
106349 }else{
106350 zType = "aggregate";
106351 }
106352 sqlite3ErrorMsg(pParse, zFormat: "misuse of %s function %#T()",zType,pExpr);
106353 pNC->nNcErr++;
106354 is_agg = 0;
106355 }
106356#else
106357 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
106358 sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr);
106359 pNC->nNcErr++;
106360 is_agg = 0;
106361 }
106362#endif
106363 else if( no_such_func && pParse->db->init.busy==0
106364#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
106365 && pParse->explain==0
106366#endif
106367 ){
106368 sqlite3ErrorMsg(pParse, zFormat: "no such function: %#T", pExpr);
106369 pNC->nNcErr++;
106370 }else if( wrong_num_args ){
106371 sqlite3ErrorMsg(pParse,zFormat: "wrong number of arguments to function %#T()",
106372 pExpr);
106373 pNC->nNcErr++;
106374 }
106375#ifndef SQLITE_OMIT_WINDOWFUNC
106376 else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
106377 sqlite3ErrorMsg(pParse,
106378 zFormat: "FILTER may not be used with non-aggregate %#T()",
106379 pExpr
106380 );
106381 pNC->nNcErr++;
106382 }
106383#endif
106384 if( is_agg ){
106385 /* Window functions may not be arguments of aggregate functions.
106386 ** Or arguments of other window functions. But aggregate functions
106387 ** may be arguments for window functions. */
106388#ifndef SQLITE_OMIT_WINDOWFUNC
106389 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
106390#else
106391 pNC->ncFlags &= ~NC_AllowAgg;
106392#endif
106393 }
106394 }
106395#ifndef SQLITE_OMIT_WINDOWFUNC
106396 else if( ExprHasProperty(pExpr, EP_WinFunc) ){
106397 is_agg = 1;
106398 }
106399#endif
106400 sqlite3WalkExprList(pWalker, p: pList);
106401 if( is_agg ){
106402#ifndef SQLITE_OMIT_WINDOWFUNC
106403 if( pWin ){
106404 Select *pSel = pNC->pWinSelect;
106405 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
106406 if( IN_RENAME_OBJECT==0 ){
106407 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
106408 if( pParse->db->mallocFailed ) break;
106409 }
106410 sqlite3WalkExprList(pWalker, p: pWin->pPartition);
106411 sqlite3WalkExprList(pWalker, p: pWin->pOrderBy);
106412 sqlite3WalkExpr(pWalker, pExpr: pWin->pFilter);
106413 sqlite3WindowLink(pSel, pWin);
106414 pNC->ncFlags |= NC_HasWin;
106415 }else
106416#endif /* SQLITE_OMIT_WINDOWFUNC */
106417 {
106418 NameContext *pNC2; /* For looping up thru outer contexts */
106419 pExpr->op = TK_AGG_FUNCTION;
106420 pExpr->op2 = 0;
106421#ifndef SQLITE_OMIT_WINDOWFUNC
106422 if( ExprHasProperty(pExpr, EP_WinFunc) ){
106423 sqlite3WalkExpr(pWalker, pExpr: pExpr->y.pWin->pFilter);
106424 }
106425#endif
106426 pNC2 = pNC;
106427 while( pNC2
106428 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
106429 ){
106430 pExpr->op2++;
106431 pNC2 = pNC2->pNext;
106432 }
106433 assert( pDef!=0 || IN_RENAME_OBJECT );
106434 if( pNC2 && pDef ){
106435 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
106436 assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg );
106437 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
106438 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
106439 pNC2->ncFlags |= NC_HasAgg
106440 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
106441 & (SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER));
106442 }
106443 }
106444 pNC->ncFlags |= savedAllowFlags;
106445 }
106446 /* FIX ME: Compute pExpr->affinity based on the expected return
106447 ** type of the function
106448 */
106449 return WRC_Prune;
106450 }
106451#ifndef SQLITE_OMIT_SUBQUERY
106452 case TK_SELECT:
106453 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
106454#endif
106455 case TK_IN: {
106456 testcase( pExpr->op==TK_IN );
106457 if( ExprUseXSelect(pExpr) ){
106458 int nRef = pNC->nRef;
106459 testcase( pNC->ncFlags & NC_IsCheck );
106460 testcase( pNC->ncFlags & NC_PartIdx );
106461 testcase( pNC->ncFlags & NC_IdxExpr );
106462 testcase( pNC->ncFlags & NC_GenCol );
106463 if( pNC->ncFlags & NC_SelfRef ){
106464 notValidImpl(pParse, pNC, zMsg: "subqueries", pExpr, pError: pExpr);
106465 }else{
106466 sqlite3WalkSelect(pWalker, p: pExpr->x.pSelect);
106467 }
106468 assert( pNC->nRef>=nRef );
106469 if( nRef!=pNC->nRef ){
106470 ExprSetProperty(pExpr, EP_VarSelect);
106471 }
106472 pNC->ncFlags |= NC_Subquery;
106473 }
106474 break;
106475 }
106476 case TK_VARIABLE: {
106477 testcase( pNC->ncFlags & NC_IsCheck );
106478 testcase( pNC->ncFlags & NC_PartIdx );
106479 testcase( pNC->ncFlags & NC_IdxExpr );
106480 testcase( pNC->ncFlags & NC_GenCol );
106481 sqlite3ResolveNotValid(pParse, pNC, "parameters",
106482 NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr, pExpr);
106483 break;
106484 }
106485 case TK_IS:
106486 case TK_ISNOT: {
106487 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
106488 assert( !ExprHasProperty(pExpr, EP_Reduced) );
106489 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
106490 ** and "x IS NOT FALSE". */
106491 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
106492 int rc = resolveExprStep(pWalker, pExpr: pRight);
106493 if( rc==WRC_Abort ) return WRC_Abort;
106494 if( pRight->op==TK_TRUEFALSE ){
106495 pExpr->op2 = pExpr->op;
106496 pExpr->op = TK_TRUTH;
106497 return WRC_Continue;
106498 }
106499 }
106500 /* no break */ deliberate_fall_through
106501 }
106502 case TK_BETWEEN:
106503 case TK_EQ:
106504 case TK_NE:
106505 case TK_LT:
106506 case TK_LE:
106507 case TK_GT:
106508 case TK_GE: {
106509 int nLeft, nRight;
106510 if( pParse->db->mallocFailed ) break;
106511 assert( pExpr->pLeft!=0 );
106512 nLeft = sqlite3ExprVectorSize(pExpr: pExpr->pLeft);
106513 if( pExpr->op==TK_BETWEEN ){
106514 assert( ExprUseXList(pExpr) );
106515 nRight = sqlite3ExprVectorSize(pExpr: pExpr->x.pList->a[0].pExpr);
106516 if( nRight==nLeft ){
106517 nRight = sqlite3ExprVectorSize(pExpr: pExpr->x.pList->a[1].pExpr);
106518 }
106519 }else{
106520 assert( pExpr->pRight!=0 );
106521 nRight = sqlite3ExprVectorSize(pExpr: pExpr->pRight);
106522 }
106523 if( nLeft!=nRight ){
106524 testcase( pExpr->op==TK_EQ );
106525 testcase( pExpr->op==TK_NE );
106526 testcase( pExpr->op==TK_LT );
106527 testcase( pExpr->op==TK_LE );
106528 testcase( pExpr->op==TK_GT );
106529 testcase( pExpr->op==TK_GE );
106530 testcase( pExpr->op==TK_IS );
106531 testcase( pExpr->op==TK_ISNOT );
106532 testcase( pExpr->op==TK_BETWEEN );
106533 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
106534 sqlite3RecordErrorOffsetOfExpr(db: pParse->db, pExpr);
106535 }
106536 break;
106537 }
106538 }
106539 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
106540 return pParse->nErr ? WRC_Abort : WRC_Continue;
106541}
106542
106543/*
106544** pEList is a list of expressions which are really the result set of the
106545** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
106546** This routine checks to see if pE is a simple identifier which corresponds
106547** to the AS-name of one of the terms of the expression list. If it is,
106548** this routine return an integer between 1 and N where N is the number of
106549** elements in pEList, corresponding to the matching entry. If there is
106550** no match, or if pE is not a simple identifier, then this routine
106551** return 0.
106552**
106553** pEList has been resolved. pE has not.
106554*/
106555static int resolveAsName(
106556 Parse *pParse, /* Parsing context for error messages */
106557 ExprList *pEList, /* List of expressions to scan */
106558 Expr *pE /* Expression we are trying to match */
106559){
106560 int i; /* Loop counter */
106561
106562 UNUSED_PARAMETER(pParse);
106563
106564 if( pE->op==TK_ID ){
106565 const char *zCol;
106566 assert( !ExprHasProperty(pE, EP_IntValue) );
106567 zCol = pE->u.zToken;
106568 for(i=0; i<pEList->nExpr; i++){
106569 if( pEList->a[i].fg.eEName==ENAME_NAME
106570 && sqlite3_stricmp(zLeft: pEList->a[i].zEName, zRight: zCol)==0
106571 ){
106572 return i+1;
106573 }
106574 }
106575 }
106576 return 0;
106577}
106578
106579/*
106580** pE is a pointer to an expression which is a single term in the
106581** ORDER BY of a compound SELECT. The expression has not been
106582** name resolved.
106583**
106584** At the point this routine is called, we already know that the
106585** ORDER BY term is not an integer index into the result set. That
106586** case is handled by the calling routine.
106587**
106588** Attempt to match pE against result set columns in the left-most
106589** SELECT statement. Return the index i of the matching column,
106590** as an indication to the caller that it should sort by the i-th column.
106591** The left-most column is 1. In other words, the value returned is the
106592** same integer value that would be used in the SQL statement to indicate
106593** the column.
106594**
106595** If there is no match, return 0. Return -1 if an error occurs.
106596*/
106597static int resolveOrderByTermToExprList(
106598 Parse *pParse, /* Parsing context for error messages */
106599 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
106600 Expr *pE /* The specific ORDER BY term */
106601){
106602 int i; /* Loop counter */
106603 ExprList *pEList; /* The columns of the result set */
106604 NameContext nc; /* Name context for resolving pE */
106605 sqlite3 *db; /* Database connection */
106606 int rc; /* Return code from subprocedures */
106607 u8 savedSuppErr; /* Saved value of db->suppressErr */
106608
106609 assert( sqlite3ExprIsInteger(pE, &i)==0 );
106610 pEList = pSelect->pEList;
106611
106612 /* Resolve all names in the ORDER BY term expression
106613 */
106614 memset(s: &nc, c: 0, n: sizeof(nc));
106615 nc.pParse = pParse;
106616 nc.pSrcList = pSelect->pSrc;
106617 nc.uNC.pEList = pEList;
106618 nc.ncFlags = NC_AllowAgg|NC_UEList|NC_NoSelect;
106619 nc.nNcErr = 0;
106620 db = pParse->db;
106621 savedSuppErr = db->suppressErr;
106622 db->suppressErr = 1;
106623 rc = sqlite3ResolveExprNames(&nc, pE);
106624 db->suppressErr = savedSuppErr;
106625 if( rc ) return 0;
106626
106627 /* Try to match the ORDER BY expression against an expression
106628 ** in the result set. Return an 1-based index of the matching
106629 ** result-set entry.
106630 */
106631 for(i=0; i<pEList->nExpr; i++){
106632 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
106633 return i+1;
106634 }
106635 }
106636
106637 /* If no match, return 0. */
106638 return 0;
106639}
106640
106641/*
106642** Generate an ORDER BY or GROUP BY term out-of-range error.
106643*/
106644static void resolveOutOfRangeError(
106645 Parse *pParse, /* The error context into which to write the error */
106646 const char *zType, /* "ORDER" or "GROUP" */
106647 int i, /* The index (1-based) of the term out of range */
106648 int mx, /* Largest permissible value of i */
106649 Expr *pError /* Associate the error with the expression */
106650){
106651 sqlite3ErrorMsg(pParse,
106652 zFormat: "%r %s BY term out of range - should be "
106653 "between 1 and %d", i, zType, mx);
106654 sqlite3RecordErrorOffsetOfExpr(db: pParse->db, pExpr: pError);
106655}
106656
106657/*
106658** Analyze the ORDER BY clause in a compound SELECT statement. Modify
106659** each term of the ORDER BY clause is a constant integer between 1
106660** and N where N is the number of columns in the compound SELECT.
106661**
106662** ORDER BY terms that are already an integer between 1 and N are
106663** unmodified. ORDER BY terms that are integers outside the range of
106664** 1 through N generate an error. ORDER BY terms that are expressions
106665** are matched against result set expressions of compound SELECT
106666** beginning with the left-most SELECT and working toward the right.
106667** At the first match, the ORDER BY expression is transformed into
106668** the integer column number.
106669**
106670** Return the number of errors seen.
106671*/
106672static int resolveCompoundOrderBy(
106673 Parse *pParse, /* Parsing context. Leave error messages here */
106674 Select *pSelect /* The SELECT statement containing the ORDER BY */
106675){
106676 int i;
106677 ExprList *pOrderBy;
106678 ExprList *pEList;
106679 sqlite3 *db;
106680 int moreToDo = 1;
106681
106682 pOrderBy = pSelect->pOrderBy;
106683 if( pOrderBy==0 ) return 0;
106684 db = pParse->db;
106685 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
106686 sqlite3ErrorMsg(pParse, zFormat: "too many terms in ORDER BY clause");
106687 return 1;
106688 }
106689 for(i=0; i<pOrderBy->nExpr; i++){
106690 pOrderBy->a[i].fg.done = 0;
106691 }
106692 pSelect->pNext = 0;
106693 while( pSelect->pPrior ){
106694 pSelect->pPrior->pNext = pSelect;
106695 pSelect = pSelect->pPrior;
106696 }
106697 while( pSelect && moreToDo ){
106698 struct ExprList_item *pItem;
106699 moreToDo = 0;
106700 pEList = pSelect->pEList;
106701 assert( pEList!=0 );
106702 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
106703 int iCol = -1;
106704 Expr *pE, *pDup;
106705 if( pItem->fg.done ) continue;
106706 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
106707 if( NEVER(pE==0) ) continue;
106708 if( sqlite3ExprIsInteger(pE, &iCol) ){
106709 if( iCol<=0 || iCol>pEList->nExpr ){
106710 resolveOutOfRangeError(pParse, zType: "ORDER", i: i+1, mx: pEList->nExpr, pError: pE);
106711 return 1;
106712 }
106713 }else{
106714 iCol = resolveAsName(pParse, pEList, pE);
106715 if( iCol==0 ){
106716 /* Now test if expression pE matches one of the values returned
106717 ** by pSelect. In the usual case this is done by duplicating the
106718 ** expression, resolving any symbols in it, and then comparing
106719 ** it against each expression returned by the SELECT statement.
106720 ** Once the comparisons are finished, the duplicate expression
106721 ** is deleted.
106722 **
106723 ** If this is running as part of an ALTER TABLE operation and
106724 ** the symbols resolve successfully, also resolve the symbols in the
106725 ** actual expression. This allows the code in alter.c to modify
106726 ** column references within the ORDER BY expression as required. */
106727 pDup = sqlite3ExprDup(db, pE, 0);
106728 if( !db->mallocFailed ){
106729 assert(pDup);
106730 iCol = resolveOrderByTermToExprList(pParse, pSelect, pE: pDup);
106731 if( IN_RENAME_OBJECT && iCol>0 ){
106732 resolveOrderByTermToExprList(pParse, pSelect, pE);
106733 }
106734 }
106735 sqlite3ExprDelete(db, pDup);
106736 }
106737 }
106738 if( iCol>0 ){
106739 /* Convert the ORDER BY term into an integer column number iCol,
106740 ** taking care to preserve the COLLATE clause if it exists. */
106741 if( !IN_RENAME_OBJECT ){
106742 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
106743 if( pNew==0 ) return 1;
106744 pNew->flags |= EP_IntValue;
106745 pNew->u.iValue = iCol;
106746 if( pItem->pExpr==pE ){
106747 pItem->pExpr = pNew;
106748 }else{
106749 Expr *pParent = pItem->pExpr;
106750 assert( pParent->op==TK_COLLATE );
106751 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
106752 assert( pParent->pLeft==pE );
106753 pParent->pLeft = pNew;
106754 }
106755 sqlite3ExprDelete(db, pE);
106756 pItem->u.x.iOrderByCol = (u16)iCol;
106757 }
106758 pItem->fg.done = 1;
106759 }else{
106760 moreToDo = 1;
106761 }
106762 }
106763 pSelect = pSelect->pNext;
106764 }
106765 for(i=0; i<pOrderBy->nExpr; i++){
106766 if( pOrderBy->a[i].fg.done==0 ){
106767 sqlite3ErrorMsg(pParse, zFormat: "%r ORDER BY term does not match any "
106768 "column in the result set", i+1);
106769 return 1;
106770 }
106771 }
106772 return 0;
106773}
106774
106775/*
106776** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
106777** the SELECT statement pSelect. If any term is reference to a
106778** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
106779** field) then convert that term into a copy of the corresponding result set
106780** column.
106781**
106782** If any errors are detected, add an error message to pParse and
106783** return non-zero. Return zero if no errors are seen.
106784*/
106785SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
106786 Parse *pParse, /* Parsing context. Leave error messages here */
106787 Select *pSelect, /* The SELECT statement containing the clause */
106788 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
106789 const char *zType /* "ORDER" or "GROUP" */
106790){
106791 int i;
106792 sqlite3 *db = pParse->db;
106793 ExprList *pEList;
106794 struct ExprList_item *pItem;
106795
106796 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
106797 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
106798 sqlite3ErrorMsg(pParse, zFormat: "too many terms in %s BY clause", zType);
106799 return 1;
106800 }
106801 pEList = pSelect->pEList;
106802 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
106803 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
106804 if( pItem->u.x.iOrderByCol ){
106805 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
106806 resolveOutOfRangeError(pParse, zType, i: i+1, mx: pEList->nExpr, pError: 0);
106807 return 1;
106808 }
106809 resolveAlias(pParse, pEList, iCol: pItem->u.x.iOrderByCol-1, pExpr: pItem->pExpr,nSubquery: 0);
106810 }
106811 }
106812 return 0;
106813}
106814
106815#ifndef SQLITE_OMIT_WINDOWFUNC
106816/*
106817** Walker callback for windowRemoveExprFromSelect().
106818*/
106819static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
106820 UNUSED_PARAMETER(pWalker);
106821 if( ExprHasProperty(pExpr, EP_WinFunc) ){
106822 Window *pWin = pExpr->y.pWin;
106823 sqlite3WindowUnlinkFromSelect(pWin);
106824 }
106825 return WRC_Continue;
106826}
106827
106828/*
106829** Remove any Window objects owned by the expression pExpr from the
106830** Select.pWin list of Select object pSelect.
106831*/
106832static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
106833 if( pSelect->pWin ){
106834 Walker sWalker;
106835 memset(s: &sWalker, c: 0, n: sizeof(Walker));
106836 sWalker.xExprCallback = resolveRemoveWindowsCb;
106837 sWalker.u.pSelect = pSelect;
106838 sqlite3WalkExpr(pWalker: &sWalker, pExpr);
106839 }
106840}
106841#else
106842# define windowRemoveExprFromSelect(a, b)
106843#endif /* SQLITE_OMIT_WINDOWFUNC */
106844
106845/*
106846** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
106847** The Name context of the SELECT statement is pNC. zType is either
106848** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
106849**
106850** This routine resolves each term of the clause into an expression.
106851** If the order-by term is an integer I between 1 and N (where N is the
106852** number of columns in the result set of the SELECT) then the expression
106853** in the resolution is a copy of the I-th result-set expression. If
106854** the order-by term is an identifier that corresponds to the AS-name of
106855** a result-set expression, then the term resolves to a copy of the
106856** result-set expression. Otherwise, the expression is resolved in
106857** the usual way - using sqlite3ResolveExprNames().
106858**
106859** This routine returns the number of errors. If errors occur, then
106860** an appropriate error message might be left in pParse. (OOM errors
106861** excepted.)
106862*/
106863static int resolveOrderGroupBy(
106864 NameContext *pNC, /* The name context of the SELECT statement */
106865 Select *pSelect, /* The SELECT statement holding pOrderBy */
106866 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
106867 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
106868){
106869 int i, j; /* Loop counters */
106870 int iCol; /* Column number */
106871 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
106872 Parse *pParse; /* Parsing context */
106873 int nResult; /* Number of terms in the result set */
106874
106875 assert( pOrderBy!=0 );
106876 nResult = pSelect->pEList->nExpr;
106877 pParse = pNC->pParse;
106878 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
106879 Expr *pE = pItem->pExpr;
106880 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
106881 if( NEVER(pE2==0) ) continue;
106882 if( zType[0]!='G' ){
106883 iCol = resolveAsName(pParse, pEList: pSelect->pEList, pE: pE2);
106884 if( iCol>0 ){
106885 /* If an AS-name match is found, mark this ORDER BY column as being
106886 ** a copy of the iCol-th result-set column. The subsequent call to
106887 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
106888 ** copy of the iCol-th result-set expression. */
106889 pItem->u.x.iOrderByCol = (u16)iCol;
106890 continue;
106891 }
106892 }
106893 if( sqlite3ExprIsInteger(pE2, &iCol) ){
106894 /* The ORDER BY term is an integer constant. Again, set the column
106895 ** number so that sqlite3ResolveOrderGroupBy() will convert the
106896 ** order-by term to a copy of the result-set expression */
106897 if( iCol<1 || iCol>0xffff ){
106898 resolveOutOfRangeError(pParse, zType, i: i+1, mx: nResult, pError: pE2);
106899 return 1;
106900 }
106901 pItem->u.x.iOrderByCol = (u16)iCol;
106902 continue;
106903 }
106904
106905 /* Otherwise, treat the ORDER BY term as an ordinary expression */
106906 pItem->u.x.iOrderByCol = 0;
106907 if( sqlite3ResolveExprNames(pNC, pE) ){
106908 return 1;
106909 }
106910 for(j=0; j<pSelect->pEList->nExpr; j++){
106911 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
106912 /* Since this expression is being changed into a reference
106913 ** to an identical expression in the result set, remove all Window
106914 ** objects belonging to the expression from the Select.pWin list. */
106915 windowRemoveExprFromSelect(pSelect, pExpr: pE);
106916 pItem->u.x.iOrderByCol = j+1;
106917 }
106918 }
106919 }
106920 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
106921}
106922
106923/*
106924** Resolve names in the SELECT statement p and all of its descendants.
106925*/
106926static int resolveSelectStep(Walker *pWalker, Select *p){
106927 NameContext *pOuterNC; /* Context that contains this SELECT */
106928 NameContext sNC; /* Name context of this SELECT */
106929 int isCompound; /* True if p is a compound select */
106930 int nCompound; /* Number of compound terms processed so far */
106931 Parse *pParse; /* Parsing context */
106932 int i; /* Loop counter */
106933 ExprList *pGroupBy; /* The GROUP BY clause */
106934 Select *pLeftmost; /* Left-most of SELECT of a compound */
106935 sqlite3 *db; /* Database connection */
106936
106937
106938 assert( p!=0 );
106939 if( p->selFlags & SF_Resolved ){
106940 return WRC_Prune;
106941 }
106942 pOuterNC = pWalker->u.pNC;
106943 pParse = pWalker->pParse;
106944 db = pParse->db;
106945
106946 /* Normally sqlite3SelectExpand() will be called first and will have
106947 ** already expanded this SELECT. However, if this is a subquery within
106948 ** an expression, sqlite3ResolveExprNames() will be called without a
106949 ** prior call to sqlite3SelectExpand(). When that happens, let
106950 ** sqlite3SelectPrep() do all of the processing for this SELECT.
106951 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
106952 ** this routine in the correct order.
106953 */
106954 if( (p->selFlags & SF_Expanded)==0 ){
106955 sqlite3SelectPrep(pParse, p, pOuterNC);
106956 return pParse->nErr ? WRC_Abort : WRC_Prune;
106957 }
106958
106959 isCompound = p->pPrior!=0;
106960 nCompound = 0;
106961 pLeftmost = p;
106962 while( p ){
106963 assert( (p->selFlags & SF_Expanded)!=0 );
106964 assert( (p->selFlags & SF_Resolved)==0 );
106965 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
106966 p->selFlags |= SF_Resolved;
106967
106968
106969 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
106970 ** are not allowed to refer to any names, so pass an empty NameContext.
106971 */
106972 memset(s: &sNC, c: 0, n: sizeof(sNC));
106973 sNC.pParse = pParse;
106974 sNC.pWinSelect = p;
106975 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
106976 return WRC_Abort;
106977 }
106978
106979 /* If the SF_Converted flags is set, then this Select object was
106980 ** was created by the convertCompoundSelectToSubquery() function.
106981 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
106982 ** as if it were part of the sub-query, not the parent. This block
106983 ** moves the pOrderBy down to the sub-query. It will be moved back
106984 ** after the names have been resolved. */
106985 if( p->selFlags & SF_Converted ){
106986 Select *pSub = p->pSrc->a[0].pSelect;
106987 assert( p->pSrc->nSrc==1 && p->pOrderBy );
106988 assert( pSub->pPrior && pSub->pOrderBy==0 );
106989 pSub->pOrderBy = p->pOrderBy;
106990 p->pOrderBy = 0;
106991 }
106992
106993 /* Recursively resolve names in all subqueries in the FROM clause
106994 */
106995 for(i=0; i<p->pSrc->nSrc; i++){
106996 SrcItem *pItem = &p->pSrc->a[i];
106997 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
106998 int nRef = pOuterNC ? pOuterNC->nRef : 0;
106999 const char *zSavedContext = pParse->zAuthContext;
107000
107001 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
107002 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
107003 pParse->zAuthContext = zSavedContext;
107004 if( pParse->nErr ) return WRC_Abort;
107005 assert( db->mallocFailed==0 );
107006
107007 /* If the number of references to the outer context changed when
107008 ** expressions in the sub-select were resolved, the sub-select
107009 ** is correlated. It is not required to check the refcount on any
107010 ** but the innermost outer context object, as lookupName() increments
107011 ** the refcount on all contexts between the current one and the
107012 ** context containing the column when it resolves a name. */
107013 if( pOuterNC ){
107014 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
107015 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
107016 }
107017 }
107018 }
107019
107020 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
107021 ** resolve the result-set expression list.
107022 */
107023 sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
107024 sNC.pSrcList = p->pSrc;
107025 sNC.pNext = pOuterNC;
107026
107027 /* Resolve names in the result set. */
107028 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
107029 sNC.ncFlags &= ~NC_AllowWin;
107030
107031 /* If there are no aggregate functions in the result-set, and no GROUP BY
107032 ** expression, do not allow aggregates in any of the other expressions.
107033 */
107034 assert( (p->selFlags & SF_Aggregate)==0 );
107035 pGroupBy = p->pGroupBy;
107036 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
107037 assert( NC_MinMaxAgg==SF_MinMaxAgg );
107038 assert( NC_OrderAgg==SF_OrderByReqd );
107039 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
107040 }else{
107041 sNC.ncFlags &= ~NC_AllowAgg;
107042 }
107043
107044 /* Add the output column list to the name-context before parsing the
107045 ** other expressions in the SELECT statement. This is so that
107046 ** expressions in the WHERE clause (etc.) can refer to expressions by
107047 ** aliases in the result set.
107048 **
107049 ** Minor point: If this is the case, then the expression will be
107050 ** re-evaluated for each reference to it.
107051 */
107052 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 );
107053 sNC.uNC.pEList = p->pEList;
107054 sNC.ncFlags |= NC_UEList;
107055 if( p->pHaving ){
107056 if( (p->selFlags & SF_Aggregate)==0 ){
107057 sqlite3ErrorMsg(pParse, zFormat: "HAVING clause on a non-aggregate query");
107058 return WRC_Abort;
107059 }
107060 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
107061 }
107062 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
107063
107064 /* Resolve names in table-valued-function arguments */
107065 for(i=0; i<p->pSrc->nSrc; i++){
107066 SrcItem *pItem = &p->pSrc->a[i];
107067 if( pItem->fg.isTabFunc
107068 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
107069 ){
107070 return WRC_Abort;
107071 }
107072 }
107073
107074#ifndef SQLITE_OMIT_WINDOWFUNC
107075 if( IN_RENAME_OBJECT ){
107076 Window *pWin;
107077 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
107078 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
107079 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
107080 ){
107081 return WRC_Abort;
107082 }
107083 }
107084 }
107085#endif
107086
107087 /* The ORDER BY and GROUP BY clauses may not refer to terms in
107088 ** outer queries
107089 */
107090 sNC.pNext = 0;
107091 sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
107092
107093 /* If this is a converted compound query, move the ORDER BY clause from
107094 ** the sub-query back to the parent query. At this point each term
107095 ** within the ORDER BY clause has been transformed to an integer value.
107096 ** These integers will be replaced by copies of the corresponding result
107097 ** set expressions by the call to resolveOrderGroupBy() below. */
107098 if( p->selFlags & SF_Converted ){
107099 Select *pSub = p->pSrc->a[0].pSelect;
107100 p->pOrderBy = pSub->pOrderBy;
107101 pSub->pOrderBy = 0;
107102 }
107103
107104 /* Process the ORDER BY clause for singleton SELECT statements.
107105 ** The ORDER BY clause for compounds SELECT statements is handled
107106 ** below, after all of the result-sets for all of the elements of
107107 ** the compound have been resolved.
107108 **
107109 ** If there is an ORDER BY clause on a term of a compound-select other
107110 ** than the right-most term, then that is a syntax error. But the error
107111 ** is not detected until much later, and so we need to go ahead and
107112 ** resolve those symbols on the incorrect ORDER BY for consistency.
107113 */
107114 if( p->pOrderBy!=0
107115 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
107116 && resolveOrderGroupBy(pNC: &sNC, pSelect: p, pOrderBy: p->pOrderBy, zType: "ORDER")
107117 ){
107118 return WRC_Abort;
107119 }
107120 if( db->mallocFailed ){
107121 return WRC_Abort;
107122 }
107123 sNC.ncFlags &= ~NC_AllowWin;
107124
107125 /* Resolve the GROUP BY clause. At the same time, make sure
107126 ** the GROUP BY clause does not contain aggregate functions.
107127 */
107128 if( pGroupBy ){
107129 struct ExprList_item *pItem;
107130
107131 if( resolveOrderGroupBy(pNC: &sNC, pSelect: p, pOrderBy: pGroupBy, zType: "GROUP") || db->mallocFailed ){
107132 return WRC_Abort;
107133 }
107134 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
107135 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
107136 sqlite3ErrorMsg(pParse, zFormat: "aggregate functions are not allowed in "
107137 "the GROUP BY clause");
107138 return WRC_Abort;
107139 }
107140 }
107141 }
107142
107143 /* If this is part of a compound SELECT, check that it has the right
107144 ** number of expressions in the select list. */
107145 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
107146 sqlite3SelectWrongNumTermsError(pParse, p: p->pNext);
107147 return WRC_Abort;
107148 }
107149
107150 /* Advance to the next term of the compound
107151 */
107152 p = p->pPrior;
107153 nCompound++;
107154 }
107155
107156 /* Resolve the ORDER BY on a compound SELECT after all terms of
107157 ** the compound have been resolved.
107158 */
107159 if( isCompound && resolveCompoundOrderBy(pParse, pSelect: pLeftmost) ){
107160 return WRC_Abort;
107161 }
107162
107163 return WRC_Prune;
107164}
107165
107166/*
107167** This routine walks an expression tree and resolves references to
107168** table columns and result-set columns. At the same time, do error
107169** checking on function usage and set a flag if any aggregate functions
107170** are seen.
107171**
107172** To resolve table columns references we look for nodes (or subtrees) of the
107173** form X.Y.Z or Y.Z or just Z where
107174**
107175** X: The name of a database. Ex: "main" or "temp" or
107176** the symbolic name assigned to an ATTACH-ed database.
107177**
107178** Y: The name of a table in a FROM clause. Or in a trigger
107179** one of the special names "old" or "new".
107180**
107181** Z: The name of a column in table Y.
107182**
107183** The node at the root of the subtree is modified as follows:
107184**
107185** Expr.op Changed to TK_COLUMN
107186** Expr.pTab Points to the Table object for X.Y
107187** Expr.iColumn The column index in X.Y. -1 for the rowid.
107188** Expr.iTable The VDBE cursor number for X.Y
107189**
107190**
107191** To resolve result-set references, look for expression nodes of the
107192** form Z (with no X and Y prefix) where the Z matches the right-hand
107193** size of an AS clause in the result-set of a SELECT. The Z expression
107194** is replaced by a copy of the left-hand side of the result-set expression.
107195** Table-name and function resolution occurs on the substituted expression
107196** tree. For example, in:
107197**
107198** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
107199**
107200** The "x" term of the order by is replaced by "a+b" to render:
107201**
107202** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
107203**
107204** Function calls are checked to make sure that the function is
107205** defined and that the correct number of arguments are specified.
107206** If the function is an aggregate function, then the NC_HasAgg flag is
107207** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
107208** If an expression contains aggregate functions then the EP_Agg
107209** property on the expression is set.
107210**
107211** An error message is left in pParse if anything is amiss. The number
107212** if errors is returned.
107213*/
107214SQLITE_PRIVATE int sqlite3ResolveExprNames(
107215 NameContext *pNC, /* Namespace to resolve expressions in. */
107216 Expr *pExpr /* The expression to be analyzed. */
107217){
107218 int savedHasAgg;
107219 Walker w;
107220
107221 if( pExpr==0 ) return SQLITE_OK;
107222 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107223 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107224 w.pParse = pNC->pParse;
107225 w.xExprCallback = resolveExprStep;
107226 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
107227 w.xSelectCallback2 = 0;
107228 w.u.pNC = pNC;
107229#if SQLITE_MAX_EXPR_DEPTH>0
107230 w.pParse->nHeight += pExpr->nHeight;
107231 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
107232 return SQLITE_ERROR;
107233 }
107234#endif
107235 assert( pExpr!=0 );
107236 sqlite3WalkExprNN(pWalker: &w, pExpr);
107237#if SQLITE_MAX_EXPR_DEPTH>0
107238 w.pParse->nHeight -= pExpr->nHeight;
107239#endif
107240 assert( EP_Agg==NC_HasAgg );
107241 assert( EP_Win==NC_HasWin );
107242 testcase( pNC->ncFlags & NC_HasAgg );
107243 testcase( pNC->ncFlags & NC_HasWin );
107244 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
107245 pNC->ncFlags |= savedHasAgg;
107246 return pNC->nNcErr>0 || w.pParse->nErr>0;
107247}
107248
107249/*
107250** Resolve all names for all expression in an expression list. This is
107251** just like sqlite3ResolveExprNames() except that it works for an expression
107252** list rather than a single expression.
107253*/
107254SQLITE_PRIVATE int sqlite3ResolveExprListNames(
107255 NameContext *pNC, /* Namespace to resolve expressions in. */
107256 ExprList *pList /* The expression list to be analyzed. */
107257){
107258 int i;
107259 int savedHasAgg = 0;
107260 Walker w;
107261 if( pList==0 ) return WRC_Continue;
107262 w.pParse = pNC->pParse;
107263 w.xExprCallback = resolveExprStep;
107264 w.xSelectCallback = resolveSelectStep;
107265 w.xSelectCallback2 = 0;
107266 w.u.pNC = pNC;
107267 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107268 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107269 for(i=0; i<pList->nExpr; i++){
107270 Expr *pExpr = pList->a[i].pExpr;
107271 if( pExpr==0 ) continue;
107272#if SQLITE_MAX_EXPR_DEPTH>0
107273 w.pParse->nHeight += pExpr->nHeight;
107274 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
107275 return WRC_Abort;
107276 }
107277#endif
107278 sqlite3WalkExprNN(pWalker: &w, pExpr);
107279#if SQLITE_MAX_EXPR_DEPTH>0
107280 w.pParse->nHeight -= pExpr->nHeight;
107281#endif
107282 assert( EP_Agg==NC_HasAgg );
107283 assert( EP_Win==NC_HasWin );
107284 testcase( pNC->ncFlags & NC_HasAgg );
107285 testcase( pNC->ncFlags & NC_HasWin );
107286 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
107287 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
107288 savedHasAgg |= pNC->ncFlags &
107289 (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107290 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107291 }
107292 if( w.pParse->nErr>0 ) return WRC_Abort;
107293 }
107294 pNC->ncFlags |= savedHasAgg;
107295 return WRC_Continue;
107296}
107297
107298/*
107299** Resolve all names in all expressions of a SELECT and in all
107300** descendants of the SELECT, including compounds off of p->pPrior,
107301** subqueries in expressions, and subqueries used as FROM clause
107302** terms.
107303**
107304** See sqlite3ResolveExprNames() for a description of the kinds of
107305** transformations that occur.
107306**
107307** All SELECT statements should have been expanded using
107308** sqlite3SelectExpand() prior to invoking this routine.
107309*/
107310SQLITE_PRIVATE void sqlite3ResolveSelectNames(
107311 Parse *pParse, /* The parser context */
107312 Select *p, /* The SELECT statement being coded. */
107313 NameContext *pOuterNC /* Name context for parent SELECT statement */
107314){
107315 Walker w;
107316
107317 assert( p!=0 );
107318 w.xExprCallback = resolveExprStep;
107319 w.xSelectCallback = resolveSelectStep;
107320 w.xSelectCallback2 = 0;
107321 w.pParse = pParse;
107322 w.u.pNC = pOuterNC;
107323 sqlite3WalkSelect(pWalker: &w, p);
107324}
107325
107326/*
107327** Resolve names in expressions that can only reference a single table
107328** or which cannot reference any tables at all. Examples:
107329**
107330** "type" flag
107331** ------------
107332** (1) CHECK constraints NC_IsCheck
107333** (2) WHERE clauses on partial indices NC_PartIdx
107334** (3) Expressions in indexes on expressions NC_IdxExpr
107335** (4) Expression arguments to VACUUM INTO. 0
107336** (5) GENERATED ALWAYS as expressions NC_GenCol
107337**
107338** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
107339** nodes of the expression is set to -1 and the Expr.iColumn value is
107340** set to the column number. In case (4), TK_COLUMN nodes cause an error.
107341**
107342** Any errors cause an error message to be set in pParse.
107343*/
107344SQLITE_PRIVATE int sqlite3ResolveSelfReference(
107345 Parse *pParse, /* Parsing context */
107346 Table *pTab, /* The table being referenced, or NULL */
107347 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
107348 Expr *pExpr, /* Expression to resolve. May be NULL. */
107349 ExprList *pList /* Expression list to resolve. May be NULL. */
107350){
107351 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
107352 NameContext sNC; /* Name context for pParse->pNewTable */
107353 int rc;
107354
107355 assert( type==0 || pTab!=0 );
107356 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr
107357 || type==NC_GenCol || pTab==0 );
107358 memset(s: &sNC, c: 0, n: sizeof(sNC));
107359 memset(s: &sSrc, c: 0, n: sizeof(sSrc));
107360 if( pTab ){
107361 sSrc.nSrc = 1;
107362 sSrc.a[0].zName = pTab->zName;
107363 sSrc.a[0].pTab = pTab;
107364 sSrc.a[0].iCursor = -1;
107365 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
107366 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
107367 ** schema elements */
107368 type |= NC_FromDDL;
107369 }
107370 }
107371 sNC.pParse = pParse;
107372 sNC.pSrcList = &sSrc;
107373 sNC.ncFlags = type | NC_IsDDL;
107374 if( (rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr))!=SQLITE_OK ) return rc;
107375 if( pList ) rc = sqlite3ResolveExprListNames(pNC: &sNC, pList);
107376 return rc;
107377}
107378
107379/************** End of resolve.c *********************************************/
107380/************** Begin file expr.c ********************************************/
107381/*
107382** 2001 September 15
107383**
107384** The author disclaims copyright to this source code. In place of
107385** a legal notice, here is a blessing:
107386**
107387** May you do good and not evil.
107388** May you find forgiveness for yourself and forgive others.
107389** May you share freely, never taking more than you give.
107390**
107391*************************************************************************
107392** This file contains routines used for analyzing expressions and
107393** for generating VDBE code that evaluates expressions in SQLite.
107394*/
107395/* #include "sqliteInt.h" */
107396
107397/* Forward declarations */
107398static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
107399static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
107400
107401/*
107402** Return the affinity character for a single column of a table.
107403*/
107404SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table *pTab, int iCol){
107405 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
107406 return pTab->aCol[iCol].affinity;
107407}
107408
107409/*
107410** Return the 'affinity' of the expression pExpr if any.
107411**
107412** If pExpr is a column, a reference to a column via an 'AS' alias,
107413** or a sub-select with a column as the return value, then the
107414** affinity of that column is returned. Otherwise, 0x00 is returned,
107415** indicating no affinity for the expression.
107416**
107417** i.e. the WHERE clause expressions in the following statements all
107418** have an affinity:
107419**
107420** CREATE TABLE t1(a);
107421** SELECT * FROM t1 WHERE a;
107422** SELECT a AS b FROM t1 WHERE b;
107423** SELECT * FROM t1 WHERE (select a from t1);
107424*/
107425SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
107426 int op;
107427 op = pExpr->op;
107428 while( 1 /* exit-by-break */ ){
107429 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
107430 assert( ExprUseYTab(pExpr) );
107431 assert( pExpr->y.pTab!=0 );
107432 return sqlite3TableColumnAffinity(pTab: pExpr->y.pTab, iCol: pExpr->iColumn);
107433 }
107434 if( op==TK_SELECT ){
107435 assert( ExprUseXSelect(pExpr) );
107436 assert( pExpr->x.pSelect!=0 );
107437 assert( pExpr->x.pSelect->pEList!=0 );
107438 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
107439 return sqlite3ExprAffinity(pExpr: pExpr->x.pSelect->pEList->a[0].pExpr);
107440 }
107441#ifndef SQLITE_OMIT_CAST
107442 if( op==TK_CAST ){
107443 assert( !ExprHasProperty(pExpr, EP_IntValue) );
107444 return sqlite3AffinityType(pExpr->u.zToken, 0);
107445 }
107446#endif
107447 if( op==TK_SELECT_COLUMN ){
107448 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
107449 assert( pExpr->iColumn < pExpr->iTable );
107450 assert( pExpr->iColumn >= 0 );
107451 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
107452 return sqlite3ExprAffinity(
107453 pExpr: pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
107454 );
107455 }
107456 if( op==TK_VECTOR ){
107457 assert( ExprUseXList(pExpr) );
107458 return sqlite3ExprAffinity(pExpr: pExpr->x.pList->a[0].pExpr);
107459 }
107460 if( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow) ){
107461 assert( pExpr->op==TK_COLLATE
107462 || pExpr->op==TK_IF_NULL_ROW
107463 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
107464 pExpr = pExpr->pLeft;
107465 op = pExpr->op;
107466 continue;
107467 }
107468 if( op!=TK_REGISTER || (op = pExpr->op2)==TK_REGISTER ) break;
107469 }
107470 return pExpr->affExpr;
107471}
107472
107473/*
107474** Make a guess at all the possible datatypes of the result that could
107475** be returned by an expression. Return a bitmask indicating the answer:
107476**
107477** 0x01 Numeric
107478** 0x02 Text
107479** 0x04 Blob
107480**
107481** If the expression must return NULL, then 0x00 is returned.
107482*/
107483SQLITE_PRIVATE int sqlite3ExprDataType(const Expr *pExpr){
107484 while( pExpr ){
107485 switch( pExpr->op ){
107486 case TK_COLLATE:
107487 case TK_IF_NULL_ROW:
107488 case TK_UPLUS: {
107489 pExpr = pExpr->pLeft;
107490 break;
107491 }
107492 case TK_NULL: {
107493 pExpr = 0;
107494 break;
107495 }
107496 case TK_STRING: {
107497 return 0x02;
107498 }
107499 case TK_BLOB: {
107500 return 0x04;
107501 }
107502 case TK_CONCAT: {
107503 return 0x06;
107504 }
107505 case TK_VARIABLE:
107506 case TK_AGG_FUNCTION:
107507 case TK_FUNCTION: {
107508 return 0x07;
107509 }
107510 case TK_COLUMN:
107511 case TK_AGG_COLUMN:
107512 case TK_SELECT:
107513 case TK_CAST:
107514 case TK_SELECT_COLUMN:
107515 case TK_VECTOR: {
107516 int aff = sqlite3ExprAffinity(pExpr);
107517 if( aff>=SQLITE_AFF_NUMERIC ) return 0x05;
107518 if( aff==SQLITE_AFF_TEXT ) return 0x06;
107519 return 0x07;
107520 }
107521 case TK_CASE: {
107522 int res = 0;
107523 int ii;
107524 ExprList *pList = pExpr->x.pList;
107525 assert( ExprUseXList(pExpr) && pList!=0 );
107526 assert( pList->nExpr > 0);
107527 for(ii=1; ii<pList->nExpr; ii+=2){
107528 res |= sqlite3ExprDataType(pExpr: pList->a[ii].pExpr);
107529 }
107530 if( pList->nExpr % 2 ){
107531 res |= sqlite3ExprDataType(pExpr: pList->a[pList->nExpr-1].pExpr);
107532 }
107533 return res;
107534 }
107535 default: {
107536 return 0x01;
107537 }
107538 } /* End of switch(op) */
107539 } /* End of while(pExpr) */
107540 return 0x00;
107541}
107542
107543/*
107544** Set the collating sequence for expression pExpr to be the collating
107545** sequence named by pToken. Return a pointer to a new Expr node that
107546** implements the COLLATE operator.
107547**
107548** If a memory allocation error occurs, that fact is recorded in pParse->db
107549** and the pExpr parameter is returned unchanged.
107550*/
107551SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
107552 const Parse *pParse, /* Parsing context */
107553 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
107554 const Token *pCollName, /* Name of collating sequence */
107555 int dequote /* True to dequote pCollName */
107556){
107557 if( pCollName->n>0 ){
107558 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
107559 if( pNew ){
107560 pNew->pLeft = pExpr;
107561 pNew->flags |= EP_Collate|EP_Skip;
107562 pExpr = pNew;
107563 }
107564 }
107565 return pExpr;
107566}
107567SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(
107568 const Parse *pParse, /* Parsing context */
107569 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
107570 const char *zC /* The collating sequence name */
107571){
107572 Token s;
107573 assert( zC!=0 );
107574 sqlite3TokenInit(p: &s, z: (char*)zC);
107575 return sqlite3ExprAddCollateToken(pParse, pExpr, pCollName: &s, dequote: 0);
107576}
107577
107578/*
107579** Skip over any TK_COLLATE operators.
107580*/
107581SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
107582 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
107583 assert( pExpr->op==TK_COLLATE );
107584 pExpr = pExpr->pLeft;
107585 }
107586 return pExpr;
107587}
107588
107589/*
107590** Skip over any TK_COLLATE operators and/or any unlikely()
107591** or likelihood() or likely() functions at the root of an
107592** expression.
107593*/
107594SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
107595 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){
107596 if( ExprHasProperty(pExpr, EP_Unlikely) ){
107597 assert( ExprUseXList(pExpr) );
107598 assert( pExpr->x.pList->nExpr>0 );
107599 assert( pExpr->op==TK_FUNCTION );
107600 pExpr = pExpr->x.pList->a[0].pExpr;
107601 }else{
107602 assert( pExpr->op==TK_COLLATE );
107603 pExpr = pExpr->pLeft;
107604 }
107605 }
107606 return pExpr;
107607}
107608
107609/*
107610** Return the collation sequence for the expression pExpr. If
107611** there is no defined collating sequence, return NULL.
107612**
107613** See also: sqlite3ExprNNCollSeq()
107614**
107615** The sqlite3ExprNNCollSeq() works the same exact that it returns the
107616** default collation if pExpr has no defined collation.
107617**
107618** The collating sequence might be determined by a COLLATE operator
107619** or by the presence of a column with a defined collating sequence.
107620** COLLATE operators take first precedence. Left operands take
107621** precedence over right operands.
107622*/
107623SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
107624 sqlite3 *db = pParse->db;
107625 CollSeq *pColl = 0;
107626 const Expr *p = pExpr;
107627 while( p ){
107628 int op = p->op;
107629 if( op==TK_REGISTER ) op = p->op2;
107630 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
107631 || op==TK_COLUMN || op==TK_TRIGGER
107632 ){
107633 int j;
107634 assert( ExprUseYTab(p) );
107635 assert( p->y.pTab!=0 );
107636 if( (j = p->iColumn)>=0 ){
107637 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
107638 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
107639 }
107640 break;
107641 }
107642 if( op==TK_CAST || op==TK_UPLUS ){
107643 p = p->pLeft;
107644 continue;
107645 }
107646 if( op==TK_VECTOR ){
107647 assert( ExprUseXList(p) );
107648 p = p->x.pList->a[0].pExpr;
107649 continue;
107650 }
107651 if( op==TK_COLLATE ){
107652 assert( !ExprHasProperty(p, EP_IntValue) );
107653 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
107654 break;
107655 }
107656 if( p->flags & EP_Collate ){
107657 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
107658 p = p->pLeft;
107659 }else{
107660 Expr *pNext = p->pRight;
107661 /* The Expr.x union is never used at the same time as Expr.pRight */
107662 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 );
107663 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){
107664 int i;
107665 for(i=0; i<p->x.pList->nExpr; i++){
107666 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
107667 pNext = p->x.pList->a[i].pExpr;
107668 break;
107669 }
107670 }
107671 }
107672 p = pNext;
107673 }
107674 }else{
107675 break;
107676 }
107677 }
107678 if( sqlite3CheckCollSeq(pParse, pColl) ){
107679 pColl = 0;
107680 }
107681 return pColl;
107682}
107683
107684/*
107685** Return the collation sequence for the expression pExpr. If
107686** there is no defined collating sequence, return a pointer to the
107687** default collation sequence.
107688**
107689** See also: sqlite3ExprCollSeq()
107690**
107691** The sqlite3ExprCollSeq() routine works the same except that it
107692** returns NULL if there is no defined collation.
107693*/
107694SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
107695 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
107696 if( p==0 ) p = pParse->db->pDfltColl;
107697 assert( p!=0 );
107698 return p;
107699}
107700
107701/*
107702** Return TRUE if the two expressions have equivalent collating sequences.
107703*/
107704SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
107705 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pExpr: pE1);
107706 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pExpr: pE2);
107707 return sqlite3StrICmp(zLeft: pColl1->zName, zRight: pColl2->zName)==0;
107708}
107709
107710/*
107711** pExpr is an operand of a comparison operator. aff2 is the
107712** type affinity of the other operand. This routine returns the
107713** type affinity that should be used for the comparison operator.
107714*/
107715SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
107716 char aff1 = sqlite3ExprAffinity(pExpr);
107717 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
107718 /* Both sides of the comparison are columns. If one has numeric
107719 ** affinity, use that. Otherwise use no affinity.
107720 */
107721 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
107722 return SQLITE_AFF_NUMERIC;
107723 }else{
107724 return SQLITE_AFF_BLOB;
107725 }
107726 }else{
107727 /* One side is a column, the other is not. Use the columns affinity. */
107728 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
107729 return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
107730 }
107731}
107732
107733/*
107734** pExpr is a comparison operator. Return the type affinity that should
107735** be applied to both operands prior to doing the comparison.
107736*/
107737static char comparisonAffinity(const Expr *pExpr){
107738 char aff;
107739 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
107740 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
107741 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
107742 assert( pExpr->pLeft );
107743 aff = sqlite3ExprAffinity(pExpr: pExpr->pLeft);
107744 if( pExpr->pRight ){
107745 aff = sqlite3CompareAffinity(pExpr: pExpr->pRight, aff2: aff);
107746 }else if( ExprUseXSelect(pExpr) ){
107747 aff = sqlite3CompareAffinity(pExpr: pExpr->x.pSelect->pEList->a[0].pExpr, aff2: aff);
107748 }else if( aff==0 ){
107749 aff = SQLITE_AFF_BLOB;
107750 }
107751 return aff;
107752}
107753
107754/*
107755** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
107756** idx_affinity is the affinity of an indexed column. Return true
107757** if the index with affinity idx_affinity may be used to implement
107758** the comparison in pExpr.
107759*/
107760SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
107761 char aff = comparisonAffinity(pExpr);
107762 if( aff<SQLITE_AFF_TEXT ){
107763 return 1;
107764 }
107765 if( aff==SQLITE_AFF_TEXT ){
107766 return idx_affinity==SQLITE_AFF_TEXT;
107767 }
107768 return sqlite3IsNumericAffinity(idx_affinity);
107769}
107770
107771/*
107772** Return the P5 value that should be used for a binary comparison
107773** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
107774*/
107775static u8 binaryCompareP5(
107776 const Expr *pExpr1, /* Left operand */
107777 const Expr *pExpr2, /* Right operand */
107778 int jumpIfNull /* Extra flags added to P5 */
107779){
107780 u8 aff = (char)sqlite3ExprAffinity(pExpr: pExpr2);
107781 aff = (u8)sqlite3CompareAffinity(pExpr: pExpr1, aff2: aff) | (u8)jumpIfNull;
107782 return aff;
107783}
107784
107785/*
107786** Return a pointer to the collation sequence that should be used by
107787** a binary comparison operator comparing pLeft and pRight.
107788**
107789** If the left hand expression has a collating sequence type, then it is
107790** used. Otherwise the collation sequence for the right hand expression
107791** is used, or the default (BINARY) if neither expression has a collating
107792** type.
107793**
107794** Argument pRight (but not pLeft) may be a null pointer. In this case,
107795** it is not considered.
107796*/
107797SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
107798 Parse *pParse,
107799 const Expr *pLeft,
107800 const Expr *pRight
107801){
107802 CollSeq *pColl;
107803 assert( pLeft );
107804 if( pLeft->flags & EP_Collate ){
107805 pColl = sqlite3ExprCollSeq(pParse, pExpr: pLeft);
107806 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
107807 pColl = sqlite3ExprCollSeq(pParse, pExpr: pRight);
107808 }else{
107809 pColl = sqlite3ExprCollSeq(pParse, pExpr: pLeft);
107810 if( !pColl ){
107811 pColl = sqlite3ExprCollSeq(pParse, pExpr: pRight);
107812 }
107813 }
107814 return pColl;
107815}
107816
107817/* Expression p is a comparison operator. Return a collation sequence
107818** appropriate for the comparison operator.
107819**
107820** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
107821** However, if the OP_Commuted flag is set, then the order of the operands
107822** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
107823** correct collating sequence is found.
107824*/
107825SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
107826 if( ExprHasProperty(p, EP_Commuted) ){
107827 return sqlite3BinaryCompareCollSeq(pParse, pLeft: p->pRight, pRight: p->pLeft);
107828 }else{
107829 return sqlite3BinaryCompareCollSeq(pParse, pLeft: p->pLeft, pRight: p->pRight);
107830 }
107831}
107832
107833/*
107834** Generate code for a comparison operator.
107835*/
107836static int codeCompare(
107837 Parse *pParse, /* The parsing (and code generating) context */
107838 Expr *pLeft, /* The left operand */
107839 Expr *pRight, /* The right operand */
107840 int opcode, /* The comparison opcode */
107841 int in1, int in2, /* Register holding operands */
107842 int dest, /* Jump here if true. */
107843 int jumpIfNull, /* If true, jump if either operand is NULL */
107844 int isCommuted /* The comparison has been commuted */
107845){
107846 int p5;
107847 int addr;
107848 CollSeq *p4;
107849
107850 if( pParse->nErr ) return 0;
107851 if( isCommuted ){
107852 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft: pRight, pRight: pLeft);
107853 }else{
107854 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
107855 }
107856 p5 = binaryCompareP5(pExpr1: pLeft, pExpr2: pRight, jumpIfNull);
107857 addr = sqlite3VdbeAddOp4(p: pParse->pVdbe, op: opcode, p1: in2, p2: dest, p3: in1,
107858 zP4: (void*)p4, P4_COLLSEQ);
107859 sqlite3VdbeChangeP5(p: pParse->pVdbe, p5: (u8)p5);
107860 return addr;
107861}
107862
107863/*
107864** Return true if expression pExpr is a vector, or false otherwise.
107865**
107866** A vector is defined as any expression that results in two or more
107867** columns of result. Every TK_VECTOR node is an vector because the
107868** parser will not generate a TK_VECTOR with fewer than two entries.
107869** But a TK_SELECT might be either a vector or a scalar. It is only
107870** considered a vector if it has two or more result columns.
107871*/
107872SQLITE_PRIVATE int sqlite3ExprIsVector(const Expr *pExpr){
107873 return sqlite3ExprVectorSize(pExpr)>1;
107874}
107875
107876/*
107877** If the expression passed as the only argument is of type TK_VECTOR
107878** return the number of expressions in the vector. Or, if the expression
107879** is a sub-select, return the number of columns in the sub-select. For
107880** any other type of expression, return 1.
107881*/
107882SQLITE_PRIVATE int sqlite3ExprVectorSize(const Expr *pExpr){
107883 u8 op = pExpr->op;
107884 if( op==TK_REGISTER ) op = pExpr->op2;
107885 if( op==TK_VECTOR ){
107886 assert( ExprUseXList(pExpr) );
107887 return pExpr->x.pList->nExpr;
107888 }else if( op==TK_SELECT ){
107889 assert( ExprUseXSelect(pExpr) );
107890 return pExpr->x.pSelect->pEList->nExpr;
107891 }else{
107892 return 1;
107893 }
107894}
107895
107896/*
107897** Return a pointer to a subexpression of pVector that is the i-th
107898** column of the vector (numbered starting with 0). The caller must
107899** ensure that i is within range.
107900**
107901** If pVector is really a scalar (and "scalar" here includes subqueries
107902** that return a single column!) then return pVector unmodified.
107903**
107904** pVector retains ownership of the returned subexpression.
107905**
107906** If the vector is a (SELECT ...) then the expression returned is
107907** just the expression for the i-th term of the result set, and may
107908** not be ready for evaluation because the table cursor has not yet
107909** been positioned.
107910*/
107911SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
107912 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
107913 if( sqlite3ExprIsVector(pExpr: pVector) ){
107914 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
107915 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
107916 assert( ExprUseXSelect(pVector) );
107917 return pVector->x.pSelect->pEList->a[i].pExpr;
107918 }else{
107919 assert( ExprUseXList(pVector) );
107920 return pVector->x.pList->a[i].pExpr;
107921 }
107922 }
107923 return pVector;
107924}
107925
107926/*
107927** Compute and return a new Expr object which when passed to
107928** sqlite3ExprCode() will generate all necessary code to compute
107929** the iField-th column of the vector expression pVector.
107930**
107931** It is ok for pVector to be a scalar (as long as iField==0).
107932** In that case, this routine works like sqlite3ExprDup().
107933**
107934** The caller owns the returned Expr object and is responsible for
107935** ensuring that the returned value eventually gets freed.
107936**
107937** The caller retains ownership of pVector. If pVector is a TK_SELECT,
107938** then the returned object will reference pVector and so pVector must remain
107939** valid for the life of the returned object. If pVector is a TK_VECTOR
107940** or a scalar expression, then it can be deleted as soon as this routine
107941** returns.
107942**
107943** A trick to cause a TK_SELECT pVector to be deleted together with
107944** the returned Expr object is to attach the pVector to the pRight field
107945** of the returned TK_SELECT_COLUMN Expr object.
107946*/
107947SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
107948 Parse *pParse, /* Parsing context */
107949 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
107950 int iField, /* Which column of the vector to return */
107951 int nField /* Total number of columns in the vector */
107952){
107953 Expr *pRet;
107954 if( pVector->op==TK_SELECT ){
107955 assert( ExprUseXSelect(pVector) );
107956 /* The TK_SELECT_COLUMN Expr node:
107957 **
107958 ** pLeft: pVector containing TK_SELECT. Not deleted.
107959 ** pRight: not used. But recursively deleted.
107960 ** iColumn: Index of a column in pVector
107961 ** iTable: 0 or the number of columns on the LHS of an assignment
107962 ** pLeft->iTable: First in an array of register holding result, or 0
107963 ** if the result is not yet computed.
107964 **
107965 ** sqlite3ExprDelete() specifically skips the recursive delete of
107966 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
107967 ** can be attached to pRight to cause this node to take ownership of
107968 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
107969 ** with the same pLeft pointer to the pVector, but only one of them
107970 ** will own the pVector.
107971 */
107972 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
107973 if( pRet ){
107974 pRet->iTable = nField;
107975 pRet->iColumn = iField;
107976 pRet->pLeft = pVector;
107977 }
107978 }else{
107979 if( pVector->op==TK_VECTOR ){
107980 Expr **ppVector;
107981 assert( ExprUseXList(pVector) );
107982 ppVector = &pVector->x.pList->a[iField].pExpr;
107983 pVector = *ppVector;
107984 if( IN_RENAME_OBJECT ){
107985 /* This must be a vector UPDATE inside a trigger */
107986 *ppVector = 0;
107987 return pVector;
107988 }
107989 }
107990 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
107991 }
107992 return pRet;
107993}
107994
107995/*
107996** If expression pExpr is of type TK_SELECT, generate code to evaluate
107997** it. Return the register in which the result is stored (or, if the
107998** sub-select returns more than one column, the first in an array
107999** of registers in which the result is stored).
108000**
108001** If pExpr is not a TK_SELECT expression, return 0.
108002*/
108003static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
108004 int reg = 0;
108005#ifndef SQLITE_OMIT_SUBQUERY
108006 if( pExpr->op==TK_SELECT ){
108007 reg = sqlite3CodeSubselect(pParse, pExpr);
108008 }
108009#endif
108010 return reg;
108011}
108012
108013/*
108014** Argument pVector points to a vector expression - either a TK_VECTOR
108015** or TK_SELECT that returns more than one column. This function returns
108016** the register number of a register that contains the value of
108017** element iField of the vector.
108018**
108019** If pVector is a TK_SELECT expression, then code for it must have
108020** already been generated using the exprCodeSubselect() routine. In this
108021** case parameter regSelect should be the first in an array of registers
108022** containing the results of the sub-select.
108023**
108024** If pVector is of type TK_VECTOR, then code for the requested field
108025** is generated. In this case (*pRegFree) may be set to the number of
108026** a temporary register to be freed by the caller before returning.
108027**
108028** Before returning, output parameter (*ppExpr) is set to point to the
108029** Expr object corresponding to element iElem of the vector.
108030*/
108031static int exprVectorRegister(
108032 Parse *pParse, /* Parse context */
108033 Expr *pVector, /* Vector to extract element from */
108034 int iField, /* Field to extract from pVector */
108035 int regSelect, /* First in array of registers */
108036 Expr **ppExpr, /* OUT: Expression element */
108037 int *pRegFree /* OUT: Temp register to free */
108038){
108039 u8 op = pVector->op;
108040 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR );
108041 if( op==TK_REGISTER ){
108042 *ppExpr = sqlite3VectorFieldSubexpr(pVector, i: iField);
108043 return pVector->iTable+iField;
108044 }
108045 if( op==TK_SELECT ){
108046 assert( ExprUseXSelect(pVector) );
108047 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
108048 return regSelect+iField;
108049 }
108050 if( op==TK_VECTOR ){
108051 assert( ExprUseXList(pVector) );
108052 *ppExpr = pVector->x.pList->a[iField].pExpr;
108053 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
108054 }
108055 return 0;
108056}
108057
108058/*
108059** Expression pExpr is a comparison between two vector values. Compute
108060** the result of the comparison (1, 0, or NULL) and write that
108061** result into register dest.
108062**
108063** The caller must satisfy the following preconditions:
108064**
108065** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
108066** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
108067** otherwise: op==pExpr->op and p5==0
108068*/
108069static void codeVectorCompare(
108070 Parse *pParse, /* Code generator context */
108071 Expr *pExpr, /* The comparison operation */
108072 int dest, /* Write results into this register */
108073 u8 op, /* Comparison operator */
108074 u8 p5 /* SQLITE_NULLEQ or zero */
108075){
108076 Vdbe *v = pParse->pVdbe;
108077 Expr *pLeft = pExpr->pLeft;
108078 Expr *pRight = pExpr->pRight;
108079 int nLeft = sqlite3ExprVectorSize(pExpr: pLeft);
108080 int i;
108081 int regLeft = 0;
108082 int regRight = 0;
108083 u8 opx = op;
108084 int addrCmp = 0;
108085 int addrDone = sqlite3VdbeMakeLabel(pParse);
108086 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
108087
108088 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
108089 if( pParse->nErr ) return;
108090 if( nLeft!=sqlite3ExprVectorSize(pExpr: pRight) ){
108091 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
108092 return;
108093 }
108094 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
108095 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
108096 || pExpr->op==TK_LT || pExpr->op==TK_GT
108097 || pExpr->op==TK_LE || pExpr->op==TK_GE
108098 );
108099 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
108100 || (pExpr->op==TK_ISNOT && op==TK_NE) );
108101 assert( p5==0 || pExpr->op!=op );
108102 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
108103
108104 if( op==TK_LE ) opx = TK_LT;
108105 if( op==TK_GE ) opx = TK_GT;
108106 if( op==TK_NE ) opx = TK_EQ;
108107
108108 regLeft = exprCodeSubselect(pParse, pExpr: pLeft);
108109 regRight = exprCodeSubselect(pParse, pExpr: pRight);
108110
108111 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: dest);
108112 for(i=0; 1 /*Loop exits by "break"*/; i++){
108113 int regFree1 = 0, regFree2 = 0;
108114 Expr *pL = 0, *pR = 0;
108115 int r1, r2;
108116 assert( i>=0 && i<nLeft );
108117 if( addrCmp ) sqlite3VdbeJumpHere(p: v, addr: addrCmp);
108118 r1 = exprVectorRegister(pParse, pVector: pLeft, iField: i, regSelect: regLeft, ppExpr: &pL, pRegFree: &regFree1);
108119 r2 = exprVectorRegister(pParse, pVector: pRight, iField: i, regSelect: regRight, ppExpr: &pR, pRegFree: &regFree2);
108120 addrCmp = sqlite3VdbeCurrentAddr(p: v);
108121 codeCompare(pParse, pLeft: pL, pRight: pR, opcode: opx, in1: r1, in2: r2, dest: addrDone, jumpIfNull: p5, isCommuted);
108122 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
108123 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108124 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108125 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108126 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
108127 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
108128 sqlite3ReleaseTempReg(pParse, regFree1);
108129 sqlite3ReleaseTempReg(pParse, regFree2);
108130 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
108131 addrCmp = sqlite3VdbeAddOp0(p: v, OP_ElseEq);
108132 testcase(opx==TK_LT); VdbeCoverageIf(v,opx==TK_LT);
108133 testcase(opx==TK_GT); VdbeCoverageIf(v,opx==TK_GT);
108134 }
108135 if( p5==SQLITE_NULLEQ ){
108136 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: dest);
108137 }else{
108138 sqlite3VdbeAddOp3(p: v, OP_ZeroOrNull, p1: r1, p2: dest, p3: r2);
108139 }
108140 if( i==nLeft-1 ){
108141 break;
108142 }
108143 if( opx==TK_EQ ){
108144 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: dest, p2: addrDone); VdbeCoverage(v);
108145 }else{
108146 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
108147 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrDone);
108148 if( i==nLeft-2 ) opx = op;
108149 }
108150 }
108151 sqlite3VdbeJumpHere(p: v, addr: addrCmp);
108152 sqlite3VdbeResolveLabel(v, x: addrDone);
108153 if( op==TK_NE ){
108154 sqlite3VdbeAddOp2(p: v, OP_Not, p1: dest, p2: dest);
108155 }
108156}
108157
108158#if SQLITE_MAX_EXPR_DEPTH>0
108159/*
108160** Check that argument nHeight is less than or equal to the maximum
108161** expression depth allowed. If it is not, leave an error message in
108162** pParse.
108163*/
108164SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
108165 int rc = SQLITE_OK;
108166 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
108167 if( nHeight>mxHeight ){
108168 sqlite3ErrorMsg(pParse,
108169 zFormat: "Expression tree is too large (maximum depth %d)", mxHeight
108170 );
108171 rc = SQLITE_ERROR;
108172 }
108173 return rc;
108174}
108175
108176/* The following three functions, heightOfExpr(), heightOfExprList()
108177** and heightOfSelect(), are used to determine the maximum height
108178** of any expression tree referenced by the structure passed as the
108179** first argument.
108180**
108181** If this maximum height is greater than the current value pointed
108182** to by pnHeight, the second parameter, then set *pnHeight to that
108183** value.
108184*/
108185static void heightOfExpr(const Expr *p, int *pnHeight){
108186 if( p ){
108187 if( p->nHeight>*pnHeight ){
108188 *pnHeight = p->nHeight;
108189 }
108190 }
108191}
108192static void heightOfExprList(const ExprList *p, int *pnHeight){
108193 if( p ){
108194 int i;
108195 for(i=0; i<p->nExpr; i++){
108196 heightOfExpr(p: p->a[i].pExpr, pnHeight);
108197 }
108198 }
108199}
108200static void heightOfSelect(const Select *pSelect, int *pnHeight){
108201 const Select *p;
108202 for(p=pSelect; p; p=p->pPrior){
108203 heightOfExpr(p: p->pWhere, pnHeight);
108204 heightOfExpr(p: p->pHaving, pnHeight);
108205 heightOfExpr(p: p->pLimit, pnHeight);
108206 heightOfExprList(p: p->pEList, pnHeight);
108207 heightOfExprList(p: p->pGroupBy, pnHeight);
108208 heightOfExprList(p: p->pOrderBy, pnHeight);
108209 }
108210}
108211
108212/*
108213** Set the Expr.nHeight variable in the structure passed as an
108214** argument. An expression with no children, Expr.pList or
108215** Expr.pSelect member has a height of 1. Any other expression
108216** has a height equal to the maximum height of any other
108217** referenced Expr plus one.
108218**
108219** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
108220** if appropriate.
108221*/
108222static void exprSetHeight(Expr *p){
108223 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
108224 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
108225 nHeight = p->pRight->nHeight;
108226 }
108227 if( ExprUseXSelect(p) ){
108228 heightOfSelect(pSelect: p->x.pSelect, pnHeight: &nHeight);
108229 }else if( p->x.pList ){
108230 heightOfExprList(p: p->x.pList, pnHeight: &nHeight);
108231 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
108232 }
108233 p->nHeight = nHeight + 1;
108234}
108235
108236/*
108237** Set the Expr.nHeight variable using the exprSetHeight() function. If
108238** the height is greater than the maximum allowed expression depth,
108239** leave an error in pParse.
108240**
108241** Also propagate all EP_Propagate flags from the Expr.x.pList into
108242** Expr.flags.
108243*/
108244SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
108245 if( pParse->nErr ) return;
108246 exprSetHeight(p);
108247 sqlite3ExprCheckHeight(pParse, nHeight: p->nHeight);
108248}
108249
108250/*
108251** Return the maximum height of any expression tree referenced
108252** by the select statement passed as an argument.
108253*/
108254SQLITE_PRIVATE int sqlite3SelectExprHeight(const Select *p){
108255 int nHeight = 0;
108256 heightOfSelect(pSelect: p, pnHeight: &nHeight);
108257 return nHeight;
108258}
108259#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
108260/*
108261** Propagate all EP_Propagate flags from the Expr.x.pList into
108262** Expr.flags.
108263*/
108264SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
108265 if( pParse->nErr ) return;
108266 if( p && ExprUseXList(p) && p->x.pList ){
108267 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
108268 }
108269}
108270#define exprSetHeight(y)
108271#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
108272
108273/*
108274** Set the error offset for an Expr node, if possible.
108275*/
108276SQLITE_PRIVATE void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){
108277 if( pExpr==0 ) return;
108278 if( NEVER(ExprUseWJoin(pExpr)) ) return;
108279 pExpr->w.iOfst = iOfst;
108280}
108281
108282/*
108283** This routine is the core allocator for Expr nodes.
108284**
108285** Construct a new expression node and return a pointer to it. Memory
108286** for this node and for the pToken argument is a single allocation
108287** obtained from sqlite3DbMalloc(). The calling function
108288** is responsible for making sure the node eventually gets freed.
108289**
108290** If dequote is true, then the token (if it exists) is dequoted.
108291** If dequote is false, no dequoting is performed. The deQuote
108292** parameter is ignored if pToken is NULL or if the token does not
108293** appear to be quoted. If the quotes were of the form "..." (double-quotes)
108294** then the EP_DblQuoted flag is set on the expression node.
108295**
108296** Special case: If op==TK_INTEGER and pToken points to a string that
108297** can be translated into a 32-bit integer, then the token is not
108298** stored in u.zToken. Instead, the integer values is written
108299** into u.iValue and the EP_IntValue flag is set. No extra storage
108300** is allocated to hold the integer text and the dequote flag is ignored.
108301*/
108302SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
108303 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
108304 int op, /* Expression opcode */
108305 const Token *pToken, /* Token argument. Might be NULL */
108306 int dequote /* True to dequote */
108307){
108308 Expr *pNew;
108309 int nExtra = 0;
108310 int iValue = 0;
108311
108312 assert( db!=0 );
108313 if( pToken ){
108314 if( op!=TK_INTEGER || pToken->z==0
108315 || sqlite3GetInt32(zNum: pToken->z, pValue: &iValue)==0 ){
108316 nExtra = pToken->n+1;
108317 assert( iValue>=0 );
108318 }
108319 }
108320 pNew = sqlite3DbMallocRawNN(db, n: sizeof(Expr)+nExtra);
108321 if( pNew ){
108322 memset(s: pNew, c: 0, n: sizeof(Expr));
108323 pNew->op = (u8)op;
108324 pNew->iAgg = -1;
108325 if( pToken ){
108326 if( nExtra==0 ){
108327 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
108328 pNew->u.iValue = iValue;
108329 }else{
108330 pNew->u.zToken = (char*)&pNew[1];
108331 assert( pToken->z!=0 || pToken->n==0 );
108332 if( pToken->n ) memcpy(dest: pNew->u.zToken, src: pToken->z, n: pToken->n);
108333 pNew->u.zToken[pToken->n] = 0;
108334 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
108335 sqlite3DequoteExpr(p: pNew);
108336 }
108337 }
108338 }
108339#if SQLITE_MAX_EXPR_DEPTH>0
108340 pNew->nHeight = 1;
108341#endif
108342 }
108343 return pNew;
108344}
108345
108346/*
108347** Allocate a new expression node from a zero-terminated token that has
108348** already been dequoted.
108349*/
108350SQLITE_PRIVATE Expr *sqlite3Expr(
108351 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
108352 int op, /* Expression opcode */
108353 const char *zToken /* Token argument. Might be NULL */
108354){
108355 Token x;
108356 x.z = zToken;
108357 x.n = sqlite3Strlen30(z: zToken);
108358 return sqlite3ExprAlloc(db, op, pToken: &x, dequote: 0);
108359}
108360
108361/*
108362** Attach subtrees pLeft and pRight to the Expr node pRoot.
108363**
108364** If pRoot==NULL that means that a memory allocation error has occurred.
108365** In that case, delete the subtrees pLeft and pRight.
108366*/
108367SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
108368 sqlite3 *db,
108369 Expr *pRoot,
108370 Expr *pLeft,
108371 Expr *pRight
108372){
108373 if( pRoot==0 ){
108374 assert( db->mallocFailed );
108375 sqlite3ExprDelete(db, pLeft);
108376 sqlite3ExprDelete(db, pRight);
108377 }else{
108378 assert( ExprUseXList(pRoot) );
108379 assert( pRoot->x.pSelect==0 );
108380 if( pRight ){
108381 pRoot->pRight = pRight;
108382 pRoot->flags |= EP_Propagate & pRight->flags;
108383#if SQLITE_MAX_EXPR_DEPTH>0
108384 pRoot->nHeight = pRight->nHeight+1;
108385 }else{
108386 pRoot->nHeight = 1;
108387#endif
108388 }
108389 if( pLeft ){
108390 pRoot->pLeft = pLeft;
108391 pRoot->flags |= EP_Propagate & pLeft->flags;
108392#if SQLITE_MAX_EXPR_DEPTH>0
108393 if( pLeft->nHeight>=pRoot->nHeight ){
108394 pRoot->nHeight = pLeft->nHeight+1;
108395 }
108396#endif
108397 }
108398 }
108399}
108400
108401/*
108402** Allocate an Expr node which joins as many as two subtrees.
108403**
108404** One or both of the subtrees can be NULL. Return a pointer to the new
108405** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
108406** free the subtrees and return NULL.
108407*/
108408SQLITE_PRIVATE Expr *sqlite3PExpr(
108409 Parse *pParse, /* Parsing context */
108410 int op, /* Expression opcode */
108411 Expr *pLeft, /* Left operand */
108412 Expr *pRight /* Right operand */
108413){
108414 Expr *p;
108415 p = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(Expr));
108416 if( p ){
108417 memset(s: p, c: 0, n: sizeof(Expr));
108418 p->op = op & 0xff;
108419 p->iAgg = -1;
108420 sqlite3ExprAttachSubtrees(db: pParse->db, pRoot: p, pLeft, pRight);
108421 sqlite3ExprCheckHeight(pParse, nHeight: p->nHeight);
108422 }else{
108423 sqlite3ExprDelete(pParse->db, pLeft);
108424 sqlite3ExprDelete(pParse->db, pRight);
108425 }
108426 return p;
108427}
108428
108429/*
108430** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
108431** do a memory allocation failure) then delete the pSelect object.
108432*/
108433SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
108434 if( pExpr ){
108435 pExpr->x.pSelect = pSelect;
108436 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
108437 sqlite3ExprSetHeightAndFlags(pParse, p: pExpr);
108438 }else{
108439 assert( pParse->db->mallocFailed );
108440 sqlite3SelectDelete(pParse->db, pSelect);
108441 }
108442}
108443
108444/*
108445** Expression list pEList is a list of vector values. This function
108446** converts the contents of pEList to a VALUES(...) Select statement
108447** returning 1 row for each element of the list. For example, the
108448** expression list:
108449**
108450** ( (1,2), (3,4) (5,6) )
108451**
108452** is translated to the equivalent of:
108453**
108454** VALUES(1,2), (3,4), (5,6)
108455**
108456** Each of the vector values in pEList must contain exactly nElem terms.
108457** If a list element that is not a vector or does not contain nElem terms,
108458** an error message is left in pParse.
108459**
108460** This is used as part of processing IN(...) expressions with a list
108461** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))".
108462*/
108463SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){
108464 int ii;
108465 Select *pRet = 0;
108466 assert( nElem>1 );
108467 for(ii=0; ii<pEList->nExpr; ii++){
108468 Select *pSel;
108469 Expr *pExpr = pEList->a[ii].pExpr;
108470 int nExprElem;
108471 if( pExpr->op==TK_VECTOR ){
108472 assert( ExprUseXList(pExpr) );
108473 nExprElem = pExpr->x.pList->nExpr;
108474 }else{
108475 nExprElem = 1;
108476 }
108477 if( nExprElem!=nElem ){
108478 sqlite3ErrorMsg(pParse, zFormat: "IN(...) element has %d term%s - expected %d",
108479 nExprElem, nExprElem>1?"s":"", nElem
108480 );
108481 break;
108482 }
108483 assert( ExprUseXList(pExpr) );
108484 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
108485 pExpr->x.pList = 0;
108486 if( pSel ){
108487 if( pRet ){
108488 pSel->op = TK_ALL;
108489 pSel->pPrior = pRet;
108490 }
108491 pRet = pSel;
108492 }
108493 }
108494
108495 if( pRet && pRet->pPrior ){
108496 pRet->selFlags |= SF_MultiValue;
108497 }
108498 sqlite3ExprListDelete(pParse->db, pEList);
108499 return pRet;
108500}
108501
108502/*
108503** Join two expressions using an AND operator. If either expression is
108504** NULL, then just return the other expression.
108505**
108506** If one side or the other of the AND is known to be false, and neither side
108507** is part of an ON clause, then instead of returning an AND expression,
108508** just return a constant expression with a value of false.
108509*/
108510SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
108511 sqlite3 *db = pParse->db;
108512 if( pLeft==0 ){
108513 return pRight;
108514 }else if( pRight==0 ){
108515 return pLeft;
108516 }else{
108517 u32 f = pLeft->flags | pRight->flags;
108518 if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse))==EP_IsFalse
108519 && !IN_RENAME_OBJECT
108520 ){
108521 sqlite3ExprDeferredDelete(pParse, pLeft);
108522 sqlite3ExprDeferredDelete(pParse, pRight);
108523 return sqlite3Expr(db, TK_INTEGER, zToken: "0");
108524 }else{
108525 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
108526 }
108527 }
108528}
108529
108530/*
108531** Construct a new expression node for a function with multiple
108532** arguments.
108533*/
108534SQLITE_PRIVATE Expr *sqlite3ExprFunction(
108535 Parse *pParse, /* Parsing context */
108536 ExprList *pList, /* Argument list */
108537 const Token *pToken, /* Name of the function */
108538 int eDistinct /* SF_Distinct or SF_ALL or 0 */
108539){
108540 Expr *pNew;
108541 sqlite3 *db = pParse->db;
108542 assert( pToken );
108543 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, dequote: 1);
108544 if( pNew==0 ){
108545 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
108546 return 0;
108547 }
108548 assert( !ExprHasProperty(pNew, EP_InnerON|EP_OuterON) );
108549 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
108550 if( pList
108551 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
108552 && !pParse->nested
108553 ){
108554 sqlite3ErrorMsg(pParse, zFormat: "too many arguments on function %T", pToken);
108555 }
108556 pNew->x.pList = pList;
108557 ExprSetProperty(pNew, EP_HasFunc);
108558 assert( ExprUseXList(pNew) );
108559 sqlite3ExprSetHeightAndFlags(pParse, p: pNew);
108560 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
108561 return pNew;
108562}
108563
108564/*
108565** Check to see if a function is usable according to current access
108566** rules:
108567**
108568** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
108569**
108570** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
108571** top-level SQL
108572**
108573** If the function is not usable, create an error.
108574*/
108575SQLITE_PRIVATE void sqlite3ExprFunctionUsable(
108576 Parse *pParse, /* Parsing and code generating context */
108577 const Expr *pExpr, /* The function invocation */
108578 const FuncDef *pDef /* The function being invoked */
108579){
108580 assert( !IN_RENAME_OBJECT );
108581 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
108582 if( ExprHasProperty(pExpr, EP_FromDDL) ){
108583 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
108584 || (pParse->db->flags & SQLITE_TrustedSchema)==0
108585 ){
108586 /* Functions prohibited in triggers and views if:
108587 ** (1) tagged with SQLITE_DIRECTONLY
108588 ** (2) not tagged with SQLITE_INNOCUOUS (which means it
108589 ** is tagged with SQLITE_FUNC_UNSAFE) and
108590 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
108591 ** that the schema is possibly tainted).
108592 */
108593 sqlite3ErrorMsg(pParse, zFormat: "unsafe use of %#T()", pExpr);
108594 }
108595 }
108596}
108597
108598/*
108599** Assign a variable number to an expression that encodes a wildcard
108600** in the original SQL statement.
108601**
108602** Wildcards consisting of a single "?" are assigned the next sequential
108603** variable number.
108604**
108605** Wildcards of the form "?nnn" are assigned the number "nnn". We make
108606** sure "nnn" is not too big to avoid a denial of service attack when
108607** the SQL statement comes from an external source.
108608**
108609** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
108610** as the previous instance of the same wildcard. Or if this is the first
108611** instance of the wildcard, the next sequential variable number is
108612** assigned.
108613*/
108614SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
108615 sqlite3 *db = pParse->db;
108616 const char *z;
108617 ynVar x;
108618
108619 if( pExpr==0 ) return;
108620 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
108621 z = pExpr->u.zToken;
108622 assert( z!=0 );
108623 assert( z[0]!=0 );
108624 assert( n==(u32)sqlite3Strlen30(z) );
108625 if( z[1]==0 ){
108626 /* Wildcard of the form "?". Assign the next variable number */
108627 assert( z[0]=='?' );
108628 x = (ynVar)(++pParse->nVar);
108629 }else{
108630 int doAdd = 0;
108631 if( z[0]=='?' ){
108632 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
108633 ** use it as the variable number */
108634 i64 i;
108635 int bOk;
108636 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
108637 i = z[1]-'0'; /* The common case of ?N for a single digit N */
108638 bOk = 1;
108639 }else{
108640 bOk = 0==sqlite3Atoi64(zNum: &z[1], pNum: &i, length: n-1, SQLITE_UTF8);
108641 }
108642 testcase( i==0 );
108643 testcase( i==1 );
108644 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
108645 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
108646 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
108647 sqlite3ErrorMsg(pParse, zFormat: "variable number must be between ?1 and ?%d",
108648 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
108649 sqlite3RecordErrorOffsetOfExpr(db: pParse->db, pExpr);
108650 return;
108651 }
108652 x = (ynVar)i;
108653 if( x>pParse->nVar ){
108654 pParse->nVar = (int)x;
108655 doAdd = 1;
108656 }else if( sqlite3VListNumToName(pIn: pParse->pVList, iVal: x)==0 ){
108657 doAdd = 1;
108658 }
108659 }else{
108660 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
108661 ** number as the prior appearance of the same name, or if the name
108662 ** has never appeared before, reuse the same variable number
108663 */
108664 x = (ynVar)sqlite3VListNameToNum(pIn: pParse->pVList, zName: z, nName: n);
108665 if( x==0 ){
108666 x = (ynVar)(++pParse->nVar);
108667 doAdd = 1;
108668 }
108669 }
108670 if( doAdd ){
108671 pParse->pVList = sqlite3VListAdd(db, pIn: pParse->pVList, zName: z, nName: n, iVal: x);
108672 }
108673 }
108674 pExpr->iColumn = x;
108675 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
108676 sqlite3ErrorMsg(pParse, zFormat: "too many SQL variables");
108677 sqlite3RecordErrorOffsetOfExpr(db: pParse->db, pExpr);
108678 }
108679}
108680
108681/*
108682** Recursively delete an expression tree.
108683*/
108684static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
108685 assert( p!=0 );
108686 assert( db!=0 );
108687 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
108688 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
108689 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
108690 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
108691#ifdef SQLITE_DEBUG
108692 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
108693 assert( p->pLeft==0 );
108694 assert( p->pRight==0 );
108695 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
108696 assert( !ExprUseXList(p) || p->x.pList==0 );
108697 }
108698#endif
108699 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
108700 /* The Expr.x union is never used at the same time as Expr.pRight */
108701 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
108702 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p: p->pLeft);
108703 if( p->pRight ){
108704 assert( !ExprHasProperty(p, EP_WinFunc) );
108705 sqlite3ExprDeleteNN(db, p: p->pRight);
108706 }else if( ExprUseXSelect(p) ){
108707 assert( !ExprHasProperty(p, EP_WinFunc) );
108708 sqlite3SelectDelete(db, p->x.pSelect);
108709 }else{
108710 sqlite3ExprListDelete(db, p->x.pList);
108711#ifndef SQLITE_OMIT_WINDOWFUNC
108712 if( ExprHasProperty(p, EP_WinFunc) ){
108713 sqlite3WindowDelete(db, p->y.pWin);
108714 }
108715#endif
108716 }
108717 }
108718 if( !ExprHasProperty(p, EP_Static) ){
108719 sqlite3DbNNFreeNN(db, p);
108720 }
108721}
108722SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
108723 if( p ) sqlite3ExprDeleteNN(db, p);
108724}
108725
108726/*
108727** Clear both elements of an OnOrUsing object
108728*/
108729SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){
108730 if( p==0 ){
108731 /* Nothing to clear */
108732 }else if( p->pOn ){
108733 sqlite3ExprDeleteNN(db, p: p->pOn);
108734 }else if( p->pUsing ){
108735 sqlite3IdListDelete(db, p->pUsing);
108736 }
108737}
108738
108739/*
108740** Arrange to cause pExpr to be deleted when the pParse is deleted.
108741** This is similar to sqlite3ExprDelete() except that the delete is
108742** deferred until the pParse is deleted.
108743**
108744** The pExpr might be deleted immediately on an OOM error.
108745**
108746** The deferred delete is (currently) implemented by adding the
108747** pExpr to the pParse->pConstExpr list with a register number of 0.
108748*/
108749SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
108750 sqlite3ParserAddCleanup(pParse,
108751 (void(*)(sqlite3*,void*))sqlite3ExprDelete,
108752 pExpr);
108753}
108754
108755/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
108756** expression.
108757*/
108758SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
108759 if( p ){
108760 if( IN_RENAME_OBJECT ){
108761 sqlite3RenameExprUnmap(pParse, p);
108762 }
108763 sqlite3ExprDeleteNN(db: pParse->db, p);
108764 }
108765}
108766
108767/*
108768** Return the number of bytes allocated for the expression structure
108769** passed as the first argument. This is always one of EXPR_FULLSIZE,
108770** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
108771*/
108772static int exprStructSize(const Expr *p){
108773 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
108774 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
108775 return EXPR_FULLSIZE;
108776}
108777
108778/*
108779** The dupedExpr*Size() routines each return the number of bytes required
108780** to store a copy of an expression or expression tree. They differ in
108781** how much of the tree is measured.
108782**
108783** dupedExprStructSize() Size of only the Expr structure
108784** dupedExprNodeSize() Size of Expr + space for token
108785** dupedExprSize() Expr + token + subtree components
108786**
108787***************************************************************************
108788**
108789** The dupedExprStructSize() function returns two values OR-ed together:
108790** (1) the space required for a copy of the Expr structure only and
108791** (2) the EP_xxx flags that indicate what the structure size should be.
108792** The return values is always one of:
108793**
108794** EXPR_FULLSIZE
108795** EXPR_REDUCEDSIZE | EP_Reduced
108796** EXPR_TOKENONLYSIZE | EP_TokenOnly
108797**
108798** The size of the structure can be found by masking the return value
108799** of this routine with 0xfff. The flags can be found by masking the
108800** return value with EP_Reduced|EP_TokenOnly.
108801**
108802** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
108803** (unreduced) Expr objects as they or originally constructed by the parser.
108804** During expression analysis, extra information is computed and moved into
108805** later parts of the Expr object and that extra information might get chopped
108806** off if the expression is reduced. Note also that it does not work to
108807** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
108808** to reduce a pristine expression tree from the parser. The implementation
108809** of dupedExprStructSize() contain multiple assert() statements that attempt
108810** to enforce this constraint.
108811*/
108812static int dupedExprStructSize(const Expr *p, int flags){
108813 int nSize;
108814 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
108815 assert( EXPR_FULLSIZE<=0xfff );
108816 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
108817 if( 0==flags || p->op==TK_SELECT_COLUMN
108818#ifndef SQLITE_OMIT_WINDOWFUNC
108819 || ExprHasProperty(p, EP_WinFunc)
108820#endif
108821 ){
108822 nSize = EXPR_FULLSIZE;
108823 }else{
108824 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
108825 assert( !ExprHasProperty(p, EP_OuterON) );
108826 assert( !ExprHasVVAProperty(p, EP_NoReduce) );
108827 if( p->pLeft || p->x.pList ){
108828 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
108829 }else{
108830 assert( p->pRight==0 );
108831 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
108832 }
108833 }
108834 return nSize;
108835}
108836
108837/*
108838** This function returns the space in bytes required to store the copy
108839** of the Expr structure and a copy of the Expr.u.zToken string (if that
108840** string is defined.)
108841*/
108842static int dupedExprNodeSize(const Expr *p, int flags){
108843 int nByte = dupedExprStructSize(p, flags) & 0xfff;
108844 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
108845 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
108846 }
108847 return ROUND8(nByte);
108848}
108849
108850/*
108851** Return the number of bytes required to create a duplicate of the
108852** expression passed as the first argument. The second argument is a
108853** mask containing EXPRDUP_XXX flags.
108854**
108855** The value returned includes space to create a copy of the Expr struct
108856** itself and the buffer referred to by Expr.u.zToken, if any.
108857**
108858** If the EXPRDUP_REDUCE flag is set, then the return value includes
108859** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
108860** and Expr.pRight variables (but not for any structures pointed to or
108861** descended from the Expr.x.pList or Expr.x.pSelect variables).
108862*/
108863static int dupedExprSize(const Expr *p, int flags){
108864 int nByte = 0;
108865 if( p ){
108866 nByte = dupedExprNodeSize(p, flags);
108867 if( flags&EXPRDUP_REDUCE ){
108868 nByte += dupedExprSize(p: p->pLeft, flags) + dupedExprSize(p: p->pRight, flags);
108869 }
108870 }
108871 return nByte;
108872}
108873
108874/*
108875** This function is similar to sqlite3ExprDup(), except that if pzBuffer
108876** is not NULL then *pzBuffer is assumed to point to a buffer large enough
108877** to store the copy of expression p, the copies of p->u.zToken
108878** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
108879** if any. Before returning, *pzBuffer is set to the first byte past the
108880** portion of the buffer copied into by this function.
108881*/
108882static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
108883 Expr *pNew; /* Value to return */
108884 u8 *zAlloc; /* Memory space from which to build Expr object */
108885 u32 staticFlag; /* EP_Static if space not obtained from malloc */
108886
108887 assert( db!=0 );
108888 assert( p );
108889 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
108890 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
108891
108892 /* Figure out where to write the new Expr structure. */
108893 if( pzBuffer ){
108894 zAlloc = *pzBuffer;
108895 staticFlag = EP_Static;
108896 assert( zAlloc!=0 );
108897 }else{
108898 zAlloc = sqlite3DbMallocRawNN(db, n: dupedExprSize(p, flags: dupFlags));
108899 staticFlag = 0;
108900 }
108901 pNew = (Expr *)zAlloc;
108902
108903 if( pNew ){
108904 /* Set nNewSize to the size allocated for the structure pointed to
108905 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
108906 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
108907 ** by the copy of the p->u.zToken string (if any).
108908 */
108909 const unsigned nStructSize = dupedExprStructSize(p, flags: dupFlags);
108910 const int nNewSize = nStructSize & 0xfff;
108911 int nToken;
108912 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
108913 nToken = sqlite3Strlen30(z: p->u.zToken) + 1;
108914 }else{
108915 nToken = 0;
108916 }
108917 if( dupFlags ){
108918 assert( ExprHasProperty(p, EP_Reduced)==0 );
108919 memcpy(dest: zAlloc, src: p, n: nNewSize);
108920 }else{
108921 u32 nSize = (u32)exprStructSize(p);
108922 memcpy(dest: zAlloc, src: p, n: nSize);
108923 if( nSize<EXPR_FULLSIZE ){
108924 memset(s: &zAlloc[nSize], c: 0, EXPR_FULLSIZE-nSize);
108925 }
108926 }
108927
108928 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
108929 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
108930 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
108931 pNew->flags |= staticFlag;
108932 ExprClearVVAProperties(pNew);
108933 if( dupFlags ){
108934 ExprSetVVAProperty(pNew, EP_Immutable);
108935 }
108936
108937 /* Copy the p->u.zToken string, if any. */
108938 if( nToken ){
108939 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
108940 memcpy(dest: zToken, src: p->u.zToken, n: nToken);
108941 }
108942
108943 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
108944 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
108945 if( ExprUseXSelect(p) ){
108946 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
108947 }else{
108948 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
108949 }
108950 }
108951
108952 /* Fill in pNew->pLeft and pNew->pRight. */
108953 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
108954 zAlloc += dupedExprNodeSize(p, flags: dupFlags);
108955 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
108956 pNew->pLeft = p->pLeft ?
108957 exprDup(db, p: p->pLeft, EXPRDUP_REDUCE, pzBuffer: &zAlloc) : 0;
108958 pNew->pRight = p->pRight ?
108959 exprDup(db, p: p->pRight, EXPRDUP_REDUCE, pzBuffer: &zAlloc) : 0;
108960 }
108961#ifndef SQLITE_OMIT_WINDOWFUNC
108962 if( ExprHasProperty(p, EP_WinFunc) ){
108963 pNew->y.pWin = sqlite3WindowDup(db, pOwner: pNew, p: p->y.pWin);
108964 assert( ExprHasProperty(pNew, EP_WinFunc) );
108965 }
108966#endif /* SQLITE_OMIT_WINDOWFUNC */
108967 if( pzBuffer ){
108968 *pzBuffer = zAlloc;
108969 }
108970 }else{
108971 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
108972 if( pNew->op==TK_SELECT_COLUMN ){
108973 pNew->pLeft = p->pLeft;
108974 assert( p->pRight==0 || p->pRight==p->pLeft
108975 || ExprHasProperty(p->pLeft, EP_Subquery) );
108976 }else{
108977 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
108978 }
108979 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
108980 }
108981 }
108982 }
108983 return pNew;
108984}
108985
108986/*
108987** Create and return a deep copy of the object passed as the second
108988** argument. If an OOM condition is encountered, NULL is returned
108989** and the db->mallocFailed flag set.
108990*/
108991#ifndef SQLITE_OMIT_CTE
108992SQLITE_PRIVATE With *sqlite3WithDup(sqlite3 *db, With *p){
108993 With *pRet = 0;
108994 if( p ){
108995 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
108996 pRet = sqlite3DbMallocZero(db, n: nByte);
108997 if( pRet ){
108998 int i;
108999 pRet->nCte = p->nCte;
109000 for(i=0; i<p->nCte; i++){
109001 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
109002 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
109003 pRet->a[i].zName = sqlite3DbStrDup(db, z: p->a[i].zName);
109004 pRet->a[i].eM10d = p->a[i].eM10d;
109005 }
109006 }
109007 }
109008 return pRet;
109009}
109010#else
109011# define sqlite3WithDup(x,y) 0
109012#endif
109013
109014#ifndef SQLITE_OMIT_WINDOWFUNC
109015/*
109016** The gatherSelectWindows() procedure and its helper routine
109017** gatherSelectWindowsCallback() are used to scan all the expressions
109018** an a newly duplicated SELECT statement and gather all of the Window
109019** objects found there, assembling them onto the linked list at Select->pWin.
109020*/
109021static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
109022 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
109023 Select *pSelect = pWalker->u.pSelect;
109024 Window *pWin = pExpr->y.pWin;
109025 assert( pWin );
109026 assert( IsWindowFunc(pExpr) );
109027 assert( pWin->ppThis==0 );
109028 sqlite3WindowLink(pSel: pSelect, pWin);
109029 }
109030 return WRC_Continue;
109031}
109032static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
109033 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
109034}
109035static void gatherSelectWindows(Select *p){
109036 Walker w;
109037 w.xExprCallback = gatherSelectWindowsCallback;
109038 w.xSelectCallback = gatherSelectWindowsSelectCallback;
109039 w.xSelectCallback2 = 0;
109040 w.pParse = 0;
109041 w.u.pSelect = p;
109042 sqlite3WalkSelect(pWalker: &w, p);
109043}
109044#endif
109045
109046
109047/*
109048** The following group of routines make deep copies of expressions,
109049** expression lists, ID lists, and select statements. The copies can
109050** be deleted (by being passed to their respective ...Delete() routines)
109051** without effecting the originals.
109052**
109053** The expression list, ID, and source lists return by sqlite3ExprListDup(),
109054** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
109055** by subsequent calls to sqlite*ListAppend() routines.
109056**
109057** Any tables that the SrcList might point to are not duplicated.
109058**
109059** The flags parameter contains a combination of the EXPRDUP_XXX flags.
109060** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
109061** truncated version of the usual Expr structure that will be stored as
109062** part of the in-memory representation of the database schema.
109063*/
109064SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){
109065 assert( flags==0 || flags==EXPRDUP_REDUCE );
109066 return p ? exprDup(db, p, dupFlags: flags, pzBuffer: 0) : 0;
109067}
109068SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){
109069 ExprList *pNew;
109070 struct ExprList_item *pItem;
109071 const struct ExprList_item *pOldItem;
109072 int i;
109073 Expr *pPriorSelectColOld = 0;
109074 Expr *pPriorSelectColNew = 0;
109075 assert( db!=0 );
109076 if( p==0 ) return 0;
109077 pNew = sqlite3DbMallocRawNN(db, n: sqlite3DbMallocSize(db, p));
109078 if( pNew==0 ) return 0;
109079 pNew->nExpr = p->nExpr;
109080 pNew->nAlloc = p->nAlloc;
109081 pItem = pNew->a;
109082 pOldItem = p->a;
109083 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
109084 Expr *pOldExpr = pOldItem->pExpr;
109085 Expr *pNewExpr;
109086 pItem->pExpr = sqlite3ExprDup(db, p: pOldExpr, flags);
109087 if( pOldExpr
109088 && pOldExpr->op==TK_SELECT_COLUMN
109089 && (pNewExpr = pItem->pExpr)!=0
109090 ){
109091 if( pNewExpr->pRight ){
109092 pPriorSelectColOld = pOldExpr->pRight;
109093 pPriorSelectColNew = pNewExpr->pRight;
109094 pNewExpr->pLeft = pNewExpr->pRight;
109095 }else{
109096 if( pOldExpr->pLeft!=pPriorSelectColOld ){
109097 pPriorSelectColOld = pOldExpr->pLeft;
109098 pPriorSelectColNew = sqlite3ExprDup(db, p: pPriorSelectColOld, flags);
109099 pNewExpr->pRight = pPriorSelectColNew;
109100 }
109101 pNewExpr->pLeft = pPriorSelectColNew;
109102 }
109103 }
109104 pItem->zEName = sqlite3DbStrDup(db, z: pOldItem->zEName);
109105 pItem->fg = pOldItem->fg;
109106 pItem->fg.done = 0;
109107 pItem->u = pOldItem->u;
109108 }
109109 return pNew;
109110}
109111
109112/*
109113** If cursors, triggers, views and subqueries are all omitted from
109114** the build, then none of the following routines, except for
109115** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
109116** called with a NULL argument.
109117*/
109118#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
109119 || !defined(SQLITE_OMIT_SUBQUERY)
109120SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
109121 SrcList *pNew;
109122 int i;
109123 int nByte;
109124 assert( db!=0 );
109125 if( p==0 ) return 0;
109126 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
109127 pNew = sqlite3DbMallocRawNN(db, n: nByte );
109128 if( pNew==0 ) return 0;
109129 pNew->nSrc = pNew->nAlloc = p->nSrc;
109130 for(i=0; i<p->nSrc; i++){
109131 SrcItem *pNewItem = &pNew->a[i];
109132 const SrcItem *pOldItem = &p->a[i];
109133 Table *pTab;
109134 pNewItem->pSchema = pOldItem->pSchema;
109135 pNewItem->zDatabase = sqlite3DbStrDup(db, z: pOldItem->zDatabase);
109136 pNewItem->zName = sqlite3DbStrDup(db, z: pOldItem->zName);
109137 pNewItem->zAlias = sqlite3DbStrDup(db, z: pOldItem->zAlias);
109138 pNewItem->fg = pOldItem->fg;
109139 pNewItem->iCursor = pOldItem->iCursor;
109140 pNewItem->addrFillSub = pOldItem->addrFillSub;
109141 pNewItem->regReturn = pOldItem->regReturn;
109142 if( pNewItem->fg.isIndexedBy ){
109143 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, z: pOldItem->u1.zIndexedBy);
109144 }
109145 pNewItem->u2 = pOldItem->u2;
109146 if( pNewItem->fg.isCte ){
109147 pNewItem->u2.pCteUse->nUse++;
109148 }
109149 if( pNewItem->fg.isTabFunc ){
109150 pNewItem->u1.pFuncArg =
109151 sqlite3ExprListDup(db, p: pOldItem->u1.pFuncArg, flags);
109152 }
109153 pTab = pNewItem->pTab = pOldItem->pTab;
109154 if( pTab ){
109155 pTab->nTabRef++;
109156 }
109157 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
109158 if( pOldItem->fg.isUsing ){
109159 assert( pNewItem->fg.isUsing );
109160 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
109161 }else{
109162 pNewItem->u3.pOn = sqlite3ExprDup(db, p: pOldItem->u3.pOn, flags);
109163 }
109164 pNewItem->colUsed = pOldItem->colUsed;
109165 }
109166 return pNew;
109167}
109168SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){
109169 IdList *pNew;
109170 int i;
109171 assert( db!=0 );
109172 if( p==0 ) return 0;
109173 assert( p->eU4!=EU4_EXPR );
109174 pNew = sqlite3DbMallocRawNN(db, n: sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
109175 if( pNew==0 ) return 0;
109176 pNew->nId = p->nId;
109177 pNew->eU4 = p->eU4;
109178 for(i=0; i<p->nId; i++){
109179 struct IdList_item *pNewItem = &pNew->a[i];
109180 const struct IdList_item *pOldItem = &p->a[i];
109181 pNewItem->zName = sqlite3DbStrDup(db, z: pOldItem->zName);
109182 pNewItem->u4 = pOldItem->u4;
109183 }
109184 return pNew;
109185}
109186SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){
109187 Select *pRet = 0;
109188 Select *pNext = 0;
109189 Select **pp = &pRet;
109190 const Select *p;
109191
109192 assert( db!=0 );
109193 for(p=pDup; p; p=p->pPrior){
109194 Select *pNew = sqlite3DbMallocRawNN(db, n: sizeof(*p) );
109195 if( pNew==0 ) break;
109196 pNew->pEList = sqlite3ExprListDup(db, p: p->pEList, flags);
109197 pNew->pSrc = sqlite3SrcListDup(db, p: p->pSrc, flags);
109198 pNew->pWhere = sqlite3ExprDup(db, p: p->pWhere, flags);
109199 pNew->pGroupBy = sqlite3ExprListDup(db, p: p->pGroupBy, flags);
109200 pNew->pHaving = sqlite3ExprDup(db, p: p->pHaving, flags);
109201 pNew->pOrderBy = sqlite3ExprListDup(db, p: p->pOrderBy, flags);
109202 pNew->op = p->op;
109203 pNew->pNext = pNext;
109204 pNew->pPrior = 0;
109205 pNew->pLimit = sqlite3ExprDup(db, p: p->pLimit, flags);
109206 pNew->iLimit = 0;
109207 pNew->iOffset = 0;
109208 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
109209 pNew->addrOpenEphm[0] = -1;
109210 pNew->addrOpenEphm[1] = -1;
109211 pNew->nSelectRow = p->nSelectRow;
109212 pNew->pWith = sqlite3WithDup(db, p: p->pWith);
109213#ifndef SQLITE_OMIT_WINDOWFUNC
109214 pNew->pWin = 0;
109215 pNew->pWinDefn = sqlite3WindowListDup(db, p: p->pWinDefn);
109216 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(p: pNew);
109217#endif
109218 pNew->selId = p->selId;
109219 if( db->mallocFailed ){
109220 /* Any prior OOM might have left the Select object incomplete.
109221 ** Delete the whole thing rather than allow an incomplete Select
109222 ** to be used by the code generator. */
109223 pNew->pNext = 0;
109224 sqlite3SelectDelete(db, pNew);
109225 break;
109226 }
109227 *pp = pNew;
109228 pp = &pNew->pPrior;
109229 pNext = pNew;
109230 }
109231
109232 return pRet;
109233}
109234#else
109235SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){
109236 assert( p==0 );
109237 return 0;
109238}
109239#endif
109240
109241
109242/*
109243** Add a new element to the end of an expression list. If pList is
109244** initially NULL, then create a new expression list.
109245**
109246** The pList argument must be either NULL or a pointer to an ExprList
109247** obtained from a prior call to sqlite3ExprListAppend(). This routine
109248** may not be used with an ExprList obtained from sqlite3ExprListDup().
109249** Reason: This routine assumes that the number of slots in pList->a[]
109250** is a power of two. That is true for sqlite3ExprListAppend() returns
109251** but is not necessarily true from the return value of sqlite3ExprListDup().
109252**
109253** If a memory allocation error occurs, the entire list is freed and
109254** NULL is returned. If non-NULL is returned, then it is guaranteed
109255** that the new entry was successfully appended.
109256*/
109257static const struct ExprList_item zeroItem = {0};
109258SQLITE_PRIVATE SQLITE_NOINLINE ExprList *sqlite3ExprListAppendNew(
109259 sqlite3 *db, /* Database handle. Used for memory allocation */
109260 Expr *pExpr /* Expression to be appended. Might be NULL */
109261){
109262 struct ExprList_item *pItem;
109263 ExprList *pList;
109264
109265 pList = sqlite3DbMallocRawNN(db, n: sizeof(ExprList)+sizeof(pList->a[0])*4 );
109266 if( pList==0 ){
109267 sqlite3ExprDelete(db, p: pExpr);
109268 return 0;
109269 }
109270 pList->nAlloc = 4;
109271 pList->nExpr = 1;
109272 pItem = &pList->a[0];
109273 *pItem = zeroItem;
109274 pItem->pExpr = pExpr;
109275 return pList;
109276}
109277SQLITE_PRIVATE SQLITE_NOINLINE ExprList *sqlite3ExprListAppendGrow(
109278 sqlite3 *db, /* Database handle. Used for memory allocation */
109279 ExprList *pList, /* List to which to append. Might be NULL */
109280 Expr *pExpr /* Expression to be appended. Might be NULL */
109281){
109282 struct ExprList_item *pItem;
109283 ExprList *pNew;
109284 pList->nAlloc *= 2;
109285 pNew = sqlite3DbRealloc(db, p: pList,
109286 n: sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
109287 if( pNew==0 ){
109288 sqlite3ExprListDelete(db, pList);
109289 sqlite3ExprDelete(db, p: pExpr);
109290 return 0;
109291 }else{
109292 pList = pNew;
109293 }
109294 pItem = &pList->a[pList->nExpr++];
109295 *pItem = zeroItem;
109296 pItem->pExpr = pExpr;
109297 return pList;
109298}
109299SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
109300 Parse *pParse, /* Parsing context */
109301 ExprList *pList, /* List to which to append. Might be NULL */
109302 Expr *pExpr /* Expression to be appended. Might be NULL */
109303){
109304 struct ExprList_item *pItem;
109305 if( pList==0 ){
109306 return sqlite3ExprListAppendNew(db: pParse->db,pExpr);
109307 }
109308 if( pList->nAlloc<pList->nExpr+1 ){
109309 return sqlite3ExprListAppendGrow(db: pParse->db,pList,pExpr);
109310 }
109311 pItem = &pList->a[pList->nExpr++];
109312 *pItem = zeroItem;
109313 pItem->pExpr = pExpr;
109314 return pList;
109315}
109316
109317/*
109318** pColumns and pExpr form a vector assignment which is part of the SET
109319** clause of an UPDATE statement. Like this:
109320**
109321** (a,b,c) = (expr1,expr2,expr3)
109322** Or: (a,b,c) = (SELECT x,y,z FROM ....)
109323**
109324** For each term of the vector assignment, append new entries to the
109325** expression list pList. In the case of a subquery on the RHS, append
109326** TK_SELECT_COLUMN expressions.
109327*/
109328SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
109329 Parse *pParse, /* Parsing context */
109330 ExprList *pList, /* List to which to append. Might be NULL */
109331 IdList *pColumns, /* List of names of LHS of the assignment */
109332 Expr *pExpr /* Vector expression to be appended. Might be NULL */
109333){
109334 sqlite3 *db = pParse->db;
109335 int n;
109336 int i;
109337 int iFirst = pList ? pList->nExpr : 0;
109338 /* pColumns can only be NULL due to an OOM but an OOM will cause an
109339 ** exit prior to this routine being invoked */
109340 if( NEVER(pColumns==0) ) goto vector_append_error;
109341 if( pExpr==0 ) goto vector_append_error;
109342
109343 /* If the RHS is a vector, then we can immediately check to see that
109344 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
109345 ** wildcards ("*") in the result set of the SELECT must be expanded before
109346 ** we can do the size check, so defer the size check until code generation.
109347 */
109348 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
109349 sqlite3ErrorMsg(pParse, zFormat: "%d columns assigned %d values",
109350 pColumns->nId, n);
109351 goto vector_append_error;
109352 }
109353
109354 for(i=0; i<pColumns->nId; i++){
109355 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pVector: pExpr, iField: i, nField: pColumns->nId);
109356 assert( pSubExpr!=0 || db->mallocFailed );
109357 if( pSubExpr==0 ) continue;
109358 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pSubExpr);
109359 if( pList ){
109360 assert( pList->nExpr==iFirst+i+1 );
109361 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
109362 pColumns->a[i].zName = 0;
109363 }
109364 }
109365
109366 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
109367 Expr *pFirst = pList->a[iFirst].pExpr;
109368 assert( pFirst!=0 );
109369 assert( pFirst->op==TK_SELECT_COLUMN );
109370
109371 /* Store the SELECT statement in pRight so it will be deleted when
109372 ** sqlite3ExprListDelete() is called */
109373 pFirst->pRight = pExpr;
109374 pExpr = 0;
109375
109376 /* Remember the size of the LHS in iTable so that we can check that
109377 ** the RHS and LHS sizes match during code generation. */
109378 pFirst->iTable = pColumns->nId;
109379 }
109380
109381vector_append_error:
109382 sqlite3ExprUnmapAndDelete(pParse, p: pExpr);
109383 sqlite3IdListDelete(db, pColumns);
109384 return pList;
109385}
109386
109387/*
109388** Set the sort order for the last element on the given ExprList.
109389*/
109390SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
109391 struct ExprList_item *pItem;
109392 if( p==0 ) return;
109393 assert( p->nExpr>0 );
109394
109395 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 );
109396 assert( iSortOrder==SQLITE_SO_UNDEFINED
109397 || iSortOrder==SQLITE_SO_ASC
109398 || iSortOrder==SQLITE_SO_DESC
109399 );
109400 assert( eNulls==SQLITE_SO_UNDEFINED
109401 || eNulls==SQLITE_SO_ASC
109402 || eNulls==SQLITE_SO_DESC
109403 );
109404
109405 pItem = &p->a[p->nExpr-1];
109406 assert( pItem->fg.bNulls==0 );
109407 if( iSortOrder==SQLITE_SO_UNDEFINED ){
109408 iSortOrder = SQLITE_SO_ASC;
109409 }
109410 pItem->fg.sortFlags = (u8)iSortOrder;
109411
109412 if( eNulls!=SQLITE_SO_UNDEFINED ){
109413 pItem->fg.bNulls = 1;
109414 if( iSortOrder!=eNulls ){
109415 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
109416 }
109417 }
109418}
109419
109420/*
109421** Set the ExprList.a[].zEName element of the most recently added item
109422** on the expression list.
109423**
109424** pList might be NULL following an OOM error. But pName should never be
109425** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
109426** is set.
109427*/
109428SQLITE_PRIVATE void sqlite3ExprListSetName(
109429 Parse *pParse, /* Parsing context */
109430 ExprList *pList, /* List to which to add the span. */
109431 const Token *pName, /* Name to be added */
109432 int dequote /* True to cause the name to be dequoted */
109433){
109434 assert( pList!=0 || pParse->db->mallocFailed!=0 );
109435 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
109436 if( pList ){
109437 struct ExprList_item *pItem;
109438 assert( pList->nExpr>0 );
109439 pItem = &pList->a[pList->nExpr-1];
109440 assert( pItem->zEName==0 );
109441 assert( pItem->fg.eEName==ENAME_NAME );
109442 pItem->zEName = sqlite3DbStrNDup(db: pParse->db, z: pName->z, n: pName->n);
109443 if( dequote ){
109444 /* If dequote==0, then pName->z does not point to part of a DDL
109445 ** statement handled by the parser. And so no token need be added
109446 ** to the token-map. */
109447 sqlite3Dequote(z: pItem->zEName);
109448 if( IN_RENAME_OBJECT ){
109449 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
109450 }
109451 }
109452 }
109453}
109454
109455/*
109456** Set the ExprList.a[].zSpan element of the most recently added item
109457** on the expression list.
109458**
109459** pList might be NULL following an OOM error. But pSpan should never be
109460** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
109461** is set.
109462*/
109463SQLITE_PRIVATE void sqlite3ExprListSetSpan(
109464 Parse *pParse, /* Parsing context */
109465 ExprList *pList, /* List to which to add the span. */
109466 const char *zStart, /* Start of the span */
109467 const char *zEnd /* End of the span */
109468){
109469 sqlite3 *db = pParse->db;
109470 assert( pList!=0 || db->mallocFailed!=0 );
109471 if( pList ){
109472 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
109473 assert( pList->nExpr>0 );
109474 if( pItem->zEName==0 ){
109475 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
109476 pItem->fg.eEName = ENAME_SPAN;
109477 }
109478 }
109479}
109480
109481/*
109482** If the expression list pEList contains more than iLimit elements,
109483** leave an error message in pParse.
109484*/
109485SQLITE_PRIVATE void sqlite3ExprListCheckLength(
109486 Parse *pParse,
109487 ExprList *pEList,
109488 const char *zObject
109489){
109490 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
109491 testcase( pEList && pEList->nExpr==mx );
109492 testcase( pEList && pEList->nExpr==mx+1 );
109493 if( pEList && pEList->nExpr>mx ){
109494 sqlite3ErrorMsg(pParse, zFormat: "too many columns in %s", zObject);
109495 }
109496}
109497
109498/*
109499** Delete an entire expression list.
109500*/
109501static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
109502 int i = pList->nExpr;
109503 struct ExprList_item *pItem = pList->a;
109504 assert( pList->nExpr>0 );
109505 assert( db!=0 );
109506 do{
109507 sqlite3ExprDelete(db, p: pItem->pExpr);
109508 if( pItem->zEName ) sqlite3DbNNFreeNN(db, p: pItem->zEName);
109509 pItem++;
109510 }while( --i>0 );
109511 sqlite3DbNNFreeNN(db, p: pList);
109512}
109513SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
109514 if( pList ) exprListDeleteNN(db, pList);
109515}
109516
109517/*
109518** Return the bitwise-OR of all Expr.flags fields in the given
109519** ExprList.
109520*/
109521SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
109522 int i;
109523 u32 m = 0;
109524 assert( pList!=0 );
109525 for(i=0; i<pList->nExpr; i++){
109526 Expr *pExpr = pList->a[i].pExpr;
109527 assert( pExpr!=0 );
109528 m |= pExpr->flags;
109529 }
109530 return m;
109531}
109532
109533/*
109534** This is a SELECT-node callback for the expression walker that
109535** always "fails". By "fail" in this case, we mean set
109536** pWalker->eCode to zero and abort.
109537**
109538** This callback is used by multiple expression walkers.
109539*/
109540SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
109541 UNUSED_PARAMETER(NotUsed);
109542 pWalker->eCode = 0;
109543 return WRC_Abort;
109544}
109545
109546/*
109547** Check the input string to see if it is "true" or "false" (in any case).
109548**
109549** If the string is.... Return
109550** "true" EP_IsTrue
109551** "false" EP_IsFalse
109552** anything else 0
109553*/
109554SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char *zIn){
109555 if( sqlite3StrICmp(zLeft: zIn, zRight: "true")==0 ) return EP_IsTrue;
109556 if( sqlite3StrICmp(zLeft: zIn, zRight: "false")==0 ) return EP_IsFalse;
109557 return 0;
109558}
109559
109560
109561/*
109562** If the input expression is an ID with the name "true" or "false"
109563** then convert it into an TK_TRUEFALSE term. Return non-zero if
109564** the conversion happened, and zero if the expression is unaltered.
109565*/
109566SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
109567 u32 v;
109568 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
109569 if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue)
109570 && (v = sqlite3IsTrueOrFalse(zIn: pExpr->u.zToken))!=0
109571 ){
109572 pExpr->op = TK_TRUEFALSE;
109573 ExprSetProperty(pExpr, v);
109574 return 1;
109575 }
109576 return 0;
109577}
109578
109579/*
109580** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
109581** and 0 if it is FALSE.
109582*/
109583SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
109584 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr: (Expr*)pExpr);
109585 assert( pExpr->op==TK_TRUEFALSE );
109586 assert( !ExprHasProperty(pExpr, EP_IntValue) );
109587 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
109588 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
109589 return pExpr->u.zToken[4]==0;
109590}
109591
109592/*
109593** If pExpr is an AND or OR expression, try to simplify it by eliminating
109594** terms that are always true or false. Return the simplified expression.
109595** Or return the original expression if no simplification is possible.
109596**
109597** Examples:
109598**
109599** (x<10) AND true => (x<10)
109600** (x<10) AND false => false
109601** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
109602** (x<10) AND (y=22 OR true) => (x<10)
109603** (y=22) OR true => true
109604*/
109605SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
109606 assert( pExpr!=0 );
109607 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
109608 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr: pExpr->pRight);
109609 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr: pExpr->pLeft);
109610 if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){
109611 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
109612 }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){
109613 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
109614 }
109615 }
109616 return pExpr;
109617}
109618
109619
109620/*
109621** These routines are Walker callbacks used to check expressions to
109622** see if they are "constant" for some definition of constant. The
109623** Walker.eCode value determines the type of "constant" we are looking
109624** for.
109625**
109626** These callback routines are used to implement the following:
109627**
109628** sqlite3ExprIsConstant() pWalker->eCode==1
109629** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
109630** sqlite3ExprIsTableConstant() pWalker->eCode==3
109631** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
109632**
109633** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
109634** is found to not be a constant.
109635**
109636** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
109637** expressions in a CREATE TABLE statement. The Walker.eCode value is 5
109638** when parsing an existing schema out of the sqlite_schema table and 4
109639** when processing a new CREATE TABLE statement. A bound parameter raises
109640** an error for new statements, but is silently converted
109641** to NULL for existing schemas. This allows sqlite_schema tables that
109642** contain a bound parameter because they were generated by older versions
109643** of SQLite to be parsed by newer versions of SQLite without raising a
109644** malformed schema error.
109645*/
109646static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
109647
109648 /* If pWalker->eCode is 2 then any term of the expression that comes from
109649 ** the ON or USING clauses of an outer join disqualifies the expression
109650 ** from being considered constant. */
109651 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
109652 pWalker->eCode = 0;
109653 return WRC_Abort;
109654 }
109655
109656 switch( pExpr->op ){
109657 /* Consider functions to be constant if all their arguments are constant
109658 ** and either pWalker->eCode==4 or 5 or the function has the
109659 ** SQLITE_FUNC_CONST flag. */
109660 case TK_FUNCTION:
109661 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
109662 && !ExprHasProperty(pExpr, EP_WinFunc)
109663 ){
109664 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
109665 return WRC_Continue;
109666 }else{
109667 pWalker->eCode = 0;
109668 return WRC_Abort;
109669 }
109670 case TK_ID:
109671 /* Convert "true" or "false" in a DEFAULT clause into the
109672 ** appropriate TK_TRUEFALSE operator */
109673 if( sqlite3ExprIdToTrueFalse(pExpr) ){
109674 return WRC_Prune;
109675 }
109676 /* no break */ deliberate_fall_through
109677 case TK_COLUMN:
109678 case TK_AGG_FUNCTION:
109679 case TK_AGG_COLUMN:
109680 testcase( pExpr->op==TK_ID );
109681 testcase( pExpr->op==TK_COLUMN );
109682 testcase( pExpr->op==TK_AGG_FUNCTION );
109683 testcase( pExpr->op==TK_AGG_COLUMN );
109684 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
109685 return WRC_Continue;
109686 }
109687 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
109688 return WRC_Continue;
109689 }
109690 /* no break */ deliberate_fall_through
109691 case TK_IF_NULL_ROW:
109692 case TK_REGISTER:
109693 case TK_DOT:
109694 testcase( pExpr->op==TK_REGISTER );
109695 testcase( pExpr->op==TK_IF_NULL_ROW );
109696 testcase( pExpr->op==TK_DOT );
109697 pWalker->eCode = 0;
109698 return WRC_Abort;
109699 case TK_VARIABLE:
109700 if( pWalker->eCode==5 ){
109701 /* Silently convert bound parameters that appear inside of CREATE
109702 ** statements into a NULL when parsing the CREATE statement text out
109703 ** of the sqlite_schema table */
109704 pExpr->op = TK_NULL;
109705 }else if( pWalker->eCode==4 ){
109706 /* A bound parameter in a CREATE statement that originates from
109707 ** sqlite3_prepare() causes an error */
109708 pWalker->eCode = 0;
109709 return WRC_Abort;
109710 }
109711 /* no break */ deliberate_fall_through
109712 default:
109713 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
109714 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
109715 return WRC_Continue;
109716 }
109717}
109718static int exprIsConst(Expr *p, int initFlag, int iCur){
109719 Walker w;
109720 w.eCode = initFlag;
109721 w.xExprCallback = exprNodeIsConstant;
109722 w.xSelectCallback = sqlite3SelectWalkFail;
109723#ifdef SQLITE_DEBUG
109724 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
109725#endif
109726 w.u.iCur = iCur;
109727 sqlite3WalkExpr(pWalker: &w, pExpr: p);
109728 return w.eCode;
109729}
109730
109731/*
109732** Walk an expression tree. Return non-zero if the expression is constant
109733** and 0 if it involves variables or function calls.
109734**
109735** For the purposes of this function, a double-quoted string (ex: "abc")
109736** is considered a variable but a single-quoted string (ex: 'abc') is
109737** a constant.
109738*/
109739SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
109740 return exprIsConst(p, initFlag: 1, iCur: 0);
109741}
109742
109743/*
109744** Walk an expression tree. Return non-zero if
109745**
109746** (1) the expression is constant, and
109747** (2) the expression does originate in the ON or USING clause
109748** of a LEFT JOIN, and
109749** (3) the expression does not contain any EP_FixedCol TK_COLUMN
109750** operands created by the constant propagation optimization.
109751**
109752** When this routine returns true, it indicates that the expression
109753** can be added to the pParse->pConstExpr list and evaluated once when
109754** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
109755*/
109756SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
109757 return exprIsConst(p, initFlag: 2, iCur: 0);
109758}
109759
109760/*
109761** Walk an expression tree. Return non-zero if the expression is constant
109762** for any single row of the table with cursor iCur. In other words, the
109763** expression must not refer to any non-deterministic function nor any
109764** table other than iCur.
109765*/
109766SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
109767 return exprIsConst(p, initFlag: 3, iCur);
109768}
109769
109770/*
109771** Check pExpr to see if it is an constraint on the single data source
109772** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
109773** constrains pSrc but does not depend on any other tables or data
109774** sources anywhere else in the query. Return true (non-zero) if pExpr
109775** is a constraint on pSrc only.
109776**
109777** This is an optimization. False negatives will perhaps cause slower
109778** queries, but false positives will yield incorrect answers. So when in
109779** doubt, return 0.
109780**
109781** To be an single-source constraint, the following must be true:
109782**
109783** (1) pExpr cannot refer to any table other than pSrc->iCursor.
109784**
109785** (2) pExpr cannot use subqueries or non-deterministic functions.
109786**
109787** (3) pSrc cannot be part of the left operand for a RIGHT JOIN.
109788** (Is there some way to relax this constraint?)
109789**
109790** (4) If pSrc is the right operand of a LEFT JOIN, then...
109791** (4a) pExpr must come from an ON clause..
109792** (4b) and specifically the ON clause associated with the LEFT JOIN.
109793**
109794** (5) If pSrc is not the right operand of a LEFT JOIN or the left
109795** operand of a RIGHT JOIN, then pExpr must be from the WHERE
109796** clause, not an ON clause.
109797**
109798** (6) Either:
109799**
109800** (6a) pExpr does not originate in an ON or USING clause, or
109801**
109802** (6b) The ON or USING clause from which pExpr is derived is
109803** not to the left of a RIGHT JOIN (or FULL JOIN).
109804**
109805** Without this restriction, accepting pExpr as a single-table
109806** constraint might move the the ON/USING filter expression
109807** from the left side of a RIGHT JOIN over to the right side,
109808** which leads to incorrect answers. See also restriction (9)
109809** on push-down.
109810*/
109811SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(
109812 Expr *pExpr, /* The constraint */
109813 const SrcList *pSrcList, /* Complete FROM clause */
109814 int iSrc /* Which element of pSrcList to use */
109815){
109816 const SrcItem *pSrc = &pSrcList->a[iSrc];
109817 if( pSrc->fg.jointype & JT_LTORJ ){
109818 return 0; /* rule (3) */
109819 }
109820 if( pSrc->fg.jointype & JT_LEFT ){
109821 if( !ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* rule (4a) */
109822 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
109823 }else{
109824 if( ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* rule (5) */
109825 }
109826 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) /* (6a) */
109827 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */
109828 ){
109829 int jj;
109830 for(jj=0; jj<iSrc; jj++){
109831 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
109832 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){
109833 return 0; /* restriction (6) */
109834 }
109835 break;
109836 }
109837 }
109838 }
109839 return sqlite3ExprIsTableConstant(p: pExpr, iCur: pSrc->iCursor); /* rules (1), (2) */
109840}
109841
109842
109843/*
109844** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
109845*/
109846static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
109847 ExprList *pGroupBy = pWalker->u.pGroupBy;
109848 int i;
109849
109850 /* Check if pExpr is identical to any GROUP BY term. If so, consider
109851 ** it constant. */
109852 for(i=0; i<pGroupBy->nExpr; i++){
109853 Expr *p = pGroupBy->a[i].pExpr;
109854 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
109855 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse: pWalker->pParse, pExpr: p);
109856 if( sqlite3IsBinary(pColl) ){
109857 return WRC_Prune;
109858 }
109859 }
109860 }
109861
109862 /* Check if pExpr is a sub-select. If so, consider it variable. */
109863 if( ExprUseXSelect(pExpr) ){
109864 pWalker->eCode = 0;
109865 return WRC_Abort;
109866 }
109867
109868 return exprNodeIsConstant(pWalker, pExpr);
109869}
109870
109871/*
109872** Walk the expression tree passed as the first argument. Return non-zero
109873** if the expression consists entirely of constants or copies of terms
109874** in pGroupBy that sort with the BINARY collation sequence.
109875**
109876** This routine is used to determine if a term of the HAVING clause can
109877** be promoted into the WHERE clause. In order for such a promotion to work,
109878** the value of the HAVING clause term must be the same for all members of
109879** a "group". The requirement that the GROUP BY term must be BINARY
109880** assumes that no other collating sequence will have a finer-grained
109881** grouping than binary. In other words (A=B COLLATE binary) implies
109882** A=B in every other collating sequence. The requirement that the
109883** GROUP BY be BINARY is stricter than necessary. It would also work
109884** to promote HAVING clauses that use the same alternative collating
109885** sequence as the GROUP BY term, but that is much harder to check,
109886** alternative collating sequences are uncommon, and this is only an
109887** optimization, so we take the easy way out and simply require the
109888** GROUP BY to use the BINARY collating sequence.
109889*/
109890SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
109891 Walker w;
109892 w.eCode = 1;
109893 w.xExprCallback = exprNodeIsConstantOrGroupBy;
109894 w.xSelectCallback = 0;
109895 w.u.pGroupBy = pGroupBy;
109896 w.pParse = pParse;
109897 sqlite3WalkExpr(pWalker: &w, pExpr: p);
109898 return w.eCode;
109899}
109900
109901/*
109902** Walk an expression tree for the DEFAULT field of a column definition
109903** in a CREATE TABLE statement. Return non-zero if the expression is
109904** acceptable for use as a DEFAULT. That is to say, return non-zero if
109905** the expression is constant or a function call with constant arguments.
109906** Return and 0 if there are any variables.
109907**
109908** isInit is true when parsing from sqlite_schema. isInit is false when
109909** processing a new CREATE TABLE statement. When isInit is true, parameters
109910** (such as ? or $abc) in the expression are converted into NULL. When
109911** isInit is false, parameters raise an error. Parameters should not be
109912** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
109913** allowed it, so we need to support it when reading sqlite_schema for
109914** backwards compatibility.
109915**
109916** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
109917**
109918** For the purposes of this function, a double-quoted string (ex: "abc")
109919** is considered a variable but a single-quoted string (ex: 'abc') is
109920** a constant.
109921*/
109922SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
109923 assert( isInit==0 || isInit==1 );
109924 return exprIsConst(p, initFlag: 4+isInit, iCur: 0);
109925}
109926
109927#ifdef SQLITE_ENABLE_CURSOR_HINTS
109928/*
109929** Walk an expression tree. Return 1 if the expression contains a
109930** subquery of some kind. Return 0 if there are no subqueries.
109931*/
109932SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
109933 Walker w;
109934 w.eCode = 1;
109935 w.xExprCallback = sqlite3ExprWalkNoop;
109936 w.xSelectCallback = sqlite3SelectWalkFail;
109937#ifdef SQLITE_DEBUG
109938 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
109939#endif
109940 sqlite3WalkExpr(&w, p);
109941 return w.eCode==0;
109942}
109943#endif
109944
109945/*
109946** If the expression p codes a constant integer that is small enough
109947** to fit in a 32-bit integer, return 1 and put the value of the integer
109948** in *pValue. If the expression is not an integer or if it is too big
109949** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
109950*/
109951SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue){
109952 int rc = 0;
109953 if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */
109954
109955 /* If an expression is an integer literal that fits in a signed 32-bit
109956 ** integer, then the EP_IntValue flag will have already been set */
109957 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
109958 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
109959
109960 if( p->flags & EP_IntValue ){
109961 *pValue = p->u.iValue;
109962 return 1;
109963 }
109964 switch( p->op ){
109965 case TK_UPLUS: {
109966 rc = sqlite3ExprIsInteger(p: p->pLeft, pValue);
109967 break;
109968 }
109969 case TK_UMINUS: {
109970 int v = 0;
109971 if( sqlite3ExprIsInteger(p: p->pLeft, pValue: &v) ){
109972 assert( ((unsigned int)v)!=0x80000000 );
109973 *pValue = -v;
109974 rc = 1;
109975 }
109976 break;
109977 }
109978 default: break;
109979 }
109980 return rc;
109981}
109982
109983/*
109984** Return FALSE if there is no chance that the expression can be NULL.
109985**
109986** If the expression might be NULL or if the expression is too complex
109987** to tell return TRUE.
109988**
109989** This routine is used as an optimization, to skip OP_IsNull opcodes
109990** when we know that a value cannot be NULL. Hence, a false positive
109991** (returning TRUE when in fact the expression can never be NULL) might
109992** be a small performance hit but is otherwise harmless. On the other
109993** hand, a false negative (returning FALSE when the result could be NULL)
109994** will likely result in an incorrect answer. So when in doubt, return
109995** TRUE.
109996*/
109997SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
109998 u8 op;
109999 assert( p!=0 );
110000 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
110001 p = p->pLeft;
110002 assert( p!=0 );
110003 }
110004 op = p->op;
110005 if( op==TK_REGISTER ) op = p->op2;
110006 switch( op ){
110007 case TK_INTEGER:
110008 case TK_STRING:
110009 case TK_FLOAT:
110010 case TK_BLOB:
110011 return 0;
110012 case TK_COLUMN:
110013 assert( ExprUseYTab(p) );
110014 return ExprHasProperty(p, EP_CanBeNull) ||
110015 p->y.pTab==0 || /* Reference to column of index on expression */
110016 (p->iColumn>=0
110017 && p->y.pTab->aCol!=0 /* Possible due to prior error */
110018 && p->y.pTab->aCol[p->iColumn].notNull==0);
110019 default:
110020 return 1;
110021 }
110022}
110023
110024/*
110025** Return TRUE if the given expression is a constant which would be
110026** unchanged by OP_Affinity with the affinity given in the second
110027** argument.
110028**
110029** This routine is used to determine if the OP_Affinity operation
110030** can be omitted. When in doubt return FALSE. A false negative
110031** is harmless. A false positive, however, can result in the wrong
110032** answer.
110033*/
110034SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
110035 u8 op;
110036 int unaryMinus = 0;
110037 if( aff==SQLITE_AFF_BLOB ) return 1;
110038 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
110039 if( p->op==TK_UMINUS ) unaryMinus = 1;
110040 p = p->pLeft;
110041 }
110042 op = p->op;
110043 if( op==TK_REGISTER ) op = p->op2;
110044 switch( op ){
110045 case TK_INTEGER: {
110046 return aff>=SQLITE_AFF_NUMERIC;
110047 }
110048 case TK_FLOAT: {
110049 return aff>=SQLITE_AFF_NUMERIC;
110050 }
110051 case TK_STRING: {
110052 return !unaryMinus && aff==SQLITE_AFF_TEXT;
110053 }
110054 case TK_BLOB: {
110055 return !unaryMinus;
110056 }
110057 case TK_COLUMN: {
110058 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
110059 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
110060 }
110061 default: {
110062 return 0;
110063 }
110064 }
110065}
110066
110067/*
110068** Return TRUE if the given string is a row-id column name.
110069*/
110070SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
110071 if( sqlite3StrICmp(zLeft: z, zRight: "_ROWID_")==0 ) return 1;
110072 if( sqlite3StrICmp(zLeft: z, zRight: "ROWID")==0 ) return 1;
110073 if( sqlite3StrICmp(zLeft: z, zRight: "OID")==0 ) return 1;
110074 return 0;
110075}
110076
110077/*
110078** pX is the RHS of an IN operator. If pX is a SELECT statement
110079** that can be simplified to a direct table access, then return
110080** a pointer to the SELECT statement. If pX is not a SELECT statement,
110081** or if the SELECT statement needs to be materialized into a transient
110082** table, then return NULL.
110083*/
110084#ifndef SQLITE_OMIT_SUBQUERY
110085static Select *isCandidateForInOpt(const Expr *pX){
110086 Select *p;
110087 SrcList *pSrc;
110088 ExprList *pEList;
110089 Table *pTab;
110090 int i;
110091 if( !ExprUseXSelect(pX) ) return 0; /* Not a subquery */
110092 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
110093 p = pX->x.pSelect;
110094 if( p->pPrior ) return 0; /* Not a compound SELECT */
110095 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
110096 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
110097 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
110098 return 0; /* No DISTINCT keyword and no aggregate functions */
110099 }
110100 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
110101 if( p->pLimit ) return 0; /* Has no LIMIT clause */
110102 if( p->pWhere ) return 0; /* Has no WHERE clause */
110103 pSrc = p->pSrc;
110104 assert( pSrc!=0 );
110105 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
110106 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
110107 pTab = pSrc->a[0].pTab;
110108 assert( pTab!=0 );
110109 assert( !IsView(pTab) ); /* FROM clause is not a view */
110110 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
110111 pEList = p->pEList;
110112 assert( pEList!=0 );
110113 /* All SELECT results must be columns. */
110114 for(i=0; i<pEList->nExpr; i++){
110115 Expr *pRes = pEList->a[i].pExpr;
110116 if( pRes->op!=TK_COLUMN ) return 0;
110117 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
110118 }
110119 return p;
110120}
110121#endif /* SQLITE_OMIT_SUBQUERY */
110122
110123#ifndef SQLITE_OMIT_SUBQUERY
110124/*
110125** Generate code that checks the left-most column of index table iCur to see if
110126** it contains any NULL entries. Cause the register at regHasNull to be set
110127** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
110128** to be set to NULL if iCur contains one or more NULL values.
110129*/
110130static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
110131 int addr1;
110132 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regHasNull);
110133 addr1 = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iCur); VdbeCoverage(v);
110134 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iCur, p2: 0, p3: regHasNull);
110135 sqlite3VdbeChangeP5(p: v, OPFLAG_TYPEOFARG);
110136 VdbeComment((v, "first_entry_in(%d)", iCur));
110137 sqlite3VdbeJumpHere(p: v, addr: addr1);
110138}
110139#endif
110140
110141
110142#ifndef SQLITE_OMIT_SUBQUERY
110143/*
110144** The argument is an IN operator with a list (not a subquery) on the
110145** right-hand side. Return TRUE if that list is constant.
110146*/
110147static int sqlite3InRhsIsConstant(Expr *pIn){
110148 Expr *pLHS;
110149 int res;
110150 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
110151 pLHS = pIn->pLeft;
110152 pIn->pLeft = 0;
110153 res = sqlite3ExprIsConstant(p: pIn);
110154 pIn->pLeft = pLHS;
110155 return res;
110156}
110157#endif
110158
110159/*
110160** This function is used by the implementation of the IN (...) operator.
110161** The pX parameter is the expression on the RHS of the IN operator, which
110162** might be either a list of expressions or a subquery.
110163**
110164** The job of this routine is to find or create a b-tree object that can
110165** be used either to test for membership in the RHS set or to iterate through
110166** all members of the RHS set, skipping duplicates.
110167**
110168** A cursor is opened on the b-tree object that is the RHS of the IN operator
110169** and the *piTab parameter is set to the index of that cursor.
110170**
110171** The returned value of this function indicates the b-tree type, as follows:
110172**
110173** IN_INDEX_ROWID - The cursor was opened on a database table.
110174** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
110175** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
110176** IN_INDEX_EPH - The cursor was opened on a specially created and
110177** populated ephemeral table.
110178** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
110179** implemented as a sequence of comparisons.
110180**
110181** An existing b-tree might be used if the RHS expression pX is a simple
110182** subquery such as:
110183**
110184** SELECT <column1>, <column2>... FROM <table>
110185**
110186** If the RHS of the IN operator is a list or a more complex subquery, then
110187** an ephemeral table might need to be generated from the RHS and then
110188** pX->iTable made to point to the ephemeral table instead of an
110189** existing table. In this case, the creation and initialization of the
110190** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag
110191** will be set on pX and the pX->y.sub fields will be set to show where
110192** the subroutine is coded.
110193**
110194** The inFlags parameter must contain, at a minimum, one of the bits
110195** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
110196** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
110197** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
110198** be used to loop over all values of the RHS of the IN operator.
110199**
110200** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
110201** through the set members) then the b-tree must not contain duplicates.
110202** An ephemeral table will be created unless the selected columns are guaranteed
110203** to be unique - either because it is an INTEGER PRIMARY KEY or due to
110204** a UNIQUE constraint or index.
110205**
110206** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
110207** for fast set membership tests) then an ephemeral table must
110208** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
110209** index can be found with the specified <columns> as its left-most.
110210**
110211** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
110212** if the RHS of the IN operator is a list (not a subquery) then this
110213** routine might decide that creating an ephemeral b-tree for membership
110214** testing is too expensive and return IN_INDEX_NOOP. In that case, the
110215** calling routine should implement the IN operator using a sequence
110216** of Eq or Ne comparison operations.
110217**
110218** When the b-tree is being used for membership tests, the calling function
110219** might need to know whether or not the RHS side of the IN operator
110220** contains a NULL. If prRhsHasNull is not a NULL pointer and
110221** if there is any chance that the (...) might contain a NULL value at
110222** runtime, then a register is allocated and the register number written
110223** to *prRhsHasNull. If there is no chance that the (...) contains a
110224** NULL value, then *prRhsHasNull is left unchanged.
110225**
110226** If a register is allocated and its location stored in *prRhsHasNull, then
110227** the value in that register will be NULL if the b-tree contains one or more
110228** NULL values, and it will be some non-NULL value if the b-tree contains no
110229** NULL values.
110230**
110231** If the aiMap parameter is not NULL, it must point to an array containing
110232** one element for each column returned by the SELECT statement on the RHS
110233** of the IN(...) operator. The i'th entry of the array is populated with the
110234** offset of the index column that matches the i'th column returned by the
110235** SELECT. For example, if the expression and selected index are:
110236**
110237** (?,?,?) IN (SELECT a, b, c FROM t1)
110238** CREATE INDEX i1 ON t1(b, c, a);
110239**
110240** then aiMap[] is populated with {2, 0, 1}.
110241*/
110242#ifndef SQLITE_OMIT_SUBQUERY
110243SQLITE_PRIVATE int sqlite3FindInIndex(
110244 Parse *pParse, /* Parsing context */
110245 Expr *pX, /* The IN expression */
110246 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
110247 int *prRhsHasNull, /* Register holding NULL status. See notes */
110248 int *aiMap, /* Mapping from Index fields to RHS fields */
110249 int *piTab /* OUT: index to use */
110250){
110251 Select *p; /* SELECT to the right of IN operator */
110252 int eType = 0; /* Type of RHS table. IN_INDEX_* */
110253 int iTab; /* Cursor of the RHS table */
110254 int mustBeUnique; /* True if RHS must be unique */
110255 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
110256
110257 assert( pX->op==TK_IN );
110258 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
110259 iTab = pParse->nTab++;
110260
110261 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
110262 ** whether or not the SELECT result contains NULL values, check whether
110263 ** or not NULL is actually possible (it may not be, for example, due
110264 ** to NOT NULL constraints in the schema). If no NULL values are possible,
110265 ** set prRhsHasNull to 0 before continuing. */
110266 if( prRhsHasNull && ExprUseXSelect(pX) ){
110267 int i;
110268 ExprList *pEList = pX->x.pSelect->pEList;
110269 for(i=0; i<pEList->nExpr; i++){
110270 if( sqlite3ExprCanBeNull(p: pEList->a[i].pExpr) ) break;
110271 }
110272 if( i==pEList->nExpr ){
110273 prRhsHasNull = 0;
110274 }
110275 }
110276
110277 /* Check to see if an existing table or index can be used to
110278 ** satisfy the query. This is preferable to generating a new
110279 ** ephemeral table. */
110280 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
110281 sqlite3 *db = pParse->db; /* Database connection */
110282 Table *pTab; /* Table <table>. */
110283 int iDb; /* Database idx for pTab */
110284 ExprList *pEList = p->pEList;
110285 int nExpr = pEList->nExpr;
110286
110287 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
110288 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
110289 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
110290 pTab = p->pSrc->a[0].pTab;
110291
110292 /* Code an OP_Transaction and OP_TableLock for <table>. */
110293 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110294 assert( iDb>=0 && iDb<SQLITE_MAX_DB );
110295 sqlite3CodeVerifySchema(pParse, iDb);
110296 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
110297
110298 assert(v); /* sqlite3GetVdbe() has always been previously called */
110299 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
110300 /* The "x IN (SELECT rowid FROM table)" case */
110301 int iAddr = sqlite3VdbeAddOp0(p: v, OP_Once);
110302 VdbeCoverage(v);
110303
110304 sqlite3OpenTable(pParse, iCur: iTab, iDb, pTab, OP_OpenRead);
110305 eType = IN_INDEX_ROWID;
110306 ExplainQueryPlan((pParse, 0,
110307 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
110308 sqlite3VdbeJumpHere(p: v, addr: iAddr);
110309 }else{
110310 Index *pIdx; /* Iterator variable */
110311 int affinity_ok = 1;
110312 int i;
110313
110314 /* Check that the affinity that will be used to perform each
110315 ** comparison is the same as the affinity of each column in table
110316 ** on the RHS of the IN operator. If it not, it is not possible to
110317 ** use any index of the RHS table. */
110318 for(i=0; i<nExpr && affinity_ok; i++){
110319 Expr *pLhs = sqlite3VectorFieldSubexpr(pVector: pX->pLeft, i);
110320 int iCol = pEList->a[i].pExpr->iColumn;
110321 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
110322 char cmpaff = sqlite3CompareAffinity(pExpr: pLhs, aff2: idxaff);
110323 testcase( cmpaff==SQLITE_AFF_BLOB );
110324 testcase( cmpaff==SQLITE_AFF_TEXT );
110325 switch( cmpaff ){
110326 case SQLITE_AFF_BLOB:
110327 break;
110328 case SQLITE_AFF_TEXT:
110329 /* sqlite3CompareAffinity() only returns TEXT if one side or the
110330 ** other has no affinity and the other side is TEXT. Hence,
110331 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
110332 ** and for the term on the LHS of the IN to have no affinity. */
110333 assert( idxaff==SQLITE_AFF_TEXT );
110334 break;
110335 default:
110336 affinity_ok = sqlite3IsNumericAffinity(idxaff);
110337 }
110338 }
110339
110340 if( affinity_ok ){
110341 /* Search for an existing index that will work for this IN operator */
110342 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
110343 Bitmask colUsed; /* Columns of the index used */
110344 Bitmask mCol; /* Mask for the current column */
110345 if( pIdx->nColumn<nExpr ) continue;
110346 if( pIdx->pPartIdxWhere!=0 ) continue;
110347 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
110348 ** BITMASK(nExpr) without overflowing */
110349 testcase( pIdx->nColumn==BMS-2 );
110350 testcase( pIdx->nColumn==BMS-1 );
110351 if( pIdx->nColumn>=BMS-1 ) continue;
110352 if( mustBeUnique ){
110353 if( pIdx->nKeyCol>nExpr
110354 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
110355 ){
110356 continue; /* This index is not unique over the IN RHS columns */
110357 }
110358 }
110359
110360 colUsed = 0; /* Columns of index used so far */
110361 for(i=0; i<nExpr; i++){
110362 Expr *pLhs = sqlite3VectorFieldSubexpr(pVector: pX->pLeft, i);
110363 Expr *pRhs = pEList->a[i].pExpr;
110364 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLeft: pLhs, pRight: pRhs);
110365 int j;
110366
110367 for(j=0; j<nExpr; j++){
110368 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
110369 assert( pIdx->azColl[j] );
110370 if( pReq!=0 && sqlite3StrICmp(zLeft: pReq->zName, zRight: pIdx->azColl[j])!=0 ){
110371 continue;
110372 }
110373 break;
110374 }
110375 if( j==nExpr ) break;
110376 mCol = MASKBIT(j);
110377 if( mCol & colUsed ) break; /* Each column used only once */
110378 colUsed |= mCol;
110379 if( aiMap ) aiMap[i] = j;
110380 }
110381
110382 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
110383 if( colUsed==(MASKBIT(nExpr)-1) ){
110384 /* If we reach this point, that means the index pIdx is usable */
110385 int iAddr = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
110386 ExplainQueryPlan((pParse, 0,
110387 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
110388 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iTab, p2: pIdx->tnum, p3: iDb);
110389 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
110390 VdbeComment((v, "%s", pIdx->zName));
110391 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
110392 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
110393
110394 if( prRhsHasNull ){
110395#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
110396 i64 mask = (1<<nExpr)-1;
110397 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
110398 iTab, 0, 0, (u8*)&mask, P4_INT64);
110399#endif
110400 *prRhsHasNull = ++pParse->nMem;
110401 if( nExpr==1 ){
110402 sqlite3SetHasNullFlag(v, iCur: iTab, regHasNull: *prRhsHasNull);
110403 }
110404 }
110405 sqlite3VdbeJumpHere(p: v, addr: iAddr);
110406 }
110407 } /* End loop over indexes */
110408 } /* End if( affinity_ok ) */
110409 } /* End if not an rowid index */
110410 } /* End attempt to optimize using an index */
110411
110412 /* If no preexisting index is available for the IN clause
110413 ** and IN_INDEX_NOOP is an allowed reply
110414 ** and the RHS of the IN operator is a list, not a subquery
110415 ** and the RHS is not constant or has two or fewer terms,
110416 ** then it is not worth creating an ephemeral table to evaluate
110417 ** the IN operator so return IN_INDEX_NOOP.
110418 */
110419 if( eType==0
110420 && (inFlags & IN_INDEX_NOOP_OK)
110421 && ExprUseXList(pX)
110422 && (!sqlite3InRhsIsConstant(pIn: pX) || pX->x.pList->nExpr<=2)
110423 ){
110424 pParse->nTab--; /* Back out the allocation of the unused cursor */
110425 iTab = -1; /* Cursor is not allocated */
110426 eType = IN_INDEX_NOOP;
110427 }
110428
110429 if( eType==0 ){
110430 /* Could not find an existing table or index to use as the RHS b-tree.
110431 ** We will have to generate an ephemeral table to do the job.
110432 */
110433 u32 savedNQueryLoop = pParse->nQueryLoop;
110434 int rMayHaveNull = 0;
110435 eType = IN_INDEX_EPH;
110436 if( inFlags & IN_INDEX_LOOP ){
110437 pParse->nQueryLoop = 0;
110438 }else if( prRhsHasNull ){
110439 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
110440 }
110441 assert( pX->op==TK_IN );
110442 sqlite3CodeRhsOfIN(pParse, pX, iTab);
110443 if( rMayHaveNull ){
110444 sqlite3SetHasNullFlag(v, iCur: iTab, regHasNull: rMayHaveNull);
110445 }
110446 pParse->nQueryLoop = savedNQueryLoop;
110447 }
110448
110449 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
110450 int i, n;
110451 n = sqlite3ExprVectorSize(pExpr: pX->pLeft);
110452 for(i=0; i<n; i++) aiMap[i] = i;
110453 }
110454 *piTab = iTab;
110455 return eType;
110456}
110457#endif
110458
110459#ifndef SQLITE_OMIT_SUBQUERY
110460/*
110461** Argument pExpr is an (?, ?...) IN(...) expression. This
110462** function allocates and returns a nul-terminated string containing
110463** the affinities to be used for each column of the comparison.
110464**
110465** It is the responsibility of the caller to ensure that the returned
110466** string is eventually freed using sqlite3DbFree().
110467*/
110468static char *exprINAffinity(Parse *pParse, const Expr *pExpr){
110469 Expr *pLeft = pExpr->pLeft;
110470 int nVal = sqlite3ExprVectorSize(pExpr: pLeft);
110471 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
110472 char *zRet;
110473
110474 assert( pExpr->op==TK_IN );
110475 zRet = sqlite3DbMallocRaw(db: pParse->db, n: nVal+1);
110476 if( zRet ){
110477 int i;
110478 for(i=0; i<nVal; i++){
110479 Expr *pA = sqlite3VectorFieldSubexpr(pVector: pLeft, i);
110480 char a = sqlite3ExprAffinity(pExpr: pA);
110481 if( pSelect ){
110482 zRet[i] = sqlite3CompareAffinity(pExpr: pSelect->pEList->a[i].pExpr, aff2: a);
110483 }else{
110484 zRet[i] = a;
110485 }
110486 }
110487 zRet[nVal] = '\0';
110488 }
110489 return zRet;
110490}
110491#endif
110492
110493#ifndef SQLITE_OMIT_SUBQUERY
110494/*
110495** Load the Parse object passed as the first argument with an error
110496** message of the form:
110497**
110498** "sub-select returns N columns - expected M"
110499*/
110500SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
110501 if( pParse->nErr==0 ){
110502 const char *zFmt = "sub-select returns %d columns - expected %d";
110503 sqlite3ErrorMsg(pParse, zFormat: zFmt, nActual, nExpect);
110504 }
110505}
110506#endif
110507
110508/*
110509** Expression pExpr is a vector that has been used in a context where
110510** it is not permitted. If pExpr is a sub-select vector, this routine
110511** loads the Parse object with a message of the form:
110512**
110513** "sub-select returns N columns - expected 1"
110514**
110515** Or, if it is a regular scalar vector:
110516**
110517** "row value misused"
110518*/
110519SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
110520#ifndef SQLITE_OMIT_SUBQUERY
110521 if( ExprUseXSelect(pExpr) ){
110522 sqlite3SubselectError(pParse, nActual: pExpr->x.pSelect->pEList->nExpr, nExpect: 1);
110523 }else
110524#endif
110525 {
110526 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
110527 }
110528}
110529
110530#ifndef SQLITE_OMIT_SUBQUERY
110531/*
110532** Generate code that will construct an ephemeral table containing all terms
110533** in the RHS of an IN operator. The IN operator can be in either of two
110534** forms:
110535**
110536** x IN (4,5,11) -- IN operator with list on right-hand side
110537** x IN (SELECT a FROM b) -- IN operator with subquery on the right
110538**
110539** The pExpr parameter is the IN operator. The cursor number for the
110540** constructed ephemeral table is returned. The first time the ephemeral
110541** table is computed, the cursor number is also stored in pExpr->iTable,
110542** however the cursor number returned might not be the same, as it might
110543** have been duplicated using OP_OpenDup.
110544**
110545** If the LHS expression ("x" in the examples) is a column value, or
110546** the SELECT statement returns a column value, then the affinity of that
110547** column is used to build the index keys. If both 'x' and the
110548** SELECT... statement are columns, then numeric affinity is used
110549** if either column has NUMERIC or INTEGER affinity. If neither
110550** 'x' nor the SELECT... statement are columns, then numeric affinity
110551** is used.
110552*/
110553SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
110554 Parse *pParse, /* Parsing context */
110555 Expr *pExpr, /* The IN operator */
110556 int iTab /* Use this cursor number */
110557){
110558 int addrOnce = 0; /* Address of the OP_Once instruction at top */
110559 int addr; /* Address of OP_OpenEphemeral instruction */
110560 Expr *pLeft; /* the LHS of the IN operator */
110561 KeyInfo *pKeyInfo = 0; /* Key information */
110562 int nVal; /* Size of vector pLeft */
110563 Vdbe *v; /* The prepared statement under construction */
110564
110565 v = pParse->pVdbe;
110566 assert( v!=0 );
110567
110568 /* The evaluation of the IN must be repeated every time it
110569 ** is encountered if any of the following is true:
110570 **
110571 ** * The right-hand side is a correlated subquery
110572 ** * The right-hand side is an expression list containing variables
110573 ** * We are inside a trigger
110574 **
110575 ** If all of the above are false, then we can compute the RHS just once
110576 ** and reuse it many names.
110577 */
110578 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
110579 /* Reuse of the RHS is allowed */
110580 /* If this routine has already been coded, but the previous code
110581 ** might not have been invoked yet, so invoke it now as a subroutine.
110582 */
110583 if( ExprHasProperty(pExpr, EP_Subrtn) ){
110584 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
110585 if( ExprUseXSelect(pExpr) ){
110586 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
110587 pExpr->x.pSelect->selId));
110588 }
110589 assert( ExprUseYSub(pExpr) );
110590 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pExpr->y.sub.regReturn,
110591 p2: pExpr->y.sub.iAddr);
110592 assert( iTab!=pExpr->iTable );
110593 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: iTab, p2: pExpr->iTable);
110594 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
110595 return;
110596 }
110597
110598 /* Begin coding the subroutine */
110599 assert( !ExprUseYWin(pExpr) );
110600 ExprSetProperty(pExpr, EP_Subrtn);
110601 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
110602 pExpr->y.sub.regReturn = ++pParse->nMem;
110603 pExpr->y.sub.iAddr =
110604 sqlite3VdbeAddOp2(p: v, OP_BeginSubrtn, p1: 0, p2: pExpr->y.sub.regReturn) + 1;
110605
110606 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
110607 }
110608
110609 /* Check to see if this is a vector IN operator */
110610 pLeft = pExpr->pLeft;
110611 nVal = sqlite3ExprVectorSize(pExpr: pLeft);
110612
110613 /* Construct the ephemeral table that will contain the content of
110614 ** RHS of the IN operator.
110615 */
110616 pExpr->iTable = iTab;
110617 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pExpr->iTable, p2: nVal);
110618#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
110619 if( ExprUseXSelect(pExpr) ){
110620 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
110621 }else{
110622 VdbeComment((v, "RHS of IN operator"));
110623 }
110624#endif
110625 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
110626
110627 if( ExprUseXSelect(pExpr) ){
110628 /* Case 1: expr IN (SELECT ...)
110629 **
110630 ** Generate code to write the results of the select into the temporary
110631 ** table allocated and opened above.
110632 */
110633 Select *pSelect = pExpr->x.pSelect;
110634 ExprList *pEList = pSelect->pEList;
110635
110636 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
110637 addrOnce?"":"CORRELATED ", pSelect->selId
110638 ));
110639 /* If the LHS and RHS of the IN operator do not match, that
110640 ** error will have been caught long before we reach this point. */
110641 if( ALWAYS(pEList->nExpr==nVal) ){
110642 Select *pCopy;
110643 SelectDest dest;
110644 int i;
110645 int rc;
110646 sqlite3SelectDestInit(&dest, SRT_Set, iTab);
110647 dest.zAffSdst = exprINAffinity(pParse, pExpr);
110648 pSelect->iLimit = 0;
110649 testcase( pSelect->selFlags & SF_Distinct );
110650 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
110651 pCopy = sqlite3SelectDup(db: pParse->db, pDup: pSelect, flags: 0);
110652 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
110653 sqlite3SelectDelete(pParse->db, pCopy);
110654 sqlite3DbFree(db: pParse->db, p: dest.zAffSdst);
110655 if( rc ){
110656 sqlite3KeyInfoUnref(pKeyInfo);
110657 return;
110658 }
110659 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
110660 assert( pEList!=0 );
110661 assert( pEList->nExpr>0 );
110662 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
110663 for(i=0; i<nVal; i++){
110664 Expr *p = sqlite3VectorFieldSubexpr(pVector: pLeft, i);
110665 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
110666 pParse, pLeft: p, pRight: pEList->a[i].pExpr
110667 );
110668 }
110669 }
110670 }else if( ALWAYS(pExpr->x.pList!=0) ){
110671 /* Case 2: expr IN (exprlist)
110672 **
110673 ** For each expression, build an index key from the evaluation and
110674 ** store it in the temporary table. If <expr> is a column, then use
110675 ** that columns affinity when building index keys. If <expr> is not
110676 ** a column, use numeric affinity.
110677 */
110678 char affinity; /* Affinity of the LHS of the IN */
110679 int i;
110680 ExprList *pList = pExpr->x.pList;
110681 struct ExprList_item *pItem;
110682 int r1, r2;
110683 affinity = sqlite3ExprAffinity(pExpr: pLeft);
110684 if( affinity<=SQLITE_AFF_NONE ){
110685 affinity = SQLITE_AFF_BLOB;
110686 }else if( affinity==SQLITE_AFF_REAL ){
110687 affinity = SQLITE_AFF_NUMERIC;
110688 }
110689 if( pKeyInfo ){
110690 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
110691 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr: pExpr->pLeft);
110692 }
110693
110694 /* Loop through each expression in <exprlist>. */
110695 r1 = sqlite3GetTempReg(pParse);
110696 r2 = sqlite3GetTempReg(pParse);
110697 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
110698 Expr *pE2 = pItem->pExpr;
110699
110700 /* If the expression is not constant then we will need to
110701 ** disable the test that was generated above that makes sure
110702 ** this code only executes once. Because for a non-constant
110703 ** expression we need to rerun this code each time.
110704 */
110705 if( addrOnce && !sqlite3ExprIsConstant(p: pE2) ){
110706 sqlite3VdbeChangeToNoop(p: v, addr: addrOnce-1);
110707 sqlite3VdbeChangeToNoop(p: v, addr: addrOnce);
110708 ExprClearProperty(pExpr, EP_Subrtn);
110709 addrOnce = 0;
110710 }
110711
110712 /* Evaluate the expression and insert it into the temp table */
110713 sqlite3ExprCode(pParse, pE2, r1);
110714 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: r1, p2: 1, p3: r2, zP4: &affinity, p4type: 1);
110715 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iTab, p2: r2, p3: r1, p4: 1);
110716 }
110717 sqlite3ReleaseTempReg(pParse, r1);
110718 sqlite3ReleaseTempReg(pParse, r2);
110719 }
110720 if( pKeyInfo ){
110721 sqlite3VdbeChangeP4(p: v, addr, zP4: (void *)pKeyInfo, P4_KEYINFO);
110722 }
110723 if( addrOnce ){
110724 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: iTab);
110725 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
110726 /* Subroutine return */
110727 assert( ExprUseYSub(pExpr) );
110728 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
110729 || pParse->nErr );
110730 sqlite3VdbeAddOp3(p: v, OP_Return, p1: pExpr->y.sub.regReturn,
110731 p2: pExpr->y.sub.iAddr, p3: 1);
110732 VdbeCoverage(v);
110733 sqlite3ClearTempRegCache(pParse);
110734 }
110735}
110736#endif /* SQLITE_OMIT_SUBQUERY */
110737
110738/*
110739** Generate code for scalar subqueries used as a subquery expression
110740** or EXISTS operator:
110741**
110742** (SELECT a FROM b) -- subquery
110743** EXISTS (SELECT a FROM b) -- EXISTS subquery
110744**
110745** The pExpr parameter is the SELECT or EXISTS operator to be coded.
110746**
110747** Return the register that holds the result. For a multi-column SELECT,
110748** the result is stored in a contiguous array of registers and the
110749** return value is the register of the left-most result column.
110750** Return 0 if an error occurs.
110751*/
110752#ifndef SQLITE_OMIT_SUBQUERY
110753SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
110754 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
110755 int rReg = 0; /* Register storing resulting */
110756 Select *pSel; /* SELECT statement to encode */
110757 SelectDest dest; /* How to deal with SELECT result */
110758 int nReg; /* Registers to allocate */
110759 Expr *pLimit; /* New limit expression */
110760#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
110761 int addrExplain; /* Address of OP_Explain instruction */
110762#endif
110763
110764 Vdbe *v = pParse->pVdbe;
110765 assert( v!=0 );
110766 if( pParse->nErr ) return 0;
110767 testcase( pExpr->op==TK_EXISTS );
110768 testcase( pExpr->op==TK_SELECT );
110769 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
110770 assert( ExprUseXSelect(pExpr) );
110771 pSel = pExpr->x.pSelect;
110772
110773 /* If this routine has already been coded, then invoke it as a
110774 ** subroutine. */
110775 if( ExprHasProperty(pExpr, EP_Subrtn) ){
110776 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
110777 assert( ExprUseYSub(pExpr) );
110778 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pExpr->y.sub.regReturn,
110779 p2: pExpr->y.sub.iAddr);
110780 return pExpr->iTable;
110781 }
110782
110783 /* Begin coding the subroutine */
110784 assert( !ExprUseYWin(pExpr) );
110785 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) );
110786 ExprSetProperty(pExpr, EP_Subrtn);
110787 pExpr->y.sub.regReturn = ++pParse->nMem;
110788 pExpr->y.sub.iAddr =
110789 sqlite3VdbeAddOp2(p: v, OP_BeginSubrtn, p1: 0, p2: pExpr->y.sub.regReturn) + 1;
110790
110791 /* The evaluation of the EXISTS/SELECT must be repeated every time it
110792 ** is encountered if any of the following is true:
110793 **
110794 ** * The right-hand side is a correlated subquery
110795 ** * The right-hand side is an expression list containing variables
110796 ** * We are inside a trigger
110797 **
110798 ** If all of the above are false, then we can run this code just once
110799 ** save the results, and reuse the same result on subsequent invocations.
110800 */
110801 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
110802 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
110803 }
110804
110805 /* For a SELECT, generate code to put the values for all columns of
110806 ** the first row into an array of registers and return the index of
110807 ** the first register.
110808 **
110809 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
110810 ** into a register and return that register number.
110811 **
110812 ** In both cases, the query is augmented with "LIMIT 1". Any
110813 ** preexisting limit is discarded in place of the new LIMIT 1.
110814 */
110815 ExplainQueryPlan2(addrExplain, (pParse, 1, "%sSCALAR SUBQUERY %d",
110816 addrOnce?"":"CORRELATED ", pSel->selId));
110817 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
110818 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
110819 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
110820 pParse->nMem += nReg;
110821 if( pExpr->op==TK_SELECT ){
110822 dest.eDest = SRT_Mem;
110823 dest.iSdst = dest.iSDParm;
110824 dest.nSdst = nReg;
110825 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: dest.iSDParm, p3: dest.iSDParm+nReg-1);
110826 VdbeComment((v, "Init subquery result"));
110827 }else{
110828 dest.eDest = SRT_Exists;
110829 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: dest.iSDParm);
110830 VdbeComment((v, "Init EXISTS result"));
110831 }
110832 if( pSel->pLimit ){
110833 /* The subquery already has a limit. If the pre-existing limit is X
110834 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
110835 sqlite3 *db = pParse->db;
110836 pLimit = sqlite3Expr(db, TK_INTEGER, zToken: "0");
110837 if( pLimit ){
110838 pLimit->affExpr = SQLITE_AFF_NUMERIC;
110839 pLimit = sqlite3PExpr(pParse, TK_NE,
110840 pLeft: sqlite3ExprDup(db, p: pSel->pLimit->pLeft, flags: 0), pRight: pLimit);
110841 }
110842 sqlite3ExprDeferredDelete(pParse, pExpr: pSel->pLimit->pLeft);
110843 pSel->pLimit->pLeft = pLimit;
110844 }else{
110845 /* If there is no pre-existing limit add a limit of 1 */
110846 pLimit = sqlite3Expr(db: pParse->db, TK_INTEGER, zToken: "1");
110847 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLeft: pLimit, pRight: 0);
110848 }
110849 pSel->iLimit = 0;
110850 if( sqlite3Select(pParse, pSel, &dest) ){
110851 pExpr->op2 = pExpr->op;
110852 pExpr->op = TK_ERROR;
110853 return 0;
110854 }
110855 pExpr->iTable = rReg = dest.iSDParm;
110856 ExprSetVVAProperty(pExpr, EP_NoReduce);
110857 if( addrOnce ){
110858 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
110859 }
110860 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
110861
110862 /* Subroutine return */
110863 assert( ExprUseYSub(pExpr) );
110864 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
110865 || pParse->nErr );
110866 sqlite3VdbeAddOp3(p: v, OP_Return, p1: pExpr->y.sub.regReturn,
110867 p2: pExpr->y.sub.iAddr, p3: 1);
110868 VdbeCoverage(v);
110869 sqlite3ClearTempRegCache(pParse);
110870 return rReg;
110871}
110872#endif /* SQLITE_OMIT_SUBQUERY */
110873
110874#ifndef SQLITE_OMIT_SUBQUERY
110875/*
110876** Expr pIn is an IN(...) expression. This function checks that the
110877** sub-select on the RHS of the IN() operator has the same number of
110878** columns as the vector on the LHS. Or, if the RHS of the IN() is not
110879** a sub-query, that the LHS is a vector of size 1.
110880*/
110881SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
110882 int nVector = sqlite3ExprVectorSize(pExpr: pIn->pLeft);
110883 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
110884 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
110885 sqlite3SubselectError(pParse, nActual: pIn->x.pSelect->pEList->nExpr, nExpect: nVector);
110886 return 1;
110887 }
110888 }else if( nVector!=1 ){
110889 sqlite3VectorErrorMsg(pParse, pExpr: pIn->pLeft);
110890 return 1;
110891 }
110892 return 0;
110893}
110894#endif
110895
110896#ifndef SQLITE_OMIT_SUBQUERY
110897/*
110898** Generate code for an IN expression.
110899**
110900** x IN (SELECT ...)
110901** x IN (value, value, ...)
110902**
110903** The left-hand side (LHS) is a scalar or vector expression. The
110904** right-hand side (RHS) is an array of zero or more scalar values, or a
110905** subquery. If the RHS is a subquery, the number of result columns must
110906** match the number of columns in the vector on the LHS. If the RHS is
110907** a list of values, the LHS must be a scalar.
110908**
110909** The IN operator is true if the LHS value is contained within the RHS.
110910** The result is false if the LHS is definitely not in the RHS. The
110911** result is NULL if the presence of the LHS in the RHS cannot be
110912** determined due to NULLs.
110913**
110914** This routine generates code that jumps to destIfFalse if the LHS is not
110915** contained within the RHS. If due to NULLs we cannot determine if the LHS
110916** is contained in the RHS then jump to destIfNull. If the LHS is contained
110917** within the RHS then fall through.
110918**
110919** See the separate in-operator.md documentation file in the canonical
110920** SQLite source tree for additional information.
110921*/
110922static void sqlite3ExprCodeIN(
110923 Parse *pParse, /* Parsing and code generating context */
110924 Expr *pExpr, /* The IN expression */
110925 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
110926 int destIfNull /* Jump here if the results are unknown due to NULLs */
110927){
110928 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
110929 int eType; /* Type of the RHS */
110930 int rLhs; /* Register(s) holding the LHS values */
110931 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
110932 Vdbe *v; /* Statement under construction */
110933 int *aiMap = 0; /* Map from vector field to index column */
110934 char *zAff = 0; /* Affinity string for comparisons */
110935 int nVector; /* Size of vectors for this IN operator */
110936 int iDummy; /* Dummy parameter to exprCodeVector() */
110937 Expr *pLeft; /* The LHS of the IN operator */
110938 int i; /* loop counter */
110939 int destStep2; /* Where to jump when NULLs seen in step 2 */
110940 int destStep6 = 0; /* Start of code for Step 6 */
110941 int addrTruthOp; /* Address of opcode that determines the IN is true */
110942 int destNotNull; /* Jump here if a comparison is not true in step 6 */
110943 int addrTop; /* Top of the step-6 loop */
110944 int iTab = 0; /* Index to use */
110945 u8 okConstFactor = pParse->okConstFactor;
110946
110947 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
110948 pLeft = pExpr->pLeft;
110949 if( sqlite3ExprCheckIN(pParse, pIn: pExpr) ) return;
110950 zAff = exprINAffinity(pParse, pExpr);
110951 nVector = sqlite3ExprVectorSize(pExpr: pExpr->pLeft);
110952 aiMap = (int*)sqlite3DbMallocZero(
110953 db: pParse->db, n: nVector*(sizeof(int) + sizeof(char)) + 1
110954 );
110955 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
110956
110957 /* Attempt to compute the RHS. After this step, if anything other than
110958 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
110959 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
110960 ** the RHS has not yet been coded. */
110961 v = pParse->pVdbe;
110962 assert( v!=0 ); /* OOM detected prior to this routine */
110963 VdbeNoopComment((v, "begin IN expr"));
110964 eType = sqlite3FindInIndex(pParse, pX: pExpr,
110965 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
110966 prRhsHasNull: destIfFalse==destIfNull ? 0 : &rRhsHasNull,
110967 aiMap, piTab: &iTab);
110968
110969 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
110970 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
110971 );
110972#ifdef SQLITE_DEBUG
110973 /* Confirm that aiMap[] contains nVector integer values between 0 and
110974 ** nVector-1. */
110975 for(i=0; i<nVector; i++){
110976 int j, cnt;
110977 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
110978 assert( cnt==1 );
110979 }
110980#endif
110981
110982 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
110983 ** vector, then it is stored in an array of nVector registers starting
110984 ** at r1.
110985 **
110986 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
110987 ** so that the fields are in the same order as an existing index. The
110988 ** aiMap[] array contains a mapping from the original LHS field order to
110989 ** the field order that matches the RHS index.
110990 **
110991 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
110992 ** even if it is constant, as OP_Affinity may be used on the register
110993 ** by code generated below. */
110994 assert( pParse->okConstFactor==okConstFactor );
110995 pParse->okConstFactor = 0;
110996 rLhsOrig = exprCodeVector(pParse, p: pLeft, piToFree: &iDummy);
110997 pParse->okConstFactor = okConstFactor;
110998 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
110999 if( i==nVector ){
111000 /* LHS fields are not reordered */
111001 rLhs = rLhsOrig;
111002 }else{
111003 /* Need to reorder the LHS fields according to aiMap */
111004 rLhs = sqlite3GetTempRange(pParse, nVector);
111005 for(i=0; i<nVector; i++){
111006 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: rLhsOrig+i, p2: rLhs+aiMap[i], p3: 0);
111007 }
111008 }
111009
111010 /* If sqlite3FindInIndex() did not find or create an index that is
111011 ** suitable for evaluating the IN operator, then evaluate using a
111012 ** sequence of comparisons.
111013 **
111014 ** This is step (1) in the in-operator.md optimized algorithm.
111015 */
111016 if( eType==IN_INDEX_NOOP ){
111017 ExprList *pList;
111018 CollSeq *pColl;
111019 int labelOk = sqlite3VdbeMakeLabel(pParse);
111020 int r2, regToFree;
111021 int regCkNull = 0;
111022 int ii;
111023 assert( ExprUseXList(pExpr) );
111024 pList = pExpr->x.pList;
111025 pColl = sqlite3ExprCollSeq(pParse, pExpr: pExpr->pLeft);
111026 if( destIfNull!=destIfFalse ){
111027 regCkNull = sqlite3GetTempReg(pParse);
111028 sqlite3VdbeAddOp3(p: v, OP_BitAnd, p1: rLhs, p2: rLhs, p3: regCkNull);
111029 }
111030 for(ii=0; ii<pList->nExpr; ii++){
111031 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
111032 if( regCkNull && sqlite3ExprCanBeNull(p: pList->a[ii].pExpr) ){
111033 sqlite3VdbeAddOp3(p: v, OP_BitAnd, p1: regCkNull, p2: r2, p3: regCkNull);
111034 }
111035 sqlite3ReleaseTempReg(pParse, regToFree);
111036 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
111037 int op = rLhs!=r2 ? OP_Eq : OP_NotNull;
111038 sqlite3VdbeAddOp4(p: v, op, p1: rLhs, p2: labelOk, p3: r2,
111039 zP4: (void*)pColl, P4_COLLSEQ);
111040 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
111041 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
111042 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
111043 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
111044 sqlite3VdbeChangeP5(p: v, p5: zAff[0]);
111045 }else{
111046 int op = rLhs!=r2 ? OP_Ne : OP_IsNull;
111047 assert( destIfNull==destIfFalse );
111048 sqlite3VdbeAddOp4(p: v, op, p1: rLhs, p2: destIfFalse, p3: r2,
111049 zP4: (void*)pColl, P4_COLLSEQ);
111050 VdbeCoverageIf(v, op==OP_Ne);
111051 VdbeCoverageIf(v, op==OP_IsNull);
111052 sqlite3VdbeChangeP5(p: v, p5: zAff[0] | SQLITE_JUMPIFNULL);
111053 }
111054 }
111055 if( regCkNull ){
111056 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regCkNull, p2: destIfNull); VdbeCoverage(v);
111057 sqlite3VdbeGoto(p: v, iDest: destIfFalse);
111058 }
111059 sqlite3VdbeResolveLabel(v, x: labelOk);
111060 sqlite3ReleaseTempReg(pParse, regCkNull);
111061 goto sqlite3ExprCodeIN_finished;
111062 }
111063
111064 /* Step 2: Check to see if the LHS contains any NULL columns. If the
111065 ** LHS does contain NULLs then the result must be either FALSE or NULL.
111066 ** We will then skip the binary search of the RHS.
111067 */
111068 if( destIfNull==destIfFalse ){
111069 destStep2 = destIfFalse;
111070 }else{
111071 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
111072 }
111073 for(i=0; i<nVector; i++){
111074 Expr *p = sqlite3VectorFieldSubexpr(pVector: pExpr->pLeft, i);
111075 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
111076 if( sqlite3ExprCanBeNull(p) ){
111077 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: rLhs+i, p2: destStep2);
111078 VdbeCoverage(v);
111079 }
111080 }
111081
111082 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
111083 ** of the RHS using the LHS as a probe. If found, the result is
111084 ** true.
111085 */
111086 if( eType==IN_INDEX_ROWID ){
111087 /* In this case, the RHS is the ROWID of table b-tree and so we also
111088 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
111089 ** into a single opcode. */
111090 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: iTab, p2: destIfFalse, p3: rLhs);
111091 VdbeCoverage(v);
111092 addrTruthOp = sqlite3VdbeAddOp0(p: v, OP_Goto); /* Return True */
111093 }else{
111094 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: rLhs, p2: nVector, p3: 0, zP4: zAff, p4type: nVector);
111095 if( destIfFalse==destIfNull ){
111096 /* Combine Step 3 and Step 5 into a single opcode */
111097 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iTab, p2: destIfFalse,
111098 p3: rLhs, p4: nVector); VdbeCoverage(v);
111099 goto sqlite3ExprCodeIN_finished;
111100 }
111101 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
111102 addrTruthOp = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iTab, p2: 0,
111103 p3: rLhs, p4: nVector); VdbeCoverage(v);
111104 }
111105
111106 /* Step 4. If the RHS is known to be non-NULL and we did not find
111107 ** an match on the search above, then the result must be FALSE.
111108 */
111109 if( rRhsHasNull && nVector==1 ){
111110 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: rRhsHasNull, p2: destIfFalse);
111111 VdbeCoverage(v);
111112 }
111113
111114 /* Step 5. If we do not care about the difference between NULL and
111115 ** FALSE, then just return false.
111116 */
111117 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(p: v, iDest: destIfFalse);
111118
111119 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
111120 ** If any comparison is NULL, then the result is NULL. If all
111121 ** comparisons are FALSE then the final result is FALSE.
111122 **
111123 ** For a scalar LHS, it is sufficient to check just the first row
111124 ** of the RHS.
111125 */
111126 if( destStep6 ) sqlite3VdbeResolveLabel(v, x: destStep6);
111127 addrTop = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iTab, p2: destIfFalse);
111128 VdbeCoverage(v);
111129 if( nVector>1 ){
111130 destNotNull = sqlite3VdbeMakeLabel(pParse);
111131 }else{
111132 /* For nVector==1, combine steps 6 and 7 by immediately returning
111133 ** FALSE if the first comparison is not NULL */
111134 destNotNull = destIfFalse;
111135 }
111136 for(i=0; i<nVector; i++){
111137 Expr *p;
111138 CollSeq *pColl;
111139 int r3 = sqlite3GetTempReg(pParse);
111140 p = sqlite3VectorFieldSubexpr(pVector: pLeft, i);
111141 pColl = sqlite3ExprCollSeq(pParse, pExpr: p);
111142 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iTab, p2: i, p3: r3);
111143 sqlite3VdbeAddOp4(p: v, OP_Ne, p1: rLhs+i, p2: destNotNull, p3: r3,
111144 zP4: (void*)pColl, P4_COLLSEQ);
111145 VdbeCoverage(v);
111146 sqlite3ReleaseTempReg(pParse, r3);
111147 }
111148 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: destIfNull);
111149 if( nVector>1 ){
111150 sqlite3VdbeResolveLabel(v, x: destNotNull);
111151 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iTab, p2: addrTop+1);
111152 VdbeCoverage(v);
111153
111154 /* Step 7: If we reach this point, we know that the result must
111155 ** be false. */
111156 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: destIfFalse);
111157 }
111158
111159 /* Jumps here in order to return true. */
111160 sqlite3VdbeJumpHere(p: v, addr: addrTruthOp);
111161
111162sqlite3ExprCodeIN_finished:
111163 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
111164 VdbeComment((v, "end IN expr"));
111165sqlite3ExprCodeIN_oom_error:
111166 sqlite3DbFree(db: pParse->db, p: aiMap);
111167 sqlite3DbFree(db: pParse->db, p: zAff);
111168}
111169#endif /* SQLITE_OMIT_SUBQUERY */
111170
111171#ifndef SQLITE_OMIT_FLOATING_POINT
111172/*
111173** Generate an instruction that will put the floating point
111174** value described by z[0..n-1] into register iMem.
111175**
111176** The z[] string will probably not be zero-terminated. But the
111177** z[n] character is guaranteed to be something that does not look
111178** like the continuation of the number.
111179*/
111180static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
111181 if( ALWAYS(z!=0) ){
111182 double value;
111183 sqlite3AtoF(z, pResult: &value, length: sqlite3Strlen30(z), SQLITE_UTF8);
111184 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
111185 if( negateFlag ) value = -value;
111186 sqlite3VdbeAddOp4Dup8(p: v, OP_Real, p1: 0, p2: iMem, p3: 0, zP4: (u8*)&value, P4_REAL);
111187 }
111188}
111189#endif
111190
111191
111192/*
111193** Generate an instruction that will put the integer describe by
111194** text z[0..n-1] into register iMem.
111195**
111196** Expr.u.zToken is always UTF8 and zero-terminated.
111197*/
111198static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
111199 Vdbe *v = pParse->pVdbe;
111200 if( pExpr->flags & EP_IntValue ){
111201 int i = pExpr->u.iValue;
111202 assert( i>=0 );
111203 if( negFlag ) i = -i;
111204 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: i, p2: iMem);
111205 }else{
111206 int c;
111207 i64 value;
111208 const char *z = pExpr->u.zToken;
111209 assert( z!=0 );
111210 c = sqlite3DecOrHexToI64(z, pOut: &value);
111211 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
111212#ifdef SQLITE_OMIT_FLOATING_POINT
111213 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
111214#else
111215#ifndef SQLITE_OMIT_HEX_INTEGER
111216 if( sqlite3_strnicmp(zLeft: z,zRight: "0x",N: 2)==0 ){
111217 sqlite3ErrorMsg(pParse, zFormat: "hex literal too big: %s%#T",
111218 negFlag?"-":"",pExpr);
111219 }else
111220#endif
111221 {
111222 codeReal(v, z, negateFlag: negFlag, iMem);
111223 }
111224#endif
111225 }else{
111226 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
111227 sqlite3VdbeAddOp4Dup8(p: v, OP_Int64, p1: 0, p2: iMem, p3: 0, zP4: (u8*)&value, P4_INT64);
111228 }
111229 }
111230}
111231
111232
111233/* Generate code that will load into register regOut a value that is
111234** appropriate for the iIdxCol-th column of index pIdx.
111235*/
111236SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
111237 Parse *pParse, /* The parsing context */
111238 Index *pIdx, /* The index whose column is to be loaded */
111239 int iTabCur, /* Cursor pointing to a table row */
111240 int iIdxCol, /* The column of the index to be loaded */
111241 int regOut /* Store the index column value in this register */
111242){
111243 i16 iTabCol = pIdx->aiColumn[iIdxCol];
111244 if( iTabCol==XN_EXPR ){
111245 assert( pIdx->aColExpr );
111246 assert( pIdx->aColExpr->nExpr>iIdxCol );
111247 pParse->iSelfTab = iTabCur + 1;
111248 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
111249 pParse->iSelfTab = 0;
111250 }else{
111251 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
111252 iTabCol, regOut);
111253 }
111254}
111255
111256#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111257/*
111258** Generate code that will compute the value of generated column pCol
111259** and store the result in register regOut
111260*/
111261SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(
111262 Parse *pParse, /* Parsing context */
111263 Table *pTab, /* Table containing the generated column */
111264 Column *pCol, /* The generated column */
111265 int regOut /* Put the result in this register */
111266){
111267 int iAddr;
111268 Vdbe *v = pParse->pVdbe;
111269 int nErr = pParse->nErr;
111270 assert( v!=0 );
111271 assert( pParse->iSelfTab!=0 );
111272 if( pParse->iSelfTab>0 ){
111273 iAddr = sqlite3VdbeAddOp3(p: v, OP_IfNullRow, p1: pParse->iSelfTab-1, p2: 0, p3: regOut);
111274 }else{
111275 iAddr = 0;
111276 }
111277 sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut);
111278 if( pCol->affinity>=SQLITE_AFF_TEXT ){
111279 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: regOut, p2: 1, p3: 0, zP4: &pCol->affinity, p4type: 1);
111280 }
111281 if( iAddr ) sqlite3VdbeJumpHere(p: v, addr: iAddr);
111282 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
111283}
111284#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
111285
111286/*
111287** Generate code to extract the value of the iCol-th column of a table.
111288*/
111289SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
111290 Vdbe *v, /* Parsing context */
111291 Table *pTab, /* The table containing the value */
111292 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
111293 int iCol, /* Index of the column to extract */
111294 int regOut /* Extract the value into this register */
111295){
111296 Column *pCol;
111297 assert( v!=0 );
111298 assert( pTab!=0 );
111299 assert( iCol!=XN_EXPR );
111300 if( iCol<0 || iCol==pTab->iPKey ){
111301 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iTabCur, p2: regOut);
111302 VdbeComment((v, "%s.rowid", pTab->zName));
111303 }else{
111304 int op;
111305 int x;
111306 if( IsVirtual(pTab) ){
111307 op = OP_VColumn;
111308 x = iCol;
111309#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111310 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
111311 Parse *pParse = sqlite3VdbeParser(p: v);
111312 if( pCol->colFlags & COLFLAG_BUSY ){
111313 sqlite3ErrorMsg(pParse, zFormat: "generated column loop on \"%s\"",
111314 pCol->zCnName);
111315 }else{
111316 int savedSelfTab = pParse->iSelfTab;
111317 pCol->colFlags |= COLFLAG_BUSY;
111318 pParse->iSelfTab = iTabCur+1;
111319 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut);
111320 pParse->iSelfTab = savedSelfTab;
111321 pCol->colFlags &= ~COLFLAG_BUSY;
111322 }
111323 return;
111324#endif
111325 }else if( !HasRowid(pTab) ){
111326 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
111327 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
111328 op = OP_Column;
111329 }else{
111330 x = sqlite3TableColumnToStorage(pTab,iCol);
111331 testcase( x!=iCol );
111332 op = OP_Column;
111333 }
111334 sqlite3VdbeAddOp3(p: v, op, p1: iTabCur, p2: x, p3: regOut);
111335 sqlite3ColumnDefault(v, pTab, iCol, regOut);
111336 }
111337}
111338
111339/*
111340** Generate code that will extract the iColumn-th column from
111341** table pTab and store the column value in register iReg.
111342**
111343** There must be an open cursor to pTab in iTable when this routine
111344** is called. If iColumn<0 then code is generated that extracts the rowid.
111345*/
111346SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
111347 Parse *pParse, /* Parsing and code generating context */
111348 Table *pTab, /* Description of the table we are reading from */
111349 int iColumn, /* Index of the table column */
111350 int iTable, /* The cursor pointing to the table */
111351 int iReg, /* Store results here */
111352 u8 p5 /* P5 value for OP_Column + FLAGS */
111353){
111354 assert( pParse->pVdbe!=0 );
111355 assert( (p5 & (OPFLAG_NOCHNG|OPFLAG_TYPEOFARG|OPFLAG_LENGTHARG))==p5 );
111356 assert( IsVirtual(pTab) || (p5 & OPFLAG_NOCHNG)==0 );
111357 sqlite3ExprCodeGetColumnOfTable(v: pParse->pVdbe, pTab, iTabCur: iTable, iCol: iColumn, regOut: iReg);
111358 if( p5 ){
111359 VdbeOp *pOp = sqlite3VdbeGetLastOp(p: pParse->pVdbe);
111360 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
111361 if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG);
111362 }
111363 return iReg;
111364}
111365
111366/*
111367** Generate code to move content from registers iFrom...iFrom+nReg-1
111368** over to iTo..iTo+nReg-1.
111369*/
111370SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
111371 sqlite3VdbeAddOp3(p: pParse->pVdbe, OP_Move, p1: iFrom, p2: iTo, p3: nReg);
111372}
111373
111374/*
111375** Convert a scalar expression node to a TK_REGISTER referencing
111376** register iReg. The caller must ensure that iReg already contains
111377** the correct value for the expression.
111378*/
111379static void exprToRegister(Expr *pExpr, int iReg){
111380 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
111381 if( NEVER(p==0) ) return;
111382 p->op2 = p->op;
111383 p->op = TK_REGISTER;
111384 p->iTable = iReg;
111385 ExprClearProperty(p, EP_Skip);
111386}
111387
111388/*
111389** Evaluate an expression (either a vector or a scalar expression) and store
111390** the result in contiguous temporary registers. Return the index of
111391** the first register used to store the result.
111392**
111393** If the returned result register is a temporary scalar, then also write
111394** that register number into *piFreeable. If the returned result register
111395** is not a temporary or if the expression is a vector set *piFreeable
111396** to 0.
111397*/
111398static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
111399 int iResult;
111400 int nResult = sqlite3ExprVectorSize(pExpr: p);
111401 if( nResult==1 ){
111402 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
111403 }else{
111404 *piFreeable = 0;
111405 if( p->op==TK_SELECT ){
111406#if SQLITE_OMIT_SUBQUERY
111407 iResult = 0;
111408#else
111409 iResult = sqlite3CodeSubselect(pParse, pExpr: p);
111410#endif
111411 }else{
111412 int i;
111413 iResult = pParse->nMem+1;
111414 pParse->nMem += nResult;
111415 assert( ExprUseXList(p) );
111416 for(i=0; i<nResult; i++){
111417 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
111418 }
111419 }
111420 }
111421 return iResult;
111422}
111423
111424/*
111425** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
111426** so that a subsequent copy will not be merged into this one.
111427*/
111428static void setDoNotMergeFlagOnCopy(Vdbe *v){
111429 if( sqlite3VdbeGetLastOp(p: v)->opcode==OP_Copy ){
111430 sqlite3VdbeChangeP5(p: v, p5: 1); /* Tag trailing OP_Copy as not mergeable */
111431 }
111432}
111433
111434/*
111435** Generate code to implement special SQL functions that are implemented
111436** in-line rather than by using the usual callbacks.
111437*/
111438static int exprCodeInlineFunction(
111439 Parse *pParse, /* Parsing context */
111440 ExprList *pFarg, /* List of function arguments */
111441 int iFuncId, /* Function ID. One of the INTFUNC_... values */
111442 int target /* Store function result in this register */
111443){
111444 int nFarg;
111445 Vdbe *v = pParse->pVdbe;
111446 assert( v!=0 );
111447 assert( pFarg!=0 );
111448 nFarg = pFarg->nExpr;
111449 assert( nFarg>0 ); /* All in-line functions have at least one argument */
111450 switch( iFuncId ){
111451 case INLINEFUNC_coalesce: {
111452 /* Attempt a direct implementation of the built-in COALESCE() and
111453 ** IFNULL() functions. This avoids unnecessary evaluation of
111454 ** arguments past the first non-NULL argument.
111455 */
111456 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
111457 int i;
111458 assert( nFarg>=2 );
111459 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
111460 for(i=1; i<nFarg; i++){
111461 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: target, p2: endCoalesce);
111462 VdbeCoverage(v);
111463 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
111464 }
111465 setDoNotMergeFlagOnCopy(v);
111466 sqlite3VdbeResolveLabel(v, x: endCoalesce);
111467 break;
111468 }
111469 case INLINEFUNC_iif: {
111470 Expr caseExpr;
111471 memset(s: &caseExpr, c: 0, n: sizeof(caseExpr));
111472 caseExpr.op = TK_CASE;
111473 caseExpr.x.pList = pFarg;
111474 return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
111475 }
111476#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
111477 case INLINEFUNC_sqlite_offset: {
111478 Expr *pArg = pFarg->a[0].pExpr;
111479 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
111480 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
111481 }else{
111482 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
111483 }
111484 break;
111485 }
111486#endif
111487 default: {
111488 /* The UNLIKELY() function is a no-op. The result is the value
111489 ** of the first argument.
111490 */
111491 assert( nFarg==1 || nFarg==2 );
111492 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
111493 break;
111494 }
111495
111496 /***********************************************************************
111497 ** Test-only SQL functions that are only usable if enabled
111498 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
111499 */
111500#if !defined(SQLITE_UNTESTABLE)
111501 case INLINEFUNC_expr_compare: {
111502 /* Compare two expressions using sqlite3ExprCompare() */
111503 assert( nFarg==2 );
111504 sqlite3VdbeAddOp2(p: v, OP_Integer,
111505 p1: sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
111506 p2: target);
111507 break;
111508 }
111509
111510 case INLINEFUNC_expr_implies_expr: {
111511 /* Compare two expressions using sqlite3ExprImpliesExpr() */
111512 assert( nFarg==2 );
111513 sqlite3VdbeAddOp2(p: v, OP_Integer,
111514 p1: sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
111515 p2: target);
111516 break;
111517 }
111518
111519 case INLINEFUNC_implies_nonnull_row: {
111520 /* Result of sqlite3ExprImpliesNonNullRow() */
111521 Expr *pA1;
111522 assert( nFarg==2 );
111523 pA1 = pFarg->a[1].pExpr;
111524 if( pA1->op==TK_COLUMN ){
111525 sqlite3VdbeAddOp2(p: v, OP_Integer,
111526 p1: sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
111527 p2: target);
111528 }else{
111529 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
111530 }
111531 break;
111532 }
111533
111534 case INLINEFUNC_affinity: {
111535 /* The AFFINITY() function evaluates to a string that describes
111536 ** the type affinity of the argument. This is used for testing of
111537 ** the SQLite type logic.
111538 */
111539 const char *azAff[] = { "blob", "text", "numeric", "integer",
111540 "real", "flexnum" };
111541 char aff;
111542 assert( nFarg==1 );
111543 aff = sqlite3ExprAffinity(pExpr: pFarg->a[0].pExpr);
111544 assert( aff<=SQLITE_AFF_NONE
111545 || (aff>=SQLITE_AFF_BLOB && aff<=SQLITE_AFF_FLEXNUM) );
111546 sqlite3VdbeLoadString(p: v, iDest: target,
111547 zStr: (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
111548 break;
111549 }
111550#endif /* !defined(SQLITE_UNTESTABLE) */
111551 }
111552 return target;
111553}
111554
111555/*
111556** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
111557** If it is, then resolve the expression by reading from the index and
111558** return the register into which the value has been read. If pExpr is
111559** not an indexed expression, then return negative.
111560*/
111561static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
111562 Parse *pParse, /* The parsing context */
111563 Expr *pExpr, /* The expression to potentially bypass */
111564 int target /* Where to store the result of the expression */
111565){
111566 IndexedExpr *p;
111567 Vdbe *v;
111568 for(p=pParse->pIdxEpr; p; p=p->pIENext){
111569 u8 exprAff;
111570 int iDataCur = p->iDataCur;
111571 if( iDataCur<0 ) continue;
111572 if( pParse->iSelfTab ){
111573 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
111574 iDataCur = -1;
111575 }
111576 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
111577 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC );
111578 exprAff = sqlite3ExprAffinity(pExpr);
111579 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB)
111580 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT)
111581 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC)
111582 ){
111583 /* Affinity mismatch on a generated column */
111584 continue;
111585 }
111586
111587 v = pParse->pVdbe;
111588 assert( v!=0 );
111589 if( p->bMaybeNullRow ){
111590 /* If the index is on a NULL row due to an outer join, then we
111591 ** cannot extract the value from the index. The value must be
111592 ** computed using the original expression. */
111593 int addr = sqlite3VdbeCurrentAddr(p: v);
111594 sqlite3VdbeAddOp3(p: v, OP_IfNullRow, p1: p->iIdxCur, p2: addr+3, p3: target);
111595 VdbeCoverage(v);
111596 sqlite3VdbeAddOp3(p: v, OP_Column, p1: p->iIdxCur, p2: p->iIdxCol, p3: target);
111597 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
111598 sqlite3VdbeGoto(p: v, iDest: 0);
111599 p = pParse->pIdxEpr;
111600 pParse->pIdxEpr = 0;
111601 sqlite3ExprCode(pParse, pExpr, target);
111602 pParse->pIdxEpr = p;
111603 sqlite3VdbeJumpHere(p: v, addr: addr+2);
111604 }else{
111605 sqlite3VdbeAddOp3(p: v, OP_Column, p1: p->iIdxCur, p2: p->iIdxCol, p3: target);
111606 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
111607 }
111608 return target;
111609 }
111610 return -1; /* Not found */
111611}
111612
111613
111614/*
111615** Generate code into the current Vdbe to evaluate the given
111616** expression. Attempt to store the results in register "target".
111617** Return the register where results are stored.
111618**
111619** With this routine, there is no guarantee that results will
111620** be stored in target. The result might be stored in some other
111621** register if it is convenient to do so. The calling function
111622** must check the return code and move the results to the desired
111623** register.
111624*/
111625SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
111626 Vdbe *v = pParse->pVdbe; /* The VM under construction */
111627 int op; /* The opcode being coded */
111628 int inReg = target; /* Results stored in register inReg */
111629 int regFree1 = 0; /* If non-zero free this temporary register */
111630 int regFree2 = 0; /* If non-zero free this temporary register */
111631 int r1, r2; /* Various register numbers */
111632 Expr tempX; /* Temporary expression node */
111633 int p5 = 0;
111634
111635 assert( target>0 && target<=pParse->nMem );
111636 assert( v!=0 );
111637
111638expr_code_doover:
111639 if( pExpr==0 ){
111640 op = TK_NULL;
111641 }else if( pParse->pIdxEpr!=0
111642 && !ExprHasProperty(pExpr, EP_Leaf)
111643 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
111644 ){
111645 return r1;
111646 }else{
111647 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
111648 op = pExpr->op;
111649 }
111650 switch( op ){
111651 case TK_AGG_COLUMN: {
111652 AggInfo *pAggInfo = pExpr->pAggInfo;
111653 struct AggInfo_col *pCol;
111654 assert( pAggInfo!=0 );
111655 assert( pExpr->iAgg>=0 );
111656 if( pExpr->iAgg>=pAggInfo->nColumn ){
111657 /* Happens when the left table of a RIGHT JOIN is null and
111658 ** is using an expression index */
111659 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
111660#ifdef SQLITE_VDBE_COVERAGE
111661 /* Verify that the OP_Null above is exercised by tests
111662 ** tag-20230325-2 */
111663 sqlite3VdbeAddOp2(v, OP_NotNull, target, 1);
111664 VdbeCoverageNeverTaken(v);
111665#endif
111666 break;
111667 }
111668 pCol = &pAggInfo->aCol[pExpr->iAgg];
111669 if( !pAggInfo->directMode ){
111670 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
111671 }else if( pAggInfo->useSortingIdx ){
111672 Table *pTab = pCol->pTab;
111673 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pAggInfo->sortingIdxPTab,
111674 p2: pCol->iSorterColumn, p3: target);
111675 if( pTab==0 ){
111676 /* No comment added */
111677 }else if( pCol->iColumn<0 ){
111678 VdbeComment((v,"%s.rowid",pTab->zName));
111679 }else{
111680 VdbeComment((v,"%s.%s",
111681 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
111682 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
111683 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: target);
111684 }
111685 }
111686 return target;
111687 }else if( pExpr->y.pTab==0 ){
111688 /* This case happens when the argument to an aggregate function
111689 ** is rewritten by aggregateConvertIndexedExprRefToColumn() */
111690 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pExpr->iTable, p2: pExpr->iColumn, p3: target);
111691 return target;
111692 }
111693 /* Otherwise, fall thru into the TK_COLUMN case */
111694 /* no break */ deliberate_fall_through
111695 }
111696 case TK_COLUMN: {
111697 int iTab = pExpr->iTable;
111698 int iReg;
111699 if( ExprHasProperty(pExpr, EP_FixedCol) ){
111700 /* This COLUMN expression is really a constant due to WHERE clause
111701 ** constraints, and that constant is coded by the pExpr->pLeft
111702 ** expression. However, make sure the constant has the correct
111703 ** datatype by applying the Affinity of the table column to the
111704 ** constant.
111705 */
111706 int aff;
111707 iReg = sqlite3ExprCodeTarget(pParse, pExpr: pExpr->pLeft,target);
111708 assert( ExprUseYTab(pExpr) );
111709 assert( pExpr->y.pTab!=0 );
111710 aff = sqlite3TableColumnAffinity(pTab: pExpr->y.pTab, iCol: pExpr->iColumn);
111711 if( aff>SQLITE_AFF_BLOB ){
111712 static const char zAff[] = "B\000C\000D\000E\000F";
111713 assert( SQLITE_AFF_BLOB=='A' );
111714 assert( SQLITE_AFF_TEXT=='B' );
111715 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: iReg, p2: 1, p3: 0,
111716 zP4: &zAff[(aff-'B')*2], P4_STATIC);
111717 }
111718 return iReg;
111719 }
111720 if( iTab<0 ){
111721 if( pParse->iSelfTab<0 ){
111722 /* Other columns in the same row for CHECK constraints or
111723 ** generated columns or for inserting into partial index.
111724 ** The row is unpacked into registers beginning at
111725 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
111726 ** immediately prior to the first column.
111727 */
111728 Column *pCol;
111729 Table *pTab;
111730 int iSrc;
111731 int iCol = pExpr->iColumn;
111732 assert( ExprUseYTab(pExpr) );
111733 pTab = pExpr->y.pTab;
111734 assert( pTab!=0 );
111735 assert( iCol>=XN_ROWID );
111736 assert( iCol<pTab->nCol );
111737 if( iCol<0 ){
111738 return -1-pParse->iSelfTab;
111739 }
111740 pCol = pTab->aCol + iCol;
111741 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
111742 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
111743#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111744 if( pCol->colFlags & COLFLAG_GENERATED ){
111745 if( pCol->colFlags & COLFLAG_BUSY ){
111746 sqlite3ErrorMsg(pParse, zFormat: "generated column loop on \"%s\"",
111747 pCol->zCnName);
111748 return 0;
111749 }
111750 pCol->colFlags |= COLFLAG_BUSY;
111751 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
111752 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut: iSrc);
111753 }
111754 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
111755 return iSrc;
111756 }else
111757#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
111758 if( pCol->affinity==SQLITE_AFF_REAL ){
111759 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: iSrc, p2: target);
111760 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: target);
111761 return target;
111762 }else{
111763 return iSrc;
111764 }
111765 }else{
111766 /* Coding an expression that is part of an index where column names
111767 ** in the index refer to the table to which the index belongs */
111768 iTab = pParse->iSelfTab - 1;
111769 }
111770 }
111771 assert( ExprUseYTab(pExpr) );
111772 assert( pExpr->y.pTab!=0 );
111773 iReg = sqlite3ExprCodeGetColumn(pParse, pTab: pExpr->y.pTab,
111774 iColumn: pExpr->iColumn, iTable: iTab, iReg: target,
111775 p5: pExpr->op2);
111776 return iReg;
111777 }
111778 case TK_INTEGER: {
111779 codeInteger(pParse, pExpr, negFlag: 0, iMem: target);
111780 return target;
111781 }
111782 case TK_TRUEFALSE: {
111783 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: sqlite3ExprTruthValue(pExpr), p2: target);
111784 return target;
111785 }
111786#ifndef SQLITE_OMIT_FLOATING_POINT
111787 case TK_FLOAT: {
111788 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111789 codeReal(v, z: pExpr->u.zToken, negateFlag: 0, iMem: target);
111790 return target;
111791 }
111792#endif
111793 case TK_STRING: {
111794 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111795 sqlite3VdbeLoadString(p: v, iDest: target, zStr: pExpr->u.zToken);
111796 return target;
111797 }
111798 default: {
111799 /* Make NULL the default case so that if a bug causes an illegal
111800 ** Expr node to be passed into this function, it will be handled
111801 ** sanely and not crash. But keep the assert() to bring the problem
111802 ** to the attention of the developers. */
111803 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
111804 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
111805 return target;
111806 }
111807#ifndef SQLITE_OMIT_BLOB_LITERAL
111808 case TK_BLOB: {
111809 int n;
111810 const char *z;
111811 char *zBlob;
111812 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111813 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
111814 assert( pExpr->u.zToken[1]=='\'' );
111815 z = &pExpr->u.zToken[2];
111816 n = sqlite3Strlen30(z) - 1;
111817 assert( z[n]=='\'' );
111818 zBlob = sqlite3HexToBlob(db: sqlite3VdbeDb(v), z, n);
111819 sqlite3VdbeAddOp4(p: v, OP_Blob, p1: n/2, p2: target, p3: 0, zP4: zBlob, P4_DYNAMIC);
111820 return target;
111821 }
111822#endif
111823 case TK_VARIABLE: {
111824 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111825 assert( pExpr->u.zToken!=0 );
111826 assert( pExpr->u.zToken[0]!=0 );
111827 sqlite3VdbeAddOp2(p: v, OP_Variable, p1: pExpr->iColumn, p2: target);
111828 if( pExpr->u.zToken[1]!=0 ){
111829 const char *z = sqlite3VListNumToName(pIn: pParse->pVList, iVal: pExpr->iColumn);
111830 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
111831 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
111832 sqlite3VdbeAppendP4(p: v, pP4: (char*)z, P4_STATIC);
111833 }
111834 return target;
111835 }
111836 case TK_REGISTER: {
111837 return pExpr->iTable;
111838 }
111839#ifndef SQLITE_OMIT_CAST
111840 case TK_CAST: {
111841 /* Expressions of the form: CAST(pLeft AS token) */
111842 sqlite3ExprCode(pParse, pExpr->pLeft, target);
111843 assert( inReg==target );
111844 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111845 sqlite3VdbeAddOp2(p: v, OP_Cast, p1: target,
111846 p2: sqlite3AffinityType(pExpr->u.zToken, 0));
111847 return inReg;
111848 }
111849#endif /* SQLITE_OMIT_CAST */
111850 case TK_IS:
111851 case TK_ISNOT:
111852 op = (op==TK_IS) ? TK_EQ : TK_NE;
111853 p5 = SQLITE_NULLEQ;
111854 /* fall-through */
111855 case TK_LT:
111856 case TK_LE:
111857 case TK_GT:
111858 case TK_GE:
111859 case TK_NE:
111860 case TK_EQ: {
111861 Expr *pLeft = pExpr->pLeft;
111862 if( sqlite3ExprIsVector(pExpr: pLeft) ){
111863 codeVectorCompare(pParse, pExpr, dest: target, op, p5);
111864 }else{
111865 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
111866 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
111867 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: inReg);
111868 codeCompare(pParse, pLeft, pRight: pExpr->pRight, opcode: op, in1: r1, in2: r2,
111869 dest: sqlite3VdbeCurrentAddr(p: v)+2, jumpIfNull: p5,
111870 ExprHasProperty(pExpr,EP_Commuted));
111871 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
111872 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
111873 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
111874 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
111875 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
111876 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
111877 if( p5==SQLITE_NULLEQ ){
111878 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: inReg);
111879 }else{
111880 sqlite3VdbeAddOp3(p: v, OP_ZeroOrNull, p1: r1, p2: inReg, p3: r2);
111881 }
111882 testcase( regFree1==0 );
111883 testcase( regFree2==0 );
111884 }
111885 break;
111886 }
111887 case TK_AND:
111888 case TK_OR:
111889 case TK_PLUS:
111890 case TK_STAR:
111891 case TK_MINUS:
111892 case TK_REM:
111893 case TK_BITAND:
111894 case TK_BITOR:
111895 case TK_SLASH:
111896 case TK_LSHIFT:
111897 case TK_RSHIFT:
111898 case TK_CONCAT: {
111899 assert( TK_AND==OP_And ); testcase( op==TK_AND );
111900 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
111901 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
111902 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
111903 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
111904 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
111905 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
111906 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
111907 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
111908 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
111909 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
111910 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111911 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
111912 sqlite3VdbeAddOp3(p: v, op, p1: r2, p2: r1, p3: target);
111913 testcase( regFree1==0 );
111914 testcase( regFree2==0 );
111915 break;
111916 }
111917 case TK_UMINUS: {
111918 Expr *pLeft = pExpr->pLeft;
111919 assert( pLeft );
111920 if( pLeft->op==TK_INTEGER ){
111921 codeInteger(pParse, pExpr: pLeft, negFlag: 1, iMem: target);
111922 return target;
111923#ifndef SQLITE_OMIT_FLOATING_POINT
111924 }else if( pLeft->op==TK_FLOAT ){
111925 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111926 codeReal(v, z: pLeft->u.zToken, negateFlag: 1, iMem: target);
111927 return target;
111928#endif
111929 }else{
111930 tempX.op = TK_INTEGER;
111931 tempX.flags = EP_IntValue|EP_TokenOnly;
111932 tempX.u.iValue = 0;
111933 ExprClearVVAProperties(&tempX);
111934 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
111935 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
111936 sqlite3VdbeAddOp3(p: v, OP_Subtract, p1: r2, p2: r1, p3: target);
111937 testcase( regFree2==0 );
111938 }
111939 break;
111940 }
111941 case TK_BITNOT:
111942 case TK_NOT: {
111943 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
111944 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
111945 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111946 testcase( regFree1==0 );
111947 sqlite3VdbeAddOp2(p: v, op, p1: r1, p2: inReg);
111948 break;
111949 }
111950 case TK_TRUTH: {
111951 int isTrue; /* IS TRUE or IS NOT TRUE */
111952 int bNormal; /* IS TRUE or IS FALSE */
111953 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111954 testcase( regFree1==0 );
111955 isTrue = sqlite3ExprTruthValue(pExpr: pExpr->pRight);
111956 bNormal = pExpr->op2==TK_IS;
111957 testcase( isTrue && bNormal);
111958 testcase( !isTrue && bNormal);
111959 sqlite3VdbeAddOp4Int(p: v, OP_IsTrue, p1: r1, p2: inReg, p3: !isTrue, p4: isTrue ^ bNormal);
111960 break;
111961 }
111962 case TK_ISNULL:
111963 case TK_NOTNULL: {
111964 int addr;
111965 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
111966 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
111967 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: target);
111968 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111969 testcase( regFree1==0 );
111970 addr = sqlite3VdbeAddOp1(p: v, op, p1: r1);
111971 VdbeCoverageIf(v, op==TK_ISNULL);
111972 VdbeCoverageIf(v, op==TK_NOTNULL);
111973 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: target);
111974 sqlite3VdbeJumpHere(p: v, addr);
111975 break;
111976 }
111977 case TK_AGG_FUNCTION: {
111978 AggInfo *pInfo = pExpr->pAggInfo;
111979 if( pInfo==0
111980 || NEVER(pExpr->iAgg<0)
111981 || NEVER(pExpr->iAgg>=pInfo->nFunc)
111982 ){
111983 assert( !ExprHasProperty(pExpr, EP_IntValue) );
111984 sqlite3ErrorMsg(pParse, zFormat: "misuse of aggregate: %#T()", pExpr);
111985 }else{
111986 return AggInfoFuncReg(pInfo, pExpr->iAgg);
111987 }
111988 break;
111989 }
111990 case TK_FUNCTION: {
111991 ExprList *pFarg; /* List of function arguments */
111992 int nFarg; /* Number of function arguments */
111993 FuncDef *pDef; /* The function definition object */
111994 const char *zId; /* The function name */
111995 u32 constMask = 0; /* Mask of function arguments that are constant */
111996 int i; /* Loop counter */
111997 sqlite3 *db = pParse->db; /* The database connection */
111998 u8 enc = ENC(db); /* The text encoding used by this database */
111999 CollSeq *pColl = 0; /* A collating sequence */
112000
112001#ifndef SQLITE_OMIT_WINDOWFUNC
112002 if( ExprHasProperty(pExpr, EP_WinFunc) ){
112003 return pExpr->y.pWin->regResult;
112004 }
112005#endif
112006
112007 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(p: pExpr) ){
112008 /* SQL functions can be expensive. So try to avoid running them
112009 ** multiple times if we know they always give the same result */
112010 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
112011 }
112012 assert( !ExprHasProperty(pExpr, EP_TokenOnly) );
112013 assert( ExprUseXList(pExpr) );
112014 pFarg = pExpr->x.pList;
112015 nFarg = pFarg ? pFarg->nExpr : 0;
112016 assert( !ExprHasProperty(pExpr, EP_IntValue) );
112017 zId = pExpr->u.zToken;
112018 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
112019#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
112020 if( pDef==0 && pParse->explain ){
112021 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
112022 }
112023#endif
112024 if( pDef==0 || pDef->xFinalize!=0 ){
112025 sqlite3ErrorMsg(pParse, zFormat: "unknown function: %#T()", pExpr);
112026 break;
112027 }
112028 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){
112029 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
112030 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
112031 return exprCodeInlineFunction(pParse, pFarg,
112032 SQLITE_PTR_TO_INT(pDef->pUserData), target);
112033 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
112034 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
112035 }
112036
112037 for(i=0; i<nFarg; i++){
112038 if( i<32 && sqlite3ExprIsConstant(p: pFarg->a[i].pExpr) ){
112039 testcase( i==31 );
112040 constMask |= MASKBIT32(i);
112041 }
112042 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
112043 pColl = sqlite3ExprCollSeq(pParse, pExpr: pFarg->a[i].pExpr);
112044 }
112045 }
112046 if( pFarg ){
112047 if( constMask ){
112048 r1 = pParse->nMem+1;
112049 pParse->nMem += nFarg;
112050 }else{
112051 r1 = sqlite3GetTempRange(pParse, nFarg);
112052 }
112053
112054 /* For length() and typeof() and octet_length() functions,
112055 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
112056 ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid
112057 ** unnecessary data loading.
112058 */
112059 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
112060 u8 exprOp;
112061 assert( nFarg==1 );
112062 assert( pFarg->a[0].pExpr!=0 );
112063 exprOp = pFarg->a[0].pExpr->op;
112064 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
112065 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
112066 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
112067 assert( SQLITE_FUNC_BYTELEN==OPFLAG_BYTELENARG );
112068 assert( (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG)==OPFLAG_BYTELENARG );
112069 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
112070 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
112071 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
112072 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG;
112073 }
112074 }
112075
112076 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, SQLITE_ECEL_FACTOR);
112077 }else{
112078 r1 = 0;
112079 }
112080#ifndef SQLITE_OMIT_VIRTUALTABLE
112081 /* Possibly overload the function if the first argument is
112082 ** a virtual table column.
112083 **
112084 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
112085 ** second argument, not the first, as the argument to test to
112086 ** see if it is a column in a virtual table. This is done because
112087 ** the left operand of infix functions (the operand we want to
112088 ** control overloading) ends up as the second argument to the
112089 ** function. The expression "A glob B" is equivalent to
112090 ** "glob(B,A). We want to use the A in "A glob B" to test
112091 ** for function overloading. But we use the B term in "glob(B,A)".
112092 */
112093 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
112094 pDef = sqlite3VtabOverloadFunction(db, pDef, nArg: nFarg, pFarg->a[1].pExpr);
112095 }else if( nFarg>0 ){
112096 pDef = sqlite3VtabOverloadFunction(db, pDef, nArg: nFarg, pFarg->a[0].pExpr);
112097 }
112098#endif
112099 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
112100 if( !pColl ) pColl = db->pDfltColl;
112101 sqlite3VdbeAddOp4(p: v, OP_CollSeq, p1: 0, p2: 0, p3: 0, zP4: (char *)pColl, P4_COLLSEQ);
112102 }
112103 sqlite3VdbeAddFunctionCall(pParse, p1: constMask, p2: r1, p3: target, nArg: nFarg,
112104 pFunc: pDef, eCallCtx: pExpr->op2);
112105 if( nFarg ){
112106 if( constMask==0 ){
112107 sqlite3ReleaseTempRange(pParse, r1, nFarg);
112108 }else{
112109 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
112110 }
112111 }
112112 return target;
112113 }
112114#ifndef SQLITE_OMIT_SUBQUERY
112115 case TK_EXISTS:
112116 case TK_SELECT: {
112117 int nCol;
112118 testcase( op==TK_EXISTS );
112119 testcase( op==TK_SELECT );
112120 if( pParse->db->mallocFailed ){
112121 return 0;
112122 }else if( op==TK_SELECT
112123 && ALWAYS( ExprUseXSelect(pExpr) )
112124 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
112125 ){
112126 sqlite3SubselectError(pParse, nActual: nCol, nExpect: 1);
112127 }else{
112128 return sqlite3CodeSubselect(pParse, pExpr);
112129 }
112130 break;
112131 }
112132 case TK_SELECT_COLUMN: {
112133 int n;
112134 Expr *pLeft = pExpr->pLeft;
112135 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
112136 pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr: pLeft);
112137 pLeft->op2 = pParse->withinRJSubrtn;
112138 }
112139 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
112140 n = sqlite3ExprVectorSize(pExpr: pLeft);
112141 if( pExpr->iTable!=n ){
112142 sqlite3ErrorMsg(pParse, zFormat: "%d columns assigned %d values",
112143 pExpr->iTable, n);
112144 }
112145 return pLeft->iTable + pExpr->iColumn;
112146 }
112147 case TK_IN: {
112148 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
112149 int destIfNull = sqlite3VdbeMakeLabel(pParse);
112150 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
112151 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
112152 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: target);
112153 sqlite3VdbeResolveLabel(v, x: destIfFalse);
112154 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: target, p2: 0);
112155 sqlite3VdbeResolveLabel(v, x: destIfNull);
112156 return target;
112157 }
112158#endif /* SQLITE_OMIT_SUBQUERY */
112159
112160
112161 /*
112162 ** x BETWEEN y AND z
112163 **
112164 ** This is equivalent to
112165 **
112166 ** x>=y AND x<=z
112167 **
112168 ** X is stored in pExpr->pLeft.
112169 ** Y is stored in pExpr->pList->a[0].pExpr.
112170 ** Z is stored in pExpr->pList->a[1].pExpr.
112171 */
112172 case TK_BETWEEN: {
112173 exprCodeBetween(pParse, pExpr, target, 0, 0);
112174 return target;
112175 }
112176 case TK_COLLATE: {
112177 if( !ExprHasProperty(pExpr, EP_Collate) ){
112178 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
112179 ** "SOFT-COLLATE" that is added to constraints that are pushed down
112180 ** from outer queries into sub-queries by the push-down optimization.
112181 ** Clear subtypes as subtypes may not cross a subquery boundary.
112182 */
112183 assert( pExpr->pLeft );
112184 sqlite3ExprCode(pParse, pExpr->pLeft, target);
112185 sqlite3VdbeAddOp1(p: v, OP_ClrSubtype, p1: target);
112186 return target;
112187 }else{
112188 pExpr = pExpr->pLeft;
112189 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
112190 }
112191 }
112192 case TK_SPAN:
112193 case TK_UPLUS: {
112194 pExpr = pExpr->pLeft;
112195 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
112196 }
112197
112198 case TK_TRIGGER: {
112199 /* If the opcode is TK_TRIGGER, then the expression is a reference
112200 ** to a column in the new.* or old.* pseudo-tables available to
112201 ** trigger programs. In this case Expr.iTable is set to 1 for the
112202 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
112203 ** is set to the column of the pseudo-table to read, or to -1 to
112204 ** read the rowid field.
112205 **
112206 ** The expression is implemented using an OP_Param opcode. The p1
112207 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
112208 ** to reference another column of the old.* pseudo-table, where
112209 ** i is the index of the column. For a new.rowid reference, p1 is
112210 ** set to (n+1), where n is the number of columns in each pseudo-table.
112211 ** For a reference to any other column in the new.* pseudo-table, p1
112212 ** is set to (n+2+i), where n and i are as defined previously. For
112213 ** example, if the table on which triggers are being fired is
112214 ** declared as:
112215 **
112216 ** CREATE TABLE t1(a, b);
112217 **
112218 ** Then p1 is interpreted as follows:
112219 **
112220 ** p1==0 -> old.rowid p1==3 -> new.rowid
112221 ** p1==1 -> old.a p1==4 -> new.a
112222 ** p1==2 -> old.b p1==5 -> new.b
112223 */
112224 Table *pTab;
112225 int iCol;
112226 int p1;
112227
112228 assert( ExprUseYTab(pExpr) );
112229 pTab = pExpr->y.pTab;
112230 iCol = pExpr->iColumn;
112231 p1 = pExpr->iTable * (pTab->nCol+1) + 1
112232 + sqlite3TableColumnToStorage(pTab, iCol);
112233
112234 assert( pExpr->iTable==0 || pExpr->iTable==1 );
112235 assert( iCol>=-1 && iCol<pTab->nCol );
112236 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
112237 assert( p1>=0 && p1<(pTab->nCol*2+2) );
112238
112239 sqlite3VdbeAddOp2(p: v, OP_Param, p1, p2: target);
112240 VdbeComment((v, "r[%d]=%s.%s", target,
112241 (pExpr->iTable ? "new" : "old"),
112242 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
112243 ));
112244
112245#ifndef SQLITE_OMIT_FLOATING_POINT
112246 /* If the column has REAL affinity, it may currently be stored as an
112247 ** integer. Use OP_RealAffinity to make sure it is really real.
112248 **
112249 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
112250 ** floating point when extracting it from the record. */
112251 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
112252 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: target);
112253 }
112254#endif
112255 break;
112256 }
112257
112258 case TK_VECTOR: {
112259 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
112260 break;
112261 }
112262
112263 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
112264 ** that derive from the right-hand table of a LEFT JOIN. The
112265 ** Expr.iTable value is the table number for the right-hand table.
112266 ** The expression is only evaluated if that table is not currently
112267 ** on a LEFT JOIN NULL row.
112268 */
112269 case TK_IF_NULL_ROW: {
112270 int addrINR;
112271 u8 okConstFactor = pParse->okConstFactor;
112272 AggInfo *pAggInfo = pExpr->pAggInfo;
112273 if( pAggInfo ){
112274 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
112275 if( !pAggInfo->directMode ){
112276 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
112277 break;
112278 }
112279 if( pExpr->pAggInfo->useSortingIdx ){
112280 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pAggInfo->sortingIdxPTab,
112281 p2: pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
112282 p3: target);
112283 inReg = target;
112284 break;
112285 }
112286 }
112287 addrINR = sqlite3VdbeAddOp3(p: v, OP_IfNullRow, p1: pExpr->iTable, p2: 0, p3: target);
112288 /* The OP_IfNullRow opcode above can overwrite the result register with
112289 ** NULL. So we have to ensure that the result register is not a value
112290 ** that is suppose to be a constant. Two defenses are needed:
112291 ** (1) Temporarily disable factoring of constant expressions
112292 ** (2) Make sure the computed value really is stored in register
112293 ** "target" and not someplace else.
112294 */
112295 pParse->okConstFactor = 0; /* note (1) above */
112296 sqlite3ExprCode(pParse, pExpr->pLeft, target);
112297 assert( target==inReg );
112298 pParse->okConstFactor = okConstFactor;
112299 sqlite3VdbeJumpHere(p: v, addr: addrINR);
112300 break;
112301 }
112302
112303 /*
112304 ** Form A:
112305 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
112306 **
112307 ** Form B:
112308 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
112309 **
112310 ** Form A is can be transformed into the equivalent form B as follows:
112311 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
112312 ** WHEN x=eN THEN rN ELSE y END
112313 **
112314 ** X (if it exists) is in pExpr->pLeft.
112315 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
112316 ** odd. The Y is also optional. If the number of elements in x.pList
112317 ** is even, then Y is omitted and the "otherwise" result is NULL.
112318 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
112319 **
112320 ** The result of the expression is the Ri for the first matching Ei,
112321 ** or if there is no matching Ei, the ELSE term Y, or if there is
112322 ** no ELSE term, NULL.
112323 */
112324 case TK_CASE: {
112325 int endLabel; /* GOTO label for end of CASE stmt */
112326 int nextCase; /* GOTO label for next WHEN clause */
112327 int nExpr; /* 2x number of WHEN terms */
112328 int i; /* Loop counter */
112329 ExprList *pEList; /* List of WHEN terms */
112330 struct ExprList_item *aListelem; /* Array of WHEN terms */
112331 Expr opCompare; /* The X==Ei expression */
112332 Expr *pX; /* The X expression */
112333 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
112334 Expr *pDel = 0;
112335 sqlite3 *db = pParse->db;
112336
112337 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
112338 assert(pExpr->x.pList->nExpr > 0);
112339 pEList = pExpr->x.pList;
112340 aListelem = pEList->a;
112341 nExpr = pEList->nExpr;
112342 endLabel = sqlite3VdbeMakeLabel(pParse);
112343 if( (pX = pExpr->pLeft)!=0 ){
112344 pDel = sqlite3ExprDup(db, p: pX, flags: 0);
112345 if( db->mallocFailed ){
112346 sqlite3ExprDelete(db, p: pDel);
112347 break;
112348 }
112349 testcase( pX->op==TK_COLUMN );
112350 exprToRegister(pExpr: pDel, iReg: exprCodeVector(pParse, p: pDel, piFreeable: &regFree1));
112351 testcase( regFree1==0 );
112352 memset(s: &opCompare, c: 0, n: sizeof(opCompare));
112353 opCompare.op = TK_EQ;
112354 opCompare.pLeft = pDel;
112355 pTest = &opCompare;
112356 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
112357 ** The value in regFree1 might get SCopy-ed into the file result.
112358 ** So make sure that the regFree1 register is not reused for other
112359 ** purposes and possibly overwritten. */
112360 regFree1 = 0;
112361 }
112362 for(i=0; i<nExpr-1; i=i+2){
112363 if( pX ){
112364 assert( pTest!=0 );
112365 opCompare.pRight = aListelem[i].pExpr;
112366 }else{
112367 pTest = aListelem[i].pExpr;
112368 }
112369 nextCase = sqlite3VdbeMakeLabel(pParse);
112370 testcase( pTest->op==TK_COLUMN );
112371 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
112372 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
112373 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
112374 sqlite3VdbeGoto(p: v, iDest: endLabel);
112375 sqlite3VdbeResolveLabel(v, x: nextCase);
112376 }
112377 if( (nExpr&1)!=0 ){
112378 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
112379 }else{
112380 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
112381 }
112382 sqlite3ExprDelete(db, p: pDel);
112383 setDoNotMergeFlagOnCopy(v);
112384 sqlite3VdbeResolveLabel(v, x: endLabel);
112385 break;
112386 }
112387#ifndef SQLITE_OMIT_TRIGGER
112388 case TK_RAISE: {
112389 assert( pExpr->affExpr==OE_Rollback
112390 || pExpr->affExpr==OE_Abort
112391 || pExpr->affExpr==OE_Fail
112392 || pExpr->affExpr==OE_Ignore
112393 );
112394 if( !pParse->pTriggerTab && !pParse->nested ){
112395 sqlite3ErrorMsg(pParse,
112396 zFormat: "RAISE() may only be used within a trigger-program");
112397 return 0;
112398 }
112399 if( pExpr->affExpr==OE_Abort ){
112400 sqlite3MayAbort(pParse);
112401 }
112402 assert( !ExprHasProperty(pExpr, EP_IntValue) );
112403 if( pExpr->affExpr==OE_Ignore ){
112404 sqlite3VdbeAddOp4(
112405 p: v, OP_Halt, SQLITE_OK, OE_Ignore, p3: 0, zP4: pExpr->u.zToken,p4type: 0);
112406 VdbeCoverage(v);
112407 }else{
112408 sqlite3HaltConstraint(pParse,
112409 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
112410 pExpr->affExpr, pExpr->u.zToken, 0, 0);
112411 }
112412
112413 break;
112414 }
112415#endif
112416 }
112417 sqlite3ReleaseTempReg(pParse, regFree1);
112418 sqlite3ReleaseTempReg(pParse, regFree2);
112419 return inReg;
112420}
112421
112422/*
112423** Generate code that will evaluate expression pExpr just one time
112424** per prepared statement execution.
112425**
112426** If the expression uses functions (that might throw an exception) then
112427** guard them with an OP_Once opcode to ensure that the code is only executed
112428** once. If no functions are involved, then factor the code out and put it at
112429** the end of the prepared statement in the initialization section.
112430**
112431** If regDest>=0 then the result is always stored in that register and the
112432** result is not reusable. If regDest<0 then this routine is free to
112433** store the value wherever it wants. The register where the expression
112434** is stored is returned. When regDest<0, two identical expressions might
112435** code to the same register, if they do not contain function calls and hence
112436** are factored out into the initialization section at the end of the
112437** prepared statement.
112438*/
112439SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
112440 Parse *pParse, /* Parsing context */
112441 Expr *pExpr, /* The expression to code when the VDBE initializes */
112442 int regDest /* Store the value in this register */
112443){
112444 ExprList *p;
112445 assert( ConstFactorOk(pParse) );
112446 p = pParse->pConstExpr;
112447 if( regDest<0 && p ){
112448 struct ExprList_item *pItem;
112449 int i;
112450 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
112451 if( pItem->fg.reusable
112452 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
112453 ){
112454 return pItem->u.iConstExprReg;
112455 }
112456 }
112457 }
112458 pExpr = sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0);
112459 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){
112460 Vdbe *v = pParse->pVdbe;
112461 int addr;
112462 assert( v );
112463 addr = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
112464 pParse->okConstFactor = 0;
112465 if( !pParse->db->mallocFailed ){
112466 if( regDest<0 ) regDest = ++pParse->nMem;
112467 sqlite3ExprCode(pParse, pExpr, regDest);
112468 }
112469 pParse->okConstFactor = 1;
112470 sqlite3ExprDelete(db: pParse->db, p: pExpr);
112471 sqlite3VdbeJumpHere(p: v, addr);
112472 }else{
112473 p = sqlite3ExprListAppend(pParse, pList: p, pExpr);
112474 if( p ){
112475 struct ExprList_item *pItem = &p->a[p->nExpr-1];
112476 pItem->fg.reusable = regDest<0;
112477 if( regDest<0 ) regDest = ++pParse->nMem;
112478 pItem->u.iConstExprReg = regDest;
112479 }
112480 pParse->pConstExpr = p;
112481 }
112482 return regDest;
112483}
112484
112485/*
112486** Generate code to evaluate an expression and store the results
112487** into a register. Return the register number where the results
112488** are stored.
112489**
112490** If the register is a temporary register that can be deallocated,
112491** then write its number into *pReg. If the result register is not
112492** a temporary, then set *pReg to zero.
112493**
112494** If pExpr is a constant, then this routine might generate this
112495** code to fill the register in the initialization section of the
112496** VDBE program, in order to factor it out of the evaluation loop.
112497*/
112498SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
112499 int r2;
112500 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
112501 if( ConstFactorOk(pParse)
112502 && ALWAYS(pExpr!=0)
112503 && pExpr->op!=TK_REGISTER
112504 && sqlite3ExprIsConstantNotJoin(p: pExpr)
112505 ){
112506 *pReg = 0;
112507 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, regDest: -1);
112508 }else{
112509 int r1 = sqlite3GetTempReg(pParse);
112510 r2 = sqlite3ExprCodeTarget(pParse, pExpr, target: r1);
112511 if( r2==r1 ){
112512 *pReg = r1;
112513 }else{
112514 sqlite3ReleaseTempReg(pParse, r1);
112515 *pReg = 0;
112516 }
112517 }
112518 return r2;
112519}
112520
112521/*
112522** Generate code that will evaluate expression pExpr and store the
112523** results in register target. The results are guaranteed to appear
112524** in register target.
112525*/
112526SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
112527 int inReg;
112528
112529 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) );
112530 assert( target>0 && target<=pParse->nMem );
112531 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
112532 if( pParse->pVdbe==0 ) return;
112533 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
112534 if( inReg!=target ){
112535 u8 op;
112536 if( ALWAYS(pExpr)
112537 && (ExprHasProperty(pExpr,EP_Subquery) || pExpr->op==TK_REGISTER)
112538 ){
112539 op = OP_Copy;
112540 }else{
112541 op = OP_SCopy;
112542 }
112543 sqlite3VdbeAddOp2(p: pParse->pVdbe, op, p1: inReg, p2: target);
112544 }
112545}
112546
112547/*
112548** Make a transient copy of expression pExpr and then code it using
112549** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
112550** except that the input expression is guaranteed to be unchanged.
112551*/
112552SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
112553 sqlite3 *db = pParse->db;
112554 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
112555 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
112556 sqlite3ExprDelete(db, p: pExpr);
112557}
112558
112559/*
112560** Generate code that will evaluate expression pExpr and store the
112561** results in register target. The results are guaranteed to appear
112562** in register target. If the expression is constant, then this routine
112563** might choose to code the expression at initialization time.
112564*/
112565SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
112566 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(p: pExpr) ){
112567 sqlite3ExprCodeRunJustOnce(pParse, pExpr, regDest: target);
112568 }else{
112569 sqlite3ExprCodeCopy(pParse, pExpr, target);
112570 }
112571}
112572
112573/*
112574** Generate code that pushes the value of every element of the given
112575** expression list into a sequence of registers beginning at target.
112576**
112577** Return the number of elements evaluated. The number returned will
112578** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
112579** is defined.
112580**
112581** The SQLITE_ECEL_DUP flag prevents the arguments from being
112582** filled using OP_SCopy. OP_Copy must be used instead.
112583**
112584** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
112585** factored out into initialization code.
112586**
112587** The SQLITE_ECEL_REF flag means that expressions in the list with
112588** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
112589** in registers at srcReg, and so the value can be copied from there.
112590** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
112591** are simply omitted rather than being copied from srcReg.
112592*/
112593SQLITE_PRIVATE int sqlite3ExprCodeExprList(
112594 Parse *pParse, /* Parsing context */
112595 ExprList *pList, /* The expression list to be coded */
112596 int target, /* Where to write results */
112597 int srcReg, /* Source registers if SQLITE_ECEL_REF */
112598 u8 flags /* SQLITE_ECEL_* flags */
112599){
112600 struct ExprList_item *pItem;
112601 int i, j, n;
112602 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
112603 Vdbe *v = pParse->pVdbe;
112604 assert( pList!=0 );
112605 assert( target>0 );
112606 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
112607 n = pList->nExpr;
112608 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
112609 for(pItem=pList->a, i=0; i<n; i++, pItem++){
112610 Expr *pExpr = pItem->pExpr;
112611#ifdef SQLITE_ENABLE_SORTER_REFERENCES
112612 if( pItem->fg.bSorterRef ){
112613 i--;
112614 n--;
112615 }else
112616#endif
112617 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
112618 if( flags & SQLITE_ECEL_OMITREF ){
112619 i--;
112620 n--;
112621 }else{
112622 sqlite3VdbeAddOp2(p: v, op: copyOp, p1: j+srcReg-1, p2: target+i);
112623 }
112624 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
112625 && sqlite3ExprIsConstantNotJoin(p: pExpr)
112626 ){
112627 sqlite3ExprCodeRunJustOnce(pParse, pExpr, regDest: target+i);
112628 }else{
112629 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target: target+i);
112630 if( inReg!=target+i ){
112631 VdbeOp *pOp;
112632 if( copyOp==OP_Copy
112633 && (pOp=sqlite3VdbeGetLastOp(p: v))->opcode==OP_Copy
112634 && pOp->p1+pOp->p3+1==inReg
112635 && pOp->p2+pOp->p3+1==target+i
112636 && pOp->p5==0 /* The do-not-merge flag must be clear */
112637 ){
112638 pOp->p3++;
112639 }else{
112640 sqlite3VdbeAddOp2(p: v, op: copyOp, p1: inReg, p2: target+i);
112641 }
112642 }
112643 }
112644 }
112645 return n;
112646}
112647
112648/*
112649** Generate code for a BETWEEN operator.
112650**
112651** x BETWEEN y AND z
112652**
112653** The above is equivalent to
112654**
112655** x>=y AND x<=z
112656**
112657** Code it as such, taking care to do the common subexpression
112658** elimination of x.
112659**
112660** The xJumpIf parameter determines details:
112661**
112662** NULL: Store the boolean result in reg[dest]
112663** sqlite3ExprIfTrue: Jump to dest if true
112664** sqlite3ExprIfFalse: Jump to dest if false
112665**
112666** The jumpIfNull parameter is ignored if xJumpIf is NULL.
112667*/
112668static void exprCodeBetween(
112669 Parse *pParse, /* Parsing and code generating context */
112670 Expr *pExpr, /* The BETWEEN expression */
112671 int dest, /* Jump destination or storage location */
112672 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
112673 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
112674){
112675 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
112676 Expr compLeft; /* The x>=y term */
112677 Expr compRight; /* The x<=z term */
112678 int regFree1 = 0; /* Temporary use register */
112679 Expr *pDel = 0;
112680 sqlite3 *db = pParse->db;
112681
112682 memset(s: &compLeft, c: 0, n: sizeof(Expr));
112683 memset(s: &compRight, c: 0, n: sizeof(Expr));
112684 memset(s: &exprAnd, c: 0, n: sizeof(Expr));
112685
112686 assert( ExprUseXList(pExpr) );
112687 pDel = sqlite3ExprDup(db, p: pExpr->pLeft, flags: 0);
112688 if( db->mallocFailed==0 ){
112689 exprAnd.op = TK_AND;
112690 exprAnd.pLeft = &compLeft;
112691 exprAnd.pRight = &compRight;
112692 compLeft.op = TK_GE;
112693 compLeft.pLeft = pDel;
112694 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
112695 compRight.op = TK_LE;
112696 compRight.pLeft = pDel;
112697 compRight.pRight = pExpr->x.pList->a[1].pExpr;
112698 exprToRegister(pExpr: pDel, iReg: exprCodeVector(pParse, p: pDel, piFreeable: &regFree1));
112699 if( xJump ){
112700 xJump(pParse, &exprAnd, dest, jumpIfNull);
112701 }else{
112702 /* Mark the expression is being from the ON or USING clause of a join
112703 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
112704 ** it into the Parse.pConstExpr list. We should use a new bit for this,
112705 ** for clarity, but we are out of bits in the Expr.flags field so we
112706 ** have to reuse the EP_OuterON bit. Bummer. */
112707 pDel->flags |= EP_OuterON;
112708 sqlite3ExprCodeTarget(pParse, pExpr: &exprAnd, target: dest);
112709 }
112710 sqlite3ReleaseTempReg(pParse, regFree1);
112711 }
112712 sqlite3ExprDelete(db, p: pDel);
112713
112714 /* Ensure adequate test coverage */
112715 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
112716 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
112717 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
112718 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
112719 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
112720 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
112721 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
112722 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
112723 testcase( xJump==0 );
112724}
112725
112726/*
112727** Generate code for a boolean expression such that a jump is made
112728** to the label "dest" if the expression is true but execution
112729** continues straight thru if the expression is false.
112730**
112731** If the expression evaluates to NULL (neither true nor false), then
112732** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
112733**
112734** This code depends on the fact that certain token values (ex: TK_EQ)
112735** are the same as opcode values (ex: OP_Eq) that implement the corresponding
112736** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
112737** the make process cause these values to align. Assert()s in the code
112738** below verify that the numbers are aligned correctly.
112739*/
112740SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
112741 Vdbe *v = pParse->pVdbe;
112742 int op = 0;
112743 int regFree1 = 0;
112744 int regFree2 = 0;
112745 int r1, r2;
112746
112747 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
112748 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
112749 if( NEVER(pExpr==0) ) return; /* No way this can happen */
112750 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) );
112751 op = pExpr->op;
112752 switch( op ){
112753 case TK_AND:
112754 case TK_OR: {
112755 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
112756 if( pAlt!=pExpr ){
112757 sqlite3ExprIfTrue(pParse, pExpr: pAlt, dest, jumpIfNull);
112758 }else if( op==TK_AND ){
112759 int d2 = sqlite3VdbeMakeLabel(pParse);
112760 testcase( jumpIfNull==0 );
112761 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
112762 jumpIfNull^SQLITE_JUMPIFNULL);
112763 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
112764 sqlite3VdbeResolveLabel(v, x: d2);
112765 }else{
112766 testcase( jumpIfNull==0 );
112767 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest, jumpIfNull);
112768 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
112769 }
112770 break;
112771 }
112772 case TK_NOT: {
112773 testcase( jumpIfNull==0 );
112774 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
112775 break;
112776 }
112777 case TK_TRUTH: {
112778 int isNot; /* IS NOT TRUE or IS NOT FALSE */
112779 int isTrue; /* IS TRUE or IS NOT TRUE */
112780 testcase( jumpIfNull==0 );
112781 isNot = pExpr->op2==TK_ISNOT;
112782 isTrue = sqlite3ExprTruthValue(pExpr: pExpr->pRight);
112783 testcase( isTrue && isNot );
112784 testcase( !isTrue && isNot );
112785 if( isTrue ^ isNot ){
112786 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest,
112787 jumpIfNull: isNot ? SQLITE_JUMPIFNULL : 0);
112788 }else{
112789 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
112790 isNot ? SQLITE_JUMPIFNULL : 0);
112791 }
112792 break;
112793 }
112794 case TK_IS:
112795 case TK_ISNOT:
112796 testcase( op==TK_IS );
112797 testcase( op==TK_ISNOT );
112798 op = (op==TK_IS) ? TK_EQ : TK_NE;
112799 jumpIfNull = SQLITE_NULLEQ;
112800 /* no break */ deliberate_fall_through
112801 case TK_LT:
112802 case TK_LE:
112803 case TK_GT:
112804 case TK_GE:
112805 case TK_NE:
112806 case TK_EQ: {
112807 if( sqlite3ExprIsVector(pExpr: pExpr->pLeft) ) goto default_expr;
112808 testcase( jumpIfNull==0 );
112809 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
112810 r2 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pRight, pReg: &regFree2);
112811 codeCompare(pParse, pLeft: pExpr->pLeft, pRight: pExpr->pRight, opcode: op,
112812 in1: r1, in2: r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted));
112813 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
112814 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
112815 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
112816 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
112817 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
112818 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
112819 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
112820 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
112821 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
112822 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
112823 testcase( regFree1==0 );
112824 testcase( regFree2==0 );
112825 break;
112826 }
112827 case TK_ISNULL:
112828 case TK_NOTNULL: {
112829 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
112830 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
112831 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
112832 sqlite3VdbeTypeofColumn(p: v, iDest: r1);
112833 sqlite3VdbeAddOp2(p: v, op, p1: r1, p2: dest);
112834 VdbeCoverageIf(v, op==TK_ISNULL);
112835 VdbeCoverageIf(v, op==TK_NOTNULL);
112836 testcase( regFree1==0 );
112837 break;
112838 }
112839 case TK_BETWEEN: {
112840 testcase( jumpIfNull==0 );
112841 exprCodeBetween(pParse, pExpr, dest, xJump: sqlite3ExprIfTrue, jumpIfNull);
112842 break;
112843 }
112844#ifndef SQLITE_OMIT_SUBQUERY
112845 case TK_IN: {
112846 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
112847 int destIfNull = jumpIfNull ? dest : destIfFalse;
112848 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
112849 sqlite3VdbeGoto(p: v, iDest: dest);
112850 sqlite3VdbeResolveLabel(v, x: destIfFalse);
112851 break;
112852 }
112853#endif
112854 default: {
112855 default_expr:
112856 if( ExprAlwaysTrue(pExpr) ){
112857 sqlite3VdbeGoto(p: v, iDest: dest);
112858 }else if( ExprAlwaysFalse(pExpr) ){
112859 /* No-op */
112860 }else{
112861 r1 = sqlite3ExprCodeTemp(pParse, pExpr, pReg: &regFree1);
112862 sqlite3VdbeAddOp3(p: v, OP_If, p1: r1, p2: dest, p3: jumpIfNull!=0);
112863 VdbeCoverage(v);
112864 testcase( regFree1==0 );
112865 testcase( jumpIfNull==0 );
112866 }
112867 break;
112868 }
112869 }
112870 sqlite3ReleaseTempReg(pParse, regFree1);
112871 sqlite3ReleaseTempReg(pParse, regFree2);
112872}
112873
112874/*
112875** Generate code for a boolean expression such that a jump is made
112876** to the label "dest" if the expression is false but execution
112877** continues straight thru if the expression is true.
112878**
112879** If the expression evaluates to NULL (neither true nor false) then
112880** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
112881** is 0.
112882*/
112883SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
112884 Vdbe *v = pParse->pVdbe;
112885 int op = 0;
112886 int regFree1 = 0;
112887 int regFree2 = 0;
112888 int r1, r2;
112889
112890 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
112891 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
112892 if( pExpr==0 ) return;
112893 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
112894
112895 /* The value of pExpr->op and op are related as follows:
112896 **
112897 ** pExpr->op op
112898 ** --------- ----------
112899 ** TK_ISNULL OP_NotNull
112900 ** TK_NOTNULL OP_IsNull
112901 ** TK_NE OP_Eq
112902 ** TK_EQ OP_Ne
112903 ** TK_GT OP_Le
112904 ** TK_LE OP_Gt
112905 ** TK_GE OP_Lt
112906 ** TK_LT OP_Ge
112907 **
112908 ** For other values of pExpr->op, op is undefined and unused.
112909 ** The value of TK_ and OP_ constants are arranged such that we
112910 ** can compute the mapping above using the following expression.
112911 ** Assert()s verify that the computation is correct.
112912 */
112913 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
112914
112915 /* Verify correct alignment of TK_ and OP_ constants
112916 */
112917 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
112918 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
112919 assert( pExpr->op!=TK_NE || op==OP_Eq );
112920 assert( pExpr->op!=TK_EQ || op==OP_Ne );
112921 assert( pExpr->op!=TK_LT || op==OP_Ge );
112922 assert( pExpr->op!=TK_LE || op==OP_Gt );
112923 assert( pExpr->op!=TK_GT || op==OP_Le );
112924 assert( pExpr->op!=TK_GE || op==OP_Lt );
112925
112926 switch( pExpr->op ){
112927 case TK_AND:
112928 case TK_OR: {
112929 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
112930 if( pAlt!=pExpr ){
112931 sqlite3ExprIfFalse(pParse, pExpr: pAlt, dest, jumpIfNull);
112932 }else if( pExpr->op==TK_AND ){
112933 testcase( jumpIfNull==0 );
112934 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pLeft, dest, jumpIfNull);
112935 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
112936 }else{
112937 int d2 = sqlite3VdbeMakeLabel(pParse);
112938 testcase( jumpIfNull==0 );
112939 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest: d2,
112940 jumpIfNull: jumpIfNull^SQLITE_JUMPIFNULL);
112941 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
112942 sqlite3VdbeResolveLabel(v, x: d2);
112943 }
112944 break;
112945 }
112946 case TK_NOT: {
112947 testcase( jumpIfNull==0 );
112948 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest, jumpIfNull);
112949 break;
112950 }
112951 case TK_TRUTH: {
112952 int isNot; /* IS NOT TRUE or IS NOT FALSE */
112953 int isTrue; /* IS TRUE or IS NOT TRUE */
112954 testcase( jumpIfNull==0 );
112955 isNot = pExpr->op2==TK_ISNOT;
112956 isTrue = sqlite3ExprTruthValue(pExpr: pExpr->pRight);
112957 testcase( isTrue && isNot );
112958 testcase( !isTrue && isNot );
112959 if( isTrue ^ isNot ){
112960 /* IS TRUE and IS NOT FALSE */
112961 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pLeft, dest,
112962 jumpIfNull: isNot ? 0 : SQLITE_JUMPIFNULL);
112963
112964 }else{
112965 /* IS FALSE and IS NOT TRUE */
112966 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest,
112967 jumpIfNull: isNot ? 0 : SQLITE_JUMPIFNULL);
112968 }
112969 break;
112970 }
112971 case TK_IS:
112972 case TK_ISNOT:
112973 testcase( pExpr->op==TK_IS );
112974 testcase( pExpr->op==TK_ISNOT );
112975 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
112976 jumpIfNull = SQLITE_NULLEQ;
112977 /* no break */ deliberate_fall_through
112978 case TK_LT:
112979 case TK_LE:
112980 case TK_GT:
112981 case TK_GE:
112982 case TK_NE:
112983 case TK_EQ: {
112984 if( sqlite3ExprIsVector(pExpr: pExpr->pLeft) ) goto default_expr;
112985 testcase( jumpIfNull==0 );
112986 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
112987 r2 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pRight, pReg: &regFree2);
112988 codeCompare(pParse, pLeft: pExpr->pLeft, pRight: pExpr->pRight, opcode: op,
112989 in1: r1, in2: r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted));
112990 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
112991 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
112992 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
112993 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
112994 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
112995 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
112996 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
112997 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
112998 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
112999 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
113000 testcase( regFree1==0 );
113001 testcase( regFree2==0 );
113002 break;
113003 }
113004 case TK_ISNULL:
113005 case TK_NOTNULL: {
113006 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
113007 sqlite3VdbeTypeofColumn(p: v, iDest: r1);
113008 sqlite3VdbeAddOp2(p: v, op, p1: r1, p2: dest);
113009 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
113010 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
113011 testcase( regFree1==0 );
113012 break;
113013 }
113014 case TK_BETWEEN: {
113015 testcase( jumpIfNull==0 );
113016 exprCodeBetween(pParse, pExpr, dest, xJump: sqlite3ExprIfFalse, jumpIfNull);
113017 break;
113018 }
113019#ifndef SQLITE_OMIT_SUBQUERY
113020 case TK_IN: {
113021 if( jumpIfNull ){
113022 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse: dest, destIfNull: dest);
113023 }else{
113024 int destIfNull = sqlite3VdbeMakeLabel(pParse);
113025 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse: dest, destIfNull);
113026 sqlite3VdbeResolveLabel(v, x: destIfNull);
113027 }
113028 break;
113029 }
113030#endif
113031 default: {
113032 default_expr:
113033 if( ExprAlwaysFalse(pExpr) ){
113034 sqlite3VdbeGoto(p: v, iDest: dest);
113035 }else if( ExprAlwaysTrue(pExpr) ){
113036 /* no-op */
113037 }else{
113038 r1 = sqlite3ExprCodeTemp(pParse, pExpr, pReg: &regFree1);
113039 sqlite3VdbeAddOp3(p: v, OP_IfNot, p1: r1, p2: dest, p3: jumpIfNull!=0);
113040 VdbeCoverage(v);
113041 testcase( regFree1==0 );
113042 testcase( jumpIfNull==0 );
113043 }
113044 break;
113045 }
113046 }
113047 sqlite3ReleaseTempReg(pParse, regFree1);
113048 sqlite3ReleaseTempReg(pParse, regFree2);
113049}
113050
113051/*
113052** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
113053** code generation, and that copy is deleted after code generation. This
113054** ensures that the original pExpr is unchanged.
113055*/
113056SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
113057 sqlite3 *db = pParse->db;
113058 Expr *pCopy = sqlite3ExprDup(db, p: pExpr, flags: 0);
113059 if( db->mallocFailed==0 ){
113060 sqlite3ExprIfFalse(pParse, pExpr: pCopy, dest, jumpIfNull);
113061 }
113062 sqlite3ExprDelete(db, p: pCopy);
113063}
113064
113065/*
113066** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
113067** type of expression.
113068**
113069** If pExpr is a simple SQL value - an integer, real, string, blob
113070** or NULL value - then the VDBE currently being prepared is configured
113071** to re-prepare each time a new value is bound to variable pVar.
113072**
113073** Additionally, if pExpr is a simple SQL value and the value is the
113074** same as that currently bound to variable pVar, non-zero is returned.
113075** Otherwise, if the values are not the same or if pExpr is not a simple
113076** SQL value, zero is returned.
113077*/
113078static int exprCompareVariable(
113079 const Parse *pParse,
113080 const Expr *pVar,
113081 const Expr *pExpr
113082){
113083 int res = 0;
113084 int iVar;
113085 sqlite3_value *pL, *pR = 0;
113086
113087 sqlite3ValueFromExpr(db: pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, ppVal: &pR);
113088 if( pR ){
113089 iVar = pVar->iColumn;
113090 sqlite3VdbeSetVarmask(v: pParse->pVdbe, iVar);
113091 pL = sqlite3VdbeGetBoundValue(v: pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
113092 if( pL ){
113093 if( sqlite3_value_type(pVal: pL)==SQLITE_TEXT ){
113094 sqlite3_value_text(pVal: pL); /* Make sure the encoding is UTF-8 */
113095 }
113096 res = 0==sqlite3MemCompare(pMem1: pL, pMem2: pR, pColl: 0);
113097 }
113098 sqlite3ValueFree(v: pR);
113099 sqlite3ValueFree(v: pL);
113100 }
113101
113102 return res;
113103}
113104
113105/*
113106** Do a deep comparison of two expression trees. Return 0 if the two
113107** expressions are completely identical. Return 1 if they differ only
113108** by a COLLATE operator at the top level. Return 2 if there are differences
113109** other than the top-level COLLATE operator.
113110**
113111** If any subelement of pB has Expr.iTable==(-1) then it is allowed
113112** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
113113**
113114** The pA side might be using TK_REGISTER. If that is the case and pB is
113115** not using TK_REGISTER but is otherwise equivalent, then still return 0.
113116**
113117** Sometimes this routine will return 2 even if the two expressions
113118** really are equivalent. If we cannot prove that the expressions are
113119** identical, we return 2 just to be safe. So if this routine
113120** returns 2, then you do not really know for certain if the two
113121** expressions are the same. But if you get a 0 or 1 return, then you
113122** can be sure the expressions are the same. In the places where
113123** this routine is used, it does not hurt to get an extra 2 - that
113124** just might result in some slightly slower code. But returning
113125** an incorrect 0 or 1 could lead to a malfunction.
113126**
113127** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
113128** pParse->pReprepare can be matched against literals in pB. The
113129** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
113130** If pParse is NULL (the normal case) then any TK_VARIABLE term in
113131** Argument pParse should normally be NULL. If it is not NULL and pA or
113132** pB causes a return value of 2.
113133*/
113134SQLITE_PRIVATE int sqlite3ExprCompare(
113135 const Parse *pParse,
113136 const Expr *pA,
113137 const Expr *pB,
113138 int iTab
113139){
113140 u32 combinedFlags;
113141 if( pA==0 || pB==0 ){
113142 return pB==pA ? 0 : 2;
113143 }
113144 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pVar: pA, pExpr: pB) ){
113145 return 0;
113146 }
113147 combinedFlags = pA->flags | pB->flags;
113148 if( combinedFlags & EP_IntValue ){
113149 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
113150 return 0;
113151 }
113152 return 2;
113153 }
113154 if( pA->op!=pB->op || pA->op==TK_RAISE ){
113155 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA: pA->pLeft,pB,iTab)<2 ){
113156 return 1;
113157 }
113158 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB: pB->pLeft,iTab)<2 ){
113159 return 1;
113160 }
113161 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
113162 && pB->iTable<0 && pA->iTable==iTab
113163 ){
113164 /* fall through */
113165 }else{
113166 return 2;
113167 }
113168 }
113169 assert( !ExprHasProperty(pA, EP_IntValue) );
113170 assert( !ExprHasProperty(pB, EP_IntValue) );
113171 if( pA->u.zToken ){
113172 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
113173 if( sqlite3StrICmp(zLeft: pA->u.zToken,zRight: pB->u.zToken)!=0 ) return 2;
113174#ifndef SQLITE_OMIT_WINDOWFUNC
113175 assert( pA->op==pB->op );
113176 if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
113177 return 2;
113178 }
113179 if( ExprHasProperty(pA,EP_WinFunc) ){
113180 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
113181 return 2;
113182 }
113183 }
113184#endif
113185 }else if( pA->op==TK_NULL ){
113186 return 0;
113187 }else if( pA->op==TK_COLLATE ){
113188 if( sqlite3_stricmp(zLeft: pA->u.zToken,zRight: pB->u.zToken)!=0 ) return 2;
113189 }else
113190 if( pB->u.zToken!=0
113191 && pA->op!=TK_COLUMN
113192 && pA->op!=TK_AGG_COLUMN
113193 && strcmp(s1: pA->u.zToken,s2: pB->u.zToken)!=0
113194 ){
113195 return 2;
113196 }
113197 }
113198 if( (pA->flags & (EP_Distinct|EP_Commuted))
113199 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
113200 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
113201 if( combinedFlags & EP_xIsSelect ) return 2;
113202 if( (combinedFlags & EP_FixedCol)==0
113203 && sqlite3ExprCompare(pParse, pA: pA->pLeft, pB: pB->pLeft, iTab) ) return 2;
113204 if( sqlite3ExprCompare(pParse, pA: pA->pRight, pB: pB->pRight, iTab) ) return 2;
113205 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
113206 if( pA->op!=TK_STRING
113207 && pA->op!=TK_TRUEFALSE
113208 && ALWAYS((combinedFlags & EP_Reduced)==0)
113209 ){
113210 if( pA->iColumn!=pB->iColumn ) return 2;
113211 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
113212 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
113213 return 2;
113214 }
113215 }
113216 }
113217 return 0;
113218}
113219
113220/*
113221** Compare two ExprList objects. Return 0 if they are identical, 1
113222** if they are certainly different, or 2 if it is not possible to
113223** determine if they are identical or not.
113224**
113225** If any subelement of pB has Expr.iTable==(-1) then it is allowed
113226** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
113227**
113228** This routine might return non-zero for equivalent ExprLists. The
113229** only consequence will be disabled optimizations. But this routine
113230** must never return 0 if the two ExprList objects are different, or
113231** a malfunction will result.
113232**
113233** Two NULL pointers are considered to be the same. But a NULL pointer
113234** always differs from a non-NULL pointer.
113235*/
113236SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){
113237 int i;
113238 if( pA==0 && pB==0 ) return 0;
113239 if( pA==0 || pB==0 ) return 1;
113240 if( pA->nExpr!=pB->nExpr ) return 1;
113241 for(i=0; i<pA->nExpr; i++){
113242 int res;
113243 Expr *pExprA = pA->a[i].pExpr;
113244 Expr *pExprB = pB->a[i].pExpr;
113245 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
113246 if( (res = sqlite3ExprCompare(pParse: 0, pA: pExprA, pB: pExprB, iTab)) ) return res;
113247 }
113248 return 0;
113249}
113250
113251/*
113252** Like sqlite3ExprCompare() except COLLATE operators at the top-level
113253** are ignored.
113254*/
113255SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
113256 return sqlite3ExprCompare(pParse: 0,
113257 pA: sqlite3ExprSkipCollateAndLikely(pExpr: pA),
113258 pB: sqlite3ExprSkipCollateAndLikely(pExpr: pB),
113259 iTab);
113260}
113261
113262/*
113263** Return non-zero if Expr p can only be true if pNN is not NULL.
113264**
113265** Or if seenNot is true, return non-zero if Expr p can only be
113266** non-NULL if pNN is not NULL
113267*/
113268static int exprImpliesNotNull(
113269 const Parse *pParse,/* Parsing context */
113270 const Expr *p, /* The expression to be checked */
113271 const Expr *pNN, /* The expression that is NOT NULL */
113272 int iTab, /* Table being evaluated */
113273 int seenNot /* Return true only if p can be any non-NULL value */
113274){
113275 assert( p );
113276 assert( pNN );
113277 if( sqlite3ExprCompare(pParse, pA: p, pB: pNN, iTab)==0 ){
113278 return pNN->op!=TK_NULL;
113279 }
113280 switch( p->op ){
113281 case TK_IN: {
113282 if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
113283 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
113284 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
113285 }
113286 case TK_BETWEEN: {
113287 ExprList *pList;
113288 assert( ExprUseXList(p) );
113289 pList = p->x.pList;
113290 assert( pList!=0 );
113291 assert( pList->nExpr==2 );
113292 if( seenNot ) return 0;
113293 if( exprImpliesNotNull(pParse, p: pList->a[0].pExpr, pNN, iTab, seenNot: 1)
113294 || exprImpliesNotNull(pParse, p: pList->a[1].pExpr, pNN, iTab, seenNot: 1)
113295 ){
113296 return 1;
113297 }
113298 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
113299 }
113300 case TK_EQ:
113301 case TK_NE:
113302 case TK_LT:
113303 case TK_LE:
113304 case TK_GT:
113305 case TK_GE:
113306 case TK_PLUS:
113307 case TK_MINUS:
113308 case TK_BITOR:
113309 case TK_LSHIFT:
113310 case TK_RSHIFT:
113311 case TK_CONCAT:
113312 seenNot = 1;
113313 /* no break */ deliberate_fall_through
113314 case TK_STAR:
113315 case TK_REM:
113316 case TK_BITAND:
113317 case TK_SLASH: {
113318 if( exprImpliesNotNull(pParse, p: p->pRight, pNN, iTab, seenNot) ) return 1;
113319 /* no break */ deliberate_fall_through
113320 }
113321 case TK_SPAN:
113322 case TK_COLLATE:
113323 case TK_UPLUS:
113324 case TK_UMINUS: {
113325 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot);
113326 }
113327 case TK_TRUTH: {
113328 if( seenNot ) return 0;
113329 if( p->op2!=TK_IS ) return 0;
113330 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
113331 }
113332 case TK_BITNOT:
113333 case TK_NOT: {
113334 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
113335 }
113336 }
113337 return 0;
113338}
113339
113340/*
113341** Return true if we can prove the pE2 will always be true if pE1 is
113342** true. Return false if we cannot complete the proof or if pE2 might
113343** be false. Examples:
113344**
113345** pE1: x==5 pE2: x==5 Result: true
113346** pE1: x>0 pE2: x==5 Result: false
113347** pE1: x=21 pE2: x=21 OR y=43 Result: true
113348** pE1: x!=123 pE2: x IS NOT NULL Result: true
113349** pE1: x!=?1 pE2: x IS NOT NULL Result: true
113350** pE1: x IS NULL pE2: x IS NOT NULL Result: false
113351** pE1: x IS ?2 pE2: x IS NOT NULL Result: false
113352**
113353** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
113354** Expr.iTable<0 then assume a table number given by iTab.
113355**
113356** If pParse is not NULL, then the values of bound variables in pE1 are
113357** compared against literal values in pE2 and pParse->pVdbe->expmask is
113358** modified to record which bound variables are referenced. If pParse
113359** is NULL, then false will be returned if pE1 contains any bound variables.
113360**
113361** When in doubt, return false. Returning true might give a performance
113362** improvement. Returning false might cause a performance reduction, but
113363** it will always give the correct answer and is hence always safe.
113364*/
113365SQLITE_PRIVATE int sqlite3ExprImpliesExpr(
113366 const Parse *pParse,
113367 const Expr *pE1,
113368 const Expr *pE2,
113369 int iTab
113370){
113371 if( sqlite3ExprCompare(pParse, pA: pE1, pB: pE2, iTab)==0 ){
113372 return 1;
113373 }
113374 if( pE2->op==TK_OR
113375 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2: pE2->pLeft, iTab)
113376 || sqlite3ExprImpliesExpr(pParse, pE1, pE2: pE2->pRight, iTab) )
113377 ){
113378 return 1;
113379 }
113380 if( pE2->op==TK_NOTNULL
113381 && exprImpliesNotNull(pParse, p: pE1, pNN: pE2->pLeft, iTab, seenNot: 0)
113382 ){
113383 return 1;
113384 }
113385 return 0;
113386}
113387
113388/* This is a helper function to impliesNotNullRow(). In this routine,
113389** set pWalker->eCode to one only if *both* of the input expressions
113390** separately have the implies-not-null-row property.
113391*/
113392static void bothImplyNotNullRow(Walker *pWalker, Expr *pE1, Expr *pE2){
113393 if( pWalker->eCode==0 ){
113394 sqlite3WalkExpr(pWalker, pExpr: pE1);
113395 if( pWalker->eCode ){
113396 pWalker->eCode = 0;
113397 sqlite3WalkExpr(pWalker, pExpr: pE2);
113398 }
113399 }
113400}
113401
113402/*
113403** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
113404** If the expression node requires that the table at pWalker->iCur
113405** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
113406**
113407** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
113408** behalf of a RIGHT JOIN (or FULL JOIN). That makes a difference when
113409** evaluating terms in the ON clause of an inner join.
113410**
113411** This routine controls an optimization. False positives (setting
113412** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
113413** (never setting pWalker->eCode) is a harmless missed optimization.
113414*/
113415static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
113416 testcase( pExpr->op==TK_AGG_COLUMN );
113417 testcase( pExpr->op==TK_AGG_FUNCTION );
113418 if( ExprHasProperty(pExpr, EP_OuterON) ) return WRC_Prune;
113419 if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){
113420 /* If iCur is used in an inner-join ON clause to the left of a
113421 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
113422 ** But it is difficult to check for that condition precisely.
113423 ** To keep things simple, any use of iCur from any inner-join is
113424 ** ignored while attempting to simplify a RIGHT JOIN. */
113425 return WRC_Prune;
113426 }
113427 switch( pExpr->op ){
113428 case TK_ISNOT:
113429 case TK_ISNULL:
113430 case TK_NOTNULL:
113431 case TK_IS:
113432 case TK_VECTOR:
113433 case TK_FUNCTION:
113434 case TK_TRUTH:
113435 case TK_CASE:
113436 testcase( pExpr->op==TK_ISNOT );
113437 testcase( pExpr->op==TK_ISNULL );
113438 testcase( pExpr->op==TK_NOTNULL );
113439 testcase( pExpr->op==TK_IS );
113440 testcase( pExpr->op==TK_VECTOR );
113441 testcase( pExpr->op==TK_FUNCTION );
113442 testcase( pExpr->op==TK_TRUTH );
113443 testcase( pExpr->op==TK_CASE );
113444 return WRC_Prune;
113445
113446 case TK_COLUMN:
113447 if( pWalker->u.iCur==pExpr->iTable ){
113448 pWalker->eCode = 1;
113449 return WRC_Abort;
113450 }
113451 return WRC_Prune;
113452
113453 case TK_OR:
113454 case TK_AND:
113455 /* Both sides of an AND or OR must separately imply non-null-row.
113456 ** Consider these cases:
113457 ** 1. NOT (x AND y)
113458 ** 2. x OR y
113459 ** If only one of x or y is non-null-row, then the overall expression
113460 ** can be true if the other arm is false (case 1) or true (case 2).
113461 */
113462 testcase( pExpr->op==TK_OR );
113463 testcase( pExpr->op==TK_AND );
113464 bothImplyNotNullRow(pWalker, pE1: pExpr->pLeft, pE2: pExpr->pRight);
113465 return WRC_Prune;
113466
113467 case TK_IN:
113468 /* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)",
113469 ** both of which can be true. But apart from these cases, if
113470 ** the left-hand side of the IN is NULL then the IN itself will be
113471 ** NULL. */
113472 if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){
113473 sqlite3WalkExpr(pWalker, pExpr: pExpr->pLeft);
113474 }
113475 return WRC_Prune;
113476
113477 case TK_BETWEEN:
113478 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
113479 ** both y and z must be non-null row */
113480 assert( ExprUseXList(pExpr) );
113481 assert( pExpr->x.pList->nExpr==2 );
113482 sqlite3WalkExpr(pWalker, pExpr: pExpr->pLeft);
113483 bothImplyNotNullRow(pWalker, pE1: pExpr->x.pList->a[0].pExpr,
113484 pE2: pExpr->x.pList->a[1].pExpr);
113485 return WRC_Prune;
113486
113487 /* Virtual tables are allowed to use constraints like x=NULL. So
113488 ** a term of the form x=y does not prove that y is not null if x
113489 ** is the column of a virtual table */
113490 case TK_EQ:
113491 case TK_NE:
113492 case TK_LT:
113493 case TK_LE:
113494 case TK_GT:
113495 case TK_GE: {
113496 Expr *pLeft = pExpr->pLeft;
113497 Expr *pRight = pExpr->pRight;
113498 testcase( pExpr->op==TK_EQ );
113499 testcase( pExpr->op==TK_NE );
113500 testcase( pExpr->op==TK_LT );
113501 testcase( pExpr->op==TK_LE );
113502 testcase( pExpr->op==TK_GT );
113503 testcase( pExpr->op==TK_GE );
113504 /* The y.pTab=0 assignment in wherecode.c always happens after the
113505 ** impliesNotNullRow() test */
113506 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
113507 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
113508 if( (pLeft->op==TK_COLUMN
113509 && ALWAYS(pLeft->y.pTab!=0)
113510 && IsVirtual(pLeft->y.pTab))
113511 || (pRight->op==TK_COLUMN
113512 && ALWAYS(pRight->y.pTab!=0)
113513 && IsVirtual(pRight->y.pTab))
113514 ){
113515 return WRC_Prune;
113516 }
113517 /* no break */ deliberate_fall_through
113518 }
113519 default:
113520 return WRC_Continue;
113521 }
113522}
113523
113524/*
113525** Return true (non-zero) if expression p can only be true if at least
113526** one column of table iTab is non-null. In other words, return true
113527** if expression p will always be NULL or false if every column of iTab
113528** is NULL.
113529**
113530** False negatives are acceptable. In other words, it is ok to return
113531** zero even if expression p will never be true of every column of iTab
113532** is NULL. A false negative is merely a missed optimization opportunity.
113533**
113534** False positives are not allowed, however. A false positive may result
113535** in an incorrect answer.
113536**
113537** Terms of p that are marked with EP_OuterON (and hence that come from
113538** the ON or USING clauses of OUTER JOINS) are excluded from the analysis.
113539**
113540** This routine is used to check if a LEFT JOIN can be converted into
113541** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
113542** clause requires that some column of the right table of the LEFT JOIN
113543** be non-NULL, then the LEFT JOIN can be safely converted into an
113544** ordinary join.
113545*/
113546SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab, int isRJ){
113547 Walker w;
113548 p = sqlite3ExprSkipCollateAndLikely(pExpr: p);
113549 if( p==0 ) return 0;
113550 if( p->op==TK_NOTNULL ){
113551 p = p->pLeft;
113552 }else{
113553 while( p->op==TK_AND ){
113554 if( sqlite3ExprImpliesNonNullRow(p: p->pLeft, iTab, isRJ) ) return 1;
113555 p = p->pRight;
113556 }
113557 }
113558 w.xExprCallback = impliesNotNullRow;
113559 w.xSelectCallback = 0;
113560 w.xSelectCallback2 = 0;
113561 w.eCode = 0;
113562 w.mWFlags = isRJ!=0;
113563 w.u.iCur = iTab;
113564 sqlite3WalkExpr(pWalker: &w, pExpr: p);
113565 return w.eCode;
113566}
113567
113568/*
113569** An instance of the following structure is used by the tree walker
113570** to determine if an expression can be evaluated by reference to the
113571** index only, without having to do a search for the corresponding
113572** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
113573** is the cursor for the table.
113574*/
113575struct IdxCover {
113576 Index *pIdx; /* The index to be tested for coverage */
113577 int iCur; /* Cursor number for the table corresponding to the index */
113578};
113579
113580/*
113581** Check to see if there are references to columns in table
113582** pWalker->u.pIdxCover->iCur can be satisfied using the index
113583** pWalker->u.pIdxCover->pIdx.
113584*/
113585static int exprIdxCover(Walker *pWalker, Expr *pExpr){
113586 if( pExpr->op==TK_COLUMN
113587 && pExpr->iTable==pWalker->u.pIdxCover->iCur
113588 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
113589 ){
113590 pWalker->eCode = 1;
113591 return WRC_Abort;
113592 }
113593 return WRC_Continue;
113594}
113595
113596/*
113597** Determine if an index pIdx on table with cursor iCur contains will
113598** the expression pExpr. Return true if the index does cover the
113599** expression and false if the pExpr expression references table columns
113600** that are not found in the index pIdx.
113601**
113602** An index covering an expression means that the expression can be
113603** evaluated using only the index and without having to lookup the
113604** corresponding table entry.
113605*/
113606SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
113607 Expr *pExpr, /* The index to be tested */
113608 int iCur, /* The cursor number for the corresponding table */
113609 Index *pIdx /* The index that might be used for coverage */
113610){
113611 Walker w;
113612 struct IdxCover xcov;
113613 memset(s: &w, c: 0, n: sizeof(w));
113614 xcov.iCur = iCur;
113615 xcov.pIdx = pIdx;
113616 w.xExprCallback = exprIdxCover;
113617 w.u.pIdxCover = &xcov;
113618 sqlite3WalkExpr(pWalker: &w, pExpr);
113619 return !w.eCode;
113620}
113621
113622
113623/* Structure used to pass information throughout the Walker in order to
113624** implement sqlite3ReferencesSrcList().
113625*/
113626struct RefSrcList {
113627 sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */
113628 SrcList *pRef; /* Looking for references to these tables */
113629 i64 nExclude; /* Number of tables to exclude from the search */
113630 int *aiExclude; /* Cursor IDs for tables to exclude from the search */
113631};
113632
113633/*
113634** Walker SELECT callbacks for sqlite3ReferencesSrcList().
113635**
113636** When entering a new subquery on the pExpr argument, add all FROM clause
113637** entries for that subquery to the exclude list.
113638**
113639** When leaving the subquery, remove those entries from the exclude list.
113640*/
113641static int selectRefEnter(Walker *pWalker, Select *pSelect){
113642 struct RefSrcList *p = pWalker->u.pRefSrcList;
113643 SrcList *pSrc = pSelect->pSrc;
113644 i64 i, j;
113645 int *piNew;
113646 if( pSrc->nSrc==0 ) return WRC_Continue;
113647 j = p->nExclude;
113648 p->nExclude += pSrc->nSrc;
113649 piNew = sqlite3DbRealloc(db: p->db, p: p->aiExclude, n: p->nExclude*sizeof(int));
113650 if( piNew==0 ){
113651 p->nExclude = 0;
113652 return WRC_Abort;
113653 }else{
113654 p->aiExclude = piNew;
113655 }
113656 for(i=0; i<pSrc->nSrc; i++, j++){
113657 p->aiExclude[j] = pSrc->a[i].iCursor;
113658 }
113659 return WRC_Continue;
113660}
113661static void selectRefLeave(Walker *pWalker, Select *pSelect){
113662 struct RefSrcList *p = pWalker->u.pRefSrcList;
113663 SrcList *pSrc = pSelect->pSrc;
113664 if( p->nExclude ){
113665 assert( p->nExclude>=pSrc->nSrc );
113666 p->nExclude -= pSrc->nSrc;
113667 }
113668}
113669
113670/* This is the Walker EXPR callback for sqlite3ReferencesSrcList().
113671**
113672** Set the 0x01 bit of pWalker->eCode if there is a reference to any
113673** of the tables shown in RefSrcList.pRef.
113674**
113675** Set the 0x02 bit of pWalker->eCode if there is a reference to a
113676** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude.
113677*/
113678static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){
113679 if( pExpr->op==TK_COLUMN
113680 || pExpr->op==TK_AGG_COLUMN
113681 ){
113682 int i;
113683 struct RefSrcList *p = pWalker->u.pRefSrcList;
113684 SrcList *pSrc = p->pRef;
113685 int nSrc = pSrc ? pSrc->nSrc : 0;
113686 for(i=0; i<nSrc; i++){
113687 if( pExpr->iTable==pSrc->a[i].iCursor ){
113688 pWalker->eCode |= 1;
113689 return WRC_Continue;
113690 }
113691 }
113692 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
113693 if( i>=p->nExclude ){
113694 pWalker->eCode |= 2;
113695 }
113696 }
113697 return WRC_Continue;
113698}
113699
113700/*
113701** Check to see if pExpr references any tables in pSrcList.
113702** Possible return values:
113703**
113704** 1 pExpr does references a table in pSrcList.
113705**
113706** 0 pExpr references some table that is not defined in either
113707** pSrcList or in subqueries of pExpr itself.
113708**
113709** -1 pExpr only references no tables at all, or it only
113710** references tables defined in subqueries of pExpr itself.
113711**
113712** As currently used, pExpr is always an aggregate function call. That
113713** fact is exploited for efficiency.
113714*/
113715SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
113716 Walker w;
113717 struct RefSrcList x;
113718 assert( pParse->db!=0 );
113719 memset(s: &w, c: 0, n: sizeof(w));
113720 memset(s: &x, c: 0, n: sizeof(x));
113721 w.xExprCallback = exprRefToSrcList;
113722 w.xSelectCallback = selectRefEnter;
113723 w.xSelectCallback2 = selectRefLeave;
113724 w.u.pRefSrcList = &x;
113725 x.db = pParse->db;
113726 x.pRef = pSrcList;
113727 assert( pExpr->op==TK_AGG_FUNCTION );
113728 assert( ExprUseXList(pExpr) );
113729 sqlite3WalkExprList(pWalker: &w, p: pExpr->x.pList);
113730#ifndef SQLITE_OMIT_WINDOWFUNC
113731 if( ExprHasProperty(pExpr, EP_WinFunc) ){
113732 sqlite3WalkExpr(pWalker: &w, pExpr: pExpr->y.pWin->pFilter);
113733 }
113734#endif
113735 if( x.aiExclude ) sqlite3DbNNFreeNN(db: pParse->db, p: x.aiExclude);
113736 if( w.eCode & 0x01 ){
113737 return 1;
113738 }else if( w.eCode ){
113739 return 0;
113740 }else{
113741 return -1;
113742 }
113743}
113744
113745/*
113746** This is a Walker expression node callback.
113747**
113748** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
113749** object that is referenced does not refer directly to the Expr. If
113750** it does, make a copy. This is done because the pExpr argument is
113751** subject to change.
113752**
113753** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete()
113754** which builds on the sqlite3ParserAddCleanup() mechanism.
113755*/
113756static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
113757 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))
113758 && pExpr->pAggInfo!=0
113759 ){
113760 AggInfo *pAggInfo = pExpr->pAggInfo;
113761 int iAgg = pExpr->iAgg;
113762 Parse *pParse = pWalker->pParse;
113763 sqlite3 *db = pParse->db;
113764 assert( iAgg>=0 );
113765 if( pExpr->op!=TK_AGG_FUNCTION ){
113766 if( iAgg<pAggInfo->nColumn
113767 && pAggInfo->aCol[iAgg].pCExpr==pExpr
113768 ){
113769 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
113770 if( pExpr ){
113771 pAggInfo->aCol[iAgg].pCExpr = pExpr;
113772 sqlite3ExprDeferredDelete(pParse, pExpr);
113773 }
113774 }
113775 }else{
113776 assert( pExpr->op==TK_AGG_FUNCTION );
113777 if( ALWAYS(iAgg<pAggInfo->nFunc)
113778 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
113779 ){
113780 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
113781 if( pExpr ){
113782 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
113783 sqlite3ExprDeferredDelete(pParse, pExpr);
113784 }
113785 }
113786 }
113787 }
113788 return WRC_Continue;
113789}
113790
113791/*
113792** Initialize a Walker object so that will persist AggInfo entries referenced
113793** by the tree that is walked.
113794*/
113795SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
113796 memset(s: pWalker, c: 0, n: sizeof(*pWalker));
113797 pWalker->pParse = pParse;
113798 pWalker->xExprCallback = agginfoPersistExprCb;
113799 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
113800}
113801
113802/*
113803** Add a new element to the pAggInfo->aCol[] array. Return the index of
113804** the new element. Return a negative number if malloc fails.
113805*/
113806static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
113807 int i;
113808 pInfo->aCol = sqlite3ArrayAllocate(
113809 db,
113810 pInfo->aCol,
113811 sizeof(pInfo->aCol[0]),
113812 &pInfo->nColumn,
113813 &i
113814 );
113815 return i;
113816}
113817
113818/*
113819** Add a new element to the pAggInfo->aFunc[] array. Return the index of
113820** the new element. Return a negative number if malloc fails.
113821*/
113822static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
113823 int i;
113824 pInfo->aFunc = sqlite3ArrayAllocate(
113825 db,
113826 pInfo->aFunc,
113827 sizeof(pInfo->aFunc[0]),
113828 &pInfo->nFunc,
113829 &i
113830 );
113831 return i;
113832}
113833
113834/*
113835** Search the AggInfo object for an aCol[] entry that has iTable and iColumn.
113836** Return the index in aCol[] of the entry that describes that column.
113837**
113838** If no prior entry is found, create a new one and return -1. The
113839** new column will have an index of pAggInfo->nColumn-1.
113840*/
113841static void findOrCreateAggInfoColumn(
113842 Parse *pParse, /* Parsing context */
113843 AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */
113844 Expr *pExpr /* Expr describing the column to find or insert */
113845){
113846 struct AggInfo_col *pCol;
113847 int k;
113848
113849 assert( pAggInfo->iFirstReg==0 );
113850 pCol = pAggInfo->aCol;
113851 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
113852 if( pCol->pCExpr==pExpr ) return;
113853 if( pCol->iTable==pExpr->iTable
113854 && pCol->iColumn==pExpr->iColumn
113855 && pExpr->op!=TK_IF_NULL_ROW
113856 ){
113857 goto fix_up_expr;
113858 }
113859 }
113860 k = addAggInfoColumn(db: pParse->db, pInfo: pAggInfo);
113861 if( k<0 ){
113862 /* OOM on resize */
113863 assert( pParse->db->mallocFailed );
113864 return;
113865 }
113866 pCol = &pAggInfo->aCol[k];
113867 assert( ExprUseYTab(pExpr) );
113868 pCol->pTab = pExpr->y.pTab;
113869 pCol->iTable = pExpr->iTable;
113870 pCol->iColumn = pExpr->iColumn;
113871 pCol->iSorterColumn = -1;
113872 pCol->pCExpr = pExpr;
113873 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
113874 int j, n;
113875 ExprList *pGB = pAggInfo->pGroupBy;
113876 struct ExprList_item *pTerm = pGB->a;
113877 n = pGB->nExpr;
113878 for(j=0; j<n; j++, pTerm++){
113879 Expr *pE = pTerm->pExpr;
113880 if( pE->op==TK_COLUMN
113881 && pE->iTable==pExpr->iTable
113882 && pE->iColumn==pExpr->iColumn
113883 ){
113884 pCol->iSorterColumn = j;
113885 break;
113886 }
113887 }
113888 }
113889 if( pCol->iSorterColumn<0 ){
113890 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
113891 }
113892fix_up_expr:
113893 ExprSetVVAProperty(pExpr, EP_NoReduce);
113894 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo );
113895 pExpr->pAggInfo = pAggInfo;
113896 if( pExpr->op==TK_COLUMN ){
113897 pExpr->op = TK_AGG_COLUMN;
113898 }
113899 pExpr->iAgg = (i16)k;
113900}
113901
113902/*
113903** This is the xExprCallback for a tree walker. It is used to
113904** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
113905** for additional information.
113906*/
113907static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
113908 int i;
113909 NameContext *pNC = pWalker->u.pNC;
113910 Parse *pParse = pNC->pParse;
113911 SrcList *pSrcList = pNC->pSrcList;
113912 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
113913
113914 assert( pNC->ncFlags & NC_UAggInfo );
113915 assert( pAggInfo->iFirstReg==0 );
113916 switch( pExpr->op ){
113917 default: {
113918 IndexedExpr *pIEpr;
113919 Expr tmp;
113920 assert( pParse->iSelfTab==0 );
113921 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
113922 if( pParse->pIdxEpr==0 ) break;
113923 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
113924 int iDataCur = pIEpr->iDataCur;
113925 if( iDataCur<0 ) continue;
113926 if( sqlite3ExprCompare(pParse: 0, pA: pExpr, pB: pIEpr->pExpr, iTab: iDataCur)==0 ) break;
113927 }
113928 if( pIEpr==0 ) break;
113929 if( NEVER(!ExprUseYTab(pExpr)) ) break;
113930 for(i=0; i<pSrcList->nSrc; i++){
113931 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
113932 }
113933 if( i>=pSrcList->nSrc ) break;
113934 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
113935 if( pParse->nErr ){ return WRC_Abort; }
113936
113937 /* If we reach this point, it means that expression pExpr can be
113938 ** translated into a reference to an index column as described by
113939 ** pIEpr.
113940 */
113941 memset(s: &tmp, c: 0, n: sizeof(tmp));
113942 tmp.op = TK_AGG_COLUMN;
113943 tmp.iTable = pIEpr->iIdxCur;
113944 tmp.iColumn = pIEpr->iIdxCol;
113945 findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr: &tmp);
113946 if( pParse->nErr ){ return WRC_Abort; }
113947 assert( pAggInfo->aCol!=0 );
113948 assert( tmp.iAgg<pAggInfo->nColumn );
113949 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
113950 pExpr->pAggInfo = pAggInfo;
113951 pExpr->iAgg = tmp.iAgg;
113952 return WRC_Prune;
113953 }
113954 case TK_IF_NULL_ROW:
113955 case TK_AGG_COLUMN:
113956 case TK_COLUMN: {
113957 testcase( pExpr->op==TK_AGG_COLUMN );
113958 testcase( pExpr->op==TK_COLUMN );
113959 testcase( pExpr->op==TK_IF_NULL_ROW );
113960 /* Check to see if the column is in one of the tables in the FROM
113961 ** clause of the aggregate query */
113962 if( ALWAYS(pSrcList!=0) ){
113963 SrcItem *pItem = pSrcList->a;
113964 for(i=0; i<pSrcList->nSrc; i++, pItem++){
113965 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
113966 if( pExpr->iTable==pItem->iCursor ){
113967 findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr);
113968 break;
113969 } /* endif pExpr->iTable==pItem->iCursor */
113970 } /* end loop over pSrcList */
113971 }
113972 return WRC_Continue;
113973 }
113974 case TK_AGG_FUNCTION: {
113975 if( (pNC->ncFlags & NC_InAggFunc)==0
113976 && pWalker->walkerDepth==pExpr->op2
113977 ){
113978 /* Check to see if pExpr is a duplicate of another aggregate
113979 ** function that is already in the pAggInfo structure
113980 */
113981 struct AggInfo_func *pItem = pAggInfo->aFunc;
113982 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
113983 if( pItem->pFExpr==pExpr ) break;
113984 if( sqlite3ExprCompare(pParse: 0, pA: pItem->pFExpr, pB: pExpr, iTab: -1)==0 ){
113985 break;
113986 }
113987 }
113988 if( i>=pAggInfo->nFunc ){
113989 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
113990 */
113991 u8 enc = ENC(pParse->db);
113992 i = addAggInfoFunc(db: pParse->db, pInfo: pAggInfo);
113993 if( i>=0 ){
113994 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
113995 pItem = &pAggInfo->aFunc[i];
113996 pItem->pFExpr = pExpr;
113997 assert( ExprUseUToken(pExpr) );
113998 pItem->pFunc = sqlite3FindFunction(pParse->db,
113999 pExpr->u.zToken,
114000 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
114001 if( pExpr->flags & EP_Distinct ){
114002 pItem->iDistinct = pParse->nTab++;
114003 }else{
114004 pItem->iDistinct = -1;
114005 }
114006 }
114007 }
114008 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
114009 */
114010 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
114011 ExprSetVVAProperty(pExpr, EP_NoReduce);
114012 pExpr->iAgg = (i16)i;
114013 pExpr->pAggInfo = pAggInfo;
114014 return WRC_Prune;
114015 }else{
114016 return WRC_Continue;
114017 }
114018 }
114019 }
114020 return WRC_Continue;
114021}
114022
114023/*
114024** Analyze the pExpr expression looking for aggregate functions and
114025** for variables that need to be added to AggInfo object that pNC->pAggInfo
114026** points to. Additional entries are made on the AggInfo object as
114027** necessary.
114028**
114029** This routine should only be called after the expression has been
114030** analyzed by sqlite3ResolveExprNames().
114031*/
114032SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
114033 Walker w;
114034 w.xExprCallback = analyzeAggregate;
114035 w.xSelectCallback = sqlite3WalkerDepthIncrease;
114036 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
114037 w.walkerDepth = 0;
114038 w.u.pNC = pNC;
114039 w.pParse = 0;
114040 assert( pNC->pSrcList!=0 );
114041 sqlite3WalkExpr(pWalker: &w, pExpr);
114042}
114043
114044/*
114045** Call sqlite3ExprAnalyzeAggregates() for every expression in an
114046** expression list. Return the number of errors.
114047**
114048** If an error is found, the analysis is cut short.
114049*/
114050SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
114051 struct ExprList_item *pItem;
114052 int i;
114053 if( pList ){
114054 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
114055 sqlite3ExprAnalyzeAggregates(pNC, pExpr: pItem->pExpr);
114056 }
114057 }
114058}
114059
114060/*
114061** Allocate a single new register for use to hold some intermediate result.
114062*/
114063SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
114064 if( pParse->nTempReg==0 ){
114065 return ++pParse->nMem;
114066 }
114067 return pParse->aTempReg[--pParse->nTempReg];
114068}
114069
114070/*
114071** Deallocate a register, making available for reuse for some other
114072** purpose.
114073*/
114074SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
114075 if( iReg ){
114076 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
114077 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
114078 pParse->aTempReg[pParse->nTempReg++] = iReg;
114079 }
114080 }
114081}
114082
114083/*
114084** Allocate or deallocate a block of nReg consecutive registers.
114085*/
114086SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
114087 int i, n;
114088 if( nReg==1 ) return sqlite3GetTempReg(pParse);
114089 i = pParse->iRangeReg;
114090 n = pParse->nRangeReg;
114091 if( nReg<=n ){
114092 pParse->iRangeReg += nReg;
114093 pParse->nRangeReg -= nReg;
114094 }else{
114095 i = pParse->nMem+1;
114096 pParse->nMem += nReg;
114097 }
114098 return i;
114099}
114100SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
114101 if( nReg==1 ){
114102 sqlite3ReleaseTempReg(pParse, iReg);
114103 return;
114104 }
114105 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
114106 if( nReg>pParse->nRangeReg ){
114107 pParse->nRangeReg = nReg;
114108 pParse->iRangeReg = iReg;
114109 }
114110}
114111
114112/*
114113** Mark all temporary registers as being unavailable for reuse.
114114**
114115** Always invoke this procedure after coding a subroutine or co-routine
114116** that might be invoked from other parts of the code, to ensure that
114117** the sub/co-routine does not use registers in common with the code that
114118** invokes the sub/co-routine.
114119*/
114120SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
114121 pParse->nTempReg = 0;
114122 pParse->nRangeReg = 0;
114123}
114124
114125/*
114126** Make sure sufficient registers have been allocated so that
114127** iReg is a valid register number.
114128*/
114129SQLITE_PRIVATE void sqlite3TouchRegister(Parse *pParse, int iReg){
114130 if( pParse->nMem<iReg ) pParse->nMem = iReg;
114131}
114132
114133#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
114134/*
114135** Return the latest reusable register in the set of all registers.
114136** The value returned is no less than iMin. If any register iMin or
114137** greater is in permanent use, then return one more than that last
114138** permanent register.
114139*/
114140SQLITE_PRIVATE int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){
114141 const ExprList *pList = pParse->pConstExpr;
114142 if( pList ){
114143 int i;
114144 for(i=0; i<pList->nExpr; i++){
114145 if( pList->a[i].u.iConstExprReg>=iMin ){
114146 iMin = pList->a[i].u.iConstExprReg + 1;
114147 }
114148 }
114149 }
114150 pParse->nTempReg = 0;
114151 pParse->nRangeReg = 0;
114152 return iMin;
114153}
114154#endif /* SQLITE_ENABLE_STAT4 || SQLITE_DEBUG */
114155
114156/*
114157** Validate that no temporary register falls within the range of
114158** iFirst..iLast, inclusive. This routine is only call from within assert()
114159** statements.
114160*/
114161#ifdef SQLITE_DEBUG
114162SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
114163 int i;
114164 if( pParse->nRangeReg>0
114165 && pParse->iRangeReg+pParse->nRangeReg > iFirst
114166 && pParse->iRangeReg <= iLast
114167 ){
114168 return 0;
114169 }
114170 for(i=0; i<pParse->nTempReg; i++){
114171 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
114172 return 0;
114173 }
114174 }
114175 if( pParse->pConstExpr ){
114176 ExprList *pList = pParse->pConstExpr;
114177 for(i=0; i<pList->nExpr; i++){
114178 int iReg = pList->a[i].u.iConstExprReg;
114179 if( iReg==0 ) continue;
114180 if( iReg>=iFirst && iReg<=iLast ) return 0;
114181 }
114182 }
114183 return 1;
114184}
114185#endif /* SQLITE_DEBUG */
114186
114187/************** End of expr.c ************************************************/
114188/************** Begin file alter.c *******************************************/
114189/*
114190** 2005 February 15
114191**
114192** The author disclaims copyright to this source code. In place of
114193** a legal notice, here is a blessing:
114194**
114195** May you do good and not evil.
114196** May you find forgiveness for yourself and forgive others.
114197** May you share freely, never taking more than you give.
114198**
114199*************************************************************************
114200** This file contains C code routines that used to generate VDBE code
114201** that implements the ALTER TABLE command.
114202*/
114203/* #include "sqliteInt.h" */
114204
114205/*
114206** The code in this file only exists if we are not omitting the
114207** ALTER TABLE logic from the build.
114208*/
114209#ifndef SQLITE_OMIT_ALTERTABLE
114210
114211/*
114212** Parameter zName is the name of a table that is about to be altered
114213** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
114214** If the table is a system table, this function leaves an error message
114215** in pParse->zErr (system tables may not be altered) and returns non-zero.
114216**
114217** Or, if zName is not a system table, zero is returned.
114218*/
114219static int isAlterableTable(Parse *pParse, Table *pTab){
114220 if( 0==sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)
114221#ifndef SQLITE_OMIT_VIRTUALTABLE
114222 || (pTab->tabFlags & TF_Eponymous)!=0
114223 || ( (pTab->tabFlags & TF_Shadow)!=0
114224 && sqlite3ReadOnlyShadowTables(db: pParse->db)
114225 )
114226#endif
114227 ){
114228 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be altered", pTab->zName);
114229 return 1;
114230 }
114231 return 0;
114232}
114233
114234/*
114235** Generate code to verify that the schemas of database zDb and, if
114236** bTemp is not true, database "temp", can still be parsed. This is
114237** called at the end of the generation of an ALTER TABLE ... RENAME ...
114238** statement to ensure that the operation has not rendered any schema
114239** objects unusable.
114240*/
114241static void renameTestSchema(
114242 Parse *pParse, /* Parse context */
114243 const char *zDb, /* Name of db to verify schema of */
114244 int bTemp, /* True if this is the temp db */
114245 const char *zWhen, /* "when" part of error message */
114246 int bNoDQS /* Do not allow DQS in the schema */
114247){
114248 pParse->colNamesSet = 1;
114249 sqlite3NestedParse(pParse,
114250 "SELECT 1 "
114251 "FROM \"%w\"." LEGACY_SCHEMA_TABLE " "
114252 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
114253 " AND sql NOT LIKE 'create virtual%%'"
114254 " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ",
114255 zDb,
114256 zDb, bTemp, zWhen, bNoDQS
114257 );
114258
114259 if( bTemp==0 ){
114260 sqlite3NestedParse(pParse,
114261 "SELECT 1 "
114262 "FROM temp." LEGACY_SCHEMA_TABLE " "
114263 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
114264 " AND sql NOT LIKE 'create virtual%%'"
114265 " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ",
114266 zDb, zWhen, bNoDQS
114267 );
114268 }
114269}
114270
114271/*
114272** Generate VM code to replace any double-quoted strings (but not double-quoted
114273** identifiers) within the "sql" column of the sqlite_schema table in
114274** database zDb with their single-quoted equivalents. If argument bTemp is
114275** not true, similarly update all SQL statements in the sqlite_schema table
114276** of the temp db.
114277*/
114278static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){
114279 sqlite3NestedParse(pParse,
114280 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE
114281 " SET sql = sqlite_rename_quotefix(%Q, sql)"
114282 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
114283 " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb
114284 );
114285 if( bTemp==0 ){
114286 sqlite3NestedParse(pParse,
114287 "UPDATE temp." LEGACY_SCHEMA_TABLE
114288 " SET sql = sqlite_rename_quotefix('temp', sql)"
114289 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
114290 " AND sql NOT LIKE 'create virtual%%'"
114291 );
114292 }
114293}
114294
114295/*
114296** Generate code to reload the schema for database iDb. And, if iDb!=1, for
114297** the temp database as well.
114298*/
114299static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){
114300 Vdbe *v = pParse->pVdbe;
114301 if( v ){
114302 sqlite3ChangeCookie(pParse, iDb);
114303 sqlite3VdbeAddParseSchemaOp(p: pParse->pVdbe, iDb, zWhere: 0, p5);
114304 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(p: pParse->pVdbe, iDb: 1, zWhere: 0, p5);
114305 }
114306}
114307
114308/*
114309** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
114310** command.
114311*/
114312SQLITE_PRIVATE void sqlite3AlterRenameTable(
114313 Parse *pParse, /* Parser context. */
114314 SrcList *pSrc, /* The table to rename. */
114315 Token *pName /* The new table name. */
114316){
114317 int iDb; /* Database that contains the table */
114318 char *zDb; /* Name of database iDb */
114319 Table *pTab; /* Table being renamed */
114320 char *zName = 0; /* NULL-terminated version of pName */
114321 sqlite3 *db = pParse->db; /* Database connection */
114322 int nTabName; /* Number of UTF-8 characters in zTabName */
114323 const char *zTabName; /* Original name of the table */
114324 Vdbe *v;
114325 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
114326
114327 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
114328 assert( pSrc->nSrc==1 );
114329 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
114330
114331 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
114332 if( !pTab ) goto exit_rename_table;
114333 iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
114334 zDb = db->aDb[iDb].zDbSName;
114335
114336 /* Get a NULL terminated version of the new table name. */
114337 zName = sqlite3NameFromToken(db, pName);
114338 if( !zName ) goto exit_rename_table;
114339
114340 /* Check that a table or index named 'zName' does not already exist
114341 ** in database iDb. If so, this is an error.
114342 */
114343 if( sqlite3FindTable(db, zName, zDb)
114344 || sqlite3FindIndex(db, zName, zDb)
114345 || sqlite3IsShadowTableOf(db, pTab, zName)
114346 ){
114347 sqlite3ErrorMsg(pParse,
114348 zFormat: "there is already another table or index with this name: %s", zName);
114349 goto exit_rename_table;
114350 }
114351
114352 /* Make sure it is not a system table being altered, or a reserved name
114353 ** that the table is being renamed to.
114354 */
114355 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
114356 goto exit_rename_table;
114357 }
114358 if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
114359 goto exit_rename_table;
114360 }
114361
114362#ifndef SQLITE_OMIT_VIEW
114363 if( IsView(pTab) ){
114364 sqlite3ErrorMsg(pParse, zFormat: "view %s may not be altered", pTab->zName);
114365 goto exit_rename_table;
114366 }
114367#endif
114368
114369#ifndef SQLITE_OMIT_AUTHORIZATION
114370 /* Invoke the authorization callback. */
114371 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
114372 goto exit_rename_table;
114373 }
114374#endif
114375
114376#ifndef SQLITE_OMIT_VIRTUALTABLE
114377 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
114378 goto exit_rename_table;
114379 }
114380 if( IsVirtual(pTab) ){
114381 pVTab = sqlite3GetVTable(db, pTab);
114382 if( pVTab->pVtab->pModule->xRename==0 ){
114383 pVTab = 0;
114384 }
114385 }
114386#endif
114387
114388 /* Begin a transaction for database iDb. Then modify the schema cookie
114389 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
114390 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
114391 ** nested SQL may raise an exception. */
114392 v = sqlite3GetVdbe(pParse);
114393 if( v==0 ){
114394 goto exit_rename_table;
114395 }
114396 sqlite3MayAbort(pParse);
114397
114398 /* figure out how many UTF-8 characters are in zName */
114399 zTabName = pTab->zName;
114400 nTabName = sqlite3Utf8CharLen(zIn: zTabName, nByte: -1);
114401
114402 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
114403 ** the schema to use the new table name. */
114404 sqlite3NestedParse(pParse,
114405 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
114406 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
114407 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
114408 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
114409 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
114410 );
114411
114412 /* Update the tbl_name and name columns of the sqlite_schema table
114413 ** as required. */
114414 sqlite3NestedParse(pParse,
114415 "UPDATE %Q." LEGACY_SCHEMA_TABLE " SET "
114416 "tbl_name = %Q, "
114417 "name = CASE "
114418 "WHEN type='table' THEN %Q "
114419 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
114420 " AND type='index' THEN "
114421 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
114422 "ELSE name END "
114423 "WHERE tbl_name=%Q COLLATE nocase AND "
114424 "(type='table' OR type='index' OR type='trigger');",
114425 zDb,
114426 zName, zName, zName,
114427 nTabName, zTabName
114428 );
114429
114430#ifndef SQLITE_OMIT_AUTOINCREMENT
114431 /* If the sqlite_sequence table exists in this database, then update
114432 ** it with the new table name.
114433 */
114434 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
114435 sqlite3NestedParse(pParse,
114436 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
114437 zDb, zName, pTab->zName);
114438 }
114439#endif
114440
114441 /* If the table being renamed is not itself part of the temp database,
114442 ** edit view and trigger definitions within the temp database
114443 ** as required. */
114444 if( iDb!=1 ){
114445 sqlite3NestedParse(pParse,
114446 "UPDATE sqlite_temp_schema SET "
114447 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
114448 "tbl_name = "
114449 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
114450 " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0) "
114451 "THEN %Q ELSE tbl_name END "
114452 "WHERE type IN ('view', 'trigger')"
114453 , zDb, zTabName, zName, zTabName, zDb, zName);
114454 }
114455
114456 /* If this is a virtual table, invoke the xRename() function if
114457 ** one is defined. The xRename() callback will modify the names
114458 ** of any resources used by the v-table implementation (including other
114459 ** SQLite tables) that are identified by the name of the virtual table.
114460 */
114461#ifndef SQLITE_OMIT_VIRTUALTABLE
114462 if( pVTab ){
114463 int i = ++pParse->nMem;
114464 sqlite3VdbeLoadString(p: v, iDest: i, zStr: zName);
114465 sqlite3VdbeAddOp4(p: v, OP_VRename, p1: i, p2: 0, p3: 0,zP4: (const char*)pVTab, P4_VTAB);
114466 }
114467#endif
114468
114469 renameReloadSchema(pParse, iDb, INITFLAG_AlterRename);
114470 renameTestSchema(pParse, zDb, bTemp: iDb==1, zWhen: "after rename", bNoDQS: 0);
114471
114472exit_rename_table:
114473 sqlite3SrcListDelete(db, pSrc);
114474 sqlite3DbFree(db, p: zName);
114475}
114476
114477/*
114478** Write code that will raise an error if the table described by
114479** zDb and zTab is not empty.
114480*/
114481static void sqlite3ErrorIfNotEmpty(
114482 Parse *pParse, /* Parsing context */
114483 const char *zDb, /* Schema holding the table */
114484 const char *zTab, /* Table to check for empty */
114485 const char *zErr /* Error message text */
114486){
114487 sqlite3NestedParse(pParse,
114488 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
114489 zErr, zDb, zTab
114490 );
114491}
114492
114493/*
114494** This function is called after an "ALTER TABLE ... ADD" statement
114495** has been parsed. Argument pColDef contains the text of the new
114496** column definition.
114497**
114498** The Table structure pParse->pNewTable was extended to include
114499** the new column during parsing.
114500*/
114501SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
114502 Table *pNew; /* Copy of pParse->pNewTable */
114503 Table *pTab; /* Table being altered */
114504 int iDb; /* Database number */
114505 const char *zDb; /* Database name */
114506 const char *zTab; /* Table name */
114507 char *zCol; /* Null-terminated column definition */
114508 Column *pCol; /* The new column */
114509 Expr *pDflt; /* Default value for the new column */
114510 sqlite3 *db; /* The database connection; */
114511 Vdbe *v; /* The prepared statement under construction */
114512 int r1; /* Temporary registers */
114513
114514 db = pParse->db;
114515 assert( db->pParse==pParse );
114516 if( pParse->nErr ) return;
114517 assert( db->mallocFailed==0 );
114518 pNew = pParse->pNewTable;
114519 assert( pNew );
114520
114521 assert( sqlite3BtreeHoldsAllMutexes(db) );
114522 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
114523 zDb = db->aDb[iDb].zDbSName;
114524 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
114525 pCol = &pNew->aCol[pNew->nCol-1];
114526 pDflt = sqlite3ColumnExpr(pNew, pCol);
114527 pTab = sqlite3FindTable(db, zTab, zDb);
114528 assert( pTab );
114529
114530#ifndef SQLITE_OMIT_AUTHORIZATION
114531 /* Invoke the authorization callback. */
114532 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
114533 return;
114534 }
114535#endif
114536
114537
114538 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
114539 ** If there is a NOT NULL constraint, then the default value for the
114540 ** column must not be NULL.
114541 */
114542 if( pCol->colFlags & COLFLAG_PRIMKEY ){
114543 sqlite3ErrorMsg(pParse, zFormat: "Cannot add a PRIMARY KEY column");
114544 return;
114545 }
114546 if( pNew->pIndex ){
114547 sqlite3ErrorMsg(pParse,
114548 zFormat: "Cannot add a UNIQUE column");
114549 return;
114550 }
114551 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
114552 /* If the default value for the new column was specified with a
114553 ** literal NULL, then set pDflt to 0. This simplifies checking
114554 ** for an SQL NULL default below.
114555 */
114556 assert( pDflt==0 || pDflt->op==TK_SPAN );
114557 if( pDflt && pDflt->pLeft->op==TK_NULL ){
114558 pDflt = 0;
114559 }
114560 assert( IsOrdinaryTable(pNew) );
114561 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
114562 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
114563 zErr: "Cannot add a REFERENCES column with non-NULL default value");
114564 }
114565 if( pCol->notNull && !pDflt ){
114566 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
114567 zErr: "Cannot add a NOT NULL column with default value NULL");
114568 }
114569
114570
114571 /* Ensure the default expression is something that sqlite3ValueFromExpr()
114572 ** can handle (i.e. not CURRENT_TIME etc.)
114573 */
114574 if( pDflt ){
114575 sqlite3_value *pVal = 0;
114576 int rc;
114577 rc = sqlite3ValueFromExpr(db, pExpr: pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, ppVal: &pVal);
114578 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
114579 if( rc!=SQLITE_OK ){
114580 assert( db->mallocFailed == 1 );
114581 return;
114582 }
114583 if( !pVal ){
114584 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
114585 zErr: "Cannot add a column with non-constant default");
114586 }
114587 sqlite3ValueFree(v: pVal);
114588 }
114589 }else if( pCol->colFlags & COLFLAG_STORED ){
114590 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, zErr: "cannot add a STORED column");
114591 }
114592
114593
114594 /* Modify the CREATE TABLE statement. */
114595 zCol = sqlite3DbStrNDup(db, z: (char*)pColDef->z, n: pColDef->n);
114596 if( zCol ){
114597 char *zEnd = &zCol[pColDef->n-1];
114598 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
114599 *zEnd-- = '\0';
114600 }
114601 /* substr() operations on characters, but addColOffset is in bytes. So we
114602 ** have to use printf() to translate between these units: */
114603 assert( IsOrdinaryTable(pTab) );
114604 assert( IsOrdinaryTable(pNew) );
114605 sqlite3NestedParse(pParse,
114606 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
114607 "sql = printf('%%.%ds, ',sql) || %Q"
114608 " || substr(sql,1+length(printf('%%.%ds',sql))) "
114609 "WHERE type = 'table' AND name = %Q",
114610 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
114611 zTab
114612 );
114613 sqlite3DbFree(db, p: zCol);
114614 }
114615
114616 v = sqlite3GetVdbe(pParse);
114617 if( v ){
114618 /* Make sure the schema version is at least 3. But do not upgrade
114619 ** from less than 3 to 4, as that will corrupt any preexisting DESC
114620 ** index.
114621 */
114622 r1 = sqlite3GetTempReg(pParse);
114623 sqlite3VdbeAddOp3(p: v, OP_ReadCookie, p1: iDb, p2: r1, BTREE_FILE_FORMAT);
114624 sqlite3VdbeUsesBtree(p: v, i: iDb);
114625 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: r1, p2: -2);
114626 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: r1, p2: sqlite3VdbeCurrentAddr(p: v)+2);
114627 VdbeCoverage(v);
114628 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_FILE_FORMAT, p3: 3);
114629 sqlite3ReleaseTempReg(pParse, iReg: r1);
114630
114631 /* Reload the table definition */
114632 renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd);
114633
114634 /* Verify that constraints are still satisfied */
114635 if( pNew->pCheck!=0
114636 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
114637 ){
114638 sqlite3NestedParse(pParse,
114639 "SELECT CASE WHEN quick_check GLOB 'CHECK*'"
114640 " THEN raise(ABORT,'CHECK constraint failed')"
114641 " ELSE raise(ABORT,'NOT NULL constraint failed')"
114642 " END"
114643 " FROM pragma_quick_check(%Q,%Q)"
114644 " WHERE quick_check GLOB 'CHECK*' OR quick_check GLOB 'NULL*'",
114645 zTab, zDb
114646 );
114647 }
114648 }
114649}
114650
114651/*
114652** This function is called by the parser after the table-name in
114653** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
114654** pSrc is the full-name of the table being altered.
114655**
114656** This routine makes a (partial) copy of the Table structure
114657** for the table being altered and sets Parse.pNewTable to point
114658** to it. Routines called by the parser as the column definition
114659** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
114660** the copy. The copy of the Table structure is deleted by tokenize.c
114661** after parsing is finished.
114662**
114663** Routine sqlite3AlterFinishAddColumn() will be called to complete
114664** coding the "ALTER TABLE ... ADD" statement.
114665*/
114666SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
114667 Table *pNew;
114668 Table *pTab;
114669 int iDb;
114670 int i;
114671 int nAlloc;
114672 sqlite3 *db = pParse->db;
114673
114674 /* Look up the table being altered. */
114675 assert( pParse->pNewTable==0 );
114676 assert( sqlite3BtreeHoldsAllMutexes(db) );
114677 if( db->mallocFailed ) goto exit_begin_add_column;
114678 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
114679 if( !pTab ) goto exit_begin_add_column;
114680
114681#ifndef SQLITE_OMIT_VIRTUALTABLE
114682 if( IsVirtual(pTab) ){
114683 sqlite3ErrorMsg(pParse, zFormat: "virtual tables may not be altered");
114684 goto exit_begin_add_column;
114685 }
114686#endif
114687
114688 /* Make sure this is not an attempt to ALTER a view. */
114689 if( IsView(pTab) ){
114690 sqlite3ErrorMsg(pParse, zFormat: "Cannot add a column to a view");
114691 goto exit_begin_add_column;
114692 }
114693 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
114694 goto exit_begin_add_column;
114695 }
114696
114697 sqlite3MayAbort(pParse);
114698 assert( IsOrdinaryTable(pTab) );
114699 assert( pTab->u.tab.addColOffset>0 );
114700 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
114701
114702 /* Put a copy of the Table struct in Parse.pNewTable for the
114703 ** sqlite3AddColumn() function and friends to modify. But modify
114704 ** the name by adding an "sqlite_altertab_" prefix. By adding this
114705 ** prefix, we insure that the name will not collide with an existing
114706 ** table because user table are not allowed to have the "sqlite_"
114707 ** prefix on their name.
114708 */
114709 pNew = (Table*)sqlite3DbMallocZero(db, n: sizeof(Table));
114710 if( !pNew ) goto exit_begin_add_column;
114711 pParse->pNewTable = pNew;
114712 pNew->nTabRef = 1;
114713 pNew->nCol = pTab->nCol;
114714 assert( pNew->nCol>0 );
114715 nAlloc = (((pNew->nCol-1)/8)*8)+8;
114716 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
114717 pNew->aCol = (Column*)sqlite3DbMallocZero(db, n: sizeof(Column)*nAlloc);
114718 pNew->zName = sqlite3MPrintf(db, zFormat: "sqlite_altertab_%s", pTab->zName);
114719 if( !pNew->aCol || !pNew->zName ){
114720 assert( db->mallocFailed );
114721 goto exit_begin_add_column;
114722 }
114723 memcpy(dest: pNew->aCol, src: pTab->aCol, n: sizeof(Column)*pNew->nCol);
114724 for(i=0; i<pNew->nCol; i++){
114725 Column *pCol = &pNew->aCol[i];
114726 pCol->zCnName = sqlite3DbStrDup(db, z: pCol->zCnName);
114727 pCol->hName = sqlite3StrIHash(z: pCol->zCnName);
114728 }
114729 assert( IsOrdinaryTable(pNew) );
114730 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, p: pTab->u.tab.pDfltList, flags: 0);
114731 pNew->pSchema = db->aDb[iDb].pSchema;
114732 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
114733 assert( pNew->nTabRef==1 );
114734
114735exit_begin_add_column:
114736 sqlite3SrcListDelete(db, pSrc);
114737 return;
114738}
114739
114740/*
114741** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
114742** command. This function checks if the table is a view or virtual
114743** table (columns of views or virtual tables may not be renamed). If so,
114744** it loads an error message into pParse and returns non-zero.
114745**
114746** Or, if pTab is not a view or virtual table, zero is returned.
114747*/
114748#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
114749static int isRealTable(Parse *pParse, Table *pTab, int bDrop){
114750 const char *zType = 0;
114751#ifndef SQLITE_OMIT_VIEW
114752 if( IsView(pTab) ){
114753 zType = "view";
114754 }
114755#endif
114756#ifndef SQLITE_OMIT_VIRTUALTABLE
114757 if( IsVirtual(pTab) ){
114758 zType = "virtual table";
114759 }
114760#endif
114761 if( zType ){
114762 sqlite3ErrorMsg(pParse, zFormat: "cannot %s %s \"%s\"",
114763 (bDrop ? "drop column from" : "rename columns of"),
114764 zType, pTab->zName
114765 );
114766 return 1;
114767 }
114768 return 0;
114769}
114770#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
114771# define isRealTable(x,y,z) (0)
114772#endif
114773
114774/*
114775** Handles the following parser reduction:
114776**
114777** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
114778*/
114779SQLITE_PRIVATE void sqlite3AlterRenameColumn(
114780 Parse *pParse, /* Parsing context */
114781 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
114782 Token *pOld, /* Name of column being changed */
114783 Token *pNew /* New column name */
114784){
114785 sqlite3 *db = pParse->db; /* Database connection */
114786 Table *pTab; /* Table being updated */
114787 int iCol; /* Index of column being renamed */
114788 char *zOld = 0; /* Old column name */
114789 char *zNew = 0; /* New column name */
114790 const char *zDb; /* Name of schema containing the table */
114791 int iSchema; /* Index of the schema */
114792 int bQuote; /* True to quote the new name */
114793
114794 /* Locate the table to be altered */
114795 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
114796 if( !pTab ) goto exit_rename_column;
114797
114798 /* Cannot alter a system table */
114799 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
114800 if( SQLITE_OK!=isRealTable(pParse, pTab, bDrop: 0) ) goto exit_rename_column;
114801
114802 /* Which schema holds the table to be altered */
114803 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
114804 assert( iSchema>=0 );
114805 zDb = db->aDb[iSchema].zDbSName;
114806
114807#ifndef SQLITE_OMIT_AUTHORIZATION
114808 /* Invoke the authorization callback. */
114809 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
114810 goto exit_rename_column;
114811 }
114812#endif
114813
114814 /* Make sure the old name really is a column name in the table to be
114815 ** altered. Set iCol to be the index of the column being renamed */
114816 zOld = sqlite3NameFromToken(db, pOld);
114817 if( !zOld ) goto exit_rename_column;
114818 for(iCol=0; iCol<pTab->nCol; iCol++){
114819 if( 0==sqlite3StrICmp(zLeft: pTab->aCol[iCol].zCnName, zRight: zOld) ) break;
114820 }
114821 if( iCol==pTab->nCol ){
114822 sqlite3ErrorMsg(pParse, zFormat: "no such column: \"%T\"", pOld);
114823 goto exit_rename_column;
114824 }
114825
114826 /* Ensure the schema contains no double-quoted strings */
114827 renameTestSchema(pParse, zDb, bTemp: iSchema==1, zWhen: "", bNoDQS: 0);
114828 renameFixQuotes(pParse, zDb, bTemp: iSchema==1);
114829
114830 /* Do the rename operation using a recursive UPDATE statement that
114831 ** uses the sqlite_rename_column() SQL function to compute the new
114832 ** CREATE statement text for the sqlite_schema table.
114833 */
114834 sqlite3MayAbort(pParse);
114835 zNew = sqlite3NameFromToken(db, pNew);
114836 if( !zNew ) goto exit_rename_column;
114837 assert( pNew->n>0 );
114838 bQuote = sqlite3Isquote(pNew->z[0]);
114839 sqlite3NestedParse(pParse,
114840 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
114841 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
114842 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
114843 " AND (type != 'index' OR tbl_name = %Q)",
114844 zDb,
114845 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
114846 pTab->zName
114847 );
114848
114849 sqlite3NestedParse(pParse,
114850 "UPDATE temp." LEGACY_SCHEMA_TABLE " SET "
114851 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
114852 "WHERE type IN ('trigger', 'view')",
114853 zDb, pTab->zName, iCol, zNew, bQuote
114854 );
114855
114856 /* Drop and reload the database schema. */
114857 renameReloadSchema(pParse, iDb: iSchema, INITFLAG_AlterRename);
114858 renameTestSchema(pParse, zDb, bTemp: iSchema==1, zWhen: "after rename", bNoDQS: 1);
114859
114860 exit_rename_column:
114861 sqlite3SrcListDelete(db, pSrc);
114862 sqlite3DbFree(db, p: zOld);
114863 sqlite3DbFree(db, p: zNew);
114864 return;
114865}
114866
114867/*
114868** Each RenameToken object maps an element of the parse tree into
114869** the token that generated that element. The parse tree element
114870** might be one of:
114871**
114872** * A pointer to an Expr that represents an ID
114873** * The name of a table column in Column.zName
114874**
114875** A list of RenameToken objects can be constructed during parsing.
114876** Each new object is created by sqlite3RenameTokenMap().
114877** As the parse tree is transformed, the sqlite3RenameTokenRemap()
114878** routine is used to keep the mapping current.
114879**
114880** After the parse finishes, renameTokenFind() routine can be used
114881** to look up the actual token value that created some element in
114882** the parse tree.
114883*/
114884struct RenameToken {
114885 const void *p; /* Parse tree element created by token t */
114886 Token t; /* The token that created parse tree element p */
114887 RenameToken *pNext; /* Next is a list of all RenameToken objects */
114888};
114889
114890/*
114891** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
114892** down into the Walker.
114893*/
114894typedef struct RenameCtx RenameCtx;
114895struct RenameCtx {
114896 RenameToken *pList; /* List of tokens to overwrite */
114897 int nList; /* Number of tokens in pList */
114898 int iCol; /* Index of column being renamed */
114899 Table *pTab; /* Table being ALTERed */
114900 const char *zOld; /* Old column name */
114901};
114902
114903#ifdef SQLITE_DEBUG
114904/*
114905** This function is only for debugging. It performs two tasks:
114906**
114907** 1. Checks that pointer pPtr does not already appear in the
114908** rename-token list.
114909**
114910** 2. Dereferences each pointer in the rename-token list.
114911**
114912** The second is most effective when debugging under valgrind or
114913** address-sanitizer or similar. If any of these pointers no longer
114914** point to valid objects, an exception is raised by the memory-checking
114915** tool.
114916**
114917** The point of this is to prevent comparisons of invalid pointer values.
114918** Even though this always seems to work, it is undefined according to the
114919** C standard. Example of undefined comparison:
114920**
114921** sqlite3_free(x);
114922** if( x==y ) ...
114923**
114924** Technically, as x no longer points into a valid object or to the byte
114925** following a valid object, it may not be used in comparison operations.
114926*/
114927static void renameTokenCheckAll(Parse *pParse, const void *pPtr){
114928 assert( pParse==pParse->db->pParse );
114929 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
114930 if( pParse->nErr==0 ){
114931 const RenameToken *p;
114932 u32 i = 1;
114933 for(p=pParse->pRename; p; p=p->pNext){
114934 if( p->p ){
114935 assert( p->p!=pPtr );
114936 i += *(u8*)(p->p) | 1;
114937 }
114938 }
114939 assert( i>0 );
114940 }
114941}
114942#else
114943# define renameTokenCheckAll(x,y)
114944#endif
114945
114946/*
114947** Remember that the parser tree element pPtr was created using
114948** the token pToken.
114949**
114950** In other words, construct a new RenameToken object and add it
114951** to the list of RenameToken objects currently being built up
114952** in pParse->pRename.
114953**
114954** The pPtr argument is returned so that this routine can be used
114955** with tail recursion in tokenExpr() routine, for a small performance
114956** improvement.
114957*/
114958SQLITE_PRIVATE const void *sqlite3RenameTokenMap(
114959 Parse *pParse,
114960 const void *pPtr,
114961 const Token *pToken
114962){
114963 RenameToken *pNew;
114964 assert( pPtr || pParse->db->mallocFailed );
114965 renameTokenCheckAll(pParse, pPtr);
114966 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
114967 pNew = sqlite3DbMallocZero(db: pParse->db, n: sizeof(RenameToken));
114968 if( pNew ){
114969 pNew->p = pPtr;
114970 pNew->t = *pToken;
114971 pNew->pNext = pParse->pRename;
114972 pParse->pRename = pNew;
114973 }
114974 }
114975
114976 return pPtr;
114977}
114978
114979/*
114980** It is assumed that there is already a RenameToken object associated
114981** with parse tree element pFrom. This function remaps the associated token
114982** to parse tree element pTo.
114983*/
114984SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, const void *pTo, const void *pFrom){
114985 RenameToken *p;
114986 renameTokenCheckAll(pParse, pTo);
114987 for(p=pParse->pRename; p; p=p->pNext){
114988 if( p->p==pFrom ){
114989 p->p = pTo;
114990 break;
114991 }
114992 }
114993}
114994
114995/*
114996** Walker callback used by sqlite3RenameExprUnmap().
114997*/
114998static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
114999 Parse *pParse = pWalker->pParse;
115000 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (const void*)pExpr);
115001 if( ExprUseYTab(pExpr) ){
115002 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (const void*)&pExpr->y.pTab);
115003 }
115004 return WRC_Continue;
115005}
115006
115007/*
115008** Iterate through the Select objects that are part of WITH clauses attached
115009** to select statement pSelect.
115010*/
115011static void renameWalkWith(Walker *pWalker, Select *pSelect){
115012 With *pWith = pSelect->pWith;
115013 if( pWith ){
115014 Parse *pParse = pWalker->pParse;
115015 int i;
115016 With *pCopy = 0;
115017 assert( pWith->nCte>0 );
115018 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
115019 /* Push a copy of the With object onto the with-stack. We use a copy
115020 ** here as the original will be expanded and resolved (flags SF_Expanded
115021 ** and SF_Resolved) below. And the parser code that uses the with-stack
115022 ** fails if the Select objects on it have already been expanded and
115023 ** resolved. */
115024 pCopy = sqlite3WithDup(db: pParse->db, p: pWith);
115025 pCopy = sqlite3WithPush(pParse, pCopy, 1);
115026 }
115027 for(i=0; i<pWith->nCte; i++){
115028 Select *p = pWith->a[i].pSelect;
115029 NameContext sNC;
115030 memset(s: &sNC, c: 0, n: sizeof(sNC));
115031 sNC.pParse = pParse;
115032 if( pCopy ) sqlite3SelectPrep(sNC.pParse, p, &sNC);
115033 if( sNC.pParse->db->mallocFailed ) return;
115034 sqlite3WalkSelect(pWalker, p);
115035 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
115036 }
115037 if( pCopy && pParse->pWith==pCopy ){
115038 pParse->pWith = pCopy->pOuter;
115039 }
115040 }
115041}
115042
115043/*
115044** Unmap all tokens in the IdList object passed as the second argument.
115045*/
115046static void unmapColumnIdlistNames(
115047 Parse *pParse,
115048 const IdList *pIdList
115049){
115050 int ii;
115051 assert( pIdList!=0 );
115052 for(ii=0; ii<pIdList->nId; ii++){
115053 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (const void*)pIdList->a[ii].zName);
115054 }
115055}
115056
115057/*
115058** Walker callback used by sqlite3RenameExprUnmap().
115059*/
115060static int renameUnmapSelectCb(Walker *pWalker, Select *p){
115061 Parse *pParse = pWalker->pParse;
115062 int i;
115063 if( pParse->nErr ) return WRC_Abort;
115064 testcase( p->selFlags & SF_View );
115065 testcase( p->selFlags & SF_CopyCte );
115066 if( p->selFlags & (SF_View|SF_CopyCte) ){
115067 return WRC_Prune;
115068 }
115069 if( ALWAYS(p->pEList) ){
115070 ExprList *pList = p->pEList;
115071 for(i=0; i<pList->nExpr; i++){
115072 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
115073 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pList->a[i].zEName);
115074 }
115075 }
115076 }
115077 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
115078 SrcList *pSrc = p->pSrc;
115079 for(i=0; i<pSrc->nSrc; i++){
115080 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pSrc->a[i].zName);
115081 if( pSrc->a[i].fg.isUsing==0 ){
115082 sqlite3WalkExpr(pWalker, pExpr: pSrc->a[i].u3.pOn);
115083 }else{
115084 unmapColumnIdlistNames(pParse, pIdList: pSrc->a[i].u3.pUsing);
115085 }
115086 }
115087 }
115088
115089 renameWalkWith(pWalker, pSelect: p);
115090 return WRC_Continue;
115091}
115092
115093/*
115094** Remove all nodes that are part of expression pExpr from the rename list.
115095*/
115096SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
115097 u8 eMode = pParse->eParseMode;
115098 Walker sWalker;
115099 memset(s: &sWalker, c: 0, n: sizeof(Walker));
115100 sWalker.pParse = pParse;
115101 sWalker.xExprCallback = renameUnmapExprCb;
115102 sWalker.xSelectCallback = renameUnmapSelectCb;
115103 pParse->eParseMode = PARSE_MODE_UNMAP;
115104 sqlite3WalkExpr(pWalker: &sWalker, pExpr);
115105 pParse->eParseMode = eMode;
115106}
115107
115108/*
115109** Remove all nodes that are part of expression-list pEList from the
115110** rename list.
115111*/
115112SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
115113 if( pEList ){
115114 int i;
115115 Walker sWalker;
115116 memset(s: &sWalker, c: 0, n: sizeof(Walker));
115117 sWalker.pParse = pParse;
115118 sWalker.xExprCallback = renameUnmapExprCb;
115119 sqlite3WalkExprList(pWalker: &sWalker, p: pEList);
115120 for(i=0; i<pEList->nExpr; i++){
115121 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
115122 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pEList->a[i].zEName);
115123 }
115124 }
115125 }
115126}
115127
115128/*
115129** Free the list of RenameToken objects given in the second argument
115130*/
115131static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
115132 RenameToken *pNext;
115133 RenameToken *p;
115134 for(p=pToken; p; p=pNext){
115135 pNext = p->pNext;
115136 sqlite3DbFree(db, p);
115137 }
115138}
115139
115140/*
115141** Search the Parse object passed as the first argument for a RenameToken
115142** object associated with parse tree element pPtr. If found, return a pointer
115143** to it. Otherwise, return NULL.
115144**
115145** If the second argument passed to this function is not NULL and a matching
115146** RenameToken object is found, remove it from the Parse object and add it to
115147** the list maintained by the RenameCtx object.
115148*/
115149static RenameToken *renameTokenFind(
115150 Parse *pParse,
115151 struct RenameCtx *pCtx,
115152 const void *pPtr
115153){
115154 RenameToken **pp;
115155 if( NEVER(pPtr==0) ){
115156 return 0;
115157 }
115158 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
115159 if( (*pp)->p==pPtr ){
115160 RenameToken *pToken = *pp;
115161 if( pCtx ){
115162 *pp = pToken->pNext;
115163 pToken->pNext = pCtx->pList;
115164 pCtx->pList = pToken;
115165 pCtx->nList++;
115166 }
115167 return pToken;
115168 }
115169 }
115170 return 0;
115171}
115172
115173/*
115174** This is a Walker select callback. It does nothing. It is only required
115175** because without a dummy callback, sqlite3WalkExpr() and similar do not
115176** descend into sub-select statements.
115177*/
115178static int renameColumnSelectCb(Walker *pWalker, Select *p){
115179 if( p->selFlags & (SF_View|SF_CopyCte) ){
115180 testcase( p->selFlags & SF_View );
115181 testcase( p->selFlags & SF_CopyCte );
115182 return WRC_Prune;
115183 }
115184 renameWalkWith(pWalker, pSelect: p);
115185 return WRC_Continue;
115186}
115187
115188/*
115189** This is a Walker expression callback.
115190**
115191** For every TK_COLUMN node in the expression tree, search to see
115192** if the column being references is the column being renamed by an
115193** ALTER TABLE statement. If it is, then attach its associated
115194** RenameToken object to the list of RenameToken objects being
115195** constructed in RenameCtx object at pWalker->u.pRename.
115196*/
115197static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
115198 RenameCtx *p = pWalker->u.pRename;
115199 if( pExpr->op==TK_TRIGGER
115200 && pExpr->iColumn==p->iCol
115201 && pWalker->pParse->pTriggerTab==p->pTab
115202 ){
115203 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: (void*)pExpr);
115204 }else if( pExpr->op==TK_COLUMN
115205 && pExpr->iColumn==p->iCol
115206 && ALWAYS(ExprUseYTab(pExpr))
115207 && p->pTab==pExpr->y.pTab
115208 ){
115209 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: (void*)pExpr);
115210 }
115211 return WRC_Continue;
115212}
115213
115214/*
115215** The RenameCtx contains a list of tokens that reference a column that
115216** is being renamed by an ALTER TABLE statement. Return the "last"
115217** RenameToken in the RenameCtx and remove that RenameToken from the
115218** RenameContext. "Last" means the last RenameToken encountered when
115219** the input SQL is parsed from left to right. Repeated calls to this routine
115220** return all column name tokens in the order that they are encountered
115221** in the SQL statement.
115222*/
115223static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
115224 RenameToken *pBest = pCtx->pList;
115225 RenameToken *pToken;
115226 RenameToken **pp;
115227
115228 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
115229 if( pToken->t.z>pBest->t.z ) pBest = pToken;
115230 }
115231 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
115232 *pp = pBest->pNext;
115233
115234 return pBest;
115235}
115236
115237/*
115238** An error occurred while parsing or otherwise processing a database
115239** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
115240** ALTER TABLE RENAME COLUMN program. The error message emitted by the
115241** sub-routine is currently stored in pParse->zErrMsg. This function
115242** adds context to the error message and then stores it in pCtx.
115243*/
115244static void renameColumnParseError(
115245 sqlite3_context *pCtx,
115246 const char *zWhen,
115247 sqlite3_value *pType,
115248 sqlite3_value *pObject,
115249 Parse *pParse
115250){
115251 const char *zT = (const char*)sqlite3_value_text(pVal: pType);
115252 const char *zN = (const char*)sqlite3_value_text(pVal: pObject);
115253 char *zErr;
115254
115255 zErr = sqlite3MPrintf(db: pParse->db, zFormat: "error in %s %s%s%s: %s",
115256 zT, zN, (zWhen[0] ? " " : ""), zWhen,
115257 pParse->zErrMsg
115258 );
115259 sqlite3_result_error(pCtx, z: zErr, n: -1);
115260 sqlite3DbFree(db: pParse->db, p: zErr);
115261}
115262
115263/*
115264** For each name in the the expression-list pEList (i.e. each
115265** pEList->a[i].zName) that matches the string in zOld, extract the
115266** corresponding rename-token from Parse object pParse and add it
115267** to the RenameCtx pCtx.
115268*/
115269static void renameColumnElistNames(
115270 Parse *pParse,
115271 RenameCtx *pCtx,
115272 const ExprList *pEList,
115273 const char *zOld
115274){
115275 if( pEList ){
115276 int i;
115277 for(i=0; i<pEList->nExpr; i++){
115278 const char *zName = pEList->a[i].zEName;
115279 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
115280 && ALWAYS(zName!=0)
115281 && 0==sqlite3_stricmp(zLeft: zName, zRight: zOld)
115282 ){
115283 renameTokenFind(pParse, pCtx, pPtr: (const void*)zName);
115284 }
115285 }
115286 }
115287}
115288
115289/*
115290** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
115291** that matches the string in zOld, extract the corresponding rename-token
115292** from Parse object pParse and add it to the RenameCtx pCtx.
115293*/
115294static void renameColumnIdlistNames(
115295 Parse *pParse,
115296 RenameCtx *pCtx,
115297 const IdList *pIdList,
115298 const char *zOld
115299){
115300 if( pIdList ){
115301 int i;
115302 for(i=0; i<pIdList->nId; i++){
115303 const char *zName = pIdList->a[i].zName;
115304 if( 0==sqlite3_stricmp(zLeft: zName, zRight: zOld) ){
115305 renameTokenFind(pParse, pCtx, pPtr: (const void*)zName);
115306 }
115307 }
115308 }
115309}
115310
115311
115312/*
115313** Parse the SQL statement zSql using Parse object (*p). The Parse object
115314** is initialized by this function before it is used.
115315*/
115316static int renameParseSql(
115317 Parse *p, /* Memory to use for Parse object */
115318 const char *zDb, /* Name of schema SQL belongs to */
115319 sqlite3 *db, /* Database handle */
115320 const char *zSql, /* SQL to parse */
115321 int bTemp /* True if SQL is from temp schema */
115322){
115323 int rc;
115324
115325 sqlite3ParseObjectInit(p, db);
115326 if( zSql==0 ){
115327 return SQLITE_NOMEM;
115328 }
115329 if( sqlite3StrNICmp(zLeft: zSql,zRight: "CREATE ",N: 7)!=0 ){
115330 return SQLITE_CORRUPT_BKPT;
115331 }
115332 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
115333 p->eParseMode = PARSE_MODE_RENAME;
115334 p->db = db;
115335 p->nQueryLoop = 1;
115336 rc = sqlite3RunParser(p, zSql);
115337 if( db->mallocFailed ) rc = SQLITE_NOMEM;
115338 if( rc==SQLITE_OK
115339 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
115340 ){
115341 rc = SQLITE_CORRUPT_BKPT;
115342 }
115343
115344#ifdef SQLITE_DEBUG
115345 /* Ensure that all mappings in the Parse.pRename list really do map to
115346 ** a part of the input string. */
115347 if( rc==SQLITE_OK ){
115348 int nSql = sqlite3Strlen30(zSql);
115349 RenameToken *pToken;
115350 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
115351 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
115352 }
115353 }
115354#endif
115355
115356 db->init.iDb = 0;
115357 return rc;
115358}
115359
115360/*
115361** This function edits SQL statement zSql, replacing each token identified
115362** by the linked list pRename with the text of zNew. If argument bQuote is
115363** true, then zNew is always quoted first. If no error occurs, the result
115364** is loaded into context object pCtx as the result.
115365**
115366** Or, if an error occurs (i.e. an OOM condition), an error is left in
115367** pCtx and an SQLite error code returned.
115368*/
115369static int renameEditSql(
115370 sqlite3_context *pCtx, /* Return result here */
115371 RenameCtx *pRename, /* Rename context */
115372 const char *zSql, /* SQL statement to edit */
115373 const char *zNew, /* New token text */
115374 int bQuote /* True to always quote token */
115375){
115376 i64 nNew = sqlite3Strlen30(z: zNew);
115377 i64 nSql = sqlite3Strlen30(z: zSql);
115378 sqlite3 *db = sqlite3_context_db_handle(p: pCtx);
115379 int rc = SQLITE_OK;
115380 char *zQuot = 0;
115381 char *zOut;
115382 i64 nQuot = 0;
115383 char *zBuf1 = 0;
115384 char *zBuf2 = 0;
115385
115386 if( zNew ){
115387 /* Set zQuot to point to a buffer containing a quoted copy of the
115388 ** identifier zNew. If the corresponding identifier in the original
115389 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
115390 ** point to zQuot so that all substitutions are made using the
115391 ** quoted version of the new column name. */
115392 zQuot = sqlite3MPrintf(db, zFormat: "\"%w\" ", zNew);
115393 if( zQuot==0 ){
115394 return SQLITE_NOMEM;
115395 }else{
115396 nQuot = sqlite3Strlen30(z: zQuot)-1;
115397 }
115398
115399 assert( nQuot>=nNew );
115400 zOut = sqlite3DbMallocZero(db, n: nSql + pRename->nList*nQuot + 1);
115401 }else{
115402 zOut = (char*)sqlite3DbMallocZero(db, n: (nSql*2+1) * 3);
115403 if( zOut ){
115404 zBuf1 = &zOut[nSql*2+1];
115405 zBuf2 = &zOut[nSql*4+2];
115406 }
115407 }
115408
115409 /* At this point pRename->pList contains a list of RenameToken objects
115410 ** corresponding to all tokens in the input SQL that must be replaced
115411 ** with the new column name, or with single-quoted versions of themselves.
115412 ** All that remains is to construct and return the edited SQL string. */
115413 if( zOut ){
115414 int nOut = nSql;
115415 memcpy(dest: zOut, src: zSql, n: nSql);
115416 while( pRename->pList ){
115417 int iOff; /* Offset of token to replace in zOut */
115418 u32 nReplace;
115419 const char *zReplace;
115420 RenameToken *pBest = renameColumnTokenNext(pCtx: pRename);
115421
115422 if( zNew ){
115423 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
115424 nReplace = nNew;
115425 zReplace = zNew;
115426 }else{
115427 nReplace = nQuot;
115428 zReplace = zQuot;
115429 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
115430 }
115431 }else{
115432 /* Dequote the double-quoted token. Then requote it again, this time
115433 ** using single quotes. If the character immediately following the
115434 ** original token within the input SQL was a single quote ('), then
115435 ** add another space after the new, single-quoted version of the
115436 ** token. This is so that (SELECT "string"'alias') maps to
115437 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
115438 memcpy(dest: zBuf1, src: pBest->t.z, n: pBest->t.n);
115439 zBuf1[pBest->t.n] = 0;
115440 sqlite3Dequote(z: zBuf1);
115441 sqlite3_snprintf(n: nSql*2, zBuf: zBuf2, zFormat: "%Q%s", zBuf1,
115442 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
115443 );
115444 zReplace = zBuf2;
115445 nReplace = sqlite3Strlen30(z: zReplace);
115446 }
115447
115448 iOff = pBest->t.z - zSql;
115449 if( pBest->t.n!=nReplace ){
115450 memmove(dest: &zOut[iOff + nReplace], src: &zOut[iOff + pBest->t.n],
115451 n: nOut - (iOff + pBest->t.n)
115452 );
115453 nOut += nReplace - pBest->t.n;
115454 zOut[nOut] = '\0';
115455 }
115456 memcpy(dest: &zOut[iOff], src: zReplace, n: nReplace);
115457 sqlite3DbFree(db, p: pBest);
115458 }
115459
115460 sqlite3_result_text(pCtx, z: zOut, n: -1, SQLITE_TRANSIENT);
115461 sqlite3DbFree(db, p: zOut);
115462 }else{
115463 rc = SQLITE_NOMEM;
115464 }
115465
115466 sqlite3_free(p: zQuot);
115467 return rc;
115468}
115469
115470/*
115471** Set all pEList->a[].fg.eEName fields in the expression-list to val.
115472*/
115473static void renameSetENames(ExprList *pEList, int val){
115474 if( pEList ){
115475 int i;
115476 for(i=0; i<pEList->nExpr; i++){
115477 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
115478 pEList->a[i].fg.eEName = val;
115479 }
115480 }
115481}
115482
115483/*
115484** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
115485** it was read from the schema of database zDb. Return SQLITE_OK if
115486** successful. Otherwise, return an SQLite error code and leave an error
115487** message in the Parse object.
115488*/
115489static int renameResolveTrigger(Parse *pParse){
115490 sqlite3 *db = pParse->db;
115491 Trigger *pNew = pParse->pNewTrigger;
115492 TriggerStep *pStep;
115493 NameContext sNC;
115494 int rc = SQLITE_OK;
115495
115496 memset(s: &sNC, c: 0, n: sizeof(sNC));
115497 sNC.pParse = pParse;
115498 assert( pNew->pTabSchema );
115499 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
115500 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
115501 );
115502 pParse->eTriggerOp = pNew->op;
115503 /* ALWAYS() because if the table of the trigger does not exist, the
115504 ** error would have been hit before this point */
115505 if( ALWAYS(pParse->pTriggerTab) ){
115506 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
115507 }
115508
115509 /* Resolve symbols in WHEN clause */
115510 if( rc==SQLITE_OK && pNew->pWhen ){
115511 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pNew->pWhen);
115512 }
115513
115514 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
115515 if( pStep->pSelect ){
115516 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
115517 if( pParse->nErr ) rc = pParse->rc;
115518 }
115519 if( rc==SQLITE_OK && pStep->zTarget ){
115520 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
115521 if( pSrc ){
115522 Select *pSel = sqlite3SelectNew(
115523 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
115524 );
115525 if( pSel==0 ){
115526 pStep->pExprList = 0;
115527 pSrc = 0;
115528 rc = SQLITE_NOMEM;
115529 }else{
115530 /* pStep->pExprList contains an expression-list used for an UPDATE
115531 ** statement. So the a[].zEName values are the RHS of the
115532 ** "<col> = <expr>" clauses of the UPDATE statement. So, before
115533 ** running SelectPrep(), change all the eEName values in
115534 ** pStep->pExprList to ENAME_SPAN (from their current value of
115535 ** ENAME_NAME). This is to prevent any ids in ON() clauses that are
115536 ** part of pSrc from being incorrectly resolved against the
115537 ** a[].zEName values as if they were column aliases. */
115538 renameSetENames(pEList: pStep->pExprList, ENAME_SPAN);
115539 sqlite3SelectPrep(pParse, pSel, 0);
115540 renameSetENames(pEList: pStep->pExprList, ENAME_NAME);
115541 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
115542 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
115543 assert( pSrc==pSel->pSrc );
115544 if( pStep->pExprList ) pSel->pEList = 0;
115545 pSel->pSrc = 0;
115546 sqlite3SelectDelete(db, pSel);
115547 }
115548 if( pStep->pFrom ){
115549 int i;
115550 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
115551 SrcItem *p = &pStep->pFrom->a[i];
115552 if( p->pSelect ){
115553 sqlite3SelectPrep(pParse, p->pSelect, 0);
115554 }
115555 }
115556 }
115557
115558 if( db->mallocFailed ){
115559 rc = SQLITE_NOMEM;
115560 }
115561 sNC.pSrcList = pSrc;
115562 if( rc==SQLITE_OK && pStep->pWhere ){
115563 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pStep->pWhere);
115564 }
115565 if( rc==SQLITE_OK ){
115566 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pStep->pExprList);
115567 }
115568 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
115569 if( pStep->pUpsert && rc==SQLITE_OK ){
115570 Upsert *pUpsert = pStep->pUpsert;
115571 pUpsert->pUpsertSrc = pSrc;
115572 sNC.uNC.pUpsert = pUpsert;
115573 sNC.ncFlags = NC_UUpsert;
115574 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pUpsert->pUpsertTarget);
115575 if( rc==SQLITE_OK ){
115576 ExprList *pUpsertSet = pUpsert->pUpsertSet;
115577 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pUpsertSet);
115578 }
115579 if( rc==SQLITE_OK ){
115580 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pUpsert->pUpsertWhere);
115581 }
115582 if( rc==SQLITE_OK ){
115583 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pUpsert->pUpsertTargetWhere);
115584 }
115585 sNC.ncFlags = 0;
115586 }
115587 sNC.pSrcList = 0;
115588 sqlite3SrcListDelete(db, pSrc);
115589 }else{
115590 rc = SQLITE_NOMEM;
115591 }
115592 }
115593 }
115594 return rc;
115595}
115596
115597/*
115598** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
115599** objects that are part of the trigger passed as the second argument.
115600*/
115601static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
115602 TriggerStep *pStep;
115603
115604 /* Find tokens to edit in WHEN clause */
115605 sqlite3WalkExpr(pWalker, pExpr: pTrigger->pWhen);
115606
115607 /* Find tokens to edit in trigger steps */
115608 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
115609 sqlite3WalkSelect(pWalker, p: pStep->pSelect);
115610 sqlite3WalkExpr(pWalker, pExpr: pStep->pWhere);
115611 sqlite3WalkExprList(pWalker, p: pStep->pExprList);
115612 if( pStep->pUpsert ){
115613 Upsert *pUpsert = pStep->pUpsert;
115614 sqlite3WalkExprList(pWalker, p: pUpsert->pUpsertTarget);
115615 sqlite3WalkExprList(pWalker, p: pUpsert->pUpsertSet);
115616 sqlite3WalkExpr(pWalker, pExpr: pUpsert->pUpsertWhere);
115617 sqlite3WalkExpr(pWalker, pExpr: pUpsert->pUpsertTargetWhere);
115618 }
115619 if( pStep->pFrom ){
115620 int i;
115621 for(i=0; i<pStep->pFrom->nSrc; i++){
115622 sqlite3WalkSelect(pWalker, p: pStep->pFrom->a[i].pSelect);
115623 }
115624 }
115625 }
115626}
115627
115628/*
115629** Free the contents of Parse object (*pParse). Do not free the memory
115630** occupied by the Parse object itself.
115631*/
115632static void renameParseCleanup(Parse *pParse){
115633 sqlite3 *db = pParse->db;
115634 Index *pIdx;
115635 if( pParse->pVdbe ){
115636 sqlite3VdbeFinalize(p: pParse->pVdbe);
115637 }
115638 sqlite3DeleteTable(db, pParse->pNewTable);
115639 while( (pIdx = pParse->pNewIndex)!=0 ){
115640 pParse->pNewIndex = pIdx->pNext;
115641 sqlite3FreeIndex(db, pIdx);
115642 }
115643 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
115644 sqlite3DbFree(db, p: pParse->zErrMsg);
115645 renameTokenFree(db, pToken: pParse->pRename);
115646 sqlite3ParseObjectReset(pParse);
115647}
115648
115649/*
115650** SQL function:
115651**
115652** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP)
115653**
115654** 0. zSql: SQL statement to rewrite
115655** 1. type: Type of object ("table", "view" etc.)
115656** 2. object: Name of object
115657** 3. Database: Database name (e.g. "main")
115658** 4. Table: Table name
115659** 5. iCol: Index of column to rename
115660** 6. zNew: New column name
115661** 7. bQuote: Non-zero if the new column name should be quoted.
115662** 8. bTemp: True if zSql comes from temp schema
115663**
115664** Do a column rename operation on the CREATE statement given in zSql.
115665** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
115666** into zNew. The name should be quoted if bQuote is true.
115667**
115668** This function is used internally by the ALTER TABLE RENAME COLUMN command.
115669** It is only accessible to SQL created using sqlite3NestedParse(). It is
115670** not reachable from ordinary SQL passed into sqlite3_prepare() unless the
115671** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled.
115672*/
115673static void renameColumnFunc(
115674 sqlite3_context *context,
115675 int NotUsed,
115676 sqlite3_value **argv
115677){
115678 sqlite3 *db = sqlite3_context_db_handle(p: context);
115679 RenameCtx sCtx;
115680 const char *zSql = (const char*)sqlite3_value_text(pVal: argv[0]);
115681 const char *zDb = (const char*)sqlite3_value_text(pVal: argv[3]);
115682 const char *zTable = (const char*)sqlite3_value_text(pVal: argv[4]);
115683 int iCol = sqlite3_value_int(pVal: argv[5]);
115684 const char *zNew = (const char*)sqlite3_value_text(pVal: argv[6]);
115685 int bQuote = sqlite3_value_int(pVal: argv[7]);
115686 int bTemp = sqlite3_value_int(pVal: argv[8]);
115687 const char *zOld;
115688 int rc;
115689 Parse sParse;
115690 Walker sWalker;
115691 Index *pIdx;
115692 int i;
115693 Table *pTab;
115694#ifndef SQLITE_OMIT_AUTHORIZATION
115695 sqlite3_xauth xAuth = db->xAuth;
115696#endif
115697
115698 UNUSED_PARAMETER(NotUsed);
115699 if( zSql==0 ) return;
115700 if( zTable==0 ) return;
115701 if( zNew==0 ) return;
115702 if( iCol<0 ) return;
115703 sqlite3BtreeEnterAll(db);
115704 pTab = sqlite3FindTable(db, zTable, zDb);
115705 if( pTab==0 || iCol>=pTab->nCol ){
115706 sqlite3BtreeLeaveAll(db);
115707 return;
115708 }
115709 zOld = pTab->aCol[iCol].zCnName;
115710 memset(s: &sCtx, c: 0, n: sizeof(sCtx));
115711 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
115712
115713#ifndef SQLITE_OMIT_AUTHORIZATION
115714 db->xAuth = 0;
115715#endif
115716 rc = renameParseSql(p: &sParse, zDb, db, zSql, bTemp);
115717
115718 /* Find tokens that need to be replaced. */
115719 memset(s: &sWalker, c: 0, n: sizeof(Walker));
115720 sWalker.pParse = &sParse;
115721 sWalker.xExprCallback = renameColumnExprCb;
115722 sWalker.xSelectCallback = renameColumnSelectCb;
115723 sWalker.u.pRename = &sCtx;
115724
115725 sCtx.pTab = pTab;
115726 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
115727 if( sParse.pNewTable ){
115728 if( IsView(sParse.pNewTable) ){
115729 Select *pSelect = sParse.pNewTable->u.view.pSelect;
115730 pSelect->selFlags &= ~SF_View;
115731 sParse.rc = SQLITE_OK;
115732 sqlite3SelectPrep(&sParse, pSelect, 0);
115733 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
115734 if( rc==SQLITE_OK ){
115735 sqlite3WalkSelect(pWalker: &sWalker, p: pSelect);
115736 }
115737 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
115738 }else if( IsOrdinaryTable(sParse.pNewTable) ){
115739 /* A regular table */
115740 int bFKOnly = sqlite3_stricmp(zLeft: zTable, zRight: sParse.pNewTable->zName);
115741 FKey *pFKey;
115742 sCtx.pTab = sParse.pNewTable;
115743 if( bFKOnly==0 ){
115744 if( iCol<sParse.pNewTable->nCol ){
115745 renameTokenFind(
115746 pParse: &sParse, pCtx: &sCtx, pPtr: (void*)sParse.pNewTable->aCol[iCol].zCnName
115747 );
115748 }
115749 if( sCtx.iCol<0 ){
115750 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)&sParse.pNewTable->iPKey);
115751 }
115752 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewTable->pCheck);
115753 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
115754 sqlite3WalkExprList(pWalker: &sWalker, p: pIdx->aColExpr);
115755 }
115756 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
115757 sqlite3WalkExprList(pWalker: &sWalker, p: pIdx->aColExpr);
115758 }
115759#ifndef SQLITE_OMIT_GENERATED_COLUMNS
115760 for(i=0; i<sParse.pNewTable->nCol; i++){
115761 Expr *pExpr = sqlite3ColumnExpr(sParse.pNewTable,
115762 &sParse.pNewTable->aCol[i]);
115763 sqlite3WalkExpr(pWalker: &sWalker, pExpr);
115764 }
115765#endif
115766 }
115767
115768 assert( IsOrdinaryTable(sParse.pNewTable) );
115769 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
115770 for(i=0; i<pFKey->nCol; i++){
115771 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
115772 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)&pFKey->aCol[i]);
115773 }
115774 if( 0==sqlite3_stricmp(zLeft: pFKey->zTo, zRight: zTable)
115775 && 0==sqlite3_stricmp(zLeft: pFKey->aCol[i].zCol, zRight: zOld)
115776 ){
115777 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)pFKey->aCol[i].zCol);
115778 }
115779 }
115780 }
115781 }
115782 }else if( sParse.pNewIndex ){
115783 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewIndex->aColExpr);
115784 sqlite3WalkExpr(pWalker: &sWalker, pExpr: sParse.pNewIndex->pPartIdxWhere);
115785 }else{
115786 /* A trigger */
115787 TriggerStep *pStep;
115788 rc = renameResolveTrigger(pParse: &sParse);
115789 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
115790
115791 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
115792 if( pStep->zTarget ){
115793 Table *pTarget = sqlite3LocateTable(&sParse, flags: 0, pStep->zTarget, zDb);
115794 if( pTarget==pTab ){
115795 if( pStep->pUpsert ){
115796 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
115797 renameColumnElistNames(pParse: &sParse, pCtx: &sCtx, pEList: pUpsertSet, zOld);
115798 }
115799 renameColumnIdlistNames(pParse: &sParse, pCtx: &sCtx, pIdList: pStep->pIdList, zOld);
115800 renameColumnElistNames(pParse: &sParse, pCtx: &sCtx, pEList: pStep->pExprList, zOld);
115801 }
115802 }
115803 }
115804
115805
115806 /* Find tokens to edit in UPDATE OF clause */
115807 if( sParse.pTriggerTab==pTab ){
115808 renameColumnIdlistNames(pParse: &sParse, pCtx: &sCtx,pIdList: sParse.pNewTrigger->pColumns,zOld);
115809 }
115810
115811 /* Find tokens to edit in various expressions and selects */
115812 renameWalkTrigger(pWalker: &sWalker, pTrigger: sParse.pNewTrigger);
115813 }
115814
115815 assert( rc==SQLITE_OK );
115816 rc = renameEditSql(pCtx: context, pRename: &sCtx, zSql, zNew, bQuote);
115817
115818renameColumnFunc_done:
115819 if( rc!=SQLITE_OK ){
115820 if( rc==SQLITE_ERROR && sqlite3WritableSchema(db) ){
115821 sqlite3_result_value(pCtx: context, pValue: argv[0]);
115822 }else if( sParse.zErrMsg ){
115823 renameColumnParseError(pCtx: context, zWhen: "", pType: argv[1], pObject: argv[2], pParse: &sParse);
115824 }else{
115825 sqlite3_result_error_code(pCtx: context, errCode: rc);
115826 }
115827 }
115828
115829 renameParseCleanup(pParse: &sParse);
115830 renameTokenFree(db, pToken: sCtx.pList);
115831#ifndef SQLITE_OMIT_AUTHORIZATION
115832 db->xAuth = xAuth;
115833#endif
115834 sqlite3BtreeLeaveAll(db);
115835}
115836
115837/*
115838** Walker expression callback used by "RENAME TABLE".
115839*/
115840static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
115841 RenameCtx *p = pWalker->u.pRename;
115842 if( pExpr->op==TK_COLUMN
115843 && ALWAYS(ExprUseYTab(pExpr))
115844 && p->pTab==pExpr->y.pTab
115845 ){
115846 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: (void*)&pExpr->y.pTab);
115847 }
115848 return WRC_Continue;
115849}
115850
115851/*
115852** Walker select callback used by "RENAME TABLE".
115853*/
115854static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
115855 int i;
115856 RenameCtx *p = pWalker->u.pRename;
115857 SrcList *pSrc = pSelect->pSrc;
115858 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
115859 testcase( pSelect->selFlags & SF_View );
115860 testcase( pSelect->selFlags & SF_CopyCte );
115861 return WRC_Prune;
115862 }
115863 if( NEVER(pSrc==0) ){
115864 assert( pWalker->pParse->db->mallocFailed );
115865 return WRC_Abort;
115866 }
115867 for(i=0; i<pSrc->nSrc; i++){
115868 SrcItem *pItem = &pSrc->a[i];
115869 if( pItem->pTab==p->pTab ){
115870 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: pItem->zName);
115871 }
115872 }
115873 renameWalkWith(pWalker, pSelect);
115874
115875 return WRC_Continue;
115876}
115877
115878
115879/*
115880** This C function implements an SQL user function that is used by SQL code
115881** generated by the ALTER TABLE ... RENAME command to modify the definition
115882** of any foreign key constraints that use the table being renamed as the
115883** parent table. It is passed three arguments:
115884**
115885** 0: The database containing the table being renamed.
115886** 1. type: Type of object ("table", "view" etc.)
115887** 2. object: Name of object
115888** 3: The complete text of the schema statement being modified,
115889** 4: The old name of the table being renamed, and
115890** 5: The new name of the table being renamed.
115891** 6: True if the schema statement comes from the temp db.
115892**
115893** It returns the new schema statement. For example:
115894**
115895** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
115896** -> 'CREATE TABLE t1(a REFERENCES t3)'
115897*/
115898static void renameTableFunc(
115899 sqlite3_context *context,
115900 int NotUsed,
115901 sqlite3_value **argv
115902){
115903 sqlite3 *db = sqlite3_context_db_handle(p: context);
115904 const char *zDb = (const char*)sqlite3_value_text(pVal: argv[0]);
115905 const char *zInput = (const char*)sqlite3_value_text(pVal: argv[3]);
115906 const char *zOld = (const char*)sqlite3_value_text(pVal: argv[4]);
115907 const char *zNew = (const char*)sqlite3_value_text(pVal: argv[5]);
115908 int bTemp = sqlite3_value_int(pVal: argv[6]);
115909 UNUSED_PARAMETER(NotUsed);
115910
115911 if( zInput && zOld && zNew ){
115912 Parse sParse;
115913 int rc;
115914 int bQuote = 1;
115915 RenameCtx sCtx;
115916 Walker sWalker;
115917
115918#ifndef SQLITE_OMIT_AUTHORIZATION
115919 sqlite3_xauth xAuth = db->xAuth;
115920 db->xAuth = 0;
115921#endif
115922
115923 sqlite3BtreeEnterAll(db);
115924
115925 memset(s: &sCtx, c: 0, n: sizeof(RenameCtx));
115926 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
115927 memset(s: &sWalker, c: 0, n: sizeof(Walker));
115928 sWalker.pParse = &sParse;
115929 sWalker.xExprCallback = renameTableExprCb;
115930 sWalker.xSelectCallback = renameTableSelectCb;
115931 sWalker.u.pRename = &sCtx;
115932
115933 rc = renameParseSql(p: &sParse, zDb, db, zSql: zInput, bTemp);
115934
115935 if( rc==SQLITE_OK ){
115936 int isLegacy = (db->flags & SQLITE_LegacyAlter);
115937 if( sParse.pNewTable ){
115938 Table *pTab = sParse.pNewTable;
115939
115940 if( IsView(pTab) ){
115941 if( isLegacy==0 ){
115942 Select *pSelect = pTab->u.view.pSelect;
115943 NameContext sNC;
115944 memset(s: &sNC, c: 0, n: sizeof(sNC));
115945 sNC.pParse = &sParse;
115946
115947 assert( pSelect->selFlags & SF_View );
115948 pSelect->selFlags &= ~SF_View;
115949 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
115950 if( sParse.nErr ){
115951 rc = sParse.rc;
115952 }else{
115953 sqlite3WalkSelect(pWalker: &sWalker, p: pTab->u.view.pSelect);
115954 }
115955 }
115956 }else{
115957 /* Modify any FK definitions to point to the new table. */
115958#ifndef SQLITE_OMIT_FOREIGN_KEY
115959 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
115960 && !IsVirtual(pTab)
115961 ){
115962 FKey *pFKey;
115963 assert( IsOrdinaryTable(pTab) );
115964 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
115965 if( sqlite3_stricmp(zLeft: pFKey->zTo, zRight: zOld)==0 ){
115966 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)pFKey->zTo);
115967 }
115968 }
115969 }
115970#endif
115971
115972 /* If this is the table being altered, fix any table refs in CHECK
115973 ** expressions. Also update the name that appears right after the
115974 ** "CREATE [VIRTUAL] TABLE" bit. */
115975 if( sqlite3_stricmp(zLeft: zOld, zRight: pTab->zName)==0 ){
115976 sCtx.pTab = pTab;
115977 if( isLegacy==0 ){
115978 sqlite3WalkExprList(pWalker: &sWalker, p: pTab->pCheck);
115979 }
115980 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: pTab->zName);
115981 }
115982 }
115983 }
115984
115985 else if( sParse.pNewIndex ){
115986 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: sParse.pNewIndex->zName);
115987 if( isLegacy==0 ){
115988 sqlite3WalkExpr(pWalker: &sWalker, pExpr: sParse.pNewIndex->pPartIdxWhere);
115989 }
115990 }
115991
115992#ifndef SQLITE_OMIT_TRIGGER
115993 else{
115994 Trigger *pTrigger = sParse.pNewTrigger;
115995 TriggerStep *pStep;
115996 if( 0==sqlite3_stricmp(zLeft: sParse.pNewTrigger->table, zRight: zOld)
115997 && sCtx.pTab->pSchema==pTrigger->pTabSchema
115998 ){
115999 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: sParse.pNewTrigger->table);
116000 }
116001
116002 if( isLegacy==0 ){
116003 rc = renameResolveTrigger(pParse: &sParse);
116004 if( rc==SQLITE_OK ){
116005 renameWalkTrigger(pWalker: &sWalker, pTrigger);
116006 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
116007 if( pStep->zTarget && 0==sqlite3_stricmp(zLeft: pStep->zTarget, zRight: zOld) ){
116008 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: pStep->zTarget);
116009 }
116010 if( pStep->pFrom ){
116011 int i;
116012 for(i=0; i<pStep->pFrom->nSrc; i++){
116013 SrcItem *pItem = &pStep->pFrom->a[i];
116014 if( 0==sqlite3_stricmp(zLeft: pItem->zName, zRight: zOld) ){
116015 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: pItem->zName);
116016 }
116017 }
116018 }
116019 }
116020 }
116021 }
116022 }
116023#endif
116024 }
116025
116026 if( rc==SQLITE_OK ){
116027 rc = renameEditSql(pCtx: context, pRename: &sCtx, zSql: zInput, zNew, bQuote);
116028 }
116029 if( rc!=SQLITE_OK ){
116030 if( rc==SQLITE_ERROR && sqlite3WritableSchema(db) ){
116031 sqlite3_result_value(pCtx: context, pValue: argv[3]);
116032 }else if( sParse.zErrMsg ){
116033 renameColumnParseError(pCtx: context, zWhen: "", pType: argv[1], pObject: argv[2], pParse: &sParse);
116034 }else{
116035 sqlite3_result_error_code(pCtx: context, errCode: rc);
116036 }
116037 }
116038
116039 renameParseCleanup(pParse: &sParse);
116040 renameTokenFree(db, pToken: sCtx.pList);
116041 sqlite3BtreeLeaveAll(db);
116042#ifndef SQLITE_OMIT_AUTHORIZATION
116043 db->xAuth = xAuth;
116044#endif
116045 }
116046
116047 return;
116048}
116049
116050static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){
116051 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
116052 renameTokenFind(pParse: pWalker->pParse, pCtx: pWalker->u.pRename, pPtr: (const void*)pExpr);
116053 }
116054 return WRC_Continue;
116055}
116056
116057/* SQL function: sqlite_rename_quotefix(DB,SQL)
116058**
116059** Rewrite the DDL statement "SQL" so that any string literals that use
116060** double-quotes use single quotes instead.
116061**
116062** Two arguments must be passed:
116063**
116064** 0: Database name ("main", "temp" etc.).
116065** 1: SQL statement to edit.
116066**
116067** The returned value is the modified SQL statement. For example, given
116068** the database schema:
116069**
116070** CREATE TABLE t1(a, b, c);
116071**
116072** SELECT sqlite_rename_quotefix('main',
116073** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1'
116074** );
116075**
116076** returns the string:
116077**
116078** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1
116079**
116080** If there is a error in the input SQL, then raise an error, except
116081** if PRAGMA writable_schema=ON, then just return the input string
116082** unmodified following an error.
116083*/
116084static void renameQuotefixFunc(
116085 sqlite3_context *context,
116086 int NotUsed,
116087 sqlite3_value **argv
116088){
116089 sqlite3 *db = sqlite3_context_db_handle(p: context);
116090 char const *zDb = (const char*)sqlite3_value_text(pVal: argv[0]);
116091 char const *zInput = (const char*)sqlite3_value_text(pVal: argv[1]);
116092
116093#ifndef SQLITE_OMIT_AUTHORIZATION
116094 sqlite3_xauth xAuth = db->xAuth;
116095 db->xAuth = 0;
116096#endif
116097
116098 sqlite3BtreeEnterAll(db);
116099
116100 UNUSED_PARAMETER(NotUsed);
116101 if( zDb && zInput ){
116102 int rc;
116103 Parse sParse;
116104 rc = renameParseSql(p: &sParse, zDb, db, zSql: zInput, bTemp: 0);
116105
116106 if( rc==SQLITE_OK ){
116107 RenameCtx sCtx;
116108 Walker sWalker;
116109
116110 /* Walker to find tokens that need to be replaced. */
116111 memset(s: &sCtx, c: 0, n: sizeof(RenameCtx));
116112 memset(s: &sWalker, c: 0, n: sizeof(Walker));
116113 sWalker.pParse = &sParse;
116114 sWalker.xExprCallback = renameQuotefixExprCb;
116115 sWalker.xSelectCallback = renameColumnSelectCb;
116116 sWalker.u.pRename = &sCtx;
116117
116118 if( sParse.pNewTable ){
116119 if( IsView(sParse.pNewTable) ){
116120 Select *pSelect = sParse.pNewTable->u.view.pSelect;
116121 pSelect->selFlags &= ~SF_View;
116122 sParse.rc = SQLITE_OK;
116123 sqlite3SelectPrep(&sParse, pSelect, 0);
116124 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
116125 if( rc==SQLITE_OK ){
116126 sqlite3WalkSelect(pWalker: &sWalker, p: pSelect);
116127 }
116128 }else{
116129 int i;
116130 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewTable->pCheck);
116131#ifndef SQLITE_OMIT_GENERATED_COLUMNS
116132 for(i=0; i<sParse.pNewTable->nCol; i++){
116133 sqlite3WalkExpr(pWalker: &sWalker,
116134 pExpr: sqlite3ColumnExpr(sParse.pNewTable,
116135 &sParse.pNewTable->aCol[i]));
116136 }
116137#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
116138 }
116139 }else if( sParse.pNewIndex ){
116140 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewIndex->aColExpr);
116141 sqlite3WalkExpr(pWalker: &sWalker, pExpr: sParse.pNewIndex->pPartIdxWhere);
116142 }else{
116143#ifndef SQLITE_OMIT_TRIGGER
116144 rc = renameResolveTrigger(pParse: &sParse);
116145 if( rc==SQLITE_OK ){
116146 renameWalkTrigger(pWalker: &sWalker, pTrigger: sParse.pNewTrigger);
116147 }
116148#endif /* SQLITE_OMIT_TRIGGER */
116149 }
116150
116151 if( rc==SQLITE_OK ){
116152 rc = renameEditSql(pCtx: context, pRename: &sCtx, zSql: zInput, zNew: 0, bQuote: 0);
116153 }
116154 renameTokenFree(db, pToken: sCtx.pList);
116155 }
116156 if( rc!=SQLITE_OK ){
116157 if( sqlite3WritableSchema(db) && rc==SQLITE_ERROR ){
116158 sqlite3_result_value(pCtx: context, pValue: argv[1]);
116159 }else{
116160 sqlite3_result_error_code(pCtx: context, errCode: rc);
116161 }
116162 }
116163 renameParseCleanup(pParse: &sParse);
116164 }
116165
116166#ifndef SQLITE_OMIT_AUTHORIZATION
116167 db->xAuth = xAuth;
116168#endif
116169
116170 sqlite3BtreeLeaveAll(db);
116171}
116172
116173/* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS)
116174**
116175** An SQL user function that checks that there are no parse or symbol
116176** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
116177** After an ALTER TABLE .. RENAME operation is performed and the schema
116178** reloaded, this function is called on each SQL statement in the schema
116179** to ensure that it is still usable.
116180**
116181** 0: Database name ("main", "temp" etc.).
116182** 1: SQL statement.
116183** 2: Object type ("view", "table", "trigger" or "index").
116184** 3: Object name.
116185** 4: True if object is from temp schema.
116186** 5: "when" part of error message.
116187** 6: True to disable the DQS quirk when parsing SQL.
116188**
116189** The return value is computed as follows:
116190**
116191** A. If an error is seen and not in PRAGMA writable_schema=ON mode,
116192** then raise the error.
116193** B. Else if a trigger is created and the the table that the trigger is
116194** attached to is in database zDb, then return 1.
116195** C. Otherwise return NULL.
116196*/
116197static void renameTableTest(
116198 sqlite3_context *context,
116199 int NotUsed,
116200 sqlite3_value **argv
116201){
116202 sqlite3 *db = sqlite3_context_db_handle(p: context);
116203 char const *zDb = (const char*)sqlite3_value_text(pVal: argv[0]);
116204 char const *zInput = (const char*)sqlite3_value_text(pVal: argv[1]);
116205 int bTemp = sqlite3_value_int(pVal: argv[4]);
116206 int isLegacy = (db->flags & SQLITE_LegacyAlter);
116207 char const *zWhen = (const char*)sqlite3_value_text(pVal: argv[5]);
116208 int bNoDQS = sqlite3_value_int(pVal: argv[6]);
116209
116210#ifndef SQLITE_OMIT_AUTHORIZATION
116211 sqlite3_xauth xAuth = db->xAuth;
116212 db->xAuth = 0;
116213#endif
116214
116215 UNUSED_PARAMETER(NotUsed);
116216
116217 if( zDb && zInput ){
116218 int rc;
116219 Parse sParse;
116220 int flags = db->flags;
116221 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
116222 rc = renameParseSql(p: &sParse, zDb, db, zSql: zInput, bTemp);
116223 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
116224 if( rc==SQLITE_OK ){
116225 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable) ){
116226 NameContext sNC;
116227 memset(s: &sNC, c: 0, n: sizeof(sNC));
116228 sNC.pParse = &sParse;
116229 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
116230 if( sParse.nErr ) rc = sParse.rc;
116231 }
116232
116233 else if( sParse.pNewTrigger ){
116234 if( isLegacy==0 ){
116235 rc = renameResolveTrigger(pParse: &sParse);
116236 }
116237 if( rc==SQLITE_OK ){
116238 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
116239 int i2 = sqlite3FindDbName(db, zDb);
116240 if( i1==i2 ){
116241 /* Handle output case B */
116242 sqlite3_result_int(pCtx: context, iVal: 1);
116243 }
116244 }
116245 }
116246 }
116247
116248 if( rc!=SQLITE_OK && zWhen && !sqlite3WritableSchema(db) ){
116249 /* Output case A */
116250 renameColumnParseError(pCtx: context, zWhen, pType: argv[2], pObject: argv[3],pParse: &sParse);
116251 }
116252 renameParseCleanup(pParse: &sParse);
116253 }
116254
116255#ifndef SQLITE_OMIT_AUTHORIZATION
116256 db->xAuth = xAuth;
116257#endif
116258}
116259
116260/*
116261** The implementation of internal UDF sqlite_drop_column().
116262**
116263** Arguments:
116264**
116265** argv[0]: An integer - the index of the schema containing the table
116266** argv[1]: CREATE TABLE statement to modify.
116267** argv[2]: An integer - the index of the column to remove.
116268**
116269** The value returned is a string containing the CREATE TABLE statement
116270** with column argv[2] removed.
116271*/
116272static void dropColumnFunc(
116273 sqlite3_context *context,
116274 int NotUsed,
116275 sqlite3_value **argv
116276){
116277 sqlite3 *db = sqlite3_context_db_handle(p: context);
116278 int iSchema = sqlite3_value_int(pVal: argv[0]);
116279 const char *zSql = (const char*)sqlite3_value_text(pVal: argv[1]);
116280 int iCol = sqlite3_value_int(pVal: argv[2]);
116281 const char *zDb = db->aDb[iSchema].zDbSName;
116282 int rc;
116283 Parse sParse;
116284 RenameToken *pCol;
116285 Table *pTab;
116286 const char *zEnd;
116287 char *zNew = 0;
116288
116289#ifndef SQLITE_OMIT_AUTHORIZATION
116290 sqlite3_xauth xAuth = db->xAuth;
116291 db->xAuth = 0;
116292#endif
116293
116294 UNUSED_PARAMETER(NotUsed);
116295 rc = renameParseSql(p: &sParse, zDb, db, zSql, bTemp: iSchema==1);
116296 if( rc!=SQLITE_OK ) goto drop_column_done;
116297 pTab = sParse.pNewTable;
116298 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
116299 /* This can happen if the sqlite_schema table is corrupt */
116300 rc = SQLITE_CORRUPT_BKPT;
116301 goto drop_column_done;
116302 }
116303
116304 pCol = renameTokenFind(pParse: &sParse, pCtx: 0, pPtr: (void*)pTab->aCol[iCol].zCnName);
116305 if( iCol<pTab->nCol-1 ){
116306 RenameToken *pEnd;
116307 pEnd = renameTokenFind(pParse: &sParse, pCtx: 0, pPtr: (void*)pTab->aCol[iCol+1].zCnName);
116308 zEnd = (const char*)pEnd->t.z;
116309 }else{
116310 assert( IsOrdinaryTable(pTab) );
116311 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
116312 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
116313 }
116314
116315 zNew = sqlite3MPrintf(db, zFormat: "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
116316 sqlite3_result_text(pCtx: context, z: zNew, n: -1, SQLITE_TRANSIENT);
116317 sqlite3_free(p: zNew);
116318
116319drop_column_done:
116320 renameParseCleanup(pParse: &sParse);
116321#ifndef SQLITE_OMIT_AUTHORIZATION
116322 db->xAuth = xAuth;
116323#endif
116324 if( rc!=SQLITE_OK ){
116325 sqlite3_result_error_code(pCtx: context, errCode: rc);
116326 }
116327}
116328
116329/*
116330** This function is called by the parser upon parsing an
116331**
116332** ALTER TABLE pSrc DROP COLUMN pName
116333**
116334** statement. Argument pSrc contains the possibly qualified name of the
116335** table being edited, and token pName the name of the column to drop.
116336*/
116337SQLITE_PRIVATE void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const Token *pName){
116338 sqlite3 *db = pParse->db; /* Database handle */
116339 Table *pTab; /* Table to modify */
116340 int iDb; /* Index of db containing pTab in aDb[] */
116341 const char *zDb; /* Database containing pTab ("main" etc.) */
116342 char *zCol = 0; /* Name of column to drop */
116343 int iCol; /* Index of column zCol in pTab->aCol[] */
116344
116345 /* Look up the table being altered. */
116346 assert( pParse->pNewTable==0 );
116347 assert( sqlite3BtreeHoldsAllMutexes(db) );
116348 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
116349 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
116350 if( !pTab ) goto exit_drop_column;
116351
116352 /* Make sure this is not an attempt to ALTER a view, virtual table or
116353 ** system table. */
116354 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_drop_column;
116355 if( SQLITE_OK!=isRealTable(pParse, pTab, bDrop: 1) ) goto exit_drop_column;
116356
116357 /* Find the index of the column being dropped. */
116358 zCol = sqlite3NameFromToken(db, pName);
116359 if( zCol==0 ){
116360 assert( db->mallocFailed );
116361 goto exit_drop_column;
116362 }
116363 iCol = sqlite3ColumnIndex(pTab, zCol);
116364 if( iCol<0 ){
116365 sqlite3ErrorMsg(pParse, zFormat: "no such column: \"%T\"", pName);
116366 goto exit_drop_column;
116367 }
116368
116369 /* Do not allow the user to drop a PRIMARY KEY column or a column
116370 ** constrained by a UNIQUE constraint. */
116371 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
116372 sqlite3ErrorMsg(pParse, zFormat: "cannot drop %s column: \"%s\"",
116373 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
116374 zCol
116375 );
116376 goto exit_drop_column;
116377 }
116378
116379 /* Do not allow the number of columns to go to zero */
116380 if( pTab->nCol<=1 ){
116381 sqlite3ErrorMsg(pParse, zFormat: "cannot drop column \"%s\": no other columns exist",zCol);
116382 goto exit_drop_column;
116383 }
116384
116385 /* Edit the sqlite_schema table */
116386 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116387 assert( iDb>=0 );
116388 zDb = db->aDb[iDb].zDbSName;
116389#ifndef SQLITE_OMIT_AUTHORIZATION
116390 /* Invoke the authorization callback. */
116391 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
116392 goto exit_drop_column;
116393 }
116394#endif
116395 renameTestSchema(pParse, zDb, bTemp: iDb==1, zWhen: "", bNoDQS: 0);
116396 renameFixQuotes(pParse, zDb, bTemp: iDb==1);
116397 sqlite3NestedParse(pParse,
116398 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
116399 "sql = sqlite_drop_column(%d, sql, %d) "
116400 "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)"
116401 , zDb, iDb, iCol, pTab->zName
116402 );
116403
116404 /* Drop and reload the database schema. */
116405 renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop);
116406 renameTestSchema(pParse, zDb, bTemp: iDb==1, zWhen: "after drop column", bNoDQS: 1);
116407
116408 /* Edit rows of table on disk */
116409 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
116410 int i;
116411 int addr;
116412 int reg;
116413 int regRec;
116414 Index *pPk = 0;
116415 int nField = 0; /* Number of non-virtual columns after drop */
116416 int iCur;
116417 Vdbe *v = sqlite3GetVdbe(pParse);
116418 iCur = pParse->nTab++;
116419 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
116420 addr = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iCur); VdbeCoverage(v);
116421 reg = ++pParse->nMem;
116422 if( HasRowid(pTab) ){
116423 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCur, p2: reg);
116424 pParse->nMem += pTab->nCol;
116425 }else{
116426 pPk = sqlite3PrimaryKeyIndex(pTab);
116427 pParse->nMem += pPk->nColumn;
116428 for(i=0; i<pPk->nKeyCol; i++){
116429 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iCur, p2: i, p3: reg+i+1);
116430 }
116431 nField = pPk->nKeyCol;
116432 }
116433 regRec = ++pParse->nMem;
116434 for(i=0; i<pTab->nCol; i++){
116435 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
116436 int regOut;
116437 if( pPk ){
116438 int iPos = sqlite3TableColumnToIndex(pPk, i);
116439 int iColPos = sqlite3TableColumnToIndex(pPk, iCol);
116440 if( iPos<pPk->nKeyCol ) continue;
116441 regOut = reg+1+iPos-(iPos>iColPos);
116442 }else{
116443 regOut = reg+1+nField;
116444 }
116445 if( i==pTab->iPKey ){
116446 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regOut);
116447 }else{
116448 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol: i, regOut);
116449 }
116450 nField++;
116451 }
116452 }
116453 if( nField==0 ){
116454 /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */
116455 pParse->nMem++;
116456 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: reg+1);
116457 nField = 1;
116458 }
116459 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: reg+1, p2: nField, p3: regRec);
116460 if( pPk ){
116461 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iCur, p2: regRec, p3: reg+1, p4: pPk->nKeyCol);
116462 }else{
116463 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iCur, p2: regRec, p3: reg);
116464 }
116465 sqlite3VdbeChangeP5(p: v, OPFLAG_SAVEPOSITION);
116466
116467 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iCur, p2: addr+1); VdbeCoverage(v);
116468 sqlite3VdbeJumpHere(p: v, addr);
116469 }
116470
116471exit_drop_column:
116472 sqlite3DbFree(db, p: zCol);
116473 sqlite3SrcListDelete(db, pSrc);
116474}
116475
116476/*
116477** Register built-in functions used to help implement ALTER TABLE
116478*/
116479SQLITE_PRIVATE void sqlite3AlterFunctions(void){
116480 static FuncDef aAlterTableFuncs[] = {
116481 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
116482 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
116483 INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest),
116484 INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc),
116485 INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc),
116486 };
116487 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
116488}
116489#endif /* SQLITE_ALTER_TABLE */
116490
116491/************** End of alter.c ***********************************************/
116492/************** Begin file analyze.c *****************************************/
116493/*
116494** 2005-07-08
116495**
116496** The author disclaims copyright to this source code. In place of
116497** a legal notice, here is a blessing:
116498**
116499** May you do good and not evil.
116500** May you find forgiveness for yourself and forgive others.
116501** May you share freely, never taking more than you give.
116502**
116503*************************************************************************
116504** This file contains code associated with the ANALYZE command.
116505**
116506** The ANALYZE command gather statistics about the content of tables
116507** and indices. These statistics are made available to the query planner
116508** to help it make better decisions about how to perform queries.
116509**
116510** The following system tables are or have been supported:
116511**
116512** CREATE TABLE sqlite_stat1(tbl, idx, stat);
116513** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
116514** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
116515** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
116516**
116517** Additional tables might be added in future releases of SQLite.
116518** The sqlite_stat2 table is not created or used unless the SQLite version
116519** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
116520** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
116521** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
116522** created and used by SQLite versions 3.7.9 through 3.29.0 when
116523** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
116524** is a superset of sqlite_stat2 and is also now deprecated. The
116525** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
116526** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
116527** versions 3.8.1 and later. STAT4 is the only variant that is still
116528** supported.
116529**
116530** For most applications, sqlite_stat1 provides all the statistics required
116531** for the query planner to make good choices.
116532**
116533** Format of sqlite_stat1:
116534**
116535** There is normally one row per index, with the index identified by the
116536** name in the idx column. The tbl column is the name of the table to
116537** which the index belongs. In each such row, the stat column will be
116538** a string consisting of a list of integers. The first integer in this
116539** list is the number of rows in the index. (This is the same as the
116540** number of rows in the table, except for partial indices.) The second
116541** integer is the average number of rows in the index that have the same
116542** value in the first column of the index. The third integer is the average
116543** number of rows in the index that have the same value for the first two
116544** columns. The N-th integer (for N>1) is the average number of rows in
116545** the index which have the same value for the first N-1 columns. For
116546** a K-column index, there will be K+1 integers in the stat column. If
116547** the index is unique, then the last integer will be 1.
116548**
116549** The list of integers in the stat column can optionally be followed
116550** by the keyword "unordered". The "unordered" keyword, if it is present,
116551** must be separated from the last integer by a single space. If the
116552** "unordered" keyword is present, then the query planner assumes that
116553** the index is unordered and will not use the index for a range query.
116554**
116555** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
116556** column contains a single integer which is the (estimated) number of
116557** rows in the table identified by sqlite_stat1.tbl.
116558**
116559** Format of sqlite_stat2:
116560**
116561** The sqlite_stat2 is only created and is only used if SQLite is compiled
116562** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
116563** 3.6.18 and 3.7.8. The "stat2" table contains additional information
116564** about the distribution of keys within an index. The index is identified by
116565** the "idx" column and the "tbl" column is the name of the table to which
116566** the index belongs. There are usually 10 rows in the sqlite_stat2
116567** table for each index.
116568**
116569** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
116570** inclusive are samples of the left-most key value in the index taken at
116571** evenly spaced points along the index. Let the number of samples be S
116572** (10 in the standard build) and let C be the number of rows in the index.
116573** Then the sampled rows are given by:
116574**
116575** rownumber = (i*C*2 + C)/(S*2)
116576**
116577** For i between 0 and S-1. Conceptually, the index space is divided into
116578** S uniform buckets and the samples are the middle row from each bucket.
116579**
116580** The format for sqlite_stat2 is recorded here for legacy reference. This
116581** version of SQLite does not support sqlite_stat2. It neither reads nor
116582** writes the sqlite_stat2 table. This version of SQLite only supports
116583** sqlite_stat3.
116584**
116585** Format for sqlite_stat3:
116586**
116587** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
116588** sqlite_stat4 format will be described first. Further information
116589** about sqlite_stat3 follows the sqlite_stat4 description.
116590**
116591** Format for sqlite_stat4:
116592**
116593** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
116594** to aid the query planner in choosing good indices based on the values
116595** that indexed columns are compared against in the WHERE clauses of
116596** queries.
116597**
116598** The sqlite_stat4 table contains multiple entries for each index.
116599** The idx column names the index and the tbl column is the table of the
116600** index. If the idx and tbl columns are the same, then the sample is
116601** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
116602** binary encoding of a key from the index. The nEq column is a
116603** list of integers. The first integer is the approximate number
116604** of entries in the index whose left-most column exactly matches
116605** the left-most column of the sample. The second integer in nEq
116606** is the approximate number of entries in the index where the
116607** first two columns match the first two columns of the sample.
116608** And so forth. nLt is another list of integers that show the approximate
116609** number of entries that are strictly less than the sample. The first
116610** integer in nLt contains the number of entries in the index where the
116611** left-most column is less than the left-most column of the sample.
116612** The K-th integer in the nLt entry is the number of index entries
116613** where the first K columns are less than the first K columns of the
116614** sample. The nDLt column is like nLt except that it contains the
116615** number of distinct entries in the index that are less than the
116616** sample.
116617**
116618** There can be an arbitrary number of sqlite_stat4 entries per index.
116619** The ANALYZE command will typically generate sqlite_stat4 tables
116620** that contain between 10 and 40 samples which are distributed across
116621** the key space, though not uniformly, and which include samples with
116622** large nEq values.
116623**
116624** Format for sqlite_stat3 redux:
116625**
116626** The sqlite_stat3 table is like sqlite_stat4 except that it only
116627** looks at the left-most column of the index. The sqlite_stat3.sample
116628** column contains the actual value of the left-most column instead
116629** of a blob encoding of the complete index key as is found in
116630** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
116631** all contain just a single integer which is the same as the first
116632** integer in the equivalent columns in sqlite_stat4.
116633*/
116634#ifndef SQLITE_OMIT_ANALYZE
116635/* #include "sqliteInt.h" */
116636
116637#if defined(SQLITE_ENABLE_STAT4)
116638# define IsStat4 1
116639#else
116640# define IsStat4 0
116641# undef SQLITE_STAT4_SAMPLES
116642# define SQLITE_STAT4_SAMPLES 1
116643#endif
116644
116645/*
116646** This routine generates code that opens the sqlite_statN tables.
116647** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
116648** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
116649** appropriate compile-time options are provided.
116650**
116651** If the sqlite_statN tables do not previously exist, it is created.
116652**
116653** Argument zWhere may be a pointer to a buffer containing a table name,
116654** or it may be a NULL pointer. If it is not NULL, then all entries in
116655** the sqlite_statN tables associated with the named table are deleted.
116656** If zWhere==0, then code is generated to delete all stat table entries.
116657*/
116658static void openStatTable(
116659 Parse *pParse, /* Parsing context */
116660 int iDb, /* The database we are looking in */
116661 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
116662 const char *zWhere, /* Delete entries for this table or index */
116663 const char *zWhereType /* Either "tbl" or "idx" */
116664){
116665 static const struct {
116666 const char *zName;
116667 const char *zCols;
116668 } aTable[] = {
116669 { "sqlite_stat1", "tbl,idx,stat" },
116670#if defined(SQLITE_ENABLE_STAT4)
116671 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
116672#else
116673 { "sqlite_stat4", 0 },
116674#endif
116675 { "sqlite_stat3", 0 },
116676 };
116677 int i;
116678 sqlite3 *db = pParse->db;
116679 Db *pDb;
116680 Vdbe *v = sqlite3GetVdbe(pParse);
116681 u32 aRoot[ArraySize(aTable)];
116682 u8 aCreateTbl[ArraySize(aTable)];
116683#ifdef SQLITE_ENABLE_STAT4
116684 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1;
116685#else
116686 const int nToOpen = 1;
116687#endif
116688
116689 if( v==0 ) return;
116690 assert( sqlite3BtreeHoldsAllMutexes(db) );
116691 assert( sqlite3VdbeDb(v)==db );
116692 pDb = &db->aDb[iDb];
116693
116694 /* Create new statistic tables if they do not exist, or clear them
116695 ** if they do already exist.
116696 */
116697 for(i=0; i<ArraySize(aTable); i++){
116698 const char *zTab = aTable[i].zName;
116699 Table *pStat;
116700 aCreateTbl[i] = 0;
116701 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
116702 if( i<nToOpen ){
116703 /* The sqlite_statN table does not exist. Create it. Note that a
116704 ** side-effect of the CREATE TABLE statement is to leave the rootpage
116705 ** of the new table in register pParse->regRoot. This is important
116706 ** because the OpenWrite opcode below will be needing it. */
116707 sqlite3NestedParse(pParse,
116708 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
116709 );
116710 aRoot[i] = (u32)pParse->regRoot;
116711 aCreateTbl[i] = OPFLAG_P2ISREG;
116712 }
116713 }else{
116714 /* The table already exists. If zWhere is not NULL, delete all entries
116715 ** associated with the table zWhere. If zWhere is NULL, delete the
116716 ** entire contents of the table. */
116717 aRoot[i] = pStat->tnum;
116718 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
116719 if( zWhere ){
116720 sqlite3NestedParse(pParse,
116721 "DELETE FROM %Q.%s WHERE %s=%Q",
116722 pDb->zDbSName, zTab, zWhereType, zWhere
116723 );
116724#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
116725 }else if( db->xPreUpdateCallback ){
116726 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
116727#endif
116728 }else{
116729 /* The sqlite_stat[134] table already exists. Delete all rows. */
116730 sqlite3VdbeAddOp2(p: v, OP_Clear, p1: (int)aRoot[i], p2: iDb);
116731 }
116732 }
116733 }
116734
116735 /* Open the sqlite_stat[134] tables for writing. */
116736 for(i=0; i<nToOpen; i++){
116737 assert( i<ArraySize(aTable) );
116738 sqlite3VdbeAddOp4Int(p: v, OP_OpenWrite, p1: iStatCur+i, p2: (int)aRoot[i], p3: iDb, p4: 3);
116739 sqlite3VdbeChangeP5(p: v, p5: aCreateTbl[i]);
116740 VdbeComment((v, aTable[i].zName));
116741 }
116742}
116743
116744/*
116745** Recommended number of samples for sqlite_stat4
116746*/
116747#ifndef SQLITE_STAT4_SAMPLES
116748# define SQLITE_STAT4_SAMPLES 24
116749#endif
116750
116751/*
116752** Three SQL functions - stat_init(), stat_push(), and stat_get() -
116753** share an instance of the following structure to hold their state
116754** information.
116755*/
116756typedef struct StatAccum StatAccum;
116757typedef struct StatSample StatSample;
116758struct StatSample {
116759 tRowcnt *anEq; /* sqlite_stat4.nEq */
116760 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
116761#ifdef SQLITE_ENABLE_STAT4
116762 tRowcnt *anLt; /* sqlite_stat4.nLt */
116763 union {
116764 i64 iRowid; /* Rowid in main table of the key */
116765 u8 *aRowid; /* Key for WITHOUT ROWID tables */
116766 } u;
116767 u32 nRowid; /* Sizeof aRowid[] */
116768 u8 isPSample; /* True if a periodic sample */
116769 int iCol; /* If !isPSample, the reason for inclusion */
116770 u32 iHash; /* Tiebreaker hash */
116771#endif
116772};
116773struct StatAccum {
116774 sqlite3 *db; /* Database connection, for malloc() */
116775 tRowcnt nEst; /* Estimated number of rows */
116776 tRowcnt nRow; /* Number of rows visited so far */
116777 int nLimit; /* Analysis row-scan limit */
116778 int nCol; /* Number of columns in index + pk/rowid */
116779 int nKeyCol; /* Number of index columns w/o the pk/rowid */
116780 u8 nSkipAhead; /* Number of times of skip-ahead */
116781 StatSample current; /* Current row as a StatSample */
116782#ifdef SQLITE_ENABLE_STAT4
116783 tRowcnt nPSample; /* How often to do a periodic sample */
116784 int mxSample; /* Maximum number of samples to accumulate */
116785 u32 iPrn; /* Pseudo-random number used for sampling */
116786 StatSample *aBest; /* Array of nCol best samples */
116787 int iMin; /* Index in a[] of entry with minimum score */
116788 int nSample; /* Current number of samples */
116789 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
116790 int iGet; /* Index of current sample accessed by stat_get() */
116791 StatSample *a; /* Array of mxSample StatSample objects */
116792#endif
116793};
116794
116795/* Reclaim memory used by a StatSample
116796*/
116797#ifdef SQLITE_ENABLE_STAT4
116798static void sampleClear(sqlite3 *db, StatSample *p){
116799 assert( db!=0 );
116800 if( p->nRowid ){
116801 sqlite3DbFree(db, p->u.aRowid);
116802 p->nRowid = 0;
116803 }
116804}
116805#endif
116806
116807/* Initialize the BLOB value of a ROWID
116808*/
116809#ifdef SQLITE_ENABLE_STAT4
116810static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
116811 assert( db!=0 );
116812 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
116813 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
116814 if( p->u.aRowid ){
116815 p->nRowid = n;
116816 memcpy(p->u.aRowid, pData, n);
116817 }else{
116818 p->nRowid = 0;
116819 }
116820}
116821#endif
116822
116823/* Initialize the INTEGER value of a ROWID.
116824*/
116825#ifdef SQLITE_ENABLE_STAT4
116826static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
116827 assert( db!=0 );
116828 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
116829 p->nRowid = 0;
116830 p->u.iRowid = iRowid;
116831}
116832#endif
116833
116834
116835/*
116836** Copy the contents of object (*pFrom) into (*pTo).
116837*/
116838#ifdef SQLITE_ENABLE_STAT4
116839static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
116840 pTo->isPSample = pFrom->isPSample;
116841 pTo->iCol = pFrom->iCol;
116842 pTo->iHash = pFrom->iHash;
116843 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
116844 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
116845 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
116846 if( pFrom->nRowid ){
116847 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
116848 }else{
116849 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
116850 }
116851}
116852#endif
116853
116854/*
116855** Reclaim all memory of a StatAccum structure.
116856*/
116857static void statAccumDestructor(void *pOld){
116858 StatAccum *p = (StatAccum*)pOld;
116859#ifdef SQLITE_ENABLE_STAT4
116860 if( p->mxSample ){
116861 int i;
116862 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
116863 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
116864 sampleClear(p->db, &p->current);
116865 }
116866#endif
116867 sqlite3DbFree(db: p->db, p);
116868}
116869
116870/*
116871** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
116872** are:
116873** N: The number of columns in the index including the rowid/pk (note 1)
116874** K: The number of columns in the index excluding the rowid/pk.
116875** C: Estimated number of rows in the index
116876** L: A limit on the number of rows to scan, or 0 for no-limit
116877**
116878** Note 1: In the special case of the covering index that implements a
116879** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
116880** total number of columns in the table.
116881**
116882** For indexes on ordinary rowid tables, N==K+1. But for indexes on
116883** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
116884** PRIMARY KEY of the table. The covering index that implements the
116885** original WITHOUT ROWID table as N==K as a special case.
116886**
116887** This routine allocates the StatAccum object in heap memory. The return
116888** value is a pointer to the StatAccum object. The datatype of the
116889** return value is BLOB, but it is really just a pointer to the StatAccum
116890** object.
116891*/
116892static void statInit(
116893 sqlite3_context *context,
116894 int argc,
116895 sqlite3_value **argv
116896){
116897 StatAccum *p;
116898 int nCol; /* Number of columns in index being sampled */
116899 int nKeyCol; /* Number of key columns */
116900 int nColUp; /* nCol rounded up for alignment */
116901 int n; /* Bytes of space to allocate */
116902 sqlite3 *db = sqlite3_context_db_handle(p: context); /* Database connection */
116903#ifdef SQLITE_ENABLE_STAT4
116904 /* Maximum number of samples. 0 if STAT4 data is not collected */
116905 int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0;
116906#endif
116907
116908 /* Decode the three function arguments */
116909 UNUSED_PARAMETER(argc);
116910 nCol = sqlite3_value_int(pVal: argv[0]);
116911 assert( nCol>0 );
116912 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
116913 nKeyCol = sqlite3_value_int(pVal: argv[1]);
116914 assert( nKeyCol<=nCol );
116915 assert( nKeyCol>0 );
116916
116917 /* Allocate the space required for the StatAccum object */
116918 n = sizeof(*p)
116919 + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
116920 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
116921#ifdef SQLITE_ENABLE_STAT4
116922 if( mxSample ){
116923 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
116924 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
116925 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
116926 }
116927#endif
116928 p = sqlite3DbMallocZero(db, n);
116929 if( p==0 ){
116930 sqlite3_result_error_nomem(pCtx: context);
116931 return;
116932 }
116933
116934 p->db = db;
116935 p->nEst = sqlite3_value_int64(pVal: argv[2]);
116936 p->nRow = 0;
116937 p->nLimit = sqlite3_value_int64(pVal: argv[3]);
116938 p->nCol = nCol;
116939 p->nKeyCol = nKeyCol;
116940 p->nSkipAhead = 0;
116941 p->current.anDLt = (tRowcnt*)&p[1];
116942 p->current.anEq = &p->current.anDLt[nColUp];
116943
116944#ifdef SQLITE_ENABLE_STAT4
116945 p->mxSample = p->nLimit==0 ? mxSample : 0;
116946 if( mxSample ){
116947 u8 *pSpace; /* Allocated space not yet assigned */
116948 int i; /* Used to iterate through p->aSample[] */
116949
116950 p->iGet = -1;
116951 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
116952 p->current.anLt = &p->current.anEq[nColUp];
116953 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
116954
116955 /* Set up the StatAccum.a[] and aBest[] arrays */
116956 p->a = (struct StatSample*)&p->current.anLt[nColUp];
116957 p->aBest = &p->a[mxSample];
116958 pSpace = (u8*)(&p->a[mxSample+nCol]);
116959 for(i=0; i<(mxSample+nCol); i++){
116960 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
116961 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
116962 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
116963 }
116964 assert( (pSpace - (u8*)p)==n );
116965
116966 for(i=0; i<nCol; i++){
116967 p->aBest[i].iCol = i;
116968 }
116969 }
116970#endif
116971
116972 /* Return a pointer to the allocated object to the caller. Note that
116973 ** only the pointer (the 2nd parameter) matters. The size of the object
116974 ** (given by the 3rd parameter) is never used and can be any positive
116975 ** value. */
116976 sqlite3_result_blob(pCtx: context, z: p, n: sizeof(*p), xDel: statAccumDestructor);
116977}
116978static const FuncDef statInitFuncdef = {
116979 4, /* nArg */
116980 SQLITE_UTF8, /* funcFlags */
116981 0, /* pUserData */
116982 0, /* pNext */
116983 statInit, /* xSFunc */
116984 0, /* xFinalize */
116985 0, 0, /* xValue, xInverse */
116986 "stat_init", /* zName */
116987 {0}
116988};
116989
116990#ifdef SQLITE_ENABLE_STAT4
116991/*
116992** pNew and pOld are both candidate non-periodic samples selected for
116993** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
116994** considering only any trailing columns and the sample hash value, this
116995** function returns true if sample pNew is to be preferred over pOld.
116996** In other words, if we assume that the cardinalities of the selected
116997** column for pNew and pOld are equal, is pNew to be preferred over pOld.
116998**
116999** This function assumes that for each argument sample, the contents of
117000** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
117001*/
117002static int sampleIsBetterPost(
117003 StatAccum *pAccum,
117004 StatSample *pNew,
117005 StatSample *pOld
117006){
117007 int nCol = pAccum->nCol;
117008 int i;
117009 assert( pNew->iCol==pOld->iCol );
117010 for(i=pNew->iCol+1; i<nCol; i++){
117011 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
117012 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
117013 }
117014 if( pNew->iHash>pOld->iHash ) return 1;
117015 return 0;
117016}
117017#endif
117018
117019#ifdef SQLITE_ENABLE_STAT4
117020/*
117021** Return true if pNew is to be preferred over pOld.
117022**
117023** This function assumes that for each argument sample, the contents of
117024** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
117025*/
117026static int sampleIsBetter(
117027 StatAccum *pAccum,
117028 StatSample *pNew,
117029 StatSample *pOld
117030){
117031 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
117032 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
117033
117034 assert( pOld->isPSample==0 && pNew->isPSample==0 );
117035 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
117036
117037 if( (nEqNew>nEqOld) ) return 1;
117038 if( nEqNew==nEqOld ){
117039 if( pNew->iCol<pOld->iCol ) return 1;
117040 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
117041 }
117042 return 0;
117043}
117044
117045/*
117046** Copy the contents of sample *pNew into the p->a[] array. If necessary,
117047** remove the least desirable sample from p->a[] to make room.
117048*/
117049static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
117050 StatSample *pSample = 0;
117051 int i;
117052
117053 assert( IsStat4 || nEqZero==0 );
117054
117055 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
117056 ** values in the anEq[] array of any sample in StatAccum.a[]. In
117057 ** other words, if nMaxEqZero is n, then it is guaranteed that there
117058 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
117059 if( nEqZero>p->nMaxEqZero ){
117060 p->nMaxEqZero = nEqZero;
117061 }
117062 if( pNew->isPSample==0 ){
117063 StatSample *pUpgrade = 0;
117064 assert( pNew->anEq[pNew->iCol]>0 );
117065
117066 /* This sample is being added because the prefix that ends in column
117067 ** iCol occurs many times in the table. However, if we have already
117068 ** added a sample that shares this prefix, there is no need to add
117069 ** this one. Instead, upgrade the priority of the highest priority
117070 ** existing sample that shares this prefix. */
117071 for(i=p->nSample-1; i>=0; i--){
117072 StatSample *pOld = &p->a[i];
117073 if( pOld->anEq[pNew->iCol]==0 ){
117074 if( pOld->isPSample ) return;
117075 assert( pOld->iCol>pNew->iCol );
117076 assert( sampleIsBetter(p, pNew, pOld) );
117077 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
117078 pUpgrade = pOld;
117079 }
117080 }
117081 }
117082 if( pUpgrade ){
117083 pUpgrade->iCol = pNew->iCol;
117084 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
117085 goto find_new_min;
117086 }
117087 }
117088
117089 /* If necessary, remove sample iMin to make room for the new sample. */
117090 if( p->nSample>=p->mxSample ){
117091 StatSample *pMin = &p->a[p->iMin];
117092 tRowcnt *anEq = pMin->anEq;
117093 tRowcnt *anLt = pMin->anLt;
117094 tRowcnt *anDLt = pMin->anDLt;
117095 sampleClear(p->db, pMin);
117096 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
117097 pSample = &p->a[p->nSample-1];
117098 pSample->nRowid = 0;
117099 pSample->anEq = anEq;
117100 pSample->anDLt = anDLt;
117101 pSample->anLt = anLt;
117102 p->nSample = p->mxSample-1;
117103 }
117104
117105 /* The "rows less-than" for the rowid column must be greater than that
117106 ** for the last sample in the p->a[] array. Otherwise, the samples would
117107 ** be out of order. */
117108 assert( p->nSample==0
117109 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
117110
117111 /* Insert the new sample */
117112 pSample = &p->a[p->nSample];
117113 sampleCopy(p, pSample, pNew);
117114 p->nSample++;
117115
117116 /* Zero the first nEqZero entries in the anEq[] array. */
117117 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
117118
117119find_new_min:
117120 if( p->nSample>=p->mxSample ){
117121 int iMin = -1;
117122 for(i=0; i<p->mxSample; i++){
117123 if( p->a[i].isPSample ) continue;
117124 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
117125 iMin = i;
117126 }
117127 }
117128 assert( iMin>=0 );
117129 p->iMin = iMin;
117130 }
117131}
117132#endif /* SQLITE_ENABLE_STAT4 */
117133
117134#ifdef SQLITE_ENABLE_STAT4
117135/*
117136** Field iChng of the index being scanned has changed. So at this point
117137** p->current contains a sample that reflects the previous row of the
117138** index. The value of anEq[iChng] and subsequent anEq[] elements are
117139** correct at this point.
117140*/
117141static void samplePushPrevious(StatAccum *p, int iChng){
117142 int i;
117143
117144 /* Check if any samples from the aBest[] array should be pushed
117145 ** into IndexSample.a[] at this point. */
117146 for(i=(p->nCol-2); i>=iChng; i--){
117147 StatSample *pBest = &p->aBest[i];
117148 pBest->anEq[i] = p->current.anEq[i];
117149 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
117150 sampleInsert(p, pBest, i);
117151 }
117152 }
117153
117154 /* Check that no sample contains an anEq[] entry with an index of
117155 ** p->nMaxEqZero or greater set to zero. */
117156 for(i=p->nSample-1; i>=0; i--){
117157 int j;
117158 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
117159 }
117160
117161 /* Update the anEq[] fields of any samples already collected. */
117162 if( iChng<p->nMaxEqZero ){
117163 for(i=p->nSample-1; i>=0; i--){
117164 int j;
117165 for(j=iChng; j<p->nCol; j++){
117166 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
117167 }
117168 }
117169 p->nMaxEqZero = iChng;
117170 }
117171}
117172#endif /* SQLITE_ENABLE_STAT4 */
117173
117174/*
117175** Implementation of the stat_push SQL function: stat_push(P,C,R)
117176** Arguments:
117177**
117178** P Pointer to the StatAccum object created by stat_init()
117179** C Index of left-most column to differ from previous row
117180** R Rowid for the current row. Might be a key record for
117181** WITHOUT ROWID tables.
117182**
117183** The purpose of this routine is to collect statistical data and/or
117184** samples from the index being analyzed into the StatAccum object.
117185** The stat_get() SQL function will be used afterwards to
117186** retrieve the information gathered.
117187**
117188** This SQL function usually returns NULL, but might return an integer
117189** if it wants the byte-code to do special processing.
117190**
117191** The R parameter is only used for STAT4
117192*/
117193static void statPush(
117194 sqlite3_context *context,
117195 int argc,
117196 sqlite3_value **argv
117197){
117198 int i;
117199
117200 /* The three function arguments */
117201 StatAccum *p = (StatAccum*)sqlite3_value_blob(pVal: argv[0]);
117202 int iChng = sqlite3_value_int(pVal: argv[1]);
117203
117204 UNUSED_PARAMETER( argc );
117205 UNUSED_PARAMETER( context );
117206 assert( p->nCol>0 );
117207 assert( iChng<p->nCol );
117208
117209 if( p->nRow==0 ){
117210 /* This is the first call to this function. Do initialization. */
117211 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
117212 }else{
117213 /* Second and subsequent calls get processed here */
117214#ifdef SQLITE_ENABLE_STAT4
117215 if( p->mxSample ) samplePushPrevious(p, iChng);
117216#endif
117217
117218 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
117219 ** to the current row of the index. */
117220 for(i=0; i<iChng; i++){
117221 p->current.anEq[i]++;
117222 }
117223 for(i=iChng; i<p->nCol; i++){
117224 p->current.anDLt[i]++;
117225#ifdef SQLITE_ENABLE_STAT4
117226 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
117227#endif
117228 p->current.anEq[i] = 1;
117229 }
117230 }
117231
117232 p->nRow++;
117233#ifdef SQLITE_ENABLE_STAT4
117234 if( p->mxSample ){
117235 tRowcnt nLt;
117236 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
117237 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
117238 }else{
117239 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
117240 sqlite3_value_blob(argv[2]));
117241 }
117242 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
117243
117244 nLt = p->current.anLt[p->nCol-1];
117245 /* Check if this is to be a periodic sample. If so, add it. */
117246 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
117247 p->current.isPSample = 1;
117248 p->current.iCol = 0;
117249 sampleInsert(p, &p->current, p->nCol-1);
117250 p->current.isPSample = 0;
117251 }
117252
117253 /* Update the aBest[] array. */
117254 for(i=0; i<(p->nCol-1); i++){
117255 p->current.iCol = i;
117256 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
117257 sampleCopy(p, &p->aBest[i], &p->current);
117258 }
117259 }
117260 }else
117261#endif
117262 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
117263 p->nSkipAhead++;
117264 sqlite3_result_int(pCtx: context, iVal: p->current.anDLt[0]>0);
117265 }
117266}
117267
117268static const FuncDef statPushFuncdef = {
117269 2+IsStat4, /* nArg */
117270 SQLITE_UTF8, /* funcFlags */
117271 0, /* pUserData */
117272 0, /* pNext */
117273 statPush, /* xSFunc */
117274 0, /* xFinalize */
117275 0, 0, /* xValue, xInverse */
117276 "stat_push", /* zName */
117277 {0}
117278};
117279
117280#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
117281#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
117282#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
117283#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
117284#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
117285
117286/*
117287** Implementation of the stat_get(P,J) SQL function. This routine is
117288** used to query statistical information that has been gathered into
117289** the StatAccum object by prior calls to stat_push(). The P parameter
117290** has type BLOB but it is really just a pointer to the StatAccum object.
117291** The content to returned is determined by the parameter J
117292** which is one of the STAT_GET_xxxx values defined above.
117293**
117294** The stat_get(P,J) function is not available to generic SQL. It is
117295** inserted as part of a manually constructed bytecode program. (See
117296** the callStatGet() routine below.) It is guaranteed that the P
117297** parameter will always be a pointer to a StatAccum object, never a
117298** NULL.
117299**
117300** If STAT4 is not enabled, then J is always
117301** STAT_GET_STAT1 and is hence omitted and this routine becomes
117302** a one-parameter function, stat_get(P), that always returns the
117303** stat1 table entry information.
117304*/
117305static void statGet(
117306 sqlite3_context *context,
117307 int argc,
117308 sqlite3_value **argv
117309){
117310 StatAccum *p = (StatAccum*)sqlite3_value_blob(pVal: argv[0]);
117311#ifdef SQLITE_ENABLE_STAT4
117312 /* STAT4 has a parameter on this routine. */
117313 int eCall = sqlite3_value_int(argv[1]);
117314 assert( argc==2 );
117315 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
117316 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
117317 || eCall==STAT_GET_NDLT
117318 );
117319 assert( eCall==STAT_GET_STAT1 || p->mxSample );
117320 if( eCall==STAT_GET_STAT1 )
117321#else
117322 assert( argc==1 );
117323#endif
117324 {
117325 /* Return the value to store in the "stat" column of the sqlite_stat1
117326 ** table for this index.
117327 **
117328 ** The value is a string composed of a list of integers describing
117329 ** the index. The first integer in the list is the total number of
117330 ** entries in the index. There is one additional integer in the list
117331 ** for each indexed column. This additional integer is an estimate of
117332 ** the number of rows matched by a equality query on the index using
117333 ** a key with the corresponding number of fields. In other words,
117334 ** if the index is on columns (a,b) and the sqlite_stat1 value is
117335 ** "100 10 2", then SQLite estimates that:
117336 **
117337 ** * the index contains 100 rows,
117338 ** * "WHERE a=?" matches 10 rows, and
117339 ** * "WHERE a=? AND b=?" matches 2 rows.
117340 **
117341 ** If D is the count of distinct values and K is the total number of
117342 ** rows, then each estimate is usually computed as:
117343 **
117344 ** I = (K+D-1)/D
117345 **
117346 ** In other words, I is K/D rounded up to the next whole integer.
117347 ** However, if I is between 1.0 and 1.1 (in other words if I is
117348 ** close to 1.0 but just a little larger) then do not round up but
117349 ** instead keep the I value at 1.0.
117350 */
117351 sqlite3_str sStat; /* Text of the constructed "stat" line */
117352 int i; /* Loop counter */
117353
117354 sqlite3StrAccumInit(p: &sStat, db: 0, zBase: 0, n: 0, mx: (p->nKeyCol+1)*100);
117355 sqlite3_str_appendf(p: &sStat, zFormat: "%llu",
117356 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
117357 for(i=0; i<p->nKeyCol; i++){
117358 u64 nDistinct = p->current.anDLt[i] + 1;
117359 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
117360 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
117361 sqlite3_str_appendf(p: &sStat, zFormat: " %llu", iVal);
117362 assert( p->current.anEq[i] );
117363 }
117364 sqlite3ResultStrAccum(pCtx: context, p: &sStat);
117365 }
117366#ifdef SQLITE_ENABLE_STAT4
117367 else if( eCall==STAT_GET_ROWID ){
117368 if( p->iGet<0 ){
117369 samplePushPrevious(p, 0);
117370 p->iGet = 0;
117371 }
117372 if( p->iGet<p->nSample ){
117373 StatSample *pS = p->a + p->iGet;
117374 if( pS->nRowid==0 ){
117375 sqlite3_result_int64(context, pS->u.iRowid);
117376 }else{
117377 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
117378 SQLITE_TRANSIENT);
117379 }
117380 }
117381 }else{
117382 tRowcnt *aCnt = 0;
117383 sqlite3_str sStat;
117384 int i;
117385
117386 assert( p->iGet<p->nSample );
117387 switch( eCall ){
117388 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
117389 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
117390 default: {
117391 aCnt = p->a[p->iGet].anDLt;
117392 p->iGet++;
117393 break;
117394 }
117395 }
117396 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
117397 for(i=0; i<p->nCol; i++){
117398 sqlite3_str_appendf(&sStat, "%llu ", (u64)aCnt[i]);
117399 }
117400 if( sStat.nChar ) sStat.nChar--;
117401 sqlite3ResultStrAccum(context, &sStat);
117402 }
117403#endif /* SQLITE_ENABLE_STAT4 */
117404#ifndef SQLITE_DEBUG
117405 UNUSED_PARAMETER( argc );
117406#endif
117407}
117408static const FuncDef statGetFuncdef = {
117409 1+IsStat4, /* nArg */
117410 SQLITE_UTF8, /* funcFlags */
117411 0, /* pUserData */
117412 0, /* pNext */
117413 statGet, /* xSFunc */
117414 0, /* xFinalize */
117415 0, 0, /* xValue, xInverse */
117416 "stat_get", /* zName */
117417 {0}
117418};
117419
117420static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
117421#ifdef SQLITE_ENABLE_STAT4
117422 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
117423#elif SQLITE_DEBUG
117424 assert( iParam==STAT_GET_STAT1 );
117425#else
117426 UNUSED_PARAMETER( iParam );
117427#endif
117428 assert( regOut!=regStat && regOut!=regStat+1 );
117429 sqlite3VdbeAddFunctionCall(pParse, p1: 0, p2: regStat, p3: regOut, nArg: 1+IsStat4,
117430 pFunc: &statGetFuncdef, eCallCtx: 0);
117431}
117432
117433#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
117434/* Add a comment to the most recent VDBE opcode that is the name
117435** of the k-th column of the pIdx index.
117436*/
117437static void analyzeVdbeCommentIndexWithColumnName(
117438 Vdbe *v, /* Prepared statement under construction */
117439 Index *pIdx, /* Index whose column is being loaded */
117440 int k /* Which column index */
117441){
117442 int i; /* Index of column in the table */
117443 assert( k>=0 && k<pIdx->nColumn );
117444 i = pIdx->aiColumn[k];
117445 if( NEVER(i==XN_ROWID) ){
117446 VdbeComment((v,"%s.rowid",pIdx->zName));
117447 }else if( i==XN_EXPR ){
117448 assert( pIdx->bHasExpr );
117449 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
117450 }else{
117451 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
117452 }
117453}
117454#else
117455# define analyzeVdbeCommentIndexWithColumnName(a,b,c)
117456#endif /* SQLITE_DEBUG */
117457
117458/*
117459** Generate code to do an analysis of all indices associated with
117460** a single table.
117461*/
117462static void analyzeOneTable(
117463 Parse *pParse, /* Parser context */
117464 Table *pTab, /* Table whose indices are to be analyzed */
117465 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
117466 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
117467 int iMem, /* Available memory locations begin here */
117468 int iTab /* Next available cursor */
117469){
117470 sqlite3 *db = pParse->db; /* Database handle */
117471 Index *pIdx; /* An index to being analyzed */
117472 int iIdxCur; /* Cursor open on index being analyzed */
117473 int iTabCur; /* Table cursor */
117474 Vdbe *v; /* The virtual machine being built up */
117475 int i; /* Loop counter */
117476 int jZeroRows = -1; /* Jump from here if number of rows is zero */
117477 int iDb; /* Index of database containing pTab */
117478 u8 needTableCnt = 1; /* True to count the table */
117479 int regNewRowid = iMem++; /* Rowid for the inserted record */
117480 int regStat = iMem++; /* Register to hold StatAccum object */
117481 int regChng = iMem++; /* Index of changed index field */
117482 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
117483 int regTemp = iMem++; /* Temporary use register */
117484 int regTemp2 = iMem++; /* Second temporary use register */
117485 int regTabname = iMem++; /* Register containing table name */
117486 int regIdxname = iMem++; /* Register containing index name */
117487 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
117488 int regPrev = iMem; /* MUST BE LAST (see below) */
117489#ifdef SQLITE_ENABLE_STAT4
117490 int doOnce = 1; /* Flag for a one-time computation */
117491#endif
117492#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
117493 Table *pStat1 = 0;
117494#endif
117495
117496 sqlite3TouchRegister(pParse, iReg: iMem);
117497 assert( sqlite3NoTempsInRange(pParse, regNewRowid, iMem) );
117498 v = sqlite3GetVdbe(pParse);
117499 if( v==0 || NEVER(pTab==0) ){
117500 return;
117501 }
117502 if( !IsOrdinaryTable(pTab) ){
117503 /* Do not gather statistics on views or virtual tables */
117504 return;
117505 }
117506 if( sqlite3_strlike(zGlob: "sqlite\\_%", zStr: pTab->zName, cEsc: '\\')==0 ){
117507 /* Do not gather statistics on system tables */
117508 return;
117509 }
117510 assert( sqlite3BtreeHoldsAllMutexes(db) );
117511 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117512 assert( iDb>=0 );
117513 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
117514#ifndef SQLITE_OMIT_AUTHORIZATION
117515 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
117516 db->aDb[iDb].zDbSName ) ){
117517 return;
117518 }
117519#endif
117520
117521#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
117522 if( db->xPreUpdateCallback ){
117523 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
117524 if( pStat1==0 ) return;
117525 pStat1->zName = (char*)&pStat1[1];
117526 memcpy(pStat1->zName, "sqlite_stat1", 13);
117527 pStat1->nCol = 3;
117528 pStat1->iPKey = -1;
117529 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
117530 }
117531#endif
117532
117533 /* Establish a read-lock on the table at the shared-cache level.
117534 ** Open a read-only cursor on the table. Also allocate a cursor number
117535 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
117536 ** this time though. */
117537 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
117538 iTabCur = iTab++;
117539 iIdxCur = iTab++;
117540 pParse->nTab = MAX(pParse->nTab, iTab);
117541 sqlite3OpenTable(pParse, iCur: iTabCur, iDb, pTab, OP_OpenRead);
117542 sqlite3VdbeLoadString(p: v, iDest: regTabname, zStr: pTab->zName);
117543
117544 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117545 int nCol; /* Number of columns in pIdx. "N" */
117546 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
117547 int addrNextRow; /* Address of "next_row:" */
117548 const char *zIdxName; /* Name of the index */
117549 int nColTest; /* Number of columns to test for changes */
117550
117551 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
117552 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
117553 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
117554 nCol = pIdx->nKeyCol;
117555 zIdxName = pTab->zName;
117556 nColTest = nCol - 1;
117557 }else{
117558 nCol = pIdx->nColumn;
117559 zIdxName = pIdx->zName;
117560 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
117561 }
117562
117563 /* Populate the register containing the index name. */
117564 sqlite3VdbeLoadString(p: v, iDest: regIdxname, zStr: zIdxName);
117565 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
117566
117567 /*
117568 ** Pseudo-code for loop that calls stat_push():
117569 **
117570 ** Rewind csr
117571 ** if eof(csr) goto end_of_scan;
117572 ** regChng = 0
117573 ** goto chng_addr_0;
117574 **
117575 ** next_row:
117576 ** regChng = 0
117577 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
117578 ** regChng = 1
117579 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
117580 ** ...
117581 ** regChng = N
117582 ** goto chng_addr_N
117583 **
117584 ** chng_addr_0:
117585 ** regPrev(0) = idx(0)
117586 ** chng_addr_1:
117587 ** regPrev(1) = idx(1)
117588 ** ...
117589 **
117590 ** endDistinctTest:
117591 ** regRowid = idx(rowid)
117592 ** stat_push(P, regChng, regRowid)
117593 ** Next csr
117594 ** if !eof(csr) goto next_row;
117595 **
117596 ** end_of_scan:
117597 */
117598
117599 /* Make sure there are enough memory cells allocated to accommodate
117600 ** the regPrev array and a trailing rowid (the rowid slot is required
117601 ** when building a record to insert into the sample column of
117602 ** the sqlite_stat4 table. */
117603 sqlite3TouchRegister(pParse, iReg: regPrev+nColTest);
117604
117605 /* Open a read-only cursor on the index being analyzed. */
117606 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
117607 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iIdxCur, p2: pIdx->tnum, p3: iDb);
117608 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
117609 VdbeComment((v, "%s", pIdx->zName));
117610
117611 /* Invoke the stat_init() function. The arguments are:
117612 **
117613 ** (1) the number of columns in the index including the rowid
117614 ** (or for a WITHOUT ROWID table, the number of PK columns),
117615 ** (2) the number of columns in the key without the rowid/pk
117616 ** (3) estimated number of rows in the index,
117617 */
117618 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: nCol, p2: regStat+1);
117619 assert( regRowid==regStat+2 );
117620 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: pIdx->nKeyCol, p2: regRowid);
117621#ifdef SQLITE_ENABLE_STAT4
117622 if( OptimizationEnabled(db, SQLITE_Stat4) ){
117623 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp);
117624 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
117625 VdbeCoverage(v);
117626 }else
117627#endif
117628 {
117629 addrRewind = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iIdxCur);
117630 VdbeCoverage(v);
117631 sqlite3VdbeAddOp3(p: v, OP_Count, p1: iIdxCur, p2: regTemp, p3: 1);
117632 }
117633 assert( regTemp2==regStat+4 );
117634 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: db->nAnalysisLimit, p2: regTemp2);
117635 sqlite3VdbeAddFunctionCall(pParse, p1: 0, p2: regStat+1, p3: regStat, nArg: 4,
117636 pFunc: &statInitFuncdef, eCallCtx: 0);
117637
117638 /* Implementation of the following:
117639 **
117640 ** Rewind csr
117641 ** if eof(csr) goto end_of_scan;
117642 ** regChng = 0
117643 ** goto next_push_0;
117644 **
117645 */
117646 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regChng);
117647 addrNextRow = sqlite3VdbeCurrentAddr(p: v);
117648
117649 if( nColTest>0 ){
117650 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
117651 int *aGotoChng; /* Array of jump instruction addresses */
117652 aGotoChng = sqlite3DbMallocRawNN(db, n: sizeof(int)*nColTest);
117653 if( aGotoChng==0 ) continue;
117654
117655 /*
117656 ** next_row:
117657 ** regChng = 0
117658 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
117659 ** regChng = 1
117660 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
117661 ** ...
117662 ** regChng = N
117663 ** goto endDistinctTest
117664 */
117665 sqlite3VdbeAddOp0(p: v, OP_Goto);
117666 addrNextRow = sqlite3VdbeCurrentAddr(p: v);
117667 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
117668 /* For a single-column UNIQUE index, once we have found a non-NULL
117669 ** row, we know that all the rest will be distinct, so skip
117670 ** subsequent distinctness tests. */
117671 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: regPrev, p2: endDistinctTest);
117672 VdbeCoverage(v);
117673 }
117674 for(i=0; i<nColTest; i++){
117675 char *pColl = (char*)sqlite3LocateCollSeq(pParse, zName: pIdx->azColl[i]);
117676 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: i, p2: regChng);
117677 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: i, p3: regTemp);
117678 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
117679 aGotoChng[i] =
117680 sqlite3VdbeAddOp4(p: v, OP_Ne, p1: regTemp, p2: 0, p3: regPrev+i, zP4: pColl, P4_COLLSEQ);
117681 sqlite3VdbeChangeP5(p: v, SQLITE_NULLEQ);
117682 VdbeCoverage(v);
117683 }
117684 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: nColTest, p2: regChng);
117685 sqlite3VdbeGoto(p: v, iDest: endDistinctTest);
117686
117687
117688 /*
117689 ** chng_addr_0:
117690 ** regPrev(0) = idx(0)
117691 ** chng_addr_1:
117692 ** regPrev(1) = idx(1)
117693 ** ...
117694 */
117695 sqlite3VdbeJumpHere(p: v, addr: addrNextRow-1);
117696 for(i=0; i<nColTest; i++){
117697 sqlite3VdbeJumpHere(p: v, addr: aGotoChng[i]);
117698 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: i, p3: regPrev+i);
117699 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
117700 }
117701 sqlite3VdbeResolveLabel(v, x: endDistinctTest);
117702 sqlite3DbFree(db, p: aGotoChng);
117703 }
117704
117705 /*
117706 ** chng_addr_N:
117707 ** regRowid = idx(rowid) // STAT4 only
117708 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only
117709 ** Next csr
117710 ** if !eof(csr) goto next_row;
117711 */
117712#ifdef SQLITE_ENABLE_STAT4
117713 if( OptimizationEnabled(db, SQLITE_Stat4) ){
117714 assert( regRowid==(regStat+2) );
117715 if( HasRowid(pTab) ){
117716 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
117717 }else{
117718 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
117719 int j, k, regKey;
117720 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
117721 for(j=0; j<pPk->nKeyCol; j++){
117722 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
117723 assert( k>=0 && k<pIdx->nColumn );
117724 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
117725 analyzeVdbeCommentIndexWithColumnName(v,pIdx,k);
117726 }
117727 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
117728 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
117729 }
117730 }
117731#endif
117732 assert( regChng==(regStat+1) );
117733 {
117734 sqlite3VdbeAddFunctionCall(pParse, p1: 1, p2: regStat, p3: regTemp, nArg: 2+IsStat4,
117735 pFunc: &statPushFuncdef, eCallCtx: 0);
117736 if( db->nAnalysisLimit ){
117737 int j1, j2, j3;
117738 j1 = sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: regTemp); VdbeCoverage(v);
117739 j2 = sqlite3VdbeAddOp1(p: v, OP_If, p1: regTemp); VdbeCoverage(v);
117740 j3 = sqlite3VdbeAddOp4Int(p: v, OP_SeekGT, p1: iIdxCur, p2: 0, p3: regPrev, p4: 1);
117741 VdbeCoverage(v);
117742 sqlite3VdbeJumpHere(p: v, addr: j1);
117743 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iIdxCur, p2: addrNextRow); VdbeCoverage(v);
117744 sqlite3VdbeJumpHere(p: v, addr: j2);
117745 sqlite3VdbeJumpHere(p: v, addr: j3);
117746 }else{
117747 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iIdxCur, p2: addrNextRow); VdbeCoverage(v);
117748 }
117749 }
117750
117751 /* Add the entry to the stat1 table. */
117752 callStatGet(pParse, regStat, STAT_GET_STAT1, regOut: regStat1);
117753 assert( "BBB"[0]==SQLITE_AFF_TEXT );
117754 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regTabname, p2: 3, p3: regTemp, zP4: "BBB", p4type: 0);
117755 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iStatCur, p2: regNewRowid);
117756 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iStatCur, p2: regTemp, p3: regNewRowid);
117757#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
117758 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
117759#endif
117760 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
117761
117762 /* Add the entries to the stat4 table. */
117763#ifdef SQLITE_ENABLE_STAT4
117764 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
117765 int regEq = regStat1;
117766 int regLt = regStat1+1;
117767 int regDLt = regStat1+2;
117768 int regSample = regStat1+3;
117769 int regCol = regStat1+4;
117770 int regSampleRowid = regCol + nCol;
117771 int addrNext;
117772 int addrIsNull;
117773 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
117774
117775 if( doOnce ){
117776 int mxCol = nCol;
117777 Index *pX;
117778
117779 /* Compute the maximum number of columns in any index */
117780 for(pX=pTab->pIndex; pX; pX=pX->pNext){
117781 int nColX; /* Number of columns in pX */
117782 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pX) ){
117783 nColX = pX->nKeyCol;
117784 }else{
117785 nColX = pX->nColumn;
117786 }
117787 if( nColX>mxCol ) mxCol = nColX;
117788 }
117789
117790 /* Allocate space to compute results for the largest index */
117791 sqlite3TouchRegister(pParse, regCol+mxCol);
117792 doOnce = 0;
117793#ifdef SQLITE_DEBUG
117794 /* Verify that the call to sqlite3ClearTempRegCache() below
117795 ** really is needed.
117796 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
117797 */
117798 testcase( !sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) );
117799#endif
117800 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
117801 assert( sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) );
117802 }
117803 assert( sqlite3NoTempsInRange(pParse, regEq, regCol+nCol) );
117804
117805 addrNext = sqlite3VdbeCurrentAddr(v);
117806 callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid);
117807 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
117808 VdbeCoverage(v);
117809 callStatGet(pParse, regStat, STAT_GET_NEQ, regEq);
117810 callStatGet(pParse, regStat, STAT_GET_NLT, regLt);
117811 callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt);
117812 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
117813 VdbeCoverage(v);
117814 for(i=0; i<nCol; i++){
117815 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
117816 }
117817 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
117818 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
117819 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
117820 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
117821 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
117822 sqlite3VdbeJumpHere(v, addrIsNull);
117823 }
117824#endif /* SQLITE_ENABLE_STAT4 */
117825
117826 /* End of analysis */
117827 sqlite3VdbeJumpHere(p: v, addr: addrRewind);
117828 }
117829
117830
117831 /* Create a single sqlite_stat1 entry containing NULL as the index
117832 ** name and the row count as the content.
117833 */
117834 if( pOnlyIdx==0 && needTableCnt ){
117835 VdbeComment((v, "%s", pTab->zName));
117836 sqlite3VdbeAddOp2(p: v, OP_Count, p1: iTabCur, p2: regStat1);
117837 jZeroRows = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: regStat1); VdbeCoverage(v);
117838 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regIdxname);
117839 assert( "BBB"[0]==SQLITE_AFF_TEXT );
117840 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regTabname, p2: 3, p3: regTemp, zP4: "BBB", p4type: 0);
117841 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iStatCur, p2: regNewRowid);
117842 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iStatCur, p2: regTemp, p3: regNewRowid);
117843 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
117844#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
117845 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
117846#endif
117847 sqlite3VdbeJumpHere(p: v, addr: jZeroRows);
117848 }
117849}
117850
117851
117852/*
117853** Generate code that will cause the most recent index analysis to
117854** be loaded into internal hash tables where is can be used.
117855*/
117856static void loadAnalysis(Parse *pParse, int iDb){
117857 Vdbe *v = sqlite3GetVdbe(pParse);
117858 if( v ){
117859 sqlite3VdbeAddOp1(p: v, OP_LoadAnalysis, p1: iDb);
117860 }
117861}
117862
117863/*
117864** Generate code that will do an analysis of an entire database
117865*/
117866static void analyzeDatabase(Parse *pParse, int iDb){
117867 sqlite3 *db = pParse->db;
117868 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
117869 HashElem *k;
117870 int iStatCur;
117871 int iMem;
117872 int iTab;
117873
117874 sqlite3BeginWriteOperation(pParse, 0, iDb);
117875 iStatCur = pParse->nTab;
117876 pParse->nTab += 3;
117877 openStatTable(pParse, iDb, iStatCur, zWhere: 0, zWhereType: 0);
117878 iMem = pParse->nMem+1;
117879 iTab = pParse->nTab;
117880 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
117881 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
117882 Table *pTab = (Table*)sqliteHashData(k);
117883 analyzeOneTable(pParse, pTab, pOnlyIdx: 0, iStatCur, iMem, iTab);
117884#ifdef SQLITE_ENABLE_STAT4
117885 iMem = sqlite3FirstAvailableRegister(pParse, iMem);
117886#else
117887 assert( iMem==sqlite3FirstAvailableRegister(pParse,iMem) );
117888#endif
117889 }
117890 loadAnalysis(pParse, iDb);
117891}
117892
117893/*
117894** Generate code that will do an analysis of a single table in
117895** a database. If pOnlyIdx is not NULL then it is a single index
117896** in pTab that should be analyzed.
117897*/
117898static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
117899 int iDb;
117900 int iStatCur;
117901
117902 assert( pTab!=0 );
117903 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
117904 iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
117905 sqlite3BeginWriteOperation(pParse, 0, iDb);
117906 iStatCur = pParse->nTab;
117907 pParse->nTab += 3;
117908 if( pOnlyIdx ){
117909 openStatTable(pParse, iDb, iStatCur, zWhere: pOnlyIdx->zName, zWhereType: "idx");
117910 }else{
117911 openStatTable(pParse, iDb, iStatCur, zWhere: pTab->zName, zWhereType: "tbl");
117912 }
117913 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,iMem: pParse->nMem+1,iTab: pParse->nTab);
117914 loadAnalysis(pParse, iDb);
117915}
117916
117917/*
117918** Generate code for the ANALYZE command. The parser calls this routine
117919** when it recognizes an ANALYZE command.
117920**
117921** ANALYZE -- 1
117922** ANALYZE <database> -- 2
117923** ANALYZE ?<database>.?<tablename> -- 3
117924**
117925** Form 1 causes all indices in all attached databases to be analyzed.
117926** Form 2 analyzes all indices the single database named.
117927** Form 3 analyzes all indices associated with the named table.
117928*/
117929SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
117930 sqlite3 *db = pParse->db;
117931 int iDb;
117932 int i;
117933 char *z, *zDb;
117934 Table *pTab;
117935 Index *pIdx;
117936 Token *pTableName;
117937 Vdbe *v;
117938
117939 /* Read the database schema. If an error occurs, leave an error message
117940 ** and code in pParse and return NULL. */
117941 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
117942 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
117943 return;
117944 }
117945
117946 assert( pName2!=0 || pName1==0 );
117947 if( pName1==0 ){
117948 /* Form 1: Analyze everything */
117949 for(i=0; i<db->nDb; i++){
117950 if( i==1 ) continue; /* Do not analyze the TEMP database */
117951 analyzeDatabase(pParse, iDb: i);
117952 }
117953 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
117954 /* Analyze the schema named as the argument */
117955 analyzeDatabase(pParse, iDb);
117956 }else{
117957 /* Form 3: Analyze the table or index named as an argument */
117958 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
117959 if( iDb>=0 ){
117960 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
117961 z = sqlite3NameFromToken(db, pTableName);
117962 if( z ){
117963 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
117964 analyzeTable(pParse, pTab: pIdx->pTable, pOnlyIdx: pIdx);
117965 }else if( (pTab = sqlite3LocateTable(pParse, flags: 0, z, zDb))!=0 ){
117966 analyzeTable(pParse, pTab, pOnlyIdx: 0);
117967 }
117968 sqlite3DbFree(db, p: z);
117969 }
117970 }
117971 }
117972 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
117973 sqlite3VdbeAddOp0(p: v, OP_Expire);
117974 }
117975}
117976
117977/*
117978** Used to pass information from the analyzer reader through to the
117979** callback routine.
117980*/
117981typedef struct analysisInfo analysisInfo;
117982struct analysisInfo {
117983 sqlite3 *db;
117984 const char *zDatabase;
117985};
117986
117987/*
117988** The first argument points to a nul-terminated string containing a
117989** list of space separated integers. Read the first nOut of these into
117990** the array aOut[].
117991*/
117992static void decodeIntArray(
117993 char *zIntArray, /* String containing int array to decode */
117994 int nOut, /* Number of slots in aOut[] */
117995 tRowcnt *aOut, /* Store integers here */
117996 LogEst *aLog, /* Or, if aOut==0, here */
117997 Index *pIndex /* Handle extra flags for this index, if not NULL */
117998){
117999 char *z = zIntArray;
118000 int c;
118001 int i;
118002 tRowcnt v;
118003
118004#ifdef SQLITE_ENABLE_STAT4
118005 if( z==0 ) z = "";
118006#else
118007 assert( z!=0 );
118008#endif
118009 for(i=0; *z && i<nOut; i++){
118010 v = 0;
118011 while( (c=z[0])>='0' && c<='9' ){
118012 v = v*10 + c - '0';
118013 z++;
118014 }
118015#ifdef SQLITE_ENABLE_STAT4
118016 if( aOut ) aOut[i] = v;
118017 if( aLog ) aLog[i] = sqlite3LogEst(v);
118018#else
118019 assert( aOut==0 );
118020 UNUSED_PARAMETER(aOut);
118021 assert( aLog!=0 );
118022 aLog[i] = sqlite3LogEst(x: v);
118023#endif
118024 if( *z==' ' ) z++;
118025 }
118026#ifndef SQLITE_ENABLE_STAT4
118027 assert( pIndex!=0 ); {
118028#else
118029 if( pIndex ){
118030#endif
118031 pIndex->bUnordered = 0;
118032 pIndex->noSkipScan = 0;
118033 while( z[0] ){
118034 if( sqlite3_strglob(zGlob: "unordered*", zStr: z)==0 ){
118035 pIndex->bUnordered = 1;
118036 }else if( sqlite3_strglob(zGlob: "sz=[0-9]*", zStr: z)==0 ){
118037 int sz = sqlite3Atoi(z: z+3);
118038 if( sz<2 ) sz = 2;
118039 pIndex->szIdxRow = sqlite3LogEst(x: sz);
118040 }else if( sqlite3_strglob(zGlob: "noskipscan*", zStr: z)==0 ){
118041 pIndex->noSkipScan = 1;
118042 }
118043#ifdef SQLITE_ENABLE_COSTMULT
118044 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
118045 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
118046 }
118047#endif
118048 while( z[0]!=0 && z[0]!=' ' ) z++;
118049 while( z[0]==' ' ) z++;
118050 }
118051 }
118052}
118053
118054/*
118055** This callback is invoked once for each index when reading the
118056** sqlite_stat1 table.
118057**
118058** argv[0] = name of the table
118059** argv[1] = name of the index (might be NULL)
118060** argv[2] = results of analysis - on integer for each column
118061**
118062** Entries for which argv[1]==NULL simply record the number of rows in
118063** the table.
118064*/
118065static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
118066 analysisInfo *pInfo = (analysisInfo*)pData;
118067 Index *pIndex;
118068 Table *pTable;
118069 const char *z;
118070
118071 assert( argc==3 );
118072 UNUSED_PARAMETER2(NotUsed, argc);
118073
118074 if( argv==0 || argv[0]==0 || argv[2]==0 ){
118075 return 0;
118076 }
118077 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
118078 if( pTable==0 ){
118079 return 0;
118080 }
118081 if( argv[1]==0 ){
118082 pIndex = 0;
118083 }else if( sqlite3_stricmp(zLeft: argv[0],zRight: argv[1])==0 ){
118084 pIndex = sqlite3PrimaryKeyIndex(pTable);
118085 }else{
118086 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
118087 }
118088 z = argv[2];
118089
118090 if( pIndex ){
118091 tRowcnt *aiRowEst = 0;
118092 int nCol = pIndex->nKeyCol+1;
118093#ifdef SQLITE_ENABLE_STAT4
118094 /* Index.aiRowEst may already be set here if there are duplicate
118095 ** sqlite_stat1 entries for this index. In that case just clobber
118096 ** the old data with the new instead of allocating a new array. */
118097 if( pIndex->aiRowEst==0 ){
118098 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
118099 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
118100 }
118101 aiRowEst = pIndex->aiRowEst;
118102#endif
118103 pIndex->bUnordered = 0;
118104 decodeIntArray(zIntArray: (char*)z, nOut: nCol, aOut: aiRowEst, aLog: pIndex->aiRowLogEst, pIndex);
118105 pIndex->hasStat1 = 1;
118106 if( pIndex->pPartIdxWhere==0 ){
118107 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
118108 pTable->tabFlags |= TF_HasStat1;
118109 }
118110 }else{
118111 Index fakeIdx;
118112 fakeIdx.szIdxRow = pTable->szTabRow;
118113#ifdef SQLITE_ENABLE_COSTMULT
118114 fakeIdx.pTable = pTable;
118115#endif
118116 decodeIntArray(zIntArray: (char*)z, nOut: 1, aOut: 0, aLog: &pTable->nRowLogEst, pIndex: &fakeIdx);
118117 pTable->szTabRow = fakeIdx.szIdxRow;
118118 pTable->tabFlags |= TF_HasStat1;
118119 }
118120
118121 return 0;
118122}
118123
118124/*
118125** If the Index.aSample variable is not NULL, delete the aSample[] array
118126** and its contents.
118127*/
118128SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
118129 assert( db!=0 );
118130 assert( pIdx!=0 );
118131#ifdef SQLITE_ENABLE_STAT4
118132 if( pIdx->aSample ){
118133 int j;
118134 for(j=0; j<pIdx->nSample; j++){
118135 IndexSample *p = &pIdx->aSample[j];
118136 sqlite3DbFree(db, p->p);
118137 }
118138 sqlite3DbFree(db, pIdx->aSample);
118139 }
118140 if( db->pnBytesFreed==0 ){
118141 pIdx->nSample = 0;
118142 pIdx->aSample = 0;
118143 }
118144#else
118145 UNUSED_PARAMETER(db);
118146 UNUSED_PARAMETER(pIdx);
118147#endif /* SQLITE_ENABLE_STAT4 */
118148}
118149
118150#ifdef SQLITE_ENABLE_STAT4
118151/*
118152** Populate the pIdx->aAvgEq[] array based on the samples currently
118153** stored in pIdx->aSample[].
118154*/
118155static void initAvgEq(Index *pIdx){
118156 if( pIdx ){
118157 IndexSample *aSample = pIdx->aSample;
118158 IndexSample *pFinal = &aSample[pIdx->nSample-1];
118159 int iCol;
118160 int nCol = 1;
118161 if( pIdx->nSampleCol>1 ){
118162 /* If this is stat4 data, then calculate aAvgEq[] values for all
118163 ** sample columns except the last. The last is always set to 1, as
118164 ** once the trailing PK fields are considered all index keys are
118165 ** unique. */
118166 nCol = pIdx->nSampleCol-1;
118167 pIdx->aAvgEq[nCol] = 1;
118168 }
118169 for(iCol=0; iCol<nCol; iCol++){
118170 int nSample = pIdx->nSample;
118171 int i; /* Used to iterate through samples */
118172 tRowcnt sumEq = 0; /* Sum of the nEq values */
118173 tRowcnt avgEq = 0;
118174 tRowcnt nRow; /* Number of rows in index */
118175 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
118176 i64 nDist100; /* Number of distinct values in index */
118177
118178 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
118179 nRow = pFinal->anLt[iCol];
118180 nDist100 = (i64)100 * pFinal->anDLt[iCol];
118181 nSample--;
118182 }else{
118183 nRow = pIdx->aiRowEst[0];
118184 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
118185 }
118186 pIdx->nRowEst0 = nRow;
118187
118188 /* Set nSum to the number of distinct (iCol+1) field prefixes that
118189 ** occur in the stat4 table for this index. Set sumEq to the sum of
118190 ** the nEq values for column iCol for the same set (adding the value
118191 ** only once where there exist duplicate prefixes). */
118192 for(i=0; i<nSample; i++){
118193 if( i==(pIdx->nSample-1)
118194 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
118195 ){
118196 sumEq += aSample[i].anEq[iCol];
118197 nSum100 += 100;
118198 }
118199 }
118200
118201 if( nDist100>nSum100 && sumEq<nRow ){
118202 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
118203 }
118204 if( avgEq==0 ) avgEq = 1;
118205 pIdx->aAvgEq[iCol] = avgEq;
118206 }
118207 }
118208}
118209
118210/*
118211** Look up an index by name. Or, if the name of a WITHOUT ROWID table
118212** is supplied instead, find the PRIMARY KEY index for that table.
118213*/
118214static Index *findIndexOrPrimaryKey(
118215 sqlite3 *db,
118216 const char *zName,
118217 const char *zDb
118218){
118219 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
118220 if( pIdx==0 ){
118221 Table *pTab = sqlite3FindTable(db, zName, zDb);
118222 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
118223 }
118224 return pIdx;
118225}
118226
118227/*
118228** Load the content from either the sqlite_stat4
118229** into the relevant Index.aSample[] arrays.
118230**
118231** Arguments zSql1 and zSql2 must point to SQL statements that return
118232** data equivalent to the following:
118233**
118234** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
118235** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
118236**
118237** where %Q is replaced with the database name before the SQL is executed.
118238*/
118239static int loadStatTbl(
118240 sqlite3 *db, /* Database handle */
118241 const char *zSql1, /* SQL statement 1 (see above) */
118242 const char *zSql2, /* SQL statement 2 (see above) */
118243 const char *zDb /* Database name (e.g. "main") */
118244){
118245 int rc; /* Result codes from subroutines */
118246 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
118247 char *zSql; /* Text of the SQL statement */
118248 Index *pPrevIdx = 0; /* Previous index in the loop */
118249 IndexSample *pSample; /* A slot in pIdx->aSample[] */
118250
118251 assert( db->lookaside.bDisable );
118252 zSql = sqlite3MPrintf(db, zSql1, zDb);
118253 if( !zSql ){
118254 return SQLITE_NOMEM_BKPT;
118255 }
118256 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
118257 sqlite3DbFree(db, zSql);
118258 if( rc ) return rc;
118259
118260 while( sqlite3_step(pStmt)==SQLITE_ROW ){
118261 int nIdxCol = 1; /* Number of columns in stat4 records */
118262
118263 char *zIndex; /* Index name */
118264 Index *pIdx; /* Pointer to the index object */
118265 int nSample; /* Number of samples */
118266 int nByte; /* Bytes of space required */
118267 int i; /* Bytes of space required */
118268 tRowcnt *pSpace;
118269
118270 zIndex = (char *)sqlite3_column_text(pStmt, 0);
118271 if( zIndex==0 ) continue;
118272 nSample = sqlite3_column_int(pStmt, 1);
118273 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
118274 assert( pIdx==0 || pIdx->nSample==0 );
118275 if( pIdx==0 ) continue;
118276 if( pIdx->aSample!=0 ){
118277 /* The same index appears in sqlite_stat4 under multiple names */
118278 continue;
118279 }
118280 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
118281 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
118282 nIdxCol = pIdx->nKeyCol;
118283 }else{
118284 nIdxCol = pIdx->nColumn;
118285 }
118286 pIdx->nSampleCol = nIdxCol;
118287 pIdx->mxSample = nSample;
118288 nByte = sizeof(IndexSample) * nSample;
118289 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
118290 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
118291
118292 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
118293 if( pIdx->aSample==0 ){
118294 sqlite3_finalize(pStmt);
118295 return SQLITE_NOMEM_BKPT;
118296 }
118297 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
118298 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
118299 pIdx->pTable->tabFlags |= TF_HasStat4;
118300 for(i=0; i<nSample; i++){
118301 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
118302 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
118303 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
118304 }
118305 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
118306 }
118307 rc = sqlite3_finalize(pStmt);
118308 if( rc ) return rc;
118309
118310 zSql = sqlite3MPrintf(db, zSql2, zDb);
118311 if( !zSql ){
118312 return SQLITE_NOMEM_BKPT;
118313 }
118314 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
118315 sqlite3DbFree(db, zSql);
118316 if( rc ) return rc;
118317
118318 while( sqlite3_step(pStmt)==SQLITE_ROW ){
118319 char *zIndex; /* Index name */
118320 Index *pIdx; /* Pointer to the index object */
118321 int nCol = 1; /* Number of columns in index */
118322
118323 zIndex = (char *)sqlite3_column_text(pStmt, 0);
118324 if( zIndex==0 ) continue;
118325 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
118326 if( pIdx==0 ) continue;
118327 if( pIdx->nSample>=pIdx->mxSample ){
118328 /* Too many slots used because the same index appears in
118329 ** sqlite_stat4 using multiple names */
118330 continue;
118331 }
118332 /* This next condition is true if data has already been loaded from
118333 ** the sqlite_stat4 table. */
118334 nCol = pIdx->nSampleCol;
118335 if( pIdx!=pPrevIdx ){
118336 initAvgEq(pPrevIdx);
118337 pPrevIdx = pIdx;
118338 }
118339 pSample = &pIdx->aSample[pIdx->nSample];
118340 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
118341 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
118342 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
118343
118344 /* Take a copy of the sample. Add 8 extra 0x00 bytes the end of the buffer.
118345 ** This is in case the sample record is corrupted. In that case, the
118346 ** sqlite3VdbeRecordCompare() may read up to two varints past the
118347 ** end of the allocated buffer before it realizes it is dealing with
118348 ** a corrupt record. Or it might try to read a large integer from the
118349 ** buffer. In any case, eight 0x00 bytes prevents this from causing
118350 ** a buffer overread. */
118351 pSample->n = sqlite3_column_bytes(pStmt, 4);
118352 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
118353 if( pSample->p==0 ){
118354 sqlite3_finalize(pStmt);
118355 return SQLITE_NOMEM_BKPT;
118356 }
118357 if( pSample->n ){
118358 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
118359 }
118360 pIdx->nSample++;
118361 }
118362 rc = sqlite3_finalize(pStmt);
118363 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
118364 return rc;
118365}
118366
118367/*
118368** Load content from the sqlite_stat4 table into
118369** the Index.aSample[] arrays of all indices.
118370*/
118371static int loadStat4(sqlite3 *db, const char *zDb){
118372 int rc = SQLITE_OK; /* Result codes from subroutines */
118373 const Table *pStat4;
118374
118375 assert( db->lookaside.bDisable );
118376 if( OptimizationEnabled(db, SQLITE_Stat4)
118377 && (pStat4 = sqlite3FindTable(db, "sqlite_stat4", zDb))!=0
118378 && IsOrdinaryTable(pStat4)
118379 ){
118380 rc = loadStatTbl(db,
118381 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx COLLATE nocase",
118382 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
118383 zDb
118384 );
118385 }
118386 return rc;
118387}
118388#endif /* SQLITE_ENABLE_STAT4 */
118389
118390/*
118391** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
118392** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
118393** arrays. The contents of sqlite_stat4 are used to populate the
118394** Index.aSample[] arrays.
118395**
118396** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
118397** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
118398** during compilation and the sqlite_stat4 table is present, no data is
118399** read from it.
118400**
118401** If SQLITE_ENABLE_STAT4 was defined during compilation and the
118402** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
118403** returned. However, in this case, data is read from the sqlite_stat1
118404** table (if it is present) before returning.
118405**
118406** If an OOM error occurs, this function always sets db->mallocFailed.
118407** This means if the caller does not care about other errors, the return
118408** code may be ignored.
118409*/
118410SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
118411 analysisInfo sInfo;
118412 HashElem *i;
118413 char *zSql;
118414 int rc = SQLITE_OK;
118415 Schema *pSchema = db->aDb[iDb].pSchema;
118416 const Table *pStat1;
118417
118418 assert( iDb>=0 && iDb<db->nDb );
118419 assert( db->aDb[iDb].pBt!=0 );
118420
118421 /* Clear any prior statistics */
118422 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
118423 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
118424 Table *pTab = sqliteHashData(i);
118425 pTab->tabFlags &= ~TF_HasStat1;
118426 }
118427 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
118428 Index *pIdx = sqliteHashData(i);
118429 pIdx->hasStat1 = 0;
118430#ifdef SQLITE_ENABLE_STAT4
118431 sqlite3DeleteIndexSamples(db, pIdx);
118432 pIdx->aSample = 0;
118433#endif
118434 }
118435
118436 /* Load new statistics out of the sqlite_stat1 table */
118437 sInfo.db = db;
118438 sInfo.zDatabase = db->aDb[iDb].zDbSName;
118439 if( (pStat1 = sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase))
118440 && IsOrdinaryTable(pStat1)
118441 ){
118442 zSql = sqlite3MPrintf(db,
118443 zFormat: "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
118444 if( zSql==0 ){
118445 rc = SQLITE_NOMEM_BKPT;
118446 }else{
118447 rc = sqlite3_exec(db, sql: zSql, callback: analysisLoader, &sInfo, errmsg: 0);
118448 sqlite3DbFree(db, p: zSql);
118449 }
118450 }
118451
118452 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
118453 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
118454 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
118455 Index *pIdx = sqliteHashData(i);
118456 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
118457 }
118458
118459 /* Load the statistics from the sqlite_stat4 table. */
118460#ifdef SQLITE_ENABLE_STAT4
118461 if( rc==SQLITE_OK ){
118462 DisableLookaside;
118463 rc = loadStat4(db, sInfo.zDatabase);
118464 EnableLookaside;
118465 }
118466 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
118467 Index *pIdx = sqliteHashData(i);
118468 sqlite3_free(pIdx->aiRowEst);
118469 pIdx->aiRowEst = 0;
118470 }
118471#endif
118472
118473 if( rc==SQLITE_NOMEM ){
118474 sqlite3OomFault(db);
118475 }
118476 return rc;
118477}
118478
118479
118480#endif /* SQLITE_OMIT_ANALYZE */
118481
118482/************** End of analyze.c *********************************************/
118483/************** Begin file attach.c ******************************************/
118484/*
118485** 2003 April 6
118486**
118487** The author disclaims copyright to this source code. In place of
118488** a legal notice, here is a blessing:
118489**
118490** May you do good and not evil.
118491** May you find forgiveness for yourself and forgive others.
118492** May you share freely, never taking more than you give.
118493**
118494*************************************************************************
118495** This file contains code used to implement the ATTACH and DETACH commands.
118496*/
118497/* #include "sqliteInt.h" */
118498
118499#ifndef SQLITE_OMIT_ATTACH
118500/*
118501** Resolve an expression that was part of an ATTACH or DETACH statement. This
118502** is slightly different from resolving a normal SQL expression, because simple
118503** identifiers are treated as strings, not possible column names or aliases.
118504**
118505** i.e. if the parser sees:
118506**
118507** ATTACH DATABASE abc AS def
118508**
118509** it treats the two expressions as literal strings 'abc' and 'def' instead of
118510** looking for columns of the same name.
118511**
118512** This only applies to the root node of pExpr, so the statement:
118513**
118514** ATTACH DATABASE abc||def AS 'db2'
118515**
118516** will fail because neither abc or def can be resolved.
118517*/
118518static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
118519{
118520 int rc = SQLITE_OK;
118521 if( pExpr ){
118522 if( pExpr->op!=TK_ID ){
118523 rc = sqlite3ResolveExprNames(pNC: pName, pExpr);
118524 }else{
118525 pExpr->op = TK_STRING;
118526 }
118527 }
118528 return rc;
118529}
118530
118531/*
118532** Return true if zName points to a name that may be used to refer to
118533** database iDb attached to handle db.
118534*/
118535SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
118536 return (
118537 sqlite3StrICmp(zLeft: db->aDb[iDb].zDbSName, zRight: zName)==0
118538 || (iDb==0 && sqlite3StrICmp(zLeft: "main", zRight: zName)==0)
118539 );
118540}
118541
118542/*
118543** An SQL user-function registered to do the work of an ATTACH statement. The
118544** three arguments to the function come directly from an attach statement:
118545**
118546** ATTACH DATABASE x AS y KEY z
118547**
118548** SELECT sqlite_attach(x, y, z)
118549**
118550** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
118551** third argument.
118552**
118553** If the db->init.reopenMemdb flags is set, then instead of attaching a
118554** new database, close the database on db->init.iDb and reopen it as an
118555** empty MemDB.
118556*/
118557static void attachFunc(
118558 sqlite3_context *context,
118559 int NotUsed,
118560 sqlite3_value **argv
118561){
118562 int i;
118563 int rc = 0;
118564 sqlite3 *db = sqlite3_context_db_handle(p: context);
118565 const char *zName;
118566 const char *zFile;
118567 char *zPath = 0;
118568 char *zErr = 0;
118569 unsigned int flags;
118570 Db *aNew; /* New array of Db pointers */
118571 Db *pNew = 0; /* Db object for the newly attached database */
118572 char *zErrDyn = 0;
118573 sqlite3_vfs *pVfs;
118574
118575 UNUSED_PARAMETER(NotUsed);
118576 zFile = (const char *)sqlite3_value_text(pVal: argv[0]);
118577 zName = (const char *)sqlite3_value_text(pVal: argv[1]);
118578 if( zFile==0 ) zFile = "";
118579 if( zName==0 ) zName = "";
118580
118581#ifndef SQLITE_OMIT_DESERIALIZE
118582# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
118583#else
118584# define REOPEN_AS_MEMDB(db) (0)
118585#endif
118586
118587 if( REOPEN_AS_MEMDB(db) ){
118588 /* This is not a real ATTACH. Instead, this routine is being called
118589 ** from sqlite3_deserialize() to close database db->init.iDb and
118590 ** reopen it as a MemDB */
118591 Btree *pNewBt = 0;
118592 pVfs = sqlite3_vfs_find(zVfs: "memdb");
118593 if( pVfs==0 ) return;
118594 rc = sqlite3BtreeOpen(pVfs, zFilename: "x\0", db, ppBtree: &pNewBt, flags: 0, SQLITE_OPEN_MAIN_DB);
118595 if( rc==SQLITE_OK ){
118596 Schema *pNewSchema = sqlite3SchemaGet(db, pNewBt);
118597 if( pNewSchema ){
118598 /* Both the Btree and the new Schema were allocated successfully.
118599 ** Close the old db and update the aDb[] slot with the new memdb
118600 ** values. */
118601 pNew = &db->aDb[db->init.iDb];
118602 if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(p: pNew->pBt);
118603 pNew->pBt = pNewBt;
118604 pNew->pSchema = pNewSchema;
118605 }else{
118606 sqlite3BtreeClose(p: pNewBt);
118607 rc = SQLITE_NOMEM;
118608 }
118609 }
118610 if( rc ) goto attach_error;
118611 }else{
118612 /* This is a real ATTACH
118613 **
118614 ** Check for the following errors:
118615 **
118616 ** * Too many attached databases,
118617 ** * Transaction currently open
118618 ** * Specified database name already being used.
118619 */
118620 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
118621 zErrDyn = sqlite3MPrintf(db, zFormat: "too many attached databases - max %d",
118622 db->aLimit[SQLITE_LIMIT_ATTACHED]
118623 );
118624 goto attach_error;
118625 }
118626 for(i=0; i<db->nDb; i++){
118627 assert( zName );
118628 if( sqlite3DbIsNamed(db, iDb: i, zName) ){
118629 zErrDyn = sqlite3MPrintf(db, zFormat: "database %s is already in use", zName);
118630 goto attach_error;
118631 }
118632 }
118633
118634 /* Allocate the new entry in the db->aDb[] array and initialize the schema
118635 ** hash tables.
118636 */
118637 if( db->aDb==db->aDbStatic ){
118638 aNew = sqlite3DbMallocRawNN(db, n: sizeof(db->aDb[0])*3 );
118639 if( aNew==0 ) return;
118640 memcpy(dest: aNew, src: db->aDb, n: sizeof(db->aDb[0])*2);
118641 }else{
118642 aNew = sqlite3DbRealloc(db, p: db->aDb, n: sizeof(db->aDb[0])*(db->nDb+1) );
118643 if( aNew==0 ) return;
118644 }
118645 db->aDb = aNew;
118646 pNew = &db->aDb[db->nDb];
118647 memset(s: pNew, c: 0, n: sizeof(*pNew));
118648
118649 /* Open the database file. If the btree is successfully opened, use
118650 ** it to obtain the database schema. At this point the schema may
118651 ** or may not be initialized.
118652 */
118653 flags = db->openFlags;
118654 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
118655 if( rc!=SQLITE_OK ){
118656 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
118657 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
118658 sqlite3_free(p: zErr);
118659 return;
118660 }
118661 assert( pVfs );
118662 flags |= SQLITE_OPEN_MAIN_DB;
118663 rc = sqlite3BtreeOpen(pVfs, zFilename: zPath, db, ppBtree: &pNew->pBt, flags: 0, vfsFlags: flags);
118664 db->nDb++;
118665 pNew->zDbSName = sqlite3DbStrDup(db, z: zName);
118666 }
118667 db->noSharedCache = 0;
118668 if( rc==SQLITE_CONSTRAINT ){
118669 rc = SQLITE_ERROR;
118670 zErrDyn = sqlite3MPrintf(db, zFormat: "database is already attached");
118671 }else if( rc==SQLITE_OK ){
118672 Pager *pPager;
118673 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
118674 if( !pNew->pSchema ){
118675 rc = SQLITE_NOMEM_BKPT;
118676 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
118677 zErrDyn = sqlite3MPrintf(db,
118678 zFormat: "attached databases must use the same text encoding as main database");
118679 rc = SQLITE_ERROR;
118680 }
118681 sqlite3BtreeEnter(p: pNew->pBt);
118682 pPager = sqlite3BtreePager(p: pNew->pBt);
118683 sqlite3PagerLockingMode(pPager, eMode: db->dfltLockMode);
118684 sqlite3BtreeSecureDelete(p: pNew->pBt,
118685 newFlag: sqlite3BtreeSecureDelete(p: db->aDb[0].pBt,newFlag: -1) );
118686#ifndef SQLITE_OMIT_PAGER_PRAGMAS
118687 sqlite3BtreeSetPagerFlags(p: pNew->pBt,
118688 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
118689#endif
118690 sqlite3BtreeLeave(p: pNew->pBt);
118691 }
118692 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
118693 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
118694 rc = SQLITE_NOMEM_BKPT;
118695 }
118696 sqlite3_free_filename( zPath );
118697
118698 /* If the file was opened successfully, read the schema for the new database.
118699 ** If this fails, or if opening the file failed, then close the file and
118700 ** remove the entry from the db->aDb[] array. i.e. put everything back the
118701 ** way we found it.
118702 */
118703 if( rc==SQLITE_OK ){
118704 sqlite3BtreeEnterAll(db);
118705 db->init.iDb = 0;
118706 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
118707 if( !REOPEN_AS_MEMDB(db) ){
118708 rc = sqlite3Init(db, &zErrDyn);
118709 }
118710 sqlite3BtreeLeaveAll(db);
118711 assert( zErrDyn==0 || rc!=SQLITE_OK );
118712 }
118713#ifdef SQLITE_USER_AUTHENTICATION
118714 if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
118715 u8 newAuth = 0;
118716 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
118717 if( newAuth<db->auth.authLevel ){
118718 rc = SQLITE_AUTH_USER;
118719 }
118720 }
118721#endif
118722 if( rc ){
118723 if( ALWAYS(!REOPEN_AS_MEMDB(db)) ){
118724 int iDb = db->nDb - 1;
118725 assert( iDb>=2 );
118726 if( db->aDb[iDb].pBt ){
118727 sqlite3BtreeClose(p: db->aDb[iDb].pBt);
118728 db->aDb[iDb].pBt = 0;
118729 db->aDb[iDb].pSchema = 0;
118730 }
118731 sqlite3ResetAllSchemasOfConnection(db);
118732 db->nDb = iDb;
118733 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
118734 sqlite3OomFault(db);
118735 sqlite3DbFree(db, p: zErrDyn);
118736 zErrDyn = sqlite3MPrintf(db, zFormat: "out of memory");
118737 }else if( zErrDyn==0 ){
118738 zErrDyn = sqlite3MPrintf(db, zFormat: "unable to open database: %s", zFile);
118739 }
118740 }
118741 goto attach_error;
118742 }
118743
118744 return;
118745
118746attach_error:
118747 /* Return an error if we get here */
118748 if( zErrDyn ){
118749 sqlite3_result_error(pCtx: context, z: zErrDyn, n: -1);
118750 sqlite3DbFree(db, p: zErrDyn);
118751 }
118752 if( rc ) sqlite3_result_error_code(pCtx: context, errCode: rc);
118753}
118754
118755/*
118756** An SQL user-function registered to do the work of an DETACH statement. The
118757** three arguments to the function come directly from a detach statement:
118758**
118759** DETACH DATABASE x
118760**
118761** SELECT sqlite_detach(x)
118762*/
118763static void detachFunc(
118764 sqlite3_context *context,
118765 int NotUsed,
118766 sqlite3_value **argv
118767){
118768 const char *zName = (const char *)sqlite3_value_text(pVal: argv[0]);
118769 sqlite3 *db = sqlite3_context_db_handle(p: context);
118770 int i;
118771 Db *pDb = 0;
118772 HashElem *pEntry;
118773 char zErr[128];
118774
118775 UNUSED_PARAMETER(NotUsed);
118776
118777 if( zName==0 ) zName = "";
118778 for(i=0; i<db->nDb; i++){
118779 pDb = &db->aDb[i];
118780 if( pDb->pBt==0 ) continue;
118781 if( sqlite3DbIsNamed(db, iDb: i, zName) ) break;
118782 }
118783
118784 if( i>=db->nDb ){
118785 sqlite3_snprintf(n: sizeof(zErr),zBuf: zErr, zFormat: "no such database: %s", zName);
118786 goto detach_error;
118787 }
118788 if( i<2 ){
118789 sqlite3_snprintf(n: sizeof(zErr),zBuf: zErr, zFormat: "cannot detach database %s", zName);
118790 goto detach_error;
118791 }
118792 if( sqlite3BtreeTxnState(p: pDb->pBt)!=SQLITE_TXN_NONE
118793 || sqlite3BtreeIsInBackup(p: pDb->pBt)
118794 ){
118795 sqlite3_snprintf(n: sizeof(zErr),zBuf: zErr, zFormat: "database %s is locked", zName);
118796 goto detach_error;
118797 }
118798
118799 /* If any TEMP triggers reference the schema being detached, move those
118800 ** triggers to reference the TEMP schema itself. */
118801 assert( db->aDb[1].pSchema );
118802 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
118803 while( pEntry ){
118804 Trigger *pTrig = (Trigger*)sqliteHashData(pEntry);
118805 if( pTrig->pTabSchema==pDb->pSchema ){
118806 pTrig->pTabSchema = pTrig->pSchema;
118807 }
118808 pEntry = sqliteHashNext(pEntry);
118809 }
118810
118811 sqlite3BtreeClose(p: pDb->pBt);
118812 pDb->pBt = 0;
118813 pDb->pSchema = 0;
118814 sqlite3CollapseDatabaseArray(db);
118815 return;
118816
118817detach_error:
118818 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
118819}
118820
118821/*
118822** This procedure generates VDBE code for a single invocation of either the
118823** sqlite_detach() or sqlite_attach() SQL user functions.
118824*/
118825static void codeAttach(
118826 Parse *pParse, /* The parser context */
118827 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
118828 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
118829 Expr *pAuthArg, /* Expression to pass to authorization callback */
118830 Expr *pFilename, /* Name of database file */
118831 Expr *pDbname, /* Name of the database to use internally */
118832 Expr *pKey /* Database key for encryption extension */
118833){
118834 int rc;
118835 NameContext sName;
118836 Vdbe *v;
118837 sqlite3* db = pParse->db;
118838 int regArgs;
118839
118840 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto attach_end;
118841
118842 if( pParse->nErr ) goto attach_end;
118843 memset(s: &sName, c: 0, n: sizeof(NameContext));
118844 sName.pParse = pParse;
118845
118846 if(
118847 SQLITE_OK!=resolveAttachExpr(pName: &sName, pExpr: pFilename) ||
118848 SQLITE_OK!=resolveAttachExpr(pName: &sName, pExpr: pDbname) ||
118849 SQLITE_OK!=resolveAttachExpr(pName: &sName, pExpr: pKey)
118850 ){
118851 goto attach_end;
118852 }
118853
118854#ifndef SQLITE_OMIT_AUTHORIZATION
118855 if( ALWAYS(pAuthArg) ){
118856 char *zAuthArg;
118857 if( pAuthArg->op==TK_STRING ){
118858 assert( !ExprHasProperty(pAuthArg, EP_IntValue) );
118859 zAuthArg = pAuthArg->u.zToken;
118860 }else{
118861 zAuthArg = 0;
118862 }
118863 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
118864 if(rc!=SQLITE_OK ){
118865 goto attach_end;
118866 }
118867 }
118868#endif /* SQLITE_OMIT_AUTHORIZATION */
118869
118870
118871 v = sqlite3GetVdbe(pParse);
118872 regArgs = sqlite3GetTempRange(pParse, nReg: 4);
118873 sqlite3ExprCode(pParse, pExpr: pFilename, target: regArgs);
118874 sqlite3ExprCode(pParse, pExpr: pDbname, target: regArgs+1);
118875 sqlite3ExprCode(pParse, pExpr: pKey, target: regArgs+2);
118876
118877 assert( v || db->mallocFailed );
118878 if( v ){
118879 sqlite3VdbeAddFunctionCall(pParse, p1: 0, p2: regArgs+3-pFunc->nArg, p3: regArgs+3,
118880 nArg: pFunc->nArg, pFunc, eCallCtx: 0);
118881 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
118882 ** statement only). For DETACH, set it to false (expire all existing
118883 ** statements).
118884 */
118885 sqlite3VdbeAddOp1(p: v, OP_Expire, p1: (type==SQLITE_ATTACH));
118886 }
118887
118888attach_end:
118889 sqlite3ExprDelete(db, p: pFilename);
118890 sqlite3ExprDelete(db, p: pDbname);
118891 sqlite3ExprDelete(db, p: pKey);
118892}
118893
118894/*
118895** Called by the parser to compile a DETACH statement.
118896**
118897** DETACH pDbname
118898*/
118899SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
118900 static const FuncDef detach_func = {
118901 1, /* nArg */
118902 SQLITE_UTF8, /* funcFlags */
118903 0, /* pUserData */
118904 0, /* pNext */
118905 detachFunc, /* xSFunc */
118906 0, /* xFinalize */
118907 0, 0, /* xValue, xInverse */
118908 "sqlite_detach", /* zName */
118909 {0}
118910 };
118911 codeAttach(pParse, SQLITE_DETACH, pFunc: &detach_func, pAuthArg: pDbname, pFilename: 0, pDbname: 0, pKey: pDbname);
118912}
118913
118914/*
118915** Called by the parser to compile an ATTACH statement.
118916**
118917** ATTACH p AS pDbname KEY pKey
118918*/
118919SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
118920 static const FuncDef attach_func = {
118921 3, /* nArg */
118922 SQLITE_UTF8, /* funcFlags */
118923 0, /* pUserData */
118924 0, /* pNext */
118925 attachFunc, /* xSFunc */
118926 0, /* xFinalize */
118927 0, 0, /* xValue, xInverse */
118928 "sqlite_attach", /* zName */
118929 {0}
118930 };
118931 codeAttach(pParse, SQLITE_ATTACH, pFunc: &attach_func, pAuthArg: p, pFilename: p, pDbname, pKey);
118932}
118933#endif /* SQLITE_OMIT_ATTACH */
118934
118935/*
118936** Expression callback used by sqlite3FixAAAA() routines.
118937*/
118938static int fixExprCb(Walker *p, Expr *pExpr){
118939 DbFixer *pFix = p->u.pFix;
118940 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
118941 if( pExpr->op==TK_VARIABLE ){
118942 if( pFix->pParse->db->init.busy ){
118943 pExpr->op = TK_NULL;
118944 }else{
118945 sqlite3ErrorMsg(pParse: pFix->pParse, zFormat: "%s cannot use variables", pFix->zType);
118946 return WRC_Abort;
118947 }
118948 }
118949 return WRC_Continue;
118950}
118951
118952/*
118953** Select callback used by sqlite3FixAAAA() routines.
118954*/
118955static int fixSelectCb(Walker *p, Select *pSelect){
118956 DbFixer *pFix = p->u.pFix;
118957 int i;
118958 SrcItem *pItem;
118959 sqlite3 *db = pFix->pParse->db;
118960 int iDb = sqlite3FindDbName(db, pFix->zDb);
118961 SrcList *pList = pSelect->pSrc;
118962
118963 if( NEVER(pList==0) ) return WRC_Continue;
118964 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
118965 if( pFix->bTemp==0 ){
118966 if( pItem->zDatabase ){
118967 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
118968 sqlite3ErrorMsg(pParse: pFix->pParse,
118969 zFormat: "%s %T cannot reference objects in database %s",
118970 pFix->zType, pFix->pName, pItem->zDatabase);
118971 return WRC_Abort;
118972 }
118973 sqlite3DbFree(db, p: pItem->zDatabase);
118974 pItem->zDatabase = 0;
118975 pItem->fg.notCte = 1;
118976 }
118977 pItem->pSchema = pFix->pSchema;
118978 pItem->fg.fromDDL = 1;
118979 }
118980#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
118981 if( pList->a[i].fg.isUsing==0
118982 && sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pList->a[i].u3.pOn)
118983 ){
118984 return WRC_Abort;
118985 }
118986#endif
118987 }
118988 if( pSelect->pWith ){
118989 for(i=0; i<pSelect->pWith->nCte; i++){
118990 if( sqlite3WalkSelect(pWalker: p, p: pSelect->pWith->a[i].pSelect) ){
118991 return WRC_Abort;
118992 }
118993 }
118994 }
118995 return WRC_Continue;
118996}
118997
118998/*
118999** Initialize a DbFixer structure. This routine must be called prior
119000** to passing the structure to one of the sqliteFixAAAA() routines below.
119001*/
119002SQLITE_PRIVATE void sqlite3FixInit(
119003 DbFixer *pFix, /* The fixer to be initialized */
119004 Parse *pParse, /* Error messages will be written here */
119005 int iDb, /* This is the database that must be used */
119006 const char *zType, /* "view", "trigger", or "index" */
119007 const Token *pName /* Name of the view, trigger, or index */
119008){
119009 sqlite3 *db = pParse->db;
119010 assert( db->nDb>iDb );
119011 pFix->pParse = pParse;
119012 pFix->zDb = db->aDb[iDb].zDbSName;
119013 pFix->pSchema = db->aDb[iDb].pSchema;
119014 pFix->zType = zType;
119015 pFix->pName = pName;
119016 pFix->bTemp = (iDb==1);
119017 pFix->w.pParse = pParse;
119018 pFix->w.xExprCallback = fixExprCb;
119019 pFix->w.xSelectCallback = fixSelectCb;
119020 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
119021 pFix->w.walkerDepth = 0;
119022 pFix->w.eCode = 0;
119023 pFix->w.u.pFix = pFix;
119024}
119025
119026/*
119027** The following set of routines walk through the parse tree and assign
119028** a specific database to all table references where the database name
119029** was left unspecified in the original SQL statement. The pFix structure
119030** must have been initialized by a prior call to sqlite3FixInit().
119031**
119032** These routines are used to make sure that an index, trigger, or
119033** view in one database does not refer to objects in a different database.
119034** (Exception: indices, triggers, and views in the TEMP database are
119035** allowed to refer to anything.) If a reference is explicitly made
119036** to an object in a different database, an error message is added to
119037** pParse->zErrMsg and these routines return non-zero. If everything
119038** checks out, these routines return 0.
119039*/
119040SQLITE_PRIVATE int sqlite3FixSrcList(
119041 DbFixer *pFix, /* Context of the fixation */
119042 SrcList *pList /* The Source list to check and modify */
119043){
119044 int res = 0;
119045 if( pList ){
119046 Select s;
119047 memset(s: &s, c: 0, n: sizeof(s));
119048 s.pSrc = pList;
119049 res = sqlite3WalkSelect(pWalker: &pFix->w, p: &s);
119050 }
119051 return res;
119052}
119053#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
119054SQLITE_PRIVATE int sqlite3FixSelect(
119055 DbFixer *pFix, /* Context of the fixation */
119056 Select *pSelect /* The SELECT statement to be fixed to one database */
119057){
119058 return sqlite3WalkSelect(pWalker: &pFix->w, p: pSelect);
119059}
119060SQLITE_PRIVATE int sqlite3FixExpr(
119061 DbFixer *pFix, /* Context of the fixation */
119062 Expr *pExpr /* The expression to be fixed to one database */
119063){
119064 return sqlite3WalkExpr(pWalker: &pFix->w, pExpr);
119065}
119066#endif
119067
119068#ifndef SQLITE_OMIT_TRIGGER
119069SQLITE_PRIVATE int sqlite3FixTriggerStep(
119070 DbFixer *pFix, /* Context of the fixation */
119071 TriggerStep *pStep /* The trigger step be fixed to one database */
119072){
119073 while( pStep ){
119074 if( sqlite3WalkSelect(pWalker: &pFix->w, p: pStep->pSelect)
119075 || sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pStep->pWhere)
119076 || sqlite3WalkExprList(pWalker: &pFix->w, p: pStep->pExprList)
119077 || sqlite3FixSrcList(pFix, pList: pStep->pFrom)
119078 ){
119079 return 1;
119080 }
119081#ifndef SQLITE_OMIT_UPSERT
119082 {
119083 Upsert *pUp;
119084 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
119085 if( sqlite3WalkExprList(pWalker: &pFix->w, p: pUp->pUpsertTarget)
119086 || sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pUp->pUpsertTargetWhere)
119087 || sqlite3WalkExprList(pWalker: &pFix->w, p: pUp->pUpsertSet)
119088 || sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pUp->pUpsertWhere)
119089 ){
119090 return 1;
119091 }
119092 }
119093 }
119094#endif
119095 pStep = pStep->pNext;
119096 }
119097
119098 return 0;
119099}
119100#endif
119101
119102/************** End of attach.c **********************************************/
119103/************** Begin file auth.c ********************************************/
119104/*
119105** 2003 January 11
119106**
119107** The author disclaims copyright to this source code. In place of
119108** a legal notice, here is a blessing:
119109**
119110** May you do good and not evil.
119111** May you find forgiveness for yourself and forgive others.
119112** May you share freely, never taking more than you give.
119113**
119114*************************************************************************
119115** This file contains code used to implement the sqlite3_set_authorizer()
119116** API. This facility is an optional feature of the library. Embedded
119117** systems that do not need this facility may omit it by recompiling
119118** the library with -DSQLITE_OMIT_AUTHORIZATION=1
119119*/
119120/* #include "sqliteInt.h" */
119121
119122/*
119123** All of the code in this file may be omitted by defining a single
119124** macro.
119125*/
119126#ifndef SQLITE_OMIT_AUTHORIZATION
119127
119128/*
119129** Set or clear the access authorization function.
119130**
119131** The access authorization function is be called during the compilation
119132** phase to verify that the user has read and/or write access permission on
119133** various fields of the database. The first argument to the auth function
119134** is a copy of the 3rd argument to this routine. The second argument
119135** to the auth function is one of these constants:
119136**
119137** SQLITE_CREATE_INDEX
119138** SQLITE_CREATE_TABLE
119139** SQLITE_CREATE_TEMP_INDEX
119140** SQLITE_CREATE_TEMP_TABLE
119141** SQLITE_CREATE_TEMP_TRIGGER
119142** SQLITE_CREATE_TEMP_VIEW
119143** SQLITE_CREATE_TRIGGER
119144** SQLITE_CREATE_VIEW
119145** SQLITE_DELETE
119146** SQLITE_DROP_INDEX
119147** SQLITE_DROP_TABLE
119148** SQLITE_DROP_TEMP_INDEX
119149** SQLITE_DROP_TEMP_TABLE
119150** SQLITE_DROP_TEMP_TRIGGER
119151** SQLITE_DROP_TEMP_VIEW
119152** SQLITE_DROP_TRIGGER
119153** SQLITE_DROP_VIEW
119154** SQLITE_INSERT
119155** SQLITE_PRAGMA
119156** SQLITE_READ
119157** SQLITE_SELECT
119158** SQLITE_TRANSACTION
119159** SQLITE_UPDATE
119160**
119161** The third and fourth arguments to the auth function are the name of
119162** the table and the column that are being accessed. The auth function
119163** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
119164** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
119165** means that the SQL statement will never-run - the sqlite3_exec() call
119166** will return with an error. SQLITE_IGNORE means that the SQL statement
119167** should run but attempts to read the specified column will return NULL
119168** and attempts to write the column will be ignored.
119169**
119170** Setting the auth function to NULL disables this hook. The default
119171** setting of the auth function is NULL.
119172*/
119173SQLITE_API int sqlite3_set_authorizer(
119174 sqlite3 *db,
119175 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
119176 void *pArg
119177){
119178#ifdef SQLITE_ENABLE_API_ARMOR
119179 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
119180#endif
119181 sqlite3_mutex_enter(p: db->mutex);
119182 db->xAuth = (sqlite3_xauth)xAuth;
119183 db->pAuthArg = pArg;
119184 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, iCode: 1);
119185 sqlite3_mutex_leave(p: db->mutex);
119186 return SQLITE_OK;
119187}
119188
119189/*
119190** Write an error message into pParse->zErrMsg that explains that the
119191** user-supplied authorization function returned an illegal value.
119192*/
119193static void sqliteAuthBadReturnCode(Parse *pParse){
119194 sqlite3ErrorMsg(pParse, zFormat: "authorizer malfunction");
119195 pParse->rc = SQLITE_ERROR;
119196}
119197
119198/*
119199** Invoke the authorization callback for permission to read column zCol from
119200** table zTab in database zDb. This function assumes that an authorization
119201** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
119202**
119203** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
119204** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
119205** is treated as SQLITE_DENY. In this case an error is left in pParse.
119206*/
119207SQLITE_PRIVATE int sqlite3AuthReadCol(
119208 Parse *pParse, /* The parser context */
119209 const char *zTab, /* Table name */
119210 const char *zCol, /* Column name */
119211 int iDb /* Index of containing database. */
119212){
119213 sqlite3 *db = pParse->db; /* Database handle */
119214 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
119215 int rc; /* Auth callback return code */
119216
119217 if( db->init.busy ) return SQLITE_OK;
119218 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
119219#ifdef SQLITE_USER_AUTHENTICATION
119220 ,db->auth.zAuthUser
119221#endif
119222 );
119223 if( rc==SQLITE_DENY ){
119224 char *z = sqlite3_mprintf(zFormat: "%s.%s", zTab, zCol);
119225 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf(zFormat: "%s.%z", zDb, z);
119226 sqlite3ErrorMsg(pParse, zFormat: "access to %z is prohibited", z);
119227 pParse->rc = SQLITE_AUTH;
119228 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
119229 sqliteAuthBadReturnCode(pParse);
119230 }
119231 return rc;
119232}
119233
119234/*
119235** The pExpr should be a TK_COLUMN expression. The table referred to
119236** is in pTabList or else it is the NEW or OLD table of a trigger.
119237** Check to see if it is OK to read this particular column.
119238**
119239** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
119240** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
119241** then generate an error.
119242*/
119243SQLITE_PRIVATE void sqlite3AuthRead(
119244 Parse *pParse, /* The parser context */
119245 Expr *pExpr, /* The expression to check authorization on */
119246 Schema *pSchema, /* The schema of the expression */
119247 SrcList *pTabList /* All table that pExpr might refer to */
119248){
119249 Table *pTab = 0; /* The table being read */
119250 const char *zCol; /* Name of the column of the table */
119251 int iSrc; /* Index in pTabList->a[] of table being read */
119252 int iDb; /* The index of the database the expression refers to */
119253 int iCol; /* Index of column in table */
119254
119255 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
119256 assert( !IN_RENAME_OBJECT );
119257 assert( pParse->db->xAuth!=0 );
119258 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema);
119259 if( iDb<0 ){
119260 /* An attempt to read a column out of a subquery or other
119261 ** temporary table. */
119262 return;
119263 }
119264
119265 if( pExpr->op==TK_TRIGGER ){
119266 pTab = pParse->pTriggerTab;
119267 }else{
119268 assert( pTabList );
119269 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
119270 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
119271 pTab = pTabList->a[iSrc].pTab;
119272 break;
119273 }
119274 }
119275 }
119276 iCol = pExpr->iColumn;
119277 if( pTab==0 ) return;
119278
119279 if( iCol>=0 ){
119280 assert( iCol<pTab->nCol );
119281 zCol = pTab->aCol[iCol].zCnName;
119282 }else if( pTab->iPKey>=0 ){
119283 assert( pTab->iPKey<pTab->nCol );
119284 zCol = pTab->aCol[pTab->iPKey].zCnName;
119285 }else{
119286 zCol = "ROWID";
119287 }
119288 assert( iDb>=0 && iDb<pParse->db->nDb );
119289 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, zTab: pTab->zName, zCol, iDb) ){
119290 pExpr->op = TK_NULL;
119291 }
119292}
119293
119294/*
119295** Do an authorization check using the code and arguments given. Return
119296** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
119297** is returned, then the error count and error message in pParse are
119298** modified appropriately.
119299*/
119300SQLITE_PRIVATE int sqlite3AuthCheck(
119301 Parse *pParse,
119302 int code,
119303 const char *zArg1,
119304 const char *zArg2,
119305 const char *zArg3
119306){
119307 sqlite3 *db = pParse->db;
119308 int rc;
119309
119310 /* Don't do any authorization checks if the database is initializing
119311 ** or if the parser is being invoked from within sqlite3_declare_vtab.
119312 */
119313 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
119314 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
119315 return SQLITE_OK;
119316 }
119317
119318 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
119319 ** callback are either NULL pointers or zero-terminated strings that
119320 ** contain additional details about the action to be authorized.
119321 **
119322 ** The following testcase() macros show that any of the 3rd through 6th
119323 ** parameters can be either NULL or a string. */
119324 testcase( zArg1==0 );
119325 testcase( zArg2==0 );
119326 testcase( zArg3==0 );
119327 testcase( pParse->zAuthContext==0 );
119328
119329 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
119330#ifdef SQLITE_USER_AUTHENTICATION
119331 ,db->auth.zAuthUser
119332#endif
119333 );
119334 if( rc==SQLITE_DENY ){
119335 sqlite3ErrorMsg(pParse, zFormat: "not authorized");
119336 pParse->rc = SQLITE_AUTH;
119337 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
119338 rc = SQLITE_DENY;
119339 sqliteAuthBadReturnCode(pParse);
119340 }
119341 return rc;
119342}
119343
119344/*
119345** Push an authorization context. After this routine is called, the
119346** zArg3 argument to authorization callbacks will be zContext until
119347** popped. Or if pParse==0, this routine is a no-op.
119348*/
119349SQLITE_PRIVATE void sqlite3AuthContextPush(
119350 Parse *pParse,
119351 AuthContext *pContext,
119352 const char *zContext
119353){
119354 assert( pParse );
119355 pContext->pParse = pParse;
119356 pContext->zAuthContext = pParse->zAuthContext;
119357 pParse->zAuthContext = zContext;
119358}
119359
119360/*
119361** Pop an authorization context that was previously pushed
119362** by sqlite3AuthContextPush
119363*/
119364SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
119365 if( pContext->pParse ){
119366 pContext->pParse->zAuthContext = pContext->zAuthContext;
119367 pContext->pParse = 0;
119368 }
119369}
119370
119371#endif /* SQLITE_OMIT_AUTHORIZATION */
119372
119373/************** End of auth.c ************************************************/
119374/************** Begin file build.c *******************************************/
119375/*
119376** 2001 September 15
119377**
119378** The author disclaims copyright to this source code. In place of
119379** a legal notice, here is a blessing:
119380**
119381** May you do good and not evil.
119382** May you find forgiveness for yourself and forgive others.
119383** May you share freely, never taking more than you give.
119384**
119385*************************************************************************
119386** This file contains C code routines that are called by the SQLite parser
119387** when syntax rules are reduced. The routines in this file handle the
119388** following kinds of SQL syntax:
119389**
119390** CREATE TABLE
119391** DROP TABLE
119392** CREATE INDEX
119393** DROP INDEX
119394** creating ID lists
119395** BEGIN TRANSACTION
119396** COMMIT
119397** ROLLBACK
119398*/
119399/* #include "sqliteInt.h" */
119400
119401#ifndef SQLITE_OMIT_SHARED_CACHE
119402/*
119403** The TableLock structure is only used by the sqlite3TableLock() and
119404** codeTableLocks() functions.
119405*/
119406struct TableLock {
119407 int iDb; /* The database containing the table to be locked */
119408 Pgno iTab; /* The root page of the table to be locked */
119409 u8 isWriteLock; /* True for write lock. False for a read lock */
119410 const char *zLockName; /* Name of the table */
119411};
119412
119413/*
119414** Record the fact that we want to lock a table at run-time.
119415**
119416** The table to be locked has root page iTab and is found in database iDb.
119417** A read or a write lock can be taken depending on isWritelock.
119418**
119419** This routine just records the fact that the lock is desired. The
119420** code to make the lock occur is generated by a later call to
119421** codeTableLocks() which occurs during sqlite3FinishCoding().
119422*/
119423static SQLITE_NOINLINE void lockTable(
119424 Parse *pParse, /* Parsing context */
119425 int iDb, /* Index of the database containing the table to lock */
119426 Pgno iTab, /* Root page number of the table to be locked */
119427 u8 isWriteLock, /* True for a write lock */
119428 const char *zName /* Name of the table to be locked */
119429){
119430 Parse *pToplevel;
119431 int i;
119432 int nBytes;
119433 TableLock *p;
119434 assert( iDb>=0 );
119435
119436 pToplevel = sqlite3ParseToplevel(pParse);
119437 for(i=0; i<pToplevel->nTableLock; i++){
119438 p = &pToplevel->aTableLock[i];
119439 if( p->iDb==iDb && p->iTab==iTab ){
119440 p->isWriteLock = (p->isWriteLock || isWriteLock);
119441 return;
119442 }
119443 }
119444
119445 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
119446 pToplevel->aTableLock =
119447 sqlite3DbReallocOrFree(db: pToplevel->db, p: pToplevel->aTableLock, n: nBytes);
119448 if( pToplevel->aTableLock ){
119449 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
119450 p->iDb = iDb;
119451 p->iTab = iTab;
119452 p->isWriteLock = isWriteLock;
119453 p->zLockName = zName;
119454 }else{
119455 pToplevel->nTableLock = 0;
119456 sqlite3OomFault(db: pToplevel->db);
119457 }
119458}
119459SQLITE_PRIVATE void sqlite3TableLock(
119460 Parse *pParse, /* Parsing context */
119461 int iDb, /* Index of the database containing the table to lock */
119462 Pgno iTab, /* Root page number of the table to be locked */
119463 u8 isWriteLock, /* True for a write lock */
119464 const char *zName /* Name of the table to be locked */
119465){
119466 if( iDb==1 ) return;
119467 if( !sqlite3BtreeSharable(p: pParse->db->aDb[iDb].pBt) ) return;
119468 lockTable(pParse, iDb, iTab, isWriteLock, zName);
119469}
119470
119471/*
119472** Code an OP_TableLock instruction for each table locked by the
119473** statement (configured by calls to sqlite3TableLock()).
119474*/
119475static void codeTableLocks(Parse *pParse){
119476 int i;
119477 Vdbe *pVdbe = pParse->pVdbe;
119478 assert( pVdbe!=0 );
119479
119480 for(i=0; i<pParse->nTableLock; i++){
119481 TableLock *p = &pParse->aTableLock[i];
119482 int p1 = p->iDb;
119483 sqlite3VdbeAddOp4(p: pVdbe, OP_TableLock, p1, p2: p->iTab, p3: p->isWriteLock,
119484 zP4: p->zLockName, P4_STATIC);
119485 }
119486}
119487#else
119488 #define codeTableLocks(x)
119489#endif
119490
119491/*
119492** Return TRUE if the given yDbMask object is empty - if it contains no
119493** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
119494** macros when SQLITE_MAX_ATTACHED is greater than 30.
119495*/
119496#if SQLITE_MAX_ATTACHED>30
119497SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
119498 int i;
119499 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
119500 return 1;
119501}
119502#endif
119503
119504/*
119505** This routine is called after a single SQL statement has been
119506** parsed and a VDBE program to execute that statement has been
119507** prepared. This routine puts the finishing touches on the
119508** VDBE program and resets the pParse structure for the next
119509** parse.
119510**
119511** Note that if an error occurred, it might be the case that
119512** no VDBE code was generated.
119513*/
119514SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
119515 sqlite3 *db;
119516 Vdbe *v;
119517 int iDb, i;
119518
119519 assert( pParse->pToplevel==0 );
119520 db = pParse->db;
119521 assert( db->pParse==pParse );
119522 if( pParse->nested ) return;
119523 if( pParse->nErr ){
119524 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
119525 return;
119526 }
119527 assert( db->mallocFailed==0 );
119528
119529 /* Begin by generating some termination code at the end of the
119530 ** vdbe program
119531 */
119532 v = pParse->pVdbe;
119533 if( v==0 ){
119534 if( db->init.busy ){
119535 pParse->rc = SQLITE_DONE;
119536 return;
119537 }
119538 v = sqlite3GetVdbe(pParse);
119539 if( v==0 ) pParse->rc = SQLITE_ERROR;
119540 }
119541 assert( !pParse->isMultiWrite
119542 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
119543 if( v ){
119544 if( pParse->bReturning ){
119545 Returning *pReturning = pParse->u1.pReturning;
119546 int addrRewind;
119547 int reg;
119548
119549 if( pReturning->nRetCol ){
119550 sqlite3VdbeAddOp0(p: v, OP_FkCheck);
119551 addrRewind =
119552 sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: pReturning->iRetCur);
119553 VdbeCoverage(v);
119554 reg = pReturning->iRetReg;
119555 for(i=0; i<pReturning->nRetCol; i++){
119556 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pReturning->iRetCur, p2: i, p3: reg+i);
119557 }
119558 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: reg, p2: i);
119559 sqlite3VdbeAddOp2(p: v, OP_Next, p1: pReturning->iRetCur, p2: addrRewind+1);
119560 VdbeCoverage(v);
119561 sqlite3VdbeJumpHere(p: v, addr: addrRewind);
119562 }
119563 }
119564 sqlite3VdbeAddOp0(p: v, OP_Halt);
119565
119566#if SQLITE_USER_AUTHENTICATION
119567 if( pParse->nTableLock>0 && db->init.busy==0 ){
119568 sqlite3UserAuthInit(db);
119569 if( db->auth.authLevel<UAUTH_User ){
119570 sqlite3ErrorMsg(pParse, "user not authenticated");
119571 pParse->rc = SQLITE_AUTH_USER;
119572 return;
119573 }
119574 }
119575#endif
119576
119577 /* The cookie mask contains one bit for each database file open.
119578 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
119579 ** set for each database that is used. Generate code to start a
119580 ** transaction on each used database and to verify the schema cookie
119581 ** on each used database.
119582 */
119583 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
119584 sqlite3VdbeJumpHere(p: v, addr: 0);
119585 assert( db->nDb>0 );
119586 iDb = 0;
119587 do{
119588 Schema *pSchema;
119589 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
119590 sqlite3VdbeUsesBtree(p: v, i: iDb);
119591 pSchema = db->aDb[iDb].pSchema;
119592 sqlite3VdbeAddOp4Int(p: v,
119593 OP_Transaction, /* Opcode */
119594 p1: iDb, /* P1 */
119595 DbMaskTest(pParse->writeMask,iDb), /* P2 */
119596 p3: pSchema->schema_cookie, /* P3 */
119597 p4: pSchema->iGeneration /* P4 */
119598 );
119599 if( db->init.busy==0 ) sqlite3VdbeChangeP5(p: v, p5: 1);
119600 VdbeComment((v,
119601 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
119602 }while( ++iDb<db->nDb );
119603#ifndef SQLITE_OMIT_VIRTUALTABLE
119604 for(i=0; i<pParse->nVtabLock; i++){
119605 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
119606 sqlite3VdbeAddOp4(p: v, OP_VBegin, p1: 0, p2: 0, p3: 0, zP4: vtab, P4_VTAB);
119607 }
119608 pParse->nVtabLock = 0;
119609#endif
119610
119611#ifndef SQLITE_OMIT_SHARED_CACHE
119612 /* Once all the cookies have been verified and transactions opened,
119613 ** obtain the required table-locks. This is a no-op unless the
119614 ** shared-cache feature is enabled.
119615 */
119616 if( pParse->nTableLock ) codeTableLocks(pParse);
119617#endif
119618
119619 /* Initialize any AUTOINCREMENT data structures required.
119620 */
119621 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
119622
119623 /* Code constant expressions that where factored out of inner loops.
119624 **
119625 ** The pConstExpr list might also contain expressions that we simply
119626 ** want to keep around until the Parse object is deleted. Such
119627 ** expressions have iConstExprReg==0. Do not generate code for
119628 ** those expressions, of course.
119629 */
119630 if( pParse->pConstExpr ){
119631 ExprList *pEL = pParse->pConstExpr;
119632 pParse->okConstFactor = 0;
119633 for(i=0; i<pEL->nExpr; i++){
119634 int iReg = pEL->a[i].u.iConstExprReg;
119635 sqlite3ExprCode(pParse, pExpr: pEL->a[i].pExpr, target: iReg);
119636 }
119637 }
119638
119639 if( pParse->bReturning ){
119640 Returning *pRet = pParse->u1.pReturning;
119641 if( pRet->nRetCol ){
119642 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pRet->iRetCur, p2: pRet->nRetCol);
119643 }
119644 }
119645
119646 /* Finally, jump back to the beginning of the executable code. */
119647 sqlite3VdbeGoto(p: v, iDest: 1);
119648 }
119649
119650 /* Get the VDBE program ready for execution
119651 */
119652 assert( v!=0 || pParse->nErr );
119653 assert( db->mallocFailed==0 || pParse->nErr );
119654 if( pParse->nErr==0 ){
119655 /* A minimum of one cursor is required if autoincrement is used
119656 * See ticket [a696379c1f08866] */
119657 assert( pParse->pAinc==0 || pParse->nTab>0 );
119658 sqlite3VdbeMakeReady(p: v, pParse);
119659 pParse->rc = SQLITE_DONE;
119660 }else{
119661 pParse->rc = SQLITE_ERROR;
119662 }
119663}
119664
119665/*
119666** Run the parser and code generator recursively in order to generate
119667** code for the SQL statement given onto the end of the pParse context
119668** currently under construction. Notes:
119669**
119670** * The final OP_Halt is not appended and other initialization
119671** and finalization steps are omitted because those are handling by the
119672** outermost parser.
119673**
119674** * Built-in SQL functions always take precedence over application-defined
119675** SQL functions. In other words, it is not possible to override a
119676** built-in function.
119677*/
119678SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
119679 va_list ap;
119680 char *zSql;
119681 sqlite3 *db = pParse->db;
119682 u32 savedDbFlags = db->mDbFlags;
119683 char saveBuf[PARSE_TAIL_SZ];
119684
119685 if( pParse->nErr ) return;
119686 if( pParse->eParseMode ) return;
119687 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
119688 va_start(ap, zFormat);
119689 zSql = sqlite3VMPrintf(db, zFormat, ap);
119690 va_end(ap);
119691 if( zSql==0 ){
119692 /* This can result either from an OOM or because the formatted string
119693 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
119694 ** an error */
119695 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
119696 pParse->nErr++;
119697 return;
119698 }
119699 pParse->nested++;
119700 memcpy(dest: saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
119701 memset(PARSE_TAIL(pParse), c: 0, PARSE_TAIL_SZ);
119702 db->mDbFlags |= DBFLAG_PreferBuiltin;
119703 sqlite3RunParser(pParse, zSql);
119704 db->mDbFlags = savedDbFlags;
119705 sqlite3DbFree(db, p: zSql);
119706 memcpy(PARSE_TAIL(pParse), src: saveBuf, PARSE_TAIL_SZ);
119707 pParse->nested--;
119708}
119709
119710#if SQLITE_USER_AUTHENTICATION
119711/*
119712** Return TRUE if zTable is the name of the system table that stores the
119713** list of users and their access credentials.
119714*/
119715SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
119716 return sqlite3_stricmp(zTable, "sqlite_user")==0;
119717}
119718#endif
119719
119720/*
119721** Locate the in-memory structure that describes a particular database
119722** table given the name of that table and (optionally) the name of the
119723** database containing the table. Return NULL if not found.
119724**
119725** If zDatabase is 0, all databases are searched for the table and the
119726** first matching table is returned. (No checking for duplicate table
119727** names is done.) The search order is TEMP first, then MAIN, then any
119728** auxiliary databases added using the ATTACH command.
119729**
119730** See also sqlite3LocateTable().
119731*/
119732SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
119733 Table *p = 0;
119734 int i;
119735
119736 /* All mutexes are required for schema access. Make sure we hold them. */
119737 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
119738#if SQLITE_USER_AUTHENTICATION
119739 /* Only the admin user is allowed to know that the sqlite_user table
119740 ** exists */
119741 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
119742 return 0;
119743 }
119744#endif
119745 if( zDatabase ){
119746 for(i=0; i<db->nDb; i++){
119747 if( sqlite3StrICmp(zLeft: zDatabase, zRight: db->aDb[i].zDbSName)==0 ) break;
119748 }
119749 if( i>=db->nDb ){
119750 /* No match against the official names. But always match "main"
119751 ** to schema 0 as a legacy fallback. */
119752 if( sqlite3StrICmp(zLeft: zDatabase,zRight: "main")==0 ){
119753 i = 0;
119754 }else{
119755 return 0;
119756 }
119757 }
119758 p = sqlite3HashFind(pH: &db->aDb[i].pSchema->tblHash, pKey: zName);
119759 if( p==0 && sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7)==0 ){
119760 if( i==1 ){
119761 if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_TEMP_SCHEMA_TABLE[7])==0
119762 || sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0
119763 || sqlite3StrICmp(zLeft: zName+7, zRight: &LEGACY_SCHEMA_TABLE[7])==0
119764 ){
119765 p = sqlite3HashFind(pH: &db->aDb[1].pSchema->tblHash,
119766 LEGACY_TEMP_SCHEMA_TABLE);
119767 }
119768 }else{
119769 if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0 ){
119770 p = sqlite3HashFind(pH: &db->aDb[i].pSchema->tblHash,
119771 LEGACY_SCHEMA_TABLE);
119772 }
119773 }
119774 }
119775 }else{
119776 /* Match against TEMP first */
119777 p = sqlite3HashFind(pH: &db->aDb[1].pSchema->tblHash, pKey: zName);
119778 if( p ) return p;
119779 /* The main database is second */
119780 p = sqlite3HashFind(pH: &db->aDb[0].pSchema->tblHash, pKey: zName);
119781 if( p ) return p;
119782 /* Attached databases are in order of attachment */
119783 for(i=2; i<db->nDb; i++){
119784 assert( sqlite3SchemaMutexHeld(db, i, 0) );
119785 p = sqlite3HashFind(pH: &db->aDb[i].pSchema->tblHash, pKey: zName);
119786 if( p ) break;
119787 }
119788 if( p==0 && sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7)==0 ){
119789 if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0 ){
119790 p = sqlite3HashFind(pH: &db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
119791 }else if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){
119792 p = sqlite3HashFind(pH: &db->aDb[1].pSchema->tblHash,
119793 LEGACY_TEMP_SCHEMA_TABLE);
119794 }
119795 }
119796 }
119797 return p;
119798}
119799
119800/*
119801** Locate the in-memory structure that describes a particular database
119802** table given the name of that table and (optionally) the name of the
119803** database containing the table. Return NULL if not found. Also leave an
119804** error message in pParse->zErrMsg.
119805**
119806** The difference between this routine and sqlite3FindTable() is that this
119807** routine leaves an error message in pParse->zErrMsg where
119808** sqlite3FindTable() does not.
119809*/
119810SQLITE_PRIVATE Table *sqlite3LocateTable(
119811 Parse *pParse, /* context in which to report errors */
119812 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
119813 const char *zName, /* Name of the table we are looking for */
119814 const char *zDbase /* Name of the database. Might be NULL */
119815){
119816 Table *p;
119817 sqlite3 *db = pParse->db;
119818
119819 /* Read the database schema. If an error occurs, leave an error message
119820 ** and code in pParse and return NULL. */
119821 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
119822 && SQLITE_OK!=sqlite3ReadSchema(pParse)
119823 ){
119824 return 0;
119825 }
119826
119827 p = sqlite3FindTable(db, zName, zDatabase: zDbase);
119828 if( p==0 ){
119829#ifndef SQLITE_OMIT_VIRTUALTABLE
119830 /* If zName is the not the name of a table in the schema created using
119831 ** CREATE, then check to see if it is the name of an virtual table that
119832 ** can be an eponymous virtual table. */
119833 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
119834 Module *pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: zName);
119835 if( pMod==0 && sqlite3_strnicmp(zLeft: zName, zRight: "pragma_", N: 7)==0 ){
119836 pMod = sqlite3PragmaVtabRegister(db, zName);
119837 }
119838 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
119839 testcase( pMod->pEpoTab==0 );
119840 return pMod->pEpoTab;
119841 }
119842 }
119843#endif
119844 if( flags & LOCATE_NOERR ) return 0;
119845 pParse->checkSchema = 1;
119846 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
119847 p = 0;
119848 }
119849
119850 if( p==0 ){
119851 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
119852 if( zDbase ){
119853 sqlite3ErrorMsg(pParse, zFormat: "%s: %s.%s", zMsg, zDbase, zName);
119854 }else{
119855 sqlite3ErrorMsg(pParse, zFormat: "%s: %s", zMsg, zName);
119856 }
119857 }else{
119858 assert( HasRowid(p) || p->iPKey<0 );
119859 }
119860
119861 return p;
119862}
119863
119864/*
119865** Locate the table identified by *p.
119866**
119867** This is a wrapper around sqlite3LocateTable(). The difference between
119868** sqlite3LocateTable() and this function is that this function restricts
119869** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
119870** non-NULL if it is part of a view or trigger program definition. See
119871** sqlite3FixSrcList() for details.
119872*/
119873SQLITE_PRIVATE Table *sqlite3LocateTableItem(
119874 Parse *pParse,
119875 u32 flags,
119876 SrcItem *p
119877){
119878 const char *zDb;
119879 assert( p->pSchema==0 || p->zDatabase==0 );
119880 if( p->pSchema ){
119881 int iDb = sqlite3SchemaToIndex(db: pParse->db, p->pSchema);
119882 zDb = pParse->db->aDb[iDb].zDbSName;
119883 }else{
119884 zDb = p->zDatabase;
119885 }
119886 return sqlite3LocateTable(pParse, flags, zName: p->zName, zDbase: zDb);
119887}
119888
119889/*
119890** Return the preferred table name for system tables. Translate legacy
119891** names into the new preferred names, as appropriate.
119892*/
119893SQLITE_PRIVATE const char *sqlite3PreferredTableName(const char *zName){
119894 if( sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7)==0 ){
119895 if( sqlite3StrICmp(zLeft: zName+7, zRight: &LEGACY_SCHEMA_TABLE[7])==0 ){
119896 return PREFERRED_SCHEMA_TABLE;
119897 }
119898 if( sqlite3StrICmp(zLeft: zName+7, zRight: &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){
119899 return PREFERRED_TEMP_SCHEMA_TABLE;
119900 }
119901 }
119902 return zName;
119903}
119904
119905/*
119906** Locate the in-memory structure that describes
119907** a particular index given the name of that index
119908** and the name of the database that contains the index.
119909** Return NULL if not found.
119910**
119911** If zDatabase is 0, all databases are searched for the
119912** table and the first matching index is returned. (No checking
119913** for duplicate index names is done.) The search order is
119914** TEMP first, then MAIN, then any auxiliary databases added
119915** using the ATTACH command.
119916*/
119917SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
119918 Index *p = 0;
119919 int i;
119920 /* All mutexes are required for schema access. Make sure we hold them. */
119921 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
119922 for(i=OMIT_TEMPDB; i<db->nDb; i++){
119923 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
119924 Schema *pSchema = db->aDb[j].pSchema;
119925 assert( pSchema );
119926 if( zDb && sqlite3DbIsNamed(db, iDb: j, zName: zDb)==0 ) continue;
119927 assert( sqlite3SchemaMutexHeld(db, j, 0) );
119928 p = sqlite3HashFind(pH: &pSchema->idxHash, pKey: zName);
119929 if( p ) break;
119930 }
119931 return p;
119932}
119933
119934/*
119935** Reclaim the memory used by an index
119936*/
119937SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
119938#ifndef SQLITE_OMIT_ANALYZE
119939 sqlite3DeleteIndexSamples(db, pIdx: p);
119940#endif
119941 sqlite3ExprDelete(db, p: p->pPartIdxWhere);
119942 sqlite3ExprListDelete(db, pList: p->aColExpr);
119943 sqlite3DbFree(db, p: p->zColAff);
119944 if( p->isResized ) sqlite3DbFree(db, p: (void *)p->azColl);
119945#ifdef SQLITE_ENABLE_STAT4
119946 sqlite3_free(p->aiRowEst);
119947#endif
119948 sqlite3DbFree(db, p);
119949}
119950
119951/*
119952** For the index called zIdxName which is found in the database iDb,
119953** unlike that index from its Table then remove the index from
119954** the index hash table and free all memory structures associated
119955** with the index.
119956*/
119957SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
119958 Index *pIndex;
119959 Hash *pHash;
119960
119961 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
119962 pHash = &db->aDb[iDb].pSchema->idxHash;
119963 pIndex = sqlite3HashInsert(pH: pHash, pKey: zIdxName, data: 0);
119964 if( ALWAYS(pIndex) ){
119965 if( pIndex->pTable->pIndex==pIndex ){
119966 pIndex->pTable->pIndex = pIndex->pNext;
119967 }else{
119968 Index *p;
119969 /* Justification of ALWAYS(); The index must be on the list of
119970 ** indices. */
119971 p = pIndex->pTable->pIndex;
119972 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
119973 if( ALWAYS(p && p->pNext==pIndex) ){
119974 p->pNext = pIndex->pNext;
119975 }
119976 }
119977 sqlite3FreeIndex(db, p: pIndex);
119978 }
119979 db->mDbFlags |= DBFLAG_SchemaChange;
119980}
119981
119982/*
119983** Look through the list of open database files in db->aDb[] and if
119984** any have been closed, remove them from the list. Reallocate the
119985** db->aDb[] structure to a smaller size, if possible.
119986**
119987** Entry 0 (the "main" database) and entry 1 (the "temp" database)
119988** are never candidates for being collapsed.
119989*/
119990SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
119991 int i, j;
119992 for(i=j=2; i<db->nDb; i++){
119993 struct Db *pDb = &db->aDb[i];
119994 if( pDb->pBt==0 ){
119995 sqlite3DbFree(db, p: pDb->zDbSName);
119996 pDb->zDbSName = 0;
119997 continue;
119998 }
119999 if( j<i ){
120000 db->aDb[j] = db->aDb[i];
120001 }
120002 j++;
120003 }
120004 db->nDb = j;
120005 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
120006 memcpy(dest: db->aDbStatic, src: db->aDb, n: 2*sizeof(db->aDb[0]));
120007 sqlite3DbFree(db, p: db->aDb);
120008 db->aDb = db->aDbStatic;
120009 }
120010}
120011
120012/*
120013** Reset the schema for the database at index iDb. Also reset the
120014** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
120015** Deferred resets may be run by calling with iDb<0.
120016*/
120017SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
120018 int i;
120019 assert( iDb<db->nDb );
120020
120021 if( iDb>=0 ){
120022 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120023 DbSetProperty(db, iDb, DB_ResetWanted);
120024 DbSetProperty(db, 1, DB_ResetWanted);
120025 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
120026 }
120027
120028 if( db->nSchemaLock==0 ){
120029 for(i=0; i<db->nDb; i++){
120030 if( DbHasProperty(db, i, DB_ResetWanted) ){
120031 sqlite3SchemaClear(db->aDb[i].pSchema);
120032 }
120033 }
120034 }
120035}
120036
120037/*
120038** Erase all schema information from all attached databases (including
120039** "main" and "temp") for a single database connection.
120040*/
120041SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
120042 int i;
120043 sqlite3BtreeEnterAll(db);
120044 for(i=0; i<db->nDb; i++){
120045 Db *pDb = &db->aDb[i];
120046 if( pDb->pSchema ){
120047 if( db->nSchemaLock==0 ){
120048 sqlite3SchemaClear(pDb->pSchema);
120049 }else{
120050 DbSetProperty(db, i, DB_ResetWanted);
120051 }
120052 }
120053 }
120054 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
120055 sqlite3VtabUnlockList(db);
120056 sqlite3BtreeLeaveAll(db);
120057 if( db->nSchemaLock==0 ){
120058 sqlite3CollapseDatabaseArray(db);
120059 }
120060}
120061
120062/*
120063** This routine is called when a commit occurs.
120064*/
120065SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
120066 db->mDbFlags &= ~DBFLAG_SchemaChange;
120067}
120068
120069/*
120070** Set the expression associated with a column. This is usually
120071** the DEFAULT value, but might also be the expression that computes
120072** the value for a generated column.
120073*/
120074SQLITE_PRIVATE void sqlite3ColumnSetExpr(
120075 Parse *pParse, /* Parsing context */
120076 Table *pTab, /* The table containing the column */
120077 Column *pCol, /* The column to receive the new DEFAULT expression */
120078 Expr *pExpr /* The new default expression */
120079){
120080 ExprList *pList;
120081 assert( IsOrdinaryTable(pTab) );
120082 pList = pTab->u.tab.pDfltList;
120083 if( pCol->iDflt==0
120084 || NEVER(pList==0)
120085 || NEVER(pList->nExpr<pCol->iDflt)
120086 ){
120087 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
120088 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
120089 }else{
120090 sqlite3ExprDelete(db: pParse->db, p: pList->a[pCol->iDflt-1].pExpr);
120091 pList->a[pCol->iDflt-1].pExpr = pExpr;
120092 }
120093}
120094
120095/*
120096** Return the expression associated with a column. The expression might be
120097** the DEFAULT clause or the AS clause of a generated column.
120098** Return NULL if the column has no associated expression.
120099*/
120100SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
120101 if( pCol->iDflt==0 ) return 0;
120102 if( NEVER(!IsOrdinaryTable(pTab)) ) return 0;
120103 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
120104 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
120105 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
120106}
120107
120108/*
120109** Set the collating sequence name for a column.
120110*/
120111SQLITE_PRIVATE void sqlite3ColumnSetColl(
120112 sqlite3 *db,
120113 Column *pCol,
120114 const char *zColl
120115){
120116 i64 nColl;
120117 i64 n;
120118 char *zNew;
120119 assert( zColl!=0 );
120120 n = sqlite3Strlen30(z: pCol->zCnName) + 1;
120121 if( pCol->colFlags & COLFLAG_HASTYPE ){
120122 n += sqlite3Strlen30(z: pCol->zCnName+n) + 1;
120123 }
120124 nColl = sqlite3Strlen30(z: zColl) + 1;
120125 zNew = sqlite3DbRealloc(db, p: pCol->zCnName, n: nColl+n);
120126 if( zNew ){
120127 pCol->zCnName = zNew;
120128 memcpy(dest: pCol->zCnName + n, src: zColl, n: nColl);
120129 pCol->colFlags |= COLFLAG_HASCOLL;
120130 }
120131}
120132
120133/*
120134** Return the collating sequence name for a column
120135*/
120136SQLITE_PRIVATE const char *sqlite3ColumnColl(Column *pCol){
120137 const char *z;
120138 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
120139 z = pCol->zCnName;
120140 while( *z ){ z++; }
120141 if( pCol->colFlags & COLFLAG_HASTYPE ){
120142 do{ z++; }while( *z );
120143 }
120144 return z+1;
120145}
120146
120147/*
120148** Delete memory allocated for the column names of a table or view (the
120149** Table.aCol[] array).
120150*/
120151SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
120152 int i;
120153 Column *pCol;
120154 assert( pTable!=0 );
120155 assert( db!=0 );
120156 if( (pCol = pTable->aCol)!=0 ){
120157 for(i=0; i<pTable->nCol; i++, pCol++){
120158 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
120159 sqlite3DbFree(db, p: pCol->zCnName);
120160 }
120161 sqlite3DbNNFreeNN(db, p: pTable->aCol);
120162 if( IsOrdinaryTable(pTable) ){
120163 sqlite3ExprListDelete(db, pList: pTable->u.tab.pDfltList);
120164 }
120165 if( db->pnBytesFreed==0 ){
120166 pTable->aCol = 0;
120167 pTable->nCol = 0;
120168 if( IsOrdinaryTable(pTable) ){
120169 pTable->u.tab.pDfltList = 0;
120170 }
120171 }
120172 }
120173}
120174
120175/*
120176** Remove the memory data structures associated with the given
120177** Table. No changes are made to disk by this routine.
120178**
120179** This routine just deletes the data structure. It does not unlink
120180** the table data structure from the hash table. But it does destroy
120181** memory structures of the indices and foreign keys associated with
120182** the table.
120183**
120184** The db parameter is optional. It is needed if the Table object
120185** contains lookaside memory. (Table objects in the schema do not use
120186** lookaside memory, but some ephemeral Table objects do.) Or the
120187** db parameter can be used with db->pnBytesFreed to measure the memory
120188** used by the Table object.
120189*/
120190static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
120191 Index *pIndex, *pNext;
120192
120193#ifdef SQLITE_DEBUG
120194 /* Record the number of outstanding lookaside allocations in schema Tables
120195 ** prior to doing any free() operations. Since schema Tables do not use
120196 ** lookaside, this number should not change.
120197 **
120198 ** If malloc has already failed, it may be that it failed while allocating
120199 ** a Table object that was going to be marked ephemeral. So do not check
120200 ** that no lookaside memory is used in this case either. */
120201 int nLookaside = 0;
120202 assert( db!=0 );
120203 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
120204 nLookaside = sqlite3LookasideUsed(db, 0);
120205 }
120206#endif
120207
120208 /* Delete all indices associated with this table. */
120209 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
120210 pNext = pIndex->pNext;
120211 assert( pIndex->pSchema==pTable->pSchema
120212 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
120213 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
120214 char *zName = pIndex->zName;
120215 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
120216 pH: &pIndex->pSchema->idxHash, pKey: zName, data: 0
120217 );
120218 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
120219 assert( pOld==pIndex || pOld==0 );
120220 }
120221 sqlite3FreeIndex(db, p: pIndex);
120222 }
120223
120224 if( IsOrdinaryTable(pTable) ){
120225 sqlite3FkDelete(db, pTable);
120226 }
120227#ifndef SQLITE_OMIT_VIRTUALTABLE
120228 else if( IsVirtual(pTable) ){
120229 sqlite3VtabClear(db, pTable);
120230 }
120231#endif
120232 else{
120233 assert( IsView(pTable) );
120234 sqlite3SelectDelete(db, pTable->u.view.pSelect);
120235 }
120236
120237 /* Delete the Table structure itself.
120238 */
120239 sqlite3DeleteColumnNames(db, pTable);
120240 sqlite3DbFree(db, p: pTable->zName);
120241 sqlite3DbFree(db, p: pTable->zColAff);
120242 sqlite3ExprListDelete(db, pList: pTable->pCheck);
120243 sqlite3DbFree(db, p: pTable);
120244
120245 /* Verify that no lookaside memory was used by schema tables */
120246 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
120247}
120248SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
120249 /* Do not delete the table until the reference count reaches zero. */
120250 assert( db!=0 );
120251 if( !pTable ) return;
120252 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
120253 deleteTable(db, pTable);
120254}
120255
120256
120257/*
120258** Unlink the given table from the hash tables and the delete the
120259** table structure with all its indices and foreign keys.
120260*/
120261SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
120262 Table *p;
120263 Db *pDb;
120264
120265 assert( db!=0 );
120266 assert( iDb>=0 && iDb<db->nDb );
120267 assert( zTabName );
120268 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120269 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
120270 pDb = &db->aDb[iDb];
120271 p = sqlite3HashInsert(pH: &pDb->pSchema->tblHash, pKey: zTabName, data: 0);
120272 sqlite3DeleteTable(db, pTable: p);
120273 db->mDbFlags |= DBFLAG_SchemaChange;
120274}
120275
120276/*
120277** Given a token, return a string that consists of the text of that
120278** token. Space to hold the returned string
120279** is obtained from sqliteMalloc() and must be freed by the calling
120280** function.
120281**
120282** Any quotation marks (ex: "name", 'name', [name], or `name`) that
120283** surround the body of the token are removed.
120284**
120285** Tokens are often just pointers into the original SQL text and so
120286** are not \000 terminated and are not persistent. The returned string
120287** is \000 terminated and is persistent.
120288*/
120289SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
120290 char *zName;
120291 if( pName ){
120292 zName = sqlite3DbStrNDup(db, z: (const char*)pName->z, n: pName->n);
120293 sqlite3Dequote(z: zName);
120294 }else{
120295 zName = 0;
120296 }
120297 return zName;
120298}
120299
120300/*
120301** Open the sqlite_schema table stored in database number iDb for
120302** writing. The table is opened using cursor 0.
120303*/
120304SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *p, int iDb){
120305 Vdbe *v = sqlite3GetVdbe(p);
120306 sqlite3TableLock(pParse: p, iDb, SCHEMA_ROOT, isWriteLock: 1, LEGACY_SCHEMA_TABLE);
120307 sqlite3VdbeAddOp4Int(p: v, OP_OpenWrite, p1: 0, SCHEMA_ROOT, p3: iDb, p4: 5);
120308 if( p->nTab==0 ){
120309 p->nTab = 1;
120310 }
120311}
120312
120313/*
120314** Parameter zName points to a nul-terminated buffer containing the name
120315** of a database ("main", "temp" or the name of an attached db). This
120316** function returns the index of the named database in db->aDb[], or
120317** -1 if the named db cannot be found.
120318*/
120319SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
120320 int i = -1; /* Database number */
120321 if( zName ){
120322 Db *pDb;
120323 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
120324 if( 0==sqlite3_stricmp(zLeft: pDb->zDbSName, zRight: zName) ) break;
120325 /* "main" is always an acceptable alias for the primary database
120326 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
120327 if( i==0 && 0==sqlite3_stricmp(zLeft: "main", zRight: zName) ) break;
120328 }
120329 }
120330 return i;
120331}
120332
120333/*
120334** The token *pName contains the name of a database (either "main" or
120335** "temp" or the name of an attached db). This routine returns the
120336** index of the named database in db->aDb[], or -1 if the named db
120337** does not exist.
120338*/
120339SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
120340 int i; /* Database number */
120341 char *zName; /* Name we are searching for */
120342 zName = sqlite3NameFromToken(db, pName);
120343 i = sqlite3FindDbName(db, zName);
120344 sqlite3DbFree(db, p: zName);
120345 return i;
120346}
120347
120348/* The table or view or trigger name is passed to this routine via tokens
120349** pName1 and pName2. If the table name was fully qualified, for example:
120350**
120351** CREATE TABLE xxx.yyy (...);
120352**
120353** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
120354** the table name is not fully qualified, i.e.:
120355**
120356** CREATE TABLE yyy(...);
120357**
120358** Then pName1 is set to "yyy" and pName2 is "".
120359**
120360** This routine sets the *ppUnqual pointer to point at the token (pName1 or
120361** pName2) that stores the unqualified table name. The index of the
120362** database "xxx" is returned.
120363*/
120364SQLITE_PRIVATE int sqlite3TwoPartName(
120365 Parse *pParse, /* Parsing and code generating context */
120366 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
120367 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
120368 Token **pUnqual /* Write the unqualified object name here */
120369){
120370 int iDb; /* Database holding the object */
120371 sqlite3 *db = pParse->db;
120372
120373 assert( pName2!=0 );
120374 if( pName2->n>0 ){
120375 if( db->init.busy ) {
120376 sqlite3ErrorMsg(pParse, zFormat: "corrupt database");
120377 return -1;
120378 }
120379 *pUnqual = pName2;
120380 iDb = sqlite3FindDb(db, pName: pName1);
120381 if( iDb<0 ){
120382 sqlite3ErrorMsg(pParse, zFormat: "unknown database %T", pName1);
120383 return -1;
120384 }
120385 }else{
120386 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
120387 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
120388 iDb = db->init.iDb;
120389 *pUnqual = pName1;
120390 }
120391 return iDb;
120392}
120393
120394/*
120395** True if PRAGMA writable_schema is ON
120396*/
120397SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
120398 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
120399 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
120400 SQLITE_WriteSchema );
120401 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
120402 SQLITE_Defensive );
120403 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
120404 (SQLITE_WriteSchema|SQLITE_Defensive) );
120405 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
120406}
120407
120408/*
120409** This routine is used to check if the UTF-8 string zName is a legal
120410** unqualified name for a new schema object (table, index, view or
120411** trigger). All names are legal except those that begin with the string
120412** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
120413** is reserved for internal use.
120414**
120415** When parsing the sqlite_schema table, this routine also checks to
120416** make sure the "type", "name", and "tbl_name" columns are consistent
120417** with the SQL.
120418*/
120419SQLITE_PRIVATE int sqlite3CheckObjectName(
120420 Parse *pParse, /* Parsing context */
120421 const char *zName, /* Name of the object to check */
120422 const char *zType, /* Type of this object */
120423 const char *zTblName /* Parent table name for triggers and indexes */
120424){
120425 sqlite3 *db = pParse->db;
120426 if( sqlite3WritableSchema(db)
120427 || db->init.imposterTable
120428 || !sqlite3Config.bExtraSchemaChecks
120429 ){
120430 /* Skip these error checks for writable_schema=ON */
120431 return SQLITE_OK;
120432 }
120433 if( db->init.busy ){
120434 if( sqlite3_stricmp(zLeft: zType, zRight: db->init.azInit[0])
120435 || sqlite3_stricmp(zLeft: zName, zRight: db->init.azInit[1])
120436 || sqlite3_stricmp(zLeft: zTblName, zRight: db->init.azInit[2])
120437 ){
120438 sqlite3ErrorMsg(pParse, zFormat: ""); /* corruptSchema() will supply the error */
120439 return SQLITE_ERROR;
120440 }
120441 }else{
120442 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7))
120443 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
120444 ){
120445 sqlite3ErrorMsg(pParse, zFormat: "object name reserved for internal use: %s",
120446 zName);
120447 return SQLITE_ERROR;
120448 }
120449
120450 }
120451 return SQLITE_OK;
120452}
120453
120454/*
120455** Return the PRIMARY KEY index of a table
120456*/
120457SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
120458 Index *p;
120459 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
120460 return p;
120461}
120462
120463/*
120464** Convert an table column number into a index column number. That is,
120465** for the column iCol in the table (as defined by the CREATE TABLE statement)
120466** find the (first) offset of that column in index pIdx. Or return -1
120467** if column iCol is not used in index pIdx.
120468*/
120469SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
120470 int i;
120471 for(i=0; i<pIdx->nColumn; i++){
120472 if( iCol==pIdx->aiColumn[i] ) return i;
120473 }
120474 return -1;
120475}
120476
120477#ifndef SQLITE_OMIT_GENERATED_COLUMNS
120478/* Convert a storage column number into a table column number.
120479**
120480** The storage column number (0,1,2,....) is the index of the value
120481** as it appears in the record on disk. The true column number
120482** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
120483**
120484** The storage column number is less than the table column number if
120485** and only there are VIRTUAL columns to the left.
120486**
120487** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
120488*/
120489SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
120490 if( pTab->tabFlags & TF_HasVirtual ){
120491 int i;
120492 for(i=0; i<=iCol; i++){
120493 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
120494 }
120495 }
120496 return iCol;
120497}
120498#endif
120499
120500#ifndef SQLITE_OMIT_GENERATED_COLUMNS
120501/* Convert a table column number into a storage column number.
120502**
120503** The storage column number (0,1,2,....) is the index of the value
120504** as it appears in the record on disk. Or, if the input column is
120505** the N-th virtual column (zero-based) then the storage number is
120506** the number of non-virtual columns in the table plus N.
120507**
120508** The true column number is the index (0,1,2,...) of the column in
120509** the CREATE TABLE statement.
120510**
120511** If the input column is a VIRTUAL column, then it should not appear
120512** in storage. But the value sometimes is cached in registers that
120513** follow the range of registers used to construct storage. This
120514** avoids computing the same VIRTUAL column multiple times, and provides
120515** values for use by OP_Param opcodes in triggers. Hence, if the
120516** input column is a VIRTUAL table, put it after all the other columns.
120517**
120518** In the following, N means "normal column", S means STORED, and
120519** V means VIRTUAL. Suppose the CREATE TABLE has columns like this:
120520**
120521** CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
120522** -- 0 1 2 3 4 5 6 7 8
120523**
120524** Then the mapping from this function is as follows:
120525**
120526** INPUTS: 0 1 2 3 4 5 6 7 8
120527** OUTPUTS: 0 1 6 2 3 7 4 5 8
120528**
120529** So, in other words, this routine shifts all the virtual columns to
120530** the end.
120531**
120532** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
120533** this routine is a no-op macro. If the pTab does not have any virtual
120534** columns, then this routine is no-op that always return iCol. If iCol
120535** is negative (indicating the ROWID column) then this routine return iCol.
120536*/
120537SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
120538 int i;
120539 i16 n;
120540 assert( iCol<pTab->nCol );
120541 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
120542 for(i=0, n=0; i<iCol; i++){
120543 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
120544 }
120545 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
120546 /* iCol is a virtual column itself */
120547 return pTab->nNVCol + i - n;
120548 }else{
120549 /* iCol is a normal or stored column */
120550 return n;
120551 }
120552}
120553#endif
120554
120555/*
120556** Insert a single OP_JournalMode query opcode in order to force the
120557** prepared statement to return false for sqlite3_stmt_readonly(). This
120558** is used by CREATE TABLE IF NOT EXISTS and similar if the table already
120559** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS
120560** will return false for sqlite3_stmt_readonly() even if that statement
120561** is a read-only no-op.
120562*/
120563static void sqlite3ForceNotReadOnly(Parse *pParse){
120564 int iReg = ++pParse->nMem;
120565 Vdbe *v = sqlite3GetVdbe(pParse);
120566 if( v ){
120567 sqlite3VdbeAddOp3(p: v, OP_JournalMode, p1: 0, p2: iReg, PAGER_JOURNALMODE_QUERY);
120568 sqlite3VdbeUsesBtree(p: v, i: 0);
120569 }
120570}
120571
120572/*
120573** Begin constructing a new table representation in memory. This is
120574** the first of several action routines that get called in response
120575** to a CREATE TABLE statement. In particular, this routine is called
120576** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
120577** flag is true if the table should be stored in the auxiliary database
120578** file instead of in the main database file. This is normally the case
120579** when the "TEMP" or "TEMPORARY" keyword occurs in between
120580** CREATE and TABLE.
120581**
120582** The new table record is initialized and put in pParse->pNewTable.
120583** As more of the CREATE TABLE statement is parsed, additional action
120584** routines will be called to add more information to this record.
120585** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
120586** is called to complete the construction of the new table record.
120587*/
120588SQLITE_PRIVATE void sqlite3StartTable(
120589 Parse *pParse, /* Parser context */
120590 Token *pName1, /* First part of the name of the table or view */
120591 Token *pName2, /* Second part of the name of the table or view */
120592 int isTemp, /* True if this is a TEMP table */
120593 int isView, /* True if this is a VIEW */
120594 int isVirtual, /* True if this is a VIRTUAL table */
120595 int noErr /* Do nothing if table already exists */
120596){
120597 Table *pTable;
120598 char *zName = 0; /* The name of the new table */
120599 sqlite3 *db = pParse->db;
120600 Vdbe *v;
120601 int iDb; /* Database number to create the table in */
120602 Token *pName; /* Unqualified name of the table to create */
120603
120604 if( db->init.busy && db->init.newTnum==1 ){
120605 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
120606 iDb = db->init.iDb;
120607 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
120608 pName = pName1;
120609 }else{
120610 /* The common case */
120611 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
120612 if( iDb<0 ) return;
120613 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
120614 /* If creating a temp table, the name may not be qualified. Unless
120615 ** the database name is "temp" anyway. */
120616 sqlite3ErrorMsg(pParse, zFormat: "temporary table name must be unqualified");
120617 return;
120618 }
120619 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
120620 zName = sqlite3NameFromToken(db, pName);
120621 if( IN_RENAME_OBJECT ){
120622 sqlite3RenameTokenMap(pParse, pPtr: (void*)zName, pToken: pName);
120623 }
120624 }
120625 pParse->sNameToken = *pName;
120626 if( zName==0 ) return;
120627 if( sqlite3CheckObjectName(pParse, zName, zType: isView?"view":"table", zTblName: zName) ){
120628 goto begin_table_error;
120629 }
120630 if( db->init.iDb==1 ) isTemp = 1;
120631#ifndef SQLITE_OMIT_AUTHORIZATION
120632 assert( isTemp==0 || isTemp==1 );
120633 assert( isView==0 || isView==1 );
120634 {
120635 static const u8 aCode[] = {
120636 SQLITE_CREATE_TABLE,
120637 SQLITE_CREATE_TEMP_TABLE,
120638 SQLITE_CREATE_VIEW,
120639 SQLITE_CREATE_TEMP_VIEW
120640 };
120641 char *zDb = db->aDb[iDb].zDbSName;
120642 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), zArg2: 0, zArg3: zDb) ){
120643 goto begin_table_error;
120644 }
120645 if( !isVirtual && sqlite3AuthCheck(pParse, code: (int)aCode[isTemp+2*isView],
120646 zArg1: zName, zArg2: 0, zArg3: zDb) ){
120647 goto begin_table_error;
120648 }
120649 }
120650#endif
120651
120652 /* Make sure the new table name does not collide with an existing
120653 ** index or table name in the same database. Issue an error message if
120654 ** it does. The exception is if the statement being parsed was passed
120655 ** to an sqlite3_declare_vtab() call. In that case only the column names
120656 ** and types will be used, so there is no need to test for namespace
120657 ** collisions.
120658 */
120659 if( !IN_SPECIAL_PARSE ){
120660 char *zDb = db->aDb[iDb].zDbSName;
120661 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
120662 goto begin_table_error;
120663 }
120664 pTable = sqlite3FindTable(db, zName, zDatabase: zDb);
120665 if( pTable ){
120666 if( !noErr ){
120667 sqlite3ErrorMsg(pParse, zFormat: "%s %T already exists",
120668 (IsView(pTable)? "view" : "table"), pName);
120669 }else{
120670 assert( !db->init.busy || CORRUPT_DB );
120671 sqlite3CodeVerifySchema(pParse, iDb);
120672 sqlite3ForceNotReadOnly(pParse);
120673 }
120674 goto begin_table_error;
120675 }
120676 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
120677 sqlite3ErrorMsg(pParse, zFormat: "there is already an index named %s", zName);
120678 goto begin_table_error;
120679 }
120680 }
120681
120682 pTable = sqlite3DbMallocZero(db, n: sizeof(Table));
120683 if( pTable==0 ){
120684 assert( db->mallocFailed );
120685 pParse->rc = SQLITE_NOMEM_BKPT;
120686 pParse->nErr++;
120687 goto begin_table_error;
120688 }
120689 pTable->zName = zName;
120690 pTable->iPKey = -1;
120691 pTable->pSchema = db->aDb[iDb].pSchema;
120692 pTable->nTabRef = 1;
120693#ifdef SQLITE_DEFAULT_ROWEST
120694 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
120695#else
120696 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
120697#endif
120698 assert( pParse->pNewTable==0 );
120699 pParse->pNewTable = pTable;
120700
120701 /* Begin generating the code that will insert the table record into
120702 ** the schema table. Note in particular that we must go ahead
120703 ** and allocate the record number for the table entry now. Before any
120704 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
120705 ** indices to be created and the table record must come before the
120706 ** indices. Hence, the record number for the table must be allocated
120707 ** now.
120708 */
120709 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
120710 int addr1;
120711 int fileFormat;
120712 int reg1, reg2, reg3;
120713 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
120714 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
120715 sqlite3BeginWriteOperation(pParse, 1, iDb);
120716
120717#ifndef SQLITE_OMIT_VIRTUALTABLE
120718 if( isVirtual ){
120719 sqlite3VdbeAddOp0(p: v, OP_VBegin);
120720 }
120721#endif
120722
120723 /* If the file format and encoding in the database have not been set,
120724 ** set them now.
120725 */
120726 reg1 = pParse->regRowid = ++pParse->nMem;
120727 reg2 = pParse->regRoot = ++pParse->nMem;
120728 reg3 = ++pParse->nMem;
120729 sqlite3VdbeAddOp3(p: v, OP_ReadCookie, p1: iDb, p2: reg3, BTREE_FILE_FORMAT);
120730 sqlite3VdbeUsesBtree(p: v, i: iDb);
120731 addr1 = sqlite3VdbeAddOp1(p: v, OP_If, p1: reg3); VdbeCoverage(v);
120732 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
120733 1 : SQLITE_MAX_FILE_FORMAT;
120734 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_FILE_FORMAT, p3: fileFormat);
120735 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_TEXT_ENCODING, ENC(db));
120736 sqlite3VdbeJumpHere(p: v, addr: addr1);
120737
120738 /* This just creates a place-holder record in the sqlite_schema table.
120739 ** The record created does not contain anything yet. It will be replaced
120740 ** by the real entry in code generated at sqlite3EndTable().
120741 **
120742 ** The rowid for the new entry is left in register pParse->regRowid.
120743 ** The root page number of the new table is left in reg pParse->regRoot.
120744 ** The rowid and root page number values are needed by the code that
120745 ** sqlite3EndTable will generate.
120746 */
120747#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
120748 if( isView || isVirtual ){
120749 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: reg2);
120750 }else
120751#endif
120752 {
120753 assert( !pParse->bReturning );
120754 pParse->u1.addrCrTab =
120755 sqlite3VdbeAddOp3(p: v, OP_CreateBtree, p1: iDb, p2: reg2, BTREE_INTKEY);
120756 }
120757 sqlite3OpenSchemaTable(p: pParse, iDb);
120758 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: 0, p2: reg1);
120759 sqlite3VdbeAddOp4(p: v, OP_Blob, p1: 6, p2: reg3, p3: 0, zP4: nullRow, P4_STATIC);
120760 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: 0, p2: reg3, p3: reg1);
120761 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
120762 sqlite3VdbeAddOp0(p: v, OP_Close);
120763 }
120764
120765 /* Normal (non-error) return. */
120766 return;
120767
120768 /* If an error occurs, we jump here */
120769begin_table_error:
120770 pParse->checkSchema = 1;
120771 sqlite3DbFree(db, p: zName);
120772 return;
120773}
120774
120775/* Set properties of a table column based on the (magical)
120776** name of the column.
120777*/
120778#if SQLITE_ENABLE_HIDDEN_COLUMNS
120779SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
120780 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
120781 pCol->colFlags |= COLFLAG_HIDDEN;
120782 if( pTab ) pTab->tabFlags |= TF_HasHidden;
120783 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
120784 pTab->tabFlags |= TF_OOOHidden;
120785 }
120786}
120787#endif
120788
120789/*
120790** Name of the special TEMP trigger used to implement RETURNING. The
120791** name begins with "sqlite_" so that it is guaranteed not to collide
120792** with any application-generated triggers.
120793*/
120794#define RETURNING_TRIGGER_NAME "sqlite_returning"
120795
120796/*
120797** Clean up the data structures associated with the RETURNING clause.
120798*/
120799static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){
120800 Hash *pHash;
120801 pHash = &(db->aDb[1].pSchema->trigHash);
120802 sqlite3HashInsert(pH: pHash, RETURNING_TRIGGER_NAME, data: 0);
120803 sqlite3ExprListDelete(db, pList: pRet->pReturnEL);
120804 sqlite3DbFree(db, p: pRet);
120805}
120806
120807/*
120808** Add the RETURNING clause to the parse currently underway.
120809**
120810** This routine creates a special TEMP trigger that will fire for each row
120811** of the DML statement. That TEMP trigger contains a single SELECT
120812** statement with a result set that is the argument of the RETURNING clause.
120813** The trigger has the Trigger.bReturning flag and an opcode of
120814** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
120815** knows to handle it specially. The TEMP trigger is automatically
120816** removed at the end of the parse.
120817**
120818** When this routine is called, we do not yet know if the RETURNING clause
120819** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
120820** RETURNING trigger instead. It will then be converted into the appropriate
120821** type on the first call to sqlite3TriggersExist().
120822*/
120823SQLITE_PRIVATE void sqlite3AddReturning(Parse *pParse, ExprList *pList){
120824 Returning *pRet;
120825 Hash *pHash;
120826 sqlite3 *db = pParse->db;
120827 if( pParse->pNewTrigger ){
120828 sqlite3ErrorMsg(pParse, zFormat: "cannot use RETURNING in a trigger");
120829 }else{
120830 assert( pParse->bReturning==0 || pParse->ifNotExists );
120831 }
120832 pParse->bReturning = 1;
120833 pRet = sqlite3DbMallocZero(db, n: sizeof(*pRet));
120834 if( pRet==0 ){
120835 sqlite3ExprListDelete(db, pList);
120836 return;
120837 }
120838 pParse->u1.pReturning = pRet;
120839 pRet->pParse = pParse;
120840 pRet->pReturnEL = pList;
120841 sqlite3ParserAddCleanup(pParse,
120842 (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet);
120843 testcase( pParse->earlyCleanup );
120844 if( db->mallocFailed ) return;
120845 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
120846 pRet->retTrig.op = TK_RETURNING;
120847 pRet->retTrig.tr_tm = TRIGGER_AFTER;
120848 pRet->retTrig.bReturning = 1;
120849 pRet->retTrig.pSchema = db->aDb[1].pSchema;
120850 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
120851 pRet->retTrig.step_list = &pRet->retTStep;
120852 pRet->retTStep.op = TK_RETURNING;
120853 pRet->retTStep.pTrig = &pRet->retTrig;
120854 pRet->retTStep.pExprList = pList;
120855 pHash = &(db->aDb[1].pSchema->trigHash);
120856 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0
120857 || pParse->nErr || pParse->ifNotExists );
120858 if( sqlite3HashInsert(pH: pHash, RETURNING_TRIGGER_NAME, data: &pRet->retTrig)
120859 ==&pRet->retTrig ){
120860 sqlite3OomFault(db);
120861 }
120862}
120863
120864/*
120865** Add a new column to the table currently being constructed.
120866**
120867** The parser calls this routine once for each column declaration
120868** in a CREATE TABLE statement. sqlite3StartTable() gets called
120869** first to get things going. Then this routine is called for each
120870** column.
120871*/
120872SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
120873 Table *p;
120874 int i;
120875 char *z;
120876 char *zType;
120877 Column *pCol;
120878 sqlite3 *db = pParse->db;
120879 u8 hName;
120880 Column *aNew;
120881 u8 eType = COLTYPE_CUSTOM;
120882 u8 szEst = 1;
120883 char affinity = SQLITE_AFF_BLOB;
120884
120885 if( (p = pParse->pNewTable)==0 ) return;
120886 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
120887 sqlite3ErrorMsg(pParse, zFormat: "too many columns on %s", p->zName);
120888 return;
120889 }
120890 if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(p: &sName);
120891
120892 /* Because keywords GENERATE ALWAYS can be converted into identifiers
120893 ** by the parser, we can sometimes end up with a typename that ends
120894 ** with "generated always". Check for this case and omit the surplus
120895 ** text. */
120896 if( sType.n>=16
120897 && sqlite3_strnicmp(zLeft: sType.z+(sType.n-6),zRight: "always",N: 6)==0
120898 ){
120899 sType.n -= 6;
120900 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
120901 if( sType.n>=9
120902 && sqlite3_strnicmp(zLeft: sType.z+(sType.n-9),zRight: "generated",N: 9)==0
120903 ){
120904 sType.n -= 9;
120905 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
120906 }
120907 }
120908
120909 /* Check for standard typenames. For standard typenames we will
120910 ** set the Column.eType field rather than storing the typename after
120911 ** the column name, in order to save space. */
120912 if( sType.n>=3 ){
120913 sqlite3DequoteToken(p: &sType);
120914 for(i=0; i<SQLITE_N_STDTYPE; i++){
120915 if( sType.n==sqlite3StdTypeLen[i]
120916 && sqlite3_strnicmp(zLeft: sType.z, zRight: sqlite3StdType[i], N: sType.n)==0
120917 ){
120918 sType.n = 0;
120919 eType = i+1;
120920 affinity = sqlite3StdTypeAffinity[i];
120921 if( affinity<=SQLITE_AFF_TEXT ) szEst = 5;
120922 break;
120923 }
120924 }
120925 }
120926
120927 z = sqlite3DbMallocRaw(db, n: (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
120928 if( z==0 ) return;
120929 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, pPtr: (void*)z, pToken: &sName);
120930 memcpy(dest: z, src: sName.z, n: sName.n);
120931 z[sName.n] = 0;
120932 sqlite3Dequote(z);
120933 hName = sqlite3StrIHash(z);
120934 for(i=0; i<p->nCol; i++){
120935 if( p->aCol[i].hName==hName && sqlite3StrICmp(zLeft: z, zRight: p->aCol[i].zCnName)==0 ){
120936 sqlite3ErrorMsg(pParse, zFormat: "duplicate column name: %s", z);
120937 sqlite3DbFree(db, p: z);
120938 return;
120939 }
120940 }
120941 aNew = sqlite3DbRealloc(db,p: p->aCol,n: ((i64)p->nCol+1)*sizeof(p->aCol[0]));
120942 if( aNew==0 ){
120943 sqlite3DbFree(db, p: z);
120944 return;
120945 }
120946 p->aCol = aNew;
120947 pCol = &p->aCol[p->nCol];
120948 memset(s: pCol, c: 0, n: sizeof(p->aCol[0]));
120949 pCol->zCnName = z;
120950 pCol->hName = hName;
120951 sqlite3ColumnPropertiesFromName(p, pCol);
120952
120953 if( sType.n==0 ){
120954 /* If there is no type specified, columns have the default affinity
120955 ** 'BLOB' with a default size of 4 bytes. */
120956 pCol->affinity = affinity;
120957 pCol->eCType = eType;
120958 pCol->szEst = szEst;
120959#ifdef SQLITE_ENABLE_SORTER_REFERENCES
120960 if( affinity==SQLITE_AFF_BLOB ){
120961 if( 4>=sqlite3GlobalConfig.szSorterRef ){
120962 pCol->colFlags |= COLFLAG_SORTERREF;
120963 }
120964 }
120965#endif
120966 }else{
120967 zType = z + sqlite3Strlen30(z) + 1;
120968 memcpy(dest: zType, src: sType.z, n: sType.n);
120969 zType[sType.n] = 0;
120970 sqlite3Dequote(z: zType);
120971 pCol->affinity = sqlite3AffinityType(zType, pCol);
120972 pCol->colFlags |= COLFLAG_HASTYPE;
120973 }
120974 p->nCol++;
120975 p->nNVCol++;
120976 pParse->constraintName.n = 0;
120977}
120978
120979/*
120980** This routine is called by the parser while in the middle of
120981** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
120982** been seen on a column. This routine sets the notNull flag on
120983** the column currently under construction.
120984*/
120985SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
120986 Table *p;
120987 Column *pCol;
120988 p = pParse->pNewTable;
120989 if( p==0 || NEVER(p->nCol<1) ) return;
120990 pCol = &p->aCol[p->nCol-1];
120991 pCol->notNull = (u8)onError;
120992 p->tabFlags |= TF_HasNotNull;
120993
120994 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
120995 ** on this column. */
120996 if( pCol->colFlags & COLFLAG_UNIQUE ){
120997 Index *pIdx;
120998 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
120999 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
121000 if( pIdx->aiColumn[0]==p->nCol-1 ){
121001 pIdx->uniqNotNull = 1;
121002 }
121003 }
121004 }
121005}
121006
121007/*
121008** Scan the column type name zType (length nType) and return the
121009** associated affinity type.
121010**
121011** This routine does a case-independent search of zType for the
121012** substrings in the following table. If one of the substrings is
121013** found, the corresponding affinity is returned. If zType contains
121014** more than one of the substrings, entries toward the top of
121015** the table take priority. For example, if zType is 'BLOBINT',
121016** SQLITE_AFF_INTEGER is returned.
121017**
121018** Substring | Affinity
121019** --------------------------------
121020** 'INT' | SQLITE_AFF_INTEGER
121021** 'CHAR' | SQLITE_AFF_TEXT
121022** 'CLOB' | SQLITE_AFF_TEXT
121023** 'TEXT' | SQLITE_AFF_TEXT
121024** 'BLOB' | SQLITE_AFF_BLOB
121025** 'REAL' | SQLITE_AFF_REAL
121026** 'FLOA' | SQLITE_AFF_REAL
121027** 'DOUB' | SQLITE_AFF_REAL
121028**
121029** If none of the substrings in the above table are found,
121030** SQLITE_AFF_NUMERIC is returned.
121031*/
121032SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
121033 u32 h = 0;
121034 char aff = SQLITE_AFF_NUMERIC;
121035 const char *zChar = 0;
121036
121037 assert( zIn!=0 );
121038 while( zIn[0] ){
121039 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
121040 zIn++;
121041 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
121042 aff = SQLITE_AFF_TEXT;
121043 zChar = zIn;
121044 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
121045 aff = SQLITE_AFF_TEXT;
121046 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
121047 aff = SQLITE_AFF_TEXT;
121048 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
121049 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
121050 aff = SQLITE_AFF_BLOB;
121051 if( zIn[0]=='(' ) zChar = zIn;
121052#ifndef SQLITE_OMIT_FLOATING_POINT
121053 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
121054 && aff==SQLITE_AFF_NUMERIC ){
121055 aff = SQLITE_AFF_REAL;
121056 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
121057 && aff==SQLITE_AFF_NUMERIC ){
121058 aff = SQLITE_AFF_REAL;
121059 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
121060 && aff==SQLITE_AFF_NUMERIC ){
121061 aff = SQLITE_AFF_REAL;
121062#endif
121063 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
121064 aff = SQLITE_AFF_INTEGER;
121065 break;
121066 }
121067 }
121068
121069 /* If pCol is not NULL, store an estimate of the field size. The
121070 ** estimate is scaled so that the size of an integer is 1. */
121071 if( pCol ){
121072 int v = 0; /* default size is approx 4 bytes */
121073 if( aff<SQLITE_AFF_NUMERIC ){
121074 if( zChar ){
121075 while( zChar[0] ){
121076 if( sqlite3Isdigit(zChar[0]) ){
121077 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
121078 sqlite3GetInt32(zNum: zChar, pValue: &v);
121079 break;
121080 }
121081 zChar++;
121082 }
121083 }else{
121084 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
121085 }
121086 }
121087#ifdef SQLITE_ENABLE_SORTER_REFERENCES
121088 if( v>=sqlite3GlobalConfig.szSorterRef ){
121089 pCol->colFlags |= COLFLAG_SORTERREF;
121090 }
121091#endif
121092 v = v/4 + 1;
121093 if( v>255 ) v = 255;
121094 pCol->szEst = v;
121095 }
121096 return aff;
121097}
121098
121099/*
121100** The expression is the default value for the most recently added column
121101** of the table currently under construction.
121102**
121103** Default value expressions must be constant. Raise an exception if this
121104** is not the case.
121105**
121106** This routine is called by the parser while in the middle of
121107** parsing a CREATE TABLE statement.
121108*/
121109SQLITE_PRIVATE void sqlite3AddDefaultValue(
121110 Parse *pParse, /* Parsing context */
121111 Expr *pExpr, /* The parsed expression of the default value */
121112 const char *zStart, /* Start of the default value text */
121113 const char *zEnd /* First character past end of default value text */
121114){
121115 Table *p;
121116 Column *pCol;
121117 sqlite3 *db = pParse->db;
121118 p = pParse->pNewTable;
121119 if( p!=0 ){
121120 int isInit = db->init.busy && db->init.iDb!=1;
121121 pCol = &(p->aCol[p->nCol-1]);
121122 if( !sqlite3ExprIsConstantOrFunction(p: pExpr, isInit) ){
121123 sqlite3ErrorMsg(pParse, zFormat: "default value of column [%s] is not constant",
121124 pCol->zCnName);
121125#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121126 }else if( pCol->colFlags & COLFLAG_GENERATED ){
121127 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
121128 testcase( pCol->colFlags & COLFLAG_STORED );
121129 sqlite3ErrorMsg(pParse, zFormat: "cannot use DEFAULT on a generated column");
121130#endif
121131 }else{
121132 /* A copy of pExpr is used instead of the original, as pExpr contains
121133 ** tokens that point to volatile memory.
121134 */
121135 Expr x, *pDfltExpr;
121136 memset(s: &x, c: 0, n: sizeof(x));
121137 x.op = TK_SPAN;
121138 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
121139 x.pLeft = pExpr;
121140 x.flags = EP_Skip;
121141 pDfltExpr = sqlite3ExprDup(db, p: &x, EXPRDUP_REDUCE);
121142 sqlite3DbFree(db, p: x.u.zToken);
121143 sqlite3ColumnSetExpr(pParse, pTab: p, pCol, pExpr: pDfltExpr);
121144 }
121145 }
121146 if( IN_RENAME_OBJECT ){
121147 sqlite3RenameExprUnmap(pParse, pExpr);
121148 }
121149 sqlite3ExprDelete(db, p: pExpr);
121150}
121151
121152/*
121153** Backwards Compatibility Hack:
121154**
121155** Historical versions of SQLite accepted strings as column names in
121156** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
121157**
121158** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
121159** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
121160**
121161** This is goofy. But to preserve backwards compatibility we continue to
121162** accept it. This routine does the necessary conversion. It converts
121163** the expression given in its argument from a TK_STRING into a TK_ID
121164** if the expression is just a TK_STRING with an optional COLLATE clause.
121165** If the expression is anything other than TK_STRING, the expression is
121166** unchanged.
121167*/
121168static void sqlite3StringToId(Expr *p){
121169 if( p->op==TK_STRING ){
121170 p->op = TK_ID;
121171 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
121172 p->pLeft->op = TK_ID;
121173 }
121174}
121175
121176/*
121177** Tag the given column as being part of the PRIMARY KEY
121178*/
121179static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
121180 pCol->colFlags |= COLFLAG_PRIMKEY;
121181#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121182 if( pCol->colFlags & COLFLAG_GENERATED ){
121183 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
121184 testcase( pCol->colFlags & COLFLAG_STORED );
121185 sqlite3ErrorMsg(pParse,
121186 zFormat: "generated columns cannot be part of the PRIMARY KEY");
121187 }
121188#endif
121189}
121190
121191/*
121192** Designate the PRIMARY KEY for the table. pList is a list of names
121193** of columns that form the primary key. If pList is NULL, then the
121194** most recently added column of the table is the primary key.
121195**
121196** A table can have at most one primary key. If the table already has
121197** a primary key (and this is the second primary key) then create an
121198** error.
121199**
121200** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
121201** then we will try to use that column as the rowid. Set the Table.iPKey
121202** field of the table under construction to be the index of the
121203** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
121204** no INTEGER PRIMARY KEY.
121205**
121206** If the key is not an INTEGER PRIMARY KEY, then create a unique
121207** index for the key. No index is created for INTEGER PRIMARY KEYs.
121208*/
121209SQLITE_PRIVATE void sqlite3AddPrimaryKey(
121210 Parse *pParse, /* Parsing context */
121211 ExprList *pList, /* List of field names to be indexed */
121212 int onError, /* What to do with a uniqueness conflict */
121213 int autoInc, /* True if the AUTOINCREMENT keyword is present */
121214 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
121215){
121216 Table *pTab = pParse->pNewTable;
121217 Column *pCol = 0;
121218 int iCol = -1, i;
121219 int nTerm;
121220 if( pTab==0 ) goto primary_key_exit;
121221 if( pTab->tabFlags & TF_HasPrimaryKey ){
121222 sqlite3ErrorMsg(pParse,
121223 zFormat: "table \"%s\" has more than one primary key", pTab->zName);
121224 goto primary_key_exit;
121225 }
121226 pTab->tabFlags |= TF_HasPrimaryKey;
121227 if( pList==0 ){
121228 iCol = pTab->nCol - 1;
121229 pCol = &pTab->aCol[iCol];
121230 makeColumnPartOfPrimaryKey(pParse, pCol);
121231 nTerm = 1;
121232 }else{
121233 nTerm = pList->nExpr;
121234 for(i=0; i<nTerm; i++){
121235 Expr *pCExpr = sqlite3ExprSkipCollate(pExpr: pList->a[i].pExpr);
121236 assert( pCExpr!=0 );
121237 sqlite3StringToId(p: pCExpr);
121238 if( pCExpr->op==TK_ID ){
121239 const char *zCName;
121240 assert( !ExprHasProperty(pCExpr, EP_IntValue) );
121241 zCName = pCExpr->u.zToken;
121242 for(iCol=0; iCol<pTab->nCol; iCol++){
121243 if( sqlite3StrICmp(zLeft: zCName, zRight: pTab->aCol[iCol].zCnName)==0 ){
121244 pCol = &pTab->aCol[iCol];
121245 makeColumnPartOfPrimaryKey(pParse, pCol);
121246 break;
121247 }
121248 }
121249 }
121250 }
121251 }
121252 if( nTerm==1
121253 && pCol
121254 && pCol->eCType==COLTYPE_INTEGER
121255 && sortOrder!=SQLITE_SO_DESC
121256 ){
121257 if( IN_RENAME_OBJECT && pList ){
121258 Expr *pCExpr = sqlite3ExprSkipCollate(pExpr: pList->a[0].pExpr);
121259 sqlite3RenameTokenRemap(pParse, pTo: &pTab->iPKey, pFrom: pCExpr);
121260 }
121261 pTab->iPKey = iCol;
121262 pTab->keyConf = (u8)onError;
121263 assert( autoInc==0 || autoInc==1 );
121264 pTab->tabFlags |= autoInc*TF_Autoincrement;
121265 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
121266 (void)sqlite3HasExplicitNulls(pParse, pList);
121267 }else if( autoInc ){
121268#ifndef SQLITE_OMIT_AUTOINCREMENT
121269 sqlite3ErrorMsg(pParse, zFormat: "AUTOINCREMENT is only allowed on an "
121270 "INTEGER PRIMARY KEY");
121271#endif
121272 }else{
121273 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
121274 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
121275 pList = 0;
121276 }
121277
121278primary_key_exit:
121279 sqlite3ExprListDelete(db: pParse->db, pList);
121280 return;
121281}
121282
121283/*
121284** Add a new CHECK constraint to the table currently under construction.
121285*/
121286SQLITE_PRIVATE void sqlite3AddCheckConstraint(
121287 Parse *pParse, /* Parsing context */
121288 Expr *pCheckExpr, /* The check expression */
121289 const char *zStart, /* Opening "(" */
121290 const char *zEnd /* Closing ")" */
121291){
121292#ifndef SQLITE_OMIT_CHECK
121293 Table *pTab = pParse->pNewTable;
121294 sqlite3 *db = pParse->db;
121295 if( pTab && !IN_DECLARE_VTAB
121296 && !sqlite3BtreeIsReadonly(p: db->aDb[db->init.iDb].pBt)
121297 ){
121298 pTab->pCheck = sqlite3ExprListAppend(pParse, pList: pTab->pCheck, pExpr: pCheckExpr);
121299 if( pParse->constraintName.n ){
121300 sqlite3ExprListSetName(pParse, pList: pTab->pCheck, pName: &pParse->constraintName, dequote: 1);
121301 }else{
121302 Token t;
121303 for(zStart++; sqlite3Isspace(zStart[0]); zStart++){}
121304 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
121305 t.z = zStart;
121306 t.n = (int)(zEnd - t.z);
121307 sqlite3ExprListSetName(pParse, pList: pTab->pCheck, pName: &t, dequote: 1);
121308 }
121309 }else
121310#endif
121311 {
121312 sqlite3ExprDelete(db: pParse->db, p: pCheckExpr);
121313 }
121314}
121315
121316/*
121317** Set the collation function of the most recently parsed table column
121318** to the CollSeq given.
121319*/
121320SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
121321 Table *p;
121322 int i;
121323 char *zColl; /* Dequoted name of collation sequence */
121324 sqlite3 *db;
121325
121326 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
121327 i = p->nCol-1;
121328 db = pParse->db;
121329 zColl = sqlite3NameFromToken(db, pName: pToken);
121330 if( !zColl ) return;
121331
121332 if( sqlite3LocateCollSeq(pParse, zName: zColl) ){
121333 Index *pIdx;
121334 sqlite3ColumnSetColl(db, pCol: &p->aCol[i], zColl);
121335
121336 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
121337 ** then an index may have been created on this column before the
121338 ** collation type was added. Correct this if it is the case.
121339 */
121340 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
121341 assert( pIdx->nKeyCol==1 );
121342 if( pIdx->aiColumn[0]==i ){
121343 pIdx->azColl[0] = sqlite3ColumnColl(pCol: &p->aCol[i]);
121344 }
121345 }
121346 }
121347 sqlite3DbFree(db, p: zColl);
121348}
121349
121350/* Change the most recently parsed column to be a GENERATED ALWAYS AS
121351** column.
121352*/
121353SQLITE_PRIVATE void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
121354#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121355 u8 eType = COLFLAG_VIRTUAL;
121356 Table *pTab = pParse->pNewTable;
121357 Column *pCol;
121358 if( pTab==0 ){
121359 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
121360 goto generated_done;
121361 }
121362 pCol = &(pTab->aCol[pTab->nCol-1]);
121363 if( IN_DECLARE_VTAB ){
121364 sqlite3ErrorMsg(pParse, zFormat: "virtual tables cannot use computed columns");
121365 goto generated_done;
121366 }
121367 if( pCol->iDflt>0 ) goto generated_error;
121368 if( pType ){
121369 if( pType->n==7 && sqlite3StrNICmp(zLeft: "virtual",zRight: pType->z,N: 7)==0 ){
121370 /* no-op */
121371 }else if( pType->n==6 && sqlite3StrNICmp(zLeft: "stored",zRight: pType->z,N: 6)==0 ){
121372 eType = COLFLAG_STORED;
121373 }else{
121374 goto generated_error;
121375 }
121376 }
121377 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
121378 pCol->colFlags |= eType;
121379 assert( TF_HasVirtual==COLFLAG_VIRTUAL );
121380 assert( TF_HasStored==COLFLAG_STORED );
121381 pTab->tabFlags |= eType;
121382 if( pCol->colFlags & COLFLAG_PRIMKEY ){
121383 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
121384 }
121385 if( ALWAYS(pExpr) && pExpr->op==TK_ID ){
121386 /* The value of a generated column needs to be a real expression, not
121387 ** just a reference to another column, in order for covering index
121388 ** optimizations to work correctly. So if the value is not an expression,
121389 ** turn it into one by adding a unary "+" operator. */
121390 pExpr = sqlite3PExpr(pParse, TK_UPLUS, pLeft: pExpr, pRight: 0);
121391 }
121392 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
121393 sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
121394 pExpr = 0;
121395 goto generated_done;
121396
121397generated_error:
121398 sqlite3ErrorMsg(pParse, zFormat: "error in generated column \"%s\"",
121399 pCol->zCnName);
121400generated_done:
121401 sqlite3ExprDelete(db: pParse->db, p: pExpr);
121402#else
121403 /* Throw and error for the GENERATED ALWAYS AS clause if the
121404 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
121405 sqlite3ErrorMsg(pParse, "generated columns not supported");
121406 sqlite3ExprDelete(pParse->db, pExpr);
121407#endif
121408}
121409
121410/*
121411** Generate code that will increment the schema cookie.
121412**
121413** The schema cookie is used to determine when the schema for the
121414** database changes. After each schema change, the cookie value
121415** changes. When a process first reads the schema it records the
121416** cookie. Thereafter, whenever it goes to access the database,
121417** it checks the cookie to make sure the schema has not changed
121418** since it was last read.
121419**
121420** This plan is not completely bullet-proof. It is possible for
121421** the schema to change multiple times and for the cookie to be
121422** set back to prior value. But schema changes are infrequent
121423** and the probability of hitting the same cookie value is only
121424** 1 chance in 2^32. So we're safe enough.
121425**
121426** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
121427** the schema-version whenever the schema changes.
121428*/
121429SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
121430 sqlite3 *db = pParse->db;
121431 Vdbe *v = pParse->pVdbe;
121432 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
121433 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_SCHEMA_VERSION,
121434 p3: (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
121435}
121436
121437/*
121438** Measure the number of characters needed to output the given
121439** identifier. The number returned includes any quotes used
121440** but does not include the null terminator.
121441**
121442** The estimate is conservative. It might be larger that what is
121443** really needed.
121444*/
121445static int identLength(const char *z){
121446 int n;
121447 for(n=0; *z; n++, z++){
121448 if( *z=='"' ){ n++; }
121449 }
121450 return n + 2;
121451}
121452
121453/*
121454** The first parameter is a pointer to an output buffer. The second
121455** parameter is a pointer to an integer that contains the offset at
121456** which to write into the output buffer. This function copies the
121457** nul-terminated string pointed to by the third parameter, zSignedIdent,
121458** to the specified offset in the buffer and updates *pIdx to refer
121459** to the first byte after the last byte written before returning.
121460**
121461** If the string zSignedIdent consists entirely of alphanumeric
121462** characters, does not begin with a digit and is not an SQL keyword,
121463** then it is copied to the output buffer exactly as it is. Otherwise,
121464** it is quoted using double-quotes.
121465*/
121466static void identPut(char *z, int *pIdx, char *zSignedIdent){
121467 unsigned char *zIdent = (unsigned char*)zSignedIdent;
121468 int i, j, needQuote;
121469 i = *pIdx;
121470
121471 for(j=0; zIdent[j]; j++){
121472 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
121473 }
121474 needQuote = sqlite3Isdigit(zIdent[0])
121475 || sqlite3KeywordCode(zIdent, j)!=TK_ID
121476 || zIdent[j]!=0
121477 || j==0;
121478
121479 if( needQuote ) z[i++] = '"';
121480 for(j=0; zIdent[j]; j++){
121481 z[i++] = zIdent[j];
121482 if( zIdent[j]=='"' ) z[i++] = '"';
121483 }
121484 if( needQuote ) z[i++] = '"';
121485 z[i] = 0;
121486 *pIdx = i;
121487}
121488
121489/*
121490** Generate a CREATE TABLE statement appropriate for the given
121491** table. Memory to hold the text of the statement is obtained
121492** from sqliteMalloc() and must be freed by the calling function.
121493*/
121494static char *createTableStmt(sqlite3 *db, Table *p){
121495 int i, k, n;
121496 char *zStmt;
121497 char *zSep, *zSep2, *zEnd;
121498 Column *pCol;
121499 n = 0;
121500 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
121501 n += identLength(z: pCol->zCnName) + 5;
121502 }
121503 n += identLength(z: p->zName);
121504 if( n<50 ){
121505 zSep = "";
121506 zSep2 = ",";
121507 zEnd = ")";
121508 }else{
121509 zSep = "\n ";
121510 zSep2 = ",\n ";
121511 zEnd = "\n)";
121512 }
121513 n += 35 + 6*p->nCol;
121514 zStmt = sqlite3DbMallocRaw(db: 0, n);
121515 if( zStmt==0 ){
121516 sqlite3OomFault(db);
121517 return 0;
121518 }
121519 sqlite3_snprintf(n, zBuf: zStmt, zFormat: "CREATE TABLE ");
121520 k = sqlite3Strlen30(z: zStmt);
121521 identPut(z: zStmt, pIdx: &k, zSignedIdent: p->zName);
121522 zStmt[k++] = '(';
121523 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
121524 static const char * const azType[] = {
121525 /* SQLITE_AFF_BLOB */ "",
121526 /* SQLITE_AFF_TEXT */ " TEXT",
121527 /* SQLITE_AFF_NUMERIC */ " NUM",
121528 /* SQLITE_AFF_INTEGER */ " INT",
121529 /* SQLITE_AFF_REAL */ " REAL",
121530 /* SQLITE_AFF_FLEXNUM */ " NUM",
121531 };
121532 int len;
121533 const char *zType;
121534
121535 sqlite3_snprintf(n: n-k, zBuf: &zStmt[k], zFormat: zSep);
121536 k += sqlite3Strlen30(z: &zStmt[k]);
121537 zSep = zSep2;
121538 identPut(z: zStmt, pIdx: &k, zSignedIdent: pCol->zCnName);
121539 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
121540 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
121541 testcase( pCol->affinity==SQLITE_AFF_BLOB );
121542 testcase( pCol->affinity==SQLITE_AFF_TEXT );
121543 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
121544 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
121545 testcase( pCol->affinity==SQLITE_AFF_REAL );
121546 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
121547
121548 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
121549 len = sqlite3Strlen30(z: zType);
121550 assert( pCol->affinity==SQLITE_AFF_BLOB
121551 || pCol->affinity==SQLITE_AFF_FLEXNUM
121552 || pCol->affinity==sqlite3AffinityType(zType, 0) );
121553 memcpy(dest: &zStmt[k], src: zType, n: len);
121554 k += len;
121555 assert( k<=n );
121556 }
121557 sqlite3_snprintf(n: n-k, zBuf: &zStmt[k], zFormat: "%s", zEnd);
121558 return zStmt;
121559}
121560
121561/*
121562** Resize an Index object to hold N columns total. Return SQLITE_OK
121563** on success and SQLITE_NOMEM on an OOM error.
121564*/
121565static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
121566 char *zExtra;
121567 int nByte;
121568 if( pIdx->nColumn>=N ) return SQLITE_OK;
121569 assert( pIdx->isResized==0 );
121570 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
121571 zExtra = sqlite3DbMallocZero(db, n: nByte);
121572 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
121573 memcpy(dest: zExtra, src: pIdx->azColl, n: sizeof(char*)*pIdx->nColumn);
121574 pIdx->azColl = (const char**)zExtra;
121575 zExtra += sizeof(char*)*N;
121576 memcpy(dest: zExtra, src: pIdx->aiRowLogEst, n: sizeof(LogEst)*(pIdx->nKeyCol+1));
121577 pIdx->aiRowLogEst = (LogEst*)zExtra;
121578 zExtra += sizeof(LogEst)*N;
121579 memcpy(dest: zExtra, src: pIdx->aiColumn, n: sizeof(i16)*pIdx->nColumn);
121580 pIdx->aiColumn = (i16*)zExtra;
121581 zExtra += sizeof(i16)*N;
121582 memcpy(dest: zExtra, src: pIdx->aSortOrder, n: pIdx->nColumn);
121583 pIdx->aSortOrder = (u8*)zExtra;
121584 pIdx->nColumn = N;
121585 pIdx->isResized = 1;
121586 return SQLITE_OK;
121587}
121588
121589/*
121590** Estimate the total row width for a table.
121591*/
121592static void estimateTableWidth(Table *pTab){
121593 unsigned wTable = 0;
121594 const Column *pTabCol;
121595 int i;
121596 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
121597 wTable += pTabCol->szEst;
121598 }
121599 if( pTab->iPKey<0 ) wTable++;
121600 pTab->szTabRow = sqlite3LogEst(x: wTable*4);
121601}
121602
121603/*
121604** Estimate the average size of a row for an index.
121605*/
121606static void estimateIndexWidth(Index *pIdx){
121607 unsigned wIndex = 0;
121608 int i;
121609 const Column *aCol = pIdx->pTable->aCol;
121610 for(i=0; i<pIdx->nColumn; i++){
121611 i16 x = pIdx->aiColumn[i];
121612 assert( x<pIdx->pTable->nCol );
121613 wIndex += x<0 ? 1 : aCol[x].szEst;
121614 }
121615 pIdx->szIdxRow = sqlite3LogEst(x: wIndex*4);
121616}
121617
121618/* Return true if column number x is any of the first nCol entries of aiCol[].
121619** This is used to determine if the column number x appears in any of the
121620** first nCol entries of an index.
121621*/
121622static int hasColumn(const i16 *aiCol, int nCol, int x){
121623 while( nCol-- > 0 ){
121624 if( x==*(aiCol++) ){
121625 return 1;
121626 }
121627 }
121628 return 0;
121629}
121630
121631/*
121632** Return true if any of the first nKey entries of index pIdx exactly
121633** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
121634** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
121635** or may not be the same index as pPk.
121636**
121637** The first nKey entries of pIdx are guaranteed to be ordinary columns,
121638** not a rowid or expression.
121639**
121640** This routine differs from hasColumn() in that both the column and the
121641** collating sequence must match for this routine, but for hasColumn() only
121642** the column name must match.
121643*/
121644static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
121645 int i, j;
121646 assert( nKey<=pIdx->nColumn );
121647 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
121648 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
121649 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
121650 assert( pPk->pTable==pIdx->pTable );
121651 testcase( pPk==pIdx );
121652 j = pPk->aiColumn[iCol];
121653 assert( j!=XN_ROWID && j!=XN_EXPR );
121654 for(i=0; i<nKey; i++){
121655 assert( pIdx->aiColumn[i]>=0 || j>=0 );
121656 if( pIdx->aiColumn[i]==j
121657 && sqlite3StrICmp(zLeft: pIdx->azColl[i], zRight: pPk->azColl[iCol])==0
121658 ){
121659 return 1;
121660 }
121661 }
121662 return 0;
121663}
121664
121665/* Recompute the colNotIdxed field of the Index.
121666**
121667** colNotIdxed is a bitmask that has a 0 bit representing each indexed
121668** columns that are within the first 63 columns of the table and a 1 for
121669** all other bits (all columns that are not in the index). The
121670** high-order bit of colNotIdxed is always 1. All unindexed columns
121671** of the table have a 1.
121672**
121673** 2019-10-24: For the purpose of this computation, virtual columns are
121674** not considered to be covered by the index, even if they are in the
121675** index, because we do not trust the logic in whereIndexExprTrans() to be
121676** able to find all instances of a reference to the indexed table column
121677** and convert them into references to the index. Hence we always want
121678** the actual table at hand in order to recompute the virtual column, if
121679** necessary.
121680**
121681** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
121682** to determine if the index is covering index.
121683*/
121684static void recomputeColumnsNotIndexed(Index *pIdx){
121685 Bitmask m = 0;
121686 int j;
121687 Table *pTab = pIdx->pTable;
121688 for(j=pIdx->nColumn-1; j>=0; j--){
121689 int x = pIdx->aiColumn[j];
121690 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
121691 testcase( x==BMS-1 );
121692 testcase( x==BMS-2 );
121693 if( x<BMS-1 ) m |= MASKBIT(x);
121694 }
121695 }
121696 pIdx->colNotIdxed = ~m;
121697 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
121698}
121699
121700/*
121701** This routine runs at the end of parsing a CREATE TABLE statement that
121702** has a WITHOUT ROWID clause. The job of this routine is to convert both
121703** internal schema data structures and the generated VDBE code so that they
121704** are appropriate for a WITHOUT ROWID table instead of a rowid table.
121705** Changes include:
121706**
121707** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
121708** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
121709** into BTREE_BLOBKEY.
121710** (3) Bypass the creation of the sqlite_schema table entry
121711** for the PRIMARY KEY as the primary key index is now
121712** identified by the sqlite_schema table entry of the table itself.
121713** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
121714** schema to the rootpage from the main table.
121715** (5) Add all table columns to the PRIMARY KEY Index object
121716** so that the PRIMARY KEY is a covering index. The surplus
121717** columns are part of KeyInfo.nAllField and are not used for
121718** sorting or lookup or uniqueness checks.
121719** (6) Replace the rowid tail on all automatically generated UNIQUE
121720** indices with the PRIMARY KEY columns.
121721**
121722** For virtual tables, only (1) is performed.
121723*/
121724static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
121725 Index *pIdx;
121726 Index *pPk;
121727 int nPk;
121728 int nExtra;
121729 int i, j;
121730 sqlite3 *db = pParse->db;
121731 Vdbe *v = pParse->pVdbe;
121732
121733 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
121734 */
121735 if( !db->init.imposterTable ){
121736 for(i=0; i<pTab->nCol; i++){
121737 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
121738 && (pTab->aCol[i].notNull==OE_None)
121739 ){
121740 pTab->aCol[i].notNull = OE_Abort;
121741 }
121742 }
121743 pTab->tabFlags |= TF_HasNotNull;
121744 }
121745
121746 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
121747 ** into BTREE_BLOBKEY.
121748 */
121749 assert( !pParse->bReturning );
121750 if( pParse->u1.addrCrTab ){
121751 assert( v );
121752 sqlite3VdbeChangeP3(p: v, addr: pParse->u1.addrCrTab, BTREE_BLOBKEY);
121753 }
121754
121755 /* Locate the PRIMARY KEY index. Or, if this table was originally
121756 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
121757 */
121758 if( pTab->iPKey>=0 ){
121759 ExprList *pList;
121760 Token ipkToken;
121761 sqlite3TokenInit(p: &ipkToken, z: pTab->aCol[pTab->iPKey].zCnName);
121762 pList = sqlite3ExprListAppend(pParse, pList: 0,
121763 pExpr: sqlite3ExprAlloc(db, TK_ID, pToken: &ipkToken, dequote: 0));
121764 if( pList==0 ){
121765 pTab->tabFlags &= ~TF_WithoutRowid;
121766 return;
121767 }
121768 if( IN_RENAME_OBJECT ){
121769 sqlite3RenameTokenRemap(pParse, pTo: pList->a[0].pExpr, pFrom: &pTab->iPKey);
121770 }
121771 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
121772 assert( pParse->pNewTable==pTab );
121773 pTab->iPKey = -1;
121774 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
121775 SQLITE_IDXTYPE_PRIMARYKEY);
121776 if( pParse->nErr ){
121777 pTab->tabFlags &= ~TF_WithoutRowid;
121778 return;
121779 }
121780 assert( db->mallocFailed==0 );
121781 pPk = sqlite3PrimaryKeyIndex(pTab);
121782 assert( pPk->nKeyCol==1 );
121783 }else{
121784 pPk = sqlite3PrimaryKeyIndex(pTab);
121785 assert( pPk!=0 );
121786
121787 /*
121788 ** Remove all redundant columns from the PRIMARY KEY. For example, change
121789 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
121790 ** code assumes the PRIMARY KEY contains no repeated columns.
121791 */
121792 for(i=j=1; i<pPk->nKeyCol; i++){
121793 if( isDupColumn(pIdx: pPk, nKey: j, pPk, iCol: i) ){
121794 pPk->nColumn--;
121795 }else{
121796 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
121797 pPk->azColl[j] = pPk->azColl[i];
121798 pPk->aSortOrder[j] = pPk->aSortOrder[i];
121799 pPk->aiColumn[j++] = pPk->aiColumn[i];
121800 }
121801 }
121802 pPk->nKeyCol = j;
121803 }
121804 assert( pPk!=0 );
121805 pPk->isCovering = 1;
121806 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
121807 nPk = pPk->nColumn = pPk->nKeyCol;
121808
121809 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
121810 ** table entry. This is only required if currently generating VDBE
121811 ** code for a CREATE TABLE (not when parsing one as part of reading
121812 ** a database schema). */
121813 if( v && pPk->tnum>0 ){
121814 assert( db->init.busy==0 );
121815 sqlite3VdbeChangeOpcode(p: v, addr: (int)pPk->tnum, OP_Goto);
121816 }
121817
121818 /* The root page of the PRIMARY KEY is the table root page */
121819 pPk->tnum = pTab->tnum;
121820
121821 /* Update the in-memory representation of all UNIQUE indices by converting
121822 ** the final rowid column into one or more columns of the PRIMARY KEY.
121823 */
121824 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121825 int n;
121826 if( IsPrimaryKeyIndex(pIdx) ) continue;
121827 for(i=n=0; i<nPk; i++){
121828 if( !isDupColumn(pIdx, nKey: pIdx->nKeyCol, pPk, iCol: i) ){
121829 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
121830 n++;
121831 }
121832 }
121833 if( n==0 ){
121834 /* This index is a superset of the primary key */
121835 pIdx->nColumn = pIdx->nKeyCol;
121836 continue;
121837 }
121838 if( resizeIndexObject(db, pIdx, N: pIdx->nKeyCol+n) ) return;
121839 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
121840 if( !isDupColumn(pIdx, nKey: pIdx->nKeyCol, pPk, iCol: i) ){
121841 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
121842 pIdx->aiColumn[j] = pPk->aiColumn[i];
121843 pIdx->azColl[j] = pPk->azColl[i];
121844 if( pPk->aSortOrder[i] ){
121845 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
121846 pIdx->bAscKeyBug = 1;
121847 }
121848 j++;
121849 }
121850 }
121851 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
121852 assert( pIdx->nColumn>=j );
121853 }
121854
121855 /* Add all table columns to the PRIMARY KEY index
121856 */
121857 nExtra = 0;
121858 for(i=0; i<pTab->nCol; i++){
121859 if( !hasColumn(aiCol: pPk->aiColumn, nCol: nPk, x: i)
121860 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
121861 }
121862 if( resizeIndexObject(db, pIdx: pPk, N: nPk+nExtra) ) return;
121863 for(i=0, j=nPk; i<pTab->nCol; i++){
121864 if( !hasColumn(aiCol: pPk->aiColumn, nCol: j, x: i)
121865 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
121866 ){
121867 assert( j<pPk->nColumn );
121868 pPk->aiColumn[j] = i;
121869 pPk->azColl[j] = sqlite3StrBINARY;
121870 j++;
121871 }
121872 }
121873 assert( pPk->nColumn==j );
121874 assert( pTab->nNVCol<=j );
121875 recomputeColumnsNotIndexed(pIdx: pPk);
121876}
121877
121878
121879#ifndef SQLITE_OMIT_VIRTUALTABLE
121880/*
121881** Return true if pTab is a virtual table and zName is a shadow table name
121882** for that virtual table.
121883*/
121884SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
121885 int nName; /* Length of zName */
121886 Module *pMod; /* Module for the virtual table */
121887
121888 if( !IsVirtual(pTab) ) return 0;
121889 nName = sqlite3Strlen30(z: pTab->zName);
121890 if( sqlite3_strnicmp(zLeft: zName, zRight: pTab->zName, N: nName)!=0 ) return 0;
121891 if( zName[nName]!='_' ) return 0;
121892 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: pTab->u.vtab.azArg[0]);
121893 if( pMod==0 ) return 0;
121894 if( pMod->pModule->iVersion<3 ) return 0;
121895 if( pMod->pModule->xShadowName==0 ) return 0;
121896 return pMod->pModule->xShadowName(zName+nName+1);
121897}
121898#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
121899
121900#ifndef SQLITE_OMIT_VIRTUALTABLE
121901/*
121902** Table pTab is a virtual table. If it the virtual table implementation
121903** exists and has an xShadowName method, then loop over all other ordinary
121904** tables within the same schema looking for shadow tables of pTab, and mark
121905** any shadow tables seen using the TF_Shadow flag.
121906*/
121907SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){
121908 int nName; /* Length of pTab->zName */
121909 Module *pMod; /* Module for the virtual table */
121910 HashElem *k; /* For looping through the symbol table */
121911
121912 assert( IsVirtual(pTab) );
121913 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: pTab->u.vtab.azArg[0]);
121914 if( pMod==0 ) return;
121915 if( NEVER(pMod->pModule==0) ) return;
121916 if( pMod->pModule->iVersion<3 ) return;
121917 if( pMod->pModule->xShadowName==0 ) return;
121918 assert( pTab->zName!=0 );
121919 nName = sqlite3Strlen30(z: pTab->zName);
121920 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
121921 Table *pOther = sqliteHashData(k);
121922 assert( pOther->zName!=0 );
121923 if( !IsOrdinaryTable(pOther) ) continue;
121924 if( pOther->tabFlags & TF_Shadow ) continue;
121925 if( sqlite3StrNICmp(zLeft: pOther->zName, zRight: pTab->zName, N: nName)==0
121926 && pOther->zName[nName]=='_'
121927 && pMod->pModule->xShadowName(pOther->zName+nName+1)
121928 ){
121929 pOther->tabFlags |= TF_Shadow;
121930 }
121931 }
121932}
121933#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
121934
121935#ifndef SQLITE_OMIT_VIRTUALTABLE
121936/*
121937** Return true if zName is a shadow table name in the current database
121938** connection.
121939**
121940** zName is temporarily modified while this routine is running, but is
121941** restored to its original value prior to this routine returning.
121942*/
121943SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
121944 char *zTail; /* Pointer to the last "_" in zName */
121945 Table *pTab; /* Table that zName is a shadow of */
121946 zTail = strrchr(s: zName, c: '_');
121947 if( zTail==0 ) return 0;
121948 *zTail = 0;
121949 pTab = sqlite3FindTable(db, zName, zDatabase: 0);
121950 *zTail = '_';
121951 if( pTab==0 ) return 0;
121952 if( !IsVirtual(pTab) ) return 0;
121953 return sqlite3IsShadowTableOf(db, pTab, zName);
121954}
121955#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
121956
121957
121958#ifdef SQLITE_DEBUG
121959/*
121960** Mark all nodes of an expression as EP_Immutable, indicating that
121961** they should not be changed. Expressions attached to a table or
121962** index definition are tagged this way to help ensure that we do
121963** not pass them into code generator routines by mistake.
121964*/
121965static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
121966 (void)pWalker;
121967 ExprSetVVAProperty(pExpr, EP_Immutable);
121968 return WRC_Continue;
121969}
121970static void markExprListImmutable(ExprList *pList){
121971 if( pList ){
121972 Walker w;
121973 memset(&w, 0, sizeof(w));
121974 w.xExprCallback = markImmutableExprStep;
121975 w.xSelectCallback = sqlite3SelectWalkNoop;
121976 w.xSelectCallback2 = 0;
121977 sqlite3WalkExprList(&w, pList);
121978 }
121979}
121980#else
121981#define markExprListImmutable(X) /* no-op */
121982#endif /* SQLITE_DEBUG */
121983
121984
121985/*
121986** This routine is called to report the final ")" that terminates
121987** a CREATE TABLE statement.
121988**
121989** The table structure that other action routines have been building
121990** is added to the internal hash tables, assuming no errors have
121991** occurred.
121992**
121993** An entry for the table is made in the schema table on disk, unless
121994** this is a temporary table or db->init.busy==1. When db->init.busy==1
121995** it means we are reading the sqlite_schema table because we just
121996** connected to the database or because the sqlite_schema table has
121997** recently changed, so the entry for this table already exists in
121998** the sqlite_schema table. We do not want to create it again.
121999**
122000** If the pSelect argument is not NULL, it means that this routine
122001** was called to create a table generated from a
122002** "CREATE TABLE ... AS SELECT ..." statement. The column names of
122003** the new table will match the result set of the SELECT.
122004*/
122005SQLITE_PRIVATE void sqlite3EndTable(
122006 Parse *pParse, /* Parse context */
122007 Token *pCons, /* The ',' token after the last column defn. */
122008 Token *pEnd, /* The ')' before options in the CREATE TABLE */
122009 u32 tabOpts, /* Extra table options. Usually 0. */
122010 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
122011){
122012 Table *p; /* The new table */
122013 sqlite3 *db = pParse->db; /* The database connection */
122014 int iDb; /* Database in which the table lives */
122015 Index *pIdx; /* An implied index of the table */
122016
122017 if( pEnd==0 && pSelect==0 ){
122018 return;
122019 }
122020 p = pParse->pNewTable;
122021 if( p==0 ) return;
122022
122023 if( pSelect==0 && sqlite3ShadowTableName(db, zName: p->zName) ){
122024 p->tabFlags |= TF_Shadow;
122025 }
122026
122027 /* If the db->init.busy is 1 it means we are reading the SQL off the
122028 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
122029 ** So do not write to the disk again. Extract the root page number
122030 ** for the table from the db->init.newTnum field. (The page number
122031 ** should have been put there by the sqliteOpenCb routine.)
122032 **
122033 ** If the root page number is 1, that means this is the sqlite_schema
122034 ** table itself. So mark it read-only.
122035 */
122036 if( db->init.busy ){
122037 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
122038 sqlite3ErrorMsg(pParse, zFormat: "");
122039 return;
122040 }
122041 p->tnum = db->init.newTnum;
122042 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
122043 }
122044
122045 /* Special processing for tables that include the STRICT keyword:
122046 **
122047 ** * Do not allow custom column datatypes. Every column must have
122048 ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
122049 **
122050 ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
122051 ** then all columns of the PRIMARY KEY must have a NOT NULL
122052 ** constraint.
122053 */
122054 if( tabOpts & TF_Strict ){
122055 int ii;
122056 p->tabFlags |= TF_Strict;
122057 for(ii=0; ii<p->nCol; ii++){
122058 Column *pCol = &p->aCol[ii];
122059 if( pCol->eCType==COLTYPE_CUSTOM ){
122060 if( pCol->colFlags & COLFLAG_HASTYPE ){
122061 sqlite3ErrorMsg(pParse,
122062 zFormat: "unknown datatype for %s.%s: \"%s\"",
122063 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, zDflt: "")
122064 );
122065 }else{
122066 sqlite3ErrorMsg(pParse, zFormat: "missing datatype for %s.%s",
122067 p->zName, pCol->zCnName);
122068 }
122069 return;
122070 }else if( pCol->eCType==COLTYPE_ANY ){
122071 pCol->affinity = SQLITE_AFF_BLOB;
122072 }
122073 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
122074 && p->iPKey!=ii
122075 && pCol->notNull == OE_None
122076 ){
122077 pCol->notNull = OE_Abort;
122078 p->tabFlags |= TF_HasNotNull;
122079 }
122080 }
122081 }
122082
122083 assert( (p->tabFlags & TF_HasPrimaryKey)==0
122084 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
122085 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
122086 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
122087
122088 /* Special processing for WITHOUT ROWID Tables */
122089 if( tabOpts & TF_WithoutRowid ){
122090 if( (p->tabFlags & TF_Autoincrement) ){
122091 sqlite3ErrorMsg(pParse,
122092 zFormat: "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
122093 return;
122094 }
122095 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
122096 sqlite3ErrorMsg(pParse, zFormat: "PRIMARY KEY missing on table %s", p->zName);
122097 return;
122098 }
122099 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
122100 convertToWithoutRowidTable(pParse, pTab: p);
122101 }
122102 iDb = sqlite3SchemaToIndex(db, p->pSchema);
122103
122104#ifndef SQLITE_OMIT_CHECK
122105 /* Resolve names in all CHECK constraint expressions.
122106 */
122107 if( p->pCheck ){
122108 sqlite3ResolveSelfReference(pParse, pTab: p, NC_IsCheck, pExpr: 0, pList: p->pCheck);
122109 if( pParse->nErr ){
122110 /* If errors are seen, delete the CHECK constraints now, else they might
122111 ** actually be used if PRAGMA writable_schema=ON is set. */
122112 sqlite3ExprListDelete(db, pList: p->pCheck);
122113 p->pCheck = 0;
122114 }else{
122115 markExprListImmutable(p->pCheck);
122116 }
122117 }
122118#endif /* !defined(SQLITE_OMIT_CHECK) */
122119#ifndef SQLITE_OMIT_GENERATED_COLUMNS
122120 if( p->tabFlags & TF_HasGenerated ){
122121 int ii, nNG = 0;
122122 testcase( p->tabFlags & TF_HasVirtual );
122123 testcase( p->tabFlags & TF_HasStored );
122124 for(ii=0; ii<p->nCol; ii++){
122125 u32 colFlags = p->aCol[ii].colFlags;
122126 if( (colFlags & COLFLAG_GENERATED)!=0 ){
122127 Expr *pX = sqlite3ColumnExpr(pTab: p, pCol: &p->aCol[ii]);
122128 testcase( colFlags & COLFLAG_VIRTUAL );
122129 testcase( colFlags & COLFLAG_STORED );
122130 if( sqlite3ResolveSelfReference(pParse, pTab: p, NC_GenCol, pExpr: pX, pList: 0) ){
122131 /* If there are errors in resolving the expression, change the
122132 ** expression to a NULL. This prevents code generators that operate
122133 ** on the expression from inserting extra parts into the expression
122134 ** tree that have been allocated from lookaside memory, which is
122135 ** illegal in a schema and will lead to errors or heap corruption
122136 ** when the database connection closes. */
122137 sqlite3ColumnSetExpr(pParse, pTab: p, pCol: &p->aCol[ii],
122138 pExpr: sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0));
122139 }
122140 }else{
122141 nNG++;
122142 }
122143 }
122144 if( nNG==0 ){
122145 sqlite3ErrorMsg(pParse, zFormat: "must have at least one non-generated column");
122146 return;
122147 }
122148 }
122149#endif
122150
122151 /* Estimate the average row size for the table and for all implied indices */
122152 estimateTableWidth(pTab: p);
122153 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
122154 estimateIndexWidth(pIdx);
122155 }
122156
122157 /* If not initializing, then create a record for the new table
122158 ** in the schema table of the database.
122159 **
122160 ** If this is a TEMPORARY table, write the entry into the auxiliary
122161 ** file instead of into the main database file.
122162 */
122163 if( !db->init.busy ){
122164 int n;
122165 Vdbe *v;
122166 char *zType; /* "view" or "table" */
122167 char *zType2; /* "VIEW" or "TABLE" */
122168 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
122169
122170 v = sqlite3GetVdbe(pParse);
122171 if( NEVER(v==0) ) return;
122172
122173 sqlite3VdbeAddOp1(p: v, OP_Close, p1: 0);
122174
122175 /*
122176 ** Initialize zType for the new view or table.
122177 */
122178 if( IsOrdinaryTable(p) ){
122179 /* A regular table */
122180 zType = "table";
122181 zType2 = "TABLE";
122182#ifndef SQLITE_OMIT_VIEW
122183 }else{
122184 /* A view */
122185 zType = "view";
122186 zType2 = "VIEW";
122187#endif
122188 }
122189
122190 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
122191 ** statement to populate the new table. The root-page number for the
122192 ** new table is in register pParse->regRoot.
122193 **
122194 ** Once the SELECT has been coded by sqlite3Select(), it is in a
122195 ** suitable state to query for the column names and types to be used
122196 ** by the new table.
122197 **
122198 ** A shared-cache write-lock is not required to write to the new table,
122199 ** as a schema-lock must have already been obtained to create it. Since
122200 ** a schema-lock excludes all other database users, the write-lock would
122201 ** be redundant.
122202 */
122203 if( pSelect ){
122204 SelectDest dest; /* Where the SELECT should store results */
122205 int regYield; /* Register holding co-routine entry-point */
122206 int addrTop; /* Top of the co-routine */
122207 int regRec; /* A record to be insert into the new table */
122208 int regRowid; /* Rowid of the next row to insert */
122209 int addrInsLoop; /* Top of the loop for inserting rows */
122210 Table *pSelTab; /* A table that describes the SELECT results */
122211
122212 if( IN_SPECIAL_PARSE ){
122213 pParse->rc = SQLITE_ERROR;
122214 pParse->nErr++;
122215 return;
122216 }
122217 regYield = ++pParse->nMem;
122218 regRec = ++pParse->nMem;
122219 regRowid = ++pParse->nMem;
122220 assert(pParse->nTab==1);
122221 sqlite3MayAbort(pParse);
122222 sqlite3VdbeAddOp3(p: v, OP_OpenWrite, p1: 1, p2: pParse->regRoot, p3: iDb);
122223 sqlite3VdbeChangeP5(p: v, OPFLAG_P2ISREG);
122224 pParse->nTab = 2;
122225 addrTop = sqlite3VdbeCurrentAddr(p: v) + 1;
122226 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: addrTop);
122227 if( pParse->nErr ) return;
122228 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
122229 if( pSelTab==0 ) return;
122230 assert( p->aCol==0 );
122231 p->nCol = p->nNVCol = pSelTab->nCol;
122232 p->aCol = pSelTab->aCol;
122233 pSelTab->nCol = 0;
122234 pSelTab->aCol = 0;
122235 sqlite3DeleteTable(db, pTable: pSelTab);
122236 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
122237 sqlite3Select(pParse, pSelect, &dest);
122238 if( pParse->nErr ) return;
122239 sqlite3VdbeEndCoroutine(v, regYield);
122240 sqlite3VdbeJumpHere(p: v, addr: addrTop - 1);
122241 addrInsLoop = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: dest.iSDParm);
122242 VdbeCoverage(v);
122243 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: dest.iSdst, p2: dest.nSdst, p3: regRec);
122244 sqlite3TableAffinity(v, p, 0);
122245 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: 1, p2: regRowid);
122246 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: 1, p2: regRec, p3: regRowid);
122247 sqlite3VdbeGoto(p: v, iDest: addrInsLoop);
122248 sqlite3VdbeJumpHere(p: v, addr: addrInsLoop);
122249 sqlite3VdbeAddOp1(p: v, OP_Close, p1: 1);
122250 }
122251
122252 /* Compute the complete text of the CREATE statement */
122253 if( pSelect ){
122254 zStmt = createTableStmt(db, p);
122255 }else{
122256 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
122257 n = (int)(pEnd2->z - pParse->sNameToken.z);
122258 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
122259 zStmt = sqlite3MPrintf(db,
122260 zFormat: "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
122261 );
122262 }
122263
122264 /* A slot for the record has already been allocated in the
122265 ** schema table. We just need to update that slot with all
122266 ** the information we've collected.
122267 */
122268 sqlite3NestedParse(pParse,
122269 zFormat: "UPDATE %Q." LEGACY_SCHEMA_TABLE
122270 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
122271 " WHERE rowid=#%d",
122272 db->aDb[iDb].zDbSName,
122273 zType,
122274 p->zName,
122275 p->zName,
122276 pParse->regRoot,
122277 zStmt,
122278 pParse->regRowid
122279 );
122280 sqlite3DbFree(db, p: zStmt);
122281 sqlite3ChangeCookie(pParse, iDb);
122282
122283#ifndef SQLITE_OMIT_AUTOINCREMENT
122284 /* Check to see if we need to create an sqlite_sequence table for
122285 ** keeping track of autoincrement keys.
122286 */
122287 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
122288 Db *pDb = &db->aDb[iDb];
122289 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
122290 if( pDb->pSchema->pSeqTab==0 ){
122291 sqlite3NestedParse(pParse,
122292 zFormat: "CREATE TABLE %Q.sqlite_sequence(name,seq)",
122293 pDb->zDbSName
122294 );
122295 }
122296 }
122297#endif
122298
122299 /* Reparse everything to update our internal data structures */
122300 sqlite3VdbeAddParseSchemaOp(p: v, iDb,
122301 zWhere: sqlite3MPrintf(db, zFormat: "tbl_name='%q' AND type!='trigger'", p->zName),p5: 0);
122302 }
122303
122304 /* Add the table to the in-memory representation of the database.
122305 */
122306 if( db->init.busy ){
122307 Table *pOld;
122308 Schema *pSchema = p->pSchema;
122309 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
122310 assert( HasRowid(p) || p->iPKey<0 );
122311 pOld = sqlite3HashInsert(pH: &pSchema->tblHash, pKey: p->zName, data: p);
122312 if( pOld ){
122313 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
122314 sqlite3OomFault(db);
122315 return;
122316 }
122317 pParse->pNewTable = 0;
122318 db->mDbFlags |= DBFLAG_SchemaChange;
122319
122320 /* If this is the magic sqlite_sequence table used by autoincrement,
122321 ** then record a pointer to this table in the main database structure
122322 ** so that INSERT can find the table easily. */
122323 assert( !pParse->nested );
122324#ifndef SQLITE_OMIT_AUTOINCREMENT
122325 if( strcmp(s1: p->zName, s2: "sqlite_sequence")==0 ){
122326 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
122327 p->pSchema->pSeqTab = p;
122328 }
122329#endif
122330 }
122331
122332#ifndef SQLITE_OMIT_ALTERTABLE
122333 if( !pSelect && IsOrdinaryTable(p) ){
122334 assert( pCons && pEnd );
122335 if( pCons->z==0 ){
122336 pCons = pEnd;
122337 }
122338 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
122339 }
122340#endif
122341}
122342
122343#ifndef SQLITE_OMIT_VIEW
122344/*
122345** The parser calls this routine in order to create a new VIEW
122346*/
122347SQLITE_PRIVATE void sqlite3CreateView(
122348 Parse *pParse, /* The parsing context */
122349 Token *pBegin, /* The CREATE token that begins the statement */
122350 Token *pName1, /* The token that holds the name of the view */
122351 Token *pName2, /* The token that holds the name of the view */
122352 ExprList *pCNames, /* Optional list of view column names */
122353 Select *pSelect, /* A SELECT statement that will become the new view */
122354 int isTemp, /* TRUE for a TEMPORARY view */
122355 int noErr /* Suppress error messages if VIEW already exists */
122356){
122357 Table *p;
122358 int n;
122359 const char *z;
122360 Token sEnd;
122361 DbFixer sFix;
122362 Token *pName = 0;
122363 int iDb;
122364 sqlite3 *db = pParse->db;
122365
122366 if( pParse->nVar>0 ){
122367 sqlite3ErrorMsg(pParse, zFormat: "parameters are not allowed in views");
122368 goto create_view_fail;
122369 }
122370 sqlite3StartTable(pParse, pName1, pName2, isTemp, isView: 1, isVirtual: 0, noErr);
122371 p = pParse->pNewTable;
122372 if( p==0 || pParse->nErr ) goto create_view_fail;
122373
122374 /* Legacy versions of SQLite allowed the use of the magic "rowid" column
122375 ** on a view, even though views do not have rowids. The following flag
122376 ** setting fixes this problem. But the fix can be disabled by compiling
122377 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
122378 ** depend upon the old buggy behavior. */
122379#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
122380 p->tabFlags |= TF_NoVisibleRowid;
122381#endif
122382
122383 sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
122384 iDb = sqlite3SchemaToIndex(db, p->pSchema);
122385 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "view", pName);
122386 if( sqlite3FixSelect(pFix: &sFix, pSelect) ) goto create_view_fail;
122387
122388 /* Make a copy of the entire SELECT statement that defines the view.
122389 ** This will force all the Expr.token.z values to be dynamically
122390 ** allocated rather than point to the input string - which means that
122391 ** they will persist after the current sqlite3_exec() call returns.
122392 */
122393 pSelect->selFlags |= SF_View;
122394 if( IN_RENAME_OBJECT ){
122395 p->u.view.pSelect = pSelect;
122396 pSelect = 0;
122397 }else{
122398 p->u.view.pSelect = sqlite3SelectDup(db, pDup: pSelect, EXPRDUP_REDUCE);
122399 }
122400 p->pCheck = sqlite3ExprListDup(db, p: pCNames, EXPRDUP_REDUCE);
122401 p->eTabType = TABTYP_VIEW;
122402 if( db->mallocFailed ) goto create_view_fail;
122403
122404 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
122405 ** the end.
122406 */
122407 sEnd = pParse->sLastToken;
122408 assert( sEnd.z[0]!=0 || sEnd.n==0 );
122409 if( sEnd.z[0]!=';' ){
122410 sEnd.z += sEnd.n;
122411 }
122412 sEnd.n = 0;
122413 n = (int)(sEnd.z - pBegin->z);
122414 assert( n>0 );
122415 z = pBegin->z;
122416 while( sqlite3Isspace(z[n-1]) ){ n--; }
122417 sEnd.z = &z[n-1];
122418 sEnd.n = 1;
122419
122420 /* Use sqlite3EndTable() to add the view to the schema table */
122421 sqlite3EndTable(pParse, pCons: 0, pEnd: &sEnd, tabOpts: 0, pSelect: 0);
122422
122423create_view_fail:
122424 sqlite3SelectDelete(db, pSelect);
122425 if( IN_RENAME_OBJECT ){
122426 sqlite3RenameExprlistUnmap(pParse, pEList: pCNames);
122427 }
122428 sqlite3ExprListDelete(db, pList: pCNames);
122429 return;
122430}
122431#endif /* SQLITE_OMIT_VIEW */
122432
122433#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
122434/*
122435** The Table structure pTable is really a VIEW. Fill in the names of
122436** the columns of the view in the pTable structure. Return the number
122437** of errors. If an error is seen leave an error message in pParse->zErrMsg.
122438*/
122439static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
122440 Table *pSelTab; /* A fake table from which we get the result set */
122441 Select *pSel; /* Copy of the SELECT that implements the view */
122442 int nErr = 0; /* Number of errors encountered */
122443 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
122444#ifndef SQLITE_OMIT_VIRTUALTABLE
122445 int rc;
122446#endif
122447#ifndef SQLITE_OMIT_AUTHORIZATION
122448 sqlite3_xauth xAuth; /* Saved xAuth pointer */
122449#endif
122450
122451 assert( pTable );
122452
122453#ifndef SQLITE_OMIT_VIRTUALTABLE
122454 if( IsVirtual(pTable) ){
122455 db->nSchemaLock++;
122456 rc = sqlite3VtabCallConnect(pParse, pTable);
122457 db->nSchemaLock--;
122458 return rc;
122459 }
122460#endif
122461
122462#ifndef SQLITE_OMIT_VIEW
122463 /* A positive nCol means the columns names for this view are
122464 ** already known. This routine is not called unless either the
122465 ** table is virtual or nCol is zero.
122466 */
122467 assert( pTable->nCol<=0 );
122468
122469 /* A negative nCol is a special marker meaning that we are currently
122470 ** trying to compute the column names. If we enter this routine with
122471 ** a negative nCol, it means two or more views form a loop, like this:
122472 **
122473 ** CREATE VIEW one AS SELECT * FROM two;
122474 ** CREATE VIEW two AS SELECT * FROM one;
122475 **
122476 ** Actually, the error above is now caught prior to reaching this point.
122477 ** But the following test is still important as it does come up
122478 ** in the following:
122479 **
122480 ** CREATE TABLE main.ex1(a);
122481 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
122482 ** SELECT * FROM temp.ex1;
122483 */
122484 if( pTable->nCol<0 ){
122485 sqlite3ErrorMsg(pParse, zFormat: "view %s is circularly defined", pTable->zName);
122486 return 1;
122487 }
122488 assert( pTable->nCol>=0 );
122489
122490 /* If we get this far, it means we need to compute the table names.
122491 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
122492 ** "*" elements in the results set of the view and will assign cursors
122493 ** to the elements of the FROM clause. But we do not want these changes
122494 ** to be permanent. So the computation is done on a copy of the SELECT
122495 ** statement that defines the view.
122496 */
122497 assert( IsView(pTable) );
122498 pSel = sqlite3SelectDup(db, pDup: pTable->u.view.pSelect, flags: 0);
122499 if( pSel ){
122500 u8 eParseMode = pParse->eParseMode;
122501 int nTab = pParse->nTab;
122502 int nSelect = pParse->nSelect;
122503 pParse->eParseMode = PARSE_MODE_NORMAL;
122504 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
122505 pTable->nCol = -1;
122506 DisableLookaside;
122507#ifndef SQLITE_OMIT_AUTHORIZATION
122508 xAuth = db->xAuth;
122509 db->xAuth = 0;
122510 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
122511 db->xAuth = xAuth;
122512#else
122513 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
122514#endif
122515 pParse->nTab = nTab;
122516 pParse->nSelect = nSelect;
122517 if( pSelTab==0 ){
122518 pTable->nCol = 0;
122519 nErr++;
122520 }else if( pTable->pCheck ){
122521 /* CREATE VIEW name(arglist) AS ...
122522 ** The names of the columns in the table are taken from
122523 ** arglist which is stored in pTable->pCheck. The pCheck field
122524 ** normally holds CHECK constraints on an ordinary table, but for
122525 ** a VIEW it holds the list of column names.
122526 */
122527 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
122528 &pTable->nCol, &pTable->aCol);
122529 if( pParse->nErr==0
122530 && pTable->nCol==pSel->pEList->nExpr
122531 ){
122532 assert( db->mallocFailed==0 );
122533 sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE);
122534 }
122535 }else{
122536 /* CREATE VIEW name AS... without an argument list. Construct
122537 ** the column names from the SELECT statement that defines the view.
122538 */
122539 assert( pTable->aCol==0 );
122540 pTable->nCol = pSelTab->nCol;
122541 pTable->aCol = pSelTab->aCol;
122542 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
122543 pSelTab->nCol = 0;
122544 pSelTab->aCol = 0;
122545 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
122546 }
122547 pTable->nNVCol = pTable->nCol;
122548 sqlite3DeleteTable(db, pTable: pSelTab);
122549 sqlite3SelectDelete(db, pSel);
122550 EnableLookaside;
122551 pParse->eParseMode = eParseMode;
122552 } else {
122553 nErr++;
122554 }
122555 pTable->pSchema->schemaFlags |= DB_UnresetViews;
122556 if( db->mallocFailed ){
122557 sqlite3DeleteColumnNames(db, pTable);
122558 }
122559#endif /* SQLITE_OMIT_VIEW */
122560 return nErr;
122561}
122562SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
122563 assert( pTable!=0 );
122564 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
122565 return viewGetColumnNames(pParse, pTable);
122566}
122567#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
122568
122569#ifndef SQLITE_OMIT_VIEW
122570/*
122571** Clear the column names from every VIEW in database idx.
122572*/
122573static void sqliteViewResetAll(sqlite3 *db, int idx){
122574 HashElem *i;
122575 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
122576 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
122577 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
122578 Table *pTab = sqliteHashData(i);
122579 if( IsView(pTab) ){
122580 sqlite3DeleteColumnNames(db, pTable: pTab);
122581 }
122582 }
122583 DbClearProperty(db, idx, DB_UnresetViews);
122584}
122585#else
122586# define sqliteViewResetAll(A,B)
122587#endif /* SQLITE_OMIT_VIEW */
122588
122589/*
122590** This function is called by the VDBE to adjust the internal schema
122591** used by SQLite when the btree layer moves a table root page. The
122592** root-page of a table or index in database iDb has changed from iFrom
122593** to iTo.
122594**
122595** Ticket #1728: The symbol table might still contain information
122596** on tables and/or indices that are the process of being deleted.
122597** If you are unlucky, one of those deleted indices or tables might
122598** have the same rootpage number as the real table or index that is
122599** being moved. So we cannot stop searching after the first match
122600** because the first match might be for one of the deleted indices
122601** or tables and not the table/index that is actually being moved.
122602** We must continue looping until all tables and indices with
122603** rootpage==iFrom have been converted to have a rootpage of iTo
122604** in order to be certain that we got the right one.
122605*/
122606#ifndef SQLITE_OMIT_AUTOVACUUM
122607SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
122608 HashElem *pElem;
122609 Hash *pHash;
122610 Db *pDb;
122611
122612 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
122613 pDb = &db->aDb[iDb];
122614 pHash = &pDb->pSchema->tblHash;
122615 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
122616 Table *pTab = sqliteHashData(pElem);
122617 if( pTab->tnum==iFrom ){
122618 pTab->tnum = iTo;
122619 }
122620 }
122621 pHash = &pDb->pSchema->idxHash;
122622 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
122623 Index *pIdx = sqliteHashData(pElem);
122624 if( pIdx->tnum==iFrom ){
122625 pIdx->tnum = iTo;
122626 }
122627 }
122628}
122629#endif
122630
122631/*
122632** Write code to erase the table with root-page iTable from database iDb.
122633** Also write code to modify the sqlite_schema table and internal schema
122634** if a root-page of another table is moved by the btree-layer whilst
122635** erasing iTable (this can happen with an auto-vacuum database).
122636*/
122637static void destroyRootPage(Parse *pParse, int iTable, int iDb){
122638 Vdbe *v = sqlite3GetVdbe(pParse);
122639 int r1 = sqlite3GetTempReg(pParse);
122640 if( iTable<2 ) sqlite3ErrorMsg(pParse, zFormat: "corrupt schema");
122641 sqlite3VdbeAddOp3(p: v, OP_Destroy, p1: iTable, p2: r1, p3: iDb);
122642 sqlite3MayAbort(pParse);
122643#ifndef SQLITE_OMIT_AUTOVACUUM
122644 /* OP_Destroy stores an in integer r1. If this integer
122645 ** is non-zero, then it is the root page number of a table moved to
122646 ** location iTable. The following code modifies the sqlite_schema table to
122647 ** reflect this.
122648 **
122649 ** The "#NNN" in the SQL is a special constant that means whatever value
122650 ** is in register NNN. See grammar rules associated with the TK_REGISTER
122651 ** token for additional information.
122652 */
122653 sqlite3NestedParse(pParse,
122654 zFormat: "UPDATE %Q." LEGACY_SCHEMA_TABLE
122655 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
122656 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
122657#endif
122658 sqlite3ReleaseTempReg(pParse, iReg: r1);
122659}
122660
122661/*
122662** Write VDBE code to erase table pTab and all associated indices on disk.
122663** Code to update the sqlite_schema tables and internal schema definitions
122664** in case a root-page belonging to another table is moved by the btree layer
122665** is also added (this can happen with an auto-vacuum database).
122666*/
122667static void destroyTable(Parse *pParse, Table *pTab){
122668 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
122669 ** is not defined), then it is important to call OP_Destroy on the
122670 ** table and index root-pages in order, starting with the numerically
122671 ** largest root-page number. This guarantees that none of the root-pages
122672 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
122673 ** following were coded:
122674 **
122675 ** OP_Destroy 4 0
122676 ** ...
122677 ** OP_Destroy 5 0
122678 **
122679 ** and root page 5 happened to be the largest root-page number in the
122680 ** database, then root page 5 would be moved to page 4 by the
122681 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
122682 ** a free-list page.
122683 */
122684 Pgno iTab = pTab->tnum;
122685 Pgno iDestroyed = 0;
122686
122687 while( 1 ){
122688 Index *pIdx;
122689 Pgno iLargest = 0;
122690
122691 if( iDestroyed==0 || iTab<iDestroyed ){
122692 iLargest = iTab;
122693 }
122694 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122695 Pgno iIdx = pIdx->tnum;
122696 assert( pIdx->pSchema==pTab->pSchema );
122697 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
122698 iLargest = iIdx;
122699 }
122700 }
122701 if( iLargest==0 ){
122702 return;
122703 }else{
122704 int iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
122705 assert( iDb>=0 && iDb<pParse->db->nDb );
122706 destroyRootPage(pParse, iTable: iLargest, iDb);
122707 iDestroyed = iLargest;
122708 }
122709 }
122710}
122711
122712/*
122713** Remove entries from the sqlite_statN tables (for N in (1,2,3))
122714** after a DROP INDEX or DROP TABLE command.
122715*/
122716static void sqlite3ClearStatTables(
122717 Parse *pParse, /* The parsing context */
122718 int iDb, /* The database number */
122719 const char *zType, /* "idx" or "tbl" */
122720 const char *zName /* Name of index or table */
122721){
122722 int i;
122723 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
122724 for(i=1; i<=4; i++){
122725 char zTab[24];
122726 sqlite3_snprintf(n: sizeof(zTab),zBuf: zTab,zFormat: "sqlite_stat%d",i);
122727 if( sqlite3FindTable(db: pParse->db, zName: zTab, zDatabase: zDbName) ){
122728 sqlite3NestedParse(pParse,
122729 zFormat: "DELETE FROM %Q.%s WHERE %s=%Q",
122730 zDbName, zTab, zType, zName
122731 );
122732 }
122733 }
122734}
122735
122736/*
122737** Generate code to drop a table.
122738*/
122739SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
122740 Vdbe *v;
122741 sqlite3 *db = pParse->db;
122742 Trigger *pTrigger;
122743 Db *pDb = &db->aDb[iDb];
122744
122745 v = sqlite3GetVdbe(pParse);
122746 assert( v!=0 );
122747 sqlite3BeginWriteOperation(pParse, 1, iDb);
122748
122749#ifndef SQLITE_OMIT_VIRTUALTABLE
122750 if( IsVirtual(pTab) ){
122751 sqlite3VdbeAddOp0(p: v, OP_VBegin);
122752 }
122753#endif
122754
122755 /* Drop all triggers associated with the table being dropped. Code
122756 ** is generated to remove entries from sqlite_schema and/or
122757 ** sqlite_temp_schema if required.
122758 */
122759 pTrigger = sqlite3TriggerList(pParse, pTab);
122760 while( pTrigger ){
122761 assert( pTrigger->pSchema==pTab->pSchema ||
122762 pTrigger->pSchema==db->aDb[1].pSchema );
122763 sqlite3DropTriggerPtr(pParse, pTrigger);
122764 pTrigger = pTrigger->pNext;
122765 }
122766
122767#ifndef SQLITE_OMIT_AUTOINCREMENT
122768 /* Remove any entries of the sqlite_sequence table associated with
122769 ** the table being dropped. This is done before the table is dropped
122770 ** at the btree level, in case the sqlite_sequence table needs to
122771 ** move as a result of the drop (can happen in auto-vacuum mode).
122772 */
122773 if( pTab->tabFlags & TF_Autoincrement ){
122774 sqlite3NestedParse(pParse,
122775 zFormat: "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
122776 pDb->zDbSName, pTab->zName
122777 );
122778 }
122779#endif
122780
122781 /* Drop all entries in the schema table that refer to the
122782 ** table. The program name loops through the schema table and deletes
122783 ** every row that refers to a table of the same name as the one being
122784 ** dropped. Triggers are handled separately because a trigger can be
122785 ** created in the temp database that refers to a table in another
122786 ** database.
122787 */
122788 sqlite3NestedParse(pParse,
122789 zFormat: "DELETE FROM %Q." LEGACY_SCHEMA_TABLE
122790 " WHERE tbl_name=%Q and type!='trigger'",
122791 pDb->zDbSName, pTab->zName);
122792 if( !isView && !IsVirtual(pTab) ){
122793 destroyTable(pParse, pTab);
122794 }
122795
122796 /* Remove the table entry from SQLite's internal schema and modify
122797 ** the schema cookie.
122798 */
122799 if( IsVirtual(pTab) ){
122800 sqlite3VdbeAddOp4(p: v, OP_VDestroy, p1: iDb, p2: 0, p3: 0, zP4: pTab->zName, p4type: 0);
122801 sqlite3MayAbort(pParse);
122802 }
122803 sqlite3VdbeAddOp4(p: v, OP_DropTable, p1: iDb, p2: 0, p3: 0, zP4: pTab->zName, p4type: 0);
122804 sqlite3ChangeCookie(pParse, iDb);
122805 sqliteViewResetAll(db, idx: iDb);
122806}
122807
122808/*
122809** Return TRUE if shadow tables should be read-only in the current
122810** context.
122811*/
122812SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db){
122813#ifndef SQLITE_OMIT_VIRTUALTABLE
122814 if( (db->flags & SQLITE_Defensive)!=0
122815 && db->pVtabCtx==0
122816 && db->nVdbeExec==0
122817 && !sqlite3VtabInSync(db)
122818 ){
122819 return 1;
122820 }
122821#endif
122822 return 0;
122823}
122824
122825/*
122826** Return true if it is not allowed to drop the given table
122827*/
122828static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
122829 if( sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)==0 ){
122830 if( sqlite3StrNICmp(zLeft: pTab->zName+7, zRight: "stat", N: 4)==0 ) return 0;
122831 if( sqlite3StrNICmp(zLeft: pTab->zName+7, zRight: "parameters", N: 10)==0 ) return 0;
122832 return 1;
122833 }
122834 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
122835 return 1;
122836 }
122837 if( pTab->tabFlags & TF_Eponymous ){
122838 return 1;
122839 }
122840 return 0;
122841}
122842
122843/*
122844** This routine is called to do the work of a DROP TABLE statement.
122845** pName is the name of the table to be dropped.
122846*/
122847SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
122848 Table *pTab;
122849 Vdbe *v;
122850 sqlite3 *db = pParse->db;
122851 int iDb;
122852
122853 if( db->mallocFailed ){
122854 goto exit_drop_table;
122855 }
122856 assert( pParse->nErr==0 );
122857 assert( pName->nSrc==1 );
122858 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
122859 if( noErr ) db->suppressErr++;
122860 assert( isView==0 || isView==LOCATE_VIEW );
122861 pTab = sqlite3LocateTableItem(pParse, flags: isView, p: &pName->a[0]);
122862 if( noErr ) db->suppressErr--;
122863
122864 if( pTab==0 ){
122865 if( noErr ){
122866 sqlite3CodeVerifyNamedSchema(pParse, zDb: pName->a[0].zDatabase);
122867 sqlite3ForceNotReadOnly(pParse);
122868 }
122869 goto exit_drop_table;
122870 }
122871 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122872 assert( iDb>=0 && iDb<db->nDb );
122873
122874 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
122875 ** it is initialized.
122876 */
122877 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
122878 goto exit_drop_table;
122879 }
122880#ifndef SQLITE_OMIT_AUTHORIZATION
122881 {
122882 int code;
122883 const char *zTab = SCHEMA_TABLE(iDb);
122884 const char *zDb = db->aDb[iDb].zDbSName;
122885 const char *zArg2 = 0;
122886 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: zTab, zArg2: 0, zArg3: zDb)){
122887 goto exit_drop_table;
122888 }
122889 if( isView ){
122890 if( !OMIT_TEMPDB && iDb==1 ){
122891 code = SQLITE_DROP_TEMP_VIEW;
122892 }else{
122893 code = SQLITE_DROP_VIEW;
122894 }
122895#ifndef SQLITE_OMIT_VIRTUALTABLE
122896 }else if( IsVirtual(pTab) ){
122897 code = SQLITE_DROP_VTABLE;
122898 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
122899#endif
122900 }else{
122901 if( !OMIT_TEMPDB && iDb==1 ){
122902 code = SQLITE_DROP_TEMP_TABLE;
122903 }else{
122904 code = SQLITE_DROP_TABLE;
122905 }
122906 }
122907 if( sqlite3AuthCheck(pParse, code, zArg1: pTab->zName, zArg2, zArg3: zDb) ){
122908 goto exit_drop_table;
122909 }
122910 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: pTab->zName, zArg2: 0, zArg3: zDb) ){
122911 goto exit_drop_table;
122912 }
122913 }
122914#endif
122915 if( tableMayNotBeDropped(db, pTab) ){
122916 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be dropped", pTab->zName);
122917 goto exit_drop_table;
122918 }
122919
122920#ifndef SQLITE_OMIT_VIEW
122921 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
122922 ** on a table.
122923 */
122924 if( isView && !IsView(pTab) ){
122925 sqlite3ErrorMsg(pParse, zFormat: "use DROP TABLE to delete table %s", pTab->zName);
122926 goto exit_drop_table;
122927 }
122928 if( !isView && IsView(pTab) ){
122929 sqlite3ErrorMsg(pParse, zFormat: "use DROP VIEW to delete view %s", pTab->zName);
122930 goto exit_drop_table;
122931 }
122932#endif
122933
122934 /* Generate code to remove the table from the schema table
122935 ** on disk.
122936 */
122937 v = sqlite3GetVdbe(pParse);
122938 if( v ){
122939 sqlite3BeginWriteOperation(pParse, 1, iDb);
122940 if( !isView ){
122941 sqlite3ClearStatTables(pParse, iDb, zType: "tbl", zName: pTab->zName);
122942 sqlite3FkDropTable(pParse, pName, pTab);
122943 }
122944 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
122945 }
122946
122947exit_drop_table:
122948 sqlite3SrcListDelete(db, pName);
122949}
122950
122951/*
122952** This routine is called to create a new foreign key on the table
122953** currently under construction. pFromCol determines which columns
122954** in the current table point to the foreign key. If pFromCol==0 then
122955** connect the key to the last column inserted. pTo is the name of
122956** the table referred to (a.k.a the "parent" table). pToCol is a list
122957** of tables in the parent pTo table. flags contains all
122958** information about the conflict resolution algorithms specified
122959** in the ON DELETE, ON UPDATE and ON INSERT clauses.
122960**
122961** An FKey structure is created and added to the table currently
122962** under construction in the pParse->pNewTable field.
122963**
122964** The foreign key is set for IMMEDIATE processing. A subsequent call
122965** to sqlite3DeferForeignKey() might change this to DEFERRED.
122966*/
122967SQLITE_PRIVATE void sqlite3CreateForeignKey(
122968 Parse *pParse, /* Parsing context */
122969 ExprList *pFromCol, /* Columns in this table that point to other table */
122970 Token *pTo, /* Name of the other table */
122971 ExprList *pToCol, /* Columns in the other table */
122972 int flags /* Conflict resolution algorithms. */
122973){
122974 sqlite3 *db = pParse->db;
122975#ifndef SQLITE_OMIT_FOREIGN_KEY
122976 FKey *pFKey = 0;
122977 FKey *pNextTo;
122978 Table *p = pParse->pNewTable;
122979 i64 nByte;
122980 int i;
122981 int nCol;
122982 char *z;
122983
122984 assert( pTo!=0 );
122985 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
122986 if( pFromCol==0 ){
122987 int iCol = p->nCol-1;
122988 if( NEVER(iCol<0) ) goto fk_end;
122989 if( pToCol && pToCol->nExpr!=1 ){
122990 sqlite3ErrorMsg(pParse, zFormat: "foreign key on %s"
122991 " should reference only one column of table %T",
122992 p->aCol[iCol].zCnName, pTo);
122993 goto fk_end;
122994 }
122995 nCol = 1;
122996 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
122997 sqlite3ErrorMsg(pParse,
122998 zFormat: "number of columns in foreign key does not match the number of "
122999 "columns in the referenced table");
123000 goto fk_end;
123001 }else{
123002 nCol = pFromCol->nExpr;
123003 }
123004 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
123005 if( pToCol ){
123006 for(i=0; i<pToCol->nExpr; i++){
123007 nByte += sqlite3Strlen30(z: pToCol->a[i].zEName) + 1;
123008 }
123009 }
123010 pFKey = sqlite3DbMallocZero(db, n: nByte );
123011 if( pFKey==0 ){
123012 goto fk_end;
123013 }
123014 pFKey->pFrom = p;
123015 assert( IsOrdinaryTable(p) );
123016 pFKey->pNextFrom = p->u.tab.pFKey;
123017 z = (char*)&pFKey->aCol[nCol];
123018 pFKey->zTo = z;
123019 if( IN_RENAME_OBJECT ){
123020 sqlite3RenameTokenMap(pParse, pPtr: (void*)z, pToken: pTo);
123021 }
123022 memcpy(dest: z, src: pTo->z, n: pTo->n);
123023 z[pTo->n] = 0;
123024 sqlite3Dequote(z);
123025 z += pTo->n+1;
123026 pFKey->nCol = nCol;
123027 if( pFromCol==0 ){
123028 pFKey->aCol[0].iFrom = p->nCol-1;
123029 }else{
123030 for(i=0; i<nCol; i++){
123031 int j;
123032 for(j=0; j<p->nCol; j++){
123033 if( sqlite3StrICmp(zLeft: p->aCol[j].zCnName, zRight: pFromCol->a[i].zEName)==0 ){
123034 pFKey->aCol[i].iFrom = j;
123035 break;
123036 }
123037 }
123038 if( j>=p->nCol ){
123039 sqlite3ErrorMsg(pParse,
123040 zFormat: "unknown column \"%s\" in foreign key definition",
123041 pFromCol->a[i].zEName);
123042 goto fk_end;
123043 }
123044 if( IN_RENAME_OBJECT ){
123045 sqlite3RenameTokenRemap(pParse, pTo: &pFKey->aCol[i], pFrom: pFromCol->a[i].zEName);
123046 }
123047 }
123048 }
123049 if( pToCol ){
123050 for(i=0; i<nCol; i++){
123051 int n = sqlite3Strlen30(z: pToCol->a[i].zEName);
123052 pFKey->aCol[i].zCol = z;
123053 if( IN_RENAME_OBJECT ){
123054 sqlite3RenameTokenRemap(pParse, pTo: z, pFrom: pToCol->a[i].zEName);
123055 }
123056 memcpy(dest: z, src: pToCol->a[i].zEName, n: n);
123057 z[n] = 0;
123058 z += n+1;
123059 }
123060 }
123061 pFKey->isDeferred = 0;
123062 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
123063 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
123064
123065 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
123066 pNextTo = (FKey *)sqlite3HashInsert(pH: &p->pSchema->fkeyHash,
123067 pKey: pFKey->zTo, data: (void *)pFKey
123068 );
123069 if( pNextTo==pFKey ){
123070 sqlite3OomFault(db);
123071 goto fk_end;
123072 }
123073 if( pNextTo ){
123074 assert( pNextTo->pPrevTo==0 );
123075 pFKey->pNextTo = pNextTo;
123076 pNextTo->pPrevTo = pFKey;
123077 }
123078
123079 /* Link the foreign key to the table as the last step.
123080 */
123081 assert( IsOrdinaryTable(p) );
123082 p->u.tab.pFKey = pFKey;
123083 pFKey = 0;
123084
123085fk_end:
123086 sqlite3DbFree(db, p: pFKey);
123087#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
123088 sqlite3ExprListDelete(db, pList: pFromCol);
123089 sqlite3ExprListDelete(db, pList: pToCol);
123090}
123091
123092/*
123093** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
123094** clause is seen as part of a foreign key definition. The isDeferred
123095** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
123096** The behavior of the most recently created foreign key is adjusted
123097** accordingly.
123098*/
123099SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
123100#ifndef SQLITE_OMIT_FOREIGN_KEY
123101 Table *pTab;
123102 FKey *pFKey;
123103 if( (pTab = pParse->pNewTable)==0 ) return;
123104 if( NEVER(!IsOrdinaryTable(pTab)) ) return;
123105 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
123106 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
123107 pFKey->isDeferred = (u8)isDeferred;
123108#endif
123109}
123110
123111/*
123112** Generate code that will erase and refill index *pIdx. This is
123113** used to initialize a newly created index or to recompute the
123114** content of an index in response to a REINDEX command.
123115**
123116** if memRootPage is not negative, it means that the index is newly
123117** created. The register specified by memRootPage contains the
123118** root page number of the index. If memRootPage is negative, then
123119** the index already exists and must be cleared before being refilled and
123120** the root page number of the index is taken from pIndex->tnum.
123121*/
123122static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
123123 Table *pTab = pIndex->pTable; /* The table that is indexed */
123124 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
123125 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
123126 int iSorter; /* Cursor opened by OpenSorter (if in use) */
123127 int addr1; /* Address of top of loop */
123128 int addr2; /* Address to jump to for next iteration */
123129 Pgno tnum; /* Root page of index */
123130 int iPartIdxLabel; /* Jump to this label to skip a row */
123131 Vdbe *v; /* Generate code into this virtual machine */
123132 KeyInfo *pKey; /* KeyInfo for index */
123133 int regRecord; /* Register holding assembled index record */
123134 sqlite3 *db = pParse->db; /* The database connection */
123135 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
123136
123137#ifndef SQLITE_OMIT_AUTHORIZATION
123138 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, zArg1: pIndex->zName, zArg2: 0,
123139 zArg3: db->aDb[iDb].zDbSName ) ){
123140 return;
123141 }
123142#endif
123143
123144 /* Require a write-lock on the table to perform this operation */
123145 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 1, zName: pTab->zName);
123146
123147 v = sqlite3GetVdbe(pParse);
123148 if( v==0 ) return;
123149 if( memRootPage>=0 ){
123150 tnum = (Pgno)memRootPage;
123151 }else{
123152 tnum = pIndex->tnum;
123153 }
123154 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
123155 assert( pKey!=0 || pParse->nErr );
123156
123157 /* Open the sorter cursor if we are to use one. */
123158 iSorter = pParse->nTab++;
123159 sqlite3VdbeAddOp4(p: v, OP_SorterOpen, p1: iSorter, p2: 0, p3: pIndex->nKeyCol, zP4: (char*)
123160 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
123161
123162 /* Open the table. Loop through all rows of the table, inserting index
123163 ** records into the sorter. */
123164 sqlite3OpenTable(pParse, iCur: iTab, iDb, pTab, OP_OpenRead);
123165 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iTab, p2: 0); VdbeCoverage(v);
123166 regRecord = sqlite3GetTempReg(pParse);
123167 sqlite3MultiWrite(pParse);
123168
123169 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
123170 sqlite3VdbeAddOp2(p: v, OP_SorterInsert, p1: iSorter, p2: regRecord);
123171 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
123172 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iTab, p2: addr1+1); VdbeCoverage(v);
123173 sqlite3VdbeJumpHere(p: v, addr: addr1);
123174 if( memRootPage<0 ) sqlite3VdbeAddOp2(p: v, OP_Clear, p1: tnum, p2: iDb);
123175 sqlite3VdbeAddOp4(p: v, OP_OpenWrite, p1: iIdx, p2: (int)tnum, p3: iDb,
123176 zP4: (char *)pKey, P4_KEYINFO);
123177 sqlite3VdbeChangeP5(p: v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
123178
123179 addr1 = sqlite3VdbeAddOp2(p: v, OP_SorterSort, p1: iSorter, p2: 0); VdbeCoverage(v);
123180 if( IsUniqueIndex(pIndex) ){
123181 int j2 = sqlite3VdbeGoto(p: v, iDest: 1);
123182 addr2 = sqlite3VdbeCurrentAddr(p: v);
123183 sqlite3VdbeVerifyAbortable(v, OE_Abort);
123184 sqlite3VdbeAddOp4Int(p: v, OP_SorterCompare, p1: iSorter, p2: j2, p3: regRecord,
123185 p4: pIndex->nKeyCol); VdbeCoverage(v);
123186 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
123187 sqlite3VdbeJumpHere(p: v, addr: j2);
123188 }else{
123189 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
123190 ** abort. The exception is if one of the indexed expressions contains a
123191 ** user function that throws an exception when it is evaluated. But the
123192 ** overhead of adding a statement journal to a CREATE INDEX statement is
123193 ** very small (since most of the pages written do not contain content that
123194 ** needs to be restored if the statement aborts), so we call
123195 ** sqlite3MayAbort() for all CREATE INDEX statements. */
123196 sqlite3MayAbort(pParse);
123197 addr2 = sqlite3VdbeCurrentAddr(p: v);
123198 }
123199 sqlite3VdbeAddOp3(p: v, OP_SorterData, p1: iSorter, p2: regRecord, p3: iIdx);
123200 if( !pIndex->bAscKeyBug ){
123201 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
123202 ** faster by avoiding unnecessary seeks. But the optimization does
123203 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
123204 ** with DESC primary keys, since those indexes have there keys in
123205 ** a different order from the main table.
123206 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
123207 */
123208 sqlite3VdbeAddOp1(p: v, OP_SeekEnd, p1: iIdx);
123209 }
123210 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: iIdx, p2: regRecord);
123211 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
123212 sqlite3ReleaseTempReg(pParse, iReg: regRecord);
123213 sqlite3VdbeAddOp2(p: v, OP_SorterNext, p1: iSorter, p2: addr2); VdbeCoverage(v);
123214 sqlite3VdbeJumpHere(p: v, addr: addr1);
123215
123216 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iTab);
123217 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iIdx);
123218 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iSorter);
123219}
123220
123221/*
123222** Allocate heap space to hold an Index object with nCol columns.
123223**
123224** Increase the allocation size to provide an extra nExtra bytes
123225** of 8-byte aligned space after the Index object and return a
123226** pointer to this extra space in *ppExtra.
123227*/
123228SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
123229 sqlite3 *db, /* Database connection */
123230 i16 nCol, /* Total number of columns in the index */
123231 int nExtra, /* Number of bytes of extra space to alloc */
123232 char **ppExtra /* Pointer to the "extra" space */
123233){
123234 Index *p; /* Allocated index object */
123235 int nByte; /* Bytes of space for Index object + arrays */
123236
123237 nByte = ROUND8(sizeof(Index)) + /* Index structure */
123238 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
123239 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
123240 sizeof(i16)*nCol + /* Index.aiColumn */
123241 sizeof(u8)*nCol); /* Index.aSortOrder */
123242 p = sqlite3DbMallocZero(db, n: nByte + nExtra);
123243 if( p ){
123244 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
123245 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
123246 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
123247 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
123248 p->aSortOrder = (u8*)pExtra;
123249 p->nColumn = nCol;
123250 p->nKeyCol = nCol - 1;
123251 *ppExtra = ((char*)p) + nByte;
123252 }
123253 return p;
123254}
123255
123256/*
123257** If expression list pList contains an expression that was parsed with
123258** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
123259** pParse and return non-zero. Otherwise, return zero.
123260*/
123261SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
123262 if( pList ){
123263 int i;
123264 for(i=0; i<pList->nExpr; i++){
123265 if( pList->a[i].fg.bNulls ){
123266 u8 sf = pList->a[i].fg.sortFlags;
123267 sqlite3ErrorMsg(pParse, zFormat: "unsupported use of NULLS %s",
123268 (sf==0 || sf==3) ? "FIRST" : "LAST"
123269 );
123270 return 1;
123271 }
123272 }
123273 }
123274 return 0;
123275}
123276
123277/*
123278** Create a new index for an SQL table. pName1.pName2 is the name of the index
123279** and pTblList is the name of the table that is to be indexed. Both will
123280** be NULL for a primary key or an index that is created to satisfy a
123281** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
123282** as the table to be indexed. pParse->pNewTable is a table that is
123283** currently being constructed by a CREATE TABLE statement.
123284**
123285** pList is a list of columns to be indexed. pList will be NULL if this
123286** is a primary key or unique-constraint on the most recent column added
123287** to the table currently under construction.
123288*/
123289SQLITE_PRIVATE void sqlite3CreateIndex(
123290 Parse *pParse, /* All information about this parse */
123291 Token *pName1, /* First part of index name. May be NULL */
123292 Token *pName2, /* Second part of index name. May be NULL */
123293 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
123294 ExprList *pList, /* A list of columns to be indexed */
123295 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
123296 Token *pStart, /* The CREATE token that begins this statement */
123297 Expr *pPIWhere, /* WHERE clause for partial indices */
123298 int sortOrder, /* Sort order of primary key when pList==NULL */
123299 int ifNotExist, /* Omit error if index already exists */
123300 u8 idxType /* The index type */
123301){
123302 Table *pTab = 0; /* Table to be indexed */
123303 Index *pIndex = 0; /* The index to be created */
123304 char *zName = 0; /* Name of the index */
123305 int nName; /* Number of characters in zName */
123306 int i, j;
123307 DbFixer sFix; /* For assigning database names to pTable */
123308 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
123309 sqlite3 *db = pParse->db;
123310 Db *pDb; /* The specific table containing the indexed database */
123311 int iDb; /* Index of the database that is being written */
123312 Token *pName = 0; /* Unqualified name of the index to create */
123313 struct ExprList_item *pListItem; /* For looping over pList */
123314 int nExtra = 0; /* Space allocated for zExtra[] */
123315 int nExtraCol; /* Number of extra columns needed */
123316 char *zExtra = 0; /* Extra space after the Index object */
123317 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
123318
123319 assert( db->pParse==pParse );
123320 if( pParse->nErr ){
123321 goto exit_create_index;
123322 }
123323 assert( db->mallocFailed==0 );
123324 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
123325 goto exit_create_index;
123326 }
123327 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
123328 goto exit_create_index;
123329 }
123330 if( sqlite3HasExplicitNulls(pParse, pList) ){
123331 goto exit_create_index;
123332 }
123333
123334 /*
123335 ** Find the table that is to be indexed. Return early if not found.
123336 */
123337 if( pTblName!=0 ){
123338
123339 /* Use the two-part index name to determine the database
123340 ** to search for the table. 'Fix' the table name to this db
123341 ** before looking up the table.
123342 */
123343 assert( pName1 && pName2 );
123344 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
123345 if( iDb<0 ) goto exit_create_index;
123346 assert( pName && pName->z );
123347
123348#ifndef SQLITE_OMIT_TEMPDB
123349 /* If the index name was unqualified, check if the table
123350 ** is a temp table. If so, set the database to 1. Do not do this
123351 ** if initializing a database schema.
123352 */
123353 if( !db->init.busy ){
123354 pTab = sqlite3SrcListLookup(pParse, pTblName);
123355 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
123356 iDb = 1;
123357 }
123358 }
123359#endif
123360
123361 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "index", pName);
123362 if( sqlite3FixSrcList(pFix: &sFix, pList: pTblName) ){
123363 /* Because the parser constructs pTblName from a single identifier,
123364 ** sqlite3FixSrcList can never fail. */
123365 assert(0);
123366 }
123367 pTab = sqlite3LocateTableItem(pParse, flags: 0, p: &pTblName->a[0]);
123368 assert( db->mallocFailed==0 || pTab==0 );
123369 if( pTab==0 ) goto exit_create_index;
123370 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
123371 sqlite3ErrorMsg(pParse,
123372 zFormat: "cannot create a TEMP index on non-TEMP table \"%s\"",
123373 pTab->zName);
123374 goto exit_create_index;
123375 }
123376 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
123377 }else{
123378 assert( pName==0 );
123379 assert( pStart==0 );
123380 pTab = pParse->pNewTable;
123381 if( !pTab ) goto exit_create_index;
123382 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
123383 }
123384 pDb = &db->aDb[iDb];
123385
123386 assert( pTab!=0 );
123387 if( sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)==0
123388 && db->init.busy==0
123389 && pTblName!=0
123390#if SQLITE_USER_AUTHENTICATION
123391 && sqlite3UserAuthTable(pTab->zName)==0
123392#endif
123393 ){
123394 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be indexed", pTab->zName);
123395 goto exit_create_index;
123396 }
123397#ifndef SQLITE_OMIT_VIEW
123398 if( IsView(pTab) ){
123399 sqlite3ErrorMsg(pParse, zFormat: "views may not be indexed");
123400 goto exit_create_index;
123401 }
123402#endif
123403#ifndef SQLITE_OMIT_VIRTUALTABLE
123404 if( IsVirtual(pTab) ){
123405 sqlite3ErrorMsg(pParse, zFormat: "virtual tables may not be indexed");
123406 goto exit_create_index;
123407 }
123408#endif
123409
123410 /*
123411 ** Find the name of the index. Make sure there is not already another
123412 ** index or table with the same name.
123413 **
123414 ** Exception: If we are reading the names of permanent indices from the
123415 ** sqlite_schema table (because some other process changed the schema) and
123416 ** one of the index names collides with the name of a temporary table or
123417 ** index, then we will continue to process this index.
123418 **
123419 ** If pName==0 it means that we are
123420 ** dealing with a primary key or UNIQUE constraint. We have to invent our
123421 ** own name.
123422 */
123423 if( pName ){
123424 zName = sqlite3NameFromToken(db, pName);
123425 if( zName==0 ) goto exit_create_index;
123426 assert( pName->z!=0 );
123427 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,zType: "index",zTblName: pTab->zName) ){
123428 goto exit_create_index;
123429 }
123430 if( !IN_RENAME_OBJECT ){
123431 if( !db->init.busy ){
123432 if( sqlite3FindTable(db, zName, zDatabase: pDb->zDbSName)!=0 ){
123433 sqlite3ErrorMsg(pParse, zFormat: "there is already a table named %s", zName);
123434 goto exit_create_index;
123435 }
123436 }
123437 if( sqlite3FindIndex(db, zName, zDb: pDb->zDbSName)!=0 ){
123438 if( !ifNotExist ){
123439 sqlite3ErrorMsg(pParse, zFormat: "index %s already exists", zName);
123440 }else{
123441 assert( !db->init.busy );
123442 sqlite3CodeVerifySchema(pParse, iDb);
123443 sqlite3ForceNotReadOnly(pParse);
123444 }
123445 goto exit_create_index;
123446 }
123447 }
123448 }else{
123449 int n;
123450 Index *pLoop;
123451 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
123452 zName = sqlite3MPrintf(db, zFormat: "sqlite_autoindex_%s_%d", pTab->zName, n);
123453 if( zName==0 ){
123454 goto exit_create_index;
123455 }
123456
123457 /* Automatic index names generated from within sqlite3_declare_vtab()
123458 ** must have names that are distinct from normal automatic index names.
123459 ** The following statement converts "sqlite3_autoindex..." into
123460 ** "sqlite3_butoindex..." in order to make the names distinct.
123461 ** The "vtab_err.test" test demonstrates the need of this statement. */
123462 if( IN_SPECIAL_PARSE ) zName[7]++;
123463 }
123464
123465 /* Check for authorization to create an index.
123466 */
123467#ifndef SQLITE_OMIT_AUTHORIZATION
123468 if( !IN_RENAME_OBJECT ){
123469 const char *zDb = pDb->zDbSName;
123470 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), zArg2: 0, zArg3: zDb) ){
123471 goto exit_create_index;
123472 }
123473 i = SQLITE_CREATE_INDEX;
123474 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
123475 if( sqlite3AuthCheck(pParse, code: i, zArg1: zName, zArg2: pTab->zName, zArg3: zDb) ){
123476 goto exit_create_index;
123477 }
123478 }
123479#endif
123480
123481 /* If pList==0, it means this routine was called to make a primary
123482 ** key out of the last column added to the table under construction.
123483 ** So create a fake list to simulate this.
123484 */
123485 if( pList==0 ){
123486 Token prevCol;
123487 Column *pCol = &pTab->aCol[pTab->nCol-1];
123488 pCol->colFlags |= COLFLAG_UNIQUE;
123489 sqlite3TokenInit(p: &prevCol, z: pCol->zCnName);
123490 pList = sqlite3ExprListAppend(pParse, pList: 0,
123491 pExpr: sqlite3ExprAlloc(db, TK_ID, pToken: &prevCol, dequote: 0));
123492 if( pList==0 ) goto exit_create_index;
123493 assert( pList->nExpr==1 );
123494 sqlite3ExprListSetSortOrder(p: pList, iSortOrder: sortOrder, SQLITE_SO_UNDEFINED);
123495 }else{
123496 sqlite3ExprListCheckLength(pParse, pEList: pList, zObject: "index");
123497 if( pParse->nErr ) goto exit_create_index;
123498 }
123499
123500 /* Figure out how many bytes of space are required to store explicitly
123501 ** specified collation sequence names.
123502 */
123503 for(i=0; i<pList->nExpr; i++){
123504 Expr *pExpr = pList->a[i].pExpr;
123505 assert( pExpr!=0 );
123506 if( pExpr->op==TK_COLLATE ){
123507 assert( !ExprHasProperty(pExpr, EP_IntValue) );
123508 nExtra += (1 + sqlite3Strlen30(z: pExpr->u.zToken));
123509 }
123510 }
123511
123512 /*
123513 ** Allocate the index structure.
123514 */
123515 nName = sqlite3Strlen30(z: zName);
123516 nExtraCol = pPk ? pPk->nKeyCol : 1;
123517 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
123518 pIndex = sqlite3AllocateIndexObject(db, nCol: pList->nExpr + nExtraCol,
123519 nExtra: nName + nExtra + 1, ppExtra: &zExtra);
123520 if( db->mallocFailed ){
123521 goto exit_create_index;
123522 }
123523 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
123524 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
123525 pIndex->zName = zExtra;
123526 zExtra += nName + 1;
123527 memcpy(dest: pIndex->zName, src: zName, n: nName+1);
123528 pIndex->pTable = pTab;
123529 pIndex->onError = (u8)onError;
123530 pIndex->uniqNotNull = onError!=OE_None;
123531 pIndex->idxType = idxType;
123532 pIndex->pSchema = db->aDb[iDb].pSchema;
123533 pIndex->nKeyCol = pList->nExpr;
123534 if( pPIWhere ){
123535 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pExpr: pPIWhere, pList: 0);
123536 pIndex->pPartIdxWhere = pPIWhere;
123537 pPIWhere = 0;
123538 }
123539 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
123540
123541 /* Check to see if we should honor DESC requests on index columns
123542 */
123543 if( pDb->pSchema->file_format>=4 ){
123544 sortOrderMask = -1; /* Honor DESC */
123545 }else{
123546 sortOrderMask = 0; /* Ignore DESC */
123547 }
123548
123549 /* Analyze the list of expressions that form the terms of the index and
123550 ** report any errors. In the common case where the expression is exactly
123551 ** a table column, store that column in aiColumn[]. For general expressions,
123552 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
123553 **
123554 ** TODO: Issue a warning if two or more columns of the index are identical.
123555 ** TODO: Issue a warning if the table primary key is used as part of the
123556 ** index key.
123557 */
123558 pListItem = pList->a;
123559 if( IN_RENAME_OBJECT ){
123560 pIndex->aColExpr = pList;
123561 pList = 0;
123562 }
123563 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
123564 Expr *pCExpr; /* The i-th index expression */
123565 int requestedSortOrder; /* ASC or DESC on the i-th expression */
123566 const char *zColl; /* Collation sequence name */
123567
123568 sqlite3StringToId(p: pListItem->pExpr);
123569 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pExpr: pListItem->pExpr, pList: 0);
123570 if( pParse->nErr ) goto exit_create_index;
123571 pCExpr = sqlite3ExprSkipCollate(pExpr: pListItem->pExpr);
123572 if( pCExpr->op!=TK_COLUMN ){
123573 if( pTab==pParse->pNewTable ){
123574 sqlite3ErrorMsg(pParse, zFormat: "expressions prohibited in PRIMARY KEY and "
123575 "UNIQUE constraints");
123576 goto exit_create_index;
123577 }
123578 if( pIndex->aColExpr==0 ){
123579 pIndex->aColExpr = pList;
123580 pList = 0;
123581 }
123582 j = XN_EXPR;
123583 pIndex->aiColumn[i] = XN_EXPR;
123584 pIndex->uniqNotNull = 0;
123585 pIndex->bHasExpr = 1;
123586 }else{
123587 j = pCExpr->iColumn;
123588 assert( j<=0x7fff );
123589 if( j<0 ){
123590 j = pTab->iPKey;
123591 }else{
123592 if( pTab->aCol[j].notNull==0 ){
123593 pIndex->uniqNotNull = 0;
123594 }
123595 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
123596 pIndex->bHasVCol = 1;
123597 pIndex->bHasExpr = 1;
123598 }
123599 }
123600 pIndex->aiColumn[i] = (i16)j;
123601 }
123602 zColl = 0;
123603 if( pListItem->pExpr->op==TK_COLLATE ){
123604 int nColl;
123605 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
123606 zColl = pListItem->pExpr->u.zToken;
123607 nColl = sqlite3Strlen30(z: zColl) + 1;
123608 assert( nExtra>=nColl );
123609 memcpy(dest: zExtra, src: zColl, n: nColl);
123610 zColl = zExtra;
123611 zExtra += nColl;
123612 nExtra -= nColl;
123613 }else if( j>=0 ){
123614 zColl = sqlite3ColumnColl(pCol: &pTab->aCol[j]);
123615 }
123616 if( !zColl ) zColl = sqlite3StrBINARY;
123617 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zName: zColl) ){
123618 goto exit_create_index;
123619 }
123620 pIndex->azColl[i] = zColl;
123621 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
123622 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
123623 }
123624
123625 /* Append the table key to the end of the index. For WITHOUT ROWID
123626 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
123627 ** normal tables (when pPk==0) this will be the rowid.
123628 */
123629 if( pPk ){
123630 for(j=0; j<pPk->nKeyCol; j++){
123631 int x = pPk->aiColumn[j];
123632 assert( x>=0 );
123633 if( isDupColumn(pIdx: pIndex, nKey: pIndex->nKeyCol, pPk, iCol: j) ){
123634 pIndex->nColumn--;
123635 }else{
123636 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
123637 pIndex->aiColumn[i] = x;
123638 pIndex->azColl[i] = pPk->azColl[j];
123639 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
123640 i++;
123641 }
123642 }
123643 assert( i==pIndex->nColumn );
123644 }else{
123645 pIndex->aiColumn[i] = XN_ROWID;
123646 pIndex->azColl[i] = sqlite3StrBINARY;
123647 }
123648 sqlite3DefaultRowEst(pIndex);
123649 if( pParse->pNewTable==0 ) estimateIndexWidth(pIdx: pIndex);
123650
123651 /* If this index contains every column of its table, then mark
123652 ** it as a covering index */
123653 assert( HasRowid(pTab)
123654 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
123655 recomputeColumnsNotIndexed(pIdx: pIndex);
123656 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
123657 pIndex->isCovering = 1;
123658 for(j=0; j<pTab->nCol; j++){
123659 if( j==pTab->iPKey ) continue;
123660 if( sqlite3TableColumnToIndex(pIdx: pIndex,iCol: j)>=0 ) continue;
123661 pIndex->isCovering = 0;
123662 break;
123663 }
123664 }
123665
123666 if( pTab==pParse->pNewTable ){
123667 /* This routine has been called to create an automatic index as a
123668 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
123669 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
123670 ** i.e. one of:
123671 **
123672 ** CREATE TABLE t(x PRIMARY KEY, y);
123673 ** CREATE TABLE t(x, y, UNIQUE(x, y));
123674 **
123675 ** Either way, check to see if the table already has such an index. If
123676 ** so, don't bother creating this one. This only applies to
123677 ** automatically created indices. Users can do as they wish with
123678 ** explicit indices.
123679 **
123680 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
123681 ** (and thus suppressing the second one) even if they have different
123682 ** sort orders.
123683 **
123684 ** If there are different collating sequences or if the columns of
123685 ** the constraint occur in different orders, then the constraints are
123686 ** considered distinct and both result in separate indices.
123687 */
123688 Index *pIdx;
123689 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123690 int k;
123691 assert( IsUniqueIndex(pIdx) );
123692 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
123693 assert( IsUniqueIndex(pIndex) );
123694
123695 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
123696 for(k=0; k<pIdx->nKeyCol; k++){
123697 const char *z1;
123698 const char *z2;
123699 assert( pIdx->aiColumn[k]>=0 );
123700 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
123701 z1 = pIdx->azColl[k];
123702 z2 = pIndex->azColl[k];
123703 if( sqlite3StrICmp(zLeft: z1, zRight: z2) ) break;
123704 }
123705 if( k==pIdx->nKeyCol ){
123706 if( pIdx->onError!=pIndex->onError ){
123707 /* This constraint creates the same index as a previous
123708 ** constraint specified somewhere in the CREATE TABLE statement.
123709 ** However the ON CONFLICT clauses are different. If both this
123710 ** constraint and the previous equivalent constraint have explicit
123711 ** ON CONFLICT clauses this is an error. Otherwise, use the
123712 ** explicitly specified behavior for the index.
123713 */
123714 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
123715 sqlite3ErrorMsg(pParse,
123716 zFormat: "conflicting ON CONFLICT clauses specified", 0);
123717 }
123718 if( pIdx->onError==OE_Default ){
123719 pIdx->onError = pIndex->onError;
123720 }
123721 }
123722 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
123723 if( IN_RENAME_OBJECT ){
123724 pIndex->pNext = pParse->pNewIndex;
123725 pParse->pNewIndex = pIndex;
123726 pIndex = 0;
123727 }
123728 goto exit_create_index;
123729 }
123730 }
123731 }
123732
123733 if( !IN_RENAME_OBJECT ){
123734
123735 /* Link the new Index structure to its table and to the other
123736 ** in-memory database structures.
123737 */
123738 assert( pParse->nErr==0 );
123739 if( db->init.busy ){
123740 Index *p;
123741 assert( !IN_SPECIAL_PARSE );
123742 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
123743 if( pTblName!=0 ){
123744 pIndex->tnum = db->init.newTnum;
123745 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
123746 sqlite3ErrorMsg(pParse, zFormat: "invalid rootpage");
123747 pParse->rc = SQLITE_CORRUPT_BKPT;
123748 goto exit_create_index;
123749 }
123750 }
123751 p = sqlite3HashInsert(pH: &pIndex->pSchema->idxHash,
123752 pKey: pIndex->zName, data: pIndex);
123753 if( p ){
123754 assert( p==pIndex ); /* Malloc must have failed */
123755 sqlite3OomFault(db);
123756 goto exit_create_index;
123757 }
123758 db->mDbFlags |= DBFLAG_SchemaChange;
123759 }
123760
123761 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
123762 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
123763 ** emit code to allocate the index rootpage on disk and make an entry for
123764 ** the index in the sqlite_schema table and populate the index with
123765 ** content. But, do not do this if we are simply reading the sqlite_schema
123766 ** table to parse the schema, or if this index is the PRIMARY KEY index
123767 ** of a WITHOUT ROWID table.
123768 **
123769 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
123770 ** or UNIQUE index in a CREATE TABLE statement. Since the table
123771 ** has just been created, it contains no data and the index initialization
123772 ** step can be skipped.
123773 */
123774 else if( HasRowid(pTab) || pTblName!=0 ){
123775 Vdbe *v;
123776 char *zStmt;
123777 int iMem = ++pParse->nMem;
123778
123779 v = sqlite3GetVdbe(pParse);
123780 if( v==0 ) goto exit_create_index;
123781
123782 sqlite3BeginWriteOperation(pParse, 1, iDb);
123783
123784 /* Create the rootpage for the index using CreateIndex. But before
123785 ** doing so, code a Noop instruction and store its address in
123786 ** Index.tnum. This is required in case this index is actually a
123787 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
123788 ** that case the convertToWithoutRowidTable() routine will replace
123789 ** the Noop with a Goto to jump over the VDBE code generated below. */
123790 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(p: v, OP_Noop);
123791 sqlite3VdbeAddOp3(p: v, OP_CreateBtree, p1: iDb, p2: iMem, BTREE_BLOBKEY);
123792
123793 /* Gather the complete text of the CREATE INDEX statement into
123794 ** the zStmt variable
123795 */
123796 assert( pName!=0 || pStart==0 );
123797 if( pStart ){
123798 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
123799 if( pName->z[n-1]==';' ) n--;
123800 /* A named index with an explicit CREATE INDEX statement */
123801 zStmt = sqlite3MPrintf(db, zFormat: "CREATE%s INDEX %.*s",
123802 onError==OE_None ? "" : " UNIQUE", n, pName->z);
123803 }else{
123804 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
123805 /* zStmt = sqlite3MPrintf(""); */
123806 zStmt = 0;
123807 }
123808
123809 /* Add an entry in sqlite_schema for this index
123810 */
123811 sqlite3NestedParse(pParse,
123812 zFormat: "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);",
123813 db->aDb[iDb].zDbSName,
123814 pIndex->zName,
123815 pTab->zName,
123816 iMem,
123817 zStmt
123818 );
123819 sqlite3DbFree(db, p: zStmt);
123820
123821 /* Fill the index with data and reparse the schema. Code an OP_Expire
123822 ** to invalidate all pre-compiled statements.
123823 */
123824 if( pTblName ){
123825 sqlite3RefillIndex(pParse, pIndex, memRootPage: iMem);
123826 sqlite3ChangeCookie(pParse, iDb);
123827 sqlite3VdbeAddParseSchemaOp(p: v, iDb,
123828 zWhere: sqlite3MPrintf(db, zFormat: "name='%q' AND type='index'", pIndex->zName), p5: 0);
123829 sqlite3VdbeAddOp2(p: v, OP_Expire, p1: 0, p2: 1);
123830 }
123831
123832 sqlite3VdbeJumpHere(p: v, addr: (int)pIndex->tnum);
123833 }
123834 }
123835 if( db->init.busy || pTblName==0 ){
123836 pIndex->pNext = pTab->pIndex;
123837 pTab->pIndex = pIndex;
123838 pIndex = 0;
123839 }
123840 else if( IN_RENAME_OBJECT ){
123841 assert( pParse->pNewIndex==0 );
123842 pParse->pNewIndex = pIndex;
123843 pIndex = 0;
123844 }
123845
123846 /* Clean up before exiting */
123847exit_create_index:
123848 if( pIndex ) sqlite3FreeIndex(db, p: pIndex);
123849 if( pTab ){
123850 /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list.
123851 ** The list was already ordered when this routine was entered, so at this
123852 ** point at most a single index (the newly added index) will be out of
123853 ** order. So we have to reorder at most one index. */
123854 Index **ppFrom;
123855 Index *pThis;
123856 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
123857 Index *pNext;
123858 if( pThis->onError!=OE_Replace ) continue;
123859 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
123860 *ppFrom = pNext;
123861 pThis->pNext = pNext->pNext;
123862 pNext->pNext = pThis;
123863 ppFrom = &pNext->pNext;
123864 }
123865 break;
123866 }
123867#ifdef SQLITE_DEBUG
123868 /* Verify that all REPLACE indexes really are now at the end
123869 ** of the index list. In other words, no other index type ever
123870 ** comes after a REPLACE index on the list. */
123871 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
123872 assert( pThis->onError!=OE_Replace
123873 || pThis->pNext==0
123874 || pThis->pNext->onError==OE_Replace );
123875 }
123876#endif
123877 }
123878 sqlite3ExprDelete(db, p: pPIWhere);
123879 sqlite3ExprListDelete(db, pList);
123880 sqlite3SrcListDelete(db, pTblName);
123881 sqlite3DbFree(db, p: zName);
123882}
123883
123884/*
123885** Fill the Index.aiRowEst[] array with default information - information
123886** to be used when we have not run the ANALYZE command.
123887**
123888** aiRowEst[0] is supposed to contain the number of elements in the index.
123889** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
123890** number of rows in the table that match any particular value of the
123891** first column of the index. aiRowEst[2] is an estimate of the number
123892** of rows that match any particular combination of the first 2 columns
123893** of the index. And so forth. It must always be the case that
123894*
123895** aiRowEst[N]<=aiRowEst[N-1]
123896** aiRowEst[N]>=1
123897**
123898** Apart from that, we have little to go on besides intuition as to
123899** how aiRowEst[] should be initialized. The numbers generated here
123900** are based on typical values found in actual indices.
123901*/
123902SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
123903 /* 10, 9, 8, 7, 6 */
123904 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
123905 LogEst *a = pIdx->aiRowLogEst;
123906 LogEst x;
123907 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
123908 int i;
123909
123910 /* Indexes with default row estimates should not have stat1 data */
123911 assert( !pIdx->hasStat1 );
123912
123913 /* Set the first entry (number of rows in the index) to the estimated
123914 ** number of rows in the table, or half the number of rows in the table
123915 ** for a partial index.
123916 **
123917 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
123918 ** table but other parts we are having to guess at, then do not let the
123919 ** estimated number of rows in the table be less than 1000 (LogEst 99).
123920 ** Failure to do this can cause the indexes for which we do not have
123921 ** stat1 data to be ignored by the query planner.
123922 */
123923 x = pIdx->pTable->nRowLogEst;
123924 assert( 99==sqlite3LogEst(1000) );
123925 if( x<99 ){
123926 pIdx->pTable->nRowLogEst = x = 99;
123927 }
123928 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
123929 a[0] = x;
123930
123931 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
123932 ** 6 and each subsequent value (if any) is 5. */
123933 memcpy(dest: &a[1], src: aVal, n: nCopy*sizeof(LogEst));
123934 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
123935 a[i] = 23; assert( 23==sqlite3LogEst(5) );
123936 }
123937
123938 assert( 0==sqlite3LogEst(1) );
123939 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
123940}
123941
123942/*
123943** This routine will drop an existing named index. This routine
123944** implements the DROP INDEX statement.
123945*/
123946SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
123947 Index *pIndex;
123948 Vdbe *v;
123949 sqlite3 *db = pParse->db;
123950 int iDb;
123951
123952 if( db->mallocFailed ){
123953 goto exit_drop_index;
123954 }
123955 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
123956 assert( pName->nSrc==1 );
123957 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
123958 goto exit_drop_index;
123959 }
123960 pIndex = sqlite3FindIndex(db, zName: pName->a[0].zName, zDb: pName->a[0].zDatabase);
123961 if( pIndex==0 ){
123962 if( !ifExists ){
123963 sqlite3ErrorMsg(pParse, zFormat: "no such index: %S", pName->a);
123964 }else{
123965 sqlite3CodeVerifyNamedSchema(pParse, zDb: pName->a[0].zDatabase);
123966 sqlite3ForceNotReadOnly(pParse);
123967 }
123968 pParse->checkSchema = 1;
123969 goto exit_drop_index;
123970 }
123971 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
123972 sqlite3ErrorMsg(pParse, zFormat: "index associated with UNIQUE "
123973 "or PRIMARY KEY constraint cannot be dropped", 0);
123974 goto exit_drop_index;
123975 }
123976 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
123977#ifndef SQLITE_OMIT_AUTHORIZATION
123978 {
123979 int code = SQLITE_DROP_INDEX;
123980 Table *pTab = pIndex->pTable;
123981 const char *zDb = db->aDb[iDb].zDbSName;
123982 const char *zTab = SCHEMA_TABLE(iDb);
123983 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: zTab, zArg2: 0, zArg3: zDb) ){
123984 goto exit_drop_index;
123985 }
123986 if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX;
123987 if( sqlite3AuthCheck(pParse, code, zArg1: pIndex->zName, zArg2: pTab->zName, zArg3: zDb) ){
123988 goto exit_drop_index;
123989 }
123990 }
123991#endif
123992
123993 /* Generate code to remove the index and from the schema table */
123994 v = sqlite3GetVdbe(pParse);
123995 if( v ){
123996 sqlite3BeginWriteOperation(pParse, 1, iDb);
123997 sqlite3NestedParse(pParse,
123998 zFormat: "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'",
123999 db->aDb[iDb].zDbSName, pIndex->zName
124000 );
124001 sqlite3ClearStatTables(pParse, iDb, zType: "idx", zName: pIndex->zName);
124002 sqlite3ChangeCookie(pParse, iDb);
124003 destroyRootPage(pParse, iTable: pIndex->tnum, iDb);
124004 sqlite3VdbeAddOp4(p: v, OP_DropIndex, p1: iDb, p2: 0, p3: 0, zP4: pIndex->zName, p4type: 0);
124005 }
124006
124007exit_drop_index:
124008 sqlite3SrcListDelete(db, pName);
124009}
124010
124011/*
124012** pArray is a pointer to an array of objects. Each object in the
124013** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
124014** to extend the array so that there is space for a new object at the end.
124015**
124016** When this function is called, *pnEntry contains the current size of
124017** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
124018** in total).
124019**
124020** If the realloc() is successful (i.e. if no OOM condition occurs), the
124021** space allocated for the new object is zeroed, *pnEntry updated to
124022** reflect the new size of the array and a pointer to the new allocation
124023** returned. *pIdx is set to the index of the new array entry in this case.
124024**
124025** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
124026** unchanged and a copy of pArray returned.
124027*/
124028SQLITE_PRIVATE void *sqlite3ArrayAllocate(
124029 sqlite3 *db, /* Connection to notify of malloc failures */
124030 void *pArray, /* Array of objects. Might be reallocated */
124031 int szEntry, /* Size of each object in the array */
124032 int *pnEntry, /* Number of objects currently in use */
124033 int *pIdx /* Write the index of a new slot here */
124034){
124035 char *z;
124036 sqlite3_int64 n = *pIdx = *pnEntry;
124037 if( (n & (n-1))==0 ){
124038 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
124039 void *pNew = sqlite3DbRealloc(db, p: pArray, n: sz*szEntry);
124040 if( pNew==0 ){
124041 *pIdx = -1;
124042 return pArray;
124043 }
124044 pArray = pNew;
124045 }
124046 z = (char*)pArray;
124047 memset(s: &z[n * szEntry], c: 0, n: szEntry);
124048 ++*pnEntry;
124049 return pArray;
124050}
124051
124052/*
124053** Append a new element to the given IdList. Create a new IdList if
124054** need be.
124055**
124056** A new IdList is returned, or NULL if malloc() fails.
124057*/
124058SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
124059 sqlite3 *db = pParse->db;
124060 int i;
124061 if( pList==0 ){
124062 pList = sqlite3DbMallocZero(db, n: sizeof(IdList) );
124063 if( pList==0 ) return 0;
124064 }else{
124065 IdList *pNew;
124066 pNew = sqlite3DbRealloc(db, p: pList,
124067 n: sizeof(IdList) + pList->nId*sizeof(pList->a));
124068 if( pNew==0 ){
124069 sqlite3IdListDelete(db, pList);
124070 return 0;
124071 }
124072 pList = pNew;
124073 }
124074 i = pList->nId++;
124075 pList->a[i].zName = sqlite3NameFromToken(db, pName: pToken);
124076 if( IN_RENAME_OBJECT && pList->a[i].zName ){
124077 sqlite3RenameTokenMap(pParse, pPtr: (void*)pList->a[i].zName, pToken);
124078 }
124079 return pList;
124080}
124081
124082/*
124083** Delete an IdList.
124084*/
124085SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
124086 int i;
124087 assert( db!=0 );
124088 if( pList==0 ) return;
124089 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
124090 for(i=0; i<pList->nId; i++){
124091 sqlite3DbFree(db, p: pList->a[i].zName);
124092 }
124093 sqlite3DbNNFreeNN(db, p: pList);
124094}
124095
124096/*
124097** Return the index in pList of the identifier named zId. Return -1
124098** if not found.
124099*/
124100SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
124101 int i;
124102 assert( pList!=0 );
124103 for(i=0; i<pList->nId; i++){
124104 if( sqlite3StrICmp(zLeft: pList->a[i].zName, zRight: zName)==0 ) return i;
124105 }
124106 return -1;
124107}
124108
124109/*
124110** Maximum size of a SrcList object.
124111** The SrcList object is used to represent the FROM clause of a
124112** SELECT statement, and the query planner cannot deal with more
124113** than 64 tables in a join. So any value larger than 64 here
124114** is sufficient for most uses. Smaller values, like say 10, are
124115** appropriate for small and memory-limited applications.
124116*/
124117#ifndef SQLITE_MAX_SRCLIST
124118# define SQLITE_MAX_SRCLIST 200
124119#endif
124120
124121/*
124122** Expand the space allocated for the given SrcList object by
124123** creating nExtra new slots beginning at iStart. iStart is zero based.
124124** New slots are zeroed.
124125**
124126** For example, suppose a SrcList initially contains two entries: A,B.
124127** To append 3 new entries onto the end, do this:
124128**
124129** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
124130**
124131** After the call above it would contain: A, B, nil, nil, nil.
124132** If the iStart argument had been 1 instead of 2, then the result
124133** would have been: A, nil, nil, nil, B. To prepend the new slots,
124134** the iStart value would be 0. The result then would
124135** be: nil, nil, nil, A, B.
124136**
124137** If a memory allocation fails or the SrcList becomes too large, leave
124138** the original SrcList unchanged, return NULL, and leave an error message
124139** in pParse.
124140*/
124141SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
124142 Parse *pParse, /* Parsing context into which errors are reported */
124143 SrcList *pSrc, /* The SrcList to be enlarged */
124144 int nExtra, /* Number of new slots to add to pSrc->a[] */
124145 int iStart /* Index in pSrc->a[] of first new slot */
124146){
124147 int i;
124148
124149 /* Sanity checking on calling parameters */
124150 assert( iStart>=0 );
124151 assert( nExtra>=1 );
124152 assert( pSrc!=0 );
124153 assert( iStart<=pSrc->nSrc );
124154
124155 /* Allocate additional space if needed */
124156 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
124157 SrcList *pNew;
124158 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
124159 sqlite3 *db = pParse->db;
124160
124161 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
124162 sqlite3ErrorMsg(pParse, zFormat: "too many FROM clause terms, max: %d",
124163 SQLITE_MAX_SRCLIST);
124164 return 0;
124165 }
124166 if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
124167 pNew = sqlite3DbRealloc(db, p: pSrc,
124168 n: sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
124169 if( pNew==0 ){
124170 assert( db->mallocFailed );
124171 return 0;
124172 }
124173 pSrc = pNew;
124174 pSrc->nAlloc = nAlloc;
124175 }
124176
124177 /* Move existing slots that come after the newly inserted slots
124178 ** out of the way */
124179 for(i=pSrc->nSrc-1; i>=iStart; i--){
124180 pSrc->a[i+nExtra] = pSrc->a[i];
124181 }
124182 pSrc->nSrc += nExtra;
124183
124184 /* Zero the newly allocated slots */
124185 memset(s: &pSrc->a[iStart], c: 0, n: sizeof(pSrc->a[0])*nExtra);
124186 for(i=iStart; i<iStart+nExtra; i++){
124187 pSrc->a[i].iCursor = -1;
124188 }
124189
124190 /* Return a pointer to the enlarged SrcList */
124191 return pSrc;
124192}
124193
124194
124195/*
124196** Append a new table name to the given SrcList. Create a new SrcList if
124197** need be. A new entry is created in the SrcList even if pTable is NULL.
124198**
124199** A SrcList is returned, or NULL if there is an OOM error or if the
124200** SrcList grows to large. The returned
124201** SrcList might be the same as the SrcList that was input or it might be
124202** a new one. If an OOM error does occurs, then the prior value of pList
124203** that is input to this routine is automatically freed.
124204**
124205** If pDatabase is not null, it means that the table has an optional
124206** database name prefix. Like this: "database.table". The pDatabase
124207** points to the table name and the pTable points to the database name.
124208** The SrcList.a[].zName field is filled with the table name which might
124209** come from pTable (if pDatabase is NULL) or from pDatabase.
124210** SrcList.a[].zDatabase is filled with the database name from pTable,
124211** or with NULL if no database is specified.
124212**
124213** In other words, if call like this:
124214**
124215** sqlite3SrcListAppend(D,A,B,0);
124216**
124217** Then B is a table name and the database name is unspecified. If called
124218** like this:
124219**
124220** sqlite3SrcListAppend(D,A,B,C);
124221**
124222** Then C is the table name and B is the database name. If C is defined
124223** then so is B. In other words, we never have a case where:
124224**
124225** sqlite3SrcListAppend(D,A,0,C);
124226**
124227** Both pTable and pDatabase are assumed to be quoted. They are dequoted
124228** before being added to the SrcList.
124229*/
124230SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
124231 Parse *pParse, /* Parsing context, in which errors are reported */
124232 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
124233 Token *pTable, /* Table to append */
124234 Token *pDatabase /* Database of the table */
124235){
124236 SrcItem *pItem;
124237 sqlite3 *db;
124238 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
124239 assert( pParse!=0 );
124240 assert( pParse->db!=0 );
124241 db = pParse->db;
124242 if( pList==0 ){
124243 pList = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(SrcList) );
124244 if( pList==0 ) return 0;
124245 pList->nAlloc = 1;
124246 pList->nSrc = 1;
124247 memset(s: &pList->a[0], c: 0, n: sizeof(pList->a[0]));
124248 pList->a[0].iCursor = -1;
124249 }else{
124250 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pSrc: pList, nExtra: 1, iStart: pList->nSrc);
124251 if( pNew==0 ){
124252 sqlite3SrcListDelete(db, pList);
124253 return 0;
124254 }else{
124255 pList = pNew;
124256 }
124257 }
124258 pItem = &pList->a[pList->nSrc-1];
124259 if( pDatabase && pDatabase->z==0 ){
124260 pDatabase = 0;
124261 }
124262 if( pDatabase ){
124263 pItem->zName = sqlite3NameFromToken(db, pName: pDatabase);
124264 pItem->zDatabase = sqlite3NameFromToken(db, pName: pTable);
124265 }else{
124266 pItem->zName = sqlite3NameFromToken(db, pName: pTable);
124267 pItem->zDatabase = 0;
124268 }
124269 return pList;
124270}
124271
124272/*
124273** Assign VdbeCursor index numbers to all tables in a SrcList
124274*/
124275SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
124276 int i;
124277 SrcItem *pItem;
124278 assert( pList || pParse->db->mallocFailed );
124279 if( ALWAYS(pList) ){
124280 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
124281 if( pItem->iCursor>=0 ) continue;
124282 pItem->iCursor = pParse->nTab++;
124283 if( pItem->pSelect ){
124284 sqlite3SrcListAssignCursors(pParse, pList: pItem->pSelect->pSrc);
124285 }
124286 }
124287 }
124288}
124289
124290/*
124291** Delete an entire SrcList including all its substructure.
124292*/
124293SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
124294 int i;
124295 SrcItem *pItem;
124296 assert( db!=0 );
124297 if( pList==0 ) return;
124298 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
124299 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, p: pItem->zDatabase);
124300 if( pItem->zName ) sqlite3DbNNFreeNN(db, p: pItem->zName);
124301 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, p: pItem->zAlias);
124302 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, p: pItem->u1.zIndexedBy);
124303 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pList: pItem->u1.pFuncArg);
124304 sqlite3DeleteTable(db, pTable: pItem->pTab);
124305 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
124306 if( pItem->fg.isUsing ){
124307 sqlite3IdListDelete(db, pList: pItem->u3.pUsing);
124308 }else if( pItem->u3.pOn ){
124309 sqlite3ExprDelete(db, p: pItem->u3.pOn);
124310 }
124311 }
124312 sqlite3DbNNFreeNN(db, p: pList);
124313}
124314
124315/*
124316** This routine is called by the parser to add a new term to the
124317** end of a growing FROM clause. The "p" parameter is the part of
124318** the FROM clause that has already been constructed. "p" is NULL
124319** if this is the first term of the FROM clause. pTable and pDatabase
124320** are the name of the table and database named in the FROM clause term.
124321** pDatabase is NULL if the database name qualifier is missing - the
124322** usual case. If the term has an alias, then pAlias points to the
124323** alias token. If the term is a subquery, then pSubquery is the
124324** SELECT statement that the subquery encodes. The pTable and
124325** pDatabase parameters are NULL for subqueries. The pOn and pUsing
124326** parameters are the content of the ON and USING clauses.
124327**
124328** Return a new SrcList which encodes is the FROM with the new
124329** term added.
124330*/
124331SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
124332 Parse *pParse, /* Parsing context */
124333 SrcList *p, /* The left part of the FROM clause already seen */
124334 Token *pTable, /* Name of the table to add to the FROM clause */
124335 Token *pDatabase, /* Name of the database containing pTable */
124336 Token *pAlias, /* The right-hand side of the AS subexpression */
124337 Select *pSubquery, /* A subquery used in place of a table name */
124338 OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */
124339){
124340 SrcItem *pItem;
124341 sqlite3 *db = pParse->db;
124342 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
124343 sqlite3ErrorMsg(pParse, zFormat: "a JOIN clause is required before %s",
124344 (pOnUsing->pOn ? "ON" : "USING")
124345 );
124346 goto append_from_error;
124347 }
124348 p = sqlite3SrcListAppend(pParse, pList: p, pTable, pDatabase);
124349 if( p==0 ){
124350 goto append_from_error;
124351 }
124352 assert( p->nSrc>0 );
124353 pItem = &p->a[p->nSrc-1];
124354 assert( (pTable==0)==(pDatabase==0) );
124355 assert( pItem->zName==0 || pDatabase!=0 );
124356 if( IN_RENAME_OBJECT && pItem->zName ){
124357 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
124358 sqlite3RenameTokenMap(pParse, pPtr: pItem->zName, pToken);
124359 }
124360 assert( pAlias!=0 );
124361 if( pAlias->n ){
124362 pItem->zAlias = sqlite3NameFromToken(db, pName: pAlias);
124363 }
124364 if( pSubquery ){
124365 pItem->pSelect = pSubquery;
124366 if( pSubquery->selFlags & SF_NestedFrom ){
124367 pItem->fg.isNestedFrom = 1;
124368 }
124369 }
124370 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
124371 assert( pItem->fg.isUsing==0 );
124372 if( pOnUsing==0 ){
124373 pItem->u3.pOn = 0;
124374 }else if( pOnUsing->pUsing ){
124375 pItem->fg.isUsing = 1;
124376 pItem->u3.pUsing = pOnUsing->pUsing;
124377 }else{
124378 pItem->u3.pOn = pOnUsing->pOn;
124379 }
124380 return p;
124381
124382append_from_error:
124383 assert( p==0 );
124384 sqlite3ClearOnOrUsing(db, p: pOnUsing);
124385 sqlite3SelectDelete(db, pSubquery);
124386 return 0;
124387}
124388
124389/*
124390** Add an INDEXED BY or NOT INDEXED clause to the most recently added
124391** element of the source-list passed as the second argument.
124392*/
124393SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
124394 assert( pIndexedBy!=0 );
124395 if( p && pIndexedBy->n>0 ){
124396 SrcItem *pItem;
124397 assert( p->nSrc>0 );
124398 pItem = &p->a[p->nSrc-1];
124399 assert( pItem->fg.notIndexed==0 );
124400 assert( pItem->fg.isIndexedBy==0 );
124401 assert( pItem->fg.isTabFunc==0 );
124402 if( pIndexedBy->n==1 && !pIndexedBy->z ){
124403 /* A "NOT INDEXED" clause was supplied. See parse.y
124404 ** construct "indexed_opt" for details. */
124405 pItem->fg.notIndexed = 1;
124406 }else{
124407 pItem->u1.zIndexedBy = sqlite3NameFromToken(db: pParse->db, pName: pIndexedBy);
124408 pItem->fg.isIndexedBy = 1;
124409 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
124410 }
124411 }
124412}
124413
124414/*
124415** Append the contents of SrcList p2 to SrcList p1 and return the resulting
124416** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
124417** are deleted by this function.
124418*/
124419SQLITE_PRIVATE SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
124420 assert( p1 && p1->nSrc==1 );
124421 if( p2 ){
124422 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pSrc: p1, nExtra: p2->nSrc, iStart: 1);
124423 if( pNew==0 ){
124424 sqlite3SrcListDelete(db: pParse->db, pList: p2);
124425 }else{
124426 p1 = pNew;
124427 memcpy(dest: &p1->a[1], src: p2->a, n: p2->nSrc*sizeof(SrcItem));
124428 sqlite3DbFree(db: pParse->db, p: p2);
124429 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
124430 }
124431 }
124432 return p1;
124433}
124434
124435/*
124436** Add the list of function arguments to the SrcList entry for a
124437** table-valued-function.
124438*/
124439SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
124440 if( p ){
124441 SrcItem *pItem = &p->a[p->nSrc-1];
124442 assert( pItem->fg.notIndexed==0 );
124443 assert( pItem->fg.isIndexedBy==0 );
124444 assert( pItem->fg.isTabFunc==0 );
124445 pItem->u1.pFuncArg = pList;
124446 pItem->fg.isTabFunc = 1;
124447 }else{
124448 sqlite3ExprListDelete(db: pParse->db, pList);
124449 }
124450}
124451
124452/*
124453** When building up a FROM clause in the parser, the join operator
124454** is initially attached to the left operand. But the code generator
124455** expects the join operator to be on the right operand. This routine
124456** Shifts all join operators from left to right for an entire FROM
124457** clause.
124458**
124459** Example: Suppose the join is like this:
124460**
124461** A natural cross join B
124462**
124463** The operator is "natural cross join". The A and B operands are stored
124464** in p->a[0] and p->a[1], respectively. The parser initially stores the
124465** operator with A. This routine shifts that operator over to B.
124466**
124467** Additional changes:
124468**
124469** * All tables to the left of the right-most RIGHT JOIN are tagged with
124470** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the
124471** code generator can easily tell that the table is part of
124472** the left operand of at least one RIGHT JOIN.
124473*/
124474SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){
124475 (void)pParse;
124476 if( p && p->nSrc>1 ){
124477 int i = p->nSrc-1;
124478 u8 allFlags = 0;
124479 do{
124480 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
124481 }while( (--i)>0 );
124482 p->a[0].fg.jointype = 0;
124483
124484 /* All terms to the left of a RIGHT JOIN should be tagged with the
124485 ** JT_LTORJ flags */
124486 if( allFlags & JT_RIGHT ){
124487 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
124488 i--;
124489 assert( i>=0 );
124490 do{
124491 p->a[i].fg.jointype |= JT_LTORJ;
124492 }while( (--i)>=0 );
124493 }
124494 }
124495}
124496
124497/*
124498** Generate VDBE code for a BEGIN statement.
124499*/
124500SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
124501 sqlite3 *db;
124502 Vdbe *v;
124503 int i;
124504
124505 assert( pParse!=0 );
124506 db = pParse->db;
124507 assert( db!=0 );
124508 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, zArg1: "BEGIN", zArg2: 0, zArg3: 0) ){
124509 return;
124510 }
124511 v = sqlite3GetVdbe(pParse);
124512 if( !v ) return;
124513 if( type!=TK_DEFERRED ){
124514 for(i=0; i<db->nDb; i++){
124515 int eTxnType;
124516 Btree *pBt = db->aDb[i].pBt;
124517 if( pBt && sqlite3BtreeIsReadonly(p: pBt) ){
124518 eTxnType = 0; /* Read txn */
124519 }else if( type==TK_EXCLUSIVE ){
124520 eTxnType = 2; /* Exclusive txn */
124521 }else{
124522 eTxnType = 1; /* Write txn */
124523 }
124524 sqlite3VdbeAddOp2(p: v, OP_Transaction, p1: i, p2: eTxnType);
124525 sqlite3VdbeUsesBtree(p: v, i);
124526 }
124527 }
124528 sqlite3VdbeAddOp0(p: v, OP_AutoCommit);
124529}
124530
124531/*
124532** Generate VDBE code for a COMMIT or ROLLBACK statement.
124533** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
124534** code is generated for a COMMIT.
124535*/
124536SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
124537 Vdbe *v;
124538 int isRollback;
124539
124540 assert( pParse!=0 );
124541 assert( pParse->db!=0 );
124542 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
124543 isRollback = eType==TK_ROLLBACK;
124544 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
124545 zArg1: isRollback ? "ROLLBACK" : "COMMIT", zArg2: 0, zArg3: 0) ){
124546 return;
124547 }
124548 v = sqlite3GetVdbe(pParse);
124549 if( v ){
124550 sqlite3VdbeAddOp2(p: v, OP_AutoCommit, p1: 1, p2: isRollback);
124551 }
124552}
124553
124554/*
124555** This function is called by the parser when it parses a command to create,
124556** release or rollback an SQL savepoint.
124557*/
124558SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
124559 char *zName = sqlite3NameFromToken(db: pParse->db, pName);
124560 if( zName ){
124561 Vdbe *v = sqlite3GetVdbe(pParse);
124562#ifndef SQLITE_OMIT_AUTHORIZATION
124563 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
124564 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
124565#endif
124566 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, zArg1: az[op], zArg2: zName, zArg3: 0) ){
124567 sqlite3DbFree(db: pParse->db, p: zName);
124568 return;
124569 }
124570 sqlite3VdbeAddOp4(p: v, OP_Savepoint, p1: op, p2: 0, p3: 0, zP4: zName, P4_DYNAMIC);
124571 }
124572}
124573
124574/*
124575** Make sure the TEMP database is open and available for use. Return
124576** the number of errors. Leave any error messages in the pParse structure.
124577*/
124578SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
124579 sqlite3 *db = pParse->db;
124580 if( db->aDb[1].pBt==0 && !pParse->explain ){
124581 int rc;
124582 Btree *pBt;
124583 static const int flags =
124584 SQLITE_OPEN_READWRITE |
124585 SQLITE_OPEN_CREATE |
124586 SQLITE_OPEN_EXCLUSIVE |
124587 SQLITE_OPEN_DELETEONCLOSE |
124588 SQLITE_OPEN_TEMP_DB;
124589
124590 rc = sqlite3BtreeOpen(pVfs: db->pVfs, zFilename: 0, db, ppBtree: &pBt, flags: 0, vfsFlags: flags);
124591 if( rc!=SQLITE_OK ){
124592 sqlite3ErrorMsg(pParse, zFormat: "unable to open a temporary database "
124593 "file for storing temporary tables");
124594 pParse->rc = rc;
124595 return 1;
124596 }
124597 db->aDb[1].pBt = pBt;
124598 assert( db->aDb[1].pSchema );
124599 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(p: pBt, pageSize: db->nextPagesize, nReserve: 0, iFix: 0) ){
124600 sqlite3OomFault(db);
124601 return 1;
124602 }
124603 }
124604 return 0;
124605}
124606
124607/*
124608** Record the fact that the schema cookie will need to be verified
124609** for database iDb. The code to actually verify the schema cookie
124610** will occur at the end of the top-level VDBE and will be generated
124611** later, by sqlite3FinishCoding().
124612*/
124613static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
124614 assert( iDb>=0 && iDb<pToplevel->db->nDb );
124615 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
124616 assert( iDb<SQLITE_MAX_DB );
124617 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
124618 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
124619 DbMaskSet(pToplevel->cookieMask, iDb);
124620 if( !OMIT_TEMPDB && iDb==1 ){
124621 sqlite3OpenTempDatabase(pParse: pToplevel);
124622 }
124623 }
124624}
124625SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
124626 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb);
124627}
124628
124629
124630/*
124631** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
124632** attached database. Otherwise, invoke it for the database named zDb only.
124633*/
124634SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
124635 sqlite3 *db = pParse->db;
124636 int i;
124637 for(i=0; i<db->nDb; i++){
124638 Db *pDb = &db->aDb[i];
124639 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zLeft: zDb, zRight: pDb->zDbSName)) ){
124640 sqlite3CodeVerifySchema(pParse, iDb: i);
124641 }
124642 }
124643}
124644
124645/*
124646** Generate VDBE code that prepares for doing an operation that
124647** might change the database.
124648**
124649** This routine starts a new transaction if we are not already within
124650** a transaction. If we are already within a transaction, then a checkpoint
124651** is set if the setStatement parameter is true. A checkpoint should
124652** be set for operations that might fail (due to a constraint) part of
124653** the way through and which will need to undo some writes without having to
124654** rollback the whole transaction. For operations where all constraints
124655** can be checked before any changes are made to the database, it is never
124656** necessary to undo a write and the checkpoint should not be set.
124657*/
124658SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
124659 Parse *pToplevel = sqlite3ParseToplevel(pParse);
124660 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
124661 DbMaskSet(pToplevel->writeMask, iDb);
124662 pToplevel->isMultiWrite |= setStatement;
124663}
124664
124665/*
124666** Indicate that the statement currently under construction might write
124667** more than one entry (example: deleting one row then inserting another,
124668** inserting multiple rows in a table, or inserting a row and index entries.)
124669** If an abort occurs after some of these writes have completed, then it will
124670** be necessary to undo the completed writes.
124671*/
124672SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
124673 Parse *pToplevel = sqlite3ParseToplevel(pParse);
124674 pToplevel->isMultiWrite = 1;
124675}
124676
124677/*
124678** The code generator calls this routine if is discovers that it is
124679** possible to abort a statement prior to completion. In order to
124680** perform this abort without corrupting the database, we need to make
124681** sure that the statement is protected by a statement transaction.
124682**
124683** Technically, we only need to set the mayAbort flag if the
124684** isMultiWrite flag was previously set. There is a time dependency
124685** such that the abort must occur after the multiwrite. This makes
124686** some statements involving the REPLACE conflict resolution algorithm
124687** go a little faster. But taking advantage of this time dependency
124688** makes it more difficult to prove that the code is correct (in
124689** particular, it prevents us from writing an effective
124690** implementation of sqlite3AssertMayAbort()) and so we have chosen
124691** to take the safe route and skip the optimization.
124692*/
124693SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
124694 Parse *pToplevel = sqlite3ParseToplevel(pParse);
124695 pToplevel->mayAbort = 1;
124696}
124697
124698/*
124699** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
124700** error. The onError parameter determines which (if any) of the statement
124701** and/or current transaction is rolled back.
124702*/
124703SQLITE_PRIVATE void sqlite3HaltConstraint(
124704 Parse *pParse, /* Parsing context */
124705 int errCode, /* extended error code */
124706 int onError, /* Constraint type */
124707 char *p4, /* Error message */
124708 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
124709 u8 p5Errmsg /* P5_ErrMsg type */
124710){
124711 Vdbe *v;
124712 assert( pParse->pVdbe!=0 );
124713 v = sqlite3GetVdbe(pParse);
124714 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
124715 if( onError==OE_Abort ){
124716 sqlite3MayAbort(pParse);
124717 }
124718 sqlite3VdbeAddOp4(p: v, OP_Halt, p1: errCode, p2: onError, p3: 0, zP4: p4, p4type);
124719 sqlite3VdbeChangeP5(p: v, p5: p5Errmsg);
124720}
124721
124722/*
124723** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
124724*/
124725SQLITE_PRIVATE void sqlite3UniqueConstraint(
124726 Parse *pParse, /* Parsing context */
124727 int onError, /* Constraint type */
124728 Index *pIdx /* The index that triggers the constraint */
124729){
124730 char *zErr;
124731 int j;
124732 StrAccum errMsg;
124733 Table *pTab = pIdx->pTable;
124734
124735 sqlite3StrAccumInit(p: &errMsg, db: pParse->db, zBase: 0, n: 0,
124736 mx: pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
124737 if( pIdx->aColExpr ){
124738 sqlite3_str_appendf(p: &errMsg, zFormat: "index '%q'", pIdx->zName);
124739 }else{
124740 for(j=0; j<pIdx->nKeyCol; j++){
124741 char *zCol;
124742 assert( pIdx->aiColumn[j]>=0 );
124743 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
124744 if( j ) sqlite3_str_append(p: &errMsg, z: ", ", N: 2);
124745 sqlite3_str_appendall(p: &errMsg, z: pTab->zName);
124746 sqlite3_str_append(p: &errMsg, z: ".", N: 1);
124747 sqlite3_str_appendall(p: &errMsg, z: zCol);
124748 }
124749 }
124750 zErr = sqlite3StrAccumFinish(p: &errMsg);
124751 sqlite3HaltConstraint(pParse,
124752 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
124753 : SQLITE_CONSTRAINT_UNIQUE,
124754 onError, p4: zErr, P4_DYNAMIC, P5_ConstraintUnique);
124755}
124756
124757
124758/*
124759** Code an OP_Halt due to non-unique rowid.
124760*/
124761SQLITE_PRIVATE void sqlite3RowidConstraint(
124762 Parse *pParse, /* Parsing context */
124763 int onError, /* Conflict resolution algorithm */
124764 Table *pTab /* The table with the non-unique rowid */
124765){
124766 char *zMsg;
124767 int rc;
124768 if( pTab->iPKey>=0 ){
124769 zMsg = sqlite3MPrintf(db: pParse->db, zFormat: "%s.%s", pTab->zName,
124770 pTab->aCol[pTab->iPKey].zCnName);
124771 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
124772 }else{
124773 zMsg = sqlite3MPrintf(db: pParse->db, zFormat: "%s.rowid", pTab->zName);
124774 rc = SQLITE_CONSTRAINT_ROWID;
124775 }
124776 sqlite3HaltConstraint(pParse, errCode: rc, onError, p4: zMsg, P4_DYNAMIC,
124777 P5_ConstraintUnique);
124778}
124779
124780/*
124781** Check to see if pIndex uses the collating sequence pColl. Return
124782** true if it does and false if it does not.
124783*/
124784#ifndef SQLITE_OMIT_REINDEX
124785static int collationMatch(const char *zColl, Index *pIndex){
124786 int i;
124787 assert( zColl!=0 );
124788 for(i=0; i<pIndex->nColumn; i++){
124789 const char *z = pIndex->azColl[i];
124790 assert( z!=0 || pIndex->aiColumn[i]<0 );
124791 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(zLeft: z, zRight: zColl) ){
124792 return 1;
124793 }
124794 }
124795 return 0;
124796}
124797#endif
124798
124799/*
124800** Recompute all indices of pTab that use the collating sequence pColl.
124801** If pColl==0 then recompute all indices of pTab.
124802*/
124803#ifndef SQLITE_OMIT_REINDEX
124804static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
124805 if( !IsVirtual(pTab) ){
124806 Index *pIndex; /* An index associated with pTab */
124807
124808 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
124809 if( zColl==0 || collationMatch(zColl, pIndex) ){
124810 int iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
124811 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
124812 sqlite3RefillIndex(pParse, pIndex, memRootPage: -1);
124813 }
124814 }
124815 }
124816}
124817#endif
124818
124819/*
124820** Recompute all indices of all tables in all databases where the
124821** indices use the collating sequence pColl. If pColl==0 then recompute
124822** all indices everywhere.
124823*/
124824#ifndef SQLITE_OMIT_REINDEX
124825static void reindexDatabases(Parse *pParse, char const *zColl){
124826 Db *pDb; /* A single database */
124827 int iDb; /* The database index number */
124828 sqlite3 *db = pParse->db; /* The database connection */
124829 HashElem *k; /* For looping over tables in pDb */
124830 Table *pTab; /* A table in the database */
124831
124832 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
124833 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
124834 assert( pDb!=0 );
124835 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
124836 pTab = (Table*)sqliteHashData(k);
124837 reindexTable(pParse, pTab, zColl);
124838 }
124839 }
124840}
124841#endif
124842
124843/*
124844** Generate code for the REINDEX command.
124845**
124846** REINDEX -- 1
124847** REINDEX <collation> -- 2
124848** REINDEX ?<database>.?<tablename> -- 3
124849** REINDEX ?<database>.?<indexname> -- 4
124850**
124851** Form 1 causes all indices in all attached databases to be rebuilt.
124852** Form 2 rebuilds all indices in all databases that use the named
124853** collating function. Forms 3 and 4 rebuild the named index or all
124854** indices associated with the named table.
124855*/
124856#ifndef SQLITE_OMIT_REINDEX
124857SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
124858 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
124859 char *z; /* Name of a table or index */
124860 const char *zDb; /* Name of the database */
124861 Table *pTab; /* A table in the database */
124862 Index *pIndex; /* An index associated with pTab */
124863 int iDb; /* The database index number */
124864 sqlite3 *db = pParse->db; /* The database connection */
124865 Token *pObjName; /* Name of the table or index to be reindexed */
124866
124867 /* Read the database schema. If an error occurs, leave an error message
124868 ** and code in pParse and return NULL. */
124869 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
124870 return;
124871 }
124872
124873 if( pName1==0 ){
124874 reindexDatabases(pParse, zColl: 0);
124875 return;
124876 }else if( NEVER(pName2==0) || pName2->z==0 ){
124877 char *zColl;
124878 assert( pName1->z );
124879 zColl = sqlite3NameFromToken(db: pParse->db, pName: pName1);
124880 if( !zColl ) return;
124881 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
124882 if( pColl ){
124883 reindexDatabases(pParse, zColl);
124884 sqlite3DbFree(db, p: zColl);
124885 return;
124886 }
124887 sqlite3DbFree(db, p: zColl);
124888 }
124889 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pObjName);
124890 if( iDb<0 ) return;
124891 z = sqlite3NameFromToken(db, pName: pObjName);
124892 if( z==0 ) return;
124893 zDb = db->aDb[iDb].zDbSName;
124894 pTab = sqlite3FindTable(db, zName: z, zDatabase: zDb);
124895 if( pTab ){
124896 reindexTable(pParse, pTab, zColl: 0);
124897 sqlite3DbFree(db, p: z);
124898 return;
124899 }
124900 pIndex = sqlite3FindIndex(db, zName: z, zDb);
124901 sqlite3DbFree(db, p: z);
124902 if( pIndex ){
124903 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
124904 sqlite3RefillIndex(pParse, pIndex, memRootPage: -1);
124905 return;
124906 }
124907 sqlite3ErrorMsg(pParse, zFormat: "unable to identify the object to be reindexed");
124908}
124909#endif
124910
124911/*
124912** Return a KeyInfo structure that is appropriate for the given Index.
124913**
124914** The caller should invoke sqlite3KeyInfoUnref() on the returned object
124915** when it has finished using it.
124916*/
124917SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
124918 int i;
124919 int nCol = pIdx->nColumn;
124920 int nKey = pIdx->nKeyCol;
124921 KeyInfo *pKey;
124922 if( pParse->nErr ) return 0;
124923 if( pIdx->uniqNotNull ){
124924 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
124925 }else{
124926 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
124927 }
124928 if( pKey ){
124929 assert( sqlite3KeyInfoIsWriteable(pKey) );
124930 for(i=0; i<nCol; i++){
124931 const char *zColl = pIdx->azColl[i];
124932 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
124933 sqlite3LocateCollSeq(pParse, zName: zColl);
124934 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
124935 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
124936 }
124937 if( pParse->nErr ){
124938 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
124939 if( pIdx->bNoQuery==0 ){
124940 /* Deactivate the index because it contains an unknown collating
124941 ** sequence. The only way to reactive the index is to reload the
124942 ** schema. Adding the missing collating sequence later does not
124943 ** reactive the index. The application had the chance to register
124944 ** the missing index using the collation-needed callback. For
124945 ** simplicity, SQLite will not give the application a second chance.
124946 */
124947 pIdx->bNoQuery = 1;
124948 pParse->rc = SQLITE_ERROR_RETRY;
124949 }
124950 sqlite3KeyInfoUnref(pKey);
124951 pKey = 0;
124952 }
124953 }
124954 return pKey;
124955}
124956
124957#ifndef SQLITE_OMIT_CTE
124958/*
124959** Create a new CTE object
124960*/
124961SQLITE_PRIVATE Cte *sqlite3CteNew(
124962 Parse *pParse, /* Parsing context */
124963 Token *pName, /* Name of the common-table */
124964 ExprList *pArglist, /* Optional column name list for the table */
124965 Select *pQuery, /* Query used to initialize the table */
124966 u8 eM10d /* The MATERIALIZED flag */
124967){
124968 Cte *pNew;
124969 sqlite3 *db = pParse->db;
124970
124971 pNew = sqlite3DbMallocZero(db, n: sizeof(*pNew));
124972 assert( pNew!=0 || db->mallocFailed );
124973
124974 if( db->mallocFailed ){
124975 sqlite3ExprListDelete(db, pList: pArglist);
124976 sqlite3SelectDelete(db, pQuery);
124977 }else{
124978 pNew->pSelect = pQuery;
124979 pNew->pCols = pArglist;
124980 pNew->zName = sqlite3NameFromToken(db: pParse->db, pName);
124981 pNew->eM10d = eM10d;
124982 }
124983 return pNew;
124984}
124985
124986/*
124987** Clear information from a Cte object, but do not deallocate storage
124988** for the object itself.
124989*/
124990static void cteClear(sqlite3 *db, Cte *pCte){
124991 assert( pCte!=0 );
124992 sqlite3ExprListDelete(db, pList: pCte->pCols);
124993 sqlite3SelectDelete(db, pCte->pSelect);
124994 sqlite3DbFree(db, p: pCte->zName);
124995}
124996
124997/*
124998** Free the contents of the CTE object passed as the second argument.
124999*/
125000SQLITE_PRIVATE void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
125001 assert( pCte!=0 );
125002 cteClear(db, pCte);
125003 sqlite3DbFree(db, p: pCte);
125004}
125005
125006/*
125007** This routine is invoked once per CTE by the parser while parsing a
125008** WITH clause. The CTE described by the third argument is added to
125009** the WITH clause of the second argument. If the second argument is
125010** NULL, then a new WITH argument is created.
125011*/
125012SQLITE_PRIVATE With *sqlite3WithAdd(
125013 Parse *pParse, /* Parsing context */
125014 With *pWith, /* Existing WITH clause, or NULL */
125015 Cte *pCte /* CTE to add to the WITH clause */
125016){
125017 sqlite3 *db = pParse->db;
125018 With *pNew;
125019 char *zName;
125020
125021 if( pCte==0 ){
125022 return pWith;
125023 }
125024
125025 /* Check that the CTE name is unique within this WITH clause. If
125026 ** not, store an error in the Parse structure. */
125027 zName = pCte->zName;
125028 if( zName && pWith ){
125029 int i;
125030 for(i=0; i<pWith->nCte; i++){
125031 if( sqlite3StrICmp(zLeft: zName, zRight: pWith->a[i].zName)==0 ){
125032 sqlite3ErrorMsg(pParse, zFormat: "duplicate WITH table name: %s", zName);
125033 }
125034 }
125035 }
125036
125037 if( pWith ){
125038 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
125039 pNew = sqlite3DbRealloc(db, p: pWith, n: nByte);
125040 }else{
125041 pNew = sqlite3DbMallocZero(db, n: sizeof(*pWith));
125042 }
125043 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
125044
125045 if( db->mallocFailed ){
125046 sqlite3CteDelete(db, pCte);
125047 pNew = pWith;
125048 }else{
125049 pNew->a[pNew->nCte++] = *pCte;
125050 sqlite3DbFree(db, p: pCte);
125051 }
125052
125053 return pNew;
125054}
125055
125056/*
125057** Free the contents of the With object passed as the second argument.
125058*/
125059SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
125060 if( pWith ){
125061 int i;
125062 for(i=0; i<pWith->nCte; i++){
125063 cteClear(db, pCte: &pWith->a[i]);
125064 }
125065 sqlite3DbFree(db, p: pWith);
125066 }
125067}
125068#endif /* !defined(SQLITE_OMIT_CTE) */
125069
125070/************** End of build.c ***********************************************/
125071/************** Begin file callback.c ****************************************/
125072/*
125073** 2005 May 23
125074**
125075** The author disclaims copyright to this source code. In place of
125076** a legal notice, here is a blessing:
125077**
125078** May you do good and not evil.
125079** May you find forgiveness for yourself and forgive others.
125080** May you share freely, never taking more than you give.
125081**
125082*************************************************************************
125083**
125084** This file contains functions used to access the internal hash tables
125085** of user defined functions and collation sequences.
125086*/
125087
125088/* #include "sqliteInt.h" */
125089
125090/*
125091** Invoke the 'collation needed' callback to request a collation sequence
125092** in the encoding enc of name zName, length nName.
125093*/
125094static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
125095 assert( !db->xCollNeeded || !db->xCollNeeded16 );
125096 if( db->xCollNeeded ){
125097 char *zExternal = sqlite3DbStrDup(db, z: zName);
125098 if( !zExternal ) return;
125099 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
125100 sqlite3DbFree(db, p: zExternal);
125101 }
125102#ifndef SQLITE_OMIT_UTF16
125103 if( db->xCollNeeded16 ){
125104 char const *zExternal;
125105 sqlite3_value *pTmp = sqlite3ValueNew(db);
125106 sqlite3ValueSetStr(v: pTmp, n: -1, z: zName, SQLITE_UTF8, SQLITE_STATIC);
125107 zExternal = sqlite3ValueText(pVal: pTmp, SQLITE_UTF16NATIVE);
125108 if( zExternal ){
125109 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
125110 }
125111 sqlite3ValueFree(v: pTmp);
125112 }
125113#endif
125114}
125115
125116/*
125117** This routine is called if the collation factory fails to deliver a
125118** collation function in the best encoding but there may be other versions
125119** of this collation function (for other text encodings) available. Use one
125120** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
125121** possible.
125122*/
125123static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
125124 CollSeq *pColl2;
125125 char *z = pColl->zName;
125126 int i;
125127 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
125128 for(i=0; i<3; i++){
125129 pColl2 = sqlite3FindCollSeq(db, enc: aEnc[i], z, 0);
125130 if( pColl2->xCmp!=0 ){
125131 memcpy(dest: pColl, src: pColl2, n: sizeof(CollSeq));
125132 pColl->xDel = 0; /* Do not copy the destructor */
125133 return SQLITE_OK;
125134 }
125135 }
125136 return SQLITE_ERROR;
125137}
125138
125139/*
125140** This routine is called on a collation sequence before it is used to
125141** check that it is defined. An undefined collation sequence exists when
125142** a database is loaded that contains references to collation sequences
125143** that have not been defined by sqlite3_create_collation() etc.
125144**
125145** If required, this routine calls the 'collation needed' callback to
125146** request a definition of the collating sequence. If this doesn't work,
125147** an equivalent collating sequence that uses a text encoding different
125148** from the main database is substituted, if one is available.
125149*/
125150SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
125151 if( pColl && pColl->xCmp==0 ){
125152 const char *zName = pColl->zName;
125153 sqlite3 *db = pParse->db;
125154 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
125155 if( !p ){
125156 return SQLITE_ERROR;
125157 }
125158 assert( p==pColl );
125159 }
125160 return SQLITE_OK;
125161}
125162
125163
125164
125165/*
125166** Locate and return an entry from the db.aCollSeq hash table. If the entry
125167** specified by zName and nName is not found and parameter 'create' is
125168** true, then create a new entry. Otherwise return NULL.
125169**
125170** Each pointer stored in the sqlite3.aCollSeq hash table contains an
125171** array of three CollSeq structures. The first is the collation sequence
125172** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
125173**
125174** Stored immediately after the three collation sequences is a copy of
125175** the collation sequence name. A pointer to this string is stored in
125176** each collation sequence structure.
125177*/
125178static CollSeq *findCollSeqEntry(
125179 sqlite3 *db, /* Database connection */
125180 const char *zName, /* Name of the collating sequence */
125181 int create /* Create a new entry if true */
125182){
125183 CollSeq *pColl;
125184 pColl = sqlite3HashFind(pH: &db->aCollSeq, pKey: zName);
125185
125186 if( 0==pColl && create ){
125187 int nName = sqlite3Strlen30(z: zName) + 1;
125188 pColl = sqlite3DbMallocZero(db, n: 3*sizeof(*pColl) + nName);
125189 if( pColl ){
125190 CollSeq *pDel = 0;
125191 pColl[0].zName = (char*)&pColl[3];
125192 pColl[0].enc = SQLITE_UTF8;
125193 pColl[1].zName = (char*)&pColl[3];
125194 pColl[1].enc = SQLITE_UTF16LE;
125195 pColl[2].zName = (char*)&pColl[3];
125196 pColl[2].enc = SQLITE_UTF16BE;
125197 memcpy(dest: pColl[0].zName, src: zName, n: nName);
125198 pDel = sqlite3HashInsert(pH: &db->aCollSeq, pKey: pColl[0].zName, data: pColl);
125199
125200 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
125201 ** return the pColl pointer to be deleted (because it wasn't added
125202 ** to the hash table).
125203 */
125204 assert( pDel==0 || pDel==pColl );
125205 if( pDel!=0 ){
125206 sqlite3OomFault(db);
125207 sqlite3DbFree(db, p: pDel);
125208 pColl = 0;
125209 }
125210 }
125211 }
125212 return pColl;
125213}
125214
125215/*
125216** Parameter zName points to a UTF-8 encoded string nName bytes long.
125217** Return the CollSeq* pointer for the collation sequence named zName
125218** for the encoding 'enc' from the database 'db'.
125219**
125220** If the entry specified is not found and 'create' is true, then create a
125221** new entry. Otherwise return NULL.
125222**
125223** A separate function sqlite3LocateCollSeq() is a wrapper around
125224** this routine. sqlite3LocateCollSeq() invokes the collation factory
125225** if necessary and generates an error message if the collating sequence
125226** cannot be found.
125227**
125228** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
125229*/
125230SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
125231 sqlite3 *db, /* Database connection to search */
125232 u8 enc, /* Desired text encoding */
125233 const char *zName, /* Name of the collating sequence. Might be NULL */
125234 int create /* True to create CollSeq if doesn't already exist */
125235){
125236 CollSeq *pColl;
125237 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
125238 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
125239 if( zName ){
125240 pColl = findCollSeqEntry(db, zName, create);
125241 if( pColl ) pColl += enc-1;
125242 }else{
125243 pColl = db->pDfltColl;
125244 }
125245 return pColl;
125246}
125247
125248/*
125249** Change the text encoding for a database connection. This means that
125250** the pDfltColl must change as well.
125251*/
125252SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
125253 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
125254 db->enc = enc;
125255 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
125256 ** strings is BINARY.
125257 */
125258 db->pDfltColl = sqlite3FindCollSeq(db, enc, zName: sqlite3StrBINARY, create: 0);
125259 sqlite3ExpirePreparedStatements(db, iCode: 1);
125260}
125261
125262/*
125263** This function is responsible for invoking the collation factory callback
125264** or substituting a collation sequence of a different encoding when the
125265** requested collation sequence is not available in the desired encoding.
125266**
125267** If it is not NULL, then pColl must point to the database native encoding
125268** collation sequence with name zName, length nName.
125269**
125270** The return value is either the collation sequence to be used in database
125271** db for collation type name zName, length nName, or NULL, if no collation
125272** sequence can be found. If no collation is found, leave an error message.
125273**
125274** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
125275*/
125276SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
125277 Parse *pParse, /* Parsing context */
125278 u8 enc, /* The desired encoding for the collating sequence */
125279 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
125280 const char *zName /* Collating sequence name */
125281){
125282 CollSeq *p;
125283 sqlite3 *db = pParse->db;
125284
125285 p = pColl;
125286 if( !p ){
125287 p = sqlite3FindCollSeq(db, enc, zName, create: 0);
125288 }
125289 if( !p || !p->xCmp ){
125290 /* No collation sequence of this type for this encoding is registered.
125291 ** Call the collation factory to see if it can supply us with one.
125292 */
125293 callCollNeeded(db, enc, zName);
125294 p = sqlite3FindCollSeq(db, enc, zName, create: 0);
125295 }
125296 if( p && !p->xCmp && synthCollSeq(db, pColl: p) ){
125297 p = 0;
125298 }
125299 assert( !p || p->xCmp );
125300 if( p==0 ){
125301 sqlite3ErrorMsg(pParse, zFormat: "no such collation sequence: %s", zName);
125302 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
125303 }
125304 return p;
125305}
125306
125307/*
125308** This function returns the collation sequence for database native text
125309** encoding identified by the string zName.
125310**
125311** If the requested collation sequence is not available, or not available
125312** in the database native encoding, the collation factory is invoked to
125313** request it. If the collation factory does not supply such a sequence,
125314** and the sequence is available in another text encoding, then that is
125315** returned instead.
125316**
125317** If no versions of the requested collations sequence are available, or
125318** another error occurs, NULL is returned and an error message written into
125319** pParse.
125320**
125321** This routine is a wrapper around sqlite3FindCollSeq(). This routine
125322** invokes the collation factory if the named collation cannot be found
125323** and generates an error message.
125324**
125325** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
125326*/
125327SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
125328 sqlite3 *db = pParse->db;
125329 u8 enc = ENC(db);
125330 u8 initbusy = db->init.busy;
125331 CollSeq *pColl;
125332
125333 pColl = sqlite3FindCollSeq(db, enc, zName, create: initbusy);
125334 if( !initbusy && (!pColl || !pColl->xCmp) ){
125335 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
125336 }
125337
125338 return pColl;
125339}
125340
125341/* During the search for the best function definition, this procedure
125342** is called to test how well the function passed as the first argument
125343** matches the request for a function with nArg arguments in a system
125344** that uses encoding enc. The value returned indicates how well the
125345** request is matched. A higher value indicates a better match.
125346**
125347** If nArg is -1 that means to only return a match (non-zero) if p->nArg
125348** is also -1. In other words, we are searching for a function that
125349** takes a variable number of arguments.
125350**
125351** If nArg is -2 that means that we are searching for any function
125352** regardless of the number of arguments it uses, so return a positive
125353** match score for any
125354**
125355** The returned value is always between 0 and 6, as follows:
125356**
125357** 0: Not a match.
125358** 1: UTF8/16 conversion required and function takes any number of arguments.
125359** 2: UTF16 byte order change required and function takes any number of args.
125360** 3: encoding matches and function takes any number of arguments
125361** 4: UTF8/16 conversion required - argument count matches exactly
125362** 5: UTF16 byte order conversion required - argument count matches exactly
125363** 6: Perfect match: encoding and argument count match exactly.
125364**
125365** If nArg==(-2) then any function with a non-null xSFunc is
125366** a perfect match and any function with xSFunc NULL is
125367** a non-match.
125368*/
125369#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
125370static int matchQuality(
125371 FuncDef *p, /* The function we are evaluating for match quality */
125372 int nArg, /* Desired number of arguments. (-1)==any */
125373 u8 enc /* Desired text encoding */
125374){
125375 int match;
125376 assert( p->nArg>=-1 );
125377
125378 /* Wrong number of arguments means "no match" */
125379 if( p->nArg!=nArg ){
125380 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
125381 if( p->nArg>=0 ) return 0;
125382 }
125383
125384 /* Give a better score to a function with a specific number of arguments
125385 ** than to function that accepts any number of arguments. */
125386 if( p->nArg==nArg ){
125387 match = 4;
125388 }else{
125389 match = 1;
125390 }
125391
125392 /* Bonus points if the text encoding matches */
125393 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
125394 match += 2; /* Exact encoding match */
125395 }else if( (enc & p->funcFlags & 2)!=0 ){
125396 match += 1; /* Both are UTF16, but with different byte orders */
125397 }
125398
125399 return match;
125400}
125401
125402/*
125403** Search a FuncDefHash for a function with the given name. Return
125404** a pointer to the matching FuncDef if found, or 0 if there is no match.
125405*/
125406SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(
125407 int h, /* Hash of the name */
125408 const char *zFunc /* Name of function */
125409){
125410 FuncDef *p;
125411 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
125412 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
125413 if( sqlite3StrICmp(zLeft: p->zName, zRight: zFunc)==0 ){
125414 return p;
125415 }
125416 }
125417 return 0;
125418}
125419
125420/*
125421** Insert a new FuncDef into a FuncDefHash hash table.
125422*/
125423SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
125424 FuncDef *aDef, /* List of global functions to be inserted */
125425 int nDef /* Length of the apDef[] list */
125426){
125427 int i;
125428 for(i=0; i<nDef; i++){
125429 FuncDef *pOther;
125430 const char *zName = aDef[i].zName;
125431 int nName = sqlite3Strlen30(z: zName);
125432 int h = SQLITE_FUNC_HASH(zName[0], nName);
125433 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN );
125434 pOther = sqlite3FunctionSearch(h, zFunc: zName);
125435 if( pOther ){
125436 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
125437 aDef[i].pNext = pOther->pNext;
125438 pOther->pNext = &aDef[i];
125439 }else{
125440 aDef[i].pNext = 0;
125441 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
125442 sqlite3BuiltinFunctions.a[h] = &aDef[i];
125443 }
125444 }
125445}
125446
125447
125448
125449/*
125450** Locate a user function given a name, a number of arguments and a flag
125451** indicating whether the function prefers UTF-16 over UTF-8. Return a
125452** pointer to the FuncDef structure that defines that function, or return
125453** NULL if the function does not exist.
125454**
125455** If the createFlag argument is true, then a new (blank) FuncDef
125456** structure is created and liked into the "db" structure if a
125457** no matching function previously existed.
125458**
125459** If nArg is -2, then the first valid function found is returned. A
125460** function is valid if xSFunc is non-zero. The nArg==(-2)
125461** case is used to see if zName is a valid function name for some number
125462** of arguments. If nArg is -2, then createFlag must be 0.
125463**
125464** If createFlag is false, then a function with the required name and
125465** number of arguments may be returned even if the eTextRep flag does not
125466** match that requested.
125467*/
125468SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
125469 sqlite3 *db, /* An open database */
125470 const char *zName, /* Name of the function. zero-terminated */
125471 int nArg, /* Number of arguments. -1 means any number */
125472 u8 enc, /* Preferred text encoding */
125473 u8 createFlag /* Create new entry if true and does not otherwise exist */
125474){
125475 FuncDef *p; /* Iterator variable */
125476 FuncDef *pBest = 0; /* Best match found so far */
125477 int bestScore = 0; /* Score of best match */
125478 int h; /* Hash value */
125479 int nName; /* Length of the name */
125480
125481 assert( nArg>=(-2) );
125482 assert( nArg>=(-1) || createFlag==0 );
125483 nName = sqlite3Strlen30(z: zName);
125484
125485 /* First search for a match amongst the application-defined functions.
125486 */
125487 p = (FuncDef*)sqlite3HashFind(pH: &db->aFunc, pKey: zName);
125488 while( p ){
125489 int score = matchQuality(p, nArg, enc);
125490 if( score>bestScore ){
125491 pBest = p;
125492 bestScore = score;
125493 }
125494 p = p->pNext;
125495 }
125496
125497 /* If no match is found, search the built-in functions.
125498 **
125499 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
125500 ** functions even if a prior app-defined function was found. And give
125501 ** priority to built-in functions.
125502 **
125503 ** Except, if createFlag is true, that means that we are trying to
125504 ** install a new function. Whatever FuncDef structure is returned it will
125505 ** have fields overwritten with new information appropriate for the
125506 ** new function. But the FuncDefs for built-in functions are read-only.
125507 ** So we must not search for built-ins when creating a new function.
125508 */
125509 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
125510 bestScore = 0;
125511 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
125512 p = sqlite3FunctionSearch(h, zFunc: zName);
125513 while( p ){
125514 int score = matchQuality(p, nArg, enc);
125515 if( score>bestScore ){
125516 pBest = p;
125517 bestScore = score;
125518 }
125519 p = p->pNext;
125520 }
125521 }
125522
125523 /* If the createFlag parameter is true and the search did not reveal an
125524 ** exact match for the name, number of arguments and encoding, then add a
125525 ** new entry to the hash table and return it.
125526 */
125527 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
125528 (pBest = sqlite3DbMallocZero(db, n: sizeof(*pBest)+nName+1))!=0 ){
125529 FuncDef *pOther;
125530 u8 *z;
125531 pBest->zName = (const char*)&pBest[1];
125532 pBest->nArg = (u16)nArg;
125533 pBest->funcFlags = enc;
125534 memcpy(dest: (char*)&pBest[1], src: zName, n: nName+1);
125535 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
125536 pOther = (FuncDef*)sqlite3HashInsert(pH: &db->aFunc, pKey: pBest->zName, data: pBest);
125537 if( pOther==pBest ){
125538 sqlite3DbFree(db, p: pBest);
125539 sqlite3OomFault(db);
125540 return 0;
125541 }else{
125542 pBest->pNext = pOther;
125543 }
125544 }
125545
125546 if( pBest && (pBest->xSFunc || createFlag) ){
125547 return pBest;
125548 }
125549 return 0;
125550}
125551
125552/*
125553** Free all resources held by the schema structure. The void* argument points
125554** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
125555** pointer itself, it just cleans up subsidiary resources (i.e. the contents
125556** of the schema hash tables).
125557**
125558** The Schema.cache_size variable is not cleared.
125559*/
125560SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
125561 Hash temp1;
125562 Hash temp2;
125563 HashElem *pElem;
125564 Schema *pSchema = (Schema *)p;
125565 sqlite3 xdb;
125566
125567 memset(s: &xdb, c: 0, n: sizeof(xdb));
125568 temp1 = pSchema->tblHash;
125569 temp2 = pSchema->trigHash;
125570 sqlite3HashInit(pNew: &pSchema->trigHash);
125571 sqlite3HashClear(pH: &pSchema->idxHash);
125572 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
125573 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
125574 }
125575 sqlite3HashClear(pH: &temp2);
125576 sqlite3HashInit(pNew: &pSchema->tblHash);
125577 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
125578 Table *pTab = sqliteHashData(pElem);
125579 sqlite3DeleteTable(db: &xdb, pTable: pTab);
125580 }
125581 sqlite3HashClear(pH: &temp1);
125582 sqlite3HashClear(pH: &pSchema->fkeyHash);
125583 pSchema->pSeqTab = 0;
125584 if( pSchema->schemaFlags & DB_SchemaLoaded ){
125585 pSchema->iGeneration++;
125586 }
125587 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
125588}
125589
125590/*
125591** Find and return the schema associated with a BTree. Create
125592** a new one if necessary.
125593*/
125594SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
125595 Schema * p;
125596 if( pBt ){
125597 p = (Schema *)sqlite3BtreeSchema(p: pBt, nBytes: sizeof(Schema), xFree: sqlite3SchemaClear);
125598 }else{
125599 p = (Schema *)sqlite3DbMallocZero(db: 0, n: sizeof(Schema));
125600 }
125601 if( !p ){
125602 sqlite3OomFault(db);
125603 }else if ( 0==p->file_format ){
125604 sqlite3HashInit(pNew: &p->tblHash);
125605 sqlite3HashInit(pNew: &p->idxHash);
125606 sqlite3HashInit(pNew: &p->trigHash);
125607 sqlite3HashInit(pNew: &p->fkeyHash);
125608 p->enc = SQLITE_UTF8;
125609 }
125610 return p;
125611}
125612
125613/************** End of callback.c ********************************************/
125614/************** Begin file delete.c ******************************************/
125615/*
125616** 2001 September 15
125617**
125618** The author disclaims copyright to this source code. In place of
125619** a legal notice, here is a blessing:
125620**
125621** May you do good and not evil.
125622** May you find forgiveness for yourself and forgive others.
125623** May you share freely, never taking more than you give.
125624**
125625*************************************************************************
125626** This file contains C code routines that are called by the parser
125627** in order to generate code for DELETE FROM statements.
125628*/
125629/* #include "sqliteInt.h" */
125630
125631/*
125632** While a SrcList can in general represent multiple tables and subqueries
125633** (as in the FROM clause of a SELECT statement) in this case it contains
125634** the name of a single table, as one might find in an INSERT, DELETE,
125635** or UPDATE statement. Look up that table in the symbol table and
125636** return a pointer. Set an error message and return NULL if the table
125637** name is not found or if any other error occurs.
125638**
125639** The following fields are initialized appropriate in pSrc:
125640**
125641** pSrc->a[0].pTab Pointer to the Table object
125642** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
125643**
125644*/
125645SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
125646 SrcItem *pItem = pSrc->a;
125647 Table *pTab;
125648 assert( pItem && pSrc->nSrc>=1 );
125649 pTab = sqlite3LocateTableItem(pParse, flags: 0, p: pItem);
125650 if( pItem->pTab ) sqlite3DeleteTable(db: pParse->db, pTable: pItem->pTab);
125651 pItem->pTab = pTab;
125652 pItem->fg.notCte = 1;
125653 if( pTab ){
125654 pTab->nTabRef++;
125655 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
125656 pTab = 0;
125657 }
125658 }
125659 return pTab;
125660}
125661
125662/* Generate byte-code that will report the number of rows modified
125663** by a DELETE, INSERT, or UPDATE statement.
125664*/
125665SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){
125666 sqlite3VdbeAddOp0(p: v, OP_FkCheck);
125667 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: regCounter, p2: 1);
125668 sqlite3VdbeSetNumCols(p: v, nResColumn: 1);
125669 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: zColName, SQLITE_STATIC);
125670}
125671
125672/* Return true if table pTab is read-only.
125673**
125674** A table is read-only if any of the following are true:
125675**
125676** 1) It is a virtual table and no implementation of the xUpdate method
125677** has been provided
125678**
125679** 2) A trigger is currently being coded and the table is a virtual table
125680** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
125681** the table is not SQLITE_VTAB_INNOCUOUS.
125682**
125683** 3) It is a system table (i.e. sqlite_schema), this call is not
125684** part of a nested parse and writable_schema pragma has not
125685** been specified
125686**
125687** 4) The table is a shadow table, the database connection is in
125688** defensive mode, and the current sqlite3_prepare()
125689** is for a top-level SQL statement.
125690*/
125691static int vtabIsReadOnly(Parse *pParse, Table *pTab){
125692 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
125693 return 1;
125694 }
125695
125696 /* Within triggers:
125697 ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
125698 ** virtual tables
125699 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
125700 ** virtual tables if PRAGMA trusted_schema=ON.
125701 */
125702 if( pParse->pToplevel!=0
125703 && pTab->u.vtab.p->eVtabRisk >
125704 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
125705 ){
125706 sqlite3ErrorMsg(pParse, zFormat: "unsafe use of virtual table \"%s\"",
125707 pTab->zName);
125708 }
125709 return 0;
125710}
125711static int tabIsReadOnly(Parse *pParse, Table *pTab){
125712 sqlite3 *db;
125713 if( IsVirtual(pTab) ){
125714 return vtabIsReadOnly(pParse, pTab);
125715 }
125716 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
125717 db = pParse->db;
125718 if( (pTab->tabFlags & TF_Readonly)!=0 ){
125719 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
125720 }
125721 assert( pTab->tabFlags & TF_Shadow );
125722 return sqlite3ReadOnlyShadowTables(db);
125723}
125724
125725/*
125726** Check to make sure the given table is writable.
125727**
125728** If pTab is not writable -> generate an error message and return 1.
125729** If pTab is writable but other errors have occurred -> return 1.
125730** If pTab is writable and no prior errors -> return 0;
125731*/
125732SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){
125733 if( tabIsReadOnly(pParse, pTab) ){
125734 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be modified", pTab->zName);
125735 return 1;
125736 }
125737#ifndef SQLITE_OMIT_VIEW
125738 if( IsView(pTab)
125739 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
125740 ){
125741 sqlite3ErrorMsg(pParse,zFormat: "cannot modify %s because it is a view",pTab->zName);
125742 return 1;
125743 }
125744#endif
125745 return 0;
125746}
125747
125748
125749#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
125750/*
125751** Evaluate a view and store its result in an ephemeral table. The
125752** pWhere argument is an optional WHERE clause that restricts the
125753** set of rows in the view that are to be added to the ephemeral table.
125754*/
125755SQLITE_PRIVATE void sqlite3MaterializeView(
125756 Parse *pParse, /* Parsing context */
125757 Table *pView, /* View definition */
125758 Expr *pWhere, /* Optional WHERE clause to be added */
125759 ExprList *pOrderBy, /* Optional ORDER BY clause */
125760 Expr *pLimit, /* Optional LIMIT clause */
125761 int iCur /* Cursor number for ephemeral table */
125762){
125763 SelectDest dest;
125764 Select *pSel;
125765 SrcList *pFrom;
125766 sqlite3 *db = pParse->db;
125767 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
125768 pWhere = sqlite3ExprDup(db, p: pWhere, flags: 0);
125769 pFrom = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
125770 if( pFrom ){
125771 assert( pFrom->nSrc==1 );
125772 pFrom->a[0].zName = sqlite3DbStrDup(db, z: pView->zName);
125773 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, z: db->aDb[iDb].zDbSName);
125774 assert( pFrom->a[0].fg.isUsing==0 );
125775 assert( pFrom->a[0].u3.pOn==0 );
125776 }
125777 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
125778 SF_IncludeHidden, pLimit);
125779 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
125780 sqlite3Select(pParse, pSel, &dest);
125781 sqlite3SelectDelete(db, pSel);
125782}
125783#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
125784
125785#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
125786/*
125787** Generate an expression tree to implement the WHERE, ORDER BY,
125788** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
125789**
125790** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
125791** \__________________________/
125792** pLimitWhere (pInClause)
125793*/
125794SQLITE_PRIVATE Expr *sqlite3LimitWhere(
125795 Parse *pParse, /* The parser context */
125796 SrcList *pSrc, /* the FROM clause -- which tables to scan */
125797 Expr *pWhere, /* The WHERE clause. May be null */
125798 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
125799 Expr *pLimit, /* The LIMIT clause. May be null */
125800 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
125801){
125802 sqlite3 *db = pParse->db;
125803 Expr *pLhs = NULL; /* LHS of IN(SELECT...) operator */
125804 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
125805 ExprList *pEList = NULL; /* Expression list containing only pSelectRowid*/
125806 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
125807 Select *pSelect = NULL; /* Complete SELECT tree */
125808 Table *pTab;
125809
125810 /* Check that there isn't an ORDER BY without a LIMIT clause.
125811 */
125812 if( pOrderBy && pLimit==0 ) {
125813 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
125814 sqlite3ExprDelete(pParse->db, pWhere);
125815 sqlite3ExprListDelete(pParse->db, pOrderBy);
125816 return 0;
125817 }
125818
125819 /* We only need to generate a select expression if there
125820 ** is a limit/offset term to enforce.
125821 */
125822 if( pLimit == 0 ) {
125823 return pWhere;
125824 }
125825
125826 /* Generate a select expression tree to enforce the limit/offset
125827 ** term for the DELETE or UPDATE statement. For example:
125828 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
125829 ** becomes:
125830 ** DELETE FROM table_a WHERE rowid IN (
125831 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
125832 ** );
125833 */
125834
125835 pTab = pSrc->a[0].pTab;
125836 if( HasRowid(pTab) ){
125837 pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
125838 pEList = sqlite3ExprListAppend(
125839 pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
125840 );
125841 }else{
125842 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
125843 assert( pPk!=0 );
125844 assert( pPk->nKeyCol>=1 );
125845 if( pPk->nKeyCol==1 ){
125846 const char *zName;
125847 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol );
125848 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
125849 pLhs = sqlite3Expr(db, TK_ID, zName);
125850 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
125851 }else{
125852 int i;
125853 for(i=0; i<pPk->nKeyCol; i++){
125854 Expr *p;
125855 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol );
125856 p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
125857 pEList = sqlite3ExprListAppend(pParse, pEList, p);
125858 }
125859 pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
125860 if( pLhs ){
125861 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
125862 }
125863 }
125864 }
125865
125866 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
125867 ** and the SELECT subtree. */
125868 pSrc->a[0].pTab = 0;
125869 pSelectSrc = sqlite3SrcListDup(db, pSrc, 0);
125870 pSrc->a[0].pTab = pTab;
125871 if( pSrc->a[0].fg.isIndexedBy ){
125872 assert( pSrc->a[0].fg.isCte==0 );
125873 pSrc->a[0].u2.pIBIndex = 0;
125874 pSrc->a[0].fg.isIndexedBy = 0;
125875 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
125876 }else if( pSrc->a[0].fg.isCte ){
125877 pSrc->a[0].u2.pCteUse->nUse++;
125878 }
125879
125880 /* generate the SELECT expression tree. */
125881 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
125882 pOrderBy,0,pLimit
125883 );
125884
125885 /* now generate the new WHERE rowid IN clause for the DELETE/UPDATE */
125886 pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
125887 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
125888 return pInClause;
125889}
125890#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
125891 /* && !defined(SQLITE_OMIT_SUBQUERY) */
125892
125893/*
125894** Generate code for a DELETE FROM statement.
125895**
125896** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
125897** \________/ \________________/
125898** pTabList pWhere
125899*/
125900SQLITE_PRIVATE void sqlite3DeleteFrom(
125901 Parse *pParse, /* The parser context */
125902 SrcList *pTabList, /* The table from which we should delete things */
125903 Expr *pWhere, /* The WHERE clause. May be null */
125904 ExprList *pOrderBy, /* ORDER BY clause. May be null */
125905 Expr *pLimit /* LIMIT clause. May be null */
125906){
125907 Vdbe *v; /* The virtual database engine */
125908 Table *pTab; /* The table from which records will be deleted */
125909 int i; /* Loop counter */
125910 WhereInfo *pWInfo; /* Information about the WHERE clause */
125911 Index *pIdx; /* For looping over indices of the table */
125912 int iTabCur; /* Cursor number for the table */
125913 int iDataCur = 0; /* VDBE cursor for the canonical data source */
125914 int iIdxCur = 0; /* Cursor number of the first index */
125915 int nIdx; /* Number of indices */
125916 sqlite3 *db; /* Main database structure */
125917 AuthContext sContext; /* Authorization context */
125918 NameContext sNC; /* Name context to resolve expressions in */
125919 int iDb; /* Database number */
125920 int memCnt = 0; /* Memory cell used for change counting */
125921 int rcauth; /* Value returned by authorization callback */
125922 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
125923 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
125924 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
125925 Index *pPk; /* The PRIMARY KEY index on the table */
125926 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
125927 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
125928 int iKey; /* Memory cell holding key of row to be deleted */
125929 i16 nKey; /* Number of memory cells in the row key */
125930 int iEphCur = 0; /* Ephemeral table holding all primary key values */
125931 int iRowSet = 0; /* Register for rowset of rows to delete */
125932 int addrBypass = 0; /* Address of jump over the delete logic */
125933 int addrLoop = 0; /* Top of the delete loop */
125934 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
125935 int bComplex; /* True if there are triggers or FKs or
125936 ** subqueries in the WHERE clause */
125937
125938#ifndef SQLITE_OMIT_TRIGGER
125939 int isView; /* True if attempting to delete from a view */
125940 Trigger *pTrigger; /* List of table triggers, if required */
125941#endif
125942
125943 memset(s: &sContext, c: 0, n: sizeof(sContext));
125944 db = pParse->db;
125945 assert( db->pParse==pParse );
125946 if( pParse->nErr ){
125947 goto delete_from_cleanup;
125948 }
125949 assert( db->mallocFailed==0 );
125950 assert( pTabList->nSrc==1 );
125951
125952 /* Locate the table which we want to delete. This table has to be
125953 ** put in an SrcList structure because some of the subroutines we
125954 ** will be calling are designed to work with multiple tables and expect
125955 ** an SrcList* parameter instead of just a Table* parameter.
125956 */
125957 pTab = sqlite3SrcListLookup(pParse, pSrc: pTabList);
125958 if( pTab==0 ) goto delete_from_cleanup;
125959
125960 /* Figure out if we have any triggers and if the table being
125961 ** deleted from is a view
125962 */
125963#ifndef SQLITE_OMIT_TRIGGER
125964 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, pMask: 0);
125965 isView = IsView(pTab);
125966#else
125967# define pTrigger 0
125968# define isView 0
125969#endif
125970 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
125971#ifdef SQLITE_OMIT_VIEW
125972# undef isView
125973# define isView 0
125974#endif
125975
125976#if TREETRACE_ENABLED
125977 if( sqlite3TreeTrace & 0x10000 ){
125978 sqlite3TreeViewLine(0, "In sqlite3Delete() at %s:%d", __FILE__, __LINE__);
125979 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
125980 pOrderBy, pLimit, pTrigger);
125981 }
125982#endif
125983
125984#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
125985 if( !isView ){
125986 pWhere = sqlite3LimitWhere(
125987 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
125988 );
125989 pOrderBy = 0;
125990 pLimit = 0;
125991 }
125992#endif
125993
125994 /* If pTab is really a view, make sure it has been initialized.
125995 */
125996 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
125997 goto delete_from_cleanup;
125998 }
125999
126000 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
126001 goto delete_from_cleanup;
126002 }
126003 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126004 assert( iDb<db->nDb );
126005 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: pTab->zName, zArg2: 0,
126006 zArg3: db->aDb[iDb].zDbSName);
126007 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
126008 if( rcauth==SQLITE_DENY ){
126009 goto delete_from_cleanup;
126010 }
126011 assert(!isView || pTrigger);
126012
126013 /* Assign cursor numbers to the table and all its indices.
126014 */
126015 assert( pTabList->nSrc==1 );
126016 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
126017 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
126018 pParse->nTab++;
126019 }
126020
126021 /* Start the view context
126022 */
126023 if( isView ){
126024 sqlite3AuthContextPush(pParse, pContext: &sContext, zContext: pTab->zName);
126025 }
126026
126027 /* Begin generating code.
126028 */
126029 v = sqlite3GetVdbe(pParse);
126030 if( v==0 ){
126031 goto delete_from_cleanup;
126032 }
126033 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
126034 sqlite3BeginWriteOperation(pParse, setStatement: bComplex, iDb);
126035
126036 /* If we are trying to delete from a view, realize that view into
126037 ** an ephemeral table.
126038 */
126039#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
126040 if( isView ){
126041 sqlite3MaterializeView(pParse, pView: pTab,
126042 pWhere, pOrderBy, pLimit, iCur: iTabCur
126043 );
126044 iDataCur = iIdxCur = iTabCur;
126045 pOrderBy = 0;
126046 pLimit = 0;
126047 }
126048#endif
126049
126050 /* Resolve the column names in the WHERE clause.
126051 */
126052 memset(s: &sNC, c: 0, n: sizeof(sNC));
126053 sNC.pParse = pParse;
126054 sNC.pSrcList = pTabList;
126055 if( sqlite3ResolveExprNames(pNC: &sNC, pExpr: pWhere) ){
126056 goto delete_from_cleanup;
126057 }
126058
126059 /* Initialize the counter of the number of rows deleted, if
126060 ** we are counting rows.
126061 */
126062 if( (db->flags & SQLITE_CountRows)!=0
126063 && !pParse->nested
126064 && !pParse->pTriggerTab
126065 && !pParse->bReturning
126066 ){
126067 memCnt = ++pParse->nMem;
126068 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: memCnt);
126069 }
126070
126071#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
126072 /* Special case: A DELETE without a WHERE clause deletes everything.
126073 ** It is easier just to erase the whole table. Prior to version 3.6.5,
126074 ** this optimization caused the row change count (the value returned by
126075 ** API function sqlite3_count_changes) to be set incorrectly.
126076 **
126077 ** The "rcauth==SQLITE_OK" terms is the
126078 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
126079 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
126080 ** the truncate optimization is disabled and all rows are deleted
126081 ** individually.
126082 */
126083 if( rcauth==SQLITE_OK
126084 && pWhere==0
126085 && !bComplex
126086 && !IsVirtual(pTab)
126087#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
126088 && db->xPreUpdateCallback==0
126089#endif
126090 ){
126091 assert( !isView );
126092 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 1, zName: pTab->zName);
126093 if( HasRowid(pTab) ){
126094 sqlite3VdbeAddOp4(p: v, OP_Clear, p1: pTab->tnum, p2: iDb, p3: memCnt ? memCnt : -1,
126095 zP4: pTab->zName, P4_STATIC);
126096 }
126097 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126098 assert( pIdx->pSchema==pTab->pSchema );
126099 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
126100 sqlite3VdbeAddOp3(p: v, OP_Clear, p1: pIdx->tnum, p2: iDb, p3: memCnt ? memCnt : -1);
126101 }else{
126102 sqlite3VdbeAddOp2(p: v, OP_Clear, p1: pIdx->tnum, p2: iDb);
126103 }
126104 }
126105 }else
126106#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
126107 {
126108 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK;
126109 if( sNC.ncFlags & NC_Subquery ) bComplex = 1;
126110 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
126111 if( HasRowid(pTab) ){
126112 /* For a rowid table, initialize the RowSet to an empty set */
126113 pPk = 0;
126114 assert( nPk==1 );
126115 iRowSet = ++pParse->nMem;
126116 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: iRowSet);
126117 }else{
126118 /* For a WITHOUT ROWID table, create an ephemeral table used to
126119 ** hold all primary keys for rows to be deleted. */
126120 pPk = sqlite3PrimaryKeyIndex(pTab);
126121 assert( pPk!=0 );
126122 nPk = pPk->nKeyCol;
126123 iPk = pParse->nMem+1;
126124 pParse->nMem += nPk;
126125 iEphCur = pParse->nTab++;
126126 addrEphOpen = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iEphCur, p2: nPk);
126127 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
126128 }
126129
126130 /* Construct a query to find the rowid or primary key for every row
126131 ** to be deleted, based on the WHERE clause. Set variable eOnePass
126132 ** to indicate the strategy used to implement this delete:
126133 **
126134 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
126135 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
126136 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
126137 */
126138 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
126139 if( pWInfo==0 ) goto delete_from_cleanup;
126140 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
126141 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
126142 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF
126143 || OptimizationDisabled(db, SQLITE_OnePass) );
126144 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
126145 if( sqlite3WhereUsesDeferredSeek(pWInfo) ){
126146 sqlite3VdbeAddOp1(p: v, OP_FinishSeek, p1: iTabCur);
126147 }
126148
126149 /* Keep track of the number of rows to be deleted */
126150 if( memCnt ){
126151 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: memCnt, p2: 1);
126152 }
126153
126154 /* Extract the rowid or primary key for the current row */
126155 if( pPk ){
126156 for(i=0; i<nPk; i++){
126157 assert( pPk->aiColumn[i]>=0 );
126158 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
126159 iCol: pPk->aiColumn[i], regOut: iPk+i);
126160 }
126161 iKey = iPk;
126162 }else{
126163 iKey = ++pParse->nMem;
126164 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol: -1, regOut: iKey);
126165 }
126166
126167 if( eOnePass!=ONEPASS_OFF ){
126168 /* For ONEPASS, no need to store the rowid/primary-key. There is only
126169 ** one, so just keep it in its register(s) and fall through to the
126170 ** delete code. */
126171 nKey = nPk; /* OP_Found will use an unpacked key */
126172 aToOpen = sqlite3DbMallocRawNN(db, n: nIdx+2);
126173 if( aToOpen==0 ){
126174 sqlite3WhereEnd(pWInfo);
126175 goto delete_from_cleanup;
126176 }
126177 memset(s: aToOpen, c: 1, n: nIdx+1);
126178 aToOpen[nIdx+1] = 0;
126179 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
126180 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
126181 if( addrEphOpen ) sqlite3VdbeChangeToNoop(p: v, addr: addrEphOpen);
126182 addrBypass = sqlite3VdbeMakeLabel(pParse);
126183 }else{
126184 if( pPk ){
126185 /* Add the PK key for this row to the temporary table */
126186 iKey = ++pParse->nMem;
126187 nKey = 0; /* Zero tells OP_Found to use a composite key */
126188 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: iPk, p2: nPk, p3: iKey,
126189 zP4: sqlite3IndexAffinityStr(pParse->db, pPk), p4type: nPk);
126190 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iEphCur, p2: iKey, p3: iPk, p4: nPk);
126191 }else{
126192 /* Add the rowid of the row to be deleted to the RowSet */
126193 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
126194 sqlite3VdbeAddOp2(p: v, OP_RowSetAdd, p1: iRowSet, p2: iKey);
126195 }
126196 sqlite3WhereEnd(pWInfo);
126197 }
126198
126199 /* Unless this is a view, open cursors for the table we are
126200 ** deleting from and all its indices. If this is a view, then the
126201 ** only effect this statement has is to fire the INSTEAD OF
126202 ** triggers.
126203 */
126204 if( !isView ){
126205 int iAddrOnce = 0;
126206 if( eOnePass==ONEPASS_MULTI ){
126207 iAddrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
126208 }
126209 testcase( IsVirtual(pTab) );
126210 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
126211 iTabCur, aToOpen, &iDataCur, &iIdxCur);
126212 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
126213 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
126214 if( eOnePass==ONEPASS_MULTI ){
126215 sqlite3VdbeJumpHereOrPopInst(p: v, addr: iAddrOnce);
126216 }
126217 }
126218
126219 /* Set up a loop over the rowids/primary-keys that were found in the
126220 ** where-clause loop above.
126221 */
126222 if( eOnePass!=ONEPASS_OFF ){
126223 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
126224 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
126225 assert( pPk!=0 || IsView(pTab) );
126226 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iDataCur, p2: addrBypass, p3: iKey, p4: nKey);
126227 VdbeCoverage(v);
126228 }
126229 }else if( pPk ){
126230 addrLoop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iEphCur); VdbeCoverage(v);
126231 if( IsVirtual(pTab) ){
126232 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEphCur, p2: 0, p3: iKey);
126233 }else{
126234 sqlite3VdbeAddOp2(p: v, OP_RowData, p1: iEphCur, p2: iKey);
126235 }
126236 assert( nKey==0 ); /* OP_Found will use a composite key */
126237 }else{
126238 addrLoop = sqlite3VdbeAddOp3(p: v, OP_RowSetRead, p1: iRowSet, p2: 0, p3: iKey);
126239 VdbeCoverage(v);
126240 assert( nKey==1 );
126241 }
126242
126243 /* Delete the row */
126244#ifndef SQLITE_OMIT_VIRTUALTABLE
126245 if( IsVirtual(pTab) ){
126246 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
126247 sqlite3VtabMakeWritable(pParse, pTab);
126248 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
126249 sqlite3MayAbort(pParse);
126250 if( eOnePass==ONEPASS_SINGLE ){
126251 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iTabCur);
126252 if( sqlite3IsToplevel(pParse) ){
126253 pParse->isMultiWrite = 0;
126254 }
126255 }
126256 sqlite3VdbeAddOp4(p: v, OP_VUpdate, p1: 0, p2: 1, p3: iKey, zP4: pVTab, P4_VTAB);
126257 sqlite3VdbeChangeP5(p: v, OE_Abort);
126258 }else
126259#endif
126260 {
126261 int count = (pParse->nested==0); /* True to count changes */
126262 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
126263 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
126264 }
126265
126266 /* End of the loop over all rowids/primary-keys. */
126267 if( eOnePass!=ONEPASS_OFF ){
126268 sqlite3VdbeResolveLabel(v, x: addrBypass);
126269 sqlite3WhereEnd(pWInfo);
126270 }else if( pPk ){
126271 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iEphCur, p2: addrLoop+1); VdbeCoverage(v);
126272 sqlite3VdbeJumpHere(p: v, addr: addrLoop);
126273 }else{
126274 sqlite3VdbeGoto(p: v, iDest: addrLoop);
126275 sqlite3VdbeJumpHere(p: v, addr: addrLoop);
126276 }
126277 } /* End non-truncate path */
126278
126279 /* Update the sqlite_sequence table by storing the content of the
126280 ** maximum rowid counter values recorded while inserting into
126281 ** autoincrement tables.
126282 */
126283 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
126284 sqlite3AutoincrementEnd(pParse);
126285 }
126286
126287 /* Return the number of rows that were deleted. If this routine is
126288 ** generating code because of a call to sqlite3NestedParse(), do not
126289 ** invoke the callback function.
126290 */
126291 if( memCnt ){
126292 sqlite3CodeChangeCount(v, regCounter: memCnt, zColName: "rows deleted");
126293 }
126294
126295delete_from_cleanup:
126296 sqlite3AuthContextPop(pContext: &sContext);
126297 sqlite3SrcListDelete(db, pList: pTabList);
126298 sqlite3ExprDelete(db, p: pWhere);
126299#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
126300 sqlite3ExprListDelete(db, pOrderBy);
126301 sqlite3ExprDelete(db, pLimit);
126302#endif
126303 if( aToOpen ) sqlite3DbNNFreeNN(db, p: aToOpen);
126304 return;
126305}
126306/* Make sure "isView" and other macros defined above are undefined. Otherwise
126307** they may interfere with compilation of other functions in this file
126308** (or in another file, if this file becomes part of the amalgamation). */
126309#ifdef isView
126310 #undef isView
126311#endif
126312#ifdef pTrigger
126313 #undef pTrigger
126314#endif
126315
126316/*
126317** This routine generates VDBE code that causes a single row of a
126318** single table to be deleted. Both the original table entry and
126319** all indices are removed.
126320**
126321** Preconditions:
126322**
126323** 1. iDataCur is an open cursor on the btree that is the canonical data
126324** store for the table. (This will be either the table itself,
126325** in the case of a rowid table, or the PRIMARY KEY index in the case
126326** of a WITHOUT ROWID table.)
126327**
126328** 2. Read/write cursors for all indices of pTab must be open as
126329** cursor number iIdxCur+i for the i-th index.
126330**
126331** 3. The primary key for the row to be deleted must be stored in a
126332** sequence of nPk memory cells starting at iPk. If nPk==0 that means
126333** that a search record formed from OP_MakeRecord is contained in the
126334** single memory location iPk.
126335**
126336** eMode:
126337** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
126338** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
126339** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
126340** then this function must seek iDataCur to the entry identified by iPk
126341** and nPk before reading from it.
126342**
126343** If eMode is ONEPASS_MULTI, then this call is being made as part
126344** of a ONEPASS delete that affects multiple rows. In this case, if
126345** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
126346** iDataCur, then its position should be preserved following the delete
126347** operation. Or, if iIdxNoSeek is not a valid cursor number, the
126348** position of iDataCur should be preserved instead.
126349**
126350** iIdxNoSeek:
126351** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
126352** then it identifies an index cursor (from within array of cursors
126353** starting at iIdxCur) that already points to the index entry to be deleted.
126354** Except, this optimization is disabled if there are BEFORE triggers since
126355** the trigger body might have moved the cursor.
126356*/
126357SQLITE_PRIVATE void sqlite3GenerateRowDelete(
126358 Parse *pParse, /* Parsing context */
126359 Table *pTab, /* Table containing the row to be deleted */
126360 Trigger *pTrigger, /* List of triggers to (potentially) fire */
126361 int iDataCur, /* Cursor from which column data is extracted */
126362 int iIdxCur, /* First index cursor */
126363 int iPk, /* First memory cell containing the PRIMARY KEY */
126364 i16 nPk, /* Number of PRIMARY KEY memory cells */
126365 u8 count, /* If non-zero, increment the row change counter */
126366 u8 onconf, /* Default ON CONFLICT policy for triggers */
126367 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
126368 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
126369){
126370 Vdbe *v = pParse->pVdbe; /* Vdbe */
126371 int iOld = 0; /* First register in OLD.* array */
126372 int iLabel; /* Label resolved to end of generated code */
126373 u8 opSeek; /* Seek opcode */
126374
126375 /* Vdbe is guaranteed to have been allocated by this stage. */
126376 assert( v );
126377 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
126378 iDataCur, iIdxCur, iPk, (int)nPk));
126379
126380 /* Seek cursor iCur to the row to delete. If this row no longer exists
126381 ** (this can happen if a trigger program has already deleted it), do
126382 ** not attempt to delete it or fire any DELETE triggers. */
126383 iLabel = sqlite3VdbeMakeLabel(pParse);
126384 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
126385 if( eMode==ONEPASS_OFF ){
126386 sqlite3VdbeAddOp4Int(p: v, op: opSeek, p1: iDataCur, p2: iLabel, p3: iPk, p4: nPk);
126387 VdbeCoverageIf(v, opSeek==OP_NotExists);
126388 VdbeCoverageIf(v, opSeek==OP_NotFound);
126389 }
126390
126391 /* If there are any triggers to fire, allocate a range of registers to
126392 ** use for the old.* references in the triggers. */
126393 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
126394 u32 mask; /* Mask of OLD.* columns in use */
126395 int iCol; /* Iterator used while populating OLD.* */
126396 int addrStart; /* Start of BEFORE trigger programs */
126397
126398 /* TODO: Could use temporary registers here. Also could attempt to
126399 ** avoid copying the contents of the rowid register. */
126400 mask = sqlite3TriggerColmask(
126401 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
126402 );
126403 mask |= sqlite3FkOldmask(pParse, pTab);
126404 iOld = pParse->nMem+1;
126405 pParse->nMem += (1 + pTab->nCol);
126406
126407 /* Populate the OLD.* pseudo-table register array. These values will be
126408 ** used by any BEFORE and AFTER triggers that exist. */
126409 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: iPk, p2: iOld);
126410 for(iCol=0; iCol<pTab->nCol; iCol++){
126411 testcase( mask!=0xffffffff && iCol==31 );
126412 testcase( mask!=0xffffffff && iCol==32 );
126413 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
126414 int kk = sqlite3TableColumnToStorage(pTab, iCol);
126415 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol, regOut: iOld+kk+1);
126416 }
126417 }
126418
126419 /* Invoke BEFORE DELETE trigger programs. */
126420 addrStart = sqlite3VdbeCurrentAddr(p: v);
126421 sqlite3CodeRowTrigger(pParse, pTrigger,
126422 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
126423 );
126424
126425 /* If any BEFORE triggers were coded, then seek the cursor to the
126426 ** row to be deleted again. It may be that the BEFORE triggers moved
126427 ** the cursor or already deleted the row that the cursor was
126428 ** pointing to.
126429 **
126430 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
126431 ** may have moved that cursor.
126432 */
126433 if( addrStart<sqlite3VdbeCurrentAddr(p: v) ){
126434 sqlite3VdbeAddOp4Int(p: v, op: opSeek, p1: iDataCur, p2: iLabel, p3: iPk, p4: nPk);
126435 VdbeCoverageIf(v, opSeek==OP_NotExists);
126436 VdbeCoverageIf(v, opSeek==OP_NotFound);
126437 testcase( iIdxNoSeek>=0 );
126438 iIdxNoSeek = -1;
126439 }
126440
126441 /* Do FK processing. This call checks that any FK constraints that
126442 ** refer to this table (i.e. constraints attached to other tables)
126443 ** are not violated by deleting this row. */
126444 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
126445 }
126446
126447 /* Delete the index and table entries. Skip this step if pTab is really
126448 ** a view (in which case the only effect of the DELETE statement is to
126449 ** fire the INSTEAD OF triggers).
126450 **
126451 ** If variable 'count' is non-zero, then this OP_Delete instruction should
126452 ** invoke the update-hook. The pre-update-hook, on the other hand should
126453 ** be invoked unless table pTab is a system table. The difference is that
126454 ** the update-hook is not invoked for rows removed by REPLACE, but the
126455 ** pre-update-hook is.
126456 */
126457 if( !IsView(pTab) ){
126458 u8 p5 = 0;
126459 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
126460 sqlite3VdbeAddOp2(p: v, OP_Delete, p1: iDataCur, p2: (count?OPFLAG_NCHANGE:0));
126461 if( pParse->nested==0 || 0==sqlite3_stricmp(zLeft: pTab->zName, zRight: "sqlite_stat1") ){
126462 sqlite3VdbeAppendP4(p: v, pP4: (char*)pTab, P4_TABLE);
126463 }
126464 if( eMode!=ONEPASS_OFF ){
126465 sqlite3VdbeChangeP5(p: v, OPFLAG_AUXDELETE);
126466 }
126467 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
126468 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: iIdxNoSeek);
126469 }
126470 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
126471 sqlite3VdbeChangeP5(p: v, p5);
126472 }
126473
126474 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
126475 ** handle rows (possibly in other tables) that refer via a foreign key
126476 ** to the row just deleted. */
126477 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
126478
126479 /* Invoke AFTER DELETE trigger programs. */
126480 if( pTrigger ){
126481 sqlite3CodeRowTrigger(pParse, pTrigger,
126482 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
126483 );
126484 }
126485
126486 /* Jump here if the row had already been deleted before any BEFORE
126487 ** trigger programs were invoked. Or if a trigger program throws a
126488 ** RAISE(IGNORE) exception. */
126489 sqlite3VdbeResolveLabel(v, x: iLabel);
126490 VdbeModuleComment((v, "END: GenRowDel()"));
126491}
126492
126493/*
126494** This routine generates VDBE code that causes the deletion of all
126495** index entries associated with a single row of a single table, pTab
126496**
126497** Preconditions:
126498**
126499** 1. A read/write cursor "iDataCur" must be open on the canonical storage
126500** btree for the table pTab. (This will be either the table itself
126501** for rowid tables or to the primary key index for WITHOUT ROWID
126502** tables.)
126503**
126504** 2. Read/write cursors for all indices of pTab must be open as
126505** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
126506** index is the 0-th index.)
126507**
126508** 3. The "iDataCur" cursor must be already be positioned on the row
126509** that is to be deleted.
126510*/
126511SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
126512 Parse *pParse, /* Parsing and code generating context */
126513 Table *pTab, /* Table containing the row to be deleted */
126514 int iDataCur, /* Cursor of table holding data. */
126515 int iIdxCur, /* First index cursor */
126516 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
126517 int iIdxNoSeek /* Do not delete from this cursor */
126518){
126519 int i; /* Index loop counter */
126520 int r1 = -1; /* Register holding an index key */
126521 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
126522 Index *pIdx; /* Current index */
126523 Index *pPrior = 0; /* Prior index */
126524 Vdbe *v; /* The prepared statement under construction */
126525 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
126526
126527 v = pParse->pVdbe;
126528 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
126529 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
126530 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
126531 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
126532 if( pIdx==pPk ) continue;
126533 if( iIdxCur+i==iIdxNoSeek ) continue;
126534 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
126535 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
126536 &iPartIdxLabel, pPrior, r1);
126537 sqlite3VdbeAddOp3(p: v, OP_IdxDelete, p1: iIdxCur+i, p2: r1,
126538 p3: pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
126539 sqlite3VdbeChangeP5(p: v, p5: 1); /* Cause IdxDelete to error if no entry found */
126540 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
126541 pPrior = pIdx;
126542 }
126543}
126544
126545/*
126546** Generate code that will assemble an index key and stores it in register
126547** regOut. The key with be for index pIdx which is an index on pTab.
126548** iCur is the index of a cursor open on the pTab table and pointing to
126549** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
126550** iCur must be the cursor of the PRIMARY KEY index.
126551**
126552** Return a register number which is the first in a block of
126553** registers that holds the elements of the index key. The
126554** block of registers has already been deallocated by the time
126555** this routine returns.
126556**
126557** If *piPartIdxLabel is not NULL, fill it in with a label and jump
126558** to that label if pIdx is a partial index that should be skipped.
126559** The label should be resolved using sqlite3ResolvePartIdxLabel().
126560** A partial index should be skipped if its WHERE clause evaluates
126561** to false or null. If pIdx is not a partial index, *piPartIdxLabel
126562** will be set to zero which is an empty label that is ignored by
126563** sqlite3ResolvePartIdxLabel().
126564**
126565** The pPrior and regPrior parameters are used to implement a cache to
126566** avoid unnecessary register loads. If pPrior is not NULL, then it is
126567** a pointer to a different index for which an index key has just been
126568** computed into register regPrior. If the current pIdx index is generating
126569** its key into the same sequence of registers and if pPrior and pIdx share
126570** a column in common, then the register corresponding to that column already
126571** holds the correct value and the loading of that register is skipped.
126572** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
126573** on a table with multiple indices, and especially with the ROWID or
126574** PRIMARY KEY columns of the index.
126575*/
126576SQLITE_PRIVATE int sqlite3GenerateIndexKey(
126577 Parse *pParse, /* Parsing context */
126578 Index *pIdx, /* The index for which to generate a key */
126579 int iDataCur, /* Cursor number from which to take column data */
126580 int regOut, /* Put the new key into this register if not 0 */
126581 int prefixOnly, /* Compute only a unique prefix of the key */
126582 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
126583 Index *pPrior, /* Previously generated index key */
126584 int regPrior /* Register holding previous generated key */
126585){
126586 Vdbe *v = pParse->pVdbe;
126587 int j;
126588 int regBase;
126589 int nCol;
126590
126591 if( piPartIdxLabel ){
126592 if( pIdx->pPartIdxWhere ){
126593 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
126594 pParse->iSelfTab = iDataCur + 1;
126595 sqlite3ExprIfFalseDup(pParse, pExpr: pIdx->pPartIdxWhere, dest: *piPartIdxLabel,
126596 SQLITE_JUMPIFNULL);
126597 pParse->iSelfTab = 0;
126598 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
126599 ** pPartIdxWhere may have corrupted regPrior registers */
126600 }else{
126601 *piPartIdxLabel = 0;
126602 }
126603 }
126604 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
126605 regBase = sqlite3GetTempRange(pParse, nReg: nCol);
126606 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
126607 for(j=0; j<nCol; j++){
126608 if( pPrior
126609 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
126610 && pPrior->aiColumn[j]!=XN_EXPR
126611 ){
126612 /* This column was already computed by the previous index */
126613 continue;
126614 }
126615 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur: iDataCur, iIdxCol: j, regOut: regBase+j);
126616 if( pIdx->aiColumn[j]>=0 ){
126617 /* If the column affinity is REAL but the number is an integer, then it
126618 ** might be stored in the table as an integer (using a compact
126619 ** representation) then converted to REAL by an OP_RealAffinity opcode.
126620 ** But we are getting ready to store this value back into an index, where
126621 ** it should be converted by to INTEGER again. So omit the
126622 ** OP_RealAffinity opcode if it is present */
126623 sqlite3VdbeDeletePriorOpcode(p: v, OP_RealAffinity);
126624 }
126625 }
126626 if( regOut ){
126627 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regBase, p2: nCol, p3: regOut);
126628 }
126629 sqlite3ReleaseTempRange(pParse, iReg: regBase, nReg: nCol);
126630 return regBase;
126631}
126632
126633/*
126634** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
126635** because it was a partial index, then this routine should be called to
126636** resolve that label.
126637*/
126638SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
126639 if( iLabel ){
126640 sqlite3VdbeResolveLabel(v: pParse->pVdbe, x: iLabel);
126641 }
126642}
126643
126644/************** End of delete.c **********************************************/
126645/************** Begin file func.c ********************************************/
126646/*
126647** 2002 February 23
126648**
126649** The author disclaims copyright to this source code. In place of
126650** a legal notice, here is a blessing:
126651**
126652** May you do good and not evil.
126653** May you find forgiveness for yourself and forgive others.
126654** May you share freely, never taking more than you give.
126655**
126656*************************************************************************
126657** This file contains the C-language implementations for many of the SQL
126658** functions of SQLite. (Some function, and in particular the date and
126659** time functions, are implemented separately.)
126660*/
126661/* #include "sqliteInt.h" */
126662/* #include <stdlib.h> */
126663/* #include <assert.h> */
126664#ifndef SQLITE_OMIT_FLOATING_POINT
126665/* #include <math.h> */
126666#endif
126667/* #include "vdbeInt.h" */
126668
126669/*
126670** Return the collating function associated with a function.
126671*/
126672static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
126673 VdbeOp *pOp;
126674 assert( context->pVdbe!=0 );
126675 pOp = &context->pVdbe->aOp[context->iOp-1];
126676 assert( pOp->opcode==OP_CollSeq );
126677 assert( pOp->p4type==P4_COLLSEQ );
126678 return pOp->p4.pColl;
126679}
126680
126681/*
126682** Indicate that the accumulator load should be skipped on this
126683** iteration of the aggregate loop.
126684*/
126685static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
126686 assert( context->isError<=0 );
126687 context->isError = -1;
126688 context->skipFlag = 1;
126689}
126690
126691/*
126692** Implementation of the non-aggregate min() and max() functions
126693*/
126694static void minmaxFunc(
126695 sqlite3_context *context,
126696 int argc,
126697 sqlite3_value **argv
126698){
126699 int i;
126700 int mask; /* 0 for min() or 0xffffffff for max() */
126701 int iBest;
126702 CollSeq *pColl;
126703
126704 assert( argc>1 );
126705 mask = sqlite3_user_data(p: context)==0 ? 0 : -1;
126706 pColl = sqlite3GetFuncCollSeq(context);
126707 assert( pColl );
126708 assert( mask==-1 || mask==0 );
126709 iBest = 0;
126710 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
126711 for(i=1; i<argc; i++){
126712 if( sqlite3_value_type(pVal: argv[i])==SQLITE_NULL ) return;
126713 if( (sqlite3MemCompare(pMem1: argv[iBest], pMem2: argv[i], pColl)^mask)>=0 ){
126714 testcase( mask==0 );
126715 iBest = i;
126716 }
126717 }
126718 sqlite3_result_value(pCtx: context, pValue: argv[iBest]);
126719}
126720
126721/*
126722** Return the type of the argument.
126723*/
126724static void typeofFunc(
126725 sqlite3_context *context,
126726 int NotUsed,
126727 sqlite3_value **argv
126728){
126729 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
126730 int i = sqlite3_value_type(pVal: argv[0]) - 1;
126731 UNUSED_PARAMETER(NotUsed);
126732 assert( i>=0 && i<ArraySize(azType) );
126733 assert( SQLITE_INTEGER==1 );
126734 assert( SQLITE_FLOAT==2 );
126735 assert( SQLITE_TEXT==3 );
126736 assert( SQLITE_BLOB==4 );
126737 assert( SQLITE_NULL==5 );
126738 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
126739 ** the datatype code for the initial datatype of the sqlite3_value object
126740 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
126741 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
126742 sqlite3_result_text(pCtx: context, z: azType[i], n: -1, SQLITE_STATIC);
126743}
126744
126745/* subtype(X)
126746**
126747** Return the subtype of X
126748*/
126749static void subtypeFunc(
126750 sqlite3_context *context,
126751 int argc,
126752 sqlite3_value **argv
126753){
126754 UNUSED_PARAMETER(argc);
126755 sqlite3_result_int(pCtx: context, iVal: sqlite3_value_subtype(pVal: argv[0]));
126756}
126757
126758/*
126759** Implementation of the length() function
126760*/
126761static void lengthFunc(
126762 sqlite3_context *context,
126763 int argc,
126764 sqlite3_value **argv
126765){
126766 assert( argc==1 );
126767 UNUSED_PARAMETER(argc);
126768 switch( sqlite3_value_type(pVal: argv[0]) ){
126769 case SQLITE_BLOB:
126770 case SQLITE_INTEGER:
126771 case SQLITE_FLOAT: {
126772 sqlite3_result_int(pCtx: context, iVal: sqlite3_value_bytes(pVal: argv[0]));
126773 break;
126774 }
126775 case SQLITE_TEXT: {
126776 const unsigned char *z = sqlite3_value_text(pVal: argv[0]);
126777 const unsigned char *z0;
126778 unsigned char c;
126779 if( z==0 ) return;
126780 z0 = z;
126781 while( (c = *z)!=0 ){
126782 z++;
126783 if( c>=0xc0 ){
126784 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
126785 }
126786 }
126787 sqlite3_result_int(pCtx: context, iVal: (int)(z-z0));
126788 break;
126789 }
126790 default: {
126791 sqlite3_result_null(pCtx: context);
126792 break;
126793 }
126794 }
126795}
126796
126797/*
126798** Implementation of the octet_length() function
126799*/
126800static void bytelengthFunc(
126801 sqlite3_context *context,
126802 int argc,
126803 sqlite3_value **argv
126804){
126805 assert( argc==1 );
126806 UNUSED_PARAMETER(argc);
126807 switch( sqlite3_value_type(pVal: argv[0]) ){
126808 case SQLITE_BLOB: {
126809 sqlite3_result_int(pCtx: context, iVal: sqlite3_value_bytes(pVal: argv[0]));
126810 break;
126811 }
126812 case SQLITE_INTEGER:
126813 case SQLITE_FLOAT: {
126814 i64 m = sqlite3_context_db_handle(p: context)->enc<=SQLITE_UTF8 ? 1 : 2;
126815 sqlite3_result_int64(pCtx: context, iVal: sqlite3_value_bytes(pVal: argv[0])*m);
126816 break;
126817 }
126818 case SQLITE_TEXT: {
126819 if( sqlite3_value_encoding(pVal: argv[0])<=SQLITE_UTF8 ){
126820 sqlite3_result_int(pCtx: context, iVal: sqlite3_value_bytes(pVal: argv[0]));
126821 }else{
126822 sqlite3_result_int(pCtx: context, iVal: sqlite3_value_bytes16(pVal: argv[0]));
126823 }
126824 break;
126825 }
126826 default: {
126827 sqlite3_result_null(pCtx: context);
126828 break;
126829 }
126830 }
126831}
126832
126833/*
126834** Implementation of the abs() function.
126835**
126836** IMP: R-23979-26855 The abs(X) function returns the absolute value of
126837** the numeric argument X.
126838*/
126839static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
126840 assert( argc==1 );
126841 UNUSED_PARAMETER(argc);
126842 switch( sqlite3_value_type(pVal: argv[0]) ){
126843 case SQLITE_INTEGER: {
126844 i64 iVal = sqlite3_value_int64(pVal: argv[0]);
126845 if( iVal<0 ){
126846 if( iVal==SMALLEST_INT64 ){
126847 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
126848 ** then abs(X) throws an integer overflow error since there is no
126849 ** equivalent positive 64-bit two complement value. */
126850 sqlite3_result_error(pCtx: context, z: "integer overflow", n: -1);
126851 return;
126852 }
126853 iVal = -iVal;
126854 }
126855 sqlite3_result_int64(pCtx: context, iVal);
126856 break;
126857 }
126858 case SQLITE_NULL: {
126859 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
126860 sqlite3_result_null(pCtx: context);
126861 break;
126862 }
126863 default: {
126864 /* Because sqlite3_value_double() returns 0.0 if the argument is not
126865 ** something that can be converted into a number, we have:
126866 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
126867 ** that cannot be converted to a numeric value.
126868 */
126869 double rVal = sqlite3_value_double(pVal: argv[0]);
126870 if( rVal<0 ) rVal = -rVal;
126871 sqlite3_result_double(pCtx: context, rVal);
126872 break;
126873 }
126874 }
126875}
126876
126877/*
126878** Implementation of the instr() function.
126879**
126880** instr(haystack,needle) finds the first occurrence of needle
126881** in haystack and returns the number of previous characters plus 1,
126882** or 0 if needle does not occur within haystack.
126883**
126884** If both haystack and needle are BLOBs, then the result is one more than
126885** the number of bytes in haystack prior to the first occurrence of needle,
126886** or 0 if needle never occurs in haystack.
126887*/
126888static void instrFunc(
126889 sqlite3_context *context,
126890 int argc,
126891 sqlite3_value **argv
126892){
126893 const unsigned char *zHaystack;
126894 const unsigned char *zNeedle;
126895 int nHaystack;
126896 int nNeedle;
126897 int typeHaystack, typeNeedle;
126898 int N = 1;
126899 int isText;
126900 unsigned char firstChar;
126901 sqlite3_value *pC1 = 0;
126902 sqlite3_value *pC2 = 0;
126903
126904 UNUSED_PARAMETER(argc);
126905 typeHaystack = sqlite3_value_type(pVal: argv[0]);
126906 typeNeedle = sqlite3_value_type(pVal: argv[1]);
126907 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
126908 nHaystack = sqlite3_value_bytes(pVal: argv[0]);
126909 nNeedle = sqlite3_value_bytes(pVal: argv[1]);
126910 if( nNeedle>0 ){
126911 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
126912 zHaystack = sqlite3_value_blob(pVal: argv[0]);
126913 zNeedle = sqlite3_value_blob(pVal: argv[1]);
126914 isText = 0;
126915 }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
126916 zHaystack = sqlite3_value_text(pVal: argv[0]);
126917 zNeedle = sqlite3_value_text(pVal: argv[1]);
126918 isText = 1;
126919 }else{
126920 pC1 = sqlite3_value_dup(pOrig: argv[0]);
126921 zHaystack = sqlite3_value_text(pVal: pC1);
126922 if( zHaystack==0 ) goto endInstrOOM;
126923 nHaystack = sqlite3_value_bytes(pVal: pC1);
126924 pC2 = sqlite3_value_dup(pOrig: argv[1]);
126925 zNeedle = sqlite3_value_text(pVal: pC2);
126926 if( zNeedle==0 ) goto endInstrOOM;
126927 nNeedle = sqlite3_value_bytes(pVal: pC2);
126928 isText = 1;
126929 }
126930 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
126931 firstChar = zNeedle[0];
126932 while( nNeedle<=nHaystack
126933 && (zHaystack[0]!=firstChar || memcmp(s1: zHaystack, s2: zNeedle, n: nNeedle)!=0)
126934 ){
126935 N++;
126936 do{
126937 nHaystack--;
126938 zHaystack++;
126939 }while( isText && (zHaystack[0]&0xc0)==0x80 );
126940 }
126941 if( nNeedle>nHaystack ) N = 0;
126942 }
126943 sqlite3_result_int(pCtx: context, iVal: N);
126944endInstr:
126945 sqlite3_value_free(pOld: pC1);
126946 sqlite3_value_free(pOld: pC2);
126947 return;
126948endInstrOOM:
126949 sqlite3_result_error_nomem(pCtx: context);
126950 goto endInstr;
126951}
126952
126953/*
126954** Implementation of the printf() (a.k.a. format()) SQL function.
126955*/
126956static void printfFunc(
126957 sqlite3_context *context,
126958 int argc,
126959 sqlite3_value **argv
126960){
126961 PrintfArguments x;
126962 StrAccum str;
126963 const char *zFormat;
126964 int n;
126965 sqlite3 *db = sqlite3_context_db_handle(p: context);
126966
126967 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(pVal: argv[0]))!=0 ){
126968 x.nArg = argc-1;
126969 x.nUsed = 0;
126970 x.apArg = argv+1;
126971 sqlite3StrAccumInit(p: &str, db, zBase: 0, n: 0, mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
126972 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
126973 sqlite3_str_appendf(p: &str, zFormat, &x);
126974 n = str.nChar;
126975 sqlite3_result_text(pCtx: context, z: sqlite3StrAccumFinish(p: &str), n,
126976 SQLITE_DYNAMIC);
126977 }
126978}
126979
126980/*
126981** Implementation of the substr() function.
126982**
126983** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
126984** p1 is 1-indexed. So substr(x,1,1) returns the first character
126985** of x. If x is text, then we actually count UTF-8 characters.
126986** If x is a blob, then we count bytes.
126987**
126988** If p1 is negative, then we begin abs(p1) from the end of x[].
126989**
126990** If p2 is negative, return the p2 characters preceding p1.
126991*/
126992static void substrFunc(
126993 sqlite3_context *context,
126994 int argc,
126995 sqlite3_value **argv
126996){
126997 const unsigned char *z;
126998 const unsigned char *z2;
126999 int len;
127000 int p0type;
127001 i64 p1, p2;
127002 int negP2 = 0;
127003
127004 assert( argc==3 || argc==2 );
127005 if( sqlite3_value_type(pVal: argv[1])==SQLITE_NULL
127006 || (argc==3 && sqlite3_value_type(pVal: argv[2])==SQLITE_NULL)
127007 ){
127008 return;
127009 }
127010 p0type = sqlite3_value_type(pVal: argv[0]);
127011 p1 = sqlite3_value_int(pVal: argv[1]);
127012 if( p0type==SQLITE_BLOB ){
127013 len = sqlite3_value_bytes(pVal: argv[0]);
127014 z = sqlite3_value_blob(pVal: argv[0]);
127015 if( z==0 ) return;
127016 assert( len==sqlite3_value_bytes(argv[0]) );
127017 }else{
127018 z = sqlite3_value_text(pVal: argv[0]);
127019 if( z==0 ) return;
127020 len = 0;
127021 if( p1<0 ){
127022 for(z2=z; *z2; len++){
127023 SQLITE_SKIP_UTF8(z2);
127024 }
127025 }
127026 }
127027#ifdef SQLITE_SUBSTR_COMPATIBILITY
127028 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
127029 ** as substr(X,1,N) - it returns the first N characters of X. This
127030 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
127031 ** from 2009-02-02 for compatibility of applications that exploited the
127032 ** old buggy behavior. */
127033 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
127034#endif
127035 if( argc==3 ){
127036 p2 = sqlite3_value_int(pVal: argv[2]);
127037 if( p2<0 ){
127038 p2 = -p2;
127039 negP2 = 1;
127040 }
127041 }else{
127042 p2 = sqlite3_context_db_handle(p: context)->aLimit[SQLITE_LIMIT_LENGTH];
127043 }
127044 if( p1<0 ){
127045 p1 += len;
127046 if( p1<0 ){
127047 p2 += p1;
127048 if( p2<0 ) p2 = 0;
127049 p1 = 0;
127050 }
127051 }else if( p1>0 ){
127052 p1--;
127053 }else if( p2>0 ){
127054 p2--;
127055 }
127056 if( negP2 ){
127057 p1 -= p2;
127058 if( p1<0 ){
127059 p2 += p1;
127060 p1 = 0;
127061 }
127062 }
127063 assert( p1>=0 && p2>=0 );
127064 if( p0type!=SQLITE_BLOB ){
127065 while( *z && p1 ){
127066 SQLITE_SKIP_UTF8(z);
127067 p1--;
127068 }
127069 for(z2=z; *z2 && p2; p2--){
127070 SQLITE_SKIP_UTF8(z2);
127071 }
127072 sqlite3_result_text64(pCtx: context, z: (char*)z, n: z2-z, SQLITE_TRANSIENT,
127073 SQLITE_UTF8);
127074 }else{
127075 if( p1+p2>len ){
127076 p2 = len-p1;
127077 if( p2<0 ) p2 = 0;
127078 }
127079 sqlite3_result_blob64(pCtx: context, z: (char*)&z[p1], n: (u64)p2, SQLITE_TRANSIENT);
127080 }
127081}
127082
127083/*
127084** Implementation of the round() function
127085*/
127086#ifndef SQLITE_OMIT_FLOATING_POINT
127087static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
127088 int n = 0;
127089 double r;
127090 char *zBuf;
127091 assert( argc==1 || argc==2 );
127092 if( argc==2 ){
127093 if( SQLITE_NULL==sqlite3_value_type(pVal: argv[1]) ) return;
127094 n = sqlite3_value_int(pVal: argv[1]);
127095 if( n>30 ) n = 30;
127096 if( n<0 ) n = 0;
127097 }
127098 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
127099 r = sqlite3_value_double(pVal: argv[0]);
127100 /* If Y==0 and X will fit in a 64-bit int,
127101 ** handle the rounding directly,
127102 ** otherwise use printf.
127103 */
127104 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
127105 /* The value has no fractional part so there is nothing to round */
127106 }else if( n==0 ){
127107 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
127108 }else{
127109 zBuf = sqlite3_mprintf(zFormat: "%!.*f",n,r);
127110 if( zBuf==0 ){
127111 sqlite3_result_error_nomem(pCtx: context);
127112 return;
127113 }
127114 sqlite3AtoF(z: zBuf, pResult: &r, length: sqlite3Strlen30(z: zBuf), SQLITE_UTF8);
127115 sqlite3_free(p: zBuf);
127116 }
127117 sqlite3_result_double(pCtx: context, rVal: r);
127118}
127119#endif
127120
127121/*
127122** Allocate nByte bytes of space using sqlite3Malloc(). If the
127123** allocation fails, call sqlite3_result_error_nomem() to notify
127124** the database handle that malloc() has failed and return NULL.
127125** If nByte is larger than the maximum string or blob length, then
127126** raise an SQLITE_TOOBIG exception and return NULL.
127127*/
127128static void *contextMalloc(sqlite3_context *context, i64 nByte){
127129 char *z;
127130 sqlite3 *db = sqlite3_context_db_handle(p: context);
127131 assert( nByte>0 );
127132 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
127133 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
127134 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
127135 sqlite3_result_error_toobig(pCtx: context);
127136 z = 0;
127137 }else{
127138 z = sqlite3Malloc(n: nByte);
127139 if( !z ){
127140 sqlite3_result_error_nomem(pCtx: context);
127141 }
127142 }
127143 return z;
127144}
127145
127146/*
127147** Implementation of the upper() and lower() SQL functions.
127148*/
127149static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
127150 char *z1;
127151 const char *z2;
127152 int i, n;
127153 UNUSED_PARAMETER(argc);
127154 z2 = (char*)sqlite3_value_text(pVal: argv[0]);
127155 n = sqlite3_value_bytes(pVal: argv[0]);
127156 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
127157 assert( z2==(char*)sqlite3_value_text(argv[0]) );
127158 if( z2 ){
127159 z1 = contextMalloc(context, nByte: ((i64)n)+1);
127160 if( z1 ){
127161 for(i=0; i<n; i++){
127162 z1[i] = (char)sqlite3Toupper(z2[i]);
127163 }
127164 sqlite3_result_text(pCtx: context, z: z1, n, xDel: sqlite3_free);
127165 }
127166 }
127167}
127168static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
127169 char *z1;
127170 const char *z2;
127171 int i, n;
127172 UNUSED_PARAMETER(argc);
127173 z2 = (char*)sqlite3_value_text(pVal: argv[0]);
127174 n = sqlite3_value_bytes(pVal: argv[0]);
127175 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
127176 assert( z2==(char*)sqlite3_value_text(argv[0]) );
127177 if( z2 ){
127178 z1 = contextMalloc(context, nByte: ((i64)n)+1);
127179 if( z1 ){
127180 for(i=0; i<n; i++){
127181 z1[i] = sqlite3Tolower(z2[i]);
127182 }
127183 sqlite3_result_text(pCtx: context, z: z1, n, xDel: sqlite3_free);
127184 }
127185 }
127186}
127187
127188/*
127189** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
127190** as VDBE code so that unused argument values do not have to be computed.
127191** However, we still need some kind of function implementation for this
127192** routines in the function table. The noopFunc macro provides this.
127193** noopFunc will never be called so it doesn't matter what the implementation
127194** is. We might as well use the "version()" function as a substitute.
127195*/
127196#define noopFunc versionFunc /* Substitute function - never called */
127197
127198/*
127199** Implementation of random(). Return a random integer.
127200*/
127201static void randomFunc(
127202 sqlite3_context *context,
127203 int NotUsed,
127204 sqlite3_value **NotUsed2
127205){
127206 sqlite_int64 r;
127207 UNUSED_PARAMETER2(NotUsed, NotUsed2);
127208 sqlite3_randomness(N: sizeof(r), pBuf: &r);
127209 if( r<0 ){
127210 /* We need to prevent a random number of 0x8000000000000000
127211 ** (or -9223372036854775808) since when you do abs() of that
127212 ** number of you get the same value back again. To do this
127213 ** in a way that is testable, mask the sign bit off of negative
127214 ** values, resulting in a positive value. Then take the
127215 ** 2s complement of that positive value. The end result can
127216 ** therefore be no less than -9223372036854775807.
127217 */
127218 r = -(r & LARGEST_INT64);
127219 }
127220 sqlite3_result_int64(pCtx: context, iVal: r);
127221}
127222
127223/*
127224** Implementation of randomblob(N). Return a random blob
127225** that is N bytes long.
127226*/
127227static void randomBlob(
127228 sqlite3_context *context,
127229 int argc,
127230 sqlite3_value **argv
127231){
127232 sqlite3_int64 n;
127233 unsigned char *p;
127234 assert( argc==1 );
127235 UNUSED_PARAMETER(argc);
127236 n = sqlite3_value_int64(pVal: argv[0]);
127237 if( n<1 ){
127238 n = 1;
127239 }
127240 p = contextMalloc(context, nByte: n);
127241 if( p ){
127242 sqlite3_randomness(N: n, pBuf: p);
127243 sqlite3_result_blob(pCtx: context, z: (char*)p, n, xDel: sqlite3_free);
127244 }
127245}
127246
127247/*
127248** Implementation of the last_insert_rowid() SQL function. The return
127249** value is the same as the sqlite3_last_insert_rowid() API function.
127250*/
127251static void last_insert_rowid(
127252 sqlite3_context *context,
127253 int NotUsed,
127254 sqlite3_value **NotUsed2
127255){
127256 sqlite3 *db = sqlite3_context_db_handle(p: context);
127257 UNUSED_PARAMETER2(NotUsed, NotUsed2);
127258 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
127259 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
127260 ** function. */
127261 sqlite3_result_int64(pCtx: context, iVal: sqlite3_last_insert_rowid(db));
127262}
127263
127264/*
127265** Implementation of the changes() SQL function.
127266**
127267** IMP: R-32760-32347 The changes() SQL function is a wrapper
127268** around the sqlite3_changes64() C/C++ function and hence follows the
127269** same rules for counting changes.
127270*/
127271static void changes(
127272 sqlite3_context *context,
127273 int NotUsed,
127274 sqlite3_value **NotUsed2
127275){
127276 sqlite3 *db = sqlite3_context_db_handle(p: context);
127277 UNUSED_PARAMETER2(NotUsed, NotUsed2);
127278 sqlite3_result_int64(pCtx: context, iVal: sqlite3_changes64(db));
127279}
127280
127281/*
127282** Implementation of the total_changes() SQL function. The return value is
127283** the same as the sqlite3_total_changes64() API function.
127284*/
127285static void total_changes(
127286 sqlite3_context *context,
127287 int NotUsed,
127288 sqlite3_value **NotUsed2
127289){
127290 sqlite3 *db = sqlite3_context_db_handle(p: context);
127291 UNUSED_PARAMETER2(NotUsed, NotUsed2);
127292 /* IMP: R-11217-42568 This function is a wrapper around the
127293 ** sqlite3_total_changes64() C/C++ interface. */
127294 sqlite3_result_int64(pCtx: context, iVal: sqlite3_total_changes64(db));
127295}
127296
127297/*
127298** A structure defining how to do GLOB-style comparisons.
127299*/
127300struct compareInfo {
127301 u8 matchAll; /* "*" or "%" */
127302 u8 matchOne; /* "?" or "_" */
127303 u8 matchSet; /* "[" or 0 */
127304 u8 noCase; /* true to ignore case differences */
127305};
127306
127307/*
127308** For LIKE and GLOB matching on EBCDIC machines, assume that every
127309** character is exactly one byte in size. Also, provide the Utf8Read()
127310** macro for fast reading of the next character in the common case where
127311** the next character is ASCII.
127312*/
127313#if defined(SQLITE_EBCDIC)
127314# define sqlite3Utf8Read(A) (*((*A)++))
127315# define Utf8Read(A) (*(A++))
127316#else
127317# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
127318#endif
127319
127320static const struct compareInfo globInfo = { '*', '?', '[', 0 };
127321/* The correct SQL-92 behavior is for the LIKE operator to ignore
127322** case. Thus 'a' LIKE 'A' would be true. */
127323static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
127324/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
127325** is case sensitive causing 'a' LIKE 'A' to be false */
127326static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
127327
127328/*
127329** Possible error returns from patternMatch()
127330*/
127331#define SQLITE_MATCH 0
127332#define SQLITE_NOMATCH 1
127333#define SQLITE_NOWILDCARDMATCH 2
127334
127335/*
127336** Compare two UTF-8 strings for equality where the first string is
127337** a GLOB or LIKE expression. Return values:
127338**
127339** SQLITE_MATCH: Match
127340** SQLITE_NOMATCH: No match
127341** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
127342**
127343** Globbing rules:
127344**
127345** '*' Matches any sequence of zero or more characters.
127346**
127347** '?' Matches exactly one character.
127348**
127349** [...] Matches one character from the enclosed list of
127350** characters.
127351**
127352** [^...] Matches one character not in the enclosed list.
127353**
127354** With the [...] and [^...] matching, a ']' character can be included
127355** in the list by making it the first character after '[' or '^'. A
127356** range of characters can be specified using '-'. Example:
127357** "[a-z]" matches any single lower-case letter. To match a '-', make
127358** it the last character in the list.
127359**
127360** Like matching rules:
127361**
127362** '%' Matches any sequence of zero or more characters
127363**
127364*** '_' Matches any one character
127365**
127366** Ec Where E is the "esc" character and c is any other
127367** character, including '%', '_', and esc, match exactly c.
127368**
127369** The comments within this routine usually assume glob matching.
127370**
127371** This routine is usually quick, but can be N**2 in the worst case.
127372*/
127373static int patternCompare(
127374 const u8 *zPattern, /* The glob pattern */
127375 const u8 *zString, /* The string to compare against the glob */
127376 const struct compareInfo *pInfo, /* Information about how to do the compare */
127377 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
127378){
127379 u32 c, c2; /* Next pattern and input string chars */
127380 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
127381 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
127382 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
127383 const u8 *zEscaped = 0; /* One past the last escaped input char */
127384
127385 while( (c = Utf8Read(zPattern))!=0 ){
127386 if( c==matchAll ){ /* Match "*" */
127387 /* Skip over multiple "*" characters in the pattern. If there
127388 ** are also "?" characters, skip those as well, but consume a
127389 ** single character of the input string for each "?" skipped */
127390 while( (c=Utf8Read(zPattern)) == matchAll
127391 || (c == matchOne && matchOne!=0) ){
127392 if( c==matchOne && sqlite3Utf8Read(pz: &zString)==0 ){
127393 return SQLITE_NOWILDCARDMATCH;
127394 }
127395 }
127396 if( c==0 ){
127397 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
127398 }else if( c==matchOther ){
127399 if( pInfo->matchSet==0 ){
127400 c = sqlite3Utf8Read(pz: &zPattern);
127401 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
127402 }else{
127403 /* "[...]" immediately follows the "*". We have to do a slow
127404 ** recursive search in this case, but it is an unusual case. */
127405 assert( matchOther<0x80 ); /* '[' is a single-byte character */
127406 while( *zString ){
127407 int bMatch = patternCompare(zPattern: &zPattern[-1],zString,pInfo,matchOther);
127408 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
127409 SQLITE_SKIP_UTF8(zString);
127410 }
127411 return SQLITE_NOWILDCARDMATCH;
127412 }
127413 }
127414
127415 /* At this point variable c contains the first character of the
127416 ** pattern string past the "*". Search in the input string for the
127417 ** first matching character and recursively continue the match from
127418 ** that point.
127419 **
127420 ** For a case-insensitive search, set variable cx to be the same as
127421 ** c but in the other case and search the input string for either
127422 ** c or cx.
127423 */
127424 if( c<0x80 ){
127425 char zStop[3];
127426 int bMatch;
127427 if( noCase ){
127428 zStop[0] = sqlite3Toupper(c);
127429 zStop[1] = sqlite3Tolower(c);
127430 zStop[2] = 0;
127431 }else{
127432 zStop[0] = c;
127433 zStop[1] = 0;
127434 }
127435 while(1){
127436 zString += strcspn(s: (const char*)zString, reject: zStop);
127437 if( zString[0]==0 ) break;
127438 zString++;
127439 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
127440 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
127441 }
127442 }else{
127443 int bMatch;
127444 while( (c2 = Utf8Read(zString))!=0 ){
127445 if( c2!=c ) continue;
127446 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
127447 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
127448 }
127449 }
127450 return SQLITE_NOWILDCARDMATCH;
127451 }
127452 if( c==matchOther ){
127453 if( pInfo->matchSet==0 ){
127454 c = sqlite3Utf8Read(pz: &zPattern);
127455 if( c==0 ) return SQLITE_NOMATCH;
127456 zEscaped = zPattern;
127457 }else{
127458 u32 prior_c = 0;
127459 int seen = 0;
127460 int invert = 0;
127461 c = sqlite3Utf8Read(pz: &zString);
127462 if( c==0 ) return SQLITE_NOMATCH;
127463 c2 = sqlite3Utf8Read(pz: &zPattern);
127464 if( c2=='^' ){
127465 invert = 1;
127466 c2 = sqlite3Utf8Read(pz: &zPattern);
127467 }
127468 if( c2==']' ){
127469 if( c==']' ) seen = 1;
127470 c2 = sqlite3Utf8Read(pz: &zPattern);
127471 }
127472 while( c2 && c2!=']' ){
127473 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
127474 c2 = sqlite3Utf8Read(pz: &zPattern);
127475 if( c>=prior_c && c<=c2 ) seen = 1;
127476 prior_c = 0;
127477 }else{
127478 if( c==c2 ){
127479 seen = 1;
127480 }
127481 prior_c = c2;
127482 }
127483 c2 = sqlite3Utf8Read(pz: &zPattern);
127484 }
127485 if( c2==0 || (seen ^ invert)==0 ){
127486 return SQLITE_NOMATCH;
127487 }
127488 continue;
127489 }
127490 }
127491 c2 = Utf8Read(zString);
127492 if( c==c2 ) continue;
127493 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
127494 continue;
127495 }
127496 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
127497 return SQLITE_NOMATCH;
127498 }
127499 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
127500}
127501
127502/*
127503** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
127504** non-zero if there is no match.
127505*/
127506SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
127507 if( zString==0 ){
127508 return zGlobPattern!=0;
127509 }else if( zGlobPattern==0 ){
127510 return 1;
127511 }else {
127512 return patternCompare(zPattern: (u8*)zGlobPattern, zString: (u8*)zString, pInfo: &globInfo, matchOther: '[');
127513 }
127514}
127515
127516/*
127517** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
127518** a miss - like strcmp().
127519*/
127520SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
127521 if( zStr==0 ){
127522 return zPattern!=0;
127523 }else if( zPattern==0 ){
127524 return 1;
127525 }else{
127526 return patternCompare(zPattern: (u8*)zPattern, zString: (u8*)zStr, pInfo: &likeInfoNorm, matchOther: esc);
127527 }
127528}
127529
127530/*
127531** Count the number of times that the LIKE operator (or GLOB which is
127532** just a variation of LIKE) gets called. This is used for testing
127533** only.
127534*/
127535#ifdef SQLITE_TEST
127536SQLITE_API int sqlite3_like_count = 0;
127537#endif
127538
127539
127540/*
127541** Implementation of the like() SQL function. This function implements
127542** the built-in LIKE operator. The first argument to the function is the
127543** pattern and the second argument is the string. So, the SQL statements:
127544**
127545** A LIKE B
127546**
127547** is implemented as like(B,A).
127548**
127549** This same function (with a different compareInfo structure) computes
127550** the GLOB operator.
127551*/
127552static void likeFunc(
127553 sqlite3_context *context,
127554 int argc,
127555 sqlite3_value **argv
127556){
127557 const unsigned char *zA, *zB;
127558 u32 escape;
127559 int nPat;
127560 sqlite3 *db = sqlite3_context_db_handle(p: context);
127561 struct compareInfo *pInfo = sqlite3_user_data(p: context);
127562 struct compareInfo backupInfo;
127563
127564#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
127565 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
127566 || sqlite3_value_type(argv[1])==SQLITE_BLOB
127567 ){
127568#ifdef SQLITE_TEST
127569 sqlite3_like_count++;
127570#endif
127571 sqlite3_result_int(context, 0);
127572 return;
127573 }
127574#endif
127575
127576 /* Limit the length of the LIKE or GLOB pattern to avoid problems
127577 ** of deep recursion and N*N behavior in patternCompare().
127578 */
127579 nPat = sqlite3_value_bytes(pVal: argv[0]);
127580 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
127581 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
127582 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
127583 sqlite3_result_error(pCtx: context, z: "LIKE or GLOB pattern too complex", n: -1);
127584 return;
127585 }
127586 if( argc==3 ){
127587 /* The escape character string must consist of a single UTF-8 character.
127588 ** Otherwise, return an error.
127589 */
127590 const unsigned char *zEsc = sqlite3_value_text(pVal: argv[2]);
127591 if( zEsc==0 ) return;
127592 if( sqlite3Utf8CharLen(zIn: (char*)zEsc, nByte: -1)!=1 ){
127593 sqlite3_result_error(pCtx: context,
127594 z: "ESCAPE expression must be a single character", n: -1);
127595 return;
127596 }
127597 escape = sqlite3Utf8Read(pz: &zEsc);
127598 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
127599 memcpy(dest: &backupInfo, src: pInfo, n: sizeof(backupInfo));
127600 pInfo = &backupInfo;
127601 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
127602 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
127603 }
127604 }else{
127605 escape = pInfo->matchSet;
127606 }
127607 zB = sqlite3_value_text(pVal: argv[0]);
127608 zA = sqlite3_value_text(pVal: argv[1]);
127609 if( zA && zB ){
127610#ifdef SQLITE_TEST
127611 sqlite3_like_count++;
127612#endif
127613 sqlite3_result_int(pCtx: context,
127614 iVal: patternCompare(zPattern: zB, zString: zA, pInfo, matchOther: escape)==SQLITE_MATCH);
127615 }
127616}
127617
127618/*
127619** Implementation of the NULLIF(x,y) function. The result is the first
127620** argument if the arguments are different. The result is NULL if the
127621** arguments are equal to each other.
127622*/
127623static void nullifFunc(
127624 sqlite3_context *context,
127625 int NotUsed,
127626 sqlite3_value **argv
127627){
127628 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
127629 UNUSED_PARAMETER(NotUsed);
127630 if( sqlite3MemCompare(pMem1: argv[0], pMem2: argv[1], pColl)!=0 ){
127631 sqlite3_result_value(pCtx: context, pValue: argv[0]);
127632 }
127633}
127634
127635/*
127636** Implementation of the sqlite_version() function. The result is the version
127637** of the SQLite library that is running.
127638*/
127639static void versionFunc(
127640 sqlite3_context *context,
127641 int NotUsed,
127642 sqlite3_value **NotUsed2
127643){
127644 UNUSED_PARAMETER2(NotUsed, NotUsed2);
127645 /* IMP: R-48699-48617 This function is an SQL wrapper around the
127646 ** sqlite3_libversion() C-interface. */
127647 sqlite3_result_text(pCtx: context, z: sqlite3_libversion(), n: -1, SQLITE_STATIC);
127648}
127649
127650/*
127651** Implementation of the sqlite_source_id() function. The result is a string
127652** that identifies the particular version of the source code used to build
127653** SQLite.
127654*/
127655static void sourceidFunc(
127656 sqlite3_context *context,
127657 int NotUsed,
127658 sqlite3_value **NotUsed2
127659){
127660 UNUSED_PARAMETER2(NotUsed, NotUsed2);
127661 /* IMP: R-24470-31136 This function is an SQL wrapper around the
127662 ** sqlite3_sourceid() C interface. */
127663 sqlite3_result_text(pCtx: context, z: sqlite3_sourceid(), n: -1, SQLITE_STATIC);
127664}
127665
127666/*
127667** Implementation of the sqlite_log() function. This is a wrapper around
127668** sqlite3_log(). The return value is NULL. The function exists purely for
127669** its side-effects.
127670*/
127671static void errlogFunc(
127672 sqlite3_context *context,
127673 int argc,
127674 sqlite3_value **argv
127675){
127676 UNUSED_PARAMETER(argc);
127677 UNUSED_PARAMETER(context);
127678 sqlite3_log(iErrCode: sqlite3_value_int(pVal: argv[0]), zFormat: "%s", sqlite3_value_text(pVal: argv[1]));
127679}
127680
127681/*
127682** Implementation of the sqlite_compileoption_used() function.
127683** The result is an integer that identifies if the compiler option
127684** was used to build SQLite.
127685*/
127686#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
127687static void compileoptionusedFunc(
127688 sqlite3_context *context,
127689 int argc,
127690 sqlite3_value **argv
127691){
127692 const char *zOptName;
127693 assert( argc==1 );
127694 UNUSED_PARAMETER(argc);
127695 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
127696 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
127697 ** function.
127698 */
127699 if( (zOptName = (const char*)sqlite3_value_text(pVal: argv[0]))!=0 ){
127700 sqlite3_result_int(pCtx: context, iVal: sqlite3_compileoption_used(zOptName));
127701 }
127702}
127703#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
127704
127705/*
127706** Implementation of the sqlite_compileoption_get() function.
127707** The result is a string that identifies the compiler options
127708** used to build SQLite.
127709*/
127710#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
127711static void compileoptiongetFunc(
127712 sqlite3_context *context,
127713 int argc,
127714 sqlite3_value **argv
127715){
127716 int n;
127717 assert( argc==1 );
127718 UNUSED_PARAMETER(argc);
127719 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
127720 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
127721 */
127722 n = sqlite3_value_int(pVal: argv[0]);
127723 sqlite3_result_text(pCtx: context, z: sqlite3_compileoption_get(N: n), n: -1, SQLITE_STATIC);
127724}
127725#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
127726
127727/* Array for converting from half-bytes (nybbles) into ASCII hex
127728** digits. */
127729static const char hexdigits[] = {
127730 '0', '1', '2', '3', '4', '5', '6', '7',
127731 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
127732};
127733
127734/*
127735** Append to pStr text that is the SQL literal representation of the
127736** value contained in pValue.
127737*/
127738SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
127739 /* As currently implemented, the string must be initially empty.
127740 ** we might relax this requirement in the future, but that will
127741 ** require enhancements to the implementation. */
127742 assert( pStr!=0 && pStr->nChar==0 );
127743
127744 switch( sqlite3_value_type(pVal: pValue) ){
127745 case SQLITE_FLOAT: {
127746 double r1, r2;
127747 const char *zVal;
127748 r1 = sqlite3_value_double(pVal: pValue);
127749 sqlite3_str_appendf(p: pStr, zFormat: "%!.15g", r1);
127750 zVal = sqlite3_str_value(p: pStr);
127751 if( zVal ){
127752 sqlite3AtoF(z: zVal, pResult: &r2, length: pStr->nChar, SQLITE_UTF8);
127753 if( r1!=r2 ){
127754 sqlite3_str_reset(p: pStr);
127755 sqlite3_str_appendf(p: pStr, zFormat: "%!.20e", r1);
127756 }
127757 }
127758 break;
127759 }
127760 case SQLITE_INTEGER: {
127761 sqlite3_str_appendf(p: pStr, zFormat: "%lld", sqlite3_value_int64(pVal: pValue));
127762 break;
127763 }
127764 case SQLITE_BLOB: {
127765 char const *zBlob = sqlite3_value_blob(pVal: pValue);
127766 i64 nBlob = sqlite3_value_bytes(pVal: pValue);
127767 assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */
127768 sqlite3StrAccumEnlarge(p: pStr, N: nBlob*2 + 4);
127769 if( pStr->accError==0 ){
127770 char *zText = pStr->zText;
127771 int i;
127772 for(i=0; i<nBlob; i++){
127773 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
127774 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
127775 }
127776 zText[(nBlob*2)+2] = '\'';
127777 zText[(nBlob*2)+3] = '\0';
127778 zText[0] = 'X';
127779 zText[1] = '\'';
127780 pStr->nChar = nBlob*2 + 3;
127781 }
127782 break;
127783 }
127784 case SQLITE_TEXT: {
127785 const unsigned char *zArg = sqlite3_value_text(pVal: pValue);
127786 sqlite3_str_appendf(p: pStr, zFormat: "%Q", zArg);
127787 break;
127788 }
127789 default: {
127790 assert( sqlite3_value_type(pValue)==SQLITE_NULL );
127791 sqlite3_str_append(p: pStr, z: "NULL", N: 4);
127792 break;
127793 }
127794 }
127795}
127796
127797/*
127798** Implementation of the QUOTE() function.
127799**
127800** The quote(X) function returns the text of an SQL literal which is the
127801** value of its argument suitable for inclusion into an SQL statement.
127802** Strings are surrounded by single-quotes with escapes on interior quotes
127803** as needed. BLOBs are encoded as hexadecimal literals. Strings with
127804** embedded NUL characters cannot be represented as string literals in SQL
127805** and hence the returned string literal is truncated prior to the first NUL.
127806*/
127807static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
127808 sqlite3_str str;
127809 sqlite3 *db = sqlite3_context_db_handle(p: context);
127810 assert( argc==1 );
127811 UNUSED_PARAMETER(argc);
127812 sqlite3StrAccumInit(p: &str, db, zBase: 0, n: 0, mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
127813 sqlite3QuoteValue(pStr: &str,pValue: argv[0]);
127814 sqlite3_result_text(pCtx: context, z: sqlite3StrAccumFinish(p: &str), n: str.nChar,
127815 SQLITE_DYNAMIC);
127816 if( str.accError!=SQLITE_OK ){
127817 sqlite3_result_null(pCtx: context);
127818 sqlite3_result_error_code(pCtx: context, errCode: str.accError);
127819 }
127820}
127821
127822/*
127823** The unicode() function. Return the integer unicode code-point value
127824** for the first character of the input string.
127825*/
127826static void unicodeFunc(
127827 sqlite3_context *context,
127828 int argc,
127829 sqlite3_value **argv
127830){
127831 const unsigned char *z = sqlite3_value_text(pVal: argv[0]);
127832 (void)argc;
127833 if( z && z[0] ) sqlite3_result_int(pCtx: context, iVal: sqlite3Utf8Read(pz: &z));
127834}
127835
127836/*
127837** The char() function takes zero or more arguments, each of which is
127838** an integer. It constructs a string where each character of the string
127839** is the unicode character for the corresponding integer argument.
127840*/
127841static void charFunc(
127842 sqlite3_context *context,
127843 int argc,
127844 sqlite3_value **argv
127845){
127846 unsigned char *z, *zOut;
127847 int i;
127848 zOut = z = sqlite3_malloc64( n: argc*4+1 );
127849 if( z==0 ){
127850 sqlite3_result_error_nomem(pCtx: context);
127851 return;
127852 }
127853 for(i=0; i<argc; i++){
127854 sqlite3_int64 x;
127855 unsigned c;
127856 x = sqlite3_value_int64(pVal: argv[i]);
127857 if( x<0 || x>0x10ffff ) x = 0xfffd;
127858 c = (unsigned)(x & 0x1fffff);
127859 if( c<0x00080 ){
127860 *zOut++ = (u8)(c&0xFF);
127861 }else if( c<0x00800 ){
127862 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
127863 *zOut++ = 0x80 + (u8)(c & 0x3F);
127864 }else if( c<0x10000 ){
127865 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
127866 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
127867 *zOut++ = 0x80 + (u8)(c & 0x3F);
127868 }else{
127869 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
127870 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
127871 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
127872 *zOut++ = 0x80 + (u8)(c & 0x3F);
127873 } \
127874 }
127875 *zOut = 0;
127876 sqlite3_result_text64(pCtx: context, z: (char*)z, n: zOut-z, xDel: sqlite3_free, SQLITE_UTF8);
127877}
127878
127879/*
127880** The hex() function. Interpret the argument as a blob. Return
127881** a hexadecimal rendering as text.
127882*/
127883static void hexFunc(
127884 sqlite3_context *context,
127885 int argc,
127886 sqlite3_value **argv
127887){
127888 int i, n;
127889 const unsigned char *pBlob;
127890 char *zHex, *z;
127891 assert( argc==1 );
127892 UNUSED_PARAMETER(argc);
127893 pBlob = sqlite3_value_blob(pVal: argv[0]);
127894 n = sqlite3_value_bytes(pVal: argv[0]);
127895 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
127896 z = zHex = contextMalloc(context, nByte: ((i64)n)*2 + 1);
127897 if( zHex ){
127898 for(i=0; i<n; i++, pBlob++){
127899 unsigned char c = *pBlob;
127900 *(z++) = hexdigits[(c>>4)&0xf];
127901 *(z++) = hexdigits[c&0xf];
127902 }
127903 *z = 0;
127904 sqlite3_result_text(pCtx: context, z: zHex, n: n*2, xDel: sqlite3_free);
127905 }
127906}
127907
127908/*
127909** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
127910** contains character ch, or 0 if it does not.
127911*/
127912static int strContainsChar(const u8 *zStr, int nStr, u32 ch){
127913 const u8 *zEnd = &zStr[nStr];
127914 const u8 *z = zStr;
127915 while( z<zEnd ){
127916 u32 tst = Utf8Read(z);
127917 if( tst==ch ) return 1;
127918 }
127919 return 0;
127920}
127921
127922/*
127923** The unhex() function. This function may be invoked with either one or
127924** two arguments. In both cases the first argument is interpreted as text
127925** a text value containing a set of pairs of hexadecimal digits which are
127926** decoded and returned as a blob.
127927**
127928** If there is only a single argument, then it must consist only of an
127929** even number of hexadecimal digits. Otherwise, return NULL.
127930**
127931** Or, if there is a second argument, then any character that appears in
127932** the second argument is also allowed to appear between pairs of hexadecimal
127933** digits in the first argument. If any other character appears in the
127934** first argument, or if one of the allowed characters appears between
127935** two hexadecimal digits that make up a single byte, NULL is returned.
127936**
127937** The following expressions are all true:
127938**
127939** unhex('ABCD') IS x'ABCD'
127940** unhex('AB CD') IS NULL
127941** unhex('AB CD', ' ') IS x'ABCD'
127942** unhex('A BCD', ' ') IS NULL
127943*/
127944static void unhexFunc(
127945 sqlite3_context *pCtx,
127946 int argc,
127947 sqlite3_value **argv
127948){
127949 const u8 *zPass = (const u8*)"";
127950 int nPass = 0;
127951 const u8 *zHex = sqlite3_value_text(pVal: argv[0]);
127952 int nHex = sqlite3_value_bytes(pVal: argv[0]);
127953#ifdef SQLITE_DEBUG
127954 const u8 *zEnd = zHex ? &zHex[nHex] : 0;
127955#endif
127956 u8 *pBlob = 0;
127957 u8 *p = 0;
127958
127959 assert( argc==1 || argc==2 );
127960 if( argc==2 ){
127961 zPass = sqlite3_value_text(pVal: argv[1]);
127962 nPass = sqlite3_value_bytes(pVal: argv[1]);
127963 }
127964 if( !zHex || !zPass ) return;
127965
127966 p = pBlob = contextMalloc(context: pCtx, nByte: (nHex/2)+1);
127967 if( pBlob ){
127968 u8 c; /* Most significant digit of next byte */
127969 u8 d; /* Least significant digit of next byte */
127970
127971 while( (c = *zHex)!=0x00 ){
127972 while( !sqlite3Isxdigit(c) ){
127973 u32 ch = Utf8Read(zHex);
127974 assert( zHex<=zEnd );
127975 if( !strContainsChar(zStr: zPass, nStr: nPass, ch) ) goto unhex_null;
127976 c = *zHex;
127977 if( c==0x00 ) goto unhex_done;
127978 }
127979 zHex++;
127980 assert( *zEnd==0x00 );
127981 assert( zHex<=zEnd );
127982 d = *(zHex++);
127983 if( !sqlite3Isxdigit(d) ) goto unhex_null;
127984 *(p++) = (sqlite3HexToInt(h: c)<<4) | sqlite3HexToInt(h: d);
127985 }
127986 }
127987
127988 unhex_done:
127989 sqlite3_result_blob(pCtx, z: pBlob, n: (p - pBlob), xDel: sqlite3_free);
127990 return;
127991
127992 unhex_null:
127993 sqlite3_free(p: pBlob);
127994 return;
127995}
127996
127997
127998/*
127999** The zeroblob(N) function returns a zero-filled blob of size N bytes.
128000*/
128001static void zeroblobFunc(
128002 sqlite3_context *context,
128003 int argc,
128004 sqlite3_value **argv
128005){
128006 i64 n;
128007 int rc;
128008 assert( argc==1 );
128009 UNUSED_PARAMETER(argc);
128010 n = sqlite3_value_int64(pVal: argv[0]);
128011 if( n<0 ) n = 0;
128012 rc = sqlite3_result_zeroblob64(pCtx: context, n); /* IMP: R-00293-64994 */
128013 if( rc ){
128014 sqlite3_result_error_code(pCtx: context, errCode: rc);
128015 }
128016}
128017
128018/*
128019** The replace() function. Three arguments are all strings: call
128020** them A, B, and C. The result is also a string which is derived
128021** from A by replacing every occurrence of B with C. The match
128022** must be exact. Collating sequences are not used.
128023*/
128024static void replaceFunc(
128025 sqlite3_context *context,
128026 int argc,
128027 sqlite3_value **argv
128028){
128029 const unsigned char *zStr; /* The input string A */
128030 const unsigned char *zPattern; /* The pattern string B */
128031 const unsigned char *zRep; /* The replacement string C */
128032 unsigned char *zOut; /* The output */
128033 int nStr; /* Size of zStr */
128034 int nPattern; /* Size of zPattern */
128035 int nRep; /* Size of zRep */
128036 i64 nOut; /* Maximum size of zOut */
128037 int loopLimit; /* Last zStr[] that might match zPattern[] */
128038 int i, j; /* Loop counters */
128039 unsigned cntExpand; /* Number zOut expansions */
128040 sqlite3 *db = sqlite3_context_db_handle(p: context);
128041
128042 assert( argc==3 );
128043 UNUSED_PARAMETER(argc);
128044 zStr = sqlite3_value_text(pVal: argv[0]);
128045 if( zStr==0 ) return;
128046 nStr = sqlite3_value_bytes(pVal: argv[0]);
128047 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
128048 zPattern = sqlite3_value_text(pVal: argv[1]);
128049 if( zPattern==0 ){
128050 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
128051 || sqlite3_context_db_handle(context)->mallocFailed );
128052 return;
128053 }
128054 if( zPattern[0]==0 ){
128055 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
128056 sqlite3_result_value(pCtx: context, pValue: argv[0]);
128057 return;
128058 }
128059 nPattern = sqlite3_value_bytes(pVal: argv[1]);
128060 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
128061 zRep = sqlite3_value_text(pVal: argv[2]);
128062 if( zRep==0 ) return;
128063 nRep = sqlite3_value_bytes(pVal: argv[2]);
128064 assert( zRep==sqlite3_value_text(argv[2]) );
128065 nOut = nStr + 1;
128066 assert( nOut<SQLITE_MAX_LENGTH );
128067 zOut = contextMalloc(context, nByte: (i64)nOut);
128068 if( zOut==0 ){
128069 return;
128070 }
128071 loopLimit = nStr - nPattern;
128072 cntExpand = 0;
128073 for(i=j=0; i<=loopLimit; i++){
128074 if( zStr[i]!=zPattern[0] || memcmp(s1: &zStr[i], s2: zPattern, n: nPattern) ){
128075 zOut[j++] = zStr[i];
128076 }else{
128077 if( nRep>nPattern ){
128078 nOut += nRep - nPattern;
128079 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
128080 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
128081 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
128082 sqlite3_result_error_toobig(pCtx: context);
128083 sqlite3_free(p: zOut);
128084 return;
128085 }
128086 cntExpand++;
128087 if( (cntExpand&(cntExpand-1))==0 ){
128088 /* Grow the size of the output buffer only on substitutions
128089 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
128090 u8 *zOld;
128091 zOld = zOut;
128092 zOut = sqlite3Realloc(pOld: zOut, nBytes: (int)nOut + (nOut - nStr - 1));
128093 if( zOut==0 ){
128094 sqlite3_result_error_nomem(pCtx: context);
128095 sqlite3_free(p: zOld);
128096 return;
128097 }
128098 }
128099 }
128100 memcpy(dest: &zOut[j], src: zRep, n: nRep);
128101 j += nRep;
128102 i += nPattern-1;
128103 }
128104 }
128105 assert( j+nStr-i+1<=nOut );
128106 memcpy(dest: &zOut[j], src: &zStr[i], n: nStr-i);
128107 j += nStr - i;
128108 assert( j<=nOut );
128109 zOut[j] = 0;
128110 sqlite3_result_text(pCtx: context, z: (char*)zOut, n: j, xDel: sqlite3_free);
128111}
128112
128113/*
128114** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
128115** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
128116*/
128117static void trimFunc(
128118 sqlite3_context *context,
128119 int argc,
128120 sqlite3_value **argv
128121){
128122 const unsigned char *zIn; /* Input string */
128123 const unsigned char *zCharSet; /* Set of characters to trim */
128124 unsigned int nIn; /* Number of bytes in input */
128125 int flags; /* 1: trimleft 2: trimright 3: trim */
128126 int i; /* Loop counter */
128127 unsigned int *aLen = 0; /* Length of each character in zCharSet */
128128 unsigned char **azChar = 0; /* Individual characters in zCharSet */
128129 int nChar; /* Number of characters in zCharSet */
128130
128131 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ){
128132 return;
128133 }
128134 zIn = sqlite3_value_text(pVal: argv[0]);
128135 if( zIn==0 ) return;
128136 nIn = (unsigned)sqlite3_value_bytes(pVal: argv[0]);
128137 assert( zIn==sqlite3_value_text(argv[0]) );
128138 if( argc==1 ){
128139 static const unsigned lenOne[] = { 1 };
128140 static unsigned char * const azOne[] = { (u8*)" " };
128141 nChar = 1;
128142 aLen = (unsigned*)lenOne;
128143 azChar = (unsigned char **)azOne;
128144 zCharSet = 0;
128145 }else if( (zCharSet = sqlite3_value_text(pVal: argv[1]))==0 ){
128146 return;
128147 }else{
128148 const unsigned char *z;
128149 for(z=zCharSet, nChar=0; *z; nChar++){
128150 SQLITE_SKIP_UTF8(z);
128151 }
128152 if( nChar>0 ){
128153 azChar = contextMalloc(context,
128154 nByte: ((i64)nChar)*(sizeof(char*)+sizeof(unsigned)));
128155 if( azChar==0 ){
128156 return;
128157 }
128158 aLen = (unsigned*)&azChar[nChar];
128159 for(z=zCharSet, nChar=0; *z; nChar++){
128160 azChar[nChar] = (unsigned char *)z;
128161 SQLITE_SKIP_UTF8(z);
128162 aLen[nChar] = (unsigned)(z - azChar[nChar]);
128163 }
128164 }
128165 }
128166 if( nChar>0 ){
128167 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
128168 if( flags & 1 ){
128169 while( nIn>0 ){
128170 unsigned int len = 0;
128171 for(i=0; i<nChar; i++){
128172 len = aLen[i];
128173 if( len<=nIn && memcmp(s1: zIn, s2: azChar[i], n: len)==0 ) break;
128174 }
128175 if( i>=nChar ) break;
128176 zIn += len;
128177 nIn -= len;
128178 }
128179 }
128180 if( flags & 2 ){
128181 while( nIn>0 ){
128182 unsigned int len = 0;
128183 for(i=0; i<nChar; i++){
128184 len = aLen[i];
128185 if( len<=nIn && memcmp(s1: &zIn[nIn-len],s2: azChar[i],n: len)==0 ) break;
128186 }
128187 if( i>=nChar ) break;
128188 nIn -= len;
128189 }
128190 }
128191 if( zCharSet ){
128192 sqlite3_free(p: azChar);
128193 }
128194 }
128195 sqlite3_result_text(pCtx: context, z: (char*)zIn, n: nIn, SQLITE_TRANSIENT);
128196}
128197
128198
128199#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
128200/*
128201** The "unknown" function is automatically substituted in place of
128202** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
128203** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
128204** When the "sqlite3" command-line shell is built using this functionality,
128205** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
128206** involving application-defined functions to be examined in a generic
128207** sqlite3 shell.
128208*/
128209static void unknownFunc(
128210 sqlite3_context *context,
128211 int argc,
128212 sqlite3_value **argv
128213){
128214 /* no-op */
128215 (void)context;
128216 (void)argc;
128217 (void)argv;
128218}
128219#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
128220
128221
128222/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
128223** is only available if the SQLITE_SOUNDEX compile-time option is used
128224** when SQLite is built.
128225*/
128226#ifdef SQLITE_SOUNDEX
128227/*
128228** Compute the soundex encoding of a word.
128229**
128230** IMP: R-59782-00072 The soundex(X) function returns a string that is the
128231** soundex encoding of the string X.
128232*/
128233static void soundexFunc(
128234 sqlite3_context *context,
128235 int argc,
128236 sqlite3_value **argv
128237){
128238 char zResult[8];
128239 const u8 *zIn;
128240 int i, j;
128241 static const unsigned char iCode[] = {
128242 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128243 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128244 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128246 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
128247 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
128248 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
128249 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
128250 };
128251 assert( argc==1 );
128252 zIn = (u8*)sqlite3_value_text(argv[0]);
128253 if( zIn==0 ) zIn = (u8*)"";
128254 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
128255 if( zIn[i] ){
128256 u8 prevcode = iCode[zIn[i]&0x7f];
128257 zResult[0] = sqlite3Toupper(zIn[i]);
128258 for(j=1; j<4 && zIn[i]; i++){
128259 int code = iCode[zIn[i]&0x7f];
128260 if( code>0 ){
128261 if( code!=prevcode ){
128262 prevcode = code;
128263 zResult[j++] = code + '0';
128264 }
128265 }else{
128266 prevcode = 0;
128267 }
128268 }
128269 while( j<4 ){
128270 zResult[j++] = '0';
128271 }
128272 zResult[j] = 0;
128273 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
128274 }else{
128275 /* IMP: R-64894-50321 The string "?000" is returned if the argument
128276 ** is NULL or contains no ASCII alphabetic characters. */
128277 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
128278 }
128279}
128280#endif /* SQLITE_SOUNDEX */
128281
128282#ifndef SQLITE_OMIT_LOAD_EXTENSION
128283/*
128284** A function that loads a shared-library extension then returns NULL.
128285*/
128286static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
128287 const char *zFile = (const char *)sqlite3_value_text(pVal: argv[0]);
128288 const char *zProc;
128289 sqlite3 *db = sqlite3_context_db_handle(p: context);
128290 char *zErrMsg = 0;
128291
128292 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
128293 ** flag is set. See the sqlite3_enable_load_extension() API.
128294 */
128295 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
128296 sqlite3_result_error(pCtx: context, z: "not authorized", n: -1);
128297 return;
128298 }
128299
128300 if( argc==2 ){
128301 zProc = (const char *)sqlite3_value_text(pVal: argv[1]);
128302 }else{
128303 zProc = 0;
128304 }
128305 if( zFile && sqlite3_load_extension(db, zFile, zProc, pzErrMsg: &zErrMsg) ){
128306 sqlite3_result_error(pCtx: context, z: zErrMsg, n: -1);
128307 sqlite3_free(p: zErrMsg);
128308 }
128309}
128310#endif
128311
128312
128313/*
128314** An instance of the following structure holds the context of a
128315** sum() or avg() aggregate computation.
128316*/
128317typedef struct SumCtx SumCtx;
128318struct SumCtx {
128319 double rSum; /* Running sum as as a double */
128320 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
128321 i64 iSum; /* Running sum as a signed integer */
128322 i64 cnt; /* Number of elements summed */
128323 u8 approx; /* True if any non-integer value was input to the sum */
128324 u8 ovrfl; /* Integer overflow seen */
128325};
128326
128327/*
128328** Do one step of the Kahan-Babushka-Neumaier summation.
128329**
128330** https://en.wikipedia.org/wiki/Kahan_summation_algorithm
128331**
128332** Variables are marked "volatile" to defeat c89 x86 floating point
128333** optimizations can mess up this algorithm.
128334*/
128335static void kahanBabuskaNeumaierStep(
128336 volatile SumCtx *pSum,
128337 volatile double r
128338){
128339 volatile double s = pSum->rSum;
128340 volatile double t = s + r;
128341 if( fabs(x: s) > fabs(x: r) ){
128342 pSum->rErr += (s - t) + r;
128343 }else{
128344 pSum->rErr += (r - t) + s;
128345 }
128346 pSum->rSum = t;
128347}
128348
128349/*
128350** Add a (possibly large) integer to the running sum.
128351*/
128352static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){
128353 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
128354 i64 iBig, iSm;
128355 iSm = iVal % 16384;
128356 iBig = iVal - iSm;
128357 kahanBabuskaNeumaierStep(pSum, r: iBig);
128358 kahanBabuskaNeumaierStep(pSum, r: iSm);
128359 }else{
128360 kahanBabuskaNeumaierStep(pSum, r: (double)iVal);
128361 }
128362}
128363
128364/*
128365** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
128366*/
128367static void kahanBabuskaNeumaierInit(
128368 volatile SumCtx *p,
128369 i64 iVal
128370){
128371 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
128372 i64 iSm = iVal % 16384;
128373 p->rSum = (double)(iVal - iSm);
128374 p->rErr = (double)iSm;
128375 }else{
128376 p->rSum = (double)iVal;
128377 p->rErr = 0.0;
128378 }
128379}
128380
128381/*
128382** Routines used to compute the sum, average, and total.
128383**
128384** The SUM() function follows the (broken) SQL standard which means
128385** that it returns NULL if it sums over no inputs. TOTAL returns
128386** 0.0 in that case. In addition, TOTAL always returns a float where
128387** SUM might return an integer if it never encounters a floating point
128388** value. TOTAL never fails, but SUM might through an exception if
128389** it overflows an integer.
128390*/
128391static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
128392 SumCtx *p;
128393 int type;
128394 assert( argc==1 );
128395 UNUSED_PARAMETER(argc);
128396 p = sqlite3_aggregate_context(p: context, nByte: sizeof(*p));
128397 type = sqlite3_value_numeric_type(pVal: argv[0]);
128398 if( p && type!=SQLITE_NULL ){
128399 p->cnt++;
128400 if( p->approx==0 ){
128401 if( type!=SQLITE_INTEGER ){
128402 kahanBabuskaNeumaierInit(p, iVal: p->iSum);
128403 p->approx = 1;
128404 kahanBabuskaNeumaierStep(pSum: p, r: sqlite3_value_double(pVal: argv[0]));
128405 }else{
128406 i64 x = p->iSum;
128407 if( sqlite3AddInt64(pA: &x, iB: sqlite3_value_int64(pVal: argv[0]))==0 ){
128408 p->iSum = x;
128409 }else{
128410 p->ovrfl = 1;
128411 kahanBabuskaNeumaierInit(p, iVal: p->iSum);
128412 p->approx = 1;
128413 kahanBabuskaNeumaierStepInt64(pSum: p, iVal: sqlite3_value_int64(pVal: argv[0]));
128414 }
128415 }
128416 }else{
128417 if( type==SQLITE_INTEGER ){
128418 kahanBabuskaNeumaierStepInt64(pSum: p, iVal: sqlite3_value_int64(pVal: argv[0]));
128419 }else{
128420 p->ovrfl = 0;
128421 kahanBabuskaNeumaierStep(pSum: p, r: sqlite3_value_double(pVal: argv[0]));
128422 }
128423 }
128424 }
128425}
128426#ifndef SQLITE_OMIT_WINDOWFUNC
128427static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
128428 SumCtx *p;
128429 int type;
128430 assert( argc==1 );
128431 UNUSED_PARAMETER(argc);
128432 p = sqlite3_aggregate_context(p: context, nByte: sizeof(*p));
128433 type = sqlite3_value_numeric_type(pVal: argv[0]);
128434 /* p is always non-NULL because sumStep() will have been called first
128435 ** to initialize it */
128436 if( ALWAYS(p) && type!=SQLITE_NULL ){
128437 assert( p->cnt>0 );
128438 p->cnt--;
128439 if( !p->approx ){
128440 p->iSum -= sqlite3_value_int64(pVal: argv[0]);
128441 }else if( type==SQLITE_INTEGER ){
128442 i64 iVal = sqlite3_value_int64(pVal: argv[0]);
128443 if( iVal!=SMALLEST_INT64 ){
128444 kahanBabuskaNeumaierStepInt64(pSum: p, iVal: -iVal);
128445 }else{
128446 kahanBabuskaNeumaierStepInt64(pSum: p, LARGEST_INT64);
128447 kahanBabuskaNeumaierStepInt64(pSum: p, iVal: 1);
128448 }
128449 }else{
128450 kahanBabuskaNeumaierStep(pSum: p, r: -sqlite3_value_double(pVal: argv[0]));
128451 }
128452 }
128453}
128454#else
128455# define sumInverse 0
128456#endif /* SQLITE_OMIT_WINDOWFUNC */
128457static void sumFinalize(sqlite3_context *context){
128458 SumCtx *p;
128459 p = sqlite3_aggregate_context(p: context, nByte: 0);
128460 if( p && p->cnt>0 ){
128461 if( p->approx ){
128462 if( p->ovrfl ){
128463 sqlite3_result_error(pCtx: context,z: "integer overflow",n: -1);
128464 }else if( !sqlite3IsNaN(x: p->rErr) ){
128465 sqlite3_result_double(pCtx: context, rVal: p->rSum+p->rErr);
128466 }else{
128467 sqlite3_result_double(pCtx: context, rVal: p->rSum);
128468 }
128469 }else{
128470 sqlite3_result_int64(pCtx: context, iVal: p->iSum);
128471 }
128472 }
128473}
128474static void avgFinalize(sqlite3_context *context){
128475 SumCtx *p;
128476 p = sqlite3_aggregate_context(p: context, nByte: 0);
128477 if( p && p->cnt>0 ){
128478 double r;
128479 if( p->approx ){
128480 r = p->rSum;
128481 if( !sqlite3IsNaN(x: p->rErr) ) r += p->rErr;
128482 }else{
128483 r = (double)(p->iSum);
128484 }
128485 sqlite3_result_double(pCtx: context, rVal: r/(double)p->cnt);
128486 }
128487}
128488static void totalFinalize(sqlite3_context *context){
128489 SumCtx *p;
128490 double r = 0.0;
128491 p = sqlite3_aggregate_context(p: context, nByte: 0);
128492 if( p ){
128493 if( p->approx ){
128494 r = p->rSum;
128495 if( !sqlite3IsNaN(x: p->rErr) ) r += p->rErr;
128496 }else{
128497 r = (double)(p->iSum);
128498 }
128499 }
128500 sqlite3_result_double(pCtx: context, rVal: r);
128501}
128502
128503/*
128504** The following structure keeps track of state information for the
128505** count() aggregate function.
128506*/
128507typedef struct CountCtx CountCtx;
128508struct CountCtx {
128509 i64 n;
128510#ifdef SQLITE_DEBUG
128511 int bInverse; /* True if xInverse() ever called */
128512#endif
128513};
128514
128515/*
128516** Routines to implement the count() aggregate function.
128517*/
128518static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
128519 CountCtx *p;
128520 p = sqlite3_aggregate_context(p: context, nByte: sizeof(*p));
128521 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(pVal: argv[0])) && p ){
128522 p->n++;
128523 }
128524
128525#ifndef SQLITE_OMIT_DEPRECATED
128526 /* The sqlite3_aggregate_count() function is deprecated. But just to make
128527 ** sure it still operates correctly, verify that its count agrees with our
128528 ** internal count when using count(*) and when the total count can be
128529 ** expressed as a 32-bit integer. */
128530 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
128531 || p->n==sqlite3_aggregate_count(context) );
128532#endif
128533}
128534static void countFinalize(sqlite3_context *context){
128535 CountCtx *p;
128536 p = sqlite3_aggregate_context(p: context, nByte: 0);
128537 sqlite3_result_int64(pCtx: context, iVal: p ? p->n : 0);
128538}
128539#ifndef SQLITE_OMIT_WINDOWFUNC
128540static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
128541 CountCtx *p;
128542 p = sqlite3_aggregate_context(p: ctx, nByte: sizeof(*p));
128543 /* p is always non-NULL since countStep() will have been called first */
128544 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(pVal: argv[0])) && ALWAYS(p) ){
128545 p->n--;
128546#ifdef SQLITE_DEBUG
128547 p->bInverse = 1;
128548#endif
128549 }
128550}
128551#else
128552# define countInverse 0
128553#endif /* SQLITE_OMIT_WINDOWFUNC */
128554
128555/*
128556** Routines to implement min() and max() aggregate functions.
128557*/
128558static void minmaxStep(
128559 sqlite3_context *context,
128560 int NotUsed,
128561 sqlite3_value **argv
128562){
128563 Mem *pArg = (Mem *)argv[0];
128564 Mem *pBest;
128565 UNUSED_PARAMETER(NotUsed);
128566
128567 pBest = (Mem *)sqlite3_aggregate_context(p: context, nByte: sizeof(*pBest));
128568 if( !pBest ) return;
128569
128570 if( sqlite3_value_type(pVal: pArg)==SQLITE_NULL ){
128571 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
128572 }else if( pBest->flags ){
128573 int max;
128574 int cmp;
128575 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
128576 /* This step function is used for both the min() and max() aggregates,
128577 ** the only difference between the two being that the sense of the
128578 ** comparison is inverted. For the max() aggregate, the
128579 ** sqlite3_user_data() function returns (void *)-1. For min() it
128580 ** returns (void *)db, where db is the sqlite3* database pointer.
128581 ** Therefore the next statement sets variable 'max' to 1 for the max()
128582 ** aggregate, or 0 for min().
128583 */
128584 max = sqlite3_user_data(p: context)!=0;
128585 cmp = sqlite3MemCompare(pMem1: pBest, pMem2: pArg, pColl);
128586 if( (max && cmp<0) || (!max && cmp>0) ){
128587 sqlite3VdbeMemCopy(pTo: pBest, pFrom: pArg);
128588 }else{
128589 sqlite3SkipAccumulatorLoad(context);
128590 }
128591 }else{
128592 pBest->db = sqlite3_context_db_handle(p: context);
128593 sqlite3VdbeMemCopy(pTo: pBest, pFrom: pArg);
128594 }
128595}
128596static void minMaxValueFinalize(sqlite3_context *context, int bValue){
128597 sqlite3_value *pRes;
128598 pRes = (sqlite3_value *)sqlite3_aggregate_context(p: context, nByte: 0);
128599 if( pRes ){
128600 if( pRes->flags ){
128601 sqlite3_result_value(pCtx: context, pValue: pRes);
128602 }
128603 if( bValue==0 ) sqlite3VdbeMemRelease(p: pRes);
128604 }
128605}
128606#ifndef SQLITE_OMIT_WINDOWFUNC
128607static void minMaxValue(sqlite3_context *context){
128608 minMaxValueFinalize(context, bValue: 1);
128609}
128610#else
128611# define minMaxValue 0
128612#endif /* SQLITE_OMIT_WINDOWFUNC */
128613static void minMaxFinalize(sqlite3_context *context){
128614 minMaxValueFinalize(context, bValue: 0);
128615}
128616
128617/*
128618** group_concat(EXPR, ?SEPARATOR?)
128619**
128620** The SEPARATOR goes before the EXPR string. This is tragic. The
128621** groupConcatInverse() implementation would have been easier if the
128622** SEPARATOR were appended after EXPR. And the order is undocumented,
128623** so we could change it, in theory. But the old behavior has been
128624** around for so long that we dare not, for fear of breaking something.
128625*/
128626typedef struct {
128627 StrAccum str; /* The accumulated concatenation */
128628#ifndef SQLITE_OMIT_WINDOWFUNC
128629 int nAccum; /* Number of strings presently concatenated */
128630 int nFirstSepLength; /* Used to detect separator length change */
128631 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
128632 ** stored as actually incorporated into presently accumulated result.
128633 ** (Hence, its slots in use number nAccum-1 between method calls.)
128634 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
128635 */
128636 int *pnSepLengths;
128637#endif
128638} GroupConcatCtx;
128639
128640static void groupConcatStep(
128641 sqlite3_context *context,
128642 int argc,
128643 sqlite3_value **argv
128644){
128645 const char *zVal;
128646 GroupConcatCtx *pGCC;
128647 const char *zSep;
128648 int nVal, nSep;
128649 assert( argc==1 || argc==2 );
128650 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
128651 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: sizeof(*pGCC));
128652 if( pGCC ){
128653 sqlite3 *db = sqlite3_context_db_handle(p: context);
128654 int firstTerm = pGCC->str.mxAlloc==0;
128655 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
128656 if( argc==1 ){
128657 if( !firstTerm ){
128658 sqlite3_str_appendchar(p: &pGCC->str, N: 1, c: ',');
128659 }
128660#ifndef SQLITE_OMIT_WINDOWFUNC
128661 else{
128662 pGCC->nFirstSepLength = 1;
128663 }
128664#endif
128665 }else if( !firstTerm ){
128666 zSep = (char*)sqlite3_value_text(pVal: argv[1]);
128667 nSep = sqlite3_value_bytes(pVal: argv[1]);
128668 if( zSep ){
128669 sqlite3_str_append(p: &pGCC->str, z: zSep, N: nSep);
128670 }
128671#ifndef SQLITE_OMIT_WINDOWFUNC
128672 else{
128673 nSep = 0;
128674 }
128675 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
128676 int *pnsl = pGCC->pnSepLengths;
128677 if( pnsl == 0 ){
128678 /* First separator length variation seen, start tracking them. */
128679 pnsl = (int*)sqlite3_malloc64(n: (pGCC->nAccum+1) * sizeof(int));
128680 if( pnsl!=0 ){
128681 int i = 0, nA = pGCC->nAccum-1;
128682 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
128683 }
128684 }else{
128685 pnsl = (int*)sqlite3_realloc64(pOld: pnsl, n: pGCC->nAccum * sizeof(int));
128686 }
128687 if( pnsl!=0 ){
128688 if( ALWAYS(pGCC->nAccum>0) ){
128689 pnsl[pGCC->nAccum-1] = nSep;
128690 }
128691 pGCC->pnSepLengths = pnsl;
128692 }else{
128693 sqlite3StrAccumSetError(p: &pGCC->str, SQLITE_NOMEM);
128694 }
128695 }
128696#endif
128697 }
128698#ifndef SQLITE_OMIT_WINDOWFUNC
128699 else{
128700 pGCC->nFirstSepLength = sqlite3_value_bytes(pVal: argv[1]);
128701 }
128702 pGCC->nAccum += 1;
128703#endif
128704 zVal = (char*)sqlite3_value_text(pVal: argv[0]);
128705 nVal = sqlite3_value_bytes(pVal: argv[0]);
128706 if( zVal ) sqlite3_str_append(p: &pGCC->str, z: zVal, N: nVal);
128707 }
128708}
128709
128710#ifndef SQLITE_OMIT_WINDOWFUNC
128711static void groupConcatInverse(
128712 sqlite3_context *context,
128713 int argc,
128714 sqlite3_value **argv
128715){
128716 GroupConcatCtx *pGCC;
128717 assert( argc==1 || argc==2 );
128718 (void)argc; /* Suppress unused parameter warning */
128719 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
128720 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: sizeof(*pGCC));
128721 /* pGCC is always non-NULL since groupConcatStep() will have always
128722 ** run first to initialize it */
128723 if( ALWAYS(pGCC) ){
128724 int nVS;
128725 /* Must call sqlite3_value_text() to convert the argument into text prior
128726 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
128727 (void)sqlite3_value_text(pVal: argv[0]);
128728 nVS = sqlite3_value_bytes(pVal: argv[0]);
128729 pGCC->nAccum -= 1;
128730 if( pGCC->pnSepLengths!=0 ){
128731 assert(pGCC->nAccum >= 0);
128732 if( pGCC->nAccum>0 ){
128733 nVS += *pGCC->pnSepLengths;
128734 memmove(dest: pGCC->pnSepLengths, src: pGCC->pnSepLengths+1,
128735 n: (pGCC->nAccum-1)*sizeof(int));
128736 }
128737 }else{
128738 /* If removing single accumulated string, harmlessly over-do. */
128739 nVS += pGCC->nFirstSepLength;
128740 }
128741 if( nVS>=(int)pGCC->str.nChar ){
128742 pGCC->str.nChar = 0;
128743 }else{
128744 pGCC->str.nChar -= nVS;
128745 memmove(dest: pGCC->str.zText, src: &pGCC->str.zText[nVS], n: pGCC->str.nChar);
128746 }
128747 if( pGCC->str.nChar==0 ){
128748 pGCC->str.mxAlloc = 0;
128749 sqlite3_free(p: pGCC->pnSepLengths);
128750 pGCC->pnSepLengths = 0;
128751 }
128752 }
128753}
128754#else
128755# define groupConcatInverse 0
128756#endif /* SQLITE_OMIT_WINDOWFUNC */
128757static void groupConcatFinalize(sqlite3_context *context){
128758 GroupConcatCtx *pGCC
128759 = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: 0);
128760 if( pGCC ){
128761 sqlite3ResultStrAccum(pCtx: context, p: &pGCC->str);
128762#ifndef SQLITE_OMIT_WINDOWFUNC
128763 sqlite3_free(p: pGCC->pnSepLengths);
128764#endif
128765 }
128766}
128767#ifndef SQLITE_OMIT_WINDOWFUNC
128768static void groupConcatValue(sqlite3_context *context){
128769 GroupConcatCtx *pGCC
128770 = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: 0);
128771 if( pGCC ){
128772 StrAccum *pAccum = &pGCC->str;
128773 if( pAccum->accError==SQLITE_TOOBIG ){
128774 sqlite3_result_error_toobig(pCtx: context);
128775 }else if( pAccum->accError==SQLITE_NOMEM ){
128776 sqlite3_result_error_nomem(pCtx: context);
128777 }else{
128778 const char *zText = sqlite3_str_value(p: pAccum);
128779 sqlite3_result_text(pCtx: context, z: zText, n: pAccum->nChar, SQLITE_TRANSIENT);
128780 }
128781 }
128782}
128783#else
128784# define groupConcatValue 0
128785#endif /* SQLITE_OMIT_WINDOWFUNC */
128786
128787/*
128788** This routine does per-connection function registration. Most
128789** of the built-in functions above are part of the global function set.
128790** This routine only deals with those that are not global.
128791*/
128792SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
128793 int rc = sqlite3_overload_function(db, zFuncName: "MATCH", nArg: 2);
128794 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
128795 if( rc==SQLITE_NOMEM ){
128796 sqlite3OomFault(db);
128797 }
128798}
128799
128800/*
128801** Re-register the built-in LIKE functions. The caseSensitive
128802** parameter determines whether or not the LIKE operator is case
128803** sensitive.
128804*/
128805SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
128806 FuncDef *pDef;
128807 struct compareInfo *pInfo;
128808 int flags;
128809 int nArg;
128810 if( caseSensitive ){
128811 pInfo = (struct compareInfo*)&likeInfoAlt;
128812 flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
128813 }else{
128814 pInfo = (struct compareInfo*)&likeInfoNorm;
128815 flags = SQLITE_FUNC_LIKE;
128816 }
128817 for(nArg=2; nArg<=3; nArg++){
128818 sqlite3CreateFunc(db, "like", nArg, SQLITE_UTF8, pInfo, likeFunc,
128819 0, 0, 0, 0, pDestructor: 0);
128820 pDef = sqlite3FindFunction(db, zName: "like", nArg, SQLITE_UTF8, createFlag: 0);
128821 pDef->funcFlags |= flags;
128822 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE;
128823 }
128824}
128825
128826/*
128827** pExpr points to an expression which implements a function. If
128828** it is appropriate to apply the LIKE optimization to that function
128829** then set aWc[0] through aWc[2] to the wildcard characters and the
128830** escape character and then return TRUE. If the function is not a
128831** LIKE-style function then return FALSE.
128832**
128833** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
128834** operator if c is a string literal that is exactly one byte in length.
128835** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
128836** no ESCAPE clause.
128837**
128838** *pIsNocase is set to true if uppercase and lowercase are equivalent for
128839** the function (default for LIKE). If the function makes the distinction
128840** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
128841** false.
128842*/
128843SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
128844 FuncDef *pDef;
128845 int nExpr;
128846 assert( pExpr!=0 );
128847 assert( pExpr->op==TK_FUNCTION );
128848 assert( ExprUseXList(pExpr) );
128849 if( !pExpr->x.pList ){
128850 return 0;
128851 }
128852 nExpr = pExpr->x.pList->nExpr;
128853 assert( !ExprHasProperty(pExpr, EP_IntValue) );
128854 pDef = sqlite3FindFunction(db, zName: pExpr->u.zToken, nArg: nExpr, SQLITE_UTF8, createFlag: 0);
128855#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
128856 if( pDef==0 ) return 0;
128857#endif
128858 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
128859 return 0;
128860 }
128861
128862 /* The memcpy() statement assumes that the wildcard characters are
128863 ** the first three statements in the compareInfo structure. The
128864 ** asserts() that follow verify that assumption
128865 */
128866 memcpy(dest: aWc, src: pDef->pUserData, n: 3);
128867 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
128868 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
128869 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
128870
128871 if( nExpr<3 ){
128872 aWc[3] = 0;
128873 }else{
128874 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
128875 char *zEscape;
128876 if( pEscape->op!=TK_STRING ) return 0;
128877 assert( !ExprHasProperty(pEscape, EP_IntValue) );
128878 zEscape = pEscape->u.zToken;
128879 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
128880 if( zEscape[0]==aWc[0] ) return 0;
128881 if( zEscape[0]==aWc[1] ) return 0;
128882 aWc[3] = zEscape[0];
128883 }
128884
128885 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
128886 return 1;
128887}
128888
128889/* Mathematical Constants */
128890#ifndef M_PI
128891# define M_PI 3.141592653589793238462643383279502884
128892#endif
128893#ifndef M_LN10
128894# define M_LN10 2.302585092994045684017991454684364208
128895#endif
128896#ifndef M_LN2
128897# define M_LN2 0.693147180559945309417232121458176568
128898#endif
128899
128900
128901/* Extra math functions that require linking with -lm
128902*/
128903#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
128904/*
128905** Implementation SQL functions:
128906**
128907** ceil(X)
128908** ceiling(X)
128909** floor(X)
128910**
128911** The sqlite3_user_data() pointer is a pointer to the libm implementation
128912** of the underlying C function.
128913*/
128914static void ceilingFunc(
128915 sqlite3_context *context,
128916 int argc,
128917 sqlite3_value **argv
128918){
128919 assert( argc==1 );
128920 switch( sqlite3_value_numeric_type(pVal: argv[0]) ){
128921 case SQLITE_INTEGER: {
128922 sqlite3_result_int64(pCtx: context, iVal: sqlite3_value_int64(pVal: argv[0]));
128923 break;
128924 }
128925 case SQLITE_FLOAT: {
128926 double (*x)(double) = (double(*)(double))sqlite3_user_data(p: context);
128927 sqlite3_result_double(pCtx: context, rVal: x(sqlite3_value_double(pVal: argv[0])));
128928 break;
128929 }
128930 default: {
128931 break;
128932 }
128933 }
128934}
128935
128936/*
128937** On some systems, ceil() and floor() are intrinsic function. You are
128938** unable to take a pointer to these functions. Hence, we here wrap them
128939** in our own actual functions.
128940*/
128941static double xCeil(double x){ return ceil(x: x); }
128942static double xFloor(double x){ return floor(x: x); }
128943
128944/*
128945** Some systems do not have log2() and log10() in their standard math
128946** libraries.
128947*/
128948#if defined(HAVE_LOG10) && HAVE_LOG10==0
128949# define log10(X) (0.4342944819032517867*log(X))
128950#endif
128951#if defined(HAVE_LOG2) && HAVE_LOG2==0
128952# define log2(X) (1.442695040888963456*log(X))
128953#endif
128954
128955
128956/*
128957** Implementation of SQL functions:
128958**
128959** ln(X) - natural logarithm
128960** log(X) - log X base 10
128961** log10(X) - log X base 10
128962** log(B,X) - log X base B
128963*/
128964static void logFunc(
128965 sqlite3_context *context,
128966 int argc,
128967 sqlite3_value **argv
128968){
128969 double x, b, ans;
128970 assert( argc==1 || argc==2 );
128971 switch( sqlite3_value_numeric_type(pVal: argv[0]) ){
128972 case SQLITE_INTEGER:
128973 case SQLITE_FLOAT:
128974 x = sqlite3_value_double(pVal: argv[0]);
128975 if( x<=0.0 ) return;
128976 break;
128977 default:
128978 return;
128979 }
128980 if( argc==2 ){
128981 switch( sqlite3_value_numeric_type(pVal: argv[0]) ){
128982 case SQLITE_INTEGER:
128983 case SQLITE_FLOAT:
128984 b = log(x: x);
128985 if( b<=0.0 ) return;
128986 x = sqlite3_value_double(pVal: argv[1]);
128987 if( x<=0.0 ) return;
128988 break;
128989 default:
128990 return;
128991 }
128992 ans = log(x: x)/b;
128993 }else{
128994 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
128995 case 1:
128996 ans = log10(x: x);
128997 break;
128998 case 2:
128999 ans = log2(x: x);
129000 break;
129001 default:
129002 ans = log(x: x);
129003 break;
129004 }
129005 }
129006 sqlite3_result_double(pCtx: context, rVal: ans);
129007}
129008
129009/*
129010** Functions to converts degrees to radians and radians to degrees.
129011*/
129012static double degToRad(double x){ return x*(M_PI/180.0); }
129013static double radToDeg(double x){ return x*(180.0/M_PI); }
129014
129015/*
129016** Implementation of 1-argument SQL math functions:
129017**
129018** exp(X) - Compute e to the X-th power
129019*/
129020static void math1Func(
129021 sqlite3_context *context,
129022 int argc,
129023 sqlite3_value **argv
129024){
129025 int type0;
129026 double v0, ans;
129027 double (*x)(double);
129028 assert( argc==1 );
129029 type0 = sqlite3_value_numeric_type(pVal: argv[0]);
129030 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
129031 v0 = sqlite3_value_double(pVal: argv[0]);
129032 x = (double(*)(double))sqlite3_user_data(p: context);
129033 ans = x(v0);
129034 sqlite3_result_double(pCtx: context, rVal: ans);
129035}
129036
129037/*
129038** Implementation of 2-argument SQL math functions:
129039**
129040** power(X,Y) - Compute X to the Y-th power
129041*/
129042static void math2Func(
129043 sqlite3_context *context,
129044 int argc,
129045 sqlite3_value **argv
129046){
129047 int type0, type1;
129048 double v0, v1, ans;
129049 double (*x)(double,double);
129050 assert( argc==2 );
129051 type0 = sqlite3_value_numeric_type(pVal: argv[0]);
129052 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
129053 type1 = sqlite3_value_numeric_type(pVal: argv[1]);
129054 if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return;
129055 v0 = sqlite3_value_double(pVal: argv[0]);
129056 v1 = sqlite3_value_double(pVal: argv[1]);
129057 x = (double(*)(double,double))sqlite3_user_data(p: context);
129058 ans = x(v0, v1);
129059 sqlite3_result_double(pCtx: context, rVal: ans);
129060}
129061
129062/*
129063** Implementation of 0-argument pi() function.
129064*/
129065static void piFunc(
129066 sqlite3_context *context,
129067 int argc,
129068 sqlite3_value **argv
129069){
129070 assert( argc==0 );
129071 (void)argv;
129072 sqlite3_result_double(pCtx: context, M_PI);
129073}
129074
129075#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
129076
129077/*
129078** Implementation of sign(X) function.
129079*/
129080static void signFunc(
129081 sqlite3_context *context,
129082 int argc,
129083 sqlite3_value **argv
129084){
129085 int type0;
129086 double x;
129087 UNUSED_PARAMETER(argc);
129088 assert( argc==1 );
129089 type0 = sqlite3_value_numeric_type(pVal: argv[0]);
129090 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
129091 x = sqlite3_value_double(pVal: argv[0]);
129092 sqlite3_result_int(pCtx: context, iVal: x<0.0 ? -1 : x>0.0 ? +1 : 0);
129093}
129094
129095#ifdef SQLITE_DEBUG
129096/*
129097** Implementation of fpdecode(x,y,z) function.
129098**
129099** x is a real number that is to be decoded. y is the precision.
129100** z is the maximum real precision.
129101*/
129102static void fpdecodeFunc(
129103 sqlite3_context *context,
129104 int argc,
129105 sqlite3_value **argv
129106){
129107 FpDecode s;
129108 double x;
129109 int y, z;
129110 char zBuf[100];
129111 UNUSED_PARAMETER(argc);
129112 assert( argc==3 );
129113 x = sqlite3_value_double(argv[0]);
129114 y = sqlite3_value_int(argv[1]);
129115 z = sqlite3_value_int(argv[2]);
129116 sqlite3FpDecode(&s, x, y, z);
129117 if( s.isSpecial==2 ){
129118 sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
129119 }else{
129120 sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
129121 }
129122 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
129123}
129124#endif /* SQLITE_DEBUG */
129125
129126/*
129127** All of the FuncDef structures in the aBuiltinFunc[] array above
129128** to the global function hash table. This occurs at start-time (as
129129** a consequence of calling sqlite3_initialize()).
129130**
129131** After this routine runs
129132*/
129133SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
129134 /*
129135 ** The following array holds FuncDef structures for all of the functions
129136 ** defined in this file.
129137 **
129138 ** The array cannot be constant since changes are made to the
129139 ** FuncDef.pHash elements at start-time. The elements of this array
129140 ** are read-only after initialization is complete.
129141 **
129142 ** For peak efficiency, put the most frequently used function last.
129143 */
129144 static FuncDef aBuiltinFunc[] = {
129145/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
129146#if !defined(SQLITE_UNTESTABLE)
129147 TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0),
129148 TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0),
129149 TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0),
129150 TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0),
129151#endif /* !defined(SQLITE_UNTESTABLE) */
129152/***** Regular functions *****/
129153#ifdef SQLITE_SOUNDEX
129154 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
129155#endif
129156#ifndef SQLITE_OMIT_LOAD_EXTENSION
129157 SFUNCTION(load_extension, 1, 0, 0, loadExt ),
129158 SFUNCTION(load_extension, 2, 0, 0, loadExt ),
129159#endif
129160#if SQLITE_USER_AUTHENTICATION
129161 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
129162#endif
129163#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
129164 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
129165 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
129166#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
129167 INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
129168 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
129169 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
129170#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
129171 INLINE_FUNC(sqlite_offset, 1, INLINEFUNC_sqlite_offset, 0 ),
129172#endif
129173 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
129174 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
129175 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
129176 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
129177 FUNCTION(trim, 1, 3, 0, trimFunc ),
129178 FUNCTION(trim, 2, 3, 0, trimFunc ),
129179 FUNCTION(min, -1, 0, 1, minmaxFunc ),
129180 FUNCTION(min, 0, 0, 1, 0 ),
129181 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
129182 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ),
129183 FUNCTION(max, -1, 1, 1, minmaxFunc ),
129184 FUNCTION(max, 0, 1, 1, 0 ),
129185 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
129186 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ),
129187 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
129188 FUNCTION2(subtype, 1, 0, 0, subtypeFunc, SQLITE_FUNC_TYPEOF),
129189 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
129190 FUNCTION2(octet_length, 1, 0, 0, bytelengthFunc,SQLITE_FUNC_BYTELEN),
129191 FUNCTION(instr, 2, 0, 0, instrFunc ),
129192 FUNCTION(printf, -1, 0, 0, printfFunc ),
129193 FUNCTION(format, -1, 0, 0, printfFunc ),
129194 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
129195 FUNCTION(char, -1, 0, 0, charFunc ),
129196 FUNCTION(abs, 1, 0, 0, absFunc ),
129197#ifdef SQLITE_DEBUG
129198 FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ),
129199#endif
129200#ifndef SQLITE_OMIT_FLOATING_POINT
129201 FUNCTION(round, 1, 0, 0, roundFunc ),
129202 FUNCTION(round, 2, 0, 0, roundFunc ),
129203#endif
129204 FUNCTION(upper, 1, 0, 0, upperFunc ),
129205 FUNCTION(lower, 1, 0, 0, lowerFunc ),
129206 FUNCTION(hex, 1, 0, 0, hexFunc ),
129207 FUNCTION(unhex, 1, 0, 0, unhexFunc ),
129208 FUNCTION(unhex, 2, 0, 0, unhexFunc ),
129209 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ),
129210 VFUNCTION(random, 0, 0, 0, randomFunc ),
129211 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
129212 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
129213 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
129214 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
129215 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
129216 FUNCTION(quote, 1, 0, 0, quoteFunc ),
129217 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
129218 VFUNCTION(changes, 0, 0, 0, changes ),
129219 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
129220 FUNCTION(replace, 3, 0, 0, replaceFunc ),
129221 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
129222 FUNCTION(substr, 2, 0, 0, substrFunc ),
129223 FUNCTION(substr, 3, 0, 0, substrFunc ),
129224 FUNCTION(substring, 2, 0, 0, substrFunc ),
129225 FUNCTION(substring, 3, 0, 0, substrFunc ),
129226 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
129227 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
129228 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
129229 WAGGREGATE(count, 0,0,0, countStep,
129230 countFinalize, countFinalize, countInverse,
129231 SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ),
129232 WAGGREGATE(count, 1,0,0, countStep,
129233 countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ),
129234 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
129235 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
129236 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
129237 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
129238
129239 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
129240#ifdef SQLITE_CASE_SENSITIVE_LIKE
129241 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
129242 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
129243#else
129244 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
129245 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
129246#endif
129247#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
129248 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
129249#endif
129250 FUNCTION(coalesce, 1, 0, 0, 0 ),
129251 FUNCTION(coalesce, 0, 0, 0, 0 ),
129252#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
129253 MFUNCTION(ceil, 1, xCeil, ceilingFunc ),
129254 MFUNCTION(ceiling, 1, xCeil, ceilingFunc ),
129255 MFUNCTION(floor, 1, xFloor, ceilingFunc ),
129256#if SQLITE_HAVE_C99_MATH_FUNCS
129257 MFUNCTION(trunc, 1, trunc, ceilingFunc ),
129258#endif
129259 FUNCTION(ln, 1, 0, 0, logFunc ),
129260 FUNCTION(log, 1, 1, 0, logFunc ),
129261 FUNCTION(log10, 1, 1, 0, logFunc ),
129262 FUNCTION(log2, 1, 2, 0, logFunc ),
129263 FUNCTION(log, 2, 0, 0, logFunc ),
129264 MFUNCTION(exp, 1, exp, math1Func ),
129265 MFUNCTION(pow, 2, pow, math2Func ),
129266 MFUNCTION(power, 2, pow, math2Func ),
129267 MFUNCTION(mod, 2, fmod, math2Func ),
129268 MFUNCTION(acos, 1, acos, math1Func ),
129269 MFUNCTION(asin, 1, asin, math1Func ),
129270 MFUNCTION(atan, 1, atan, math1Func ),
129271 MFUNCTION(atan2, 2, atan2, math2Func ),
129272 MFUNCTION(cos, 1, cos, math1Func ),
129273 MFUNCTION(sin, 1, sin, math1Func ),
129274 MFUNCTION(tan, 1, tan, math1Func ),
129275 MFUNCTION(cosh, 1, cosh, math1Func ),
129276 MFUNCTION(sinh, 1, sinh, math1Func ),
129277 MFUNCTION(tanh, 1, tanh, math1Func ),
129278#if SQLITE_HAVE_C99_MATH_FUNCS
129279 MFUNCTION(acosh, 1, acosh, math1Func ),
129280 MFUNCTION(asinh, 1, asinh, math1Func ),
129281 MFUNCTION(atanh, 1, atanh, math1Func ),
129282#endif
129283 MFUNCTION(sqrt, 1, sqrt, math1Func ),
129284 MFUNCTION(radians, 1, degToRad, math1Func ),
129285 MFUNCTION(degrees, 1, radToDeg, math1Func ),
129286 FUNCTION(pi, 0, 0, 0, piFunc ),
129287#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
129288 FUNCTION(sign, 1, 0, 0, signFunc ),
129289 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
129290 INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ),
129291 };
129292#ifndef SQLITE_OMIT_ALTERTABLE
129293 sqlite3AlterFunctions();
129294#endif
129295 sqlite3WindowFunctions();
129296 sqlite3RegisterDateTimeFunctions();
129297 sqlite3RegisterJsonFunctions();
129298 sqlite3InsertBuiltinFuncs(aDef: aBuiltinFunc, ArraySize(aBuiltinFunc));
129299
129300#if 0 /* Enable to print out how the built-in functions are hashed */
129301 {
129302 int i;
129303 FuncDef *p;
129304 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
129305 printf("FUNC-HASH %02d:", i);
129306 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
129307 int n = sqlite3Strlen30(p->zName);
129308 int h = p->zName[0] + n;
129309 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
129310 printf(" %s(%d)", p->zName, h);
129311 }
129312 printf("\n");
129313 }
129314 }
129315#endif
129316}
129317
129318/************** End of func.c ************************************************/
129319/************** Begin file fkey.c ********************************************/
129320/*
129321**
129322** The author disclaims copyright to this source code. In place of
129323** a legal notice, here is a blessing:
129324**
129325** May you do good and not evil.
129326** May you find forgiveness for yourself and forgive others.
129327** May you share freely, never taking more than you give.
129328**
129329*************************************************************************
129330** This file contains code used by the compiler to add foreign key
129331** support to compiled SQL statements.
129332*/
129333/* #include "sqliteInt.h" */
129334
129335#ifndef SQLITE_OMIT_FOREIGN_KEY
129336#ifndef SQLITE_OMIT_TRIGGER
129337
129338/*
129339** Deferred and Immediate FKs
129340** --------------------------
129341**
129342** Foreign keys in SQLite come in two flavours: deferred and immediate.
129343** If an immediate foreign key constraint is violated,
129344** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
129345** statement transaction rolled back. If a
129346** deferred foreign key constraint is violated, no action is taken
129347** immediately. However if the application attempts to commit the
129348** transaction before fixing the constraint violation, the attempt fails.
129349**
129350** Deferred constraints are implemented using a simple counter associated
129351** with the database handle. The counter is set to zero each time a
129352** database transaction is opened. Each time a statement is executed
129353** that causes a foreign key violation, the counter is incremented. Each
129354** time a statement is executed that removes an existing violation from
129355** the database, the counter is decremented. When the transaction is
129356** committed, the commit fails if the current value of the counter is
129357** greater than zero. This scheme has two big drawbacks:
129358**
129359** * When a commit fails due to a deferred foreign key constraint,
129360** there is no way to tell which foreign constraint is not satisfied,
129361** or which row it is not satisfied for.
129362**
129363** * If the database contains foreign key violations when the
129364** transaction is opened, this may cause the mechanism to malfunction.
129365**
129366** Despite these problems, this approach is adopted as it seems simpler
129367** than the alternatives.
129368**
129369** INSERT operations:
129370**
129371** I.1) For each FK for which the table is the child table, search
129372** the parent table for a match. If none is found increment the
129373** constraint counter.
129374**
129375** I.2) For each FK for which the table is the parent table,
129376** search the child table for rows that correspond to the new
129377** row in the parent table. Decrement the counter for each row
129378** found (as the constraint is now satisfied).
129379**
129380** DELETE operations:
129381**
129382** D.1) For each FK for which the table is the child table,
129383** search the parent table for a row that corresponds to the
129384** deleted row in the child table. If such a row is not found,
129385** decrement the counter.
129386**
129387** D.2) For each FK for which the table is the parent table, search
129388** the child table for rows that correspond to the deleted row
129389** in the parent table. For each found increment the counter.
129390**
129391** UPDATE operations:
129392**
129393** An UPDATE command requires that all 4 steps above are taken, but only
129394** for FK constraints for which the affected columns are actually
129395** modified (values must be compared at runtime).
129396**
129397** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
129398** This simplifies the implementation a bit.
129399**
129400** For the purposes of immediate FK constraints, the OR REPLACE conflict
129401** resolution is considered to delete rows before the new row is inserted.
129402** If a delete caused by OR REPLACE violates an FK constraint, an exception
129403** is thrown, even if the FK constraint would be satisfied after the new
129404** row is inserted.
129405**
129406** Immediate constraints are usually handled similarly. The only difference
129407** is that the counter used is stored as part of each individual statement
129408** object (struct Vdbe). If, after the statement has run, its immediate
129409** constraint counter is greater than zero,
129410** it returns SQLITE_CONSTRAINT_FOREIGNKEY
129411** and the statement transaction is rolled back. An exception is an INSERT
129412** statement that inserts a single row only (no triggers). In this case,
129413** instead of using a counter, an exception is thrown immediately if the
129414** INSERT violates a foreign key constraint. This is necessary as such
129415** an INSERT does not open a statement transaction.
129416**
129417** TODO: How should dropping a table be handled? How should renaming a
129418** table be handled?
129419**
129420**
129421** Query API Notes
129422** ---------------
129423**
129424** Before coding an UPDATE or DELETE row operation, the code-generator
129425** for those two operations needs to know whether or not the operation
129426** requires any FK processing and, if so, which columns of the original
129427** row are required by the FK processing VDBE code (i.e. if FKs were
129428** implemented using triggers, which of the old.* columns would be
129429** accessed). No information is required by the code-generator before
129430** coding an INSERT operation. The functions used by the UPDATE/DELETE
129431** generation code to query for this information are:
129432**
129433** sqlite3FkRequired() - Test to see if FK processing is required.
129434** sqlite3FkOldmask() - Query for the set of required old.* columns.
129435**
129436**
129437** Externally accessible module functions
129438** --------------------------------------
129439**
129440** sqlite3FkCheck() - Check for foreign key violations.
129441** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
129442** sqlite3FkDelete() - Delete an FKey structure.
129443*/
129444
129445/*
129446** VDBE Calling Convention
129447** -----------------------
129448**
129449** Example:
129450**
129451** For the following INSERT statement:
129452**
129453** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
129454** INSERT INTO t1 VALUES(1, 2, 3.1);
129455**
129456** Register (x): 2 (type integer)
129457** Register (x+1): 1 (type integer)
129458** Register (x+2): NULL (type NULL)
129459** Register (x+3): 3.1 (type real)
129460*/
129461
129462/*
129463** A foreign key constraint requires that the key columns in the parent
129464** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
129465** Given that pParent is the parent table for foreign key constraint pFKey,
129466** search the schema for a unique index on the parent key columns.
129467**
129468** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
129469** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
129470** is set to point to the unique index.
129471**
129472** If the parent key consists of a single column (the foreign key constraint
129473** is not a composite foreign key), output variable *paiCol is set to NULL.
129474** Otherwise, it is set to point to an allocated array of size N, where
129475** N is the number of columns in the parent key. The first element of the
129476** array is the index of the child table column that is mapped by the FK
129477** constraint to the parent table column stored in the left-most column
129478** of index *ppIdx. The second element of the array is the index of the
129479** child table column that corresponds to the second left-most column of
129480** *ppIdx, and so on.
129481**
129482** If the required index cannot be found, either because:
129483**
129484** 1) The named parent key columns do not exist, or
129485**
129486** 2) The named parent key columns do exist, but are not subject to a
129487** UNIQUE or PRIMARY KEY constraint, or
129488**
129489** 3) No parent key columns were provided explicitly as part of the
129490** foreign key definition, and the parent table does not have a
129491** PRIMARY KEY, or
129492**
129493** 4) No parent key columns were provided explicitly as part of the
129494** foreign key definition, and the PRIMARY KEY of the parent table
129495** consists of a different number of columns to the child key in
129496** the child table.
129497**
129498** then non-zero is returned, and a "foreign key mismatch" error loaded
129499** into pParse. If an OOM error occurs, non-zero is returned and the
129500** pParse->db->mallocFailed flag is set.
129501*/
129502SQLITE_PRIVATE int sqlite3FkLocateIndex(
129503 Parse *pParse, /* Parse context to store any error in */
129504 Table *pParent, /* Parent table of FK constraint pFKey */
129505 FKey *pFKey, /* Foreign key to find index for */
129506 Index **ppIdx, /* OUT: Unique index on parent table */
129507 int **paiCol /* OUT: Map of index columns in pFKey */
129508){
129509 Index *pIdx = 0; /* Value to return via *ppIdx */
129510 int *aiCol = 0; /* Value to return via *paiCol */
129511 int nCol = pFKey->nCol; /* Number of columns in parent key */
129512 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
129513
129514 /* The caller is responsible for zeroing output parameters. */
129515 assert( ppIdx && *ppIdx==0 );
129516 assert( !paiCol || *paiCol==0 );
129517 assert( pParse );
129518
129519 /* If this is a non-composite (single column) foreign key, check if it
129520 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
129521 ** and *paiCol set to zero and return early.
129522 **
129523 ** Otherwise, for a composite foreign key (more than one column), allocate
129524 ** space for the aiCol array (returned via output parameter *paiCol).
129525 ** Non-composite foreign keys do not require the aiCol array.
129526 */
129527 if( nCol==1 ){
129528 /* The FK maps to the IPK if any of the following are true:
129529 **
129530 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
129531 ** mapped to the primary key of table pParent, or
129532 ** 2) The FK is explicitly mapped to a column declared as INTEGER
129533 ** PRIMARY KEY.
129534 */
129535 if( pParent->iPKey>=0 ){
129536 if( !zKey ) return 0;
129537 if( !sqlite3StrICmp(zLeft: pParent->aCol[pParent->iPKey].zCnName, zRight: zKey) ){
129538 return 0;
129539 }
129540 }
129541 }else if( paiCol ){
129542 assert( nCol>1 );
129543 aiCol = (int *)sqlite3DbMallocRawNN(db: pParse->db, n: nCol*sizeof(int));
129544 if( !aiCol ) return 1;
129545 *paiCol = aiCol;
129546 }
129547
129548 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
129549 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
129550 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
129551 ** of columns. If each indexed column corresponds to a foreign key
129552 ** column of pFKey, then this index is a winner. */
129553
129554 if( zKey==0 ){
129555 /* If zKey is NULL, then this foreign key is implicitly mapped to
129556 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
129557 ** identified by the test. */
129558 if( IsPrimaryKeyIndex(pIdx) ){
129559 if( aiCol ){
129560 int i;
129561 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
129562 }
129563 break;
129564 }
129565 }else{
129566 /* If zKey is non-NULL, then this foreign key was declared to
129567 ** map to an explicit list of columns in table pParent. Check if this
129568 ** index matches those columns. Also, check that the index uses
129569 ** the default collation sequences for each column. */
129570 int i, j;
129571 for(i=0; i<nCol; i++){
129572 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
129573 const char *zDfltColl; /* Def. collation for column */
129574 char *zIdxCol; /* Name of indexed column */
129575
129576 if( iCol<0 ) break; /* No foreign keys against expression indexes */
129577
129578 /* If the index uses a collation sequence that is different from
129579 ** the default collation sequence for the column, this index is
129580 ** unusable. Bail out early in this case. */
129581 zDfltColl = sqlite3ColumnColl(pCol: &pParent->aCol[iCol]);
129582 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
129583 if( sqlite3StrICmp(zLeft: pIdx->azColl[i], zRight: zDfltColl) ) break;
129584
129585 zIdxCol = pParent->aCol[iCol].zCnName;
129586 for(j=0; j<nCol; j++){
129587 if( sqlite3StrICmp(zLeft: pFKey->aCol[j].zCol, zRight: zIdxCol)==0 ){
129588 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
129589 break;
129590 }
129591 }
129592 if( j==nCol ) break;
129593 }
129594 if( i==nCol ) break; /* pIdx is usable */
129595 }
129596 }
129597 }
129598
129599 if( !pIdx ){
129600 if( !pParse->disableTriggers ){
129601 sqlite3ErrorMsg(pParse,
129602 zFormat: "foreign key mismatch - \"%w\" referencing \"%w\"",
129603 pFKey->pFrom->zName, pFKey->zTo);
129604 }
129605 sqlite3DbFree(db: pParse->db, p: aiCol);
129606 return 1;
129607 }
129608
129609 *ppIdx = pIdx;
129610 return 0;
129611}
129612
129613/*
129614** This function is called when a row is inserted into or deleted from the
129615** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
129616** on the child table of pFKey, this function is invoked twice for each row
129617** affected - once to "delete" the old row, and then again to "insert" the
129618** new row.
129619**
129620** Each time it is called, this function generates VDBE code to locate the
129621** row in the parent table that corresponds to the row being inserted into
129622** or deleted from the child table. If the parent row can be found, no
129623** special action is taken. Otherwise, if the parent row can *not* be
129624** found in the parent table:
129625**
129626** Operation | FK type | Action taken
129627** --------------------------------------------------------------------------
129628** INSERT immediate Increment the "immediate constraint counter".
129629**
129630** DELETE immediate Decrement the "immediate constraint counter".
129631**
129632** INSERT deferred Increment the "deferred constraint counter".
129633**
129634** DELETE deferred Decrement the "deferred constraint counter".
129635**
129636** These operations are identified in the comment at the top of this file
129637** (fkey.c) as "I.1" and "D.1".
129638*/
129639static void fkLookupParent(
129640 Parse *pParse, /* Parse context */
129641 int iDb, /* Index of database housing pTab */
129642 Table *pTab, /* Parent table of FK pFKey */
129643 Index *pIdx, /* Unique index on parent key columns in pTab */
129644 FKey *pFKey, /* Foreign key constraint */
129645 int *aiCol, /* Map from parent key columns to child table columns */
129646 int regData, /* Address of array containing child table row */
129647 int nIncr, /* Increment constraint counter by this */
129648 int isIgnore /* If true, pretend pTab contains all NULL values */
129649){
129650 int i; /* Iterator variable */
129651 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
129652 int iCur = pParse->nTab - 1; /* Cursor number to use */
129653 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
129654
129655 sqlite3VdbeVerifyAbortable(v,
129656 (!pFKey->isDeferred
129657 && !(pParse->db->flags & SQLITE_DeferFKs)
129658 && !pParse->pToplevel
129659 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
129660
129661 /* If nIncr is less than zero, then check at runtime if there are any
129662 ** outstanding constraints to resolve. If there are not, there is no need
129663 ** to check if deleting this row resolves any outstanding violations.
129664 **
129665 ** Check if any of the key columns in the child table row are NULL. If
129666 ** any are, then the constraint is considered satisfied. No need to
129667 ** search for a matching row in the parent table. */
129668 if( nIncr<0 ){
129669 sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: pFKey->isDeferred, p2: iOk);
129670 VdbeCoverage(v);
129671 }
129672 for(i=0; i<pFKey->nCol; i++){
129673 int iReg = sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: aiCol[i]) + regData + 1;
129674 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: iReg, p2: iOk); VdbeCoverage(v);
129675 }
129676
129677 if( isIgnore==0 ){
129678 if( pIdx==0 ){
129679 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
129680 ** column of the parent table (table pTab). */
129681 int iMustBeInt; /* Address of MustBeInt instruction */
129682 int regTemp = sqlite3GetTempReg(pParse);
129683
129684 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
129685 ** apply the affinity of the parent key). If this fails, then there
129686 ** is no matching parent key. Before using MustBeInt, make a copy of
129687 ** the value. Otherwise, the value inserted into the child key column
129688 ** will have INTEGER affinity applied to it, which may not be correct. */
129689 sqlite3VdbeAddOp2(p: v, OP_SCopy,
129690 p1: sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: aiCol[0])+1+regData, p2: regTemp);
129691 iMustBeInt = sqlite3VdbeAddOp2(p: v, OP_MustBeInt, p1: regTemp, p2: 0);
129692 VdbeCoverage(v);
129693
129694 /* If the parent table is the same as the child table, and we are about
129695 ** to increment the constraint-counter (i.e. this is an INSERT operation),
129696 ** then check if the row being inserted matches itself. If so, do not
129697 ** increment the constraint-counter. */
129698 if( pTab==pFKey->pFrom && nIncr==1 ){
129699 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regData, p2: iOk, p3: regTemp); VdbeCoverage(v);
129700 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
129701 }
129702
129703 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
129704 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iCur, p2: 0, p3: regTemp); VdbeCoverage(v);
129705 sqlite3VdbeGoto(p: v, iDest: iOk);
129706 sqlite3VdbeJumpHere(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-2);
129707 sqlite3VdbeJumpHere(p: v, addr: iMustBeInt);
129708 sqlite3ReleaseTempReg(pParse, iReg: regTemp);
129709 }else{
129710 int nCol = pFKey->nCol;
129711 int regTemp = sqlite3GetTempRange(pParse, nReg: nCol);
129712
129713 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iCur, p2: pIdx->tnum, p3: iDb);
129714 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
129715 for(i=0; i<nCol; i++){
129716 sqlite3VdbeAddOp2(p: v, OP_Copy,
129717 p1: sqlite3TableColumnToStorage(pTab: pFKey->pFrom, iCol: aiCol[i])+1+regData,
129718 p2: regTemp+i);
129719 }
129720
129721 /* If the parent table is the same as the child table, and we are about
129722 ** to increment the constraint-counter (i.e. this is an INSERT operation),
129723 ** then check if the row being inserted matches itself. If so, do not
129724 ** increment the constraint-counter.
129725 **
129726 ** If any of the parent-key values are NULL, then the row cannot match
129727 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
129728 ** of the parent-key values are NULL (at this point it is known that
129729 ** none of the child key values are).
129730 */
129731 if( pTab==pFKey->pFrom && nIncr==1 ){
129732 int iJump = sqlite3VdbeCurrentAddr(p: v) + nCol + 1;
129733 for(i=0; i<nCol; i++){
129734 int iChild = sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: aiCol[i])
129735 +1+regData;
129736 int iParent = 1+regData;
129737 iParent += sqlite3TableColumnToStorage(pTab: pIdx->pTable,
129738 iCol: pIdx->aiColumn[i]);
129739 assert( pIdx->aiColumn[i]>=0 );
129740 assert( aiCol[i]!=pTab->iPKey );
129741 if( pIdx->aiColumn[i]==pTab->iPKey ){
129742 /* The parent key is a composite key that includes the IPK column */
129743 iParent = regData;
129744 }
129745 sqlite3VdbeAddOp3(p: v, OP_Ne, p1: iChild, p2: iJump, p3: iParent); VdbeCoverage(v);
129746 sqlite3VdbeChangeP5(p: v, SQLITE_JUMPIFNULL);
129747 }
129748 sqlite3VdbeGoto(p: v, iDest: iOk);
129749 }
129750
129751 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: regTemp, p2: nCol, p3: 0,
129752 zP4: sqlite3IndexAffinityStr(pParse->db,pIdx), p4type: nCol);
129753 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iCur, p2: iOk, p3: regTemp, p4: nCol);
129754 VdbeCoverage(v);
129755 sqlite3ReleaseTempRange(pParse, iReg: regTemp, nReg: nCol);
129756 }
129757 }
129758
129759 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
129760 && !pParse->pToplevel
129761 && !pParse->isMultiWrite
129762 ){
129763 /* Special case: If this is an INSERT statement that will insert exactly
129764 ** one row into the table, raise a constraint immediately instead of
129765 ** incrementing a counter. This is necessary as the VM code is being
129766 ** generated for will not open a statement transaction. */
129767 assert( nIncr==1 );
129768 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
129769 OE_Abort, p4: 0, P4_STATIC, P5_ConstraintFK);
129770 }else{
129771 if( nIncr>0 && pFKey->isDeferred==0 ){
129772 sqlite3MayAbort(pParse);
129773 }
129774 sqlite3VdbeAddOp2(p: v, OP_FkCounter, p1: pFKey->isDeferred, p2: nIncr);
129775 }
129776
129777 sqlite3VdbeResolveLabel(v, x: iOk);
129778 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iCur);
129779}
129780
129781
129782/*
129783** Return an Expr object that refers to a memory register corresponding
129784** to column iCol of table pTab.
129785**
129786** regBase is the first of an array of register that contains the data
129787** for pTab. regBase itself holds the rowid. regBase+1 holds the first
129788** column. regBase+2 holds the second column, and so forth.
129789*/
129790static Expr *exprTableRegister(
129791 Parse *pParse, /* Parsing and code generating context */
129792 Table *pTab, /* The table whose content is at r[regBase]... */
129793 int regBase, /* Contents of table pTab */
129794 i16 iCol /* Which column of pTab is desired */
129795){
129796 Expr *pExpr;
129797 Column *pCol;
129798 const char *zColl;
129799 sqlite3 *db = pParse->db;
129800
129801 pExpr = sqlite3Expr(db, TK_REGISTER, zToken: 0);
129802 if( pExpr ){
129803 if( iCol>=0 && iCol!=pTab->iPKey ){
129804 pCol = &pTab->aCol[iCol];
129805 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
129806 pExpr->affExpr = pCol->affinity;
129807 zColl = sqlite3ColumnColl(pCol);
129808 if( zColl==0 ) zColl = db->pDfltColl->zName;
129809 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zC: zColl);
129810 }else{
129811 pExpr->iTable = regBase;
129812 pExpr->affExpr = SQLITE_AFF_INTEGER;
129813 }
129814 }
129815 return pExpr;
129816}
129817
129818/*
129819** Return an Expr object that refers to column iCol of table pTab which
129820** has cursor iCur.
129821*/
129822static Expr *exprTableColumn(
129823 sqlite3 *db, /* The database connection */
129824 Table *pTab, /* The table whose column is desired */
129825 int iCursor, /* The open cursor on the table */
129826 i16 iCol /* The column that is wanted */
129827){
129828 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, zToken: 0);
129829 if( pExpr ){
129830 assert( ExprUseYTab(pExpr) );
129831 pExpr->y.pTab = pTab;
129832 pExpr->iTable = iCursor;
129833 pExpr->iColumn = iCol;
129834 }
129835 return pExpr;
129836}
129837
129838/*
129839** This function is called to generate code executed when a row is deleted
129840** from the parent table of foreign key constraint pFKey and, if pFKey is
129841** deferred, when a row is inserted into the same table. When generating
129842** code for an SQL UPDATE operation, this function may be called twice -
129843** once to "delete" the old row and once to "insert" the new row.
129844**
129845** Parameter nIncr is passed -1 when inserting a row (as this may decrease
129846** the number of FK violations in the db) or +1 when deleting one (as this
129847** may increase the number of FK constraint problems).
129848**
129849** The code generated by this function scans through the rows in the child
129850** table that correspond to the parent table row being deleted or inserted.
129851** For each child row found, one of the following actions is taken:
129852**
129853** Operation | FK type | Action taken
129854** --------------------------------------------------------------------------
129855** DELETE immediate Increment the "immediate constraint counter".
129856**
129857** INSERT immediate Decrement the "immediate constraint counter".
129858**
129859** DELETE deferred Increment the "deferred constraint counter".
129860**
129861** INSERT deferred Decrement the "deferred constraint counter".
129862**
129863** These operations are identified in the comment at the top of this file
129864** (fkey.c) as "I.2" and "D.2".
129865*/
129866static void fkScanChildren(
129867 Parse *pParse, /* Parse context */
129868 SrcList *pSrc, /* The child table to be scanned */
129869 Table *pTab, /* The parent table */
129870 Index *pIdx, /* Index on parent covering the foreign key */
129871 FKey *pFKey, /* The foreign key linking pSrc to pTab */
129872 int *aiCol, /* Map from pIdx cols to child table cols */
129873 int regData, /* Parent row data starts here */
129874 int nIncr /* Amount to increment deferred counter by */
129875){
129876 sqlite3 *db = pParse->db; /* Database handle */
129877 int i; /* Iterator variable */
129878 Expr *pWhere = 0; /* WHERE clause to scan with */
129879 NameContext sNameContext; /* Context used to resolve WHERE clause */
129880 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
129881 int iFkIfZero = 0; /* Address of OP_FkIfZero */
129882 Vdbe *v = sqlite3GetVdbe(pParse);
129883
129884 assert( pIdx==0 || pIdx->pTable==pTab );
129885 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
129886 assert( pIdx!=0 || pFKey->nCol==1 );
129887 assert( pIdx!=0 || HasRowid(pTab) );
129888
129889 if( nIncr<0 ){
129890 iFkIfZero = sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: pFKey->isDeferred, p2: 0);
129891 VdbeCoverage(v);
129892 }
129893
129894 /* Create an Expr object representing an SQL expression like:
129895 **
129896 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
129897 **
129898 ** The collation sequence used for the comparison should be that of
129899 ** the parent key columns. The affinity of the parent key column should
129900 ** be applied to each child key value before the comparison takes place.
129901 */
129902 for(i=0; i<pFKey->nCol; i++){
129903 Expr *pLeft; /* Value from parent table row */
129904 Expr *pRight; /* Column ref to child table */
129905 Expr *pEq; /* Expression (pLeft = pRight) */
129906 i16 iCol; /* Index of column in child table */
129907 const char *zCol; /* Name of column in child table */
129908
129909 iCol = pIdx ? pIdx->aiColumn[i] : -1;
129910 pLeft = exprTableRegister(pParse, pTab, regBase: regData, iCol);
129911 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
129912 assert( iCol>=0 );
129913 zCol = pFKey->pFrom->aCol[iCol].zCnName;
129914 pRight = sqlite3Expr(db, TK_ID, zToken: zCol);
129915 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
129916 pWhere = sqlite3ExprAnd(pParse, pLeft: pWhere, pRight: pEq);
129917 }
129918
129919 /* If the child table is the same as the parent table, then add terms
129920 ** to the WHERE clause that prevent this entry from being scanned.
129921 ** The added WHERE clause terms are like this:
129922 **
129923 ** $current_rowid!=rowid
129924 ** NOT( $current_a==a AND $current_b==b AND ... )
129925 **
129926 ** The first form is used for rowid tables. The second form is used
129927 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
129928 ** (a,b,...). Either the parent or primary key could be used to
129929 ** uniquely identify the current row, but the parent key is more convenient
129930 ** as the required values have already been loaded into registers
129931 ** by the caller.
129932 */
129933 if( pTab==pFKey->pFrom && nIncr>0 ){
129934 Expr *pNe; /* Expression (pLeft != pRight) */
129935 Expr *pLeft; /* Value from parent table row */
129936 Expr *pRight; /* Column ref to child table */
129937 if( HasRowid(pTab) ){
129938 pLeft = exprTableRegister(pParse, pTab, regBase: regData, iCol: -1);
129939 pRight = exprTableColumn(db, pTab, iCursor: pSrc->a[0].iCursor, iCol: -1);
129940 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
129941 }else{
129942 Expr *pEq, *pAll = 0;
129943 assert( pIdx!=0 );
129944 for(i=0; i<pIdx->nKeyCol; i++){
129945 i16 iCol = pIdx->aiColumn[i];
129946 assert( iCol>=0 );
129947 pLeft = exprTableRegister(pParse, pTab, regBase: regData, iCol);
129948 pRight = sqlite3Expr(db, TK_ID, zToken: pTab->aCol[iCol].zCnName);
129949 pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight);
129950 pAll = sqlite3ExprAnd(pParse, pLeft: pAll, pRight: pEq);
129951 }
129952 pNe = sqlite3PExpr(pParse, TK_NOT, pLeft: pAll, pRight: 0);
129953 }
129954 pWhere = sqlite3ExprAnd(pParse, pLeft: pWhere, pRight: pNe);
129955 }
129956
129957 /* Resolve the references in the WHERE clause. */
129958 memset(s: &sNameContext, c: 0, n: sizeof(NameContext));
129959 sNameContext.pSrcList = pSrc;
129960 sNameContext.pParse = pParse;
129961 sqlite3ResolveExprNames(pNC: &sNameContext, pExpr: pWhere);
129962
129963 /* Create VDBE to loop through the entries in pSrc that match the WHERE
129964 ** clause. For each row found, increment either the deferred or immediate
129965 ** foreign key constraint counter. */
129966 if( pParse->nErr==0 ){
129967 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0);
129968 sqlite3VdbeAddOp2(p: v, OP_FkCounter, p1: pFKey->isDeferred, p2: nIncr);
129969 if( pWInfo ){
129970 sqlite3WhereEnd(pWInfo);
129971 }
129972 }
129973
129974 /* Clean up the WHERE clause constructed above. */
129975 sqlite3ExprDelete(db, p: pWhere);
129976 if( iFkIfZero ){
129977 sqlite3VdbeJumpHereOrPopInst(p: v, addr: iFkIfZero);
129978 }
129979}
129980
129981/*
129982** This function returns a linked list of FKey objects (connected by
129983** FKey.pNextTo) holding all children of table pTab. For example,
129984** given the following schema:
129985**
129986** CREATE TABLE t1(a PRIMARY KEY);
129987** CREATE TABLE t2(b REFERENCES t1(a);
129988**
129989** Calling this function with table "t1" as an argument returns a pointer
129990** to the FKey structure representing the foreign key constraint on table
129991** "t2". Calling this function with "t2" as the argument would return a
129992** NULL pointer (as there are no FK constraints for which t2 is the parent
129993** table).
129994*/
129995SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
129996 return (FKey *)sqlite3HashFind(pH: &pTab->pSchema->fkeyHash, pKey: pTab->zName);
129997}
129998
129999/*
130000** The second argument is a Trigger structure allocated by the
130001** fkActionTrigger() routine. This function deletes the Trigger structure
130002** and all of its sub-components.
130003**
130004** The Trigger structure or any of its sub-components may be allocated from
130005** the lookaside buffer belonging to database handle dbMem.
130006*/
130007static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
130008 if( p ){
130009 TriggerStep *pStep = p->step_list;
130010 sqlite3ExprDelete(db: dbMem, p: pStep->pWhere);
130011 sqlite3ExprListDelete(db: dbMem, pList: pStep->pExprList);
130012 sqlite3SelectDelete(dbMem, pStep->pSelect);
130013 sqlite3ExprDelete(db: dbMem, p: p->pWhen);
130014 sqlite3DbFree(db: dbMem, p);
130015 }
130016}
130017
130018/*
130019** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys
130020** in a particular database. This needs to happen when the schema
130021** changes.
130022*/
130023SQLITE_PRIVATE void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){
130024 HashElem *k;
130025 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
130026 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k)){
130027 Table *pTab = sqliteHashData(k);
130028 FKey *pFKey;
130029 if( !IsOrdinaryTable(pTab) ) continue;
130030 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
130031 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
130032 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
130033 }
130034 }
130035}
130036
130037/*
130038** This function is called to generate code that runs when table pTab is
130039** being dropped from the database. The SrcList passed as the second argument
130040** to this function contains a single entry guaranteed to resolve to
130041** table pTab.
130042**
130043** Normally, no code is required. However, if either
130044**
130045** (a) The table is the parent table of a FK constraint, or
130046** (b) The table is the child table of a deferred FK constraint and it is
130047** determined at runtime that there are outstanding deferred FK
130048** constraint violations in the database,
130049**
130050** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
130051** the table from the database. Triggers are disabled while running this
130052** DELETE, but foreign key actions are not.
130053*/
130054SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
130055 sqlite3 *db = pParse->db;
130056 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
130057 int iSkip = 0;
130058 Vdbe *v = sqlite3GetVdbe(pParse);
130059
130060 assert( v ); /* VDBE has already been allocated */
130061 assert( IsOrdinaryTable(pTab) );
130062 if( sqlite3FkReferences(pTab)==0 ){
130063 /* Search for a deferred foreign key constraint for which this table
130064 ** is the child table. If one cannot be found, return without
130065 ** generating any VDBE code. If one can be found, then jump over
130066 ** the entire DELETE if there are no outstanding deferred constraints
130067 ** when this statement is run. */
130068 FKey *p;
130069 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
130070 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
130071 }
130072 if( !p ) return;
130073 iSkip = sqlite3VdbeMakeLabel(pParse);
130074 sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: 1, p2: iSkip); VdbeCoverage(v);
130075 }
130076
130077 pParse->disableTriggers = 1;
130078 sqlite3DeleteFrom(pParse, pTabList: sqlite3SrcListDup(db, p: pName, flags: 0), pWhere: 0, pOrderBy: 0, pLimit: 0);
130079 pParse->disableTriggers = 0;
130080
130081 /* If the DELETE has generated immediate foreign key constraint
130082 ** violations, halt the VDBE and return an error at this point, before
130083 ** any modifications to the schema are made. This is because statement
130084 ** transactions are not able to rollback schema changes.
130085 **
130086 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
130087 ** the statement transaction will not be rolled back even if FK
130088 ** constraints are violated.
130089 */
130090 if( (db->flags & SQLITE_DeferFKs)==0 ){
130091 sqlite3VdbeVerifyAbortable(v, OE_Abort);
130092 sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: 0, p2: sqlite3VdbeCurrentAddr(p: v)+2);
130093 VdbeCoverage(v);
130094 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
130095 OE_Abort, p4: 0, P4_STATIC, P5_ConstraintFK);
130096 }
130097
130098 if( iSkip ){
130099 sqlite3VdbeResolveLabel(v, x: iSkip);
130100 }
130101 }
130102}
130103
130104
130105/*
130106** The second argument points to an FKey object representing a foreign key
130107** for which pTab is the child table. An UPDATE statement against pTab
130108** is currently being processed. For each column of the table that is
130109** actually updated, the corresponding element in the aChange[] array
130110** is zero or greater (if a column is unmodified the corresponding element
130111** is set to -1). If the rowid column is modified by the UPDATE statement
130112** the bChngRowid argument is non-zero.
130113**
130114** This function returns true if any of the columns that are part of the
130115** child key for FK constraint *p are modified.
130116*/
130117static int fkChildIsModified(
130118 Table *pTab, /* Table being updated */
130119 FKey *p, /* Foreign key for which pTab is the child */
130120 int *aChange, /* Array indicating modified columns */
130121 int bChngRowid /* True if rowid is modified by this update */
130122){
130123 int i;
130124 for(i=0; i<p->nCol; i++){
130125 int iChildKey = p->aCol[i].iFrom;
130126 if( aChange[iChildKey]>=0 ) return 1;
130127 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
130128 }
130129 return 0;
130130}
130131
130132/*
130133** The second argument points to an FKey object representing a foreign key
130134** for which pTab is the parent table. An UPDATE statement against pTab
130135** is currently being processed. For each column of the table that is
130136** actually updated, the corresponding element in the aChange[] array
130137** is zero or greater (if a column is unmodified the corresponding element
130138** is set to -1). If the rowid column is modified by the UPDATE statement
130139** the bChngRowid argument is non-zero.
130140**
130141** This function returns true if any of the columns that are part of the
130142** parent key for FK constraint *p are modified.
130143*/
130144static int fkParentIsModified(
130145 Table *pTab,
130146 FKey *p,
130147 int *aChange,
130148 int bChngRowid
130149){
130150 int i;
130151 for(i=0; i<p->nCol; i++){
130152 char *zKey = p->aCol[i].zCol;
130153 int iKey;
130154 for(iKey=0; iKey<pTab->nCol; iKey++){
130155 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
130156 Column *pCol = &pTab->aCol[iKey];
130157 if( zKey ){
130158 if( 0==sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zKey) ) return 1;
130159 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
130160 return 1;
130161 }
130162 }
130163 }
130164 }
130165 return 0;
130166}
130167
130168/*
130169** Return true if the parser passed as the first argument is being
130170** used to code a trigger that is really a "SET NULL" action belonging
130171** to trigger pFKey.
130172*/
130173static int isSetNullAction(Parse *pParse, FKey *pFKey){
130174 Parse *pTop = sqlite3ParseToplevel(pParse);
130175 if( pTop->pTriggerPrg ){
130176 Trigger *p = pTop->pTriggerPrg->pTrigger;
130177 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
130178 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
130179 ){
130180 return 1;
130181 }
130182 }
130183 return 0;
130184}
130185
130186/*
130187** This function is called when inserting, deleting or updating a row of
130188** table pTab to generate VDBE code to perform foreign key constraint
130189** processing for the operation.
130190**
130191** For a DELETE operation, parameter regOld is passed the index of the
130192** first register in an array of (pTab->nCol+1) registers containing the
130193** rowid of the row being deleted, followed by each of the column values
130194** of the row being deleted, from left to right. Parameter regNew is passed
130195** zero in this case.
130196**
130197** For an INSERT operation, regOld is passed zero and regNew is passed the
130198** first register of an array of (pTab->nCol+1) registers containing the new
130199** row data.
130200**
130201** For an UPDATE operation, this function is called twice. Once before
130202** the original record is deleted from the table using the calling convention
130203** described for DELETE. Then again after the original record is deleted
130204** but before the new record is inserted using the INSERT convention.
130205*/
130206SQLITE_PRIVATE void sqlite3FkCheck(
130207 Parse *pParse, /* Parse context */
130208 Table *pTab, /* Row is being deleted from this table */
130209 int regOld, /* Previous row data is stored here */
130210 int regNew, /* New row data is stored here */
130211 int *aChange, /* Array indicating UPDATEd columns (or 0) */
130212 int bChngRowid /* True if rowid is UPDATEd */
130213){
130214 sqlite3 *db = pParse->db; /* Database handle */
130215 FKey *pFKey; /* Used to iterate through FKs */
130216 int iDb; /* Index of database containing pTab */
130217 const char *zDb; /* Name of database containing pTab */
130218 int isIgnoreErrors = pParse->disableTriggers;
130219
130220 /* Exactly one of regOld and regNew should be non-zero. */
130221 assert( (regOld==0)!=(regNew==0) );
130222
130223 /* If foreign-keys are disabled, this function is a no-op. */
130224 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
130225 if( !IsOrdinaryTable(pTab) ) return;
130226
130227 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130228 zDb = db->aDb[iDb].zDbSName;
130229
130230 /* Loop through all the foreign key constraints for which pTab is the
130231 ** child table (the table that the foreign key definition is part of). */
130232 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
130233 Table *pTo; /* Parent table of foreign key pFKey */
130234 Index *pIdx = 0; /* Index on key columns in pTo */
130235 int *aiFree = 0;
130236 int *aiCol;
130237 int iCol;
130238 int i;
130239 int bIgnore = 0;
130240
130241 if( aChange
130242 && sqlite3_stricmp(zLeft: pTab->zName, zRight: pFKey->zTo)!=0
130243 && fkChildIsModified(pTab, p: pFKey, aChange, bChngRowid)==0
130244 ){
130245 continue;
130246 }
130247
130248 /* Find the parent table of this foreign key. Also find a unique index
130249 ** on the parent key columns in the parent table. If either of these
130250 ** schema items cannot be located, set an error in pParse and return
130251 ** early. */
130252 if( pParse->disableTriggers ){
130253 pTo = sqlite3FindTable(db, zName: pFKey->zTo, zDatabase: zDb);
130254 }else{
130255 pTo = sqlite3LocateTable(pParse, flags: 0, zName: pFKey->zTo, zDbase: zDb);
130256 }
130257 if( !pTo || sqlite3FkLocateIndex(pParse, pParent: pTo, pFKey, ppIdx: &pIdx, paiCol: &aiFree) ){
130258 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
130259 if( !isIgnoreErrors || db->mallocFailed ) return;
130260 if( pTo==0 ){
130261 /* If isIgnoreErrors is true, then a table is being dropped. In this
130262 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
130263 ** before actually dropping it in order to check FK constraints.
130264 ** If the parent table of an FK constraint on the current table is
130265 ** missing, behave as if it is empty. i.e. decrement the relevant
130266 ** FK counter for each row of the current table with non-NULL keys.
130267 */
130268 Vdbe *v = sqlite3GetVdbe(pParse);
130269 int iJump = sqlite3VdbeCurrentAddr(p: v) + pFKey->nCol + 1;
130270 for(i=0; i<pFKey->nCol; i++){
130271 int iFromCol, iReg;
130272 iFromCol = pFKey->aCol[i].iFrom;
130273 iReg = sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: iFromCol) + regOld+1;
130274 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: iReg, p2: iJump); VdbeCoverage(v);
130275 }
130276 sqlite3VdbeAddOp2(p: v, OP_FkCounter, p1: pFKey->isDeferred, p2: -1);
130277 }
130278 continue;
130279 }
130280 assert( pFKey->nCol==1 || (aiFree && pIdx) );
130281
130282 if( aiFree ){
130283 aiCol = aiFree;
130284 }else{
130285 iCol = pFKey->aCol[0].iFrom;
130286 aiCol = &iCol;
130287 }
130288 for(i=0; i<pFKey->nCol; i++){
130289 if( aiCol[i]==pTab->iPKey ){
130290 aiCol[i] = -1;
130291 }
130292 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
130293#ifndef SQLITE_OMIT_AUTHORIZATION
130294 /* Request permission to read the parent key columns. If the
130295 ** authorization callback returns SQLITE_IGNORE, behave as if any
130296 ** values read from the parent table are NULL. */
130297 if( db->xAuth ){
130298 int rcauth;
130299 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
130300 rcauth = sqlite3AuthReadCol(pParse, zTab: pTo->zName, zCol, iDb);
130301 bIgnore = (rcauth==SQLITE_IGNORE);
130302 }
130303#endif
130304 }
130305
130306 /* Take a shared-cache advisory read-lock on the parent table. Allocate
130307 ** a cursor to use to search the unique index on the parent key columns
130308 ** in the parent table. */
130309 sqlite3TableLock(pParse, iDb, iTab: pTo->tnum, isWriteLock: 0, zName: pTo->zName);
130310 pParse->nTab++;
130311
130312 if( regOld!=0 ){
130313 /* A row is being removed from the child table. Search for the parent.
130314 ** If the parent does not exist, removing the child row resolves an
130315 ** outstanding foreign key constraint violation. */
130316 fkLookupParent(pParse, iDb, pTab: pTo, pIdx, pFKey, aiCol, regData: regOld, nIncr: -1, isIgnore: bIgnore);
130317 }
130318 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
130319 /* A row is being added to the child table. If a parent row cannot
130320 ** be found, adding the child row has violated the FK constraint.
130321 **
130322 ** If this operation is being performed as part of a trigger program
130323 ** that is actually a "SET NULL" action belonging to this very
130324 ** foreign key, then omit this scan altogether. As all child key
130325 ** values are guaranteed to be NULL, it is not possible for adding
130326 ** this row to cause an FK violation. */
130327 fkLookupParent(pParse, iDb, pTab: pTo, pIdx, pFKey, aiCol, regData: regNew, nIncr: +1, isIgnore: bIgnore);
130328 }
130329
130330 sqlite3DbFree(db, p: aiFree);
130331 }
130332
130333 /* Loop through all the foreign key constraints that refer to this table.
130334 ** (the "child" constraints) */
130335 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
130336 Index *pIdx = 0; /* Foreign key index for pFKey */
130337 SrcList *pSrc;
130338 int *aiCol = 0;
130339
130340 if( aChange && fkParentIsModified(pTab, p: pFKey, aChange, bChngRowid)==0 ){
130341 continue;
130342 }
130343
130344 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
130345 && !pParse->pToplevel && !pParse->isMultiWrite
130346 ){
130347 assert( regOld==0 && regNew!=0 );
130348 /* Inserting a single row into a parent table cannot cause (or fix)
130349 ** an immediate foreign key violation. So do nothing in this case. */
130350 continue;
130351 }
130352
130353 if( sqlite3FkLocateIndex(pParse, pParent: pTab, pFKey, ppIdx: &pIdx, paiCol: &aiCol) ){
130354 if( !isIgnoreErrors || db->mallocFailed ) return;
130355 continue;
130356 }
130357 assert( aiCol || pFKey->nCol==1 );
130358
130359 /* Create a SrcList structure containing the child table. We need the
130360 ** child table as a SrcList for sqlite3WhereBegin() */
130361 pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
130362 if( pSrc ){
130363 SrcItem *pItem = pSrc->a;
130364 pItem->pTab = pFKey->pFrom;
130365 pItem->zName = pFKey->pFrom->zName;
130366 pItem->pTab->nTabRef++;
130367 pItem->iCursor = pParse->nTab++;
130368
130369 if( regNew!=0 ){
130370 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regData: regNew, nIncr: -1);
130371 }
130372 if( regOld!=0 ){
130373 int eAction = pFKey->aAction[aChange!=0];
130374 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regData: regOld, nIncr: 1);
130375 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
130376 ** action applies, then any foreign key violations caused by
130377 ** removing the parent key will be rectified by the action trigger.
130378 ** So do not set the "may-abort" flag in this case.
130379 **
130380 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
130381 ** may-abort flag will eventually be set on this statement anyway
130382 ** (when this function is called as part of processing the UPDATE
130383 ** within the action trigger).
130384 **
130385 ** Note 2: At first glance it may seem like SQLite could simply omit
130386 ** all OP_FkCounter related scans when either CASCADE or SET NULL
130387 ** applies. The trouble starts if the CASCADE or SET NULL action
130388 ** trigger causes other triggers or action rules attached to the
130389 ** child table to fire. In these cases the fk constraint counters
130390 ** might be set incorrectly if any OP_FkCounter related scans are
130391 ** omitted. */
130392 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
130393 sqlite3MayAbort(pParse);
130394 }
130395 }
130396 pItem->zName = 0;
130397 sqlite3SrcListDelete(db, pList: pSrc);
130398 }
130399 sqlite3DbFree(db, p: aiCol);
130400 }
130401}
130402
130403#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
130404
130405/*
130406** This function is called before generating code to update or delete a
130407** row contained in table pTab.
130408*/
130409SQLITE_PRIVATE u32 sqlite3FkOldmask(
130410 Parse *pParse, /* Parse context */
130411 Table *pTab /* Table being modified */
130412){
130413 u32 mask = 0;
130414 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
130415 FKey *p;
130416 int i;
130417 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
130418 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
130419 }
130420 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
130421 Index *pIdx = 0;
130422 sqlite3FkLocateIndex(pParse, pParent: pTab, pFKey: p, ppIdx: &pIdx, paiCol: 0);
130423 if( pIdx ){
130424 for(i=0; i<pIdx->nKeyCol; i++){
130425 assert( pIdx->aiColumn[i]>=0 );
130426 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
130427 }
130428 }
130429 }
130430 }
130431 return mask;
130432}
130433
130434
130435/*
130436** This function is called before generating code to update or delete a
130437** row contained in table pTab. If the operation is a DELETE, then
130438** parameter aChange is passed a NULL value. For an UPDATE, aChange points
130439** to an array of size N, where N is the number of columns in table pTab.
130440** If the i'th column is not modified by the UPDATE, then the corresponding
130441** entry in the aChange[] array is set to -1. If the column is modified,
130442** the value is 0 or greater. Parameter chngRowid is set to true if the
130443** UPDATE statement modifies the rowid fields of the table.
130444**
130445** If any foreign key processing will be required, this function returns
130446** non-zero. If there is no foreign key related processing, this function
130447** returns zero.
130448**
130449** For an UPDATE, this function returns 2 if:
130450**
130451** * There are any FKs for which pTab is the child and the parent table
130452** and any FK processing at all is required (even of a different FK), or
130453**
130454** * the UPDATE modifies one or more parent keys for which the action is
130455** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
130456**
130457** Or, assuming some other foreign key processing is required, 1.
130458*/
130459SQLITE_PRIVATE int sqlite3FkRequired(
130460 Parse *pParse, /* Parse context */
130461 Table *pTab, /* Table being modified */
130462 int *aChange, /* Non-NULL for UPDATE operations */
130463 int chngRowid /* True for UPDATE that affects rowid */
130464){
130465 int eRet = 1; /* Value to return if bHaveFK is true */
130466 int bHaveFK = 0; /* If FK processing is required */
130467 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
130468 if( !aChange ){
130469 /* A DELETE operation. Foreign key processing is required if the
130470 ** table in question is either the child or parent table for any
130471 ** foreign key constraint. */
130472 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
130473 }else{
130474 /* This is an UPDATE. Foreign key processing is only required if the
130475 ** operation modifies one or more child or parent key columns. */
130476 FKey *p;
130477
130478 /* Check if any child key columns are being modified. */
130479 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
130480 if( fkChildIsModified(pTab, p, aChange, bChngRowid: chngRowid) ){
130481 if( 0==sqlite3_stricmp(zLeft: pTab->zName, zRight: p->zTo) ) eRet = 2;
130482 bHaveFK = 1;
130483 }
130484 }
130485
130486 /* Check if any parent key columns are being modified. */
130487 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
130488 if( fkParentIsModified(pTab, p, aChange, bChngRowid: chngRowid) ){
130489 if( p->aAction[1]!=OE_None ) return 2;
130490 bHaveFK = 1;
130491 }
130492 }
130493 }
130494 }
130495 return bHaveFK ? eRet : 0;
130496}
130497
130498/*
130499** This function is called when an UPDATE or DELETE operation is being
130500** compiled on table pTab, which is the parent table of foreign-key pFKey.
130501** If the current operation is an UPDATE, then the pChanges parameter is
130502** passed a pointer to the list of columns being modified. If it is a
130503** DELETE, pChanges is passed a NULL pointer.
130504**
130505** It returns a pointer to a Trigger structure containing a trigger
130506** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
130507** If the action is "NO ACTION" then a NULL pointer is returned (these actions
130508** require no special handling by the triggers sub-system, code for them is
130509** created by fkScanChildren()).
130510**
130511** For example, if pFKey is the foreign key and pTab is table "p" in
130512** the following schema:
130513**
130514** CREATE TABLE p(pk PRIMARY KEY);
130515** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
130516**
130517** then the returned trigger structure is equivalent to:
130518**
130519** CREATE TRIGGER ... DELETE ON p BEGIN
130520** DELETE FROM c WHERE ck = old.pk;
130521** END;
130522**
130523** The returned pointer is cached as part of the foreign key object. It
130524** is eventually freed along with the rest of the foreign key object by
130525** sqlite3FkDelete().
130526*/
130527static Trigger *fkActionTrigger(
130528 Parse *pParse, /* Parse context */
130529 Table *pTab, /* Table being updated or deleted from */
130530 FKey *pFKey, /* Foreign key to get action for */
130531 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
130532){
130533 sqlite3 *db = pParse->db; /* Database handle */
130534 int action; /* One of OE_None, OE_Cascade etc. */
130535 Trigger *pTrigger; /* Trigger definition to return */
130536 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
130537
130538 action = pFKey->aAction[iAction];
130539 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
130540 return 0;
130541 }
130542 pTrigger = pFKey->apTrigger[iAction];
130543
130544 if( action!=OE_None && !pTrigger ){
130545 char const *zFrom; /* Name of child table */
130546 int nFrom; /* Length in bytes of zFrom */
130547 Index *pIdx = 0; /* Parent key index for this FK */
130548 int *aiCol = 0; /* child table cols -> parent key cols */
130549 TriggerStep *pStep = 0; /* First (only) step of trigger program */
130550 Expr *pWhere = 0; /* WHERE clause of trigger step */
130551 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
130552 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
130553 int i; /* Iterator variable */
130554 Expr *pWhen = 0; /* WHEN clause for the trigger */
130555
130556 if( sqlite3FkLocateIndex(pParse, pParent: pTab, pFKey, ppIdx: &pIdx, paiCol: &aiCol) ) return 0;
130557 assert( aiCol || pFKey->nCol==1 );
130558
130559 for(i=0; i<pFKey->nCol; i++){
130560 Token tOld = { "old", 3 }; /* Literal "old" token */
130561 Token tNew = { "new", 3 }; /* Literal "new" token */
130562 Token tFromCol; /* Name of column in child table */
130563 Token tToCol; /* Name of column in parent table */
130564 int iFromCol; /* Idx of column in child table */
130565 Expr *pEq; /* tFromCol = OLD.tToCol */
130566
130567 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
130568 assert( iFromCol>=0 );
130569 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
130570 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
130571 sqlite3TokenInit(p: &tToCol,
130572 z: pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
130573 sqlite3TokenInit(p: &tFromCol, z: pFKey->pFrom->aCol[iFromCol].zCnName);
130574
130575 /* Create the expression "OLD.zToCol = zFromCol". It is important
130576 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
130577 ** that the affinity and collation sequence associated with the
130578 ** parent table are used for the comparison. */
130579 pEq = sqlite3PExpr(pParse, TK_EQ,
130580 pLeft: sqlite3PExpr(pParse, TK_DOT,
130581 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tOld, dequote: 0),
130582 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0)),
130583 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tFromCol, dequote: 0)
130584 );
130585 pWhere = sqlite3ExprAnd(pParse, pLeft: pWhere, pRight: pEq);
130586
130587 /* For ON UPDATE, construct the next term of the WHEN clause.
130588 ** The final WHEN clause will be like this:
130589 **
130590 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
130591 */
130592 if( pChanges ){
130593 pEq = sqlite3PExpr(pParse, TK_IS,
130594 pLeft: sqlite3PExpr(pParse, TK_DOT,
130595 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tOld, dequote: 0),
130596 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0)),
130597 pRight: sqlite3PExpr(pParse, TK_DOT,
130598 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tNew, dequote: 0),
130599 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0))
130600 );
130601 pWhen = sqlite3ExprAnd(pParse, pLeft: pWhen, pRight: pEq);
130602 }
130603
130604 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
130605 Expr *pNew;
130606 if( action==OE_Cascade ){
130607 pNew = sqlite3PExpr(pParse, TK_DOT,
130608 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tNew, dequote: 0),
130609 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0));
130610 }else if( action==OE_SetDflt ){
130611 Column *pCol = pFKey->pFrom->aCol + iFromCol;
130612 Expr *pDflt;
130613 if( pCol->colFlags & COLFLAG_GENERATED ){
130614 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
130615 testcase( pCol->colFlags & COLFLAG_STORED );
130616 pDflt = 0;
130617 }else{
130618 pDflt = sqlite3ColumnExpr(pTab: pFKey->pFrom, pCol);
130619 }
130620 if( pDflt ){
130621 pNew = sqlite3ExprDup(db, p: pDflt, flags: 0);
130622 }else{
130623 pNew = sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0);
130624 }
130625 }else{
130626 pNew = sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0);
130627 }
130628 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pNew);
130629 sqlite3ExprListSetName(pParse, pList, pName: &tFromCol, dequote: 0);
130630 }
130631 }
130632 sqlite3DbFree(db, p: aiCol);
130633
130634 zFrom = pFKey->pFrom->zName;
130635 nFrom = sqlite3Strlen30(z: zFrom);
130636
130637 if( action==OE_Restrict ){
130638 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130639 SrcList *pSrc;
130640 Expr *pRaise;
130641
130642 pRaise = sqlite3Expr(db, TK_RAISE, zToken: "FOREIGN KEY constraint failed");
130643 if( pRaise ){
130644 pRaise->affExpr = OE_Abort;
130645 }
130646 pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
130647 if( pSrc ){
130648 assert( pSrc->nSrc==1 );
130649 pSrc->a[0].zName = sqlite3DbStrDup(db, z: zFrom);
130650 pSrc->a[0].zDatabase = sqlite3DbStrDup(db, z: db->aDb[iDb].zDbSName);
130651 }
130652 pSelect = sqlite3SelectNew(pParse,
130653 sqlite3ExprListAppend(pParse, pList: 0, pExpr: pRaise),
130654 pSrc,
130655 pWhere,
130656 0, 0, 0, 0, 0
130657 );
130658 pWhere = 0;
130659 }
130660
130661 /* Disable lookaside memory allocation */
130662 DisableLookaside;
130663
130664 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
130665 n: sizeof(Trigger) + /* struct Trigger */
130666 sizeof(TriggerStep) + /* Single step in trigger program */
130667 nFrom + 1 /* Space for pStep->zTarget */
130668 );
130669 if( pTrigger ){
130670 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
130671 pStep->zTarget = (char *)&pStep[1];
130672 memcpy(dest: (char *)pStep->zTarget, src: zFrom, n: nFrom);
130673
130674 pStep->pWhere = sqlite3ExprDup(db, p: pWhere, EXPRDUP_REDUCE);
130675 pStep->pExprList = sqlite3ExprListDup(db, p: pList, EXPRDUP_REDUCE);
130676 pStep->pSelect = sqlite3SelectDup(db, pDup: pSelect, EXPRDUP_REDUCE);
130677 if( pWhen ){
130678 pWhen = sqlite3PExpr(pParse, TK_NOT, pLeft: pWhen, pRight: 0);
130679 pTrigger->pWhen = sqlite3ExprDup(db, p: pWhen, EXPRDUP_REDUCE);
130680 }
130681 }
130682
130683 /* Re-enable the lookaside buffer, if it was disabled earlier. */
130684 EnableLookaside;
130685
130686 sqlite3ExprDelete(db, p: pWhere);
130687 sqlite3ExprDelete(db, p: pWhen);
130688 sqlite3ExprListDelete(db, pList);
130689 sqlite3SelectDelete(db, pSelect);
130690 if( db->mallocFailed==1 ){
130691 fkTriggerDelete(dbMem: db, p: pTrigger);
130692 return 0;
130693 }
130694 assert( pStep!=0 );
130695 assert( pTrigger!=0 );
130696
130697 switch( action ){
130698 case OE_Restrict:
130699 pStep->op = TK_SELECT;
130700 break;
130701 case OE_Cascade:
130702 if( !pChanges ){
130703 pStep->op = TK_DELETE;
130704 break;
130705 }
130706 /* no break */ deliberate_fall_through
130707 default:
130708 pStep->op = TK_UPDATE;
130709 }
130710 pStep->pTrig = pTrigger;
130711 pTrigger->pSchema = pTab->pSchema;
130712 pTrigger->pTabSchema = pTab->pSchema;
130713 pFKey->apTrigger[iAction] = pTrigger;
130714 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
130715 }
130716
130717 return pTrigger;
130718}
130719
130720/*
130721** This function is called when deleting or updating a row to implement
130722** any required CASCADE, SET NULL or SET DEFAULT actions.
130723*/
130724SQLITE_PRIVATE void sqlite3FkActions(
130725 Parse *pParse, /* Parse context */
130726 Table *pTab, /* Table being updated or deleted from */
130727 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
130728 int regOld, /* Address of array containing old row */
130729 int *aChange, /* Array indicating UPDATEd columns (or 0) */
130730 int bChngRowid /* True if rowid is UPDATEd */
130731){
130732 /* If foreign-key support is enabled, iterate through all FKs that
130733 ** refer to table pTab. If there is an action associated with the FK
130734 ** for this operation (either update or delete), invoke the associated
130735 ** trigger sub-program. */
130736 if( pParse->db->flags&SQLITE_ForeignKeys ){
130737 FKey *pFKey; /* Iterator variable */
130738 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
130739 if( aChange==0 || fkParentIsModified(pTab, p: pFKey, aChange, bChngRowid) ){
130740 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
130741 if( pAct ){
130742 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
130743 }
130744 }
130745 }
130746 }
130747}
130748
130749#endif /* ifndef SQLITE_OMIT_TRIGGER */
130750
130751/*
130752** Free all memory associated with foreign key definitions attached to
130753** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
130754** hash table.
130755*/
130756SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
130757 FKey *pFKey; /* Iterator variable */
130758 FKey *pNext; /* Copy of pFKey->pNextFrom */
130759
130760 assert( IsOrdinaryTable(pTab) );
130761 assert( db!=0 );
130762 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
130763 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
130764
130765 /* Remove the FK from the fkeyHash hash table. */
130766 if( db->pnBytesFreed==0 ){
130767 if( pFKey->pPrevTo ){
130768 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
130769 }else{
130770 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
130771 sqlite3HashInsert(pH: &pTab->pSchema->fkeyHash, pKey: z, data: pFKey->pNextTo);
130772 }
130773 if( pFKey->pNextTo ){
130774 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
130775 }
130776 }
130777
130778 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
130779 ** classified as either immediate or deferred.
130780 */
130781 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
130782
130783 /* Delete any triggers created to implement actions for this FK. */
130784#ifndef SQLITE_OMIT_TRIGGER
130785 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[0]);
130786 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[1]);
130787#endif
130788
130789 pNext = pFKey->pNextFrom;
130790 sqlite3DbFree(db, p: pFKey);
130791 }
130792}
130793#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
130794
130795/************** End of fkey.c ************************************************/
130796/************** Begin file insert.c ******************************************/
130797/*
130798** 2001 September 15
130799**
130800** The author disclaims copyright to this source code. In place of
130801** a legal notice, here is a blessing:
130802**
130803** May you do good and not evil.
130804** May you find forgiveness for yourself and forgive others.
130805** May you share freely, never taking more than you give.
130806**
130807*************************************************************************
130808** This file contains C code routines that are called by the parser
130809** to handle INSERT statements in SQLite.
130810*/
130811/* #include "sqliteInt.h" */
130812
130813/*
130814** Generate code that will
130815**
130816** (1) acquire a lock for table pTab then
130817** (2) open pTab as cursor iCur.
130818**
130819** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
130820** for that table that is actually opened.
130821*/
130822SQLITE_PRIVATE void sqlite3OpenTable(
130823 Parse *pParse, /* Generate code into this VDBE */
130824 int iCur, /* The cursor number of the table */
130825 int iDb, /* The database index in sqlite3.aDb[] */
130826 Table *pTab, /* The table to be opened */
130827 int opcode /* OP_OpenRead or OP_OpenWrite */
130828){
130829 Vdbe *v;
130830 assert( !IsVirtual(pTab) );
130831 assert( pParse->pVdbe!=0 );
130832 v = pParse->pVdbe;
130833 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
130834 if( !pParse->db->noSharedCache ){
130835 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum,
130836 isWriteLock: (opcode==OP_OpenWrite)?1:0, zName: pTab->zName);
130837 }
130838 if( HasRowid(pTab) ){
130839 sqlite3VdbeAddOp4Int(p: v, op: opcode, p1: iCur, p2: pTab->tnum, p3: iDb, p4: pTab->nNVCol);
130840 VdbeComment((v, "%s", pTab->zName));
130841 }else{
130842 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
130843 assert( pPk!=0 );
130844 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
130845 sqlite3VdbeAddOp3(p: v, op: opcode, p1: iCur, p2: pPk->tnum, p3: iDb);
130846 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
130847 VdbeComment((v, "%s", pTab->zName));
130848 }
130849}
130850
130851/*
130852** Return a pointer to the column affinity string associated with index
130853** pIdx. A column affinity string has one character for each column in
130854** the table, according to the affinity of the column:
130855**
130856** Character Column affinity
130857** ------------------------------
130858** 'A' BLOB
130859** 'B' TEXT
130860** 'C' NUMERIC
130861** 'D' INTEGER
130862** 'F' REAL
130863**
130864** An extra 'D' is appended to the end of the string to cover the
130865** rowid that appears as the last column in every index.
130866**
130867** Memory for the buffer containing the column index affinity string
130868** is managed along with the rest of the Index structure. It will be
130869** released when sqlite3DeleteIndex() is called.
130870*/
130871static SQLITE_NOINLINE const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){
130872 /* The first time a column affinity string for a particular index is
130873 ** required, it is allocated and populated here. It is then stored as
130874 ** a member of the Index structure for subsequent use.
130875 **
130876 ** The column affinity string will eventually be deleted by
130877 ** sqliteDeleteIndex() when the Index structure itself is cleaned
130878 ** up.
130879 */
130880 int n;
130881 Table *pTab = pIdx->pTable;
130882 pIdx->zColAff = (char *)sqlite3DbMallocRaw(db: 0, n: pIdx->nColumn+1);
130883 if( !pIdx->zColAff ){
130884 sqlite3OomFault(db);
130885 return 0;
130886 }
130887 for(n=0; n<pIdx->nColumn; n++){
130888 i16 x = pIdx->aiColumn[n];
130889 char aff;
130890 if( x>=0 ){
130891 aff = pTab->aCol[x].affinity;
130892 }else if( x==XN_ROWID ){
130893 aff = SQLITE_AFF_INTEGER;
130894 }else{
130895 assert( x==XN_EXPR );
130896 assert( pIdx->bHasExpr );
130897 assert( pIdx->aColExpr!=0 );
130898 aff = sqlite3ExprAffinity(pExpr: pIdx->aColExpr->a[n].pExpr);
130899 }
130900 if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
130901 if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
130902 pIdx->zColAff[n] = aff;
130903 }
130904 pIdx->zColAff[n] = 0;
130905 return pIdx->zColAff;
130906}
130907SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
130908 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
130909 return pIdx->zColAff;
130910}
130911
130912
130913/*
130914** Compute an affinity string for a table. Space is obtained
130915** from sqlite3DbMalloc(). The caller is responsible for freeing
130916** the space when done.
130917*/
130918SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
130919 char *zColAff;
130920 zColAff = (char *)sqlite3DbMallocRaw(db, n: pTab->nCol+1);
130921 if( zColAff ){
130922 int i, j;
130923 for(i=j=0; i<pTab->nCol; i++){
130924 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
130925 zColAff[j++] = pTab->aCol[i].affinity;
130926 }
130927 }
130928 do{
130929 zColAff[j--] = 0;
130930 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
130931 }
130932 return zColAff;
130933}
130934
130935/*
130936** Make changes to the evolving bytecode to do affinity transformations
130937** of values that are about to be gathered into a row for table pTab.
130938**
130939** For ordinary (legacy, non-strict) tables:
130940** -----------------------------------------
130941**
130942** Compute the affinity string for table pTab, if it has not already been
130943** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
130944**
130945** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
130946** which were then optimized out) then this routine becomes a no-op.
130947**
130948** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
130949** affinities for register iReg and following. Or if iReg==0,
130950** then just set the P4 operand of the previous opcode (which should be
130951** an OP_MakeRecord) to the affinity string.
130952**
130953** A column affinity string has one character per column:
130954**
130955** Character Column affinity
130956** --------- ---------------
130957** 'A' BLOB
130958** 'B' TEXT
130959** 'C' NUMERIC
130960** 'D' INTEGER
130961** 'E' REAL
130962**
130963** For STRICT tables:
130964** ------------------
130965**
130966** Generate an appropriate OP_TypeCheck opcode that will verify the
130967** datatypes against the column definitions in pTab. If iReg==0, that
130968** means an OP_MakeRecord opcode has already been generated and should be
130969** the last opcode generated. The new OP_TypeCheck needs to be inserted
130970** before the OP_MakeRecord. The new OP_TypeCheck should use the same
130971** register set as the OP_MakeRecord. If iReg>0 then register iReg is
130972** the first of a series of registers that will form the new record.
130973** Apply the type checking to that array of registers.
130974*/
130975SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
130976 int i;
130977 char *zColAff;
130978 if( pTab->tabFlags & TF_Strict ){
130979 if( iReg==0 ){
130980 /* Move the previous opcode (which should be OP_MakeRecord) forward
130981 ** by one slot and insert a new OP_TypeCheck where the current
130982 ** OP_MakeRecord is found */
130983 VdbeOp *pPrev;
130984 sqlite3VdbeAppendP4(p: v, pP4: pTab, P4_TABLE);
130985 pPrev = sqlite3VdbeGetLastOp(p: v);
130986 assert( pPrev!=0 );
130987 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
130988 pPrev->opcode = OP_TypeCheck;
130989 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: pPrev->p1, p2: pPrev->p2, p3: pPrev->p3);
130990 }else{
130991 /* Insert an isolated OP_Typecheck */
130992 sqlite3VdbeAddOp2(p: v, OP_TypeCheck, p1: iReg, p2: pTab->nNVCol);
130993 sqlite3VdbeAppendP4(p: v, pP4: pTab, P4_TABLE);
130994 }
130995 return;
130996 }
130997 zColAff = pTab->zColAff;
130998 if( zColAff==0 ){
130999 zColAff = sqlite3TableAffinityStr(db: 0, pTab);
131000 if( !zColAff ){
131001 sqlite3OomFault(db: sqlite3VdbeDb(v));
131002 return;
131003 }
131004 pTab->zColAff = zColAff;
131005 }
131006 assert( zColAff!=0 );
131007 i = sqlite3Strlen30NN(zColAff);
131008 if( i ){
131009 if( iReg ){
131010 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: iReg, p2: i, p3: 0, zP4: zColAff, p4type: i);
131011 }else{
131012 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
131013 || sqlite3VdbeDb(v)->mallocFailed );
131014 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: zColAff, n: i);
131015 }
131016 }
131017}
131018
131019/*
131020** Return non-zero if the table pTab in database iDb or any of its indices
131021** have been opened at any point in the VDBE program. This is used to see if
131022** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
131023** run without using a temporary table for the results of the SELECT.
131024*/
131025static int readsTable(Parse *p, int iDb, Table *pTab){
131026 Vdbe *v = sqlite3GetVdbe(p);
131027 int i;
131028 int iEnd = sqlite3VdbeCurrentAddr(p: v);
131029#ifndef SQLITE_OMIT_VIRTUALTABLE
131030 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
131031#endif
131032
131033 for(i=1; i<iEnd; i++){
131034 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: i);
131035 assert( pOp!=0 );
131036 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
131037 Index *pIndex;
131038 Pgno tnum = pOp->p2;
131039 if( tnum==pTab->tnum ){
131040 return 1;
131041 }
131042 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
131043 if( tnum==pIndex->tnum ){
131044 return 1;
131045 }
131046 }
131047 }
131048#ifndef SQLITE_OMIT_VIRTUALTABLE
131049 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
131050 assert( pOp->p4.pVtab!=0 );
131051 assert( pOp->p4type==P4_VTAB );
131052 return 1;
131053 }
131054#endif
131055 }
131056 return 0;
131057}
131058
131059/* This walker callback will compute the union of colFlags flags for all
131060** referenced columns in a CHECK constraint or generated column expression.
131061*/
131062static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
131063 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
131064 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
131065 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
131066 }
131067 return WRC_Continue;
131068}
131069
131070#ifndef SQLITE_OMIT_GENERATED_COLUMNS
131071/*
131072** All regular columns for table pTab have been puts into registers
131073** starting with iRegStore. The registers that correspond to STORED
131074** or VIRTUAL columns have not yet been initialized. This routine goes
131075** back and computes the values for those columns based on the previously
131076** computed normal columns.
131077*/
131078SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(
131079 Parse *pParse, /* Parsing context */
131080 int iRegStore, /* Register holding the first column */
131081 Table *pTab /* The table */
131082){
131083 int i;
131084 Walker w;
131085 Column *pRedo;
131086 int eProgress;
131087 VdbeOp *pOp;
131088
131089 assert( pTab->tabFlags & TF_HasGenerated );
131090 testcase( pTab->tabFlags & TF_HasVirtual );
131091 testcase( pTab->tabFlags & TF_HasStored );
131092
131093 /* Before computing generated columns, first go through and make sure
131094 ** that appropriate affinity has been applied to the regular columns
131095 */
131096 sqlite3TableAffinity(v: pParse->pVdbe, pTab, iReg: iRegStore);
131097 if( (pTab->tabFlags & TF_HasStored)!=0 ){
131098 pOp = sqlite3VdbeGetLastOp(p: pParse->pVdbe);
131099 if( pOp->opcode==OP_Affinity ){
131100 /* Change the OP_Affinity argument to '@' (NONE) for all stored
131101 ** columns. '@' is the no-op affinity and those columns have not
131102 ** yet been computed. */
131103 int ii, jj;
131104 char *zP4 = pOp->p4.z;
131105 assert( zP4!=0 );
131106 assert( pOp->p4type==P4_DYNAMIC );
131107 for(ii=jj=0; zP4[jj]; ii++){
131108 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
131109 continue;
131110 }
131111 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
131112 zP4[jj] = SQLITE_AFF_NONE;
131113 }
131114 jj++;
131115 }
131116 }else if( pOp->opcode==OP_TypeCheck ){
131117 /* If an OP_TypeCheck was generated because the table is STRICT,
131118 ** then set the P3 operand to indicate that generated columns should
131119 ** not be checked */
131120 pOp->p3 = 1;
131121 }
131122 }
131123
131124 /* Because there can be multiple generated columns that refer to one another,
131125 ** this is a two-pass algorithm. On the first pass, mark all generated
131126 ** columns as "not available".
131127 */
131128 for(i=0; i<pTab->nCol; i++){
131129 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
131130 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
131131 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
131132 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
131133 }
131134 }
131135
131136 w.u.pTab = pTab;
131137 w.xExprCallback = exprColumnFlagUnion;
131138 w.xSelectCallback = 0;
131139 w.xSelectCallback2 = 0;
131140
131141 /* On the second pass, compute the value of each NOT-AVAILABLE column.
131142 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
131143 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
131144 ** they are needed.
131145 */
131146 pParse->iSelfTab = -iRegStore;
131147 do{
131148 eProgress = 0;
131149 pRedo = 0;
131150 for(i=0; i<pTab->nCol; i++){
131151 Column *pCol = pTab->aCol + i;
131152 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
131153 int x;
131154 pCol->colFlags |= COLFLAG_BUSY;
131155 w.eCode = 0;
131156 sqlite3WalkExpr(pWalker: &w, pExpr: sqlite3ColumnExpr(pTab, pCol));
131157 pCol->colFlags &= ~COLFLAG_BUSY;
131158 if( w.eCode & COLFLAG_NOTAVAIL ){
131159 pRedo = pCol;
131160 continue;
131161 }
131162 eProgress = 1;
131163 assert( pCol->colFlags & COLFLAG_GENERATED );
131164 x = sqlite3TableColumnToStorage(pTab, iCol: i) + iRegStore;
131165 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut: x);
131166 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
131167 }
131168 }
131169 }while( pRedo && eProgress );
131170 if( pRedo ){
131171 sqlite3ErrorMsg(pParse, zFormat: "generated column loop on \"%s\"", pRedo->zCnName);
131172 }
131173 pParse->iSelfTab = 0;
131174}
131175#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
131176
131177
131178#ifndef SQLITE_OMIT_AUTOINCREMENT
131179/*
131180** Locate or create an AutoincInfo structure associated with table pTab
131181** which is in database iDb. Return the register number for the register
131182** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
131183** table. (Also return zero when doing a VACUUM since we do not want to
131184** update the AUTOINCREMENT counters during a VACUUM.)
131185**
131186** There is at most one AutoincInfo structure per table even if the
131187** same table is autoincremented multiple times due to inserts within
131188** triggers. A new AutoincInfo structure is created if this is the
131189** first use of table pTab. On 2nd and subsequent uses, the original
131190** AutoincInfo structure is used.
131191**
131192** Four consecutive registers are allocated:
131193**
131194** (1) The name of the pTab table.
131195** (2) The maximum ROWID of pTab.
131196** (3) The rowid in sqlite_sequence of pTab
131197** (4) The original value of the max ROWID in pTab, or NULL if none
131198**
131199** The 2nd register is the one that is returned. That is all the
131200** insert routine needs to know about.
131201*/
131202static int autoIncBegin(
131203 Parse *pParse, /* Parsing context */
131204 int iDb, /* Index of the database holding pTab */
131205 Table *pTab /* The table we are writing to */
131206){
131207 int memId = 0; /* Register holding maximum rowid */
131208 assert( pParse->db->aDb[iDb].pSchema!=0 );
131209 if( (pTab->tabFlags & TF_Autoincrement)!=0
131210 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
131211 ){
131212 Parse *pToplevel = sqlite3ParseToplevel(pParse);
131213 AutoincInfo *pInfo;
131214 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
131215
131216 /* Verify that the sqlite_sequence table exists and is an ordinary
131217 ** rowid table with exactly two columns.
131218 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
131219 if( pSeqTab==0
131220 || !HasRowid(pSeqTab)
131221 || NEVER(IsVirtual(pSeqTab))
131222 || pSeqTab->nCol!=2
131223 ){
131224 pParse->nErr++;
131225 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
131226 return 0;
131227 }
131228
131229 pInfo = pToplevel->pAinc;
131230 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
131231 if( pInfo==0 ){
131232 pInfo = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(*pInfo));
131233 sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
131234 testcase( pParse->earlyCleanup );
131235 if( pParse->db->mallocFailed ) return 0;
131236 pInfo->pNext = pToplevel->pAinc;
131237 pToplevel->pAinc = pInfo;
131238 pInfo->pTab = pTab;
131239 pInfo->iDb = iDb;
131240 pToplevel->nMem++; /* Register to hold name of table */
131241 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
131242 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
131243 }
131244 memId = pInfo->regCtr;
131245 }
131246 return memId;
131247}
131248
131249/*
131250** This routine generates code that will initialize all of the
131251** register used by the autoincrement tracker.
131252*/
131253SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
131254 AutoincInfo *p; /* Information about an AUTOINCREMENT */
131255 sqlite3 *db = pParse->db; /* The database connection */
131256 Db *pDb; /* Database only autoinc table */
131257 int memId; /* Register holding max rowid */
131258 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
131259
131260 /* This routine is never called during trigger-generation. It is
131261 ** only called from the top-level */
131262 assert( pParse->pTriggerTab==0 );
131263 assert( sqlite3IsToplevel(pParse) );
131264
131265 assert( v ); /* We failed long ago if this is not so */
131266 for(p = pParse->pAinc; p; p = p->pNext){
131267 static const int iLn = VDBE_OFFSET_LINENO(2);
131268 static const VdbeOpList autoInc[] = {
131269 /* 0 */ {OP_Null, 0, 0, 0},
131270 /* 1 */ {OP_Rewind, 0, 10, 0},
131271 /* 2 */ {OP_Column, 0, 0, 0},
131272 /* 3 */ {OP_Ne, 0, 9, 0},
131273 /* 4 */ {OP_Rowid, 0, 0, 0},
131274 /* 5 */ {OP_Column, 0, 1, 0},
131275 /* 6 */ {OP_AddImm, 0, 0, 0},
131276 /* 7 */ {OP_Copy, 0, 0, 0},
131277 /* 8 */ {OP_Goto, 0, 11, 0},
131278 /* 9 */ {OP_Next, 0, 2, 0},
131279 /* 10 */ {OP_Integer, 0, 0, 0},
131280 /* 11 */ {OP_Close, 0, 0, 0}
131281 };
131282 VdbeOp *aOp;
131283 pDb = &db->aDb[p->iDb];
131284 memId = p->regCtr;
131285 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
131286 sqlite3OpenTable(pParse, iCur: 0, iDb: p->iDb, pTab: pDb->pSchema->pSeqTab, OP_OpenRead);
131287 sqlite3VdbeLoadString(p: v, iDest: memId-1, zStr: p->pTab->zName);
131288 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(autoInc), aOp: autoInc, iLineno: iLn);
131289 if( aOp==0 ) break;
131290 aOp[0].p2 = memId;
131291 aOp[0].p3 = memId+2;
131292 aOp[2].p3 = memId;
131293 aOp[3].p1 = memId-1;
131294 aOp[3].p3 = memId;
131295 aOp[3].p5 = SQLITE_JUMPIFNULL;
131296 aOp[4].p2 = memId+1;
131297 aOp[5].p3 = memId;
131298 aOp[6].p1 = memId;
131299 aOp[7].p2 = memId+2;
131300 aOp[7].p1 = memId;
131301 aOp[10].p2 = memId;
131302 if( pParse->nTab==0 ) pParse->nTab = 1;
131303 }
131304}
131305
131306/*
131307** Update the maximum rowid for an autoincrement calculation.
131308**
131309** This routine should be called when the regRowid register holds a
131310** new rowid that is about to be inserted. If that new rowid is
131311** larger than the maximum rowid in the memId memory cell, then the
131312** memory cell is updated.
131313*/
131314static void autoIncStep(Parse *pParse, int memId, int regRowid){
131315 if( memId>0 ){
131316 sqlite3VdbeAddOp2(p: pParse->pVdbe, OP_MemMax, p1: memId, p2: regRowid);
131317 }
131318}
131319
131320/*
131321** This routine generates the code needed to write autoincrement
131322** maximum rowid values back into the sqlite_sequence register.
131323** Every statement that might do an INSERT into an autoincrement
131324** table (either directly or through triggers) needs to call this
131325** routine just before the "exit" code.
131326*/
131327static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
131328 AutoincInfo *p;
131329 Vdbe *v = pParse->pVdbe;
131330 sqlite3 *db = pParse->db;
131331
131332 assert( v );
131333 for(p = pParse->pAinc; p; p = p->pNext){
131334 static const int iLn = VDBE_OFFSET_LINENO(2);
131335 static const VdbeOpList autoIncEnd[] = {
131336 /* 0 */ {OP_NotNull, 0, 2, 0},
131337 /* 1 */ {OP_NewRowid, 0, 0, 0},
131338 /* 2 */ {OP_MakeRecord, 0, 2, 0},
131339 /* 3 */ {OP_Insert, 0, 0, 0},
131340 /* 4 */ {OP_Close, 0, 0, 0}
131341 };
131342 VdbeOp *aOp;
131343 Db *pDb = &db->aDb[p->iDb];
131344 int iRec;
131345 int memId = p->regCtr;
131346
131347 iRec = sqlite3GetTempReg(pParse);
131348 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
131349 sqlite3VdbeAddOp3(p: v, OP_Le, p1: memId+2, p2: sqlite3VdbeCurrentAddr(p: v)+7, p3: memId);
131350 VdbeCoverage(v);
131351 sqlite3OpenTable(pParse, iCur: 0, iDb: p->iDb, pTab: pDb->pSchema->pSeqTab, OP_OpenWrite);
131352 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(autoIncEnd), aOp: autoIncEnd, iLineno: iLn);
131353 if( aOp==0 ) break;
131354 aOp[0].p1 = memId+1;
131355 aOp[1].p2 = memId+1;
131356 aOp[2].p1 = memId-1;
131357 aOp[2].p3 = iRec;
131358 aOp[3].p2 = iRec;
131359 aOp[3].p3 = memId+1;
131360 aOp[3].p5 = OPFLAG_APPEND;
131361 sqlite3ReleaseTempReg(pParse, iReg: iRec);
131362 }
131363}
131364SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
131365 if( pParse->pAinc ) autoIncrementEnd(pParse);
131366}
131367#else
131368/*
131369** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
131370** above are all no-ops
131371*/
131372# define autoIncBegin(A,B,C) (0)
131373# define autoIncStep(A,B,C)
131374#endif /* SQLITE_OMIT_AUTOINCREMENT */
131375
131376
131377/* Forward declaration */
131378static int xferOptimization(
131379 Parse *pParse, /* Parser context */
131380 Table *pDest, /* The table we are inserting into */
131381 Select *pSelect, /* A SELECT statement to use as the data source */
131382 int onError, /* How to handle constraint errors */
131383 int iDbDest /* The database of pDest */
131384);
131385
131386/*
131387** This routine is called to handle SQL of the following forms:
131388**
131389** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
131390** insert into TABLE (IDLIST) select
131391** insert into TABLE (IDLIST) default values
131392**
131393** The IDLIST following the table name is always optional. If omitted,
131394** then a list of all (non-hidden) columns for the table is substituted.
131395** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
131396** is omitted.
131397**
131398** For the pSelect parameter holds the values to be inserted for the
131399** first two forms shown above. A VALUES clause is really just short-hand
131400** for a SELECT statement that omits the FROM clause and everything else
131401** that follows. If the pSelect parameter is NULL, that means that the
131402** DEFAULT VALUES form of the INSERT statement is intended.
131403**
131404** The code generated follows one of four templates. For a simple
131405** insert with data coming from a single-row VALUES clause, the code executes
131406** once straight down through. Pseudo-code follows (we call this
131407** the "1st template"):
131408**
131409** open write cursor to <table> and its indices
131410** put VALUES clause expressions into registers
131411** write the resulting record into <table>
131412** cleanup
131413**
131414** The three remaining templates assume the statement is of the form
131415**
131416** INSERT INTO <table> SELECT ...
131417**
131418** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
131419** in other words if the SELECT pulls all columns from a single table
131420** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
131421** if <table2> and <table1> are distinct tables but have identical
131422** schemas, including all the same indices, then a special optimization
131423** is invoked that copies raw records from <table2> over to <table1>.
131424** See the xferOptimization() function for the implementation of this
131425** template. This is the 2nd template.
131426**
131427** open a write cursor to <table>
131428** open read cursor on <table2>
131429** transfer all records in <table2> over to <table>
131430** close cursors
131431** foreach index on <table>
131432** open a write cursor on the <table> index
131433** open a read cursor on the corresponding <table2> index
131434** transfer all records from the read to the write cursors
131435** close cursors
131436** end foreach
131437**
131438** The 3rd template is for when the second template does not apply
131439** and the SELECT clause does not read from <table> at any time.
131440** The generated code follows this template:
131441**
131442** X <- A
131443** goto B
131444** A: setup for the SELECT
131445** loop over the rows in the SELECT
131446** load values into registers R..R+n
131447** yield X
131448** end loop
131449** cleanup after the SELECT
131450** end-coroutine X
131451** B: open write cursor to <table> and its indices
131452** C: yield X, at EOF goto D
131453** insert the select result into <table> from R..R+n
131454** goto C
131455** D: cleanup
131456**
131457** The 4th template is used if the insert statement takes its
131458** values from a SELECT but the data is being inserted into a table
131459** that is also read as part of the SELECT. In the third form,
131460** we have to use an intermediate table to store the results of
131461** the select. The template is like this:
131462**
131463** X <- A
131464** goto B
131465** A: setup for the SELECT
131466** loop over the tables in the SELECT
131467** load value into register R..R+n
131468** yield X
131469** end loop
131470** cleanup after the SELECT
131471** end co-routine R
131472** B: open temp table
131473** L: yield X, at EOF goto M
131474** insert row from R..R+n into temp table
131475** goto L
131476** M: open write cursor to <table> and its indices
131477** rewind temp table
131478** C: loop over rows of intermediate table
131479** transfer values form intermediate table into <table>
131480** end loop
131481** D: cleanup
131482*/
131483SQLITE_PRIVATE void sqlite3Insert(
131484 Parse *pParse, /* Parser context */
131485 SrcList *pTabList, /* Name of table into which we are inserting */
131486 Select *pSelect, /* A SELECT statement to use as the data source */
131487 IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
131488 int onError, /* How to handle constraint errors */
131489 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
131490){
131491 sqlite3 *db; /* The main database structure */
131492 Table *pTab; /* The table to insert into. aka TABLE */
131493 int i, j; /* Loop counters */
131494 Vdbe *v; /* Generate code into this virtual machine */
131495 Index *pIdx; /* For looping over indices of the table */
131496 int nColumn; /* Number of columns in the data */
131497 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
131498 int iDataCur = 0; /* VDBE cursor that is the main data repository */
131499 int iIdxCur = 0; /* First index cursor */
131500 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
131501 int endOfLoop; /* Label for the end of the insertion loop */
131502 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
131503 int addrInsTop = 0; /* Jump to label "D" */
131504 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
131505 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
131506 int iDb; /* Index of database holding TABLE */
131507 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
131508 u8 appendFlag = 0; /* True if the insert is likely to be an append */
131509 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
131510 u8 bIdListInOrder; /* True if IDLIST is in table order */
131511 ExprList *pList = 0; /* List of VALUES() to be inserted */
131512 int iRegStore; /* Register in which to store next column */
131513
131514 /* Register allocations */
131515 int regFromSelect = 0;/* Base register for data coming from SELECT */
131516 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
131517 int regRowCount = 0; /* Memory cell used for the row counter */
131518 int regIns; /* Block of regs holding rowid+data being inserted */
131519 int regRowid; /* registers holding insert rowid */
131520 int regData; /* register holding first column to insert */
131521 int *aRegIdx = 0; /* One register allocated to each index */
131522
131523#ifndef SQLITE_OMIT_TRIGGER
131524 int isView; /* True if attempting to insert into a view */
131525 Trigger *pTrigger; /* List of triggers on pTab, if required */
131526 int tmask; /* Mask of trigger times */
131527#endif
131528
131529 db = pParse->db;
131530 assert( db->pParse==pParse );
131531 if( pParse->nErr ){
131532 goto insert_cleanup;
131533 }
131534 assert( db->mallocFailed==0 );
131535 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
131536
131537 /* If the Select object is really just a simple VALUES() list with a
131538 ** single row (the common case) then keep that one row of values
131539 ** and discard the other (unused) parts of the pSelect object
131540 */
131541 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
131542 pList = pSelect->pEList;
131543 pSelect->pEList = 0;
131544 sqlite3SelectDelete(db, pSelect);
131545 pSelect = 0;
131546 }
131547
131548 /* Locate the table into which we will be inserting new information.
131549 */
131550 assert( pTabList->nSrc==1 );
131551 pTab = sqlite3SrcListLookup(pParse, pSrc: pTabList);
131552 if( pTab==0 ){
131553 goto insert_cleanup;
131554 }
131555 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
131556 assert( iDb<db->nDb );
131557 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, zArg1: pTab->zName, zArg2: 0,
131558 zArg3: db->aDb[iDb].zDbSName) ){
131559 goto insert_cleanup;
131560 }
131561 withoutRowid = !HasRowid(pTab);
131562
131563 /* Figure out if we have any triggers and if the table being
131564 ** inserted into is a view
131565 */
131566#ifndef SQLITE_OMIT_TRIGGER
131567 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, pMask: &tmask);
131568 isView = IsView(pTab);
131569#else
131570# define pTrigger 0
131571# define tmask 0
131572# define isView 0
131573#endif
131574#ifdef SQLITE_OMIT_VIEW
131575# undef isView
131576# define isView 0
131577#endif
131578 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
131579
131580#if TREETRACE_ENABLED
131581 if( sqlite3TreeTrace & 0x10000 ){
131582 sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__, __LINE__);
131583 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
131584 onError, pUpsert, pTrigger);
131585 }
131586#endif
131587
131588 /* If pTab is really a view, make sure it has been initialized.
131589 ** ViewGetColumnNames() is a no-op if pTab is not a view.
131590 */
131591 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
131592 goto insert_cleanup;
131593 }
131594
131595 /* Cannot insert into a read-only table.
131596 */
131597 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
131598 goto insert_cleanup;
131599 }
131600
131601 /* Allocate a VDBE
131602 */
131603 v = sqlite3GetVdbe(pParse);
131604 if( v==0 ) goto insert_cleanup;
131605 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
131606 sqlite3BeginWriteOperation(pParse, setStatement: pSelect || pTrigger, iDb);
131607
131608#ifndef SQLITE_OMIT_XFER_OPT
131609 /* If the statement is of the form
131610 **
131611 ** INSERT INTO <table1> SELECT * FROM <table2>;
131612 **
131613 ** Then special optimizations can be applied that make the transfer
131614 ** very fast and which reduce fragmentation of indices.
131615 **
131616 ** This is the 2nd template.
131617 */
131618 if( pColumn==0
131619 && pSelect!=0
131620 && pTrigger==0
131621 && xferOptimization(pParse, pDest: pTab, pSelect, onError, iDbDest: iDb)
131622 ){
131623 assert( !pTrigger );
131624 assert( pList==0 );
131625 goto insert_end;
131626 }
131627#endif /* SQLITE_OMIT_XFER_OPT */
131628
131629 /* If this is an AUTOINCREMENT table, look up the sequence number in the
131630 ** sqlite_sequence table and store it in memory cell regAutoinc.
131631 */
131632 regAutoinc = autoIncBegin(pParse, iDb, pTab);
131633
131634 /* Allocate a block registers to hold the rowid and the values
131635 ** for all columns of the new row.
131636 */
131637 regRowid = regIns = pParse->nMem+1;
131638 pParse->nMem += pTab->nCol + 1;
131639 if( IsVirtual(pTab) ){
131640 regRowid++;
131641 pParse->nMem++;
131642 }
131643 regData = regRowid+1;
131644
131645 /* If the INSERT statement included an IDLIST term, then make sure
131646 ** all elements of the IDLIST really are columns of the table and
131647 ** remember the column indices.
131648 **
131649 ** If the table has an INTEGER PRIMARY KEY column and that column
131650 ** is named in the IDLIST, then record in the ipkColumn variable
131651 ** the index into IDLIST of the primary key column. ipkColumn is
131652 ** the index of the primary key as it appears in IDLIST, not as
131653 ** is appears in the original table. (The index of the INTEGER
131654 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
131655 ** loop, if ipkColumn==(-1), that means that integer primary key
131656 ** is unspecified, and hence the table is either WITHOUT ROWID or
131657 ** it will automatically generated an integer primary key.
131658 **
131659 ** bIdListInOrder is true if the columns in IDLIST are in storage
131660 ** order. This enables an optimization that avoids shuffling the
131661 ** columns into storage order. False negatives are harmless,
131662 ** but false positives will cause database corruption.
131663 */
131664 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
131665 if( pColumn ){
131666 assert( pColumn->eU4!=EU4_EXPR );
131667 pColumn->eU4 = EU4_IDX;
131668 for(i=0; i<pColumn->nId; i++){
131669 pColumn->a[i].u4.idx = -1;
131670 }
131671 for(i=0; i<pColumn->nId; i++){
131672 for(j=0; j<pTab->nCol; j++){
131673 if( sqlite3StrICmp(zLeft: pColumn->a[i].zName, zRight: pTab->aCol[j].zCnName)==0 ){
131674 pColumn->a[i].u4.idx = j;
131675 if( i!=j ) bIdListInOrder = 0;
131676 if( j==pTab->iPKey ){
131677 ipkColumn = i; assert( !withoutRowid );
131678 }
131679#ifndef SQLITE_OMIT_GENERATED_COLUMNS
131680 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
131681 sqlite3ErrorMsg(pParse,
131682 zFormat: "cannot INSERT into generated column \"%s\"",
131683 pTab->aCol[j].zCnName);
131684 goto insert_cleanup;
131685 }
131686#endif
131687 break;
131688 }
131689 }
131690 if( j>=pTab->nCol ){
131691 if( sqlite3IsRowid(z: pColumn->a[i].zName) && !withoutRowid ){
131692 ipkColumn = i;
131693 bIdListInOrder = 0;
131694 }else{
131695 sqlite3ErrorMsg(pParse, zFormat: "table %S has no column named %s",
131696 pTabList->a, pColumn->a[i].zName);
131697 pParse->checkSchema = 1;
131698 goto insert_cleanup;
131699 }
131700 }
131701 }
131702 }
131703
131704 /* Figure out how many columns of data are supplied. If the data
131705 ** is coming from a SELECT statement, then generate a co-routine that
131706 ** produces a single row of the SELECT on each invocation. The
131707 ** co-routine is the common header to the 3rd and 4th templates.
131708 */
131709 if( pSelect ){
131710 /* Data is coming from a SELECT or from a multi-row VALUES clause.
131711 ** Generate a co-routine to run the SELECT. */
131712 int regYield; /* Register holding co-routine entry-point */
131713 int addrTop; /* Top of the co-routine */
131714 int rc; /* Result code */
131715
131716 regYield = ++pParse->nMem;
131717 addrTop = sqlite3VdbeCurrentAddr(p: v) + 1;
131718 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: addrTop);
131719 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
131720 dest.iSdst = bIdListInOrder ? regData : 0;
131721 dest.nSdst = pTab->nCol;
131722 rc = sqlite3Select(pParse, pSelect, &dest);
131723 regFromSelect = dest.iSdst;
131724 assert( db->pParse==pParse );
131725 if( rc || pParse->nErr ) goto insert_cleanup;
131726 assert( db->mallocFailed==0 );
131727 sqlite3VdbeEndCoroutine(v, regYield);
131728 sqlite3VdbeJumpHere(p: v, addr: addrTop - 1); /* label B: */
131729 assert( pSelect->pEList );
131730 nColumn = pSelect->pEList->nExpr;
131731
131732 /* Set useTempTable to TRUE if the result of the SELECT statement
131733 ** should be written into a temporary table (template 4). Set to
131734 ** FALSE if each output row of the SELECT can be written directly into
131735 ** the destination table (template 3).
131736 **
131737 ** A temp table must be used if the table being updated is also one
131738 ** of the tables being read by the SELECT statement. Also use a
131739 ** temp table in the case of row triggers.
131740 */
131741 if( pTrigger || readsTable(p: pParse, iDb, pTab) ){
131742 useTempTable = 1;
131743 }
131744
131745 if( useTempTable ){
131746 /* Invoke the coroutine to extract information from the SELECT
131747 ** and add it to a transient table srcTab. The code generated
131748 ** here is from the 4th template:
131749 **
131750 ** B: open temp table
131751 ** L: yield X, goto M at EOF
131752 ** insert row from R..R+n into temp table
131753 ** goto L
131754 ** M: ...
131755 */
131756 int regRec; /* Register to hold packed record */
131757 int regTempRowid; /* Register to hold temp table ROWID */
131758 int addrL; /* Label "L" */
131759
131760 srcTab = pParse->nTab++;
131761 regRec = sqlite3GetTempReg(pParse);
131762 regTempRowid = sqlite3GetTempReg(pParse);
131763 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: srcTab, p2: nColumn);
131764 addrL = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: dest.iSDParm); VdbeCoverage(v);
131765 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regFromSelect, p2: nColumn, p3: regRec);
131766 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: srcTab, p2: regTempRowid);
131767 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: srcTab, p2: regRec, p3: regTempRowid);
131768 sqlite3VdbeGoto(p: v, iDest: addrL);
131769 sqlite3VdbeJumpHere(p: v, addr: addrL);
131770 sqlite3ReleaseTempReg(pParse, iReg: regRec);
131771 sqlite3ReleaseTempReg(pParse, iReg: regTempRowid);
131772 }
131773 }else{
131774 /* This is the case if the data for the INSERT is coming from a
131775 ** single-row VALUES clause
131776 */
131777 NameContext sNC;
131778 memset(s: &sNC, c: 0, n: sizeof(sNC));
131779 sNC.pParse = pParse;
131780 srcTab = -1;
131781 assert( useTempTable==0 );
131782 if( pList ){
131783 nColumn = pList->nExpr;
131784 if( sqlite3ResolveExprListNames(pNC: &sNC, pList) ){
131785 goto insert_cleanup;
131786 }
131787 }else{
131788 nColumn = 0;
131789 }
131790 }
131791
131792 /* If there is no IDLIST term but the table has an integer primary
131793 ** key, the set the ipkColumn variable to the integer primary key
131794 ** column index in the original table definition.
131795 */
131796 if( pColumn==0 && nColumn>0 ){
131797 ipkColumn = pTab->iPKey;
131798#ifndef SQLITE_OMIT_GENERATED_COLUMNS
131799 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
131800 testcase( pTab->tabFlags & TF_HasVirtual );
131801 testcase( pTab->tabFlags & TF_HasStored );
131802 for(i=ipkColumn-1; i>=0; i--){
131803 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
131804 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
131805 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
131806 ipkColumn--;
131807 }
131808 }
131809 }
131810#endif
131811
131812 /* Make sure the number of columns in the source data matches the number
131813 ** of columns to be inserted into the table.
131814 */
131815 assert( TF_HasHidden==COLFLAG_HIDDEN );
131816 assert( TF_HasGenerated==COLFLAG_GENERATED );
131817 assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) );
131818 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
131819 for(i=0; i<pTab->nCol; i++){
131820 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
131821 }
131822 }
131823 if( nColumn!=(pTab->nCol-nHidden) ){
131824 sqlite3ErrorMsg(pParse,
131825 zFormat: "table %S has %d columns but %d values were supplied",
131826 pTabList->a, pTab->nCol-nHidden, nColumn);
131827 goto insert_cleanup;
131828 }
131829 }
131830 if( pColumn!=0 && nColumn!=pColumn->nId ){
131831 sqlite3ErrorMsg(pParse, zFormat: "%d values for %d columns", nColumn, pColumn->nId);
131832 goto insert_cleanup;
131833 }
131834
131835 /* Initialize the count of rows to be inserted
131836 */
131837 if( (db->flags & SQLITE_CountRows)!=0
131838 && !pParse->nested
131839 && !pParse->pTriggerTab
131840 && !pParse->bReturning
131841 ){
131842 regRowCount = ++pParse->nMem;
131843 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regRowCount);
131844 }
131845
131846 /* If this is not a view, open the table and and all indices */
131847 if( !isView ){
131848 int nIdx;
131849 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
131850 &iDataCur, &iIdxCur);
131851 aRegIdx = sqlite3DbMallocRawNN(db, n: sizeof(int)*(nIdx+2));
131852 if( aRegIdx==0 ){
131853 goto insert_cleanup;
131854 }
131855 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
131856 assert( pIdx );
131857 aRegIdx[i] = ++pParse->nMem;
131858 pParse->nMem += pIdx->nColumn;
131859 }
131860 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
131861 }
131862#ifndef SQLITE_OMIT_UPSERT
131863 if( pUpsert ){
131864 Upsert *pNx;
131865 if( IsVirtual(pTab) ){
131866 sqlite3ErrorMsg(pParse, zFormat: "UPSERT not implemented for virtual table \"%s\"",
131867 pTab->zName);
131868 goto insert_cleanup;
131869 }
131870 if( IsView(pTab) ){
131871 sqlite3ErrorMsg(pParse, zFormat: "cannot UPSERT a view");
131872 goto insert_cleanup;
131873 }
131874 if( sqlite3HasExplicitNulls(pParse, pList: pUpsert->pUpsertTarget) ){
131875 goto insert_cleanup;
131876 }
131877 pTabList->a[0].iCursor = iDataCur;
131878 pNx = pUpsert;
131879 do{
131880 pNx->pUpsertSrc = pTabList;
131881 pNx->regData = regData;
131882 pNx->iDataCur = iDataCur;
131883 pNx->iIdxCur = iIdxCur;
131884 if( pNx->pUpsertTarget ){
131885 if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx) ){
131886 goto insert_cleanup;
131887 }
131888 }
131889 pNx = pNx->pNextUpsert;
131890 }while( pNx!=0 );
131891 }
131892#endif
131893
131894
131895 /* This is the top of the main insertion loop */
131896 if( useTempTable ){
131897 /* This block codes the top of loop only. The complete loop is the
131898 ** following pseudocode (template 4):
131899 **
131900 ** rewind temp table, if empty goto D
131901 ** C: loop over rows of intermediate table
131902 ** transfer values form intermediate table into <table>
131903 ** end loop
131904 ** D: ...
131905 */
131906 addrInsTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: srcTab); VdbeCoverage(v);
131907 addrCont = sqlite3VdbeCurrentAddr(p: v);
131908 }else if( pSelect ){
131909 /* This block codes the top of loop only. The complete loop is the
131910 ** following pseudocode (template 3):
131911 **
131912 ** C: yield X, at EOF goto D
131913 ** insert the select result into <table> from R..R+n
131914 ** goto C
131915 ** D: ...
131916 */
131917 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
131918 addrInsTop = addrCont = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: dest.iSDParm);
131919 VdbeCoverage(v);
131920 if( ipkColumn>=0 ){
131921 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
131922 ** SELECT, go ahead and copy the value into the rowid slot now, so that
131923 ** the value does not get overwritten by a NULL at tag-20191021-002. */
131924 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regFromSelect+ipkColumn, p2: regRowid);
131925 }
131926 }
131927
131928 /* Compute data for ordinary columns of the new entry. Values
131929 ** are written in storage order into registers starting with regData.
131930 ** Only ordinary columns are computed in this loop. The rowid
131931 ** (if there is one) is computed later and generated columns are
131932 ** computed after the rowid since they might depend on the value
131933 ** of the rowid.
131934 */
131935 nHidden = 0;
131936 iRegStore = regData; assert( regData==regRowid+1 );
131937 for(i=0; i<pTab->nCol; i++, iRegStore++){
131938 int k;
131939 u32 colFlags;
131940 assert( i>=nHidden );
131941 if( i==pTab->iPKey ){
131942 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
131943 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
131944 ** using excess space. The file format definition requires this extra
131945 ** NULL - we cannot optimize further by skipping the column completely */
131946 sqlite3VdbeAddOp1(p: v, OP_SoftNull, p1: iRegStore);
131947 continue;
131948 }
131949 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
131950 nHidden++;
131951 if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
131952 /* Virtual columns do not participate in OP_MakeRecord. So back up
131953 ** iRegStore by one slot to compensate for the iRegStore++ in the
131954 ** outer for() loop */
131955 iRegStore--;
131956 continue;
131957 }else if( (colFlags & COLFLAG_STORED)!=0 ){
131958 /* Stored columns are computed later. But if there are BEFORE
131959 ** triggers, the slots used for stored columns will be OP_Copy-ed
131960 ** to a second block of registers, so the register needs to be
131961 ** initialized to NULL to avoid an uninitialized register read */
131962 if( tmask & TRIGGER_BEFORE ){
131963 sqlite3VdbeAddOp1(p: v, OP_SoftNull, p1: iRegStore);
131964 }
131965 continue;
131966 }else if( pColumn==0 ){
131967 /* Hidden columns that are not explicitly named in the INSERT
131968 ** get there default value */
131969 sqlite3ExprCodeFactorable(pParse,
131970 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
131971 target: iRegStore);
131972 continue;
131973 }
131974 }
131975 if( pColumn ){
131976 assert( pColumn->eU4==EU4_IDX );
131977 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
131978 if( j>=pColumn->nId ){
131979 /* A column not named in the insert column list gets its
131980 ** default value */
131981 sqlite3ExprCodeFactorable(pParse,
131982 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
131983 target: iRegStore);
131984 continue;
131985 }
131986 k = j;
131987 }else if( nColumn==0 ){
131988 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
131989 sqlite3ExprCodeFactorable(pParse,
131990 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
131991 target: iRegStore);
131992 continue;
131993 }else{
131994 k = i - nHidden;
131995 }
131996
131997 if( useTempTable ){
131998 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: k, p3: iRegStore);
131999 }else if( pSelect ){
132000 if( regFromSelect!=regData ){
132001 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: regFromSelect+k, p2: iRegStore);
132002 }
132003 }else{
132004 Expr *pX = pList->a[k].pExpr;
132005 int y = sqlite3ExprCodeTarget(pParse, pExpr: pX, target: iRegStore);
132006 if( y!=iRegStore ){
132007 sqlite3VdbeAddOp2(p: v,
132008 ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, p1: y, p2: iRegStore);
132009 }
132010 }
132011 }
132012
132013
132014 /* Run the BEFORE and INSTEAD OF triggers, if there are any
132015 */
132016 endOfLoop = sqlite3VdbeMakeLabel(pParse);
132017 if( tmask & TRIGGER_BEFORE ){
132018 int regCols = sqlite3GetTempRange(pParse, nReg: pTab->nCol+1);
132019
132020 /* build the NEW.* reference row. Note that if there is an INTEGER
132021 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
132022 ** translated into a unique ID for the row. But on a BEFORE trigger,
132023 ** we do not know what the unique ID will be (because the insert has
132024 ** not happened yet) so we substitute a rowid of -1
132025 */
132026 if( ipkColumn<0 ){
132027 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: -1, p2: regCols);
132028 }else{
132029 int addr1;
132030 assert( !withoutRowid );
132031 if( useTempTable ){
132032 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: ipkColumn, p3: regCols);
132033 }else{
132034 assert( pSelect==0 ); /* Otherwise useTempTable is true */
132035 sqlite3ExprCode(pParse, pExpr: pList->a[ipkColumn].pExpr, target: regCols);
132036 }
132037 addr1 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: regCols); VdbeCoverage(v);
132038 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: -1, p2: regCols);
132039 sqlite3VdbeJumpHere(p: v, addr: addr1);
132040 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: regCols); VdbeCoverage(v);
132041 }
132042
132043 /* Copy the new data already generated. */
132044 assert( pTab->nNVCol>0 || pParse->nErr>0 );
132045 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regRowid+1, p2: regCols+1, p3: pTab->nNVCol-1);
132046
132047#ifndef SQLITE_OMIT_GENERATED_COLUMNS
132048 /* Compute the new value for generated columns after all other
132049 ** columns have already been computed. This must be done after
132050 ** computing the ROWID in case one of the generated columns
132051 ** refers to the ROWID. */
132052 if( pTab->tabFlags & TF_HasGenerated ){
132053 testcase( pTab->tabFlags & TF_HasVirtual );
132054 testcase( pTab->tabFlags & TF_HasStored );
132055 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regCols+1, pTab);
132056 }
132057#endif
132058
132059 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
132060 ** do not attempt any conversions before assembling the record.
132061 ** If this is a real table, attempt conversions as required by the
132062 ** table column affinities.
132063 */
132064 if( !isView ){
132065 sqlite3TableAffinity(v, pTab, iReg: regCols+1);
132066 }
132067
132068 /* Fire BEFORE or INSTEAD OF triggers */
132069 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
132070 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
132071
132072 sqlite3ReleaseTempRange(pParse, iReg: regCols, nReg: pTab->nCol+1);
132073 }
132074
132075 if( !isView ){
132076 if( IsVirtual(pTab) ){
132077 /* The row that the VUpdate opcode will delete: none */
132078 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regIns);
132079 }
132080 if( ipkColumn>=0 ){
132081 /* Compute the new rowid */
132082 if( useTempTable ){
132083 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: ipkColumn, p3: regRowid);
132084 }else if( pSelect ){
132085 /* Rowid already initialized at tag-20191021-001 */
132086 }else{
132087 Expr *pIpk = pList->a[ipkColumn].pExpr;
132088 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
132089 sqlite3VdbeAddOp3(p: v, OP_NewRowid, p1: iDataCur, p2: regRowid, p3: regAutoinc);
132090 appendFlag = 1;
132091 }else{
132092 sqlite3ExprCode(pParse, pExpr: pList->a[ipkColumn].pExpr, target: regRowid);
132093 }
132094 }
132095 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
132096 ** to generate a unique primary key value.
132097 */
132098 if( !appendFlag ){
132099 int addr1;
132100 if( !IsVirtual(pTab) ){
132101 addr1 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: regRowid); VdbeCoverage(v);
132102 sqlite3VdbeAddOp3(p: v, OP_NewRowid, p1: iDataCur, p2: regRowid, p3: regAutoinc);
132103 sqlite3VdbeJumpHere(p: v, addr: addr1);
132104 }else{
132105 addr1 = sqlite3VdbeCurrentAddr(p: v);
132106 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regRowid, p2: addr1+2); VdbeCoverage(v);
132107 }
132108 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: regRowid); VdbeCoverage(v);
132109 }
132110 }else if( IsVirtual(pTab) || withoutRowid ){
132111 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regRowid);
132112 }else{
132113 sqlite3VdbeAddOp3(p: v, OP_NewRowid, p1: iDataCur, p2: regRowid, p3: regAutoinc);
132114 appendFlag = 1;
132115 }
132116 autoIncStep(pParse, memId: regAutoinc, regRowid);
132117
132118#ifndef SQLITE_OMIT_GENERATED_COLUMNS
132119 /* Compute the new value for generated columns after all other
132120 ** columns have already been computed. This must be done after
132121 ** computing the ROWID in case one of the generated columns
132122 ** is derived from the INTEGER PRIMARY KEY. */
132123 if( pTab->tabFlags & TF_HasGenerated ){
132124 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regRowid+1, pTab);
132125 }
132126#endif
132127
132128 /* Generate code to check constraints and generate index keys and
132129 ** do the insertion.
132130 */
132131#ifndef SQLITE_OMIT_VIRTUALTABLE
132132 if( IsVirtual(pTab) ){
132133 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
132134 sqlite3VtabMakeWritable(pParse, pTab);
132135 sqlite3VdbeAddOp4(p: v, OP_VUpdate, p1: 1, p2: pTab->nCol+2, p3: regIns, zP4: pVTab, P4_VTAB);
132136 sqlite3VdbeChangeP5(p: v, p5: onError==OE_Default ? OE_Abort : onError);
132137 sqlite3MayAbort(pParse);
132138 }else
132139#endif
132140 {
132141 int isReplace = 0;/* Set to true if constraints may cause a replace */
132142 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
132143 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
132144 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
132145 );
132146 if( db->flags & SQLITE_ForeignKeys ){
132147 sqlite3FkCheck(pParse, pTab, regOld: 0, regNew: regIns, aChange: 0, bChngRowid: 0);
132148 }
132149
132150 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
132151 ** constraints or (b) there are no triggers and this table is not a
132152 ** parent table in a foreign key constraint. It is safe to set the
132153 ** flag in the second case as if any REPLACE constraint is hit, an
132154 ** OP_Delete or OP_IdxDelete instruction will be executed on each
132155 ** cursor that is disturbed. And these instructions both clear the
132156 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
132157 ** functionality. */
132158 bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(pVdbe: v));
132159 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
132160 regIns, aRegIdx, 0, appendFlag, bUseSeek
132161 );
132162 }
132163#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
132164 }else if( pParse->bReturning ){
132165 /* If there is a RETURNING clause, populate the rowid register with
132166 ** constant value -1, in case one or more of the returned expressions
132167 ** refer to the "rowid" of the view. */
132168 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
132169#endif
132170 }
132171
132172 /* Update the count of rows that are inserted
132173 */
132174 if( regRowCount ){
132175 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regRowCount, p2: 1);
132176 }
132177
132178 if( pTrigger ){
132179 /* Code AFTER triggers */
132180 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
132181 pTab, regData-2-pTab->nCol, onError, endOfLoop);
132182 }
132183
132184 /* The bottom of the main insertion loop, if the data source
132185 ** is a SELECT statement.
132186 */
132187 sqlite3VdbeResolveLabel(v, x: endOfLoop);
132188 if( useTempTable ){
132189 sqlite3VdbeAddOp2(p: v, OP_Next, p1: srcTab, p2: addrCont); VdbeCoverage(v);
132190 sqlite3VdbeJumpHere(p: v, addr: addrInsTop);
132191 sqlite3VdbeAddOp1(p: v, OP_Close, p1: srcTab);
132192 }else if( pSelect ){
132193 sqlite3VdbeGoto(p: v, iDest: addrCont);
132194#ifdef SQLITE_DEBUG
132195 /* If we are jumping back to an OP_Yield that is preceded by an
132196 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
132197 ** OP_ReleaseReg will be included in the loop. */
132198 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
132199 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
132200 sqlite3VdbeChangeP5(v, 1);
132201 }
132202#endif
132203 sqlite3VdbeJumpHere(p: v, addr: addrInsTop);
132204 }
132205
132206#ifndef SQLITE_OMIT_XFER_OPT
132207insert_end:
132208#endif /* SQLITE_OMIT_XFER_OPT */
132209 /* Update the sqlite_sequence table by storing the content of the
132210 ** maximum rowid counter values recorded while inserting into
132211 ** autoincrement tables.
132212 */
132213 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
132214 sqlite3AutoincrementEnd(pParse);
132215 }
132216
132217 /*
132218 ** Return the number of rows inserted. If this routine is
132219 ** generating code because of a call to sqlite3NestedParse(), do not
132220 ** invoke the callback function.
132221 */
132222 if( regRowCount ){
132223 sqlite3CodeChangeCount(v, regCounter: regRowCount, zColName: "rows inserted");
132224 }
132225
132226insert_cleanup:
132227 sqlite3SrcListDelete(db, pList: pTabList);
132228 sqlite3ExprListDelete(db, pList);
132229 sqlite3UpsertDelete(db, pUpsert);
132230 sqlite3SelectDelete(db, pSelect);
132231 sqlite3IdListDelete(db, pList: pColumn);
132232 if( aRegIdx ) sqlite3DbNNFreeNN(db, p: aRegIdx);
132233}
132234
132235/* Make sure "isView" and other macros defined above are undefined. Otherwise
132236** they may interfere with compilation of other functions in this file
132237** (or in another file, if this file becomes part of the amalgamation). */
132238#ifdef isView
132239 #undef isView
132240#endif
132241#ifdef pTrigger
132242 #undef pTrigger
132243#endif
132244#ifdef tmask
132245 #undef tmask
132246#endif
132247
132248/*
132249** Meanings of bits in of pWalker->eCode for
132250** sqlite3ExprReferencesUpdatedColumn()
132251*/
132252#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
132253#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
132254
132255/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
132256* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
132257** expression node references any of the
132258** columns that are being modified by an UPDATE statement.
132259*/
132260static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
132261 if( pExpr->op==TK_COLUMN ){
132262 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
132263 if( pExpr->iColumn>=0 ){
132264 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
132265 pWalker->eCode |= CKCNSTRNT_COLUMN;
132266 }
132267 }else{
132268 pWalker->eCode |= CKCNSTRNT_ROWID;
132269 }
132270 }
132271 return WRC_Continue;
132272}
132273
132274/*
132275** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
132276** only columns that are modified by the UPDATE are those for which
132277** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
132278**
132279** Return true if CHECK constraint pExpr uses any of the
132280** changing columns (or the rowid if it is changing). In other words,
132281** return true if this CHECK constraint must be validated for
132282** the new row in the UPDATE statement.
132283**
132284** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
132285** The operation of this routine is the same - return true if an only if
132286** the expression uses one or more of columns identified by the second and
132287** third arguments.
132288*/
132289SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
132290 Expr *pExpr, /* The expression to be checked */
132291 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
132292 int chngRowid /* True if UPDATE changes the rowid */
132293){
132294 Walker w;
132295 memset(s: &w, c: 0, n: sizeof(w));
132296 w.eCode = 0;
132297 w.xExprCallback = checkConstraintExprNode;
132298 w.u.aiCol = aiChng;
132299 sqlite3WalkExpr(pWalker: &w, pExpr);
132300 if( !chngRowid ){
132301 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
132302 w.eCode &= ~CKCNSTRNT_ROWID;
132303 }
132304 testcase( w.eCode==0 );
132305 testcase( w.eCode==CKCNSTRNT_COLUMN );
132306 testcase( w.eCode==CKCNSTRNT_ROWID );
132307 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
132308 return w.eCode!=0;
132309}
132310
132311/*
132312** The sqlite3GenerateConstraintChecks() routine usually wants to visit
132313** the indexes of a table in the order provided in the Table->pIndex list.
132314** However, sometimes (rarely - when there is an upsert) it wants to visit
132315** the indexes in a different order. The following data structures accomplish
132316** this.
132317**
132318** The IndexIterator object is used to walk through all of the indexes
132319** of a table in either Index.pNext order, or in some other order established
132320** by an array of IndexListTerm objects.
132321*/
132322typedef struct IndexListTerm IndexListTerm;
132323typedef struct IndexIterator IndexIterator;
132324struct IndexIterator {
132325 int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
132326 int i; /* Index of the current item from the list */
132327 union {
132328 struct { /* Use this object for eType==0: A Index.pNext list */
132329 Index *pIdx; /* The current Index */
132330 } lx;
132331 struct { /* Use this object for eType==1; Array of IndexListTerm */
132332 int nIdx; /* Size of the array */
132333 IndexListTerm *aIdx; /* Array of IndexListTerms */
132334 } ax;
132335 } u;
132336};
132337
132338/* When IndexIterator.eType==1, then each index is an array of instances
132339** of the following object
132340*/
132341struct IndexListTerm {
132342 Index *p; /* The index */
132343 int ix; /* Which entry in the original Table.pIndex list is this index*/
132344};
132345
132346/* Return the first index on the list */
132347static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
132348 assert( pIter->i==0 );
132349 if( pIter->eType ){
132350 *pIx = pIter->u.ax.aIdx[0].ix;
132351 return pIter->u.ax.aIdx[0].p;
132352 }else{
132353 *pIx = 0;
132354 return pIter->u.lx.pIdx;
132355 }
132356}
132357
132358/* Return the next index from the list. Return NULL when out of indexes */
132359static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
132360 if( pIter->eType ){
132361 int i = ++pIter->i;
132362 if( i>=pIter->u.ax.nIdx ){
132363 *pIx = i;
132364 return 0;
132365 }
132366 *pIx = pIter->u.ax.aIdx[i].ix;
132367 return pIter->u.ax.aIdx[i].p;
132368 }else{
132369 ++(*pIx);
132370 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
132371 return pIter->u.lx.pIdx;
132372 }
132373}
132374
132375/*
132376** Generate code to do constraint checks prior to an INSERT or an UPDATE
132377** on table pTab.
132378**
132379** The regNewData parameter is the first register in a range that contains
132380** the data to be inserted or the data after the update. There will be
132381** pTab->nCol+1 registers in this range. The first register (the one
132382** that regNewData points to) will contain the new rowid, or NULL in the
132383** case of a WITHOUT ROWID table. The second register in the range will
132384** contain the content of the first table column. The third register will
132385** contain the content of the second table column. And so forth.
132386**
132387** The regOldData parameter is similar to regNewData except that it contains
132388** the data prior to an UPDATE rather than afterwards. regOldData is zero
132389** for an INSERT. This routine can distinguish between UPDATE and INSERT by
132390** checking regOldData for zero.
132391**
132392** For an UPDATE, the pkChng boolean is true if the true primary key (the
132393** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
132394** might be modified by the UPDATE. If pkChng is false, then the key of
132395** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
132396**
132397** For an INSERT, the pkChng boolean indicates whether or not the rowid
132398** was explicitly specified as part of the INSERT statement. If pkChng
132399** is zero, it means that the either rowid is computed automatically or
132400** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
132401** pkChng will only be true if the INSERT statement provides an integer
132402** value for either the rowid column or its INTEGER PRIMARY KEY alias.
132403**
132404** The code generated by this routine will store new index entries into
132405** registers identified by aRegIdx[]. No index entry is created for
132406** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
132407** the same as the order of indices on the linked list of indices
132408** at pTab->pIndex.
132409**
132410** (2019-05-07) The generated code also creates a new record for the
132411** main table, if pTab is a rowid table, and stores that record in the
132412** register identified by aRegIdx[nIdx] - in other words in the first
132413** entry of aRegIdx[] past the last index. It is important that the
132414** record be generated during constraint checks to avoid affinity changes
132415** to the register content that occur after constraint checks but before
132416** the new record is inserted.
132417**
132418** The caller must have already opened writeable cursors on the main
132419** table and all applicable indices (that is to say, all indices for which
132420** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
132421** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
132422** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
132423** for the first index in the pTab->pIndex list. Cursors for other indices
132424** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
132425**
132426** This routine also generates code to check constraints. NOT NULL,
132427** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
132428** then the appropriate action is performed. There are five possible
132429** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
132430**
132431** Constraint type Action What Happens
132432** --------------- ---------- ----------------------------------------
132433** any ROLLBACK The current transaction is rolled back and
132434** sqlite3_step() returns immediately with a
132435** return code of SQLITE_CONSTRAINT.
132436**
132437** any ABORT Back out changes from the current command
132438** only (do not do a complete rollback) then
132439** cause sqlite3_step() to return immediately
132440** with SQLITE_CONSTRAINT.
132441**
132442** any FAIL Sqlite3_step() returns immediately with a
132443** return code of SQLITE_CONSTRAINT. The
132444** transaction is not rolled back and any
132445** changes to prior rows are retained.
132446**
132447** any IGNORE The attempt in insert or update the current
132448** row is skipped, without throwing an error.
132449** Processing continues with the next row.
132450** (There is an immediate jump to ignoreDest.)
132451**
132452** NOT NULL REPLACE The NULL value is replace by the default
132453** value for that column. If the default value
132454** is NULL, the action is the same as ABORT.
132455**
132456** UNIQUE REPLACE The other row that conflicts with the row
132457** being inserted is removed.
132458**
132459** CHECK REPLACE Illegal. The results in an exception.
132460**
132461** Which action to take is determined by the overrideError parameter.
132462** Or if overrideError==OE_Default, then the pParse->onError parameter
132463** is used. Or if pParse->onError==OE_Default then the onError value
132464** for the constraint is used.
132465*/
132466SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
132467 Parse *pParse, /* The parser context */
132468 Table *pTab, /* The table being inserted or updated */
132469 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
132470 int iDataCur, /* Canonical data cursor (main table or PK index) */
132471 int iIdxCur, /* First index cursor */
132472 int regNewData, /* First register in a range holding values to insert */
132473 int regOldData, /* Previous content. 0 for INSERTs */
132474 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
132475 u8 overrideError, /* Override onError to this if not OE_Default */
132476 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
132477 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
132478 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
132479 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
132480){
132481 Vdbe *v; /* VDBE under construction */
132482 Index *pIdx; /* Pointer to one of the indices */
132483 Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */
132484 sqlite3 *db; /* Database connection */
132485 int i; /* loop counter */
132486 int ix; /* Index loop counter */
132487 int nCol; /* Number of columns */
132488 int onError; /* Conflict resolution strategy */
132489 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
132490 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
132491 Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */
132492 u8 isUpdate; /* True if this is an UPDATE operation */
132493 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
132494 int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
132495 int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */
132496 int ipkTop = 0; /* Top of the IPK uniqueness check */
132497 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
132498 /* Variables associated with retesting uniqueness constraints after
132499 ** replace triggers fire have run */
132500 int regTrigCnt; /* Register used to count replace trigger invocations */
132501 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
132502 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
132503 Trigger *pTrigger; /* List of DELETE triggers on the table pTab */
132504 int nReplaceTrig = 0; /* Number of replace triggers coded */
132505 IndexIterator sIdxIter; /* Index iterator */
132506
132507 isUpdate = regOldData!=0;
132508 db = pParse->db;
132509 v = pParse->pVdbe;
132510 assert( v!=0 );
132511 assert( !IsView(pTab) ); /* This table is not a VIEW */
132512 nCol = pTab->nCol;
132513
132514 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
132515 ** normal rowid tables. nPkField is the number of key fields in the
132516 ** pPk index or 1 for a rowid table. In other words, nPkField is the
132517 ** number of fields in the true primary key of the table. */
132518 if( HasRowid(pTab) ){
132519 pPk = 0;
132520 nPkField = 1;
132521 }else{
132522 pPk = sqlite3PrimaryKeyIndex(pTab);
132523 nPkField = pPk->nKeyCol;
132524 }
132525
132526 /* Record that this module has started */
132527 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
132528 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
132529
132530 /* Test all NOT NULL constraints.
132531 */
132532 if( pTab->tabFlags & TF_HasNotNull ){
132533 int b2ndPass = 0; /* True if currently running 2nd pass */
132534 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
132535 int nGenerated = 0; /* Number of generated columns with NOT NULL */
132536 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
132537 for(i=0; i<nCol; i++){
132538 int iReg; /* Register holding column value */
132539 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
132540 int isGenerated; /* non-zero if column is generated */
132541 onError = pCol->notNull;
132542 if( onError==OE_None ) continue; /* No NOT NULL on this column */
132543 if( i==pTab->iPKey ){
132544 continue; /* ROWID is never NULL */
132545 }
132546 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
132547 if( isGenerated && !b2ndPass ){
132548 nGenerated++;
132549 continue; /* Generated columns processed on 2nd pass */
132550 }
132551 if( aiChng && aiChng[i]<0 && !isGenerated ){
132552 /* Do not check NOT NULL on columns that do not change */
132553 continue;
132554 }
132555 if( overrideError!=OE_Default ){
132556 onError = overrideError;
132557 }else if( onError==OE_Default ){
132558 onError = OE_Abort;
132559 }
132560 if( onError==OE_Replace ){
132561 if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */
132562 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
132563 ){
132564 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
132565 testcase( pCol->colFlags & COLFLAG_STORED );
132566 testcase( pCol->colFlags & COLFLAG_GENERATED );
132567 onError = OE_Abort;
132568 }else{
132569 assert( !isGenerated );
132570 }
132571 }else if( b2ndPass && !isGenerated ){
132572 continue;
132573 }
132574 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
132575 || onError==OE_Ignore || onError==OE_Replace );
132576 testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
132577 iReg = sqlite3TableColumnToStorage(pTab, iCol: i) + regNewData + 1;
132578 switch( onError ){
132579 case OE_Replace: {
132580 int addr1 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: iReg);
132581 VdbeCoverage(v);
132582 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
132583 nSeenReplace++;
132584 sqlite3ExprCodeCopy(pParse,
132585 pExpr: sqlite3ColumnExpr(pTab, pCol), target: iReg);
132586 sqlite3VdbeJumpHere(p: v, addr: addr1);
132587 break;
132588 }
132589 case OE_Abort:
132590 sqlite3MayAbort(pParse);
132591 /* no break */ deliberate_fall_through
132592 case OE_Rollback:
132593 case OE_Fail: {
132594 char *zMsg = sqlite3MPrintf(db, zFormat: "%s.%s", pTab->zName,
132595 pCol->zCnName);
132596 testcase( zMsg==0 && db->mallocFailed==0 );
132597 sqlite3VdbeAddOp3(p: v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
132598 p2: onError, p3: iReg);
132599 sqlite3VdbeAppendP4(p: v, pP4: zMsg, P4_DYNAMIC);
132600 sqlite3VdbeChangeP5(p: v, P5_ConstraintNotNull);
132601 VdbeCoverage(v);
132602 break;
132603 }
132604 default: {
132605 assert( onError==OE_Ignore );
132606 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: iReg, p2: ignoreDest);
132607 VdbeCoverage(v);
132608 break;
132609 }
132610 } /* end switch(onError) */
132611 } /* end loop i over columns */
132612 if( nGenerated==0 && nSeenReplace==0 ){
132613 /* If there are no generated columns with NOT NULL constraints
132614 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
132615 ** pass is sufficient */
132616 break;
132617 }
132618 if( b2ndPass ) break; /* Never need more than 2 passes */
132619 b2ndPass = 1;
132620#ifndef SQLITE_OMIT_GENERATED_COLUMNS
132621 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
132622 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
132623 ** first pass, recomputed values for all generated columns, as
132624 ** those values might depend on columns affected by the REPLACE.
132625 */
132626 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regNewData+1, pTab);
132627 }
132628#endif
132629 } /* end of 2-pass loop */
132630 } /* end if( has-not-null-constraints ) */
132631
132632 /* Test all CHECK constraints
132633 */
132634#ifndef SQLITE_OMIT_CHECK
132635 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
132636 ExprList *pCheck = pTab->pCheck;
132637 pParse->iSelfTab = -(regNewData+1);
132638 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
132639 for(i=0; i<pCheck->nExpr; i++){
132640 int allOk;
132641 Expr *pCopy;
132642 Expr *pExpr = pCheck->a[i].pExpr;
132643 if( aiChng
132644 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, chngRowid: pkChng)
132645 ){
132646 /* The check constraints do not reference any of the columns being
132647 ** updated so there is no point it verifying the check constraint */
132648 continue;
132649 }
132650 if( bAffinityDone==0 ){
132651 sqlite3TableAffinity(v, pTab, iReg: regNewData+1);
132652 bAffinityDone = 1;
132653 }
132654 allOk = sqlite3VdbeMakeLabel(pParse);
132655 sqlite3VdbeVerifyAbortable(v, onError);
132656 pCopy = sqlite3ExprDup(db, p: pExpr, flags: 0);
132657 if( !db->mallocFailed ){
132658 sqlite3ExprIfTrue(pParse, pExpr: pCopy, dest: allOk, SQLITE_JUMPIFNULL);
132659 }
132660 sqlite3ExprDelete(db, p: pCopy);
132661 if( onError==OE_Ignore ){
132662 sqlite3VdbeGoto(p: v, iDest: ignoreDest);
132663 }else{
132664 char *zName = pCheck->a[i].zEName;
132665 assert( zName!=0 || pParse->db->mallocFailed );
132666 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
132667 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
132668 onError, p4: zName, P4_TRANSIENT,
132669 P5_ConstraintCheck);
132670 }
132671 sqlite3VdbeResolveLabel(v, x: allOk);
132672 }
132673 pParse->iSelfTab = 0;
132674 }
132675#endif /* !defined(SQLITE_OMIT_CHECK) */
132676
132677 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
132678 ** order:
132679 **
132680 ** (1) OE_Update
132681 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
132682 ** (3) OE_Replace
132683 **
132684 ** OE_Fail and OE_Ignore must happen before any changes are made.
132685 ** OE_Update guarantees that only a single row will change, so it
132686 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
132687 ** could happen in any order, but they are grouped up front for
132688 ** convenience.
132689 **
132690 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
132691 ** The order of constraints used to have OE_Update as (2) and OE_Abort
132692 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
132693 ** constraint before any others, so it had to be moved.
132694 **
132695 ** Constraint checking code is generated in this order:
132696 ** (A) The rowid constraint
132697 ** (B) Unique index constraints that do not have OE_Replace as their
132698 ** default conflict resolution strategy
132699 ** (C) Unique index that do use OE_Replace by default.
132700 **
132701 ** The ordering of (2) and (3) is accomplished by making sure the linked
132702 ** list of indexes attached to a table puts all OE_Replace indexes last
132703 ** in the list. See sqlite3CreateIndex() for where that happens.
132704 */
132705 sIdxIter.eType = 0;
132706 sIdxIter.i = 0;
132707 sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */
132708 sIdxIter.u.lx.pIdx = pTab->pIndex;
132709 if( pUpsert ){
132710 if( pUpsert->pUpsertTarget==0 ){
132711 /* There is just on ON CONFLICT clause and it has no constraint-target */
132712 assert( pUpsert->pNextUpsert==0 );
132713 if( pUpsert->isDoUpdate==0 ){
132714 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
132715 ** Make all unique constraint resolution be OE_Ignore */
132716 overrideError = OE_Ignore;
132717 pUpsert = 0;
132718 }else{
132719 /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */
132720 overrideError = OE_Update;
132721 }
132722 }else if( pTab->pIndex!=0 ){
132723 /* Otherwise, we'll need to run the IndexListTerm array version of the
132724 ** iterator to ensure that all of the ON CONFLICT conditions are
132725 ** checked first and in order. */
132726 int nIdx, jj;
132727 u64 nByte;
132728 Upsert *pTerm;
132729 u8 *bUsed;
132730 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
132731 assert( aRegIdx[nIdx]>0 );
132732 }
132733 sIdxIter.eType = 1;
132734 sIdxIter.u.ax.nIdx = nIdx;
132735 nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
132736 sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, n: nByte);
132737 if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
132738 bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
132739 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
132740 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
132741 if( pTerm->pUpsertTarget==0 ) break;
132742 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
132743 jj = 0;
132744 pIdx = pTab->pIndex;
132745 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
132746 pIdx = pIdx->pNext;
132747 jj++;
132748 }
132749 if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
132750 bUsed[jj] = 1;
132751 sIdxIter.u.ax.aIdx[i].p = pIdx;
132752 sIdxIter.u.ax.aIdx[i].ix = jj;
132753 i++;
132754 }
132755 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
132756 if( bUsed[jj] ) continue;
132757 sIdxIter.u.ax.aIdx[i].p = pIdx;
132758 sIdxIter.u.ax.aIdx[i].ix = jj;
132759 i++;
132760 }
132761 assert( i==nIdx );
132762 }
132763 }
132764
132765 /* Determine if it is possible that triggers (either explicitly coded
132766 ** triggers or FK resolution actions) might run as a result of deletes
132767 ** that happen when OE_Replace conflict resolution occurs. (Call these
132768 ** "replace triggers".) If any replace triggers run, we will need to
132769 ** recheck all of the uniqueness constraints after they have all run.
132770 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
132771 **
132772 ** If replace triggers are a possibility, then
132773 **
132774 ** (1) Allocate register regTrigCnt and initialize it to zero.
132775 ** That register will count the number of replace triggers that
132776 ** fire. Constraint recheck only occurs if the number is positive.
132777 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
132778 ** (3) Initialize addrRecheck and lblRecheckOk
132779 **
132780 ** The uniqueness rechecking code will create a series of tests to run
132781 ** in a second pass. The addrRecheck and lblRecheckOk variables are
132782 ** used to link together these tests which are separated from each other
132783 ** in the generate bytecode.
132784 */
132785 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
132786 /* There are not DELETE triggers nor FK constraints. No constraint
132787 ** rechecks are needed. */
132788 pTrigger = 0;
132789 regTrigCnt = 0;
132790 }else{
132791 if( db->flags&SQLITE_RecTriggers ){
132792 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, pMask: 0);
132793 regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, aChange: 0, chngRowid: 0);
132794 }else{
132795 pTrigger = 0;
132796 regTrigCnt = sqlite3FkRequired(pParse, pTab, aChange: 0, chngRowid: 0);
132797 }
132798 if( regTrigCnt ){
132799 /* Replace triggers might exist. Allocate the counter and
132800 ** initialize it to zero. */
132801 regTrigCnt = ++pParse->nMem;
132802 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regTrigCnt);
132803 VdbeComment((v, "trigger count"));
132804 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
132805 addrRecheck = lblRecheckOk;
132806 }
132807 }
132808
132809 /* If rowid is changing, make sure the new rowid does not previously
132810 ** exist in the table.
132811 */
132812 if( pkChng && pPk==0 ){
132813 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
132814
132815 /* Figure out what action to take in case of a rowid collision */
132816 onError = pTab->keyConf;
132817 if( overrideError!=OE_Default ){
132818 onError = overrideError;
132819 }else if( onError==OE_Default ){
132820 onError = OE_Abort;
132821 }
132822
132823 /* figure out whether or not upsert applies in this case */
132824 if( pUpsert ){
132825 pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
132826 if( pUpsertClause!=0 ){
132827 if( pUpsertClause->isDoUpdate==0 ){
132828 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
132829 }else{
132830 onError = OE_Update; /* DO UPDATE */
132831 }
132832 }
132833 if( pUpsertClause!=pUpsert ){
132834 /* The first ON CONFLICT clause has a conflict target other than
132835 ** the IPK. We have to jump ahead to that first ON CONFLICT clause
132836 ** and then come back here and deal with the IPK afterwards */
132837 upsertIpkDelay = sqlite3VdbeAddOp0(p: v, OP_Goto);
132838 }
132839 }
132840
132841 /* If the response to a rowid conflict is REPLACE but the response
132842 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
132843 ** to defer the running of the rowid conflict checking until after
132844 ** the UNIQUE constraints have run.
132845 */
132846 if( onError==OE_Replace /* IPK rule is REPLACE */
132847 && onError!=overrideError /* Rules for other constraints are different */
132848 && pTab->pIndex /* There exist other constraints */
132849 && !upsertIpkDelay /* IPK check already deferred by UPSERT */
132850 ){
132851 ipkTop = sqlite3VdbeAddOp0(p: v, OP_Goto)+1;
132852 VdbeComment((v, "defer IPK REPLACE until last"));
132853 }
132854
132855 if( isUpdate ){
132856 /* pkChng!=0 does not mean that the rowid has changed, only that
132857 ** it might have changed. Skip the conflict logic below if the rowid
132858 ** is unchanged. */
132859 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regNewData, p2: addrRowidOk, p3: regOldData);
132860 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
132861 VdbeCoverage(v);
132862 }
132863
132864 /* Check to see if the new rowid already exists in the table. Skip
132865 ** the following conflict logic if it does not. */
132866 VdbeNoopComment((v, "uniqueness check for ROWID"));
132867 sqlite3VdbeVerifyAbortable(v, onError);
132868 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: addrRowidOk, p3: regNewData);
132869 VdbeCoverage(v);
132870
132871 switch( onError ){
132872 default: {
132873 onError = OE_Abort;
132874 /* no break */ deliberate_fall_through
132875 }
132876 case OE_Rollback:
132877 case OE_Abort:
132878 case OE_Fail: {
132879 testcase( onError==OE_Rollback );
132880 testcase( onError==OE_Abort );
132881 testcase( onError==OE_Fail );
132882 sqlite3RowidConstraint(pParse, onError, pTab);
132883 break;
132884 }
132885 case OE_Replace: {
132886 /* If there are DELETE triggers on this table and the
132887 ** recursive-triggers flag is set, call GenerateRowDelete() to
132888 ** remove the conflicting row from the table. This will fire
132889 ** the triggers and remove both the table and index b-tree entries.
132890 **
132891 ** Otherwise, if there are no triggers or the recursive-triggers
132892 ** flag is not set, but the table has one or more indexes, call
132893 ** GenerateRowIndexDelete(). This removes the index b-tree entries
132894 ** only. The table b-tree entry will be replaced by the new entry
132895 ** when it is inserted.
132896 **
132897 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
132898 ** also invoke MultiWrite() to indicate that this VDBE may require
132899 ** statement rollback (if the statement is aborted after the delete
132900 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
132901 ** but being more selective here allows statements like:
132902 **
132903 ** REPLACE INTO t(rowid) VALUES($newrowid)
132904 **
132905 ** to run without a statement journal if there are no indexes on the
132906 ** table.
132907 */
132908 if( regTrigCnt ){
132909 sqlite3MultiWrite(pParse);
132910 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
132911 iPk: regNewData, nPk: 1, count: 0, OE_Replace, eMode: 1, iIdxNoSeek: -1);
132912 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regTrigCnt, p2: 1); /* incr trigger cnt */
132913 nReplaceTrig++;
132914 }else{
132915#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
132916 assert( HasRowid(pTab) );
132917 /* This OP_Delete opcode fires the pre-update-hook only. It does
132918 ** not modify the b-tree. It is more efficient to let the coming
132919 ** OP_Insert replace the existing entry than it is to delete the
132920 ** existing entry and then insert a new one. */
132921 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
132922 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
132923#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
132924 if( pTab->pIndex ){
132925 sqlite3MultiWrite(pParse);
132926 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,aRegIdx: 0,iIdxNoSeek: -1);
132927 }
132928 }
132929 seenReplace = 1;
132930 break;
132931 }
132932#ifndef SQLITE_OMIT_UPSERT
132933 case OE_Update: {
132934 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
132935 /* no break */ deliberate_fall_through
132936 }
132937#endif
132938 case OE_Ignore: {
132939 testcase( onError==OE_Ignore );
132940 sqlite3VdbeGoto(p: v, iDest: ignoreDest);
132941 break;
132942 }
132943 }
132944 sqlite3VdbeResolveLabel(v, x: addrRowidOk);
132945 if( pUpsert && pUpsertClause!=pUpsert ){
132946 upsertIpkReturn = sqlite3VdbeAddOp0(p: v, OP_Goto);
132947 }else if( ipkTop ){
132948 ipkBottom = sqlite3VdbeAddOp0(p: v, OP_Goto);
132949 sqlite3VdbeJumpHere(p: v, addr: ipkTop-1);
132950 }
132951 }
132952
132953 /* Test all UNIQUE constraints by creating entries for each UNIQUE
132954 ** index and making sure that duplicate entries do not already exist.
132955 ** Compute the revised record entries for indices as we go.
132956 **
132957 ** This loop also handles the case of the PRIMARY KEY index for a
132958 ** WITHOUT ROWID table.
132959 */
132960 for(pIdx = indexIteratorFirst(pIter: &sIdxIter, pIx: &ix);
132961 pIdx;
132962 pIdx = indexIteratorNext(pIter: &sIdxIter, pIx: &ix)
132963 ){
132964 int regIdx; /* Range of registers holding content for pIdx */
132965 int regR; /* Range of registers holding conflicting PK */
132966 int iThisCur; /* Cursor for this UNIQUE index */
132967 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
132968 int addrConflictCk; /* First opcode in the conflict check logic */
132969
132970 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
132971 if( pUpsert ){
132972 pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
132973 if( upsertIpkDelay && pUpsertClause==pUpsert ){
132974 sqlite3VdbeJumpHere(p: v, addr: upsertIpkDelay);
132975 }
132976 }
132977 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
132978 if( bAffinityDone==0 ){
132979 sqlite3TableAffinity(v, pTab, iReg: regNewData+1);
132980 bAffinityDone = 1;
132981 }
132982 VdbeNoopComment((v, "prep index %s", pIdx->zName));
132983 iThisCur = iIdxCur+ix;
132984
132985
132986 /* Skip partial indices for which the WHERE clause is not true */
132987 if( pIdx->pPartIdxWhere ){
132988 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: aRegIdx[ix]);
132989 pParse->iSelfTab = -(regNewData+1);
132990 sqlite3ExprIfFalseDup(pParse, pExpr: pIdx->pPartIdxWhere, dest: addrUniqueOk,
132991 SQLITE_JUMPIFNULL);
132992 pParse->iSelfTab = 0;
132993 }
132994
132995 /* Create a record for this index entry as it should appear after
132996 ** the insert or update. Store that record in the aRegIdx[ix] register
132997 */
132998 regIdx = aRegIdx[ix]+1;
132999 for(i=0; i<pIdx->nColumn; i++){
133000 int iField = pIdx->aiColumn[i];
133001 int x;
133002 if( iField==XN_EXPR ){
133003 pParse->iSelfTab = -(regNewData+1);
133004 sqlite3ExprCodeCopy(pParse, pExpr: pIdx->aColExpr->a[i].pExpr, target: regIdx+i);
133005 pParse->iSelfTab = 0;
133006 VdbeComment((v, "%s column %d", pIdx->zName, i));
133007 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
133008 x = regNewData;
133009 sqlite3VdbeAddOp2(p: v, OP_IntCopy, p1: x, p2: regIdx+i);
133010 VdbeComment((v, "rowid"));
133011 }else{
133012 testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
133013 x = sqlite3TableColumnToStorage(pTab, iCol: iField) + regNewData + 1;
133014 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: x, p2: regIdx+i);
133015 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
133016 }
133017 }
133018 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regIdx, p2: pIdx->nColumn, p3: aRegIdx[ix]);
133019 VdbeComment((v, "for %s", pIdx->zName));
133020#ifdef SQLITE_ENABLE_NULL_TRIM
133021 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
133022 sqlite3SetMakeRecordP5(v, pIdx->pTable);
133023 }
133024#endif
133025 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
133026
133027 /* In an UPDATE operation, if this index is the PRIMARY KEY index
133028 ** of a WITHOUT ROWID table and there has been no change the
133029 ** primary key, then no collision is possible. The collision detection
133030 ** logic below can all be skipped. */
133031 if( isUpdate && pPk==pIdx && pkChng==0 ){
133032 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
133033 continue;
133034 }
133035
133036 /* Find out what action to take in case there is a uniqueness conflict */
133037 onError = pIdx->onError;
133038 if( onError==OE_None ){
133039 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
133040 continue; /* pIdx is not a UNIQUE index */
133041 }
133042 if( overrideError!=OE_Default ){
133043 onError = overrideError;
133044 }else if( onError==OE_Default ){
133045 onError = OE_Abort;
133046 }
133047
133048 /* Figure out if the upsert clause applies to this index */
133049 if( pUpsertClause ){
133050 if( pUpsertClause->isDoUpdate==0 ){
133051 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
133052 }else{
133053 onError = OE_Update; /* DO UPDATE */
133054 }
133055 }
133056
133057 /* Collision detection may be omitted if all of the following are true:
133058 ** (1) The conflict resolution algorithm is REPLACE
133059 ** (2) The table is a WITHOUT ROWID table
133060 ** (3) There are no secondary indexes on the table
133061 ** (4) No delete triggers need to be fired if there is a conflict
133062 ** (5) No FK constraint counters need to be updated if a conflict occurs.
133063 **
133064 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
133065 ** must be explicitly deleted in order to ensure any pre-update hook
133066 ** is invoked. */
133067 assert( IsOrdinaryTable(pTab) );
133068#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
133069 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
133070 && pPk==pIdx /* Condition 2 */
133071 && onError==OE_Replace /* Condition 1 */
133072 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
133073 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, pMask: 0))
133074 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
133075 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
133076 ){
133077 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
133078 continue;
133079 }
133080#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
133081
133082 /* Check to see if the new index entry will be unique */
133083 sqlite3VdbeVerifyAbortable(v, onError);
133084 addrConflictCk =
133085 sqlite3VdbeAddOp4Int(p: v, OP_NoConflict, p1: iThisCur, p2: addrUniqueOk,
133086 p3: regIdx, p4: pIdx->nKeyCol); VdbeCoverage(v);
133087
133088 /* Generate code to handle collisions */
133089 regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nReg: nPkField);
133090 if( isUpdate || onError==OE_Replace ){
133091 if( HasRowid(pTab) ){
133092 sqlite3VdbeAddOp2(p: v, OP_IdxRowid, p1: iThisCur, p2: regR);
133093 /* Conflict only if the rowid of the existing index entry
133094 ** is different from old-rowid */
133095 if( isUpdate ){
133096 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regR, p2: addrUniqueOk, p3: regOldData);
133097 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
133098 VdbeCoverage(v);
133099 }
133100 }else{
133101 int x;
133102 /* Extract the PRIMARY KEY from the end of the index entry and
133103 ** store it in registers regR..regR+nPk-1 */
133104 if( pIdx!=pPk ){
133105 for(i=0; i<pPk->nKeyCol; i++){
133106 assert( pPk->aiColumn[i]>=0 );
133107 x = sqlite3TableColumnToIndex(pIdx, iCol: pPk->aiColumn[i]);
133108 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iThisCur, p2: x, p3: regR+i);
133109 VdbeComment((v, "%s.%s", pTab->zName,
133110 pTab->aCol[pPk->aiColumn[i]].zCnName));
133111 }
133112 }
133113 if( isUpdate ){
133114 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
133115 ** table, only conflict if the new PRIMARY KEY values are actually
133116 ** different from the old. See TH3 withoutrowid04.test.
133117 **
133118 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
133119 ** of the matched index row are different from the original PRIMARY
133120 ** KEY values of this row before the update. */
133121 int addrJump = sqlite3VdbeCurrentAddr(p: v)+pPk->nKeyCol;
133122 int op = OP_Ne;
133123 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
133124
133125 for(i=0; i<pPk->nKeyCol; i++){
133126 char *p4 = (char*)sqlite3LocateCollSeq(pParse, zName: pPk->azColl[i]);
133127 x = pPk->aiColumn[i];
133128 assert( x>=0 );
133129 if( i==(pPk->nKeyCol-1) ){
133130 addrJump = addrUniqueOk;
133131 op = OP_Eq;
133132 }
133133 x = sqlite3TableColumnToStorage(pTab, iCol: x);
133134 sqlite3VdbeAddOp4(p: v, op,
133135 p1: regOldData+1+x, p2: addrJump, p3: regCmp+i, zP4: p4, P4_COLLSEQ
133136 );
133137 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
133138 VdbeCoverageIf(v, op==OP_Eq);
133139 VdbeCoverageIf(v, op==OP_Ne);
133140 }
133141 }
133142 }
133143 }
133144
133145 /* Generate code that executes if the new index entry is not unique */
133146 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
133147 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
133148 switch( onError ){
133149 case OE_Rollback:
133150 case OE_Abort:
133151 case OE_Fail: {
133152 testcase( onError==OE_Rollback );
133153 testcase( onError==OE_Abort );
133154 testcase( onError==OE_Fail );
133155 sqlite3UniqueConstraint(pParse, onError, pIdx);
133156 break;
133157 }
133158#ifndef SQLITE_OMIT_UPSERT
133159 case OE_Update: {
133160 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
133161 /* no break */ deliberate_fall_through
133162 }
133163#endif
133164 case OE_Ignore: {
133165 testcase( onError==OE_Ignore );
133166 sqlite3VdbeGoto(p: v, iDest: ignoreDest);
133167 break;
133168 }
133169 default: {
133170 int nConflictCk; /* Number of opcodes in conflict check logic */
133171
133172 assert( onError==OE_Replace );
133173 nConflictCk = sqlite3VdbeCurrentAddr(p: v) - addrConflictCk;
133174 assert( nConflictCk>0 || db->mallocFailed );
133175 testcase( nConflictCk<=0 );
133176 testcase( nConflictCk>1 );
133177 if( regTrigCnt ){
133178 sqlite3MultiWrite(pParse);
133179 nReplaceTrig++;
133180 }
133181 if( pTrigger && isUpdate ){
133182 sqlite3VdbeAddOp1(p: v, OP_CursorLock, p1: iDataCur);
133183 }
133184 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
133185 iPk: regR, nPk: nPkField, count: 0, OE_Replace,
133186 eMode: (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iIdxNoSeek: iThisCur);
133187 if( pTrigger && isUpdate ){
133188 sqlite3VdbeAddOp1(p: v, OP_CursorUnlock, p1: iDataCur);
133189 }
133190 if( regTrigCnt ){
133191 int addrBypass; /* Jump destination to bypass recheck logic */
133192
133193 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regTrigCnt, p2: 1); /* incr trigger cnt */
133194 addrBypass = sqlite3VdbeAddOp0(p: v, OP_Goto); /* Bypass recheck */
133195 VdbeComment((v, "bypass recheck"));
133196
133197 /* Here we insert code that will be invoked after all constraint
133198 ** checks have run, if and only if one or more replace triggers
133199 ** fired. */
133200 sqlite3VdbeResolveLabel(v, x: lblRecheckOk);
133201 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
133202 if( pIdx->pPartIdxWhere ){
133203 /* Bypass the recheck if this partial index is not defined
133204 ** for the current row */
133205 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regIdx-1, p2: lblRecheckOk);
133206 VdbeCoverage(v);
133207 }
133208 /* Copy the constraint check code from above, except change
133209 ** the constraint-ok jump destination to be the address of
133210 ** the next retest block */
133211 while( nConflictCk>0 ){
133212 VdbeOp x; /* Conflict check opcode to copy */
133213 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
133214 ** Hence, make a complete copy of the opcode, rather than using
133215 ** a pointer to the opcode. */
133216 x = *sqlite3VdbeGetOp(p: v, addr: addrConflictCk);
133217 if( x.opcode!=OP_IdxRowid ){
133218 int p2; /* New P2 value for copied conflict check opcode */
133219 const char *zP4;
133220 if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){
133221 p2 = lblRecheckOk;
133222 }else{
133223 p2 = x.p2;
133224 }
133225 zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z;
133226 sqlite3VdbeAddOp4(p: v, op: x.opcode, p1: x.p1, p2, p3: x.p3, zP4, p4type: x.p4type);
133227 sqlite3VdbeChangeP5(p: v, p5: x.p5);
133228 VdbeCoverageIf(v, p2!=x.p2);
133229 }
133230 nConflictCk--;
133231 addrConflictCk++;
133232 }
133233 /* If the retest fails, issue an abort */
133234 sqlite3UniqueConstraint(pParse, OE_Abort, pIdx);
133235
133236 sqlite3VdbeJumpHere(p: v, addr: addrBypass); /* Terminate the recheck bypass */
133237 }
133238 seenReplace = 1;
133239 break;
133240 }
133241 }
133242 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
133243 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, iReg: regR, nReg: nPkField);
133244 if( pUpsertClause
133245 && upsertIpkReturn
133246 && sqlite3UpsertNextIsIPK(pUpsertClause)
133247 ){
133248 sqlite3VdbeGoto(p: v, iDest: upsertIpkDelay+1);
133249 sqlite3VdbeJumpHere(p: v, addr: upsertIpkReturn);
133250 upsertIpkReturn = 0;
133251 }
133252 }
133253
133254 /* If the IPK constraint is a REPLACE, run it last */
133255 if( ipkTop ){
133256 sqlite3VdbeGoto(p: v, iDest: ipkTop);
133257 VdbeComment((v, "Do IPK REPLACE"));
133258 assert( ipkBottom>0 );
133259 sqlite3VdbeJumpHere(p: v, addr: ipkBottom);
133260 }
133261
133262 /* Recheck all uniqueness constraints after replace triggers have run */
133263 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
133264 assert( regTrigCnt!=0 || nReplaceTrig==0 );
133265 if( nReplaceTrig ){
133266 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: regTrigCnt, p2: lblRecheckOk);VdbeCoverage(v);
133267 if( !pPk ){
133268 if( isUpdate ){
133269 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regNewData, p2: addrRecheck, p3: regOldData);
133270 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
133271 VdbeCoverage(v);
133272 }
133273 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: addrRecheck, p3: regNewData);
133274 VdbeCoverage(v);
133275 sqlite3RowidConstraint(pParse, OE_Abort, pTab);
133276 }else{
133277 sqlite3VdbeGoto(p: v, iDest: addrRecheck);
133278 }
133279 sqlite3VdbeResolveLabel(v, x: lblRecheckOk);
133280 }
133281
133282 /* Generate the table record */
133283 if( HasRowid(pTab) ){
133284 int regRec = aRegIdx[ix];
133285 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regNewData+1, p2: pTab->nNVCol, p3: regRec);
133286 sqlite3SetMakeRecordP5(v, pTab);
133287 if( !bAffinityDone ){
133288 sqlite3TableAffinity(v, pTab, iReg: 0);
133289 }
133290 }
133291
133292 *pbMayReplace = seenReplace;
133293 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
133294}
133295
133296#ifdef SQLITE_ENABLE_NULL_TRIM
133297/*
133298** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
133299** to be the number of columns in table pTab that must not be NULL-trimmed.
133300**
133301** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
133302*/
133303SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
133304 u16 i;
133305
133306 /* Records with omitted columns are only allowed for schema format
133307 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
133308 if( pTab->pSchema->file_format<2 ) return;
133309
133310 for(i=pTab->nCol-1; i>0; i--){
133311 if( pTab->aCol[i].iDflt!=0 ) break;
133312 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
133313 }
133314 sqlite3VdbeChangeP5(v, i+1);
133315}
133316#endif
133317
133318/*
133319** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
133320** number is iCur, and register regData contains the new record for the
133321** PK index. This function adds code to invoke the pre-update hook,
133322** if one is registered.
133323*/
133324#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
133325static void codeWithoutRowidPreupdate(
133326 Parse *pParse, /* Parse context */
133327 Table *pTab, /* Table being updated */
133328 int iCur, /* Cursor number for table */
133329 int regData /* Data containing new record */
133330){
133331 Vdbe *v = pParse->pVdbe;
133332 int r = sqlite3GetTempReg(pParse);
133333 assert( !HasRowid(pTab) );
133334 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
133335 sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
133336 sqlite3VdbeAddOp4(v, OP_Insert, iCur, regData, r, (char*)pTab, P4_TABLE);
133337 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
133338 sqlite3ReleaseTempReg(pParse, r);
133339}
133340#else
133341# define codeWithoutRowidPreupdate(a,b,c,d)
133342#endif
133343
133344/*
133345** This routine generates code to finish the INSERT or UPDATE operation
133346** that was started by a prior call to sqlite3GenerateConstraintChecks.
133347** A consecutive range of registers starting at regNewData contains the
133348** rowid and the content to be inserted.
133349**
133350** The arguments to this routine should be the same as the first six
133351** arguments to sqlite3GenerateConstraintChecks.
133352*/
133353SQLITE_PRIVATE void sqlite3CompleteInsertion(
133354 Parse *pParse, /* The parser context */
133355 Table *pTab, /* the table into which we are inserting */
133356 int iDataCur, /* Cursor of the canonical data source */
133357 int iIdxCur, /* First index cursor */
133358 int regNewData, /* Range of content */
133359 int *aRegIdx, /* Register used by each index. 0 for unused indices */
133360 int update_flags, /* True for UPDATE, False for INSERT */
133361 int appendBias, /* True if this is likely to be an append */
133362 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
133363){
133364 Vdbe *v; /* Prepared statements under construction */
133365 Index *pIdx; /* An index being inserted or updated */
133366 u8 pik_flags; /* flag values passed to the btree insert */
133367 int i; /* Loop counter */
133368
133369 assert( update_flags==0
133370 || update_flags==OPFLAG_ISUPDATE
133371 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
133372 );
133373
133374 v = pParse->pVdbe;
133375 assert( v!=0 );
133376 assert( !IsView(pTab) ); /* This table is not a VIEW */
133377 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
133378 /* All REPLACE indexes are at the end of the list */
133379 assert( pIdx->onError!=OE_Replace
133380 || pIdx->pNext==0
133381 || pIdx->pNext->onError==OE_Replace );
133382 if( aRegIdx[i]==0 ) continue;
133383 if( pIdx->pPartIdxWhere ){
133384 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: aRegIdx[i], p2: sqlite3VdbeCurrentAddr(p: v)+2);
133385 VdbeCoverage(v);
133386 }
133387 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
133388 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
133389 pik_flags |= OPFLAG_NCHANGE;
133390 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
133391 if( update_flags==0 ){
133392 codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
133393 }
133394 }
133395 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iIdxCur+i, p2: aRegIdx[i],
133396 p3: aRegIdx[i]+1,
133397 p4: pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
133398 sqlite3VdbeChangeP5(p: v, p5: pik_flags);
133399 }
133400 if( !HasRowid(pTab) ) return;
133401 if( pParse->nested ){
133402 pik_flags = 0;
133403 }else{
133404 pik_flags = OPFLAG_NCHANGE;
133405 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
133406 }
133407 if( appendBias ){
133408 pik_flags |= OPFLAG_APPEND;
133409 }
133410 if( useSeekResult ){
133411 pik_flags |= OPFLAG_USESEEKRESULT;
133412 }
133413 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iDataCur, p2: aRegIdx[i], p3: regNewData);
133414 if( !pParse->nested ){
133415 sqlite3VdbeAppendP4(p: v, pP4: pTab, P4_TABLE);
133416 }
133417 sqlite3VdbeChangeP5(p: v, p5: pik_flags);
133418}
133419
133420/*
133421** Allocate cursors for the pTab table and all its indices and generate
133422** code to open and initialized those cursors.
133423**
133424** The cursor for the object that contains the complete data (normally
133425** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
133426** ROWID table) is returned in *piDataCur. The first index cursor is
133427** returned in *piIdxCur. The number of indices is returned.
133428**
133429** Use iBase as the first cursor (either the *piDataCur for rowid tables
133430** or the first index for WITHOUT ROWID tables) if it is non-negative.
133431** If iBase is negative, then allocate the next available cursor.
133432**
133433** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
133434** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
133435** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
133436** pTab->pIndex list.
133437**
133438** If pTab is a virtual table, then this routine is a no-op and the
133439** *piDataCur and *piIdxCur values are left uninitialized.
133440*/
133441SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
133442 Parse *pParse, /* Parsing context */
133443 Table *pTab, /* Table to be opened */
133444 int op, /* OP_OpenRead or OP_OpenWrite */
133445 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
133446 int iBase, /* Use this for the table cursor, if there is one */
133447 u8 *aToOpen, /* If not NULL: boolean for each table and index */
133448 int *piDataCur, /* Write the database source cursor number here */
133449 int *piIdxCur /* Write the first index cursor number here */
133450){
133451 int i;
133452 int iDb;
133453 int iDataCur;
133454 Index *pIdx;
133455 Vdbe *v;
133456
133457 assert( op==OP_OpenRead || op==OP_OpenWrite );
133458 assert( op==OP_OpenWrite || p5==0 );
133459 assert( piDataCur!=0 );
133460 assert( piIdxCur!=0 );
133461 if( IsVirtual(pTab) ){
133462 /* This routine is a no-op for virtual tables. Leave the output
133463 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
133464 ** for improved error detection. */
133465 *piDataCur = *piIdxCur = -999;
133466 return 0;
133467 }
133468 iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
133469 v = pParse->pVdbe;
133470 assert( v!=0 );
133471 if( iBase<0 ) iBase = pParse->nTab;
133472 iDataCur = iBase++;
133473 *piDataCur = iDataCur;
133474 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
133475 sqlite3OpenTable(pParse, iCur: iDataCur, iDb, pTab, opcode: op);
133476 }else if( pParse->db->noSharedCache==0 ){
133477 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: op==OP_OpenWrite, zName: pTab->zName);
133478 }
133479 *piIdxCur = iBase;
133480 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
133481 int iIdxCur = iBase++;
133482 assert( pIdx->pSchema==pTab->pSchema );
133483 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
133484 *piDataCur = iIdxCur;
133485 p5 = 0;
133486 }
133487 if( aToOpen==0 || aToOpen[i+1] ){
133488 sqlite3VdbeAddOp3(p: v, op, p1: iIdxCur, p2: pIdx->tnum, p3: iDb);
133489 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
133490 sqlite3VdbeChangeP5(p: v, p5);
133491 VdbeComment((v, "%s", pIdx->zName));
133492 }
133493 }
133494 if( iBase>pParse->nTab ) pParse->nTab = iBase;
133495 return i;
133496}
133497
133498
133499#ifdef SQLITE_TEST
133500/*
133501** The following global variable is incremented whenever the
133502** transfer optimization is used. This is used for testing
133503** purposes only - to make sure the transfer optimization really
133504** is happening when it is supposed to.
133505*/
133506SQLITE_API int sqlite3_xferopt_count;
133507#endif /* SQLITE_TEST */
133508
133509
133510#ifndef SQLITE_OMIT_XFER_OPT
133511/*
133512** Check to see if index pSrc is compatible as a source of data
133513** for index pDest in an insert transfer optimization. The rules
133514** for a compatible index:
133515**
133516** * The index is over the same set of columns
133517** * The same DESC and ASC markings occurs on all columns
133518** * The same onError processing (OE_Abort, OE_Ignore, etc)
133519** * The same collating sequence on each column
133520** * The index has the exact same WHERE clause
133521*/
133522static int xferCompatibleIndex(Index *pDest, Index *pSrc){
133523 int i;
133524 assert( pDest && pSrc );
133525 assert( pDest->pTable!=pSrc->pTable );
133526 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
133527 return 0; /* Different number of columns */
133528 }
133529 if( pDest->onError!=pSrc->onError ){
133530 return 0; /* Different conflict resolution strategies */
133531 }
133532 for(i=0; i<pSrc->nKeyCol; i++){
133533 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
133534 return 0; /* Different columns indexed */
133535 }
133536 if( pSrc->aiColumn[i]==XN_EXPR ){
133537 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
133538 if( sqlite3ExprCompare(pParse: 0, pA: pSrc->aColExpr->a[i].pExpr,
133539 pB: pDest->aColExpr->a[i].pExpr, iTab: -1)!=0 ){
133540 return 0; /* Different expressions in the index */
133541 }
133542 }
133543 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
133544 return 0; /* Different sort orders */
133545 }
133546 if( sqlite3_stricmp(zLeft: pSrc->azColl[i],zRight: pDest->azColl[i])!=0 ){
133547 return 0; /* Different collating sequences */
133548 }
133549 }
133550 if( sqlite3ExprCompare(pParse: 0, pA: pSrc->pPartIdxWhere, pB: pDest->pPartIdxWhere, iTab: -1) ){
133551 return 0; /* Different WHERE clauses */
133552 }
133553
133554 /* If no test above fails then the indices must be compatible */
133555 return 1;
133556}
133557
133558/*
133559** Attempt the transfer optimization on INSERTs of the form
133560**
133561** INSERT INTO tab1 SELECT * FROM tab2;
133562**
133563** The xfer optimization transfers raw records from tab2 over to tab1.
133564** Columns are not decoded and reassembled, which greatly improves
133565** performance. Raw index records are transferred in the same way.
133566**
133567** The xfer optimization is only attempted if tab1 and tab2 are compatible.
133568** There are lots of rules for determining compatibility - see comments
133569** embedded in the code for details.
133570**
133571** This routine returns TRUE if the optimization is guaranteed to be used.
133572** Sometimes the xfer optimization will only work if the destination table
133573** is empty - a factor that can only be determined at run-time. In that
133574** case, this routine generates code for the xfer optimization but also
133575** does a test to see if the destination table is empty and jumps over the
133576** xfer optimization code if the test fails. In that case, this routine
133577** returns FALSE so that the caller will know to go ahead and generate
133578** an unoptimized transfer. This routine also returns FALSE if there
133579** is no chance that the xfer optimization can be applied.
133580**
133581** This optimization is particularly useful at making VACUUM run faster.
133582*/
133583static int xferOptimization(
133584 Parse *pParse, /* Parser context */
133585 Table *pDest, /* The table we are inserting into */
133586 Select *pSelect, /* A SELECT statement to use as the data source */
133587 int onError, /* How to handle constraint errors */
133588 int iDbDest /* The database of pDest */
133589){
133590 sqlite3 *db = pParse->db;
133591 ExprList *pEList; /* The result set of the SELECT */
133592 Table *pSrc; /* The table in the FROM clause of SELECT */
133593 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
133594 SrcItem *pItem; /* An element of pSelect->pSrc */
133595 int i; /* Loop counter */
133596 int iDbSrc; /* The database of pSrc */
133597 int iSrc, iDest; /* Cursors from source and destination */
133598 int addr1, addr2; /* Loop addresses */
133599 int emptyDestTest = 0; /* Address of test for empty pDest */
133600 int emptySrcTest = 0; /* Address of test for empty pSrc */
133601 Vdbe *v; /* The VDBE we are building */
133602 int regAutoinc; /* Memory register used by AUTOINC */
133603 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
133604 int regData, regRowid; /* Registers holding data and rowid */
133605
133606 assert( pSelect!=0 );
133607 if( pParse->pWith || pSelect->pWith ){
133608 /* Do not attempt to process this query if there are an WITH clauses
133609 ** attached to it. Proceeding may generate a false "no such table: xxx"
133610 ** error if pSelect reads from a CTE named "xxx". */
133611 return 0;
133612 }
133613#ifndef SQLITE_OMIT_VIRTUALTABLE
133614 if( IsVirtual(pDest) ){
133615 return 0; /* tab1 must not be a virtual table */
133616 }
133617#endif
133618 if( onError==OE_Default ){
133619 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
133620 if( onError==OE_Default ) onError = OE_Abort;
133621 }
133622 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
133623 if( pSelect->pSrc->nSrc!=1 ){
133624 return 0; /* FROM clause must have exactly one term */
133625 }
133626 if( pSelect->pSrc->a[0].pSelect ){
133627 return 0; /* FROM clause cannot contain a subquery */
133628 }
133629 if( pSelect->pWhere ){
133630 return 0; /* SELECT may not have a WHERE clause */
133631 }
133632 if( pSelect->pOrderBy ){
133633 return 0; /* SELECT may not have an ORDER BY clause */
133634 }
133635 /* Do not need to test for a HAVING clause. If HAVING is present but
133636 ** there is no ORDER BY, we will get an error. */
133637 if( pSelect->pGroupBy ){
133638 return 0; /* SELECT may not have a GROUP BY clause */
133639 }
133640 if( pSelect->pLimit ){
133641 return 0; /* SELECT may not have a LIMIT clause */
133642 }
133643 if( pSelect->pPrior ){
133644 return 0; /* SELECT may not be a compound query */
133645 }
133646 if( pSelect->selFlags & SF_Distinct ){
133647 return 0; /* SELECT may not be DISTINCT */
133648 }
133649 pEList = pSelect->pEList;
133650 assert( pEList!=0 );
133651 if( pEList->nExpr!=1 ){
133652 return 0; /* The result set must have exactly one column */
133653 }
133654 assert( pEList->a[0].pExpr );
133655 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
133656 return 0; /* The result set must be the special operator "*" */
133657 }
133658
133659 /* At this point we have established that the statement is of the
133660 ** correct syntactic form to participate in this optimization. Now
133661 ** we have to check the semantics.
133662 */
133663 pItem = pSelect->pSrc->a;
133664 pSrc = sqlite3LocateTableItem(pParse, flags: 0, p: pItem);
133665 if( pSrc==0 ){
133666 return 0; /* FROM clause does not contain a real table */
133667 }
133668 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
133669 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
133670 return 0; /* tab1 and tab2 may not be the same table */
133671 }
133672 if( HasRowid(pDest)!=HasRowid(pSrc) ){
133673 return 0; /* source and destination must both be WITHOUT ROWID or not */
133674 }
133675 if( !IsOrdinaryTable(pSrc) ){
133676 return 0; /* tab2 may not be a view or virtual table */
133677 }
133678 if( pDest->nCol!=pSrc->nCol ){
133679 return 0; /* Number of columns must be the same in tab1 and tab2 */
133680 }
133681 if( pDest->iPKey!=pSrc->iPKey ){
133682 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
133683 }
133684 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
133685 return 0; /* Cannot feed from a non-strict into a strict table */
133686 }
133687 for(i=0; i<pDest->nCol; i++){
133688 Column *pDestCol = &pDest->aCol[i];
133689 Column *pSrcCol = &pSrc->aCol[i];
133690#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
133691 if( (db->mDbFlags & DBFLAG_Vacuum)==0
133692 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
133693 ){
133694 return 0; /* Neither table may have __hidden__ columns */
133695 }
133696#endif
133697#ifndef SQLITE_OMIT_GENERATED_COLUMNS
133698 /* Even if tables t1 and t2 have identical schemas, if they contain
133699 ** generated columns, then this statement is semantically incorrect:
133700 **
133701 ** INSERT INTO t2 SELECT * FROM t1;
133702 **
133703 ** The reason is that generated column values are returned by the
133704 ** the SELECT statement on the right but the INSERT statement on the
133705 ** left wants them to be omitted.
133706 **
133707 ** Nevertheless, this is a useful notational shorthand to tell SQLite
133708 ** to do a bulk transfer all of the content from t1 over to t2.
133709 **
133710 ** We could, in theory, disable this (except for internal use by the
133711 ** VACUUM command where it is actually needed). But why do that? It
133712 ** seems harmless enough, and provides a useful service.
133713 */
133714 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
133715 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
133716 return 0; /* Both columns have the same generated-column type */
133717 }
133718 /* But the transfer is only allowed if both the source and destination
133719 ** tables have the exact same expressions for generated columns.
133720 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
133721 */
133722 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
133723 if( sqlite3ExprCompare(pParse: 0,
133724 pA: sqlite3ColumnExpr(pTab: pSrc, pCol: pSrcCol),
133725 pB: sqlite3ColumnExpr(pTab: pDest, pCol: pDestCol), iTab: -1)!=0 ){
133726 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
133727 testcase( pDestCol->colFlags & COLFLAG_STORED );
133728 return 0; /* Different generator expressions */
133729 }
133730 }
133731#endif
133732 if( pDestCol->affinity!=pSrcCol->affinity ){
133733 return 0; /* Affinity must be the same on all columns */
133734 }
133735 if( sqlite3_stricmp(zLeft: sqlite3ColumnColl(pCol: pDestCol),
133736 zRight: sqlite3ColumnColl(pCol: pSrcCol))!=0 ){
133737 return 0; /* Collating sequence must be the same on all columns */
133738 }
133739 if( pDestCol->notNull && !pSrcCol->notNull ){
133740 return 0; /* tab2 must be NOT NULL if tab1 is */
133741 }
133742 /* Default values for second and subsequent columns need to match. */
133743 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
133744 Expr *pDestExpr = sqlite3ColumnExpr(pTab: pDest, pCol: pDestCol);
133745 Expr *pSrcExpr = sqlite3ColumnExpr(pTab: pSrc, pCol: pSrcCol);
133746 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
133747 assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) );
133748 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
133749 assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) );
133750 if( (pDestExpr==0)!=(pSrcExpr==0)
133751 || (pDestExpr!=0 && strcmp(s1: pDestExpr->u.zToken,
133752 s2: pSrcExpr->u.zToken)!=0)
133753 ){
133754 return 0; /* Default values must be the same for all columns */
133755 }
133756 }
133757 }
133758 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
133759 if( IsUniqueIndex(pDestIdx) ){
133760 destHasUniqueIdx = 1;
133761 }
133762 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
133763 if( xferCompatibleIndex(pDest: pDestIdx, pSrc: pSrcIdx) ) break;
133764 }
133765 if( pSrcIdx==0 ){
133766 return 0; /* pDestIdx has no corresponding index in pSrc */
133767 }
133768 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
133769 && sqlite3FaultSim(iTest: 411)==SQLITE_OK ){
133770 /* The sqlite3FaultSim() call allows this corruption test to be
133771 ** bypassed during testing, in order to exercise other corruption tests
133772 ** further downstream. */
133773 return 0; /* Corrupt schema - two indexes on the same btree */
133774 }
133775 }
133776#ifndef SQLITE_OMIT_CHECK
133777 if( pDest->pCheck && sqlite3ExprListCompare(pA: pSrc->pCheck,pB: pDest->pCheck,iTab: -1) ){
133778 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
133779 }
133780#endif
133781#ifndef SQLITE_OMIT_FOREIGN_KEY
133782 /* Disallow the transfer optimization if the destination table contains
133783 ** any foreign key constraints. This is more restrictive than necessary.
133784 ** But the main beneficiary of the transfer optimization is the VACUUM
133785 ** command, and the VACUUM command disables foreign key constraints. So
133786 ** the extra complication to make this rule less restrictive is probably
133787 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
133788 */
133789 assert( IsOrdinaryTable(pDest) );
133790 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
133791 return 0;
133792 }
133793#endif
133794 if( (db->flags & SQLITE_CountRows)!=0 ){
133795 return 0; /* xfer opt does not play well with PRAGMA count_changes */
133796 }
133797
133798 /* If we get this far, it means that the xfer optimization is at
133799 ** least a possibility, though it might only work if the destination
133800 ** table (tab1) is initially empty.
133801 */
133802#ifdef SQLITE_TEST
133803 sqlite3_xferopt_count++;
133804#endif
133805 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
133806 v = sqlite3GetVdbe(pParse);
133807 sqlite3CodeVerifySchema(pParse, iDb: iDbSrc);
133808 iSrc = pParse->nTab++;
133809 iDest = pParse->nTab++;
133810 regAutoinc = autoIncBegin(pParse, iDb: iDbDest, pTab: pDest);
133811 regData = sqlite3GetTempReg(pParse);
133812 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regData);
133813 regRowid = sqlite3GetTempReg(pParse);
133814 sqlite3OpenTable(pParse, iCur: iDest, iDb: iDbDest, pTab: pDest, OP_OpenWrite);
133815 assert( HasRowid(pDest) || destHasUniqueIdx );
133816 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
133817 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
133818 || destHasUniqueIdx /* (2) */
133819 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
133820 )){
133821 /* In some circumstances, we are able to run the xfer optimization
133822 ** only if the destination table is initially empty. Unless the
133823 ** DBFLAG_Vacuum flag is set, this block generates code to make
133824 ** that determination. If DBFLAG_Vacuum is set, then the destination
133825 ** table is always empty.
133826 **
133827 ** Conditions under which the destination must be empty:
133828 **
133829 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
133830 ** (If the destination is not initially empty, the rowid fields
133831 ** of index entries might need to change.)
133832 **
133833 ** (2) The destination has a unique index. (The xfer optimization
133834 ** is unable to test uniqueness.)
133835 **
133836 ** (3) onError is something other than OE_Abort and OE_Rollback.
133837 */
133838 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iDest, p2: 0); VdbeCoverage(v);
133839 emptyDestTest = sqlite3VdbeAddOp0(p: v, OP_Goto);
133840 sqlite3VdbeJumpHere(p: v, addr: addr1);
133841 }
133842 if( HasRowid(pSrc) ){
133843 u8 insFlags;
133844 sqlite3OpenTable(pParse, iCur: iSrc, iDb: iDbSrc, pTab: pSrc, OP_OpenRead);
133845 emptySrcTest = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iSrc, p2: 0); VdbeCoverage(v);
133846 if( pDest->iPKey>=0 ){
133847 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iSrc, p2: regRowid);
133848 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
133849 sqlite3VdbeVerifyAbortable(v, onError);
133850 addr2 = sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDest, p2: 0, p3: regRowid);
133851 VdbeCoverage(v);
133852 sqlite3RowidConstraint(pParse, onError, pTab: pDest);
133853 sqlite3VdbeJumpHere(p: v, addr: addr2);
133854 }
133855 autoIncStep(pParse, memId: regAutoinc, regRowid);
133856 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
133857 addr1 = sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iDest, p2: regRowid);
133858 }else{
133859 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iSrc, p2: regRowid);
133860 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
133861 }
133862
133863 if( db->mDbFlags & DBFLAG_Vacuum ){
133864 sqlite3VdbeAddOp1(p: v, OP_SeekEnd, p1: iDest);
133865 insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
133866 }else{
133867 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT;
133868 }
133869#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
133870 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
133871 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
133872 insFlags &= ~OPFLAG_PREFORMAT;
133873 }else
133874#endif
133875 {
133876 sqlite3VdbeAddOp3(p: v, OP_RowCell, p1: iDest, p2: iSrc, p3: regRowid);
133877 }
133878 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iDest, p2: regData, p3: regRowid);
133879 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
133880 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char*)pDest, P4_TABLE);
133881 }
133882 sqlite3VdbeChangeP5(p: v, p5: insFlags);
133883
133884 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iSrc, p2: addr1); VdbeCoverage(v);
133885 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iSrc, p2: 0);
133886 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iDest, p2: 0);
133887 }else{
133888 sqlite3TableLock(pParse, iDb: iDbDest, iTab: pDest->tnum, isWriteLock: 1, zName: pDest->zName);
133889 sqlite3TableLock(pParse, iDb: iDbSrc, iTab: pSrc->tnum, isWriteLock: 0, zName: pSrc->zName);
133890 }
133891 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
133892 u8 idxInsFlags = 0;
133893 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
133894 if( xferCompatibleIndex(pDest: pDestIdx, pSrc: pSrcIdx) ) break;
133895 }
133896 assert( pSrcIdx );
133897 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iSrc, p2: pSrcIdx->tnum, p3: iDbSrc);
133898 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pSrcIdx);
133899 VdbeComment((v, "%s", pSrcIdx->zName));
133900 sqlite3VdbeAddOp3(p: v, OP_OpenWrite, p1: iDest, p2: pDestIdx->tnum, p3: iDbDest);
133901 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pDestIdx);
133902 sqlite3VdbeChangeP5(p: v, OPFLAG_BULKCSR);
133903 VdbeComment((v, "%s", pDestIdx->zName));
133904 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iSrc, p2: 0); VdbeCoverage(v);
133905 if( db->mDbFlags & DBFLAG_Vacuum ){
133906 /* This INSERT command is part of a VACUUM operation, which guarantees
133907 ** that the destination table is empty. If all indexed columns use
133908 ** collation sequence BINARY, then it can also be assumed that the
133909 ** index will be populated by inserting keys in strictly sorted
133910 ** order. In this case, instead of seeking within the b-tree as part
133911 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
133912 ** OP_IdxInsert to seek to the point within the b-tree where each key
133913 ** should be inserted. This is faster.
133914 **
133915 ** If any of the indexed columns use a collation sequence other than
133916 ** BINARY, this optimization is disabled. This is because the user
133917 ** might change the definition of a collation sequence and then run
133918 ** a VACUUM command. In that case keys may not be written in strictly
133919 ** sorted order. */
133920 for(i=0; i<pSrcIdx->nColumn; i++){
133921 const char *zColl = pSrcIdx->azColl[i];
133922 if( sqlite3_stricmp(zLeft: sqlite3StrBINARY, zRight: zColl) ) break;
133923 }
133924 if( i==pSrcIdx->nColumn ){
133925 idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
133926 sqlite3VdbeAddOp1(p: v, OP_SeekEnd, p1: iDest);
133927 sqlite3VdbeAddOp2(p: v, OP_RowCell, p1: iDest, p2: iSrc);
133928 }
133929 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
133930 idxInsFlags |= OPFLAG_NCHANGE;
133931 }
133932 if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
133933 sqlite3VdbeAddOp3(p: v, OP_RowData, p1: iSrc, p2: regData, p3: 1);
133934 if( (db->mDbFlags & DBFLAG_Vacuum)==0
133935 && !HasRowid(pDest)
133936 && IsPrimaryKeyIndex(pDestIdx)
133937 ){
133938 codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
133939 }
133940 }
133941 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: iDest, p2: regData);
133942 sqlite3VdbeChangeP5(p: v, p5: idxInsFlags|OPFLAG_APPEND);
133943 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iSrc, p2: addr1+1); VdbeCoverage(v);
133944 sqlite3VdbeJumpHere(p: v, addr: addr1);
133945 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iSrc, p2: 0);
133946 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iDest, p2: 0);
133947 }
133948 if( emptySrcTest ) sqlite3VdbeJumpHere(p: v, addr: emptySrcTest);
133949 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
133950 sqlite3ReleaseTempReg(pParse, iReg: regData);
133951 if( emptyDestTest ){
133952 sqlite3AutoincrementEnd(pParse);
133953 sqlite3VdbeAddOp2(p: v, OP_Halt, SQLITE_OK, p2: 0);
133954 sqlite3VdbeJumpHere(p: v, addr: emptyDestTest);
133955 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iDest, p2: 0);
133956 return 0;
133957 }else{
133958 return 1;
133959 }
133960}
133961#endif /* SQLITE_OMIT_XFER_OPT */
133962
133963/************** End of insert.c **********************************************/
133964/************** Begin file legacy.c ******************************************/
133965/*
133966** 2001 September 15
133967**
133968** The author disclaims copyright to this source code. In place of
133969** a legal notice, here is a blessing:
133970**
133971** May you do good and not evil.
133972** May you find forgiveness for yourself and forgive others.
133973** May you share freely, never taking more than you give.
133974**
133975*************************************************************************
133976** Main file for the SQLite library. The routines in this file
133977** implement the programmer interface to the library. Routines in
133978** other files are for internal use by SQLite and should not be
133979** accessed by users of the library.
133980*/
133981
133982/* #include "sqliteInt.h" */
133983
133984/*
133985** Execute SQL code. Return one of the SQLITE_ success/failure
133986** codes. Also write an error message into memory obtained from
133987** malloc() and make *pzErrMsg point to that message.
133988**
133989** If the SQL is a query, then for each row in the query result
133990** the xCallback() function is called. pArg becomes the first
133991** argument to xCallback(). If xCallback=NULL then no callback
133992** is invoked, even for queries.
133993*/
133994SQLITE_API int sqlite3_exec(
133995 sqlite3 *db, /* The database on which the SQL executes */
133996 const char *zSql, /* The SQL to be executed */
133997 sqlite3_callback xCallback, /* Invoke this callback routine */
133998 void *pArg, /* First argument to xCallback() */
133999 char **pzErrMsg /* Write error messages here */
134000){
134001 int rc = SQLITE_OK; /* Return code */
134002 const char *zLeftover; /* Tail of unprocessed SQL */
134003 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
134004 char **azCols = 0; /* Names of result columns */
134005 int callbackIsInit; /* True if callback data is initialized */
134006
134007 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
134008 if( zSql==0 ) zSql = "";
134009
134010 sqlite3_mutex_enter(p: db->mutex);
134011 sqlite3Error(db, SQLITE_OK);
134012 while( rc==SQLITE_OK && zSql[0] ){
134013 int nCol = 0;
134014 char **azVals = 0;
134015
134016 pStmt = 0;
134017 rc = sqlite3_prepare_v2(db, zSql, nByte: -1, ppStmt: &pStmt, pzTail: &zLeftover);
134018 assert( rc==SQLITE_OK || pStmt==0 );
134019 if( rc!=SQLITE_OK ){
134020 continue;
134021 }
134022 if( !pStmt ){
134023 /* this happens for a comment or white-space */
134024 zSql = zLeftover;
134025 continue;
134026 }
134027 callbackIsInit = 0;
134028
134029 while( 1 ){
134030 int i;
134031 rc = sqlite3_step(pStmt);
134032
134033 /* Invoke the callback function if required */
134034 if( xCallback && (SQLITE_ROW==rc ||
134035 (SQLITE_DONE==rc && !callbackIsInit
134036 && db->flags&SQLITE_NullCallback)) ){
134037 if( !callbackIsInit ){
134038 nCol = sqlite3_column_count(pStmt);
134039 azCols = sqlite3DbMallocRaw(db, n: (2*nCol+1)*sizeof(const char*));
134040 if( azCols==0 ){
134041 goto exec_out;
134042 }
134043 for(i=0; i<nCol; i++){
134044 azCols[i] = (char *)sqlite3_column_name(pStmt, N: i);
134045 /* sqlite3VdbeSetColName() installs column names as UTF8
134046 ** strings so there is no way for sqlite3_column_name() to fail. */
134047 assert( azCols[i]!=0 );
134048 }
134049 callbackIsInit = 1;
134050 }
134051 if( rc==SQLITE_ROW ){
134052 azVals = &azCols[nCol];
134053 for(i=0; i<nCol; i++){
134054 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
134055 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
134056 sqlite3OomFault(db);
134057 goto exec_out;
134058 }
134059 }
134060 azVals[i] = 0;
134061 }
134062 if( xCallback(pArg, nCol, azVals, azCols) ){
134063 /* EVIDENCE-OF: R-38229-40159 If the callback function to
134064 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
134065 ** return SQLITE_ABORT. */
134066 rc = SQLITE_ABORT;
134067 sqlite3VdbeFinalize(p: (Vdbe *)pStmt);
134068 pStmt = 0;
134069 sqlite3Error(db, SQLITE_ABORT);
134070 goto exec_out;
134071 }
134072 }
134073
134074 if( rc!=SQLITE_ROW ){
134075 rc = sqlite3VdbeFinalize(p: (Vdbe *)pStmt);
134076 pStmt = 0;
134077 zSql = zLeftover;
134078 while( sqlite3Isspace(zSql[0]) ) zSql++;
134079 break;
134080 }
134081 }
134082
134083 sqlite3DbFree(db, p: azCols);
134084 azCols = 0;
134085 }
134086
134087exec_out:
134088 if( pStmt ) sqlite3VdbeFinalize(p: (Vdbe *)pStmt);
134089 sqlite3DbFree(db, p: azCols);
134090
134091 rc = sqlite3ApiExit(db, rc);
134092 if( rc!=SQLITE_OK && pzErrMsg ){
134093 *pzErrMsg = sqlite3DbStrDup(db: 0, z: sqlite3_errmsg(db));
134094 if( *pzErrMsg==0 ){
134095 rc = SQLITE_NOMEM_BKPT;
134096 sqlite3Error(db, SQLITE_NOMEM);
134097 }
134098 }else if( pzErrMsg ){
134099 *pzErrMsg = 0;
134100 }
134101
134102 assert( (rc&db->errMask)==rc );
134103 sqlite3_mutex_leave(p: db->mutex);
134104 return rc;
134105}
134106
134107/************** End of legacy.c **********************************************/
134108/************** Begin file loadext.c *****************************************/
134109/*
134110** 2006 June 7
134111**
134112** The author disclaims copyright to this source code. In place of
134113** a legal notice, here is a blessing:
134114**
134115** May you do good and not evil.
134116** May you find forgiveness for yourself and forgive others.
134117** May you share freely, never taking more than you give.
134118**
134119*************************************************************************
134120** This file contains code used to dynamically load extensions into
134121** the SQLite library.
134122*/
134123
134124#ifndef SQLITE_CORE
134125 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
134126#endif
134127/************** Include sqlite3ext.h in the middle of loadext.c **************/
134128/************** Begin file sqlite3ext.h **************************************/
134129/*
134130** 2006 June 7
134131**
134132** The author disclaims copyright to this source code. In place of
134133** a legal notice, here is a blessing:
134134**
134135** May you do good and not evil.
134136** May you find forgiveness for yourself and forgive others.
134137** May you share freely, never taking more than you give.
134138**
134139*************************************************************************
134140** This header file defines the SQLite interface for use by
134141** shared libraries that want to be imported as extensions into
134142** an SQLite instance. Shared libraries that intend to be loaded
134143** as extensions by SQLite should #include this file instead of
134144** sqlite3.h.
134145*/
134146#ifndef SQLITE3EXT_H
134147#define SQLITE3EXT_H
134148/* #include "sqlite3.h" */
134149
134150/*
134151** The following structure holds pointers to all of the SQLite API
134152** routines.
134153**
134154** WARNING: In order to maintain backwards compatibility, add new
134155** interfaces to the end of this structure only. If you insert new
134156** interfaces in the middle of this structure, then older different
134157** versions of SQLite will not be able to load each other's shared
134158** libraries!
134159*/
134160struct sqlite3_api_routines {
134161 void * (*aggregate_context)(sqlite3_context*,int nBytes);
134162 int (*aggregate_count)(sqlite3_context*);
134163 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
134164 int (*bind_double)(sqlite3_stmt*,int,double);
134165 int (*bind_int)(sqlite3_stmt*,int,int);
134166 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
134167 int (*bind_null)(sqlite3_stmt*,int);
134168 int (*bind_parameter_count)(sqlite3_stmt*);
134169 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
134170 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
134171 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
134172 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
134173 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
134174 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
134175 int (*busy_timeout)(sqlite3*,int ms);
134176 int (*changes)(sqlite3*);
134177 int (*close)(sqlite3*);
134178 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
134179 int eTextRep,const char*));
134180 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
134181 int eTextRep,const void*));
134182 const void * (*column_blob)(sqlite3_stmt*,int iCol);
134183 int (*column_bytes)(sqlite3_stmt*,int iCol);
134184 int (*column_bytes16)(sqlite3_stmt*,int iCol);
134185 int (*column_count)(sqlite3_stmt*pStmt);
134186 const char * (*column_database_name)(sqlite3_stmt*,int);
134187 const void * (*column_database_name16)(sqlite3_stmt*,int);
134188 const char * (*column_decltype)(sqlite3_stmt*,int i);
134189 const void * (*column_decltype16)(sqlite3_stmt*,int);
134190 double (*column_double)(sqlite3_stmt*,int iCol);
134191 int (*column_int)(sqlite3_stmt*,int iCol);
134192 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
134193 const char * (*column_name)(sqlite3_stmt*,int);
134194 const void * (*column_name16)(sqlite3_stmt*,int);
134195 const char * (*column_origin_name)(sqlite3_stmt*,int);
134196 const void * (*column_origin_name16)(sqlite3_stmt*,int);
134197 const char * (*column_table_name)(sqlite3_stmt*,int);
134198 const void * (*column_table_name16)(sqlite3_stmt*,int);
134199 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
134200 const void * (*column_text16)(sqlite3_stmt*,int iCol);
134201 int (*column_type)(sqlite3_stmt*,int iCol);
134202 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
134203 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
134204 int (*complete)(const char*sql);
134205 int (*complete16)(const void*sql);
134206 int (*create_collation)(sqlite3*,const char*,int,void*,
134207 int(*)(void*,int,const void*,int,const void*));
134208 int (*create_collation16)(sqlite3*,const void*,int,void*,
134209 int(*)(void*,int,const void*,int,const void*));
134210 int (*create_function)(sqlite3*,const char*,int,int,void*,
134211 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
134212 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
134213 void (*xFinal)(sqlite3_context*));
134214 int (*create_function16)(sqlite3*,const void*,int,int,void*,
134215 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
134216 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
134217 void (*xFinal)(sqlite3_context*));
134218 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
134219 int (*data_count)(sqlite3_stmt*pStmt);
134220 sqlite3 * (*db_handle)(sqlite3_stmt*);
134221 int (*declare_vtab)(sqlite3*,const char*);
134222 int (*enable_shared_cache)(int);
134223 int (*errcode)(sqlite3*db);
134224 const char * (*errmsg)(sqlite3*);
134225 const void * (*errmsg16)(sqlite3*);
134226 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
134227 int (*expired)(sqlite3_stmt*);
134228 int (*finalize)(sqlite3_stmt*pStmt);
134229 void (*free)(void*);
134230 void (*free_table)(char**result);
134231 int (*get_autocommit)(sqlite3*);
134232 void * (*get_auxdata)(sqlite3_context*,int);
134233 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
134234 int (*global_recover)(void);
134235 void (*interruptx)(sqlite3*);
134236 sqlite_int64 (*last_insert_rowid)(sqlite3*);
134237 const char * (*libversion)(void);
134238 int (*libversion_number)(void);
134239 void *(*malloc)(int);
134240 char * (*mprintf)(const char*,...);
134241 int (*open)(const char*,sqlite3**);
134242 int (*open16)(const void*,sqlite3**);
134243 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
134244 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
134245 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
134246 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
134247 void *(*realloc)(void*,int);
134248 int (*reset)(sqlite3_stmt*pStmt);
134249 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
134250 void (*result_double)(sqlite3_context*,double);
134251 void (*result_error)(sqlite3_context*,const char*,int);
134252 void (*result_error16)(sqlite3_context*,const void*,int);
134253 void (*result_int)(sqlite3_context*,int);
134254 void (*result_int64)(sqlite3_context*,sqlite_int64);
134255 void (*result_null)(sqlite3_context*);
134256 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
134257 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
134258 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
134259 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
134260 void (*result_value)(sqlite3_context*,sqlite3_value*);
134261 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
134262 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
134263 const char*,const char*),void*);
134264 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
134265 char * (*xsnprintf)(int,char*,const char*,...);
134266 int (*step)(sqlite3_stmt*);
134267 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
134268 char const**,char const**,int*,int*,int*);
134269 void (*thread_cleanup)(void);
134270 int (*total_changes)(sqlite3*);
134271 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
134272 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
134273 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
134274 sqlite_int64),void*);
134275 void * (*user_data)(sqlite3_context*);
134276 const void * (*value_blob)(sqlite3_value*);
134277 int (*value_bytes)(sqlite3_value*);
134278 int (*value_bytes16)(sqlite3_value*);
134279 double (*value_double)(sqlite3_value*);
134280 int (*value_int)(sqlite3_value*);
134281 sqlite_int64 (*value_int64)(sqlite3_value*);
134282 int (*value_numeric_type)(sqlite3_value*);
134283 const unsigned char * (*value_text)(sqlite3_value*);
134284 const void * (*value_text16)(sqlite3_value*);
134285 const void * (*value_text16be)(sqlite3_value*);
134286 const void * (*value_text16le)(sqlite3_value*);
134287 int (*value_type)(sqlite3_value*);
134288 char *(*vmprintf)(const char*,va_list);
134289 /* Added ??? */
134290 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
134291 /* Added by 3.3.13 */
134292 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
134293 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
134294 int (*clear_bindings)(sqlite3_stmt*);
134295 /* Added by 3.4.1 */
134296 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
134297 void (*xDestroy)(void *));
134298 /* Added by 3.5.0 */
134299 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
134300 int (*blob_bytes)(sqlite3_blob*);
134301 int (*blob_close)(sqlite3_blob*);
134302 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
134303 int,sqlite3_blob**);
134304 int (*blob_read)(sqlite3_blob*,void*,int,int);
134305 int (*blob_write)(sqlite3_blob*,const void*,int,int);
134306 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
134307 int(*)(void*,int,const void*,int,const void*),
134308 void(*)(void*));
134309 int (*file_control)(sqlite3*,const char*,int,void*);
134310 sqlite3_int64 (*memory_highwater)(int);
134311 sqlite3_int64 (*memory_used)(void);
134312 sqlite3_mutex *(*mutex_alloc)(int);
134313 void (*mutex_enter)(sqlite3_mutex*);
134314 void (*mutex_free)(sqlite3_mutex*);
134315 void (*mutex_leave)(sqlite3_mutex*);
134316 int (*mutex_try)(sqlite3_mutex*);
134317 int (*open_v2)(const char*,sqlite3**,int,const char*);
134318 int (*release_memory)(int);
134319 void (*result_error_nomem)(sqlite3_context*);
134320 void (*result_error_toobig)(sqlite3_context*);
134321 int (*sleep)(int);
134322 void (*soft_heap_limit)(int);
134323 sqlite3_vfs *(*vfs_find)(const char*);
134324 int (*vfs_register)(sqlite3_vfs*,int);
134325 int (*vfs_unregister)(sqlite3_vfs*);
134326 int (*xthreadsafe)(void);
134327 void (*result_zeroblob)(sqlite3_context*,int);
134328 void (*result_error_code)(sqlite3_context*,int);
134329 int (*test_control)(int, ...);
134330 void (*randomness)(int,void*);
134331 sqlite3 *(*context_db_handle)(sqlite3_context*);
134332 int (*extended_result_codes)(sqlite3*,int);
134333 int (*limit)(sqlite3*,int,int);
134334 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
134335 const char *(*sql)(sqlite3_stmt*);
134336 int (*status)(int,int*,int*,int);
134337 int (*backup_finish)(sqlite3_backup*);
134338 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
134339 int (*backup_pagecount)(sqlite3_backup*);
134340 int (*backup_remaining)(sqlite3_backup*);
134341 int (*backup_step)(sqlite3_backup*,int);
134342 const char *(*compileoption_get)(int);
134343 int (*compileoption_used)(const char*);
134344 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
134345 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
134346 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
134347 void (*xFinal)(sqlite3_context*),
134348 void(*xDestroy)(void*));
134349 int (*db_config)(sqlite3*,int,...);
134350 sqlite3_mutex *(*db_mutex)(sqlite3*);
134351 int (*db_status)(sqlite3*,int,int*,int*,int);
134352 int (*extended_errcode)(sqlite3*);
134353 void (*log)(int,const char*,...);
134354 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
134355 const char *(*sourceid)(void);
134356 int (*stmt_status)(sqlite3_stmt*,int,int);
134357 int (*strnicmp)(const char*,const char*,int);
134358 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
134359 int (*wal_autocheckpoint)(sqlite3*,int);
134360 int (*wal_checkpoint)(sqlite3*,const char*);
134361 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
134362 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
134363 int (*vtab_config)(sqlite3*,int op,...);
134364 int (*vtab_on_conflict)(sqlite3*);
134365 /* Version 3.7.16 and later */
134366 int (*close_v2)(sqlite3*);
134367 const char *(*db_filename)(sqlite3*,const char*);
134368 int (*db_readonly)(sqlite3*,const char*);
134369 int (*db_release_memory)(sqlite3*);
134370 const char *(*errstr)(int);
134371 int (*stmt_busy)(sqlite3_stmt*);
134372 int (*stmt_readonly)(sqlite3_stmt*);
134373 int (*stricmp)(const char*,const char*);
134374 int (*uri_boolean)(const char*,const char*,int);
134375 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
134376 const char *(*uri_parameter)(const char*,const char*);
134377 char *(*xvsnprintf)(int,char*,const char*,va_list);
134378 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
134379 /* Version 3.8.7 and later */
134380 int (*auto_extension)(void(*)(void));
134381 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
134382 void(*)(void*));
134383 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
134384 void(*)(void*),unsigned char);
134385 int (*cancel_auto_extension)(void(*)(void));
134386 int (*load_extension)(sqlite3*,const char*,const char*,char**);
134387 void *(*malloc64)(sqlite3_uint64);
134388 sqlite3_uint64 (*msize)(void*);
134389 void *(*realloc64)(void*,sqlite3_uint64);
134390 void (*reset_auto_extension)(void);
134391 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
134392 void(*)(void*));
134393 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
134394 void(*)(void*), unsigned char);
134395 int (*strglob)(const char*,const char*);
134396 /* Version 3.8.11 and later */
134397 sqlite3_value *(*value_dup)(const sqlite3_value*);
134398 void (*value_free)(sqlite3_value*);
134399 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
134400 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
134401 /* Version 3.9.0 and later */
134402 unsigned int (*value_subtype)(sqlite3_value*);
134403 void (*result_subtype)(sqlite3_context*,unsigned int);
134404 /* Version 3.10.0 and later */
134405 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
134406 int (*strlike)(const char*,const char*,unsigned int);
134407 int (*db_cacheflush)(sqlite3*);
134408 /* Version 3.12.0 and later */
134409 int (*system_errno)(sqlite3*);
134410 /* Version 3.14.0 and later */
134411 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
134412 char *(*expanded_sql)(sqlite3_stmt*);
134413 /* Version 3.18.0 and later */
134414 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
134415 /* Version 3.20.0 and later */
134416 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
134417 sqlite3_stmt**,const char**);
134418 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
134419 sqlite3_stmt**,const void**);
134420 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
134421 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
134422 void *(*value_pointer)(sqlite3_value*,const char*);
134423 int (*vtab_nochange)(sqlite3_context*);
134424 int (*value_nochange)(sqlite3_value*);
134425 const char *(*vtab_collation)(sqlite3_index_info*,int);
134426 /* Version 3.24.0 and later */
134427 int (*keyword_count)(void);
134428 int (*keyword_name)(int,const char**,int*);
134429 int (*keyword_check)(const char*,int);
134430 sqlite3_str *(*str_new)(sqlite3*);
134431 char *(*str_finish)(sqlite3_str*);
134432 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
134433 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
134434 void (*str_append)(sqlite3_str*, const char *zIn, int N);
134435 void (*str_appendall)(sqlite3_str*, const char *zIn);
134436 void (*str_appendchar)(sqlite3_str*, int N, char C);
134437 void (*str_reset)(sqlite3_str*);
134438 int (*str_errcode)(sqlite3_str*);
134439 int (*str_length)(sqlite3_str*);
134440 char *(*str_value)(sqlite3_str*);
134441 /* Version 3.25.0 and later */
134442 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
134443 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
134444 void (*xFinal)(sqlite3_context*),
134445 void (*xValue)(sqlite3_context*),
134446 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
134447 void(*xDestroy)(void*));
134448 /* Version 3.26.0 and later */
134449 const char *(*normalized_sql)(sqlite3_stmt*);
134450 /* Version 3.28.0 and later */
134451 int (*stmt_isexplain)(sqlite3_stmt*);
134452 int (*value_frombind)(sqlite3_value*);
134453 /* Version 3.30.0 and later */
134454 int (*drop_modules)(sqlite3*,const char**);
134455 /* Version 3.31.0 and later */
134456 sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
134457 const char *(*uri_key)(const char*,int);
134458 const char *(*filename_database)(const char*);
134459 const char *(*filename_journal)(const char*);
134460 const char *(*filename_wal)(const char*);
134461 /* Version 3.32.0 and later */
134462 const char *(*create_filename)(const char*,const char*,const char*,
134463 int,const char**);
134464 void (*free_filename)(const char*);
134465 sqlite3_file *(*database_file_object)(const char*);
134466 /* Version 3.34.0 and later */
134467 int (*txn_state)(sqlite3*,const char*);
134468 /* Version 3.36.1 and later */
134469 sqlite3_int64 (*changes64)(sqlite3*);
134470 sqlite3_int64 (*total_changes64)(sqlite3*);
134471 /* Version 3.37.0 and later */
134472 int (*autovacuum_pages)(sqlite3*,
134473 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
134474 void*, void(*)(void*));
134475 /* Version 3.38.0 and later */
134476 int (*error_offset)(sqlite3*);
134477 int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
134478 int (*vtab_distinct)(sqlite3_index_info*);
134479 int (*vtab_in)(sqlite3_index_info*,int,int);
134480 int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
134481 int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
134482 /* Version 3.39.0 and later */
134483 int (*deserialize)(sqlite3*,const char*,unsigned char*,
134484 sqlite3_int64,sqlite3_int64,unsigned);
134485 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
134486 unsigned int);
134487 const char *(*db_name)(sqlite3*,int);
134488 /* Version 3.40.0 and later */
134489 int (*value_encoding)(sqlite3_value*);
134490 /* Version 3.41.0 and later */
134491 int (*is_interrupted)(sqlite3*);
134492 /* Version 3.43.0 and later */
134493 int (*stmt_explain)(sqlite3_stmt*,int);
134494};
134495
134496/*
134497** This is the function signature used for all extension entry points. It
134498** is also defined in the file "loadext.c".
134499*/
134500typedef int (*sqlite3_loadext_entry)(
134501 sqlite3 *db, /* Handle to the database. */
134502 char **pzErrMsg, /* Used to set error string on failure. */
134503 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
134504);
134505
134506/*
134507** The following macros redefine the API routines so that they are
134508** redirected through the global sqlite3_api structure.
134509**
134510** This header file is also used by the loadext.c source file
134511** (part of the main SQLite library - not an extension) so that
134512** it can get access to the sqlite3_api_routines structure
134513** definition. But the main library does not want to redefine
134514** the API. So the redefinition macros are only valid if the
134515** SQLITE_CORE macros is undefined.
134516*/
134517#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
134518#define sqlite3_aggregate_context sqlite3_api->aggregate_context
134519#ifndef SQLITE_OMIT_DEPRECATED
134520#define sqlite3_aggregate_count sqlite3_api->aggregate_count
134521#endif
134522#define sqlite3_bind_blob sqlite3_api->bind_blob
134523#define sqlite3_bind_double sqlite3_api->bind_double
134524#define sqlite3_bind_int sqlite3_api->bind_int
134525#define sqlite3_bind_int64 sqlite3_api->bind_int64
134526#define sqlite3_bind_null sqlite3_api->bind_null
134527#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
134528#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
134529#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
134530#define sqlite3_bind_text sqlite3_api->bind_text
134531#define sqlite3_bind_text16 sqlite3_api->bind_text16
134532#define sqlite3_bind_value sqlite3_api->bind_value
134533#define sqlite3_busy_handler sqlite3_api->busy_handler
134534#define sqlite3_busy_timeout sqlite3_api->busy_timeout
134535#define sqlite3_changes sqlite3_api->changes
134536#define sqlite3_close sqlite3_api->close
134537#define sqlite3_collation_needed sqlite3_api->collation_needed
134538#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
134539#define sqlite3_column_blob sqlite3_api->column_blob
134540#define sqlite3_column_bytes sqlite3_api->column_bytes
134541#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
134542#define sqlite3_column_count sqlite3_api->column_count
134543#define sqlite3_column_database_name sqlite3_api->column_database_name
134544#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
134545#define sqlite3_column_decltype sqlite3_api->column_decltype
134546#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
134547#define sqlite3_column_double sqlite3_api->column_double
134548#define sqlite3_column_int sqlite3_api->column_int
134549#define sqlite3_column_int64 sqlite3_api->column_int64
134550#define sqlite3_column_name sqlite3_api->column_name
134551#define sqlite3_column_name16 sqlite3_api->column_name16
134552#define sqlite3_column_origin_name sqlite3_api->column_origin_name
134553#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
134554#define sqlite3_column_table_name sqlite3_api->column_table_name
134555#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
134556#define sqlite3_column_text sqlite3_api->column_text
134557#define sqlite3_column_text16 sqlite3_api->column_text16
134558#define sqlite3_column_type sqlite3_api->column_type
134559#define sqlite3_column_value sqlite3_api->column_value
134560#define sqlite3_commit_hook sqlite3_api->commit_hook
134561#define sqlite3_complete sqlite3_api->complete
134562#define sqlite3_complete16 sqlite3_api->complete16
134563#define sqlite3_create_collation sqlite3_api->create_collation
134564#define sqlite3_create_collation16 sqlite3_api->create_collation16
134565#define sqlite3_create_function sqlite3_api->create_function
134566#define sqlite3_create_function16 sqlite3_api->create_function16
134567#define sqlite3_create_module sqlite3_api->create_module
134568#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
134569#define sqlite3_data_count sqlite3_api->data_count
134570#define sqlite3_db_handle sqlite3_api->db_handle
134571#define sqlite3_declare_vtab sqlite3_api->declare_vtab
134572#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
134573#define sqlite3_errcode sqlite3_api->errcode
134574#define sqlite3_errmsg sqlite3_api->errmsg
134575#define sqlite3_errmsg16 sqlite3_api->errmsg16
134576#define sqlite3_exec sqlite3_api->exec
134577#ifndef SQLITE_OMIT_DEPRECATED
134578#define sqlite3_expired sqlite3_api->expired
134579#endif
134580#define sqlite3_finalize sqlite3_api->finalize
134581#define sqlite3_free sqlite3_api->free
134582#define sqlite3_free_table sqlite3_api->free_table
134583#define sqlite3_get_autocommit sqlite3_api->get_autocommit
134584#define sqlite3_get_auxdata sqlite3_api->get_auxdata
134585#define sqlite3_get_table sqlite3_api->get_table
134586#ifndef SQLITE_OMIT_DEPRECATED
134587#define sqlite3_global_recover sqlite3_api->global_recover
134588#endif
134589#define sqlite3_interrupt sqlite3_api->interruptx
134590#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
134591#define sqlite3_libversion sqlite3_api->libversion
134592#define sqlite3_libversion_number sqlite3_api->libversion_number
134593#define sqlite3_malloc sqlite3_api->malloc
134594#define sqlite3_mprintf sqlite3_api->mprintf
134595#define sqlite3_open sqlite3_api->open
134596#define sqlite3_open16 sqlite3_api->open16
134597#define sqlite3_prepare sqlite3_api->prepare
134598#define sqlite3_prepare16 sqlite3_api->prepare16
134599#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
134600#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
134601#define sqlite3_profile sqlite3_api->profile
134602#define sqlite3_progress_handler sqlite3_api->progress_handler
134603#define sqlite3_realloc sqlite3_api->realloc
134604#define sqlite3_reset sqlite3_api->reset
134605#define sqlite3_result_blob sqlite3_api->result_blob
134606#define sqlite3_result_double sqlite3_api->result_double
134607#define sqlite3_result_error sqlite3_api->result_error
134608#define sqlite3_result_error16 sqlite3_api->result_error16
134609#define sqlite3_result_int sqlite3_api->result_int
134610#define sqlite3_result_int64 sqlite3_api->result_int64
134611#define sqlite3_result_null sqlite3_api->result_null
134612#define sqlite3_result_text sqlite3_api->result_text
134613#define sqlite3_result_text16 sqlite3_api->result_text16
134614#define sqlite3_result_text16be sqlite3_api->result_text16be
134615#define sqlite3_result_text16le sqlite3_api->result_text16le
134616#define sqlite3_result_value sqlite3_api->result_value
134617#define sqlite3_rollback_hook sqlite3_api->rollback_hook
134618#define sqlite3_set_authorizer sqlite3_api->set_authorizer
134619#define sqlite3_set_auxdata sqlite3_api->set_auxdata
134620#define sqlite3_snprintf sqlite3_api->xsnprintf
134621#define sqlite3_step sqlite3_api->step
134622#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
134623#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
134624#define sqlite3_total_changes sqlite3_api->total_changes
134625#define sqlite3_trace sqlite3_api->trace
134626#ifndef SQLITE_OMIT_DEPRECATED
134627#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
134628#endif
134629#define sqlite3_update_hook sqlite3_api->update_hook
134630#define sqlite3_user_data sqlite3_api->user_data
134631#define sqlite3_value_blob sqlite3_api->value_blob
134632#define sqlite3_value_bytes sqlite3_api->value_bytes
134633#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
134634#define sqlite3_value_double sqlite3_api->value_double
134635#define sqlite3_value_int sqlite3_api->value_int
134636#define sqlite3_value_int64 sqlite3_api->value_int64
134637#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
134638#define sqlite3_value_text sqlite3_api->value_text
134639#define sqlite3_value_text16 sqlite3_api->value_text16
134640#define sqlite3_value_text16be sqlite3_api->value_text16be
134641#define sqlite3_value_text16le sqlite3_api->value_text16le
134642#define sqlite3_value_type sqlite3_api->value_type
134643#define sqlite3_vmprintf sqlite3_api->vmprintf
134644#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
134645#define sqlite3_overload_function sqlite3_api->overload_function
134646#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
134647#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
134648#define sqlite3_clear_bindings sqlite3_api->clear_bindings
134649#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
134650#define sqlite3_blob_bytes sqlite3_api->blob_bytes
134651#define sqlite3_blob_close sqlite3_api->blob_close
134652#define sqlite3_blob_open sqlite3_api->blob_open
134653#define sqlite3_blob_read sqlite3_api->blob_read
134654#define sqlite3_blob_write sqlite3_api->blob_write
134655#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
134656#define sqlite3_file_control sqlite3_api->file_control
134657#define sqlite3_memory_highwater sqlite3_api->memory_highwater
134658#define sqlite3_memory_used sqlite3_api->memory_used
134659#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
134660#define sqlite3_mutex_enter sqlite3_api->mutex_enter
134661#define sqlite3_mutex_free sqlite3_api->mutex_free
134662#define sqlite3_mutex_leave sqlite3_api->mutex_leave
134663#define sqlite3_mutex_try sqlite3_api->mutex_try
134664#define sqlite3_open_v2 sqlite3_api->open_v2
134665#define sqlite3_release_memory sqlite3_api->release_memory
134666#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
134667#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
134668#define sqlite3_sleep sqlite3_api->sleep
134669#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
134670#define sqlite3_vfs_find sqlite3_api->vfs_find
134671#define sqlite3_vfs_register sqlite3_api->vfs_register
134672#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
134673#define sqlite3_threadsafe sqlite3_api->xthreadsafe
134674#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
134675#define sqlite3_result_error_code sqlite3_api->result_error_code
134676#define sqlite3_test_control sqlite3_api->test_control
134677#define sqlite3_randomness sqlite3_api->randomness
134678#define sqlite3_context_db_handle sqlite3_api->context_db_handle
134679#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
134680#define sqlite3_limit sqlite3_api->limit
134681#define sqlite3_next_stmt sqlite3_api->next_stmt
134682#define sqlite3_sql sqlite3_api->sql
134683#define sqlite3_status sqlite3_api->status
134684#define sqlite3_backup_finish sqlite3_api->backup_finish
134685#define sqlite3_backup_init sqlite3_api->backup_init
134686#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
134687#define sqlite3_backup_remaining sqlite3_api->backup_remaining
134688#define sqlite3_backup_step sqlite3_api->backup_step
134689#define sqlite3_compileoption_get sqlite3_api->compileoption_get
134690#define sqlite3_compileoption_used sqlite3_api->compileoption_used
134691#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
134692#define sqlite3_db_config sqlite3_api->db_config
134693#define sqlite3_db_mutex sqlite3_api->db_mutex
134694#define sqlite3_db_status sqlite3_api->db_status
134695#define sqlite3_extended_errcode sqlite3_api->extended_errcode
134696#define sqlite3_log sqlite3_api->log
134697#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
134698#define sqlite3_sourceid sqlite3_api->sourceid
134699#define sqlite3_stmt_status sqlite3_api->stmt_status
134700#define sqlite3_strnicmp sqlite3_api->strnicmp
134701#define sqlite3_unlock_notify sqlite3_api->unlock_notify
134702#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
134703#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
134704#define sqlite3_wal_hook sqlite3_api->wal_hook
134705#define sqlite3_blob_reopen sqlite3_api->blob_reopen
134706#define sqlite3_vtab_config sqlite3_api->vtab_config
134707#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
134708/* Version 3.7.16 and later */
134709#define sqlite3_close_v2 sqlite3_api->close_v2
134710#define sqlite3_db_filename sqlite3_api->db_filename
134711#define sqlite3_db_readonly sqlite3_api->db_readonly
134712#define sqlite3_db_release_memory sqlite3_api->db_release_memory
134713#define sqlite3_errstr sqlite3_api->errstr
134714#define sqlite3_stmt_busy sqlite3_api->stmt_busy
134715#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
134716#define sqlite3_stricmp sqlite3_api->stricmp
134717#define sqlite3_uri_boolean sqlite3_api->uri_boolean
134718#define sqlite3_uri_int64 sqlite3_api->uri_int64
134719#define sqlite3_uri_parameter sqlite3_api->uri_parameter
134720#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
134721#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
134722/* Version 3.8.7 and later */
134723#define sqlite3_auto_extension sqlite3_api->auto_extension
134724#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
134725#define sqlite3_bind_text64 sqlite3_api->bind_text64
134726#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
134727#define sqlite3_load_extension sqlite3_api->load_extension
134728#define sqlite3_malloc64 sqlite3_api->malloc64
134729#define sqlite3_msize sqlite3_api->msize
134730#define sqlite3_realloc64 sqlite3_api->realloc64
134731#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
134732#define sqlite3_result_blob64 sqlite3_api->result_blob64
134733#define sqlite3_result_text64 sqlite3_api->result_text64
134734#define sqlite3_strglob sqlite3_api->strglob
134735/* Version 3.8.11 and later */
134736#define sqlite3_value_dup sqlite3_api->value_dup
134737#define sqlite3_value_free sqlite3_api->value_free
134738#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
134739#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
134740/* Version 3.9.0 and later */
134741#define sqlite3_value_subtype sqlite3_api->value_subtype
134742#define sqlite3_result_subtype sqlite3_api->result_subtype
134743/* Version 3.10.0 and later */
134744#define sqlite3_status64 sqlite3_api->status64
134745#define sqlite3_strlike sqlite3_api->strlike
134746#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
134747/* Version 3.12.0 and later */
134748#define sqlite3_system_errno sqlite3_api->system_errno
134749/* Version 3.14.0 and later */
134750#define sqlite3_trace_v2 sqlite3_api->trace_v2
134751#define sqlite3_expanded_sql sqlite3_api->expanded_sql
134752/* Version 3.18.0 and later */
134753#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
134754/* Version 3.20.0 and later */
134755#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
134756#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
134757#define sqlite3_bind_pointer sqlite3_api->bind_pointer
134758#define sqlite3_result_pointer sqlite3_api->result_pointer
134759#define sqlite3_value_pointer sqlite3_api->value_pointer
134760/* Version 3.22.0 and later */
134761#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
134762#define sqlite3_value_nochange sqlite3_api->value_nochange
134763#define sqlite3_vtab_collation sqlite3_api->vtab_collation
134764/* Version 3.24.0 and later */
134765#define sqlite3_keyword_count sqlite3_api->keyword_count
134766#define sqlite3_keyword_name sqlite3_api->keyword_name
134767#define sqlite3_keyword_check sqlite3_api->keyword_check
134768#define sqlite3_str_new sqlite3_api->str_new
134769#define sqlite3_str_finish sqlite3_api->str_finish
134770#define sqlite3_str_appendf sqlite3_api->str_appendf
134771#define sqlite3_str_vappendf sqlite3_api->str_vappendf
134772#define sqlite3_str_append sqlite3_api->str_append
134773#define sqlite3_str_appendall sqlite3_api->str_appendall
134774#define sqlite3_str_appendchar sqlite3_api->str_appendchar
134775#define sqlite3_str_reset sqlite3_api->str_reset
134776#define sqlite3_str_errcode sqlite3_api->str_errcode
134777#define sqlite3_str_length sqlite3_api->str_length
134778#define sqlite3_str_value sqlite3_api->str_value
134779/* Version 3.25.0 and later */
134780#define sqlite3_create_window_function sqlite3_api->create_window_function
134781/* Version 3.26.0 and later */
134782#define sqlite3_normalized_sql sqlite3_api->normalized_sql
134783/* Version 3.28.0 and later */
134784#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
134785#define sqlite3_value_frombind sqlite3_api->value_frombind
134786/* Version 3.30.0 and later */
134787#define sqlite3_drop_modules sqlite3_api->drop_modules
134788/* Version 3.31.0 and later */
134789#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
134790#define sqlite3_uri_key sqlite3_api->uri_key
134791#define sqlite3_filename_database sqlite3_api->filename_database
134792#define sqlite3_filename_journal sqlite3_api->filename_journal
134793#define sqlite3_filename_wal sqlite3_api->filename_wal
134794/* Version 3.32.0 and later */
134795#define sqlite3_create_filename sqlite3_api->create_filename
134796#define sqlite3_free_filename sqlite3_api->free_filename
134797#define sqlite3_database_file_object sqlite3_api->database_file_object
134798/* Version 3.34.0 and later */
134799#define sqlite3_txn_state sqlite3_api->txn_state
134800/* Version 3.36.1 and later */
134801#define sqlite3_changes64 sqlite3_api->changes64
134802#define sqlite3_total_changes64 sqlite3_api->total_changes64
134803/* Version 3.37.0 and later */
134804#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
134805/* Version 3.38.0 and later */
134806#define sqlite3_error_offset sqlite3_api->error_offset
134807#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
134808#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
134809#define sqlite3_vtab_in sqlite3_api->vtab_in
134810#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
134811#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
134812/* Version 3.39.0 and later */
134813#ifndef SQLITE_OMIT_DESERIALIZE
134814#define sqlite3_deserialize sqlite3_api->deserialize
134815#define sqlite3_serialize sqlite3_api->serialize
134816#endif
134817#define sqlite3_db_name sqlite3_api->db_name
134818/* Version 3.40.0 and later */
134819#define sqlite3_value_encoding sqlite3_api->value_encoding
134820/* Version 3.41.0 and later */
134821#define sqlite3_is_interrupted sqlite3_api->is_interrupted
134822/* Version 3.43.0 and later */
134823#define sqlite3_stmt_explain sqlite3_api->stmt_explain
134824#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
134825
134826#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
134827 /* This case when the file really is being compiled as a loadable
134828 ** extension */
134829# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
134830# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
134831# define SQLITE_EXTENSION_INIT3 \
134832 extern const sqlite3_api_routines *sqlite3_api;
134833#else
134834 /* This case when the file is being statically linked into the
134835 ** application */
134836# define SQLITE_EXTENSION_INIT1 /*no-op*/
134837# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
134838# define SQLITE_EXTENSION_INIT3 /*no-op*/
134839#endif
134840
134841#endif /* SQLITE3EXT_H */
134842
134843/************** End of sqlite3ext.h ******************************************/
134844/************** Continuing where we left off in loadext.c ********************/
134845/* #include "sqliteInt.h" */
134846
134847#ifndef SQLITE_OMIT_LOAD_EXTENSION
134848/*
134849** Some API routines are omitted when various features are
134850** excluded from a build of SQLite. Substitute a NULL pointer
134851** for any missing APIs.
134852*/
134853#ifndef SQLITE_ENABLE_COLUMN_METADATA
134854# define sqlite3_column_database_name 0
134855# define sqlite3_column_database_name16 0
134856# define sqlite3_column_table_name 0
134857# define sqlite3_column_table_name16 0
134858# define sqlite3_column_origin_name 0
134859# define sqlite3_column_origin_name16 0
134860#endif
134861
134862#ifdef SQLITE_OMIT_AUTHORIZATION
134863# define sqlite3_set_authorizer 0
134864#endif
134865
134866#ifdef SQLITE_OMIT_UTF16
134867# define sqlite3_bind_text16 0
134868# define sqlite3_collation_needed16 0
134869# define sqlite3_column_decltype16 0
134870# define sqlite3_column_name16 0
134871# define sqlite3_column_text16 0
134872# define sqlite3_complete16 0
134873# define sqlite3_create_collation16 0
134874# define sqlite3_create_function16 0
134875# define sqlite3_errmsg16 0
134876# define sqlite3_open16 0
134877# define sqlite3_prepare16 0
134878# define sqlite3_prepare16_v2 0
134879# define sqlite3_prepare16_v3 0
134880# define sqlite3_result_error16 0
134881# define sqlite3_result_text16 0
134882# define sqlite3_result_text16be 0
134883# define sqlite3_result_text16le 0
134884# define sqlite3_value_text16 0
134885# define sqlite3_value_text16be 0
134886# define sqlite3_value_text16le 0
134887# define sqlite3_column_database_name16 0
134888# define sqlite3_column_table_name16 0
134889# define sqlite3_column_origin_name16 0
134890#endif
134891
134892#ifdef SQLITE_OMIT_COMPLETE
134893# define sqlite3_complete 0
134894# define sqlite3_complete16 0
134895#endif
134896
134897#ifdef SQLITE_OMIT_DECLTYPE
134898# define sqlite3_column_decltype16 0
134899# define sqlite3_column_decltype 0
134900#endif
134901
134902#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
134903# define sqlite3_progress_handler 0
134904#endif
134905
134906#ifdef SQLITE_OMIT_VIRTUALTABLE
134907# define sqlite3_create_module 0
134908# define sqlite3_create_module_v2 0
134909# define sqlite3_declare_vtab 0
134910# define sqlite3_vtab_config 0
134911# define sqlite3_vtab_on_conflict 0
134912# define sqlite3_vtab_collation 0
134913#endif
134914
134915#ifdef SQLITE_OMIT_SHARED_CACHE
134916# define sqlite3_enable_shared_cache 0
134917#endif
134918
134919#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
134920# define sqlite3_profile 0
134921# define sqlite3_trace 0
134922#endif
134923
134924#ifdef SQLITE_OMIT_GET_TABLE
134925# define sqlite3_free_table 0
134926# define sqlite3_get_table 0
134927#endif
134928
134929#ifdef SQLITE_OMIT_INCRBLOB
134930#define sqlite3_bind_zeroblob 0
134931#define sqlite3_blob_bytes 0
134932#define sqlite3_blob_close 0
134933#define sqlite3_blob_open 0
134934#define sqlite3_blob_read 0
134935#define sqlite3_blob_write 0
134936#define sqlite3_blob_reopen 0
134937#endif
134938
134939#if defined(SQLITE_OMIT_TRACE)
134940# define sqlite3_trace_v2 0
134941#endif
134942
134943/*
134944** The following structure contains pointers to all SQLite API routines.
134945** A pointer to this structure is passed into extensions when they are
134946** loaded so that the extension can make calls back into the SQLite
134947** library.
134948**
134949** When adding new APIs, add them to the bottom of this structure
134950** in order to preserve backwards compatibility.
134951**
134952** Extensions that use newer APIs should first call the
134953** sqlite3_libversion_number() to make sure that the API they
134954** intend to use is supported by the library. Extensions should
134955** also check to make sure that the pointer to the function is
134956** not NULL before calling it.
134957*/
134958static const sqlite3_api_routines sqlite3Apis = {
134959 sqlite3_aggregate_context,
134960#ifndef SQLITE_OMIT_DEPRECATED
134961 sqlite3_aggregate_count,
134962#else
134963 0,
134964#endif
134965 sqlite3_bind_blob,
134966 sqlite3_bind_double,
134967 sqlite3_bind_int,
134968 sqlite3_bind_int64,
134969 sqlite3_bind_null,
134970 sqlite3_bind_parameter_count,
134971 sqlite3_bind_parameter_index,
134972 sqlite3_bind_parameter_name,
134973 sqlite3_bind_text,
134974 sqlite3_bind_text16,
134975 sqlite3_bind_value,
134976 sqlite3_busy_handler,
134977 sqlite3_busy_timeout,
134978 sqlite3_changes,
134979 sqlite3_close,
134980 sqlite3_collation_needed,
134981 sqlite3_collation_needed16,
134982 sqlite3_column_blob,
134983 sqlite3_column_bytes,
134984 sqlite3_column_bytes16,
134985 sqlite3_column_count,
134986 sqlite3_column_database_name,
134987 sqlite3_column_database_name16,
134988 sqlite3_column_decltype,
134989 sqlite3_column_decltype16,
134990 sqlite3_column_double,
134991 sqlite3_column_int,
134992 sqlite3_column_int64,
134993 sqlite3_column_name,
134994 sqlite3_column_name16,
134995 sqlite3_column_origin_name,
134996 sqlite3_column_origin_name16,
134997 sqlite3_column_table_name,
134998 sqlite3_column_table_name16,
134999 sqlite3_column_text,
135000 sqlite3_column_text16,
135001 sqlite3_column_type,
135002 sqlite3_column_value,
135003 sqlite3_commit_hook,
135004 sqlite3_complete,
135005 sqlite3_complete16,
135006 sqlite3_create_collation,
135007 sqlite3_create_collation16,
135008 sqlite3_create_function,
135009 sqlite3_create_function16,
135010 sqlite3_create_module,
135011 sqlite3_data_count,
135012 sqlite3_db_handle,
135013 sqlite3_declare_vtab,
135014 sqlite3_enable_shared_cache,
135015 sqlite3_errcode,
135016 sqlite3_errmsg,
135017 sqlite3_errmsg16,
135018 sqlite3_exec,
135019#ifndef SQLITE_OMIT_DEPRECATED
135020 sqlite3_expired,
135021#else
135022 0,
135023#endif
135024 sqlite3_finalize,
135025 sqlite3_free,
135026 sqlite3_free_table,
135027 sqlite3_get_autocommit,
135028 sqlite3_get_auxdata,
135029 sqlite3_get_table,
135030 0, /* Was sqlite3_global_recover(), but that function is deprecated */
135031 sqlite3_interrupt,
135032 sqlite3_last_insert_rowid,
135033 sqlite3_libversion,
135034 sqlite3_libversion_number,
135035 sqlite3_malloc,
135036 sqlite3_mprintf,
135037 sqlite3_open,
135038 sqlite3_open16,
135039 sqlite3_prepare,
135040 sqlite3_prepare16,
135041 sqlite3_profile,
135042 sqlite3_progress_handler,
135043 sqlite3_realloc,
135044 sqlite3_reset,
135045 sqlite3_result_blob,
135046 sqlite3_result_double,
135047 sqlite3_result_error,
135048 sqlite3_result_error16,
135049 sqlite3_result_int,
135050 sqlite3_result_int64,
135051 sqlite3_result_null,
135052 sqlite3_result_text,
135053 sqlite3_result_text16,
135054 sqlite3_result_text16be,
135055 sqlite3_result_text16le,
135056 sqlite3_result_value,
135057 sqlite3_rollback_hook,
135058 sqlite3_set_authorizer,
135059 sqlite3_set_auxdata,
135060 sqlite3_snprintf,
135061 sqlite3_step,
135062 sqlite3_table_column_metadata,
135063#ifndef SQLITE_OMIT_DEPRECATED
135064 sqlite3_thread_cleanup,
135065#else
135066 0,
135067#endif
135068 sqlite3_total_changes,
135069 sqlite3_trace,
135070#ifndef SQLITE_OMIT_DEPRECATED
135071 sqlite3_transfer_bindings,
135072#else
135073 0,
135074#endif
135075 sqlite3_update_hook,
135076 sqlite3_user_data,
135077 sqlite3_value_blob,
135078 sqlite3_value_bytes,
135079 sqlite3_value_bytes16,
135080 sqlite3_value_double,
135081 sqlite3_value_int,
135082 sqlite3_value_int64,
135083 sqlite3_value_numeric_type,
135084 sqlite3_value_text,
135085 sqlite3_value_text16,
135086 sqlite3_value_text16be,
135087 sqlite3_value_text16le,
135088 sqlite3_value_type,
135089 sqlite3_vmprintf,
135090 /*
135091 ** The original API set ends here. All extensions can call any
135092 ** of the APIs above provided that the pointer is not NULL. But
135093 ** before calling APIs that follow, extension should check the
135094 ** sqlite3_libversion_number() to make sure they are dealing with
135095 ** a library that is new enough to support that API.
135096 *************************************************************************
135097 */
135098 sqlite3_overload_function,
135099
135100 /*
135101 ** Added after 3.3.13
135102 */
135103 sqlite3_prepare_v2,
135104 sqlite3_prepare16_v2,
135105 sqlite3_clear_bindings,
135106
135107 /*
135108 ** Added for 3.4.1
135109 */
135110 sqlite3_create_module_v2,
135111
135112 /*
135113 ** Added for 3.5.0
135114 */
135115 sqlite3_bind_zeroblob,
135116 sqlite3_blob_bytes,
135117 sqlite3_blob_close,
135118 sqlite3_blob_open,
135119 sqlite3_blob_read,
135120 sqlite3_blob_write,
135121 sqlite3_create_collation_v2,
135122 sqlite3_file_control,
135123 sqlite3_memory_highwater,
135124 sqlite3_memory_used,
135125#ifdef SQLITE_MUTEX_OMIT
135126 0,
135127 0,
135128 0,
135129 0,
135130 0,
135131#else
135132 sqlite3_mutex_alloc,
135133 sqlite3_mutex_enter,
135134 sqlite3_mutex_free,
135135 sqlite3_mutex_leave,
135136 sqlite3_mutex_try,
135137#endif
135138 sqlite3_open_v2,
135139 sqlite3_release_memory,
135140 sqlite3_result_error_nomem,
135141 sqlite3_result_error_toobig,
135142 sqlite3_sleep,
135143 sqlite3_soft_heap_limit,
135144 sqlite3_vfs_find,
135145 sqlite3_vfs_register,
135146 sqlite3_vfs_unregister,
135147
135148 /*
135149 ** Added for 3.5.8
135150 */
135151 sqlite3_threadsafe,
135152 sqlite3_result_zeroblob,
135153 sqlite3_result_error_code,
135154 sqlite3_test_control,
135155 sqlite3_randomness,
135156 sqlite3_context_db_handle,
135157
135158 /*
135159 ** Added for 3.6.0
135160 */
135161 sqlite3_extended_result_codes,
135162 sqlite3_limit,
135163 sqlite3_next_stmt,
135164 sqlite3_sql,
135165 sqlite3_status,
135166
135167 /*
135168 ** Added for 3.7.4
135169 */
135170 sqlite3_backup_finish,
135171 sqlite3_backup_init,
135172 sqlite3_backup_pagecount,
135173 sqlite3_backup_remaining,
135174 sqlite3_backup_step,
135175#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
135176 sqlite3_compileoption_get,
135177 sqlite3_compileoption_used,
135178#else
135179 0,
135180 0,
135181#endif
135182 sqlite3_create_function_v2,
135183 sqlite3_db_config,
135184 sqlite3_db_mutex,
135185 sqlite3_db_status,
135186 sqlite3_extended_errcode,
135187 sqlite3_log,
135188 sqlite3_soft_heap_limit64,
135189 sqlite3_sourceid,
135190 sqlite3_stmt_status,
135191 sqlite3_strnicmp,
135192#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
135193 sqlite3_unlock_notify,
135194#else
135195 0,
135196#endif
135197#ifndef SQLITE_OMIT_WAL
135198 sqlite3_wal_autocheckpoint,
135199 sqlite3_wal_checkpoint,
135200 sqlite3_wal_hook,
135201#else
135202 0,
135203 0,
135204 0,
135205#endif
135206 sqlite3_blob_reopen,
135207 sqlite3_vtab_config,
135208 sqlite3_vtab_on_conflict,
135209 sqlite3_close_v2,
135210 sqlite3_db_filename,
135211 sqlite3_db_readonly,
135212 sqlite3_db_release_memory,
135213 sqlite3_errstr,
135214 sqlite3_stmt_busy,
135215 sqlite3_stmt_readonly,
135216 sqlite3_stricmp,
135217 sqlite3_uri_boolean,
135218 sqlite3_uri_int64,
135219 sqlite3_uri_parameter,
135220 sqlite3_vsnprintf,
135221 sqlite3_wal_checkpoint_v2,
135222 /* Version 3.8.7 and later */
135223 sqlite3_auto_extension,
135224 sqlite3_bind_blob64,
135225 sqlite3_bind_text64,
135226 sqlite3_cancel_auto_extension,
135227 sqlite3_load_extension,
135228 sqlite3_malloc64,
135229 sqlite3_msize,
135230 sqlite3_realloc64,
135231 sqlite3_reset_auto_extension,
135232 sqlite3_result_blob64,
135233 sqlite3_result_text64,
135234 sqlite3_strglob,
135235 /* Version 3.8.11 and later */
135236 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
135237 sqlite3_value_free,
135238 sqlite3_result_zeroblob64,
135239 sqlite3_bind_zeroblob64,
135240 /* Version 3.9.0 and later */
135241 sqlite3_value_subtype,
135242 sqlite3_result_subtype,
135243 /* Version 3.10.0 and later */
135244 sqlite3_status64,
135245 sqlite3_strlike,
135246 sqlite3_db_cacheflush,
135247 /* Version 3.12.0 and later */
135248 sqlite3_system_errno,
135249 /* Version 3.14.0 and later */
135250 sqlite3_trace_v2,
135251 sqlite3_expanded_sql,
135252 /* Version 3.18.0 and later */
135253 sqlite3_set_last_insert_rowid,
135254 /* Version 3.20.0 and later */
135255 sqlite3_prepare_v3,
135256 sqlite3_prepare16_v3,
135257 sqlite3_bind_pointer,
135258 sqlite3_result_pointer,
135259 sqlite3_value_pointer,
135260 /* Version 3.22.0 and later */
135261 sqlite3_vtab_nochange,
135262 sqlite3_value_nochange,
135263 sqlite3_vtab_collation,
135264 /* Version 3.24.0 and later */
135265 sqlite3_keyword_count,
135266 sqlite3_keyword_name,
135267 sqlite3_keyword_check,
135268 sqlite3_str_new,
135269 sqlite3_str_finish,
135270 sqlite3_str_appendf,
135271 sqlite3_str_vappendf,
135272 sqlite3_str_append,
135273 sqlite3_str_appendall,
135274 sqlite3_str_appendchar,
135275 sqlite3_str_reset,
135276 sqlite3_str_errcode,
135277 sqlite3_str_length,
135278 sqlite3_str_value,
135279 /* Version 3.25.0 and later */
135280 sqlite3_create_window_function,
135281 /* Version 3.26.0 and later */
135282#ifdef SQLITE_ENABLE_NORMALIZE
135283 sqlite3_normalized_sql,
135284#else
135285 0,
135286#endif
135287 /* Version 3.28.0 and later */
135288 sqlite3_stmt_isexplain,
135289 sqlite3_value_frombind,
135290 /* Version 3.30.0 and later */
135291#ifndef SQLITE_OMIT_VIRTUALTABLE
135292 sqlite3_drop_modules,
135293#else
135294 0,
135295#endif
135296 /* Version 3.31.0 and later */
135297 sqlite3_hard_heap_limit64,
135298 sqlite3_uri_key,
135299 sqlite3_filename_database,
135300 sqlite3_filename_journal,
135301 sqlite3_filename_wal,
135302 /* Version 3.32.0 and later */
135303 sqlite3_create_filename,
135304 sqlite3_free_filename,
135305 sqlite3_database_file_object,
135306 /* Version 3.34.0 and later */
135307 sqlite3_txn_state,
135308 /* Version 3.36.1 and later */
135309 sqlite3_changes64,
135310 sqlite3_total_changes64,
135311 /* Version 3.37.0 and later */
135312 sqlite3_autovacuum_pages,
135313 /* Version 3.38.0 and later */
135314 sqlite3_error_offset,
135315#ifndef SQLITE_OMIT_VIRTUALTABLE
135316 sqlite3_vtab_rhs_value,
135317 sqlite3_vtab_distinct,
135318 sqlite3_vtab_in,
135319 sqlite3_vtab_in_first,
135320 sqlite3_vtab_in_next,
135321#else
135322 0,
135323 0,
135324 0,
135325 0,
135326 0,
135327#endif
135328 /* Version 3.39.0 and later */
135329#ifndef SQLITE_OMIT_DESERIALIZE
135330 sqlite3_deserialize,
135331 sqlite3_serialize,
135332#else
135333 0,
135334 0,
135335#endif
135336 sqlite3_db_name,
135337 /* Version 3.40.0 and later */
135338 sqlite3_value_encoding,
135339 /* Version 3.41.0 and later */
135340 sqlite3_is_interrupted,
135341 /* Version 3.43.0 and later */
135342 sqlite3_stmt_explain
135343};
135344
135345/* True if x is the directory separator character
135346*/
135347#if SQLITE_OS_WIN
135348# define DirSep(X) ((X)=='/'||(X)=='\\')
135349#else
135350# define DirSep(X) ((X)=='/')
135351#endif
135352
135353/*
135354** Attempt to load an SQLite extension library contained in the file
135355** zFile. The entry point is zProc. zProc may be 0 in which case a
135356** default entry point name (sqlite3_extension_init) is used. Use
135357** of the default name is recommended.
135358**
135359** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
135360**
135361** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
135362** error message text. The calling function should free this memory
135363** by calling sqlite3DbFree(db, ).
135364*/
135365static int sqlite3LoadExtension(
135366 sqlite3 *db, /* Load the extension into this database connection */
135367 const char *zFile, /* Name of the shared library containing extension */
135368 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
135369 char **pzErrMsg /* Put error message here if not 0 */
135370){
135371 sqlite3_vfs *pVfs = db->pVfs;
135372 void *handle;
135373 sqlite3_loadext_entry xInit;
135374 char *zErrmsg = 0;
135375 const char *zEntry;
135376 char *zAltEntry = 0;
135377 void **aHandle;
135378 u64 nMsg = strlen(s: zFile);
135379 int ii;
135380 int rc;
135381
135382 /* Shared library endings to try if zFile cannot be loaded as written */
135383 static const char *azEndings[] = {
135384#if SQLITE_OS_WIN
135385 "dll"
135386#elif defined(__APPLE__)
135387 "dylib"
135388#else
135389 "so"
135390#endif
135391 };
135392
135393
135394 if( pzErrMsg ) *pzErrMsg = 0;
135395
135396 /* Ticket #1863. To avoid a creating security problems for older
135397 ** applications that relink against newer versions of SQLite, the
135398 ** ability to run load_extension is turned off by default. One
135399 ** must call either sqlite3_enable_load_extension(db) or
135400 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
135401 ** to turn on extension loading.
135402 */
135403 if( (db->flags & SQLITE_LoadExtension)==0 ){
135404 if( pzErrMsg ){
135405 *pzErrMsg = sqlite3_mprintf(zFormat: "not authorized");
135406 }
135407 return SQLITE_ERROR;
135408 }
135409
135410 zEntry = zProc ? zProc : "sqlite3_extension_init";
135411
135412 /* tag-20210611-1. Some dlopen() implementations will segfault if given
135413 ** an oversize filename. Most filesystems have a pathname limit of 4K,
135414 ** so limit the extension filename length to about twice that.
135415 ** https://sqlite.org/forum/forumpost/08a0d6d9bf
135416 **
135417 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
135418 ** See https://sqlite.org/forum/forumpost/24083b579d.
135419 */
135420 if( nMsg>SQLITE_MAX_PATHLEN ) goto extension_not_found;
135421
135422 /* Do not allow sqlite3_load_extension() to link to a copy of the
135423 ** running application, by passing in an empty filename. */
135424 if( nMsg==0 ) goto extension_not_found;
135425
135426 handle = sqlite3OsDlOpen(pVfs, zPath: zFile);
135427#if SQLITE_OS_UNIX || SQLITE_OS_WIN
135428 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
135429 char *zAltFile = sqlite3_mprintf(zFormat: "%s.%s", zFile, azEndings[ii]);
135430 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
135431 if( nMsg+strlen(s: azEndings[ii])+1<=SQLITE_MAX_PATHLEN ){
135432 handle = sqlite3OsDlOpen(pVfs, zPath: zAltFile);
135433 }
135434 sqlite3_free(p: zAltFile);
135435 }
135436#endif
135437 if( handle==0 ) goto extension_not_found;
135438 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, pHdle: handle, zSym: zEntry);
135439
135440 /* If no entry point was specified and the default legacy
135441 ** entry point name "sqlite3_extension_init" was not found, then
135442 ** construct an entry point name "sqlite3_X_init" where the X is
135443 ** replaced by the lowercase value of every ASCII alphabetic
135444 ** character in the filename after the last "/" upto the first ".",
135445 ** and eliding the first three characters if they are "lib".
135446 ** Examples:
135447 **
135448 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
135449 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
135450 */
135451 if( xInit==0 && zProc==0 ){
135452 int iFile, iEntry, c;
135453 int ncFile = sqlite3Strlen30(z: zFile);
135454 zAltEntry = sqlite3_malloc64(n: ncFile+30);
135455 if( zAltEntry==0 ){
135456 sqlite3OsDlClose(pVfs, pHandle: handle);
135457 return SQLITE_NOMEM_BKPT;
135458 }
135459 memcpy(dest: zAltEntry, src: "sqlite3_", n: 8);
135460 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
135461 iFile++;
135462 if( sqlite3_strnicmp(zLeft: zFile+iFile, zRight: "lib", N: 3)==0 ) iFile += 3;
135463 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
135464 if( sqlite3Isalpha(c) ){
135465 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
135466 }
135467 }
135468 memcpy(dest: zAltEntry+iEntry, src: "_init", n: 6);
135469 zEntry = zAltEntry;
135470 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, pHdle: handle, zSym: zEntry);
135471 }
135472 if( xInit==0 ){
135473 if( pzErrMsg ){
135474 nMsg += strlen(s: zEntry) + 300;
135475 *pzErrMsg = zErrmsg = sqlite3_malloc64(n: nMsg);
135476 if( zErrmsg ){
135477 assert( nMsg<0x7fffffff ); /* zErrmsg would be NULL if not so */
135478 sqlite3_snprintf(n: (int)nMsg, zBuf: zErrmsg,
135479 zFormat: "no entry point [%s] in shared library [%s]", zEntry, zFile);
135480 sqlite3OsDlError(pVfs, nByte: nMsg-1, zBufOut: zErrmsg);
135481 }
135482 }
135483 sqlite3OsDlClose(pVfs, pHandle: handle);
135484 sqlite3_free(p: zAltEntry);
135485 return SQLITE_ERROR;
135486 }
135487 sqlite3_free(p: zAltEntry);
135488 rc = xInit(db, &zErrmsg, &sqlite3Apis);
135489 if( rc ){
135490 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
135491 if( pzErrMsg ){
135492 *pzErrMsg = sqlite3_mprintf(zFormat: "error during initialization: %s", zErrmsg);
135493 }
135494 sqlite3_free(p: zErrmsg);
135495 sqlite3OsDlClose(pVfs, pHandle: handle);
135496 return SQLITE_ERROR;
135497 }
135498
135499 /* Append the new shared library handle to the db->aExtension array. */
135500 aHandle = sqlite3DbMallocZero(db, n: sizeof(handle)*(db->nExtension+1));
135501 if( aHandle==0 ){
135502 return SQLITE_NOMEM_BKPT;
135503 }
135504 if( db->nExtension>0 ){
135505 memcpy(dest: aHandle, src: db->aExtension, n: sizeof(handle)*db->nExtension);
135506 }
135507 sqlite3DbFree(db, p: db->aExtension);
135508 db->aExtension = aHandle;
135509
135510 db->aExtension[db->nExtension++] = handle;
135511 return SQLITE_OK;
135512
135513extension_not_found:
135514 if( pzErrMsg ){
135515 nMsg += 300;
135516 *pzErrMsg = zErrmsg = sqlite3_malloc64(n: nMsg);
135517 if( zErrmsg ){
135518 assert( nMsg<0x7fffffff ); /* zErrmsg would be NULL if not so */
135519 sqlite3_snprintf(n: (int)nMsg, zBuf: zErrmsg,
135520 zFormat: "unable to open shared library [%.*s]", SQLITE_MAX_PATHLEN, zFile);
135521 sqlite3OsDlError(pVfs, nByte: nMsg-1, zBufOut: zErrmsg);
135522 }
135523 }
135524 return SQLITE_ERROR;
135525}
135526SQLITE_API int sqlite3_load_extension(
135527 sqlite3 *db, /* Load the extension into this database connection */
135528 const char *zFile, /* Name of the shared library containing extension */
135529 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
135530 char **pzErrMsg /* Put error message here if not 0 */
135531){
135532 int rc;
135533 sqlite3_mutex_enter(p: db->mutex);
135534 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
135535 rc = sqlite3ApiExit(db, rc);
135536 sqlite3_mutex_leave(p: db->mutex);
135537 return rc;
135538}
135539
135540/*
135541** Call this routine when the database connection is closing in order
135542** to clean up loaded extensions
135543*/
135544SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
135545 int i;
135546 assert( sqlite3_mutex_held(db->mutex) );
135547 for(i=0; i<db->nExtension; i++){
135548 sqlite3OsDlClose(pVfs: db->pVfs, pHandle: db->aExtension[i]);
135549 }
135550 sqlite3DbFree(db, p: db->aExtension);
135551}
135552
135553/*
135554** Enable or disable extension loading. Extension loading is disabled by
135555** default so as not to open security holes in older applications.
135556*/
135557SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
135558 sqlite3_mutex_enter(p: db->mutex);
135559 if( onoff ){
135560 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
135561 }else{
135562 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
135563 }
135564 sqlite3_mutex_leave(p: db->mutex);
135565 return SQLITE_OK;
135566}
135567
135568#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
135569
135570/*
135571** The following object holds the list of automatically loaded
135572** extensions.
135573**
135574** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
135575** mutex must be held while accessing this list.
135576*/
135577typedef struct sqlite3AutoExtList sqlite3AutoExtList;
135578static SQLITE_WSD struct sqlite3AutoExtList {
135579 u32 nExt; /* Number of entries in aExt[] */
135580 void (**aExt)(void); /* Pointers to the extension init functions */
135581} sqlite3Autoext = { 0, 0 };
135582
135583/* The "wsdAutoext" macro will resolve to the autoextension
135584** state vector. If writable static data is unsupported on the target,
135585** we have to locate the state vector at run-time. In the more common
135586** case where writable static data is supported, wsdStat can refer directly
135587** to the "sqlite3Autoext" state vector declared above.
135588*/
135589#ifdef SQLITE_OMIT_WSD
135590# define wsdAutoextInit \
135591 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
135592# define wsdAutoext x[0]
135593#else
135594# define wsdAutoextInit
135595# define wsdAutoext sqlite3Autoext
135596#endif
135597
135598
135599/*
135600** Register a statically linked extension that is automatically
135601** loaded by every new database connection.
135602*/
135603SQLITE_API int sqlite3_auto_extension(
135604 void (*xInit)(void)
135605){
135606 int rc = SQLITE_OK;
135607#ifndef SQLITE_OMIT_AUTOINIT
135608 rc = sqlite3_initialize();
135609 if( rc ){
135610 return rc;
135611 }else
135612#endif
135613 {
135614 u32 i;
135615#if SQLITE_THREADSAFE
135616 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
135617#endif
135618 wsdAutoextInit;
135619 sqlite3_mutex_enter(p: mutex);
135620 for(i=0; i<wsdAutoext.nExt; i++){
135621 if( wsdAutoext.aExt[i]==xInit ) break;
135622 }
135623 if( i==wsdAutoext.nExt ){
135624 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
135625 void (**aNew)(void);
135626 aNew = sqlite3_realloc64(wsdAutoext.aExt, n: nByte);
135627 if( aNew==0 ){
135628 rc = SQLITE_NOMEM_BKPT;
135629 }else{
135630 wsdAutoext.aExt = aNew;
135631 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
135632 wsdAutoext.nExt++;
135633 }
135634 }
135635 sqlite3_mutex_leave(p: mutex);
135636 assert( (rc&0xff)==rc );
135637 return rc;
135638 }
135639}
135640
135641/*
135642** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
135643** set of routines that is invoked for each new database connection, if it
135644** is currently on the list. If xInit is not on the list, then this
135645** routine is a no-op.
135646**
135647** Return 1 if xInit was found on the list and removed. Return 0 if xInit
135648** was not on the list.
135649*/
135650SQLITE_API int sqlite3_cancel_auto_extension(
135651 void (*xInit)(void)
135652){
135653#if SQLITE_THREADSAFE
135654 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
135655#endif
135656 int i;
135657 int n = 0;
135658 wsdAutoextInit;
135659 sqlite3_mutex_enter(p: mutex);
135660 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
135661 if( wsdAutoext.aExt[i]==xInit ){
135662 wsdAutoext.nExt--;
135663 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
135664 n++;
135665 break;
135666 }
135667 }
135668 sqlite3_mutex_leave(p: mutex);
135669 return n;
135670}
135671
135672/*
135673** Reset the automatic extension loading mechanism.
135674*/
135675SQLITE_API void sqlite3_reset_auto_extension(void){
135676#ifndef SQLITE_OMIT_AUTOINIT
135677 if( sqlite3_initialize()==SQLITE_OK )
135678#endif
135679 {
135680#if SQLITE_THREADSAFE
135681 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
135682#endif
135683 wsdAutoextInit;
135684 sqlite3_mutex_enter(p: mutex);
135685 sqlite3_free(wsdAutoext.aExt);
135686 wsdAutoext.aExt = 0;
135687 wsdAutoext.nExt = 0;
135688 sqlite3_mutex_leave(p: mutex);
135689 }
135690}
135691
135692/*
135693** Load all automatic extensions.
135694**
135695** If anything goes wrong, set an error in the database connection.
135696*/
135697SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
135698 u32 i;
135699 int go = 1;
135700 int rc;
135701 sqlite3_loadext_entry xInit;
135702
135703 wsdAutoextInit;
135704 if( wsdAutoext.nExt==0 ){
135705 /* Common case: early out without every having to acquire a mutex */
135706 return;
135707 }
135708 for(i=0; go; i++){
135709 char *zErrmsg;
135710#if SQLITE_THREADSAFE
135711 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
135712#endif
135713#ifdef SQLITE_OMIT_LOAD_EXTENSION
135714 const sqlite3_api_routines *pThunk = 0;
135715#else
135716 const sqlite3_api_routines *pThunk = &sqlite3Apis;
135717#endif
135718 sqlite3_mutex_enter(p: mutex);
135719 if( i>=wsdAutoext.nExt ){
135720 xInit = 0;
135721 go = 0;
135722 }else{
135723 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
135724 }
135725 sqlite3_mutex_leave(p: mutex);
135726 zErrmsg = 0;
135727 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
135728 sqlite3ErrorWithMsg(db, err_code: rc,
135729 zFormat: "automatic extension loading failed: %s", zErrmsg);
135730 go = 0;
135731 }
135732 sqlite3_free(p: zErrmsg);
135733 }
135734}
135735
135736/************** End of loadext.c *********************************************/
135737/************** Begin file pragma.c ******************************************/
135738/*
135739** 2003 April 6
135740**
135741** The author disclaims copyright to this source code. In place of
135742** a legal notice, here is a blessing:
135743**
135744** May you do good and not evil.
135745** May you find forgiveness for yourself and forgive others.
135746** May you share freely, never taking more than you give.
135747**
135748*************************************************************************
135749** This file contains code used to implement the PRAGMA command.
135750*/
135751/* #include "sqliteInt.h" */
135752
135753#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
135754# if defined(__APPLE__)
135755# define SQLITE_ENABLE_LOCKING_STYLE 1
135756# else
135757# define SQLITE_ENABLE_LOCKING_STYLE 0
135758# endif
135759#endif
135760
135761/***************************************************************************
135762** The "pragma.h" include file is an automatically generated file that
135763** that includes the PragType_XXXX macro definitions and the aPragmaName[]
135764** object. This ensures that the aPragmaName[] table is arranged in
135765** lexicographical order to facility a binary search of the pragma name.
135766** Do not edit pragma.h directly. Edit and rerun the script in at
135767** ../tool/mkpragmatab.tcl. */
135768/************** Include pragma.h in the middle of pragma.c *******************/
135769/************** Begin file pragma.h ******************************************/
135770/* DO NOT EDIT!
135771** This file is automatically generated by the script at
135772** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
135773** that script and rerun it.
135774*/
135775
135776/* The various pragma types */
135777#define PragTyp_ACTIVATE_EXTENSIONS 0
135778#define PragTyp_ANALYSIS_LIMIT 1
135779#define PragTyp_HEADER_VALUE 2
135780#define PragTyp_AUTO_VACUUM 3
135781#define PragTyp_FLAG 4
135782#define PragTyp_BUSY_TIMEOUT 5
135783#define PragTyp_CACHE_SIZE 6
135784#define PragTyp_CACHE_SPILL 7
135785#define PragTyp_CASE_SENSITIVE_LIKE 8
135786#define PragTyp_COLLATION_LIST 9
135787#define PragTyp_COMPILE_OPTIONS 10
135788#define PragTyp_DATA_STORE_DIRECTORY 11
135789#define PragTyp_DATABASE_LIST 12
135790#define PragTyp_DEFAULT_CACHE_SIZE 13
135791#define PragTyp_ENCODING 14
135792#define PragTyp_FOREIGN_KEY_CHECK 15
135793#define PragTyp_FOREIGN_KEY_LIST 16
135794#define PragTyp_FUNCTION_LIST 17
135795#define PragTyp_HARD_HEAP_LIMIT 18
135796#define PragTyp_INCREMENTAL_VACUUM 19
135797#define PragTyp_INDEX_INFO 20
135798#define PragTyp_INDEX_LIST 21
135799#define PragTyp_INTEGRITY_CHECK 22
135800#define PragTyp_JOURNAL_MODE 23
135801#define PragTyp_JOURNAL_SIZE_LIMIT 24
135802#define PragTyp_LOCK_PROXY_FILE 25
135803#define PragTyp_LOCKING_MODE 26
135804#define PragTyp_PAGE_COUNT 27
135805#define PragTyp_MMAP_SIZE 28
135806#define PragTyp_MODULE_LIST 29
135807#define PragTyp_OPTIMIZE 30
135808#define PragTyp_PAGE_SIZE 31
135809#define PragTyp_PRAGMA_LIST 32
135810#define PragTyp_SECURE_DELETE 33
135811#define PragTyp_SHRINK_MEMORY 34
135812#define PragTyp_SOFT_HEAP_LIMIT 35
135813#define PragTyp_SYNCHRONOUS 36
135814#define PragTyp_TABLE_INFO 37
135815#define PragTyp_TABLE_LIST 38
135816#define PragTyp_TEMP_STORE 39
135817#define PragTyp_TEMP_STORE_DIRECTORY 40
135818#define PragTyp_THREADS 41
135819#define PragTyp_WAL_AUTOCHECKPOINT 42
135820#define PragTyp_WAL_CHECKPOINT 43
135821#define PragTyp_LOCK_STATUS 44
135822#define PragTyp_STATS 45
135823
135824/* Property flags associated with various pragma. */
135825#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
135826#define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */
135827#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
135828#define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
135829#define PragFlg_Result0 0x10 /* Acts as query when no argument */
135830#define PragFlg_Result1 0x20 /* Acts as query when has one argument */
135831#define PragFlg_SchemaOpt 0x40 /* Schema restricts name search if present */
135832#define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
135833
135834/* Names of columns for pragmas that return multi-column result
135835** or that return single-column results where the name of the
135836** result column is different from the name of the pragma
135837*/
135838static const char *const pragCName[] = {
135839 /* 0 */ "id", /* Used by: foreign_key_list */
135840 /* 1 */ "seq",
135841 /* 2 */ "table",
135842 /* 3 */ "from",
135843 /* 4 */ "to",
135844 /* 5 */ "on_update",
135845 /* 6 */ "on_delete",
135846 /* 7 */ "match",
135847 /* 8 */ "cid", /* Used by: table_xinfo */
135848 /* 9 */ "name",
135849 /* 10 */ "type",
135850 /* 11 */ "notnull",
135851 /* 12 */ "dflt_value",
135852 /* 13 */ "pk",
135853 /* 14 */ "hidden",
135854 /* table_info reuses 8 */
135855 /* 15 */ "schema", /* Used by: table_list */
135856 /* 16 */ "name",
135857 /* 17 */ "type",
135858 /* 18 */ "ncol",
135859 /* 19 */ "wr",
135860 /* 20 */ "strict",
135861 /* 21 */ "seqno", /* Used by: index_xinfo */
135862 /* 22 */ "cid",
135863 /* 23 */ "name",
135864 /* 24 */ "desc",
135865 /* 25 */ "coll",
135866 /* 26 */ "key",
135867 /* 27 */ "name", /* Used by: function_list */
135868 /* 28 */ "builtin",
135869 /* 29 */ "type",
135870 /* 30 */ "enc",
135871 /* 31 */ "narg",
135872 /* 32 */ "flags",
135873 /* 33 */ "tbl", /* Used by: stats */
135874 /* 34 */ "idx",
135875 /* 35 */ "wdth",
135876 /* 36 */ "hght",
135877 /* 37 */ "flgs",
135878 /* 38 */ "seq", /* Used by: index_list */
135879 /* 39 */ "name",
135880 /* 40 */ "unique",
135881 /* 41 */ "origin",
135882 /* 42 */ "partial",
135883 /* 43 */ "table", /* Used by: foreign_key_check */
135884 /* 44 */ "rowid",
135885 /* 45 */ "parent",
135886 /* 46 */ "fkid",
135887 /* index_info reuses 21 */
135888 /* 47 */ "seq", /* Used by: database_list */
135889 /* 48 */ "name",
135890 /* 49 */ "file",
135891 /* 50 */ "busy", /* Used by: wal_checkpoint */
135892 /* 51 */ "log",
135893 /* 52 */ "checkpointed",
135894 /* collation_list reuses 38 */
135895 /* 53 */ "database", /* Used by: lock_status */
135896 /* 54 */ "status",
135897 /* 55 */ "cache_size", /* Used by: default_cache_size */
135898 /* module_list pragma_list reuses 9 */
135899 /* 56 */ "timeout", /* Used by: busy_timeout */
135900};
135901
135902/* Definitions of all built-in pragmas */
135903typedef struct PragmaName {
135904 const char *const zName; /* Name of pragma */
135905 u8 ePragTyp; /* PragTyp_XXX value */
135906 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
135907 u8 iPragCName; /* Start of column names in pragCName[] */
135908 u8 nPragCName; /* Num of col names. 0 means use pragma name */
135909 u64 iArg; /* Extra argument */
135910} PragmaName;
135911static const PragmaName aPragmaName[] = {
135912#if defined(SQLITE_ENABLE_CEROD)
135913 {/* zName: */ "activate_extensions",
135914 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
135915 /* ePragFlg: */ 0,
135916 /* ColNames: */ 0, 0,
135917 /* iArg: */ 0 },
135918#endif
135919 {/* zName: */ "analysis_limit",
135920 /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT,
135921 /* ePragFlg: */ PragFlg_Result0,
135922 /* ColNames: */ 0, 0,
135923 /* iArg: */ 0 },
135924#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
135925 {/* zName: */ "application_id",
135926 /* ePragTyp: */ PragTyp_HEADER_VALUE,
135927 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
135928 /* ColNames: */ 0, 0,
135929 /* iArg: */ BTREE_APPLICATION_ID },
135930#endif
135931#if !defined(SQLITE_OMIT_AUTOVACUUM)
135932 {/* zName: */ "auto_vacuum",
135933 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
135934 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
135935 /* ColNames: */ 0, 0,
135936 /* iArg: */ 0 },
135937#endif
135938#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
135939#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
135940 {/* zName: */ "automatic_index",
135941 /* ePragTyp: */ PragTyp_FLAG,
135942 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
135943 /* ColNames: */ 0, 0,
135944 /* iArg: */ SQLITE_AutoIndex },
135945#endif
135946#endif
135947 {/* zName: */ "busy_timeout",
135948 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
135949 /* ePragFlg: */ PragFlg_Result0,
135950 /* ColNames: */ 56, 1,
135951 /* iArg: */ 0 },
135952#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
135953 {/* zName: */ "cache_size",
135954 /* ePragTyp: */ PragTyp_CACHE_SIZE,
135955 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
135956 /* ColNames: */ 0, 0,
135957 /* iArg: */ 0 },
135958#endif
135959#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
135960 {/* zName: */ "cache_spill",
135961 /* ePragTyp: */ PragTyp_CACHE_SPILL,
135962 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
135963 /* ColNames: */ 0, 0,
135964 /* iArg: */ 0 },
135965#endif
135966#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
135967 {/* zName: */ "case_sensitive_like",
135968 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
135969 /* ePragFlg: */ PragFlg_NoColumns,
135970 /* ColNames: */ 0, 0,
135971 /* iArg: */ 0 },
135972#endif
135973 {/* zName: */ "cell_size_check",
135974 /* ePragTyp: */ PragTyp_FLAG,
135975 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
135976 /* ColNames: */ 0, 0,
135977 /* iArg: */ SQLITE_CellSizeCk },
135978#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
135979 {/* zName: */ "checkpoint_fullfsync",
135980 /* ePragTyp: */ PragTyp_FLAG,
135981 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
135982 /* ColNames: */ 0, 0,
135983 /* iArg: */ SQLITE_CkptFullFSync },
135984#endif
135985#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
135986 {/* zName: */ "collation_list",
135987 /* ePragTyp: */ PragTyp_COLLATION_LIST,
135988 /* ePragFlg: */ PragFlg_Result0,
135989 /* ColNames: */ 38, 2,
135990 /* iArg: */ 0 },
135991#endif
135992#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
135993 {/* zName: */ "compile_options",
135994 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
135995 /* ePragFlg: */ PragFlg_Result0,
135996 /* ColNames: */ 0, 0,
135997 /* iArg: */ 0 },
135998#endif
135999#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136000 {/* zName: */ "count_changes",
136001 /* ePragTyp: */ PragTyp_FLAG,
136002 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136003 /* ColNames: */ 0, 0,
136004 /* iArg: */ SQLITE_CountRows },
136005#endif
136006#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
136007 {/* zName: */ "data_store_directory",
136008 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
136009 /* ePragFlg: */ PragFlg_NoColumns1,
136010 /* ColNames: */ 0, 0,
136011 /* iArg: */ 0 },
136012#endif
136013#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
136014 {/* zName: */ "data_version",
136015 /* ePragTyp: */ PragTyp_HEADER_VALUE,
136016 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
136017 /* ColNames: */ 0, 0,
136018 /* iArg: */ BTREE_DATA_VERSION },
136019#endif
136020#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
136021 {/* zName: */ "database_list",
136022 /* ePragTyp: */ PragTyp_DATABASE_LIST,
136023 /* ePragFlg: */ PragFlg_Result0,
136024 /* ColNames: */ 47, 3,
136025 /* iArg: */ 0 },
136026#endif
136027#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
136028 {/* zName: */ "default_cache_size",
136029 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
136030 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
136031 /* ColNames: */ 55, 1,
136032 /* iArg: */ 0 },
136033#endif
136034#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136035#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
136036 {/* zName: */ "defer_foreign_keys",
136037 /* ePragTyp: */ PragTyp_FLAG,
136038 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136039 /* ColNames: */ 0, 0,
136040 /* iArg: */ SQLITE_DeferFKs },
136041#endif
136042#endif
136043#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136044 {/* zName: */ "empty_result_callbacks",
136045 /* ePragTyp: */ PragTyp_FLAG,
136046 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136047 /* ColNames: */ 0, 0,
136048 /* iArg: */ SQLITE_NullCallback },
136049#endif
136050#if !defined(SQLITE_OMIT_UTF16)
136051 {/* zName: */ "encoding",
136052 /* ePragTyp: */ PragTyp_ENCODING,
136053 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136054 /* ColNames: */ 0, 0,
136055 /* iArg: */ 0 },
136056#endif
136057#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
136058 {/* zName: */ "foreign_key_check",
136059 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
136060 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
136061 /* ColNames: */ 43, 4,
136062 /* iArg: */ 0 },
136063#endif
136064#if !defined(SQLITE_OMIT_FOREIGN_KEY)
136065 {/* zName: */ "foreign_key_list",
136066 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
136067 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
136068 /* ColNames: */ 0, 8,
136069 /* iArg: */ 0 },
136070#endif
136071#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136072#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
136073 {/* zName: */ "foreign_keys",
136074 /* ePragTyp: */ PragTyp_FLAG,
136075 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136076 /* ColNames: */ 0, 0,
136077 /* iArg: */ SQLITE_ForeignKeys },
136078#endif
136079#endif
136080#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
136081 {/* zName: */ "freelist_count",
136082 /* ePragTyp: */ PragTyp_HEADER_VALUE,
136083 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
136084 /* ColNames: */ 0, 0,
136085 /* iArg: */ BTREE_FREE_PAGE_COUNT },
136086#endif
136087#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136088 {/* zName: */ "full_column_names",
136089 /* ePragTyp: */ PragTyp_FLAG,
136090 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136091 /* ColNames: */ 0, 0,
136092 /* iArg: */ SQLITE_FullColNames },
136093 {/* zName: */ "fullfsync",
136094 /* ePragTyp: */ PragTyp_FLAG,
136095 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136096 /* ColNames: */ 0, 0,
136097 /* iArg: */ SQLITE_FullFSync },
136098#endif
136099#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
136100#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
136101 {/* zName: */ "function_list",
136102 /* ePragTyp: */ PragTyp_FUNCTION_LIST,
136103 /* ePragFlg: */ PragFlg_Result0,
136104 /* ColNames: */ 27, 6,
136105 /* iArg: */ 0 },
136106#endif
136107#endif
136108 {/* zName: */ "hard_heap_limit",
136109 /* ePragTyp: */ PragTyp_HARD_HEAP_LIMIT,
136110 /* ePragFlg: */ PragFlg_Result0,
136111 /* ColNames: */ 0, 0,
136112 /* iArg: */ 0 },
136113#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136114#if !defined(SQLITE_OMIT_CHECK)
136115 {/* zName: */ "ignore_check_constraints",
136116 /* ePragTyp: */ PragTyp_FLAG,
136117 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136118 /* ColNames: */ 0, 0,
136119 /* iArg: */ SQLITE_IgnoreChecks },
136120#endif
136121#endif
136122#if !defined(SQLITE_OMIT_AUTOVACUUM)
136123 {/* zName: */ "incremental_vacuum",
136124 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
136125 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_NoColumns,
136126 /* ColNames: */ 0, 0,
136127 /* iArg: */ 0 },
136128#endif
136129#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
136130 {/* zName: */ "index_info",
136131 /* ePragTyp: */ PragTyp_INDEX_INFO,
136132 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
136133 /* ColNames: */ 21, 3,
136134 /* iArg: */ 0 },
136135 {/* zName: */ "index_list",
136136 /* ePragTyp: */ PragTyp_INDEX_LIST,
136137 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
136138 /* ColNames: */ 38, 5,
136139 /* iArg: */ 0 },
136140 {/* zName: */ "index_xinfo",
136141 /* ePragTyp: */ PragTyp_INDEX_INFO,
136142 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
136143 /* ColNames: */ 21, 6,
136144 /* iArg: */ 1 },
136145#endif
136146#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
136147 {/* zName: */ "integrity_check",
136148 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
136149 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
136150 /* ColNames: */ 0, 0,
136151 /* iArg: */ 0 },
136152#endif
136153#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136154 {/* zName: */ "journal_mode",
136155 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
136156 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
136157 /* ColNames: */ 0, 0,
136158 /* iArg: */ 0 },
136159 {/* zName: */ "journal_size_limit",
136160 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
136161 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
136162 /* ColNames: */ 0, 0,
136163 /* iArg: */ 0 },
136164#endif
136165#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136166 {/* zName: */ "legacy_alter_table",
136167 /* ePragTyp: */ PragTyp_FLAG,
136168 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136169 /* ColNames: */ 0, 0,
136170 /* iArg: */ SQLITE_LegacyAlter },
136171#endif
136172#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
136173 {/* zName: */ "lock_proxy_file",
136174 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
136175 /* ePragFlg: */ PragFlg_NoColumns1,
136176 /* ColNames: */ 0, 0,
136177 /* iArg: */ 0 },
136178#endif
136179#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
136180 {/* zName: */ "lock_status",
136181 /* ePragTyp: */ PragTyp_LOCK_STATUS,
136182 /* ePragFlg: */ PragFlg_Result0,
136183 /* ColNames: */ 53, 2,
136184 /* iArg: */ 0 },
136185#endif
136186#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136187 {/* zName: */ "locking_mode",
136188 /* ePragTyp: */ PragTyp_LOCKING_MODE,
136189 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
136190 /* ColNames: */ 0, 0,
136191 /* iArg: */ 0 },
136192 {/* zName: */ "max_page_count",
136193 /* ePragTyp: */ PragTyp_PAGE_COUNT,
136194 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
136195 /* ColNames: */ 0, 0,
136196 /* iArg: */ 0 },
136197 {/* zName: */ "mmap_size",
136198 /* ePragTyp: */ PragTyp_MMAP_SIZE,
136199 /* ePragFlg: */ 0,
136200 /* ColNames: */ 0, 0,
136201 /* iArg: */ 0 },
136202#endif
136203#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
136204#if !defined(SQLITE_OMIT_VIRTUALTABLE)
136205#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
136206 {/* zName: */ "module_list",
136207 /* ePragTyp: */ PragTyp_MODULE_LIST,
136208 /* ePragFlg: */ PragFlg_Result0,
136209 /* ColNames: */ 9, 1,
136210 /* iArg: */ 0 },
136211#endif
136212#endif
136213#endif
136214 {/* zName: */ "optimize",
136215 /* ePragTyp: */ PragTyp_OPTIMIZE,
136216 /* ePragFlg: */ PragFlg_Result1|PragFlg_NeedSchema,
136217 /* ColNames: */ 0, 0,
136218 /* iArg: */ 0 },
136219#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136220 {/* zName: */ "page_count",
136221 /* ePragTyp: */ PragTyp_PAGE_COUNT,
136222 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
136223 /* ColNames: */ 0, 0,
136224 /* iArg: */ 0 },
136225 {/* zName: */ "page_size",
136226 /* ePragTyp: */ PragTyp_PAGE_SIZE,
136227 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
136228 /* ColNames: */ 0, 0,
136229 /* iArg: */ 0 },
136230#endif
136231#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136232#if defined(SQLITE_DEBUG)
136233 {/* zName: */ "parser_trace",
136234 /* ePragTyp: */ PragTyp_FLAG,
136235 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136236 /* ColNames: */ 0, 0,
136237 /* iArg: */ SQLITE_ParserTrace },
136238#endif
136239#endif
136240#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
136241 {/* zName: */ "pragma_list",
136242 /* ePragTyp: */ PragTyp_PRAGMA_LIST,
136243 /* ePragFlg: */ PragFlg_Result0,
136244 /* ColNames: */ 9, 1,
136245 /* iArg: */ 0 },
136246#endif
136247#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136248 {/* zName: */ "query_only",
136249 /* ePragTyp: */ PragTyp_FLAG,
136250 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136251 /* ColNames: */ 0, 0,
136252 /* iArg: */ SQLITE_QueryOnly },
136253#endif
136254#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
136255 {/* zName: */ "quick_check",
136256 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
136257 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
136258 /* ColNames: */ 0, 0,
136259 /* iArg: */ 0 },
136260#endif
136261#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136262 {/* zName: */ "read_uncommitted",
136263 /* ePragTyp: */ PragTyp_FLAG,
136264 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136265 /* ColNames: */ 0, 0,
136266 /* iArg: */ SQLITE_ReadUncommit },
136267 {/* zName: */ "recursive_triggers",
136268 /* ePragTyp: */ PragTyp_FLAG,
136269 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136270 /* ColNames: */ 0, 0,
136271 /* iArg: */ SQLITE_RecTriggers },
136272 {/* zName: */ "reverse_unordered_selects",
136273 /* ePragTyp: */ PragTyp_FLAG,
136274 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136275 /* ColNames: */ 0, 0,
136276 /* iArg: */ SQLITE_ReverseOrder },
136277#endif
136278#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
136279 {/* zName: */ "schema_version",
136280 /* ePragTyp: */ PragTyp_HEADER_VALUE,
136281 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
136282 /* ColNames: */ 0, 0,
136283 /* iArg: */ BTREE_SCHEMA_VERSION },
136284#endif
136285#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136286 {/* zName: */ "secure_delete",
136287 /* ePragTyp: */ PragTyp_SECURE_DELETE,
136288 /* ePragFlg: */ PragFlg_Result0,
136289 /* ColNames: */ 0, 0,
136290 /* iArg: */ 0 },
136291#endif
136292#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136293 {/* zName: */ "short_column_names",
136294 /* ePragTyp: */ PragTyp_FLAG,
136295 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136296 /* ColNames: */ 0, 0,
136297 /* iArg: */ SQLITE_ShortColNames },
136298#endif
136299 {/* zName: */ "shrink_memory",
136300 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
136301 /* ePragFlg: */ PragFlg_NoColumns,
136302 /* ColNames: */ 0, 0,
136303 /* iArg: */ 0 },
136304 {/* zName: */ "soft_heap_limit",
136305 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
136306 /* ePragFlg: */ PragFlg_Result0,
136307 /* ColNames: */ 0, 0,
136308 /* iArg: */ 0 },
136309#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136310#if defined(SQLITE_DEBUG)
136311 {/* zName: */ "sql_trace",
136312 /* ePragTyp: */ PragTyp_FLAG,
136313 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136314 /* ColNames: */ 0, 0,
136315 /* iArg: */ SQLITE_SqlTrace },
136316#endif
136317#endif
136318#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
136319 {/* zName: */ "stats",
136320 /* ePragTyp: */ PragTyp_STATS,
136321 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
136322 /* ColNames: */ 33, 5,
136323 /* iArg: */ 0 },
136324#endif
136325#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136326 {/* zName: */ "synchronous",
136327 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
136328 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
136329 /* ColNames: */ 0, 0,
136330 /* iArg: */ 0 },
136331#endif
136332#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
136333 {/* zName: */ "table_info",
136334 /* ePragTyp: */ PragTyp_TABLE_INFO,
136335 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
136336 /* ColNames: */ 8, 6,
136337 /* iArg: */ 0 },
136338 {/* zName: */ "table_list",
136339 /* ePragTyp: */ PragTyp_TABLE_LIST,
136340 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1,
136341 /* ColNames: */ 15, 6,
136342 /* iArg: */ 0 },
136343 {/* zName: */ "table_xinfo",
136344 /* ePragTyp: */ PragTyp_TABLE_INFO,
136345 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
136346 /* ColNames: */ 8, 7,
136347 /* iArg: */ 1 },
136348#endif
136349#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136350 {/* zName: */ "temp_store",
136351 /* ePragTyp: */ PragTyp_TEMP_STORE,
136352 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136353 /* ColNames: */ 0, 0,
136354 /* iArg: */ 0 },
136355 {/* zName: */ "temp_store_directory",
136356 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
136357 /* ePragFlg: */ PragFlg_NoColumns1,
136358 /* ColNames: */ 0, 0,
136359 /* iArg: */ 0 },
136360#endif
136361 {/* zName: */ "threads",
136362 /* ePragTyp: */ PragTyp_THREADS,
136363 /* ePragFlg: */ PragFlg_Result0,
136364 /* ColNames: */ 0, 0,
136365 /* iArg: */ 0 },
136366#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136367 {/* zName: */ "trusted_schema",
136368 /* ePragTyp: */ PragTyp_FLAG,
136369 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136370 /* ColNames: */ 0, 0,
136371 /* iArg: */ SQLITE_TrustedSchema },
136372#endif
136373#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
136374 {/* zName: */ "user_version",
136375 /* ePragTyp: */ PragTyp_HEADER_VALUE,
136376 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
136377 /* ColNames: */ 0, 0,
136378 /* iArg: */ BTREE_USER_VERSION },
136379#endif
136380#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136381#if defined(SQLITE_DEBUG)
136382 {/* zName: */ "vdbe_addoptrace",
136383 /* ePragTyp: */ PragTyp_FLAG,
136384 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136385 /* ColNames: */ 0, 0,
136386 /* iArg: */ SQLITE_VdbeAddopTrace },
136387 {/* zName: */ "vdbe_debug",
136388 /* ePragTyp: */ PragTyp_FLAG,
136389 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136390 /* ColNames: */ 0, 0,
136391 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
136392 {/* zName: */ "vdbe_eqp",
136393 /* ePragTyp: */ PragTyp_FLAG,
136394 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136395 /* ColNames: */ 0, 0,
136396 /* iArg: */ SQLITE_VdbeEQP },
136397 {/* zName: */ "vdbe_listing",
136398 /* ePragTyp: */ PragTyp_FLAG,
136399 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136400 /* ColNames: */ 0, 0,
136401 /* iArg: */ SQLITE_VdbeListing },
136402 {/* zName: */ "vdbe_trace",
136403 /* ePragTyp: */ PragTyp_FLAG,
136404 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136405 /* ColNames: */ 0, 0,
136406 /* iArg: */ SQLITE_VdbeTrace },
136407#endif
136408#endif
136409#if !defined(SQLITE_OMIT_WAL)
136410 {/* zName: */ "wal_autocheckpoint",
136411 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
136412 /* ePragFlg: */ 0,
136413 /* ColNames: */ 0, 0,
136414 /* iArg: */ 0 },
136415 {/* zName: */ "wal_checkpoint",
136416 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
136417 /* ePragFlg: */ PragFlg_NeedSchema,
136418 /* ColNames: */ 50, 3,
136419 /* iArg: */ 0 },
136420#endif
136421#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136422 {/* zName: */ "writable_schema",
136423 /* ePragTyp: */ PragTyp_FLAG,
136424 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
136425 /* ColNames: */ 0, 0,
136426 /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
136427#endif
136428};
136429/* Number of pragmas: 68 on by default, 78 total. */
136430
136431/************** End of pragma.h **********************************************/
136432/************** Continuing where we left off in pragma.c *********************/
136433
136434/*
136435** Interpret the given string as a safety level. Return 0 for OFF,
136436** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
136437** unrecognized string argument. The FULL and EXTRA option is disallowed
136438** if the omitFull parameter it 1.
136439**
136440** Note that the values returned are one less that the values that
136441** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
136442** to support legacy SQL code. The safety level used to be boolean
136443** and older scripts may have used numbers 0 for OFF and 1 for ON.
136444*/
136445static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
136446 /* 123456789 123456789 123 */
136447 static const char zText[] = "onoffalseyestruextrafull";
136448 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
136449 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
136450 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
136451 /* on no off false yes true extra full */
136452 int i, n;
136453 if( sqlite3Isdigit(*z) ){
136454 return (u8)sqlite3Atoi(z);
136455 }
136456 n = sqlite3Strlen30(z);
136457 for(i=0; i<ArraySize(iLength); i++){
136458 if( iLength[i]==n && sqlite3StrNICmp(zLeft: &zText[iOffset[i]],zRight: z,N: n)==0
136459 && (!omitFull || iValue[i]<=1)
136460 ){
136461 return iValue[i];
136462 }
136463 }
136464 return dflt;
136465}
136466
136467/*
136468** Interpret the given string as a boolean value.
136469*/
136470SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
136471 return getSafetyLevel(z,omitFull: 1,dflt)!=0;
136472}
136473
136474/* The sqlite3GetBoolean() function is used by other modules but the
136475** remainder of this file is specific to PRAGMA processing. So omit
136476** the rest of the file if PRAGMAs are omitted from the build.
136477*/
136478#if !defined(SQLITE_OMIT_PRAGMA)
136479
136480/*
136481** Interpret the given string as a locking mode value.
136482*/
136483static int getLockingMode(const char *z){
136484 if( z ){
136485 if( 0==sqlite3StrICmp(zLeft: z, zRight: "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
136486 if( 0==sqlite3StrICmp(zLeft: z, zRight: "normal") ) return PAGER_LOCKINGMODE_NORMAL;
136487 }
136488 return PAGER_LOCKINGMODE_QUERY;
136489}
136490
136491#ifndef SQLITE_OMIT_AUTOVACUUM
136492/*
136493** Interpret the given string as an auto-vacuum mode value.
136494**
136495** The following strings, "none", "full" and "incremental" are
136496** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
136497*/
136498static int getAutoVacuum(const char *z){
136499 int i;
136500 if( 0==sqlite3StrICmp(zLeft: z, zRight: "none") ) return BTREE_AUTOVACUUM_NONE;
136501 if( 0==sqlite3StrICmp(zLeft: z, zRight: "full") ) return BTREE_AUTOVACUUM_FULL;
136502 if( 0==sqlite3StrICmp(zLeft: z, zRight: "incremental") ) return BTREE_AUTOVACUUM_INCR;
136503 i = sqlite3Atoi(z);
136504 return (u8)((i>=0&&i<=2)?i:0);
136505}
136506#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
136507
136508#ifndef SQLITE_OMIT_PAGER_PRAGMAS
136509/*
136510** Interpret the given string as a temp db location. Return 1 for file
136511** backed temporary databases, 2 for the Red-Black tree in memory database
136512** and 0 to use the compile-time default.
136513*/
136514static int getTempStore(const char *z){
136515 if( z[0]>='0' && z[0]<='2' ){
136516 return z[0] - '0';
136517 }else if( sqlite3StrICmp(zLeft: z, zRight: "file")==0 ){
136518 return 1;
136519 }else if( sqlite3StrICmp(zLeft: z, zRight: "memory")==0 ){
136520 return 2;
136521 }else{
136522 return 0;
136523 }
136524}
136525#endif /* SQLITE_PAGER_PRAGMAS */
136526
136527#ifndef SQLITE_OMIT_PAGER_PRAGMAS
136528/*
136529** Invalidate temp storage, either when the temp storage is changed
136530** from default, or when 'file' and the temp_store_directory has changed
136531*/
136532static int invalidateTempStorage(Parse *pParse){
136533 sqlite3 *db = pParse->db;
136534 if( db->aDb[1].pBt!=0 ){
136535 if( !db->autoCommit
136536 || sqlite3BtreeTxnState(p: db->aDb[1].pBt)!=SQLITE_TXN_NONE
136537 ){
136538 sqlite3ErrorMsg(pParse, zFormat: "temporary storage cannot be changed "
136539 "from within a transaction");
136540 return SQLITE_ERROR;
136541 }
136542 sqlite3BtreeClose(p: db->aDb[1].pBt);
136543 db->aDb[1].pBt = 0;
136544 sqlite3ResetAllSchemasOfConnection(db);
136545 }
136546 return SQLITE_OK;
136547}
136548#endif /* SQLITE_PAGER_PRAGMAS */
136549
136550#ifndef SQLITE_OMIT_PAGER_PRAGMAS
136551/*
136552** If the TEMP database is open, close it and mark the database schema
136553** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
136554** or DEFAULT_TEMP_STORE pragmas.
136555*/
136556static int changeTempStorage(Parse *pParse, const char *zStorageType){
136557 int ts = getTempStore(z: zStorageType);
136558 sqlite3 *db = pParse->db;
136559 if( db->temp_store==ts ) return SQLITE_OK;
136560 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
136561 return SQLITE_ERROR;
136562 }
136563 db->temp_store = (u8)ts;
136564 return SQLITE_OK;
136565}
136566#endif /* SQLITE_PAGER_PRAGMAS */
136567
136568/*
136569** Set result column names for a pragma.
136570*/
136571static void setPragmaResultColumnNames(
136572 Vdbe *v, /* The query under construction */
136573 const PragmaName *pPragma /* The pragma */
136574){
136575 u8 n = pPragma->nPragCName;
136576 sqlite3VdbeSetNumCols(p: v, nResColumn: n==0 ? 1 : n);
136577 if( n==0 ){
136578 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: pPragma->zName, SQLITE_STATIC);
136579 }else{
136580 int i, j;
136581 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
136582 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName: pragCName[j], SQLITE_STATIC);
136583 }
136584 }
136585}
136586
136587/*
136588** Generate code to return a single integer value.
136589*/
136590static void returnSingleInt(Vdbe *v, i64 value){
136591 sqlite3VdbeAddOp4Dup8(p: v, OP_Int64, p1: 0, p2: 1, p3: 0, zP4: (const u8*)&value, P4_INT64);
136592 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
136593}
136594
136595/*
136596** Generate code to return a single text value.
136597*/
136598static void returnSingleText(
136599 Vdbe *v, /* Prepared statement under construction */
136600 const char *zValue /* Value to be returned */
136601){
136602 if( zValue ){
136603 sqlite3VdbeLoadString(p: v, iDest: 1, zStr: (const char*)zValue);
136604 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
136605 }
136606}
136607
136608
136609/*
136610** Set the safety_level and pager flags for pager iDb. Or if iDb<0
136611** set these values for all pagers.
136612*/
136613#ifndef SQLITE_OMIT_PAGER_PRAGMAS
136614static void setAllPagerFlags(sqlite3 *db){
136615 if( db->autoCommit ){
136616 Db *pDb = db->aDb;
136617 int n = db->nDb;
136618 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
136619 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
136620 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
136621 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
136622 == PAGER_FLAGS_MASK );
136623 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
136624 while( (n--) > 0 ){
136625 if( pDb->pBt ){
136626 sqlite3BtreeSetPagerFlags(p: pDb->pBt,
136627 pgFlags: pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
136628 }
136629 pDb++;
136630 }
136631 }
136632}
136633#else
136634# define setAllPagerFlags(X) /* no-op */
136635#endif
136636
136637
136638/*
136639** Return a human-readable name for a constraint resolution action.
136640*/
136641#ifndef SQLITE_OMIT_FOREIGN_KEY
136642static const char *actionName(u8 action){
136643 const char *zName;
136644 switch( action ){
136645 case OE_SetNull: zName = "SET NULL"; break;
136646 case OE_SetDflt: zName = "SET DEFAULT"; break;
136647 case OE_Cascade: zName = "CASCADE"; break;
136648 case OE_Restrict: zName = "RESTRICT"; break;
136649 default: zName = "NO ACTION";
136650 assert( action==OE_None ); break;
136651 }
136652 return zName;
136653}
136654#endif
136655
136656
136657/*
136658** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
136659** defined in pager.h. This function returns the associated lowercase
136660** journal-mode name.
136661*/
136662SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
136663 static char * const azModeName[] = {
136664 "delete", "persist", "off", "truncate", "memory"
136665#ifndef SQLITE_OMIT_WAL
136666 , "wal"
136667#endif
136668 };
136669 assert( PAGER_JOURNALMODE_DELETE==0 );
136670 assert( PAGER_JOURNALMODE_PERSIST==1 );
136671 assert( PAGER_JOURNALMODE_OFF==2 );
136672 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
136673 assert( PAGER_JOURNALMODE_MEMORY==4 );
136674 assert( PAGER_JOURNALMODE_WAL==5 );
136675 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
136676
136677 if( eMode==ArraySize(azModeName) ) return 0;
136678 return azModeName[eMode];
136679}
136680
136681/*
136682** Locate a pragma in the aPragmaName[] array.
136683*/
136684static const PragmaName *pragmaLocate(const char *zName){
136685 int upr, lwr, mid = 0, rc;
136686 lwr = 0;
136687 upr = ArraySize(aPragmaName)-1;
136688 while( lwr<=upr ){
136689 mid = (lwr+upr)/2;
136690 rc = sqlite3_stricmp(zLeft: zName, zRight: aPragmaName[mid].zName);
136691 if( rc==0 ) break;
136692 if( rc<0 ){
136693 upr = mid - 1;
136694 }else{
136695 lwr = mid + 1;
136696 }
136697 }
136698 return lwr>upr ? 0 : &aPragmaName[mid];
136699}
136700
136701/*
136702** Create zero or more entries in the output for the SQL functions
136703** defined by FuncDef p.
136704*/
136705static void pragmaFunclistLine(
136706 Vdbe *v, /* The prepared statement being created */
136707 FuncDef *p, /* A particular function definition */
136708 int isBuiltin, /* True if this is a built-in function */
136709 int showInternFuncs /* True if showing internal functions */
136710){
136711 u32 mask =
136712 SQLITE_DETERMINISTIC |
136713 SQLITE_DIRECTONLY |
136714 SQLITE_SUBTYPE |
136715 SQLITE_INNOCUOUS |
136716 SQLITE_FUNC_INTERNAL
136717 ;
136718 if( showInternFuncs ) mask = 0xffffffff;
136719 for(; p; p=p->pNext){
136720 const char *zType;
136721 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
136722
136723 assert( SQLITE_FUNC_ENCMASK==0x3 );
136724 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
136725 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
136726 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
136727
136728 if( p->xSFunc==0 ) continue;
136729 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
136730 && showInternFuncs==0
136731 ){
136732 continue;
136733 }
136734 if( p->xValue!=0 ){
136735 zType = "w";
136736 }else if( p->xFinalize!=0 ){
136737 zType = "a";
136738 }else{
136739 zType = "s";
136740 }
136741 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "sissii",
136742 p->zName, isBuiltin,
136743 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
136744 p->nArg,
136745 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
136746 );
136747 }
136748}
136749
136750
136751/*
136752** Helper subroutine for PRAGMA integrity_check:
136753**
136754** Generate code to output a single-column result row with a value of the
136755** string held in register 3. Decrement the result count in register 1
136756** and halt if the maximum number of result rows have been issued.
136757*/
136758static int integrityCheckResultRow(Vdbe *v){
136759 int addr;
136760 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 3, p2: 1);
136761 addr = sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: 1, p2: sqlite3VdbeCurrentAddr(p: v)+2, p3: 1);
136762 VdbeCoverage(v);
136763 sqlite3VdbeAddOp0(p: v, OP_Halt);
136764 return addr;
136765}
136766
136767/*
136768** Process a pragma statement.
136769**
136770** Pragmas are of this form:
136771**
136772** PRAGMA [schema.]id [= value]
136773**
136774** The identifier might also be a string. The value is a string, and
136775** identifier, or a number. If minusFlag is true, then the value is
136776** a number that was preceded by a minus sign.
136777**
136778** If the left side is "database.id" then pId1 is the database name
136779** and pId2 is the id. If the left side is just "id" then pId1 is the
136780** id and pId2 is any empty string.
136781*/
136782SQLITE_PRIVATE void sqlite3Pragma(
136783 Parse *pParse,
136784 Token *pId1, /* First part of [schema.]id field */
136785 Token *pId2, /* Second part of [schema.]id field, or NULL */
136786 Token *pValue, /* Token for <value>, or NULL */
136787 int minusFlag /* True if a '-' sign preceded <value> */
136788){
136789 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
136790 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
136791 const char *zDb = 0; /* The database name */
136792 Token *pId; /* Pointer to <id> token */
136793 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
136794 int iDb; /* Database index for <database> */
136795 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
136796 sqlite3 *db = pParse->db; /* The database connection */
136797 Db *pDb; /* The specific database being pragmaed */
136798 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
136799 const PragmaName *pPragma; /* The pragma */
136800
136801 if( v==0 ) return;
136802 sqlite3VdbeRunOnlyOnce(p: v);
136803 pParse->nMem = 2;
136804
136805 /* Interpret the [schema.] part of the pragma statement. iDb is the
136806 ** index of the database this pragma is being applied to in db.aDb[]. */
136807 iDb = sqlite3TwoPartName(pParse, pName1: pId1, pName2: pId2, pUnqual: &pId);
136808 if( iDb<0 ) return;
136809 pDb = &db->aDb[iDb];
136810
136811 /* If the temp database has been explicitly named as part of the
136812 ** pragma, make sure it is open.
136813 */
136814 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
136815 return;
136816 }
136817
136818 zLeft = sqlite3NameFromToken(db, pName: pId);
136819 if( !zLeft ) return;
136820 if( minusFlag ){
136821 zRight = sqlite3MPrintf(db, zFormat: "-%T", pValue);
136822 }else{
136823 zRight = sqlite3NameFromToken(db, pName: pValue);
136824 }
136825
136826 assert( pId2 );
136827 zDb = pId2->n>0 ? pDb->zDbSName : 0;
136828 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zArg1: zLeft, zArg2: zRight, zArg3: zDb) ){
136829 goto pragma_out;
136830 }
136831
136832 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
136833 ** connection. If it returns SQLITE_OK, then assume that the VFS
136834 ** handled the pragma and generate a no-op prepared statement.
136835 **
136836 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
136837 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
136838 ** object corresponding to the database file to which the pragma
136839 ** statement refers.
136840 **
136841 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
136842 ** file control is an array of pointers to strings (char**) in which the
136843 ** second element of the array is the name of the pragma and the third
136844 ** element is the argument to the pragma or NULL if the pragma has no
136845 ** argument.
136846 */
136847 aFcntl[0] = 0;
136848 aFcntl[1] = zLeft;
136849 aFcntl[2] = zRight;
136850 aFcntl[3] = 0;
136851 db->busyHandler.nBusy = 0;
136852 rc = sqlite3_file_control(db, zDbName: zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
136853 if( rc==SQLITE_OK ){
136854 sqlite3VdbeSetNumCols(p: v, nResColumn: 1);
136855 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: aFcntl[0], SQLITE_TRANSIENT);
136856 returnSingleText(v, zValue: aFcntl[0]);
136857 sqlite3_free(p: aFcntl[0]);
136858 goto pragma_out;
136859 }
136860 if( rc!=SQLITE_NOTFOUND ){
136861 if( aFcntl[0] ){
136862 sqlite3ErrorMsg(pParse, zFormat: "%s", aFcntl[0]);
136863 sqlite3_free(p: aFcntl[0]);
136864 }
136865 pParse->nErr++;
136866 pParse->rc = rc;
136867 goto pragma_out;
136868 }
136869
136870 /* Locate the pragma in the lookup table */
136871 pPragma = pragmaLocate(zName: zLeft);
136872 if( pPragma==0 ){
136873 /* IMP: R-43042-22504 No error messages are generated if an
136874 ** unknown pragma is issued. */
136875 goto pragma_out;
136876 }
136877
136878 /* Make sure the database schema is loaded if the pragma requires that */
136879 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
136880 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
136881 }
136882
136883 /* Register the result column names for pragmas that return results */
136884 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
136885 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
136886 ){
136887 setPragmaResultColumnNames(v, pPragma);
136888 }
136889
136890 /* Jump to the appropriate pragma handler */
136891 switch( pPragma->ePragTyp ){
136892
136893#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
136894 /*
136895 ** PRAGMA [schema.]default_cache_size
136896 ** PRAGMA [schema.]default_cache_size=N
136897 **
136898 ** The first form reports the current persistent setting for the
136899 ** page cache size. The value returned is the maximum number of
136900 ** pages in the page cache. The second form sets both the current
136901 ** page cache size value and the persistent page cache size value
136902 ** stored in the database file.
136903 **
136904 ** Older versions of SQLite would set the default cache size to a
136905 ** negative number to indicate synchronous=OFF. These days, synchronous
136906 ** is always on by default regardless of the sign of the default cache
136907 ** size. But continue to take the absolute value of the default cache
136908 ** size of historical compatibility.
136909 */
136910 case PragTyp_DEFAULT_CACHE_SIZE: {
136911 static const int iLn = VDBE_OFFSET_LINENO(2);
136912 static const VdbeOpList getCacheSize[] = {
136913 { OP_Transaction, 0, 0, 0}, /* 0 */
136914 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
136915 { OP_IfPos, 1, 8, 0},
136916 { OP_Integer, 0, 2, 0},
136917 { OP_Subtract, 1, 2, 1},
136918 { OP_IfPos, 1, 8, 0},
136919 { OP_Integer, 0, 1, 0}, /* 6 */
136920 { OP_Noop, 0, 0, 0},
136921 { OP_ResultRow, 1, 1, 0},
136922 };
136923 VdbeOp *aOp;
136924 sqlite3VdbeUsesBtree(p: v, i: iDb);
136925 if( !zRight ){
136926 pParse->nMem += 2;
136927 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
136928 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(getCacheSize), aOp: getCacheSize, iLineno: iLn);
136929 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
136930 aOp[0].p1 = iDb;
136931 aOp[1].p1 = iDb;
136932 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
136933 }else{
136934 int size = sqlite3AbsInt32(x: sqlite3Atoi(z: zRight));
136935 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
136936 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_DEFAULT_CACHE_SIZE, p3: size);
136937 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
136938 pDb->pSchema->cache_size = size;
136939 sqlite3BtreeSetCacheSize(p: pDb->pBt, mxPage: pDb->pSchema->cache_size);
136940 }
136941 break;
136942 }
136943#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
136944
136945#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
136946 /*
136947 ** PRAGMA [schema.]page_size
136948 ** PRAGMA [schema.]page_size=N
136949 **
136950 ** The first form reports the current setting for the
136951 ** database page size in bytes. The second form sets the
136952 ** database page size value. The value can only be set if
136953 ** the database has not yet been created.
136954 */
136955 case PragTyp_PAGE_SIZE: {
136956 Btree *pBt = pDb->pBt;
136957 assert( pBt!=0 );
136958 if( !zRight ){
136959 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(p: pBt) : 0;
136960 returnSingleInt(v, value: size);
136961 }else{
136962 /* Malloc may fail when setting the page-size, as there is an internal
136963 ** buffer that the pager module resizes using sqlite3_realloc().
136964 */
136965 db->nextPagesize = sqlite3Atoi(z: zRight);
136966 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(p: pBt, pageSize: db->nextPagesize,nReserve: 0,iFix: 0) ){
136967 sqlite3OomFault(db);
136968 }
136969 }
136970 break;
136971 }
136972
136973 /*
136974 ** PRAGMA [schema.]secure_delete
136975 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
136976 **
136977 ** The first form reports the current setting for the
136978 ** secure_delete flag. The second form changes the secure_delete
136979 ** flag setting and reports the new value.
136980 */
136981 case PragTyp_SECURE_DELETE: {
136982 Btree *pBt = pDb->pBt;
136983 int b = -1;
136984 assert( pBt!=0 );
136985 if( zRight ){
136986 if( sqlite3_stricmp(zLeft: zRight, zRight: "fast")==0 ){
136987 b = 2;
136988 }else{
136989 b = sqlite3GetBoolean(z: zRight, dflt: 0);
136990 }
136991 }
136992 if( pId2->n==0 && b>=0 ){
136993 int ii;
136994 for(ii=0; ii<db->nDb; ii++){
136995 sqlite3BtreeSecureDelete(p: db->aDb[ii].pBt, newFlag: b);
136996 }
136997 }
136998 b = sqlite3BtreeSecureDelete(p: pBt, newFlag: b);
136999 returnSingleInt(v, value: b);
137000 break;
137001 }
137002
137003 /*
137004 ** PRAGMA [schema.]max_page_count
137005 ** PRAGMA [schema.]max_page_count=N
137006 **
137007 ** The first form reports the current setting for the
137008 ** maximum number of pages in the database file. The
137009 ** second form attempts to change this setting. Both
137010 ** forms return the current setting.
137011 **
137012 ** The absolute value of N is used. This is undocumented and might
137013 ** change. The only purpose is to provide an easy way to test
137014 ** the sqlite3AbsInt32() function.
137015 **
137016 ** PRAGMA [schema.]page_count
137017 **
137018 ** Return the number of pages in the specified database.
137019 */
137020 case PragTyp_PAGE_COUNT: {
137021 int iReg;
137022 i64 x = 0;
137023 sqlite3CodeVerifySchema(pParse, iDb);
137024 iReg = ++pParse->nMem;
137025 if( sqlite3Tolower(zLeft[0])=='p' ){
137026 sqlite3VdbeAddOp2(p: v, OP_Pagecount, p1: iDb, p2: iReg);
137027 }else{
137028 if( zRight && sqlite3DecOrHexToI64(z: zRight,pOut: &x)==0 ){
137029 if( x<0 ) x = 0;
137030 else if( x>0xfffffffe ) x = 0xfffffffe;
137031 }else{
137032 x = 0;
137033 }
137034 sqlite3VdbeAddOp3(p: v, OP_MaxPgcnt, p1: iDb, p2: iReg, p3: (int)x);
137035 }
137036 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: iReg, p2: 1);
137037 break;
137038 }
137039
137040 /*
137041 ** PRAGMA [schema.]locking_mode
137042 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
137043 */
137044 case PragTyp_LOCKING_MODE: {
137045 const char *zRet = "normal";
137046 int eMode = getLockingMode(z: zRight);
137047
137048 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
137049 /* Simple "PRAGMA locking_mode;" statement. This is a query for
137050 ** the current default locking mode (which may be different to
137051 ** the locking-mode of the main database).
137052 */
137053 eMode = db->dfltLockMode;
137054 }else{
137055 Pager *pPager;
137056 if( pId2->n==0 ){
137057 /* This indicates that no database name was specified as part
137058 ** of the PRAGMA command. In this case the locking-mode must be
137059 ** set on all attached databases, as well as the main db file.
137060 **
137061 ** Also, the sqlite3.dfltLockMode variable is set so that
137062 ** any subsequently attached databases also use the specified
137063 ** locking mode.
137064 */
137065 int ii;
137066 assert(pDb==&db->aDb[0]);
137067 for(ii=2; ii<db->nDb; ii++){
137068 pPager = sqlite3BtreePager(p: db->aDb[ii].pBt);
137069 sqlite3PagerLockingMode(pPager, eMode);
137070 }
137071 db->dfltLockMode = (u8)eMode;
137072 }
137073 pPager = sqlite3BtreePager(p: pDb->pBt);
137074 eMode = sqlite3PagerLockingMode(pPager, eMode);
137075 }
137076
137077 assert( eMode==PAGER_LOCKINGMODE_NORMAL
137078 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
137079 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
137080 zRet = "exclusive";
137081 }
137082 returnSingleText(v, zValue: zRet);
137083 break;
137084 }
137085
137086 /*
137087 ** PRAGMA [schema.]journal_mode
137088 ** PRAGMA [schema.]journal_mode =
137089 ** (delete|persist|off|truncate|memory|wal|off)
137090 */
137091 case PragTyp_JOURNAL_MODE: {
137092 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
137093 int ii; /* Loop counter */
137094
137095 if( zRight==0 ){
137096 /* If there is no "=MODE" part of the pragma, do a query for the
137097 ** current mode */
137098 eMode = PAGER_JOURNALMODE_QUERY;
137099 }else{
137100 const char *zMode;
137101 int n = sqlite3Strlen30(z: zRight);
137102 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
137103 if( sqlite3StrNICmp(zLeft: zRight, zRight: zMode, N: n)==0 ) break;
137104 }
137105 if( !zMode ){
137106 /* If the "=MODE" part does not match any known journal mode,
137107 ** then do a query */
137108 eMode = PAGER_JOURNALMODE_QUERY;
137109 }
137110 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
137111 /* Do not allow journal-mode "OFF" in defensive since the database
137112 ** can become corrupted using ordinary SQL when the journal is off */
137113 eMode = PAGER_JOURNALMODE_QUERY;
137114 }
137115 }
137116 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
137117 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
137118 iDb = 0;
137119 pId2->n = 1;
137120 }
137121 for(ii=db->nDb-1; ii>=0; ii--){
137122 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
137123 sqlite3VdbeUsesBtree(p: v, i: ii);
137124 sqlite3VdbeAddOp3(p: v, OP_JournalMode, p1: ii, p2: 1, p3: eMode);
137125 }
137126 }
137127 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
137128 break;
137129 }
137130
137131 /*
137132 ** PRAGMA [schema.]journal_size_limit
137133 ** PRAGMA [schema.]journal_size_limit=N
137134 **
137135 ** Get or set the size limit on rollback journal files.
137136 */
137137 case PragTyp_JOURNAL_SIZE_LIMIT: {
137138 Pager *pPager = sqlite3BtreePager(p: pDb->pBt);
137139 i64 iLimit = -2;
137140 if( zRight ){
137141 sqlite3DecOrHexToI64(z: zRight, pOut: &iLimit);
137142 if( iLimit<-1 ) iLimit = -1;
137143 }
137144 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
137145 returnSingleInt(v, value: iLimit);
137146 break;
137147 }
137148
137149#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
137150
137151 /*
137152 ** PRAGMA [schema.]auto_vacuum
137153 ** PRAGMA [schema.]auto_vacuum=N
137154 **
137155 ** Get or set the value of the database 'auto-vacuum' parameter.
137156 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
137157 */
137158#ifndef SQLITE_OMIT_AUTOVACUUM
137159 case PragTyp_AUTO_VACUUM: {
137160 Btree *pBt = pDb->pBt;
137161 assert( pBt!=0 );
137162 if( !zRight ){
137163 returnSingleInt(v, value: sqlite3BtreeGetAutoVacuum(p: pBt));
137164 }else{
137165 int eAuto = getAutoVacuum(z: zRight);
137166 assert( eAuto>=0 && eAuto<=2 );
137167 db->nextAutovac = (u8)eAuto;
137168 /* Call SetAutoVacuum() to set initialize the internal auto and
137169 ** incr-vacuum flags. This is required in case this connection
137170 ** creates the database file. It is important that it is created
137171 ** as an auto-vacuum capable db.
137172 */
137173 rc = sqlite3BtreeSetAutoVacuum(p: pBt, autoVacuum: eAuto);
137174 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
137175 /* When setting the auto_vacuum mode to either "full" or
137176 ** "incremental", write the value of meta[6] in the database
137177 ** file. Before writing to meta[6], check that meta[3] indicates
137178 ** that this really is an auto-vacuum capable database.
137179 */
137180 static const int iLn = VDBE_OFFSET_LINENO(2);
137181 static const VdbeOpList setMeta6[] = {
137182 { OP_Transaction, 0, 1, 0}, /* 0 */
137183 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
137184 { OP_If, 1, 0, 0}, /* 2 */
137185 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
137186 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
137187 };
137188 VdbeOp *aOp;
137189 int iAddr = sqlite3VdbeCurrentAddr(p: v);
137190 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
137191 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(setMeta6), aOp: setMeta6, iLineno: iLn);
137192 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
137193 aOp[0].p1 = iDb;
137194 aOp[1].p1 = iDb;
137195 aOp[2].p2 = iAddr+4;
137196 aOp[4].p1 = iDb;
137197 aOp[4].p3 = eAuto - 1;
137198 sqlite3VdbeUsesBtree(p: v, i: iDb);
137199 }
137200 }
137201 break;
137202 }
137203#endif
137204
137205 /*
137206 ** PRAGMA [schema.]incremental_vacuum(N)
137207 **
137208 ** Do N steps of incremental vacuuming on a database.
137209 */
137210#ifndef SQLITE_OMIT_AUTOVACUUM
137211 case PragTyp_INCREMENTAL_VACUUM: {
137212 int iLimit = 0, addr;
137213 if( zRight==0 || !sqlite3GetInt32(zNum: zRight, pValue: &iLimit) || iLimit<=0 ){
137214 iLimit = 0x7fffffff;
137215 }
137216 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
137217 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: iLimit, p2: 1);
137218 addr = sqlite3VdbeAddOp1(p: v, OP_IncrVacuum, p1: iDb); VdbeCoverage(v);
137219 sqlite3VdbeAddOp1(p: v, OP_ResultRow, p1: 1);
137220 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: 1, p2: -1);
137221 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: 1, p2: addr); VdbeCoverage(v);
137222 sqlite3VdbeJumpHere(p: v, addr);
137223 break;
137224 }
137225#endif
137226
137227#ifndef SQLITE_OMIT_PAGER_PRAGMAS
137228 /*
137229 ** PRAGMA [schema.]cache_size
137230 ** PRAGMA [schema.]cache_size=N
137231 **
137232 ** The first form reports the current local setting for the
137233 ** page cache size. The second form sets the local
137234 ** page cache size value. If N is positive then that is the
137235 ** number of pages in the cache. If N is negative, then the
137236 ** number of pages is adjusted so that the cache uses -N kibibytes
137237 ** of memory.
137238 */
137239 case PragTyp_CACHE_SIZE: {
137240 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
137241 if( !zRight ){
137242 returnSingleInt(v, value: pDb->pSchema->cache_size);
137243 }else{
137244 int size = sqlite3Atoi(z: zRight);
137245 pDb->pSchema->cache_size = size;
137246 sqlite3BtreeSetCacheSize(p: pDb->pBt, mxPage: pDb->pSchema->cache_size);
137247 }
137248 break;
137249 }
137250
137251 /*
137252 ** PRAGMA [schema.]cache_spill
137253 ** PRAGMA cache_spill=BOOLEAN
137254 ** PRAGMA [schema.]cache_spill=N
137255 **
137256 ** The first form reports the current local setting for the
137257 ** page cache spill size. The second form turns cache spill on
137258 ** or off. When turning cache spill on, the size is set to the
137259 ** current cache_size. The third form sets a spill size that
137260 ** may be different form the cache size.
137261 ** If N is positive then that is the
137262 ** number of pages in the cache. If N is negative, then the
137263 ** number of pages is adjusted so that the cache uses -N kibibytes
137264 ** of memory.
137265 **
137266 ** If the number of cache_spill pages is less then the number of
137267 ** cache_size pages, no spilling occurs until the page count exceeds
137268 ** the number of cache_size pages.
137269 **
137270 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
137271 ** not just the schema specified.
137272 */
137273 case PragTyp_CACHE_SPILL: {
137274 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
137275 if( !zRight ){
137276 returnSingleInt(v,
137277 value: (db->flags & SQLITE_CacheSpill)==0 ? 0 :
137278 sqlite3BtreeSetSpillSize(p: pDb->pBt,mxPage: 0));
137279 }else{
137280 int size = 1;
137281 if( sqlite3GetInt32(zNum: zRight, pValue: &size) ){
137282 sqlite3BtreeSetSpillSize(p: pDb->pBt, mxPage: size);
137283 }
137284 if( sqlite3GetBoolean(z: zRight, dflt: size!=0) ){
137285 db->flags |= SQLITE_CacheSpill;
137286 }else{
137287 db->flags &= ~(u64)SQLITE_CacheSpill;
137288 }
137289 setAllPagerFlags(db);
137290 }
137291 break;
137292 }
137293
137294 /*
137295 ** PRAGMA [schema.]mmap_size(N)
137296 **
137297 ** Used to set mapping size limit. The mapping size limit is
137298 ** used to limit the aggregate size of all memory mapped regions of the
137299 ** database file. If this parameter is set to zero, then memory mapping
137300 ** is not used at all. If N is negative, then the default memory map
137301 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
137302 ** The parameter N is measured in bytes.
137303 **
137304 ** This value is advisory. The underlying VFS is free to memory map
137305 ** as little or as much as it wants. Except, if N is set to 0 then the
137306 ** upper layers will never invoke the xFetch interfaces to the VFS.
137307 */
137308 case PragTyp_MMAP_SIZE: {
137309 sqlite3_int64 sz;
137310#if SQLITE_MAX_MMAP_SIZE>0
137311 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
137312 if( zRight ){
137313 int ii;
137314 sqlite3DecOrHexToI64(z: zRight, pOut: &sz);
137315 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
137316 if( pId2->n==0 ) db->szMmap = sz;
137317 for(ii=db->nDb-1; ii>=0; ii--){
137318 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
137319 sqlite3BtreeSetMmapLimit(p: db->aDb[ii].pBt, szMmap: sz);
137320 }
137321 }
137322 }
137323 sz = -1;
137324 rc = sqlite3_file_control(db, zDbName: zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
137325#else
137326 sz = 0;
137327 rc = SQLITE_OK;
137328#endif
137329 if( rc==SQLITE_OK ){
137330 returnSingleInt(v, value: sz);
137331 }else if( rc!=SQLITE_NOTFOUND ){
137332 pParse->nErr++;
137333 pParse->rc = rc;
137334 }
137335 break;
137336 }
137337
137338 /*
137339 ** PRAGMA temp_store
137340 ** PRAGMA temp_store = "default"|"memory"|"file"
137341 **
137342 ** Return or set the local value of the temp_store flag. Changing
137343 ** the local value does not make changes to the disk file and the default
137344 ** value will be restored the next time the database is opened.
137345 **
137346 ** Note that it is possible for the library compile-time options to
137347 ** override this setting
137348 */
137349 case PragTyp_TEMP_STORE: {
137350 if( !zRight ){
137351 returnSingleInt(v, value: db->temp_store);
137352 }else{
137353 changeTempStorage(pParse, zStorageType: zRight);
137354 }
137355 break;
137356 }
137357
137358 /*
137359 ** PRAGMA temp_store_directory
137360 ** PRAGMA temp_store_directory = ""|"directory_name"
137361 **
137362 ** Return or set the local value of the temp_store_directory flag. Changing
137363 ** the value sets a specific directory to be used for temporary files.
137364 ** Setting to a null string reverts to the default temporary directory search.
137365 ** If temporary directory is changed, then invalidateTempStorage.
137366 **
137367 */
137368 case PragTyp_TEMP_STORE_DIRECTORY: {
137369 sqlite3_mutex_enter(p: sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
137370 if( !zRight ){
137371 returnSingleText(v, zValue: sqlite3_temp_directory);
137372 }else{
137373#ifndef SQLITE_OMIT_WSD
137374 if( zRight[0] ){
137375 int res;
137376 rc = sqlite3OsAccess(pVfs: db->pVfs, zPath: zRight, SQLITE_ACCESS_READWRITE, pResOut: &res);
137377 if( rc!=SQLITE_OK || res==0 ){
137378 sqlite3ErrorMsg(pParse, zFormat: "not a writable directory");
137379 sqlite3_mutex_leave(p: sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
137380 goto pragma_out;
137381 }
137382 }
137383 if( SQLITE_TEMP_STORE==0
137384 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
137385 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
137386 ){
137387 invalidateTempStorage(pParse);
137388 }
137389 sqlite3_free(p: sqlite3_temp_directory);
137390 if( zRight[0] ){
137391 sqlite3_temp_directory = sqlite3_mprintf(zFormat: "%s", zRight);
137392 }else{
137393 sqlite3_temp_directory = 0;
137394 }
137395#endif /* SQLITE_OMIT_WSD */
137396 }
137397 sqlite3_mutex_leave(p: sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
137398 break;
137399 }
137400
137401#if SQLITE_OS_WIN
137402 /*
137403 ** PRAGMA data_store_directory
137404 ** PRAGMA data_store_directory = ""|"directory_name"
137405 **
137406 ** Return or set the local value of the data_store_directory flag. Changing
137407 ** the value sets a specific directory to be used for database files that
137408 ** were specified with a relative pathname. Setting to a null string reverts
137409 ** to the default database directory, which for database files specified with
137410 ** a relative path will probably be based on the current directory for the
137411 ** process. Database file specified with an absolute path are not impacted
137412 ** by this setting, regardless of its value.
137413 **
137414 */
137415 case PragTyp_DATA_STORE_DIRECTORY: {
137416 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
137417 if( !zRight ){
137418 returnSingleText(v, sqlite3_data_directory);
137419 }else{
137420#ifndef SQLITE_OMIT_WSD
137421 if( zRight[0] ){
137422 int res;
137423 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
137424 if( rc!=SQLITE_OK || res==0 ){
137425 sqlite3ErrorMsg(pParse, "not a writable directory");
137426 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
137427 goto pragma_out;
137428 }
137429 }
137430 sqlite3_free(sqlite3_data_directory);
137431 if( zRight[0] ){
137432 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
137433 }else{
137434 sqlite3_data_directory = 0;
137435 }
137436#endif /* SQLITE_OMIT_WSD */
137437 }
137438 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
137439 break;
137440 }
137441#endif
137442
137443#if SQLITE_ENABLE_LOCKING_STYLE
137444 /*
137445 ** PRAGMA [schema.]lock_proxy_file
137446 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
137447 **
137448 ** Return or set the value of the lock_proxy_file flag. Changing
137449 ** the value sets a specific file to be used for database access locks.
137450 **
137451 */
137452 case PragTyp_LOCK_PROXY_FILE: {
137453 if( !zRight ){
137454 Pager *pPager = sqlite3BtreePager(pDb->pBt);
137455 char *proxy_file_path = NULL;
137456 sqlite3_file *pFile = sqlite3PagerFile(pPager);
137457 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
137458 &proxy_file_path);
137459 returnSingleText(v, proxy_file_path);
137460 }else{
137461 Pager *pPager = sqlite3BtreePager(pDb->pBt);
137462 sqlite3_file *pFile = sqlite3PagerFile(pPager);
137463 int res;
137464 if( zRight[0] ){
137465 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
137466 zRight);
137467 } else {
137468 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
137469 NULL);
137470 }
137471 if( res!=SQLITE_OK ){
137472 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
137473 goto pragma_out;
137474 }
137475 }
137476 break;
137477 }
137478#endif /* SQLITE_ENABLE_LOCKING_STYLE */
137479
137480 /*
137481 ** PRAGMA [schema.]synchronous
137482 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
137483 **
137484 ** Return or set the local value of the synchronous flag. Changing
137485 ** the local value does not make changes to the disk file and the
137486 ** default value will be restored the next time the database is
137487 ** opened.
137488 */
137489 case PragTyp_SYNCHRONOUS: {
137490 if( !zRight ){
137491 returnSingleInt(v, value: pDb->safety_level-1);
137492 }else{
137493 if( !db->autoCommit ){
137494 sqlite3ErrorMsg(pParse,
137495 zFormat: "Safety level may not be changed inside a transaction");
137496 }else if( iDb!=1 ){
137497 int iLevel = (getSafetyLevel(z: zRight,omitFull: 0,dflt: 1)+1) & PAGER_SYNCHRONOUS_MASK;
137498 if( iLevel==0 ) iLevel = 1;
137499 pDb->safety_level = iLevel;
137500 pDb->bSyncSet = 1;
137501 setAllPagerFlags(db);
137502 }
137503 }
137504 break;
137505 }
137506#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
137507
137508#ifndef SQLITE_OMIT_FLAG_PRAGMAS
137509 case PragTyp_FLAG: {
137510 if( zRight==0 ){
137511 setPragmaResultColumnNames(v, pPragma);
137512 returnSingleInt(v, value: (db->flags & pPragma->iArg)!=0 );
137513 }else{
137514 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
137515 if( db->autoCommit==0 ){
137516 /* Foreign key support may not be enabled or disabled while not
137517 ** in auto-commit mode. */
137518 mask &= ~(SQLITE_ForeignKeys);
137519 }
137520#if SQLITE_USER_AUTHENTICATION
137521 if( db->auth.authLevel==UAUTH_User ){
137522 /* Do not allow non-admin users to modify the schema arbitrarily */
137523 mask &= ~(SQLITE_WriteSchema);
137524 }
137525#endif
137526
137527 if( sqlite3GetBoolean(z: zRight, dflt: 0) ){
137528 db->flags |= mask;
137529 }else{
137530 db->flags &= ~mask;
137531 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
137532 if( (mask & SQLITE_WriteSchema)!=0
137533 && sqlite3_stricmp(zLeft: zRight, zRight: "reset")==0
137534 ){
137535 /* IMP: R-60817-01178 If the argument is "RESET" then schema
137536 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
137537 ** in addition, the schema is reloaded. */
137538 sqlite3ResetAllSchemasOfConnection(db);
137539 }
137540 }
137541
137542 /* Many of the flag-pragmas modify the code generated by the SQL
137543 ** compiler (eg. count_changes). So add an opcode to expire all
137544 ** compiled SQL statements after modifying a pragma value.
137545 */
137546 sqlite3VdbeAddOp0(p: v, OP_Expire);
137547 setAllPagerFlags(db);
137548 }
137549 break;
137550 }
137551#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
137552
137553#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
137554 /*
137555 ** PRAGMA table_info(<table>)
137556 **
137557 ** Return a single row for each column of the named table. The columns of
137558 ** the returned data set are:
137559 **
137560 ** cid: Column id (numbered from left to right, starting at 0)
137561 ** name: Column name
137562 ** type: Column declaration type.
137563 ** notnull: True if 'NOT NULL' is part of column declaration
137564 ** dflt_value: The default value for the column, if any.
137565 ** pk: Non-zero for PK fields.
137566 */
137567 case PragTyp_TABLE_INFO: if( zRight ){
137568 Table *pTab;
137569 sqlite3CodeVerifyNamedSchema(pParse, zDb);
137570 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zName: zRight, zDbase: zDb);
137571 if( pTab ){
137572 int i, k;
137573 int nHidden = 0;
137574 Column *pCol;
137575 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
137576 pParse->nMem = 7;
137577 sqlite3ViewGetColumnNames(pParse, pTable: pTab);
137578 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
137579 int isHidden = 0;
137580 const Expr *pColExpr;
137581 if( pCol->colFlags & COLFLAG_NOINSERT ){
137582 if( pPragma->iArg==0 ){
137583 nHidden++;
137584 continue;
137585 }
137586 if( pCol->colFlags & COLFLAG_VIRTUAL ){
137587 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
137588 }else if( pCol->colFlags & COLFLAG_STORED ){
137589 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
137590 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
137591 isHidden = 1; /* HIDDEN */
137592 }
137593 }
137594 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
137595 k = 0;
137596 }else if( pPk==0 ){
137597 k = 1;
137598 }else{
137599 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
137600 }
137601 pColExpr = sqlite3ColumnExpr(pTab,pCol);
137602 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
137603 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
137604 || isHidden>=2 );
137605 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: pPragma->iArg ? "issisii" : "issisi",
137606 i-nHidden,
137607 pCol->zCnName,
137608 sqlite3ColumnType(pCol,zDflt: ""),
137609 pCol->notNull ? 1 : 0,
137610 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
137611 k,
137612 isHidden);
137613 }
137614 }
137615 }
137616 break;
137617
137618 /*
137619 ** PRAGMA table_list
137620 **
137621 ** Return a single row for each table, virtual table, or view in the
137622 ** entire schema.
137623 **
137624 ** schema: Name of attached database hold this table
137625 ** name: Name of the table itself
137626 ** type: "table", "view", "virtual", "shadow"
137627 ** ncol: Number of columns
137628 ** wr: True for a WITHOUT ROWID table
137629 ** strict: True for a STRICT table
137630 */
137631 case PragTyp_TABLE_LIST: {
137632 int ii;
137633 pParse->nMem = 6;
137634 sqlite3CodeVerifyNamedSchema(pParse, zDb);
137635 for(ii=0; ii<db->nDb; ii++){
137636 HashElem *k;
137637 Hash *pHash;
137638 int initNCol;
137639 if( zDb && sqlite3_stricmp(zLeft: zDb, zRight: db->aDb[ii].zDbSName)!=0 ) continue;
137640
137641 /* Ensure that the Table.nCol field is initialized for all views
137642 ** and virtual tables. Each time we initialize a Table.nCol value
137643 ** for a table, that can potentially disrupt the hash table, so restart
137644 ** the initialization scan.
137645 */
137646 pHash = &db->aDb[ii].pSchema->tblHash;
137647 initNCol = sqliteHashCount(pHash);
137648 while( initNCol-- ){
137649 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
137650 Table *pTab;
137651 if( k==0 ){ initNCol = 0; break; }
137652 pTab = sqliteHashData(k);
137653 if( pTab->nCol==0 ){
137654 char *zSql = sqlite3MPrintf(db, zFormat: "SELECT*FROM\"%w\"", pTab->zName);
137655 if( zSql ){
137656 sqlite3_stmt *pDummy = 0;
137657 (void)sqlite3_prepare(db, zSql, nByte: -1, ppStmt: &pDummy, pzTail: 0);
137658 (void)sqlite3_finalize(pStmt: pDummy);
137659 sqlite3DbFree(db, p: zSql);
137660 }
137661 if( db->mallocFailed ){
137662 sqlite3ErrorMsg(pParse: db->pParse, zFormat: "out of memory");
137663 db->pParse->rc = SQLITE_NOMEM_BKPT;
137664 }
137665 pHash = &db->aDb[ii].pSchema->tblHash;
137666 break;
137667 }
137668 }
137669 }
137670
137671 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
137672 Table *pTab = sqliteHashData(k);
137673 const char *zType;
137674 if( zRight && sqlite3_stricmp(zLeft: zRight, zRight: pTab->zName)!=0 ) continue;
137675 if( IsView(pTab) ){
137676 zType = "view";
137677 }else if( IsVirtual(pTab) ){
137678 zType = "virtual";
137679 }else if( pTab->tabFlags & TF_Shadow ){
137680 zType = "shadow";
137681 }else{
137682 zType = "table";
137683 }
137684 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "sssiii",
137685 db->aDb[ii].zDbSName,
137686 sqlite3PreferredTableName(zName: pTab->zName),
137687 zType,
137688 pTab->nCol,
137689 (pTab->tabFlags & TF_WithoutRowid)!=0,
137690 (pTab->tabFlags & TF_Strict)!=0
137691 );
137692 }
137693 }
137694 }
137695 break;
137696
137697#ifdef SQLITE_DEBUG
137698 case PragTyp_STATS: {
137699 Index *pIdx;
137700 HashElem *i;
137701 pParse->nMem = 5;
137702 sqlite3CodeVerifySchema(pParse, iDb);
137703 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
137704 Table *pTab = sqliteHashData(i);
137705 sqlite3VdbeMultiLoad(v, 1, "ssiii",
137706 sqlite3PreferredTableName(pTab->zName),
137707 0,
137708 pTab->szTabRow,
137709 pTab->nRowLogEst,
137710 pTab->tabFlags);
137711 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
137712 sqlite3VdbeMultiLoad(v, 2, "siiiX",
137713 pIdx->zName,
137714 pIdx->szIdxRow,
137715 pIdx->aiRowLogEst[0],
137716 pIdx->hasStat1);
137717 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
137718 }
137719 }
137720 }
137721 break;
137722#endif
137723
137724 case PragTyp_INDEX_INFO: if( zRight ){
137725 Index *pIdx;
137726 Table *pTab;
137727 pIdx = sqlite3FindIndex(db, zName: zRight, zDb);
137728 if( pIdx==0 ){
137729 /* If there is no index named zRight, check to see if there is a
137730 ** WITHOUT ROWID table named zRight, and if there is, show the
137731 ** structure of the PRIMARY KEY index for that table. */
137732 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zName: zRight, zDbase: zDb);
137733 if( pTab && !HasRowid(pTab) ){
137734 pIdx = sqlite3PrimaryKeyIndex(pTab);
137735 }
137736 }
137737 if( pIdx ){
137738 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
137739 int i;
137740 int mx;
137741 if( pPragma->iArg ){
137742 /* PRAGMA index_xinfo (newer version with more rows and columns) */
137743 mx = pIdx->nColumn;
137744 pParse->nMem = 6;
137745 }else{
137746 /* PRAGMA index_info (legacy version) */
137747 mx = pIdx->nKeyCol;
137748 pParse->nMem = 3;
137749 }
137750 pTab = pIdx->pTable;
137751 sqlite3CodeVerifySchema(pParse, iDb: iIdxDb);
137752 assert( pParse->nMem<=pPragma->nPragCName );
137753 for(i=0; i<mx; i++){
137754 i16 cnum = pIdx->aiColumn[i];
137755 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "iisX", i, cnum,
137756 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
137757 if( pPragma->iArg ){
137758 sqlite3VdbeMultiLoad(p: v, iDest: 4, zTypes: "isiX",
137759 pIdx->aSortOrder[i],
137760 pIdx->azColl[i],
137761 i<pIdx->nKeyCol);
137762 }
137763 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: pParse->nMem);
137764 }
137765 }
137766 }
137767 break;
137768
137769 case PragTyp_INDEX_LIST: if( zRight ){
137770 Index *pIdx;
137771 Table *pTab;
137772 int i;
137773 pTab = sqlite3FindTable(db, zName: zRight, zDatabase: zDb);
137774 if( pTab ){
137775 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
137776 pParse->nMem = 5;
137777 sqlite3CodeVerifySchema(pParse, iDb: iTabDb);
137778 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
137779 const char *azOrigin[] = { "c", "u", "pk" };
137780 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "isisi",
137781 i,
137782 pIdx->zName,
137783 IsUniqueIndex(pIdx),
137784 azOrigin[pIdx->idxType],
137785 pIdx->pPartIdxWhere!=0);
137786 }
137787 }
137788 }
137789 break;
137790
137791 case PragTyp_DATABASE_LIST: {
137792 int i;
137793 pParse->nMem = 3;
137794 for(i=0; i<db->nDb; i++){
137795 if( db->aDb[i].pBt==0 ) continue;
137796 assert( db->aDb[i].zDbSName!=0 );
137797 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "iss",
137798 i,
137799 db->aDb[i].zDbSName,
137800 sqlite3BtreeGetFilename(p: db->aDb[i].pBt));
137801 }
137802 }
137803 break;
137804
137805 case PragTyp_COLLATION_LIST: {
137806 int i = 0;
137807 HashElem *p;
137808 pParse->nMem = 2;
137809 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
137810 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
137811 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "is", i++, pColl->zName);
137812 }
137813 }
137814 break;
137815
137816#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
137817 case PragTyp_FUNCTION_LIST: {
137818 int i;
137819 HashElem *j;
137820 FuncDef *p;
137821 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
137822 pParse->nMem = 6;
137823 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
137824 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
137825 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
137826 pragmaFunclistLine(v, p, isBuiltin: 1, showInternFuncs: showInternFunc);
137827 }
137828 }
137829 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
137830 p = (FuncDef*)sqliteHashData(j);
137831 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
137832 pragmaFunclistLine(v, p, isBuiltin: 0, showInternFuncs: showInternFunc);
137833 }
137834 }
137835 break;
137836
137837#ifndef SQLITE_OMIT_VIRTUALTABLE
137838 case PragTyp_MODULE_LIST: {
137839 HashElem *j;
137840 pParse->nMem = 1;
137841 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
137842 Module *pMod = (Module*)sqliteHashData(j);
137843 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "s", pMod->zName);
137844 }
137845 }
137846 break;
137847#endif /* SQLITE_OMIT_VIRTUALTABLE */
137848
137849 case PragTyp_PRAGMA_LIST: {
137850 int i;
137851 for(i=0; i<ArraySize(aPragmaName); i++){
137852 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "s", aPragmaName[i].zName);
137853 }
137854 }
137855 break;
137856#endif /* SQLITE_INTROSPECTION_PRAGMAS */
137857
137858#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
137859
137860#ifndef SQLITE_OMIT_FOREIGN_KEY
137861 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
137862 FKey *pFK;
137863 Table *pTab;
137864 pTab = sqlite3FindTable(db, zName: zRight, zDatabase: zDb);
137865 if( pTab && IsOrdinaryTable(pTab) ){
137866 pFK = pTab->u.tab.pFKey;
137867 if( pFK ){
137868 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
137869 int i = 0;
137870 pParse->nMem = 8;
137871 sqlite3CodeVerifySchema(pParse, iDb: iTabDb);
137872 while(pFK){
137873 int j;
137874 for(j=0; j<pFK->nCol; j++){
137875 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "iissssss",
137876 i,
137877 j,
137878 pFK->zTo,
137879 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
137880 pFK->aCol[j].zCol,
137881 actionName(action: pFK->aAction[1]), /* ON UPDATE */
137882 actionName(action: pFK->aAction[0]), /* ON DELETE */
137883 "NONE");
137884 }
137885 ++i;
137886 pFK = pFK->pNextFrom;
137887 }
137888 }
137889 }
137890 }
137891 break;
137892#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
137893
137894#ifndef SQLITE_OMIT_FOREIGN_KEY
137895#ifndef SQLITE_OMIT_TRIGGER
137896 case PragTyp_FOREIGN_KEY_CHECK: {
137897 FKey *pFK; /* A foreign key constraint */
137898 Table *pTab; /* Child table contain "REFERENCES" keyword */
137899 Table *pParent; /* Parent table that child points to */
137900 Index *pIdx; /* Index in the parent table */
137901 int i; /* Loop counter: Foreign key number for pTab */
137902 int j; /* Loop counter: Field of the foreign key */
137903 HashElem *k; /* Loop counter: Next table in schema */
137904 int x; /* result variable */
137905 int regResult; /* 3 registers to hold a result row */
137906 int regRow; /* Registers to hold a row from pTab */
137907 int addrTop; /* Top of a loop checking foreign keys */
137908 int addrOk; /* Jump here if the key is OK */
137909 int *aiCols; /* child to parent column mapping */
137910
137911 regResult = pParse->nMem+1;
137912 pParse->nMem += 4;
137913 regRow = ++pParse->nMem;
137914 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
137915 while( k ){
137916 if( zRight ){
137917 pTab = sqlite3LocateTable(pParse, flags: 0, zName: zRight, zDbase: zDb);
137918 k = 0;
137919 }else{
137920 pTab = (Table*)sqliteHashData(k);
137921 k = sqliteHashNext(k);
137922 }
137923 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
137924 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
137925 zDb = db->aDb[iDb].zDbSName;
137926 sqlite3CodeVerifySchema(pParse, iDb);
137927 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 0, zName: pTab->zName);
137928 sqlite3TouchRegister(pParse, iReg: pTab->nCol+regRow);
137929 sqlite3OpenTable(pParse, iCur: 0, iDb, pTab, OP_OpenRead);
137930 sqlite3VdbeLoadString(p: v, iDest: regResult, zStr: pTab->zName);
137931 assert( IsOrdinaryTable(pTab) );
137932 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
137933 pParent = sqlite3FindTable(db, zName: pFK->zTo, zDatabase: zDb);
137934 if( pParent==0 ) continue;
137935 pIdx = 0;
137936 sqlite3TableLock(pParse, iDb, iTab: pParent->tnum, isWriteLock: 0, zName: pParent->zName);
137937 x = sqlite3FkLocateIndex(pParse, pParent, pFKey: pFK, ppIdx: &pIdx, paiCol: 0);
137938 if( x==0 ){
137939 if( pIdx==0 ){
137940 sqlite3OpenTable(pParse, iCur: i, iDb, pTab: pParent, OP_OpenRead);
137941 }else{
137942 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: i, p2: pIdx->tnum, p3: iDb);
137943 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
137944 }
137945 }else{
137946 k = 0;
137947 break;
137948 }
137949 }
137950 assert( pParse->nErr>0 || pFK==0 );
137951 if( pFK ) break;
137952 if( pParse->nTab<i ) pParse->nTab = i;
137953 addrTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: 0); VdbeCoverage(v);
137954 assert( IsOrdinaryTable(pTab) );
137955 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
137956 pParent = sqlite3FindTable(db, zName: pFK->zTo, zDatabase: zDb);
137957 pIdx = 0;
137958 aiCols = 0;
137959 if( pParent ){
137960 x = sqlite3FkLocateIndex(pParse, pParent, pFKey: pFK, ppIdx: &pIdx, paiCol: &aiCols);
137961 assert( x==0 || db->mallocFailed );
137962 }
137963 addrOk = sqlite3VdbeMakeLabel(pParse);
137964
137965 /* Generate code to read the child key values into registers
137966 ** regRow..regRow+n. If any of the child key values are NULL, this
137967 ** row cannot cause an FK violation. Jump directly to addrOk in
137968 ** this case. */
137969 sqlite3TouchRegister(pParse, iReg: regRow + pFK->nCol);
137970 for(j=0; j<pFK->nCol; j++){
137971 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
137972 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: 0, iCol, regOut: regRow+j);
137973 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regRow+j, p2: addrOk); VdbeCoverage(v);
137974 }
137975
137976 /* Generate code to query the parent index for a matching parent
137977 ** key. If a match is found, jump to addrOk. */
137978 if( pIdx ){
137979 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: regRow, p2: pFK->nCol, p3: 0,
137980 zP4: sqlite3IndexAffinityStr(db,pIdx), p4type: pFK->nCol);
137981 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: i, p2: addrOk, p3: regRow, p4: pFK->nCol);
137982 VdbeCoverage(v);
137983 }else if( pParent ){
137984 int jmp = sqlite3VdbeCurrentAddr(p: v)+2;
137985 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: i, p2: jmp, p3: regRow); VdbeCoverage(v);
137986 sqlite3VdbeGoto(p: v, iDest: addrOk);
137987 assert( pFK->nCol==1 || db->mallocFailed );
137988 }
137989
137990 /* Generate code to report an FK violation to the caller. */
137991 if( HasRowid(pTab) ){
137992 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: 0, p2: regResult+1);
137993 }else{
137994 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regResult+1);
137995 }
137996 sqlite3VdbeMultiLoad(p: v, iDest: regResult+2, zTypes: "siX", pFK->zTo, i-1);
137997 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: regResult, p2: 4);
137998 sqlite3VdbeResolveLabel(v, x: addrOk);
137999 sqlite3DbFree(db, p: aiCols);
138000 }
138001 sqlite3VdbeAddOp2(p: v, OP_Next, p1: 0, p2: addrTop+1); VdbeCoverage(v);
138002 sqlite3VdbeJumpHere(p: v, addr: addrTop);
138003 }
138004 }
138005 break;
138006#endif /* !defined(SQLITE_OMIT_TRIGGER) */
138007#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
138008
138009#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
138010 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
138011 ** used will be case sensitive or not depending on the RHS.
138012 */
138013 case PragTyp_CASE_SENSITIVE_LIKE: {
138014 if( zRight ){
138015 sqlite3RegisterLikeFunctions(db, caseSensitive: sqlite3GetBoolean(z: zRight, dflt: 0));
138016 }
138017 }
138018 break;
138019#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
138020
138021#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
138022# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
138023#endif
138024
138025#ifndef SQLITE_OMIT_INTEGRITY_CHECK
138026 /* PRAGMA integrity_check
138027 ** PRAGMA integrity_check(N)
138028 ** PRAGMA quick_check
138029 ** PRAGMA quick_check(N)
138030 **
138031 ** Verify the integrity of the database.
138032 **
138033 ** The "quick_check" is reduced version of
138034 ** integrity_check designed to detect most database corruption
138035 ** without the overhead of cross-checking indexes. Quick_check
138036 ** is linear time whereas integrity_check is O(NlogN).
138037 **
138038 ** The maximum number of errors is 100 by default. A different default
138039 ** can be specified using a numeric parameter N.
138040 **
138041 ** Or, the parameter N can be the name of a table. In that case, only
138042 ** the one table named is verified. The freelist is only verified if
138043 ** the named table is "sqlite_schema" (or one of its aliases).
138044 **
138045 ** All schemas are checked by default. To check just a single
138046 ** schema, use the form:
138047 **
138048 ** PRAGMA schema.integrity_check;
138049 */
138050 case PragTyp_INTEGRITY_CHECK: {
138051 int i, j, addr, mxErr;
138052 Table *pObjTab = 0; /* Check only this one table, if not NULL */
138053
138054 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
138055
138056 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
138057 ** then iDb is set to the index of the database identified by <db>.
138058 ** In this case, the integrity of database iDb only is verified by
138059 ** the VDBE created below.
138060 **
138061 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
138062 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
138063 ** to -1 here, to indicate that the VDBE should verify the integrity
138064 ** of all attached databases. */
138065 assert( iDb>=0 );
138066 assert( iDb==0 || pId2->z );
138067 if( pId2->z==0 ) iDb = -1;
138068
138069 /* Initialize the VDBE program */
138070 pParse->nMem = 6;
138071
138072 /* Set the maximum error count */
138073 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
138074 if( zRight ){
138075 if( sqlite3GetInt32(zNum: zRight, pValue: &mxErr) ){
138076 if( mxErr<=0 ){
138077 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
138078 }
138079 }else{
138080 pObjTab = sqlite3LocateTable(pParse, flags: 0, zName: zRight,
138081 zDbase: iDb>=0 ? db->aDb[iDb].zDbSName : 0);
138082 }
138083 }
138084 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: mxErr-1, p2: 1); /* reg[1] holds errors left */
138085
138086 /* Do an integrity check on each database file */
138087 for(i=0; i<db->nDb; i++){
138088 HashElem *x; /* For looping over tables in the schema */
138089 Hash *pTbls; /* Set of all tables in the schema */
138090 int *aRoot; /* Array of root page numbers of all btrees */
138091 int cnt = 0; /* Number of entries in aRoot[] */
138092 int mxIdx = 0; /* Maximum number of indexes for any table */
138093
138094 if( OMIT_TEMPDB && i==1 ) continue;
138095 if( iDb>=0 && i!=iDb ) continue;
138096
138097 sqlite3CodeVerifySchema(pParse, iDb: i);
138098 pParse->okConstFactor = 0; /* tag-20230327-1 */
138099
138100 /* Do an integrity check of the B-Tree
138101 **
138102 ** Begin by finding the root pages numbers
138103 ** for all tables and indices in the database.
138104 */
138105 assert( sqlite3SchemaMutexHeld(db, i, 0) );
138106 pTbls = &db->aDb[i].pSchema->tblHash;
138107 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
138108 Table *pTab = sqliteHashData(x); /* Current table */
138109 Index *pIdx; /* An index on pTab */
138110 int nIdx; /* Number of indexes on pTab */
138111 if( pObjTab && pObjTab!=pTab ) continue;
138112 if( HasRowid(pTab) ) cnt++;
138113 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
138114 if( nIdx>mxIdx ) mxIdx = nIdx;
138115 }
138116 if( cnt==0 ) continue;
138117 if( pObjTab ) cnt++;
138118 aRoot = sqlite3DbMallocRawNN(db, n: sizeof(int)*(cnt+1));
138119 if( aRoot==0 ) break;
138120 cnt = 0;
138121 if( pObjTab ) aRoot[++cnt] = 0;
138122 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
138123 Table *pTab = sqliteHashData(x);
138124 Index *pIdx;
138125 if( pObjTab && pObjTab!=pTab ) continue;
138126 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
138127 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138128 aRoot[++cnt] = pIdx->tnum;
138129 }
138130 }
138131 aRoot[0] = cnt;
138132
138133 /* Make sure sufficient number of registers have been allocated */
138134 sqlite3TouchRegister(pParse, iReg: 8+mxIdx);
138135 sqlite3ClearTempRegCache(pParse);
138136
138137 /* Do the b-tree integrity checks */
138138 sqlite3VdbeAddOp4(p: v, OP_IntegrityCk, p1: 2, p2: cnt, p3: 1, zP4: (char*)aRoot,P4_INTARRAY);
138139 sqlite3VdbeChangeP5(p: v, p5: (u8)i);
138140 addr = sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: 2); VdbeCoverage(v);
138141 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0,
138142 zP4: sqlite3MPrintf(db, zFormat: "*** in database %s ***\n", db->aDb[i].zDbSName),
138143 P4_DYNAMIC);
138144 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 2, p2: 3, p3: 3);
138145 integrityCheckResultRow(v);
138146 sqlite3VdbeJumpHere(p: v, addr);
138147
138148 /* Make sure all the indices are constructed correctly.
138149 */
138150 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
138151 Table *pTab = sqliteHashData(x);
138152 Index *pIdx, *pPk;
138153 Index *pPrior = 0; /* Previous index */
138154 int loopTop;
138155 int iDataCur, iIdxCur;
138156 int r1 = -1;
138157 int bStrict; /* True for a STRICT table */
138158 int r2; /* Previous key for WITHOUT ROWID tables */
138159 int mxCol; /* Maximum non-virtual column number */
138160
138161 if( !IsOrdinaryTable(pTab) ) continue;
138162 if( pObjTab && pObjTab!=pTab ) continue;
138163 if( isQuick || HasRowid(pTab) ){
138164 pPk = 0;
138165 r2 = 0;
138166 }else{
138167 pPk = sqlite3PrimaryKeyIndex(pTab);
138168 r2 = sqlite3GetTempRange(pParse, nReg: pPk->nKeyCol);
138169 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 1, p2: r2, p3: r2+pPk->nKeyCol-1);
138170 }
138171 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, p5: 0,
138172 iBase: 1, aToOpen: 0, piDataCur: &iDataCur, piIdxCur: &iIdxCur);
138173 /* reg[7] counts the number of entries in the table.
138174 ** reg[8+i] counts the number of entries in the i-th index
138175 */
138176 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: 7);
138177 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
138178 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: 8+j); /* index entries counter */
138179 }
138180 assert( pParse->nMem>=8+j );
138181 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
138182 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iDataCur, p2: 0); VdbeCoverage(v);
138183 loopTop = sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: 7, p2: 1);
138184
138185 /* Fetch the right-most column from the table. This will cause
138186 ** the entire record header to be parsed and sanity checked. It
138187 ** will also prepopulate the cursor column cache that is used
138188 ** by the OP_IsType code, so it is a required step.
138189 */
138190 assert( !IsVirtual(pTab) );
138191 if( HasRowid(pTab) ){
138192 mxCol = -1;
138193 for(j=0; j<pTab->nCol; j++){
138194 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
138195 }
138196 if( mxCol==pTab->iPKey ) mxCol--;
138197 }else{
138198 /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID
138199 ** PK index column-count, so there is no need to account for them
138200 ** in this case. */
138201 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
138202 }
138203 if( mxCol>=0 ){
138204 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iDataCur, p2: mxCol, p3: 3);
138205 sqlite3VdbeTypeofColumn(p: v, iDest: 3);
138206 }
138207
138208 if( !isQuick ){
138209 if( pPk ){
138210 /* Verify WITHOUT ROWID keys are in ascending order */
138211 int a1;
138212 char *zErr;
138213 a1 = sqlite3VdbeAddOp4Int(p: v, OP_IdxGT, p1: iDataCur, p2: 0,p3: r2,p4: pPk->nKeyCol);
138214 VdbeCoverage(v);
138215 sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: r2); VdbeCoverage(v);
138216 zErr = sqlite3MPrintf(db,
138217 zFormat: "row not in PRIMARY KEY order for %s",
138218 pTab->zName);
138219 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
138220 integrityCheckResultRow(v);
138221 sqlite3VdbeJumpHere(p: v, addr: a1);
138222 sqlite3VdbeJumpHere(p: v, addr: a1+1);
138223 for(j=0; j<pPk->nKeyCol; j++){
138224 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx: pPk, iTabCur: iDataCur, iIdxCol: j, regOut: r2+j);
138225 }
138226 }
138227 }
138228 /* Verify datatypes for all columns:
138229 **
138230 ** (1) NOT NULL columns may not contain a NULL
138231 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
138232 ** (3) Datatype for TEXT columns in non-STRICT tables must be
138233 ** NULL, TEXT, or BLOB.
138234 ** (4) Datatype for numeric columns in non-STRICT tables must not
138235 ** be a TEXT value that can be losslessly converted to numeric.
138236 */
138237 bStrict = (pTab->tabFlags & TF_Strict)!=0;
138238 for(j=0; j<pTab->nCol; j++){
138239 char *zErr;
138240 Column *pCol = pTab->aCol + j; /* The column to be checked */
138241 int labelError; /* Jump here to report an error */
138242 int labelOk; /* Jump here if all looks ok */
138243 int p1, p3, p4; /* Operands to the OP_IsType opcode */
138244 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
138245
138246 if( j==pTab->iPKey ) continue;
138247 if( bStrict ){
138248 doTypeCheck = pCol->eCType>COLTYPE_ANY;
138249 }else{
138250 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
138251 }
138252 if( pCol->notNull==0 && !doTypeCheck ) continue;
138253
138254 /* Compute the operands that will be needed for OP_IsType */
138255 p4 = SQLITE_NULL;
138256 if( pCol->colFlags & COLFLAG_VIRTUAL ){
138257 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: j, regOut: 3);
138258 p1 = -1;
138259 p3 = 3;
138260 }else{
138261 if( pCol->iDflt ){
138262 sqlite3_value *pDfltValue = 0;
138263 sqlite3ValueFromExpr(db, pExpr: sqlite3ColumnExpr(pTab,pCol), ENC(db),
138264 affinity: pCol->affinity, ppVal: &pDfltValue);
138265 if( pDfltValue ){
138266 p4 = sqlite3_value_type(pVal: pDfltValue);
138267 sqlite3ValueFree(v: pDfltValue);
138268 }
138269 }
138270 p1 = iDataCur;
138271 if( !HasRowid(pTab) ){
138272 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
138273 p3 = sqlite3TableColumnToIndex(pIdx: sqlite3PrimaryKeyIndex(pTab), iCol: j);
138274 }else{
138275 p3 = sqlite3TableColumnToStorage(pTab,iCol: j);
138276 testcase( p3!=j);
138277 }
138278 }
138279
138280 labelError = sqlite3VdbeMakeLabel(pParse);
138281 labelOk = sqlite3VdbeMakeLabel(pParse);
138282 if( pCol->notNull ){
138283 /* (1) NOT NULL columns may not contain a NULL */
138284 int jmp3;
138285 int jmp2 = sqlite3VdbeAddOp4Int(p: v, OP_IsType, p1, p2: labelOk, p3, p4);
138286 VdbeCoverage(v);
138287 if( p1<0 ){
138288 sqlite3VdbeChangeP5(p: v, p5: 0x0f); /* INT, REAL, TEXT, or BLOB */
138289 jmp3 = jmp2;
138290 }else{
138291 sqlite3VdbeChangeP5(p: v, p5: 0x0d); /* INT, TEXT, or BLOB */
138292 /* OP_IsType does not detect NaN values in the database file
138293 ** which should be treated as a NULL. So if the header type
138294 ** is REAL, we have to load the actual data using OP_Column
138295 ** to reliably determine if the value is a NULL. */
138296 sqlite3VdbeAddOp3(p: v, OP_Column, p1, p2: p3, p3: 3);
138297 jmp3 = sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: 3, p2: labelOk);
138298 VdbeCoverage(v);
138299 }
138300 zErr = sqlite3MPrintf(db, zFormat: "NULL value in %s.%s", pTab->zName,
138301 pCol->zCnName);
138302 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
138303 if( doTypeCheck ){
138304 sqlite3VdbeGoto(p: v, iDest: labelError);
138305 sqlite3VdbeJumpHere(p: v, addr: jmp2);
138306 sqlite3VdbeJumpHere(p: v, addr: jmp3);
138307 }else{
138308 /* VDBE byte code will fall thru */
138309 }
138310 }
138311 if( bStrict && doTypeCheck ){
138312 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
138313 static unsigned char aStdTypeMask[] = {
138314 0x1f, /* ANY */
138315 0x18, /* BLOB */
138316 0x11, /* INT */
138317 0x11, /* INTEGER */
138318 0x13, /* REAL */
138319 0x14 /* TEXT */
138320 };
138321 sqlite3VdbeAddOp4Int(p: v, OP_IsType, p1, p2: labelOk, p3, p4);
138322 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
138323 sqlite3VdbeChangeP5(p: v, p5: aStdTypeMask[pCol->eCType-1]);
138324 VdbeCoverage(v);
138325 zErr = sqlite3MPrintf(db, zFormat: "non-%s value in %s.%s",
138326 sqlite3StdType[pCol->eCType-1],
138327 pTab->zName, pTab->aCol[j].zCnName);
138328 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
138329 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
138330 /* (3) Datatype for TEXT columns in non-STRICT tables must be
138331 ** NULL, TEXT, or BLOB. */
138332 sqlite3VdbeAddOp4Int(p: v, OP_IsType, p1, p2: labelOk, p3, p4);
138333 sqlite3VdbeChangeP5(p: v, p5: 0x1c); /* NULL, TEXT, or BLOB */
138334 VdbeCoverage(v);
138335 zErr = sqlite3MPrintf(db, zFormat: "NUMERIC value in %s.%s",
138336 pTab->zName, pTab->aCol[j].zCnName);
138337 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
138338 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
138339 /* (4) Datatype for numeric columns in non-STRICT tables must not
138340 ** be a TEXT value that can be converted to numeric. */
138341 sqlite3VdbeAddOp4Int(p: v, OP_IsType, p1, p2: labelOk, p3, p4);
138342 sqlite3VdbeChangeP5(p: v, p5: 0x1b); /* NULL, INT, FLOAT, or BLOB */
138343 VdbeCoverage(v);
138344 if( p1>=0 ){
138345 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: j, regOut: 3);
138346 }
138347 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: 3, p2: 1, p3: 0, zP4: "C", P4_STATIC);
138348 sqlite3VdbeAddOp4Int(p: v, OP_IsType, p1: -1, p2: labelOk, p3: 3, p4);
138349 sqlite3VdbeChangeP5(p: v, p5: 0x1c); /* NULL, TEXT, or BLOB */
138350 VdbeCoverage(v);
138351 zErr = sqlite3MPrintf(db, zFormat: "TEXT value in %s.%s",
138352 pTab->zName, pTab->aCol[j].zCnName);
138353 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
138354 }
138355 sqlite3VdbeResolveLabel(v, x: labelError);
138356 integrityCheckResultRow(v);
138357 sqlite3VdbeResolveLabel(v, x: labelOk);
138358 }
138359 /* Verify CHECK constraints */
138360 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
138361 ExprList *pCheck = sqlite3ExprListDup(db, p: pTab->pCheck, flags: 0);
138362 if( db->mallocFailed==0 ){
138363 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
138364 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
138365 char *zErr;
138366 int k;
138367 pParse->iSelfTab = iDataCur + 1;
138368 for(k=pCheck->nExpr-1; k>0; k--){
138369 sqlite3ExprIfFalse(pParse, pExpr: pCheck->a[k].pExpr, dest: addrCkFault, jumpIfNull: 0);
138370 }
138371 sqlite3ExprIfTrue(pParse, pExpr: pCheck->a[0].pExpr, dest: addrCkOk,
138372 SQLITE_JUMPIFNULL);
138373 sqlite3VdbeResolveLabel(v, x: addrCkFault);
138374 pParse->iSelfTab = 0;
138375 zErr = sqlite3MPrintf(db, zFormat: "CHECK constraint failed in %s",
138376 pTab->zName);
138377 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
138378 integrityCheckResultRow(v);
138379 sqlite3VdbeResolveLabel(v, x: addrCkOk);
138380 }
138381 sqlite3ExprListDelete(db, pList: pCheck);
138382 }
138383 if( !isQuick ){ /* Omit the remaining tests for quick_check */
138384 /* Validate index entries for the current row */
138385 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
138386 int jmp2, jmp3, jmp4, jmp5, label6;
138387 int kk;
138388 int ckUniq = sqlite3VdbeMakeLabel(pParse);
138389 if( pPk==pIdx ) continue;
138390 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, regOut: 0, prefixOnly: 0, piPartIdxLabel: &jmp3,
138391 pPrior, regPrior: r1);
138392 pPrior = pIdx;
138393 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: 8+j, p2: 1);/* increment entry count */
138394 /* Verify that an index entry exists for the current table row */
138395 jmp2 = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iIdxCur+j, p2: ckUniq, p3: r1,
138396 p4: pIdx->nColumn); VdbeCoverage(v);
138397 sqlite3VdbeLoadString(p: v, iDest: 3, zStr: "row ");
138398 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 7, p2: 3, p3: 3);
138399 sqlite3VdbeLoadString(p: v, iDest: 4, zStr: " missing from index ");
138400 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 4, p2: 3, p3: 3);
138401 jmp5 = sqlite3VdbeLoadString(p: v, iDest: 4, zStr: pIdx->zName);
138402 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 4, p2: 3, p3: 3);
138403 jmp4 = integrityCheckResultRow(v);
138404 sqlite3VdbeJumpHere(p: v, addr: jmp2);
138405
138406 /* The OP_IdxRowid opcode is an optimized version of OP_Column
138407 ** that extracts the rowid off the end of the index record.
138408 ** But it only works correctly if index record does not have
138409 ** any extra bytes at the end. Verify that this is the case. */
138410 if( HasRowid(pTab) ){
138411 int jmp7;
138412 sqlite3VdbeAddOp2(p: v, OP_IdxRowid, p1: iIdxCur+j, p2: 3);
138413 jmp7 = sqlite3VdbeAddOp3(p: v, OP_Eq, p1: 3, p2: 0, p3: r1+pIdx->nColumn-1);
138414 VdbeCoverageNeverNull(v);
138415 sqlite3VdbeLoadString(p: v, iDest: 3,
138416 zStr: "rowid not at end-of-record for row ");
138417 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 7, p2: 3, p3: 3);
138418 sqlite3VdbeLoadString(p: v, iDest: 4, zStr: " of index ");
138419 sqlite3VdbeGoto(p: v, iDest: jmp5-1);
138420 sqlite3VdbeJumpHere(p: v, addr: jmp7);
138421 }
138422
138423 /* Any indexed columns with non-BINARY collations must still hold
138424 ** the exact same text value as the table. */
138425 label6 = 0;
138426 for(kk=0; kk<pIdx->nKeyCol; kk++){
138427 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
138428 if( label6==0 ) label6 = sqlite3VdbeMakeLabel(pParse);
138429 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur+j, p2: kk, p3: 3);
138430 sqlite3VdbeAddOp3(p: v, OP_Ne, p1: 3, p2: label6, p3: r1+kk); VdbeCoverage(v);
138431 }
138432 if( label6 ){
138433 int jmp6 = sqlite3VdbeAddOp0(p: v, OP_Goto);
138434 sqlite3VdbeResolveLabel(v, x: label6);
138435 sqlite3VdbeLoadString(p: v, iDest: 3, zStr: "row ");
138436 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 7, p2: 3, p3: 3);
138437 sqlite3VdbeLoadString(p: v, iDest: 4, zStr: " values differ from index ");
138438 sqlite3VdbeGoto(p: v, iDest: jmp5-1);
138439 sqlite3VdbeJumpHere(p: v, addr: jmp6);
138440 }
138441
138442 /* For UNIQUE indexes, verify that only one entry exists with the
138443 ** current key. The entry is unique if (1) any column is NULL
138444 ** or (2) the next entry has a different key */
138445 if( IsUniqueIndex(pIdx) ){
138446 int uniqOk = sqlite3VdbeMakeLabel(pParse);
138447 int jmp6;
138448 for(kk=0; kk<pIdx->nKeyCol; kk++){
138449 int iCol = pIdx->aiColumn[kk];
138450 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
138451 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
138452 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: r1+kk, p2: uniqOk);
138453 VdbeCoverage(v);
138454 }
138455 jmp6 = sqlite3VdbeAddOp1(p: v, OP_Next, p1: iIdxCur+j); VdbeCoverage(v);
138456 sqlite3VdbeGoto(p: v, iDest: uniqOk);
138457 sqlite3VdbeJumpHere(p: v, addr: jmp6);
138458 sqlite3VdbeAddOp4Int(p: v, OP_IdxGT, p1: iIdxCur+j, p2: uniqOk, p3: r1,
138459 p4: pIdx->nKeyCol); VdbeCoverage(v);
138460 sqlite3VdbeLoadString(p: v, iDest: 3, zStr: "non-unique entry in index ");
138461 sqlite3VdbeGoto(p: v, iDest: jmp5);
138462 sqlite3VdbeResolveLabel(v, x: uniqOk);
138463 }
138464 sqlite3VdbeJumpHere(p: v, addr: jmp4);
138465 sqlite3ResolvePartIdxLabel(pParse, iLabel: jmp3);
138466 }
138467 }
138468 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iDataCur, p2: loopTop); VdbeCoverage(v);
138469 sqlite3VdbeJumpHere(p: v, addr: loopTop-1);
138470 if( !isQuick ){
138471 sqlite3VdbeLoadString(p: v, iDest: 2, zStr: "wrong # of entries in index ");
138472 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
138473 if( pPk==pIdx ) continue;
138474 sqlite3VdbeAddOp2(p: v, OP_Count, p1: iIdxCur+j, p2: 3);
138475 addr = sqlite3VdbeAddOp3(p: v, OP_Eq, p1: 8+j, p2: 0, p3: 3); VdbeCoverage(v);
138476 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
138477 sqlite3VdbeLoadString(p: v, iDest: 4, zStr: pIdx->zName);
138478 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 4, p2: 2, p3: 3);
138479 integrityCheckResultRow(v);
138480 sqlite3VdbeJumpHere(p: v, addr);
138481 }
138482 if( pPk ){
138483 sqlite3ReleaseTempRange(pParse, iReg: r2, nReg: pPk->nKeyCol);
138484 }
138485 }
138486 }
138487 }
138488 {
138489 static const int iLn = VDBE_OFFSET_LINENO(2);
138490 static const VdbeOpList endCode[] = {
138491 { OP_AddImm, 1, 0, 0}, /* 0 */
138492 { OP_IfNotZero, 1, 4, 0}, /* 1 */
138493 { OP_String8, 0, 3, 0}, /* 2 */
138494 { OP_ResultRow, 3, 1, 0}, /* 3 */
138495 { OP_Halt, 0, 0, 0}, /* 4 */
138496 { OP_String8, 0, 3, 0}, /* 5 */
138497 { OP_Goto, 0, 3, 0}, /* 6 */
138498 };
138499 VdbeOp *aOp;
138500
138501 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(endCode), aOp: endCode, iLineno: iLn);
138502 if( aOp ){
138503 aOp[0].p2 = 1-mxErr;
138504 aOp[2].p4type = P4_STATIC;
138505 aOp[2].p4.z = "ok";
138506 aOp[5].p4type = P4_STATIC;
138507 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
138508 }
138509 sqlite3VdbeChangeP3(p: v, addr: 0, val: sqlite3VdbeCurrentAddr(p: v)-2);
138510 }
138511 }
138512 break;
138513#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
138514
138515#ifndef SQLITE_OMIT_UTF16
138516 /*
138517 ** PRAGMA encoding
138518 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
138519 **
138520 ** In its first form, this pragma returns the encoding of the main
138521 ** database. If the database is not initialized, it is initialized now.
138522 **
138523 ** The second form of this pragma is a no-op if the main database file
138524 ** has not already been initialized. In this case it sets the default
138525 ** encoding that will be used for the main database file if a new file
138526 ** is created. If an existing main database file is opened, then the
138527 ** default text encoding for the existing database is used.
138528 **
138529 ** In all cases new databases created using the ATTACH command are
138530 ** created to use the same default text encoding as the main database. If
138531 ** the main database has not been initialized and/or created when ATTACH
138532 ** is executed, this is done before the ATTACH operation.
138533 **
138534 ** In the second form this pragma sets the text encoding to be used in
138535 ** new database files created using this database handle. It is only
138536 ** useful if invoked immediately after the main database i
138537 */
138538 case PragTyp_ENCODING: {
138539 static const struct EncName {
138540 char *zName;
138541 u8 enc;
138542 } encnames[] = {
138543 { "UTF8", SQLITE_UTF8 },
138544 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
138545 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
138546 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
138547 { "UTF16le", SQLITE_UTF16LE },
138548 { "UTF16be", SQLITE_UTF16BE },
138549 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
138550 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
138551 { 0, 0 }
138552 };
138553 const struct EncName *pEnc;
138554 if( !zRight ){ /* "PRAGMA encoding" */
138555 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
138556 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
138557 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
138558 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
138559 returnSingleText(v, zValue: encnames[ENC(pParse->db)].zName);
138560 }else{ /* "PRAGMA encoding = XXX" */
138561 /* Only change the value of sqlite.enc if the database handle is not
138562 ** initialized. If the main database exists, the new sqlite.enc value
138563 ** will be overwritten when the schema is next loaded. If it does not
138564 ** already exists, it will be created to use the new encoding value.
138565 */
138566 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
138567 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
138568 if( 0==sqlite3StrICmp(zLeft: zRight, zRight: pEnc->zName) ){
138569 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
138570 SCHEMA_ENC(db) = enc;
138571 sqlite3SetTextEncoding(db, enc);
138572 break;
138573 }
138574 }
138575 if( !pEnc->zName ){
138576 sqlite3ErrorMsg(pParse, zFormat: "unsupported encoding: %s", zRight);
138577 }
138578 }
138579 }
138580 }
138581 break;
138582#endif /* SQLITE_OMIT_UTF16 */
138583
138584#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
138585 /*
138586 ** PRAGMA [schema.]schema_version
138587 ** PRAGMA [schema.]schema_version = <integer>
138588 **
138589 ** PRAGMA [schema.]user_version
138590 ** PRAGMA [schema.]user_version = <integer>
138591 **
138592 ** PRAGMA [schema.]freelist_count
138593 **
138594 ** PRAGMA [schema.]data_version
138595 **
138596 ** PRAGMA [schema.]application_id
138597 ** PRAGMA [schema.]application_id = <integer>
138598 **
138599 ** The pragma's schema_version and user_version are used to set or get
138600 ** the value of the schema-version and user-version, respectively. Both
138601 ** the schema-version and the user-version are 32-bit signed integers
138602 ** stored in the database header.
138603 **
138604 ** The schema-cookie is usually only manipulated internally by SQLite. It
138605 ** is incremented by SQLite whenever the database schema is modified (by
138606 ** creating or dropping a table or index). The schema version is used by
138607 ** SQLite each time a query is executed to ensure that the internal cache
138608 ** of the schema used when compiling the SQL query matches the schema of
138609 ** the database against which the compiled query is actually executed.
138610 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
138611 ** the schema-version is potentially dangerous and may lead to program
138612 ** crashes or database corruption. Use with caution!
138613 **
138614 ** The user-version is not used internally by SQLite. It may be used by
138615 ** applications for any purpose.
138616 */
138617 case PragTyp_HEADER_VALUE: {
138618 int iCookie = pPragma->iArg; /* Which cookie to read or write */
138619 sqlite3VdbeUsesBtree(p: v, i: iDb);
138620 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
138621 /* Write the specified cookie value */
138622 static const VdbeOpList setCookie[] = {
138623 { OP_Transaction, 0, 1, 0}, /* 0 */
138624 { OP_SetCookie, 0, 0, 0}, /* 1 */
138625 };
138626 VdbeOp *aOp;
138627 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
138628 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(setCookie), aOp: setCookie, iLineno: 0);
138629 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
138630 aOp[0].p1 = iDb;
138631 aOp[1].p1 = iDb;
138632 aOp[1].p2 = iCookie;
138633 aOp[1].p3 = sqlite3Atoi(z: zRight);
138634 aOp[1].p5 = 1;
138635 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
138636 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
138637 ** mode. Change the OP_SetCookie opcode into a no-op. */
138638 aOp[1].opcode = OP_Noop;
138639 }
138640 }else{
138641 /* Read the specified cookie value */
138642 static const VdbeOpList readCookie[] = {
138643 { OP_Transaction, 0, 0, 0}, /* 0 */
138644 { OP_ReadCookie, 0, 1, 0}, /* 1 */
138645 { OP_ResultRow, 1, 1, 0}
138646 };
138647 VdbeOp *aOp;
138648 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
138649 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(readCookie),aOp: readCookie,iLineno: 0);
138650 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
138651 aOp[0].p1 = iDb;
138652 aOp[1].p1 = iDb;
138653 aOp[1].p3 = iCookie;
138654 sqlite3VdbeReusable(p: v);
138655 }
138656 }
138657 break;
138658#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
138659
138660#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
138661 /*
138662 ** PRAGMA compile_options
138663 **
138664 ** Return the names of all compile-time options used in this build,
138665 ** one option per row.
138666 */
138667 case PragTyp_COMPILE_OPTIONS: {
138668 int i = 0;
138669 const char *zOpt;
138670 pParse->nMem = 1;
138671 while( (zOpt = sqlite3_compileoption_get(N: i++))!=0 ){
138672 sqlite3VdbeLoadString(p: v, iDest: 1, zStr: zOpt);
138673 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
138674 }
138675 sqlite3VdbeReusable(p: v);
138676 }
138677 break;
138678#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
138679
138680#ifndef SQLITE_OMIT_WAL
138681 /*
138682 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
138683 **
138684 ** Checkpoint the database.
138685 */
138686 case PragTyp_WAL_CHECKPOINT: {
138687 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
138688 int eMode = SQLITE_CHECKPOINT_PASSIVE;
138689 if( zRight ){
138690 if( sqlite3StrICmp(zLeft: zRight, zRight: "full")==0 ){
138691 eMode = SQLITE_CHECKPOINT_FULL;
138692 }else if( sqlite3StrICmp(zLeft: zRight, zRight: "restart")==0 ){
138693 eMode = SQLITE_CHECKPOINT_RESTART;
138694 }else if( sqlite3StrICmp(zLeft: zRight, zRight: "truncate")==0 ){
138695 eMode = SQLITE_CHECKPOINT_TRUNCATE;
138696 }
138697 }
138698 pParse->nMem = 3;
138699 sqlite3VdbeAddOp3(p: v, OP_Checkpoint, p1: iBt, p2: eMode, p3: 1);
138700 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 3);
138701 }
138702 break;
138703
138704 /*
138705 ** PRAGMA wal_autocheckpoint
138706 ** PRAGMA wal_autocheckpoint = N
138707 **
138708 ** Configure a database connection to automatically checkpoint a database
138709 ** after accumulating N frames in the log. Or query for the current value
138710 ** of N.
138711 */
138712 case PragTyp_WAL_AUTOCHECKPOINT: {
138713 if( zRight ){
138714 sqlite3_wal_autocheckpoint(db, N: sqlite3Atoi(z: zRight));
138715 }
138716 returnSingleInt(v,
138717 value: db->xWalCallback==sqlite3WalDefaultHook ?
138718 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
138719 }
138720 break;
138721#endif
138722
138723 /*
138724 ** PRAGMA shrink_memory
138725 **
138726 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
138727 ** connection on which it is invoked to free up as much memory as it
138728 ** can, by calling sqlite3_db_release_memory().
138729 */
138730 case PragTyp_SHRINK_MEMORY: {
138731 sqlite3_db_release_memory(db);
138732 break;
138733 }
138734
138735 /*
138736 ** PRAGMA optimize
138737 ** PRAGMA optimize(MASK)
138738 ** PRAGMA schema.optimize
138739 ** PRAGMA schema.optimize(MASK)
138740 **
138741 ** Attempt to optimize the database. All schemas are optimized in the first
138742 ** two forms, and only the specified schema is optimized in the latter two.
138743 **
138744 ** The details of optimizations performed by this pragma are expected
138745 ** to change and improve over time. Applications should anticipate that
138746 ** this pragma will perform new optimizations in future releases.
138747 **
138748 ** The optional argument is a bitmask of optimizations to perform:
138749 **
138750 ** 0x0001 Debugging mode. Do not actually perform any optimizations
138751 ** but instead return one line of text for each optimization
138752 ** that would have been done. Off by default.
138753 **
138754 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
138755 ** See below for additional information.
138756 **
138757 ** 0x0004 (Not yet implemented) Record usage and performance
138758 ** information from the current session in the
138759 ** database file so that it will be available to "optimize"
138760 ** pragmas run by future database connections.
138761 **
138762 ** 0x0008 (Not yet implemented) Create indexes that might have
138763 ** been helpful to recent queries
138764 **
138765 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
138766 ** of the optimizations listed above except Debug Mode, including new
138767 ** optimizations that have not yet been invented. If new optimizations are
138768 ** ever added that should be off by default, those off-by-default
138769 ** optimizations will have bitmasks of 0x10000 or larger.
138770 **
138771 ** DETERMINATION OF WHEN TO RUN ANALYZE
138772 **
138773 ** In the current implementation, a table is analyzed if only if all of
138774 ** the following are true:
138775 **
138776 ** (1) MASK bit 0x02 is set.
138777 **
138778 ** (2) The query planner used sqlite_stat1-style statistics for one or
138779 ** more indexes of the table at some point during the lifetime of
138780 ** the current connection.
138781 **
138782 ** (3) One or more indexes of the table are currently unanalyzed OR
138783 ** the number of rows in the table has increased by 25 times or more
138784 ** since the last time ANALYZE was run.
138785 **
138786 ** The rules for when tables are analyzed are likely to change in
138787 ** future releases.
138788 */
138789 case PragTyp_OPTIMIZE: {
138790 int iDbLast; /* Loop termination point for the schema loop */
138791 int iTabCur; /* Cursor for a table whose size needs checking */
138792 HashElem *k; /* Loop over tables of a schema */
138793 Schema *pSchema; /* The current schema */
138794 Table *pTab; /* A table in the schema */
138795 Index *pIdx; /* An index of the table */
138796 LogEst szThreshold; /* Size threshold above which reanalysis needed */
138797 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
138798 u32 opMask; /* Mask of operations to perform */
138799
138800 if( zRight ){
138801 opMask = (u32)sqlite3Atoi(z: zRight);
138802 if( (opMask & 0x02)==0 ) break;
138803 }else{
138804 opMask = 0xfffe;
138805 }
138806 iTabCur = pParse->nTab++;
138807 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
138808 if( iDb==1 ) continue;
138809 sqlite3CodeVerifySchema(pParse, iDb);
138810 pSchema = db->aDb[iDb].pSchema;
138811 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
138812 pTab = (Table*)sqliteHashData(k);
138813
138814 /* If table pTab has not been used in a way that would benefit from
138815 ** having analysis statistics during the current session, then skip it.
138816 ** This also has the effect of skipping virtual tables and views */
138817 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
138818
138819 /* Reanalyze if the table is 25 times larger than the last analysis */
138820 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
138821 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138822 if( !pIdx->hasStat1 ){
138823 szThreshold = 0; /* Always analyze if any index lacks statistics */
138824 break;
138825 }
138826 }
138827 if( szThreshold ){
138828 sqlite3OpenTable(pParse, iCur: iTabCur, iDb, pTab, OP_OpenRead);
138829 sqlite3VdbeAddOp3(p: v, OP_IfSmaller, p1: iTabCur,
138830 p2: sqlite3VdbeCurrentAddr(p: v)+2+(opMask&1), p3: szThreshold);
138831 VdbeCoverage(v);
138832 }
138833 zSubSql = sqlite3MPrintf(db, zFormat: "ANALYZE \"%w\".\"%w\"",
138834 db->aDb[iDb].zDbSName, pTab->zName);
138835 if( opMask & 0x01 ){
138836 int r1 = sqlite3GetTempReg(pParse);
138837 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: r1, p3: 0, zP4: zSubSql, P4_DYNAMIC);
138838 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: r1, p2: 1);
138839 }else{
138840 sqlite3VdbeAddOp4(p: v, OP_SqlExec, p1: 0, p2: 0, p3: 0, zP4: zSubSql, P4_DYNAMIC);
138841 }
138842 }
138843 }
138844 sqlite3VdbeAddOp0(p: v, OP_Expire);
138845 break;
138846 }
138847
138848 /*
138849 ** PRAGMA busy_timeout
138850 ** PRAGMA busy_timeout = N
138851 **
138852 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
138853 ** if one is set. If no busy handler or a different busy handler is set
138854 ** then 0 is returned. Setting the busy_timeout to 0 or negative
138855 ** disables the timeout.
138856 */
138857 /*case PragTyp_BUSY_TIMEOUT*/ default: {
138858 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
138859 if( zRight ){
138860 sqlite3_busy_timeout(db, ms: sqlite3Atoi(z: zRight));
138861 }
138862 returnSingleInt(v, value: db->busyTimeout);
138863 break;
138864 }
138865
138866 /*
138867 ** PRAGMA soft_heap_limit
138868 ** PRAGMA soft_heap_limit = N
138869 **
138870 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
138871 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
138872 ** specified and is a non-negative integer.
138873 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
138874 ** returns the same integer that would be returned by the
138875 ** sqlite3_soft_heap_limit64(-1) C-language function.
138876 */
138877 case PragTyp_SOFT_HEAP_LIMIT: {
138878 sqlite3_int64 N;
138879 if( zRight && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK ){
138880 sqlite3_soft_heap_limit64(n: N);
138881 }
138882 returnSingleInt(v, value: sqlite3_soft_heap_limit64(n: -1));
138883 break;
138884 }
138885
138886 /*
138887 ** PRAGMA hard_heap_limit
138888 ** PRAGMA hard_heap_limit = N
138889 **
138890 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
138891 ** limit. The hard heap limit can be activated or lowered by this
138892 ** pragma, but not raised or deactivated. Only the
138893 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
138894 ** the hard heap limit. This allows an application to set a heap limit
138895 ** constraint that cannot be relaxed by an untrusted SQL script.
138896 */
138897 case PragTyp_HARD_HEAP_LIMIT: {
138898 sqlite3_int64 N;
138899 if( zRight && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK ){
138900 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(n: -1);
138901 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(n: N);
138902 }
138903 returnSingleInt(v, value: sqlite3_hard_heap_limit64(n: -1));
138904 break;
138905 }
138906
138907 /*
138908 ** PRAGMA threads
138909 ** PRAGMA threads = N
138910 **
138911 ** Configure the maximum number of worker threads. Return the new
138912 ** maximum, which might be less than requested.
138913 */
138914 case PragTyp_THREADS: {
138915 sqlite3_int64 N;
138916 if( zRight
138917 && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK
138918 && N>=0
138919 ){
138920 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, newVal: (int)(N&0x7fffffff));
138921 }
138922 returnSingleInt(v, value: sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, newVal: -1));
138923 break;
138924 }
138925
138926 /*
138927 ** PRAGMA analysis_limit
138928 ** PRAGMA analysis_limit = N
138929 **
138930 ** Configure the maximum number of rows that ANALYZE will examine
138931 ** in each index that it looks at. Return the new limit.
138932 */
138933 case PragTyp_ANALYSIS_LIMIT: {
138934 sqlite3_int64 N;
138935 if( zRight
138936 && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK /* IMP: R-40975-20399 */
138937 && N>=0
138938 ){
138939 db->nAnalysisLimit = (int)(N&0x7fffffff);
138940 }
138941 returnSingleInt(v, value: db->nAnalysisLimit); /* IMP: R-57594-65522 */
138942 break;
138943 }
138944
138945#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
138946 /*
138947 ** Report the current state of file logs for all databases
138948 */
138949 case PragTyp_LOCK_STATUS: {
138950 static const char *const azLockName[] = {
138951 "unlocked", "shared", "reserved", "pending", "exclusive"
138952 };
138953 int i;
138954 pParse->nMem = 2;
138955 for(i=0; i<db->nDb; i++){
138956 Btree *pBt;
138957 const char *zState = "unknown";
138958 int j;
138959 if( db->aDb[i].zDbSName==0 ) continue;
138960 pBt = db->aDb[i].pBt;
138961 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
138962 zState = "closed";
138963 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
138964 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
138965 zState = azLockName[j];
138966 }
138967 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
138968 }
138969 break;
138970 }
138971#endif
138972
138973#if defined(SQLITE_ENABLE_CEROD)
138974 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
138975 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
138976 sqlite3_activate_cerod(&zRight[6]);
138977 }
138978 }
138979 break;
138980#endif
138981
138982 } /* End of the PRAGMA switch */
138983
138984 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
138985 ** purpose is to execute assert() statements to verify that if the
138986 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
138987 ** to the PRAGMA, the implementation has not added any OP_ResultRow
138988 ** instructions to the VM. */
138989 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
138990 sqlite3VdbeVerifyNoResultRow(v);
138991 }
138992
138993pragma_out:
138994 sqlite3DbFree(db, p: zLeft);
138995 sqlite3DbFree(db, p: zRight);
138996}
138997#ifndef SQLITE_OMIT_VIRTUALTABLE
138998/*****************************************************************************
138999** Implementation of an eponymous virtual table that runs a pragma.
139000**
139001*/
139002typedef struct PragmaVtab PragmaVtab;
139003typedef struct PragmaVtabCursor PragmaVtabCursor;
139004struct PragmaVtab {
139005 sqlite3_vtab base; /* Base class. Must be first */
139006 sqlite3 *db; /* The database connection to which it belongs */
139007 const PragmaName *pName; /* Name of the pragma */
139008 u8 nHidden; /* Number of hidden columns */
139009 u8 iHidden; /* Index of the first hidden column */
139010};
139011struct PragmaVtabCursor {
139012 sqlite3_vtab_cursor base; /* Base class. Must be first */
139013 sqlite3_stmt *pPragma; /* The pragma statement to run */
139014 sqlite_int64 iRowid; /* Current rowid */
139015 char *azArg[2]; /* Value of the argument and schema */
139016};
139017
139018/*
139019** Pragma virtual table module xConnect method.
139020*/
139021static int pragmaVtabConnect(
139022 sqlite3 *db,
139023 void *pAux,
139024 int argc, const char *const*argv,
139025 sqlite3_vtab **ppVtab,
139026 char **pzErr
139027){
139028 const PragmaName *pPragma = (const PragmaName*)pAux;
139029 PragmaVtab *pTab = 0;
139030 int rc;
139031 int i, j;
139032 char cSep = '(';
139033 StrAccum acc;
139034 char zBuf[200];
139035
139036 UNUSED_PARAMETER(argc);
139037 UNUSED_PARAMETER(argv);
139038 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n: sizeof(zBuf), mx: 0);
139039 sqlite3_str_appendall(p: &acc, z: "CREATE TABLE x");
139040 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
139041 sqlite3_str_appendf(p: &acc, zFormat: "%c\"%s\"", cSep, pragCName[j]);
139042 cSep = ',';
139043 }
139044 if( i==0 ){
139045 sqlite3_str_appendf(p: &acc, zFormat: "(\"%s\"", pPragma->zName);
139046 i++;
139047 }
139048 j = 0;
139049 if( pPragma->mPragFlg & PragFlg_Result1 ){
139050 sqlite3_str_appendall(p: &acc, z: ",arg HIDDEN");
139051 j++;
139052 }
139053 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
139054 sqlite3_str_appendall(p: &acc, z: ",schema HIDDEN");
139055 j++;
139056 }
139057 sqlite3_str_append(p: &acc, z: ")", N: 1);
139058 sqlite3StrAccumFinish(p: &acc);
139059 assert( strlen(zBuf) < sizeof(zBuf)-1 );
139060 rc = sqlite3_declare_vtab(db, zSQL: zBuf);
139061 if( rc==SQLITE_OK ){
139062 pTab = (PragmaVtab*)sqlite3_malloc(n: sizeof(PragmaVtab));
139063 if( pTab==0 ){
139064 rc = SQLITE_NOMEM;
139065 }else{
139066 memset(s: pTab, c: 0, n: sizeof(PragmaVtab));
139067 pTab->pName = pPragma;
139068 pTab->db = db;
139069 pTab->iHidden = i;
139070 pTab->nHidden = j;
139071 }
139072 }else{
139073 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
139074 }
139075
139076 *ppVtab = (sqlite3_vtab*)pTab;
139077 return rc;
139078}
139079
139080/*
139081** Pragma virtual table module xDisconnect method.
139082*/
139083static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
139084 PragmaVtab *pTab = (PragmaVtab*)pVtab;
139085 sqlite3_free(p: pTab);
139086 return SQLITE_OK;
139087}
139088
139089/* Figure out the best index to use to search a pragma virtual table.
139090**
139091** There are not really any index choices. But we want to encourage the
139092** query planner to give == constraints on as many hidden parameters as
139093** possible, and especially on the first hidden parameter. So return a
139094** high cost if hidden parameters are unconstrained.
139095*/
139096static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
139097 PragmaVtab *pTab = (PragmaVtab*)tab;
139098 const struct sqlite3_index_constraint *pConstraint;
139099 int i, j;
139100 int seen[2];
139101
139102 pIdxInfo->estimatedCost = (double)1;
139103 if( pTab->nHidden==0 ){ return SQLITE_OK; }
139104 pConstraint = pIdxInfo->aConstraint;
139105 seen[0] = 0;
139106 seen[1] = 0;
139107 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
139108 if( pConstraint->usable==0 ) continue;
139109 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
139110 if( pConstraint->iColumn < pTab->iHidden ) continue;
139111 j = pConstraint->iColumn - pTab->iHidden;
139112 assert( j < 2 );
139113 seen[j] = i+1;
139114 }
139115 if( seen[0]==0 ){
139116 pIdxInfo->estimatedCost = (double)2147483647;
139117 pIdxInfo->estimatedRows = 2147483647;
139118 return SQLITE_OK;
139119 }
139120 j = seen[0]-1;
139121 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
139122 pIdxInfo->aConstraintUsage[j].omit = 1;
139123 if( seen[1]==0 ) return SQLITE_OK;
139124 pIdxInfo->estimatedCost = (double)20;
139125 pIdxInfo->estimatedRows = 20;
139126 j = seen[1]-1;
139127 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
139128 pIdxInfo->aConstraintUsage[j].omit = 1;
139129 return SQLITE_OK;
139130}
139131
139132/* Create a new cursor for the pragma virtual table */
139133static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
139134 PragmaVtabCursor *pCsr;
139135 pCsr = (PragmaVtabCursor*)sqlite3_malloc(n: sizeof(*pCsr));
139136 if( pCsr==0 ) return SQLITE_NOMEM;
139137 memset(s: pCsr, c: 0, n: sizeof(PragmaVtabCursor));
139138 pCsr->base.pVtab = pVtab;
139139 *ppCursor = &pCsr->base;
139140 return SQLITE_OK;
139141}
139142
139143/* Clear all content from pragma virtual table cursor. */
139144static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
139145 int i;
139146 sqlite3_finalize(pStmt: pCsr->pPragma);
139147 pCsr->pPragma = 0;
139148 for(i=0; i<ArraySize(pCsr->azArg); i++){
139149 sqlite3_free(p: pCsr->azArg[i]);
139150 pCsr->azArg[i] = 0;
139151 }
139152}
139153
139154/* Close a pragma virtual table cursor */
139155static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
139156 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
139157 pragmaVtabCursorClear(pCsr);
139158 sqlite3_free(p: pCsr);
139159 return SQLITE_OK;
139160}
139161
139162/* Advance the pragma virtual table cursor to the next row */
139163static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
139164 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
139165 int rc = SQLITE_OK;
139166
139167 /* Increment the xRowid value */
139168 pCsr->iRowid++;
139169 assert( pCsr->pPragma );
139170 if( SQLITE_ROW!=sqlite3_step(pStmt: pCsr->pPragma) ){
139171 rc = sqlite3_finalize(pStmt: pCsr->pPragma);
139172 pCsr->pPragma = 0;
139173 pragmaVtabCursorClear(pCsr);
139174 }
139175 return rc;
139176}
139177
139178/*
139179** Pragma virtual table module xFilter method.
139180*/
139181static int pragmaVtabFilter(
139182 sqlite3_vtab_cursor *pVtabCursor,
139183 int idxNum, const char *idxStr,
139184 int argc, sqlite3_value **argv
139185){
139186 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
139187 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
139188 int rc;
139189 int i, j;
139190 StrAccum acc;
139191 char *zSql;
139192
139193 UNUSED_PARAMETER(idxNum);
139194 UNUSED_PARAMETER(idxStr);
139195 pragmaVtabCursorClear(pCsr);
139196 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
139197 for(i=0; i<argc; i++, j++){
139198 const char *zText = (const char*)sqlite3_value_text(pVal: argv[i]);
139199 assert( j<ArraySize(pCsr->azArg) );
139200 assert( pCsr->azArg[j]==0 );
139201 if( zText ){
139202 pCsr->azArg[j] = sqlite3_mprintf(zFormat: "%s", zText);
139203 if( pCsr->azArg[j]==0 ){
139204 return SQLITE_NOMEM;
139205 }
139206 }
139207 }
139208 sqlite3StrAccumInit(p: &acc, db: 0, zBase: 0, n: 0, mx: pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
139209 sqlite3_str_appendall(p: &acc, z: "PRAGMA ");
139210 if( pCsr->azArg[1] ){
139211 sqlite3_str_appendf(p: &acc, zFormat: "%Q.", pCsr->azArg[1]);
139212 }
139213 sqlite3_str_appendall(p: &acc, z: pTab->pName->zName);
139214 if( pCsr->azArg[0] ){
139215 sqlite3_str_appendf(p: &acc, zFormat: "=%Q", pCsr->azArg[0]);
139216 }
139217 zSql = sqlite3StrAccumFinish(p: &acc);
139218 if( zSql==0 ) return SQLITE_NOMEM;
139219 rc = sqlite3_prepare_v2(db: pTab->db, zSql, nByte: -1, ppStmt: &pCsr->pPragma, pzTail: 0);
139220 sqlite3_free(p: zSql);
139221 if( rc!=SQLITE_OK ){
139222 pTab->base.zErrMsg = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(pTab->db));
139223 return rc;
139224 }
139225 return pragmaVtabNext(pVtabCursor);
139226}
139227
139228/*
139229** Pragma virtual table module xEof method.
139230*/
139231static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
139232 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
139233 return (pCsr->pPragma==0);
139234}
139235
139236/* The xColumn method simply returns the corresponding column from
139237** the PRAGMA.
139238*/
139239static int pragmaVtabColumn(
139240 sqlite3_vtab_cursor *pVtabCursor,
139241 sqlite3_context *ctx,
139242 int i
139243){
139244 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
139245 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
139246 if( i<pTab->iHidden ){
139247 sqlite3_result_value(pCtx: ctx, pValue: sqlite3_column_value(pStmt: pCsr->pPragma, i));
139248 }else{
139249 sqlite3_result_text(pCtx: ctx, z: pCsr->azArg[i-pTab->iHidden],n: -1,SQLITE_TRANSIENT);
139250 }
139251 return SQLITE_OK;
139252}
139253
139254/*
139255** Pragma virtual table module xRowid method.
139256*/
139257static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
139258 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
139259 *p = pCsr->iRowid;
139260 return SQLITE_OK;
139261}
139262
139263/* The pragma virtual table object */
139264static const sqlite3_module pragmaVtabModule = {
139265 0, /* iVersion */
139266 0, /* xCreate - create a table */
139267 pragmaVtabConnect, /* xConnect - connect to an existing table */
139268 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
139269 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
139270 0, /* xDestroy - Drop a table */
139271 pragmaVtabOpen, /* xOpen - open a cursor */
139272 pragmaVtabClose, /* xClose - close a cursor */
139273 pragmaVtabFilter, /* xFilter - configure scan constraints */
139274 pragmaVtabNext, /* xNext - advance a cursor */
139275 pragmaVtabEof, /* xEof */
139276 pragmaVtabColumn, /* xColumn - read data */
139277 pragmaVtabRowid, /* xRowid - read data */
139278 0, /* xUpdate - write data */
139279 0, /* xBegin - begin transaction */
139280 0, /* xSync - sync transaction */
139281 0, /* xCommit - commit transaction */
139282 0, /* xRollback - rollback transaction */
139283 0, /* xFindFunction - function overloading */
139284 0, /* xRename - rename the table */
139285 0, /* xSavepoint */
139286 0, /* xRelease */
139287 0, /* xRollbackTo */
139288 0 /* xShadowName */
139289};
139290
139291/*
139292** Check to see if zTabName is really the name of a pragma. If it is,
139293** then register an eponymous virtual table for that pragma and return
139294** a pointer to the Module object for the new virtual table.
139295*/
139296SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
139297 const PragmaName *pName;
139298 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
139299 pName = pragmaLocate(zName: zName+7);
139300 if( pName==0 ) return 0;
139301 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
139302 assert( sqlite3HashFind(&db->aModule, zName)==0 );
139303 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
139304}
139305
139306#endif /* SQLITE_OMIT_VIRTUALTABLE */
139307
139308#endif /* SQLITE_OMIT_PRAGMA */
139309
139310/************** End of pragma.c **********************************************/
139311/************** Begin file prepare.c *****************************************/
139312/*
139313** 2005 May 25
139314**
139315** The author disclaims copyright to this source code. In place of
139316** a legal notice, here is a blessing:
139317**
139318** May you do good and not evil.
139319** May you find forgiveness for yourself and forgive others.
139320** May you share freely, never taking more than you give.
139321**
139322*************************************************************************
139323** This file contains the implementation of the sqlite3_prepare()
139324** interface, and routines that contribute to loading the database schema
139325** from disk.
139326*/
139327/* #include "sqliteInt.h" */
139328
139329/*
139330** Fill the InitData structure with an error message that indicates
139331** that the database is corrupt.
139332*/
139333static void corruptSchema(
139334 InitData *pData, /* Initialization context */
139335 char **azObj, /* Type and name of object being parsed */
139336 const char *zExtra /* Error information */
139337){
139338 sqlite3 *db = pData->db;
139339 if( db->mallocFailed ){
139340 pData->rc = SQLITE_NOMEM_BKPT;
139341 }else if( pData->pzErrMsg[0]!=0 ){
139342 /* A error message has already been generated. Do not overwrite it */
139343 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
139344 static const char *azAlterType[] = {
139345 "rename",
139346 "drop column",
139347 "add column"
139348 };
139349 *pData->pzErrMsg = sqlite3MPrintf(db,
139350 zFormat: "error in %s %s after %s: %s", azObj[0], azObj[1],
139351 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
139352 zExtra
139353 );
139354 pData->rc = SQLITE_ERROR;
139355 }else if( db->flags & SQLITE_WriteSchema ){
139356 pData->rc = SQLITE_CORRUPT_BKPT;
139357 }else{
139358 char *z;
139359 const char *zObj = azObj[1] ? azObj[1] : "?";
139360 z = sqlite3MPrintf(db, zFormat: "malformed database schema (%s)", zObj);
139361 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, zFormat: "%z - %s", z, zExtra);
139362 *pData->pzErrMsg = z;
139363 pData->rc = SQLITE_CORRUPT_BKPT;
139364 }
139365}
139366
139367/*
139368** Check to see if any sibling index (another index on the same table)
139369** of pIndex has the same root page number, and if it does, return true.
139370** This would indicate a corrupt schema.
139371*/
139372SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
139373 Index *p;
139374 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
139375 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
139376 }
139377 return 0;
139378}
139379
139380/* forward declaration */
139381static int sqlite3Prepare(
139382 sqlite3 *db, /* Database handle. */
139383 const char *zSql, /* UTF-8 encoded SQL statement. */
139384 int nBytes, /* Length of zSql in bytes. */
139385 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
139386 Vdbe *pReprepare, /* VM being reprepared */
139387 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
139388 const char **pzTail /* OUT: End of parsed string */
139389);
139390
139391
139392/*
139393** This is the callback routine for the code that initializes the
139394** database. See sqlite3Init() below for additional information.
139395** This routine is also called from the OP_ParseSchema opcode of the VDBE.
139396**
139397** Each callback contains the following information:
139398**
139399** argv[0] = type of object: "table", "index", "trigger", or "view".
139400** argv[1] = name of thing being created
139401** argv[2] = associated table if an index or trigger
139402** argv[3] = root page number for table or index. 0 for trigger or view.
139403** argv[4] = SQL text for the CREATE statement.
139404**
139405*/
139406SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
139407 InitData *pData = (InitData*)pInit;
139408 sqlite3 *db = pData->db;
139409 int iDb = pData->iDb;
139410
139411 assert( argc==5 );
139412 UNUSED_PARAMETER2(NotUsed, argc);
139413 assert( sqlite3_mutex_held(db->mutex) );
139414 db->mDbFlags |= DBFLAG_EncodingFixed;
139415 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
139416 pData->nInitRow++;
139417 if( db->mallocFailed ){
139418 corruptSchema(pData, azObj: argv, zExtra: 0);
139419 return 1;
139420 }
139421
139422 assert( iDb>=0 && iDb<db->nDb );
139423 if( argv[3]==0 ){
139424 corruptSchema(pData, azObj: argv, zExtra: 0);
139425 }else if( argv[4]
139426 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
139427 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
139428 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
139429 ** But because db->init.busy is set to 1, no VDBE code is generated
139430 ** or executed. All the parser does is build the internal data
139431 ** structures that describe the table, index, or view.
139432 **
139433 ** No other valid SQL statement, other than the variable CREATE statements,
139434 ** can begin with the letters "C" and "R". Thus, it is not possible run
139435 ** any other kind of statement while parsing the schema, even a corrupt
139436 ** schema.
139437 */
139438 int rc;
139439 u8 saved_iDb = db->init.iDb;
139440 sqlite3_stmt *pStmt;
139441 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
139442
139443 assert( db->init.busy );
139444 db->init.iDb = iDb;
139445 if( sqlite3GetUInt32(z: argv[3], pI: &db->init.newTnum)==0
139446 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
139447 ){
139448 if( sqlite3Config.bExtraSchemaChecks ){
139449 corruptSchema(pData, azObj: argv, zExtra: "invalid rootpage");
139450 }
139451 }
139452 db->init.orphanTrigger = 0;
139453 db->init.azInit = (const char**)argv;
139454 pStmt = 0;
139455 TESTONLY(rcp = ) sqlite3Prepare(db, zSql: argv[4], nBytes: -1, prepFlags: 0, pReprepare: 0, ppStmt: &pStmt, pzTail: 0);
139456 rc = db->errCode;
139457 assert( (rc&0xFF)==(rcp&0xFF) );
139458 db->init.iDb = saved_iDb;
139459 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
139460 if( SQLITE_OK!=rc ){
139461 if( db->init.orphanTrigger ){
139462 assert( iDb==1 );
139463 }else{
139464 if( rc > pData->rc ) pData->rc = rc;
139465 if( rc==SQLITE_NOMEM ){
139466 sqlite3OomFault(db);
139467 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
139468 corruptSchema(pData, azObj: argv, zExtra: sqlite3_errmsg(db));
139469 }
139470 }
139471 }
139472 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
139473 sqlite3_finalize(pStmt);
139474 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
139475 corruptSchema(pData, azObj: argv, zExtra: 0);
139476 }else{
139477 /* If the SQL column is blank it means this is an index that
139478 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
139479 ** constraint for a CREATE TABLE. The index should have already
139480 ** been created when we processed the CREATE TABLE. All we have
139481 ** to do here is record the root page number for that index.
139482 */
139483 Index *pIndex;
139484 pIndex = sqlite3FindIndex(db, zName: argv[1], zDb: db->aDb[iDb].zDbSName);
139485 if( pIndex==0 ){
139486 corruptSchema(pData, azObj: argv, zExtra: "orphan index");
139487 }else
139488 if( sqlite3GetUInt32(z: argv[3],pI: &pIndex->tnum)==0
139489 || pIndex->tnum<2
139490 || pIndex->tnum>pData->mxPage
139491 || sqlite3IndexHasDuplicateRootPage(pIndex)
139492 ){
139493 if( sqlite3Config.bExtraSchemaChecks ){
139494 corruptSchema(pData, azObj: argv, zExtra: "invalid rootpage");
139495 }
139496 }
139497 }
139498 return 0;
139499}
139500
139501/*
139502** Attempt to read the database schema and initialize internal
139503** data structures for a single database file. The index of the
139504** database file is given by iDb. iDb==0 is used for the main
139505** database. iDb==1 should never be used. iDb>=2 is used for
139506** auxiliary databases. Return one of the SQLITE_ error codes to
139507** indicate success or failure.
139508*/
139509SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
139510 int rc;
139511 int i;
139512#ifndef SQLITE_OMIT_DEPRECATED
139513 int size;
139514#endif
139515 Db *pDb;
139516 char const *azArg[6];
139517 int meta[5];
139518 InitData initData;
139519 const char *zSchemaTabName;
139520 int openedTransaction = 0;
139521 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
139522
139523 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
139524 assert( iDb>=0 && iDb<db->nDb );
139525 assert( db->aDb[iDb].pSchema );
139526 assert( sqlite3_mutex_held(db->mutex) );
139527 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
139528
139529 db->init.busy = 1;
139530
139531 /* Construct the in-memory representation schema tables (sqlite_schema or
139532 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
139533 ** table name will be inserted automatically by the parser so we can just
139534 ** use the abbreviation "x" here. The parser will also automatically tag
139535 ** the schema table as read-only. */
139536 azArg[0] = "table";
139537 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);
139538 azArg[2] = azArg[1];
139539 azArg[3] = "1";
139540 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
139541 "rootpage int,sql text)";
139542 azArg[5] = 0;
139543 initData.db = db;
139544 initData.iDb = iDb;
139545 initData.rc = SQLITE_OK;
139546 initData.pzErrMsg = pzErrMsg;
139547 initData.mInitFlags = mFlags;
139548 initData.nInitRow = 0;
139549 initData.mxPage = 0;
139550 sqlite3InitCallback(pInit: &initData, argc: 5, argv: (char **)azArg, NotUsed: 0);
139551 db->mDbFlags &= mask;
139552 if( initData.rc ){
139553 rc = initData.rc;
139554 goto error_out;
139555 }
139556
139557 /* Create a cursor to hold the database open
139558 */
139559 pDb = &db->aDb[iDb];
139560 if( pDb->pBt==0 ){
139561 assert( iDb==1 );
139562 DbSetProperty(db, 1, DB_SchemaLoaded);
139563 rc = SQLITE_OK;
139564 goto error_out;
139565 }
139566
139567 /* If there is not already a read-only (or read-write) transaction opened
139568 ** on the b-tree database, open one now. If a transaction is opened, it
139569 ** will be closed before this function returns. */
139570 sqlite3BtreeEnter(p: pDb->pBt);
139571 if( sqlite3BtreeTxnState(p: pDb->pBt)==SQLITE_TXN_NONE ){
139572 rc = sqlite3BtreeBeginTrans(p: pDb->pBt, wrflag: 0, pSchemaVersion: 0);
139573 if( rc!=SQLITE_OK ){
139574 sqlite3SetString(pz: pzErrMsg, db, zNew: sqlite3ErrStr(rc));
139575 goto initone_error_out;
139576 }
139577 openedTransaction = 1;
139578 }
139579
139580 /* Get the database meta information.
139581 **
139582 ** Meta values are as follows:
139583 ** meta[0] Schema cookie. Changes with each schema change.
139584 ** meta[1] File format of schema layer.
139585 ** meta[2] Size of the page cache.
139586 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
139587 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
139588 ** meta[5] User version
139589 ** meta[6] Incremental vacuum mode
139590 ** meta[7] unused
139591 ** meta[8] unused
139592 ** meta[9] unused
139593 **
139594 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
139595 ** the possible values of meta[4].
139596 */
139597 for(i=0; i<ArraySize(meta); i++){
139598 sqlite3BtreeGetMeta(p: pDb->pBt, idx: i+1, pMeta: (u32 *)&meta[i]);
139599 }
139600 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
139601 memset(s: meta, c: 0, n: sizeof(meta));
139602 }
139603 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
139604
139605 /* If opening a non-empty database, check the text encoding. For the
139606 ** main database, set sqlite3.enc to the encoding of the main database.
139607 ** For an attached db, it is an error if the encoding is not the same
139608 ** as sqlite3.enc.
139609 */
139610 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
139611 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
139612 u8 encoding;
139613#ifndef SQLITE_OMIT_UTF16
139614 /* If opening the main database, set ENC(db). */
139615 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
139616 if( encoding==0 ) encoding = SQLITE_UTF8;
139617#else
139618 encoding = SQLITE_UTF8;
139619#endif
139620 if( db->nVdbeActive>0 && encoding!=ENC(db)
139621 && (db->mDbFlags & DBFLAG_Vacuum)==0
139622 ){
139623 rc = SQLITE_LOCKED;
139624 goto initone_error_out;
139625 }else{
139626 sqlite3SetTextEncoding(db, enc: encoding);
139627 }
139628 }else{
139629 /* If opening an attached database, the encoding much match ENC(db) */
139630 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
139631 sqlite3SetString(pz: pzErrMsg, db, zNew: "attached databases must use the same"
139632 " text encoding as main database");
139633 rc = SQLITE_ERROR;
139634 goto initone_error_out;
139635 }
139636 }
139637 }
139638 pDb->pSchema->enc = ENC(db);
139639
139640 if( pDb->pSchema->cache_size==0 ){
139641#ifndef SQLITE_OMIT_DEPRECATED
139642 size = sqlite3AbsInt32(x: meta[BTREE_DEFAULT_CACHE_SIZE-1]);
139643 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
139644 pDb->pSchema->cache_size = size;
139645#else
139646 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
139647#endif
139648 sqlite3BtreeSetCacheSize(p: pDb->pBt, mxPage: pDb->pSchema->cache_size);
139649 }
139650
139651 /*
139652 ** file_format==1 Version 3.0.0.
139653 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
139654 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
139655 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
139656 */
139657 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
139658 if( pDb->pSchema->file_format==0 ){
139659 pDb->pSchema->file_format = 1;
139660 }
139661 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
139662 sqlite3SetString(pz: pzErrMsg, db, zNew: "unsupported file format");
139663 rc = SQLITE_ERROR;
139664 goto initone_error_out;
139665 }
139666
139667 /* Ticket #2804: When we open a database in the newer file format,
139668 ** clear the legacy_file_format pragma flag so that a VACUUM will
139669 ** not downgrade the database and thus invalidate any descending
139670 ** indices that the user might have created.
139671 */
139672 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
139673 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
139674 }
139675
139676 /* Read the schema information out of the schema tables
139677 */
139678 assert( db->init.busy );
139679 initData.mxPage = sqlite3BtreeLastPage(p: pDb->pBt);
139680 {
139681 char *zSql;
139682 zSql = sqlite3MPrintf(db,
139683 zFormat: "SELECT*FROM\"%w\".%s ORDER BY rowid",
139684 db->aDb[iDb].zDbSName, zSchemaTabName);
139685#ifndef SQLITE_OMIT_AUTHORIZATION
139686 {
139687 sqlite3_xauth xAuth;
139688 xAuth = db->xAuth;
139689 db->xAuth = 0;
139690#endif
139691 rc = sqlite3_exec(db, zSql, xCallback: sqlite3InitCallback, pArg: &initData, pzErrMsg: 0);
139692#ifndef SQLITE_OMIT_AUTHORIZATION
139693 db->xAuth = xAuth;
139694 }
139695#endif
139696 if( rc==SQLITE_OK ) rc = initData.rc;
139697 sqlite3DbFree(db, p: zSql);
139698#ifndef SQLITE_OMIT_ANALYZE
139699 if( rc==SQLITE_OK ){
139700 sqlite3AnalysisLoad(db, iDb);
139701 }
139702#endif
139703 }
139704 assert( pDb == &(db->aDb[iDb]) );
139705 if( db->mallocFailed ){
139706 rc = SQLITE_NOMEM_BKPT;
139707 sqlite3ResetAllSchemasOfConnection(db);
139708 pDb = &db->aDb[iDb];
139709 }else
139710 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
139711 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
139712 ** the schema loaded, even if errors (other than OOM) occurred. In
139713 ** this situation the current sqlite3_prepare() operation will fail,
139714 ** but the following one will attempt to compile the supplied statement
139715 ** against whatever subset of the schema was loaded before the error
139716 ** occurred.
139717 **
139718 ** The primary purpose of this is to allow access to the sqlite_schema
139719 ** table even when its contents have been corrupted.
139720 */
139721 DbSetProperty(db, iDb, DB_SchemaLoaded);
139722 rc = SQLITE_OK;
139723 }
139724
139725 /* Jump here for an error that occurs after successfully allocating
139726 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
139727 ** before that point, jump to error_out.
139728 */
139729initone_error_out:
139730 if( openedTransaction ){
139731 sqlite3BtreeCommit(p: pDb->pBt);
139732 }
139733 sqlite3BtreeLeave(p: pDb->pBt);
139734
139735error_out:
139736 if( rc ){
139737 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
139738 sqlite3OomFault(db);
139739 }
139740 sqlite3ResetOneSchema(db, iDb);
139741 }
139742 db->init.busy = 0;
139743 return rc;
139744}
139745
139746/*
139747** Initialize all database files - the main database file, the file
139748** used to store temporary tables, and any additional database files
139749** created using ATTACH statements. Return a success code. If an
139750** error occurs, write an error message into *pzErrMsg.
139751**
139752** After a database is initialized, the DB_SchemaLoaded bit is set
139753** bit is set in the flags field of the Db structure.
139754*/
139755SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
139756 int i, rc;
139757 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
139758
139759 assert( sqlite3_mutex_held(db->mutex) );
139760 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
139761 assert( db->init.busy==0 );
139762 ENC(db) = SCHEMA_ENC(db);
139763 assert( db->nDb>0 );
139764 /* Do the main schema first */
139765 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
139766 rc = sqlite3InitOne(db, iDb: 0, pzErrMsg, mFlags: 0);
139767 if( rc ) return rc;
139768 }
139769 /* All other schemas after the main schema. The "temp" schema must be last */
139770 for(i=db->nDb-1; i>0; i--){
139771 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
139772 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
139773 rc = sqlite3InitOne(db, iDb: i, pzErrMsg, mFlags: 0);
139774 if( rc ) return rc;
139775 }
139776 }
139777 if( commit_internal ){
139778 sqlite3CommitInternalChanges(db);
139779 }
139780 return SQLITE_OK;
139781}
139782
139783/*
139784** This routine is a no-op if the database schema is already initialized.
139785** Otherwise, the schema is loaded. An error code is returned.
139786*/
139787SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
139788 int rc = SQLITE_OK;
139789 sqlite3 *db = pParse->db;
139790 assert( sqlite3_mutex_held(db->mutex) );
139791 if( !db->init.busy ){
139792 rc = sqlite3Init(db, pzErrMsg: &pParse->zErrMsg);
139793 if( rc!=SQLITE_OK ){
139794 pParse->rc = rc;
139795 pParse->nErr++;
139796 }else if( db->noSharedCache ){
139797 db->mDbFlags |= DBFLAG_SchemaKnownOk;
139798 }
139799 }
139800 return rc;
139801}
139802
139803
139804/*
139805** Check schema cookies in all databases. If any cookie is out
139806** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
139807** make no changes to pParse->rc.
139808*/
139809static void schemaIsValid(Parse *pParse){
139810 sqlite3 *db = pParse->db;
139811 int iDb;
139812 int rc;
139813 int cookie;
139814
139815 assert( pParse->checkSchema );
139816 assert( sqlite3_mutex_held(db->mutex) );
139817 for(iDb=0; iDb<db->nDb; iDb++){
139818 int openedTransaction = 0; /* True if a transaction is opened */
139819 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
139820 if( pBt==0 ) continue;
139821
139822 /* If there is not already a read-only (or read-write) transaction opened
139823 ** on the b-tree database, open one now. If a transaction is opened, it
139824 ** will be closed immediately after reading the meta-value. */
139825 if( sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_NONE ){
139826 rc = sqlite3BtreeBeginTrans(p: pBt, wrflag: 0, pSchemaVersion: 0);
139827 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
139828 sqlite3OomFault(db);
139829 pParse->rc = SQLITE_NOMEM;
139830 }
139831 if( rc!=SQLITE_OK ) return;
139832 openedTransaction = 1;
139833 }
139834
139835 /* Read the schema cookie from the database. If it does not match the
139836 ** value stored as part of the in-memory schema representation,
139837 ** set Parse.rc to SQLITE_SCHEMA. */
139838 sqlite3BtreeGetMeta(p: pBt, BTREE_SCHEMA_VERSION, pMeta: (u32 *)&cookie);
139839 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
139840 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
139841 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
139842 sqlite3ResetOneSchema(db, iDb);
139843 }
139844
139845 /* Close the transaction, if one was opened. */
139846 if( openedTransaction ){
139847 sqlite3BtreeCommit(p: pBt);
139848 }
139849 }
139850}
139851
139852/*
139853** Convert a schema pointer into the iDb index that indicates
139854** which database file in db->aDb[] the schema refers to.
139855**
139856** If the same database is attached more than once, the first
139857** attached database is returned.
139858*/
139859SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
139860 int i = -32768;
139861
139862 /* If pSchema is NULL, then return -32768. This happens when code in
139863 ** expr.c is trying to resolve a reference to a transient table (i.e. one
139864 ** created by a sub-select). In this case the return value of this
139865 ** function should never be used.
139866 **
139867 ** We return -32768 instead of the more usual -1 simply because using
139868 ** -32768 as the incorrect index into db->aDb[] is much
139869 ** more likely to cause a segfault than -1 (of course there are assert()
139870 ** statements too, but it never hurts to play the odds) and
139871 ** -32768 will still fit into a 16-bit signed integer.
139872 */
139873 assert( sqlite3_mutex_held(db->mutex) );
139874 if( pSchema ){
139875 for(i=0; 1; i++){
139876 assert( i<db->nDb );
139877 if( db->aDb[i].pSchema==pSchema ){
139878 break;
139879 }
139880 }
139881 assert( i>=0 && i<db->nDb );
139882 }
139883 return i;
139884}
139885
139886/*
139887** Free all memory allocations in the pParse object
139888*/
139889SQLITE_PRIVATE void sqlite3ParseObjectReset(Parse *pParse){
139890 sqlite3 *db = pParse->db;
139891 assert( db!=0 );
139892 assert( db->pParse==pParse );
139893 assert( pParse->nested==0 );
139894#ifndef SQLITE_OMIT_SHARED_CACHE
139895 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, p: pParse->aTableLock);
139896#endif
139897 while( pParse->pCleanup ){
139898 ParseCleanup *pCleanup = pParse->pCleanup;
139899 pParse->pCleanup = pCleanup->pNext;
139900 pCleanup->xCleanup(db, pCleanup->pPtr);
139901 sqlite3DbNNFreeNN(db, p: pCleanup);
139902 }
139903 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, p: pParse->aLabel);
139904 if( pParse->pConstExpr ){
139905 sqlite3ExprListDelete(db, pList: pParse->pConstExpr);
139906 }
139907 assert( db->lookaside.bDisable >= pParse->disableLookaside );
139908 db->lookaside.bDisable -= pParse->disableLookaside;
139909 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
139910 assert( pParse->db->pParse==pParse );
139911 db->pParse = pParse->pOuterParse;
139912 pParse->db = 0;
139913 pParse->disableLookaside = 0;
139914}
139915
139916/*
139917** Add a new cleanup operation to a Parser. The cleanup should happen when
139918** the parser object is destroyed. But, beware: the cleanup might happen
139919** immediately.
139920**
139921** Use this mechanism for uncommon cleanups. There is a higher setup
139922** cost for this mechanism (an extra malloc), so it should not be used
139923** for common cleanups that happen on most calls. But for less
139924** common cleanups, we save a single NULL-pointer comparison in
139925** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.
139926**
139927** If a memory allocation error occurs, then the cleanup happens immediately.
139928** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
139929** pParse->earlyCleanup flag is set in that case. Calling code show verify
139930** that test cases exist for which this happens, to guard against possible
139931** use-after-free errors following an OOM. The preferred way to do this is
139932** to immediately follow the call to this routine with:
139933**
139934** testcase( pParse->earlyCleanup );
139935**
139936** This routine returns a copy of its pPtr input (the third parameter)
139937** except if an early cleanup occurs, in which case it returns NULL. So
139938** another way to check for early cleanup is to check the return value.
139939** Or, stop using the pPtr parameter with this call and use only its
139940** return value thereafter. Something like this:
139941**
139942** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
139943*/
139944SQLITE_PRIVATE void *sqlite3ParserAddCleanup(
139945 Parse *pParse, /* Destroy when this Parser finishes */
139946 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
139947 void *pPtr /* Pointer to object to be cleaned up */
139948){
139949 ParseCleanup *pCleanup = sqlite3DbMallocRaw(db: pParse->db, n: sizeof(*pCleanup));
139950 if( pCleanup ){
139951 pCleanup->pNext = pParse->pCleanup;
139952 pParse->pCleanup = pCleanup;
139953 pCleanup->pPtr = pPtr;
139954 pCleanup->xCleanup = xCleanup;
139955 }else{
139956 xCleanup(pParse->db, pPtr);
139957 pPtr = 0;
139958#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
139959 pParse->earlyCleanup = 1;
139960#endif
139961 }
139962 return pPtr;
139963}
139964
139965/*
139966** Turn bulk memory into a valid Parse object and link that Parse object
139967** into database connection db.
139968**
139969** Call sqlite3ParseObjectReset() to undo this operation.
139970**
139971** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which
139972** is generated by Lemon.
139973*/
139974SQLITE_PRIVATE void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){
139975 memset(PARSE_HDR(pParse), c: 0, PARSE_HDR_SZ);
139976 memset(PARSE_TAIL(pParse), c: 0, PARSE_TAIL_SZ);
139977 assert( db->pParse!=pParse );
139978 pParse->pOuterParse = db->pParse;
139979 db->pParse = pParse;
139980 pParse->db = db;
139981 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, zFormat: "out of memory");
139982}
139983
139984/*
139985** Maximum number of times that we will try again to prepare a statement
139986** that returns SQLITE_ERROR_RETRY.
139987*/
139988#ifndef SQLITE_MAX_PREPARE_RETRY
139989# define SQLITE_MAX_PREPARE_RETRY 25
139990#endif
139991
139992/*
139993** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
139994*/
139995static int sqlite3Prepare(
139996 sqlite3 *db, /* Database handle. */
139997 const char *zSql, /* UTF-8 encoded SQL statement. */
139998 int nBytes, /* Length of zSql in bytes. */
139999 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
140000 Vdbe *pReprepare, /* VM being reprepared */
140001 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140002 const char **pzTail /* OUT: End of parsed string */
140003){
140004 int rc = SQLITE_OK; /* Result code */
140005 int i; /* Loop counter */
140006 Parse sParse; /* Parsing context */
140007
140008 /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
140009 memset(PARSE_HDR(&sParse), c: 0, PARSE_HDR_SZ);
140010 memset(PARSE_TAIL(&sParse), c: 0, PARSE_TAIL_SZ);
140011 sParse.pOuterParse = db->pParse;
140012 db->pParse = &sParse;
140013 sParse.db = db;
140014 if( pReprepare ){
140015 sParse.pReprepare = pReprepare;
140016 sParse.explain = sqlite3_stmt_isexplain(pStmt: (sqlite3_stmt*)pReprepare);
140017 }else{
140018 assert( sParse.pReprepare==0 );
140019 }
140020 assert( ppStmt && *ppStmt==0 );
140021 if( db->mallocFailed ){
140022 sqlite3ErrorMsg(pParse: &sParse, zFormat: "out of memory");
140023 db->errCode = rc = SQLITE_NOMEM;
140024 goto end_prepare;
140025 }
140026 assert( sqlite3_mutex_held(db->mutex) );
140027
140028 /* For a long-term use prepared statement avoid the use of
140029 ** lookaside memory.
140030 */
140031 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
140032 sParse.disableLookaside++;
140033 DisableLookaside;
140034 }
140035 sParse.prepFlags = prepFlags & 0xff;
140036
140037 /* Check to verify that it is possible to get a read lock on all
140038 ** database schemas. The inability to get a read lock indicates that
140039 ** some other database connection is holding a write-lock, which in
140040 ** turn means that the other connection has made uncommitted changes
140041 ** to the schema.
140042 **
140043 ** Were we to proceed and prepare the statement against the uncommitted
140044 ** schema changes and if those schema changes are subsequently rolled
140045 ** back and different changes are made in their place, then when this
140046 ** prepared statement goes to run the schema cookie would fail to detect
140047 ** the schema change. Disaster would follow.
140048 **
140049 ** This thread is currently holding mutexes on all Btrees (because
140050 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
140051 ** is not possible for another thread to start a new schema change
140052 ** while this routine is running. Hence, we do not need to hold
140053 ** locks on the schema, we just need to make sure nobody else is
140054 ** holding them.
140055 **
140056 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
140057 ** but it does *not* override schema lock detection, so this all still
140058 ** works even if READ_UNCOMMITTED is set.
140059 */
140060 if( !db->noSharedCache ){
140061 for(i=0; i<db->nDb; i++) {
140062 Btree *pBt = db->aDb[i].pBt;
140063 if( pBt ){
140064 assert( sqlite3BtreeHoldsMutex(pBt) );
140065 rc = sqlite3BtreeSchemaLocked(p: pBt);
140066 if( rc ){
140067 const char *zDb = db->aDb[i].zDbSName;
140068 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: "database schema is locked: %s", zDb);
140069 testcase( db->flags & SQLITE_ReadUncommit );
140070 goto end_prepare;
140071 }
140072 }
140073 }
140074 }
140075
140076#ifndef SQLITE_OMIT_VIRTUALTABLE
140077 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
140078#endif
140079
140080 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
140081 char *zSqlCopy;
140082 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
140083 testcase( nBytes==mxLen );
140084 testcase( nBytes==mxLen+1 );
140085 if( nBytes>mxLen ){
140086 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, zFormat: "statement too long");
140087 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
140088 goto end_prepare;
140089 }
140090 zSqlCopy = sqlite3DbStrNDup(db, z: zSql, n: nBytes);
140091 if( zSqlCopy ){
140092 sqlite3RunParser(&sParse, zSqlCopy);
140093 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
140094 sqlite3DbFree(db, p: zSqlCopy);
140095 }else{
140096 sParse.zTail = &zSql[nBytes];
140097 }
140098 }else{
140099 sqlite3RunParser(&sParse, zSql);
140100 }
140101 assert( 0==sParse.nQueryLoop );
140102
140103 if( pzTail ){
140104 *pzTail = sParse.zTail;
140105 }
140106
140107 if( db->init.busy==0 ){
140108 sqlite3VdbeSetSql(p: sParse.pVdbe, z: zSql, n: (int)(sParse.zTail-zSql), prepFlags);
140109 }
140110 if( db->mallocFailed ){
140111 sParse.rc = SQLITE_NOMEM_BKPT;
140112 sParse.checkSchema = 0;
140113 }
140114 if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){
140115 if( sParse.checkSchema && db->init.busy==0 ){
140116 schemaIsValid(pParse: &sParse);
140117 }
140118 if( sParse.pVdbe ){
140119 sqlite3VdbeFinalize(p: sParse.pVdbe);
140120 }
140121 assert( 0==(*ppStmt) );
140122 rc = sParse.rc;
140123 if( sParse.zErrMsg ){
140124 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: "%s", sParse.zErrMsg);
140125 sqlite3DbFree(db, p: sParse.zErrMsg);
140126 }else{
140127 sqlite3Error(db, err_code: rc);
140128 }
140129 }else{
140130 assert( sParse.zErrMsg==0 );
140131 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
140132 rc = SQLITE_OK;
140133 sqlite3ErrorClear(db);
140134 }
140135
140136
140137 /* Delete any TriggerPrg structures allocated while parsing this statement. */
140138 while( sParse.pTriggerPrg ){
140139 TriggerPrg *pT = sParse.pTriggerPrg;
140140 sParse.pTriggerPrg = pT->pNext;
140141 sqlite3DbFree(db, p: pT);
140142 }
140143
140144end_prepare:
140145
140146 sqlite3ParseObjectReset(pParse: &sParse);
140147 return rc;
140148}
140149static int sqlite3LockAndPrepare(
140150 sqlite3 *db, /* Database handle. */
140151 const char *zSql, /* UTF-8 encoded SQL statement. */
140152 int nBytes, /* Length of zSql in bytes. */
140153 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
140154 Vdbe *pOld, /* VM being reprepared */
140155 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140156 const char **pzTail /* OUT: End of parsed string */
140157){
140158 int rc;
140159 int cnt = 0;
140160
140161#ifdef SQLITE_ENABLE_API_ARMOR
140162 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
140163#endif
140164 *ppStmt = 0;
140165 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
140166 return SQLITE_MISUSE_BKPT;
140167 }
140168 sqlite3_mutex_enter(p: db->mutex);
140169 sqlite3BtreeEnterAll(db);
140170 do{
140171 /* Make multiple attempts to compile the SQL, until it either succeeds
140172 ** or encounters a permanent error. A schema problem after one schema
140173 ** reset is considered a permanent error. */
140174 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pReprepare: pOld, ppStmt, pzTail);
140175 assert( rc==SQLITE_OK || *ppStmt==0 );
140176 if( rc==SQLITE_OK || db->mallocFailed ) break;
140177 }while( (rc==SQLITE_ERROR_RETRY && (cnt++)<SQLITE_MAX_PREPARE_RETRY)
140178 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,iDb: -1), cnt++)==0) );
140179 sqlite3BtreeLeaveAll(db);
140180 rc = sqlite3ApiExit(db, rc);
140181 assert( (rc&db->errMask)==rc );
140182 db->busyHandler.nBusy = 0;
140183 sqlite3_mutex_leave(p: db->mutex);
140184 return rc;
140185}
140186
140187
140188/*
140189** Rerun the compilation of a statement after a schema change.
140190**
140191** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
140192** if the statement cannot be recompiled because another connection has
140193** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
140194** occurs, return SQLITE_SCHEMA.
140195*/
140196SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
140197 int rc;
140198 sqlite3_stmt *pNew;
140199 const char *zSql;
140200 sqlite3 *db;
140201 u8 prepFlags;
140202
140203 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
140204 zSql = sqlite3_sql(pStmt: (sqlite3_stmt *)p);
140205 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
140206 db = sqlite3VdbeDb(v: p);
140207 assert( sqlite3_mutex_held(db->mutex) );
140208 prepFlags = sqlite3VdbePrepareFlags(v: p);
140209 rc = sqlite3LockAndPrepare(db, zSql, nBytes: -1, prepFlags, pOld: p, ppStmt: &pNew, pzTail: 0);
140210 if( rc ){
140211 if( rc==SQLITE_NOMEM ){
140212 sqlite3OomFault(db);
140213 }
140214 assert( pNew==0 );
140215 return rc;
140216 }else{
140217 assert( pNew!=0 );
140218 }
140219 sqlite3VdbeSwap(pA: (Vdbe*)pNew, pB: p);
140220 sqlite3TransferBindings(pFromStmt: pNew, pToStmt: (sqlite3_stmt*)p);
140221 sqlite3VdbeResetStepResult(p: (Vdbe*)pNew);
140222 sqlite3VdbeFinalize(p: (Vdbe*)pNew);
140223 return SQLITE_OK;
140224}
140225
140226
140227/*
140228** Two versions of the official API. Legacy and new use. In the legacy
140229** version, the original SQL text is not saved in the prepared statement
140230** and so if a schema change occurs, SQLITE_SCHEMA is returned by
140231** sqlite3_step(). In the new version, the original SQL text is retained
140232** and the statement is automatically recompiled if an schema change
140233** occurs.
140234*/
140235SQLITE_API int sqlite3_prepare(
140236 sqlite3 *db, /* Database handle. */
140237 const char *zSql, /* UTF-8 encoded SQL statement. */
140238 int nBytes, /* Length of zSql in bytes. */
140239 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140240 const char **pzTail /* OUT: End of parsed string */
140241){
140242 int rc;
140243 rc = sqlite3LockAndPrepare(db,zSql,nBytes,prepFlags: 0,pOld: 0,ppStmt,pzTail);
140244 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
140245 return rc;
140246}
140247SQLITE_API int sqlite3_prepare_v2(
140248 sqlite3 *db, /* Database handle. */
140249 const char *zSql, /* UTF-8 encoded SQL statement. */
140250 int nBytes, /* Length of zSql in bytes. */
140251 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140252 const char **pzTail /* OUT: End of parsed string */
140253){
140254 int rc;
140255 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
140256 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
140257 ** parameter.
140258 **
140259 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
140260 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,pOld: 0,
140261 ppStmt,pzTail);
140262 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
140263 return rc;
140264}
140265SQLITE_API int sqlite3_prepare_v3(
140266 sqlite3 *db, /* Database handle. */
140267 const char *zSql, /* UTF-8 encoded SQL statement. */
140268 int nBytes, /* Length of zSql in bytes. */
140269 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
140270 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140271 const char **pzTail /* OUT: End of parsed string */
140272){
140273 int rc;
140274 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
140275 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
140276 ** which is a bit array consisting of zero or more of the
140277 ** SQLITE_PREPARE_* flags.
140278 **
140279 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
140280 ** directly above. */
140281 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
140282 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
140283 pOld: 0,ppStmt,pzTail);
140284 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
140285 return rc;
140286}
140287
140288
140289#ifndef SQLITE_OMIT_UTF16
140290/*
140291** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
140292*/
140293static int sqlite3Prepare16(
140294 sqlite3 *db, /* Database handle. */
140295 const void *zSql, /* UTF-16 encoded SQL statement. */
140296 int nBytes, /* Length of zSql in bytes. */
140297 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
140298 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140299 const void **pzTail /* OUT: End of parsed string */
140300){
140301 /* This function currently works by first transforming the UTF-16
140302 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
140303 ** tricky bit is figuring out the pointer to return in *pzTail.
140304 */
140305 char *zSql8;
140306 const char *zTail8 = 0;
140307 int rc = SQLITE_OK;
140308
140309#ifdef SQLITE_ENABLE_API_ARMOR
140310 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
140311#endif
140312 *ppStmt = 0;
140313 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
140314 return SQLITE_MISUSE_BKPT;
140315 }
140316 if( nBytes>=0 ){
140317 int sz;
140318 const char *z = (const char*)zSql;
140319 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
140320 nBytes = sz;
140321 }
140322 sqlite3_mutex_enter(p: db->mutex);
140323 zSql8 = sqlite3Utf16to8(db, z: zSql, nByte: nBytes, SQLITE_UTF16NATIVE);
140324 if( zSql8 ){
140325 rc = sqlite3LockAndPrepare(db, zSql: zSql8, nBytes: -1, prepFlags, pOld: 0, ppStmt, pzTail: &zTail8);
140326 }
140327
140328 if( zTail8 && pzTail ){
140329 /* If sqlite3_prepare returns a tail pointer, we calculate the
140330 ** equivalent pointer into the UTF-16 string by counting the unicode
140331 ** characters between zSql8 and zTail8, and then returning a pointer
140332 ** the same number of characters into the UTF-16 string.
140333 */
140334 int chars_parsed = sqlite3Utf8CharLen(zIn: zSql8, nByte: (int)(zTail8-zSql8));
140335 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zIn: zSql, nChar: chars_parsed);
140336 }
140337 sqlite3DbFree(db, p: zSql8);
140338 rc = sqlite3ApiExit(db, rc);
140339 sqlite3_mutex_leave(p: db->mutex);
140340 return rc;
140341}
140342
140343/*
140344** Two versions of the official API. Legacy and new use. In the legacy
140345** version, the original SQL text is not saved in the prepared statement
140346** and so if a schema change occurs, SQLITE_SCHEMA is returned by
140347** sqlite3_step(). In the new version, the original SQL text is retained
140348** and the statement is automatically recompiled if an schema change
140349** occurs.
140350*/
140351SQLITE_API int sqlite3_prepare16(
140352 sqlite3 *db, /* Database handle. */
140353 const void *zSql, /* UTF-16 encoded SQL statement. */
140354 int nBytes, /* Length of zSql in bytes. */
140355 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140356 const void **pzTail /* OUT: End of parsed string */
140357){
140358 int rc;
140359 rc = sqlite3Prepare16(db,zSql,nBytes,prepFlags: 0,ppStmt,pzTail);
140360 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
140361 return rc;
140362}
140363SQLITE_API int sqlite3_prepare16_v2(
140364 sqlite3 *db, /* Database handle. */
140365 const void *zSql, /* UTF-16 encoded SQL statement. */
140366 int nBytes, /* Length of zSql in bytes. */
140367 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140368 const void **pzTail /* OUT: End of parsed string */
140369){
140370 int rc;
140371 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
140372 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
140373 return rc;
140374}
140375SQLITE_API int sqlite3_prepare16_v3(
140376 sqlite3 *db, /* Database handle. */
140377 const void *zSql, /* UTF-16 encoded SQL statement. */
140378 int nBytes, /* Length of zSql in bytes. */
140379 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
140380 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
140381 const void **pzTail /* OUT: End of parsed string */
140382){
140383 int rc;
140384 rc = sqlite3Prepare16(db,zSql,nBytes,
140385 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
140386 ppStmt,pzTail);
140387 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
140388 return rc;
140389}
140390
140391#endif /* SQLITE_OMIT_UTF16 */
140392
140393/************** End of prepare.c *********************************************/
140394/************** Begin file select.c ******************************************/
140395/*
140396** 2001 September 15
140397**
140398** The author disclaims copyright to this source code. In place of
140399** a legal notice, here is a blessing:
140400**
140401** May you do good and not evil.
140402** May you find forgiveness for yourself and forgive others.
140403** May you share freely, never taking more than you give.
140404**
140405*************************************************************************
140406** This file contains C code routines that are called by the parser
140407** to handle SELECT statements in SQLite.
140408*/
140409/* #include "sqliteInt.h" */
140410
140411/*
140412** An instance of the following object is used to record information about
140413** how to process the DISTINCT keyword, to simplify passing that information
140414** into the selectInnerLoop() routine.
140415*/
140416typedef struct DistinctCtx DistinctCtx;
140417struct DistinctCtx {
140418 u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */
140419 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
140420 int tabTnct; /* Ephemeral table used for DISTINCT processing */
140421 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
140422};
140423
140424/*
140425** An instance of the following object is used to record information about
140426** the ORDER BY (or GROUP BY) clause of query is being coded.
140427**
140428** The aDefer[] array is used by the sorter-references optimization. For
140429** example, assuming there is no index that can be used for the ORDER BY,
140430** for the query:
140431**
140432** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
140433**
140434** it may be more efficient to add just the "a" values to the sorter, and
140435** retrieve the associated "bigblob" values directly from table t1 as the
140436** 10 smallest "a" values are extracted from the sorter.
140437**
140438** When the sorter-reference optimization is used, there is one entry in the
140439** aDefer[] array for each database table that may be read as values are
140440** extracted from the sorter.
140441*/
140442typedef struct SortCtx SortCtx;
140443struct SortCtx {
140444 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
140445 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
140446 int iECursor; /* Cursor number for the sorter */
140447 int regReturn; /* Register holding block-output return address */
140448 int labelBkOut; /* Start label for the block-output subroutine */
140449 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
140450 int labelDone; /* Jump here when done, ex: LIMIT reached */
140451 int labelOBLopt; /* Jump here when sorter is full */
140452 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
140453#ifdef SQLITE_ENABLE_SORTER_REFERENCES
140454 u8 nDefer; /* Number of valid entries in aDefer[] */
140455 struct DeferredCsr {
140456 Table *pTab; /* Table definition */
140457 int iCsr; /* Cursor number for table */
140458 int nKey; /* Number of PK columns for table pTab (>=1) */
140459 } aDefer[4];
140460#endif
140461 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
140462#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
140463 int addrPush; /* First instruction to push data into sorter */
140464 int addrPushEnd; /* Last instruction that pushes data into sorter */
140465#endif
140466};
140467#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
140468
140469/*
140470** Delete all the content of a Select structure. Deallocate the structure
140471** itself depending on the value of bFree
140472**
140473** If bFree==1, call sqlite3DbFree() on the p object.
140474** If bFree==0, Leave the first Select object unfreed
140475*/
140476static void clearSelect(sqlite3 *db, Select *p, int bFree){
140477 assert( db!=0 );
140478 while( p ){
140479 Select *pPrior = p->pPrior;
140480 sqlite3ExprListDelete(db, pList: p->pEList);
140481 sqlite3SrcListDelete(db, pList: p->pSrc);
140482 sqlite3ExprDelete(db, p: p->pWhere);
140483 sqlite3ExprListDelete(db, pList: p->pGroupBy);
140484 sqlite3ExprDelete(db, p: p->pHaving);
140485 sqlite3ExprListDelete(db, pList: p->pOrderBy);
140486 sqlite3ExprDelete(db, p: p->pLimit);
140487 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, pWith: p->pWith);
140488#ifndef SQLITE_OMIT_WINDOWFUNC
140489 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
140490 sqlite3WindowListDelete(db, p: p->pWinDefn);
140491 }
140492 while( p->pWin ){
140493 assert( p->pWin->ppThis==&p->pWin );
140494 sqlite3WindowUnlinkFromSelect(p->pWin);
140495 }
140496#endif
140497 if( bFree ) sqlite3DbNNFreeNN(db, p);
140498 p = pPrior;
140499 bFree = 1;
140500 }
140501}
140502
140503/*
140504** Initialize a SelectDest structure.
140505*/
140506SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
140507 pDest->eDest = (u8)eDest;
140508 pDest->iSDParm = iParm;
140509 pDest->iSDParm2 = 0;
140510 pDest->zAffSdst = 0;
140511 pDest->iSdst = 0;
140512 pDest->nSdst = 0;
140513}
140514
140515
140516/*
140517** Allocate a new Select structure and return a pointer to that
140518** structure.
140519*/
140520SQLITE_PRIVATE Select *sqlite3SelectNew(
140521 Parse *pParse, /* Parsing context */
140522 ExprList *pEList, /* which columns to include in the result */
140523 SrcList *pSrc, /* the FROM clause -- which tables to scan */
140524 Expr *pWhere, /* the WHERE clause */
140525 ExprList *pGroupBy, /* the GROUP BY clause */
140526 Expr *pHaving, /* the HAVING clause */
140527 ExprList *pOrderBy, /* the ORDER BY clause */
140528 u32 selFlags, /* Flag parameters, such as SF_Distinct */
140529 Expr *pLimit /* LIMIT value. NULL means not used */
140530){
140531 Select *pNew, *pAllocated;
140532 Select standin;
140533 pAllocated = pNew = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(*pNew) );
140534 if( pNew==0 ){
140535 assert( pParse->db->mallocFailed );
140536 pNew = &standin;
140537 }
140538 if( pEList==0 ){
140539 pEList = sqlite3ExprListAppend(pParse, pList: 0,
140540 pExpr: sqlite3Expr(db: pParse->db,TK_ASTERISK,zToken: 0));
140541 }
140542 pNew->pEList = pEList;
140543 pNew->op = TK_SELECT;
140544 pNew->selFlags = selFlags;
140545 pNew->iLimit = 0;
140546 pNew->iOffset = 0;
140547 pNew->selId = ++pParse->nSelect;
140548 pNew->addrOpenEphm[0] = -1;
140549 pNew->addrOpenEphm[1] = -1;
140550 pNew->nSelectRow = 0;
140551 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db: pParse->db, n: sizeof(*pSrc));
140552 pNew->pSrc = pSrc;
140553 pNew->pWhere = pWhere;
140554 pNew->pGroupBy = pGroupBy;
140555 pNew->pHaving = pHaving;
140556 pNew->pOrderBy = pOrderBy;
140557 pNew->pPrior = 0;
140558 pNew->pNext = 0;
140559 pNew->pLimit = pLimit;
140560 pNew->pWith = 0;
140561#ifndef SQLITE_OMIT_WINDOWFUNC
140562 pNew->pWin = 0;
140563 pNew->pWinDefn = 0;
140564#endif
140565 if( pParse->db->mallocFailed ) {
140566 clearSelect(db: pParse->db, p: pNew, bFree: pNew!=&standin);
140567 pAllocated = 0;
140568 }else{
140569 assert( pNew->pSrc!=0 || pParse->nErr>0 );
140570 }
140571 return pAllocated;
140572}
140573
140574
140575/*
140576** Delete the given Select structure and all of its substructures.
140577*/
140578SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
140579 if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, bFree: 1);
140580}
140581
140582/*
140583** Return a pointer to the right-most SELECT statement in a compound.
140584*/
140585static Select *findRightmost(Select *p){
140586 while( p->pNext ) p = p->pNext;
140587 return p;
140588}
140589
140590/*
140591** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
140592** type of join. Return an integer constant that expresses that type
140593** in terms of the following bit values:
140594**
140595** JT_INNER
140596** JT_CROSS
140597** JT_OUTER
140598** JT_NATURAL
140599** JT_LEFT
140600** JT_RIGHT
140601**
140602** A full outer join is the combination of JT_LEFT and JT_RIGHT.
140603**
140604** If an illegal or unsupported join type is seen, then still return
140605** a join type, but put an error in the pParse structure.
140606**
140607** These are the valid join types:
140608**
140609**
140610** pA pB pC Return Value
140611** ------- ----- ----- ------------
140612** CROSS - - JT_CROSS
140613** INNER - - JT_INNER
140614** LEFT - - JT_LEFT|JT_OUTER
140615** LEFT OUTER - JT_LEFT|JT_OUTER
140616** RIGHT - - JT_RIGHT|JT_OUTER
140617** RIGHT OUTER - JT_RIGHT|JT_OUTER
140618** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
140619** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
140620** NATURAL INNER - JT_NATURAL|JT_INNER
140621** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
140622** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER
140623** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
140624** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER
140625** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
140626** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT
140627**
140628** To preserve historical compatibly, SQLite also accepts a variety
140629** of other non-standard and in many cases nonsensical join types.
140630** This routine makes as much sense at it can from the nonsense join
140631** type and returns a result. Examples of accepted nonsense join types
140632** include but are not limited to:
140633**
140634** INNER CROSS JOIN -> same as JOIN
140635** NATURAL CROSS JOIN -> same as NATURAL JOIN
140636** OUTER LEFT JOIN -> same as LEFT JOIN
140637** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
140638** LEFT RIGHT JOIN -> same as FULL JOIN
140639** RIGHT OUTER FULL JOIN -> same as FULL JOIN
140640** CROSS CROSS CROSS JOIN -> same as JOIN
140641**
140642** The only restrictions on the join type name are:
140643**
140644** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",
140645** or "FULL".
140646**
140647** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,
140648** or "FULL".
140649**
140650** * If "OUTER" is present then there must also be one of
140651** "LEFT", "RIGHT", or "FULL"
140652*/
140653SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
140654 int jointype = 0;
140655 Token *apAll[3];
140656 Token *p;
140657 /* 0123456789 123456789 123456789 123 */
140658 static const char zKeyText[] = "naturaleftouterightfullinnercross";
140659 static const struct {
140660 u8 i; /* Beginning of keyword text in zKeyText[] */
140661 u8 nChar; /* Length of the keyword in characters */
140662 u8 code; /* Join type mask */
140663 } aKeyword[] = {
140664 /* (0) natural */ { 0, 7, JT_NATURAL },
140665 /* (1) left */ { 6, 4, JT_LEFT|JT_OUTER },
140666 /* (2) outer */ { 10, 5, JT_OUTER },
140667 /* (3) right */ { 14, 5, JT_RIGHT|JT_OUTER },
140668 /* (4) full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
140669 /* (5) inner */ { 23, 5, JT_INNER },
140670 /* (6) cross */ { 28, 5, JT_INNER|JT_CROSS },
140671 };
140672 int i, j;
140673 apAll[0] = pA;
140674 apAll[1] = pB;
140675 apAll[2] = pC;
140676 for(i=0; i<3 && apAll[i]; i++){
140677 p = apAll[i];
140678 for(j=0; j<ArraySize(aKeyword); j++){
140679 if( p->n==aKeyword[j].nChar
140680 && sqlite3StrNICmp(zLeft: (char*)p->z, zRight: &zKeyText[aKeyword[j].i], N: p->n)==0 ){
140681 jointype |= aKeyword[j].code;
140682 break;
140683 }
140684 }
140685 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
140686 if( j>=ArraySize(aKeyword) ){
140687 jointype |= JT_ERROR;
140688 break;
140689 }
140690 }
140691 if(
140692 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
140693 (jointype & JT_ERROR)!=0 ||
140694 (jointype & (JT_OUTER|JT_LEFT|JT_RIGHT))==JT_OUTER
140695 ){
140696 const char *zSp1 = " ";
140697 const char *zSp2 = " ";
140698 if( pB==0 ){ zSp1++; }
140699 if( pC==0 ){ zSp2++; }
140700 sqlite3ErrorMsg(pParse, zFormat: "unknown join type: "
140701 "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
140702 jointype = JT_INNER;
140703 }
140704 return jointype;
140705}
140706
140707/*
140708** Return the index of a column in a table. Return -1 if the column
140709** is not contained in the table.
140710*/
140711SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol){
140712 int i;
140713 u8 h = sqlite3StrIHash(z: zCol);
140714 Column *pCol;
140715 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
140716 if( pCol->hName==h && sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zCol)==0 ) return i;
140717 }
140718 return -1;
140719}
140720
140721/*
140722** Mark a subquery result column as having been used.
140723*/
140724SQLITE_PRIVATE void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){
140725 assert( pItem!=0 );
140726 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
140727 if( pItem->fg.isNestedFrom ){
140728 ExprList *pResults;
140729 assert( pItem->pSelect!=0 );
140730 pResults = pItem->pSelect->pEList;
140731 assert( pResults!=0 );
140732 assert( iCol>=0 && iCol<pResults->nExpr );
140733 pResults->a[iCol].fg.bUsed = 1;
140734 }
140735}
140736
140737/*
140738** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a
140739** table that has a column named zCol. The search is left-to-right.
140740** The first match found is returned.
140741**
140742** When found, set *piTab and *piCol to the table index and column index
140743** of the matching column and return TRUE.
140744**
140745** If not found, return FALSE.
140746*/
140747static int tableAndColumnIndex(
140748 SrcList *pSrc, /* Array of tables to search */
140749 int iStart, /* First member of pSrc->a[] to check */
140750 int iEnd, /* Last member of pSrc->a[] to check */
140751 const char *zCol, /* Name of the column we are looking for */
140752 int *piTab, /* Write index of pSrc->a[] here */
140753 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
140754 int bIgnoreHidden /* Ignore hidden columns */
140755){
140756 int i; /* For looping over tables in pSrc */
140757 int iCol; /* Index of column matching zCol */
140758
140759 assert( iEnd<pSrc->nSrc );
140760 assert( iStart>=0 );
140761 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
140762
140763 for(i=iStart; i<=iEnd; i++){
140764 iCol = sqlite3ColumnIndex(pTab: pSrc->a[i].pTab, zCol);
140765 if( iCol>=0
140766 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
140767 ){
140768 if( piTab ){
140769 sqlite3SrcItemColumnUsed(pItem: &pSrc->a[i], iCol);
140770 *piTab = i;
140771 *piCol = iCol;
140772 }
140773 return 1;
140774 }
140775 }
140776 return 0;
140777}
140778
140779/*
140780** Set the EP_OuterON property on all terms of the given expression.
140781** And set the Expr.w.iJoin to iTable for every term in the
140782** expression.
140783**
140784** The EP_OuterON property is used on terms of an expression to tell
140785** the OUTER JOIN processing logic that this term is part of the
140786** join restriction specified in the ON or USING clause and not a part
140787** of the more general WHERE clause. These terms are moved over to the
140788** WHERE clause during join processing but we need to remember that they
140789** originated in the ON or USING clause.
140790**
140791** The Expr.w.iJoin tells the WHERE clause processing that the
140792** expression depends on table w.iJoin even if that table is not
140793** explicitly mentioned in the expression. That information is needed
140794** for cases like this:
140795**
140796** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
140797**
140798** The where clause needs to defer the handling of the t1.x=5
140799** term until after the t2 loop of the join. In that way, a
140800** NULL t2 row will be inserted whenever t1.x!=5. If we do not
140801** defer the handling of t1.x=5, it will be processed immediately
140802** after the t1 loop and rows with t1.x!=5 will never appear in
140803** the output, which is incorrect.
140804*/
140805SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){
140806 assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON );
140807 while( p ){
140808 ExprSetProperty(p, joinFlag);
140809 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
140810 ExprSetVVAProperty(p, EP_NoReduce);
140811 p->w.iJoin = iTable;
140812 if( p->op==TK_FUNCTION ){
140813 assert( ExprUseXList(p) );
140814 if( p->x.pList ){
140815 int i;
140816 for(i=0; i<p->x.pList->nExpr; i++){
140817 sqlite3SetJoinExpr(p: p->x.pList->a[i].pExpr, iTable, joinFlag);
140818 }
140819 }
140820 }
140821 sqlite3SetJoinExpr(p: p->pLeft, iTable, joinFlag);
140822 p = p->pRight;
140823 }
140824}
140825
140826/* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN
140827** is simplified into an ordinary JOIN, and when an ON expression is
140828** "pushed down" into the WHERE clause of a subquery.
140829**
140830** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into
140831** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then
140832** just clear every EP_OuterON and EP_InnerON mark from the expression tree.
140833**
140834** If nullable is true, that means that Expr p might evaluate to NULL even
140835** if it is a reference to a NOT NULL column. This can happen, for example,
140836** if the table that p references is on the left side of a RIGHT JOIN.
140837** If nullable is true, then take care to not remove the EP_CanBeNull bit.
140838** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c
140839*/
140840static void unsetJoinExpr(Expr *p, int iTable, int nullable){
140841 while( p ){
140842 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
140843 ExprClearProperty(p, EP_OuterON|EP_InnerON);
140844 if( iTable>=0 ) ExprSetProperty(p, EP_InnerON);
140845 }
140846 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
140847 ExprClearProperty(p, EP_CanBeNull);
140848 }
140849 if( p->op==TK_FUNCTION ){
140850 assert( ExprUseXList(p) );
140851 if( p->x.pList ){
140852 int i;
140853 for(i=0; i<p->x.pList->nExpr; i++){
140854 unsetJoinExpr(p: p->x.pList->a[i].pExpr, iTable, nullable);
140855 }
140856 }
140857 }
140858 unsetJoinExpr(p: p->pLeft, iTable, nullable);
140859 p = p->pRight;
140860 }
140861}
140862
140863/*
140864** This routine processes the join information for a SELECT statement.
140865**
140866** * A NATURAL join is converted into a USING join. After that, we
140867** do not need to be concerned with NATURAL joins and we only have
140868** think about USING joins.
140869**
140870** * ON and USING clauses result in extra terms being added to the
140871** WHERE clause to enforce the specified constraints. The extra
140872** WHERE clause terms will be tagged with EP_OuterON or
140873** EP_InnerON so that we know that they originated in ON/USING.
140874**
140875** The terms of a FROM clause are contained in the Select.pSrc structure.
140876** The left most table is the first entry in Select.pSrc. The right-most
140877** table is the last entry. The join operator is held in the entry to
140878** the right. Thus entry 1 contains the join operator for the join between
140879** entries 0 and 1. Any ON or USING clauses associated with the join are
140880** also attached to the right entry.
140881**
140882** This routine returns the number of errors encountered.
140883*/
140884static int sqlite3ProcessJoin(Parse *pParse, Select *p){
140885 SrcList *pSrc; /* All tables in the FROM clause */
140886 int i, j; /* Loop counters */
140887 SrcItem *pLeft; /* Left table being joined */
140888 SrcItem *pRight; /* Right table being joined */
140889
140890 pSrc = p->pSrc;
140891 pLeft = &pSrc->a[0];
140892 pRight = &pLeft[1];
140893 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
140894 Table *pRightTab = pRight->pTab;
140895 u32 joinType;
140896
140897 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
140898 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
140899
140900 /* If this is a NATURAL join, synthesize an appropriate USING clause
140901 ** to specify which columns should be joined.
140902 */
140903 if( pRight->fg.jointype & JT_NATURAL ){
140904 IdList *pUsing = 0;
140905 if( pRight->fg.isUsing || pRight->u3.pOn ){
140906 sqlite3ErrorMsg(pParse, zFormat: "a NATURAL join may not have "
140907 "an ON or USING clause", 0);
140908 return 1;
140909 }
140910 for(j=0; j<pRightTab->nCol; j++){
140911 char *zName; /* Name of column in the right table */
140912
140913 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
140914 zName = pRightTab->aCol[j].zCnName;
140915 if( tableAndColumnIndex(pSrc, iStart: 0, iEnd: i, zCol: zName, piTab: 0, piCol: 0, bIgnoreHidden: 1) ){
140916 pUsing = sqlite3IdListAppend(pParse, pList: pUsing, pToken: 0);
140917 if( pUsing ){
140918 assert( pUsing->nId>0 );
140919 assert( pUsing->a[pUsing->nId-1].zName==0 );
140920 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(db: pParse->db, z: zName);
140921 }
140922 }
140923 }
140924 if( pUsing ){
140925 pRight->fg.isUsing = 1;
140926 pRight->fg.isSynthUsing = 1;
140927 pRight->u3.pUsing = pUsing;
140928 }
140929 if( pParse->nErr ) return 1;
140930 }
140931
140932 /* Create extra terms on the WHERE clause for each column named
140933 ** in the USING clause. Example: If the two tables to be joined are
140934 ** A and B and the USING clause names X, Y, and Z, then add this
140935 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
140936 ** Report an error if any column mentioned in the USING clause is
140937 ** not contained in both tables to be joined.
140938 */
140939 if( pRight->fg.isUsing ){
140940 IdList *pList = pRight->u3.pUsing;
140941 sqlite3 *db = pParse->db;
140942 assert( pList!=0 );
140943 for(j=0; j<pList->nId; j++){
140944 char *zName; /* Name of the term in the USING clause */
140945 int iLeft; /* Table on the left with matching column name */
140946 int iLeftCol; /* Column number of matching column on the left */
140947 int iRightCol; /* Column number of matching column on the right */
140948 Expr *pE1; /* Reference to the column on the LEFT of the join */
140949 Expr *pE2; /* Reference to the column on the RIGHT of the join */
140950 Expr *pEq; /* Equality constraint. pE1 == pE2 */
140951
140952 zName = pList->a[j].zName;
140953 iRightCol = sqlite3ColumnIndex(pTab: pRightTab, zCol: zName);
140954 if( iRightCol<0
140955 || tableAndColumnIndex(pSrc, iStart: 0, iEnd: i, zCol: zName, piTab: &iLeft, piCol: &iLeftCol,
140956 bIgnoreHidden: pRight->fg.isSynthUsing)==0
140957 ){
140958 sqlite3ErrorMsg(pParse, zFormat: "cannot join using column %s - column "
140959 "not present in both tables", zName);
140960 return 1;
140961 }
140962 pE1 = sqlite3CreateColumnExpr(db, pSrc, iSrc: iLeft, iCol: iLeftCol);
140963 sqlite3SrcItemColumnUsed(pItem: &pSrc->a[iLeft], iCol: iLeftCol);
140964 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
140965 /* This branch runs if the query contains one or more RIGHT or FULL
140966 ** JOINs. If only a single table on the left side of this join
140967 ** contains the zName column, then this branch is a no-op.
140968 ** But if there are two or more tables on the left side
140969 ** of the join, construct a coalesce() function that gathers all
140970 ** such tables. Raise an error if more than one of those references
140971 ** to zName is not also within a prior USING clause.
140972 **
140973 ** We really ought to raise an error if there are two or more
140974 ** non-USING references to zName on the left of an INNER or LEFT
140975 ** JOIN. But older versions of SQLite do not do that, so we avoid
140976 ** adding a new error so as to not break legacy applications.
140977 */
140978 ExprList *pFuncArgs = 0; /* Arguments to the coalesce() */
140979 static const Token tkCoalesce = { "coalesce", 8 };
140980 while( tableAndColumnIndex(pSrc, iStart: iLeft+1, iEnd: i, zCol: zName, piTab: &iLeft, piCol: &iLeftCol,
140981 bIgnoreHidden: pRight->fg.isSynthUsing)!=0 ){
140982 if( pSrc->a[iLeft].fg.isUsing==0
140983 || sqlite3IdListIndex(pList: pSrc->a[iLeft].u3.pUsing, zName)<0
140984 ){
140985 sqlite3ErrorMsg(pParse, zFormat: "ambiguous reference to %s in USING()",
140986 zName);
140987 break;
140988 }
140989 pFuncArgs = sqlite3ExprListAppend(pParse, pList: pFuncArgs, pExpr: pE1);
140990 pE1 = sqlite3CreateColumnExpr(db, pSrc, iSrc: iLeft, iCol: iLeftCol);
140991 sqlite3SrcItemColumnUsed(pItem: &pSrc->a[iLeft], iCol: iLeftCol);
140992 }
140993 if( pFuncArgs ){
140994 pFuncArgs = sqlite3ExprListAppend(pParse, pList: pFuncArgs, pExpr: pE1);
140995 pE1 = sqlite3ExprFunction(pParse, pList: pFuncArgs, pToken: &tkCoalesce, eDistinct: 0);
140996 }
140997 }
140998 pE2 = sqlite3CreateColumnExpr(db, pSrc, iSrc: i+1, iCol: iRightCol);
140999 sqlite3SrcItemColumnUsed(pItem: pRight, iCol: iRightCol);
141000 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft: pE1, pRight: pE2);
141001 assert( pE2!=0 || pEq==0 );
141002 if( pEq ){
141003 ExprSetProperty(pEq, joinType);
141004 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
141005 ExprSetVVAProperty(pEq, EP_NoReduce);
141006 pEq->w.iJoin = pE2->iTable;
141007 }
141008 p->pWhere = sqlite3ExprAnd(pParse, pLeft: p->pWhere, pRight: pEq);
141009 }
141010 }
141011
141012 /* Add the ON clause to the end of the WHERE clause, connected by
141013 ** an AND operator.
141014 */
141015 else if( pRight->u3.pOn ){
141016 sqlite3SetJoinExpr(p: pRight->u3.pOn, iTable: pRight->iCursor, joinFlag: joinType);
141017 p->pWhere = sqlite3ExprAnd(pParse, pLeft: p->pWhere, pRight: pRight->u3.pOn);
141018 pRight->u3.pOn = 0;
141019 pRight->fg.isOn = 1;
141020 }
141021 }
141022 return 0;
141023}
141024
141025/*
141026** An instance of this object holds information (beyond pParse and pSelect)
141027** needed to load the next result row that is to be added to the sorter.
141028*/
141029typedef struct RowLoadInfo RowLoadInfo;
141030struct RowLoadInfo {
141031 int regResult; /* Store results in array of registers here */
141032 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
141033#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141034 ExprList *pExtra; /* Extra columns needed by sorter refs */
141035 int regExtraResult; /* Where to load the extra columns */
141036#endif
141037};
141038
141039/*
141040** This routine does the work of loading query data into an array of
141041** registers so that it can be added to the sorter.
141042*/
141043static void innerLoopLoadRow(
141044 Parse *pParse, /* Statement under construction */
141045 Select *pSelect, /* The query being coded */
141046 RowLoadInfo *pInfo /* Info needed to complete the row load */
141047){
141048 sqlite3ExprCodeExprList(pParse, pList: pSelect->pEList, target: pInfo->regResult,
141049 srcReg: 0, flags: pInfo->ecelFlags);
141050#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141051 if( pInfo->pExtra ){
141052 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
141053 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
141054 }
141055#endif
141056}
141057
141058/*
141059** Code the OP_MakeRecord instruction that generates the entry to be
141060** added into the sorter.
141061**
141062** Return the register in which the result is stored.
141063*/
141064static int makeSorterRecord(
141065 Parse *pParse,
141066 SortCtx *pSort,
141067 Select *pSelect,
141068 int regBase,
141069 int nBase
141070){
141071 int nOBSat = pSort->nOBSat;
141072 Vdbe *v = pParse->pVdbe;
141073 int regOut = ++pParse->nMem;
141074 if( pSort->pDeferredRowLoad ){
141075 innerLoopLoadRow(pParse, pSelect, pInfo: pSort->pDeferredRowLoad);
141076 }
141077 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regBase+nOBSat, p2: nBase-nOBSat, p3: regOut);
141078 return regOut;
141079}
141080
141081/*
141082** Generate code that will push the record in registers regData
141083** through regData+nData-1 onto the sorter.
141084*/
141085static void pushOntoSorter(
141086 Parse *pParse, /* Parser context */
141087 SortCtx *pSort, /* Information about the ORDER BY clause */
141088 Select *pSelect, /* The whole SELECT statement */
141089 int regData, /* First register holding data to be sorted */
141090 int regOrigData, /* First register holding data before packing */
141091 int nData, /* Number of elements in the regData data array */
141092 int nPrefixReg /* No. of reg prior to regData available for use */
141093){
141094 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
141095 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
141096 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
141097 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
141098 int regBase; /* Regs for sorter record */
141099 int regRecord = 0; /* Assembled sorter record */
141100 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
141101 int op; /* Opcode to add sorter record to sorter */
141102 int iLimit; /* LIMIT counter */
141103 int iSkip = 0; /* End of the sorter insert loop */
141104
141105 assert( bSeq==0 || bSeq==1 );
141106
141107 /* Three cases:
141108 ** (1) The data to be sorted has already been packed into a Record
141109 ** by a prior OP_MakeRecord. In this case nData==1 and regData
141110 ** will be completely unrelated to regOrigData.
141111 ** (2) All output columns are included in the sort record. In that
141112 ** case regData==regOrigData.
141113 ** (3) Some output columns are omitted from the sort record due to
141114 ** the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the
141115 ** SQLITE_ECEL_OMITREF optimization, or due to the
141116 ** SortCtx.pDeferredRowLoad optimization. In any of these cases
141117 ** regOrigData is 0 to prevent this routine from trying to copy
141118 ** values that might not yet exist.
141119 */
141120 assert( nData==1 || regData==regOrigData || regOrigData==0 );
141121
141122#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
141123 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
141124#endif
141125
141126 if( nPrefixReg ){
141127 assert( nPrefixReg==nExpr+bSeq );
141128 regBase = regData - nPrefixReg;
141129 }else{
141130 regBase = pParse->nMem + 1;
141131 pParse->nMem += nBase;
141132 }
141133 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
141134 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
141135 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
141136 sqlite3ExprCodeExprList(pParse, pList: pSort->pOrderBy, target: regBase, srcReg: regOrigData,
141137 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
141138 if( bSeq ){
141139 sqlite3VdbeAddOp2(p: v, OP_Sequence, p1: pSort->iECursor, p2: regBase+nExpr);
141140 }
141141 if( nPrefixReg==0 && nData>0 ){
141142 sqlite3ExprCodeMove(pParse, iFrom: regData, iTo: regBase+nExpr+bSeq, nReg: nData);
141143 }
141144 if( nOBSat>0 ){
141145 int regPrevKey; /* The first nOBSat columns of the previous row */
141146 int addrFirst; /* Address of the OP_IfNot opcode */
141147 int addrJmp; /* Address of the OP_Jump opcode */
141148 VdbeOp *pOp; /* Opcode that opens the sorter */
141149 int nKey; /* Number of sorting key columns, including OP_Sequence */
141150 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
141151
141152 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
141153 regPrevKey = pParse->nMem+1;
141154 pParse->nMem += pSort->nOBSat;
141155 nKey = nExpr - pSort->nOBSat + bSeq;
141156 if( bSeq ){
141157 addrFirst = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: regBase+nExpr);
141158 }else{
141159 addrFirst = sqlite3VdbeAddOp1(p: v, OP_SequenceTest, p1: pSort->iECursor);
141160 }
141161 VdbeCoverage(v);
141162 sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regPrevKey, p2: regBase, p3: pSort->nOBSat);
141163 pOp = sqlite3VdbeGetOp(p: v, addr: pSort->addrSortIndex);
141164 if( pParse->db->mallocFailed ) return;
141165 pOp->p2 = nKey + nData;
141166 pKI = pOp->p4.pKeyInfo;
141167 memset(s: pKI->aSortFlags, c: 0, n: pKI->nKeyField); /* Makes OP_Jump testable */
141168 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char*)pKI, P4_KEYINFO);
141169 testcase( pKI->nAllField > pKI->nKeyField+2 );
141170 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
141171 pKI->nAllField-pKI->nKeyField-1);
141172 pOp = 0; /* Ensure pOp not used after sqlite3VdbeAddOp3() */
141173 addrJmp = sqlite3VdbeCurrentAddr(p: v);
141174 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addrJmp+1, p2: 0, p3: addrJmp+1); VdbeCoverage(v);
141175 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
141176 pSort->regReturn = ++pParse->nMem;
141177 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pSort->regReturn, p2: pSort->labelBkOut);
141178 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: pSort->iECursor);
141179 if( iLimit ){
141180 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: iLimit, p2: pSort->labelDone);
141181 VdbeCoverage(v);
141182 }
141183 sqlite3VdbeJumpHere(p: v, addr: addrFirst);
141184 sqlite3ExprCodeMove(pParse, iFrom: regBase, iTo: regPrevKey, nReg: pSort->nOBSat);
141185 sqlite3VdbeJumpHere(p: v, addr: addrJmp);
141186 }
141187 if( iLimit ){
141188 /* At this point the values for the new sorter entry are stored
141189 ** in an array of registers. They need to be composed into a record
141190 ** and inserted into the sorter if either (a) there are currently
141191 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
141192 ** the largest record currently in the sorter. If (b) is true and there
141193 ** are already LIMIT+OFFSET items in the sorter, delete the largest
141194 ** entry before inserting the new one. This way there are never more
141195 ** than LIMIT+OFFSET items in the sorter.
141196 **
141197 ** If the new record does not need to be inserted into the sorter,
141198 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
141199 ** value is not zero, then it is a label of where to jump. Otherwise,
141200 ** just bypass the row insert logic. See the header comment on the
141201 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
141202 */
141203 int iCsr = pSort->iECursor;
141204 sqlite3VdbeAddOp2(p: v, OP_IfNotZero, p1: iLimit, p2: sqlite3VdbeCurrentAddr(p: v)+4);
141205 VdbeCoverage(v);
141206 sqlite3VdbeAddOp2(p: v, OP_Last, p1: iCsr, p2: 0);
141207 iSkip = sqlite3VdbeAddOp4Int(p: v, OP_IdxLE,
141208 p1: iCsr, p2: 0, p3: regBase+nOBSat, p4: nExpr-nOBSat);
141209 VdbeCoverage(v);
141210 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: iCsr);
141211 }
141212 if( regRecord==0 ){
141213 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
141214 }
141215 if( pSort->sortFlags & SORTFLAG_UseSorter ){
141216 op = OP_SorterInsert;
141217 }else{
141218 op = OP_IdxInsert;
141219 }
141220 sqlite3VdbeAddOp4Int(p: v, op, p1: pSort->iECursor, p2: regRecord,
141221 p3: regBase+nOBSat, p4: nBase-nOBSat);
141222 if( iSkip ){
141223 sqlite3VdbeChangeP2(p: v, addr: iSkip,
141224 val: pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(p: v));
141225 }
141226#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
141227 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
141228#endif
141229}
141230
141231/*
141232** Add code to implement the OFFSET
141233*/
141234static void codeOffset(
141235 Vdbe *v, /* Generate code into this VM */
141236 int iOffset, /* Register holding the offset counter */
141237 int iContinue /* Jump here to skip the current record */
141238){
141239 if( iOffset>0 ){
141240 sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: iOffset, p2: iContinue, p3: 1); VdbeCoverage(v);
141241 VdbeComment((v, "OFFSET"));
141242 }
141243}
141244
141245/*
141246** Add code that will check to make sure the array of registers starting at
141247** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and
141248** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies
141249** are available. Which is used depends on the value of parameter eTnctType,
141250** as follows:
141251**
141252** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:
141253** Build an ephemeral table that contains all entries seen before and
141254** skip entries which have been seen before.
141255**
141256** Parameter iTab is the cursor number of an ephemeral table that must
141257** be opened before the VM code generated by this routine is executed.
141258** The ephemeral cursor table is queried for a record identical to the
141259** record formed by the current array of registers. If one is found,
141260** jump to VM address addrRepeat. Otherwise, insert a new record into
141261** the ephemeral cursor and proceed.
141262**
141263** The returned value in this case is a copy of parameter iTab.
141264**
141265** WHERE_DISTINCT_ORDERED:
141266** In this case rows are being delivered sorted order. The ephemeral
141267** table is not required. Instead, the current set of values
141268** is compared against previous row. If they match, the new row
141269** is not distinct and control jumps to VM address addrRepeat. Otherwise,
141270** the VM program proceeds with processing the new row.
141271**
141272** The returned value in this case is the register number of the first
141273** in an array of registers used to store the previous result row so that
141274** it can be compared to the next. The caller must ensure that this
141275** register is initialized to NULL. (The fixDistinctOpenEph() routine
141276** will take care of this initialization.)
141277**
141278** WHERE_DISTINCT_UNIQUE:
141279** In this case it has already been determined that the rows are distinct.
141280** No special action is required. The return value is zero.
141281**
141282** Parameter pEList is the list of expressions used to generated the
141283** contents of each row. It is used by this routine to determine (a)
141284** how many elements there are in the array of registers and (b) the
141285** collation sequences that should be used for the comparisons if
141286** eTnctType is WHERE_DISTINCT_ORDERED.
141287*/
141288static int codeDistinct(
141289 Parse *pParse, /* Parsing and code generating context */
141290 int eTnctType, /* WHERE_DISTINCT_* value */
141291 int iTab, /* A sorting index used to test for distinctness */
141292 int addrRepeat, /* Jump to here if not distinct */
141293 ExprList *pEList, /* Expression for each element */
141294 int regElem /* First element */
141295){
141296 int iRet = 0;
141297 int nResultCol = pEList->nExpr;
141298 Vdbe *v = pParse->pVdbe;
141299
141300 switch( eTnctType ){
141301 case WHERE_DISTINCT_ORDERED: {
141302 int i;
141303 int iJump; /* Jump destination */
141304 int regPrev; /* Previous row content */
141305
141306 /* Allocate space for the previous row */
141307 iRet = regPrev = pParse->nMem+1;
141308 pParse->nMem += nResultCol;
141309
141310 iJump = sqlite3VdbeCurrentAddr(p: v) + nResultCol;
141311 for(i=0; i<nResultCol; i++){
141312 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr: pEList->a[i].pExpr);
141313 if( i<nResultCol-1 ){
141314 sqlite3VdbeAddOp3(p: v, OP_Ne, p1: regElem+i, p2: iJump, p3: regPrev+i);
141315 VdbeCoverage(v);
141316 }else{
141317 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regElem+i, p2: addrRepeat, p3: regPrev+i);
141318 VdbeCoverage(v);
141319 }
141320 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (const char *)pColl, P4_COLLSEQ);
141321 sqlite3VdbeChangeP5(p: v, SQLITE_NULLEQ);
141322 }
141323 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
141324 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regElem, p2: regPrev, p3: nResultCol-1);
141325 break;
141326 }
141327
141328 case WHERE_DISTINCT_UNIQUE: {
141329 /* nothing to do */
141330 break;
141331 }
141332
141333 default: {
141334 int r1 = sqlite3GetTempReg(pParse);
141335 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iTab, p2: addrRepeat, p3: regElem, p4: nResultCol);
141336 VdbeCoverage(v);
141337 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regElem, p2: nResultCol, p3: r1);
141338 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iTab, p2: r1, p3: regElem, p4: nResultCol);
141339 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
141340 sqlite3ReleaseTempReg(pParse, iReg: r1);
141341 iRet = iTab;
141342 break;
141343 }
141344 }
141345
141346 return iRet;
141347}
141348
141349/*
141350** This routine runs after codeDistinct(). It makes necessary
141351** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()
141352** routine made use of. This processing must be done separately since
141353** sometimes codeDistinct is called before the OP_OpenEphemeral is actually
141354** laid down.
141355**
141356** WHERE_DISTINCT_NOOP:
141357** WHERE_DISTINCT_UNORDERED:
141358**
141359** No adjustments necessary. This function is a no-op.
141360**
141361** WHERE_DISTINCT_UNIQUE:
141362**
141363** The ephemeral table is not needed. So change the
141364** OP_OpenEphemeral opcode into an OP_Noop.
141365**
141366** WHERE_DISTINCT_ORDERED:
141367**
141368** The ephemeral table is not needed. But we do need register
141369** iVal to be initialized to NULL. So change the OP_OpenEphemeral
141370** into an OP_Null on the iVal register.
141371*/
141372static void fixDistinctOpenEph(
141373 Parse *pParse, /* Parsing and code generating context */
141374 int eTnctType, /* WHERE_DISTINCT_* value */
141375 int iVal, /* Value returned by codeDistinct() */
141376 int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */
141377){
141378 if( pParse->nErr==0
141379 && (eTnctType==WHERE_DISTINCT_UNIQUE || eTnctType==WHERE_DISTINCT_ORDERED)
141380 ){
141381 Vdbe *v = pParse->pVdbe;
141382 sqlite3VdbeChangeToNoop(p: v, addr: iOpenEphAddr);
141383 if( sqlite3VdbeGetOp(p: v, addr: iOpenEphAddr+1)->opcode==OP_Explain ){
141384 sqlite3VdbeChangeToNoop(p: v, addr: iOpenEphAddr+1);
141385 }
141386 if( eTnctType==WHERE_DISTINCT_ORDERED ){
141387 /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared
141388 ** bit on the first register of the previous value. This will cause the
141389 ** OP_Ne added in codeDistinct() to always fail on the first iteration of
141390 ** the loop even if the first row is all NULLs. */
141391 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: iOpenEphAddr);
141392 pOp->opcode = OP_Null;
141393 pOp->p1 = 1;
141394 pOp->p2 = iVal;
141395 }
141396 }
141397}
141398
141399#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141400/*
141401** This function is called as part of inner-loop generation for a SELECT
141402** statement with an ORDER BY that is not optimized by an index. It
141403** determines the expressions, if any, that the sorter-reference
141404** optimization should be used for. The sorter-reference optimization
141405** is used for SELECT queries like:
141406**
141407** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
141408**
141409** If the optimization is used for expression "bigblob", then instead of
141410** storing values read from that column in the sorter records, the PK of
141411** the row from table t1 is stored instead. Then, as records are extracted from
141412** the sorter to return to the user, the required value of bigblob is
141413** retrieved directly from table t1. If the values are very large, this
141414** can be more efficient than storing them directly in the sorter records.
141415**
141416** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList
141417** for which the sorter-reference optimization should be enabled.
141418** Additionally, the pSort->aDefer[] array is populated with entries
141419** for all cursors required to evaluate all selected expressions. Finally.
141420** output variable (*ppExtra) is set to an expression list containing
141421** expressions for all extra PK values that should be stored in the
141422** sorter records.
141423*/
141424static void selectExprDefer(
141425 Parse *pParse, /* Leave any error here */
141426 SortCtx *pSort, /* Sorter context */
141427 ExprList *pEList, /* Expressions destined for sorter */
141428 ExprList **ppExtra /* Expressions to append to sorter record */
141429){
141430 int i;
141431 int nDefer = 0;
141432 ExprList *pExtra = 0;
141433 for(i=0; i<pEList->nExpr; i++){
141434 struct ExprList_item *pItem = &pEList->a[i];
141435 if( pItem->u.x.iOrderByCol==0 ){
141436 Expr *pExpr = pItem->pExpr;
141437 Table *pTab;
141438 if( pExpr->op==TK_COLUMN
141439 && pExpr->iColumn>=0
141440 && ALWAYS( ExprUseYTab(pExpr) )
141441 && (pTab = pExpr->y.pTab)!=0
141442 && IsOrdinaryTable(pTab)
141443 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
141444 ){
141445 int j;
141446 for(j=0; j<nDefer; j++){
141447 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
141448 }
141449 if( j==nDefer ){
141450 if( nDefer==ArraySize(pSort->aDefer) ){
141451 continue;
141452 }else{
141453 int nKey = 1;
141454 int k;
141455 Index *pPk = 0;
141456 if( !HasRowid(pTab) ){
141457 pPk = sqlite3PrimaryKeyIndex(pTab);
141458 nKey = pPk->nKeyCol;
141459 }
141460 for(k=0; k<nKey; k++){
141461 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
141462 if( pNew ){
141463 pNew->iTable = pExpr->iTable;
141464 assert( ExprUseYTab(pNew) );
141465 pNew->y.pTab = pExpr->y.pTab;
141466 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
141467 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
141468 }
141469 }
141470 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
141471 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
141472 pSort->aDefer[nDefer].nKey = nKey;
141473 nDefer++;
141474 }
141475 }
141476 pItem->fg.bSorterRef = 1;
141477 }
141478 }
141479 }
141480 pSort->nDefer = (u8)nDefer;
141481 *ppExtra = pExtra;
141482}
141483#endif
141484
141485/*
141486** This routine generates the code for the inside of the inner loop
141487** of a SELECT.
141488**
141489** If srcTab is negative, then the p->pEList expressions
141490** are evaluated in order to get the data for this row. If srcTab is
141491** zero or more, then data is pulled from srcTab and p->pEList is used only
141492** to get the number of columns and the collation sequence for each column.
141493*/
141494static void selectInnerLoop(
141495 Parse *pParse, /* The parser context */
141496 Select *p, /* The complete select statement being coded */
141497 int srcTab, /* Pull data from this table if non-negative */
141498 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
141499 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
141500 SelectDest *pDest, /* How to dispose of the results */
141501 int iContinue, /* Jump here to continue with next row */
141502 int iBreak /* Jump here to break out of the inner loop */
141503){
141504 Vdbe *v = pParse->pVdbe;
141505 int i;
141506 int hasDistinct; /* True if the DISTINCT keyword is present */
141507 int eDest = pDest->eDest; /* How to dispose of results */
141508 int iParm = pDest->iSDParm; /* First argument to disposal method */
141509 int nResultCol; /* Number of result columns */
141510 int nPrefixReg = 0; /* Number of extra registers before regResult */
141511 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
141512
141513 /* Usually, regResult is the first cell in an array of memory cells
141514 ** containing the current result row. In this case regOrig is set to the
141515 ** same value. However, if the results are being sent to the sorter, the
141516 ** values for any expressions that are also part of the sort-key are omitted
141517 ** from this array. In this case regOrig is set to zero. */
141518 int regResult; /* Start of memory holding current results */
141519 int regOrig; /* Start of memory holding full result (or 0) */
141520
141521 assert( v );
141522 assert( p->pEList!=0 );
141523 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
141524 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
141525 if( pSort==0 && !hasDistinct ){
141526 assert( iContinue!=0 );
141527 codeOffset(v, iOffset: p->iOffset, iContinue);
141528 }
141529
141530 /* Pull the requested columns.
141531 */
141532 nResultCol = p->pEList->nExpr;
141533
141534 if( pDest->iSdst==0 ){
141535 if( pSort ){
141536 nPrefixReg = pSort->pOrderBy->nExpr;
141537 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
141538 pParse->nMem += nPrefixReg;
141539 }
141540 pDest->iSdst = pParse->nMem+1;
141541 pParse->nMem += nResultCol;
141542 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
141543 /* This is an error condition that can result, for example, when a SELECT
141544 ** on the right-hand side of an INSERT contains more result columns than
141545 ** there are columns in the table on the left. The error will be caught
141546 ** and reported later. But we need to make sure enough memory is allocated
141547 ** to avoid other spurious errors in the meantime. */
141548 pParse->nMem += nResultCol;
141549 }
141550 pDest->nSdst = nResultCol;
141551 regOrig = regResult = pDest->iSdst;
141552 if( srcTab>=0 ){
141553 for(i=0; i<nResultCol; i++){
141554 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: i, p3: regResult+i);
141555 VdbeComment((v, "%s", p->pEList->a[i].zEName));
141556 }
141557 }else if( eDest!=SRT_Exists ){
141558#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141559 ExprList *pExtra = 0;
141560#endif
141561 /* If the destination is an EXISTS(...) expression, the actual
141562 ** values returned by the SELECT are not required.
141563 */
141564 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
141565 ExprList *pEList;
141566 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
141567 ecelFlags = SQLITE_ECEL_DUP;
141568 }else{
141569 ecelFlags = 0;
141570 }
141571 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
141572 /* For each expression in p->pEList that is a copy of an expression in
141573 ** the ORDER BY clause (pSort->pOrderBy), set the associated
141574 ** iOrderByCol value to one more than the index of the ORDER BY
141575 ** expression within the sort-key that pushOntoSorter() will generate.
141576 ** This allows the p->pEList field to be omitted from the sorted record,
141577 ** saving space and CPU cycles. */
141578 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
141579
141580 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
141581 int j;
141582 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
141583 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
141584 }
141585 }
141586#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141587 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
141588 if( pExtra && pParse->db->mallocFailed==0 ){
141589 /* If there are any extra PK columns to add to the sorter records,
141590 ** allocate extra memory cells and adjust the OpenEphemeral
141591 ** instruction to account for the larger records. This is only
141592 ** required if there are one or more WITHOUT ROWID tables with
141593 ** composite primary keys in the SortCtx.aDefer[] array. */
141594 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
141595 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
141596 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
141597 pParse->nMem += pExtra->nExpr;
141598 }
141599#endif
141600
141601 /* Adjust nResultCol to account for columns that are omitted
141602 ** from the sorter by the optimizations in this branch */
141603 pEList = p->pEList;
141604 for(i=0; i<pEList->nExpr; i++){
141605 if( pEList->a[i].u.x.iOrderByCol>0
141606#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141607 || pEList->a[i].fg.bSorterRef
141608#endif
141609 ){
141610 nResultCol--;
141611 regOrig = 0;
141612 }
141613 }
141614
141615 testcase( regOrig );
141616 testcase( eDest==SRT_Set );
141617 testcase( eDest==SRT_Mem );
141618 testcase( eDest==SRT_Coroutine );
141619 testcase( eDest==SRT_Output );
141620 assert( eDest==SRT_Set || eDest==SRT_Mem
141621 || eDest==SRT_Coroutine || eDest==SRT_Output
141622 || eDest==SRT_Upfrom );
141623 }
141624 sRowLoadInfo.regResult = regResult;
141625 sRowLoadInfo.ecelFlags = ecelFlags;
141626#ifdef SQLITE_ENABLE_SORTER_REFERENCES
141627 sRowLoadInfo.pExtra = pExtra;
141628 sRowLoadInfo.regExtraResult = regResult + nResultCol;
141629 if( pExtra ) nResultCol += pExtra->nExpr;
141630#endif
141631 if( p->iLimit
141632 && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
141633 && nPrefixReg>0
141634 ){
141635 assert( pSort!=0 );
141636 assert( hasDistinct==0 );
141637 pSort->pDeferredRowLoad = &sRowLoadInfo;
141638 regOrig = 0;
141639 }else{
141640 innerLoopLoadRow(pParse, pSelect: p, pInfo: &sRowLoadInfo);
141641 }
141642 }
141643
141644 /* If the DISTINCT keyword was present on the SELECT statement
141645 ** and this row has been seen before, then do not make this row
141646 ** part of the result.
141647 */
141648 if( hasDistinct ){
141649 int eType = pDistinct->eTnctType;
141650 int iTab = pDistinct->tabTnct;
141651 assert( nResultCol==p->pEList->nExpr );
141652 iTab = codeDistinct(pParse, eTnctType: eType, iTab, addrRepeat: iContinue, pEList: p->pEList, regElem: regResult);
141653 fixDistinctOpenEph(pParse, eTnctType: eType, iVal: iTab, iOpenEphAddr: pDistinct->addrTnct);
141654 if( pSort==0 ){
141655 codeOffset(v, iOffset: p->iOffset, iContinue);
141656 }
141657 }
141658
141659 switch( eDest ){
141660 /* In this mode, write each query result to the key of the temporary
141661 ** table iParm.
141662 */
141663#ifndef SQLITE_OMIT_COMPOUND_SELECT
141664 case SRT_Union: {
141665 int r1;
141666 r1 = sqlite3GetTempReg(pParse);
141667 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol, p3: r1);
141668 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regResult, p4: nResultCol);
141669 sqlite3ReleaseTempReg(pParse, iReg: r1);
141670 break;
141671 }
141672
141673 /* Construct a record from the query result, but instead of
141674 ** saving that record, use it as a key to delete elements from
141675 ** the temporary table iParm.
141676 */
141677 case SRT_Except: {
141678 sqlite3VdbeAddOp3(p: v, OP_IdxDelete, p1: iParm, p2: regResult, p3: nResultCol);
141679 break;
141680 }
141681#endif /* SQLITE_OMIT_COMPOUND_SELECT */
141682
141683 /* Store the result as data using a unique key.
141684 */
141685 case SRT_Fifo:
141686 case SRT_DistFifo:
141687 case SRT_Table:
141688 case SRT_EphemTab: {
141689 int r1 = sqlite3GetTempRange(pParse, nReg: nPrefixReg+1);
141690 testcase( eDest==SRT_Table );
141691 testcase( eDest==SRT_EphemTab );
141692 testcase( eDest==SRT_Fifo );
141693 testcase( eDest==SRT_DistFifo );
141694 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol, p3: r1+nPrefixReg);
141695#if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG)
141696 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
141697 ** that this is an "UPDATE ... FROM" on a virtual table or view. In this
141698 ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC.
141699 ** This does not affect operation in any way - it just allows MakeRecord
141700 ** to process OPFLAG_NOCHANGE values without an assert() failing. */
141701 if( eDest==SRT_Table && pDest->iSDParm2 ){
141702 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
141703 }
141704#endif
141705#ifndef SQLITE_OMIT_CTE
141706 if( eDest==SRT_DistFifo ){
141707 /* If the destination is DistFifo, then cursor (iParm+1) is open
141708 ** on an ephemeral index. If the current row is already present
141709 ** in the index, do not write it to the output. If not, add the
141710 ** current row to the index and proceed with writing it to the
141711 ** output table as well. */
141712 int addr = sqlite3VdbeCurrentAddr(p: v) + 4;
141713 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iParm+1, p2: addr, p3: r1, p4: 0);
141714 VdbeCoverage(v);
141715 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm+1, p2: r1,p3: regResult,p4: nResultCol);
141716 assert( pSort==0 );
141717 }
141718#endif
141719 if( pSort ){
141720 assert( regResult==regOrig );
141721 pushOntoSorter(pParse, pSort, pSelect: p, regData: r1+nPrefixReg, regOrigData: regOrig, nData: 1, nPrefixReg);
141722 }else{
141723 int r2 = sqlite3GetTempReg(pParse);
141724 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iParm, p2: r2);
141725 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: r1, p3: r2);
141726 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
141727 sqlite3ReleaseTempReg(pParse, iReg: r2);
141728 }
141729 sqlite3ReleaseTempRange(pParse, iReg: r1, nReg: nPrefixReg+1);
141730 break;
141731 }
141732
141733 case SRT_Upfrom: {
141734 if( pSort ){
141735 pushOntoSorter(
141736 pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol, nPrefixReg);
141737 }else{
141738 int i2 = pDest->iSDParm2;
141739 int r1 = sqlite3GetTempReg(pParse);
141740
141741 /* If the UPDATE FROM join is an aggregate that matches no rows, it
141742 ** might still be trying to return one row, because that is what
141743 ** aggregates do. Don't record that empty row in the output table. */
141744 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regResult, p2: iBreak); VdbeCoverage(v);
141745
141746 sqlite3VdbeAddOp3(p: v, OP_MakeRecord,
141747 p1: regResult+(i2<0), p2: nResultCol-(i2<0), p3: r1);
141748 if( i2<0 ){
141749 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: r1, p3: regResult);
141750 }else{
141751 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regResult, p4: i2);
141752 }
141753 }
141754 break;
141755 }
141756
141757#ifndef SQLITE_OMIT_SUBQUERY
141758 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
141759 ** then there should be a single item on the stack. Write this
141760 ** item into the set table with bogus data.
141761 */
141762 case SRT_Set: {
141763 if( pSort ){
141764 /* At first glance you would think we could optimize out the
141765 ** ORDER BY in this case since the order of entries in the set
141766 ** does not matter. But there might be a LIMIT clause, in which
141767 ** case the order does matter */
141768 pushOntoSorter(
141769 pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol, nPrefixReg);
141770 }else{
141771 int r1 = sqlite3GetTempReg(pParse);
141772 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
141773 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol,
141774 p3: r1, zP4: pDest->zAffSdst, p4type: nResultCol);
141775 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regResult, p4: nResultCol);
141776 sqlite3ReleaseTempReg(pParse, iReg: r1);
141777 }
141778 break;
141779 }
141780
141781
141782 /* If any row exist in the result set, record that fact and abort.
141783 */
141784 case SRT_Exists: {
141785 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: iParm);
141786 /* The LIMIT clause will terminate the loop for us */
141787 break;
141788 }
141789
141790 /* If this is a scalar select that is part of an expression, then
141791 ** store the results in the appropriate memory cell or array of
141792 ** memory cells and break out of the scan loop.
141793 */
141794 case SRT_Mem: {
141795 if( pSort ){
141796 assert( nResultCol<=pDest->nSdst );
141797 pushOntoSorter(
141798 pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol, nPrefixReg);
141799 }else{
141800 assert( nResultCol==pDest->nSdst );
141801 assert( regResult==iParm );
141802 /* The LIMIT clause will jump out of the loop for us */
141803 }
141804 break;
141805 }
141806#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
141807
141808 case SRT_Coroutine: /* Send data to a co-routine */
141809 case SRT_Output: { /* Return the results */
141810 testcase( eDest==SRT_Coroutine );
141811 testcase( eDest==SRT_Output );
141812 if( pSort ){
141813 pushOntoSorter(pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol,
141814 nPrefixReg);
141815 }else if( eDest==SRT_Coroutine ){
141816 sqlite3VdbeAddOp1(p: v, OP_Yield, p1: pDest->iSDParm);
141817 }else{
141818 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: regResult, p2: nResultCol);
141819 }
141820 break;
141821 }
141822
141823#ifndef SQLITE_OMIT_CTE
141824 /* Write the results into a priority queue that is order according to
141825 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
141826 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
141827 ** pSO->nExpr columns, then make sure all keys are unique by adding a
141828 ** final OP_Sequence column. The last column is the record as a blob.
141829 */
141830 case SRT_DistQueue:
141831 case SRT_Queue: {
141832 int nKey;
141833 int r1, r2, r3;
141834 int addrTest = 0;
141835 ExprList *pSO;
141836 pSO = pDest->pOrderBy;
141837 assert( pSO );
141838 nKey = pSO->nExpr;
141839 r1 = sqlite3GetTempReg(pParse);
141840 r2 = sqlite3GetTempRange(pParse, nReg: nKey+2);
141841 r3 = r2+nKey+1;
141842 if( eDest==SRT_DistQueue ){
141843 /* If the destination is DistQueue, then cursor (iParm+1) is open
141844 ** on a second ephemeral index that holds all values every previously
141845 ** added to the queue. */
141846 addrTest = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iParm+1, p2: 0,
141847 p3: regResult, p4: nResultCol);
141848 VdbeCoverage(v);
141849 }
141850 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol, p3: r3);
141851 if( eDest==SRT_DistQueue ){
141852 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: iParm+1, p2: r3);
141853 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
141854 }
141855 for(i=0; i<nKey; i++){
141856 sqlite3VdbeAddOp2(p: v, OP_SCopy,
141857 p1: regResult + pSO->a[i].u.x.iOrderByCol - 1,
141858 p2: r2+i);
141859 }
141860 sqlite3VdbeAddOp2(p: v, OP_Sequence, p1: iParm, p2: r2+nKey);
141861 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: r2, p2: nKey+2, p3: r1);
141862 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: r2, p4: nKey+2);
141863 if( addrTest ) sqlite3VdbeJumpHere(p: v, addr: addrTest);
141864 sqlite3ReleaseTempReg(pParse, iReg: r1);
141865 sqlite3ReleaseTempRange(pParse, iReg: r2, nReg: nKey+2);
141866 break;
141867 }
141868#endif /* SQLITE_OMIT_CTE */
141869
141870
141871
141872#if !defined(SQLITE_OMIT_TRIGGER)
141873 /* Discard the results. This is used for SELECT statements inside
141874 ** the body of a TRIGGER. The purpose of such selects is to call
141875 ** user-defined functions that have side effects. We do not care
141876 ** about the actual results of the select.
141877 */
141878 default: {
141879 assert( eDest==SRT_Discard );
141880 break;
141881 }
141882#endif
141883 }
141884
141885 /* Jump to the end of the loop if the LIMIT is reached. Except, if
141886 ** there is a sorter, in which case the sorter has already limited
141887 ** the output for us.
141888 */
141889 if( pSort==0 && p->iLimit ){
141890 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: p->iLimit, p2: iBreak); VdbeCoverage(v);
141891 }
141892}
141893
141894/*
141895** Allocate a KeyInfo object sufficient for an index of N key columns and
141896** X extra columns.
141897*/
141898SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
141899 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
141900 KeyInfo *p = sqlite3DbMallocRawNN(db, n: sizeof(KeyInfo) + nExtra);
141901 if( p ){
141902 p->aSortFlags = (u8*)&p->aColl[N+X];
141903 p->nKeyField = (u16)N;
141904 p->nAllField = (u16)(N+X);
141905 p->enc = ENC(db);
141906 p->db = db;
141907 p->nRef = 1;
141908 memset(s: &p[1], c: 0, n: nExtra);
141909 }else{
141910 return (KeyInfo*)sqlite3OomFault(db);
141911 }
141912 return p;
141913}
141914
141915/*
141916** Deallocate a KeyInfo object
141917*/
141918SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
141919 if( p ){
141920 assert( p->db!=0 );
141921 assert( p->nRef>0 );
141922 p->nRef--;
141923 if( p->nRef==0 ) sqlite3DbNNFreeNN(db: p->db, p);
141924 }
141925}
141926
141927/*
141928** Make a new pointer to a KeyInfo object
141929*/
141930SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
141931 if( p ){
141932 assert( p->nRef>0 );
141933 p->nRef++;
141934 }
141935 return p;
141936}
141937
141938#ifdef SQLITE_DEBUG
141939/*
141940** Return TRUE if a KeyInfo object can be change. The KeyInfo object
141941** can only be changed if this is just a single reference to the object.
141942**
141943** This routine is used only inside of assert() statements.
141944*/
141945SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
141946#endif /* SQLITE_DEBUG */
141947
141948/*
141949** Given an expression list, generate a KeyInfo structure that records
141950** the collating sequence for each expression in that expression list.
141951**
141952** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
141953** KeyInfo structure is appropriate for initializing a virtual index to
141954** implement that clause. If the ExprList is the result set of a SELECT
141955** then the KeyInfo structure is appropriate for initializing a virtual
141956** index to implement a DISTINCT test.
141957**
141958** Space to hold the KeyInfo structure is obtained from malloc. The calling
141959** function is responsible for seeing that this structure is eventually
141960** freed.
141961*/
141962SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
141963 Parse *pParse, /* Parsing context */
141964 ExprList *pList, /* Form the KeyInfo object from this ExprList */
141965 int iStart, /* Begin with this column of pList */
141966 int nExtra /* Add this many extra columns to the end */
141967){
141968 int nExpr;
141969 KeyInfo *pInfo;
141970 struct ExprList_item *pItem;
141971 sqlite3 *db = pParse->db;
141972 int i;
141973
141974 nExpr = pList->nExpr;
141975 pInfo = sqlite3KeyInfoAlloc(db, N: nExpr-iStart, X: nExtra+1);
141976 if( pInfo ){
141977 assert( sqlite3KeyInfoIsWriteable(pInfo) );
141978 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
141979 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pExpr: pItem->pExpr);
141980 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
141981 }
141982 }
141983 return pInfo;
141984}
141985
141986/*
141987** Name of the connection operator, used for error messages.
141988*/
141989SQLITE_PRIVATE const char *sqlite3SelectOpName(int id){
141990 char *z;
141991 switch( id ){
141992 case TK_ALL: z = "UNION ALL"; break;
141993 case TK_INTERSECT: z = "INTERSECT"; break;
141994 case TK_EXCEPT: z = "EXCEPT"; break;
141995 default: z = "UNION"; break;
141996 }
141997 return z;
141998}
141999
142000#ifndef SQLITE_OMIT_EXPLAIN
142001/*
142002** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
142003** is a no-op. Otherwise, it adds a single row of output to the EQP result,
142004** where the caption is of the form:
142005**
142006** "USE TEMP B-TREE FOR xxx"
142007**
142008** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
142009** is determined by the zUsage argument.
142010*/
142011static void explainTempTable(Parse *pParse, const char *zUsage){
142012 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
142013}
142014
142015/*
142016** Assign expression b to lvalue a. A second, no-op, version of this macro
142017** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
142018** in sqlite3Select() to assign values to structure member variables that
142019** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
142020** code with #ifndef directives.
142021*/
142022# define explainSetInteger(a, b) a = b
142023
142024#else
142025/* No-op versions of the explainXXX() functions and macros. */
142026# define explainTempTable(y,z)
142027# define explainSetInteger(y,z)
142028#endif
142029
142030
142031/*
142032** If the inner loop was generated using a non-null pOrderBy argument,
142033** then the results were placed in a sorter. After the loop is terminated
142034** we need to run the sorter and output the results. The following
142035** routine generates the code needed to do that.
142036*/
142037static void generateSortTail(
142038 Parse *pParse, /* Parsing context */
142039 Select *p, /* The SELECT statement */
142040 SortCtx *pSort, /* Information on the ORDER BY clause */
142041 int nColumn, /* Number of columns of data */
142042 SelectDest *pDest /* Write the sorted results here */
142043){
142044 Vdbe *v = pParse->pVdbe; /* The prepared statement */
142045 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
142046 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
142047 int addr; /* Top of output loop. Jump for Next. */
142048 int addrOnce = 0;
142049 int iTab;
142050 ExprList *pOrderBy = pSort->pOrderBy;
142051 int eDest = pDest->eDest;
142052 int iParm = pDest->iSDParm;
142053 int regRow;
142054 int regRowid;
142055 int iCol;
142056 int nKey; /* Number of key columns in sorter record */
142057 int iSortTab; /* Sorter cursor to read from */
142058 int i;
142059 int bSeq; /* True if sorter record includes seq. no. */
142060 int nRefKey = 0;
142061 struct ExprList_item *aOutEx = p->pEList->a;
142062#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
142063 int addrExplain; /* Address of OP_Explain instruction */
142064#endif
142065
142066 ExplainQueryPlan2(addrExplain, (pParse, 0,
142067 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat>0?"RIGHT PART OF ":"")
142068 );
142069 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
142070 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
142071
142072
142073 assert( addrBreak<0 );
142074 if( pSort->labelBkOut ){
142075 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pSort->regReturn, p2: pSort->labelBkOut);
142076 sqlite3VdbeGoto(p: v, iDest: addrBreak);
142077 sqlite3VdbeResolveLabel(v, x: pSort->labelBkOut);
142078 }
142079
142080#ifdef SQLITE_ENABLE_SORTER_REFERENCES
142081 /* Open any cursors needed for sorter-reference expressions */
142082 for(i=0; i<pSort->nDefer; i++){
142083 Table *pTab = pSort->aDefer[i].pTab;
142084 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
142085 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
142086 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
142087 }
142088#endif
142089
142090 iTab = pSort->iECursor;
142091 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
142092 if( eDest==SRT_Mem && p->iOffset ){
142093 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pDest->iSdst);
142094 }
142095 regRowid = 0;
142096 regRow = pDest->iSdst;
142097 }else{
142098 regRowid = sqlite3GetTempReg(pParse);
142099 if( eDest==SRT_EphemTab || eDest==SRT_Table ){
142100 regRow = sqlite3GetTempReg(pParse);
142101 nColumn = 0;
142102 }else{
142103 regRow = sqlite3GetTempRange(pParse, nReg: nColumn);
142104 }
142105 }
142106 nKey = pOrderBy->nExpr - pSort->nOBSat;
142107 if( pSort->sortFlags & SORTFLAG_UseSorter ){
142108 int regSortOut = ++pParse->nMem;
142109 iSortTab = pParse->nTab++;
142110 if( pSort->labelBkOut ){
142111 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
142112 }
142113 sqlite3VdbeAddOp3(p: v, OP_OpenPseudo, p1: iSortTab, p2: regSortOut,
142114 p3: nKey+1+nColumn+nRefKey);
142115 if( addrOnce ) sqlite3VdbeJumpHere(p: v, addr: addrOnce);
142116 addr = 1 + sqlite3VdbeAddOp2(p: v, OP_SorterSort, p1: iTab, p2: addrBreak);
142117 VdbeCoverage(v);
142118 assert( p->iLimit==0 && p->iOffset==0 );
142119 sqlite3VdbeAddOp3(p: v, OP_SorterData, p1: iTab, p2: regSortOut, p3: iSortTab);
142120 bSeq = 0;
142121 }else{
142122 addr = 1 + sqlite3VdbeAddOp2(p: v, OP_Sort, p1: iTab, p2: addrBreak); VdbeCoverage(v);
142123 codeOffset(v, iOffset: p->iOffset, iContinue: addrContinue);
142124 iSortTab = iTab;
142125 bSeq = 1;
142126 if( p->iOffset>0 ){
142127 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: p->iLimit, p2: -1);
142128 }
142129 }
142130 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
142131#ifdef SQLITE_ENABLE_SORTER_REFERENCES
142132 if( aOutEx[i].fg.bSorterRef ) continue;
142133#endif
142134 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
142135 }
142136#ifdef SQLITE_ENABLE_SORTER_REFERENCES
142137 if( pSort->nDefer ){
142138 int iKey = iCol+1;
142139 int regKey = sqlite3GetTempRange(pParse, nRefKey);
142140
142141 for(i=0; i<pSort->nDefer; i++){
142142 int iCsr = pSort->aDefer[i].iCsr;
142143 Table *pTab = pSort->aDefer[i].pTab;
142144 int nKey = pSort->aDefer[i].nKey;
142145
142146 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
142147 if( HasRowid(pTab) ){
142148 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
142149 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
142150 sqlite3VdbeCurrentAddr(v)+1, regKey);
142151 }else{
142152 int k;
142153 int iJmp;
142154 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
142155 for(k=0; k<nKey; k++){
142156 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
142157 }
142158 iJmp = sqlite3VdbeCurrentAddr(v);
142159 sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
142160 sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
142161 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
142162 }
142163 }
142164 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
142165 }
142166#endif
142167 for(i=nColumn-1; i>=0; i--){
142168#ifdef SQLITE_ENABLE_SORTER_REFERENCES
142169 if( aOutEx[i].fg.bSorterRef ){
142170 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
142171 }else
142172#endif
142173 {
142174 int iRead;
142175 if( aOutEx[i].u.x.iOrderByCol ){
142176 iRead = aOutEx[i].u.x.iOrderByCol-1;
142177 }else{
142178 iRead = iCol--;
142179 }
142180 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iSortTab, p2: iRead, p3: regRow+i);
142181 VdbeComment((v, "%s", aOutEx[i].zEName));
142182 }
142183 }
142184 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
142185 switch( eDest ){
142186 case SRT_Table:
142187 case SRT_EphemTab: {
142188 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iSortTab, p2: nKey+bSeq, p3: regRow);
142189 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iParm, p2: regRowid);
142190 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: regRow, p3: regRowid);
142191 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
142192 break;
142193 }
142194#ifndef SQLITE_OMIT_SUBQUERY
142195 case SRT_Set: {
142196 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
142197 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regRow, p2: nColumn, p3: regRowid,
142198 zP4: pDest->zAffSdst, p4type: nColumn);
142199 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: regRowid, p3: regRow, p4: nColumn);
142200 break;
142201 }
142202 case SRT_Mem: {
142203 /* The LIMIT clause will terminate the loop for us */
142204 break;
142205 }
142206#endif
142207 case SRT_Upfrom: {
142208 int i2 = pDest->iSDParm2;
142209 int r1 = sqlite3GetTempReg(pParse);
142210 sqlite3VdbeAddOp3(p: v, OP_MakeRecord,p1: regRow+(i2<0),p2: nColumn-(i2<0),p3: r1);
142211 if( i2<0 ){
142212 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: r1, p3: regRow);
142213 }else{
142214 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regRow, p4: i2);
142215 }
142216 break;
142217 }
142218 default: {
142219 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
142220 testcase( eDest==SRT_Output );
142221 testcase( eDest==SRT_Coroutine );
142222 if( eDest==SRT_Output ){
142223 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: pDest->iSdst, p2: nColumn);
142224 }else{
142225 sqlite3VdbeAddOp1(p: v, OP_Yield, p1: pDest->iSDParm);
142226 }
142227 break;
142228 }
142229 }
142230 if( regRowid ){
142231 if( eDest==SRT_Set ){
142232 sqlite3ReleaseTempRange(pParse, iReg: regRow, nReg: nColumn);
142233 }else{
142234 sqlite3ReleaseTempReg(pParse, iReg: regRow);
142235 }
142236 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
142237 }
142238 /* The bottom of the loop
142239 */
142240 sqlite3VdbeResolveLabel(v, x: addrContinue);
142241 if( pSort->sortFlags & SORTFLAG_UseSorter ){
142242 sqlite3VdbeAddOp2(p: v, OP_SorterNext, p1: iTab, p2: addr); VdbeCoverage(v);
142243 }else{
142244 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iTab, p2: addr); VdbeCoverage(v);
142245 }
142246 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
142247 if( pSort->regReturn ) sqlite3VdbeAddOp1(p: v, OP_Return, p1: pSort->regReturn);
142248 sqlite3VdbeResolveLabel(v, x: addrBreak);
142249}
142250
142251/*
142252** Return a pointer to a string containing the 'declaration type' of the
142253** expression pExpr. The string may be treated as static by the caller.
142254**
142255** The declaration type is the exact datatype definition extracted from the
142256** original CREATE TABLE statement if the expression is a column. The
142257** declaration type for a ROWID field is INTEGER. Exactly when an expression
142258** is considered a column can be complex in the presence of subqueries. The
142259** result-set expression in all of the following SELECT statements is
142260** considered a column by this function.
142261**
142262** SELECT col FROM tbl;
142263** SELECT (SELECT col FROM tbl;
142264** SELECT (SELECT col FROM tbl);
142265** SELECT abc FROM (SELECT col AS abc FROM tbl);
142266**
142267** The declaration type for any expression other than a column is NULL.
142268**
142269** This routine has either 3 or 6 parameters depending on whether or not
142270** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
142271*/
142272#ifdef SQLITE_ENABLE_COLUMN_METADATA
142273# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
142274#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
142275# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
142276#endif
142277static const char *columnTypeImpl(
142278 NameContext *pNC,
142279#ifndef SQLITE_ENABLE_COLUMN_METADATA
142280 Expr *pExpr
142281#else
142282 Expr *pExpr,
142283 const char **pzOrigDb,
142284 const char **pzOrigTab,
142285 const char **pzOrigCol
142286#endif
142287){
142288 char const *zType = 0;
142289 int j;
142290#ifdef SQLITE_ENABLE_COLUMN_METADATA
142291 char const *zOrigDb = 0;
142292 char const *zOrigTab = 0;
142293 char const *zOrigCol = 0;
142294#endif
142295
142296 assert( pExpr!=0 );
142297 assert( pNC->pSrcList!=0 );
142298 switch( pExpr->op ){
142299 case TK_COLUMN: {
142300 /* The expression is a column. Locate the table the column is being
142301 ** extracted from in NameContext.pSrcList. This table may be real
142302 ** database table or a subquery.
142303 */
142304 Table *pTab = 0; /* Table structure column is extracted from */
142305 Select *pS = 0; /* Select the column is extracted from */
142306 int iCol = pExpr->iColumn; /* Index of column in pTab */
142307 while( pNC && !pTab ){
142308 SrcList *pTabList = pNC->pSrcList;
142309 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
142310 if( j<pTabList->nSrc ){
142311 pTab = pTabList->a[j].pTab;
142312 pS = pTabList->a[j].pSelect;
142313 }else{
142314 pNC = pNC->pNext;
142315 }
142316 }
142317
142318 if( pTab==0 ){
142319 /* At one time, code such as "SELECT new.x" within a trigger would
142320 ** cause this condition to run. Since then, we have restructured how
142321 ** trigger code is generated and so this condition is no longer
142322 ** possible. However, it can still be true for statements like
142323 ** the following:
142324 **
142325 ** CREATE TABLE t1(col INTEGER);
142326 ** SELECT (SELECT t1.col) FROM FROM t1;
142327 **
142328 ** when columnType() is called on the expression "t1.col" in the
142329 ** sub-select. In this case, set the column type to NULL, even
142330 ** though it should really be "INTEGER".
142331 **
142332 ** This is not a problem, as the column type of "t1.col" is never
142333 ** used. When columnType() is called on the expression
142334 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
142335 ** branch below. */
142336 break;
142337 }
142338
142339 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
142340 if( pS ){
142341 /* The "table" is actually a sub-select or a view in the FROM clause
142342 ** of the SELECT statement. Return the declaration type and origin
142343 ** data for the result-set column of the sub-select.
142344 */
142345 if( iCol<pS->pEList->nExpr
142346#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
142347 && iCol>=0
142348#else
142349 && ALWAYS(iCol>=0)
142350#endif
142351 ){
142352 /* If iCol is less than zero, then the expression requests the
142353 ** rowid of the sub-select or view. This expression is legal (see
142354 ** test case misc2.2.2) - it always evaluates to NULL.
142355 */
142356 NameContext sNC;
142357 Expr *p = pS->pEList->a[iCol].pExpr;
142358 sNC.pSrcList = pS->pSrc;
142359 sNC.pNext = pNC;
142360 sNC.pParse = pNC->pParse;
142361 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
142362 }
142363 }else{
142364 /* A real table or a CTE table */
142365 assert( !pS );
142366#ifdef SQLITE_ENABLE_COLUMN_METADATA
142367 if( iCol<0 ) iCol = pTab->iPKey;
142368 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
142369 if( iCol<0 ){
142370 zType = "INTEGER";
142371 zOrigCol = "rowid";
142372 }else{
142373 zOrigCol = pTab->aCol[iCol].zCnName;
142374 zType = sqlite3ColumnType(pCol: &pTab->aCol[iCol],zDflt: 0);
142375 }
142376 zOrigTab = pTab->zName;
142377 if( pNC->pParse && pTab->pSchema ){
142378 int iDb = sqlite3SchemaToIndex(db: pNC->pParse->db, pSchema: pTab->pSchema);
142379 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
142380 }
142381#else
142382 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
142383 if( iCol<0 ){
142384 zType = "INTEGER";
142385 }else{
142386 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
142387 }
142388#endif
142389 }
142390 break;
142391 }
142392#ifndef SQLITE_OMIT_SUBQUERY
142393 case TK_SELECT: {
142394 /* The expression is a sub-select. Return the declaration type and
142395 ** origin info for the single column in the result set of the SELECT
142396 ** statement.
142397 */
142398 NameContext sNC;
142399 Select *pS;
142400 Expr *p;
142401 assert( ExprUseXSelect(pExpr) );
142402 pS = pExpr->x.pSelect;
142403 p = pS->pEList->a[0].pExpr;
142404 sNC.pSrcList = pS->pSrc;
142405 sNC.pNext = pNC;
142406 sNC.pParse = pNC->pParse;
142407 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
142408 break;
142409 }
142410#endif
142411 }
142412
142413#ifdef SQLITE_ENABLE_COLUMN_METADATA
142414 if( pzOrigDb ){
142415 assert( pzOrigTab && pzOrigCol );
142416 *pzOrigDb = zOrigDb;
142417 *pzOrigTab = zOrigTab;
142418 *pzOrigCol = zOrigCol;
142419 }
142420#endif
142421 return zType;
142422}
142423
142424/*
142425** Generate code that will tell the VDBE the declaration types of columns
142426** in the result set.
142427*/
142428static void generateColumnTypes(
142429 Parse *pParse, /* Parser context */
142430 SrcList *pTabList, /* List of tables */
142431 ExprList *pEList /* Expressions defining the result set */
142432){
142433#ifndef SQLITE_OMIT_DECLTYPE
142434 Vdbe *v = pParse->pVdbe;
142435 int i;
142436 NameContext sNC;
142437 sNC.pSrcList = pTabList;
142438 sNC.pParse = pParse;
142439 sNC.pNext = 0;
142440 for(i=0; i<pEList->nExpr; i++){
142441 Expr *p = pEList->a[i].pExpr;
142442 const char *zType;
142443#ifdef SQLITE_ENABLE_COLUMN_METADATA
142444 const char *zOrigDb = 0;
142445 const char *zOrigTab = 0;
142446 const char *zOrigCol = 0;
142447 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
142448
142449 /* The vdbe must make its own copy of the column-type and other
142450 ** column specific strings, in case the schema is reset before this
142451 ** virtual machine is deleted.
142452 */
142453 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_DATABASE, zName: zOrigDb, SQLITE_TRANSIENT);
142454 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_TABLE, zName: zOrigTab, SQLITE_TRANSIENT);
142455 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_COLUMN, zName: zOrigCol, SQLITE_TRANSIENT);
142456#else
142457 zType = columnType(&sNC, p, 0, 0, 0);
142458#endif
142459 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_DECLTYPE, zName: zType, SQLITE_TRANSIENT);
142460 }
142461#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
142462}
142463
142464
142465/*
142466** Compute the column names for a SELECT statement.
142467**
142468** The only guarantee that SQLite makes about column names is that if the
142469** column has an AS clause assigning it a name, that will be the name used.
142470** That is the only documented guarantee. However, countless applications
142471** developed over the years have made baseless assumptions about column names
142472** and will break if those assumptions changes. Hence, use extreme caution
142473** when modifying this routine to avoid breaking legacy.
142474**
142475** See Also: sqlite3ColumnsFromExprList()
142476**
142477** The PRAGMA short_column_names and PRAGMA full_column_names settings are
142478** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
142479** applications should operate this way. Nevertheless, we need to support the
142480** other modes for legacy:
142481**
142482** short=OFF, full=OFF: Column name is the text of the expression has it
142483** originally appears in the SELECT statement. In
142484** other words, the zSpan of the result expression.
142485**
142486** short=ON, full=OFF: (This is the default setting). If the result
142487** refers directly to a table column, then the
142488** result column name is just the table column
142489** name: COLUMN. Otherwise use zSpan.
142490**
142491** full=ON, short=ANY: If the result refers directly to a table column,
142492** then the result column name with the table name
142493** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
142494*/
142495SQLITE_PRIVATE void sqlite3GenerateColumnNames(
142496 Parse *pParse, /* Parser context */
142497 Select *pSelect /* Generate column names for this SELECT statement */
142498){
142499 Vdbe *v = pParse->pVdbe;
142500 int i;
142501 Table *pTab;
142502 SrcList *pTabList;
142503 ExprList *pEList;
142504 sqlite3 *db = pParse->db;
142505 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
142506 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
142507
142508 if( pParse->colNamesSet ) return;
142509 /* Column names are determined by the left-most term of a compound select */
142510 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
142511 TREETRACE(0x80,pParse,pSelect,("generating column names\n"));
142512 pTabList = pSelect->pSrc;
142513 pEList = pSelect->pEList;
142514 assert( v!=0 );
142515 assert( pTabList!=0 );
142516 pParse->colNamesSet = 1;
142517 fullName = (db->flags & SQLITE_FullColNames)!=0;
142518 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
142519 sqlite3VdbeSetNumCols(p: v, nResColumn: pEList->nExpr);
142520 for(i=0; i<pEList->nExpr; i++){
142521 Expr *p = pEList->a[i].pExpr;
142522
142523 assert( p!=0 );
142524 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
142525 assert( p->op!=TK_COLUMN
142526 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
142527 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
142528 /* An AS clause always takes first priority */
142529 char *zName = pEList->a[i].zEName;
142530 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
142531 }else if( srcName && p->op==TK_COLUMN ){
142532 char *zCol;
142533 int iCol = p->iColumn;
142534 pTab = p->y.pTab;
142535 assert( pTab!=0 );
142536 if( iCol<0 ) iCol = pTab->iPKey;
142537 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
142538 if( iCol<0 ){
142539 zCol = "rowid";
142540 }else{
142541 zCol = pTab->aCol[iCol].zCnName;
142542 }
142543 if( fullName ){
142544 char *zName = 0;
142545 zName = sqlite3MPrintf(db, zFormat: "%s.%s", pTab->zName, zCol);
142546 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
142547 }else{
142548 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName: zCol, SQLITE_TRANSIENT);
142549 }
142550 }else{
142551 const char *z = pEList->a[i].zEName;
142552 z = z==0 ? sqlite3MPrintf(db, zFormat: "column%d", i+1) : sqlite3DbStrDup(db, z);
142553 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName: z, SQLITE_DYNAMIC);
142554 }
142555 }
142556 generateColumnTypes(pParse, pTabList, pEList);
142557}
142558
142559/*
142560** Given an expression list (which is really the list of expressions
142561** that form the result set of a SELECT statement) compute appropriate
142562** column names for a table that would hold the expression list.
142563**
142564** All column names will be unique.
142565**
142566** Only the column names are computed. Column.zType, Column.zColl,
142567** and other fields of Column are zeroed.
142568**
142569** Return SQLITE_OK on success. If a memory allocation error occurs,
142570** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
142571**
142572** The only guarantee that SQLite makes about column names is that if the
142573** column has an AS clause assigning it a name, that will be the name used.
142574** That is the only documented guarantee. However, countless applications
142575** developed over the years have made baseless assumptions about column names
142576** and will break if those assumptions changes. Hence, use extreme caution
142577** when modifying this routine to avoid breaking legacy.
142578**
142579** See Also: sqlite3GenerateColumnNames()
142580*/
142581SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
142582 Parse *pParse, /* Parsing context */
142583 ExprList *pEList, /* Expr list from which to derive column names */
142584 i16 *pnCol, /* Write the number of columns here */
142585 Column **paCol /* Write the new column list here */
142586){
142587 sqlite3 *db = pParse->db; /* Database connection */
142588 int i, j; /* Loop counters */
142589 u32 cnt; /* Index added to make the name unique */
142590 Column *aCol, *pCol; /* For looping over result columns */
142591 int nCol; /* Number of columns in the result set */
142592 char *zName; /* Column name */
142593 int nName; /* Size of name in zName[] */
142594 Hash ht; /* Hash table of column names */
142595 Table *pTab;
142596
142597 sqlite3HashInit(pNew: &ht);
142598 if( pEList ){
142599 nCol = pEList->nExpr;
142600 aCol = sqlite3DbMallocZero(db, n: sizeof(aCol[0])*nCol);
142601 testcase( aCol==0 );
142602 if( NEVER(nCol>32767) ) nCol = 32767;
142603 }else{
142604 nCol = 0;
142605 aCol = 0;
142606 }
142607 assert( nCol==(i16)nCol );
142608 *pnCol = nCol;
142609 *paCol = aCol;
142610
142611 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
142612 struct ExprList_item *pX = &pEList->a[i];
142613 struct ExprList_item *pCollide;
142614 /* Get an appropriate name for the column
142615 */
142616 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
142617 /* If the column contains an "AS <name>" phrase, use <name> as the name */
142618 }else{
142619 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pX->pExpr);
142620 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
142621 pColExpr = pColExpr->pRight;
142622 assert( pColExpr!=0 );
142623 }
142624 if( pColExpr->op==TK_COLUMN
142625 && ALWAYS( ExprUseYTab(pColExpr) )
142626 && ALWAYS( pColExpr->y.pTab!=0 )
142627 ){
142628 /* For columns use the column name name */
142629 int iCol = pColExpr->iColumn;
142630 pTab = pColExpr->y.pTab;
142631 if( iCol<0 ) iCol = pTab->iPKey;
142632 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
142633 }else if( pColExpr->op==TK_ID ){
142634 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
142635 zName = pColExpr->u.zToken;
142636 }else{
142637 /* Use the original text of the column expression as its name */
142638 assert( zName==pX->zEName ); /* pointer comparison intended */
142639 }
142640 }
142641 if( zName && !sqlite3IsTrueOrFalse(zIn: zName) ){
142642 zName = sqlite3DbStrDup(db, z: zName);
142643 }else{
142644 zName = sqlite3MPrintf(db,zFormat: "column%d",i+1);
142645 }
142646
142647 /* Make sure the column name is unique. If the name is not unique,
142648 ** append an integer to the name so that it becomes unique.
142649 */
142650 cnt = 0;
142651 while( zName && (pCollide = sqlite3HashFind(pH: &ht, pKey: zName))!=0 ){
142652 if( pCollide->fg.bUsingTerm ){
142653 pCol->colFlags |= COLFLAG_NOEXPAND;
142654 }
142655 nName = sqlite3Strlen30(z: zName);
142656 if( nName>0 ){
142657 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
142658 if( zName[j]==':' ) nName = j;
142659 }
142660 zName = sqlite3MPrintf(db, zFormat: "%.*z:%u", nName, zName, ++cnt);
142661 sqlite3ProgressCheck(p: pParse);
142662 if( cnt>3 ){
142663 sqlite3_randomness(N: sizeof(cnt), pBuf: &cnt);
142664 }
142665 }
142666 pCol->zCnName = zName;
142667 pCol->hName = sqlite3StrIHash(z: zName);
142668 if( pX->fg.bNoExpand ){
142669 pCol->colFlags |= COLFLAG_NOEXPAND;
142670 }
142671 sqlite3ColumnPropertiesFromName(0, pCol);
142672 if( zName && sqlite3HashInsert(pH: &ht, pKey: zName, data: pX)==pX ){
142673 sqlite3OomFault(db);
142674 }
142675 }
142676 sqlite3HashClear(pH: &ht);
142677 if( pParse->nErr ){
142678 for(j=0; j<i; j++){
142679 sqlite3DbFree(db, p: aCol[j].zCnName);
142680 }
142681 sqlite3DbFree(db, p: aCol);
142682 *paCol = 0;
142683 *pnCol = 0;
142684 return pParse->rc;
142685 }
142686 return SQLITE_OK;
142687}
142688
142689/*
142690** pTab is a transient Table object that represents a subquery of some
142691** kind (maybe a parenthesized subquery in the FROM clause of a larger
142692** query, or a VIEW, or a CTE). This routine computes type information
142693** for that Table object based on the Select object that implements the
142694** subquery. For the purposes of this routine, "type information" means:
142695**
142696** * The datatype name, as it might appear in a CREATE TABLE statement
142697** * Which collating sequence to use for the column
142698** * The affinity of the column
142699*/
142700SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(
142701 Parse *pParse, /* Parsing contexts */
142702 Table *pTab, /* Add column type information to this table */
142703 Select *pSelect, /* SELECT used to determine types and collations */
142704 char aff /* Default affinity. */
142705){
142706 sqlite3 *db = pParse->db;
142707 Column *pCol;
142708 CollSeq *pColl;
142709 int i,j;
142710 Expr *p;
142711 struct ExprList_item *a;
142712 NameContext sNC;
142713
142714 assert( pSelect!=0 );
142715 assert( (pSelect->selFlags & SF_Resolved)!=0 );
142716 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
142717 assert( aff==SQLITE_AFF_NONE || aff==SQLITE_AFF_BLOB );
142718 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
142719 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
142720 a = pSelect->pEList->a;
142721 memset(s: &sNC, c: 0, n: sizeof(sNC));
142722 sNC.pSrcList = pSelect->pSrc;
142723 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
142724 const char *zType;
142725 i64 n;
142726 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
142727 p = a[i].pExpr;
142728 /* pCol->szEst = ... // Column size est for SELECT tables never used */
142729 pCol->affinity = sqlite3ExprAffinity(pExpr: p);
142730 if( pCol->affinity<=SQLITE_AFF_NONE ){
142731 pCol->affinity = aff;
142732 }
142733 if( pCol->affinity>=SQLITE_AFF_TEXT && pSelect->pNext ){
142734 int m = 0;
142735 Select *pS2;
142736 for(m=0, pS2=pSelect->pNext; pS2; pS2=pS2->pNext){
142737 m |= sqlite3ExprDataType(pExpr: pS2->pEList->a[i].pExpr);
142738 }
142739 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
142740 pCol->affinity = SQLITE_AFF_BLOB;
142741 }else
142742 if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){
142743 pCol->affinity = SQLITE_AFF_BLOB;
142744 }
142745 if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){
142746 pCol->affinity = SQLITE_AFF_FLEXNUM;
142747 }
142748 }
142749 zType = columnType(&sNC, p, 0, 0, 0);
142750 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zIn: zType, pCol: 0) ){
142751 if( pCol->affinity==SQLITE_AFF_NUMERIC
142752 || pCol->affinity==SQLITE_AFF_FLEXNUM
142753 ){
142754 zType = "NUM";
142755 }else{
142756 zType = 0;
142757 for(j=1; j<SQLITE_N_STDTYPE; j++){
142758 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
142759 zType = sqlite3StdType[j];
142760 break;
142761 }
142762 }
142763 }
142764 }
142765 if( zType ){
142766 i64 m = sqlite3Strlen30(z: zType);
142767 n = sqlite3Strlen30(z: pCol->zCnName);
142768 pCol->zCnName = sqlite3DbReallocOrFree(db, p: pCol->zCnName, n: n+m+2);
142769 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
142770 if( pCol->zCnName ){
142771 memcpy(dest: &pCol->zCnName[n+1], src: zType, n: m+1);
142772 pCol->colFlags |= COLFLAG_HASTYPE;
142773 }
142774 }
142775 pColl = sqlite3ExprCollSeq(pParse, pExpr: p);
142776 if( pColl ){
142777 assert( pTab->pIndex==0 );
142778 sqlite3ColumnSetColl(db, pCol, zColl: pColl->zName);
142779 }
142780 }
142781 pTab->szTabRow = 1; /* Any non-zero value works */
142782}
142783
142784/*
142785** Given a SELECT statement, generate a Table structure that describes
142786** the result set of that SELECT.
142787*/
142788SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
142789 Table *pTab;
142790 sqlite3 *db = pParse->db;
142791 u64 savedFlags;
142792
142793 savedFlags = db->flags;
142794 db->flags &= ~(u64)SQLITE_FullColNames;
142795 db->flags |= SQLITE_ShortColNames;
142796 sqlite3SelectPrep(pParse, pSelect, 0);
142797 db->flags = savedFlags;
142798 if( pParse->nErr ) return 0;
142799 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
142800 pTab = sqlite3DbMallocZero(db, n: sizeof(Table) );
142801 if( pTab==0 ){
142802 return 0;
142803 }
142804 pTab->nTabRef = 1;
142805 pTab->zName = 0;
142806 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
142807 sqlite3ColumnsFromExprList(pParse, pEList: pSelect->pEList, pnCol: &pTab->nCol, paCol: &pTab->aCol);
142808 sqlite3SubqueryColumnTypes(pParse, pTab, pSelect, aff);
142809 pTab->iPKey = -1;
142810 if( db->mallocFailed ){
142811 sqlite3DeleteTable(db, pTable: pTab);
142812 return 0;
142813 }
142814 return pTab;
142815}
142816
142817/*
142818** Get a VDBE for the given parser context. Create a new one if necessary.
142819** If an error occurs, return NULL and leave a message in pParse.
142820*/
142821SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
142822 if( pParse->pVdbe ){
142823 return pParse->pVdbe;
142824 }
142825 if( pParse->pToplevel==0
142826 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
142827 ){
142828 pParse->okConstFactor = 1;
142829 }
142830 return sqlite3VdbeCreate(pParse);
142831}
142832
142833
142834/*
142835** Compute the iLimit and iOffset fields of the SELECT based on the
142836** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
142837** that appear in the original SQL statement after the LIMIT and OFFSET
142838** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
142839** are the integer memory register numbers for counters used to compute
142840** the limit and offset. If there is no limit and/or offset, then
142841** iLimit and iOffset are negative.
142842**
142843** This routine changes the values of iLimit and iOffset only if
142844** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
142845** and iOffset should have been preset to appropriate default values (zero)
142846** prior to calling this routine.
142847**
142848** The iOffset register (if it exists) is initialized to the value
142849** of the OFFSET. The iLimit register is initialized to LIMIT. Register
142850** iOffset+1 is initialized to LIMIT+OFFSET.
142851**
142852** Only if pLimit->pLeft!=0 do the limit registers get
142853** redefined. The UNION ALL operator uses this property to force
142854** the reuse of the same limit and offset registers across multiple
142855** SELECT statements.
142856*/
142857static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
142858 Vdbe *v = 0;
142859 int iLimit = 0;
142860 int iOffset;
142861 int n;
142862 Expr *pLimit = p->pLimit;
142863
142864 if( p->iLimit ) return;
142865
142866 /*
142867 ** "LIMIT -1" always shows all rows. There is some
142868 ** controversy about what the correct behavior should be.
142869 ** The current implementation interprets "LIMIT 0" to mean
142870 ** no rows.
142871 */
142872 if( pLimit ){
142873 assert( pLimit->op==TK_LIMIT );
142874 assert( pLimit->pLeft!=0 );
142875 p->iLimit = iLimit = ++pParse->nMem;
142876 v = sqlite3GetVdbe(pParse);
142877 assert( v!=0 );
142878 if( sqlite3ExprIsInteger(p: pLimit->pLeft, pValue: &n) ){
142879 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: n, p2: iLimit);
142880 VdbeComment((v, "LIMIT counter"));
142881 if( n==0 ){
142882 sqlite3VdbeGoto(p: v, iDest: iBreak);
142883 }else if( n>=0 && p->nSelectRow>sqlite3LogEst(x: (u64)n) ){
142884 p->nSelectRow = sqlite3LogEst(x: (u64)n);
142885 p->selFlags |= SF_FixedLimit;
142886 }
142887 }else{
142888 sqlite3ExprCode(pParse, pExpr: pLimit->pLeft, target: iLimit);
142889 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: iLimit); VdbeCoverage(v);
142890 VdbeComment((v, "LIMIT counter"));
142891 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: iLimit, p2: iBreak); VdbeCoverage(v);
142892 }
142893 if( pLimit->pRight ){
142894 p->iOffset = iOffset = ++pParse->nMem;
142895 pParse->nMem++; /* Allocate an extra register for limit+offset */
142896 sqlite3ExprCode(pParse, pExpr: pLimit->pRight, target: iOffset);
142897 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: iOffset); VdbeCoverage(v);
142898 VdbeComment((v, "OFFSET counter"));
142899 sqlite3VdbeAddOp3(p: v, OP_OffsetLimit, p1: iLimit, p2: iOffset+1, p3: iOffset);
142900 VdbeComment((v, "LIMIT+OFFSET"));
142901 }
142902 }
142903}
142904
142905#ifndef SQLITE_OMIT_COMPOUND_SELECT
142906/*
142907** Return the appropriate collating sequence for the iCol-th column of
142908** the result set for the compound-select statement "p". Return NULL if
142909** the column has no default collating sequence.
142910**
142911** The collating sequence for the compound select is taken from the
142912** left-most term of the select that has a collating sequence.
142913*/
142914static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
142915 CollSeq *pRet;
142916 if( p->pPrior ){
142917 pRet = multiSelectCollSeq(pParse, p: p->pPrior, iCol);
142918 }else{
142919 pRet = 0;
142920 }
142921 assert( iCol>=0 );
142922 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
142923 ** have been thrown during name resolution and we would not have gotten
142924 ** this far */
142925 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
142926 pRet = sqlite3ExprCollSeq(pParse, pExpr: p->pEList->a[iCol].pExpr);
142927 }
142928 return pRet;
142929}
142930
142931/*
142932** The select statement passed as the second parameter is a compound SELECT
142933** with an ORDER BY clause. This function allocates and returns a KeyInfo
142934** structure suitable for implementing the ORDER BY.
142935**
142936** Space to hold the KeyInfo structure is obtained from malloc. The calling
142937** function is responsible for ensuring that this structure is eventually
142938** freed.
142939*/
142940static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
142941 ExprList *pOrderBy = p->pOrderBy;
142942 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
142943 sqlite3 *db = pParse->db;
142944 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, N: nOrderBy+nExtra, X: 1);
142945 if( pRet ){
142946 int i;
142947 for(i=0; i<nOrderBy; i++){
142948 struct ExprList_item *pItem = &pOrderBy->a[i];
142949 Expr *pTerm = pItem->pExpr;
142950 CollSeq *pColl;
142951
142952 if( pTerm->flags & EP_Collate ){
142953 pColl = sqlite3ExprCollSeq(pParse, pExpr: pTerm);
142954 }else{
142955 pColl = multiSelectCollSeq(pParse, p, iCol: pItem->u.x.iOrderByCol-1);
142956 if( pColl==0 ) pColl = db->pDfltColl;
142957 pOrderBy->a[i].pExpr =
142958 sqlite3ExprAddCollateString(pParse, pExpr: pTerm, zC: pColl->zName);
142959 }
142960 assert( sqlite3KeyInfoIsWriteable(pRet) );
142961 pRet->aColl[i] = pColl;
142962 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
142963 }
142964 }
142965
142966 return pRet;
142967}
142968
142969#ifndef SQLITE_OMIT_CTE
142970/*
142971** This routine generates VDBE code to compute the content of a WITH RECURSIVE
142972** query of the form:
142973**
142974** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
142975** \___________/ \_______________/
142976** p->pPrior p
142977**
142978**
142979** There is exactly one reference to the recursive-table in the FROM clause
142980** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
142981**
142982** The setup-query runs once to generate an initial set of rows that go
142983** into a Queue table. Rows are extracted from the Queue table one by
142984** one. Each row extracted from Queue is output to pDest. Then the single
142985** extracted row (now in the iCurrent table) becomes the content of the
142986** recursive-table for a recursive-query run. The output of the recursive-query
142987** is added back into the Queue table. Then another row is extracted from Queue
142988** and the iteration continues until the Queue table is empty.
142989**
142990** If the compound query operator is UNION then no duplicate rows are ever
142991** inserted into the Queue table. The iDistinct table keeps a copy of all rows
142992** that have ever been inserted into Queue and causes duplicates to be
142993** discarded. If the operator is UNION ALL, then duplicates are allowed.
142994**
142995** If the query has an ORDER BY, then entries in the Queue table are kept in
142996** ORDER BY order and the first entry is extracted for each cycle. Without
142997** an ORDER BY, the Queue table is just a FIFO.
142998**
142999** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
143000** have been output to pDest. A LIMIT of zero means to output no rows and a
143001** negative LIMIT means to output all rows. If there is also an OFFSET clause
143002** with a positive value, then the first OFFSET outputs are discarded rather
143003** than being sent to pDest. The LIMIT count does not begin until after OFFSET
143004** rows have been skipped.
143005*/
143006static void generateWithRecursiveQuery(
143007 Parse *pParse, /* Parsing context */
143008 Select *p, /* The recursive SELECT to be coded */
143009 SelectDest *pDest /* What to do with query results */
143010){
143011 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
143012 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
143013 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
143014 Select *pSetup; /* The setup query */
143015 Select *pFirstRec; /* Left-most recursive term */
143016 int addrTop; /* Top of the loop */
143017 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
143018 int iCurrent = 0; /* The Current table */
143019 int regCurrent; /* Register holding Current table */
143020 int iQueue; /* The Queue table */
143021 int iDistinct = 0; /* To ensure unique results if UNION */
143022 int eDest = SRT_Fifo; /* How to write to Queue */
143023 SelectDest destQueue; /* SelectDest targeting the Queue table */
143024 int i; /* Loop counter */
143025 int rc; /* Result code */
143026 ExprList *pOrderBy; /* The ORDER BY clause */
143027 Expr *pLimit; /* Saved LIMIT and OFFSET */
143028 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
143029
143030#ifndef SQLITE_OMIT_WINDOWFUNC
143031 if( p->pWin ){
143032 sqlite3ErrorMsg(pParse, zFormat: "cannot use window functions in recursive queries");
143033 return;
143034 }
143035#endif
143036
143037 /* Obtain authorization to do a recursive query */
143038 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, zArg1: 0, zArg2: 0, zArg3: 0) ) return;
143039
143040 /* Process the LIMIT and OFFSET clauses, if they exist */
143041 addrBreak = sqlite3VdbeMakeLabel(pParse);
143042 p->nSelectRow = 320; /* 4 billion rows */
143043 computeLimitRegisters(pParse, p, iBreak: addrBreak);
143044 pLimit = p->pLimit;
143045 regLimit = p->iLimit;
143046 regOffset = p->iOffset;
143047 p->pLimit = 0;
143048 p->iLimit = p->iOffset = 0;
143049 pOrderBy = p->pOrderBy;
143050
143051 /* Locate the cursor number of the Current table */
143052 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
143053 if( pSrc->a[i].fg.isRecursive ){
143054 iCurrent = pSrc->a[i].iCursor;
143055 break;
143056 }
143057 }
143058
143059 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
143060 ** the Distinct table must be exactly one greater than Queue in order
143061 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
143062 iQueue = pParse->nTab++;
143063 if( p->op==TK_UNION ){
143064 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
143065 iDistinct = pParse->nTab++;
143066 }else{
143067 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
143068 }
143069 sqlite3SelectDestInit(pDest: &destQueue, eDest, iParm: iQueue);
143070
143071 /* Allocate cursors for Current, Queue, and Distinct. */
143072 regCurrent = ++pParse->nMem;
143073 sqlite3VdbeAddOp3(p: v, OP_OpenPseudo, p1: iCurrent, p2: regCurrent, p3: nCol);
143074 if( pOrderBy ){
143075 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, nExtra: 1);
143076 sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral, p1: iQueue, p2: pOrderBy->nExpr+2, p3: 0,
143077 zP4: (char*)pKeyInfo, P4_KEYINFO);
143078 destQueue.pOrderBy = pOrderBy;
143079 }else{
143080 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iQueue, p2: nCol);
143081 }
143082 VdbeComment((v, "Queue table"));
143083 if( iDistinct ){
143084 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iDistinct, p2: 0);
143085 p->selFlags |= SF_UsesEphemeral;
143086 }
143087
143088 /* Detach the ORDER BY clause from the compound SELECT */
143089 p->pOrderBy = 0;
143090
143091 /* Figure out how many elements of the compound SELECT are part of the
143092 ** recursive query. Make sure no recursive elements use aggregate
143093 ** functions. Mark the recursive elements as UNION ALL even if they
143094 ** are really UNION because the distinctness will be enforced by the
143095 ** iDistinct table. pFirstRec is left pointing to the left-most
143096 ** recursive term of the CTE.
143097 */
143098 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
143099 if( pFirstRec->selFlags & SF_Aggregate ){
143100 sqlite3ErrorMsg(pParse, zFormat: "recursive aggregate queries not supported");
143101 goto end_of_recursive_query;
143102 }
143103 pFirstRec->op = TK_ALL;
143104 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
143105 }
143106
143107 /* Store the results of the setup-query in Queue. */
143108 pSetup = pFirstRec->pPrior;
143109 pSetup->pNext = 0;
143110 ExplainQueryPlan((pParse, 1, "SETUP"));
143111 rc = sqlite3Select(pParse, pSetup, &destQueue);
143112 pSetup->pNext = p;
143113 if( rc ) goto end_of_recursive_query;
143114
143115 /* Find the next row in the Queue and output that row */
143116 addrTop = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iQueue, p2: addrBreak); VdbeCoverage(v);
143117
143118 /* Transfer the next row in Queue over to Current */
143119 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: iCurrent); /* To reset column cache */
143120 if( pOrderBy ){
143121 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iQueue, p2: pOrderBy->nExpr+1, p3: regCurrent);
143122 }else{
143123 sqlite3VdbeAddOp2(p: v, OP_RowData, p1: iQueue, p2: regCurrent);
143124 }
143125 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: iQueue);
143126
143127 /* Output the single row in Current */
143128 addrCont = sqlite3VdbeMakeLabel(pParse);
143129 codeOffset(v, iOffset: regOffset, iContinue: addrCont);
143130 selectInnerLoop(pParse, p, srcTab: iCurrent,
143131 pSort: 0, pDistinct: 0, pDest, iContinue: addrCont, iBreak: addrBreak);
143132 if( regLimit ){
143133 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: regLimit, p2: addrBreak);
143134 VdbeCoverage(v);
143135 }
143136 sqlite3VdbeResolveLabel(v, x: addrCont);
143137
143138 /* Execute the recursive SELECT taking the single row in Current as
143139 ** the value for the recursive-table. Store the results in the Queue.
143140 */
143141 pFirstRec->pPrior = 0;
143142 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
143143 sqlite3Select(pParse, p, &destQueue);
143144 assert( pFirstRec->pPrior==0 );
143145 pFirstRec->pPrior = pSetup;
143146
143147 /* Keep running the loop until the Queue is empty */
143148 sqlite3VdbeGoto(p: v, iDest: addrTop);
143149 sqlite3VdbeResolveLabel(v, x: addrBreak);
143150
143151end_of_recursive_query:
143152 sqlite3ExprListDelete(db: pParse->db, pList: p->pOrderBy);
143153 p->pOrderBy = pOrderBy;
143154 p->pLimit = pLimit;
143155 return;
143156}
143157#endif /* SQLITE_OMIT_CTE */
143158
143159/* Forward references */
143160static int multiSelectOrderBy(
143161 Parse *pParse, /* Parsing context */
143162 Select *p, /* The right-most of SELECTs to be coded */
143163 SelectDest *pDest /* What to do with query results */
143164);
143165
143166/*
143167** Handle the special case of a compound-select that originates from a
143168** VALUES clause. By handling this as a special case, we avoid deep
143169** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
143170** on a VALUES clause.
143171**
143172** Because the Select object originates from a VALUES clause:
143173** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
143174** (2) All terms are UNION ALL
143175** (3) There is no ORDER BY clause
143176**
143177** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
143178** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
143179** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
143180** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
143181*/
143182static int multiSelectValues(
143183 Parse *pParse, /* Parsing context */
143184 Select *p, /* The right-most of SELECTs to be coded */
143185 SelectDest *pDest /* What to do with query results */
143186){
143187 int nRow = 1;
143188 int rc = 0;
143189 int bShowAll = p->pLimit==0;
143190 assert( p->selFlags & SF_MultiValue );
143191 do{
143192 assert( p->selFlags & SF_Values );
143193 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
143194 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
143195#ifndef SQLITE_OMIT_WINDOWFUNC
143196 if( p->pWin ) return -1;
143197#endif
143198 if( p->pPrior==0 ) break;
143199 assert( p->pPrior->pNext==p );
143200 p = p->pPrior;
143201 nRow += bShowAll;
143202 }while(1);
143203 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
143204 nRow==1 ? "" : "S"));
143205 while( p ){
143206 selectInnerLoop(pParse, p, srcTab: -1, pSort: 0, pDistinct: 0, pDest, iContinue: 1, iBreak: 1);
143207 if( !bShowAll ) break;
143208 p->nSelectRow = nRow;
143209 p = p->pNext;
143210 }
143211 return rc;
143212}
143213
143214/*
143215** Return true if the SELECT statement which is known to be the recursive
143216** part of a recursive CTE still has its anchor terms attached. If the
143217** anchor terms have already been removed, then return false.
143218*/
143219static int hasAnchor(Select *p){
143220 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
143221 return p!=0;
143222}
143223
143224/*
143225** This routine is called to process a compound query form from
143226** two or more separate queries using UNION, UNION ALL, EXCEPT, or
143227** INTERSECT
143228**
143229** "p" points to the right-most of the two queries. the query on the
143230** left is p->pPrior. The left query could also be a compound query
143231** in which case this routine will be called recursively.
143232**
143233** The results of the total query are to be written into a destination
143234** of type eDest with parameter iParm.
143235**
143236** Example 1: Consider a three-way compound SQL statement.
143237**
143238** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
143239**
143240** This statement is parsed up as follows:
143241**
143242** SELECT c FROM t3
143243** |
143244** `-----> SELECT b FROM t2
143245** |
143246** `------> SELECT a FROM t1
143247**
143248** The arrows in the diagram above represent the Select.pPrior pointer.
143249** So if this routine is called with p equal to the t3 query, then
143250** pPrior will be the t2 query. p->op will be TK_UNION in this case.
143251**
143252** Notice that because of the way SQLite parses compound SELECTs, the
143253** individual selects always group from left to right.
143254*/
143255static int multiSelect(
143256 Parse *pParse, /* Parsing context */
143257 Select *p, /* The right-most of SELECTs to be coded */
143258 SelectDest *pDest /* What to do with query results */
143259){
143260 int rc = SQLITE_OK; /* Success code from a subroutine */
143261 Select *pPrior; /* Another SELECT immediately to our left */
143262 Vdbe *v; /* Generate code to this VDBE */
143263 SelectDest dest; /* Alternative data destination */
143264 Select *pDelete = 0; /* Chain of simple selects to delete */
143265 sqlite3 *db; /* Database connection */
143266
143267 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
143268 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
143269 */
143270 assert( p && p->pPrior ); /* Calling function guarantees this much */
143271 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
143272 assert( p->selFlags & SF_Compound );
143273 db = pParse->db;
143274 pPrior = p->pPrior;
143275 dest = *pDest;
143276 assert( pPrior->pOrderBy==0 );
143277 assert( pPrior->pLimit==0 );
143278
143279 v = sqlite3GetVdbe(pParse);
143280 assert( v!=0 ); /* The VDBE already created by calling function */
143281
143282 /* Create the destination temporary table if necessary
143283 */
143284 if( dest.eDest==SRT_EphemTab ){
143285 assert( p->pEList );
143286 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: dest.iSDParm, p2: p->pEList->nExpr);
143287 dest.eDest = SRT_Table;
143288 }
143289
143290 /* Special handling for a compound-select that originates as a VALUES clause.
143291 */
143292 if( p->selFlags & SF_MultiValue ){
143293 rc = multiSelectValues(pParse, p, pDest: &dest);
143294 if( rc>=0 ) goto multi_select_end;
143295 rc = SQLITE_OK;
143296 }
143297
143298 /* Make sure all SELECTs in the statement have the same number of elements
143299 ** in their result sets.
143300 */
143301 assert( p->pEList && pPrior->pEList );
143302 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
143303
143304#ifndef SQLITE_OMIT_CTE
143305 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
143306 generateWithRecursiveQuery(pParse, p, pDest: &dest);
143307 }else
143308#endif
143309
143310 /* Compound SELECTs that have an ORDER BY clause are handled separately.
143311 */
143312 if( p->pOrderBy ){
143313 return multiSelectOrderBy(pParse, p, pDest);
143314 }else{
143315
143316#ifndef SQLITE_OMIT_EXPLAIN
143317 if( pPrior->pPrior==0 ){
143318 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
143319 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
143320 }
143321#endif
143322
143323 /* Generate code for the left and right SELECT statements.
143324 */
143325 switch( p->op ){
143326 case TK_ALL: {
143327 int addr = 0;
143328 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
143329 assert( !pPrior->pLimit );
143330 pPrior->iLimit = p->iLimit;
143331 pPrior->iOffset = p->iOffset;
143332 pPrior->pLimit = p->pLimit;
143333 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
143334 rc = sqlite3Select(pParse, pPrior, &dest);
143335 pPrior->pLimit = 0;
143336 if( rc ){
143337 goto multi_select_end;
143338 }
143339 p->pPrior = 0;
143340 p->iLimit = pPrior->iLimit;
143341 p->iOffset = pPrior->iOffset;
143342 if( p->iLimit ){
143343 addr = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: p->iLimit); VdbeCoverage(v);
143344 VdbeComment((v, "Jump ahead if LIMIT reached"));
143345 if( p->iOffset ){
143346 sqlite3VdbeAddOp3(p: v, OP_OffsetLimit,
143347 p1: p->iLimit, p2: p->iOffset+1, p3: p->iOffset);
143348 }
143349 }
143350 ExplainQueryPlan((pParse, 1, "UNION ALL"));
143351 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
143352 rc = sqlite3Select(pParse, p, &dest);
143353 testcase( rc!=SQLITE_OK );
143354 pDelete = p->pPrior;
143355 p->pPrior = pPrior;
143356 p->nSelectRow = sqlite3LogEstAdd(a: p->nSelectRow, b: pPrior->nSelectRow);
143357 if( p->pLimit
143358 && sqlite3ExprIsInteger(p: p->pLimit->pLeft, pValue: &nLimit)
143359 && nLimit>0 && p->nSelectRow > sqlite3LogEst(x: (u64)nLimit)
143360 ){
143361 p->nSelectRow = sqlite3LogEst(x: (u64)nLimit);
143362 }
143363 if( addr ){
143364 sqlite3VdbeJumpHere(p: v, addr);
143365 }
143366 break;
143367 }
143368 case TK_EXCEPT:
143369 case TK_UNION: {
143370 int unionTab; /* Cursor number of the temp table holding result */
143371 u8 op = 0; /* One of the SRT_ operations to apply to self */
143372 int priorOp; /* The SRT_ operation to apply to prior selects */
143373 Expr *pLimit; /* Saved values of p->nLimit */
143374 int addr;
143375 SelectDest uniondest;
143376
143377 testcase( p->op==TK_EXCEPT );
143378 testcase( p->op==TK_UNION );
143379 priorOp = SRT_Union;
143380 if( dest.eDest==priorOp ){
143381 /* We can reuse a temporary table generated by a SELECT to our
143382 ** right.
143383 */
143384 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
143385 unionTab = dest.iSDParm;
143386 }else{
143387 /* We will need to create our own temporary table to hold the
143388 ** intermediate results.
143389 */
143390 unionTab = pParse->nTab++;
143391 assert( p->pOrderBy==0 );
143392 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: unionTab, p2: 0);
143393 assert( p->addrOpenEphm[0] == -1 );
143394 p->addrOpenEphm[0] = addr;
143395 findRightmost(p)->selFlags |= SF_UsesEphemeral;
143396 assert( p->pEList );
143397 }
143398
143399
143400 /* Code the SELECT statements to our left
143401 */
143402 assert( !pPrior->pOrderBy );
143403 sqlite3SelectDestInit(pDest: &uniondest, eDest: priorOp, iParm: unionTab);
143404 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
143405 rc = sqlite3Select(pParse, pPrior, &uniondest);
143406 if( rc ){
143407 goto multi_select_end;
143408 }
143409
143410 /* Code the current SELECT statement
143411 */
143412 if( p->op==TK_EXCEPT ){
143413 op = SRT_Except;
143414 }else{
143415 assert( p->op==TK_UNION );
143416 op = SRT_Union;
143417 }
143418 p->pPrior = 0;
143419 pLimit = p->pLimit;
143420 p->pLimit = 0;
143421 uniondest.eDest = op;
143422 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
143423 sqlite3SelectOpName(p->op)));
143424 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
143425 rc = sqlite3Select(pParse, p, &uniondest);
143426 testcase( rc!=SQLITE_OK );
143427 assert( p->pOrderBy==0 );
143428 pDelete = p->pPrior;
143429 p->pPrior = pPrior;
143430 p->pOrderBy = 0;
143431 if( p->op==TK_UNION ){
143432 p->nSelectRow = sqlite3LogEstAdd(a: p->nSelectRow, b: pPrior->nSelectRow);
143433 }
143434 sqlite3ExprDelete(db, p: p->pLimit);
143435 p->pLimit = pLimit;
143436 p->iLimit = 0;
143437 p->iOffset = 0;
143438
143439 /* Convert the data in the temporary table into whatever form
143440 ** it is that we currently need.
143441 */
143442 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
143443 assert( p->pEList || db->mallocFailed );
143444 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
143445 int iCont, iBreak, iStart;
143446 iBreak = sqlite3VdbeMakeLabel(pParse);
143447 iCont = sqlite3VdbeMakeLabel(pParse);
143448 computeLimitRegisters(pParse, p, iBreak);
143449 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: unionTab, p2: iBreak); VdbeCoverage(v);
143450 iStart = sqlite3VdbeCurrentAddr(p: v);
143451 selectInnerLoop(pParse, p, srcTab: unionTab,
143452 pSort: 0, pDistinct: 0, pDest: &dest, iContinue: iCont, iBreak);
143453 sqlite3VdbeResolveLabel(v, x: iCont);
143454 sqlite3VdbeAddOp2(p: v, OP_Next, p1: unionTab, p2: iStart); VdbeCoverage(v);
143455 sqlite3VdbeResolveLabel(v, x: iBreak);
143456 sqlite3VdbeAddOp2(p: v, OP_Close, p1: unionTab, p2: 0);
143457 }
143458 break;
143459 }
143460 default: assert( p->op==TK_INTERSECT ); {
143461 int tab1, tab2;
143462 int iCont, iBreak, iStart;
143463 Expr *pLimit;
143464 int addr;
143465 SelectDest intersectdest;
143466 int r1;
143467
143468 /* INTERSECT is different from the others since it requires
143469 ** two temporary tables. Hence it has its own case. Begin
143470 ** by allocating the tables we will need.
143471 */
143472 tab1 = pParse->nTab++;
143473 tab2 = pParse->nTab++;
143474 assert( p->pOrderBy==0 );
143475
143476 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: tab1, p2: 0);
143477 assert( p->addrOpenEphm[0] == -1 );
143478 p->addrOpenEphm[0] = addr;
143479 findRightmost(p)->selFlags |= SF_UsesEphemeral;
143480 assert( p->pEList );
143481
143482 /* Code the SELECTs to our left into temporary table "tab1".
143483 */
143484 sqlite3SelectDestInit(pDest: &intersectdest, SRT_Union, iParm: tab1);
143485 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
143486 rc = sqlite3Select(pParse, pPrior, &intersectdest);
143487 if( rc ){
143488 goto multi_select_end;
143489 }
143490
143491 /* Code the current SELECT into temporary table "tab2"
143492 */
143493 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: tab2, p2: 0);
143494 assert( p->addrOpenEphm[1] == -1 );
143495 p->addrOpenEphm[1] = addr;
143496 p->pPrior = 0;
143497 pLimit = p->pLimit;
143498 p->pLimit = 0;
143499 intersectdest.iSDParm = tab2;
143500 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
143501 sqlite3SelectOpName(p->op)));
143502 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
143503 rc = sqlite3Select(pParse, p, &intersectdest);
143504 testcase( rc!=SQLITE_OK );
143505 pDelete = p->pPrior;
143506 p->pPrior = pPrior;
143507 if( p->nSelectRow>pPrior->nSelectRow ){
143508 p->nSelectRow = pPrior->nSelectRow;
143509 }
143510 sqlite3ExprDelete(db, p: p->pLimit);
143511 p->pLimit = pLimit;
143512
143513 /* Generate code to take the intersection of the two temporary
143514 ** tables.
143515 */
143516 if( rc ) break;
143517 assert( p->pEList );
143518 iBreak = sqlite3VdbeMakeLabel(pParse);
143519 iCont = sqlite3VdbeMakeLabel(pParse);
143520 computeLimitRegisters(pParse, p, iBreak);
143521 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: tab1, p2: iBreak); VdbeCoverage(v);
143522 r1 = sqlite3GetTempReg(pParse);
143523 iStart = sqlite3VdbeAddOp2(p: v, OP_RowData, p1: tab1, p2: r1);
143524 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: tab2, p2: iCont, p3: r1, p4: 0);
143525 VdbeCoverage(v);
143526 sqlite3ReleaseTempReg(pParse, iReg: r1);
143527 selectInnerLoop(pParse, p, srcTab: tab1,
143528 pSort: 0, pDistinct: 0, pDest: &dest, iContinue: iCont, iBreak);
143529 sqlite3VdbeResolveLabel(v, x: iCont);
143530 sqlite3VdbeAddOp2(p: v, OP_Next, p1: tab1, p2: iStart); VdbeCoverage(v);
143531 sqlite3VdbeResolveLabel(v, x: iBreak);
143532 sqlite3VdbeAddOp2(p: v, OP_Close, p1: tab2, p2: 0);
143533 sqlite3VdbeAddOp2(p: v, OP_Close, p1: tab1, p2: 0);
143534 break;
143535 }
143536 }
143537
143538 #ifndef SQLITE_OMIT_EXPLAIN
143539 if( p->pNext==0 ){
143540 ExplainQueryPlanPop(pParse);
143541 }
143542 #endif
143543 }
143544 if( pParse->nErr ) goto multi_select_end;
143545
143546 /* Compute collating sequences used by
143547 ** temporary tables needed to implement the compound select.
143548 ** Attach the KeyInfo structure to all temporary tables.
143549 **
143550 ** This section is run by the right-most SELECT statement only.
143551 ** SELECT statements to the left always skip this part. The right-most
143552 ** SELECT might also skip this part if it has no ORDER BY clause and
143553 ** no temp tables are required.
143554 */
143555 if( p->selFlags & SF_UsesEphemeral ){
143556 int i; /* Loop counter */
143557 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
143558 Select *pLoop; /* For looping through SELECT statements */
143559 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
143560 int nCol; /* Number of columns in result set */
143561
143562 assert( p->pNext==0 );
143563 assert( p->pEList!=0 );
143564 nCol = p->pEList->nExpr;
143565 pKeyInfo = sqlite3KeyInfoAlloc(db, N: nCol, X: 1);
143566 if( !pKeyInfo ){
143567 rc = SQLITE_NOMEM_BKPT;
143568 goto multi_select_end;
143569 }
143570 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
143571 *apColl = multiSelectCollSeq(pParse, p, iCol: i);
143572 if( 0==*apColl ){
143573 *apColl = db->pDfltColl;
143574 }
143575 }
143576
143577 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
143578 for(i=0; i<2; i++){
143579 int addr = pLoop->addrOpenEphm[i];
143580 if( addr<0 ){
143581 /* If [0] is unused then [1] is also unused. So we can
143582 ** always safely abort as soon as the first unused slot is found */
143583 assert( pLoop->addrOpenEphm[1]<0 );
143584 break;
143585 }
143586 sqlite3VdbeChangeP2(p: v, addr, val: nCol);
143587 sqlite3VdbeChangeP4(p: v, addr, zP4: (char*)sqlite3KeyInfoRef(p: pKeyInfo),
143588 P4_KEYINFO);
143589 pLoop->addrOpenEphm[i] = -1;
143590 }
143591 }
143592 sqlite3KeyInfoUnref(p: pKeyInfo);
143593 }
143594
143595multi_select_end:
143596 pDest->iSdst = dest.iSdst;
143597 pDest->nSdst = dest.nSdst;
143598 if( pDelete ){
143599 sqlite3ParserAddCleanup(pParse,
143600 xCleanup: (void(*)(sqlite3*,void*))sqlite3SelectDelete,
143601 pPtr: pDelete);
143602 }
143603 return rc;
143604}
143605#endif /* SQLITE_OMIT_COMPOUND_SELECT */
143606
143607/*
143608** Error message for when two or more terms of a compound select have different
143609** size result sets.
143610*/
143611SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
143612 if( p->selFlags & SF_Values ){
143613 sqlite3ErrorMsg(pParse, zFormat: "all VALUES must have the same number of terms");
143614 }else{
143615 sqlite3ErrorMsg(pParse, zFormat: "SELECTs to the left and right of %s"
143616 " do not have the same number of result columns",
143617 sqlite3SelectOpName(id: p->op));
143618 }
143619}
143620
143621/*
143622** Code an output subroutine for a coroutine implementation of a
143623** SELECT statement.
143624**
143625** The data to be output is contained in pIn->iSdst. There are
143626** pIn->nSdst columns to be output. pDest is where the output should
143627** be sent.
143628**
143629** regReturn is the number of the register holding the subroutine
143630** return address.
143631**
143632** If regPrev>0 then it is the first register in a vector that
143633** records the previous output. mem[regPrev] is a flag that is false
143634** if there has been no previous output. If regPrev>0 then code is
143635** generated to suppress duplicates. pKeyInfo is used for comparing
143636** keys.
143637**
143638** If the LIMIT found in p->iLimit is reached, jump immediately to
143639** iBreak.
143640*/
143641static int generateOutputSubroutine(
143642 Parse *pParse, /* Parsing context */
143643 Select *p, /* The SELECT statement */
143644 SelectDest *pIn, /* Coroutine supplying data */
143645 SelectDest *pDest, /* Where to send the data */
143646 int regReturn, /* The return address register */
143647 int regPrev, /* Previous result register. No uniqueness if 0 */
143648 KeyInfo *pKeyInfo, /* For comparing with previous entry */
143649 int iBreak /* Jump here if we hit the LIMIT */
143650){
143651 Vdbe *v = pParse->pVdbe;
143652 int iContinue;
143653 int addr;
143654
143655 addr = sqlite3VdbeCurrentAddr(p: v);
143656 iContinue = sqlite3VdbeMakeLabel(pParse);
143657
143658 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
143659 */
143660 if( regPrev ){
143661 int addr1, addr2;
143662 addr1 = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: regPrev); VdbeCoverage(v);
143663 addr2 = sqlite3VdbeAddOp4(p: v, OP_Compare, p1: pIn->iSdst, p2: regPrev+1, p3: pIn->nSdst,
143664 zP4: (char*)sqlite3KeyInfoRef(p: pKeyInfo), P4_KEYINFO);
143665 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr2+2, p2: iContinue, p3: addr2+2); VdbeCoverage(v);
143666 sqlite3VdbeJumpHere(p: v, addr: addr1);
143667 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: pIn->iSdst, p2: regPrev+1, p3: pIn->nSdst-1);
143668 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: regPrev);
143669 }
143670 if( pParse->db->mallocFailed ) return 0;
143671
143672 /* Suppress the first OFFSET entries if there is an OFFSET clause
143673 */
143674 codeOffset(v, iOffset: p->iOffset, iContinue);
143675
143676 assert( pDest->eDest!=SRT_Exists );
143677 assert( pDest->eDest!=SRT_Table );
143678 switch( pDest->eDest ){
143679 /* Store the result as data using a unique key.
143680 */
143681 case SRT_EphemTab: {
143682 int r1 = sqlite3GetTempReg(pParse);
143683 int r2 = sqlite3GetTempReg(pParse);
143684 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: pIn->iSdst, p2: pIn->nSdst, p3: r1);
143685 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: pDest->iSDParm, p2: r2);
143686 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: pDest->iSDParm, p2: r1, p3: r2);
143687 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
143688 sqlite3ReleaseTempReg(pParse, iReg: r2);
143689 sqlite3ReleaseTempReg(pParse, iReg: r1);
143690 break;
143691 }
143692
143693#ifndef SQLITE_OMIT_SUBQUERY
143694 /* If we are creating a set for an "expr IN (SELECT ...)".
143695 */
143696 case SRT_Set: {
143697 int r1;
143698 testcase( pIn->nSdst>1 );
143699 r1 = sqlite3GetTempReg(pParse);
143700 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: pIn->iSdst, p2: pIn->nSdst,
143701 p3: r1, zP4: pDest->zAffSdst, p4type: pIn->nSdst);
143702 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: pDest->iSDParm, p2: r1,
143703 p3: pIn->iSdst, p4: pIn->nSdst);
143704 sqlite3ReleaseTempReg(pParse, iReg: r1);
143705 break;
143706 }
143707
143708 /* If this is a scalar select that is part of an expression, then
143709 ** store the results in the appropriate memory cell and break out
143710 ** of the scan loop. Note that the select might return multiple columns
143711 ** if it is the RHS of a row-value IN operator.
143712 */
143713 case SRT_Mem: {
143714 testcase( pIn->nSdst>1 );
143715 sqlite3ExprCodeMove(pParse, iFrom: pIn->iSdst, iTo: pDest->iSDParm, nReg: pIn->nSdst);
143716 /* The LIMIT clause will jump out of the loop for us */
143717 break;
143718 }
143719#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
143720
143721 /* The results are stored in a sequence of registers
143722 ** starting at pDest->iSdst. Then the co-routine yields.
143723 */
143724 case SRT_Coroutine: {
143725 if( pDest->iSdst==0 ){
143726 pDest->iSdst = sqlite3GetTempRange(pParse, nReg: pIn->nSdst);
143727 pDest->nSdst = pIn->nSdst;
143728 }
143729 sqlite3ExprCodeMove(pParse, iFrom: pIn->iSdst, iTo: pDest->iSdst, nReg: pIn->nSdst);
143730 sqlite3VdbeAddOp1(p: v, OP_Yield, p1: pDest->iSDParm);
143731 break;
143732 }
143733
143734 /* If none of the above, then the result destination must be
143735 ** SRT_Output. This routine is never called with any other
143736 ** destination other than the ones handled above or SRT_Output.
143737 **
143738 ** For SRT_Output, results are stored in a sequence of registers.
143739 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
143740 ** return the next row of result.
143741 */
143742 default: {
143743 assert( pDest->eDest==SRT_Output );
143744 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: pIn->iSdst, p2: pIn->nSdst);
143745 break;
143746 }
143747 }
143748
143749 /* Jump to the end of the loop if the LIMIT is reached.
143750 */
143751 if( p->iLimit ){
143752 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: p->iLimit, p2: iBreak); VdbeCoverage(v);
143753 }
143754
143755 /* Generate the subroutine return
143756 */
143757 sqlite3VdbeResolveLabel(v, x: iContinue);
143758 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regReturn);
143759
143760 return addr;
143761}
143762
143763/*
143764** Alternative compound select code generator for cases when there
143765** is an ORDER BY clause.
143766**
143767** We assume a query of the following form:
143768**
143769** <selectA> <operator> <selectB> ORDER BY <orderbylist>
143770**
143771** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
143772** is to code both <selectA> and <selectB> with the ORDER BY clause as
143773** co-routines. Then run the co-routines in parallel and merge the results
143774** into the output. In addition to the two coroutines (called selectA and
143775** selectB) there are 7 subroutines:
143776**
143777** outA: Move the output of the selectA coroutine into the output
143778** of the compound query.
143779**
143780** outB: Move the output of the selectB coroutine into the output
143781** of the compound query. (Only generated for UNION and
143782** UNION ALL. EXCEPT and INSERTSECT never output a row that
143783** appears only in B.)
143784**
143785** AltB: Called when there is data from both coroutines and A<B.
143786**
143787** AeqB: Called when there is data from both coroutines and A==B.
143788**
143789** AgtB: Called when there is data from both coroutines and A>B.
143790**
143791** EofA: Called when data is exhausted from selectA.
143792**
143793** EofB: Called when data is exhausted from selectB.
143794**
143795** The implementation of the latter five subroutines depend on which
143796** <operator> is used:
143797**
143798**
143799** UNION ALL UNION EXCEPT INTERSECT
143800** ------------- ----------------- -------------- -----------------
143801** AltB: outA, nextA outA, nextA outA, nextA nextA
143802**
143803** AeqB: outA, nextA nextA nextA outA, nextA
143804**
143805** AgtB: outB, nextB outB, nextB nextB nextB
143806**
143807** EofA: outB, nextB outB, nextB halt halt
143808**
143809** EofB: outA, nextA outA, nextA outA, nextA halt
143810**
143811** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
143812** causes an immediate jump to EofA and an EOF on B following nextB causes
143813** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
143814** following nextX causes a jump to the end of the select processing.
143815**
143816** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
143817** within the output subroutine. The regPrev register set holds the previously
143818** output value. A comparison is made against this value and the output
143819** is skipped if the next results would be the same as the previous.
143820**
143821** The implementation plan is to implement the two coroutines and seven
143822** subroutines first, then put the control logic at the bottom. Like this:
143823**
143824** goto Init
143825** coA: coroutine for left query (A)
143826** coB: coroutine for right query (B)
143827** outA: output one row of A
143828** outB: output one row of B (UNION and UNION ALL only)
143829** EofA: ...
143830** EofB: ...
143831** AltB: ...
143832** AeqB: ...
143833** AgtB: ...
143834** Init: initialize coroutine registers
143835** yield coA
143836** if eof(A) goto EofA
143837** yield coB
143838** if eof(B) goto EofB
143839** Cmpr: Compare A, B
143840** Jump AltB, AeqB, AgtB
143841** End: ...
143842**
143843** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
143844** actually called using Gosub and they do not Return. EofA and EofB loop
143845** until all data is exhausted then jump to the "end" label. AltB, AeqB,
143846** and AgtB jump to either L2 or to one of EofA or EofB.
143847*/
143848#ifndef SQLITE_OMIT_COMPOUND_SELECT
143849static int multiSelectOrderBy(
143850 Parse *pParse, /* Parsing context */
143851 Select *p, /* The right-most of SELECTs to be coded */
143852 SelectDest *pDest /* What to do with query results */
143853){
143854 int i, j; /* Loop counters */
143855 Select *pPrior; /* Another SELECT immediately to our left */
143856 Select *pSplit; /* Left-most SELECT in the right-hand group */
143857 int nSelect; /* Number of SELECT statements in the compound */
143858 Vdbe *v; /* Generate code to this VDBE */
143859 SelectDest destA; /* Destination for coroutine A */
143860 SelectDest destB; /* Destination for coroutine B */
143861 int regAddrA; /* Address register for select-A coroutine */
143862 int regAddrB; /* Address register for select-B coroutine */
143863 int addrSelectA; /* Address of the select-A coroutine */
143864 int addrSelectB; /* Address of the select-B coroutine */
143865 int regOutA; /* Address register for the output-A subroutine */
143866 int regOutB; /* Address register for the output-B subroutine */
143867 int addrOutA; /* Address of the output-A subroutine */
143868 int addrOutB = 0; /* Address of the output-B subroutine */
143869 int addrEofA; /* Address of the select-A-exhausted subroutine */
143870 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
143871 int addrEofB; /* Address of the select-B-exhausted subroutine */
143872 int addrAltB; /* Address of the A<B subroutine */
143873 int addrAeqB; /* Address of the A==B subroutine */
143874 int addrAgtB; /* Address of the A>B subroutine */
143875 int regLimitA; /* Limit register for select-A */
143876 int regLimitB; /* Limit register for select-A */
143877 int regPrev; /* A range of registers to hold previous output */
143878 int savedLimit; /* Saved value of p->iLimit */
143879 int savedOffset; /* Saved value of p->iOffset */
143880 int labelCmpr; /* Label for the start of the merge algorithm */
143881 int labelEnd; /* Label for the end of the overall SELECT stmt */
143882 int addr1; /* Jump instructions that get retargeted */
143883 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
143884 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
143885 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
143886 sqlite3 *db; /* Database connection */
143887 ExprList *pOrderBy; /* The ORDER BY clause */
143888 int nOrderBy; /* Number of terms in the ORDER BY clause */
143889 u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */
143890
143891 assert( p->pOrderBy!=0 );
143892 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
143893 db = pParse->db;
143894 v = pParse->pVdbe;
143895 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
143896 labelEnd = sqlite3VdbeMakeLabel(pParse);
143897 labelCmpr = sqlite3VdbeMakeLabel(pParse);
143898
143899
143900 /* Patch up the ORDER BY clause
143901 */
143902 op = p->op;
143903 assert( p->pPrior->pOrderBy==0 );
143904 pOrderBy = p->pOrderBy;
143905 assert( pOrderBy );
143906 nOrderBy = pOrderBy->nExpr;
143907
143908 /* For operators other than UNION ALL we have to make sure that
143909 ** the ORDER BY clause covers every term of the result set. Add
143910 ** terms to the ORDER BY clause as necessary.
143911 */
143912 if( op!=TK_ALL ){
143913 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
143914 struct ExprList_item *pItem;
143915 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
143916 assert( pItem!=0 );
143917 assert( pItem->u.x.iOrderByCol>0 );
143918 if( pItem->u.x.iOrderByCol==i ) break;
143919 }
143920 if( j==nOrderBy ){
143921 Expr *pNew = sqlite3Expr(db, TK_INTEGER, zToken: 0);
143922 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
143923 pNew->flags |= EP_IntValue;
143924 pNew->u.iValue = i;
143925 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pList: pOrderBy, pExpr: pNew);
143926 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
143927 }
143928 }
143929 }
143930
143931 /* Compute the comparison permutation and keyinfo that is used with
143932 ** the permutation used to determine if the next
143933 ** row of results comes from selectA or selectB. Also add explicit
143934 ** collations to the ORDER BY clause terms so that when the subqueries
143935 ** to the right and the left are evaluated, they use the correct
143936 ** collation.
143937 */
143938 aPermute = sqlite3DbMallocRawNN(db, n: sizeof(u32)*(nOrderBy + 1));
143939 if( aPermute ){
143940 struct ExprList_item *pItem;
143941 aPermute[0] = nOrderBy;
143942 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
143943 assert( pItem!=0 );
143944 assert( pItem->u.x.iOrderByCol>0 );
143945 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
143946 aPermute[i] = pItem->u.x.iOrderByCol - 1;
143947 }
143948 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, nExtra: 1);
143949 }else{
143950 pKeyMerge = 0;
143951 }
143952
143953 /* Allocate a range of temporary registers and the KeyInfo needed
143954 ** for the logic that removes duplicate result rows when the
143955 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
143956 */
143957 if( op==TK_ALL ){
143958 regPrev = 0;
143959 }else{
143960 int nExpr = p->pEList->nExpr;
143961 assert( nOrderBy>=nExpr || db->mallocFailed );
143962 regPrev = pParse->nMem+1;
143963 pParse->nMem += nExpr+1;
143964 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regPrev);
143965 pKeyDup = sqlite3KeyInfoAlloc(db, N: nExpr, X: 1);
143966 if( pKeyDup ){
143967 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
143968 for(i=0; i<nExpr; i++){
143969 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, iCol: i);
143970 pKeyDup->aSortFlags[i] = 0;
143971 }
143972 }
143973 }
143974
143975 /* Separate the left and the right query from one another
143976 */
143977 nSelect = 1;
143978 if( (op==TK_ALL || op==TK_UNION)
143979 && OptimizationEnabled(db, SQLITE_BalancedMerge)
143980 ){
143981 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
143982 nSelect++;
143983 assert( pSplit->pPrior->pNext==pSplit );
143984 }
143985 }
143986 if( nSelect<=3 ){
143987 pSplit = p;
143988 }else{
143989 pSplit = p;
143990 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
143991 }
143992 pPrior = pSplit->pPrior;
143993 assert( pPrior!=0 );
143994 pSplit->pPrior = 0;
143995 pPrior->pNext = 0;
143996 assert( p->pOrderBy == pOrderBy );
143997 assert( pOrderBy!=0 || db->mallocFailed );
143998 pPrior->pOrderBy = sqlite3ExprListDup(db: pParse->db, p: pOrderBy, flags: 0);
143999 sqlite3ResolveOrderGroupBy(pParse, pSelect: p, pOrderBy: p->pOrderBy, zType: "ORDER");
144000 sqlite3ResolveOrderGroupBy(pParse, pSelect: pPrior, pOrderBy: pPrior->pOrderBy, zType: "ORDER");
144001
144002 /* Compute the limit registers */
144003 computeLimitRegisters(pParse, p, iBreak: labelEnd);
144004 if( p->iLimit && op==TK_ALL ){
144005 regLimitA = ++pParse->nMem;
144006 regLimitB = ++pParse->nMem;
144007 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: p->iOffset ? p->iOffset+1 : p->iLimit,
144008 p2: regLimitA);
144009 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regLimitA, p2: regLimitB);
144010 }else{
144011 regLimitA = regLimitB = 0;
144012 }
144013 sqlite3ExprDelete(db, p: p->pLimit);
144014 p->pLimit = 0;
144015
144016 regAddrA = ++pParse->nMem;
144017 regAddrB = ++pParse->nMem;
144018 regOutA = ++pParse->nMem;
144019 regOutB = ++pParse->nMem;
144020 sqlite3SelectDestInit(pDest: &destA, SRT_Coroutine, iParm: regAddrA);
144021 sqlite3SelectDestInit(pDest: &destB, SRT_Coroutine, iParm: regAddrB);
144022
144023 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
144024
144025 /* Generate a coroutine to evaluate the SELECT statement to the
144026 ** left of the compound operator - the "A" select.
144027 */
144028 addrSelectA = sqlite3VdbeCurrentAddr(p: v) + 1;
144029 addr1 = sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regAddrA, p2: 0, p3: addrSelectA);
144030 VdbeComment((v, "left SELECT"));
144031 pPrior->iLimit = regLimitA;
144032 ExplainQueryPlan((pParse, 1, "LEFT"));
144033 sqlite3Select(pParse, pPrior, &destA);
144034 sqlite3VdbeEndCoroutine(v, regYield: regAddrA);
144035 sqlite3VdbeJumpHere(p: v, addr: addr1);
144036
144037 /* Generate a coroutine to evaluate the SELECT statement on
144038 ** the right - the "B" select
144039 */
144040 addrSelectB = sqlite3VdbeCurrentAddr(p: v) + 1;
144041 addr1 = sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regAddrB, p2: 0, p3: addrSelectB);
144042 VdbeComment((v, "right SELECT"));
144043 savedLimit = p->iLimit;
144044 savedOffset = p->iOffset;
144045 p->iLimit = regLimitB;
144046 p->iOffset = 0;
144047 ExplainQueryPlan((pParse, 1, "RIGHT"));
144048 sqlite3Select(pParse, p, &destB);
144049 p->iLimit = savedLimit;
144050 p->iOffset = savedOffset;
144051 sqlite3VdbeEndCoroutine(v, regYield: regAddrB);
144052
144053 /* Generate a subroutine that outputs the current row of the A
144054 ** select as the next output row of the compound select.
144055 */
144056 VdbeNoopComment((v, "Output routine for A"));
144057 addrOutA = generateOutputSubroutine(pParse,
144058 p, pIn: &destA, pDest, regReturn: regOutA,
144059 regPrev, pKeyInfo: pKeyDup, iBreak: labelEnd);
144060
144061 /* Generate a subroutine that outputs the current row of the B
144062 ** select as the next output row of the compound select.
144063 */
144064 if( op==TK_ALL || op==TK_UNION ){
144065 VdbeNoopComment((v, "Output routine for B"));
144066 addrOutB = generateOutputSubroutine(pParse,
144067 p, pIn: &destB, pDest, regReturn: regOutB,
144068 regPrev, pKeyInfo: pKeyDup, iBreak: labelEnd);
144069 }
144070 sqlite3KeyInfoUnref(p: pKeyDup);
144071
144072 /* Generate a subroutine to run when the results from select A
144073 ** are exhausted and only data in select B remains.
144074 */
144075 if( op==TK_EXCEPT || op==TK_INTERSECT ){
144076 addrEofA_noB = addrEofA = labelEnd;
144077 }else{
144078 VdbeNoopComment((v, "eof-A subroutine"));
144079 addrEofA = sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutB, p2: addrOutB);
144080 addrEofA_noB = sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrB, p2: labelEnd);
144081 VdbeCoverage(v);
144082 sqlite3VdbeGoto(p: v, iDest: addrEofA);
144083 p->nSelectRow = sqlite3LogEstAdd(a: p->nSelectRow, b: pPrior->nSelectRow);
144084 }
144085
144086 /* Generate a subroutine to run when the results from select B
144087 ** are exhausted and only data in select A remains.
144088 */
144089 if( op==TK_INTERSECT ){
144090 addrEofB = addrEofA;
144091 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
144092 }else{
144093 VdbeNoopComment((v, "eof-B subroutine"));
144094 addrEofB = sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutA, p2: addrOutA);
144095 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: labelEnd); VdbeCoverage(v);
144096 sqlite3VdbeGoto(p: v, iDest: addrEofB);
144097 }
144098
144099 /* Generate code to handle the case of A<B
144100 */
144101 VdbeNoopComment((v, "A-lt-B subroutine"));
144102 addrAltB = sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutA, p2: addrOutA);
144103 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: addrEofA); VdbeCoverage(v);
144104 sqlite3VdbeGoto(p: v, iDest: labelCmpr);
144105
144106 /* Generate code to handle the case of A==B
144107 */
144108 if( op==TK_ALL ){
144109 addrAeqB = addrAltB;
144110 }else if( op==TK_INTERSECT ){
144111 addrAeqB = addrAltB;
144112 addrAltB++;
144113 }else{
144114 VdbeNoopComment((v, "A-eq-B subroutine"));
144115 addrAeqB =
144116 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: addrEofA); VdbeCoverage(v);
144117 sqlite3VdbeGoto(p: v, iDest: labelCmpr);
144118 }
144119
144120 /* Generate code to handle the case of A>B
144121 */
144122 VdbeNoopComment((v, "A-gt-B subroutine"));
144123 addrAgtB = sqlite3VdbeCurrentAddr(p: v);
144124 if( op==TK_ALL || op==TK_UNION ){
144125 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutB, p2: addrOutB);
144126 }
144127 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrB, p2: addrEofB); VdbeCoverage(v);
144128 sqlite3VdbeGoto(p: v, iDest: labelCmpr);
144129
144130 /* This code runs once to initialize everything.
144131 */
144132 sqlite3VdbeJumpHere(p: v, addr: addr1);
144133 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: addrEofA_noB); VdbeCoverage(v);
144134 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrB, p2: addrEofB); VdbeCoverage(v);
144135
144136 /* Implement the main merge loop
144137 */
144138 sqlite3VdbeResolveLabel(v, x: labelCmpr);
144139 sqlite3VdbeAddOp4(p: v, OP_Permutation, p1: 0, p2: 0, p3: 0, zP4: (char*)aPermute, P4_INTARRAY);
144140 sqlite3VdbeAddOp4(p: v, OP_Compare, p1: destA.iSdst, p2: destB.iSdst, p3: nOrderBy,
144141 zP4: (char*)pKeyMerge, P4_KEYINFO);
144142 sqlite3VdbeChangeP5(p: v, OPFLAG_PERMUTE);
144143 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addrAltB, p2: addrAeqB, p3: addrAgtB); VdbeCoverage(v);
144144
144145 /* Jump to the this point in order to terminate the query.
144146 */
144147 sqlite3VdbeResolveLabel(v, x: labelEnd);
144148
144149 /* Make arrangements to free the 2nd and subsequent arms of the compound
144150 ** after the parse has finished */
144151 if( pSplit->pPrior ){
144152 sqlite3ParserAddCleanup(pParse,
144153 xCleanup: (void(*)(sqlite3*,void*))sqlite3SelectDelete, pPtr: pSplit->pPrior);
144154 }
144155 pSplit->pPrior = pPrior;
144156 pPrior->pNext = pSplit;
144157 sqlite3ExprListDelete(db, pList: pPrior->pOrderBy);
144158 pPrior->pOrderBy = 0;
144159
144160 /*** TBD: Insert subroutine calls to close cursors on incomplete
144161 **** subqueries ****/
144162 ExplainQueryPlanPop(pParse);
144163 return pParse->nErr!=0;
144164}
144165#endif
144166
144167#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
144168
144169/* An instance of the SubstContext object describes an substitution edit
144170** to be performed on a parse tree.
144171**
144172** All references to columns in table iTable are to be replaced by corresponding
144173** expressions in pEList.
144174**
144175** ## About "isOuterJoin":
144176**
144177** The isOuterJoin column indicates that the replacement will occur into a
144178** position in the parent that NULL-able due to an OUTER JOIN. Either the
144179** target slot in the parent is the right operand of a LEFT JOIN, or one of
144180** the left operands of a RIGHT JOIN. In either case, we need to potentially
144181** bypass the substituted expression with OP_IfNullRow.
144182**
144183** Suppose the original expression is an integer constant. Even though the table
144184** has the nullRow flag set, because the expression is an integer constant,
144185** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode
144186** that checks to see if the nullRow flag is set on the table. If the nullRow
144187** flag is set, then the value in the register is set to NULL and the original
144188** expression is bypassed. If the nullRow flag is not set, then the original
144189** expression runs to populate the register.
144190**
144191** Example where this is needed:
144192**
144193** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
144194** CREATE TABLE t2(x INT UNIQUE);
144195**
144196** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x;
144197**
144198** When the subquery on the right side of the LEFT JOIN is flattened, we
144199** have to add OP_IfNullRow in front of the OP_Integer that implements the
144200** "m" value of the subquery so that a NULL will be loaded instead of 59
144201** when processing a non-matched row of the left.
144202*/
144203typedef struct SubstContext {
144204 Parse *pParse; /* The parsing context */
144205 int iTable; /* Replace references to this table */
144206 int iNewTable; /* New table number */
144207 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
144208 ExprList *pEList; /* Replacement expressions */
144209 ExprList *pCList; /* Collation sequences for replacement expr */
144210} SubstContext;
144211
144212/* Forward Declarations */
144213static void substExprList(SubstContext*, ExprList*);
144214static void substSelect(SubstContext*, Select*, int);
144215
144216/*
144217** Scan through the expression pExpr. Replace every reference to
144218** a column in table number iTable with a copy of the iColumn-th
144219** entry in pEList. (But leave references to the ROWID column
144220** unchanged.)
144221**
144222** This routine is part of the flattening procedure. A subquery
144223** whose result set is defined by pEList appears as entry in the
144224** FROM clause of a SELECT such that the VDBE cursor assigned to that
144225** FORM clause entry is iTable. This routine makes the necessary
144226** changes to pExpr so that it refers directly to the source table
144227** of the subquery rather the result set of the subquery.
144228*/
144229static Expr *substExpr(
144230 SubstContext *pSubst, /* Description of the substitution */
144231 Expr *pExpr /* Expr in which substitution occurs */
144232){
144233 if( pExpr==0 ) return 0;
144234 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)
144235 && pExpr->w.iJoin==pSubst->iTable
144236 ){
144237 testcase( ExprHasProperty(pExpr, EP_InnerON) );
144238 pExpr->w.iJoin = pSubst->iNewTable;
144239 }
144240 if( pExpr->op==TK_COLUMN
144241 && pExpr->iTable==pSubst->iTable
144242 && !ExprHasProperty(pExpr, EP_FixedCol)
144243 ){
144244#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
144245 if( pExpr->iColumn<0 ){
144246 pExpr->op = TK_NULL;
144247 }else
144248#endif
144249 {
144250 Expr *pNew;
144251 int iColumn;
144252 Expr *pCopy;
144253 Expr ifNullRow;
144254 iColumn = pExpr->iColumn;
144255 assert( iColumn>=0 );
144256 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
144257 assert( pExpr->pRight==0 );
144258 pCopy = pSubst->pEList->a[iColumn].pExpr;
144259 if( sqlite3ExprIsVector(pExpr: pCopy) ){
144260 sqlite3VectorErrorMsg(pParse: pSubst->pParse, pExpr: pCopy);
144261 }else{
144262 sqlite3 *db = pSubst->pParse->db;
144263 if( pSubst->isOuterJoin
144264 && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable)
144265 ){
144266 memset(s: &ifNullRow, c: 0, n: sizeof(ifNullRow));
144267 ifNullRow.op = TK_IF_NULL_ROW;
144268 ifNullRow.pLeft = pCopy;
144269 ifNullRow.iTable = pSubst->iNewTable;
144270 ifNullRow.iColumn = -99;
144271 ifNullRow.flags = EP_IfNullRow;
144272 pCopy = &ifNullRow;
144273 }
144274 testcase( ExprHasProperty(pCopy, EP_Subquery) );
144275 pNew = sqlite3ExprDup(db, p: pCopy, flags: 0);
144276 if( db->mallocFailed ){
144277 sqlite3ExprDelete(db, p: pNew);
144278 return pExpr;
144279 }
144280 if( pSubst->isOuterJoin ){
144281 ExprSetProperty(pNew, EP_CanBeNull);
144282 }
144283 if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) ){
144284 sqlite3SetJoinExpr(p: pNew, iTable: pExpr->w.iJoin,
144285 joinFlag: pExpr->flags & (EP_OuterON|EP_InnerON));
144286 }
144287 sqlite3ExprDelete(db, p: pExpr);
144288 pExpr = pNew;
144289 if( pExpr->op==TK_TRUEFALSE ){
144290 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
144291 pExpr->op = TK_INTEGER;
144292 ExprSetProperty(pExpr, EP_IntValue);
144293 }
144294
144295 /* Ensure that the expression now has an implicit collation sequence,
144296 ** just as it did when it was a column of a view or sub-query. */
144297 {
144298 CollSeq *pNat = sqlite3ExprCollSeq(pParse: pSubst->pParse, pExpr);
144299 CollSeq *pColl = sqlite3ExprCollSeq(pParse: pSubst->pParse,
144300 pExpr: pSubst->pCList->a[iColumn].pExpr
144301 );
144302 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
144303 pExpr = sqlite3ExprAddCollateString(pParse: pSubst->pParse, pExpr,
144304 zC: (pColl ? pColl->zName : "BINARY")
144305 );
144306 }
144307 }
144308 ExprClearProperty(pExpr, EP_Collate);
144309 }
144310 }
144311 }else{
144312 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
144313 pExpr->iTable = pSubst->iNewTable;
144314 }
144315 pExpr->pLeft = substExpr(pSubst, pExpr: pExpr->pLeft);
144316 pExpr->pRight = substExpr(pSubst, pExpr: pExpr->pRight);
144317 if( ExprUseXSelect(pExpr) ){
144318 substSelect(pSubst, pExpr->x.pSelect, 1);
144319 }else{
144320 substExprList(pSubst, pExpr->x.pList);
144321 }
144322#ifndef SQLITE_OMIT_WINDOWFUNC
144323 if( ExprHasProperty(pExpr, EP_WinFunc) ){
144324 Window *pWin = pExpr->y.pWin;
144325 pWin->pFilter = substExpr(pSubst, pExpr: pWin->pFilter);
144326 substExprList(pSubst, pWin->pPartition);
144327 substExprList(pSubst, pWin->pOrderBy);
144328 }
144329#endif
144330 }
144331 return pExpr;
144332}
144333static void substExprList(
144334 SubstContext *pSubst, /* Description of the substitution */
144335 ExprList *pList /* List to scan and in which to make substitutes */
144336){
144337 int i;
144338 if( pList==0 ) return;
144339 for(i=0; i<pList->nExpr; i++){
144340 pList->a[i].pExpr = substExpr(pSubst, pExpr: pList->a[i].pExpr);
144341 }
144342}
144343static void substSelect(
144344 SubstContext *pSubst, /* Description of the substitution */
144345 Select *p, /* SELECT statement in which to make substitutions */
144346 int doPrior /* Do substitutes on p->pPrior too */
144347){
144348 SrcList *pSrc;
144349 SrcItem *pItem;
144350 int i;
144351 if( !p ) return;
144352 do{
144353 substExprList(pSubst, pList: p->pEList);
144354 substExprList(pSubst, pList: p->pGroupBy);
144355 substExprList(pSubst, pList: p->pOrderBy);
144356 p->pHaving = substExpr(pSubst, pExpr: p->pHaving);
144357 p->pWhere = substExpr(pSubst, pExpr: p->pWhere);
144358 pSrc = p->pSrc;
144359 assert( pSrc!=0 );
144360 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
144361 substSelect(pSubst, p: pItem->pSelect, doPrior: 1);
144362 if( pItem->fg.isTabFunc ){
144363 substExprList(pSubst, pList: pItem->u1.pFuncArg);
144364 }
144365 }
144366 }while( doPrior && (p = p->pPrior)!=0 );
144367}
144368#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
144369
144370#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
144371/*
144372** pSelect is a SELECT statement and pSrcItem is one item in the FROM
144373** clause of that SELECT.
144374**
144375** This routine scans the entire SELECT statement and recomputes the
144376** pSrcItem->colUsed mask.
144377*/
144378static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
144379 SrcItem *pItem;
144380 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
144381 pItem = pWalker->u.pSrcItem;
144382 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
144383 if( pExpr->iColumn<0 ) return WRC_Continue;
144384 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
144385 return WRC_Continue;
144386}
144387static void recomputeColumnsUsed(
144388 Select *pSelect, /* The complete SELECT statement */
144389 SrcItem *pSrcItem /* Which FROM clause item to recompute */
144390){
144391 Walker w;
144392 if( NEVER(pSrcItem->pTab==0) ) return;
144393 memset(s: &w, c: 0, n: sizeof(w));
144394 w.xExprCallback = recomputeColumnsUsedExpr;
144395 w.xSelectCallback = sqlite3SelectWalkNoop;
144396 w.u.pSrcItem = pSrcItem;
144397 pSrcItem->colUsed = 0;
144398 sqlite3WalkSelect(pWalker: &w, p: pSelect);
144399}
144400#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
144401
144402#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
144403/*
144404** Assign new cursor numbers to each of the items in pSrc. For each
144405** new cursor number assigned, set an entry in the aCsrMap[] array
144406** to map the old cursor number to the new:
144407**
144408** aCsrMap[iOld+1] = iNew;
144409**
144410** The array is guaranteed by the caller to be large enough for all
144411** existing cursor numbers in pSrc. aCsrMap[0] is the array size.
144412**
144413** If pSrc contains any sub-selects, call this routine recursively
144414** on the FROM clause of each such sub-select, with iExcept set to -1.
144415*/
144416static void srclistRenumberCursors(
144417 Parse *pParse, /* Parse context */
144418 int *aCsrMap, /* Array to store cursor mappings in */
144419 SrcList *pSrc, /* FROM clause to renumber */
144420 int iExcept /* FROM clause item to skip */
144421){
144422 int i;
144423 SrcItem *pItem;
144424 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
144425 if( i!=iExcept ){
144426 Select *p;
144427 assert( pItem->iCursor < aCsrMap[0] );
144428 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
144429 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
144430 }
144431 pItem->iCursor = aCsrMap[pItem->iCursor+1];
144432 for(p=pItem->pSelect; p; p=p->pPrior){
144433 srclistRenumberCursors(pParse, aCsrMap, pSrc: p->pSrc, iExcept: -1);
144434 }
144435 }
144436 }
144437}
144438
144439/*
144440** *piCursor is a cursor number. Change it if it needs to be mapped.
144441*/
144442static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){
144443 int *aCsrMap = pWalker->u.aiCol;
144444 int iCsr = *piCursor;
144445 if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){
144446 *piCursor = aCsrMap[iCsr+1];
144447 }
144448}
144449
144450/*
144451** Expression walker callback used by renumberCursors() to update
144452** Expr objects to match newly assigned cursor numbers.
144453*/
144454static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
144455 int op = pExpr->op;
144456 if( op==TK_COLUMN || op==TK_IF_NULL_ROW ){
144457 renumberCursorDoMapping(pWalker, piCursor: &pExpr->iTable);
144458 }
144459 if( ExprHasProperty(pExpr, EP_OuterON) ){
144460 renumberCursorDoMapping(pWalker, piCursor: &pExpr->w.iJoin);
144461 }
144462 return WRC_Continue;
144463}
144464
144465/*
144466** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
144467** of the SELECT statement passed as the second argument, and to each
144468** cursor in the FROM clause of any FROM clause sub-selects, recursively.
144469** Except, do not assign a new cursor number to the iExcept'th element in
144470** the FROM clause of (*p). Update all expressions and other references
144471** to refer to the new cursor numbers.
144472**
144473** Argument aCsrMap is an array that may be used for temporary working
144474** space. Two guarantees are made by the caller:
144475**
144476** * the array is larger than the largest cursor number used within the
144477** select statement passed as an argument, and
144478**
144479** * the array entries for all cursor numbers that do *not* appear in
144480** FROM clauses of the select statement as described above are
144481** initialized to zero.
144482*/
144483static void renumberCursors(
144484 Parse *pParse, /* Parse context */
144485 Select *p, /* Select to renumber cursors within */
144486 int iExcept, /* FROM clause item to skip */
144487 int *aCsrMap /* Working space */
144488){
144489 Walker w;
144490 srclistRenumberCursors(pParse, aCsrMap, pSrc: p->pSrc, iExcept);
144491 memset(s: &w, c: 0, n: sizeof(w));
144492 w.u.aiCol = aCsrMap;
144493 w.xExprCallback = renumberCursorsCb;
144494 w.xSelectCallback = sqlite3SelectWalkNoop;
144495 sqlite3WalkSelect(pWalker: &w, p);
144496}
144497#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
144498
144499/*
144500** If pSel is not part of a compound SELECT, return a pointer to its
144501** expression list. Otherwise, return a pointer to the expression list
144502** of the leftmost SELECT in the compound.
144503*/
144504static ExprList *findLeftmostExprlist(Select *pSel){
144505 while( pSel->pPrior ){
144506 pSel = pSel->pPrior;
144507 }
144508 return pSel->pEList;
144509}
144510
144511/*
144512** Return true if any of the result-set columns in the compound query
144513** have incompatible affinities on one or more arms of the compound.
144514*/
144515static int compoundHasDifferentAffinities(Select *p){
144516 int ii;
144517 ExprList *pList;
144518 assert( p!=0 );
144519 assert( p->pEList!=0 );
144520 assert( p->pPrior!=0 );
144521 pList = p->pEList;
144522 for(ii=0; ii<pList->nExpr; ii++){
144523 char aff;
144524 Select *pSub1;
144525 assert( pList->a[ii].pExpr!=0 );
144526 aff = sqlite3ExprAffinity(pExpr: pList->a[ii].pExpr);
144527 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
144528 assert( pSub1->pEList!=0 );
144529 assert( pSub1->pEList->nExpr>ii );
144530 assert( pSub1->pEList->a[ii].pExpr!=0 );
144531 if( sqlite3ExprAffinity(pExpr: pSub1->pEList->a[ii].pExpr)!=aff ){
144532 return 1;
144533 }
144534 }
144535 }
144536 return 0;
144537}
144538
144539#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
144540/*
144541** This routine attempts to flatten subqueries as a performance optimization.
144542** This routine returns 1 if it makes changes and 0 if no flattening occurs.
144543**
144544** To understand the concept of flattening, consider the following
144545** query:
144546**
144547** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
144548**
144549** The default way of implementing this query is to execute the
144550** subquery first and store the results in a temporary table, then
144551** run the outer query on that temporary table. This requires two
144552** passes over the data. Furthermore, because the temporary table
144553** has no indices, the WHERE clause on the outer query cannot be
144554** optimized.
144555**
144556** This routine attempts to rewrite queries such as the above into
144557** a single flat select, like this:
144558**
144559** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
144560**
144561** The code generated for this simplification gives the same result
144562** but only has to scan the data once. And because indices might
144563** exist on the table t1, a complete scan of the data might be
144564** avoided.
144565**
144566** Flattening is subject to the following constraints:
144567**
144568** (**) We no longer attempt to flatten aggregate subqueries. Was:
144569** The subquery and the outer query cannot both be aggregates.
144570**
144571** (**) We no longer attempt to flatten aggregate subqueries. Was:
144572** (2) If the subquery is an aggregate then
144573** (2a) the outer query must not be a join and
144574** (2b) the outer query must not use subqueries
144575** other than the one FROM-clause subquery that is a candidate
144576** for flattening. (This is due to ticket [2f7170d73bf9abf80]
144577** from 2015-02-09.)
144578**
144579** (3) If the subquery is the right operand of a LEFT JOIN then
144580** (3a) the subquery may not be a join and
144581** (3b) the FROM clause of the subquery may not contain a virtual
144582** table and
144583** (**) Was: "The outer query may not have a GROUP BY." This case
144584** is now managed correctly
144585** (3d) the outer query may not be DISTINCT.
144586** See also (26) for restrictions on RIGHT JOIN.
144587**
144588** (4) The subquery can not be DISTINCT.
144589**
144590** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
144591** sub-queries that were excluded from this optimization. Restriction
144592** (4) has since been expanded to exclude all DISTINCT subqueries.
144593**
144594** (**) We no longer attempt to flatten aggregate subqueries. Was:
144595** If the subquery is aggregate, the outer query may not be DISTINCT.
144596**
144597** (7) The subquery must have a FROM clause. TODO: For subqueries without
144598** A FROM clause, consider adding a FROM clause with the special
144599** table sqlite_once that consists of a single row containing a
144600** single NULL.
144601**
144602** (8) If the subquery uses LIMIT then the outer query may not be a join.
144603**
144604** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
144605**
144606** (**) Restriction (10) was removed from the code on 2005-02-05 but we
144607** accidentally carried the comment forward until 2014-09-15. Original
144608** constraint: "If the subquery is aggregate then the outer query
144609** may not use LIMIT."
144610**
144611** (11) The subquery and the outer query may not both have ORDER BY clauses.
144612**
144613** (**) Not implemented. Subsumed into restriction (3). Was previously
144614** a separate restriction deriving from ticket #350.
144615**
144616** (13) The subquery and outer query may not both use LIMIT.
144617**
144618** (14) The subquery may not use OFFSET.
144619**
144620** (15) If the outer query is part of a compound select, then the
144621** subquery may not use LIMIT.
144622** (See ticket #2339 and ticket [02a8e81d44]).
144623**
144624** (16) If the outer query is aggregate, then the subquery may not
144625** use ORDER BY. (Ticket #2942) This used to not matter
144626** until we introduced the group_concat() function.
144627**
144628** (17) If the subquery is a compound select, then
144629** (17a) all compound operators must be a UNION ALL, and
144630** (17b) no terms within the subquery compound may be aggregate
144631** or DISTINCT, and
144632** (17c) every term within the subquery compound must have a FROM clause
144633** (17d) the outer query may not be
144634** (17d1) aggregate, or
144635** (17d2) DISTINCT
144636** (17e) the subquery may not contain window functions, and
144637** (17f) the subquery must not be the RHS of a LEFT JOIN.
144638** (17g) either the subquery is the first element of the outer
144639** query or there are no RIGHT or FULL JOINs in any arm
144640** of the subquery. (This is a duplicate of condition (27b).)
144641** (17h) The corresponding result set expressions in all arms of the
144642** compound must have the same affinity.
144643**
144644** The parent and sub-query may contain WHERE clauses. Subject to
144645** rules (11), (13) and (14), they may also contain ORDER BY,
144646** LIMIT and OFFSET clauses. The subquery cannot use any compound
144647** operator other than UNION ALL because all the other compound
144648** operators have an implied DISTINCT which is disallowed by
144649** restriction (4).
144650**
144651** Also, each component of the sub-query must return the same number
144652** of result columns. This is actually a requirement for any compound
144653** SELECT statement, but all the code here does is make sure that no
144654** such (illegal) sub-query is flattened. The caller will detect the
144655** syntax error and return a detailed message.
144656**
144657** (18) If the sub-query is a compound select, then all terms of the
144658** ORDER BY clause of the parent must be copies of a term returned
144659** by the parent query.
144660**
144661** (19) If the subquery uses LIMIT then the outer query may not
144662** have a WHERE clause.
144663**
144664** (20) If the sub-query is a compound select, then it must not use
144665** an ORDER BY clause. Ticket #3773. We could relax this constraint
144666** somewhat by saying that the terms of the ORDER BY clause must
144667** appear as unmodified result columns in the outer query. But we
144668** have other optimizations in mind to deal with that case.
144669**
144670** (21) If the subquery uses LIMIT then the outer query may not be
144671** DISTINCT. (See ticket [752e1646fc]).
144672**
144673** (22) The subquery may not be a recursive CTE.
144674**
144675** (23) If the outer query is a recursive CTE, then the sub-query may not be
144676** a compound query. This restriction is because transforming the
144677** parent to a compound query confuses the code that handles
144678** recursive queries in multiSelect().
144679**
144680** (**) We no longer attempt to flatten aggregate subqueries. Was:
144681** The subquery may not be an aggregate that uses the built-in min() or
144682** or max() functions. (Without this restriction, a query like:
144683** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
144684** return the value X for which Y was maximal.)
144685**
144686** (25) If either the subquery or the parent query contains a window
144687** function in the select list or ORDER BY clause, flattening
144688** is not attempted.
144689**
144690** (26) The subquery may not be the right operand of a RIGHT JOIN.
144691** See also (3) for restrictions on LEFT JOIN.
144692**
144693** (27) The subquery may not contain a FULL or RIGHT JOIN unless it
144694** is the first element of the parent query. Two subcases:
144695** (27a) the subquery is not a compound query.
144696** (27b) the subquery is a compound query and the RIGHT JOIN occurs
144697** in any arm of the compound query. (See also (17g).)
144698**
144699** (28) The subquery is not a MATERIALIZED CTE. (This is handled
144700** in the caller before ever reaching this routine.)
144701**
144702**
144703** In this routine, the "p" parameter is a pointer to the outer query.
144704** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
144705** uses aggregates.
144706**
144707** If flattening is not attempted, this routine is a no-op and returns 0.
144708** If flattening is attempted this routine returns 1.
144709**
144710** All of the expression analysis must occur on both the outer query and
144711** the subquery before this routine runs.
144712*/
144713static int flattenSubquery(
144714 Parse *pParse, /* Parsing context */
144715 Select *p, /* The parent or outer SELECT statement */
144716 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
144717 int isAgg /* True if outer SELECT uses aggregate functions */
144718){
144719 const char *zSavedAuthContext = pParse->zAuthContext;
144720 Select *pParent; /* Current UNION ALL term of the other query */
144721 Select *pSub; /* The inner query or "subquery" */
144722 Select *pSub1; /* Pointer to the rightmost select in sub-query */
144723 SrcList *pSrc; /* The FROM clause of the outer query */
144724 SrcList *pSubSrc; /* The FROM clause of the subquery */
144725 int iParent; /* VDBE cursor number of the pSub result set temp table */
144726 int iNewParent = -1;/* Replacement table for iParent */
144727 int isOuterJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
144728 int i; /* Loop counter */
144729 Expr *pWhere; /* The WHERE clause */
144730 SrcItem *pSubitem; /* The subquery */
144731 sqlite3 *db = pParse->db;
144732 Walker w; /* Walker to persist agginfo data */
144733 int *aCsrMap = 0;
144734
144735 /* Check to see if flattening is permitted. Return 0 if not.
144736 */
144737 assert( p!=0 );
144738 assert( p->pPrior==0 );
144739 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
144740 pSrc = p->pSrc;
144741 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
144742 pSubitem = &pSrc->a[iFrom];
144743 iParent = pSubitem->iCursor;
144744 pSub = pSubitem->pSelect;
144745 assert( pSub!=0 );
144746
144747#ifndef SQLITE_OMIT_WINDOWFUNC
144748 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
144749#endif
144750
144751 pSubSrc = pSub->pSrc;
144752 assert( pSubSrc );
144753 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
144754 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
144755 ** because they could be computed at compile-time. But when LIMIT and OFFSET
144756 ** became arbitrary expressions, we were forced to add restrictions (13)
144757 ** and (14). */
144758 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
144759 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
144760 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
144761 return 0; /* Restriction (15) */
144762 }
144763 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
144764 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
144765 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
144766 return 0; /* Restrictions (8)(9) */
144767 }
144768 if( p->pOrderBy && pSub->pOrderBy ){
144769 return 0; /* Restriction (11) */
144770 }
144771 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
144772 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
144773 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
144774 return 0; /* Restriction (21) */
144775 }
144776 if( pSub->selFlags & (SF_Recursive) ){
144777 return 0; /* Restrictions (22) */
144778 }
144779
144780 /*
144781 ** If the subquery is the right operand of a LEFT JOIN, then the
144782 ** subquery may not be a join itself (3a). Example of why this is not
144783 ** allowed:
144784 **
144785 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
144786 **
144787 ** If we flatten the above, we would get
144788 **
144789 ** (t1 LEFT OUTER JOIN t2) JOIN t3
144790 **
144791 ** which is not at all the same thing.
144792 **
144793 ** See also tickets #306, #350, and #3300.
144794 */
144795 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
144796 if( pSubSrc->nSrc>1 /* (3a) */
144797 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
144798 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
144799 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
144800 ){
144801 return 0;
144802 }
144803 isOuterJoin = 1;
144804 }
144805
144806 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
144807 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
144808 return 0; /* Restriction (27a) */
144809 }
144810
144811 /* Condition (28) is blocked by the caller */
144812 assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes );
144813
144814 /* Restriction (17): If the sub-query is a compound SELECT, then it must
144815 ** use only the UNION ALL operator. And none of the simple select queries
144816 ** that make up the compound SELECT are allowed to be aggregate or distinct
144817 ** queries.
144818 */
144819 if( pSub->pPrior ){
144820 int ii;
144821 if( pSub->pOrderBy ){
144822 return 0; /* Restriction (20) */
144823 }
144824 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
144825 return 0; /* (17d1), (17d2), or (17f) */
144826 }
144827 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
144828 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
144829 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
144830 assert( pSub->pSrc!=0 );
144831 assert( (pSub->selFlags & SF_Recursive)==0 );
144832 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
144833 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
144834 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
144835 || pSub1->pSrc->nSrc<1 /* (17c) */
144836#ifndef SQLITE_OMIT_WINDOWFUNC
144837 || pSub1->pWin /* (17e) */
144838#endif
144839 ){
144840 return 0;
144841 }
144842 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
144843 /* Without this restriction, the JT_LTORJ flag would end up being
144844 ** omitted on left-hand tables of the right join that is being
144845 ** flattened. */
144846 return 0; /* Restrictions (17g), (27b) */
144847 }
144848 testcase( pSub1->pSrc->nSrc>1 );
144849 }
144850
144851 /* Restriction (18). */
144852 if( p->pOrderBy ){
144853 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
144854 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
144855 }
144856 }
144857
144858 /* Restriction (23) */
144859 if( (p->selFlags & SF_Recursive) ) return 0;
144860
144861 /* Restriction (17h) */
144862 if( compoundHasDifferentAffinities(p: pSub) ) return 0;
144863
144864 if( pSrc->nSrc>1 ){
144865 if( pParse->nSelect>500 ) return 0;
144866 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
144867 aCsrMap = sqlite3DbMallocZero(db, n: ((i64)pParse->nTab+1)*sizeof(int));
144868 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
144869 }
144870 }
144871
144872 /***** If we reach this point, flattening is permitted. *****/
144873 TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n",
144874 pSub->selId, pSub, iFrom));
144875
144876 /* Authorize the subquery */
144877 pParse->zAuthContext = pSubitem->zName;
144878 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, zArg1: 0, zArg2: 0, zArg3: 0);
144879 testcase( i==SQLITE_DENY );
144880 pParse->zAuthContext = zSavedAuthContext;
144881
144882 /* Delete the transient structures associated with the subquery */
144883 pSub1 = pSubitem->pSelect;
144884 sqlite3DbFree(db, p: pSubitem->zDatabase);
144885 sqlite3DbFree(db, p: pSubitem->zName);
144886 sqlite3DbFree(db, p: pSubitem->zAlias);
144887 pSubitem->zDatabase = 0;
144888 pSubitem->zName = 0;
144889 pSubitem->zAlias = 0;
144890 pSubitem->pSelect = 0;
144891 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
144892
144893 /* If the sub-query is a compound SELECT statement, then (by restrictions
144894 ** 17 and 18 above) it must be a UNION ALL and the parent query must
144895 ** be of the form:
144896 **
144897 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
144898 **
144899 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
144900 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
144901 ** OFFSET clauses and joins them to the left-hand-side of the original
144902 ** using UNION ALL operators. In this case N is the number of simple
144903 ** select statements in the compound sub-query.
144904 **
144905 ** Example:
144906 **
144907 ** SELECT a+1 FROM (
144908 ** SELECT x FROM tab
144909 ** UNION ALL
144910 ** SELECT y FROM tab
144911 ** UNION ALL
144912 ** SELECT abs(z*2) FROM tab2
144913 ** ) WHERE a!=5 ORDER BY 1
144914 **
144915 ** Transformed into:
144916 **
144917 ** SELECT x+1 FROM tab WHERE x+1!=5
144918 ** UNION ALL
144919 ** SELECT y+1 FROM tab WHERE y+1!=5
144920 ** UNION ALL
144921 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
144922 ** ORDER BY 1
144923 **
144924 ** We call this the "compound-subquery flattening".
144925 */
144926 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
144927 Select *pNew;
144928 ExprList *pOrderBy = p->pOrderBy;
144929 Expr *pLimit = p->pLimit;
144930 Select *pPrior = p->pPrior;
144931 Table *pItemTab = pSubitem->pTab;
144932 pSubitem->pTab = 0;
144933 p->pOrderBy = 0;
144934 p->pPrior = 0;
144935 p->pLimit = 0;
144936 pNew = sqlite3SelectDup(db, pDup: p, flags: 0);
144937 p->pLimit = pLimit;
144938 p->pOrderBy = pOrderBy;
144939 p->op = TK_ALL;
144940 pSubitem->pTab = pItemTab;
144941 if( pNew==0 ){
144942 p->pPrior = pPrior;
144943 }else{
144944 pNew->selId = ++pParse->nSelect;
144945 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
144946 renumberCursors(pParse, p: pNew, iExcept: iFrom, aCsrMap);
144947 }
144948 pNew->pPrior = pPrior;
144949 if( pPrior ) pPrior->pNext = pNew;
144950 pNew->pNext = p;
144951 p->pPrior = pNew;
144952 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
144953 " creates %u as peer\n",pNew->selId));
144954 }
144955 assert( pSubitem->pSelect==0 );
144956 }
144957 sqlite3DbFree(db, p: aCsrMap);
144958 if( db->mallocFailed ){
144959 pSubitem->pSelect = pSub1;
144960 return 1;
144961 }
144962
144963 /* Defer deleting the Table object associated with the
144964 ** subquery until code generation is
144965 ** complete, since there may still exist Expr.pTab entries that
144966 ** refer to the subquery even after flattening. Ticket #3346.
144967 **
144968 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
144969 */
144970 if( ALWAYS(pSubitem->pTab!=0) ){
144971 Table *pTabToDel = pSubitem->pTab;
144972 if( pTabToDel->nTabRef==1 ){
144973 Parse *pToplevel = sqlite3ParseToplevel(pParse);
144974 sqlite3ParserAddCleanup(pParse: pToplevel,
144975 xCleanup: (void(*)(sqlite3*,void*))sqlite3DeleteTable,
144976 pPtr: pTabToDel);
144977 testcase( pToplevel->earlyCleanup );
144978 }else{
144979 pTabToDel->nTabRef--;
144980 }
144981 pSubitem->pTab = 0;
144982 }
144983
144984 /* The following loop runs once for each term in a compound-subquery
144985 ** flattening (as described above). If we are doing a different kind
144986 ** of flattening - a flattening other than a compound-subquery flattening -
144987 ** then this loop only runs once.
144988 **
144989 ** This loop moves all of the FROM elements of the subquery into the
144990 ** the FROM clause of the outer query. Before doing this, remember
144991 ** the cursor number for the original outer query FROM element in
144992 ** iParent. The iParent cursor will never be used. Subsequent code
144993 ** will scan expressions looking for iParent references and replace
144994 ** those references with expressions that resolve to the subquery FROM
144995 ** elements we are now copying in.
144996 */
144997 pSub = pSub1;
144998 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
144999 int nSubSrc;
145000 u8 jointype = 0;
145001 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
145002 assert( pSub!=0 );
145003 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
145004 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
145005 pSrc = pParent->pSrc; /* FROM clause of the outer query */
145006
145007 if( pParent==p ){
145008 jointype = pSubitem->fg.jointype; /* First time through the loop */
145009 }
145010
145011 /* The subquery uses a single slot of the FROM clause of the outer
145012 ** query. If the subquery has more than one element in its FROM clause,
145013 ** then expand the outer query to make space for it to hold all elements
145014 ** of the subquery.
145015 **
145016 ** Example:
145017 **
145018 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
145019 **
145020 ** The outer query has 3 slots in its FROM clause. One slot of the
145021 ** outer query (the middle slot) is used by the subquery. The next
145022 ** block of code will expand the outer query FROM clause to 4 slots.
145023 ** The middle slot is expanded to two slots in order to make space
145024 ** for the two elements in the FROM clause of the subquery.
145025 */
145026 if( nSubSrc>1 ){
145027 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nExtra: nSubSrc-1,iStart: iFrom+1);
145028 if( pSrc==0 ) break;
145029 pParent->pSrc = pSrc;
145030 }
145031
145032 /* Transfer the FROM clause terms from the subquery into the
145033 ** outer query.
145034 */
145035 for(i=0; i<nSubSrc; i++){
145036 SrcItem *pItem = &pSrc->a[i+iFrom];
145037 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pList: pItem->u3.pUsing);
145038 assert( pItem->fg.isTabFunc==0 );
145039 *pItem = pSubSrc->a[i];
145040 pItem->fg.jointype |= ltorj;
145041 iNewParent = pSubSrc->a[i].iCursor;
145042 memset(s: &pSubSrc->a[i], c: 0, n: sizeof(pSubSrc->a[i]));
145043 }
145044 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
145045 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
145046
145047 /* Now begin substituting subquery result set expressions for
145048 ** references to the iParent in the outer query.
145049 **
145050 ** Example:
145051 **
145052 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
145053 ** \ \_____________ subquery __________/ /
145054 ** \_____________________ outer query ______________________________/
145055 **
145056 ** We look at every expression in the outer query and every place we see
145057 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
145058 */
145059 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
145060 /* At this point, any non-zero iOrderByCol values indicate that the
145061 ** ORDER BY column expression is identical to the iOrderByCol'th
145062 ** expression returned by SELECT statement pSub. Since these values
145063 ** do not necessarily correspond to columns in SELECT statement pParent,
145064 ** zero them before transferring the ORDER BY clause.
145065 **
145066 ** Not doing this may cause an error if a subsequent call to this
145067 ** function attempts to flatten a compound sub-query into pParent
145068 ** (the only way this can happen is if the compound sub-query is
145069 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
145070 ExprList *pOrderBy = pSub->pOrderBy;
145071 for(i=0; i<pOrderBy->nExpr; i++){
145072 pOrderBy->a[i].u.x.iOrderByCol = 0;
145073 }
145074 assert( pParent->pOrderBy==0 );
145075 pParent->pOrderBy = pOrderBy;
145076 pSub->pOrderBy = 0;
145077 }
145078 pWhere = pSub->pWhere;
145079 pSub->pWhere = 0;
145080 if( isOuterJoin>0 ){
145081 sqlite3SetJoinExpr(p: pWhere, iTable: iNewParent, EP_OuterON);
145082 }
145083 if( pWhere ){
145084 if( pParent->pWhere ){
145085 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pLeft: pWhere, pRight: pParent->pWhere);
145086 }else{
145087 pParent->pWhere = pWhere;
145088 }
145089 }
145090 if( db->mallocFailed==0 ){
145091 SubstContext x;
145092 x.pParse = pParse;
145093 x.iTable = iParent;
145094 x.iNewTable = iNewParent;
145095 x.isOuterJoin = isOuterJoin;
145096 x.pEList = pSub->pEList;
145097 x.pCList = findLeftmostExprlist(pSel: pSub);
145098 substSelect(pSubst: &x, p: pParent, doPrior: 0);
145099 }
145100
145101 /* The flattened query is a compound if either the inner or the
145102 ** outer query is a compound. */
145103 pParent->selFlags |= pSub->selFlags & SF_Compound;
145104 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
145105
145106 /*
145107 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
145108 **
145109 ** One is tempted to try to add a and b to combine the limits. But this
145110 ** does not work if either limit is negative.
145111 */
145112 if( pSub->pLimit ){
145113 pParent->pLimit = pSub->pLimit;
145114 pSub->pLimit = 0;
145115 }
145116
145117 /* Recompute the SrcItem.colUsed masks for the flattened
145118 ** tables. */
145119 for(i=0; i<nSubSrc; i++){
145120 recomputeColumnsUsed(pSelect: pParent, pSrcItem: &pSrc->a[i+iFrom]);
145121 }
145122 }
145123
145124 /* Finally, delete what is left of the subquery and return success.
145125 */
145126 sqlite3AggInfoPersistWalkerInit(pWalker: &w, pParse);
145127 sqlite3WalkSelect(pWalker: &w,p: pSub1);
145128 sqlite3SelectDelete(db, p: pSub1);
145129
145130#if TREETRACE_ENABLED
145131 if( sqlite3TreeTrace & 0x4 ){
145132 TREETRACE(0x4,pParse,p,("After flattening:\n"));
145133 sqlite3TreeViewSelect(0, p, 0);
145134 }
145135#endif
145136
145137 return 1;
145138}
145139#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
145140
145141/*
145142** A structure to keep track of all of the column values that are fixed to
145143** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
145144*/
145145typedef struct WhereConst WhereConst;
145146struct WhereConst {
145147 Parse *pParse; /* Parsing context */
145148 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
145149 int nConst; /* Number for COLUMN=CONSTANT terms */
145150 int nChng; /* Number of times a constant is propagated */
145151 int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */
145152 u32 mExcludeOn; /* Which ON expressions to exclude from considertion.
145153 ** Either EP_OuterON or EP_InnerON|EP_OuterON */
145154 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
145155};
145156
145157/*
145158** Add a new entry to the pConst object. Except, do not add duplicate
145159** pColumn entries. Also, do not add if doing so would not be appropriate.
145160**
145161** The caller guarantees the pColumn is a column and pValue is a constant.
145162** This routine has to do some additional checks before completing the
145163** insert.
145164*/
145165static void constInsert(
145166 WhereConst *pConst, /* The WhereConst into which we are inserting */
145167 Expr *pColumn, /* The COLUMN part of the constraint */
145168 Expr *pValue, /* The VALUE part of the constraint */
145169 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
145170){
145171 int i;
145172 assert( pColumn->op==TK_COLUMN );
145173 assert( sqlite3ExprIsConstant(pValue) );
145174
145175 if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
145176 if( sqlite3ExprAffinity(pExpr: pValue)!=0 ) return;
145177 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pParse: pConst->pParse,p: pExpr)) ){
145178 return;
145179 }
145180
145181 /* 2018-10-25 ticket [cf5ed20f]
145182 ** Make sure the same pColumn is not inserted more than once */
145183 for(i=0; i<pConst->nConst; i++){
145184 const Expr *pE2 = pConst->apExpr[i*2];
145185 assert( pE2->op==TK_COLUMN );
145186 if( pE2->iTable==pColumn->iTable
145187 && pE2->iColumn==pColumn->iColumn
145188 ){
145189 return; /* Already present. Return without doing anything. */
145190 }
145191 }
145192 if( sqlite3ExprAffinity(pExpr: pColumn)==SQLITE_AFF_BLOB ){
145193 pConst->bHasAffBlob = 1;
145194 }
145195
145196 pConst->nConst++;
145197 pConst->apExpr = sqlite3DbReallocOrFree(db: pConst->pParse->db, p: pConst->apExpr,
145198 n: pConst->nConst*2*sizeof(Expr*));
145199 if( pConst->apExpr==0 ){
145200 pConst->nConst = 0;
145201 }else{
145202 pConst->apExpr[pConst->nConst*2-2] = pColumn;
145203 pConst->apExpr[pConst->nConst*2-1] = pValue;
145204 }
145205}
145206
145207/*
145208** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
145209** is a constant expression and where the term must be true because it
145210** is part of the AND-connected terms of the expression. For each term
145211** found, add it to the pConst structure.
145212*/
145213static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
145214 Expr *pRight, *pLeft;
145215 if( NEVER(pExpr==0) ) return;
145216 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
145217 testcase( ExprHasProperty(pExpr, EP_OuterON) );
145218 testcase( ExprHasProperty(pExpr, EP_InnerON) );
145219 return;
145220 }
145221 if( pExpr->op==TK_AND ){
145222 findConstInWhere(pConst, pExpr: pExpr->pRight);
145223 findConstInWhere(pConst, pExpr: pExpr->pLeft);
145224 return;
145225 }
145226 if( pExpr->op!=TK_EQ ) return;
145227 pRight = pExpr->pRight;
145228 pLeft = pExpr->pLeft;
145229 assert( pRight!=0 );
145230 assert( pLeft!=0 );
145231 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(p: pLeft) ){
145232 constInsert(pConst,pColumn: pRight,pValue: pLeft,pExpr);
145233 }
145234 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(p: pRight) ){
145235 constInsert(pConst,pColumn: pLeft,pValue: pRight,pExpr);
145236 }
145237}
145238
145239/*
145240** This is a helper function for Walker callback propagateConstantExprRewrite().
145241**
145242** Argument pExpr is a candidate expression to be replaced by a value. If
145243** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
145244** then overwrite it with the corresponding value. Except, do not do so
145245** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
145246** is SQLITE_AFF_BLOB.
145247*/
145248static int propagateConstantExprRewriteOne(
145249 WhereConst *pConst,
145250 Expr *pExpr,
145251 int bIgnoreAffBlob
145252){
145253 int i;
145254 if( pConst->pOomFault[0] ) return WRC_Prune;
145255 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
145256 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
145257 testcase( ExprHasProperty(pExpr, EP_FixedCol) );
145258 testcase( ExprHasProperty(pExpr, EP_OuterON) );
145259 testcase( ExprHasProperty(pExpr, EP_InnerON) );
145260 return WRC_Continue;
145261 }
145262 for(i=0; i<pConst->nConst; i++){
145263 Expr *pColumn = pConst->apExpr[i*2];
145264 if( pColumn==pExpr ) continue;
145265 if( pColumn->iTable!=pExpr->iTable ) continue;
145266 if( pColumn->iColumn!=pExpr->iColumn ) continue;
145267 if( bIgnoreAffBlob && sqlite3ExprAffinity(pExpr: pColumn)==SQLITE_AFF_BLOB ){
145268 break;
145269 }
145270 /* A match is found. Add the EP_FixedCol property */
145271 pConst->nChng++;
145272 ExprClearProperty(pExpr, EP_Leaf);
145273 ExprSetProperty(pExpr, EP_FixedCol);
145274 assert( pExpr->pLeft==0 );
145275 pExpr->pLeft = sqlite3ExprDup(db: pConst->pParse->db, p: pConst->apExpr[i*2+1], flags: 0);
145276 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
145277 break;
145278 }
145279 return WRC_Prune;
145280}
145281
145282/*
145283** This is a Walker expression callback. pExpr is a node from the WHERE
145284** clause of a SELECT statement. This function examines pExpr to see if
145285** any substitutions based on the contents of pWalker->u.pConst should
145286** be made to pExpr or its immediate children.
145287**
145288** A substitution is made if:
145289**
145290** + pExpr is a column with an affinity other than BLOB that matches
145291** one of the columns in pWalker->u.pConst, or
145292**
145293** + pExpr is a binary comparison operator (=, <=, >=, <, >) that
145294** uses an affinity other than TEXT and one of its immediate
145295** children is a column that matches one of the columns in
145296** pWalker->u.pConst.
145297*/
145298static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
145299 WhereConst *pConst = pWalker->u.pConst;
145300 assert( TK_GT==TK_EQ+1 );
145301 assert( TK_LE==TK_EQ+2 );
145302 assert( TK_LT==TK_EQ+3 );
145303 assert( TK_GE==TK_EQ+4 );
145304 if( pConst->bHasAffBlob ){
145305 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
145306 || pExpr->op==TK_IS
145307 ){
145308 propagateConstantExprRewriteOne(pConst, pExpr: pExpr->pLeft, bIgnoreAffBlob: 0);
145309 if( pConst->pOomFault[0] ) return WRC_Prune;
145310 if( sqlite3ExprAffinity(pExpr: pExpr->pLeft)!=SQLITE_AFF_TEXT ){
145311 propagateConstantExprRewriteOne(pConst, pExpr: pExpr->pRight, bIgnoreAffBlob: 0);
145312 }
145313 }
145314 }
145315 return propagateConstantExprRewriteOne(pConst, pExpr, bIgnoreAffBlob: pConst->bHasAffBlob);
145316}
145317
145318/*
145319** The WHERE-clause constant propagation optimization.
145320**
145321** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
145322** CONSTANT=COLUMN that are top-level AND-connected terms that are not
145323** part of a ON clause from a LEFT JOIN, then throughout the query
145324** replace all other occurrences of COLUMN with CONSTANT.
145325**
145326** For example, the query:
145327**
145328** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
145329**
145330** Is transformed into
145331**
145332** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
145333**
145334** Return true if any transformations where made and false if not.
145335**
145336** Implementation note: Constant propagation is tricky due to affinity
145337** and collating sequence interactions. Consider this example:
145338**
145339** CREATE TABLE t1(a INT,b TEXT);
145340** INSERT INTO t1 VALUES(123,'0123');
145341** SELECT * FROM t1 WHERE a=123 AND b=a;
145342** SELECT * FROM t1 WHERE a=123 AND b=123;
145343**
145344** The two SELECT statements above should return different answers. b=a
145345** is always true because the comparison uses numeric affinity, but b=123
145346** is false because it uses text affinity and '0123' is not the same as '123'.
145347** To work around this, the expression tree is not actually changed from
145348** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
145349** and the "123" value is hung off of the pLeft pointer. Code generator
145350** routines know to generate the constant "123" instead of looking up the
145351** column value. Also, to avoid collation problems, this optimization is
145352** only attempted if the "a=123" term uses the default BINARY collation.
145353**
145354** 2021-05-25 forum post 6a06202608: Another troublesome case is...
145355**
145356** CREATE TABLE t1(x);
145357** INSERT INTO t1 VALUES(10.0);
145358** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
145359**
145360** The query should return no rows, because the t1.x value is '10.0' not '10'
145361** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE
145362** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
145363** resulting in a false positive. To avoid this, constant propagation for
145364** columns with BLOB affinity is only allowed if the constant is used with
145365** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
145366** type conversions to occur. See logic associated with the bHasAffBlob flag
145367** for details.
145368*/
145369static int propagateConstants(
145370 Parse *pParse, /* The parsing context */
145371 Select *p /* The query in which to propagate constants */
145372){
145373 WhereConst x;
145374 Walker w;
145375 int nChng = 0;
145376 x.pParse = pParse;
145377 x.pOomFault = &pParse->db->mallocFailed;
145378 do{
145379 x.nConst = 0;
145380 x.nChng = 0;
145381 x.apExpr = 0;
145382 x.bHasAffBlob = 0;
145383 if( ALWAYS(p->pSrc!=0)
145384 && p->pSrc->nSrc>0
145385 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
145386 ){
145387 /* Do not propagate constants on any ON clause if there is a
145388 ** RIGHT JOIN anywhere in the query */
145389 x.mExcludeOn = EP_InnerON | EP_OuterON;
145390 }else{
145391 /* Do not propagate constants through the ON clause of a LEFT JOIN */
145392 x.mExcludeOn = EP_OuterON;
145393 }
145394 findConstInWhere(pConst: &x, pExpr: p->pWhere);
145395 if( x.nConst ){
145396 memset(s: &w, c: 0, n: sizeof(w));
145397 w.pParse = pParse;
145398 w.xExprCallback = propagateConstantExprRewrite;
145399 w.xSelectCallback = sqlite3SelectWalkNoop;
145400 w.xSelectCallback2 = 0;
145401 w.walkerDepth = 0;
145402 w.u.pConst = &x;
145403 sqlite3WalkExpr(pWalker: &w, pExpr: p->pWhere);
145404 sqlite3DbFree(db: x.pParse->db, p: x.apExpr);
145405 nChng += x.nChng;
145406 }
145407 }while( x.nChng );
145408 return nChng;
145409}
145410
145411#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
145412# if !defined(SQLITE_OMIT_WINDOWFUNC)
145413/*
145414** This function is called to determine whether or not it is safe to
145415** push WHERE clause expression pExpr down to FROM clause sub-query
145416** pSubq, which contains at least one window function. Return 1
145417** if it is safe and the expression should be pushed down, or 0
145418** otherwise.
145419**
145420** It is only safe to push the expression down if it consists only
145421** of constants and copies of expressions that appear in the PARTITION
145422** BY clause of all window function used by the sub-query. It is safe
145423** to filter out entire partitions, but not rows within partitions, as
145424** this may change the results of the window functions.
145425**
145426** At the time this function is called it is guaranteed that
145427**
145428** * the sub-query uses only one distinct window frame, and
145429** * that the window frame has a PARTITION BY clause.
145430*/
145431static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
145432 assert( pSubq->pWin->pPartition );
145433 assert( (pSubq->selFlags & SF_MultiPart)==0 );
145434 assert( pSubq->pPrior==0 );
145435 return sqlite3ExprIsConstantOrGroupBy(pParse, p: pExpr, pGroupBy: pSubq->pWin->pPartition);
145436}
145437# endif /* SQLITE_OMIT_WINDOWFUNC */
145438#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
145439
145440#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
145441/*
145442** Make copies of relevant WHERE clause terms of the outer query into
145443** the WHERE clause of subquery. Example:
145444**
145445** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
145446**
145447** Transformed into:
145448**
145449** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
145450** WHERE x=5 AND y=10;
145451**
145452** The hope is that the terms added to the inner query will make it more
145453** efficient.
145454**
145455** Do not attempt this optimization if:
145456**
145457** (1) (** This restriction was removed on 2017-09-29. We used to
145458** disallow this optimization for aggregate subqueries, but now
145459** it is allowed by putting the extra terms on the HAVING clause.
145460** The added HAVING clause is pointless if the subquery lacks
145461** a GROUP BY clause. But such a HAVING clause is also harmless
145462** so there does not appear to be any reason to add extra logic
145463** to suppress it. **)
145464**
145465** (2) The inner query is the recursive part of a common table expression.
145466**
145467** (3) The inner query has a LIMIT clause (since the changes to the WHERE
145468** clause would change the meaning of the LIMIT).
145469**
145470** (4) The inner query is the right operand of a LEFT JOIN and the
145471** expression to be pushed down does not come from the ON clause
145472** on that LEFT JOIN.
145473**
145474** (5) The WHERE clause expression originates in the ON or USING clause
145475** of a LEFT JOIN where iCursor is not the right-hand table of that
145476** left join. An example:
145477**
145478** SELECT *
145479** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
145480** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
145481** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
145482**
145483** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
145484** But if the (b2=2) term were to be pushed down into the bb subquery,
145485** then the (1,1,NULL) row would be suppressed.
145486**
145487** (6) Window functions make things tricky as changes to the WHERE clause
145488** of the inner query could change the window over which window
145489** functions are calculated. Therefore, do not attempt the optimization
145490** if:
145491**
145492** (6a) The inner query uses multiple incompatible window partitions.
145493**
145494** (6b) The inner query is a compound and uses window-functions.
145495**
145496** (6c) The WHERE clause does not consist entirely of constants and
145497** copies of expressions found in the PARTITION BY clause of
145498** all window-functions used by the sub-query. It is safe to
145499** filter out entire partitions, as this does not change the
145500** window over which any window-function is calculated.
145501**
145502** (7) The inner query is a Common Table Expression (CTE) that should
145503** be materialized. (This restriction is implemented in the calling
145504** routine.)
145505**
145506** (8) If the subquery is a compound that uses UNION, INTERSECT,
145507** or EXCEPT, then all of the result set columns for all arms of
145508** the compound must use the BINARY collating sequence.
145509**
145510** (9) All three of the following are true:
145511**
145512** (9a) The WHERE clause expression originates in the ON or USING clause
145513** of a join (either an INNER or an OUTER join), and
145514**
145515** (9b) The subquery is to the right of the ON/USING clause
145516**
145517** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING
145518** clause and the subquery.
145519**
145520** Without this restriction, the push-down optimization might move
145521** the ON/USING filter expression from the left side of a RIGHT JOIN
145522** over to the right side, which leads to incorrect answers. See
145523** also restriction (6) in sqlite3ExprIsSingleTableConstraint().
145524**
145525** (10) The inner query is not the right-hand table of a RIGHT JOIN.
145526**
145527** (11) The subquery is not a VALUES clause
145528**
145529** Return 0 if no changes are made and non-zero if one or more WHERE clause
145530** terms are duplicated into the subquery.
145531*/
145532static int pushDownWhereTerms(
145533 Parse *pParse, /* Parse context (for malloc() and error reporting) */
145534 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
145535 Expr *pWhere, /* The WHERE clause of the outer query */
145536 SrcList *pSrcList, /* The complete from clause of the outer query */
145537 int iSrc /* Which FROM clause term to try to push into */
145538){
145539 Expr *pNew;
145540 SrcItem *pSrc; /* The subquery FROM term into which WHERE is pushed */
145541 int nChng = 0;
145542 pSrc = &pSrcList->a[iSrc];
145543 if( pWhere==0 ) return 0;
145544 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ){
145545 return 0; /* restrictions (2) and (11) */
145546 }
145547 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ){
145548 return 0; /* restrictions (10) */
145549 }
145550
145551 if( pSubq->pPrior ){
145552 Select *pSel;
145553 int notUnionAll = 0;
145554 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
145555 u8 op = pSel->op;
145556 assert( op==TK_ALL || op==TK_SELECT
145557 || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
145558 if( op!=TK_ALL && op!=TK_SELECT ){
145559 notUnionAll = 1;
145560 }
145561#ifndef SQLITE_OMIT_WINDOWFUNC
145562 if( pSel->pWin ) return 0; /* restriction (6b) */
145563#endif
145564 }
145565 if( notUnionAll ){
145566 /* If any of the compound arms are connected using UNION, INTERSECT,
145567 ** or EXCEPT, then we must ensure that none of the columns use a
145568 ** non-BINARY collating sequence. */
145569 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
145570 int ii;
145571 const ExprList *pList = pSel->pEList;
145572 assert( pList!=0 );
145573 for(ii=0; ii<pList->nExpr; ii++){
145574 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr: pList->a[ii].pExpr);
145575 if( !sqlite3IsBinary(pColl) ){
145576 return 0; /* Restriction (8) */
145577 }
145578 }
145579 }
145580 }
145581 }else{
145582#ifndef SQLITE_OMIT_WINDOWFUNC
145583 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
145584#endif
145585 }
145586
145587#ifdef SQLITE_DEBUG
145588 /* Only the first term of a compound can have a WITH clause. But make
145589 ** sure no other terms are marked SF_Recursive in case something changes
145590 ** in the future.
145591 */
145592 {
145593 Select *pX;
145594 for(pX=pSubq; pX; pX=pX->pPrior){
145595 assert( (pX->selFlags & (SF_Recursive))==0 );
145596 }
145597 }
145598#endif
145599
145600 if( pSubq->pLimit!=0 ){
145601 return 0; /* restriction (3) */
145602 }
145603 while( pWhere->op==TK_AND ){
145604 nChng += pushDownWhereTerms(pParse, pSubq, pWhere: pWhere->pRight, pSrcList, iSrc);
145605 pWhere = pWhere->pLeft;
145606 }
145607
145608#if 0 /* These checks now done by sqlite3ExprIsSingleTableConstraint() */
145609 if( ExprHasProperty(pWhere, EP_OuterON|EP_InnerON) /* (9a) */
145610 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (9c) */
145611 ){
145612 int jj;
145613 for(jj=0; jj<iSrc; jj++){
145614 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
145615 /* If we reach this point, both (9a) and (9b) are satisfied.
145616 ** The following loop checks (9c):
145617 */
145618 for(jj++; jj<iSrc; jj++){
145619 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT)!=0 ){
145620 return 0; /* restriction (9) */
145621 }
145622 }
145623 }
145624 }
145625 }
145626 if( isLeftJoin
145627 && (ExprHasProperty(pWhere,EP_OuterON)==0
145628 || pWhere->w.iJoin!=iCursor)
145629 ){
145630 return 0; /* restriction (4) */
145631 }
145632 if( ExprHasProperty(pWhere,EP_OuterON)
145633 && pWhere->w.iJoin!=iCursor
145634 ){
145635 return 0; /* restriction (5) */
145636 }
145637#endif
145638
145639 if( sqlite3ExprIsSingleTableConstraint(pExpr: pWhere, pSrcList, iSrc) ){
145640 nChng++;
145641 pSubq->selFlags |= SF_PushDown;
145642 while( pSubq ){
145643 SubstContext x;
145644 pNew = sqlite3ExprDup(db: pParse->db, p: pWhere, flags: 0);
145645 unsetJoinExpr(p: pNew, iTable: -1, nullable: 1);
145646 x.pParse = pParse;
145647 x.iTable = pSrc->iCursor;
145648 x.iNewTable = pSrc->iCursor;
145649 x.isOuterJoin = 0;
145650 x.pEList = pSubq->pEList;
145651 x.pCList = findLeftmostExprlist(pSel: pSubq);
145652 pNew = substExpr(pSubst: &x, pExpr: pNew);
145653#ifndef SQLITE_OMIT_WINDOWFUNC
145654 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pExpr: pNew) ){
145655 /* Restriction 6c has prevented push-down in this case */
145656 sqlite3ExprDelete(db: pParse->db, p: pNew);
145657 nChng--;
145658 break;
145659 }
145660#endif
145661 if( pSubq->selFlags & SF_Aggregate ){
145662 pSubq->pHaving = sqlite3ExprAnd(pParse, pLeft: pSubq->pHaving, pRight: pNew);
145663 }else{
145664 pSubq->pWhere = sqlite3ExprAnd(pParse, pLeft: pSubq->pWhere, pRight: pNew);
145665 }
145666 pSubq = pSubq->pPrior;
145667 }
145668 }
145669 return nChng;
145670}
145671#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
145672
145673/*
145674** Check to see if a subquery contains result-set columns that are
145675** never used. If it does, change the value of those result-set columns
145676** to NULL so that they do not cause unnecessary work to compute.
145677**
145678** Return the number of column that were changed to NULL.
145679*/
145680static int disableUnusedSubqueryResultColumns(SrcItem *pItem){
145681 int nCol;
145682 Select *pSub; /* The subquery to be simplified */
145683 Select *pX; /* For looping over compound elements of pSub */
145684 Table *pTab; /* The table that describes the subquery */
145685 int j; /* Column number */
145686 int nChng = 0; /* Number of columns converted to NULL */
145687 Bitmask colUsed; /* Columns that may not be NULLed out */
145688
145689 assert( pItem!=0 );
145690 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
145691 return 0;
145692 }
145693 assert( pItem->pTab!=0 );
145694 pTab = pItem->pTab;
145695 assert( pItem->pSelect!=0 );
145696 pSub = pItem->pSelect;
145697 assert( pSub->pEList->nExpr==pTab->nCol );
145698 for(pX=pSub; pX; pX=pX->pPrior){
145699 if( (pX->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){
145700 testcase( pX->selFlags & SF_Distinct );
145701 testcase( pX->selFlags & SF_Aggregate );
145702 return 0;
145703 }
145704 if( pX->pPrior && pX->op!=TK_ALL ){
145705 /* This optimization does not work for compound subqueries that
145706 ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */
145707 return 0;
145708 }
145709#ifndef SQLITE_OMIT_WINDOWFUNC
145710 if( pX->pWin ){
145711 /* This optimization does not work for subqueries that use window
145712 ** functions. */
145713 return 0;
145714 }
145715#endif
145716 }
145717 colUsed = pItem->colUsed;
145718 if( pSub->pOrderBy ){
145719 ExprList *pList = pSub->pOrderBy;
145720 for(j=0; j<pList->nExpr; j++){
145721 u16 iCol = pList->a[j].u.x.iOrderByCol;
145722 if( iCol>0 ){
145723 iCol--;
145724 colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
145725 }
145726 }
145727 }
145728 nCol = pTab->nCol;
145729 for(j=0; j<nCol; j++){
145730 Bitmask m = j<BMS-1 ? MASKBIT(j) : TOPBIT;
145731 if( (m & colUsed)!=0 ) continue;
145732 for(pX=pSub; pX; pX=pX->pPrior) {
145733 Expr *pY = pX->pEList->a[j].pExpr;
145734 if( pY->op==TK_NULL ) continue;
145735 pY->op = TK_NULL;
145736 ExprClearProperty(pY, EP_Skip|EP_Unlikely);
145737 pX->selFlags |= SF_PushDown;
145738 nChng++;
145739 }
145740 }
145741 return nChng;
145742}
145743
145744
145745/*
145746** The pFunc is the only aggregate function in the query. Check to see
145747** if the query is a candidate for the min/max optimization.
145748**
145749** If the query is a candidate for the min/max optimization, then set
145750** *ppMinMax to be an ORDER BY clause to be used for the optimization
145751** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
145752** whether pFunc is a min() or max() function.
145753**
145754** If the query is not a candidate for the min/max optimization, return
145755** WHERE_ORDERBY_NORMAL (which must be zero).
145756**
145757** This routine must be called after aggregate functions have been
145758** located but before their arguments have been subjected to aggregate
145759** analysis.
145760*/
145761static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
145762 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
145763 ExprList *pEList; /* Arguments to agg function */
145764 const char *zFunc; /* Name of aggregate function pFunc */
145765 ExprList *pOrderBy;
145766 u8 sortFlags = 0;
145767
145768 assert( *ppMinMax==0 );
145769 assert( pFunc->op==TK_AGG_FUNCTION );
145770 assert( !IsWindowFunc(pFunc) );
145771 assert( ExprUseXList(pFunc) );
145772 pEList = pFunc->x.pList;
145773 if( pEList==0
145774 || pEList->nExpr!=1
145775 || ExprHasProperty(pFunc, EP_WinFunc)
145776 || OptimizationDisabled(db, SQLITE_MinMaxOpt)
145777 ){
145778 return eRet;
145779 }
145780 assert( !ExprHasProperty(pFunc, EP_IntValue) );
145781 zFunc = pFunc->u.zToken;
145782 if( sqlite3StrICmp(zLeft: zFunc, zRight: "min")==0 ){
145783 eRet = WHERE_ORDERBY_MIN;
145784 if( sqlite3ExprCanBeNull(p: pEList->a[0].pExpr) ){
145785 sortFlags = KEYINFO_ORDER_BIGNULL;
145786 }
145787 }else if( sqlite3StrICmp(zLeft: zFunc, zRight: "max")==0 ){
145788 eRet = WHERE_ORDERBY_MAX;
145789 sortFlags = KEYINFO_ORDER_DESC;
145790 }else{
145791 return eRet;
145792 }
145793 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, p: pEList, flags: 0);
145794 assert( pOrderBy!=0 || db->mallocFailed );
145795 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
145796 return eRet;
145797}
145798
145799/*
145800** The select statement passed as the first argument is an aggregate query.
145801** The second argument is the associated aggregate-info object. This
145802** function tests if the SELECT is of the form:
145803**
145804** SELECT count(*) FROM <tbl>
145805**
145806** where table is a database table, not a sub-select or view. If the query
145807** does match this pattern, then a pointer to the Table object representing
145808** <tbl> is returned. Otherwise, NULL is returned.
145809**
145810** This routine checks to see if it is safe to use the count optimization.
145811** A correct answer is still obtained (though perhaps more slowly) if
145812** this routine returns NULL when it could have returned a table pointer.
145813** But returning the pointer when NULL should have been returned can
145814** result in incorrect answers and/or crashes. So, when in doubt, return NULL.
145815*/
145816static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
145817 Table *pTab;
145818 Expr *pExpr;
145819
145820 assert( !p->pGroupBy );
145821
145822 if( p->pWhere
145823 || p->pEList->nExpr!=1
145824 || p->pSrc->nSrc!=1
145825 || p->pSrc->a[0].pSelect
145826 || pAggInfo->nFunc!=1
145827 || p->pHaving
145828 ){
145829 return 0;
145830 }
145831 pTab = p->pSrc->a[0].pTab;
145832 assert( pTab!=0 );
145833 assert( !IsView(pTab) );
145834 if( !IsOrdinaryTable(pTab) ) return 0;
145835 pExpr = p->pEList->a[0].pExpr;
145836 assert( pExpr!=0 );
145837 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
145838 if( pExpr->pAggInfo!=pAggInfo ) return 0;
145839 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
145840 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
145841 testcase( ExprHasProperty(pExpr, EP_Distinct) );
145842 testcase( ExprHasProperty(pExpr, EP_WinFunc) );
145843 if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
145844
145845 return pTab;
145846}
145847
145848/*
145849** If the source-list item passed as an argument was augmented with an
145850** INDEXED BY clause, then try to locate the specified index. If there
145851** was such a clause and the named index cannot be found, return
145852** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
145853** pFrom->pIndex and return SQLITE_OK.
145854*/
145855SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){
145856 Table *pTab = pFrom->pTab;
145857 char *zIndexedBy = pFrom->u1.zIndexedBy;
145858 Index *pIdx;
145859 assert( pTab!=0 );
145860 assert( pFrom->fg.isIndexedBy!=0 );
145861
145862 for(pIdx=pTab->pIndex;
145863 pIdx && sqlite3StrICmp(zLeft: pIdx->zName, zRight: zIndexedBy);
145864 pIdx=pIdx->pNext
145865 );
145866 if( !pIdx ){
145867 sqlite3ErrorMsg(pParse, zFormat: "no such index: %s", zIndexedBy, 0);
145868 pParse->checkSchema = 1;
145869 return SQLITE_ERROR;
145870 }
145871 assert( pFrom->fg.isCte==0 );
145872 pFrom->u2.pIBIndex = pIdx;
145873 return SQLITE_OK;
145874}
145875
145876/*
145877** Detect compound SELECT statements that use an ORDER BY clause with
145878** an alternative collating sequence.
145879**
145880** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
145881**
145882** These are rewritten as a subquery:
145883**
145884** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
145885** ORDER BY ... COLLATE ...
145886**
145887** This transformation is necessary because the multiSelectOrderBy() routine
145888** above that generates the code for a compound SELECT with an ORDER BY clause
145889** uses a merge algorithm that requires the same collating sequence on the
145890** result columns as on the ORDER BY clause. See ticket
145891** http://www.sqlite.org/src/info/6709574d2a
145892**
145893** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
145894** The UNION ALL operator works fine with multiSelectOrderBy() even when
145895** there are COLLATE terms in the ORDER BY.
145896*/
145897static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
145898 int i;
145899 Select *pNew;
145900 Select *pX;
145901 sqlite3 *db;
145902 struct ExprList_item *a;
145903 SrcList *pNewSrc;
145904 Parse *pParse;
145905 Token dummy;
145906
145907 if( p->pPrior==0 ) return WRC_Continue;
145908 if( p->pOrderBy==0 ) return WRC_Continue;
145909 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
145910 if( pX==0 ) return WRC_Continue;
145911 a = p->pOrderBy->a;
145912#ifndef SQLITE_OMIT_WINDOWFUNC
145913 /* If iOrderByCol is already non-zero, then it has already been matched
145914 ** to a result column of the SELECT statement. This occurs when the
145915 ** SELECT is rewritten for window-functions processing and then passed
145916 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
145917 ** by this function is not required in this case. */
145918 if( a[0].u.x.iOrderByCol ) return WRC_Continue;
145919#endif
145920 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
145921 if( a[i].pExpr->flags & EP_Collate ) break;
145922 }
145923 if( i<0 ) return WRC_Continue;
145924
145925 /* If we reach this point, that means the transformation is required. */
145926
145927 pParse = pWalker->pParse;
145928 db = pParse->db;
145929 pNew = sqlite3DbMallocZero(db, n: sizeof(*pNew) );
145930 if( pNew==0 ) return WRC_Abort;
145931 memset(s: &dummy, c: 0, n: sizeof(dummy));
145932 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,p: 0,pTable: 0,pDatabase: 0,pAlias: &dummy,pSubquery: pNew,pOnUsing: 0);
145933 if( pNewSrc==0 ) return WRC_Abort;
145934 *pNew = *p;
145935 p->pSrc = pNewSrc;
145936 p->pEList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: sqlite3Expr(db, TK_ASTERISK, zToken: 0));
145937 p->op = TK_SELECT;
145938 p->pWhere = 0;
145939 pNew->pGroupBy = 0;
145940 pNew->pHaving = 0;
145941 pNew->pOrderBy = 0;
145942 p->pPrior = 0;
145943 p->pNext = 0;
145944 p->pWith = 0;
145945#ifndef SQLITE_OMIT_WINDOWFUNC
145946 p->pWinDefn = 0;
145947#endif
145948 p->selFlags &= ~SF_Compound;
145949 assert( (p->selFlags & SF_Converted)==0 );
145950 p->selFlags |= SF_Converted;
145951 assert( pNew->pPrior!=0 );
145952 pNew->pPrior->pNext = pNew;
145953 pNew->pLimit = 0;
145954 return WRC_Continue;
145955}
145956
145957/*
145958** Check to see if the FROM clause term pFrom has table-valued function
145959** arguments. If it does, leave an error message in pParse and return
145960** non-zero, since pFrom is not allowed to be a table-valued function.
145961*/
145962static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){
145963 if( pFrom->fg.isTabFunc ){
145964 sqlite3ErrorMsg(pParse, zFormat: "'%s' is not a function", pFrom->zName);
145965 return 1;
145966 }
145967 return 0;
145968}
145969
145970#ifndef SQLITE_OMIT_CTE
145971/*
145972** Argument pWith (which may be NULL) points to a linked list of nested
145973** WITH contexts, from inner to outermost. If the table identified by
145974** FROM clause element pItem is really a common-table-expression (CTE)
145975** then return a pointer to the CTE definition for that table. Otherwise
145976** return NULL.
145977**
145978** If a non-NULL value is returned, set *ppContext to point to the With
145979** object that the returned CTE belongs to.
145980*/
145981static struct Cte *searchWith(
145982 With *pWith, /* Current innermost WITH clause */
145983 SrcItem *pItem, /* FROM clause element to resolve */
145984 With **ppContext /* OUT: WITH clause return value belongs to */
145985){
145986 const char *zName = pItem->zName;
145987 With *p;
145988 assert( pItem->zDatabase==0 );
145989 assert( zName!=0 );
145990 for(p=pWith; p; p=p->pOuter){
145991 int i;
145992 for(i=0; i<p->nCte; i++){
145993 if( sqlite3StrICmp(zLeft: zName, zRight: p->a[i].zName)==0 ){
145994 *ppContext = p;
145995 return &p->a[i];
145996 }
145997 }
145998 if( p->bView ) break;
145999 }
146000 return 0;
146001}
146002
146003/* The code generator maintains a stack of active WITH clauses
146004** with the inner-most WITH clause being at the top of the stack.
146005**
146006** This routine pushes the WITH clause passed as the second argument
146007** onto the top of the stack. If argument bFree is true, then this
146008** WITH clause will never be popped from the stack but should instead
146009** be freed along with the Parse object. In other cases, when
146010** bFree==0, the With object will be freed along with the SELECT
146011** statement with which it is associated.
146012**
146013** This routine returns a copy of pWith. Or, if bFree is true and
146014** the pWith object is destroyed immediately due to an OOM condition,
146015** then this routine return NULL.
146016**
146017** If bFree is true, do not continue to use the pWith pointer after
146018** calling this routine, Instead, use only the return value.
146019*/
146020SQLITE_PRIVATE With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
146021 if( pWith ){
146022 if( bFree ){
146023 pWith = (With*)sqlite3ParserAddCleanup(pParse,
146024 xCleanup: (void(*)(sqlite3*,void*))sqlite3WithDelete,
146025 pPtr: pWith);
146026 if( pWith==0 ) return 0;
146027 }
146028 if( pParse->nErr==0 ){
146029 assert( pParse->pWith!=pWith );
146030 pWith->pOuter = pParse->pWith;
146031 pParse->pWith = pWith;
146032 }
146033 }
146034 return pWith;
146035}
146036
146037/*
146038** This function checks if argument pFrom refers to a CTE declared by
146039** a WITH clause on the stack currently maintained by the parser (on the
146040** pParse->pWith linked list). And if currently processing a CTE
146041** CTE expression, through routine checks to see if the reference is
146042** a recursive reference to the CTE.
146043**
146044** If pFrom matches a CTE according to either of these two above, pFrom->pTab
146045** and other fields are populated accordingly.
146046**
146047** Return 0 if no match is found.
146048** Return 1 if a match is found.
146049** Return 2 if an error condition is detected.
146050*/
146051static int resolveFromTermToCte(
146052 Parse *pParse, /* The parsing context */
146053 Walker *pWalker, /* Current tree walker */
146054 SrcItem *pFrom /* The FROM clause term to check */
146055){
146056 Cte *pCte; /* Matched CTE (or NULL if no match) */
146057 With *pWith; /* The matching WITH */
146058
146059 assert( pFrom->pTab==0 );
146060 if( pParse->pWith==0 ){
146061 /* There are no WITH clauses in the stack. No match is possible */
146062 return 0;
146063 }
146064 if( pParse->nErr ){
146065 /* Prior errors might have left pParse->pWith in a goofy state, so
146066 ** go no further. */
146067 return 0;
146068 }
146069 if( pFrom->zDatabase!=0 ){
146070 /* The FROM term contains a schema qualifier (ex: main.t1) and so
146071 ** it cannot possibly be a CTE reference. */
146072 return 0;
146073 }
146074 if( pFrom->fg.notCte ){
146075 /* The FROM term is specifically excluded from matching a CTE.
146076 ** (1) It is part of a trigger that used to have zDatabase but had
146077 ** zDatabase removed by sqlite3FixTriggerStep().
146078 ** (2) This is the first term in the FROM clause of an UPDATE.
146079 */
146080 return 0;
146081 }
146082 pCte = searchWith(pWith: pParse->pWith, pItem: pFrom, ppContext: &pWith);
146083 if( pCte ){
146084 sqlite3 *db = pParse->db;
146085 Table *pTab;
146086 ExprList *pEList;
146087 Select *pSel;
146088 Select *pLeft; /* Left-most SELECT statement */
146089 Select *pRecTerm; /* Left-most recursive term */
146090 int bMayRecursive; /* True if compound joined by UNION [ALL] */
146091 With *pSavedWith; /* Initial value of pParse->pWith */
146092 int iRecTab = -1; /* Cursor for recursive table */
146093 CteUse *pCteUse;
146094
146095 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
146096 ** recursive reference to CTE pCte. Leave an error in pParse and return
146097 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
146098 ** In this case, proceed. */
146099 if( pCte->zCteErr ){
146100 sqlite3ErrorMsg(pParse, zFormat: pCte->zCteErr, pCte->zName);
146101 return 2;
146102 }
146103 if( cannotBeFunction(pParse, pFrom) ) return 2;
146104
146105 assert( pFrom->pTab==0 );
146106 pTab = sqlite3DbMallocZero(db, n: sizeof(Table));
146107 if( pTab==0 ) return 2;
146108 pCteUse = pCte->pUse;
146109 if( pCteUse==0 ){
146110 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, n: sizeof(pCteUse[0]));
146111 if( pCteUse==0
146112 || sqlite3ParserAddCleanup(pParse,xCleanup: sqlite3DbFree,pPtr: pCteUse)==0
146113 ){
146114 sqlite3DbFree(db, p: pTab);
146115 return 2;
146116 }
146117 pCteUse->eM10d = pCte->eM10d;
146118 }
146119 pFrom->pTab = pTab;
146120 pTab->nTabRef = 1;
146121 pTab->zName = sqlite3DbStrDup(db, z: pCte->zName);
146122 pTab->iPKey = -1;
146123 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
146124 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
146125 pFrom->pSelect = sqlite3SelectDup(db, pDup: pCte->pSelect, flags: 0);
146126 if( db->mallocFailed ) return 2;
146127 pFrom->pSelect->selFlags |= SF_CopyCte;
146128 assert( pFrom->pSelect );
146129 if( pFrom->fg.isIndexedBy ){
146130 sqlite3ErrorMsg(pParse, zFormat: "no such index: \"%s\"", pFrom->u1.zIndexedBy);
146131 return 2;
146132 }
146133 pFrom->fg.isCte = 1;
146134 pFrom->u2.pCteUse = pCteUse;
146135 pCteUse->nUse++;
146136
146137 /* Check if this is a recursive CTE. */
146138 pRecTerm = pSel = pFrom->pSelect;
146139 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
146140 while( bMayRecursive && pRecTerm->op==pSel->op ){
146141 int i;
146142 SrcList *pSrc = pRecTerm->pSrc;
146143 assert( pRecTerm->pPrior!=0 );
146144 for(i=0; i<pSrc->nSrc; i++){
146145 SrcItem *pItem = &pSrc->a[i];
146146 if( pItem->zDatabase==0
146147 && pItem->zName!=0
146148 && 0==sqlite3StrICmp(zLeft: pItem->zName, zRight: pCte->zName)
146149 ){
146150 pItem->pTab = pTab;
146151 pTab->nTabRef++;
146152 pItem->fg.isRecursive = 1;
146153 if( pRecTerm->selFlags & SF_Recursive ){
146154 sqlite3ErrorMsg(pParse,
146155 zFormat: "multiple references to recursive table: %s", pCte->zName
146156 );
146157 return 2;
146158 }
146159 pRecTerm->selFlags |= SF_Recursive;
146160 if( iRecTab<0 ) iRecTab = pParse->nTab++;
146161 pItem->iCursor = iRecTab;
146162 }
146163 }
146164 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
146165 pRecTerm = pRecTerm->pPrior;
146166 }
146167
146168 pCte->zCteErr = "circular reference: %s";
146169 pSavedWith = pParse->pWith;
146170 pParse->pWith = pWith;
146171 if( pSel->selFlags & SF_Recursive ){
146172 int rc;
146173 assert( pRecTerm!=0 );
146174 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
146175 assert( pRecTerm->pNext!=0 );
146176 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
146177 assert( pRecTerm->pWith==0 );
146178 pRecTerm->pWith = pSel->pWith;
146179 rc = sqlite3WalkSelect(pWalker, p: pRecTerm);
146180 pRecTerm->pWith = 0;
146181 if( rc ){
146182 pParse->pWith = pSavedWith;
146183 return 2;
146184 }
146185 }else{
146186 if( sqlite3WalkSelect(pWalker, p: pSel) ){
146187 pParse->pWith = pSavedWith;
146188 return 2;
146189 }
146190 }
146191 pParse->pWith = pWith;
146192
146193 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
146194 pEList = pLeft->pEList;
146195 if( pCte->pCols ){
146196 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
146197 sqlite3ErrorMsg(pParse, zFormat: "table %s has %d values for %d columns",
146198 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
146199 );
146200 pParse->pWith = pSavedWith;
146201 return 2;
146202 }
146203 pEList = pCte->pCols;
146204 }
146205
146206 sqlite3ColumnsFromExprList(pParse, pEList, pnCol: &pTab->nCol, paCol: &pTab->aCol);
146207 if( bMayRecursive ){
146208 if( pSel->selFlags & SF_Recursive ){
146209 pCte->zCteErr = "multiple recursive references: %s";
146210 }else{
146211 pCte->zCteErr = "recursive reference in a subquery: %s";
146212 }
146213 sqlite3WalkSelect(pWalker, p: pSel);
146214 }
146215 pCte->zCteErr = 0;
146216 pParse->pWith = pSavedWith;
146217 return 1; /* Success */
146218 }
146219 return 0; /* No match */
146220}
146221#endif
146222
146223#ifndef SQLITE_OMIT_CTE
146224/*
146225** If the SELECT passed as the second argument has an associated WITH
146226** clause, pop it from the stack stored as part of the Parse object.
146227**
146228** This function is used as the xSelectCallback2() callback by
146229** sqlite3SelectExpand() when walking a SELECT tree to resolve table
146230** names and other FROM clause elements.
146231*/
146232SQLITE_PRIVATE void sqlite3SelectPopWith(Walker *pWalker, Select *p){
146233 Parse *pParse = pWalker->pParse;
146234 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
146235 With *pWith = findRightmost(p)->pWith;
146236 if( pWith!=0 ){
146237 assert( pParse->pWith==pWith || pParse->nErr );
146238 pParse->pWith = pWith->pOuter;
146239 }
146240 }
146241}
146242#endif
146243
146244/*
146245** The SrcItem structure passed as the second argument represents a
146246** sub-query in the FROM clause of a SELECT statement. This function
146247** allocates and populates the SrcItem.pTab object. If successful,
146248** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
146249** SQLITE_NOMEM.
146250*/
146251SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
146252 Select *pSel = pFrom->pSelect;
146253 Table *pTab;
146254
146255 assert( pSel );
146256 pFrom->pTab = pTab = sqlite3DbMallocZero(db: pParse->db, n: sizeof(Table));
146257 if( pTab==0 ) return SQLITE_NOMEM;
146258 pTab->nTabRef = 1;
146259 if( pFrom->zAlias ){
146260 pTab->zName = sqlite3DbStrDup(db: pParse->db, z: pFrom->zAlias);
146261 }else{
146262 pTab->zName = sqlite3MPrintf(db: pParse->db, zFormat: "%!S", pFrom);
146263 }
146264 while( pSel->pPrior ){ pSel = pSel->pPrior; }
146265 sqlite3ColumnsFromExprList(pParse, pEList: pSel->pEList,pnCol: &pTab->nCol,paCol: &pTab->aCol);
146266 pTab->iPKey = -1;
146267 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
146268#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
146269 /* The usual case - do not allow ROWID on a subquery */
146270 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
146271#else
146272 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
146273#endif
146274 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
146275}
146276
146277
146278/*
146279** Check the N SrcItem objects to the right of pBase. (N might be zero!)
146280** If any of those SrcItem objects have a USING clause containing zName
146281** then return true.
146282**
146283** If N is zero, or none of the N SrcItem objects to the right of pBase
146284** contains a USING clause, or if none of the USING clauses contain zName,
146285** then return false.
146286*/
146287static int inAnyUsingClause(
146288 const char *zName, /* Name we are looking for */
146289 SrcItem *pBase, /* The base SrcItem. Looking at pBase[1] and following */
146290 int N /* How many SrcItems to check */
146291){
146292 while( N>0 ){
146293 N--;
146294 pBase++;
146295 if( pBase->fg.isUsing==0 ) continue;
146296 if( NEVER(pBase->u3.pUsing==0) ) continue;
146297 if( sqlite3IdListIndex(pList: pBase->u3.pUsing, zName)>=0 ) return 1;
146298 }
146299 return 0;
146300}
146301
146302
146303/*
146304** This routine is a Walker callback for "expanding" a SELECT statement.
146305** "Expanding" means to do the following:
146306**
146307** (1) Make sure VDBE cursor numbers have been assigned to every
146308** element of the FROM clause.
146309**
146310** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
146311** defines FROM clause. When views appear in the FROM clause,
146312** fill pTabList->a[].pSelect with a copy of the SELECT statement
146313** that implements the view. A copy is made of the view's SELECT
146314** statement so that we can freely modify or delete that statement
146315** without worrying about messing up the persistent representation
146316** of the view.
146317**
146318** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
146319** on joins and the ON and USING clause of joins.
146320**
146321** (4) Scan the list of columns in the result set (pEList) looking
146322** for instances of the "*" operator or the TABLE.* operator.
146323** If found, expand each "*" to be every column in every table
146324** and TABLE.* to be every column in TABLE.
146325**
146326*/
146327static int selectExpander(Walker *pWalker, Select *p){
146328 Parse *pParse = pWalker->pParse;
146329 int i, j, k, rc;
146330 SrcList *pTabList;
146331 ExprList *pEList;
146332 SrcItem *pFrom;
146333 sqlite3 *db = pParse->db;
146334 Expr *pE, *pRight, *pExpr;
146335 u16 selFlags = p->selFlags;
146336 u32 elistFlags = 0;
146337
146338 p->selFlags |= SF_Expanded;
146339 if( db->mallocFailed ){
146340 return WRC_Abort;
146341 }
146342 assert( p->pSrc!=0 );
146343 if( (selFlags & SF_Expanded)!=0 ){
146344 return WRC_Prune;
146345 }
146346 if( pWalker->eCode ){
146347 /* Renumber selId because it has been copied from a view */
146348 p->selId = ++pParse->nSelect;
146349 }
146350 pTabList = p->pSrc;
146351 pEList = p->pEList;
146352 if( pParse->pWith && (p->selFlags & SF_View) ){
146353 if( p->pWith==0 ){
146354 p->pWith = (With*)sqlite3DbMallocZero(db, n: sizeof(With));
146355 if( p->pWith==0 ){
146356 return WRC_Abort;
146357 }
146358 }
146359 p->pWith->bView = 1;
146360 }
146361 sqlite3WithPush(pParse, pWith: p->pWith, bFree: 0);
146362
146363 /* Make sure cursor numbers have been assigned to all entries in
146364 ** the FROM clause of the SELECT statement.
146365 */
146366 sqlite3SrcListAssignCursors(pParse, pList: pTabList);
146367
146368 /* Look up every table named in the FROM clause of the select. If
146369 ** an entry of the FROM clause is a subquery instead of a table or view,
146370 ** then create a transient table structure to describe the subquery.
146371 */
146372 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
146373 Table *pTab;
146374 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
146375 if( pFrom->pTab ) continue;
146376 assert( pFrom->fg.isRecursive==0 );
146377 if( pFrom->zName==0 ){
146378#ifndef SQLITE_OMIT_SUBQUERY
146379 Select *pSel = pFrom->pSelect;
146380 /* A sub-query in the FROM clause of a SELECT */
146381 assert( pSel!=0 );
146382 assert( pFrom->pTab==0 );
146383 if( sqlite3WalkSelect(pWalker, p: pSel) ) return WRC_Abort;
146384 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
146385#endif
146386#ifndef SQLITE_OMIT_CTE
146387 }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){
146388 if( rc>1 ) return WRC_Abort;
146389 pTab = pFrom->pTab;
146390 assert( pTab!=0 );
146391#endif
146392 }else{
146393 /* An ordinary table or view name in the FROM clause */
146394 assert( pFrom->pTab==0 );
146395 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, flags: 0, p: pFrom);
146396 if( pTab==0 ) return WRC_Abort;
146397 if( pTab->nTabRef>=0xffff ){
146398 sqlite3ErrorMsg(pParse, zFormat: "too many references to \"%s\": max 65535",
146399 pTab->zName);
146400 pFrom->pTab = 0;
146401 return WRC_Abort;
146402 }
146403 pTab->nTabRef++;
146404 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
146405 return WRC_Abort;
146406 }
146407#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
146408 if( !IsOrdinaryTable(pTab) ){
146409 i16 nCol;
146410 u8 eCodeOrig = pWalker->eCode;
146411 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ) return WRC_Abort;
146412 assert( pFrom->pSelect==0 );
146413 if( IsView(pTab) ){
146414 if( (db->flags & SQLITE_EnableView)==0
146415 && pTab->pSchema!=db->aDb[1].pSchema
146416 ){
146417 sqlite3ErrorMsg(pParse, zFormat: "access to view \"%s\" prohibited",
146418 pTab->zName);
146419 }
146420 pFrom->pSelect = sqlite3SelectDup(db, pDup: pTab->u.view.pSelect, flags: 0);
146421 }
146422#ifndef SQLITE_OMIT_VIRTUALTABLE
146423 else if( ALWAYS(IsVirtual(pTab))
146424 && pFrom->fg.fromDDL
146425 && ALWAYS(pTab->u.vtab.p!=0)
146426 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
146427 ){
146428 sqlite3ErrorMsg(pParse, zFormat: "unsafe use of virtual table \"%s\"",
146429 pTab->zName);
146430 }
146431 assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 );
146432#endif
146433 nCol = pTab->nCol;
146434 pTab->nCol = -1;
146435 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
146436 sqlite3WalkSelect(pWalker, p: pFrom->pSelect);
146437 pWalker->eCode = eCodeOrig;
146438 pTab->nCol = nCol;
146439 }
146440#endif
146441 }
146442
146443 /* Locate the index named by the INDEXED BY clause, if any. */
146444 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
146445 return WRC_Abort;
146446 }
146447 }
146448
146449 /* Process NATURAL keywords, and ON and USING clauses of joins.
146450 */
146451 assert( db->mallocFailed==0 || pParse->nErr!=0 );
146452 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
146453 return WRC_Abort;
146454 }
146455
146456 /* For every "*" that occurs in the column list, insert the names of
146457 ** all columns in all tables. And for every TABLE.* insert the names
146458 ** of all columns in TABLE. The parser inserted a special expression
146459 ** with the TK_ASTERISK operator for each "*" that it found in the column
146460 ** list. The following code just has to locate the TK_ASTERISK
146461 ** expressions and expand each one to the list of all columns in
146462 ** all tables.
146463 **
146464 ** The first loop just checks to see if there are any "*" operators
146465 ** that need expanding.
146466 */
146467 for(k=0; k<pEList->nExpr; k++){
146468 pE = pEList->a[k].pExpr;
146469 if( pE->op==TK_ASTERISK ) break;
146470 assert( pE->op!=TK_DOT || pE->pRight!=0 );
146471 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
146472 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
146473 elistFlags |= pE->flags;
146474 }
146475 if( k<pEList->nExpr ){
146476 /*
146477 ** If we get here it means the result set contains one or more "*"
146478 ** operators that need to be expanded. Loop through each expression
146479 ** in the result set and expand them one by one.
146480 */
146481 struct ExprList_item *a = pEList->a;
146482 ExprList *pNew = 0;
146483 int flags = pParse->db->flags;
146484 int longNames = (flags & SQLITE_FullColNames)!=0
146485 && (flags & SQLITE_ShortColNames)==0;
146486
146487 for(k=0; k<pEList->nExpr; k++){
146488 pE = a[k].pExpr;
146489 elistFlags |= pE->flags;
146490 pRight = pE->pRight;
146491 assert( pE->op!=TK_DOT || pRight!=0 );
146492 if( pE->op!=TK_ASTERISK
146493 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
146494 ){
146495 /* This particular expression does not need to be expanded.
146496 */
146497 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: a[k].pExpr);
146498 if( pNew ){
146499 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
146500 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
146501 a[k].zEName = 0;
146502 }
146503 a[k].pExpr = 0;
146504 }else{
146505 /* This expression is a "*" or a "TABLE.*" and needs to be
146506 ** expanded. */
146507 int tableSeen = 0; /* Set to 1 when TABLE matches */
146508 char *zTName = 0; /* text of name of TABLE */
146509 int iErrOfst;
146510 if( pE->op==TK_DOT ){
146511 assert( pE->pLeft!=0 );
146512 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
146513 zTName = pE->pLeft->u.zToken;
146514 assert( ExprUseWOfst(pE->pLeft) );
146515 iErrOfst = pE->pRight->w.iOfst;
146516 }else{
146517 assert( ExprUseWOfst(pE) );
146518 iErrOfst = pE->w.iOfst;
146519 }
146520 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
146521 Table *pTab = pFrom->pTab; /* Table for this data source */
146522 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
146523 char *zTabName; /* AS name for this data source */
146524 const char *zSchemaName = 0; /* Schema name for this data source */
146525 int iDb; /* Schema index for this data src */
146526 IdList *pUsing; /* USING clause for pFrom[1] */
146527
146528 if( (zTabName = pFrom->zAlias)==0 ){
146529 zTabName = pTab->zName;
146530 }
146531 if( db->mallocFailed ) break;
146532 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
146533 if( pFrom->fg.isNestedFrom ){
146534 assert( pFrom->pSelect!=0 );
146535 pNestedFrom = pFrom->pSelect->pEList;
146536 assert( pNestedFrom!=0 );
146537 assert( pNestedFrom->nExpr==pTab->nCol );
146538 }else{
146539 if( zTName && sqlite3StrICmp(zLeft: zTName, zRight: zTabName)!=0 ){
146540 continue;
146541 }
146542 pNestedFrom = 0;
146543 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
146544 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
146545 }
146546 if( i+1<pTabList->nSrc
146547 && pFrom[1].fg.isUsing
146548 && (selFlags & SF_NestedFrom)!=0
146549 ){
146550 int ii;
146551 pUsing = pFrom[1].u3.pUsing;
146552 for(ii=0; ii<pUsing->nId; ii++){
146553 const char *zUName = pUsing->a[ii].zName;
146554 pRight = sqlite3Expr(db, TK_ID, zToken: zUName);
146555 sqlite3ExprSetErrorOffset(pExpr: pRight, iOfst: iErrOfst);
146556 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: pRight);
146557 if( pNew ){
146558 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
146559 assert( pX->zEName==0 );
146560 pX->zEName = sqlite3MPrintf(db,zFormat: "..%s", zUName);
146561 pX->fg.eEName = ENAME_TAB;
146562 pX->fg.bUsingTerm = 1;
146563 }
146564 }
146565 }else{
146566 pUsing = 0;
146567 }
146568 for(j=0; j<pTab->nCol; j++){
146569 char *zName = pTab->aCol[j].zCnName;
146570 struct ExprList_item *pX; /* Newly added ExprList term */
146571
146572 assert( zName );
146573 if( zTName
146574 && pNestedFrom
146575 && sqlite3MatchEName(pItem: &pNestedFrom->a[j], zCol: 0, zTab: zTName, zDb: 0)==0
146576 ){
146577 continue;
146578 }
146579
146580 /* If a column is marked as 'hidden', omit it from the expanded
146581 ** result-set list unless the SELECT has the SF_IncludeHidden
146582 ** bit set.
146583 */
146584 if( (p->selFlags & SF_IncludeHidden)==0
146585 && IsHiddenColumn(&pTab->aCol[j])
146586 ){
146587 continue;
146588 }
146589 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
146590 && zTName==0
146591 && (selFlags & (SF_NestedFrom))==0
146592 ){
146593 continue;
146594 }
146595 tableSeen = 1;
146596
146597 if( i>0 && zTName==0 && (selFlags & SF_NestedFrom)==0 ){
146598 if( pFrom->fg.isUsing
146599 && sqlite3IdListIndex(pList: pFrom->u3.pUsing, zName)>=0
146600 ){
146601 /* In a join with a USING clause, omit columns in the
146602 ** using clause from the table on the right. */
146603 continue;
146604 }
146605 }
146606 pRight = sqlite3Expr(db, TK_ID, zToken: zName);
146607 if( (pTabList->nSrc>1
146608 && ( (pFrom->fg.jointype & JT_LTORJ)==0
146609 || (selFlags & SF_NestedFrom)!=0
146610 || !inAnyUsingClause(zName,pBase: pFrom,N: pTabList->nSrc-i-1)
146611 )
146612 )
146613 || IN_RENAME_OBJECT
146614 ){
146615 Expr *pLeft;
146616 pLeft = sqlite3Expr(db, TK_ID, zToken: zTabName);
146617 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
146618 if( IN_RENAME_OBJECT && pE->pLeft ){
146619 sqlite3RenameTokenRemap(pParse, pTo: pLeft, pFrom: pE->pLeft);
146620 }
146621 if( zSchemaName ){
146622 pLeft = sqlite3Expr(db, TK_ID, zToken: zSchemaName);
146623 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight: pExpr);
146624 }
146625 }else{
146626 pExpr = pRight;
146627 }
146628 sqlite3ExprSetErrorOffset(pExpr, iOfst: iErrOfst);
146629 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr);
146630 if( pNew==0 ){
146631 break; /* OOM */
146632 }
146633 pX = &pNew->a[pNew->nExpr-1];
146634 assert( pX->zEName==0 );
146635 if( (selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
146636 if( pNestedFrom ){
146637 pX->zEName = sqlite3DbStrDup(db, z: pNestedFrom->a[j].zEName);
146638 testcase( pX->zEName==0 );
146639 }else{
146640 pX->zEName = sqlite3MPrintf(db, zFormat: "%s.%s.%s",
146641 zSchemaName, zTabName, zName);
146642 testcase( pX->zEName==0 );
146643 }
146644 pX->fg.eEName = ENAME_TAB;
146645 if( (pFrom->fg.isUsing
146646 && sqlite3IdListIndex(pList: pFrom->u3.pUsing, zName)>=0)
146647 || (pUsing && sqlite3IdListIndex(pList: pUsing, zName)>=0)
146648 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
146649 ){
146650 pX->fg.bNoExpand = 1;
146651 }
146652 }else if( longNames ){
146653 pX->zEName = sqlite3MPrintf(db, zFormat: "%s.%s", zTabName, zName);
146654 pX->fg.eEName = ENAME_NAME;
146655 }else{
146656 pX->zEName = sqlite3DbStrDup(db, z: zName);
146657 pX->fg.eEName = ENAME_NAME;
146658 }
146659 }
146660 }
146661 if( !tableSeen ){
146662 if( zTName ){
146663 sqlite3ErrorMsg(pParse, zFormat: "no such table: %s", zTName);
146664 }else{
146665 sqlite3ErrorMsg(pParse, zFormat: "no tables specified");
146666 }
146667 }
146668 }
146669 }
146670 sqlite3ExprListDelete(db, pList: pEList);
146671 p->pEList = pNew;
146672 }
146673 if( p->pEList ){
146674 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
146675 sqlite3ErrorMsg(pParse, zFormat: "too many columns in result set");
146676 return WRC_Abort;
146677 }
146678 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
146679 p->selFlags |= SF_ComplexResult;
146680 }
146681 }
146682#if TREETRACE_ENABLED
146683 if( sqlite3TreeTrace & 0x8 ){
146684 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
146685 sqlite3TreeViewSelect(0, p, 0);
146686 }
146687#endif
146688 return WRC_Continue;
146689}
146690
146691#if SQLITE_DEBUG
146692/*
146693** Always assert. This xSelectCallback2 implementation proves that the
146694** xSelectCallback2 is never invoked.
146695*/
146696SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
146697 UNUSED_PARAMETER2(NotUsed, NotUsed2);
146698 assert( 0 );
146699}
146700#endif
146701/*
146702** This routine "expands" a SELECT statement and all of its subqueries.
146703** For additional information on what it means to "expand" a SELECT
146704** statement, see the comment on the selectExpand worker callback above.
146705**
146706** Expanding a SELECT statement is the first step in processing a
146707** SELECT statement. The SELECT statement must be expanded before
146708** name resolution is performed.
146709**
146710** If anything goes wrong, an error message is written into pParse.
146711** The calling function can detect the problem by looking at pParse->nErr
146712** and/or pParse->db->mallocFailed.
146713*/
146714static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
146715 Walker w;
146716 w.xExprCallback = sqlite3ExprWalkNoop;
146717 w.pParse = pParse;
146718 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
146719 w.xSelectCallback = convertCompoundSelectToSubquery;
146720 w.xSelectCallback2 = 0;
146721 sqlite3WalkSelect(pWalker: &w, p: pSelect);
146722 }
146723 w.xSelectCallback = selectExpander;
146724 w.xSelectCallback2 = sqlite3SelectPopWith;
146725 w.eCode = 0;
146726 sqlite3WalkSelect(pWalker: &w, p: pSelect);
146727}
146728
146729
146730#ifndef SQLITE_OMIT_SUBQUERY
146731/*
146732** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
146733** interface.
146734**
146735** For each FROM-clause subquery, add Column.zType, Column.zColl, and
146736** Column.affinity information to the Table structure that represents
146737** the result set of that subquery.
146738**
146739** The Table structure that represents the result set was constructed
146740** by selectExpander() but the type and collation and affinity information
146741** was omitted at that point because identifiers had not yet been resolved.
146742** This routine is called after identifier resolution.
146743*/
146744static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
146745 Parse *pParse;
146746 int i;
146747 SrcList *pTabList;
146748 SrcItem *pFrom;
146749
146750 assert( p->selFlags & SF_Resolved );
146751 if( p->selFlags & SF_HasTypeInfo ) return;
146752 p->selFlags |= SF_HasTypeInfo;
146753 pParse = pWalker->pParse;
146754 pTabList = p->pSrc;
146755 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
146756 Table *pTab = pFrom->pTab;
146757 assert( pTab!=0 );
146758 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
146759 /* A sub-query in the FROM clause of a SELECT */
146760 Select *pSel = pFrom->pSelect;
146761 if( pSel ){
146762 sqlite3SubqueryColumnTypes(pParse, pTab, pSelect: pSel, SQLITE_AFF_NONE);
146763 }
146764 }
146765 }
146766}
146767#endif
146768
146769
146770/*
146771** This routine adds datatype and collating sequence information to
146772** the Table structures of all FROM-clause subqueries in a
146773** SELECT statement.
146774**
146775** Use this routine after name resolution.
146776*/
146777static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
146778#ifndef SQLITE_OMIT_SUBQUERY
146779 Walker w;
146780 w.xSelectCallback = sqlite3SelectWalkNoop;
146781 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
146782 w.xExprCallback = sqlite3ExprWalkNoop;
146783 w.pParse = pParse;
146784 sqlite3WalkSelect(pWalker: &w, p: pSelect);
146785#endif
146786}
146787
146788
146789/*
146790** This routine sets up a SELECT statement for processing. The
146791** following is accomplished:
146792**
146793** * VDBE Cursor numbers are assigned to all FROM-clause terms.
146794** * Ephemeral Table objects are created for all FROM-clause subqueries.
146795** * ON and USING clauses are shifted into WHERE statements
146796** * Wildcards "*" and "TABLE.*" in result sets are expanded.
146797** * Identifiers in expression are matched to tables.
146798**
146799** This routine acts recursively on all subqueries within the SELECT.
146800*/
146801SQLITE_PRIVATE void sqlite3SelectPrep(
146802 Parse *pParse, /* The parser context */
146803 Select *p, /* The SELECT statement being coded. */
146804 NameContext *pOuterNC /* Name context for container */
146805){
146806 assert( p!=0 || pParse->db->mallocFailed );
146807 assert( pParse->db->pParse==pParse );
146808 if( pParse->db->mallocFailed ) return;
146809 if( p->selFlags & SF_HasTypeInfo ) return;
146810 sqlite3SelectExpand(pParse, pSelect: p);
146811 if( pParse->nErr ) return;
146812 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
146813 if( pParse->nErr ) return;
146814 sqlite3SelectAddTypeInfo(pParse, pSelect: p);
146815}
146816
146817#if TREETRACE_ENABLED
146818/*
146819** Display all information about an AggInfo object
146820*/
146821static void printAggInfo(AggInfo *pAggInfo){
146822 int ii;
146823 for(ii=0; ii<pAggInfo->nColumn; ii++){
146824 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
146825 sqlite3DebugPrintf(
146826 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
146827 " iSorterColumn=%d %s\n",
146828 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
146829 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
146830 pCol->iSorterColumn,
146831 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
146832 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
146833 }
146834 for(ii=0; ii<pAggInfo->nFunc; ii++){
146835 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
146836 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
146837 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
146838 }
146839}
146840#endif /* TREETRACE_ENABLED */
146841
146842/*
146843** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
146844** entries for columns that are arguments to aggregate functions but which
146845** are not otherwise used.
146846**
146847** The aCol[] entries in AggInfo prior to nAccumulator are columns that
146848** are referenced outside of aggregate functions. These might be columns
146849** that are part of the GROUP by clause, for example. Other database engines
146850** would throw an error if there is a column reference that is not in the
146851** GROUP BY clause and that is not part of an aggregate function argument.
146852** But SQLite allows this.
146853**
146854** The aCol[] entries beginning with the aCol[nAccumulator] and following
146855** are column references that are used exclusively as arguments to
146856** aggregate functions. This routine is responsible for computing
146857** (or recomputing) those aCol[] entries.
146858*/
146859static void analyzeAggFuncArgs(
146860 AggInfo *pAggInfo,
146861 NameContext *pNC
146862){
146863 int i;
146864 assert( pAggInfo!=0 );
146865 assert( pAggInfo->iFirstReg==0 );
146866 pNC->ncFlags |= NC_InAggFunc;
146867 for(i=0; i<pAggInfo->nFunc; i++){
146868 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
146869 assert( ExprUseXList(pExpr) );
146870 sqlite3ExprAnalyzeAggList(pNC, pList: pExpr->x.pList);
146871#ifndef SQLITE_OMIT_WINDOWFUNC
146872 assert( !IsWindowFunc(pExpr) );
146873 if( ExprHasProperty(pExpr, EP_WinFunc) ){
146874 sqlite3ExprAnalyzeAggregates(pNC, pExpr: pExpr->y.pWin->pFilter);
146875 }
146876#endif
146877 }
146878 pNC->ncFlags &= ~NC_InAggFunc;
146879}
146880
146881/*
146882** An index on expressions is being used in the inner loop of an
146883** aggregate query with a GROUP BY clause. This routine attempts
146884** to adjust the AggInfo object to take advantage of index and to
146885** perhaps use the index as a covering index.
146886**
146887*/
146888static void optimizeAggregateUseOfIndexedExpr(
146889 Parse *pParse, /* Parsing context */
146890 Select *pSelect, /* The SELECT statement being processed */
146891 AggInfo *pAggInfo, /* The aggregate info */
146892 NameContext *pNC /* Name context used to resolve agg-func args */
146893){
146894 assert( pAggInfo->iFirstReg==0 );
146895 assert( pSelect!=0 );
146896 assert( pSelect->pGroupBy!=0 );
146897 pAggInfo->nColumn = pAggInfo->nAccumulator;
146898 if( ALWAYS(pAggInfo->nSortingColumn>0) ){
146899 int mx = pSelect->pGroupBy->nExpr - 1;
146900 int j, k;
146901 for(j=0; j<pAggInfo->nColumn; j++){
146902 k = pAggInfo->aCol[j].iSorterColumn;
146903 if( k>mx ) mx = k;
146904 }
146905 pAggInfo->nSortingColumn = mx+1;
146906 }
146907 analyzeAggFuncArgs(pAggInfo, pNC);
146908#if TREETRACE_ENABLED
146909 if( sqlite3TreeTrace & 0x20 ){
146910 IndexedExpr *pIEpr;
146911 TREETRACE(0x20, pParse, pSelect,
146912 ("AggInfo (possibly) adjusted for Indexed Exprs\n"));
146913 sqlite3TreeViewSelect(0, pSelect, 0);
146914 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
146915 printf("data-cursor=%d index={%d,%d}\n",
146916 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
146917 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
146918 }
146919 printAggInfo(pAggInfo);
146920 }
146921#else
146922 UNUSED_PARAMETER(pSelect);
146923 UNUSED_PARAMETER(pParse);
146924#endif
146925}
146926
146927/*
146928** Walker callback for aggregateConvertIndexedExprRefToColumn().
146929*/
146930static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){
146931 AggInfo *pAggInfo;
146932 struct AggInfo_col *pCol;
146933 UNUSED_PARAMETER(pWalker);
146934 if( pExpr->pAggInfo==0 ) return WRC_Continue;
146935 if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
146936 if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
146937 if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
146938 pAggInfo = pExpr->pAggInfo;
146939 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue;
146940 assert( pExpr->iAgg>=0 );
146941 pCol = &pAggInfo->aCol[pExpr->iAgg];
146942 pExpr->op = TK_AGG_COLUMN;
146943 pExpr->iTable = pCol->iTable;
146944 pExpr->iColumn = pCol->iColumn;
146945 ExprClearProperty(pExpr, EP_Skip|EP_Collate|EP_Unlikely);
146946 return WRC_Prune;
146947}
146948
146949/*
146950** Convert every pAggInfo->aFunc[].pExpr such that any node within
146951** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
146952** opcode.
146953*/
146954static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){
146955 int i;
146956 Walker w;
146957 memset(s: &w, c: 0, n: sizeof(w));
146958 w.xExprCallback = aggregateIdxEprRefToColCallback;
146959 for(i=0; i<pAggInfo->nFunc; i++){
146960 sqlite3WalkExpr(pWalker: &w, pExpr: pAggInfo->aFunc[i].pFExpr);
146961 }
146962}
146963
146964
146965/*
146966** Allocate a block of registers so that there is one register for each
146967** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
146968** register in this block is stored in pAggInfo->iFirstReg.
146969**
146970** This routine may only be called once for each AggInfo object. Prior
146971** to calling this routine:
146972**
146973** * The aCol[] and aFunc[] arrays may be modified
146974** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used
146975**
146976** After calling this routine:
146977**
146978** * The aCol[] and aFunc[] arrays are fixed
146979** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used
146980**
146981*/
146982static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){
146983 assert( pAggInfo!=0 );
146984 assert( pAggInfo->iFirstReg==0 );
146985 pAggInfo->iFirstReg = pParse->nMem + 1;
146986 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
146987}
146988
146989/*
146990** Reset the aggregate accumulator.
146991**
146992** The aggregate accumulator is a set of memory cells that hold
146993** intermediate results while calculating an aggregate. This
146994** routine generates code that stores NULLs in all of those memory
146995** cells.
146996*/
146997static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
146998 Vdbe *v = pParse->pVdbe;
146999 int i;
147000 struct AggInfo_func *pFunc;
147001 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
147002 assert( pAggInfo->iFirstReg>0 );
147003 assert( pParse->db->pParse==pParse );
147004 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
147005 if( nReg==0 ) return;
147006 if( pParse->nErr ) return;
147007 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: pAggInfo->iFirstReg,
147008 p3: pAggInfo->iFirstReg+nReg-1);
147009 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
147010 if( pFunc->iDistinct>=0 ){
147011 Expr *pE = pFunc->pFExpr;
147012 assert( ExprUseXList(pE) );
147013 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
147014 sqlite3ErrorMsg(pParse, zFormat: "DISTINCT aggregates must have exactly one "
147015 "argument");
147016 pFunc->iDistinct = -1;
147017 }else{
147018 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pE->x.pList,iStart: 0,nExtra: 0);
147019 pFunc->iDistAddr = sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral,
147020 p1: pFunc->iDistinct, p2: 0, p3: 0, zP4: (char*)pKeyInfo, P4_KEYINFO);
147021 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
147022 pFunc->pFunc->zName));
147023 }
147024 }
147025 }
147026}
147027
147028/*
147029** Invoke the OP_AggFinalize opcode for every aggregate function
147030** in the AggInfo structure.
147031*/
147032static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
147033 Vdbe *v = pParse->pVdbe;
147034 int i;
147035 struct AggInfo_func *pF;
147036 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
147037 ExprList *pList;
147038 assert( ExprUseXList(pF->pFExpr) );
147039 pList = pF->pFExpr->x.pList;
147040 sqlite3VdbeAddOp2(p: v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i),
147041 p2: pList ? pList->nExpr : 0);
147042 sqlite3VdbeAppendP4(p: v, pP4: pF->pFunc, P4_FUNCDEF);
147043 }
147044}
147045
147046
147047/*
147048** Generate code that will update the accumulator memory cells for an
147049** aggregate based on the current cursor position.
147050**
147051** If regAcc is non-zero and there are no min() or max() aggregates
147052** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
147053** registers if register regAcc contains 0. The caller will take care
147054** of setting and clearing regAcc.
147055*/
147056static void updateAccumulator(
147057 Parse *pParse,
147058 int regAcc,
147059 AggInfo *pAggInfo,
147060 int eDistinctType
147061){
147062 Vdbe *v = pParse->pVdbe;
147063 int i;
147064 int regHit = 0;
147065 int addrHitTest = 0;
147066 struct AggInfo_func *pF;
147067 struct AggInfo_col *pC;
147068
147069 assert( pAggInfo->iFirstReg>0 );
147070 if( pParse->nErr ) return;
147071 pAggInfo->directMode = 1;
147072 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
147073 int nArg;
147074 int addrNext = 0;
147075 int regAgg;
147076 ExprList *pList;
147077 assert( ExprUseXList(pF->pFExpr) );
147078 assert( !IsWindowFunc(pF->pFExpr) );
147079 pList = pF->pFExpr->x.pList;
147080 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
147081 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
147082 if( pAggInfo->nAccumulator
147083 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
147084 && regAcc
147085 ){
147086 /* If regAcc==0, there there exists some min() or max() function
147087 ** without a FILTER clause that will ensure the magnet registers
147088 ** are populated. */
147089 if( regHit==0 ) regHit = ++pParse->nMem;
147090 /* If this is the first row of the group (regAcc contains 0), clear the
147091 ** "magnet" register regHit so that the accumulator registers
147092 ** are populated if the FILTER clause jumps over the the
147093 ** invocation of min() or max() altogether. Or, if this is not
147094 ** the first row (regAcc contains 1), set the magnet register so that
147095 ** the accumulators are not populated unless the min()/max() is invoked
147096 ** and indicates that they should be. */
147097 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regAcc, p2: regHit);
147098 }
147099 addrNext = sqlite3VdbeMakeLabel(pParse);
147100 sqlite3ExprIfFalse(pParse, pExpr: pFilter, dest: addrNext, SQLITE_JUMPIFNULL);
147101 }
147102 if( pList ){
147103 nArg = pList->nExpr;
147104 regAgg = sqlite3GetTempRange(pParse, nReg: nArg);
147105 sqlite3ExprCodeExprList(pParse, pList, target: regAgg, srcReg: 0, SQLITE_ECEL_DUP);
147106 }else{
147107 nArg = 0;
147108 regAgg = 0;
147109 }
147110 if( pF->iDistinct>=0 && pList ){
147111 if( addrNext==0 ){
147112 addrNext = sqlite3VdbeMakeLabel(pParse);
147113 }
147114 pF->iDistinct = codeDistinct(pParse, eTnctType: eDistinctType,
147115 iTab: pF->iDistinct, addrRepeat: addrNext, pEList: pList, regElem: regAgg);
147116 }
147117 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
147118 CollSeq *pColl = 0;
147119 struct ExprList_item *pItem;
147120 int j;
147121 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
147122 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
147123 pColl = sqlite3ExprCollSeq(pParse, pExpr: pItem->pExpr);
147124 }
147125 if( !pColl ){
147126 pColl = pParse->db->pDfltColl;
147127 }
147128 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
147129 sqlite3VdbeAddOp4(p: v, OP_CollSeq, p1: regHit, p2: 0, p3: 0, zP4: (char *)pColl, P4_COLLSEQ);
147130 }
147131 sqlite3VdbeAddOp3(p: v, OP_AggStep, p1: 0, p2: regAgg, AggInfoFuncReg(pAggInfo,i));
147132 sqlite3VdbeAppendP4(p: v, pP4: pF->pFunc, P4_FUNCDEF);
147133 sqlite3VdbeChangeP5(p: v, p5: (u8)nArg);
147134 sqlite3ReleaseTempRange(pParse, iReg: regAgg, nReg: nArg);
147135 if( addrNext ){
147136 sqlite3VdbeResolveLabel(v, x: addrNext);
147137 }
147138 }
147139 if( regHit==0 && pAggInfo->nAccumulator ){
147140 regHit = regAcc;
147141 }
147142 if( regHit ){
147143 addrHitTest = sqlite3VdbeAddOp1(p: v, OP_If, p1: regHit); VdbeCoverage(v);
147144 }
147145 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
147146 sqlite3ExprCode(pParse, pExpr: pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
147147 }
147148
147149 pAggInfo->directMode = 0;
147150 if( addrHitTest ){
147151 sqlite3VdbeJumpHereOrPopInst(p: v, addr: addrHitTest);
147152 }
147153}
147154
147155/*
147156** Add a single OP_Explain instruction to the VDBE to explain a simple
147157** count(*) query ("SELECT count(*) FROM pTab").
147158*/
147159#ifndef SQLITE_OMIT_EXPLAIN
147160static void explainSimpleCount(
147161 Parse *pParse, /* Parse context */
147162 Table *pTab, /* Table being queried */
147163 Index *pIdx /* Index used to optimize scan, or NULL */
147164){
147165 if( pParse->explain==2 ){
147166 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
147167 sqlite3VdbeExplain(pParse, bPush: 0, zFmt: "SCAN %s%s%s",
147168 pTab->zName,
147169 bCover ? " USING COVERING INDEX " : "",
147170 bCover ? pIdx->zName : ""
147171 );
147172 }
147173}
147174#else
147175# define explainSimpleCount(a,b,c)
147176#endif
147177
147178/*
147179** sqlite3WalkExpr() callback used by havingToWhere().
147180**
147181** If the node passed to the callback is a TK_AND node, return
147182** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
147183**
147184** Otherwise, return WRC_Prune. In this case, also check if the
147185** sub-expression matches the criteria for being moved to the WHERE
147186** clause. If so, add it to the WHERE clause and replace the sub-expression
147187** within the HAVING expression with a constant "1".
147188*/
147189static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
147190 if( pExpr->op!=TK_AND ){
147191 Select *pS = pWalker->u.pSelect;
147192 /* This routine is called before the HAVING clause of the current
147193 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
147194 ** here, it indicates that the expression is a correlated reference to a
147195 ** column from an outer aggregate query, or an aggregate function that
147196 ** belongs to an outer query. Do not move the expression to the WHERE
147197 ** clause in this obscure case, as doing so may corrupt the outer Select
147198 ** statements AggInfo structure. */
147199 if( sqlite3ExprIsConstantOrGroupBy(pParse: pWalker->pParse, p: pExpr, pGroupBy: pS->pGroupBy)
147200 && ExprAlwaysFalse(pExpr)==0
147201 && pExpr->pAggInfo==0
147202 ){
147203 sqlite3 *db = pWalker->pParse->db;
147204 Expr *pNew = sqlite3Expr(db, TK_INTEGER, zToken: "1");
147205 if( pNew ){
147206 Expr *pWhere = pS->pWhere;
147207 SWAP(Expr, *pNew, *pExpr);
147208 pNew = sqlite3ExprAnd(pParse: pWalker->pParse, pLeft: pWhere, pRight: pNew);
147209 pS->pWhere = pNew;
147210 pWalker->eCode = 1;
147211 }
147212 }
147213 return WRC_Prune;
147214 }
147215 return WRC_Continue;
147216}
147217
147218/*
147219** Transfer eligible terms from the HAVING clause of a query, which is
147220** processed after grouping, to the WHERE clause, which is processed before
147221** grouping. For example, the query:
147222**
147223** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
147224**
147225** can be rewritten as:
147226**
147227** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
147228**
147229** A term of the HAVING expression is eligible for transfer if it consists
147230** entirely of constants and expressions that are also GROUP BY terms that
147231** use the "BINARY" collation sequence.
147232*/
147233static void havingToWhere(Parse *pParse, Select *p){
147234 Walker sWalker;
147235 memset(s: &sWalker, c: 0, n: sizeof(sWalker));
147236 sWalker.pParse = pParse;
147237 sWalker.xExprCallback = havingToWhereExprCb;
147238 sWalker.u.pSelect = p;
147239 sqlite3WalkExpr(pWalker: &sWalker, pExpr: p->pHaving);
147240#if TREETRACE_ENABLED
147241 if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
147242 TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
147243 sqlite3TreeViewSelect(0, p, 0);
147244 }
147245#endif
147246}
147247
147248/*
147249** Check to see if the pThis entry of pTabList is a self-join of another view.
147250** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
147251** but stopping before iEnd.
147252**
147253** If pThis is a self-join, then return the SrcItem for the first other
147254** instance of that view found. If pThis is not a self-join then return 0.
147255*/
147256static SrcItem *isSelfJoinView(
147257 SrcList *pTabList, /* Search for self-joins in this FROM clause */
147258 SrcItem *pThis, /* Search for prior reference to this subquery */
147259 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
147260){
147261 SrcItem *pItem;
147262 assert( pThis->pSelect!=0 );
147263 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
147264 while( iFirst<iEnd ){
147265 Select *pS1;
147266 pItem = &pTabList->a[iFirst++];
147267 if( pItem->pSelect==0 ) continue;
147268 if( pItem->fg.viaCoroutine ) continue;
147269 if( pItem->zName==0 ) continue;
147270 assert( pItem->pTab!=0 );
147271 assert( pThis->pTab!=0 );
147272 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
147273 if( sqlite3_stricmp(zLeft: pItem->zName, zRight: pThis->zName)!=0 ) continue;
147274 pS1 = pItem->pSelect;
147275 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
147276 /* The query flattener left two different CTE tables with identical
147277 ** names in the same FROM clause. */
147278 continue;
147279 }
147280 if( pItem->pSelect->selFlags & SF_PushDown ){
147281 /* The view was modified by some other optimization such as
147282 ** pushDownWhereTerms() */
147283 continue;
147284 }
147285 return pItem;
147286 }
147287 return 0;
147288}
147289
147290/*
147291** Deallocate a single AggInfo object
147292*/
147293static void agginfoFree(sqlite3 *db, AggInfo *p){
147294 sqlite3DbFree(db, p: p->aCol);
147295 sqlite3DbFree(db, p: p->aFunc);
147296 sqlite3DbFreeNN(db, p);
147297}
147298
147299/*
147300** Attempt to transform a query of the form
147301**
147302** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
147303**
147304** Into this:
147305**
147306** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
147307**
147308** The transformation only works if all of the following are true:
147309**
147310** * The subquery is a UNION ALL of two or more terms
147311** * The subquery does not have a LIMIT clause
147312** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
147313** * The outer query is a simple count(*) with no WHERE clause or other
147314** extraneous syntax.
147315**
147316** Return TRUE if the optimization is undertaken.
147317*/
147318static int countOfViewOptimization(Parse *pParse, Select *p){
147319 Select *pSub, *pPrior;
147320 Expr *pExpr;
147321 Expr *pCount;
147322 sqlite3 *db;
147323 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
147324 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
147325 if( p->pWhere ) return 0;
147326 if( p->pHaving ) return 0;
147327 if( p->pGroupBy ) return 0;
147328 if( p->pOrderBy ) return 0;
147329 pExpr = p->pEList->a[0].pExpr;
147330 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
147331 assert( ExprUseUToken(pExpr) );
147332 if( sqlite3_stricmp(zLeft: pExpr->u.zToken,zRight: "count") ) return 0; /* Is count() */
147333 assert( ExprUseXList(pExpr) );
147334 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
147335 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
147336 if( ExprHasProperty(pExpr, EP_WinFunc) ) return 0;/* Not a window function */
147337 pSub = p->pSrc->a[0].pSelect;
147338 if( pSub==0 ) return 0; /* The FROM is a subquery */
147339 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
147340 if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */
147341 do{
147342 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
147343 if( pSub->pWhere ) return 0; /* No WHERE clause */
147344 if( pSub->pLimit ) return 0; /* No LIMIT clause */
147345 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
147346 assert( pSub->pHaving==0 ); /* Due to the previous */
147347 pSub = pSub->pPrior; /* Repeat over compound */
147348 }while( pSub );
147349
147350 /* If we reach this point then it is OK to perform the transformation */
147351
147352 db = pParse->db;
147353 pCount = pExpr;
147354 pExpr = 0;
147355 pSub = p->pSrc->a[0].pSelect;
147356 p->pSrc->a[0].pSelect = 0;
147357 sqlite3SrcListDelete(db, pList: p->pSrc);
147358 p->pSrc = sqlite3DbMallocZero(db: pParse->db, n: sizeof(*p->pSrc));
147359 while( pSub ){
147360 Expr *pTerm;
147361 pPrior = pSub->pPrior;
147362 pSub->pPrior = 0;
147363 pSub->pNext = 0;
147364 pSub->selFlags |= SF_Aggregate;
147365 pSub->selFlags &= ~SF_Compound;
147366 pSub->nSelectRow = 0;
147367 sqlite3ExprListDelete(db, pList: pSub->pEList);
147368 pTerm = pPrior ? sqlite3ExprDup(db, p: pCount, flags: 0) : pCount;
147369 pSub->pEList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: pTerm);
147370 pTerm = sqlite3PExpr(pParse, TK_SELECT, pLeft: 0, pRight: 0);
147371 sqlite3PExprAddSelect(pParse, pExpr: pTerm, pSelect: pSub);
147372 if( pExpr==0 ){
147373 pExpr = pTerm;
147374 }else{
147375 pExpr = sqlite3PExpr(pParse, TK_PLUS, pLeft: pTerm, pRight: pExpr);
147376 }
147377 pSub = pPrior;
147378 }
147379 p->pEList->a[0].pExpr = pExpr;
147380 p->selFlags &= ~SF_Aggregate;
147381
147382#if TREETRACE_ENABLED
147383 if( sqlite3TreeTrace & 0x200 ){
147384 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
147385 sqlite3TreeViewSelect(0, p, 0);
147386 }
147387#endif
147388 return 1;
147389}
147390
147391/*
147392** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
147393** as pSrcItem but has the same alias as p0, then return true.
147394** Otherwise return false.
147395*/
147396static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){
147397 int i;
147398 for(i=0; i<pSrc->nSrc; i++){
147399 SrcItem *p1 = &pSrc->a[i];
147400 if( p1==p0 ) continue;
147401 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(zLeft: p0->zAlias, zRight: p1->zAlias) ){
147402 return 1;
147403 }
147404 if( p1->pSelect
147405 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
147406 && sameSrcAlias(p0, pSrc: p1->pSelect->pSrc)
147407 ){
147408 return 1;
147409 }
147410 }
147411 return 0;
147412}
147413
147414/*
147415** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
147416** be implemented as a co-routine. The i-th entry is guaranteed to be
147417** a subquery.
147418**
147419** The subquery is implemented as a co-routine if all of the following are
147420** true:
147421**
147422** (1) The subquery will likely be implemented in the outer loop of
147423** the query. This will be the case if any one of the following
147424** conditions hold:
147425** (a) The subquery is the only term in the FROM clause
147426** (b) The subquery is the left-most term and a CROSS JOIN or similar
147427** requires it to be the outer loop
147428** (c) All of the following are true:
147429** (i) The subquery is the left-most subquery in the FROM clause
147430** (ii) There is nothing that would prevent the subquery from
147431** being used as the outer loop if the sqlite3WhereBegin()
147432** routine nominates it to that position.
147433** (iii) The query is not a UPDATE ... FROM
147434** (2) The subquery is not a CTE that should be materialized because
147435** (a) the AS MATERIALIZED keyword is used, or
147436** (b) the CTE is used multiple times and does not have the
147437** NOT MATERIALIZED keyword
147438** (3) The subquery is not part of a left operand for a RIGHT JOIN
147439** (4) The SQLITE_Coroutine optimization disable flag is not set
147440** (5) The subquery is not self-joined
147441*/
147442static int fromClauseTermCanBeCoroutine(
147443 Parse *pParse, /* Parsing context */
147444 SrcList *pTabList, /* FROM clause */
147445 int i, /* Which term of the FROM clause holds the subquery */
147446 int selFlags /* Flags on the SELECT statement */
147447){
147448 SrcItem *pItem = &pTabList->a[i];
147449 if( pItem->fg.isCte ){
147450 const CteUse *pCteUse = pItem->u2.pCteUse;
147451 if( pCteUse->eM10d==M10d_Yes ) return 0; /* (2a) */
147452 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No ) return 0; /* (2b) */
147453 }
147454 if( pTabList->a[0].fg.jointype & JT_LTORJ ) return 0; /* (3) */
147455 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines) ) return 0; /* (4) */
147456 if( isSelfJoinView(pTabList, pThis: pItem, iFirst: i+1, iEnd: pTabList->nSrc)!=0 ){
147457 return 0; /* (5) */
147458 }
147459 if( i==0 ){
147460 if( pTabList->nSrc==1 ) return 1; /* (1a) */
147461 if( pTabList->a[1].fg.jointype & JT_CROSS ) return 1; /* (1b) */
147462 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
147463 return 1;
147464 }
147465 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
147466 while( 1 /*exit-by-break*/ ){
147467 if( pItem->fg.jointype & (JT_OUTER|JT_CROSS) ) return 0; /* (1c-ii) */
147468 if( i==0 ) break;
147469 i--;
147470 pItem--;
147471 if( pItem->pSelect!=0 ) return 0; /* (1c-i) */
147472 }
147473 return 1;
147474}
147475
147476/*
147477** Generate code for the SELECT statement given in the p argument.
147478**
147479** The results are returned according to the SelectDest structure.
147480** See comments in sqliteInt.h for further information.
147481**
147482** This routine returns the number of errors. If any errors are
147483** encountered, then an appropriate error message is left in
147484** pParse->zErrMsg.
147485**
147486** This routine does NOT free the Select structure passed in. The
147487** calling function needs to do that.
147488*/
147489SQLITE_PRIVATE int sqlite3Select(
147490 Parse *pParse, /* The parser context */
147491 Select *p, /* The SELECT statement being coded. */
147492 SelectDest *pDest /* What to do with the query results */
147493){
147494 int i, j; /* Loop counters */
147495 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
147496 Vdbe *v; /* The virtual machine under construction */
147497 int isAgg; /* True for select lists like "count(*)" */
147498 ExprList *pEList = 0; /* List of columns to extract. */
147499 SrcList *pTabList; /* List of tables to select from */
147500 Expr *pWhere; /* The WHERE clause. May be NULL */
147501 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
147502 Expr *pHaving; /* The HAVING clause. May be NULL */
147503 AggInfo *pAggInfo = 0; /* Aggregate information */
147504 int rc = 1; /* Value to return from this function */
147505 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
147506 SortCtx sSort; /* Info on how to code the ORDER BY clause */
147507 int iEnd; /* Address of the end of the query */
147508 sqlite3 *db; /* The database connection */
147509 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
147510 u8 minMaxFlag; /* Flag for min/max queries */
147511
147512 db = pParse->db;
147513 assert( pParse==db->pParse );
147514 v = sqlite3GetVdbe(pParse);
147515 if( p==0 || pParse->nErr ){
147516 return 1;
147517 }
147518 assert( db->mallocFailed==0 );
147519 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, zArg1: 0, zArg2: 0, zArg3: 0) ) return 1;
147520#if TREETRACE_ENABLED
147521 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
147522 if( sqlite3TreeTrace & 0x10000 ){
147523 if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
147524 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
147525 __FILE__, __LINE__);
147526 }
147527 sqlite3ShowSelect(p);
147528 }
147529#endif
147530
147531 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
147532 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
147533 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
147534 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
147535 if( IgnorableDistinct(pDest) ){
147536 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
147537 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
147538 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
147539 /* All of these destinations are also able to ignore the ORDER BY clause */
147540 if( p->pOrderBy ){
147541#if TREETRACE_ENABLED
147542 TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
147543 if( sqlite3TreeTrace & 0x800 ){
147544 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
147545 }
147546#endif
147547 sqlite3ParserAddCleanup(pParse,
147548 xCleanup: (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
147549 pPtr: p->pOrderBy);
147550 testcase( pParse->earlyCleanup );
147551 p->pOrderBy = 0;
147552 }
147553 p->selFlags &= ~SF_Distinct;
147554 p->selFlags |= SF_NoopOrderBy;
147555 }
147556 sqlite3SelectPrep(pParse, p, pOuterNC: 0);
147557 if( pParse->nErr ){
147558 goto select_end;
147559 }
147560 assert( db->mallocFailed==0 );
147561 assert( p->pEList!=0 );
147562#if TREETRACE_ENABLED
147563 if( sqlite3TreeTrace & 0x10 ){
147564 TREETRACE(0x10,pParse,p, ("after name resolution:\n"));
147565 sqlite3TreeViewSelect(0, p, 0);
147566 }
147567#endif
147568
147569 /* If the SF_UFSrcCheck flag is set, then this function is being called
147570 ** as part of populating the temp table for an UPDATE...FROM statement.
147571 ** In this case, it is an error if the target object (pSrc->a[0]) name
147572 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
147573 **
147574 ** Postgres disallows this case too. The reason is that some other
147575 ** systems handle this case differently, and not all the same way,
147576 ** which is just confusing. To avoid this, we follow PG's lead and
147577 ** disallow it altogether. */
147578 if( p->selFlags & SF_UFSrcCheck ){
147579 SrcItem *p0 = &p->pSrc->a[0];
147580 if( sameSrcAlias(p0, pSrc: p->pSrc) ){
147581 sqlite3ErrorMsg(pParse,
147582 zFormat: "target object/alias may not appear in FROM clause: %s",
147583 p0->zAlias ? p0->zAlias : p0->pTab->zName
147584 );
147585 goto select_end;
147586 }
147587
147588 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
147589 ** and leaving this flag set can cause errors if a compound sub-query
147590 ** in p->pSrc is flattened into this query and this function called
147591 ** again as part of compound SELECT processing. */
147592 p->selFlags &= ~SF_UFSrcCheck;
147593 }
147594
147595 if( pDest->eDest==SRT_Output ){
147596 sqlite3GenerateColumnNames(pParse, pSelect: p);
147597 }
147598
147599#ifndef SQLITE_OMIT_WINDOWFUNC
147600 if( sqlite3WindowRewrite(pParse, p) ){
147601 assert( pParse->nErr );
147602 goto select_end;
147603 }
147604#if TREETRACE_ENABLED
147605 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
147606 TREETRACE(0x40,pParse,p, ("after window rewrite:\n"));
147607 sqlite3TreeViewSelect(0, p, 0);
147608 }
147609#endif
147610#endif /* SQLITE_OMIT_WINDOWFUNC */
147611 pTabList = p->pSrc;
147612 isAgg = (p->selFlags & SF_Aggregate)!=0;
147613 memset(s: &sSort, c: 0, n: sizeof(sSort));
147614 sSort.pOrderBy = p->pOrderBy;
147615
147616 /* Try to do various optimizations (flattening subqueries, and strength
147617 ** reduction of join operators) in the FROM clause up into the main query
147618 */
147619#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147620 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
147621 SrcItem *pItem = &pTabList->a[i];
147622 Select *pSub = pItem->pSelect;
147623 Table *pTab = pItem->pTab;
147624
147625 /* The expander should have already created transient Table objects
147626 ** even for FROM clause elements such as subqueries that do not correspond
147627 ** to a real table */
147628 assert( pTab!=0 );
147629
147630 /* Try to simplify joins:
147631 **
147632 ** LEFT JOIN -> JOIN
147633 ** RIGHT JOIN -> JOIN
147634 ** FULL JOIN -> RIGHT JOIN
147635 **
147636 ** If terms of the i-th table are used in the WHERE clause in such a
147637 ** way that the i-th table cannot be the NULL row of a join, then
147638 ** perform the appropriate simplification. This is called
147639 ** "OUTER JOIN strength reduction" in the SQLite documentation.
147640 */
147641 if( (pItem->fg.jointype & (JT_LEFT|JT_LTORJ))!=0
147642 && sqlite3ExprImpliesNonNullRow(p: p->pWhere, iTab: pItem->iCursor,
147643 isRJ: pItem->fg.jointype & JT_LTORJ)
147644 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
147645 ){
147646 if( pItem->fg.jointype & JT_LEFT ){
147647 if( pItem->fg.jointype & JT_RIGHT ){
147648 TREETRACE(0x1000,pParse,p,
147649 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i));
147650 pItem->fg.jointype &= ~JT_LEFT;
147651 }else{
147652 TREETRACE(0x1000,pParse,p,
147653 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
147654 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
147655 }
147656 }
147657 if( pItem->fg.jointype & JT_LTORJ ){
147658 for(j=i+1; j<pTabList->nSrc; j++){
147659 SrcItem *pI2 = &pTabList->a[j];
147660 if( pI2->fg.jointype & JT_RIGHT ){
147661 if( pI2->fg.jointype & JT_LEFT ){
147662 TREETRACE(0x1000,pParse,p,
147663 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j));
147664 pI2->fg.jointype &= ~JT_RIGHT;
147665 }else{
147666 TREETRACE(0x1000,pParse,p,
147667 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
147668 pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
147669 }
147670 }
147671 }
147672 for(j=pTabList->nSrc-1; j>=i; j--){
147673 pTabList->a[j].fg.jointype &= ~JT_LTORJ;
147674 if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
147675 }
147676 }
147677 assert( pItem->iCursor>=0 );
147678 unsetJoinExpr(p: p->pWhere, iTable: pItem->iCursor,
147679 nullable: pTabList->a[0].fg.jointype & JT_LTORJ);
147680 }
147681
147682 /* No further action if this term of the FROM clause is not a subquery */
147683 if( pSub==0 ) continue;
147684
147685 /* Catch mismatch in the declared columns of a view and the number of
147686 ** columns in the SELECT on the RHS */
147687 if( pTab->nCol!=pSub->pEList->nExpr ){
147688 sqlite3ErrorMsg(pParse, zFormat: "expected %d columns for '%s' but got %d",
147689 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
147690 goto select_end;
147691 }
147692
147693 /* Do not attempt the usual optimizations (flattening and ORDER BY
147694 ** elimination) on a MATERIALIZED common table expression because
147695 ** a MATERIALIZED common table expression is an optimization fence.
147696 */
147697 if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){
147698 continue;
147699 }
147700
147701 /* Do not try to flatten an aggregate subquery.
147702 **
147703 ** Flattening an aggregate subquery is only possible if the outer query
147704 ** is not a join. But if the outer query is not a join, then the subquery
147705 ** will be implemented as a co-routine and there is no advantage to
147706 ** flattening in that case.
147707 */
147708 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
147709 assert( pSub->pGroupBy==0 );
147710
147711 /* If a FROM-clause subquery has an ORDER BY clause that is not
147712 ** really doing anything, then delete it now so that it does not
147713 ** interfere with query flattening. See the discussion at
147714 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
147715 **
147716 ** Beware of these cases where the ORDER BY clause may not be safely
147717 ** omitted:
147718 **
147719 ** (1) There is also a LIMIT clause
147720 ** (2) The subquery was added to help with window-function
147721 ** processing
147722 ** (3) The subquery is in the FROM clause of an UPDATE
147723 ** (4) The outer query uses an aggregate function other than
147724 ** the built-in count(), min(), or max().
147725 ** (5) The ORDER BY isn't going to accomplish anything because
147726 ** one of:
147727 ** (a) The outer query has a different ORDER BY clause
147728 ** (b) The subquery is part of a join
147729 ** See forum post 062d576715d277c8
147730 **
147731 ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled.
147732 */
147733 if( pSub->pOrderBy!=0
147734 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
147735 && pSub->pLimit==0 /* Condition (1) */
147736 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
147737 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
147738 && OptimizationEnabled(db, SQLITE_OmitOrderBy)
147739 ){
147740 TREETRACE(0x800,pParse,p,
147741 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
147742 sqlite3ParserAddCleanup(pParse,
147743 xCleanup: (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
147744 pPtr: pSub->pOrderBy);
147745 pSub->pOrderBy = 0;
147746 }
147747
147748 /* If the outer query contains a "complex" result set (that is,
147749 ** if the result set of the outer query uses functions or subqueries)
147750 ** and if the subquery contains an ORDER BY clause and if
147751 ** it will be implemented as a co-routine, then do not flatten. This
147752 ** restriction allows SQL constructs like this:
147753 **
147754 ** SELECT expensive_function(x)
147755 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
147756 **
147757 ** The expensive_function() is only computed on the 10 rows that
147758 ** are output, rather than every row of the table.
147759 **
147760 ** The requirement that the outer query have a complex result set
147761 ** means that flattening does occur on simpler SQL constraints without
147762 ** the expensive_function() like:
147763 **
147764 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
147765 */
147766 if( pSub->pOrderBy!=0
147767 && i==0
147768 && (p->selFlags & SF_ComplexResult)!=0
147769 && (pTabList->nSrc==1
147770 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
147771 ){
147772 continue;
147773 }
147774
147775 if( flattenSubquery(pParse, p, iFrom: i, isAgg) ){
147776 if( pParse->nErr ) goto select_end;
147777 /* This subquery can be absorbed into its parent. */
147778 i = -1;
147779 }
147780 pTabList = p->pSrc;
147781 if( db->mallocFailed ) goto select_end;
147782 if( !IgnorableOrderby(pDest) ){
147783 sSort.pOrderBy = p->pOrderBy;
147784 }
147785 }
147786#endif
147787
147788#ifndef SQLITE_OMIT_COMPOUND_SELECT
147789 /* Handle compound SELECT statements using the separate multiSelect()
147790 ** procedure.
147791 */
147792 if( p->pPrior ){
147793 rc = multiSelect(pParse, p, pDest);
147794#if TREETRACE_ENABLED
147795 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
147796 if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)==0 ){
147797 sqlite3TreeViewSelect(0, p, 0);
147798 }
147799#endif
147800 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
147801 return rc;
147802 }
147803#endif
147804
147805 /* Do the WHERE-clause constant propagation optimization if this is
147806 ** a join. No need to speed time on this operation for non-join queries
147807 ** as the equivalent optimization will be handled by query planner in
147808 ** sqlite3WhereBegin().
147809 */
147810 if( p->pWhere!=0
147811 && p->pWhere->op==TK_AND
147812 && OptimizationEnabled(db, SQLITE_PropagateConst)
147813 && propagateConstants(pParse, p)
147814 ){
147815#if TREETRACE_ENABLED
147816 if( sqlite3TreeTrace & 0x2000 ){
147817 TREETRACE(0x2000,pParse,p,("After constant propagation:\n"));
147818 sqlite3TreeViewSelect(0, p, 0);
147819 }
147820#endif
147821 }else{
147822 TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n"));
147823 }
147824
147825 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
147826 && countOfViewOptimization(pParse, p)
147827 ){
147828 if( db->mallocFailed ) goto select_end;
147829 pTabList = p->pSrc;
147830 }
147831
147832 /* For each term in the FROM clause, do two things:
147833 ** (1) Authorized unreferenced tables
147834 ** (2) Generate code for all sub-queries
147835 */
147836 for(i=0; i<pTabList->nSrc; i++){
147837 SrcItem *pItem = &pTabList->a[i];
147838 SrcItem *pPrior;
147839 SelectDest dest;
147840 Select *pSub;
147841#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147842 const char *zSavedAuthContext;
147843#endif
147844
147845 /* Issue SQLITE_READ authorizations with a fake column name for any
147846 ** tables that are referenced but from which no values are extracted.
147847 ** Examples of where these kinds of null SQLITE_READ authorizations
147848 ** would occur:
147849 **
147850 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
147851 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
147852 **
147853 ** The fake column name is an empty string. It is possible for a table to
147854 ** have a column named by the empty string, in which case there is no way to
147855 ** distinguish between an unreferenced table and an actual reference to the
147856 ** "" column. The original design was for the fake column name to be a NULL,
147857 ** which would be unambiguous. But legacy authorization callbacks might
147858 ** assume the column name is non-NULL and segfault. The use of an empty
147859 ** string for the fake column name seems safer.
147860 */
147861 if( pItem->colUsed==0 && pItem->zName!=0 ){
147862 sqlite3AuthCheck(pParse, SQLITE_READ, zArg1: pItem->zName, zArg2: "", zArg3: pItem->zDatabase);
147863 }
147864
147865#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147866 /* Generate code for all sub-queries in the FROM clause
147867 */
147868 pSub = pItem->pSelect;
147869 if( pSub==0 ) continue;
147870
147871 /* The code for a subquery should only be generated once. */
147872 assert( pItem->addrFillSub==0 );
147873
147874 /* Increment Parse.nHeight by the height of the largest expression
147875 ** tree referred to by this, the parent select. The child select
147876 ** may contain expression trees of at most
147877 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
147878 ** more conservative than necessary, but much easier than enforcing
147879 ** an exact limit.
147880 */
147881 pParse->nHeight += sqlite3SelectExprHeight(p);
147882
147883 /* Make copies of constant WHERE-clause terms in the outer query down
147884 ** inside the subquery. This can help the subquery to run more efficiently.
147885 */
147886 if( OptimizationEnabled(db, SQLITE_PushDown)
147887 && (pItem->fg.isCte==0
147888 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
147889 && pushDownWhereTerms(pParse, pSubq: pSub, pWhere: p->pWhere, pSrcList: pTabList, iSrc: i)
147890 ){
147891#if TREETRACE_ENABLED
147892 if( sqlite3TreeTrace & 0x4000 ){
147893 TREETRACE(0x4000,pParse,p,
147894 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
147895 sqlite3TreeViewSelect(0, p, 0);
147896 }
147897#endif
147898 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
147899 }else{
147900 TREETRACE(0x4000,pParse,p,("Push-down not possible\n"));
147901 }
147902
147903 /* Convert unused result columns of the subquery into simple NULL
147904 ** expressions, to avoid unneeded searching and computation.
147905 */
147906 if( OptimizationEnabled(db, SQLITE_NullUnusedCols)
147907 && disableUnusedSubqueryResultColumns(pItem)
147908 ){
147909#if TREETRACE_ENABLED
147910 if( sqlite3TreeTrace & 0x4000 ){
147911 TREETRACE(0x4000,pParse,p,
147912 ("Change unused result columns to NULL for subquery %d:\n",
147913 pSub->selId));
147914 sqlite3TreeViewSelect(0, p, 0);
147915 }
147916#endif
147917 }
147918
147919 zSavedAuthContext = pParse->zAuthContext;
147920 pParse->zAuthContext = pItem->zName;
147921
147922 /* Generate code to implement the subquery
147923 */
147924 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, selFlags: p->selFlags) ){
147925 /* Implement a co-routine that will return a single row of the result
147926 ** set on each invocation.
147927 */
147928 int addrTop = sqlite3VdbeCurrentAddr(p: v)+1;
147929
147930 pItem->regReturn = ++pParse->nMem;
147931 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: pItem->regReturn, p2: 0, p3: addrTop);
147932 VdbeComment((v, "%!S", pItem));
147933 pItem->addrFillSub = addrTop;
147934 sqlite3SelectDestInit(pDest: &dest, SRT_Coroutine, iParm: pItem->regReturn);
147935 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
147936 sqlite3Select(pParse, p: pSub, pDest: &dest);
147937 pItem->pTab->nRowLogEst = pSub->nSelectRow;
147938 pItem->fg.viaCoroutine = 1;
147939 pItem->regResult = dest.iSdst;
147940 sqlite3VdbeEndCoroutine(v, regYield: pItem->regReturn);
147941 sqlite3VdbeJumpHere(p: v, addr: addrTop-1);
147942 sqlite3ClearTempRegCache(pParse);
147943 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
147944 /* This is a CTE for which materialization code has already been
147945 ** generated. Invoke the subroutine to compute the materialization,
147946 ** the make the pItem->iCursor be a copy of the ephemeral table that
147947 ** holds the result of the materialization. */
147948 CteUse *pCteUse = pItem->u2.pCteUse;
147949 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pCteUse->regRtn, p2: pCteUse->addrM9e);
147950 if( pItem->iCursor!=pCteUse->iCur ){
147951 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pItem->iCursor, p2: pCteUse->iCur);
147952 VdbeComment((v, "%!S", pItem));
147953 }
147954 pSub->nSelectRow = pCteUse->nRowEst;
147955 }else if( (pPrior = isSelfJoinView(pTabList, pThis: pItem, iFirst: 0, iEnd: i))!=0 ){
147956 /* This view has already been materialized by a prior entry in
147957 ** this same FROM clause. Reuse it. */
147958 if( pPrior->addrFillSub ){
147959 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pPrior->regReturn, p2: pPrior->addrFillSub);
147960 }
147961 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pItem->iCursor, p2: pPrior->iCursor);
147962 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
147963 }else{
147964 /* Materialize the view. If the view is not correlated, generate a
147965 ** subroutine to do the materialization so that subsequent uses of
147966 ** the same view can reuse the materialization. */
147967 int topAddr;
147968 int onceAddr = 0;
147969#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
147970 int addrExplain;
147971#endif
147972
147973 pItem->regReturn = ++pParse->nMem;
147974 topAddr = sqlite3VdbeAddOp0(p: v, OP_Goto);
147975 pItem->addrFillSub = topAddr+1;
147976 pItem->fg.isMaterialized = 1;
147977 if( pItem->fg.isCorrelated==0 ){
147978 /* If the subquery is not correlated and if we are not inside of
147979 ** a trigger, then we only need to compute the value of the subquery
147980 ** once. */
147981 onceAddr = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
147982 VdbeComment((v, "materialize %!S", pItem));
147983 }else{
147984 VdbeNoopComment((v, "materialize %!S", pItem));
147985 }
147986 sqlite3SelectDestInit(pDest: &dest, SRT_EphemTab, iParm: pItem->iCursor);
147987
147988 ExplainQueryPlan2(addrExplain, (pParse, 1, "MATERIALIZE %!S", pItem));
147989 sqlite3Select(pParse, p: pSub, pDest: &dest);
147990 pItem->pTab->nRowLogEst = pSub->nSelectRow;
147991 if( onceAddr ) sqlite3VdbeJumpHere(p: v, addr: onceAddr);
147992 sqlite3VdbeAddOp2(p: v, OP_Return, p1: pItem->regReturn, p2: topAddr+1);
147993 VdbeComment((v, "end %!S", pItem));
147994 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
147995 sqlite3VdbeJumpHere(p: v, addr: topAddr);
147996 sqlite3ClearTempRegCache(pParse);
147997 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
147998 CteUse *pCteUse = pItem->u2.pCteUse;
147999 pCteUse->addrM9e = pItem->addrFillSub;
148000 pCteUse->regRtn = pItem->regReturn;
148001 pCteUse->iCur = pItem->iCursor;
148002 pCteUse->nRowEst = pSub->nSelectRow;
148003 }
148004 }
148005 if( db->mallocFailed ) goto select_end;
148006 pParse->nHeight -= sqlite3SelectExprHeight(p);
148007 pParse->zAuthContext = zSavedAuthContext;
148008#endif
148009 }
148010
148011 /* Various elements of the SELECT copied into local variables for
148012 ** convenience */
148013 pEList = p->pEList;
148014 pWhere = p->pWhere;
148015 pGroupBy = p->pGroupBy;
148016 pHaving = p->pHaving;
148017 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
148018
148019#if TREETRACE_ENABLED
148020 if( sqlite3TreeTrace & 0x8000 ){
148021 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
148022 sqlite3TreeViewSelect(0, p, 0);
148023 }
148024#endif
148025
148026 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
148027 ** if the select-list is the same as the ORDER BY list, then this query
148028 ** can be rewritten as a GROUP BY. In other words, this:
148029 **
148030 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
148031 **
148032 ** is transformed to:
148033 **
148034 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
148035 **
148036 ** The second form is preferred as a single index (or temp-table) may be
148037 ** used for both the ORDER BY and DISTINCT processing. As originally
148038 ** written the query must use a temp-table for at least one of the ORDER
148039 ** BY and DISTINCT, and an index or separate temp-table for the other.
148040 */
148041 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
148042 && sqlite3ExprListCompare(pA: sSort.pOrderBy, pB: pEList, iTab: -1)==0
148043#ifndef SQLITE_OMIT_WINDOWFUNC
148044 && p->pWin==0
148045#endif
148046 ){
148047 p->selFlags &= ~SF_Distinct;
148048 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, p: pEList, flags: 0);
148049 p->selFlags |= SF_Aggregate;
148050 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
148051 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
148052 ** original setting of the SF_Distinct flag, not the current setting */
148053 assert( sDistinct.isTnct );
148054 sDistinct.isTnct = 2;
148055
148056#if TREETRACE_ENABLED
148057 if( sqlite3TreeTrace & 0x20000 ){
148058 TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
148059 sqlite3TreeViewSelect(0, p, 0);
148060 }
148061#endif
148062 }
148063
148064 /* If there is an ORDER BY clause, then create an ephemeral index to
148065 ** do the sorting. But this sorting ephemeral index might end up
148066 ** being unused if the data can be extracted in pre-sorted order.
148067 ** If that is the case, then the OP_OpenEphemeral instruction will be
148068 ** changed to an OP_Noop once we figure out that the sorting index is
148069 ** not needed. The sSort.addrSortIndex variable is used to facilitate
148070 ** that change.
148071 */
148072 if( sSort.pOrderBy ){
148073 KeyInfo *pKeyInfo;
148074 pKeyInfo = sqlite3KeyInfoFromExprList(
148075 pParse, pList: sSort.pOrderBy, iStart: 0, nExtra: pEList->nExpr);
148076 sSort.iECursor = pParse->nTab++;
148077 sSort.addrSortIndex =
148078 sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral,
148079 p1: sSort.iECursor, p2: sSort.pOrderBy->nExpr+1+pEList->nExpr, p3: 0,
148080 zP4: (char*)pKeyInfo, P4_KEYINFO
148081 );
148082 }else{
148083 sSort.addrSortIndex = -1;
148084 }
148085
148086 /* If the output is destined for a temporary table, open that table.
148087 */
148088 if( pDest->eDest==SRT_EphemTab ){
148089 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pDest->iSDParm, p2: pEList->nExpr);
148090 if( p->selFlags & SF_NestedFrom ){
148091 /* Delete or NULL-out result columns that will never be used */
148092 int ii;
148093 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
148094 sqlite3ExprDelete(db, p: pEList->a[ii].pExpr);
148095 sqlite3DbFree(db, p: pEList->a[ii].zEName);
148096 pEList->nExpr--;
148097 }
148098 for(ii=0; ii<pEList->nExpr; ii++){
148099 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
148100 }
148101 }
148102 }
148103
148104 /* Set the limiter.
148105 */
148106 iEnd = sqlite3VdbeMakeLabel(pParse);
148107 if( (p->selFlags & SF_FixedLimit)==0 ){
148108 p->nSelectRow = 320; /* 4 billion rows */
148109 }
148110 if( p->pLimit ) computeLimitRegisters(pParse, p, iBreak: iEnd);
148111 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
148112 sqlite3VdbeChangeOpcode(p: v, addr: sSort.addrSortIndex, OP_SorterOpen);
148113 sSort.sortFlags |= SORTFLAG_UseSorter;
148114 }
148115
148116 /* Open an ephemeral index to use for the distinct set.
148117 */
148118 if( p->selFlags & SF_Distinct ){
148119 sDistinct.tabTnct = pParse->nTab++;
148120 sDistinct.addrTnct = sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral,
148121 p1: sDistinct.tabTnct, p2: 0, p3: 0,
148122 zP4: (char*)sqlite3KeyInfoFromExprList(pParse, pList: p->pEList,iStart: 0,nExtra: 0),
148123 P4_KEYINFO);
148124 sqlite3VdbeChangeP5(p: v, BTREE_UNORDERED);
148125 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
148126 }else{
148127 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
148128 }
148129
148130 if( !isAgg && pGroupBy==0 ){
148131 /* No aggregate functions and no GROUP BY clause */
148132 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
148133 | (p->selFlags & SF_FixedLimit);
148134#ifndef SQLITE_OMIT_WINDOWFUNC
148135 Window *pWin = p->pWin; /* Main window object (or NULL) */
148136 if( pWin ){
148137 sqlite3WindowCodeInit(pParse, p);
148138 }
148139#endif
148140 assert( WHERE_USE_LIMIT==SF_FixedLimit );
148141
148142
148143 /* Begin the database scan. */
148144 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
148145 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
148146 p->pEList, p, wctrlFlags, p->nSelectRow);
148147 if( pWInfo==0 ) goto select_end;
148148 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
148149 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
148150 }
148151 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
148152 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
148153 }
148154 if( sSort.pOrderBy ){
148155 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
148156 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
148157 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
148158 sSort.pOrderBy = 0;
148159 }
148160 }
148161 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
148162
148163 /* If sorting index that was created by a prior OP_OpenEphemeral
148164 ** instruction ended up not being needed, then change the OP_OpenEphemeral
148165 ** into an OP_Noop.
148166 */
148167 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
148168 sqlite3VdbeChangeToNoop(p: v, addr: sSort.addrSortIndex);
148169 }
148170
148171 assert( p->pEList==pEList );
148172#ifndef SQLITE_OMIT_WINDOWFUNC
148173 if( pWin ){
148174 int addrGosub = sqlite3VdbeMakeLabel(pParse);
148175 int iCont = sqlite3VdbeMakeLabel(pParse);
148176 int iBreak = sqlite3VdbeMakeLabel(pParse);
148177 int regGosub = ++pParse->nMem;
148178
148179 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
148180
148181 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: iBreak);
148182 sqlite3VdbeResolveLabel(v, x: addrGosub);
148183 VdbeNoopComment((v, "inner-loop subroutine"));
148184 sSort.labelOBLopt = 0;
148185 selectInnerLoop(pParse, p, srcTab: -1, pSort: &sSort, pDistinct: &sDistinct, pDest, iContinue: iCont, iBreak);
148186 sqlite3VdbeResolveLabel(v, x: iCont);
148187 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regGosub);
148188 VdbeComment((v, "end inner-loop subroutine"));
148189 sqlite3VdbeResolveLabel(v, x: iBreak);
148190 }else
148191#endif /* SQLITE_OMIT_WINDOWFUNC */
148192 {
148193 /* Use the standard inner loop. */
148194 selectInnerLoop(pParse, p, srcTab: -1, pSort: &sSort, pDistinct: &sDistinct, pDest,
148195 iContinue: sqlite3WhereContinueLabel(pWInfo),
148196 iBreak: sqlite3WhereBreakLabel(pWInfo));
148197
148198 /* End the database scan loop.
148199 */
148200 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
148201 sqlite3WhereEnd(pWInfo);
148202 }
148203 }else{
148204 /* This case when there exist aggregate functions or a GROUP BY clause
148205 ** or both */
148206 NameContext sNC; /* Name context for processing aggregate information */
148207 int iAMem; /* First Mem address for storing current GROUP BY */
148208 int iBMem; /* First Mem address for previous GROUP BY */
148209 int iUseFlag; /* Mem address holding flag indicating that at least
148210 ** one row of the input to the aggregator has been
148211 ** processed */
148212 int iAbortFlag; /* Mem address which causes query abort if positive */
148213 int groupBySort; /* Rows come from source in GROUP BY order */
148214 int addrEnd; /* End of processing for this SELECT */
148215 int sortPTab = 0; /* Pseudotable used to decode sorting results */
148216 int sortOut = 0; /* Output register from the sorter */
148217 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
148218
148219 /* Remove any and all aliases between the result set and the
148220 ** GROUP BY clause.
148221 */
148222 if( pGroupBy ){
148223 int k; /* Loop counter */
148224 struct ExprList_item *pItem; /* For looping over expression in a list */
148225
148226 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
148227 pItem->u.x.iAlias = 0;
148228 }
148229 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
148230 pItem->u.x.iAlias = 0;
148231 }
148232 assert( 66==sqlite3LogEst(100) );
148233 if( p->nSelectRow>66 ) p->nSelectRow = 66;
148234
148235 /* If there is both a GROUP BY and an ORDER BY clause and they are
148236 ** identical, then it may be possible to disable the ORDER BY clause
148237 ** on the grounds that the GROUP BY will cause elements to come out
148238 ** in the correct order. It also may not - the GROUP BY might use a
148239 ** database index that causes rows to be grouped together as required
148240 ** but not actually sorted. Either way, record the fact that the
148241 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
148242 ** variable. */
148243 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
148244 int ii;
148245 /* The GROUP BY processing doesn't care whether rows are delivered in
148246 ** ASC or DESC order - only that each group is returned contiguously.
148247 ** So set the ASC/DESC flags in the GROUP BY to match those in the
148248 ** ORDER BY to maximize the chances of rows being delivered in an
148249 ** order that makes the ORDER BY redundant. */
148250 for(ii=0; ii<pGroupBy->nExpr; ii++){
148251 u8 sortFlags;
148252 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
148253 pGroupBy->a[ii].fg.sortFlags = sortFlags;
148254 }
148255 if( sqlite3ExprListCompare(pA: pGroupBy, pB: sSort.pOrderBy, iTab: -1)==0 ){
148256 orderByGrp = 1;
148257 }
148258 }
148259 }else{
148260 assert( 0==sqlite3LogEst(1) );
148261 p->nSelectRow = 0;
148262 }
148263
148264 /* Create a label to jump to when we want to abort the query */
148265 addrEnd = sqlite3VdbeMakeLabel(pParse);
148266
148267 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
148268 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
148269 ** SELECT statement.
148270 */
148271 pAggInfo = sqlite3DbMallocZero(db, n: sizeof(*pAggInfo) );
148272 if( pAggInfo ){
148273 sqlite3ParserAddCleanup(pParse,
148274 xCleanup: (void(*)(sqlite3*,void*))agginfoFree, pPtr: pAggInfo);
148275 testcase( pParse->earlyCleanup );
148276 }
148277 if( db->mallocFailed ){
148278 goto select_end;
148279 }
148280 pAggInfo->selId = p->selId;
148281#ifdef SQLITE_DEBUG
148282 pAggInfo->pSelect = p;
148283#endif
148284 memset(s: &sNC, c: 0, n: sizeof(sNC));
148285 sNC.pParse = pParse;
148286 sNC.pSrcList = pTabList;
148287 sNC.uNC.pAggInfo = pAggInfo;
148288 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
148289 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
148290 pAggInfo->pGroupBy = pGroupBy;
148291 sqlite3ExprAnalyzeAggList(pNC: &sNC, pList: pEList);
148292 sqlite3ExprAnalyzeAggList(pNC: &sNC, pList: sSort.pOrderBy);
148293 if( pHaving ){
148294 if( pGroupBy ){
148295 assert( pWhere==p->pWhere );
148296 assert( pHaving==p->pHaving );
148297 assert( pGroupBy==p->pGroupBy );
148298 havingToWhere(pParse, p);
148299 pWhere = p->pWhere;
148300 }
148301 sqlite3ExprAnalyzeAggregates(pNC: &sNC, pExpr: pHaving);
148302 }
148303 pAggInfo->nAccumulator = pAggInfo->nColumn;
148304 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
148305 minMaxFlag = minMaxQuery(db, pFunc: pAggInfo->aFunc[0].pFExpr, ppMinMax: &pMinMaxOrderBy);
148306 }else{
148307 minMaxFlag = WHERE_ORDERBY_NORMAL;
148308 }
148309 analyzeAggFuncArgs(pAggInfo, pNC: &sNC);
148310 if( db->mallocFailed ) goto select_end;
148311#if TREETRACE_ENABLED
148312 if( sqlite3TreeTrace & 0x20 ){
148313 TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
148314 sqlite3TreeViewSelect(0, p, 0);
148315 if( minMaxFlag ){
148316 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
148317 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
148318 }
148319 printAggInfo(pAggInfo);
148320 }
148321#endif
148322
148323
148324 /* Processing for aggregates with GROUP BY is very different and
148325 ** much more complex than aggregates without a GROUP BY.
148326 */
148327 if( pGroupBy ){
148328 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
148329 int addr1; /* A-vs-B comparison jump */
148330 int addrOutputRow; /* Start of subroutine that outputs a result row */
148331 int regOutputRow; /* Return address register for output subroutine */
148332 int addrSetAbort; /* Set the abort flag and return */
148333 int addrTopOfLoop; /* Top of the input loop */
148334 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
148335 int addrReset; /* Subroutine for resetting the accumulator */
148336 int regReset; /* Return address register for reset subroutine */
148337 ExprList *pDistinct = 0;
148338 u16 distFlag = 0;
148339 int eDist = WHERE_DISTINCT_NOOP;
148340
148341 if( pAggInfo->nFunc==1
148342 && pAggInfo->aFunc[0].iDistinct>=0
148343 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
148344 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
148345 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
148346 ){
148347 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
148348 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
148349 pDistinct = sqlite3ExprListDup(db, p: pGroupBy, flags: 0);
148350 pDistinct = sqlite3ExprListAppend(pParse, pList: pDistinct, pExpr);
148351 distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
148352 }
148353
148354 /* If there is a GROUP BY clause we might need a sorting index to
148355 ** implement it. Allocate that sorting index now. If it turns out
148356 ** that we do not need it after all, the OP_SorterOpen instruction
148357 ** will be converted into a Noop.
148358 */
148359 pAggInfo->sortingIdx = pParse->nTab++;
148360 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pGroupBy,
148361 iStart: 0, nExtra: pAggInfo->nColumn);
148362 addrSortingIdx = sqlite3VdbeAddOp4(p: v, OP_SorterOpen,
148363 p1: pAggInfo->sortingIdx, p2: pAggInfo->nSortingColumn,
148364 p3: 0, zP4: (char*)pKeyInfo, P4_KEYINFO);
148365
148366 /* Initialize memory locations used by GROUP BY aggregate processing
148367 */
148368 iUseFlag = ++pParse->nMem;
148369 iAbortFlag = ++pParse->nMem;
148370 regOutputRow = ++pParse->nMem;
148371 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
148372 regReset = ++pParse->nMem;
148373 addrReset = sqlite3VdbeMakeLabel(pParse);
148374 iAMem = pParse->nMem + 1;
148375 pParse->nMem += pGroupBy->nExpr;
148376 iBMem = pParse->nMem + 1;
148377 pParse->nMem += pGroupBy->nExpr;
148378 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: iAbortFlag);
148379 VdbeComment((v, "clear abort flag"));
148380 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: iAMem, p3: iAMem+pGroupBy->nExpr-1);
148381
148382 /* Begin a loop that will extract all source rows in GROUP BY order.
148383 ** This might involve two separate loops with an OP_Sort in between, or
148384 ** it might be a single loop that uses an index to extract information
148385 ** in the right order to begin with.
148386 */
148387 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regReset, p2: addrReset);
148388 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
148389 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
148390 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
148391 | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
148392 );
148393 if( pWInfo==0 ){
148394 sqlite3ExprListDelete(db, pList: pDistinct);
148395 goto select_end;
148396 }
148397 if( pParse->pIdxEpr ){
148398 optimizeAggregateUseOfIndexedExpr(pParse, pSelect: p, pAggInfo, pNC: &sNC);
148399 }
148400 assignAggregateRegisters(pParse, pAggInfo);
148401 eDist = sqlite3WhereIsDistinct(pWInfo);
148402 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
148403 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
148404 /* The optimizer is able to deliver rows in group by order so
148405 ** we do not have to sort. The OP_OpenEphemeral table will be
148406 ** cancelled later because we still need to use the pKeyInfo
148407 */
148408 groupBySort = 0;
148409 }else{
148410 /* Rows are coming out in undetermined order. We have to push
148411 ** each row into a sorting index, terminate the first loop,
148412 ** then loop over the sorting index in order to get the output
148413 ** in sorted order
148414 */
148415 int regBase;
148416 int regRecord;
148417 int nCol;
148418 int nGroupBy;
148419
148420#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
148421 int addrExp; /* Address of OP_Explain instruction */
148422#endif
148423 ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s",
148424 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
148425 "DISTINCT" : "GROUP BY"
148426 ));
148427
148428 groupBySort = 1;
148429 nGroupBy = pGroupBy->nExpr;
148430 nCol = nGroupBy;
148431 j = nGroupBy;
148432 for(i=0; i<pAggInfo->nColumn; i++){
148433 if( pAggInfo->aCol[i].iSorterColumn>=j ){
148434 nCol++;
148435 j++;
148436 }
148437 }
148438 regBase = sqlite3GetTempRange(pParse, nReg: nCol);
148439 sqlite3ExprCodeExprList(pParse, pList: pGroupBy, target: regBase, srcReg: 0, flags: 0);
148440 j = nGroupBy;
148441 pAggInfo->directMode = 1;
148442 for(i=0; i<pAggInfo->nColumn; i++){
148443 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
148444 if( pCol->iSorterColumn>=j ){
148445 sqlite3ExprCode(pParse, pExpr: pCol->pCExpr, target: j + regBase);
148446 j++;
148447 }
148448 }
148449 pAggInfo->directMode = 0;
148450 regRecord = sqlite3GetTempReg(pParse);
148451 sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v));
148452 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regBase, p2: nCol, p3: regRecord);
148453 sqlite3VdbeAddOp2(p: v, OP_SorterInsert, p1: pAggInfo->sortingIdx, p2: regRecord);
148454 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
148455 sqlite3ReleaseTempReg(pParse, iReg: regRecord);
148456 sqlite3ReleaseTempRange(pParse, iReg: regBase, nReg: nCol);
148457 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
148458 sqlite3WhereEnd(pWInfo);
148459 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
148460 sortOut = sqlite3GetTempReg(pParse);
148461 sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0);
148462 sqlite3VdbeAddOp3(p: v, OP_OpenPseudo, p1: sortPTab, p2: sortOut, p3: nCol);
148463 sqlite3VdbeAddOp2(p: v, OP_SorterSort, p1: pAggInfo->sortingIdx, p2: addrEnd);
148464 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
148465 pAggInfo->useSortingIdx = 1;
148466 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
148467 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
148468 }
148469
148470 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
148471 ** that are indexed (and that were previously identified and tagged
148472 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
148473 ** must now be converted into a TK_AGG_COLUMN node so that the value
148474 ** is correctly pulled from the index rather than being recomputed. */
148475 if( pParse->pIdxEpr ){
148476 aggregateConvertIndexedExprRefToColumn(pAggInfo);
148477#if TREETRACE_ENABLED
148478 if( sqlite3TreeTrace & 0x20 ){
148479 TREETRACE(0x20, pParse, p,
148480 ("AggInfo function expressions converted to reference index\n"));
148481 sqlite3TreeViewSelect(0, p, 0);
148482 printAggInfo(pAggInfo);
148483 }
148484#endif
148485 }
148486
148487 /* If the index or temporary table used by the GROUP BY sort
148488 ** will naturally deliver rows in the order required by the ORDER BY
148489 ** clause, cancel the ephemeral table open coded earlier.
148490 **
148491 ** This is an optimization - the correct answer should result regardless.
148492 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
148493 ** disable this optimization for testing purposes. */
148494 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
148495 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
148496 ){
148497 sSort.pOrderBy = 0;
148498 sqlite3VdbeChangeToNoop(p: v, addr: sSort.addrSortIndex);
148499 }
148500
148501 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
148502 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
148503 ** Then compare the current GROUP BY terms against the GROUP BY terms
148504 ** from the previous row currently stored in a0, a1, a2...
148505 */
148506 addrTopOfLoop = sqlite3VdbeCurrentAddr(p: v);
148507 if( groupBySort ){
148508 sqlite3VdbeAddOp3(p: v, OP_SorterData, p1: pAggInfo->sortingIdx,
148509 p2: sortOut, p3: sortPTab);
148510 }
148511 for(j=0; j<pGroupBy->nExpr; j++){
148512 if( groupBySort ){
148513 sqlite3VdbeAddOp3(p: v, OP_Column, p1: sortPTab, p2: j, p3: iBMem+j);
148514 }else{
148515 pAggInfo->directMode = 1;
148516 sqlite3ExprCode(pParse, pExpr: pGroupBy->a[j].pExpr, target: iBMem+j);
148517 }
148518 }
148519 sqlite3VdbeAddOp4(p: v, OP_Compare, p1: iAMem, p2: iBMem, p3: pGroupBy->nExpr,
148520 zP4: (char*)sqlite3KeyInfoRef(p: pKeyInfo), P4_KEYINFO);
148521 addr1 = sqlite3VdbeCurrentAddr(p: v);
148522 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr1+1, p2: 0, p3: addr1+1); VdbeCoverage(v);
148523
148524 /* Generate code that runs whenever the GROUP BY changes.
148525 ** Changes in the GROUP BY are detected by the previous code
148526 ** block. If there were no changes, this block is skipped.
148527 **
148528 ** This code copies current group by terms in b0,b1,b2,...
148529 ** over to a0,a1,a2. It then calls the output subroutine
148530 ** and resets the aggregate accumulator registers in preparation
148531 ** for the next GROUP BY batch.
148532 */
148533 sqlite3ExprCodeMove(pParse, iFrom: iBMem, iTo: iAMem, nReg: pGroupBy->nExpr);
148534 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutputRow, p2: addrOutputRow);
148535 VdbeComment((v, "output one row"));
148536 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: iAbortFlag, p2: addrEnd); VdbeCoverage(v);
148537 VdbeComment((v, "check abort flag"));
148538 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regReset, p2: addrReset);
148539 VdbeComment((v, "reset accumulator"));
148540
148541 /* Update the aggregate accumulators based on the content of
148542 ** the current row
148543 */
148544 sqlite3VdbeJumpHere(p: v, addr: addr1);
148545 updateAccumulator(pParse, regAcc: iUseFlag, pAggInfo, eDistinctType: eDist);
148546 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: iUseFlag);
148547 VdbeComment((v, "indicate data in accumulator"));
148548
148549 /* End of the loop
148550 */
148551 if( groupBySort ){
148552 sqlite3VdbeAddOp2(p: v, OP_SorterNext, p1: pAggInfo->sortingIdx,p2: addrTopOfLoop);
148553 VdbeCoverage(v);
148554 }else{
148555 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
148556 sqlite3WhereEnd(pWInfo);
148557 sqlite3VdbeChangeToNoop(p: v, addr: addrSortingIdx);
148558 }
148559 sqlite3ExprListDelete(db, pList: pDistinct);
148560
148561 /* Output the final row of result
148562 */
148563 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutputRow, p2: addrOutputRow);
148564 VdbeComment((v, "output final row"));
148565
148566 /* Jump over the subroutines
148567 */
148568 sqlite3VdbeGoto(p: v, iDest: addrEnd);
148569
148570 /* Generate a subroutine that outputs a single row of the result
148571 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
148572 ** is less than or equal to zero, the subroutine is a no-op. If
148573 ** the processing calls for the query to abort, this subroutine
148574 ** increments the iAbortFlag memory location before returning in
148575 ** order to signal the caller to abort.
148576 */
148577 addrSetAbort = sqlite3VdbeCurrentAddr(p: v);
148578 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: iAbortFlag);
148579 VdbeComment((v, "set abort flag"));
148580 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regOutputRow);
148581 sqlite3VdbeResolveLabel(v, x: addrOutputRow);
148582 addrOutputRow = sqlite3VdbeCurrentAddr(p: v);
148583 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: iUseFlag, p2: addrOutputRow+2);
148584 VdbeCoverage(v);
148585 VdbeComment((v, "Groupby result generator entry point"));
148586 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regOutputRow);
148587 finalizeAggFunctions(pParse, pAggInfo);
148588 sqlite3ExprIfFalse(pParse, pExpr: pHaving, dest: addrOutputRow+1, SQLITE_JUMPIFNULL);
148589 selectInnerLoop(pParse, p, srcTab: -1, pSort: &sSort,
148590 pDistinct: &sDistinct, pDest,
148591 iContinue: addrOutputRow+1, iBreak: addrSetAbort);
148592 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regOutputRow);
148593 VdbeComment((v, "end groupby result generator"));
148594
148595 /* Generate a subroutine that will reset the group-by accumulator
148596 */
148597 sqlite3VdbeResolveLabel(v, x: addrReset);
148598 resetAccumulator(pParse, pAggInfo);
148599 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: iUseFlag);
148600 VdbeComment((v, "indicate accumulator empty"));
148601 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regReset);
148602
148603 if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP ){
148604 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
148605 fixDistinctOpenEph(pParse, eTnctType: eDist, iVal: pF->iDistinct, iOpenEphAddr: pF->iDistAddr);
148606 }
148607 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
148608 else {
148609 Table *pTab;
148610 if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
148611 /* If isSimpleCount() returns a pointer to a Table structure, then
148612 ** the SQL statement is of the form:
148613 **
148614 ** SELECT count(*) FROM <tbl>
148615 **
148616 ** where the Table structure returned represents table <tbl>.
148617 **
148618 ** This statement is so common that it is optimized specially. The
148619 ** OP_Count instruction is executed either on the intkey table that
148620 ** contains the data for table <tbl> or on one of its indexes. It
148621 ** is better to execute the op on an index, as indexes are almost
148622 ** always spread across less pages than their corresponding tables.
148623 */
148624 const int iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTab->pSchema);
148625 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
148626 Index *pIdx; /* Iterator variable */
148627 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
148628 Index *pBest = 0; /* Best index found so far */
148629 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
148630
148631 sqlite3CodeVerifySchema(pParse, iDb);
148632 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 0, zName: pTab->zName);
148633
148634 /* Search for the index that has the lowest scan cost.
148635 **
148636 ** (2011-04-15) Do not do a full scan of an unordered index.
148637 **
148638 ** (2013-10-03) Do not count the entries in a partial index.
148639 **
148640 ** In practice the KeyInfo structure will not be used. It is only
148641 ** passed to keep OP_OpenRead happy.
148642 */
148643 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
148644 if( !p->pSrc->a[0].fg.notIndexed ){
148645 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
148646 if( pIdx->bUnordered==0
148647 && pIdx->szIdxRow<pTab->szTabRow
148648 && pIdx->pPartIdxWhere==0
148649 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
148650 ){
148651 pBest = pIdx;
148652 }
148653 }
148654 }
148655 if( pBest ){
148656 iRoot = pBest->tnum;
148657 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx: pBest);
148658 }
148659
148660 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
148661 sqlite3VdbeAddOp4Int(p: v, OP_OpenRead, p1: iCsr, p2: (int)iRoot, p3: iDb, p4: 1);
148662 if( pKeyInfo ){
148663 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char *)pKeyInfo, P4_KEYINFO);
148664 }
148665 assignAggregateRegisters(pParse, pAggInfo);
148666 sqlite3VdbeAddOp2(p: v, OP_Count, p1: iCsr, AggInfoFuncReg(pAggInfo,0));
148667 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iCsr);
148668 explainSimpleCount(pParse, pTab, pIdx: pBest);
148669 }else{
148670 int regAcc = 0; /* "populate accumulators" flag */
148671 ExprList *pDistinct = 0;
148672 u16 distFlag = 0;
148673 int eDist;
148674
148675 /* If there are accumulator registers but no min() or max() functions
148676 ** without FILTER clauses, allocate register regAcc. Register regAcc
148677 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
148678 ** The code generated by updateAccumulator() uses this to ensure
148679 ** that the accumulator registers are (a) updated only once if
148680 ** there are no min() or max functions or (b) always updated for the
148681 ** first row visited by the aggregate, so that they are updated at
148682 ** least once even if the FILTER clause means the min() or max()
148683 ** function visits zero rows. */
148684 if( pAggInfo->nAccumulator ){
148685 for(i=0; i<pAggInfo->nFunc; i++){
148686 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
148687 continue;
148688 }
148689 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
148690 break;
148691 }
148692 }
148693 if( i==pAggInfo->nFunc ){
148694 regAcc = ++pParse->nMem;
148695 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regAcc);
148696 }
148697 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
148698 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
148699 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
148700 distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
148701 }
148702 assignAggregateRegisters(pParse, pAggInfo);
148703
148704 /* This case runs if the aggregate has no GROUP BY clause. The
148705 ** processing is much simpler since there is only a single row
148706 ** of output.
148707 */
148708 assert( p->pGroupBy==0 );
148709 resetAccumulator(pParse, pAggInfo);
148710
148711 /* If this query is a candidate for the min/max optimization, then
148712 ** minMaxFlag will have been previously set to either
148713 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
148714 ** be an appropriate ORDER BY expression for the optimization.
148715 */
148716 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
148717 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
148718
148719 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
148720 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
148721 pDistinct, p, minMaxFlag|distFlag, 0);
148722 if( pWInfo==0 ){
148723 goto select_end;
148724 }
148725 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
148726 eDist = sqlite3WhereIsDistinct(pWInfo);
148727 updateAccumulator(pParse, regAcc, pAggInfo, eDistinctType: eDist);
148728 if( eDist!=WHERE_DISTINCT_NOOP ){
148729 struct AggInfo_func *pF = pAggInfo->aFunc;
148730 if( pF ){
148731 fixDistinctOpenEph(pParse, eTnctType: eDist, iVal: pF->iDistinct, iOpenEphAddr: pF->iDistAddr);
148732 }
148733 }
148734
148735 if( regAcc ) sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: regAcc);
148736 if( minMaxFlag ){
148737 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
148738 }
148739 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
148740 sqlite3WhereEnd(pWInfo);
148741 finalizeAggFunctions(pParse, pAggInfo);
148742 }
148743
148744 sSort.pOrderBy = 0;
148745 sqlite3ExprIfFalse(pParse, pExpr: pHaving, dest: addrEnd, SQLITE_JUMPIFNULL);
148746 selectInnerLoop(pParse, p, srcTab: -1, pSort: 0, pDistinct: 0,
148747 pDest, iContinue: addrEnd, iBreak: addrEnd);
148748 }
148749 sqlite3VdbeResolveLabel(v, x: addrEnd);
148750
148751 } /* endif aggregate query */
148752
148753 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
148754 explainTempTable(pParse, zUsage: "DISTINCT");
148755 }
148756
148757 /* If there is an ORDER BY clause, then we need to sort the results
148758 ** and send them to the callback one by one.
148759 */
148760 if( sSort.pOrderBy ){
148761 assert( p->pEList==pEList );
148762 generateSortTail(pParse, p, pSort: &sSort, nColumn: pEList->nExpr, pDest);
148763 }
148764
148765 /* Jump here to skip this query
148766 */
148767 sqlite3VdbeResolveLabel(v, x: iEnd);
148768
148769 /* The SELECT has been coded. If there is an error in the Parse structure,
148770 ** set the return code to 1. Otherwise 0. */
148771 rc = (pParse->nErr>0);
148772
148773 /* Control jumps to here if an error is encountered above, or upon
148774 ** successful coding of the SELECT.
148775 */
148776select_end:
148777 assert( db->mallocFailed==0 || db->mallocFailed==1 );
148778 assert( db->mallocFailed==0 || pParse->nErr!=0 );
148779 sqlite3ExprListDelete(db, pList: pMinMaxOrderBy);
148780#ifdef SQLITE_DEBUG
148781 if( pAggInfo && !db->mallocFailed ){
148782 for(i=0; i<pAggInfo->nColumn; i++){
148783 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
148784 if( pExpr==0 ) continue;
148785 assert( pExpr->pAggInfo==pAggInfo );
148786 assert( pExpr->iAgg==i );
148787 }
148788 for(i=0; i<pAggInfo->nFunc; i++){
148789 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
148790 assert( pExpr!=0 );
148791 assert( pExpr->pAggInfo==pAggInfo );
148792 assert( pExpr->iAgg==i );
148793 }
148794 }
148795#endif
148796
148797#if TREETRACE_ENABLED
148798 TREETRACE(0x1,pParse,p,("end processing\n"));
148799 if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
148800 sqlite3TreeViewSelect(0, p, 0);
148801 }
148802#endif
148803 ExplainQueryPlanPop(pParse);
148804 return rc;
148805}
148806
148807/************** End of select.c **********************************************/
148808/************** Begin file table.c *******************************************/
148809/*
148810** 2001 September 15
148811**
148812** The author disclaims copyright to this source code. In place of
148813** a legal notice, here is a blessing:
148814**
148815** May you do good and not evil.
148816** May you find forgiveness for yourself and forgive others.
148817** May you share freely, never taking more than you give.
148818**
148819*************************************************************************
148820** This file contains the sqlite3_get_table() and sqlite3_free_table()
148821** interface routines. These are just wrappers around the main
148822** interface routine of sqlite3_exec().
148823**
148824** These routines are in a separate files so that they will not be linked
148825** if they are not used.
148826*/
148827/* #include "sqliteInt.h" */
148828
148829#ifndef SQLITE_OMIT_GET_TABLE
148830
148831/*
148832** This structure is used to pass data from sqlite3_get_table() through
148833** to the callback function is uses to build the result.
148834*/
148835typedef struct TabResult {
148836 char **azResult; /* Accumulated output */
148837 char *zErrMsg; /* Error message text, if an error occurs */
148838 u32 nAlloc; /* Slots allocated for azResult[] */
148839 u32 nRow; /* Number of rows in the result */
148840 u32 nColumn; /* Number of columns in the result */
148841 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
148842 int rc; /* Return code from sqlite3_exec() */
148843} TabResult;
148844
148845/*
148846** This routine is called once for each row in the result table. Its job
148847** is to fill in the TabResult structure appropriately, allocating new
148848** memory as necessary.
148849*/
148850static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
148851 TabResult *p = (TabResult*)pArg; /* Result accumulator */
148852 int need; /* Slots needed in p->azResult[] */
148853 int i; /* Loop counter */
148854 char *z; /* A single column of result */
148855
148856 /* Make sure there is enough space in p->azResult to hold everything
148857 ** we need to remember from this invocation of the callback.
148858 */
148859 if( p->nRow==0 && argv!=0 ){
148860 need = nCol*2;
148861 }else{
148862 need = nCol;
148863 }
148864 if( p->nData + need > p->nAlloc ){
148865 char **azNew;
148866 p->nAlloc = p->nAlloc*2 + need;
148867 azNew = sqlite3Realloc( pOld: p->azResult, nBytes: sizeof(char*)*p->nAlloc );
148868 if( azNew==0 ) goto malloc_failed;
148869 p->azResult = azNew;
148870 }
148871
148872 /* If this is the first row, then generate an extra row containing
148873 ** the names of all columns.
148874 */
148875 if( p->nRow==0 ){
148876 p->nColumn = nCol;
148877 for(i=0; i<nCol; i++){
148878 z = sqlite3_mprintf(zFormat: "%s", colv[i]);
148879 if( z==0 ) goto malloc_failed;
148880 p->azResult[p->nData++] = z;
148881 }
148882 }else if( (int)p->nColumn!=nCol ){
148883 sqlite3_free(p: p->zErrMsg);
148884 p->zErrMsg = sqlite3_mprintf(
148885 zFormat: "sqlite3_get_table() called with two or more incompatible queries"
148886 );
148887 p->rc = SQLITE_ERROR;
148888 return 1;
148889 }
148890
148891 /* Copy over the row data
148892 */
148893 if( argv!=0 ){
148894 for(i=0; i<nCol; i++){
148895 if( argv[i]==0 ){
148896 z = 0;
148897 }else{
148898 int n = sqlite3Strlen30(z: argv[i])+1;
148899 z = sqlite3_malloc64( n );
148900 if( z==0 ) goto malloc_failed;
148901 memcpy(dest: z, src: argv[i], n: n);
148902 }
148903 p->azResult[p->nData++] = z;
148904 }
148905 p->nRow++;
148906 }
148907 return 0;
148908
148909malloc_failed:
148910 p->rc = SQLITE_NOMEM_BKPT;
148911 return 1;
148912}
148913
148914/*
148915** Query the database. But instead of invoking a callback for each row,
148916** malloc() for space to hold the result and return the entire results
148917** at the conclusion of the call.
148918**
148919** The result that is written to ***pazResult is held in memory obtained
148920** from malloc(). But the caller cannot free this memory directly.
148921** Instead, the entire table should be passed to sqlite3_free_table() when
148922** the calling procedure is finished using it.
148923*/
148924SQLITE_API int sqlite3_get_table(
148925 sqlite3 *db, /* The database on which the SQL executes */
148926 const char *zSql, /* The SQL to be executed */
148927 char ***pazResult, /* Write the result table here */
148928 int *pnRow, /* Write the number of rows in the result here */
148929 int *pnColumn, /* Write the number of columns of result here */
148930 char **pzErrMsg /* Write error messages here */
148931){
148932 int rc;
148933 TabResult res;
148934
148935#ifdef SQLITE_ENABLE_API_ARMOR
148936 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
148937#endif
148938 *pazResult = 0;
148939 if( pnColumn ) *pnColumn = 0;
148940 if( pnRow ) *pnRow = 0;
148941 if( pzErrMsg ) *pzErrMsg = 0;
148942 res.zErrMsg = 0;
148943 res.nRow = 0;
148944 res.nColumn = 0;
148945 res.nData = 1;
148946 res.nAlloc = 20;
148947 res.rc = SQLITE_OK;
148948 res.azResult = sqlite3_malloc64(n: sizeof(char*)*res.nAlloc );
148949 if( res.azResult==0 ){
148950 db->errCode = SQLITE_NOMEM;
148951 return SQLITE_NOMEM_BKPT;
148952 }
148953 res.azResult[0] = 0;
148954 rc = sqlite3_exec(db, zSql, xCallback: sqlite3_get_table_cb, pArg: &res, pzErrMsg);
148955 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
148956 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
148957 if( (rc&0xff)==SQLITE_ABORT ){
148958 sqlite3_free_table(result: &res.azResult[1]);
148959 if( res.zErrMsg ){
148960 if( pzErrMsg ){
148961 sqlite3_free(p: *pzErrMsg);
148962 *pzErrMsg = sqlite3_mprintf(zFormat: "%s",res.zErrMsg);
148963 }
148964 sqlite3_free(p: res.zErrMsg);
148965 }
148966 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
148967 return res.rc;
148968 }
148969 sqlite3_free(p: res.zErrMsg);
148970 if( rc!=SQLITE_OK ){
148971 sqlite3_free_table(result: &res.azResult[1]);
148972 return rc;
148973 }
148974 if( res.nAlloc>res.nData ){
148975 char **azNew;
148976 azNew = sqlite3Realloc( pOld: res.azResult, nBytes: sizeof(char*)*res.nData );
148977 if( azNew==0 ){
148978 sqlite3_free_table(result: &res.azResult[1]);
148979 db->errCode = SQLITE_NOMEM;
148980 return SQLITE_NOMEM_BKPT;
148981 }
148982 res.azResult = azNew;
148983 }
148984 *pazResult = &res.azResult[1];
148985 if( pnColumn ) *pnColumn = res.nColumn;
148986 if( pnRow ) *pnRow = res.nRow;
148987 return rc;
148988}
148989
148990/*
148991** This routine frees the space the sqlite3_get_table() malloced.
148992*/
148993SQLITE_API void sqlite3_free_table(
148994 char **azResult /* Result returned from sqlite3_get_table() */
148995){
148996 if( azResult ){
148997 int i, n;
148998 azResult--;
148999 assert( azResult!=0 );
149000 n = SQLITE_PTR_TO_INT(azResult[0]);
149001 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(p: azResult[i]); }
149002 sqlite3_free(p: azResult);
149003 }
149004}
149005
149006#endif /* SQLITE_OMIT_GET_TABLE */
149007
149008/************** End of table.c ***********************************************/
149009/************** Begin file trigger.c *****************************************/
149010/*
149011**
149012** The author disclaims copyright to this source code. In place of
149013** a legal notice, here is a blessing:
149014**
149015** May you do good and not evil.
149016** May you find forgiveness for yourself and forgive others.
149017** May you share freely, never taking more than you give.
149018**
149019*************************************************************************
149020** This file contains the implementation for TRIGGERs
149021*/
149022/* #include "sqliteInt.h" */
149023
149024#ifndef SQLITE_OMIT_TRIGGER
149025/*
149026** Delete a linked list of TriggerStep structures.
149027*/
149028SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
149029 while( pTriggerStep ){
149030 TriggerStep * pTmp = pTriggerStep;
149031 pTriggerStep = pTriggerStep->pNext;
149032
149033 sqlite3ExprDelete(db, p: pTmp->pWhere);
149034 sqlite3ExprListDelete(db, pList: pTmp->pExprList);
149035 sqlite3SelectDelete(db, p: pTmp->pSelect);
149036 sqlite3IdListDelete(db, pList: pTmp->pIdList);
149037 sqlite3UpsertDelete(db, pTmp->pUpsert);
149038 sqlite3SrcListDelete(db, pList: pTmp->pFrom);
149039 sqlite3DbFree(db, p: pTmp->zSpan);
149040
149041 sqlite3DbFree(db, p: pTmp);
149042 }
149043}
149044
149045/*
149046** Given table pTab, return a list of all the triggers attached to
149047** the table. The list is connected by Trigger.pNext pointers.
149048**
149049** All of the triggers on pTab that are in the same database as pTab
149050** are already attached to pTab->pTrigger. But there might be additional
149051** triggers on pTab in the TEMP schema. This routine prepends all
149052** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
149053** and returns the combined list.
149054**
149055** To state it another way: This routine returns a list of all triggers
149056** that fire off of pTab. The list will include any TEMP triggers on
149057** pTab as well as the triggers lised in pTab->pTrigger.
149058*/
149059SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
149060 Schema *pTmpSchema; /* Schema of the pTab table */
149061 Trigger *pList; /* List of triggers to return */
149062 HashElem *p; /* Loop variable for TEMP triggers */
149063
149064 assert( pParse->disableTriggers==0 );
149065 pTmpSchema = pParse->db->aDb[1].pSchema;
149066 p = sqliteHashFirst(&pTmpSchema->trigHash);
149067 pList = pTab->pTrigger;
149068 while( p ){
149069 Trigger *pTrig = (Trigger *)sqliteHashData(p);
149070 if( pTrig->pTabSchema==pTab->pSchema
149071 && pTrig->table
149072 && 0==sqlite3StrICmp(zLeft: pTrig->table, zRight: pTab->zName)
149073 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
149074 ){
149075 pTrig->pNext = pList;
149076 pList = pTrig;
149077 }else if( pTrig->op==TK_RETURNING ){
149078#ifndef SQLITE_OMIT_VIRTUALTABLE
149079 assert( pParse->db->pVtabCtx==0 );
149080#endif
149081 assert( pParse->bReturning );
149082 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
149083 pTrig->table = pTab->zName;
149084 pTrig->pTabSchema = pTab->pSchema;
149085 pTrig->pNext = pList;
149086 pList = pTrig;
149087 }
149088 p = sqliteHashNext(p);
149089 }
149090#if 0
149091 if( pList ){
149092 Trigger *pX;
149093 printf("Triggers for %s:", pTab->zName);
149094 for(pX=pList; pX; pX=pX->pNext){
149095 printf(" %s", pX->zName);
149096 }
149097 printf("\n");
149098 fflush(stdout);
149099 }
149100#endif
149101 return pList;
149102}
149103
149104/*
149105** This is called by the parser when it sees a CREATE TRIGGER statement
149106** up to the point of the BEGIN before the trigger actions. A Trigger
149107** structure is generated based on the information available and stored
149108** in pParse->pNewTrigger. After the trigger actions have been parsed, the
149109** sqlite3FinishTrigger() function is called to complete the trigger
149110** construction process.
149111*/
149112SQLITE_PRIVATE void sqlite3BeginTrigger(
149113 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
149114 Token *pName1, /* The name of the trigger */
149115 Token *pName2, /* The name of the trigger */
149116 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
149117 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
149118 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
149119 SrcList *pTableName,/* The name of the table/view the trigger applies to */
149120 Expr *pWhen, /* WHEN clause */
149121 int isTemp, /* True if the TEMPORARY keyword is present */
149122 int noErr /* Suppress errors if the trigger already exists */
149123){
149124 Trigger *pTrigger = 0; /* The new trigger */
149125 Table *pTab; /* Table that the trigger fires off of */
149126 char *zName = 0; /* Name of the trigger */
149127 sqlite3 *db = pParse->db; /* The database connection */
149128 int iDb; /* The database to store the trigger in */
149129 Token *pName; /* The unqualified db name */
149130 DbFixer sFix; /* State vector for the DB fixer */
149131
149132 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
149133 assert( pName2!=0 );
149134 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
149135 assert( op>0 && op<0xff );
149136 if( isTemp ){
149137 /* If TEMP was specified, then the trigger name may not be qualified. */
149138 if( pName2->n>0 ){
149139 sqlite3ErrorMsg(pParse, zFormat: "temporary trigger may not have qualified name");
149140 goto trigger_cleanup;
149141 }
149142 iDb = 1;
149143 pName = pName1;
149144 }else{
149145 /* Figure out the db that the trigger will be created in */
149146 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
149147 if( iDb<0 ){
149148 goto trigger_cleanup;
149149 }
149150 }
149151 if( !pTableName || db->mallocFailed ){
149152 goto trigger_cleanup;
149153 }
149154
149155 /* A long-standing parser bug is that this syntax was allowed:
149156 **
149157 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
149158 ** ^^^^^^^^
149159 **
149160 ** To maintain backwards compatibility, ignore the database
149161 ** name on pTableName if we are reparsing out of the schema table
149162 */
149163 if( db->init.busy && iDb!=1 ){
149164 sqlite3DbFree(db, p: pTableName->a[0].zDatabase);
149165 pTableName->a[0].zDatabase = 0;
149166 }
149167
149168 /* If the trigger name was unqualified, and the table is a temp table,
149169 ** then set iDb to 1 to create the trigger in the temporary database.
149170 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
149171 ** exist, the error is caught by the block below.
149172 */
149173 pTab = sqlite3SrcListLookup(pParse, pSrc: pTableName);
149174 if( db->init.busy==0 && pName2->n==0 && pTab
149175 && pTab->pSchema==db->aDb[1].pSchema ){
149176 iDb = 1;
149177 }
149178
149179 /* Ensure the table name matches database name and that the table exists */
149180 if( db->mallocFailed ) goto trigger_cleanup;
149181 assert( pTableName->nSrc==1 );
149182 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "trigger", pName);
149183 if( sqlite3FixSrcList(pFix: &sFix, pList: pTableName) ){
149184 goto trigger_cleanup;
149185 }
149186 pTab = sqlite3SrcListLookup(pParse, pSrc: pTableName);
149187 if( !pTab ){
149188 /* The table does not exist. */
149189 goto trigger_orphan_error;
149190 }
149191 if( IsVirtual(pTab) ){
149192 sqlite3ErrorMsg(pParse, zFormat: "cannot create triggers on virtual tables");
149193 goto trigger_orphan_error;
149194 }
149195
149196 /* Check that the trigger name is not reserved and that no trigger of the
149197 ** specified name exists */
149198 zName = sqlite3NameFromToken(db, pName);
149199 if( zName==0 ){
149200 assert( db->mallocFailed );
149201 goto trigger_cleanup;
149202 }
149203 if( sqlite3CheckObjectName(pParse, zName, zType: "trigger", zTblName: pTab->zName) ){
149204 goto trigger_cleanup;
149205 }
149206 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
149207 if( !IN_RENAME_OBJECT ){
149208 if( sqlite3HashFind(pH: &(db->aDb[iDb].pSchema->trigHash),pKey: zName) ){
149209 if( !noErr ){
149210 sqlite3ErrorMsg(pParse, zFormat: "trigger %T already exists", pName);
149211 }else{
149212 assert( !db->init.busy );
149213 sqlite3CodeVerifySchema(pParse, iDb);
149214 VVA_ONLY( pParse->ifNotExists = 1; )
149215 }
149216 goto trigger_cleanup;
149217 }
149218 }
149219
149220 /* Do not create a trigger on a system table */
149221 if( sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)==0 ){
149222 sqlite3ErrorMsg(pParse, zFormat: "cannot create trigger on system table");
149223 goto trigger_cleanup;
149224 }
149225
149226 /* INSTEAD of triggers are only for views and views only support INSTEAD
149227 ** of triggers.
149228 */
149229 if( IsView(pTab) && tr_tm!=TK_INSTEAD ){
149230 sqlite3ErrorMsg(pParse, zFormat: "cannot create %s trigger on view: %S",
149231 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
149232 goto trigger_orphan_error;
149233 }
149234 if( !IsView(pTab) && tr_tm==TK_INSTEAD ){
149235 sqlite3ErrorMsg(pParse, zFormat: "cannot create INSTEAD OF"
149236 " trigger on table: %S", pTableName->a);
149237 goto trigger_orphan_error;
149238 }
149239
149240#ifndef SQLITE_OMIT_AUTHORIZATION
149241 if( !IN_RENAME_OBJECT ){
149242 int iTabDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
149243 int code = SQLITE_CREATE_TRIGGER;
149244 const char *zDb = db->aDb[iTabDb].zDbSName;
149245 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
149246 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
149247 if( sqlite3AuthCheck(pParse, code, zArg1: zName, zArg2: pTab->zName, zArg3: zDbTrig) ){
149248 goto trigger_cleanup;
149249 }
149250 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),zArg2: 0,zArg3: zDb)){
149251 goto trigger_cleanup;
149252 }
149253 }
149254#endif
149255
149256 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
149257 ** cannot appear on views. So we might as well translate every
149258 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
149259 ** elsewhere.
149260 */
149261 if (tr_tm == TK_INSTEAD){
149262 tr_tm = TK_BEFORE;
149263 }
149264
149265 /* Build the Trigger object */
149266 pTrigger = (Trigger*)sqlite3DbMallocZero(db, n: sizeof(Trigger));
149267 if( pTrigger==0 ) goto trigger_cleanup;
149268 pTrigger->zName = zName;
149269 zName = 0;
149270 pTrigger->table = sqlite3DbStrDup(db, z: pTableName->a[0].zName);
149271 pTrigger->pSchema = db->aDb[iDb].pSchema;
149272 pTrigger->pTabSchema = pTab->pSchema;
149273 pTrigger->op = (u8)op;
149274 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
149275 if( IN_RENAME_OBJECT ){
149276 sqlite3RenameTokenRemap(pParse, pTo: pTrigger->table, pFrom: pTableName->a[0].zName);
149277 pTrigger->pWhen = pWhen;
149278 pWhen = 0;
149279 }else{
149280 pTrigger->pWhen = sqlite3ExprDup(db, p: pWhen, EXPRDUP_REDUCE);
149281 }
149282 pTrigger->pColumns = pColumns;
149283 pColumns = 0;
149284 assert( pParse->pNewTrigger==0 );
149285 pParse->pNewTrigger = pTrigger;
149286
149287trigger_cleanup:
149288 sqlite3DbFree(db, p: zName);
149289 sqlite3SrcListDelete(db, pList: pTableName);
149290 sqlite3IdListDelete(db, pList: pColumns);
149291 sqlite3ExprDelete(db, p: pWhen);
149292 if( !pParse->pNewTrigger ){
149293 sqlite3DeleteTrigger(db, pTrigger);
149294 }else{
149295 assert( pParse->pNewTrigger==pTrigger );
149296 }
149297 return;
149298
149299trigger_orphan_error:
149300 if( db->init.iDb==1 ){
149301 /* Ticket #3810.
149302 ** Normally, whenever a table is dropped, all associated triggers are
149303 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
149304 ** and the table is dropped by a different database connection, the
149305 ** trigger is not visible to the database connection that does the
149306 ** drop so the trigger cannot be dropped. This results in an
149307 ** "orphaned trigger" - a trigger whose associated table is missing.
149308 **
149309 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
149310 */
149311 db->init.orphanTrigger = 1;
149312 }
149313 goto trigger_cleanup;
149314}
149315
149316/*
149317** This routine is called after all of the trigger actions have been parsed
149318** in order to complete the process of building the trigger.
149319*/
149320SQLITE_PRIVATE void sqlite3FinishTrigger(
149321 Parse *pParse, /* Parser context */
149322 TriggerStep *pStepList, /* The triggered program */
149323 Token *pAll /* Token that describes the complete CREATE TRIGGER */
149324){
149325 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
149326 char *zName; /* Name of trigger */
149327 sqlite3 *db = pParse->db; /* The database */
149328 DbFixer sFix; /* Fixer object */
149329 int iDb; /* Database containing the trigger */
149330 Token nameToken; /* Trigger name for error reporting */
149331
149332 pParse->pNewTrigger = 0;
149333 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
149334 zName = pTrig->zName;
149335 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTrig->pSchema);
149336 pTrig->step_list = pStepList;
149337 while( pStepList ){
149338 pStepList->pTrig = pTrig;
149339 pStepList = pStepList->pNext;
149340 }
149341 sqlite3TokenInit(p: &nameToken, z: pTrig->zName);
149342 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "trigger", pName: &nameToken);
149343 if( sqlite3FixTriggerStep(pFix: &sFix, pStep: pTrig->step_list)
149344 || sqlite3FixExpr(pFix: &sFix, pExpr: pTrig->pWhen)
149345 ){
149346 goto triggerfinish_cleanup;
149347 }
149348
149349#ifndef SQLITE_OMIT_ALTERTABLE
149350 if( IN_RENAME_OBJECT ){
149351 assert( !db->init.busy );
149352 pParse->pNewTrigger = pTrig;
149353 pTrig = 0;
149354 }else
149355#endif
149356
149357 /* if we are not initializing,
149358 ** build the sqlite_schema entry
149359 */
149360 if( !db->init.busy ){
149361 Vdbe *v;
149362 char *z;
149363
149364 /* If this is a new CREATE TABLE statement, and if shadow tables
149365 ** are read-only, and the trigger makes a change to a shadow table,
149366 ** then raise an error - do not allow the trigger to be created. */
149367 if( sqlite3ReadOnlyShadowTables(db) ){
149368 TriggerStep *pStep;
149369 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
149370 if( pStep->zTarget!=0
149371 && sqlite3ShadowTableName(db, zName: pStep->zTarget)
149372 ){
149373 sqlite3ErrorMsg(pParse,
149374 zFormat: "trigger \"%s\" may not write to shadow table \"%s\"",
149375 pTrig->zName, pStep->zTarget);
149376 goto triggerfinish_cleanup;
149377 }
149378 }
149379 }
149380
149381 /* Make an entry in the sqlite_schema table */
149382 v = sqlite3GetVdbe(pParse);
149383 if( v==0 ) goto triggerfinish_cleanup;
149384 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
149385 z = sqlite3DbStrNDup(db, z: (char*)pAll->z, n: pAll->n);
149386 testcase( z==0 );
149387 sqlite3NestedParse(pParse,
149388 zFormat: "INSERT INTO %Q." LEGACY_SCHEMA_TABLE
149389 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
149390 db->aDb[iDb].zDbSName, zName,
149391 pTrig->table, z);
149392 sqlite3DbFree(db, p: z);
149393 sqlite3ChangeCookie(pParse, iDb);
149394 sqlite3VdbeAddParseSchemaOp(p: v, iDb,
149395 zWhere: sqlite3MPrintf(db, zFormat: "type='trigger' AND name='%q'", zName), p5: 0);
149396 }
149397
149398 if( db->init.busy ){
149399 Trigger *pLink = pTrig;
149400 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
149401 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
149402 assert( pLink!=0 );
149403 pTrig = sqlite3HashInsert(pH: pHash, pKey: zName, data: pTrig);
149404 if( pTrig ){
149405 sqlite3OomFault(db);
149406 }else if( pLink->pSchema==pLink->pTabSchema ){
149407 Table *pTab;
149408 pTab = sqlite3HashFind(pH: &pLink->pTabSchema->tblHash, pKey: pLink->table);
149409 assert( pTab!=0 );
149410 pLink->pNext = pTab->pTrigger;
149411 pTab->pTrigger = pLink;
149412 }
149413 }
149414
149415triggerfinish_cleanup:
149416 sqlite3DeleteTrigger(db, pTrig);
149417 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
149418 sqlite3DeleteTriggerStep(db, pTriggerStep: pStepList);
149419}
149420
149421/*
149422** Duplicate a range of text from an SQL statement, then convert all
149423** whitespace characters into ordinary space characters.
149424*/
149425static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
149426 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
149427 int i;
149428 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
149429 return z;
149430}
149431
149432/*
149433** Turn a SELECT statement (that the pSelect parameter points to) into
149434** a trigger step. Return a pointer to a TriggerStep structure.
149435**
149436** The parser calls this routine when it finds a SELECT statement in
149437** body of a TRIGGER.
149438*/
149439SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
149440 sqlite3 *db, /* Database connection */
149441 Select *pSelect, /* The SELECT statement */
149442 const char *zStart, /* Start of SQL text */
149443 const char *zEnd /* End of SQL text */
149444){
149445 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, n: sizeof(TriggerStep));
149446 if( pTriggerStep==0 ) {
149447 sqlite3SelectDelete(db, p: pSelect);
149448 return 0;
149449 }
149450 pTriggerStep->op = TK_SELECT;
149451 pTriggerStep->pSelect = pSelect;
149452 pTriggerStep->orconf = OE_Default;
149453 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
149454 return pTriggerStep;
149455}
149456
149457/*
149458** Allocate space to hold a new trigger step. The allocated space
149459** holds both the TriggerStep object and the TriggerStep.target.z string.
149460**
149461** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
149462*/
149463static TriggerStep *triggerStepAllocate(
149464 Parse *pParse, /* Parser context */
149465 u8 op, /* Trigger opcode */
149466 Token *pName, /* The target name */
149467 const char *zStart, /* Start of SQL text */
149468 const char *zEnd /* End of SQL text */
149469){
149470 sqlite3 *db = pParse->db;
149471 TriggerStep *pTriggerStep;
149472
149473 if( pParse->nErr ) return 0;
149474 pTriggerStep = sqlite3DbMallocZero(db, n: sizeof(TriggerStep) + pName->n + 1);
149475 if( pTriggerStep ){
149476 char *z = (char*)&pTriggerStep[1];
149477 memcpy(dest: z, src: pName->z, n: pName->n);
149478 sqlite3Dequote(z);
149479 pTriggerStep->zTarget = z;
149480 pTriggerStep->op = op;
149481 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
149482 if( IN_RENAME_OBJECT ){
149483 sqlite3RenameTokenMap(pParse, pPtr: pTriggerStep->zTarget, pToken: pName);
149484 }
149485 }
149486 return pTriggerStep;
149487}
149488
149489/*
149490** Build a trigger step out of an INSERT statement. Return a pointer
149491** to the new trigger step.
149492**
149493** The parser calls this routine when it sees an INSERT inside the
149494** body of a trigger.
149495*/
149496SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
149497 Parse *pParse, /* Parser */
149498 Token *pTableName, /* Name of the table into which we insert */
149499 IdList *pColumn, /* List of columns in pTableName to insert into */
149500 Select *pSelect, /* A SELECT statement that supplies values */
149501 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
149502 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
149503 const char *zStart, /* Start of SQL text */
149504 const char *zEnd /* End of SQL text */
149505){
149506 sqlite3 *db = pParse->db;
149507 TriggerStep *pTriggerStep;
149508
149509 assert(pSelect != 0 || db->mallocFailed);
149510
149511 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pName: pTableName,zStart,zEnd);
149512 if( pTriggerStep ){
149513 if( IN_RENAME_OBJECT ){
149514 pTriggerStep->pSelect = pSelect;
149515 pSelect = 0;
149516 }else{
149517 pTriggerStep->pSelect = sqlite3SelectDup(db, pDup: pSelect, EXPRDUP_REDUCE);
149518 }
149519 pTriggerStep->pIdList = pColumn;
149520 pTriggerStep->pUpsert = pUpsert;
149521 pTriggerStep->orconf = orconf;
149522 if( pUpsert ){
149523 sqlite3HasExplicitNulls(pParse, pList: pUpsert->pUpsertTarget);
149524 }
149525 }else{
149526 testcase( pColumn );
149527 sqlite3IdListDelete(db, pList: pColumn);
149528 testcase( pUpsert );
149529 sqlite3UpsertDelete(db, pUpsert);
149530 }
149531 sqlite3SelectDelete(db, p: pSelect);
149532
149533 return pTriggerStep;
149534}
149535
149536/*
149537** Construct a trigger step that implements an UPDATE statement and return
149538** a pointer to that trigger step. The parser calls this routine when it
149539** sees an UPDATE statement inside the body of a CREATE TRIGGER.
149540*/
149541SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
149542 Parse *pParse, /* Parser */
149543 Token *pTableName, /* Name of the table to be updated */
149544 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
149545 ExprList *pEList, /* The SET clause: list of column and new values */
149546 Expr *pWhere, /* The WHERE clause */
149547 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
149548 const char *zStart, /* Start of SQL text */
149549 const char *zEnd /* End of SQL text */
149550){
149551 sqlite3 *db = pParse->db;
149552 TriggerStep *pTriggerStep;
149553
149554 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pName: pTableName,zStart,zEnd);
149555 if( pTriggerStep ){
149556 if( IN_RENAME_OBJECT ){
149557 pTriggerStep->pExprList = pEList;
149558 pTriggerStep->pWhere = pWhere;
149559 pTriggerStep->pFrom = pFrom;
149560 pEList = 0;
149561 pWhere = 0;
149562 pFrom = 0;
149563 }else{
149564 pTriggerStep->pExprList = sqlite3ExprListDup(db, p: pEList, EXPRDUP_REDUCE);
149565 pTriggerStep->pWhere = sqlite3ExprDup(db, p: pWhere, EXPRDUP_REDUCE);
149566 pTriggerStep->pFrom = sqlite3SrcListDup(db, p: pFrom, EXPRDUP_REDUCE);
149567 }
149568 pTriggerStep->orconf = orconf;
149569 }
149570 sqlite3ExprListDelete(db, pList: pEList);
149571 sqlite3ExprDelete(db, p: pWhere);
149572 sqlite3SrcListDelete(db, pList: pFrom);
149573 return pTriggerStep;
149574}
149575
149576/*
149577** Construct a trigger step that implements a DELETE statement and return
149578** a pointer to that trigger step. The parser calls this routine when it
149579** sees a DELETE statement inside the body of a CREATE TRIGGER.
149580*/
149581SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
149582 Parse *pParse, /* Parser */
149583 Token *pTableName, /* The table from which rows are deleted */
149584 Expr *pWhere, /* The WHERE clause */
149585 const char *zStart, /* Start of SQL text */
149586 const char *zEnd /* End of SQL text */
149587){
149588 sqlite3 *db = pParse->db;
149589 TriggerStep *pTriggerStep;
149590
149591 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pName: pTableName,zStart,zEnd);
149592 if( pTriggerStep ){
149593 if( IN_RENAME_OBJECT ){
149594 pTriggerStep->pWhere = pWhere;
149595 pWhere = 0;
149596 }else{
149597 pTriggerStep->pWhere = sqlite3ExprDup(db, p: pWhere, EXPRDUP_REDUCE);
149598 }
149599 pTriggerStep->orconf = OE_Default;
149600 }
149601 sqlite3ExprDelete(db, p: pWhere);
149602 return pTriggerStep;
149603}
149604
149605/*
149606** Recursively delete a Trigger structure
149607*/
149608SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
149609 if( pTrigger==0 || pTrigger->bReturning ) return;
149610 sqlite3DeleteTriggerStep(db, pTriggerStep: pTrigger->step_list);
149611 sqlite3DbFree(db, p: pTrigger->zName);
149612 sqlite3DbFree(db, p: pTrigger->table);
149613 sqlite3ExprDelete(db, p: pTrigger->pWhen);
149614 sqlite3IdListDelete(db, pList: pTrigger->pColumns);
149615 sqlite3DbFree(db, p: pTrigger);
149616}
149617
149618/*
149619** This function is called to drop a trigger from the database schema.
149620**
149621** This may be called directly from the parser and therefore identifies
149622** the trigger by name. The sqlite3DropTriggerPtr() routine does the
149623** same job as this routine except it takes a pointer to the trigger
149624** instead of the trigger name.
149625**/
149626SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
149627 Trigger *pTrigger = 0;
149628 int i;
149629 const char *zDb;
149630 const char *zName;
149631 sqlite3 *db = pParse->db;
149632
149633 if( db->mallocFailed ) goto drop_trigger_cleanup;
149634 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
149635 goto drop_trigger_cleanup;
149636 }
149637
149638 assert( pName->nSrc==1 );
149639 zDb = pName->a[0].zDatabase;
149640 zName = pName->a[0].zName;
149641 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
149642 for(i=OMIT_TEMPDB; i<db->nDb; i++){
149643 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
149644 if( zDb && sqlite3DbIsNamed(db, iDb: j, zName: zDb)==0 ) continue;
149645 assert( sqlite3SchemaMutexHeld(db, j, 0) );
149646 pTrigger = sqlite3HashFind(pH: &(db->aDb[j].pSchema->trigHash), pKey: zName);
149647 if( pTrigger ) break;
149648 }
149649 if( !pTrigger ){
149650 if( !noErr ){
149651 sqlite3ErrorMsg(pParse, zFormat: "no such trigger: %S", pName->a);
149652 }else{
149653 sqlite3CodeVerifyNamedSchema(pParse, zDb);
149654 }
149655 pParse->checkSchema = 1;
149656 goto drop_trigger_cleanup;
149657 }
149658 sqlite3DropTriggerPtr(pParse, pTrigger);
149659
149660drop_trigger_cleanup:
149661 sqlite3SrcListDelete(db, pList: pName);
149662}
149663
149664/*
149665** Return a pointer to the Table structure for the table that a trigger
149666** is set on.
149667*/
149668static Table *tableOfTrigger(Trigger *pTrigger){
149669 return sqlite3HashFind(pH: &pTrigger->pTabSchema->tblHash, pKey: pTrigger->table);
149670}
149671
149672
149673/*
149674** Drop a trigger given a pointer to that trigger.
149675*/
149676SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
149677 Table *pTable;
149678 Vdbe *v;
149679 sqlite3 *db = pParse->db;
149680 int iDb;
149681
149682 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTrigger->pSchema);
149683 assert( iDb>=0 && iDb<db->nDb );
149684 pTable = tableOfTrigger(pTrigger);
149685 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
149686#ifndef SQLITE_OMIT_AUTHORIZATION
149687 if( pTable ){
149688 int code = SQLITE_DROP_TRIGGER;
149689 const char *zDb = db->aDb[iDb].zDbSName;
149690 const char *zTab = SCHEMA_TABLE(iDb);
149691 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
149692 if( sqlite3AuthCheck(pParse, code, zArg1: pTrigger->zName, zArg2: pTable->zName, zArg3: zDb) ||
149693 sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: zTab, zArg2: 0, zArg3: zDb) ){
149694 return;
149695 }
149696 }
149697#endif
149698
149699 /* Generate code to destroy the database record of the trigger.
149700 */
149701 if( (v = sqlite3GetVdbe(pParse))!=0 ){
149702 sqlite3NestedParse(pParse,
149703 zFormat: "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
149704 db->aDb[iDb].zDbSName, pTrigger->zName
149705 );
149706 sqlite3ChangeCookie(pParse, iDb);
149707 sqlite3VdbeAddOp4(p: v, OP_DropTrigger, p1: iDb, p2: 0, p3: 0, zP4: pTrigger->zName, p4type: 0);
149708 }
149709}
149710
149711/*
149712** Remove a trigger from the hash tables of the sqlite* pointer.
149713*/
149714SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
149715 Trigger *pTrigger;
149716 Hash *pHash;
149717
149718 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
149719 pHash = &(db->aDb[iDb].pSchema->trigHash);
149720 pTrigger = sqlite3HashInsert(pH: pHash, pKey: zName, data: 0);
149721 if( ALWAYS(pTrigger) ){
149722 if( pTrigger->pSchema==pTrigger->pTabSchema ){
149723 Table *pTab = tableOfTrigger(pTrigger);
149724 if( pTab ){
149725 Trigger **pp;
149726 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
149727 if( *pp==pTrigger ){
149728 *pp = (*pp)->pNext;
149729 break;
149730 }
149731 }
149732 }
149733 }
149734 sqlite3DeleteTrigger(db, pTrigger);
149735 db->mDbFlags |= DBFLAG_SchemaChange;
149736 }
149737}
149738
149739/*
149740** pEList is the SET clause of an UPDATE statement. Each entry
149741** in pEList is of the format <id>=<expr>. If any of the entries
149742** in pEList have an <id> which matches an identifier in pIdList,
149743** then return TRUE. If pIdList==NULL, then it is considered a
149744** wildcard that matches anything. Likewise if pEList==NULL then
149745** it matches anything so always return true. Return false only
149746** if there is no match.
149747*/
149748static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
149749 int e;
149750 if( pIdList==0 || NEVER(pEList==0) ) return 1;
149751 for(e=0; e<pEList->nExpr; e++){
149752 if( sqlite3IdListIndex(pList: pIdList, zName: pEList->a[e].zEName)>=0 ) return 1;
149753 }
149754 return 0;
149755}
149756
149757/*
149758** Return true if any TEMP triggers exist
149759*/
149760static int tempTriggersExist(sqlite3 *db){
149761 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
149762 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
149763 return 1;
149764}
149765
149766/*
149767** Return a list of all triggers on table pTab if there exists at least
149768** one trigger that must be fired when an operation of type 'op' is
149769** performed on the table, and, if that operation is an UPDATE, if at
149770** least one of the columns in pChanges is being modified.
149771*/
149772static SQLITE_NOINLINE Trigger *triggersReallyExist(
149773 Parse *pParse, /* Parse context */
149774 Table *pTab, /* The table the contains the triggers */
149775 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
149776 ExprList *pChanges, /* Columns that change in an UPDATE statement */
149777 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
149778){
149779 int mask = 0;
149780 Trigger *pList = 0;
149781 Trigger *p;
149782
149783 pList = sqlite3TriggerList(pParse, pTab);
149784 assert( pList==0 || IsVirtual(pTab)==0
149785 || (pList->bReturning && pList->pNext==0) );
149786 if( pList!=0 ){
149787 p = pList;
149788 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
149789 && pTab->pTrigger!=0
149790 ){
149791 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
149792 ** only TEMP triggers are allowed. Truncate the pList so that it
149793 ** includes only TEMP triggers */
149794 if( pList==pTab->pTrigger ){
149795 pList = 0;
149796 goto exit_triggers_exist;
149797 }
149798 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
149799 p->pNext = 0;
149800 p = pList;
149801 }
149802 do{
149803 if( p->op==op && checkColumnOverlap(pIdList: p->pColumns, pEList: pChanges) ){
149804 mask |= p->tr_tm;
149805 }else if( p->op==TK_RETURNING ){
149806 /* The first time a RETURNING trigger is seen, the "op" value tells
149807 ** us what time of trigger it should be. */
149808 assert( sqlite3IsToplevel(pParse) );
149809 p->op = op;
149810 if( IsVirtual(pTab) ){
149811 if( op!=TK_INSERT ){
149812 sqlite3ErrorMsg(pParse,
149813 zFormat: "%s RETURNING is not available on virtual tables",
149814 op==TK_DELETE ? "DELETE" : "UPDATE");
149815 }
149816 p->tr_tm = TRIGGER_BEFORE;
149817 }else{
149818 p->tr_tm = TRIGGER_AFTER;
149819 }
149820 mask |= p->tr_tm;
149821 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
149822 && sqlite3IsToplevel(pParse) ){
149823 /* Also fire a RETURNING trigger for an UPSERT */
149824 mask |= p->tr_tm;
149825 }
149826 p = p->pNext;
149827 }while( p );
149828 }
149829exit_triggers_exist:
149830 if( pMask ){
149831 *pMask = mask;
149832 }
149833 return (mask ? pList : 0);
149834}
149835SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
149836 Parse *pParse, /* Parse context */
149837 Table *pTab, /* The table the contains the triggers */
149838 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
149839 ExprList *pChanges, /* Columns that change in an UPDATE statement */
149840 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
149841){
149842 assert( pTab!=0 );
149843 if( (pTab->pTrigger==0 && !tempTriggersExist(db: pParse->db))
149844 || pParse->disableTriggers
149845 ){
149846 if( pMask ) *pMask = 0;
149847 return 0;
149848 }
149849 return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
149850}
149851
149852/*
149853** Convert the pStep->zTarget string into a SrcList and return a pointer
149854** to that SrcList.
149855**
149856** This routine adds a specific database name, if needed, to the target when
149857** forming the SrcList. This prevents a trigger in one database from
149858** referring to a target in another database. An exception is when the
149859** trigger is in TEMP in which case it can refer to any other database it
149860** wants.
149861*/
149862SQLITE_PRIVATE SrcList *sqlite3TriggerStepSrc(
149863 Parse *pParse, /* The parsing context */
149864 TriggerStep *pStep /* The trigger containing the target token */
149865){
149866 sqlite3 *db = pParse->db;
149867 SrcList *pSrc; /* SrcList to be returned */
149868 char *zName = sqlite3DbStrDup(db, z: pStep->zTarget);
149869 pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
149870 assert( pSrc==0 || pSrc->nSrc==1 );
149871 assert( zName || pSrc==0 );
149872 if( pSrc ){
149873 Schema *pSchema = pStep->pTrig->pSchema;
149874 pSrc->a[0].zName = zName;
149875 if( pSchema!=db->aDb[1].pSchema ){
149876 pSrc->a[0].pSchema = pSchema;
149877 }
149878 if( pStep->pFrom ){
149879 SrcList *pDup = sqlite3SrcListDup(db, p: pStep->pFrom, flags: 0);
149880 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
149881 Select *pSubquery;
149882 Token as;
149883 pSubquery = sqlite3SelectNew(pParse,pEList: 0,pSrc: pDup,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_NestedFrom,pLimit: 0);
149884 as.n = 0;
149885 as.z = 0;
149886 pDup = sqlite3SrcListAppendFromTerm(pParse,p: 0,pTable: 0,pDatabase: 0,pAlias: &as,pSubquery,pOnUsing: 0);
149887 }
149888 pSrc = sqlite3SrcListAppendList(pParse, p1: pSrc, p2: pDup);
149889 }
149890 }else{
149891 sqlite3DbFree(db, p: zName);
149892 }
149893 return pSrc;
149894}
149895
149896/*
149897** Return true if the pExpr term from the RETURNING clause argument
149898** list is of the form "*". Raise an error if the terms if of the
149899** form "table.*".
149900*/
149901static int isAsteriskTerm(
149902 Parse *pParse, /* Parsing context */
149903 Expr *pTerm /* A term in the RETURNING clause */
149904){
149905 assert( pTerm!=0 );
149906 if( pTerm->op==TK_ASTERISK ) return 1;
149907 if( pTerm->op!=TK_DOT ) return 0;
149908 assert( pTerm->pRight!=0 );
149909 assert( pTerm->pLeft!=0 );
149910 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
149911 sqlite3ErrorMsg(pParse, zFormat: "RETURNING may not use \"TABLE.*\" wildcards");
149912 return 1;
149913}
149914
149915/* The input list pList is the list of result set terms from a RETURNING
149916** clause. The table that we are returning from is pTab.
149917**
149918** This routine makes a copy of the pList, and at the same time expands
149919** any "*" wildcards to be the complete set of columns from pTab.
149920*/
149921static ExprList *sqlite3ExpandReturning(
149922 Parse *pParse, /* Parsing context */
149923 ExprList *pList, /* The arguments to RETURNING */
149924 Table *pTab /* The table being updated */
149925){
149926 ExprList *pNew = 0;
149927 sqlite3 *db = pParse->db;
149928 int i;
149929
149930 for(i=0; i<pList->nExpr; i++){
149931 Expr *pOldExpr = pList->a[i].pExpr;
149932 if( NEVER(pOldExpr==0) ) continue;
149933 if( isAsteriskTerm(pParse, pTerm: pOldExpr) ){
149934 int jj;
149935 for(jj=0; jj<pTab->nCol; jj++){
149936 Expr *pNewExpr;
149937 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
149938 pNewExpr = sqlite3Expr(db, TK_ID, zToken: pTab->aCol[jj].zCnName);
149939 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: pNewExpr);
149940 if( !db->mallocFailed ){
149941 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
149942 pItem->zEName = sqlite3DbStrDup(db, z: pTab->aCol[jj].zCnName);
149943 pItem->fg.eEName = ENAME_NAME;
149944 }
149945 }
149946 }else{
149947 Expr *pNewExpr = sqlite3ExprDup(db, p: pOldExpr, flags: 0);
149948 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: pNewExpr);
149949 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
149950 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
149951 pItem->zEName = sqlite3DbStrDup(db, z: pList->a[i].zEName);
149952 pItem->fg.eEName = pList->a[i].fg.eEName;
149953 }
149954 }
149955 }
149956 return pNew;
149957}
149958
149959/*
149960** Generate code for the RETURNING trigger. Unlike other triggers
149961** that invoke a subprogram in the bytecode, the code for RETURNING
149962** is generated in-line.
149963*/
149964static void codeReturningTrigger(
149965 Parse *pParse, /* Parse context */
149966 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
149967 Table *pTab, /* The table to code triggers from */
149968 int regIn /* The first in an array of registers */
149969){
149970 Vdbe *v = pParse->pVdbe;
149971 sqlite3 *db = pParse->db;
149972 ExprList *pNew;
149973 Returning *pReturning;
149974 Select sSelect;
149975 SrcList sFrom;
149976
149977 assert( v!=0 );
149978 assert( pParse->bReturning );
149979 assert( db->pParse==pParse );
149980 pReturning = pParse->u1.pReturning;
149981 assert( pTrigger == &(pReturning->retTrig) );
149982 memset(s: &sSelect, c: 0, n: sizeof(sSelect));
149983 memset(s: &sFrom, c: 0, n: sizeof(sFrom));
149984 sSelect.pEList = sqlite3ExprListDup(db, p: pReturning->pReturnEL, flags: 0);
149985 sSelect.pSrc = &sFrom;
149986 sFrom.nSrc = 1;
149987 sFrom.a[0].pTab = pTab;
149988 sFrom.a[0].iCursor = -1;
149989 sqlite3SelectPrep(pParse, p: &sSelect, pOuterNC: 0);
149990 if( pParse->nErr==0 ){
149991 assert( db->mallocFailed==0 );
149992 sqlite3GenerateColumnNames(pParse, pSelect: &sSelect);
149993 }
149994 sqlite3ExprListDelete(db, pList: sSelect.pEList);
149995 pNew = sqlite3ExpandReturning(pParse, pList: pReturning->pReturnEL, pTab);
149996 if( pParse->nErr==0 ){
149997 NameContext sNC;
149998 memset(s: &sNC, c: 0, n: sizeof(sNC));
149999 if( pReturning->nRetCol==0 ){
150000 pReturning->nRetCol = pNew->nExpr;
150001 pReturning->iRetCur = pParse->nTab++;
150002 }
150003 sNC.pParse = pParse;
150004 sNC.uNC.iBaseReg = regIn;
150005 sNC.ncFlags = NC_UBaseReg;
150006 pParse->eTriggerOp = pTrigger->op;
150007 pParse->pTriggerTab = pTab;
150008 if( sqlite3ResolveExprListNames(pNC: &sNC, pList: pNew)==SQLITE_OK
150009 && ALWAYS(!db->mallocFailed)
150010 ){
150011 int i;
150012 int nCol = pNew->nExpr;
150013 int reg = pParse->nMem+1;
150014 pParse->nMem += nCol+2;
150015 pReturning->iRetReg = reg;
150016 for(i=0; i<nCol; i++){
150017 Expr *pCol = pNew->a[i].pExpr;
150018 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
150019 sqlite3ExprCodeFactorable(pParse, pExpr: pCol, target: reg+i);
150020 if( sqlite3ExprAffinity(pExpr: pCol)==SQLITE_AFF_REAL ){
150021 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: reg+i);
150022 }
150023 }
150024 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: reg, p2: i, p3: reg+i);
150025 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: pReturning->iRetCur, p2: reg+i+1);
150026 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: pReturning->iRetCur, p2: reg+i, p3: reg+i+1);
150027 }
150028 }
150029 sqlite3ExprListDelete(db, pList: pNew);
150030 pParse->eTriggerOp = 0;
150031 pParse->pTriggerTab = 0;
150032}
150033
150034
150035
150036/*
150037** Generate VDBE code for the statements inside the body of a single
150038** trigger.
150039*/
150040static int codeTriggerProgram(
150041 Parse *pParse, /* The parser context */
150042 TriggerStep *pStepList, /* List of statements inside the trigger body */
150043 int orconf /* Conflict algorithm. (OE_Abort, etc) */
150044){
150045 TriggerStep *pStep;
150046 Vdbe *v = pParse->pVdbe;
150047 sqlite3 *db = pParse->db;
150048
150049 assert( pParse->pTriggerTab && pParse->pToplevel );
150050 assert( pStepList );
150051 assert( v!=0 );
150052 for(pStep=pStepList; pStep; pStep=pStep->pNext){
150053 /* Figure out the ON CONFLICT policy that will be used for this step
150054 ** of the trigger program. If the statement that caused this trigger
150055 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
150056 ** the ON CONFLICT policy that was specified as part of the trigger
150057 ** step statement. Example:
150058 **
150059 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
150060 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
150061 ** END;
150062 **
150063 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
150064 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
150065 */
150066 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
150067 assert( pParse->okConstFactor==0 );
150068
150069#ifndef SQLITE_OMIT_TRACE
150070 if( pStep->zSpan ){
150071 sqlite3VdbeAddOp4(p: v, OP_Trace, p1: 0x7fffffff, p2: 1, p3: 0,
150072 zP4: sqlite3MPrintf(db, zFormat: "-- %s", pStep->zSpan),
150073 P4_DYNAMIC);
150074 }
150075#endif
150076
150077 switch( pStep->op ){
150078 case TK_UPDATE: {
150079 sqlite3Update(pParse,
150080 sqlite3TriggerStepSrc(pParse, pStep),
150081 sqlite3ExprListDup(db, p: pStep->pExprList, flags: 0),
150082 sqlite3ExprDup(db, p: pStep->pWhere, flags: 0),
150083 pParse->eOrconf, 0, 0, 0
150084 );
150085 sqlite3VdbeAddOp0(p: v, OP_ResetCount);
150086 break;
150087 }
150088 case TK_INSERT: {
150089 sqlite3Insert(pParse,
150090 pTabList: sqlite3TriggerStepSrc(pParse, pStep),
150091 pSelect: sqlite3SelectDup(db, pDup: pStep->pSelect, flags: 0),
150092 pColumn: sqlite3IdListDup(db, p: pStep->pIdList),
150093 onError: pParse->eOrconf,
150094 pUpsert: sqlite3UpsertDup(db, pStep->pUpsert)
150095 );
150096 sqlite3VdbeAddOp0(p: v, OP_ResetCount);
150097 break;
150098 }
150099 case TK_DELETE: {
150100 sqlite3DeleteFrom(pParse,
150101 pTabList: sqlite3TriggerStepSrc(pParse, pStep),
150102 pWhere: sqlite3ExprDup(db, p: pStep->pWhere, flags: 0), pOrderBy: 0, pLimit: 0
150103 );
150104 sqlite3VdbeAddOp0(p: v, OP_ResetCount);
150105 break;
150106 }
150107 default: assert( pStep->op==TK_SELECT ); {
150108 SelectDest sDest;
150109 Select *pSelect = sqlite3SelectDup(db, pDup: pStep->pSelect, flags: 0);
150110 sqlite3SelectDestInit(pDest: &sDest, SRT_Discard, iParm: 0);
150111 sqlite3Select(pParse, p: pSelect, pDest: &sDest);
150112 sqlite3SelectDelete(db, p: pSelect);
150113 break;
150114 }
150115 }
150116 }
150117
150118 return 0;
150119}
150120
150121#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
150122/*
150123** This function is used to add VdbeComment() annotations to a VDBE
150124** program. It is not used in production code, only for debugging.
150125*/
150126static const char *onErrorText(int onError){
150127 switch( onError ){
150128 case OE_Abort: return "abort";
150129 case OE_Rollback: return "rollback";
150130 case OE_Fail: return "fail";
150131 case OE_Replace: return "replace";
150132 case OE_Ignore: return "ignore";
150133 case OE_Default: return "default";
150134 }
150135 return "n/a";
150136}
150137#endif
150138
150139/*
150140** Parse context structure pFrom has just been used to create a sub-vdbe
150141** (trigger program). If an error has occurred, transfer error information
150142** from pFrom to pTo.
150143*/
150144static void transferParseError(Parse *pTo, Parse *pFrom){
150145 assert( pFrom->zErrMsg==0 || pFrom->nErr );
150146 assert( pTo->zErrMsg==0 || pTo->nErr );
150147 if( pTo->nErr==0 ){
150148 pTo->zErrMsg = pFrom->zErrMsg;
150149 pTo->nErr = pFrom->nErr;
150150 pTo->rc = pFrom->rc;
150151 }else{
150152 sqlite3DbFree(db: pFrom->db, p: pFrom->zErrMsg);
150153 }
150154}
150155
150156/*
150157** Create and populate a new TriggerPrg object with a sub-program
150158** implementing trigger pTrigger with ON CONFLICT policy orconf.
150159*/
150160static TriggerPrg *codeRowTrigger(
150161 Parse *pParse, /* Current parse context */
150162 Trigger *pTrigger, /* Trigger to code */
150163 Table *pTab, /* The table pTrigger is attached to */
150164 int orconf /* ON CONFLICT policy to code trigger program with */
150165){
150166 Parse *pTop = sqlite3ParseToplevel(pParse);
150167 sqlite3 *db = pParse->db; /* Database handle */
150168 TriggerPrg *pPrg; /* Value to return */
150169 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
150170 Vdbe *v; /* Temporary VM */
150171 NameContext sNC; /* Name context for sub-vdbe */
150172 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
150173 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
150174 Parse sSubParse; /* Parse context for sub-vdbe */
150175
150176 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
150177 assert( pTop->pVdbe );
150178
150179 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
150180 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
150181 ** list of the top-level Parse object sooner rather than later. */
150182 pPrg = sqlite3DbMallocZero(db, n: sizeof(TriggerPrg));
150183 if( !pPrg ) return 0;
150184 pPrg->pNext = pTop->pTriggerPrg;
150185 pTop->pTriggerPrg = pPrg;
150186 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, n: sizeof(SubProgram));
150187 if( !pProgram ) return 0;
150188 sqlite3VdbeLinkSubProgram(pVdbe: pTop->pVdbe, p: pProgram);
150189 pPrg->pTrigger = pTrigger;
150190 pPrg->orconf = orconf;
150191 pPrg->aColmask[0] = 0xffffffff;
150192 pPrg->aColmask[1] = 0xffffffff;
150193
150194 /* Allocate and populate a new Parse context to use for coding the
150195 ** trigger sub-program. */
150196 sqlite3ParseObjectInit(pParse: &sSubParse, db);
150197 memset(s: &sNC, c: 0, n: sizeof(sNC));
150198 sNC.pParse = &sSubParse;
150199 sSubParse.pTriggerTab = pTab;
150200 sSubParse.pToplevel = pTop;
150201 sSubParse.zAuthContext = pTrigger->zName;
150202 sSubParse.eTriggerOp = pTrigger->op;
150203 sSubParse.nQueryLoop = pParse->nQueryLoop;
150204 sSubParse.prepFlags = pParse->prepFlags;
150205
150206 v = sqlite3GetVdbe(pParse: &sSubParse);
150207 if( v ){
150208 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
150209 pTrigger->zName, onErrorText(orconf),
150210 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
150211 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
150212 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
150213 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
150214 pTab->zName
150215 ));
150216#ifndef SQLITE_OMIT_TRACE
150217 if( pTrigger->zName ){
150218 sqlite3VdbeChangeP4(p: v, addr: -1,
150219 zP4: sqlite3MPrintf(db, zFormat: "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
150220 );
150221 }
150222#endif
150223
150224 /* If one was specified, code the WHEN clause. If it evaluates to false
150225 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
150226 ** OP_Halt inserted at the end of the program. */
150227 if( pTrigger->pWhen ){
150228 pWhen = sqlite3ExprDup(db, p: pTrigger->pWhen, flags: 0);
150229 if( db->mallocFailed==0
150230 && SQLITE_OK==sqlite3ResolveExprNames(pNC: &sNC, pExpr: pWhen)
150231 ){
150232 iEndTrigger = sqlite3VdbeMakeLabel(pParse: &sSubParse);
150233 sqlite3ExprIfFalse(pParse: &sSubParse, pExpr: pWhen, dest: iEndTrigger, SQLITE_JUMPIFNULL);
150234 }
150235 sqlite3ExprDelete(db, p: pWhen);
150236 }
150237
150238 /* Code the trigger program into the sub-vdbe. */
150239 codeTriggerProgram(pParse: &sSubParse, pStepList: pTrigger->step_list, orconf);
150240
150241 /* Insert an OP_Halt at the end of the sub-program. */
150242 if( iEndTrigger ){
150243 sqlite3VdbeResolveLabel(v, x: iEndTrigger);
150244 }
150245 sqlite3VdbeAddOp0(p: v, OP_Halt);
150246 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
150247 transferParseError(pTo: pParse, pFrom: &sSubParse);
150248
150249 if( pParse->nErr==0 ){
150250 assert( db->mallocFailed==0 );
150251 pProgram->aOp = sqlite3VdbeTakeOpArray(p: v, pnOp: &pProgram->nOp, pnMaxArg: &pTop->nMaxArg);
150252 }
150253 pProgram->nMem = sSubParse.nMem;
150254 pProgram->nCsr = sSubParse.nTab;
150255 pProgram->token = (void *)pTrigger;
150256 pPrg->aColmask[0] = sSubParse.oldmask;
150257 pPrg->aColmask[1] = sSubParse.newmask;
150258 sqlite3VdbeDelete(p: v);
150259 }else{
150260 transferParseError(pTo: pParse, pFrom: &sSubParse);
150261 }
150262
150263 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg );
150264 sqlite3ParseObjectReset(pParse: &sSubParse);
150265 return pPrg;
150266}
150267
150268/*
150269** Return a pointer to a TriggerPrg object containing the sub-program for
150270** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
150271** TriggerPrg object exists, a new object is allocated and populated before
150272** being returned.
150273*/
150274static TriggerPrg *getRowTrigger(
150275 Parse *pParse, /* Current parse context */
150276 Trigger *pTrigger, /* Trigger to code */
150277 Table *pTab, /* The table trigger pTrigger is attached to */
150278 int orconf /* ON CONFLICT algorithm. */
150279){
150280 Parse *pRoot = sqlite3ParseToplevel(pParse);
150281 TriggerPrg *pPrg;
150282
150283 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
150284
150285 /* It may be that this trigger has already been coded (or is in the
150286 ** process of being coded). If this is the case, then an entry with
150287 ** a matching TriggerPrg.pTrigger field will be present somewhere
150288 ** in the Parse.pTriggerPrg list. Search for such an entry. */
150289 for(pPrg=pRoot->pTriggerPrg;
150290 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
150291 pPrg=pPrg->pNext
150292 );
150293
150294 /* If an existing TriggerPrg could not be located, create a new one. */
150295 if( !pPrg ){
150296 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
150297 pParse->db->errByteOffset = -1;
150298 }
150299
150300 return pPrg;
150301}
150302
150303/*
150304** Generate code for the trigger program associated with trigger p on
150305** table pTab. The reg, orconf and ignoreJump parameters passed to this
150306** function are the same as those described in the header function for
150307** sqlite3CodeRowTrigger()
150308*/
150309SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
150310 Parse *pParse, /* Parse context */
150311 Trigger *p, /* Trigger to code */
150312 Table *pTab, /* The table to code triggers from */
150313 int reg, /* Reg array containing OLD.* and NEW.* values */
150314 int orconf, /* ON CONFLICT policy */
150315 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
150316){
150317 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
150318 TriggerPrg *pPrg;
150319 pPrg = getRowTrigger(pParse, pTrigger: p, pTab, orconf);
150320 assert( pPrg || pParse->nErr );
150321
150322 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
150323 ** is a pointer to the sub-vdbe containing the trigger program. */
150324 if( pPrg ){
150325 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
150326
150327 sqlite3VdbeAddOp4(p: v, OP_Program, p1: reg, p2: ignoreJump, p3: ++pParse->nMem,
150328 zP4: (const char *)pPrg->pProgram, P4_SUBPROGRAM);
150329 VdbeComment(
150330 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
150331
150332 /* Set the P5 operand of the OP_Program instruction to non-zero if
150333 ** recursive invocation of this trigger program is disallowed. Recursive
150334 ** invocation is disallowed if (a) the sub-program is really a trigger,
150335 ** not a foreign key action, and (b) the flag to enable recursive triggers
150336 ** is clear. */
150337 sqlite3VdbeChangeP5(p: v, p5: (u8)bRecursive);
150338 }
150339}
150340
150341/*
150342** This is called to code the required FOR EACH ROW triggers for an operation
150343** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
150344** is given by the op parameter. The tr_tm parameter determines whether the
150345** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
150346** parameter pChanges is passed the list of columns being modified.
150347**
150348** If there are no triggers that fire at the specified time for the specified
150349** operation on pTab, this function is a no-op.
150350**
150351** The reg argument is the address of the first in an array of registers
150352** that contain the values substituted for the new.* and old.* references
150353** in the trigger program. If N is the number of columns in table pTab
150354** (a copy of pTab->nCol), then registers are populated as follows:
150355**
150356** Register Contains
150357** ------------------------------------------------------
150358** reg+0 OLD.rowid
150359** reg+1 OLD.* value of left-most column of pTab
150360** ... ...
150361** reg+N OLD.* value of right-most column of pTab
150362** reg+N+1 NEW.rowid
150363** reg+N+2 NEW.* value of left-most column of pTab
150364** ... ...
150365** reg+N+N+1 NEW.* value of right-most column of pTab
150366**
150367** For ON DELETE triggers, the registers containing the NEW.* values will
150368** never be accessed by the trigger program, so they are not allocated or
150369** populated by the caller (there is no data to populate them with anyway).
150370** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
150371** are never accessed, and so are not allocated by the caller. So, for an
150372** ON INSERT trigger, the value passed to this function as parameter reg
150373** is not a readable register, although registers (reg+N) through
150374** (reg+N+N+1) are.
150375**
150376** Parameter orconf is the default conflict resolution algorithm for the
150377** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
150378** is the instruction that control should jump to if a trigger program
150379** raises an IGNORE exception.
150380*/
150381SQLITE_PRIVATE void sqlite3CodeRowTrigger(
150382 Parse *pParse, /* Parse context */
150383 Trigger *pTrigger, /* List of triggers on table pTab */
150384 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
150385 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
150386 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
150387 Table *pTab, /* The table to code triggers from */
150388 int reg, /* The first in an array of registers (see above) */
150389 int orconf, /* ON CONFLICT policy */
150390 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
150391){
150392 Trigger *p; /* Used to iterate through pTrigger list */
150393
150394 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
150395 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
150396 assert( (op==TK_UPDATE)==(pChanges!=0) );
150397
150398 for(p=pTrigger; p; p=p->pNext){
150399
150400 /* Sanity checking: The schema for the trigger and for the table are
150401 ** always defined. The trigger must be in the same schema as the table
150402 ** or else it must be a TEMP trigger. */
150403 assert( p->pSchema!=0 );
150404 assert( p->pTabSchema!=0 );
150405 assert( p->pSchema==p->pTabSchema
150406 || p->pSchema==pParse->db->aDb[1].pSchema );
150407
150408 /* Determine whether we should code this trigger. One of two choices:
150409 ** 1. The trigger is an exact match to the current DML statement
150410 ** 2. This is a RETURNING trigger for INSERT but we are currently
150411 ** doing the UPDATE part of an UPSERT.
150412 */
150413 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
150414 && p->tr_tm==tr_tm
150415 && checkColumnOverlap(pIdList: p->pColumns, pEList: pChanges)
150416 ){
150417 if( !p->bReturning ){
150418 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
150419 }else if( sqlite3IsToplevel(pParse) ){
150420 codeReturningTrigger(pParse, pTrigger: p, pTab, regIn: reg);
150421 }
150422 }
150423 }
150424}
150425
150426/*
150427** Triggers may access values stored in the old.* or new.* pseudo-table.
150428** This function returns a 32-bit bitmask indicating which columns of the
150429** old.* or new.* tables actually are used by triggers. This information
150430** may be used by the caller, for example, to avoid having to load the entire
150431** old.* record into memory when executing an UPDATE or DELETE command.
150432**
150433** Bit 0 of the returned mask is set if the left-most column of the
150434** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
150435** the second leftmost column value is required, and so on. If there
150436** are more than 32 columns in the table, and at least one of the columns
150437** with an index greater than 32 may be accessed, 0xffffffff is returned.
150438**
150439** It is not possible to determine if the old.rowid or new.rowid column is
150440** accessed by triggers. The caller must always assume that it is.
150441**
150442** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
150443** applies to the old.* table. If 1, the new.* table.
150444**
150445** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
150446** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
150447** included in the returned mask if the TRIGGER_BEFORE bit is set in the
150448** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
150449** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
150450*/
150451SQLITE_PRIVATE u32 sqlite3TriggerColmask(
150452 Parse *pParse, /* Parse context */
150453 Trigger *pTrigger, /* List of triggers on table pTab */
150454 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
150455 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
150456 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
150457 Table *pTab, /* The table to code triggers from */
150458 int orconf /* Default ON CONFLICT policy for trigger steps */
150459){
150460 const int op = pChanges ? TK_UPDATE : TK_DELETE;
150461 u32 mask = 0;
150462 Trigger *p;
150463
150464 assert( isNew==1 || isNew==0 );
150465 if( IsView(pTab) ){
150466 return 0xffffffff;
150467 }
150468 for(p=pTrigger; p; p=p->pNext){
150469 if( p->op==op
150470 && (tr_tm&p->tr_tm)
150471 && checkColumnOverlap(pIdList: p->pColumns,pEList: pChanges)
150472 ){
150473 if( p->bReturning ){
150474 mask = 0xffffffff;
150475 }else{
150476 TriggerPrg *pPrg;
150477 pPrg = getRowTrigger(pParse, pTrigger: p, pTab, orconf);
150478 if( pPrg ){
150479 mask |= pPrg->aColmask[isNew];
150480 }
150481 }
150482 }
150483 }
150484
150485 return mask;
150486}
150487
150488#endif /* !defined(SQLITE_OMIT_TRIGGER) */
150489
150490/************** End of trigger.c *********************************************/
150491/************** Begin file update.c ******************************************/
150492/*
150493** 2001 September 15
150494**
150495** The author disclaims copyright to this source code. In place of
150496** a legal notice, here is a blessing:
150497**
150498** May you do good and not evil.
150499** May you find forgiveness for yourself and forgive others.
150500** May you share freely, never taking more than you give.
150501**
150502*************************************************************************
150503** This file contains C code routines that are called by the parser
150504** to handle UPDATE statements.
150505*/
150506/* #include "sqliteInt.h" */
150507
150508#ifndef SQLITE_OMIT_VIRTUALTABLE
150509/* Forward declaration */
150510static void updateVirtualTable(
150511 Parse *pParse, /* The parsing context */
150512 SrcList *pSrc, /* The virtual table to be modified */
150513 Table *pTab, /* The virtual table */
150514 ExprList *pChanges, /* The columns to change in the UPDATE statement */
150515 Expr *pRowidExpr, /* Expression used to recompute the rowid */
150516 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
150517 Expr *pWhere, /* WHERE clause of the UPDATE statement */
150518 int onError /* ON CONFLICT strategy */
150519);
150520#endif /* SQLITE_OMIT_VIRTUALTABLE */
150521
150522/*
150523** The most recently coded instruction was an OP_Column to retrieve the
150524** i-th column of table pTab. This routine sets the P4 parameter of the
150525** OP_Column to the default value, if any.
150526**
150527** The default value of a column is specified by a DEFAULT clause in the
150528** column definition. This was either supplied by the user when the table
150529** was created, or added later to the table definition by an ALTER TABLE
150530** command. If the latter, then the row-records in the table btree on disk
150531** may not contain a value for the column and the default value, taken
150532** from the P4 parameter of the OP_Column instruction, is returned instead.
150533** If the former, then all row-records are guaranteed to include a value
150534** for the column and the P4 value is not required.
150535**
150536** Column definitions created by an ALTER TABLE command may only have
150537** literal default values specified: a number, null or a string. (If a more
150538** complicated default expression value was provided, it is evaluated
150539** when the ALTER TABLE is executed and one of the literal values written
150540** into the sqlite_schema table.)
150541**
150542** Therefore, the P4 parameter is only required if the default value for
150543** the column is a literal number, string or null. The sqlite3ValueFromExpr()
150544** function is capable of transforming these types of expressions into
150545** sqlite3_value objects.
150546**
150547** If column as REAL affinity and the table is an ordinary b-tree table
150548** (not a virtual table) then the value might have been stored as an
150549** integer. In that case, add an OP_RealAffinity opcode to make sure
150550** it has been converted into REAL.
150551*/
150552SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
150553 Column *pCol;
150554 assert( pTab!=0 );
150555 assert( pTab->nCol>i );
150556 pCol = &pTab->aCol[i];
150557 if( pCol->iDflt ){
150558 sqlite3_value *pValue = 0;
150559 u8 enc = ENC(sqlite3VdbeDb(v));
150560 assert( !IsView(pTab) );
150561 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
150562 assert( i<pTab->nCol );
150563 sqlite3ValueFromExpr(db: sqlite3VdbeDb(v),
150564 pExpr: sqlite3ColumnExpr(pTab,pCol), enc,
150565 affinity: pCol->affinity, ppVal: &pValue);
150566 if( pValue ){
150567 sqlite3VdbeAppendP4(p: v, pP4: pValue, P4_MEM);
150568 }
150569 }
150570#ifndef SQLITE_OMIT_FLOATING_POINT
150571 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
150572 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: iReg);
150573 }
150574#endif
150575}
150576
150577/*
150578** Check to see if column iCol of index pIdx references any of the
150579** columns defined by aXRef and chngRowid. Return true if it does
150580** and false if not. This is an optimization. False-positives are a
150581** performance degradation, but false-negatives can result in a corrupt
150582** index and incorrect answers.
150583**
150584** aXRef[j] will be non-negative if column j of the original table is
150585** being updated. chngRowid will be true if the rowid of the table is
150586** being updated.
150587*/
150588static int indexColumnIsBeingUpdated(
150589 Index *pIdx, /* The index to check */
150590 int iCol, /* Which column of the index to check */
150591 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
150592 int chngRowid /* true if the rowid is being updated */
150593){
150594 i16 iIdxCol = pIdx->aiColumn[iCol];
150595 assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
150596 if( iIdxCol>=0 ){
150597 return aXRef[iIdxCol]>=0;
150598 }
150599 assert( iIdxCol==XN_EXPR );
150600 assert( pIdx->aColExpr!=0 );
150601 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
150602 return sqlite3ExprReferencesUpdatedColumn(pExpr: pIdx->aColExpr->a[iCol].pExpr,
150603 aiChng: aXRef,chngRowid);
150604}
150605
150606/*
150607** Check to see if index pIdx is a partial index whose conditional
150608** expression might change values due to an UPDATE. Return true if
150609** the index is subject to change and false if the index is guaranteed
150610** to be unchanged. This is an optimization. False-positives are a
150611** performance degradation, but false-negatives can result in a corrupt
150612** index and incorrect answers.
150613**
150614** aXRef[j] will be non-negative if column j of the original table is
150615** being updated. chngRowid will be true if the rowid of the table is
150616** being updated.
150617*/
150618static int indexWhereClauseMightChange(
150619 Index *pIdx, /* The index to check */
150620 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
150621 int chngRowid /* true if the rowid is being updated */
150622){
150623 if( pIdx->pPartIdxWhere==0 ) return 0;
150624 return sqlite3ExprReferencesUpdatedColumn(pExpr: pIdx->pPartIdxWhere,
150625 aiChng: aXRef, chngRowid);
150626}
150627
150628/*
150629** Allocate and return a pointer to an expression of type TK_ROW with
150630** Expr.iColumn set to value (iCol+1). The resolver will modify the
150631** expression to be a TK_COLUMN reading column iCol of the first
150632** table in the source-list (pSrc->a[0]).
150633*/
150634static Expr *exprRowColumn(Parse *pParse, int iCol){
150635 Expr *pRet = sqlite3PExpr(pParse, TK_ROW, pLeft: 0, pRight: 0);
150636 if( pRet ) pRet->iColumn = iCol+1;
150637 return pRet;
150638}
150639
150640/*
150641** Assuming both the pLimit and pOrderBy parameters are NULL, this function
150642** generates VM code to run the query:
150643**
150644** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
150645**
150646** and write the results to the ephemeral table already opened as cursor
150647** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
150648** this function, they must be deleted by the caller.
150649**
150650** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
150651**
150652** SELECT <other-columns>, pChanges FROM pTabList
150653** WHERE pWhere
150654** GROUP BY <other-columns>
150655** ORDER BY pOrderBy LIMIT pLimit
150656**
150657** If pTab is a view, the GROUP BY clause is omitted.
150658**
150659** Exactly how results are written to table iEph, and exactly what
150660** the <other-columns> in the query above are is determined by the type
150661** of table pTabList->a[0].pTab.
150662**
150663** If the table is a WITHOUT ROWID table, then argument pPk must be its
150664** PRIMARY KEY. In this case <other-columns> are the primary key columns
150665** of the table, in order. The results of the query are written to ephemeral
150666** table iEph as index keys, using OP_IdxInsert.
150667**
150668** If the table is actually a view, then <other-columns> are all columns of
150669** the view. The results are written to the ephemeral table iEph as records
150670** with automatically assigned integer keys.
150671**
150672** If the table is a virtual or ordinary intkey table, then <other-columns>
150673** is its rowid. For a virtual table, the results are written to iEph as
150674** records with automatically assigned integer keys For intkey tables, the
150675** rowid value in <other-columns> is used as the integer key, and the
150676** remaining fields make up the table record.
150677*/
150678static void updateFromSelect(
150679 Parse *pParse, /* Parse context */
150680 int iEph, /* Cursor for open eph. table */
150681 Index *pPk, /* PK if table 0 is WITHOUT ROWID */
150682 ExprList *pChanges, /* List of expressions to return */
150683 SrcList *pTabList, /* List of tables to select from */
150684 Expr *pWhere, /* WHERE clause for query */
150685 ExprList *pOrderBy, /* ORDER BY clause */
150686 Expr *pLimit /* LIMIT clause */
150687){
150688 int i;
150689 SelectDest dest;
150690 Select *pSelect = 0;
150691 ExprList *pList = 0;
150692 ExprList *pGrp = 0;
150693 Expr *pLimit2 = 0;
150694 ExprList *pOrderBy2 = 0;
150695 sqlite3 *db = pParse->db;
150696 Table *pTab = pTabList->a[0].pTab;
150697 SrcList *pSrc;
150698 Expr *pWhere2;
150699 int eDest;
150700
150701#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
150702 if( pOrderBy && pLimit==0 ) {
150703 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");
150704 return;
150705 }
150706 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);
150707 pLimit2 = sqlite3ExprDup(db, pLimit, 0);
150708#else
150709 UNUSED_PARAMETER(pOrderBy);
150710 UNUSED_PARAMETER(pLimit);
150711#endif
150712
150713 pSrc = sqlite3SrcListDup(db, p: pTabList, flags: 0);
150714 pWhere2 = sqlite3ExprDup(db, p: pWhere, flags: 0);
150715
150716 assert( pTabList->nSrc>1 );
150717 if( pSrc ){
150718 assert( pSrc->a[0].fg.notCte );
150719 pSrc->a[0].iCursor = -1;
150720 pSrc->a[0].pTab->nTabRef--;
150721 pSrc->a[0].pTab = 0;
150722 }
150723 if( pPk ){
150724 for(i=0; i<pPk->nKeyCol; i++){
150725 Expr *pNew = exprRowColumn(pParse, iCol: pPk->aiColumn[i]);
150726#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
150727 if( pLimit ){
150728 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));
150729 }
150730#endif
150731 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pNew);
150732 }
150733 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;
150734 }else if( IsView(pTab) ){
150735 for(i=0; i<pTab->nCol; i++){
150736 pList = sqlite3ExprListAppend(pParse, pList, pExpr: exprRowColumn(pParse, iCol: i));
150737 }
150738 eDest = SRT_Table;
150739 }else{
150740 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;
150741 pList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: sqlite3PExpr(pParse,TK_ROW,pLeft: 0,pRight: 0));
150742#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
150743 if( pLimit ){
150744 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0));
150745 }
150746#endif
150747 }
150748 assert( pChanges!=0 || pParse->db->mallocFailed );
150749 if( pChanges ){
150750 for(i=0; i<pChanges->nExpr; i++){
150751 pList = sqlite3ExprListAppend(pParse, pList,
150752 pExpr: sqlite3ExprDup(db, p: pChanges->a[i].pExpr, flags: 0)
150753 );
150754 }
150755 }
150756 pSelect = sqlite3SelectNew(pParse, pEList: pList,
150757 pSrc, pWhere: pWhere2, pGroupBy: pGrp, pHaving: 0, pOrderBy: pOrderBy2,
150758 SF_UFSrcCheck|SF_IncludeHidden|SF_UpdateFrom, pLimit: pLimit2
150759 );
150760 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
150761 sqlite3SelectDestInit(pDest: &dest, eDest, iParm: iEph);
150762 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
150763 sqlite3Select(pParse, p: pSelect, pDest: &dest);
150764 sqlite3SelectDelete(db, p: pSelect);
150765}
150766
150767/*
150768** Process an UPDATE statement.
150769**
150770** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
150771** \_______/ \_/ \______/ \_____/ \________________/
150772** onError | pChanges | pWhere
150773** \_______________________/
150774** pTabList
150775*/
150776SQLITE_PRIVATE void sqlite3Update(
150777 Parse *pParse, /* The parser context */
150778 SrcList *pTabList, /* The table in which we should change things */
150779 ExprList *pChanges, /* Things to be changed */
150780 Expr *pWhere, /* The WHERE clause. May be null */
150781 int onError, /* How to handle constraint errors */
150782 ExprList *pOrderBy, /* ORDER BY clause. May be null */
150783 Expr *pLimit, /* LIMIT clause. May be null */
150784 Upsert *pUpsert /* ON CONFLICT clause, or null */
150785){
150786 int i, j, k; /* Loop counters */
150787 Table *pTab; /* The table to be updated */
150788 int addrTop = 0; /* VDBE instruction address of the start of the loop */
150789 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */
150790 Vdbe *v; /* The virtual database engine */
150791 Index *pIdx; /* For looping over indices */
150792 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
150793 int nIdx; /* Number of indices that need updating */
150794 int nAllIdx; /* Total number of indexes */
150795 int iBaseCur; /* Base cursor number */
150796 int iDataCur; /* Cursor for the canonical data btree */
150797 int iIdxCur; /* Cursor for the first index */
150798 sqlite3 *db; /* The database structure */
150799 int *aRegIdx = 0; /* Registers for to each index and the main table */
150800 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
150801 ** an expression for the i-th column of the table.
150802 ** aXRef[i]==-1 if the i-th column is not changed. */
150803 u8 *aToOpen; /* 1 for tables and indices to be opened */
150804 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
150805 u8 chngRowid; /* Rowid changed in a normal table */
150806 u8 chngKey; /* Either chngPk or chngRowid */
150807 Expr *pRowidExpr = 0; /* Expression defining the new record number */
150808 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
150809 AuthContext sContext; /* The authorization context */
150810 NameContext sNC; /* The name-context to resolve expressions in */
150811 int iDb; /* Database containing the table being updated */
150812 int eOnePass; /* ONEPASS_XXX value from where.c */
150813 int hasFK; /* True if foreign key processing is required */
150814 int labelBreak; /* Jump here to break out of UPDATE loop */
150815 int labelContinue; /* Jump here to continue next step of UPDATE loop */
150816 int flags; /* Flags for sqlite3WhereBegin() */
150817
150818#ifndef SQLITE_OMIT_TRIGGER
150819 int isView; /* True when updating a view (INSTEAD OF trigger) */
150820 Trigger *pTrigger; /* List of triggers on pTab, if required */
150821 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
150822#endif
150823 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
150824 int iEph = 0; /* Ephemeral table holding all primary key values */
150825 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
150826 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
150827 int addrOpen = 0; /* Address of OP_OpenEphemeral */
150828 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
150829 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
150830 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
150831 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */
150832 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
150833
150834 /* Register Allocations */
150835 int regRowCount = 0; /* A count of rows changed */
150836 int regOldRowid = 0; /* The old rowid */
150837 int regNewRowid = 0; /* The new rowid */
150838 int regNew = 0; /* Content of the NEW.* table in triggers */
150839 int regOld = 0; /* Content of OLD.* table in triggers */
150840 int regRowSet = 0; /* Rowset of rows to be updated */
150841 int regKey = 0; /* composite PRIMARY KEY value */
150842
150843 memset(s: &sContext, c: 0, n: sizeof(sContext));
150844 db = pParse->db;
150845 assert( db->pParse==pParse );
150846 if( pParse->nErr ){
150847 goto update_cleanup;
150848 }
150849 assert( db->mallocFailed==0 );
150850
150851 /* Locate the table which we want to update.
150852 */
150853 pTab = sqlite3SrcListLookup(pParse, pSrc: pTabList);
150854 if( pTab==0 ) goto update_cleanup;
150855 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTab->pSchema);
150856
150857 /* Figure out if we have any triggers and if the table being
150858 ** updated is a view.
150859 */
150860#ifndef SQLITE_OMIT_TRIGGER
150861 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, pMask: &tmask);
150862 isView = IsView(pTab);
150863 assert( pTrigger || tmask==0 );
150864#else
150865# define pTrigger 0
150866# define isView 0
150867# define tmask 0
150868#endif
150869#ifdef SQLITE_OMIT_VIEW
150870# undef isView
150871# define isView 0
150872#endif
150873
150874#if TREETRACE_ENABLED
150875 if( sqlite3TreeTrace & 0x10000 ){
150876 sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__, __LINE__);
150877 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
150878 onError, pOrderBy, pLimit, pUpsert, pTrigger);
150879 }
150880#endif
150881
150882 /* If there was a FROM clause, set nChangeFrom to the number of expressions
150883 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
150884 ** clause if this function is being called to generate code for part of
150885 ** an UPSERT statement. */
150886 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
150887 assert( nChangeFrom==0 || pUpsert==0 );
150888
150889#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
150890 if( !isView && nChangeFrom==0 ){
150891 pWhere = sqlite3LimitWhere(
150892 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
150893 );
150894 pOrderBy = 0;
150895 pLimit = 0;
150896 }
150897#endif
150898
150899 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
150900 goto update_cleanup;
150901 }
150902 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
150903 goto update_cleanup;
150904 }
150905
150906 /* Allocate a cursors for the main database table and for all indices.
150907 ** The index cursors might not be used, but if they are used they
150908 ** need to occur right after the database cursor. So go ahead and
150909 ** allocate enough space, just in case.
150910 */
150911 iBaseCur = iDataCur = pParse->nTab++;
150912 iIdxCur = iDataCur+1;
150913 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
150914 testcase( pPk!=0 && pPk!=pTab->pIndex );
150915 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
150916 if( pPk==pIdx ){
150917 iDataCur = pParse->nTab;
150918 }
150919 pParse->nTab++;
150920 }
150921 if( pUpsert ){
150922 /* On an UPSERT, reuse the same cursors already opened by INSERT */
150923 iDataCur = pUpsert->iDataCur;
150924 iIdxCur = pUpsert->iIdxCur;
150925 pParse->nTab = iBaseCur;
150926 }
150927 pTabList->a[0].iCursor = iDataCur;
150928
150929 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
150930 ** Initialize aXRef[] and aToOpen[] to their default values.
150931 */
150932 aXRef = sqlite3DbMallocRawNN(db, n: sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
150933 if( aXRef==0 ) goto update_cleanup;
150934 aRegIdx = aXRef+pTab->nCol;
150935 aToOpen = (u8*)(aRegIdx+nIdx+1);
150936 memset(s: aToOpen, c: 1, n: nIdx+1);
150937 aToOpen[nIdx+1] = 0;
150938 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
150939
150940 /* Initialize the name-context */
150941 memset(s: &sNC, c: 0, n: sizeof(sNC));
150942 sNC.pParse = pParse;
150943 sNC.pSrcList = pTabList;
150944 sNC.uNC.pUpsert = pUpsert;
150945 sNC.ncFlags = NC_UUpsert;
150946
150947 /* Begin generating code. */
150948 v = sqlite3GetVdbe(pParse);
150949 if( v==0 ) goto update_cleanup;
150950
150951 /* Resolve the column names in all the expressions of the
150952 ** of the UPDATE statement. Also find the column index
150953 ** for each column to be updated in the pChanges array. For each
150954 ** column to be updated, make sure we have authorization to change
150955 ** that column.
150956 */
150957 chngRowid = chngPk = 0;
150958 for(i=0; i<pChanges->nExpr; i++){
150959 u8 hCol = sqlite3StrIHash(z: pChanges->a[i].zEName);
150960 /* If this is an UPDATE with a FROM clause, do not resolve expressions
150961 ** here. The call to sqlite3Select() below will do that. */
150962 if( nChangeFrom==0 && sqlite3ResolveExprNames(pNC: &sNC, pExpr: pChanges->a[i].pExpr) ){
150963 goto update_cleanup;
150964 }
150965 for(j=0; j<pTab->nCol; j++){
150966 if( pTab->aCol[j].hName==hCol
150967 && sqlite3StrICmp(zLeft: pTab->aCol[j].zCnName, zRight: pChanges->a[i].zEName)==0
150968 ){
150969 if( j==pTab->iPKey ){
150970 chngRowid = 1;
150971 pRowidExpr = pChanges->a[i].pExpr;
150972 iRowidExpr = i;
150973 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
150974 chngPk = 1;
150975 }
150976#ifndef SQLITE_OMIT_GENERATED_COLUMNS
150977 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
150978 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
150979 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
150980 sqlite3ErrorMsg(pParse,
150981 zFormat: "cannot UPDATE generated column \"%s\"",
150982 pTab->aCol[j].zCnName);
150983 goto update_cleanup;
150984 }
150985#endif
150986 aXRef[j] = i;
150987 break;
150988 }
150989 }
150990 if( j>=pTab->nCol ){
150991 if( pPk==0 && sqlite3IsRowid(z: pChanges->a[i].zEName) ){
150992 j = -1;
150993 chngRowid = 1;
150994 pRowidExpr = pChanges->a[i].pExpr;
150995 iRowidExpr = i;
150996 }else{
150997 sqlite3ErrorMsg(pParse, zFormat: "no such column: %s", pChanges->a[i].zEName);
150998 pParse->checkSchema = 1;
150999 goto update_cleanup;
151000 }
151001 }
151002#ifndef SQLITE_OMIT_AUTHORIZATION
151003 {
151004 int rc;
151005 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, zArg1: pTab->zName,
151006 zArg2: j<0 ? "ROWID" : pTab->aCol[j].zCnName,
151007 zArg3: db->aDb[iDb].zDbSName);
151008 if( rc==SQLITE_DENY ){
151009 goto update_cleanup;
151010 }else if( rc==SQLITE_IGNORE ){
151011 aXRef[j] = -1;
151012 }
151013 }
151014#endif
151015 }
151016 assert( (chngRowid & chngPk)==0 );
151017 assert( chngRowid==0 || chngRowid==1 );
151018 assert( chngPk==0 || chngPk==1 );
151019 chngKey = chngRowid + chngPk;
151020
151021#ifndef SQLITE_OMIT_GENERATED_COLUMNS
151022 /* Mark generated columns as changing if their generator expressions
151023 ** reference any changing column. The actual aXRef[] value for
151024 ** generated expressions is not used, other than to check to see that it
151025 ** is non-negative, so the value of aXRef[] for generated columns can be
151026 ** set to any non-negative number. We use 99999 so that the value is
151027 ** obvious when looking at aXRef[] in a symbolic debugger.
151028 */
151029 if( pTab->tabFlags & TF_HasGenerated ){
151030 int bProgress;
151031 testcase( pTab->tabFlags & TF_HasVirtual );
151032 testcase( pTab->tabFlags & TF_HasStored );
151033 do{
151034 bProgress = 0;
151035 for(i=0; i<pTab->nCol; i++){
151036 if( aXRef[i]>=0 ) continue;
151037 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
151038 if( sqlite3ExprReferencesUpdatedColumn(
151039 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
151040 aiChng: aXRef, chngRowid)
151041 ){
151042 aXRef[i] = 99999;
151043 bProgress = 1;
151044 }
151045 }
151046 }while( bProgress );
151047 }
151048#endif
151049
151050 /* The SET expressions are not actually used inside the WHERE loop.
151051 ** So reset the colUsed mask. Unless this is a virtual table. In that
151052 ** case, set all bits of the colUsed mask (to ensure that the virtual
151053 ** table implementation makes all columns available).
151054 */
151055 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
151056
151057 hasFK = sqlite3FkRequired(pParse, pTab, aChange: aXRef, chngRowid: chngKey);
151058
151059 /* There is one entry in the aRegIdx[] array for each index on the table
151060 ** being updated. Fill in aRegIdx[] with a register number that will hold
151061 ** the key for accessing each index.
151062 */
151063 if( onError==OE_Replace ) bReplace = 1;
151064 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
151065 int reg;
151066 if( chngKey || hasFK>1 || pIdx==pPk
151067 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
151068 ){
151069 reg = ++pParse->nMem;
151070 pParse->nMem += pIdx->nColumn;
151071 }else{
151072 reg = 0;
151073 for(i=0; i<pIdx->nKeyCol; i++){
151074 if( indexColumnIsBeingUpdated(pIdx, iCol: i, aXRef, chngRowid) ){
151075 reg = ++pParse->nMem;
151076 pParse->nMem += pIdx->nColumn;
151077 if( onError==OE_Default && pIdx->onError==OE_Replace ){
151078 bReplace = 1;
151079 }
151080 break;
151081 }
151082 }
151083 }
151084 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
151085 aRegIdx[nAllIdx] = reg;
151086 }
151087 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
151088 if( bReplace ){
151089 /* If REPLACE conflict resolution might be invoked, open cursors on all
151090 ** indexes in case they are needed to delete records. */
151091 memset(s: aToOpen, c: 1, n: nIdx+1);
151092 }
151093
151094 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
151095 sqlite3BeginWriteOperation(pParse, setStatement: pTrigger || hasFK, iDb);
151096
151097 /* Allocate required registers. */
151098 if( !IsVirtual(pTab) ){
151099 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
151100 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
151101 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
151102 ** table record is written. regRowSet holds the RowSet for the
151103 ** two-pass update algorithm. */
151104 assert( aRegIdx[nAllIdx]==pParse->nMem );
151105 regRowSet = aRegIdx[nAllIdx];
151106 regOldRowid = regNewRowid = ++pParse->nMem;
151107 if( chngPk || pTrigger || hasFK ){
151108 regOld = pParse->nMem + 1;
151109 pParse->nMem += pTab->nCol;
151110 }
151111 if( chngKey || pTrigger || hasFK ){
151112 regNewRowid = ++pParse->nMem;
151113 }
151114 regNew = pParse->nMem + 1;
151115 pParse->nMem += pTab->nCol;
151116 }
151117
151118 /* Start the view context. */
151119 if( isView ){
151120 sqlite3AuthContextPush(pParse, pContext: &sContext, zContext: pTab->zName);
151121 }
151122
151123 /* If we are trying to update a view, realize that view into
151124 ** an ephemeral table.
151125 */
151126#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
151127 if( nChangeFrom==0 && isView ){
151128 sqlite3MaterializeView(pParse, pView: pTab,
151129 pWhere, pOrderBy, pLimit, iCur: iDataCur
151130 );
151131 pOrderBy = 0;
151132 pLimit = 0;
151133 }
151134#endif
151135
151136 /* Resolve the column names in all the expressions in the
151137 ** WHERE clause.
151138 */
151139 if( nChangeFrom==0 && sqlite3ResolveExprNames(pNC: &sNC, pExpr: pWhere) ){
151140 goto update_cleanup;
151141 }
151142
151143#ifndef SQLITE_OMIT_VIRTUALTABLE
151144 /* Virtual tables must be handled separately */
151145 if( IsVirtual(pTab) ){
151146 updateVirtualTable(pParse, pSrc: pTabList, pTab, pChanges, pRowidExpr, aXRef,
151147 pWhere, onError);
151148 goto update_cleanup;
151149 }
151150#endif
151151
151152 /* Jump to labelBreak to abandon further processing of this UPDATE */
151153 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
151154
151155 /* Not an UPSERT. Normal processing. Begin by
151156 ** initialize the count of updated rows */
151157 if( (db->flags&SQLITE_CountRows)!=0
151158 && !pParse->pTriggerTab
151159 && !pParse->nested
151160 && !pParse->bReturning
151161 && pUpsert==0
151162 ){
151163 regRowCount = ++pParse->nMem;
151164 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regRowCount);
151165 }
151166
151167 if( nChangeFrom==0 && HasRowid(pTab) ){
151168 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: regRowSet, p3: regOldRowid);
151169 iEph = pParse->nTab++;
151170 addrOpen = sqlite3VdbeAddOp3(p: v, OP_OpenEphemeral, p1: iEph, p2: 0, p3: regRowSet);
151171 }else{
151172 assert( pPk!=0 || HasRowid(pTab) );
151173 nPk = pPk ? pPk->nKeyCol : 0;
151174 iPk = pParse->nMem+1;
151175 pParse->nMem += nPk;
151176 pParse->nMem += nChangeFrom;
151177 regKey = ++pParse->nMem;
151178 if( pUpsert==0 ){
151179 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
151180 iEph = pParse->nTab++;
151181 if( pPk ) sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: iPk, p3: iPk+nPk-1);
151182 addrOpen = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iEph, p2: nEphCol);
151183 if( pPk ){
151184 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx: pPk);
151185 if( pKeyInfo ){
151186 pKeyInfo->nAllField = nEphCol;
151187 sqlite3VdbeAppendP4(p: v, pP4: pKeyInfo, P4_KEYINFO);
151188 }
151189 }
151190 if( nChangeFrom ){
151191 updateFromSelect(
151192 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit
151193 );
151194#ifndef SQLITE_OMIT_SUBQUERY
151195 if( isView ) iDataCur = iEph;
151196#endif
151197 }
151198 }
151199 }
151200
151201 if( nChangeFrom ){
151202 sqlite3MultiWrite(pParse);
151203 eOnePass = ONEPASS_OFF;
151204 nKey = nPk;
151205 regKey = iPk;
151206 }else{
151207 if( pUpsert ){
151208 /* If this is an UPSERT, then all cursors have already been opened by
151209 ** the outer INSERT and the data cursor should be pointing at the row
151210 ** that is to be updated. So bypass the code that searches for the
151211 ** row(s) to be updated.
151212 */
151213 pWInfo = 0;
151214 eOnePass = ONEPASS_SINGLE;
151215 sqlite3ExprIfFalse(pParse, pExpr: pWhere, dest: labelBreak, SQLITE_JUMPIFNULL);
151216 bFinishSeek = 0;
151217 }else{
151218 /* Begin the database scan.
151219 **
151220 ** Do not consider a single-pass strategy for a multi-row update if
151221 ** there is anything that might disrupt the cursor being used to do
151222 ** the UPDATE:
151223 ** (1) This is a nested UPDATE
151224 ** (2) There are triggers
151225 ** (3) There are FOREIGN KEY constraints
151226 ** (4) There are REPLACE conflict handlers
151227 ** (5) There are subqueries in the WHERE clause
151228 */
151229 flags = WHERE_ONEPASS_DESIRED;
151230 if( !pParse->nested
151231 && !pTrigger
151232 && !hasFK
151233 && !chngKey
151234 && !bReplace
151235 && (pWhere==0 || !ExprHasProperty(pWhere, EP_Subquery))
151236 ){
151237 flags |= WHERE_ONEPASS_MULTIROW;
151238 }
151239 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);
151240 if( pWInfo==0 ) goto update_cleanup;
151241
151242 /* A one-pass strategy that might update more than one row may not
151243 ** be used if any column of the index used for the scan is being
151244 ** updated. Otherwise, if there is an index on "b", statements like
151245 ** the following could create an infinite loop:
151246 **
151247 ** UPDATE t1 SET b=b+1 WHERE b>?
151248 **
151249 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
151250 ** strategy that uses an index for which one or more columns are being
151251 ** updated. */
151252 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
151253 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
151254 if( eOnePass!=ONEPASS_SINGLE ){
151255 sqlite3MultiWrite(pParse);
151256 if( eOnePass==ONEPASS_MULTI ){
151257 int iCur = aiCurOnePass[1];
151258 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
151259 eOnePass = ONEPASS_OFF;
151260 }
151261 assert( iCur!=iDataCur || !HasRowid(pTab) );
151262 }
151263 }
151264 }
151265
151266 if( HasRowid(pTab) ){
151267 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
151268 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
151269 ** leave it in register regOldRowid. */
151270 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iDataCur, p2: regOldRowid);
151271 if( eOnePass==ONEPASS_OFF ){
151272 aRegIdx[nAllIdx] = ++pParse->nMem;
151273 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iEph, p2: regRowSet, p3: regOldRowid);
151274 }else{
151275 if( ALWAYS(addrOpen) ) sqlite3VdbeChangeToNoop(p: v, addr: addrOpen);
151276 }
151277 }else{
151278 /* Read the PK of the current row into an array of registers. In
151279 ** ONEPASS_OFF mode, serialize the array into a record and store it in
151280 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
151281 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
151282 ** is not required) and leave the PK fields in the array of registers. */
151283 for(i=0; i<nPk; i++){
151284 assert( pPk->aiColumn[i]>=0 );
151285 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur,
151286 iCol: pPk->aiColumn[i], regOut: iPk+i);
151287 }
151288 if( eOnePass ){
151289 if( addrOpen ) sqlite3VdbeChangeToNoop(p: v, addr: addrOpen);
151290 nKey = nPk;
151291 regKey = iPk;
151292 }else{
151293 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: iPk, p2: nPk, p3: regKey,
151294 zP4: sqlite3IndexAffinityStr(db, pIdx: pPk), p4type: nPk);
151295 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iEph, p2: regKey, p3: iPk, p4: nPk);
151296 }
151297 }
151298 }
151299
151300 if( pUpsert==0 ){
151301 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI ){
151302 sqlite3WhereEnd(pWInfo);
151303 }
151304
151305 if( !isView ){
151306 int addrOnce = 0;
151307 int iNotUsed1 = 0;
151308 int iNotUsed2 = 0;
151309
151310 /* Open every index that needs updating. */
151311 if( eOnePass!=ONEPASS_OFF ){
151312 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
151313 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
151314 }
151315
151316 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
151317 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
151318 }
151319 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, p5: 0, iBase: iBaseCur,
151320 aToOpen, piDataCur: &iNotUsed1, piIdxCur: &iNotUsed2);
151321 if( addrOnce ){
151322 sqlite3VdbeJumpHereOrPopInst(p: v, addr: addrOnce);
151323 }
151324 }
151325
151326 /* Top of the update loop */
151327 if( eOnePass!=ONEPASS_OFF ){
151328 if( aiCurOnePass[0]!=iDataCur
151329 && aiCurOnePass[1]!=iDataCur
151330#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
151331 && !isView
151332#endif
151333 ){
151334 assert( pPk );
151335 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iDataCur, p2: labelBreak, p3: regKey,p4: nKey);
151336 VdbeCoverage(v);
151337 }
151338 if( eOnePass!=ONEPASS_SINGLE ){
151339 labelContinue = sqlite3VdbeMakeLabel(pParse);
151340 }
151341 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: pPk ? regKey : regOldRowid, p2: labelBreak);
151342 VdbeCoverageIf(v, pPk==0);
151343 VdbeCoverageIf(v, pPk!=0);
151344 }else if( pPk || nChangeFrom ){
151345 labelContinue = sqlite3VdbeMakeLabel(pParse);
151346 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iEph, p2: labelBreak); VdbeCoverage(v);
151347 addrTop = sqlite3VdbeCurrentAddr(p: v);
151348 if( nChangeFrom ){
151349 if( !isView ){
151350 if( pPk ){
151351 for(i=0; i<nPk; i++){
151352 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: i, p3: iPk+i);
151353 }
151354 sqlite3VdbeAddOp4Int(
151355 p: v, OP_NotFound, p1: iDataCur, p2: labelContinue, p3: iPk, p4: nPk
151356 ); VdbeCoverage(v);
151357 }else{
151358 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iEph, p2: regOldRowid);
151359 sqlite3VdbeAddOp3(
151360 p: v, OP_NotExists, p1: iDataCur, p2: labelContinue, p3: regOldRowid
151361 ); VdbeCoverage(v);
151362 }
151363 }
151364 }else{
151365 sqlite3VdbeAddOp2(p: v, OP_RowData, p1: iEph, p2: regKey);
151366 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iDataCur, p2: labelContinue, p3: regKey,p4: 0);
151367 VdbeCoverage(v);
151368 }
151369 }else{
151370 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iEph, p2: labelBreak); VdbeCoverage(v);
151371 labelContinue = sqlite3VdbeMakeLabel(pParse);
151372 addrTop = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iEph, p2: regOldRowid);
151373 VdbeCoverage(v);
151374 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: labelContinue, p3: regOldRowid);
151375 VdbeCoverage(v);
151376 }
151377 }
151378
151379 /* If the rowid value will change, set register regNewRowid to
151380 ** contain the new value. If the rowid is not being modified,
151381 ** then regNewRowid is the same register as regOldRowid, which is
151382 ** already populated. */
151383 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
151384 if( chngRowid ){
151385 assert( iRowidExpr>=0 );
151386 if( nChangeFrom==0 ){
151387 sqlite3ExprCode(pParse, pExpr: pRowidExpr, target: regNewRowid);
151388 }else{
151389 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: iRowidExpr, p3: regNewRowid);
151390 }
151391 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: regNewRowid); VdbeCoverage(v);
151392 }
151393
151394 /* Compute the old pre-UPDATE content of the row being changed, if that
151395 ** information is needed */
151396 if( chngPk || hasFK || pTrigger ){
151397 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
151398 oldmask |= sqlite3TriggerColmask(pParse,
151399 pTrigger, pChanges, isNew: 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, orconf: onError
151400 );
151401 for(i=0; i<pTab->nCol; i++){
151402 u32 colFlags = pTab->aCol[i].colFlags;
151403 k = sqlite3TableColumnToStorage(pTab, iCol: i) + regOld;
151404 if( oldmask==0xffffffff
151405 || (i<32 && (oldmask & MASKBIT32(i))!=0)
151406 || (colFlags & COLFLAG_PRIMKEY)!=0
151407 ){
151408 testcase( oldmask!=0xffffffff && i==31 );
151409 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: i, regOut: k);
151410 }else{
151411 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: k);
151412 }
151413 }
151414 if( chngRowid==0 && pPk==0 ){
151415 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regOldRowid, p2: regNewRowid);
151416 }
151417 }
151418
151419 /* Populate the array of registers beginning at regNew with the new
151420 ** row data. This array is used to check constants, create the new
151421 ** table and index records, and as the values for any new.* references
151422 ** made by triggers.
151423 **
151424 ** If there are one or more BEFORE triggers, then do not populate the
151425 ** registers associated with columns that are (a) not modified by
151426 ** this UPDATE statement and (b) not accessed by new.* references. The
151427 ** values for registers not modified by the UPDATE must be reloaded from
151428 ** the database after the BEFORE triggers are fired anyway (as the trigger
151429 ** may have modified them). So not loading those that are not going to
151430 ** be used eliminates some redundant opcodes.
151431 */
151432 newmask = sqlite3TriggerColmask(
151433 pParse, pTrigger, pChanges, isNew: 1, TRIGGER_BEFORE, pTab, orconf: onError
151434 );
151435 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
151436 if( i==pTab->iPKey ){
151437 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: k);
151438 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
151439 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
151440 }else{
151441 j = aXRef[i];
151442 if( j>=0 ){
151443 if( nChangeFrom ){
151444 int nOff = (isView ? pTab->nCol : nPk);
151445 assert( eOnePass==ONEPASS_OFF );
151446 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: nOff+j, p3: k);
151447 }else{
151448 sqlite3ExprCode(pParse, pExpr: pChanges->a[j].pExpr, target: k);
151449 }
151450 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
151451 /* This branch loads the value of a column that will not be changed
151452 ** into a register. This is done if there are no BEFORE triggers, or
151453 ** if there are one or more BEFORE triggers that use this value via
151454 ** a new.* reference in a trigger program.
151455 */
151456 testcase( i==31 );
151457 testcase( i==32 );
151458 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: i, regOut: k);
151459 bFinishSeek = 0;
151460 }else{
151461 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: k);
151462 }
151463 }
151464 }
151465#ifndef SQLITE_OMIT_GENERATED_COLUMNS
151466 if( pTab->tabFlags & TF_HasGenerated ){
151467 testcase( pTab->tabFlags & TF_HasVirtual );
151468 testcase( pTab->tabFlags & TF_HasStored );
151469 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regNew, pTab);
151470 }
151471#endif
151472
151473 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
151474 ** verified. One could argue that this is wrong.
151475 */
151476 if( tmask&TRIGGER_BEFORE ){
151477 sqlite3TableAffinity(v, pTab, iReg: regNew);
151478 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
151479 TRIGGER_BEFORE, pTab, reg: regOldRowid, orconf: onError, ignoreJump: labelContinue);
151480
151481 if( !isView ){
151482 /* The row-trigger may have deleted the row being updated. In this
151483 ** case, jump to the next row. No updates or AFTER triggers are
151484 ** required. This behavior - what happens when the row being updated
151485 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
151486 ** documentation.
151487 */
151488 if( pPk ){
151489 sqlite3VdbeAddOp4Int(p: v, OP_NotFound,p1: iDataCur,p2: labelContinue,p3: regKey,p4: nKey);
151490 VdbeCoverage(v);
151491 }else{
151492 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: labelContinue,p3: regOldRowid);
151493 VdbeCoverage(v);
151494 }
151495
151496 /* After-BEFORE-trigger-reload-loop:
151497 ** If it did not delete it, the BEFORE trigger may still have modified
151498 ** some of the columns of the row being updated. Load the values for
151499 ** all columns not modified by the update statement into their registers
151500 ** in case this has happened. Only unmodified columns are reloaded.
151501 ** The values computed for modified columns use the values before the
151502 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
151503 ** for an example.
151504 */
151505 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
151506 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
151507 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
151508 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
151509 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: i, regOut: k);
151510 }
151511 }
151512#ifndef SQLITE_OMIT_GENERATED_COLUMNS
151513 if( pTab->tabFlags & TF_HasGenerated ){
151514 testcase( pTab->tabFlags & TF_HasVirtual );
151515 testcase( pTab->tabFlags & TF_HasStored );
151516 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regNew, pTab);
151517 }
151518#endif
151519 }
151520 }
151521
151522 if( !isView ){
151523 /* Do constraint checks. */
151524 assert( regOldRowid>0 );
151525 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
151526 regNewData: regNewRowid, regOldData: regOldRowid, pkChng: chngKey, overrideError: onError, ignoreDest: labelContinue, pbMayReplace: &bReplace,
151527 aiChng: aXRef, pUpsert: 0);
151528
151529 /* If REPLACE conflict handling may have been used, or if the PK of the
151530 ** row is changing, then the GenerateConstraintChecks() above may have
151531 ** moved cursor iDataCur. Reseek it. */
151532 if( bReplace || chngKey ){
151533 if( pPk ){
151534 sqlite3VdbeAddOp4Int(p: v, OP_NotFound,p1: iDataCur,p2: labelContinue,p3: regKey,p4: nKey);
151535 }else{
151536 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: labelContinue,p3: regOldRowid);
151537 }
151538 VdbeCoverage(v);
151539 }
151540
151541 /* Do FK constraint checks. */
151542 if( hasFK ){
151543 sqlite3FkCheck(pParse, pTab, regOld: regOldRowid, regNew: 0, aChange: aXRef, bChngRowid: chngKey);
151544 }
151545
151546 /* Delete the index entries associated with the current record. */
151547 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, iIdxNoSeek: -1);
151548
151549 /* We must run the OP_FinishSeek opcode to resolve a prior
151550 ** OP_DeferredSeek if there is any possibility that there have been
151551 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
151552 ** we want to avoid the OP_FinishSeek if possible, as running it
151553 ** costs CPU cycles. */
151554 if( bFinishSeek ){
151555 sqlite3VdbeAddOp1(p: v, OP_FinishSeek, p1: iDataCur);
151556 }
151557
151558 /* If changing the rowid value, or if there are foreign key constraints
151559 ** to process, delete the old record. Otherwise, add a noop OP_Delete
151560 ** to invoke the pre-update hook.
151561 **
151562 ** That (regNew==regnewRowid+1) is true is also important for the
151563 ** pre-update hook. If the caller invokes preupdate_new(), the returned
151564 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
151565 ** is the column index supplied by the user.
151566 */
151567 assert( regNew==regNewRowid+1 );
151568#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
151569 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
151570 OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
151571 regNewRowid
151572 );
151573 if( eOnePass==ONEPASS_MULTI ){
151574 assert( hasFK==0 && chngKey==0 );
151575 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
151576 }
151577 if( !pParse->nested ){
151578 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
151579 }
151580#else
151581 if( hasFK>1 || chngKey ){
151582 sqlite3VdbeAddOp2(p: v, OP_Delete, p1: iDataCur, p2: 0);
151583 }
151584#endif
151585
151586 if( hasFK ){
151587 sqlite3FkCheck(pParse, pTab, regOld: 0, regNew: regNewRowid, aChange: aXRef, bChngRowid: chngKey);
151588 }
151589
151590 /* Insert the new index entries and the new record. */
151591 sqlite3CompleteInsertion(
151592 pParse, pTab, iDataCur, iIdxCur, regNewData: regNewRowid, aRegIdx,
151593 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
151594 appendBias: 0, useSeekResult: 0
151595 );
151596
151597 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
151598 ** handle rows (possibly in other tables) that refer via a foreign key
151599 ** to the row just updated. */
151600 if( hasFK ){
151601 sqlite3FkActions(pParse, pTab, pChanges, regOld: regOldRowid, aChange: aXRef, bChngRowid: chngKey);
151602 }
151603 }
151604
151605 /* Increment the row counter
151606 */
151607 if( regRowCount ){
151608 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regRowCount, p2: 1);
151609 }
151610
151611 if( pTrigger ){
151612 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
151613 TRIGGER_AFTER, pTab, reg: regOldRowid, orconf: onError, ignoreJump: labelContinue);
151614 }
151615
151616 /* Repeat the above with the next record to be updated, until
151617 ** all record selected by the WHERE clause have been updated.
151618 */
151619 if( eOnePass==ONEPASS_SINGLE ){
151620 /* Nothing to do at end-of-loop for a single-pass */
151621 }else if( eOnePass==ONEPASS_MULTI ){
151622 sqlite3VdbeResolveLabel(v, x: labelContinue);
151623 sqlite3WhereEnd(pWInfo);
151624 }else{
151625 sqlite3VdbeResolveLabel(v, x: labelContinue);
151626 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iEph, p2: addrTop); VdbeCoverage(v);
151627 }
151628 sqlite3VdbeResolveLabel(v, x: labelBreak);
151629
151630 /* Update the sqlite_sequence table by storing the content of the
151631 ** maximum rowid counter values recorded while inserting into
151632 ** autoincrement tables.
151633 */
151634 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
151635 sqlite3AutoincrementEnd(pParse);
151636 }
151637
151638 /*
151639 ** Return the number of rows that were changed, if we are tracking
151640 ** that information.
151641 */
151642 if( regRowCount ){
151643 sqlite3CodeChangeCount(v, regCounter: regRowCount, zColName: "rows updated");
151644 }
151645
151646update_cleanup:
151647 sqlite3AuthContextPop(pContext: &sContext);
151648 sqlite3DbFree(db, p: aXRef); /* Also frees aRegIdx[] and aToOpen[] */
151649 sqlite3SrcListDelete(db, pList: pTabList);
151650 sqlite3ExprListDelete(db, pList: pChanges);
151651 sqlite3ExprDelete(db, p: pWhere);
151652#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
151653 sqlite3ExprListDelete(db, pOrderBy);
151654 sqlite3ExprDelete(db, pLimit);
151655#endif
151656 return;
151657}
151658/* Make sure "isView" and other macros defined above are undefined. Otherwise
151659** they may interfere with compilation of other functions in this file
151660** (or in another file, if this file becomes part of the amalgamation). */
151661#ifdef isView
151662 #undef isView
151663#endif
151664#ifdef pTrigger
151665 #undef pTrigger
151666#endif
151667
151668#ifndef SQLITE_OMIT_VIRTUALTABLE
151669/*
151670** Generate code for an UPDATE of a virtual table.
151671**
151672** There are two possible strategies - the default and the special
151673** "onepass" strategy. Onepass is only used if the virtual table
151674** implementation indicates that pWhere may match at most one row.
151675**
151676** The default strategy is to create an ephemeral table that contains
151677** for each row to be changed:
151678**
151679** (A) The original rowid of that row.
151680** (B) The revised rowid for the row.
151681** (C) The content of every column in the row.
151682**
151683** Then loop through the contents of this ephemeral table executing a
151684** VUpdate for each row. When finished, drop the ephemeral table.
151685**
151686** The "onepass" strategy does not use an ephemeral table. Instead, it
151687** stores the same values (A, B and C above) in a register array and
151688** makes a single invocation of VUpdate.
151689*/
151690static void updateVirtualTable(
151691 Parse *pParse, /* The parsing context */
151692 SrcList *pSrc, /* The virtual table to be modified */
151693 Table *pTab, /* The virtual table */
151694 ExprList *pChanges, /* The columns to change in the UPDATE statement */
151695 Expr *pRowid, /* Expression used to recompute the rowid */
151696 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
151697 Expr *pWhere, /* WHERE clause of the UPDATE statement */
151698 int onError /* ON CONFLICT strategy */
151699){
151700 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
151701 int ephemTab; /* Table holding the result of the SELECT */
151702 int i; /* Loop counter */
151703 sqlite3 *db = pParse->db; /* Database connection */
151704 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
151705 WhereInfo *pWInfo = 0;
151706 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
151707 int regArg; /* First register in VUpdate arg array */
151708 int regRec; /* Register in which to assemble record */
151709 int regRowid; /* Register for ephemeral table rowid */
151710 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
151711 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
151712 int eOnePass; /* True to use onepass strategy */
151713 int addr; /* Address of OP_OpenEphemeral */
151714
151715 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
151716 ** create and open the ephemeral table in which the records created from
151717 ** these arguments will be temporarily stored. */
151718 assert( v );
151719 ephemTab = pParse->nTab++;
151720 addr= sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: ephemTab, p2: nArg);
151721 regArg = pParse->nMem + 1;
151722 pParse->nMem += nArg;
151723 if( pSrc->nSrc>1 ){
151724 Index *pPk = 0;
151725 Expr *pRow;
151726 ExprList *pList;
151727 if( HasRowid(pTab) ){
151728 if( pRowid ){
151729 pRow = sqlite3ExprDup(db, p: pRowid, flags: 0);
151730 }else{
151731 pRow = sqlite3PExpr(pParse, TK_ROW, pLeft: 0, pRight: 0);
151732 }
151733 }else{
151734 i16 iPk; /* PRIMARY KEY column */
151735 pPk = sqlite3PrimaryKeyIndex(pTab);
151736 assert( pPk!=0 );
151737 assert( pPk->nKeyCol==1 );
151738 iPk = pPk->aiColumn[0];
151739 if( aXRef[iPk]>=0 ){
151740 pRow = sqlite3ExprDup(db, p: pChanges->a[aXRef[iPk]].pExpr, flags: 0);
151741 }else{
151742 pRow = exprRowColumn(pParse, iCol: iPk);
151743 }
151744 }
151745 pList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: pRow);
151746
151747 for(i=0; i<pTab->nCol; i++){
151748 if( aXRef[i]>=0 ){
151749 pList = sqlite3ExprListAppend(pParse, pList,
151750 pExpr: sqlite3ExprDup(db, p: pChanges->a[aXRef[i]].pExpr, flags: 0)
151751 );
151752 }else{
151753 Expr *pRowExpr = exprRowColumn(pParse, iCol: i);
151754 if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG;
151755 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pRowExpr);
151756 }
151757 }
151758
151759 updateFromSelect(pParse, iEph: ephemTab, pPk, pChanges: pList, pTabList: pSrc, pWhere, pOrderBy: 0, pLimit: 0);
151760 sqlite3ExprListDelete(db, pList);
151761 eOnePass = ONEPASS_OFF;
151762 }else{
151763 regRec = ++pParse->nMem;
151764 regRowid = ++pParse->nMem;
151765
151766 /* Start scanning the virtual table */
151767 pWInfo = sqlite3WhereBegin(
151768 pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0
151769 );
151770 if( pWInfo==0 ) return;
151771
151772 /* Populate the argument registers. */
151773 for(i=0; i<pTab->nCol; i++){
151774 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
151775 if( aXRef[i]>=0 ){
151776 sqlite3ExprCode(pParse, pExpr: pChanges->a[aXRef[i]].pExpr, target: regArg+2+i);
151777 }else{
151778 sqlite3VdbeAddOp3(p: v, OP_VColumn, p1: iCsr, p2: i, p3: regArg+2+i);
151779 sqlite3VdbeChangeP5(p: v, OPFLAG_NOCHNG);/* For sqlite3_vtab_nochange() */
151780 }
151781 }
151782 if( HasRowid(pTab) ){
151783 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCsr, p2: regArg);
151784 if( pRowid ){
151785 sqlite3ExprCode(pParse, pExpr: pRowid, target: regArg+1);
151786 }else{
151787 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCsr, p2: regArg+1);
151788 }
151789 }else{
151790 Index *pPk; /* PRIMARY KEY index */
151791 i16 iPk; /* PRIMARY KEY column */
151792 pPk = sqlite3PrimaryKeyIndex(pTab);
151793 assert( pPk!=0 );
151794 assert( pPk->nKeyCol==1 );
151795 iPk = pPk->aiColumn[0];
151796 sqlite3VdbeAddOp3(p: v, OP_VColumn, p1: iCsr, p2: iPk, p3: regArg);
151797 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: regArg+2+iPk, p2: regArg+1);
151798 }
151799
151800 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
151801
151802 /* There is no ONEPASS_MULTI on virtual tables */
151803 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
151804
151805 if( eOnePass ){
151806 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
151807 ** above. */
151808 sqlite3VdbeChangeToNoop(p: v, addr);
151809 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iCsr);
151810 }else{
151811 /* Create a record from the argument register contents and insert it into
151812 ** the ephemeral table. */
151813 sqlite3MultiWrite(pParse);
151814 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regArg, p2: nArg, p3: regRec);
151815#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
151816 /* Signal an assert() within OP_MakeRecord that it is allowed to
151817 ** accept no-change records with serial_type 10 */
151818 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
151819#endif
151820 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: ephemTab, p2: regRowid);
151821 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: ephemTab, p2: regRec, p3: regRowid);
151822 }
151823 }
151824
151825
151826 if( eOnePass==ONEPASS_OFF ){
151827 /* End the virtual table scan */
151828 if( pSrc->nSrc==1 ){
151829 sqlite3WhereEnd(pWInfo);
151830 }
151831
151832 /* Begin scanning through the ephemeral table. */
151833 addr = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: ephemTab); VdbeCoverage(v);
151834
151835 /* Extract arguments from the current row of the ephemeral table and
151836 ** invoke the VUpdate method. */
151837 for(i=0; i<nArg; i++){
151838 sqlite3VdbeAddOp3(p: v, OP_Column, p1: ephemTab, p2: i, p3: regArg+i);
151839 }
151840 }
151841 sqlite3VtabMakeWritable(pParse, pTab);
151842 sqlite3VdbeAddOp4(p: v, OP_VUpdate, p1: 0, p2: nArg, p3: regArg, zP4: pVTab, P4_VTAB);
151843 sqlite3VdbeChangeP5(p: v, p5: onError==OE_Default ? OE_Abort : onError);
151844 sqlite3MayAbort(pParse);
151845
151846 /* End of the ephemeral table scan. Or, if using the onepass strategy,
151847 ** jump to here if the scan visited zero rows. */
151848 if( eOnePass==ONEPASS_OFF ){
151849 sqlite3VdbeAddOp2(p: v, OP_Next, p1: ephemTab, p2: addr+1); VdbeCoverage(v);
151850 sqlite3VdbeJumpHere(p: v, addr);
151851 sqlite3VdbeAddOp2(p: v, OP_Close, p1: ephemTab, p2: 0);
151852 }else{
151853 sqlite3WhereEnd(pWInfo);
151854 }
151855}
151856#endif /* SQLITE_OMIT_VIRTUALTABLE */
151857
151858/************** End of update.c **********************************************/
151859/************** Begin file upsert.c ******************************************/
151860/*
151861** 2018-04-12
151862**
151863** The author disclaims copyright to this source code. In place of
151864** a legal notice, here is a blessing:
151865**
151866** May you do good and not evil.
151867** May you find forgiveness for yourself and forgive others.
151868** May you share freely, never taking more than you give.
151869**
151870*************************************************************************
151871** This file contains code to implement various aspects of UPSERT
151872** processing and handling of the Upsert object.
151873*/
151874/* #include "sqliteInt.h" */
151875
151876#ifndef SQLITE_OMIT_UPSERT
151877/*
151878** Free a list of Upsert objects
151879*/
151880static void SQLITE_NOINLINE upsertDelete(sqlite3 *db, Upsert *p){
151881 do{
151882 Upsert *pNext = p->pNextUpsert;
151883 sqlite3ExprListDelete(db, pList: p->pUpsertTarget);
151884 sqlite3ExprDelete(db, p: p->pUpsertTargetWhere);
151885 sqlite3ExprListDelete(db, pList: p->pUpsertSet);
151886 sqlite3ExprDelete(db, p: p->pUpsertWhere);
151887 sqlite3DbFree(db, p: p->pToFree);
151888 sqlite3DbFree(db, p);
151889 p = pNext;
151890 }while( p );
151891}
151892SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
151893 if( p ) upsertDelete(db, p);
151894}
151895
151896
151897/*
151898** Duplicate an Upsert object.
151899*/
151900SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
151901 if( p==0 ) return 0;
151902 return sqlite3UpsertNew(db,
151903 sqlite3ExprListDup(db, p: p->pUpsertTarget, flags: 0),
151904 sqlite3ExprDup(db, p: p->pUpsertTargetWhere, flags: 0),
151905 sqlite3ExprListDup(db, p: p->pUpsertSet, flags: 0),
151906 sqlite3ExprDup(db, p: p->pUpsertWhere, flags: 0),
151907 sqlite3UpsertDup(db, p: p->pNextUpsert)
151908 );
151909}
151910
151911/*
151912** Create a new Upsert object.
151913*/
151914SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
151915 sqlite3 *db, /* Determines which memory allocator to use */
151916 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
151917 Expr *pTargetWhere, /* Optional WHERE clause on the target */
151918 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
151919 Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */
151920 Upsert *pNext /* Next ON CONFLICT clause in the list */
151921){
151922 Upsert *pNew;
151923 pNew = sqlite3DbMallocZero(db, n: sizeof(Upsert));
151924 if( pNew==0 ){
151925 sqlite3ExprListDelete(db, pList: pTarget);
151926 sqlite3ExprDelete(db, p: pTargetWhere);
151927 sqlite3ExprListDelete(db, pList: pSet);
151928 sqlite3ExprDelete(db, p: pWhere);
151929 sqlite3UpsertDelete(db, p: pNext);
151930 return 0;
151931 }else{
151932 pNew->pUpsertTarget = pTarget;
151933 pNew->pUpsertTargetWhere = pTargetWhere;
151934 pNew->pUpsertSet = pSet;
151935 pNew->pUpsertWhere = pWhere;
151936 pNew->isDoUpdate = pSet!=0;
151937 pNew->pNextUpsert = pNext;
151938 }
151939 return pNew;
151940}
151941
151942/*
151943** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
151944** symbols in the conflict-target.
151945**
151946** Return SQLITE_OK if everything works, or an error code is something
151947** is wrong.
151948*/
151949SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
151950 Parse *pParse, /* The parsing context */
151951 SrcList *pTabList, /* Table into which we are inserting */
151952 Upsert *pUpsert /* The ON CONFLICT clauses */
151953){
151954 Table *pTab; /* That table into which we are inserting */
151955 int rc; /* Result code */
151956 int iCursor; /* Cursor used by pTab */
151957 Index *pIdx; /* One of the indexes of pTab */
151958 ExprList *pTarget; /* The conflict-target clause */
151959 Expr *pTerm; /* One term of the conflict-target clause */
151960 NameContext sNC; /* Context for resolving symbolic names */
151961 Expr sCol[2]; /* Index column converted into an Expr */
151962 int nClause = 0; /* Counter of ON CONFLICT clauses */
151963
151964 assert( pTabList->nSrc==1 );
151965 assert( pTabList->a[0].pTab!=0 );
151966 assert( pUpsert!=0 );
151967 assert( pUpsert->pUpsertTarget!=0 );
151968
151969 /* Resolve all symbolic names in the conflict-target clause, which
151970 ** includes both the list of columns and the optional partial-index
151971 ** WHERE clause.
151972 */
151973 memset(s: &sNC, c: 0, n: sizeof(sNC));
151974 sNC.pParse = pParse;
151975 sNC.pSrcList = pTabList;
151976 for(; pUpsert && pUpsert->pUpsertTarget;
151977 pUpsert=pUpsert->pNextUpsert, nClause++){
151978 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pUpsert->pUpsertTarget);
151979 if( rc ) return rc;
151980 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pUpsert->pUpsertTargetWhere);
151981 if( rc ) return rc;
151982
151983 /* Check to see if the conflict target matches the rowid. */
151984 pTab = pTabList->a[0].pTab;
151985 pTarget = pUpsert->pUpsertTarget;
151986 iCursor = pTabList->a[0].iCursor;
151987 if( HasRowid(pTab)
151988 && pTarget->nExpr==1
151989 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
151990 && pTerm->iColumn==XN_ROWID
151991 ){
151992 /* The conflict-target is the rowid of the primary table */
151993 assert( pUpsert->pUpsertIdx==0 );
151994 continue;
151995 }
151996
151997 /* Initialize sCol[0..1] to be an expression parse tree for a
151998 ** single column of an index. The sCol[0] node will be the TK_COLLATE
151999 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
152000 ** will populate the specific collation and column number values
152001 ** prior to comparing against the conflict-target expression.
152002 */
152003 memset(s: sCol, c: 0, n: sizeof(sCol));
152004 sCol[0].op = TK_COLLATE;
152005 sCol[0].pLeft = &sCol[1];
152006 sCol[1].op = TK_COLUMN;
152007 sCol[1].iTable = pTabList->a[0].iCursor;
152008
152009 /* Check for matches against other indexes */
152010 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
152011 int ii, jj, nn;
152012 if( !IsUniqueIndex(pIdx) ) continue;
152013 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
152014 if( pIdx->pPartIdxWhere ){
152015 if( pUpsert->pUpsertTargetWhere==0 ) continue;
152016 if( sqlite3ExprCompare(pParse, pA: pUpsert->pUpsertTargetWhere,
152017 pB: pIdx->pPartIdxWhere, iTab: iCursor)!=0 ){
152018 continue;
152019 }
152020 }
152021 nn = pIdx->nKeyCol;
152022 for(ii=0; ii<nn; ii++){
152023 Expr *pExpr;
152024 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
152025 if( pIdx->aiColumn[ii]==XN_EXPR ){
152026 assert( pIdx->aColExpr!=0 );
152027 assert( pIdx->aColExpr->nExpr>ii );
152028 assert( pIdx->bHasExpr );
152029 pExpr = pIdx->aColExpr->a[ii].pExpr;
152030 if( pExpr->op!=TK_COLLATE ){
152031 sCol[0].pLeft = pExpr;
152032 pExpr = &sCol[0];
152033 }
152034 }else{
152035 sCol[0].pLeft = &sCol[1];
152036 sCol[1].iColumn = pIdx->aiColumn[ii];
152037 pExpr = &sCol[0];
152038 }
152039 for(jj=0; jj<nn; jj++){
152040 if( sqlite3ExprCompare(pParse: 0,pA: pTarget->a[jj].pExpr,pB: pExpr,iTab: iCursor)<2 ){
152041 break; /* Column ii of the index matches column jj of target */
152042 }
152043 }
152044 if( jj>=nn ){
152045 /* The target contains no match for column jj of the index */
152046 break;
152047 }
152048 }
152049 if( ii<nn ){
152050 /* Column ii of the index did not match any term of the conflict target.
152051 ** Continue the search with the next index. */
152052 continue;
152053 }
152054 pUpsert->pUpsertIdx = pIdx;
152055 break;
152056 }
152057 if( pUpsert->pUpsertIdx==0 ){
152058 char zWhich[16];
152059 if( nClause==0 && pUpsert->pNextUpsert==0 ){
152060 zWhich[0] = 0;
152061 }else{
152062 sqlite3_snprintf(n: sizeof(zWhich),zBuf: zWhich,zFormat: "%r ", nClause+1);
152063 }
152064 sqlite3ErrorMsg(pParse, zFormat: "%sON CONFLICT clause does not match any "
152065 "PRIMARY KEY or UNIQUE constraint", zWhich);
152066 return SQLITE_ERROR;
152067 }
152068 }
152069 return SQLITE_OK;
152070}
152071
152072/*
152073** Return true if pUpsert is the last ON CONFLICT clause with a
152074** conflict target, or if pUpsert is followed by another ON CONFLICT
152075** clause that targets the INTEGER PRIMARY KEY.
152076*/
152077SQLITE_PRIVATE int sqlite3UpsertNextIsIPK(Upsert *pUpsert){
152078 Upsert *pNext;
152079 if( NEVER(pUpsert==0) ) return 0;
152080 pNext = pUpsert->pNextUpsert;
152081 if( pNext==0 ) return 1;
152082 if( pNext->pUpsertTarget==0 ) return 1;
152083 if( pNext->pUpsertIdx==0 ) return 1;
152084 return 0;
152085}
152086
152087/*
152088** Given the list of ON CONFLICT clauses described by pUpsert, and
152089** a particular index pIdx, return a pointer to the particular ON CONFLICT
152090** clause that applies to the index. Or, if the index is not subject to
152091** any ON CONFLICT clause, return NULL.
152092*/
152093SQLITE_PRIVATE Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){
152094 while(
152095 pUpsert
152096 && pUpsert->pUpsertTarget!=0
152097 && pUpsert->pUpsertIdx!=pIdx
152098 ){
152099 pUpsert = pUpsert->pNextUpsert;
152100 }
152101 return pUpsert;
152102}
152103
152104/*
152105** Generate bytecode that does an UPDATE as part of an upsert.
152106**
152107** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
152108** In this case parameter iCur is a cursor open on the table b-tree that
152109** currently points to the conflicting table row. Otherwise, if pIdx
152110** is not NULL, then pIdx is the constraint that failed and iCur is a
152111** cursor points to the conflicting row.
152112*/
152113SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
152114 Parse *pParse, /* The parsing and code-generating context */
152115 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
152116 Table *pTab, /* The table being updated */
152117 Index *pIdx, /* The UNIQUE constraint that failed */
152118 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
152119){
152120 Vdbe *v = pParse->pVdbe;
152121 sqlite3 *db = pParse->db;
152122 SrcList *pSrc; /* FROM clause for the UPDATE */
152123 int iDataCur;
152124 int i;
152125 Upsert *pTop = pUpsert;
152126
152127 assert( v!=0 );
152128 assert( pUpsert!=0 );
152129 iDataCur = pUpsert->iDataCur;
152130 pUpsert = sqlite3UpsertOfIndex(pUpsert: pTop, pIdx);
152131 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
152132 if( pIdx && iCur!=iDataCur ){
152133 if( HasRowid(pTab) ){
152134 int regRowid = sqlite3GetTempReg(pParse);
152135 sqlite3VdbeAddOp2(p: v, OP_IdxRowid, p1: iCur, p2: regRowid);
152136 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: iDataCur, p2: 0, p3: regRowid);
152137 VdbeCoverage(v);
152138 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
152139 }else{
152140 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
152141 int nPk = pPk->nKeyCol;
152142 int iPk = pParse->nMem+1;
152143 pParse->nMem += nPk;
152144 for(i=0; i<nPk; i++){
152145 int k;
152146 assert( pPk->aiColumn[i]>=0 );
152147 k = sqlite3TableColumnToIndex(pIdx, iCol: pPk->aiColumn[i]);
152148 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iCur, p2: k, p3: iPk+i);
152149 VdbeComment((v, "%s.%s", pIdx->zName,
152150 pTab->aCol[pPk->aiColumn[i]].zCnName));
152151 }
152152 sqlite3VdbeVerifyAbortable(v, OE_Abort);
152153 i = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iDataCur, p2: 0, p3: iPk, p4: nPk);
152154 VdbeCoverage(v);
152155 sqlite3VdbeAddOp4(p: v, OP_Halt, SQLITE_CORRUPT, OE_Abort, p3: 0,
152156 zP4: "corrupt database", P4_STATIC);
152157 sqlite3MayAbort(pParse);
152158 sqlite3VdbeJumpHere(p: v, addr: i);
152159 }
152160 }
152161 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
152162 ** So we have to make a copy before passing it down into sqlite3Update() */
152163 pSrc = sqlite3SrcListDup(db, p: pTop->pUpsertSrc, flags: 0);
152164 /* excluded.* columns of type REAL need to be converted to a hard real */
152165 for(i=0; i<pTab->nCol; i++){
152166 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
152167 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: pTop->regData+i);
152168 }
152169 }
152170 sqlite3Update(pParse, pTabList: pSrc, pChanges: sqlite3ExprListDup(db,p: pUpsert->pUpsertSet,flags: 0),
152171 pWhere: sqlite3ExprDup(db,p: pUpsert->pUpsertWhere,flags: 0), OE_Abort, pOrderBy: 0, pLimit: 0, pUpsert);
152172 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
152173}
152174
152175#endif /* SQLITE_OMIT_UPSERT */
152176
152177/************** End of upsert.c **********************************************/
152178/************** Begin file vacuum.c ******************************************/
152179/*
152180** 2003 April 6
152181**
152182** The author disclaims copyright to this source code. In place of
152183** a legal notice, here is a blessing:
152184**
152185** May you do good and not evil.
152186** May you find forgiveness for yourself and forgive others.
152187** May you share freely, never taking more than you give.
152188**
152189*************************************************************************
152190** This file contains code used to implement the VACUUM command.
152191**
152192** Most of the code in this file may be omitted by defining the
152193** SQLITE_OMIT_VACUUM macro.
152194*/
152195/* #include "sqliteInt.h" */
152196/* #include "vdbeInt.h" */
152197
152198#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
152199
152200/*
152201** Execute zSql on database db.
152202**
152203** If zSql returns rows, then each row will have exactly one
152204** column. (This will only happen if zSql begins with "SELECT".)
152205** Take each row of result and call execSql() again recursively.
152206**
152207** The execSqlF() routine does the same thing, except it accepts
152208** a format string as its third argument
152209*/
152210static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
152211 sqlite3_stmt *pStmt;
152212 int rc;
152213
152214 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
152215 rc = sqlite3_prepare_v2(db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
152216 if( rc!=SQLITE_OK ) return rc;
152217 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
152218 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,i: 0);
152219 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
152220 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
152221 ** or INSERT. Historically there have been attacks that first
152222 ** corrupt the sqlite_schema.sql field with other kinds of statements
152223 ** then run VACUUM to get those statements to execute at inappropriate
152224 ** times. */
152225 if( zSubSql
152226 && (strncmp(s1: zSubSql,s2: "CRE",n: 3)==0 || strncmp(s1: zSubSql,s2: "INS",n: 3)==0)
152227 ){
152228 rc = execSql(db, pzErrMsg, zSql: zSubSql);
152229 if( rc!=SQLITE_OK ) break;
152230 }
152231 }
152232 assert( rc!=SQLITE_ROW );
152233 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
152234 if( rc ){
152235 sqlite3SetString(pz: pzErrMsg, db, zNew: sqlite3_errmsg(db));
152236 }
152237 (void)sqlite3_finalize(pStmt);
152238 return rc;
152239}
152240static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
152241 char *z;
152242 va_list ap;
152243 int rc;
152244 va_start(ap, zSql);
152245 z = sqlite3VMPrintf(db, zFormat: zSql, ap);
152246 va_end(ap);
152247 if( z==0 ) return SQLITE_NOMEM;
152248 rc = execSql(db, pzErrMsg, zSql: z);
152249 sqlite3DbFree(db, p: z);
152250 return rc;
152251}
152252
152253/*
152254** The VACUUM command is used to clean up the database,
152255** collapse free space, etc. It is modelled after the VACUUM command
152256** in PostgreSQL. The VACUUM command works as follows:
152257**
152258** (1) Create a new transient database file
152259** (2) Copy all content from the database being vacuumed into
152260** the new transient database file
152261** (3) Copy content from the transient database back into the
152262** original database.
152263**
152264** The transient database requires temporary disk space approximately
152265** equal to the size of the original database. The copy operation of
152266** step (3) requires additional temporary disk space approximately equal
152267** to the size of the original database for the rollback journal.
152268** Hence, temporary disk space that is approximately 2x the size of the
152269** original database is required. Every page of the database is written
152270** approximately 3 times: Once for step (2) and twice for step (3).
152271** Two writes per page are required in step (3) because the original
152272** database content must be written into the rollback journal prior to
152273** overwriting the database with the vacuumed content.
152274**
152275** Only 1x temporary space and only 1x writes would be required if
152276** the copy of step (3) were replaced by deleting the original database
152277** and renaming the transient database as the original. But that will
152278** not work if other processes are attached to the original database.
152279** And a power loss in between deleting the original and renaming the
152280** transient would cause the database file to appear to be deleted
152281** following reboot.
152282*/
152283SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
152284 Vdbe *v = sqlite3GetVdbe(pParse);
152285 int iDb = 0;
152286 if( v==0 ) goto build_vacuum_end;
152287 if( pParse->nErr ) goto build_vacuum_end;
152288 if( pNm ){
152289#ifndef SQLITE_BUG_COMPATIBLE_20160819
152290 /* Default behavior: Report an error if the argument to VACUUM is
152291 ** not recognized */
152292 iDb = sqlite3TwoPartName(pParse, pName1: pNm, pName2: pNm, pUnqual: &pNm);
152293 if( iDb<0 ) goto build_vacuum_end;
152294#else
152295 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
152296 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
152297 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
152298 ** The buggy behavior is required for binary compatibility with some
152299 ** legacy applications. */
152300 iDb = sqlite3FindDb(pParse->db, pNm);
152301 if( iDb<0 ) iDb = 0;
152302#endif
152303 }
152304 if( iDb!=1 ){
152305 int iIntoReg = 0;
152306 if( pInto && sqlite3ResolveSelfReference(pParse,pTab: 0,type: 0,pExpr: pInto,pList: 0)==0 ){
152307 iIntoReg = ++pParse->nMem;
152308 sqlite3ExprCode(pParse, pExpr: pInto, target: iIntoReg);
152309 }
152310 sqlite3VdbeAddOp2(p: v, OP_Vacuum, p1: iDb, p2: iIntoReg);
152311 sqlite3VdbeUsesBtree(p: v, i: iDb);
152312 }
152313build_vacuum_end:
152314 sqlite3ExprDelete(db: pParse->db, p: pInto);
152315 return;
152316}
152317
152318/*
152319** This routine implements the OP_Vacuum opcode of the VDBE.
152320*/
152321SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
152322 char **pzErrMsg, /* Write error message here */
152323 sqlite3 *db, /* Database connection */
152324 int iDb, /* Which attached DB to vacuum */
152325 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
152326){
152327 int rc = SQLITE_OK; /* Return code from service routines */
152328 Btree *pMain; /* The database being vacuumed */
152329 Btree *pTemp; /* The temporary database we vacuum into */
152330 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
152331 u64 saved_flags; /* Saved value of db->flags */
152332 i64 saved_nChange; /* Saved value of db->nChange */
152333 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
152334 u32 saved_openFlags; /* Saved value of db->openFlags */
152335 u8 saved_mTrace; /* Saved trace settings */
152336 Db *pDb = 0; /* Database to detach at end of vacuum */
152337 int isMemDb; /* True if vacuuming a :memory: database */
152338 int nRes; /* Bytes of reserved space at the end of each page */
152339 int nDb; /* Number of attached databases */
152340 const char *zDbMain; /* Schema name of database to vacuum */
152341 const char *zOut; /* Name of output file */
152342 u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */
152343
152344 if( !db->autoCommit ){
152345 sqlite3SetString(pz: pzErrMsg, db, zNew: "cannot VACUUM from within a transaction");
152346 return SQLITE_ERROR; /* IMP: R-12218-18073 */
152347 }
152348 if( db->nVdbeActive>1 ){
152349 sqlite3SetString(pz: pzErrMsg, db,zNew: "cannot VACUUM - SQL statements in progress");
152350 return SQLITE_ERROR; /* IMP: R-15610-35227 */
152351 }
152352 saved_openFlags = db->openFlags;
152353 if( pOut ){
152354 if( sqlite3_value_type(pVal: pOut)!=SQLITE_TEXT ){
152355 sqlite3SetString(pz: pzErrMsg, db, zNew: "non-text filename");
152356 return SQLITE_ERROR;
152357 }
152358 zOut = (const char*)sqlite3_value_text(pVal: pOut);
152359 db->openFlags &= ~SQLITE_OPEN_READONLY;
152360 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
152361 }else{
152362 zOut = "";
152363 }
152364
152365 /* Save the current value of the database flags so that it can be
152366 ** restored before returning. Then set the writable-schema flag, and
152367 ** disable CHECK and foreign key constraints. */
152368 saved_flags = db->flags;
152369 saved_mDbFlags = db->mDbFlags;
152370 saved_nChange = db->nChange;
152371 saved_nTotalChange = db->nTotalChange;
152372 saved_mTrace = db->mTrace;
152373 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
152374 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
152375 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
152376 | SQLITE_Defensive | SQLITE_CountRows);
152377 db->mTrace = 0;
152378
152379 zDbMain = db->aDb[iDb].zDbSName;
152380 pMain = db->aDb[iDb].pBt;
152381 isMemDb = sqlite3PagerIsMemdb(pPager: sqlite3BtreePager(p: pMain));
152382
152383 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
152384 ** can be set to 'off' for this file, as it is not recovered if a crash
152385 ** occurs anyway. The integrity of the database is maintained by a
152386 ** (possibly synchronous) transaction opened on the main database before
152387 ** sqlite3BtreeCopyFile() is called.
152388 **
152389 ** An optimization would be to use a non-journaled pager.
152390 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
152391 ** that actually made the VACUUM run slower. Very little journalling
152392 ** actually occurs when doing a vacuum since the vacuum_db is initially
152393 ** empty. Only the journal header is written. Apparently it takes more
152394 ** time to parse and run the PRAGMA to turn journalling off than it does
152395 ** to write the journal header file.
152396 */
152397 nDb = db->nDb;
152398 rc = execSqlF(db, pzErrMsg, zSql: "ATTACH %Q AS vacuum_db", zOut);
152399 db->openFlags = saved_openFlags;
152400 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152401 assert( (db->nDb-1)==nDb );
152402 pDb = &db->aDb[nDb];
152403 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
152404 pTemp = pDb->pBt;
152405 if( pOut ){
152406 sqlite3_file *id = sqlite3PagerFile(pPager: sqlite3BtreePager(p: pTemp));
152407 i64 sz = 0;
152408 if( id->pMethods!=0 && (sqlite3OsFileSize(id, pSize: &sz)!=SQLITE_OK || sz>0) ){
152409 rc = SQLITE_ERROR;
152410 sqlite3SetString(pz: pzErrMsg, db, zNew: "output file already exists");
152411 goto end_of_vacuum;
152412 }
152413 db->mDbFlags |= DBFLAG_VacuumInto;
152414
152415 /* For a VACUUM INTO, the pager-flags are set to the same values as
152416 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
152417 ** is always set. */
152418 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
152419 }
152420 nRes = sqlite3BtreeGetRequestedReserve(p: pMain);
152421
152422 sqlite3BtreeSetCacheSize(p: pTemp, mxPage: db->aDb[iDb].pSchema->cache_size);
152423 sqlite3BtreeSetSpillSize(p: pTemp, mxPage: sqlite3BtreeSetSpillSize(p: pMain,mxPage: 0));
152424 sqlite3BtreeSetPagerFlags(p: pTemp, pgFlags: pgflags|PAGER_CACHESPILL);
152425
152426 /* Begin a transaction and take an exclusive lock on the main database
152427 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
152428 ** to ensure that we do not try to change the page-size on a WAL database.
152429 */
152430 rc = execSql(db, pzErrMsg, zSql: "BEGIN");
152431 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152432 rc = sqlite3BtreeBeginTrans(p: pMain, wrflag: pOut==0 ? 2 : 0, pSchemaVersion: 0);
152433 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152434
152435 /* Do not attempt to change the page size for a WAL database */
152436 if( sqlite3PagerGetJournalMode(pPager: sqlite3BtreePager(p: pMain))
152437 ==PAGER_JOURNALMODE_WAL
152438 && pOut==0
152439 ){
152440 db->nextPagesize = 0;
152441 }
152442
152443 if( sqlite3BtreeSetPageSize(p: pTemp, pageSize: sqlite3BtreeGetPageSize(p: pMain), nReserve: nRes, iFix: 0)
152444 || (!isMemDb && sqlite3BtreeSetPageSize(p: pTemp, pageSize: db->nextPagesize, nReserve: nRes, iFix: 0))
152445 || NEVER(db->mallocFailed)
152446 ){
152447 rc = SQLITE_NOMEM_BKPT;
152448 goto end_of_vacuum;
152449 }
152450
152451#ifndef SQLITE_OMIT_AUTOVACUUM
152452 sqlite3BtreeSetAutoVacuum(p: pTemp, autoVacuum: db->nextAutovac>=0 ? db->nextAutovac :
152453 sqlite3BtreeGetAutoVacuum(p: pMain));
152454#endif
152455
152456 /* Query the schema of the main database. Create a mirror schema
152457 ** in the temporary database.
152458 */
152459 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
152460 rc = execSqlF(db, pzErrMsg,
152461 zSql: "SELECT sql FROM \"%w\".sqlite_schema"
152462 " WHERE type='table'AND name<>'sqlite_sequence'"
152463 " AND coalesce(rootpage,1)>0",
152464 zDbMain
152465 );
152466 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152467 rc = execSqlF(db, pzErrMsg,
152468 zSql: "SELECT sql FROM \"%w\".sqlite_schema"
152469 " WHERE type='index'",
152470 zDbMain
152471 );
152472 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152473 db->init.iDb = 0;
152474
152475 /* Loop through the tables in the main database. For each, do
152476 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
152477 ** the contents to the temporary database.
152478 */
152479 rc = execSqlF(db, pzErrMsg,
152480 zSql: "SELECT'INSERT INTO vacuum_db.'||quote(name)"
152481 "||' SELECT*FROM\"%w\".'||quote(name)"
152482 "FROM vacuum_db.sqlite_schema "
152483 "WHERE type='table'AND coalesce(rootpage,1)>0",
152484 zDbMain
152485 );
152486 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
152487 db->mDbFlags &= ~DBFLAG_Vacuum;
152488 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152489
152490 /* Copy the triggers, views, and virtual tables from the main database
152491 ** over to the temporary database. None of these objects has any
152492 ** associated storage, so all we have to do is copy their entries
152493 ** from the schema table.
152494 */
152495 rc = execSqlF(db, pzErrMsg,
152496 zSql: "INSERT INTO vacuum_db.sqlite_schema"
152497 " SELECT*FROM \"%w\".sqlite_schema"
152498 " WHERE type IN('view','trigger')"
152499 " OR(type='table'AND rootpage=0)",
152500 zDbMain
152501 );
152502 if( rc ) goto end_of_vacuum;
152503
152504 /* At this point, there is a write transaction open on both the
152505 ** vacuum database and the main database. Assuming no error occurs,
152506 ** both transactions are closed by this block - the main database
152507 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
152508 ** call to sqlite3BtreeCommit().
152509 */
152510 {
152511 u32 meta;
152512 int i;
152513
152514 /* This array determines which meta meta values are preserved in the
152515 ** vacuum. Even entries are the meta value number and odd entries
152516 ** are an increment to apply to the meta value after the vacuum.
152517 ** The increment is used to increase the schema cookie so that other
152518 ** connections to the same database will know to reread the schema.
152519 */
152520 static const unsigned char aCopy[] = {
152521 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
152522 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
152523 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
152524 BTREE_USER_VERSION, 0, /* Preserve the user version */
152525 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
152526 };
152527
152528 assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) );
152529 assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) );
152530
152531 /* Copy Btree meta values */
152532 for(i=0; i<ArraySize(aCopy); i+=2){
152533 /* GetMeta() and UpdateMeta() cannot fail in this context because
152534 ** we already have page 1 loaded into cache and marked dirty. */
152535 sqlite3BtreeGetMeta(p: pMain, idx: aCopy[i], pMeta: &meta);
152536 rc = sqlite3BtreeUpdateMeta(p: pTemp, idx: aCopy[i], iMeta: meta+aCopy[i+1]);
152537 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
152538 }
152539
152540 if( pOut==0 ){
152541 rc = sqlite3BtreeCopyFile(pTo: pMain, pFrom: pTemp);
152542 }
152543 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152544 rc = sqlite3BtreeCommit(p: pTemp);
152545 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152546#ifndef SQLITE_OMIT_AUTOVACUUM
152547 if( pOut==0 ){
152548 sqlite3BtreeSetAutoVacuum(p: pMain, autoVacuum: sqlite3BtreeGetAutoVacuum(p: pTemp));
152549 }
152550#endif
152551 }
152552
152553 assert( rc==SQLITE_OK );
152554 if( pOut==0 ){
152555 nRes = sqlite3BtreeGetRequestedReserve(p: pTemp);
152556 rc = sqlite3BtreeSetPageSize(p: pMain, pageSize: sqlite3BtreeGetPageSize(p: pTemp), nReserve: nRes,iFix: 1);
152557 }
152558
152559end_of_vacuum:
152560 /* Restore the original value of db->flags */
152561 db->init.iDb = 0;
152562 db->mDbFlags = saved_mDbFlags;
152563 db->flags = saved_flags;
152564 db->nChange = saved_nChange;
152565 db->nTotalChange = saved_nTotalChange;
152566 db->mTrace = saved_mTrace;
152567 sqlite3BtreeSetPageSize(p: pMain, pageSize: -1, nReserve: 0, iFix: 1);
152568
152569 /* Currently there is an SQL level transaction open on the vacuum
152570 ** database. No locks are held on any other files (since the main file
152571 ** was committed at the btree level). So it safe to end the transaction
152572 ** by manually setting the autoCommit flag to true and detaching the
152573 ** vacuum database. The vacuum_db journal file is deleted when the pager
152574 ** is closed by the DETACH.
152575 */
152576 db->autoCommit = 1;
152577
152578 if( pDb ){
152579 sqlite3BtreeClose(p: pDb->pBt);
152580 pDb->pBt = 0;
152581 pDb->pSchema = 0;
152582 }
152583
152584 /* This both clears the schemas and reduces the size of the db->aDb[]
152585 ** array. */
152586 sqlite3ResetAllSchemasOfConnection(db);
152587
152588 return rc;
152589}
152590
152591#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
152592
152593/************** End of vacuum.c **********************************************/
152594/************** Begin file vtab.c ********************************************/
152595/*
152596** 2006 June 10
152597**
152598** The author disclaims copyright to this source code. In place of
152599** a legal notice, here is a blessing:
152600**
152601** May you do good and not evil.
152602** May you find forgiveness for yourself and forgive others.
152603** May you share freely, never taking more than you give.
152604**
152605*************************************************************************
152606** This file contains code used to help implement virtual tables.
152607*/
152608#ifndef SQLITE_OMIT_VIRTUALTABLE
152609/* #include "sqliteInt.h" */
152610
152611/*
152612** Before a virtual table xCreate() or xConnect() method is invoked, the
152613** sqlite3.pVtabCtx member variable is set to point to an instance of
152614** this struct allocated on the stack. It is used by the implementation of
152615** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
152616** are invoked only from within xCreate and xConnect methods.
152617*/
152618struct VtabCtx {
152619 VTable *pVTable; /* The virtual table being constructed */
152620 Table *pTab; /* The Table object to which the virtual table belongs */
152621 VtabCtx *pPrior; /* Parent context (if any) */
152622 int bDeclared; /* True after sqlite3_declare_vtab() is called */
152623};
152624
152625/*
152626** Construct and install a Module object for a virtual table. When this
152627** routine is called, it is guaranteed that all appropriate locks are held
152628** and the module is not already part of the connection.
152629**
152630** If there already exists a module with zName, replace it with the new one.
152631** If pModule==0, then delete the module zName if it exists.
152632*/
152633SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
152634 sqlite3 *db, /* Database in which module is registered */
152635 const char *zName, /* Name assigned to this module */
152636 const sqlite3_module *pModule, /* The definition of the module */
152637 void *pAux, /* Context pointer for xCreate/xConnect */
152638 void (*xDestroy)(void *) /* Module destructor function */
152639){
152640 Module *pMod;
152641 Module *pDel;
152642 char *zCopy;
152643 if( pModule==0 ){
152644 zCopy = (char*)zName;
152645 pMod = 0;
152646 }else{
152647 int nName = sqlite3Strlen30(z: zName);
152648 pMod = (Module *)sqlite3Malloc(n: sizeof(Module) + nName + 1);
152649 if( pMod==0 ){
152650 sqlite3OomFault(db);
152651 return 0;
152652 }
152653 zCopy = (char *)(&pMod[1]);
152654 memcpy(dest: zCopy, src: zName, n: nName+1);
152655 pMod->zName = zCopy;
152656 pMod->pModule = pModule;
152657 pMod->pAux = pAux;
152658 pMod->xDestroy = xDestroy;
152659 pMod->pEpoTab = 0;
152660 pMod->nRefModule = 1;
152661 }
152662 pDel = (Module *)sqlite3HashInsert(pH: &db->aModule,pKey: zCopy,data: (void*)pMod);
152663 if( pDel ){
152664 if( pDel==pMod ){
152665 sqlite3OomFault(db);
152666 sqlite3DbFree(db, p: pDel);
152667 pMod = 0;
152668 }else{
152669 sqlite3VtabEponymousTableClear(db, pDel);
152670 sqlite3VtabModuleUnref(db, pDel);
152671 }
152672 }
152673 return pMod;
152674}
152675
152676/*
152677** The actual function that does the work of creating a new module.
152678** This function implements the sqlite3_create_module() and
152679** sqlite3_create_module_v2() interfaces.
152680*/
152681static int createModule(
152682 sqlite3 *db, /* Database in which module is registered */
152683 const char *zName, /* Name assigned to this module */
152684 const sqlite3_module *pModule, /* The definition of the module */
152685 void *pAux, /* Context pointer for xCreate/xConnect */
152686 void (*xDestroy)(void *) /* Module destructor function */
152687){
152688 int rc = SQLITE_OK;
152689
152690 sqlite3_mutex_enter(p: db->mutex);
152691 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
152692 rc = sqlite3ApiExit(db, rc);
152693 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
152694 sqlite3_mutex_leave(p: db->mutex);
152695 return rc;
152696}
152697
152698
152699/*
152700** External API function used to create a new virtual-table module.
152701*/
152702SQLITE_API int sqlite3_create_module(
152703 sqlite3 *db, /* Database in which module is registered */
152704 const char *zName, /* Name assigned to this module */
152705 const sqlite3_module *pModule, /* The definition of the module */
152706 void *pAux /* Context pointer for xCreate/xConnect */
152707){
152708#ifdef SQLITE_ENABLE_API_ARMOR
152709 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
152710#endif
152711 return createModule(db, zName, pModule, pAux, xDestroy: 0);
152712}
152713
152714/*
152715** External API function used to create a new virtual-table module.
152716*/
152717SQLITE_API int sqlite3_create_module_v2(
152718 sqlite3 *db, /* Database in which module is registered */
152719 const char *zName, /* Name assigned to this module */
152720 const sqlite3_module *pModule, /* The definition of the module */
152721 void *pAux, /* Context pointer for xCreate/xConnect */
152722 void (*xDestroy)(void *) /* Module destructor function */
152723){
152724#ifdef SQLITE_ENABLE_API_ARMOR
152725 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
152726#endif
152727 return createModule(db, zName, pModule, pAux, xDestroy);
152728}
152729
152730/*
152731** External API to drop all virtual-table modules, except those named
152732** on the azNames list.
152733*/
152734SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
152735 HashElem *pThis, *pNext;
152736#ifdef SQLITE_ENABLE_API_ARMOR
152737 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
152738#endif
152739 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
152740 Module *pMod = (Module*)sqliteHashData(pThis);
152741 pNext = sqliteHashNext(pThis);
152742 if( azNames ){
152743 int ii;
152744 for(ii=0; azNames[ii]!=0 && strcmp(s1: azNames[ii],s2: pMod->zName)!=0; ii++){}
152745 if( azNames[ii]!=0 ) continue;
152746 }
152747 createModule(db, zName: pMod->zName, pModule: 0, pAux: 0, xDestroy: 0);
152748 }
152749 return SQLITE_OK;
152750}
152751
152752/*
152753** Decrement the reference count on a Module object. Destroy the
152754** module when the reference count reaches zero.
152755*/
152756SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
152757 assert( pMod->nRefModule>0 );
152758 pMod->nRefModule--;
152759 if( pMod->nRefModule==0 ){
152760 if( pMod->xDestroy ){
152761 pMod->xDestroy(pMod->pAux);
152762 }
152763 assert( pMod->pEpoTab==0 );
152764 sqlite3DbFree(db, p: pMod);
152765 }
152766}
152767
152768/*
152769** Lock the virtual table so that it cannot be disconnected.
152770** Locks nest. Every lock should have a corresponding unlock.
152771** If an unlock is omitted, resources leaks will occur.
152772**
152773** If a disconnect is attempted while a virtual table is locked,
152774** the disconnect is deferred until all locks have been removed.
152775*/
152776SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
152777 pVTab->nRef++;
152778}
152779
152780
152781/*
152782** pTab is a pointer to a Table structure representing a virtual-table.
152783** Return a pointer to the VTable object used by connection db to access
152784** this virtual-table, if one has been created, or NULL otherwise.
152785*/
152786SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
152787 VTable *pVtab;
152788 assert( IsVirtual(pTab) );
152789 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
152790 return pVtab;
152791}
152792
152793/*
152794** Decrement the ref-count on a virtual table object. When the ref-count
152795** reaches zero, call the xDisconnect() method to delete the object.
152796*/
152797SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
152798 sqlite3 *db = pVTab->db;
152799
152800 assert( db );
152801 assert( pVTab->nRef>0 );
152802 assert( db->eOpenState==SQLITE_STATE_OPEN
152803 || db->eOpenState==SQLITE_STATE_ZOMBIE );
152804
152805 pVTab->nRef--;
152806 if( pVTab->nRef==0 ){
152807 sqlite3_vtab *p = pVTab->pVtab;
152808 if( p ){
152809 p->pModule->xDisconnect(p);
152810 }
152811 sqlite3VtabModuleUnref(db: pVTab->db, pMod: pVTab->pMod);
152812 sqlite3DbFree(db, p: pVTab);
152813 }
152814}
152815
152816/*
152817** Table p is a virtual table. This function moves all elements in the
152818** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
152819** database connections to be disconnected at the next opportunity.
152820** Except, if argument db is not NULL, then the entry associated with
152821** connection db is left in the p->u.vtab.p list.
152822*/
152823static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
152824 VTable *pRet = 0;
152825 VTable *pVTable;
152826
152827 assert( IsVirtual(p) );
152828 pVTable = p->u.vtab.p;
152829 p->u.vtab.p = 0;
152830
152831 /* Assert that the mutex (if any) associated with the BtShared database
152832 ** that contains table p is held by the caller. See header comments
152833 ** above function sqlite3VtabUnlockList() for an explanation of why
152834 ** this makes it safe to access the sqlite3.pDisconnect list of any
152835 ** database connection that may have an entry in the p->u.vtab.p list.
152836 */
152837 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
152838
152839 while( pVTable ){
152840 sqlite3 *db2 = pVTable->db;
152841 VTable *pNext = pVTable->pNext;
152842 assert( db2 );
152843 if( db2==db ){
152844 pRet = pVTable;
152845 p->u.vtab.p = pRet;
152846 pRet->pNext = 0;
152847 }else{
152848 pVTable->pNext = db2->pDisconnect;
152849 db2->pDisconnect = pVTable;
152850 }
152851 pVTable = pNext;
152852 }
152853
152854 assert( !db || pRet );
152855 return pRet;
152856}
152857
152858/*
152859** Table *p is a virtual table. This function removes the VTable object
152860** for table *p associated with database connection db from the linked
152861** list in p->pVTab. It also decrements the VTable ref count. This is
152862** used when closing database connection db to free all of its VTable
152863** objects without disturbing the rest of the Schema object (which may
152864** be being used by other shared-cache connections).
152865*/
152866SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
152867 VTable **ppVTab;
152868
152869 assert( IsVirtual(p) );
152870 assert( sqlite3BtreeHoldsAllMutexes(db) );
152871 assert( sqlite3_mutex_held(db->mutex) );
152872
152873 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
152874 if( (*ppVTab)->db==db ){
152875 VTable *pVTab = *ppVTab;
152876 *ppVTab = pVTab->pNext;
152877 sqlite3VtabUnlock(pVTab);
152878 break;
152879 }
152880 }
152881}
152882
152883
152884/*
152885** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
152886**
152887** This function may only be called when the mutexes associated with all
152888** shared b-tree databases opened using connection db are held by the
152889** caller. This is done to protect the sqlite3.pDisconnect list. The
152890** sqlite3.pDisconnect list is accessed only as follows:
152891**
152892** 1) By this function. In this case, all BtShared mutexes and the mutex
152893** associated with the database handle itself must be held.
152894**
152895** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
152896** the sqlite3.pDisconnect list. In this case either the BtShared mutex
152897** associated with the database the virtual table is stored in is held
152898** or, if the virtual table is stored in a non-sharable database, then
152899** the database handle mutex is held.
152900**
152901** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
152902** by multiple threads. It is thread-safe.
152903*/
152904SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
152905 VTable *p = db->pDisconnect;
152906
152907 assert( sqlite3BtreeHoldsAllMutexes(db) );
152908 assert( sqlite3_mutex_held(db->mutex) );
152909
152910 if( p ){
152911 db->pDisconnect = 0;
152912 sqlite3ExpirePreparedStatements(db, iCode: 0);
152913 do {
152914 VTable *pNext = p->pNext;
152915 sqlite3VtabUnlock(pVTab: p);
152916 p = pNext;
152917 }while( p );
152918 }
152919}
152920
152921/*
152922** Clear any and all virtual-table information from the Table record.
152923** This routine is called, for example, just before deleting the Table
152924** record.
152925**
152926** Since it is a virtual-table, the Table structure contains a pointer
152927** to the head of a linked list of VTable structures. Each VTable
152928** structure is associated with a single sqlite3* user of the schema.
152929** The reference count of the VTable structure associated with database
152930** connection db is decremented immediately (which may lead to the
152931** structure being xDisconnected and free). Any other VTable structures
152932** in the list are moved to the sqlite3.pDisconnect list of the associated
152933** database connection.
152934*/
152935SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
152936 assert( IsVirtual(p) );
152937 assert( db!=0 );
152938 if( db->pnBytesFreed==0 ) vtabDisconnectAll(db: 0, p);
152939 if( p->u.vtab.azArg ){
152940 int i;
152941 for(i=0; i<p->u.vtab.nArg; i++){
152942 if( i!=1 ) sqlite3DbFree(db, p: p->u.vtab.azArg[i]);
152943 }
152944 sqlite3DbFree(db, p: p->u.vtab.azArg);
152945 }
152946}
152947
152948/*
152949** Add a new module argument to pTable->u.vtab.azArg[].
152950** The string is not copied - the pointer is stored. The
152951** string will be freed automatically when the table is
152952** deleted.
152953*/
152954static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
152955 sqlite3_int64 nBytes;
152956 char **azModuleArg;
152957 sqlite3 *db = pParse->db;
152958
152959 assert( IsVirtual(pTable) );
152960 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
152961 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
152962 sqlite3ErrorMsg(pParse, zFormat: "too many columns on %s", pTable->zName);
152963 }
152964 azModuleArg = sqlite3DbRealloc(db, p: pTable->u.vtab.azArg, n: nBytes);
152965 if( azModuleArg==0 ){
152966 sqlite3DbFree(db, p: zArg);
152967 }else{
152968 int i = pTable->u.vtab.nArg++;
152969 azModuleArg[i] = zArg;
152970 azModuleArg[i+1] = 0;
152971 pTable->u.vtab.azArg = azModuleArg;
152972 }
152973}
152974
152975/*
152976** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
152977** statement. The module name has been parsed, but the optional list
152978** of parameters that follow the module name are still pending.
152979*/
152980SQLITE_PRIVATE void sqlite3VtabBeginParse(
152981 Parse *pParse, /* Parsing context */
152982 Token *pName1, /* Name of new table, or database name */
152983 Token *pName2, /* Name of new table or NULL */
152984 Token *pModuleName, /* Name of the module for the virtual table */
152985 int ifNotExists /* No error if the table already exists */
152986){
152987 Table *pTable; /* The new virtual table */
152988 sqlite3 *db; /* Database connection */
152989
152990 sqlite3StartTable(pParse, pName1, pName2, isTemp: 0, isView: 0, isVirtual: 1, noErr: ifNotExists);
152991 pTable = pParse->pNewTable;
152992 if( pTable==0 ) return;
152993 assert( 0==pTable->pIndex );
152994 pTable->eTabType = TABTYP_VTAB;
152995
152996 db = pParse->db;
152997
152998 assert( pTable->u.vtab.nArg==0 );
152999 addModuleArgument(pParse, pTable, zArg: sqlite3NameFromToken(db, pName: pModuleName));
153000 addModuleArgument(pParse, pTable, zArg: 0);
153001 addModuleArgument(pParse, pTable, zArg: sqlite3DbStrDup(db, z: pTable->zName));
153002 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
153003 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
153004 );
153005 pParse->sNameToken.n = (int)(
153006 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
153007 );
153008
153009#ifndef SQLITE_OMIT_AUTHORIZATION
153010 /* Creating a virtual table invokes the authorization callback twice.
153011 ** The first invocation, to obtain permission to INSERT a row into the
153012 ** sqlite_schema table, has already been made by sqlite3StartTable().
153013 ** The second call, to obtain permission to create the table, is made now.
153014 */
153015 if( pTable->u.vtab.azArg ){
153016 int iDb = sqlite3SchemaToIndex(db, pSchema: pTable->pSchema);
153017 assert( iDb>=0 ); /* The database the table is being created in */
153018 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, zArg1: pTable->zName,
153019 zArg2: pTable->u.vtab.azArg[0], zArg3: pParse->db->aDb[iDb].zDbSName);
153020 }
153021#endif
153022}
153023
153024/*
153025** This routine takes the module argument that has been accumulating
153026** in pParse->zArg[] and appends it to the list of arguments on the
153027** virtual table currently under construction in pParse->pTable.
153028*/
153029static void addArgumentToVtab(Parse *pParse){
153030 if( pParse->sArg.z && pParse->pNewTable ){
153031 const char *z = (const char*)pParse->sArg.z;
153032 int n = pParse->sArg.n;
153033 sqlite3 *db = pParse->db;
153034 addModuleArgument(pParse, pTable: pParse->pNewTable, zArg: sqlite3DbStrNDup(db, z, n));
153035 }
153036}
153037
153038/*
153039** The parser calls this routine after the CREATE VIRTUAL TABLE statement
153040** has been completely parsed.
153041*/
153042SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
153043 Table *pTab = pParse->pNewTable; /* The table being constructed */
153044 sqlite3 *db = pParse->db; /* The database connection */
153045
153046 if( pTab==0 ) return;
153047 assert( IsVirtual(pTab) );
153048 addArgumentToVtab(pParse);
153049 pParse->sArg.z = 0;
153050 if( pTab->u.vtab.nArg<1 ) return;
153051
153052 /* If the CREATE VIRTUAL TABLE statement is being entered for the
153053 ** first time (in other words if the virtual table is actually being
153054 ** created now instead of just being read out of sqlite_schema) then
153055 ** do additional initialization work and store the statement text
153056 ** in the sqlite_schema table.
153057 */
153058 if( !db->init.busy ){
153059 char *zStmt;
153060 char *zWhere;
153061 int iDb;
153062 int iReg;
153063 Vdbe *v;
153064
153065 sqlite3MayAbort(pParse);
153066
153067 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
153068 if( pEnd ){
153069 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
153070 }
153071 zStmt = sqlite3MPrintf(db, zFormat: "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
153072
153073 /* A slot for the record has already been allocated in the
153074 ** schema table. We just need to update that slot with all
153075 ** the information we've collected.
153076 **
153077 ** The VM register number pParse->regRowid holds the rowid of an
153078 ** entry in the sqlite_schema table that was created for this vtab
153079 ** by sqlite3StartTable().
153080 */
153081 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
153082 sqlite3NestedParse(pParse,
153083 zFormat: "UPDATE %Q." LEGACY_SCHEMA_TABLE " "
153084 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
153085 "WHERE rowid=#%d",
153086 db->aDb[iDb].zDbSName,
153087 pTab->zName,
153088 pTab->zName,
153089 zStmt,
153090 pParse->regRowid
153091 );
153092 v = sqlite3GetVdbe(pParse);
153093 sqlite3ChangeCookie(pParse, iDb);
153094
153095 sqlite3VdbeAddOp0(p: v, OP_Expire);
153096 zWhere = sqlite3MPrintf(db, zFormat: "name=%Q AND sql=%Q", pTab->zName, zStmt);
153097 sqlite3VdbeAddParseSchemaOp(p: v, iDb, zWhere, p5: 0);
153098 sqlite3DbFree(db, p: zStmt);
153099
153100 iReg = ++pParse->nMem;
153101 sqlite3VdbeLoadString(p: v, iDest: iReg, zStr: pTab->zName);
153102 sqlite3VdbeAddOp2(p: v, OP_VCreate, p1: iDb, p2: iReg);
153103 }else{
153104 /* If we are rereading the sqlite_schema table create the in-memory
153105 ** record of the table. */
153106 Table *pOld;
153107 Schema *pSchema = pTab->pSchema;
153108 const char *zName = pTab->zName;
153109 assert( zName!=0 );
153110 sqlite3MarkAllShadowTablesOf(db, pTab);
153111 pOld = sqlite3HashInsert(pH: &pSchema->tblHash, pKey: zName, data: pTab);
153112 if( pOld ){
153113 sqlite3OomFault(db);
153114 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
153115 return;
153116 }
153117 pParse->pNewTable = 0;
153118 }
153119}
153120
153121/*
153122** The parser calls this routine when it sees the first token
153123** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
153124*/
153125SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
153126 addArgumentToVtab(pParse);
153127 pParse->sArg.z = 0;
153128 pParse->sArg.n = 0;
153129}
153130
153131/*
153132** The parser calls this routine for each token after the first token
153133** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
153134*/
153135SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
153136 Token *pArg = &pParse->sArg;
153137 if( pArg->z==0 ){
153138 pArg->z = p->z;
153139 pArg->n = p->n;
153140 }else{
153141 assert(pArg->z <= p->z);
153142 pArg->n = (int)(&p->z[p->n] - pArg->z);
153143 }
153144}
153145
153146/*
153147** Invoke a virtual table constructor (either xCreate or xConnect). The
153148** pointer to the function to invoke is passed as the fourth parameter
153149** to this procedure.
153150*/
153151static int vtabCallConstructor(
153152 sqlite3 *db,
153153 Table *pTab,
153154 Module *pMod,
153155 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
153156 char **pzErr
153157){
153158 VtabCtx sCtx;
153159 VTable *pVTable;
153160 int rc;
153161 const char *const*azArg;
153162 int nArg = pTab->u.vtab.nArg;
153163 char *zErr = 0;
153164 char *zModuleName;
153165 int iDb;
153166 VtabCtx *pCtx;
153167
153168 assert( IsVirtual(pTab) );
153169 azArg = (const char *const*)pTab->u.vtab.azArg;
153170
153171 /* Check that the virtual-table is not already being initialized */
153172 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
153173 if( pCtx->pTab==pTab ){
153174 *pzErr = sqlite3MPrintf(db,
153175 zFormat: "vtable constructor called recursively: %s", pTab->zName
153176 );
153177 return SQLITE_LOCKED;
153178 }
153179 }
153180
153181 zModuleName = sqlite3DbStrDup(db, z: pTab->zName);
153182 if( !zModuleName ){
153183 return SQLITE_NOMEM_BKPT;
153184 }
153185
153186 pVTable = sqlite3MallocZero(n: sizeof(VTable));
153187 if( !pVTable ){
153188 sqlite3OomFault(db);
153189 sqlite3DbFree(db, p: zModuleName);
153190 return SQLITE_NOMEM_BKPT;
153191 }
153192 pVTable->db = db;
153193 pVTable->pMod = pMod;
153194 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
153195
153196 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
153197 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
153198
153199 /* Invoke the virtual table constructor */
153200 assert( &db->pVtabCtx );
153201 assert( xConstruct );
153202 sCtx.pTab = pTab;
153203 sCtx.pVTable = pVTable;
153204 sCtx.pPrior = db->pVtabCtx;
153205 sCtx.bDeclared = 0;
153206 db->pVtabCtx = &sCtx;
153207 pTab->nTabRef++;
153208 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
153209 sqlite3DeleteTable(db, pTable: pTab);
153210 db->pVtabCtx = sCtx.pPrior;
153211 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
153212 assert( sCtx.pTab==pTab );
153213
153214 if( SQLITE_OK!=rc ){
153215 if( zErr==0 ){
153216 *pzErr = sqlite3MPrintf(db, zFormat: "vtable constructor failed: %s", zModuleName);
153217 }else {
153218 *pzErr = sqlite3MPrintf(db, zFormat: "%s", zErr);
153219 sqlite3_free(p: zErr);
153220 }
153221 sqlite3DbFree(db, p: pVTable);
153222 }else if( ALWAYS(pVTable->pVtab) ){
153223 /* Justification of ALWAYS(): A correct vtab constructor must allocate
153224 ** the sqlite3_vtab object if successful. */
153225 memset(s: pVTable->pVtab, c: 0, n: sizeof(pVTable->pVtab[0]));
153226 pVTable->pVtab->pModule = pMod->pModule;
153227 pMod->nRefModule++;
153228 pVTable->nRef = 1;
153229 if( sCtx.bDeclared==0 ){
153230 const char *zFormat = "vtable constructor did not declare schema: %s";
153231 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
153232 sqlite3VtabUnlock(pVTab: pVTable);
153233 rc = SQLITE_ERROR;
153234 }else{
153235 int iCol;
153236 u16 oooHidden = 0;
153237 /* If everything went according to plan, link the new VTable structure
153238 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
153239 ** columns of the table to see if any of them contain the token "hidden".
153240 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
153241 ** the type string. */
153242 pVTable->pNext = pTab->u.vtab.p;
153243 pTab->u.vtab.p = pVTable;
153244
153245 for(iCol=0; iCol<pTab->nCol; iCol++){
153246 char *zType = sqlite3ColumnType(pCol: &pTab->aCol[iCol], zDflt: "");
153247 int nType;
153248 int i = 0;
153249 nType = sqlite3Strlen30(z: zType);
153250 for(i=0; i<nType; i++){
153251 if( 0==sqlite3StrNICmp(zLeft: "hidden", zRight: &zType[i], N: 6)
153252 && (i==0 || zType[i-1]==' ')
153253 && (zType[i+6]=='\0' || zType[i+6]==' ')
153254 ){
153255 break;
153256 }
153257 }
153258 if( i<nType ){
153259 int j;
153260 int nDel = 6 + (zType[i+6] ? 1 : 0);
153261 for(j=i; (j+nDel)<=nType; j++){
153262 zType[j] = zType[j+nDel];
153263 }
153264 if( zType[i]=='\0' && i>0 ){
153265 assert(zType[i-1]==' ');
153266 zType[i-1] = '\0';
153267 }
153268 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
153269 pTab->tabFlags |= TF_HasHidden;
153270 oooHidden = TF_OOOHidden;
153271 }else{
153272 pTab->tabFlags |= oooHidden;
153273 }
153274 }
153275 }
153276 }
153277
153278 sqlite3DbFree(db, p: zModuleName);
153279 return rc;
153280}
153281
153282/*
153283** This function is invoked by the parser to call the xConnect() method
153284** of the virtual table pTab. If an error occurs, an error code is returned
153285** and an error left in pParse.
153286**
153287** This call is a no-op if table pTab is not a virtual table.
153288*/
153289SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
153290 sqlite3 *db = pParse->db;
153291 const char *zMod;
153292 Module *pMod;
153293 int rc;
153294
153295 assert( pTab );
153296 assert( IsVirtual(pTab) );
153297 if( sqlite3GetVTable(db, pTab) ){
153298 return SQLITE_OK;
153299 }
153300
153301 /* Locate the required virtual table module */
153302 zMod = pTab->u.vtab.azArg[0];
153303 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: zMod);
153304
153305 if( !pMod ){
153306 const char *zModule = pTab->u.vtab.azArg[0];
153307 sqlite3ErrorMsg(pParse, zFormat: "no such module: %s", zModule);
153308 rc = SQLITE_ERROR;
153309 }else{
153310 char *zErr = 0;
153311 rc = vtabCallConstructor(db, pTab, pMod, xConstruct: pMod->pModule->xConnect, pzErr: &zErr);
153312 if( rc!=SQLITE_OK ){
153313 sqlite3ErrorMsg(pParse, zFormat: "%s", zErr);
153314 pParse->rc = rc;
153315 }
153316 sqlite3DbFree(db, p: zErr);
153317 }
153318
153319 return rc;
153320}
153321/*
153322** Grow the db->aVTrans[] array so that there is room for at least one
153323** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
153324*/
153325static int growVTrans(sqlite3 *db){
153326 const int ARRAY_INCR = 5;
153327
153328 /* Grow the sqlite3.aVTrans array if required */
153329 if( (db->nVTrans%ARRAY_INCR)==0 ){
153330 VTable **aVTrans;
153331 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
153332 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
153333 aVTrans = sqlite3DbRealloc(db, p: (void *)db->aVTrans, n: nBytes);
153334 if( !aVTrans ){
153335 return SQLITE_NOMEM_BKPT;
153336 }
153337 memset(s: &aVTrans[db->nVTrans], c: 0, n: sizeof(sqlite3_vtab *)*ARRAY_INCR);
153338 db->aVTrans = aVTrans;
153339 }
153340
153341 return SQLITE_OK;
153342}
153343
153344/*
153345** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
153346** have already been reserved using growVTrans().
153347*/
153348static void addToVTrans(sqlite3 *db, VTable *pVTab){
153349 /* Add pVtab to the end of sqlite3.aVTrans */
153350 db->aVTrans[db->nVTrans++] = pVTab;
153351 sqlite3VtabLock(pVTab);
153352}
153353
153354/*
153355** This function is invoked by the vdbe to call the xCreate method
153356** of the virtual table named zTab in database iDb.
153357**
153358** If an error occurs, *pzErr is set to point to an English language
153359** description of the error and an SQLITE_XXX error code is returned.
153360** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
153361*/
153362SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
153363 int rc = SQLITE_OK;
153364 Table *pTab;
153365 Module *pMod;
153366 const char *zMod;
153367
153368 pTab = sqlite3FindTable(db, zName: zTab, zDatabase: db->aDb[iDb].zDbSName);
153369 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
153370
153371 /* Locate the required virtual table module */
153372 zMod = pTab->u.vtab.azArg[0];
153373 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: zMod);
153374
153375 /* If the module has been registered and includes a Create method,
153376 ** invoke it now. If the module has not been registered, return an
153377 ** error. Otherwise, do nothing.
153378 */
153379 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
153380 *pzErr = sqlite3MPrintf(db, zFormat: "no such module: %s", zMod);
153381 rc = SQLITE_ERROR;
153382 }else{
153383 rc = vtabCallConstructor(db, pTab, pMod, xConstruct: pMod->pModule->xCreate, pzErr);
153384 }
153385
153386 /* Justification of ALWAYS(): The xConstructor method is required to
153387 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
153388 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
153389 rc = growVTrans(db);
153390 if( rc==SQLITE_OK ){
153391 addToVTrans(db, pVTab: sqlite3GetVTable(db, pTab));
153392 }
153393 }
153394
153395 return rc;
153396}
153397
153398/*
153399** This function is used to set the schema of a virtual table. It is only
153400** valid to call this function from within the xCreate() or xConnect() of a
153401** virtual table module.
153402*/
153403SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
153404 VtabCtx *pCtx;
153405 int rc = SQLITE_OK;
153406 Table *pTab;
153407 Parse sParse;
153408 int initBusy;
153409
153410#ifdef SQLITE_ENABLE_API_ARMOR
153411 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
153412 return SQLITE_MISUSE_BKPT;
153413 }
153414#endif
153415 sqlite3_mutex_enter(p: db->mutex);
153416 pCtx = db->pVtabCtx;
153417 if( !pCtx || pCtx->bDeclared ){
153418 sqlite3Error(db, SQLITE_MISUSE);
153419 sqlite3_mutex_leave(p: db->mutex);
153420 return SQLITE_MISUSE_BKPT;
153421 }
153422 pTab = pCtx->pTab;
153423 assert( IsVirtual(pTab) );
153424
153425 sqlite3ParseObjectInit(pParse: &sParse, db);
153426 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
153427 sParse.disableTriggers = 1;
153428 /* We should never be able to reach this point while loading the
153429 ** schema. Nevertheless, defend against that (turn off db->init.busy)
153430 ** in case a bug arises. */
153431 assert( db->init.busy==0 );
153432 initBusy = db->init.busy;
153433 db->init.busy = 0;
153434 sParse.nQueryLoop = 1;
153435 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable)
153436 && ALWAYS(sParse.pNewTable!=0)
153437 && ALWAYS(!db->mallocFailed)
153438 && IsOrdinaryTable(sParse.pNewTable)
153439 ){
153440 assert( sParse.zErrMsg==0 );
153441 if( !pTab->aCol ){
153442 Table *pNew = sParse.pNewTable;
153443 Index *pIdx;
153444 pTab->aCol = pNew->aCol;
153445 sqlite3ExprListDelete(db, pList: pNew->u.tab.pDfltList);
153446 pTab->nNVCol = pTab->nCol = pNew->nCol;
153447 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
153448 pNew->nCol = 0;
153449 pNew->aCol = 0;
153450 assert( pTab->pIndex==0 );
153451 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
153452 if( !HasRowid(pNew)
153453 && pCtx->pVTable->pMod->pModule->xUpdate!=0
153454 && sqlite3PrimaryKeyIndex(pTab: pNew)->nKeyCol!=1
153455 ){
153456 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
153457 ** or else must have a single-column PRIMARY KEY */
153458 rc = SQLITE_ERROR;
153459 }
153460 pIdx = pNew->pIndex;
153461 if( pIdx ){
153462 assert( pIdx->pNext==0 );
153463 pTab->pIndex = pIdx;
153464 pNew->pIndex = 0;
153465 pIdx->pTable = pTab;
153466 }
153467 }
153468 pCtx->bDeclared = 1;
153469 }else{
153470 sqlite3ErrorWithMsg(db, SQLITE_ERROR,
153471 zFormat: (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg);
153472 sqlite3DbFree(db, p: sParse.zErrMsg);
153473 rc = SQLITE_ERROR;
153474 }
153475 sParse.eParseMode = PARSE_MODE_NORMAL;
153476
153477 if( sParse.pVdbe ){
153478 sqlite3VdbeFinalize(p: sParse.pVdbe);
153479 }
153480 sqlite3DeleteTable(db, pTable: sParse.pNewTable);
153481 sqlite3ParseObjectReset(pParse: &sParse);
153482 db->init.busy = initBusy;
153483
153484 assert( (rc&0xff)==rc );
153485 rc = sqlite3ApiExit(db, rc);
153486 sqlite3_mutex_leave(p: db->mutex);
153487 return rc;
153488}
153489
153490/*
153491** This function is invoked by the vdbe to call the xDestroy method
153492** of the virtual table named zTab in database iDb. This occurs
153493** when a DROP TABLE is mentioned.
153494**
153495** This call is a no-op if zTab is not a virtual table.
153496*/
153497SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
153498 int rc = SQLITE_OK;
153499 Table *pTab;
153500
153501 pTab = sqlite3FindTable(db, zName: zTab, zDatabase: db->aDb[iDb].zDbSName);
153502 if( ALWAYS(pTab!=0)
153503 && ALWAYS(IsVirtual(pTab))
153504 && ALWAYS(pTab->u.vtab.p!=0)
153505 ){
153506 VTable *p;
153507 int (*xDestroy)(sqlite3_vtab *);
153508 for(p=pTab->u.vtab.p; p; p=p->pNext){
153509 assert( p->pVtab );
153510 if( p->pVtab->nRef>0 ){
153511 return SQLITE_LOCKED;
153512 }
153513 }
153514 p = vtabDisconnectAll(db, p: pTab);
153515 xDestroy = p->pMod->pModule->xDestroy;
153516 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
153517 assert( xDestroy!=0 );
153518 pTab->nTabRef++;
153519 rc = xDestroy(p->pVtab);
153520 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
153521 if( rc==SQLITE_OK ){
153522 assert( pTab->u.vtab.p==p && p->pNext==0 );
153523 p->pVtab = 0;
153524 pTab->u.vtab.p = 0;
153525 sqlite3VtabUnlock(pVTab: p);
153526 }
153527 sqlite3DeleteTable(db, pTable: pTab);
153528 }
153529
153530 return rc;
153531}
153532
153533/*
153534** This function invokes either the xRollback or xCommit method
153535** of each of the virtual tables in the sqlite3.aVTrans array. The method
153536** called is identified by the second argument, "offset", which is
153537** the offset of the method to call in the sqlite3_module structure.
153538**
153539** The array is cleared after invoking the callbacks.
153540*/
153541static void callFinaliser(sqlite3 *db, int offset){
153542 int i;
153543 if( db->aVTrans ){
153544 VTable **aVTrans = db->aVTrans;
153545 db->aVTrans = 0;
153546 for(i=0; i<db->nVTrans; i++){
153547 VTable *pVTab = aVTrans[i];
153548 sqlite3_vtab *p = pVTab->pVtab;
153549 if( p ){
153550 int (*x)(sqlite3_vtab *);
153551 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
153552 if( x ) x(p);
153553 }
153554 pVTab->iSavepoint = 0;
153555 sqlite3VtabUnlock(pVTab);
153556 }
153557 sqlite3DbFree(db, p: aVTrans);
153558 db->nVTrans = 0;
153559 }
153560}
153561
153562/*
153563** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
153564** array. Return the error code for the first error that occurs, or
153565** SQLITE_OK if all xSync operations are successful.
153566**
153567** If an error message is available, leave it in p->zErrMsg.
153568*/
153569SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
153570 int i;
153571 int rc = SQLITE_OK;
153572 VTable **aVTrans = db->aVTrans;
153573
153574 db->aVTrans = 0;
153575 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
153576 int (*x)(sqlite3_vtab *);
153577 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
153578 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
153579 rc = x(pVtab);
153580 sqlite3VtabImportErrmsg(p, pVtab);
153581 }
153582 }
153583 db->aVTrans = aVTrans;
153584 return rc;
153585}
153586
153587/*
153588** Invoke the xRollback method of all virtual tables in the
153589** sqlite3.aVTrans array. Then clear the array itself.
153590*/
153591SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
153592 callFinaliser(db, offsetof(sqlite3_module,xRollback));
153593 return SQLITE_OK;
153594}
153595
153596/*
153597** Invoke the xCommit method of all virtual tables in the
153598** sqlite3.aVTrans array. Then clear the array itself.
153599*/
153600SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
153601 callFinaliser(db, offsetof(sqlite3_module,xCommit));
153602 return SQLITE_OK;
153603}
153604
153605/*
153606** If the virtual table pVtab supports the transaction interface
153607** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
153608** not currently open, invoke the xBegin method now.
153609**
153610** If the xBegin call is successful, place the sqlite3_vtab pointer
153611** in the sqlite3.aVTrans array.
153612*/
153613SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
153614 int rc = SQLITE_OK;
153615 const sqlite3_module *pModule;
153616
153617 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
153618 ** than zero, then this function is being called from within a
153619 ** virtual module xSync() callback. It is illegal to write to
153620 ** virtual module tables in this case, so return SQLITE_LOCKED.
153621 */
153622 if( sqlite3VtabInSync(db) ){
153623 return SQLITE_LOCKED;
153624 }
153625 if( !pVTab ){
153626 return SQLITE_OK;
153627 }
153628 pModule = pVTab->pVtab->pModule;
153629
153630 if( pModule->xBegin ){
153631 int i;
153632
153633 /* If pVtab is already in the aVTrans array, return early */
153634 for(i=0; i<db->nVTrans; i++){
153635 if( db->aVTrans[i]==pVTab ){
153636 return SQLITE_OK;
153637 }
153638 }
153639
153640 /* Invoke the xBegin method. If successful, add the vtab to the
153641 ** sqlite3.aVTrans[] array. */
153642 rc = growVTrans(db);
153643 if( rc==SQLITE_OK ){
153644 rc = pModule->xBegin(pVTab->pVtab);
153645 if( rc==SQLITE_OK ){
153646 int iSvpt = db->nStatement + db->nSavepoint;
153647 addToVTrans(db, pVTab);
153648 if( iSvpt && pModule->xSavepoint ){
153649 pVTab->iSavepoint = iSvpt;
153650 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
153651 }
153652 }
153653 }
153654 }
153655 return rc;
153656}
153657
153658/*
153659** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
153660** virtual tables that currently have an open transaction. Pass iSavepoint
153661** as the second argument to the virtual table method invoked.
153662**
153663** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
153664** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
153665** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
153666** an open transaction is invoked.
153667**
153668** If any virtual table method returns an error code other than SQLITE_OK,
153669** processing is abandoned and the error returned to the caller of this
153670** function immediately. If all calls to virtual table methods are successful,
153671** SQLITE_OK is returned.
153672*/
153673SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
153674 int rc = SQLITE_OK;
153675
153676 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
153677 assert( iSavepoint>=-1 );
153678 if( db->aVTrans ){
153679 int i;
153680 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
153681 VTable *pVTab = db->aVTrans[i];
153682 const sqlite3_module *pMod = pVTab->pMod->pModule;
153683 if( pVTab->pVtab && pMod->iVersion>=2 ){
153684 int (*xMethod)(sqlite3_vtab *, int);
153685 sqlite3VtabLock(pVTab);
153686 switch( op ){
153687 case SAVEPOINT_BEGIN:
153688 xMethod = pMod->xSavepoint;
153689 pVTab->iSavepoint = iSavepoint+1;
153690 break;
153691 case SAVEPOINT_ROLLBACK:
153692 xMethod = pMod->xRollbackTo;
153693 break;
153694 default:
153695 xMethod = pMod->xRelease;
153696 break;
153697 }
153698 if( xMethod && pVTab->iSavepoint>iSavepoint ){
153699 u64 savedFlags = (db->flags & SQLITE_Defensive);
153700 db->flags &= ~(u64)SQLITE_Defensive;
153701 rc = xMethod(pVTab->pVtab, iSavepoint);
153702 db->flags |= savedFlags;
153703 }
153704 sqlite3VtabUnlock(pVTab);
153705 }
153706 }
153707 }
153708 return rc;
153709}
153710
153711/*
153712** The first parameter (pDef) is a function implementation. The
153713** second parameter (pExpr) is the first argument to this function.
153714** If pExpr is a column in a virtual table, then let the virtual
153715** table implementation have an opportunity to overload the function.
153716**
153717** This routine is used to allow virtual table implementations to
153718** overload MATCH, LIKE, GLOB, and REGEXP operators.
153719**
153720** Return either the pDef argument (indicating no change) or a
153721** new FuncDef structure that is marked as ephemeral using the
153722** SQLITE_FUNC_EPHEM flag.
153723*/
153724SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
153725 sqlite3 *db, /* Database connection for reporting malloc problems */
153726 FuncDef *pDef, /* Function to possibly overload */
153727 int nArg, /* Number of arguments to the function */
153728 Expr *pExpr /* First argument to the function */
153729){
153730 Table *pTab;
153731 sqlite3_vtab *pVtab;
153732 sqlite3_module *pMod;
153733 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
153734 void *pArg = 0;
153735 FuncDef *pNew;
153736 int rc = 0;
153737
153738 /* Check to see the left operand is a column in a virtual table */
153739 if( NEVER(pExpr==0) ) return pDef;
153740 if( pExpr->op!=TK_COLUMN ) return pDef;
153741 assert( ExprUseYTab(pExpr) );
153742 pTab = pExpr->y.pTab;
153743 if( NEVER(pTab==0) ) return pDef;
153744 if( !IsVirtual(pTab) ) return pDef;
153745 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
153746 assert( pVtab!=0 );
153747 assert( pVtab->pModule!=0 );
153748 pMod = (sqlite3_module *)pVtab->pModule;
153749 if( pMod->xFindFunction==0 ) return pDef;
153750
153751 /* Call the xFindFunction method on the virtual table implementation
153752 ** to see if the implementation wants to overload this function.
153753 **
153754 ** Though undocumented, we have historically always invoked xFindFunction
153755 ** with an all lower-case function name. Continue in this tradition to
153756 ** avoid any chance of an incompatibility.
153757 */
153758#ifdef SQLITE_DEBUG
153759 {
153760 int i;
153761 for(i=0; pDef->zName[i]; i++){
153762 unsigned char x = (unsigned char)pDef->zName[i];
153763 assert( x==sqlite3UpperToLower[x] );
153764 }
153765 }
153766#endif
153767 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
153768 if( rc==0 ){
153769 return pDef;
153770 }
153771
153772 /* Create a new ephemeral function definition for the overloaded
153773 ** function */
153774 pNew = sqlite3DbMallocZero(db, n: sizeof(*pNew)
153775 + sqlite3Strlen30(z: pDef->zName) + 1);
153776 if( pNew==0 ){
153777 return pDef;
153778 }
153779 *pNew = *pDef;
153780 pNew->zName = (const char*)&pNew[1];
153781 memcpy(dest: (char*)&pNew[1], src: pDef->zName, n: sqlite3Strlen30(z: pDef->zName)+1);
153782 pNew->xSFunc = xSFunc;
153783 pNew->pUserData = pArg;
153784 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
153785 return pNew;
153786}
153787
153788/*
153789** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
153790** array so that an OP_VBegin will get generated for it. Add pTab to the
153791** array if it is missing. If pTab is already in the array, this routine
153792** is a no-op.
153793*/
153794SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
153795 Parse *pToplevel = sqlite3ParseToplevel(pParse);
153796 int i, n;
153797 Table **apVtabLock;
153798
153799 assert( IsVirtual(pTab) );
153800 for(i=0; i<pToplevel->nVtabLock; i++){
153801 if( pTab==pToplevel->apVtabLock[i] ) return;
153802 }
153803 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
153804 apVtabLock = sqlite3Realloc(pOld: pToplevel->apVtabLock, nBytes: n);
153805 if( apVtabLock ){
153806 pToplevel->apVtabLock = apVtabLock;
153807 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
153808 }else{
153809 sqlite3OomFault(db: pToplevel->db);
153810 }
153811}
153812
153813/*
153814** Check to see if virtual table module pMod can be have an eponymous
153815** virtual table instance. If it can, create one if one does not already
153816** exist. Return non-zero if either the eponymous virtual table instance
153817** exists when this routine returns or if an attempt to create it failed
153818** and an error message was left in pParse.
153819**
153820** An eponymous virtual table instance is one that is named after its
153821** module, and more importantly, does not require a CREATE VIRTUAL TABLE
153822** statement in order to come into existence. Eponymous virtual table
153823** instances always exist. They cannot be DROP-ed.
153824**
153825** Any virtual table module for which xConnect and xCreate are the same
153826** method can have an eponymous virtual table instance.
153827*/
153828SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
153829 const sqlite3_module *pModule = pMod->pModule;
153830 Table *pTab;
153831 char *zErr = 0;
153832 int rc;
153833 sqlite3 *db = pParse->db;
153834 if( pMod->pEpoTab ) return 1;
153835 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
153836 pTab = sqlite3DbMallocZero(db, n: sizeof(Table));
153837 if( pTab==0 ) return 0;
153838 pTab->zName = sqlite3DbStrDup(db, z: pMod->zName);
153839 if( pTab->zName==0 ){
153840 sqlite3DbFree(db, p: pTab);
153841 return 0;
153842 }
153843 pMod->pEpoTab = pTab;
153844 pTab->nTabRef = 1;
153845 pTab->eTabType = TABTYP_VTAB;
153846 pTab->pSchema = db->aDb[0].pSchema;
153847 assert( pTab->u.vtab.nArg==0 );
153848 pTab->iPKey = -1;
153849 pTab->tabFlags |= TF_Eponymous;
153850 addModuleArgument(pParse, pTable: pTab, zArg: sqlite3DbStrDup(db, z: pTab->zName));
153851 addModuleArgument(pParse, pTable: pTab, zArg: 0);
153852 addModuleArgument(pParse, pTable: pTab, zArg: sqlite3DbStrDup(db, z: pTab->zName));
153853 rc = vtabCallConstructor(db, pTab, pMod, xConstruct: pModule->xConnect, pzErr: &zErr);
153854 if( rc ){
153855 sqlite3ErrorMsg(pParse, zFormat: "%s", zErr);
153856 sqlite3DbFree(db, p: zErr);
153857 sqlite3VtabEponymousTableClear(db, pMod);
153858 }
153859 return 1;
153860}
153861
153862/*
153863** Erase the eponymous virtual table instance associated with
153864** virtual table module pMod, if it exists.
153865*/
153866SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
153867 Table *pTab = pMod->pEpoTab;
153868 if( pTab!=0 ){
153869 /* Mark the table as Ephemeral prior to deleting it, so that the
153870 ** sqlite3DeleteTable() routine will know that it is not stored in
153871 ** the schema. */
153872 pTab->tabFlags |= TF_Ephemeral;
153873 sqlite3DeleteTable(db, pTable: pTab);
153874 pMod->pEpoTab = 0;
153875 }
153876}
153877
153878/*
153879** Return the ON CONFLICT resolution mode in effect for the virtual
153880** table update operation currently in progress.
153881**
153882** The results of this routine are undefined unless it is called from
153883** within an xUpdate method.
153884*/
153885SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
153886 static const unsigned char aMap[] = {
153887 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
153888 };
153889#ifdef SQLITE_ENABLE_API_ARMOR
153890 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153891#endif
153892 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
153893 assert( OE_Ignore==4 && OE_Replace==5 );
153894 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
153895 return (int)aMap[db->vtabOnConflict-1];
153896}
153897
153898/*
153899** Call from within the xCreate() or xConnect() methods to provide
153900** the SQLite core with additional information about the behavior
153901** of the virtual table being implemented.
153902*/
153903SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
153904 va_list ap;
153905 int rc = SQLITE_OK;
153906 VtabCtx *p;
153907
153908#ifdef SQLITE_ENABLE_API_ARMOR
153909 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153910#endif
153911 sqlite3_mutex_enter(p: db->mutex);
153912 p = db->pVtabCtx;
153913 if( !p ){
153914 rc = SQLITE_MISUSE_BKPT;
153915 }else{
153916 assert( p->pTab==0 || IsVirtual(p->pTab) );
153917 va_start(ap, op);
153918 switch( op ){
153919 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
153920 p->pVTable->bConstraint = (u8)va_arg(ap, int);
153921 break;
153922 }
153923 case SQLITE_VTAB_INNOCUOUS: {
153924 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
153925 break;
153926 }
153927 case SQLITE_VTAB_DIRECTONLY: {
153928 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
153929 break;
153930 }
153931 case SQLITE_VTAB_USES_ALL_SCHEMAS: {
153932 p->pVTable->bAllSchemas = 1;
153933 break;
153934 }
153935 default: {
153936 rc = SQLITE_MISUSE_BKPT;
153937 break;
153938 }
153939 }
153940 va_end(ap);
153941 }
153942
153943 if( rc!=SQLITE_OK ) sqlite3Error(db, err_code: rc);
153944 sqlite3_mutex_leave(p: db->mutex);
153945 return rc;
153946}
153947
153948#endif /* SQLITE_OMIT_VIRTUALTABLE */
153949
153950/************** End of vtab.c ************************************************/
153951/************** Begin file wherecode.c ***************************************/
153952/*
153953** 2015-06-06
153954**
153955** The author disclaims copyright to this source code. In place of
153956** a legal notice, here is a blessing:
153957**
153958** May you do good and not evil.
153959** May you find forgiveness for yourself and forgive others.
153960** May you share freely, never taking more than you give.
153961**
153962*************************************************************************
153963** This module contains C code that generates VDBE code used to process
153964** the WHERE clause of SQL statements.
153965**
153966** This file was split off from where.c on 2015-06-06 in order to reduce the
153967** size of where.c and make it easier to edit. This file contains the routines
153968** that actually generate the bulk of the WHERE loop code. The original where.c
153969** file retains the code that does query planning and analysis.
153970*/
153971/* #include "sqliteInt.h" */
153972/************** Include whereInt.h in the middle of wherecode.c **************/
153973/************** Begin file whereInt.h ****************************************/
153974/*
153975** 2013-11-12
153976**
153977** The author disclaims copyright to this source code. In place of
153978** a legal notice, here is a blessing:
153979**
153980** May you do good and not evil.
153981** May you find forgiveness for yourself and forgive others.
153982** May you share freely, never taking more than you give.
153983**
153984*************************************************************************
153985**
153986** This file contains structure and macro definitions for the query
153987** planner logic in "where.c". These definitions are broken out into
153988** a separate source file for easier editing.
153989*/
153990#ifndef SQLITE_WHEREINT_H
153991#define SQLITE_WHEREINT_H
153992
153993
153994/* Forward references
153995*/
153996typedef struct WhereClause WhereClause;
153997typedef struct WhereMaskSet WhereMaskSet;
153998typedef struct WhereOrInfo WhereOrInfo;
153999typedef struct WhereAndInfo WhereAndInfo;
154000typedef struct WhereLevel WhereLevel;
154001typedef struct WhereLoop WhereLoop;
154002typedef struct WherePath WherePath;
154003typedef struct WhereTerm WhereTerm;
154004typedef struct WhereLoopBuilder WhereLoopBuilder;
154005typedef struct WhereScan WhereScan;
154006typedef struct WhereOrCost WhereOrCost;
154007typedef struct WhereOrSet WhereOrSet;
154008typedef struct WhereMemBlock WhereMemBlock;
154009typedef struct WhereRightJoin WhereRightJoin;
154010
154011/*
154012** This object is a header on a block of allocated memory that will be
154013** automatically freed when its WInfo object is destructed.
154014*/
154015struct WhereMemBlock {
154016 WhereMemBlock *pNext; /* Next block in the chain */
154017 u64 sz; /* Bytes of space */
154018};
154019
154020/*
154021** Extra information attached to a WhereLevel that is a RIGHT JOIN.
154022*/
154023struct WhereRightJoin {
154024 int iMatch; /* Cursor used to determine prior matched rows */
154025 int regBloom; /* Bloom filter for iRJMatch */
154026 int regReturn; /* Return register for the interior subroutine */
154027 int addrSubrtn; /* Starting address for the interior subroutine */
154028 int endSubrtn; /* The last opcode in the interior subroutine */
154029};
154030
154031/*
154032** This object contains information needed to implement a single nested
154033** loop in WHERE clause.
154034**
154035** Contrast this object with WhereLoop. This object describes the
154036** implementation of the loop. WhereLoop describes the algorithm.
154037** This object contains a pointer to the WhereLoop algorithm as one of
154038** its elements.
154039**
154040** The WhereInfo object contains a single instance of this object for
154041** each term in the FROM clause (which is to say, for each of the
154042** nested loops as implemented). The order of WhereLevel objects determines
154043** the loop nested order, with WhereInfo.a[0] being the outer loop and
154044** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
154045*/
154046struct WhereLevel {
154047 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
154048 int iTabCur; /* The VDBE cursor used to access the table */
154049 int iIdxCur; /* The VDBE cursor used to access pIdx */
154050 int addrBrk; /* Jump here to break out of the loop */
154051 int addrNxt; /* Jump here to start the next IN combination */
154052 int addrSkip; /* Jump here for next iteration of skip-scan */
154053 int addrCont; /* Jump here to continue with the next loop cycle */
154054 int addrFirst; /* First instruction of interior of the loop */
154055 int addrBody; /* Beginning of the body of this loop */
154056 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
154057 int addrBignull; /* Jump here for next part of big-null scan */
154058#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
154059 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
154060 int addrLikeRep; /* LIKE range processing address */
154061#endif
154062 int regFilter; /* Bloom filter */
154063 WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */
154064 u8 iFrom; /* Which entry in the FROM clause */
154065 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
154066 int p1, p2; /* Operands of the opcode used to end the loop */
154067 union { /* Information that depends on pWLoop->wsFlags */
154068 struct {
154069 int nIn; /* Number of entries in aInLoop[] */
154070 struct InLoop {
154071 int iCur; /* The VDBE cursor used by this IN operator */
154072 int addrInTop; /* Top of the IN loop */
154073 int iBase; /* Base register of multi-key index record */
154074 int nPrefix; /* Number of prior entries in the key */
154075 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
154076 } *aInLoop; /* Information about each nested IN operator */
154077 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
154078 Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */
154079 } u;
154080 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
154081 Bitmask notReady; /* FROM entries not usable at this level */
154082#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
154083 int addrVisit; /* Address at which row is visited */
154084#endif
154085};
154086
154087/*
154088** Each instance of this object represents an algorithm for evaluating one
154089** term of a join. Every term of the FROM clause will have at least
154090** one corresponding WhereLoop object (unless INDEXED BY constraints
154091** prevent a query solution - which is an error) and many terms of the
154092** FROM clause will have multiple WhereLoop objects, each describing a
154093** potential way of implementing that FROM-clause term, together with
154094** dependencies and cost estimates for using the chosen algorithm.
154095**
154096** Query planning consists of building up a collection of these WhereLoop
154097** objects, then computing a particular sequence of WhereLoop objects, with
154098** one WhereLoop object per FROM clause term, that satisfy all dependencies
154099** and that minimize the overall cost.
154100*/
154101struct WhereLoop {
154102 Bitmask prereq; /* Bitmask of other loops that must run first */
154103 Bitmask maskSelf; /* Bitmask identifying table iTab */
154104#ifdef SQLITE_DEBUG
154105 char cId; /* Symbolic ID of this loop for debugging use */
154106#endif
154107 u8 iTab; /* Position in FROM clause of table for this loop */
154108 u8 iSortIdx; /* Sorting index number. 0==None */
154109 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
154110 LogEst rRun; /* Cost of running each loop */
154111 LogEst nOut; /* Estimated number of output rows */
154112 union {
154113 struct { /* Information for internal btree tables */
154114 u16 nEq; /* Number of equality constraints */
154115 u16 nBtm; /* Size of BTM vector */
154116 u16 nTop; /* Size of TOP vector */
154117 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
154118 Index *pIndex; /* Index used, or NULL */
154119 } btree;
154120 struct { /* Information for virtual tables */
154121 int idxNum; /* Index number */
154122 u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
154123 u32 bOmitOffset : 1; /* True to let virtual table handle offset */
154124 i8 isOrdered; /* True if satisfies ORDER BY */
154125 u16 omitMask; /* Terms that may be omitted */
154126 char *idxStr; /* Index identifier string */
154127 u32 mHandleIn; /* Terms to handle as IN(...) instead of == */
154128 } vtab;
154129 } u;
154130 u32 wsFlags; /* WHERE_* flags describing the plan */
154131 u16 nLTerm; /* Number of entries in aLTerm[] */
154132 u16 nSkip; /* Number of NULL aLTerm[] entries */
154133 /**** whereLoopXfer() copies fields above ***********************/
154134# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
154135 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
154136 WhereTerm **aLTerm; /* WhereTerms used */
154137 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
154138 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
154139};
154140
154141/* This object holds the prerequisites and the cost of running a
154142** subquery on one operand of an OR operator in the WHERE clause.
154143** See WhereOrSet for additional information
154144*/
154145struct WhereOrCost {
154146 Bitmask prereq; /* Prerequisites */
154147 LogEst rRun; /* Cost of running this subquery */
154148 LogEst nOut; /* Number of outputs for this subquery */
154149};
154150
154151/* The WhereOrSet object holds a set of possible WhereOrCosts that
154152** correspond to the subquery(s) of OR-clause processing. Only the
154153** best N_OR_COST elements are retained.
154154*/
154155#define N_OR_COST 3
154156struct WhereOrSet {
154157 u16 n; /* Number of valid a[] entries */
154158 WhereOrCost a[N_OR_COST]; /* Set of best costs */
154159};
154160
154161/*
154162** Each instance of this object holds a sequence of WhereLoop objects
154163** that implement some or all of a query plan.
154164**
154165** Think of each WhereLoop object as a node in a graph with arcs
154166** showing dependencies and costs for travelling between nodes. (That is
154167** not a completely accurate description because WhereLoop costs are a
154168** vector, not a scalar, and because dependencies are many-to-one, not
154169** one-to-one as are graph nodes. But it is a useful visualization aid.)
154170** Then a WherePath object is a path through the graph that visits some
154171** or all of the WhereLoop objects once.
154172**
154173** The "solver" works by creating the N best WherePath objects of length
154174** 1. Then using those as a basis to compute the N best WherePath objects
154175** of length 2. And so forth until the length of WherePaths equals the
154176** number of nodes in the FROM clause. The best (lowest cost) WherePath
154177** at the end is the chosen query plan.
154178*/
154179struct WherePath {
154180 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
154181 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
154182 LogEst nRow; /* Estimated number of rows generated by this path */
154183 LogEst rCost; /* Total cost of this path */
154184 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
154185 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
154186 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
154187};
154188
154189/*
154190** The query generator uses an array of instances of this structure to
154191** help it analyze the subexpressions of the WHERE clause. Each WHERE
154192** clause subexpression is separated from the others by AND operators,
154193** usually, or sometimes subexpressions separated by OR.
154194**
154195** All WhereTerms are collected into a single WhereClause structure.
154196** The following identity holds:
154197**
154198** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
154199**
154200** When a term is of the form:
154201**
154202** X <op> <expr>
154203**
154204** where X is a column name and <op> is one of certain operators,
154205** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
154206** cursor number and column number for X. WhereTerm.eOperator records
154207** the <op> using a bitmask encoding defined by WO_xxx below. The
154208** use of a bitmask encoding for the operator allows us to search
154209** quickly for terms that match any of several different operators.
154210**
154211** A WhereTerm might also be two or more subterms connected by OR:
154212**
154213** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
154214**
154215** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
154216** and the WhereTerm.u.pOrInfo field points to auxiliary information that
154217** is collected about the OR clause.
154218**
154219** If a term in the WHERE clause does not match either of the two previous
154220** categories, then eOperator==0. The WhereTerm.pExpr field is still set
154221** to the original subexpression content and wtFlags is set up appropriately
154222** but no other fields in the WhereTerm object are meaningful.
154223**
154224** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
154225** but they do so indirectly. A single WhereMaskSet structure translates
154226** cursor number into bits and the translated bit is stored in the prereq
154227** fields. The translation is used in order to maximize the number of
154228** bits that will fit in a Bitmask. The VDBE cursor numbers might be
154229** spread out over the non-negative integers. For example, the cursor
154230** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
154231** translates these sparse cursor numbers into consecutive integers
154232** beginning with 0 in order to make the best possible use of the available
154233** bits in the Bitmask. So, in the example above, the cursor numbers
154234** would be mapped into integers 0 through 7.
154235**
154236** The number of terms in a join is limited by the number of bits
154237** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
154238** is only able to process joins with 64 or fewer tables.
154239*/
154240struct WhereTerm {
154241 Expr *pExpr; /* Pointer to the subexpression that is this term */
154242 WhereClause *pWC; /* The clause this term is part of */
154243 LogEst truthProb; /* Probability of truth for this expression */
154244 u16 wtFlags; /* TERM_xxx bit flags. See below */
154245 u16 eOperator; /* A WO_xx value describing <op> */
154246 u8 nChild; /* Number of children that must disable us */
154247 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
154248 int iParent; /* Disable pWC->a[iParent] when this term disabled */
154249 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
154250 union {
154251 struct {
154252 int leftColumn; /* Column number of X in "X <op> <expr>" */
154253 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
154254 } x; /* Opcode other than OP_OR or OP_AND */
154255 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
154256 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
154257 } u;
154258 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
154259 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
154260};
154261
154262/*
154263** Allowed values of WhereTerm.wtFlags
154264*/
154265#define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
154266#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
154267#define TERM_CODED 0x0004 /* This term is already coded */
154268#define TERM_COPIED 0x0008 /* Has a child */
154269#define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
154270#define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
154271#define TERM_OK 0x0040 /* Used during OR-clause processing */
154272#define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
154273#define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
154274#define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
154275#define TERM_LIKE 0x0400 /* The original LIKE operator */
154276#define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
154277#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
154278#define TERM_HEURTRUTH 0x2000 /* Heuristic truthProb used */
154279#ifdef SQLITE_ENABLE_STAT4
154280# define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */
154281#else
154282# define TERM_HIGHTRUTH 0 /* Only used with STAT4 */
154283#endif
154284#define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
154285
154286/*
154287** An instance of the WhereScan object is used as an iterator for locating
154288** terms in the WHERE clause that are useful to the query planner.
154289*/
154290struct WhereScan {
154291 WhereClause *pOrigWC; /* Original, innermost WhereClause */
154292 WhereClause *pWC; /* WhereClause currently being scanned */
154293 const char *zCollName; /* Required collating sequence, if not NULL */
154294 Expr *pIdxExpr; /* Search for this index expression */
154295 int k; /* Resume scanning at this->pWC->a[this->k] */
154296 u32 opMask; /* Acceptable operators */
154297 char idxaff; /* Must match this affinity, if zCollName!=NULL */
154298 unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */
154299 unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */
154300 int aiCur[11]; /* Cursors in the equivalence class */
154301 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
154302};
154303
154304/*
154305** An instance of the following structure holds all information about a
154306** WHERE clause. Mostly this is a container for one or more WhereTerms.
154307**
154308** Explanation of pOuter: For a WHERE clause of the form
154309**
154310** a AND ((b AND c) OR (d AND e)) AND f
154311**
154312** There are separate WhereClause objects for the whole clause and for
154313** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
154314** subclauses points to the WhereClause object for the whole clause.
154315*/
154316struct WhereClause {
154317 WhereInfo *pWInfo; /* WHERE clause processing context */
154318 WhereClause *pOuter; /* Outer conjunction */
154319 u8 op; /* Split operator. TK_AND or TK_OR */
154320 u8 hasOr; /* True if any a[].eOperator is WO_OR */
154321 int nTerm; /* Number of terms */
154322 int nSlot; /* Number of entries in a[] */
154323 int nBase; /* Number of terms through the last non-Virtual */
154324 WhereTerm *a; /* Each a[] describes a term of the WHERE clause */
154325#if defined(SQLITE_SMALL_STACK)
154326 WhereTerm aStatic[1]; /* Initial static space for a[] */
154327#else
154328 WhereTerm aStatic[8]; /* Initial static space for a[] */
154329#endif
154330};
154331
154332/*
154333** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
154334** a dynamically allocated instance of the following structure.
154335*/
154336struct WhereOrInfo {
154337 WhereClause wc; /* Decomposition into subterms */
154338 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
154339};
154340
154341/*
154342** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
154343** a dynamically allocated instance of the following structure.
154344*/
154345struct WhereAndInfo {
154346 WhereClause wc; /* The subexpression broken out */
154347};
154348
154349/*
154350** An instance of the following structure keeps track of a mapping
154351** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
154352**
154353** The VDBE cursor numbers are small integers contained in
154354** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
154355** clause, the cursor numbers might not begin with 0 and they might
154356** contain gaps in the numbering sequence. But we want to make maximum
154357** use of the bits in our bitmasks. This structure provides a mapping
154358** from the sparse cursor numbers into consecutive integers beginning
154359** with 0.
154360**
154361** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
154362** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
154363**
154364** For example, if the WHERE clause expression used these VDBE
154365** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
154366** would map those cursor numbers into bits 0 through 5.
154367**
154368** Note that the mapping is not necessarily ordered. In the example
154369** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
154370** 57->5, 73->4. Or one of 719 other combinations might be used. It
154371** does not really matter. What is important is that sparse cursor
154372** numbers all get mapped into bit numbers that begin with 0 and contain
154373** no gaps.
154374*/
154375struct WhereMaskSet {
154376 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
154377 int n; /* Number of assigned cursor values */
154378 int ix[BMS]; /* Cursor assigned to each bit */
154379};
154380
154381/*
154382** This object is a convenience wrapper holding all information needed
154383** to construct WhereLoop objects for a particular query.
154384*/
154385struct WhereLoopBuilder {
154386 WhereInfo *pWInfo; /* Information about this WHERE */
154387 WhereClause *pWC; /* WHERE clause terms */
154388 WhereLoop *pNew; /* Template WhereLoop */
154389 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
154390#ifdef SQLITE_ENABLE_STAT4
154391 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
154392 int nRecValid; /* Number of valid fields currently in pRec */
154393#endif
154394 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
154395 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
154396 unsigned int iPlanLimit; /* Search limiter */
154397};
154398
154399/* Allowed values for WhereLoopBuider.bldFlags */
154400#define SQLITE_BLDF1_INDEXED 0x0001 /* An index is used */
154401#define SQLITE_BLDF1_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
154402
154403#define SQLITE_BLDF2_2NDPASS 0x0004 /* Second builder pass needed */
154404
154405/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
154406** index+constraint combinations the query planner will consider for a
154407** particular query. If this parameter is unlimited, then certain
154408** pathological queries can spend excess time in the sqlite3WhereBegin()
154409** routine. The limit is high enough that is should not impact real-world
154410** queries.
154411**
154412** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
154413** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
154414** clause is processed, so that every table in a join is guaranteed to be
154415** able to propose a some index+constraint combinations even if the initial
154416** baseline limit was exhausted by prior tables of the join.
154417*/
154418#ifndef SQLITE_QUERY_PLANNER_LIMIT
154419# define SQLITE_QUERY_PLANNER_LIMIT 20000
154420#endif
154421#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
154422# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
154423#endif
154424
154425/*
154426** The WHERE clause processing routine has two halves. The
154427** first part does the start of the WHERE loop and the second
154428** half does the tail of the WHERE loop. An instance of
154429** this structure is returned by the first half and passed
154430** into the second half to give some continuity.
154431**
154432** An instance of this object holds the complete state of the query
154433** planner.
154434*/
154435struct WhereInfo {
154436 Parse *pParse; /* Parsing and code generating context */
154437 SrcList *pTabList; /* List of tables in the join */
154438 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
154439 ExprList *pResultSet; /* Result set of the query */
154440#if WHERETRACE_ENABLED
154441 Expr *pWhere; /* The complete WHERE clause */
154442#endif
154443 Select *pSelect; /* The entire SELECT statement containing WHERE */
154444 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
154445 int iContinue; /* Jump here to continue with next record */
154446 int iBreak; /* Jump here to break out of the loop */
154447 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
154448 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
154449 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
154450 u8 nLevel; /* Number of nested loop */
154451 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
154452 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
154453 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
154454 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
154455 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
154456 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
154457 unsigned sorted :1; /* True if really sorted (not just grouped) */
154458 LogEst nRowOut; /* Estimated number of output rows */
154459 int iTop; /* The very beginning of the WHERE loop */
154460 int iEndWhere; /* End of the WHERE clause itself */
154461 WhereLoop *pLoops; /* List of all WhereLoop objects */
154462 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
154463 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
154464 WhereClause sWC; /* Decomposition of the WHERE clause */
154465 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
154466 WhereLevel a[1]; /* Information about each nest loop in WHERE */
154467};
154468
154469/*
154470** Private interfaces - callable only by other where.c routines.
154471**
154472** where.c:
154473*/
154474SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
154475#ifdef WHERETRACE_ENABLED
154476SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
154477SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
154478SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
154479#endif
154480SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
154481 WhereClause *pWC, /* The WHERE clause to be searched */
154482 int iCur, /* Cursor number of LHS */
154483 int iColumn, /* Column number of LHS */
154484 Bitmask notReady, /* RHS must not overlap with this mask */
154485 u32 op, /* Mask of WO_xx values describing operator */
154486 Index *pIdx /* Must be compatible with this index, if not NULL */
154487);
154488SQLITE_PRIVATE void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte);
154489SQLITE_PRIVATE void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte);
154490
154491/* wherecode.c: */
154492#ifndef SQLITE_OMIT_EXPLAIN
154493SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
154494 Parse *pParse, /* Parse context */
154495 SrcList *pTabList, /* Table list this loop refers to */
154496 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
154497 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
154498);
154499SQLITE_PRIVATE int sqlite3WhereExplainBloomFilter(
154500 const Parse *pParse, /* Parse context */
154501 const WhereInfo *pWInfo, /* WHERE clause */
154502 const WhereLevel *pLevel /* Bloom filter on this level */
154503);
154504#else
154505# define sqlite3WhereExplainOneScan(u,v,w,x) 0
154506# define sqlite3WhereExplainBloomFilter(u,v,w) 0
154507#endif /* SQLITE_OMIT_EXPLAIN */
154508#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
154509SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
154510 Vdbe *v, /* Vdbe to add scanstatus entry to */
154511 SrcList *pSrclist, /* FROM clause pLvl reads data from */
154512 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
154513 int addrExplain /* Address of OP_Explain (or 0) */
154514);
154515#else
154516# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
154517#endif
154518SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
154519 Parse *pParse, /* Parsing context */
154520 Vdbe *v, /* Prepared statement under construction */
154521 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
154522 int iLevel, /* Which level of pWInfo->a[] should be coded */
154523 WhereLevel *pLevel, /* The current level pointer */
154524 Bitmask notReady /* Which tables are currently available */
154525);
154526SQLITE_PRIVATE SQLITE_NOINLINE void sqlite3WhereRightJoinLoop(
154527 WhereInfo *pWInfo,
154528 int iLevel,
154529 WhereLevel *pLevel
154530);
154531
154532/* whereexpr.c: */
154533SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
154534SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
154535SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
154536SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause*, Select*);
154537SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
154538SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
154539SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
154540SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
154541SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
154542
154543
154544
154545
154546
154547/*
154548** Bitmasks for the operators on WhereTerm objects. These are all
154549** operators that are of interest to the query planner. An
154550** OR-ed combination of these values can be used when searching for
154551** particular WhereTerms within a WhereClause.
154552**
154553** Value constraints:
154554** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
154555** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
154556** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
154557** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
154558** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
154559*/
154560#define WO_IN 0x0001
154561#define WO_EQ 0x0002
154562#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
154563#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
154564#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
154565#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
154566#define WO_AUX 0x0040 /* Op useful to virtual tables only */
154567#define WO_IS 0x0080
154568#define WO_ISNULL 0x0100
154569#define WO_OR 0x0200 /* Two or more OR-connected terms */
154570#define WO_AND 0x0400 /* Two or more AND-connected terms */
154571#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
154572#define WO_NOOP 0x1000 /* This term does not restrict search space */
154573#define WO_ROWVAL 0x2000 /* A row-value term */
154574
154575#define WO_ALL 0x3fff /* Mask of all possible WO_* values */
154576#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
154577
154578/*
154579** These are definitions of bits in the WhereLoop.wsFlags field.
154580** The particular combination of bits in each WhereLoop help to
154581** determine the algorithm that WhereLoop represents.
154582*/
154583#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
154584#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
154585#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
154586#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
154587#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
154588#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
154589#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
154590#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
154591#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
154592#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
154593#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
154594#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
154595#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
154596#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
154597#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
154598#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
154599#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
154600#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
154601#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
154602#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */
154603#define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
154604#define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
154605#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
154606#define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
154607#define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
154608#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
154609#define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
154610#define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
154611
154612#endif /* !defined(SQLITE_WHEREINT_H) */
154613
154614/************** End of whereInt.h ********************************************/
154615/************** Continuing where we left off in wherecode.c ******************/
154616
154617#ifndef SQLITE_OMIT_EXPLAIN
154618
154619/*
154620** Return the name of the i-th column of the pIdx index.
154621*/
154622static const char *explainIndexColumnName(Index *pIdx, int i){
154623 i = pIdx->aiColumn[i];
154624 if( i==XN_EXPR ) return "<expr>";
154625 if( i==XN_ROWID ) return "rowid";
154626 return pIdx->pTable->aCol[i].zCnName;
154627}
154628
154629/*
154630** This routine is a helper for explainIndexRange() below
154631**
154632** pStr holds the text of an expression that we are building up one term
154633** at a time. This routine adds a new term to the end of the expression.
154634** Terms are separated by AND so add the "AND" text for second and subsequent
154635** terms only.
154636*/
154637static void explainAppendTerm(
154638 StrAccum *pStr, /* The text expression being built */
154639 Index *pIdx, /* Index to read column names from */
154640 int nTerm, /* Number of terms */
154641 int iTerm, /* Zero-based index of first term. */
154642 int bAnd, /* Non-zero to append " AND " */
154643 const char *zOp /* Name of the operator */
154644){
154645 int i;
154646
154647 assert( nTerm>=1 );
154648 if( bAnd ) sqlite3_str_append(p: pStr, z: " AND ", N: 5);
154649
154650 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: "(", N: 1);
154651 for(i=0; i<nTerm; i++){
154652 if( i ) sqlite3_str_append(p: pStr, z: ",", N: 1);
154653 sqlite3_str_appendall(p: pStr, z: explainIndexColumnName(pIdx, i: iTerm+i));
154654 }
154655 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: ")", N: 1);
154656
154657 sqlite3_str_append(p: pStr, z: zOp, N: 1);
154658
154659 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: "(", N: 1);
154660 for(i=0; i<nTerm; i++){
154661 if( i ) sqlite3_str_append(p: pStr, z: ",", N: 1);
154662 sqlite3_str_append(p: pStr, z: "?", N: 1);
154663 }
154664 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: ")", N: 1);
154665}
154666
154667/*
154668** Argument pLevel describes a strategy for scanning table pTab. This
154669** function appends text to pStr that describes the subset of table
154670** rows scanned by the strategy in the form of an SQL expression.
154671**
154672** For example, if the query:
154673**
154674** SELECT * FROM t1 WHERE a=1 AND b>2;
154675**
154676** is run and there is an index on (a, b), then this function returns a
154677** string similar to:
154678**
154679** "a=? AND b>?"
154680*/
154681static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
154682 Index *pIndex = pLoop->u.btree.pIndex;
154683 u16 nEq = pLoop->u.btree.nEq;
154684 u16 nSkip = pLoop->nSkip;
154685 int i, j;
154686
154687 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
154688 sqlite3_str_append(p: pStr, z: " (", N: 2);
154689 for(i=0; i<nEq; i++){
154690 const char *z = explainIndexColumnName(pIdx: pIndex, i);
154691 if( i ) sqlite3_str_append(p: pStr, z: " AND ", N: 5);
154692 sqlite3_str_appendf(p: pStr, zFormat: i>=nSkip ? "%s=?" : "ANY(%s)", z);
154693 }
154694
154695 j = i;
154696 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
154697 explainAppendTerm(pStr, pIdx: pIndex, nTerm: pLoop->u.btree.nBtm, iTerm: j, bAnd: i, zOp: ">");
154698 i = 1;
154699 }
154700 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
154701 explainAppendTerm(pStr, pIdx: pIndex, nTerm: pLoop->u.btree.nTop, iTerm: j, bAnd: i, zOp: "<");
154702 }
154703 sqlite3_str_append(p: pStr, z: ")", N: 1);
154704}
154705
154706/*
154707** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
154708** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG
154709** was defined at compile-time. If it is not a no-op, a single OP_Explain
154710** opcode is added to the output to describe the table scan strategy in pLevel.
154711**
154712** If an OP_Explain opcode is added to the VM, its address is returned.
154713** Otherwise, if no OP_Explain is coded, zero is returned.
154714*/
154715SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
154716 Parse *pParse, /* Parse context */
154717 SrcList *pTabList, /* Table list this loop refers to */
154718 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
154719 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
154720){
154721 int ret = 0;
154722#if !defined(SQLITE_DEBUG)
154723 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
154724#endif
154725 {
154726 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
154727 Vdbe *v = pParse->pVdbe; /* VM being constructed */
154728 sqlite3 *db = pParse->db; /* Database handle */
154729 int isSearch; /* True for a SEARCH. False for SCAN. */
154730 WhereLoop *pLoop; /* The controlling WhereLoop object */
154731 u32 flags; /* Flags that describe this loop */
154732 char *zMsg; /* Text to add to EQP output */
154733 StrAccum str; /* EQP output string */
154734 char zBuf[100]; /* Initial space for EQP output string */
154735
154736 pLoop = pLevel->pWLoop;
154737 flags = pLoop->wsFlags;
154738 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
154739
154740 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
154741 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
154742 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
154743
154744 sqlite3StrAccumInit(p: &str, db, zBase: zBuf, n: sizeof(zBuf), SQLITE_MAX_LENGTH);
154745 str.printfFlags = SQLITE_PRINTF_INTERNAL;
154746 sqlite3_str_appendf(p: &str, zFormat: "%s %S", isSearch ? "SEARCH" : "SCAN", pItem);
154747 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
154748 const char *zFmt = 0;
154749 Index *pIdx;
154750
154751 assert( pLoop->u.btree.pIndex!=0 );
154752 pIdx = pLoop->u.btree.pIndex;
154753 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
154754 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
154755 if( isSearch ){
154756 zFmt = "PRIMARY KEY";
154757 }
154758 }else if( flags & WHERE_PARTIALIDX ){
154759 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
154760 }else if( flags & WHERE_AUTO_INDEX ){
154761 zFmt = "AUTOMATIC COVERING INDEX";
154762 }else if( flags & WHERE_IDX_ONLY ){
154763 zFmt = "COVERING INDEX %s";
154764 }else{
154765 zFmt = "INDEX %s";
154766 }
154767 if( zFmt ){
154768 sqlite3_str_append(p: &str, z: " USING ", N: 7);
154769 sqlite3_str_appendf(p: &str, zFormat: zFmt, pIdx->zName);
154770 explainIndexRange(pStr: &str, pLoop);
154771 }
154772 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
154773 char cRangeOp;
154774#if 0 /* Better output, but breaks many tests */
154775 const Table *pTab = pItem->pTab;
154776 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
154777 "rowid";
154778#else
154779 const char *zRowid = "rowid";
154780#endif
154781 sqlite3_str_appendf(p: &str, zFormat: " USING INTEGER PRIMARY KEY (%s", zRowid);
154782 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
154783 cRangeOp = '=';
154784 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
154785 sqlite3_str_appendf(p: &str, zFormat: ">? AND %s", zRowid);
154786 cRangeOp = '<';
154787 }else if( flags&WHERE_BTM_LIMIT ){
154788 cRangeOp = '>';
154789 }else{
154790 assert( flags&WHERE_TOP_LIMIT);
154791 cRangeOp = '<';
154792 }
154793 sqlite3_str_appendf(p: &str, zFormat: "%c?)", cRangeOp);
154794 }
154795#ifndef SQLITE_OMIT_VIRTUALTABLE
154796 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
154797 sqlite3_str_appendf(p: &str, zFormat: " VIRTUAL TABLE INDEX %d:%s",
154798 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
154799 }
154800#endif
154801 if( pItem->fg.jointype & JT_LEFT ){
154802 sqlite3_str_appendf(p: &str, zFormat: " LEFT-JOIN");
154803 }
154804#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
154805 if( pLoop->nOut>=10 ){
154806 sqlite3_str_appendf(&str, " (~%llu rows)",
154807 sqlite3LogEstToInt(pLoop->nOut));
154808 }else{
154809 sqlite3_str_append(&str, " (~1 row)", 9);
154810 }
154811#endif
154812 zMsg = sqlite3StrAccumFinish(p: &str);
154813 sqlite3ExplainBreakpoint("",zMsg);
154814 ret = sqlite3VdbeAddOp4(p: v, OP_Explain, p1: sqlite3VdbeCurrentAddr(p: v),
154815 p2: pParse->addrExplain, p3: 0, zP4: zMsg,P4_DYNAMIC);
154816 }
154817 return ret;
154818}
154819
154820/*
154821** Add a single OP_Explain opcode that describes a Bloom filter.
154822**
154823** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or
154824** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not
154825** required and this routine is a no-op.
154826**
154827** If an OP_Explain opcode is added to the VM, its address is returned.
154828** Otherwise, if no OP_Explain is coded, zero is returned.
154829*/
154830SQLITE_PRIVATE int sqlite3WhereExplainBloomFilter(
154831 const Parse *pParse, /* Parse context */
154832 const WhereInfo *pWInfo, /* WHERE clause */
154833 const WhereLevel *pLevel /* Bloom filter on this level */
154834){
154835 int ret = 0;
154836 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
154837 Vdbe *v = pParse->pVdbe; /* VM being constructed */
154838 sqlite3 *db = pParse->db; /* Database handle */
154839 char *zMsg; /* Text to add to EQP output */
154840 int i; /* Loop counter */
154841 WhereLoop *pLoop; /* The where loop */
154842 StrAccum str; /* EQP output string */
154843 char zBuf[100]; /* Initial space for EQP output string */
154844
154845 sqlite3StrAccumInit(p: &str, db, zBase: zBuf, n: sizeof(zBuf), SQLITE_MAX_LENGTH);
154846 str.printfFlags = SQLITE_PRINTF_INTERNAL;
154847 sqlite3_str_appendf(p: &str, zFormat: "BLOOM FILTER ON %S (", pItem);
154848 pLoop = pLevel->pWLoop;
154849 if( pLoop->wsFlags & WHERE_IPK ){
154850 const Table *pTab = pItem->pTab;
154851 if( pTab->iPKey>=0 ){
154852 sqlite3_str_appendf(p: &str, zFormat: "%s=?", pTab->aCol[pTab->iPKey].zCnName);
154853 }else{
154854 sqlite3_str_appendf(p: &str, zFormat: "rowid=?");
154855 }
154856 }else{
154857 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
154858 const char *z = explainIndexColumnName(pIdx: pLoop->u.btree.pIndex, i);
154859 if( i>pLoop->nSkip ) sqlite3_str_append(p: &str, z: " AND ", N: 5);
154860 sqlite3_str_appendf(p: &str, zFormat: "%s=?", z);
154861 }
154862 }
154863 sqlite3_str_append(p: &str, z: ")", N: 1);
154864 zMsg = sqlite3StrAccumFinish(p: &str);
154865 ret = sqlite3VdbeAddOp4(p: v, OP_Explain, p1: sqlite3VdbeCurrentAddr(p: v),
154866 p2: pParse->addrExplain, p3: 0, zP4: zMsg,P4_DYNAMIC);
154867
154868 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
154869 return ret;
154870}
154871#endif /* SQLITE_OMIT_EXPLAIN */
154872
154873#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
154874/*
154875** Configure the VM passed as the first argument with an
154876** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
154877** implement level pLvl. Argument pSrclist is a pointer to the FROM
154878** clause that the scan reads data from.
154879**
154880** If argument addrExplain is not 0, it must be the address of an
154881** OP_Explain instruction that describes the same loop.
154882*/
154883SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
154884 Vdbe *v, /* Vdbe to add scanstatus entry to */
154885 SrcList *pSrclist, /* FROM clause pLvl reads data from */
154886 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
154887 int addrExplain /* Address of OP_Explain (or 0) */
154888){
154889 if( IS_STMT_SCANSTATUS( sqlite3VdbeDb(v) ) ){
154890 const char *zObj = 0;
154891 WhereLoop *pLoop = pLvl->pWLoop;
154892 int wsFlags = pLoop->wsFlags;
154893 int viaCoroutine = 0;
154894
154895 if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
154896 zObj = pLoop->u.btree.pIndex->zName;
154897 }else{
154898 zObj = pSrclist->a[pLvl->iFrom].zName;
154899 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
154900 }
154901 sqlite3VdbeScanStatus(
154902 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
154903 );
154904
154905 if( viaCoroutine==0 ){
154906 if( (wsFlags & (WHERE_MULTI_OR|WHERE_AUTO_INDEX))==0 ){
154907 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
154908 }
154909 if( wsFlags & WHERE_INDEXED ){
154910 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
154911 }
154912 }else{
154913 int addr = pSrclist->a[pLvl->iFrom].addrFillSub;
154914 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1);
154915 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
154916 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
154917 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
154918 }
154919 }
154920}
154921#endif
154922
154923
154924/*
154925** Disable a term in the WHERE clause. Except, do not disable the term
154926** if it controls a LEFT OUTER JOIN and it did not originate in the ON
154927** or USING clause of that join.
154928**
154929** Consider the term t2.z='ok' in the following queries:
154930**
154931** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
154932** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
154933** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
154934**
154935** The t2.z='ok' is disabled in the in (2) because it originates
154936** in the ON clause. The term is disabled in (3) because it is not part
154937** of a LEFT OUTER JOIN. In (1), the term is not disabled.
154938**
154939** Disabling a term causes that term to not be tested in the inner loop
154940** of the join. Disabling is an optimization. When terms are satisfied
154941** by indices, we disable them to prevent redundant tests in the inner
154942** loop. We would get the correct results if nothing were ever disabled,
154943** but joins might run a little slower. The trick is to disable as much
154944** as we can without disabling too much. If we disabled in (1), we'd get
154945** the wrong answer. See ticket #813.
154946**
154947** If all the children of a term are disabled, then that term is also
154948** automatically disabled. In this way, terms get disabled if derived
154949** virtual terms are tested first. For example:
154950**
154951** x GLOB 'abc*' AND x>='abc' AND x<'acd'
154952** \___________/ \______/ \_____/
154953** parent child1 child2
154954**
154955** Only the parent term was in the original WHERE clause. The child1
154956** and child2 terms were added by the LIKE optimization. If both of
154957** the virtual child terms are valid, then testing of the parent can be
154958** skipped.
154959**
154960** Usually the parent term is marked as TERM_CODED. But if the parent
154961** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
154962** The TERM_LIKECOND marking indicates that the term should be coded inside
154963** a conditional such that is only evaluated on the second pass of a
154964** LIKE-optimization loop, when scanning BLOBs instead of strings.
154965*/
154966static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
154967 int nLoop = 0;
154968 assert( pTerm!=0 );
154969 while( (pTerm->wtFlags & TERM_CODED)==0
154970 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
154971 && (pLevel->notReady & pTerm->prereqAll)==0
154972 ){
154973 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
154974 pTerm->wtFlags |= TERM_LIKECOND;
154975 }else{
154976 pTerm->wtFlags |= TERM_CODED;
154977 }
154978#ifdef WHERETRACE_ENABLED
154979 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
154980 sqlite3DebugPrintf("DISABLE-");
154981 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
154982 }
154983#endif
154984 if( pTerm->iParent<0 ) break;
154985 pTerm = &pTerm->pWC->a[pTerm->iParent];
154986 assert( pTerm!=0 );
154987 pTerm->nChild--;
154988 if( pTerm->nChild!=0 ) break;
154989 nLoop++;
154990 }
154991}
154992
154993/*
154994** Code an OP_Affinity opcode to apply the column affinity string zAff
154995** to the n registers starting at base.
154996**
154997** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
154998** are no-ops) at the beginning and end of zAff are ignored. If all entries
154999** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
155000**
155001** This routine makes its own copy of zAff so that the caller is free
155002** to modify zAff after this routine returns.
155003*/
155004static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
155005 Vdbe *v = pParse->pVdbe;
155006 if( zAff==0 ){
155007 assert( pParse->db->mallocFailed );
155008 return;
155009 }
155010 assert( v!=0 );
155011
155012 /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
155013 ** entries at the beginning and end of the affinity string.
155014 */
155015 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB );
155016 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){
155017 n--;
155018 base++;
155019 zAff++;
155020 }
155021 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
155022 n--;
155023 }
155024
155025 /* Code the OP_Affinity opcode if there is anything left to do. */
155026 if( n>0 ){
155027 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: base, p2: n, p3: 0, zP4: zAff, p4type: n);
155028 }
155029}
155030
155031/*
155032** Expression pRight, which is the RHS of a comparison operation, is
155033** either a vector of n elements or, if n==1, a scalar expression.
155034** Before the comparison operation, affinity zAff is to be applied
155035** to the pRight values. This function modifies characters within the
155036** affinity string to SQLITE_AFF_BLOB if either:
155037**
155038** * the comparison will be performed with no affinity, or
155039** * the affinity change in zAff is guaranteed not to change the value.
155040*/
155041static void updateRangeAffinityStr(
155042 Expr *pRight, /* RHS of comparison */
155043 int n, /* Number of vector elements in comparison */
155044 char *zAff /* Affinity string to modify */
155045){
155046 int i;
155047 for(i=0; i<n; i++){
155048 Expr *p = sqlite3VectorFieldSubexpr(pVector: pRight, i);
155049 if( sqlite3CompareAffinity(pExpr: p, aff2: zAff[i])==SQLITE_AFF_BLOB
155050 || sqlite3ExprNeedsNoAffinityChange(p, aff: zAff[i])
155051 ){
155052 zAff[i] = SQLITE_AFF_BLOB;
155053 }
155054 }
155055}
155056
155057
155058/*
155059** pX is an expression of the form: (vector) IN (SELECT ...)
155060** In other words, it is a vector IN operator with a SELECT clause on the
155061** LHS. But not all terms in the vector are indexable and the terms might
155062** not be in the correct order for indexing.
155063**
155064** This routine makes a copy of the input pX expression and then adjusts
155065** the vector on the LHS with corresponding changes to the SELECT so that
155066** the vector contains only index terms and those terms are in the correct
155067** order. The modified IN expression is returned. The caller is responsible
155068** for deleting the returned expression.
155069**
155070** Example:
155071**
155072** CREATE TABLE t1(a,b,c,d,e,f);
155073** CREATE INDEX t1x1 ON t1(e,c);
155074** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
155075** \_______________________________________/
155076** The pX expression
155077**
155078** Since only columns e and c can be used with the index, in that order,
155079** the modified IN expression that is returned will be:
155080**
155081** (e,c) IN (SELECT z,x FROM t2)
155082**
155083** The reduced pX is different from the original (obviously) and thus is
155084** only used for indexing, to improve performance. The original unaltered
155085** IN expression must also be run on each output row for correctness.
155086*/
155087static Expr *removeUnindexableInClauseTerms(
155088 Parse *pParse, /* The parsing context */
155089 int iEq, /* Look at loop terms starting here */
155090 WhereLoop *pLoop, /* The current loop */
155091 Expr *pX /* The IN expression to be reduced */
155092){
155093 sqlite3 *db = pParse->db;
155094 Select *pSelect; /* Pointer to the SELECT on the RHS */
155095 Expr *pNew;
155096 pNew = sqlite3ExprDup(db, p: pX, flags: 0);
155097 if( db->mallocFailed==0 ){
155098 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
155099 ExprList *pOrigRhs; /* Original unmodified RHS */
155100 ExprList *pOrigLhs = 0; /* Original unmodified LHS */
155101 ExprList *pRhs = 0; /* New RHS after modifications */
155102 ExprList *pLhs = 0; /* New LHS after mods */
155103 int i; /* Loop counter */
155104
155105 assert( ExprUseXSelect(pNew) );
155106 pOrigRhs = pSelect->pEList;
155107 assert( pNew->pLeft!=0 );
155108 assert( ExprUseXList(pNew->pLeft) );
155109 if( pSelect==pNew->x.pSelect ){
155110 pOrigLhs = pNew->pLeft->x.pList;
155111 }
155112 for(i=iEq; i<pLoop->nLTerm; i++){
155113 if( pLoop->aLTerm[i]->pExpr==pX ){
155114 int iField;
155115 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
155116 iField = pLoop->aLTerm[i]->u.x.iField - 1;
155117 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
155118 pRhs = sqlite3ExprListAppend(pParse, pList: pRhs, pExpr: pOrigRhs->a[iField].pExpr);
155119 pOrigRhs->a[iField].pExpr = 0;
155120 if( pOrigLhs ){
155121 assert( pOrigLhs->a[iField].pExpr!=0 );
155122 pLhs = sqlite3ExprListAppend(pParse,pList: pLhs,pExpr: pOrigLhs->a[iField].pExpr);
155123 pOrigLhs->a[iField].pExpr = 0;
155124 }
155125 }
155126 }
155127 sqlite3ExprListDelete(db, pList: pOrigRhs);
155128 if( pOrigLhs ){
155129 sqlite3ExprListDelete(db, pList: pOrigLhs);
155130 pNew->pLeft->x.pList = pLhs;
155131 }
155132 pSelect->pEList = pRhs;
155133 if( pLhs && pLhs->nExpr==1 ){
155134 /* Take care here not to generate a TK_VECTOR containing only a
155135 ** single value. Since the parser never creates such a vector, some
155136 ** of the subroutines do not handle this case. */
155137 Expr *p = pLhs->a[0].pExpr;
155138 pLhs->a[0].pExpr = 0;
155139 sqlite3ExprDelete(db, p: pNew->pLeft);
155140 pNew->pLeft = p;
155141 }
155142 if( pSelect->pOrderBy ){
155143 /* If the SELECT statement has an ORDER BY clause, zero the
155144 ** iOrderByCol variables. These are set to non-zero when an
155145 ** ORDER BY term exactly matches one of the terms of the
155146 ** result-set. Since the result-set of the SELECT statement may
155147 ** have been modified or reordered, these variables are no longer
155148 ** set correctly. Since setting them is just an optimization,
155149 ** it's easiest just to zero them here. */
155150 ExprList *pOrderBy = pSelect->pOrderBy;
155151 for(i=0; i<pOrderBy->nExpr; i++){
155152 pOrderBy->a[i].u.x.iOrderByCol = 0;
155153 }
155154 }
155155
155156#if 0
155157 printf("For indexing, change the IN expr:\n");
155158 sqlite3TreeViewExpr(0, pX, 0);
155159 printf("Into:\n");
155160 sqlite3TreeViewExpr(0, pNew, 0);
155161#endif
155162 }
155163 }
155164 return pNew;
155165}
155166
155167
155168/*
155169** Generate code for a single equality term of the WHERE clause. An equality
155170** term can be either X=expr or X IN (...). pTerm is the term to be
155171** coded.
155172**
155173** The current value for the constraint is left in a register, the index
155174** of which is returned. An attempt is made store the result in iTarget but
155175** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
155176** constraint is a TK_EQ or TK_IS, then the current value might be left in
155177** some other register and it is the caller's responsibility to compensate.
155178**
155179** For a constraint of the form X=expr, the expression is evaluated in
155180** straight-line code. For constraints of the form X IN (...)
155181** this routine sets up a loop that will iterate over all values of X.
155182*/
155183static int codeEqualityTerm(
155184 Parse *pParse, /* The parsing context */
155185 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
155186 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
155187 int iEq, /* Index of the equality term within this level */
155188 int bRev, /* True for reverse-order IN operations */
155189 int iTarget /* Attempt to leave results in this register */
155190){
155191 Expr *pX = pTerm->pExpr;
155192 Vdbe *v = pParse->pVdbe;
155193 int iReg; /* Register holding results */
155194
155195 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
155196 assert( iTarget>0 );
155197 if( pX->op==TK_EQ || pX->op==TK_IS ){
155198 iReg = sqlite3ExprCodeTarget(pParse, pExpr: pX->pRight, target: iTarget);
155199 }else if( pX->op==TK_ISNULL ){
155200 iReg = iTarget;
155201 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: iReg);
155202#ifndef SQLITE_OMIT_SUBQUERY
155203 }else{
155204 int eType = IN_INDEX_NOOP;
155205 int iTab;
155206 struct InLoop *pIn;
155207 WhereLoop *pLoop = pLevel->pWLoop;
155208 int i;
155209 int nEq = 0;
155210 int *aiMap = 0;
155211
155212 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
155213 && pLoop->u.btree.pIndex!=0
155214 && pLoop->u.btree.pIndex->aSortOrder[iEq]
155215 ){
155216 testcase( iEq==0 );
155217 testcase( bRev );
155218 bRev = !bRev;
155219 }
155220 assert( pX->op==TK_IN );
155221 iReg = iTarget;
155222
155223 for(i=0; i<iEq; i++){
155224 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
155225 disableTerm(pLevel, pTerm);
155226 return iTarget;
155227 }
155228 }
155229 for(i=iEq;i<pLoop->nLTerm; i++){
155230 assert( pLoop->aLTerm[i]!=0 );
155231 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
155232 }
155233
155234 iTab = 0;
155235 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
155236 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, prRhsHasNull: 0, aiMap: 0, piTab: &iTab);
155237 }else{
155238 Expr *pExpr = pTerm->pExpr;
155239 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
155240 sqlite3 *db = pParse->db;
155241 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
155242 if( !db->mallocFailed ){
155243 aiMap = (int*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(int)*nEq);
155244 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, prRhsHasNull: 0, aiMap,piTab: &iTab);
155245 pExpr->iTable = iTab;
155246 }
155247 sqlite3ExprDelete(db, p: pX);
155248 }else{
155249 int n = sqlite3ExprVectorSize(pExpr: pX->pLeft);
155250 aiMap = (int*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(int)*MAX(nEq,n));
155251 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, prRhsHasNull: 0, aiMap, piTab: &iTab);
155252 }
155253 pX = pExpr;
155254 }
155255
155256 if( eType==IN_INDEX_INDEX_DESC ){
155257 testcase( bRev );
155258 bRev = !bRev;
155259 }
155260 sqlite3VdbeAddOp2(p: v, op: bRev ? OP_Last : OP_Rewind, p1: iTab, p2: 0);
155261 VdbeCoverageIf(v, bRev);
155262 VdbeCoverageIf(v, !bRev);
155263
155264 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
155265 pLoop->wsFlags |= WHERE_IN_ABLE;
155266 if( pLevel->u.in.nIn==0 ){
155267 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
155268 }
155269 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
155270 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
155271 }
155272
155273 i = pLevel->u.in.nIn;
155274 pLevel->u.in.nIn += nEq;
155275 pLevel->u.in.aInLoop =
155276 sqlite3WhereRealloc(pWInfo: pTerm->pWC->pWInfo,
155277 pOld: pLevel->u.in.aInLoop,
155278 nByte: sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
155279 pIn = pLevel->u.in.aInLoop;
155280 if( pIn ){
155281 int iMap = 0; /* Index in aiMap[] */
155282 pIn += i;
155283 for(i=iEq;i<pLoop->nLTerm; i++){
155284 if( pLoop->aLTerm[i]->pExpr==pX ){
155285 int iOut = iReg + i - iEq;
155286 if( eType==IN_INDEX_ROWID ){
155287 pIn->addrInTop = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iTab, p2: iOut);
155288 }else{
155289 int iCol = aiMap ? aiMap[iMap++] : 0;
155290 pIn->addrInTop = sqlite3VdbeAddOp3(p: v,OP_Column,p1: iTab, p2: iCol, p3: iOut);
155291 }
155292 sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: iOut); VdbeCoverage(v);
155293 if( i==iEq ){
155294 pIn->iCur = iTab;
155295 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
155296 if( iEq>0 ){
155297 pIn->iBase = iReg - i;
155298 pIn->nPrefix = i;
155299 }else{
155300 pIn->nPrefix = 0;
155301 }
155302 }else{
155303 pIn->eEndLoopOp = OP_Noop;
155304 }
155305 pIn++;
155306 }
155307 }
155308 testcase( iEq>0
155309 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
155310 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
155311 if( iEq>0
155312 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
155313 ){
155314 sqlite3VdbeAddOp3(p: v, OP_SeekHit, p1: pLevel->iIdxCur, p2: 0, p3: iEq);
155315 }
155316 }else{
155317 pLevel->u.in.nIn = 0;
155318 }
155319 sqlite3DbFree(db: pParse->db, p: aiMap);
155320#endif
155321 }
155322
155323 /* As an optimization, try to disable the WHERE clause term that is
155324 ** driving the index as it will always be true. The correct answer is
155325 ** obtained regardless, but we might get the answer with fewer CPU cycles
155326 ** by omitting the term.
155327 **
155328 ** But do not disable the term unless we are certain that the term is
155329 ** not a transitive constraint. For an example of where that does not
155330 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
155331 */
155332 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
155333 || (pTerm->eOperator & WO_EQUIV)==0
155334 ){
155335 disableTerm(pLevel, pTerm);
155336 }
155337
155338 return iReg;
155339}
155340
155341/*
155342** Generate code that will evaluate all == and IN constraints for an
155343** index scan.
155344**
155345** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
155346** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
155347** The index has as many as three equality constraints, but in this
155348** example, the third "c" value is an inequality. So only two
155349** constraints are coded. This routine will generate code to evaluate
155350** a==5 and b IN (1,2,3). The current values for a and b will be stored
155351** in consecutive registers and the index of the first register is returned.
155352**
155353** In the example above nEq==2. But this subroutine works for any value
155354** of nEq including 0. If nEq==0, this routine is nearly a no-op.
155355** The only thing it does is allocate the pLevel->iMem memory cell and
155356** compute the affinity string.
155357**
155358** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
155359** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
155360** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
155361** occurs after the nEq quality constraints.
155362**
155363** This routine allocates a range of nEq+nExtraReg memory cells and returns
155364** the index of the first memory cell in that range. The code that
155365** calls this routine will use that memory range to store keys for
155366** start and termination conditions of the loop.
155367** key value of the loop. If one or more IN operators appear, then
155368** this routine allocates an additional nEq memory cells for internal
155369** use.
155370**
155371** Before returning, *pzAff is set to point to a buffer containing a
155372** copy of the column affinity string of the index allocated using
155373** sqlite3DbMalloc(). Except, entries in the copy of the string associated
155374** with equality constraints that use BLOB or NONE affinity are set to
155375** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
155376**
155377** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
155378** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
155379**
155380** In the example above, the index on t1(a) has TEXT affinity. But since
155381** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
155382** no conversion should be attempted before using a t2.b value as part of
155383** a key to search the index. Hence the first byte in the returned affinity
155384** string in this example would be set to SQLITE_AFF_BLOB.
155385*/
155386static int codeAllEqualityTerms(
155387 Parse *pParse, /* Parsing context */
155388 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
155389 int bRev, /* Reverse the order of IN operators */
155390 int nExtraReg, /* Number of extra registers to allocate */
155391 char **pzAff /* OUT: Set to point to affinity string */
155392){
155393 u16 nEq; /* The number of == or IN constraints to code */
155394 u16 nSkip; /* Number of left-most columns to skip */
155395 Vdbe *v = pParse->pVdbe; /* The vm under construction */
155396 Index *pIdx; /* The index being used for this loop */
155397 WhereTerm *pTerm; /* A single constraint term */
155398 WhereLoop *pLoop; /* The WhereLoop object */
155399 int j; /* Loop counter */
155400 int regBase; /* Base register */
155401 int nReg; /* Number of registers to allocate */
155402 char *zAff; /* Affinity string to return */
155403
155404 /* This module is only called on query plans that use an index. */
155405 pLoop = pLevel->pWLoop;
155406 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
155407 nEq = pLoop->u.btree.nEq;
155408 nSkip = pLoop->nSkip;
155409 pIdx = pLoop->u.btree.pIndex;
155410 assert( pIdx!=0 );
155411
155412 /* Figure out how many memory cells we will need then allocate them.
155413 */
155414 regBase = pParse->nMem + 1;
155415 nReg = nEq + nExtraReg;
155416 pParse->nMem += nReg;
155417
155418 zAff = sqlite3DbStrDup(db: pParse->db,z: sqlite3IndexAffinityStr(db: pParse->db,pIdx));
155419 assert( zAff!=0 || pParse->db->mallocFailed );
155420
155421 if( nSkip ){
155422 int iIdxCur = pLevel->iIdxCur;
155423 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: regBase, p3: regBase+nSkip-1);
155424 sqlite3VdbeAddOp1(p: v, op: (bRev?OP_Last:OP_Rewind), p1: iIdxCur);
155425 VdbeCoverageIf(v, bRev==0);
155426 VdbeCoverageIf(v, bRev!=0);
155427 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
155428 j = sqlite3VdbeAddOp0(p: v, OP_Goto);
155429 assert( pLevel->addrSkip==0 );
155430 pLevel->addrSkip = sqlite3VdbeAddOp4Int(p: v, op: (bRev?OP_SeekLT:OP_SeekGT),
155431 p1: iIdxCur, p2: 0, p3: regBase, p4: nSkip);
155432 VdbeCoverageIf(v, bRev==0);
155433 VdbeCoverageIf(v, bRev!=0);
155434 sqlite3VdbeJumpHere(p: v, addr: j);
155435 for(j=0; j<nSkip; j++){
155436 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: j, p3: regBase+j);
155437 testcase( pIdx->aiColumn[j]==XN_EXPR );
155438 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
155439 }
155440 }
155441
155442 /* Evaluate the equality constraints
155443 */
155444 assert( zAff==0 || (int)strlen(zAff)>=nEq );
155445 for(j=nSkip; j<nEq; j++){
155446 int r1;
155447 pTerm = pLoop->aLTerm[j];
155448 assert( pTerm!=0 );
155449 /* The following testcase is true for indices with redundant columns.
155450 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
155451 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
155452 testcase( pTerm->wtFlags & TERM_VIRTUAL );
155453 r1 = codeEqualityTerm(pParse, pTerm, pLevel, iEq: j, bRev, iTarget: regBase+j);
155454 if( r1!=regBase+j ){
155455 if( nReg==1 ){
155456 sqlite3ReleaseTempReg(pParse, iReg: regBase);
155457 regBase = r1;
155458 }else{
155459 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: r1, p2: regBase+j);
155460 }
155461 }
155462 if( pTerm->eOperator & WO_IN ){
155463 if( pTerm->pExpr->flags & EP_xIsSelect ){
155464 /* No affinity ever needs to be (or should be) applied to a value
155465 ** from the RHS of an "? IN (SELECT ...)" expression. The
155466 ** sqlite3FindInIndex() routine has already ensured that the
155467 ** affinity of the comparison has been applied to the value. */
155468 if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
155469 }
155470 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
155471 Expr *pRight = pTerm->pExpr->pRight;
155472 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(p: pRight) ){
155473 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regBase+j, p2: pLevel->addrBrk);
155474 VdbeCoverage(v);
155475 }
155476 if( pParse->nErr==0 ){
155477 assert( pParse->db->mallocFailed==0 );
155478 if( sqlite3CompareAffinity(pExpr: pRight, aff2: zAff[j])==SQLITE_AFF_BLOB ){
155479 zAff[j] = SQLITE_AFF_BLOB;
155480 }
155481 if( sqlite3ExprNeedsNoAffinityChange(p: pRight, aff: zAff[j]) ){
155482 zAff[j] = SQLITE_AFF_BLOB;
155483 }
155484 }
155485 }
155486 }
155487 *pzAff = zAff;
155488 return regBase;
155489}
155490
155491#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
155492/*
155493** If the most recently coded instruction is a constant range constraint
155494** (a string literal) that originated from the LIKE optimization, then
155495** set P3 and P5 on the OP_String opcode so that the string will be cast
155496** to a BLOB at appropriate times.
155497**
155498** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
155499** expression: "x>='ABC' AND x<'abd'". But this requires that the range
155500** scan loop run twice, once for strings and a second time for BLOBs.
155501** The OP_String opcodes on the second pass convert the upper and lower
155502** bound string constants to blobs. This routine makes the necessary changes
155503** to the OP_String opcodes for that to happen.
155504**
155505** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
155506** only the one pass through the string space is required, so this routine
155507** becomes a no-op.
155508*/
155509static void whereLikeOptimizationStringFixup(
155510 Vdbe *v, /* prepared statement under construction */
155511 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
155512 WhereTerm *pTerm /* The upper or lower bound just coded */
155513){
155514 if( pTerm->wtFlags & TERM_LIKEOPT ){
155515 VdbeOp *pOp;
155516 assert( pLevel->iLikeRepCntr>0 );
155517 pOp = sqlite3VdbeGetLastOp(p: v);
155518 assert( pOp!=0 );
155519 assert( pOp->opcode==OP_String8
155520 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
155521 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
155522 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
155523 }
155524}
155525#else
155526# define whereLikeOptimizationStringFixup(A,B,C)
155527#endif
155528
155529#ifdef SQLITE_ENABLE_CURSOR_HINTS
155530/*
155531** Information is passed from codeCursorHint() down to individual nodes of
155532** the expression tree (by sqlite3WalkExpr()) using an instance of this
155533** structure.
155534*/
155535struct CCurHint {
155536 int iTabCur; /* Cursor for the main table */
155537 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
155538 Index *pIdx; /* The index used to access the table */
155539};
155540
155541/*
155542** This function is called for every node of an expression that is a candidate
155543** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
155544** the table CCurHint.iTabCur, verify that the same column can be
155545** accessed through the index. If it cannot, then set pWalker->eCode to 1.
155546*/
155547static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
155548 struct CCurHint *pHint = pWalker->u.pCCurHint;
155549 assert( pHint->pIdx!=0 );
155550 if( pExpr->op==TK_COLUMN
155551 && pExpr->iTable==pHint->iTabCur
155552 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
155553 ){
155554 pWalker->eCode = 1;
155555 }
155556 return WRC_Continue;
155557}
155558
155559/*
155560** Test whether or not expression pExpr, which was part of a WHERE clause,
155561** should be included in the cursor-hint for a table that is on the rhs
155562** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
155563** expression is not suitable.
155564**
155565** An expression is unsuitable if it might evaluate to non NULL even if
155566** a TK_COLUMN node that does affect the value of the expression is set
155567** to NULL. For example:
155568**
155569** col IS NULL
155570** col IS NOT NULL
155571** coalesce(col, 1)
155572** CASE WHEN col THEN 0 ELSE 1 END
155573*/
155574static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
155575 if( pExpr->op==TK_IS
155576 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
155577 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
155578 ){
155579 pWalker->eCode = 1;
155580 }else if( pExpr->op==TK_FUNCTION ){
155581 int d1;
155582 char d2[4];
155583 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
155584 pWalker->eCode = 1;
155585 }
155586 }
155587
155588 return WRC_Continue;
155589}
155590
155591
155592/*
155593** This function is called on every node of an expression tree used as an
155594** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
155595** that accesses any table other than the one identified by
155596** CCurHint.iTabCur, then do the following:
155597**
155598** 1) allocate a register and code an OP_Column instruction to read
155599** the specified column into the new register, and
155600**
155601** 2) transform the expression node to a TK_REGISTER node that reads
155602** from the newly populated register.
155603**
155604** Also, if the node is a TK_COLUMN that does access the table identified
155605** by pCCurHint.iTabCur, and an index is being used (which we will
155606** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
155607** an access of the index rather than the original table.
155608*/
155609static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
155610 int rc = WRC_Continue;
155611 int reg;
155612 struct CCurHint *pHint = pWalker->u.pCCurHint;
155613 if( pExpr->op==TK_COLUMN ){
155614 if( pExpr->iTable!=pHint->iTabCur ){
155615 reg = ++pWalker->pParse->nMem; /* Register for column value */
155616 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
155617 pExpr->op = TK_REGISTER;
155618 pExpr->iTable = reg;
155619 }else if( pHint->pIdx!=0 ){
155620 pExpr->iTable = pHint->iIdxCur;
155621 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
155622 assert( pExpr->iColumn>=0 );
155623 }
155624 }else if( pExpr->pAggInfo ){
155625 rc = WRC_Prune;
155626 reg = ++pWalker->pParse->nMem; /* Register for column value */
155627 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
155628 pExpr->op = TK_REGISTER;
155629 pExpr->iTable = reg;
155630 }else if( pExpr->op==TK_TRUEFALSE ){
155631 /* Do not walk disabled expressions. tag-20230504-1 */
155632 return WRC_Prune;
155633 }
155634 return rc;
155635}
155636
155637/*
155638** Insert an OP_CursorHint instruction if it is appropriate to do so.
155639*/
155640static void codeCursorHint(
155641 SrcItem *pTabItem, /* FROM clause item */
155642 WhereInfo *pWInfo, /* The where clause */
155643 WhereLevel *pLevel, /* Which loop to provide hints for */
155644 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
155645){
155646 Parse *pParse = pWInfo->pParse;
155647 sqlite3 *db = pParse->db;
155648 Vdbe *v = pParse->pVdbe;
155649 Expr *pExpr = 0;
155650 WhereLoop *pLoop = pLevel->pWLoop;
155651 int iCur;
155652 WhereClause *pWC;
155653 WhereTerm *pTerm;
155654 int i, j;
155655 struct CCurHint sHint;
155656 Walker sWalker;
155657
155658 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
155659 iCur = pLevel->iTabCur;
155660 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
155661 sHint.iTabCur = iCur;
155662 sHint.iIdxCur = pLevel->iIdxCur;
155663 sHint.pIdx = pLoop->u.btree.pIndex;
155664 memset(&sWalker, 0, sizeof(sWalker));
155665 sWalker.pParse = pParse;
155666 sWalker.u.pCCurHint = &sHint;
155667 pWC = &pWInfo->sWC;
155668 for(i=0; i<pWC->nBase; i++){
155669 pTerm = &pWC->a[i];
155670 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
155671 if( pTerm->prereqAll & pLevel->notReady ) continue;
155672
155673 /* Any terms specified as part of the ON(...) clause for any LEFT
155674 ** JOIN for which the current table is not the rhs are omitted
155675 ** from the cursor-hint.
155676 **
155677 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
155678 ** that were specified as part of the WHERE clause must be excluded.
155679 ** This is to address the following:
155680 **
155681 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
155682 **
155683 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
155684 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
155685 ** pushed down to the cursor, this row is filtered out, causing
155686 ** SQLite to synthesize a row of NULL values. Which does match the
155687 ** WHERE clause, and so the query returns a row. Which is incorrect.
155688 **
155689 ** For the same reason, WHERE terms such as:
155690 **
155691 ** WHERE 1 = (t2.c IS NULL)
155692 **
155693 ** are also excluded. See codeCursorHintIsOrFunction() for details.
155694 */
155695 if( pTabItem->fg.jointype & JT_LEFT ){
155696 Expr *pExpr = pTerm->pExpr;
155697 if( !ExprHasProperty(pExpr, EP_OuterON)
155698 || pExpr->w.iJoin!=pTabItem->iCursor
155699 ){
155700 sWalker.eCode = 0;
155701 sWalker.xExprCallback = codeCursorHintIsOrFunction;
155702 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
155703 if( sWalker.eCode ) continue;
155704 }
155705 }else{
155706 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
155707 }
155708
155709 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
155710 ** the cursor. These terms are not needed as hints for a pure range
155711 ** scan (that has no == terms) so omit them. */
155712 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
155713 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
155714 if( j<pLoop->nLTerm ) continue;
155715 }
155716
155717 /* No subqueries or non-deterministic functions allowed */
155718 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
155719
155720 /* For an index scan, make sure referenced columns are actually in
155721 ** the index. */
155722 if( sHint.pIdx!=0 ){
155723 sWalker.eCode = 0;
155724 sWalker.xExprCallback = codeCursorHintCheckExpr;
155725 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
155726 if( sWalker.eCode ) continue;
155727 }
155728
155729 /* If we survive all prior tests, that means this term is worth hinting */
155730 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
155731 }
155732 if( pExpr!=0 ){
155733 sWalker.xExprCallback = codeCursorHintFixExpr;
155734 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
155735 sqlite3VdbeAddOp4(v, OP_CursorHint,
155736 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
155737 (const char*)pExpr, P4_EXPR);
155738 }
155739}
155740#else
155741# define codeCursorHint(A,B,C,D) /* No-op */
155742#endif /* SQLITE_ENABLE_CURSOR_HINTS */
155743
155744/*
155745** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
155746** a rowid value just read from cursor iIdxCur, open on index pIdx. This
155747** function generates code to do a deferred seek of cursor iCur to the
155748** rowid stored in register iRowid.
155749**
155750** Normally, this is just:
155751**
155752** OP_DeferredSeek $iCur $iRowid
155753**
155754** Which causes a seek on $iCur to the row with rowid $iRowid.
155755**
155756** However, if the scan currently being coded is a branch of an OR-loop and
155757** the statement currently being coded is a SELECT, then additional information
155758** is added that might allow OP_Column to omit the seek and instead do its
155759** lookup on the index, thus avoiding an expensive seek operation. To
155760** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur
155761** and P4 is set to an array of integers containing one entry for each column
155762** in the table. For each table column, if the column is the i'th
155763** column of the index, then the corresponding array entry is set to (i+1).
155764** If the column does not appear in the index at all, the array entry is set
155765** to 0. The OP_Column opcode can check this array to see if the column it
155766** wants is in the index and if it is, it will substitute the index cursor
155767** and column number and continue with those new values, rather than seeking
155768** the table cursor.
155769*/
155770static void codeDeferredSeek(
155771 WhereInfo *pWInfo, /* Where clause context */
155772 Index *pIdx, /* Index scan is using */
155773 int iCur, /* Cursor for IPK b-tree */
155774 int iIdxCur /* Index cursor */
155775){
155776 Parse *pParse = pWInfo->pParse; /* Parse context */
155777 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
155778
155779 assert( iIdxCur>0 );
155780 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
155781
155782 pWInfo->bDeferredSeek = 1;
155783 sqlite3VdbeAddOp3(p: v, OP_DeferredSeek, p1: iIdxCur, p2: 0, p3: iCur);
155784 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
155785 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
155786 ){
155787 int i;
155788 Table *pTab = pIdx->pTable;
155789 u32 *ai = (u32*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(u32)*(pTab->nCol+1));
155790 if( ai ){
155791 ai[0] = pTab->nCol;
155792 for(i=0; i<pIdx->nColumn-1; i++){
155793 int x1, x2;
155794 assert( pIdx->aiColumn[i]<pTab->nCol );
155795 x1 = pIdx->aiColumn[i];
155796 x2 = sqlite3TableColumnToStorage(pTab, iCol: x1);
155797 testcase( x1!=x2 );
155798 if( x1>=0 ) ai[x2+1] = i+1;
155799 }
155800 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char*)ai, P4_INTARRAY);
155801 }
155802 }
155803}
155804
155805/*
155806** If the expression passed as the second argument is a vector, generate
155807** code to write the first nReg elements of the vector into an array
155808** of registers starting with iReg.
155809**
155810** If the expression is not a vector, then nReg must be passed 1. In
155811** this case, generate code to evaluate the expression and leave the
155812** result in register iReg.
155813*/
155814static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
155815 assert( nReg>0 );
155816 if( p && sqlite3ExprIsVector(pExpr: p) ){
155817#ifndef SQLITE_OMIT_SUBQUERY
155818 if( ExprUseXSelect(p) ){
155819 Vdbe *v = pParse->pVdbe;
155820 int iSelect;
155821 assert( p->op==TK_SELECT );
155822 iSelect = sqlite3CodeSubselect(pParse, pExpr: p);
155823 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: iSelect, p2: iReg, p3: nReg-1);
155824 }else
155825#endif
155826 {
155827 int i;
155828 const ExprList *pList;
155829 assert( ExprUseXList(p) );
155830 pList = p->x.pList;
155831 assert( nReg<=pList->nExpr );
155832 for(i=0; i<nReg; i++){
155833 sqlite3ExprCode(pParse, pExpr: pList->a[i].pExpr, target: iReg+i);
155834 }
155835 }
155836 }else{
155837 assert( nReg==1 || pParse->nErr );
155838 sqlite3ExprCode(pParse, pExpr: p, target: iReg);
155839 }
155840}
155841
155842/*
155843** The pTruth expression is always true because it is the WHERE clause
155844** a partial index that is driving a query loop. Look through all of the
155845** WHERE clause terms on the query, and if any of those terms must be
155846** true because pTruth is true, then mark those WHERE clause terms as
155847** coded.
155848*/
155849static void whereApplyPartialIndexConstraints(
155850 Expr *pTruth,
155851 int iTabCur,
155852 WhereClause *pWC
155853){
155854 int i;
155855 WhereTerm *pTerm;
155856 while( pTruth->op==TK_AND ){
155857 whereApplyPartialIndexConstraints(pTruth: pTruth->pLeft, iTabCur, pWC);
155858 pTruth = pTruth->pRight;
155859 }
155860 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155861 Expr *pExpr;
155862 if( pTerm->wtFlags & TERM_CODED ) continue;
155863 pExpr = pTerm->pExpr;
155864 if( sqlite3ExprCompare(pParse: 0, pA: pExpr, pB: pTruth, iTab: iTabCur)==0 ){
155865 pTerm->wtFlags |= TERM_CODED;
155866 }
155867 }
155868}
155869
155870/*
155871** This routine is called right after An OP_Filter has been generated and
155872** before the corresponding index search has been performed. This routine
155873** checks to see if there are additional Bloom filters in inner loops that
155874** can be checked prior to doing the index lookup. If there are available
155875** inner-loop Bloom filters, then evaluate those filters now, before the
155876** index lookup. The idea is that a Bloom filter check is way faster than
155877** an index lookup, and the Bloom filter might return false, meaning that
155878** the index lookup can be skipped.
155879**
155880** We know that an inner loop uses a Bloom filter because it has the
155881** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
155882** then clear the WhereLevel.regFilter value to prevent the Bloom filter
155883** from being checked a second time when the inner loop is evaluated.
155884*/
155885static SQLITE_NOINLINE void filterPullDown(
155886 Parse *pParse, /* Parsing context */
155887 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
155888 int iLevel, /* Which level of pWInfo->a[] should be coded */
155889 int addrNxt, /* Jump here to bypass inner loops */
155890 Bitmask notReady /* Loops that are not ready */
155891){
155892 while( ++iLevel < pWInfo->nLevel ){
155893 WhereLevel *pLevel = &pWInfo->a[iLevel];
155894 WhereLoop *pLoop = pLevel->pWLoop;
155895 if( pLevel->regFilter==0 ) continue;
155896 if( pLevel->pWLoop->nSkip ) continue;
155897 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
155898 ** vvvvv--' pLevel->regFilter if this were true. */
155899 if( NEVER(pLoop->prereq & notReady) ) continue;
155900 assert( pLevel->addrBrk==0 );
155901 pLevel->addrBrk = addrNxt;
155902 if( pLoop->wsFlags & WHERE_IPK ){
155903 WhereTerm *pTerm = pLoop->aLTerm[0];
155904 int regRowid;
155905 assert( pTerm!=0 );
155906 assert( pTerm->pExpr!=0 );
155907 testcase( pTerm->wtFlags & TERM_VIRTUAL );
155908 regRowid = sqlite3GetTempReg(pParse);
155909 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, iEq: 0, bRev: 0, iTarget: regRowid);
155910 sqlite3VdbeAddOp2(p: pParse->pVdbe, OP_MustBeInt, p1: regRowid, p2: addrNxt);
155911 VdbeCoverage(pParse->pVdbe);
155912 sqlite3VdbeAddOp4Int(p: pParse->pVdbe, OP_Filter, p1: pLevel->regFilter,
155913 p2: addrNxt, p3: regRowid, p4: 1);
155914 VdbeCoverage(pParse->pVdbe);
155915 }else{
155916 u16 nEq = pLoop->u.btree.nEq;
155917 int r1;
155918 char *zStartAff;
155919
155920 assert( pLoop->wsFlags & WHERE_INDEXED );
155921 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
155922 r1 = codeAllEqualityTerms(pParse,pLevel,bRev: 0,nExtraReg: 0,pzAff: &zStartAff);
155923 codeApplyAffinity(pParse, base: r1, n: nEq, zAff: zStartAff);
155924 sqlite3DbFree(db: pParse->db, p: zStartAff);
155925 sqlite3VdbeAddOp4Int(p: pParse->pVdbe, OP_Filter, p1: pLevel->regFilter,
155926 p2: addrNxt, p3: r1, p4: nEq);
155927 VdbeCoverage(pParse->pVdbe);
155928 }
155929 pLevel->regFilter = 0;
155930 pLevel->addrBrk = 0;
155931 }
155932}
155933
155934/*
155935** Generate code for the start of the iLevel-th loop in the WHERE clause
155936** implementation described by pWInfo.
155937*/
155938SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
155939 Parse *pParse, /* Parsing context */
155940 Vdbe *v, /* Prepared statement under construction */
155941 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
155942 int iLevel, /* Which level of pWInfo->a[] should be coded */
155943 WhereLevel *pLevel, /* The current level pointer */
155944 Bitmask notReady /* Which tables are currently available */
155945){
155946 int j, k; /* Loop counters */
155947 int iCur; /* The VDBE cursor for the table */
155948 int addrNxt; /* Where to jump to continue with the next IN case */
155949 int bRev; /* True if we need to scan in reverse order */
155950 WhereLoop *pLoop; /* The WhereLoop object being coded */
155951 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
155952 WhereTerm *pTerm; /* A WHERE clause term */
155953 sqlite3 *db; /* Database connection */
155954 SrcItem *pTabItem; /* FROM clause term being coded */
155955 int addrBrk; /* Jump here to break out of the loop */
155956 int addrHalt; /* addrBrk for the outermost loop */
155957 int addrCont; /* Jump here to continue with next cycle */
155958 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
155959 int iReleaseReg = 0; /* Temp register to free before returning */
155960 Index *pIdx = 0; /* Index used by loop (if any) */
155961 int iLoop; /* Iteration of constraint generator loop */
155962
155963 pWC = &pWInfo->sWC;
155964 db = pParse->db;
155965 pLoop = pLevel->pWLoop;
155966 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
155967 iCur = pTabItem->iCursor;
155968 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
155969 bRev = (pWInfo->revMask>>iLevel)&1;
155970 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
155971#if WHERETRACE_ENABLED /* 0x4001 */
155972 if( sqlite3WhereTrace & 0x1 ){
155973 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
155974 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
155975 if( sqlite3WhereTrace & 0x1000 ){
155976 sqlite3WhereLoopPrint(pLoop, pWC);
155977 }
155978 }
155979 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
155980 if( iLevel==0 ){
155981 sqlite3DebugPrintf("WHERE clause being coded:\n");
155982 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
155983 }
155984 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
155985 sqlite3WhereClausePrint(pWC);
155986 }
155987#endif
155988
155989 /* Create labels for the "break" and "continue" instructions
155990 ** for the current loop. Jump to addrBrk to break out of a loop.
155991 ** Jump to cont to go immediately to the next iteration of the
155992 ** loop.
155993 **
155994 ** When there is an IN operator, we also have a "addrNxt" label that
155995 ** means to continue with the next IN value combination. When
155996 ** there are no IN operators in the constraints, the "addrNxt" label
155997 ** is the same as "addrBrk".
155998 */
155999 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
156000 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
156001
156002 /* If this is the right table of a LEFT OUTER JOIN, allocate and
156003 ** initialize a memory cell that records if this table matches any
156004 ** row of the left table of the join.
156005 */
156006 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
156007 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
156008 );
156009 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
156010 pLevel->iLeftJoin = ++pParse->nMem;
156011 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pLevel->iLeftJoin);
156012 VdbeComment((v, "init LEFT JOIN no-match flag"));
156013 }
156014
156015 /* Compute a safe address to jump to if we discover that the table for
156016 ** this loop is empty and can never contribute content. */
156017 for(j=iLevel; j>0; j--){
156018 if( pWInfo->a[j].iLeftJoin ) break;
156019 if( pWInfo->a[j].pRJ ) break;
156020 }
156021 addrHalt = pWInfo->a[j].addrBrk;
156022
156023 /* Special case of a FROM clause subquery implemented as a co-routine */
156024 if( pTabItem->fg.viaCoroutine ){
156025 int regYield = pTabItem->regReturn;
156026 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: pTabItem->addrFillSub);
156027 pLevel->p2 = sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regYield, p2: addrBrk);
156028 VdbeCoverage(v);
156029 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
156030 pLevel->op = OP_Goto;
156031 }else
156032
156033#ifndef SQLITE_OMIT_VIRTUALTABLE
156034 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
156035 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
156036 ** to access the data.
156037 */
156038 int iReg; /* P3 Value for OP_VFilter */
156039 int addrNotFound;
156040 int nConstraint = pLoop->nLTerm;
156041
156042 iReg = sqlite3GetTempRange(pParse, nReg: nConstraint+2);
156043 addrNotFound = pLevel->addrBrk;
156044 for(j=0; j<nConstraint; j++){
156045 int iTarget = iReg+j+2;
156046 pTerm = pLoop->aLTerm[j];
156047 if( NEVER(pTerm==0) ) continue;
156048 if( pTerm->eOperator & WO_IN ){
156049 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
156050 int iTab = pParse->nTab++;
156051 int iCache = ++pParse->nMem;
156052 sqlite3CodeRhsOfIN(pParse, pExpr: pTerm->pExpr, iTab);
156053 sqlite3VdbeAddOp3(p: v, OP_VInitIn, p1: iTab, p2: iTarget, p3: iCache);
156054 }else{
156055 codeEqualityTerm(pParse, pTerm, pLevel, iEq: j, bRev, iTarget);
156056 addrNotFound = pLevel->addrNxt;
156057 }
156058 }else{
156059 Expr *pRight = pTerm->pExpr->pRight;
156060 codeExprOrVector(pParse, p: pRight, iReg: iTarget, nReg: 1);
156061 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
156062 && pLoop->u.vtab.bOmitOffset
156063 ){
156064 assert( pTerm->eOperator==WO_AUX );
156065 assert( pWInfo->pSelect!=0 );
156066 assert( pWInfo->pSelect->iOffset>0 );
156067 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWInfo->pSelect->iOffset);
156068 VdbeComment((v,"Zero OFFSET counter"));
156069 }
156070 }
156071 }
156072 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: pLoop->u.vtab.idxNum, p2: iReg);
156073 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: nConstraint, p2: iReg+1);
156074 sqlite3VdbeAddOp4(p: v, OP_VFilter, p1: iCur, p2: addrNotFound, p3: iReg,
156075 zP4: pLoop->u.vtab.idxStr,
156076 p4type: pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
156077 VdbeCoverage(v);
156078 pLoop->u.vtab.needFree = 0;
156079 /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed
156080 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
156081 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
156082 pLevel->p1 = iCur;
156083 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
156084 pLevel->p2 = sqlite3VdbeCurrentAddr(p: v);
156085 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
156086
156087 for(j=0; j<nConstraint; j++){
156088 pTerm = pLoop->aLTerm[j];
156089 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
156090 disableTerm(pLevel, pTerm);
156091 continue;
156092 }
156093 if( (pTerm->eOperator & WO_IN)!=0
156094 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
156095 && !db->mallocFailed
156096 ){
156097 Expr *pCompare; /* The comparison operator */
156098 Expr *pRight; /* RHS of the comparison */
156099 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
156100 int iIn; /* IN loop corresponding to the j-th constraint */
156101
156102 /* Reload the constraint value into reg[iReg+j+2]. The same value
156103 ** was loaded into the same register prior to the OP_VFilter, but
156104 ** the xFilter implementation might have changed the datatype or
156105 ** encoding of the value in the register, so it *must* be reloaded.
156106 */
156107 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
156108 pOp = sqlite3VdbeGetOp(p: v, addr: pLevel->u.in.aInLoop[iIn].addrInTop);
156109 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
156110 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
156111 ){
156112 testcase( pOp->opcode==OP_Rowid );
156113 sqlite3VdbeAddOp3(p: v, op: pOp->opcode, p1: pOp->p1, p2: pOp->p2, p3: pOp->p3);
156114 break;
156115 }
156116 }
156117
156118 /* Generate code that will continue to the next row if
156119 ** the IN constraint is not satisfied
156120 */
156121 pCompare = sqlite3PExpr(pParse, TK_EQ, pLeft: 0, pRight: 0);
156122 if( !db->mallocFailed ){
156123 int iFld = pTerm->u.x.iField;
156124 Expr *pLeft = pTerm->pExpr->pLeft;
156125 assert( pLeft!=0 );
156126 if( iFld>0 ){
156127 assert( pLeft->op==TK_VECTOR );
156128 assert( ExprUseXList(pLeft) );
156129 assert( iFld<=pLeft->x.pList->nExpr );
156130 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
156131 }else{
156132 pCompare->pLeft = pLeft;
156133 }
156134 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, zToken: 0);
156135 if( pRight ){
156136 pRight->iTable = iReg+j+2;
156137 sqlite3ExprIfFalse(
156138 pParse, pExpr: pCompare, dest: pLevel->addrCont, SQLITE_JUMPIFNULL
156139 );
156140 }
156141 pCompare->pLeft = 0;
156142 }
156143 sqlite3ExprDelete(db, p: pCompare);
156144 }
156145 }
156146
156147 /* These registers need to be preserved in case there is an IN operator
156148 ** loop. So we could deallocate the registers here (and potentially
156149 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
156150 ** simpler and safer to simply not reuse the registers.
156151 **
156152 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
156153 */
156154 }else
156155#endif /* SQLITE_OMIT_VIRTUALTABLE */
156156
156157 if( (pLoop->wsFlags & WHERE_IPK)!=0
156158 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
156159 ){
156160 /* Case 2: We can directly reference a single row using an
156161 ** equality comparison against the ROWID field. Or
156162 ** we reference multiple rows using a "rowid IN (...)"
156163 ** construct.
156164 */
156165 assert( pLoop->u.btree.nEq==1 );
156166 pTerm = pLoop->aLTerm[0];
156167 assert( pTerm!=0 );
156168 assert( pTerm->pExpr!=0 );
156169 testcase( pTerm->wtFlags & TERM_VIRTUAL );
156170 iReleaseReg = ++pParse->nMem;
156171 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iEq: 0, bRev, iTarget: iReleaseReg);
156172 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReg: iReleaseReg);
156173 addrNxt = pLevel->addrNxt;
156174 if( pLevel->regFilter ){
156175 sqlite3VdbeAddOp2(p: v, OP_MustBeInt, p1: iRowidReg, p2: addrNxt);
156176 VdbeCoverage(v);
156177 sqlite3VdbeAddOp4Int(p: v, OP_Filter, p1: pLevel->regFilter, p2: addrNxt,
156178 p3: iRowidReg, p4: 1);
156179 VdbeCoverage(v);
156180 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
156181 }
156182 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: iCur, p2: addrNxt, p3: iRowidReg);
156183 VdbeCoverage(v);
156184 pLevel->op = OP_Noop;
156185 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
156186 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
156187 ){
156188 /* Case 3: We have an inequality comparison against the ROWID field.
156189 */
156190 int testOp = OP_Noop;
156191 int start;
156192 int memEndValue = 0;
156193 WhereTerm *pStart, *pEnd;
156194
156195 j = 0;
156196 pStart = pEnd = 0;
156197 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
156198 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
156199 assert( pStart!=0 || pEnd!=0 );
156200 if( bRev ){
156201 pTerm = pStart;
156202 pStart = pEnd;
156203 pEnd = pTerm;
156204 }
156205 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
156206 if( pStart ){
156207 Expr *pX; /* The expression that defines the start bound */
156208 int r1, rTemp; /* Registers for holding the start boundary */
156209 int op; /* Cursor seek operation */
156210
156211 /* The following constant maps TK_xx codes into corresponding
156212 ** seek opcodes. It depends on a particular ordering of TK_xx
156213 */
156214 const u8 aMoveOp[] = {
156215 /* TK_GT */ OP_SeekGT,
156216 /* TK_LE */ OP_SeekLE,
156217 /* TK_LT */ OP_SeekLT,
156218 /* TK_GE */ OP_SeekGE
156219 };
156220 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
156221 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
156222 assert( TK_GE==TK_GT+3 ); /* ... is correct. */
156223
156224 assert( (pStart->wtFlags & TERM_VNULL)==0 );
156225 testcase( pStart->wtFlags & TERM_VIRTUAL );
156226 pX = pStart->pExpr;
156227 assert( pX!=0 );
156228 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
156229 if( sqlite3ExprIsVector(pExpr: pX->pRight) ){
156230 r1 = rTemp = sqlite3GetTempReg(pParse);
156231 codeExprOrVector(pParse, p: pX->pRight, iReg: r1, nReg: 1);
156232 testcase( pX->op==TK_GT );
156233 testcase( pX->op==TK_GE );
156234 testcase( pX->op==TK_LT );
156235 testcase( pX->op==TK_LE );
156236 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
156237 assert( pX->op!=TK_GT || op==OP_SeekGE );
156238 assert( pX->op!=TK_GE || op==OP_SeekGE );
156239 assert( pX->op!=TK_LT || op==OP_SeekLE );
156240 assert( pX->op!=TK_LE || op==OP_SeekLE );
156241 }else{
156242 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pX->pRight, pReg: &rTemp);
156243 disableTerm(pLevel, pTerm: pStart);
156244 op = aMoveOp[(pX->op - TK_GT)];
156245 }
156246 sqlite3VdbeAddOp3(p: v, op, p1: iCur, p2: addrBrk, p3: r1);
156247 VdbeComment((v, "pk"));
156248 VdbeCoverageIf(v, pX->op==TK_GT);
156249 VdbeCoverageIf(v, pX->op==TK_LE);
156250 VdbeCoverageIf(v, pX->op==TK_LT);
156251 VdbeCoverageIf(v, pX->op==TK_GE);
156252 sqlite3ReleaseTempReg(pParse, iReg: rTemp);
156253 }else{
156254 sqlite3VdbeAddOp2(p: v, op: bRev ? OP_Last : OP_Rewind, p1: iCur, p2: addrHalt);
156255 VdbeCoverageIf(v, bRev==0);
156256 VdbeCoverageIf(v, bRev!=0);
156257 }
156258 if( pEnd ){
156259 Expr *pX;
156260 pX = pEnd->pExpr;
156261 assert( pX!=0 );
156262 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
156263 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
156264 testcase( pEnd->wtFlags & TERM_VIRTUAL );
156265 memEndValue = ++pParse->nMem;
156266 codeExprOrVector(pParse, p: pX->pRight, iReg: memEndValue, nReg: 1);
156267 if( 0==sqlite3ExprIsVector(pExpr: pX->pRight)
156268 && (pX->op==TK_LT || pX->op==TK_GT)
156269 ){
156270 testOp = bRev ? OP_Le : OP_Ge;
156271 }else{
156272 testOp = bRev ? OP_Lt : OP_Gt;
156273 }
156274 if( 0==sqlite3ExprIsVector(pExpr: pX->pRight) ){
156275 disableTerm(pLevel, pTerm: pEnd);
156276 }
156277 }
156278 start = sqlite3VdbeCurrentAddr(p: v);
156279 pLevel->op = bRev ? OP_Prev : OP_Next;
156280 pLevel->p1 = iCur;
156281 pLevel->p2 = start;
156282 assert( pLevel->p5==0 );
156283 if( testOp!=OP_Noop ){
156284 iRowidReg = ++pParse->nMem;
156285 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCur, p2: iRowidReg);
156286 sqlite3VdbeAddOp3(p: v, op: testOp, p1: memEndValue, p2: addrBrk, p3: iRowidReg);
156287 VdbeCoverageIf(v, testOp==OP_Le);
156288 VdbeCoverageIf(v, testOp==OP_Lt);
156289 VdbeCoverageIf(v, testOp==OP_Ge);
156290 VdbeCoverageIf(v, testOp==OP_Gt);
156291 sqlite3VdbeChangeP5(p: v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
156292 }
156293 }else if( pLoop->wsFlags & WHERE_INDEXED ){
156294 /* Case 4: A scan using an index.
156295 **
156296 ** The WHERE clause may contain zero or more equality
156297 ** terms ("==" or "IN" operators) that refer to the N
156298 ** left-most columns of the index. It may also contain
156299 ** inequality constraints (>, <, >= or <=) on the indexed
156300 ** column that immediately follows the N equalities. Only
156301 ** the right-most column can be an inequality - the rest must
156302 ** use the "==" and "IN" operators. For example, if the
156303 ** index is on (x,y,z), then the following clauses are all
156304 ** optimized:
156305 **
156306 ** x=5
156307 ** x=5 AND y=10
156308 ** x=5 AND y<10
156309 ** x=5 AND y>5 AND y<10
156310 ** x=5 AND y=5 AND z<=10
156311 **
156312 ** The z<10 term of the following cannot be used, only
156313 ** the x=5 term:
156314 **
156315 ** x=5 AND z<10
156316 **
156317 ** N may be zero if there are inequality constraints.
156318 ** If there are no inequality constraints, then N is at
156319 ** least one.
156320 **
156321 ** This case is also used when there are no WHERE clause
156322 ** constraints but an index is selected anyway, in order
156323 ** to force the output order to conform to an ORDER BY.
156324 */
156325 static const u8 aStartOp[] = {
156326 0,
156327 0,
156328 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
156329 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
156330 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
156331 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
156332 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
156333 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
156334 };
156335 static const u8 aEndOp[] = {
156336 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
156337 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
156338 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
156339 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
156340 };
156341 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
156342 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
156343 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
156344 int regBase; /* Base register holding constraint values */
156345 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
156346 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
156347 int startEq; /* True if range start uses ==, >= or <= */
156348 int endEq; /* True if range end uses ==, >= or <= */
156349 int start_constraints; /* Start of range is constrained */
156350 int nConstraint; /* Number of constraint terms */
156351 int iIdxCur; /* The VDBE cursor for the index */
156352 int nExtraReg = 0; /* Number of extra registers needed */
156353 int op; /* Instruction opcode */
156354 char *zStartAff; /* Affinity for start of range constraint */
156355 char *zEndAff = 0; /* Affinity for end of range constraint */
156356 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
156357 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
156358 int omitTable; /* True if we use the index only */
156359 int regBignull = 0; /* big-null flag register */
156360 int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */
156361
156362 pIdx = pLoop->u.btree.pIndex;
156363 iIdxCur = pLevel->iIdxCur;
156364 assert( nEq>=pLoop->nSkip );
156365
156366 /* Find any inequality constraint terms for the start and end
156367 ** of the range.
156368 */
156369 j = nEq;
156370 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
156371 pRangeStart = pLoop->aLTerm[j++];
156372 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
156373 /* Like optimization range constraints always occur in pairs */
156374 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
156375 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
156376 }
156377 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
156378 pRangeEnd = pLoop->aLTerm[j++];
156379 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
156380#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
156381 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
156382 assert( pRangeStart!=0 ); /* LIKE opt constraints */
156383 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
156384 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
156385 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: (int)pLevel->iLikeRepCntr);
156386 VdbeComment((v, "LIKE loop counter"));
156387 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(p: v);
156388 /* iLikeRepCntr actually stores 2x the counter register number. The
156389 ** bottom bit indicates whether the search order is ASC or DESC. */
156390 testcase( bRev );
156391 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
156392 assert( (bRev & ~1)==0 );
156393 pLevel->iLikeRepCntr <<=1;
156394 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
156395 }
156396#endif
156397 if( pRangeStart==0 ){
156398 j = pIdx->aiColumn[nEq];
156399 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
156400 bSeekPastNull = 1;
156401 }
156402 }
156403 }
156404 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
156405
156406 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
156407 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
156408 ** FIRST). In both cases separate ordered scans are made of those
156409 ** index entries for which the column is null and for those for which
156410 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
156411 ** For DESC, NULL entries are scanned first.
156412 */
156413 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
156414 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
156415 ){
156416 assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 );
156417 assert( pRangeEnd==0 && pRangeStart==0 );
156418 testcase( pLoop->nSkip>0 );
156419 nExtraReg = 1;
156420 bSeekPastNull = 1;
156421 pLevel->regBignull = regBignull = ++pParse->nMem;
156422 if( pLevel->iLeftJoin ){
156423 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regBignull);
156424 }
156425 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
156426 }
156427
156428 /* If we are doing a reverse order scan on an ascending index, or
156429 ** a forward order scan on a descending index, interchange the
156430 ** start and end terms (pRangeStart and pRangeEnd).
156431 */
156432 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
156433 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
156434 SWAP(u8, bSeekPastNull, bStopAtNull);
156435 SWAP(u8, nBtm, nTop);
156436 }
156437
156438 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
156439 /* In case OP_SeekScan is used, ensure that the index cursor does not
156440 ** point to a valid row for the first iteration of this loop. */
156441 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: iIdxCur);
156442 }
156443
156444 /* Generate code to evaluate all constraint terms using == or IN
156445 ** and store the values of those terms in an array of registers
156446 ** starting at regBase.
156447 */
156448 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
156449 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,pzAff: &zStartAff);
156450 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
156451 if( zStartAff && nTop ){
156452 zEndAff = sqlite3DbStrDup(db, z: &zStartAff[nEq]);
156453 }
156454 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
156455
156456 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
156457 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
156458 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
156459 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
156460 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
156461 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
156462 start_constraints = pRangeStart || nEq>0;
156463
156464 /* Seek the index cursor to the start of the range. */
156465 nConstraint = nEq;
156466 if( pRangeStart ){
156467 Expr *pRight = pRangeStart->pExpr->pRight;
156468 codeExprOrVector(pParse, p: pRight, iReg: regBase+nEq, nReg: nBtm);
156469 whereLikeOptimizationStringFixup(v, pLevel, pTerm: pRangeStart);
156470 if( (pRangeStart->wtFlags & TERM_VNULL)==0
156471 && sqlite3ExprCanBeNull(p: pRight)
156472 ){
156473 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regBase+nEq, p2: addrNxt);
156474 VdbeCoverage(v);
156475 }
156476 if( zStartAff ){
156477 updateRangeAffinityStr(pRight, n: nBtm, zAff: &zStartAff[nEq]);
156478 }
156479 nConstraint += nBtm;
156480 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
156481 if( sqlite3ExprIsVector(pExpr: pRight)==0 ){
156482 disableTerm(pLevel, pTerm: pRangeStart);
156483 }else{
156484 startEq = 1;
156485 }
156486 bSeekPastNull = 0;
156487 }else if( bSeekPastNull ){
156488 startEq = 0;
156489 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regBase+nEq);
156490 start_constraints = 1;
156491 nConstraint++;
156492 }else if( regBignull ){
156493 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regBase+nEq);
156494 start_constraints = 1;
156495 nConstraint++;
156496 }
156497 codeApplyAffinity(pParse, base: regBase, n: nConstraint - bSeekPastNull, zAff: zStartAff);
156498 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
156499 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
156500 ** above has already left the cursor sitting on the correct row,
156501 ** so no further seeking is needed */
156502 }else{
156503 if( regBignull ){
156504 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: regBignull);
156505 VdbeComment((v, "NULL-scan pass ctr"));
156506 }
156507 if( pLevel->regFilter ){
156508 sqlite3VdbeAddOp4Int(p: v, OP_Filter, p1: pLevel->regFilter, p2: addrNxt,
156509 p3: regBase, p4: nEq);
156510 VdbeCoverage(v);
156511 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
156512 }
156513
156514 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
156515 assert( op!=0 );
156516 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
156517 assert( regBignull==0 );
156518 /* TUNING: The OP_SeekScan opcode seeks to reduce the number
156519 ** of expensive seek operations by replacing a single seek with
156520 ** 1 or more step operations. The question is, how many steps
156521 ** should we try before giving up and going with a seek. The cost
156522 ** of a seek is proportional to the logarithm of the of the number
156523 ** of entries in the tree, so basing the number of steps to try
156524 ** on the estimated number of rows in the btree seems like a good
156525 ** guess. */
156526 addrSeekScan = sqlite3VdbeAddOp1(p: v, OP_SeekScan,
156527 p1: (pIdx->aiRowLogEst[0]+9)/10);
156528 if( pRangeStart || pRangeEnd ){
156529 sqlite3VdbeChangeP5(p: v, p5: 1);
156530 sqlite3VdbeChangeP2(p: v, addr: addrSeekScan, val: sqlite3VdbeCurrentAddr(p: v)+1);
156531 addrSeekScan = 0;
156532 }
156533 VdbeCoverage(v);
156534 }
156535 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase, p4: nConstraint);
156536 VdbeCoverage(v);
156537 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
156538 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
156539 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
156540 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
156541 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
156542 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
156543
156544 assert( bSeekPastNull==0 || bStopAtNull==0 );
156545 if( regBignull ){
156546 assert( bSeekPastNull==1 || bStopAtNull==1 );
156547 assert( bSeekPastNull==!bStopAtNull );
156548 assert( bStopAtNull==startEq );
156549 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: sqlite3VdbeCurrentAddr(p: v)+2);
156550 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
156551 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase,
156552 p4: nConstraint-startEq);
156553 VdbeCoverage(v);
156554 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
156555 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
156556 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
156557 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
156558 assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE);
156559 }
156560 }
156561
156562 /* Load the value for the inequality constraint at the end of the
156563 ** range (if any).
156564 */
156565 nConstraint = nEq;
156566 assert( pLevel->p2==0 );
156567 if( pRangeEnd ){
156568 Expr *pRight = pRangeEnd->pExpr->pRight;
156569 assert( addrSeekScan==0 );
156570 codeExprOrVector(pParse, p: pRight, iReg: regBase+nEq, nReg: nTop);
156571 whereLikeOptimizationStringFixup(v, pLevel, pTerm: pRangeEnd);
156572 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
156573 && sqlite3ExprCanBeNull(p: pRight)
156574 ){
156575 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regBase+nEq, p2: addrNxt);
156576 VdbeCoverage(v);
156577 }
156578 if( zEndAff ){
156579 updateRangeAffinityStr(pRight, n: nTop, zAff: zEndAff);
156580 codeApplyAffinity(pParse, base: regBase+nEq, n: nTop, zAff: zEndAff);
156581 }else{
156582 assert( pParse->db->mallocFailed );
156583 }
156584 nConstraint += nTop;
156585 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
156586
156587 if( sqlite3ExprIsVector(pExpr: pRight)==0 ){
156588 disableTerm(pLevel, pTerm: pRangeEnd);
156589 }else{
156590 endEq = 1;
156591 }
156592 }else if( bStopAtNull ){
156593 if( regBignull==0 ){
156594 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regBase+nEq);
156595 endEq = 0;
156596 }
156597 nConstraint++;
156598 }
156599 if( zStartAff ) sqlite3DbNNFreeNN(db, p: zStartAff);
156600 if( zEndAff ) sqlite3DbNNFreeNN(db, p: zEndAff);
156601
156602 /* Top of the loop body */
156603 pLevel->p2 = sqlite3VdbeCurrentAddr(p: v);
156604
156605 /* Check if the index cursor is past the end of the range. */
156606 if( nConstraint ){
156607 if( regBignull ){
156608 /* Except, skip the end-of-range check while doing the NULL-scan */
156609 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: regBignull, p2: sqlite3VdbeCurrentAddr(p: v)+3);
156610 VdbeComment((v, "If NULL-scan 2nd pass"));
156611 VdbeCoverage(v);
156612 }
156613 op = aEndOp[bRev*2 + endEq];
156614 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase, p4: nConstraint);
156615 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
156616 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
156617 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
156618 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
156619 if( addrSeekScan ) sqlite3VdbeJumpHere(p: v, addr: addrSeekScan);
156620 }
156621 if( regBignull ){
156622 /* During a NULL-scan, check to see if we have reached the end of
156623 ** the NULLs */
156624 assert( bSeekPastNull==!bStopAtNull );
156625 assert( bSeekPastNull+bStopAtNull==1 );
156626 assert( nConstraint+bSeekPastNull>0 );
156627 sqlite3VdbeAddOp2(p: v, OP_If, p1: regBignull, p2: sqlite3VdbeCurrentAddr(p: v)+2);
156628 VdbeComment((v, "If NULL-scan 1st pass"));
156629 VdbeCoverage(v);
156630 op = aEndOp[bRev*2 + bSeekPastNull];
156631 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase,
156632 p4: nConstraint+bSeekPastNull);
156633 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
156634 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
156635 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
156636 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
156637 }
156638
156639 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
156640 sqlite3VdbeAddOp3(p: v, OP_SeekHit, p1: iIdxCur, p2: nEq, p3: nEq);
156641 }
156642
156643 /* Seek the table cursor, if required */
156644 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
156645 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
156646 if( omitTable ){
156647 /* pIdx is a covering index. No need to access the main table. */
156648 }else if( HasRowid(pIdx->pTable) ){
156649 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
156650 }else if( iCur!=iIdxCur ){
156651 Index *pPk = sqlite3PrimaryKeyIndex(pTab: pIdx->pTable);
156652 iRowidReg = sqlite3GetTempRange(pParse, nReg: pPk->nKeyCol);
156653 for(j=0; j<pPk->nKeyCol; j++){
156654 k = sqlite3TableColumnToIndex(pIdx, iCol: pPk->aiColumn[j]);
156655 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: k, p3: iRowidReg+j);
156656 }
156657 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iCur, p2: addrCont,
156658 p3: iRowidReg, p4: pPk->nKeyCol); VdbeCoverage(v);
156659 }
156660
156661 if( pLevel->iLeftJoin==0 ){
156662 /* If a partial index is driving the loop, try to eliminate WHERE clause
156663 ** terms from the query that must be true due to the WHERE clause of
156664 ** the partial index.
156665 **
156666 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
156667 ** for a LEFT JOIN.
156668 */
156669 if( pIdx->pPartIdxWhere ){
156670 whereApplyPartialIndexConstraints(pTruth: pIdx->pPartIdxWhere, iTabCur: iCur, pWC);
156671 }
156672 }else{
156673 testcase( pIdx->pPartIdxWhere );
156674 /* The following assert() is not a requirement, merely an observation:
156675 ** The OR-optimization doesn't work for the right hand table of
156676 ** a LEFT JOIN: */
156677 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
156678 }
156679
156680 /* Record the instruction used to terminate the loop. */
156681 if( pLoop->wsFlags & WHERE_ONEROW ){
156682 pLevel->op = OP_Noop;
156683 }else if( bRev ){
156684 pLevel->op = OP_Prev;
156685 }else{
156686 pLevel->op = OP_Next;
156687 }
156688 pLevel->p1 = iIdxCur;
156689 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
156690 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
156691 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
156692 }else{
156693 assert( pLevel->p5==0 );
156694 }
156695 if( omitTable ) pIdx = 0;
156696 }else
156697
156698#ifndef SQLITE_OMIT_OR_OPTIMIZATION
156699 if( pLoop->wsFlags & WHERE_MULTI_OR ){
156700 /* Case 5: Two or more separately indexed terms connected by OR
156701 **
156702 ** Example:
156703 **
156704 ** CREATE TABLE t1(a,b,c,d);
156705 ** CREATE INDEX i1 ON t1(a);
156706 ** CREATE INDEX i2 ON t1(b);
156707 ** CREATE INDEX i3 ON t1(c);
156708 **
156709 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
156710 **
156711 ** In the example, there are three indexed terms connected by OR.
156712 ** The top of the loop looks like this:
156713 **
156714 ** Null 1 # Zero the rowset in reg 1
156715 **
156716 ** Then, for each indexed term, the following. The arguments to
156717 ** RowSetTest are such that the rowid of the current row is inserted
156718 ** into the RowSet. If it is already present, control skips the
156719 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
156720 **
156721 ** sqlite3WhereBegin(<term>)
156722 ** RowSetTest # Insert rowid into rowset
156723 ** Gosub 2 A
156724 ** sqlite3WhereEnd()
156725 **
156726 ** Following the above, code to terminate the loop. Label A, the target
156727 ** of the Gosub above, jumps to the instruction right after the Goto.
156728 **
156729 ** Null 1 # Zero the rowset in reg 1
156730 ** Goto B # The loop is finished.
156731 **
156732 ** A: <loop body> # Return data, whatever.
156733 **
156734 ** Return 2 # Jump back to the Gosub
156735 **
156736 ** B: <after the loop>
156737 **
156738 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
156739 ** use an ephemeral index instead of a RowSet to record the primary
156740 ** keys of the rows we have already seen.
156741 **
156742 */
156743 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
156744 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
156745 Index *pCov = 0; /* Potential covering index (or NULL) */
156746 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
156747
156748 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
156749 int regRowset = 0; /* Register for RowSet object */
156750 int regRowid = 0; /* Register holding rowid */
156751 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
156752 int iRetInit; /* Address of regReturn init */
156753 int untestedTerms = 0; /* Some terms not completely tested */
156754 int ii; /* Loop counter */
156755 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
156756 Table *pTab = pTabItem->pTab;
156757
156758 pTerm = pLoop->aLTerm[0];
156759 assert( pTerm!=0 );
156760 assert( pTerm->eOperator & WO_OR );
156761 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
156762 pOrWc = &pTerm->u.pOrInfo->wc;
156763 pLevel->op = OP_Return;
156764 pLevel->p1 = regReturn;
156765
156766 /* Set up a new SrcList in pOrTab containing the table being scanned
156767 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
156768 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
156769 */
156770 if( pWInfo->nLevel>1 ){
156771 int nNotReady; /* The number of notReady tables */
156772 SrcItem *origSrc; /* Original list of tables */
156773 nNotReady = pWInfo->nLevel - iLevel - 1;
156774 pOrTab = sqlite3DbMallocRawNN(db,
156775 n: sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
156776 if( pOrTab==0 ) return notReady;
156777 pOrTab->nAlloc = (u8)(nNotReady + 1);
156778 pOrTab->nSrc = pOrTab->nAlloc;
156779 memcpy(dest: pOrTab->a, src: pTabItem, n: sizeof(*pTabItem));
156780 origSrc = pWInfo->pTabList->a;
156781 for(k=1; k<=nNotReady; k++){
156782 memcpy(dest: &pOrTab->a[k], src: &origSrc[pLevel[k].iFrom], n: sizeof(pOrTab->a[k]));
156783 }
156784 }else{
156785 pOrTab = pWInfo->pTabList;
156786 }
156787
156788 /* Initialize the rowset register to contain NULL. An SQL NULL is
156789 ** equivalent to an empty rowset. Or, create an ephemeral index
156790 ** capable of holding primary keys in the case of a WITHOUT ROWID.
156791 **
156792 ** Also initialize regReturn to contain the address of the instruction
156793 ** immediately following the OP_Return at the bottom of the loop. This
156794 ** is required in a few obscure LEFT JOIN cases where control jumps
156795 ** over the top of the loop into the body of it. In this case the
156796 ** correct response for the end-of-loop code (the OP_Return) is to
156797 ** fall through to the next instruction, just as an OP_Next does if
156798 ** called on an uninitialized cursor.
156799 */
156800 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
156801 if( HasRowid(pTab) ){
156802 regRowset = ++pParse->nMem;
156803 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regRowset);
156804 }else{
156805 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
156806 regRowset = pParse->nTab++;
156807 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: regRowset, p2: pPk->nKeyCol);
156808 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
156809 }
156810 regRowid = ++pParse->nMem;
156811 }
156812 iRetInit = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regReturn);
156813
156814 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
156815 ** Then for every term xN, evaluate as the subexpression: xN AND y
156816 ** That way, terms in y that are factored into the disjunction will
156817 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
156818 **
156819 ** Actually, each subexpression is converted to "xN AND w" where w is
156820 ** the "interesting" terms of z - terms that did not originate in the
156821 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
156822 ** indices.
156823 **
156824 ** This optimization also only applies if the (x1 OR x2 OR ...) term
156825 ** is not contained in the ON clause of a LEFT JOIN.
156826 ** See ticket http://www.sqlite.org/src/info/f2369304e4
156827 **
156828 ** 2022-02-04: Do not push down slices of a row-value comparison.
156829 ** In other words, "w" or "y" may not be a slice of a vector. Otherwise,
156830 ** the initialization of the right-hand operand of the vector comparison
156831 ** might not occur, or might occur only in an OR branch that is not
156832 ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
156833 **
156834 ** 2022-03-03: Do not push down expressions that involve subqueries.
156835 ** The subquery might get coded as a subroutine. Any table-references
156836 ** in the subquery might be resolved to index-references for the index on
156837 ** the OR branch in which the subroutine is coded. But if the subroutine
156838 ** is invoked from a different OR branch that uses a different index, such
156839 ** index-references will not work. tag-20220303a
156840 ** https://sqlite.org/forum/forumpost/36937b197273d403
156841 */
156842 if( pWC->nTerm>1 ){
156843 int iTerm;
156844 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
156845 Expr *pExpr = pWC->a[iTerm].pExpr;
156846 if( &pWC->a[iTerm] == pTerm ) continue;
156847 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
156848 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
156849 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
156850 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
156851 continue;
156852 }
156853 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
156854 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
156855 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
156856 pAndExpr = sqlite3ExprAnd(pParse, pLeft: pAndExpr, pRight: pExpr);
156857 }
156858 if( pAndExpr ){
156859 /* The extra 0x10000 bit on the opcode is masked off and does not
156860 ** become part of the new Expr.op. However, it does make the
156861 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
156862 ** prevents sqlite3PExpr() from applying the AND short-circuit
156863 ** optimization, which we do not want here. */
156864 pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, pLeft: 0, pRight: pAndExpr);
156865 }
156866 }
156867
156868 /* Run a separate WHERE clause for each term of the OR clause. After
156869 ** eliminating duplicates from other WHERE clauses, the action for each
156870 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
156871 */
156872 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
156873 for(ii=0; ii<pOrWc->nTerm; ii++){
156874 WhereTerm *pOrTerm = &pOrWc->a[ii];
156875 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
156876 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
156877 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
156878 Expr *pDelete; /* Local copy of OR clause term */
156879 int jmp1 = 0; /* Address of jump operation */
156880 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
156881 && !ExprHasProperty(pOrExpr, EP_OuterON)
156882 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
156883 pDelete = pOrExpr = sqlite3ExprDup(db, p: pOrExpr, flags: 0);
156884 if( db->mallocFailed ){
156885 sqlite3ExprDelete(db, p: pDelete);
156886 continue;
156887 }
156888 if( pAndExpr ){
156889 pAndExpr->pLeft = pOrExpr;
156890 pOrExpr = pAndExpr;
156891 }
156892 /* Loop through table entries that match term pOrTerm. */
156893 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
156894 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
156895 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
156896 WHERE_OR_SUBCLAUSE, iCovCur);
156897 assert( pSubWInfo || pParse->nErr );
156898 if( pSubWInfo ){
156899 WhereLoop *pSubLoop;
156900 int addrExplain = sqlite3WhereExplainOneScan(
156901 pParse, pTabList: pOrTab, pLevel: &pSubWInfo->a[0], wctrlFlags: 0
156902 );
156903 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
156904
156905 /* This is the sub-WHERE clause body. First skip over
156906 ** duplicate rows from prior sub-WHERE clauses, and record the
156907 ** rowid (or PRIMARY KEY) for the current row so that the same
156908 ** row will be skipped in subsequent sub-WHERE clauses.
156909 */
156910 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
156911 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
156912 if( HasRowid(pTab) ){
156913 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol: -1, regOut: regRowid);
156914 jmp1 = sqlite3VdbeAddOp4Int(p: v, OP_RowSetTest, p1: regRowset, p2: 0,
156915 p3: regRowid, p4: iSet);
156916 VdbeCoverage(v);
156917 }else{
156918 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
156919 int nPk = pPk->nKeyCol;
156920 int iPk;
156921 int r;
156922
156923 /* Read the PK into an array of temp registers. */
156924 r = sqlite3GetTempRange(pParse, nReg: nPk);
156925 for(iPk=0; iPk<nPk; iPk++){
156926 int iCol = pPk->aiColumn[iPk];
156927 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol,regOut: r+iPk);
156928 }
156929
156930 /* Check if the temp table already contains this key. If so,
156931 ** the row has already been included in the result set and
156932 ** can be ignored (by jumping past the Gosub below). Otherwise,
156933 ** insert the key into the temp table and proceed with processing
156934 ** the row.
156935 **
156936 ** Use some of the same optimizations as OP_RowSetTest: If iSet
156937 ** is zero, assume that the key cannot already be present in
156938 ** the temp table. And if iSet is -1, assume that there is no
156939 ** need to insert the key into the temp table, as it will never
156940 ** be tested for. */
156941 if( iSet ){
156942 jmp1 = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: regRowset, p2: 0, p3: r, p4: nPk);
156943 VdbeCoverage(v);
156944 }
156945 if( iSet>=0 ){
156946 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: r, p2: nPk, p3: regRowid);
156947 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: regRowset, p2: regRowid,
156948 p3: r, p4: nPk);
156949 if( iSet ) sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
156950 }
156951
156952 /* Release the array of temp registers */
156953 sqlite3ReleaseTempRange(pParse, iReg: r, nReg: nPk);
156954 }
156955 }
156956
156957 /* Invoke the main loop body as a subroutine */
156958 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regReturn, p2: iLoopBody);
156959
156960 /* Jump here (skipping the main loop body subroutine) if the
156961 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
156962 if( jmp1 ) sqlite3VdbeJumpHere(p: v, addr: jmp1);
156963
156964 /* The pSubWInfo->untestedTerms flag means that this OR term
156965 ** contained one or more AND term from a notReady table. The
156966 ** terms from the notReady table could not be tested and will
156967 ** need to be tested later.
156968 */
156969 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
156970
156971 /* If all of the OR-connected terms are optimized using the same
156972 ** index, and the index is opened using the same cursor number
156973 ** by each call to sqlite3WhereBegin() made by this loop, it may
156974 ** be possible to use that index as a covering index.
156975 **
156976 ** If the call to sqlite3WhereBegin() above resulted in a scan that
156977 ** uses an index, and this is either the first OR-connected term
156978 ** processed or the index is the same as that used by all previous
156979 ** terms, set pCov to the candidate covering index. Otherwise, set
156980 ** pCov to NULL to indicate that no candidate covering index will
156981 ** be available.
156982 */
156983 pSubLoop = pSubWInfo->a[0].pWLoop;
156984 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
156985 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
156986 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
156987 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
156988 ){
156989 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
156990 pCov = pSubLoop->u.btree.pIndex;
156991 }else{
156992 pCov = 0;
156993 }
156994 if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){
156995 pWInfo->bDeferredSeek = 1;
156996 }
156997
156998 /* Finish the loop through table entries that match term pOrTerm. */
156999 sqlite3WhereEnd(pSubWInfo);
157000 ExplainQueryPlanPop(pParse);
157001 }
157002 sqlite3ExprDelete(db, p: pDelete);
157003 }
157004 }
157005 ExplainQueryPlanPop(pParse);
157006 assert( pLevel->pWLoop==pLoop );
157007 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
157008 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
157009 pLevel->u.pCoveringIdx = pCov;
157010 if( pCov ) pLevel->iIdxCur = iCovCur;
157011 if( pAndExpr ){
157012 pAndExpr->pLeft = 0;
157013 sqlite3ExprDelete(db, p: pAndExpr);
157014 }
157015 sqlite3VdbeChangeP1(p: v, addr: iRetInit, val: sqlite3VdbeCurrentAddr(p: v));
157016 sqlite3VdbeGoto(p: v, iDest: pLevel->addrBrk);
157017 sqlite3VdbeResolveLabel(v, x: iLoopBody);
157018
157019 /* Set the P2 operand of the OP_Return opcode that will end the current
157020 ** loop to point to this spot, which is the top of the next containing
157021 ** loop. The byte-code formatter will use that P2 value as a hint to
157022 ** indent everything in between the this point and the final OP_Return.
157023 ** See tag-20220407a in vdbe.c and shell.c */
157024 assert( pLevel->op==OP_Return );
157025 pLevel->p2 = sqlite3VdbeCurrentAddr(p: v);
157026
157027 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, p: pOrTab); }
157028 if( !untestedTerms ) disableTerm(pLevel, pTerm);
157029 }else
157030#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
157031
157032 {
157033 /* Case 6: There is no usable index. We must do a complete
157034 ** scan of the entire table.
157035 */
157036 static const u8 aStep[] = { OP_Next, OP_Prev };
157037 static const u8 aStart[] = { OP_Rewind, OP_Last };
157038 assert( bRev==0 || bRev==1 );
157039 if( pTabItem->fg.isRecursive ){
157040 /* Tables marked isRecursive have only a single row that is stored in
157041 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
157042 pLevel->op = OP_Noop;
157043 }else{
157044 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
157045 pLevel->op = aStep[bRev];
157046 pLevel->p1 = iCur;
157047 pLevel->p2 = 1 + sqlite3VdbeAddOp2(p: v, op: aStart[bRev], p1: iCur, p2: addrHalt);
157048 VdbeCoverageIf(v, bRev==0);
157049 VdbeCoverageIf(v, bRev!=0);
157050 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
157051 }
157052 }
157053
157054#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
157055 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
157056#endif
157057
157058 /* Insert code to test every subexpression that can be completely
157059 ** computed using the current set of tables.
157060 **
157061 ** This loop may run between one and three times, depending on the
157062 ** constraints to be generated. The value of stack variable iLoop
157063 ** determines the constraints coded by each iteration, as follows:
157064 **
157065 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
157066 ** iLoop==2: Code remaining expressions that do not contain correlated
157067 ** sub-queries.
157068 ** iLoop==3: Code all remaining expressions.
157069 **
157070 ** An effort is made to skip unnecessary iterations of the loop.
157071 */
157072 iLoop = (pIdx ? 1 : 2);
157073 do{
157074 int iNext = 0; /* Next value for iLoop */
157075 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
157076 Expr *pE;
157077 int skipLikeAddr = 0;
157078 testcase( pTerm->wtFlags & TERM_VIRTUAL );
157079 testcase( pTerm->wtFlags & TERM_CODED );
157080 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
157081 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
157082 testcase( pWInfo->untestedTerms==0
157083 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
157084 pWInfo->untestedTerms = 1;
157085 continue;
157086 }
157087 pE = pTerm->pExpr;
157088 assert( pE!=0 );
157089 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
157090 if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON) ){
157091 /* Defer processing WHERE clause constraints until after outer
157092 ** join processing. tag-20220513a */
157093 continue;
157094 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
157095 && !ExprHasProperty(pE,EP_OuterON) ){
157096 continue;
157097 }else{
157098 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
157099 if( m & pLevel->notReady ){
157100 /* An ON clause that is not ripe */
157101 continue;
157102 }
157103 }
157104 }
157105 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pExpr: pE, iCur: pLevel->iTabCur, pIdx) ){
157106 iNext = 2;
157107 continue;
157108 }
157109 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
157110 if( iNext==0 ) iNext = 3;
157111 continue;
157112 }
157113
157114 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
157115 /* If the TERM_LIKECOND flag is set, that means that the range search
157116 ** is sufficient to guarantee that the LIKE operator is true, so we
157117 ** can skip the call to the like(A,B) function. But this only works
157118 ** for strings. So do not skip the call to the function on the pass
157119 ** that compares BLOBs. */
157120#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
157121 continue;
157122#else
157123 u32 x = pLevel->iLikeRepCntr;
157124 if( x>0 ){
157125 skipLikeAddr = sqlite3VdbeAddOp1(p: v, op: (x&1)?OP_IfNot:OP_If,p1: (int)(x>>1));
157126 VdbeCoverageIf(v, (x&1)==1);
157127 VdbeCoverageIf(v, (x&1)==0);
157128 }
157129#endif
157130 }
157131#ifdef WHERETRACE_ENABLED /* 0xffffffff */
157132 if( sqlite3WhereTrace ){
157133 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
157134 pWC->nTerm-j, pTerm, iLoop));
157135 }
157136 if( sqlite3WhereTrace & 0x4000 ){
157137 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
157138 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
157139 }
157140#endif
157141 sqlite3ExprIfFalse(pParse, pExpr: pE, dest: addrCont, SQLITE_JUMPIFNULL);
157142 if( skipLikeAddr ) sqlite3VdbeJumpHere(p: v, addr: skipLikeAddr);
157143 pTerm->wtFlags |= TERM_CODED;
157144 }
157145 iLoop = iNext;
157146 }while( iLoop>0 );
157147
157148 /* Insert code to test for implied constraints based on transitivity
157149 ** of the "==" operator.
157150 **
157151 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
157152 ** and we are coding the t1 loop and the t2 loop has not yet coded,
157153 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
157154 ** the implied "t1.a=123" constraint.
157155 */
157156 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
157157 Expr *pE, sEAlt;
157158 WhereTerm *pAlt;
157159 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
157160 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
157161 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
157162 if( pTerm->leftCursor!=iCur ) continue;
157163 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
157164 pE = pTerm->pExpr;
157165#ifdef WHERETRACE_ENABLED /* 0x4001 */
157166 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
157167 sqlite3DebugPrintf("Coding transitive constraint:\n");
157168 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
157169 }
157170#endif
157171 assert( !ExprHasProperty(pE, EP_OuterON) );
157172 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
157173 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157174 pAlt = sqlite3WhereFindTerm(pWC, iCur, iColumn: pTerm->u.x.leftColumn, notReady,
157175 WO_EQ|WO_IN|WO_IS, pIdx: 0);
157176 if( pAlt==0 ) continue;
157177 if( pAlt->wtFlags & (TERM_CODED) ) continue;
157178 if( (pAlt->eOperator & WO_IN)
157179 && ExprUseXSelect(pAlt->pExpr)
157180 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
157181 ){
157182 continue;
157183 }
157184 testcase( pAlt->eOperator & WO_EQ );
157185 testcase( pAlt->eOperator & WO_IS );
157186 testcase( pAlt->eOperator & WO_IN );
157187 VdbeModuleComment((v, "begin transitive constraint"));
157188 sEAlt = *pAlt->pExpr;
157189 sEAlt.pLeft = pE->pLeft;
157190 sqlite3ExprIfFalse(pParse, pExpr: &sEAlt, dest: addrCont, SQLITE_JUMPIFNULL);
157191 pAlt->wtFlags |= TERM_CODED;
157192 }
157193
157194 /* For a RIGHT OUTER JOIN, record the fact that the current row has
157195 ** been matched at least once.
157196 */
157197 if( pLevel->pRJ ){
157198 Table *pTab;
157199 int nPk;
157200 int r;
157201 int jmp1 = 0;
157202 WhereRightJoin *pRJ = pLevel->pRJ;
157203
157204 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
157205 ** will record that the current row of that table has been matched at
157206 ** least once. This is accomplished by storing the PK for the row in
157207 ** both the iMatch index and the regBloom Bloom filter.
157208 */
157209 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
157210 if( HasRowid(pTab) ){
157211 r = sqlite3GetTempRange(pParse, nReg: 2);
157212 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: pLevel->iTabCur, iCol: -1, regOut: r+1);
157213 nPk = 1;
157214 }else{
157215 int iPk;
157216 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
157217 nPk = pPk->nKeyCol;
157218 r = sqlite3GetTempRange(pParse, nReg: nPk+1);
157219 for(iPk=0; iPk<nPk; iPk++){
157220 int iCol = pPk->aiColumn[iPk];
157221 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol,regOut: r+1+iPk);
157222 }
157223 }
157224 jmp1 = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: pRJ->iMatch, p2: 0, p3: r+1, p4: nPk);
157225 VdbeCoverage(v);
157226 VdbeComment((v, "match against %s", pTab->zName));
157227 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: r+1, p2: nPk, p3: r);
157228 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: pRJ->iMatch, p2: r, p3: r+1, p4: nPk);
157229 sqlite3VdbeAddOp4Int(p: v, OP_FilterAdd, p1: pRJ->regBloom, p2: 0, p3: r+1, p4: nPk);
157230 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
157231 sqlite3VdbeJumpHere(p: v, addr: jmp1);
157232 sqlite3ReleaseTempRange(pParse, iReg: r, nReg: nPk+1);
157233 }
157234
157235 /* For a LEFT OUTER JOIN, generate code that will record the fact that
157236 ** at least one row of the right table has matched the left table.
157237 */
157238 if( pLevel->iLeftJoin ){
157239 pLevel->addrFirst = sqlite3VdbeCurrentAddr(p: v);
157240 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pLevel->iLeftJoin);
157241 VdbeComment((v, "record LEFT JOIN hit"));
157242 if( pLevel->pRJ==0 ){
157243 goto code_outer_join_constraints; /* WHERE clause constraints */
157244 }
157245 }
157246
157247 if( pLevel->pRJ ){
157248 /* Create a subroutine used to process all interior loops and code
157249 ** of the RIGHT JOIN. During normal operation, the subroutine will
157250 ** be in-line with the rest of the code. But at the end, a separate
157251 ** loop will run that invokes this subroutine for unmatched rows
157252 ** of pTab, with all tables to left begin set to NULL.
157253 */
157254 WhereRightJoin *pRJ = pLevel->pRJ;
157255 sqlite3VdbeAddOp2(p: v, OP_BeginSubrtn, p1: 0, p2: pRJ->regReturn);
157256 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(p: v);
157257 assert( pParse->withinRJSubrtn < 255 );
157258 pParse->withinRJSubrtn++;
157259
157260 /* WHERE clause constraints must be deferred until after outer join
157261 ** row elimination has completed, since WHERE clause constraints apply
157262 ** to the results of the OUTER JOIN. The following loop generates the
157263 ** appropriate WHERE clause constraint checks. tag-20220513a.
157264 */
157265 code_outer_join_constraints:
157266 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
157267 testcase( pTerm->wtFlags & TERM_VIRTUAL );
157268 testcase( pTerm->wtFlags & TERM_CODED );
157269 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
157270 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
157271 assert( pWInfo->untestedTerms );
157272 continue;
157273 }
157274 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
157275 assert( pTerm->pExpr );
157276 sqlite3ExprIfFalse(pParse, pExpr: pTerm->pExpr, dest: addrCont, SQLITE_JUMPIFNULL);
157277 pTerm->wtFlags |= TERM_CODED;
157278 }
157279 }
157280
157281#if WHERETRACE_ENABLED /* 0x4001 */
157282 if( sqlite3WhereTrace & 0x4000 ){
157283 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
157284 iLevel);
157285 sqlite3WhereClausePrint(pWC);
157286 }
157287 if( sqlite3WhereTrace & 0x1 ){
157288 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
157289 iLevel, (u64)pLevel->notReady);
157290 }
157291#endif
157292 return pLevel->notReady;
157293}
157294
157295/*
157296** Generate the code for the loop that finds all non-matched terms
157297** for a RIGHT JOIN.
157298*/
157299SQLITE_PRIVATE SQLITE_NOINLINE void sqlite3WhereRightJoinLoop(
157300 WhereInfo *pWInfo,
157301 int iLevel,
157302 WhereLevel *pLevel
157303){
157304 Parse *pParse = pWInfo->pParse;
157305 Vdbe *v = pParse->pVdbe;
157306 WhereRightJoin *pRJ = pLevel->pRJ;
157307 Expr *pSubWhere = 0;
157308 WhereClause *pWC = &pWInfo->sWC;
157309 WhereInfo *pSubWInfo;
157310 WhereLoop *pLoop = pLevel->pWLoop;
157311 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
157312 SrcList sFrom;
157313 Bitmask mAll = 0;
157314 int k;
157315
157316 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
157317 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
157318 pRJ->regReturn);
157319 for(k=0; k<iLevel; k++){
157320 int iIdxCur;
157321 mAll |= pWInfo->a[k].pWLoop->maskSelf;
157322 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: pWInfo->a[k].iTabCur);
157323 iIdxCur = pWInfo->a[k].iIdxCur;
157324 if( iIdxCur ){
157325 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: iIdxCur);
157326 }
157327 }
157328 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
157329 mAll |= pLoop->maskSelf;
157330 for(k=0; k<pWC->nTerm; k++){
157331 WhereTerm *pTerm = &pWC->a[k];
157332 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
157333 && pTerm->eOperator!=WO_ROWVAL
157334 ){
157335 break;
157336 }
157337 if( pTerm->prereqAll & ~mAll ) continue;
157338 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
157339 pSubWhere = sqlite3ExprAnd(pParse, pLeft: pSubWhere,
157340 pRight: sqlite3ExprDup(db: pParse->db, p: pTerm->pExpr, flags: 0));
157341 }
157342 }
157343 sFrom.nSrc = 1;
157344 sFrom.nAlloc = 1;
157345 memcpy(dest: &sFrom.a[0], src: pTabItem, n: sizeof(SrcItem));
157346 sFrom.a[0].fg.jointype = 0;
157347 assert( pParse->withinRJSubrtn < 100 );
157348 pParse->withinRJSubrtn++;
157349 pSubWInfo = sqlite3WhereBegin(pParse, &sFrom, pSubWhere, 0, 0, 0,
157350 WHERE_RIGHT_JOIN, 0);
157351 if( pSubWInfo ){
157352 int iCur = pLevel->iTabCur;
157353 int r = ++pParse->nMem;
157354 int nPk;
157355 int jmp;
157356 int addrCont = sqlite3WhereContinueLabel(pSubWInfo);
157357 Table *pTab = pTabItem->pTab;
157358 if( HasRowid(pTab) ){
157359 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol: -1, regOut: r);
157360 nPk = 1;
157361 }else{
157362 int iPk;
157363 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
157364 nPk = pPk->nKeyCol;
157365 pParse->nMem += nPk - 1;
157366 for(iPk=0; iPk<nPk; iPk++){
157367 int iCol = pPk->aiColumn[iPk];
157368 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol,regOut: r+iPk);
157369 }
157370 }
157371 jmp = sqlite3VdbeAddOp4Int(p: v, OP_Filter, p1: pRJ->regBloom, p2: 0, p3: r, p4: nPk);
157372 VdbeCoverage(v);
157373 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: pRJ->iMatch, p2: addrCont, p3: r, p4: nPk);
157374 VdbeCoverage(v);
157375 sqlite3VdbeJumpHere(p: v, addr: jmp);
157376 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pRJ->regReturn, p2: pRJ->addrSubrtn);
157377 sqlite3WhereEnd(pSubWInfo);
157378 }
157379 sqlite3ExprDelete(db: pParse->db, p: pSubWhere);
157380 ExplainQueryPlanPop(pParse);
157381 assert( pParse->withinRJSubrtn>0 );
157382 pParse->withinRJSubrtn--;
157383}
157384
157385/************** End of wherecode.c *******************************************/
157386/************** Begin file whereexpr.c ***************************************/
157387/*
157388** 2015-06-08
157389**
157390** The author disclaims copyright to this source code. In place of
157391** a legal notice, here is a blessing:
157392**
157393** May you do good and not evil.
157394** May you find forgiveness for yourself and forgive others.
157395** May you share freely, never taking more than you give.
157396**
157397*************************************************************************
157398** This module contains C code that generates VDBE code used to process
157399** the WHERE clause of SQL statements.
157400**
157401** This file was originally part of where.c but was split out to improve
157402** readability and editability. This file contains utility routines for
157403** analyzing Expr objects in the WHERE clause.
157404*/
157405/* #include "sqliteInt.h" */
157406/* #include "whereInt.h" */
157407
157408/* Forward declarations */
157409static void exprAnalyze(SrcList*, WhereClause*, int);
157410
157411/*
157412** Deallocate all memory associated with a WhereOrInfo object.
157413*/
157414static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
157415 sqlite3WhereClauseClear(&p->wc);
157416 sqlite3DbFree(db, p);
157417}
157418
157419/*
157420** Deallocate all memory associated with a WhereAndInfo object.
157421*/
157422static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
157423 sqlite3WhereClauseClear(&p->wc);
157424 sqlite3DbFree(db, p);
157425}
157426
157427/*
157428** Add a single new WhereTerm entry to the WhereClause object pWC.
157429** The new WhereTerm object is constructed from Expr p and with wtFlags.
157430** The index in pWC->a[] of the new WhereTerm is returned on success.
157431** 0 is returned if the new WhereTerm could not be added due to a memory
157432** allocation error. The memory allocation failure will be recorded in
157433** the db->mallocFailed flag so that higher-level functions can detect it.
157434**
157435** This routine will increase the size of the pWC->a[] array as necessary.
157436**
157437** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
157438** for freeing the expression p is assumed by the WhereClause object pWC.
157439** This is true even if this routine fails to allocate a new WhereTerm.
157440**
157441** WARNING: This routine might reallocate the space used to store
157442** WhereTerms. All pointers to WhereTerms should be invalidated after
157443** calling this routine. Such pointers may be reinitialized by referencing
157444** the pWC->a[] array.
157445*/
157446static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
157447 WhereTerm *pTerm;
157448 int idx;
157449 testcase( wtFlags & TERM_VIRTUAL );
157450 if( pWC->nTerm>=pWC->nSlot ){
157451 WhereTerm *pOld = pWC->a;
157452 sqlite3 *db = pWC->pWInfo->pParse->db;
157453 pWC->a = sqlite3WhereMalloc(pWInfo: pWC->pWInfo, nByte: sizeof(pWC->a[0])*pWC->nSlot*2 );
157454 if( pWC->a==0 ){
157455 if( wtFlags & TERM_DYNAMIC ){
157456 sqlite3ExprDelete(db, p);
157457 }
157458 pWC->a = pOld;
157459 return 0;
157460 }
157461 memcpy(dest: pWC->a, src: pOld, n: sizeof(pWC->a[0])*pWC->nTerm);
157462 pWC->nSlot = pWC->nSlot*2;
157463 }
157464 pTerm = &pWC->a[idx = pWC->nTerm++];
157465 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
157466 if( p && ExprHasProperty(p, EP_Unlikely) ){
157467 pTerm->truthProb = sqlite3LogEst(x: p->iTable) - 270;
157468 }else{
157469 pTerm->truthProb = 1;
157470 }
157471 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(pExpr: p);
157472 pTerm->wtFlags = wtFlags;
157473 pTerm->pWC = pWC;
157474 pTerm->iParent = -1;
157475 memset(s: &pTerm->eOperator, c: 0,
157476 n: sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
157477 return idx;
157478}
157479
157480/*
157481** Return TRUE if the given operator is one of the operators that is
157482** allowed for an indexable WHERE clause term. The allowed operators are
157483** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
157484*/
157485static int allowedOp(int op){
157486 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
157487 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
157488 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
157489 assert( TK_GE==TK_EQ+4 );
157490 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
157491}
157492
157493/*
157494** Commute a comparison operator. Expressions of the form "X op Y"
157495** are converted into "Y op X".
157496*/
157497static u16 exprCommute(Parse *pParse, Expr *pExpr){
157498 if( pExpr->pLeft->op==TK_VECTOR
157499 || pExpr->pRight->op==TK_VECTOR
157500 || sqlite3BinaryCompareCollSeq(pParse, pLeft: pExpr->pLeft, pRight: pExpr->pRight) !=
157501 sqlite3BinaryCompareCollSeq(pParse, pLeft: pExpr->pRight, pRight: pExpr->pLeft)
157502 ){
157503 pExpr->flags ^= EP_Commuted;
157504 }
157505 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
157506 if( pExpr->op>=TK_GT ){
157507 assert( TK_LT==TK_GT+2 );
157508 assert( TK_GE==TK_LE+2 );
157509 assert( TK_GT>TK_EQ );
157510 assert( TK_GT<TK_LE );
157511 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
157512 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
157513 }
157514 return 0;
157515}
157516
157517/*
157518** Translate from TK_xx operator to WO_xx bitmask.
157519*/
157520static u16 operatorMask(int op){
157521 u16 c;
157522 assert( allowedOp(op) );
157523 if( op==TK_IN ){
157524 c = WO_IN;
157525 }else if( op==TK_ISNULL ){
157526 c = WO_ISNULL;
157527 }else if( op==TK_IS ){
157528 c = WO_IS;
157529 }else{
157530 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
157531 c = (u16)(WO_EQ<<(op-TK_EQ));
157532 }
157533 assert( op!=TK_ISNULL || c==WO_ISNULL );
157534 assert( op!=TK_IN || c==WO_IN );
157535 assert( op!=TK_EQ || c==WO_EQ );
157536 assert( op!=TK_LT || c==WO_LT );
157537 assert( op!=TK_LE || c==WO_LE );
157538 assert( op!=TK_GT || c==WO_GT );
157539 assert( op!=TK_GE || c==WO_GE );
157540 assert( op!=TK_IS || c==WO_IS );
157541 return c;
157542}
157543
157544
157545#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
157546/*
157547** Check to see if the given expression is a LIKE or GLOB operator that
157548** can be optimized using inequality constraints. Return TRUE if it is
157549** so and false if not.
157550**
157551** In order for the operator to be optimizible, the RHS must be a string
157552** literal that does not begin with a wildcard. The LHS must be a column
157553** that may only be NULL, a string, or a BLOB, never a number. (This means
157554** that virtual tables cannot participate in the LIKE optimization.) The
157555** collating sequence for the column on the LHS must be appropriate for
157556** the operator.
157557*/
157558static int isLikeOrGlob(
157559 Parse *pParse, /* Parsing and code generating context */
157560 Expr *pExpr, /* Test this expression */
157561 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
157562 int *pisComplete, /* True if the only wildcard is % in the last character */
157563 int *pnoCase /* True if uppercase is equivalent to lowercase */
157564){
157565 const u8 *z = 0; /* String on RHS of LIKE operator */
157566 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
157567 ExprList *pList; /* List of operands to the LIKE operator */
157568 u8 c; /* One character in z[] */
157569 int cnt; /* Number of non-wildcard prefix characters */
157570 u8 wc[4]; /* Wildcard characters */
157571 sqlite3 *db = pParse->db; /* Database connection */
157572 sqlite3_value *pVal = 0;
157573 int op; /* Opcode of pRight */
157574 int rc; /* Result code to return */
157575
157576 if( !sqlite3IsLikeFunction(db, pExpr, pIsNocase: pnoCase, aWc: (char*)wc) ){
157577 return 0;
157578 }
157579#ifdef SQLITE_EBCDIC
157580 if( *pnoCase ) return 0;
157581#endif
157582 assert( ExprUseXList(pExpr) );
157583 pList = pExpr->x.pList;
157584 pLeft = pList->a[1].pExpr;
157585
157586 pRight = sqlite3ExprSkipCollate(pExpr: pList->a[0].pExpr);
157587 op = pRight->op;
157588 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
157589 Vdbe *pReprepare = pParse->pReprepare;
157590 int iCol = pRight->iColumn;
157591 pVal = sqlite3VdbeGetBoundValue(v: pReprepare, iVar: iCol, SQLITE_AFF_BLOB);
157592 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
157593 z = sqlite3_value_text(pVal);
157594 }
157595 sqlite3VdbeSetVarmask(v: pParse->pVdbe, iVar: iCol);
157596 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
157597 }else if( op==TK_STRING ){
157598 assert( !ExprHasProperty(pRight, EP_IntValue) );
157599 z = (u8*)pRight->u.zToken;
157600 }
157601 if( z ){
157602
157603 /* Count the number of prefix characters prior to the first wildcard */
157604 cnt = 0;
157605 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
157606 cnt++;
157607 if( c==wc[3] && z[cnt]!=0 ) cnt++;
157608 }
157609
157610 /* The optimization is possible only if (1) the pattern does not begin
157611 ** with a wildcard and if (2) the non-wildcard prefix does not end with
157612 ** an (illegal 0xff) character, or (3) the pattern does not consist of
157613 ** a single escape character. The second condition is necessary so
157614 ** that we can increment the prefix key to find an upper bound for the
157615 ** range search. The third is because the caller assumes that the pattern
157616 ** consists of at least one character after all escapes have been
157617 ** removed. */
157618 if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && 255!=(u8)z[cnt-1] ){
157619 Expr *pPrefix;
157620
157621 /* A "complete" match if the pattern ends with "*" or "%" */
157622 *pisComplete = c==wc[0] && z[cnt+1]==0;
157623
157624 /* Get the pattern prefix. Remove all escapes from the prefix. */
157625 pPrefix = sqlite3Expr(db, TK_STRING, zToken: (char*)z);
157626 if( pPrefix ){
157627 int iFrom, iTo;
157628 char *zNew;
157629 assert( !ExprHasProperty(pPrefix, EP_IntValue) );
157630 zNew = pPrefix->u.zToken;
157631 zNew[cnt] = 0;
157632 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
157633 if( zNew[iFrom]==wc[3] ) iFrom++;
157634 zNew[iTo++] = zNew[iFrom];
157635 }
157636 zNew[iTo] = 0;
157637 assert( iTo>0 );
157638
157639 /* If the LHS is not an ordinary column with TEXT affinity, then the
157640 ** pattern prefix boundaries (both the start and end boundaries) must
157641 ** not look like a number. Otherwise the pattern might be treated as
157642 ** a number, which will invalidate the LIKE optimization.
157643 **
157644 ** Getting this right has been a persistent source of bugs in the
157645 ** LIKE optimization. See, for example:
157646 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
157647 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
157648 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
157649 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
157650 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
157651 */
157652 if( pLeft->op!=TK_COLUMN
157653 || sqlite3ExprAffinity(pExpr: pLeft)!=SQLITE_AFF_TEXT
157654 || (ALWAYS( ExprUseYTab(pLeft) )
157655 && ALWAYS(pLeft->y.pTab)
157656 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
157657 ){
157658 int isNum;
157659 double rDummy;
157660 isNum = sqlite3AtoF(z: zNew, pResult: &rDummy, length: iTo, SQLITE_UTF8);
157661 if( isNum<=0 ){
157662 if( iTo==1 && zNew[0]=='-' ){
157663 isNum = +1;
157664 }else{
157665 zNew[iTo-1]++;
157666 isNum = sqlite3AtoF(z: zNew, pResult: &rDummy, length: iTo, SQLITE_UTF8);
157667 zNew[iTo-1]--;
157668 }
157669 }
157670 if( isNum>0 ){
157671 sqlite3ExprDelete(db, p: pPrefix);
157672 sqlite3ValueFree(v: pVal);
157673 return 0;
157674 }
157675 }
157676 }
157677 *ppPrefix = pPrefix;
157678
157679 /* If the RHS pattern is a bound parameter, make arrangements to
157680 ** reprepare the statement when that parameter is rebound */
157681 if( op==TK_VARIABLE ){
157682 Vdbe *v = pParse->pVdbe;
157683 sqlite3VdbeSetVarmask(v, iVar: pRight->iColumn);
157684 assert( !ExprHasProperty(pRight, EP_IntValue) );
157685 if( *pisComplete && pRight->u.zToken[1] ){
157686 /* If the rhs of the LIKE expression is a variable, and the current
157687 ** value of the variable means there is no need to invoke the LIKE
157688 ** function, then no OP_Variable will be added to the program.
157689 ** This causes problems for the sqlite3_bind_parameter_name()
157690 ** API. To work around them, add a dummy OP_Variable here.
157691 */
157692 int r1 = sqlite3GetTempReg(pParse);
157693 sqlite3ExprCodeTarget(pParse, pExpr: pRight, target: r1);
157694 sqlite3VdbeChangeP3(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-1, val: 0);
157695 sqlite3ReleaseTempReg(pParse, iReg: r1);
157696 }
157697 }
157698 }else{
157699 z = 0;
157700 }
157701 }
157702
157703 rc = (z!=0);
157704 sqlite3ValueFree(v: pVal);
157705 return rc;
157706}
157707#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
157708
157709
157710#ifndef SQLITE_OMIT_VIRTUALTABLE
157711/*
157712** Check to see if the pExpr expression is a form that needs to be passed
157713** to the xBestIndex method of virtual tables. Forms of interest include:
157714**
157715** Expression Virtual Table Operator
157716** ----------------------- ---------------------------------
157717** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
157718** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
157719** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
157720** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
157721** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
157722** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
157723** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
157724** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
157725** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
157726**
157727** In every case, "column" must be a column of a virtual table. If there
157728** is a match, set *ppLeft to the "column" expression, set *ppRight to the
157729** "expr" expression (even though in forms (6) and (8) the column is on the
157730** right and the expression is on the left). Also set *peOp2 to the
157731** appropriate virtual table operator. The return value is 1 or 2 if there
157732** is a match. The usual return is 1, but if the RHS is also a column
157733** of virtual table in forms (5) or (7) then return 2.
157734**
157735** If the expression matches none of the patterns above, return 0.
157736*/
157737static int isAuxiliaryVtabOperator(
157738 sqlite3 *db, /* Parsing context */
157739 Expr *pExpr, /* Test this expression */
157740 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
157741 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
157742 Expr **ppRight /* Expression to left of MATCH/op2 */
157743){
157744 if( pExpr->op==TK_FUNCTION ){
157745 static const struct Op2 {
157746 const char *zOp;
157747 unsigned char eOp2;
157748 } aOp[] = {
157749 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
157750 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
157751 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
157752 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
157753 };
157754 ExprList *pList;
157755 Expr *pCol; /* Column reference */
157756 int i;
157757
157758 assert( ExprUseXList(pExpr) );
157759 pList = pExpr->x.pList;
157760 if( pList==0 || pList->nExpr!=2 ){
157761 return 0;
157762 }
157763
157764 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
157765 ** virtual table on their second argument, which is the same as
157766 ** the left-hand side operand in their in-fix form.
157767 **
157768 ** vtab_column MATCH expression
157769 ** MATCH(expression,vtab_column)
157770 */
157771 pCol = pList->a[1].pExpr;
157772 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
157773 if( ExprIsVtab(pCol) ){
157774 for(i=0; i<ArraySize(aOp); i++){
157775 assert( !ExprHasProperty(pExpr, EP_IntValue) );
157776 if( sqlite3StrICmp(zLeft: pExpr->u.zToken, zRight: aOp[i].zOp)==0 ){
157777 *peOp2 = aOp[i].eOp2;
157778 *ppRight = pList->a[0].pExpr;
157779 *ppLeft = pCol;
157780 return 1;
157781 }
157782 }
157783 }
157784
157785 /* We can also match against the first column of overloaded
157786 ** functions where xFindFunction returns a value of at least
157787 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
157788 **
157789 ** OVERLOADED(vtab_column,expression)
157790 **
157791 ** Historically, xFindFunction expected to see lower-case function
157792 ** names. But for this use case, xFindFunction is expected to deal
157793 ** with function names in an arbitrary case.
157794 */
157795 pCol = pList->a[0].pExpr;
157796 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
157797 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
157798 if( ExprIsVtab(pCol) ){
157799 sqlite3_vtab *pVtab;
157800 sqlite3_module *pMod;
157801 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
157802 void *pNotUsed;
157803 pVtab = sqlite3GetVTable(db, pTab: pCol->y.pTab)->pVtab;
157804 assert( pVtab!=0 );
157805 assert( pVtab->pModule!=0 );
157806 assert( !ExprHasProperty(pExpr, EP_IntValue) );
157807 pMod = (sqlite3_module *)pVtab->pModule;
157808 if( pMod->xFindFunction!=0 ){
157809 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
157810 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
157811 *peOp2 = i;
157812 *ppRight = pList->a[1].pExpr;
157813 *ppLeft = pCol;
157814 return 1;
157815 }
157816 }
157817 }
157818 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
157819 int res = 0;
157820 Expr *pLeft = pExpr->pLeft;
157821 Expr *pRight = pExpr->pRight;
157822 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
157823 if( ExprIsVtab(pLeft) ){
157824 res++;
157825 }
157826 assert( pRight==0 || pRight->op!=TK_COLUMN
157827 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
157828 if( pRight && ExprIsVtab(pRight) ){
157829 res++;
157830 SWAP(Expr*, pLeft, pRight);
157831 }
157832 *ppLeft = pLeft;
157833 *ppRight = pRight;
157834 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
157835 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
157836 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
157837 return res;
157838 }
157839 return 0;
157840}
157841#endif /* SQLITE_OMIT_VIRTUALTABLE */
157842
157843/*
157844** If the pBase expression originated in the ON or USING clause of
157845** a join, then transfer the appropriate markings over to derived.
157846*/
157847static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
157848 if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON) ){
157849 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
157850 pDerived->w.iJoin = pBase->w.iJoin;
157851 }
157852}
157853
157854/*
157855** Mark term iChild as being a child of term iParent
157856*/
157857static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
157858 pWC->a[iChild].iParent = iParent;
157859 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
157860 pWC->a[iParent].nChild++;
157861}
157862
157863/*
157864** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
157865** a conjunction, then return just pTerm when N==0. If N is exceeds
157866** the number of available subterms, return NULL.
157867*/
157868static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
157869 if( pTerm->eOperator!=WO_AND ){
157870 return N==0 ? pTerm : 0;
157871 }
157872 if( N<pTerm->u.pAndInfo->wc.nTerm ){
157873 return &pTerm->u.pAndInfo->wc.a[N];
157874 }
157875 return 0;
157876}
157877
157878/*
157879** Subterms pOne and pTwo are contained within WHERE clause pWC. The
157880** two subterms are in disjunction - they are OR-ed together.
157881**
157882** If these two terms are both of the form: "A op B" with the same
157883** A and B values but different operators and if the operators are
157884** compatible (if one is = and the other is <, for example) then
157885** add a new virtual AND term to pWC that is the combination of the
157886** two.
157887**
157888** Some examples:
157889**
157890** x<y OR x=y --> x<=y
157891** x=y OR x=y --> x=y
157892** x<=y OR x<y --> x<=y
157893**
157894** The following is NOT generated:
157895**
157896** x<y OR x>y --> x!=y
157897*/
157898static void whereCombineDisjuncts(
157899 SrcList *pSrc, /* the FROM clause */
157900 WhereClause *pWC, /* The complete WHERE clause */
157901 WhereTerm *pOne, /* First disjunct */
157902 WhereTerm *pTwo /* Second disjunct */
157903){
157904 u16 eOp = pOne->eOperator | pTwo->eOperator;
157905 sqlite3 *db; /* Database connection (for malloc) */
157906 Expr *pNew; /* New virtual expression */
157907 int op; /* Operator for the combined expression */
157908 int idxNew; /* Index in pWC of the next virtual term */
157909
157910 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
157911 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
157912 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
157913 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
157914 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
157915 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
157916 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
157917 if( sqlite3ExprCompare(pParse: 0,pA: pOne->pExpr->pLeft, pB: pTwo->pExpr->pLeft, iTab: -1) ) return;
157918 if( sqlite3ExprCompare(pParse: 0,pA: pOne->pExpr->pRight, pB: pTwo->pExpr->pRight,iTab: -1) )return;
157919 /* If we reach this point, it means the two subterms can be combined */
157920 if( (eOp & (eOp-1))!=0 ){
157921 if( eOp & (WO_LT|WO_LE) ){
157922 eOp = WO_LE;
157923 }else{
157924 assert( eOp & (WO_GT|WO_GE) );
157925 eOp = WO_GE;
157926 }
157927 }
157928 db = pWC->pWInfo->pParse->db;
157929 pNew = sqlite3ExprDup(db, p: pOne->pExpr, flags: 0);
157930 if( pNew==0 ) return;
157931 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
157932 pNew->op = op;
157933 idxNew = whereClauseInsert(pWC, p: pNew, TERM_VIRTUAL|TERM_DYNAMIC);
157934 exprAnalyze(pSrc, pWC, idxNew);
157935}
157936
157937#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
157938/*
157939** Analyze a term that consists of two or more OR-connected
157940** subterms. So in:
157941**
157942** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
157943** ^^^^^^^^^^^^^^^^^^^^
157944**
157945** This routine analyzes terms such as the middle term in the above example.
157946** A WhereOrTerm object is computed and attached to the term under
157947** analysis, regardless of the outcome of the analysis. Hence:
157948**
157949** WhereTerm.wtFlags |= TERM_ORINFO
157950** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
157951**
157952** The term being analyzed must have two or more of OR-connected subterms.
157953** A single subterm might be a set of AND-connected sub-subterms.
157954** Examples of terms under analysis:
157955**
157956** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
157957** (B) x=expr1 OR expr2=x OR x=expr3
157958** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
157959** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
157960** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
157961** (F) x>A OR (x=A AND y>=B)
157962**
157963** CASE 1:
157964**
157965** If all subterms are of the form T.C=expr for some single column of C and
157966** a single table T (as shown in example B above) then create a new virtual
157967** term that is an equivalent IN expression. In other words, if the term
157968** being analyzed is:
157969**
157970** x = expr1 OR expr2 = x OR x = expr3
157971**
157972** then create a new virtual term like this:
157973**
157974** x IN (expr1,expr2,expr3)
157975**
157976** CASE 2:
157977**
157978** If there are exactly two disjuncts and one side has x>A and the other side
157979** has x=A (for the same x and A) then add a new virtual conjunct term to the
157980** WHERE clause of the form "x>=A". Example:
157981**
157982** x>A OR (x=A AND y>B) adds: x>=A
157983**
157984** The added conjunct can sometimes be helpful in query planning.
157985**
157986** CASE 3:
157987**
157988** If all subterms are indexable by a single table T, then set
157989**
157990** WhereTerm.eOperator = WO_OR
157991** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
157992**
157993** A subterm is "indexable" if it is of the form
157994** "T.C <op> <expr>" where C is any column of table T and
157995** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
157996** A subterm is also indexable if it is an AND of two or more
157997** subsubterms at least one of which is indexable. Indexable AND
157998** subterms have their eOperator set to WO_AND and they have
157999** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
158000**
158001** From another point of view, "indexable" means that the subterm could
158002** potentially be used with an index if an appropriate index exists.
158003** This analysis does not consider whether or not the index exists; that
158004** is decided elsewhere. This analysis only looks at whether subterms
158005** appropriate for indexing exist.
158006**
158007** All examples A through E above satisfy case 3. But if a term
158008** also satisfies case 1 (such as B) we know that the optimizer will
158009** always prefer case 1, so in that case we pretend that case 3 is not
158010** satisfied.
158011**
158012** It might be the case that multiple tables are indexable. For example,
158013** (E) above is indexable on tables P, Q, and R.
158014**
158015** Terms that satisfy case 3 are candidates for lookup by using
158016** separate indices to find rowids for each subterm and composing
158017** the union of all rowids using a RowSet object. This is similar
158018** to "bitmap indices" in other database engines.
158019**
158020** OTHERWISE:
158021**
158022** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
158023** zero. This term is not useful for search.
158024*/
158025static void exprAnalyzeOrTerm(
158026 SrcList *pSrc, /* the FROM clause */
158027 WhereClause *pWC, /* the complete WHERE clause */
158028 int idxTerm /* Index of the OR-term to be analyzed */
158029){
158030 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
158031 Parse *pParse = pWInfo->pParse; /* Parser context */
158032 sqlite3 *db = pParse->db; /* Database connection */
158033 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
158034 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
158035 int i; /* Loop counters */
158036 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
158037 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
158038 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
158039 Bitmask chngToIN; /* Tables that might satisfy case 1 */
158040 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
158041
158042 /*
158043 ** Break the OR clause into its separate subterms. The subterms are
158044 ** stored in a WhereClause structure containing within the WhereOrInfo
158045 ** object that is attached to the original OR clause term.
158046 */
158047 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
158048 assert( pExpr->op==TK_OR );
158049 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, n: sizeof(*pOrInfo));
158050 if( pOrInfo==0 ) return;
158051 pTerm->wtFlags |= TERM_ORINFO;
158052 pOrWc = &pOrInfo->wc;
158053 memset(s: pOrWc->aStatic, c: 0, n: sizeof(pOrWc->aStatic));
158054 sqlite3WhereClauseInit(pOrWc, pWInfo);
158055 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
158056 sqlite3WhereExprAnalyze(pSrc, pOrWc);
158057 if( db->mallocFailed ) return;
158058 assert( pOrWc->nTerm>=2 );
158059
158060 /*
158061 ** Compute the set of tables that might satisfy cases 1 or 3.
158062 */
158063 indexable = ~(Bitmask)0;
158064 chngToIN = ~(Bitmask)0;
158065 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
158066 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
158067 WhereAndInfo *pAndInfo;
158068 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
158069 chngToIN = 0;
158070 pAndInfo = sqlite3DbMallocRawNN(db, n: sizeof(*pAndInfo));
158071 if( pAndInfo ){
158072 WhereClause *pAndWC;
158073 WhereTerm *pAndTerm;
158074 int j;
158075 Bitmask b = 0;
158076 pOrTerm->u.pAndInfo = pAndInfo;
158077 pOrTerm->wtFlags |= TERM_ANDINFO;
158078 pOrTerm->eOperator = WO_AND;
158079 pOrTerm->leftCursor = -1;
158080 pAndWC = &pAndInfo->wc;
158081 memset(s: pAndWC->aStatic, c: 0, n: sizeof(pAndWC->aStatic));
158082 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
158083 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
158084 sqlite3WhereExprAnalyze(pSrc, pAndWC);
158085 pAndWC->pOuter = pWC;
158086 if( !db->mallocFailed ){
158087 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
158088 assert( pAndTerm->pExpr );
158089 if( allowedOp(op: pAndTerm->pExpr->op)
158090 || pAndTerm->eOperator==WO_AUX
158091 ){
158092 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
158093 }
158094 }
158095 }
158096 indexable &= b;
158097 }
158098 }else if( pOrTerm->wtFlags & TERM_COPIED ){
158099 /* Skip this term for now. We revisit it when we process the
158100 ** corresponding TERM_VIRTUAL term */
158101 }else{
158102 Bitmask b;
158103 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
158104 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
158105 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
158106 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
158107 }
158108 indexable &= b;
158109 if( (pOrTerm->eOperator & WO_EQ)==0 ){
158110 chngToIN = 0;
158111 }else{
158112 chngToIN &= b;
158113 }
158114 }
158115 }
158116
158117 /*
158118 ** Record the set of tables that satisfy case 3. The set might be
158119 ** empty.
158120 */
158121 pOrInfo->indexable = indexable;
158122 pTerm->eOperator = WO_OR;
158123 pTerm->leftCursor = -1;
158124 if( indexable ){
158125 pWC->hasOr = 1;
158126 }
158127
158128 /* For a two-way OR, attempt to implementation case 2.
158129 */
158130 if( indexable && pOrWc->nTerm==2 ){
158131 int iOne = 0;
158132 WhereTerm *pOne;
158133 while( (pOne = whereNthSubterm(pTerm: &pOrWc->a[0],N: iOne++))!=0 ){
158134 int iTwo = 0;
158135 WhereTerm *pTwo;
158136 while( (pTwo = whereNthSubterm(pTerm: &pOrWc->a[1],N: iTwo++))!=0 ){
158137 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
158138 }
158139 }
158140 }
158141
158142 /*
158143 ** chngToIN holds a set of tables that *might* satisfy case 1. But
158144 ** we have to do some additional checking to see if case 1 really
158145 ** is satisfied.
158146 **
158147 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
158148 ** that there is no possibility of transforming the OR clause into an
158149 ** IN operator because one or more terms in the OR clause contain
158150 ** something other than == on a column in the single table. The 1-bit
158151 ** case means that every term of the OR clause is of the form
158152 ** "table.column=expr" for some single table. The one bit that is set
158153 ** will correspond to the common table. We still need to check to make
158154 ** sure the same column is used on all terms. The 2-bit case is when
158155 ** the all terms are of the form "table1.column=table2.column". It
158156 ** might be possible to form an IN operator with either table1.column
158157 ** or table2.column as the LHS if either is common to every term of
158158 ** the OR clause.
158159 **
158160 ** Note that terms of the form "table.column1=table.column2" (the
158161 ** same table on both sizes of the ==) cannot be optimized.
158162 */
158163 if( chngToIN ){
158164 int okToChngToIN = 0; /* True if the conversion to IN is valid */
158165 int iColumn = -1; /* Column index on lhs of IN operator */
158166 int iCursor = -1; /* Table cursor common to all terms */
158167 int j = 0; /* Loop counter */
158168
158169 /* Search for a table and column that appears on one side or the
158170 ** other of the == operator in every subterm. That table and column
158171 ** will be recorded in iCursor and iColumn. There might not be any
158172 ** such table and column. Set okToChngToIN if an appropriate table
158173 ** and column is found but leave okToChngToIN false if not found.
158174 */
158175 for(j=0; j<2 && !okToChngToIN; j++){
158176 Expr *pLeft = 0;
158177 pOrTerm = pOrWc->a;
158178 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
158179 assert( pOrTerm->eOperator & WO_EQ );
158180 pOrTerm->wtFlags &= ~TERM_OK;
158181 if( pOrTerm->leftCursor==iCursor ){
158182 /* This is the 2-bit case and we are on the second iteration and
158183 ** current term is from the first iteration. So skip this term. */
158184 assert( j==1 );
158185 continue;
158186 }
158187 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
158188 pOrTerm->leftCursor))==0 ){
158189 /* This term must be of the form t1.a==t2.b where t2 is in the
158190 ** chngToIN set but t1 is not. This term will be either preceded
158191 ** or followed by an inverted copy (t2.b==t1.a). Skip this term
158192 ** and use its inversion. */
158193 testcase( pOrTerm->wtFlags & TERM_COPIED );
158194 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
158195 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
158196 continue;
158197 }
158198 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
158199 iColumn = pOrTerm->u.x.leftColumn;
158200 iCursor = pOrTerm->leftCursor;
158201 pLeft = pOrTerm->pExpr->pLeft;
158202 break;
158203 }
158204 if( i<0 ){
158205 /* No candidate table+column was found. This can only occur
158206 ** on the second iteration */
158207 assert( j==1 );
158208 assert( IsPowerOfTwo(chngToIN) );
158209 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
158210 break;
158211 }
158212 testcase( j==1 );
158213
158214 /* We have found a candidate table and column. Check to see if that
158215 ** table and column is common to every term in the OR clause */
158216 okToChngToIN = 1;
158217 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
158218 assert( pOrTerm->eOperator & WO_EQ );
158219 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
158220 if( pOrTerm->leftCursor!=iCursor ){
158221 pOrTerm->wtFlags &= ~TERM_OK;
158222 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
158223 && sqlite3ExprCompare(pParse, pA: pOrTerm->pExpr->pLeft, pB: pLeft, iTab: -1)
158224 )){
158225 okToChngToIN = 0;
158226 }else{
158227 int affLeft, affRight;
158228 /* If the right-hand side is also a column, then the affinities
158229 ** of both right and left sides must be such that no type
158230 ** conversions are required on the right. (Ticket #2249)
158231 */
158232 affRight = sqlite3ExprAffinity(pExpr: pOrTerm->pExpr->pRight);
158233 affLeft = sqlite3ExprAffinity(pExpr: pOrTerm->pExpr->pLeft);
158234 if( affRight!=0 && affRight!=affLeft ){
158235 okToChngToIN = 0;
158236 }else{
158237 pOrTerm->wtFlags |= TERM_OK;
158238 }
158239 }
158240 }
158241 }
158242
158243 /* At this point, okToChngToIN is true if original pTerm satisfies
158244 ** case 1. In that case, construct a new virtual term that is
158245 ** pTerm converted into an IN operator.
158246 */
158247 if( okToChngToIN ){
158248 Expr *pDup; /* A transient duplicate expression */
158249 ExprList *pList = 0; /* The RHS of the IN operator */
158250 Expr *pLeft = 0; /* The LHS of the IN operator */
158251 Expr *pNew; /* The complete IN operator */
158252
158253 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
158254 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
158255 assert( pOrTerm->eOperator & WO_EQ );
158256 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
158257 assert( pOrTerm->leftCursor==iCursor );
158258 assert( pOrTerm->u.x.leftColumn==iColumn );
158259 pDup = sqlite3ExprDup(db, p: pOrTerm->pExpr->pRight, flags: 0);
158260 pList = sqlite3ExprListAppend(pParse: pWInfo->pParse, pList, pExpr: pDup);
158261 pLeft = pOrTerm->pExpr->pLeft;
158262 }
158263 assert( pLeft!=0 );
158264 pDup = sqlite3ExprDup(db, p: pLeft, flags: 0);
158265 pNew = sqlite3PExpr(pParse, TK_IN, pLeft: pDup, pRight: 0);
158266 if( pNew ){
158267 int idxNew;
158268 transferJoinMarkings(pDerived: pNew, pBase: pExpr);
158269 assert( ExprUseXList(pNew) );
158270 pNew->x.pList = pList;
158271 idxNew = whereClauseInsert(pWC, p: pNew, TERM_VIRTUAL|TERM_DYNAMIC);
158272 testcase( idxNew==0 );
158273 exprAnalyze(pSrc, pWC, idxNew);
158274 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
158275 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
158276 }else{
158277 sqlite3ExprListDelete(db, pList);
158278 }
158279 }
158280 }
158281}
158282#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
158283
158284/*
158285** We already know that pExpr is a binary operator where both operands are
158286** column references. This routine checks to see if pExpr is an equivalence
158287** relation:
158288** 1. The SQLITE_Transitive optimization must be enabled
158289** 2. Must be either an == or an IS operator
158290** 3. Not originating in the ON clause of an OUTER JOIN
158291** 4. The affinities of A and B must be compatible
158292** 5a. Both operands use the same collating sequence OR
158293** 5b. The overall collating sequence is BINARY
158294** If this routine returns TRUE, that means that the RHS can be substituted
158295** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
158296** This is an optimization. No harm comes from returning 0. But if 1 is
158297** returned when it should not be, then incorrect answers might result.
158298*/
158299static int termIsEquivalence(Parse *pParse, Expr *pExpr){
158300 char aff1, aff2;
158301 CollSeq *pColl;
158302 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
158303 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
158304 if( ExprHasProperty(pExpr, EP_OuterON) ) return 0;
158305 aff1 = sqlite3ExprAffinity(pExpr: pExpr->pLeft);
158306 aff2 = sqlite3ExprAffinity(pExpr: pExpr->pRight);
158307 if( aff1!=aff2
158308 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
158309 ){
158310 return 0;
158311 }
158312 pColl = sqlite3ExprCompareCollSeq(pParse, p: pExpr);
158313 if( sqlite3IsBinary(pColl) ) return 1;
158314 return sqlite3ExprCollSeqMatch(pParse, pE1: pExpr->pLeft, pE2: pExpr->pRight);
158315}
158316
158317/*
158318** Recursively walk the expressions of a SELECT statement and generate
158319** a bitmask indicating which tables are used in that expression
158320** tree.
158321*/
158322static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
158323 Bitmask mask = 0;
158324 while( pS ){
158325 SrcList *pSrc = pS->pSrc;
158326 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
158327 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
158328 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
158329 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
158330 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
158331 if( ALWAYS(pSrc!=0) ){
158332 int i;
158333 for(i=0; i<pSrc->nSrc; i++){
158334 mask |= exprSelectUsage(pMaskSet, pS: pSrc->a[i].pSelect);
158335 if( pSrc->a[i].fg.isUsing==0 ){
158336 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
158337 }
158338 if( pSrc->a[i].fg.isTabFunc ){
158339 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
158340 }
158341 }
158342 }
158343 pS = pS->pPrior;
158344 }
158345 return mask;
158346}
158347
158348/*
158349** Expression pExpr is one operand of a comparison operator that might
158350** be useful for indexing. This routine checks to see if pExpr appears
158351** in any index. Return TRUE (1) if pExpr is an indexed term and return
158352** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
158353** number of the table that is indexed and aiCurCol[1] to the column number
158354** of the column that is indexed, or XN_EXPR (-2) if an expression is being
158355** indexed.
158356**
158357** If pExpr is a TK_COLUMN column reference, then this routine always returns
158358** true even if that particular column is not indexed, because the column
158359** might be added to an automatic index later.
158360*/
158361static SQLITE_NOINLINE int exprMightBeIndexed2(
158362 SrcList *pFrom, /* The FROM clause */
158363 int *aiCurCol, /* Write the referenced table cursor and column here */
158364 Expr *pExpr, /* An operand of a comparison operator */
158365 int j /* Start looking with the j-th pFrom entry */
158366){
158367 Index *pIdx;
158368 int i;
158369 int iCur;
158370 do{
158371 iCur = pFrom->a[j].iCursor;
158372 for(pIdx=pFrom->a[j].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
158373 if( pIdx->aColExpr==0 ) continue;
158374 for(i=0; i<pIdx->nKeyCol; i++){
158375 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
158376 assert( pIdx->bHasExpr );
158377 if( sqlite3ExprCompareSkip(pA: pExpr,pB: pIdx->aColExpr->a[i].pExpr,iTab: iCur)==0
158378 && pExpr->op!=TK_STRING
158379 ){
158380 aiCurCol[0] = iCur;
158381 aiCurCol[1] = XN_EXPR;
158382 return 1;
158383 }
158384 }
158385 }
158386 }while( ++j < pFrom->nSrc );
158387 return 0;
158388}
158389static int exprMightBeIndexed(
158390 SrcList *pFrom, /* The FROM clause */
158391 int *aiCurCol, /* Write the referenced table cursor & column here */
158392 Expr *pExpr, /* An operand of a comparison operator */
158393 int op /* The specific comparison operator */
158394){
158395 int i;
158396
158397 /* If this expression is a vector to the left or right of a
158398 ** inequality constraint (>, <, >= or <=), perform the processing
158399 ** on the first element of the vector. */
158400 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
158401 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
158402 assert( op<=TK_GE );
158403 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
158404 assert( ExprUseXList(pExpr) );
158405 pExpr = pExpr->x.pList->a[0].pExpr;
158406 }
158407
158408 if( pExpr->op==TK_COLUMN ){
158409 aiCurCol[0] = pExpr->iTable;
158410 aiCurCol[1] = pExpr->iColumn;
158411 return 1;
158412 }
158413
158414 for(i=0; i<pFrom->nSrc; i++){
158415 Index *pIdx;
158416 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
158417 if( pIdx->aColExpr ){
158418 return exprMightBeIndexed2(pFrom,aiCurCol,pExpr,j: i);
158419 }
158420 }
158421 }
158422 return 0;
158423}
158424
158425
158426/*
158427** The input to this routine is an WhereTerm structure with only the
158428** "pExpr" field filled in. The job of this routine is to analyze the
158429** subexpression and populate all the other fields of the WhereTerm
158430** structure.
158431**
158432** If the expression is of the form "<expr> <op> X" it gets commuted
158433** to the standard form of "X <op> <expr>".
158434**
158435** If the expression is of the form "X <op> Y" where both X and Y are
158436** columns, then the original expression is unchanged and a new virtual
158437** term of the form "Y <op> X" is added to the WHERE clause and
158438** analyzed separately. The original term is marked with TERM_COPIED
158439** and the new term is marked with TERM_DYNAMIC (because it's pExpr
158440** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
158441** is a commuted copy of a prior term.) The original term has nChild=1
158442** and the copy has idxParent set to the index of the original term.
158443*/
158444static void exprAnalyze(
158445 SrcList *pSrc, /* the FROM clause */
158446 WhereClause *pWC, /* the WHERE clause */
158447 int idxTerm /* Index of the term to be analyzed */
158448){
158449 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
158450 WhereTerm *pTerm; /* The term to be analyzed */
158451 WhereMaskSet *pMaskSet; /* Set of table index masks */
158452 Expr *pExpr; /* The expression to be analyzed */
158453 Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */
158454 Bitmask prereqAll; /* Prerequisites of pExpr */
158455 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
158456 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
158457 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
158458 int noCase = 0; /* uppercase equivalent to lowercase */
158459 int op; /* Top-level operator. pExpr->op */
158460 Parse *pParse = pWInfo->pParse; /* Parsing context */
158461 sqlite3 *db = pParse->db; /* Database connection */
158462 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
158463 int nLeft; /* Number of elements on left side vector */
158464
158465 if( db->mallocFailed ){
158466 return;
158467 }
158468 assert( pWC->nTerm > idxTerm );
158469 pTerm = &pWC->a[idxTerm];
158470 pMaskSet = &pWInfo->sMaskSet;
158471 pExpr = pTerm->pExpr;
158472 assert( pExpr!=0 ); /* Because malloc() has not failed */
158473 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
158474 pMaskSet->bVarSelect = 0;
158475 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
158476 op = pExpr->op;
158477 if( op==TK_IN ){
158478 assert( pExpr->pRight==0 );
158479 if( sqlite3ExprCheckIN(pParse, pIn: pExpr) ) return;
158480 if( ExprUseXSelect(pExpr) ){
158481 pTerm->prereqRight = exprSelectUsage(pMaskSet, pS: pExpr->x.pSelect);
158482 }else{
158483 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
158484 }
158485 prereqAll = prereqLeft | pTerm->prereqRight;
158486 }else{
158487 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
158488 if( pExpr->pLeft==0
158489 || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow)
158490 || pExpr->x.pList!=0
158491 ){
158492 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
158493 }else{
158494 prereqAll = prereqLeft | pTerm->prereqRight;
158495 }
158496 }
158497 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
158498
158499#ifdef SQLITE_DEBUG
158500 if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){
158501 printf("\n*** Incorrect prereqAll computed for:\n");
158502 sqlite3TreeViewExpr(0,pExpr,0);
158503 assert( 0 );
158504 }
158505#endif
158506
158507 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) ){
158508 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
158509 if( ExprHasProperty(pExpr, EP_OuterON) ){
158510 prereqAll |= x;
158511 extraRight = x-1; /* ON clause terms may not be used with an index
158512 ** on left table of a LEFT JOIN. Ticket #3015 */
158513 if( (prereqAll>>1)>=x ){
158514 sqlite3ErrorMsg(pParse, zFormat: "ON clause references tables to its right");
158515 return;
158516 }
158517 }else if( (prereqAll>>1)>=x ){
158518 /* The ON clause of an INNER JOIN references a table to its right.
158519 ** Most other SQL database engines raise an error. But SQLite versions
158520 ** 3.0 through 3.38 just put the ON clause constraint into the WHERE
158521 ** clause and carried on. Beginning with 3.39, raise an error only
158522 ** if there is a RIGHT or FULL JOIN in the query. This makes SQLite
158523 ** more like other systems, and also preserves legacy. */
158524 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
158525 sqlite3ErrorMsg(pParse, zFormat: "ON clause references tables to its right");
158526 return;
158527 }
158528 ExprClearProperty(pExpr, EP_InnerON);
158529 }
158530 }
158531 pTerm->prereqAll = prereqAll;
158532 pTerm->leftCursor = -1;
158533 pTerm->iParent = -1;
158534 pTerm->eOperator = 0;
158535 if( allowedOp(op) ){
158536 int aiCurCol[2];
158537 Expr *pLeft = sqlite3ExprSkipCollate(pExpr: pExpr->pLeft);
158538 Expr *pRight = sqlite3ExprSkipCollate(pExpr: pExpr->pRight);
158539 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
158540
158541 if( pTerm->u.x.iField>0 ){
158542 assert( op==TK_IN );
158543 assert( pLeft->op==TK_VECTOR );
158544 assert( ExprUseXList(pLeft) );
158545 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
158546 }
158547
158548 if( exprMightBeIndexed(pFrom: pSrc, aiCurCol, pExpr: pLeft, op) ){
158549 pTerm->leftCursor = aiCurCol[0];
158550 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158551 pTerm->u.x.leftColumn = aiCurCol[1];
158552 pTerm->eOperator = operatorMask(op) & opMask;
158553 }
158554 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
158555 if( pRight
158556 && exprMightBeIndexed(pFrom: pSrc, aiCurCol, pExpr: pRight, op)
158557 && !ExprHasProperty(pRight, EP_FixedCol)
158558 ){
158559 WhereTerm *pNew;
158560 Expr *pDup;
158561 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
158562 assert( pTerm->u.x.iField==0 );
158563 if( pTerm->leftCursor>=0 ){
158564 int idxNew;
158565 pDup = sqlite3ExprDup(db, p: pExpr, flags: 0);
158566 if( db->mallocFailed ){
158567 sqlite3ExprDelete(db, p: pDup);
158568 return;
158569 }
158570 idxNew = whereClauseInsert(pWC, p: pDup, TERM_VIRTUAL|TERM_DYNAMIC);
158571 if( idxNew==0 ) return;
158572 pNew = &pWC->a[idxNew];
158573 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
158574 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
158575 pTerm = &pWC->a[idxTerm];
158576 pTerm->wtFlags |= TERM_COPIED;
158577
158578 if( termIsEquivalence(pParse, pExpr: pDup) ){
158579 pTerm->eOperator |= WO_EQUIV;
158580 eExtraOp = WO_EQUIV;
158581 }
158582 }else{
158583 pDup = pExpr;
158584 pNew = pTerm;
158585 }
158586 pNew->wtFlags |= exprCommute(pParse, pExpr: pDup);
158587 pNew->leftCursor = aiCurCol[0];
158588 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158589 pNew->u.x.leftColumn = aiCurCol[1];
158590 testcase( (prereqLeft | extraRight) != prereqLeft );
158591 pNew->prereqRight = prereqLeft | extraRight;
158592 pNew->prereqAll = prereqAll;
158593 pNew->eOperator = (operatorMask(op: pDup->op) + eExtraOp) & opMask;
158594 }else
158595 if( op==TK_ISNULL
158596 && !ExprHasProperty(pExpr,EP_OuterON)
158597 && 0==sqlite3ExprCanBeNull(p: pLeft)
158598 ){
158599 assert( !ExprHasProperty(pExpr, EP_IntValue) );
158600 pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */
158601 pExpr->u.zToken = "false";
158602 ExprSetProperty(pExpr, EP_IsFalse);
158603 pTerm->prereqAll = 0;
158604 pTerm->eOperator = 0;
158605 }
158606 }
158607
158608#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
158609 /* If a term is the BETWEEN operator, create two new virtual terms
158610 ** that define the range that the BETWEEN implements. For example:
158611 **
158612 ** a BETWEEN b AND c
158613 **
158614 ** is converted into:
158615 **
158616 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
158617 **
158618 ** The two new terms are added onto the end of the WhereClause object.
158619 ** The new terms are "dynamic" and are children of the original BETWEEN
158620 ** term. That means that if the BETWEEN term is coded, the children are
158621 ** skipped. Or, if the children are satisfied by an index, the original
158622 ** BETWEEN term is skipped.
158623 */
158624 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
158625 ExprList *pList;
158626 int i;
158627 static const u8 ops[] = {TK_GE, TK_LE};
158628 assert( ExprUseXList(pExpr) );
158629 pList = pExpr->x.pList;
158630 assert( pList!=0 );
158631 assert( pList->nExpr==2 );
158632 for(i=0; i<2; i++){
158633 Expr *pNewExpr;
158634 int idxNew;
158635 pNewExpr = sqlite3PExpr(pParse, op: ops[i],
158636 pLeft: sqlite3ExprDup(db, p: pExpr->pLeft, flags: 0),
158637 pRight: sqlite3ExprDup(db, p: pList->a[i].pExpr, flags: 0));
158638 transferJoinMarkings(pDerived: pNewExpr, pBase: pExpr);
158639 idxNew = whereClauseInsert(pWC, p: pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
158640 testcase( idxNew==0 );
158641 exprAnalyze(pSrc, pWC, idxTerm: idxNew);
158642 pTerm = &pWC->a[idxTerm];
158643 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
158644 }
158645 }
158646#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
158647
158648#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
158649 /* Analyze a term that is composed of two or more subterms connected by
158650 ** an OR operator.
158651 */
158652 else if( pExpr->op==TK_OR ){
158653 assert( pWC->op==TK_AND );
158654 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
158655 pTerm = &pWC->a[idxTerm];
158656 }
158657#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
158658 /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently
158659 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
158660 ** virtual term of that form.
158661 **
158662 ** The virtual term must be tagged with TERM_VNULL.
158663 */
158664 else if( pExpr->op==TK_NOTNULL ){
158665 if( pExpr->pLeft->op==TK_COLUMN
158666 && pExpr->pLeft->iColumn>=0
158667 && !ExprHasProperty(pExpr, EP_OuterON)
158668 ){
158669 Expr *pNewExpr;
158670 Expr *pLeft = pExpr->pLeft;
158671 int idxNew;
158672 WhereTerm *pNewTerm;
158673
158674 pNewExpr = sqlite3PExpr(pParse, TK_GT,
158675 pLeft: sqlite3ExprDup(db, p: pLeft, flags: 0),
158676 pRight: sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0));
158677
158678 idxNew = whereClauseInsert(pWC, p: pNewExpr,
158679 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
158680 if( idxNew ){
158681 pNewTerm = &pWC->a[idxNew];
158682 pNewTerm->prereqRight = 0;
158683 pNewTerm->leftCursor = pLeft->iTable;
158684 pNewTerm->u.x.leftColumn = pLeft->iColumn;
158685 pNewTerm->eOperator = WO_GT;
158686 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
158687 pTerm = &pWC->a[idxTerm];
158688 pTerm->wtFlags |= TERM_COPIED;
158689 pNewTerm->prereqAll = pTerm->prereqAll;
158690 }
158691 }
158692 }
158693
158694
158695#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
158696 /* Add constraints to reduce the search space on a LIKE or GLOB
158697 ** operator.
158698 **
158699 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
158700 **
158701 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
158702 **
158703 ** The last character of the prefix "abc" is incremented to form the
158704 ** termination condition "abd". If case is not significant (the default
158705 ** for LIKE) then the lower-bound is made all uppercase and the upper-
158706 ** bound is made all lowercase so that the bounds also work when comparing
158707 ** BLOBs.
158708 */
158709 else if( pExpr->op==TK_FUNCTION
158710 && pWC->op==TK_AND
158711 && isLikeOrGlob(pParse, pExpr, ppPrefix: &pStr1, pisComplete: &isComplete, pnoCase: &noCase)
158712 ){
158713 Expr *pLeft; /* LHS of LIKE/GLOB operator */
158714 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
158715 Expr *pNewExpr1;
158716 Expr *pNewExpr2;
158717 int idxNew1;
158718 int idxNew2;
158719 const char *zCollSeqName; /* Name of collating sequence */
158720 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
158721
158722 assert( ExprUseXList(pExpr) );
158723 pLeft = pExpr->x.pList->a[1].pExpr;
158724 pStr2 = sqlite3ExprDup(db, p: pStr1, flags: 0);
158725 assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) );
158726 assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) );
158727
158728
158729 /* Convert the lower bound to upper-case and the upper bound to
158730 ** lower-case (upper-case is less than lower-case in ASCII) so that
158731 ** the range constraints also work for BLOBs
158732 */
158733 if( noCase && !pParse->db->mallocFailed ){
158734 int i;
158735 char c;
158736 pTerm->wtFlags |= TERM_LIKE;
158737 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
158738 pStr1->u.zToken[i] = sqlite3Toupper(c);
158739 pStr2->u.zToken[i] = sqlite3Tolower(c);
158740 }
158741 }
158742
158743 if( !db->mallocFailed ){
158744 u8 c, *pC; /* Last character before the first wildcard */
158745 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(z: pStr2->u.zToken)-1];
158746 c = *pC;
158747 if( noCase ){
158748 /* The point is to increment the last character before the first
158749 ** wildcard. But if we increment '@', that will push it into the
158750 ** alphabetic range where case conversions will mess up the
158751 ** inequality. To avoid this, make sure to also run the full
158752 ** LIKE on all candidate expressions by clearing the isComplete flag
158753 */
158754 if( c=='A'-1 ) isComplete = 0;
158755 c = sqlite3UpperToLower[c];
158756 }
158757 *pC = c + 1;
158758 }
158759 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
158760 pNewExpr1 = sqlite3ExprDup(db, p: pLeft, flags: 0);
158761 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
158762 pLeft: sqlite3ExprAddCollateString(pParse,pExpr: pNewExpr1,zC: zCollSeqName),
158763 pRight: pStr1);
158764 transferJoinMarkings(pDerived: pNewExpr1, pBase: pExpr);
158765 idxNew1 = whereClauseInsert(pWC, p: pNewExpr1, wtFlags);
158766 testcase( idxNew1==0 );
158767 pNewExpr2 = sqlite3ExprDup(db, p: pLeft, flags: 0);
158768 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
158769 pLeft: sqlite3ExprAddCollateString(pParse,pExpr: pNewExpr2,zC: zCollSeqName),
158770 pRight: pStr2);
158771 transferJoinMarkings(pDerived: pNewExpr2, pBase: pExpr);
158772 idxNew2 = whereClauseInsert(pWC, p: pNewExpr2, wtFlags);
158773 testcase( idxNew2==0 );
158774 exprAnalyze(pSrc, pWC, idxTerm: idxNew1);
158775 exprAnalyze(pSrc, pWC, idxTerm: idxNew2);
158776 pTerm = &pWC->a[idxTerm];
158777 if( isComplete ){
158778 markTermAsChild(pWC, iChild: idxNew1, iParent: idxTerm);
158779 markTermAsChild(pWC, iChild: idxNew2, iParent: idxTerm);
158780 }
158781 }
158782#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
158783
158784 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
158785 ** new terms for each component comparison - "a = ?" and "b = ?". The
158786 ** new terms completely replace the original vector comparison, which is
158787 ** no longer used.
158788 **
158789 ** This is only required if at least one side of the comparison operation
158790 ** is not a sub-select.
158791 **
158792 ** tag-20220128a
158793 */
158794 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
158795 && (nLeft = sqlite3ExprVectorSize(pExpr: pExpr->pLeft))>1
158796 && sqlite3ExprVectorSize(pExpr: pExpr->pRight)==nLeft
158797 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
158798 || (pExpr->pRight->flags & EP_xIsSelect)==0)
158799 && pWC->op==TK_AND
158800 ){
158801 int i;
158802 for(i=0; i<nLeft; i++){
158803 int idxNew;
158804 Expr *pNew;
158805 Expr *pLeft = sqlite3ExprForVectorField(pParse, pVector: pExpr->pLeft, iField: i, nField: nLeft);
158806 Expr *pRight = sqlite3ExprForVectorField(pParse, pVector: pExpr->pRight, iField: i, nField: nLeft);
158807
158808 pNew = sqlite3PExpr(pParse, op: pExpr->op, pLeft, pRight);
158809 transferJoinMarkings(pDerived: pNew, pBase: pExpr);
158810 idxNew = whereClauseInsert(pWC, p: pNew, TERM_DYNAMIC|TERM_SLICE);
158811 exprAnalyze(pSrc, pWC, idxTerm: idxNew);
158812 }
158813 pTerm = &pWC->a[idxTerm];
158814 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
158815 pTerm->eOperator = WO_ROWVAL;
158816 }
158817
158818 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
158819 ** a virtual term for each vector component. The expression object
158820 ** used by each such virtual term is pExpr (the full vector IN(...)
158821 ** expression). The WhereTerm.u.x.iField variable identifies the index within
158822 ** the vector on the LHS that the virtual term represents.
158823 **
158824 ** This only works if the RHS is a simple SELECT (not a compound) that does
158825 ** not use window functions.
158826 */
158827 else if( pExpr->op==TK_IN
158828 && pTerm->u.x.iField==0
158829 && pExpr->pLeft->op==TK_VECTOR
158830 && ALWAYS( ExprUseXSelect(pExpr) )
158831 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values))
158832#ifndef SQLITE_OMIT_WINDOWFUNC
158833 && pExpr->x.pSelect->pWin==0
158834#endif
158835 && pWC->op==TK_AND
158836 ){
158837 int i;
158838 for(i=0; i<sqlite3ExprVectorSize(pExpr: pExpr->pLeft); i++){
158839 int idxNew;
158840 idxNew = whereClauseInsert(pWC, p: pExpr, TERM_VIRTUAL|TERM_SLICE);
158841 pWC->a[idxNew].u.x.iField = i+1;
158842 exprAnalyze(pSrc, pWC, idxTerm: idxNew);
158843 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
158844 }
158845 }
158846
158847#ifndef SQLITE_OMIT_VIRTUALTABLE
158848 /* Add a WO_AUX auxiliary term to the constraint set if the
158849 ** current expression is of the form "column OP expr" where OP
158850 ** is an operator that gets passed into virtual tables but which is
158851 ** not normally optimized for ordinary tables. In other words, OP
158852 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
158853 ** This information is used by the xBestIndex methods of
158854 ** virtual tables. The native query optimizer does not attempt
158855 ** to do anything with MATCH functions.
158856 */
158857 else if( pWC->op==TK_AND ){
158858 Expr *pRight = 0, *pLeft = 0;
158859 int res = isAuxiliaryVtabOperator(db, pExpr, peOp2: &eOp2, ppLeft: &pLeft, ppRight: &pRight);
158860 while( res-- > 0 ){
158861 int idxNew;
158862 WhereTerm *pNewTerm;
158863 Bitmask prereqColumn, prereqExpr;
158864
158865 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
158866 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
158867 if( (prereqExpr & prereqColumn)==0 ){
158868 Expr *pNewExpr;
158869 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
158870 pLeft: 0, pRight: sqlite3ExprDup(db, p: pRight, flags: 0));
158871 if( ExprHasProperty(pExpr, EP_OuterON) && pNewExpr ){
158872 ExprSetProperty(pNewExpr, EP_OuterON);
158873 pNewExpr->w.iJoin = pExpr->w.iJoin;
158874 }
158875 idxNew = whereClauseInsert(pWC, p: pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
158876 testcase( idxNew==0 );
158877 pNewTerm = &pWC->a[idxNew];
158878 pNewTerm->prereqRight = prereqExpr;
158879 pNewTerm->leftCursor = pLeft->iTable;
158880 pNewTerm->u.x.leftColumn = pLeft->iColumn;
158881 pNewTerm->eOperator = WO_AUX;
158882 pNewTerm->eMatchOp = eOp2;
158883 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
158884 pTerm = &pWC->a[idxTerm];
158885 pTerm->wtFlags |= TERM_COPIED;
158886 pNewTerm->prereqAll = pTerm->prereqAll;
158887 }
158888 SWAP(Expr*, pLeft, pRight);
158889 }
158890 }
158891#endif /* SQLITE_OMIT_VIRTUALTABLE */
158892
158893 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
158894 ** an index for tables to the left of the join.
158895 */
158896 testcase( pTerm!=&pWC->a[idxTerm] );
158897 pTerm = &pWC->a[idxTerm];
158898 pTerm->prereqRight |= extraRight;
158899}
158900
158901/***************************************************************************
158902** Routines with file scope above. Interface to the rest of the where.c
158903** subsystem follows.
158904***************************************************************************/
158905
158906/*
158907** This routine identifies subexpressions in the WHERE clause where
158908** each subexpression is separated by the AND operator or some other
158909** operator specified in the op parameter. The WhereClause structure
158910** is filled with pointers to subexpressions. For example:
158911**
158912** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
158913** \________/ \_______________/ \________________/
158914** slot[0] slot[1] slot[2]
158915**
158916** The original WHERE clause in pExpr is unaltered. All this routine
158917** does is make slot[] entries point to substructure within pExpr.
158918**
158919** In the previous sentence and in the diagram, "slot[]" refers to
158920** the WhereClause.a[] array. The slot[] array grows as needed to contain
158921** all terms of the WHERE clause.
158922*/
158923SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
158924 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
158925 pWC->op = op;
158926 assert( pE2!=0 || pExpr==0 );
158927 if( pE2==0 ) return;
158928 if( pE2->op!=op ){
158929 whereClauseInsert(pWC, p: pExpr, wtFlags: 0);
158930 }else{
158931 sqlite3WhereSplit(pWC, pExpr: pE2->pLeft, op);
158932 sqlite3WhereSplit(pWC, pExpr: pE2->pRight, op);
158933 }
158934}
158935
158936/*
158937** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
158938** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
158939** where-clause passed as the first argument. The value for the term
158940** is found in register iReg.
158941**
158942** In the common case where the value is a simple integer
158943** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
158944** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
158945** If not, then it codes as a TK_REGISTER expression.
158946*/
158947static void whereAddLimitExpr(
158948 WhereClause *pWC, /* Add the constraint to this WHERE clause */
158949 int iReg, /* Register that will hold value of the limit/offset */
158950 Expr *pExpr, /* Expression that defines the limit/offset */
158951 int iCsr, /* Cursor to which the constraint applies */
158952 int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
158953){
158954 Parse *pParse = pWC->pWInfo->pParse;
158955 sqlite3 *db = pParse->db;
158956 Expr *pNew;
158957 int iVal = 0;
158958
158959 if( sqlite3ExprIsInteger(p: pExpr, pValue: &iVal) && iVal>=0 ){
158960 Expr *pVal = sqlite3Expr(db, TK_INTEGER, zToken: 0);
158961 if( pVal==0 ) return;
158962 ExprSetProperty(pVal, EP_IntValue);
158963 pVal->u.iValue = iVal;
158964 pNew = sqlite3PExpr(pParse, TK_MATCH, pLeft: 0, pRight: pVal);
158965 }else{
158966 Expr *pVal = sqlite3Expr(db, TK_REGISTER, zToken: 0);
158967 if( pVal==0 ) return;
158968 pVal->iTable = iReg;
158969 pNew = sqlite3PExpr(pParse, TK_MATCH, pLeft: 0, pRight: pVal);
158970 }
158971 if( pNew ){
158972 WhereTerm *pTerm;
158973 int idx;
158974 idx = whereClauseInsert(pWC, p: pNew, TERM_DYNAMIC|TERM_VIRTUAL);
158975 pTerm = &pWC->a[idx];
158976 pTerm->leftCursor = iCsr;
158977 pTerm->eOperator = WO_AUX;
158978 pTerm->eMatchOp = eMatchOp;
158979 }
158980}
158981
158982/*
158983** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
158984** SELECT statement passed as the second argument. These terms are only
158985** added if:
158986**
158987** 1. The SELECT statement has a LIMIT clause, and
158988** 2. The SELECT statement is not an aggregate or DISTINCT query, and
158989** 3. The SELECT statement has exactly one object in its from clause, and
158990** that object is a virtual table, and
158991** 4. There are no terms in the WHERE clause that will not be passed
158992** to the virtual table xBestIndex method.
158993** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
158994** method.
158995**
158996** LIMIT and OFFSET terms are ignored by most of the planner code. They
158997** exist only so that they may be passed to the xBestIndex method of the
158998** single virtual table in the FROM clause of the SELECT.
158999*/
159000SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
159001 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
159002 if( p->pGroupBy==0
159003 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
159004 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
159005 ){
159006 ExprList *pOrderBy = p->pOrderBy;
159007 int iCsr = p->pSrc->a[0].iCursor;
159008 int ii;
159009
159010 /* Check condition (4). Return early if it is not met. */
159011 for(ii=0; ii<pWC->nTerm; ii++){
159012 if( pWC->a[ii].wtFlags & TERM_CODED ){
159013 /* This term is a vector operation that has been decomposed into
159014 ** other, subsequent terms. It can be ignored. See tag-20220128a */
159015 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
159016 assert( pWC->a[ii].eOperator==WO_ROWVAL );
159017 continue;
159018 }
159019 if( pWC->a[ii].nChild ){
159020 /* If this term has child terms, then they are also part of the
159021 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
159022 ** will only be added if each of the child terms passes the
159023 ** (leftCursor==iCsr) test below. */
159024 continue;
159025 }
159026 if( pWC->a[ii].leftCursor!=iCsr ) return;
159027 }
159028
159029 /* Check condition (5). Return early if it is not met. */
159030 if( pOrderBy ){
159031 for(ii=0; ii<pOrderBy->nExpr; ii++){
159032 Expr *pExpr = pOrderBy->a[ii].pExpr;
159033 if( pExpr->op!=TK_COLUMN ) return;
159034 if( pExpr->iTable!=iCsr ) return;
159035 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
159036 }
159037 }
159038
159039 /* All conditions are met. Add the terms to the where-clause object. */
159040 assert( p->pLimit->op==TK_LIMIT );
159041 whereAddLimitExpr(pWC, iReg: p->iLimit, pExpr: p->pLimit->pLeft,
159042 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
159043 if( p->iOffset>0 ){
159044 whereAddLimitExpr(pWC, iReg: p->iOffset, pExpr: p->pLimit->pRight,
159045 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
159046 }
159047 }
159048}
159049
159050/*
159051** Initialize a preallocated WhereClause structure.
159052*/
159053SQLITE_PRIVATE void sqlite3WhereClauseInit(
159054 WhereClause *pWC, /* The WhereClause to be initialized */
159055 WhereInfo *pWInfo /* The WHERE processing context */
159056){
159057 pWC->pWInfo = pWInfo;
159058 pWC->hasOr = 0;
159059 pWC->pOuter = 0;
159060 pWC->nTerm = 0;
159061 pWC->nBase = 0;
159062 pWC->nSlot = ArraySize(pWC->aStatic);
159063 pWC->a = pWC->aStatic;
159064}
159065
159066/*
159067** Deallocate a WhereClause structure. The WhereClause structure
159068** itself is not freed. This routine is the inverse of
159069** sqlite3WhereClauseInit().
159070*/
159071SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
159072 sqlite3 *db = pWC->pWInfo->pParse->db;
159073 assert( pWC->nTerm>=pWC->nBase );
159074 if( pWC->nTerm>0 ){
159075 WhereTerm *a = pWC->a;
159076 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
159077#ifdef SQLITE_DEBUG
159078 int i;
159079 /* Verify that every term past pWC->nBase is virtual */
159080 for(i=pWC->nBase; i<pWC->nTerm; i++){
159081 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
159082 }
159083#endif
159084 while(1){
159085 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
159086 if( a->wtFlags & TERM_DYNAMIC ){
159087 sqlite3ExprDelete(db, p: a->pExpr);
159088 }
159089 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
159090 if( a->wtFlags & TERM_ORINFO ){
159091 assert( (a->wtFlags & TERM_ANDINFO)==0 );
159092 whereOrInfoDelete(db, p: a->u.pOrInfo);
159093 }else{
159094 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
159095 whereAndInfoDelete(db, p: a->u.pAndInfo);
159096 }
159097 }
159098 if( a==aLast ) break;
159099 a++;
159100 }
159101 }
159102}
159103
159104
159105/*
159106** These routines walk (recursively) an expression tree and generate
159107** a bitmask indicating which tables are used in that expression
159108** tree.
159109**
159110** sqlite3WhereExprUsage(MaskSet, Expr) ->
159111**
159112** Return a Bitmask of all tables referenced by Expr. Expr can be
159113** be NULL, in which case 0 is returned.
159114**
159115** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
159116**
159117** Same as sqlite3WhereExprUsage() except that Expr must not be
159118** NULL. The "NN" suffix on the name stands for "Not Null".
159119**
159120** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
159121**
159122** Return a Bitmask of all tables referenced by every expression
159123** in the expression list ExprList. ExprList can be NULL, in which
159124** case 0 is returned.
159125**
159126** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
159127**
159128** Internal use only. Called only by sqlite3WhereExprUsageNN() for
159129** complex expressions that require pushing register values onto
159130** the stack. Many calls to sqlite3WhereExprUsageNN() do not need
159131** the more complex analysis done by this routine. Hence, the
159132** computations done by this routine are broken out into a separate
159133** "no-inline" function to avoid the stack push overhead in the
159134** common case where it is not needed.
159135*/
159136static SQLITE_NOINLINE Bitmask sqlite3WhereExprUsageFull(
159137 WhereMaskSet *pMaskSet,
159138 Expr *p
159139){
159140 Bitmask mask;
159141 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
159142 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
159143 if( p->pRight ){
159144 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
159145 assert( p->x.pList==0 );
159146 }else if( ExprUseXSelect(p) ){
159147 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
159148 mask |= exprSelectUsage(pMaskSet, pS: p->x.pSelect);
159149 }else if( p->x.pList ){
159150 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
159151 }
159152#ifndef SQLITE_OMIT_WINDOWFUNC
159153 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
159154 assert( p->y.pWin!=0 );
159155 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
159156 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
159157 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
159158 }
159159#endif
159160 return mask;
159161}
159162SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
159163 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
159164 return sqlite3WhereGetMask(pMaskSet, p->iTable);
159165 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
159166 assert( p->op!=TK_IF_NULL_ROW );
159167 return 0;
159168 }
159169 return sqlite3WhereExprUsageFull(pMaskSet, p);
159170}
159171SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
159172 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
159173}
159174SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
159175 int i;
159176 Bitmask mask = 0;
159177 if( pList ){
159178 for(i=0; i<pList->nExpr; i++){
159179 mask |= sqlite3WhereExprUsage(pMaskSet, p: pList->a[i].pExpr);
159180 }
159181 }
159182 return mask;
159183}
159184
159185
159186/*
159187** Call exprAnalyze on all terms in a WHERE clause.
159188**
159189** Note that exprAnalyze() might add new virtual terms onto the
159190** end of the WHERE clause. We do not want to analyze these new
159191** virtual terms, so start analyzing at the end and work forward
159192** so that the added virtual terms are never processed.
159193*/
159194SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
159195 SrcList *pTabList, /* the FROM clause */
159196 WhereClause *pWC /* the WHERE clause to be analyzed */
159197){
159198 int i;
159199 for(i=pWC->nTerm-1; i>=0; i--){
159200 exprAnalyze(pSrc: pTabList, pWC, idxTerm: i);
159201 }
159202}
159203
159204/*
159205** For table-valued-functions, transform the function arguments into
159206** new WHERE clause terms.
159207**
159208** Each function argument translates into an equality constraint against
159209** a HIDDEN column in the table.
159210*/
159211SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
159212 Parse *pParse, /* Parsing context */
159213 SrcItem *pItem, /* The FROM clause term to process */
159214 WhereClause *pWC /* Xfer function arguments to here */
159215){
159216 Table *pTab;
159217 int j, k;
159218 ExprList *pArgs;
159219 Expr *pColRef;
159220 Expr *pTerm;
159221 if( pItem->fg.isTabFunc==0 ) return;
159222 pTab = pItem->pTab;
159223 assert( pTab!=0 );
159224 pArgs = pItem->u1.pFuncArg;
159225 if( pArgs==0 ) return;
159226 for(j=k=0; j<pArgs->nExpr; j++){
159227 Expr *pRhs;
159228 u32 joinType;
159229 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
159230 if( k>=pTab->nCol ){
159231 sqlite3ErrorMsg(pParse, zFormat: "too many arguments on %s() - max %d",
159232 pTab->zName, j);
159233 return;
159234 }
159235 pColRef = sqlite3ExprAlloc(db: pParse->db, TK_COLUMN, pToken: 0, dequote: 0);
159236 if( pColRef==0 ) return;
159237 pColRef->iTable = pItem->iCursor;
159238 pColRef->iColumn = k++;
159239 assert( ExprUseYTab(pColRef) );
159240 pColRef->y.pTab = pTab;
159241 pItem->colUsed |= sqlite3ExprColUsed(pExpr: pColRef);
159242 pRhs = sqlite3PExpr(pParse, TK_UPLUS,
159243 pLeft: sqlite3ExprDup(db: pParse->db, p: pArgs->a[j].pExpr, flags: 0), pRight: 0);
159244 pTerm = sqlite3PExpr(pParse, TK_EQ, pLeft: pColRef, pRight: pRhs);
159245 if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){
159246 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
159247 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
159248 joinType = EP_OuterON;
159249 }else{
159250 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
159251 joinType = EP_InnerON;
159252 }
159253 sqlite3SetJoinExpr(p: pTerm, iTable: pItem->iCursor, joinFlag: joinType);
159254 whereClauseInsert(pWC, p: pTerm, TERM_DYNAMIC);
159255 }
159256}
159257
159258/************** End of whereexpr.c *******************************************/
159259/************** Begin file where.c *******************************************/
159260/*
159261** 2001 September 15
159262**
159263** The author disclaims copyright to this source code. In place of
159264** a legal notice, here is a blessing:
159265**
159266** May you do good and not evil.
159267** May you find forgiveness for yourself and forgive others.
159268** May you share freely, never taking more than you give.
159269**
159270*************************************************************************
159271** This module contains C code that generates VDBE code used to process
159272** the WHERE clause of SQL statements. This module is responsible for
159273** generating the code that loops through a table looking for applicable
159274** rows. Indices are selected and used to speed the search when doing
159275** so is applicable. Because this module is responsible for selecting
159276** indices, you might also think of this module as the "query optimizer".
159277*/
159278/* #include "sqliteInt.h" */
159279/* #include "whereInt.h" */
159280
159281/*
159282** Extra information appended to the end of sqlite3_index_info but not
159283** visible to the xBestIndex function, at least not directly. The
159284** sqlite3_vtab_collation() interface knows how to reach it, however.
159285**
159286** This object is not an API and can be changed from one release to the
159287** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
159288** agree on the structure, all will be well.
159289*/
159290typedef struct HiddenIndexInfo HiddenIndexInfo;
159291struct HiddenIndexInfo {
159292 WhereClause *pWC; /* The Where clause being analyzed */
159293 Parse *pParse; /* The parsing context */
159294 int eDistinct; /* Value to return from sqlite3_vtab_distinct() */
159295 u32 mIn; /* Mask of terms that are <col> IN (...) */
159296 u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */
159297 sqlite3_value *aRhs[1]; /* RHS values for constraints. MUST BE LAST
159298 ** because extra space is allocated to hold up
159299 ** to nTerm such values */
159300};
159301
159302/* Forward declaration of methods */
159303static int whereLoopResize(sqlite3*, WhereLoop*, int);
159304
159305/*
159306** Return the estimated number of output rows from a WHERE clause
159307*/
159308SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
159309 return pWInfo->nRowOut;
159310}
159311
159312/*
159313** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
159314** WHERE clause returns outputs for DISTINCT processing.
159315*/
159316SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
159317 return pWInfo->eDistinct;
159318}
159319
159320/*
159321** Return the number of ORDER BY terms that are satisfied by the
159322** WHERE clause. A return of 0 means that the output must be
159323** completely sorted. A return equal to the number of ORDER BY
159324** terms means that no sorting is needed at all. A return that
159325** is positive but less than the number of ORDER BY terms means that
159326** block sorting is required.
159327*/
159328SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
159329 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
159330}
159331
159332/*
159333** In the ORDER BY LIMIT optimization, if the inner-most loop is known
159334** to emit rows in increasing order, and if the last row emitted by the
159335** inner-most loop did not fit within the sorter, then we can skip all
159336** subsequent rows for the current iteration of the inner loop (because they
159337** will not fit in the sorter either) and continue with the second inner
159338** loop - the loop immediately outside the inner-most.
159339**
159340** When a row does not fit in the sorter (because the sorter already
159341** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
159342** label returned by this function.
159343**
159344** If the ORDER BY LIMIT optimization applies, the jump destination should
159345** be the continuation for the second-inner-most loop. If the ORDER BY
159346** LIMIT optimization does not apply, then the jump destination should
159347** be the continuation for the inner-most loop.
159348**
159349** It is always safe for this routine to return the continuation of the
159350** inner-most loop, in the sense that a correct answer will result.
159351** Returning the continuation the second inner loop is an optimization
159352** that might make the code run a little faster, but should not change
159353** the final answer.
159354*/
159355SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
159356 WhereLevel *pInner;
159357 if( !pWInfo->bOrderedInnerLoop ){
159358 /* The ORDER BY LIMIT optimization does not apply. Jump to the
159359 ** continuation of the inner-most loop. */
159360 return pWInfo->iContinue;
159361 }
159362 pInner = &pWInfo->a[pWInfo->nLevel-1];
159363 assert( pInner->addrNxt!=0 );
159364 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
159365}
159366
159367/*
159368** While generating code for the min/max optimization, after handling
159369** the aggregate-step call to min() or max(), check to see if any
159370** additional looping is required. If the output order is such that
159371** we are certain that the correct answer has already been found, then
159372** code an OP_Goto to by pass subsequent processing.
159373**
159374** Any extra OP_Goto that is coded here is an optimization. The
159375** correct answer should be obtained regardless. This OP_Goto just
159376** makes the answer appear faster.
159377*/
159378SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
159379 WhereLevel *pInner;
159380 int i;
159381 if( !pWInfo->bOrderedInnerLoop ) return;
159382 if( pWInfo->nOBSat==0 ) return;
159383 for(i=pWInfo->nLevel-1; i>=0; i--){
159384 pInner = &pWInfo->a[i];
159385 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
159386 sqlite3VdbeGoto(p: v, iDest: pInner->addrNxt);
159387 return;
159388 }
159389 }
159390 sqlite3VdbeGoto(p: v, iDest: pWInfo->iBreak);
159391}
159392
159393/*
159394** Return the VDBE address or label to jump to in order to continue
159395** immediately with the next row of a WHERE clause.
159396*/
159397SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
159398 assert( pWInfo->iContinue!=0 );
159399 return pWInfo->iContinue;
159400}
159401
159402/*
159403** Return the VDBE address or label to jump to in order to break
159404** out of a WHERE loop.
159405*/
159406SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
159407 return pWInfo->iBreak;
159408}
159409
159410/*
159411** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
159412** operate directly on the rowids returned by a WHERE clause. Return
159413** ONEPASS_SINGLE (1) if the statement can operation directly because only
159414** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
159415** optimization can be used on multiple
159416**
159417** If the ONEPASS optimization is used (if this routine returns true)
159418** then also write the indices of open cursors used by ONEPASS
159419** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
159420** table and iaCur[1] gets the cursor used by an auxiliary index.
159421** Either value may be -1, indicating that cursor is not used.
159422** Any cursors returned will have been opened for writing.
159423**
159424** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
159425** unable to use the ONEPASS optimization.
159426*/
159427SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
159428 memcpy(dest: aiCur, src: pWInfo->aiCurOnePass, n: sizeof(int)*2);
159429#ifdef WHERETRACE_ENABLED
159430 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
159431 sqlite3DebugPrintf("%s cursors: %d %d\n",
159432 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
159433 aiCur[0], aiCur[1]);
159434 }
159435#endif
159436 return pWInfo->eOnePass;
159437}
159438
159439/*
159440** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
159441** the data cursor to the row selected by the index cursor.
159442*/
159443SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
159444 return pWInfo->bDeferredSeek;
159445}
159446
159447/*
159448** Move the content of pSrc into pDest
159449*/
159450static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
159451 pDest->n = pSrc->n;
159452 memcpy(dest: pDest->a, src: pSrc->a, n: pDest->n*sizeof(pDest->a[0]));
159453}
159454
159455/*
159456** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
159457**
159458** The new entry might overwrite an existing entry, or it might be
159459** appended, or it might be discarded. Do whatever is the right thing
159460** so that pSet keeps the N_OR_COST best entries seen so far.
159461*/
159462static int whereOrInsert(
159463 WhereOrSet *pSet, /* The WhereOrSet to be updated */
159464 Bitmask prereq, /* Prerequisites of the new entry */
159465 LogEst rRun, /* Run-cost of the new entry */
159466 LogEst nOut /* Number of outputs for the new entry */
159467){
159468 u16 i;
159469 WhereOrCost *p;
159470 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
159471 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
159472 goto whereOrInsert_done;
159473 }
159474 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
159475 return 0;
159476 }
159477 }
159478 if( pSet->n<N_OR_COST ){
159479 p = &pSet->a[pSet->n++];
159480 p->nOut = nOut;
159481 }else{
159482 p = pSet->a;
159483 for(i=1; i<pSet->n; i++){
159484 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
159485 }
159486 if( p->rRun<=rRun ) return 0;
159487 }
159488whereOrInsert_done:
159489 p->prereq = prereq;
159490 p->rRun = rRun;
159491 if( p->nOut>nOut ) p->nOut = nOut;
159492 return 1;
159493}
159494
159495/*
159496** Return the bitmask for the given cursor number. Return 0 if
159497** iCursor is not in the set.
159498*/
159499SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
159500 int i;
159501 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
159502 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
159503 assert( iCursor>=-1 );
159504 if( pMaskSet->ix[0]==iCursor ){
159505 return 1;
159506 }
159507 for(i=1; i<pMaskSet->n; i++){
159508 if( pMaskSet->ix[i]==iCursor ){
159509 return MASKBIT(i);
159510 }
159511 }
159512 return 0;
159513}
159514
159515/* Allocate memory that is automatically freed when pWInfo is freed.
159516*/
159517SQLITE_PRIVATE void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte){
159518 WhereMemBlock *pBlock;
159519 pBlock = sqlite3DbMallocRawNN(db: pWInfo->pParse->db, n: nByte+sizeof(*pBlock));
159520 if( pBlock ){
159521 pBlock->pNext = pWInfo->pMemToFree;
159522 pBlock->sz = nByte;
159523 pWInfo->pMemToFree = pBlock;
159524 pBlock++;
159525 }
159526 return (void*)pBlock;
159527}
159528SQLITE_PRIVATE void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte){
159529 void *pNew = sqlite3WhereMalloc(pWInfo, nByte);
159530 if( pNew && pOld ){
159531 WhereMemBlock *pOldBlk = (WhereMemBlock*)pOld;
159532 pOldBlk--;
159533 assert( pOldBlk->sz<nByte );
159534 memcpy(dest: pNew, src: pOld, n: pOldBlk->sz);
159535 }
159536 return pNew;
159537}
159538
159539/*
159540** Create a new mask for cursor iCursor.
159541**
159542** There is one cursor per table in the FROM clause. The number of
159543** tables in the FROM clause is limited by a test early in the
159544** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
159545** array will never overflow.
159546*/
159547static void createMask(WhereMaskSet *pMaskSet, int iCursor){
159548 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
159549 pMaskSet->ix[pMaskSet->n++] = iCursor;
159550}
159551
159552/*
159553** If the right-hand branch of the expression is a TK_COLUMN, then return
159554** a pointer to the right-hand branch. Otherwise, return NULL.
159555*/
159556static Expr *whereRightSubexprIsColumn(Expr *p){
159557 p = sqlite3ExprSkipCollateAndLikely(pExpr: p->pRight);
159558 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
159559 return p;
159560 }
159561 return 0;
159562}
159563
159564/*
159565** Advance to the next WhereTerm that matches according to the criteria
159566** established when the pScan object was initialized by whereScanInit().
159567** Return NULL if there are no more matching WhereTerms.
159568*/
159569static WhereTerm *whereScanNext(WhereScan *pScan){
159570 int iCur; /* The cursor on the LHS of the term */
159571 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
159572 Expr *pX; /* An expression being tested */
159573 WhereClause *pWC; /* Shorthand for pScan->pWC */
159574 WhereTerm *pTerm; /* The term being tested */
159575 int k = pScan->k; /* Where to start scanning */
159576
159577 assert( pScan->iEquiv<=pScan->nEquiv );
159578 pWC = pScan->pWC;
159579 while(1){
159580 iColumn = pScan->aiColumn[pScan->iEquiv-1];
159581 iCur = pScan->aiCur[pScan->iEquiv-1];
159582 assert( pWC!=0 );
159583 assert( iCur>=0 );
159584 do{
159585 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
159586 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
159587 if( pTerm->leftCursor==iCur
159588 && pTerm->u.x.leftColumn==iColumn
159589 && (iColumn!=XN_EXPR
159590 || sqlite3ExprCompareSkip(pA: pTerm->pExpr->pLeft,
159591 pB: pScan->pIdxExpr,iTab: iCur)==0)
159592 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
159593 ){
159594 if( (pTerm->eOperator & WO_EQUIV)!=0
159595 && pScan->nEquiv<ArraySize(pScan->aiCur)
159596 && (pX = whereRightSubexprIsColumn(p: pTerm->pExpr))!=0
159597 ){
159598 int j;
159599 for(j=0; j<pScan->nEquiv; j++){
159600 if( pScan->aiCur[j]==pX->iTable
159601 && pScan->aiColumn[j]==pX->iColumn ){
159602 break;
159603 }
159604 }
159605 if( j==pScan->nEquiv ){
159606 pScan->aiCur[j] = pX->iTable;
159607 pScan->aiColumn[j] = pX->iColumn;
159608 pScan->nEquiv++;
159609 }
159610 }
159611 if( (pTerm->eOperator & pScan->opMask)!=0 ){
159612 /* Verify the affinity and collating sequence match */
159613 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
159614 CollSeq *pColl;
159615 Parse *pParse = pWC->pWInfo->pParse;
159616 pX = pTerm->pExpr;
159617 if( !sqlite3IndexAffinityOk(pExpr: pX, idx_affinity: pScan->idxaff) ){
159618 continue;
159619 }
159620 assert(pX->pLeft);
159621 pColl = sqlite3ExprCompareCollSeq(pParse, p: pX);
159622 if( pColl==0 ) pColl = pParse->db->pDfltColl;
159623 if( sqlite3StrICmp(zLeft: pColl->zName, zRight: pScan->zCollName) ){
159624 continue;
159625 }
159626 }
159627 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
159628 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
159629 && pX->op==TK_COLUMN
159630 && pX->iTable==pScan->aiCur[0]
159631 && pX->iColumn==pScan->aiColumn[0]
159632 ){
159633 testcase( pTerm->eOperator & WO_IS );
159634 continue;
159635 }
159636 pScan->pWC = pWC;
159637 pScan->k = k+1;
159638#ifdef WHERETRACE_ENABLED
159639 if( sqlite3WhereTrace & 0x20000 ){
159640 int ii;
159641 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
159642 pTerm, pScan->nEquiv);
159643 for(ii=0; ii<pScan->nEquiv; ii++){
159644 sqlite3DebugPrintf(" {%d:%d}",
159645 pScan->aiCur[ii], pScan->aiColumn[ii]);
159646 }
159647 sqlite3DebugPrintf("\n");
159648 }
159649#endif
159650 return pTerm;
159651 }
159652 }
159653 }
159654 pWC = pWC->pOuter;
159655 k = 0;
159656 }while( pWC!=0 );
159657 if( pScan->iEquiv>=pScan->nEquiv ) break;
159658 pWC = pScan->pOrigWC;
159659 k = 0;
159660 pScan->iEquiv++;
159661 }
159662 return 0;
159663}
159664
159665/*
159666** This is whereScanInit() for the case of an index on an expression.
159667** It is factored out into a separate tail-recursion subroutine so that
159668** the normal whereScanInit() routine, which is a high-runner, does not
159669** need to push registers onto the stack as part of its prologue.
159670*/
159671static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
159672 pScan->idxaff = sqlite3ExprAffinity(pExpr: pScan->pIdxExpr);
159673 return whereScanNext(pScan);
159674}
159675
159676/*
159677** Initialize a WHERE clause scanner object. Return a pointer to the
159678** first match. Return NULL if there are no matches.
159679**
159680** The scanner will be searching the WHERE clause pWC. It will look
159681** for terms of the form "X <op> <expr>" where X is column iColumn of table
159682** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
159683** must be one of the indexes of table iCur.
159684**
159685** The <op> must be one of the operators described by opMask.
159686**
159687** If the search is for X and the WHERE clause contains terms of the
159688** form X=Y then this routine might also return terms of the form
159689** "Y <op> <expr>". The number of levels of transitivity is limited,
159690** but is enough to handle most commonly occurring SQL statements.
159691**
159692** If X is not the INTEGER PRIMARY KEY then X must be compatible with
159693** index pIdx.
159694*/
159695static WhereTerm *whereScanInit(
159696 WhereScan *pScan, /* The WhereScan object being initialized */
159697 WhereClause *pWC, /* The WHERE clause to be scanned */
159698 int iCur, /* Cursor to scan for */
159699 int iColumn, /* Column to scan for */
159700 u32 opMask, /* Operator(s) to scan for */
159701 Index *pIdx /* Must be compatible with this index */
159702){
159703 pScan->pOrigWC = pWC;
159704 pScan->pWC = pWC;
159705 pScan->pIdxExpr = 0;
159706 pScan->idxaff = 0;
159707 pScan->zCollName = 0;
159708 pScan->opMask = opMask;
159709 pScan->k = 0;
159710 pScan->aiCur[0] = iCur;
159711 pScan->nEquiv = 1;
159712 pScan->iEquiv = 1;
159713 if( pIdx ){
159714 int j = iColumn;
159715 iColumn = pIdx->aiColumn[j];
159716 if( iColumn==pIdx->pTable->iPKey ){
159717 iColumn = XN_ROWID;
159718 }else if( iColumn>=0 ){
159719 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
159720 pScan->zCollName = pIdx->azColl[j];
159721 }else if( iColumn==XN_EXPR ){
159722 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
159723 pScan->zCollName = pIdx->azColl[j];
159724 pScan->aiColumn[0] = XN_EXPR;
159725 return whereScanInitIndexExpr(pScan);
159726 }
159727 }else if( iColumn==XN_EXPR ){
159728 return 0;
159729 }
159730 pScan->aiColumn[0] = iColumn;
159731 return whereScanNext(pScan);
159732}
159733
159734/*
159735** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
159736** where X is a reference to the iColumn of table iCur or of index pIdx
159737** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
159738** the op parameter. Return a pointer to the term. Return 0 if not found.
159739**
159740** If pIdx!=0 then it must be one of the indexes of table iCur.
159741** Search for terms matching the iColumn-th column of pIdx
159742** rather than the iColumn-th column of table iCur.
159743**
159744** The term returned might by Y=<expr> if there is another constraint in
159745** the WHERE clause that specifies that X=Y. Any such constraints will be
159746** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
159747** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
159748** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
159749** other equivalent values. Hence a search for X will return <expr> if X=A1
159750** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
159751**
159752** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
159753** then try for the one with no dependencies on <expr> - in other words where
159754** <expr> is a constant expression of some kind. Only return entries of
159755** the form "X <op> Y" where Y is a column in another table if no terms of
159756** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
159757** exist, try to return a term that does not use WO_EQUIV.
159758*/
159759SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
159760 WhereClause *pWC, /* The WHERE clause to be searched */
159761 int iCur, /* Cursor number of LHS */
159762 int iColumn, /* Column number of LHS */
159763 Bitmask notReady, /* RHS must not overlap with this mask */
159764 u32 op, /* Mask of WO_xx values describing operator */
159765 Index *pIdx /* Must be compatible with this index, if not NULL */
159766){
159767 WhereTerm *pResult = 0;
159768 WhereTerm *p;
159769 WhereScan scan;
159770
159771 p = whereScanInit(pScan: &scan, pWC, iCur, iColumn, opMask: op, pIdx);
159772 op &= WO_EQ|WO_IS;
159773 while( p ){
159774 if( (p->prereqRight & notReady)==0 ){
159775 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
159776 testcase( p->eOperator & WO_IS );
159777 return p;
159778 }
159779 if( pResult==0 ) pResult = p;
159780 }
159781 p = whereScanNext(pScan: &scan);
159782 }
159783 return pResult;
159784}
159785
159786/*
159787** This function searches pList for an entry that matches the iCol-th column
159788** of index pIdx.
159789**
159790** If such an expression is found, its index in pList->a[] is returned. If
159791** no expression is found, -1 is returned.
159792*/
159793static int findIndexCol(
159794 Parse *pParse, /* Parse context */
159795 ExprList *pList, /* Expression list to search */
159796 int iBase, /* Cursor for table associated with pIdx */
159797 Index *pIdx, /* Index to match column of */
159798 int iCol /* Column of index to match */
159799){
159800 int i;
159801 const char *zColl = pIdx->azColl[iCol];
159802
159803 for(i=0; i<pList->nExpr; i++){
159804 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr: pList->a[i].pExpr);
159805 if( ALWAYS(p!=0)
159806 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
159807 && p->iColumn==pIdx->aiColumn[iCol]
159808 && p->iTable==iBase
159809 ){
159810 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pExpr: pList->a[i].pExpr);
159811 if( 0==sqlite3StrICmp(zLeft: pColl->zName, zRight: zColl) ){
159812 return i;
159813 }
159814 }
159815 }
159816
159817 return -1;
159818}
159819
159820/*
159821** Return TRUE if the iCol-th column of index pIdx is NOT NULL
159822*/
159823static int indexColumnNotNull(Index *pIdx, int iCol){
159824 int j;
159825 assert( pIdx!=0 );
159826 assert( iCol>=0 && iCol<pIdx->nColumn );
159827 j = pIdx->aiColumn[iCol];
159828 if( j>=0 ){
159829 return pIdx->pTable->aCol[j].notNull;
159830 }else if( j==(-1) ){
159831 return 1;
159832 }else{
159833 assert( j==(-2) );
159834 return 0; /* Assume an indexed expression can always yield a NULL */
159835
159836 }
159837}
159838
159839/*
159840** Return true if the DISTINCT expression-list passed as the third argument
159841** is redundant.
159842**
159843** A DISTINCT list is redundant if any subset of the columns in the
159844** DISTINCT list are collectively unique and individually non-null.
159845*/
159846static int isDistinctRedundant(
159847 Parse *pParse, /* Parsing context */
159848 SrcList *pTabList, /* The FROM clause */
159849 WhereClause *pWC, /* The WHERE clause */
159850 ExprList *pDistinct /* The result set that needs to be DISTINCT */
159851){
159852 Table *pTab;
159853 Index *pIdx;
159854 int i;
159855 int iBase;
159856
159857 /* If there is more than one table or sub-select in the FROM clause of
159858 ** this query, then it will not be possible to show that the DISTINCT
159859 ** clause is redundant. */
159860 if( pTabList->nSrc!=1 ) return 0;
159861 iBase = pTabList->a[0].iCursor;
159862 pTab = pTabList->a[0].pTab;
159863
159864 /* If any of the expressions is an IPK column on table iBase, then return
159865 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
159866 ** current SELECT is a correlated sub-query.
159867 */
159868 for(i=0; i<pDistinct->nExpr; i++){
159869 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr: pDistinct->a[i].pExpr);
159870 if( NEVER(p==0) ) continue;
159871 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
159872 if( p->iTable==iBase && p->iColumn<0 ) return 1;
159873 }
159874
159875 /* Loop through all indices on the table, checking each to see if it makes
159876 ** the DISTINCT qualifier redundant. It does so if:
159877 **
159878 ** 1. The index is itself UNIQUE, and
159879 **
159880 ** 2. All of the columns in the index are either part of the pDistinct
159881 ** list, or else the WHERE clause contains a term of the form "col=X",
159882 ** where X is a constant value. The collation sequences of the
159883 ** comparison and select-list expressions must match those of the index.
159884 **
159885 ** 3. All of those index columns for which the WHERE clause does not
159886 ** contain a "col=X" term are subject to a NOT NULL constraint.
159887 */
159888 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
159889 if( !IsUniqueIndex(pIdx) ) continue;
159890 if( pIdx->pPartIdxWhere ) continue;
159891 for(i=0; i<pIdx->nKeyCol; i++){
159892 if( 0==sqlite3WhereFindTerm(pWC, iCur: iBase, iColumn: i, notReady: ~(Bitmask)0, WO_EQ, pIdx) ){
159893 if( findIndexCol(pParse, pList: pDistinct, iBase, pIdx, iCol: i)<0 ) break;
159894 if( indexColumnNotNull(pIdx, iCol: i)==0 ) break;
159895 }
159896 }
159897 if( i==pIdx->nKeyCol ){
159898 /* This index implies that the DISTINCT qualifier is redundant. */
159899 return 1;
159900 }
159901 }
159902
159903 return 0;
159904}
159905
159906
159907/*
159908** Estimate the logarithm of the input value to base 2.
159909*/
159910static LogEst estLog(LogEst N){
159911 return N<=10 ? 0 : sqlite3LogEst(x: N) - 33;
159912}
159913
159914/*
159915** Convert OP_Column opcodes to OP_Copy in previously generated code.
159916**
159917** This routine runs over generated VDBE code and translates OP_Column
159918** opcodes into OP_Copy when the table is being accessed via co-routine
159919** instead of via table lookup.
159920**
159921** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
159922** cursor iTabCur are transformed into OP_Sequence opcode for the
159923** iAutoidxCur cursor, in order to generate unique rowids for the
159924** automatic index being generated.
159925*/
159926static void translateColumnToCopy(
159927 Parse *pParse, /* Parsing context */
159928 int iStart, /* Translate from this opcode to the end */
159929 int iTabCur, /* OP_Column/OP_Rowid references to this table */
159930 int iRegister, /* The first column is in this register */
159931 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
159932){
159933 Vdbe *v = pParse->pVdbe;
159934 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: iStart);
159935 int iEnd = sqlite3VdbeCurrentAddr(p: v);
159936 if( pParse->db->mallocFailed ) return;
159937 for(; iStart<iEnd; iStart++, pOp++){
159938 if( pOp->p1!=iTabCur ) continue;
159939 if( pOp->opcode==OP_Column ){
159940 pOp->opcode = OP_Copy;
159941 pOp->p1 = pOp->p2 + iRegister;
159942 pOp->p2 = pOp->p3;
159943 pOp->p3 = 0;
159944 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
159945 }else if( pOp->opcode==OP_Rowid ){
159946 pOp->opcode = OP_Sequence;
159947 pOp->p1 = iAutoidxCur;
159948#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
159949 if( iAutoidxCur==0 ){
159950 pOp->opcode = OP_Null;
159951 pOp->p3 = 0;
159952 }
159953#endif
159954 }
159955 }
159956}
159957
159958/*
159959** Two routines for printing the content of an sqlite3_index_info
159960** structure. Used for testing and debugging only. If neither
159961** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
159962** are no-ops.
159963*/
159964#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
159965static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
159966 int i;
159967 if( (sqlite3WhereTrace & 0x10)==0 ) return;
159968 for(i=0; i<p->nConstraint; i++){
159969 sqlite3DebugPrintf(
159970 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
159971 i,
159972 p->aConstraint[i].iColumn,
159973 p->aConstraint[i].iTermOffset,
159974 p->aConstraint[i].op,
159975 p->aConstraint[i].usable,
159976 sqlite3_vtab_collation(p,i));
159977 }
159978 for(i=0; i<p->nOrderBy; i++){
159979 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
159980 i,
159981 p->aOrderBy[i].iColumn,
159982 p->aOrderBy[i].desc);
159983 }
159984}
159985static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
159986 int i;
159987 if( (sqlite3WhereTrace & 0x10)==0 ) return;
159988 for(i=0; i<p->nConstraint; i++){
159989 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
159990 i,
159991 p->aConstraintUsage[i].argvIndex,
159992 p->aConstraintUsage[i].omit);
159993 }
159994 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
159995 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
159996 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
159997 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
159998 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
159999}
160000#else
160001#define whereTraceIndexInfoInputs(A)
160002#define whereTraceIndexInfoOutputs(A)
160003#endif
160004
160005/*
160006** We know that pSrc is an operand of an outer join. Return true if
160007** pTerm is a constraint that is compatible with that join.
160008**
160009** pTerm must be EP_OuterON if pSrc is the right operand of an
160010** outer join. pTerm can be either EP_OuterON or EP_InnerON if pSrc
160011** is the left operand of a RIGHT join.
160012**
160013** See https://sqlite.org/forum/forumpost/206d99a16dd9212f
160014** for an example of a WHERE clause constraints that may not be used on
160015** the right table of a RIGHT JOIN because the constraint implies a
160016** not-NULL condition on the left table of the RIGHT JOIN.
160017*/
160018static int constraintCompatibleWithOuterJoin(
160019 const WhereTerm *pTerm, /* WHERE clause term to check */
160020 const SrcItem *pSrc /* Table we are trying to access */
160021){
160022 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
160023 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
160024 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
160025 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
160026 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
160027 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
160028 || pTerm->pExpr->w.iJoin != pSrc->iCursor
160029 ){
160030 return 0;
160031 }
160032 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
160033 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
160034 ){
160035 return 0;
160036 }
160037 return 1;
160038}
160039
160040
160041
160042#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
160043/*
160044** Return TRUE if the WHERE clause term pTerm is of a form where it
160045** could be used with an index to access pSrc, assuming an appropriate
160046** index existed.
160047*/
160048static int termCanDriveIndex(
160049 const WhereTerm *pTerm, /* WHERE clause term to check */
160050 const SrcItem *pSrc, /* Table we are trying to access */
160051 const Bitmask notReady /* Tables in outer loops of the join */
160052){
160053 char aff;
160054 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
160055 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
160056 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
160057 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
160058 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
160059 ){
160060 return 0; /* See https://sqlite.org/forum/forumpost/51e6959f61 */
160061 }
160062 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
160063 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
160064 if( pTerm->u.x.leftColumn<0 ) return 0;
160065 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
160066 if( !sqlite3IndexAffinityOk(pExpr: pTerm->pExpr, idx_affinity: aff) ) return 0;
160067 testcase( pTerm->pExpr->op==TK_IS );
160068 return 1;
160069}
160070#endif
160071
160072
160073#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
160074
160075#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
160076/*
160077** Argument pIdx represents an automatic index that the current statement
160078** will create and populate. Add an OP_Explain with text of the form:
160079**
160080** CREATE AUTOMATIC INDEX ON <table>(<cols>) [WHERE <expr>]
160081**
160082** This is only required if sqlite3_stmt_scanstatus() is enabled, to
160083** associate an SQLITE_SCANSTAT_NCYCLE and SQLITE_SCANSTAT_NLOOP
160084** values with. In order to avoid breaking legacy code and test cases,
160085** the OP_Explain is not added if this is an EXPLAIN QUERY PLAN command.
160086*/
160087static void explainAutomaticIndex(
160088 Parse *pParse,
160089 Index *pIdx, /* Automatic index to explain */
160090 int bPartial, /* True if pIdx is a partial index */
160091 int *pAddrExplain /* OUT: Address of OP_Explain */
160092){
160093 if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){
160094 Table *pTab = pIdx->pTable;
160095 const char *zSep = "";
160096 char *zText = 0;
160097 int ii = 0;
160098 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
160099 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
160100 assert( pIdx->nColumn>1 );
160101 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID );
160102 for(ii=0; ii<(pIdx->nColumn-1); ii++){
160103 const char *zName = 0;
160104 int iCol = pIdx->aiColumn[ii];
160105
160106 zName = pTab->aCol[iCol].zCnName;
160107 sqlite3_str_appendf(pStr, "%s%s", zSep, zName);
160108 zSep = ", ";
160109 }
160110 zText = sqlite3_str_finish(pStr);
160111 if( zText==0 ){
160112 sqlite3OomFault(pParse->db);
160113 }else{
160114 *pAddrExplain = sqlite3VdbeExplain(
160115 pParse, 0, "%s)%s", zText, (bPartial ? " WHERE <expr>" : "")
160116 );
160117 sqlite3_free(zText);
160118 }
160119 }
160120}
160121#else
160122# define explainAutomaticIndex(a,b,c,d)
160123#endif
160124
160125/*
160126** Generate code to construct the Index object for an automatic index
160127** and to set up the WhereLevel object pLevel so that the code generator
160128** makes use of the automatic index.
160129*/
160130static SQLITE_NOINLINE void constructAutomaticIndex(
160131 Parse *pParse, /* The parsing context */
160132 WhereClause *pWC, /* The WHERE clause */
160133 const Bitmask notReady, /* Mask of cursors that are not available */
160134 WhereLevel *pLevel /* Write new index here */
160135){
160136 int nKeyCol; /* Number of columns in the constructed index */
160137 WhereTerm *pTerm; /* A single term of the WHERE clause */
160138 WhereTerm *pWCEnd; /* End of pWC->a[] */
160139 Index *pIdx; /* Object describing the transient index */
160140 Vdbe *v; /* Prepared statement under construction */
160141 int addrInit; /* Address of the initialization bypass jump */
160142 Table *pTable; /* The table being indexed */
160143 int addrTop; /* Top of the index fill loop */
160144 int regRecord; /* Register holding an index record */
160145 int n; /* Column counter */
160146 int i; /* Loop counter */
160147 int mxBitCol; /* Maximum column in pSrc->colUsed */
160148 CollSeq *pColl; /* Collating sequence to on a column */
160149 WhereLoop *pLoop; /* The Loop object */
160150 char *zNotUsed; /* Extra space on the end of pIdx */
160151 Bitmask idxCols; /* Bitmap of columns used for indexing */
160152 Bitmask extraCols; /* Bitmap of additional columns */
160153 u8 sentWarning = 0; /* True if a warning has been issued */
160154 u8 useBloomFilter = 0; /* True to also add a Bloom filter */
160155 Expr *pPartial = 0; /* Partial Index Expression */
160156 int iContinue = 0; /* Jump here to skip excluded rows */
160157 SrcList *pTabList; /* The complete FROM clause */
160158 SrcItem *pSrc; /* The FROM clause term to get the next index */
160159 int addrCounter = 0; /* Address where integer counter is initialized */
160160 int regBase; /* Array of registers where record is assembled */
160161#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
160162 int addrExp = 0; /* Address of OP_Explain */
160163#endif
160164
160165 /* Generate code to skip over the creation and initialization of the
160166 ** transient index on 2nd and subsequent iterations of the loop. */
160167 v = pParse->pVdbe;
160168 assert( v!=0 );
160169 addrInit = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
160170
160171 /* Count the number of columns that will be added to the index
160172 ** and used to match WHERE clause constraints */
160173 nKeyCol = 0;
160174 pTabList = pWC->pWInfo->pTabList;
160175 pSrc = &pTabList->a[pLevel->iFrom];
160176 pTable = pSrc->pTab;
160177 pWCEnd = &pWC->a[pWC->nTerm];
160178 pLoop = pLevel->pWLoop;
160179 idxCols = 0;
160180 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
160181 Expr *pExpr = pTerm->pExpr;
160182 /* Make the automatic index a partial index if there are terms in the
160183 ** WHERE clause (or the ON clause of a LEFT join) that constrain which
160184 ** rows of the target table (pSrc) that can be used. */
160185 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
160186 && sqlite3ExprIsSingleTableConstraint(pExpr, pSrcList: pTabList, iSrc: pLevel->iFrom)
160187 ){
160188 pPartial = sqlite3ExprAnd(pParse, pLeft: pPartial,
160189 pRight: sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0));
160190 }
160191 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
160192 int iCol;
160193 Bitmask cMask;
160194 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
160195 iCol = pTerm->u.x.leftColumn;
160196 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
160197 testcase( iCol==BMS );
160198 testcase( iCol==BMS-1 );
160199 if( !sentWarning ){
160200 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
160201 zFormat: "automatic index on %s(%s)", pTable->zName,
160202 pTable->aCol[iCol].zCnName);
160203 sentWarning = 1;
160204 }
160205 if( (idxCols & cMask)==0 ){
160206 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
160207 goto end_auto_index_create;
160208 }
160209 pLoop->aLTerm[nKeyCol++] = pTerm;
160210 idxCols |= cMask;
160211 }
160212 }
160213 }
160214 assert( nKeyCol>0 || pParse->db->mallocFailed );
160215 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
160216 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
160217 | WHERE_AUTO_INDEX;
160218
160219 /* Count the number of additional columns needed to create a
160220 ** covering index. A "covering index" is an index that contains all
160221 ** columns that are needed by the query. With a covering index, the
160222 ** original table never needs to be accessed. Automatic indices must
160223 ** be a covering index because the index will not be updated if the
160224 ** original table changes and the index and table cannot both be used
160225 ** if they go out of sync.
160226 */
160227 if( IsView(pTable) ){
160228 extraCols = ALLBITS;
160229 }else{
160230 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
160231 }
160232 mxBitCol = MIN(BMS-1,pTable->nCol);
160233 testcase( pTable->nCol==BMS-1 );
160234 testcase( pTable->nCol==BMS-2 );
160235 for(i=0; i<mxBitCol; i++){
160236 if( extraCols & MASKBIT(i) ) nKeyCol++;
160237 }
160238 if( pSrc->colUsed & MASKBIT(BMS-1) ){
160239 nKeyCol += pTable->nCol - BMS + 1;
160240 }
160241
160242 /* Construct the Index object to describe this index */
160243 pIdx = sqlite3AllocateIndexObject(db: pParse->db, nCol: nKeyCol+1, nExtra: 0, ppExtra: &zNotUsed);
160244 if( pIdx==0 ) goto end_auto_index_create;
160245 pLoop->u.btree.pIndex = pIdx;
160246 pIdx->zName = "auto-index";
160247 pIdx->pTable = pTable;
160248 n = 0;
160249 idxCols = 0;
160250 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
160251 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
160252 int iCol;
160253 Bitmask cMask;
160254 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
160255 iCol = pTerm->u.x.leftColumn;
160256 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
160257 testcase( iCol==BMS-1 );
160258 testcase( iCol==BMS );
160259 if( (idxCols & cMask)==0 ){
160260 Expr *pX = pTerm->pExpr;
160261 idxCols |= cMask;
160262 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
160263 pColl = sqlite3ExprCompareCollSeq(pParse, p: pX);
160264 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
160265 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
160266 n++;
160267 if( ALWAYS(pX->pLeft!=0)
160268 && sqlite3ExprAffinity(pExpr: pX->pLeft)!=SQLITE_AFF_TEXT
160269 ){
160270 /* TUNING: only use a Bloom filter on an automatic index
160271 ** if one or more key columns has the ability to hold numeric
160272 ** values, since strings all have the same hash in the Bloom
160273 ** filter implementation and hence a Bloom filter on a text column
160274 ** is not usually helpful. */
160275 useBloomFilter = 1;
160276 }
160277 }
160278 }
160279 }
160280 assert( (u32)n==pLoop->u.btree.nEq );
160281
160282 /* Add additional columns needed to make the automatic index into
160283 ** a covering index */
160284 for(i=0; i<mxBitCol; i++){
160285 if( extraCols & MASKBIT(i) ){
160286 pIdx->aiColumn[n] = i;
160287 pIdx->azColl[n] = sqlite3StrBINARY;
160288 n++;
160289 }
160290 }
160291 if( pSrc->colUsed & MASKBIT(BMS-1) ){
160292 for(i=BMS-1; i<pTable->nCol; i++){
160293 pIdx->aiColumn[n] = i;
160294 pIdx->azColl[n] = sqlite3StrBINARY;
160295 n++;
160296 }
160297 }
160298 assert( n==nKeyCol );
160299 pIdx->aiColumn[n] = XN_ROWID;
160300 pIdx->azColl[n] = sqlite3StrBINARY;
160301
160302 /* Create the automatic index */
160303 explainAutomaticIndex(pParse, pIdx, pPartial!=0, &addrExp);
160304 assert( pLevel->iIdxCur>=0 );
160305 pLevel->iIdxCur = pParse->nTab++;
160306 sqlite3VdbeAddOp2(p: v, OP_OpenAutoindex, p1: pLevel->iIdxCur, p2: nKeyCol+1);
160307 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
160308 VdbeComment((v, "for %s", pTable->zName));
160309 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) && useBloomFilter ){
160310 sqlite3WhereExplainBloomFilter(pParse, pWInfo: pWC->pWInfo, pLevel);
160311 pLevel->regFilter = ++pParse->nMem;
160312 sqlite3VdbeAddOp2(p: v, OP_Blob, p1: 10000, p2: pLevel->regFilter);
160313 }
160314
160315 /* Fill the automatic index with content */
160316 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] );
160317 if( pSrc->fg.viaCoroutine ){
160318 int regYield = pSrc->regReturn;
160319 addrCounter = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: 0);
160320 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: pSrc->addrFillSub);
160321 addrTop = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: regYield);
160322 VdbeCoverage(v);
160323 VdbeComment((v, "next row of %s", pSrc->pTab->zName));
160324 }else{
160325 addrTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: pLevel->iTabCur); VdbeCoverage(v);
160326 }
160327 if( pPartial ){
160328 iContinue = sqlite3VdbeMakeLabel(pParse);
160329 sqlite3ExprIfFalse(pParse, pExpr: pPartial, dest: iContinue, SQLITE_JUMPIFNULL);
160330 pLoop->wsFlags |= WHERE_PARTIALIDX;
160331 }
160332 regRecord = sqlite3GetTempReg(pParse);
160333 regBase = sqlite3GenerateIndexKey(
160334 pParse, pIdx, iDataCur: pLevel->iTabCur, regOut: regRecord, prefixOnly: 0, piPartIdxLabel: 0, pPrior: 0, regPrior: 0
160335 );
160336 if( pLevel->regFilter ){
160337 sqlite3VdbeAddOp4Int(p: v, OP_FilterAdd, p1: pLevel->regFilter, p2: 0,
160338 p3: regBase, p4: pLoop->u.btree.nEq);
160339 }
160340 sqlite3VdbeScanStatusCounters(v, addrExp, addrExp, sqlite3VdbeCurrentAddr(v));
160341 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: pLevel->iIdxCur, p2: regRecord);
160342 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
160343 if( pPartial ) sqlite3VdbeResolveLabel(v, x: iContinue);
160344 if( pSrc->fg.viaCoroutine ){
160345 sqlite3VdbeChangeP2(p: v, addr: addrCounter, val: regBase+n);
160346 testcase( pParse->db->mallocFailed );
160347 assert( pLevel->iIdxCur>0 );
160348 translateColumnToCopy(pParse, iStart: addrTop, iTabCur: pLevel->iTabCur,
160349 iRegister: pSrc->regResult, iAutoidxCur: pLevel->iIdxCur);
160350 sqlite3VdbeGoto(p: v, iDest: addrTop);
160351 pSrc->fg.viaCoroutine = 0;
160352 }else{
160353 sqlite3VdbeAddOp2(p: v, OP_Next, p1: pLevel->iTabCur, p2: addrTop+1); VdbeCoverage(v);
160354 sqlite3VdbeChangeP5(p: v, SQLITE_STMTSTATUS_AUTOINDEX);
160355 }
160356 sqlite3VdbeJumpHere(p: v, addr: addrTop);
160357 sqlite3ReleaseTempReg(pParse, iReg: regRecord);
160358
160359 /* Jump here when skipping the initialization */
160360 sqlite3VdbeJumpHere(p: v, addr: addrInit);
160361 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
160362
160363end_auto_index_create:
160364 sqlite3ExprDelete(db: pParse->db, p: pPartial);
160365}
160366#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
160367
160368/*
160369** Generate bytecode that will initialize a Bloom filter that is appropriate
160370** for pLevel.
160371**
160372** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER
160373** flag set, initialize a Bloomfilter for them as well. Except don't do
160374** this recursive initialization if the SQLITE_BloomPulldown optimization has
160375** been turned off.
160376**
160377** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared
160378** from the loop, but the regFilter value is set to a register that implements
160379** the Bloom filter. When regFilter is positive, the
160380** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter
160381** and skip the subsequence B-Tree seek if the Bloom filter indicates that
160382** no matching rows exist.
160383**
160384** This routine may only be called if it has previously been determined that
160385** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
160386** is set.
160387*/
160388static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
160389 WhereInfo *pWInfo, /* The WHERE clause */
160390 int iLevel, /* Index in pWInfo->a[] that is pLevel */
160391 WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
160392 Bitmask notReady /* Loops that are not ready */
160393){
160394 int addrOnce; /* Address of opening OP_Once */
160395 int addrTop; /* Address of OP_Rewind */
160396 int addrCont; /* Jump here to skip a row */
160397 const WhereTerm *pTerm; /* For looping over WHERE clause terms */
160398 const WhereTerm *pWCEnd; /* Last WHERE clause term */
160399 Parse *pParse = pWInfo->pParse; /* Parsing context */
160400 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
160401 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
160402 int iCur; /* Cursor for table getting the filter */
160403 IndexedExpr *saved_pIdxEpr; /* saved copy of Parse.pIdxEpr */
160404
160405 saved_pIdxEpr = pParse->pIdxEpr;
160406 pParse->pIdxEpr = 0;
160407
160408 assert( pLoop!=0 );
160409 assert( v!=0 );
160410 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
160411
160412 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
160413 do{
160414 const SrcList *pTabList;
160415 const SrcItem *pItem;
160416 const Table *pTab;
160417 u64 sz;
160418 int iSrc;
160419 sqlite3WhereExplainBloomFilter(pParse, pWInfo, pLevel);
160420 addrCont = sqlite3VdbeMakeLabel(pParse);
160421 iCur = pLevel->iTabCur;
160422 pLevel->regFilter = ++pParse->nMem;
160423
160424 /* The Bloom filter is a Blob held in a register. Initialize it
160425 ** to zero-filled blob of at least 80K bits, but maybe more if the
160426 ** estimated size of the table is larger. We could actually
160427 ** measure the size of the table at run-time using OP_Count with
160428 ** P3==1 and use that value to initialize the blob. But that makes
160429 ** testing complicated. By basing the blob size on the value in the
160430 ** sqlite_stat1 table, testing is much easier.
160431 */
160432 pTabList = pWInfo->pTabList;
160433 iSrc = pLevel->iFrom;
160434 pItem = &pTabList->a[iSrc];
160435 assert( pItem!=0 );
160436 pTab = pItem->pTab;
160437 assert( pTab!=0 );
160438 sz = sqlite3LogEstToInt(x: pTab->nRowLogEst);
160439 if( sz<10000 ){
160440 sz = 10000;
160441 }else if( sz>10000000 ){
160442 sz = 10000000;
160443 }
160444 sqlite3VdbeAddOp2(p: v, OP_Blob, p1: (int)sz, p2: pLevel->regFilter);
160445
160446 addrTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iCur); VdbeCoverage(v);
160447 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
160448 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
160449 Expr *pExpr = pTerm->pExpr;
160450 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
160451 && sqlite3ExprIsSingleTableConstraint(pExpr, pSrcList: pTabList, iSrc)
160452 ){
160453 sqlite3ExprIfFalse(pParse, pExpr: pTerm->pExpr, dest: addrCont, SQLITE_JUMPIFNULL);
160454 }
160455 }
160456 if( pLoop->wsFlags & WHERE_IPK ){
160457 int r1 = sqlite3GetTempReg(pParse);
160458 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCur, p2: r1);
160459 sqlite3VdbeAddOp4Int(p: v, OP_FilterAdd, p1: pLevel->regFilter, p2: 0, p3: r1, p4: 1);
160460 sqlite3ReleaseTempReg(pParse, iReg: r1);
160461 }else{
160462 Index *pIdx = pLoop->u.btree.pIndex;
160463 int n = pLoop->u.btree.nEq;
160464 int r1 = sqlite3GetTempRange(pParse, nReg: n);
160465 int jj;
160466 for(jj=0; jj<n; jj++){
160467 assert( pIdx->pTable==pItem->pTab );
160468 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur: iCur, iIdxCol: jj, regOut: r1+jj);
160469 }
160470 sqlite3VdbeAddOp4Int(p: v, OP_FilterAdd, p1: pLevel->regFilter, p2: 0, p3: r1, p4: n);
160471 sqlite3ReleaseTempRange(pParse, iReg: r1, nReg: n);
160472 }
160473 sqlite3VdbeResolveLabel(v, x: addrCont);
160474 sqlite3VdbeAddOp2(p: v, OP_Next, p1: pLevel->iTabCur, p2: addrTop+1);
160475 VdbeCoverage(v);
160476 sqlite3VdbeJumpHere(p: v, addr: addrTop);
160477 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
160478 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
160479 while( ++iLevel < pWInfo->nLevel ){
160480 const SrcItem *pTabItem;
160481 pLevel = &pWInfo->a[iLevel];
160482 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
160483 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
160484 pLoop = pLevel->pWLoop;
160485 if( NEVER(pLoop==0) ) continue;
160486 if( pLoop->prereq & notReady ) continue;
160487 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
160488 ==WHERE_BLOOMFILTER
160489 ){
160490 /* This is a candidate for bloom-filter pull-down (early evaluation).
160491 ** The test that WHERE_COLUMN_IN is omitted is important, as we are
160492 ** not able to do early evaluation of bloom filters that make use of
160493 ** the IN operator */
160494 break;
160495 }
160496 }
160497 }while( iLevel < pWInfo->nLevel );
160498 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
160499 pParse->pIdxEpr = saved_pIdxEpr;
160500}
160501
160502
160503#ifndef SQLITE_OMIT_VIRTUALTABLE
160504/*
160505** Allocate and populate an sqlite3_index_info structure. It is the
160506** responsibility of the caller to eventually release the structure
160507** by passing the pointer returned by this function to freeIndexInfo().
160508*/
160509static sqlite3_index_info *allocateIndexInfo(
160510 WhereInfo *pWInfo, /* The WHERE clause */
160511 WhereClause *pWC, /* The WHERE clause being analyzed */
160512 Bitmask mUnusable, /* Ignore terms with these prereqs */
160513 SrcItem *pSrc, /* The FROM clause term that is the vtab */
160514 u16 *pmNoOmit /* Mask of terms not to omit */
160515){
160516 int i, j;
160517 int nTerm;
160518 Parse *pParse = pWInfo->pParse;
160519 struct sqlite3_index_constraint *pIdxCons;
160520 struct sqlite3_index_orderby *pIdxOrderBy;
160521 struct sqlite3_index_constraint_usage *pUsage;
160522 struct HiddenIndexInfo *pHidden;
160523 WhereTerm *pTerm;
160524 int nOrderBy;
160525 sqlite3_index_info *pIdxInfo;
160526 u16 mNoOmit = 0;
160527 const Table *pTab;
160528 int eDistinct = 0;
160529 ExprList *pOrderBy = pWInfo->pOrderBy;
160530
160531 assert( pSrc!=0 );
160532 pTab = pSrc->pTab;
160533 assert( pTab!=0 );
160534 assert( IsVirtual(pTab) );
160535
160536 /* Find all WHERE clause constraints referring to this virtual table.
160537 ** Mark each term with the TERM_OK flag. Set nTerm to the number of
160538 ** terms found.
160539 */
160540 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
160541 pTerm->wtFlags &= ~TERM_OK;
160542 if( pTerm->leftCursor != pSrc->iCursor ) continue;
160543 if( pTerm->prereqRight & mUnusable ) continue;
160544 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
160545 testcase( pTerm->eOperator & WO_IN );
160546 testcase( pTerm->eOperator & WO_ISNULL );
160547 testcase( pTerm->eOperator & WO_IS );
160548 testcase( pTerm->eOperator & WO_ALL );
160549 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
160550 if( pTerm->wtFlags & TERM_VNULL ) continue;
160551
160552 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
160553 assert( pTerm->u.x.leftColumn>=XN_ROWID );
160554 assert( pTerm->u.x.leftColumn<pTab->nCol );
160555 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
160556 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
160557 ){
160558 continue;
160559 }
160560 nTerm++;
160561 pTerm->wtFlags |= TERM_OK;
160562 }
160563
160564 /* If the ORDER BY clause contains only columns in the current
160565 ** virtual table then allocate space for the aOrderBy part of
160566 ** the sqlite3_index_info structure.
160567 */
160568 nOrderBy = 0;
160569 if( pOrderBy ){
160570 int n = pOrderBy->nExpr;
160571 for(i=0; i<n; i++){
160572 Expr *pExpr = pOrderBy->a[i].pExpr;
160573 Expr *pE2;
160574
160575 /* Skip over constant terms in the ORDER BY clause */
160576 if( sqlite3ExprIsConstant(p: pExpr) ){
160577 continue;
160578 }
160579
160580 /* Virtual tables are unable to deal with NULLS FIRST */
160581 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
160582
160583 /* First case - a direct column references without a COLLATE operator */
160584 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
160585 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
160586 continue;
160587 }
160588
160589 /* 2nd case - a column reference with a COLLATE operator. Only match
160590 ** of the COLLATE operator matches the collation of the column. */
160591 if( pExpr->op==TK_COLLATE
160592 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
160593 && pE2->iTable==pSrc->iCursor
160594 ){
160595 const char *zColl; /* The collating sequence name */
160596 assert( !ExprHasProperty(pExpr, EP_IntValue) );
160597 assert( pExpr->u.zToken!=0 );
160598 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
160599 pExpr->iColumn = pE2->iColumn;
160600 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
160601 zColl = sqlite3ColumnColl(pCol: &pTab->aCol[pE2->iColumn]);
160602 if( zColl==0 ) zColl = sqlite3StrBINARY;
160603 if( sqlite3_stricmp(zLeft: pExpr->u.zToken, zRight: zColl)==0 ) continue;
160604 }
160605
160606 /* No matches cause a break out of the loop */
160607 break;
160608 }
160609 if( i==n ){
160610 nOrderBy = n;
160611 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
160612 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
160613 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
160614 eDistinct = 1;
160615 }
160616 }
160617 }
160618
160619 /* Allocate the sqlite3_index_info structure
160620 */
160621 pIdxInfo = sqlite3DbMallocZero(db: pParse->db, n: sizeof(*pIdxInfo)
160622 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
160623 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden)
160624 + sizeof(sqlite3_value*)*nTerm );
160625 if( pIdxInfo==0 ){
160626 sqlite3ErrorMsg(pParse, zFormat: "out of memory");
160627 return 0;
160628 }
160629 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
160630 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
160631 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
160632 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
160633 pIdxInfo->aConstraint = pIdxCons;
160634 pIdxInfo->aOrderBy = pIdxOrderBy;
160635 pIdxInfo->aConstraintUsage = pUsage;
160636 pHidden->pWC = pWC;
160637 pHidden->pParse = pParse;
160638 pHidden->eDistinct = eDistinct;
160639 pHidden->mIn = 0;
160640 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
160641 u16 op;
160642 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
160643 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
160644 pIdxCons[j].iTermOffset = i;
160645 op = pTerm->eOperator & WO_ALL;
160646 if( op==WO_IN ){
160647 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
160648 pHidden->mIn |= SMASKBIT32(j);
160649 }
160650 op = WO_EQ;
160651 }
160652 if( op==WO_AUX ){
160653 pIdxCons[j].op = pTerm->eMatchOp;
160654 }else if( op & (WO_ISNULL|WO_IS) ){
160655 if( op==WO_ISNULL ){
160656 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
160657 }else{
160658 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
160659 }
160660 }else{
160661 pIdxCons[j].op = (u8)op;
160662 /* The direct assignment in the previous line is possible only because
160663 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
160664 ** following asserts verify this fact. */
160665 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
160666 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
160667 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
160668 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
160669 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
160670 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
160671
160672 if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
160673 && sqlite3ExprIsVector(pExpr: pTerm->pExpr->pRight)
160674 ){
160675 testcase( j!=i );
160676 if( j<16 ) mNoOmit |= (1 << j);
160677 if( op==WO_LT ) pIdxCons[j].op = WO_LE;
160678 if( op==WO_GT ) pIdxCons[j].op = WO_GE;
160679 }
160680 }
160681
160682 j++;
160683 }
160684 assert( j==nTerm );
160685 pIdxInfo->nConstraint = j;
160686 for(i=j=0; i<nOrderBy; i++){
160687 Expr *pExpr = pOrderBy->a[i].pExpr;
160688 if( sqlite3ExprIsConstant(p: pExpr) ) continue;
160689 assert( pExpr->op==TK_COLUMN
160690 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
160691 && pExpr->iColumn==pExpr->pLeft->iColumn) );
160692 pIdxOrderBy[j].iColumn = pExpr->iColumn;
160693 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
160694 j++;
160695 }
160696 pIdxInfo->nOrderBy = j;
160697
160698 *pmNoOmit = mNoOmit;
160699 return pIdxInfo;
160700}
160701
160702/*
160703** Free an sqlite3_index_info structure allocated by allocateIndexInfo()
160704** and possibly modified by xBestIndex methods.
160705*/
160706static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){
160707 HiddenIndexInfo *pHidden;
160708 int i;
160709 assert( pIdxInfo!=0 );
160710 pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
160711 assert( pHidden->pParse!=0 );
160712 assert( pHidden->pParse->db==db );
160713 for(i=0; i<pIdxInfo->nConstraint; i++){
160714 sqlite3ValueFree(v: pHidden->aRhs[i]); /* IMP: R-14553-25174 */
160715 pHidden->aRhs[i] = 0;
160716 }
160717 sqlite3DbFree(db, p: pIdxInfo);
160718}
160719
160720/*
160721** The table object reference passed as the second argument to this function
160722** must represent a virtual table. This function invokes the xBestIndex()
160723** method of the virtual table with the sqlite3_index_info object that
160724** comes in as the 3rd argument to this function.
160725**
160726** If an error occurs, pParse is populated with an error message and an
160727** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
160728** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
160729** the current configuration of "unusable" flags in sqlite3_index_info can
160730** not result in a valid plan.
160731**
160732** Whether or not an error is returned, it is the responsibility of the
160733** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
160734** that this is required.
160735*/
160736static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
160737 sqlite3_vtab *pVtab = sqlite3GetVTable(db: pParse->db, pTab)->pVtab;
160738 int rc;
160739
160740 whereTraceIndexInfoInputs(p);
160741 pParse->db->nSchemaLock++;
160742 rc = pVtab->pModule->xBestIndex(pVtab, p);
160743 pParse->db->nSchemaLock--;
160744 whereTraceIndexInfoOutputs(p);
160745
160746 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
160747 if( rc==SQLITE_NOMEM ){
160748 sqlite3OomFault(db: pParse->db);
160749 }else if( !pVtab->zErrMsg ){
160750 sqlite3ErrorMsg(pParse, zFormat: "%s", sqlite3ErrStr(rc));
160751 }else{
160752 sqlite3ErrorMsg(pParse, zFormat: "%s", pVtab->zErrMsg);
160753 }
160754 }
160755 if( pTab->u.vtab.p->bAllSchemas ){
160756 sqlite3VtabUsesAllSchemas(pParse);
160757 }
160758 sqlite3_free(p: pVtab->zErrMsg);
160759 pVtab->zErrMsg = 0;
160760 return rc;
160761}
160762#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
160763
160764#ifdef SQLITE_ENABLE_STAT4
160765/*
160766** Estimate the location of a particular key among all keys in an
160767** index. Store the results in aStat as follows:
160768**
160769** aStat[0] Est. number of rows less than pRec
160770** aStat[1] Est. number of rows equal to pRec
160771**
160772** Return the index of the sample that is the smallest sample that
160773** is greater than or equal to pRec. Note that this index is not an index
160774** into the aSample[] array - it is an index into a virtual set of samples
160775** based on the contents of aSample[] and the number of fields in record
160776** pRec.
160777*/
160778static int whereKeyStats(
160779 Parse *pParse, /* Database connection */
160780 Index *pIdx, /* Index to consider domain of */
160781 UnpackedRecord *pRec, /* Vector of values to consider */
160782 int roundUp, /* Round up if true. Round down if false */
160783 tRowcnt *aStat /* OUT: stats written here */
160784){
160785 IndexSample *aSample = pIdx->aSample;
160786 int iCol; /* Index of required stats in anEq[] etc. */
160787 int i; /* Index of first sample >= pRec */
160788 int iSample; /* Smallest sample larger than or equal to pRec */
160789 int iMin = 0; /* Smallest sample not yet tested */
160790 int iTest; /* Next sample to test */
160791 int res; /* Result of comparison operation */
160792 int nField; /* Number of fields in pRec */
160793 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
160794
160795#ifndef SQLITE_DEBUG
160796 UNUSED_PARAMETER( pParse );
160797#endif
160798 assert( pRec!=0 );
160799 assert( pIdx->nSample>0 );
160800 assert( pRec->nField>0 );
160801
160802
160803 /* Do a binary search to find the first sample greater than or equal
160804 ** to pRec. If pRec contains a single field, the set of samples to search
160805 ** is simply the aSample[] array. If the samples in aSample[] contain more
160806 ** than one fields, all fields following the first are ignored.
160807 **
160808 ** If pRec contains N fields, where N is more than one, then as well as the
160809 ** samples in aSample[] (truncated to N fields), the search also has to
160810 ** consider prefixes of those samples. For example, if the set of samples
160811 ** in aSample is:
160812 **
160813 ** aSample[0] = (a, 5)
160814 ** aSample[1] = (a, 10)
160815 ** aSample[2] = (b, 5)
160816 ** aSample[3] = (c, 100)
160817 ** aSample[4] = (c, 105)
160818 **
160819 ** Then the search space should ideally be the samples above and the
160820 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
160821 ** the code actually searches this set:
160822 **
160823 ** 0: (a)
160824 ** 1: (a, 5)
160825 ** 2: (a, 10)
160826 ** 3: (a, 10)
160827 ** 4: (b)
160828 ** 5: (b, 5)
160829 ** 6: (c)
160830 ** 7: (c, 100)
160831 ** 8: (c, 105)
160832 ** 9: (c, 105)
160833 **
160834 ** For each sample in the aSample[] array, N samples are present in the
160835 ** effective sample array. In the above, samples 0 and 1 are based on
160836 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
160837 **
160838 ** Often, sample i of each block of N effective samples has (i+1) fields.
160839 ** Except, each sample may be extended to ensure that it is greater than or
160840 ** equal to the previous sample in the array. For example, in the above,
160841 ** sample 2 is the first sample of a block of N samples, so at first it
160842 ** appears that it should be 1 field in size. However, that would make it
160843 ** smaller than sample 1, so the binary search would not work. As a result,
160844 ** it is extended to two fields. The duplicates that this creates do not
160845 ** cause any problems.
160846 */
160847 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
160848 nField = pIdx->nKeyCol;
160849 }else{
160850 nField = pIdx->nColumn;
160851 }
160852 nField = MIN(pRec->nField, nField);
160853 iCol = 0;
160854 iSample = pIdx->nSample * nField;
160855 do{
160856 int iSamp; /* Index in aSample[] of test sample */
160857 int n; /* Number of fields in test sample */
160858
160859 iTest = (iMin+iSample)/2;
160860 iSamp = iTest / nField;
160861 if( iSamp>0 ){
160862 /* The proposed effective sample is a prefix of sample aSample[iSamp].
160863 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
160864 ** fields that is greater than the previous effective sample. */
160865 for(n=(iTest % nField) + 1; n<nField; n++){
160866 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
160867 }
160868 }else{
160869 n = iTest + 1;
160870 }
160871
160872 pRec->nField = n;
160873 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
160874 if( res<0 ){
160875 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
160876 iMin = iTest+1;
160877 }else if( res==0 && n<nField ){
160878 iLower = aSample[iSamp].anLt[n-1];
160879 iMin = iTest+1;
160880 res = -1;
160881 }else{
160882 iSample = iTest;
160883 iCol = n-1;
160884 }
160885 }while( res && iMin<iSample );
160886 i = iSample / nField;
160887
160888#ifdef SQLITE_DEBUG
160889 /* The following assert statements check that the binary search code
160890 ** above found the right answer. This block serves no purpose other
160891 ** than to invoke the asserts. */
160892 if( pParse->db->mallocFailed==0 ){
160893 if( res==0 ){
160894 /* If (res==0) is true, then pRec must be equal to sample i. */
160895 assert( i<pIdx->nSample );
160896 assert( iCol==nField-1 );
160897 pRec->nField = nField;
160898 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
160899 || pParse->db->mallocFailed
160900 );
160901 }else{
160902 /* Unless i==pIdx->nSample, indicating that pRec is larger than
160903 ** all samples in the aSample[] array, pRec must be smaller than the
160904 ** (iCol+1) field prefix of sample i. */
160905 assert( i<=pIdx->nSample && i>=0 );
160906 pRec->nField = iCol+1;
160907 assert( i==pIdx->nSample
160908 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
160909 || pParse->db->mallocFailed );
160910
160911 /* if i==0 and iCol==0, then record pRec is smaller than all samples
160912 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
160913 ** be greater than or equal to the (iCol) field prefix of sample i.
160914 ** If (i>0), then pRec must also be greater than sample (i-1). */
160915 if( iCol>0 ){
160916 pRec->nField = iCol;
160917 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
160918 || pParse->db->mallocFailed || CORRUPT_DB );
160919 }
160920 if( i>0 ){
160921 pRec->nField = nField;
160922 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
160923 || pParse->db->mallocFailed || CORRUPT_DB );
160924 }
160925 }
160926 }
160927#endif /* ifdef SQLITE_DEBUG */
160928
160929 if( res==0 ){
160930 /* Record pRec is equal to sample i */
160931 assert( iCol==nField-1 );
160932 aStat[0] = aSample[i].anLt[iCol];
160933 aStat[1] = aSample[i].anEq[iCol];
160934 }else{
160935 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
160936 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
160937 ** is larger than all samples in the array. */
160938 tRowcnt iUpper, iGap;
160939 if( i>=pIdx->nSample ){
160940 iUpper = pIdx->nRowEst0;
160941 }else{
160942 iUpper = aSample[i].anLt[iCol];
160943 }
160944
160945 if( iLower>=iUpper ){
160946 iGap = 0;
160947 }else{
160948 iGap = iUpper - iLower;
160949 }
160950 if( roundUp ){
160951 iGap = (iGap*2)/3;
160952 }else{
160953 iGap = iGap/3;
160954 }
160955 aStat[0] = iLower + iGap;
160956 aStat[1] = pIdx->aAvgEq[nField-1];
160957 }
160958
160959 /* Restore the pRec->nField value before returning. */
160960 pRec->nField = nField;
160961 return i;
160962}
160963#endif /* SQLITE_ENABLE_STAT4 */
160964
160965/*
160966** If it is not NULL, pTerm is a term that provides an upper or lower
160967** bound on a range scan. Without considering pTerm, it is estimated
160968** that the scan will visit nNew rows. This function returns the number
160969** estimated to be visited after taking pTerm into account.
160970**
160971** If the user explicitly specified a likelihood() value for this term,
160972** then the return value is the likelihood multiplied by the number of
160973** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
160974** has a likelihood of 0.50, and any other term a likelihood of 0.25.
160975*/
160976static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
160977 LogEst nRet = nNew;
160978 if( pTerm ){
160979 if( pTerm->truthProb<=0 ){
160980 nRet += pTerm->truthProb;
160981 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
160982 nRet -= 20; assert( 20==sqlite3LogEst(4) );
160983 }
160984 }
160985 return nRet;
160986}
160987
160988
160989#ifdef SQLITE_ENABLE_STAT4
160990/*
160991** Return the affinity for a single column of an index.
160992*/
160993SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
160994 assert( iCol>=0 && iCol<pIdx->nColumn );
160995 if( !pIdx->zColAff ){
160996 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
160997 }
160998 assert( pIdx->zColAff[iCol]!=0 );
160999 return pIdx->zColAff[iCol];
161000}
161001#endif
161002
161003
161004#ifdef SQLITE_ENABLE_STAT4
161005/*
161006** This function is called to estimate the number of rows visited by a
161007** range-scan on a skip-scan index. For example:
161008**
161009** CREATE INDEX i1 ON t1(a, b, c);
161010** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
161011**
161012** Value pLoop->nOut is currently set to the estimated number of rows
161013** visited for scanning (a=? AND b=?). This function reduces that estimate
161014** by some factor to account for the (c BETWEEN ? AND ?) expression based
161015** on the stat4 data for the index. this scan will be performed multiple
161016** times (once for each (a,b) combination that matches a=?) is dealt with
161017** by the caller.
161018**
161019** It does this by scanning through all stat4 samples, comparing values
161020** extracted from pLower and pUpper with the corresponding column in each
161021** sample. If L and U are the number of samples found to be less than or
161022** equal to the values extracted from pLower and pUpper respectively, and
161023** N is the total number of samples, the pLoop->nOut value is adjusted
161024** as follows:
161025**
161026** nOut = nOut * ( min(U - L, 1) / N )
161027**
161028** If pLower is NULL, or a value cannot be extracted from the term, L is
161029** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
161030** U is set to N.
161031**
161032** Normally, this function sets *pbDone to 1 before returning. However,
161033** if no value can be extracted from either pLower or pUpper (and so the
161034** estimate of the number of rows delivered remains unchanged), *pbDone
161035** is left as is.
161036**
161037** If an error occurs, an SQLite error code is returned. Otherwise,
161038** SQLITE_OK.
161039*/
161040static int whereRangeSkipScanEst(
161041 Parse *pParse, /* Parsing & code generating context */
161042 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
161043 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
161044 WhereLoop *pLoop, /* Update the .nOut value of this loop */
161045 int *pbDone /* Set to true if at least one expr. value extracted */
161046){
161047 Index *p = pLoop->u.btree.pIndex;
161048 int nEq = pLoop->u.btree.nEq;
161049 sqlite3 *db = pParse->db;
161050 int nLower = -1;
161051 int nUpper = p->nSample+1;
161052 int rc = SQLITE_OK;
161053 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
161054 CollSeq *pColl;
161055
161056 sqlite3_value *p1 = 0; /* Value extracted from pLower */
161057 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
161058 sqlite3_value *pVal = 0; /* Value extracted from record */
161059
161060 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
161061 if( pLower ){
161062 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
161063 nLower = 0;
161064 }
161065 if( pUpper && rc==SQLITE_OK ){
161066 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
161067 nUpper = p2 ? 0 : p->nSample;
161068 }
161069
161070 if( p1 || p2 ){
161071 int i;
161072 int nDiff;
161073 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
161074 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
161075 if( rc==SQLITE_OK && p1 ){
161076 int res = sqlite3MemCompare(p1, pVal, pColl);
161077 if( res>=0 ) nLower++;
161078 }
161079 if( rc==SQLITE_OK && p2 ){
161080 int res = sqlite3MemCompare(p2, pVal, pColl);
161081 if( res>=0 ) nUpper++;
161082 }
161083 }
161084 nDiff = (nUpper - nLower);
161085 if( nDiff<=0 ) nDiff = 1;
161086
161087 /* If there is both an upper and lower bound specified, and the
161088 ** comparisons indicate that they are close together, use the fallback
161089 ** method (assume that the scan visits 1/64 of the rows) for estimating
161090 ** the number of rows visited. Otherwise, estimate the number of rows
161091 ** using the method described in the header comment for this function. */
161092 if( nDiff!=1 || pUpper==0 || pLower==0 ){
161093 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
161094 pLoop->nOut -= nAdjust;
161095 *pbDone = 1;
161096 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
161097 nLower, nUpper, nAdjust*-1, pLoop->nOut));
161098 }
161099
161100 }else{
161101 assert( *pbDone==0 );
161102 }
161103
161104 sqlite3ValueFree(p1);
161105 sqlite3ValueFree(p2);
161106 sqlite3ValueFree(pVal);
161107
161108 return rc;
161109}
161110#endif /* SQLITE_ENABLE_STAT4 */
161111
161112/*
161113** This function is used to estimate the number of rows that will be visited
161114** by scanning an index for a range of values. The range may have an upper
161115** bound, a lower bound, or both. The WHERE clause terms that set the upper
161116** and lower bounds are represented by pLower and pUpper respectively. For
161117** example, assuming that index p is on t1(a):
161118**
161119** ... FROM t1 WHERE a > ? AND a < ? ...
161120** |_____| |_____|
161121** | |
161122** pLower pUpper
161123**
161124** If either of the upper or lower bound is not present, then NULL is passed in
161125** place of the corresponding WhereTerm.
161126**
161127** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
161128** column subject to the range constraint. Or, equivalently, the number of
161129** equality constraints optimized by the proposed index scan. For example,
161130** assuming index p is on t1(a, b), and the SQL query is:
161131**
161132** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
161133**
161134** then nEq is set to 1 (as the range restricted column, b, is the second
161135** left-most column of the index). Or, if the query is:
161136**
161137** ... FROM t1 WHERE a > ? AND a < ? ...
161138**
161139** then nEq is set to 0.
161140**
161141** When this function is called, *pnOut is set to the sqlite3LogEst() of the
161142** number of rows that the index scan is expected to visit without
161143** considering the range constraints. If nEq is 0, then *pnOut is the number of
161144** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
161145** to account for the range constraints pLower and pUpper.
161146**
161147** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
161148** used, a single range inequality reduces the search space by a factor of 4.
161149** and a pair of constraints (x>? AND x<?) reduces the expected number of
161150** rows visited by a factor of 64.
161151*/
161152static int whereRangeScanEst(
161153 Parse *pParse, /* Parsing & code generating context */
161154 WhereLoopBuilder *pBuilder,
161155 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
161156 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
161157 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
161158){
161159 int rc = SQLITE_OK;
161160 int nOut = pLoop->nOut;
161161 LogEst nNew;
161162
161163#ifdef SQLITE_ENABLE_STAT4
161164 Index *p = pLoop->u.btree.pIndex;
161165 int nEq = pLoop->u.btree.nEq;
161166
161167 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
161168 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
161169 ){
161170 if( nEq==pBuilder->nRecValid ){
161171 UnpackedRecord *pRec = pBuilder->pRec;
161172 tRowcnt a[2];
161173 int nBtm = pLoop->u.btree.nBtm;
161174 int nTop = pLoop->u.btree.nTop;
161175
161176 /* Variable iLower will be set to the estimate of the number of rows in
161177 ** the index that are less than the lower bound of the range query. The
161178 ** lower bound being the concatenation of $P and $L, where $P is the
161179 ** key-prefix formed by the nEq values matched against the nEq left-most
161180 ** columns of the index, and $L is the value in pLower.
161181 **
161182 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
161183 ** is not a simple variable or literal value), the lower bound of the
161184 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
161185 ** if $L is available, whereKeyStats() is called for both ($P) and
161186 ** ($P:$L) and the larger of the two returned values is used.
161187 **
161188 ** Similarly, iUpper is to be set to the estimate of the number of rows
161189 ** less than the upper bound of the range query. Where the upper bound
161190 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
161191 ** of iUpper are requested of whereKeyStats() and the smaller used.
161192 **
161193 ** The number of rows between the two bounds is then just iUpper-iLower.
161194 */
161195 tRowcnt iLower; /* Rows less than the lower bound */
161196 tRowcnt iUpper; /* Rows less than the upper bound */
161197 int iLwrIdx = -2; /* aSample[] for the lower bound */
161198 int iUprIdx = -1; /* aSample[] for the upper bound */
161199
161200 if( pRec ){
161201 testcase( pRec->nField!=pBuilder->nRecValid );
161202 pRec->nField = pBuilder->nRecValid;
161203 }
161204 /* Determine iLower and iUpper using ($P) only. */
161205 if( nEq==0 ){
161206 iLower = 0;
161207 iUpper = p->nRowEst0;
161208 }else{
161209 /* Note: this call could be optimized away - since the same values must
161210 ** have been requested when testing key $P in whereEqualScanEst(). */
161211 whereKeyStats(pParse, p, pRec, 0, a);
161212 iLower = a[0];
161213 iUpper = a[0] + a[1];
161214 }
161215
161216 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
161217 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
161218 assert( p->aSortOrder!=0 );
161219 if( p->aSortOrder[nEq] ){
161220 /* The roles of pLower and pUpper are swapped for a DESC index */
161221 SWAP(WhereTerm*, pLower, pUpper);
161222 SWAP(int, nBtm, nTop);
161223 }
161224
161225 /* If possible, improve on the iLower estimate using ($P:$L). */
161226 if( pLower ){
161227 int n; /* Values extracted from pExpr */
161228 Expr *pExpr = pLower->pExpr->pRight;
161229 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
161230 if( rc==SQLITE_OK && n ){
161231 tRowcnt iNew;
161232 u16 mask = WO_GT|WO_LE;
161233 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
161234 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
161235 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
161236 if( iNew>iLower ) iLower = iNew;
161237 nOut--;
161238 pLower = 0;
161239 }
161240 }
161241
161242 /* If possible, improve on the iUpper estimate using ($P:$U). */
161243 if( pUpper ){
161244 int n; /* Values extracted from pExpr */
161245 Expr *pExpr = pUpper->pExpr->pRight;
161246 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
161247 if( rc==SQLITE_OK && n ){
161248 tRowcnt iNew;
161249 u16 mask = WO_GT|WO_LE;
161250 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
161251 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
161252 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
161253 if( iNew<iUpper ) iUpper = iNew;
161254 nOut--;
161255 pUpper = 0;
161256 }
161257 }
161258
161259 pBuilder->pRec = pRec;
161260 if( rc==SQLITE_OK ){
161261 if( iUpper>iLower ){
161262 nNew = sqlite3LogEst(iUpper - iLower);
161263 /* TUNING: If both iUpper and iLower are derived from the same
161264 ** sample, then assume they are 4x more selective. This brings
161265 ** the estimated selectivity more in line with what it would be
161266 ** if estimated without the use of STAT4 tables. */
161267 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
161268 }else{
161269 nNew = 10; assert( 10==sqlite3LogEst(2) );
161270 }
161271 if( nNew<nOut ){
161272 nOut = nNew;
161273 }
161274 WHERETRACE(0x20, ("STAT4 range scan: %u..%u est=%d\n",
161275 (u32)iLower, (u32)iUpper, nOut));
161276 }
161277 }else{
161278 int bDone = 0;
161279 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
161280 if( bDone ) return rc;
161281 }
161282 }
161283#else
161284 UNUSED_PARAMETER(pParse);
161285 UNUSED_PARAMETER(pBuilder);
161286 assert( pLower || pUpper );
161287#endif
161288 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 );
161289 nNew = whereRangeAdjust(pTerm: pLower, nNew: nOut);
161290 nNew = whereRangeAdjust(pTerm: pUpper, nNew);
161291
161292 /* TUNING: If there is both an upper and lower limit and neither limit
161293 ** has an application-defined likelihood(), assume the range is
161294 ** reduced by an additional 75%. This means that, by default, an open-ended
161295 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
161296 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
161297 ** match 1/64 of the index. */
161298 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
161299 nNew -= 20;
161300 }
161301
161302 nOut -= (pLower!=0) + (pUpper!=0);
161303 if( nNew<10 ) nNew = 10;
161304 if( nNew<nOut ) nOut = nNew;
161305#if defined(WHERETRACE_ENABLED)
161306 if( pLoop->nOut>nOut ){
161307 WHERETRACE(0x20,("Range scan lowers nOut from %d to %d\n",
161308 pLoop->nOut, nOut));
161309 }
161310#endif
161311 pLoop->nOut = (LogEst)nOut;
161312 return rc;
161313}
161314
161315#ifdef SQLITE_ENABLE_STAT4
161316/*
161317** Estimate the number of rows that will be returned based on
161318** an equality constraint x=VALUE and where that VALUE occurs in
161319** the histogram data. This only works when x is the left-most
161320** column of an index and sqlite_stat4 histogram data is available
161321** for that index. When pExpr==NULL that means the constraint is
161322** "x IS NULL" instead of "x=VALUE".
161323**
161324** Write the estimated row count into *pnRow and return SQLITE_OK.
161325** If unable to make an estimate, leave *pnRow unchanged and return
161326** non-zero.
161327**
161328** This routine can fail if it is unable to load a collating sequence
161329** required for string comparison, or if unable to allocate memory
161330** for a UTF conversion required for comparison. The error is stored
161331** in the pParse structure.
161332*/
161333static int whereEqualScanEst(
161334 Parse *pParse, /* Parsing & code generating context */
161335 WhereLoopBuilder *pBuilder,
161336 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
161337 tRowcnt *pnRow /* Write the revised row estimate here */
161338){
161339 Index *p = pBuilder->pNew->u.btree.pIndex;
161340 int nEq = pBuilder->pNew->u.btree.nEq;
161341 UnpackedRecord *pRec = pBuilder->pRec;
161342 int rc; /* Subfunction return code */
161343 tRowcnt a[2]; /* Statistics */
161344 int bOk;
161345
161346 assert( nEq>=1 );
161347 assert( nEq<=p->nColumn );
161348 assert( p->aSample!=0 );
161349 assert( p->nSample>0 );
161350 assert( pBuilder->nRecValid<nEq );
161351
161352 /* If values are not available for all fields of the index to the left
161353 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
161354 if( pBuilder->nRecValid<(nEq-1) ){
161355 return SQLITE_NOTFOUND;
161356 }
161357
161358 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
161359 ** below would return the same value. */
161360 if( nEq>=p->nColumn ){
161361 *pnRow = 1;
161362 return SQLITE_OK;
161363 }
161364
161365 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
161366 pBuilder->pRec = pRec;
161367 if( rc!=SQLITE_OK ) return rc;
161368 if( bOk==0 ) return SQLITE_NOTFOUND;
161369 pBuilder->nRecValid = nEq;
161370
161371 whereKeyStats(pParse, p, pRec, 0, a);
161372 WHERETRACE(0x20,("equality scan regions %s(%d): %d\n",
161373 p->zName, nEq-1, (int)a[1]));
161374 *pnRow = a[1];
161375
161376 return rc;
161377}
161378#endif /* SQLITE_ENABLE_STAT4 */
161379
161380#ifdef SQLITE_ENABLE_STAT4
161381/*
161382** Estimate the number of rows that will be returned based on
161383** an IN constraint where the right-hand side of the IN operator
161384** is a list of values. Example:
161385**
161386** WHERE x IN (1,2,3,4)
161387**
161388** Write the estimated row count into *pnRow and return SQLITE_OK.
161389** If unable to make an estimate, leave *pnRow unchanged and return
161390** non-zero.
161391**
161392** This routine can fail if it is unable to load a collating sequence
161393** required for string comparison, or if unable to allocate memory
161394** for a UTF conversion required for comparison. The error is stored
161395** in the pParse structure.
161396*/
161397static int whereInScanEst(
161398 Parse *pParse, /* Parsing & code generating context */
161399 WhereLoopBuilder *pBuilder,
161400 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
161401 tRowcnt *pnRow /* Write the revised row estimate here */
161402){
161403 Index *p = pBuilder->pNew->u.btree.pIndex;
161404 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
161405 int nRecValid = pBuilder->nRecValid;
161406 int rc = SQLITE_OK; /* Subfunction return code */
161407 tRowcnt nEst; /* Number of rows for a single term */
161408 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
161409 int i; /* Loop counter */
161410
161411 assert( p->aSample!=0 );
161412 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
161413 nEst = nRow0;
161414 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
161415 nRowEst += nEst;
161416 pBuilder->nRecValid = nRecValid;
161417 }
161418
161419 if( rc==SQLITE_OK ){
161420 if( nRowEst > (tRowcnt)nRow0 ) nRowEst = nRow0;
161421 *pnRow = nRowEst;
161422 WHERETRACE(0x20,("IN row estimate: est=%d\n", nRowEst));
161423 }
161424 assert( pBuilder->nRecValid==nRecValid );
161425 return rc;
161426}
161427#endif /* SQLITE_ENABLE_STAT4 */
161428
161429
161430#ifdef WHERETRACE_ENABLED
161431/*
161432** Print the content of a WhereTerm object
161433*/
161434SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
161435 if( pTerm==0 ){
161436 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
161437 }else{
161438 char zType[8];
161439 char zLeft[50];
161440 memcpy(zType, "....", 5);
161441 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
161442 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
161443 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
161444 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
161445 if( pTerm->eOperator & WO_SINGLE ){
161446 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161447 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
161448 pTerm->leftCursor, pTerm->u.x.leftColumn);
161449 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
161450 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%llx",
161451 pTerm->u.pOrInfo->indexable);
161452 }else{
161453 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
161454 }
161455 sqlite3DebugPrintf(
161456 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
161457 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
161458 /* The 0x10000 .wheretrace flag causes extra information to be
161459 ** shown about each Term */
161460 if( sqlite3WhereTrace & 0x10000 ){
161461 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
161462 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
161463 }
161464 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
161465 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
161466 }
161467 if( pTerm->iParent>=0 ){
161468 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
161469 }
161470 sqlite3DebugPrintf("\n");
161471 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
161472 }
161473}
161474#endif
161475
161476#ifdef WHERETRACE_ENABLED
161477/*
161478** Show the complete content of a WhereClause
161479*/
161480SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
161481 int i;
161482 for(i=0; i<pWC->nTerm; i++){
161483 sqlite3WhereTermPrint(&pWC->a[i], i);
161484 }
161485}
161486#endif
161487
161488#ifdef WHERETRACE_ENABLED
161489/*
161490** Print a WhereLoop object for debugging purposes
161491*/
161492SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
161493 WhereInfo *pWInfo = pWC->pWInfo;
161494 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
161495 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
161496 Table *pTab = pItem->pTab;
161497 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
161498 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
161499 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
161500 sqlite3DebugPrintf(" %12s",
161501 pItem->zAlias ? pItem->zAlias : pTab->zName);
161502 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
161503 const char *zName;
161504 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
161505 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
161506 int i = sqlite3Strlen30(zName) - 1;
161507 while( zName[i]!='_' ) i--;
161508 zName += i;
161509 }
161510 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
161511 }else{
161512 sqlite3DebugPrintf("%20s","");
161513 }
161514 }else{
161515 char *z;
161516 if( p->u.vtab.idxStr ){
161517 z = sqlite3_mprintf("(%d,\"%s\",%#x)",
161518 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
161519 }else{
161520 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
161521 }
161522 sqlite3DebugPrintf(" %-19s", z);
161523 sqlite3_free(z);
161524 }
161525 if( p->wsFlags & WHERE_SKIPSCAN ){
161526 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
161527 }else{
161528 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
161529 }
161530 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
161531 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
161532 int i;
161533 for(i=0; i<p->nLTerm; i++){
161534 sqlite3WhereTermPrint(p->aLTerm[i], i);
161535 }
161536 }
161537}
161538#endif
161539
161540/*
161541** Convert bulk memory into a valid WhereLoop that can be passed
161542** to whereLoopClear harmlessly.
161543*/
161544static void whereLoopInit(WhereLoop *p){
161545 p->aLTerm = p->aLTermSpace;
161546 p->nLTerm = 0;
161547 p->nLSlot = ArraySize(p->aLTermSpace);
161548 p->wsFlags = 0;
161549}
161550
161551/*
161552** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
161553*/
161554static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
161555 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
161556 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
161557 sqlite3_free(p: p->u.vtab.idxStr);
161558 p->u.vtab.needFree = 0;
161559 p->u.vtab.idxStr = 0;
161560 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
161561 sqlite3DbFree(db, p: p->u.btree.pIndex->zColAff);
161562 sqlite3DbFreeNN(db, p: p->u.btree.pIndex);
161563 p->u.btree.pIndex = 0;
161564 }
161565 }
161566}
161567
161568/*
161569** Deallocate internal memory used by a WhereLoop object. Leave the
161570** object in an initialized state, as if it had been newly allocated.
161571*/
161572static void whereLoopClear(sqlite3 *db, WhereLoop *p){
161573 if( p->aLTerm!=p->aLTermSpace ){
161574 sqlite3DbFreeNN(db, p: p->aLTerm);
161575 p->aLTerm = p->aLTermSpace;
161576 p->nLSlot = ArraySize(p->aLTermSpace);
161577 }
161578 whereLoopClearUnion(db, p);
161579 p->nLTerm = 0;
161580 p->wsFlags = 0;
161581}
161582
161583/*
161584** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
161585*/
161586static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
161587 WhereTerm **paNew;
161588 if( p->nLSlot>=n ) return SQLITE_OK;
161589 n = (n+7)&~7;
161590 paNew = sqlite3DbMallocRawNN(db, n: sizeof(p->aLTerm[0])*n);
161591 if( paNew==0 ) return SQLITE_NOMEM_BKPT;
161592 memcpy(dest: paNew, src: p->aLTerm, n: sizeof(p->aLTerm[0])*p->nLSlot);
161593 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p: p->aLTerm);
161594 p->aLTerm = paNew;
161595 p->nLSlot = n;
161596 return SQLITE_OK;
161597}
161598
161599/*
161600** Transfer content from the second pLoop into the first.
161601*/
161602static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
161603 whereLoopClearUnion(db, p: pTo);
161604 if( pFrom->nLTerm > pTo->nLSlot
161605 && whereLoopResize(db, p: pTo, n: pFrom->nLTerm)
161606 ){
161607 memset(s: pTo, c: 0, WHERE_LOOP_XFER_SZ);
161608 return SQLITE_NOMEM_BKPT;
161609 }
161610 memcpy(dest: pTo, src: pFrom, WHERE_LOOP_XFER_SZ);
161611 memcpy(dest: pTo->aLTerm, src: pFrom->aLTerm, n: pTo->nLTerm*sizeof(pTo->aLTerm[0]));
161612 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
161613 pFrom->u.vtab.needFree = 0;
161614 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
161615 pFrom->u.btree.pIndex = 0;
161616 }
161617 return SQLITE_OK;
161618}
161619
161620/*
161621** Delete a WhereLoop object
161622*/
161623static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
161624 assert( db!=0 );
161625 whereLoopClear(db, p);
161626 sqlite3DbNNFreeNN(db, p);
161627}
161628
161629/*
161630** Free a WhereInfo structure
161631*/
161632static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
161633 assert( pWInfo!=0 );
161634 assert( db!=0 );
161635 sqlite3WhereClauseClear(pWC: &pWInfo->sWC);
161636 while( pWInfo->pLoops ){
161637 WhereLoop *p = pWInfo->pLoops;
161638 pWInfo->pLoops = p->pNextLoop;
161639 whereLoopDelete(db, p);
161640 }
161641 while( pWInfo->pMemToFree ){
161642 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
161643 sqlite3DbNNFreeNN(db, p: pWInfo->pMemToFree);
161644 pWInfo->pMemToFree = pNext;
161645 }
161646 sqlite3DbNNFreeNN(db, p: pWInfo);
161647}
161648
161649/*
161650** Return TRUE if all of the following are true:
161651**
161652** (1) X has the same or lower cost, or returns the same or fewer rows,
161653** than Y.
161654** (2) X uses fewer WHERE clause terms than Y
161655** (3) Every WHERE clause term used by X is also used by Y
161656** (4) X skips at least as many columns as Y
161657** (5) If X is a covering index, than Y is too
161658**
161659** Conditions (2) and (3) mean that X is a "proper subset" of Y.
161660** If X is a proper subset of Y then Y is a better choice and ought
161661** to have a lower cost. This routine returns TRUE when that cost
161662** relationship is inverted and needs to be adjusted. Constraint (4)
161663** was added because if X uses skip-scan less than Y it still might
161664** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
161665** was added because a covering index probably deserves to have a lower cost
161666** than a non-covering index even if it is a proper subset.
161667*/
161668static int whereLoopCheaperProperSubset(
161669 const WhereLoop *pX, /* First WhereLoop to compare */
161670 const WhereLoop *pY /* Compare against this WhereLoop */
161671){
161672 int i, j;
161673 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
161674 return 0; /* X is not a subset of Y */
161675 }
161676 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
161677 if( pY->nSkip > pX->nSkip ) return 0;
161678 for(i=pX->nLTerm-1; i>=0; i--){
161679 if( pX->aLTerm[i]==0 ) continue;
161680 for(j=pY->nLTerm-1; j>=0; j--){
161681 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
161682 }
161683 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
161684 }
161685 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
161686 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
161687 return 0; /* Constraint (5) */
161688 }
161689 return 1; /* All conditions meet */
161690}
161691
161692/*
161693** Try to adjust the cost and number of output rows of WhereLoop pTemplate
161694** upwards or downwards so that:
161695**
161696** (1) pTemplate costs less than any other WhereLoops that are a proper
161697** subset of pTemplate
161698**
161699** (2) pTemplate costs more than any other WhereLoops for which pTemplate
161700** is a proper subset.
161701**
161702** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
161703** WHERE clause terms than Y and that every WHERE clause term used by X is
161704** also used by Y.
161705*/
161706static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
161707 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
161708 for(; p; p=p->pNextLoop){
161709 if( p->iTab!=pTemplate->iTab ) continue;
161710 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
161711 if( whereLoopCheaperProperSubset(pX: p, pY: pTemplate) ){
161712 /* Adjust pTemplate cost downward so that it is cheaper than its
161713 ** subset p. */
161714 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
161715 pTemplate->rRun, pTemplate->nOut,
161716 MIN(p->rRun, pTemplate->rRun),
161717 MIN(p->nOut - 1, pTemplate->nOut)));
161718 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
161719 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
161720 }else if( whereLoopCheaperProperSubset(pX: pTemplate, pY: p) ){
161721 /* Adjust pTemplate cost upward so that it is costlier than p since
161722 ** pTemplate is a proper subset of p */
161723 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
161724 pTemplate->rRun, pTemplate->nOut,
161725 MAX(p->rRun, pTemplate->rRun),
161726 MAX(p->nOut + 1, pTemplate->nOut)));
161727 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
161728 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
161729 }
161730 }
161731}
161732
161733/*
161734** Search the list of WhereLoops in *ppPrev looking for one that can be
161735** replaced by pTemplate.
161736**
161737** Return NULL if pTemplate does not belong on the WhereLoop list.
161738** In other words if pTemplate ought to be dropped from further consideration.
161739**
161740** If pX is a WhereLoop that pTemplate can replace, then return the
161741** link that points to pX.
161742**
161743** If pTemplate cannot replace any existing element of the list but needs
161744** to be added to the list as a new entry, then return a pointer to the
161745** tail of the list.
161746*/
161747static WhereLoop **whereLoopFindLesser(
161748 WhereLoop **ppPrev,
161749 const WhereLoop *pTemplate
161750){
161751 WhereLoop *p;
161752 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
161753 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
161754 /* If either the iTab or iSortIdx values for two WhereLoop are different
161755 ** then those WhereLoops need to be considered separately. Neither is
161756 ** a candidate to replace the other. */
161757 continue;
161758 }
161759 /* In the current implementation, the rSetup value is either zero
161760 ** or the cost of building an automatic index (NlogN) and the NlogN
161761 ** is the same for compatible WhereLoops. */
161762 assert( p->rSetup==0 || pTemplate->rSetup==0
161763 || p->rSetup==pTemplate->rSetup );
161764
161765 /* whereLoopAddBtree() always generates and inserts the automatic index
161766 ** case first. Hence compatible candidate WhereLoops never have a larger
161767 ** rSetup. Call this SETUP-INVARIANT */
161768 assert( p->rSetup>=pTemplate->rSetup );
161769
161770 /* Any loop using an application-defined index (or PRIMARY KEY or
161771 ** UNIQUE constraint) with one or more == constraints is better
161772 ** than an automatic index. Unless it is a skip-scan. */
161773 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
161774 && (pTemplate->nSkip)==0
161775 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
161776 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
161777 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
161778 ){
161779 break;
161780 }
161781
161782 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
161783 ** discarded. WhereLoop p is better if:
161784 ** (1) p has no more dependencies than pTemplate, and
161785 ** (2) p has an equal or lower cost than pTemplate
161786 */
161787 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
161788 && p->rSetup<=pTemplate->rSetup /* (2a) */
161789 && p->rRun<=pTemplate->rRun /* (2b) */
161790 && p->nOut<=pTemplate->nOut /* (2c) */
161791 ){
161792 return 0; /* Discard pTemplate */
161793 }
161794
161795 /* If pTemplate is always better than p, then cause p to be overwritten
161796 ** with pTemplate. pTemplate is better than p if:
161797 ** (1) pTemplate has no more dependencies than p, and
161798 ** (2) pTemplate has an equal or lower cost than p.
161799 */
161800 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
161801 && p->rRun>=pTemplate->rRun /* (2a) */
161802 && p->nOut>=pTemplate->nOut /* (2b) */
161803 ){
161804 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
161805 break; /* Cause p to be overwritten by pTemplate */
161806 }
161807 }
161808 return ppPrev;
161809}
161810
161811/*
161812** Insert or replace a WhereLoop entry using the template supplied.
161813**
161814** An existing WhereLoop entry might be overwritten if the new template
161815** is better and has fewer dependencies. Or the template will be ignored
161816** and no insert will occur if an existing WhereLoop is faster and has
161817** fewer dependencies than the template. Otherwise a new WhereLoop is
161818** added based on the template.
161819**
161820** If pBuilder->pOrSet is not NULL then we care about only the
161821** prerequisites and rRun and nOut costs of the N best loops. That
161822** information is gathered in the pBuilder->pOrSet object. This special
161823** processing mode is used only for OR clause processing.
161824**
161825** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
161826** still might overwrite similar loops with the new template if the
161827** new template is better. Loops may be overwritten if the following
161828** conditions are met:
161829**
161830** (1) They have the same iTab.
161831** (2) They have the same iSortIdx.
161832** (3) The template has same or fewer dependencies than the current loop
161833** (4) The template has the same or lower cost than the current loop
161834*/
161835static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
161836 WhereLoop **ppPrev, *p;
161837 WhereInfo *pWInfo = pBuilder->pWInfo;
161838 sqlite3 *db = pWInfo->pParse->db;
161839 int rc;
161840
161841 /* Stop the search once we hit the query planner search limit */
161842 if( pBuilder->iPlanLimit==0 ){
161843 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
161844 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
161845 return SQLITE_DONE;
161846 }
161847 pBuilder->iPlanLimit--;
161848
161849 whereLoopAdjustCost(p: pWInfo->pLoops, pTemplate);
161850
161851 /* If pBuilder->pOrSet is defined, then only keep track of the costs
161852 ** and prereqs.
161853 */
161854 if( pBuilder->pOrSet!=0 ){
161855 if( pTemplate->nLTerm ){
161856#if WHERETRACE_ENABLED
161857 u16 n = pBuilder->pOrSet->n;
161858 int x =
161859#endif
161860 whereOrInsert(pSet: pBuilder->pOrSet, prereq: pTemplate->prereq, rRun: pTemplate->rRun,
161861 nOut: pTemplate->nOut);
161862#if WHERETRACE_ENABLED /* 0x8 */
161863 if( sqlite3WhereTrace & 0x8 ){
161864 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
161865 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
161866 }
161867#endif
161868 }
161869 return SQLITE_OK;
161870 }
161871
161872 /* Look for an existing WhereLoop to replace with pTemplate
161873 */
161874 ppPrev = whereLoopFindLesser(ppPrev: &pWInfo->pLoops, pTemplate);
161875
161876 if( ppPrev==0 ){
161877 /* There already exists a WhereLoop on the list that is better
161878 ** than pTemplate, so just ignore pTemplate */
161879#if WHERETRACE_ENABLED /* 0x8 */
161880 if( sqlite3WhereTrace & 0x8 ){
161881 sqlite3DebugPrintf(" skip: ");
161882 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
161883 }
161884#endif
161885 return SQLITE_OK;
161886 }else{
161887 p = *ppPrev;
161888 }
161889
161890 /* If we reach this point it means that either p[] should be overwritten
161891 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
161892 ** WhereLoop and insert it.
161893 */
161894#if WHERETRACE_ENABLED /* 0x8 */
161895 if( sqlite3WhereTrace & 0x8 ){
161896 if( p!=0 ){
161897 sqlite3DebugPrintf("replace: ");
161898 sqlite3WhereLoopPrint(p, pBuilder->pWC);
161899 sqlite3DebugPrintf(" with: ");
161900 }else{
161901 sqlite3DebugPrintf(" add: ");
161902 }
161903 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
161904 }
161905#endif
161906 if( p==0 ){
161907 /* Allocate a new WhereLoop to add to the end of the list */
161908 *ppPrev = p = sqlite3DbMallocRawNN(db, n: sizeof(WhereLoop));
161909 if( p==0 ) return SQLITE_NOMEM_BKPT;
161910 whereLoopInit(p);
161911 p->pNextLoop = 0;
161912 }else{
161913 /* We will be overwriting WhereLoop p[]. But before we do, first
161914 ** go through the rest of the list and delete any other entries besides
161915 ** p[] that are also supplanted by pTemplate */
161916 WhereLoop **ppTail = &p->pNextLoop;
161917 WhereLoop *pToDel;
161918 while( *ppTail ){
161919 ppTail = whereLoopFindLesser(ppPrev: ppTail, pTemplate);
161920 if( ppTail==0 ) break;
161921 pToDel = *ppTail;
161922 if( pToDel==0 ) break;
161923 *ppTail = pToDel->pNextLoop;
161924#if WHERETRACE_ENABLED /* 0x8 */
161925 if( sqlite3WhereTrace & 0x8 ){
161926 sqlite3DebugPrintf(" delete: ");
161927 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
161928 }
161929#endif
161930 whereLoopDelete(db, p: pToDel);
161931 }
161932 }
161933 rc = whereLoopXfer(db, pTo: p, pFrom: pTemplate);
161934 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
161935 Index *pIndex = p->u.btree.pIndex;
161936 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
161937 p->u.btree.pIndex = 0;
161938 }
161939 }
161940 return rc;
161941}
161942
161943/*
161944** Adjust the WhereLoop.nOut value downward to account for terms of the
161945** WHERE clause that reference the loop but which are not used by an
161946** index.
161947*
161948** For every WHERE clause term that is not used by the index
161949** and which has a truth probability assigned by one of the likelihood(),
161950** likely(), or unlikely() SQL functions, reduce the estimated number
161951** of output rows by the probability specified.
161952**
161953** TUNING: For every WHERE clause term that is not used by the index
161954** and which does not have an assigned truth probability, heuristics
161955** described below are used to try to estimate the truth probability.
161956** TODO --> Perhaps this is something that could be improved by better
161957** table statistics.
161958**
161959** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
161960** value corresponds to -1 in LogEst notation, so this means decrement
161961** the WhereLoop.nOut field for every such WHERE clause term.
161962**
161963** Heuristic 2: If there exists one or more WHERE clause terms of the
161964** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
161965** final output row estimate is no greater than 1/4 of the total number
161966** of rows in the table. In other words, assume that x==EXPR will filter
161967** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
161968** "x" column is boolean or else -1 or 0 or 1 is a common default value
161969** on the "x" column and so in that case only cap the output row estimate
161970** at 1/2 instead of 1/4.
161971*/
161972static void whereLoopOutputAdjust(
161973 WhereClause *pWC, /* The WHERE clause */
161974 WhereLoop *pLoop, /* The loop to adjust downward */
161975 LogEst nRow /* Number of rows in the entire table */
161976){
161977 WhereTerm *pTerm, *pX;
161978 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
161979 int i, j;
161980 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
161981
161982 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
161983 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
161984 assert( pTerm!=0 );
161985 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
161986 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
161987 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
161988 for(j=pLoop->nLTerm-1; j>=0; j--){
161989 pX = pLoop->aLTerm[j];
161990 if( pX==0 ) continue;
161991 if( pX==pTerm ) break;
161992 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
161993 }
161994 if( j<0 ){
161995 sqlite3ProgressCheck(p: pWC->pWInfo->pParse);
161996 if( pLoop->maskSelf==pTerm->prereqAll ){
161997 /* If there are extra terms in the WHERE clause not used by an index
161998 ** that depend only on the table being scanned, and that will tend to
161999 ** cause many rows to be omitted, then mark that table as
162000 ** "self-culling".
162001 **
162002 ** 2022-03-24: Self-culling only applies if either the extra terms
162003 ** are straight comparison operators that are non-true with NULL
162004 ** operand, or if the loop is not an OUTER JOIN.
162005 */
162006 if( (pTerm->eOperator & 0x3f)!=0
162007 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
162008 & (JT_LEFT|JT_LTORJ))==0
162009 ){
162010 pLoop->wsFlags |= WHERE_SELFCULL;
162011 }
162012 }
162013 if( pTerm->truthProb<=0 ){
162014 /* If a truth probability is specified using the likelihood() hints,
162015 ** then use the probability provided by the application. */
162016 pLoop->nOut += pTerm->truthProb;
162017 }else{
162018 /* In the absence of explicit truth probabilities, use heuristics to
162019 ** guess a reasonable truth probability. */
162020 pLoop->nOut--;
162021 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
162022 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
162023 ){
162024 Expr *pRight = pTerm->pExpr->pRight;
162025 int k = 0;
162026 testcase( pTerm->pExpr->op==TK_IS );
162027 if( sqlite3ExprIsInteger(p: pRight, pValue: &k) && k>=(-1) && k<=1 ){
162028 k = 10;
162029 }else{
162030 k = 20;
162031 }
162032 if( iReduce<k ){
162033 pTerm->wtFlags |= TERM_HEURTRUTH;
162034 iReduce = k;
162035 }
162036 }
162037 }
162038 }
162039 }
162040 if( pLoop->nOut > nRow-iReduce ){
162041 pLoop->nOut = nRow - iReduce;
162042 }
162043}
162044
162045/*
162046** Term pTerm is a vector range comparison operation. The first comparison
162047** in the vector can be optimized using column nEq of the index. This
162048** function returns the total number of vector elements that can be used
162049** as part of the range comparison.
162050**
162051** For example, if the query is:
162052**
162053** WHERE a = ? AND (b, c, d) > (?, ?, ?)
162054**
162055** and the index:
162056**
162057** CREATE INDEX ... ON (a, b, c, d, e)
162058**
162059** then this function would be invoked with nEq=1. The value returned in
162060** this case is 3.
162061*/
162062static int whereRangeVectorLen(
162063 Parse *pParse, /* Parsing context */
162064 int iCur, /* Cursor open on pIdx */
162065 Index *pIdx, /* The index to be used for a inequality constraint */
162066 int nEq, /* Number of prior equality constraints on same index */
162067 WhereTerm *pTerm /* The vector inequality constraint */
162068){
162069 int nCmp = sqlite3ExprVectorSize(pExpr: pTerm->pExpr->pLeft);
162070 int i;
162071
162072 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
162073 for(i=1; i<nCmp; i++){
162074 /* Test if comparison i of pTerm is compatible with column (i+nEq)
162075 ** of the index. If not, exit the loop. */
162076 char aff; /* Comparison affinity */
162077 char idxaff = 0; /* Indexed columns affinity */
162078 CollSeq *pColl; /* Comparison collation sequence */
162079 Expr *pLhs, *pRhs;
162080
162081 assert( ExprUseXList(pTerm->pExpr->pLeft) );
162082 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
162083 pRhs = pTerm->pExpr->pRight;
162084 if( ExprUseXSelect(pRhs) ){
162085 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
162086 }else{
162087 pRhs = pRhs->x.pList->a[i].pExpr;
162088 }
162089
162090 /* Check that the LHS of the comparison is a column reference to
162091 ** the right column of the right source table. And that the sort
162092 ** order of the index column is the same as the sort order of the
162093 ** leftmost index column. */
162094 if( pLhs->op!=TK_COLUMN
162095 || pLhs->iTable!=iCur
162096 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
162097 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
162098 ){
162099 break;
162100 }
162101
162102 testcase( pLhs->iColumn==XN_ROWID );
162103 aff = sqlite3CompareAffinity(pExpr: pRhs, aff2: sqlite3ExprAffinity(pExpr: pLhs));
162104 idxaff = sqlite3TableColumnAffinity(pTab: pIdx->pTable, iCol: pLhs->iColumn);
162105 if( aff!=idxaff ) break;
162106
162107 pColl = sqlite3BinaryCompareCollSeq(pParse, pLeft: pLhs, pRight: pRhs);
162108 if( pColl==0 ) break;
162109 if( sqlite3StrICmp(zLeft: pColl->zName, zRight: pIdx->azColl[i+nEq]) ) break;
162110 }
162111 return i;
162112}
162113
162114/*
162115** Adjust the cost C by the costMult factor T. This only occurs if
162116** compiled with -DSQLITE_ENABLE_COSTMULT
162117*/
162118#ifdef SQLITE_ENABLE_COSTMULT
162119# define ApplyCostMultiplier(C,T) C += T
162120#else
162121# define ApplyCostMultiplier(C,T)
162122#endif
162123
162124/*
162125** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
162126** index pIndex. Try to match one more.
162127**
162128** When this function is called, pBuilder->pNew->nOut contains the
162129** number of rows expected to be visited by filtering using the nEq
162130** terms only. If it is modified, this value is restored before this
162131** function returns.
162132**
162133** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
162134** a fake index used for the INTEGER PRIMARY KEY.
162135*/
162136static int whereLoopAddBtreeIndex(
162137 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
162138 SrcItem *pSrc, /* FROM clause term being analyzed */
162139 Index *pProbe, /* An index on pSrc */
162140 LogEst nInMul /* log(Number of iterations due to IN) */
162141){
162142 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */
162143 Parse *pParse = pWInfo->pParse; /* Parsing context */
162144 sqlite3 *db = pParse->db; /* Database connection malloc context */
162145 WhereLoop *pNew; /* Template WhereLoop under construction */
162146 WhereTerm *pTerm; /* A WhereTerm under consideration */
162147 int opMask; /* Valid operators for constraints */
162148 WhereScan scan; /* Iterator for WHERE terms */
162149 Bitmask saved_prereq; /* Original value of pNew->prereq */
162150 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
162151 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
162152 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
162153 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
162154 u16 saved_nSkip; /* Original value of pNew->nSkip */
162155 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
162156 LogEst saved_nOut; /* Original value of pNew->nOut */
162157 int rc = SQLITE_OK; /* Return code */
162158 LogEst rSize; /* Number of rows in the table */
162159 LogEst rLogSize; /* Logarithm of table size */
162160 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
162161
162162 pNew = pBuilder->pNew;
162163 assert( db->mallocFailed==0 || pParse->nErr>0 );
162164 if( pParse->nErr ){
162165 return pParse->rc;
162166 }
162167 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d, rRun=%d\n",
162168 pProbe->pTable->zName,pProbe->zName,
162169 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
162170
162171 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
162172 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
162173 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
162174 opMask = WO_LT|WO_LE;
162175 }else{
162176 assert( pNew->u.btree.nBtm==0 );
162177 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
162178 }
162179 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
162180
162181 assert( pNew->u.btree.nEq<pProbe->nColumn );
162182 assert( pNew->u.btree.nEq<pProbe->nKeyCol
162183 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
162184
162185 saved_nEq = pNew->u.btree.nEq;
162186 saved_nBtm = pNew->u.btree.nBtm;
162187 saved_nTop = pNew->u.btree.nTop;
162188 saved_nSkip = pNew->nSkip;
162189 saved_nLTerm = pNew->nLTerm;
162190 saved_wsFlags = pNew->wsFlags;
162191 saved_prereq = pNew->prereq;
162192 saved_nOut = pNew->nOut;
162193 pTerm = whereScanInit(pScan: &scan, pWC: pBuilder->pWC, iCur: pSrc->iCursor, iColumn: saved_nEq,
162194 opMask, pIdx: pProbe);
162195 pNew->rSetup = 0;
162196 rSize = pProbe->aiRowLogEst[0];
162197 rLogSize = estLog(N: rSize);
162198 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(pScan: &scan)){
162199 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
162200 LogEst rCostIdx;
162201 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
162202 int nIn = 0;
162203#ifdef SQLITE_ENABLE_STAT4
162204 int nRecValid = pBuilder->nRecValid;
162205#endif
162206 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
162207 && indexColumnNotNull(pIdx: pProbe, iCol: saved_nEq)
162208 ){
162209 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
162210 }
162211 if( pTerm->prereqRight & pNew->maskSelf ) continue;
162212
162213 /* Do not allow the upper bound of a LIKE optimization range constraint
162214 ** to mix with a lower range bound from some other source */
162215 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
162216
162217 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
162218 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
162219 ){
162220 continue;
162221 }
162222 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
162223 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
162224 }else{
162225 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
162226 }
162227 pNew->wsFlags = saved_wsFlags;
162228 pNew->u.btree.nEq = saved_nEq;
162229 pNew->u.btree.nBtm = saved_nBtm;
162230 pNew->u.btree.nTop = saved_nTop;
162231 pNew->nLTerm = saved_nLTerm;
162232 if( pNew->nLTerm>=pNew->nLSlot
162233 && whereLoopResize(db, p: pNew, n: pNew->nLTerm+1)
162234 ){
162235 break; /* OOM while trying to enlarge the pNew->aLTerm array */
162236 }
162237 pNew->aLTerm[pNew->nLTerm++] = pTerm;
162238 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
162239
162240 assert( nInMul==0
162241 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
162242 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
162243 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
162244 );
162245
162246 if( eOp & WO_IN ){
162247 Expr *pExpr = pTerm->pExpr;
162248 if( ExprUseXSelect(pExpr) ){
162249 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
162250 int i;
162251 nIn = 46; assert( 46==sqlite3LogEst(25) );
162252
162253 /* The expression may actually be of the form (x, y) IN (SELECT...).
162254 ** In this case there is a separate term for each of (x) and (y).
162255 ** However, the nIn multiplier should only be applied once, not once
162256 ** for each such term. The following loop checks that pTerm is the
162257 ** first such term in use, and sets nIn back to 0 if it is not. */
162258 for(i=0; i<pNew->nLTerm-1; i++){
162259 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
162260 }
162261 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
162262 /* "x IN (value, value, ...)" */
162263 nIn = sqlite3LogEst(x: pExpr->x.pList->nExpr);
162264 }
162265 if( pProbe->hasStat1 && rLogSize>=10 ){
162266 LogEst M, logK, x;
162267 /* Let:
162268 ** N = the total number of rows in the table
162269 ** K = the number of entries on the RHS of the IN operator
162270 ** M = the number of rows in the table that match terms to the
162271 ** to the left in the same index. If the IN operator is on
162272 ** the left-most index column, M==N.
162273 **
162274 ** Given the definitions above, it is better to omit the IN operator
162275 ** from the index lookup and instead do a scan of the M elements,
162276 ** testing each scanned row against the IN operator separately, if:
162277 **
162278 ** M*log(K) < K*log(N)
162279 **
162280 ** Our estimates for M, K, and N might be inaccurate, so we build in
162281 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
162282 ** with the index, as using an index has better worst-case behavior.
162283 ** If we do not have real sqlite_stat1 data, always prefer to use
162284 ** the index. Do not bother with this optimization on very small
162285 ** tables (less than 2 rows) as it is pointless in that case.
162286 */
162287 M = pProbe->aiRowLogEst[saved_nEq];
162288 logK = estLog(N: nIn);
162289 /* TUNING v----- 10 to bias toward indexed IN */
162290 x = M + logK + 10 - (nIn + rLogSize);
162291 if( x>=0 ){
162292 WHERETRACE(0x40,
162293 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d) "
162294 "prefers indexed lookup\n",
162295 saved_nEq, M, logK, nIn, rLogSize, x));
162296 }else if( nInMul<2 && OptimizationEnabled(db, SQLITE_SeekScan) ){
162297 WHERETRACE(0x40,
162298 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
162299 " nInMul=%d) prefers skip-scan\n",
162300 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
162301 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
162302 }else{
162303 WHERETRACE(0x40,
162304 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
162305 " nInMul=%d) prefers normal scan\n",
162306 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
162307 continue;
162308 }
162309 }
162310 pNew->wsFlags |= WHERE_COLUMN_IN;
162311 }else if( eOp & (WO_EQ|WO_IS) ){
162312 int iCol = pProbe->aiColumn[saved_nEq];
162313 pNew->wsFlags |= WHERE_COLUMN_EQ;
162314 assert( saved_nEq==pNew->u.btree.nEq );
162315 if( iCol==XN_ROWID
162316 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
162317 ){
162318 if( iCol==XN_ROWID || pProbe->uniqNotNull
162319 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
162320 ){
162321 pNew->wsFlags |= WHERE_ONEROW;
162322 }else{
162323 pNew->wsFlags |= WHERE_UNQ_WANTED;
162324 }
162325 }
162326 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
162327 }else if( eOp & WO_ISNULL ){
162328 pNew->wsFlags |= WHERE_COLUMN_NULL;
162329 }else{
162330 int nVecLen = whereRangeVectorLen(
162331 pParse, iCur: pSrc->iCursor, pIdx: pProbe, nEq: saved_nEq, pTerm
162332 );
162333 if( eOp & (WO_GT|WO_GE) ){
162334 testcase( eOp & WO_GT );
162335 testcase( eOp & WO_GE );
162336 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
162337 pNew->u.btree.nBtm = nVecLen;
162338 pBtm = pTerm;
162339 pTop = 0;
162340 if( pTerm->wtFlags & TERM_LIKEOPT ){
162341 /* Range constraints that come from the LIKE optimization are
162342 ** always used in pairs. */
162343 pTop = &pTerm[1];
162344 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
162345 assert( pTop->wtFlags & TERM_LIKEOPT );
162346 assert( pTop->eOperator==WO_LT );
162347 if( whereLoopResize(db, p: pNew, n: pNew->nLTerm+1) ) break; /* OOM */
162348 pNew->aLTerm[pNew->nLTerm++] = pTop;
162349 pNew->wsFlags |= WHERE_TOP_LIMIT;
162350 pNew->u.btree.nTop = 1;
162351 }
162352 }else{
162353 assert( eOp & (WO_LT|WO_LE) );
162354 testcase( eOp & WO_LT );
162355 testcase( eOp & WO_LE );
162356 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
162357 pNew->u.btree.nTop = nVecLen;
162358 pTop = pTerm;
162359 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
162360 pNew->aLTerm[pNew->nLTerm-2] : 0;
162361 }
162362 }
162363
162364 /* At this point pNew->nOut is set to the number of rows expected to
162365 ** be visited by the index scan before considering term pTerm, or the
162366 ** values of nIn and nInMul. In other words, assuming that all
162367 ** "x IN(...)" terms are replaced with "x = ?". This block updates
162368 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
162369 assert( pNew->nOut==saved_nOut );
162370 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
162371 /* Adjust nOut using stat4 data. Or, if there is no stat4
162372 ** data, using some other estimate. */
162373 whereRangeScanEst(pParse, pBuilder, pLower: pBtm, pUpper: pTop, pLoop: pNew);
162374 }else{
162375 int nEq = ++pNew->u.btree.nEq;
162376 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
162377
162378 assert( pNew->nOut==saved_nOut );
162379 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
162380 assert( (eOp & WO_IN) || nIn==0 );
162381 testcase( eOp & WO_IN );
162382 pNew->nOut += pTerm->truthProb;
162383 pNew->nOut -= nIn;
162384 }else{
162385#ifdef SQLITE_ENABLE_STAT4
162386 tRowcnt nOut = 0;
162387 if( nInMul==0
162388 && pProbe->nSample
162389 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
162390 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
162391 && OptimizationEnabled(db, SQLITE_Stat4)
162392 ){
162393 Expr *pExpr = pTerm->pExpr;
162394 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
162395 testcase( eOp & WO_EQ );
162396 testcase( eOp & WO_IS );
162397 testcase( eOp & WO_ISNULL );
162398 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
162399 }else{
162400 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
162401 }
162402 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
162403 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
162404 if( nOut ){
162405 pNew->nOut = sqlite3LogEst(nOut);
162406 if( nEq==1
162407 /* TUNING: Mark terms as "low selectivity" if they seem likely
162408 ** to be true for half or more of the rows in the table.
162409 ** See tag-202002240-1 */
162410 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
162411 ){
162412#if WHERETRACE_ENABLED /* 0x01 */
162413 if( sqlite3WhereTrace & 0x20 ){
162414 sqlite3DebugPrintf(
162415 "STAT4 determines term has low selectivity:\n");
162416 sqlite3WhereTermPrint(pTerm, 999);
162417 }
162418#endif
162419 pTerm->wtFlags |= TERM_HIGHTRUTH;
162420 if( pTerm->wtFlags & TERM_HEURTRUTH ){
162421 /* If the term has previously been used with an assumption of
162422 ** higher selectivity, then set the flag to rerun the
162423 ** loop computations. */
162424 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
162425 }
162426 }
162427 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
162428 pNew->nOut -= nIn;
162429 }
162430 }
162431 if( nOut==0 )
162432#endif
162433 {
162434 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
162435 if( eOp & WO_ISNULL ){
162436 /* TUNING: If there is no likelihood() value, assume that a
162437 ** "col IS NULL" expression matches twice as many rows
162438 ** as (col=?). */
162439 pNew->nOut += 10;
162440 }
162441 }
162442 }
162443 }
162444
162445 /* Set rCostIdx to the cost of visiting selected rows in index. Add
162446 ** it to pNew->rRun, which is currently set to the cost of the index
162447 ** seek only. Then, if this is a non-covering index, add the cost of
162448 ** visiting the rows in the main table. */
162449 assert( pSrc->pTab->szTabRow>0 );
162450 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
162451 /* The pProbe->szIdxRow is low for an IPK table since the interior
162452 ** pages are small. Thus szIdxRow gives a good estimate of seek cost.
162453 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
162454 ** under-estimate the scanning cost. */
162455 rCostIdx = pNew->nOut + 16;
162456 }else{
162457 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
162458 }
162459 pNew->rRun = sqlite3LogEstAdd(a: rLogSize, b: rCostIdx);
162460 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
162461 pNew->rRun = sqlite3LogEstAdd(a: pNew->rRun, b: pNew->nOut + 16);
162462 }
162463 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
162464
162465 nOutUnadjusted = pNew->nOut;
162466 pNew->rRun += nInMul + nIn;
162467 pNew->nOut += nInMul + nIn;
162468 whereLoopOutputAdjust(pWC: pBuilder->pWC, pLoop: pNew, nRow: rSize);
162469 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
162470
162471 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
162472 pNew->nOut = saved_nOut;
162473 }else{
162474 pNew->nOut = nOutUnadjusted;
162475 }
162476
162477 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
162478 && pNew->u.btree.nEq<pProbe->nColumn
162479 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
162480 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
162481 ){
162482 if( pNew->u.btree.nEq>3 ){
162483 sqlite3ProgressCheck(p: pParse);
162484 }
162485 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul: nInMul+nIn);
162486 }
162487 pNew->nOut = saved_nOut;
162488#ifdef SQLITE_ENABLE_STAT4
162489 pBuilder->nRecValid = nRecValid;
162490#endif
162491 }
162492 pNew->prereq = saved_prereq;
162493 pNew->u.btree.nEq = saved_nEq;
162494 pNew->u.btree.nBtm = saved_nBtm;
162495 pNew->u.btree.nTop = saved_nTop;
162496 pNew->nSkip = saved_nSkip;
162497 pNew->wsFlags = saved_wsFlags;
162498 pNew->nOut = saved_nOut;
162499 pNew->nLTerm = saved_nLTerm;
162500
162501 /* Consider using a skip-scan if there are no WHERE clause constraints
162502 ** available for the left-most terms of the index, and if the average
162503 ** number of repeats in the left-most terms is at least 18.
162504 **
162505 ** The magic number 18 is selected on the basis that scanning 17 rows
162506 ** is almost always quicker than an index seek (even though if the index
162507 ** contains fewer than 2^17 rows we assume otherwise in other parts of
162508 ** the code). And, even if it is not, it should not be too much slower.
162509 ** On the other hand, the extra seeks could end up being significantly
162510 ** more expensive. */
162511 assert( 42==sqlite3LogEst(18) );
162512 if( saved_nEq==saved_nSkip
162513 && saved_nEq+1<pProbe->nKeyCol
162514 && saved_nEq==pNew->nLTerm
162515 && pProbe->noSkipScan==0
162516 && pProbe->hasStat1!=0
162517 && OptimizationEnabled(db, SQLITE_SkipScan)
162518 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
162519 && (rc = whereLoopResize(db, p: pNew, n: pNew->nLTerm+1))==SQLITE_OK
162520 ){
162521 LogEst nIter;
162522 pNew->u.btree.nEq++;
162523 pNew->nSkip++;
162524 pNew->aLTerm[pNew->nLTerm++] = 0;
162525 pNew->wsFlags |= WHERE_SKIPSCAN;
162526 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
162527 pNew->nOut -= nIter;
162528 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
162529 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
162530 nIter += 5;
162531 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul: nIter + nInMul);
162532 pNew->nOut = saved_nOut;
162533 pNew->u.btree.nEq = saved_nEq;
162534 pNew->nSkip = saved_nSkip;
162535 pNew->wsFlags = saved_wsFlags;
162536 }
162537
162538 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
162539 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
162540 return rc;
162541}
162542
162543/*
162544** Return True if it is possible that pIndex might be useful in
162545** implementing the ORDER BY clause in pBuilder.
162546**
162547** Return False if pBuilder does not contain an ORDER BY clause or
162548** if there is no way for pIndex to be useful in implementing that
162549** ORDER BY clause.
162550*/
162551static int indexMightHelpWithOrderBy(
162552 WhereLoopBuilder *pBuilder,
162553 Index *pIndex,
162554 int iCursor
162555){
162556 ExprList *pOB;
162557 ExprList *aColExpr;
162558 int ii, jj;
162559
162560 if( pIndex->bUnordered ) return 0;
162561 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
162562 for(ii=0; ii<pOB->nExpr; ii++){
162563 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pOB->a[ii].pExpr);
162564 if( NEVER(pExpr==0) ) continue;
162565 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
162566 if( pExpr->iColumn<0 ) return 1;
162567 for(jj=0; jj<pIndex->nKeyCol; jj++){
162568 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
162569 }
162570 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
162571 for(jj=0; jj<pIndex->nKeyCol; jj++){
162572 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
162573 if( sqlite3ExprCompareSkip(pA: pExpr,pB: aColExpr->a[jj].pExpr,iTab: iCursor)==0 ){
162574 return 1;
162575 }
162576 }
162577 }
162578 }
162579 return 0;
162580}
162581
162582/* Check to see if a partial index with pPartIndexWhere can be used
162583** in the current query. Return true if it can be and false if not.
162584*/
162585static int whereUsablePartialIndex(
162586 int iTab, /* The table for which we want an index */
162587 u8 jointype, /* The JT_* flags on the join */
162588 WhereClause *pWC, /* The WHERE clause of the query */
162589 Expr *pWhere /* The WHERE clause from the partial index */
162590){
162591 int i;
162592 WhereTerm *pTerm;
162593 Parse *pParse;
162594
162595 if( jointype & JT_LTORJ ) return 0;
162596 pParse = pWC->pWInfo->pParse;
162597 while( pWhere->op==TK_AND ){
162598 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere: pWhere->pLeft) ) return 0;
162599 pWhere = pWhere->pRight;
162600 }
162601 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
162602 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
162603 Expr *pExpr;
162604 pExpr = pTerm->pExpr;
162605 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
162606 && ((jointype & JT_OUTER)==0 || ExprHasProperty(pExpr, EP_OuterON))
162607 && sqlite3ExprImpliesExpr(pParse, pE1: pExpr, pE2: pWhere, iTab)
162608 && (pTerm->wtFlags & TERM_VNULL)==0
162609 ){
162610 return 1;
162611 }
162612 }
162613 return 0;
162614}
162615
162616/*
162617** pIdx is an index containing expressions. Check it see if any of the
162618** expressions in the index match the pExpr expression.
162619*/
162620static int exprIsCoveredByIndex(
162621 const Expr *pExpr,
162622 const Index *pIdx,
162623 int iTabCur
162624){
162625 int i;
162626 for(i=0; i<pIdx->nColumn; i++){
162627 if( pIdx->aiColumn[i]==XN_EXPR
162628 && sqlite3ExprCompare(pParse: 0, pA: pExpr, pB: pIdx->aColExpr->a[i].pExpr, iTab: iTabCur)==0
162629 ){
162630 return 1;
162631 }
162632 }
162633 return 0;
162634}
162635
162636/*
162637** Structure passed to the whereIsCoveringIndex Walker callback.
162638*/
162639typedef struct CoveringIndexCheck CoveringIndexCheck;
162640struct CoveringIndexCheck {
162641 Index *pIdx; /* The index */
162642 int iTabCur; /* Cursor number for the corresponding table */
162643 u8 bExpr; /* Uses an indexed expression */
162644 u8 bUnidx; /* Uses an unindexed column not within an indexed expr */
162645};
162646
162647/*
162648** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
162649**
162650** If the Expr node references the table with cursor pCk->iTabCur, then
162651** make sure that column is covered by the index pCk->pIdx. We know that
162652** all columns less than 63 (really BMS-1) are covered, so we don't need
162653** to check them. But we do need to check any column at 63 or greater.
162654**
162655** If the index does not cover the column, then set pWalk->eCode to
162656** non-zero and return WRC_Abort to stop the search.
162657**
162658** If this node does not disprove that the index can be a covering index,
162659** then just return WRC_Continue, to continue the search.
162660**
162661** If pCk->pIdx contains indexed expressions and one of those expressions
162662** matches pExpr, then prune the search.
162663*/
162664static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
162665 int i; /* Loop counter */
162666 const Index *pIdx; /* The index of interest */
162667 const i16 *aiColumn; /* Columns contained in the index */
162668 u16 nColumn; /* Number of columns in the index */
162669 CoveringIndexCheck *pCk; /* Info about this search */
162670
162671 pCk = pWalk->u.pCovIdxCk;
162672 pIdx = pCk->pIdx;
162673 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
162674 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
162675 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
162676 pIdx = pWalk->u.pCovIdxCk->pIdx;
162677 aiColumn = pIdx->aiColumn;
162678 nColumn = pIdx->nColumn;
162679 for(i=0; i<nColumn; i++){
162680 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
162681 }
162682 pCk->bUnidx = 1;
162683 return WRC_Abort;
162684 }else if( pIdx->bHasExpr
162685 && exprIsCoveredByIndex(pExpr, pIdx, iTabCur: pWalk->u.pCovIdxCk->iTabCur) ){
162686 pCk->bExpr = 1;
162687 return WRC_Prune;
162688 }
162689 return WRC_Continue;
162690}
162691
162692
162693/*
162694** pIdx is an index that covers all of the low-number columns used by
162695** pWInfo->pSelect (columns from 0 through 62) or an index that has
162696** expressions terms. Hence, we cannot determine whether or not it is
162697** a covering index by using the colUsed bitmasks. We have to do a search
162698** to see if the index is covering. This routine does that search.
162699**
162700** The return value is one of these:
162701**
162702** 0 The index is definitely not a covering index
162703**
162704** WHERE_IDX_ONLY The index is definitely a covering index
162705**
162706** WHERE_EXPRIDX The index is likely a covering index, but it is
162707** difficult to determine precisely because of the
162708** expressions that are indexed. Score it as a
162709** covering index, but still keep the main table open
162710** just in case we need it.
162711**
162712** This routine is an optimization. It is always safe to return zero.
162713** But returning one of the other two values when zero should have been
162714** returned can lead to incorrect bytecode and assertion faults.
162715*/
162716static SQLITE_NOINLINE u32 whereIsCoveringIndex(
162717 WhereInfo *pWInfo, /* The WHERE clause context */
162718 Index *pIdx, /* Index that is being tested */
162719 int iTabCur /* Cursor for the table being indexed */
162720){
162721 int i, rc;
162722 struct CoveringIndexCheck ck;
162723 Walker w;
162724 if( pWInfo->pSelect==0 ){
162725 /* We don't have access to the full query, so we cannot check to see
162726 ** if pIdx is covering. Assume it is not. */
162727 return 0;
162728 }
162729 if( pIdx->bHasExpr==0 ){
162730 for(i=0; i<pIdx->nColumn; i++){
162731 if( pIdx->aiColumn[i]>=BMS-1 ) break;
162732 }
162733 if( i>=pIdx->nColumn ){
162734 /* pIdx does not index any columns greater than 62, but we know from
162735 ** colMask that columns greater than 62 are used, so this is not a
162736 ** covering index */
162737 return 0;
162738 }
162739 }
162740 ck.pIdx = pIdx;
162741 ck.iTabCur = iTabCur;
162742 ck.bExpr = 0;
162743 ck.bUnidx = 0;
162744 memset(s: &w, c: 0, n: sizeof(w));
162745 w.xExprCallback = whereIsCoveringIndexWalkCallback;
162746 w.xSelectCallback = sqlite3SelectWalkNoop;
162747 w.u.pCovIdxCk = &ck;
162748 sqlite3WalkSelect(pWalker: &w, p: pWInfo->pSelect);
162749 if( ck.bUnidx ){
162750 rc = 0;
162751 }else if( ck.bExpr ){
162752 rc = WHERE_EXPRIDX;
162753 }else{
162754 rc = WHERE_IDX_ONLY;
162755 }
162756 return rc;
162757}
162758
162759/*
162760** Add all WhereLoop objects for a single table of the join where the table
162761** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
162762** a b-tree table, not a virtual table.
162763**
162764** The costs (WhereLoop.rRun) of the b-tree loops added by this function
162765** are calculated as follows:
162766**
162767** For a full scan, assuming the table (or index) contains nRow rows:
162768**
162769** cost = nRow * 3.0 // full-table scan
162770** cost = nRow * K // scan of covering index
162771** cost = nRow * (K+3.0) // scan of non-covering index
162772**
162773** where K is a value between 1.1 and 3.0 set based on the relative
162774** estimated average size of the index and table records.
162775**
162776** For an index scan, where nVisit is the number of index rows visited
162777** by the scan, and nSeek is the number of seek operations required on
162778** the index b-tree:
162779**
162780** cost = nSeek * (log(nRow) + K * nVisit) // covering index
162781** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
162782**
162783** Normally, nSeek is 1. nSeek values greater than 1 come about if the
162784** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
162785** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
162786**
162787** The estimated values (nRow, nVisit, nSeek) often contain a large amount
162788** of uncertainty. For this reason, scoring is designed to pick plans that
162789** "do the least harm" if the estimates are inaccurate. For example, a
162790** log(nRow) factor is omitted from a non-covering index scan in order to
162791** bias the scoring in favor of using an index, since the worst-case
162792** performance of using an index is far better than the worst-case performance
162793** of a full table scan.
162794*/
162795static int whereLoopAddBtree(
162796 WhereLoopBuilder *pBuilder, /* WHERE clause information */
162797 Bitmask mPrereq /* Extra prerequisites for using this table */
162798){
162799 WhereInfo *pWInfo; /* WHERE analysis context */
162800 Index *pProbe; /* An index we are evaluating */
162801 Index sPk; /* A fake index object for the primary key */
162802 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
162803 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
162804 SrcList *pTabList; /* The FROM clause */
162805 SrcItem *pSrc; /* The FROM clause btree term to add */
162806 WhereLoop *pNew; /* Template WhereLoop object */
162807 int rc = SQLITE_OK; /* Return code */
162808 int iSortIdx = 1; /* Index number */
162809 int b; /* A boolean value */
162810 LogEst rSize; /* number of rows in the table */
162811 WhereClause *pWC; /* The parsed WHERE clause */
162812 Table *pTab; /* Table being queried */
162813
162814 pNew = pBuilder->pNew;
162815 pWInfo = pBuilder->pWInfo;
162816 pTabList = pWInfo->pTabList;
162817 pSrc = pTabList->a + pNew->iTab;
162818 pTab = pSrc->pTab;
162819 pWC = pBuilder->pWC;
162820 assert( !IsVirtual(pSrc->pTab) );
162821
162822 if( pSrc->fg.isIndexedBy ){
162823 assert( pSrc->fg.isCte==0 );
162824 /* An INDEXED BY clause specifies a particular index to use */
162825 pProbe = pSrc->u2.pIBIndex;
162826 }else if( !HasRowid(pTab) ){
162827 pProbe = pTab->pIndex;
162828 }else{
162829 /* There is no INDEXED BY clause. Create a fake Index object in local
162830 ** variable sPk to represent the rowid primary key index. Make this
162831 ** fake index the first in a chain of Index objects with all of the real
162832 ** indices to follow */
162833 Index *pFirst; /* First of real indices on the table */
162834 memset(s: &sPk, c: 0, n: sizeof(Index));
162835 sPk.nKeyCol = 1;
162836 sPk.nColumn = 1;
162837 sPk.aiColumn = &aiColumnPk;
162838 sPk.aiRowLogEst = aiRowEstPk;
162839 sPk.onError = OE_Replace;
162840 sPk.pTable = pTab;
162841 sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
162842 sPk.idxType = SQLITE_IDXTYPE_IPK;
162843 aiRowEstPk[0] = pTab->nRowLogEst;
162844 aiRowEstPk[1] = 0;
162845 pFirst = pSrc->pTab->pIndex;
162846 if( pSrc->fg.notIndexed==0 ){
162847 /* The real indices of the table are only considered if the
162848 ** NOT INDEXED qualifier is omitted from the FROM clause */
162849 sPk.pNext = pFirst;
162850 }
162851 pProbe = &sPk;
162852 }
162853 rSize = pTab->nRowLogEst;
162854
162855#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
162856 /* Automatic indexes */
162857 if( !pBuilder->pOrSet /* Not part of an OR optimization */
162858 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
162859 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
162860 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
162861 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
162862 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
162863 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
162864 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
162865 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
162866 ){
162867 /* Generate auto-index WhereLoops */
162868 LogEst rLogSize; /* Logarithm of the number of rows in the table */
162869 WhereTerm *pTerm;
162870 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
162871 rLogSize = estLog(N: rSize);
162872 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
162873 if( pTerm->prereqRight & pNew->maskSelf ) continue;
162874 if( termCanDriveIndex(pTerm, pSrc, notReady: 0) ){
162875 pNew->u.btree.nEq = 1;
162876 pNew->nSkip = 0;
162877 pNew->u.btree.pIndex = 0;
162878 pNew->nLTerm = 1;
162879 pNew->aLTerm[0] = pTerm;
162880 /* TUNING: One-time cost for computing the automatic index is
162881 ** estimated to be X*N*log2(N) where N is the number of rows in
162882 ** the table being indexed and where X is 7 (LogEst=28) for normal
162883 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
162884 ** of X is smaller for views and subqueries so that the query planner
162885 ** will be more aggressive about generating automatic indexes for
162886 ** those objects, since there is no opportunity to add schema
162887 ** indexes on subqueries and views. */
162888 pNew->rSetup = rLogSize + rSize;
162889 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
162890 pNew->rSetup += 28;
162891 }else{
162892 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
162893 ** on ephemeral materializations of views */
162894 }
162895 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
162896 if( pNew->rSetup<0 ) pNew->rSetup = 0;
162897 /* TUNING: Each index lookup yields 20 rows in the table. This
162898 ** is more than the usual guess of 10 rows, since we have no way
162899 ** of knowing how selective the index will ultimately be. It would
162900 ** not be unreasonable to make this value much larger. */
162901 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
162902 pNew->rRun = sqlite3LogEstAdd(a: rLogSize,b: pNew->nOut);
162903 pNew->wsFlags = WHERE_AUTO_INDEX;
162904 pNew->prereq = mPrereq | pTerm->prereqRight;
162905 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
162906 }
162907 }
162908 }
162909#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
162910
162911 /* Loop over all indices. If there was an INDEXED BY clause, then only
162912 ** consider index pProbe. */
162913 for(; rc==SQLITE_OK && pProbe;
162914 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
162915 ){
162916 if( pProbe->pPartIdxWhere!=0
162917 && !whereUsablePartialIndex(iTab: pSrc->iCursor, jointype: pSrc->fg.jointype, pWC,
162918 pWhere: pProbe->pPartIdxWhere)
162919 ){
162920 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
162921 continue; /* Partial index inappropriate for this query */
162922 }
162923 if( pProbe->bNoQuery ) continue;
162924 rSize = pProbe->aiRowLogEst[0];
162925 pNew->u.btree.nEq = 0;
162926 pNew->u.btree.nBtm = 0;
162927 pNew->u.btree.nTop = 0;
162928 pNew->nSkip = 0;
162929 pNew->nLTerm = 0;
162930 pNew->iSortIdx = 0;
162931 pNew->rSetup = 0;
162932 pNew->prereq = mPrereq;
162933 pNew->nOut = rSize;
162934 pNew->u.btree.pIndex = pProbe;
162935 b = indexMightHelpWithOrderBy(pBuilder, pIndex: pProbe, iCursor: pSrc->iCursor);
162936
162937 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
162938 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
162939 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
162940 /* Integer primary key index */
162941 pNew->wsFlags = WHERE_IPK;
162942
162943 /* Full table scan */
162944 pNew->iSortIdx = b ? iSortIdx : 0;
162945 /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an
162946 ** extra cost designed to discourage the use of full table scans,
162947 ** since index lookups have better worst-case performance if our
162948 ** stat guesses are wrong. Reduce the 3.0 penalty slightly
162949 ** (to 2.75) if we have valid STAT4 information for the table.
162950 ** At 2.75, a full table scan is preferred over using an index on
162951 ** a column with just two distinct values where each value has about
162952 ** an equal number of appearances. Without STAT4 data, we still want
162953 ** to use an index in that case, since the constraint might be for
162954 ** the scarcer of the two values, and in that case an index lookup is
162955 ** better.
162956 */
162957#ifdef SQLITE_ENABLE_STAT4
162958 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
162959#else
162960 pNew->rRun = rSize + 16;
162961#endif
162962 if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){
162963 pNew->wsFlags |= WHERE_VIEWSCAN;
162964 }
162965 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
162966 whereLoopOutputAdjust(pWC, pLoop: pNew, nRow: rSize);
162967 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
162968 pNew->nOut = rSize;
162969 if( rc ) break;
162970 }else{
162971 Bitmask m;
162972 if( pProbe->isCovering ){
162973 m = 0;
162974 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
162975 }else{
162976 m = pSrc->colUsed & pProbe->colNotIdxed;
162977 pNew->wsFlags = WHERE_INDEXED;
162978 if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
162979 u32 isCov = whereIsCoveringIndex(pWInfo, pIdx: pProbe, iTabCur: pSrc->iCursor);
162980 if( isCov==0 ){
162981 WHERETRACE(0x200,
162982 ("-> %s is not a covering index"
162983 " according to whereIsCoveringIndex()\n", pProbe->zName));
162984 assert( m!=0 );
162985 }else{
162986 m = 0;
162987 pNew->wsFlags |= isCov;
162988 if( isCov & WHERE_IDX_ONLY ){
162989 WHERETRACE(0x200,
162990 ("-> %s is a covering expression index"
162991 " according to whereIsCoveringIndex()\n", pProbe->zName));
162992 }else{
162993 assert( isCov==WHERE_EXPRIDX );
162994 WHERETRACE(0x200,
162995 ("-> %s might be a covering expression index"
162996 " according to whereIsCoveringIndex()\n", pProbe->zName));
162997 }
162998 }
162999 }else if( m==0 ){
163000 WHERETRACE(0x200,
163001 ("-> %s a covering index according to bitmasks\n",
163002 pProbe->zName, m==0 ? "is" : "is not"));
163003 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
163004 }
163005 }
163006
163007 /* Full scan via index */
163008 if( b
163009 || !HasRowid(pTab)
163010 || pProbe->pPartIdxWhere!=0
163011 || pSrc->fg.isIndexedBy
163012 || ( m==0
163013 && pProbe->bUnordered==0
163014 && (pProbe->szIdxRow<pTab->szTabRow)
163015 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
163016 && sqlite3GlobalConfig.bUseCis
163017 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
163018 )
163019 ){
163020 pNew->iSortIdx = b ? iSortIdx : 0;
163021
163022 /* The cost of visiting the index rows is N*K, where K is
163023 ** between 1.1 and 3.0, depending on the relative sizes of the
163024 ** index and table rows. */
163025 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
163026 if( m!=0 ){
163027 /* If this is a non-covering index scan, add in the cost of
163028 ** doing table lookups. The cost will be 3x the number of
163029 ** lookups. Take into account WHERE clause terms that can be
163030 ** satisfied using just the index, and that do not require a
163031 ** table lookup. */
163032 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
163033 int ii;
163034 int iCur = pSrc->iCursor;
163035 WhereClause *pWC2 = &pWInfo->sWC;
163036 for(ii=0; ii<pWC2->nTerm; ii++){
163037 WhereTerm *pTerm = &pWC2->a[ii];
163038 if( !sqlite3ExprCoveredByIndex(pExpr: pTerm->pExpr, iCur, pIdx: pProbe) ){
163039 break;
163040 }
163041 /* pTerm can be evaluated using just the index. So reduce
163042 ** the expected number of table lookups accordingly */
163043 if( pTerm->truthProb<=0 ){
163044 nLookup += pTerm->truthProb;
163045 }else{
163046 nLookup--;
163047 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
163048 }
163049 }
163050
163051 pNew->rRun = sqlite3LogEstAdd(a: pNew->rRun, b: nLookup);
163052 }
163053 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
163054 whereLoopOutputAdjust(pWC, pLoop: pNew, nRow: rSize);
163055 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
163056 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
163057 ** because the cursor used to access the index might not be
163058 ** positioned to the correct row during the right-join no-match
163059 ** loop. */
163060 }else{
163061 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
163062 }
163063 pNew->nOut = rSize;
163064 if( rc ) break;
163065 }
163066 }
163067
163068 pBuilder->bldFlags1 = 0;
163069 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul: 0);
163070 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
163071 /* If a non-unique index is used, or if a prefix of the key for
163072 ** unique index is used (making the index functionally non-unique)
163073 ** then the sqlite_stat1 data becomes important for scoring the
163074 ** plan */
163075 pTab->tabFlags |= TF_StatsUsed;
163076 }
163077#ifdef SQLITE_ENABLE_STAT4
163078 sqlite3Stat4ProbeFree(pBuilder->pRec);
163079 pBuilder->nRecValid = 0;
163080 pBuilder->pRec = 0;
163081#endif
163082 }
163083 return rc;
163084}
163085
163086#ifndef SQLITE_OMIT_VIRTUALTABLE
163087
163088/*
163089** Return true if pTerm is a virtual table LIMIT or OFFSET term.
163090*/
163091static int isLimitTerm(WhereTerm *pTerm){
163092 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
163093 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
163094 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
163095}
163096
163097/*
163098** Argument pIdxInfo is already populated with all constraints that may
163099** be used by the virtual table identified by pBuilder->pNew->iTab. This
163100** function marks a subset of those constraints usable, invokes the
163101** xBestIndex method and adds the returned plan to pBuilder.
163102**
163103** A constraint is marked usable if:
163104**
163105** * Argument mUsable indicates that its prerequisites are available, and
163106**
163107** * It is not one of the operators specified in the mExclude mask passed
163108** as the fourth argument (which in practice is either WO_IN or 0).
163109**
163110** Argument mPrereq is a mask of tables that must be scanned before the
163111** virtual table in question. These are added to the plans prerequisites
163112** before it is added to pBuilder.
163113**
163114** Output parameter *pbIn is set to true if the plan added to pBuilder
163115** uses one or more WO_IN terms, or false otherwise.
163116*/
163117static int whereLoopAddVirtualOne(
163118 WhereLoopBuilder *pBuilder,
163119 Bitmask mPrereq, /* Mask of tables that must be used. */
163120 Bitmask mUsable, /* Mask of usable tables */
163121 u16 mExclude, /* Exclude terms using these operators */
163122 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
163123 u16 mNoOmit, /* Do not omit these constraints */
163124 int *pbIn, /* OUT: True if plan uses an IN(...) op */
163125 int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */
163126){
163127 WhereClause *pWC = pBuilder->pWC;
163128 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
163129 struct sqlite3_index_constraint *pIdxCons;
163130 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
163131 int i;
163132 int mxTerm;
163133 int rc = SQLITE_OK;
163134 WhereLoop *pNew = pBuilder->pNew;
163135 Parse *pParse = pBuilder->pWInfo->pParse;
163136 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
163137 int nConstraint = pIdxInfo->nConstraint;
163138
163139 assert( (mUsable & mPrereq)==mPrereq );
163140 *pbIn = 0;
163141 pNew->prereq = mPrereq;
163142
163143 /* Set the usable flag on the subset of constraints identified by
163144 ** arguments mUsable and mExclude. */
163145 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
163146 for(i=0; i<nConstraint; i++, pIdxCons++){
163147 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
163148 pIdxCons->usable = 0;
163149 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
163150 && (pTerm->eOperator & mExclude)==0
163151 && (pbRetryLimit || !isLimitTerm(pTerm))
163152 ){
163153 pIdxCons->usable = 1;
163154 }
163155 }
163156
163157 /* Initialize the output fields of the sqlite3_index_info structure */
163158 memset(s: pUsage, c: 0, n: sizeof(pUsage[0])*nConstraint);
163159 assert( pIdxInfo->needToFreeIdxStr==0 );
163160 pIdxInfo->idxStr = 0;
163161 pIdxInfo->idxNum = 0;
163162 pIdxInfo->orderByConsumed = 0;
163163 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
163164 pIdxInfo->estimatedRows = 25;
163165 pIdxInfo->idxFlags = 0;
163166 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
163167 pHidden->mHandleIn = 0;
163168
163169 /* Invoke the virtual table xBestIndex() method */
163170 rc = vtabBestIndex(pParse, pTab: pSrc->pTab, p: pIdxInfo);
163171 if( rc ){
163172 if( rc==SQLITE_CONSTRAINT ){
163173 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
163174 ** that the particular combination of parameters provided is unusable.
163175 ** Make no entries in the loop table.
163176 */
163177 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
163178 return SQLITE_OK;
163179 }
163180 return rc;
163181 }
163182
163183 mxTerm = -1;
163184 assert( pNew->nLSlot>=nConstraint );
163185 memset(s: pNew->aLTerm, c: 0, n: sizeof(pNew->aLTerm[0])*nConstraint );
163186 memset(s: &pNew->u.vtab, c: 0, n: sizeof(pNew->u.vtab));
163187 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
163188 for(i=0; i<nConstraint; i++, pIdxCons++){
163189 int iTerm;
163190 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
163191 WhereTerm *pTerm;
163192 int j = pIdxCons->iTermOffset;
163193 if( iTerm>=nConstraint
163194 || j<0
163195 || j>=pWC->nTerm
163196 || pNew->aLTerm[iTerm]!=0
163197 || pIdxCons->usable==0
163198 ){
163199 sqlite3ErrorMsg(pParse,zFormat: "%s.xBestIndex malfunction",pSrc->pTab->zName);
163200 testcase( pIdxInfo->needToFreeIdxStr );
163201 return SQLITE_ERROR;
163202 }
163203 testcase( iTerm==nConstraint-1 );
163204 testcase( j==0 );
163205 testcase( j==pWC->nTerm-1 );
163206 pTerm = &pWC->a[j];
163207 pNew->prereq |= pTerm->prereqRight;
163208 assert( iTerm<pNew->nLSlot );
163209 pNew->aLTerm[iTerm] = pTerm;
163210 if( iTerm>mxTerm ) mxTerm = iTerm;
163211 testcase( iTerm==15 );
163212 testcase( iTerm==16 );
163213 if( pUsage[i].omit ){
163214 if( i<16 && ((1<<i)&mNoOmit)==0 ){
163215 testcase( i!=iTerm );
163216 pNew->u.vtab.omitMask |= 1<<iTerm;
163217 }else{
163218 testcase( i!=iTerm );
163219 }
163220 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
163221 pNew->u.vtab.bOmitOffset = 1;
163222 }
163223 }
163224 if( SMASKBIT32(i) & pHidden->mHandleIn ){
163225 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
163226 }else if( (pTerm->eOperator & WO_IN)!=0 ){
163227 /* A virtual table that is constrained by an IN clause may not
163228 ** consume the ORDER BY clause because (1) the order of IN terms
163229 ** is not necessarily related to the order of output terms and
163230 ** (2) Multiple outputs from a single IN value will not merge
163231 ** together. */
163232 pIdxInfo->orderByConsumed = 0;
163233 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
163234 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
163235 }
163236
163237 assert( pbRetryLimit || !isLimitTerm(pTerm) );
163238 if( isLimitTerm(pTerm) && *pbIn ){
163239 /* If there is an IN(...) term handled as an == (separate call to
163240 ** xFilter for each value on the RHS of the IN) and a LIMIT or
163241 ** OFFSET term handled as well, the plan is unusable. Set output
163242 ** variable *pbRetryLimit to true to tell the caller to retry with
163243 ** LIMIT and OFFSET disabled. */
163244 if( pIdxInfo->needToFreeIdxStr ){
163245 sqlite3_free(p: pIdxInfo->idxStr);
163246 pIdxInfo->idxStr = 0;
163247 pIdxInfo->needToFreeIdxStr = 0;
163248 }
163249 *pbRetryLimit = 1;
163250 return SQLITE_OK;
163251 }
163252 }
163253 }
163254
163255 pNew->nLTerm = mxTerm+1;
163256 for(i=0; i<=mxTerm; i++){
163257 if( pNew->aLTerm[i]==0 ){
163258 /* The non-zero argvIdx values must be contiguous. Raise an
163259 ** error if they are not */
163260 sqlite3ErrorMsg(pParse,zFormat: "%s.xBestIndex malfunction",pSrc->pTab->zName);
163261 testcase( pIdxInfo->needToFreeIdxStr );
163262 return SQLITE_ERROR;
163263 }
163264 }
163265 assert( pNew->nLTerm<=pNew->nLSlot );
163266 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
163267 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
163268 pIdxInfo->needToFreeIdxStr = 0;
163269 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
163270 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
163271 pIdxInfo->nOrderBy : 0);
163272 pNew->rSetup = 0;
163273 pNew->rRun = sqlite3LogEstFromDouble(x: pIdxInfo->estimatedCost);
163274 pNew->nOut = sqlite3LogEst(x: pIdxInfo->estimatedRows);
163275
163276 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
163277 ** that the scan will visit at most one row. Clear it otherwise. */
163278 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
163279 pNew->wsFlags |= WHERE_ONEROW;
163280 }else{
163281 pNew->wsFlags &= ~WHERE_ONEROW;
163282 }
163283 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
163284 if( pNew->u.vtab.needFree ){
163285 sqlite3_free(p: pNew->u.vtab.idxStr);
163286 pNew->u.vtab.needFree = 0;
163287 }
163288 WHERETRACE(0xffffffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
163289 *pbIn, (sqlite3_uint64)mPrereq,
163290 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
163291
163292 return rc;
163293}
163294
163295/*
163296** Return the collating sequence for a constraint passed into xBestIndex.
163297**
163298** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex.
163299** This routine depends on there being a HiddenIndexInfo structure immediately
163300** following the sqlite3_index_info structure.
163301**
163302** Return a pointer to the collation name:
163303**
163304** 1. If there is an explicit COLLATE operator on the constraint, return it.
163305**
163306** 2. Else, if the column has an alternative collation, return that.
163307**
163308** 3. Otherwise, return "BINARY".
163309*/
163310SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
163311 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
163312 const char *zRet = 0;
163313 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
163314 CollSeq *pC = 0;
163315 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
163316 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
163317 if( pX->pLeft ){
163318 pC = sqlite3ExprCompareCollSeq(pParse: pHidden->pParse, p: pX);
163319 }
163320 zRet = (pC ? pC->zName : sqlite3StrBINARY);
163321 }
163322 return zRet;
163323}
163324
163325/*
163326** Return true if constraint iCons is really an IN(...) constraint, or
163327** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0)
163328** or clear (if bHandle==0) the flag to handle it using an iterator.
163329*/
163330SQLITE_API int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){
163331 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
163332 u32 m = SMASKBIT32(iCons);
163333 if( m & pHidden->mIn ){
163334 if( bHandle==0 ){
163335 pHidden->mHandleIn &= ~m;
163336 }else if( bHandle>0 ){
163337 pHidden->mHandleIn |= m;
163338 }
163339 return 1;
163340 }
163341 return 0;
163342}
163343
163344/*
163345** This interface is callable from within the xBestIndex callback only.
163346**
163347** If possible, set (*ppVal) to point to an object containing the value
163348** on the right-hand-side of constraint iCons.
163349*/
163350SQLITE_API int sqlite3_vtab_rhs_value(
163351 sqlite3_index_info *pIdxInfo, /* Copy of first argument to xBestIndex */
163352 int iCons, /* Constraint for which RHS is wanted */
163353 sqlite3_value **ppVal /* Write value extracted here */
163354){
163355 HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1];
163356 sqlite3_value *pVal = 0;
163357 int rc = SQLITE_OK;
163358 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
163359 rc = SQLITE_MISUSE; /* EV: R-30545-25046 */
163360 }else{
163361 if( pH->aRhs[iCons]==0 ){
163362 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
163363 rc = sqlite3ValueFromExpr(
163364 db: pH->pParse->db, pExpr: pTerm->pExpr->pRight, ENC(pH->pParse->db),
163365 SQLITE_AFF_BLOB, ppVal: &pH->aRhs[iCons]
163366 );
163367 testcase( rc!=SQLITE_OK );
163368 }
163369 pVal = pH->aRhs[iCons];
163370 }
163371 *ppVal = pVal;
163372
163373 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
163374 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
163375 }
163376
163377 return rc;
163378}
163379
163380/*
163381** Return true if ORDER BY clause may be handled as DISTINCT.
163382*/
163383SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){
163384 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
163385 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
163386 return pHidden->eDistinct;
163387}
163388
163389/*
163390** Cause the prepared statement that is associated with a call to
163391** xBestIndex to potentially use all schemas. If the statement being
163392** prepared is read-only, then just start read transactions on all
163393** schemas. But if this is a write operation, start writes on all
163394** schemas.
163395**
163396** This is used by the (built-in) sqlite_dbpage virtual table.
163397*/
163398SQLITE_PRIVATE void sqlite3VtabUsesAllSchemas(Parse *pParse){
163399 int nDb = pParse->db->nDb;
163400 int i;
163401 for(i=0; i<nDb; i++){
163402 sqlite3CodeVerifySchema(pParse, iDb: i);
163403 }
163404 if( DbMaskNonZero(pParse->writeMask) ){
163405 for(i=0; i<nDb; i++){
163406 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb: i);
163407 }
163408 }
163409}
163410
163411/*
163412** Add all WhereLoop objects for a table of the join identified by
163413** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
163414**
163415** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
163416** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
163417** entries that occur before the virtual table in the FROM clause and are
163418** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
163419** mUnusable mask contains all FROM clause entries that occur after the
163420** virtual table and are separated from it by at least one LEFT or
163421** CROSS JOIN.
163422**
163423** For example, if the query were:
163424**
163425** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
163426**
163427** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
163428**
163429** All the tables in mPrereq must be scanned before the current virtual
163430** table. So any terms for which all prerequisites are satisfied by
163431** mPrereq may be specified as "usable" in all calls to xBestIndex.
163432** Conversely, all tables in mUnusable must be scanned after the current
163433** virtual table, so any terms for which the prerequisites overlap with
163434** mUnusable should always be configured as "not-usable" for xBestIndex.
163435*/
163436static int whereLoopAddVirtual(
163437 WhereLoopBuilder *pBuilder, /* WHERE clause information */
163438 Bitmask mPrereq, /* Tables that must be scanned before this one */
163439 Bitmask mUnusable /* Tables that must be scanned after this one */
163440){
163441 int rc = SQLITE_OK; /* Return code */
163442 WhereInfo *pWInfo; /* WHERE analysis context */
163443 Parse *pParse; /* The parsing context */
163444 WhereClause *pWC; /* The WHERE clause */
163445 SrcItem *pSrc; /* The FROM clause term to search */
163446 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
163447 int nConstraint; /* Number of constraints in p */
163448 int bIn; /* True if plan uses IN(...) operator */
163449 WhereLoop *pNew;
163450 Bitmask mBest; /* Tables used by best possible plan */
163451 u16 mNoOmit;
163452 int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */
163453
163454 assert( (mPrereq & mUnusable)==0 );
163455 pWInfo = pBuilder->pWInfo;
163456 pParse = pWInfo->pParse;
163457 pWC = pBuilder->pWC;
163458 pNew = pBuilder->pNew;
163459 pSrc = &pWInfo->pTabList->a[pNew->iTab];
163460 assert( IsVirtual(pSrc->pTab) );
163461 p = allocateIndexInfo(pWInfo, pWC, mUnusable, pSrc, pmNoOmit: &mNoOmit);
163462 if( p==0 ) return SQLITE_NOMEM_BKPT;
163463 pNew->rSetup = 0;
163464 pNew->wsFlags = WHERE_VIRTUALTABLE;
163465 pNew->nLTerm = 0;
163466 pNew->u.vtab.needFree = 0;
163467 nConstraint = p->nConstraint;
163468 if( whereLoopResize(db: pParse->db, p: pNew, n: nConstraint) ){
163469 freeIndexInfo(db: pParse->db, pIdxInfo: p);
163470 return SQLITE_NOMEM_BKPT;
163471 }
163472
163473 /* First call xBestIndex() with all constraints usable. */
163474 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
163475 WHERETRACE(0x800, (" VirtualOne: all usable\n"));
163476 rc = whereLoopAddVirtualOne(
163477 pBuilder, mPrereq, ALLBITS, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn, pbRetryLimit: &bRetry
163478 );
163479 if( bRetry ){
163480 assert( rc==SQLITE_OK );
163481 rc = whereLoopAddVirtualOne(
163482 pBuilder, mPrereq, ALLBITS, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn, pbRetryLimit: 0
163483 );
163484 }
163485
163486 /* If the call to xBestIndex() with all terms enabled produced a plan
163487 ** that does not require any source tables (IOW: a plan with mBest==0)
163488 ** and does not use an IN(...) operator, then there is no point in making
163489 ** any further calls to xBestIndex() since they will all return the same
163490 ** result (if the xBestIndex() implementation is sane). */
163491 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
163492 int seenZero = 0; /* True if a plan with no prereqs seen */
163493 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
163494 Bitmask mPrev = 0;
163495 Bitmask mBestNoIn = 0;
163496
163497 /* If the plan produced by the earlier call uses an IN(...) term, call
163498 ** xBestIndex again, this time with IN(...) terms disabled. */
163499 if( bIn ){
163500 WHERETRACE(0x800, (" VirtualOne: all usable w/o IN\n"));
163501 rc = whereLoopAddVirtualOne(
163502 pBuilder, mPrereq, ALLBITS, WO_IN, pIdxInfo: p, mNoOmit, pbIn: &bIn, pbRetryLimit: 0);
163503 assert( bIn==0 );
163504 mBestNoIn = pNew->prereq & ~mPrereq;
163505 if( mBestNoIn==0 ){
163506 seenZero = 1;
163507 seenZeroNoIN = 1;
163508 }
163509 }
163510
163511 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
163512 ** in the set of terms that apply to the current virtual table. */
163513 while( rc==SQLITE_OK ){
163514 int i;
163515 Bitmask mNext = ALLBITS;
163516 assert( mNext>0 );
163517 for(i=0; i<nConstraint; i++){
163518 Bitmask mThis = (
163519 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
163520 );
163521 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
163522 }
163523 mPrev = mNext;
163524 if( mNext==ALLBITS ) break;
163525 if( mNext==mBest || mNext==mBestNoIn ) continue;
163526 WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
163527 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
163528 rc = whereLoopAddVirtualOne(
163529 pBuilder, mPrereq, mUsable: mNext|mPrereq, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn, pbRetryLimit: 0);
163530 if( pNew->prereq==mPrereq ){
163531 seenZero = 1;
163532 if( bIn==0 ) seenZeroNoIN = 1;
163533 }
163534 }
163535
163536 /* If the calls to xBestIndex() in the above loop did not find a plan
163537 ** that requires no source tables at all (i.e. one guaranteed to be
163538 ** usable), make a call here with all source tables disabled */
163539 if( rc==SQLITE_OK && seenZero==0 ){
163540 WHERETRACE(0x800, (" VirtualOne: all disabled\n"));
163541 rc = whereLoopAddVirtualOne(
163542 pBuilder, mPrereq, mUsable: mPrereq, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn, pbRetryLimit: 0);
163543 if( bIn==0 ) seenZeroNoIN = 1;
163544 }
163545
163546 /* If the calls to xBestIndex() have so far failed to find a plan
163547 ** that requires no source tables at all and does not use an IN(...)
163548 ** operator, make a final call to obtain one here. */
163549 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
163550 WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n"));
163551 rc = whereLoopAddVirtualOne(
163552 pBuilder, mPrereq, mUsable: mPrereq, WO_IN, pIdxInfo: p, mNoOmit, pbIn: &bIn, pbRetryLimit: 0);
163553 }
163554 }
163555
163556 if( p->needToFreeIdxStr ) sqlite3_free(p: p->idxStr);
163557 freeIndexInfo(db: pParse->db, pIdxInfo: p);
163558 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
163559 return rc;
163560}
163561#endif /* SQLITE_OMIT_VIRTUALTABLE */
163562
163563/*
163564** Add WhereLoop entries to handle OR terms. This works for either
163565** btrees or virtual tables.
163566*/
163567static int whereLoopAddOr(
163568 WhereLoopBuilder *pBuilder,
163569 Bitmask mPrereq,
163570 Bitmask mUnusable
163571){
163572 WhereInfo *pWInfo = pBuilder->pWInfo;
163573 WhereClause *pWC;
163574 WhereLoop *pNew;
163575 WhereTerm *pTerm, *pWCEnd;
163576 int rc = SQLITE_OK;
163577 int iCur;
163578 WhereClause tempWC;
163579 WhereLoopBuilder sSubBuild;
163580 WhereOrSet sSum, sCur;
163581 SrcItem *pItem;
163582
163583 pWC = pBuilder->pWC;
163584 pWCEnd = pWC->a + pWC->nTerm;
163585 pNew = pBuilder->pNew;
163586 memset(s: &sSum, c: 0, n: sizeof(sSum));
163587 pItem = pWInfo->pTabList->a + pNew->iTab;
163588 iCur = pItem->iCursor;
163589
163590 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
163591 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
163592
163593 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
163594 if( (pTerm->eOperator & WO_OR)!=0
163595 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
163596 ){
163597 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
163598 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
163599 WhereTerm *pOrTerm;
163600 int once = 1;
163601 int i, j;
163602
163603 sSubBuild = *pBuilder;
163604 sSubBuild.pOrSet = &sCur;
163605
163606 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
163607 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
163608 if( (pOrTerm->eOperator & WO_AND)!=0 ){
163609 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
163610 }else if( pOrTerm->leftCursor==iCur ){
163611 tempWC.pWInfo = pWC->pWInfo;
163612 tempWC.pOuter = pWC;
163613 tempWC.op = TK_AND;
163614 tempWC.nTerm = 1;
163615 tempWC.nBase = 1;
163616 tempWC.a = pOrTerm;
163617 sSubBuild.pWC = &tempWC;
163618 }else{
163619 continue;
163620 }
163621 sCur.n = 0;
163622#ifdef WHERETRACE_ENABLED
163623 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
163624 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
163625 if( sqlite3WhereTrace & 0x20000 ){
163626 sqlite3WhereClausePrint(sSubBuild.pWC);
163627 }
163628#endif
163629#ifndef SQLITE_OMIT_VIRTUALTABLE
163630 if( IsVirtual(pItem->pTab) ){
163631 rc = whereLoopAddVirtual(pBuilder: &sSubBuild, mPrereq, mUnusable);
163632 }else
163633#endif
163634 {
163635 rc = whereLoopAddBtree(pBuilder: &sSubBuild, mPrereq);
163636 }
163637 if( rc==SQLITE_OK ){
163638 rc = whereLoopAddOr(pBuilder: &sSubBuild, mPrereq, mUnusable);
163639 }
163640 testcase( rc==SQLITE_NOMEM && sCur.n>0 );
163641 testcase( rc==SQLITE_DONE );
163642 if( sCur.n==0 ){
163643 sSum.n = 0;
163644 break;
163645 }else if( once ){
163646 whereOrMove(pDest: &sSum, pSrc: &sCur);
163647 once = 0;
163648 }else{
163649 WhereOrSet sPrev;
163650 whereOrMove(pDest: &sPrev, pSrc: &sSum);
163651 sSum.n = 0;
163652 for(i=0; i<sPrev.n; i++){
163653 for(j=0; j<sCur.n; j++){
163654 whereOrInsert(pSet: &sSum, prereq: sPrev.a[i].prereq | sCur.a[j].prereq,
163655 rRun: sqlite3LogEstAdd(a: sPrev.a[i].rRun, b: sCur.a[j].rRun),
163656 nOut: sqlite3LogEstAdd(a: sPrev.a[i].nOut, b: sCur.a[j].nOut));
163657 }
163658 }
163659 }
163660 }
163661 pNew->nLTerm = 1;
163662 pNew->aLTerm[0] = pTerm;
163663 pNew->wsFlags = WHERE_MULTI_OR;
163664 pNew->rSetup = 0;
163665 pNew->iSortIdx = 0;
163666 memset(s: &pNew->u, c: 0, n: sizeof(pNew->u));
163667 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
163668 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
163669 ** of all sub-scans required by the OR-scan. However, due to rounding
163670 ** errors, it may be that the cost of the OR-scan is equal to its
163671 ** most expensive sub-scan. Add the smallest possible penalty
163672 ** (equivalent to multiplying the cost by 1.07) to ensure that
163673 ** this does not happen. Otherwise, for WHERE clauses such as the
163674 ** following where there is an index on "y":
163675 **
163676 ** WHERE likelihood(x=?, 0.99) OR y=?
163677 **
163678 ** the planner may elect to "OR" together a full-table scan and an
163679 ** index lookup. And other similarly odd results. */
163680 pNew->rRun = sSum.a[i].rRun + 1;
163681 pNew->nOut = sSum.a[i].nOut;
163682 pNew->prereq = sSum.a[i].prereq;
163683 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
163684 }
163685 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
163686 }
163687 }
163688 return rc;
163689}
163690
163691/*
163692** Add all WhereLoop objects for all tables
163693*/
163694static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
163695 WhereInfo *pWInfo = pBuilder->pWInfo;
163696 Bitmask mPrereq = 0;
163697 Bitmask mPrior = 0;
163698 int iTab;
163699 SrcList *pTabList = pWInfo->pTabList;
163700 SrcItem *pItem;
163701 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
163702 sqlite3 *db = pWInfo->pParse->db;
163703 int rc = SQLITE_OK;
163704 int bFirstPastRJ = 0;
163705 int hasRightJoin = 0;
163706 WhereLoop *pNew;
163707
163708
163709 /* Loop over the tables in the join, from left to right */
163710 pNew = pBuilder->pNew;
163711
163712 /* Verify that pNew has already been initialized */
163713 assert( pNew->nLTerm==0 );
163714 assert( pNew->wsFlags==0 );
163715 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
163716 assert( pNew->aLTerm!=0 );
163717
163718 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
163719 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
163720 Bitmask mUnusable = 0;
163721 pNew->iTab = iTab;
163722 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
163723 pNew->maskSelf = sqlite3WhereGetMask(pMaskSet: &pWInfo->sMaskSet, iCursor: pItem->iCursor);
163724 if( bFirstPastRJ
163725 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
163726 ){
163727 /* Add prerequisites to prevent reordering of FROM clause terms
163728 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
163729 ** prevents the right operand of a RIGHT JOIN from being swapped with
163730 ** other elements even further to the right.
163731 **
163732 ** The JT_LTORJ case and the hasRightJoin flag work together to
163733 ** prevent FROM-clause terms from moving from the right side of
163734 ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
163735 ** is itself on the left side of a RIGHT JOIN.
163736 */
163737 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
163738 mPrereq |= mPrior;
163739 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
163740 }else if( !hasRightJoin ){
163741 mPrereq = 0;
163742 }
163743#ifndef SQLITE_OMIT_VIRTUALTABLE
163744 if( IsVirtual(pItem->pTab) ){
163745 SrcItem *p;
163746 for(p=&pItem[1]; p<pEnd; p++){
163747 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
163748 mUnusable |= sqlite3WhereGetMask(pMaskSet: &pWInfo->sMaskSet, iCursor: p->iCursor);
163749 }
163750 }
163751 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
163752 }else
163753#endif /* SQLITE_OMIT_VIRTUALTABLE */
163754 {
163755 rc = whereLoopAddBtree(pBuilder, mPrereq);
163756 }
163757 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
163758 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
163759 }
163760 mPrior |= pNew->maskSelf;
163761 if( rc || db->mallocFailed ){
163762 if( rc==SQLITE_DONE ){
163763 /* We hit the query planner search limit set by iPlanLimit */
163764 sqlite3_log(SQLITE_WARNING, zFormat: "abbreviated query algorithm search");
163765 rc = SQLITE_OK;
163766 }else{
163767 break;
163768 }
163769 }
163770 }
163771
163772 whereLoopClear(db, p: pNew);
163773 return rc;
163774}
163775
163776/*
163777** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
163778** parameters) to see if it outputs rows in the requested ORDER BY
163779** (or GROUP BY) without requiring a separate sort operation. Return N:
163780**
163781** N>0: N terms of the ORDER BY clause are satisfied
163782** N==0: No terms of the ORDER BY clause are satisfied
163783** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
163784**
163785** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
163786** strict. With GROUP BY and DISTINCT the only requirement is that
163787** equivalent rows appear immediately adjacent to one another. GROUP BY
163788** and DISTINCT do not require rows to appear in any particular order as long
163789** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
163790** the pOrderBy terms can be matched in any order. With ORDER BY, the
163791** pOrderBy terms must be matched in strict left-to-right order.
163792*/
163793static i8 wherePathSatisfiesOrderBy(
163794 WhereInfo *pWInfo, /* The WHERE clause */
163795 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
163796 WherePath *pPath, /* The WherePath to check */
163797 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
163798 u16 nLoop, /* Number of entries in pPath->aLoop[] */
163799 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
163800 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
163801){
163802 u8 revSet; /* True if rev is known */
163803 u8 rev; /* Composite sort order */
163804 u8 revIdx; /* Index sort order */
163805 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
163806 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
163807 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
163808 u16 eqOpMask; /* Allowed equality operators */
163809 u16 nKeyCol; /* Number of key columns in pIndex */
163810 u16 nColumn; /* Total number of ordered columns in the index */
163811 u16 nOrderBy; /* Number terms in the ORDER BY clause */
163812 int iLoop; /* Index of WhereLoop in pPath being processed */
163813 int i, j; /* Loop counters */
163814 int iCur; /* Cursor number for current WhereLoop */
163815 int iColumn; /* A column number within table iCur */
163816 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
163817 WhereTerm *pTerm; /* A single term of the WHERE clause */
163818 Expr *pOBExpr; /* An expression from the ORDER BY clause */
163819 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
163820 Index *pIndex; /* The index associated with pLoop */
163821 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
163822 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
163823 Bitmask obDone; /* Mask of all ORDER BY terms */
163824 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
163825 Bitmask ready; /* Mask of inner loops */
163826
163827 /*
163828 ** We say the WhereLoop is "one-row" if it generates no more than one
163829 ** row of output. A WhereLoop is one-row if all of the following are true:
163830 ** (a) All index columns match with WHERE_COLUMN_EQ.
163831 ** (b) The index is unique
163832 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
163833 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
163834 **
163835 ** We say the WhereLoop is "order-distinct" if the set of columns from
163836 ** that WhereLoop that are in the ORDER BY clause are different for every
163837 ** row of the WhereLoop. Every one-row WhereLoop is automatically
163838 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
163839 ** is not order-distinct. To be order-distinct is not quite the same as being
163840 ** UNIQUE since a UNIQUE column or index can have multiple rows that
163841 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
163842 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
163843 **
163844 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
163845 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
163846 ** automatically order-distinct.
163847 */
163848
163849 assert( pOrderBy!=0 );
163850 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
163851
163852 nOrderBy = pOrderBy->nExpr;
163853 testcase( nOrderBy==BMS-1 );
163854 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
163855 isOrderDistinct = 1;
163856 obDone = MASKBIT(nOrderBy)-1;
163857 orderDistinctMask = 0;
163858 ready = 0;
163859 eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
163860 if( wctrlFlags & (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MAX|WHERE_ORDERBY_MIN) ){
163861 eqOpMask |= WO_IN;
163862 }
163863 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
163864 if( iLoop>0 ) ready |= pLoop->maskSelf;
163865 if( iLoop<nLoop ){
163866 pLoop = pPath->aLoop[iLoop];
163867 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
163868 }else{
163869 pLoop = pLast;
163870 }
163871 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
163872 if( pLoop->u.vtab.isOrdered
163873 && ((wctrlFlags&(WHERE_DISTINCTBY|WHERE_SORTBYGROUP))!=WHERE_DISTINCTBY)
163874 ){
163875 obSat = obDone;
163876 }
163877 break;
163878 }else if( wctrlFlags & WHERE_DISTINCTBY ){
163879 pLoop->u.btree.nDistinctCol = 0;
163880 }
163881 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
163882
163883 /* Mark off any ORDER BY term X that is a column in the table of
163884 ** the current loop for which there is term in the WHERE
163885 ** clause of the form X IS NULL or X=? that reference only outer
163886 ** loops.
163887 */
163888 for(i=0; i<nOrderBy; i++){
163889 if( MASKBIT(i) & obSat ) continue;
163890 pOBExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pOrderBy->a[i].pExpr);
163891 if( NEVER(pOBExpr==0) ) continue;
163892 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
163893 if( pOBExpr->iTable!=iCur ) continue;
163894 pTerm = sqlite3WhereFindTerm(pWC: &pWInfo->sWC, iCur, iColumn: pOBExpr->iColumn,
163895 notReady: ~ready, op: eqOpMask, pIdx: 0);
163896 if( pTerm==0 ) continue;
163897 if( pTerm->eOperator==WO_IN ){
163898 /* IN terms are only valid for sorting in the ORDER BY LIMIT
163899 ** optimization, and then only if they are actually used
163900 ** by the query plan */
163901 assert( wctrlFlags &
163902 (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) );
163903 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
163904 if( j>=pLoop->nLTerm ) continue;
163905 }
163906 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
163907 Parse *pParse = pWInfo->pParse;
163908 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pExpr: pOrderBy->a[i].pExpr);
163909 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, p: pTerm->pExpr);
163910 assert( pColl1 );
163911 if( pColl2==0 || sqlite3StrICmp(zLeft: pColl1->zName, zRight: pColl2->zName) ){
163912 continue;
163913 }
163914 testcase( pTerm->pExpr->op==TK_IS );
163915 }
163916 obSat |= MASKBIT(i);
163917 }
163918
163919 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
163920 if( pLoop->wsFlags & WHERE_IPK ){
163921 pIndex = 0;
163922 nKeyCol = 0;
163923 nColumn = 1;
163924 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
163925 return 0;
163926 }else{
163927 nKeyCol = pIndex->nKeyCol;
163928 nColumn = pIndex->nColumn;
163929 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
163930 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
163931 || !HasRowid(pIndex->pTable));
163932 /* All relevant terms of the index must also be non-NULL in order
163933 ** for isOrderDistinct to be true. So the isOrderDistint value
163934 ** computed here might be a false positive. Corrections will be
163935 ** made at tag-20210426-1 below */
163936 isOrderDistinct = IsUniqueIndex(pIndex)
163937 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
163938 }
163939
163940 /* Loop through all columns of the index and deal with the ones
163941 ** that are not constrained by == or IN.
163942 */
163943 rev = revSet = 0;
163944 distinctColumns = 0;
163945 for(j=0; j<nColumn; j++){
163946 u8 bOnce = 1; /* True to run the ORDER BY search loop */
163947
163948 assert( j>=pLoop->u.btree.nEq
163949 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
163950 );
163951 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
163952 u16 eOp = pLoop->aLTerm[j]->eOperator;
163953
163954 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
163955 ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL
163956 ** terms imply that the index is not UNIQUE NOT NULL in which case
163957 ** the loop need to be marked as not order-distinct because it can
163958 ** have repeated NULL rows.
163959 **
163960 ** If the current term is a column of an ((?,?) IN (SELECT...))
163961 ** expression for which the SELECT returns more than one column,
163962 ** check that it is the only column used by this loop. Otherwise,
163963 ** if it is one of two or more, none of the columns can be
163964 ** considered to match an ORDER BY term.
163965 */
163966 if( (eOp & eqOpMask)!=0 ){
163967 if( eOp & (WO_ISNULL|WO_IS) ){
163968 testcase( eOp & WO_ISNULL );
163969 testcase( eOp & WO_IS );
163970 testcase( isOrderDistinct );
163971 isOrderDistinct = 0;
163972 }
163973 continue;
163974 }else if( ALWAYS(eOp & WO_IN) ){
163975 /* ALWAYS() justification: eOp is an equality operator due to the
163976 ** j<pLoop->u.btree.nEq constraint above. Any equality other
163977 ** than WO_IN is captured by the previous "if". So this one
163978 ** always has to be WO_IN. */
163979 Expr *pX = pLoop->aLTerm[j]->pExpr;
163980 for(i=j+1; i<pLoop->u.btree.nEq; i++){
163981 if( pLoop->aLTerm[i]->pExpr==pX ){
163982 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
163983 bOnce = 0;
163984 break;
163985 }
163986 }
163987 }
163988 }
163989
163990 /* Get the column number in the table (iColumn) and sort order
163991 ** (revIdx) for the j-th column of the index.
163992 */
163993 if( pIndex ){
163994 iColumn = pIndex->aiColumn[j];
163995 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
163996 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
163997 }else{
163998 iColumn = XN_ROWID;
163999 revIdx = 0;
164000 }
164001
164002 /* An unconstrained column that might be NULL means that this
164003 ** WhereLoop is not well-ordered. tag-20210426-1
164004 */
164005 if( isOrderDistinct ){
164006 if( iColumn>=0
164007 && j>=pLoop->u.btree.nEq
164008 && pIndex->pTable->aCol[iColumn].notNull==0
164009 ){
164010 isOrderDistinct = 0;
164011 }
164012 if( iColumn==XN_EXPR ){
164013 isOrderDistinct = 0;
164014 }
164015 }
164016
164017 /* Find the ORDER BY term that corresponds to the j-th column
164018 ** of the index and mark that ORDER BY term off
164019 */
164020 isMatch = 0;
164021 for(i=0; bOnce && i<nOrderBy; i++){
164022 if( MASKBIT(i) & obSat ) continue;
164023 pOBExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pOrderBy->a[i].pExpr);
164024 testcase( wctrlFlags & WHERE_GROUPBY );
164025 testcase( wctrlFlags & WHERE_DISTINCTBY );
164026 if( NEVER(pOBExpr==0) ) continue;
164027 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
164028 if( iColumn>=XN_ROWID ){
164029 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
164030 if( pOBExpr->iTable!=iCur ) continue;
164031 if( pOBExpr->iColumn!=iColumn ) continue;
164032 }else{
164033 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
164034 if( sqlite3ExprCompareSkip(pA: pOBExpr, pB: pIxExpr, iTab: iCur) ){
164035 continue;
164036 }
164037 }
164038 if( iColumn!=XN_ROWID ){
164039 pColl = sqlite3ExprNNCollSeq(pParse: pWInfo->pParse, pExpr: pOrderBy->a[i].pExpr);
164040 if( sqlite3StrICmp(zLeft: pColl->zName, zRight: pIndex->azColl[j])!=0 ) continue;
164041 }
164042 if( wctrlFlags & WHERE_DISTINCTBY ){
164043 pLoop->u.btree.nDistinctCol = j+1;
164044 }
164045 isMatch = 1;
164046 break;
164047 }
164048 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
164049 /* Make sure the sort order is compatible in an ORDER BY clause.
164050 ** Sort order is irrelevant for a GROUP BY clause. */
164051 if( revSet ){
164052 if( (rev ^ revIdx)
164053 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
164054 ){
164055 isMatch = 0;
164056 }
164057 }else{
164058 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
164059 if( rev ) *pRevMask |= MASKBIT(iLoop);
164060 revSet = 1;
164061 }
164062 }
164063 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
164064 if( j==pLoop->u.btree.nEq ){
164065 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
164066 }else{
164067 isMatch = 0;
164068 }
164069 }
164070 if( isMatch ){
164071 if( iColumn==XN_ROWID ){
164072 testcase( distinctColumns==0 );
164073 distinctColumns = 1;
164074 }
164075 obSat |= MASKBIT(i);
164076 }else{
164077 /* No match found */
164078 if( j==0 || j<nKeyCol ){
164079 testcase( isOrderDistinct!=0 );
164080 isOrderDistinct = 0;
164081 }
164082 break;
164083 }
164084 } /* end Loop over all index columns */
164085 if( distinctColumns ){
164086 testcase( isOrderDistinct==0 );
164087 isOrderDistinct = 1;
164088 }
164089 } /* end-if not one-row */
164090
164091 /* Mark off any other ORDER BY terms that reference pLoop */
164092 if( isOrderDistinct ){
164093 orderDistinctMask |= pLoop->maskSelf;
164094 for(i=0; i<nOrderBy; i++){
164095 Expr *p;
164096 Bitmask mTerm;
164097 if( MASKBIT(i) & obSat ) continue;
164098 p = pOrderBy->a[i].pExpr;
164099 mTerm = sqlite3WhereExprUsage(pMaskSet: &pWInfo->sMaskSet,p);
164100 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
164101 if( (mTerm&~orderDistinctMask)==0 ){
164102 obSat |= MASKBIT(i);
164103 }
164104 }
164105 }
164106 } /* End the loop over all WhereLoops from outer-most down to inner-most */
164107 if( obSat==obDone ) return (i8)nOrderBy;
164108 if( !isOrderDistinct ){
164109 for(i=nOrderBy-1; i>0; i--){
164110 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
164111 if( (obSat&m)==m ) return i;
164112 }
164113 return 0;
164114 }
164115 return -1;
164116}
164117
164118
164119/*
164120** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
164121** the planner assumes that the specified pOrderBy list is actually a GROUP
164122** BY clause - and so any order that groups rows as required satisfies the
164123** request.
164124**
164125** Normally, in this case it is not possible for the caller to determine
164126** whether or not the rows are really being delivered in sorted order, or
164127** just in some other order that provides the required grouping. However,
164128** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
164129** this function may be called on the returned WhereInfo object. It returns
164130** true if the rows really will be sorted in the specified order, or false
164131** otherwise.
164132**
164133** For example, assuming:
164134**
164135** CREATE INDEX i1 ON t1(x, Y);
164136**
164137** then
164138**
164139** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
164140** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
164141*/
164142SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
164143 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
164144 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
164145 return pWInfo->sorted;
164146}
164147
164148#ifdef WHERETRACE_ENABLED
164149/* For debugging use only: */
164150static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
164151 static char zName[65];
164152 int i;
164153 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
164154 if( pLast ) zName[i++] = pLast->cId;
164155 zName[i] = 0;
164156 return zName;
164157}
164158#endif
164159
164160/*
164161** Return the cost of sorting nRow rows, assuming that the keys have
164162** nOrderby columns and that the first nSorted columns are already in
164163** order.
164164*/
164165static LogEst whereSortingCost(
164166 WhereInfo *pWInfo, /* Query planning context */
164167 LogEst nRow, /* Estimated number of rows to sort */
164168 int nOrderBy, /* Number of ORDER BY clause terms */
164169 int nSorted /* Number of initial ORDER BY terms naturally in order */
164170){
164171 /* Estimated cost of a full external sort, where N is
164172 ** the number of rows to sort is:
164173 **
164174 ** cost = (K * N * log(N)).
164175 **
164176 ** Or, if the order-by clause has X terms but only the last Y
164177 ** terms are out of order, then block-sorting will reduce the
164178 ** sorting cost to:
164179 **
164180 ** cost = (K * N * log(N)) * (Y/X)
164181 **
164182 ** The constant K is at least 2.0 but will be larger if there are a
164183 ** large number of columns to be sorted, as the sorting time is
164184 ** proportional to the amount of content to be sorted. The algorithm
164185 ** does not currently distinguish between fat columns (BLOBs and TEXTs)
164186 ** and skinny columns (INTs). It just uses the number of columns as
164187 ** an approximation for the row width.
164188 **
164189 ** And extra factor of 2.0 or 3.0 is added to the sorting cost if the sort
164190 ** is built using OP_IdxInsert and OP_Sort rather than with OP_SorterInsert.
164191 */
164192 LogEst rSortCost, nCol;
164193 assert( pWInfo->pSelect!=0 );
164194 assert( pWInfo->pSelect->pEList!=0 );
164195 /* TUNING: sorting cost proportional to the number of output columns: */
164196 nCol = sqlite3LogEst(x: (pWInfo->pSelect->pEList->nExpr+59)/30);
164197 rSortCost = nRow + nCol;
164198 if( nSorted>0 ){
164199 /* Scale the result by (Y/X) */
164200 rSortCost += sqlite3LogEst(x: (nOrderBy-nSorted)*100/nOrderBy) - 66;
164201 }
164202
164203 /* Multiple by log(M) where M is the number of output rows.
164204 ** Use the LIMIT for M if it is smaller. Or if this sort is for
164205 ** a DISTINCT operator, M will be the number of distinct output
164206 ** rows, so fudge it downwards a bit.
164207 */
164208 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
164209 rSortCost += 10; /* TUNING: Extra 2.0x if using LIMIT */
164210 if( nSorted!=0 ){
164211 rSortCost += 6; /* TUNING: Extra 1.5x if also using partial sort */
164212 }
164213 if( pWInfo->iLimit<nRow ){
164214 nRow = pWInfo->iLimit;
164215 }
164216 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
164217 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
164218 ** reduces the number of output rows by a factor of 2 */
164219 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
164220 }
164221 rSortCost += estLog(N: nRow);
164222 return rSortCost;
164223}
164224
164225/*
164226** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
164227** attempts to find the lowest cost path that visits each WhereLoop
164228** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
164229**
164230** Assume that the total number of output rows that will need to be sorted
164231** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
164232** costs if nRowEst==0.
164233**
164234** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
164235** error occurs.
164236*/
164237static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
164238 int mxChoice; /* Maximum number of simultaneous paths tracked */
164239 int nLoop; /* Number of terms in the join */
164240 Parse *pParse; /* Parsing context */
164241 int iLoop; /* Loop counter over the terms of the join */
164242 int ii, jj; /* Loop counters */
164243 int mxI = 0; /* Index of next entry to replace */
164244 int nOrderBy; /* Number of ORDER BY clause terms */
164245 LogEst mxCost = 0; /* Maximum cost of a set of paths */
164246 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
164247 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
164248 WherePath *aFrom; /* All nFrom paths at the previous level */
164249 WherePath *aTo; /* The nTo best paths at the current level */
164250 WherePath *pFrom; /* An element of aFrom[] that we are working on */
164251 WherePath *pTo; /* An element of aTo[] that we are working on */
164252 WhereLoop *pWLoop; /* One of the WhereLoop objects */
164253 WhereLoop **pX; /* Used to divy up the pSpace memory */
164254 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
164255 char *pSpace; /* Temporary memory used by this routine */
164256 int nSpace; /* Bytes of space allocated at pSpace */
164257
164258 pParse = pWInfo->pParse;
164259 nLoop = pWInfo->nLevel;
164260 /* TUNING: For simple queries, only the best path is tracked.
164261 ** For 2-way joins, the 5 best paths are followed.
164262 ** For joins of 3 or more tables, track the 10 best paths */
164263 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
164264 assert( nLoop<=pWInfo->pTabList->nSrc );
164265 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n",
164266 nRowEst, pParse->nQueryLoop));
164267
164268 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
164269 ** case the purpose of this call is to estimate the number of rows returned
164270 ** by the overall query. Once this estimate has been obtained, the caller
164271 ** will invoke this function a second time, passing the estimate as the
164272 ** nRowEst parameter. */
164273 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
164274 nOrderBy = 0;
164275 }else{
164276 nOrderBy = pWInfo->pOrderBy->nExpr;
164277 }
164278
164279 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
164280 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
164281 nSpace += sizeof(LogEst) * nOrderBy;
164282 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
164283 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
164284 aTo = (WherePath*)pSpace;
164285 aFrom = aTo+mxChoice;
164286 memset(s: aFrom, c: 0, n: sizeof(aFrom[0]));
164287 pX = (WhereLoop**)(aFrom+mxChoice);
164288 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
164289 pFrom->aLoop = pX;
164290 }
164291 if( nOrderBy ){
164292 /* If there is an ORDER BY clause and it is not being ignored, set up
164293 ** space for the aSortCost[] array. Each element of the aSortCost array
164294 ** is either zero - meaning it has not yet been initialized - or the
164295 ** cost of sorting nRowEst rows of data where the first X terms of
164296 ** the ORDER BY clause are already in order, where X is the array
164297 ** index. */
164298 aSortCost = (LogEst*)pX;
164299 memset(s: aSortCost, c: 0, n: sizeof(LogEst) * nOrderBy);
164300 }
164301 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
164302 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
164303
164304 /* Seed the search with a single WherePath containing zero WhereLoops.
164305 **
164306 ** TUNING: Do not let the number of iterations go above 28. If the cost
164307 ** of computing an automatic index is not paid back within the first 28
164308 ** rows, then do not use the automatic index. */
164309 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
164310 nFrom = 1;
164311 assert( aFrom[0].isOrdered==0 );
164312 if( nOrderBy ){
164313 /* If nLoop is zero, then there are no FROM terms in the query. Since
164314 ** in this case the query may return a maximum of one row, the results
164315 ** are already in the requested order. Set isOrdered to nOrderBy to
164316 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
164317 ** -1, indicating that the result set may or may not be ordered,
164318 ** depending on the loops added to the current plan. */
164319 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
164320 }
164321
164322 /* Compute successively longer WherePaths using the previous generation
164323 ** of WherePaths as the basis for the next. Keep track of the mxChoice
164324 ** best paths at each generation */
164325 for(iLoop=0; iLoop<nLoop; iLoop++){
164326 nTo = 0;
164327 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
164328 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
164329 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
164330 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
164331 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
164332 i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */
164333 Bitmask maskNew; /* Mask of src visited by (..) */
164334 Bitmask revMask; /* Mask of rev-order loops for (..) */
164335
164336 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
164337 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
164338 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
164339 /* Do not use an automatic index if the this loop is expected
164340 ** to run less than 1.25 times. It is tempting to also exclude
164341 ** automatic index usage on an outer loop, but sometimes an automatic
164342 ** index is useful in the outer loop of a correlated subquery. */
164343 assert( 10==sqlite3LogEst(2) );
164344 continue;
164345 }
164346
164347 /* At this point, pWLoop is a candidate to be the next loop.
164348 ** Compute its cost */
164349 rUnsorted = sqlite3LogEstAdd(a: pWLoop->rSetup,b: pWLoop->rRun + pFrom->nRow);
164350 rUnsorted = sqlite3LogEstAdd(a: rUnsorted, b: pFrom->rUnsorted);
164351 nOut = pFrom->nRow + pWLoop->nOut;
164352 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
164353 isOrdered = pFrom->isOrdered;
164354 if( isOrdered<0 ){
164355 revMask = 0;
164356 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
164357 pOrderBy: pWInfo->pOrderBy, pPath: pFrom, wctrlFlags: pWInfo->wctrlFlags,
164358 nLoop: iLoop, pLast: pWLoop, pRevMask: &revMask);
164359 }else{
164360 revMask = pFrom->revLoop;
164361 }
164362 if( isOrdered>=0 && isOrdered<nOrderBy ){
164363 if( aSortCost[isOrdered]==0 ){
164364 aSortCost[isOrdered] = whereSortingCost(
164365 pWInfo, nRow: nRowEst, nOrderBy, nSorted: isOrdered
164366 );
164367 }
164368 /* TUNING: Add a small extra penalty (3) to sorting as an
164369 ** extra encouragement to the query planner to select a plan
164370 ** where the rows emerge in the correct order without any sorting
164371 ** required. */
164372 rCost = sqlite3LogEstAdd(a: rUnsorted, b: aSortCost[isOrdered]) + 3;
164373
164374 WHERETRACE(0x002,
164375 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
164376 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
164377 rUnsorted, rCost));
164378 }else{
164379 rCost = rUnsorted;
164380 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
164381 }
164382
164383 /* TUNING: A full-scan of a VIEW or subquery in the outer loop
164384 ** is not so bad. */
164385 if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 && nLoop>1 ){
164386 rCost += -10;
164387 nOut += -30;
164388 WHERETRACE(0x80,("VIEWSCAN cost reduction for %c\n",pWLoop->cId));
164389 }
164390
164391 /* Check to see if pWLoop should be added to the set of
164392 ** mxChoice best-so-far paths.
164393 **
164394 ** First look for an existing path among best-so-far paths
164395 ** that covers the same set of loops and has the same isOrdered
164396 ** setting as the current path candidate.
164397 **
164398 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
164399 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
164400 ** of legal values for isOrdered, -1..64.
164401 */
164402 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
164403 if( pTo->maskLoop==maskNew
164404 && ((pTo->isOrdered^isOrdered)&0x80)==0
164405 ){
164406 testcase( jj==nTo-1 );
164407 break;
164408 }
164409 }
164410 if( jj>=nTo ){
164411 /* None of the existing best-so-far paths match the candidate. */
164412 if( nTo>=mxChoice
164413 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
164414 ){
164415 /* The current candidate is no better than any of the mxChoice
164416 ** paths currently in the best-so-far buffer. So discard
164417 ** this candidate as not viable. */
164418#ifdef WHERETRACE_ENABLED /* 0x4 */
164419 if( sqlite3WhereTrace&0x4 ){
164420 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
164421 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
164422 isOrdered>=0 ? isOrdered+'0' : '?');
164423 }
164424#endif
164425 continue;
164426 }
164427 /* If we reach this points it means that the new candidate path
164428 ** needs to be added to the set of best-so-far paths. */
164429 if( nTo<mxChoice ){
164430 /* Increase the size of the aTo set by one */
164431 jj = nTo++;
164432 }else{
164433 /* New path replaces the prior worst to keep count below mxChoice */
164434 jj = mxI;
164435 }
164436 pTo = &aTo[jj];
164437#ifdef WHERETRACE_ENABLED /* 0x4 */
164438 if( sqlite3WhereTrace&0x4 ){
164439 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
164440 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
164441 isOrdered>=0 ? isOrdered+'0' : '?');
164442 }
164443#endif
164444 }else{
164445 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
164446 ** same set of loops and has the same isOrdered setting as the
164447 ** candidate path. Check to see if the candidate should replace
164448 ** pTo or if the candidate should be skipped.
164449 **
164450 ** The conditional is an expanded vector comparison equivalent to:
164451 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
164452 */
164453 if( pTo->rCost<rCost
164454 || (pTo->rCost==rCost
164455 && (pTo->nRow<nOut
164456 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
164457 )
164458 )
164459 ){
164460#ifdef WHERETRACE_ENABLED /* 0x4 */
164461 if( sqlite3WhereTrace&0x4 ){
164462 sqlite3DebugPrintf(
164463 "Skip %s cost=%-3d,%3d,%3d order=%c",
164464 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
164465 isOrdered>=0 ? isOrdered+'0' : '?');
164466 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
164467 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
164468 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
164469 }
164470#endif
164471 /* Discard the candidate path from further consideration */
164472 testcase( pTo->rCost==rCost );
164473 continue;
164474 }
164475 testcase( pTo->rCost==rCost+1 );
164476 /* Control reaches here if the candidate path is better than the
164477 ** pTo path. Replace pTo with the candidate. */
164478#ifdef WHERETRACE_ENABLED /* 0x4 */
164479 if( sqlite3WhereTrace&0x4 ){
164480 sqlite3DebugPrintf(
164481 "Update %s cost=%-3d,%3d,%3d order=%c",
164482 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
164483 isOrdered>=0 ? isOrdered+'0' : '?');
164484 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
164485 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
164486 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
164487 }
164488#endif
164489 }
164490 /* pWLoop is a winner. Add it to the set of best so far */
164491 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
164492 pTo->revLoop = revMask;
164493 pTo->nRow = nOut;
164494 pTo->rCost = rCost;
164495 pTo->rUnsorted = rUnsorted;
164496 pTo->isOrdered = isOrdered;
164497 memcpy(dest: pTo->aLoop, src: pFrom->aLoop, n: sizeof(WhereLoop*)*iLoop);
164498 pTo->aLoop[iLoop] = pWLoop;
164499 if( nTo>=mxChoice ){
164500 mxI = 0;
164501 mxCost = aTo[0].rCost;
164502 mxUnsorted = aTo[0].nRow;
164503 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
164504 if( pTo->rCost>mxCost
164505 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
164506 ){
164507 mxCost = pTo->rCost;
164508 mxUnsorted = pTo->rUnsorted;
164509 mxI = jj;
164510 }
164511 }
164512 }
164513 }
164514 }
164515
164516#ifdef WHERETRACE_ENABLED /* >=2 */
164517 if( sqlite3WhereTrace & 0x02 ){
164518 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
164519 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
164520 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
164521 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
164522 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
164523 if( pTo->isOrdered>0 ){
164524 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
164525 }else{
164526 sqlite3DebugPrintf("\n");
164527 }
164528 }
164529 }
164530#endif
164531
164532 /* Swap the roles of aFrom and aTo for the next generation */
164533 pFrom = aTo;
164534 aTo = aFrom;
164535 aFrom = pFrom;
164536 nFrom = nTo;
164537 }
164538
164539 if( nFrom==0 ){
164540 sqlite3ErrorMsg(pParse, zFormat: "no query solution");
164541 sqlite3StackFreeNN(pParse->db, pSpace);
164542 return SQLITE_ERROR;
164543 }
164544
164545 /* Find the lowest cost path. pFrom will be left pointing to that path */
164546 pFrom = aFrom;
164547 for(ii=1; ii<nFrom; ii++){
164548 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
164549 }
164550 assert( pWInfo->nLevel==nLoop );
164551 /* Load the lowest cost path into pWInfo */
164552 for(iLoop=0; iLoop<nLoop; iLoop++){
164553 WhereLevel *pLevel = pWInfo->a + iLoop;
164554 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
164555 pLevel->iFrom = pWLoop->iTab;
164556 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
164557 }
164558 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
164559 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
164560 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
164561 && nRowEst
164562 ){
164563 Bitmask notUsed;
164564 int rc = wherePathSatisfiesOrderBy(pWInfo, pOrderBy: pWInfo->pResultSet, pPath: pFrom,
164565 WHERE_DISTINCTBY, nLoop: nLoop-1, pLast: pFrom->aLoop[nLoop-1], pRevMask: &notUsed);
164566 if( rc==pWInfo->pResultSet->nExpr ){
164567 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
164568 }
164569 }
164570 pWInfo->bOrderedInnerLoop = 0;
164571 if( pWInfo->pOrderBy ){
164572 pWInfo->nOBSat = pFrom->isOrdered;
164573 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
164574 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
164575 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
164576 }
164577 if( pWInfo->pSelect->pOrderBy
164578 && pWInfo->nOBSat > pWInfo->pSelect->pOrderBy->nExpr ){
164579 pWInfo->nOBSat = pWInfo->pSelect->pOrderBy->nExpr;
164580 }
164581 }else{
164582 pWInfo->revMask = pFrom->revLoop;
164583 if( pWInfo->nOBSat<=0 ){
164584 pWInfo->nOBSat = 0;
164585 if( nLoop>0 ){
164586 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
164587 if( (wsFlags & WHERE_ONEROW)==0
164588 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
164589 ){
164590 Bitmask m = 0;
164591 int rc = wherePathSatisfiesOrderBy(pWInfo, pOrderBy: pWInfo->pOrderBy, pPath: pFrom,
164592 WHERE_ORDERBY_LIMIT, nLoop: nLoop-1, pLast: pFrom->aLoop[nLoop-1], pRevMask: &m);
164593 testcase( wsFlags & WHERE_IPK );
164594 testcase( wsFlags & WHERE_COLUMN_IN );
164595 if( rc==pWInfo->pOrderBy->nExpr ){
164596 pWInfo->bOrderedInnerLoop = 1;
164597 pWInfo->revMask = m;
164598 }
164599 }
164600 }
164601 }else if( nLoop
164602 && pWInfo->nOBSat==1
164603 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
164604 ){
164605 pWInfo->bOrderedInnerLoop = 1;
164606 }
164607 }
164608 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
164609 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
164610 ){
164611 Bitmask revMask = 0;
164612 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pOrderBy: pWInfo->pOrderBy,
164613 pPath: pFrom, wctrlFlags: 0, nLoop: nLoop-1, pLast: pFrom->aLoop[nLoop-1], pRevMask: &revMask
164614 );
164615 assert( pWInfo->sorted==0 );
164616 if( nOrder==pWInfo->pOrderBy->nExpr ){
164617 pWInfo->sorted = 1;
164618 pWInfo->revMask = revMask;
164619 }
164620 }
164621 }
164622
164623
164624 pWInfo->nRowOut = pFrom->nRow;
164625
164626 /* Free temporary memory and return success */
164627 sqlite3StackFreeNN(pParse->db, pSpace);
164628 return SQLITE_OK;
164629}
164630
164631/*
164632** Most queries use only a single table (they are not joins) and have
164633** simple == constraints against indexed fields. This routine attempts
164634** to plan those simple cases using much less ceremony than the
164635** general-purpose query planner, and thereby yield faster sqlite3_prepare()
164636** times for the common case.
164637**
164638** Return non-zero on success, if this query can be handled by this
164639** no-frills query planner. Return zero if this query needs the
164640** general-purpose query planner.
164641*/
164642static int whereShortCut(WhereLoopBuilder *pBuilder){
164643 WhereInfo *pWInfo;
164644 SrcItem *pItem;
164645 WhereClause *pWC;
164646 WhereTerm *pTerm;
164647 WhereLoop *pLoop;
164648 int iCur;
164649 int j;
164650 Table *pTab;
164651 Index *pIdx;
164652 WhereScan scan;
164653
164654 pWInfo = pBuilder->pWInfo;
164655 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
164656 assert( pWInfo->pTabList->nSrc>=1 );
164657 pItem = pWInfo->pTabList->a;
164658 pTab = pItem->pTab;
164659 if( IsVirtual(pTab) ) return 0;
164660 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
164661 testcase( pItem->fg.isIndexedBy );
164662 testcase( pItem->fg.notIndexed );
164663 return 0;
164664 }
164665 iCur = pItem->iCursor;
164666 pWC = &pWInfo->sWC;
164667 pLoop = pBuilder->pNew;
164668 pLoop->wsFlags = 0;
164669 pLoop->nSkip = 0;
164670 pTerm = whereScanInit(pScan: &scan, pWC, iCur, iColumn: -1, WO_EQ|WO_IS, pIdx: 0);
164671 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(pScan: &scan);
164672 if( pTerm ){
164673 testcase( pTerm->eOperator & WO_IS );
164674 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
164675 pLoop->aLTerm[0] = pTerm;
164676 pLoop->nLTerm = 1;
164677 pLoop->u.btree.nEq = 1;
164678 /* TUNING: Cost of a rowid lookup is 10 */
164679 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
164680 }else{
164681 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
164682 int opMask;
164683 assert( pLoop->aLTermSpace==pLoop->aLTerm );
164684 if( !IsUniqueIndex(pIdx)
164685 || pIdx->pPartIdxWhere!=0
164686 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
164687 ) continue;
164688 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
164689 for(j=0; j<pIdx->nKeyCol; j++){
164690 pTerm = whereScanInit(pScan: &scan, pWC, iCur, iColumn: j, opMask, pIdx);
164691 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(pScan: &scan);
164692 if( pTerm==0 ) break;
164693 testcase( pTerm->eOperator & WO_IS );
164694 pLoop->aLTerm[j] = pTerm;
164695 }
164696 if( j!=pIdx->nKeyCol ) continue;
164697 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
164698 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
164699 pLoop->wsFlags |= WHERE_IDX_ONLY;
164700 }
164701 pLoop->nLTerm = j;
164702 pLoop->u.btree.nEq = j;
164703 pLoop->u.btree.pIndex = pIdx;
164704 /* TUNING: Cost of a unique index lookup is 15 */
164705 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
164706 break;
164707 }
164708 }
164709 if( pLoop->wsFlags ){
164710 pLoop->nOut = (LogEst)1;
164711 pWInfo->a[0].pWLoop = pLoop;
164712 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
164713 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
164714 pWInfo->a[0].iTabCur = iCur;
164715 pWInfo->nRowOut = 1;
164716 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
164717 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
164718 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
164719 }
164720 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
164721#ifdef SQLITE_DEBUG
164722 pLoop->cId = '0';
164723#endif
164724#ifdef WHERETRACE_ENABLED
164725 if( sqlite3WhereTrace & 0x02 ){
164726 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
164727 }
164728#endif
164729 return 1;
164730 }
164731 return 0;
164732}
164733
164734/*
164735** Helper function for exprIsDeterministic().
164736*/
164737static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
164738 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
164739 pWalker->eCode = 0;
164740 return WRC_Abort;
164741 }
164742 return WRC_Continue;
164743}
164744
164745/*
164746** Return true if the expression contains no non-deterministic SQL
164747** functions. Do not consider non-deterministic SQL functions that are
164748** part of sub-select statements.
164749*/
164750static int exprIsDeterministic(Expr *p){
164751 Walker w;
164752 memset(s: &w, c: 0, n: sizeof(w));
164753 w.eCode = 1;
164754 w.xExprCallback = exprNodeIsDeterministic;
164755 w.xSelectCallback = sqlite3SelectWalkFail;
164756 sqlite3WalkExpr(pWalker: &w, pExpr: p);
164757 return w.eCode;
164758}
164759
164760
164761#ifdef WHERETRACE_ENABLED
164762/*
164763** Display all WhereLoops in pWInfo
164764*/
164765static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
164766 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
164767 WhereLoop *p;
164768 int i;
164769 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
164770 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
164771 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
164772 p->cId = zLabel[i%(sizeof(zLabel)-1)];
164773 sqlite3WhereLoopPrint(p, pWC);
164774 }
164775 }
164776}
164777# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
164778#else
164779# define WHERETRACE_ALL_LOOPS(W,C)
164780#endif
164781
164782/* Attempt to omit tables from a join that do not affect the result.
164783** For a table to not affect the result, the following must be true:
164784**
164785** 1) The query must not be an aggregate.
164786** 2) The table must be the RHS of a LEFT JOIN.
164787** 3) Either the query must be DISTINCT, or else the ON or USING clause
164788** must contain a constraint that limits the scan of the table to
164789** at most a single row.
164790** 4) The table must not be referenced by any part of the query apart
164791** from its own USING or ON clause.
164792** 5) The table must not have an inner-join ON or USING clause if there is
164793** a RIGHT JOIN anywhere in the query. Otherwise the ON/USING clause
164794** might move from the right side to the left side of the RIGHT JOIN.
164795** Note: Due to (2), this condition can only arise if the table is
164796** the right-most table of a subquery that was flattened into the
164797** main query and that subquery was the right-hand operand of an
164798** inner join that held an ON or USING clause.
164799**
164800** For example, given:
164801**
164802** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
164803** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
164804** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
164805**
164806** then table t2 can be omitted from the following:
164807**
164808** SELECT v1, v3 FROM t1
164809** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
164810** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
164811**
164812** or from:
164813**
164814** SELECT DISTINCT v1, v3 FROM t1
164815** LEFT JOIN t2
164816** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
164817*/
164818static SQLITE_NOINLINE Bitmask whereOmitNoopJoin(
164819 WhereInfo *pWInfo,
164820 Bitmask notReady
164821){
164822 int i;
164823 Bitmask tabUsed;
164824 int hasRightJoin;
164825
164826 /* Preconditions checked by the caller */
164827 assert( pWInfo->nLevel>=2 );
164828 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
164829
164830 /* These two preconditions checked by the caller combine to guarantee
164831 ** condition (1) of the header comment */
164832 assert( pWInfo->pResultSet!=0 );
164833 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
164834
164835 tabUsed = sqlite3WhereExprListUsage(pMaskSet: &pWInfo->sMaskSet, pList: pWInfo->pResultSet);
164836 if( pWInfo->pOrderBy ){
164837 tabUsed |= sqlite3WhereExprListUsage(pMaskSet: &pWInfo->sMaskSet, pList: pWInfo->pOrderBy);
164838 }
164839 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0;
164840 for(i=pWInfo->nLevel-1; i>=1; i--){
164841 WhereTerm *pTerm, *pEnd;
164842 SrcItem *pItem;
164843 WhereLoop *pLoop;
164844 pLoop = pWInfo->a[i].pWLoop;
164845 pItem = &pWInfo->pTabList->a[pLoop->iTab];
164846 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
164847 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
164848 && (pLoop->wsFlags & WHERE_ONEROW)==0
164849 ){
164850 continue;
164851 }
164852 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
164853 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
164854 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
164855 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
164856 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
164857 || pTerm->pExpr->w.iJoin!=pItem->iCursor
164858 ){
164859 break;
164860 }
164861 }
164862 if( hasRightJoin
164863 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
164864 && pTerm->pExpr->w.iJoin==pItem->iCursor
164865 ){
164866 break; /* restriction (5) */
164867 }
164868 }
164869 if( pTerm<pEnd ) continue;
164870 WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
164871 notReady &= ~pLoop->maskSelf;
164872 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
164873 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
164874 pTerm->wtFlags |= TERM_CODED;
164875 }
164876 }
164877 if( i!=pWInfo->nLevel-1 ){
164878 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
164879 memmove(dest: &pWInfo->a[i], src: &pWInfo->a[i+1], n: nByte);
164880 }
164881 pWInfo->nLevel--;
164882 assert( pWInfo->nLevel>0 );
164883 }
164884 return notReady;
164885}
164886
164887/*
164888** Check to see if there are any SEARCH loops that might benefit from
164889** using a Bloom filter. Consider a Bloom filter if:
164890**
164891** (1) The SEARCH happens more than N times where N is the number
164892** of rows in the table that is being considered for the Bloom
164893** filter.
164894** (2) Some searches are expected to find zero rows. (This is determined
164895** by the WHERE_SELFCULL flag on the term.)
164896** (3) Bloom-filter processing is not disabled. (Checked by the
164897** caller.)
164898** (4) The size of the table being searched is known by ANALYZE.
164899**
164900** This block of code merely checks to see if a Bloom filter would be
164901** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the
164902** WhereLoop. The implementation of the Bloom filter comes further
164903** down where the code for each WhereLoop is generated.
164904*/
164905static SQLITE_NOINLINE void whereCheckIfBloomFilterIsUseful(
164906 const WhereInfo *pWInfo
164907){
164908 int i;
164909 LogEst nSearch = 0;
164910
164911 assert( pWInfo->nLevel>=2 );
164912 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
164913 for(i=0; i<pWInfo->nLevel; i++){
164914 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
164915 const unsigned int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ);
164916 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
164917 Table *pTab = pItem->pTab;
164918 if( (pTab->tabFlags & TF_HasStat1)==0 ) break;
164919 pTab->tabFlags |= TF_StatsUsed;
164920 if( i>=1
164921 && (pLoop->wsFlags & reqFlags)==reqFlags
164922 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
164923 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
164924 ){
164925 if( nSearch > pTab->nRowLogEst ){
164926 testcase( pItem->fg.jointype & JT_LEFT );
164927 pLoop->wsFlags |= WHERE_BLOOMFILTER;
164928 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
164929 WHERETRACE(0xffffffff, (
164930 "-> use Bloom-filter on loop %c because there are ~%.1e "
164931 "lookups into %s which has only ~%.1e rows\n",
164932 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
164933 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
164934 }
164935 }
164936 nSearch += pLoop->nOut;
164937 }
164938}
164939
164940/*
164941** This is an sqlite3ParserAddCleanup() callback that is invoked to
164942** free the Parse->pIdxEpr list when the Parse object is destroyed.
164943*/
164944static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
164945 Parse *pParse = (Parse*)pObject;
164946 while( pParse->pIdxEpr!=0 ){
164947 IndexedExpr *p = pParse->pIdxEpr;
164948 pParse->pIdxEpr = p->pIENext;
164949 sqlite3ExprDelete(db, p: p->pExpr);
164950 sqlite3DbFreeNN(db, p);
164951 }
164952}
164953
164954/*
164955** The index pIdx is used by a query and contains one or more expressions.
164956** In other words pIdx is an index on an expression. iIdxCur is the cursor
164957** number for the index and iDataCur is the cursor number for the corresponding
164958** table.
164959**
164960** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
164961** each of the expressions in the index so that the expression code generator
164962** will know to replace occurrences of the indexed expression with
164963** references to the corresponding column of the index.
164964*/
164965static SQLITE_NOINLINE void whereAddIndexedExpr(
164966 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
164967 Index *pIdx, /* The index-on-expression that contains the expressions */
164968 int iIdxCur, /* Cursor number for pIdx */
164969 SrcItem *pTabItem /* The FROM clause entry for the table */
164970){
164971 int i;
164972 IndexedExpr *p;
164973 Table *pTab;
164974 assert( pIdx->bHasExpr );
164975 pTab = pIdx->pTable;
164976 for(i=0; i<pIdx->nColumn; i++){
164977 Expr *pExpr;
164978 int j = pIdx->aiColumn[i];
164979 int bMaybeNullRow;
164980 if( j==XN_EXPR ){
164981 pExpr = pIdx->aColExpr->a[i].pExpr;
164982 testcase( pTabItem->fg.jointype & JT_LEFT );
164983 testcase( pTabItem->fg.jointype & JT_RIGHT );
164984 testcase( pTabItem->fg.jointype & JT_LTORJ );
164985 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
164986 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
164987 pExpr = sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[j]);
164988 bMaybeNullRow = 0;
164989 }else{
164990 continue;
164991 }
164992 if( sqlite3ExprIsConstant(p: pExpr) ) continue;
164993 p = sqlite3DbMallocRaw(db: pParse->db, n: sizeof(IndexedExpr));
164994 if( p==0 ) break;
164995 p->pIENext = pParse->pIdxEpr;
164996#ifdef WHERETRACE_ENABLED
164997 if( sqlite3WhereTrace & 0x200 ){
164998 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
164999 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(pExpr);
165000 }
165001#endif
165002 p->pExpr = sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0);
165003 p->iDataCur = pTabItem->iCursor;
165004 p->iIdxCur = iIdxCur;
165005 p->iIdxCol = i;
165006 p->bMaybeNullRow = bMaybeNullRow;
165007 if( sqlite3IndexAffinityStr(db: pParse->db, pIdx) ){
165008 p->aff = pIdx->zColAff[i];
165009 }
165010#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
165011 p->zIdxName = pIdx->zName;
165012#endif
165013 pParse->pIdxEpr = p;
165014 if( p->pIENext==0 ){
165015 sqlite3ParserAddCleanup(pParse, xCleanup: whereIndexedExprCleanup, pPtr: pParse);
165016 }
165017 }
165018}
165019
165020/*
165021** Set the reverse-scan order mask to one for all tables in the query
165022** with the exception of MATERIALIZED common table expressions that have
165023** their own internal ORDER BY clauses.
165024**
165025** This implements the PRAGMA reverse_unordered_selects=ON setting.
165026** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER).
165027*/
165028static SQLITE_NOINLINE void whereReverseScanOrder(WhereInfo *pWInfo){
165029 int ii;
165030 for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
165031 SrcItem *pItem = &pWInfo->pTabList->a[ii];
165032 if( !pItem->fg.isCte
165033 || pItem->u2.pCteUse->eM10d!=M10d_Yes
165034 || NEVER(pItem->pSelect==0)
165035 || pItem->pSelect->pOrderBy==0
165036 ){
165037 pWInfo->revMask |= MASKBIT(ii);
165038 }
165039 }
165040}
165041
165042/*
165043** Generate the beginning of the loop used for WHERE clause processing.
165044** The return value is a pointer to an opaque structure that contains
165045** information needed to terminate the loop. Later, the calling routine
165046** should invoke sqlite3WhereEnd() with the return value of this function
165047** in order to complete the WHERE clause processing.
165048**
165049** If an error occurs, this routine returns NULL.
165050**
165051** The basic idea is to do a nested loop, one loop for each table in
165052** the FROM clause of a select. (INSERT and UPDATE statements are the
165053** same as a SELECT with only a single table in the FROM clause.) For
165054** example, if the SQL is this:
165055**
165056** SELECT * FROM t1, t2, t3 WHERE ...;
165057**
165058** Then the code generated is conceptually like the following:
165059**
165060** foreach row1 in t1 do \ Code generated
165061** foreach row2 in t2 do |-- by sqlite3WhereBegin()
165062** foreach row3 in t3 do /
165063** ...
165064** end \ Code generated
165065** end |-- by sqlite3WhereEnd()
165066** end /
165067**
165068** Note that the loops might not be nested in the order in which they
165069** appear in the FROM clause if a different order is better able to make
165070** use of indices. Note also that when the IN operator appears in
165071** the WHERE clause, it might result in additional nested loops for
165072** scanning through all values on the right-hand side of the IN.
165073**
165074** There are Btree cursors associated with each table. t1 uses cursor
165075** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
165076** And so forth. This routine generates code to open those VDBE cursors
165077** and sqlite3WhereEnd() generates the code to close them.
165078**
165079** The code that sqlite3WhereBegin() generates leaves the cursors named
165080** in pTabList pointing at their appropriate entries. The [...] code
165081** can use OP_Column and OP_Rowid opcodes on these cursors to extract
165082** data from the various tables of the loop.
165083**
165084** If the WHERE clause is empty, the foreach loops must each scan their
165085** entire tables. Thus a three-way join is an O(N^3) operation. But if
165086** the tables have indices and there are terms in the WHERE clause that
165087** refer to those indices, a complete table scan can be avoided and the
165088** code will run much faster. Most of the work of this routine is checking
165089** to see if there are indices that can be used to speed up the loop.
165090**
165091** Terms of the WHERE clause are also used to limit which rows actually
165092** make it to the "..." in the middle of the loop. After each "foreach",
165093** terms of the WHERE clause that use only terms in that loop and outer
165094** loops are evaluated and if false a jump is made around all subsequent
165095** inner loops (or around the "..." if the test occurs within the inner-
165096** most loop)
165097**
165098** OUTER JOINS
165099**
165100** An outer join of tables t1 and t2 is conceptually coded as follows:
165101**
165102** foreach row1 in t1 do
165103** flag = 0
165104** foreach row2 in t2 do
165105** start:
165106** ...
165107** flag = 1
165108** end
165109** if flag==0 then
165110** move the row2 cursor to a null row
165111** goto start
165112** fi
165113** end
165114**
165115** ORDER BY CLAUSE PROCESSING
165116**
165117** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
165118** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
165119** if there is one. If there is no ORDER BY clause or if this routine
165120** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
165121**
165122** The iIdxCur parameter is the cursor number of an index. If
165123** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
165124** to use for OR clause processing. The WHERE clause should use this
165125** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
165126** the first cursor in an array of cursors for all indices. iIdxCur should
165127** be used to compute the appropriate cursor depending on which index is
165128** used.
165129*/
165130SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
165131 Parse *pParse, /* The parser context */
165132 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
165133 Expr *pWhere, /* The WHERE clause */
165134 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
165135 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
165136 Select *pSelect, /* The entire SELECT statement */
165137 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
165138 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
165139 ** If WHERE_USE_LIMIT, then the limit amount */
165140){
165141 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
165142 int nTabList; /* Number of elements in pTabList */
165143 WhereInfo *pWInfo; /* Will become the return value of this function */
165144 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
165145 Bitmask notReady; /* Cursors that are not yet positioned */
165146 WhereLoopBuilder sWLB; /* The WhereLoop builder */
165147 WhereMaskSet *pMaskSet; /* The expression mask set */
165148 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
165149 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
165150 int ii; /* Loop counter */
165151 sqlite3 *db; /* Database connection */
165152 int rc; /* Return code */
165153 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
165154
165155 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
165156 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
165157 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
165158 ));
165159
165160 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
165161 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
165162 || (wctrlFlags & WHERE_USE_LIMIT)==0 );
165163
165164 /* Variable initialization */
165165 db = pParse->db;
165166 memset(s: &sWLB, c: 0, n: sizeof(sWLB));
165167
165168 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
165169 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
165170 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
165171
165172 /* The number of tables in the FROM clause is limited by the number of
165173 ** bits in a Bitmask
165174 */
165175 testcase( pTabList->nSrc==BMS );
165176 if( pTabList->nSrc>BMS ){
165177 sqlite3ErrorMsg(pParse, zFormat: "at most %d tables in a join", BMS);
165178 return 0;
165179 }
165180
165181 /* This function normally generates a nested loop for all tables in
165182 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
165183 ** only generate code for the first table in pTabList and assume that
165184 ** any cursors associated with subsequent tables are uninitialized.
165185 */
165186 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
165187
165188 /* Allocate and initialize the WhereInfo structure that will become the
165189 ** return value. A single allocation is used to store the WhereInfo
165190 ** struct, the contents of WhereInfo.a[], the WhereClause structure
165191 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
165192 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
165193 ** some architectures. Hence the ROUND8() below.
165194 */
165195 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
165196 pWInfo = sqlite3DbMallocRawNN(db, n: nByteWInfo + sizeof(WhereLoop));
165197 if( db->mallocFailed ){
165198 sqlite3DbFree(db, p: pWInfo);
165199 pWInfo = 0;
165200 goto whereBeginError;
165201 }
165202 pWInfo->pParse = pParse;
165203 pWInfo->pTabList = pTabList;
165204 pWInfo->pOrderBy = pOrderBy;
165205#if WHERETRACE_ENABLED
165206 pWInfo->pWhere = pWhere;
165207#endif
165208 pWInfo->pResultSet = pResultSet;
165209 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
165210 pWInfo->nLevel = nTabList;
165211 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
165212 pWInfo->wctrlFlags = wctrlFlags;
165213 pWInfo->iLimit = iAuxArg;
165214 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
165215 pWInfo->pSelect = pSelect;
165216 memset(s: &pWInfo->nOBSat, c: 0,
165217 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
165218 memset(s: &pWInfo->a[0], c: 0, n: sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
165219 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
165220 pMaskSet = &pWInfo->sMaskSet;
165221 pMaskSet->n = 0;
165222 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
165223 ** a valid cursor number, to avoid an initial
165224 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
165225 sWLB.pWInfo = pWInfo;
165226 sWLB.pWC = &pWInfo->sWC;
165227 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
165228 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
165229 whereLoopInit(p: sWLB.pNew);
165230#ifdef SQLITE_DEBUG
165231 sWLB.pNew->cId = '*';
165232#endif
165233
165234 /* Split the WHERE clause into separate subexpressions where each
165235 ** subexpression is separated by an AND operator.
165236 */
165237 sqlite3WhereClauseInit(pWC: &pWInfo->sWC, pWInfo);
165238 sqlite3WhereSplit(pWC: &pWInfo->sWC, pExpr: pWhere, TK_AND);
165239
165240 /* Special case: No FROM clause
165241 */
165242 if( nTabList==0 ){
165243 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
165244 if( (wctrlFlags & WHERE_WANT_DISTINCT)!=0
165245 && OptimizationEnabled(db, SQLITE_DistinctOpt)
165246 ){
165247 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
165248 }
165249 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
165250 }else{
165251 /* Assign a bit from the bitmask to every term in the FROM clause.
165252 **
165253 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
165254 **
165255 ** The rule of the previous sentence ensures that if X is the bitmask for
165256 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
165257 ** Knowing the bitmask for all tables to the left of a left join is
165258 ** important. Ticket #3015.
165259 **
165260 ** Note that bitmasks are created for all pTabList->nSrc tables in
165261 ** pTabList, not just the first nTabList tables. nTabList is normally
165262 ** equal to pTabList->nSrc but might be shortened to 1 if the
165263 ** WHERE_OR_SUBCLAUSE flag is set.
165264 */
165265 ii = 0;
165266 do{
165267 createMask(pMaskSet, iCursor: pTabList->a[ii].iCursor);
165268 sqlite3WhereTabFuncArgs(pParse, pItem: &pTabList->a[ii], pWC: &pWInfo->sWC);
165269 }while( (++ii)<pTabList->nSrc );
165270 #ifdef SQLITE_DEBUG
165271 {
165272 Bitmask mx = 0;
165273 for(ii=0; ii<pTabList->nSrc; ii++){
165274 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
165275 assert( m>=mx );
165276 mx = m;
165277 }
165278 }
165279 #endif
165280 }
165281
165282 /* Analyze all of the subexpressions. */
165283 sqlite3WhereExprAnalyze(pTabList, pWC: &pWInfo->sWC);
165284 if( pSelect && pSelect->pLimit ){
165285 sqlite3WhereAddLimit(pWC: &pWInfo->sWC, p: pSelect);
165286 }
165287 if( pParse->nErr ) goto whereBeginError;
165288
165289 /* The False-WHERE-Term-Bypass optimization:
165290 **
165291 ** If there are WHERE terms that are false, then no rows will be output,
165292 ** so skip over all of the code generated here.
165293 **
165294 ** Conditions:
165295 **
165296 ** (1) The WHERE term must not refer to any tables in the join.
165297 ** (2) The term must not come from an ON clause on the
165298 ** right-hand side of a LEFT or FULL JOIN.
165299 ** (3) The term must not come from an ON clause, or there must be
165300 ** no RIGHT or FULL OUTER joins in pTabList.
165301 ** (4) If the expression contains non-deterministic functions
165302 ** that are not within a sub-select. This is not required
165303 ** for correctness but rather to preserves SQLite's legacy
165304 ** behaviour in the following two cases:
165305 **
165306 ** WHERE random()>0; -- eval random() once per row
165307 ** WHERE (SELECT random())>0; -- eval random() just once overall
165308 **
165309 ** Note that the Where term need not be a constant in order for this
165310 ** optimization to apply, though it does need to be constant relative to
165311 ** the current subquery (condition 1). The term might include variables
165312 ** from outer queries so that the value of the term changes from one
165313 ** invocation of the current subquery to the next.
165314 */
165315 for(ii=0; ii<sWLB.pWC->nBase; ii++){
165316 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
165317 Expr *pX; /* The expression of pT */
165318 if( pT->wtFlags & TERM_VIRTUAL ) continue;
165319 pX = pT->pExpr;
165320 assert( pX!=0 );
165321 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) );
165322 if( pT->prereqAll==0 /* Conditions (1) and (2) */
165323 && (nTabList==0 || exprIsDeterministic(p: pX)) /* Condition (4) */
165324 && !(ExprHasProperty(pX, EP_InnerON) /* Condition (3) */
165325 && (pTabList->a[0].fg.jointype & JT_LTORJ)!=0 )
165326 ){
165327 sqlite3ExprIfFalse(pParse, pExpr: pX, dest: pWInfo->iBreak, SQLITE_JUMPIFNULL);
165328 pT->wtFlags |= TERM_CODED;
165329 }
165330 }
165331
165332 if( wctrlFlags & WHERE_WANT_DISTINCT ){
165333 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
165334 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
165335 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
165336 wctrlFlags &= ~WHERE_WANT_DISTINCT;
165337 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
165338 }else if( isDistinctRedundant(pParse, pTabList, pWC: &pWInfo->sWC, pDistinct: pResultSet) ){
165339 /* The DISTINCT marking is pointless. Ignore it. */
165340 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
165341 }else if( pOrderBy==0 ){
165342 /* Try to ORDER BY the result set to make distinct processing easier */
165343 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
165344 pWInfo->pOrderBy = pResultSet;
165345 }
165346 }
165347
165348 /* Construct the WhereLoop objects */
165349#if defined(WHERETRACE_ENABLED)
165350 if( sqlite3WhereTrace & 0xffffffff ){
165351 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
165352 if( wctrlFlags & WHERE_USE_LIMIT ){
165353 sqlite3DebugPrintf(", limit: %d", iAuxArg);
165354 }
165355 sqlite3DebugPrintf(")\n");
165356 if( sqlite3WhereTrace & 0x8000 ){
165357 Select sSelect;
165358 memset(&sSelect, 0, sizeof(sSelect));
165359 sSelect.selFlags = SF_WhereBegin;
165360 sSelect.pSrc = pTabList;
165361 sSelect.pWhere = pWhere;
165362 sSelect.pOrderBy = pOrderBy;
165363 sSelect.pEList = pResultSet;
165364 sqlite3TreeViewSelect(0, &sSelect, 0);
165365 }
165366 if( sqlite3WhereTrace & 0x4000 ){ /* Display all WHERE clause terms */
165367 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
165368 sqlite3WhereClausePrint(sWLB.pWC);
165369 }
165370 }
165371#endif
165372
165373 if( nTabList!=1 || whereShortCut(pBuilder: &sWLB)==0 ){
165374 rc = whereLoopAddAll(pBuilder: &sWLB);
165375 if( rc ) goto whereBeginError;
165376
165377#ifdef SQLITE_ENABLE_STAT4
165378 /* If one or more WhereTerm.truthProb values were used in estimating
165379 ** loop parameters, but then those truthProb values were subsequently
165380 ** changed based on STAT4 information while computing subsequent loops,
165381 ** then we need to rerun the whole loop building process so that all
165382 ** loops will be built using the revised truthProb values. */
165383 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
165384 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
165385 WHERETRACE(0xffffffff,
165386 ("**** Redo all loop computations due to"
165387 " TERM_HIGHTRUTH changes ****\n"));
165388 while( pWInfo->pLoops ){
165389 WhereLoop *p = pWInfo->pLoops;
165390 pWInfo->pLoops = p->pNextLoop;
165391 whereLoopDelete(db, p);
165392 }
165393 rc = whereLoopAddAll(&sWLB);
165394 if( rc ) goto whereBeginError;
165395 }
165396#endif
165397 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
165398
165399 wherePathSolver(pWInfo, nRowEst: 0);
165400 if( db->mallocFailed ) goto whereBeginError;
165401 if( pWInfo->pOrderBy ){
165402 wherePathSolver(pWInfo, nRowEst: pWInfo->nRowOut+1);
165403 if( db->mallocFailed ) goto whereBeginError;
165404 }
165405 }
165406 assert( pWInfo->pTabList!=0 );
165407 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
165408 whereReverseScanOrder(pWInfo);
165409 }
165410 if( pParse->nErr ){
165411 goto whereBeginError;
165412 }
165413 assert( db->mallocFailed==0 );
165414#ifdef WHERETRACE_ENABLED
165415 if( sqlite3WhereTrace ){
165416 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
165417 if( pWInfo->nOBSat>0 ){
165418 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
165419 }
165420 switch( pWInfo->eDistinct ){
165421 case WHERE_DISTINCT_UNIQUE: {
165422 sqlite3DebugPrintf(" DISTINCT=unique");
165423 break;
165424 }
165425 case WHERE_DISTINCT_ORDERED: {
165426 sqlite3DebugPrintf(" DISTINCT=ordered");
165427 break;
165428 }
165429 case WHERE_DISTINCT_UNORDERED: {
165430 sqlite3DebugPrintf(" DISTINCT=unordered");
165431 break;
165432 }
165433 }
165434 sqlite3DebugPrintf("\n");
165435 for(ii=0; ii<pWInfo->nLevel; ii++){
165436 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
165437 }
165438 }
165439#endif
165440
165441 /* Attempt to omit tables from a join that do not affect the result.
165442 ** See the comment on whereOmitNoopJoin() for further information.
165443 **
165444 ** This query optimization is factored out into a separate "no-inline"
165445 ** procedure to keep the sqlite3WhereBegin() procedure from becoming
165446 ** too large. If sqlite3WhereBegin() becomes too large, that prevents
165447 ** some C-compiler optimizers from in-lining the
165448 ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to
165449 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
165450 */
165451 notReady = ~(Bitmask)0;
165452 if( pWInfo->nLevel>=2
165453 && pResultSet!=0 /* these two combine to guarantee */
165454 && 0==(wctrlFlags & WHERE_AGG_DISTINCT) /* condition (1) above */
165455 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
165456 ){
165457 notReady = whereOmitNoopJoin(pWInfo, notReady);
165458 nTabList = pWInfo->nLevel;
165459 assert( nTabList>0 );
165460 }
165461
165462 /* Check to see if there are any SEARCH loops that might benefit from
165463 ** using a Bloom filter.
165464 */
165465 if( pWInfo->nLevel>=2
165466 && OptimizationEnabled(db, SQLITE_BloomFilter)
165467 ){
165468 whereCheckIfBloomFilterIsUseful(pWInfo);
165469 }
165470
165471#if defined(WHERETRACE_ENABLED)
165472 if( sqlite3WhereTrace & 0x4000 ){ /* Display all terms of the WHERE clause */
165473 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
165474 sqlite3WhereClausePrint(sWLB.pWC);
165475 }
165476 WHERETRACE(0xffffffff,("*** Optimizer Finished ***\n"));
165477#endif
165478 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
165479
165480 /* If the caller is an UPDATE or DELETE statement that is requesting
165481 ** to use a one-pass algorithm, determine if this is appropriate.
165482 **
165483 ** A one-pass approach can be used if the caller has requested one
165484 ** and either (a) the scan visits at most one row or (b) each
165485 ** of the following are true:
165486 **
165487 ** * the caller has indicated that a one-pass approach can be used
165488 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
165489 ** * the table is not a virtual table, and
165490 ** * either the scan does not use the OR optimization or the caller
165491 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
165492 ** for DELETE).
165493 **
165494 ** The last qualification is because an UPDATE statement uses
165495 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
165496 ** use a one-pass approach, and this is not set accurately for scans
165497 ** that use the OR optimization.
165498 */
165499 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
165500 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
165501 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
165502 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
165503 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
165504 if( bOnerow || (
165505 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
165506 && !IsVirtual(pTabList->a[0].pTab)
165507 && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
165508 && OptimizationEnabled(db, SQLITE_OnePass)
165509 )){
165510 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
165511 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
165512 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
165513 bFordelete = OPFLAG_FORDELETE;
165514 }
165515 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
165516 }
165517 }
165518 }
165519
165520 /* Open all tables in the pTabList and any indices selected for
165521 ** searching those tables.
165522 */
165523 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
165524 Table *pTab; /* Table to open */
165525 int iDb; /* Index of database containing table/index */
165526 SrcItem *pTabItem;
165527
165528 pTabItem = &pTabList->a[pLevel->iFrom];
165529 pTab = pTabItem->pTab;
165530 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
165531 pLoop = pLevel->pWLoop;
165532 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
165533 /* Do nothing */
165534 }else
165535#ifndef SQLITE_OMIT_VIRTUALTABLE
165536 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
165537 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
165538 int iCur = pTabItem->iCursor;
165539 sqlite3VdbeAddOp4(p: v, OP_VOpen, p1: iCur, p2: 0, p3: 0, zP4: pVTab, P4_VTAB);
165540 }else if( IsVirtual(pTab) ){
165541 /* noop */
165542 }else
165543#endif
165544 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
165545 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0)
165546 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
165547 ){
165548 int op = OP_OpenRead;
165549 if( pWInfo->eOnePass!=ONEPASS_OFF ){
165550 op = OP_OpenWrite;
165551 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
165552 };
165553 sqlite3OpenTable(pParse, iCur: pTabItem->iCursor, iDb, pTab, opcode: op);
165554 assert( pTabItem->iCursor==pLevel->iTabCur );
165555 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
165556 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
165557 if( pWInfo->eOnePass==ONEPASS_OFF
165558 && pTab->nCol<BMS
165559 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
165560 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
165561 ){
165562 /* If we know that only a prefix of the record will be used,
165563 ** it is advantageous to reduce the "column count" field in
165564 ** the P4 operand of the OP_OpenRead/Write opcode. */
165565 Bitmask b = pTabItem->colUsed;
165566 int n = 0;
165567 for(; b; b=b>>1, n++){}
165568 sqlite3VdbeChangeP4(p: v, addr: -1, SQLITE_INT_TO_PTR(n), P4_INT32);
165569 assert( n<=pTab->nCol );
165570 }
165571#ifdef SQLITE_ENABLE_CURSOR_HINTS
165572 if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid)==0 ){
165573 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
165574 }else
165575#endif
165576 {
165577 sqlite3VdbeChangeP5(p: v, p5: bFordelete);
165578 }
165579#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
165580 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
165581 (const u8*)&pTabItem->colUsed, P4_INT64);
165582#endif
165583 }else{
165584 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 0, zName: pTab->zName);
165585 }
165586 if( pLoop->wsFlags & WHERE_INDEXED ){
165587 Index *pIx = pLoop->u.btree.pIndex;
165588 int iIndexCur;
165589 int op = OP_OpenRead;
165590 /* iAuxArg is always set to a positive value if ONEPASS is possible */
165591 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
165592 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
165593 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
165594 ){
165595 /* This is one term of an OR-optimization using the PRIMARY KEY of a
165596 ** WITHOUT ROWID table. No need for a separate index */
165597 iIndexCur = pLevel->iTabCur;
165598 op = 0;
165599 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
165600 Index *pJ = pTabItem->pTab->pIndex;
165601 iIndexCur = iAuxArg;
165602 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
165603 while( ALWAYS(pJ) && pJ!=pIx ){
165604 iIndexCur++;
165605 pJ = pJ->pNext;
165606 }
165607 op = OP_OpenWrite;
165608 pWInfo->aiCurOnePass[1] = iIndexCur;
165609 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
165610 iIndexCur = iAuxArg;
165611 op = OP_ReopenIdx;
165612 }else{
165613 iIndexCur = pParse->nTab++;
165614 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
165615 whereAddIndexedExpr(pParse, pIdx: pIx, iIdxCur: iIndexCur, pTabItem);
165616 }
165617 }
165618 pLevel->iIdxCur = iIndexCur;
165619 assert( pIx!=0 );
165620 assert( pIx->pSchema==pTab->pSchema );
165621 assert( iIndexCur>=0 );
165622 if( op ){
165623 sqlite3VdbeAddOp3(p: v, op, p1: iIndexCur, p2: pIx->tnum, p3: iDb);
165624 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pIx);
165625 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
165626 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
165627 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
165628 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
165629 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
165630 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
165631 ){
165632 sqlite3VdbeChangeP5(p: v, OPFLAG_SEEKEQ);
165633 }
165634 VdbeComment((v, "%s", pIx->zName));
165635#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
165636 {
165637 u64 colUsed = 0;
165638 int ii, jj;
165639 for(ii=0; ii<pIx->nColumn; ii++){
165640 jj = pIx->aiColumn[ii];
165641 if( jj<0 ) continue;
165642 if( jj>63 ) jj = 63;
165643 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
165644 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
165645 }
165646 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
165647 (u8*)&colUsed, P4_INT64);
165648 }
165649#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
165650 }
165651 }
165652 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
165653 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
165654 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, nByte: sizeof(WhereRightJoin)))!=0
165655 ){
165656 WhereRightJoin *pRJ = pLevel->pRJ;
165657 pRJ->iMatch = pParse->nTab++;
165658 pRJ->regBloom = ++pParse->nMem;
165659 sqlite3VdbeAddOp2(p: v, OP_Blob, p1: 65536, p2: pRJ->regBloom);
165660 pRJ->regReturn = ++pParse->nMem;
165661 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pRJ->regReturn);
165662 assert( pTab==pTabItem->pTab );
165663 if( HasRowid(pTab) ){
165664 KeyInfo *pInfo;
165665 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pRJ->iMatch, p2: 1);
165666 pInfo = sqlite3KeyInfoAlloc(db: pParse->db, N: 1, X: 0);
165667 if( pInfo ){
165668 pInfo->aColl[0] = 0;
165669 pInfo->aSortFlags[0] = 0;
165670 sqlite3VdbeAppendP4(p: v, pP4: pInfo, P4_KEYINFO);
165671 }
165672 }else{
165673 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
165674 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pRJ->iMatch, p2: pPk->nKeyCol);
165675 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
165676 }
165677 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
165678 /* The nature of RIGHT JOIN processing is such that it messes up
165679 ** the output order. So omit any ORDER BY/GROUP BY elimination
165680 ** optimizations. We need to do an actual sort for RIGHT JOIN. */
165681 pWInfo->nOBSat = 0;
165682 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
165683 }
165684 }
165685 pWInfo->iTop = sqlite3VdbeCurrentAddr(p: v);
165686 if( db->mallocFailed ) goto whereBeginError;
165687
165688 /* Generate the code to do the search. Each iteration of the for
165689 ** loop below generates code for a single nested loop of the VM
165690 ** program.
165691 */
165692 for(ii=0; ii<nTabList; ii++){
165693 int addrExplain;
165694 int wsFlags;
165695 SrcItem *pSrc;
165696 if( pParse->nErr ) goto whereBeginError;
165697 pLevel = &pWInfo->a[ii];
165698 wsFlags = pLevel->pWLoop->wsFlags;
165699 pSrc = &pTabList->a[pLevel->iFrom];
165700 if( pSrc->fg.isMaterialized ){
165701 if( pSrc->fg.isCorrelated ){
165702 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pSrc->regReturn, p2: pSrc->addrFillSub);
165703 }else{
165704 int iOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
165705 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pSrc->regReturn, p2: pSrc->addrFillSub);
165706 sqlite3VdbeJumpHere(p: v, addr: iOnce);
165707 }
165708 }
165709 assert( pTabList == pWInfo->pTabList );
165710 if( (wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))!=0 ){
165711 if( (wsFlags & WHERE_AUTO_INDEX)!=0 ){
165712#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
165713 constructAutomaticIndex(pParse, pWC: &pWInfo->sWC, notReady, pLevel);
165714#endif
165715 }else{
165716 sqlite3ConstructBloomFilter(pWInfo, iLevel: ii, pLevel, notReady);
165717 }
165718 if( db->mallocFailed ) goto whereBeginError;
165719 }
165720 addrExplain = sqlite3WhereExplainOneScan(
165721 pParse, pTabList, pLevel, wctrlFlags
165722 );
165723 pLevel->addrBody = sqlite3VdbeCurrentAddr(p: v);
165724 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,iLevel: ii,pLevel,notReady);
165725 pWInfo->iContinue = pLevel->addrCont;
165726 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
165727 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
165728 }
165729 }
165730
165731 /* Done. */
165732 VdbeModuleComment((v, "Begin WHERE-core"));
165733 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(p: v);
165734 return pWInfo;
165735
165736 /* Jump here if malloc fails */
165737whereBeginError:
165738 if( pWInfo ){
165739 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
165740 whereInfoFree(db, pWInfo);
165741 }
165742 return 0;
165743}
165744
165745/*
165746** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
165747** index rather than the main table. In SQLITE_DEBUG mode, we want
165748** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
165749** does that.
165750*/
165751#ifndef SQLITE_DEBUG
165752# define OpcodeRewriteTrace(D,K,P) /* no-op */
165753#else
165754# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
165755 static void sqlite3WhereOpcodeRewriteTrace(
165756 sqlite3 *db,
165757 int pc,
165758 VdbeOp *pOp
165759 ){
165760 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
165761 sqlite3VdbePrintOp(0, pc, pOp);
165762 }
165763#endif
165764
165765#ifdef SQLITE_DEBUG
165766/*
165767** Return true if cursor iCur is opened by instruction k of the
165768** bytecode. Used inside of assert() only.
165769*/
165770static int cursorIsOpen(Vdbe *v, int iCur, int k){
165771 while( k>=0 ){
165772 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
165773 if( pOp->p1!=iCur ) continue;
165774 if( pOp->opcode==OP_Close ) return 0;
165775 if( pOp->opcode==OP_OpenRead ) return 1;
165776 if( pOp->opcode==OP_OpenWrite ) return 1;
165777 if( pOp->opcode==OP_OpenDup ) return 1;
165778 if( pOp->opcode==OP_OpenAutoindex ) return 1;
165779 if( pOp->opcode==OP_OpenEphemeral ) return 1;
165780 }
165781 return 0;
165782}
165783#endif /* SQLITE_DEBUG */
165784
165785/*
165786** Generate the end of the WHERE loop. See comments on
165787** sqlite3WhereBegin() for additional information.
165788*/
165789SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
165790 Parse *pParse = pWInfo->pParse;
165791 Vdbe *v = pParse->pVdbe;
165792 int i;
165793 WhereLevel *pLevel;
165794 WhereLoop *pLoop;
165795 SrcList *pTabList = pWInfo->pTabList;
165796 sqlite3 *db = pParse->db;
165797 int iEnd = sqlite3VdbeCurrentAddr(p: v);
165798 int nRJ = 0;
165799
165800 /* Generate loop termination code.
165801 */
165802 VdbeModuleComment((v, "End WHERE-core"));
165803 for(i=pWInfo->nLevel-1; i>=0; i--){
165804 int addr;
165805 pLevel = &pWInfo->a[i];
165806 if( pLevel->pRJ ){
165807 /* Terminate the subroutine that forms the interior of the loop of
165808 ** the RIGHT JOIN table */
165809 WhereRightJoin *pRJ = pLevel->pRJ;
165810 sqlite3VdbeResolveLabel(v, x: pLevel->addrCont);
165811 pLevel->addrCont = 0;
165812 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(p: v);
165813 sqlite3VdbeAddOp3(p: v, OP_Return, p1: pRJ->regReturn, p2: pRJ->addrSubrtn, p3: 1);
165814 VdbeCoverage(v);
165815 nRJ++;
165816 }
165817 pLoop = pLevel->pWLoop;
165818 if( pLevel->op!=OP_Noop ){
165819#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
165820 int addrSeek = 0;
165821 Index *pIdx;
165822 int n;
165823 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
165824 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
165825 && (pLoop->wsFlags & WHERE_INDEXED)!=0
165826 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
165827 && (n = pLoop->u.btree.nDistinctCol)>0
165828 && pIdx->aiRowLogEst[n]>=36
165829 ){
165830 int r1 = pParse->nMem+1;
165831 int j, op;
165832 for(j=0; j<n; j++){
165833 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pLevel->iIdxCur, p2: j, p3: r1+j);
165834 }
165835 pParse->nMem += n+1;
165836 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
165837 addrSeek = sqlite3VdbeAddOp4Int(p: v, op, p1: pLevel->iIdxCur, p2: 0, p3: r1, p4: n);
165838 VdbeCoverageIf(v, op==OP_SeekLT);
165839 VdbeCoverageIf(v, op==OP_SeekGT);
165840 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 1, p2: pLevel->p2);
165841 }
165842#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
165843 /* The common case: Advance to the next row */
165844 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, x: pLevel->addrCont);
165845 sqlite3VdbeAddOp3(p: v, op: pLevel->op, p1: pLevel->p1, p2: pLevel->p2, p3: pLevel->p3);
165846 sqlite3VdbeChangeP5(p: v, p5: pLevel->p5);
165847 VdbeCoverage(v);
165848 VdbeCoverageIf(v, pLevel->op==OP_Next);
165849 VdbeCoverageIf(v, pLevel->op==OP_Prev);
165850 VdbeCoverageIf(v, pLevel->op==OP_VNext);
165851 if( pLevel->regBignull ){
165852 sqlite3VdbeResolveLabel(v, x: pLevel->addrBignull);
165853 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: pLevel->regBignull, p2: pLevel->p2-1);
165854 VdbeCoverage(v);
165855 }
165856#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
165857 if( addrSeek ) sqlite3VdbeJumpHere(p: v, addr: addrSeek);
165858#endif
165859 }else if( pLevel->addrCont ){
165860 sqlite3VdbeResolveLabel(v, x: pLevel->addrCont);
165861 }
165862 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
165863 struct InLoop *pIn;
165864 int j;
165865 sqlite3VdbeResolveLabel(v, x: pLevel->addrNxt);
165866 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
165867 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
165868 || pParse->db->mallocFailed );
165869 sqlite3VdbeJumpHere(p: v, addr: pIn->addrInTop+1);
165870 if( pIn->eEndLoopOp!=OP_Noop ){
165871 if( pIn->nPrefix ){
165872 int bEarlyOut =
165873 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
165874 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
165875 if( pLevel->iLeftJoin ){
165876 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
165877 ** opened yet. This occurs for WHERE clauses such as
165878 ** "a = ? AND b IN (...)", where the index is on (a, b). If
165879 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
165880 ** never have been coded, but the body of the loop run to
165881 ** return the null-row. So, if the cursor is not open yet,
165882 ** jump over the OP_Next or OP_Prev instruction about to
165883 ** be coded. */
165884 sqlite3VdbeAddOp2(p: v, OP_IfNotOpen, p1: pIn->iCur,
165885 p2: sqlite3VdbeCurrentAddr(p: v) + 2 + bEarlyOut);
165886 VdbeCoverage(v);
165887 }
165888 if( bEarlyOut ){
165889 sqlite3VdbeAddOp4Int(p: v, OP_IfNoHope, p1: pLevel->iIdxCur,
165890 p2: sqlite3VdbeCurrentAddr(p: v)+2,
165891 p3: pIn->iBase, p4: pIn->nPrefix);
165892 VdbeCoverage(v);
165893 /* Retarget the OP_IsNull against the left operand of IN so
165894 ** it jumps past the OP_IfNoHope. This is because the
165895 ** OP_IsNull also bypasses the OP_Affinity opcode that is
165896 ** required by OP_IfNoHope. */
165897 sqlite3VdbeJumpHere(p: v, addr: pIn->addrInTop+1);
165898 }
165899 }
165900 sqlite3VdbeAddOp2(p: v, op: pIn->eEndLoopOp, p1: pIn->iCur, p2: pIn->addrInTop);
165901 VdbeCoverage(v);
165902 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
165903 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
165904 }
165905 sqlite3VdbeJumpHere(p: v, addr: pIn->addrInTop-1);
165906 }
165907 }
165908 sqlite3VdbeResolveLabel(v, x: pLevel->addrBrk);
165909 if( pLevel->pRJ ){
165910 sqlite3VdbeAddOp3(p: v, OP_Return, p1: pLevel->pRJ->regReturn, p2: 0, p3: 1);
165911 VdbeCoverage(v);
165912 }
165913 if( pLevel->addrSkip ){
165914 sqlite3VdbeGoto(p: v, iDest: pLevel->addrSkip);
165915 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
165916 sqlite3VdbeJumpHere(p: v, addr: pLevel->addrSkip);
165917 sqlite3VdbeJumpHere(p: v, addr: pLevel->addrSkip-2);
165918 }
165919#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
165920 if( pLevel->addrLikeRep ){
165921 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: (int)(pLevel->iLikeRepCntr>>1),
165922 p2: pLevel->addrLikeRep);
165923 VdbeCoverage(v);
165924 }
165925#endif
165926 if( pLevel->iLeftJoin ){
165927 int ws = pLoop->wsFlags;
165928 addr = sqlite3VdbeAddOp1(p: v, OP_IfPos, p1: pLevel->iLeftJoin); VdbeCoverage(v);
165929 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
165930 if( (ws & WHERE_IDX_ONLY)==0 ){
165931 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
165932 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: pLevel->iTabCur);
165933 }
165934 if( (ws & WHERE_INDEXED)
165935 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
165936 ){
165937 if( ws & WHERE_MULTI_OR ){
165938 Index *pIx = pLevel->u.pCoveringIdx;
165939 int iDb = sqlite3SchemaToIndex(db, pSchema: pIx->pSchema);
165940 sqlite3VdbeAddOp3(p: v, OP_ReopenIdx, p1: pLevel->iIdxCur, p2: pIx->tnum, p3: iDb);
165941 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pIx);
165942 }
165943 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: pLevel->iIdxCur);
165944 }
165945 if( pLevel->op==OP_Return ){
165946 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pLevel->p1, p2: pLevel->addrFirst);
165947 }else{
165948 sqlite3VdbeGoto(p: v, iDest: pLevel->addrFirst);
165949 }
165950 sqlite3VdbeJumpHere(p: v, addr);
165951 }
165952 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
165953 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
165954 }
165955
165956 assert( pWInfo->nLevel<=pTabList->nSrc );
165957 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
165958 int k, last;
165959 VdbeOp *pOp, *pLastOp;
165960 Index *pIdx = 0;
165961 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
165962 Table *pTab = pTabItem->pTab;
165963 assert( pTab!=0 );
165964 pLoop = pLevel->pWLoop;
165965
165966 /* Do RIGHT JOIN processing. Generate code that will output the
165967 ** unmatched rows of the right operand of the RIGHT JOIN with
165968 ** all of the columns of the left operand set to NULL.
165969 */
165970 if( pLevel->pRJ ){
165971 sqlite3WhereRightJoinLoop(pWInfo, iLevel: i, pLevel);
165972 continue;
165973 }
165974
165975 /* For a co-routine, change all OP_Column references to the table of
165976 ** the co-routine into OP_Copy of result contained in a register.
165977 ** OP_Rowid becomes OP_Null.
165978 */
165979 if( pTabItem->fg.viaCoroutine ){
165980 testcase( pParse->db->mallocFailed );
165981 translateColumnToCopy(pParse, iStart: pLevel->addrBody, iTabCur: pLevel->iTabCur,
165982 iRegister: pTabItem->regResult, iAutoidxCur: 0);
165983 continue;
165984 }
165985
165986 /* If this scan uses an index, make VDBE code substitutions to read data
165987 ** from the index instead of from the table where possible. In some cases
165988 ** this optimization prevents the table from ever being read, which can
165989 ** yield a significant performance boost.
165990 **
165991 ** Calls to the code generator in between sqlite3WhereBegin and
165992 ** sqlite3WhereEnd will have created code that references the table
165993 ** directly. This loop scans all that code looking for opcodes
165994 ** that reference the table and converts them into opcodes that
165995 ** reference the index.
165996 */
165997 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
165998 pIdx = pLoop->u.btree.pIndex;
165999 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
166000 pIdx = pLevel->u.pCoveringIdx;
166001 }
166002 if( pIdx
166003 && !db->mallocFailed
166004 ){
166005 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
166006 last = iEnd;
166007 }else{
166008 last = pWInfo->iEndWhere;
166009 }
166010 if( pIdx->bHasExpr ){
166011 IndexedExpr *p = pParse->pIdxEpr;
166012 while( p ){
166013 if( p->iIdxCur==pLevel->iIdxCur ){
166014#ifdef WHERETRACE_ENABLED
166015 if( sqlite3WhereTrace & 0x200 ){
166016 sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n",
166017 p->iIdxCur, p->iIdxCol);
166018 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr);
166019 }
166020#endif
166021 p->iDataCur = -1;
166022 p->iIdxCur = -1;
166023 }
166024 p = p->pIENext;
166025 }
166026 }
166027 k = pLevel->addrBody + 1;
166028#ifdef SQLITE_DEBUG
166029 if( db->flags & SQLITE_VdbeAddopTrace ){
166030 printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n",
166031 pLevel->iTabCur, pLevel->iIdxCur, k, last-1);
166032 }
166033 /* Proof that the "+1" on the k value above is safe */
166034 pOp = sqlite3VdbeGetOp(v, k - 1);
166035 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
166036 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
166037 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
166038#endif
166039 pOp = sqlite3VdbeGetOp(p: v, addr: k);
166040 pLastOp = pOp + (last - k);
166041 assert( pOp<=pLastOp );
166042 do{
166043 if( pOp->p1!=pLevel->iTabCur ){
166044 /* no-op */
166045 }else if( pOp->opcode==OP_Column
166046#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
166047 || pOp->opcode==OP_Offset
166048#endif
166049 ){
166050 int x = pOp->p2;
166051 assert( pIdx->pTable==pTab );
166052#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
166053 if( pOp->opcode==OP_Offset ){
166054 /* Do not need to translate the column number */
166055 }else
166056#endif
166057 if( !HasRowid(pTab) ){
166058 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
166059 x = pPk->aiColumn[x];
166060 assert( x>=0 );
166061 }else{
166062 testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
166063 x = sqlite3StorageColumnToTable(pTab,iCol: x);
166064 }
166065 x = sqlite3TableColumnToIndex(pIdx, iCol: x);
166066 if( x>=0 ){
166067 pOp->p2 = x;
166068 pOp->p1 = pLevel->iIdxCur;
166069 OpcodeRewriteTrace(db, k, pOp);
166070 }else{
166071 /* Unable to translate the table reference into an index
166072 ** reference. Verify that this is harmless - that the
166073 ** table being referenced really is open.
166074 */
166075#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
166076 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
166077 || cursorIsOpen(v,pOp->p1,k)
166078 || pOp->opcode==OP_Offset
166079 );
166080#else
166081 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
166082 || cursorIsOpen(v,pOp->p1,k)
166083 );
166084#endif
166085 }
166086 }else if( pOp->opcode==OP_Rowid ){
166087 pOp->p1 = pLevel->iIdxCur;
166088 pOp->opcode = OP_IdxRowid;
166089 OpcodeRewriteTrace(db, k, pOp);
166090 }else if( pOp->opcode==OP_IfNullRow ){
166091 pOp->p1 = pLevel->iIdxCur;
166092 OpcodeRewriteTrace(db, k, pOp);
166093 }
166094#ifdef SQLITE_DEBUG
166095 k++;
166096#endif
166097 }while( (++pOp)<pLastOp );
166098#ifdef SQLITE_DEBUG
166099 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
166100#endif
166101 }
166102 }
166103
166104 /* The "break" point is here, just past the end of the outer loop.
166105 ** Set it.
166106 */
166107 sqlite3VdbeResolveLabel(v, x: pWInfo->iBreak);
166108
166109 /* Final cleanup
166110 */
166111 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
166112 whereInfoFree(db, pWInfo);
166113 pParse->withinRJSubrtn -= nRJ;
166114 return;
166115}
166116
166117/************** End of where.c ***********************************************/
166118/************** Begin file window.c ******************************************/
166119/*
166120** 2018 May 08
166121**
166122** The author disclaims copyright to this source code. In place of
166123** a legal notice, here is a blessing:
166124**
166125** May you do good and not evil.
166126** May you find forgiveness for yourself and forgive others.
166127** May you share freely, never taking more than you give.
166128**
166129*************************************************************************
166130*/
166131/* #include "sqliteInt.h" */
166132
166133#ifndef SQLITE_OMIT_WINDOWFUNC
166134
166135/*
166136** SELECT REWRITING
166137**
166138** Any SELECT statement that contains one or more window functions in
166139** either the select list or ORDER BY clause (the only two places window
166140** functions may be used) is transformed by function sqlite3WindowRewrite()
166141** in order to support window function processing. For example, with the
166142** schema:
166143**
166144** CREATE TABLE t1(a, b, c, d, e, f, g);
166145**
166146** the statement:
166147**
166148** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
166149**
166150** is transformed to:
166151**
166152** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
166153** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
166154** ) ORDER BY e;
166155**
166156** The flattening optimization is disabled when processing this transformed
166157** SELECT statement. This allows the implementation of the window function
166158** (in this case max()) to process rows sorted in order of (c, d), which
166159** makes things easier for obvious reasons. More generally:
166160**
166161** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
166162** the sub-query.
166163**
166164** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
166165**
166166** * Terminals from each of the expression trees that make up the
166167** select-list and ORDER BY expressions in the parent query are
166168** selected by the sub-query. For the purposes of the transformation,
166169** terminals are column references and aggregate functions.
166170**
166171** If there is more than one window function in the SELECT that uses
166172** the same window declaration (the OVER bit), then a single scan may
166173** be used to process more than one window function. For example:
166174**
166175** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
166176** min(e) OVER (PARTITION BY c ORDER BY d)
166177** FROM t1;
166178**
166179** is transformed in the same way as the example above. However:
166180**
166181** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
166182** min(e) OVER (PARTITION BY a ORDER BY b)
166183** FROM t1;
166184**
166185** Must be transformed to:
166186**
166187** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
166188** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
166189** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
166190** ) ORDER BY c, d
166191** ) ORDER BY e;
166192**
166193** so that both min() and max() may process rows in the order defined by
166194** their respective window declarations.
166195**
166196** INTERFACE WITH SELECT.C
166197**
166198** When processing the rewritten SELECT statement, code in select.c calls
166199** sqlite3WhereBegin() to begin iterating through the results of the
166200** sub-query, which is always implemented as a co-routine. It then calls
166201** sqlite3WindowCodeStep() to process rows and finish the scan by calling
166202** sqlite3WhereEnd().
166203**
166204** sqlite3WindowCodeStep() generates VM code so that, for each row returned
166205** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
166206** When the sub-routine is invoked:
166207**
166208** * The results of all window-functions for the row are stored
166209** in the associated Window.regResult registers.
166210**
166211** * The required terminal values are stored in the current row of
166212** temp table Window.iEphCsr.
166213**
166214** In some cases, depending on the window frame and the specific window
166215** functions invoked, sqlite3WindowCodeStep() caches each entire partition
166216** in a temp table before returning any rows. In other cases it does not.
166217** This detail is encapsulated within this file, the code generated by
166218** select.c is the same in either case.
166219**
166220** BUILT-IN WINDOW FUNCTIONS
166221**
166222** This implementation features the following built-in window functions:
166223**
166224** row_number()
166225** rank()
166226** dense_rank()
166227** percent_rank()
166228** cume_dist()
166229** ntile(N)
166230** lead(expr [, offset [, default]])
166231** lag(expr [, offset [, default]])
166232** first_value(expr)
166233** last_value(expr)
166234** nth_value(expr, N)
166235**
166236** These are the same built-in window functions supported by Postgres.
166237** Although the behaviour of aggregate window functions (functions that
166238** can be used as either aggregates or window functions) allows them to
166239** be implemented using an API, built-in window functions are much more
166240** esoteric. Additionally, some window functions (e.g. nth_value())
166241** may only be implemented by caching the entire partition in memory.
166242** As such, some built-in window functions use the same API as aggregate
166243** window functions and some are implemented directly using VDBE
166244** instructions. Additionally, for those functions that use the API, the
166245** window frame is sometimes modified before the SELECT statement is
166246** rewritten. For example, regardless of the specified window frame, the
166247** row_number() function always uses:
166248**
166249** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
166250**
166251** See sqlite3WindowUpdate() for details.
166252**
166253** As well as some of the built-in window functions, aggregate window
166254** functions min() and max() are implemented using VDBE instructions if
166255** the start of the window frame is declared as anything other than
166256** UNBOUNDED PRECEDING.
166257*/
166258
166259/*
166260** Implementation of built-in window function row_number(). Assumes that the
166261** window frame has been coerced to:
166262**
166263** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
166264*/
166265static void row_numberStepFunc(
166266 sqlite3_context *pCtx,
166267 int nArg,
166268 sqlite3_value **apArg
166269){
166270 i64 *p = (i64*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166271 if( p ) (*p)++;
166272 UNUSED_PARAMETER(nArg);
166273 UNUSED_PARAMETER(apArg);
166274}
166275static void row_numberValueFunc(sqlite3_context *pCtx){
166276 i64 *p = (i64*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166277 sqlite3_result_int64(pCtx, iVal: (p ? *p : 0));
166278}
166279
166280/*
166281** Context object type used by rank(), dense_rank(), percent_rank() and
166282** cume_dist().
166283*/
166284struct CallCount {
166285 i64 nValue;
166286 i64 nStep;
166287 i64 nTotal;
166288};
166289
166290/*
166291** Implementation of built-in window function dense_rank(). Assumes that
166292** the window frame has been set to:
166293**
166294** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
166295*/
166296static void dense_rankStepFunc(
166297 sqlite3_context *pCtx,
166298 int nArg,
166299 sqlite3_value **apArg
166300){
166301 struct CallCount *p;
166302 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166303 if( p ) p->nStep = 1;
166304 UNUSED_PARAMETER(nArg);
166305 UNUSED_PARAMETER(apArg);
166306}
166307static void dense_rankValueFunc(sqlite3_context *pCtx){
166308 struct CallCount *p;
166309 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166310 if( p ){
166311 if( p->nStep ){
166312 p->nValue++;
166313 p->nStep = 0;
166314 }
166315 sqlite3_result_int64(pCtx, iVal: p->nValue);
166316 }
166317}
166318
166319/*
166320** Implementation of built-in window function nth_value(). This
166321** implementation is used in "slow mode" only - when the EXCLUDE clause
166322** is not set to the default value "NO OTHERS".
166323*/
166324struct NthValueCtx {
166325 i64 nStep;
166326 sqlite3_value *pValue;
166327};
166328static void nth_valueStepFunc(
166329 sqlite3_context *pCtx,
166330 int nArg,
166331 sqlite3_value **apArg
166332){
166333 struct NthValueCtx *p;
166334 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166335 if( p ){
166336 i64 iVal;
166337 switch( sqlite3_value_numeric_type(pVal: apArg[1]) ){
166338 case SQLITE_INTEGER:
166339 iVal = sqlite3_value_int64(pVal: apArg[1]);
166340 break;
166341 case SQLITE_FLOAT: {
166342 double fVal = sqlite3_value_double(pVal: apArg[1]);
166343 if( ((i64)fVal)!=fVal ) goto error_out;
166344 iVal = (i64)fVal;
166345 break;
166346 }
166347 default:
166348 goto error_out;
166349 }
166350 if( iVal<=0 ) goto error_out;
166351
166352 p->nStep++;
166353 if( iVal==p->nStep ){
166354 p->pValue = sqlite3_value_dup(pOrig: apArg[0]);
166355 if( !p->pValue ){
166356 sqlite3_result_error_nomem(pCtx);
166357 }
166358 }
166359 }
166360 UNUSED_PARAMETER(nArg);
166361 UNUSED_PARAMETER(apArg);
166362 return;
166363
166364 error_out:
166365 sqlite3_result_error(
166366 pCtx, z: "second argument to nth_value must be a positive integer", n: -1
166367 );
166368}
166369static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
166370 struct NthValueCtx *p;
166371 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: 0);
166372 if( p && p->pValue ){
166373 sqlite3_result_value(pCtx, pValue: p->pValue);
166374 sqlite3_value_free(pOld: p->pValue);
166375 p->pValue = 0;
166376 }
166377}
166378#define nth_valueInvFunc noopStepFunc
166379#define nth_valueValueFunc noopValueFunc
166380
166381static void first_valueStepFunc(
166382 sqlite3_context *pCtx,
166383 int nArg,
166384 sqlite3_value **apArg
166385){
166386 struct NthValueCtx *p;
166387 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166388 if( p && p->pValue==0 ){
166389 p->pValue = sqlite3_value_dup(pOrig: apArg[0]);
166390 if( !p->pValue ){
166391 sqlite3_result_error_nomem(pCtx);
166392 }
166393 }
166394 UNUSED_PARAMETER(nArg);
166395 UNUSED_PARAMETER(apArg);
166396}
166397static void first_valueFinalizeFunc(sqlite3_context *pCtx){
166398 struct NthValueCtx *p;
166399 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166400 if( p && p->pValue ){
166401 sqlite3_result_value(pCtx, pValue: p->pValue);
166402 sqlite3_value_free(pOld: p->pValue);
166403 p->pValue = 0;
166404 }
166405}
166406#define first_valueInvFunc noopStepFunc
166407#define first_valueValueFunc noopValueFunc
166408
166409/*
166410** Implementation of built-in window function rank(). Assumes that
166411** the window frame has been set to:
166412**
166413** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
166414*/
166415static void rankStepFunc(
166416 sqlite3_context *pCtx,
166417 int nArg,
166418 sqlite3_value **apArg
166419){
166420 struct CallCount *p;
166421 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166422 if( p ){
166423 p->nStep++;
166424 if( p->nValue==0 ){
166425 p->nValue = p->nStep;
166426 }
166427 }
166428 UNUSED_PARAMETER(nArg);
166429 UNUSED_PARAMETER(apArg);
166430}
166431static void rankValueFunc(sqlite3_context *pCtx){
166432 struct CallCount *p;
166433 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166434 if( p ){
166435 sqlite3_result_int64(pCtx, iVal: p->nValue);
166436 p->nValue = 0;
166437 }
166438}
166439
166440/*
166441** Implementation of built-in window function percent_rank(). Assumes that
166442** the window frame has been set to:
166443**
166444** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
166445*/
166446static void percent_rankStepFunc(
166447 sqlite3_context *pCtx,
166448 int nArg,
166449 sqlite3_value **apArg
166450){
166451 struct CallCount *p;
166452 UNUSED_PARAMETER(nArg); assert( nArg==0 );
166453 UNUSED_PARAMETER(apArg);
166454 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166455 if( p ){
166456 p->nTotal++;
166457 }
166458}
166459static void percent_rankInvFunc(
166460 sqlite3_context *pCtx,
166461 int nArg,
166462 sqlite3_value **apArg
166463){
166464 struct CallCount *p;
166465 UNUSED_PARAMETER(nArg); assert( nArg==0 );
166466 UNUSED_PARAMETER(apArg);
166467 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166468 p->nStep++;
166469}
166470static void percent_rankValueFunc(sqlite3_context *pCtx){
166471 struct CallCount *p;
166472 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166473 if( p ){
166474 p->nValue = p->nStep;
166475 if( p->nTotal>1 ){
166476 double r = (double)p->nValue / (double)(p->nTotal-1);
166477 sqlite3_result_double(pCtx, rVal: r);
166478 }else{
166479 sqlite3_result_double(pCtx, rVal: 0.0);
166480 }
166481 }
166482}
166483#define percent_rankFinalizeFunc percent_rankValueFunc
166484
166485/*
166486** Implementation of built-in window function cume_dist(). Assumes that
166487** the window frame has been set to:
166488**
166489** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
166490*/
166491static void cume_distStepFunc(
166492 sqlite3_context *pCtx,
166493 int nArg,
166494 sqlite3_value **apArg
166495){
166496 struct CallCount *p;
166497 UNUSED_PARAMETER(nArg); assert( nArg==0 );
166498 UNUSED_PARAMETER(apArg);
166499 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166500 if( p ){
166501 p->nTotal++;
166502 }
166503}
166504static void cume_distInvFunc(
166505 sqlite3_context *pCtx,
166506 int nArg,
166507 sqlite3_value **apArg
166508){
166509 struct CallCount *p;
166510 UNUSED_PARAMETER(nArg); assert( nArg==0 );
166511 UNUSED_PARAMETER(apArg);
166512 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166513 p->nStep++;
166514}
166515static void cume_distValueFunc(sqlite3_context *pCtx){
166516 struct CallCount *p;
166517 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: 0);
166518 if( p ){
166519 double r = (double)(p->nStep) / (double)(p->nTotal);
166520 sqlite3_result_double(pCtx, rVal: r);
166521 }
166522}
166523#define cume_distFinalizeFunc cume_distValueFunc
166524
166525/*
166526** Context object for ntile() window function.
166527*/
166528struct NtileCtx {
166529 i64 nTotal; /* Total rows in partition */
166530 i64 nParam; /* Parameter passed to ntile(N) */
166531 i64 iRow; /* Current row */
166532};
166533
166534/*
166535** Implementation of ntile(). This assumes that the window frame has
166536** been coerced to:
166537**
166538** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
166539*/
166540static void ntileStepFunc(
166541 sqlite3_context *pCtx,
166542 int nArg,
166543 sqlite3_value **apArg
166544){
166545 struct NtileCtx *p;
166546 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
166547 p = (struct NtileCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166548 if( p ){
166549 if( p->nTotal==0 ){
166550 p->nParam = sqlite3_value_int64(pVal: apArg[0]);
166551 if( p->nParam<=0 ){
166552 sqlite3_result_error(
166553 pCtx, z: "argument of ntile must be a positive integer", n: -1
166554 );
166555 }
166556 }
166557 p->nTotal++;
166558 }
166559}
166560static void ntileInvFunc(
166561 sqlite3_context *pCtx,
166562 int nArg,
166563 sqlite3_value **apArg
166564){
166565 struct NtileCtx *p;
166566 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
166567 UNUSED_PARAMETER(apArg);
166568 p = (struct NtileCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166569 p->iRow++;
166570}
166571static void ntileValueFunc(sqlite3_context *pCtx){
166572 struct NtileCtx *p;
166573 p = (struct NtileCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166574 if( p && p->nParam>0 ){
166575 int nSize = (p->nTotal / p->nParam);
166576 if( nSize==0 ){
166577 sqlite3_result_int64(pCtx, iVal: p->iRow+1);
166578 }else{
166579 i64 nLarge = p->nTotal - p->nParam*nSize;
166580 i64 iSmall = nLarge*(nSize+1);
166581 i64 iRow = p->iRow;
166582
166583 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
166584
166585 if( iRow<iSmall ){
166586 sqlite3_result_int64(pCtx, iVal: 1 + iRow/(nSize+1));
166587 }else{
166588 sqlite3_result_int64(pCtx, iVal: 1 + nLarge + (iRow-iSmall)/nSize);
166589 }
166590 }
166591 }
166592}
166593#define ntileFinalizeFunc ntileValueFunc
166594
166595/*
166596** Context object for last_value() window function.
166597*/
166598struct LastValueCtx {
166599 sqlite3_value *pVal;
166600 int nVal;
166601};
166602
166603/*
166604** Implementation of last_value().
166605*/
166606static void last_valueStepFunc(
166607 sqlite3_context *pCtx,
166608 int nArg,
166609 sqlite3_value **apArg
166610){
166611 struct LastValueCtx *p;
166612 UNUSED_PARAMETER(nArg);
166613 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166614 if( p ){
166615 sqlite3_value_free(pOld: p->pVal);
166616 p->pVal = sqlite3_value_dup(pOrig: apArg[0]);
166617 if( p->pVal==0 ){
166618 sqlite3_result_error_nomem(pCtx);
166619 }else{
166620 p->nVal++;
166621 }
166622 }
166623}
166624static void last_valueInvFunc(
166625 sqlite3_context *pCtx,
166626 int nArg,
166627 sqlite3_value **apArg
166628){
166629 struct LastValueCtx *p;
166630 UNUSED_PARAMETER(nArg);
166631 UNUSED_PARAMETER(apArg);
166632 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166633 if( ALWAYS(p) ){
166634 p->nVal--;
166635 if( p->nVal==0 ){
166636 sqlite3_value_free(pOld: p->pVal);
166637 p->pVal = 0;
166638 }
166639 }
166640}
166641static void last_valueValueFunc(sqlite3_context *pCtx){
166642 struct LastValueCtx *p;
166643 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: 0);
166644 if( p && p->pVal ){
166645 sqlite3_result_value(pCtx, pValue: p->pVal);
166646 }
166647}
166648static void last_valueFinalizeFunc(sqlite3_context *pCtx){
166649 struct LastValueCtx *p;
166650 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
166651 if( p && p->pVal ){
166652 sqlite3_result_value(pCtx, pValue: p->pVal);
166653 sqlite3_value_free(pOld: p->pVal);
166654 p->pVal = 0;
166655 }
166656}
166657
166658/*
166659** Static names for the built-in window function names. These static
166660** names are used, rather than string literals, so that FuncDef objects
166661** can be associated with a particular window function by direct
166662** comparison of the zName pointer. Example:
166663**
166664** if( pFuncDef->zName==row_valueName ){ ... }
166665*/
166666static const char row_numberName[] = "row_number";
166667static const char dense_rankName[] = "dense_rank";
166668static const char rankName[] = "rank";
166669static const char percent_rankName[] = "percent_rank";
166670static const char cume_distName[] = "cume_dist";
166671static const char ntileName[] = "ntile";
166672static const char last_valueName[] = "last_value";
166673static const char nth_valueName[] = "nth_value";
166674static const char first_valueName[] = "first_value";
166675static const char leadName[] = "lead";
166676static const char lagName[] = "lag";
166677
166678/*
166679** No-op implementations of xStep() and xFinalize(). Used as place-holders
166680** for built-in window functions that never call those interfaces.
166681**
166682** The noopValueFunc() is called but is expected to do nothing. The
166683** noopStepFunc() is never called, and so it is marked with NO_TEST to
166684** let the test coverage routine know not to expect this function to be
166685** invoked.
166686*/
166687static void noopStepFunc( /*NO_TEST*/
166688 sqlite3_context *p, /*NO_TEST*/
166689 int n, /*NO_TEST*/
166690 sqlite3_value **a /*NO_TEST*/
166691){ /*NO_TEST*/
166692 UNUSED_PARAMETER(p); /*NO_TEST*/
166693 UNUSED_PARAMETER(n); /*NO_TEST*/
166694 UNUSED_PARAMETER(a); /*NO_TEST*/
166695 assert(0); /*NO_TEST*/
166696} /*NO_TEST*/
166697static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
166698
166699/* Window functions that use all window interfaces: xStep, xFinal,
166700** xValue, and xInverse */
166701#define WINDOWFUNCALL(name,nArg,extra) { \
166702 nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
166703 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
166704 name ## InvFunc, name ## Name, {0} \
166705}
166706
166707/* Window functions that are implemented using bytecode and thus have
166708** no-op routines for their methods */
166709#define WINDOWFUNCNOOP(name,nArg,extra) { \
166710 nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
166711 noopStepFunc, noopValueFunc, noopValueFunc, \
166712 noopStepFunc, name ## Name, {0} \
166713}
166714
166715/* Window functions that use all window interfaces: xStep, the
166716** same routine for xFinalize and xValue and which never call
166717** xInverse. */
166718#define WINDOWFUNCX(name,nArg,extra) { \
166719 nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
166720 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
166721 noopStepFunc, name ## Name, {0} \
166722}
166723
166724
166725/*
166726** Register those built-in window functions that are not also aggregates.
166727*/
166728SQLITE_PRIVATE void sqlite3WindowFunctions(void){
166729 static FuncDef aWindowFuncs[] = {
166730 WINDOWFUNCX(row_number, 0, 0),
166731 WINDOWFUNCX(dense_rank, 0, 0),
166732 WINDOWFUNCX(rank, 0, 0),
166733 WINDOWFUNCALL(percent_rank, 0, 0),
166734 WINDOWFUNCALL(cume_dist, 0, 0),
166735 WINDOWFUNCALL(ntile, 1, 0),
166736 WINDOWFUNCALL(last_value, 1, 0),
166737 WINDOWFUNCALL(nth_value, 2, 0),
166738 WINDOWFUNCALL(first_value, 1, 0),
166739 WINDOWFUNCNOOP(lead, 1, 0),
166740 WINDOWFUNCNOOP(lead, 2, 0),
166741 WINDOWFUNCNOOP(lead, 3, 0),
166742 WINDOWFUNCNOOP(lag, 1, 0),
166743 WINDOWFUNCNOOP(lag, 2, 0),
166744 WINDOWFUNCNOOP(lag, 3, 0),
166745 };
166746 sqlite3InsertBuiltinFuncs(aDef: aWindowFuncs, ArraySize(aWindowFuncs));
166747}
166748
166749static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
166750 Window *p;
166751 for(p=pList; p; p=p->pNextWin){
166752 if( sqlite3StrICmp(zLeft: p->zName, zRight: zName)==0 ) break;
166753 }
166754 if( p==0 ){
166755 sqlite3ErrorMsg(pParse, zFormat: "no such window: %s", zName);
166756 }
166757 return p;
166758}
166759
166760/*
166761** This function is called immediately after resolving the function name
166762** for a window function within a SELECT statement. Argument pList is a
166763** linked list of WINDOW definitions for the current SELECT statement.
166764** Argument pFunc is the function definition just resolved and pWin
166765** is the Window object representing the associated OVER clause. This
166766** function updates the contents of pWin as follows:
166767**
166768** * If the OVER clause referred to a named window (as in "max(x) OVER win"),
166769** search list pList for a matching WINDOW definition, and update pWin
166770** accordingly. If no such WINDOW clause can be found, leave an error
166771** in pParse.
166772**
166773** * If the function is a built-in window function that requires the
166774** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
166775** of this file), pWin is updated here.
166776*/
166777SQLITE_PRIVATE void sqlite3WindowUpdate(
166778 Parse *pParse,
166779 Window *pList, /* List of named windows for this SELECT */
166780 Window *pWin, /* Window frame to update */
166781 FuncDef *pFunc /* Window function definition */
166782){
166783 if( pWin->zName && pWin->eFrmType==0 ){
166784 Window *p = windowFind(pParse, pList, zName: pWin->zName);
166785 if( p==0 ) return;
166786 pWin->pPartition = sqlite3ExprListDup(db: pParse->db, p: p->pPartition, flags: 0);
166787 pWin->pOrderBy = sqlite3ExprListDup(db: pParse->db, p: p->pOrderBy, flags: 0);
166788 pWin->pStart = sqlite3ExprDup(db: pParse->db, p: p->pStart, flags: 0);
166789 pWin->pEnd = sqlite3ExprDup(db: pParse->db, p: p->pEnd, flags: 0);
166790 pWin->eStart = p->eStart;
166791 pWin->eEnd = p->eEnd;
166792 pWin->eFrmType = p->eFrmType;
166793 pWin->eExclude = p->eExclude;
166794 }else{
166795 sqlite3WindowChain(pParse, pWin, pList);
166796 }
166797 if( (pWin->eFrmType==TK_RANGE)
166798 && (pWin->pStart || pWin->pEnd)
166799 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
166800 ){
166801 sqlite3ErrorMsg(pParse,
166802 zFormat: "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
166803 );
166804 }else
166805 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
166806 sqlite3 *db = pParse->db;
166807 if( pWin->pFilter ){
166808 sqlite3ErrorMsg(pParse,
166809 zFormat: "FILTER clause may only be used with aggregate window functions"
166810 );
166811 }else{
166812 struct WindowUpdate {
166813 const char *zFunc;
166814 int eFrmType;
166815 int eStart;
166816 int eEnd;
166817 } aUp[] = {
166818 { row_numberName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
166819 { dense_rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
166820 { rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
166821 { percent_rankName, TK_GROUPS, TK_CURRENT, TK_UNBOUNDED },
166822 { cume_distName, TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED },
166823 { ntileName, TK_ROWS, TK_CURRENT, TK_UNBOUNDED },
166824 { leadName, TK_ROWS, TK_UNBOUNDED, TK_UNBOUNDED },
166825 { lagName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
166826 };
166827 int i;
166828 for(i=0; i<ArraySize(aUp); i++){
166829 if( pFunc->zName==aUp[i].zFunc ){
166830 sqlite3ExprDelete(db, p: pWin->pStart);
166831 sqlite3ExprDelete(db, p: pWin->pEnd);
166832 pWin->pEnd = pWin->pStart = 0;
166833 pWin->eFrmType = aUp[i].eFrmType;
166834 pWin->eStart = aUp[i].eStart;
166835 pWin->eEnd = aUp[i].eEnd;
166836 pWin->eExclude = 0;
166837 if( pWin->eStart==TK_FOLLOWING ){
166838 pWin->pStart = sqlite3Expr(db, TK_INTEGER, zToken: "1");
166839 }
166840 break;
166841 }
166842 }
166843 }
166844 }
166845 pWin->pWFunc = pFunc;
166846}
166847
166848/*
166849** Context object passed through sqlite3WalkExprList() to
166850** selectWindowRewriteExprCb() by selectWindowRewriteEList().
166851*/
166852typedef struct WindowRewrite WindowRewrite;
166853struct WindowRewrite {
166854 Window *pWin;
166855 SrcList *pSrc;
166856 ExprList *pSub;
166857 Table *pTab;
166858 Select *pSubSelect; /* Current sub-select, if any */
166859};
166860
166861/*
166862** Callback function used by selectWindowRewriteEList(). If necessary,
166863** this function appends to the output expression-list and updates
166864** expression (*ppExpr) in place.
166865*/
166866static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
166867 struct WindowRewrite *p = pWalker->u.pRewrite;
166868 Parse *pParse = pWalker->pParse;
166869 assert( p!=0 );
166870 assert( p->pWin!=0 );
166871
166872 /* If this function is being called from within a scalar sub-select
166873 ** that used by the SELECT statement being processed, only process
166874 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
166875 ** not process aggregates or window functions at all, as they belong
166876 ** to the scalar sub-select. */
166877 if( p->pSubSelect ){
166878 if( pExpr->op!=TK_COLUMN ){
166879 return WRC_Continue;
166880 }else{
166881 int nSrc = p->pSrc->nSrc;
166882 int i;
166883 for(i=0; i<nSrc; i++){
166884 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
166885 }
166886 if( i==nSrc ) return WRC_Continue;
166887 }
166888 }
166889
166890 switch( pExpr->op ){
166891
166892 case TK_FUNCTION:
166893 if( !ExprHasProperty(pExpr, EP_WinFunc) ){
166894 break;
166895 }else{
166896 Window *pWin;
166897 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
166898 if( pExpr->y.pWin==pWin ){
166899 assert( pWin->pOwner==pExpr );
166900 return WRC_Prune;
166901 }
166902 }
166903 }
166904 /* no break */ deliberate_fall_through
166905
166906 case TK_IF_NULL_ROW:
166907 case TK_AGG_FUNCTION:
166908 case TK_COLUMN: {
166909 int iCol = -1;
166910 if( pParse->db->mallocFailed ) return WRC_Abort;
166911 if( p->pSub ){
166912 int i;
166913 for(i=0; i<p->pSub->nExpr; i++){
166914 if( 0==sqlite3ExprCompare(pParse: 0, pA: p->pSub->a[i].pExpr, pB: pExpr, iTab: -1) ){
166915 iCol = i;
166916 break;
166917 }
166918 }
166919 }
166920 if( iCol<0 ){
166921 Expr *pDup = sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0);
166922 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
166923 p->pSub = sqlite3ExprListAppend(pParse, pList: p->pSub, pExpr: pDup);
166924 }
166925 if( p->pSub ){
166926 int f = pExpr->flags & EP_Collate;
166927 assert( ExprHasProperty(pExpr, EP_Static)==0 );
166928 ExprSetProperty(pExpr, EP_Static);
166929 sqlite3ExprDelete(db: pParse->db, p: pExpr);
166930 ExprClearProperty(pExpr, EP_Static);
166931 memset(s: pExpr, c: 0, n: sizeof(Expr));
166932
166933 pExpr->op = TK_COLUMN;
166934 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
166935 pExpr->iTable = p->pWin->iEphCsr;
166936 pExpr->y.pTab = p->pTab;
166937 pExpr->flags = f;
166938 }
166939 if( pParse->db->mallocFailed ) return WRC_Abort;
166940 break;
166941 }
166942
166943 default: /* no-op */
166944 break;
166945 }
166946
166947 return WRC_Continue;
166948}
166949static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
166950 struct WindowRewrite *p = pWalker->u.pRewrite;
166951 Select *pSave = p->pSubSelect;
166952 if( pSave==pSelect ){
166953 return WRC_Continue;
166954 }else{
166955 p->pSubSelect = pSelect;
166956 sqlite3WalkSelect(pWalker, p: pSelect);
166957 p->pSubSelect = pSave;
166958 }
166959 return WRC_Prune;
166960}
166961
166962
166963/*
166964** Iterate through each expression in expression-list pEList. For each:
166965**
166966** * TK_COLUMN,
166967** * aggregate function, or
166968** * window function with a Window object that is not a member of the
166969** Window list passed as the second argument (pWin).
166970**
166971** Append the node to output expression-list (*ppSub). And replace it
166972** with a TK_COLUMN that reads the (N-1)th element of table
166973** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
166974** appending the new one.
166975*/
166976static void selectWindowRewriteEList(
166977 Parse *pParse,
166978 Window *pWin,
166979 SrcList *pSrc,
166980 ExprList *pEList, /* Rewrite expressions in this list */
166981 Table *pTab,
166982 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
166983){
166984 Walker sWalker;
166985 WindowRewrite sRewrite;
166986
166987 assert( pWin!=0 );
166988 memset(s: &sWalker, c: 0, n: sizeof(Walker));
166989 memset(s: &sRewrite, c: 0, n: sizeof(WindowRewrite));
166990
166991 sRewrite.pSub = *ppSub;
166992 sRewrite.pWin = pWin;
166993 sRewrite.pSrc = pSrc;
166994 sRewrite.pTab = pTab;
166995
166996 sWalker.pParse = pParse;
166997 sWalker.xExprCallback = selectWindowRewriteExprCb;
166998 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
166999 sWalker.u.pRewrite = &sRewrite;
167000
167001 (void)sqlite3WalkExprList(pWalker: &sWalker, p: pEList);
167002
167003 *ppSub = sRewrite.pSub;
167004}
167005
167006/*
167007** Append a copy of each expression in expression-list pAppend to
167008** expression list pList. Return a pointer to the result list.
167009*/
167010static ExprList *exprListAppendList(
167011 Parse *pParse, /* Parsing context */
167012 ExprList *pList, /* List to which to append. Might be NULL */
167013 ExprList *pAppend, /* List of values to append. Might be NULL */
167014 int bIntToNull
167015){
167016 if( pAppend ){
167017 int i;
167018 int nInit = pList ? pList->nExpr : 0;
167019 for(i=0; i<pAppend->nExpr; i++){
167020 sqlite3 *db = pParse->db;
167021 Expr *pDup = sqlite3ExprDup(db, p: pAppend->a[i].pExpr, flags: 0);
167022 if( db->mallocFailed ){
167023 sqlite3ExprDelete(db, p: pDup);
167024 break;
167025 }
167026 if( bIntToNull ){
167027 int iDummy;
167028 Expr *pSub;
167029 pSub = sqlite3ExprSkipCollateAndLikely(pExpr: pDup);
167030 if( sqlite3ExprIsInteger(p: pSub, pValue: &iDummy) ){
167031 pSub->op = TK_NULL;
167032 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
167033 pSub->u.zToken = 0;
167034 }
167035 }
167036 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pDup);
167037 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
167038 }
167039 }
167040 return pList;
167041}
167042
167043/*
167044** When rewriting a query, if the new subquery in the FROM clause
167045** contains TK_AGG_FUNCTION nodes that refer to an outer query,
167046** then we have to increase the Expr->op2 values of those nodes
167047** due to the extra subquery layer that was added.
167048**
167049** See also the incrAggDepth() routine in resolve.c
167050*/
167051static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
167052 if( pExpr->op==TK_AGG_FUNCTION
167053 && pExpr->op2>=pWalker->walkerDepth
167054 ){
167055 pExpr->op2++;
167056 }
167057 return WRC_Continue;
167058}
167059
167060static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
167061 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
167062 assert( !ExprHasProperty(pExpr, EP_IntValue) );
167063 sqlite3ErrorMsg(pParse: pWalker->pParse,
167064 zFormat: "misuse of aggregate: %s()", pExpr->u.zToken);
167065 }
167066 return WRC_Continue;
167067}
167068
167069/*
167070** If the SELECT statement passed as the second argument does not invoke
167071** any SQL window functions, this function is a no-op. Otherwise, it
167072** rewrites the SELECT statement so that window function xStep functions
167073** are invoked in the correct order as described under "SELECT REWRITING"
167074** at the top of this file.
167075*/
167076SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
167077 int rc = SQLITE_OK;
167078 if( p->pWin
167079 && p->pPrior==0
167080 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
167081 && ALWAYS(!IN_RENAME_OBJECT)
167082 ){
167083 Vdbe *v = sqlite3GetVdbe(pParse);
167084 sqlite3 *db = pParse->db;
167085 Select *pSub = 0; /* The subquery */
167086 SrcList *pSrc = p->pSrc;
167087 Expr *pWhere = p->pWhere;
167088 ExprList *pGroupBy = p->pGroupBy;
167089 Expr *pHaving = p->pHaving;
167090 ExprList *pSort = 0;
167091
167092 ExprList *pSublist = 0; /* Expression list for sub-query */
167093 Window *pMWin = p->pWin; /* Main window object */
167094 Window *pWin; /* Window object iterator */
167095 Table *pTab;
167096 Walker w;
167097
167098 u32 selFlags = p->selFlags;
167099
167100 pTab = sqlite3DbMallocZero(db, n: sizeof(Table));
167101 if( pTab==0 ){
167102 return sqlite3ErrorToParser(db, SQLITE_NOMEM);
167103 }
167104 sqlite3AggInfoPersistWalkerInit(pWalker: &w, pParse);
167105 sqlite3WalkSelect(pWalker: &w, p);
167106 if( (p->selFlags & SF_Aggregate)==0 ){
167107 w.xExprCallback = disallowAggregatesInOrderByCb;
167108 w.xSelectCallback = 0;
167109 sqlite3WalkExprList(pWalker: &w, p: p->pOrderBy);
167110 }
167111
167112 p->pSrc = 0;
167113 p->pWhere = 0;
167114 p->pGroupBy = 0;
167115 p->pHaving = 0;
167116 p->selFlags &= ~SF_Aggregate;
167117 p->selFlags |= SF_WinRewrite;
167118
167119 /* Create the ORDER BY clause for the sub-select. This is the concatenation
167120 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
167121 ** redundant, remove the ORDER BY from the parent SELECT. */
167122 pSort = exprListAppendList(pParse, pList: 0, pAppend: pMWin->pPartition, bIntToNull: 1);
167123 pSort = exprListAppendList(pParse, pList: pSort, pAppend: pMWin->pOrderBy, bIntToNull: 1);
167124 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
167125 int nSave = pSort->nExpr;
167126 pSort->nExpr = p->pOrderBy->nExpr;
167127 if( sqlite3ExprListCompare(pA: pSort, pB: p->pOrderBy, iTab: -1)==0 ){
167128 sqlite3ExprListDelete(db, pList: p->pOrderBy);
167129 p->pOrderBy = 0;
167130 }
167131 pSort->nExpr = nSave;
167132 }
167133
167134 /* Assign a cursor number for the ephemeral table used to buffer rows.
167135 ** The OpenEphemeral instruction is coded later, after it is known how
167136 ** many columns the table will have. */
167137 pMWin->iEphCsr = pParse->nTab++;
167138 pParse->nTab += 3;
167139
167140 selectWindowRewriteEList(pParse, pWin: pMWin, pSrc, pEList: p->pEList, pTab, ppSub: &pSublist);
167141 selectWindowRewriteEList(pParse, pWin: pMWin, pSrc, pEList: p->pOrderBy, pTab, ppSub: &pSublist);
167142 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
167143
167144 /* Append the PARTITION BY and ORDER BY expressions to the to the
167145 ** sub-select expression list. They are required to figure out where
167146 ** boundaries for partitions and sets of peer rows lie. */
167147 pSublist = exprListAppendList(pParse, pList: pSublist, pAppend: pMWin->pPartition, bIntToNull: 0);
167148 pSublist = exprListAppendList(pParse, pList: pSublist, pAppend: pMWin->pOrderBy, bIntToNull: 0);
167149
167150 /* Append the arguments passed to each window function to the
167151 ** sub-select expression list. Also allocate two registers for each
167152 ** window function - one for the accumulator, another for interim
167153 ** results. */
167154 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
167155 ExprList *pArgs;
167156 assert( ExprUseXList(pWin->pOwner) );
167157 assert( pWin->pWFunc!=0 );
167158 pArgs = pWin->pOwner->x.pList;
167159 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
167160 selectWindowRewriteEList(pParse, pWin: pMWin, pSrc, pEList: pArgs, pTab, ppSub: &pSublist);
167161 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
167162 pWin->bExprArgs = 1;
167163 }else{
167164 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
167165 pSublist = exprListAppendList(pParse, pList: pSublist, pAppend: pArgs, bIntToNull: 0);
167166 }
167167 if( pWin->pFilter ){
167168 Expr *pFilter = sqlite3ExprDup(db, p: pWin->pFilter, flags: 0);
167169 pSublist = sqlite3ExprListAppend(pParse, pList: pSublist, pExpr: pFilter);
167170 }
167171 pWin->regAccum = ++pParse->nMem;
167172 pWin->regResult = ++pParse->nMem;
167173 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
167174 }
167175
167176 /* If there is no ORDER BY or PARTITION BY clause, and the window
167177 ** function accepts zero arguments, and there are no other columns
167178 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
167179 ** that pSublist is still NULL here. Add a constant expression here to
167180 ** keep everything legal in this case.
167181 */
167182 if( pSublist==0 ){
167183 pSublist = sqlite3ExprListAppend(pParse, pList: 0,
167184 pExpr: sqlite3Expr(db, TK_INTEGER, zToken: "0")
167185 );
167186 }
167187
167188 pSub = sqlite3SelectNew(
167189 pParse, pEList: pSublist, pSrc, pWhere, pGroupBy, pHaving, pOrderBy: pSort, selFlags: 0, pLimit: 0
167190 );
167191 TREETRACE(0x40,pParse,pSub,
167192 ("New window-function subquery in FROM clause of (%u/%p)\n",
167193 p->selId, p));
167194 p->pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
167195 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
167196 ** of sqlite3DbMallocRawNN() called from
167197 ** sqlite3SrcListAppend() */
167198 if( p->pSrc ){
167199 Table *pTab2;
167200 p->pSrc->a[0].pSelect = pSub;
167201 p->pSrc->a[0].fg.isCorrelated = 1;
167202 sqlite3SrcListAssignCursors(pParse, pList: p->pSrc);
167203 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
167204 pTab2 = sqlite3ResultSetOfSelect(pParse, pSelect: pSub, SQLITE_AFF_NONE);
167205 pSub->selFlags |= (selFlags & SF_Aggregate);
167206 if( pTab2==0 ){
167207 /* Might actually be some other kind of error, but in that case
167208 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
167209 ** the correct error message regardless. */
167210 rc = SQLITE_NOMEM;
167211 }else{
167212 memcpy(dest: pTab, src: pTab2, n: sizeof(Table));
167213 pTab->tabFlags |= TF_Ephemeral;
167214 p->pSrc->a[0].pTab = pTab;
167215 pTab = pTab2;
167216 memset(s: &w, c: 0, n: sizeof(w));
167217 w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
167218 w.xSelectCallback = sqlite3WalkerDepthIncrease;
167219 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
167220 sqlite3WalkSelect(pWalker: &w, p: pSub);
167221 }
167222 }else{
167223 sqlite3SelectDelete(db, p: pSub);
167224 }
167225 if( db->mallocFailed ) rc = SQLITE_NOMEM;
167226
167227 /* Defer deleting the temporary table pTab because if an error occurred,
167228 ** there could still be references to that table embedded in the
167229 ** result-set or ORDER BY clause of the SELECT statement p. */
167230 sqlite3ParserAddCleanup(pParse, xCleanup: sqlite3DbFree, pPtr: pTab);
167231 }
167232
167233 assert( rc==SQLITE_OK || pParse->nErr!=0 );
167234 return rc;
167235}
167236
167237/*
167238** Unlink the Window object from the Select to which it is attached,
167239** if it is attached.
167240*/
167241SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window *p){
167242 if( p->ppThis ){
167243 *p->ppThis = p->pNextWin;
167244 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
167245 p->ppThis = 0;
167246 }
167247}
167248
167249/*
167250** Free the Window object passed as the second argument.
167251*/
167252SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
167253 if( p ){
167254 sqlite3WindowUnlinkFromSelect(p);
167255 sqlite3ExprDelete(db, p: p->pFilter);
167256 sqlite3ExprListDelete(db, pList: p->pPartition);
167257 sqlite3ExprListDelete(db, pList: p->pOrderBy);
167258 sqlite3ExprDelete(db, p: p->pEnd);
167259 sqlite3ExprDelete(db, p: p->pStart);
167260 sqlite3DbFree(db, p: p->zName);
167261 sqlite3DbFree(db, p: p->zBase);
167262 sqlite3DbFree(db, p);
167263 }
167264}
167265
167266/*
167267** Free the linked list of Window objects starting at the second argument.
167268*/
167269SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
167270 while( p ){
167271 Window *pNext = p->pNextWin;
167272 sqlite3WindowDelete(db, p);
167273 p = pNext;
167274 }
167275}
167276
167277/*
167278** The argument expression is an PRECEDING or FOLLOWING offset. The
167279** value should be a non-negative integer. If the value is not a
167280** constant, change it to NULL. The fact that it is then a non-negative
167281** integer will be caught later. But it is important not to leave
167282** variable values in the expression tree.
167283*/
167284static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
167285 if( 0==sqlite3ExprIsConstant(p: pExpr) ){
167286 if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
167287 sqlite3ExprDelete(db: pParse->db, p: pExpr);
167288 pExpr = sqlite3ExprAlloc(db: pParse->db, TK_NULL, pToken: 0, dequote: 0);
167289 }
167290 return pExpr;
167291}
167292
167293/*
167294** Allocate and return a new Window object describing a Window Definition.
167295*/
167296SQLITE_PRIVATE Window *sqlite3WindowAlloc(
167297 Parse *pParse, /* Parsing context */
167298 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
167299 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
167300 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
167301 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
167302 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
167303 u8 eExclude /* EXCLUDE clause */
167304){
167305 Window *pWin = 0;
167306 int bImplicitFrame = 0;
167307
167308 /* Parser assures the following: */
167309 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
167310 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
167311 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
167312 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
167313 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
167314 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
167315 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
167316
167317 if( eType==0 ){
167318 bImplicitFrame = 1;
167319 eType = TK_RANGE;
167320 }
167321
167322 /* Additionally, the
167323 ** starting boundary type may not occur earlier in the following list than
167324 ** the ending boundary type:
167325 **
167326 ** UNBOUNDED PRECEDING
167327 ** <expr> PRECEDING
167328 ** CURRENT ROW
167329 ** <expr> FOLLOWING
167330 ** UNBOUNDED FOLLOWING
167331 **
167332 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
167333 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
167334 ** frame boundary.
167335 */
167336 if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
167337 || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
167338 ){
167339 sqlite3ErrorMsg(pParse, zFormat: "unsupported frame specification");
167340 goto windowAllocErr;
167341 }
167342
167343 pWin = (Window*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(Window));
167344 if( pWin==0 ) goto windowAllocErr;
167345 pWin->eFrmType = eType;
167346 pWin->eStart = eStart;
167347 pWin->eEnd = eEnd;
167348 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
167349 eExclude = TK_NO;
167350 }
167351 pWin->eExclude = eExclude;
167352 pWin->bImplicitFrame = bImplicitFrame;
167353 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pExpr: pEnd);
167354 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pExpr: pStart);
167355 return pWin;
167356
167357windowAllocErr:
167358 sqlite3ExprDelete(db: pParse->db, p: pEnd);
167359 sqlite3ExprDelete(db: pParse->db, p: pStart);
167360 return 0;
167361}
167362
167363/*
167364** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
167365** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
167366** equivalent nul-terminated string.
167367*/
167368SQLITE_PRIVATE Window *sqlite3WindowAssemble(
167369 Parse *pParse,
167370 Window *pWin,
167371 ExprList *pPartition,
167372 ExprList *pOrderBy,
167373 Token *pBase
167374){
167375 if( pWin ){
167376 pWin->pPartition = pPartition;
167377 pWin->pOrderBy = pOrderBy;
167378 if( pBase ){
167379 pWin->zBase = sqlite3DbStrNDup(db: pParse->db, z: pBase->z, n: pBase->n);
167380 }
167381 }else{
167382 sqlite3ExprListDelete(db: pParse->db, pList: pPartition);
167383 sqlite3ExprListDelete(db: pParse->db, pList: pOrderBy);
167384 }
167385 return pWin;
167386}
167387
167388/*
167389** Window *pWin has just been created from a WINDOW clause. Token pBase
167390** is the base window. Earlier windows from the same WINDOW clause are
167391** stored in the linked list starting at pWin->pNextWin. This function
167392** either updates *pWin according to the base specification, or else
167393** leaves an error in pParse.
167394*/
167395SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
167396 if( pWin->zBase ){
167397 sqlite3 *db = pParse->db;
167398 Window *pExist = windowFind(pParse, pList, zName: pWin->zBase);
167399 if( pExist ){
167400 const char *zErr = 0;
167401 /* Check for errors */
167402 if( pWin->pPartition ){
167403 zErr = "PARTITION clause";
167404 }else if( pExist->pOrderBy && pWin->pOrderBy ){
167405 zErr = "ORDER BY clause";
167406 }else if( pExist->bImplicitFrame==0 ){
167407 zErr = "frame specification";
167408 }
167409 if( zErr ){
167410 sqlite3ErrorMsg(pParse,
167411 zFormat: "cannot override %s of window: %s", zErr, pWin->zBase
167412 );
167413 }else{
167414 pWin->pPartition = sqlite3ExprListDup(db, p: pExist->pPartition, flags: 0);
167415 if( pExist->pOrderBy ){
167416 assert( pWin->pOrderBy==0 );
167417 pWin->pOrderBy = sqlite3ExprListDup(db, p: pExist->pOrderBy, flags: 0);
167418 }
167419 sqlite3DbFree(db, p: pWin->zBase);
167420 pWin->zBase = 0;
167421 }
167422 }
167423 }
167424}
167425
167426/*
167427** Attach window object pWin to expression p.
167428*/
167429SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
167430 if( p ){
167431 assert( p->op==TK_FUNCTION );
167432 assert( pWin );
167433 p->y.pWin = pWin;
167434 ExprSetProperty(p, EP_WinFunc);
167435 pWin->pOwner = p;
167436 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
167437 sqlite3ErrorMsg(pParse,
167438 zFormat: "DISTINCT is not supported for window functions"
167439 );
167440 }
167441 }else{
167442 sqlite3WindowDelete(db: pParse->db, p: pWin);
167443 }
167444}
167445
167446/*
167447** Possibly link window pWin into the list at pSel->pWin (window functions
167448** to be processed as part of SELECT statement pSel). The window is linked
167449** in if either (a) there are no other windows already linked to this
167450** SELECT, or (b) the windows already linked use a compatible window frame.
167451*/
167452SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin){
167453 if( pSel ){
167454 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
167455 pWin->pNextWin = pSel->pWin;
167456 if( pSel->pWin ){
167457 pSel->pWin->ppThis = &pWin->pNextWin;
167458 }
167459 pSel->pWin = pWin;
167460 pWin->ppThis = &pSel->pWin;
167461 }else{
167462 if( sqlite3ExprListCompare(pA: pWin->pPartition, pB: pSel->pWin->pPartition,iTab: -1) ){
167463 pSel->selFlags |= SF_MultiPart;
167464 }
167465 }
167466 }
167467}
167468
167469/*
167470** Return 0 if the two window objects are identical, 1 if they are
167471** different, or 2 if it cannot be determined if the objects are identical
167472** or not. Identical window objects can be processed in a single scan.
167473*/
167474SQLITE_PRIVATE int sqlite3WindowCompare(
167475 const Parse *pParse,
167476 const Window *p1,
167477 const Window *p2,
167478 int bFilter
167479){
167480 int res;
167481 if( NEVER(p1==0) || NEVER(p2==0) ) return 1;
167482 if( p1->eFrmType!=p2->eFrmType ) return 1;
167483 if( p1->eStart!=p2->eStart ) return 1;
167484 if( p1->eEnd!=p2->eEnd ) return 1;
167485 if( p1->eExclude!=p2->eExclude ) return 1;
167486 if( sqlite3ExprCompare(pParse, pA: p1->pStart, pB: p2->pStart, iTab: -1) ) return 1;
167487 if( sqlite3ExprCompare(pParse, pA: p1->pEnd, pB: p2->pEnd, iTab: -1) ) return 1;
167488 if( (res = sqlite3ExprListCompare(pA: p1->pPartition, pB: p2->pPartition, iTab: -1)) ){
167489 return res;
167490 }
167491 if( (res = sqlite3ExprListCompare(pA: p1->pOrderBy, pB: p2->pOrderBy, iTab: -1)) ){
167492 return res;
167493 }
167494 if( bFilter ){
167495 if( (res = sqlite3ExprCompare(pParse, pA: p1->pFilter, pB: p2->pFilter, iTab: -1)) ){
167496 return res;
167497 }
167498 }
167499 return 0;
167500}
167501
167502
167503/*
167504** This is called by code in select.c before it calls sqlite3WhereBegin()
167505** to begin iterating through the sub-query results. It is used to allocate
167506** and initialize registers and cursors used by sqlite3WindowCodeStep().
167507*/
167508SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
167509 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
167510 Window *pMWin = pSelect->pWin;
167511 Window *pWin;
167512 Vdbe *v = sqlite3GetVdbe(pParse);
167513
167514 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pMWin->iEphCsr, p2: nEphExpr);
167515 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->iEphCsr+1, p2: pMWin->iEphCsr);
167516 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->iEphCsr+2, p2: pMWin->iEphCsr);
167517 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->iEphCsr+3, p2: pMWin->iEphCsr);
167518
167519 /* Allocate registers to use for PARTITION BY values, if any. Initialize
167520 ** said registers to NULL. */
167521 if( pMWin->pPartition ){
167522 int nExpr = pMWin->pPartition->nExpr;
167523 pMWin->regPart = pParse->nMem+1;
167524 pParse->nMem += nExpr;
167525 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: pMWin->regPart, p3: pMWin->regPart+nExpr-1);
167526 }
167527
167528 pMWin->regOne = ++pParse->nMem;
167529 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pMWin->regOne);
167530
167531 if( pMWin->eExclude ){
167532 pMWin->regStartRowid = ++pParse->nMem;
167533 pMWin->regEndRowid = ++pParse->nMem;
167534 pMWin->csrApp = pParse->nTab++;
167535 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pMWin->regStartRowid);
167536 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pMWin->regEndRowid);
167537 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->csrApp, p2: pMWin->iEphCsr);
167538 return;
167539 }
167540
167541 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
167542 FuncDef *p = pWin->pWFunc;
167543 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
167544 /* The inline versions of min() and max() require a single ephemeral
167545 ** table and 3 registers. The registers are used as follows:
167546 **
167547 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
167548 ** regApp+1: integer value used to ensure keys are unique
167549 ** regApp+2: output of MakeRecord
167550 */
167551 ExprList *pList;
167552 KeyInfo *pKeyInfo;
167553 assert( ExprUseXList(pWin->pOwner) );
167554 pList = pWin->pOwner->x.pList;
167555 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, iStart: 0, nExtra: 0);
167556 pWin->csrApp = pParse->nTab++;
167557 pWin->regApp = pParse->nMem+1;
167558 pParse->nMem += 3;
167559 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
167560 assert( pKeyInfo->aSortFlags[0]==0 );
167561 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
167562 }
167563 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pWin->csrApp, p2: 2);
167564 sqlite3VdbeAppendP4(p: v, pP4: pKeyInfo, P4_KEYINFO);
167565 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp+1);
167566 }
167567 else if( p->zName==nth_valueName || p->zName==first_valueName ){
167568 /* Allocate two registers at pWin->regApp. These will be used to
167569 ** store the start and end index of the current frame. */
167570 pWin->regApp = pParse->nMem+1;
167571 pWin->csrApp = pParse->nTab++;
167572 pParse->nMem += 2;
167573 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pWin->csrApp, p2: pMWin->iEphCsr);
167574 }
167575 else if( p->zName==leadName || p->zName==lagName ){
167576 pWin->csrApp = pParse->nTab++;
167577 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pWin->csrApp, p2: pMWin->iEphCsr);
167578 }
167579 }
167580}
167581
167582#define WINDOW_STARTING_INT 0
167583#define WINDOW_ENDING_INT 1
167584#define WINDOW_NTH_VALUE_INT 2
167585#define WINDOW_STARTING_NUM 3
167586#define WINDOW_ENDING_NUM 4
167587
167588/*
167589** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
167590** value of the second argument to nth_value() (eCond==2) has just been
167591** evaluated and the result left in register reg. This function generates VM
167592** code to check that the value is a non-negative integer and throws an
167593** exception if it is not.
167594*/
167595static void windowCheckValue(Parse *pParse, int reg, int eCond){
167596 static const char *azErr[] = {
167597 "frame starting offset must be a non-negative integer",
167598 "frame ending offset must be a non-negative integer",
167599 "second argument to nth_value must be a positive integer",
167600 "frame starting offset must be a non-negative number",
167601 "frame ending offset must be a non-negative number",
167602 };
167603 static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
167604 Vdbe *v = sqlite3GetVdbe(pParse);
167605 int regZero = sqlite3GetTempReg(pParse);
167606 assert( eCond>=0 && eCond<ArraySize(azErr) );
167607 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regZero);
167608 if( eCond>=WINDOW_STARTING_NUM ){
167609 int regString = sqlite3GetTempReg(pParse);
167610 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: regString, p3: 0, zP4: "", P4_STATIC);
167611 sqlite3VdbeAddOp3(p: v, OP_Ge, p1: regString, p2: sqlite3VdbeCurrentAddr(p: v)+2, p3: reg);
167612 sqlite3VdbeChangeP5(p: v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
167613 VdbeCoverage(v);
167614 assert( eCond==3 || eCond==4 );
167615 VdbeCoverageIf(v, eCond==3);
167616 VdbeCoverageIf(v, eCond==4);
167617 }else{
167618 sqlite3VdbeAddOp2(p: v, OP_MustBeInt, p1: reg, p2: sqlite3VdbeCurrentAddr(p: v)+2);
167619 VdbeCoverage(v);
167620 assert( eCond==0 || eCond==1 || eCond==2 );
167621 VdbeCoverageIf(v, eCond==0);
167622 VdbeCoverageIf(v, eCond==1);
167623 VdbeCoverageIf(v, eCond==2);
167624 }
167625 sqlite3VdbeAddOp3(p: v, op: aOp[eCond], p1: regZero, p2: sqlite3VdbeCurrentAddr(p: v)+2, p3: reg);
167626 sqlite3VdbeChangeP5(p: v, SQLITE_AFF_NUMERIC);
167627 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
167628 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
167629 VdbeCoverageNeverNullIf(v, eCond==2);
167630 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
167631 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
167632 sqlite3MayAbort(pParse);
167633 sqlite3VdbeAddOp2(p: v, OP_Halt, SQLITE_ERROR, OE_Abort);
167634 sqlite3VdbeAppendP4(p: v, pP4: (void*)azErr[eCond], P4_STATIC);
167635 sqlite3ReleaseTempReg(pParse, iReg: regZero);
167636}
167637
167638/*
167639** Return the number of arguments passed to the window-function associated
167640** with the object passed as the only argument to this function.
167641*/
167642static int windowArgCount(Window *pWin){
167643 const ExprList *pList;
167644 assert( ExprUseXList(pWin->pOwner) );
167645 pList = pWin->pOwner->x.pList;
167646 return (pList ? pList->nExpr : 0);
167647}
167648
167649typedef struct WindowCodeArg WindowCodeArg;
167650typedef struct WindowCsrAndReg WindowCsrAndReg;
167651
167652/*
167653** See comments above struct WindowCodeArg.
167654*/
167655struct WindowCsrAndReg {
167656 int csr; /* Cursor number */
167657 int reg; /* First in array of peer values */
167658};
167659
167660/*
167661** A single instance of this structure is allocated on the stack by
167662** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
167663** routines. This is to reduce the number of arguments required by each
167664** helper function.
167665**
167666** regArg:
167667** Each window function requires an accumulator register (just as an
167668** ordinary aggregate function does). This variable is set to the first
167669** in an array of accumulator registers - one for each window function
167670** in the WindowCodeArg.pMWin list.
167671**
167672** eDelete:
167673** The window functions implementation sometimes caches the input rows
167674** that it processes in a temporary table. If it is not zero, this
167675** variable indicates when rows may be removed from the temp table (in
167676** order to reduce memory requirements - it would always be safe just
167677** to leave them there). Possible values for eDelete are:
167678**
167679** WINDOW_RETURN_ROW:
167680** An input row can be discarded after it is returned to the caller.
167681**
167682** WINDOW_AGGINVERSE:
167683** An input row can be discarded after the window functions xInverse()
167684** callbacks have been invoked in it.
167685**
167686** WINDOW_AGGSTEP:
167687** An input row can be discarded after the window functions xStep()
167688** callbacks have been invoked in it.
167689**
167690** start,current,end
167691** Consider a window-frame similar to the following:
167692**
167693** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
167694**
167695** The windows functions implementation caches the input rows in a temp
167696** table, sorted by "a, b" (it actually populates the cache lazily, and
167697** aggressively removes rows once they are no longer required, but that's
167698** a mere detail). It keeps three cursors open on the temp table. One
167699** (current) that points to the next row to return to the query engine
167700** once its window function values have been calculated. Another (end)
167701** points to the next row to call the xStep() method of each window function
167702** on (so that it is 2 groups ahead of current). And a third (start) that
167703** points to the next row to call the xInverse() method of each window
167704** function on.
167705**
167706** Each cursor (start, current and end) consists of a VDBE cursor
167707** (WindowCsrAndReg.csr) and an array of registers (starting at
167708** WindowCodeArg.reg) that always contains a copy of the peer values
167709** read from the corresponding cursor.
167710**
167711** Depending on the window-frame in question, all three cursors may not
167712** be required. In this case both WindowCodeArg.csr and reg are set to
167713** 0.
167714*/
167715struct WindowCodeArg {
167716 Parse *pParse; /* Parse context */
167717 Window *pMWin; /* First in list of functions being processed */
167718 Vdbe *pVdbe; /* VDBE object */
167719 int addrGosub; /* OP_Gosub to this address to return one row */
167720 int regGosub; /* Register used with OP_Gosub(addrGosub) */
167721 int regArg; /* First in array of accumulator registers */
167722 int eDelete; /* See above */
167723 int regRowid;
167724
167725 WindowCsrAndReg start;
167726 WindowCsrAndReg current;
167727 WindowCsrAndReg end;
167728};
167729
167730/*
167731** Generate VM code to read the window frames peer values from cursor csr into
167732** an array of registers starting at reg.
167733*/
167734static void windowReadPeerValues(
167735 WindowCodeArg *p,
167736 int csr,
167737 int reg
167738){
167739 Window *pMWin = p->pMWin;
167740 ExprList *pOrderBy = pMWin->pOrderBy;
167741 if( pOrderBy ){
167742 Vdbe *v = sqlite3GetVdbe(pParse: p->pParse);
167743 ExprList *pPart = pMWin->pPartition;
167744 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
167745 int i;
167746 for(i=0; i<pOrderBy->nExpr; i++){
167747 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: iColOff+i, p3: reg+i);
167748 }
167749 }
167750}
167751
167752/*
167753** Generate VM code to invoke either xStep() (if bInverse is 0) or
167754** xInverse (if bInverse is non-zero) for each window function in the
167755** linked list starting at pMWin. Or, for built-in window functions
167756** that do not use the standard function API, generate the required
167757** inline VM code.
167758**
167759** If argument csr is greater than or equal to 0, then argument reg is
167760** the first register in an array of registers guaranteed to be large
167761** enough to hold the array of arguments for each function. In this case
167762** the arguments are extracted from the current row of csr into the
167763** array of registers before invoking OP_AggStep or OP_AggInverse
167764**
167765** Or, if csr is less than zero, then the array of registers at reg is
167766** already populated with all columns from the current row of the sub-query.
167767**
167768** If argument regPartSize is non-zero, then it is a register containing the
167769** number of rows in the current partition.
167770*/
167771static void windowAggStep(
167772 WindowCodeArg *p,
167773 Window *pMWin, /* Linked list of window functions */
167774 int csr, /* Read arguments from this cursor */
167775 int bInverse, /* True to invoke xInverse instead of xStep */
167776 int reg /* Array of registers */
167777){
167778 Parse *pParse = p->pParse;
167779 Vdbe *v = sqlite3GetVdbe(pParse);
167780 Window *pWin;
167781 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
167782 FuncDef *pFunc = pWin->pWFunc;
167783 int regArg;
167784 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
167785 int i;
167786
167787 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
167788
167789 /* All OVER clauses in the same window function aggregate step must
167790 ** be the same. */
167791 assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 );
167792
167793 for(i=0; i<nArg; i++){
167794 if( i!=1 || pFunc->zName!=nth_valueName ){
167795 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol+i, p3: reg+i);
167796 }else{
167797 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pMWin->iEphCsr, p2: pWin->iArgCol+i, p3: reg+i);
167798 }
167799 }
167800 regArg = reg;
167801
167802 if( pMWin->regStartRowid==0
167803 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
167804 && (pWin->eStart!=TK_UNBOUNDED)
167805 ){
167806 int addrIsNull = sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: regArg);
167807 VdbeCoverage(v);
167808 if( bInverse==0 ){
167809 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pWin->regApp+1, p2: 1);
167810 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: regArg, p2: pWin->regApp);
167811 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: pWin->regApp, p2: 2, p3: pWin->regApp+2);
167812 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: pWin->csrApp, p2: pWin->regApp+2);
167813 }else{
167814 sqlite3VdbeAddOp4Int(p: v, OP_SeekGE, p1: pWin->csrApp, p2: 0, p3: regArg, p4: 1);
167815 VdbeCoverageNeverTaken(v);
167816 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: pWin->csrApp);
167817 sqlite3VdbeJumpHere(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-2);
167818 }
167819 sqlite3VdbeJumpHere(p: v, addr: addrIsNull);
167820 }else if( pWin->regApp ){
167821 assert( pFunc->zName==nth_valueName
167822 || pFunc->zName==first_valueName
167823 );
167824 assert( bInverse==0 || bInverse==1 );
167825 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pWin->regApp+1-bInverse, p2: 1);
167826 }else if( pFunc->xSFunc!=noopStepFunc ){
167827 int addrIf = 0;
167828 if( pWin->pFilter ){
167829 int regTmp;
167830 assert( ExprUseXList(pWin->pOwner) );
167831 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
167832 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
167833 regTmp = sqlite3GetTempReg(pParse);
167834 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol+nArg,p3: regTmp);
167835 addrIf = sqlite3VdbeAddOp3(p: v, OP_IfNot, p1: regTmp, p2: 0, p3: 1);
167836 VdbeCoverage(v);
167837 sqlite3ReleaseTempReg(pParse, iReg: regTmp);
167838 }
167839
167840 if( pWin->bExprArgs ){
167841 int iOp = sqlite3VdbeCurrentAddr(p: v);
167842 int iEnd;
167843
167844 assert( ExprUseXList(pWin->pOwner) );
167845 nArg = pWin->pOwner->x.pList->nExpr;
167846 regArg = sqlite3GetTempRange(pParse, nReg: nArg);
167847 sqlite3ExprCodeExprList(pParse, pList: pWin->pOwner->x.pList, target: regArg, srcReg: 0, flags: 0);
167848
167849 for(iEnd=sqlite3VdbeCurrentAddr(p: v); iOp<iEnd; iOp++){
167850 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: iOp);
167851 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
167852 pOp->p1 = csr;
167853 }
167854 }
167855 }
167856 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
167857 CollSeq *pColl;
167858 assert( nArg>0 );
167859 assert( ExprUseXList(pWin->pOwner) );
167860 pColl = sqlite3ExprNNCollSeq(pParse, pExpr: pWin->pOwner->x.pList->a[0].pExpr);
167861 sqlite3VdbeAddOp4(p: v, OP_CollSeq, p1: 0,p2: 0,p3: 0, zP4: (const char*)pColl, P4_COLLSEQ);
167862 }
167863 sqlite3VdbeAddOp3(p: v, op: bInverse? OP_AggInverse : OP_AggStep,
167864 p1: bInverse, p2: regArg, p3: pWin->regAccum);
167865 sqlite3VdbeAppendP4(p: v, pP4: pFunc, P4_FUNCDEF);
167866 sqlite3VdbeChangeP5(p: v, p5: (u8)nArg);
167867 if( pWin->bExprArgs ){
167868 sqlite3ReleaseTempRange(pParse, iReg: regArg, nReg: nArg);
167869 }
167870 if( addrIf ) sqlite3VdbeJumpHere(p: v, addr: addrIf);
167871 }
167872 }
167873}
167874
167875/*
167876** Values that may be passed as the second argument to windowCodeOp().
167877*/
167878#define WINDOW_RETURN_ROW 1
167879#define WINDOW_AGGINVERSE 2
167880#define WINDOW_AGGSTEP 3
167881
167882/*
167883** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
167884** (bFin==1) for each window function in the linked list starting at
167885** pMWin. Or, for built-in window-functions that do not use the standard
167886** API, generate the equivalent VM code.
167887*/
167888static void windowAggFinal(WindowCodeArg *p, int bFin){
167889 Parse *pParse = p->pParse;
167890 Window *pMWin = p->pMWin;
167891 Vdbe *v = sqlite3GetVdbe(pParse);
167892 Window *pWin;
167893
167894 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
167895 if( pMWin->regStartRowid==0
167896 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
167897 && (pWin->eStart!=TK_UNBOUNDED)
167898 ){
167899 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regResult);
167900 sqlite3VdbeAddOp1(p: v, OP_Last, p1: pWin->csrApp);
167901 VdbeCoverage(v);
167902 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pWin->csrApp, p2: 0, p3: pWin->regResult);
167903 sqlite3VdbeJumpHere(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-2);
167904 }else if( pWin->regApp ){
167905 assert( pMWin->regStartRowid==0 );
167906 }else{
167907 int nArg = windowArgCount(pWin);
167908 if( bFin ){
167909 sqlite3VdbeAddOp2(p: v, OP_AggFinal, p1: pWin->regAccum, p2: nArg);
167910 sqlite3VdbeAppendP4(p: v, pP4: pWin->pWFunc, P4_FUNCDEF);
167911 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: pWin->regAccum, p2: pWin->regResult);
167912 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
167913 }else{
167914 sqlite3VdbeAddOp3(p: v, OP_AggValue,p1: pWin->regAccum,p2: nArg,p3: pWin->regResult);
167915 sqlite3VdbeAppendP4(p: v, pP4: pWin->pWFunc, P4_FUNCDEF);
167916 }
167917 }
167918 }
167919}
167920
167921/*
167922** Generate code to calculate the current values of all window functions in the
167923** p->pMWin list by doing a full scan of the current window frame. Store the
167924** results in the Window.regResult registers, ready to return the upper
167925** layer.
167926*/
167927static void windowFullScan(WindowCodeArg *p){
167928 Window *pWin;
167929 Parse *pParse = p->pParse;
167930 Window *pMWin = p->pMWin;
167931 Vdbe *v = p->pVdbe;
167932
167933 int regCRowid = 0; /* Current rowid value */
167934 int regCPeer = 0; /* Current peer values */
167935 int regRowid = 0; /* AggStep rowid value */
167936 int regPeer = 0; /* AggStep peer values */
167937
167938 int nPeer;
167939 int lblNext;
167940 int lblBrk;
167941 int addrNext;
167942 int csr;
167943
167944 VdbeModuleComment((v, "windowFullScan begin"));
167945
167946 assert( pMWin!=0 );
167947 csr = pMWin->csrApp;
167948 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
167949
167950 lblNext = sqlite3VdbeMakeLabel(pParse);
167951 lblBrk = sqlite3VdbeMakeLabel(pParse);
167952
167953 regCRowid = sqlite3GetTempReg(pParse);
167954 regRowid = sqlite3GetTempReg(pParse);
167955 if( nPeer ){
167956 regCPeer = sqlite3GetTempRange(pParse, nReg: nPeer);
167957 regPeer = sqlite3GetTempRange(pParse, nReg: nPeer);
167958 }
167959
167960 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: pMWin->iEphCsr, p2: regCRowid);
167961 windowReadPeerValues(p, csr: pMWin->iEphCsr, reg: regCPeer);
167962
167963 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
167964 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
167965 }
167966
167967 sqlite3VdbeAddOp3(p: v, OP_SeekGE, p1: csr, p2: lblBrk, p3: pMWin->regStartRowid);
167968 VdbeCoverage(v);
167969 addrNext = sqlite3VdbeCurrentAddr(p: v);
167970 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: csr, p2: regRowid);
167971 sqlite3VdbeAddOp3(p: v, OP_Gt, p1: pMWin->regEndRowid, p2: lblBrk, p3: regRowid);
167972 VdbeCoverageNeverNull(v);
167973
167974 if( pMWin->eExclude==TK_CURRENT ){
167975 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regCRowid, p2: lblNext, p3: regRowid);
167976 VdbeCoverageNeverNull(v);
167977 }else if( pMWin->eExclude!=TK_NO ){
167978 int addr;
167979 int addrEq = 0;
167980 KeyInfo *pKeyInfo = 0;
167981
167982 if( pMWin->pOrderBy ){
167983 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pMWin->pOrderBy, iStart: 0, nExtra: 0);
167984 }
167985 if( pMWin->eExclude==TK_TIES ){
167986 addrEq = sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regCRowid, p2: 0, p3: regRowid);
167987 VdbeCoverageNeverNull(v);
167988 }
167989 if( pKeyInfo ){
167990 windowReadPeerValues(p, csr, reg: regPeer);
167991 sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regPeer, p2: regCPeer, p3: nPeer);
167992 sqlite3VdbeAppendP4(p: v, pP4: (void*)pKeyInfo, P4_KEYINFO);
167993 addr = sqlite3VdbeCurrentAddr(p: v)+1;
167994 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr, p2: lblNext, p3: addr);
167995 VdbeCoverageEqNe(v);
167996 }else{
167997 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblNext);
167998 }
167999 if( addrEq ) sqlite3VdbeJumpHere(p: v, addr: addrEq);
168000 }
168001
168002 windowAggStep(p, pMWin, csr, bInverse: 0, reg: p->regArg);
168003
168004 sqlite3VdbeResolveLabel(v, x: lblNext);
168005 sqlite3VdbeAddOp2(p: v, OP_Next, p1: csr, p2: addrNext);
168006 VdbeCoverage(v);
168007 sqlite3VdbeJumpHere(p: v, addr: addrNext-1);
168008 sqlite3VdbeJumpHere(p: v, addr: addrNext+1);
168009 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
168010 sqlite3ReleaseTempReg(pParse, iReg: regCRowid);
168011 if( nPeer ){
168012 sqlite3ReleaseTempRange(pParse, iReg: regPeer, nReg: nPeer);
168013 sqlite3ReleaseTempRange(pParse, iReg: regCPeer, nReg: nPeer);
168014 }
168015
168016 windowAggFinal(p, bFin: 1);
168017 VdbeModuleComment((v, "windowFullScan end"));
168018}
168019
168020/*
168021** Invoke the sub-routine at regGosub (generated by code in select.c) to
168022** return the current row of Window.iEphCsr. If all window functions are
168023** aggregate window functions that use the standard API, a single
168024** OP_Gosub instruction is all that this routine generates. Extra VM code
168025** for per-row processing is only generated for the following built-in window
168026** functions:
168027**
168028** nth_value()
168029** first_value()
168030** lag()
168031** lead()
168032*/
168033static void windowReturnOneRow(WindowCodeArg *p){
168034 Window *pMWin = p->pMWin;
168035 Vdbe *v = p->pVdbe;
168036
168037 if( pMWin->regStartRowid ){
168038 windowFullScan(p);
168039 }else{
168040 Parse *pParse = p->pParse;
168041 Window *pWin;
168042
168043 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168044 FuncDef *pFunc = pWin->pWFunc;
168045 assert( ExprUseXList(pWin->pOwner) );
168046 if( pFunc->zName==nth_valueName
168047 || pFunc->zName==first_valueName
168048 ){
168049 int csr = pWin->csrApp;
168050 int lbl = sqlite3VdbeMakeLabel(pParse);
168051 int tmpReg = sqlite3GetTempReg(pParse);
168052 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regResult);
168053
168054 if( pFunc->zName==nth_valueName ){
168055 sqlite3VdbeAddOp3(p: v, OP_Column,p1: pMWin->iEphCsr,p2: pWin->iArgCol+1,p3: tmpReg);
168056 windowCheckValue(pParse, reg: tmpReg, eCond: 2);
168057 }else{
168058 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: tmpReg);
168059 }
168060 sqlite3VdbeAddOp3(p: v, OP_Add, p1: tmpReg, p2: pWin->regApp, p3: tmpReg);
168061 sqlite3VdbeAddOp3(p: v, OP_Gt, p1: pWin->regApp+1, p2: lbl, p3: tmpReg);
168062 VdbeCoverageNeverNull(v);
168063 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: csr, p2: 0, p3: tmpReg);
168064 VdbeCoverageNeverTaken(v);
168065 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol, p3: pWin->regResult);
168066 sqlite3VdbeResolveLabel(v, x: lbl);
168067 sqlite3ReleaseTempReg(pParse, iReg: tmpReg);
168068 }
168069 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
168070 int nArg = pWin->pOwner->x.pList->nExpr;
168071 int csr = pWin->csrApp;
168072 int lbl = sqlite3VdbeMakeLabel(pParse);
168073 int tmpReg = sqlite3GetTempReg(pParse);
168074 int iEph = pMWin->iEphCsr;
168075
168076 if( nArg<3 ){
168077 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regResult);
168078 }else{
168079 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph,p2: pWin->iArgCol+2,p3: pWin->regResult);
168080 }
168081 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iEph, p2: tmpReg);
168082 if( nArg<2 ){
168083 int val = (pFunc->zName==leadName ? 1 : -1);
168084 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: tmpReg, p2: val);
168085 }else{
168086 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
168087 int tmpReg2 = sqlite3GetTempReg(pParse);
168088 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: pWin->iArgCol+1, p3: tmpReg2);
168089 sqlite3VdbeAddOp3(p: v, op, p1: tmpReg2, p2: tmpReg, p3: tmpReg);
168090 sqlite3ReleaseTempReg(pParse, iReg: tmpReg2);
168091 }
168092
168093 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: csr, p2: lbl, p3: tmpReg);
168094 VdbeCoverage(v);
168095 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol, p3: pWin->regResult);
168096 sqlite3VdbeResolveLabel(v, x: lbl);
168097 sqlite3ReleaseTempReg(pParse, iReg: tmpReg);
168098 }
168099 }
168100 }
168101 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: p->regGosub, p2: p->addrGosub);
168102}
168103
168104/*
168105** Generate code to set the accumulator register for each window function
168106** in the linked list passed as the second argument to NULL. And perform
168107** any equivalent initialization required by any built-in window functions
168108** in the list.
168109*/
168110static int windowInitAccum(Parse *pParse, Window *pMWin){
168111 Vdbe *v = sqlite3GetVdbe(pParse);
168112 int regArg;
168113 int nArg = 0;
168114 Window *pWin;
168115 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168116 FuncDef *pFunc = pWin->pWFunc;
168117 assert( pWin->regAccum );
168118 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
168119 nArg = MAX(nArg, windowArgCount(pWin));
168120 if( pMWin->regStartRowid==0 ){
168121 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
168122 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp);
168123 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp+1);
168124 }
168125
168126 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
168127 assert( pWin->eStart!=TK_UNBOUNDED );
168128 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: pWin->csrApp);
168129 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp+1);
168130 }
168131 }
168132 }
168133 regArg = pParse->nMem+1;
168134 pParse->nMem += nArg;
168135 return regArg;
168136}
168137
168138/*
168139** Return true if the current frame should be cached in the ephemeral table,
168140** even if there are no xInverse() calls required.
168141*/
168142static int windowCacheFrame(Window *pMWin){
168143 Window *pWin;
168144 if( pMWin->regStartRowid ) return 1;
168145 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168146 FuncDef *pFunc = pWin->pWFunc;
168147 if( (pFunc->zName==nth_valueName)
168148 || (pFunc->zName==first_valueName)
168149 || (pFunc->zName==leadName)
168150 || (pFunc->zName==lagName)
168151 ){
168152 return 1;
168153 }
168154 }
168155 return 0;
168156}
168157
168158/*
168159** regOld and regNew are each the first register in an array of size
168160** pOrderBy->nExpr. This function generates code to compare the two
168161** arrays of registers using the collation sequences and other comparison
168162** parameters specified by pOrderBy.
168163**
168164** If the two arrays are not equal, the contents of regNew is copied to
168165** regOld and control falls through. Otherwise, if the contents of the arrays
168166** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
168167*/
168168static void windowIfNewPeer(
168169 Parse *pParse,
168170 ExprList *pOrderBy,
168171 int regNew, /* First in array of new values */
168172 int regOld, /* First in array of old values */
168173 int addr /* Jump here */
168174){
168175 Vdbe *v = sqlite3GetVdbe(pParse);
168176 if( pOrderBy ){
168177 int nVal = pOrderBy->nExpr;
168178 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pOrderBy, iStart: 0, nExtra: 0);
168179 sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regOld, p2: regNew, p3: nVal);
168180 sqlite3VdbeAppendP4(p: v, pP4: (void*)pKeyInfo, P4_KEYINFO);
168181 sqlite3VdbeAddOp3(p: v, OP_Jump,
168182 p1: sqlite3VdbeCurrentAddr(p: v)+1, p2: addr, p3: sqlite3VdbeCurrentAddr(p: v)+1
168183 );
168184 VdbeCoverageEqNe(v);
168185 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regNew, p2: regOld, p3: nVal-1);
168186 }else{
168187 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addr);
168188 }
168189}
168190
168191/*
168192** This function is called as part of generating VM programs for RANGE
168193** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
168194** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
168195** code equivalent to:
168196**
168197** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
168198**
168199** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
168200** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
168201**
168202** If the sort-order for the ORDER BY term in the window is DESC, then the
168203** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
168204** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
168205** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
168206** is OP_Ge, the generated code is equivalent to:
168207**
168208** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
168209**
168210** A special type of arithmetic is used such that if csr1.peerVal is not
168211** a numeric type (real or integer), then the result of the addition
168212** or subtraction is a a copy of csr1.peerVal.
168213*/
168214static void windowCodeRangeTest(
168215 WindowCodeArg *p,
168216 int op, /* OP_Ge, OP_Gt, or OP_Le */
168217 int csr1, /* Cursor number for cursor 1 */
168218 int regVal, /* Register containing non-negative number */
168219 int csr2, /* Cursor number for cursor 2 */
168220 int lbl /* Jump destination if condition is true */
168221){
168222 Parse *pParse = p->pParse;
168223 Vdbe *v = sqlite3GetVdbe(pParse);
168224 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
168225 int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */
168226 int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */
168227 int regString = ++pParse->nMem; /* Reg. for constant value '' */
168228 int arith = OP_Add; /* OP_Add or OP_Subtract */
168229 int addrGe; /* Jump destination */
168230 int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */
168231 CollSeq *pColl;
168232
168233 /* Read the peer-value from each cursor into a register */
168234 windowReadPeerValues(p, csr: csr1, reg: reg1);
168235 windowReadPeerValues(p, csr: csr2, reg: reg2);
168236
168237 assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
168238 assert( pOrderBy && pOrderBy->nExpr==1 );
168239 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
168240 switch( op ){
168241 case OP_Ge: op = OP_Le; break;
168242 case OP_Gt: op = OP_Lt; break;
168243 default: assert( op==OP_Le ); op = OP_Ge; break;
168244 }
168245 arith = OP_Subtract;
168246 }
168247
168248 VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
168249 reg1, (arith==OP_Add ? "+" : "-"), regVal,
168250 ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
168251 ));
168252
168253 /* If the BIGNULL flag is set for the ORDER BY, then it is required to
168254 ** consider NULL values to be larger than all other values, instead of
168255 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
168256 ** (and adding that capability causes a performance regression), so
168257 ** instead if the BIGNULL flag is set then cases where either reg1 or
168258 ** reg2 are NULL are handled separately in the following block. The code
168259 ** generated is equivalent to:
168260 **
168261 ** if( reg1 IS NULL ){
168262 ** if( op==OP_Ge ) goto lbl;
168263 ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
168264 ** if( op==OP_Le && reg2 IS NULL ) goto lbl;
168265 ** }else if( reg2 IS NULL ){
168266 ** if( op==OP_Le ) goto lbl;
168267 ** }
168268 **
168269 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
168270 ** not taken, control jumps over the comparison operator coded below this
168271 ** block. */
168272 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
168273 /* This block runs if reg1 contains a NULL. */
168274 int addr = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: reg1); VdbeCoverage(v);
168275 switch( op ){
168276 case OP_Ge:
168277 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lbl);
168278 break;
168279 case OP_Gt:
168280 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: reg2, p2: lbl);
168281 VdbeCoverage(v);
168282 break;
168283 case OP_Le:
168284 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: reg2, p2: lbl);
168285 VdbeCoverage(v);
168286 break;
168287 default: assert( op==OP_Lt ); /* no-op */ break;
168288 }
168289 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrDone);
168290
168291 /* This block runs if reg1 is not NULL, but reg2 is. */
168292 sqlite3VdbeJumpHere(p: v, addr);
168293 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: reg2,
168294 p2: (op==OP_Gt || op==OP_Ge) ? addrDone : lbl);
168295 VdbeCoverage(v);
168296 }
168297
168298 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
168299 ** This block adds (or subtracts for DESC) the numeric value in regVal
168300 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
168301 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
168302 **
168303 ** if( reg1>='' ) goto addrGe;
168304 ** reg1 = reg1 +/- regVal
168305 ** addrGe:
168306 **
168307 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
168308 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
168309 ** then the arithmetic is performed, but since adding or subtracting from
168310 ** NULL is always NULL anyway, this case is handled as required too. */
168311 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: regString, p3: 0, zP4: "", P4_STATIC);
168312 addrGe = sqlite3VdbeAddOp3(p: v, OP_Ge, p1: regString, p2: 0, p3: reg1);
168313 VdbeCoverage(v);
168314 if( (op==OP_Ge && arith==OP_Add) || (op==OP_Le && arith==OP_Subtract) ){
168315 sqlite3VdbeAddOp3(p: v, op, p1: reg2, p2: lbl, p3: reg1); VdbeCoverage(v);
168316 }
168317 sqlite3VdbeAddOp3(p: v, op: arith, p1: regVal, p2: reg1, p3: reg1);
168318 sqlite3VdbeJumpHere(p: v, addr: addrGe);
168319
168320 /* Compare registers reg2 and reg1, taking the jump if required. Note that
168321 ** control skips over this test if the BIGNULL flag is set and either
168322 ** reg1 or reg2 contain a NULL value. */
168323 sqlite3VdbeAddOp3(p: v, op, p1: reg2, p2: lbl, p3: reg1); VdbeCoverage(v);
168324 pColl = sqlite3ExprNNCollSeq(pParse, pExpr: pOrderBy->a[0].pExpr);
168325 sqlite3VdbeAppendP4(p: v, pP4: (void*)pColl, P4_COLLSEQ);
168326 sqlite3VdbeChangeP5(p: v, SQLITE_NULLEQ);
168327 sqlite3VdbeResolveLabel(v, x: addrDone);
168328
168329 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
168330 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
168331 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
168332 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
168333 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
168334 sqlite3ReleaseTempReg(pParse, iReg: reg1);
168335 sqlite3ReleaseTempReg(pParse, iReg: reg2);
168336
168337 VdbeModuleComment((v, "CodeRangeTest: end"));
168338}
168339
168340/*
168341** Helper function for sqlite3WindowCodeStep(). Each call to this function
168342** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
168343** operation. Refer to the header comment for sqlite3WindowCodeStep() for
168344** details.
168345*/
168346static int windowCodeOp(
168347 WindowCodeArg *p, /* Context object */
168348 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
168349 int regCountdown, /* Register for OP_IfPos countdown */
168350 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
168351){
168352 int csr, reg;
168353 Parse *pParse = p->pParse;
168354 Window *pMWin = p->pMWin;
168355 int ret = 0;
168356 Vdbe *v = p->pVdbe;
168357 int addrContinue = 0;
168358 int bPeer = (pMWin->eFrmType!=TK_ROWS);
168359
168360 int lblDone = sqlite3VdbeMakeLabel(pParse);
168361 int addrNextRange = 0;
168362
168363 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
168364 ** starts with UNBOUNDED PRECEDING. */
168365 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
168366 assert( regCountdown==0 && jumpOnEof==0 );
168367 return 0;
168368 }
168369
168370 if( regCountdown>0 ){
168371 if( pMWin->eFrmType==TK_RANGE ){
168372 addrNextRange = sqlite3VdbeCurrentAddr(p: v);
168373 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
168374 if( op==WINDOW_AGGINVERSE ){
168375 if( pMWin->eStart==TK_FOLLOWING ){
168376 windowCodeRangeTest(
168377 p, OP_Le, csr1: p->current.csr, regVal: regCountdown, csr2: p->start.csr, lbl: lblDone
168378 );
168379 }else{
168380 windowCodeRangeTest(
168381 p, OP_Ge, csr1: p->start.csr, regVal: regCountdown, csr2: p->current.csr, lbl: lblDone
168382 );
168383 }
168384 }else{
168385 windowCodeRangeTest(
168386 p, OP_Gt, csr1: p->end.csr, regVal: regCountdown, csr2: p->current.csr, lbl: lblDone
168387 );
168388 }
168389 }else{
168390 sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: regCountdown, p2: lblDone, p3: 1);
168391 VdbeCoverage(v);
168392 }
168393 }
168394
168395 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
168396 windowAggFinal(p, bFin: 0);
168397 }
168398 addrContinue = sqlite3VdbeCurrentAddr(p: v);
168399
168400 /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
168401 ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
168402 ** start cursor does not advance past the end cursor within the
168403 ** temporary table. It otherwise might, if (a>b). Also ensure that,
168404 ** if the input cursor is still finding new rows, that the end
168405 ** cursor does not go past it to EOF. */
168406 if( pMWin->eStart==pMWin->eEnd && regCountdown
168407 && pMWin->eFrmType==TK_RANGE
168408 ){
168409 int regRowid1 = sqlite3GetTempReg(pParse);
168410 int regRowid2 = sqlite3GetTempReg(pParse);
168411 if( op==WINDOW_AGGINVERSE ){
168412 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: p->start.csr, p2: regRowid1);
168413 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: p->end.csr, p2: regRowid2);
168414 sqlite3VdbeAddOp3(p: v, OP_Ge, p1: regRowid2, p2: lblDone, p3: regRowid1);
168415 VdbeCoverage(v);
168416 }else if( p->regRowid ){
168417 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: p->end.csr, p2: regRowid1);
168418 sqlite3VdbeAddOp3(p: v, OP_Ge, p1: p->regRowid, p2: lblDone, p3: regRowid1);
168419 VdbeCoverageNeverNull(v);
168420 }
168421 sqlite3ReleaseTempReg(pParse, iReg: regRowid1);
168422 sqlite3ReleaseTempReg(pParse, iReg: regRowid2);
168423 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
168424 }
168425
168426 switch( op ){
168427 case WINDOW_RETURN_ROW:
168428 csr = p->current.csr;
168429 reg = p->current.reg;
168430 windowReturnOneRow(p);
168431 break;
168432
168433 case WINDOW_AGGINVERSE:
168434 csr = p->start.csr;
168435 reg = p->start.reg;
168436 if( pMWin->regStartRowid ){
168437 assert( pMWin->regEndRowid );
168438 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pMWin->regStartRowid, p2: 1);
168439 }else{
168440 windowAggStep(p, pMWin, csr, bInverse: 1, reg: p->regArg);
168441 }
168442 break;
168443
168444 default:
168445 assert( op==WINDOW_AGGSTEP );
168446 csr = p->end.csr;
168447 reg = p->end.reg;
168448 if( pMWin->regStartRowid ){
168449 assert( pMWin->regEndRowid );
168450 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pMWin->regEndRowid, p2: 1);
168451 }else{
168452 windowAggStep(p, pMWin, csr, bInverse: 0, reg: p->regArg);
168453 }
168454 break;
168455 }
168456
168457 if( op==p->eDelete ){
168458 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: csr);
168459 sqlite3VdbeChangeP5(p: v, OPFLAG_SAVEPOSITION);
168460 }
168461
168462 if( jumpOnEof ){
168463 sqlite3VdbeAddOp2(p: v, OP_Next, p1: csr, p2: sqlite3VdbeCurrentAddr(p: v)+2);
168464 VdbeCoverage(v);
168465 ret = sqlite3VdbeAddOp0(p: v, OP_Goto);
168466 }else{
168467 sqlite3VdbeAddOp2(p: v, OP_Next, p1: csr, p2: sqlite3VdbeCurrentAddr(p: v)+1+bPeer);
168468 VdbeCoverage(v);
168469 if( bPeer ){
168470 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblDone);
168471 }
168472 }
168473
168474 if( bPeer ){
168475 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
168476 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
168477 windowReadPeerValues(p, csr, reg: regTmp);
168478 windowIfNewPeer(pParse, pOrderBy: pMWin->pOrderBy, regNew: regTmp, regOld: reg, addr: addrContinue);
168479 sqlite3ReleaseTempRange(pParse, iReg: regTmp, nReg);
168480 }
168481
168482 if( addrNextRange ){
168483 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrNextRange);
168484 }
168485 sqlite3VdbeResolveLabel(v, x: lblDone);
168486 return ret;
168487}
168488
168489
168490/*
168491** Allocate and return a duplicate of the Window object indicated by the
168492** third argument. Set the Window.pOwner field of the new object to
168493** pOwner.
168494*/
168495SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
168496 Window *pNew = 0;
168497 if( ALWAYS(p) ){
168498 pNew = sqlite3DbMallocZero(db, n: sizeof(Window));
168499 if( pNew ){
168500 pNew->zName = sqlite3DbStrDup(db, z: p->zName);
168501 pNew->zBase = sqlite3DbStrDup(db, z: p->zBase);
168502 pNew->pFilter = sqlite3ExprDup(db, p: p->pFilter, flags: 0);
168503 pNew->pWFunc = p->pWFunc;
168504 pNew->pPartition = sqlite3ExprListDup(db, p: p->pPartition, flags: 0);
168505 pNew->pOrderBy = sqlite3ExprListDup(db, p: p->pOrderBy, flags: 0);
168506 pNew->eFrmType = p->eFrmType;
168507 pNew->eEnd = p->eEnd;
168508 pNew->eStart = p->eStart;
168509 pNew->eExclude = p->eExclude;
168510 pNew->regResult = p->regResult;
168511 pNew->regAccum = p->regAccum;
168512 pNew->iArgCol = p->iArgCol;
168513 pNew->iEphCsr = p->iEphCsr;
168514 pNew->bExprArgs = p->bExprArgs;
168515 pNew->pStart = sqlite3ExprDup(db, p: p->pStart, flags: 0);
168516 pNew->pEnd = sqlite3ExprDup(db, p: p->pEnd, flags: 0);
168517 pNew->pOwner = pOwner;
168518 pNew->bImplicitFrame = p->bImplicitFrame;
168519 }
168520 }
168521 return pNew;
168522}
168523
168524/*
168525** Return a copy of the linked list of Window objects passed as the
168526** second argument.
168527*/
168528SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
168529 Window *pWin;
168530 Window *pRet = 0;
168531 Window **pp = &pRet;
168532
168533 for(pWin=p; pWin; pWin=pWin->pNextWin){
168534 *pp = sqlite3WindowDup(db, pOwner: 0, p: pWin);
168535 if( *pp==0 ) break;
168536 pp = &((*pp)->pNextWin);
168537 }
168538
168539 return pRet;
168540}
168541
168542/*
168543** Return true if it can be determined at compile time that expression
168544** pExpr evaluates to a value that, when cast to an integer, is greater
168545** than zero. False otherwise.
168546**
168547** If an OOM error occurs, this function sets the Parse.db.mallocFailed
168548** flag and returns zero.
168549*/
168550static int windowExprGtZero(Parse *pParse, Expr *pExpr){
168551 int ret = 0;
168552 sqlite3 *db = pParse->db;
168553 sqlite3_value *pVal = 0;
168554 sqlite3ValueFromExpr(db, pExpr, enc: db->enc, SQLITE_AFF_NUMERIC, ppVal: &pVal);
168555 if( pVal && sqlite3_value_int(pVal)>0 ){
168556 ret = 1;
168557 }
168558 sqlite3ValueFree(v: pVal);
168559 return ret;
168560}
168561
168562/*
168563** sqlite3WhereBegin() has already been called for the SELECT statement
168564** passed as the second argument when this function is invoked. It generates
168565** code to populate the Window.regResult register for each window function
168566** and invoke the sub-routine at instruction addrGosub once for each row.
168567** sqlite3WhereEnd() is always called before returning.
168568**
168569** This function handles several different types of window frames, which
168570** require slightly different processing. The following pseudo code is
168571** used to implement window frames of the form:
168572**
168573** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
168574**
168575** Other window frame types use variants of the following:
168576**
168577** ... loop started by sqlite3WhereBegin() ...
168578** if( new partition ){
168579** Gosub flush
168580** }
168581** Insert new row into eph table.
168582**
168583** if( first row of partition ){
168584** // Rewind three cursors, all open on the eph table.
168585** Rewind(csrEnd);
168586** Rewind(csrStart);
168587** Rewind(csrCurrent);
168588**
168589** regEnd = <expr2> // FOLLOWING expression
168590** regStart = <expr1> // PRECEDING expression
168591** }else{
168592** // First time this branch is taken, the eph table contains two
168593** // rows. The first row in the partition, which all three cursors
168594** // currently point to, and the following row.
168595** AGGSTEP
168596** if( (regEnd--)<=0 ){
168597** RETURN_ROW
168598** if( (regStart--)<=0 ){
168599** AGGINVERSE
168600** }
168601** }
168602** }
168603** }
168604** flush:
168605** AGGSTEP
168606** while( 1 ){
168607** RETURN ROW
168608** if( csrCurrent is EOF ) break;
168609** if( (regStart--)<=0 ){
168610** AggInverse(csrStart)
168611** Next(csrStart)
168612** }
168613** }
168614**
168615** The pseudo-code above uses the following shorthand:
168616**
168617** AGGSTEP: invoke the aggregate xStep() function for each window function
168618** with arguments read from the current row of cursor csrEnd, then
168619** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
168620**
168621** RETURN_ROW: return a row to the caller based on the contents of the
168622** current row of csrCurrent and the current state of all
168623** aggregates. Then step cursor csrCurrent forward one row.
168624**
168625** AGGINVERSE: invoke the aggregate xInverse() function for each window
168626** functions with arguments read from the current row of cursor
168627** csrStart. Then step csrStart forward one row.
168628**
168629** There are two other ROWS window frames that are handled significantly
168630** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
168631** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
168632** cases because they change the order in which the three cursors (csrStart,
168633** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
168634** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
168635** three.
168636**
168637** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
168638**
168639** ... loop started by sqlite3WhereBegin() ...
168640** if( new partition ){
168641** Gosub flush
168642** }
168643** Insert new row into eph table.
168644** if( first row of partition ){
168645** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168646** regEnd = <expr2>
168647** regStart = <expr1>
168648** }else{
168649** if( (regEnd--)<=0 ){
168650** AGGSTEP
168651** }
168652** RETURN_ROW
168653** if( (regStart--)<=0 ){
168654** AGGINVERSE
168655** }
168656** }
168657** }
168658** flush:
168659** if( (regEnd--)<=0 ){
168660** AGGSTEP
168661** }
168662** RETURN_ROW
168663**
168664**
168665** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
168666**
168667** ... loop started by sqlite3WhereBegin() ...
168668** if( new partition ){
168669** Gosub flush
168670** }
168671** Insert new row into eph table.
168672** if( first row of partition ){
168673** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168674** regEnd = <expr2>
168675** regStart = regEnd - <expr1>
168676** }else{
168677** AGGSTEP
168678** if( (regEnd--)<=0 ){
168679** RETURN_ROW
168680** }
168681** if( (regStart--)<=0 ){
168682** AGGINVERSE
168683** }
168684** }
168685** }
168686** flush:
168687** AGGSTEP
168688** while( 1 ){
168689** if( (regEnd--)<=0 ){
168690** RETURN_ROW
168691** if( eof ) break;
168692** }
168693** if( (regStart--)<=0 ){
168694** AGGINVERSE
168695** if( eof ) break
168696** }
168697** }
168698** while( !eof csrCurrent ){
168699** RETURN_ROW
168700** }
168701**
168702** For the most part, the patterns above are adapted to support UNBOUNDED by
168703** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
168704** CURRENT ROW by assuming that it is equivalent to "0 PRECEDING/FOLLOWING".
168705** This is optimized of course - branches that will never be taken and
168706** conditions that are always true are omitted from the VM code. The only
168707** exceptional case is:
168708**
168709** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
168710**
168711** ... loop started by sqlite3WhereBegin() ...
168712** if( new partition ){
168713** Gosub flush
168714** }
168715** Insert new row into eph table.
168716** if( first row of partition ){
168717** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168718** regStart = <expr1>
168719** }else{
168720** AGGSTEP
168721** }
168722** }
168723** flush:
168724** AGGSTEP
168725** while( 1 ){
168726** if( (regStart--)<=0 ){
168727** AGGINVERSE
168728** if( eof ) break
168729** }
168730** RETURN_ROW
168731** }
168732** while( !eof csrCurrent ){
168733** RETURN_ROW
168734** }
168735**
168736** Also requiring special handling are the cases:
168737**
168738** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
168739** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
168740**
168741** when (expr1 < expr2). This is detected at runtime, not by this function.
168742** To handle this case, the pseudo-code programs depicted above are modified
168743** slightly to be:
168744**
168745** ... loop started by sqlite3WhereBegin() ...
168746** if( new partition ){
168747** Gosub flush
168748** }
168749** Insert new row into eph table.
168750** if( first row of partition ){
168751** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168752** regEnd = <expr2>
168753** regStart = <expr1>
168754** if( regEnd < regStart ){
168755** RETURN_ROW
168756** delete eph table contents
168757** continue
168758** }
168759** ...
168760**
168761** The new "continue" statement in the above jumps to the next iteration
168762** of the outer loop - the one started by sqlite3WhereBegin().
168763**
168764** The various GROUPS cases are implemented using the same patterns as
168765** ROWS. The VM code is modified slightly so that:
168766**
168767** 1. The else branch in the main loop is only taken if the row just
168768** added to the ephemeral table is the start of a new group. In
168769** other words, it becomes:
168770**
168771** ... loop started by sqlite3WhereBegin() ...
168772** if( new partition ){
168773** Gosub flush
168774** }
168775** Insert new row into eph table.
168776** if( first row of partition ){
168777** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168778** regEnd = <expr2>
168779** regStart = <expr1>
168780** }else if( new group ){
168781** ...
168782** }
168783** }
168784**
168785** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
168786** AGGINVERSE step processes the current row of the relevant cursor and
168787** all subsequent rows belonging to the same group.
168788**
168789** RANGE window frames are a little different again. As for GROUPS, the
168790** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
168791** deal in groups instead of rows. As for ROWS and GROUPS, there are three
168792** basic cases:
168793**
168794** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
168795**
168796** ... loop started by sqlite3WhereBegin() ...
168797** if( new partition ){
168798** Gosub flush
168799** }
168800** Insert new row into eph table.
168801** if( first row of partition ){
168802** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168803** regEnd = <expr2>
168804** regStart = <expr1>
168805** }else{
168806** AGGSTEP
168807** while( (csrCurrent.key + regEnd) < csrEnd.key ){
168808** RETURN_ROW
168809** while( csrStart.key + regStart) < csrCurrent.key ){
168810** AGGINVERSE
168811** }
168812** }
168813** }
168814** }
168815** flush:
168816** AGGSTEP
168817** while( 1 ){
168818** RETURN ROW
168819** if( csrCurrent is EOF ) break;
168820** while( csrStart.key + regStart) < csrCurrent.key ){
168821** AGGINVERSE
168822** }
168823** }
168824** }
168825**
168826** In the above notation, "csr.key" means the current value of the ORDER BY
168827** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
168828** or <expr PRECEDING) read from cursor csr.
168829**
168830** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
168831**
168832** ... loop started by sqlite3WhereBegin() ...
168833** if( new partition ){
168834** Gosub flush
168835** }
168836** Insert new row into eph table.
168837** if( first row of partition ){
168838** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168839** regEnd = <expr2>
168840** regStart = <expr1>
168841** }else{
168842** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
168843** AGGSTEP
168844** }
168845** while( (csrStart.key + regStart) < csrCurrent.key ){
168846** AGGINVERSE
168847** }
168848** RETURN_ROW
168849** }
168850** }
168851** flush:
168852** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
168853** AGGSTEP
168854** }
168855** while( (csrStart.key + regStart) < csrCurrent.key ){
168856** AGGINVERSE
168857** }
168858** RETURN_ROW
168859**
168860** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
168861**
168862** ... loop started by sqlite3WhereBegin() ...
168863** if( new partition ){
168864** Gosub flush
168865** }
168866** Insert new row into eph table.
168867** if( first row of partition ){
168868** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
168869** regEnd = <expr2>
168870** regStart = <expr1>
168871** }else{
168872** AGGSTEP
168873** while( (csrCurrent.key + regEnd) < csrEnd.key ){
168874** while( (csrCurrent.key + regStart) > csrStart.key ){
168875** AGGINVERSE
168876** }
168877** RETURN_ROW
168878** }
168879** }
168880** }
168881** flush:
168882** AGGSTEP
168883** while( 1 ){
168884** while( (csrCurrent.key + regStart) > csrStart.key ){
168885** AGGINVERSE
168886** if( eof ) break "while( 1 )" loop.
168887** }
168888** RETURN_ROW
168889** }
168890** while( !eof csrCurrent ){
168891** RETURN_ROW
168892** }
168893**
168894** The text above leaves out many details. Refer to the code and comments
168895** below for a more complete picture.
168896*/
168897SQLITE_PRIVATE void sqlite3WindowCodeStep(
168898 Parse *pParse, /* Parse context */
168899 Select *p, /* Rewritten SELECT statement */
168900 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
168901 int regGosub, /* Register for OP_Gosub */
168902 int addrGosub /* OP_Gosub here to return each row */
168903){
168904 Window *pMWin = p->pWin;
168905 ExprList *pOrderBy = pMWin->pOrderBy;
168906 Vdbe *v = sqlite3GetVdbe(pParse);
168907 int csrWrite; /* Cursor used to write to eph. table */
168908 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
168909 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
168910 int iInput; /* To iterate through sub cols */
168911 int addrNe; /* Address of OP_Ne */
168912 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
168913 int addrInteger = 0; /* Address of OP_Integer */
168914 int addrEmpty; /* Address of OP_Rewind in flush: */
168915 int regNew; /* Array of registers holding new input row */
168916 int regRecord; /* regNew array in record form */
168917 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
168918 int regPeer = 0; /* Peer values for current row */
168919 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
168920 WindowCodeArg s; /* Context object for sub-routines */
168921 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
168922 int regStart = 0; /* Value of <expr> PRECEDING */
168923 int regEnd = 0; /* Value of <expr> FOLLOWING */
168924
168925 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
168926 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
168927 );
168928 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
168929 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
168930 );
168931 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
168932 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
168933 || pMWin->eExclude==TK_NO
168934 );
168935
168936 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
168937
168938 /* Fill in the context object */
168939 memset(s: &s, c: 0, n: sizeof(WindowCodeArg));
168940 s.pParse = pParse;
168941 s.pMWin = pMWin;
168942 s.pVdbe = v;
168943 s.regGosub = regGosub;
168944 s.addrGosub = addrGosub;
168945 s.current.csr = pMWin->iEphCsr;
168946 csrWrite = s.current.csr+1;
168947 s.start.csr = s.current.csr+2;
168948 s.end.csr = s.current.csr+3;
168949
168950 /* Figure out when rows may be deleted from the ephemeral table. There
168951 ** are four options - they may never be deleted (eDelete==0), they may
168952 ** be deleted as soon as they are no longer part of the window frame
168953 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
168954 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
168955 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
168956 switch( pMWin->eStart ){
168957 case TK_FOLLOWING:
168958 if( pMWin->eFrmType!=TK_RANGE
168959 && windowExprGtZero(pParse, pExpr: pMWin->pStart)
168960 ){
168961 s.eDelete = WINDOW_RETURN_ROW;
168962 }
168963 break;
168964 case TK_UNBOUNDED:
168965 if( windowCacheFrame(pMWin)==0 ){
168966 if( pMWin->eEnd==TK_PRECEDING ){
168967 if( pMWin->eFrmType!=TK_RANGE
168968 && windowExprGtZero(pParse, pExpr: pMWin->pEnd)
168969 ){
168970 s.eDelete = WINDOW_AGGSTEP;
168971 }
168972 }else{
168973 s.eDelete = WINDOW_RETURN_ROW;
168974 }
168975 }
168976 break;
168977 default:
168978 s.eDelete = WINDOW_AGGINVERSE;
168979 break;
168980 }
168981
168982 /* Allocate registers for the array of values from the sub-query, the
168983 ** same values in record form, and the rowid used to insert said record
168984 ** into the ephemeral table. */
168985 regNew = pParse->nMem+1;
168986 pParse->nMem += nInput;
168987 regRecord = ++pParse->nMem;
168988 s.regRowid = ++pParse->nMem;
168989
168990 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
168991 ** clause, allocate registers to store the results of evaluating each
168992 ** <expr>. */
168993 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
168994 regStart = ++pParse->nMem;
168995 }
168996 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
168997 regEnd = ++pParse->nMem;
168998 }
168999
169000 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
169001 ** registers to store copies of the ORDER BY expressions (peer values)
169002 ** for the main loop, and for each cursor (start, current and end). */
169003 if( pMWin->eFrmType!=TK_ROWS ){
169004 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
169005 regNewPeer = regNew + pMWin->nBufferCol;
169006 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
169007 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
169008 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
169009 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
169010 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
169011 }
169012
169013 /* Load the column values for the row returned by the sub-select
169014 ** into an array of registers starting at regNew. Assemble them into
169015 ** a record in register regRecord. */
169016 for(iInput=0; iInput<nInput; iInput++){
169017 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csrInput, p2: iInput, p3: regNew+iInput);
169018 }
169019 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regNew, p2: nInput, p3: regRecord);
169020
169021 /* An input row has just been read into an array of registers starting
169022 ** at regNew. If the window has a PARTITION clause, this block generates
169023 ** VM code to check if the input row is the start of a new partition.
169024 ** If so, it does an OP_Gosub to an address to be filled in later. The
169025 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
169026 if( pMWin->pPartition ){
169027 int addr;
169028 ExprList *pPart = pMWin->pPartition;
169029 int nPart = pPart->nExpr;
169030 int regNewPart = regNew + pMWin->nBufferCol;
169031 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pPart, iStart: 0, nExtra: 0);
169032
169033 regFlushPart = ++pParse->nMem;
169034 addr = sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regNewPart, p2: pMWin->regPart, p3: nPart);
169035 sqlite3VdbeAppendP4(p: v, pP4: (void*)pKeyInfo, P4_KEYINFO);
169036 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr+2, p2: addr+4, p3: addr+2);
169037 VdbeCoverageEqNe(v);
169038 addrGosubFlush = sqlite3VdbeAddOp1(p: v, OP_Gosub, p1: regFlushPart);
169039 VdbeComment((v, "call flush_partition"));
169040 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regNewPart, p2: pMWin->regPart, p3: nPart-1);
169041 }
169042
169043 /* Insert the new row into the ephemeral table */
169044 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: csrWrite, p2: s.regRowid);
169045 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: csrWrite, p2: regRecord, p3: s.regRowid);
169046 addrNe = sqlite3VdbeAddOp3(p: v, OP_Ne, p1: pMWin->regOne, p2: 0, p3: s.regRowid);
169047 VdbeCoverageNeverNull(v);
169048
169049 /* This block is run for the first row of each partition */
169050 s.regArg = windowInitAccum(pParse, pMWin);
169051
169052 if( regStart ){
169053 sqlite3ExprCode(pParse, pExpr: pMWin->pStart, target: regStart);
169054 windowCheckValue(pParse, reg: regStart, eCond: 0 + (pMWin->eFrmType==TK_RANGE?3:0));
169055 }
169056 if( regEnd ){
169057 sqlite3ExprCode(pParse, pExpr: pMWin->pEnd, target: regEnd);
169058 windowCheckValue(pParse, reg: regEnd, eCond: 1 + (pMWin->eFrmType==TK_RANGE?3:0));
169059 }
169060
169061 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
169062 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
169063 int addrGe = sqlite3VdbeAddOp3(p: v, op, p1: regStart, p2: 0, p3: regEnd);
169064 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
169065 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
169066 windowAggFinal(p: &s, bFin: 0);
169067 sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: s.current.csr);
169068 windowReturnOneRow(p: &s);
169069 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: s.current.csr);
169070 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblWhereEnd);
169071 sqlite3VdbeJumpHere(p: v, addr: addrGe);
169072 }
169073 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
169074 assert( pMWin->eEnd==TK_FOLLOWING );
169075 sqlite3VdbeAddOp3(p: v, OP_Subtract, p1: regStart, p2: regEnd, p3: regStart);
169076 }
169077
169078 if( pMWin->eStart!=TK_UNBOUNDED ){
169079 sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: s.start.csr);
169080 }
169081 sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: s.current.csr);
169082 sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: s.end.csr);
169083 if( regPeer && pOrderBy ){
169084 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regNewPeer, p2: regPeer, p3: pOrderBy->nExpr-1);
169085 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regPeer, p2: s.start.reg, p3: pOrderBy->nExpr-1);
169086 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regPeer, p2: s.current.reg, p3: pOrderBy->nExpr-1);
169087 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regPeer, p2: s.end.reg, p3: pOrderBy->nExpr-1);
169088 }
169089
169090 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblWhereEnd);
169091
169092 sqlite3VdbeJumpHere(p: v, addr: addrNe);
169093
169094 /* Beginning of the block executed for the second and subsequent rows. */
169095 if( regPeer ){
169096 windowIfNewPeer(pParse, pOrderBy, regNew: regNewPeer, regOld: regPeer, addr: lblWhereEnd);
169097 }
169098 if( pMWin->eStart==TK_FOLLOWING ){
169099 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
169100 if( pMWin->eEnd!=TK_UNBOUNDED ){
169101 if( pMWin->eFrmType==TK_RANGE ){
169102 int lbl = sqlite3VdbeMakeLabel(pParse);
169103 int addrNext = sqlite3VdbeCurrentAddr(p: v);
169104 windowCodeRangeTest(p: &s, OP_Ge, csr1: s.current.csr, regVal: regEnd, csr2: s.end.csr, lbl);
169105 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169106 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
169107 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrNext);
169108 sqlite3VdbeResolveLabel(v, x: lbl);
169109 }else{
169110 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: regEnd, jumpOnEof: 0);
169111 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169112 }
169113 }
169114 }else
169115 if( pMWin->eEnd==TK_PRECEDING ){
169116 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
169117 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: regEnd, jumpOnEof: 0);
169118 if( bRPS ) windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169119 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
169120 if( !bRPS ) windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169121 }else{
169122 int addr = 0;
169123 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
169124 if( pMWin->eEnd!=TK_UNBOUNDED ){
169125 if( pMWin->eFrmType==TK_RANGE ){
169126 int lbl = 0;
169127 addr = sqlite3VdbeCurrentAddr(p: v);
169128 if( regEnd ){
169129 lbl = sqlite3VdbeMakeLabel(pParse);
169130 windowCodeRangeTest(p: &s, OP_Ge, csr1: s.current.csr, regVal: regEnd, csr2: s.end.csr, lbl);
169131 }
169132 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
169133 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169134 if( regEnd ){
169135 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addr);
169136 sqlite3VdbeResolveLabel(v, x: lbl);
169137 }
169138 }else{
169139 if( regEnd ){
169140 addr = sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: regEnd, p2: 0, p3: 1);
169141 VdbeCoverage(v);
169142 }
169143 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
169144 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169145 if( regEnd ) sqlite3VdbeJumpHere(p: v, addr);
169146 }
169147 }
169148 }
169149
169150 /* End of the main input loop */
169151 sqlite3VdbeResolveLabel(v, x: lblWhereEnd);
169152 sqlite3WhereEnd(pWInfo);
169153
169154 /* Fall through */
169155 if( pMWin->pPartition ){
169156 addrInteger = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regFlushPart);
169157 sqlite3VdbeJumpHere(p: v, addr: addrGosubFlush);
169158 }
169159
169160 s.regRowid = 0;
169161 addrEmpty = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: csrWrite);
169162 VdbeCoverage(v);
169163 if( pMWin->eEnd==TK_PRECEDING ){
169164 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
169165 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: regEnd, jumpOnEof: 0);
169166 if( bRPS ) windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169167 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
169168 }else if( pMWin->eStart==TK_FOLLOWING ){
169169 int addrStart;
169170 int addrBreak1;
169171 int addrBreak2;
169172 int addrBreak3;
169173 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
169174 if( pMWin->eFrmType==TK_RANGE ){
169175 addrStart = sqlite3VdbeCurrentAddr(p: v);
169176 addrBreak2 = windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 1);
169177 addrBreak1 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 1);
169178 }else
169179 if( pMWin->eEnd==TK_UNBOUNDED ){
169180 addrStart = sqlite3VdbeCurrentAddr(p: v);
169181 addrBreak1 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: regStart, jumpOnEof: 1);
169182 addrBreak2 = windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: 0, jumpOnEof: 1);
169183 }else{
169184 assert( pMWin->eEnd==TK_FOLLOWING );
169185 addrStart = sqlite3VdbeCurrentAddr(p: v);
169186 addrBreak1 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: regEnd, jumpOnEof: 1);
169187 addrBreak2 = windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 1);
169188 }
169189 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrStart);
169190 sqlite3VdbeJumpHere(p: v, addr: addrBreak2);
169191 addrStart = sqlite3VdbeCurrentAddr(p: v);
169192 addrBreak3 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 1);
169193 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrStart);
169194 sqlite3VdbeJumpHere(p: v, addr: addrBreak1);
169195 sqlite3VdbeJumpHere(p: v, addr: addrBreak3);
169196 }else{
169197 int addrBreak;
169198 int addrStart;
169199 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
169200 addrStart = sqlite3VdbeCurrentAddr(p: v);
169201 addrBreak = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 1);
169202 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
169203 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrStart);
169204 sqlite3VdbeJumpHere(p: v, addr: addrBreak);
169205 }
169206 sqlite3VdbeJumpHere(p: v, addr: addrEmpty);
169207
169208 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: s.current.csr);
169209 if( pMWin->pPartition ){
169210 if( pMWin->regStartRowid ){
169211 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pMWin->regStartRowid);
169212 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pMWin->regEndRowid);
169213 }
169214 sqlite3VdbeChangeP1(p: v, addr: addrInteger, val: sqlite3VdbeCurrentAddr(p: v));
169215 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regFlushPart);
169216 }
169217}
169218
169219#endif /* SQLITE_OMIT_WINDOWFUNC */
169220
169221/************** End of window.c **********************************************/
169222/************** Begin file parse.c *******************************************/
169223/* This file is automatically generated by Lemon from input grammar
169224** source file "parse.y".
169225*/
169226/*
169227** 2001-09-15
169228**
169229** The author disclaims copyright to this source code. In place of
169230** a legal notice, here is a blessing:
169231**
169232** May you do good and not evil.
169233** May you find forgiveness for yourself and forgive others.
169234** May you share freely, never taking more than you give.
169235**
169236*************************************************************************
169237** This file contains SQLite's SQL parser.
169238**
169239** The canonical source code to this file ("parse.y") is a Lemon grammar
169240** file that specifies the input grammar and actions to take while parsing.
169241** That input file is processed by Lemon to generate a C-language
169242** implementation of a parser for the given grammar. You might be reading
169243** this comment as part of the translated C-code. Edits should be made
169244** to the original parse.y sources.
169245*/
169246
169247/* #include "sqliteInt.h" */
169248
169249/*
169250** Disable all error recovery processing in the parser push-down
169251** automaton.
169252*/
169253#define YYNOERRORRECOVERY 1
169254
169255/*
169256** Make yytestcase() the same as testcase()
169257*/
169258#define yytestcase(X) testcase(X)
169259
169260/*
169261** Indicate that sqlite3ParserFree() will never be called with a null
169262** pointer.
169263*/
169264#define YYPARSEFREENEVERNULL 1
169265
169266/*
169267** In the amalgamation, the parse.c file generated by lemon and the
169268** tokenize.c file are concatenated. In that case, sqlite3RunParser()
169269** has access to the the size of the yyParser object and so the parser
169270** engine can be allocated from stack. In that case, only the
169271** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
169272** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
169273** omitted.
169274*/
169275#ifdef SQLITE_AMALGAMATION
169276# define sqlite3Parser_ENGINEALWAYSONSTACK 1
169277#endif
169278
169279/*
169280** Alternative datatype for the argument to the malloc() routine passed
169281** into sqlite3ParserAlloc(). The default is size_t.
169282*/
169283#define YYMALLOCARGTYPE u64
169284
169285/*
169286** An instance of the following structure describes the event of a
169287** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
169288** TK_DELETE, or TK_INSTEAD. If the event is of the form
169289**
169290** UPDATE ON (a,b,c)
169291**
169292** Then the "b" IdList records the list "a,b,c".
169293*/
169294struct TrigEvent { int a; IdList * b; };
169295
169296struct FrameBound { int eType; Expr *pExpr; };
169297
169298/*
169299** Disable lookaside memory allocation for objects that might be
169300** shared across database connections.
169301*/
169302static void disableLookaside(Parse *pParse){
169303 sqlite3 *db = pParse->db;
169304 pParse->disableLookaside++;
169305 DisableLookaside;
169306}
169307
169308#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
169309 && defined(SQLITE_UDL_CAPABLE_PARSER)
169310/*
169311** Issue an error message if an ORDER BY or LIMIT clause occurs on an
169312** UPDATE or DELETE statement.
169313*/
169314static void updateDeleteLimitError(
169315 Parse *pParse,
169316 ExprList *pOrderBy,
169317 Expr *pLimit
169318){
169319 if( pOrderBy ){
169320 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
169321 }else{
169322 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
169323 }
169324 sqlite3ExprListDelete(pParse->db, pOrderBy);
169325 sqlite3ExprDelete(pParse->db, pLimit);
169326}
169327#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
169328
169329
169330 /*
169331 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
169332 ** all elements in the list. And make sure list length does not exceed
169333 ** SQLITE_LIMIT_COMPOUND_SELECT.
169334 */
169335 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
169336 assert( p!=0 );
169337 if( p->pPrior ){
169338 Select *pNext = 0, *pLoop = p;
169339 int mxSelect, cnt = 1;
169340 while(1){
169341 pLoop->pNext = pNext;
169342 pLoop->selFlags |= SF_Compound;
169343 pNext = pLoop;
169344 pLoop = pLoop->pPrior;
169345 if( pLoop==0 ) break;
169346 cnt++;
169347 if( pLoop->pOrderBy || pLoop->pLimit ){
169348 sqlite3ErrorMsg(pParse,zFormat: "%s clause should come after %s not before",
169349 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
169350 sqlite3SelectOpName(id: pNext->op));
169351 break;
169352 }
169353 }
169354 if( (p->selFlags & SF_MultiValue)==0 &&
169355 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
169356 cnt>mxSelect
169357 ){
169358 sqlite3ErrorMsg(pParse, zFormat: "too many terms in compound SELECT");
169359 }
169360 }
169361 }
169362
169363 /* Attach a With object describing the WITH clause to a Select
169364 ** object describing the query for which the WITH clause is a prefix.
169365 */
169366 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
169367 if( pSelect ){
169368 pSelect->pWith = pWith;
169369 parserDoubleLinkSelect(pParse, p: pSelect);
169370 }else{
169371 sqlite3WithDelete(db: pParse->db, pWith);
169372 }
169373 return pSelect;
169374 }
169375
169376
169377 /* Construct a new Expr object from a single token */
169378 static Expr *tokenExpr(Parse *pParse, int op, Token t){
169379 Expr *p = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(Expr)+t.n+1);
169380 if( p ){
169381 /* memset(p, 0, sizeof(Expr)); */
169382 p->op = (u8)op;
169383 p->affExpr = 0;
169384 p->flags = EP_Leaf;
169385 ExprClearVVAProperties(p);
169386 /* p->iAgg = -1; // Not required */
169387 p->pLeft = p->pRight = 0;
169388 p->pAggInfo = 0;
169389 memset(s: &p->x, c: 0, n: sizeof(p->x));
169390 memset(s: &p->y, c: 0, n: sizeof(p->y));
169391 p->op2 = 0;
169392 p->iTable = 0;
169393 p->iColumn = 0;
169394 p->u.zToken = (char*)&p[1];
169395 memcpy(dest: p->u.zToken, src: t.z, n: t.n);
169396 p->u.zToken[t.n] = 0;
169397 p->w.iOfst = (int)(t.z - pParse->zTail);
169398 if( sqlite3Isquote(p->u.zToken[0]) ){
169399 sqlite3DequoteExpr(p);
169400 }
169401#if SQLITE_MAX_EXPR_DEPTH>0
169402 p->nHeight = 1;
169403#endif
169404 if( IN_RENAME_OBJECT ){
169405 return (Expr*)sqlite3RenameTokenMap(pParse, pPtr: (void*)p, pToken: &t);
169406 }
169407 }
169408 return p;
169409 }
169410
169411
169412 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
169413 ** unary TK_ISNULL or TK_NOTNULL expression. */
169414 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
169415 sqlite3 *db = pParse->db;
169416 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
169417 pA->op = (u8)op;
169418 sqlite3ExprDelete(db, p: pA->pRight);
169419 pA->pRight = 0;
169420 }
169421 }
169422
169423 /* Add a single new term to an ExprList that is used to store a
169424 ** list of identifiers. Report an error if the ID list contains
169425 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
169426 ** error while parsing a legacy schema.
169427 */
169428 static ExprList *parserAddExprIdListTerm(
169429 Parse *pParse,
169430 ExprList *pPrior,
169431 Token *pIdToken,
169432 int hasCollate,
169433 int sortOrder
169434 ){
169435 ExprList *p = sqlite3ExprListAppend(pParse, pList: pPrior, pExpr: 0);
169436 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
169437 && pParse->db->init.busy==0
169438 ){
169439 sqlite3ErrorMsg(pParse, zFormat: "syntax error after column name \"%.*s\"",
169440 pIdToken->n, pIdToken->z);
169441 }
169442 sqlite3ExprListSetName(pParse, pList: p, pName: pIdToken, dequote: 1);
169443 return p;
169444 }
169445
169446#if TK_SPAN>255
169447# error too many tokens in the grammar
169448#endif
169449/**************** End of %include directives **********************************/
169450/* These constants specify the various numeric values for terminal symbols.
169451***************** Begin token definitions *************************************/
169452#ifndef TK_SEMI
169453#define TK_SEMI 1
169454#define TK_EXPLAIN 2
169455#define TK_QUERY 3
169456#define TK_PLAN 4
169457#define TK_BEGIN 5
169458#define TK_TRANSACTION 6
169459#define TK_DEFERRED 7
169460#define TK_IMMEDIATE 8
169461#define TK_EXCLUSIVE 9
169462#define TK_COMMIT 10
169463#define TK_END 11
169464#define TK_ROLLBACK 12
169465#define TK_SAVEPOINT 13
169466#define TK_RELEASE 14
169467#define TK_TO 15
169468#define TK_TABLE 16
169469#define TK_CREATE 17
169470#define TK_IF 18
169471#define TK_NOT 19
169472#define TK_EXISTS 20
169473#define TK_TEMP 21
169474#define TK_LP 22
169475#define TK_RP 23
169476#define TK_AS 24
169477#define TK_COMMA 25
169478#define TK_WITHOUT 26
169479#define TK_ABORT 27
169480#define TK_ACTION 28
169481#define TK_AFTER 29
169482#define TK_ANALYZE 30
169483#define TK_ASC 31
169484#define TK_ATTACH 32
169485#define TK_BEFORE 33
169486#define TK_BY 34
169487#define TK_CASCADE 35
169488#define TK_CAST 36
169489#define TK_CONFLICT 37
169490#define TK_DATABASE 38
169491#define TK_DESC 39
169492#define TK_DETACH 40
169493#define TK_EACH 41
169494#define TK_FAIL 42
169495#define TK_OR 43
169496#define TK_AND 44
169497#define TK_IS 45
169498#define TK_MATCH 46
169499#define TK_LIKE_KW 47
169500#define TK_BETWEEN 48
169501#define TK_IN 49
169502#define TK_ISNULL 50
169503#define TK_NOTNULL 51
169504#define TK_NE 52
169505#define TK_EQ 53
169506#define TK_GT 54
169507#define TK_LE 55
169508#define TK_LT 56
169509#define TK_GE 57
169510#define TK_ESCAPE 58
169511#define TK_ID 59
169512#define TK_COLUMNKW 60
169513#define TK_DO 61
169514#define TK_FOR 62
169515#define TK_IGNORE 63
169516#define TK_INITIALLY 64
169517#define TK_INSTEAD 65
169518#define TK_NO 66
169519#define TK_KEY 67
169520#define TK_OF 68
169521#define TK_OFFSET 69
169522#define TK_PRAGMA 70
169523#define TK_RAISE 71
169524#define TK_RECURSIVE 72
169525#define TK_REPLACE 73
169526#define TK_RESTRICT 74
169527#define TK_ROW 75
169528#define TK_ROWS 76
169529#define TK_TRIGGER 77
169530#define TK_VACUUM 78
169531#define TK_VIEW 79
169532#define TK_VIRTUAL 80
169533#define TK_WITH 81
169534#define TK_NULLS 82
169535#define TK_FIRST 83
169536#define TK_LAST 84
169537#define TK_CURRENT 85
169538#define TK_FOLLOWING 86
169539#define TK_PARTITION 87
169540#define TK_PRECEDING 88
169541#define TK_RANGE 89
169542#define TK_UNBOUNDED 90
169543#define TK_EXCLUDE 91
169544#define TK_GROUPS 92
169545#define TK_OTHERS 93
169546#define TK_TIES 94
169547#define TK_GENERATED 95
169548#define TK_ALWAYS 96
169549#define TK_MATERIALIZED 97
169550#define TK_REINDEX 98
169551#define TK_RENAME 99
169552#define TK_CTIME_KW 100
169553#define TK_ANY 101
169554#define TK_BITAND 102
169555#define TK_BITOR 103
169556#define TK_LSHIFT 104
169557#define TK_RSHIFT 105
169558#define TK_PLUS 106
169559#define TK_MINUS 107
169560#define TK_STAR 108
169561#define TK_SLASH 109
169562#define TK_REM 110
169563#define TK_CONCAT 111
169564#define TK_PTR 112
169565#define TK_COLLATE 113
169566#define TK_BITNOT 114
169567#define TK_ON 115
169568#define TK_INDEXED 116
169569#define TK_STRING 117
169570#define TK_JOIN_KW 118
169571#define TK_CONSTRAINT 119
169572#define TK_DEFAULT 120
169573#define TK_NULL 121
169574#define TK_PRIMARY 122
169575#define TK_UNIQUE 123
169576#define TK_CHECK 124
169577#define TK_REFERENCES 125
169578#define TK_AUTOINCR 126
169579#define TK_INSERT 127
169580#define TK_DELETE 128
169581#define TK_UPDATE 129
169582#define TK_SET 130
169583#define TK_DEFERRABLE 131
169584#define TK_FOREIGN 132
169585#define TK_DROP 133
169586#define TK_UNION 134
169587#define TK_ALL 135
169588#define TK_EXCEPT 136
169589#define TK_INTERSECT 137
169590#define TK_SELECT 138
169591#define TK_VALUES 139
169592#define TK_DISTINCT 140
169593#define TK_DOT 141
169594#define TK_FROM 142
169595#define TK_JOIN 143
169596#define TK_USING 144
169597#define TK_ORDER 145
169598#define TK_GROUP 146
169599#define TK_HAVING 147
169600#define TK_LIMIT 148
169601#define TK_WHERE 149
169602#define TK_RETURNING 150
169603#define TK_INTO 151
169604#define TK_NOTHING 152
169605#define TK_FLOAT 153
169606#define TK_BLOB 154
169607#define TK_INTEGER 155
169608#define TK_VARIABLE 156
169609#define TK_CASE 157
169610#define TK_WHEN 158
169611#define TK_THEN 159
169612#define TK_ELSE 160
169613#define TK_INDEX 161
169614#define TK_ALTER 162
169615#define TK_ADD 163
169616#define TK_WINDOW 164
169617#define TK_OVER 165
169618#define TK_FILTER 166
169619#define TK_COLUMN 167
169620#define TK_AGG_FUNCTION 168
169621#define TK_AGG_COLUMN 169
169622#define TK_TRUEFALSE 170
169623#define TK_ISNOT 171
169624#define TK_FUNCTION 172
169625#define TK_UMINUS 173
169626#define TK_UPLUS 174
169627#define TK_TRUTH 175
169628#define TK_REGISTER 176
169629#define TK_VECTOR 177
169630#define TK_SELECT_COLUMN 178
169631#define TK_IF_NULL_ROW 179
169632#define TK_ASTERISK 180
169633#define TK_SPAN 181
169634#define TK_ERROR 182
169635#define TK_SPACE 183
169636#define TK_ILLEGAL 184
169637#endif
169638/**************** End token definitions ***************************************/
169639
169640/* The next sections is a series of control #defines.
169641** various aspects of the generated parser.
169642** YYCODETYPE is the data type used to store the integer codes
169643** that represent terminal and non-terminal symbols.
169644** "unsigned char" is used if there are fewer than
169645** 256 symbols. Larger types otherwise.
169646** YYNOCODE is a number of type YYCODETYPE that is not used for
169647** any terminal or nonterminal symbol.
169648** YYFALLBACK If defined, this indicates that one or more tokens
169649** (also known as: "terminal symbols") have fall-back
169650** values which should be used if the original symbol
169651** would not parse. This permits keywords to sometimes
169652** be used as identifiers, for example.
169653** YYACTIONTYPE is the data type used for "action codes" - numbers
169654** that indicate what to do in response to the next
169655** token.
169656** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
169657** symbols. Background: A "minor type" is a semantic
169658** value associated with a terminal or non-terminal
169659** symbols. For example, for an "ID" terminal symbol,
169660** the minor type might be the name of the identifier.
169661** Each non-terminal can have a different minor type.
169662** Terminal symbols all have the same minor type, though.
169663** This macros defines the minor type for terminal
169664** symbols.
169665** YYMINORTYPE is the data type used for all minor types.
169666** This is typically a union of many types, one of
169667** which is sqlite3ParserTOKENTYPE. The entry in the union
169668** for terminal symbols is called "yy0".
169669** YYSTACKDEPTH is the maximum depth of the parser's stack. If
169670** zero the stack is dynamically sized using realloc()
169671** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
169672** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
169673** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
169674** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
169675** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
169676** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
169677** YYERRORSYMBOL is the code number of the error symbol. If not
169678** defined, then do no error processing.
169679** YYNSTATE the combined number of states.
169680** YYNRULE the number of rules in the grammar
169681** YYNTOKEN Number of terminal symbols
169682** YY_MAX_SHIFT Maximum value for shift actions
169683** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
169684** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
169685** YY_ERROR_ACTION The yy_action[] code for syntax error
169686** YY_ACCEPT_ACTION The yy_action[] code for accept
169687** YY_NO_ACTION The yy_action[] code for no-op
169688** YY_MIN_REDUCE Minimum value for reduce actions
169689** YY_MAX_REDUCE Maximum value for reduce actions
169690*/
169691#ifndef INTERFACE
169692# define INTERFACE 1
169693#endif
169694/************* Begin control #defines *****************************************/
169695#define YYCODETYPE unsigned short int
169696#define YYNOCODE 319
169697#define YYACTIONTYPE unsigned short int
169698#define YYWILDCARD 101
169699#define sqlite3ParserTOKENTYPE Token
169700typedef union {
169701 int yyinit;
169702 sqlite3ParserTOKENTYPE yy0;
169703 TriggerStep* yy33;
169704 Window* yy41;
169705 Select* yy47;
169706 SrcList* yy131;
169707 struct TrigEvent yy180;
169708 struct {int value; int mask;} yy231;
169709 IdList* yy254;
169710 u32 yy285;
169711 ExprList* yy322;
169712 Cte* yy385;
169713 int yy394;
169714 Upsert* yy444;
169715 u8 yy516;
169716 With* yy521;
169717 const char* yy522;
169718 Expr* yy528;
169719 OnOrUsing yy561;
169720 struct FrameBound yy595;
169721} YYMINORTYPE;
169722#ifndef YYSTACKDEPTH
169723#define YYSTACKDEPTH 100
169724#endif
169725#define sqlite3ParserARG_SDECL
169726#define sqlite3ParserARG_PDECL
169727#define sqlite3ParserARG_PARAM
169728#define sqlite3ParserARG_FETCH
169729#define sqlite3ParserARG_STORE
169730#define sqlite3ParserCTX_SDECL Parse *pParse;
169731#define sqlite3ParserCTX_PDECL ,Parse *pParse
169732#define sqlite3ParserCTX_PARAM ,pParse
169733#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
169734#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
169735#define YYFALLBACK 1
169736#define YYNSTATE 575
169737#define YYNRULE 403
169738#define YYNRULE_WITH_ACTION 338
169739#define YYNTOKEN 185
169740#define YY_MAX_SHIFT 574
169741#define YY_MIN_SHIFTREDUCE 833
169742#define YY_MAX_SHIFTREDUCE 1235
169743#define YY_ERROR_ACTION 1236
169744#define YY_ACCEPT_ACTION 1237
169745#define YY_NO_ACTION 1238
169746#define YY_MIN_REDUCE 1239
169747#define YY_MAX_REDUCE 1641
169748/************* End control #defines *******************************************/
169749#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
169750
169751/* Define the yytestcase() macro to be a no-op if is not already defined
169752** otherwise.
169753**
169754** Applications can choose to define yytestcase() in the %include section
169755** to a macro that can assist in verifying code coverage. For production
169756** code the yytestcase() macro should be turned off. But it is useful
169757** for testing.
169758*/
169759#ifndef yytestcase
169760# define yytestcase(X)
169761#endif
169762
169763
169764/* Next are the tables used to determine what action to take based on the
169765** current state and lookahead token. These tables are used to implement
169766** functions that take a state number and lookahead value and return an
169767** action integer.
169768**
169769** Suppose the action integer is N. Then the action is determined as
169770** follows
169771**
169772** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
169773** token onto the stack and goto state N.
169774**
169775** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
169776** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
169777**
169778** N == YY_ERROR_ACTION A syntax error has occurred.
169779**
169780** N == YY_ACCEPT_ACTION The parser accepts its input.
169781**
169782** N == YY_NO_ACTION No such action. Denotes unused
169783** slots in the yy_action[] table.
169784**
169785** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
169786** and YY_MAX_REDUCE
169787**
169788** The action table is constructed as a single large table named yy_action[].
169789** Given state S and lookahead X, the action is computed as either:
169790**
169791** (A) N = yy_action[ yy_shift_ofst[S] + X ]
169792** (B) N = yy_default[S]
169793**
169794** The (A) formula is preferred. The B formula is used instead if
169795** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
169796**
169797** The formulas above are for computing the action when the lookahead is
169798** a terminal symbol. If the lookahead is a non-terminal (as occurs after
169799** a reduce action) then the yy_reduce_ofst[] array is used in place of
169800** the yy_shift_ofst[] array.
169801**
169802** The following are the tables generated in this section:
169803**
169804** yy_action[] A single table containing all actions.
169805** yy_lookahead[] A table containing the lookahead for each entry in
169806** yy_action. Used to detect hash collisions.
169807** yy_shift_ofst[] For each state, the offset into yy_action for
169808** shifting terminals.
169809** yy_reduce_ofst[] For each state, the offset into yy_action for
169810** shifting non-terminals after a reduce.
169811** yy_default[] Default action for each state.
169812**
169813*********** Begin parsing tables **********************************************/
169814#define YY_ACTTAB_COUNT (2096)
169815static const YYACTIONTYPE yy_action[] = {
169816 /* 0 */ 568, 208, 568, 118, 115, 229, 568, 118, 115, 229,
169817 /* 10 */ 568, 1310, 377, 1289, 408, 562, 562, 562, 568, 409,
169818 /* 20 */ 378, 1310, 1272, 41, 41, 41, 41, 208, 1520, 71,
169819 /* 30 */ 71, 969, 419, 41, 41, 491, 303, 279, 303, 970,
169820 /* 40 */ 397, 71, 71, 125, 126, 80, 1210, 1210, 1047, 1050,
169821 /* 50 */ 1037, 1037, 123, 123, 124, 124, 124, 124, 476, 409,
169822 /* 60 */ 1237, 1, 1, 574, 2, 1241, 550, 118, 115, 229,
169823 /* 70 */ 317, 480, 146, 480, 524, 118, 115, 229, 529, 1323,
169824 /* 80 */ 417, 523, 142, 125, 126, 80, 1210, 1210, 1047, 1050,
169825 /* 90 */ 1037, 1037, 123, 123, 124, 124, 124, 124, 118, 115,
169826 /* 100 */ 229, 327, 122, 122, 122, 122, 121, 121, 120, 120,
169827 /* 110 */ 120, 119, 116, 444, 284, 284, 284, 284, 442, 442,
169828 /* 120 */ 442, 1559, 376, 1561, 1186, 375, 1157, 565, 1157, 565,
169829 /* 130 */ 409, 1559, 537, 259, 226, 444, 101, 145, 449, 316,
169830 /* 140 */ 559, 240, 122, 122, 122, 122, 121, 121, 120, 120,
169831 /* 150 */ 120, 119, 116, 444, 125, 126, 80, 1210, 1210, 1047,
169832 /* 160 */ 1050, 1037, 1037, 123, 123, 124, 124, 124, 124, 142,
169833 /* 170 */ 294, 1186, 339, 448, 120, 120, 120, 119, 116, 444,
169834 /* 180 */ 127, 1186, 1187, 1186, 148, 441, 440, 568, 119, 116,
169835 /* 190 */ 444, 124, 124, 124, 124, 117, 122, 122, 122, 122,
169836 /* 200 */ 121, 121, 120, 120, 120, 119, 116, 444, 454, 113,
169837 /* 210 */ 13, 13, 546, 122, 122, 122, 122, 121, 121, 120,
169838 /* 220 */ 120, 120, 119, 116, 444, 422, 316, 559, 1186, 1187,
169839 /* 230 */ 1186, 149, 1218, 409, 1218, 124, 124, 124, 124, 122,
169840 /* 240 */ 122, 122, 122, 121, 121, 120, 120, 120, 119, 116,
169841 /* 250 */ 444, 465, 342, 1034, 1034, 1048, 1051, 125, 126, 80,
169842 /* 260 */ 1210, 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124,
169843 /* 270 */ 124, 124, 1275, 522, 222, 1186, 568, 409, 224, 514,
169844 /* 280 */ 175, 82, 83, 122, 122, 122, 122, 121, 121, 120,
169845 /* 290 */ 120, 120, 119, 116, 444, 1005, 16, 16, 1186, 133,
169846 /* 300 */ 133, 125, 126, 80, 1210, 1210, 1047, 1050, 1037, 1037,
169847 /* 310 */ 123, 123, 124, 124, 124, 124, 122, 122, 122, 122,
169848 /* 320 */ 121, 121, 120, 120, 120, 119, 116, 444, 1038, 546,
169849 /* 330 */ 1186, 373, 1186, 1187, 1186, 252, 1429, 399, 504, 501,
169850 /* 340 */ 500, 111, 560, 566, 4, 924, 924, 433, 499, 340,
169851 /* 350 */ 460, 328, 360, 394, 1231, 1186, 1187, 1186, 563, 568,
169852 /* 360 */ 122, 122, 122, 122, 121, 121, 120, 120, 120, 119,
169853 /* 370 */ 116, 444, 284, 284, 369, 1572, 1598, 441, 440, 154,
169854 /* 380 */ 409, 445, 71, 71, 1282, 565, 1215, 1186, 1187, 1186,
169855 /* 390 */ 85, 1217, 271, 557, 543, 515, 515, 568, 98, 1216,
169856 /* 400 */ 6, 1274, 472, 142, 125, 126, 80, 1210, 1210, 1047,
169857 /* 410 */ 1050, 1037, 1037, 123, 123, 124, 124, 124, 124, 550,
169858 /* 420 */ 13, 13, 1024, 507, 1218, 1186, 1218, 549, 109, 109,
169859 /* 430 */ 222, 568, 1232, 175, 568, 427, 110, 197, 445, 569,
169860 /* 440 */ 445, 430, 1546, 1014, 325, 551, 1186, 270, 287, 368,
169861 /* 450 */ 510, 363, 509, 257, 71, 71, 543, 71, 71, 359,
169862 /* 460 */ 316, 559, 1604, 122, 122, 122, 122, 121, 121, 120,
169863 /* 470 */ 120, 120, 119, 116, 444, 1014, 1014, 1016, 1017, 27,
169864 /* 480 */ 284, 284, 1186, 1187, 1186, 1152, 568, 1603, 409, 899,
169865 /* 490 */ 190, 550, 356, 565, 550, 935, 533, 517, 1152, 516,
169866 /* 500 */ 413, 1152, 552, 1186, 1187, 1186, 568, 544, 544, 51,
169867 /* 510 */ 51, 214, 125, 126, 80, 1210, 1210, 1047, 1050, 1037,
169868 /* 520 */ 1037, 123, 123, 124, 124, 124, 124, 1186, 474, 135,
169869 /* 530 */ 135, 409, 284, 284, 1484, 505, 121, 121, 120, 120,
169870 /* 540 */ 120, 119, 116, 444, 1005, 565, 518, 217, 541, 541,
169871 /* 550 */ 316, 559, 142, 6, 532, 125, 126, 80, 1210, 1210,
169872 /* 560 */ 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124, 124,
169873 /* 570 */ 1548, 122, 122, 122, 122, 121, 121, 120, 120, 120,
169874 /* 580 */ 119, 116, 444, 485, 1186, 1187, 1186, 482, 281, 1263,
169875 /* 590 */ 955, 252, 1186, 373, 504, 501, 500, 1186, 340, 570,
169876 /* 600 */ 1186, 570, 409, 292, 499, 955, 874, 191, 480, 316,
169877 /* 610 */ 559, 384, 290, 380, 122, 122, 122, 122, 121, 121,
169878 /* 620 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1210,
169879 /* 630 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169880 /* 640 */ 124, 409, 394, 1132, 1186, 867, 100, 284, 284, 1186,
169881 /* 650 */ 1187, 1186, 373, 1089, 1186, 1187, 1186, 1186, 1187, 1186,
169882 /* 660 */ 565, 455, 32, 373, 233, 125, 126, 80, 1210, 1210,
169883 /* 670 */ 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124, 124,
169884 /* 680 */ 1428, 957, 568, 228, 956, 122, 122, 122, 122, 121,
169885 /* 690 */ 121, 120, 120, 120, 119, 116, 444, 1152, 228, 1186,
169886 /* 700 */ 157, 1186, 1187, 1186, 1547, 13, 13, 301, 955, 1226,
169887 /* 710 */ 1152, 153, 409, 1152, 373, 1575, 1170, 5, 369, 1572,
169888 /* 720 */ 429, 1232, 3, 955, 122, 122, 122, 122, 121, 121,
169889 /* 730 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1210,
169890 /* 740 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169891 /* 750 */ 124, 409, 208, 567, 1186, 1025, 1186, 1187, 1186, 1186,
169892 /* 760 */ 388, 850, 155, 1546, 286, 402, 1094, 1094, 488, 568,
169893 /* 770 */ 465, 342, 1315, 1315, 1546, 125, 126, 80, 1210, 1210,
169894 /* 780 */ 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124, 124,
169895 /* 790 */ 129, 568, 13, 13, 374, 122, 122, 122, 122, 121,
169896 /* 800 */ 121, 120, 120, 120, 119, 116, 444, 302, 568, 453,
169897 /* 810 */ 528, 1186, 1187, 1186, 13, 13, 1186, 1187, 1186, 1293,
169898 /* 820 */ 463, 1263, 409, 1313, 1313, 1546, 1010, 453, 452, 200,
169899 /* 830 */ 299, 71, 71, 1261, 122, 122, 122, 122, 121, 121,
169900 /* 840 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1210,
169901 /* 850 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169902 /* 860 */ 124, 409, 227, 1069, 1152, 284, 284, 419, 312, 278,
169903 /* 870 */ 278, 285, 285, 1415, 406, 405, 382, 1152, 565, 568,
169904 /* 880 */ 1152, 1189, 565, 1592, 565, 125, 126, 80, 1210, 1210,
169905 /* 890 */ 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124, 124,
169906 /* 900 */ 453, 1476, 13, 13, 1530, 122, 122, 122, 122, 121,
169907 /* 910 */ 121, 120, 120, 120, 119, 116, 444, 201, 568, 354,
169908 /* 920 */ 1578, 574, 2, 1241, 838, 839, 840, 1554, 317, 1205,
169909 /* 930 */ 146, 6, 409, 255, 254, 253, 206, 1323, 9, 1189,
169910 /* 940 */ 262, 71, 71, 424, 122, 122, 122, 122, 121, 121,
169911 /* 950 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1210,
169912 /* 960 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169913 /* 970 */ 124, 568, 284, 284, 568, 1206, 409, 573, 313, 1241,
169914 /* 980 */ 349, 1292, 352, 419, 317, 565, 146, 491, 525, 1635,
169915 /* 990 */ 395, 371, 491, 1323, 70, 70, 1291, 71, 71, 240,
169916 /* 1000 */ 1321, 104, 80, 1210, 1210, 1047, 1050, 1037, 1037, 123,
169917 /* 1010 */ 123, 124, 124, 124, 124, 122, 122, 122, 122, 121,
169918 /* 1020 */ 121, 120, 120, 120, 119, 116, 444, 1110, 284, 284,
169919 /* 1030 */ 428, 448, 1519, 1206, 439, 284, 284, 1483, 1348, 311,
169920 /* 1040 */ 474, 565, 1111, 969, 491, 491, 217, 1259, 565, 1532,
169921 /* 1050 */ 568, 970, 207, 568, 1024, 240, 383, 1112, 519, 122,
169922 /* 1060 */ 122, 122, 122, 121, 121, 120, 120, 120, 119, 116,
169923 /* 1070 */ 444, 1015, 107, 71, 71, 1014, 13, 13, 910, 568,
169924 /* 1080 */ 1489, 568, 284, 284, 97, 526, 491, 448, 911, 1322,
169925 /* 1090 */ 1318, 545, 409, 284, 284, 565, 151, 209, 1489, 1491,
169926 /* 1100 */ 262, 450, 55, 55, 56, 56, 565, 1014, 1014, 1016,
169927 /* 1110 */ 443, 332, 409, 527, 12, 295, 125, 126, 80, 1210,
169928 /* 1120 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169929 /* 1130 */ 124, 347, 409, 862, 1528, 1206, 125, 126, 80, 1210,
169930 /* 1140 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169931 /* 1150 */ 124, 1133, 1633, 474, 1633, 371, 125, 114, 80, 1210,
169932 /* 1160 */ 1210, 1047, 1050, 1037, 1037, 123, 123, 124, 124, 124,
169933 /* 1170 */ 124, 1489, 329, 474, 331, 122, 122, 122, 122, 121,
169934 /* 1180 */ 121, 120, 120, 120, 119, 116, 444, 203, 1415, 568,
169935 /* 1190 */ 1290, 862, 464, 1206, 436, 122, 122, 122, 122, 121,
169936 /* 1200 */ 121, 120, 120, 120, 119, 116, 444, 553, 1133, 1634,
169937 /* 1210 */ 539, 1634, 15, 15, 890, 122, 122, 122, 122, 121,
169938 /* 1220 */ 121, 120, 120, 120, 119, 116, 444, 568, 298, 538,
169939 /* 1230 */ 1131, 1415, 1552, 1553, 1327, 409, 6, 6, 1163, 1264,
169940 /* 1240 */ 415, 320, 284, 284, 1415, 508, 565, 525, 300, 457,
169941 /* 1250 */ 43, 43, 568, 891, 12, 565, 330, 478, 425, 407,
169942 /* 1260 */ 126, 80, 1210, 1210, 1047, 1050, 1037, 1037, 123, 123,
169943 /* 1270 */ 124, 124, 124, 124, 568, 57, 57, 288, 1186, 1415,
169944 /* 1280 */ 496, 458, 392, 392, 391, 273, 389, 1131, 1551, 847,
169945 /* 1290 */ 1163, 407, 6, 568, 321, 1152, 470, 44, 44, 1550,
169946 /* 1300 */ 1110, 426, 234, 6, 323, 256, 540, 256, 1152, 431,
169947 /* 1310 */ 568, 1152, 322, 17, 487, 1111, 58, 58, 122, 122,
169948 /* 1320 */ 122, 122, 121, 121, 120, 120, 120, 119, 116, 444,
169949 /* 1330 */ 1112, 216, 481, 59, 59, 1186, 1187, 1186, 111, 560,
169950 /* 1340 */ 324, 4, 236, 456, 526, 568, 237, 456, 568, 437,
169951 /* 1350 */ 168, 556, 420, 141, 479, 563, 568, 293, 568, 1091,
169952 /* 1360 */ 568, 293, 568, 1091, 531, 568, 870, 8, 60, 60,
169953 /* 1370 */ 235, 61, 61, 568, 414, 568, 414, 568, 445, 62,
169954 /* 1380 */ 62, 45, 45, 46, 46, 47, 47, 199, 49, 49,
169955 /* 1390 */ 557, 568, 359, 568, 100, 486, 50, 50, 63, 63,
169956 /* 1400 */ 64, 64, 561, 415, 535, 410, 568, 1024, 568, 534,
169957 /* 1410 */ 316, 559, 316, 559, 65, 65, 14, 14, 568, 1024,
169958 /* 1420 */ 568, 512, 930, 870, 1015, 109, 109, 929, 1014, 66,
169959 /* 1430 */ 66, 131, 131, 110, 451, 445, 569, 445, 416, 177,
169960 /* 1440 */ 1014, 132, 132, 67, 67, 568, 467, 568, 930, 471,
169961 /* 1450 */ 1360, 283, 226, 929, 315, 1359, 407, 568, 459, 407,
169962 /* 1460 */ 1014, 1014, 1016, 239, 407, 86, 213, 1346, 52, 52,
169963 /* 1470 */ 68, 68, 1014, 1014, 1016, 1017, 27, 1577, 1174, 447,
169964 /* 1480 */ 69, 69, 288, 97, 108, 1535, 106, 392, 392, 391,
169965 /* 1490 */ 273, 389, 568, 877, 847, 881, 568, 111, 560, 466,
169966 /* 1500 */ 4, 568, 152, 30, 38, 568, 1128, 234, 396, 323,
169967 /* 1510 */ 111, 560, 527, 4, 563, 53, 53, 322, 568, 163,
169968 /* 1520 */ 163, 568, 337, 468, 164, 164, 333, 563, 76, 76,
169969 /* 1530 */ 568, 289, 1508, 568, 31, 1507, 568, 445, 338, 483,
169970 /* 1540 */ 100, 54, 54, 344, 72, 72, 296, 236, 1076, 557,
169971 /* 1550 */ 445, 877, 1356, 134, 134, 168, 73, 73, 141, 161,
169972 /* 1560 */ 161, 1566, 557, 535, 568, 319, 568, 348, 536, 1007,
169973 /* 1570 */ 473, 261, 261, 889, 888, 235, 535, 568, 1024, 568,
169974 /* 1580 */ 475, 534, 261, 367, 109, 109, 521, 136, 136, 130,
169975 /* 1590 */ 130, 1024, 110, 366, 445, 569, 445, 109, 109, 1014,
169976 /* 1600 */ 162, 162, 156, 156, 568, 110, 1076, 445, 569, 445,
169977 /* 1610 */ 410, 351, 1014, 568, 353, 316, 559, 568, 343, 568,
169978 /* 1620 */ 100, 497, 357, 258, 100, 896, 897, 140, 140, 355,
169979 /* 1630 */ 1306, 1014, 1014, 1016, 1017, 27, 139, 139, 362, 451,
169980 /* 1640 */ 137, 137, 138, 138, 1014, 1014, 1016, 1017, 27, 1174,
169981 /* 1650 */ 447, 568, 372, 288, 111, 560, 1018, 4, 392, 392,
169982 /* 1660 */ 391, 273, 389, 568, 1137, 847, 568, 1072, 568, 258,
169983 /* 1670 */ 492, 563, 568, 211, 75, 75, 555, 960, 234, 261,
169984 /* 1680 */ 323, 111, 560, 927, 4, 113, 77, 77, 322, 74,
169985 /* 1690 */ 74, 42, 42, 1369, 445, 48, 48, 1414, 563, 972,
169986 /* 1700 */ 973, 1088, 1087, 1088, 1087, 860, 557, 150, 928, 1342,
169987 /* 1710 */ 113, 1354, 554, 1419, 1018, 1271, 1262, 1250, 236, 1249,
169988 /* 1720 */ 1251, 445, 1585, 1339, 308, 276, 168, 309, 11, 141,
169989 /* 1730 */ 393, 310, 232, 557, 1401, 1024, 335, 291, 1396, 219,
169990 /* 1740 */ 336, 109, 109, 934, 297, 1406, 235, 341, 477, 110,
169991 /* 1750 */ 502, 445, 569, 445, 1389, 1405, 1014, 400, 1289, 365,
169992 /* 1760 */ 223, 1480, 1024, 1479, 1351, 1352, 1350, 1349, 109, 109,
169993 /* 1770 */ 204, 1588, 1226, 558, 265, 218, 110, 205, 445, 569,
169994 /* 1780 */ 445, 410, 387, 1014, 1527, 179, 316, 559, 1014, 1014,
169995 /* 1790 */ 1016, 1017, 27, 230, 1525, 1223, 79, 560, 85, 4,
169996 /* 1800 */ 418, 215, 548, 81, 84, 188, 1402, 173, 181, 461,
169997 /* 1810 */ 451, 35, 462, 563, 183, 1014, 1014, 1016, 1017, 27,
169998 /* 1820 */ 184, 1485, 185, 186, 495, 242, 98, 398, 1408, 36,
169999 /* 1830 */ 1407, 484, 91, 469, 401, 1410, 445, 192, 1474, 246,
170000 /* 1840 */ 1496, 490, 346, 277, 248, 196, 493, 511, 557, 350,
170001 /* 1850 */ 1252, 249, 250, 403, 1309, 1308, 111, 560, 432, 4,
170002 /* 1860 */ 1307, 1300, 93, 1602, 881, 1601, 224, 404, 434, 520,
170003 /* 1870 */ 263, 435, 1571, 563, 1279, 1278, 364, 1024, 306, 1277,
170004 /* 1880 */ 264, 1600, 1557, 109, 109, 370, 1299, 307, 1556, 438,
170005 /* 1890 */ 128, 110, 1374, 445, 569, 445, 445, 546, 1014, 10,
170006 /* 1900 */ 1461, 105, 381, 1373, 34, 571, 99, 1332, 557, 314,
170007 /* 1910 */ 1180, 530, 272, 274, 379, 210, 1331, 547, 385, 386,
170008 /* 1920 */ 275, 572, 1247, 1242, 411, 412, 1512, 165, 178, 1513,
170009 /* 1930 */ 1014, 1014, 1016, 1017, 27, 1511, 1510, 1024, 78, 147,
170010 /* 1940 */ 166, 220, 221, 109, 109, 834, 304, 167, 446, 212,
170011 /* 1950 */ 318, 110, 231, 445, 569, 445, 144, 1086, 1014, 1084,
170012 /* 1960 */ 326, 180, 169, 1205, 182, 334, 238, 913, 241, 1100,
170013 /* 1970 */ 187, 170, 171, 421, 87, 88, 423, 189, 89, 90,
170014 /* 1980 */ 172, 1103, 243, 1099, 244, 158, 18, 245, 345, 247,
170015 /* 1990 */ 1014, 1014, 1016, 1017, 27, 261, 1092, 193, 1220, 489,
170016 /* 2000 */ 194, 37, 366, 849, 494, 251, 195, 506, 92, 19,
170017 /* 2010 */ 498, 358, 20, 503, 879, 361, 94, 892, 305, 159,
170018 /* 2020 */ 513, 39, 95, 1168, 160, 1053, 964, 1139, 96, 174,
170019 /* 2030 */ 1138, 225, 280, 282, 198, 958, 113, 1158, 1154, 260,
170020 /* 2040 */ 21, 22, 23, 1156, 1162, 1161, 1143, 24, 33, 25,
170021 /* 2050 */ 202, 542, 26, 100, 1067, 102, 1054, 103, 7, 1052,
170022 /* 2060 */ 1056, 1109, 1057, 1108, 266, 267, 28, 40, 390, 1019,
170023 /* 2070 */ 861, 112, 29, 564, 1176, 1175, 268, 176, 143, 923,
170024 /* 2080 */ 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238,
170025 /* 2090 */ 1238, 1238, 1238, 1238, 269, 1593,
170026};
170027static const YYCODETYPE yy_lookahead[] = {
170028 /* 0 */ 193, 193, 193, 274, 275, 276, 193, 274, 275, 276,
170029 /* 10 */ 193, 223, 219, 225, 206, 210, 211, 212, 193, 19,
170030 /* 20 */ 219, 233, 216, 216, 217, 216, 217, 193, 295, 216,
170031 /* 30 */ 217, 31, 193, 216, 217, 193, 228, 213, 230, 39,
170032 /* 40 */ 206, 216, 217, 43, 44, 45, 46, 47, 48, 49,
170033 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 193, 19,
170034 /* 60 */ 185, 186, 187, 188, 189, 190, 253, 274, 275, 276,
170035 /* 70 */ 195, 193, 197, 193, 261, 274, 275, 276, 253, 204,
170036 /* 80 */ 238, 204, 81, 43, 44, 45, 46, 47, 48, 49,
170037 /* 90 */ 50, 51, 52, 53, 54, 55, 56, 57, 274, 275,
170038 /* 100 */ 276, 262, 102, 103, 104, 105, 106, 107, 108, 109,
170039 /* 110 */ 110, 111, 112, 113, 239, 240, 239, 240, 210, 211,
170040 /* 120 */ 212, 314, 315, 314, 59, 316, 86, 252, 88, 252,
170041 /* 130 */ 19, 314, 315, 256, 257, 113, 25, 72, 296, 138,
170042 /* 140 */ 139, 266, 102, 103, 104, 105, 106, 107, 108, 109,
170043 /* 150 */ 110, 111, 112, 113, 43, 44, 45, 46, 47, 48,
170044 /* 160 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 81,
170045 /* 170 */ 292, 59, 292, 298, 108, 109, 110, 111, 112, 113,
170046 /* 180 */ 69, 116, 117, 118, 72, 106, 107, 193, 111, 112,
170047 /* 190 */ 113, 54, 55, 56, 57, 58, 102, 103, 104, 105,
170048 /* 200 */ 106, 107, 108, 109, 110, 111, 112, 113, 120, 25,
170049 /* 210 */ 216, 217, 145, 102, 103, 104, 105, 106, 107, 108,
170050 /* 220 */ 109, 110, 111, 112, 113, 231, 138, 139, 116, 117,
170051 /* 230 */ 118, 164, 153, 19, 155, 54, 55, 56, 57, 102,
170052 /* 240 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
170053 /* 250 */ 113, 128, 129, 46, 47, 48, 49, 43, 44, 45,
170054 /* 260 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
170055 /* 270 */ 56, 57, 216, 193, 25, 59, 193, 19, 165, 166,
170056 /* 280 */ 193, 67, 24, 102, 103, 104, 105, 106, 107, 108,
170057 /* 290 */ 109, 110, 111, 112, 113, 73, 216, 217, 59, 216,
170058 /* 300 */ 217, 43, 44, 45, 46, 47, 48, 49, 50, 51,
170059 /* 310 */ 52, 53, 54, 55, 56, 57, 102, 103, 104, 105,
170060 /* 320 */ 106, 107, 108, 109, 110, 111, 112, 113, 121, 145,
170061 /* 330 */ 59, 193, 116, 117, 118, 119, 273, 204, 122, 123,
170062 /* 340 */ 124, 19, 20, 134, 22, 136, 137, 19, 132, 127,
170063 /* 350 */ 128, 129, 24, 22, 23, 116, 117, 118, 36, 193,
170064 /* 360 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
170065 /* 370 */ 112, 113, 239, 240, 311, 312, 215, 106, 107, 241,
170066 /* 380 */ 19, 59, 216, 217, 223, 252, 115, 116, 117, 118,
170067 /* 390 */ 151, 120, 26, 71, 193, 308, 309, 193, 149, 128,
170068 /* 400 */ 313, 216, 269, 81, 43, 44, 45, 46, 47, 48,
170069 /* 410 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 253,
170070 /* 420 */ 216, 217, 100, 95, 153, 59, 155, 261, 106, 107,
170071 /* 430 */ 25, 193, 101, 193, 193, 231, 114, 25, 116, 117,
170072 /* 440 */ 118, 113, 304, 121, 193, 204, 59, 119, 120, 121,
170073 /* 450 */ 122, 123, 124, 125, 216, 217, 193, 216, 217, 131,
170074 /* 460 */ 138, 139, 230, 102, 103, 104, 105, 106, 107, 108,
170075 /* 470 */ 109, 110, 111, 112, 113, 153, 154, 155, 156, 157,
170076 /* 480 */ 239, 240, 116, 117, 118, 76, 193, 23, 19, 25,
170077 /* 490 */ 22, 253, 23, 252, 253, 108, 87, 204, 89, 261,
170078 /* 500 */ 198, 92, 261, 116, 117, 118, 193, 306, 307, 216,
170079 /* 510 */ 217, 150, 43, 44, 45, 46, 47, 48, 49, 50,
170080 /* 520 */ 51, 52, 53, 54, 55, 56, 57, 59, 193, 216,
170081 /* 530 */ 217, 19, 239, 240, 283, 23, 106, 107, 108, 109,
170082 /* 540 */ 110, 111, 112, 113, 73, 252, 253, 142, 308, 309,
170083 /* 550 */ 138, 139, 81, 313, 145, 43, 44, 45, 46, 47,
170084 /* 560 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
170085 /* 570 */ 307, 102, 103, 104, 105, 106, 107, 108, 109, 110,
170086 /* 580 */ 111, 112, 113, 281, 116, 117, 118, 285, 23, 193,
170087 /* 590 */ 25, 119, 59, 193, 122, 123, 124, 59, 127, 203,
170088 /* 600 */ 59, 205, 19, 268, 132, 25, 23, 22, 193, 138,
170089 /* 610 */ 139, 249, 204, 251, 102, 103, 104, 105, 106, 107,
170090 /* 620 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
170091 /* 630 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170092 /* 640 */ 57, 19, 22, 23, 59, 23, 25, 239, 240, 116,
170093 /* 650 */ 117, 118, 193, 11, 116, 117, 118, 116, 117, 118,
170094 /* 660 */ 252, 269, 22, 193, 15, 43, 44, 45, 46, 47,
170095 /* 670 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
170096 /* 680 */ 273, 143, 193, 118, 143, 102, 103, 104, 105, 106,
170097 /* 690 */ 107, 108, 109, 110, 111, 112, 113, 76, 118, 59,
170098 /* 700 */ 241, 116, 117, 118, 304, 216, 217, 292, 143, 60,
170099 /* 710 */ 89, 241, 19, 92, 193, 193, 23, 22, 311, 312,
170100 /* 720 */ 231, 101, 22, 143, 102, 103, 104, 105, 106, 107,
170101 /* 730 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
170102 /* 740 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170103 /* 750 */ 57, 19, 193, 193, 59, 23, 116, 117, 118, 59,
170104 /* 760 */ 201, 21, 241, 304, 22, 206, 127, 128, 129, 193,
170105 /* 770 */ 128, 129, 235, 236, 304, 43, 44, 45, 46, 47,
170106 /* 780 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
170107 /* 790 */ 22, 193, 216, 217, 193, 102, 103, 104, 105, 106,
170108 /* 800 */ 107, 108, 109, 110, 111, 112, 113, 231, 193, 193,
170109 /* 810 */ 193, 116, 117, 118, 216, 217, 116, 117, 118, 226,
170110 /* 820 */ 80, 193, 19, 235, 236, 304, 23, 211, 212, 231,
170111 /* 830 */ 204, 216, 217, 205, 102, 103, 104, 105, 106, 107,
170112 /* 840 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
170113 /* 850 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170114 /* 860 */ 57, 19, 193, 123, 76, 239, 240, 193, 253, 239,
170115 /* 870 */ 240, 239, 240, 193, 106, 107, 193, 89, 252, 193,
170116 /* 880 */ 92, 59, 252, 141, 252, 43, 44, 45, 46, 47,
170117 /* 890 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
170118 /* 900 */ 284, 161, 216, 217, 193, 102, 103, 104, 105, 106,
170119 /* 910 */ 107, 108, 109, 110, 111, 112, 113, 231, 193, 16,
170120 /* 920 */ 187, 188, 189, 190, 7, 8, 9, 309, 195, 25,
170121 /* 930 */ 197, 313, 19, 127, 128, 129, 262, 204, 22, 117,
170122 /* 940 */ 24, 216, 217, 263, 102, 103, 104, 105, 106, 107,
170123 /* 950 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
170124 /* 960 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170125 /* 970 */ 57, 193, 239, 240, 193, 59, 19, 188, 253, 190,
170126 /* 980 */ 77, 226, 79, 193, 195, 252, 197, 193, 19, 301,
170127 /* 990 */ 302, 193, 193, 204, 216, 217, 226, 216, 217, 266,
170128 /* 1000 */ 204, 159, 45, 46, 47, 48, 49, 50, 51, 52,
170129 /* 1010 */ 53, 54, 55, 56, 57, 102, 103, 104, 105, 106,
170130 /* 1020 */ 107, 108, 109, 110, 111, 112, 113, 12, 239, 240,
170131 /* 1030 */ 232, 298, 238, 117, 253, 239, 240, 238, 259, 260,
170132 /* 1040 */ 193, 252, 27, 31, 193, 193, 142, 204, 252, 193,
170133 /* 1050 */ 193, 39, 262, 193, 100, 266, 278, 42, 204, 102,
170134 /* 1060 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
170135 /* 1070 */ 113, 117, 159, 216, 217, 121, 216, 217, 63, 193,
170136 /* 1080 */ 193, 193, 239, 240, 115, 116, 193, 298, 73, 238,
170137 /* 1090 */ 238, 231, 19, 239, 240, 252, 22, 24, 211, 212,
170138 /* 1100 */ 24, 193, 216, 217, 216, 217, 252, 153, 154, 155,
170139 /* 1110 */ 253, 16, 19, 144, 213, 268, 43, 44, 45, 46,
170140 /* 1120 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170141 /* 1130 */ 57, 238, 19, 59, 193, 59, 43, 44, 45, 46,
170142 /* 1140 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170143 /* 1150 */ 57, 22, 23, 193, 25, 193, 43, 44, 45, 46,
170144 /* 1160 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
170145 /* 1170 */ 57, 284, 77, 193, 79, 102, 103, 104, 105, 106,
170146 /* 1180 */ 107, 108, 109, 110, 111, 112, 113, 286, 193, 193,
170147 /* 1190 */ 193, 117, 291, 117, 232, 102, 103, 104, 105, 106,
170148 /* 1200 */ 107, 108, 109, 110, 111, 112, 113, 204, 22, 23,
170149 /* 1210 */ 66, 25, 216, 217, 35, 102, 103, 104, 105, 106,
170150 /* 1220 */ 107, 108, 109, 110, 111, 112, 113, 193, 268, 85,
170151 /* 1230 */ 101, 193, 309, 309, 240, 19, 313, 313, 94, 208,
170152 /* 1240 */ 209, 193, 239, 240, 193, 66, 252, 19, 268, 244,
170153 /* 1250 */ 216, 217, 193, 74, 213, 252, 161, 19, 263, 254,
170154 /* 1260 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
170155 /* 1270 */ 54, 55, 56, 57, 193, 216, 217, 5, 59, 193,
170156 /* 1280 */ 19, 244, 10, 11, 12, 13, 14, 101, 309, 17,
170157 /* 1290 */ 146, 254, 313, 193, 193, 76, 115, 216, 217, 309,
170158 /* 1300 */ 12, 263, 30, 313, 32, 46, 87, 46, 89, 130,
170159 /* 1310 */ 193, 92, 40, 22, 263, 27, 216, 217, 102, 103,
170160 /* 1320 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
170161 /* 1330 */ 42, 150, 291, 216, 217, 116, 117, 118, 19, 20,
170162 /* 1340 */ 193, 22, 70, 260, 116, 193, 24, 264, 193, 263,
170163 /* 1350 */ 78, 63, 61, 81, 116, 36, 193, 260, 193, 29,
170164 /* 1360 */ 193, 264, 193, 33, 145, 193, 59, 48, 216, 217,
170165 /* 1370 */ 98, 216, 217, 193, 115, 193, 115, 193, 59, 216,
170166 /* 1380 */ 217, 216, 217, 216, 217, 216, 217, 255, 216, 217,
170167 /* 1390 */ 71, 193, 131, 193, 25, 65, 216, 217, 216, 217,
170168 /* 1400 */ 216, 217, 208, 209, 85, 133, 193, 100, 193, 90,
170169 /* 1410 */ 138, 139, 138, 139, 216, 217, 216, 217, 193, 100,
170170 /* 1420 */ 193, 108, 135, 116, 117, 106, 107, 140, 121, 216,
170171 /* 1430 */ 217, 216, 217, 114, 162, 116, 117, 118, 299, 300,
170172 /* 1440 */ 121, 216, 217, 216, 217, 193, 244, 193, 135, 244,
170173 /* 1450 */ 193, 256, 257, 140, 244, 193, 254, 193, 193, 254,
170174 /* 1460 */ 153, 154, 155, 141, 254, 149, 150, 258, 216, 217,
170175 /* 1470 */ 216, 217, 153, 154, 155, 156, 157, 0, 1, 2,
170176 /* 1480 */ 216, 217, 5, 115, 158, 193, 160, 10, 11, 12,
170177 /* 1490 */ 13, 14, 193, 59, 17, 126, 193, 19, 20, 129,
170178 /* 1500 */ 22, 193, 22, 22, 24, 193, 23, 30, 25, 32,
170179 /* 1510 */ 19, 20, 144, 22, 36, 216, 217, 40, 193, 216,
170180 /* 1520 */ 217, 193, 152, 129, 216, 217, 193, 36, 216, 217,
170181 /* 1530 */ 193, 99, 193, 193, 53, 193, 193, 59, 23, 193,
170182 /* 1540 */ 25, 216, 217, 193, 216, 217, 152, 70, 59, 71,
170183 /* 1550 */ 59, 117, 193, 216, 217, 78, 216, 217, 81, 216,
170184 /* 1560 */ 217, 318, 71, 85, 193, 133, 193, 193, 90, 23,
170185 /* 1570 */ 23, 25, 25, 120, 121, 98, 85, 193, 100, 193,
170186 /* 1580 */ 23, 90, 25, 121, 106, 107, 19, 216, 217, 216,
170187 /* 1590 */ 217, 100, 114, 131, 116, 117, 118, 106, 107, 121,
170188 /* 1600 */ 216, 217, 216, 217, 193, 114, 117, 116, 117, 118,
170189 /* 1610 */ 133, 193, 121, 193, 193, 138, 139, 193, 23, 193,
170190 /* 1620 */ 25, 23, 23, 25, 25, 7, 8, 216, 217, 193,
170191 /* 1630 */ 193, 153, 154, 155, 156, 157, 216, 217, 193, 162,
170192 /* 1640 */ 216, 217, 216, 217, 153, 154, 155, 156, 157, 1,
170193 /* 1650 */ 2, 193, 193, 5, 19, 20, 59, 22, 10, 11,
170194 /* 1660 */ 12, 13, 14, 193, 97, 17, 193, 23, 193, 25,
170195 /* 1670 */ 288, 36, 193, 242, 216, 217, 236, 23, 30, 25,
170196 /* 1680 */ 32, 19, 20, 23, 22, 25, 216, 217, 40, 216,
170197 /* 1690 */ 217, 216, 217, 193, 59, 216, 217, 193, 36, 83,
170198 /* 1700 */ 84, 153, 153, 155, 155, 23, 71, 25, 23, 193,
170199 /* 1710 */ 25, 193, 193, 193, 117, 193, 193, 193, 70, 193,
170200 /* 1720 */ 193, 59, 193, 255, 255, 287, 78, 255, 243, 81,
170201 /* 1730 */ 191, 255, 297, 71, 271, 100, 293, 245, 267, 214,
170202 /* 1740 */ 246, 106, 107, 108, 246, 271, 98, 245, 293, 114,
170203 /* 1750 */ 220, 116, 117, 118, 267, 271, 121, 271, 225, 219,
170204 /* 1760 */ 229, 219, 100, 219, 259, 259, 259, 259, 106, 107,
170205 /* 1770 */ 249, 196, 60, 280, 141, 243, 114, 249, 116, 117,
170206 /* 1780 */ 118, 133, 245, 121, 200, 297, 138, 139, 153, 154,
170207 /* 1790 */ 155, 156, 157, 297, 200, 38, 19, 20, 151, 22,
170208 /* 1800 */ 200, 150, 140, 294, 294, 22, 272, 43, 234, 18,
170209 /* 1810 */ 162, 270, 200, 36, 237, 153, 154, 155, 156, 157,
170210 /* 1820 */ 237, 283, 237, 237, 18, 199, 149, 246, 272, 270,
170211 /* 1830 */ 272, 200, 158, 246, 246, 234, 59, 234, 246, 199,
170212 /* 1840 */ 290, 62, 289, 200, 199, 22, 221, 115, 71, 200,
170213 /* 1850 */ 200, 199, 199, 221, 218, 218, 19, 20, 64, 22,
170214 /* 1860 */ 218, 227, 22, 224, 126, 224, 165, 221, 24, 305,
170215 /* 1870 */ 200, 113, 312, 36, 218, 220, 218, 100, 282, 218,
170216 /* 1880 */ 91, 218, 317, 106, 107, 221, 227, 282, 317, 82,
170217 /* 1890 */ 148, 114, 265, 116, 117, 118, 59, 145, 121, 22,
170218 /* 1900 */ 277, 158, 200, 265, 25, 202, 147, 250, 71, 279,
170219 /* 1910 */ 13, 146, 194, 194, 249, 248, 250, 140, 247, 246,
170220 /* 1920 */ 6, 192, 192, 192, 303, 303, 213, 207, 300, 213,
170221 /* 1930 */ 153, 154, 155, 156, 157, 213, 213, 100, 213, 222,
170222 /* 1940 */ 207, 214, 214, 106, 107, 4, 222, 207, 3, 22,
170223 /* 1950 */ 163, 114, 15, 116, 117, 118, 16, 23, 121, 23,
170224 /* 1960 */ 139, 151, 130, 25, 142, 16, 24, 20, 144, 1,
170225 /* 1970 */ 142, 130, 130, 61, 53, 53, 37, 151, 53, 53,
170226 /* 1980 */ 130, 116, 34, 1, 141, 5, 22, 115, 161, 141,
170227 /* 1990 */ 153, 154, 155, 156, 157, 25, 68, 68, 75, 41,
170228 /* 2000 */ 115, 24, 131, 20, 19, 125, 22, 96, 22, 22,
170229 /* 2010 */ 67, 23, 22, 67, 59, 24, 22, 28, 67, 23,
170230 /* 2020 */ 22, 22, 149, 23, 23, 23, 116, 23, 25, 37,
170231 /* 2030 */ 97, 141, 23, 23, 22, 143, 25, 75, 88, 34,
170232 /* 2040 */ 34, 34, 34, 86, 75, 93, 23, 34, 22, 34,
170233 /* 2050 */ 25, 24, 34, 25, 23, 142, 23, 142, 44, 23,
170234 /* 2060 */ 23, 23, 11, 23, 25, 22, 22, 22, 15, 23,
170235 /* 2070 */ 23, 22, 22, 25, 1, 1, 141, 25, 23, 135,
170236 /* 2080 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170237 /* 2090 */ 319, 319, 319, 319, 141, 141, 319, 319, 319, 319,
170238 /* 2100 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170239 /* 2110 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170240 /* 2120 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170241 /* 2130 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170242 /* 2140 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170243 /* 2150 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170244 /* 2160 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170245 /* 2170 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170246 /* 2180 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170247 /* 2190 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170248 /* 2200 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170249 /* 2210 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170250 /* 2220 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170251 /* 2230 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170252 /* 2240 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170253 /* 2250 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170254 /* 2260 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170255 /* 2270 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
170256 /* 2280 */ 319,
170257};
170258#define YY_SHIFT_COUNT (574)
170259#define YY_SHIFT_MIN (0)
170260#define YY_SHIFT_MAX (2074)
170261static const unsigned short int yy_shift_ofst[] = {
170262 /* 0 */ 1648, 1477, 1272, 322, 322, 1, 1319, 1478, 1491, 1837,
170263 /* 10 */ 1837, 1837, 471, 0, 0, 214, 1093, 1837, 1837, 1837,
170264 /* 20 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
170265 /* 30 */ 271, 271, 1219, 1219, 216, 88, 1, 1, 1, 1,
170266 /* 40 */ 1, 40, 111, 258, 361, 469, 512, 583, 622, 693,
170267 /* 50 */ 732, 803, 842, 913, 1073, 1093, 1093, 1093, 1093, 1093,
170268 /* 60 */ 1093, 1093, 1093, 1093, 1093, 1093, 1093, 1093, 1093, 1093,
170269 /* 70 */ 1093, 1093, 1093, 1113, 1093, 1216, 957, 957, 1635, 1662,
170270 /* 80 */ 1777, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
170271 /* 90 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
170272 /* 100 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
170273 /* 110 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
170274 /* 120 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
170275 /* 130 */ 137, 181, 181, 181, 181, 181, 181, 181, 94, 430,
170276 /* 140 */ 66, 65, 112, 366, 533, 533, 740, 1261, 533, 533,
170277 /* 150 */ 79, 79, 533, 412, 412, 412, 77, 412, 123, 113,
170278 /* 160 */ 113, 22, 22, 2096, 2096, 328, 328, 328, 239, 468,
170279 /* 170 */ 468, 468, 468, 1015, 1015, 409, 366, 1129, 1186, 533,
170280 /* 180 */ 533, 533, 533, 533, 533, 533, 533, 533, 533, 533,
170281 /* 190 */ 533, 533, 533, 533, 533, 533, 533, 533, 533, 969,
170282 /* 200 */ 621, 621, 533, 642, 788, 788, 1228, 1228, 822, 822,
170283 /* 210 */ 67, 1274, 2096, 2096, 2096, 2096, 2096, 2096, 2096, 1307,
170284 /* 220 */ 954, 954, 585, 472, 640, 387, 695, 538, 541, 700,
170285 /* 230 */ 533, 533, 533, 533, 533, 533, 533, 533, 533, 533,
170286 /* 240 */ 222, 533, 533, 533, 533, 533, 533, 533, 533, 533,
170287 /* 250 */ 533, 533, 533, 1179, 1179, 1179, 533, 533, 533, 565,
170288 /* 260 */ 533, 533, 533, 916, 1144, 533, 533, 1288, 533, 533,
170289 /* 270 */ 533, 533, 533, 533, 533, 533, 639, 1330, 209, 1076,
170290 /* 280 */ 1076, 1076, 1076, 580, 209, 209, 1313, 768, 917, 649,
170291 /* 290 */ 1181, 1316, 405, 1316, 1238, 249, 1181, 1181, 249, 1181,
170292 /* 300 */ 405, 1238, 1369, 464, 1259, 1012, 1012, 1012, 1368, 1368,
170293 /* 310 */ 1368, 1368, 184, 184, 1326, 904, 1287, 1480, 1712, 1712,
170294 /* 320 */ 1633, 1633, 1757, 1757, 1633, 1647, 1651, 1783, 1764, 1791,
170295 /* 330 */ 1791, 1791, 1791, 1633, 1806, 1677, 1651, 1651, 1677, 1783,
170296 /* 340 */ 1764, 1677, 1764, 1677, 1633, 1806, 1674, 1779, 1633, 1806,
170297 /* 350 */ 1823, 1633, 1806, 1633, 1806, 1823, 1732, 1732, 1732, 1794,
170298 /* 360 */ 1840, 1840, 1823, 1732, 1738, 1732, 1794, 1732, 1732, 1701,
170299 /* 370 */ 1844, 1758, 1758, 1823, 1633, 1789, 1789, 1807, 1807, 1742,
170300 /* 380 */ 1752, 1877, 1633, 1743, 1742, 1759, 1765, 1677, 1879, 1897,
170301 /* 390 */ 1897, 1914, 1914, 1914, 2096, 2096, 2096, 2096, 2096, 2096,
170302 /* 400 */ 2096, 2096, 2096, 2096, 2096, 2096, 2096, 2096, 2096, 207,
170303 /* 410 */ 1095, 331, 620, 903, 806, 1074, 1483, 1432, 1481, 1322,
170304 /* 420 */ 1370, 1394, 1515, 1291, 1546, 1547, 1557, 1595, 1598, 1599,
170305 /* 430 */ 1434, 1453, 1618, 1462, 1567, 1489, 1644, 1654, 1616, 1660,
170306 /* 440 */ 1548, 1549, 1682, 1685, 1597, 742, 1941, 1945, 1927, 1787,
170307 /* 450 */ 1937, 1940, 1934, 1936, 1821, 1810, 1832, 1938, 1938, 1942,
170308 /* 460 */ 1822, 1947, 1824, 1949, 1968, 1828, 1841, 1938, 1842, 1912,
170309 /* 470 */ 1939, 1938, 1826, 1921, 1922, 1925, 1926, 1850, 1865, 1948,
170310 /* 480 */ 1843, 1982, 1980, 1964, 1872, 1827, 1928, 1970, 1929, 1923,
170311 /* 490 */ 1958, 1848, 1885, 1977, 1983, 1985, 1871, 1880, 1984, 1943,
170312 /* 500 */ 1986, 1987, 1988, 1990, 1946, 1955, 1991, 1911, 1989, 1994,
170313 /* 510 */ 1951, 1992, 1996, 1873, 1998, 2000, 2001, 2002, 2003, 2004,
170314 /* 520 */ 1999, 1933, 1890, 2009, 2010, 1910, 2005, 2012, 1892, 2011,
170315 /* 530 */ 2006, 2007, 2008, 2013, 1950, 1962, 1957, 2014, 1969, 1952,
170316 /* 540 */ 2015, 2023, 2026, 2027, 2025, 2028, 2018, 1913, 1915, 2031,
170317 /* 550 */ 2011, 2033, 2036, 2037, 2038, 2039, 2040, 2043, 2051, 2044,
170318 /* 560 */ 2045, 2046, 2047, 2049, 2050, 2048, 1944, 1935, 1953, 1954,
170319 /* 570 */ 2052, 2055, 2053, 2073, 2074,
170320};
170321#define YY_REDUCE_COUNT (408)
170322#define YY_REDUCE_MIN (-271)
170323#define YY_REDUCE_MAX (1740)
170324static const short yy_reduce_ofst[] = {
170325 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
170326 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
170327 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
170328 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
170329 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
170330 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
170331 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
170332 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
170333 /* 80 */ 313, 886, 888, 996, 1034, 1059, 1081, 1100, 1117, 1152,
170334 /* 90 */ 1155, 1163, 1165, 1167, 1169, 1172, 1180, 1182, 1184, 1198,
170335 /* 100 */ 1200, 1213, 1215, 1225, 1227, 1252, 1254, 1264, 1299, 1303,
170336 /* 110 */ 1308, 1312, 1325, 1328, 1337, 1340, 1343, 1371, 1373, 1384,
170337 /* 120 */ 1386, 1411, 1420, 1424, 1426, 1458, 1470, 1473, 1475, 1479,
170338 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
170339 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
170340 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
170341 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
170342 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
170343 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
170344 /* 190 */ 995, 1038, 415, 1051, 893, 798, 962, 400, 1086, 779,
170345 /* 200 */ 923, 924, 263, 1041, 979, 990, 1083, 1097, 1031, 1194,
170346 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
170347 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
170348 /* 230 */ 711, 856, 908, 941, 1048, 1101, 1147, 1257, 1262, 1265,
170349 /* 240 */ 392, 1292, 1333, 1339, 1342, 1346, 1350, 1359, 1374, 1418,
170350 /* 250 */ 1421, 1436, 1437, 593, 755, 770, 997, 1445, 1459, 1209,
170351 /* 260 */ 1500, 1504, 1516, 1132, 1243, 1518, 1519, 1440, 1520, 560,
170352 /* 270 */ 1522, 1523, 1524, 1526, 1527, 1529, 1382, 1438, 1431, 1468,
170353 /* 280 */ 1469, 1472, 1476, 1209, 1431, 1431, 1485, 1525, 1539, 1435,
170354 /* 290 */ 1463, 1471, 1492, 1487, 1443, 1494, 1474, 1484, 1498, 1486,
170355 /* 300 */ 1502, 1455, 1530, 1531, 1533, 1540, 1542, 1544, 1505, 1506,
170356 /* 310 */ 1507, 1508, 1521, 1528, 1493, 1537, 1532, 1575, 1488, 1496,
170357 /* 320 */ 1584, 1594, 1509, 1510, 1600, 1538, 1534, 1541, 1574, 1577,
170358 /* 330 */ 1583, 1585, 1586, 1612, 1626, 1581, 1556, 1558, 1587, 1559,
170359 /* 340 */ 1601, 1588, 1603, 1592, 1631, 1640, 1550, 1553, 1643, 1645,
170360 /* 350 */ 1625, 1649, 1652, 1650, 1653, 1632, 1636, 1637, 1642, 1634,
170361 /* 360 */ 1639, 1641, 1646, 1656, 1655, 1658, 1659, 1661, 1663, 1560,
170362 /* 370 */ 1564, 1596, 1605, 1664, 1670, 1565, 1571, 1627, 1638, 1657,
170363 /* 380 */ 1665, 1623, 1702, 1630, 1666, 1667, 1671, 1673, 1703, 1718,
170364 /* 390 */ 1719, 1729, 1730, 1731, 1621, 1622, 1628, 1720, 1713, 1716,
170365 /* 400 */ 1722, 1723, 1733, 1717, 1724, 1727, 1728, 1725, 1740,
170366};
170367static const YYACTIONTYPE yy_default[] = {
170368 /* 0 */ 1639, 1639, 1639, 1469, 1236, 1347, 1236, 1236, 1236, 1469,
170369 /* 10 */ 1469, 1469, 1236, 1377, 1377, 1522, 1269, 1236, 1236, 1236,
170370 /* 20 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1468, 1236, 1236,
170371 /* 30 */ 1236, 1236, 1555, 1555, 1236, 1236, 1236, 1236, 1236, 1236,
170372 /* 40 */ 1236, 1236, 1386, 1236, 1393, 1236, 1236, 1236, 1236, 1236,
170373 /* 50 */ 1470, 1471, 1236, 1236, 1236, 1521, 1523, 1486, 1400, 1399,
170374 /* 60 */ 1398, 1397, 1504, 1365, 1391, 1384, 1388, 1465, 1466, 1464,
170375 /* 70 */ 1617, 1471, 1470, 1236, 1387, 1433, 1449, 1432, 1236, 1236,
170376 /* 80 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170377 /* 90 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170378 /* 100 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170379 /* 110 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170380 /* 120 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170381 /* 130 */ 1441, 1448, 1447, 1446, 1455, 1445, 1442, 1435, 1434, 1436,
170382 /* 140 */ 1437, 1236, 1236, 1260, 1236, 1236, 1257, 1311, 1236, 1236,
170383 /* 150 */ 1236, 1236, 1236, 1541, 1540, 1236, 1438, 1236, 1269, 1427,
170384 /* 160 */ 1426, 1452, 1439, 1451, 1450, 1529, 1591, 1590, 1487, 1236,
170385 /* 170 */ 1236, 1236, 1236, 1236, 1236, 1555, 1236, 1236, 1236, 1236,
170386 /* 180 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170387 /* 190 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1367,
170388 /* 200 */ 1555, 1555, 1236, 1269, 1555, 1555, 1368, 1368, 1265, 1265,
170389 /* 210 */ 1371, 1236, 1536, 1338, 1338, 1338, 1338, 1347, 1338, 1236,
170390 /* 220 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170391 /* 230 */ 1236, 1236, 1236, 1236, 1526, 1524, 1236, 1236, 1236, 1236,
170392 /* 240 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170393 /* 250 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170394 /* 260 */ 1236, 1236, 1236, 1343, 1236, 1236, 1236, 1236, 1236, 1236,
170395 /* 270 */ 1236, 1236, 1236, 1236, 1236, 1584, 1236, 1499, 1325, 1343,
170396 /* 280 */ 1343, 1343, 1343, 1345, 1326, 1324, 1337, 1270, 1243, 1631,
170397 /* 290 */ 1403, 1392, 1344, 1392, 1628, 1390, 1403, 1403, 1390, 1403,
170398 /* 300 */ 1344, 1628, 1286, 1606, 1281, 1377, 1377, 1377, 1367, 1367,
170399 /* 310 */ 1367, 1367, 1371, 1371, 1467, 1344, 1337, 1236, 1631, 1631,
170400 /* 320 */ 1353, 1353, 1630, 1630, 1353, 1487, 1614, 1412, 1314, 1320,
170401 /* 330 */ 1320, 1320, 1320, 1353, 1254, 1390, 1614, 1614, 1390, 1412,
170402 /* 340 */ 1314, 1390, 1314, 1390, 1353, 1254, 1503, 1625, 1353, 1254,
170403 /* 350 */ 1477, 1353, 1254, 1353, 1254, 1477, 1312, 1312, 1312, 1301,
170404 /* 360 */ 1236, 1236, 1477, 1312, 1286, 1312, 1301, 1312, 1312, 1573,
170405 /* 370 */ 1236, 1481, 1481, 1477, 1353, 1565, 1565, 1380, 1380, 1385,
170406 /* 380 */ 1371, 1472, 1353, 1236, 1385, 1383, 1381, 1390, 1304, 1587,
170407 /* 390 */ 1587, 1583, 1583, 1583, 1636, 1636, 1536, 1599, 1269, 1269,
170408 /* 400 */ 1269, 1269, 1599, 1288, 1288, 1270, 1270, 1269, 1599, 1236,
170409 /* 410 */ 1236, 1236, 1236, 1236, 1236, 1594, 1236, 1531, 1488, 1357,
170410 /* 420 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170411 /* 430 */ 1236, 1236, 1236, 1236, 1542, 1236, 1236, 1236, 1236, 1236,
170412 /* 440 */ 1236, 1236, 1236, 1236, 1236, 1417, 1236, 1239, 1533, 1236,
170413 /* 450 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1394, 1395, 1358,
170414 /* 460 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1409, 1236, 1236,
170415 /* 470 */ 1236, 1404, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170416 /* 480 */ 1627, 1236, 1236, 1236, 1236, 1236, 1236, 1502, 1501, 1236,
170417 /* 490 */ 1236, 1355, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170418 /* 500 */ 1236, 1236, 1236, 1236, 1236, 1284, 1236, 1236, 1236, 1236,
170419 /* 510 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170420 /* 520 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1382,
170421 /* 530 */ 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170422 /* 540 */ 1236, 1236, 1236, 1236, 1570, 1372, 1236, 1236, 1236, 1236,
170423 /* 550 */ 1618, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236, 1236,
170424 /* 560 */ 1236, 1236, 1236, 1236, 1236, 1610, 1328, 1418, 1236, 1421,
170425 /* 570 */ 1258, 1236, 1248, 1236, 1236,
170426};
170427/********** End of lemon-generated parsing tables *****************************/
170428
170429/* The next table maps tokens (terminal symbols) into fallback tokens.
170430** If a construct like the following:
170431**
170432** %fallback ID X Y Z.
170433**
170434** appears in the grammar, then ID becomes a fallback token for X, Y,
170435** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
170436** but it does not parse, the type of the token is changed to ID and
170437** the parse is retried before an error is thrown.
170438**
170439** This feature can be used, for example, to cause some keywords in a language
170440** to revert to identifiers if they keyword does not apply in the context where
170441** it appears.
170442*/
170443#ifdef YYFALLBACK
170444static const YYCODETYPE yyFallback[] = {
170445 0, /* $ => nothing */
170446 0, /* SEMI => nothing */
170447 59, /* EXPLAIN => ID */
170448 59, /* QUERY => ID */
170449 59, /* PLAN => ID */
170450 59, /* BEGIN => ID */
170451 0, /* TRANSACTION => nothing */
170452 59, /* DEFERRED => ID */
170453 59, /* IMMEDIATE => ID */
170454 59, /* EXCLUSIVE => ID */
170455 0, /* COMMIT => nothing */
170456 59, /* END => ID */
170457 59, /* ROLLBACK => ID */
170458 59, /* SAVEPOINT => ID */
170459 59, /* RELEASE => ID */
170460 0, /* TO => nothing */
170461 0, /* TABLE => nothing */
170462 0, /* CREATE => nothing */
170463 59, /* IF => ID */
170464 0, /* NOT => nothing */
170465 0, /* EXISTS => nothing */
170466 59, /* TEMP => ID */
170467 0, /* LP => nothing */
170468 0, /* RP => nothing */
170469 0, /* AS => nothing */
170470 0, /* COMMA => nothing */
170471 59, /* WITHOUT => ID */
170472 59, /* ABORT => ID */
170473 59, /* ACTION => ID */
170474 59, /* AFTER => ID */
170475 59, /* ANALYZE => ID */
170476 59, /* ASC => ID */
170477 59, /* ATTACH => ID */
170478 59, /* BEFORE => ID */
170479 59, /* BY => ID */
170480 59, /* CASCADE => ID */
170481 59, /* CAST => ID */
170482 59, /* CONFLICT => ID */
170483 59, /* DATABASE => ID */
170484 59, /* DESC => ID */
170485 59, /* DETACH => ID */
170486 59, /* EACH => ID */
170487 59, /* FAIL => ID */
170488 0, /* OR => nothing */
170489 0, /* AND => nothing */
170490 0, /* IS => nothing */
170491 59, /* MATCH => ID */
170492 59, /* LIKE_KW => ID */
170493 0, /* BETWEEN => nothing */
170494 0, /* IN => nothing */
170495 0, /* ISNULL => nothing */
170496 0, /* NOTNULL => nothing */
170497 0, /* NE => nothing */
170498 0, /* EQ => nothing */
170499 0, /* GT => nothing */
170500 0, /* LE => nothing */
170501 0, /* LT => nothing */
170502 0, /* GE => nothing */
170503 0, /* ESCAPE => nothing */
170504 0, /* ID => nothing */
170505 59, /* COLUMNKW => ID */
170506 59, /* DO => ID */
170507 59, /* FOR => ID */
170508 59, /* IGNORE => ID */
170509 59, /* INITIALLY => ID */
170510 59, /* INSTEAD => ID */
170511 59, /* NO => ID */
170512 59, /* KEY => ID */
170513 59, /* OF => ID */
170514 59, /* OFFSET => ID */
170515 59, /* PRAGMA => ID */
170516 59, /* RAISE => ID */
170517 59, /* RECURSIVE => ID */
170518 59, /* REPLACE => ID */
170519 59, /* RESTRICT => ID */
170520 59, /* ROW => ID */
170521 59, /* ROWS => ID */
170522 59, /* TRIGGER => ID */
170523 59, /* VACUUM => ID */
170524 59, /* VIEW => ID */
170525 59, /* VIRTUAL => ID */
170526 59, /* WITH => ID */
170527 59, /* NULLS => ID */
170528 59, /* FIRST => ID */
170529 59, /* LAST => ID */
170530 59, /* CURRENT => ID */
170531 59, /* FOLLOWING => ID */
170532 59, /* PARTITION => ID */
170533 59, /* PRECEDING => ID */
170534 59, /* RANGE => ID */
170535 59, /* UNBOUNDED => ID */
170536 59, /* EXCLUDE => ID */
170537 59, /* GROUPS => ID */
170538 59, /* OTHERS => ID */
170539 59, /* TIES => ID */
170540 59, /* GENERATED => ID */
170541 59, /* ALWAYS => ID */
170542 59, /* MATERIALIZED => ID */
170543 59, /* REINDEX => ID */
170544 59, /* RENAME => ID */
170545 59, /* CTIME_KW => ID */
170546 0, /* ANY => nothing */
170547 0, /* BITAND => nothing */
170548 0, /* BITOR => nothing */
170549 0, /* LSHIFT => nothing */
170550 0, /* RSHIFT => nothing */
170551 0, /* PLUS => nothing */
170552 0, /* MINUS => nothing */
170553 0, /* STAR => nothing */
170554 0, /* SLASH => nothing */
170555 0, /* REM => nothing */
170556 0, /* CONCAT => nothing */
170557 0, /* PTR => nothing */
170558 0, /* COLLATE => nothing */
170559 0, /* BITNOT => nothing */
170560 0, /* ON => nothing */
170561 0, /* INDEXED => nothing */
170562 0, /* STRING => nothing */
170563 0, /* JOIN_KW => nothing */
170564 0, /* CONSTRAINT => nothing */
170565 0, /* DEFAULT => nothing */
170566 0, /* NULL => nothing */
170567 0, /* PRIMARY => nothing */
170568 0, /* UNIQUE => nothing */
170569 0, /* CHECK => nothing */
170570 0, /* REFERENCES => nothing */
170571 0, /* AUTOINCR => nothing */
170572 0, /* INSERT => nothing */
170573 0, /* DELETE => nothing */
170574 0, /* UPDATE => nothing */
170575 0, /* SET => nothing */
170576 0, /* DEFERRABLE => nothing */
170577 0, /* FOREIGN => nothing */
170578 0, /* DROP => nothing */
170579 0, /* UNION => nothing */
170580 0, /* ALL => nothing */
170581 0, /* EXCEPT => nothing */
170582 0, /* INTERSECT => nothing */
170583 0, /* SELECT => nothing */
170584 0, /* VALUES => nothing */
170585 0, /* DISTINCT => nothing */
170586 0, /* DOT => nothing */
170587 0, /* FROM => nothing */
170588 0, /* JOIN => nothing */
170589 0, /* USING => nothing */
170590 0, /* ORDER => nothing */
170591 0, /* GROUP => nothing */
170592 0, /* HAVING => nothing */
170593 0, /* LIMIT => nothing */
170594 0, /* WHERE => nothing */
170595 0, /* RETURNING => nothing */
170596 0, /* INTO => nothing */
170597 0, /* NOTHING => nothing */
170598 0, /* FLOAT => nothing */
170599 0, /* BLOB => nothing */
170600 0, /* INTEGER => nothing */
170601 0, /* VARIABLE => nothing */
170602 0, /* CASE => nothing */
170603 0, /* WHEN => nothing */
170604 0, /* THEN => nothing */
170605 0, /* ELSE => nothing */
170606 0, /* INDEX => nothing */
170607 0, /* ALTER => nothing */
170608 0, /* ADD => nothing */
170609 0, /* WINDOW => nothing */
170610 0, /* OVER => nothing */
170611 0, /* FILTER => nothing */
170612 0, /* COLUMN => nothing */
170613 0, /* AGG_FUNCTION => nothing */
170614 0, /* AGG_COLUMN => nothing */
170615 0, /* TRUEFALSE => nothing */
170616 0, /* ISNOT => nothing */
170617 0, /* FUNCTION => nothing */
170618 0, /* UMINUS => nothing */
170619 0, /* UPLUS => nothing */
170620 0, /* TRUTH => nothing */
170621 0, /* REGISTER => nothing */
170622 0, /* VECTOR => nothing */
170623 0, /* SELECT_COLUMN => nothing */
170624 0, /* IF_NULL_ROW => nothing */
170625 0, /* ASTERISK => nothing */
170626 0, /* SPAN => nothing */
170627 0, /* ERROR => nothing */
170628 0, /* SPACE => nothing */
170629 0, /* ILLEGAL => nothing */
170630};
170631#endif /* YYFALLBACK */
170632
170633/* The following structure represents a single element of the
170634** parser's stack. Information stored includes:
170635**
170636** + The state number for the parser at this level of the stack.
170637**
170638** + The value of the token stored at this level of the stack.
170639** (In other words, the "major" token.)
170640**
170641** + The semantic value stored at this level of the stack. This is
170642** the information used by the action routines in the grammar.
170643** It is sometimes called the "minor" token.
170644**
170645** After the "shift" half of a SHIFTREDUCE action, the stateno field
170646** actually contains the reduce action for the second half of the
170647** SHIFTREDUCE.
170648*/
170649struct yyStackEntry {
170650 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
170651 YYCODETYPE major; /* The major token value. This is the code
170652 ** number for the token at this stack level */
170653 YYMINORTYPE minor; /* The user-supplied minor token value. This
170654 ** is the value of the token */
170655};
170656typedef struct yyStackEntry yyStackEntry;
170657
170658/* The state of the parser is completely contained in an instance of
170659** the following structure */
170660struct yyParser {
170661 yyStackEntry *yytos; /* Pointer to top element of the stack */
170662#ifdef YYTRACKMAXSTACKDEPTH
170663 int yyhwm; /* High-water mark of the stack */
170664#endif
170665#ifndef YYNOERRORRECOVERY
170666 int yyerrcnt; /* Shifts left before out of the error */
170667#endif
170668 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
170669 sqlite3ParserCTX_SDECL /* A place to hold %extra_context */
170670#if YYSTACKDEPTH<=0
170671 int yystksz; /* Current side of the stack */
170672 yyStackEntry *yystack; /* The parser's stack */
170673 yyStackEntry yystk0; /* First stack entry */
170674#else
170675 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
170676 yyStackEntry *yystackEnd; /* Last entry in the stack */
170677#endif
170678};
170679typedef struct yyParser yyParser;
170680
170681/* #include <assert.h> */
170682#ifndef NDEBUG
170683/* #include <stdio.h> */
170684static FILE *yyTraceFILE = 0;
170685static char *yyTracePrompt = 0;
170686#endif /* NDEBUG */
170687
170688#ifndef NDEBUG
170689/*
170690** Turn parser tracing on by giving a stream to which to write the trace
170691** and a prompt to preface each trace message. Tracing is turned off
170692** by making either argument NULL
170693**
170694** Inputs:
170695** <ul>
170696** <li> A FILE* to which trace output should be written.
170697** If NULL, then tracing is turned off.
170698** <li> A prefix string written at the beginning of every
170699** line of trace output. If NULL, then tracing is
170700** turned off.
170701** </ul>
170702**
170703** Outputs:
170704** None.
170705*/
170706SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
170707 yyTraceFILE = TraceFILE;
170708 yyTracePrompt = zTracePrompt;
170709 if( yyTraceFILE==0 ) yyTracePrompt = 0;
170710 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
170711}
170712#endif /* NDEBUG */
170713
170714#if defined(YYCOVERAGE) || !defined(NDEBUG)
170715/* For tracing shifts, the names of all terminals and nonterminals
170716** are required. The following table supplies these names */
170717static const char *const yyTokenName[] = {
170718 /* 0 */ "$",
170719 /* 1 */ "SEMI",
170720 /* 2 */ "EXPLAIN",
170721 /* 3 */ "QUERY",
170722 /* 4 */ "PLAN",
170723 /* 5 */ "BEGIN",
170724 /* 6 */ "TRANSACTION",
170725 /* 7 */ "DEFERRED",
170726 /* 8 */ "IMMEDIATE",
170727 /* 9 */ "EXCLUSIVE",
170728 /* 10 */ "COMMIT",
170729 /* 11 */ "END",
170730 /* 12 */ "ROLLBACK",
170731 /* 13 */ "SAVEPOINT",
170732 /* 14 */ "RELEASE",
170733 /* 15 */ "TO",
170734 /* 16 */ "TABLE",
170735 /* 17 */ "CREATE",
170736 /* 18 */ "IF",
170737 /* 19 */ "NOT",
170738 /* 20 */ "EXISTS",
170739 /* 21 */ "TEMP",
170740 /* 22 */ "LP",
170741 /* 23 */ "RP",
170742 /* 24 */ "AS",
170743 /* 25 */ "COMMA",
170744 /* 26 */ "WITHOUT",
170745 /* 27 */ "ABORT",
170746 /* 28 */ "ACTION",
170747 /* 29 */ "AFTER",
170748 /* 30 */ "ANALYZE",
170749 /* 31 */ "ASC",
170750 /* 32 */ "ATTACH",
170751 /* 33 */ "BEFORE",
170752 /* 34 */ "BY",
170753 /* 35 */ "CASCADE",
170754 /* 36 */ "CAST",
170755 /* 37 */ "CONFLICT",
170756 /* 38 */ "DATABASE",
170757 /* 39 */ "DESC",
170758 /* 40 */ "DETACH",
170759 /* 41 */ "EACH",
170760 /* 42 */ "FAIL",
170761 /* 43 */ "OR",
170762 /* 44 */ "AND",
170763 /* 45 */ "IS",
170764 /* 46 */ "MATCH",
170765 /* 47 */ "LIKE_KW",
170766 /* 48 */ "BETWEEN",
170767 /* 49 */ "IN",
170768 /* 50 */ "ISNULL",
170769 /* 51 */ "NOTNULL",
170770 /* 52 */ "NE",
170771 /* 53 */ "EQ",
170772 /* 54 */ "GT",
170773 /* 55 */ "LE",
170774 /* 56 */ "LT",
170775 /* 57 */ "GE",
170776 /* 58 */ "ESCAPE",
170777 /* 59 */ "ID",
170778 /* 60 */ "COLUMNKW",
170779 /* 61 */ "DO",
170780 /* 62 */ "FOR",
170781 /* 63 */ "IGNORE",
170782 /* 64 */ "INITIALLY",
170783 /* 65 */ "INSTEAD",
170784 /* 66 */ "NO",
170785 /* 67 */ "KEY",
170786 /* 68 */ "OF",
170787 /* 69 */ "OFFSET",
170788 /* 70 */ "PRAGMA",
170789 /* 71 */ "RAISE",
170790 /* 72 */ "RECURSIVE",
170791 /* 73 */ "REPLACE",
170792 /* 74 */ "RESTRICT",
170793 /* 75 */ "ROW",
170794 /* 76 */ "ROWS",
170795 /* 77 */ "TRIGGER",
170796 /* 78 */ "VACUUM",
170797 /* 79 */ "VIEW",
170798 /* 80 */ "VIRTUAL",
170799 /* 81 */ "WITH",
170800 /* 82 */ "NULLS",
170801 /* 83 */ "FIRST",
170802 /* 84 */ "LAST",
170803 /* 85 */ "CURRENT",
170804 /* 86 */ "FOLLOWING",
170805 /* 87 */ "PARTITION",
170806 /* 88 */ "PRECEDING",
170807 /* 89 */ "RANGE",
170808 /* 90 */ "UNBOUNDED",
170809 /* 91 */ "EXCLUDE",
170810 /* 92 */ "GROUPS",
170811 /* 93 */ "OTHERS",
170812 /* 94 */ "TIES",
170813 /* 95 */ "GENERATED",
170814 /* 96 */ "ALWAYS",
170815 /* 97 */ "MATERIALIZED",
170816 /* 98 */ "REINDEX",
170817 /* 99 */ "RENAME",
170818 /* 100 */ "CTIME_KW",
170819 /* 101 */ "ANY",
170820 /* 102 */ "BITAND",
170821 /* 103 */ "BITOR",
170822 /* 104 */ "LSHIFT",
170823 /* 105 */ "RSHIFT",
170824 /* 106 */ "PLUS",
170825 /* 107 */ "MINUS",
170826 /* 108 */ "STAR",
170827 /* 109 */ "SLASH",
170828 /* 110 */ "REM",
170829 /* 111 */ "CONCAT",
170830 /* 112 */ "PTR",
170831 /* 113 */ "COLLATE",
170832 /* 114 */ "BITNOT",
170833 /* 115 */ "ON",
170834 /* 116 */ "INDEXED",
170835 /* 117 */ "STRING",
170836 /* 118 */ "JOIN_KW",
170837 /* 119 */ "CONSTRAINT",
170838 /* 120 */ "DEFAULT",
170839 /* 121 */ "NULL",
170840 /* 122 */ "PRIMARY",
170841 /* 123 */ "UNIQUE",
170842 /* 124 */ "CHECK",
170843 /* 125 */ "REFERENCES",
170844 /* 126 */ "AUTOINCR",
170845 /* 127 */ "INSERT",
170846 /* 128 */ "DELETE",
170847 /* 129 */ "UPDATE",
170848 /* 130 */ "SET",
170849 /* 131 */ "DEFERRABLE",
170850 /* 132 */ "FOREIGN",
170851 /* 133 */ "DROP",
170852 /* 134 */ "UNION",
170853 /* 135 */ "ALL",
170854 /* 136 */ "EXCEPT",
170855 /* 137 */ "INTERSECT",
170856 /* 138 */ "SELECT",
170857 /* 139 */ "VALUES",
170858 /* 140 */ "DISTINCT",
170859 /* 141 */ "DOT",
170860 /* 142 */ "FROM",
170861 /* 143 */ "JOIN",
170862 /* 144 */ "USING",
170863 /* 145 */ "ORDER",
170864 /* 146 */ "GROUP",
170865 /* 147 */ "HAVING",
170866 /* 148 */ "LIMIT",
170867 /* 149 */ "WHERE",
170868 /* 150 */ "RETURNING",
170869 /* 151 */ "INTO",
170870 /* 152 */ "NOTHING",
170871 /* 153 */ "FLOAT",
170872 /* 154 */ "BLOB",
170873 /* 155 */ "INTEGER",
170874 /* 156 */ "VARIABLE",
170875 /* 157 */ "CASE",
170876 /* 158 */ "WHEN",
170877 /* 159 */ "THEN",
170878 /* 160 */ "ELSE",
170879 /* 161 */ "INDEX",
170880 /* 162 */ "ALTER",
170881 /* 163 */ "ADD",
170882 /* 164 */ "WINDOW",
170883 /* 165 */ "OVER",
170884 /* 166 */ "FILTER",
170885 /* 167 */ "COLUMN",
170886 /* 168 */ "AGG_FUNCTION",
170887 /* 169 */ "AGG_COLUMN",
170888 /* 170 */ "TRUEFALSE",
170889 /* 171 */ "ISNOT",
170890 /* 172 */ "FUNCTION",
170891 /* 173 */ "UMINUS",
170892 /* 174 */ "UPLUS",
170893 /* 175 */ "TRUTH",
170894 /* 176 */ "REGISTER",
170895 /* 177 */ "VECTOR",
170896 /* 178 */ "SELECT_COLUMN",
170897 /* 179 */ "IF_NULL_ROW",
170898 /* 180 */ "ASTERISK",
170899 /* 181 */ "SPAN",
170900 /* 182 */ "ERROR",
170901 /* 183 */ "SPACE",
170902 /* 184 */ "ILLEGAL",
170903 /* 185 */ "input",
170904 /* 186 */ "cmdlist",
170905 /* 187 */ "ecmd",
170906 /* 188 */ "cmdx",
170907 /* 189 */ "explain",
170908 /* 190 */ "cmd",
170909 /* 191 */ "transtype",
170910 /* 192 */ "trans_opt",
170911 /* 193 */ "nm",
170912 /* 194 */ "savepoint_opt",
170913 /* 195 */ "create_table",
170914 /* 196 */ "create_table_args",
170915 /* 197 */ "createkw",
170916 /* 198 */ "temp",
170917 /* 199 */ "ifnotexists",
170918 /* 200 */ "dbnm",
170919 /* 201 */ "columnlist",
170920 /* 202 */ "conslist_opt",
170921 /* 203 */ "table_option_set",
170922 /* 204 */ "select",
170923 /* 205 */ "table_option",
170924 /* 206 */ "columnname",
170925 /* 207 */ "carglist",
170926 /* 208 */ "typetoken",
170927 /* 209 */ "typename",
170928 /* 210 */ "signed",
170929 /* 211 */ "plus_num",
170930 /* 212 */ "minus_num",
170931 /* 213 */ "scanpt",
170932 /* 214 */ "scantok",
170933 /* 215 */ "ccons",
170934 /* 216 */ "term",
170935 /* 217 */ "expr",
170936 /* 218 */ "onconf",
170937 /* 219 */ "sortorder",
170938 /* 220 */ "autoinc",
170939 /* 221 */ "eidlist_opt",
170940 /* 222 */ "refargs",
170941 /* 223 */ "defer_subclause",
170942 /* 224 */ "generated",
170943 /* 225 */ "refarg",
170944 /* 226 */ "refact",
170945 /* 227 */ "init_deferred_pred_opt",
170946 /* 228 */ "conslist",
170947 /* 229 */ "tconscomma",
170948 /* 230 */ "tcons",
170949 /* 231 */ "sortlist",
170950 /* 232 */ "eidlist",
170951 /* 233 */ "defer_subclause_opt",
170952 /* 234 */ "orconf",
170953 /* 235 */ "resolvetype",
170954 /* 236 */ "raisetype",
170955 /* 237 */ "ifexists",
170956 /* 238 */ "fullname",
170957 /* 239 */ "selectnowith",
170958 /* 240 */ "oneselect",
170959 /* 241 */ "wqlist",
170960 /* 242 */ "multiselect_op",
170961 /* 243 */ "distinct",
170962 /* 244 */ "selcollist",
170963 /* 245 */ "from",
170964 /* 246 */ "where_opt",
170965 /* 247 */ "groupby_opt",
170966 /* 248 */ "having_opt",
170967 /* 249 */ "orderby_opt",
170968 /* 250 */ "limit_opt",
170969 /* 251 */ "window_clause",
170970 /* 252 */ "values",
170971 /* 253 */ "nexprlist",
170972 /* 254 */ "sclp",
170973 /* 255 */ "as",
170974 /* 256 */ "seltablist",
170975 /* 257 */ "stl_prefix",
170976 /* 258 */ "joinop",
170977 /* 259 */ "on_using",
170978 /* 260 */ "indexed_by",
170979 /* 261 */ "exprlist",
170980 /* 262 */ "xfullname",
170981 /* 263 */ "idlist",
170982 /* 264 */ "indexed_opt",
170983 /* 265 */ "nulls",
170984 /* 266 */ "with",
170985 /* 267 */ "where_opt_ret",
170986 /* 268 */ "setlist",
170987 /* 269 */ "insert_cmd",
170988 /* 270 */ "idlist_opt",
170989 /* 271 */ "upsert",
170990 /* 272 */ "returning",
170991 /* 273 */ "filter_over",
170992 /* 274 */ "likeop",
170993 /* 275 */ "between_op",
170994 /* 276 */ "in_op",
170995 /* 277 */ "paren_exprlist",
170996 /* 278 */ "case_operand",
170997 /* 279 */ "case_exprlist",
170998 /* 280 */ "case_else",
170999 /* 281 */ "uniqueflag",
171000 /* 282 */ "collate",
171001 /* 283 */ "vinto",
171002 /* 284 */ "nmnum",
171003 /* 285 */ "trigger_decl",
171004 /* 286 */ "trigger_cmd_list",
171005 /* 287 */ "trigger_time",
171006 /* 288 */ "trigger_event",
171007 /* 289 */ "foreach_clause",
171008 /* 290 */ "when_clause",
171009 /* 291 */ "trigger_cmd",
171010 /* 292 */ "trnm",
171011 /* 293 */ "tridxby",
171012 /* 294 */ "database_kw_opt",
171013 /* 295 */ "key_opt",
171014 /* 296 */ "add_column_fullname",
171015 /* 297 */ "kwcolumn_opt",
171016 /* 298 */ "create_vtab",
171017 /* 299 */ "vtabarglist",
171018 /* 300 */ "vtabarg",
171019 /* 301 */ "vtabargtoken",
171020 /* 302 */ "lp",
171021 /* 303 */ "anylist",
171022 /* 304 */ "wqitem",
171023 /* 305 */ "wqas",
171024 /* 306 */ "windowdefn_list",
171025 /* 307 */ "windowdefn",
171026 /* 308 */ "window",
171027 /* 309 */ "frame_opt",
171028 /* 310 */ "part_opt",
171029 /* 311 */ "filter_clause",
171030 /* 312 */ "over_clause",
171031 /* 313 */ "range_or_rows",
171032 /* 314 */ "frame_bound",
171033 /* 315 */ "frame_bound_s",
171034 /* 316 */ "frame_bound_e",
171035 /* 317 */ "frame_exclude_opt",
171036 /* 318 */ "frame_exclude",
171037};
171038#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
171039
171040#ifndef NDEBUG
171041/* For tracing reduce actions, the names of all rules are required.
171042*/
171043static const char *const yyRuleName[] = {
171044 /* 0 */ "explain ::= EXPLAIN",
171045 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
171046 /* 2 */ "cmdx ::= cmd",
171047 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
171048 /* 4 */ "transtype ::=",
171049 /* 5 */ "transtype ::= DEFERRED",
171050 /* 6 */ "transtype ::= IMMEDIATE",
171051 /* 7 */ "transtype ::= EXCLUSIVE",
171052 /* 8 */ "cmd ::= COMMIT|END trans_opt",
171053 /* 9 */ "cmd ::= ROLLBACK trans_opt",
171054 /* 10 */ "cmd ::= SAVEPOINT nm",
171055 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
171056 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
171057 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
171058 /* 14 */ "createkw ::= CREATE",
171059 /* 15 */ "ifnotexists ::=",
171060 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
171061 /* 17 */ "temp ::= TEMP",
171062 /* 18 */ "temp ::=",
171063 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_option_set",
171064 /* 20 */ "create_table_args ::= AS select",
171065 /* 21 */ "table_option_set ::=",
171066 /* 22 */ "table_option_set ::= table_option_set COMMA table_option",
171067 /* 23 */ "table_option ::= WITHOUT nm",
171068 /* 24 */ "table_option ::= nm",
171069 /* 25 */ "columnname ::= nm typetoken",
171070 /* 26 */ "typetoken ::=",
171071 /* 27 */ "typetoken ::= typename LP signed RP",
171072 /* 28 */ "typetoken ::= typename LP signed COMMA signed RP",
171073 /* 29 */ "typename ::= typename ID|STRING",
171074 /* 30 */ "scanpt ::=",
171075 /* 31 */ "scantok ::=",
171076 /* 32 */ "ccons ::= CONSTRAINT nm",
171077 /* 33 */ "ccons ::= DEFAULT scantok term",
171078 /* 34 */ "ccons ::= DEFAULT LP expr RP",
171079 /* 35 */ "ccons ::= DEFAULT PLUS scantok term",
171080 /* 36 */ "ccons ::= DEFAULT MINUS scantok term",
171081 /* 37 */ "ccons ::= DEFAULT scantok ID|INDEXED",
171082 /* 38 */ "ccons ::= NOT NULL onconf",
171083 /* 39 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
171084 /* 40 */ "ccons ::= UNIQUE onconf",
171085 /* 41 */ "ccons ::= CHECK LP expr RP",
171086 /* 42 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
171087 /* 43 */ "ccons ::= defer_subclause",
171088 /* 44 */ "ccons ::= COLLATE ID|STRING",
171089 /* 45 */ "generated ::= LP expr RP",
171090 /* 46 */ "generated ::= LP expr RP ID",
171091 /* 47 */ "autoinc ::=",
171092 /* 48 */ "autoinc ::= AUTOINCR",
171093 /* 49 */ "refargs ::=",
171094 /* 50 */ "refargs ::= refargs refarg",
171095 /* 51 */ "refarg ::= MATCH nm",
171096 /* 52 */ "refarg ::= ON INSERT refact",
171097 /* 53 */ "refarg ::= ON DELETE refact",
171098 /* 54 */ "refarg ::= ON UPDATE refact",
171099 /* 55 */ "refact ::= SET NULL",
171100 /* 56 */ "refact ::= SET DEFAULT",
171101 /* 57 */ "refact ::= CASCADE",
171102 /* 58 */ "refact ::= RESTRICT",
171103 /* 59 */ "refact ::= NO ACTION",
171104 /* 60 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
171105 /* 61 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
171106 /* 62 */ "init_deferred_pred_opt ::=",
171107 /* 63 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
171108 /* 64 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
171109 /* 65 */ "conslist_opt ::=",
171110 /* 66 */ "tconscomma ::= COMMA",
171111 /* 67 */ "tcons ::= CONSTRAINT nm",
171112 /* 68 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
171113 /* 69 */ "tcons ::= UNIQUE LP sortlist RP onconf",
171114 /* 70 */ "tcons ::= CHECK LP expr RP onconf",
171115 /* 71 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
171116 /* 72 */ "defer_subclause_opt ::=",
171117 /* 73 */ "onconf ::=",
171118 /* 74 */ "onconf ::= ON CONFLICT resolvetype",
171119 /* 75 */ "orconf ::=",
171120 /* 76 */ "orconf ::= OR resolvetype",
171121 /* 77 */ "resolvetype ::= IGNORE",
171122 /* 78 */ "resolvetype ::= REPLACE",
171123 /* 79 */ "cmd ::= DROP TABLE ifexists fullname",
171124 /* 80 */ "ifexists ::= IF EXISTS",
171125 /* 81 */ "ifexists ::=",
171126 /* 82 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
171127 /* 83 */ "cmd ::= DROP VIEW ifexists fullname",
171128 /* 84 */ "cmd ::= select",
171129 /* 85 */ "select ::= WITH wqlist selectnowith",
171130 /* 86 */ "select ::= WITH RECURSIVE wqlist selectnowith",
171131 /* 87 */ "select ::= selectnowith",
171132 /* 88 */ "selectnowith ::= selectnowith multiselect_op oneselect",
171133 /* 89 */ "multiselect_op ::= UNION",
171134 /* 90 */ "multiselect_op ::= UNION ALL",
171135 /* 91 */ "multiselect_op ::= EXCEPT|INTERSECT",
171136 /* 92 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
171137 /* 93 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
171138 /* 94 */ "values ::= VALUES LP nexprlist RP",
171139 /* 95 */ "values ::= values COMMA LP nexprlist RP",
171140 /* 96 */ "distinct ::= DISTINCT",
171141 /* 97 */ "distinct ::= ALL",
171142 /* 98 */ "distinct ::=",
171143 /* 99 */ "sclp ::=",
171144 /* 100 */ "selcollist ::= sclp scanpt expr scanpt as",
171145 /* 101 */ "selcollist ::= sclp scanpt STAR",
171146 /* 102 */ "selcollist ::= sclp scanpt nm DOT STAR",
171147 /* 103 */ "as ::= AS nm",
171148 /* 104 */ "as ::=",
171149 /* 105 */ "from ::=",
171150 /* 106 */ "from ::= FROM seltablist",
171151 /* 107 */ "stl_prefix ::= seltablist joinop",
171152 /* 108 */ "stl_prefix ::=",
171153 /* 109 */ "seltablist ::= stl_prefix nm dbnm as on_using",
171154 /* 110 */ "seltablist ::= stl_prefix nm dbnm as indexed_by on_using",
171155 /* 111 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using",
171156 /* 112 */ "seltablist ::= stl_prefix LP select RP as on_using",
171157 /* 113 */ "seltablist ::= stl_prefix LP seltablist RP as on_using",
171158 /* 114 */ "dbnm ::=",
171159 /* 115 */ "dbnm ::= DOT nm",
171160 /* 116 */ "fullname ::= nm",
171161 /* 117 */ "fullname ::= nm DOT nm",
171162 /* 118 */ "xfullname ::= nm",
171163 /* 119 */ "xfullname ::= nm DOT nm",
171164 /* 120 */ "xfullname ::= nm DOT nm AS nm",
171165 /* 121 */ "xfullname ::= nm AS nm",
171166 /* 122 */ "joinop ::= COMMA|JOIN",
171167 /* 123 */ "joinop ::= JOIN_KW JOIN",
171168 /* 124 */ "joinop ::= JOIN_KW nm JOIN",
171169 /* 125 */ "joinop ::= JOIN_KW nm nm JOIN",
171170 /* 126 */ "on_using ::= ON expr",
171171 /* 127 */ "on_using ::= USING LP idlist RP",
171172 /* 128 */ "on_using ::=",
171173 /* 129 */ "indexed_opt ::=",
171174 /* 130 */ "indexed_by ::= INDEXED BY nm",
171175 /* 131 */ "indexed_by ::= NOT INDEXED",
171176 /* 132 */ "orderby_opt ::=",
171177 /* 133 */ "orderby_opt ::= ORDER BY sortlist",
171178 /* 134 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
171179 /* 135 */ "sortlist ::= expr sortorder nulls",
171180 /* 136 */ "sortorder ::= ASC",
171181 /* 137 */ "sortorder ::= DESC",
171182 /* 138 */ "sortorder ::=",
171183 /* 139 */ "nulls ::= NULLS FIRST",
171184 /* 140 */ "nulls ::= NULLS LAST",
171185 /* 141 */ "nulls ::=",
171186 /* 142 */ "groupby_opt ::=",
171187 /* 143 */ "groupby_opt ::= GROUP BY nexprlist",
171188 /* 144 */ "having_opt ::=",
171189 /* 145 */ "having_opt ::= HAVING expr",
171190 /* 146 */ "limit_opt ::=",
171191 /* 147 */ "limit_opt ::= LIMIT expr",
171192 /* 148 */ "limit_opt ::= LIMIT expr OFFSET expr",
171193 /* 149 */ "limit_opt ::= LIMIT expr COMMA expr",
171194 /* 150 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret",
171195 /* 151 */ "where_opt ::=",
171196 /* 152 */ "where_opt ::= WHERE expr",
171197 /* 153 */ "where_opt_ret ::=",
171198 /* 154 */ "where_opt_ret ::= WHERE expr",
171199 /* 155 */ "where_opt_ret ::= RETURNING selcollist",
171200 /* 156 */ "where_opt_ret ::= WHERE expr RETURNING selcollist",
171201 /* 157 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret",
171202 /* 158 */ "setlist ::= setlist COMMA nm EQ expr",
171203 /* 159 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
171204 /* 160 */ "setlist ::= nm EQ expr",
171205 /* 161 */ "setlist ::= LP idlist RP EQ expr",
171206 /* 162 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
171207 /* 163 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning",
171208 /* 164 */ "upsert ::=",
171209 /* 165 */ "upsert ::= RETURNING selcollist",
171210 /* 166 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert",
171211 /* 167 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert",
171212 /* 168 */ "upsert ::= ON CONFLICT DO NOTHING returning",
171213 /* 169 */ "upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning",
171214 /* 170 */ "returning ::= RETURNING selcollist",
171215 /* 171 */ "insert_cmd ::= INSERT orconf",
171216 /* 172 */ "insert_cmd ::= REPLACE",
171217 /* 173 */ "idlist_opt ::=",
171218 /* 174 */ "idlist_opt ::= LP idlist RP",
171219 /* 175 */ "idlist ::= idlist COMMA nm",
171220 /* 176 */ "idlist ::= nm",
171221 /* 177 */ "expr ::= LP expr RP",
171222 /* 178 */ "expr ::= ID|INDEXED|JOIN_KW",
171223 /* 179 */ "expr ::= nm DOT nm",
171224 /* 180 */ "expr ::= nm DOT nm DOT nm",
171225 /* 181 */ "term ::= NULL|FLOAT|BLOB",
171226 /* 182 */ "term ::= STRING",
171227 /* 183 */ "term ::= INTEGER",
171228 /* 184 */ "expr ::= VARIABLE",
171229 /* 185 */ "expr ::= expr COLLATE ID|STRING",
171230 /* 186 */ "expr ::= CAST LP expr AS typetoken RP",
171231 /* 187 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP",
171232 /* 188 */ "expr ::= ID|INDEXED|JOIN_KW LP STAR RP",
171233 /* 189 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over",
171234 /* 190 */ "expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over",
171235 /* 191 */ "term ::= CTIME_KW",
171236 /* 192 */ "expr ::= LP nexprlist COMMA expr RP",
171237 /* 193 */ "expr ::= expr AND expr",
171238 /* 194 */ "expr ::= expr OR expr",
171239 /* 195 */ "expr ::= expr LT|GT|GE|LE expr",
171240 /* 196 */ "expr ::= expr EQ|NE expr",
171241 /* 197 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
171242 /* 198 */ "expr ::= expr PLUS|MINUS expr",
171243 /* 199 */ "expr ::= expr STAR|SLASH|REM expr",
171244 /* 200 */ "expr ::= expr CONCAT expr",
171245 /* 201 */ "likeop ::= NOT LIKE_KW|MATCH",
171246 /* 202 */ "expr ::= expr likeop expr",
171247 /* 203 */ "expr ::= expr likeop expr ESCAPE expr",
171248 /* 204 */ "expr ::= expr ISNULL|NOTNULL",
171249 /* 205 */ "expr ::= expr NOT NULL",
171250 /* 206 */ "expr ::= expr IS expr",
171251 /* 207 */ "expr ::= expr IS NOT expr",
171252 /* 208 */ "expr ::= expr IS NOT DISTINCT FROM expr",
171253 /* 209 */ "expr ::= expr IS DISTINCT FROM expr",
171254 /* 210 */ "expr ::= NOT expr",
171255 /* 211 */ "expr ::= BITNOT expr",
171256 /* 212 */ "expr ::= PLUS|MINUS expr",
171257 /* 213 */ "expr ::= expr PTR expr",
171258 /* 214 */ "between_op ::= BETWEEN",
171259 /* 215 */ "between_op ::= NOT BETWEEN",
171260 /* 216 */ "expr ::= expr between_op expr AND expr",
171261 /* 217 */ "in_op ::= IN",
171262 /* 218 */ "in_op ::= NOT IN",
171263 /* 219 */ "expr ::= expr in_op LP exprlist RP",
171264 /* 220 */ "expr ::= LP select RP",
171265 /* 221 */ "expr ::= expr in_op LP select RP",
171266 /* 222 */ "expr ::= expr in_op nm dbnm paren_exprlist",
171267 /* 223 */ "expr ::= EXISTS LP select RP",
171268 /* 224 */ "expr ::= CASE case_operand case_exprlist case_else END",
171269 /* 225 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
171270 /* 226 */ "case_exprlist ::= WHEN expr THEN expr",
171271 /* 227 */ "case_else ::= ELSE expr",
171272 /* 228 */ "case_else ::=",
171273 /* 229 */ "case_operand ::=",
171274 /* 230 */ "exprlist ::=",
171275 /* 231 */ "nexprlist ::= nexprlist COMMA expr",
171276 /* 232 */ "nexprlist ::= expr",
171277 /* 233 */ "paren_exprlist ::=",
171278 /* 234 */ "paren_exprlist ::= LP exprlist RP",
171279 /* 235 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
171280 /* 236 */ "uniqueflag ::= UNIQUE",
171281 /* 237 */ "uniqueflag ::=",
171282 /* 238 */ "eidlist_opt ::=",
171283 /* 239 */ "eidlist_opt ::= LP eidlist RP",
171284 /* 240 */ "eidlist ::= eidlist COMMA nm collate sortorder",
171285 /* 241 */ "eidlist ::= nm collate sortorder",
171286 /* 242 */ "collate ::=",
171287 /* 243 */ "collate ::= COLLATE ID|STRING",
171288 /* 244 */ "cmd ::= DROP INDEX ifexists fullname",
171289 /* 245 */ "cmd ::= VACUUM vinto",
171290 /* 246 */ "cmd ::= VACUUM nm vinto",
171291 /* 247 */ "vinto ::= INTO expr",
171292 /* 248 */ "vinto ::=",
171293 /* 249 */ "cmd ::= PRAGMA nm dbnm",
171294 /* 250 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
171295 /* 251 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
171296 /* 252 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
171297 /* 253 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
171298 /* 254 */ "plus_num ::= PLUS INTEGER|FLOAT",
171299 /* 255 */ "minus_num ::= MINUS INTEGER|FLOAT",
171300 /* 256 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
171301 /* 257 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
171302 /* 258 */ "trigger_time ::= BEFORE|AFTER",
171303 /* 259 */ "trigger_time ::= INSTEAD OF",
171304 /* 260 */ "trigger_time ::=",
171305 /* 261 */ "trigger_event ::= DELETE|INSERT",
171306 /* 262 */ "trigger_event ::= UPDATE",
171307 /* 263 */ "trigger_event ::= UPDATE OF idlist",
171308 /* 264 */ "when_clause ::=",
171309 /* 265 */ "when_clause ::= WHEN expr",
171310 /* 266 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
171311 /* 267 */ "trigger_cmd_list ::= trigger_cmd SEMI",
171312 /* 268 */ "trnm ::= nm DOT nm",
171313 /* 269 */ "tridxby ::= INDEXED BY nm",
171314 /* 270 */ "tridxby ::= NOT INDEXED",
171315 /* 271 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt",
171316 /* 272 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
171317 /* 273 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
171318 /* 274 */ "trigger_cmd ::= scanpt select scanpt",
171319 /* 275 */ "expr ::= RAISE LP IGNORE RP",
171320 /* 276 */ "expr ::= RAISE LP raisetype COMMA nm RP",
171321 /* 277 */ "raisetype ::= ROLLBACK",
171322 /* 278 */ "raisetype ::= ABORT",
171323 /* 279 */ "raisetype ::= FAIL",
171324 /* 280 */ "cmd ::= DROP TRIGGER ifexists fullname",
171325 /* 281 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
171326 /* 282 */ "cmd ::= DETACH database_kw_opt expr",
171327 /* 283 */ "key_opt ::=",
171328 /* 284 */ "key_opt ::= KEY expr",
171329 /* 285 */ "cmd ::= REINDEX",
171330 /* 286 */ "cmd ::= REINDEX nm dbnm",
171331 /* 287 */ "cmd ::= ANALYZE",
171332 /* 288 */ "cmd ::= ANALYZE nm dbnm",
171333 /* 289 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
171334 /* 290 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
171335 /* 291 */ "cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm",
171336 /* 292 */ "add_column_fullname ::= fullname",
171337 /* 293 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
171338 /* 294 */ "cmd ::= create_vtab",
171339 /* 295 */ "cmd ::= create_vtab LP vtabarglist RP",
171340 /* 296 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
171341 /* 297 */ "vtabarg ::=",
171342 /* 298 */ "vtabargtoken ::= ANY",
171343 /* 299 */ "vtabargtoken ::= lp anylist RP",
171344 /* 300 */ "lp ::= LP",
171345 /* 301 */ "with ::= WITH wqlist",
171346 /* 302 */ "with ::= WITH RECURSIVE wqlist",
171347 /* 303 */ "wqas ::= AS",
171348 /* 304 */ "wqas ::= AS MATERIALIZED",
171349 /* 305 */ "wqas ::= AS NOT MATERIALIZED",
171350 /* 306 */ "wqitem ::= nm eidlist_opt wqas LP select RP",
171351 /* 307 */ "wqlist ::= wqitem",
171352 /* 308 */ "wqlist ::= wqlist COMMA wqitem",
171353 /* 309 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
171354 /* 310 */ "windowdefn ::= nm AS LP window RP",
171355 /* 311 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
171356 /* 312 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
171357 /* 313 */ "window ::= ORDER BY sortlist frame_opt",
171358 /* 314 */ "window ::= nm ORDER BY sortlist frame_opt",
171359 /* 315 */ "window ::= nm frame_opt",
171360 /* 316 */ "frame_opt ::=",
171361 /* 317 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
171362 /* 318 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
171363 /* 319 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
171364 /* 320 */ "frame_bound_s ::= frame_bound",
171365 /* 321 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
171366 /* 322 */ "frame_bound_e ::= frame_bound",
171367 /* 323 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
171368 /* 324 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
171369 /* 325 */ "frame_bound ::= CURRENT ROW",
171370 /* 326 */ "frame_exclude_opt ::=",
171371 /* 327 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
171372 /* 328 */ "frame_exclude ::= NO OTHERS",
171373 /* 329 */ "frame_exclude ::= CURRENT ROW",
171374 /* 330 */ "frame_exclude ::= GROUP|TIES",
171375 /* 331 */ "window_clause ::= WINDOW windowdefn_list",
171376 /* 332 */ "filter_over ::= filter_clause over_clause",
171377 /* 333 */ "filter_over ::= over_clause",
171378 /* 334 */ "filter_over ::= filter_clause",
171379 /* 335 */ "over_clause ::= OVER LP window RP",
171380 /* 336 */ "over_clause ::= OVER nm",
171381 /* 337 */ "filter_clause ::= FILTER LP WHERE expr RP",
171382 /* 338 */ "input ::= cmdlist",
171383 /* 339 */ "cmdlist ::= cmdlist ecmd",
171384 /* 340 */ "cmdlist ::= ecmd",
171385 /* 341 */ "ecmd ::= SEMI",
171386 /* 342 */ "ecmd ::= cmdx SEMI",
171387 /* 343 */ "ecmd ::= explain cmdx SEMI",
171388 /* 344 */ "trans_opt ::=",
171389 /* 345 */ "trans_opt ::= TRANSACTION",
171390 /* 346 */ "trans_opt ::= TRANSACTION nm",
171391 /* 347 */ "savepoint_opt ::= SAVEPOINT",
171392 /* 348 */ "savepoint_opt ::=",
171393 /* 349 */ "cmd ::= create_table create_table_args",
171394 /* 350 */ "table_option_set ::= table_option",
171395 /* 351 */ "columnlist ::= columnlist COMMA columnname carglist",
171396 /* 352 */ "columnlist ::= columnname carglist",
171397 /* 353 */ "nm ::= ID|INDEXED|JOIN_KW",
171398 /* 354 */ "nm ::= STRING",
171399 /* 355 */ "typetoken ::= typename",
171400 /* 356 */ "typename ::= ID|STRING",
171401 /* 357 */ "signed ::= plus_num",
171402 /* 358 */ "signed ::= minus_num",
171403 /* 359 */ "carglist ::= carglist ccons",
171404 /* 360 */ "carglist ::=",
171405 /* 361 */ "ccons ::= NULL onconf",
171406 /* 362 */ "ccons ::= GENERATED ALWAYS AS generated",
171407 /* 363 */ "ccons ::= AS generated",
171408 /* 364 */ "conslist_opt ::= COMMA conslist",
171409 /* 365 */ "conslist ::= conslist tconscomma tcons",
171410 /* 366 */ "conslist ::= tcons",
171411 /* 367 */ "tconscomma ::=",
171412 /* 368 */ "defer_subclause_opt ::= defer_subclause",
171413 /* 369 */ "resolvetype ::= raisetype",
171414 /* 370 */ "selectnowith ::= oneselect",
171415 /* 371 */ "oneselect ::= values",
171416 /* 372 */ "sclp ::= selcollist COMMA",
171417 /* 373 */ "as ::= ID|STRING",
171418 /* 374 */ "indexed_opt ::= indexed_by",
171419 /* 375 */ "returning ::=",
171420 /* 376 */ "expr ::= term",
171421 /* 377 */ "likeop ::= LIKE_KW|MATCH",
171422 /* 378 */ "case_operand ::= expr",
171423 /* 379 */ "exprlist ::= nexprlist",
171424 /* 380 */ "nmnum ::= plus_num",
171425 /* 381 */ "nmnum ::= nm",
171426 /* 382 */ "nmnum ::= ON",
171427 /* 383 */ "nmnum ::= DELETE",
171428 /* 384 */ "nmnum ::= DEFAULT",
171429 /* 385 */ "plus_num ::= INTEGER|FLOAT",
171430 /* 386 */ "foreach_clause ::=",
171431 /* 387 */ "foreach_clause ::= FOR EACH ROW",
171432 /* 388 */ "trnm ::= nm",
171433 /* 389 */ "tridxby ::=",
171434 /* 390 */ "database_kw_opt ::= DATABASE",
171435 /* 391 */ "database_kw_opt ::=",
171436 /* 392 */ "kwcolumn_opt ::=",
171437 /* 393 */ "kwcolumn_opt ::= COLUMNKW",
171438 /* 394 */ "vtabarglist ::= vtabarg",
171439 /* 395 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
171440 /* 396 */ "vtabarg ::= vtabarg vtabargtoken",
171441 /* 397 */ "anylist ::=",
171442 /* 398 */ "anylist ::= anylist LP anylist RP",
171443 /* 399 */ "anylist ::= anylist ANY",
171444 /* 400 */ "with ::=",
171445 /* 401 */ "windowdefn_list ::= windowdefn",
171446 /* 402 */ "window ::= frame_opt",
171447};
171448#endif /* NDEBUG */
171449
171450
171451#if YYSTACKDEPTH<=0
171452/*
171453** Try to increase the size of the parser stack. Return the number
171454** of errors. Return 0 on success.
171455*/
171456static int yyGrowStack(yyParser *p){
171457 int newSize;
171458 int idx;
171459 yyStackEntry *pNew;
171460
171461 newSize = p->yystksz*2 + 100;
171462 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
171463 if( p->yystack==&p->yystk0 ){
171464 pNew = malloc(newSize*sizeof(pNew[0]));
171465 if( pNew ) pNew[0] = p->yystk0;
171466 }else{
171467 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
171468 }
171469 if( pNew ){
171470 p->yystack = pNew;
171471 p->yytos = &p->yystack[idx];
171472#ifndef NDEBUG
171473 if( yyTraceFILE ){
171474 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
171475 yyTracePrompt, p->yystksz, newSize);
171476 }
171477#endif
171478 p->yystksz = newSize;
171479 }
171480 return pNew==0;
171481}
171482#endif
171483
171484/* Datatype of the argument to the memory allocated passed as the
171485** second argument to sqlite3ParserAlloc() below. This can be changed by
171486** putting an appropriate #define in the %include section of the input
171487** grammar.
171488*/
171489#ifndef YYMALLOCARGTYPE
171490# define YYMALLOCARGTYPE size_t
171491#endif
171492
171493/* Initialize a new parser that has already been allocated.
171494*/
171495SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
171496 yyParser *yypParser = (yyParser*)yypRawParser;
171497 sqlite3ParserCTX_STORE
171498#ifdef YYTRACKMAXSTACKDEPTH
171499 yypParser->yyhwm = 0;
171500#endif
171501#if YYSTACKDEPTH<=0
171502 yypParser->yytos = NULL;
171503 yypParser->yystack = NULL;
171504 yypParser->yystksz = 0;
171505 if( yyGrowStack(yypParser) ){
171506 yypParser->yystack = &yypParser->yystk0;
171507 yypParser->yystksz = 1;
171508 }
171509#endif
171510#ifndef YYNOERRORRECOVERY
171511 yypParser->yyerrcnt = -1;
171512#endif
171513 yypParser->yytos = yypParser->yystack;
171514 yypParser->yystack[0].stateno = 0;
171515 yypParser->yystack[0].major = 0;
171516#if YYSTACKDEPTH>0
171517 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
171518#endif
171519}
171520
171521#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
171522/*
171523** This function allocates a new parser.
171524** The only argument is a pointer to a function which works like
171525** malloc.
171526**
171527** Inputs:
171528** A pointer to the function used to allocate memory.
171529**
171530** Outputs:
171531** A pointer to a parser. This pointer is used in subsequent calls
171532** to sqlite3Parser and sqlite3ParserFree.
171533*/
171534SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
171535 yyParser *yypParser;
171536 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
171537 if( yypParser ){
171538 sqlite3ParserCTX_STORE
171539 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
171540 }
171541 return (void*)yypParser;
171542}
171543#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
171544
171545
171546/* The following function deletes the "minor type" or semantic value
171547** associated with a symbol. The symbol can be either a terminal
171548** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
171549** a pointer to the value to be deleted. The code used to do the
171550** deletions is derived from the %destructor and/or %token_destructor
171551** directives of the input grammar.
171552*/
171553static void yy_destructor(
171554 yyParser *yypParser, /* The parser */
171555 YYCODETYPE yymajor, /* Type code for object to destroy */
171556 YYMINORTYPE *yypminor /* The object to be destroyed */
171557){
171558 sqlite3ParserARG_FETCH
171559 sqlite3ParserCTX_FETCH
171560 switch( yymajor ){
171561 /* Here is inserted the actions which take place when a
171562 ** terminal or non-terminal is destroyed. This can happen
171563 ** when the symbol is popped from the stack during a
171564 ** reduce or during error processing or when a parser is
171565 ** being destroyed before it is finished parsing.
171566 **
171567 ** Note: during a reduce, the only symbols destroyed are those
171568 ** which appear on the RHS of the rule, but which are *not* used
171569 ** inside the C code.
171570 */
171571/********* Begin destructor definitions ***************************************/
171572 case 204: /* select */
171573 case 239: /* selectnowith */
171574 case 240: /* oneselect */
171575 case 252: /* values */
171576{
171577sqlite3SelectDelete(db: pParse->db, p: (yypminor->yy47));
171578}
171579 break;
171580 case 216: /* term */
171581 case 217: /* expr */
171582 case 246: /* where_opt */
171583 case 248: /* having_opt */
171584 case 267: /* where_opt_ret */
171585 case 278: /* case_operand */
171586 case 280: /* case_else */
171587 case 283: /* vinto */
171588 case 290: /* when_clause */
171589 case 295: /* key_opt */
171590 case 311: /* filter_clause */
171591{
171592sqlite3ExprDelete(db: pParse->db, p: (yypminor->yy528));
171593}
171594 break;
171595 case 221: /* eidlist_opt */
171596 case 231: /* sortlist */
171597 case 232: /* eidlist */
171598 case 244: /* selcollist */
171599 case 247: /* groupby_opt */
171600 case 249: /* orderby_opt */
171601 case 253: /* nexprlist */
171602 case 254: /* sclp */
171603 case 261: /* exprlist */
171604 case 268: /* setlist */
171605 case 277: /* paren_exprlist */
171606 case 279: /* case_exprlist */
171607 case 310: /* part_opt */
171608{
171609sqlite3ExprListDelete(db: pParse->db, pList: (yypminor->yy322));
171610}
171611 break;
171612 case 238: /* fullname */
171613 case 245: /* from */
171614 case 256: /* seltablist */
171615 case 257: /* stl_prefix */
171616 case 262: /* xfullname */
171617{
171618sqlite3SrcListDelete(db: pParse->db, pList: (yypminor->yy131));
171619}
171620 break;
171621 case 241: /* wqlist */
171622{
171623sqlite3WithDelete(db: pParse->db, pWith: (yypminor->yy521));
171624}
171625 break;
171626 case 251: /* window_clause */
171627 case 306: /* windowdefn_list */
171628{
171629sqlite3WindowListDelete(db: pParse->db, p: (yypminor->yy41));
171630}
171631 break;
171632 case 263: /* idlist */
171633 case 270: /* idlist_opt */
171634{
171635sqlite3IdListDelete(db: pParse->db, pList: (yypminor->yy254));
171636}
171637 break;
171638 case 273: /* filter_over */
171639 case 307: /* windowdefn */
171640 case 308: /* window */
171641 case 309: /* frame_opt */
171642 case 312: /* over_clause */
171643{
171644sqlite3WindowDelete(db: pParse->db, p: (yypminor->yy41));
171645}
171646 break;
171647 case 286: /* trigger_cmd_list */
171648 case 291: /* trigger_cmd */
171649{
171650sqlite3DeleteTriggerStep(db: pParse->db, pTriggerStep: (yypminor->yy33));
171651}
171652 break;
171653 case 288: /* trigger_event */
171654{
171655sqlite3IdListDelete(db: pParse->db, pList: (yypminor->yy180).b);
171656}
171657 break;
171658 case 314: /* frame_bound */
171659 case 315: /* frame_bound_s */
171660 case 316: /* frame_bound_e */
171661{
171662sqlite3ExprDelete(db: pParse->db, p: (yypminor->yy595).pExpr);
171663}
171664 break;
171665/********* End destructor definitions *****************************************/
171666 default: break; /* If no destructor action specified: do nothing */
171667 }
171668}
171669
171670/*
171671** Pop the parser's stack once.
171672**
171673** If there is a destructor routine associated with the token which
171674** is popped from the stack, then call it.
171675*/
171676static void yy_pop_parser_stack(yyParser *pParser){
171677 yyStackEntry *yytos;
171678 assert( pParser->yytos!=0 );
171679 assert( pParser->yytos > pParser->yystack );
171680 yytos = pParser->yytos--;
171681#ifndef NDEBUG
171682 if( yyTraceFILE ){
171683 fprintf(yyTraceFILE,"%sPopping %s\n",
171684 yyTracePrompt,
171685 yyTokenName[yytos->major]);
171686 }
171687#endif
171688 yy_destructor(yypParser: pParser, yymajor: yytos->major, yypminor: &yytos->minor);
171689}
171690
171691/*
171692** Clear all secondary memory allocations from the parser
171693*/
171694SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
171695 yyParser *pParser = (yyParser*)p;
171696 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
171697#if YYSTACKDEPTH<=0
171698 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
171699#endif
171700}
171701
171702#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
171703/*
171704** Deallocate and destroy a parser. Destructors are called for
171705** all stack elements before shutting the parser down.
171706**
171707** If the YYPARSEFREENEVERNULL macro exists (for example because it
171708** is defined in a %include section of the input grammar) then it is
171709** assumed that the input pointer is never NULL.
171710*/
171711SQLITE_PRIVATE void sqlite3ParserFree(
171712 void *p, /* The parser to be deleted */
171713 void (*freeProc)(void*) /* Function used to reclaim memory */
171714){
171715#ifndef YYPARSEFREENEVERNULL
171716 if( p==0 ) return;
171717#endif
171718 sqlite3ParserFinalize(p);
171719 (*freeProc)(p);
171720}
171721#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
171722
171723/*
171724** Return the peak depth of the stack for a parser.
171725*/
171726#ifdef YYTRACKMAXSTACKDEPTH
171727SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
171728 yyParser *pParser = (yyParser*)p;
171729 return pParser->yyhwm;
171730}
171731#endif
171732
171733/* This array of booleans keeps track of the parser statement
171734** coverage. The element yycoverage[X][Y] is set when the parser
171735** is in state X and has a lookahead token Y. In a well-tested
171736** systems, every element of this matrix should end up being set.
171737*/
171738#if defined(YYCOVERAGE)
171739static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
171740#endif
171741
171742/*
171743** Write into out a description of every state/lookahead combination that
171744**
171745** (1) has not been used by the parser, and
171746** (2) is not a syntax error.
171747**
171748** Return the number of missed state/lookahead combinations.
171749*/
171750#if defined(YYCOVERAGE)
171751SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
171752 int stateno, iLookAhead, i;
171753 int nMissed = 0;
171754 for(stateno=0; stateno<YYNSTATE; stateno++){
171755 i = yy_shift_ofst[stateno];
171756 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
171757 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
171758 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
171759 if( out ){
171760 fprintf(out,"State %d lookahead %s %s\n", stateno,
171761 yyTokenName[iLookAhead],
171762 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
171763 }
171764 }
171765 }
171766 return nMissed;
171767}
171768#endif
171769
171770/*
171771** Find the appropriate action for a parser given the terminal
171772** look-ahead token iLookAhead.
171773*/
171774static YYACTIONTYPE yy_find_shift_action(
171775 YYCODETYPE iLookAhead, /* The look-ahead token */
171776 YYACTIONTYPE stateno /* Current state number */
171777){
171778 int i;
171779
171780 if( stateno>YY_MAX_SHIFT ) return stateno;
171781 assert( stateno <= YY_SHIFT_COUNT );
171782#if defined(YYCOVERAGE)
171783 yycoverage[stateno][iLookAhead] = 1;
171784#endif
171785 do{
171786 i = yy_shift_ofst[stateno];
171787 assert( i>=0 );
171788 assert( i<=YY_ACTTAB_COUNT );
171789 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
171790 assert( iLookAhead!=YYNOCODE );
171791 assert( iLookAhead < YYNTOKEN );
171792 i += iLookAhead;
171793 assert( i<(int)YY_NLOOKAHEAD );
171794 if( yy_lookahead[i]!=iLookAhead ){
171795#ifdef YYFALLBACK
171796 YYCODETYPE iFallback; /* Fallback token */
171797 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
171798 iFallback = yyFallback[iLookAhead];
171799 if( iFallback!=0 ){
171800#ifndef NDEBUG
171801 if( yyTraceFILE ){
171802 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
171803 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
171804 }
171805#endif
171806 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
171807 iLookAhead = iFallback;
171808 continue;
171809 }
171810#endif
171811#ifdef YYWILDCARD
171812 {
171813 int j = i - iLookAhead + YYWILDCARD;
171814 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
171815 if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
171816#ifndef NDEBUG
171817 if( yyTraceFILE ){
171818 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
171819 yyTracePrompt, yyTokenName[iLookAhead],
171820 yyTokenName[YYWILDCARD]);
171821 }
171822#endif /* NDEBUG */
171823 return yy_action[j];
171824 }
171825 }
171826#endif /* YYWILDCARD */
171827 return yy_default[stateno];
171828 }else{
171829 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) );
171830 return yy_action[i];
171831 }
171832 }while(1);
171833}
171834
171835/*
171836** Find the appropriate action for a parser given the non-terminal
171837** look-ahead token iLookAhead.
171838*/
171839static YYACTIONTYPE yy_find_reduce_action(
171840 YYACTIONTYPE stateno, /* Current state number */
171841 YYCODETYPE iLookAhead /* The look-ahead token */
171842){
171843 int i;
171844#ifdef YYERRORSYMBOL
171845 if( stateno>YY_REDUCE_COUNT ){
171846 return yy_default[stateno];
171847 }
171848#else
171849 assert( stateno<=YY_REDUCE_COUNT );
171850#endif
171851 i = yy_reduce_ofst[stateno];
171852 assert( iLookAhead!=YYNOCODE );
171853 i += iLookAhead;
171854#ifdef YYERRORSYMBOL
171855 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
171856 return yy_default[stateno];
171857 }
171858#else
171859 assert( i>=0 && i<YY_ACTTAB_COUNT );
171860 assert( yy_lookahead[i]==iLookAhead );
171861#endif
171862 return yy_action[i];
171863}
171864
171865/*
171866** The following routine is called if the stack overflows.
171867*/
171868static void yyStackOverflow(yyParser *yypParser){
171869 sqlite3ParserARG_FETCH
171870 sqlite3ParserCTX_FETCH
171871#ifndef NDEBUG
171872 if( yyTraceFILE ){
171873 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
171874 }
171875#endif
171876 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(pParser: yypParser);
171877 /* Here code is inserted which will execute if the parser
171878 ** stack every overflows */
171879/******** Begin %stack_overflow code ******************************************/
171880
171881 sqlite3ErrorMsg(pParse, zFormat: "parser stack overflow");
171882/******** End %stack_overflow code ********************************************/
171883 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
171884 sqlite3ParserCTX_STORE
171885}
171886
171887/*
171888** Print tracing information for a SHIFT action
171889*/
171890#ifndef NDEBUG
171891static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
171892 if( yyTraceFILE ){
171893 if( yyNewState<YYNSTATE ){
171894 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
171895 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
171896 yyNewState);
171897 }else{
171898 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
171899 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
171900 yyNewState - YY_MIN_REDUCE);
171901 }
171902 }
171903}
171904#else
171905# define yyTraceShift(X,Y,Z)
171906#endif
171907
171908/*
171909** Perform a shift action.
171910*/
171911static void yy_shift(
171912 yyParser *yypParser, /* The parser to be shifted */
171913 YYACTIONTYPE yyNewState, /* The new state to shift in */
171914 YYCODETYPE yyMajor, /* The major token to shift in */
171915 sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */
171916){
171917 yyStackEntry *yytos;
171918 yypParser->yytos++;
171919#ifdef YYTRACKMAXSTACKDEPTH
171920 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
171921 yypParser->yyhwm++;
171922 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
171923 }
171924#endif
171925#if YYSTACKDEPTH>0
171926 if( yypParser->yytos>yypParser->yystackEnd ){
171927 yypParser->yytos--;
171928 yyStackOverflow(yypParser);
171929 return;
171930 }
171931#else
171932 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
171933 if( yyGrowStack(yypParser) ){
171934 yypParser->yytos--;
171935 yyStackOverflow(yypParser);
171936 return;
171937 }
171938 }
171939#endif
171940 if( yyNewState > YY_MAX_SHIFT ){
171941 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
171942 }
171943 yytos = yypParser->yytos;
171944 yytos->stateno = yyNewState;
171945 yytos->major = yyMajor;
171946 yytos->minor.yy0 = yyMinor;
171947 yyTraceShift(yypParser, yyNewState, "Shift");
171948}
171949
171950/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
171951** of that rule */
171952static const YYCODETYPE yyRuleInfoLhs[] = {
171953 189, /* (0) explain ::= EXPLAIN */
171954 189, /* (1) explain ::= EXPLAIN QUERY PLAN */
171955 188, /* (2) cmdx ::= cmd */
171956 190, /* (3) cmd ::= BEGIN transtype trans_opt */
171957 191, /* (4) transtype ::= */
171958 191, /* (5) transtype ::= DEFERRED */
171959 191, /* (6) transtype ::= IMMEDIATE */
171960 191, /* (7) transtype ::= EXCLUSIVE */
171961 190, /* (8) cmd ::= COMMIT|END trans_opt */
171962 190, /* (9) cmd ::= ROLLBACK trans_opt */
171963 190, /* (10) cmd ::= SAVEPOINT nm */
171964 190, /* (11) cmd ::= RELEASE savepoint_opt nm */
171965 190, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
171966 195, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
171967 197, /* (14) createkw ::= CREATE */
171968 199, /* (15) ifnotexists ::= */
171969 199, /* (16) ifnotexists ::= IF NOT EXISTS */
171970 198, /* (17) temp ::= TEMP */
171971 198, /* (18) temp ::= */
171972 196, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
171973 196, /* (20) create_table_args ::= AS select */
171974 203, /* (21) table_option_set ::= */
171975 203, /* (22) table_option_set ::= table_option_set COMMA table_option */
171976 205, /* (23) table_option ::= WITHOUT nm */
171977 205, /* (24) table_option ::= nm */
171978 206, /* (25) columnname ::= nm typetoken */
171979 208, /* (26) typetoken ::= */
171980 208, /* (27) typetoken ::= typename LP signed RP */
171981 208, /* (28) typetoken ::= typename LP signed COMMA signed RP */
171982 209, /* (29) typename ::= typename ID|STRING */
171983 213, /* (30) scanpt ::= */
171984 214, /* (31) scantok ::= */
171985 215, /* (32) ccons ::= CONSTRAINT nm */
171986 215, /* (33) ccons ::= DEFAULT scantok term */
171987 215, /* (34) ccons ::= DEFAULT LP expr RP */
171988 215, /* (35) ccons ::= DEFAULT PLUS scantok term */
171989 215, /* (36) ccons ::= DEFAULT MINUS scantok term */
171990 215, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
171991 215, /* (38) ccons ::= NOT NULL onconf */
171992 215, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
171993 215, /* (40) ccons ::= UNIQUE onconf */
171994 215, /* (41) ccons ::= CHECK LP expr RP */
171995 215, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
171996 215, /* (43) ccons ::= defer_subclause */
171997 215, /* (44) ccons ::= COLLATE ID|STRING */
171998 224, /* (45) generated ::= LP expr RP */
171999 224, /* (46) generated ::= LP expr RP ID */
172000 220, /* (47) autoinc ::= */
172001 220, /* (48) autoinc ::= AUTOINCR */
172002 222, /* (49) refargs ::= */
172003 222, /* (50) refargs ::= refargs refarg */
172004 225, /* (51) refarg ::= MATCH nm */
172005 225, /* (52) refarg ::= ON INSERT refact */
172006 225, /* (53) refarg ::= ON DELETE refact */
172007 225, /* (54) refarg ::= ON UPDATE refact */
172008 226, /* (55) refact ::= SET NULL */
172009 226, /* (56) refact ::= SET DEFAULT */
172010 226, /* (57) refact ::= CASCADE */
172011 226, /* (58) refact ::= RESTRICT */
172012 226, /* (59) refact ::= NO ACTION */
172013 223, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
172014 223, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
172015 227, /* (62) init_deferred_pred_opt ::= */
172016 227, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
172017 227, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
172018 202, /* (65) conslist_opt ::= */
172019 229, /* (66) tconscomma ::= COMMA */
172020 230, /* (67) tcons ::= CONSTRAINT nm */
172021 230, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
172022 230, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
172023 230, /* (70) tcons ::= CHECK LP expr RP onconf */
172024 230, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
172025 233, /* (72) defer_subclause_opt ::= */
172026 218, /* (73) onconf ::= */
172027 218, /* (74) onconf ::= ON CONFLICT resolvetype */
172028 234, /* (75) orconf ::= */
172029 234, /* (76) orconf ::= OR resolvetype */
172030 235, /* (77) resolvetype ::= IGNORE */
172031 235, /* (78) resolvetype ::= REPLACE */
172032 190, /* (79) cmd ::= DROP TABLE ifexists fullname */
172033 237, /* (80) ifexists ::= IF EXISTS */
172034 237, /* (81) ifexists ::= */
172035 190, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
172036 190, /* (83) cmd ::= DROP VIEW ifexists fullname */
172037 190, /* (84) cmd ::= select */
172038 204, /* (85) select ::= WITH wqlist selectnowith */
172039 204, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
172040 204, /* (87) select ::= selectnowith */
172041 239, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
172042 242, /* (89) multiselect_op ::= UNION */
172043 242, /* (90) multiselect_op ::= UNION ALL */
172044 242, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
172045 240, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
172046 240, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
172047 252, /* (94) values ::= VALUES LP nexprlist RP */
172048 252, /* (95) values ::= values COMMA LP nexprlist RP */
172049 243, /* (96) distinct ::= DISTINCT */
172050 243, /* (97) distinct ::= ALL */
172051 243, /* (98) distinct ::= */
172052 254, /* (99) sclp ::= */
172053 244, /* (100) selcollist ::= sclp scanpt expr scanpt as */
172054 244, /* (101) selcollist ::= sclp scanpt STAR */
172055 244, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
172056 255, /* (103) as ::= AS nm */
172057 255, /* (104) as ::= */
172058 245, /* (105) from ::= */
172059 245, /* (106) from ::= FROM seltablist */
172060 257, /* (107) stl_prefix ::= seltablist joinop */
172061 257, /* (108) stl_prefix ::= */
172062 256, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
172063 256, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
172064 256, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
172065 256, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
172066 256, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
172067 200, /* (114) dbnm ::= */
172068 200, /* (115) dbnm ::= DOT nm */
172069 238, /* (116) fullname ::= nm */
172070 238, /* (117) fullname ::= nm DOT nm */
172071 262, /* (118) xfullname ::= nm */
172072 262, /* (119) xfullname ::= nm DOT nm */
172073 262, /* (120) xfullname ::= nm DOT nm AS nm */
172074 262, /* (121) xfullname ::= nm AS nm */
172075 258, /* (122) joinop ::= COMMA|JOIN */
172076 258, /* (123) joinop ::= JOIN_KW JOIN */
172077 258, /* (124) joinop ::= JOIN_KW nm JOIN */
172078 258, /* (125) joinop ::= JOIN_KW nm nm JOIN */
172079 259, /* (126) on_using ::= ON expr */
172080 259, /* (127) on_using ::= USING LP idlist RP */
172081 259, /* (128) on_using ::= */
172082 264, /* (129) indexed_opt ::= */
172083 260, /* (130) indexed_by ::= INDEXED BY nm */
172084 260, /* (131) indexed_by ::= NOT INDEXED */
172085 249, /* (132) orderby_opt ::= */
172086 249, /* (133) orderby_opt ::= ORDER BY sortlist */
172087 231, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
172088 231, /* (135) sortlist ::= expr sortorder nulls */
172089 219, /* (136) sortorder ::= ASC */
172090 219, /* (137) sortorder ::= DESC */
172091 219, /* (138) sortorder ::= */
172092 265, /* (139) nulls ::= NULLS FIRST */
172093 265, /* (140) nulls ::= NULLS LAST */
172094 265, /* (141) nulls ::= */
172095 247, /* (142) groupby_opt ::= */
172096 247, /* (143) groupby_opt ::= GROUP BY nexprlist */
172097 248, /* (144) having_opt ::= */
172098 248, /* (145) having_opt ::= HAVING expr */
172099 250, /* (146) limit_opt ::= */
172100 250, /* (147) limit_opt ::= LIMIT expr */
172101 250, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
172102 250, /* (149) limit_opt ::= LIMIT expr COMMA expr */
172103 190, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
172104 246, /* (151) where_opt ::= */
172105 246, /* (152) where_opt ::= WHERE expr */
172106 267, /* (153) where_opt_ret ::= */
172107 267, /* (154) where_opt_ret ::= WHERE expr */
172108 267, /* (155) where_opt_ret ::= RETURNING selcollist */
172109 267, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
172110 190, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
172111 268, /* (158) setlist ::= setlist COMMA nm EQ expr */
172112 268, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
172113 268, /* (160) setlist ::= nm EQ expr */
172114 268, /* (161) setlist ::= LP idlist RP EQ expr */
172115 190, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
172116 190, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
172117 271, /* (164) upsert ::= */
172118 271, /* (165) upsert ::= RETURNING selcollist */
172119 271, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
172120 271, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
172121 271, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
172122 271, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
172123 272, /* (170) returning ::= RETURNING selcollist */
172124 269, /* (171) insert_cmd ::= INSERT orconf */
172125 269, /* (172) insert_cmd ::= REPLACE */
172126 270, /* (173) idlist_opt ::= */
172127 270, /* (174) idlist_opt ::= LP idlist RP */
172128 263, /* (175) idlist ::= idlist COMMA nm */
172129 263, /* (176) idlist ::= nm */
172130 217, /* (177) expr ::= LP expr RP */
172131 217, /* (178) expr ::= ID|INDEXED|JOIN_KW */
172132 217, /* (179) expr ::= nm DOT nm */
172133 217, /* (180) expr ::= nm DOT nm DOT nm */
172134 216, /* (181) term ::= NULL|FLOAT|BLOB */
172135 216, /* (182) term ::= STRING */
172136 216, /* (183) term ::= INTEGER */
172137 217, /* (184) expr ::= VARIABLE */
172138 217, /* (185) expr ::= expr COLLATE ID|STRING */
172139 217, /* (186) expr ::= CAST LP expr AS typetoken RP */
172140 217, /* (187) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
172141 217, /* (188) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
172142 217, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
172143 217, /* (190) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
172144 216, /* (191) term ::= CTIME_KW */
172145 217, /* (192) expr ::= LP nexprlist COMMA expr RP */
172146 217, /* (193) expr ::= expr AND expr */
172147 217, /* (194) expr ::= expr OR expr */
172148 217, /* (195) expr ::= expr LT|GT|GE|LE expr */
172149 217, /* (196) expr ::= expr EQ|NE expr */
172150 217, /* (197) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
172151 217, /* (198) expr ::= expr PLUS|MINUS expr */
172152 217, /* (199) expr ::= expr STAR|SLASH|REM expr */
172153 217, /* (200) expr ::= expr CONCAT expr */
172154 274, /* (201) likeop ::= NOT LIKE_KW|MATCH */
172155 217, /* (202) expr ::= expr likeop expr */
172156 217, /* (203) expr ::= expr likeop expr ESCAPE expr */
172157 217, /* (204) expr ::= expr ISNULL|NOTNULL */
172158 217, /* (205) expr ::= expr NOT NULL */
172159 217, /* (206) expr ::= expr IS expr */
172160 217, /* (207) expr ::= expr IS NOT expr */
172161 217, /* (208) expr ::= expr IS NOT DISTINCT FROM expr */
172162 217, /* (209) expr ::= expr IS DISTINCT FROM expr */
172163 217, /* (210) expr ::= NOT expr */
172164 217, /* (211) expr ::= BITNOT expr */
172165 217, /* (212) expr ::= PLUS|MINUS expr */
172166 217, /* (213) expr ::= expr PTR expr */
172167 275, /* (214) between_op ::= BETWEEN */
172168 275, /* (215) between_op ::= NOT BETWEEN */
172169 217, /* (216) expr ::= expr between_op expr AND expr */
172170 276, /* (217) in_op ::= IN */
172171 276, /* (218) in_op ::= NOT IN */
172172 217, /* (219) expr ::= expr in_op LP exprlist RP */
172173 217, /* (220) expr ::= LP select RP */
172174 217, /* (221) expr ::= expr in_op LP select RP */
172175 217, /* (222) expr ::= expr in_op nm dbnm paren_exprlist */
172176 217, /* (223) expr ::= EXISTS LP select RP */
172177 217, /* (224) expr ::= CASE case_operand case_exprlist case_else END */
172178 279, /* (225) case_exprlist ::= case_exprlist WHEN expr THEN expr */
172179 279, /* (226) case_exprlist ::= WHEN expr THEN expr */
172180 280, /* (227) case_else ::= ELSE expr */
172181 280, /* (228) case_else ::= */
172182 278, /* (229) case_operand ::= */
172183 261, /* (230) exprlist ::= */
172184 253, /* (231) nexprlist ::= nexprlist COMMA expr */
172185 253, /* (232) nexprlist ::= expr */
172186 277, /* (233) paren_exprlist ::= */
172187 277, /* (234) paren_exprlist ::= LP exprlist RP */
172188 190, /* (235) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
172189 281, /* (236) uniqueflag ::= UNIQUE */
172190 281, /* (237) uniqueflag ::= */
172191 221, /* (238) eidlist_opt ::= */
172192 221, /* (239) eidlist_opt ::= LP eidlist RP */
172193 232, /* (240) eidlist ::= eidlist COMMA nm collate sortorder */
172194 232, /* (241) eidlist ::= nm collate sortorder */
172195 282, /* (242) collate ::= */
172196 282, /* (243) collate ::= COLLATE ID|STRING */
172197 190, /* (244) cmd ::= DROP INDEX ifexists fullname */
172198 190, /* (245) cmd ::= VACUUM vinto */
172199 190, /* (246) cmd ::= VACUUM nm vinto */
172200 283, /* (247) vinto ::= INTO expr */
172201 283, /* (248) vinto ::= */
172202 190, /* (249) cmd ::= PRAGMA nm dbnm */
172203 190, /* (250) cmd ::= PRAGMA nm dbnm EQ nmnum */
172204 190, /* (251) cmd ::= PRAGMA nm dbnm LP nmnum RP */
172205 190, /* (252) cmd ::= PRAGMA nm dbnm EQ minus_num */
172206 190, /* (253) cmd ::= PRAGMA nm dbnm LP minus_num RP */
172207 211, /* (254) plus_num ::= PLUS INTEGER|FLOAT */
172208 212, /* (255) minus_num ::= MINUS INTEGER|FLOAT */
172209 190, /* (256) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
172210 285, /* (257) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
172211 287, /* (258) trigger_time ::= BEFORE|AFTER */
172212 287, /* (259) trigger_time ::= INSTEAD OF */
172213 287, /* (260) trigger_time ::= */
172214 288, /* (261) trigger_event ::= DELETE|INSERT */
172215 288, /* (262) trigger_event ::= UPDATE */
172216 288, /* (263) trigger_event ::= UPDATE OF idlist */
172217 290, /* (264) when_clause ::= */
172218 290, /* (265) when_clause ::= WHEN expr */
172219 286, /* (266) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
172220 286, /* (267) trigger_cmd_list ::= trigger_cmd SEMI */
172221 292, /* (268) trnm ::= nm DOT nm */
172222 293, /* (269) tridxby ::= INDEXED BY nm */
172223 293, /* (270) tridxby ::= NOT INDEXED */
172224 291, /* (271) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
172225 291, /* (272) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
172226 291, /* (273) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
172227 291, /* (274) trigger_cmd ::= scanpt select scanpt */
172228 217, /* (275) expr ::= RAISE LP IGNORE RP */
172229 217, /* (276) expr ::= RAISE LP raisetype COMMA nm RP */
172230 236, /* (277) raisetype ::= ROLLBACK */
172231 236, /* (278) raisetype ::= ABORT */
172232 236, /* (279) raisetype ::= FAIL */
172233 190, /* (280) cmd ::= DROP TRIGGER ifexists fullname */
172234 190, /* (281) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
172235 190, /* (282) cmd ::= DETACH database_kw_opt expr */
172236 295, /* (283) key_opt ::= */
172237 295, /* (284) key_opt ::= KEY expr */
172238 190, /* (285) cmd ::= REINDEX */
172239 190, /* (286) cmd ::= REINDEX nm dbnm */
172240 190, /* (287) cmd ::= ANALYZE */
172241 190, /* (288) cmd ::= ANALYZE nm dbnm */
172242 190, /* (289) cmd ::= ALTER TABLE fullname RENAME TO nm */
172243 190, /* (290) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
172244 190, /* (291) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
172245 296, /* (292) add_column_fullname ::= fullname */
172246 190, /* (293) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
172247 190, /* (294) cmd ::= create_vtab */
172248 190, /* (295) cmd ::= create_vtab LP vtabarglist RP */
172249 298, /* (296) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
172250 300, /* (297) vtabarg ::= */
172251 301, /* (298) vtabargtoken ::= ANY */
172252 301, /* (299) vtabargtoken ::= lp anylist RP */
172253 302, /* (300) lp ::= LP */
172254 266, /* (301) with ::= WITH wqlist */
172255 266, /* (302) with ::= WITH RECURSIVE wqlist */
172256 305, /* (303) wqas ::= AS */
172257 305, /* (304) wqas ::= AS MATERIALIZED */
172258 305, /* (305) wqas ::= AS NOT MATERIALIZED */
172259 304, /* (306) wqitem ::= nm eidlist_opt wqas LP select RP */
172260 241, /* (307) wqlist ::= wqitem */
172261 241, /* (308) wqlist ::= wqlist COMMA wqitem */
172262 306, /* (309) windowdefn_list ::= windowdefn_list COMMA windowdefn */
172263 307, /* (310) windowdefn ::= nm AS LP window RP */
172264 308, /* (311) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
172265 308, /* (312) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
172266 308, /* (313) window ::= ORDER BY sortlist frame_opt */
172267 308, /* (314) window ::= nm ORDER BY sortlist frame_opt */
172268 308, /* (315) window ::= nm frame_opt */
172269 309, /* (316) frame_opt ::= */
172270 309, /* (317) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
172271 309, /* (318) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
172272 313, /* (319) range_or_rows ::= RANGE|ROWS|GROUPS */
172273 315, /* (320) frame_bound_s ::= frame_bound */
172274 315, /* (321) frame_bound_s ::= UNBOUNDED PRECEDING */
172275 316, /* (322) frame_bound_e ::= frame_bound */
172276 316, /* (323) frame_bound_e ::= UNBOUNDED FOLLOWING */
172277 314, /* (324) frame_bound ::= expr PRECEDING|FOLLOWING */
172278 314, /* (325) frame_bound ::= CURRENT ROW */
172279 317, /* (326) frame_exclude_opt ::= */
172280 317, /* (327) frame_exclude_opt ::= EXCLUDE frame_exclude */
172281 318, /* (328) frame_exclude ::= NO OTHERS */
172282 318, /* (329) frame_exclude ::= CURRENT ROW */
172283 318, /* (330) frame_exclude ::= GROUP|TIES */
172284 251, /* (331) window_clause ::= WINDOW windowdefn_list */
172285 273, /* (332) filter_over ::= filter_clause over_clause */
172286 273, /* (333) filter_over ::= over_clause */
172287 273, /* (334) filter_over ::= filter_clause */
172288 312, /* (335) over_clause ::= OVER LP window RP */
172289 312, /* (336) over_clause ::= OVER nm */
172290 311, /* (337) filter_clause ::= FILTER LP WHERE expr RP */
172291 185, /* (338) input ::= cmdlist */
172292 186, /* (339) cmdlist ::= cmdlist ecmd */
172293 186, /* (340) cmdlist ::= ecmd */
172294 187, /* (341) ecmd ::= SEMI */
172295 187, /* (342) ecmd ::= cmdx SEMI */
172296 187, /* (343) ecmd ::= explain cmdx SEMI */
172297 192, /* (344) trans_opt ::= */
172298 192, /* (345) trans_opt ::= TRANSACTION */
172299 192, /* (346) trans_opt ::= TRANSACTION nm */
172300 194, /* (347) savepoint_opt ::= SAVEPOINT */
172301 194, /* (348) savepoint_opt ::= */
172302 190, /* (349) cmd ::= create_table create_table_args */
172303 203, /* (350) table_option_set ::= table_option */
172304 201, /* (351) columnlist ::= columnlist COMMA columnname carglist */
172305 201, /* (352) columnlist ::= columnname carglist */
172306 193, /* (353) nm ::= ID|INDEXED|JOIN_KW */
172307 193, /* (354) nm ::= STRING */
172308 208, /* (355) typetoken ::= typename */
172309 209, /* (356) typename ::= ID|STRING */
172310 210, /* (357) signed ::= plus_num */
172311 210, /* (358) signed ::= minus_num */
172312 207, /* (359) carglist ::= carglist ccons */
172313 207, /* (360) carglist ::= */
172314 215, /* (361) ccons ::= NULL onconf */
172315 215, /* (362) ccons ::= GENERATED ALWAYS AS generated */
172316 215, /* (363) ccons ::= AS generated */
172317 202, /* (364) conslist_opt ::= COMMA conslist */
172318 228, /* (365) conslist ::= conslist tconscomma tcons */
172319 228, /* (366) conslist ::= tcons */
172320 229, /* (367) tconscomma ::= */
172321 233, /* (368) defer_subclause_opt ::= defer_subclause */
172322 235, /* (369) resolvetype ::= raisetype */
172323 239, /* (370) selectnowith ::= oneselect */
172324 240, /* (371) oneselect ::= values */
172325 254, /* (372) sclp ::= selcollist COMMA */
172326 255, /* (373) as ::= ID|STRING */
172327 264, /* (374) indexed_opt ::= indexed_by */
172328 272, /* (375) returning ::= */
172329 217, /* (376) expr ::= term */
172330 274, /* (377) likeop ::= LIKE_KW|MATCH */
172331 278, /* (378) case_operand ::= expr */
172332 261, /* (379) exprlist ::= nexprlist */
172333 284, /* (380) nmnum ::= plus_num */
172334 284, /* (381) nmnum ::= nm */
172335 284, /* (382) nmnum ::= ON */
172336 284, /* (383) nmnum ::= DELETE */
172337 284, /* (384) nmnum ::= DEFAULT */
172338 211, /* (385) plus_num ::= INTEGER|FLOAT */
172339 289, /* (386) foreach_clause ::= */
172340 289, /* (387) foreach_clause ::= FOR EACH ROW */
172341 292, /* (388) trnm ::= nm */
172342 293, /* (389) tridxby ::= */
172343 294, /* (390) database_kw_opt ::= DATABASE */
172344 294, /* (391) database_kw_opt ::= */
172345 297, /* (392) kwcolumn_opt ::= */
172346 297, /* (393) kwcolumn_opt ::= COLUMNKW */
172347 299, /* (394) vtabarglist ::= vtabarg */
172348 299, /* (395) vtabarglist ::= vtabarglist COMMA vtabarg */
172349 300, /* (396) vtabarg ::= vtabarg vtabargtoken */
172350 303, /* (397) anylist ::= */
172351 303, /* (398) anylist ::= anylist LP anylist RP */
172352 303, /* (399) anylist ::= anylist ANY */
172353 266, /* (400) with ::= */
172354 306, /* (401) windowdefn_list ::= windowdefn */
172355 308, /* (402) window ::= frame_opt */
172356};
172357
172358/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
172359** of symbols on the right-hand side of that rule. */
172360static const signed char yyRuleInfoNRhs[] = {
172361 -1, /* (0) explain ::= EXPLAIN */
172362 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
172363 -1, /* (2) cmdx ::= cmd */
172364 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
172365 0, /* (4) transtype ::= */
172366 -1, /* (5) transtype ::= DEFERRED */
172367 -1, /* (6) transtype ::= IMMEDIATE */
172368 -1, /* (7) transtype ::= EXCLUSIVE */
172369 -2, /* (8) cmd ::= COMMIT|END trans_opt */
172370 -2, /* (9) cmd ::= ROLLBACK trans_opt */
172371 -2, /* (10) cmd ::= SAVEPOINT nm */
172372 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
172373 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
172374 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
172375 -1, /* (14) createkw ::= CREATE */
172376 0, /* (15) ifnotexists ::= */
172377 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
172378 -1, /* (17) temp ::= TEMP */
172379 0, /* (18) temp ::= */
172380 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
172381 -2, /* (20) create_table_args ::= AS select */
172382 0, /* (21) table_option_set ::= */
172383 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
172384 -2, /* (23) table_option ::= WITHOUT nm */
172385 -1, /* (24) table_option ::= nm */
172386 -2, /* (25) columnname ::= nm typetoken */
172387 0, /* (26) typetoken ::= */
172388 -4, /* (27) typetoken ::= typename LP signed RP */
172389 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
172390 -2, /* (29) typename ::= typename ID|STRING */
172391 0, /* (30) scanpt ::= */
172392 0, /* (31) scantok ::= */
172393 -2, /* (32) ccons ::= CONSTRAINT nm */
172394 -3, /* (33) ccons ::= DEFAULT scantok term */
172395 -4, /* (34) ccons ::= DEFAULT LP expr RP */
172396 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
172397 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
172398 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
172399 -3, /* (38) ccons ::= NOT NULL onconf */
172400 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
172401 -2, /* (40) ccons ::= UNIQUE onconf */
172402 -4, /* (41) ccons ::= CHECK LP expr RP */
172403 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
172404 -1, /* (43) ccons ::= defer_subclause */
172405 -2, /* (44) ccons ::= COLLATE ID|STRING */
172406 -3, /* (45) generated ::= LP expr RP */
172407 -4, /* (46) generated ::= LP expr RP ID */
172408 0, /* (47) autoinc ::= */
172409 -1, /* (48) autoinc ::= AUTOINCR */
172410 0, /* (49) refargs ::= */
172411 -2, /* (50) refargs ::= refargs refarg */
172412 -2, /* (51) refarg ::= MATCH nm */
172413 -3, /* (52) refarg ::= ON INSERT refact */
172414 -3, /* (53) refarg ::= ON DELETE refact */
172415 -3, /* (54) refarg ::= ON UPDATE refact */
172416 -2, /* (55) refact ::= SET NULL */
172417 -2, /* (56) refact ::= SET DEFAULT */
172418 -1, /* (57) refact ::= CASCADE */
172419 -1, /* (58) refact ::= RESTRICT */
172420 -2, /* (59) refact ::= NO ACTION */
172421 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
172422 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
172423 0, /* (62) init_deferred_pred_opt ::= */
172424 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
172425 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
172426 0, /* (65) conslist_opt ::= */
172427 -1, /* (66) tconscomma ::= COMMA */
172428 -2, /* (67) tcons ::= CONSTRAINT nm */
172429 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
172430 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
172431 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
172432 -10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
172433 0, /* (72) defer_subclause_opt ::= */
172434 0, /* (73) onconf ::= */
172435 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
172436 0, /* (75) orconf ::= */
172437 -2, /* (76) orconf ::= OR resolvetype */
172438 -1, /* (77) resolvetype ::= IGNORE */
172439 -1, /* (78) resolvetype ::= REPLACE */
172440 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
172441 -2, /* (80) ifexists ::= IF EXISTS */
172442 0, /* (81) ifexists ::= */
172443 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
172444 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
172445 -1, /* (84) cmd ::= select */
172446 -3, /* (85) select ::= WITH wqlist selectnowith */
172447 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
172448 -1, /* (87) select ::= selectnowith */
172449 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
172450 -1, /* (89) multiselect_op ::= UNION */
172451 -2, /* (90) multiselect_op ::= UNION ALL */
172452 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
172453 -9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
172454 -10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
172455 -4, /* (94) values ::= VALUES LP nexprlist RP */
172456 -5, /* (95) values ::= values COMMA LP nexprlist RP */
172457 -1, /* (96) distinct ::= DISTINCT */
172458 -1, /* (97) distinct ::= ALL */
172459 0, /* (98) distinct ::= */
172460 0, /* (99) sclp ::= */
172461 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
172462 -3, /* (101) selcollist ::= sclp scanpt STAR */
172463 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
172464 -2, /* (103) as ::= AS nm */
172465 0, /* (104) as ::= */
172466 0, /* (105) from ::= */
172467 -2, /* (106) from ::= FROM seltablist */
172468 -2, /* (107) stl_prefix ::= seltablist joinop */
172469 0, /* (108) stl_prefix ::= */
172470 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
172471 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
172472 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
172473 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
172474 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
172475 0, /* (114) dbnm ::= */
172476 -2, /* (115) dbnm ::= DOT nm */
172477 -1, /* (116) fullname ::= nm */
172478 -3, /* (117) fullname ::= nm DOT nm */
172479 -1, /* (118) xfullname ::= nm */
172480 -3, /* (119) xfullname ::= nm DOT nm */
172481 -5, /* (120) xfullname ::= nm DOT nm AS nm */
172482 -3, /* (121) xfullname ::= nm AS nm */
172483 -1, /* (122) joinop ::= COMMA|JOIN */
172484 -2, /* (123) joinop ::= JOIN_KW JOIN */
172485 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
172486 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
172487 -2, /* (126) on_using ::= ON expr */
172488 -4, /* (127) on_using ::= USING LP idlist RP */
172489 0, /* (128) on_using ::= */
172490 0, /* (129) indexed_opt ::= */
172491 -3, /* (130) indexed_by ::= INDEXED BY nm */
172492 -2, /* (131) indexed_by ::= NOT INDEXED */
172493 0, /* (132) orderby_opt ::= */
172494 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
172495 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
172496 -3, /* (135) sortlist ::= expr sortorder nulls */
172497 -1, /* (136) sortorder ::= ASC */
172498 -1, /* (137) sortorder ::= DESC */
172499 0, /* (138) sortorder ::= */
172500 -2, /* (139) nulls ::= NULLS FIRST */
172501 -2, /* (140) nulls ::= NULLS LAST */
172502 0, /* (141) nulls ::= */
172503 0, /* (142) groupby_opt ::= */
172504 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
172505 0, /* (144) having_opt ::= */
172506 -2, /* (145) having_opt ::= HAVING expr */
172507 0, /* (146) limit_opt ::= */
172508 -2, /* (147) limit_opt ::= LIMIT expr */
172509 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
172510 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
172511 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
172512 0, /* (151) where_opt ::= */
172513 -2, /* (152) where_opt ::= WHERE expr */
172514 0, /* (153) where_opt_ret ::= */
172515 -2, /* (154) where_opt_ret ::= WHERE expr */
172516 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
172517 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
172518 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
172519 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
172520 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
172521 -3, /* (160) setlist ::= nm EQ expr */
172522 -5, /* (161) setlist ::= LP idlist RP EQ expr */
172523 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
172524 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
172525 0, /* (164) upsert ::= */
172526 -2, /* (165) upsert ::= RETURNING selcollist */
172527 -12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
172528 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
172529 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
172530 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
172531 -2, /* (170) returning ::= RETURNING selcollist */
172532 -2, /* (171) insert_cmd ::= INSERT orconf */
172533 -1, /* (172) insert_cmd ::= REPLACE */
172534 0, /* (173) idlist_opt ::= */
172535 -3, /* (174) idlist_opt ::= LP idlist RP */
172536 -3, /* (175) idlist ::= idlist COMMA nm */
172537 -1, /* (176) idlist ::= nm */
172538 -3, /* (177) expr ::= LP expr RP */
172539 -1, /* (178) expr ::= ID|INDEXED|JOIN_KW */
172540 -3, /* (179) expr ::= nm DOT nm */
172541 -5, /* (180) expr ::= nm DOT nm DOT nm */
172542 -1, /* (181) term ::= NULL|FLOAT|BLOB */
172543 -1, /* (182) term ::= STRING */
172544 -1, /* (183) term ::= INTEGER */
172545 -1, /* (184) expr ::= VARIABLE */
172546 -3, /* (185) expr ::= expr COLLATE ID|STRING */
172547 -6, /* (186) expr ::= CAST LP expr AS typetoken RP */
172548 -5, /* (187) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
172549 -4, /* (188) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
172550 -6, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
172551 -5, /* (190) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
172552 -1, /* (191) term ::= CTIME_KW */
172553 -5, /* (192) expr ::= LP nexprlist COMMA expr RP */
172554 -3, /* (193) expr ::= expr AND expr */
172555 -3, /* (194) expr ::= expr OR expr */
172556 -3, /* (195) expr ::= expr LT|GT|GE|LE expr */
172557 -3, /* (196) expr ::= expr EQ|NE expr */
172558 -3, /* (197) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
172559 -3, /* (198) expr ::= expr PLUS|MINUS expr */
172560 -3, /* (199) expr ::= expr STAR|SLASH|REM expr */
172561 -3, /* (200) expr ::= expr CONCAT expr */
172562 -2, /* (201) likeop ::= NOT LIKE_KW|MATCH */
172563 -3, /* (202) expr ::= expr likeop expr */
172564 -5, /* (203) expr ::= expr likeop expr ESCAPE expr */
172565 -2, /* (204) expr ::= expr ISNULL|NOTNULL */
172566 -3, /* (205) expr ::= expr NOT NULL */
172567 -3, /* (206) expr ::= expr IS expr */
172568 -4, /* (207) expr ::= expr IS NOT expr */
172569 -6, /* (208) expr ::= expr IS NOT DISTINCT FROM expr */
172570 -5, /* (209) expr ::= expr IS DISTINCT FROM expr */
172571 -2, /* (210) expr ::= NOT expr */
172572 -2, /* (211) expr ::= BITNOT expr */
172573 -2, /* (212) expr ::= PLUS|MINUS expr */
172574 -3, /* (213) expr ::= expr PTR expr */
172575 -1, /* (214) between_op ::= BETWEEN */
172576 -2, /* (215) between_op ::= NOT BETWEEN */
172577 -5, /* (216) expr ::= expr between_op expr AND expr */
172578 -1, /* (217) in_op ::= IN */
172579 -2, /* (218) in_op ::= NOT IN */
172580 -5, /* (219) expr ::= expr in_op LP exprlist RP */
172581 -3, /* (220) expr ::= LP select RP */
172582 -5, /* (221) expr ::= expr in_op LP select RP */
172583 -5, /* (222) expr ::= expr in_op nm dbnm paren_exprlist */
172584 -4, /* (223) expr ::= EXISTS LP select RP */
172585 -5, /* (224) expr ::= CASE case_operand case_exprlist case_else END */
172586 -5, /* (225) case_exprlist ::= case_exprlist WHEN expr THEN expr */
172587 -4, /* (226) case_exprlist ::= WHEN expr THEN expr */
172588 -2, /* (227) case_else ::= ELSE expr */
172589 0, /* (228) case_else ::= */
172590 0, /* (229) case_operand ::= */
172591 0, /* (230) exprlist ::= */
172592 -3, /* (231) nexprlist ::= nexprlist COMMA expr */
172593 -1, /* (232) nexprlist ::= expr */
172594 0, /* (233) paren_exprlist ::= */
172595 -3, /* (234) paren_exprlist ::= LP exprlist RP */
172596 -12, /* (235) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
172597 -1, /* (236) uniqueflag ::= UNIQUE */
172598 0, /* (237) uniqueflag ::= */
172599 0, /* (238) eidlist_opt ::= */
172600 -3, /* (239) eidlist_opt ::= LP eidlist RP */
172601 -5, /* (240) eidlist ::= eidlist COMMA nm collate sortorder */
172602 -3, /* (241) eidlist ::= nm collate sortorder */
172603 0, /* (242) collate ::= */
172604 -2, /* (243) collate ::= COLLATE ID|STRING */
172605 -4, /* (244) cmd ::= DROP INDEX ifexists fullname */
172606 -2, /* (245) cmd ::= VACUUM vinto */
172607 -3, /* (246) cmd ::= VACUUM nm vinto */
172608 -2, /* (247) vinto ::= INTO expr */
172609 0, /* (248) vinto ::= */
172610 -3, /* (249) cmd ::= PRAGMA nm dbnm */
172611 -5, /* (250) cmd ::= PRAGMA nm dbnm EQ nmnum */
172612 -6, /* (251) cmd ::= PRAGMA nm dbnm LP nmnum RP */
172613 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ minus_num */
172614 -6, /* (253) cmd ::= PRAGMA nm dbnm LP minus_num RP */
172615 -2, /* (254) plus_num ::= PLUS INTEGER|FLOAT */
172616 -2, /* (255) minus_num ::= MINUS INTEGER|FLOAT */
172617 -5, /* (256) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
172618 -11, /* (257) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
172619 -1, /* (258) trigger_time ::= BEFORE|AFTER */
172620 -2, /* (259) trigger_time ::= INSTEAD OF */
172621 0, /* (260) trigger_time ::= */
172622 -1, /* (261) trigger_event ::= DELETE|INSERT */
172623 -1, /* (262) trigger_event ::= UPDATE */
172624 -3, /* (263) trigger_event ::= UPDATE OF idlist */
172625 0, /* (264) when_clause ::= */
172626 -2, /* (265) when_clause ::= WHEN expr */
172627 -3, /* (266) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
172628 -2, /* (267) trigger_cmd_list ::= trigger_cmd SEMI */
172629 -3, /* (268) trnm ::= nm DOT nm */
172630 -3, /* (269) tridxby ::= INDEXED BY nm */
172631 -2, /* (270) tridxby ::= NOT INDEXED */
172632 -9, /* (271) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
172633 -8, /* (272) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
172634 -6, /* (273) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
172635 -3, /* (274) trigger_cmd ::= scanpt select scanpt */
172636 -4, /* (275) expr ::= RAISE LP IGNORE RP */
172637 -6, /* (276) expr ::= RAISE LP raisetype COMMA nm RP */
172638 -1, /* (277) raisetype ::= ROLLBACK */
172639 -1, /* (278) raisetype ::= ABORT */
172640 -1, /* (279) raisetype ::= FAIL */
172641 -4, /* (280) cmd ::= DROP TRIGGER ifexists fullname */
172642 -6, /* (281) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
172643 -3, /* (282) cmd ::= DETACH database_kw_opt expr */
172644 0, /* (283) key_opt ::= */
172645 -2, /* (284) key_opt ::= KEY expr */
172646 -1, /* (285) cmd ::= REINDEX */
172647 -3, /* (286) cmd ::= REINDEX nm dbnm */
172648 -1, /* (287) cmd ::= ANALYZE */
172649 -3, /* (288) cmd ::= ANALYZE nm dbnm */
172650 -6, /* (289) cmd ::= ALTER TABLE fullname RENAME TO nm */
172651 -7, /* (290) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
172652 -6, /* (291) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
172653 -1, /* (292) add_column_fullname ::= fullname */
172654 -8, /* (293) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
172655 -1, /* (294) cmd ::= create_vtab */
172656 -4, /* (295) cmd ::= create_vtab LP vtabarglist RP */
172657 -8, /* (296) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
172658 0, /* (297) vtabarg ::= */
172659 -1, /* (298) vtabargtoken ::= ANY */
172660 -3, /* (299) vtabargtoken ::= lp anylist RP */
172661 -1, /* (300) lp ::= LP */
172662 -2, /* (301) with ::= WITH wqlist */
172663 -3, /* (302) with ::= WITH RECURSIVE wqlist */
172664 -1, /* (303) wqas ::= AS */
172665 -2, /* (304) wqas ::= AS MATERIALIZED */
172666 -3, /* (305) wqas ::= AS NOT MATERIALIZED */
172667 -6, /* (306) wqitem ::= nm eidlist_opt wqas LP select RP */
172668 -1, /* (307) wqlist ::= wqitem */
172669 -3, /* (308) wqlist ::= wqlist COMMA wqitem */
172670 -3, /* (309) windowdefn_list ::= windowdefn_list COMMA windowdefn */
172671 -5, /* (310) windowdefn ::= nm AS LP window RP */
172672 -5, /* (311) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
172673 -6, /* (312) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
172674 -4, /* (313) window ::= ORDER BY sortlist frame_opt */
172675 -5, /* (314) window ::= nm ORDER BY sortlist frame_opt */
172676 -2, /* (315) window ::= nm frame_opt */
172677 0, /* (316) frame_opt ::= */
172678 -3, /* (317) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
172679 -6, /* (318) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
172680 -1, /* (319) range_or_rows ::= RANGE|ROWS|GROUPS */
172681 -1, /* (320) frame_bound_s ::= frame_bound */
172682 -2, /* (321) frame_bound_s ::= UNBOUNDED PRECEDING */
172683 -1, /* (322) frame_bound_e ::= frame_bound */
172684 -2, /* (323) frame_bound_e ::= UNBOUNDED FOLLOWING */
172685 -2, /* (324) frame_bound ::= expr PRECEDING|FOLLOWING */
172686 -2, /* (325) frame_bound ::= CURRENT ROW */
172687 0, /* (326) frame_exclude_opt ::= */
172688 -2, /* (327) frame_exclude_opt ::= EXCLUDE frame_exclude */
172689 -2, /* (328) frame_exclude ::= NO OTHERS */
172690 -2, /* (329) frame_exclude ::= CURRENT ROW */
172691 -1, /* (330) frame_exclude ::= GROUP|TIES */
172692 -2, /* (331) window_clause ::= WINDOW windowdefn_list */
172693 -2, /* (332) filter_over ::= filter_clause over_clause */
172694 -1, /* (333) filter_over ::= over_clause */
172695 -1, /* (334) filter_over ::= filter_clause */
172696 -4, /* (335) over_clause ::= OVER LP window RP */
172697 -2, /* (336) over_clause ::= OVER nm */
172698 -5, /* (337) filter_clause ::= FILTER LP WHERE expr RP */
172699 -1, /* (338) input ::= cmdlist */
172700 -2, /* (339) cmdlist ::= cmdlist ecmd */
172701 -1, /* (340) cmdlist ::= ecmd */
172702 -1, /* (341) ecmd ::= SEMI */
172703 -2, /* (342) ecmd ::= cmdx SEMI */
172704 -3, /* (343) ecmd ::= explain cmdx SEMI */
172705 0, /* (344) trans_opt ::= */
172706 -1, /* (345) trans_opt ::= TRANSACTION */
172707 -2, /* (346) trans_opt ::= TRANSACTION nm */
172708 -1, /* (347) savepoint_opt ::= SAVEPOINT */
172709 0, /* (348) savepoint_opt ::= */
172710 -2, /* (349) cmd ::= create_table create_table_args */
172711 -1, /* (350) table_option_set ::= table_option */
172712 -4, /* (351) columnlist ::= columnlist COMMA columnname carglist */
172713 -2, /* (352) columnlist ::= columnname carglist */
172714 -1, /* (353) nm ::= ID|INDEXED|JOIN_KW */
172715 -1, /* (354) nm ::= STRING */
172716 -1, /* (355) typetoken ::= typename */
172717 -1, /* (356) typename ::= ID|STRING */
172718 -1, /* (357) signed ::= plus_num */
172719 -1, /* (358) signed ::= minus_num */
172720 -2, /* (359) carglist ::= carglist ccons */
172721 0, /* (360) carglist ::= */
172722 -2, /* (361) ccons ::= NULL onconf */
172723 -4, /* (362) ccons ::= GENERATED ALWAYS AS generated */
172724 -2, /* (363) ccons ::= AS generated */
172725 -2, /* (364) conslist_opt ::= COMMA conslist */
172726 -3, /* (365) conslist ::= conslist tconscomma tcons */
172727 -1, /* (366) conslist ::= tcons */
172728 0, /* (367) tconscomma ::= */
172729 -1, /* (368) defer_subclause_opt ::= defer_subclause */
172730 -1, /* (369) resolvetype ::= raisetype */
172731 -1, /* (370) selectnowith ::= oneselect */
172732 -1, /* (371) oneselect ::= values */
172733 -2, /* (372) sclp ::= selcollist COMMA */
172734 -1, /* (373) as ::= ID|STRING */
172735 -1, /* (374) indexed_opt ::= indexed_by */
172736 0, /* (375) returning ::= */
172737 -1, /* (376) expr ::= term */
172738 -1, /* (377) likeop ::= LIKE_KW|MATCH */
172739 -1, /* (378) case_operand ::= expr */
172740 -1, /* (379) exprlist ::= nexprlist */
172741 -1, /* (380) nmnum ::= plus_num */
172742 -1, /* (381) nmnum ::= nm */
172743 -1, /* (382) nmnum ::= ON */
172744 -1, /* (383) nmnum ::= DELETE */
172745 -1, /* (384) nmnum ::= DEFAULT */
172746 -1, /* (385) plus_num ::= INTEGER|FLOAT */
172747 0, /* (386) foreach_clause ::= */
172748 -3, /* (387) foreach_clause ::= FOR EACH ROW */
172749 -1, /* (388) trnm ::= nm */
172750 0, /* (389) tridxby ::= */
172751 -1, /* (390) database_kw_opt ::= DATABASE */
172752 0, /* (391) database_kw_opt ::= */
172753 0, /* (392) kwcolumn_opt ::= */
172754 -1, /* (393) kwcolumn_opt ::= COLUMNKW */
172755 -1, /* (394) vtabarglist ::= vtabarg */
172756 -3, /* (395) vtabarglist ::= vtabarglist COMMA vtabarg */
172757 -2, /* (396) vtabarg ::= vtabarg vtabargtoken */
172758 0, /* (397) anylist ::= */
172759 -4, /* (398) anylist ::= anylist LP anylist RP */
172760 -2, /* (399) anylist ::= anylist ANY */
172761 0, /* (400) with ::= */
172762 -1, /* (401) windowdefn_list ::= windowdefn */
172763 -1, /* (402) window ::= frame_opt */
172764};
172765
172766static void yy_accept(yyParser*); /* Forward Declaration */
172767
172768/*
172769** Perform a reduce action and the shift that must immediately
172770** follow the reduce.
172771**
172772** The yyLookahead and yyLookaheadToken parameters provide reduce actions
172773** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
172774** if the lookahead token has already been consumed. As this procedure is
172775** only called from one place, optimizing compilers will in-line it, which
172776** means that the extra parameters have no performance impact.
172777*/
172778static YYACTIONTYPE yy_reduce(
172779 yyParser *yypParser, /* The parser */
172780 unsigned int yyruleno, /* Number of the rule by which to reduce */
172781 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
172782 sqlite3ParserTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
172783 sqlite3ParserCTX_PDECL /* %extra_context */
172784){
172785 int yygoto; /* The next state */
172786 YYACTIONTYPE yyact; /* The next action */
172787 yyStackEntry *yymsp; /* The top of the parser's stack */
172788 int yysize; /* Amount to pop the stack */
172789 sqlite3ParserARG_FETCH
172790 (void)yyLookahead;
172791 (void)yyLookaheadToken;
172792 yymsp = yypParser->yytos;
172793
172794 switch( yyruleno ){
172795 /* Beginning here are the reduction cases. A typical example
172796 ** follows:
172797 ** case 0:
172798 ** #line <lineno> <grammarfile>
172799 ** { ... } // User supplied code
172800 ** #line <lineno> <thisfile>
172801 ** break;
172802 */
172803/********** Begin reduce actions **********************************************/
172804 YYMINORTYPE yylhsminor;
172805 case 0: /* explain ::= EXPLAIN */
172806{ if( pParse->pReprepare==0 ) pParse->explain = 1; }
172807 break;
172808 case 1: /* explain ::= EXPLAIN QUERY PLAN */
172809{ if( pParse->pReprepare==0 ) pParse->explain = 2; }
172810 break;
172811 case 2: /* cmdx ::= cmd */
172812{ sqlite3FinishCoding(pParse); }
172813 break;
172814 case 3: /* cmd ::= BEGIN transtype trans_opt */
172815{sqlite3BeginTransaction(pParse, type: yymsp[-1].minor.yy394);}
172816 break;
172817 case 4: /* transtype ::= */
172818{yymsp[1].minor.yy394 = TK_DEFERRED;}
172819 break;
172820 case 5: /* transtype ::= DEFERRED */
172821 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
172822 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
172823 case 319: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==319);
172824{yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
172825 break;
172826 case 8: /* cmd ::= COMMIT|END trans_opt */
172827 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
172828{sqlite3EndTransaction(pParse,eType: yymsp[-1].major);}
172829 break;
172830 case 10: /* cmd ::= SAVEPOINT nm */
172831{
172832 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, pName: &yymsp[0].minor.yy0);
172833}
172834 break;
172835 case 11: /* cmd ::= RELEASE savepoint_opt nm */
172836{
172837 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, pName: &yymsp[0].minor.yy0);
172838}
172839 break;
172840 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
172841{
172842 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, pName: &yymsp[0].minor.yy0);
172843}
172844 break;
172845 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
172846{
172847 sqlite3StartTable(pParse,pName1: &yymsp[-1].minor.yy0,pName2: &yymsp[0].minor.yy0,isTemp: yymsp[-4].minor.yy394,isView: 0,isVirtual: 0,noErr: yymsp[-2].minor.yy394);
172848}
172849 break;
172850 case 14: /* createkw ::= CREATE */
172851{disableLookaside(pParse);}
172852 break;
172853 case 15: /* ifnotexists ::= */
172854 case 18: /* temp ::= */ yytestcase(yyruleno==18);
172855 case 47: /* autoinc ::= */ yytestcase(yyruleno==47);
172856 case 62: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==62);
172857 case 72: /* defer_subclause_opt ::= */ yytestcase(yyruleno==72);
172858 case 81: /* ifexists ::= */ yytestcase(yyruleno==81);
172859 case 98: /* distinct ::= */ yytestcase(yyruleno==98);
172860 case 242: /* collate ::= */ yytestcase(yyruleno==242);
172861{yymsp[1].minor.yy394 = 0;}
172862 break;
172863 case 16: /* ifnotexists ::= IF NOT EXISTS */
172864{yymsp[-2].minor.yy394 = 1;}
172865 break;
172866 case 17: /* temp ::= TEMP */
172867{yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
172868 break;
172869 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */
172870{
172871 sqlite3EndTable(pParse,pCons: &yymsp[-2].minor.yy0,pEnd: &yymsp[-1].minor.yy0,tabOpts: yymsp[0].minor.yy285,pSelect: 0);
172872}
172873 break;
172874 case 20: /* create_table_args ::= AS select */
172875{
172876 sqlite3EndTable(pParse,pCons: 0,pEnd: 0,tabOpts: 0,pSelect: yymsp[0].minor.yy47);
172877 sqlite3SelectDelete(db: pParse->db, p: yymsp[0].minor.yy47);
172878}
172879 break;
172880 case 21: /* table_option_set ::= */
172881{yymsp[1].minor.yy285 = 0;}
172882 break;
172883 case 22: /* table_option_set ::= table_option_set COMMA table_option */
172884{yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
172885 yymsp[-2].minor.yy285 = yylhsminor.yy285;
172886 break;
172887 case 23: /* table_option ::= WITHOUT nm */
172888{
172889 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(zLeft: yymsp[0].minor.yy0.z,zRight: "rowid",N: 5)==0 ){
172890 yymsp[-1].minor.yy285 = TF_WithoutRowid | TF_NoVisibleRowid;
172891 }else{
172892 yymsp[-1].minor.yy285 = 0;
172893 sqlite3ErrorMsg(pParse, zFormat: "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
172894 }
172895}
172896 break;
172897 case 24: /* table_option ::= nm */
172898{
172899 if( yymsp[0].minor.yy0.n==6 && sqlite3_strnicmp(zLeft: yymsp[0].minor.yy0.z,zRight: "strict",N: 6)==0 ){
172900 yylhsminor.yy285 = TF_Strict;
172901 }else{
172902 yylhsminor.yy285 = 0;
172903 sqlite3ErrorMsg(pParse, zFormat: "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
172904 }
172905}
172906 yymsp[0].minor.yy285 = yylhsminor.yy285;
172907 break;
172908 case 25: /* columnname ::= nm typetoken */
172909{sqlite3AddColumn(pParse,sName: yymsp[-1].minor.yy0,sType: yymsp[0].minor.yy0);}
172910 break;
172911 case 26: /* typetoken ::= */
172912 case 65: /* conslist_opt ::= */ yytestcase(yyruleno==65);
172913 case 104: /* as ::= */ yytestcase(yyruleno==104);
172914{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
172915 break;
172916 case 27: /* typetoken ::= typename LP signed RP */
172917{
172918 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
172919}
172920 break;
172921 case 28: /* typetoken ::= typename LP signed COMMA signed RP */
172922{
172923 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
172924}
172925 break;
172926 case 29: /* typename ::= typename ID|STRING */
172927{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
172928 break;
172929 case 30: /* scanpt ::= */
172930{
172931 assert( yyLookahead!=YYNOCODE );
172932 yymsp[1].minor.yy522 = yyLookaheadToken.z;
172933}
172934 break;
172935 case 31: /* scantok ::= */
172936{
172937 assert( yyLookahead!=YYNOCODE );
172938 yymsp[1].minor.yy0 = yyLookaheadToken;
172939}
172940 break;
172941 case 32: /* ccons ::= CONSTRAINT nm */
172942 case 67: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==67);
172943{pParse->constraintName = yymsp[0].minor.yy0;}
172944 break;
172945 case 33: /* ccons ::= DEFAULT scantok term */
172946{sqlite3AddDefaultValue(pParse,pExpr: yymsp[0].minor.yy528,zStart: yymsp[-1].minor.yy0.z,zEnd: &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
172947 break;
172948 case 34: /* ccons ::= DEFAULT LP expr RP */
172949{sqlite3AddDefaultValue(pParse,pExpr: yymsp[-1].minor.yy528,zStart: yymsp[-2].minor.yy0.z+1,zEnd: yymsp[0].minor.yy0.z);}
172950 break;
172951 case 35: /* ccons ::= DEFAULT PLUS scantok term */
172952{sqlite3AddDefaultValue(pParse,pExpr: yymsp[0].minor.yy528,zStart: yymsp[-2].minor.yy0.z,zEnd: &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
172953 break;
172954 case 36: /* ccons ::= DEFAULT MINUS scantok term */
172955{
172956 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, pLeft: yymsp[0].minor.yy528, pRight: 0);
172957 sqlite3AddDefaultValue(pParse,pExpr: p,zStart: yymsp[-2].minor.yy0.z,zEnd: &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
172958}
172959 break;
172960 case 37: /* ccons ::= DEFAULT scantok ID|INDEXED */
172961{
172962 Expr *p = tokenExpr(pParse, TK_STRING, t: yymsp[0].minor.yy0);
172963 if( p ){
172964 sqlite3ExprIdToTrueFalse(pExpr: p);
172965 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
172966 }
172967 sqlite3AddDefaultValue(pParse,pExpr: p,zStart: yymsp[0].minor.yy0.z,zEnd: yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
172968}
172969 break;
172970 case 38: /* ccons ::= NOT NULL onconf */
172971{sqlite3AddNotNull(pParse, onError: yymsp[0].minor.yy394);}
172972 break;
172973 case 39: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
172974{sqlite3AddPrimaryKey(pParse,pList: 0,onError: yymsp[-1].minor.yy394,autoInc: yymsp[0].minor.yy394,sortOrder: yymsp[-2].minor.yy394);}
172975 break;
172976 case 40: /* ccons ::= UNIQUE onconf */
172977{sqlite3CreateIndex(pParse,pName1: 0,pName2: 0,pTblName: 0,pList: 0,onError: yymsp[0].minor.yy394,pStart: 0,pPIWhere: 0,sortOrder: 0,ifNotExist: 0,
172978 SQLITE_IDXTYPE_UNIQUE);}
172979 break;
172980 case 41: /* ccons ::= CHECK LP expr RP */
172981{sqlite3AddCheckConstraint(pParse,pCheckExpr: yymsp[-1].minor.yy528,zStart: yymsp[-2].minor.yy0.z,zEnd: yymsp[0].minor.yy0.z);}
172982 break;
172983 case 42: /* ccons ::= REFERENCES nm eidlist_opt refargs */
172984{sqlite3CreateForeignKey(pParse,pFromCol: 0,pTo: &yymsp[-2].minor.yy0,pToCol: yymsp[-1].minor.yy322,flags: yymsp[0].minor.yy394);}
172985 break;
172986 case 43: /* ccons ::= defer_subclause */
172987{sqlite3DeferForeignKey(pParse,isDeferred: yymsp[0].minor.yy394);}
172988 break;
172989 case 44: /* ccons ::= COLLATE ID|STRING */
172990{sqlite3AddCollateType(pParse, pToken: &yymsp[0].minor.yy0);}
172991 break;
172992 case 45: /* generated ::= LP expr RP */
172993{sqlite3AddGenerated(pParse,pExpr: yymsp[-1].minor.yy528,pType: 0);}
172994 break;
172995 case 46: /* generated ::= LP expr RP ID */
172996{sqlite3AddGenerated(pParse,pExpr: yymsp[-2].minor.yy528,pType: &yymsp[0].minor.yy0);}
172997 break;
172998 case 48: /* autoinc ::= AUTOINCR */
172999{yymsp[0].minor.yy394 = 1;}
173000 break;
173001 case 49: /* refargs ::= */
173002{ yymsp[1].minor.yy394 = OE_None*0x0101; /* EV: R-19803-45884 */}
173003 break;
173004 case 50: /* refargs ::= refargs refarg */
173005{ yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy231.value; }
173006 break;
173007 case 51: /* refarg ::= MATCH nm */
173008{ yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
173009 break;
173010 case 52: /* refarg ::= ON INSERT refact */
173011{ yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
173012 break;
173013 case 53: /* refarg ::= ON DELETE refact */
173014{ yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
173015 break;
173016 case 54: /* refarg ::= ON UPDATE refact */
173017{ yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
173018 break;
173019 case 55: /* refact ::= SET NULL */
173020{ yymsp[-1].minor.yy394 = OE_SetNull; /* EV: R-33326-45252 */}
173021 break;
173022 case 56: /* refact ::= SET DEFAULT */
173023{ yymsp[-1].minor.yy394 = OE_SetDflt; /* EV: R-33326-45252 */}
173024 break;
173025 case 57: /* refact ::= CASCADE */
173026{ yymsp[0].minor.yy394 = OE_Cascade; /* EV: R-33326-45252 */}
173027 break;
173028 case 58: /* refact ::= RESTRICT */
173029{ yymsp[0].minor.yy394 = OE_Restrict; /* EV: R-33326-45252 */}
173030 break;
173031 case 59: /* refact ::= NO ACTION */
173032{ yymsp[-1].minor.yy394 = OE_None; /* EV: R-33326-45252 */}
173033 break;
173034 case 60: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
173035{yymsp[-2].minor.yy394 = 0;}
173036 break;
173037 case 61: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
173038 case 76: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==76);
173039 case 171: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==171);
173040{yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
173041 break;
173042 case 63: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
173043 case 80: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==80);
173044 case 215: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==215);
173045 case 218: /* in_op ::= NOT IN */ yytestcase(yyruleno==218);
173046 case 243: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==243);
173047{yymsp[-1].minor.yy394 = 1;}
173048 break;
173049 case 64: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
173050{yymsp[-1].minor.yy394 = 0;}
173051 break;
173052 case 66: /* tconscomma ::= COMMA */
173053{pParse->constraintName.n = 0;}
173054 break;
173055 case 68: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
173056{sqlite3AddPrimaryKey(pParse,pList: yymsp[-3].minor.yy322,onError: yymsp[0].minor.yy394,autoInc: yymsp[-2].minor.yy394,sortOrder: 0);}
173057 break;
173058 case 69: /* tcons ::= UNIQUE LP sortlist RP onconf */
173059{sqlite3CreateIndex(pParse,pName1: 0,pName2: 0,pTblName: 0,pList: yymsp[-2].minor.yy322,onError: yymsp[0].minor.yy394,pStart: 0,pPIWhere: 0,sortOrder: 0,ifNotExist: 0,
173060 SQLITE_IDXTYPE_UNIQUE);}
173061 break;
173062 case 70: /* tcons ::= CHECK LP expr RP onconf */
173063{sqlite3AddCheckConstraint(pParse,pCheckExpr: yymsp[-2].minor.yy528,zStart: yymsp[-3].minor.yy0.z,zEnd: yymsp[-1].minor.yy0.z);}
173064 break;
173065 case 71: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
173066{
173067 sqlite3CreateForeignKey(pParse, pFromCol: yymsp[-6].minor.yy322, pTo: &yymsp[-3].minor.yy0, pToCol: yymsp[-2].minor.yy322, flags: yymsp[-1].minor.yy394);
173068 sqlite3DeferForeignKey(pParse, isDeferred: yymsp[0].minor.yy394);
173069}
173070 break;
173071 case 73: /* onconf ::= */
173072 case 75: /* orconf ::= */ yytestcase(yyruleno==75);
173073{yymsp[1].minor.yy394 = OE_Default;}
173074 break;
173075 case 74: /* onconf ::= ON CONFLICT resolvetype */
173076{yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
173077 break;
173078 case 77: /* resolvetype ::= IGNORE */
173079{yymsp[0].minor.yy394 = OE_Ignore;}
173080 break;
173081 case 78: /* resolvetype ::= REPLACE */
173082 case 172: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==172);
173083{yymsp[0].minor.yy394 = OE_Replace;}
173084 break;
173085 case 79: /* cmd ::= DROP TABLE ifexists fullname */
173086{
173087 sqlite3DropTable(pParse, pName: yymsp[0].minor.yy131, isView: 0, noErr: yymsp[-1].minor.yy394);
173088}
173089 break;
173090 case 82: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
173091{
173092 sqlite3CreateView(pParse, pBegin: &yymsp[-8].minor.yy0, pName1: &yymsp[-4].minor.yy0, pName2: &yymsp[-3].minor.yy0, pCNames: yymsp[-2].minor.yy322, pSelect: yymsp[0].minor.yy47, isTemp: yymsp[-7].minor.yy394, noErr: yymsp[-5].minor.yy394);
173093}
173094 break;
173095 case 83: /* cmd ::= DROP VIEW ifexists fullname */
173096{
173097 sqlite3DropTable(pParse, pName: yymsp[0].minor.yy131, isView: 1, noErr: yymsp[-1].minor.yy394);
173098}
173099 break;
173100 case 84: /* cmd ::= select */
173101{
173102 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
173103 sqlite3Select(pParse, p: yymsp[0].minor.yy47, pDest: &dest);
173104 sqlite3SelectDelete(db: pParse->db, p: yymsp[0].minor.yy47);
173105}
173106 break;
173107 case 85: /* select ::= WITH wqlist selectnowith */
173108{yymsp[-2].minor.yy47 = attachWithToSelect(pParse,pSelect: yymsp[0].minor.yy47,pWith: yymsp[-1].minor.yy521);}
173109 break;
173110 case 86: /* select ::= WITH RECURSIVE wqlist selectnowith */
173111{yymsp[-3].minor.yy47 = attachWithToSelect(pParse,pSelect: yymsp[0].minor.yy47,pWith: yymsp[-1].minor.yy521);}
173112 break;
173113 case 87: /* select ::= selectnowith */
173114{
173115 Select *p = yymsp[0].minor.yy47;
173116 if( p ){
173117 parserDoubleLinkSelect(pParse, p);
173118 }
173119}
173120 break;
173121 case 88: /* selectnowith ::= selectnowith multiselect_op oneselect */
173122{
173123 Select *pRhs = yymsp[0].minor.yy47;
173124 Select *pLhs = yymsp[-2].minor.yy47;
173125 if( pRhs && pRhs->pPrior ){
173126 SrcList *pFrom;
173127 Token x;
173128 x.n = 0;
173129 parserDoubleLinkSelect(pParse, p: pRhs);
173130 pFrom = sqlite3SrcListAppendFromTerm(pParse,p: 0,pTable: 0,pDatabase: 0,pAlias: &x,pSubquery: pRhs,pOnUsing: 0);
173131 pRhs = sqlite3SelectNew(pParse,pEList: 0,pSrc: pFrom,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,selFlags: 0,pLimit: 0);
173132 }
173133 if( pRhs ){
173134 pRhs->op = (u8)yymsp[-1].minor.yy394;
173135 pRhs->pPrior = pLhs;
173136 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
173137 pRhs->selFlags &= ~SF_MultiValue;
173138 if( yymsp[-1].minor.yy394!=TK_ALL ) pParse->hasCompound = 1;
173139 }else{
173140 sqlite3SelectDelete(db: pParse->db, p: pLhs);
173141 }
173142 yymsp[-2].minor.yy47 = pRhs;
173143}
173144 break;
173145 case 89: /* multiselect_op ::= UNION */
173146 case 91: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==91);
173147{yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
173148 break;
173149 case 90: /* multiselect_op ::= UNION ALL */
173150{yymsp[-1].minor.yy394 = TK_ALL;}
173151 break;
173152 case 92: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
173153{
173154 yymsp[-8].minor.yy47 = sqlite3SelectNew(pParse,pEList: yymsp[-6].minor.yy322,pSrc: yymsp[-5].minor.yy131,pWhere: yymsp[-4].minor.yy528,pGroupBy: yymsp[-3].minor.yy322,pHaving: yymsp[-2].minor.yy528,pOrderBy: yymsp[-1].minor.yy322,selFlags: yymsp[-7].minor.yy394,pLimit: yymsp[0].minor.yy528);
173155}
173156 break;
173157 case 93: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
173158{
173159 yymsp[-9].minor.yy47 = sqlite3SelectNew(pParse,pEList: yymsp[-7].minor.yy322,pSrc: yymsp[-6].minor.yy131,pWhere: yymsp[-5].minor.yy528,pGroupBy: yymsp[-4].minor.yy322,pHaving: yymsp[-3].minor.yy528,pOrderBy: yymsp[-1].minor.yy322,selFlags: yymsp[-8].minor.yy394,pLimit: yymsp[0].minor.yy528);
173160 if( yymsp[-9].minor.yy47 ){
173161 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
173162 }else{
173163 sqlite3WindowListDelete(db: pParse->db, p: yymsp[-2].minor.yy41);
173164 }
173165}
173166 break;
173167 case 94: /* values ::= VALUES LP nexprlist RP */
173168{
173169 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,pEList: yymsp[-1].minor.yy322,pSrc: 0,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_Values,pLimit: 0);
173170}
173171 break;
173172 case 95: /* values ::= values COMMA LP nexprlist RP */
173173{
173174 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
173175 pRight = sqlite3SelectNew(pParse,pEList: yymsp[-1].minor.yy322,pSrc: 0,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_Values|SF_MultiValue,pLimit: 0);
173176 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
173177 if( pRight ){
173178 pRight->op = TK_ALL;
173179 pRight->pPrior = pLeft;
173180 yymsp[-4].minor.yy47 = pRight;
173181 }else{
173182 yymsp[-4].minor.yy47 = pLeft;
173183 }
173184}
173185 break;
173186 case 96: /* distinct ::= DISTINCT */
173187{yymsp[0].minor.yy394 = SF_Distinct;}
173188 break;
173189 case 97: /* distinct ::= ALL */
173190{yymsp[0].minor.yy394 = SF_All;}
173191 break;
173192 case 99: /* sclp ::= */
173193 case 132: /* orderby_opt ::= */ yytestcase(yyruleno==132);
173194 case 142: /* groupby_opt ::= */ yytestcase(yyruleno==142);
173195 case 230: /* exprlist ::= */ yytestcase(yyruleno==230);
173196 case 233: /* paren_exprlist ::= */ yytestcase(yyruleno==233);
173197 case 238: /* eidlist_opt ::= */ yytestcase(yyruleno==238);
173198{yymsp[1].minor.yy322 = 0;}
173199 break;
173200 case 100: /* selcollist ::= sclp scanpt expr scanpt as */
173201{
173202 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, pList: yymsp[-4].minor.yy322, pExpr: yymsp[-2].minor.yy528);
173203 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, pList: yymsp[-4].minor.yy322, pName: &yymsp[0].minor.yy0, dequote: 1);
173204 sqlite3ExprListSetSpan(pParse,pList: yymsp[-4].minor.yy322,zStart: yymsp[-3].minor.yy522,zEnd: yymsp[-1].minor.yy522);
173205}
173206 break;
173207 case 101: /* selcollist ::= sclp scanpt STAR */
173208{
173209 Expr *p = sqlite3Expr(db: pParse->db, TK_ASTERISK, zToken: 0);
173210 sqlite3ExprSetErrorOffset(pExpr: p, iOfst: (int)(yymsp[0].minor.yy0.z - pParse->zTail));
173211 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, pList: yymsp[-2].minor.yy322, pExpr: p);
173212}
173213 break;
173214 case 102: /* selcollist ::= sclp scanpt nm DOT STAR */
173215{
173216 Expr *pRight, *pLeft, *pDot;
173217 pRight = sqlite3PExpr(pParse, TK_ASTERISK, pLeft: 0, pRight: 0);
173218 sqlite3ExprSetErrorOffset(pExpr: pRight, iOfst: (int)(yymsp[0].minor.yy0.z - pParse->zTail));
173219 pLeft = tokenExpr(pParse, TK_ID, t: yymsp[-2].minor.yy0);
173220 pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
173221 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy322, pExpr: pDot);
173222}
173223 break;
173224 case 103: /* as ::= AS nm */
173225 case 115: /* dbnm ::= DOT nm */ yytestcase(yyruleno==115);
173226 case 254: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==254);
173227 case 255: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==255);
173228{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
173229 break;
173230 case 105: /* from ::= */
173231 case 108: /* stl_prefix ::= */ yytestcase(yyruleno==108);
173232{yymsp[1].minor.yy131 = 0;}
173233 break;
173234 case 106: /* from ::= FROM seltablist */
173235{
173236 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
173237 sqlite3SrcListShiftJoinType(pParse,p: yymsp[-1].minor.yy131);
173238}
173239 break;
173240 case 107: /* stl_prefix ::= seltablist joinop */
173241{
173242 if( ALWAYS(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;
173243}
173244 break;
173245 case 109: /* seltablist ::= stl_prefix nm dbnm as on_using */
173246{
173247 yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-4].minor.yy131,pTable: &yymsp[-3].minor.yy0,pDatabase: &yymsp[-2].minor.yy0,pAlias: &yymsp[-1].minor.yy0,pSubquery: 0,pOnUsing: &yymsp[0].minor.yy561);
173248}
173249 break;
173250 case 110: /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
173251{
173252 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-5].minor.yy131,pTable: &yymsp[-4].minor.yy0,pDatabase: &yymsp[-3].minor.yy0,pAlias: &yymsp[-2].minor.yy0,pSubquery: 0,pOnUsing: &yymsp[0].minor.yy561);
173253 sqlite3SrcListIndexedBy(pParse, p: yymsp[-5].minor.yy131, pIndexedBy: &yymsp[-1].minor.yy0);
173254}
173255 break;
173256 case 111: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
173257{
173258 yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-7].minor.yy131,pTable: &yymsp[-6].minor.yy0,pDatabase: &yymsp[-5].minor.yy0,pAlias: &yymsp[-1].minor.yy0,pSubquery: 0,pOnUsing: &yymsp[0].minor.yy561);
173259 sqlite3SrcListFuncArgs(pParse, p: yymsp[-7].minor.yy131, pList: yymsp[-3].minor.yy322);
173260}
173261 break;
173262 case 112: /* seltablist ::= stl_prefix LP select RP as on_using */
173263{
173264 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-5].minor.yy131,pTable: 0,pDatabase: 0,pAlias: &yymsp[-1].minor.yy0,pSubquery: yymsp[-3].minor.yy47,pOnUsing: &yymsp[0].minor.yy561);
173265 }
173266 break;
173267 case 113: /* seltablist ::= stl_prefix LP seltablist RP as on_using */
173268{
173269 if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0].minor.yy561.pUsing==0 ){
173270 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
173271 }else if( ALWAYS(yymsp[-3].minor.yy131!=0) && yymsp[-3].minor.yy131->nSrc==1 ){
173272 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-5].minor.yy131,pTable: 0,pDatabase: 0,pAlias: &yymsp[-1].minor.yy0,pSubquery: 0,pOnUsing: &yymsp[0].minor.yy561);
173273 if( yymsp[-5].minor.yy131 ){
173274 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
173275 SrcItem *pOld = yymsp[-3].minor.yy131->a;
173276 pNew->zName = pOld->zName;
173277 pNew->zDatabase = pOld->zDatabase;
173278 pNew->pSelect = pOld->pSelect;
173279 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
173280 pNew->fg.isNestedFrom = 1;
173281 }
173282 if( pOld->fg.isTabFunc ){
173283 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
173284 pOld->u1.pFuncArg = 0;
173285 pOld->fg.isTabFunc = 0;
173286 pNew->fg.isTabFunc = 1;
173287 }
173288 pOld->zName = pOld->zDatabase = 0;
173289 pOld->pSelect = 0;
173290 }
173291 sqlite3SrcListDelete(db: pParse->db, pList: yymsp[-3].minor.yy131);
173292 }else{
173293 Select *pSubquery;
173294 sqlite3SrcListShiftJoinType(pParse,p: yymsp[-3].minor.yy131);
173295 pSubquery = sqlite3SelectNew(pParse,pEList: 0,pSrc: yymsp[-3].minor.yy131,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_NestedFrom,pLimit: 0);
173296 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-5].minor.yy131,pTable: 0,pDatabase: 0,pAlias: &yymsp[-1].minor.yy0,pSubquery,pOnUsing: &yymsp[0].minor.yy561);
173297 }
173298 }
173299 break;
173300 case 114: /* dbnm ::= */
173301 case 129: /* indexed_opt ::= */ yytestcase(yyruleno==129);
173302{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
173303 break;
173304 case 116: /* fullname ::= nm */
173305{
173306 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[0].minor.yy0,pDatabase: 0);
173307 if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, pPtr: yylhsminor.yy131->a[0].zName, pToken: &yymsp[0].minor.yy0);
173308}
173309 yymsp[0].minor.yy131 = yylhsminor.yy131;
173310 break;
173311 case 117: /* fullname ::= nm DOT nm */
173312{
173313 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: &yymsp[0].minor.yy0);
173314 if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, pPtr: yylhsminor.yy131->a[0].zName, pToken: &yymsp[0].minor.yy0);
173315}
173316 yymsp[-2].minor.yy131 = yylhsminor.yy131;
173317 break;
173318 case 118: /* xfullname ::= nm */
173319{yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[0].minor.yy0,pDatabase: 0); /*A-overwrites-X*/}
173320 break;
173321 case 119: /* xfullname ::= nm DOT nm */
173322{yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: &yymsp[0].minor.yy0); /*A-overwrites-X*/}
173323 break;
173324 case 120: /* xfullname ::= nm DOT nm AS nm */
173325{
173326 yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-4].minor.yy0,pDatabase: &yymsp[-2].minor.yy0); /*A-overwrites-X*/
173327 if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(db: pParse->db, pName: &yymsp[0].minor.yy0);
173328}
173329 break;
173330 case 121: /* xfullname ::= nm AS nm */
173331{
173332 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: 0); /*A-overwrites-X*/
173333 if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(db: pParse->db, pName: &yymsp[0].minor.yy0);
173334}
173335 break;
173336 case 122: /* joinop ::= COMMA|JOIN */
173337{ yymsp[0].minor.yy394 = JT_INNER; }
173338 break;
173339 case 123: /* joinop ::= JOIN_KW JOIN */
173340{yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,pA: &yymsp[-1].minor.yy0,pB: 0,pC: 0); /*X-overwrites-A*/}
173341 break;
173342 case 124: /* joinop ::= JOIN_KW nm JOIN */
173343{yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,pA: &yymsp[-2].minor.yy0,pB: &yymsp[-1].minor.yy0,pC: 0); /*X-overwrites-A*/}
173344 break;
173345 case 125: /* joinop ::= JOIN_KW nm nm JOIN */
173346{yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,pA: &yymsp[-3].minor.yy0,pB: &yymsp[-2].minor.yy0,pC: &yymsp[-1].minor.yy0);/*X-overwrites-A*/}
173347 break;
173348 case 126: /* on_using ::= ON expr */
173349{yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
173350 break;
173351 case 127: /* on_using ::= USING LP idlist RP */
173352{yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
173353 break;
173354 case 128: /* on_using ::= */
173355{yymsp[1].minor.yy561.pOn = 0; yymsp[1].minor.yy561.pUsing = 0;}
173356 break;
173357 case 130: /* indexed_by ::= INDEXED BY nm */
173358{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
173359 break;
173360 case 131: /* indexed_by ::= NOT INDEXED */
173361{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
173362 break;
173363 case 133: /* orderby_opt ::= ORDER BY sortlist */
173364 case 143: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==143);
173365{yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
173366 break;
173367 case 134: /* sortlist ::= sortlist COMMA expr sortorder nulls */
173368{
173369 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy322,pExpr: yymsp[-2].minor.yy528);
173370 sqlite3ExprListSetSortOrder(p: yymsp[-4].minor.yy322,iSortOrder: yymsp[-1].minor.yy394,eNulls: yymsp[0].minor.yy394);
173371}
173372 break;
173373 case 135: /* sortlist ::= expr sortorder nulls */
173374{
173375 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,pList: 0,pExpr: yymsp[-2].minor.yy528); /*A-overwrites-Y*/
173376 sqlite3ExprListSetSortOrder(p: yymsp[-2].minor.yy322,iSortOrder: yymsp[-1].minor.yy394,eNulls: yymsp[0].minor.yy394);
173377}
173378 break;
173379 case 136: /* sortorder ::= ASC */
173380{yymsp[0].minor.yy394 = SQLITE_SO_ASC;}
173381 break;
173382 case 137: /* sortorder ::= DESC */
173383{yymsp[0].minor.yy394 = SQLITE_SO_DESC;}
173384 break;
173385 case 138: /* sortorder ::= */
173386 case 141: /* nulls ::= */ yytestcase(yyruleno==141);
173387{yymsp[1].minor.yy394 = SQLITE_SO_UNDEFINED;}
173388 break;
173389 case 139: /* nulls ::= NULLS FIRST */
173390{yymsp[-1].minor.yy394 = SQLITE_SO_ASC;}
173391 break;
173392 case 140: /* nulls ::= NULLS LAST */
173393{yymsp[-1].minor.yy394 = SQLITE_SO_DESC;}
173394 break;
173395 case 144: /* having_opt ::= */
173396 case 146: /* limit_opt ::= */ yytestcase(yyruleno==146);
173397 case 151: /* where_opt ::= */ yytestcase(yyruleno==151);
173398 case 153: /* where_opt_ret ::= */ yytestcase(yyruleno==153);
173399 case 228: /* case_else ::= */ yytestcase(yyruleno==228);
173400 case 229: /* case_operand ::= */ yytestcase(yyruleno==229);
173401 case 248: /* vinto ::= */ yytestcase(yyruleno==248);
173402{yymsp[1].minor.yy528 = 0;}
173403 break;
173404 case 145: /* having_opt ::= HAVING expr */
173405 case 152: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==152);
173406 case 154: /* where_opt_ret ::= WHERE expr */ yytestcase(yyruleno==154);
173407 case 227: /* case_else ::= ELSE expr */ yytestcase(yyruleno==227);
173408 case 247: /* vinto ::= INTO expr */ yytestcase(yyruleno==247);
173409{yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
173410 break;
173411 case 147: /* limit_opt ::= LIMIT expr */
173412{yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,pLeft: yymsp[0].minor.yy528,pRight: 0);}
173413 break;
173414 case 148: /* limit_opt ::= LIMIT expr OFFSET expr */
173415{yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,pLeft: yymsp[-2].minor.yy528,pRight: yymsp[0].minor.yy528);}
173416 break;
173417 case 149: /* limit_opt ::= LIMIT expr COMMA expr */
173418{yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,pLeft: yymsp[0].minor.yy528,pRight: yymsp[-2].minor.yy528);}
173419 break;
173420 case 150: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
173421{
173422 sqlite3SrcListIndexedBy(pParse, p: yymsp[-2].minor.yy131, pIndexedBy: &yymsp[-1].minor.yy0);
173423 sqlite3DeleteFrom(pParse,pTabList: yymsp[-2].minor.yy131,pWhere: yymsp[0].minor.yy528,pOrderBy: 0,pLimit: 0);
173424}
173425 break;
173426 case 155: /* where_opt_ret ::= RETURNING selcollist */
173427{sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
173428 break;
173429 case 156: /* where_opt_ret ::= WHERE expr RETURNING selcollist */
173430{sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
173431 break;
173432 case 157: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
173433{
173434 sqlite3SrcListIndexedBy(pParse, p: yymsp[-5].minor.yy131, pIndexedBy: &yymsp[-4].minor.yy0);
173435 sqlite3ExprListCheckLength(pParse,pEList: yymsp[-2].minor.yy322,zObject: "set list");
173436 if( yymsp[-1].minor.yy131 ){
173437 SrcList *pFromClause = yymsp[-1].minor.yy131;
173438 if( pFromClause->nSrc>1 ){
173439 Select *pSubquery;
173440 Token as;
173441 pSubquery = sqlite3SelectNew(pParse,pEList: 0,pSrc: pFromClause,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_NestedFrom,pLimit: 0);
173442 as.n = 0;
173443 as.z = 0;
173444 pFromClause = sqlite3SrcListAppendFromTerm(pParse,p: 0,pTable: 0,pDatabase: 0,pAlias: &as,pSubquery,pOnUsing: 0);
173445 }
173446 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, p1: yymsp[-5].minor.yy131, p2: pFromClause);
173447 }
173448 sqlite3Update(pParse,pTabList: yymsp[-5].minor.yy131,pChanges: yymsp[-2].minor.yy322,pWhere: yymsp[0].minor.yy528,onError: yymsp[-6].minor.yy394,pOrderBy: 0,pLimit: 0,pUpsert: 0);
173449}
173450 break;
173451 case 158: /* setlist ::= setlist COMMA nm EQ expr */
173452{
173453 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, pList: yymsp[-4].minor.yy322, pExpr: yymsp[0].minor.yy528);
173454 sqlite3ExprListSetName(pParse, pList: yymsp[-4].minor.yy322, pName: &yymsp[-2].minor.yy0, dequote: 1);
173455}
173456 break;
173457 case 159: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
173458{
173459 yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, pList: yymsp[-6].minor.yy322, pColumns: yymsp[-3].minor.yy254, pExpr: yymsp[0].minor.yy528);
173460}
173461 break;
173462 case 160: /* setlist ::= nm EQ expr */
173463{
173464 yylhsminor.yy322 = sqlite3ExprListAppend(pParse, pList: 0, pExpr: yymsp[0].minor.yy528);
173465 sqlite3ExprListSetName(pParse, pList: yylhsminor.yy322, pName: &yymsp[-2].minor.yy0, dequote: 1);
173466}
173467 yymsp[-2].minor.yy322 = yylhsminor.yy322;
173468 break;
173469 case 161: /* setlist ::= LP idlist RP EQ expr */
173470{
173471 yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, pList: 0, pColumns: yymsp[-3].minor.yy254, pExpr: yymsp[0].minor.yy528);
173472}
173473 break;
173474 case 162: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
173475{
173476 sqlite3Insert(pParse, pTabList: yymsp[-3].minor.yy131, pSelect: yymsp[-1].minor.yy47, pColumn: yymsp[-2].minor.yy254, onError: yymsp[-5].minor.yy394, pUpsert: yymsp[0].minor.yy444);
173477}
173478 break;
173479 case 163: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
173480{
173481 sqlite3Insert(pParse, pTabList: yymsp[-4].minor.yy131, pSelect: 0, pColumn: yymsp[-3].minor.yy254, onError: yymsp[-6].minor.yy394, pUpsert: 0);
173482}
173483 break;
173484 case 164: /* upsert ::= */
173485{ yymsp[1].minor.yy444 = 0; }
173486 break;
173487 case 165: /* upsert ::= RETURNING selcollist */
173488{ yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy322); }
173489 break;
173490 case 166: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
173491{ yymsp[-11].minor.yy444 = sqlite3UpsertNew(db: pParse->db,pTarget: yymsp[-8].minor.yy322,pTargetWhere: yymsp[-6].minor.yy528,pSet: yymsp[-2].minor.yy322,pWhere: yymsp[-1].minor.yy528,pNext: yymsp[0].minor.yy444);}
173492 break;
173493 case 167: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
173494{ yymsp[-8].minor.yy444 = sqlite3UpsertNew(db: pParse->db,pTarget: yymsp[-5].minor.yy322,pTargetWhere: yymsp[-3].minor.yy528,pSet: 0,pWhere: 0,pNext: yymsp[0].minor.yy444); }
173495 break;
173496 case 168: /* upsert ::= ON CONFLICT DO NOTHING returning */
173497{ yymsp[-4].minor.yy444 = sqlite3UpsertNew(db: pParse->db,pTarget: 0,pTargetWhere: 0,pSet: 0,pWhere: 0,pNext: 0); }
173498 break;
173499 case 169: /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
173500{ yymsp[-7].minor.yy444 = sqlite3UpsertNew(db: pParse->db,pTarget: 0,pTargetWhere: 0,pSet: yymsp[-2].minor.yy322,pWhere: yymsp[-1].minor.yy528,pNext: 0);}
173501 break;
173502 case 170: /* returning ::= RETURNING selcollist */
173503{sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy322);}
173504 break;
173505 case 173: /* idlist_opt ::= */
173506{yymsp[1].minor.yy254 = 0;}
173507 break;
173508 case 174: /* idlist_opt ::= LP idlist RP */
173509{yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
173510 break;
173511 case 175: /* idlist ::= idlist COMMA nm */
173512{yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,pList: yymsp[-2].minor.yy254,pToken: &yymsp[0].minor.yy0);}
173513 break;
173514 case 176: /* idlist ::= nm */
173515{yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,pList: 0,pToken: &yymsp[0].minor.yy0); /*A-overwrites-Y*/}
173516 break;
173517 case 177: /* expr ::= LP expr RP */
173518{yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
173519 break;
173520 case 178: /* expr ::= ID|INDEXED|JOIN_KW */
173521{yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,t: yymsp[0].minor.yy0); /*A-overwrites-X*/}
173522 break;
173523 case 179: /* expr ::= nm DOT nm */
173524{
173525 Expr *temp1 = tokenExpr(pParse,TK_ID,t: yymsp[-2].minor.yy0);
173526 Expr *temp2 = tokenExpr(pParse,TK_ID,t: yymsp[0].minor.yy0);
173527 yylhsminor.yy528 = sqlite3PExpr(pParse, TK_DOT, pLeft: temp1, pRight: temp2);
173528}
173529 yymsp[-2].minor.yy528 = yylhsminor.yy528;
173530 break;
173531 case 180: /* expr ::= nm DOT nm DOT nm */
173532{
173533 Expr *temp1 = tokenExpr(pParse,TK_ID,t: yymsp[-4].minor.yy0);
173534 Expr *temp2 = tokenExpr(pParse,TK_ID,t: yymsp[-2].minor.yy0);
173535 Expr *temp3 = tokenExpr(pParse,TK_ID,t: yymsp[0].minor.yy0);
173536 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, pLeft: temp2, pRight: temp3);
173537 if( IN_RENAME_OBJECT ){
173538 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: temp1);
173539 }
173540 yylhsminor.yy528 = sqlite3PExpr(pParse, TK_DOT, pLeft: temp1, pRight: temp4);
173541}
173542 yymsp[-4].minor.yy528 = yylhsminor.yy528;
173543 break;
173544 case 181: /* term ::= NULL|FLOAT|BLOB */
173545 case 182: /* term ::= STRING */ yytestcase(yyruleno==182);
173546{yymsp[0].minor.yy528=tokenExpr(pParse,op: yymsp[0].major,t: yymsp[0].minor.yy0); /*A-overwrites-X*/}
173547 break;
173548 case 183: /* term ::= INTEGER */
173549{
173550 yylhsminor.yy528 = sqlite3ExprAlloc(db: pParse->db, TK_INTEGER, pToken: &yymsp[0].minor.yy0, dequote: 1);
173551 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
173552}
173553 yymsp[0].minor.yy528 = yylhsminor.yy528;
173554 break;
173555 case 184: /* expr ::= VARIABLE */
173556{
173557 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
173558 u32 n = yymsp[0].minor.yy0.n;
173559 yymsp[0].minor.yy528 = tokenExpr(pParse, TK_VARIABLE, t: yymsp[0].minor.yy0);
173560 sqlite3ExprAssignVarNumber(pParse, pExpr: yymsp[0].minor.yy528, n);
173561 }else{
173562 /* When doing a nested parse, one can include terms in an expression
173563 ** that look like this: #1 #2 ... These terms refer to registers
173564 ** in the virtual machine. #N is the N-th register. */
173565 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
173566 assert( t.n>=2 );
173567 if( pParse->nested==0 ){
173568 sqlite3ErrorMsg(pParse, zFormat: "near \"%T\": syntax error", &t);
173569 yymsp[0].minor.yy528 = 0;
173570 }else{
173571 yymsp[0].minor.yy528 = sqlite3PExpr(pParse, TK_REGISTER, pLeft: 0, pRight: 0);
173572 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(zNum: &t.z[1], pValue: &yymsp[0].minor.yy528->iTable);
173573 }
173574 }
173575}
173576 break;
173577 case 185: /* expr ::= expr COLLATE ID|STRING */
173578{
173579 yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, pExpr: yymsp[-2].minor.yy528, pCollName: &yymsp[0].minor.yy0, dequote: 1);
173580}
173581 break;
173582 case 186: /* expr ::= CAST LP expr AS typetoken RP */
173583{
173584 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(db: pParse->db, TK_CAST, pToken: &yymsp[-1].minor.yy0, dequote: 1);
173585 sqlite3ExprAttachSubtrees(db: pParse->db, pRoot: yymsp[-5].minor.yy528, pLeft: yymsp[-3].minor.yy528, pRight: 0);
173586}
173587 break;
173588 case 187: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
173589{
173590 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList: yymsp[-1].minor.yy322, pToken: &yymsp[-4].minor.yy0, eDistinct: yymsp[-2].minor.yy394);
173591}
173592 yymsp[-4].minor.yy528 = yylhsminor.yy528;
173593 break;
173594 case 188: /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
173595{
173596 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList: 0, pToken: &yymsp[-3].minor.yy0, eDistinct: 0);
173597}
173598 yymsp[-3].minor.yy528 = yylhsminor.yy528;
173599 break;
173600 case 189: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
173601{
173602 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList: yymsp[-2].minor.yy322, pToken: &yymsp[-5].minor.yy0, eDistinct: yymsp[-3].minor.yy394);
173603 sqlite3WindowAttach(pParse, p: yylhsminor.yy528, pWin: yymsp[0].minor.yy41);
173604}
173605 yymsp[-5].minor.yy528 = yylhsminor.yy528;
173606 break;
173607 case 190: /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
173608{
173609 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList: 0, pToken: &yymsp[-4].minor.yy0, eDistinct: 0);
173610 sqlite3WindowAttach(pParse, p: yylhsminor.yy528, pWin: yymsp[0].minor.yy41);
173611}
173612 yymsp[-4].minor.yy528 = yylhsminor.yy528;
173613 break;
173614 case 191: /* term ::= CTIME_KW */
173615{
173616 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList: 0, pToken: &yymsp[0].minor.yy0, eDistinct: 0);
173617}
173618 yymsp[0].minor.yy528 = yylhsminor.yy528;
173619 break;
173620 case 192: /* expr ::= LP nexprlist COMMA expr RP */
173621{
173622 ExprList *pList = sqlite3ExprListAppend(pParse, pList: yymsp[-3].minor.yy322, pExpr: yymsp[-1].minor.yy528);
173623 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR, pLeft: 0, pRight: 0);
173624 if( yymsp[-4].minor.yy528 ){
173625 yymsp[-4].minor.yy528->x.pList = pList;
173626 if( ALWAYS(pList->nExpr) ){
173627 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate;
173628 }
173629 }else{
173630 sqlite3ExprListDelete(db: pParse->db, pList);
173631 }
173632}
173633 break;
173634 case 193: /* expr ::= expr AND expr */
173635{yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,pLeft: yymsp[-2].minor.yy528,pRight: yymsp[0].minor.yy528);}
173636 break;
173637 case 194: /* expr ::= expr OR expr */
173638 case 195: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==195);
173639 case 196: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==196);
173640 case 197: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==197);
173641 case 198: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==198);
173642 case 199: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==199);
173643 case 200: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==200);
173644{yymsp[-2].minor.yy528=sqlite3PExpr(pParse,op: yymsp[-1].major,pLeft: yymsp[-2].minor.yy528,pRight: yymsp[0].minor.yy528);}
173645 break;
173646 case 201: /* likeop ::= NOT LIKE_KW|MATCH */
173647{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
173648 break;
173649 case 202: /* expr ::= expr likeop expr */
173650{
173651 ExprList *pList;
173652 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
173653 yymsp[-1].minor.yy0.n &= 0x7fffffff;
173654 pList = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[0].minor.yy528);
173655 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[-2].minor.yy528);
173656 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, pToken: &yymsp[-1].minor.yy0, eDistinct: 0);
173657 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-2].minor.yy528, pRight: 0);
173658 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc;
173659}
173660 break;
173661 case 203: /* expr ::= expr likeop expr ESCAPE expr */
173662{
173663 ExprList *pList;
173664 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
173665 yymsp[-3].minor.yy0.n &= 0x7fffffff;
173666 pList = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[-2].minor.yy528);
173667 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[-4].minor.yy528);
173668 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[0].minor.yy528);
173669 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, pToken: &yymsp[-3].minor.yy0, eDistinct: 0);
173670 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173671 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc;
173672}
173673 break;
173674 case 204: /* expr ::= expr ISNULL|NOTNULL */
173675{yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,op: yymsp[0].major,pLeft: yymsp[-1].minor.yy528,pRight: 0);}
173676 break;
173677 case 205: /* expr ::= expr NOT NULL */
173678{yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL,pLeft: yymsp[-2].minor.yy528,pRight: 0);}
173679 break;
173680 case 206: /* expr ::= expr IS expr */
173681{
173682 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS,pLeft: yymsp[-2].minor.yy528,pRight: yymsp[0].minor.yy528);
173683 binaryToUnaryIfNull(pParse, pY: yymsp[0].minor.yy528, pA: yymsp[-2].minor.yy528, TK_ISNULL);
173684}
173685 break;
173686 case 207: /* expr ::= expr IS NOT expr */
173687{
173688 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,pLeft: yymsp[-3].minor.yy528,pRight: yymsp[0].minor.yy528);
173689 binaryToUnaryIfNull(pParse, pY: yymsp[0].minor.yy528, pA: yymsp[-3].minor.yy528, TK_NOTNULL);
173690}
173691 break;
173692 case 208: /* expr ::= expr IS NOT DISTINCT FROM expr */
173693{
173694 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS,pLeft: yymsp[-5].minor.yy528,pRight: yymsp[0].minor.yy528);
173695 binaryToUnaryIfNull(pParse, pY: yymsp[0].minor.yy528, pA: yymsp[-5].minor.yy528, TK_ISNULL);
173696}
173697 break;
173698 case 209: /* expr ::= expr IS DISTINCT FROM expr */
173699{
173700 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,pLeft: yymsp[-4].minor.yy528,pRight: yymsp[0].minor.yy528);
173701 binaryToUnaryIfNull(pParse, pY: yymsp[0].minor.yy528, pA: yymsp[-4].minor.yy528, TK_NOTNULL);
173702}
173703 break;
173704 case 210: /* expr ::= NOT expr */
173705 case 211: /* expr ::= BITNOT expr */ yytestcase(yyruleno==211);
173706{yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, op: yymsp[-1].major, pLeft: yymsp[0].minor.yy528, pRight: 0);/*A-overwrites-B*/}
173707 break;
173708 case 212: /* expr ::= PLUS|MINUS expr */
173709{
173710 yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, op: yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, pLeft: yymsp[0].minor.yy528, pRight: 0);
173711 /*A-overwrites-B*/
173712}
173713 break;
173714 case 213: /* expr ::= expr PTR expr */
173715{
173716 ExprList *pList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: yymsp[-2].minor.yy528);
173717 pList = sqlite3ExprListAppend(pParse, pList, pExpr: yymsp[0].minor.yy528);
173718 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, pToken: &yymsp[-1].minor.yy0, eDistinct: 0);
173719}
173720 yymsp[-2].minor.yy528 = yylhsminor.yy528;
173721 break;
173722 case 214: /* between_op ::= BETWEEN */
173723 case 217: /* in_op ::= IN */ yytestcase(yyruleno==217);
173724{yymsp[0].minor.yy394 = 0;}
173725 break;
173726 case 216: /* expr ::= expr between_op expr AND expr */
173727{
173728 ExprList *pList = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[-2].minor.yy528);
173729 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[0].minor.yy528);
173730 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173731 if( yymsp[-4].minor.yy528 ){
173732 yymsp[-4].minor.yy528->x.pList = pList;
173733 }else{
173734 sqlite3ExprListDelete(db: pParse->db, pList);
173735 }
173736 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173737}
173738 break;
173739 case 219: /* expr ::= expr in_op LP exprlist RP */
173740{
173741 if( yymsp[-1].minor.yy322==0 ){
173742 /* Expressions of the form
173743 **
173744 ** expr1 IN ()
173745 ** expr1 NOT IN ()
173746 **
173747 ** simplify to constants 0 (false) and 1 (true), respectively,
173748 ** regardless of the value of expr1.
173749 */
173750 sqlite3ExprUnmapAndDelete(pParse, p: yymsp[-4].minor.yy528);
173751 yymsp[-4].minor.yy528 = sqlite3Expr(db: pParse->db, TK_STRING, zToken: yymsp[-3].minor.yy394 ? "true" : "false");
173752 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(pExpr: yymsp[-4].minor.yy528);
173753 }else{
173754 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
173755 if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(p: pRHS) && yymsp[-4].minor.yy528->op!=TK_VECTOR ){
173756 yymsp[-1].minor.yy322->a[0].pExpr = 0;
173757 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-1].minor.yy322);
173758 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pLeft: pRHS, pRight: 0);
173759 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ, pLeft: yymsp[-4].minor.yy528, pRight: pRHS);
173760 }else if( yymsp[-1].minor.yy322->nExpr==1 && pRHS->op==TK_SELECT ){
173761 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173762 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy528, pSelect: pRHS->x.pSelect);
173763 pRHS->x.pSelect = 0;
173764 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-1].minor.yy322);
173765 }else{
173766 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173767 if( yymsp[-4].minor.yy528==0 ){
173768 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-1].minor.yy322);
173769 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR ){
173770 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
173771 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nElem: nExpr, pEList: yymsp[-1].minor.yy322);
173772 if( pSelectRHS ){
173773 parserDoubleLinkSelect(pParse, p: pSelectRHS);
173774 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy528, pSelect: pSelectRHS);
173775 }
173776 }else{
173777 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
173778 sqlite3ExprSetHeightAndFlags(pParse, p: yymsp[-4].minor.yy528);
173779 }
173780 }
173781 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173782 }
173783 }
173784 break;
173785 case 220: /* expr ::= LP select RP */
173786{
173787 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT, pLeft: 0, pRight: 0);
173788 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-2].minor.yy528, pSelect: yymsp[-1].minor.yy47);
173789 }
173790 break;
173791 case 221: /* expr ::= expr in_op LP select RP */
173792{
173793 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173794 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy528, pSelect: yymsp[-1].minor.yy47);
173795 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173796 }
173797 break;
173798 case 222: /* expr ::= expr in_op nm dbnm paren_exprlist */
173799{
173800 SrcList *pSrc = sqlite3SrcListAppend(pParse, pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: &yymsp[-1].minor.yy0);
173801 Select *pSelect = sqlite3SelectNew(pParse, pEList: 0,pSrc,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,selFlags: 0,pLimit: 0);
173802 if( yymsp[0].minor.yy322 ) sqlite3SrcListFuncArgs(pParse, p: pSelect ? pSrc : 0, pList: yymsp[0].minor.yy322);
173803 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173804 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy528, pSelect);
173805 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy528, pRight: 0);
173806 }
173807 break;
173808 case 223: /* expr ::= EXISTS LP select RP */
173809{
173810 Expr *p;
173811 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS, pLeft: 0, pRight: 0);
173812 sqlite3PExprAddSelect(pParse, pExpr: p, pSelect: yymsp[-1].minor.yy47);
173813 }
173814 break;
173815 case 224: /* expr ::= CASE case_operand case_exprlist case_else END */
173816{
173817 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE, pLeft: yymsp[-3].minor.yy528, pRight: 0);
173818 if( yymsp[-4].minor.yy528 ){
173819 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,pList: yymsp[-2].minor.yy322,pExpr: yymsp[-1].minor.yy528) : yymsp[-2].minor.yy322;
173820 sqlite3ExprSetHeightAndFlags(pParse, p: yymsp[-4].minor.yy528);
173821 }else{
173822 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-2].minor.yy322);
173823 sqlite3ExprDelete(db: pParse->db, p: yymsp[-1].minor.yy528);
173824 }
173825}
173826 break;
173827 case 225: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
173828{
173829 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy322, pExpr: yymsp[-2].minor.yy528);
173830 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy322, pExpr: yymsp[0].minor.yy528);
173831}
173832 break;
173833 case 226: /* case_exprlist ::= WHEN expr THEN expr */
173834{
173835 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[-2].minor.yy528);
173836 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,pList: yymsp[-3].minor.yy322, pExpr: yymsp[0].minor.yy528);
173837}
173838 break;
173839 case 231: /* nexprlist ::= nexprlist COMMA expr */
173840{yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,pList: yymsp[-2].minor.yy322,pExpr: yymsp[0].minor.yy528);}
173841 break;
173842 case 232: /* nexprlist ::= expr */
173843{yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,pList: 0,pExpr: yymsp[0].minor.yy528); /*A-overwrites-Y*/}
173844 break;
173845 case 234: /* paren_exprlist ::= LP exprlist RP */
173846 case 239: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==239);
173847{yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
173848 break;
173849 case 235: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
173850{
173851 sqlite3CreateIndex(pParse, pName1: &yymsp[-7].minor.yy0, pName2: &yymsp[-6].minor.yy0,
173852 pTblName: sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-4].minor.yy0,pDatabase: 0), pList: yymsp[-2].minor.yy322, onError: yymsp[-10].minor.yy394,
173853 pStart: &yymsp[-11].minor.yy0, pPIWhere: yymsp[0].minor.yy528, SQLITE_SO_ASC, ifNotExist: yymsp[-8].minor.yy394, SQLITE_IDXTYPE_APPDEF);
173854 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
173855 sqlite3RenameTokenMap(pParse, pPtr: pParse->pNewIndex->zName, pToken: &yymsp[-4].minor.yy0);
173856 }
173857}
173858 break;
173859 case 236: /* uniqueflag ::= UNIQUE */
173860 case 278: /* raisetype ::= ABORT */ yytestcase(yyruleno==278);
173861{yymsp[0].minor.yy394 = OE_Abort;}
173862 break;
173863 case 237: /* uniqueflag ::= */
173864{yymsp[1].minor.yy394 = OE_None;}
173865 break;
173866 case 240: /* eidlist ::= eidlist COMMA nm collate sortorder */
173867{
173868 yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, pPrior: yymsp[-4].minor.yy322, pIdToken: &yymsp[-2].minor.yy0, hasCollate: yymsp[-1].minor.yy394, sortOrder: yymsp[0].minor.yy394);
173869}
173870 break;
173871 case 241: /* eidlist ::= nm collate sortorder */
173872{
173873 yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, pPrior: 0, pIdToken: &yymsp[-2].minor.yy0, hasCollate: yymsp[-1].minor.yy394, sortOrder: yymsp[0].minor.yy394); /*A-overwrites-Y*/
173874}
173875 break;
173876 case 244: /* cmd ::= DROP INDEX ifexists fullname */
173877{sqlite3DropIndex(pParse, pName: yymsp[0].minor.yy131, ifExists: yymsp[-1].minor.yy394);}
173878 break;
173879 case 245: /* cmd ::= VACUUM vinto */
173880{sqlite3Vacuum(pParse,pNm: 0,pInto: yymsp[0].minor.yy528);}
173881 break;
173882 case 246: /* cmd ::= VACUUM nm vinto */
173883{sqlite3Vacuum(pParse,pNm: &yymsp[-1].minor.yy0,pInto: yymsp[0].minor.yy528);}
173884 break;
173885 case 249: /* cmd ::= PRAGMA nm dbnm */
173886{sqlite3Pragma(pParse,pId1: &yymsp[-1].minor.yy0,pId2: &yymsp[0].minor.yy0,pValue: 0,minusFlag: 0);}
173887 break;
173888 case 250: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
173889{sqlite3Pragma(pParse,pId1: &yymsp[-3].minor.yy0,pId2: &yymsp[-2].minor.yy0,pValue: &yymsp[0].minor.yy0,minusFlag: 0);}
173890 break;
173891 case 251: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
173892{sqlite3Pragma(pParse,pId1: &yymsp[-4].minor.yy0,pId2: &yymsp[-3].minor.yy0,pValue: &yymsp[-1].minor.yy0,minusFlag: 0);}
173893 break;
173894 case 252: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
173895{sqlite3Pragma(pParse,pId1: &yymsp[-3].minor.yy0,pId2: &yymsp[-2].minor.yy0,pValue: &yymsp[0].minor.yy0,minusFlag: 1);}
173896 break;
173897 case 253: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
173898{sqlite3Pragma(pParse,pId1: &yymsp[-4].minor.yy0,pId2: &yymsp[-3].minor.yy0,pValue: &yymsp[-1].minor.yy0,minusFlag: 1);}
173899 break;
173900 case 256: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
173901{
173902 Token all;
173903 all.z = yymsp[-3].minor.yy0.z;
173904 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
173905 sqlite3FinishTrigger(pParse, pStepList: yymsp[-1].minor.yy33, pAll: &all);
173906}
173907 break;
173908 case 257: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
173909{
173910 sqlite3BeginTrigger(pParse, pName1: &yymsp[-7].minor.yy0, pName2: &yymsp[-6].minor.yy0, tr_tm: yymsp[-5].minor.yy394, op: yymsp[-4].minor.yy180.a, pColumns: yymsp[-4].minor.yy180.b, pTableName: yymsp[-2].minor.yy131, pWhen: yymsp[0].minor.yy528, isTemp: yymsp[-10].minor.yy394, noErr: yymsp[-8].minor.yy394);
173911 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
173912}
173913 break;
173914 case 258: /* trigger_time ::= BEFORE|AFTER */
173915{ yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
173916 break;
173917 case 259: /* trigger_time ::= INSTEAD OF */
173918{ yymsp[-1].minor.yy394 = TK_INSTEAD;}
173919 break;
173920 case 260: /* trigger_time ::= */
173921{ yymsp[1].minor.yy394 = TK_BEFORE; }
173922 break;
173923 case 261: /* trigger_event ::= DELETE|INSERT */
173924 case 262: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==262);
173925{yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
173926 break;
173927 case 263: /* trigger_event ::= UPDATE OF idlist */
173928{yymsp[-2].minor.yy180.a = TK_UPDATE; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
173929 break;
173930 case 264: /* when_clause ::= */
173931 case 283: /* key_opt ::= */ yytestcase(yyruleno==283);
173932{ yymsp[1].minor.yy528 = 0; }
173933 break;
173934 case 265: /* when_clause ::= WHEN expr */
173935 case 284: /* key_opt ::= KEY expr */ yytestcase(yyruleno==284);
173936{ yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
173937 break;
173938 case 266: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
173939{
173940 assert( yymsp[-2].minor.yy33!=0 );
173941 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
173942 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
173943}
173944 break;
173945 case 267: /* trigger_cmd_list ::= trigger_cmd SEMI */
173946{
173947 assert( yymsp[-1].minor.yy33!=0 );
173948 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
173949}
173950 break;
173951 case 268: /* trnm ::= nm DOT nm */
173952{
173953 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
173954 sqlite3ErrorMsg(pParse,
173955 zFormat: "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
173956 "statements within triggers");
173957}
173958 break;
173959 case 269: /* tridxby ::= INDEXED BY nm */
173960{
173961 sqlite3ErrorMsg(pParse,
173962 zFormat: "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
173963 "within triggers");
173964}
173965 break;
173966 case 270: /* tridxby ::= NOT INDEXED */
173967{
173968 sqlite3ErrorMsg(pParse,
173969 zFormat: "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
173970 "within triggers");
173971}
173972 break;
173973 case 271: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
173974{yylhsminor.yy33 = sqlite3TriggerUpdateStep(pParse, pTableName: &yymsp[-6].minor.yy0, pFrom: yymsp[-2].minor.yy131, pEList: yymsp[-3].minor.yy322, pWhere: yymsp[-1].minor.yy528, orconf: yymsp[-7].minor.yy394, zStart: yymsp[-8].minor.yy0.z, zEnd: yymsp[0].minor.yy522);}
173975 yymsp[-8].minor.yy33 = yylhsminor.yy33;
173976 break;
173977 case 272: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
173978{
173979 yylhsminor.yy33 = sqlite3TriggerInsertStep(pParse,pTableName: &yymsp[-4].minor.yy0,pColumn: yymsp[-3].minor.yy254,pSelect: yymsp[-2].minor.yy47,orconf: yymsp[-6].minor.yy394,pUpsert: yymsp[-1].minor.yy444,zStart: yymsp[-7].minor.yy522,zEnd: yymsp[0].minor.yy522);/*yylhsminor.yy33-overwrites-yymsp[-6].minor.yy394*/
173980}
173981 yymsp[-7].minor.yy33 = yylhsminor.yy33;
173982 break;
173983 case 273: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
173984{yylhsminor.yy33 = sqlite3TriggerDeleteStep(pParse, pTableName: &yymsp[-3].minor.yy0, pWhere: yymsp[-1].minor.yy528, zStart: yymsp[-5].minor.yy0.z, zEnd: yymsp[0].minor.yy522);}
173985 yymsp[-5].minor.yy33 = yylhsminor.yy33;
173986 break;
173987 case 274: /* trigger_cmd ::= scanpt select scanpt */
173988{yylhsminor.yy33 = sqlite3TriggerSelectStep(db: pParse->db, pSelect: yymsp[-1].minor.yy47, zStart: yymsp[-2].minor.yy522, zEnd: yymsp[0].minor.yy522); /*yylhsminor.yy33-overwrites-yymsp[-1].minor.yy47*/}
173989 yymsp[-2].minor.yy33 = yylhsminor.yy33;
173990 break;
173991 case 275: /* expr ::= RAISE LP IGNORE RP */
173992{
173993 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE, pLeft: 0, pRight: 0);
173994 if( yymsp[-3].minor.yy528 ){
173995 yymsp[-3].minor.yy528->affExpr = OE_Ignore;
173996 }
173997}
173998 break;
173999 case 276: /* expr ::= RAISE LP raisetype COMMA nm RP */
174000{
174001 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(db: pParse->db, TK_RAISE, pToken: &yymsp[-1].minor.yy0, dequote: 1);
174002 if( yymsp[-5].minor.yy528 ) {
174003 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
174004 }
174005}
174006 break;
174007 case 277: /* raisetype ::= ROLLBACK */
174008{yymsp[0].minor.yy394 = OE_Rollback;}
174009 break;
174010 case 279: /* raisetype ::= FAIL */
174011{yymsp[0].minor.yy394 = OE_Fail;}
174012 break;
174013 case 280: /* cmd ::= DROP TRIGGER ifexists fullname */
174014{
174015 sqlite3DropTrigger(pParse,pName: yymsp[0].minor.yy131,noErr: yymsp[-1].minor.yy394);
174016}
174017 break;
174018 case 281: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
174019{
174020 sqlite3Attach(pParse, p: yymsp[-3].minor.yy528, pDbname: yymsp[-1].minor.yy528, pKey: yymsp[0].minor.yy528);
174021}
174022 break;
174023 case 282: /* cmd ::= DETACH database_kw_opt expr */
174024{
174025 sqlite3Detach(pParse, pDbname: yymsp[0].minor.yy528);
174026}
174027 break;
174028 case 285: /* cmd ::= REINDEX */
174029{sqlite3Reindex(pParse, pName1: 0, pName2: 0);}
174030 break;
174031 case 286: /* cmd ::= REINDEX nm dbnm */
174032{sqlite3Reindex(pParse, pName1: &yymsp[-1].minor.yy0, pName2: &yymsp[0].minor.yy0);}
174033 break;
174034 case 287: /* cmd ::= ANALYZE */
174035{sqlite3Analyze(pParse, pName1: 0, pName2: 0);}
174036 break;
174037 case 288: /* cmd ::= ANALYZE nm dbnm */
174038{sqlite3Analyze(pParse, pName1: &yymsp[-1].minor.yy0, pName2: &yymsp[0].minor.yy0);}
174039 break;
174040 case 289: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
174041{
174042 sqlite3AlterRenameTable(pParse,pSrc: yymsp[-3].minor.yy131,pName: &yymsp[0].minor.yy0);
174043}
174044 break;
174045 case 290: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
174046{
174047 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
174048 sqlite3AlterFinishAddColumn(pParse, pColDef: &yymsp[-1].minor.yy0);
174049}
174050 break;
174051 case 291: /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
174052{
174053 sqlite3AlterDropColumn(pParse, pSrc: yymsp[-3].minor.yy131, pName: &yymsp[0].minor.yy0);
174054}
174055 break;
174056 case 292: /* add_column_fullname ::= fullname */
174057{
174058 disableLookaside(pParse);
174059 sqlite3AlterBeginAddColumn(pParse, pSrc: yymsp[0].minor.yy131);
174060}
174061 break;
174062 case 293: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
174063{
174064 sqlite3AlterRenameColumn(pParse, pSrc: yymsp[-5].minor.yy131, pOld: &yymsp[-2].minor.yy0, pNew: &yymsp[0].minor.yy0);
174065}
174066 break;
174067 case 294: /* cmd ::= create_vtab */
174068{sqlite3VtabFinishParse(pParse,pEnd: 0);}
174069 break;
174070 case 295: /* cmd ::= create_vtab LP vtabarglist RP */
174071{sqlite3VtabFinishParse(pParse,pEnd: &yymsp[0].minor.yy0);}
174072 break;
174073 case 296: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
174074{
174075 sqlite3VtabBeginParse(pParse, pName1: &yymsp[-3].minor.yy0, pName2: &yymsp[-2].minor.yy0, pModuleName: &yymsp[0].minor.yy0, ifNotExists: yymsp[-4].minor.yy394);
174076}
174077 break;
174078 case 297: /* vtabarg ::= */
174079{sqlite3VtabArgInit(pParse);}
174080 break;
174081 case 298: /* vtabargtoken ::= ANY */
174082 case 299: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==299);
174083 case 300: /* lp ::= LP */ yytestcase(yyruleno==300);
174084{sqlite3VtabArgExtend(pParse,p: &yymsp[0].minor.yy0);}
174085 break;
174086 case 301: /* with ::= WITH wqlist */
174087 case 302: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==302);
174088{ sqlite3WithPush(pParse, pWith: yymsp[0].minor.yy521, bFree: 1); }
174089 break;
174090 case 303: /* wqas ::= AS */
174091{yymsp[0].minor.yy516 = M10d_Any;}
174092 break;
174093 case 304: /* wqas ::= AS MATERIALIZED */
174094{yymsp[-1].minor.yy516 = M10d_Yes;}
174095 break;
174096 case 305: /* wqas ::= AS NOT MATERIALIZED */
174097{yymsp[-2].minor.yy516 = M10d_No;}
174098 break;
174099 case 306: /* wqitem ::= nm eidlist_opt wqas LP select RP */
174100{
174101 yymsp[-5].minor.yy385 = sqlite3CteNew(pParse, pName: &yymsp[-5].minor.yy0, pArglist: yymsp[-4].minor.yy322, pQuery: yymsp[-1].minor.yy47, eM10d: yymsp[-3].minor.yy516); /*A-overwrites-X*/
174102}
174103 break;
174104 case 307: /* wqlist ::= wqitem */
174105{
174106 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, pWith: 0, pCte: yymsp[0].minor.yy385); /*A-overwrites-X*/
174107}
174108 break;
174109 case 308: /* wqlist ::= wqlist COMMA wqitem */
174110{
174111 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, pWith: yymsp[-2].minor.yy521, pCte: yymsp[0].minor.yy385);
174112}
174113 break;
174114 case 309: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
174115{
174116 assert( yymsp[0].minor.yy41!=0 );
174117 sqlite3WindowChain(pParse, pWin: yymsp[0].minor.yy41, pList: yymsp[-2].minor.yy41);
174118 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
174119 yylhsminor.yy41 = yymsp[0].minor.yy41;
174120}
174121 yymsp[-2].minor.yy41 = yylhsminor.yy41;
174122 break;
174123 case 310: /* windowdefn ::= nm AS LP window RP */
174124{
174125 if( ALWAYS(yymsp[-1].minor.yy41) ){
174126 yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(db: pParse->db, z: yymsp[-4].minor.yy0.z, n: yymsp[-4].minor.yy0.n);
174127 }
174128 yylhsminor.yy41 = yymsp[-1].minor.yy41;
174129}
174130 yymsp[-4].minor.yy41 = yylhsminor.yy41;
174131 break;
174132 case 311: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
174133{
174134 yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy41, pPartition: yymsp[-2].minor.yy322, pOrderBy: yymsp[-1].minor.yy322, pBase: 0);
174135}
174136 break;
174137 case 312: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
174138{
174139 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy41, pPartition: yymsp[-2].minor.yy322, pOrderBy: yymsp[-1].minor.yy322, pBase: &yymsp[-5].minor.yy0);
174140}
174141 yymsp[-5].minor.yy41 = yylhsminor.yy41;
174142 break;
174143 case 313: /* window ::= ORDER BY sortlist frame_opt */
174144{
174145 yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy41, pPartition: 0, pOrderBy: yymsp[-1].minor.yy322, pBase: 0);
174146}
174147 break;
174148 case 314: /* window ::= nm ORDER BY sortlist frame_opt */
174149{
174150 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy41, pPartition: 0, pOrderBy: yymsp[-1].minor.yy322, pBase: &yymsp[-4].minor.yy0);
174151}
174152 yymsp[-4].minor.yy41 = yylhsminor.yy41;
174153 break;
174154 case 315: /* window ::= nm frame_opt */
174155{
174156 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy41, pPartition: 0, pOrderBy: 0, pBase: &yymsp[-1].minor.yy0);
174157}
174158 yymsp[-1].minor.yy41 = yylhsminor.yy41;
174159 break;
174160 case 316: /* frame_opt ::= */
174161{
174162 yymsp[1].minor.yy41 = sqlite3WindowAlloc(pParse, eType: 0, TK_UNBOUNDED, pStart: 0, TK_CURRENT, pEnd: 0, eExclude: 0);
174163}
174164 break;
174165 case 317: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
174166{
174167 yylhsminor.yy41 = sqlite3WindowAlloc(pParse, eType: yymsp[-2].minor.yy394, eStart: yymsp[-1].minor.yy595.eType, pStart: yymsp[-1].minor.yy595.pExpr, TK_CURRENT, pEnd: 0, eExclude: yymsp[0].minor.yy516);
174168}
174169 yymsp[-2].minor.yy41 = yylhsminor.yy41;
174170 break;
174171 case 318: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
174172{
174173 yylhsminor.yy41 = sqlite3WindowAlloc(pParse, eType: yymsp[-5].minor.yy394, eStart: yymsp[-3].minor.yy595.eType, pStart: yymsp[-3].minor.yy595.pExpr, eEnd: yymsp[-1].minor.yy595.eType, pEnd: yymsp[-1].minor.yy595.pExpr, eExclude: yymsp[0].minor.yy516);
174174}
174175 yymsp[-5].minor.yy41 = yylhsminor.yy41;
174176 break;
174177 case 320: /* frame_bound_s ::= frame_bound */
174178 case 322: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==322);
174179{yylhsminor.yy595 = yymsp[0].minor.yy595;}
174180 yymsp[0].minor.yy595 = yylhsminor.yy595;
174181 break;
174182 case 321: /* frame_bound_s ::= UNBOUNDED PRECEDING */
174183 case 323: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==323);
174184 case 325: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==325);
174185{yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
174186 yymsp[-1].minor.yy595 = yylhsminor.yy595;
174187 break;
174188 case 324: /* frame_bound ::= expr PRECEDING|FOLLOWING */
174189{yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
174190 yymsp[-1].minor.yy595 = yylhsminor.yy595;
174191 break;
174192 case 326: /* frame_exclude_opt ::= */
174193{yymsp[1].minor.yy516 = 0;}
174194 break;
174195 case 327: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
174196{yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
174197 break;
174198 case 328: /* frame_exclude ::= NO OTHERS */
174199 case 329: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==329);
174200{yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
174201 break;
174202 case 330: /* frame_exclude ::= GROUP|TIES */
174203{yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
174204 break;
174205 case 331: /* window_clause ::= WINDOW windowdefn_list */
174206{ yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
174207 break;
174208 case 332: /* filter_over ::= filter_clause over_clause */
174209{
174210 if( yymsp[0].minor.yy41 ){
174211 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
174212 }else{
174213 sqlite3ExprDelete(db: pParse->db, p: yymsp[-1].minor.yy528);
174214 }
174215 yylhsminor.yy41 = yymsp[0].minor.yy41;
174216}
174217 yymsp[-1].minor.yy41 = yylhsminor.yy41;
174218 break;
174219 case 333: /* filter_over ::= over_clause */
174220{
174221 yylhsminor.yy41 = yymsp[0].minor.yy41;
174222}
174223 yymsp[0].minor.yy41 = yylhsminor.yy41;
174224 break;
174225 case 334: /* filter_over ::= filter_clause */
174226{
174227 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(Window));
174228 if( yylhsminor.yy41 ){
174229 yylhsminor.yy41->eFrmType = TK_FILTER;
174230 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
174231 }else{
174232 sqlite3ExprDelete(db: pParse->db, p: yymsp[0].minor.yy528);
174233 }
174234}
174235 yymsp[0].minor.yy41 = yylhsminor.yy41;
174236 break;
174237 case 335: /* over_clause ::= OVER LP window RP */
174238{
174239 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
174240 assert( yymsp[-3].minor.yy41!=0 );
174241}
174242 break;
174243 case 336: /* over_clause ::= OVER nm */
174244{
174245 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(Window));
174246 if( yymsp[-1].minor.yy41 ){
174247 yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(db: pParse->db, z: yymsp[0].minor.yy0.z, n: yymsp[0].minor.yy0.n);
174248 }
174249}
174250 break;
174251 case 337: /* filter_clause ::= FILTER LP WHERE expr RP */
174252{ yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
174253 break;
174254 default:
174255 /* (338) input ::= cmdlist */ yytestcase(yyruleno==338);
174256 /* (339) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==339);
174257 /* (340) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=340);
174258 /* (341) ecmd ::= SEMI */ yytestcase(yyruleno==341);
174259 /* (342) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==342);
174260 /* (343) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=343);
174261 /* (344) trans_opt ::= */ yytestcase(yyruleno==344);
174262 /* (345) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==345);
174263 /* (346) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==346);
174264 /* (347) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==347);
174265 /* (348) savepoint_opt ::= */ yytestcase(yyruleno==348);
174266 /* (349) cmd ::= create_table create_table_args */ yytestcase(yyruleno==349);
174267 /* (350) table_option_set ::= table_option (OPTIMIZED OUT) */ assert(yyruleno!=350);
174268 /* (351) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==351);
174269 /* (352) columnlist ::= columnname carglist */ yytestcase(yyruleno==352);
174270 /* (353) nm ::= ID|INDEXED|JOIN_KW */ yytestcase(yyruleno==353);
174271 /* (354) nm ::= STRING */ yytestcase(yyruleno==354);
174272 /* (355) typetoken ::= typename */ yytestcase(yyruleno==355);
174273 /* (356) typename ::= ID|STRING */ yytestcase(yyruleno==356);
174274 /* (357) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=357);
174275 /* (358) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=358);
174276 /* (359) carglist ::= carglist ccons */ yytestcase(yyruleno==359);
174277 /* (360) carglist ::= */ yytestcase(yyruleno==360);
174278 /* (361) ccons ::= NULL onconf */ yytestcase(yyruleno==361);
174279 /* (362) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==362);
174280 /* (363) ccons ::= AS generated */ yytestcase(yyruleno==363);
174281 /* (364) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==364);
174282 /* (365) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==365);
174283 /* (366) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=366);
174284 /* (367) tconscomma ::= */ yytestcase(yyruleno==367);
174285 /* (368) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=368);
174286 /* (369) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=369);
174287 /* (370) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=370);
174288 /* (371) oneselect ::= values */ yytestcase(yyruleno==371);
174289 /* (372) sclp ::= selcollist COMMA */ yytestcase(yyruleno==372);
174290 /* (373) as ::= ID|STRING */ yytestcase(yyruleno==373);
174291 /* (374) indexed_opt ::= indexed_by (OPTIMIZED OUT) */ assert(yyruleno!=374);
174292 /* (375) returning ::= */ yytestcase(yyruleno==375);
174293 /* (376) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=376);
174294 /* (377) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==377);
174295 /* (378) case_operand ::= expr */ yytestcase(yyruleno==378);
174296 /* (379) exprlist ::= nexprlist */ yytestcase(yyruleno==379);
174297 /* (380) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=380);
174298 /* (381) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=381);
174299 /* (382) nmnum ::= ON */ yytestcase(yyruleno==382);
174300 /* (383) nmnum ::= DELETE */ yytestcase(yyruleno==383);
174301 /* (384) nmnum ::= DEFAULT */ yytestcase(yyruleno==384);
174302 /* (385) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==385);
174303 /* (386) foreach_clause ::= */ yytestcase(yyruleno==386);
174304 /* (387) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==387);
174305 /* (388) trnm ::= nm */ yytestcase(yyruleno==388);
174306 /* (389) tridxby ::= */ yytestcase(yyruleno==389);
174307 /* (390) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==390);
174308 /* (391) database_kw_opt ::= */ yytestcase(yyruleno==391);
174309 /* (392) kwcolumn_opt ::= */ yytestcase(yyruleno==392);
174310 /* (393) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==393);
174311 /* (394) vtabarglist ::= vtabarg */ yytestcase(yyruleno==394);
174312 /* (395) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==395);
174313 /* (396) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==396);
174314 /* (397) anylist ::= */ yytestcase(yyruleno==397);
174315 /* (398) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==398);
174316 /* (399) anylist ::= anylist ANY */ yytestcase(yyruleno==399);
174317 /* (400) with ::= */ yytestcase(yyruleno==400);
174318 /* (401) windowdefn_list ::= windowdefn (OPTIMIZED OUT) */ assert(yyruleno!=401);
174319 /* (402) window ::= frame_opt (OPTIMIZED OUT) */ assert(yyruleno!=402);
174320 break;
174321/********** End reduce actions ************************************************/
174322 };
174323 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
174324 yygoto = yyRuleInfoLhs[yyruleno];
174325 yysize = yyRuleInfoNRhs[yyruleno];
174326 yyact = yy_find_reduce_action(stateno: yymsp[yysize].stateno,iLookAhead: (YYCODETYPE)yygoto);
174327
174328 /* There are no SHIFTREDUCE actions on nonterminals because the table
174329 ** generator has simplified them to pure REDUCE actions. */
174330 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
174331
174332 /* It is not possible for a REDUCE to be followed by an error */
174333 assert( yyact!=YY_ERROR_ACTION );
174334
174335 yymsp += yysize+1;
174336 yypParser->yytos = yymsp;
174337 yymsp->stateno = (YYACTIONTYPE)yyact;
174338 yymsp->major = (YYCODETYPE)yygoto;
174339 yyTraceShift(yypParser, yyact, "... then shift");
174340 return yyact;
174341}
174342
174343/*
174344** The following code executes when the parse fails
174345*/
174346#ifndef YYNOERRORRECOVERY
174347static void yy_parse_failed(
174348 yyParser *yypParser /* The parser */
174349){
174350 sqlite3ParserARG_FETCH
174351 sqlite3ParserCTX_FETCH
174352#ifndef NDEBUG
174353 if( yyTraceFILE ){
174354 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
174355 }
174356#endif
174357 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
174358 /* Here code is inserted which will be executed whenever the
174359 ** parser fails */
174360/************ Begin %parse_failure code ***************************************/
174361/************ End %parse_failure code *****************************************/
174362 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
174363 sqlite3ParserCTX_STORE
174364}
174365#endif /* YYNOERRORRECOVERY */
174366
174367/*
174368** The following code executes when a syntax error first occurs.
174369*/
174370static void yy_syntax_error(
174371 yyParser *yypParser, /* The parser */
174372 int yymajor, /* The major type of the error token */
174373 sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */
174374){
174375 sqlite3ParserARG_FETCH
174376 sqlite3ParserCTX_FETCH
174377#define TOKEN yyminor
174378/************ Begin %syntax_error code ****************************************/
174379
174380 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
174381 if( TOKEN.z[0] ){
174382 sqlite3ErrorMsg(pParse, zFormat: "near \"%T\": syntax error", &TOKEN);
174383 }else{
174384 sqlite3ErrorMsg(pParse, zFormat: "incomplete input");
174385 }
174386/************ End %syntax_error code ******************************************/
174387 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
174388 sqlite3ParserCTX_STORE
174389}
174390
174391/*
174392** The following is executed when the parser accepts
174393*/
174394static void yy_accept(
174395 yyParser *yypParser /* The parser */
174396){
174397 sqlite3ParserARG_FETCH
174398 sqlite3ParserCTX_FETCH
174399#ifndef NDEBUG
174400 if( yyTraceFILE ){
174401 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
174402 }
174403#endif
174404#ifndef YYNOERRORRECOVERY
174405 yypParser->yyerrcnt = -1;
174406#endif
174407 assert( yypParser->yytos==yypParser->yystack );
174408 /* Here code is inserted which will be executed whenever the
174409 ** parser accepts */
174410/*********** Begin %parse_accept code *****************************************/
174411/*********** End %parse_accept code *******************************************/
174412 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
174413 sqlite3ParserCTX_STORE
174414}
174415
174416/* The main parser program.
174417** The first argument is a pointer to a structure obtained from
174418** "sqlite3ParserAlloc" which describes the current state of the parser.
174419** The second argument is the major token number. The third is
174420** the minor token. The fourth optional argument is whatever the
174421** user wants (and specified in the grammar) and is available for
174422** use by the action routines.
174423**
174424** Inputs:
174425** <ul>
174426** <li> A pointer to the parser (an opaque structure.)
174427** <li> The major token number.
174428** <li> The minor token number.
174429** <li> An option argument of a grammar-specified type.
174430** </ul>
174431**
174432** Outputs:
174433** None.
174434*/
174435SQLITE_PRIVATE void sqlite3Parser(
174436 void *yyp, /* The parser */
174437 int yymajor, /* The major token code number */
174438 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
174439 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
174440){
174441 YYMINORTYPE yyminorunion;
174442 YYACTIONTYPE yyact; /* The parser action. */
174443#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
174444 int yyendofinput; /* True if we are at the end of input */
174445#endif
174446#ifdef YYERRORSYMBOL
174447 int yyerrorhit = 0; /* True if yymajor has invoked an error */
174448#endif
174449 yyParser *yypParser = (yyParser*)yyp; /* The parser */
174450 sqlite3ParserCTX_FETCH
174451 sqlite3ParserARG_STORE
174452
174453 assert( yypParser->yytos!=0 );
174454#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
174455 yyendofinput = (yymajor==0);
174456#endif
174457
174458 yyact = yypParser->yytos->stateno;
174459#ifndef NDEBUG
174460 if( yyTraceFILE ){
174461 if( yyact < YY_MIN_REDUCE ){
174462 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
174463 yyTracePrompt,yyTokenName[yymajor],yyact);
174464 }else{
174465 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
174466 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
174467 }
174468 }
174469#endif
174470
174471 while(1){ /* Exit by "break" */
174472 assert( yypParser->yytos>=yypParser->yystack );
174473 assert( yyact==yypParser->yytos->stateno );
174474 yyact = yy_find_shift_action(iLookAhead: (YYCODETYPE)yymajor,stateno: yyact);
174475 if( yyact >= YY_MIN_REDUCE ){
174476 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
174477#ifndef NDEBUG
174478 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) );
174479 if( yyTraceFILE ){
174480 int yysize = yyRuleInfoNRhs[yyruleno];
174481 if( yysize ){
174482 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
174483 yyTracePrompt,
174484 yyruleno, yyRuleName[yyruleno],
174485 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
174486 yypParser->yytos[yysize].stateno);
174487 }else{
174488 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
174489 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
174490 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
174491 }
174492 }
174493#endif /* NDEBUG */
174494
174495 /* Check that the stack is large enough to grow by a single entry
174496 ** if the RHS of the rule is empty. This ensures that there is room
174497 ** enough on the stack to push the LHS value */
174498 if( yyRuleInfoNRhs[yyruleno]==0 ){
174499#ifdef YYTRACKMAXSTACKDEPTH
174500 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
174501 yypParser->yyhwm++;
174502 assert( yypParser->yyhwm ==
174503 (int)(yypParser->yytos - yypParser->yystack));
174504 }
174505#endif
174506#if YYSTACKDEPTH>0
174507 if( yypParser->yytos>=yypParser->yystackEnd ){
174508 yyStackOverflow(yypParser);
174509 break;
174510 }
174511#else
174512 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
174513 if( yyGrowStack(yypParser) ){
174514 yyStackOverflow(yypParser);
174515 break;
174516 }
174517 }
174518#endif
174519 }
174520 yyact = yy_reduce(yypParser,yyruleno,yyLookahead: yymajor,yyLookaheadToken: yyminor sqlite3ParserCTX_PARAM);
174521 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
174522 yy_shift(yypParser,yyNewState: yyact,yyMajor: (YYCODETYPE)yymajor,yyMinor: yyminor);
174523#ifndef YYNOERRORRECOVERY
174524 yypParser->yyerrcnt--;
174525#endif
174526 break;
174527 }else if( yyact==YY_ACCEPT_ACTION ){
174528 yypParser->yytos--;
174529 yy_accept(yypParser);
174530 return;
174531 }else{
174532 assert( yyact == YY_ERROR_ACTION );
174533 yyminorunion.yy0 = yyminor;
174534#ifdef YYERRORSYMBOL
174535 int yymx;
174536#endif
174537#ifndef NDEBUG
174538 if( yyTraceFILE ){
174539 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
174540 }
174541#endif
174542#ifdef YYERRORSYMBOL
174543 /* A syntax error has occurred.
174544 ** The response to an error depends upon whether or not the
174545 ** grammar defines an error token "ERROR".
174546 **
174547 ** This is what we do if the grammar does define ERROR:
174548 **
174549 ** * Call the %syntax_error function.
174550 **
174551 ** * Begin popping the stack until we enter a state where
174552 ** it is legal to shift the error symbol, then shift
174553 ** the error symbol.
174554 **
174555 ** * Set the error count to three.
174556 **
174557 ** * Begin accepting and shifting new tokens. No new error
174558 ** processing will occur until three tokens have been
174559 ** shifted successfully.
174560 **
174561 */
174562 if( yypParser->yyerrcnt<0 ){
174563 yy_syntax_error(yypParser,yymajor,yyminor);
174564 }
174565 yymx = yypParser->yytos->major;
174566 if( yymx==YYERRORSYMBOL || yyerrorhit ){
174567#ifndef NDEBUG
174568 if( yyTraceFILE ){
174569 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
174570 yyTracePrompt,yyTokenName[yymajor]);
174571 }
174572#endif
174573 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
174574 yymajor = YYNOCODE;
174575 }else{
174576 while( yypParser->yytos > yypParser->yystack ){
174577 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
174578 YYERRORSYMBOL);
174579 if( yyact<=YY_MAX_SHIFTREDUCE ) break;
174580 yy_pop_parser_stack(yypParser);
174581 }
174582 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
174583 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
174584 yy_parse_failed(yypParser);
174585#ifndef YYNOERRORRECOVERY
174586 yypParser->yyerrcnt = -1;
174587#endif
174588 yymajor = YYNOCODE;
174589 }else if( yymx!=YYERRORSYMBOL ){
174590 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
174591 }
174592 }
174593 yypParser->yyerrcnt = 3;
174594 yyerrorhit = 1;
174595 if( yymajor==YYNOCODE ) break;
174596 yyact = yypParser->yytos->stateno;
174597#elif defined(YYNOERRORRECOVERY)
174598 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
174599 ** do any kind of error recovery. Instead, simply invoke the syntax
174600 ** error routine and continue going as if nothing had happened.
174601 **
174602 ** Applications can set this macro (for example inside %include) if
174603 ** they intend to abandon the parse upon the first syntax error seen.
174604 */
174605 yy_syntax_error(yypParser,yymajor, yyminor);
174606 yy_destructor(yypParser,yymajor: (YYCODETYPE)yymajor,yypminor: &yyminorunion);
174607 break;
174608#else /* YYERRORSYMBOL is not defined */
174609 /* This is what we do if the grammar does not define ERROR:
174610 **
174611 ** * Report an error message, and throw away the input token.
174612 **
174613 ** * If the input token is $, then fail the parse.
174614 **
174615 ** As before, subsequent error messages are suppressed until
174616 ** three input tokens have been successfully shifted.
174617 */
174618 if( yypParser->yyerrcnt<=0 ){
174619 yy_syntax_error(yypParser,yymajor, yyminor);
174620 }
174621 yypParser->yyerrcnt = 3;
174622 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
174623 if( yyendofinput ){
174624 yy_parse_failed(yypParser);
174625#ifndef YYNOERRORRECOVERY
174626 yypParser->yyerrcnt = -1;
174627#endif
174628 }
174629 break;
174630#endif
174631 }
174632 }
174633#ifndef NDEBUG
174634 if( yyTraceFILE ){
174635 yyStackEntry *i;
174636 char cDiv = '[';
174637 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
174638 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
174639 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
174640 cDiv = ' ';
174641 }
174642 fprintf(yyTraceFILE,"]\n");
174643 }
174644#endif
174645 return;
174646}
174647
174648/*
174649** Return the fallback token corresponding to canonical token iToken, or
174650** 0 if iToken has no fallback.
174651*/
174652SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
174653#ifdef YYFALLBACK
174654 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
174655 return yyFallback[iToken];
174656#else
174657 (void)iToken;
174658 return 0;
174659#endif
174660}
174661
174662/************** End of parse.c ***********************************************/
174663/************** Begin file tokenize.c ****************************************/
174664/*
174665** 2001 September 15
174666**
174667** The author disclaims copyright to this source code. In place of
174668** a legal notice, here is a blessing:
174669**
174670** May you do good and not evil.
174671** May you find forgiveness for yourself and forgive others.
174672** May you share freely, never taking more than you give.
174673**
174674*************************************************************************
174675** An tokenizer for SQL
174676**
174677** This file contains C code that splits an SQL input string up into
174678** individual tokens and sends those tokens one-by-one over to the
174679** parser for analysis.
174680*/
174681/* #include "sqliteInt.h" */
174682/* #include <stdlib.h> */
174683
174684/* Character classes for tokenizing
174685**
174686** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
174687** using a lookup table, whereas a switch() directly on c uses a binary search.
174688** The lookup table is much faster. To maximize speed, and to ensure that
174689** a lookup table is used, all of the classes need to be small integers and
174690** all of them need to be used within the switch.
174691*/
174692#define CC_X 0 /* The letter 'x', or start of BLOB literal */
174693#define CC_KYWD0 1 /* First letter of a keyword */
174694#define CC_KYWD 2 /* Alphabetics or '_'. Usable in a keyword */
174695#define CC_DIGIT 3 /* Digits */
174696#define CC_DOLLAR 4 /* '$' */
174697#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
174698#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
174699#define CC_SPACE 7 /* Space characters */
174700#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
174701#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
174702#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
174703#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
174704#define CC_LT 12 /* '<'. Part of < or <= or <> */
174705#define CC_GT 13 /* '>'. Part of > or >= */
174706#define CC_EQ 14 /* '='. Part of = or == */
174707#define CC_BANG 15 /* '!'. Part of != */
174708#define CC_SLASH 16 /* '/'. / or c-style comment */
174709#define CC_LP 17 /* '(' */
174710#define CC_RP 18 /* ')' */
174711#define CC_SEMI 19 /* ';' */
174712#define CC_PLUS 20 /* '+' */
174713#define CC_STAR 21 /* '*' */
174714#define CC_PERCENT 22 /* '%' */
174715#define CC_COMMA 23 /* ',' */
174716#define CC_AND 24 /* '&' */
174717#define CC_TILDA 25 /* '~' */
174718#define CC_DOT 26 /* '.' */
174719#define CC_ID 27 /* unicode characters usable in IDs */
174720#define CC_ILLEGAL 28 /* Illegal character */
174721#define CC_NUL 29 /* 0x00 */
174722#define CC_BOM 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */
174723
174724static const unsigned char aiClass[] = {
174725#ifdef SQLITE_ASCII
174726/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
174727/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28,
174728/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
174729/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
174730/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
174731/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174732/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2,
174733/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174734/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28,
174735/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
174736/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
174737/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
174738/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
174739/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
174740/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
174741/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
174742/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
174743#endif
174744#ifdef SQLITE_EBCDIC
174745/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
174746/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28,
174747/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
174748/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
174749/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
174750/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
174751/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28,
174752/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6,
174753/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8,
174754/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
174755/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
174756/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
174757/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28,
174758/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
174759/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
174760/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
174761/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28,
174762#endif
174763};
174764
174765/*
174766** The charMap() macro maps alphabetic characters (only) into their
174767** lower-case ASCII equivalent. On ASCII machines, this is just
174768** an upper-to-lower case map. On EBCDIC machines we also need
174769** to adjust the encoding. The mapping is only valid for alphabetics
174770** which are the only characters for which this feature is used.
174771**
174772** Used by keywordhash.h
174773*/
174774#ifdef SQLITE_ASCII
174775# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
174776#endif
174777#ifdef SQLITE_EBCDIC
174778# define charMap(X) ebcdicToAscii[(unsigned char)X]
174779const unsigned char ebcdicToAscii[] = {
174780/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
174781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
174782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
174783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
174784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
174785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
174786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
174787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
174788 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
174789 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
174790 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
174791 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
174792 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
174793 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
174794 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
174795 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
174796 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
174797};
174798#endif
174799
174800/*
174801** The sqlite3KeywordCode function looks up an identifier to determine if
174802** it is a keyword. If it is a keyword, the token code of that keyword is
174803** returned. If the input is not a keyword, TK_ID is returned.
174804**
174805** The implementation of this routine was generated by a program,
174806** mkkeywordhash.c, located in the tool subdirectory of the distribution.
174807** The output of the mkkeywordhash.c program is written into a file
174808** named keywordhash.h and then included into this source file by
174809** the #include below.
174810*/
174811/************** Include keywordhash.h in the middle of tokenize.c ************/
174812/************** Begin file keywordhash.h *************************************/
174813/***** This file contains automatically generated code ******
174814**
174815** The code in this file has been automatically generated by
174816**
174817** sqlite/tool/mkkeywordhash.c
174818**
174819** The code in this file implements a function that determines whether
174820** or not a given identifier is really an SQL keyword. The same thing
174821** might be implemented more directly using a hand-written hash table.
174822** But by using this automatically generated code, the size of the code
174823** is substantially reduced. This is important for embedded applications
174824** on platforms with limited memory.
174825*/
174826/* Hash score: 231 */
174827/* zKWText[] encodes 1007 bytes of keyword text in 667 bytes */
174828/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
174829/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */
174830/* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */
174831/* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */
174832/* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */
174833/* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */
174834/* PRAGMATERIALIZEDEFERREDISTINCTUPDATEVALUESVIRTUALWAYSWHENWHERE */
174835/* CURSIVEABORTAFTERENAMEANDROPARTITIONAUTOINCREMENTCASTCOLUMN */
174836/* COMMITCONFLICTCROSSCURRENT_TIMESTAMPRECEDINGFAILASTFILTER */
174837/* EPLACEFIRSTFOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVER */
174838/* ETURNINGRIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBY */
174839/* INITIALLYPRIMARY */
174840static const char zKWText[666] = {
174841 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
174842 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
174843 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
174844 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
174845 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
174846 'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
174847 'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
174848 'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
174849 'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
174850 'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
174851 'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
174852 'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
174853 'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
174854 'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
174855 'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
174856 'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
174857 'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
174858 'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
174859 'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
174860 'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
174861 'A','T','E','R','I','A','L','I','Z','E','D','E','F','E','R','R','E','D',
174862 'I','S','T','I','N','C','T','U','P','D','A','T','E','V','A','L','U','E',
174863 'S','V','I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H',
174864 'E','R','E','C','U','R','S','I','V','E','A','B','O','R','T','A','F','T',
174865 'E','R','E','N','A','M','E','A','N','D','R','O','P','A','R','T','I','T',
174866 'I','O','N','A','U','T','O','I','N','C','R','E','M','E','N','T','C','A',
174867 'S','T','C','O','L','U','M','N','C','O','M','M','I','T','C','O','N','F',
174868 'L','I','C','T','C','R','O','S','S','C','U','R','R','E','N','T','_','T',
174869 'I','M','E','S','T','A','M','P','R','E','C','E','D','I','N','G','F','A',
174870 'I','L','A','S','T','F','I','L','T','E','R','E','P','L','A','C','E','F',
174871 'I','R','S','T','F','O','L','L','O','W','I','N','G','F','R','O','M','F',
174872 'U','L','L','I','M','I','T','I','F','O','R','D','E','R','E','S','T','R',
174873 'I','C','T','O','T','H','E','R','S','O','V','E','R','E','T','U','R','N',
174874 'I','N','G','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O',
174875 'W','S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S',
174876 'I','N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W',
174877 'B','Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
174878};
174879/* aKWHash[i] is the hash value for the i-th keyword */
174880static const unsigned char aKWHash[127] = {
174881 84, 92, 134, 82, 105, 29, 0, 0, 94, 0, 85, 72, 0,
174882 53, 35, 86, 15, 0, 42, 97, 54, 89, 135, 19, 0, 0,
174883 140, 0, 40, 129, 0, 22, 107, 0, 9, 0, 0, 123, 80,
174884 0, 78, 6, 0, 65, 103, 147, 0, 136, 115, 0, 0, 48,
174885 0, 90, 24, 0, 17, 0, 27, 70, 23, 26, 5, 60, 142,
174886 110, 122, 0, 73, 91, 71, 145, 61, 120, 74, 0, 49, 0,
174887 11, 41, 0, 113, 0, 0, 0, 109, 10, 111, 116, 125, 14,
174888 50, 124, 0, 100, 0, 18, 121, 144, 56, 130, 139, 88, 83,
174889 37, 30, 126, 0, 0, 108, 51, 131, 128, 0, 34, 0, 0,
174890 132, 0, 98, 38, 39, 0, 20, 45, 117, 93,
174891};
174892/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
174893** then the i-th keyword has no more hash collisions. Otherwise,
174894** the next keyword with the same hash is aKWHash[i]-1. */
174895static const unsigned char aKWNext[148] = {0,
174896 0, 0, 0, 0, 4, 0, 43, 0, 0, 106, 114, 0, 0,
174897 0, 2, 0, 0, 143, 0, 0, 0, 13, 0, 0, 0, 0,
174898 141, 0, 0, 119, 52, 0, 0, 137, 12, 0, 0, 62, 0,
174899 138, 0, 133, 0, 0, 36, 0, 0, 28, 77, 0, 0, 0,
174900 0, 59, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174901 0, 69, 0, 0, 0, 0, 0, 146, 3, 0, 58, 0, 1,
174902 75, 0, 0, 0, 31, 0, 0, 0, 0, 0, 127, 0, 104,
174903 0, 64, 66, 63, 0, 0, 0, 0, 0, 46, 0, 16, 8,
174904 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 101, 0,
174905 112, 21, 7, 67, 0, 79, 96, 118, 0, 0, 68, 0, 0,
174906 99, 44, 0, 55, 0, 76, 0, 95, 32, 33, 57, 25, 0,
174907 102, 0, 0, 87,
174908};
174909/* aKWLen[i] is the length (in bytes) of the i-th keyword */
174910static const unsigned char aKWLen[148] = {0,
174911 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
174912 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
174913 6, 9, 4, 2, 6, 5, 9, 9, 4, 7, 3, 2, 4,
174914 4, 6, 11, 6, 2, 7, 5, 5, 9, 6, 10, 4, 6,
174915 2, 3, 7, 5, 9, 6, 6, 4, 5, 5, 10, 6, 5,
174916 7, 4, 5, 7, 6, 7, 7, 6, 5, 7, 3, 7, 4,
174917 7, 6, 12, 9, 4, 6, 5, 4, 7, 6, 12, 8, 8,
174918 2, 6, 6, 7, 6, 4, 5, 9, 5, 5, 6, 3, 4,
174919 9, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 9,
174920 4, 4, 6, 7, 5, 9, 4, 4, 5, 2, 5, 8, 6,
174921 4, 9, 5, 8, 4, 3, 9, 5, 5, 6, 4, 6, 2,
174922 2, 9, 3, 7,
174923};
174924/* aKWOffset[i] is the index into zKWText[] of the start of
174925** the text for the i-th keyword. */
174926static const unsigned short int aKWOffset[148] = {0,
174927 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
174928 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
174929 86, 90, 90, 94, 99, 101, 105, 111, 119, 123, 123, 123, 126,
174930 129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
174931 184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
174932 244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
174933 299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 359, 370, 377,
174934 378, 385, 391, 397, 402, 408, 412, 415, 424, 429, 433, 439, 441,
174935 444, 453, 455, 457, 466, 470, 476, 482, 490, 495, 495, 495, 511,
174936 520, 523, 527, 532, 539, 544, 553, 557, 560, 565, 567, 571, 579,
174937 585, 588, 597, 602, 610, 610, 614, 623, 628, 633, 639, 642, 645,
174938 648, 650, 655, 659,
174939};
174940/* aKWCode[i] is the parser symbol code for the i-th keyword */
174941static const unsigned char aKWCode[148] = {0,
174942 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
174943 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
174944 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
174945 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
174946 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
174947 TK_EXCLUDE, TK_DELETE, TK_TEMP, TK_TEMP, TK_OR,
174948 TK_ISNULL, TK_NULLS, TK_SAVEPOINT, TK_INTERSECT, TK_TIES,
174949 TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, TK_LIKE_KW,
174950 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
174951 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_CONSTRAINT,
174952 TK_INTO, TK_OFFSET, TK_OF, TK_SET, TK_TRIGGER,
174953 TK_RANGE, TK_GENERATED, TK_DETACH, TK_HAVING, TK_LIKE_KW,
174954 TK_BEGIN, TK_JOIN_KW, TK_REFERENCES, TK_UNIQUE, TK_QUERY,
174955 TK_WITHOUT, TK_WITH, TK_JOIN_KW, TK_RELEASE, TK_ATTACH,
174956 TK_BETWEEN, TK_NOTHING, TK_GROUPS, TK_GROUP, TK_CASCADE,
174957 TK_ASC, TK_DEFAULT, TK_CASE, TK_COLLATE, TK_CREATE,
174958 TK_CTIME_KW, TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_MATCH,
174959 TK_PLAN, TK_ANALYZE, TK_PRAGMA, TK_MATERIALIZED, TK_DEFERRED,
174960 TK_DISTINCT, TK_IS, TK_UPDATE, TK_VALUES, TK_VIRTUAL,
174961 TK_ALWAYS, TK_WHEN, TK_WHERE, TK_RECURSIVE, TK_ABORT,
174962 TK_AFTER, TK_RENAME, TK_AND, TK_DROP, TK_PARTITION,
174963 TK_AUTOINCR, TK_TO, TK_IN, TK_CAST, TK_COLUMNKW,
174964 TK_COMMIT, TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW,
174965 TK_CURRENT, TK_PRECEDING, TK_FAIL, TK_LAST, TK_FILTER,
174966 TK_REPLACE, TK_FIRST, TK_FOLLOWING, TK_FROM, TK_JOIN_KW,
174967 TK_LIMIT, TK_IF, TK_ORDER, TK_RESTRICT, TK_OTHERS,
174968 TK_OVER, TK_RETURNING, TK_JOIN_KW, TK_ROLLBACK, TK_ROWS,
174969 TK_ROW, TK_UNBOUNDED, TK_UNION, TK_USING, TK_VACUUM,
174970 TK_VIEW, TK_WINDOW, TK_DO, TK_BY, TK_INITIALLY,
174971 TK_ALL, TK_PRIMARY,
174972};
174973/* Hash table decoded:
174974** 0: INSERT
174975** 1: IS
174976** 2: ROLLBACK TRIGGER
174977** 3: IMMEDIATE
174978** 4: PARTITION
174979** 5: TEMP
174980** 6:
174981** 7:
174982** 8: VALUES WITHOUT
174983** 9:
174984** 10: MATCH
174985** 11: NOTHING
174986** 12:
174987** 13: OF
174988** 14: TIES IGNORE
174989** 15: PLAN
174990** 16: INSTEAD INDEXED
174991** 17:
174992** 18: TRANSACTION RIGHT
174993** 19: WHEN
174994** 20: SET HAVING
174995** 21: MATERIALIZED IF
174996** 22: ROWS
174997** 23: SELECT
174998** 24:
174999** 25:
175000** 26: VACUUM SAVEPOINT
175001** 27:
175002** 28: LIKE UNION VIRTUAL REFERENCES
175003** 29: RESTRICT
175004** 30:
175005** 31: THEN REGEXP
175006** 32: TO
175007** 33:
175008** 34: BEFORE
175009** 35:
175010** 36:
175011** 37: FOLLOWING COLLATE CASCADE
175012** 38: CREATE
175013** 39:
175014** 40: CASE REINDEX
175015** 41: EACH
175016** 42:
175017** 43: QUERY
175018** 44: AND ADD
175019** 45: PRIMARY ANALYZE
175020** 46:
175021** 47: ROW ASC DETACH
175022** 48: CURRENT_TIME CURRENT_DATE
175023** 49:
175024** 50:
175025** 51: EXCLUSIVE TEMPORARY
175026** 52:
175027** 53: DEFERRED
175028** 54: DEFERRABLE
175029** 55:
175030** 56: DATABASE
175031** 57:
175032** 58: DELETE VIEW GENERATED
175033** 59: ATTACH
175034** 60: END
175035** 61: EXCLUDE
175036** 62: ESCAPE DESC
175037** 63: GLOB
175038** 64: WINDOW ELSE
175039** 65: COLUMN
175040** 66: FIRST
175041** 67:
175042** 68: GROUPS ALL
175043** 69: DISTINCT DROP KEY
175044** 70: BETWEEN
175045** 71: INITIALLY
175046** 72: BEGIN
175047** 73: FILTER CHECK ACTION
175048** 74: GROUP INDEX
175049** 75:
175050** 76: EXISTS DEFAULT
175051** 77:
175052** 78: FOR CURRENT_TIMESTAMP
175053** 79: EXCEPT
175054** 80:
175055** 81: CROSS
175056** 82:
175057** 83:
175058** 84:
175059** 85: CAST
175060** 86: FOREIGN AUTOINCREMENT
175061** 87: COMMIT
175062** 88: CURRENT AFTER ALTER
175063** 89: FULL FAIL CONFLICT
175064** 90: EXPLAIN
175065** 91: CONSTRAINT
175066** 92: FROM ALWAYS
175067** 93:
175068** 94: ABORT
175069** 95:
175070** 96: AS DO
175071** 97: REPLACE WITH RELEASE
175072** 98: BY RENAME
175073** 99: RANGE RAISE
175074** 100: OTHERS
175075** 101: USING NULLS
175076** 102: PRAGMA
175077** 103: JOIN ISNULL OFFSET
175078** 104: NOT
175079** 105: OR LAST LEFT
175080** 106: LIMIT
175081** 107:
175082** 108:
175083** 109: IN
175084** 110: INTO
175085** 111: OVER RECURSIVE
175086** 112: ORDER OUTER
175087** 113:
175088** 114: INTERSECT UNBOUNDED
175089** 115:
175090** 116:
175091** 117: RETURNING ON
175092** 118:
175093** 119: WHERE
175094** 120: NO INNER
175095** 121: NULL
175096** 122:
175097** 123: TABLE
175098** 124: NATURAL NOTNULL
175099** 125: PRECEDING
175100** 126: UPDATE UNIQUE
175101*/
175102/* Check to see if z[0..n-1] is a keyword. If it is, write the
175103** parser symbol code for that keyword into *pType. Always
175104** return the integer n (the length of the token). */
175105static int keywordCode(const char *z, int n, int *pType){
175106 int i, j;
175107 const char *zKW;
175108 assert( n>=2 );
175109 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
175110 for(i=(int)aKWHash[i]; i>0; i=aKWNext[i]){
175111 if( aKWLen[i]!=n ) continue;
175112 zKW = &zKWText[aKWOffset[i]];
175113#ifdef SQLITE_ASCII
175114 if( (z[0]&~0x20)!=zKW[0] ) continue;
175115 if( (z[1]&~0x20)!=zKW[1] ) continue;
175116 j = 2;
175117 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
175118#endif
175119#ifdef SQLITE_EBCDIC
175120 if( toupper(z[0])!=zKW[0] ) continue;
175121 if( toupper(z[1])!=zKW[1] ) continue;
175122 j = 2;
175123 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
175124#endif
175125 if( j<n ) continue;
175126 testcase( i==1 ); /* REINDEX */
175127 testcase( i==2 ); /* INDEXED */
175128 testcase( i==3 ); /* INDEX */
175129 testcase( i==4 ); /* DESC */
175130 testcase( i==5 ); /* ESCAPE */
175131 testcase( i==6 ); /* EACH */
175132 testcase( i==7 ); /* CHECK */
175133 testcase( i==8 ); /* KEY */
175134 testcase( i==9 ); /* BEFORE */
175135 testcase( i==10 ); /* FOREIGN */
175136 testcase( i==11 ); /* FOR */
175137 testcase( i==12 ); /* IGNORE */
175138 testcase( i==13 ); /* REGEXP */
175139 testcase( i==14 ); /* EXPLAIN */
175140 testcase( i==15 ); /* INSTEAD */
175141 testcase( i==16 ); /* ADD */
175142 testcase( i==17 ); /* DATABASE */
175143 testcase( i==18 ); /* AS */
175144 testcase( i==19 ); /* SELECT */
175145 testcase( i==20 ); /* TABLE */
175146 testcase( i==21 ); /* LEFT */
175147 testcase( i==22 ); /* THEN */
175148 testcase( i==23 ); /* END */
175149 testcase( i==24 ); /* DEFERRABLE */
175150 testcase( i==25 ); /* ELSE */
175151 testcase( i==26 ); /* EXCLUDE */
175152 testcase( i==27 ); /* DELETE */
175153 testcase( i==28 ); /* TEMPORARY */
175154 testcase( i==29 ); /* TEMP */
175155 testcase( i==30 ); /* OR */
175156 testcase( i==31 ); /* ISNULL */
175157 testcase( i==32 ); /* NULLS */
175158 testcase( i==33 ); /* SAVEPOINT */
175159 testcase( i==34 ); /* INTERSECT */
175160 testcase( i==35 ); /* TIES */
175161 testcase( i==36 ); /* NOTNULL */
175162 testcase( i==37 ); /* NOT */
175163 testcase( i==38 ); /* NO */
175164 testcase( i==39 ); /* NULL */
175165 testcase( i==40 ); /* LIKE */
175166 testcase( i==41 ); /* EXCEPT */
175167 testcase( i==42 ); /* TRANSACTION */
175168 testcase( i==43 ); /* ACTION */
175169 testcase( i==44 ); /* ON */
175170 testcase( i==45 ); /* NATURAL */
175171 testcase( i==46 ); /* ALTER */
175172 testcase( i==47 ); /* RAISE */
175173 testcase( i==48 ); /* EXCLUSIVE */
175174 testcase( i==49 ); /* EXISTS */
175175 testcase( i==50 ); /* CONSTRAINT */
175176 testcase( i==51 ); /* INTO */
175177 testcase( i==52 ); /* OFFSET */
175178 testcase( i==53 ); /* OF */
175179 testcase( i==54 ); /* SET */
175180 testcase( i==55 ); /* TRIGGER */
175181 testcase( i==56 ); /* RANGE */
175182 testcase( i==57 ); /* GENERATED */
175183 testcase( i==58 ); /* DETACH */
175184 testcase( i==59 ); /* HAVING */
175185 testcase( i==60 ); /* GLOB */
175186 testcase( i==61 ); /* BEGIN */
175187 testcase( i==62 ); /* INNER */
175188 testcase( i==63 ); /* REFERENCES */
175189 testcase( i==64 ); /* UNIQUE */
175190 testcase( i==65 ); /* QUERY */
175191 testcase( i==66 ); /* WITHOUT */
175192 testcase( i==67 ); /* WITH */
175193 testcase( i==68 ); /* OUTER */
175194 testcase( i==69 ); /* RELEASE */
175195 testcase( i==70 ); /* ATTACH */
175196 testcase( i==71 ); /* BETWEEN */
175197 testcase( i==72 ); /* NOTHING */
175198 testcase( i==73 ); /* GROUPS */
175199 testcase( i==74 ); /* GROUP */
175200 testcase( i==75 ); /* CASCADE */
175201 testcase( i==76 ); /* ASC */
175202 testcase( i==77 ); /* DEFAULT */
175203 testcase( i==78 ); /* CASE */
175204 testcase( i==79 ); /* COLLATE */
175205 testcase( i==80 ); /* CREATE */
175206 testcase( i==81 ); /* CURRENT_DATE */
175207 testcase( i==82 ); /* IMMEDIATE */
175208 testcase( i==83 ); /* JOIN */
175209 testcase( i==84 ); /* INSERT */
175210 testcase( i==85 ); /* MATCH */
175211 testcase( i==86 ); /* PLAN */
175212 testcase( i==87 ); /* ANALYZE */
175213 testcase( i==88 ); /* PRAGMA */
175214 testcase( i==89 ); /* MATERIALIZED */
175215 testcase( i==90 ); /* DEFERRED */
175216 testcase( i==91 ); /* DISTINCT */
175217 testcase( i==92 ); /* IS */
175218 testcase( i==93 ); /* UPDATE */
175219 testcase( i==94 ); /* VALUES */
175220 testcase( i==95 ); /* VIRTUAL */
175221 testcase( i==96 ); /* ALWAYS */
175222 testcase( i==97 ); /* WHEN */
175223 testcase( i==98 ); /* WHERE */
175224 testcase( i==99 ); /* RECURSIVE */
175225 testcase( i==100 ); /* ABORT */
175226 testcase( i==101 ); /* AFTER */
175227 testcase( i==102 ); /* RENAME */
175228 testcase( i==103 ); /* AND */
175229 testcase( i==104 ); /* DROP */
175230 testcase( i==105 ); /* PARTITION */
175231 testcase( i==106 ); /* AUTOINCREMENT */
175232 testcase( i==107 ); /* TO */
175233 testcase( i==108 ); /* IN */
175234 testcase( i==109 ); /* CAST */
175235 testcase( i==110 ); /* COLUMN */
175236 testcase( i==111 ); /* COMMIT */
175237 testcase( i==112 ); /* CONFLICT */
175238 testcase( i==113 ); /* CROSS */
175239 testcase( i==114 ); /* CURRENT_TIMESTAMP */
175240 testcase( i==115 ); /* CURRENT_TIME */
175241 testcase( i==116 ); /* CURRENT */
175242 testcase( i==117 ); /* PRECEDING */
175243 testcase( i==118 ); /* FAIL */
175244 testcase( i==119 ); /* LAST */
175245 testcase( i==120 ); /* FILTER */
175246 testcase( i==121 ); /* REPLACE */
175247 testcase( i==122 ); /* FIRST */
175248 testcase( i==123 ); /* FOLLOWING */
175249 testcase( i==124 ); /* FROM */
175250 testcase( i==125 ); /* FULL */
175251 testcase( i==126 ); /* LIMIT */
175252 testcase( i==127 ); /* IF */
175253 testcase( i==128 ); /* ORDER */
175254 testcase( i==129 ); /* RESTRICT */
175255 testcase( i==130 ); /* OTHERS */
175256 testcase( i==131 ); /* OVER */
175257 testcase( i==132 ); /* RETURNING */
175258 testcase( i==133 ); /* RIGHT */
175259 testcase( i==134 ); /* ROLLBACK */
175260 testcase( i==135 ); /* ROWS */
175261 testcase( i==136 ); /* ROW */
175262 testcase( i==137 ); /* UNBOUNDED */
175263 testcase( i==138 ); /* UNION */
175264 testcase( i==139 ); /* USING */
175265 testcase( i==140 ); /* VACUUM */
175266 testcase( i==141 ); /* VIEW */
175267 testcase( i==142 ); /* WINDOW */
175268 testcase( i==143 ); /* DO */
175269 testcase( i==144 ); /* BY */
175270 testcase( i==145 ); /* INITIALLY */
175271 testcase( i==146 ); /* ALL */
175272 testcase( i==147 ); /* PRIMARY */
175273 *pType = aKWCode[i];
175274 break;
175275 }
175276 return n;
175277}
175278SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
175279 int id = TK_ID;
175280 if( n>=2 ) keywordCode(z: (char*)z, n, pType: &id);
175281 return id;
175282}
175283#define SQLITE_N_KEYWORD 147
175284SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
175285 if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
175286 i++;
175287 *pzName = zKWText + aKWOffset[i];
175288 *pnName = aKWLen[i];
175289 return SQLITE_OK;
175290}
175291SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
175292SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
175293 return TK_ID!=sqlite3KeywordCode(z: (const u8*)zName, n: nName);
175294}
175295
175296/************** End of keywordhash.h *****************************************/
175297/************** Continuing where we left off in tokenize.c *******************/
175298
175299
175300/*
175301** If X is a character that can be used in an identifier then
175302** IdChar(X) will be true. Otherwise it is false.
175303**
175304** For ASCII, any character with the high-order bit set is
175305** allowed in an identifier. For 7-bit characters,
175306** sqlite3IsIdChar[X] must be 1.
175307**
175308** For EBCDIC, the rules are more complex but have the same
175309** end result.
175310**
175311** Ticket #1066. the SQL standard does not allow '$' in the
175312** middle of identifiers. But many SQL implementations do.
175313** SQLite will allow '$' in identifiers for compatibility.
175314** But the feature is undocumented.
175315*/
175316#ifdef SQLITE_ASCII
175317#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
175318#endif
175319#ifdef SQLITE_EBCDIC
175320SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
175321/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
175322 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
175323 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
175324 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
175325 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
175326 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
175327 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
175328 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
175329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
175330 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
175331 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
175332 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
175333 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
175334};
175335#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
175336#endif
175337
175338/* Make the IdChar function accessible from ctime.c and alter.c */
175339SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
175340
175341#ifndef SQLITE_OMIT_WINDOWFUNC
175342/*
175343** Return the id of the next token in string (*pz). Before returning, set
175344** (*pz) to point to the byte following the parsed token.
175345*/
175346static int getToken(const unsigned char **pz){
175347 const unsigned char *z = *pz;
175348 int t; /* Token type to return */
175349 do {
175350 z += sqlite3GetToken(z, &t);
175351 }while( t==TK_SPACE );
175352 if( t==TK_ID
175353 || t==TK_STRING
175354 || t==TK_JOIN_KW
175355 || t==TK_WINDOW
175356 || t==TK_OVER
175357 || sqlite3ParserFallback(iToken: t)==TK_ID
175358 ){
175359 t = TK_ID;
175360 }
175361 *pz = z;
175362 return t;
175363}
175364
175365/*
175366** The following three functions are called immediately after the tokenizer
175367** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
175368** whether the token should be treated as a keyword or an SQL identifier.
175369** This cannot be handled by the usual lemon %fallback method, due to
175370** the ambiguity in some constructions. e.g.
175371**
175372** SELECT sum(x) OVER ...
175373**
175374** In the above, "OVER" might be a keyword, or it might be an alias for the
175375** sum(x) expression. If a "%fallback ID OVER" directive were added to
175376** grammar, then SQLite would always treat "OVER" as an alias, making it
175377** impossible to call a window-function without a FILTER clause.
175378**
175379** WINDOW is treated as a keyword if:
175380**
175381** * the following token is an identifier, or a keyword that can fallback
175382** to being an identifier, and
175383** * the token after than one is TK_AS.
175384**
175385** OVER is a keyword if:
175386**
175387** * the previous token was TK_RP, and
175388** * the next token is either TK_LP or an identifier.
175389**
175390** FILTER is a keyword if:
175391**
175392** * the previous token was TK_RP, and
175393** * the next token is TK_LP.
175394*/
175395static int analyzeWindowKeyword(const unsigned char *z){
175396 int t;
175397 t = getToken(pz: &z);
175398 if( t!=TK_ID ) return TK_ID;
175399 t = getToken(pz: &z);
175400 if( t!=TK_AS ) return TK_ID;
175401 return TK_WINDOW;
175402}
175403static int analyzeOverKeyword(const unsigned char *z, int lastToken){
175404 if( lastToken==TK_RP ){
175405 int t = getToken(pz: &z);
175406 if( t==TK_LP || t==TK_ID ) return TK_OVER;
175407 }
175408 return TK_ID;
175409}
175410static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
175411 if( lastToken==TK_RP && getToken(pz: &z)==TK_LP ){
175412 return TK_FILTER;
175413 }
175414 return TK_ID;
175415}
175416#endif /* SQLITE_OMIT_WINDOWFUNC */
175417
175418/*
175419** Return the length (in bytes) of the token that begins at z[0].
175420** Store the token type in *tokenType before returning.
175421*/
175422SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
175423 int i, c;
175424 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
175425 ** of the token. See the comment on the CC_ defines
175426 ** above. */
175427 case CC_SPACE: {
175428 testcase( z[0]==' ' );
175429 testcase( z[0]=='\t' );
175430 testcase( z[0]=='\n' );
175431 testcase( z[0]=='\f' );
175432 testcase( z[0]=='\r' );
175433 for(i=1; sqlite3Isspace(z[i]); i++){}
175434 *tokenType = TK_SPACE;
175435 return i;
175436 }
175437 case CC_MINUS: {
175438 if( z[1]=='-' ){
175439 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
175440 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
175441 return i;
175442 }else if( z[1]=='>' ){
175443 *tokenType = TK_PTR;
175444 return 2 + (z[2]=='>');
175445 }
175446 *tokenType = TK_MINUS;
175447 return 1;
175448 }
175449 case CC_LP: {
175450 *tokenType = TK_LP;
175451 return 1;
175452 }
175453 case CC_RP: {
175454 *tokenType = TK_RP;
175455 return 1;
175456 }
175457 case CC_SEMI: {
175458 *tokenType = TK_SEMI;
175459 return 1;
175460 }
175461 case CC_PLUS: {
175462 *tokenType = TK_PLUS;
175463 return 1;
175464 }
175465 case CC_STAR: {
175466 *tokenType = TK_STAR;
175467 return 1;
175468 }
175469 case CC_SLASH: {
175470 if( z[1]!='*' || z[2]==0 ){
175471 *tokenType = TK_SLASH;
175472 return 1;
175473 }
175474 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
175475 if( c ) i++;
175476 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
175477 return i;
175478 }
175479 case CC_PERCENT: {
175480 *tokenType = TK_REM;
175481 return 1;
175482 }
175483 case CC_EQ: {
175484 *tokenType = TK_EQ;
175485 return 1 + (z[1]=='=');
175486 }
175487 case CC_LT: {
175488 if( (c=z[1])=='=' ){
175489 *tokenType = TK_LE;
175490 return 2;
175491 }else if( c=='>' ){
175492 *tokenType = TK_NE;
175493 return 2;
175494 }else if( c=='<' ){
175495 *tokenType = TK_LSHIFT;
175496 return 2;
175497 }else{
175498 *tokenType = TK_LT;
175499 return 1;
175500 }
175501 }
175502 case CC_GT: {
175503 if( (c=z[1])=='=' ){
175504 *tokenType = TK_GE;
175505 return 2;
175506 }else if( c=='>' ){
175507 *tokenType = TK_RSHIFT;
175508 return 2;
175509 }else{
175510 *tokenType = TK_GT;
175511 return 1;
175512 }
175513 }
175514 case CC_BANG: {
175515 if( z[1]!='=' ){
175516 *tokenType = TK_ILLEGAL;
175517 return 1;
175518 }else{
175519 *tokenType = TK_NE;
175520 return 2;
175521 }
175522 }
175523 case CC_PIPE: {
175524 if( z[1]!='|' ){
175525 *tokenType = TK_BITOR;
175526 return 1;
175527 }else{
175528 *tokenType = TK_CONCAT;
175529 return 2;
175530 }
175531 }
175532 case CC_COMMA: {
175533 *tokenType = TK_COMMA;
175534 return 1;
175535 }
175536 case CC_AND: {
175537 *tokenType = TK_BITAND;
175538 return 1;
175539 }
175540 case CC_TILDA: {
175541 *tokenType = TK_BITNOT;
175542 return 1;
175543 }
175544 case CC_QUOTE: {
175545 int delim = z[0];
175546 testcase( delim=='`' );
175547 testcase( delim=='\'' );
175548 testcase( delim=='"' );
175549 for(i=1; (c=z[i])!=0; i++){
175550 if( c==delim ){
175551 if( z[i+1]==delim ){
175552 i++;
175553 }else{
175554 break;
175555 }
175556 }
175557 }
175558 if( c=='\'' ){
175559 *tokenType = TK_STRING;
175560 return i+1;
175561 }else if( c!=0 ){
175562 *tokenType = TK_ID;
175563 return i+1;
175564 }else{
175565 *tokenType = TK_ILLEGAL;
175566 return i;
175567 }
175568 }
175569 case CC_DOT: {
175570#ifndef SQLITE_OMIT_FLOATING_POINT
175571 if( !sqlite3Isdigit(z[1]) )
175572#endif
175573 {
175574 *tokenType = TK_DOT;
175575 return 1;
175576 }
175577 /* If the next character is a digit, this is a floating point
175578 ** number that begins with ".". Fall thru into the next case */
175579 /* no break */ deliberate_fall_through
175580 }
175581 case CC_DIGIT: {
175582 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
175583 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
175584 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
175585 testcase( z[0]=='9' ); testcase( z[0]=='.' );
175586 *tokenType = TK_INTEGER;
175587#ifndef SQLITE_OMIT_HEX_INTEGER
175588 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
175589 for(i=3; sqlite3Isxdigit(z[i]); i++){}
175590 return i;
175591 }
175592#endif
175593 for(i=0; sqlite3Isdigit(z[i]); i++){}
175594#ifndef SQLITE_OMIT_FLOATING_POINT
175595 if( z[i]=='.' ){
175596 i++;
175597 while( sqlite3Isdigit(z[i]) ){ i++; }
175598 *tokenType = TK_FLOAT;
175599 }
175600 if( (z[i]=='e' || z[i]=='E') &&
175601 ( sqlite3Isdigit(z[i+1])
175602 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
175603 )
175604 ){
175605 i += 2;
175606 while( sqlite3Isdigit(z[i]) ){ i++; }
175607 *tokenType = TK_FLOAT;
175608 }
175609#endif
175610 while( IdChar(z[i]) ){
175611 *tokenType = TK_ILLEGAL;
175612 i++;
175613 }
175614 return i;
175615 }
175616 case CC_QUOTE2: {
175617 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
175618 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
175619 return i;
175620 }
175621 case CC_VARNUM: {
175622 *tokenType = TK_VARIABLE;
175623 for(i=1; sqlite3Isdigit(z[i]); i++){}
175624 return i;
175625 }
175626 case CC_DOLLAR:
175627 case CC_VARALPHA: {
175628 int n = 0;
175629 testcase( z[0]=='$' ); testcase( z[0]=='@' );
175630 testcase( z[0]==':' ); testcase( z[0]=='#' );
175631 *tokenType = TK_VARIABLE;
175632 for(i=1; (c=z[i])!=0; i++){
175633 if( IdChar(c) ){
175634 n++;
175635#ifndef SQLITE_OMIT_TCL_VARIABLE
175636 }else if( c=='(' && n>0 ){
175637 do{
175638 i++;
175639 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
175640 if( c==')' ){
175641 i++;
175642 }else{
175643 *tokenType = TK_ILLEGAL;
175644 }
175645 break;
175646 }else if( c==':' && z[i+1]==':' ){
175647 i++;
175648#endif
175649 }else{
175650 break;
175651 }
175652 }
175653 if( n==0 ) *tokenType = TK_ILLEGAL;
175654 return i;
175655 }
175656 case CC_KYWD0: {
175657 if( aiClass[z[1]]>CC_KYWD ){ i = 1; break; }
175658 for(i=2; aiClass[z[i]]<=CC_KYWD; i++){}
175659 if( IdChar(z[i]) ){
175660 /* This token started out using characters that can appear in keywords,
175661 ** but z[i] is a character not allowed within keywords, so this must
175662 ** be an identifier instead */
175663 i++;
175664 break;
175665 }
175666 *tokenType = TK_ID;
175667 return keywordCode(z: (char*)z, n: i, pType: tokenType);
175668 }
175669 case CC_X: {
175670#ifndef SQLITE_OMIT_BLOB_LITERAL
175671 testcase( z[0]=='x' ); testcase( z[0]=='X' );
175672 if( z[1]=='\'' ){
175673 *tokenType = TK_BLOB;
175674 for(i=2; sqlite3Isxdigit(z[i]); i++){}
175675 if( z[i]!='\'' || i%2 ){
175676 *tokenType = TK_ILLEGAL;
175677 while( z[i] && z[i]!='\'' ){ i++; }
175678 }
175679 if( z[i] ) i++;
175680 return i;
175681 }
175682#endif
175683 /* If it is not a BLOB literal, then it must be an ID, since no
175684 ** SQL keywords start with the letter 'x'. Fall through */
175685 /* no break */ deliberate_fall_through
175686 }
175687 case CC_KYWD:
175688 case CC_ID: {
175689 i = 1;
175690 break;
175691 }
175692 case CC_BOM: {
175693 if( z[1]==0xbb && z[2]==0xbf ){
175694 *tokenType = TK_SPACE;
175695 return 3;
175696 }
175697 i = 1;
175698 break;
175699 }
175700 case CC_NUL: {
175701 *tokenType = TK_ILLEGAL;
175702 return 0;
175703 }
175704 default: {
175705 *tokenType = TK_ILLEGAL;
175706 return 1;
175707 }
175708 }
175709 while( IdChar(z[i]) ){ i++; }
175710 *tokenType = TK_ID;
175711 return i;
175712}
175713
175714/*
175715** Run the parser on the given SQL string.
175716*/
175717SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql){
175718 int nErr = 0; /* Number of errors encountered */
175719 void *pEngine; /* The LEMON-generated LALR(1) parser */
175720 int n = 0; /* Length of the next token token */
175721 int tokenType; /* type of the next token */
175722 int lastTokenParsed = -1; /* type of the previous token */
175723 sqlite3 *db = pParse->db; /* The database connection */
175724 int mxSqlLen; /* Max length of an SQL string */
175725 Parse *pParentParse = 0; /* Outer parse context, if any */
175726#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
175727 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
175728#endif
175729 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
175730
175731 assert( zSql!=0 );
175732 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
175733 if( db->nVdbeActive==0 ){
175734 AtomicStore(&db->u1.isInterrupted, 0);
175735 }
175736 pParse->rc = SQLITE_OK;
175737 pParse->zTail = zSql;
175738#ifdef SQLITE_DEBUG
175739 if( db->flags & SQLITE_ParserTrace ){
175740 printf("parser: [[[%s]]]\n", zSql);
175741 sqlite3ParserTrace(stdout, "parser: ");
175742 }else{
175743 sqlite3ParserTrace(0, 0);
175744 }
175745#endif
175746#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
175747 pEngine = &sEngine;
175748 sqlite3ParserInit(yypRawParser: pEngine, pParse);
175749#else
175750 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
175751 if( pEngine==0 ){
175752 sqlite3OomFault(db);
175753 return SQLITE_NOMEM_BKPT;
175754 }
175755#endif
175756 assert( pParse->pNewTable==0 );
175757 assert( pParse->pNewTrigger==0 );
175758 assert( pParse->nVar==0 );
175759 assert( pParse->pVList==0 );
175760 pParentParse = db->pParse;
175761 db->pParse = pParse;
175762 while( 1 ){
175763 n = sqlite3GetToken(z: (u8*)zSql, tokenType: &tokenType);
175764 mxSqlLen -= n;
175765 if( mxSqlLen<0 ){
175766 pParse->rc = SQLITE_TOOBIG;
175767 pParse->nErr++;
175768 break;
175769 }
175770#ifndef SQLITE_OMIT_WINDOWFUNC
175771 if( tokenType>=TK_WINDOW ){
175772 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
175773 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
175774 );
175775#else
175776 if( tokenType>=TK_SPACE ){
175777 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
175778#endif /* SQLITE_OMIT_WINDOWFUNC */
175779 if( AtomicLoad(&db->u1.isInterrupted) ){
175780 pParse->rc = SQLITE_INTERRUPT;
175781 pParse->nErr++;
175782 break;
175783 }
175784 if( tokenType==TK_SPACE ){
175785 zSql += n;
175786 continue;
175787 }
175788 if( zSql[0]==0 ){
175789 /* Upon reaching the end of input, call the parser two more times
175790 ** with tokens TK_SEMI and 0, in that order. */
175791 if( lastTokenParsed==TK_SEMI ){
175792 tokenType = 0;
175793 }else if( lastTokenParsed==0 ){
175794 break;
175795 }else{
175796 tokenType = TK_SEMI;
175797 }
175798 n = 0;
175799#ifndef SQLITE_OMIT_WINDOWFUNC
175800 }else if( tokenType==TK_WINDOW ){
175801 assert( n==6 );
175802 tokenType = analyzeWindowKeyword(z: (const u8*)&zSql[6]);
175803 }else if( tokenType==TK_OVER ){
175804 assert( n==4 );
175805 tokenType = analyzeOverKeyword(z: (const u8*)&zSql[4], lastToken: lastTokenParsed);
175806 }else if( tokenType==TK_FILTER ){
175807 assert( n==6 );
175808 tokenType = analyzeFilterKeyword(z: (const u8*)&zSql[6], lastToken: lastTokenParsed);
175809#endif /* SQLITE_OMIT_WINDOWFUNC */
175810 }else{
175811 Token x;
175812 x.z = zSql;
175813 x.n = n;
175814 sqlite3ErrorMsg(pParse, zFormat: "unrecognized token: \"%T\"", &x);
175815 break;
175816 }
175817 }
175818 pParse->sLastToken.z = zSql;
175819 pParse->sLastToken.n = n;
175820 sqlite3Parser(yyp: pEngine, yymajor: tokenType, yyminor: pParse->sLastToken);
175821 lastTokenParsed = tokenType;
175822 zSql += n;
175823 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
175824 if( pParse->rc!=SQLITE_OK ) break;
175825 }
175826 assert( nErr==0 );
175827#ifdef YYTRACKMAXSTACKDEPTH
175828 sqlite3_mutex_enter(sqlite3MallocMutex());
175829 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
175830 sqlite3ParserStackPeak(pEngine)
175831 );
175832 sqlite3_mutex_leave(sqlite3MallocMutex());
175833#endif /* YYDEBUG */
175834#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
175835 sqlite3ParserFinalize(p: pEngine);
175836#else
175837 sqlite3ParserFree(pEngine, sqlite3_free);
175838#endif
175839 if( db->mallocFailed ){
175840 pParse->rc = SQLITE_NOMEM_BKPT;
175841 }
175842 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
175843 if( pParse->zErrMsg==0 ){
175844 pParse->zErrMsg = sqlite3MPrintf(db, zFormat: "%s", sqlite3ErrStr(pParse->rc));
175845 }
175846 sqlite3_log(iErrCode: pParse->rc, zFormat: "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
175847 nErr++;
175848 }
175849 pParse->zTail = zSql;
175850#ifndef SQLITE_OMIT_VIRTUALTABLE
175851 sqlite3_free(p: pParse->apVtabLock);
175852#endif
175853
175854 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
175855 /* If the pParse->declareVtab flag is set, do not delete any table
175856 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
175857 ** will take responsibility for freeing the Table structure.
175858 */
175859 sqlite3DeleteTable(db, pTable: pParse->pNewTable);
175860 }
175861 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
175862 sqlite3DeleteTrigger(db, pTrigger: pParse->pNewTrigger);
175863 }
175864 if( pParse->pVList ) sqlite3DbNNFreeNN(db, p: pParse->pVList);
175865 db->pParse = pParentParse;
175866 assert( nErr==0 || pParse->rc!=SQLITE_OK );
175867 return nErr;
175868}
175869
175870
175871#ifdef SQLITE_ENABLE_NORMALIZE
175872/*
175873** Insert a single space character into pStr if the current string
175874** ends with an identifier
175875*/
175876static void addSpaceSeparator(sqlite3_str *pStr){
175877 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
175878 sqlite3_str_append(pStr, " ", 1);
175879 }
175880}
175881
175882/*
175883** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
175884** the normalization in space obtained from sqlite3DbMalloc(). Or return
175885** NULL if anything goes wrong or if zSql is NULL.
175886*/
175887SQLITE_PRIVATE char *sqlite3Normalize(
175888 Vdbe *pVdbe, /* VM being reprepared */
175889 const char *zSql /* The original SQL string */
175890){
175891 sqlite3 *db; /* The database connection */
175892 int i; /* Next unread byte of zSql[] */
175893 int n; /* length of current token */
175894 int tokenType; /* type of current token */
175895 int prevType = 0; /* Previous non-whitespace token */
175896 int nParen; /* Number of nested levels of parentheses */
175897 int iStartIN; /* Start of RHS of IN operator in z[] */
175898 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
175899 u32 j; /* Bytes of normalized SQL generated so far */
175900 sqlite3_str *pStr; /* The normalized SQL string under construction */
175901
175902 db = sqlite3VdbeDb(pVdbe);
175903 tokenType = -1;
175904 nParen = iStartIN = nParenAtIN = 0;
175905 pStr = sqlite3_str_new(db);
175906 assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */
175907 for(i=0; zSql[i] && pStr->accError==0; i+=n){
175908 if( tokenType!=TK_SPACE ){
175909 prevType = tokenType;
175910 }
175911 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
175912 if( NEVER(n<=0) ) break;
175913 switch( tokenType ){
175914 case TK_SPACE: {
175915 break;
175916 }
175917 case TK_NULL: {
175918 if( prevType==TK_IS || prevType==TK_NOT ){
175919 sqlite3_str_append(pStr, " NULL", 5);
175920 break;
175921 }
175922 /* Fall through */
175923 }
175924 case TK_STRING:
175925 case TK_INTEGER:
175926 case TK_FLOAT:
175927 case TK_VARIABLE:
175928 case TK_BLOB: {
175929 sqlite3_str_append(pStr, "?", 1);
175930 break;
175931 }
175932 case TK_LP: {
175933 nParen++;
175934 if( prevType==TK_IN ){
175935 iStartIN = pStr->nChar;
175936 nParenAtIN = nParen;
175937 }
175938 sqlite3_str_append(pStr, "(", 1);
175939 break;
175940 }
175941 case TK_RP: {
175942 if( iStartIN>0 && nParen==nParenAtIN ){
175943 assert( pStr->nChar>=(u32)iStartIN );
175944 pStr->nChar = iStartIN+1;
175945 sqlite3_str_append(pStr, "?,?,?", 5);
175946 iStartIN = 0;
175947 }
175948 nParen--;
175949 sqlite3_str_append(pStr, ")", 1);
175950 break;
175951 }
175952 case TK_ID: {
175953 iStartIN = 0;
175954 j = pStr->nChar;
175955 if( sqlite3Isquote(zSql[i]) ){
175956 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
175957 int nId;
175958 int eType = 0;
175959 if( zId==0 ) break;
175960 sqlite3Dequote(zId);
175961 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
175962 sqlite3_str_append(pStr, "?", 1);
175963 sqlite3DbFree(db, zId);
175964 break;
175965 }
175966 nId = sqlite3Strlen30(zId);
175967 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
175968 addSpaceSeparator(pStr);
175969 sqlite3_str_append(pStr, zId, nId);
175970 }else{
175971 sqlite3_str_appendf(pStr, "\"%w\"", zId);
175972 }
175973 sqlite3DbFree(db, zId);
175974 }else{
175975 addSpaceSeparator(pStr);
175976 sqlite3_str_append(pStr, zSql+i, n);
175977 }
175978 while( j<pStr->nChar ){
175979 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
175980 j++;
175981 }
175982 break;
175983 }
175984 case TK_SELECT: {
175985 iStartIN = 0;
175986 /* fall through */
175987 }
175988 default: {
175989 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
175990 j = pStr->nChar;
175991 sqlite3_str_append(pStr, zSql+i, n);
175992 while( j<pStr->nChar ){
175993 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
175994 j++;
175995 }
175996 break;
175997 }
175998 }
175999 }
176000 if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
176001 return sqlite3_str_finish(pStr);
176002}
176003#endif /* SQLITE_ENABLE_NORMALIZE */
176004
176005/************** End of tokenize.c ********************************************/
176006/************** Begin file complete.c ****************************************/
176007/*
176008** 2001 September 15
176009**
176010** The author disclaims copyright to this source code. In place of
176011** a legal notice, here is a blessing:
176012**
176013** May you do good and not evil.
176014** May you find forgiveness for yourself and forgive others.
176015** May you share freely, never taking more than you give.
176016**
176017*************************************************************************
176018** An tokenizer for SQL
176019**
176020** This file contains C code that implements the sqlite3_complete() API.
176021** This code used to be part of the tokenizer.c source file. But by
176022** separating it out, the code will be automatically omitted from
176023** static links that do not use it.
176024*/
176025/* #include "sqliteInt.h" */
176026#ifndef SQLITE_OMIT_COMPLETE
176027
176028/*
176029** This is defined in tokenize.c. We just have to import the definition.
176030*/
176031#ifndef SQLITE_AMALGAMATION
176032#ifdef SQLITE_ASCII
176033#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
176034#endif
176035#ifdef SQLITE_EBCDIC
176036SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
176037#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
176038#endif
176039#endif /* SQLITE_AMALGAMATION */
176040
176041
176042/*
176043** Token types used by the sqlite3_complete() routine. See the header
176044** comments on that procedure for additional information.
176045*/
176046#define tkSEMI 0
176047#define tkWS 1
176048#define tkOTHER 2
176049#ifndef SQLITE_OMIT_TRIGGER
176050#define tkEXPLAIN 3
176051#define tkCREATE 4
176052#define tkTEMP 5
176053#define tkTRIGGER 6
176054#define tkEND 7
176055#endif
176056
176057/*
176058** Return TRUE if the given SQL string ends in a semicolon.
176059**
176060** Special handling is require for CREATE TRIGGER statements.
176061** Whenever the CREATE TRIGGER keywords are seen, the statement
176062** must end with ";END;".
176063**
176064** This implementation uses a state machine with 8 states:
176065**
176066** (0) INVALID We have not yet seen a non-whitespace character.
176067**
176068** (1) START At the beginning or end of an SQL statement. This routine
176069** returns 1 if it ends in the START state and 0 if it ends
176070** in any other state.
176071**
176072** (2) NORMAL We are in the middle of statement which ends with a single
176073** semicolon.
176074**
176075** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
176076** a statement.
176077**
176078** (4) CREATE The keyword CREATE has been seen at the beginning of a
176079** statement, possibly preceded by EXPLAIN and/or followed by
176080** TEMP or TEMPORARY
176081**
176082** (5) TRIGGER We are in the middle of a trigger definition that must be
176083** ended by a semicolon, the keyword END, and another semicolon.
176084**
176085** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
176086** the end of a trigger definition.
176087**
176088** (7) END We've seen the ";END" of the ";END;" that occurs at the end
176089** of a trigger definition.
176090**
176091** Transitions between states above are determined by tokens extracted
176092** from the input. The following tokens are significant:
176093**
176094** (0) tkSEMI A semicolon.
176095** (1) tkWS Whitespace.
176096** (2) tkOTHER Any other SQL token.
176097** (3) tkEXPLAIN The "explain" keyword.
176098** (4) tkCREATE The "create" keyword.
176099** (5) tkTEMP The "temp" or "temporary" keyword.
176100** (6) tkTRIGGER The "trigger" keyword.
176101** (7) tkEND The "end" keyword.
176102**
176103** Whitespace never causes a state transition and is always ignored.
176104** This means that a SQL string of all whitespace is invalid.
176105**
176106** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
176107** to recognize the end of a trigger can be omitted. All we have to do
176108** is look for a semicolon that is not part of an string or comment.
176109*/
176110SQLITE_API int sqlite3_complete(const char *zSql){
176111 u8 state = 0; /* Current state, using numbers defined in header comment */
176112 u8 token; /* Value of the next token */
176113
176114#ifndef SQLITE_OMIT_TRIGGER
176115 /* A complex statement machine used to detect the end of a CREATE TRIGGER
176116 ** statement. This is the normal case.
176117 */
176118 static const u8 trans[8][8] = {
176119 /* Token: */
176120 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
176121 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
176122 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
176123 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
176124 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
176125 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
176126 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
176127 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
176128 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
176129 };
176130#else
176131 /* If triggers are not supported by this compile then the statement machine
176132 ** used to detect the end of a statement is much simpler
176133 */
176134 static const u8 trans[3][3] = {
176135 /* Token: */
176136 /* State: ** SEMI WS OTHER */
176137 /* 0 INVALID: */ { 1, 0, 2, },
176138 /* 1 START: */ { 1, 1, 2, },
176139 /* 2 NORMAL: */ { 1, 2, 2, },
176140 };
176141#endif /* SQLITE_OMIT_TRIGGER */
176142
176143#ifdef SQLITE_ENABLE_API_ARMOR
176144 if( zSql==0 ){
176145 (void)SQLITE_MISUSE_BKPT;
176146 return 0;
176147 }
176148#endif
176149
176150 while( *zSql ){
176151 switch( *zSql ){
176152 case ';': { /* A semicolon */
176153 token = tkSEMI;
176154 break;
176155 }
176156 case ' ':
176157 case '\r':
176158 case '\t':
176159 case '\n':
176160 case '\f': { /* White space is ignored */
176161 token = tkWS;
176162 break;
176163 }
176164 case '/': { /* C-style comments */
176165 if( zSql[1]!='*' ){
176166 token = tkOTHER;
176167 break;
176168 }
176169 zSql += 2;
176170 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
176171 if( zSql[0]==0 ) return 0;
176172 zSql++;
176173 token = tkWS;
176174 break;
176175 }
176176 case '-': { /* SQL-style comments from "--" to end of line */
176177 if( zSql[1]!='-' ){
176178 token = tkOTHER;
176179 break;
176180 }
176181 while( *zSql && *zSql!='\n' ){ zSql++; }
176182 if( *zSql==0 ) return state==1;
176183 token = tkWS;
176184 break;
176185 }
176186 case '[': { /* Microsoft-style identifiers in [...] */
176187 zSql++;
176188 while( *zSql && *zSql!=']' ){ zSql++; }
176189 if( *zSql==0 ) return 0;
176190 token = tkOTHER;
176191 break;
176192 }
176193 case '`': /* Grave-accent quoted symbols used by MySQL */
176194 case '"': /* single- and double-quoted strings */
176195 case '\'': {
176196 int c = *zSql;
176197 zSql++;
176198 while( *zSql && *zSql!=c ){ zSql++; }
176199 if( *zSql==0 ) return 0;
176200 token = tkOTHER;
176201 break;
176202 }
176203 default: {
176204#ifdef SQLITE_EBCDIC
176205 unsigned char c;
176206#endif
176207 if( IdChar((u8)*zSql) ){
176208 /* Keywords and unquoted identifiers */
176209 int nId;
176210 for(nId=1; IdChar(zSql[nId]); nId++){}
176211#ifdef SQLITE_OMIT_TRIGGER
176212 token = tkOTHER;
176213#else
176214 switch( *zSql ){
176215 case 'c': case 'C': {
176216 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
176217 token = tkCREATE;
176218 }else{
176219 token = tkOTHER;
176220 }
176221 break;
176222 }
176223 case 't': case 'T': {
176224 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
176225 token = tkTRIGGER;
176226 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
176227 token = tkTEMP;
176228 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
176229 token = tkTEMP;
176230 }else{
176231 token = tkOTHER;
176232 }
176233 break;
176234 }
176235 case 'e': case 'E': {
176236 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
176237 token = tkEND;
176238 }else
176239#ifndef SQLITE_OMIT_EXPLAIN
176240 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
176241 token = tkEXPLAIN;
176242 }else
176243#endif
176244 {
176245 token = tkOTHER;
176246 }
176247 break;
176248 }
176249 default: {
176250 token = tkOTHER;
176251 break;
176252 }
176253 }
176254#endif /* SQLITE_OMIT_TRIGGER */
176255 zSql += nId-1;
176256 }else{
176257 /* Operators and special symbols */
176258 token = tkOTHER;
176259 }
176260 break;
176261 }
176262 }
176263 state = trans[state][token];
176264 zSql++;
176265 }
176266 return state==1;
176267}
176268
176269#ifndef SQLITE_OMIT_UTF16
176270/*
176271** This routine is the same as the sqlite3_complete() routine described
176272** above, except that the parameter is required to be UTF-16 encoded, not
176273** UTF-8.
176274*/
176275SQLITE_API int sqlite3_complete16(const void *zSql){
176276 sqlite3_value *pVal;
176277 char const *zSql8;
176278 int rc;
176279
176280#ifndef SQLITE_OMIT_AUTOINIT
176281 rc = sqlite3_initialize();
176282 if( rc ) return rc;
176283#endif
176284 pVal = sqlite3ValueNew(0);
176285 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
176286 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
176287 if( zSql8 ){
176288 rc = sqlite3_complete(zSql8);
176289 }else{
176290 rc = SQLITE_NOMEM_BKPT;
176291 }
176292 sqlite3ValueFree(pVal);
176293 return rc & 0xff;
176294}
176295#endif /* SQLITE_OMIT_UTF16 */
176296#endif /* SQLITE_OMIT_COMPLETE */
176297
176298/************** End of complete.c ********************************************/
176299/************** Begin file main.c ********************************************/
176300/*
176301** 2001 September 15
176302**
176303** The author disclaims copyright to this source code. In place of
176304** a legal notice, here is a blessing:
176305**
176306** May you do good and not evil.
176307** May you find forgiveness for yourself and forgive others.
176308** May you share freely, never taking more than you give.
176309**
176310*************************************************************************
176311** Main file for the SQLite library. The routines in this file
176312** implement the programmer interface to the library. Routines in
176313** other files are for internal use by SQLite and should not be
176314** accessed by users of the library.
176315*/
176316/* #include "sqliteInt.h" */
176317
176318#ifdef SQLITE_ENABLE_FTS3
176319/************** Include fts3.h in the middle of main.c ***********************/
176320/************** Begin file fts3.h ********************************************/
176321/*
176322** 2006 Oct 10
176323**
176324** The author disclaims copyright to this source code. In place of
176325** a legal notice, here is a blessing:
176326**
176327** May you do good and not evil.
176328** May you find forgiveness for yourself and forgive others.
176329** May you share freely, never taking more than you give.
176330**
176331******************************************************************************
176332**
176333** This header file is used by programs that want to link against the
176334** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
176335*/
176336/* #include "sqlite3.h" */
176337
176338#if 0
176339extern "C" {
176340#endif /* __cplusplus */
176341
176342SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
176343
176344#if 0
176345} /* extern "C" */
176346#endif /* __cplusplus */
176347
176348/************** End of fts3.h ************************************************/
176349/************** Continuing where we left off in main.c ***********************/
176350#endif
176351#ifdef SQLITE_ENABLE_RTREE
176352/************** Include rtree.h in the middle of main.c **********************/
176353/************** Begin file rtree.h *******************************************/
176354/*
176355** 2008 May 26
176356**
176357** The author disclaims copyright to this source code. In place of
176358** a legal notice, here is a blessing:
176359**
176360** May you do good and not evil.
176361** May you find forgiveness for yourself and forgive others.
176362** May you share freely, never taking more than you give.
176363**
176364******************************************************************************
176365**
176366** This header file is used by programs that want to link against the
176367** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
176368*/
176369/* #include "sqlite3.h" */
176370
176371#ifdef SQLITE_OMIT_VIRTUALTABLE
176372# undef SQLITE_ENABLE_RTREE
176373#endif
176374
176375#if 0
176376extern "C" {
176377#endif /* __cplusplus */
176378
176379SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
176380
176381#if 0
176382} /* extern "C" */
176383#endif /* __cplusplus */
176384
176385/************** End of rtree.h ***********************************************/
176386/************** Continuing where we left off in main.c ***********************/
176387#endif
176388#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
176389/************** Include sqliteicu.h in the middle of main.c ******************/
176390/************** Begin file sqliteicu.h ***************************************/
176391/*
176392** 2008 May 26
176393**
176394** The author disclaims copyright to this source code. In place of
176395** a legal notice, here is a blessing:
176396**
176397** May you do good and not evil.
176398** May you find forgiveness for yourself and forgive others.
176399** May you share freely, never taking more than you give.
176400**
176401******************************************************************************
176402**
176403** This header file is used by programs that want to link against the
176404** ICU extension. All it does is declare the sqlite3IcuInit() interface.
176405*/
176406/* #include "sqlite3.h" */
176407
176408#if 0
176409extern "C" {
176410#endif /* __cplusplus */
176411
176412SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
176413
176414#if 0
176415} /* extern "C" */
176416#endif /* __cplusplus */
176417
176418/************** End of sqliteicu.h *******************************************/
176419/************** Continuing where we left off in main.c ***********************/
176420#endif
176421
176422/*
176423** This is an extension initializer that is a no-op and always
176424** succeeds, except that it fails if the fault-simulation is set
176425** to 500.
176426*/
176427static int sqlite3TestExtInit(sqlite3 *db){
176428 (void)db;
176429 return sqlite3FaultSim(iTest: 500);
176430}
176431
176432
176433/*
176434** Forward declarations of external module initializer functions
176435** for modules that need them.
176436*/
176437#ifdef SQLITE_ENABLE_FTS5
176438SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
176439#endif
176440#ifdef SQLITE_ENABLE_STMTVTAB
176441SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
176442#endif
176443
176444/*
176445** An array of pointers to extension initializer functions for
176446** built-in extensions.
176447*/
176448static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
176449#ifdef SQLITE_ENABLE_FTS3
176450 sqlite3Fts3Init,
176451#endif
176452#ifdef SQLITE_ENABLE_FTS5
176453 sqlite3Fts5Init,
176454#endif
176455#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
176456 sqlite3IcuInit,
176457#endif
176458#ifdef SQLITE_ENABLE_RTREE
176459 sqlite3RtreeInit,
176460#endif
176461#ifdef SQLITE_ENABLE_DBPAGE_VTAB
176462 sqlite3DbpageRegister,
176463#endif
176464#ifdef SQLITE_ENABLE_DBSTAT_VTAB
176465 sqlite3DbstatRegister,
176466#endif
176467 sqlite3TestExtInit,
176468#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
176469 sqlite3JsonTableFunctions,
176470#endif
176471#ifdef SQLITE_ENABLE_STMTVTAB
176472 sqlite3StmtVtabInit,
176473#endif
176474#ifdef SQLITE_ENABLE_BYTECODE_VTAB
176475 sqlite3VdbeBytecodeVtabInit,
176476#endif
176477};
176478
176479#ifndef SQLITE_AMALGAMATION
176480/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
176481** contains the text of SQLITE_VERSION macro.
176482*/
176483SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
176484#endif
176485
176486/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
176487** a pointer to the to the sqlite3_version[] string constant.
176488*/
176489SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
176490
176491/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
176492** pointer to a string constant whose value is the same as the
176493** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
176494** an edited copy of the amalgamation, then the last four characters of
176495** the hash might be different from SQLITE_SOURCE_ID.
176496*/
176497/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
176498
176499/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
176500** returns an integer equal to SQLITE_VERSION_NUMBER.
176501*/
176502SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
176503
176504/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
176505** zero if and only if SQLite was compiled with mutexing code omitted due to
176506** the SQLITE_THREADSAFE compile-time option being set to 0.
176507*/
176508SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
176509
176510/*
176511** When compiling the test fixture or with debugging enabled (on Win32),
176512** this variable being set to non-zero will cause OSTRACE macros to emit
176513** extra diagnostic information.
176514*/
176515#ifdef SQLITE_HAVE_OS_TRACE
176516# ifndef SQLITE_DEBUG_OS_TRACE
176517# define SQLITE_DEBUG_OS_TRACE 0
176518# endif
176519 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
176520#endif
176521
176522#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
176523/*
176524** If the following function pointer is not NULL and if
176525** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
176526** I/O active are written using this function. These messages
176527** are intended for debugging activity only.
176528*/
176529SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
176530#endif
176531
176532/*
176533** If the following global variable points to a string which is the
176534** name of a directory, then that directory will be used to store
176535** temporary files.
176536**
176537** See also the "PRAGMA temp_store_directory" SQL command.
176538*/
176539SQLITE_API char *sqlite3_temp_directory = 0;
176540
176541/*
176542** If the following global variable points to a string which is the
176543** name of a directory, then that directory will be used to store
176544** all database files specified with a relative pathname.
176545**
176546** See also the "PRAGMA data_store_directory" SQL command.
176547*/
176548SQLITE_API char *sqlite3_data_directory = 0;
176549
176550/*
176551** Initialize SQLite.
176552**
176553** This routine must be called to initialize the memory allocation,
176554** VFS, and mutex subsystems prior to doing any serious work with
176555** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
176556** this routine will be called automatically by key routines such as
176557** sqlite3_open().
176558**
176559** This routine is a no-op except on its very first call for the process,
176560** or for the first call after a call to sqlite3_shutdown.
176561**
176562** The first thread to call this routine runs the initialization to
176563** completion. If subsequent threads call this routine before the first
176564** thread has finished the initialization process, then the subsequent
176565** threads must block until the first thread finishes with the initialization.
176566**
176567** The first thread might call this routine recursively. Recursive
176568** calls to this routine should not block, of course. Otherwise the
176569** initialization process would never complete.
176570**
176571** Let X be the first thread to enter this routine. Let Y be some other
176572** thread. Then while the initial invocation of this routine by X is
176573** incomplete, it is required that:
176574**
176575** * Calls to this routine from Y must block until the outer-most
176576** call by X completes.
176577**
176578** * Recursive calls to this routine from thread X return immediately
176579** without blocking.
176580*/
176581SQLITE_API int sqlite3_initialize(void){
176582 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
176583 int rc; /* Result code */
176584#ifdef SQLITE_EXTRA_INIT
176585 int bRunExtraInit = 0; /* Extra initialization needed */
176586#endif
176587
176588#ifdef SQLITE_OMIT_WSD
176589 rc = sqlite3_wsd_init(4096, 24);
176590 if( rc!=SQLITE_OK ){
176591 return rc;
176592 }
176593#endif
176594
176595 /* If the following assert() fails on some obscure processor/compiler
176596 ** combination, the work-around is to set the correct pointer
176597 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
176598 assert( SQLITE_PTRSIZE==sizeof(char*) );
176599
176600 /* If SQLite is already completely initialized, then this call
176601 ** to sqlite3_initialize() should be a no-op. But the initialization
176602 ** must be complete. So isInit must not be set until the very end
176603 ** of this routine.
176604 */
176605 if( sqlite3GlobalConfig.isInit ){
176606 sqlite3MemoryBarrier();
176607 return SQLITE_OK;
176608 }
176609
176610 /* Make sure the mutex subsystem is initialized. If unable to
176611 ** initialize the mutex subsystem, return early with the error.
176612 ** If the system is so sick that we are unable to allocate a mutex,
176613 ** there is not much SQLite is going to be able to do.
176614 **
176615 ** The mutex subsystem must take care of serializing its own
176616 ** initialization.
176617 */
176618 rc = sqlite3MutexInit();
176619 if( rc ) return rc;
176620
176621 /* Initialize the malloc() system and the recursive pInitMutex mutex.
176622 ** This operation is protected by the STATIC_MAIN mutex. Note that
176623 ** MutexAlloc() is called for a static mutex prior to initializing the
176624 ** malloc subsystem - this implies that the allocation of a static
176625 ** mutex must not require support from the malloc subsystem.
176626 */
176627 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
176628 sqlite3_mutex_enter(p: pMainMtx);
176629 sqlite3GlobalConfig.isMutexInit = 1;
176630 if( !sqlite3GlobalConfig.isMallocInit ){
176631 rc = sqlite3MallocInit();
176632 }
176633 if( rc==SQLITE_OK ){
176634 sqlite3GlobalConfig.isMallocInit = 1;
176635 if( !sqlite3GlobalConfig.pInitMutex ){
176636 sqlite3GlobalConfig.pInitMutex =
176637 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
176638 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
176639 rc = SQLITE_NOMEM_BKPT;
176640 }
176641 }
176642 }
176643 if( rc==SQLITE_OK ){
176644 sqlite3GlobalConfig.nRefInitMutex++;
176645 }
176646 sqlite3_mutex_leave(p: pMainMtx);
176647
176648 /* If rc is not SQLITE_OK at this point, then either the malloc
176649 ** subsystem could not be initialized or the system failed to allocate
176650 ** the pInitMutex mutex. Return an error in either case. */
176651 if( rc!=SQLITE_OK ){
176652 return rc;
176653 }
176654
176655 /* Do the rest of the initialization under the recursive mutex so
176656 ** that we will be able to handle recursive calls into
176657 ** sqlite3_initialize(). The recursive calls normally come through
176658 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
176659 ** recursive calls might also be possible.
176660 **
176661 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
176662 ** to the xInit method, so the xInit method need not be threadsafe.
176663 **
176664 ** The following mutex is what serializes access to the appdef pcache xInit
176665 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
176666 ** call to sqlite3PcacheInitialize().
176667 */
176668 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
176669 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
176670 sqlite3GlobalConfig.inProgress = 1;
176671#ifdef SQLITE_ENABLE_SQLLOG
176672 {
176673 extern void sqlite3_init_sqllog(void);
176674 sqlite3_init_sqllog();
176675 }
176676#endif
176677 memset(s: &sqlite3BuiltinFunctions, c: 0, n: sizeof(sqlite3BuiltinFunctions));
176678 sqlite3RegisterBuiltinFunctions();
176679 if( sqlite3GlobalConfig.isPCacheInit==0 ){
176680 rc = sqlite3PcacheInitialize();
176681 }
176682 if( rc==SQLITE_OK ){
176683 sqlite3GlobalConfig.isPCacheInit = 1;
176684 rc = sqlite3OsInit();
176685 }
176686#ifndef SQLITE_OMIT_DESERIALIZE
176687 if( rc==SQLITE_OK ){
176688 rc = sqlite3MemdbInit();
176689 }
176690#endif
176691 if( rc==SQLITE_OK ){
176692 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
176693 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
176694 sqlite3MemoryBarrier();
176695 sqlite3GlobalConfig.isInit = 1;
176696#ifdef SQLITE_EXTRA_INIT
176697 bRunExtraInit = 1;
176698#endif
176699 }
176700 sqlite3GlobalConfig.inProgress = 0;
176701 }
176702 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
176703
176704 /* Go back under the static mutex and clean up the recursive
176705 ** mutex to prevent a resource leak.
176706 */
176707 sqlite3_mutex_enter(p: pMainMtx);
176708 sqlite3GlobalConfig.nRefInitMutex--;
176709 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
176710 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
176711 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
176712 sqlite3GlobalConfig.pInitMutex = 0;
176713 }
176714 sqlite3_mutex_leave(p: pMainMtx);
176715
176716 /* The following is just a sanity check to make sure SQLite has
176717 ** been compiled correctly. It is important to run this code, but
176718 ** we don't want to run it too often and soak up CPU cycles for no
176719 ** reason. So we run it once during initialization.
176720 */
176721#ifndef NDEBUG
176722#ifndef SQLITE_OMIT_FLOATING_POINT
176723 /* This section of code's only "output" is via assert() statements. */
176724 if( rc==SQLITE_OK ){
176725 u64 x = (((u64)1)<<63)-1;
176726 double y;
176727 assert(sizeof(x)==8);
176728 assert(sizeof(x)==sizeof(y));
176729 memcpy(&y, &x, 8);
176730 assert( sqlite3IsNaN(y) );
176731 }
176732#endif
176733#endif
176734
176735 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
176736 ** compile-time option.
176737 */
176738#ifdef SQLITE_EXTRA_INIT
176739 if( bRunExtraInit ){
176740 int SQLITE_EXTRA_INIT(const char*);
176741 rc = SQLITE_EXTRA_INIT(0);
176742 }
176743#endif
176744
176745 return rc;
176746}
176747
176748/*
176749** Undo the effects of sqlite3_initialize(). Must not be called while
176750** there are outstanding database connections or memory allocations or
176751** while any part of SQLite is otherwise in use in any thread. This
176752** routine is not threadsafe. But it is safe to invoke this routine
176753** on when SQLite is already shut down. If SQLite is already shut down
176754** when this routine is invoked, then this routine is a harmless no-op.
176755*/
176756SQLITE_API int sqlite3_shutdown(void){
176757#ifdef SQLITE_OMIT_WSD
176758 int rc = sqlite3_wsd_init(4096, 24);
176759 if( rc!=SQLITE_OK ){
176760 return rc;
176761 }
176762#endif
176763
176764 if( sqlite3GlobalConfig.isInit ){
176765#ifdef SQLITE_EXTRA_SHUTDOWN
176766 void SQLITE_EXTRA_SHUTDOWN(void);
176767 SQLITE_EXTRA_SHUTDOWN();
176768#endif
176769 sqlite3_os_end();
176770 sqlite3_reset_auto_extension();
176771 sqlite3GlobalConfig.isInit = 0;
176772 }
176773 if( sqlite3GlobalConfig.isPCacheInit ){
176774 sqlite3PcacheShutdown();
176775 sqlite3GlobalConfig.isPCacheInit = 0;
176776 }
176777 if( sqlite3GlobalConfig.isMallocInit ){
176778 sqlite3MallocEnd();
176779 sqlite3GlobalConfig.isMallocInit = 0;
176780
176781#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
176782 /* The heap subsystem has now been shutdown and these values are supposed
176783 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
176784 ** which would rely on that heap subsystem; therefore, make sure these
176785 ** values cannot refer to heap memory that was just invalidated when the
176786 ** heap subsystem was shutdown. This is only done if the current call to
176787 ** this function resulted in the heap subsystem actually being shutdown.
176788 */
176789 sqlite3_data_directory = 0;
176790 sqlite3_temp_directory = 0;
176791#endif
176792 }
176793 if( sqlite3GlobalConfig.isMutexInit ){
176794 sqlite3MutexEnd();
176795 sqlite3GlobalConfig.isMutexInit = 0;
176796 }
176797
176798 return SQLITE_OK;
176799}
176800
176801/*
176802** This API allows applications to modify the global configuration of
176803** the SQLite library at run-time.
176804**
176805** This routine should only be called when there are no outstanding
176806** database connections or memory allocations. This routine is not
176807** threadsafe. Failure to heed these warnings can lead to unpredictable
176808** behavior.
176809*/
176810SQLITE_API int sqlite3_config(int op, ...){
176811 va_list ap;
176812 int rc = SQLITE_OK;
176813
176814 /* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while
176815 ** the SQLite library is in use. Except, a few selected opcodes
176816 ** are allowed.
176817 */
176818 if( sqlite3GlobalConfig.isInit ){
176819 static const u64 mAnytimeConfigOption = 0
176820 | MASKBIT64( SQLITE_CONFIG_LOG )
176821 | MASKBIT64( SQLITE_CONFIG_PCACHE_HDRSZ )
176822 ;
176823 if( op<0 || op>63 || (MASKBIT64(op) & mAnytimeConfigOption)==0 ){
176824 return SQLITE_MISUSE_BKPT;
176825 }
176826 testcase( op==SQLITE_CONFIG_LOG );
176827 testcase( op==SQLITE_CONFIG_PCACHE_HDRSZ );
176828 }
176829
176830 va_start(ap, op);
176831 switch( op ){
176832
176833 /* Mutex configuration options are only available in a threadsafe
176834 ** compile.
176835 */
176836#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
176837 case SQLITE_CONFIG_SINGLETHREAD: {
176838 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
176839 ** Single-thread. */
176840 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
176841 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
176842 break;
176843 }
176844#endif
176845#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
176846 case SQLITE_CONFIG_MULTITHREAD: {
176847 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
176848 ** Multi-thread. */
176849 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
176850 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
176851 break;
176852 }
176853#endif
176854#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
176855 case SQLITE_CONFIG_SERIALIZED: {
176856 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
176857 ** Serialized. */
176858 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
176859 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
176860 break;
176861 }
176862#endif
176863#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
176864 case SQLITE_CONFIG_MUTEX: {
176865 /* Specify an alternative mutex implementation */
176866 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
176867 break;
176868 }
176869#endif
176870#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
176871 case SQLITE_CONFIG_GETMUTEX: {
176872 /* Retrieve the current mutex implementation */
176873 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
176874 break;
176875 }
176876#endif
176877
176878 case SQLITE_CONFIG_MALLOC: {
176879 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
176880 ** single argument which is a pointer to an instance of the
176881 ** sqlite3_mem_methods structure. The argument specifies alternative
176882 ** low-level memory allocation routines to be used in place of the memory
176883 ** allocation routines built into SQLite. */
176884 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
176885 break;
176886 }
176887 case SQLITE_CONFIG_GETMALLOC: {
176888 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
176889 ** single argument which is a pointer to an instance of the
176890 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
176891 ** filled with the currently defined memory allocation routines. */
176892 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
176893 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
176894 break;
176895 }
176896 case SQLITE_CONFIG_MEMSTATUS: {
176897 assert( !sqlite3GlobalConfig.isInit ); /* Cannot change at runtime */
176898 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
176899 ** single argument of type int, interpreted as a boolean, which enables
176900 ** or disables the collection of memory allocation statistics. */
176901 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
176902 break;
176903 }
176904 case SQLITE_CONFIG_SMALL_MALLOC: {
176905 sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
176906 break;
176907 }
176908 case SQLITE_CONFIG_PAGECACHE: {
176909 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
176910 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
176911 ** the size of each page cache line (sz), and the number of cache lines
176912 ** (N). */
176913 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
176914 sqlite3GlobalConfig.szPage = va_arg(ap, int);
176915 sqlite3GlobalConfig.nPage = va_arg(ap, int);
176916 break;
176917 }
176918 case SQLITE_CONFIG_PCACHE_HDRSZ: {
176919 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
176920 ** a single parameter which is a pointer to an integer and writes into
176921 ** that integer the number of extra bytes per page required for each page
176922 ** in SQLITE_CONFIG_PAGECACHE. */
176923 *va_arg(ap, int*) =
176924 sqlite3HeaderSizeBtree() +
176925 sqlite3HeaderSizePcache() +
176926 sqlite3HeaderSizePcache1();
176927 break;
176928 }
176929
176930 case SQLITE_CONFIG_PCACHE: {
176931 /* no-op */
176932 break;
176933 }
176934 case SQLITE_CONFIG_GETPCACHE: {
176935 /* now an error */
176936 rc = SQLITE_ERROR;
176937 break;
176938 }
176939
176940 case SQLITE_CONFIG_PCACHE2: {
176941 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
176942 ** single argument which is a pointer to an sqlite3_pcache_methods2
176943 ** object. This object specifies the interface to a custom page cache
176944 ** implementation. */
176945 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
176946 break;
176947 }
176948 case SQLITE_CONFIG_GETPCACHE2: {
176949 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
176950 ** single argument which is a pointer to an sqlite3_pcache_methods2
176951 ** object. SQLite copies of the current page cache implementation into
176952 ** that object. */
176953 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
176954 sqlite3PCacheSetDefault();
176955 }
176956 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
176957 break;
176958 }
176959
176960/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
176961** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
176962** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
176963#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
176964 case SQLITE_CONFIG_HEAP: {
176965 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
176966 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
176967 ** number of bytes in the memory buffer, and the minimum allocation size.
176968 */
176969 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
176970 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
176971 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
176972
176973 if( sqlite3GlobalConfig.mnReq<1 ){
176974 sqlite3GlobalConfig.mnReq = 1;
176975 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
176976 /* cap min request size at 2^12 */
176977 sqlite3GlobalConfig.mnReq = (1<<12);
176978 }
176979
176980 if( sqlite3GlobalConfig.pHeap==0 ){
176981 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
176982 ** is NULL, then SQLite reverts to using its default memory allocator
176983 ** (the system malloc() implementation), undoing any prior invocation of
176984 ** SQLITE_CONFIG_MALLOC.
176985 **
176986 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
176987 ** revert to its default implementation when sqlite3_initialize() is run
176988 */
176989 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
176990 }else{
176991 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
176992 ** alternative memory allocator is engaged to handle all of SQLites
176993 ** memory allocation needs. */
176994#ifdef SQLITE_ENABLE_MEMSYS3
176995 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
176996#endif
176997#ifdef SQLITE_ENABLE_MEMSYS5
176998 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
176999#endif
177000 }
177001 break;
177002 }
177003#endif
177004
177005 case SQLITE_CONFIG_LOOKASIDE: {
177006 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
177007 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
177008 break;
177009 }
177010
177011 /* Record a pointer to the logger function and its first argument.
177012 ** The default is NULL. Logging is disabled if the function pointer is
177013 ** NULL.
177014 */
177015 case SQLITE_CONFIG_LOG: {
177016 /* MSVC is picky about pulling func ptrs from va lists.
177017 ** http://support.microsoft.com/kb/47961
177018 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
177019 */
177020 typedef void(*LOGFUNC_t)(void*,int,const char*);
177021 LOGFUNC_t xLog = va_arg(ap, LOGFUNC_t);
177022 void *pLogArg = va_arg(ap, void*);
177023 AtomicStore(&sqlite3GlobalConfig.xLog, xLog);
177024 AtomicStore(&sqlite3GlobalConfig.pLogArg, pLogArg);
177025 break;
177026 }
177027
177028 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
177029 ** can be changed at start-time using the
177030 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
177031 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
177032 */
177033 case SQLITE_CONFIG_URI: {
177034 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
177035 ** argument of type int. If non-zero, then URI handling is globally
177036 ** enabled. If the parameter is zero, then URI handling is globally
177037 ** disabled. */
177038 int bOpenUri = va_arg(ap, int);
177039 AtomicStore(&sqlite3GlobalConfig.bOpenUri, bOpenUri);
177040 break;
177041 }
177042
177043 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
177044 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
177045 ** option takes a single integer argument which is interpreted as a
177046 ** boolean in order to enable or disable the use of covering indices for
177047 ** full table scans in the query optimizer. */
177048 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
177049 break;
177050 }
177051
177052#ifdef SQLITE_ENABLE_SQLLOG
177053 case SQLITE_CONFIG_SQLLOG: {
177054 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
177055 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
177056 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
177057 break;
177058 }
177059#endif
177060
177061 case SQLITE_CONFIG_MMAP_SIZE: {
177062 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
177063 ** integer (sqlite3_int64) values that are the default mmap size limit
177064 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
177065 ** mmap size limit. */
177066 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
177067 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
177068 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
177069 ** negative, then that argument is changed to its compile-time default.
177070 **
177071 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
177072 ** silently truncated if necessary so that it does not exceed the
177073 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
177074 ** compile-time option.
177075 */
177076 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
177077 mxMmap = SQLITE_MAX_MMAP_SIZE;
177078 }
177079 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
177080 if( szMmap>mxMmap) szMmap = mxMmap;
177081 sqlite3GlobalConfig.mxMmap = mxMmap;
177082 sqlite3GlobalConfig.szMmap = szMmap;
177083 break;
177084 }
177085
177086#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
177087 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
177088 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
177089 ** unsigned integer value that specifies the maximum size of the created
177090 ** heap. */
177091 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
177092 break;
177093 }
177094#endif
177095
177096 case SQLITE_CONFIG_PMASZ: {
177097 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
177098 break;
177099 }
177100
177101 case SQLITE_CONFIG_STMTJRNL_SPILL: {
177102 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
177103 break;
177104 }
177105
177106#ifdef SQLITE_ENABLE_SORTER_REFERENCES
177107 case SQLITE_CONFIG_SORTERREF_SIZE: {
177108 int iVal = va_arg(ap, int);
177109 if( iVal<0 ){
177110 iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
177111 }
177112 sqlite3GlobalConfig.szSorterRef = (u32)iVal;
177113 break;
177114 }
177115#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
177116
177117#ifndef SQLITE_OMIT_DESERIALIZE
177118 case SQLITE_CONFIG_MEMDB_MAXSIZE: {
177119 sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64);
177120 break;
177121 }
177122#endif /* SQLITE_OMIT_DESERIALIZE */
177123
177124 default: {
177125 rc = SQLITE_ERROR;
177126 break;
177127 }
177128 }
177129 va_end(ap);
177130 return rc;
177131}
177132
177133/*
177134** Set up the lookaside buffers for a database connection.
177135** Return SQLITE_OK on success.
177136** If lookaside is already active, return SQLITE_BUSY.
177137**
177138** The sz parameter is the number of bytes in each lookaside slot.
177139** The cnt parameter is the number of slots. If pStart is NULL the
177140** space for the lookaside memory is obtained from sqlite3_malloc().
177141** If pStart is not NULL then it is sz*cnt bytes of memory to use for
177142** the lookaside memory.
177143*/
177144static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
177145#ifndef SQLITE_OMIT_LOOKASIDE
177146 void *pStart;
177147 sqlite3_int64 szAlloc = sz*(sqlite3_int64)cnt;
177148 int nBig; /* Number of full-size slots */
177149 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
177150
177151 if( sqlite3LookasideUsed(db,pHighwater: 0)>0 ){
177152 return SQLITE_BUSY;
177153 }
177154 /* Free any existing lookaside buffer for this handle before
177155 ** allocating a new one so we don't have to have space for
177156 ** both at the same time.
177157 */
177158 if( db->lookaside.bMalloced ){
177159 sqlite3_free(p: db->lookaside.pStart);
177160 }
177161 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
177162 ** than a pointer to be useful.
177163 */
177164 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
177165 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
177166 if( cnt<0 ) cnt = 0;
177167 if( sz==0 || cnt==0 ){
177168 sz = 0;
177169 pStart = 0;
177170 }else if( pBuf==0 ){
177171 sqlite3BeginBenignMalloc();
177172 pStart = sqlite3Malloc( n: szAlloc ); /* IMP: R-61949-35727 */
177173 sqlite3EndBenignMalloc();
177174 if( pStart ) szAlloc = sqlite3MallocSize(p: pStart);
177175 }else{
177176 pStart = pBuf;
177177 }
177178#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
177179 if( sz>=LOOKASIDE_SMALL*3 ){
177180 nBig = szAlloc/(3*LOOKASIDE_SMALL+sz);
177181 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
177182 }else if( sz>=LOOKASIDE_SMALL*2 ){
177183 nBig = szAlloc/(LOOKASIDE_SMALL+sz);
177184 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
177185 }else
177186#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
177187 if( sz>0 ){
177188 nBig = szAlloc/sz;
177189 nSm = 0;
177190 }else{
177191 nBig = nSm = 0;
177192 }
177193 db->lookaside.pStart = pStart;
177194 db->lookaside.pInit = 0;
177195 db->lookaside.pFree = 0;
177196 db->lookaside.sz = (u16)sz;
177197 db->lookaside.szTrue = (u16)sz;
177198 if( pStart ){
177199 int i;
177200 LookasideSlot *p;
177201 assert( sz > (int)sizeof(LookasideSlot*) );
177202 p = (LookasideSlot*)pStart;
177203 for(i=0; i<nBig; i++){
177204 p->pNext = db->lookaside.pInit;
177205 db->lookaside.pInit = p;
177206 p = (LookasideSlot*)&((u8*)p)[sz];
177207 }
177208#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
177209 db->lookaside.pSmallInit = 0;
177210 db->lookaside.pSmallFree = 0;
177211 db->lookaside.pMiddle = p;
177212 for(i=0; i<nSm; i++){
177213 p->pNext = db->lookaside.pSmallInit;
177214 db->lookaside.pSmallInit = p;
177215 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL];
177216 }
177217#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
177218 assert( ((uptr)p)<=szAlloc + (uptr)pStart );
177219 db->lookaside.pEnd = p;
177220 db->lookaside.bDisable = 0;
177221 db->lookaside.bMalloced = pBuf==0 ?1:0;
177222 db->lookaside.nSlot = nBig+nSm;
177223 }else{
177224 db->lookaside.pStart = 0;
177225#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
177226 db->lookaside.pSmallInit = 0;
177227 db->lookaside.pSmallFree = 0;
177228 db->lookaside.pMiddle = 0;
177229#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
177230 db->lookaside.pEnd = 0;
177231 db->lookaside.bDisable = 1;
177232 db->lookaside.sz = 0;
177233 db->lookaside.bMalloced = 0;
177234 db->lookaside.nSlot = 0;
177235 }
177236 db->lookaside.pTrueEnd = db->lookaside.pEnd;
177237 assert( sqlite3LookasideUsed(db,0)==0 );
177238#endif /* SQLITE_OMIT_LOOKASIDE */
177239 return SQLITE_OK;
177240}
177241
177242/*
177243** Return the mutex associated with a database connection.
177244*/
177245SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
177246#ifdef SQLITE_ENABLE_API_ARMOR
177247 if( !sqlite3SafetyCheckOk(db) ){
177248 (void)SQLITE_MISUSE_BKPT;
177249 return 0;
177250 }
177251#endif
177252 return db->mutex;
177253}
177254
177255/*
177256** Free up as much memory as we can from the given database
177257** connection.
177258*/
177259SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
177260 int i;
177261
177262#ifdef SQLITE_ENABLE_API_ARMOR
177263 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
177264#endif
177265 sqlite3_mutex_enter(p: db->mutex);
177266 sqlite3BtreeEnterAll(db);
177267 for(i=0; i<db->nDb; i++){
177268 Btree *pBt = db->aDb[i].pBt;
177269 if( pBt ){
177270 Pager *pPager = sqlite3BtreePager(p: pBt);
177271 sqlite3PagerShrink(pPager);
177272 }
177273 }
177274 sqlite3BtreeLeaveAll(db);
177275 sqlite3_mutex_leave(p: db->mutex);
177276 return SQLITE_OK;
177277}
177278
177279/*
177280** Flush any dirty pages in the pager-cache for any attached database
177281** to disk.
177282*/
177283SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
177284 int i;
177285 int rc = SQLITE_OK;
177286 int bSeenBusy = 0;
177287
177288#ifdef SQLITE_ENABLE_API_ARMOR
177289 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
177290#endif
177291 sqlite3_mutex_enter(p: db->mutex);
177292 sqlite3BtreeEnterAll(db);
177293 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
177294 Btree *pBt = db->aDb[i].pBt;
177295 if( pBt && sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_WRITE ){
177296 Pager *pPager = sqlite3BtreePager(p: pBt);
177297 rc = sqlite3PagerFlush(pPager);
177298 if( rc==SQLITE_BUSY ){
177299 bSeenBusy = 1;
177300 rc = SQLITE_OK;
177301 }
177302 }
177303 }
177304 sqlite3BtreeLeaveAll(db);
177305 sqlite3_mutex_leave(p: db->mutex);
177306 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
177307}
177308
177309/*
177310** Configuration settings for an individual database connection
177311*/
177312SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
177313 va_list ap;
177314 int rc;
177315 sqlite3_mutex_enter(p: db->mutex);
177316 va_start(ap, op);
177317 switch( op ){
177318 case SQLITE_DBCONFIG_MAINDBNAME: {
177319 /* IMP: R-06824-28531 */
177320 /* IMP: R-36257-52125 */
177321 db->aDb[0].zDbSName = va_arg(ap,char*);
177322 rc = SQLITE_OK;
177323 break;
177324 }
177325 case SQLITE_DBCONFIG_LOOKASIDE: {
177326 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
177327 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
177328 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
177329 rc = setupLookaside(db, pBuf, sz, cnt);
177330 break;
177331 }
177332 default: {
177333 static const struct {
177334 int op; /* The opcode */
177335 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
177336 } aFlagOp[] = {
177337 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
177338 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
177339 { SQLITE_DBCONFIG_ENABLE_VIEW, SQLITE_EnableView },
177340 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
177341 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
177342 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
177343 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
177344 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
177345 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
177346 { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive },
177347 { SQLITE_DBCONFIG_WRITABLE_SCHEMA, SQLITE_WriteSchema|
177348 SQLITE_NoSchemaError },
177349 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, SQLITE_LegacyAlter },
177350 { SQLITE_DBCONFIG_DQS_DDL, SQLITE_DqsDDL },
177351 { SQLITE_DBCONFIG_DQS_DML, SQLITE_DqsDML },
177352 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT, SQLITE_LegacyFileFmt },
177353 { SQLITE_DBCONFIG_TRUSTED_SCHEMA, SQLITE_TrustedSchema },
177354 { SQLITE_DBCONFIG_STMT_SCANSTATUS, SQLITE_StmtScanStatus },
177355 { SQLITE_DBCONFIG_REVERSE_SCANORDER, SQLITE_ReverseOrder },
177356 };
177357 unsigned int i;
177358 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
177359 for(i=0; i<ArraySize(aFlagOp); i++){
177360 if( aFlagOp[i].op==op ){
177361 int onoff = va_arg(ap, int);
177362 int *pRes = va_arg(ap, int*);
177363 u64 oldFlags = db->flags;
177364 if( onoff>0 ){
177365 db->flags |= aFlagOp[i].mask;
177366 }else if( onoff==0 ){
177367 db->flags &= ~(u64)aFlagOp[i].mask;
177368 }
177369 if( oldFlags!=db->flags ){
177370 sqlite3ExpirePreparedStatements(db, iCode: 0);
177371 }
177372 if( pRes ){
177373 *pRes = (db->flags & aFlagOp[i].mask)!=0;
177374 }
177375 rc = SQLITE_OK;
177376 break;
177377 }
177378 }
177379 break;
177380 }
177381 }
177382 va_end(ap);
177383 sqlite3_mutex_leave(p: db->mutex);
177384 return rc;
177385}
177386
177387/*
177388** This is the default collating function named "BINARY" which is always
177389** available.
177390*/
177391static int binCollFunc(
177392 void *NotUsed,
177393 int nKey1, const void *pKey1,
177394 int nKey2, const void *pKey2
177395){
177396 int rc, n;
177397 UNUSED_PARAMETER(NotUsed);
177398 n = nKey1<nKey2 ? nKey1 : nKey2;
177399 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
177400 ** strings byte by byte using the memcmp() function from the standard C
177401 ** library. */
177402 assert( pKey1 && pKey2 );
177403 rc = memcmp(s1: pKey1, s2: pKey2, n: n);
177404 if( rc==0 ){
177405 rc = nKey1 - nKey2;
177406 }
177407 return rc;
177408}
177409
177410/*
177411** This is the collating function named "RTRIM" which is always
177412** available. Ignore trailing spaces.
177413*/
177414static int rtrimCollFunc(
177415 void *pUser,
177416 int nKey1, const void *pKey1,
177417 int nKey2, const void *pKey2
177418){
177419 const u8 *pK1 = (const u8*)pKey1;
177420 const u8 *pK2 = (const u8*)pKey2;
177421 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
177422 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
177423 return binCollFunc(NotUsed: pUser, nKey1, pKey1, nKey2, pKey2);
177424}
177425
177426/*
177427** Return true if CollSeq is the default built-in BINARY.
177428*/
177429SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
177430 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
177431 return p==0 || p->xCmp==binCollFunc;
177432}
177433
177434/*
177435** Another built-in collating sequence: NOCASE.
177436**
177437** This collating sequence is intended to be used for "case independent
177438** comparison". SQLite's knowledge of upper and lower case equivalents
177439** extends only to the 26 characters used in the English language.
177440**
177441** At the moment there is only a UTF-8 implementation.
177442*/
177443static int nocaseCollatingFunc(
177444 void *NotUsed,
177445 int nKey1, const void *pKey1,
177446 int nKey2, const void *pKey2
177447){
177448 int r = sqlite3StrNICmp(
177449 zLeft: (const char *)pKey1, zRight: (const char *)pKey2, N: (nKey1<nKey2)?nKey1:nKey2);
177450 UNUSED_PARAMETER(NotUsed);
177451 if( 0==r ){
177452 r = nKey1-nKey2;
177453 }
177454 return r;
177455}
177456
177457/*
177458** Return the ROWID of the most recent insert
177459*/
177460SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
177461#ifdef SQLITE_ENABLE_API_ARMOR
177462 if( !sqlite3SafetyCheckOk(db) ){
177463 (void)SQLITE_MISUSE_BKPT;
177464 return 0;
177465 }
177466#endif
177467 return db->lastRowid;
177468}
177469
177470/*
177471** Set the value returned by the sqlite3_last_insert_rowid() API function.
177472*/
177473SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
177474#ifdef SQLITE_ENABLE_API_ARMOR
177475 if( !sqlite3SafetyCheckOk(db) ){
177476 (void)SQLITE_MISUSE_BKPT;
177477 return;
177478 }
177479#endif
177480 sqlite3_mutex_enter(p: db->mutex);
177481 db->lastRowid = iRowid;
177482 sqlite3_mutex_leave(p: db->mutex);
177483}
177484
177485/*
177486** Return the number of changes in the most recent call to sqlite3_exec().
177487*/
177488SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3 *db){
177489#ifdef SQLITE_ENABLE_API_ARMOR
177490 if( !sqlite3SafetyCheckOk(db) ){
177491 (void)SQLITE_MISUSE_BKPT;
177492 return 0;
177493 }
177494#endif
177495 return db->nChange;
177496}
177497SQLITE_API int sqlite3_changes(sqlite3 *db){
177498 return (int)sqlite3_changes64(db);
177499}
177500
177501/*
177502** Return the number of changes since the database handle was opened.
177503*/
177504SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){
177505#ifdef SQLITE_ENABLE_API_ARMOR
177506 if( !sqlite3SafetyCheckOk(db) ){
177507 (void)SQLITE_MISUSE_BKPT;
177508 return 0;
177509 }
177510#endif
177511 return db->nTotalChange;
177512}
177513SQLITE_API int sqlite3_total_changes(sqlite3 *db){
177514 return (int)sqlite3_total_changes64(db);
177515}
177516
177517/*
177518** Close all open savepoints. This function only manipulates fields of the
177519** database handle object, it does not close any savepoints that may be open
177520** at the b-tree/pager level.
177521*/
177522SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
177523 while( db->pSavepoint ){
177524 Savepoint *pTmp = db->pSavepoint;
177525 db->pSavepoint = pTmp->pNext;
177526 sqlite3DbFree(db, p: pTmp);
177527 }
177528 db->nSavepoint = 0;
177529 db->nStatement = 0;
177530 db->isTransactionSavepoint = 0;
177531}
177532
177533/*
177534** Invoke the destructor function associated with FuncDef p, if any. Except,
177535** if this is not the last copy of the function, do not invoke it. Multiple
177536** copies of a single function are created when create_function() is called
177537** with SQLITE_ANY as the encoding.
177538*/
177539static void functionDestroy(sqlite3 *db, FuncDef *p){
177540 FuncDestructor *pDestructor;
177541 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
177542 pDestructor = p->u.pDestructor;
177543 if( pDestructor ){
177544 pDestructor->nRef--;
177545 if( pDestructor->nRef==0 ){
177546 pDestructor->xDestroy(pDestructor->pUserData);
177547 sqlite3DbFree(db, p: pDestructor);
177548 }
177549 }
177550}
177551
177552/*
177553** Disconnect all sqlite3_vtab objects that belong to database connection
177554** db. This is called when db is being closed.
177555*/
177556static void disconnectAllVtab(sqlite3 *db){
177557#ifndef SQLITE_OMIT_VIRTUALTABLE
177558 int i;
177559 HashElem *p;
177560 sqlite3BtreeEnterAll(db);
177561 for(i=0; i<db->nDb; i++){
177562 Schema *pSchema = db->aDb[i].pSchema;
177563 if( pSchema ){
177564 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
177565 Table *pTab = (Table *)sqliteHashData(p);
177566 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, p: pTab);
177567 }
177568 }
177569 }
177570 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
177571 Module *pMod = (Module *)sqliteHashData(p);
177572 if( pMod->pEpoTab ){
177573 sqlite3VtabDisconnect(db, p: pMod->pEpoTab);
177574 }
177575 }
177576 sqlite3VtabUnlockList(db);
177577 sqlite3BtreeLeaveAll(db);
177578#else
177579 UNUSED_PARAMETER(db);
177580#endif
177581}
177582
177583/*
177584** Return TRUE if database connection db has unfinalized prepared
177585** statements or unfinished sqlite3_backup objects.
177586*/
177587static int connectionIsBusy(sqlite3 *db){
177588 int j;
177589 assert( sqlite3_mutex_held(db->mutex) );
177590 if( db->pVdbe ) return 1;
177591 for(j=0; j<db->nDb; j++){
177592 Btree *pBt = db->aDb[j].pBt;
177593 if( pBt && sqlite3BtreeIsInBackup(p: pBt) ) return 1;
177594 }
177595 return 0;
177596}
177597
177598/*
177599** Close an existing SQLite database
177600*/
177601static int sqlite3Close(sqlite3 *db, int forceZombie){
177602 if( !db ){
177603 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
177604 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
177605 return SQLITE_OK;
177606 }
177607 if( !sqlite3SafetyCheckSickOrOk(db) ){
177608 return SQLITE_MISUSE_BKPT;
177609 }
177610 sqlite3_mutex_enter(p: db->mutex);
177611 if( db->mTrace & SQLITE_TRACE_CLOSE ){
177612 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
177613 }
177614
177615 /* Force xDisconnect calls on all virtual tables */
177616 disconnectAllVtab(db);
177617
177618 /* If a transaction is open, the disconnectAllVtab() call above
177619 ** will not have called the xDisconnect() method on any virtual
177620 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
177621 ** call will do so. We need to do this before the check for active
177622 ** SQL statements below, as the v-table implementation may be storing
177623 ** some prepared statements internally.
177624 */
177625 sqlite3VtabRollback(db);
177626
177627 /* Legacy behavior (sqlite3_close() behavior) is to return
177628 ** SQLITE_BUSY if the connection can not be closed immediately.
177629 */
177630 if( !forceZombie && connectionIsBusy(db) ){
177631 sqlite3ErrorWithMsg(db, SQLITE_BUSY, zFormat: "unable to close due to unfinalized "
177632 "statements or unfinished backups");
177633 sqlite3_mutex_leave(p: db->mutex);
177634 return SQLITE_BUSY;
177635 }
177636
177637#ifdef SQLITE_ENABLE_SQLLOG
177638 if( sqlite3GlobalConfig.xSqllog ){
177639 /* Closing the handle. Fourth parameter is passed the value 2. */
177640 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
177641 }
177642#endif
177643
177644 /* Convert the connection into a zombie and then close it.
177645 */
177646 db->eOpenState = SQLITE_STATE_ZOMBIE;
177647 sqlite3LeaveMutexAndCloseZombie(db);
177648 return SQLITE_OK;
177649}
177650
177651/*
177652** Return the transaction state for a single databse, or the maximum
177653** transaction state over all attached databases if zSchema is null.
177654*/
177655SQLITE_API int sqlite3_txn_state(sqlite3 *db, const char *zSchema){
177656 int iDb, nDb;
177657 int iTxn = -1;
177658#ifdef SQLITE_ENABLE_API_ARMOR
177659 if( !sqlite3SafetyCheckOk(db) ){
177660 (void)SQLITE_MISUSE_BKPT;
177661 return -1;
177662 }
177663#endif
177664 sqlite3_mutex_enter(p: db->mutex);
177665 if( zSchema ){
177666 nDb = iDb = sqlite3FindDbName(db, zName: zSchema);
177667 if( iDb<0 ) nDb--;
177668 }else{
177669 iDb = 0;
177670 nDb = db->nDb-1;
177671 }
177672 for(; iDb<=nDb; iDb++){
177673 Btree *pBt = db->aDb[iDb].pBt;
177674 int x = pBt!=0 ? sqlite3BtreeTxnState(p: pBt) : SQLITE_TXN_NONE;
177675 if( x>iTxn ) iTxn = x;
177676 }
177677 sqlite3_mutex_leave(p: db->mutex);
177678 return iTxn;
177679}
177680
177681/*
177682** Two variations on the public interface for closing a database
177683** connection. The sqlite3_close() version returns SQLITE_BUSY and
177684** leaves the connection open if there are unfinalized prepared
177685** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
177686** version forces the connection to become a zombie if there are
177687** unclosed resources, and arranges for deallocation when the last
177688** prepare statement or sqlite3_backup closes.
177689*/
177690SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,forceZombie: 0); }
177691SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,forceZombie: 1); }
177692
177693
177694/*
177695** Close the mutex on database connection db.
177696**
177697** Furthermore, if database connection db is a zombie (meaning that there
177698** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
177699** every sqlite3_stmt has now been finalized and every sqlite3_backup has
177700** finished, then free all resources.
177701*/
177702SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
177703 HashElem *i; /* Hash table iterator */
177704 int j;
177705
177706 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
177707 ** or if the connection has not yet been closed by sqlite3_close_v2(),
177708 ** then just leave the mutex and return.
177709 */
177710 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
177711 sqlite3_mutex_leave(p: db->mutex);
177712 return;
177713 }
177714
177715 /* If we reach this point, it means that the database connection has
177716 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
177717 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
177718 ** go ahead and free all resources.
177719 */
177720
177721 /* If a transaction is open, roll it back. This also ensures that if
177722 ** any database schemas have been modified by an uncommitted transaction
177723 ** they are reset. And that the required b-tree mutex is held to make
177724 ** the pager rollback and schema reset an atomic operation. */
177725 sqlite3RollbackAll(db, SQLITE_OK);
177726
177727 /* Free any outstanding Savepoint structures. */
177728 sqlite3CloseSavepoints(db);
177729
177730 /* Close all database connections */
177731 for(j=0; j<db->nDb; j++){
177732 struct Db *pDb = &db->aDb[j];
177733 if( pDb->pBt ){
177734 sqlite3BtreeClose(p: pDb->pBt);
177735 pDb->pBt = 0;
177736 if( j!=1 ){
177737 pDb->pSchema = 0;
177738 }
177739 }
177740 }
177741 /* Clear the TEMP schema separately and last */
177742 if( db->aDb[1].pSchema ){
177743 sqlite3SchemaClear(p: db->aDb[1].pSchema);
177744 }
177745 sqlite3VtabUnlockList(db);
177746
177747 /* Free up the array of auxiliary databases */
177748 sqlite3CollapseDatabaseArray(db);
177749 assert( db->nDb<=2 );
177750 assert( db->aDb==db->aDbStatic );
177751
177752 /* Tell the code in notify.c that the connection no longer holds any
177753 ** locks and does not require any further unlock-notify callbacks.
177754 */
177755 sqlite3ConnectionClosed(db);
177756
177757 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
177758 FuncDef *pNext, *p;
177759 p = sqliteHashData(i);
177760 do{
177761 functionDestroy(db, p);
177762 pNext = p->pNext;
177763 sqlite3DbFree(db, p);
177764 p = pNext;
177765 }while( p );
177766 }
177767 sqlite3HashClear(pH: &db->aFunc);
177768 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
177769 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
177770 /* Invoke any destructors registered for collation sequence user data. */
177771 for(j=0; j<3; j++){
177772 if( pColl[j].xDel ){
177773 pColl[j].xDel(pColl[j].pUser);
177774 }
177775 }
177776 sqlite3DbFree(db, p: pColl);
177777 }
177778 sqlite3HashClear(pH: &db->aCollSeq);
177779#ifndef SQLITE_OMIT_VIRTUALTABLE
177780 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
177781 Module *pMod = (Module *)sqliteHashData(i);
177782 sqlite3VtabEponymousTableClear(db, pMod);
177783 sqlite3VtabModuleUnref(db, pMod);
177784 }
177785 sqlite3HashClear(pH: &db->aModule);
177786#endif
177787
177788 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
177789 sqlite3ValueFree(v: db->pErr);
177790 sqlite3CloseExtensions(db);
177791#if SQLITE_USER_AUTHENTICATION
177792 sqlite3_free(db->auth.zAuthUser);
177793 sqlite3_free(db->auth.zAuthPW);
177794#endif
177795
177796 db->eOpenState = SQLITE_STATE_ERROR;
177797
177798 /* The temp-database schema is allocated differently from the other schema
177799 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
177800 ** So it needs to be freed here. Todo: Why not roll the temp schema into
177801 ** the same sqliteMalloc() as the one that allocates the database
177802 ** structure?
177803 */
177804 sqlite3DbFree(db, p: db->aDb[1].pSchema);
177805 if( db->xAutovacDestr ){
177806 db->xAutovacDestr(db->pAutovacPagesArg);
177807 }
177808 sqlite3_mutex_leave(p: db->mutex);
177809 db->eOpenState = SQLITE_STATE_CLOSED;
177810 sqlite3_mutex_free(p: db->mutex);
177811 assert( sqlite3LookasideUsed(db,0)==0 );
177812 if( db->lookaside.bMalloced ){
177813 sqlite3_free(p: db->lookaside.pStart);
177814 }
177815 sqlite3_free(p: db);
177816}
177817
177818/*
177819** Rollback all database files. If tripCode is not SQLITE_OK, then
177820** any write cursors are invalidated ("tripped" - as in "tripping a circuit
177821** breaker") and made to return tripCode if there are any further
177822** attempts to use that cursor. Read cursors remain open and valid
177823** but are "saved" in case the table pages are moved around.
177824*/
177825SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
177826 int i;
177827 int inTrans = 0;
177828 int schemaChange;
177829 assert( sqlite3_mutex_held(db->mutex) );
177830 sqlite3BeginBenignMalloc();
177831
177832 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
177833 ** This is important in case the transaction being rolled back has
177834 ** modified the database schema. If the b-tree mutexes are not taken
177835 ** here, then another shared-cache connection might sneak in between
177836 ** the database rollback and schema reset, which can cause false
177837 ** corruption reports in some cases. */
177838 sqlite3BtreeEnterAll(db);
177839 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
177840
177841 for(i=0; i<db->nDb; i++){
177842 Btree *p = db->aDb[i].pBt;
177843 if( p ){
177844 if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE ){
177845 inTrans = 1;
177846 }
177847 sqlite3BtreeRollback(p, tripCode, writeOnly: !schemaChange);
177848 }
177849 }
177850 sqlite3VtabRollback(db);
177851 sqlite3EndBenignMalloc();
177852
177853 if( schemaChange ){
177854 sqlite3ExpirePreparedStatements(db, iCode: 0);
177855 sqlite3ResetAllSchemasOfConnection(db);
177856 }
177857 sqlite3BtreeLeaveAll(db);
177858
177859 /* Any deferred constraint violations have now been resolved. */
177860 db->nDeferredCons = 0;
177861 db->nDeferredImmCons = 0;
177862 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
177863
177864 /* If one has been configured, invoke the rollback-hook callback */
177865 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
177866 db->xRollbackCallback(db->pRollbackArg);
177867 }
177868}
177869
177870/*
177871** Return a static string containing the name corresponding to the error code
177872** specified in the argument.
177873*/
177874#if defined(SQLITE_NEED_ERR_NAME)
177875SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
177876 const char *zName = 0;
177877 int i, origRc = rc;
177878 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
177879 switch( rc ){
177880 case SQLITE_OK: zName = "SQLITE_OK"; break;
177881 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
177882 case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break;
177883 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
177884 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
177885 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
177886 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
177887 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
177888 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
177889 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
177890 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
177891 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
177892 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
177893 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
177894 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
177895 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
177896 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
177897 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
177898 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
177899 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
177900 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
177901 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
177902 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
177903 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
177904 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
177905 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
177906 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
177907 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
177908 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
177909 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
177910 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
177911 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
177912 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
177913 case SQLITE_IOERR_CHECKRESERVEDLOCK:
177914 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
177915 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
177916 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
177917 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
177918 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
177919 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
177920 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
177921 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
177922 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
177923 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
177924 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
177925 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
177926 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
177927 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
177928 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
177929 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
177930 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
177931 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
177932 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
177933 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
177934 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
177935 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
177936 case SQLITE_CANTOPEN_SYMLINK: zName = "SQLITE_CANTOPEN_SYMLINK"; break;
177937 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
177938 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
177939 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
177940 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
177941 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
177942 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
177943 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
177944 case SQLITE_CONSTRAINT_FOREIGNKEY:
177945 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
177946 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
177947 case SQLITE_CONSTRAINT_PRIMARYKEY:
177948 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
177949 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
177950 case SQLITE_CONSTRAINT_COMMITHOOK:
177951 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
177952 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
177953 case SQLITE_CONSTRAINT_FUNCTION:
177954 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
177955 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
177956 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
177957 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
177958 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
177959 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
177960 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
177961 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
177962 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
177963 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
177964 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
177965 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
177966 case SQLITE_NOTICE_RECOVER_ROLLBACK:
177967 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
177968 case SQLITE_NOTICE_RBU: zName = "SQLITE_NOTICE_RBU"; break;
177969 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
177970 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
177971 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
177972 }
177973 }
177974 if( zName==0 ){
177975 static char zBuf[50];
177976 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
177977 zName = zBuf;
177978 }
177979 return zName;
177980}
177981#endif
177982
177983/*
177984** Return a static string that describes the kind of error specified in the
177985** argument.
177986*/
177987SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
177988 static const char* const aMsg[] = {
177989 /* SQLITE_OK */ "not an error",
177990 /* SQLITE_ERROR */ "SQL logic error",
177991 /* SQLITE_INTERNAL */ 0,
177992 /* SQLITE_PERM */ "access permission denied",
177993 /* SQLITE_ABORT */ "query aborted",
177994 /* SQLITE_BUSY */ "database is locked",
177995 /* SQLITE_LOCKED */ "database table is locked",
177996 /* SQLITE_NOMEM */ "out of memory",
177997 /* SQLITE_READONLY */ "attempt to write a readonly database",
177998 /* SQLITE_INTERRUPT */ "interrupted",
177999 /* SQLITE_IOERR */ "disk I/O error",
178000 /* SQLITE_CORRUPT */ "database disk image is malformed",
178001 /* SQLITE_NOTFOUND */ "unknown operation",
178002 /* SQLITE_FULL */ "database or disk is full",
178003 /* SQLITE_CANTOPEN */ "unable to open database file",
178004 /* SQLITE_PROTOCOL */ "locking protocol",
178005 /* SQLITE_EMPTY */ 0,
178006 /* SQLITE_SCHEMA */ "database schema has changed",
178007 /* SQLITE_TOOBIG */ "string or blob too big",
178008 /* SQLITE_CONSTRAINT */ "constraint failed",
178009 /* SQLITE_MISMATCH */ "datatype mismatch",
178010 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
178011#ifdef SQLITE_DISABLE_LFS
178012 /* SQLITE_NOLFS */ "large file support is disabled",
178013#else
178014 /* SQLITE_NOLFS */ 0,
178015#endif
178016 /* SQLITE_AUTH */ "authorization denied",
178017 /* SQLITE_FORMAT */ 0,
178018 /* SQLITE_RANGE */ "column index out of range",
178019 /* SQLITE_NOTADB */ "file is not a database",
178020 /* SQLITE_NOTICE */ "notification message",
178021 /* SQLITE_WARNING */ "warning message",
178022 };
178023 const char *zErr = "unknown error";
178024 switch( rc ){
178025 case SQLITE_ABORT_ROLLBACK: {
178026 zErr = "abort due to ROLLBACK";
178027 break;
178028 }
178029 case SQLITE_ROW: {
178030 zErr = "another row available";
178031 break;
178032 }
178033 case SQLITE_DONE: {
178034 zErr = "no more rows available";
178035 break;
178036 }
178037 default: {
178038 rc &= 0xff;
178039 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
178040 zErr = aMsg[rc];
178041 }
178042 break;
178043 }
178044 }
178045 return zErr;
178046}
178047
178048/*
178049** This routine implements a busy callback that sleeps and tries
178050** again until a timeout value is reached. The timeout value is
178051** an integer number of milliseconds passed in as the first
178052** argument.
178053**
178054** Return non-zero to retry the lock. Return zero to stop trying
178055** and cause SQLite to return SQLITE_BUSY.
178056*/
178057static int sqliteDefaultBusyCallback(
178058 void *ptr, /* Database connection */
178059 int count /* Number of times table has been busy */
178060){
178061#if SQLITE_OS_WIN || !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP
178062 /* This case is for systems that have support for sleeping for fractions of
178063 ** a second. Examples: All windows systems, unix systems with nanosleep() */
178064 static const u8 delays[] =
178065 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
178066 static const u8 totals[] =
178067 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
178068# define NDELAY ArraySize(delays)
178069 sqlite3 *db = (sqlite3 *)ptr;
178070 int tmout = db->busyTimeout;
178071 int delay, prior;
178072
178073 assert( count>=0 );
178074 if( count < NDELAY ){
178075 delay = delays[count];
178076 prior = totals[count];
178077 }else{
178078 delay = delays[NDELAY-1];
178079 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
178080 }
178081 if( prior + delay > tmout ){
178082 delay = tmout - prior;
178083 if( delay<=0 ) return 0;
178084 }
178085 sqlite3OsSleep(pVfs: db->pVfs, nMicro: delay*1000);
178086 return 1;
178087#else
178088 /* This case for unix systems that lack usleep() support. Sleeping
178089 ** must be done in increments of whole seconds */
178090 sqlite3 *db = (sqlite3 *)ptr;
178091 int tmout = ((sqlite3 *)ptr)->busyTimeout;
178092 if( (count+1)*1000 > tmout ){
178093 return 0;
178094 }
178095 sqlite3OsSleep(db->pVfs, 1000000);
178096 return 1;
178097#endif
178098}
178099
178100/*
178101** Invoke the given busy handler.
178102**
178103** This routine is called when an operation failed to acquire a
178104** lock on VFS file pFile.
178105**
178106** If this routine returns non-zero, the lock is retried. If it
178107** returns 0, the operation aborts with an SQLITE_BUSY error.
178108*/
178109SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
178110 int rc;
178111 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
178112 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
178113 if( rc==0 ){
178114 p->nBusy = -1;
178115 }else{
178116 p->nBusy++;
178117 }
178118 return rc;
178119}
178120
178121/*
178122** This routine sets the busy callback for an Sqlite database to the
178123** given callback function with the given argument.
178124*/
178125SQLITE_API int sqlite3_busy_handler(
178126 sqlite3 *db,
178127 int (*xBusy)(void*,int),
178128 void *pArg
178129){
178130#ifdef SQLITE_ENABLE_API_ARMOR
178131 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
178132#endif
178133 sqlite3_mutex_enter(p: db->mutex);
178134 db->busyHandler.xBusyHandler = xBusy;
178135 db->busyHandler.pBusyArg = pArg;
178136 db->busyHandler.nBusy = 0;
178137 db->busyTimeout = 0;
178138 sqlite3_mutex_leave(p: db->mutex);
178139 return SQLITE_OK;
178140}
178141
178142#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
178143/*
178144** This routine sets the progress callback for an Sqlite database to the
178145** given callback function with the given argument. The progress callback will
178146** be invoked every nOps opcodes.
178147*/
178148SQLITE_API void sqlite3_progress_handler(
178149 sqlite3 *db,
178150 int nOps,
178151 int (*xProgress)(void*),
178152 void *pArg
178153){
178154#ifdef SQLITE_ENABLE_API_ARMOR
178155 if( !sqlite3SafetyCheckOk(db) ){
178156 (void)SQLITE_MISUSE_BKPT;
178157 return;
178158 }
178159#endif
178160 sqlite3_mutex_enter(p: db->mutex);
178161 if( nOps>0 ){
178162 db->xProgress = xProgress;
178163 db->nProgressOps = (unsigned)nOps;
178164 db->pProgressArg = pArg;
178165 }else{
178166 db->xProgress = 0;
178167 db->nProgressOps = 0;
178168 db->pProgressArg = 0;
178169 }
178170 sqlite3_mutex_leave(p: db->mutex);
178171}
178172#endif
178173
178174
178175/*
178176** This routine installs a default busy handler that waits for the
178177** specified number of milliseconds before returning 0.
178178*/
178179SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
178180#ifdef SQLITE_ENABLE_API_ARMOR
178181 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
178182#endif
178183 if( ms>0 ){
178184 sqlite3_busy_handler(db, xBusy: (int(*)(void*,int))sqliteDefaultBusyCallback,
178185 pArg: (void*)db);
178186 db->busyTimeout = ms;
178187 }else{
178188 sqlite3_busy_handler(db, xBusy: 0, pArg: 0);
178189 }
178190 return SQLITE_OK;
178191}
178192
178193/*
178194** Cause any pending operation to stop at its earliest opportunity.
178195*/
178196SQLITE_API void sqlite3_interrupt(sqlite3 *db){
178197#ifdef SQLITE_ENABLE_API_ARMOR
178198 if( !sqlite3SafetyCheckOk(db)
178199 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE)
178200 ){
178201 (void)SQLITE_MISUSE_BKPT;
178202 return;
178203 }
178204#endif
178205 AtomicStore(&db->u1.isInterrupted, 1);
178206}
178207
178208/*
178209** Return true or false depending on whether or not an interrupt is
178210** pending on connection db.
178211*/
178212SQLITE_API int sqlite3_is_interrupted(sqlite3 *db){
178213#ifdef SQLITE_ENABLE_API_ARMOR
178214 if( !sqlite3SafetyCheckOk(db)
178215 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE)
178216 ){
178217 (void)SQLITE_MISUSE_BKPT;
178218 return 0;
178219 }
178220#endif
178221 return AtomicLoad(&db->u1.isInterrupted)!=0;
178222}
178223
178224/*
178225** This function is exactly the same as sqlite3_create_function(), except
178226** that it is designed to be called by internal code. The difference is
178227** that if a malloc() fails in sqlite3_create_function(), an error code
178228** is returned and the mallocFailed flag cleared.
178229*/
178230SQLITE_PRIVATE int sqlite3CreateFunc(
178231 sqlite3 *db,
178232 const char *zFunctionName,
178233 int nArg,
178234 int enc,
178235 void *pUserData,
178236 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
178237 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
178238 void (*xFinal)(sqlite3_context*),
178239 void (*xValue)(sqlite3_context*),
178240 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
178241 FuncDestructor *pDestructor
178242){
178243 FuncDef *p;
178244 int extraFlags;
178245
178246 assert( sqlite3_mutex_held(db->mutex) );
178247 assert( xValue==0 || xSFunc==0 );
178248 if( zFunctionName==0 /* Must have a valid name */
178249 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
178250 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
178251 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
178252 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
178253 || (255<sqlite3Strlen30(z: zFunctionName))
178254 ){
178255 return SQLITE_MISUSE_BKPT;
178256 }
178257
178258 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
178259 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
178260 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
178261 SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
178262 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
178263
178264 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
178265 ** the meaning is inverted. So flip the bit. */
178266 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
178267 extraFlags ^= SQLITE_FUNC_UNSAFE; /* tag-20230109-1 */
178268
178269
178270#ifndef SQLITE_OMIT_UTF16
178271 /* If SQLITE_UTF16 is specified as the encoding type, transform this
178272 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
178273 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
178274 **
178275 ** If SQLITE_ANY is specified, add three versions of the function
178276 ** to the hash table.
178277 */
178278 switch( enc ){
178279 case SQLITE_UTF16:
178280 enc = SQLITE_UTF16NATIVE;
178281 break;
178282 case SQLITE_ANY: {
178283 int rc;
178284 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
178285 enc: (SQLITE_UTF8|extraFlags)^SQLITE_FUNC_UNSAFE, /* tag-20230109-1 */
178286 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
178287 if( rc==SQLITE_OK ){
178288 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
178289 enc: (SQLITE_UTF16LE|extraFlags)^SQLITE_FUNC_UNSAFE, /* tag-20230109-1*/
178290 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
178291 }
178292 if( rc!=SQLITE_OK ){
178293 return rc;
178294 }
178295 enc = SQLITE_UTF16BE;
178296 break;
178297 }
178298 case SQLITE_UTF8:
178299 case SQLITE_UTF16LE:
178300 case SQLITE_UTF16BE:
178301 break;
178302 default:
178303 enc = SQLITE_UTF8;
178304 break;
178305 }
178306#else
178307 enc = SQLITE_UTF8;
178308#endif
178309
178310 /* Check if an existing function is being overridden or deleted. If so,
178311 ** and there are active VMs, then return SQLITE_BUSY. If a function
178312 ** is being overridden/deleted but there are no active VMs, allow the
178313 ** operation to continue but invalidate all precompiled statements.
178314 */
178315 p = sqlite3FindFunction(db, zName: zFunctionName, nArg, enc: (u8)enc, createFlag: 0);
178316 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
178317 if( db->nVdbeActive ){
178318 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
178319 zFormat: "unable to delete/modify user-function due to active statements");
178320 assert( !db->mallocFailed );
178321 return SQLITE_BUSY;
178322 }else{
178323 sqlite3ExpirePreparedStatements(db, iCode: 0);
178324 }
178325 }else if( xSFunc==0 && xFinal==0 ){
178326 /* Trying to delete a function that does not exist. This is a no-op.
178327 ** https://sqlite.org/forum/forumpost/726219164b */
178328 return SQLITE_OK;
178329 }
178330
178331 p = sqlite3FindFunction(db, zName: zFunctionName, nArg, enc: (u8)enc, createFlag: 1);
178332 assert(p || db->mallocFailed);
178333 if( !p ){
178334 return SQLITE_NOMEM_BKPT;
178335 }
178336
178337 /* If an older version of the function with a configured destructor is
178338 ** being replaced invoke the destructor function here. */
178339 functionDestroy(db, p);
178340
178341 if( pDestructor ){
178342 pDestructor->nRef++;
178343 }
178344 p->u.pDestructor = pDestructor;
178345 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
178346 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
178347 testcase( p->funcFlags & SQLITE_DIRECTONLY );
178348 p->xSFunc = xSFunc ? xSFunc : xStep;
178349 p->xFinalize = xFinal;
178350 p->xValue = xValue;
178351 p->xInverse = xInverse;
178352 p->pUserData = pUserData;
178353 p->nArg = (u16)nArg;
178354 return SQLITE_OK;
178355}
178356
178357/*
178358** Worker function used by utf-8 APIs that create new functions:
178359**
178360** sqlite3_create_function()
178361** sqlite3_create_function_v2()
178362** sqlite3_create_window_function()
178363*/
178364static int createFunctionApi(
178365 sqlite3 *db,
178366 const char *zFunc,
178367 int nArg,
178368 int enc,
178369 void *p,
178370 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
178371 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
178372 void (*xFinal)(sqlite3_context*),
178373 void (*xValue)(sqlite3_context*),
178374 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
178375 void(*xDestroy)(void*)
178376){
178377 int rc = SQLITE_ERROR;
178378 FuncDestructor *pArg = 0;
178379
178380#ifdef SQLITE_ENABLE_API_ARMOR
178381 if( !sqlite3SafetyCheckOk(db) ){
178382 return SQLITE_MISUSE_BKPT;
178383 }
178384#endif
178385 sqlite3_mutex_enter(p: db->mutex);
178386 if( xDestroy ){
178387 pArg = (FuncDestructor *)sqlite3Malloc(n: sizeof(FuncDestructor));
178388 if( !pArg ){
178389 sqlite3OomFault(db);
178390 xDestroy(p);
178391 goto out;
178392 }
178393 pArg->nRef = 0;
178394 pArg->xDestroy = xDestroy;
178395 pArg->pUserData = p;
178396 }
178397 rc = sqlite3CreateFunc(db, zFunctionName: zFunc, nArg, enc, pUserData: p,
178398 xSFunc, xStep, xFinal, xValue, xInverse, pDestructor: pArg
178399 );
178400 if( pArg && pArg->nRef==0 ){
178401 assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) );
178402 xDestroy(p);
178403 sqlite3_free(p: pArg);
178404 }
178405
178406 out:
178407 rc = sqlite3ApiExit(db, rc);
178408 sqlite3_mutex_leave(p: db->mutex);
178409 return rc;
178410}
178411
178412/*
178413** Create new user functions.
178414*/
178415SQLITE_API int sqlite3_create_function(
178416 sqlite3 *db,
178417 const char *zFunc,
178418 int nArg,
178419 int enc,
178420 void *p,
178421 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
178422 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
178423 void (*xFinal)(sqlite3_context*)
178424){
178425 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
178426 xFinal, xValue: 0, xInverse: 0, xDestroy: 0);
178427}
178428SQLITE_API int sqlite3_create_function_v2(
178429 sqlite3 *db,
178430 const char *zFunc,
178431 int nArg,
178432 int enc,
178433 void *p,
178434 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
178435 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
178436 void (*xFinal)(sqlite3_context*),
178437 void (*xDestroy)(void *)
178438){
178439 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
178440 xFinal, xValue: 0, xInverse: 0, xDestroy);
178441}
178442SQLITE_API int sqlite3_create_window_function(
178443 sqlite3 *db,
178444 const char *zFunc,
178445 int nArg,
178446 int enc,
178447 void *p,
178448 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
178449 void (*xFinal)(sqlite3_context*),
178450 void (*xValue)(sqlite3_context*),
178451 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
178452 void (*xDestroy)(void *)
178453){
178454 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc: 0, xStep,
178455 xFinal, xValue, xInverse, xDestroy);
178456}
178457
178458#ifndef SQLITE_OMIT_UTF16
178459SQLITE_API int sqlite3_create_function16(
178460 sqlite3 *db,
178461 const void *zFunctionName,
178462 int nArg,
178463 int eTextRep,
178464 void *p,
178465 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
178466 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
178467 void (*xFinal)(sqlite3_context*)
178468){
178469 int rc;
178470 char *zFunc8;
178471
178472#ifdef SQLITE_ENABLE_API_ARMOR
178473 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
178474#endif
178475 sqlite3_mutex_enter(p: db->mutex);
178476 assert( !db->mallocFailed );
178477 zFunc8 = sqlite3Utf16to8(db, z: zFunctionName, nByte: -1, SQLITE_UTF16NATIVE);
178478 rc = sqlite3CreateFunc(db, zFunctionName: zFunc8, nArg, enc: eTextRep, pUserData: p, xSFunc,xStep,xFinal,xValue: 0,xInverse: 0,pDestructor: 0);
178479 sqlite3DbFree(db, p: zFunc8);
178480 rc = sqlite3ApiExit(db, rc);
178481 sqlite3_mutex_leave(p: db->mutex);
178482 return rc;
178483}
178484#endif
178485
178486
178487/*
178488** The following is the implementation of an SQL function that always
178489** fails with an error message stating that the function is used in the
178490** wrong context. The sqlite3_overload_function() API might construct
178491** SQL function that use this routine so that the functions will exist
178492** for name resolution but are actually overloaded by the xFindFunction
178493** method of virtual tables.
178494*/
178495static void sqlite3InvalidFunction(
178496 sqlite3_context *context, /* The function calling context */
178497 int NotUsed, /* Number of arguments to the function */
178498 sqlite3_value **NotUsed2 /* Value of each argument */
178499){
178500 const char *zName = (const char*)sqlite3_user_data(p: context);
178501 char *zErr;
178502 UNUSED_PARAMETER2(NotUsed, NotUsed2);
178503 zErr = sqlite3_mprintf(
178504 zFormat: "unable to use function %s in the requested context", zName);
178505 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
178506 sqlite3_free(p: zErr);
178507}
178508
178509/*
178510** Declare that a function has been overloaded by a virtual table.
178511**
178512** If the function already exists as a regular global function, then
178513** this routine is a no-op. If the function does not exist, then create
178514** a new one that always throws a run-time error.
178515**
178516** When virtual tables intend to provide an overloaded function, they
178517** should call this routine to make sure the global function exists.
178518** A global function must exist in order for name resolution to work
178519** properly.
178520*/
178521SQLITE_API int sqlite3_overload_function(
178522 sqlite3 *db,
178523 const char *zName,
178524 int nArg
178525){
178526 int rc;
178527 char *zCopy;
178528
178529#ifdef SQLITE_ENABLE_API_ARMOR
178530 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
178531 return SQLITE_MISUSE_BKPT;
178532 }
178533#endif
178534 sqlite3_mutex_enter(p: db->mutex);
178535 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, createFlag: 0)!=0;
178536 sqlite3_mutex_leave(p: db->mutex);
178537 if( rc ) return SQLITE_OK;
178538 zCopy = sqlite3_mprintf(zFormat: "%s", zName);
178539 if( zCopy==0 ) return SQLITE_NOMEM;
178540 return sqlite3_create_function_v2(db, zFunc: zName, nArg, SQLITE_UTF8,
178541 p: zCopy, xSFunc: sqlite3InvalidFunction, xStep: 0, xFinal: 0, xDestroy: sqlite3_free);
178542}
178543
178544#ifndef SQLITE_OMIT_TRACE
178545/*
178546** Register a trace function. The pArg from the previously registered trace
178547** is returned.
178548**
178549** A NULL trace function means that no tracing is executes. A non-NULL
178550** trace is a pointer to a function that is invoked at the start of each
178551** SQL statement.
178552*/
178553#ifndef SQLITE_OMIT_DEPRECATED
178554SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
178555 void *pOld;
178556
178557#ifdef SQLITE_ENABLE_API_ARMOR
178558 if( !sqlite3SafetyCheckOk(db) ){
178559 (void)SQLITE_MISUSE_BKPT;
178560 return 0;
178561 }
178562#endif
178563 sqlite3_mutex_enter(p: db->mutex);
178564 pOld = db->pTraceArg;
178565 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
178566 db->trace.xLegacy = xTrace;
178567 db->pTraceArg = pArg;
178568 sqlite3_mutex_leave(p: db->mutex);
178569 return pOld;
178570}
178571#endif /* SQLITE_OMIT_DEPRECATED */
178572
178573/* Register a trace callback using the version-2 interface.
178574*/
178575SQLITE_API int sqlite3_trace_v2(
178576 sqlite3 *db, /* Trace this connection */
178577 unsigned mTrace, /* Mask of events to be traced */
178578 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
178579 void *pArg /* Context */
178580){
178581#ifdef SQLITE_ENABLE_API_ARMOR
178582 if( !sqlite3SafetyCheckOk(db) ){
178583 return SQLITE_MISUSE_BKPT;
178584 }
178585#endif
178586 sqlite3_mutex_enter(p: db->mutex);
178587 if( mTrace==0 ) xTrace = 0;
178588 if( xTrace==0 ) mTrace = 0;
178589 db->mTrace = mTrace;
178590 db->trace.xV2 = xTrace;
178591 db->pTraceArg = pArg;
178592 sqlite3_mutex_leave(p: db->mutex);
178593 return SQLITE_OK;
178594}
178595
178596#ifndef SQLITE_OMIT_DEPRECATED
178597/*
178598** Register a profile function. The pArg from the previously registered
178599** profile function is returned.
178600**
178601** A NULL profile function means that no profiling is executes. A non-NULL
178602** profile is a pointer to a function that is invoked at the conclusion of
178603** each SQL statement that is run.
178604*/
178605SQLITE_API void *sqlite3_profile(
178606 sqlite3 *db,
178607 void (*xProfile)(void*,const char*,sqlite_uint64),
178608 void *pArg
178609){
178610 void *pOld;
178611
178612#ifdef SQLITE_ENABLE_API_ARMOR
178613 if( !sqlite3SafetyCheckOk(db) ){
178614 (void)SQLITE_MISUSE_BKPT;
178615 return 0;
178616 }
178617#endif
178618 sqlite3_mutex_enter(p: db->mutex);
178619 pOld = db->pProfileArg;
178620 db->xProfile = xProfile;
178621 db->pProfileArg = pArg;
178622 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
178623 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
178624 sqlite3_mutex_leave(p: db->mutex);
178625 return pOld;
178626}
178627#endif /* SQLITE_OMIT_DEPRECATED */
178628#endif /* SQLITE_OMIT_TRACE */
178629
178630/*
178631** Register a function to be invoked when a transaction commits.
178632** If the invoked function returns non-zero, then the commit becomes a
178633** rollback.
178634*/
178635SQLITE_API void *sqlite3_commit_hook(
178636 sqlite3 *db, /* Attach the hook to this database */
178637 int (*xCallback)(void*), /* Function to invoke on each commit */
178638 void *pArg /* Argument to the function */
178639){
178640 void *pOld;
178641
178642#ifdef SQLITE_ENABLE_API_ARMOR
178643 if( !sqlite3SafetyCheckOk(db) ){
178644 (void)SQLITE_MISUSE_BKPT;
178645 return 0;
178646 }
178647#endif
178648 sqlite3_mutex_enter(p: db->mutex);
178649 pOld = db->pCommitArg;
178650 db->xCommitCallback = xCallback;
178651 db->pCommitArg = pArg;
178652 sqlite3_mutex_leave(p: db->mutex);
178653 return pOld;
178654}
178655
178656/*
178657** Register a callback to be invoked each time a row is updated,
178658** inserted or deleted using this database connection.
178659*/
178660SQLITE_API void *sqlite3_update_hook(
178661 sqlite3 *db, /* Attach the hook to this database */
178662 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
178663 void *pArg /* Argument to the function */
178664){
178665 void *pRet;
178666
178667#ifdef SQLITE_ENABLE_API_ARMOR
178668 if( !sqlite3SafetyCheckOk(db) ){
178669 (void)SQLITE_MISUSE_BKPT;
178670 return 0;
178671 }
178672#endif
178673 sqlite3_mutex_enter(p: db->mutex);
178674 pRet = db->pUpdateArg;
178675 db->xUpdateCallback = xCallback;
178676 db->pUpdateArg = pArg;
178677 sqlite3_mutex_leave(p: db->mutex);
178678 return pRet;
178679}
178680
178681/*
178682** Register a callback to be invoked each time a transaction is rolled
178683** back by this database connection.
178684*/
178685SQLITE_API void *sqlite3_rollback_hook(
178686 sqlite3 *db, /* Attach the hook to this database */
178687 void (*xCallback)(void*), /* Callback function */
178688 void *pArg /* Argument to the function */
178689){
178690 void *pRet;
178691
178692#ifdef SQLITE_ENABLE_API_ARMOR
178693 if( !sqlite3SafetyCheckOk(db) ){
178694 (void)SQLITE_MISUSE_BKPT;
178695 return 0;
178696 }
178697#endif
178698 sqlite3_mutex_enter(p: db->mutex);
178699 pRet = db->pRollbackArg;
178700 db->xRollbackCallback = xCallback;
178701 db->pRollbackArg = pArg;
178702 sqlite3_mutex_leave(p: db->mutex);
178703 return pRet;
178704}
178705
178706#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
178707/*
178708** Register a callback to be invoked each time a row is updated,
178709** inserted or deleted using this database connection.
178710*/
178711SQLITE_API void *sqlite3_preupdate_hook(
178712 sqlite3 *db, /* Attach the hook to this database */
178713 void(*xCallback)( /* Callback function */
178714 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
178715 void *pArg /* First callback argument */
178716){
178717 void *pRet;
178718 sqlite3_mutex_enter(db->mutex);
178719 pRet = db->pPreUpdateArg;
178720 db->xPreUpdateCallback = xCallback;
178721 db->pPreUpdateArg = pArg;
178722 sqlite3_mutex_leave(db->mutex);
178723 return pRet;
178724}
178725#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
178726
178727/*
178728** Register a function to be invoked prior to each autovacuum that
178729** determines the number of pages to vacuum.
178730*/
178731SQLITE_API int sqlite3_autovacuum_pages(
178732 sqlite3 *db, /* Attach the hook to this database */
178733 unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
178734 void *pArg, /* Argument to the function */
178735 void (*xDestructor)(void*) /* Destructor for pArg */
178736){
178737#ifdef SQLITE_ENABLE_API_ARMOR
178738 if( !sqlite3SafetyCheckOk(db) ){
178739 if( xDestructor ) xDestructor(pArg);
178740 return SQLITE_MISUSE_BKPT;
178741 }
178742#endif
178743 sqlite3_mutex_enter(p: db->mutex);
178744 if( db->xAutovacDestr ){
178745 db->xAutovacDestr(db->pAutovacPagesArg);
178746 }
178747 db->xAutovacPages = xCallback;
178748 db->pAutovacPagesArg = pArg;
178749 db->xAutovacDestr = xDestructor;
178750 sqlite3_mutex_leave(p: db->mutex);
178751 return SQLITE_OK;
178752}
178753
178754
178755#ifndef SQLITE_OMIT_WAL
178756/*
178757** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
178758** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
178759** is greater than sqlite3.pWalArg cast to an integer (the value configured by
178760** wal_autocheckpoint()).
178761*/
178762SQLITE_PRIVATE int sqlite3WalDefaultHook(
178763 void *pClientData, /* Argument */
178764 sqlite3 *db, /* Connection */
178765 const char *zDb, /* Database */
178766 int nFrame /* Size of WAL */
178767){
178768 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
178769 sqlite3BeginBenignMalloc();
178770 sqlite3_wal_checkpoint(db, zDb);
178771 sqlite3EndBenignMalloc();
178772 }
178773 return SQLITE_OK;
178774}
178775#endif /* SQLITE_OMIT_WAL */
178776
178777/*
178778** Configure an sqlite3_wal_hook() callback to automatically checkpoint
178779** a database after committing a transaction if there are nFrame or
178780** more frames in the log file. Passing zero or a negative value as the
178781** nFrame parameter disables automatic checkpoints entirely.
178782**
178783** The callback registered by this function replaces any existing callback
178784** registered using sqlite3_wal_hook(). Likewise, registering a callback
178785** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
178786** configured by this function.
178787*/
178788SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
178789#ifdef SQLITE_OMIT_WAL
178790 UNUSED_PARAMETER(db);
178791 UNUSED_PARAMETER(nFrame);
178792#else
178793#ifdef SQLITE_ENABLE_API_ARMOR
178794 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
178795#endif
178796 if( nFrame>0 ){
178797 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
178798 }else{
178799 sqlite3_wal_hook(db, 0, 0);
178800 }
178801#endif
178802 return SQLITE_OK;
178803}
178804
178805/*
178806** Register a callback to be invoked each time a transaction is written
178807** into the write-ahead-log by this database connection.
178808*/
178809SQLITE_API void *sqlite3_wal_hook(
178810 sqlite3 *db, /* Attach the hook to this db handle */
178811 int(*xCallback)(void *, sqlite3*, const char*, int),
178812 void *pArg /* First argument passed to xCallback() */
178813){
178814#ifndef SQLITE_OMIT_WAL
178815 void *pRet;
178816#ifdef SQLITE_ENABLE_API_ARMOR
178817 if( !sqlite3SafetyCheckOk(db) ){
178818 (void)SQLITE_MISUSE_BKPT;
178819 return 0;
178820 }
178821#endif
178822 sqlite3_mutex_enter(p: db->mutex);
178823 pRet = db->pWalArg;
178824 db->xWalCallback = xCallback;
178825 db->pWalArg = pArg;
178826 sqlite3_mutex_leave(p: db->mutex);
178827 return pRet;
178828#else
178829 return 0;
178830#endif
178831}
178832
178833/*
178834** Checkpoint database zDb.
178835*/
178836SQLITE_API int sqlite3_wal_checkpoint_v2(
178837 sqlite3 *db, /* Database handle */
178838 const char *zDb, /* Name of attached database (or NULL) */
178839 int eMode, /* SQLITE_CHECKPOINT_* value */
178840 int *pnLog, /* OUT: Size of WAL log in frames */
178841 int *pnCkpt /* OUT: Total number of frames checkpointed */
178842){
178843#ifdef SQLITE_OMIT_WAL
178844 return SQLITE_OK;
178845#else
178846 int rc; /* Return code */
178847 int iDb; /* Schema to checkpoint */
178848
178849#ifdef SQLITE_ENABLE_API_ARMOR
178850 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
178851#endif
178852
178853 /* Initialize the output variables to -1 in case an error occurs. */
178854 if( pnLog ) *pnLog = -1;
178855 if( pnCkpt ) *pnCkpt = -1;
178856
178857 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
178858 assert( SQLITE_CHECKPOINT_FULL==1 );
178859 assert( SQLITE_CHECKPOINT_RESTART==2 );
178860 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
178861 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
178862 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
178863 ** mode: */
178864 return SQLITE_MISUSE;
178865 }
178866
178867 sqlite3_mutex_enter(p: db->mutex);
178868 if( zDb && zDb[0] ){
178869 iDb = sqlite3FindDbName(db, zName: zDb);
178870 }else{
178871 iDb = SQLITE_MAX_DB; /* This means process all schemas */
178872 }
178873 if( iDb<0 ){
178874 rc = SQLITE_ERROR;
178875 sqlite3ErrorWithMsg(db, SQLITE_ERROR, zFormat: "unknown database: %s", zDb);
178876 }else{
178877 db->busyHandler.nBusy = 0;
178878 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
178879 sqlite3Error(db, err_code: rc);
178880 }
178881 rc = sqlite3ApiExit(db, rc);
178882
178883 /* If there are no active statements, clear the interrupt flag at this
178884 ** point. */
178885 if( db->nVdbeActive==0 ){
178886 AtomicStore(&db->u1.isInterrupted, 0);
178887 }
178888
178889 sqlite3_mutex_leave(p: db->mutex);
178890 return rc;
178891#endif
178892}
178893
178894
178895/*
178896** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
178897** to contains a zero-length string, all attached databases are
178898** checkpointed.
178899*/
178900SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
178901 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
178902 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
178903 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,pnLog: 0,pnCkpt: 0);
178904}
178905
178906#ifndef SQLITE_OMIT_WAL
178907/*
178908** Run a checkpoint on database iDb. This is a no-op if database iDb is
178909** not currently open in WAL mode.
178910**
178911** If a transaction is open on the database being checkpointed, this
178912** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
178913** an error occurs while running the checkpoint, an SQLite error code is
178914** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
178915**
178916** The mutex on database handle db should be held by the caller. The mutex
178917** associated with the specific b-tree being checkpointed is taken by
178918** this function while the checkpoint is running.
178919**
178920** If iDb is passed SQLITE_MAX_DB then all attached databases are
178921** checkpointed. If an error is encountered it is returned immediately -
178922** no attempt is made to checkpoint any remaining databases.
178923**
178924** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
178925** or TRUNCATE.
178926*/
178927SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
178928 int rc = SQLITE_OK; /* Return code */
178929 int i; /* Used to iterate through attached dbs */
178930 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
178931
178932 assert( sqlite3_mutex_held(db->mutex) );
178933 assert( !pnLog || *pnLog==-1 );
178934 assert( !pnCkpt || *pnCkpt==-1 );
178935 testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */
178936 testcase( iDb==SQLITE_MAX_DB );
178937
178938 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
178939 if( i==iDb || iDb==SQLITE_MAX_DB ){
178940 rc = sqlite3BtreeCheckpoint(p: db->aDb[i].pBt, eMode, pnLog, pnCkpt);
178941 pnLog = 0;
178942 pnCkpt = 0;
178943 if( rc==SQLITE_BUSY ){
178944 bBusy = 1;
178945 rc = SQLITE_OK;
178946 }
178947 }
178948 }
178949
178950 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
178951}
178952#endif /* SQLITE_OMIT_WAL */
178953
178954/*
178955** This function returns true if main-memory should be used instead of
178956** a temporary file for transient pager files and statement journals.
178957** The value returned depends on the value of db->temp_store (runtime
178958** parameter) and the compile time value of SQLITE_TEMP_STORE. The
178959** following table describes the relationship between these two values
178960** and this functions return value.
178961**
178962** SQLITE_TEMP_STORE db->temp_store Location of temporary database
178963** ----------------- -------------- ------------------------------
178964** 0 any file (return 0)
178965** 1 1 file (return 0)
178966** 1 2 memory (return 1)
178967** 1 0 file (return 0)
178968** 2 1 file (return 0)
178969** 2 2 memory (return 1)
178970** 2 0 memory (return 1)
178971** 3 any memory (return 1)
178972*/
178973SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
178974#if SQLITE_TEMP_STORE==1
178975 return ( db->temp_store==2 );
178976#endif
178977#if SQLITE_TEMP_STORE==2
178978 return ( db->temp_store!=1 );
178979#endif
178980#if SQLITE_TEMP_STORE==3
178981 UNUSED_PARAMETER(db);
178982 return 1;
178983#endif
178984#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
178985 UNUSED_PARAMETER(db);
178986 return 0;
178987#endif
178988}
178989
178990/*
178991** Return UTF-8 encoded English language explanation of the most recent
178992** error.
178993*/
178994SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
178995 const char *z;
178996 if( !db ){
178997 return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
178998 }
178999 if( !sqlite3SafetyCheckSickOrOk(db) ){
179000 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
179001 }
179002 sqlite3_mutex_enter(p: db->mutex);
179003 if( db->mallocFailed ){
179004 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
179005 }else{
179006 testcase( db->pErr==0 );
179007 z = db->errCode ? (char*)sqlite3_value_text(pVal: db->pErr) : 0;
179008 assert( !db->mallocFailed );
179009 if( z==0 ){
179010 z = sqlite3ErrStr(rc: db->errCode);
179011 }
179012 }
179013 sqlite3_mutex_leave(p: db->mutex);
179014 return z;
179015}
179016
179017/*
179018** Return the byte offset of the most recent error
179019*/
179020SQLITE_API int sqlite3_error_offset(sqlite3 *db){
179021 int iOffset = -1;
179022 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
179023 sqlite3_mutex_enter(p: db->mutex);
179024 iOffset = db->errByteOffset;
179025 sqlite3_mutex_leave(p: db->mutex);
179026 }
179027 return iOffset;
179028}
179029
179030#ifndef SQLITE_OMIT_UTF16
179031/*
179032** Return UTF-16 encoded English language explanation of the most recent
179033** error.
179034*/
179035SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
179036 static const u16 outOfMem[] = {
179037 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
179038 };
179039 static const u16 misuse[] = {
179040 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
179041 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
179042 'm', 'i', 's', 'u', 's', 'e', 0
179043 };
179044
179045 const void *z;
179046 if( !db ){
179047 return (void *)outOfMem;
179048 }
179049 if( !sqlite3SafetyCheckSickOrOk(db) ){
179050 return (void *)misuse;
179051 }
179052 sqlite3_mutex_enter(p: db->mutex);
179053 if( db->mallocFailed ){
179054 z = (void *)outOfMem;
179055 }else{
179056 z = sqlite3_value_text16(pVal: db->pErr);
179057 if( z==0 ){
179058 sqlite3ErrorWithMsg(db, err_code: db->errCode, zFormat: sqlite3ErrStr(rc: db->errCode));
179059 z = sqlite3_value_text16(pVal: db->pErr);
179060 }
179061 /* A malloc() may have failed within the call to sqlite3_value_text16()
179062 ** above. If this is the case, then the db->mallocFailed flag needs to
179063 ** be cleared before returning. Do this directly, instead of via
179064 ** sqlite3ApiExit(), to avoid setting the database handle error message.
179065 */
179066 sqlite3OomClear(db);
179067 }
179068 sqlite3_mutex_leave(p: db->mutex);
179069 return z;
179070}
179071#endif /* SQLITE_OMIT_UTF16 */
179072
179073/*
179074** Return the most recent error code generated by an SQLite routine. If NULL is
179075** passed to this function, we assume a malloc() failed during sqlite3_open().
179076*/
179077SQLITE_API int sqlite3_errcode(sqlite3 *db){
179078 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
179079 return SQLITE_MISUSE_BKPT;
179080 }
179081 if( !db || db->mallocFailed ){
179082 return SQLITE_NOMEM_BKPT;
179083 }
179084 return db->errCode & db->errMask;
179085}
179086SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
179087 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
179088 return SQLITE_MISUSE_BKPT;
179089 }
179090 if( !db || db->mallocFailed ){
179091 return SQLITE_NOMEM_BKPT;
179092 }
179093 return db->errCode;
179094}
179095SQLITE_API int sqlite3_system_errno(sqlite3 *db){
179096 return db ? db->iSysErrno : 0;
179097}
179098
179099/*
179100** Return a string that describes the kind of error specified in the
179101** argument. For now, this simply calls the internal sqlite3ErrStr()
179102** function.
179103*/
179104SQLITE_API const char *sqlite3_errstr(int rc){
179105 return sqlite3ErrStr(rc);
179106}
179107
179108/*
179109** Create a new collating function for database "db". The name is zName
179110** and the encoding is enc.
179111*/
179112static int createCollation(
179113 sqlite3* db,
179114 const char *zName,
179115 u8 enc,
179116 void* pCtx,
179117 int(*xCompare)(void*,int,const void*,int,const void*),
179118 void(*xDel)(void*)
179119){
179120 CollSeq *pColl;
179121 int enc2;
179122
179123 assert( sqlite3_mutex_held(db->mutex) );
179124
179125 /* If SQLITE_UTF16 is specified as the encoding type, transform this
179126 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
179127 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
179128 */
179129 enc2 = enc;
179130 testcase( enc2==SQLITE_UTF16 );
179131 testcase( enc2==SQLITE_UTF16_ALIGNED );
179132 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
179133 enc2 = SQLITE_UTF16NATIVE;
179134 }
179135 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
179136 return SQLITE_MISUSE_BKPT;
179137 }
179138
179139 /* Check if this call is removing or replacing an existing collation
179140 ** sequence. If so, and there are active VMs, return busy. If there
179141 ** are no active VMs, invalidate any pre-compiled statements.
179142 */
179143 pColl = sqlite3FindCollSeq(db, enc: (u8)enc2, zName, create: 0);
179144 if( pColl && pColl->xCmp ){
179145 if( db->nVdbeActive ){
179146 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
179147 zFormat: "unable to delete/modify collation sequence due to active statements");
179148 return SQLITE_BUSY;
179149 }
179150 sqlite3ExpirePreparedStatements(db, iCode: 0);
179151
179152 /* If collation sequence pColl was created directly by a call to
179153 ** sqlite3_create_collation, and not generated by synthCollSeq(),
179154 ** then any copies made by synthCollSeq() need to be invalidated.
179155 ** Also, collation destructor - CollSeq.xDel() - function may need
179156 ** to be called.
179157 */
179158 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
179159 CollSeq *aColl = sqlite3HashFind(pH: &db->aCollSeq, pKey: zName);
179160 int j;
179161 for(j=0; j<3; j++){
179162 CollSeq *p = &aColl[j];
179163 if( p->enc==pColl->enc ){
179164 if( p->xDel ){
179165 p->xDel(p->pUser);
179166 }
179167 p->xCmp = 0;
179168 }
179169 }
179170 }
179171 }
179172
179173 pColl = sqlite3FindCollSeq(db, enc: (u8)enc2, zName, create: 1);
179174 if( pColl==0 ) return SQLITE_NOMEM_BKPT;
179175 pColl->xCmp = xCompare;
179176 pColl->pUser = pCtx;
179177 pColl->xDel = xDel;
179178 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
179179 sqlite3Error(db, SQLITE_OK);
179180 return SQLITE_OK;
179181}
179182
179183
179184/*
179185** This array defines hard upper bounds on limit values. The
179186** initializer must be kept in sync with the SQLITE_LIMIT_*
179187** #defines in sqlite3.h.
179188*/
179189static const int aHardLimit[] = {
179190 SQLITE_MAX_LENGTH,
179191 SQLITE_MAX_SQL_LENGTH,
179192 SQLITE_MAX_COLUMN,
179193 SQLITE_MAX_EXPR_DEPTH,
179194 SQLITE_MAX_COMPOUND_SELECT,
179195 SQLITE_MAX_VDBE_OP,
179196 SQLITE_MAX_FUNCTION_ARG,
179197 SQLITE_MAX_ATTACHED,
179198 SQLITE_MAX_LIKE_PATTERN_LENGTH,
179199 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
179200 SQLITE_MAX_TRIGGER_DEPTH,
179201 SQLITE_MAX_WORKER_THREADS,
179202};
179203
179204/*
179205** Make sure the hard limits are set to reasonable values
179206*/
179207#if SQLITE_MAX_LENGTH<100
179208# error SQLITE_MAX_LENGTH must be at least 100
179209#endif
179210#if SQLITE_MAX_SQL_LENGTH<100
179211# error SQLITE_MAX_SQL_LENGTH must be at least 100
179212#endif
179213#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
179214# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
179215#endif
179216#if SQLITE_MAX_COMPOUND_SELECT<2
179217# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
179218#endif
179219#if SQLITE_MAX_VDBE_OP<40
179220# error SQLITE_MAX_VDBE_OP must be at least 40
179221#endif
179222#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
179223# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
179224#endif
179225#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
179226# error SQLITE_MAX_ATTACHED must be between 0 and 125
179227#endif
179228#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
179229# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
179230#endif
179231#if SQLITE_MAX_COLUMN>32767
179232# error SQLITE_MAX_COLUMN must not exceed 32767
179233#endif
179234#if SQLITE_MAX_TRIGGER_DEPTH<1
179235# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
179236#endif
179237#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
179238# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
179239#endif
179240
179241
179242/*
179243** Change the value of a limit. Report the old value.
179244** If an invalid limit index is supplied, report -1.
179245** Make no changes but still report the old value if the
179246** new limit is negative.
179247**
179248** A new lower limit does not shrink existing constructs.
179249** It merely prevents new constructs that exceed the limit
179250** from forming.
179251*/
179252SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
179253 int oldLimit;
179254
179255#ifdef SQLITE_ENABLE_API_ARMOR
179256 if( !sqlite3SafetyCheckOk(db) ){
179257 (void)SQLITE_MISUSE_BKPT;
179258 return -1;
179259 }
179260#endif
179261
179262 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
179263 ** there is a hard upper bound set at compile-time by a C preprocessor
179264 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
179265 ** "_MAX_".)
179266 */
179267 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
179268 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
179269 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
179270 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
179271 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
179272 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
179273 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
179274 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
179275 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
179276 SQLITE_MAX_LIKE_PATTERN_LENGTH );
179277 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
179278 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
179279 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
179280 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
179281
179282
179283 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
179284 return -1;
179285 }
179286 oldLimit = db->aLimit[limitId];
179287 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
179288 if( newLimit>aHardLimit[limitId] ){
179289 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
179290 }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH ){
179291 newLimit = 1;
179292 }
179293 db->aLimit[limitId] = newLimit;
179294 }
179295 return oldLimit; /* IMP: R-53341-35419 */
179296}
179297
179298/*
179299** This function is used to parse both URIs and non-URI filenames passed by the
179300** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
179301** URIs specified as part of ATTACH statements.
179302**
179303** The first argument to this function is the name of the VFS to use (or
179304** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
179305** query parameter. The second argument contains the URI (or non-URI filename)
179306** itself. When this function is called the *pFlags variable should contain
179307** the default flags to open the database handle with. The value stored in
179308** *pFlags may be updated before returning if the URI filename contains
179309** "cache=xxx" or "mode=xxx" query parameters.
179310**
179311** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
179312** the VFS that should be used to open the database file. *pzFile is set to
179313** point to a buffer containing the name of the file to open. The value
179314** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
179315** and is in the same format as names created using sqlite3_create_filename().
179316** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
179317** the value returned in *pzFile to avoid a memory leak.
179318**
179319** If an error occurs, then an SQLite error code is returned and *pzErrMsg
179320** may be set to point to a buffer containing an English language error
179321** message. It is the responsibility of the caller to eventually release
179322** this buffer by calling sqlite3_free().
179323*/
179324SQLITE_PRIVATE int sqlite3ParseUri(
179325 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
179326 const char *zUri, /* Nul-terminated URI to parse */
179327 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
179328 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
179329 char **pzFile, /* OUT: Filename component of URI */
179330 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
179331){
179332 int rc = SQLITE_OK;
179333 unsigned int flags = *pFlags;
179334 const char *zVfs = zDefaultVfs;
179335 char *zFile;
179336 char c;
179337 int nUri = sqlite3Strlen30(z: zUri);
179338
179339 assert( *pzErrMsg==0 );
179340
179341 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
179342 || AtomicLoad(&sqlite3GlobalConfig.bOpenUri)) /* IMP: R-51689-46548 */
179343 && nUri>=5 && memcmp(s1: zUri, s2: "file:", n: 5)==0 /* IMP: R-57884-37496 */
179344 ){
179345 char *zOpt;
179346 int eState; /* Parser state when parsing URI */
179347 int iIn; /* Input character index */
179348 int iOut = 0; /* Output character index */
179349 u64 nByte = nUri+8; /* Bytes of space to allocate */
179350
179351 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
179352 ** method that there may be extra parameters following the file-name. */
179353 flags |= SQLITE_OPEN_URI;
179354
179355 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
179356 zFile = sqlite3_malloc64(n: nByte);
179357 if( !zFile ) return SQLITE_NOMEM_BKPT;
179358
179359 memset(s: zFile, c: 0, n: 4); /* 4-byte of 0x00 is the start of DB name marker */
179360 zFile += 4;
179361
179362 iIn = 5;
179363#ifdef SQLITE_ALLOW_URI_AUTHORITY
179364 if( strncmp(zUri+5, "///", 3)==0 ){
179365 iIn = 7;
179366 /* The following condition causes URIs with five leading / characters
179367 ** like file://///host/path to be converted into UNCs like //host/path.
179368 ** The correct URI for that UNC has only two or four leading / characters
179369 ** file://host/path or file:////host/path. But 5 leading slashes is a
179370 ** common error, we are told, so we handle it as a special case. */
179371 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
179372 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
179373 iIn = 16;
179374 }
179375#else
179376 /* Discard the scheme and authority segments of the URI. */
179377 if( zUri[5]=='/' && zUri[6]=='/' ){
179378 iIn = 7;
179379 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
179380 if( iIn!=7 && (iIn!=16 || memcmp(s1: "localhost", s2: &zUri[7], n: 9)) ){
179381 *pzErrMsg = sqlite3_mprintf(zFormat: "invalid uri authority: %.*s",
179382 iIn-7, &zUri[7]);
179383 rc = SQLITE_ERROR;
179384 goto parse_uri_out;
179385 }
179386 }
179387#endif
179388
179389 /* Copy the filename and any query parameters into the zFile buffer.
179390 ** Decode %HH escape codes along the way.
179391 **
179392 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
179393 ** on the parsing context. As follows:
179394 **
179395 ** 0: Parsing file-name.
179396 ** 1: Parsing name section of a name=value query parameter.
179397 ** 2: Parsing value section of a name=value query parameter.
179398 */
179399 eState = 0;
179400 while( (c = zUri[iIn])!=0 && c!='#' ){
179401 iIn++;
179402 if( c=='%'
179403 && sqlite3Isxdigit(zUri[iIn])
179404 && sqlite3Isxdigit(zUri[iIn+1])
179405 ){
179406 int octet = (sqlite3HexToInt(h: zUri[iIn++]) << 4);
179407 octet += sqlite3HexToInt(h: zUri[iIn++]);
179408
179409 assert( octet>=0 && octet<256 );
179410 if( octet==0 ){
179411#ifndef SQLITE_ENABLE_URI_00_ERROR
179412 /* This branch is taken when "%00" appears within the URI. In this
179413 ** case we ignore all text in the remainder of the path, name or
179414 ** value currently being parsed. So ignore the current character
179415 ** and skip to the next "?", "=" or "&", as appropriate. */
179416 while( (c = zUri[iIn])!=0 && c!='#'
179417 && (eState!=0 || c!='?')
179418 && (eState!=1 || (c!='=' && c!='&'))
179419 && (eState!=2 || c!='&')
179420 ){
179421 iIn++;
179422 }
179423 continue;
179424#else
179425 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
179426 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
179427 rc = SQLITE_ERROR;
179428 goto parse_uri_out;
179429#endif
179430 }
179431 c = octet;
179432 }else if( eState==1 && (c=='&' || c=='=') ){
179433 if( zFile[iOut-1]==0 ){
179434 /* An empty option name. Ignore this option altogether. */
179435 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
179436 continue;
179437 }
179438 if( c=='&' ){
179439 zFile[iOut++] = '\0';
179440 }else{
179441 eState = 2;
179442 }
179443 c = 0;
179444 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
179445 c = 0;
179446 eState = 1;
179447 }
179448 zFile[iOut++] = c;
179449 }
179450 if( eState==1 ) zFile[iOut++] = '\0';
179451 memset(s: zFile+iOut, c: 0, n: 4); /* end-of-options + empty journal filenames */
179452
179453 /* Check if there were any options specified that should be interpreted
179454 ** here. Options that are interpreted here include "vfs" and those that
179455 ** correspond to flags that may be passed to the sqlite3_open_v2()
179456 ** method. */
179457 zOpt = &zFile[sqlite3Strlen30(z: zFile)+1];
179458 while( zOpt[0] ){
179459 int nOpt = sqlite3Strlen30(z: zOpt);
179460 char *zVal = &zOpt[nOpt+1];
179461 int nVal = sqlite3Strlen30(z: zVal);
179462
179463 if( nOpt==3 && memcmp(s1: "vfs", s2: zOpt, n: 3)==0 ){
179464 zVfs = zVal;
179465 }else{
179466 struct OpenMode {
179467 const char *z;
179468 int mode;
179469 } *aMode = 0;
179470 char *zModeType = 0;
179471 int mask = 0;
179472 int limit = 0;
179473
179474 if( nOpt==5 && memcmp(s1: "cache", s2: zOpt, n: 5)==0 ){
179475 static struct OpenMode aCacheMode[] = {
179476 { "shared", SQLITE_OPEN_SHAREDCACHE },
179477 { "private", SQLITE_OPEN_PRIVATECACHE },
179478 { 0, 0 }
179479 };
179480
179481 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
179482 aMode = aCacheMode;
179483 limit = mask;
179484 zModeType = "cache";
179485 }
179486 if( nOpt==4 && memcmp(s1: "mode", s2: zOpt, n: 4)==0 ){
179487 static struct OpenMode aOpenMode[] = {
179488 { "ro", SQLITE_OPEN_READONLY },
179489 { "rw", SQLITE_OPEN_READWRITE },
179490 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
179491 { "memory", SQLITE_OPEN_MEMORY },
179492 { 0, 0 }
179493 };
179494
179495 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
179496 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
179497 aMode = aOpenMode;
179498 limit = mask & flags;
179499 zModeType = "access";
179500 }
179501
179502 if( aMode ){
179503 int i;
179504 int mode = 0;
179505 for(i=0; aMode[i].z; i++){
179506 const char *z = aMode[i].z;
179507 if( nVal==sqlite3Strlen30(z) && 0==memcmp(s1: zVal, s2: z, n: nVal) ){
179508 mode = aMode[i].mode;
179509 break;
179510 }
179511 }
179512 if( mode==0 ){
179513 *pzErrMsg = sqlite3_mprintf(zFormat: "no such %s mode: %s", zModeType, zVal);
179514 rc = SQLITE_ERROR;
179515 goto parse_uri_out;
179516 }
179517 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
179518 *pzErrMsg = sqlite3_mprintf(zFormat: "%s mode not allowed: %s",
179519 zModeType, zVal);
179520 rc = SQLITE_PERM;
179521 goto parse_uri_out;
179522 }
179523 flags = (flags & ~mask) | mode;
179524 }
179525 }
179526
179527 zOpt = &zVal[nVal+1];
179528 }
179529
179530 }else{
179531 zFile = sqlite3_malloc64(n: nUri+8);
179532 if( !zFile ) return SQLITE_NOMEM_BKPT;
179533 memset(s: zFile, c: 0, n: 4);
179534 zFile += 4;
179535 if( nUri ){
179536 memcpy(dest: zFile, src: zUri, n: nUri);
179537 }
179538 memset(s: zFile+nUri, c: 0, n: 4);
179539 flags &= ~SQLITE_OPEN_URI;
179540 }
179541
179542 *ppVfs = sqlite3_vfs_find(zVfs);
179543 if( *ppVfs==0 ){
179544 *pzErrMsg = sqlite3_mprintf(zFormat: "no such vfs: %s", zVfs);
179545 rc = SQLITE_ERROR;
179546 }
179547 parse_uri_out:
179548 if( rc!=SQLITE_OK ){
179549 sqlite3_free_filename(zFile);
179550 zFile = 0;
179551 }
179552 *pFlags = flags;
179553 *pzFile = zFile;
179554 return rc;
179555}
179556
179557/*
179558** This routine does the core work of extracting URI parameters from a
179559** database filename for the sqlite3_uri_parameter() interface.
179560*/
179561static const char *uriParameter(const char *zFilename, const char *zParam){
179562 zFilename += sqlite3Strlen30(z: zFilename) + 1;
179563 while( ALWAYS(zFilename!=0) && zFilename[0] ){
179564 int x = strcmp(s1: zFilename, s2: zParam);
179565 zFilename += sqlite3Strlen30(z: zFilename) + 1;
179566 if( x==0 ) return zFilename;
179567 zFilename += sqlite3Strlen30(z: zFilename) + 1;
179568 }
179569 return 0;
179570}
179571
179572
179573
179574/*
179575** This routine does the work of opening a database on behalf of
179576** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
179577** is UTF-8 encoded.
179578*/
179579static int openDatabase(
179580 const char *zFilename, /* Database filename UTF-8 encoded */
179581 sqlite3 **ppDb, /* OUT: Returned database handle */
179582 unsigned int flags, /* Operational flags */
179583 const char *zVfs /* Name of the VFS to use */
179584){
179585 sqlite3 *db; /* Store allocated handle here */
179586 int rc; /* Return code */
179587 int isThreadsafe; /* True for threadsafe connections */
179588 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
179589 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
179590 int i; /* Loop counter */
179591
179592#ifdef SQLITE_ENABLE_API_ARMOR
179593 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
179594#endif
179595 *ppDb = 0;
179596#ifndef SQLITE_OMIT_AUTOINIT
179597 rc = sqlite3_initialize();
179598 if( rc ) return rc;
179599#endif
179600
179601 if( sqlite3GlobalConfig.bCoreMutex==0 ){
179602 isThreadsafe = 0;
179603 }else if( flags & SQLITE_OPEN_NOMUTEX ){
179604 isThreadsafe = 0;
179605 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
179606 isThreadsafe = 1;
179607 }else{
179608 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
179609 }
179610
179611 if( flags & SQLITE_OPEN_PRIVATECACHE ){
179612 flags &= ~SQLITE_OPEN_SHAREDCACHE;
179613 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
179614 flags |= SQLITE_OPEN_SHAREDCACHE;
179615 }
179616
179617 /* Remove harmful bits from the flags parameter
179618 **
179619 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
179620 ** dealt with in the previous code block. Besides these, the only
179621 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
179622 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
179623 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
179624 ** bits. Silently mask off all other flags.
179625 */
179626 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
179627 SQLITE_OPEN_EXCLUSIVE |
179628 SQLITE_OPEN_MAIN_DB |
179629 SQLITE_OPEN_TEMP_DB |
179630 SQLITE_OPEN_TRANSIENT_DB |
179631 SQLITE_OPEN_MAIN_JOURNAL |
179632 SQLITE_OPEN_TEMP_JOURNAL |
179633 SQLITE_OPEN_SUBJOURNAL |
179634 SQLITE_OPEN_SUPER_JOURNAL |
179635 SQLITE_OPEN_NOMUTEX |
179636 SQLITE_OPEN_FULLMUTEX |
179637 SQLITE_OPEN_WAL
179638 );
179639
179640 /* Allocate the sqlite data structure */
179641 db = sqlite3MallocZero( n: sizeof(sqlite3) );
179642 if( db==0 ) goto opendb_out;
179643 if( isThreadsafe
179644#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
179645 || sqlite3GlobalConfig.bCoreMutex
179646#endif
179647 ){
179648 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
179649 if( db->mutex==0 ){
179650 sqlite3_free(p: db);
179651 db = 0;
179652 goto opendb_out;
179653 }
179654 if( isThreadsafe==0 ){
179655 sqlite3MutexWarnOnContention(db->mutex);
179656 }
179657 }
179658 sqlite3_mutex_enter(p: db->mutex);
179659 db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff;
179660 db->nDb = 2;
179661 db->eOpenState = SQLITE_STATE_BUSY;
179662 db->aDb = db->aDbStatic;
179663 db->lookaside.bDisable = 1;
179664 db->lookaside.sz = 0;
179665
179666 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
179667 memcpy(dest: db->aLimit, src: aHardLimit, n: sizeof(db->aLimit));
179668 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
179669 db->autoCommit = 1;
179670 db->nextAutovac = -1;
179671 db->szMmap = sqlite3GlobalConfig.szMmap;
179672 db->nextPagesize = 0;
179673 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
179674#ifdef SQLITE_ENABLE_SORTER_MMAP
179675 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
179676 ** the temporary files used to do external sorts (see code in vdbesort.c)
179677 ** is disabled. It can still be used either by defining
179678 ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the
179679 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
179680 db->nMaxSorterMmap = 0x7FFFFFFF;
179681#endif
179682 db->flags |= SQLITE_ShortColNames
179683 | SQLITE_EnableTrigger
179684 | SQLITE_EnableView
179685 | SQLITE_CacheSpill
179686#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
179687 | SQLITE_TrustedSchema
179688#endif
179689/* The SQLITE_DQS compile-time option determines the default settings
179690** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
179691**
179692** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
179693** ---------- ----------------------- -----------------------
179694** undefined on on
179695** 3 on on
179696** 2 on off
179697** 1 off on
179698** 0 off off
179699**
179700** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
179701** and so that is the default. But developers are encouraged to use
179702** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
179703*/
179704#if !defined(SQLITE_DQS)
179705# define SQLITE_DQS 3
179706#endif
179707#if (SQLITE_DQS&1)==1
179708 | SQLITE_DqsDML
179709#endif
179710#if (SQLITE_DQS&2)==2
179711 | SQLITE_DqsDDL
179712#endif
179713
179714#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
179715 | SQLITE_AutoIndex
179716#endif
179717#if SQLITE_DEFAULT_CKPTFULLFSYNC
179718 | SQLITE_CkptFullFSync
179719#endif
179720#if SQLITE_DEFAULT_FILE_FORMAT<4
179721 | SQLITE_LegacyFileFmt
179722#endif
179723#ifdef SQLITE_ENABLE_LOAD_EXTENSION
179724 | SQLITE_LoadExtension
179725#endif
179726#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
179727 | SQLITE_RecTriggers
179728#endif
179729#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
179730 | SQLITE_ForeignKeys
179731#endif
179732#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
179733 | SQLITE_ReverseOrder
179734#endif
179735#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
179736 | SQLITE_CellSizeCk
179737#endif
179738#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
179739 | SQLITE_Fts3Tokenizer
179740#endif
179741#if defined(SQLITE_ENABLE_QPSG)
179742 | SQLITE_EnableQPSG
179743#endif
179744#if defined(SQLITE_DEFAULT_DEFENSIVE)
179745 | SQLITE_Defensive
179746#endif
179747#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
179748 | SQLITE_LegacyAlter
179749#endif
179750#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
179751 | SQLITE_StmtScanStatus
179752#endif
179753 ;
179754 sqlite3HashInit(pNew: &db->aCollSeq);
179755#ifndef SQLITE_OMIT_VIRTUALTABLE
179756 sqlite3HashInit(pNew: &db->aModule);
179757#endif
179758
179759 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
179760 ** and UTF-16, so add a version for each to avoid any unnecessary
179761 ** conversions. The only error that can occur here is a malloc() failure.
179762 **
179763 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
179764 ** functions:
179765 */
179766 createCollation(db, zName: sqlite3StrBINARY, SQLITE_UTF8, pCtx: 0, xCompare: binCollFunc, xDel: 0);
179767 createCollation(db, zName: sqlite3StrBINARY, SQLITE_UTF16BE, pCtx: 0, xCompare: binCollFunc, xDel: 0);
179768 createCollation(db, zName: sqlite3StrBINARY, SQLITE_UTF16LE, pCtx: 0, xCompare: binCollFunc, xDel: 0);
179769 createCollation(db, zName: "NOCASE", SQLITE_UTF8, pCtx: 0, xCompare: nocaseCollatingFunc, xDel: 0);
179770 createCollation(db, zName: "RTRIM", SQLITE_UTF8, pCtx: 0, xCompare: rtrimCollFunc, xDel: 0);
179771 if( db->mallocFailed ){
179772 goto opendb_out;
179773 }
179774
179775#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
179776 /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
179777 if( zFilename && zFilename[0]==':' ){
179778 if( strcmp(zFilename, ":localStorage:")==0 ){
179779 zFilename = "file:local?vfs=kvvfs";
179780 flags |= SQLITE_OPEN_URI;
179781 }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
179782 zFilename = "file:session?vfs=kvvfs";
179783 flags |= SQLITE_OPEN_URI;
179784 }
179785 }
179786#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
179787
179788 /* Parse the filename/URI argument
179789 **
179790 ** Only allow sensible combinations of bits in the flags argument.
179791 ** Throw an error if any non-sense combination is used. If we
179792 ** do not block illegal combinations here, it could trigger
179793 ** assert() statements in deeper layers. Sensible combinations
179794 ** are:
179795 **
179796 ** 1: SQLITE_OPEN_READONLY
179797 ** 2: SQLITE_OPEN_READWRITE
179798 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
179799 */
179800 db->openFlags = flags;
179801 assert( SQLITE_OPEN_READONLY == 0x01 );
179802 assert( SQLITE_OPEN_READWRITE == 0x02 );
179803 assert( SQLITE_OPEN_CREATE == 0x04 );
179804 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
179805 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
179806 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
179807 if( ((1<<(flags&7)) & 0x46)==0 ){
179808 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
179809 }else{
179810 rc = sqlite3ParseUri(zDefaultVfs: zVfs, zUri: zFilename, pFlags: &flags, ppVfs: &db->pVfs, pzFile: &zOpen, pzErrMsg: &zErrMsg);
179811 }
179812 if( rc!=SQLITE_OK ){
179813 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
179814 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: zErrMsg ? "%s" : 0, zErrMsg);
179815 sqlite3_free(p: zErrMsg);
179816 goto opendb_out;
179817 }
179818 assert( db->pVfs!=0 );
179819#if SQLITE_OS_KV || defined(SQLITE_OS_KV_OPTIONAL)
179820 if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
179821 db->temp_store = 2;
179822 }
179823#endif
179824
179825 /* Open the backend database driver */
179826 rc = sqlite3BtreeOpen(pVfs: db->pVfs, zFilename: zOpen, db, ppBtree: &db->aDb[0].pBt, flags: 0,
179827 vfsFlags: flags | SQLITE_OPEN_MAIN_DB);
179828 if( rc!=SQLITE_OK ){
179829 if( rc==SQLITE_IOERR_NOMEM ){
179830 rc = SQLITE_NOMEM_BKPT;
179831 }
179832 sqlite3Error(db, err_code: rc);
179833 goto opendb_out;
179834 }
179835 sqlite3BtreeEnter(p: db->aDb[0].pBt);
179836 db->aDb[0].pSchema = sqlite3SchemaGet(db, pBt: db->aDb[0].pBt);
179837 if( !db->mallocFailed ){
179838 sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
179839 }
179840 sqlite3BtreeLeave(p: db->aDb[0].pBt);
179841 db->aDb[1].pSchema = sqlite3SchemaGet(db, pBt: 0);
179842
179843 /* The default safety_level for the main database is FULL; for the temp
179844 ** database it is OFF. This matches the pager layer defaults.
179845 */
179846 db->aDb[0].zDbSName = "main";
179847 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
179848 db->aDb[1].zDbSName = "temp";
179849 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
179850
179851 db->eOpenState = SQLITE_STATE_OPEN;
179852 if( db->mallocFailed ){
179853 goto opendb_out;
179854 }
179855
179856 /* Register all built-in functions, but do not attempt to read the
179857 ** database schema yet. This is delayed until the first time the database
179858 ** is accessed.
179859 */
179860 sqlite3Error(db, SQLITE_OK);
179861 sqlite3RegisterPerConnectionBuiltinFunctions(db);
179862 rc = sqlite3_errcode(db);
179863
179864
179865 /* Load compiled-in extensions */
179866 for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){
179867 rc = sqlite3BuiltinExtensions[i](db);
179868 }
179869
179870 /* Load automatic extensions - extensions that have been registered
179871 ** using the sqlite3_automatic_extension() API.
179872 */
179873 if( rc==SQLITE_OK ){
179874 sqlite3AutoLoadExtensions(db);
179875 rc = sqlite3_errcode(db);
179876 if( rc!=SQLITE_OK ){
179877 goto opendb_out;
179878 }
179879 }
179880
179881#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
179882 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
179883 ** option gives access to internal functions by default.
179884 ** Testing use only!!! */
179885 db->mDbFlags |= DBFLAG_InternalFunc;
179886#endif
179887
179888 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
179889 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
179890 ** mode. Doing nothing at all also makes NORMAL the default.
179891 */
179892#ifdef SQLITE_DEFAULT_LOCKING_MODE
179893 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
179894 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
179895 SQLITE_DEFAULT_LOCKING_MODE);
179896#endif
179897
179898 if( rc ) sqlite3Error(db, err_code: rc);
179899
179900 /* Enable the lookaside-malloc subsystem */
179901 setupLookaside(db, pBuf: 0, sqlite3GlobalConfig.szLookaside,
179902 sqlite3GlobalConfig.nLookaside);
179903
179904 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
179905
179906opendb_out:
179907 if( db ){
179908 assert( db->mutex!=0 || isThreadsafe==0
179909 || sqlite3GlobalConfig.bFullMutex==0 );
179910 sqlite3_mutex_leave(p: db->mutex);
179911 }
179912 rc = sqlite3_errcode(db);
179913 assert( db!=0 || (rc&0xff)==SQLITE_NOMEM );
179914 if( (rc&0xff)==SQLITE_NOMEM ){
179915 sqlite3_close(db);
179916 db = 0;
179917 }else if( rc!=SQLITE_OK ){
179918 db->eOpenState = SQLITE_STATE_SICK;
179919 }
179920 *ppDb = db;
179921#ifdef SQLITE_ENABLE_SQLLOG
179922 if( sqlite3GlobalConfig.xSqllog ){
179923 /* Opening a db handle. Fourth parameter is passed 0. */
179924 void *pArg = sqlite3GlobalConfig.pSqllogArg;
179925 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
179926 }
179927#endif
179928 sqlite3_free_filename(zOpen);
179929 return rc;
179930}
179931
179932
179933/*
179934** Open a new database handle.
179935*/
179936SQLITE_API int sqlite3_open(
179937 const char *zFilename,
179938 sqlite3 **ppDb
179939){
179940 return openDatabase(zFilename, ppDb,
179941 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, zVfs: 0);
179942}
179943SQLITE_API int sqlite3_open_v2(
179944 const char *filename, /* Database filename (UTF-8) */
179945 sqlite3 **ppDb, /* OUT: SQLite db handle */
179946 int flags, /* Flags */
179947 const char *zVfs /* Name of VFS module to use */
179948){
179949 return openDatabase(zFilename: filename, ppDb, flags: (unsigned int)flags, zVfs);
179950}
179951
179952#ifndef SQLITE_OMIT_UTF16
179953/*
179954** Open a new database handle.
179955*/
179956SQLITE_API int sqlite3_open16(
179957 const void *zFilename,
179958 sqlite3 **ppDb
179959){
179960 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
179961 sqlite3_value *pVal;
179962 int rc;
179963
179964#ifdef SQLITE_ENABLE_API_ARMOR
179965 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
179966#endif
179967 *ppDb = 0;
179968#ifndef SQLITE_OMIT_AUTOINIT
179969 rc = sqlite3_initialize();
179970 if( rc ) return rc;
179971#endif
179972 if( zFilename==0 ) zFilename = "\000\000";
179973 pVal = sqlite3ValueNew(db: 0);
179974 sqlite3ValueSetStr(v: pVal, n: -1, z: zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
179975 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
179976 if( zFilename8 ){
179977 rc = openDatabase(zFilename: zFilename8, ppDb,
179978 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, zVfs: 0);
179979 assert( *ppDb || rc==SQLITE_NOMEM );
179980 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
179981 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
179982 }
179983 }else{
179984 rc = SQLITE_NOMEM_BKPT;
179985 }
179986 sqlite3ValueFree(v: pVal);
179987
179988 return rc & 0xff;
179989}
179990#endif /* SQLITE_OMIT_UTF16 */
179991
179992/*
179993** Register a new collation sequence with the database handle db.
179994*/
179995SQLITE_API int sqlite3_create_collation(
179996 sqlite3* db,
179997 const char *zName,
179998 int enc,
179999 void* pCtx,
180000 int(*xCompare)(void*,int,const void*,int,const void*)
180001){
180002 return sqlite3_create_collation_v2(db, zName, eTextRep: enc, pArg: pCtx, xCompare, xDestroy: 0);
180003}
180004
180005/*
180006** Register a new collation sequence with the database handle db.
180007*/
180008SQLITE_API int sqlite3_create_collation_v2(
180009 sqlite3* db,
180010 const char *zName,
180011 int enc,
180012 void* pCtx,
180013 int(*xCompare)(void*,int,const void*,int,const void*),
180014 void(*xDel)(void*)
180015){
180016 int rc;
180017
180018#ifdef SQLITE_ENABLE_API_ARMOR
180019 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
180020#endif
180021 sqlite3_mutex_enter(p: db->mutex);
180022 assert( !db->mallocFailed );
180023 rc = createCollation(db, zName, enc: (u8)enc, pCtx, xCompare, xDel);
180024 rc = sqlite3ApiExit(db, rc);
180025 sqlite3_mutex_leave(p: db->mutex);
180026 return rc;
180027}
180028
180029#ifndef SQLITE_OMIT_UTF16
180030/*
180031** Register a new collation sequence with the database handle db.
180032*/
180033SQLITE_API int sqlite3_create_collation16(
180034 sqlite3* db,
180035 const void *zName,
180036 int enc,
180037 void* pCtx,
180038 int(*xCompare)(void*,int,const void*,int,const void*)
180039){
180040 int rc = SQLITE_OK;
180041 char *zName8;
180042
180043#ifdef SQLITE_ENABLE_API_ARMOR
180044 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
180045#endif
180046 sqlite3_mutex_enter(p: db->mutex);
180047 assert( !db->mallocFailed );
180048 zName8 = sqlite3Utf16to8(db, z: zName, nByte: -1, SQLITE_UTF16NATIVE);
180049 if( zName8 ){
180050 rc = createCollation(db, zName: zName8, enc: (u8)enc, pCtx, xCompare, xDel: 0);
180051 sqlite3DbFree(db, p: zName8);
180052 }
180053 rc = sqlite3ApiExit(db, rc);
180054 sqlite3_mutex_leave(p: db->mutex);
180055 return rc;
180056}
180057#endif /* SQLITE_OMIT_UTF16 */
180058
180059/*
180060** Register a collation sequence factory callback with the database handle
180061** db. Replace any previously installed collation sequence factory.
180062*/
180063SQLITE_API int sqlite3_collation_needed(
180064 sqlite3 *db,
180065 void *pCollNeededArg,
180066 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
180067){
180068#ifdef SQLITE_ENABLE_API_ARMOR
180069 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
180070#endif
180071 sqlite3_mutex_enter(p: db->mutex);
180072 db->xCollNeeded = xCollNeeded;
180073 db->xCollNeeded16 = 0;
180074 db->pCollNeededArg = pCollNeededArg;
180075 sqlite3_mutex_leave(p: db->mutex);
180076 return SQLITE_OK;
180077}
180078
180079#ifndef SQLITE_OMIT_UTF16
180080/*
180081** Register a collation sequence factory callback with the database handle
180082** db. Replace any previously installed collation sequence factory.
180083*/
180084SQLITE_API int sqlite3_collation_needed16(
180085 sqlite3 *db,
180086 void *pCollNeededArg,
180087 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
180088){
180089#ifdef SQLITE_ENABLE_API_ARMOR
180090 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
180091#endif
180092 sqlite3_mutex_enter(p: db->mutex);
180093 db->xCollNeeded = 0;
180094 db->xCollNeeded16 = xCollNeeded16;
180095 db->pCollNeededArg = pCollNeededArg;
180096 sqlite3_mutex_leave(p: db->mutex);
180097 return SQLITE_OK;
180098}
180099#endif /* SQLITE_OMIT_UTF16 */
180100
180101#ifndef SQLITE_OMIT_DEPRECATED
180102/*
180103** This function is now an anachronism. It used to be used to recover from a
180104** malloc() failure, but SQLite now does this automatically.
180105*/
180106SQLITE_API int sqlite3_global_recover(void){
180107 return SQLITE_OK;
180108}
180109#endif
180110
180111/*
180112** Test to see whether or not the database connection is in autocommit
180113** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
180114** by default. Autocommit is disabled by a BEGIN statement and reenabled
180115** by the next COMMIT or ROLLBACK.
180116*/
180117SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
180118#ifdef SQLITE_ENABLE_API_ARMOR
180119 if( !sqlite3SafetyCheckOk(db) ){
180120 (void)SQLITE_MISUSE_BKPT;
180121 return 0;
180122 }
180123#endif
180124 return db->autoCommit;
180125}
180126
180127/*
180128** The following routines are substitutes for constants SQLITE_CORRUPT,
180129** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
180130** constants. They serve two purposes:
180131**
180132** 1. Serve as a convenient place to set a breakpoint in a debugger
180133** to detect when version error conditions occurs.
180134**
180135** 2. Invoke sqlite3_log() to provide the source code location where
180136** a low-level error is first detected.
180137*/
180138SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
180139 sqlite3_log(iErrCode: iErr, zFormat: "%s at line %d of [%.10s]",
180140 zType, lineno, 20+sqlite3_sourceid());
180141 return iErr;
180142}
180143SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
180144 testcase( sqlite3GlobalConfig.xLog!=0 );
180145 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zType: "database corruption");
180146}
180147SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
180148 testcase( sqlite3GlobalConfig.xLog!=0 );
180149 return sqlite3ReportError(SQLITE_MISUSE, lineno, zType: "misuse");
180150}
180151SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
180152 testcase( sqlite3GlobalConfig.xLog!=0 );
180153 return sqlite3ReportError(SQLITE_CANTOPEN, lineno, zType: "cannot open file");
180154}
180155#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
180156SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
180157 char zMsg[100];
180158 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
180159 testcase( sqlite3GlobalConfig.xLog!=0 );
180160 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
180161}
180162#endif
180163#ifdef SQLITE_DEBUG
180164SQLITE_PRIVATE int sqlite3NomemError(int lineno){
180165 testcase( sqlite3GlobalConfig.xLog!=0 );
180166 return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
180167}
180168SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
180169 testcase( sqlite3GlobalConfig.xLog!=0 );
180170 return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
180171}
180172#endif
180173
180174#ifndef SQLITE_OMIT_DEPRECATED
180175/*
180176** This is a convenience routine that makes sure that all thread-specific
180177** data for this thread has been deallocated.
180178**
180179** SQLite no longer uses thread-specific data so this routine is now a
180180** no-op. It is retained for historical compatibility.
180181*/
180182SQLITE_API void sqlite3_thread_cleanup(void){
180183}
180184#endif
180185
180186/*
180187** Return meta information about a specific column of a database table.
180188** See comment in sqlite3.h (sqlite.h.in) for details.
180189*/
180190SQLITE_API int sqlite3_table_column_metadata(
180191 sqlite3 *db, /* Connection handle */
180192 const char *zDbName, /* Database name or NULL */
180193 const char *zTableName, /* Table name */
180194 const char *zColumnName, /* Column name */
180195 char const **pzDataType, /* OUTPUT: Declared data type */
180196 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
180197 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
180198 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
180199 int *pAutoinc /* OUTPUT: True if column is auto-increment */
180200){
180201 int rc;
180202 char *zErrMsg = 0;
180203 Table *pTab = 0;
180204 Column *pCol = 0;
180205 int iCol = 0;
180206 char const *zDataType = 0;
180207 char const *zCollSeq = 0;
180208 int notnull = 0;
180209 int primarykey = 0;
180210 int autoinc = 0;
180211
180212
180213#ifdef SQLITE_ENABLE_API_ARMOR
180214 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
180215 return SQLITE_MISUSE_BKPT;
180216 }
180217#endif
180218
180219 /* Ensure the database schema has been loaded */
180220 sqlite3_mutex_enter(p: db->mutex);
180221 sqlite3BtreeEnterAll(db);
180222 rc = sqlite3Init(db, pzErrMsg: &zErrMsg);
180223 if( SQLITE_OK!=rc ){
180224 goto error_out;
180225 }
180226
180227 /* Locate the table in question */
180228 pTab = sqlite3FindTable(db, zName: zTableName, zDatabase: zDbName);
180229 if( !pTab || IsView(pTab) ){
180230 pTab = 0;
180231 goto error_out;
180232 }
180233
180234 /* Find the column for which info is requested */
180235 if( zColumnName==0 ){
180236 /* Query for existence of table only */
180237 }else{
180238 for(iCol=0; iCol<pTab->nCol; iCol++){
180239 pCol = &pTab->aCol[iCol];
180240 if( 0==sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zColumnName) ){
180241 break;
180242 }
180243 }
180244 if( iCol==pTab->nCol ){
180245 if( HasRowid(pTab) && sqlite3IsRowid(z: zColumnName) ){
180246 iCol = pTab->iPKey;
180247 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
180248 }else{
180249 pTab = 0;
180250 goto error_out;
180251 }
180252 }
180253 }
180254
180255 /* The following block stores the meta information that will be returned
180256 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
180257 ** and autoinc. At this point there are two possibilities:
180258 **
180259 ** 1. The specified column name was rowid", "oid" or "_rowid_"
180260 ** and there is no explicitly declared IPK column.
180261 **
180262 ** 2. The table is not a view and the column name identified an
180263 ** explicitly declared column. Copy meta information from *pCol.
180264 */
180265 if( pCol ){
180266 zDataType = sqlite3ColumnType(pCol,zDflt: 0);
180267 zCollSeq = sqlite3ColumnColl(pCol);
180268 notnull = pCol->notNull!=0;
180269 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
180270 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
180271 }else{
180272 zDataType = "INTEGER";
180273 primarykey = 1;
180274 }
180275 if( !zCollSeq ){
180276 zCollSeq = sqlite3StrBINARY;
180277 }
180278
180279error_out:
180280 sqlite3BtreeLeaveAll(db);
180281
180282 /* Whether the function call succeeded or failed, set the output parameters
180283 ** to whatever their local counterparts contain. If an error did occur,
180284 ** this has the effect of zeroing all output parameters.
180285 */
180286 if( pzDataType ) *pzDataType = zDataType;
180287 if( pzCollSeq ) *pzCollSeq = zCollSeq;
180288 if( pNotNull ) *pNotNull = notnull;
180289 if( pPrimaryKey ) *pPrimaryKey = primarykey;
180290 if( pAutoinc ) *pAutoinc = autoinc;
180291
180292 if( SQLITE_OK==rc && !pTab ){
180293 sqlite3DbFree(db, p: zErrMsg);
180294 zErrMsg = sqlite3MPrintf(db, zFormat: "no such table column: %s.%s", zTableName,
180295 zColumnName);
180296 rc = SQLITE_ERROR;
180297 }
180298 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: (zErrMsg?"%s":0), zErrMsg);
180299 sqlite3DbFree(db, p: zErrMsg);
180300 rc = sqlite3ApiExit(db, rc);
180301 sqlite3_mutex_leave(p: db->mutex);
180302 return rc;
180303}
180304
180305/*
180306** Sleep for a little while. Return the amount of time slept.
180307*/
180308SQLITE_API int sqlite3_sleep(int ms){
180309 sqlite3_vfs *pVfs;
180310 int rc;
180311 pVfs = sqlite3_vfs_find(zVfs: 0);
180312 if( pVfs==0 ) return 0;
180313
180314 /* This function works in milliseconds, but the underlying OsSleep()
180315 ** API uses microseconds. Hence the 1000's.
180316 */
180317 rc = (sqlite3OsSleep(pVfs, nMicro: ms<0 ? 0 : 1000*ms)/1000);
180318 return rc;
180319}
180320
180321/*
180322** Enable or disable the extended result codes.
180323*/
180324SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
180325#ifdef SQLITE_ENABLE_API_ARMOR
180326 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
180327#endif
180328 sqlite3_mutex_enter(p: db->mutex);
180329 db->errMask = onoff ? 0xffffffff : 0xff;
180330 sqlite3_mutex_leave(p: db->mutex);
180331 return SQLITE_OK;
180332}
180333
180334/*
180335** Invoke the xFileControl method on a particular database.
180336*/
180337SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
180338 int rc = SQLITE_ERROR;
180339 Btree *pBtree;
180340
180341#ifdef SQLITE_ENABLE_API_ARMOR
180342 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
180343#endif
180344 sqlite3_mutex_enter(p: db->mutex);
180345 pBtree = sqlite3DbNameToBtree(db, zDbName);
180346 if( pBtree ){
180347 Pager *pPager;
180348 sqlite3_file *fd;
180349 sqlite3BtreeEnter(p: pBtree);
180350 pPager = sqlite3BtreePager(p: pBtree);
180351 assert( pPager!=0 );
180352 fd = sqlite3PagerFile(pPager);
180353 assert( fd!=0 );
180354 if( op==SQLITE_FCNTL_FILE_POINTER ){
180355 *(sqlite3_file**)pArg = fd;
180356 rc = SQLITE_OK;
180357 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
180358 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
180359 rc = SQLITE_OK;
180360 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
180361 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
180362 rc = SQLITE_OK;
180363 }else if( op==SQLITE_FCNTL_DATA_VERSION ){
180364 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
180365 rc = SQLITE_OK;
180366 }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){
180367 int iNew = *(int*)pArg;
180368 *(int*)pArg = sqlite3BtreeGetRequestedReserve(p: pBtree);
180369 if( iNew>=0 && iNew<=255 ){
180370 sqlite3BtreeSetPageSize(p: pBtree, pageSize: 0, nReserve: iNew, iFix: 0);
180371 }
180372 rc = SQLITE_OK;
180373 }else if( op==SQLITE_FCNTL_RESET_CACHE ){
180374 sqlite3BtreeClearCache(p: pBtree);
180375 rc = SQLITE_OK;
180376 }else{
180377 int nSave = db->busyHandler.nBusy;
180378 rc = sqlite3OsFileControl(id: fd, op, pArg);
180379 db->busyHandler.nBusy = nSave;
180380 }
180381 sqlite3BtreeLeave(p: pBtree);
180382 }
180383 sqlite3_mutex_leave(p: db->mutex);
180384 return rc;
180385}
180386
180387/*
180388** Interface to the testing logic.
180389*/
180390SQLITE_API int sqlite3_test_control(int op, ...){
180391 int rc = 0;
180392#ifdef SQLITE_UNTESTABLE
180393 UNUSED_PARAMETER(op);
180394#else
180395 va_list ap;
180396 va_start(ap, op);
180397 switch( op ){
180398
180399 /*
180400 ** Save the current state of the PRNG.
180401 */
180402 case SQLITE_TESTCTRL_PRNG_SAVE: {
180403 sqlite3PrngSaveState();
180404 break;
180405 }
180406
180407 /*
180408 ** Restore the state of the PRNG to the last state saved using
180409 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
180410 ** this verb acts like PRNG_RESET.
180411 */
180412 case SQLITE_TESTCTRL_PRNG_RESTORE: {
180413 sqlite3PrngRestoreState();
180414 break;
180415 }
180416
180417 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
180418 **
180419 ** Control the seed for the pseudo-random number generator (PRNG) that
180420 ** is built into SQLite. Cases:
180421 **
180422 ** x!=0 && db!=0 Seed the PRNG to the current value of the
180423 ** schema cookie in the main database for db, or
180424 ** x if the schema cookie is zero. This case
180425 ** is convenient to use with database fuzzers
180426 ** as it allows the fuzzer some control over the
180427 ** the PRNG seed.
180428 **
180429 ** x!=0 && db==0 Seed the PRNG to the value of x.
180430 **
180431 ** x==0 && db==0 Revert to default behavior of using the
180432 ** xRandomness method on the primary VFS.
180433 **
180434 ** This test-control also resets the PRNG so that the new seed will
180435 ** be used for the next call to sqlite3_randomness().
180436 */
180437#ifndef SQLITE_OMIT_WSD
180438 case SQLITE_TESTCTRL_PRNG_SEED: {
180439 int x = va_arg(ap, int);
180440 int y;
180441 sqlite3 *db = va_arg(ap, sqlite3*);
180442 assert( db==0 || db->aDb[0].pSchema!=0 );
180443 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
180444 sqlite3Config.iPrngSeed = x;
180445 sqlite3_randomness(N: 0,pBuf: 0);
180446 break;
180447 }
180448#endif
180449
180450 /*
180451 ** sqlite3_test_control(BITVEC_TEST, size, program)
180452 **
180453 ** Run a test against a Bitvec object of size. The program argument
180454 ** is an array of integers that defines the test. Return -1 on a
180455 ** memory allocation error, 0 on success, or non-zero for an error.
180456 ** See the sqlite3BitvecBuiltinTest() for additional information.
180457 */
180458 case SQLITE_TESTCTRL_BITVEC_TEST: {
180459 int sz = va_arg(ap, int);
180460 int *aProg = va_arg(ap, int*);
180461 rc = sqlite3BitvecBuiltinTest(sz, aOp: aProg);
180462 break;
180463 }
180464
180465 /*
180466 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
180467 **
180468 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
180469 ** if xCallback is not NULL.
180470 **
180471 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
180472 ** is called immediately after installing the new callback and the return
180473 ** value from sqlite3FaultSim(0) becomes the return from
180474 ** sqlite3_test_control().
180475 */
180476 case SQLITE_TESTCTRL_FAULT_INSTALL: {
180477 /* A bug in MSVC prevents it from understanding pointers to functions
180478 ** types in the second argument to va_arg(). Work around the problem
180479 ** using a typedef.
180480 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
180481 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
180482 ** of the link above to see the original text.
180483 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
180484 */
180485 typedef int(*sqlite3FaultFuncType)(int);
180486 sqlite3GlobalConfig.xTestCallback = va_arg(ap, sqlite3FaultFuncType);
180487 rc = sqlite3FaultSim(iTest: 0);
180488 break;
180489 }
180490
180491 /*
180492 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
180493 **
180494 ** Register hooks to call to indicate which malloc() failures
180495 ** are benign.
180496 */
180497 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
180498 typedef void (*void_function)(void);
180499 void_function xBenignBegin;
180500 void_function xBenignEnd;
180501 xBenignBegin = va_arg(ap, void_function);
180502 xBenignEnd = va_arg(ap, void_function);
180503 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
180504 break;
180505 }
180506
180507 /*
180508 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
180509 **
180510 ** Set the PENDING byte to the value in the argument, if X>0.
180511 ** Make no changes if X==0. Return the value of the pending byte
180512 ** as it existing before this routine was called.
180513 **
180514 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
180515 ** an incompatible database file format. Changing the PENDING byte
180516 ** while any database connection is open results in undefined and
180517 ** deleterious behavior.
180518 */
180519 case SQLITE_TESTCTRL_PENDING_BYTE: {
180520 rc = PENDING_BYTE;
180521#ifndef SQLITE_OMIT_WSD
180522 {
180523 unsigned int newVal = va_arg(ap, unsigned int);
180524 if( newVal ) sqlite3PendingByte = newVal;
180525 }
180526#endif
180527 break;
180528 }
180529
180530 /*
180531 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
180532 **
180533 ** This action provides a run-time test to see whether or not
180534 ** assert() was enabled at compile-time. If X is true and assert()
180535 ** is enabled, then the return value is true. If X is true and
180536 ** assert() is disabled, then the return value is zero. If X is
180537 ** false and assert() is enabled, then the assertion fires and the
180538 ** process aborts. If X is false and assert() is disabled, then the
180539 ** return value is zero.
180540 */
180541 case SQLITE_TESTCTRL_ASSERT: {
180542 volatile int x = 0;
180543 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
180544 rc = x;
180545#if defined(SQLITE_DEBUG)
180546 /* Invoke these debugging routines so that the compiler does not
180547 ** issue "defined but not used" warnings. */
180548 if( x==9999 ){
180549 sqlite3ShowExpr(0);
180550 sqlite3ShowExpr(0);
180551 sqlite3ShowExprList(0);
180552 sqlite3ShowIdList(0);
180553 sqlite3ShowSrcList(0);
180554 sqlite3ShowWith(0);
180555 sqlite3ShowUpsert(0);
180556#ifndef SQLITE_OMIT_TRIGGER
180557 sqlite3ShowTriggerStep(0);
180558 sqlite3ShowTriggerStepList(0);
180559 sqlite3ShowTrigger(0);
180560 sqlite3ShowTriggerList(0);
180561#endif
180562#ifndef SQLITE_OMIT_WINDOWFUNC
180563 sqlite3ShowWindow(0);
180564 sqlite3ShowWinFunc(0);
180565#endif
180566 sqlite3ShowSelect(0);
180567 }
180568#endif
180569 break;
180570 }
180571
180572
180573 /*
180574 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
180575 **
180576 ** This action provides a run-time test to see how the ALWAYS and
180577 ** NEVER macros were defined at compile-time.
180578 **
180579 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
180580 **
180581 ** The recommended test is X==2. If the return value is 2, that means
180582 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
180583 ** default setting. If the return value is 1, then ALWAYS() is either
180584 ** hard-coded to true or else it asserts if its argument is false.
180585 ** The first behavior (hard-coded to true) is the case if
180586 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
180587 ** behavior (assert if the argument to ALWAYS() is false) is the case if
180588 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
180589 **
180590 ** The run-time test procedure might look something like this:
180591 **
180592 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
180593 ** // ALWAYS() and NEVER() are no-op pass-through macros
180594 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
180595 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
180596 ** }else{
180597 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
180598 ** }
180599 */
180600 case SQLITE_TESTCTRL_ALWAYS: {
180601 int x = va_arg(ap,int);
180602 rc = x ? ALWAYS(x) : 0;
180603 break;
180604 }
180605
180606 /*
180607 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
180608 **
180609 ** The integer returned reveals the byte-order of the computer on which
180610 ** SQLite is running:
180611 **
180612 ** 1 big-endian, determined at run-time
180613 ** 10 little-endian, determined at run-time
180614 ** 432101 big-endian, determined at compile-time
180615 ** 123410 little-endian, determined at compile-time
180616 */
180617 case SQLITE_TESTCTRL_BYTEORDER: {
180618 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
180619 break;
180620 }
180621
180622 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
180623 **
180624 ** Enable or disable various optimizations for testing purposes. The
180625 ** argument N is a bitmask of optimizations to be disabled. For normal
180626 ** operation N should be 0. The idea is that a test program (like the
180627 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
180628 ** with various optimizations disabled to verify that the same answer
180629 ** is obtained in every case.
180630 */
180631 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
180632 sqlite3 *db = va_arg(ap, sqlite3*);
180633 db->dbOptFlags = va_arg(ap, u32);
180634 break;
180635 }
180636
180637 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt);
180638 **
180639 ** If parameter onoff is 1, subsequent calls to localtime() fail.
180640 ** If 2, then invoke xAlt() instead of localtime(). If 0, normal
180641 ** processing.
180642 **
180643 ** xAlt arguments are void pointers, but they really want to be:
180644 **
180645 ** int xAlt(const time_t*, struct tm*);
180646 **
180647 ** xAlt should write results in to struct tm object of its 2nd argument
180648 ** and return zero on success, or return non-zero on failure.
180649 */
180650 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
180651 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
180652 if( sqlite3GlobalConfig.bLocaltimeFault==2 ){
180653 typedef int(*sqlite3LocaltimeType)(const void*,void*);
180654 sqlite3GlobalConfig.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType);
180655 }else{
180656 sqlite3GlobalConfig.xAltLocaltime = 0;
180657 }
180658 break;
180659 }
180660
180661 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
180662 **
180663 ** Toggle the ability to use internal functions on or off for
180664 ** the database connection given in the argument.
180665 */
180666 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
180667 sqlite3 *db = va_arg(ap, sqlite3*);
180668 db->mDbFlags ^= DBFLAG_InternalFunc;
180669 break;
180670 }
180671
180672 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
180673 **
180674 ** Set or clear a flag that indicates that the database file is always well-
180675 ** formed and never corrupt. This flag is clear by default, indicating that
180676 ** database files might have arbitrary corruption. Setting the flag during
180677 ** testing causes certain assert() statements in the code to be activated
180678 ** that demonstrate invariants on well-formed database files.
180679 */
180680 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
180681 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
180682 break;
180683 }
180684
180685 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
180686 **
180687 ** Set or clear a flag that causes SQLite to verify that type, name,
180688 ** and tbl_name fields of the sqlite_schema table. This is normally
180689 ** on, but it is sometimes useful to turn it off for testing.
180690 **
180691 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
180692 ** verification of rootpage numbers when parsing the schema. This
180693 ** is useful to make it easier to reach strange internal error states
180694 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
180695 ** in production.
180696 */
180697 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
180698 sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
180699 break;
180700 }
180701
180702 /* Set the threshold at which OP_Once counters reset back to zero.
180703 ** By default this is 0x7ffffffe (over 2 billion), but that value is
180704 ** too big to test in a reasonable amount of time, so this control is
180705 ** provided to set a small and easily reachable reset value.
180706 */
180707 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
180708 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
180709 break;
180710 }
180711
180712 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
180713 **
180714 ** Set the VDBE coverage callback function to xCallback with context
180715 ** pointer ptr.
180716 */
180717 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
180718#ifdef SQLITE_VDBE_COVERAGE
180719 typedef void (*branch_callback)(void*,unsigned int,
180720 unsigned char,unsigned char);
180721 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
180722 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
180723#endif
180724 break;
180725 }
180726
180727 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
180728 case SQLITE_TESTCTRL_SORTER_MMAP: {
180729 sqlite3 *db = va_arg(ap, sqlite3*);
180730 db->nMaxSorterMmap = va_arg(ap, int);
180731 break;
180732 }
180733
180734 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
180735 **
180736 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
180737 ** not.
180738 */
180739 case SQLITE_TESTCTRL_ISINIT: {
180740 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
180741 break;
180742 }
180743
180744 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
180745 **
180746 ** This test control is used to create imposter tables. "db" is a pointer
180747 ** to the database connection. dbName is the database name (ex: "main" or
180748 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
180749 ** or off. "tnum" is the root page of the b-tree to which the imposter
180750 ** table should connect.
180751 **
180752 ** Enable imposter mode only when the schema has already been parsed. Then
180753 ** run a single CREATE TABLE statement to construct the imposter table in
180754 ** the parsed schema. Then turn imposter mode back off again.
180755 **
180756 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
180757 ** the schema to be reparsed the next time it is needed. This has the
180758 ** effect of erasing all imposter tables.
180759 */
180760 case SQLITE_TESTCTRL_IMPOSTER: {
180761 sqlite3 *db = va_arg(ap, sqlite3*);
180762 int iDb;
180763 sqlite3_mutex_enter(p: db->mutex);
180764 iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
180765 if( iDb>=0 ){
180766 db->init.iDb = iDb;
180767 db->init.busy = db->init.imposterTable = va_arg(ap,int);
180768 db->init.newTnum = va_arg(ap,int);
180769 if( db->init.busy==0 && db->init.newTnum>0 ){
180770 sqlite3ResetAllSchemasOfConnection(db);
180771 }
180772 }
180773 sqlite3_mutex_leave(p: db->mutex);
180774 break;
180775 }
180776
180777#if defined(YYCOVERAGE)
180778 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
180779 **
180780 ** This test control (only available when SQLite is compiled with
180781 ** -DYYCOVERAGE) writes a report onto "out" that shows all
180782 ** state/lookahead combinations in the parser state machine
180783 ** which are never exercised. If any state is missed, make the
180784 ** return code SQLITE_ERROR.
180785 */
180786 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
180787 FILE *out = va_arg(ap, FILE*);
180788 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
180789 break;
180790 }
180791#endif /* defined(YYCOVERAGE) */
180792
180793 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
180794 **
180795 ** This test-control causes the most recent sqlite3_result_int64() value
180796 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
180797 ** MEM_IntReal values only arise during an INSERT operation of integer
180798 ** values into a REAL column, so they can be challenging to test. This
180799 ** test-control enables us to write an intreal() SQL function that can
180800 ** inject an intreal() value at arbitrary places in an SQL statement,
180801 ** for testing purposes.
180802 */
180803 case SQLITE_TESTCTRL_RESULT_INTREAL: {
180804 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
180805 sqlite3ResultIntReal(pCtx);
180806 break;
180807 }
180808
180809 /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
180810 ** sqlite3 *db, // Database connection
180811 ** u64 *pnSeek // Write seek count here
180812 ** );
180813 **
180814 ** This test-control queries the seek-counter on the "main" database
180815 ** file. The seek-counter is written into *pnSeek and is then reset.
180816 ** The seek-count is only available if compiled with SQLITE_DEBUG.
180817 */
180818 case SQLITE_TESTCTRL_SEEK_COUNT: {
180819 sqlite3 *db = va_arg(ap, sqlite3*);
180820 u64 *pn = va_arg(ap, sqlite3_uint64*);
180821 *pn = sqlite3BtreeSeekCount(db->aDb->pBt);
180822 (void)db; /* Silence harmless unused variable warning */
180823 break;
180824 }
180825
180826 /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
180827 **
180828 ** "ptr" is a pointer to a u32.
180829 **
180830 ** op==0 Store the current sqlite3TreeTrace in *ptr
180831 ** op==1 Set sqlite3TreeTrace to the value *ptr
180832 ** op==2 Store the current sqlite3WhereTrace in *ptr
180833 ** op==3 Set sqlite3WhereTrace to the value *ptr
180834 */
180835 case SQLITE_TESTCTRL_TRACEFLAGS: {
180836 int opTrace = va_arg(ap, int);
180837 u32 *ptr = va_arg(ap, u32*);
180838 switch( opTrace ){
180839 case 0: *ptr = sqlite3TreeTrace; break;
180840 case 1: sqlite3TreeTrace = *ptr; break;
180841 case 2: *ptr = sqlite3WhereTrace; break;
180842 case 3: sqlite3WhereTrace = *ptr; break;
180843 }
180844 break;
180845 }
180846
180847 /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST,
180848 ** double fIn, // Input value
180849 ** int *pLogEst, // sqlite3LogEstFromDouble(fIn)
180850 ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst)
180851 ** int *pLogEst2 // sqlite3LogEst(*pInt)
180852 ** );
180853 **
180854 ** Test access for the LogEst conversion routines.
180855 */
180856 case SQLITE_TESTCTRL_LOGEST: {
180857 double rIn = va_arg(ap, double);
180858 LogEst rLogEst = sqlite3LogEstFromDouble(x: rIn);
180859 int *pI1 = va_arg(ap,int*);
180860 u64 *pU64 = va_arg(ap,u64*);
180861 int *pI2 = va_arg(ap,int*);
180862 *pI1 = rLogEst;
180863 *pU64 = sqlite3LogEstToInt(x: rLogEst);
180864 *pI2 = sqlite3LogEst(x: *pU64);
180865 break;
180866 }
180867
180868#if !defined(SQLITE_OMIT_WSD)
180869 /* sqlite3_test_control(SQLITE_TESTCTRL_USELONGDOUBLE, int X);
180870 **
180871 ** X<0 Make no changes to the bUseLongDouble. Just report value.
180872 ** X==0 Disable bUseLongDouble
180873 ** X==1 Enable bUseLongDouble
180874 ** X==2 Set bUseLongDouble to its default value for this platform
180875 */
180876 case SQLITE_TESTCTRL_USELONGDOUBLE: {
180877 int b = va_arg(ap, int);
180878 if( b==2 ) b = sizeof(LONGDOUBLE_TYPE)>8;
180879 if( b>=0 ) sqlite3Config.bUseLongDouble = b>0;
180880 rc = sqlite3Config.bUseLongDouble!=0;
180881 break;
180882 }
180883#endif
180884
180885
180886#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
180887 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
180888 **
180889 ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value
180890 ** of the id-th tuning parameter to *piValue. If "id" is between -1
180891 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
180892 ** tuning parameter into *piValue.
180893 **
180894 ** Tuning parameters are for use during transient development builds,
180895 ** to help find the best values for constants in the query planner.
180896 ** Access tuning parameters using the Tuning(ID) macro. Set the
180897 ** parameters in the CLI using ".testctrl tune ID VALUE".
180898 **
180899 ** Transient use only. Tuning parameters should not be used in
180900 ** checked-in code.
180901 */
180902 case SQLITE_TESTCTRL_TUNE: {
180903 int id = va_arg(ap, int);
180904 int *piValue = va_arg(ap, int*);
180905 if( id>0 && id<=SQLITE_NTUNE ){
180906 Tuning(id) = *piValue;
180907 }else if( id<0 && id>=-SQLITE_NTUNE ){
180908 *piValue = Tuning(-id);
180909 }else{
180910 rc = SQLITE_NOTFOUND;
180911 }
180912 break;
180913 }
180914#endif
180915 }
180916 va_end(ap);
180917#endif /* SQLITE_UNTESTABLE */
180918 return rc;
180919}
180920
180921/*
180922** The Pager stores the Database filename, Journal filename, and WAL filename
180923** consecutively in memory, in that order. The database filename is prefixed
180924** by four zero bytes. Locate the start of the database filename by searching
180925** backwards for the first byte following four consecutive zero bytes.
180926**
180927** This only works if the filename passed in was obtained from the Pager.
180928*/
180929static const char *databaseName(const char *zName){
180930 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
180931 zName--;
180932 }
180933 return zName;
180934}
180935
180936/*
180937** Append text z[] to the end of p[]. Return a pointer to the first
180938** character after then zero terminator on the new text in p[].
180939*/
180940static char *appendText(char *p, const char *z){
180941 size_t n = strlen(s: z);
180942 memcpy(dest: p, src: z, n: n+1);
180943 return p+n+1;
180944}
180945
180946/*
180947** Allocate memory to hold names for a database, journal file, WAL file,
180948** and query parameters. The pointer returned is valid for use by
180949** sqlite3_filename_database() and sqlite3_uri_parameter() and related
180950** functions.
180951**
180952** Memory layout must be compatible with that generated by the pager
180953** and expected by sqlite3_uri_parameter() and databaseName().
180954*/
180955SQLITE_API const char *sqlite3_create_filename(
180956 const char *zDatabase,
180957 const char *zJournal,
180958 const char *zWal,
180959 int nParam,
180960 const char **azParam
180961){
180962 sqlite3_int64 nByte;
180963 int i;
180964 char *pResult, *p;
180965 nByte = strlen(s: zDatabase) + strlen(s: zJournal) + strlen(s: zWal) + 10;
180966 for(i=0; i<nParam*2; i++){
180967 nByte += strlen(s: azParam[i])+1;
180968 }
180969 pResult = p = sqlite3_malloc64( n: nByte );
180970 if( p==0 ) return 0;
180971 memset(s: p, c: 0, n: 4);
180972 p += 4;
180973 p = appendText(p, z: zDatabase);
180974 for(i=0; i<nParam*2; i++){
180975 p = appendText(p, z: azParam[i]);
180976 }
180977 *(p++) = 0;
180978 p = appendText(p, z: zJournal);
180979 p = appendText(p, z: zWal);
180980 *(p++) = 0;
180981 *(p++) = 0;
180982 assert( (sqlite3_int64)(p - pResult)==nByte );
180983 return pResult + 4;
180984}
180985
180986/*
180987** Free memory obtained from sqlite3_create_filename(). It is a severe
180988** error to call this routine with any parameter other than a pointer
180989** previously obtained from sqlite3_create_filename() or a NULL pointer.
180990*/
180991SQLITE_API void sqlite3_free_filename(const char *p){
180992 if( p==0 ) return;
180993 p = databaseName(zName: p);
180994 sqlite3_free(p: (char*)p - 4);
180995}
180996
180997
180998/*
180999** This is a utility routine, useful to VFS implementations, that checks
181000** to see if a database file was a URI that contained a specific query
181001** parameter, and if so obtains the value of the query parameter.
181002**
181003** The zFilename argument is the filename pointer passed into the xOpen()
181004** method of a VFS implementation. The zParam argument is the name of the
181005** query parameter we seek. This routine returns the value of the zParam
181006** parameter if it exists. If the parameter does not exist, this routine
181007** returns a NULL pointer.
181008*/
181009SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
181010 if( zFilename==0 || zParam==0 ) return 0;
181011 zFilename = databaseName(zName: zFilename);
181012 return uriParameter(zFilename, zParam);
181013}
181014
181015/*
181016** Return a pointer to the name of Nth query parameter of the filename.
181017*/
181018SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
181019 if( zFilename==0 || N<0 ) return 0;
181020 zFilename = databaseName(zName: zFilename);
181021 zFilename += sqlite3Strlen30(z: zFilename) + 1;
181022 while( ALWAYS(zFilename) && zFilename[0] && (N--)>0 ){
181023 zFilename += sqlite3Strlen30(z: zFilename) + 1;
181024 zFilename += sqlite3Strlen30(z: zFilename) + 1;
181025 }
181026 return zFilename[0] ? zFilename : 0;
181027}
181028
181029/*
181030** Return a boolean value for a query parameter.
181031*/
181032SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
181033 const char *z = sqlite3_uri_parameter(zFilename, zParam);
181034 bDflt = bDflt!=0;
181035 return z ? sqlite3GetBoolean(z, dflt: bDflt) : bDflt;
181036}
181037
181038/*
181039** Return a 64-bit integer value for a query parameter.
181040*/
181041SQLITE_API sqlite3_int64 sqlite3_uri_int64(
181042 const char *zFilename, /* Filename as passed to xOpen */
181043 const char *zParam, /* URI parameter sought */
181044 sqlite3_int64 bDflt /* return if parameter is missing */
181045){
181046 const char *z = sqlite3_uri_parameter(zFilename, zParam);
181047 sqlite3_int64 v;
181048 if( z && sqlite3DecOrHexToI64(z, pOut: &v)==0 ){
181049 bDflt = v;
181050 }
181051 return bDflt;
181052}
181053
181054/*
181055** Translate a filename that was handed to a VFS routine into the corresponding
181056** database, journal, or WAL file.
181057**
181058** It is an error to pass this routine a filename string that was not
181059** passed into the VFS from the SQLite core. Doing so is similar to
181060** passing free() a pointer that was not obtained from malloc() - it is
181061** an error that we cannot easily detect but that will likely cause memory
181062** corruption.
181063*/
181064SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
181065 if( zFilename==0 ) return 0;
181066 return databaseName(zName: zFilename);
181067}
181068SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
181069 if( zFilename==0 ) return 0;
181070 zFilename = databaseName(zName: zFilename);
181071 zFilename += sqlite3Strlen30(z: zFilename) + 1;
181072 while( ALWAYS(zFilename) && zFilename[0] ){
181073 zFilename += sqlite3Strlen30(z: zFilename) + 1;
181074 zFilename += sqlite3Strlen30(z: zFilename) + 1;
181075 }
181076 return zFilename + 1;
181077}
181078SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
181079#ifdef SQLITE_OMIT_WAL
181080 return 0;
181081#else
181082 zFilename = sqlite3_filename_journal(zFilename);
181083 if( zFilename ) zFilename += sqlite3Strlen30(z: zFilename) + 1;
181084 return zFilename;
181085#endif
181086}
181087
181088/*
181089** Return the Btree pointer identified by zDbName. Return NULL if not found.
181090*/
181091SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
181092 int iDb = zDbName ? sqlite3FindDbName(db, zName: zDbName) : 0;
181093 return iDb<0 ? 0 : db->aDb[iDb].pBt;
181094}
181095
181096/*
181097** Return the name of the N-th database schema. Return NULL if N is out
181098** of range.
181099*/
181100SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N){
181101#ifdef SQLITE_ENABLE_API_ARMOR
181102 if( !sqlite3SafetyCheckOk(db) ){
181103 (void)SQLITE_MISUSE_BKPT;
181104 return 0;
181105 }
181106#endif
181107 if( N<0 || N>=db->nDb ){
181108 return 0;
181109 }else{
181110 return db->aDb[N].zDbSName;
181111 }
181112}
181113
181114/*
181115** Return the filename of the database associated with a database
181116** connection.
181117*/
181118SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
181119 Btree *pBt;
181120#ifdef SQLITE_ENABLE_API_ARMOR
181121 if( !sqlite3SafetyCheckOk(db) ){
181122 (void)SQLITE_MISUSE_BKPT;
181123 return 0;
181124 }
181125#endif
181126 pBt = sqlite3DbNameToBtree(db, zDbName);
181127 return pBt ? sqlite3BtreeGetFilename(p: pBt) : 0;
181128}
181129
181130/*
181131** Return 1 if database is read-only or 0 if read/write. Return -1 if
181132** no such database exists.
181133*/
181134SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
181135 Btree *pBt;
181136#ifdef SQLITE_ENABLE_API_ARMOR
181137 if( !sqlite3SafetyCheckOk(db) ){
181138 (void)SQLITE_MISUSE_BKPT;
181139 return -1;
181140 }
181141#endif
181142 pBt = sqlite3DbNameToBtree(db, zDbName);
181143 return pBt ? sqlite3BtreeIsReadonly(p: pBt) : -1;
181144}
181145
181146#ifdef SQLITE_ENABLE_SNAPSHOT
181147/*
181148** Obtain a snapshot handle for the snapshot of database zDb currently
181149** being read by handle db.
181150*/
181151SQLITE_API int sqlite3_snapshot_get(
181152 sqlite3 *db,
181153 const char *zDb,
181154 sqlite3_snapshot **ppSnapshot
181155){
181156 int rc = SQLITE_ERROR;
181157#ifndef SQLITE_OMIT_WAL
181158
181159#ifdef SQLITE_ENABLE_API_ARMOR
181160 if( !sqlite3SafetyCheckOk(db) ){
181161 return SQLITE_MISUSE_BKPT;
181162 }
181163#endif
181164 sqlite3_mutex_enter(db->mutex);
181165
181166 if( db->autoCommit==0 ){
181167 int iDb = sqlite3FindDbName(db, zDb);
181168 if( iDb==0 || iDb>1 ){
181169 Btree *pBt = db->aDb[iDb].pBt;
181170 if( SQLITE_TXN_WRITE!=sqlite3BtreeTxnState(pBt) ){
181171 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
181172 if( rc==SQLITE_OK ){
181173 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
181174 }
181175 }
181176 }
181177 }
181178
181179 sqlite3_mutex_leave(db->mutex);
181180#endif /* SQLITE_OMIT_WAL */
181181 return rc;
181182}
181183
181184/*
181185** Open a read-transaction on the snapshot identified by pSnapshot.
181186*/
181187SQLITE_API int sqlite3_snapshot_open(
181188 sqlite3 *db,
181189 const char *zDb,
181190 sqlite3_snapshot *pSnapshot
181191){
181192 int rc = SQLITE_ERROR;
181193#ifndef SQLITE_OMIT_WAL
181194
181195#ifdef SQLITE_ENABLE_API_ARMOR
181196 if( !sqlite3SafetyCheckOk(db) ){
181197 return SQLITE_MISUSE_BKPT;
181198 }
181199#endif
181200 sqlite3_mutex_enter(db->mutex);
181201 if( db->autoCommit==0 ){
181202 int iDb;
181203 iDb = sqlite3FindDbName(db, zDb);
181204 if( iDb==0 || iDb>1 ){
181205 Btree *pBt = db->aDb[iDb].pBt;
181206 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ){
181207 Pager *pPager = sqlite3BtreePager(pBt);
181208 int bUnlock = 0;
181209 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE ){
181210 if( db->nVdbeActive==0 ){
181211 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
181212 if( rc==SQLITE_OK ){
181213 bUnlock = 1;
181214 rc = sqlite3BtreeCommit(pBt);
181215 }
181216 }
181217 }else{
181218 rc = SQLITE_OK;
181219 }
181220 if( rc==SQLITE_OK ){
181221 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
181222 }
181223 if( rc==SQLITE_OK ){
181224 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
181225 sqlite3PagerSnapshotOpen(pPager, 0);
181226 }
181227 if( bUnlock ){
181228 sqlite3PagerSnapshotUnlock(pPager);
181229 }
181230 }
181231 }
181232 }
181233
181234 sqlite3_mutex_leave(db->mutex);
181235#endif /* SQLITE_OMIT_WAL */
181236 return rc;
181237}
181238
181239/*
181240** Recover as many snapshots as possible from the wal file associated with
181241** schema zDb of database db.
181242*/
181243SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
181244 int rc = SQLITE_ERROR;
181245#ifndef SQLITE_OMIT_WAL
181246 int iDb;
181247
181248#ifdef SQLITE_ENABLE_API_ARMOR
181249 if( !sqlite3SafetyCheckOk(db) ){
181250 return SQLITE_MISUSE_BKPT;
181251 }
181252#endif
181253
181254 sqlite3_mutex_enter(db->mutex);
181255 iDb = sqlite3FindDbName(db, zDb);
181256 if( iDb==0 || iDb>1 ){
181257 Btree *pBt = db->aDb[iDb].pBt;
181258 if( SQLITE_TXN_NONE==sqlite3BtreeTxnState(pBt) ){
181259 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
181260 if( rc==SQLITE_OK ){
181261 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
181262 sqlite3BtreeCommit(pBt);
181263 }
181264 }
181265 }
181266 sqlite3_mutex_leave(db->mutex);
181267#endif /* SQLITE_OMIT_WAL */
181268 return rc;
181269}
181270
181271/*
181272** Free a snapshot handle obtained from sqlite3_snapshot_get().
181273*/
181274SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
181275 sqlite3_free(pSnapshot);
181276}
181277#endif /* SQLITE_ENABLE_SNAPSHOT */
181278
181279#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
181280/*
181281** Given the name of a compile-time option, return true if that option
181282** was used and false if not.
181283**
181284** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
181285** is not required for a match.
181286*/
181287SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
181288 int i, n;
181289 int nOpt;
181290 const char **azCompileOpt;
181291
181292#if SQLITE_ENABLE_API_ARMOR
181293 if( zOptName==0 ){
181294 (void)SQLITE_MISUSE_BKPT;
181295 return 0;
181296 }
181297#endif
181298
181299 azCompileOpt = sqlite3CompileOptions(pnOpt: &nOpt);
181300
181301 if( sqlite3StrNICmp(zLeft: zOptName, zRight: "SQLITE_", N: 7)==0 ) zOptName += 7;
181302 n = sqlite3Strlen30(z: zOptName);
181303
181304 /* Since nOpt is normally in single digits, a linear search is
181305 ** adequate. No need for a binary search. */
181306 for(i=0; i<nOpt; i++){
181307 if( sqlite3StrNICmp(zLeft: zOptName, zRight: azCompileOpt[i], N: n)==0
181308 && sqlite3IsIdChar(c: (unsigned char)azCompileOpt[i][n])==0
181309 ){
181310 return 1;
181311 }
181312 }
181313 return 0;
181314}
181315
181316/*
181317** Return the N-th compile-time option string. If N is out of range,
181318** return a NULL pointer.
181319*/
181320SQLITE_API const char *sqlite3_compileoption_get(int N){
181321 int nOpt;
181322 const char **azCompileOpt;
181323 azCompileOpt = sqlite3CompileOptions(pnOpt: &nOpt);
181324 if( N>=0 && N<nOpt ){
181325 return azCompileOpt[N];
181326 }
181327 return 0;
181328}
181329#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
181330
181331/************** End of main.c ************************************************/
181332/************** Begin file notify.c ******************************************/
181333/*
181334** 2009 March 3
181335**
181336** The author disclaims copyright to this source code. In place of
181337** a legal notice, here is a blessing:
181338**
181339** May you do good and not evil.
181340** May you find forgiveness for yourself and forgive others.
181341** May you share freely, never taking more than you give.
181342**
181343*************************************************************************
181344**
181345** This file contains the implementation of the sqlite3_unlock_notify()
181346** API method and its associated functionality.
181347*/
181348/* #include "sqliteInt.h" */
181349/* #include "btreeInt.h" */
181350
181351/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
181352#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
181353
181354/*
181355** Public interfaces:
181356**
181357** sqlite3ConnectionBlocked()
181358** sqlite3ConnectionUnlocked()
181359** sqlite3ConnectionClosed()
181360** sqlite3_unlock_notify()
181361*/
181362
181363#define assertMutexHeld() \
181364 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )
181365
181366/*
181367** Head of a linked list of all sqlite3 objects created by this process
181368** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
181369** is not NULL. This variable may only accessed while the STATIC_MAIN
181370** mutex is held.
181371*/
181372static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
181373
181374#ifndef NDEBUG
181375/*
181376** This function is a complex assert() that verifies the following
181377** properties of the blocked connections list:
181378**
181379** 1) Each entry in the list has a non-NULL value for either
181380** pUnlockConnection or pBlockingConnection, or both.
181381**
181382** 2) All entries in the list that share a common value for
181383** xUnlockNotify are grouped together.
181384**
181385** 3) If the argument db is not NULL, then none of the entries in the
181386** blocked connections list have pUnlockConnection or pBlockingConnection
181387** set to db. This is used when closing connection db.
181388*/
181389static void checkListProperties(sqlite3 *db){
181390 sqlite3 *p;
181391 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
181392 int seen = 0;
181393 sqlite3 *p2;
181394
181395 /* Verify property (1) */
181396 assert( p->pUnlockConnection || p->pBlockingConnection );
181397
181398 /* Verify property (2) */
181399 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
181400 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
181401 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
181402 assert( db==0 || p->pUnlockConnection!=db );
181403 assert( db==0 || p->pBlockingConnection!=db );
181404 }
181405 }
181406}
181407#else
181408# define checkListProperties(x)
181409#endif
181410
181411/*
181412** Remove connection db from the blocked connections list. If connection
181413** db is not currently a part of the list, this function is a no-op.
181414*/
181415static void removeFromBlockedList(sqlite3 *db){
181416 sqlite3 **pp;
181417 assertMutexHeld();
181418 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
181419 if( *pp==db ){
181420 *pp = (*pp)->pNextBlocked;
181421 break;
181422 }
181423 }
181424}
181425
181426/*
181427** Add connection db to the blocked connections list. It is assumed
181428** that it is not already a part of the list.
181429*/
181430static void addToBlockedList(sqlite3 *db){
181431 sqlite3 **pp;
181432 assertMutexHeld();
181433 for(
181434 pp=&sqlite3BlockedList;
181435 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
181436 pp=&(*pp)->pNextBlocked
181437 );
181438 db->pNextBlocked = *pp;
181439 *pp = db;
181440}
181441
181442/*
181443** Obtain the STATIC_MAIN mutex.
181444*/
181445static void enterMutex(void){
181446 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
181447 checkListProperties(0);
181448}
181449
181450/*
181451** Release the STATIC_MAIN mutex.
181452*/
181453static void leaveMutex(void){
181454 assertMutexHeld();
181455 checkListProperties(0);
181456 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
181457}
181458
181459/*
181460** Register an unlock-notify callback.
181461**
181462** This is called after connection "db" has attempted some operation
181463** but has received an SQLITE_LOCKED error because another connection
181464** (call it pOther) in the same process was busy using the same shared
181465** cache. pOther is found by looking at db->pBlockingConnection.
181466**
181467** If there is no blocking connection, the callback is invoked immediately,
181468** before this routine returns.
181469**
181470** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
181471** a deadlock.
181472**
181473** Otherwise, make arrangements to invoke xNotify when pOther drops
181474** its locks.
181475**
181476** Each call to this routine overrides any prior callbacks registered
181477** on the same "db". If xNotify==0 then any prior callbacks are immediately
181478** cancelled.
181479*/
181480SQLITE_API int sqlite3_unlock_notify(
181481 sqlite3 *db,
181482 void (*xNotify)(void **, int),
181483 void *pArg
181484){
181485 int rc = SQLITE_OK;
181486
181487 sqlite3_mutex_enter(db->mutex);
181488 enterMutex();
181489
181490 if( xNotify==0 ){
181491 removeFromBlockedList(db);
181492 db->pBlockingConnection = 0;
181493 db->pUnlockConnection = 0;
181494 db->xUnlockNotify = 0;
181495 db->pUnlockArg = 0;
181496 }else if( 0==db->pBlockingConnection ){
181497 /* The blocking transaction has been concluded. Or there never was a
181498 ** blocking transaction. In either case, invoke the notify callback
181499 ** immediately.
181500 */
181501 xNotify(&pArg, 1);
181502 }else{
181503 sqlite3 *p;
181504
181505 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
181506 if( p ){
181507 rc = SQLITE_LOCKED; /* Deadlock detected. */
181508 }else{
181509 db->pUnlockConnection = db->pBlockingConnection;
181510 db->xUnlockNotify = xNotify;
181511 db->pUnlockArg = pArg;
181512 removeFromBlockedList(db);
181513 addToBlockedList(db);
181514 }
181515 }
181516
181517 leaveMutex();
181518 assert( !db->mallocFailed );
181519 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
181520 sqlite3_mutex_leave(db->mutex);
181521 return rc;
181522}
181523
181524/*
181525** This function is called while stepping or preparing a statement
181526** associated with connection db. The operation will return SQLITE_LOCKED
181527** to the user because it requires a lock that will not be available
181528** until connection pBlocker concludes its current transaction.
181529*/
181530SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
181531 enterMutex();
181532 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
181533 addToBlockedList(db);
181534 }
181535 db->pBlockingConnection = pBlocker;
181536 leaveMutex();
181537}
181538
181539/*
181540** This function is called when
181541** the transaction opened by database db has just finished. Locks held
181542** by database connection db have been released.
181543**
181544** This function loops through each entry in the blocked connections
181545** list and does the following:
181546**
181547** 1) If the sqlite3.pBlockingConnection member of a list entry is
181548** set to db, then set pBlockingConnection=0.
181549**
181550** 2) If the sqlite3.pUnlockConnection member of a list entry is
181551** set to db, then invoke the configured unlock-notify callback and
181552** set pUnlockConnection=0.
181553**
181554** 3) If the two steps above mean that pBlockingConnection==0 and
181555** pUnlockConnection==0, remove the entry from the blocked connections
181556** list.
181557*/
181558SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
181559 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
181560 int nArg = 0; /* Number of entries in aArg[] */
181561 sqlite3 **pp; /* Iterator variable */
181562 void **aArg; /* Arguments to the unlock callback */
181563 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
181564 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
181565
181566 aArg = aStatic;
181567 enterMutex(); /* Enter STATIC_MAIN mutex */
181568
181569 /* This loop runs once for each entry in the blocked-connections list. */
181570 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
181571 sqlite3 *p = *pp;
181572
181573 /* Step 1. */
181574 if( p->pBlockingConnection==db ){
181575 p->pBlockingConnection = 0;
181576 }
181577
181578 /* Step 2. */
181579 if( p->pUnlockConnection==db ){
181580 assert( p->xUnlockNotify );
181581 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
181582 xUnlockNotify(aArg, nArg);
181583 nArg = 0;
181584 }
181585
181586 sqlite3BeginBenignMalloc();
181587 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
181588 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
181589 if( (!aDyn && nArg==(int)ArraySize(aStatic))
181590 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
181591 ){
181592 /* The aArg[] array needs to grow. */
181593 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
181594 if( pNew ){
181595 memcpy(pNew, aArg, nArg*sizeof(void *));
181596 sqlite3_free(aDyn);
181597 aDyn = aArg = pNew;
181598 }else{
181599 /* This occurs when the array of context pointers that need to
181600 ** be passed to the unlock-notify callback is larger than the
181601 ** aStatic[] array allocated on the stack and the attempt to
181602 ** allocate a larger array from the heap has failed.
181603 **
181604 ** This is a difficult situation to handle. Returning an error
181605 ** code to the caller is insufficient, as even if an error code
181606 ** is returned the transaction on connection db will still be
181607 ** closed and the unlock-notify callbacks on blocked connections
181608 ** will go unissued. This might cause the application to wait
181609 ** indefinitely for an unlock-notify callback that will never
181610 ** arrive.
181611 **
181612 ** Instead, invoke the unlock-notify callback with the context
181613 ** array already accumulated. We can then clear the array and
181614 ** begin accumulating any further context pointers without
181615 ** requiring any dynamic allocation. This is sub-optimal because
181616 ** it means that instead of one callback with a large array of
181617 ** context pointers the application will receive two or more
181618 ** callbacks with smaller arrays of context pointers, which will
181619 ** reduce the applications ability to prioritize multiple
181620 ** connections. But it is the best that can be done under the
181621 ** circumstances.
181622 */
181623 xUnlockNotify(aArg, nArg);
181624 nArg = 0;
181625 }
181626 }
181627 sqlite3EndBenignMalloc();
181628
181629 aArg[nArg++] = p->pUnlockArg;
181630 xUnlockNotify = p->xUnlockNotify;
181631 p->pUnlockConnection = 0;
181632 p->xUnlockNotify = 0;
181633 p->pUnlockArg = 0;
181634 }
181635
181636 /* Step 3. */
181637 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
181638 /* Remove connection p from the blocked connections list. */
181639 *pp = p->pNextBlocked;
181640 p->pNextBlocked = 0;
181641 }else{
181642 pp = &p->pNextBlocked;
181643 }
181644 }
181645
181646 if( nArg!=0 ){
181647 xUnlockNotify(aArg, nArg);
181648 }
181649 sqlite3_free(aDyn);
181650 leaveMutex(); /* Leave STATIC_MAIN mutex */
181651}
181652
181653/*
181654** This is called when the database connection passed as an argument is
181655** being closed. The connection is removed from the blocked list.
181656*/
181657SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
181658 sqlite3ConnectionUnlocked(db);
181659 enterMutex();
181660 removeFromBlockedList(db);
181661 checkListProperties(db);
181662 leaveMutex();
181663}
181664#endif
181665
181666/************** End of notify.c **********************************************/
181667/************** Begin file fts3.c ********************************************/
181668/*
181669** 2006 Oct 10
181670**
181671** The author disclaims copyright to this source code. In place of
181672** a legal notice, here is a blessing:
181673**
181674** May you do good and not evil.
181675** May you find forgiveness for yourself and forgive others.
181676** May you share freely, never taking more than you give.
181677**
181678******************************************************************************
181679**
181680** This is an SQLite module implementing full-text search.
181681*/
181682
181683/*
181684** The code in this file is only compiled if:
181685**
181686** * The FTS3 module is being built as an extension
181687** (in which case SQLITE_CORE is not defined), or
181688**
181689** * The FTS3 module is being built into the core of
181690** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
181691*/
181692
181693/* The full-text index is stored in a series of b+tree (-like)
181694** structures called segments which map terms to doclists. The
181695** structures are like b+trees in layout, but are constructed from the
181696** bottom up in optimal fashion and are not updatable. Since trees
181697** are built from the bottom up, things will be described from the
181698** bottom up.
181699**
181700**
181701**** Varints ****
181702** The basic unit of encoding is a variable-length integer called a
181703** varint. We encode variable-length integers in little-endian order
181704** using seven bits * per byte as follows:
181705**
181706** KEY:
181707** A = 0xxxxxxx 7 bits of data and one flag bit
181708** B = 1xxxxxxx 7 bits of data and one flag bit
181709**
181710** 7 bits - A
181711** 14 bits - BA
181712** 21 bits - BBA
181713** and so on.
181714**
181715** This is similar in concept to how sqlite encodes "varints" but
181716** the encoding is not the same. SQLite varints are big-endian
181717** are are limited to 9 bytes in length whereas FTS3 varints are
181718** little-endian and can be up to 10 bytes in length (in theory).
181719**
181720** Example encodings:
181721**
181722** 1: 0x01
181723** 127: 0x7f
181724** 128: 0x81 0x00
181725**
181726**
181727**** Document lists ****
181728** A doclist (document list) holds a docid-sorted list of hits for a
181729** given term. Doclists hold docids and associated token positions.
181730** A docid is the unique integer identifier for a single document.
181731** A position is the index of a word within the document. The first
181732** word of the document has a position of 0.
181733**
181734** FTS3 used to optionally store character offsets using a compile-time
181735** option. But that functionality is no longer supported.
181736**
181737** A doclist is stored like this:
181738**
181739** array {
181740** varint docid; (delta from previous doclist)
181741** array { (position list for column 0)
181742** varint position; (2 more than the delta from previous position)
181743** }
181744** array {
181745** varint POS_COLUMN; (marks start of position list for new column)
181746** varint column; (index of new column)
181747** array {
181748** varint position; (2 more than the delta from previous position)
181749** }
181750** }
181751** varint POS_END; (marks end of positions for this document.
181752** }
181753**
181754** Here, array { X } means zero or more occurrences of X, adjacent in
181755** memory. A "position" is an index of a token in the token stream
181756** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
181757** in the same logical place as the position element, and act as sentinals
181758** ending a position list array. POS_END is 0. POS_COLUMN is 1.
181759** The positions numbers are not stored literally but rather as two more
181760** than the difference from the prior position, or the just the position plus
181761** 2 for the first position. Example:
181762**
181763** label: A B C D E F G H I J K
181764** value: 123 5 9 1 1 14 35 0 234 72 0
181765**
181766** The 123 value is the first docid. For column zero in this document
181767** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
181768** at D signals the start of a new column; the 1 at E indicates that the
181769** new column is column number 1. There are two positions at 12 and 45
181770** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
181771** 234 at I is the delta to next docid (357). It has one position 70
181772** (72-2) and then terminates with the 0 at K.
181773**
181774** A "position-list" is the list of positions for multiple columns for
181775** a single docid. A "column-list" is the set of positions for a single
181776** column. Hence, a position-list consists of one or more column-lists,
181777** a document record consists of a docid followed by a position-list and
181778** a doclist consists of one or more document records.
181779**
181780** A bare doclist omits the position information, becoming an
181781** array of varint-encoded docids.
181782**
181783**** Segment leaf nodes ****
181784** Segment leaf nodes store terms and doclists, ordered by term. Leaf
181785** nodes are written using LeafWriter, and read using LeafReader (to
181786** iterate through a single leaf node's data) and LeavesReader (to
181787** iterate through a segment's entire leaf layer). Leaf nodes have
181788** the format:
181789**
181790** varint iHeight; (height from leaf level, always 0)
181791** varint nTerm; (length of first term)
181792** char pTerm[nTerm]; (content of first term)
181793** varint nDoclist; (length of term's associated doclist)
181794** char pDoclist[nDoclist]; (content of doclist)
181795** array {
181796** (further terms are delta-encoded)
181797** varint nPrefix; (length of prefix shared with previous term)
181798** varint nSuffix; (length of unshared suffix)
181799** char pTermSuffix[nSuffix];(unshared suffix of next term)
181800** varint nDoclist; (length of term's associated doclist)
181801** char pDoclist[nDoclist]; (content of doclist)
181802** }
181803**
181804** Here, array { X } means zero or more occurrences of X, adjacent in
181805** memory.
181806**
181807** Leaf nodes are broken into blocks which are stored contiguously in
181808** the %_segments table in sorted order. This means that when the end
181809** of a node is reached, the next term is in the node with the next
181810** greater node id.
181811**
181812** New data is spilled to a new leaf node when the current node
181813** exceeds LEAF_MAX bytes (default 2048). New data which itself is
181814** larger than STANDALONE_MIN (default 1024) is placed in a standalone
181815** node (a leaf node with a single term and doclist). The goal of
181816** these settings is to pack together groups of small doclists while
181817** making it efficient to directly access large doclists. The
181818** assumption is that large doclists represent terms which are more
181819** likely to be query targets.
181820**
181821** TODO(shess) It may be useful for blocking decisions to be more
181822** dynamic. For instance, it may make more sense to have a 2.5k leaf
181823** node rather than splitting into 2k and .5k nodes. My intuition is
181824** that this might extend through 2x or 4x the pagesize.
181825**
181826**
181827**** Segment interior nodes ****
181828** Segment interior nodes store blockids for subtree nodes and terms
181829** to describe what data is stored by the each subtree. Interior
181830** nodes are written using InteriorWriter, and read using
181831** InteriorReader. InteriorWriters are created as needed when
181832** SegmentWriter creates new leaf nodes, or when an interior node
181833** itself grows too big and must be split. The format of interior
181834** nodes:
181835**
181836** varint iHeight; (height from leaf level, always >0)
181837** varint iBlockid; (block id of node's leftmost subtree)
181838** optional {
181839** varint nTerm; (length of first term)
181840** char pTerm[nTerm]; (content of first term)
181841** array {
181842** (further terms are delta-encoded)
181843** varint nPrefix; (length of shared prefix with previous term)
181844** varint nSuffix; (length of unshared suffix)
181845** char pTermSuffix[nSuffix]; (unshared suffix of next term)
181846** }
181847** }
181848**
181849** Here, optional { X } means an optional element, while array { X }
181850** means zero or more occurrences of X, adjacent in memory.
181851**
181852** An interior node encodes n terms separating n+1 subtrees. The
181853** subtree blocks are contiguous, so only the first subtree's blockid
181854** is encoded. The subtree at iBlockid will contain all terms less
181855** than the first term encoded (or all terms if no term is encoded).
181856** Otherwise, for terms greater than or equal to pTerm[i] but less
181857** than pTerm[i+1], the subtree for that term will be rooted at
181858** iBlockid+i. Interior nodes only store enough term data to
181859** distinguish adjacent children (if the rightmost term of the left
181860** child is "something", and the leftmost term of the right child is
181861** "wicked", only "w" is stored).
181862**
181863** New data is spilled to a new interior node at the same height when
181864** the current node exceeds INTERIOR_MAX bytes (default 2048).
181865** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
181866** interior nodes and making the tree too skinny. The interior nodes
181867** at a given height are naturally tracked by interior nodes at
181868** height+1, and so on.
181869**
181870**
181871**** Segment directory ****
181872** The segment directory in table %_segdir stores meta-information for
181873** merging and deleting segments, and also the root node of the
181874** segment's tree.
181875**
181876** The root node is the top node of the segment's tree after encoding
181877** the entire segment, restricted to ROOT_MAX bytes (default 1024).
181878** This could be either a leaf node or an interior node. If the top
181879** node requires more than ROOT_MAX bytes, it is flushed to %_segments
181880** and a new root interior node is generated (which should always fit
181881** within ROOT_MAX because it only needs space for 2 varints, the
181882** height and the blockid of the previous root).
181883**
181884** The meta-information in the segment directory is:
181885** level - segment level (see below)
181886** idx - index within level
181887** - (level,idx uniquely identify a segment)
181888** start_block - first leaf node
181889** leaves_end_block - last leaf node
181890** end_block - last block (including interior nodes)
181891** root - contents of root node
181892**
181893** If the root node is a leaf node, then start_block,
181894** leaves_end_block, and end_block are all 0.
181895**
181896**
181897**** Segment merging ****
181898** To amortize update costs, segments are grouped into levels and
181899** merged in batches. Each increase in level represents exponentially
181900** more documents.
181901**
181902** New documents (actually, document updates) are tokenized and
181903** written individually (using LeafWriter) to a level 0 segment, with
181904** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
181905** level 0 segments are merged into a single level 1 segment. Level 1
181906** is populated like level 0, and eventually MERGE_COUNT level 1
181907** segments are merged to a single level 2 segment (representing
181908** MERGE_COUNT^2 updates), and so on.
181909**
181910** A segment merge traverses all segments at a given level in
181911** parallel, performing a straightforward sorted merge. Since segment
181912** leaf nodes are written in to the %_segments table in order, this
181913** merge traverses the underlying sqlite disk structures efficiently.
181914** After the merge, all segment blocks from the merged level are
181915** deleted.
181916**
181917** MERGE_COUNT controls how often we merge segments. 16 seems to be
181918** somewhat of a sweet spot for insertion performance. 32 and 64 show
181919** very similar performance numbers to 16 on insertion, though they're
181920** a tiny bit slower (perhaps due to more overhead in merge-time
181921** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
181922** 16, 2 about 66% slower than 16.
181923**
181924** At query time, high MERGE_COUNT increases the number of segments
181925** which need to be scanned and merged. For instance, with 100k docs
181926** inserted:
181927**
181928** MERGE_COUNT segments
181929** 16 25
181930** 8 12
181931** 4 10
181932** 2 6
181933**
181934** This appears to have only a moderate impact on queries for very
181935** frequent terms (which are somewhat dominated by segment merge
181936** costs), and infrequent and non-existent terms still seem to be fast
181937** even with many segments.
181938**
181939** TODO(shess) That said, it would be nice to have a better query-side
181940** argument for MERGE_COUNT of 16. Also, it is possible/likely that
181941** optimizations to things like doclist merging will swing the sweet
181942** spot around.
181943**
181944**
181945**
181946**** Handling of deletions and updates ****
181947** Since we're using a segmented structure, with no docid-oriented
181948** index into the term index, we clearly cannot simply update the term
181949** index when a document is deleted or updated. For deletions, we
181950** write an empty doclist (varint(docid) varint(POS_END)), for updates
181951** we simply write the new doclist. Segment merges overwrite older
181952** data for a particular docid with newer data, so deletes or updates
181953** will eventually overtake the earlier data and knock it out. The
181954** query logic likewise merges doclists so that newer data knocks out
181955** older data.
181956*/
181957
181958/************** Include fts3Int.h in the middle of fts3.c ********************/
181959/************** Begin file fts3Int.h *****************************************/
181960/*
181961** 2009 Nov 12
181962**
181963** The author disclaims copyright to this source code. In place of
181964** a legal notice, here is a blessing:
181965**
181966** May you do good and not evil.
181967** May you find forgiveness for yourself and forgive others.
181968** May you share freely, never taking more than you give.
181969**
181970******************************************************************************
181971**
181972*/
181973#ifndef _FTSINT_H
181974#define _FTSINT_H
181975
181976#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
181977# define NDEBUG 1
181978#endif
181979
181980/* FTS3/FTS4 require virtual tables */
181981#ifdef SQLITE_OMIT_VIRTUALTABLE
181982# undef SQLITE_ENABLE_FTS3
181983# undef SQLITE_ENABLE_FTS4
181984#endif
181985
181986/*
181987** FTS4 is really an extension for FTS3. It is enabled using the
181988** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
181989** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
181990*/
181991#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
181992# define SQLITE_ENABLE_FTS3
181993#endif
181994
181995#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
181996
181997/* If not building as part of the core, include sqlite3ext.h. */
181998#ifndef SQLITE_CORE
181999/* # include "sqlite3ext.h" */
182000SQLITE_EXTENSION_INIT3
182001#endif
182002
182003/* #include "sqlite3.h" */
182004/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
182005/************** Begin file fts3_tokenizer.h **********************************/
182006/*
182007** 2006 July 10
182008**
182009** The author disclaims copyright to this source code.
182010**
182011*************************************************************************
182012** Defines the interface to tokenizers used by fulltext-search. There
182013** are three basic components:
182014**
182015** sqlite3_tokenizer_module is a singleton defining the tokenizer
182016** interface functions. This is essentially the class structure for
182017** tokenizers.
182018**
182019** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
182020** including customization information defined at creation time.
182021**
182022** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
182023** tokens from a particular input.
182024*/
182025#ifndef _FTS3_TOKENIZER_H_
182026#define _FTS3_TOKENIZER_H_
182027
182028/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
182029** If tokenizers are to be allowed to call sqlite3_*() functions, then
182030** we will need a way to register the API consistently.
182031*/
182032/* #include "sqlite3.h" */
182033
182034/*
182035** Structures used by the tokenizer interface. When a new tokenizer
182036** implementation is registered, the caller provides a pointer to
182037** an sqlite3_tokenizer_module containing pointers to the callback
182038** functions that make up an implementation.
182039**
182040** When an fts3 table is created, it passes any arguments passed to
182041** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
182042** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
182043** implementation. The xCreate() function in turn returns an
182044** sqlite3_tokenizer structure representing the specific tokenizer to
182045** be used for the fts3 table (customized by the tokenizer clause arguments).
182046**
182047** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
182048** method is called. It returns an sqlite3_tokenizer_cursor object
182049** that may be used to tokenize a specific input buffer based on
182050** the tokenization rules supplied by a specific sqlite3_tokenizer
182051** object.
182052*/
182053typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
182054typedef struct sqlite3_tokenizer sqlite3_tokenizer;
182055typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
182056
182057struct sqlite3_tokenizer_module {
182058
182059 /*
182060 ** Structure version. Should always be set to 0 or 1.
182061 */
182062 int iVersion;
182063
182064 /*
182065 ** Create a new tokenizer. The values in the argv[] array are the
182066 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
182067 ** TABLE statement that created the fts3 table. For example, if
182068 ** the following SQL is executed:
182069 **
182070 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
182071 **
182072 ** then argc is set to 2, and the argv[] array contains pointers
182073 ** to the strings "arg1" and "arg2".
182074 **
182075 ** This method should return either SQLITE_OK (0), or an SQLite error
182076 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
182077 ** to point at the newly created tokenizer structure. The generic
182078 ** sqlite3_tokenizer.pModule variable should not be initialized by
182079 ** this callback. The caller will do so.
182080 */
182081 int (*xCreate)(
182082 int argc, /* Size of argv array */
182083 const char *const*argv, /* Tokenizer argument strings */
182084 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
182085 );
182086
182087 /*
182088 ** Destroy an existing tokenizer. The fts3 module calls this method
182089 ** exactly once for each successful call to xCreate().
182090 */
182091 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
182092
182093 /*
182094 ** Create a tokenizer cursor to tokenize an input buffer. The caller
182095 ** is responsible for ensuring that the input buffer remains valid
182096 ** until the cursor is closed (using the xClose() method).
182097 */
182098 int (*xOpen)(
182099 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
182100 const char *pInput, int nBytes, /* Input buffer */
182101 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
182102 );
182103
182104 /*
182105 ** Destroy an existing tokenizer cursor. The fts3 module calls this
182106 ** method exactly once for each successful call to xOpen().
182107 */
182108 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
182109
182110 /*
182111 ** Retrieve the next token from the tokenizer cursor pCursor. This
182112 ** method should either return SQLITE_OK and set the values of the
182113 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
182114 ** the end of the buffer has been reached, or an SQLite error code.
182115 **
182116 ** *ppToken should be set to point at a buffer containing the
182117 ** normalized version of the token (i.e. after any case-folding and/or
182118 ** stemming has been performed). *pnBytes should be set to the length
182119 ** of this buffer in bytes. The input text that generated the token is
182120 ** identified by the byte offsets returned in *piStartOffset and
182121 ** *piEndOffset. *piStartOffset should be set to the index of the first
182122 ** byte of the token in the input buffer. *piEndOffset should be set
182123 ** to the index of the first byte just past the end of the token in
182124 ** the input buffer.
182125 **
182126 ** The buffer *ppToken is set to point at is managed by the tokenizer
182127 ** implementation. It is only required to be valid until the next call
182128 ** to xNext() or xClose().
182129 */
182130 /* TODO(shess) current implementation requires pInput to be
182131 ** nul-terminated. This should either be fixed, or pInput/nBytes
182132 ** should be converted to zInput.
182133 */
182134 int (*xNext)(
182135 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
182136 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
182137 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
182138 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
182139 int *piPosition /* OUT: Number of tokens returned before this one */
182140 );
182141
182142 /***********************************************************************
182143 ** Methods below this point are only available if iVersion>=1.
182144 */
182145
182146 /*
182147 ** Configure the language id of a tokenizer cursor.
182148 */
182149 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
182150};
182151
182152struct sqlite3_tokenizer {
182153 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
182154 /* Tokenizer implementations will typically add additional fields */
182155};
182156
182157struct sqlite3_tokenizer_cursor {
182158 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
182159 /* Tokenizer implementations will typically add additional fields */
182160};
182161
182162int fts3_global_term_cnt(int iTerm, int iCol);
182163int fts3_term_cnt(int iTerm, int iCol);
182164
182165
182166#endif /* _FTS3_TOKENIZER_H_ */
182167
182168/************** End of fts3_tokenizer.h **************************************/
182169/************** Continuing where we left off in fts3Int.h ********************/
182170/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
182171/************** Begin file fts3_hash.h ***************************************/
182172/*
182173** 2001 September 22
182174**
182175** The author disclaims copyright to this source code. In place of
182176** a legal notice, here is a blessing:
182177**
182178** May you do good and not evil.
182179** May you find forgiveness for yourself and forgive others.
182180** May you share freely, never taking more than you give.
182181**
182182*************************************************************************
182183** This is the header file for the generic hash-table implementation
182184** used in SQLite. We've modified it slightly to serve as a standalone
182185** hash table implementation for the full-text indexing module.
182186**
182187*/
182188#ifndef _FTS3_HASH_H_
182189#define _FTS3_HASH_H_
182190
182191/* Forward declarations of structures. */
182192typedef struct Fts3Hash Fts3Hash;
182193typedef struct Fts3HashElem Fts3HashElem;
182194
182195/* A complete hash table is an instance of the following structure.
182196** The internals of this structure are intended to be opaque -- client
182197** code should not attempt to access or modify the fields of this structure
182198** directly. Change this structure only by using the routines below.
182199** However, many of the "procedures" and "functions" for modifying and
182200** accessing this structure are really macros, so we can't really make
182201** this structure opaque.
182202*/
182203struct Fts3Hash {
182204 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
182205 char copyKey; /* True if copy of key made on insert */
182206 int count; /* Number of entries in this table */
182207 Fts3HashElem *first; /* The first element of the array */
182208 int htsize; /* Number of buckets in the hash table */
182209 struct _fts3ht { /* the hash table */
182210 int count; /* Number of entries with this hash */
182211 Fts3HashElem *chain; /* Pointer to first entry with this hash */
182212 } *ht;
182213};
182214
182215/* Each element in the hash table is an instance of the following
182216** structure. All elements are stored on a single doubly-linked list.
182217**
182218** Again, this structure is intended to be opaque, but it can't really
182219** be opaque because it is used by macros.
182220*/
182221struct Fts3HashElem {
182222 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
182223 void *data; /* Data associated with this element */
182224 void *pKey; int nKey; /* Key associated with this element */
182225};
182226
182227/*
182228** There are 2 different modes of operation for a hash table:
182229**
182230** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
182231** (including the null-terminator, if any). Case
182232** is respected in comparisons.
182233**
182234** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
182235** memcmp() is used to compare keys.
182236**
182237** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
182238*/
182239#define FTS3_HASH_STRING 1
182240#define FTS3_HASH_BINARY 2
182241
182242/*
182243** Access routines. To delete, insert a NULL pointer.
182244*/
182245SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
182246SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
182247SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
182248SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
182249SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
182250
182251/*
182252** Shorthand for the functions above
182253*/
182254#define fts3HashInit sqlite3Fts3HashInit
182255#define fts3HashInsert sqlite3Fts3HashInsert
182256#define fts3HashFind sqlite3Fts3HashFind
182257#define fts3HashClear sqlite3Fts3HashClear
182258#define fts3HashFindElem sqlite3Fts3HashFindElem
182259
182260/*
182261** Macros for looping over all elements of a hash table. The idiom is
182262** like this:
182263**
182264** Fts3Hash h;
182265** Fts3HashElem *p;
182266** ...
182267** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
182268** SomeStructure *pData = fts3HashData(p);
182269** // do something with pData
182270** }
182271*/
182272#define fts3HashFirst(H) ((H)->first)
182273#define fts3HashNext(E) ((E)->next)
182274#define fts3HashData(E) ((E)->data)
182275#define fts3HashKey(E) ((E)->pKey)
182276#define fts3HashKeysize(E) ((E)->nKey)
182277
182278/*
182279** Number of entries in a hash table
182280*/
182281#define fts3HashCount(H) ((H)->count)
182282
182283#endif /* _FTS3_HASH_H_ */
182284
182285/************** End of fts3_hash.h *******************************************/
182286/************** Continuing where we left off in fts3Int.h ********************/
182287
182288/*
182289** This constant determines the maximum depth of an FTS expression tree
182290** that the library will create and use. FTS uses recursion to perform
182291** various operations on the query tree, so the disadvantage of a large
182292** limit is that it may allow very large queries to use large amounts
182293** of stack space (perhaps causing a stack overflow).
182294*/
182295#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
182296# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
182297#endif
182298
182299
182300/*
182301** This constant controls how often segments are merged. Once there are
182302** FTS3_MERGE_COUNT segments of level N, they are merged into a single
182303** segment of level N+1.
182304*/
182305#define FTS3_MERGE_COUNT 16
182306
182307/*
182308** This is the maximum amount of data (in bytes) to store in the
182309** Fts3Table.pendingTerms hash table. Normally, the hash table is
182310** populated as documents are inserted/updated/deleted in a transaction
182311** and used to create a new segment when the transaction is committed.
182312** However if this limit is reached midway through a transaction, a new
182313** segment is created and the hash table cleared immediately.
182314*/
182315#define FTS3_MAX_PENDING_DATA (1*1024*1024)
182316
182317/*
182318** Macro to return the number of elements in an array. SQLite has a
182319** similar macro called ArraySize(). Use a different name to avoid
182320** a collision when building an amalgamation with built-in FTS3.
182321*/
182322#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
182323
182324
182325#ifndef MIN
182326# define MIN(x,y) ((x)<(y)?(x):(y))
182327#endif
182328#ifndef MAX
182329# define MAX(x,y) ((x)>(y)?(x):(y))
182330#endif
182331
182332/*
182333** Maximum length of a varint encoded integer. The varint format is different
182334** from that used by SQLite, so the maximum length is 10, not 9.
182335*/
182336#define FTS3_VARINT_MAX 10
182337
182338#define FTS3_BUFFER_PADDING 8
182339
182340/*
182341** FTS4 virtual tables may maintain multiple indexes - one index of all terms
182342** in the document set and zero or more prefix indexes. All indexes are stored
182343** as one or more b+-trees in the %_segments and %_segdir tables.
182344**
182345** It is possible to determine which index a b+-tree belongs to based on the
182346** value stored in the "%_segdir.level" column. Given this value L, the index
182347** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
182348** level values between 0 and 1023 (inclusive) belong to index 0, all levels
182349** between 1024 and 2047 to index 1, and so on.
182350**
182351** It is considered impossible for an index to use more than 1024 levels. In
182352** theory though this may happen, but only after at least
182353** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
182354*/
182355#define FTS3_SEGDIR_MAXLEVEL 1024
182356#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
182357
182358/*
182359** The testcase() macro is only used by the amalgamation. If undefined,
182360** make it a no-op.
182361*/
182362#ifndef testcase
182363# define testcase(X)
182364#endif
182365
182366/*
182367** Terminator values for position-lists and column-lists.
182368*/
182369#define POS_COLUMN (1) /* Column-list terminator */
182370#define POS_END (0) /* Position-list terminator */
182371
182372/*
182373** The assert_fts3_nc() macro is similar to the assert() macro, except that it
182374** is used for assert() conditions that are true only if it can be
182375** guranteed that the database is not corrupt.
182376*/
182377#ifdef SQLITE_DEBUG
182378SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
182379# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))
182380#else
182381# define assert_fts3_nc(x) assert(x)
182382#endif
182383
182384/*
182385** This section provides definitions to allow the
182386** FTS3 extension to be compiled outside of the
182387** amalgamation.
182388*/
182389#ifndef SQLITE_AMALGAMATION
182390/*
182391** Macros indicating that conditional expressions are always true or
182392** false.
182393*/
182394#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
182395# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
182396#endif
182397#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
182398# define ALWAYS(X) (1)
182399# define NEVER(X) (0)
182400#elif !defined(NDEBUG)
182401# define ALWAYS(X) ((X)?1:(assert(0),0))
182402# define NEVER(X) ((X)?(assert(0),1):0)
182403#else
182404# define ALWAYS(X) (X)
182405# define NEVER(X) (X)
182406#endif
182407
182408/*
182409** Internal types used by SQLite.
182410*/
182411typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
182412typedef short int i16; /* 2-byte (or larger) signed integer */
182413typedef unsigned int u32; /* 4-byte unsigned integer */
182414typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
182415typedef sqlite3_int64 i64; /* 8-byte signed integer */
182416
182417/*
182418** Macro used to suppress compiler warnings for unused parameters.
182419*/
182420#define UNUSED_PARAMETER(x) (void)(x)
182421
182422/*
182423** Activate assert() only if SQLITE_TEST is enabled.
182424*/
182425#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
182426# define NDEBUG 1
182427#endif
182428
182429/*
182430** The TESTONLY macro is used to enclose variable declarations or
182431** other bits of code that are needed to support the arguments
182432** within testcase() and assert() macros.
182433*/
182434#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
182435# define TESTONLY(X) X
182436#else
182437# define TESTONLY(X)
182438#endif
182439
182440#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
182441#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
182442
182443#define deliberate_fall_through
182444
182445#endif /* SQLITE_AMALGAMATION */
182446
182447#ifdef SQLITE_DEBUG
182448SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
182449# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
182450#else
182451# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
182452#endif
182453
182454typedef struct Fts3Table Fts3Table;
182455typedef struct Fts3Cursor Fts3Cursor;
182456typedef struct Fts3Expr Fts3Expr;
182457typedef struct Fts3Phrase Fts3Phrase;
182458typedef struct Fts3PhraseToken Fts3PhraseToken;
182459
182460typedef struct Fts3Doclist Fts3Doclist;
182461typedef struct Fts3SegFilter Fts3SegFilter;
182462typedef struct Fts3DeferredToken Fts3DeferredToken;
182463typedef struct Fts3SegReader Fts3SegReader;
182464typedef struct Fts3MultiSegReader Fts3MultiSegReader;
182465
182466typedef struct MatchinfoBuffer MatchinfoBuffer;
182467
182468/*
182469** A connection to a fulltext index is an instance of the following
182470** structure. The xCreate and xConnect methods create an instance
182471** of this structure and xDestroy and xDisconnect free that instance.
182472** All other methods receive a pointer to the structure as one of their
182473** arguments.
182474*/
182475struct Fts3Table {
182476 sqlite3_vtab base; /* Base class used by SQLite core */
182477 sqlite3 *db; /* The database connection */
182478 const char *zDb; /* logical database name */
182479 const char *zName; /* virtual table name */
182480 int nColumn; /* number of named columns in virtual table */
182481 char **azColumn; /* column names. malloced */
182482 u8 *abNotindexed; /* True for 'notindexed' columns */
182483 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
182484 char *zContentTbl; /* content=xxx option, or NULL */
182485 char *zLanguageid; /* languageid=xxx option, or NULL */
182486 int nAutoincrmerge; /* Value configured by 'automerge' */
182487 u32 nLeafAdd; /* Number of leaf blocks added this trans */
182488 int bLock; /* Used to prevent recursive content= tbls */
182489
182490 /* Precompiled statements used by the implementation. Each of these
182491 ** statements is run and reset within a single virtual table API call.
182492 */
182493 sqlite3_stmt *aStmt[40];
182494 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
182495
182496 char *zReadExprlist;
182497 char *zWriteExprlist;
182498
182499 int nNodeSize; /* Soft limit for node size */
182500 u8 bFts4; /* True for FTS4, false for FTS3 */
182501 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
182502 u8 bHasDocsize; /* True if %_docsize table exists */
182503 u8 bDescIdx; /* True if doclists are in reverse order */
182504 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
182505 int nPgsz; /* Page size for host database */
182506 char *zSegmentsTbl; /* Name of %_segments table */
182507 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
182508
182509 /*
182510 ** The following array of hash tables is used to buffer pending index
182511 ** updates during transactions. All pending updates buffered at any one
182512 ** time must share a common language-id (see the FTS4 langid= feature).
182513 ** The current language id is stored in variable iPrevLangid.
182514 **
182515 ** A single FTS4 table may have multiple full-text indexes. For each index
182516 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
182517 ** terms that appear in the document set. Each subsequent index in aIndex[]
182518 ** is an index of prefixes of a specific length.
182519 **
182520 ** Variable nPendingData contains an estimate the memory consumed by the
182521 ** pending data structures, including hash table overhead, but not including
182522 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
182523 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
182524 ** recently inserted record.
182525 */
182526 int nIndex; /* Size of aIndex[] */
182527 struct Fts3Index {
182528 int nPrefix; /* Prefix length (0 for main terms index) */
182529 Fts3Hash hPending; /* Pending terms table for this index */
182530 } *aIndex;
182531 int nMaxPendingData; /* Max pending data before flush to disk */
182532 int nPendingData; /* Current bytes of pending data */
182533 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
182534 int iPrevLangid; /* Langid of recently inserted document */
182535 int bPrevDelete; /* True if last operation was a delete */
182536
182537#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
182538 /* State variables used for validating that the transaction control
182539 ** methods of the virtual table are called at appropriate times. These
182540 ** values do not contribute to FTS functionality; they are used for
182541 ** verifying the operation of the SQLite core.
182542 */
182543 int inTransaction; /* True after xBegin but before xCommit/xRollback */
182544 int mxSavepoint; /* Largest valid xSavepoint integer */
182545#endif
182546
182547#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
182548 /* True to disable the incremental doclist optimization. This is controled
182549 ** by special insert command 'test-no-incr-doclist'. */
182550 int bNoIncrDoclist;
182551
182552 /* Number of segments in a level */
182553 int nMergeCount;
182554#endif
182555};
182556
182557/* Macro to find the number of segments to merge */
182558#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
182559# define MergeCount(P) ((P)->nMergeCount)
182560#else
182561# define MergeCount(P) FTS3_MERGE_COUNT
182562#endif
182563
182564/*
182565** When the core wants to read from the virtual table, it creates a
182566** virtual table cursor (an instance of the following structure) using
182567** the xOpen method. Cursors are destroyed using the xClose method.
182568*/
182569struct Fts3Cursor {
182570 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
182571 i16 eSearch; /* Search strategy (see below) */
182572 u8 isEof; /* True if at End Of Results */
182573 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
182574 u8 bSeekStmt; /* True if pStmt is a seek */
182575 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
182576 Fts3Expr *pExpr; /* Parsed MATCH query string */
182577 int iLangid; /* Language being queried for */
182578 int nPhrase; /* Number of matchable phrases in query */
182579 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
182580 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
182581 char *pNextId; /* Pointer into the body of aDoclist */
182582 char *aDoclist; /* List of docids for full-text queries */
182583 int nDoclist; /* Size of buffer at aDoclist */
182584 u8 bDesc; /* True to sort in descending order */
182585 int eEvalmode; /* An FTS3_EVAL_XX constant */
182586 int nRowAvg; /* Average size of database rows, in pages */
182587 sqlite3_int64 nDoc; /* Documents in table */
182588 i64 iMinDocid; /* Minimum docid to return */
182589 i64 iMaxDocid; /* Maximum docid to return */
182590 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
182591 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
182592};
182593
182594#define FTS3_EVAL_FILTER 0
182595#define FTS3_EVAL_NEXT 1
182596#define FTS3_EVAL_MATCHINFO 2
182597
182598/*
182599** The Fts3Cursor.eSearch member is always set to one of the following.
182600** Actualy, Fts3Cursor.eSearch can be greater than or equal to
182601** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
182602** of the column to be searched. For example, in
182603**
182604** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
182605** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
182606**
182607** Because the LHS of the MATCH operator is 2nd column "b",
182608** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
182609** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
182610** indicating that all columns should be searched,
182611** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
182612*/
182613#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
182614#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
182615#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
182616
182617/*
182618** The lower 16-bits of the sqlite3_index_info.idxNum value set by
182619** the xBestIndex() method contains the Fts3Cursor.eSearch value described
182620** above. The upper 16-bits contain a combination of the following
182621** bits, used to describe extra constraints on full-text searches.
182622*/
182623#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
182624#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
182625#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
182626
182627struct Fts3Doclist {
182628 char *aAll; /* Array containing doclist (or NULL) */
182629 int nAll; /* Size of a[] in bytes */
182630 char *pNextDocid; /* Pointer to next docid */
182631
182632 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
182633 int bFreeList; /* True if pList should be sqlite3_free()d */
182634 char *pList; /* Pointer to position list following iDocid */
182635 int nList; /* Length of position list */
182636};
182637
182638/*
182639** A "phrase" is a sequence of one or more tokens that must match in
182640** sequence. A single token is the base case and the most common case.
182641** For a sequence of tokens contained in double-quotes (i.e. "one two three")
182642** nToken will be the number of tokens in the string.
182643*/
182644struct Fts3PhraseToken {
182645 char *z; /* Text of the token */
182646 int n; /* Number of bytes in buffer z */
182647 int isPrefix; /* True if token ends with a "*" character */
182648 int bFirst; /* True if token must appear at position 0 */
182649
182650 /* Variables above this point are populated when the expression is
182651 ** parsed (by code in fts3_expr.c). Below this point the variables are
182652 ** used when evaluating the expression. */
182653 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
182654 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
182655};
182656
182657struct Fts3Phrase {
182658 /* Cache of doclist for this phrase. */
182659 Fts3Doclist doclist;
182660 int bIncr; /* True if doclist is loaded incrementally */
182661 int iDoclistToken;
182662
182663 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
182664 ** OR condition. */
182665 char *pOrPoslist;
182666 i64 iOrDocid;
182667
182668 /* Variables below this point are populated by fts3_expr.c when parsing
182669 ** a MATCH expression. Everything above is part of the evaluation phase.
182670 */
182671 int nToken; /* Number of tokens in the phrase */
182672 int iColumn; /* Index of column this phrase must match */
182673 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
182674};
182675
182676/*
182677** A tree of these objects forms the RHS of a MATCH operator.
182678**
182679** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
182680** points to a malloced buffer, size nDoclist bytes, containing the results
182681** of this phrase query in FTS3 doclist format. As usual, the initial
182682** "Length" field found in doclists stored on disk is omitted from this
182683** buffer.
182684**
182685** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
182686** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
182687** where nCol is the number of columns in the queried FTS table. The array
182688** is populated as follows:
182689**
182690** aMI[iCol*3 + 0] = Undefined
182691** aMI[iCol*3 + 1] = Number of occurrences
182692** aMI[iCol*3 + 2] = Number of rows containing at least one instance
182693**
182694** The aMI array is allocated using sqlite3_malloc(). It should be freed
182695** when the expression node is.
182696*/
182697struct Fts3Expr {
182698 int eType; /* One of the FTSQUERY_XXX values defined below */
182699 int nNear; /* Valid if eType==FTSQUERY_NEAR */
182700 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
182701 Fts3Expr *pLeft; /* Left operand */
182702 Fts3Expr *pRight; /* Right operand */
182703 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
182704
182705 /* The following are used by the fts3_eval.c module. */
182706 sqlite3_int64 iDocid; /* Current docid */
182707 u8 bEof; /* True this expression is at EOF already */
182708 u8 bStart; /* True if iDocid is valid */
182709 u8 bDeferred; /* True if this expression is entirely deferred */
182710
182711 /* The following are used by the fts3_snippet.c module. */
182712 int iPhrase; /* Index of this phrase in matchinfo() results */
182713 u32 *aMI; /* See above */
182714};
182715
182716/*
182717** Candidate values for Fts3Query.eType. Note that the order of the first
182718** four values is in order of precedence when parsing expressions. For
182719** example, the following:
182720**
182721** "a OR b AND c NOT d NEAR e"
182722**
182723** is equivalent to:
182724**
182725** "a OR (b AND (c NOT (d NEAR e)))"
182726*/
182727#define FTSQUERY_NEAR 1
182728#define FTSQUERY_NOT 2
182729#define FTSQUERY_AND 3
182730#define FTSQUERY_OR 4
182731#define FTSQUERY_PHRASE 5
182732
182733
182734/* fts3_write.c */
182735SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
182736SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
182737SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
182738SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
182739SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
182740 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
182741SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
182742 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
182743SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
182744SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
182745SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
182746
182747SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
182748SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
182749
182750#ifndef SQLITE_DISABLE_FTS4_DEFERRED
182751SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
182752SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
182753SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
182754SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
182755SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
182756#else
182757# define sqlite3Fts3FreeDeferredTokens(x)
182758# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
182759# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
182760# define sqlite3Fts3FreeDeferredDoclists(x)
182761# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
182762#endif
182763
182764SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
182765SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
182766
182767/* Special values interpreted by sqlite3SegReaderCursor() */
182768#define FTS3_SEGCURSOR_PENDING -1
182769#define FTS3_SEGCURSOR_ALL -2
182770
182771SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
182772SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
182773SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
182774
182775SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
182776 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
182777
182778/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
182779#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
182780#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
182781#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
182782#define FTS3_SEGMENT_PREFIX 0x00000008
182783#define FTS3_SEGMENT_SCAN 0x00000010
182784#define FTS3_SEGMENT_FIRST 0x00000020
182785
182786/* Type passed as 4th argument to SegmentReaderIterate() */
182787struct Fts3SegFilter {
182788 const char *zTerm;
182789 int nTerm;
182790 int iCol;
182791 int flags;
182792};
182793
182794struct Fts3MultiSegReader {
182795 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
182796 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
182797 int nSegment; /* Size of apSegment array */
182798 int nAdvance; /* How many seg-readers to advance */
182799 Fts3SegFilter *pFilter; /* Pointer to filter object */
182800 char *aBuffer; /* Buffer to merge doclists in */
182801 i64 nBuffer; /* Allocated size of aBuffer[] in bytes */
182802
182803 int iColFilter; /* If >=0, filter for this column */
182804 int bRestart;
182805
182806 /* Used by fts3.c only. */
182807 int nCost; /* Cost of running iterator */
182808 int bLookup; /* True if a lookup of a single entry. */
182809
182810 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
182811 char *zTerm; /* Pointer to term buffer */
182812 int nTerm; /* Size of zTerm in bytes */
182813 char *aDoclist; /* Pointer to doclist buffer */
182814 int nDoclist; /* Size of aDoclist[] in bytes */
182815};
182816
182817SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
182818
182819#define fts3GetVarint32(p, piVal) ( \
182820 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
182821)
182822
182823/* fts3.c */
182824SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
182825SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
182826SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
182827SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
182828SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
182829SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
182830SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
182831SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
182832SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
182833SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
182834SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
182835SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
182836SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
182837SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut);
182838
182839/* fts3_tokenizer.c */
182840SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
182841SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
182842SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
182843 sqlite3_tokenizer **, char **
182844);
182845SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
182846
182847/* fts3_snippet.c */
182848SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
182849SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
182850 const char *, const char *, int, int
182851);
182852SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
182853SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
182854
182855/* fts3_expr.c */
182856SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
182857 char **, int, int, int, const char *, int, Fts3Expr **, char **
182858);
182859SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
182860#ifdef SQLITE_TEST
182861SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
182862SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
182863#endif
182864SQLITE_PRIVATE void *sqlite3Fts3MallocZero(i64 nByte);
182865
182866SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
182867 sqlite3_tokenizer_cursor **
182868);
182869
182870/* fts3_aux.c */
182871SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
182872
182873SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
182874
182875SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
182876 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
182877SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
182878 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
182879SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
182880SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
182881SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
182882
182883/* fts3_tokenize_vtab.c */
182884SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
182885
182886/* fts3_unicode2.c (functions generated by parsing unicode text files) */
182887#ifndef SQLITE_DISABLE_FTS3_UNICODE
182888SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
182889SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
182890SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
182891#endif
182892
182893SQLITE_PRIVATE int sqlite3Fts3ExprIterate(Fts3Expr*, int (*x)(Fts3Expr*,int,void*), void*);
182894
182895#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
182896#endif /* _FTSINT_H */
182897
182898/************** End of fts3Int.h *********************************************/
182899/************** Continuing where we left off in fts3.c ***********************/
182900#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
182901
182902#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
182903# define SQLITE_CORE 1
182904#endif
182905
182906/* #include <assert.h> */
182907/* #include <stdlib.h> */
182908/* #include <stddef.h> */
182909/* #include <stdio.h> */
182910/* #include <string.h> */
182911/* #include <stdarg.h> */
182912
182913/* #include "fts3.h" */
182914#ifndef SQLITE_CORE
182915/* # include "sqlite3ext.h" */
182916 SQLITE_EXTENSION_INIT1
182917#endif
182918
182919typedef struct Fts3HashWrapper Fts3HashWrapper;
182920struct Fts3HashWrapper {
182921 Fts3Hash hash; /* Hash table */
182922 int nRef; /* Number of pointers to this object */
182923};
182924
182925static int fts3EvalNext(Fts3Cursor *pCsr);
182926static int fts3EvalStart(Fts3Cursor *pCsr);
182927static int fts3TermSegReaderCursor(
182928 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
182929
182930/*
182931** This variable is set to false when running tests for which the on disk
182932** structures should not be corrupt. Otherwise, true. If it is false, extra
182933** assert() conditions in the fts3 code are activated - conditions that are
182934** only true if it is guaranteed that the fts3 database is not corrupt.
182935*/
182936#ifdef SQLITE_DEBUG
182937SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
182938#endif
182939
182940/*
182941** Write a 64-bit variable-length integer to memory starting at p[0].
182942** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
182943** The number of bytes written is returned.
182944*/
182945SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
182946 unsigned char *q = (unsigned char *) p;
182947 sqlite_uint64 vu = v;
182948 do{
182949 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
182950 vu >>= 7;
182951 }while( vu!=0 );
182952 q[-1] &= 0x7f; /* turn off high bit in final byte */
182953 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
182954 return (int) (q - (unsigned char *)p);
182955}
182956
182957#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
182958 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
182959 if( (v & mask2)==0 ){ var = v; return ret; }
182960#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
182961 v = (*ptr++); \
182962 if( (v & mask2)==0 ){ var = v; return ret; }
182963
182964SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
182965 const unsigned char *p = (const unsigned char*)pBuf;
182966 const unsigned char *pStart = p;
182967 u32 a;
182968 u64 b;
182969 int shift;
182970
182971 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
182972 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
182973 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
182974 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
182975 b = (a & 0x0FFFFFFF );
182976
182977 for(shift=28; shift<=63; shift+=7){
182978 u64 c = *p++;
182979 b += (c&0x7F) << shift;
182980 if( (c & 0x80)==0 ) break;
182981 }
182982 *v = b;
182983 return (int)(p - pStart);
182984}
182985
182986/*
182987** Read a 64-bit variable-length integer from memory starting at p[0].
182988** Return the number of bytes read, or 0 on error.
182989** The value is stored in *v.
182990*/
182991SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
182992 return sqlite3Fts3GetVarintU(pBuf, v: (sqlite3_uint64*)v);
182993}
182994
182995/*
182996** Read a 64-bit variable-length integer from memory starting at p[0] and
182997** not extending past pEnd[-1].
182998** Return the number of bytes read, or 0 on error.
182999** The value is stored in *v.
183000*/
183001SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(
183002 const char *pBuf,
183003 const char *pEnd,
183004 sqlite_int64 *v
183005){
183006 const unsigned char *p = (const unsigned char*)pBuf;
183007 const unsigned char *pStart = p;
183008 const unsigned char *pX = (const unsigned char*)pEnd;
183009 u64 b = 0;
183010 int shift;
183011 for(shift=0; shift<=63; shift+=7){
183012 u64 c = p<pX ? *p : 0;
183013 p++;
183014 b += (c&0x7F) << shift;
183015 if( (c & 0x80)==0 ) break;
183016 }
183017 *v = b;
183018 return (int)(p - pStart);
183019}
183020
183021/*
183022** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
183023** a non-negative 32-bit integer before it is returned.
183024*/
183025SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
183026 const unsigned char *ptr = (const unsigned char*)p;
183027 u32 a;
183028
183029#ifndef fts3GetVarint32
183030 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1);
183031#else
183032 a = (*ptr++);
183033 assert( a & 0x80 );
183034#endif
183035
183036 GETVARINT_STEP(a, ptr, 7, 0x7F, 0x4000, *pi, 2);
183037 GETVARINT_STEP(a, ptr, 14, 0x3FFF, 0x200000, *pi, 3);
183038 GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
183039 a = (a & 0x0FFFFFFF );
183040 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
183041 assert( 0==(a & 0x80000000) );
183042 assert( *pi>=0 );
183043 return 5;
183044}
183045
183046/*
183047** Return the number of bytes required to encode v as a varint
183048*/
183049SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
183050 int i = 0;
183051 do{
183052 i++;
183053 v >>= 7;
183054 }while( v!=0 );
183055 return i;
183056}
183057
183058/*
183059** Convert an SQL-style quoted string into a normal string by removing
183060** the quote characters. The conversion is done in-place. If the
183061** input does not begin with a quote character, then this routine
183062** is a no-op.
183063**
183064** Examples:
183065**
183066** "abc" becomes abc
183067** 'xyz' becomes xyz
183068** [pqr] becomes pqr
183069** `mno` becomes mno
183070**
183071*/
183072SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
183073 char quote; /* Quote character (if any ) */
183074
183075 quote = z[0];
183076 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
183077 int iIn = 1; /* Index of next byte to read from input */
183078 int iOut = 0; /* Index of next byte to write to output */
183079
183080 /* If the first byte was a '[', then the close-quote character is a ']' */
183081 if( quote=='[' ) quote = ']';
183082
183083 while( z[iIn] ){
183084 if( z[iIn]==quote ){
183085 if( z[iIn+1]!=quote ) break;
183086 z[iOut++] = quote;
183087 iIn += 2;
183088 }else{
183089 z[iOut++] = z[iIn++];
183090 }
183091 }
183092 z[iOut] = '\0';
183093 }
183094}
183095
183096/*
183097** Read a single varint from the doclist at *pp and advance *pp to point
183098** to the first byte past the end of the varint. Add the value of the varint
183099** to *pVal.
183100*/
183101static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
183102 sqlite3_int64 iVal;
183103 *pp += sqlite3Fts3GetVarint(pBuf: *pp, v: &iVal);
183104 *pVal += iVal;
183105}
183106
183107/*
183108** When this function is called, *pp points to the first byte following a
183109** varint that is part of a doclist (or position-list, or any other list
183110** of varints). This function moves *pp to point to the start of that varint,
183111** and sets *pVal by the varint value.
183112**
183113** Argument pStart points to the first byte of the doclist that the
183114** varint is part of.
183115*/
183116static void fts3GetReverseVarint(
183117 char **pp,
183118 char *pStart,
183119 sqlite3_int64 *pVal
183120){
183121 sqlite3_int64 iVal;
183122 char *p;
183123
183124 /* Pointer p now points at the first byte past the varint we are
183125 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
183126 ** clear on character p[-1]. */
183127 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
183128 p++;
183129 *pp = p;
183130
183131 sqlite3Fts3GetVarint(pBuf: p, v: &iVal);
183132 *pVal = iVal;
183133}
183134
183135/*
183136** The xDisconnect() virtual table method.
183137*/
183138static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
183139 Fts3Table *p = (Fts3Table *)pVtab;
183140 int i;
183141
183142 assert( p->nPendingData==0 );
183143 assert( p->pSegments==0 );
183144
183145 /* Free any prepared statements held */
183146 sqlite3_finalize(pStmt: p->pSeekStmt);
183147 for(i=0; i<SizeofArray(p->aStmt); i++){
183148 sqlite3_finalize(pStmt: p->aStmt[i]);
183149 }
183150 sqlite3_free(p: p->zSegmentsTbl);
183151 sqlite3_free(p: p->zReadExprlist);
183152 sqlite3_free(p: p->zWriteExprlist);
183153 sqlite3_free(p: p->zContentTbl);
183154 sqlite3_free(p: p->zLanguageid);
183155
183156 /* Invoke the tokenizer destructor to free the tokenizer. */
183157 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
183158
183159 sqlite3_free(p);
183160 return SQLITE_OK;
183161}
183162
183163/*
183164** Write an error message into *pzErr
183165*/
183166SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
183167 va_list ap;
183168 sqlite3_free(p: *pzErr);
183169 va_start(ap, zFormat);
183170 *pzErr = sqlite3_vmprintf(zFormat, ap);
183171 va_end(ap);
183172}
183173
183174/*
183175** Construct one or more SQL statements from the format string given
183176** and then evaluate those statements. The success code is written
183177** into *pRc.
183178**
183179** If *pRc is initially non-zero then this routine is a no-op.
183180*/
183181static void fts3DbExec(
183182 int *pRc, /* Success code */
183183 sqlite3 *db, /* Database in which to run SQL */
183184 const char *zFormat, /* Format string for SQL */
183185 ... /* Arguments to the format string */
183186){
183187 va_list ap;
183188 char *zSql;
183189 if( *pRc ) return;
183190 va_start(ap, zFormat);
183191 zSql = sqlite3_vmprintf(zFormat, ap);
183192 va_end(ap);
183193 if( zSql==0 ){
183194 *pRc = SQLITE_NOMEM;
183195 }else{
183196 *pRc = sqlite3_exec(db, zSql, xCallback: 0, pArg: 0, pzErrMsg: 0);
183197 sqlite3_free(p: zSql);
183198 }
183199}
183200
183201/*
183202** The xDestroy() virtual table method.
183203*/
183204static int fts3DestroyMethod(sqlite3_vtab *pVtab){
183205 Fts3Table *p = (Fts3Table *)pVtab;
183206 int rc = SQLITE_OK; /* Return code */
183207 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
183208 sqlite3 *db = p->db; /* Database handle */
183209
183210 /* Drop the shadow tables */
183211 fts3DbExec(pRc: &rc, db,
183212 zFormat: "DROP TABLE IF EXISTS %Q.'%q_segments';"
183213 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
183214 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
183215 "DROP TABLE IF EXISTS %Q.'%q_stat';"
183216 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
183217 zDb, p->zName,
183218 zDb, p->zName,
183219 zDb, p->zName,
183220 zDb, p->zName,
183221 (p->zContentTbl ? "--" : ""), zDb,p->zName
183222 );
183223
183224 /* If everything has worked, invoke fts3DisconnectMethod() to free the
183225 ** memory associated with the Fts3Table structure and return SQLITE_OK.
183226 ** Otherwise, return an SQLite error code.
183227 */
183228 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
183229}
183230
183231
183232/*
183233** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
183234** passed as the first argument. This is done as part of the xConnect()
183235** and xCreate() methods.
183236**
183237** If *pRc is non-zero when this function is called, it is a no-op.
183238** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
183239** before returning.
183240*/
183241static void fts3DeclareVtab(int *pRc, Fts3Table *p){
183242 if( *pRc==SQLITE_OK ){
183243 int i; /* Iterator variable */
183244 int rc; /* Return code */
183245 char *zSql; /* SQL statement passed to declare_vtab() */
183246 char *zCols; /* List of user defined columns */
183247 const char *zLanguageid;
183248
183249 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
183250 sqlite3_vtab_config(db: p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
183251
183252 /* Create a list of user columns for the virtual table */
183253 zCols = sqlite3_mprintf(zFormat: "%Q, ", p->azColumn[0]);
183254 for(i=1; zCols && i<p->nColumn; i++){
183255 zCols = sqlite3_mprintf(zFormat: "%z%Q, ", zCols, p->azColumn[i]);
183256 }
183257
183258 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
183259 zSql = sqlite3_mprintf(
183260 zFormat: "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
183261 zCols, p->zName, zLanguageid
183262 );
183263 if( !zCols || !zSql ){
183264 rc = SQLITE_NOMEM;
183265 }else{
183266 rc = sqlite3_declare_vtab(db: p->db, zCreateTable: zSql);
183267 }
183268
183269 sqlite3_free(p: zSql);
183270 sqlite3_free(p: zCols);
183271 *pRc = rc;
183272 }
183273}
183274
183275/*
183276** Create the %_stat table if it does not already exist.
183277*/
183278SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
183279 fts3DbExec(pRc, db: p->db,
183280 zFormat: "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
183281 "(id INTEGER PRIMARY KEY, value BLOB);",
183282 p->zDb, p->zName
183283 );
183284 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
183285}
183286
183287/*
183288** Create the backing store tables (%_content, %_segments and %_segdir)
183289** required by the FTS3 table passed as the only argument. This is done
183290** as part of the vtab xCreate() method.
183291**
183292** If the p->bHasDocsize boolean is true (indicating that this is an
183293** FTS4 table, not an FTS3 table) then also create the %_docsize and
183294** %_stat tables required by FTS4.
183295*/
183296static int fts3CreateTables(Fts3Table *p){
183297 int rc = SQLITE_OK; /* Return code */
183298 int i; /* Iterator variable */
183299 sqlite3 *db = p->db; /* The database connection */
183300
183301 if( p->zContentTbl==0 ){
183302 const char *zLanguageid = p->zLanguageid;
183303 char *zContentCols; /* Columns of %_content table */
183304
183305 /* Create a list of user columns for the content table */
183306 zContentCols = sqlite3_mprintf(zFormat: "docid INTEGER PRIMARY KEY");
183307 for(i=0; zContentCols && i<p->nColumn; i++){
183308 char *z = p->azColumn[i];
183309 zContentCols = sqlite3_mprintf(zFormat: "%z, 'c%d%q'", zContentCols, i, z);
183310 }
183311 if( zLanguageid && zContentCols ){
183312 zContentCols = sqlite3_mprintf(zFormat: "%z, langid", zContentCols, zLanguageid);
183313 }
183314 if( zContentCols==0 ) rc = SQLITE_NOMEM;
183315
183316 /* Create the content table */
183317 fts3DbExec(pRc: &rc, db,
183318 zFormat: "CREATE TABLE %Q.'%q_content'(%s)",
183319 p->zDb, p->zName, zContentCols
183320 );
183321 sqlite3_free(p: zContentCols);
183322 }
183323
183324 /* Create other tables */
183325 fts3DbExec(pRc: &rc, db,
183326 zFormat: "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
183327 p->zDb, p->zName
183328 );
183329 fts3DbExec(pRc: &rc, db,
183330 zFormat: "CREATE TABLE %Q.'%q_segdir'("
183331 "level INTEGER,"
183332 "idx INTEGER,"
183333 "start_block INTEGER,"
183334 "leaves_end_block INTEGER,"
183335 "end_block INTEGER,"
183336 "root BLOB,"
183337 "PRIMARY KEY(level, idx)"
183338 ");",
183339 p->zDb, p->zName
183340 );
183341 if( p->bHasDocsize ){
183342 fts3DbExec(pRc: &rc, db,
183343 zFormat: "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
183344 p->zDb, p->zName
183345 );
183346 }
183347 assert( p->bHasStat==p->bFts4 );
183348 if( p->bHasStat ){
183349 sqlite3Fts3CreateStatTable(pRc: &rc, p);
183350 }
183351 return rc;
183352}
183353
183354/*
183355** Store the current database page-size in bytes in p->nPgsz.
183356**
183357** If *pRc is non-zero when this function is called, it is a no-op.
183358** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
183359** before returning.
183360*/
183361static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
183362 if( *pRc==SQLITE_OK ){
183363 int rc; /* Return code */
183364 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
183365 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
183366
183367 zSql = sqlite3_mprintf(zFormat: "PRAGMA %Q.page_size", p->zDb);
183368 if( !zSql ){
183369 rc = SQLITE_NOMEM;
183370 }else{
183371 rc = sqlite3_prepare(db: p->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
183372 if( rc==SQLITE_OK ){
183373 sqlite3_step(pStmt);
183374 p->nPgsz = sqlite3_column_int(pStmt, i: 0);
183375 rc = sqlite3_finalize(pStmt);
183376 }else if( rc==SQLITE_AUTH ){
183377 p->nPgsz = 1024;
183378 rc = SQLITE_OK;
183379 }
183380 }
183381 assert( p->nPgsz>0 || rc!=SQLITE_OK );
183382 sqlite3_free(p: zSql);
183383 *pRc = rc;
183384 }
183385}
183386
183387/*
183388** "Special" FTS4 arguments are column specifications of the following form:
183389**
183390** <key> = <value>
183391**
183392** There may not be whitespace surrounding the "=" character. The <value>
183393** term may be quoted, but the <key> may not.
183394*/
183395static int fts3IsSpecialColumn(
183396 const char *z,
183397 int *pnKey,
183398 char **pzValue
183399){
183400 char *zValue;
183401 const char *zCsr = z;
183402
183403 while( *zCsr!='=' ){
183404 if( *zCsr=='\0' ) return 0;
183405 zCsr++;
183406 }
183407
183408 *pnKey = (int)(zCsr-z);
183409 zValue = sqlite3_mprintf(zFormat: "%s", &zCsr[1]);
183410 if( zValue ){
183411 sqlite3Fts3Dequote(z: zValue);
183412 }
183413 *pzValue = zValue;
183414 return 1;
183415}
183416
183417/*
183418** Append the output of a printf() style formatting to an existing string.
183419*/
183420static void fts3Appendf(
183421 int *pRc, /* IN/OUT: Error code */
183422 char **pz, /* IN/OUT: Pointer to string buffer */
183423 const char *zFormat, /* Printf format string to append */
183424 ... /* Arguments for printf format string */
183425){
183426 if( *pRc==SQLITE_OK ){
183427 va_list ap;
183428 char *z;
183429 va_start(ap, zFormat);
183430 z = sqlite3_vmprintf(zFormat, ap);
183431 va_end(ap);
183432 if( z && *pz ){
183433 char *z2 = sqlite3_mprintf(zFormat: "%s%s", *pz, z);
183434 sqlite3_free(p: z);
183435 z = z2;
183436 }
183437 if( z==0 ) *pRc = SQLITE_NOMEM;
183438 sqlite3_free(p: *pz);
183439 *pz = z;
183440 }
183441}
183442
183443/*
183444** Return a copy of input string zInput enclosed in double-quotes (") and
183445** with all double quote characters escaped. For example:
183446**
183447** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
183448**
183449** The pointer returned points to memory obtained from sqlite3_malloc(). It
183450** is the callers responsibility to call sqlite3_free() to release this
183451** memory.
183452*/
183453static char *fts3QuoteId(char const *zInput){
183454 sqlite3_int64 nRet;
183455 char *zRet;
183456 nRet = 2 + (int)strlen(s: zInput)*2 + 1;
183457 zRet = sqlite3_malloc64(n: nRet);
183458 if( zRet ){
183459 int i;
183460 char *z = zRet;
183461 *(z++) = '"';
183462 for(i=0; zInput[i]; i++){
183463 if( zInput[i]=='"' ) *(z++) = '"';
183464 *(z++) = zInput[i];
183465 }
183466 *(z++) = '"';
183467 *(z++) = '\0';
183468 }
183469 return zRet;
183470}
183471
183472/*
183473** Return a list of comma separated SQL expressions and a FROM clause that
183474** could be used in a SELECT statement such as the following:
183475**
183476** SELECT <list of expressions> FROM %_content AS x ...
183477**
183478** to return the docid, followed by each column of text data in order
183479** from left to write. If parameter zFunc is not NULL, then instead of
183480** being returned directly each column of text data is passed to an SQL
183481** function named zFunc first. For example, if zFunc is "unzip" and the
183482** table has the three user-defined columns "a", "b", and "c", the following
183483** string is returned:
183484**
183485** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
183486**
183487** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
183488** is the responsibility of the caller to eventually free it.
183489**
183490** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
183491** a NULL pointer is returned). Otherwise, if an OOM error is encountered
183492** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
183493** no error occurs, *pRc is left unmodified.
183494*/
183495static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
183496 char *zRet = 0;
183497 char *zFree = 0;
183498 char *zFunction;
183499 int i;
183500
183501 if( p->zContentTbl==0 ){
183502 if( !zFunc ){
183503 zFunction = "";
183504 }else{
183505 zFree = zFunction = fts3QuoteId(zInput: zFunc);
183506 }
183507 fts3Appendf(pRc, pz: &zRet, zFormat: "docid");
183508 for(i=0; i<p->nColumn; i++){
183509 fts3Appendf(pRc, pz: &zRet, zFormat: ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
183510 }
183511 if( p->zLanguageid ){
183512 fts3Appendf(pRc, pz: &zRet, zFormat: ", x.%Q", "langid");
183513 }
183514 sqlite3_free(p: zFree);
183515 }else{
183516 fts3Appendf(pRc, pz: &zRet, zFormat: "rowid");
183517 for(i=0; i<p->nColumn; i++){
183518 fts3Appendf(pRc, pz: &zRet, zFormat: ", x.'%q'", p->azColumn[i]);
183519 }
183520 if( p->zLanguageid ){
183521 fts3Appendf(pRc, pz: &zRet, zFormat: ", x.%Q", p->zLanguageid);
183522 }
183523 }
183524 fts3Appendf(pRc, pz: &zRet, zFormat: " FROM '%q'.'%q%s' AS x",
183525 p->zDb,
183526 (p->zContentTbl ? p->zContentTbl : p->zName),
183527 (p->zContentTbl ? "" : "_content")
183528 );
183529 return zRet;
183530}
183531
183532/*
183533** Return a list of N comma separated question marks, where N is the number
183534** of columns in the %_content table (one for the docid plus one for each
183535** user-defined text column).
183536**
183537** If argument zFunc is not NULL, then all but the first question mark
183538** is preceded by zFunc and an open bracket, and followed by a closed
183539** bracket. For example, if zFunc is "zip" and the FTS3 table has three
183540** user-defined text columns, the following string is returned:
183541**
183542** "?, zip(?), zip(?), zip(?)"
183543**
183544** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
183545** is the responsibility of the caller to eventually free it.
183546**
183547** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
183548** a NULL pointer is returned). Otherwise, if an OOM error is encountered
183549** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
183550** no error occurs, *pRc is left unmodified.
183551*/
183552static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
183553 char *zRet = 0;
183554 char *zFree = 0;
183555 char *zFunction;
183556 int i;
183557
183558 if( !zFunc ){
183559 zFunction = "";
183560 }else{
183561 zFree = zFunction = fts3QuoteId(zInput: zFunc);
183562 }
183563 fts3Appendf(pRc, pz: &zRet, zFormat: "?");
183564 for(i=0; i<p->nColumn; i++){
183565 fts3Appendf(pRc, pz: &zRet, zFormat: ",%s(?)", zFunction);
183566 }
183567 if( p->zLanguageid ){
183568 fts3Appendf(pRc, pz: &zRet, zFormat: ", ?");
183569 }
183570 sqlite3_free(p: zFree);
183571 return zRet;
183572}
183573
183574/*
183575** Buffer z contains a positive integer value encoded as utf-8 text.
183576** Decode this value and store it in *pnOut, returning the number of bytes
183577** consumed. If an overflow error occurs return a negative value.
183578*/
183579SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut){
183580 u64 iVal = 0;
183581 int i;
183582 for(i=0; z[i]>='0' && z[i]<='9'; i++){
183583 iVal = iVal*10 + (z[i] - '0');
183584 if( iVal>0x7FFFFFFF ) return -1;
183585 }
183586 *pnOut = (int)iVal;
183587 return i;
183588}
183589
183590/*
183591** This function interprets the string at (*pp) as a non-negative integer
183592** value. It reads the integer and sets *pnOut to the value read, then
183593** sets *pp to point to the byte immediately following the last byte of
183594** the integer value.
183595**
183596** Only decimal digits ('0'..'9') may be part of an integer value.
183597**
183598** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
183599** the output value undefined. Otherwise SQLITE_OK is returned.
183600**
183601** This function is used when parsing the "prefix=" FTS4 parameter.
183602*/
183603static int fts3GobbleInt(const char **pp, int *pnOut){
183604 const int MAX_NPREFIX = 10000000;
183605 int nInt = 0; /* Output value */
183606 int nByte;
183607 nByte = sqlite3Fts3ReadInt(z: *pp, pnOut: &nInt);
183608 if( nInt>MAX_NPREFIX ){
183609 nInt = 0;
183610 }
183611 if( nByte==0 ){
183612 return SQLITE_ERROR;
183613 }
183614 *pnOut = nInt;
183615 *pp += nByte;
183616 return SQLITE_OK;
183617}
183618
183619/*
183620** This function is called to allocate an array of Fts3Index structures
183621** representing the indexes maintained by the current FTS table. FTS tables
183622** always maintain the main "terms" index, but may also maintain one or
183623** more "prefix" indexes, depending on the value of the "prefix=" parameter
183624** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
183625**
183626** Argument zParam is passed the value of the "prefix=" option if one was
183627** specified, or NULL otherwise.
183628**
183629** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
183630** the allocated array. *pnIndex is set to the number of elements in the
183631** array. If an error does occur, an SQLite error code is returned.
183632**
183633** Regardless of whether or not an error is returned, it is the responsibility
183634** of the caller to call sqlite3_free() on the output array to free it.
183635*/
183636static int fts3PrefixParameter(
183637 const char *zParam, /* ABC in prefix=ABC parameter to parse */
183638 int *pnIndex, /* OUT: size of *apIndex[] array */
183639 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
183640){
183641 struct Fts3Index *aIndex; /* Allocated array */
183642 int nIndex = 1; /* Number of entries in array */
183643
183644 if( zParam && zParam[0] ){
183645 const char *p;
183646 nIndex++;
183647 for(p=zParam; *p; p++){
183648 if( *p==',' ) nIndex++;
183649 }
183650 }
183651
183652 aIndex = sqlite3_malloc64(n: sizeof(struct Fts3Index) * nIndex);
183653 *apIndex = aIndex;
183654 if( !aIndex ){
183655 return SQLITE_NOMEM;
183656 }
183657
183658 memset(s: aIndex, c: 0, n: sizeof(struct Fts3Index) * nIndex);
183659 if( zParam ){
183660 const char *p = zParam;
183661 int i;
183662 for(i=1; i<nIndex; i++){
183663 int nPrefix = 0;
183664 if( fts3GobbleInt(pp: &p, pnOut: &nPrefix) ) return SQLITE_ERROR;
183665 assert( nPrefix>=0 );
183666 if( nPrefix==0 ){
183667 nIndex--;
183668 i--;
183669 }else{
183670 aIndex[i].nPrefix = nPrefix;
183671 }
183672 p++;
183673 }
183674 }
183675
183676 *pnIndex = nIndex;
183677 return SQLITE_OK;
183678}
183679
183680/*
183681** This function is called when initializing an FTS4 table that uses the
183682** content=xxx option. It determines the number of and names of the columns
183683** of the new FTS4 table.
183684**
183685** The third argument passed to this function is the value passed to the
183686** config=xxx option (i.e. "xxx"). This function queries the database for
183687** a table of that name. If found, the output variables are populated
183688** as follows:
183689**
183690** *pnCol: Set to the number of columns table xxx has,
183691**
183692** *pnStr: Set to the total amount of space required to store a copy
183693** of each columns name, including the nul-terminator.
183694**
183695** *pazCol: Set to point to an array of *pnCol strings. Each string is
183696** the name of the corresponding column in table xxx. The array
183697** and its contents are allocated using a single allocation. It
183698** is the responsibility of the caller to free this allocation
183699** by eventually passing the *pazCol value to sqlite3_free().
183700**
183701** If the table cannot be found, an error code is returned and the output
183702** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
183703** returned (and the output variables are undefined).
183704*/
183705static int fts3ContentColumns(
183706 sqlite3 *db, /* Database handle */
183707 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
183708 const char *zTbl, /* Name of content table */
183709 const char ***pazCol, /* OUT: Malloc'd array of column names */
183710 int *pnCol, /* OUT: Size of array *pazCol */
183711 int *pnStr, /* OUT: Bytes of string content */
183712 char **pzErr /* OUT: error message */
183713){
183714 int rc = SQLITE_OK; /* Return code */
183715 char *zSql; /* "SELECT *" statement on zTbl */
183716 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
183717
183718 zSql = sqlite3_mprintf(zFormat: "SELECT * FROM %Q.%Q", zDb, zTbl);
183719 if( !zSql ){
183720 rc = SQLITE_NOMEM;
183721 }else{
183722 rc = sqlite3_prepare(db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
183723 if( rc!=SQLITE_OK ){
183724 sqlite3Fts3ErrMsg(pzErr, zFormat: "%s", sqlite3_errmsg(db));
183725 }
183726 }
183727 sqlite3_free(p: zSql);
183728
183729 if( rc==SQLITE_OK ){
183730 const char **azCol; /* Output array */
183731 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
183732 int nCol; /* Number of table columns */
183733 int i; /* Used to iterate through columns */
183734
183735 /* Loop through the returned columns. Set nStr to the number of bytes of
183736 ** space required to store a copy of each column name, including the
183737 ** nul-terminator byte. */
183738 nCol = sqlite3_column_count(pStmt);
183739 for(i=0; i<nCol; i++){
183740 const char *zCol = sqlite3_column_name(pStmt, N: i);
183741 nStr += strlen(s: zCol) + 1;
183742 }
183743
183744 /* Allocate and populate the array to return. */
183745 azCol = (const char **)sqlite3_malloc64(n: sizeof(char *) * nCol + nStr);
183746 if( azCol==0 ){
183747 rc = SQLITE_NOMEM;
183748 }else{
183749 char *p = (char *)&azCol[nCol];
183750 for(i=0; i<nCol; i++){
183751 const char *zCol = sqlite3_column_name(pStmt, N: i);
183752 int n = (int)strlen(s: zCol)+1;
183753 memcpy(dest: p, src: zCol, n: n);
183754 azCol[i] = p;
183755 p += n;
183756 }
183757 }
183758 sqlite3_finalize(pStmt);
183759
183760 /* Set the output variables. */
183761 *pnCol = nCol;
183762 *pnStr = nStr;
183763 *pazCol = azCol;
183764 }
183765
183766 return rc;
183767}
183768
183769/*
183770** This function is the implementation of both the xConnect and xCreate
183771** methods of the FTS3 virtual table.
183772**
183773** The argv[] array contains the following:
183774**
183775** argv[0] -> module name ("fts3" or "fts4")
183776** argv[1] -> database name
183777** argv[2] -> table name
183778** argv[...] -> "column name" and other module argument fields.
183779*/
183780static int fts3InitVtab(
183781 int isCreate, /* True for xCreate, false for xConnect */
183782 sqlite3 *db, /* The SQLite database connection */
183783 void *pAux, /* Hash table containing tokenizers */
183784 int argc, /* Number of elements in argv array */
183785 const char * const *argv, /* xCreate/xConnect argument array */
183786 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
183787 char **pzErr /* Write any error message here */
183788){
183789 Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
183790 Fts3Table *p = 0; /* Pointer to allocated vtab */
183791 int rc = SQLITE_OK; /* Return code */
183792 int i; /* Iterator variable */
183793 sqlite3_int64 nByte; /* Size of allocation used for *p */
183794 int iCol; /* Column index */
183795 int nString = 0; /* Bytes required to hold all column names */
183796 int nCol = 0; /* Number of columns in the FTS table */
183797 char *zCsr; /* Space for holding column names */
183798 int nDb; /* Bytes required to hold database name */
183799 int nName; /* Bytes required to hold table name */
183800 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
183801 const char **aCol; /* Array of column names */
183802 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
183803
183804 int nIndex = 0; /* Size of aIndex[] array */
183805 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
183806
183807 /* The results of parsing supported FTS4 key=value options: */
183808 int bNoDocsize = 0; /* True to omit %_docsize table */
183809 int bDescIdx = 0; /* True to store descending indexes */
183810 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
183811 char *zCompress = 0; /* compress=? parameter (or NULL) */
183812 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
183813 char *zContent = 0; /* content=? parameter (or NULL) */
183814 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
183815 char **azNotindexed = 0; /* The set of notindexed= columns */
183816 int nNotindexed = 0; /* Size of azNotindexed[] array */
183817
183818 assert( strlen(argv[0])==4 );
183819 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
183820 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
183821 );
183822
183823 nDb = (int)strlen(s: argv[1]) + 1;
183824 nName = (int)strlen(s: argv[2]) + 1;
183825
183826 nByte = sizeof(const char *) * (argc-2);
183827 aCol = (const char **)sqlite3_malloc64(n: nByte);
183828 if( aCol ){
183829 memset(s: (void*)aCol, c: 0, n: nByte);
183830 azNotindexed = (char **)sqlite3_malloc64(n: nByte);
183831 }
183832 if( azNotindexed ){
183833 memset(s: azNotindexed, c: 0, n: nByte);
183834 }
183835 if( !aCol || !azNotindexed ){
183836 rc = SQLITE_NOMEM;
183837 goto fts3_init_out;
183838 }
183839
183840 /* Loop through all of the arguments passed by the user to the FTS3/4
183841 ** module (i.e. all the column names and special arguments). This loop
183842 ** does the following:
183843 **
183844 ** + Figures out the number of columns the FTSX table will have, and
183845 ** the number of bytes of space that must be allocated to store copies
183846 ** of the column names.
183847 **
183848 ** + If there is a tokenizer specification included in the arguments,
183849 ** initializes the tokenizer pTokenizer.
183850 */
183851 for(i=3; rc==SQLITE_OK && i<argc; i++){
183852 char const *z = argv[i];
183853 int nKey;
183854 char *zVal;
183855
183856 /* Check if this is a tokenizer specification */
183857 if( !pTokenizer
183858 && strlen(s: z)>8
183859 && 0==sqlite3_strnicmp(zLeft: z, zRight: "tokenize", N: 8)
183860 && 0==sqlite3Fts3IsIdChar(z[8])
183861 ){
183862 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
183863 }
183864
183865 /* Check if it is an FTS4 special argument. */
183866 else if( isFts4 && fts3IsSpecialColumn(z, pnKey: &nKey, pzValue: &zVal) ){
183867 struct Fts4Option {
183868 const char *zOpt;
183869 int nOpt;
183870 } aFts4Opt[] = {
183871 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
183872 { "prefix", 6 }, /* 1 -> PREFIX */
183873 { "compress", 8 }, /* 2 -> COMPRESS */
183874 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
183875 { "order", 5 }, /* 4 -> ORDER */
183876 { "content", 7 }, /* 5 -> CONTENT */
183877 { "languageid", 10 }, /* 6 -> LANGUAGEID */
183878 { "notindexed", 10 } /* 7 -> NOTINDEXED */
183879 };
183880
183881 int iOpt;
183882 if( !zVal ){
183883 rc = SQLITE_NOMEM;
183884 }else{
183885 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
183886 struct Fts4Option *pOp = &aFts4Opt[iOpt];
183887 if( nKey==pOp->nOpt && !sqlite3_strnicmp(zLeft: z, zRight: pOp->zOpt, N: pOp->nOpt) ){
183888 break;
183889 }
183890 }
183891 switch( iOpt ){
183892 case 0: /* MATCHINFO */
183893 if( strlen(s: zVal)!=4 || sqlite3_strnicmp(zLeft: zVal, zRight: "fts3", N: 4) ){
183894 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized matchinfo: %s", zVal);
183895 rc = SQLITE_ERROR;
183896 }
183897 bNoDocsize = 1;
183898 break;
183899
183900 case 1: /* PREFIX */
183901 sqlite3_free(p: zPrefix);
183902 zPrefix = zVal;
183903 zVal = 0;
183904 break;
183905
183906 case 2: /* COMPRESS */
183907 sqlite3_free(p: zCompress);
183908 zCompress = zVal;
183909 zVal = 0;
183910 break;
183911
183912 case 3: /* UNCOMPRESS */
183913 sqlite3_free(p: zUncompress);
183914 zUncompress = zVal;
183915 zVal = 0;
183916 break;
183917
183918 case 4: /* ORDER */
183919 if( (strlen(s: zVal)!=3 || sqlite3_strnicmp(zLeft: zVal, zRight: "asc", N: 3))
183920 && (strlen(s: zVal)!=4 || sqlite3_strnicmp(zLeft: zVal, zRight: "desc", N: 4))
183921 ){
183922 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized order: %s", zVal);
183923 rc = SQLITE_ERROR;
183924 }
183925 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
183926 break;
183927
183928 case 5: /* CONTENT */
183929 sqlite3_free(p: zContent);
183930 zContent = zVal;
183931 zVal = 0;
183932 break;
183933
183934 case 6: /* LANGUAGEID */
183935 assert( iOpt==6 );
183936 sqlite3_free(p: zLanguageid);
183937 zLanguageid = zVal;
183938 zVal = 0;
183939 break;
183940
183941 case 7: /* NOTINDEXED */
183942 azNotindexed[nNotindexed++] = zVal;
183943 zVal = 0;
183944 break;
183945
183946 default:
183947 assert( iOpt==SizeofArray(aFts4Opt) );
183948 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized parameter: %s", z);
183949 rc = SQLITE_ERROR;
183950 break;
183951 }
183952 sqlite3_free(p: zVal);
183953 }
183954 }
183955
183956 /* Otherwise, the argument is a column name. */
183957 else {
183958 nString += (int)(strlen(s: z) + 1);
183959 aCol[nCol++] = z;
183960 }
183961 }
183962
183963 /* If a content=xxx option was specified, the following:
183964 **
183965 ** 1. Ignore any compress= and uncompress= options.
183966 **
183967 ** 2. If no column names were specified as part of the CREATE VIRTUAL
183968 ** TABLE statement, use all columns from the content table.
183969 */
183970 if( rc==SQLITE_OK && zContent ){
183971 sqlite3_free(p: zCompress);
183972 sqlite3_free(p: zUncompress);
183973 zCompress = 0;
183974 zUncompress = 0;
183975 if( nCol==0 ){
183976 sqlite3_free(p: (void*)aCol);
183977 aCol = 0;
183978 rc = fts3ContentColumns(db, zDb: argv[1], zTbl: zContent,pazCol: &aCol,pnCol: &nCol,pnStr: &nString,pzErr);
183979
183980 /* If a languageid= option was specified, remove the language id
183981 ** column from the aCol[] array. */
183982 if( rc==SQLITE_OK && zLanguageid ){
183983 int j;
183984 for(j=0; j<nCol; j++){
183985 if( sqlite3_stricmp(zLeft: zLanguageid, zRight: aCol[j])==0 ){
183986 int k;
183987 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
183988 nCol--;
183989 break;
183990 }
183991 }
183992 }
183993 }
183994 }
183995 if( rc!=SQLITE_OK ) goto fts3_init_out;
183996
183997 if( nCol==0 ){
183998 assert( nString==0 );
183999 aCol[0] = "content";
184000 nString = 8;
184001 nCol = 1;
184002 }
184003
184004 if( pTokenizer==0 ){
184005 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
184006 if( rc!=SQLITE_OK ) goto fts3_init_out;
184007 }
184008 assert( pTokenizer );
184009
184010 rc = fts3PrefixParameter(zParam: zPrefix, pnIndex: &nIndex, apIndex: &aIndex);
184011 if( rc==SQLITE_ERROR ){
184012 assert( zPrefix );
184013 sqlite3Fts3ErrMsg(pzErr, zFormat: "error parsing prefix parameter: %s", zPrefix);
184014 }
184015 if( rc!=SQLITE_OK ) goto fts3_init_out;
184016
184017 /* Allocate and populate the Fts3Table structure. */
184018 nByte = sizeof(Fts3Table) + /* Fts3Table */
184019 nCol * sizeof(char *) + /* azColumn */
184020 nIndex * sizeof(struct Fts3Index) + /* aIndex */
184021 nCol * sizeof(u8) + /* abNotindexed */
184022 nName + /* zName */
184023 nDb + /* zDb */
184024 nString; /* Space for azColumn strings */
184025 p = (Fts3Table*)sqlite3_malloc64(n: nByte);
184026 if( p==0 ){
184027 rc = SQLITE_NOMEM;
184028 goto fts3_init_out;
184029 }
184030 memset(s: p, c: 0, n: nByte);
184031 p->db = db;
184032 p->nColumn = nCol;
184033 p->nPendingData = 0;
184034 p->azColumn = (char **)&p[1];
184035 p->pTokenizer = pTokenizer;
184036 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
184037 p->bHasDocsize = (isFts4 && bNoDocsize==0);
184038 p->bHasStat = (u8)isFts4;
184039 p->bFts4 = (u8)isFts4;
184040 p->bDescIdx = (u8)bDescIdx;
184041 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
184042 p->zContentTbl = zContent;
184043 p->zLanguageid = zLanguageid;
184044 zContent = 0;
184045 zLanguageid = 0;
184046 TESTONLY( p->inTransaction = -1 );
184047 TESTONLY( p->mxSavepoint = -1 );
184048
184049 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
184050 memcpy(dest: p->aIndex, src: aIndex, n: sizeof(struct Fts3Index) * nIndex);
184051 p->nIndex = nIndex;
184052 for(i=0; i<nIndex; i++){
184053 fts3HashInit(pNew: &p->aIndex[i].hPending, FTS3_HASH_STRING, copyKey: 1);
184054 }
184055 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
184056
184057 /* Fill in the zName and zDb fields of the vtab structure. */
184058 zCsr = (char *)&p->abNotindexed[nCol];
184059 p->zName = zCsr;
184060 memcpy(dest: zCsr, src: argv[2], n: nName);
184061 zCsr += nName;
184062 p->zDb = zCsr;
184063 memcpy(dest: zCsr, src: argv[1], n: nDb);
184064 zCsr += nDb;
184065
184066 /* Fill in the azColumn array */
184067 for(iCol=0; iCol<nCol; iCol++){
184068 char *z;
184069 int n = 0;
184070 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
184071 if( n>0 ){
184072 memcpy(dest: zCsr, src: z, n: n);
184073 }
184074 zCsr[n] = '\0';
184075 sqlite3Fts3Dequote(z: zCsr);
184076 p->azColumn[iCol] = zCsr;
184077 zCsr += n+1;
184078 assert( zCsr <= &((char *)p)[nByte] );
184079 }
184080
184081 /* Fill in the abNotindexed array */
184082 for(iCol=0; iCol<nCol; iCol++){
184083 int n = (int)strlen(s: p->azColumn[iCol]);
184084 for(i=0; i<nNotindexed; i++){
184085 char *zNot = azNotindexed[i];
184086 if( zNot && n==(int)strlen(s: zNot)
184087 && 0==sqlite3_strnicmp(zLeft: p->azColumn[iCol], zRight: zNot, N: n)
184088 ){
184089 p->abNotindexed[iCol] = 1;
184090 sqlite3_free(p: zNot);
184091 azNotindexed[i] = 0;
184092 }
184093 }
184094 }
184095 for(i=0; i<nNotindexed; i++){
184096 if( azNotindexed[i] ){
184097 sqlite3Fts3ErrMsg(pzErr, zFormat: "no such column: %s", azNotindexed[i]);
184098 rc = SQLITE_ERROR;
184099 }
184100 }
184101
184102 if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
184103 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
184104 rc = SQLITE_ERROR;
184105 sqlite3Fts3ErrMsg(pzErr, zFormat: "missing %s parameter in fts4 constructor", zMiss);
184106 }
184107 p->zReadExprlist = fts3ReadExprList(p, zFunc: zUncompress, pRc: &rc);
184108 p->zWriteExprlist = fts3WriteExprList(p, zFunc: zCompress, pRc: &rc);
184109 if( rc!=SQLITE_OK ) goto fts3_init_out;
184110
184111 /* If this is an xCreate call, create the underlying tables in the
184112 ** database. TODO: For xConnect(), it could verify that said tables exist.
184113 */
184114 if( isCreate ){
184115 rc = fts3CreateTables(p);
184116 }
184117
184118 /* Check to see if a legacy fts3 table has been "upgraded" by the
184119 ** addition of a %_stat table so that it can use incremental merge.
184120 */
184121 if( !isFts4 && !isCreate ){
184122 p->bHasStat = 2;
184123 }
184124
184125 /* Figure out the page-size for the database. This is required in order to
184126 ** estimate the cost of loading large doclists from the database. */
184127 fts3DatabasePageSize(pRc: &rc, p);
184128 p->nNodeSize = p->nPgsz-35;
184129
184130#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
184131 p->nMergeCount = FTS3_MERGE_COUNT;
184132#endif
184133
184134 /* Declare the table schema to SQLite. */
184135 fts3DeclareVtab(pRc: &rc, p);
184136
184137fts3_init_out:
184138 sqlite3_free(p: zPrefix);
184139 sqlite3_free(p: aIndex);
184140 sqlite3_free(p: zCompress);
184141 sqlite3_free(p: zUncompress);
184142 sqlite3_free(p: zContent);
184143 sqlite3_free(p: zLanguageid);
184144 for(i=0; i<nNotindexed; i++) sqlite3_free(p: azNotindexed[i]);
184145 sqlite3_free(p: (void *)aCol);
184146 sqlite3_free(p: (void *)azNotindexed);
184147 if( rc!=SQLITE_OK ){
184148 if( p ){
184149 fts3DisconnectMethod(pVtab: (sqlite3_vtab *)p);
184150 }else if( pTokenizer ){
184151 pTokenizer->pModule->xDestroy(pTokenizer);
184152 }
184153 }else{
184154 assert( p->pSegments==0 );
184155 *ppVTab = &p->base;
184156 }
184157 return rc;
184158}
184159
184160/*
184161** The xConnect() and xCreate() methods for the virtual table. All the
184162** work is done in function fts3InitVtab().
184163*/
184164static int fts3ConnectMethod(
184165 sqlite3 *db, /* Database connection */
184166 void *pAux, /* Pointer to tokenizer hash table */
184167 int argc, /* Number of elements in argv array */
184168 const char * const *argv, /* xCreate/xConnect argument array */
184169 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
184170 char **pzErr /* OUT: sqlite3_malloc'd error message */
184171){
184172 return fts3InitVtab(isCreate: 0, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
184173}
184174static int fts3CreateMethod(
184175 sqlite3 *db, /* Database connection */
184176 void *pAux, /* Pointer to tokenizer hash table */
184177 int argc, /* Number of elements in argv array */
184178 const char * const *argv, /* xCreate/xConnect argument array */
184179 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
184180 char **pzErr /* OUT: sqlite3_malloc'd error message */
184181){
184182 return fts3InitVtab(isCreate: 1, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
184183}
184184
184185/*
184186** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
184187** extension is currently being used by a version of SQLite too old to
184188** support estimatedRows. In that case this function is a no-op.
184189*/
184190static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
184191#if SQLITE_VERSION_NUMBER>=3008002
184192 if( sqlite3_libversion_number()>=3008002 ){
184193 pIdxInfo->estimatedRows = nRow;
184194 }
184195#endif
184196}
184197
184198/*
184199** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
184200** extension is currently being used by a version of SQLite too old to
184201** support index-info flags. In that case this function is a no-op.
184202*/
184203static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
184204#if SQLITE_VERSION_NUMBER>=3008012
184205 if( sqlite3_libversion_number()>=3008012 ){
184206 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
184207 }
184208#endif
184209}
184210
184211/*
184212** Implementation of the xBestIndex method for FTS3 tables. There
184213** are three possible strategies, in order of preference:
184214**
184215** 1. Direct lookup by rowid or docid.
184216** 2. Full-text search using a MATCH operator on a non-docid column.
184217** 3. Linear scan of %_content table.
184218*/
184219static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
184220 Fts3Table *p = (Fts3Table *)pVTab;
184221 int i; /* Iterator variable */
184222 int iCons = -1; /* Index of constraint to use */
184223
184224 int iLangidCons = -1; /* Index of langid=x constraint, if present */
184225 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
184226 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
184227 int iIdx;
184228
184229 if( p->bLock ){
184230 return SQLITE_ERROR;
184231 }
184232
184233 /* By default use a full table scan. This is an expensive option,
184234 ** so search through the constraints to see if a more efficient
184235 ** strategy is possible.
184236 */
184237 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
184238 pInfo->estimatedCost = 5000000;
184239 for(i=0; i<pInfo->nConstraint; i++){
184240 int bDocid; /* True if this constraint is on docid */
184241 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
184242 if( pCons->usable==0 ){
184243 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
184244 /* There exists an unusable MATCH constraint. This means that if
184245 ** the planner does elect to use the results of this call as part
184246 ** of the overall query plan the user will see an "unable to use
184247 ** function MATCH in the requested context" error. To discourage
184248 ** this, return a very high cost here. */
184249 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
184250 pInfo->estimatedCost = 1e50;
184251 fts3SetEstimatedRows(pIdxInfo: pInfo, nRow: ((sqlite3_int64)1) << 50);
184252 return SQLITE_OK;
184253 }
184254 continue;
184255 }
184256
184257 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
184258
184259 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
184260 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
184261 pInfo->idxNum = FTS3_DOCID_SEARCH;
184262 pInfo->estimatedCost = 1.0;
184263 iCons = i;
184264 }
184265
184266 /* A MATCH constraint. Use a full-text search.
184267 **
184268 ** If there is more than one MATCH constraint available, use the first
184269 ** one encountered. If there is both a MATCH constraint and a direct
184270 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
184271 ** though the rowid/docid lookup is faster than a MATCH query, selecting
184272 ** it would lead to an "unable to use function MATCH in the requested
184273 ** context" error.
184274 */
184275 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
184276 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
184277 ){
184278 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
184279 pInfo->estimatedCost = 2.0;
184280 iCons = i;
184281 }
184282
184283 /* Equality constraint on the langid column */
184284 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
184285 && pCons->iColumn==p->nColumn + 2
184286 ){
184287 iLangidCons = i;
184288 }
184289
184290 if( bDocid ){
184291 switch( pCons->op ){
184292 case SQLITE_INDEX_CONSTRAINT_GE:
184293 case SQLITE_INDEX_CONSTRAINT_GT:
184294 iDocidGe = i;
184295 break;
184296
184297 case SQLITE_INDEX_CONSTRAINT_LE:
184298 case SQLITE_INDEX_CONSTRAINT_LT:
184299 iDocidLe = i;
184300 break;
184301 }
184302 }
184303 }
184304
184305 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
184306 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pIdxInfo: pInfo);
184307
184308 iIdx = 1;
184309 if( iCons>=0 ){
184310 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
184311 pInfo->aConstraintUsage[iCons].omit = 1;
184312 }
184313 if( iLangidCons>=0 ){
184314 pInfo->idxNum |= FTS3_HAVE_LANGID;
184315 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
184316 }
184317 if( iDocidGe>=0 ){
184318 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
184319 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
184320 }
184321 if( iDocidLe>=0 ){
184322 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
184323 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
184324 }
184325
184326 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
184327 ** docid) order. Both ascending and descending are possible.
184328 */
184329 if( pInfo->nOrderBy==1 ){
184330 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
184331 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
184332 if( pOrder->desc ){
184333 pInfo->idxStr = "DESC";
184334 }else{
184335 pInfo->idxStr = "ASC";
184336 }
184337 pInfo->orderByConsumed = 1;
184338 }
184339 }
184340
184341 assert( p->pSegments==0 );
184342 return SQLITE_OK;
184343}
184344
184345/*
184346** Implementation of xOpen method.
184347*/
184348static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
184349 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
184350
184351 UNUSED_PARAMETER(pVTab);
184352
184353 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
184354 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
184355 ** if the allocation fails, return SQLITE_NOMEM.
184356 */
184357 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(n: sizeof(Fts3Cursor));
184358 if( !pCsr ){
184359 return SQLITE_NOMEM;
184360 }
184361 memset(s: pCsr, c: 0, n: sizeof(Fts3Cursor));
184362 return SQLITE_OK;
184363}
184364
184365/*
184366** Finalize the statement handle at pCsr->pStmt.
184367**
184368** Or, if that statement handle is one created by fts3CursorSeekStmt(),
184369** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
184370** pointer there instead of finalizing it.
184371*/
184372static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
184373 if( pCsr->bSeekStmt ){
184374 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
184375 if( p->pSeekStmt==0 ){
184376 p->pSeekStmt = pCsr->pStmt;
184377 sqlite3_reset(pStmt: pCsr->pStmt);
184378 pCsr->pStmt = 0;
184379 }
184380 pCsr->bSeekStmt = 0;
184381 }
184382 sqlite3_finalize(pStmt: pCsr->pStmt);
184383}
184384
184385/*
184386** Free all resources currently held by the cursor passed as the only
184387** argument.
184388*/
184389static void fts3ClearCursor(Fts3Cursor *pCsr){
184390 fts3CursorFinalizeStmt(pCsr);
184391 sqlite3Fts3FreeDeferredTokens(pCsr);
184392 sqlite3_free(p: pCsr->aDoclist);
184393 sqlite3Fts3MIBufferFree(p: pCsr->pMIBuffer);
184394 sqlite3Fts3ExprFree(pCsr->pExpr);
184395 memset(s: &(&pCsr->base)[1], c: 0, n: sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
184396}
184397
184398/*
184399** Close the cursor. For additional information see the documentation
184400** on the xClose method of the virtual table interface.
184401*/
184402static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
184403 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
184404 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
184405 fts3ClearCursor(pCsr);
184406 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
184407 sqlite3_free(p: pCsr);
184408 return SQLITE_OK;
184409}
184410
184411/*
184412** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
184413** compose and prepare an SQL statement of the form:
184414**
184415** "SELECT <columns> FROM %_content WHERE rowid = ?"
184416**
184417** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
184418** it. If an error occurs, return an SQLite error code.
184419*/
184420static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
184421 int rc = SQLITE_OK;
184422 if( pCsr->pStmt==0 ){
184423 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
184424 char *zSql;
184425 if( p->pSeekStmt ){
184426 pCsr->pStmt = p->pSeekStmt;
184427 p->pSeekStmt = 0;
184428 }else{
184429 zSql = sqlite3_mprintf(zFormat: "SELECT %s WHERE rowid = ?", p->zReadExprlist);
184430 if( !zSql ) return SQLITE_NOMEM;
184431 p->bLock++;
184432 rc = sqlite3_prepare_v3(
184433 db: p->db, zSql,nBytes: -1,SQLITE_PREPARE_PERSISTENT,ppStmt: &pCsr->pStmt,pzTail: 0
184434 );
184435 p->bLock--;
184436 sqlite3_free(p: zSql);
184437 }
184438 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
184439 }
184440 return rc;
184441}
184442
184443/*
184444** Position the pCsr->pStmt statement so that it is on the row
184445** of the %_content table that contains the last match. Return
184446** SQLITE_OK on success.
184447*/
184448static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
184449 int rc = SQLITE_OK;
184450 if( pCsr->isRequireSeek ){
184451 rc = fts3CursorSeekStmt(pCsr);
184452 if( rc==SQLITE_OK ){
184453 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
184454 pTab->bLock++;
184455 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 1, iValue: pCsr->iPrevId);
184456 pCsr->isRequireSeek = 0;
184457 if( SQLITE_ROW==sqlite3_step(pStmt: pCsr->pStmt) ){
184458 pTab->bLock--;
184459 return SQLITE_OK;
184460 }else{
184461 pTab->bLock--;
184462 rc = sqlite3_reset(pStmt: pCsr->pStmt);
184463 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
184464 /* If no row was found and no error has occurred, then the %_content
184465 ** table is missing a row that is present in the full-text index.
184466 ** The data structures are corrupt. */
184467 rc = FTS_CORRUPT_VTAB;
184468 pCsr->isEof = 1;
184469 }
184470 }
184471 }
184472 }
184473
184474 if( rc!=SQLITE_OK && pContext ){
184475 sqlite3_result_error_code(pCtx: pContext, errCode: rc);
184476 }
184477 return rc;
184478}
184479
184480/*
184481** This function is used to process a single interior node when searching
184482** a b-tree for a term or term prefix. The node data is passed to this
184483** function via the zNode/nNode parameters. The term to search for is
184484** passed in zTerm/nTerm.
184485**
184486** If piFirst is not NULL, then this function sets *piFirst to the blockid
184487** of the child node that heads the sub-tree that may contain the term.
184488**
184489** If piLast is not NULL, then *piLast is set to the right-most child node
184490** that heads a sub-tree that may contain a term for which zTerm/nTerm is
184491** a prefix.
184492**
184493** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
184494*/
184495static int fts3ScanInteriorNode(
184496 const char *zTerm, /* Term to select leaves for */
184497 int nTerm, /* Size of term zTerm in bytes */
184498 const char *zNode, /* Buffer containing segment interior node */
184499 int nNode, /* Size of buffer at zNode */
184500 sqlite3_int64 *piFirst, /* OUT: Selected child node */
184501 sqlite3_int64 *piLast /* OUT: Selected child node */
184502){
184503 int rc = SQLITE_OK; /* Return code */
184504 const char *zCsr = zNode; /* Cursor to iterate through node */
184505 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
184506 char *zBuffer = 0; /* Buffer to load terms into */
184507 i64 nAlloc = 0; /* Size of allocated buffer */
184508 int isFirstTerm = 1; /* True when processing first term on page */
184509 u64 iChild; /* Block id of child node to descend to */
184510 int nBuffer = 0; /* Total term size */
184511
184512 /* Skip over the 'height' varint that occurs at the start of every
184513 ** interior node. Then load the blockid of the left-child of the b-tree
184514 ** node into variable iChild.
184515 **
184516 ** Even if the data structure on disk is corrupted, this (reading two
184517 ** varints from the buffer) does not risk an overread. If zNode is a
184518 ** root node, then the buffer comes from a SELECT statement. SQLite does
184519 ** not make this guarantee explicitly, but in practice there are always
184520 ** either more than 20 bytes of allocated space following the nNode bytes of
184521 ** contents, or two zero bytes. Or, if the node is read from the %_segments
184522 ** table, then there are always 20 bytes of zeroed padding following the
184523 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
184524 */
184525 zCsr += sqlite3Fts3GetVarintU(pBuf: zCsr, v: &iChild);
184526 zCsr += sqlite3Fts3GetVarintU(pBuf: zCsr, v: &iChild);
184527 if( zCsr>zEnd ){
184528 return FTS_CORRUPT_VTAB;
184529 }
184530
184531 while( zCsr<zEnd && (piFirst || piLast) ){
184532 int cmp; /* memcmp() result */
184533 int nSuffix; /* Size of term suffix */
184534 int nPrefix = 0; /* Size of term prefix */
184535
184536 /* Load the next term on the node into zBuffer. Use realloc() to expand
184537 ** the size of zBuffer if required. */
184538 if( !isFirstTerm ){
184539 zCsr += fts3GetVarint32(zCsr, &nPrefix);
184540 if( nPrefix>nBuffer ){
184541 rc = FTS_CORRUPT_VTAB;
184542 goto finish_scan;
184543 }
184544 }
184545 isFirstTerm = 0;
184546 zCsr += fts3GetVarint32(zCsr, &nSuffix);
184547
184548 assert( nPrefix>=0 && nSuffix>=0 );
184549 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
184550 rc = FTS_CORRUPT_VTAB;
184551 goto finish_scan;
184552 }
184553 if( (i64)nPrefix+nSuffix>nAlloc ){
184554 char *zNew;
184555 nAlloc = ((i64)nPrefix+nSuffix) * 2;
184556 zNew = (char *)sqlite3_realloc64(pOld: zBuffer, n: nAlloc);
184557 if( !zNew ){
184558 rc = SQLITE_NOMEM;
184559 goto finish_scan;
184560 }
184561 zBuffer = zNew;
184562 }
184563 assert( zBuffer );
184564 memcpy(dest: &zBuffer[nPrefix], src: zCsr, n: nSuffix);
184565 nBuffer = nPrefix + nSuffix;
184566 zCsr += nSuffix;
184567
184568 /* Compare the term we are searching for with the term just loaded from
184569 ** the interior node. If the specified term is greater than or equal
184570 ** to the term from the interior node, then all terms on the sub-tree
184571 ** headed by node iChild are smaller than zTerm. No need to search
184572 ** iChild.
184573 **
184574 ** If the interior node term is larger than the specified term, then
184575 ** the tree headed by iChild may contain the specified term.
184576 */
184577 cmp = memcmp(s1: zTerm, s2: zBuffer, n: (nBuffer>nTerm ? nTerm : nBuffer));
184578 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
184579 *piFirst = (i64)iChild;
184580 piFirst = 0;
184581 }
184582
184583 if( piLast && cmp<0 ){
184584 *piLast = (i64)iChild;
184585 piLast = 0;
184586 }
184587
184588 iChild++;
184589 };
184590
184591 if( piFirst ) *piFirst = (i64)iChild;
184592 if( piLast ) *piLast = (i64)iChild;
184593
184594 finish_scan:
184595 sqlite3_free(p: zBuffer);
184596 return rc;
184597}
184598
184599
184600/*
184601** The buffer pointed to by argument zNode (size nNode bytes) contains an
184602** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
184603** contains a term. This function searches the sub-tree headed by the zNode
184604** node for the range of leaf nodes that may contain the specified term
184605** or terms for which the specified term is a prefix.
184606**
184607** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
184608** left-most leaf node in the tree that may contain the specified term.
184609** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
184610** right-most leaf node that may contain a term for which the specified
184611** term is a prefix.
184612**
184613** It is possible that the range of returned leaf nodes does not contain
184614** the specified term or any terms for which it is a prefix. However, if the
184615** segment does contain any such terms, they are stored within the identified
184616** range. Because this function only inspects interior segment nodes (and
184617** never loads leaf nodes into memory), it is not possible to be sure.
184618**
184619** If an error occurs, an error code other than SQLITE_OK is returned.
184620*/
184621static int fts3SelectLeaf(
184622 Fts3Table *p, /* Virtual table handle */
184623 const char *zTerm, /* Term to select leaves for */
184624 int nTerm, /* Size of term zTerm in bytes */
184625 const char *zNode, /* Buffer containing segment interior node */
184626 int nNode, /* Size of buffer at zNode */
184627 sqlite3_int64 *piLeaf, /* Selected leaf node */
184628 sqlite3_int64 *piLeaf2 /* Selected leaf node */
184629){
184630 int rc = SQLITE_OK; /* Return code */
184631 int iHeight; /* Height of this node in tree */
184632
184633 assert( piLeaf || piLeaf2 );
184634
184635 fts3GetVarint32(zNode, &iHeight);
184636 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piFirst: piLeaf, piLast: piLeaf2);
184637 assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
184638
184639 if( rc==SQLITE_OK && iHeight>1 ){
184640 char *zBlob = 0; /* Blob read from %_segments table */
184641 int nBlob = 0; /* Size of zBlob in bytes */
184642
184643 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
184644 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
184645 if( rc==SQLITE_OK ){
184646 rc = fts3SelectLeaf(p, zTerm, nTerm, zNode: zBlob, nNode: nBlob, piLeaf, piLeaf2: 0);
184647 }
184648 sqlite3_free(p: zBlob);
184649 piLeaf = 0;
184650 zBlob = 0;
184651 }
184652
184653 if( rc==SQLITE_OK ){
184654 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
184655 }
184656 if( rc==SQLITE_OK ){
184657 int iNewHeight = 0;
184658 fts3GetVarint32(zBlob, &iNewHeight);
184659 if( iNewHeight>=iHeight ){
184660 rc = FTS_CORRUPT_VTAB;
184661 }else{
184662 rc = fts3SelectLeaf(p, zTerm, nTerm, zNode: zBlob, nNode: nBlob, piLeaf, piLeaf2);
184663 }
184664 }
184665 sqlite3_free(p: zBlob);
184666 }
184667
184668 return rc;
184669}
184670
184671/*
184672** This function is used to create delta-encoded serialized lists of FTS3
184673** varints. Each call to this function appends a single varint to a list.
184674*/
184675static void fts3PutDeltaVarint(
184676 char **pp, /* IN/OUT: Output pointer */
184677 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
184678 sqlite3_int64 iVal /* Write this value to the list */
184679){
184680 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
184681 *pp += sqlite3Fts3PutVarint(p: *pp, v: iVal-*piPrev);
184682 *piPrev = iVal;
184683}
184684
184685/*
184686** When this function is called, *ppPoslist is assumed to point to the
184687** start of a position-list. After it returns, *ppPoslist points to the
184688** first byte after the position-list.
184689**
184690** A position list is list of positions (delta encoded) and columns for
184691** a single document record of a doclist. So, in other words, this
184692** routine advances *ppPoslist so that it points to the next docid in
184693** the doclist, or to the first byte past the end of the doclist.
184694**
184695** If pp is not NULL, then the contents of the position list are copied
184696** to *pp. *pp is set to point to the first byte past the last byte copied
184697** before this function returns.
184698*/
184699static void fts3PoslistCopy(char **pp, char **ppPoslist){
184700 char *pEnd = *ppPoslist;
184701 char c = 0;
184702
184703 /* The end of a position list is marked by a zero encoded as an FTS3
184704 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
184705 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
184706 ** of some other, multi-byte, value.
184707 **
184708 ** The following while-loop moves pEnd to point to the first byte that is not
184709 ** immediately preceded by a byte with the 0x80 bit set. Then increments
184710 ** pEnd once more so that it points to the byte immediately following the
184711 ** last byte in the position-list.
184712 */
184713 while( *pEnd | c ){
184714 c = *pEnd++ & 0x80;
184715 testcase( c!=0 && (*pEnd)==0 );
184716 }
184717 pEnd++; /* Advance past the POS_END terminator byte */
184718
184719 if( pp ){
184720 int n = (int)(pEnd - *ppPoslist);
184721 char *p = *pp;
184722 memcpy(dest: p, src: *ppPoslist, n: n);
184723 p += n;
184724 *pp = p;
184725 }
184726 *ppPoslist = pEnd;
184727}
184728
184729/*
184730** When this function is called, *ppPoslist is assumed to point to the
184731** start of a column-list. After it returns, *ppPoslist points to the
184732** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
184733**
184734** A column-list is list of delta-encoded positions for a single column
184735** within a single document within a doclist.
184736**
184737** The column-list is terminated either by a POS_COLUMN varint (1) or
184738** a POS_END varint (0). This routine leaves *ppPoslist pointing to
184739** the POS_COLUMN or POS_END that terminates the column-list.
184740**
184741** If pp is not NULL, then the contents of the column-list are copied
184742** to *pp. *pp is set to point to the first byte past the last byte copied
184743** before this function returns. The POS_COLUMN or POS_END terminator
184744** is not copied into *pp.
184745*/
184746static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
184747 char *pEnd = *ppPoslist;
184748 char c = 0;
184749
184750 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
184751 ** not part of a multi-byte varint.
184752 */
184753 while( 0xFE & (*pEnd | c) ){
184754 c = *pEnd++ & 0x80;
184755 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
184756 }
184757 if( pp ){
184758 int n = (int)(pEnd - *ppPoslist);
184759 char *p = *pp;
184760 memcpy(dest: p, src: *ppPoslist, n: n);
184761 p += n;
184762 *pp = p;
184763 }
184764 *ppPoslist = pEnd;
184765}
184766
184767/*
184768** Value used to signify the end of an position-list. This must be
184769** as large or larger than any value that might appear on the
184770** position-list, even a position list that has been corrupted.
184771*/
184772#define POSITION_LIST_END LARGEST_INT64
184773
184774/*
184775** This function is used to help parse position-lists. When this function is
184776** called, *pp may point to the start of the next varint in the position-list
184777** being parsed, or it may point to 1 byte past the end of the position-list
184778** (in which case **pp will be a terminator bytes POS_END (0) or
184779** (1)).
184780**
184781** If *pp points past the end of the current position-list, set *pi to
184782** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
184783** increment the current value of *pi by the value read, and set *pp to
184784** point to the next value before returning.
184785**
184786** Before calling this routine *pi must be initialized to the value of
184787** the previous position, or zero if we are reading the first position
184788** in the position-list. Because positions are delta-encoded, the value
184789** of the previous position is needed in order to compute the value of
184790** the next position.
184791*/
184792static void fts3ReadNextPos(
184793 char **pp, /* IN/OUT: Pointer into position-list buffer */
184794 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
184795){
184796 if( (**pp)&0xFE ){
184797 int iVal;
184798 *pp += fts3GetVarint32((*pp), &iVal);
184799 *pi += iVal;
184800 *pi -= 2;
184801 }else{
184802 *pi = POSITION_LIST_END;
184803 }
184804}
184805
184806/*
184807** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
184808** the value of iCol encoded as a varint to *pp. This will start a new
184809** column list.
184810**
184811** Set *pp to point to the byte just after the last byte written before
184812** returning (do not modify it if iCol==0). Return the total number of bytes
184813** written (0 if iCol==0).
184814*/
184815static int fts3PutColNumber(char **pp, int iCol){
184816 int n = 0; /* Number of bytes written */
184817 if( iCol ){
184818 char *p = *pp; /* Output pointer */
184819 n = 1 + sqlite3Fts3PutVarint(p: &p[1], v: iCol);
184820 *p = 0x01;
184821 *pp = &p[n];
184822 }
184823 return n;
184824}
184825
184826/*
184827** Compute the union of two position lists. The output written
184828** into *pp contains all positions of both *pp1 and *pp2 in sorted
184829** order and with any duplicates removed. All pointers are
184830** updated appropriately. The caller is responsible for insuring
184831** that there is enough space in *pp to hold the complete output.
184832*/
184833static int fts3PoslistMerge(
184834 char **pp, /* Output buffer */
184835 char **pp1, /* Left input list */
184836 char **pp2 /* Right input list */
184837){
184838 char *p = *pp;
184839 char *p1 = *pp1;
184840 char *p2 = *pp2;
184841
184842 while( *p1 || *p2 ){
184843 int iCol1; /* The current column index in pp1 */
184844 int iCol2; /* The current column index in pp2 */
184845
184846 if( *p1==POS_COLUMN ){
184847 fts3GetVarint32(&p1[1], &iCol1);
184848 if( iCol1==0 ) return FTS_CORRUPT_VTAB;
184849 }
184850 else if( *p1==POS_END ) iCol1 = 0x7fffffff;
184851 else iCol1 = 0;
184852
184853 if( *p2==POS_COLUMN ){
184854 fts3GetVarint32(&p2[1], &iCol2);
184855 if( iCol2==0 ) return FTS_CORRUPT_VTAB;
184856 }
184857 else if( *p2==POS_END ) iCol2 = 0x7fffffff;
184858 else iCol2 = 0;
184859
184860 if( iCol1==iCol2 ){
184861 sqlite3_int64 i1 = 0; /* Last position from pp1 */
184862 sqlite3_int64 i2 = 0; /* Last position from pp2 */
184863 sqlite3_int64 iPrev = 0;
184864 int n = fts3PutColNumber(pp: &p, iCol: iCol1);
184865 p1 += n;
184866 p2 += n;
184867
184868 /* At this point, both p1 and p2 point to the start of column-lists
184869 ** for the same column (the column with index iCol1 and iCol2).
184870 ** A column-list is a list of non-negative delta-encoded varints, each
184871 ** incremented by 2 before being stored. Each list is terminated by a
184872 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
184873 ** and writes the results to buffer p. p is left pointing to the byte
184874 ** after the list written. No terminator (POS_END or POS_COLUMN) is
184875 ** written to the output.
184876 */
184877 fts3GetDeltaVarint(pp: &p1, pVal: &i1);
184878 fts3GetDeltaVarint(pp: &p2, pVal: &i2);
184879 if( i1<2 || i2<2 ){
184880 break;
184881 }
184882 do {
184883 fts3PutDeltaVarint(pp: &p, piPrev: &iPrev, iVal: (i1<i2) ? i1 : i2);
184884 iPrev -= 2;
184885 if( i1==i2 ){
184886 fts3ReadNextPos(pp: &p1, pi: &i1);
184887 fts3ReadNextPos(pp: &p2, pi: &i2);
184888 }else if( i1<i2 ){
184889 fts3ReadNextPos(pp: &p1, pi: &i1);
184890 }else{
184891 fts3ReadNextPos(pp: &p2, pi: &i2);
184892 }
184893 }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
184894 }else if( iCol1<iCol2 ){
184895 p1 += fts3PutColNumber(pp: &p, iCol: iCol1);
184896 fts3ColumnlistCopy(pp: &p, ppPoslist: &p1);
184897 }else{
184898 p2 += fts3PutColNumber(pp: &p, iCol: iCol2);
184899 fts3ColumnlistCopy(pp: &p, ppPoslist: &p2);
184900 }
184901 }
184902
184903 *p++ = POS_END;
184904 *pp = p;
184905 *pp1 = p1 + 1;
184906 *pp2 = p2 + 1;
184907 return SQLITE_OK;
184908}
184909
184910/*
184911** This function is used to merge two position lists into one. When it is
184912** called, *pp1 and *pp2 must both point to position lists. A position-list is
184913** the part of a doclist that follows each document id. For example, if a row
184914** contains:
184915**
184916** 'a b c'|'x y z'|'a b b a'
184917**
184918** Then the position list for this row for token 'b' would consist of:
184919**
184920** 0x02 0x01 0x02 0x03 0x03 0x00
184921**
184922** When this function returns, both *pp1 and *pp2 are left pointing to the
184923** byte following the 0x00 terminator of their respective position lists.
184924**
184925** If isSaveLeft is 0, an entry is added to the output position list for
184926** each position in *pp2 for which there exists one or more positions in
184927** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
184928** when the *pp1 token appears before the *pp2 token, but not more than nToken
184929** slots before it.
184930**
184931** e.g. nToken==1 searches for adjacent positions.
184932*/
184933static int fts3PoslistPhraseMerge(
184934 char **pp, /* IN/OUT: Preallocated output buffer */
184935 int nToken, /* Maximum difference in token positions */
184936 int isSaveLeft, /* Save the left position */
184937 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
184938 char **pp1, /* IN/OUT: Left input list */
184939 char **pp2 /* IN/OUT: Right input list */
184940){
184941 char *p = *pp;
184942 char *p1 = *pp1;
184943 char *p2 = *pp2;
184944 int iCol1 = 0;
184945 int iCol2 = 0;
184946
184947 /* Never set both isSaveLeft and isExact for the same invocation. */
184948 assert( isSaveLeft==0 || isExact==0 );
184949
184950 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
184951 if( *p1==POS_COLUMN ){
184952 p1++;
184953 p1 += fts3GetVarint32(p1, &iCol1);
184954 }
184955 if( *p2==POS_COLUMN ){
184956 p2++;
184957 p2 += fts3GetVarint32(p2, &iCol2);
184958 }
184959
184960 while( 1 ){
184961 if( iCol1==iCol2 ){
184962 char *pSave = p;
184963 sqlite3_int64 iPrev = 0;
184964 sqlite3_int64 iPos1 = 0;
184965 sqlite3_int64 iPos2 = 0;
184966
184967 if( iCol1 ){
184968 *p++ = POS_COLUMN;
184969 p += sqlite3Fts3PutVarint(p, v: iCol1);
184970 }
184971
184972 fts3GetDeltaVarint(pp: &p1, pVal: &iPos1); iPos1 -= 2;
184973 fts3GetDeltaVarint(pp: &p2, pVal: &iPos2); iPos2 -= 2;
184974 if( iPos1<0 || iPos2<0 ) break;
184975
184976 while( 1 ){
184977 if( iPos2==iPos1+nToken
184978 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
184979 ){
184980 sqlite3_int64 iSave;
184981 iSave = isSaveLeft ? iPos1 : iPos2;
184982 fts3PutDeltaVarint(pp: &p, piPrev: &iPrev, iVal: iSave+2); iPrev -= 2;
184983 pSave = 0;
184984 assert( p );
184985 }
184986 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
184987 if( (*p2&0xFE)==0 ) break;
184988 fts3GetDeltaVarint(pp: &p2, pVal: &iPos2); iPos2 -= 2;
184989 }else{
184990 if( (*p1&0xFE)==0 ) break;
184991 fts3GetDeltaVarint(pp: &p1, pVal: &iPos1); iPos1 -= 2;
184992 }
184993 }
184994
184995 if( pSave ){
184996 assert( pp && p );
184997 p = pSave;
184998 }
184999
185000 fts3ColumnlistCopy(pp: 0, ppPoslist: &p1);
185001 fts3ColumnlistCopy(pp: 0, ppPoslist: &p2);
185002 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
185003 if( 0==*p1 || 0==*p2 ) break;
185004
185005 p1++;
185006 p1 += fts3GetVarint32(p1, &iCol1);
185007 p2++;
185008 p2 += fts3GetVarint32(p2, &iCol2);
185009 }
185010
185011 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
185012 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
185013 ** end of the position list, or the 0x01 that precedes the next
185014 ** column-number in the position list.
185015 */
185016 else if( iCol1<iCol2 ){
185017 fts3ColumnlistCopy(pp: 0, ppPoslist: &p1);
185018 if( 0==*p1 ) break;
185019 p1++;
185020 p1 += fts3GetVarint32(p1, &iCol1);
185021 }else{
185022 fts3ColumnlistCopy(pp: 0, ppPoslist: &p2);
185023 if( 0==*p2 ) break;
185024 p2++;
185025 p2 += fts3GetVarint32(p2, &iCol2);
185026 }
185027 }
185028
185029 fts3PoslistCopy(pp: 0, ppPoslist: &p2);
185030 fts3PoslistCopy(pp: 0, ppPoslist: &p1);
185031 *pp1 = p1;
185032 *pp2 = p2;
185033 if( *pp==p ){
185034 return 0;
185035 }
185036 *p++ = 0x00;
185037 *pp = p;
185038 return 1;
185039}
185040
185041/*
185042** Merge two position-lists as required by the NEAR operator. The argument
185043** position lists correspond to the left and right phrases of an expression
185044** like:
185045**
185046** "phrase 1" NEAR "phrase number 2"
185047**
185048** Position list *pp1 corresponds to the left-hand side of the NEAR
185049** expression and *pp2 to the right. As usual, the indexes in the position
185050** lists are the offsets of the last token in each phrase (tokens "1" and "2"
185051** in the example above).
185052**
185053** The output position list - written to *pp - is a copy of *pp2 with those
185054** entries that are not sufficiently NEAR entries in *pp1 removed.
185055*/
185056static int fts3PoslistNearMerge(
185057 char **pp, /* Output buffer */
185058 char *aTmp, /* Temporary buffer space */
185059 int nRight, /* Maximum difference in token positions */
185060 int nLeft, /* Maximum difference in token positions */
185061 char **pp1, /* IN/OUT: Left input list */
185062 char **pp2 /* IN/OUT: Right input list */
185063){
185064 char *p1 = *pp1;
185065 char *p2 = *pp2;
185066
185067 char *pTmp1 = aTmp;
185068 char *pTmp2;
185069 char *aTmp2;
185070 int res = 1;
185071
185072 fts3PoslistPhraseMerge(pp: &pTmp1, nToken: nRight, isSaveLeft: 0, isExact: 0, pp1, pp2);
185073 aTmp2 = pTmp2 = pTmp1;
185074 *pp1 = p1;
185075 *pp2 = p2;
185076 fts3PoslistPhraseMerge(pp: &pTmp2, nToken: nLeft, isSaveLeft: 1, isExact: 0, pp1: pp2, pp2: pp1);
185077 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
185078 fts3PoslistMerge(pp, pp1: &aTmp, pp2: &aTmp2);
185079 }else if( pTmp1!=aTmp ){
185080 fts3PoslistCopy(pp, ppPoslist: &aTmp);
185081 }else if( pTmp2!=aTmp2 ){
185082 fts3PoslistCopy(pp, ppPoslist: &aTmp2);
185083 }else{
185084 res = 0;
185085 }
185086
185087 return res;
185088}
185089
185090/*
185091** An instance of this function is used to merge together the (potentially
185092** large number of) doclists for each term that matches a prefix query.
185093** See function fts3TermSelectMerge() for details.
185094*/
185095typedef struct TermSelect TermSelect;
185096struct TermSelect {
185097 char *aaOutput[16]; /* Malloc'd output buffers */
185098 int anOutput[16]; /* Size each output buffer in bytes */
185099};
185100
185101/*
185102** This function is used to read a single varint from a buffer. Parameter
185103** pEnd points 1 byte past the end of the buffer. When this function is
185104** called, if *pp points to pEnd or greater, then the end of the buffer
185105** has been reached. In this case *pp is set to 0 and the function returns.
185106**
185107** If *pp does not point to or past pEnd, then a single varint is read
185108** from *pp. *pp is then set to point 1 byte past the end of the read varint.
185109**
185110** If bDescIdx is false, the value read is added to *pVal before returning.
185111** If it is true, the value read is subtracted from *pVal before this
185112** function returns.
185113*/
185114static void fts3GetDeltaVarint3(
185115 char **pp, /* IN/OUT: Point to read varint from */
185116 char *pEnd, /* End of buffer */
185117 int bDescIdx, /* True if docids are descending */
185118 sqlite3_int64 *pVal /* IN/OUT: Integer value */
185119){
185120 if( *pp>=pEnd ){
185121 *pp = 0;
185122 }else{
185123 u64 iVal;
185124 *pp += sqlite3Fts3GetVarintU(pBuf: *pp, v: &iVal);
185125 if( bDescIdx ){
185126 *pVal = (i64)((u64)*pVal - iVal);
185127 }else{
185128 *pVal = (i64)((u64)*pVal + iVal);
185129 }
185130 }
185131}
185132
185133/*
185134** This function is used to write a single varint to a buffer. The varint
185135** is written to *pp. Before returning, *pp is set to point 1 byte past the
185136** end of the value written.
185137**
185138** If *pbFirst is zero when this function is called, the value written to
185139** the buffer is that of parameter iVal.
185140**
185141** If *pbFirst is non-zero when this function is called, then the value
185142** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
185143** (if bDescIdx is non-zero).
185144**
185145** Before returning, this function always sets *pbFirst to 1 and *piPrev
185146** to the value of parameter iVal.
185147*/
185148static void fts3PutDeltaVarint3(
185149 char **pp, /* IN/OUT: Output pointer */
185150 int bDescIdx, /* True for descending docids */
185151 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
185152 int *pbFirst, /* IN/OUT: True after first int written */
185153 sqlite3_int64 iVal /* Write this value to the list */
185154){
185155 sqlite3_uint64 iWrite;
185156 if( bDescIdx==0 || *pbFirst==0 ){
185157 assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev );
185158 iWrite = (u64)iVal - (u64)*piPrev;
185159 }else{
185160 assert_fts3_nc( *piPrev>=iVal );
185161 iWrite = (u64)*piPrev - (u64)iVal;
185162 }
185163 assert( *pbFirst || *piPrev==0 );
185164 assert_fts3_nc( *pbFirst==0 || iWrite>0 );
185165 *pp += sqlite3Fts3PutVarint(p: *pp, v: iWrite);
185166 *piPrev = iVal;
185167 *pbFirst = 1;
185168}
185169
185170
185171/*
185172** This macro is used by various functions that merge doclists. The two
185173** arguments are 64-bit docid values. If the value of the stack variable
185174** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
185175** Otherwise, (i2-i1).
185176**
185177** Using this makes it easier to write code that can merge doclists that are
185178** sorted in either ascending or descending order.
185179*/
185180/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
185181#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
185182
185183/*
185184** This function does an "OR" merge of two doclists (output contains all
185185** positions contained in either argument doclist). If the docids in the
185186** input doclists are sorted in ascending order, parameter bDescDoclist
185187** should be false. If they are sorted in ascending order, it should be
185188** passed a non-zero value.
185189**
185190** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
185191** containing the output doclist and SQLITE_OK is returned. In this case
185192** *pnOut is set to the number of bytes in the output doclist.
185193**
185194** If an error occurs, an SQLite error code is returned. The output values
185195** are undefined in this case.
185196*/
185197static int fts3DoclistOrMerge(
185198 int bDescDoclist, /* True if arguments are desc */
185199 char *a1, int n1, /* First doclist */
185200 char *a2, int n2, /* Second doclist */
185201 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
185202){
185203 int rc = SQLITE_OK;
185204 sqlite3_int64 i1 = 0;
185205 sqlite3_int64 i2 = 0;
185206 sqlite3_int64 iPrev = 0;
185207 char *pEnd1 = &a1[n1];
185208 char *pEnd2 = &a2[n2];
185209 char *p1 = a1;
185210 char *p2 = a2;
185211 char *p;
185212 char *aOut;
185213 int bFirstOut = 0;
185214
185215 *paOut = 0;
185216 *pnOut = 0;
185217
185218 /* Allocate space for the output. Both the input and output doclists
185219 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
185220 ** then the first docid in each list is simply encoded as a varint. For
185221 ** each subsequent docid, the varint stored is the difference between the
185222 ** current and previous docid (a positive number - since the list is in
185223 ** ascending order).
185224 **
185225 ** The first docid written to the output is therefore encoded using the
185226 ** same number of bytes as it is in whichever of the input lists it is
185227 ** read from. And each subsequent docid read from the same input list
185228 ** consumes either the same or less bytes as it did in the input (since
185229 ** the difference between it and the previous value in the output must
185230 ** be a positive value less than or equal to the delta value read from
185231 ** the input list). The same argument applies to all but the first docid
185232 ** read from the 'other' list. And to the contents of all position lists
185233 ** that will be copied and merged from the input to the output.
185234 **
185235 ** However, if the first docid copied to the output is a negative number,
185236 ** then the encoding of the first docid from the 'other' input list may
185237 ** be larger in the output than it was in the input (since the delta value
185238 ** may be a larger positive integer than the actual docid).
185239 **
185240 ** The space required to store the output is therefore the sum of the
185241 ** sizes of the two inputs, plus enough space for exactly one of the input
185242 ** docids to grow.
185243 **
185244 ** A symetric argument may be made if the doclists are in descending
185245 ** order.
185246 */
185247 aOut = sqlite3_malloc64(n: (i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
185248 if( !aOut ) return SQLITE_NOMEM;
185249
185250 p = aOut;
185251 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: 0, pVal: &i1);
185252 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: 0, pVal: &i2);
185253 while( p1 || p2 ){
185254 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
185255
185256 if( p2 && p1 && iDiff==0 ){
185257 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i1);
185258 rc = fts3PoslistMerge(pp: &p, pp1: &p1, pp2: &p2);
185259 if( rc ) break;
185260 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
185261 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
185262 }else if( !p2 || (p1 && iDiff<0) ){
185263 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i1);
185264 fts3PoslistCopy(pp: &p, ppPoslist: &p1);
185265 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
185266 }else{
185267 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i2);
185268 fts3PoslistCopy(pp: &p, ppPoslist: &p2);
185269 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
185270 }
185271
185272 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
185273 }
185274
185275 if( rc!=SQLITE_OK ){
185276 sqlite3_free(p: aOut);
185277 p = aOut = 0;
185278 }else{
185279 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
185280 memset(s: &aOut[(p-aOut)], c: 0, FTS3_BUFFER_PADDING);
185281 }
185282 *paOut = aOut;
185283 *pnOut = (int)(p-aOut);
185284 return rc;
185285}
185286
185287/*
185288** This function does a "phrase" merge of two doclists. In a phrase merge,
185289** the output contains a copy of each position from the right-hand input
185290** doclist for which there is a position in the left-hand input doclist
185291** exactly nDist tokens before it.
185292**
185293** If the docids in the input doclists are sorted in ascending order,
185294** parameter bDescDoclist should be false. If they are sorted in ascending
185295** order, it should be passed a non-zero value.
185296**
185297** The right-hand input doclist is overwritten by this function.
185298*/
185299static int fts3DoclistPhraseMerge(
185300 int bDescDoclist, /* True if arguments are desc */
185301 int nDist, /* Distance from left to right (1=adjacent) */
185302 char *aLeft, int nLeft, /* Left doclist */
185303 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
185304){
185305 sqlite3_int64 i1 = 0;
185306 sqlite3_int64 i2 = 0;
185307 sqlite3_int64 iPrev = 0;
185308 char *aRight = *paRight;
185309 char *pEnd1 = &aLeft[nLeft];
185310 char *pEnd2 = &aRight[*pnRight];
185311 char *p1 = aLeft;
185312 char *p2 = aRight;
185313 char *p;
185314 int bFirstOut = 0;
185315 char *aOut;
185316
185317 assert( nDist>0 );
185318 if( bDescDoclist ){
185319 aOut = sqlite3_malloc64(n: (sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
185320 if( aOut==0 ) return SQLITE_NOMEM;
185321 }else{
185322 aOut = aRight;
185323 }
185324 p = aOut;
185325
185326 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: 0, pVal: &i1);
185327 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: 0, pVal: &i2);
185328
185329 while( p1 && p2 ){
185330 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
185331 if( iDiff==0 ){
185332 char *pSave = p;
185333 sqlite3_int64 iPrevSave = iPrev;
185334 int bFirstOutSave = bFirstOut;
185335
185336 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i1);
185337 if( 0==fts3PoslistPhraseMerge(pp: &p, nToken: nDist, isSaveLeft: 0, isExact: 1, pp1: &p1, pp2: &p2) ){
185338 p = pSave;
185339 iPrev = iPrevSave;
185340 bFirstOut = bFirstOutSave;
185341 }
185342 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
185343 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
185344 }else if( iDiff<0 ){
185345 fts3PoslistCopy(pp: 0, ppPoslist: &p1);
185346 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
185347 }else{
185348 fts3PoslistCopy(pp: 0, ppPoslist: &p2);
185349 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
185350 }
185351 }
185352
185353 *pnRight = (int)(p - aOut);
185354 if( bDescDoclist ){
185355 sqlite3_free(p: aRight);
185356 *paRight = aOut;
185357 }
185358
185359 return SQLITE_OK;
185360}
185361
185362/*
185363** Argument pList points to a position list nList bytes in size. This
185364** function checks to see if the position list contains any entries for
185365** a token in position 0 (of any column). If so, it writes argument iDelta
185366** to the output buffer pOut, followed by a position list consisting only
185367** of the entries from pList at position 0, and terminated by an 0x00 byte.
185368** The value returned is the number of bytes written to pOut (if any).
185369*/
185370SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
185371 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
185372 char *pList, /* Position list (no 0x00 term) */
185373 int nList, /* Size of pList in bytes */
185374 char *pOut /* Write output here */
185375){
185376 int nOut = 0;
185377 int bWritten = 0; /* True once iDelta has been written */
185378 char *p = pList;
185379 char *pEnd = &pList[nList];
185380
185381 if( *p!=0x01 ){
185382 if( *p==0x02 ){
185383 nOut += sqlite3Fts3PutVarint(p: &pOut[nOut], v: iDelta);
185384 pOut[nOut++] = 0x02;
185385 bWritten = 1;
185386 }
185387 fts3ColumnlistCopy(pp: 0, ppPoslist: &p);
185388 }
185389
185390 while( p<pEnd ){
185391 sqlite3_int64 iCol;
185392 p++;
185393 p += sqlite3Fts3GetVarint(pBuf: p, v: &iCol);
185394 if( *p==0x02 ){
185395 if( bWritten==0 ){
185396 nOut += sqlite3Fts3PutVarint(p: &pOut[nOut], v: iDelta);
185397 bWritten = 1;
185398 }
185399 pOut[nOut++] = 0x01;
185400 nOut += sqlite3Fts3PutVarint(p: &pOut[nOut], v: iCol);
185401 pOut[nOut++] = 0x02;
185402 }
185403 fts3ColumnlistCopy(pp: 0, ppPoslist: &p);
185404 }
185405 if( bWritten ){
185406 pOut[nOut++] = 0x00;
185407 }
185408
185409 return nOut;
185410}
185411
185412
185413/*
185414** Merge all doclists in the TermSelect.aaOutput[] array into a single
185415** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
185416** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
185417**
185418** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
185419** the responsibility of the caller to free any doclists left in the
185420** TermSelect.aaOutput[] array.
185421*/
185422static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
185423 char *aOut = 0;
185424 int nOut = 0;
185425 int i;
185426
185427 /* Loop through the doclists in the aaOutput[] array. Merge them all
185428 ** into a single doclist.
185429 */
185430 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
185431 if( pTS->aaOutput[i] ){
185432 if( !aOut ){
185433 aOut = pTS->aaOutput[i];
185434 nOut = pTS->anOutput[i];
185435 pTS->aaOutput[i] = 0;
185436 }else{
185437 int nNew;
185438 char *aNew;
185439
185440 int rc = fts3DoclistOrMerge(bDescDoclist: p->bDescIdx,
185441 a1: pTS->aaOutput[i], n1: pTS->anOutput[i], a2: aOut, n2: nOut, paOut: &aNew, pnOut: &nNew
185442 );
185443 if( rc!=SQLITE_OK ){
185444 sqlite3_free(p: aOut);
185445 return rc;
185446 }
185447
185448 sqlite3_free(p: pTS->aaOutput[i]);
185449 sqlite3_free(p: aOut);
185450 pTS->aaOutput[i] = 0;
185451 aOut = aNew;
185452 nOut = nNew;
185453 }
185454 }
185455 }
185456
185457 pTS->aaOutput[0] = aOut;
185458 pTS->anOutput[0] = nOut;
185459 return SQLITE_OK;
185460}
185461
185462/*
185463** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
185464** as the first argument. The merge is an "OR" merge (see function
185465** fts3DoclistOrMerge() for details).
185466**
185467** This function is called with the doclist for each term that matches
185468** a queried prefix. It merges all these doclists into one, the doclist
185469** for the specified prefix. Since there can be a very large number of
185470** doclists to merge, the merging is done pair-wise using the TermSelect
185471** object.
185472**
185473** This function returns SQLITE_OK if the merge is successful, or an
185474** SQLite error code (SQLITE_NOMEM) if an error occurs.
185475*/
185476static int fts3TermSelectMerge(
185477 Fts3Table *p, /* FTS table handle */
185478 TermSelect *pTS, /* TermSelect object to merge into */
185479 char *aDoclist, /* Pointer to doclist */
185480 int nDoclist /* Size of aDoclist in bytes */
185481){
185482 if( pTS->aaOutput[0]==0 ){
185483 /* If this is the first term selected, copy the doclist to the output
185484 ** buffer using memcpy().
185485 **
185486 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
185487 ** allocation. This is so as to ensure that the buffer is big enough
185488 ** to hold the current doclist AND'd with any other doclist. If the
185489 ** doclists are stored in order=ASC order, this padding would not be
185490 ** required (since the size of [doclistA AND doclistB] is always less
185491 ** than or equal to the size of [doclistA] in that case). But this is
185492 ** not true for order=DESC. For example, a doclist containing (1, -1)
185493 ** may be smaller than (-1), as in the first example the -1 may be stored
185494 ** as a single-byte delta, whereas in the second it must be stored as a
185495 ** FTS3_VARINT_MAX byte varint.
185496 **
185497 ** Similar padding is added in the fts3DoclistOrMerge() function.
185498 */
185499 pTS->aaOutput[0] = sqlite3_malloc64(n: (i64)nDoclist + FTS3_VARINT_MAX + 1);
185500 pTS->anOutput[0] = nDoclist;
185501 if( pTS->aaOutput[0] ){
185502 memcpy(dest: pTS->aaOutput[0], src: aDoclist, n: nDoclist);
185503 memset(s: &pTS->aaOutput[0][nDoclist], c: 0, FTS3_VARINT_MAX);
185504 }else{
185505 return SQLITE_NOMEM;
185506 }
185507 }else{
185508 char *aMerge = aDoclist;
185509 int nMerge = nDoclist;
185510 int iOut;
185511
185512 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
185513 if( pTS->aaOutput[iOut]==0 ){
185514 assert( iOut>0 );
185515 pTS->aaOutput[iOut] = aMerge;
185516 pTS->anOutput[iOut] = nMerge;
185517 break;
185518 }else{
185519 char *aNew;
185520 int nNew;
185521
185522 int rc = fts3DoclistOrMerge(bDescDoclist: p->bDescIdx, a1: aMerge, n1: nMerge,
185523 a2: pTS->aaOutput[iOut], n2: pTS->anOutput[iOut], paOut: &aNew, pnOut: &nNew
185524 );
185525 if( rc!=SQLITE_OK ){
185526 if( aMerge!=aDoclist ) sqlite3_free(p: aMerge);
185527 return rc;
185528 }
185529
185530 if( aMerge!=aDoclist ) sqlite3_free(p: aMerge);
185531 sqlite3_free(p: pTS->aaOutput[iOut]);
185532 pTS->aaOutput[iOut] = 0;
185533
185534 aMerge = aNew;
185535 nMerge = nNew;
185536 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
185537 pTS->aaOutput[iOut] = aMerge;
185538 pTS->anOutput[iOut] = nMerge;
185539 }
185540 }
185541 }
185542 }
185543 return SQLITE_OK;
185544}
185545
185546/*
185547** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
185548*/
185549static int fts3SegReaderCursorAppend(
185550 Fts3MultiSegReader *pCsr,
185551 Fts3SegReader *pNew
185552){
185553 if( (pCsr->nSegment%16)==0 ){
185554 Fts3SegReader **apNew;
185555 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
185556 apNew = (Fts3SegReader **)sqlite3_realloc64(pOld: pCsr->apSegment, n: nByte);
185557 if( !apNew ){
185558 sqlite3Fts3SegReaderFree(pNew);
185559 return SQLITE_NOMEM;
185560 }
185561 pCsr->apSegment = apNew;
185562 }
185563 pCsr->apSegment[pCsr->nSegment++] = pNew;
185564 return SQLITE_OK;
185565}
185566
185567/*
185568** Add seg-reader objects to the Fts3MultiSegReader object passed as the
185569** 8th argument.
185570**
185571** This function returns SQLITE_OK if successful, or an SQLite error code
185572** otherwise.
185573*/
185574static int fts3SegReaderCursor(
185575 Fts3Table *p, /* FTS3 table handle */
185576 int iLangid, /* Language id */
185577 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
185578 int iLevel, /* Level of segments to scan */
185579 const char *zTerm, /* Term to query for */
185580 int nTerm, /* Size of zTerm in bytes */
185581 int isPrefix, /* True for a prefix search */
185582 int isScan, /* True to scan from zTerm to EOF */
185583 Fts3MultiSegReader *pCsr /* Cursor object to populate */
185584){
185585 int rc = SQLITE_OK; /* Error code */
185586 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
185587 int rc2; /* Result of sqlite3_reset() */
185588
185589 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
185590 ** for the pending-terms. If this is a scan, then this call must be being
185591 ** made by an fts4aux module, not an FTS table. In this case calling
185592 ** Fts3SegReaderPending might segfault, as the data structures used by
185593 ** fts4aux are not completely populated. So it's easiest to filter these
185594 ** calls out here. */
185595 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
185596 Fts3SegReader *pSeg = 0;
185597 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
185598 if( rc==SQLITE_OK && pSeg ){
185599 rc = fts3SegReaderCursorAppend(pCsr, pNew: pSeg);
185600 }
185601 }
185602
185603 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
185604 if( rc==SQLITE_OK ){
185605 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
185606 }
185607
185608 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
185609 Fts3SegReader *pSeg = 0;
185610
185611 /* Read the values returned by the SELECT into local variables. */
185612 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, i: 1);
185613 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, i: 2);
185614 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, i: 3);
185615 int nRoot = sqlite3_column_bytes(pStmt, i: 4);
185616 char const *zRoot = sqlite3_column_blob(pStmt, i: 4);
185617
185618 /* If zTerm is not NULL, and this segment is not stored entirely on its
185619 ** root node, the range of leaves scanned can be reduced. Do this. */
185620 if( iStartBlock && zTerm && zRoot ){
185621 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
185622 rc = fts3SelectLeaf(p, zTerm, nTerm, zNode: zRoot, nNode: nRoot, piLeaf: &iStartBlock, piLeaf2: pi);
185623 if( rc!=SQLITE_OK ) goto finished;
185624 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
185625 }
185626
185627 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
185628 (isPrefix==0 && isScan==0),
185629 iStartBlock, iLeavesEndBlock,
185630 iEndBlock, zRoot, nRoot, &pSeg
185631 );
185632 if( rc!=SQLITE_OK ) goto finished;
185633 rc = fts3SegReaderCursorAppend(pCsr, pNew: pSeg);
185634 }
185635 }
185636
185637 finished:
185638 rc2 = sqlite3_reset(pStmt);
185639 if( rc==SQLITE_DONE ) rc = rc2;
185640
185641 return rc;
185642}
185643
185644/*
185645** Set up a cursor object for iterating through a full-text index or a
185646** single level therein.
185647*/
185648SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
185649 Fts3Table *p, /* FTS3 table handle */
185650 int iLangid, /* Language-id to search */
185651 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
185652 int iLevel, /* Level of segments to scan */
185653 const char *zTerm, /* Term to query for */
185654 int nTerm, /* Size of zTerm in bytes */
185655 int isPrefix, /* True for a prefix search */
185656 int isScan, /* True to scan from zTerm to EOF */
185657 Fts3MultiSegReader *pCsr /* Cursor object to populate */
185658){
185659 assert( iIndex>=0 && iIndex<p->nIndex );
185660 assert( iLevel==FTS3_SEGCURSOR_ALL
185661 || iLevel==FTS3_SEGCURSOR_PENDING
185662 || iLevel>=0
185663 );
185664 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
185665 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
185666 assert( isPrefix==0 || isScan==0 );
185667
185668 memset(s: pCsr, c: 0, n: sizeof(Fts3MultiSegReader));
185669 return fts3SegReaderCursor(
185670 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
185671 );
185672}
185673
185674/*
185675** In addition to its current configuration, have the Fts3MultiSegReader
185676** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
185677**
185678** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
185679*/
185680static int fts3SegReaderCursorAddZero(
185681 Fts3Table *p, /* FTS virtual table handle */
185682 int iLangid,
185683 const char *zTerm, /* Term to scan doclist of */
185684 int nTerm, /* Number of bytes in zTerm */
185685 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
185686){
185687 return fts3SegReaderCursor(p,
185688 iLangid, iIndex: 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix: 0, isScan: 0,pCsr
185689 );
185690}
185691
185692/*
185693** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
185694** if isPrefix is true, to scan the doclist for all terms for which
185695** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
185696** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
185697** an SQLite error code.
185698**
185699** It is the responsibility of the caller to free this object by eventually
185700** passing it to fts3SegReaderCursorFree()
185701**
185702** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
185703** Output parameter *ppSegcsr is set to 0 if an error occurs.
185704*/
185705static int fts3TermSegReaderCursor(
185706 Fts3Cursor *pCsr, /* Virtual table cursor handle */
185707 const char *zTerm, /* Term to query for */
185708 int nTerm, /* Size of zTerm in bytes */
185709 int isPrefix, /* True for a prefix search */
185710 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
185711){
185712 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
185713 int rc = SQLITE_NOMEM; /* Return code */
185714
185715 pSegcsr = sqlite3_malloc(n: sizeof(Fts3MultiSegReader));
185716 if( pSegcsr ){
185717 int i;
185718 int bFound = 0; /* True once an index has been found */
185719 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
185720
185721 if( isPrefix ){
185722 for(i=1; bFound==0 && i<p->nIndex; i++){
185723 if( p->aIndex[i].nPrefix==nTerm ){
185724 bFound = 1;
185725 rc = sqlite3Fts3SegReaderCursor(p, iLangid: pCsr->iLangid,
185726 iIndex: i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix: 0, isScan: 0, pCsr: pSegcsr
185727 );
185728 pSegcsr->bLookup = 1;
185729 }
185730 }
185731
185732 for(i=1; bFound==0 && i<p->nIndex; i++){
185733 if( p->aIndex[i].nPrefix==nTerm+1 ){
185734 bFound = 1;
185735 rc = sqlite3Fts3SegReaderCursor(p, iLangid: pCsr->iLangid,
185736 iIndex: i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix: 1, isScan: 0, pCsr: pSegcsr
185737 );
185738 if( rc==SQLITE_OK ){
185739 rc = fts3SegReaderCursorAddZero(
185740 p, iLangid: pCsr->iLangid, zTerm, nTerm, pCsr: pSegcsr
185741 );
185742 }
185743 }
185744 }
185745 }
185746
185747 if( bFound==0 ){
185748 rc = sqlite3Fts3SegReaderCursor(p, iLangid: pCsr->iLangid,
185749 iIndex: 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, isScan: 0, pCsr: pSegcsr
185750 );
185751 pSegcsr->bLookup = !isPrefix;
185752 }
185753 }
185754
185755 *ppSegcsr = pSegcsr;
185756 return rc;
185757}
185758
185759/*
185760** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
185761*/
185762static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
185763 sqlite3Fts3SegReaderFinish(pSegcsr);
185764 sqlite3_free(p: pSegcsr);
185765}
185766
185767/*
185768** This function retrieves the doclist for the specified term (or term
185769** prefix) from the database.
185770*/
185771static int fts3TermSelect(
185772 Fts3Table *p, /* Virtual table handle */
185773 Fts3PhraseToken *pTok, /* Token to query for */
185774 int iColumn, /* Column to query (or -ve for all columns) */
185775 int *pnOut, /* OUT: Size of buffer at *ppOut */
185776 char **ppOut /* OUT: Malloced result buffer */
185777){
185778 int rc; /* Return code */
185779 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
185780 TermSelect tsc; /* Object for pair-wise doclist merging */
185781 Fts3SegFilter filter; /* Segment term filter configuration */
185782
185783 pSegcsr = pTok->pSegcsr;
185784 memset(s: &tsc, c: 0, n: sizeof(TermSelect));
185785
185786 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
185787 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
185788 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
185789 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
185790 filter.iCol = iColumn;
185791 filter.zTerm = pTok->z;
185792 filter.nTerm = pTok->n;
185793
185794 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
185795 while( SQLITE_OK==rc
185796 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
185797 ){
185798 rc = fts3TermSelectMerge(p, pTS: &tsc, aDoclist: pSegcsr->aDoclist, nDoclist: pSegcsr->nDoclist);
185799 }
185800
185801 if( rc==SQLITE_OK ){
185802 rc = fts3TermSelectFinishMerge(p, pTS: &tsc);
185803 }
185804 if( rc==SQLITE_OK ){
185805 *ppOut = tsc.aaOutput[0];
185806 *pnOut = tsc.anOutput[0];
185807 }else{
185808 int i;
185809 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
185810 sqlite3_free(p: tsc.aaOutput[i]);
185811 }
185812 }
185813
185814 fts3SegReaderCursorFree(pSegcsr);
185815 pTok->pSegcsr = 0;
185816 return rc;
185817}
185818
185819/*
185820** This function counts the total number of docids in the doclist stored
185821** in buffer aList[], size nList bytes.
185822**
185823** If the isPoslist argument is true, then it is assumed that the doclist
185824** contains a position-list following each docid. Otherwise, it is assumed
185825** that the doclist is simply a list of docids stored as delta encoded
185826** varints.
185827*/
185828static int fts3DoclistCountDocids(char *aList, int nList){
185829 int nDoc = 0; /* Return value */
185830 if( aList ){
185831 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
185832 char *p = aList; /* Cursor */
185833 while( p<aEnd ){
185834 nDoc++;
185835 while( (*p++)&0x80 ); /* Skip docid varint */
185836 fts3PoslistCopy(pp: 0, ppPoslist: &p); /* Skip over position list */
185837 }
185838 }
185839
185840 return nDoc;
185841}
185842
185843/*
185844** Advance the cursor to the next row in the %_content table that
185845** matches the search criteria. For a MATCH search, this will be
185846** the next row that matches. For a full-table scan, this will be
185847** simply the next row in the %_content table. For a docid lookup,
185848** this routine simply sets the EOF flag.
185849**
185850** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
185851** even if we reach end-of-file. The fts3EofMethod() will be called
185852** subsequently to determine whether or not an EOF was hit.
185853*/
185854static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
185855 int rc;
185856 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
185857 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
185858 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
185859 pTab->bLock++;
185860 if( SQLITE_ROW!=sqlite3_step(pStmt: pCsr->pStmt) ){
185861 pCsr->isEof = 1;
185862 rc = sqlite3_reset(pStmt: pCsr->pStmt);
185863 }else{
185864 pCsr->iPrevId = sqlite3_column_int64(pStmt: pCsr->pStmt, i: 0);
185865 rc = SQLITE_OK;
185866 }
185867 pTab->bLock--;
185868 }else{
185869 rc = fts3EvalNext(pCsr: (Fts3Cursor *)pCursor);
185870 }
185871 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
185872 return rc;
185873}
185874
185875/*
185876** If the numeric type of argument pVal is "integer", then return it
185877** converted to a 64-bit signed integer. Otherwise, return a copy of
185878** the second parameter, iDefault.
185879*/
185880static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
185881 if( pVal ){
185882 int eType = sqlite3_value_numeric_type(pVal);
185883 if( eType==SQLITE_INTEGER ){
185884 return sqlite3_value_int64(pVal);
185885 }
185886 }
185887 return iDefault;
185888}
185889
185890/*
185891** This is the xFilter interface for the virtual table. See
185892** the virtual table xFilter method documentation for additional
185893** information.
185894**
185895** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
185896** the %_content table.
185897**
185898** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
185899** in the %_content table.
185900**
185901** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
185902** column on the left-hand side of the MATCH operator is column
185903** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
185904** side of the MATCH operator.
185905*/
185906static int fts3FilterMethod(
185907 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
185908 int idxNum, /* Strategy index */
185909 const char *idxStr, /* Unused */
185910 int nVal, /* Number of elements in apVal */
185911 sqlite3_value **apVal /* Arguments for the indexing scheme */
185912){
185913 int rc = SQLITE_OK;
185914 char *zSql; /* SQL statement used to access %_content */
185915 int eSearch;
185916 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
185917 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
185918
185919 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
185920 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
185921 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
185922 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
185923 int iIdx;
185924
185925 UNUSED_PARAMETER(idxStr);
185926 UNUSED_PARAMETER(nVal);
185927
185928 if( p->bLock ){
185929 return SQLITE_ERROR;
185930 }
185931
185932 eSearch = (idxNum & 0x0000FFFF);
185933 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
185934 assert( p->pSegments==0 );
185935
185936 /* Collect arguments into local variables */
185937 iIdx = 0;
185938 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
185939 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
185940 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
185941 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
185942 assert( iIdx==nVal );
185943
185944 /* In case the cursor has been used before, clear it now. */
185945 fts3ClearCursor(pCsr);
185946
185947 /* Set the lower and upper bounds on docids to return */
185948 pCsr->iMinDocid = fts3DocidRange(pVal: pDocidGe, SMALLEST_INT64);
185949 pCsr->iMaxDocid = fts3DocidRange(pVal: pDocidLe, LARGEST_INT64);
185950
185951 if( idxStr ){
185952 pCsr->bDesc = (idxStr[0]=='D');
185953 }else{
185954 pCsr->bDesc = p->bDescIdx;
185955 }
185956 pCsr->eSearch = (i16)eSearch;
185957
185958 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
185959 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
185960 const char *zQuery = (const char *)sqlite3_value_text(pVal: pCons);
185961
185962 if( zQuery==0 && sqlite3_value_type(pVal: pCons)!=SQLITE_NULL ){
185963 return SQLITE_NOMEM;
185964 }
185965
185966 pCsr->iLangid = 0;
185967 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pVal: pLangid);
185968
185969 assert( p->base.zErrMsg==0 );
185970 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
185971 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
185972 &p->base.zErrMsg
185973 );
185974 if( rc!=SQLITE_OK ){
185975 return rc;
185976 }
185977
185978 rc = fts3EvalStart(pCsr);
185979 sqlite3Fts3SegmentsClose(p);
185980 if( rc!=SQLITE_OK ) return rc;
185981 pCsr->pNextId = pCsr->aDoclist;
185982 pCsr->iPrevId = 0;
185983 }
185984
185985 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
185986 ** statement loops through all rows of the %_content table. For a
185987 ** full-text query or docid lookup, the statement retrieves a single
185988 ** row by docid.
185989 */
185990 if( eSearch==FTS3_FULLSCAN_SEARCH ){
185991 if( pDocidGe || pDocidLe ){
185992 zSql = sqlite3_mprintf(
185993 zFormat: "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
185994 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
185995 (pCsr->bDesc ? "DESC" : "ASC")
185996 );
185997 }else{
185998 zSql = sqlite3_mprintf(zFormat: "SELECT %s ORDER BY rowid %s",
185999 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
186000 );
186001 }
186002 if( zSql ){
186003 p->bLock++;
186004 rc = sqlite3_prepare_v3(
186005 db: p->db,zSql,nBytes: -1,SQLITE_PREPARE_PERSISTENT,ppStmt: &pCsr->pStmt,pzTail: 0
186006 );
186007 p->bLock--;
186008 sqlite3_free(p: zSql);
186009 }else{
186010 rc = SQLITE_NOMEM;
186011 }
186012 }else if( eSearch==FTS3_DOCID_SEARCH ){
186013 rc = fts3CursorSeekStmt(pCsr);
186014 if( rc==SQLITE_OK ){
186015 rc = sqlite3_bind_value(pStmt: pCsr->pStmt, i: 1, pValue: pCons);
186016 }
186017 }
186018 if( rc!=SQLITE_OK ) return rc;
186019
186020 return fts3NextMethod(pCursor);
186021}
186022
186023/*
186024** This is the xEof method of the virtual table. SQLite calls this
186025** routine to find out if it has reached the end of a result set.
186026*/
186027static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
186028 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
186029 if( pCsr->isEof ){
186030 fts3ClearCursor(pCsr);
186031 pCsr->isEof = 1;
186032 }
186033 return pCsr->isEof;
186034}
186035
186036/*
186037** This is the xRowid method. The SQLite core calls this routine to
186038** retrieve the rowid for the current row of the result set. fts3
186039** exposes %_content.docid as the rowid for the virtual table. The
186040** rowid should be written to *pRowid.
186041*/
186042static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
186043 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
186044 *pRowid = pCsr->iPrevId;
186045 return SQLITE_OK;
186046}
186047
186048/*
186049** This is the xColumn method, called by SQLite to request a value from
186050** the row that the supplied cursor currently points to.
186051**
186052** If:
186053**
186054** (iCol < p->nColumn) -> The value of the iCol'th user column.
186055** (iCol == p->nColumn) -> Magic column with the same name as the table.
186056** (iCol == p->nColumn+1) -> Docid column
186057** (iCol == p->nColumn+2) -> Langid column
186058*/
186059static int fts3ColumnMethod(
186060 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
186061 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
186062 int iCol /* Index of column to read value from */
186063){
186064 int rc = SQLITE_OK; /* Return Code */
186065 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
186066 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
186067
186068 /* The column value supplied by SQLite must be in range. */
186069 assert( iCol>=0 && iCol<=p->nColumn+2 );
186070
186071 switch( iCol-p->nColumn ){
186072 case 0:
186073 /* The special 'table-name' column */
186074 sqlite3_result_pointer(pCtx, pPtr: pCsr, zPType: "fts3cursor", xDestructor: 0);
186075 break;
186076
186077 case 1:
186078 /* The docid column */
186079 sqlite3_result_int64(pCtx, iVal: pCsr->iPrevId);
186080 break;
186081
186082 case 2:
186083 if( pCsr->pExpr ){
186084 sqlite3_result_int64(pCtx, iVal: pCsr->iLangid);
186085 break;
186086 }else if( p->zLanguageid==0 ){
186087 sqlite3_result_int(pCtx, iVal: 0);
186088 break;
186089 }else{
186090 iCol = p->nColumn;
186091 /* no break */ deliberate_fall_through
186092 }
186093
186094 default:
186095 /* A user column. Or, if this is a full-table scan, possibly the
186096 ** language-id column. Seek the cursor. */
186097 rc = fts3CursorSeek(pContext: 0, pCsr);
186098 if( rc==SQLITE_OK && sqlite3_data_count(pStmt: pCsr->pStmt)-1>iCol ){
186099 sqlite3_result_value(pCtx, pValue: sqlite3_column_value(pStmt: pCsr->pStmt, i: iCol+1));
186100 }
186101 break;
186102 }
186103
186104 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
186105 return rc;
186106}
186107
186108/*
186109** This function is the implementation of the xUpdate callback used by
186110** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
186111** inserted, updated or deleted.
186112*/
186113static int fts3UpdateMethod(
186114 sqlite3_vtab *pVtab, /* Virtual table handle */
186115 int nArg, /* Size of argument array */
186116 sqlite3_value **apVal, /* Array of arguments */
186117 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
186118){
186119 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
186120}
186121
186122/*
186123** Implementation of xSync() method. Flush the contents of the pending-terms
186124** hash-table to the database.
186125*/
186126static int fts3SyncMethod(sqlite3_vtab *pVtab){
186127
186128 /* Following an incremental-merge operation, assuming that the input
186129 ** segments are not completely consumed (the usual case), they are updated
186130 ** in place to remove the entries that have already been merged. This
186131 ** involves updating the leaf block that contains the smallest unmerged
186132 ** entry and each block (if any) between the leaf and the root node. So
186133 ** if the height of the input segment b-trees is N, and input segments
186134 ** are merged eight at a time, updating the input segments at the end
186135 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
186136 ** small - often between 0 and 2. So the overhead of the incremental
186137 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
186138 ** dwarfing the actual productive work accomplished, the incremental merge
186139 ** is only attempted if it will write at least 64 leaf blocks. Hence
186140 ** nMinMerge.
186141 **
186142 ** Of course, updating the input segments also involves deleting a bunch
186143 ** of blocks from the segments table. But this is not considered overhead
186144 ** as it would also be required by a crisis-merge that used the same input
186145 ** segments.
186146 */
186147 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
186148
186149 Fts3Table *p = (Fts3Table*)pVtab;
186150 int rc;
186151 i64 iLastRowid = sqlite3_last_insert_rowid(db: p->db);
186152
186153 rc = sqlite3Fts3PendingTermsFlush(p);
186154 if( rc==SQLITE_OK
186155 && p->nLeafAdd>(nMinMerge/16)
186156 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
186157 ){
186158 int mxLevel = 0; /* Maximum relative level value in db */
186159 int A; /* Incr-merge parameter A */
186160
186161 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
186162 assert( rc==SQLITE_OK || mxLevel==0 );
186163 A = p->nLeafAdd * mxLevel;
186164 A += (A/2);
186165 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
186166 }
186167 sqlite3Fts3SegmentsClose(p);
186168 sqlite3_set_last_insert_rowid(db: p->db, iRowid: iLastRowid);
186169 return rc;
186170}
186171
186172/*
186173** If it is currently unknown whether or not the FTS table has an %_stat
186174** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
186175** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
186176** if an error occurs.
186177*/
186178static int fts3SetHasStat(Fts3Table *p){
186179 int rc = SQLITE_OK;
186180 if( p->bHasStat==2 ){
186181 char *zTbl = sqlite3_mprintf(zFormat: "%s_stat", p->zName);
186182 if( zTbl ){
186183 int res = sqlite3_table_column_metadata(db: p->db, zDbName: p->zDb, zTableName: zTbl, zColumnName: 0,pzDataType: 0,pzCollSeq: 0,pNotNull: 0,pPrimaryKey: 0,pAutoinc: 0);
186184 sqlite3_free(p: zTbl);
186185 p->bHasStat = (res==SQLITE_OK);
186186 }else{
186187 rc = SQLITE_NOMEM;
186188 }
186189 }
186190 return rc;
186191}
186192
186193/*
186194** Implementation of xBegin() method.
186195*/
186196static int fts3BeginMethod(sqlite3_vtab *pVtab){
186197 Fts3Table *p = (Fts3Table*)pVtab;
186198 int rc;
186199 UNUSED_PARAMETER(pVtab);
186200 assert( p->pSegments==0 );
186201 assert( p->nPendingData==0 );
186202 assert( p->inTransaction!=1 );
186203 p->nLeafAdd = 0;
186204 rc = fts3SetHasStat(p);
186205#ifdef SQLITE_DEBUG
186206 if( rc==SQLITE_OK ){
186207 p->inTransaction = 1;
186208 p->mxSavepoint = -1;
186209 }
186210#endif
186211 return rc;
186212}
186213
186214/*
186215** Implementation of xCommit() method. This is a no-op. The contents of
186216** the pending-terms hash-table have already been flushed into the database
186217** by fts3SyncMethod().
186218*/
186219static int fts3CommitMethod(sqlite3_vtab *pVtab){
186220 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
186221 UNUSED_PARAMETER(pVtab);
186222 assert( p->nPendingData==0 );
186223 assert( p->inTransaction!=0 );
186224 assert( p->pSegments==0 );
186225 TESTONLY( p->inTransaction = 0 );
186226 TESTONLY( p->mxSavepoint = -1; );
186227 return SQLITE_OK;
186228}
186229
186230/*
186231** Implementation of xRollback(). Discard the contents of the pending-terms
186232** hash-table. Any changes made to the database are reverted by SQLite.
186233*/
186234static int fts3RollbackMethod(sqlite3_vtab *pVtab){
186235 Fts3Table *p = (Fts3Table*)pVtab;
186236 sqlite3Fts3PendingTermsClear(p);
186237 assert( p->inTransaction!=0 );
186238 TESTONLY( p->inTransaction = 0 );
186239 TESTONLY( p->mxSavepoint = -1; );
186240 return SQLITE_OK;
186241}
186242
186243/*
186244** When called, *ppPoslist must point to the byte immediately following the
186245** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
186246** moves *ppPoslist so that it instead points to the first byte of the
186247** same position list.
186248*/
186249static void fts3ReversePoslist(char *pStart, char **ppPoslist){
186250 char *p = &(*ppPoslist)[-2];
186251 char c = 0;
186252
186253 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
186254 while( p>pStart && (c=*p--)==0 );
186255
186256 /* Search backwards for a varint with value zero (the end of the previous
186257 ** poslist). This is an 0x00 byte preceded by some byte that does not
186258 ** have the 0x80 bit set. */
186259 while( p>pStart && (*p & 0x80) | c ){
186260 c = *p--;
186261 }
186262 assert( p==pStart || c==0 );
186263
186264 /* At this point p points to that preceding byte without the 0x80 bit
186265 ** set. So to find the start of the poslist, skip forward 2 bytes then
186266 ** over a varint.
186267 **
186268 ** Normally. The other case is that p==pStart and the poslist to return
186269 ** is the first in the doclist. In this case do not skip forward 2 bytes.
186270 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
186271 ** is required for cases where the first byte of a doclist and the
186272 ** doclist is empty. For example, if the first docid is 10, a doclist
186273 ** that begins with:
186274 **
186275 ** 0x0A 0x00 <next docid delta varint>
186276 */
186277 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
186278 while( *p++&0x80 );
186279 *ppPoslist = p;
186280}
186281
186282/*
186283** Helper function used by the implementation of the overloaded snippet(),
186284** offsets() and optimize() SQL functions.
186285**
186286** If the value passed as the third argument is a blob of size
186287** sizeof(Fts3Cursor*), then the blob contents are copied to the
186288** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
186289** message is written to context pContext and SQLITE_ERROR returned. The
186290** string passed via zFunc is used as part of the error message.
186291*/
186292static int fts3FunctionArg(
186293 sqlite3_context *pContext, /* SQL function call context */
186294 const char *zFunc, /* Function name */
186295 sqlite3_value *pVal, /* argv[0] passed to function */
186296 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
186297){
186298 int rc;
186299 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, zPType: "fts3cursor");
186300 if( (*ppCsr)!=0 ){
186301 rc = SQLITE_OK;
186302 }else{
186303 char *zErr = sqlite3_mprintf(zFormat: "illegal first argument to %s", zFunc);
186304 sqlite3_result_error(pCtx: pContext, z: zErr, n: -1);
186305 sqlite3_free(p: zErr);
186306 rc = SQLITE_ERROR;
186307 }
186308 return rc;
186309}
186310
186311/*
186312** Implementation of the snippet() function for FTS3
186313*/
186314static void fts3SnippetFunc(
186315 sqlite3_context *pContext, /* SQLite function call context */
186316 int nVal, /* Size of apVal[] array */
186317 sqlite3_value **apVal /* Array of arguments */
186318){
186319 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
186320 const char *zStart = "<b>";
186321 const char *zEnd = "</b>";
186322 const char *zEllipsis = "<b>...</b>";
186323 int iCol = -1;
186324 int nToken = 15; /* Default number of tokens in snippet */
186325
186326 /* There must be at least one argument passed to this function (otherwise
186327 ** the non-overloaded version would have been called instead of this one).
186328 */
186329 assert( nVal>=1 );
186330
186331 if( nVal>6 ){
186332 sqlite3_result_error(pCtx: pContext,
186333 z: "wrong number of arguments to function snippet()", n: -1);
186334 return;
186335 }
186336 if( fts3FunctionArg(pContext, zFunc: "snippet", pVal: apVal[0], ppCsr: &pCsr) ) return;
186337
186338 switch( nVal ){
186339 case 6: nToken = sqlite3_value_int(pVal: apVal[5]);
186340 /* no break */ deliberate_fall_through
186341 case 5: iCol = sqlite3_value_int(pVal: apVal[4]);
186342 /* no break */ deliberate_fall_through
186343 case 4: zEllipsis = (const char*)sqlite3_value_text(pVal: apVal[3]);
186344 /* no break */ deliberate_fall_through
186345 case 3: zEnd = (const char*)sqlite3_value_text(pVal: apVal[2]);
186346 /* no break */ deliberate_fall_through
186347 case 2: zStart = (const char*)sqlite3_value_text(pVal: apVal[1]);
186348 }
186349 if( !zEllipsis || !zEnd || !zStart ){
186350 sqlite3_result_error_nomem(pCtx: pContext);
186351 }else if( nToken==0 ){
186352 sqlite3_result_text(pCtx: pContext, z: "", n: -1, SQLITE_STATIC);
186353 }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
186354 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
186355 }
186356}
186357
186358/*
186359** Implementation of the offsets() function for FTS3
186360*/
186361static void fts3OffsetsFunc(
186362 sqlite3_context *pContext, /* SQLite function call context */
186363 int nVal, /* Size of argument array */
186364 sqlite3_value **apVal /* Array of arguments */
186365){
186366 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
186367
186368 UNUSED_PARAMETER(nVal);
186369
186370 assert( nVal==1 );
186371 if( fts3FunctionArg(pContext, zFunc: "offsets", pVal: apVal[0], ppCsr: &pCsr) ) return;
186372 assert( pCsr );
186373 if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
186374 sqlite3Fts3Offsets(pContext, pCsr);
186375 }
186376}
186377
186378/*
186379** Implementation of the special optimize() function for FTS3. This
186380** function merges all segments in the database to a single segment.
186381** Example usage is:
186382**
186383** SELECT optimize(t) FROM t LIMIT 1;
186384**
186385** where 't' is the name of an FTS3 table.
186386*/
186387static void fts3OptimizeFunc(
186388 sqlite3_context *pContext, /* SQLite function call context */
186389 int nVal, /* Size of argument array */
186390 sqlite3_value **apVal /* Array of arguments */
186391){
186392 int rc; /* Return code */
186393 Fts3Table *p; /* Virtual table handle */
186394 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
186395
186396 UNUSED_PARAMETER(nVal);
186397
186398 assert( nVal==1 );
186399 if( fts3FunctionArg(pContext, zFunc: "optimize", pVal: apVal[0], ppCsr: &pCursor) ) return;
186400 p = (Fts3Table *)pCursor->base.pVtab;
186401 assert( p );
186402
186403 rc = sqlite3Fts3Optimize(p);
186404
186405 switch( rc ){
186406 case SQLITE_OK:
186407 sqlite3_result_text(pCtx: pContext, z: "Index optimized", n: -1, SQLITE_STATIC);
186408 break;
186409 case SQLITE_DONE:
186410 sqlite3_result_text(pCtx: pContext, z: "Index already optimal", n: -1, SQLITE_STATIC);
186411 break;
186412 default:
186413 sqlite3_result_error_code(pCtx: pContext, errCode: rc);
186414 break;
186415 }
186416}
186417
186418/*
186419** Implementation of the matchinfo() function for FTS3
186420*/
186421static void fts3MatchinfoFunc(
186422 sqlite3_context *pContext, /* SQLite function call context */
186423 int nVal, /* Size of argument array */
186424 sqlite3_value **apVal /* Array of arguments */
186425){
186426 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
186427 assert( nVal==1 || nVal==2 );
186428 if( SQLITE_OK==fts3FunctionArg(pContext, zFunc: "matchinfo", pVal: apVal[0], ppCsr: &pCsr) ){
186429 const char *zArg = 0;
186430 if( nVal>1 ){
186431 zArg = (const char *)sqlite3_value_text(pVal: apVal[1]);
186432 }
186433 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
186434 }
186435}
186436
186437/*
186438** This routine implements the xFindFunction method for the FTS3
186439** virtual table.
186440*/
186441static int fts3FindFunctionMethod(
186442 sqlite3_vtab *pVtab, /* Virtual table handle */
186443 int nArg, /* Number of SQL function arguments */
186444 const char *zName, /* Name of SQL function */
186445 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
186446 void **ppArg /* Unused */
186447){
186448 struct Overloaded {
186449 const char *zName;
186450 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
186451 } aOverload[] = {
186452 { "snippet", fts3SnippetFunc },
186453 { "offsets", fts3OffsetsFunc },
186454 { "optimize", fts3OptimizeFunc },
186455 { "matchinfo", fts3MatchinfoFunc },
186456 };
186457 int i; /* Iterator variable */
186458
186459 UNUSED_PARAMETER(pVtab);
186460 UNUSED_PARAMETER(nArg);
186461 UNUSED_PARAMETER(ppArg);
186462
186463 for(i=0; i<SizeofArray(aOverload); i++){
186464 if( strcmp(s1: zName, s2: aOverload[i].zName)==0 ){
186465 *pxFunc = aOverload[i].xFunc;
186466 return 1;
186467 }
186468 }
186469
186470 /* No function of the specified name was found. Return 0. */
186471 return 0;
186472}
186473
186474/*
186475** Implementation of FTS3 xRename method. Rename an fts3 table.
186476*/
186477static int fts3RenameMethod(
186478 sqlite3_vtab *pVtab, /* Virtual table handle */
186479 const char *zName /* New name of table */
186480){
186481 Fts3Table *p = (Fts3Table *)pVtab;
186482 sqlite3 *db = p->db; /* Database connection */
186483 int rc; /* Return Code */
186484
186485 /* At this point it must be known if the %_stat table exists or not.
186486 ** So bHasStat may not be 2. */
186487 rc = fts3SetHasStat(p);
186488
186489 /* As it happens, the pending terms table is always empty here. This is
186490 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
186491 ** always opens a savepoint transaction. And the xSavepoint() method
186492 ** flushes the pending terms table. But leave the (no-op) call to
186493 ** PendingTermsFlush() in in case that changes.
186494 */
186495 assert( p->nPendingData==0 );
186496 if( rc==SQLITE_OK ){
186497 rc = sqlite3Fts3PendingTermsFlush(p);
186498 }
186499
186500 if( p->zContentTbl==0 ){
186501 fts3DbExec(pRc: &rc, db,
186502 zFormat: "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
186503 p->zDb, p->zName, zName
186504 );
186505 }
186506
186507 if( p->bHasDocsize ){
186508 fts3DbExec(pRc: &rc, db,
186509 zFormat: "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
186510 p->zDb, p->zName, zName
186511 );
186512 }
186513 if( p->bHasStat ){
186514 fts3DbExec(pRc: &rc, db,
186515 zFormat: "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
186516 p->zDb, p->zName, zName
186517 );
186518 }
186519 fts3DbExec(pRc: &rc, db,
186520 zFormat: "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
186521 p->zDb, p->zName, zName
186522 );
186523 fts3DbExec(pRc: &rc, db,
186524 zFormat: "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
186525 p->zDb, p->zName, zName
186526 );
186527 return rc;
186528}
186529
186530/*
186531** The xSavepoint() method.
186532**
186533** Flush the contents of the pending-terms table to disk.
186534*/
186535static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
186536 int rc = SQLITE_OK;
186537 UNUSED_PARAMETER(iSavepoint);
186538 assert( ((Fts3Table *)pVtab)->inTransaction );
186539 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
186540 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
186541 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
186542 rc = fts3SyncMethod(pVtab);
186543 }
186544 return rc;
186545}
186546
186547/*
186548** The xRelease() method.
186549**
186550** This is a no-op.
186551*/
186552static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
186553 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
186554 UNUSED_PARAMETER(iSavepoint);
186555 UNUSED_PARAMETER(pVtab);
186556 assert( p->inTransaction );
186557 assert( p->mxSavepoint >= iSavepoint );
186558 TESTONLY( p->mxSavepoint = iSavepoint-1 );
186559 return SQLITE_OK;
186560}
186561
186562/*
186563** The xRollbackTo() method.
186564**
186565** Discard the contents of the pending terms table.
186566*/
186567static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
186568 Fts3Table *p = (Fts3Table*)pVtab;
186569 UNUSED_PARAMETER(iSavepoint);
186570 assert( p->inTransaction );
186571 TESTONLY( p->mxSavepoint = iSavepoint );
186572 sqlite3Fts3PendingTermsClear(p);
186573 return SQLITE_OK;
186574}
186575
186576/*
186577** Return true if zName is the extension on one of the shadow tables used
186578** by this module.
186579*/
186580static int fts3ShadowName(const char *zName){
186581 static const char *azName[] = {
186582 "content", "docsize", "segdir", "segments", "stat",
186583 };
186584 unsigned int i;
186585 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
186586 if( sqlite3_stricmp(zLeft: zName, zRight: azName[i])==0 ) return 1;
186587 }
186588 return 0;
186589}
186590
186591static const sqlite3_module fts3Module = {
186592 /* iVersion */ 3,
186593 /* xCreate */ fts3CreateMethod,
186594 /* xConnect */ fts3ConnectMethod,
186595 /* xBestIndex */ fts3BestIndexMethod,
186596 /* xDisconnect */ fts3DisconnectMethod,
186597 /* xDestroy */ fts3DestroyMethod,
186598 /* xOpen */ fts3OpenMethod,
186599 /* xClose */ fts3CloseMethod,
186600 /* xFilter */ fts3FilterMethod,
186601 /* xNext */ fts3NextMethod,
186602 /* xEof */ fts3EofMethod,
186603 /* xColumn */ fts3ColumnMethod,
186604 /* xRowid */ fts3RowidMethod,
186605 /* xUpdate */ fts3UpdateMethod,
186606 /* xBegin */ fts3BeginMethod,
186607 /* xSync */ fts3SyncMethod,
186608 /* xCommit */ fts3CommitMethod,
186609 /* xRollback */ fts3RollbackMethod,
186610 /* xFindFunction */ fts3FindFunctionMethod,
186611 /* xRename */ fts3RenameMethod,
186612 /* xSavepoint */ fts3SavepointMethod,
186613 /* xRelease */ fts3ReleaseMethod,
186614 /* xRollbackTo */ fts3RollbackToMethod,
186615 /* xShadowName */ fts3ShadowName,
186616};
186617
186618/*
186619** This function is registered as the module destructor (called when an
186620** FTS3 enabled database connection is closed). It frees the memory
186621** allocated for the tokenizer hash table.
186622*/
186623static void hashDestroy(void *p){
186624 Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
186625 pHash->nRef--;
186626 if( pHash->nRef<=0 ){
186627 sqlite3Fts3HashClear(&pHash->hash);
186628 sqlite3_free(p: pHash);
186629 }
186630}
186631
186632/*
186633** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
186634** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
186635** respectively. The following three forward declarations are for functions
186636** declared in these files used to retrieve the respective implementations.
186637**
186638** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
186639** to by the argument to point to the "simple" tokenizer implementation.
186640** And so on.
186641*/
186642SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
186643SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
186644#ifndef SQLITE_DISABLE_FTS3_UNICODE
186645SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
186646#endif
186647#ifdef SQLITE_ENABLE_ICU
186648SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
186649#endif
186650
186651/*
186652** Initialize the fts3 extension. If this extension is built as part
186653** of the sqlite library, then this function is called directly by
186654** SQLite. If fts3 is built as a dynamically loadable extension, this
186655** function is called by the sqlite3_extension_init() entry point.
186656*/
186657SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
186658 int rc = SQLITE_OK;
186659 Fts3HashWrapper *pHash = 0;
186660 const sqlite3_tokenizer_module *pSimple = 0;
186661 const sqlite3_tokenizer_module *pPorter = 0;
186662#ifndef SQLITE_DISABLE_FTS3_UNICODE
186663 const sqlite3_tokenizer_module *pUnicode = 0;
186664#endif
186665
186666#ifdef SQLITE_ENABLE_ICU
186667 const sqlite3_tokenizer_module *pIcu = 0;
186668 sqlite3Fts3IcuTokenizerModule(&pIcu);
186669#endif
186670
186671#ifndef SQLITE_DISABLE_FTS3_UNICODE
186672 sqlite3Fts3UnicodeTokenizer(ppModule: &pUnicode);
186673#endif
186674
186675#ifdef SQLITE_TEST
186676 rc = sqlite3Fts3InitTerm(db);
186677 if( rc!=SQLITE_OK ) return rc;
186678#endif
186679
186680 rc = sqlite3Fts3InitAux(db);
186681 if( rc!=SQLITE_OK ) return rc;
186682
186683 sqlite3Fts3SimpleTokenizerModule(ppModule: &pSimple);
186684 sqlite3Fts3PorterTokenizerModule(ppModule: &pPorter);
186685
186686 /* Allocate and initialize the hash-table used to store tokenizers. */
186687 pHash = sqlite3_malloc(n: sizeof(Fts3HashWrapper));
186688 if( !pHash ){
186689 rc = SQLITE_NOMEM;
186690 }else{
186691 sqlite3Fts3HashInit(pNew: &pHash->hash, FTS3_HASH_STRING, copyKey: 1);
186692 pHash->nRef = 0;
186693 }
186694
186695 /* Load the built-in tokenizers into the hash table */
186696 if( rc==SQLITE_OK ){
186697 if( sqlite3Fts3HashInsert(&pHash->hash, pKey: "simple", nKey: 7, pData: (void *)pSimple)
186698 || sqlite3Fts3HashInsert(&pHash->hash, pKey: "porter", nKey: 7, pData: (void *)pPorter)
186699
186700#ifndef SQLITE_DISABLE_FTS3_UNICODE
186701 || sqlite3Fts3HashInsert(&pHash->hash, pKey: "unicode61", nKey: 10, pData: (void *)pUnicode)
186702#endif
186703#ifdef SQLITE_ENABLE_ICU
186704 || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
186705#endif
186706 ){
186707 rc = SQLITE_NOMEM;
186708 }
186709 }
186710
186711#ifdef SQLITE_TEST
186712 if( rc==SQLITE_OK ){
186713 rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
186714 }
186715#endif
186716
186717 /* Create the virtual table wrapper around the hash-table and overload
186718 ** the four scalar functions. If this is successful, register the
186719 ** module with sqlite.
186720 */
186721 if( SQLITE_OK==rc
186722 && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
186723 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "snippet", nArg: -1))
186724 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "offsets", nArg: 1))
186725 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "matchinfo", nArg: 1))
186726 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "matchinfo", nArg: 2))
186727 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "optimize", nArg: 1))
186728 ){
186729 pHash->nRef++;
186730 rc = sqlite3_create_module_v2(
186731 db, zName: "fts3", pModule: &fts3Module, pAux: (void *)pHash, xDestroy: hashDestroy
186732 );
186733 if( rc==SQLITE_OK ){
186734 pHash->nRef++;
186735 rc = sqlite3_create_module_v2(
186736 db, zName: "fts4", pModule: &fts3Module, pAux: (void *)pHash, xDestroy: hashDestroy
186737 );
186738 }
186739 if( rc==SQLITE_OK ){
186740 pHash->nRef++;
186741 rc = sqlite3Fts3InitTok(db, (void *)pHash, xDestroy: hashDestroy);
186742 }
186743 return rc;
186744 }
186745
186746
186747 /* An error has occurred. Delete the hash table and return the error code. */
186748 assert( rc!=SQLITE_OK );
186749 if( pHash ){
186750 sqlite3Fts3HashClear(&pHash->hash);
186751 sqlite3_free(p: pHash);
186752 }
186753 return rc;
186754}
186755
186756/*
186757** Allocate an Fts3MultiSegReader for each token in the expression headed
186758** by pExpr.
186759**
186760** An Fts3SegReader object is a cursor that can seek or scan a range of
186761** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
186762** Fts3SegReader objects internally to provide an interface to seek or scan
186763** within the union of all segments of a b-tree. Hence the name.
186764**
186765** If the allocated Fts3MultiSegReader just seeks to a single entry in a
186766** segment b-tree (if the term is not a prefix or it is a prefix for which
186767** there exists prefix b-tree of the right length) then it may be traversed
186768** and merged incrementally. Otherwise, it has to be merged into an in-memory
186769** doclist and then traversed.
186770*/
186771static void fts3EvalAllocateReaders(
186772 Fts3Cursor *pCsr, /* FTS cursor handle */
186773 Fts3Expr *pExpr, /* Allocate readers for this expression */
186774 int *pnToken, /* OUT: Total number of tokens in phrase. */
186775 int *pnOr, /* OUT: Total number of OR nodes in expr. */
186776 int *pRc /* IN/OUT: Error code */
186777){
186778 if( pExpr && SQLITE_OK==*pRc ){
186779 if( pExpr->eType==FTSQUERY_PHRASE ){
186780 int i;
186781 int nToken = pExpr->pPhrase->nToken;
186782 *pnToken += nToken;
186783 for(i=0; i<nToken; i++){
186784 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
186785 int rc = fts3TermSegReaderCursor(pCsr,
186786 zTerm: pToken->z, nTerm: pToken->n, isPrefix: pToken->isPrefix, ppSegcsr: &pToken->pSegcsr
186787 );
186788 if( rc!=SQLITE_OK ){
186789 *pRc = rc;
186790 return;
186791 }
186792 }
186793 assert( pExpr->pPhrase->iDoclistToken==0 );
186794 pExpr->pPhrase->iDoclistToken = -1;
186795 }else{
186796 *pnOr += (pExpr->eType==FTSQUERY_OR);
186797 fts3EvalAllocateReaders(pCsr, pExpr: pExpr->pLeft, pnToken, pnOr, pRc);
186798 fts3EvalAllocateReaders(pCsr, pExpr: pExpr->pRight, pnToken, pnOr, pRc);
186799 }
186800 }
186801}
186802
186803/*
186804** Arguments pList/nList contain the doclist for token iToken of phrase p.
186805** It is merged into the main doclist stored in p->doclist.aAll/nAll.
186806**
186807** This function assumes that pList points to a buffer allocated using
186808** sqlite3_malloc(). This function takes responsibility for eventually
186809** freeing the buffer.
186810**
186811** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
186812*/
186813static int fts3EvalPhraseMergeToken(
186814 Fts3Table *pTab, /* FTS Table pointer */
186815 Fts3Phrase *p, /* Phrase to merge pList/nList into */
186816 int iToken, /* Token pList/nList corresponds to */
186817 char *pList, /* Pointer to doclist */
186818 int nList /* Number of bytes in pList */
186819){
186820 int rc = SQLITE_OK;
186821 assert( iToken!=p->iDoclistToken );
186822
186823 if( pList==0 ){
186824 sqlite3_free(p: p->doclist.aAll);
186825 p->doclist.aAll = 0;
186826 p->doclist.nAll = 0;
186827 }
186828
186829 else if( p->iDoclistToken<0 ){
186830 p->doclist.aAll = pList;
186831 p->doclist.nAll = nList;
186832 }
186833
186834 else if( p->doclist.aAll==0 ){
186835 sqlite3_free(p: pList);
186836 }
186837
186838 else {
186839 char *pLeft;
186840 char *pRight;
186841 int nLeft;
186842 int nRight;
186843 int nDiff;
186844
186845 if( p->iDoclistToken<iToken ){
186846 pLeft = p->doclist.aAll;
186847 nLeft = p->doclist.nAll;
186848 pRight = pList;
186849 nRight = nList;
186850 nDiff = iToken - p->iDoclistToken;
186851 }else{
186852 pRight = p->doclist.aAll;
186853 nRight = p->doclist.nAll;
186854 pLeft = pList;
186855 nLeft = nList;
186856 nDiff = p->iDoclistToken - iToken;
186857 }
186858
186859 rc = fts3DoclistPhraseMerge(
186860 bDescDoclist: pTab->bDescIdx, nDist: nDiff, aLeft: pLeft, nLeft, paRight: &pRight, pnRight: &nRight
186861 );
186862 sqlite3_free(p: pLeft);
186863 p->doclist.aAll = pRight;
186864 p->doclist.nAll = nRight;
186865 }
186866
186867 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
186868 return rc;
186869}
186870
186871/*
186872** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
186873** does not take deferred tokens into account.
186874**
186875** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
186876*/
186877static int fts3EvalPhraseLoad(
186878 Fts3Cursor *pCsr, /* FTS Cursor handle */
186879 Fts3Phrase *p /* Phrase object */
186880){
186881 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
186882 int iToken;
186883 int rc = SQLITE_OK;
186884
186885 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
186886 Fts3PhraseToken *pToken = &p->aToken[iToken];
186887 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
186888
186889 if( pToken->pSegcsr ){
186890 int nThis = 0;
186891 char *pThis = 0;
186892 rc = fts3TermSelect(p: pTab, pTok: pToken, iColumn: p->iColumn, pnOut: &nThis, ppOut: &pThis);
186893 if( rc==SQLITE_OK ){
186894 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pList: pThis, nList: nThis);
186895 }
186896 }
186897 assert( pToken->pSegcsr==0 );
186898 }
186899
186900 return rc;
186901}
186902
186903#ifndef SQLITE_DISABLE_FTS4_DEFERRED
186904/*
186905** This function is called on each phrase after the position lists for
186906** any deferred tokens have been loaded into memory. It updates the phrases
186907** current position list to include only those positions that are really
186908** instances of the phrase (after considering deferred tokens). If this
186909** means that the phrase does not appear in the current row, doclist.pList
186910** and doclist.nList are both zeroed.
186911**
186912** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
186913*/
186914static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
186915 int iToken; /* Used to iterate through phrase tokens */
186916 char *aPoslist = 0; /* Position list for deferred tokens */
186917 int nPoslist = 0; /* Number of bytes in aPoslist */
186918 int iPrev = -1; /* Token number of previous deferred token */
186919 char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0);
186920
186921 for(iToken=0; iToken<pPhrase->nToken; iToken++){
186922 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
186923 Fts3DeferredToken *pDeferred = pToken->pDeferred;
186924
186925 if( pDeferred ){
186926 char *pList;
186927 int nList;
186928 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
186929 if( rc!=SQLITE_OK ) return rc;
186930
186931 if( pList==0 ){
186932 sqlite3_free(p: aPoslist);
186933 sqlite3_free(p: aFree);
186934 pPhrase->doclist.pList = 0;
186935 pPhrase->doclist.nList = 0;
186936 return SQLITE_OK;
186937
186938 }else if( aPoslist==0 ){
186939 aPoslist = pList;
186940 nPoslist = nList;
186941
186942 }else{
186943 char *aOut = pList;
186944 char *p1 = aPoslist;
186945 char *p2 = aOut;
186946
186947 assert( iPrev>=0 );
186948 fts3PoslistPhraseMerge(pp: &aOut, nToken: iToken-iPrev, isSaveLeft: 0, isExact: 1, pp1: &p1, pp2: &p2);
186949 sqlite3_free(p: aPoslist);
186950 aPoslist = pList;
186951 nPoslist = (int)(aOut - aPoslist);
186952 if( nPoslist==0 ){
186953 sqlite3_free(p: aPoslist);
186954 sqlite3_free(p: aFree);
186955 pPhrase->doclist.pList = 0;
186956 pPhrase->doclist.nList = 0;
186957 return SQLITE_OK;
186958 }
186959 }
186960 iPrev = iToken;
186961 }
186962 }
186963
186964 if( iPrev>=0 ){
186965 int nMaxUndeferred = pPhrase->iDoclistToken;
186966 if( nMaxUndeferred<0 ){
186967 pPhrase->doclist.pList = aPoslist;
186968 pPhrase->doclist.nList = nPoslist;
186969 pPhrase->doclist.iDocid = pCsr->iPrevId;
186970 pPhrase->doclist.bFreeList = 1;
186971 }else{
186972 int nDistance;
186973 char *p1;
186974 char *p2;
186975 char *aOut;
186976
186977 if( nMaxUndeferred>iPrev ){
186978 p1 = aPoslist;
186979 p2 = pPhrase->doclist.pList;
186980 nDistance = nMaxUndeferred - iPrev;
186981 }else{
186982 p1 = pPhrase->doclist.pList;
186983 p2 = aPoslist;
186984 nDistance = iPrev - nMaxUndeferred;
186985 }
186986
186987 aOut = (char *)sqlite3Fts3MallocZero(nByte: nPoslist+FTS3_BUFFER_PADDING);
186988 if( !aOut ){
186989 sqlite3_free(p: aPoslist);
186990 return SQLITE_NOMEM;
186991 }
186992
186993 pPhrase->doclist.pList = aOut;
186994 assert( p1 && p2 );
186995 if( fts3PoslistPhraseMerge(pp: &aOut, nToken: nDistance, isSaveLeft: 0, isExact: 1, pp1: &p1, pp2: &p2) ){
186996 pPhrase->doclist.bFreeList = 1;
186997 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
186998 }else{
186999 sqlite3_free(p: aOut);
187000 pPhrase->doclist.pList = 0;
187001 pPhrase->doclist.nList = 0;
187002 }
187003 sqlite3_free(p: aPoslist);
187004 }
187005 }
187006
187007 if( pPhrase->doclist.pList!=aFree ) sqlite3_free(p: aFree);
187008 return SQLITE_OK;
187009}
187010#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
187011
187012/*
187013** Maximum number of tokens a phrase may have to be considered for the
187014** incremental doclists strategy.
187015*/
187016#define MAX_INCR_PHRASE_TOKENS 4
187017
187018/*
187019** This function is called for each Fts3Phrase in a full-text query
187020** expression to initialize the mechanism for returning rows. Once this
187021** function has been called successfully on an Fts3Phrase, it may be
187022** used with fts3EvalPhraseNext() to iterate through the matching docids.
187023**
187024** If parameter bOptOk is true, then the phrase may (or may not) use the
187025** incremental loading strategy. Otherwise, the entire doclist is loaded into
187026** memory within this call.
187027**
187028** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
187029*/
187030static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
187031 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
187032 int rc = SQLITE_OK; /* Error code */
187033 int i;
187034
187035 /* Determine if doclists may be loaded from disk incrementally. This is
187036 ** possible if the bOptOk argument is true, the FTS doclists will be
187037 ** scanned in forward order, and the phrase consists of
187038 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
187039 ** tokens or prefix tokens that cannot use a prefix-index. */
187040 int bHaveIncr = 0;
187041 int bIncrOk = (bOptOk
187042 && pCsr->bDesc==pTab->bDescIdx
187043 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
187044#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
187045 && pTab->bNoIncrDoclist==0
187046#endif
187047 );
187048 for(i=0; bIncrOk==1 && i<p->nToken; i++){
187049 Fts3PhraseToken *pToken = &p->aToken[i];
187050 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
187051 bIncrOk = 0;
187052 }
187053 if( pToken->pSegcsr ) bHaveIncr = 1;
187054 }
187055
187056 if( bIncrOk && bHaveIncr ){
187057 /* Use the incremental approach. */
187058 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
187059 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
187060 Fts3PhraseToken *pToken = &p->aToken[i];
187061 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
187062 if( pSegcsr ){
187063 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
187064 }
187065 }
187066 p->bIncr = 1;
187067 }else{
187068 /* Load the full doclist for the phrase into memory. */
187069 rc = fts3EvalPhraseLoad(pCsr, p);
187070 p->bIncr = 0;
187071 }
187072
187073 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
187074 return rc;
187075}
187076
187077/*
187078** This function is used to iterate backwards (from the end to start)
187079** through doclists. It is used by this module to iterate through phrase
187080** doclists in reverse and by the fts3_write.c module to iterate through
187081** pending-terms lists when writing to databases with "order=desc".
187082**
187083** The doclist may be sorted in ascending (parameter bDescIdx==0) or
187084** descending (parameter bDescIdx==1) order of docid. Regardless, this
187085** function iterates from the end of the doclist to the beginning.
187086*/
187087SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
187088 int bDescIdx, /* True if the doclist is desc */
187089 char *aDoclist, /* Pointer to entire doclist */
187090 int nDoclist, /* Length of aDoclist in bytes */
187091 char **ppIter, /* IN/OUT: Iterator pointer */
187092 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
187093 int *pnList, /* OUT: List length pointer */
187094 u8 *pbEof /* OUT: End-of-file flag */
187095){
187096 char *p = *ppIter;
187097
187098 assert( nDoclist>0 );
187099 assert( *pbEof==0 );
187100 assert_fts3_nc( p || *piDocid==0 );
187101 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
187102
187103 if( p==0 ){
187104 sqlite3_int64 iDocid = 0;
187105 char *pNext = 0;
187106 char *pDocid = aDoclist;
187107 char *pEnd = &aDoclist[nDoclist];
187108 int iMul = 1;
187109
187110 while( pDocid<pEnd ){
187111 sqlite3_int64 iDelta;
187112 pDocid += sqlite3Fts3GetVarint(pBuf: pDocid, v: &iDelta);
187113 iDocid += (iMul * iDelta);
187114 pNext = pDocid;
187115 fts3PoslistCopy(pp: 0, ppPoslist: &pDocid);
187116 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
187117 iMul = (bDescIdx ? -1 : 1);
187118 }
187119
187120 *pnList = (int)(pEnd - pNext);
187121 *ppIter = pNext;
187122 *piDocid = iDocid;
187123 }else{
187124 int iMul = (bDescIdx ? -1 : 1);
187125 sqlite3_int64 iDelta;
187126 fts3GetReverseVarint(pp: &p, pStart: aDoclist, pVal: &iDelta);
187127 *piDocid -= (iMul * iDelta);
187128
187129 if( p==aDoclist ){
187130 *pbEof = 1;
187131 }else{
187132 char *pSave = p;
187133 fts3ReversePoslist(pStart: aDoclist, ppPoslist: &p);
187134 *pnList = (int)(pSave - p);
187135 }
187136 *ppIter = p;
187137 }
187138}
187139
187140/*
187141** Iterate forwards through a doclist.
187142*/
187143SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
187144 int bDescIdx, /* True if the doclist is desc */
187145 char *aDoclist, /* Pointer to entire doclist */
187146 int nDoclist, /* Length of aDoclist in bytes */
187147 char **ppIter, /* IN/OUT: Iterator pointer */
187148 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
187149 u8 *pbEof /* OUT: End-of-file flag */
187150){
187151 char *p = *ppIter;
187152
187153 assert( nDoclist>0 );
187154 assert( *pbEof==0 );
187155 assert_fts3_nc( p || *piDocid==0 );
187156 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
187157
187158 if( p==0 ){
187159 p = aDoclist;
187160 p += sqlite3Fts3GetVarint(pBuf: p, v: piDocid);
187161 }else{
187162 fts3PoslistCopy(pp: 0, ppPoslist: &p);
187163 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
187164 if( p>=&aDoclist[nDoclist] ){
187165 *pbEof = 1;
187166 }else{
187167 sqlite3_int64 iVar;
187168 p += sqlite3Fts3GetVarint(pBuf: p, v: &iVar);
187169 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
187170 }
187171 }
187172
187173 *ppIter = p;
187174}
187175
187176/*
187177** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
187178** to true if EOF is reached.
187179*/
187180static void fts3EvalDlPhraseNext(
187181 Fts3Table *pTab,
187182 Fts3Doclist *pDL,
187183 u8 *pbEof
187184){
187185 char *pIter; /* Used to iterate through aAll */
187186 char *pEnd; /* 1 byte past end of aAll */
187187
187188 if( pDL->pNextDocid ){
187189 pIter = pDL->pNextDocid;
187190 assert( pDL->aAll!=0 || pIter==0 );
187191 }else{
187192 pIter = pDL->aAll;
187193 }
187194
187195 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
187196 /* We have already reached the end of this doclist. EOF. */
187197 *pbEof = 1;
187198 }else{
187199 sqlite3_int64 iDelta;
187200 pIter += sqlite3Fts3GetVarint(pBuf: pIter, v: &iDelta);
187201 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
187202 pDL->iDocid += iDelta;
187203 }else{
187204 pDL->iDocid -= iDelta;
187205 }
187206 pDL->pList = pIter;
187207 fts3PoslistCopy(pp: 0, ppPoslist: &pIter);
187208 pDL->nList = (int)(pIter - pDL->pList);
187209
187210 /* pIter now points just past the 0x00 that terminates the position-
187211 ** list for document pDL->iDocid. However, if this position-list was
187212 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
187213 ** point to the start of the next docid value. The following line deals
187214 ** with this case by advancing pIter past the zero-padding added by
187215 ** fts3EvalNearTrim(). */
187216 while( pIter<pEnd && *pIter==0 ) pIter++;
187217
187218 pDL->pNextDocid = pIter;
187219 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
187220 *pbEof = 0;
187221 }
187222}
187223
187224/*
187225** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
187226*/
187227typedef struct TokenDoclist TokenDoclist;
187228struct TokenDoclist {
187229 int bIgnore;
187230 sqlite3_int64 iDocid;
187231 char *pList;
187232 int nList;
187233};
187234
187235/*
187236** Token pToken is an incrementally loaded token that is part of a
187237** multi-token phrase. Advance it to the next matching document in the
187238** database and populate output variable *p with the details of the new
187239** entry. Or, if the iterator has reached EOF, set *pbEof to true.
187240**
187241** If an error occurs, return an SQLite error code. Otherwise, return
187242** SQLITE_OK.
187243*/
187244static int incrPhraseTokenNext(
187245 Fts3Table *pTab, /* Virtual table handle */
187246 Fts3Phrase *pPhrase, /* Phrase to advance token of */
187247 int iToken, /* Specific token to advance */
187248 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
187249 u8 *pbEof /* OUT: True if iterator is at EOF */
187250){
187251 int rc = SQLITE_OK;
187252
187253 if( pPhrase->iDoclistToken==iToken ){
187254 assert( p->bIgnore==0 );
187255 assert( pPhrase->aToken[iToken].pSegcsr==0 );
187256 fts3EvalDlPhraseNext(pTab, pDL: &pPhrase->doclist, pbEof);
187257 p->pList = pPhrase->doclist.pList;
187258 p->nList = pPhrase->doclist.nList;
187259 p->iDocid = pPhrase->doclist.iDocid;
187260 }else{
187261 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
187262 assert( pToken->pDeferred==0 );
187263 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
187264 if( pToken->pSegcsr ){
187265 assert( p->bIgnore==0 );
187266 rc = sqlite3Fts3MsrIncrNext(
187267 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
187268 );
187269 if( p->pList==0 ) *pbEof = 1;
187270 }else{
187271 p->bIgnore = 1;
187272 }
187273 }
187274
187275 return rc;
187276}
187277
187278
187279/*
187280** The phrase iterator passed as the second argument:
187281**
187282** * features at least one token that uses an incremental doclist, and
187283**
187284** * does not contain any deferred tokens.
187285**
187286** Advance it to the next matching documnent in the database and populate
187287** the Fts3Doclist.pList and nList fields.
187288**
187289** If there is no "next" entry and no error occurs, then *pbEof is set to
187290** 1 before returning. Otherwise, if no error occurs and the iterator is
187291** successfully advanced, *pbEof is set to 0.
187292**
187293** If an error occurs, return an SQLite error code. Otherwise, return
187294** SQLITE_OK.
187295*/
187296static int fts3EvalIncrPhraseNext(
187297 Fts3Cursor *pCsr, /* FTS Cursor handle */
187298 Fts3Phrase *p, /* Phrase object to advance to next docid */
187299 u8 *pbEof /* OUT: Set to 1 if EOF */
187300){
187301 int rc = SQLITE_OK;
187302 Fts3Doclist *pDL = &p->doclist;
187303 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
187304 u8 bEof = 0;
187305
187306 /* This is only called if it is guaranteed that the phrase has at least
187307 ** one incremental token. In which case the bIncr flag is set. */
187308 assert( p->bIncr==1 );
187309
187310 if( p->nToken==1 ){
187311 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
187312 &pDL->iDocid, &pDL->pList, &pDL->nList
187313 );
187314 if( pDL->pList==0 ) bEof = 1;
187315 }else{
187316 int bDescDoclist = pCsr->bDesc;
187317 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
187318
187319 memset(s: a, c: 0, n: sizeof(a));
187320 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
187321 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
187322
187323 while( bEof==0 ){
187324 int bMaxSet = 0;
187325 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
187326 int i; /* Used to iterate through tokens */
187327
187328 /* Advance the iterator for each token in the phrase once. */
187329 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
187330 rc = incrPhraseTokenNext(pTab, pPhrase: p, iToken: i, p: &a[i], pbEof: &bEof);
187331 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
187332 iMax = a[i].iDocid;
187333 bMaxSet = 1;
187334 }
187335 }
187336 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
187337 assert( rc!=SQLITE_OK || bMaxSet );
187338
187339 /* Keep advancing iterators until they all point to the same document */
187340 for(i=0; i<p->nToken; i++){
187341 while( rc==SQLITE_OK && bEof==0
187342 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
187343 ){
187344 rc = incrPhraseTokenNext(pTab, pPhrase: p, iToken: i, p: &a[i], pbEof: &bEof);
187345 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
187346 iMax = a[i].iDocid;
187347 i = 0;
187348 }
187349 }
187350 }
187351
187352 /* Check if the current entries really are a phrase match */
187353 if( bEof==0 ){
187354 int nList = 0;
187355 int nByte = a[p->nToken-1].nList;
187356 char *aDoclist = sqlite3_malloc64(n: (i64)nByte+FTS3_BUFFER_PADDING);
187357 if( !aDoclist ) return SQLITE_NOMEM;
187358 memcpy(dest: aDoclist, src: a[p->nToken-1].pList, n: nByte+1);
187359 memset(s: &aDoclist[nByte], c: 0, FTS3_BUFFER_PADDING);
187360
187361 for(i=0; i<(p->nToken-1); i++){
187362 if( a[i].bIgnore==0 ){
187363 char *pL = a[i].pList;
187364 char *pR = aDoclist;
187365 char *pOut = aDoclist;
187366 int nDist = p->nToken-1-i;
187367 int res = fts3PoslistPhraseMerge(pp: &pOut, nToken: nDist, isSaveLeft: 0, isExact: 1, pp1: &pL, pp2: &pR);
187368 if( res==0 ) break;
187369 nList = (int)(pOut - aDoclist);
187370 }
187371 }
187372 if( i==(p->nToken-1) ){
187373 pDL->iDocid = iMax;
187374 pDL->pList = aDoclist;
187375 pDL->nList = nList;
187376 pDL->bFreeList = 1;
187377 break;
187378 }
187379 sqlite3_free(p: aDoclist);
187380 }
187381 }
187382 }
187383
187384 *pbEof = bEof;
187385 return rc;
187386}
187387
187388/*
187389** Attempt to move the phrase iterator to point to the next matching docid.
187390** If an error occurs, return an SQLite error code. Otherwise, return
187391** SQLITE_OK.
187392**
187393** If there is no "next" entry and no error occurs, then *pbEof is set to
187394** 1 before returning. Otherwise, if no error occurs and the iterator is
187395** successfully advanced, *pbEof is set to 0.
187396*/
187397static int fts3EvalPhraseNext(
187398 Fts3Cursor *pCsr, /* FTS Cursor handle */
187399 Fts3Phrase *p, /* Phrase object to advance to next docid */
187400 u8 *pbEof /* OUT: Set to 1 if EOF */
187401){
187402 int rc = SQLITE_OK;
187403 Fts3Doclist *pDL = &p->doclist;
187404 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
187405
187406 if( p->bIncr ){
187407 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
187408 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
187409 sqlite3Fts3DoclistPrev(bDescIdx: pTab->bDescIdx, aDoclist: pDL->aAll, nDoclist: pDL->nAll,
187410 ppIter: &pDL->pNextDocid, piDocid: &pDL->iDocid, pnList: &pDL->nList, pbEof
187411 );
187412 pDL->pList = pDL->pNextDocid;
187413 }else{
187414 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
187415 }
187416
187417 return rc;
187418}
187419
187420/*
187421**
187422** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
187423** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
187424** expression. Also the Fts3Expr.bDeferred variable is set to true for any
187425** expressions for which all descendent tokens are deferred.
187426**
187427** If parameter bOptOk is zero, then it is guaranteed that the
187428** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
187429** each phrase in the expression (subject to deferred token processing).
187430** Or, if bOptOk is non-zero, then one or more tokens within the expression
187431** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
187432**
187433** If an error occurs within this function, *pRc is set to an SQLite error
187434** code before returning.
187435*/
187436static void fts3EvalStartReaders(
187437 Fts3Cursor *pCsr, /* FTS Cursor handle */
187438 Fts3Expr *pExpr, /* Expression to initialize phrases in */
187439 int *pRc /* IN/OUT: Error code */
187440){
187441 if( pExpr && SQLITE_OK==*pRc ){
187442 if( pExpr->eType==FTSQUERY_PHRASE ){
187443 int nToken = pExpr->pPhrase->nToken;
187444 if( nToken ){
187445 int i;
187446 for(i=0; i<nToken; i++){
187447 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
187448 }
187449 pExpr->bDeferred = (i==nToken);
187450 }
187451 *pRc = fts3EvalPhraseStart(pCsr, bOptOk: 1, p: pExpr->pPhrase);
187452 }else{
187453 fts3EvalStartReaders(pCsr, pExpr: pExpr->pLeft, pRc);
187454 fts3EvalStartReaders(pCsr, pExpr: pExpr->pRight, pRc);
187455 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
187456 }
187457 }
187458}
187459
187460/*
187461** An array of the following structures is assembled as part of the process
187462** of selecting tokens to defer before the query starts executing (as part
187463** of the xFilter() method). There is one element in the array for each
187464** token in the FTS expression.
187465**
187466** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
187467** to phrases that are connected only by AND and NEAR operators (not OR or
187468** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
187469** separately. The root of a tokens AND/NEAR cluster is stored in
187470** Fts3TokenAndCost.pRoot.
187471*/
187472typedef struct Fts3TokenAndCost Fts3TokenAndCost;
187473struct Fts3TokenAndCost {
187474 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
187475 int iToken; /* Position of token in phrase */
187476 Fts3PhraseToken *pToken; /* The token itself */
187477 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
187478 int nOvfl; /* Number of overflow pages to load doclist */
187479 int iCol; /* The column the token must match */
187480};
187481
187482/*
187483** This function is used to populate an allocated Fts3TokenAndCost array.
187484**
187485** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
187486** Otherwise, if an error occurs during execution, *pRc is set to an
187487** SQLite error code.
187488*/
187489static void fts3EvalTokenCosts(
187490 Fts3Cursor *pCsr, /* FTS Cursor handle */
187491 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
187492 Fts3Expr *pExpr, /* Expression to consider */
187493 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
187494 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
187495 int *pRc /* IN/OUT: Error code */
187496){
187497 if( *pRc==SQLITE_OK ){
187498 if( pExpr->eType==FTSQUERY_PHRASE ){
187499 Fts3Phrase *pPhrase = pExpr->pPhrase;
187500 int i;
187501 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
187502 Fts3TokenAndCost *pTC = (*ppTC)++;
187503 pTC->pPhrase = pPhrase;
187504 pTC->iToken = i;
187505 pTC->pRoot = pRoot;
187506 pTC->pToken = &pPhrase->aToken[i];
187507 pTC->iCol = pPhrase->iColumn;
187508 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
187509 }
187510 }else if( pExpr->eType!=FTSQUERY_NOT ){
187511 assert( pExpr->eType==FTSQUERY_OR
187512 || pExpr->eType==FTSQUERY_AND
187513 || pExpr->eType==FTSQUERY_NEAR
187514 );
187515 assert( pExpr->pLeft && pExpr->pRight );
187516 if( pExpr->eType==FTSQUERY_OR ){
187517 pRoot = pExpr->pLeft;
187518 **ppOr = pRoot;
187519 (*ppOr)++;
187520 }
187521 fts3EvalTokenCosts(pCsr, pRoot, pExpr: pExpr->pLeft, ppTC, ppOr, pRc);
187522 if( pExpr->eType==FTSQUERY_OR ){
187523 pRoot = pExpr->pRight;
187524 **ppOr = pRoot;
187525 (*ppOr)++;
187526 }
187527 fts3EvalTokenCosts(pCsr, pRoot, pExpr: pExpr->pRight, ppTC, ppOr, pRc);
187528 }
187529 }
187530}
187531
187532/*
187533** Determine the average document (row) size in pages. If successful,
187534** write this value to *pnPage and return SQLITE_OK. Otherwise, return
187535** an SQLite error code.
187536**
187537** The average document size in pages is calculated by first calculating
187538** determining the average size in bytes, B. If B is less than the amount
187539** of data that will fit on a single leaf page of an intkey table in
187540** this database, then the average docsize is 1. Otherwise, it is 1 plus
187541** the number of overflow pages consumed by a record B bytes in size.
187542*/
187543static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
187544 int rc = SQLITE_OK;
187545 if( pCsr->nRowAvg==0 ){
187546 /* The average document size, which is required to calculate the cost
187547 ** of each doclist, has not yet been determined. Read the required
187548 ** data from the %_stat table to calculate it.
187549 **
187550 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
187551 ** varints, where nCol is the number of columns in the FTS3 table.
187552 ** The first varint is the number of documents currently stored in
187553 ** the table. The following nCol varints contain the total amount of
187554 ** data stored in all rows of each column of the table, from left
187555 ** to right.
187556 */
187557 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
187558 sqlite3_stmt *pStmt;
187559 sqlite3_int64 nDoc = 0;
187560 sqlite3_int64 nByte = 0;
187561 const char *pEnd;
187562 const char *a;
187563
187564 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
187565 if( rc!=SQLITE_OK ) return rc;
187566 a = sqlite3_column_blob(pStmt, i: 0);
187567 testcase( a==0 ); /* If %_stat.value set to X'' */
187568 if( a ){
187569 pEnd = &a[sqlite3_column_bytes(pStmt, i: 0)];
187570 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nDoc);
187571 while( a<pEnd ){
187572 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nByte);
187573 }
187574 }
187575 if( nDoc==0 || nByte==0 ){
187576 sqlite3_reset(pStmt);
187577 return FTS_CORRUPT_VTAB;
187578 }
187579
187580 pCsr->nDoc = nDoc;
187581 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
187582 assert( pCsr->nRowAvg>0 );
187583 rc = sqlite3_reset(pStmt);
187584 }
187585
187586 *pnPage = pCsr->nRowAvg;
187587 return rc;
187588}
187589
187590/*
187591** This function is called to select the tokens (if any) that will be
187592** deferred. The array aTC[] has already been populated when this is
187593** called.
187594**
187595** This function is called once for each AND/NEAR cluster in the
187596** expression. Each invocation determines which tokens to defer within
187597** the cluster with root node pRoot. See comments above the definition
187598** of struct Fts3TokenAndCost for more details.
187599**
187600** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
187601** called on each token to defer. Otherwise, an SQLite error code is
187602** returned.
187603*/
187604static int fts3EvalSelectDeferred(
187605 Fts3Cursor *pCsr, /* FTS Cursor handle */
187606 Fts3Expr *pRoot, /* Consider tokens with this root node */
187607 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
187608 int nTC /* Number of entries in aTC[] */
187609){
187610 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
187611 int nDocSize = 0; /* Number of pages per doc loaded */
187612 int rc = SQLITE_OK; /* Return code */
187613 int ii; /* Iterator variable for various purposes */
187614 int nOvfl = 0; /* Total overflow pages used by doclists */
187615 int nToken = 0; /* Total number of tokens in cluster */
187616
187617 int nMinEst = 0; /* The minimum count for any phrase so far. */
187618 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
187619
187620 /* Tokens are never deferred for FTS tables created using the content=xxx
187621 ** option. The reason being that it is not guaranteed that the content
187622 ** table actually contains the same data as the index. To prevent this from
187623 ** causing any problems, the deferred token optimization is completely
187624 ** disabled for content=xxx tables. */
187625 if( pTab->zContentTbl ){
187626 return SQLITE_OK;
187627 }
187628
187629 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
187630 ** associated with the tokens spill onto overflow pages, or if there is
187631 ** only 1 token, exit early. No tokens to defer in this case. */
187632 for(ii=0; ii<nTC; ii++){
187633 if( aTC[ii].pRoot==pRoot ){
187634 nOvfl += aTC[ii].nOvfl;
187635 nToken++;
187636 }
187637 }
187638 if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
187639
187640 /* Obtain the average docsize (in pages). */
187641 rc = fts3EvalAverageDocsize(pCsr, pnPage: &nDocSize);
187642 assert( rc!=SQLITE_OK || nDocSize>0 );
187643
187644
187645 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
187646 ** of the number of overflow pages that will be loaded by the pager layer
187647 ** to retrieve the entire doclist for the token from the full-text index.
187648 ** Load the doclists for tokens that are either:
187649 **
187650 ** a. The cheapest token in the entire query (i.e. the one visited by the
187651 ** first iteration of this loop), or
187652 **
187653 ** b. Part of a multi-token phrase.
187654 **
187655 ** After each token doclist is loaded, merge it with the others from the
187656 ** same phrase and count the number of documents that the merged doclist
187657 ** contains. Set variable "nMinEst" to the smallest number of documents in
187658 ** any phrase doclist for which 1 or more token doclists have been loaded.
187659 ** Let nOther be the number of other phrases for which it is certain that
187660 ** one or more tokens will not be deferred.
187661 **
187662 ** Then, for each token, defer it if loading the doclist would result in
187663 ** loading N or more overflow pages into memory, where N is computed as:
187664 **
187665 ** (nMinEst + 4^nOther - 1) / (4^nOther)
187666 */
187667 for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
187668 int iTC; /* Used to iterate through aTC[] array. */
187669 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
187670
187671 /* Set pTC to point to the cheapest remaining token. */
187672 for(iTC=0; iTC<nTC; iTC++){
187673 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
187674 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
187675 ){
187676 pTC = &aTC[iTC];
187677 }
187678 }
187679 assert( pTC );
187680
187681 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
187682 /* The number of overflow pages to load for this (and therefore all
187683 ** subsequent) tokens is greater than the estimated number of pages
187684 ** that will be loaded if all subsequent tokens are deferred.
187685 */
187686 Fts3PhraseToken *pToken = pTC->pToken;
187687 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
187688 fts3SegReaderCursorFree(pSegcsr: pToken->pSegcsr);
187689 pToken->pSegcsr = 0;
187690 }else{
187691 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
187692 ** for-loop. Except, limit the value to 2^24 to prevent it from
187693 ** overflowing the 32-bit integer it is stored in. */
187694 if( ii<12 ) nLoad4 = nLoad4*4;
187695
187696 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
187697 /* Either this is the cheapest token in the entire query, or it is
187698 ** part of a multi-token phrase. Either way, the entire doclist will
187699 ** (eventually) be loaded into memory. It may as well be now. */
187700 Fts3PhraseToken *pToken = pTC->pToken;
187701 int nList = 0;
187702 char *pList = 0;
187703 rc = fts3TermSelect(p: pTab, pTok: pToken, iColumn: pTC->iCol, pnOut: &nList, ppOut: &pList);
187704 assert( rc==SQLITE_OK || pList==0 );
187705 if( rc==SQLITE_OK ){
187706 rc = fts3EvalPhraseMergeToken(
187707 pTab, p: pTC->pPhrase, iToken: pTC->iToken,pList,nList
187708 );
187709 }
187710 if( rc==SQLITE_OK ){
187711 int nCount;
187712 nCount = fts3DoclistCountDocids(
187713 aList: pTC->pPhrase->doclist.aAll, nList: pTC->pPhrase->doclist.nAll
187714 );
187715 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
187716 }
187717 }
187718 }
187719 pTC->pToken = 0;
187720 }
187721
187722 return rc;
187723}
187724
187725/*
187726** This function is called from within the xFilter method. It initializes
187727** the full-text query currently stored in pCsr->pExpr. To iterate through
187728** the results of a query, the caller does:
187729**
187730** fts3EvalStart(pCsr);
187731** while( 1 ){
187732** fts3EvalNext(pCsr);
187733** if( pCsr->bEof ) break;
187734** ... return row pCsr->iPrevId to the caller ...
187735** }
187736*/
187737static int fts3EvalStart(Fts3Cursor *pCsr){
187738 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
187739 int rc = SQLITE_OK;
187740 int nToken = 0;
187741 int nOr = 0;
187742
187743 /* Allocate a MultiSegReader for each token in the expression. */
187744 fts3EvalAllocateReaders(pCsr, pExpr: pCsr->pExpr, pnToken: &nToken, pnOr: &nOr, pRc: &rc);
187745
187746 /* Determine which, if any, tokens in the expression should be deferred. */
187747#ifndef SQLITE_DISABLE_FTS4_DEFERRED
187748 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
187749 Fts3TokenAndCost *aTC;
187750 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
187751 n: sizeof(Fts3TokenAndCost) * nToken
187752 + sizeof(Fts3Expr *) * nOr * 2
187753 );
187754
187755 if( !aTC ){
187756 rc = SQLITE_NOMEM;
187757 }else{
187758 Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken];
187759 int ii;
187760 Fts3TokenAndCost *pTC = aTC;
187761 Fts3Expr **ppOr = apOr;
187762
187763 fts3EvalTokenCosts(pCsr, pRoot: 0, pExpr: pCsr->pExpr, ppTC: &pTC, ppOr: &ppOr, pRc: &rc);
187764 nToken = (int)(pTC-aTC);
187765 nOr = (int)(ppOr-apOr);
187766
187767 if( rc==SQLITE_OK ){
187768 rc = fts3EvalSelectDeferred(pCsr, pRoot: 0, aTC, nTC: nToken);
187769 for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
187770 rc = fts3EvalSelectDeferred(pCsr, pRoot: apOr[ii], aTC, nTC: nToken);
187771 }
187772 }
187773
187774 sqlite3_free(p: aTC);
187775 }
187776 }
187777#endif
187778
187779 fts3EvalStartReaders(pCsr, pExpr: pCsr->pExpr, pRc: &rc);
187780 return rc;
187781}
187782
187783/*
187784** Invalidate the current position list for phrase pPhrase.
187785*/
187786static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
187787 if( pPhrase->doclist.bFreeList ){
187788 sqlite3_free(p: pPhrase->doclist.pList);
187789 }
187790 pPhrase->doclist.pList = 0;
187791 pPhrase->doclist.nList = 0;
187792 pPhrase->doclist.bFreeList = 0;
187793}
187794
187795/*
187796** This function is called to edit the position list associated with
187797** the phrase object passed as the fifth argument according to a NEAR
187798** condition. For example:
187799**
187800** abc NEAR/5 "def ghi"
187801**
187802** Parameter nNear is passed the NEAR distance of the expression (5 in
187803** the example above). When this function is called, *paPoslist points to
187804** the position list, and *pnToken is the number of phrase tokens in the
187805** phrase on the other side of the NEAR operator to pPhrase. For example,
187806** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
187807** the position list associated with phrase "abc".
187808**
187809** All positions in the pPhrase position list that are not sufficiently
187810** close to a position in the *paPoslist position list are removed. If this
187811** leaves 0 positions, zero is returned. Otherwise, non-zero.
187812**
187813** Before returning, *paPoslist is set to point to the position lsit
187814** associated with pPhrase. And *pnToken is set to the number of tokens in
187815** pPhrase.
187816*/
187817static int fts3EvalNearTrim(
187818 int nNear, /* NEAR distance. As in "NEAR/nNear". */
187819 char *aTmp, /* Temporary space to use */
187820 char **paPoslist, /* IN/OUT: Position list */
187821 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
187822 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
187823){
187824 int nParam1 = nNear + pPhrase->nToken;
187825 int nParam2 = nNear + *pnToken;
187826 int nNew;
187827 char *p2;
187828 char *pOut;
187829 int res;
187830
187831 assert( pPhrase->doclist.pList );
187832
187833 p2 = pOut = pPhrase->doclist.pList;
187834 res = fts3PoslistNearMerge(
187835 pp: &pOut, aTmp, nRight: nParam1, nLeft: nParam2, pp1: paPoslist, pp2: &p2
187836 );
187837 if( res ){
187838 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
187839 assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 );
187840 if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
187841 assert( pPhrase->doclist.pList[nNew]=='\0' );
187842 memset(s: &pPhrase->doclist.pList[nNew], c: 0, n: pPhrase->doclist.nList - nNew);
187843 pPhrase->doclist.nList = nNew;
187844 }
187845 *paPoslist = pPhrase->doclist.pList;
187846 *pnToken = pPhrase->nToken;
187847 }
187848
187849 return res;
187850}
187851
187852/*
187853** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
187854** Otherwise, it advances the expression passed as the second argument to
187855** point to the next matching row in the database. Expressions iterate through
187856** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
187857** or descending if it is non-zero.
187858**
187859** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
187860** successful, the following variables in pExpr are set:
187861**
187862** Fts3Expr.bEof (non-zero if EOF - there is no next row)
187863** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
187864**
187865** If the expression is of type FTSQUERY_PHRASE, and the expression is not
187866** at EOF, then the following variables are populated with the position list
187867** for the phrase for the visited row:
187868**
187869** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
187870** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
187871**
187872** It says above that this function advances the expression to the next
187873** matching row. This is usually true, but there are the following exceptions:
187874**
187875** 1. Deferred tokens are not taken into account. If a phrase consists
187876** entirely of deferred tokens, it is assumed to match every row in
187877** the db. In this case the position-list is not populated at all.
187878**
187879** Or, if a phrase contains one or more deferred tokens and one or
187880** more non-deferred tokens, then the expression is advanced to the
187881** next possible match, considering only non-deferred tokens. In other
187882** words, if the phrase is "A B C", and "B" is deferred, the expression
187883** is advanced to the next row that contains an instance of "A * C",
187884** where "*" may match any single token. The position list in this case
187885** is populated as for "A * C" before returning.
187886**
187887** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
187888** advanced to point to the next row that matches "x AND y".
187889**
187890** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
187891** really a match, taking into account deferred tokens and NEAR operators.
187892*/
187893static void fts3EvalNextRow(
187894 Fts3Cursor *pCsr, /* FTS Cursor handle */
187895 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
187896 int *pRc /* IN/OUT: Error code */
187897){
187898 if( *pRc==SQLITE_OK && pExpr->bEof==0 ){
187899 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
187900 pExpr->bStart = 1;
187901
187902 switch( pExpr->eType ){
187903 case FTSQUERY_NEAR:
187904 case FTSQUERY_AND: {
187905 Fts3Expr *pLeft = pExpr->pLeft;
187906 Fts3Expr *pRight = pExpr->pRight;
187907 assert( !pLeft->bDeferred || !pRight->bDeferred );
187908
187909 if( pLeft->bDeferred ){
187910 /* LHS is entirely deferred. So we assume it matches every row.
187911 ** Advance the RHS iterator to find the next row visited. */
187912 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187913 pExpr->iDocid = pRight->iDocid;
187914 pExpr->bEof = pRight->bEof;
187915 }else if( pRight->bDeferred ){
187916 /* RHS is entirely deferred. So we assume it matches every row.
187917 ** Advance the LHS iterator to find the next row visited. */
187918 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187919 pExpr->iDocid = pLeft->iDocid;
187920 pExpr->bEof = pLeft->bEof;
187921 }else{
187922 /* Neither the RHS or LHS are deferred. */
187923 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187924 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187925 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
187926 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
187927 if( iDiff==0 ) break;
187928 if( iDiff<0 ){
187929 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187930 }else{
187931 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187932 }
187933 }
187934 pExpr->iDocid = pLeft->iDocid;
187935 pExpr->bEof = (pLeft->bEof || pRight->bEof);
187936 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
187937 assert( pRight->eType==FTSQUERY_PHRASE );
187938 if( pRight->pPhrase->doclist.aAll ){
187939 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
187940 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
187941 memset(s: pDl->pList, c: 0, n: pDl->nList);
187942 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187943 }
187944 }
187945 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
187946 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
187947 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
187948 memset(s: pDl->pList, c: 0, n: pDl->nList);
187949 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187950 }
187951 }
187952 pRight->bEof = pLeft->bEof = 1;
187953 }
187954 }
187955 break;
187956 }
187957
187958 case FTSQUERY_OR: {
187959 Fts3Expr *pLeft = pExpr->pLeft;
187960 Fts3Expr *pRight = pExpr->pRight;
187961 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
187962
187963 assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
187964 assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid );
187965
187966 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
187967 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187968 }else if( pLeft->bEof || iCmp>0 ){
187969 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187970 }else{
187971 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187972 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187973 }
187974
187975 pExpr->bEof = (pLeft->bEof && pRight->bEof);
187976 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
187977 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
187978 pExpr->iDocid = pLeft->iDocid;
187979 }else{
187980 pExpr->iDocid = pRight->iDocid;
187981 }
187982
187983 break;
187984 }
187985
187986 case FTSQUERY_NOT: {
187987 Fts3Expr *pLeft = pExpr->pLeft;
187988 Fts3Expr *pRight = pExpr->pRight;
187989
187990 if( pRight->bStart==0 ){
187991 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
187992 assert( *pRc!=SQLITE_OK || pRight->bStart );
187993 }
187994
187995 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
187996 if( pLeft->bEof==0 ){
187997 while( !*pRc
187998 && !pRight->bEof
187999 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
188000 ){
188001 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
188002 }
188003 }
188004 pExpr->iDocid = pLeft->iDocid;
188005 pExpr->bEof = pLeft->bEof;
188006 break;
188007 }
188008
188009 default: {
188010 Fts3Phrase *pPhrase = pExpr->pPhrase;
188011 fts3EvalInvalidatePoslist(pPhrase);
188012 *pRc = fts3EvalPhraseNext(pCsr, p: pPhrase, pbEof: &pExpr->bEof);
188013 pExpr->iDocid = pPhrase->doclist.iDocid;
188014 break;
188015 }
188016 }
188017 }
188018}
188019
188020/*
188021** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
188022** cluster, then this function returns 1 immediately.
188023**
188024** Otherwise, it checks if the current row really does match the NEAR
188025** expression, using the data currently stored in the position lists
188026** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
188027**
188028** If the current row is a match, the position list associated with each
188029** phrase in the NEAR expression is edited in place to contain only those
188030** phrase instances sufficiently close to their peers to satisfy all NEAR
188031** constraints. In this case it returns 1. If the NEAR expression does not
188032** match the current row, 0 is returned. The position lists may or may not
188033** be edited if 0 is returned.
188034*/
188035static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
188036 int res = 1;
188037
188038 /* The following block runs if pExpr is the root of a NEAR query.
188039 ** For example, the query:
188040 **
188041 ** "w" NEAR "x" NEAR "y" NEAR "z"
188042 **
188043 ** which is represented in tree form as:
188044 **
188045 ** |
188046 ** +--NEAR--+ <-- root of NEAR query
188047 ** | |
188048 ** +--NEAR--+ "z"
188049 ** | |
188050 ** +--NEAR--+ "y"
188051 ** | |
188052 ** "w" "x"
188053 **
188054 ** The right-hand child of a NEAR node is always a phrase. The
188055 ** left-hand child may be either a phrase or a NEAR node. There are
188056 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
188057 */
188058 if( *pRc==SQLITE_OK
188059 && pExpr->eType==FTSQUERY_NEAR
188060 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
188061 ){
188062 Fts3Expr *p;
188063 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
188064 char *aTmp; /* Temp space for PoslistNearMerge() */
188065
188066 /* Allocate temporary working space. */
188067 for(p=pExpr; p->pLeft; p=p->pLeft){
188068 assert( p->pRight->pPhrase->doclist.nList>0 );
188069 nTmp += p->pRight->pPhrase->doclist.nList;
188070 }
188071 nTmp += p->pPhrase->doclist.nList;
188072 aTmp = sqlite3_malloc64(n: nTmp*2);
188073 if( !aTmp ){
188074 *pRc = SQLITE_NOMEM;
188075 res = 0;
188076 }else{
188077 char *aPoslist = p->pPhrase->doclist.pList;
188078 int nToken = p->pPhrase->nToken;
188079
188080 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
188081 Fts3Phrase *pPhrase = p->pRight->pPhrase;
188082 int nNear = p->nNear;
188083 res = fts3EvalNearTrim(nNear, aTmp, paPoslist: &aPoslist, pnToken: &nToken, pPhrase);
188084 }
188085
188086 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
188087 nToken = pExpr->pRight->pPhrase->nToken;
188088 for(p=pExpr->pLeft; p && res; p=p->pLeft){
188089 int nNear;
188090 Fts3Phrase *pPhrase;
188091 assert( p->pParent && p->pParent->pLeft==p );
188092 nNear = p->pParent->nNear;
188093 pPhrase = (
188094 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
188095 );
188096 res = fts3EvalNearTrim(nNear, aTmp, paPoslist: &aPoslist, pnToken: &nToken, pPhrase);
188097 }
188098 }
188099
188100 sqlite3_free(p: aTmp);
188101 }
188102
188103 return res;
188104}
188105
188106/*
188107** This function is a helper function for sqlite3Fts3EvalTestDeferred().
188108** Assuming no error occurs or has occurred, It returns non-zero if the
188109** expression passed as the second argument matches the row that pCsr
188110** currently points to, or zero if it does not.
188111**
188112** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
188113** If an error occurs during execution of this function, *pRc is set to
188114** the appropriate SQLite error code. In this case the returned value is
188115** undefined.
188116*/
188117static int fts3EvalTestExpr(
188118 Fts3Cursor *pCsr, /* FTS cursor handle */
188119 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
188120 int *pRc /* IN/OUT: Error code */
188121){
188122 int bHit = 1; /* Return value */
188123 if( *pRc==SQLITE_OK ){
188124 switch( pExpr->eType ){
188125 case FTSQUERY_NEAR:
188126 case FTSQUERY_AND:
188127 bHit = (
188128 fts3EvalTestExpr(pCsr, pExpr: pExpr->pLeft, pRc)
188129 && fts3EvalTestExpr(pCsr, pExpr: pExpr->pRight, pRc)
188130 && fts3EvalNearTest(pExpr, pRc)
188131 );
188132
188133 /* If the NEAR expression does not match any rows, zero the doclist for
188134 ** all phrases involved in the NEAR. This is because the snippet(),
188135 ** offsets() and matchinfo() functions are not supposed to recognize
188136 ** any instances of phrases that are part of unmatched NEAR queries.
188137 ** For example if this expression:
188138 **
188139 ** ... MATCH 'a OR (b NEAR c)'
188140 **
188141 ** is matched against a row containing:
188142 **
188143 ** 'a b d e'
188144 **
188145 ** then any snippet() should ony highlight the "a" term, not the "b"
188146 ** (as "b" is part of a non-matching NEAR clause).
188147 */
188148 if( bHit==0
188149 && pExpr->eType==FTSQUERY_NEAR
188150 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
188151 ){
188152 Fts3Expr *p;
188153 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
188154 if( p->pRight->iDocid==pCsr->iPrevId ){
188155 fts3EvalInvalidatePoslist(pPhrase: p->pRight->pPhrase);
188156 }
188157 }
188158 if( p->iDocid==pCsr->iPrevId ){
188159 fts3EvalInvalidatePoslist(pPhrase: p->pPhrase);
188160 }
188161 }
188162
188163 break;
188164
188165 case FTSQUERY_OR: {
188166 int bHit1 = fts3EvalTestExpr(pCsr, pExpr: pExpr->pLeft, pRc);
188167 int bHit2 = fts3EvalTestExpr(pCsr, pExpr: pExpr->pRight, pRc);
188168 bHit = bHit1 || bHit2;
188169 break;
188170 }
188171
188172 case FTSQUERY_NOT:
188173 bHit = (
188174 fts3EvalTestExpr(pCsr, pExpr: pExpr->pLeft, pRc)
188175 && !fts3EvalTestExpr(pCsr, pExpr: pExpr->pRight, pRc)
188176 );
188177 break;
188178
188179 default: {
188180#ifndef SQLITE_DISABLE_FTS4_DEFERRED
188181 if( pCsr->pDeferred && (pExpr->bDeferred || (
188182 pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList
188183 ))){
188184 Fts3Phrase *pPhrase = pExpr->pPhrase;
188185 if( pExpr->bDeferred ){
188186 fts3EvalInvalidatePoslist(pPhrase);
188187 }
188188 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
188189 bHit = (pPhrase->doclist.pList!=0);
188190 pExpr->iDocid = pCsr->iPrevId;
188191 }else
188192#endif
188193 {
188194 bHit = (
188195 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
188196 && pExpr->pPhrase->doclist.nList>0
188197 );
188198 }
188199 break;
188200 }
188201 }
188202 }
188203 return bHit;
188204}
188205
188206/*
188207** This function is called as the second part of each xNext operation when
188208** iterating through the results of a full-text query. At this point the
188209** cursor points to a row that matches the query expression, with the
188210** following caveats:
188211**
188212** * Up until this point, "NEAR" operators in the expression have been
188213** treated as "AND".
188214**
188215** * Deferred tokens have not yet been considered.
188216**
188217** If *pRc is not SQLITE_OK when this function is called, it immediately
188218** returns 0. Otherwise, it tests whether or not after considering NEAR
188219** operators and deferred tokens the current row is still a match for the
188220** expression. It returns 1 if both of the following are true:
188221**
188222** 1. *pRc is SQLITE_OK when this function returns, and
188223**
188224** 2. After scanning the current FTS table row for the deferred tokens,
188225** it is determined that the row does *not* match the query.
188226**
188227** Or, if no error occurs and it seems the current row does match the FTS
188228** query, return 0.
188229*/
188230SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
188231 int rc = *pRc;
188232 int bMiss = 0;
188233 if( rc==SQLITE_OK ){
188234
188235 /* If there are one or more deferred tokens, load the current row into
188236 ** memory and scan it to determine the position list for each deferred
188237 ** token. Then, see if this row is really a match, considering deferred
188238 ** tokens and NEAR operators (neither of which were taken into account
188239 ** earlier, by fts3EvalNextRow()).
188240 */
188241 if( pCsr->pDeferred ){
188242 rc = fts3CursorSeek(pContext: 0, pCsr);
188243 if( rc==SQLITE_OK ){
188244 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
188245 }
188246 }
188247 bMiss = (0==fts3EvalTestExpr(pCsr, pExpr: pCsr->pExpr, pRc: &rc));
188248
188249 /* Free the position-lists accumulated for each deferred token above. */
188250 sqlite3Fts3FreeDeferredDoclists(pCsr);
188251 *pRc = rc;
188252 }
188253 return (rc==SQLITE_OK && bMiss);
188254}
188255
188256/*
188257** Advance to the next document that matches the FTS expression in
188258** Fts3Cursor.pExpr.
188259*/
188260static int fts3EvalNext(Fts3Cursor *pCsr){
188261 int rc = SQLITE_OK; /* Return Code */
188262 Fts3Expr *pExpr = pCsr->pExpr;
188263 assert( pCsr->isEof==0 );
188264 if( pExpr==0 ){
188265 pCsr->isEof = 1;
188266 }else{
188267 do {
188268 if( pCsr->isRequireSeek==0 ){
188269 sqlite3_reset(pStmt: pCsr->pStmt);
188270 }
188271 assert( sqlite3_data_count(pCsr->pStmt)==0 );
188272 fts3EvalNextRow(pCsr, pExpr, pRc: &rc);
188273 pCsr->isEof = pExpr->bEof;
188274 pCsr->isRequireSeek = 1;
188275 pCsr->isMatchinfoNeeded = 1;
188276 pCsr->iPrevId = pExpr->iDocid;
188277 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, pRc: &rc) );
188278 }
188279
188280 /* Check if the cursor is past the end of the docid range specified
188281 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
188282 if( rc==SQLITE_OK && (
188283 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
188284 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
188285 )){
188286 pCsr->isEof = 1;
188287 }
188288
188289 return rc;
188290}
188291
188292/*
188293** Restart interation for expression pExpr so that the next call to
188294** fts3EvalNext() visits the first row. Do not allow incremental
188295** loading or merging of phrase doclists for this iteration.
188296**
188297** If *pRc is other than SQLITE_OK when this function is called, it is
188298** a no-op. If an error occurs within this function, *pRc is set to an
188299** SQLite error code before returning.
188300*/
188301static void fts3EvalRestart(
188302 Fts3Cursor *pCsr,
188303 Fts3Expr *pExpr,
188304 int *pRc
188305){
188306 if( pExpr && *pRc==SQLITE_OK ){
188307 Fts3Phrase *pPhrase = pExpr->pPhrase;
188308
188309 if( pPhrase ){
188310 fts3EvalInvalidatePoslist(pPhrase);
188311 if( pPhrase->bIncr ){
188312 int i;
188313 for(i=0; i<pPhrase->nToken; i++){
188314 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
188315 assert( pToken->pDeferred==0 );
188316 if( pToken->pSegcsr ){
188317 sqlite3Fts3MsrIncrRestart(pCsr: pToken->pSegcsr);
188318 }
188319 }
188320 *pRc = fts3EvalPhraseStart(pCsr, bOptOk: 0, p: pPhrase);
188321 }
188322 pPhrase->doclist.pNextDocid = 0;
188323 pPhrase->doclist.iDocid = 0;
188324 pPhrase->pOrPoslist = 0;
188325 }
188326
188327 pExpr->iDocid = 0;
188328 pExpr->bEof = 0;
188329 pExpr->bStart = 0;
188330
188331 fts3EvalRestart(pCsr, pExpr: pExpr->pLeft, pRc);
188332 fts3EvalRestart(pCsr, pExpr: pExpr->pRight, pRc);
188333 }
188334}
188335
188336/*
188337** After allocating the Fts3Expr.aMI[] array for each phrase in the
188338** expression rooted at pExpr, the cursor iterates through all rows matched
188339** by pExpr, calling this function for each row. This function increments
188340** the values in Fts3Expr.aMI[] according to the position-list currently
188341** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
188342** expression nodes.
188343*/
188344static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
188345 if( pExpr ){
188346 Fts3Phrase *pPhrase = pExpr->pPhrase;
188347 if( pPhrase && pPhrase->doclist.pList ){
188348 int iCol = 0;
188349 char *p = pPhrase->doclist.pList;
188350
188351 do{
188352 u8 c = 0;
188353 int iCnt = 0;
188354 while( 0xFE & (*p | c) ){
188355 if( (c&0x80)==0 ) iCnt++;
188356 c = *p++ & 0x80;
188357 }
188358
188359 /* aMI[iCol*3 + 1] = Number of occurrences
188360 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
188361 */
188362 pExpr->aMI[iCol*3 + 1] += iCnt;
188363 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
188364 if( *p==0x00 ) break;
188365 p++;
188366 p += fts3GetVarint32(p, &iCol);
188367 }while( iCol<nCol );
188368 }
188369
188370 fts3EvalUpdateCounts(pExpr: pExpr->pLeft, nCol);
188371 fts3EvalUpdateCounts(pExpr: pExpr->pRight, nCol);
188372 }
188373}
188374
188375/*
188376** This is an sqlite3Fts3ExprIterate() callback. If the Fts3Expr.aMI[] array
188377** has not yet been allocated, allocate and zero it. Otherwise, just zero
188378** it.
188379*/
188380static int fts3AllocateMSI(Fts3Expr *pExpr, int iPhrase, void *pCtx){
188381 Fts3Table *pTab = (Fts3Table*)pCtx;
188382 UNUSED_PARAMETER(iPhrase);
188383 if( pExpr->aMI==0 ){
188384 pExpr->aMI = (u32 *)sqlite3_malloc64(n: pTab->nColumn * 3 * sizeof(u32));
188385 if( pExpr->aMI==0 ) return SQLITE_NOMEM;
188386 }
188387 memset(s: pExpr->aMI, c: 0, n: pTab->nColumn * 3 * sizeof(u32));
188388 return SQLITE_OK;
188389}
188390
188391/*
188392** Expression pExpr must be of type FTSQUERY_PHRASE.
188393**
188394** If it is not already allocated and populated, this function allocates and
188395** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
188396** of a NEAR expression, then it also allocates and populates the same array
188397** for all other phrases that are part of the NEAR expression.
188398**
188399** SQLITE_OK is returned if the aMI[] array is successfully allocated and
188400** populated. Otherwise, if an error occurs, an SQLite error code is returned.
188401*/
188402static int fts3EvalGatherStats(
188403 Fts3Cursor *pCsr, /* Cursor object */
188404 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
188405){
188406 int rc = SQLITE_OK; /* Return code */
188407
188408 assert( pExpr->eType==FTSQUERY_PHRASE );
188409 if( pExpr->aMI==0 ){
188410 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
188411 Fts3Expr *pRoot; /* Root of NEAR expression */
188412
188413 sqlite3_int64 iPrevId = pCsr->iPrevId;
188414 sqlite3_int64 iDocid;
188415 u8 bEof;
188416
188417 /* Find the root of the NEAR expression */
188418 pRoot = pExpr;
188419 while( pRoot->pParent
188420 && (pRoot->pParent->eType==FTSQUERY_NEAR || pRoot->bDeferred)
188421 ){
188422 pRoot = pRoot->pParent;
188423 }
188424 iDocid = pRoot->iDocid;
188425 bEof = pRoot->bEof;
188426 assert( pRoot->bStart );
188427
188428 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
188429 rc = sqlite3Fts3ExprIterate(pRoot, x: fts3AllocateMSI, (void*)pTab);
188430 if( rc!=SQLITE_OK ) return rc;
188431 fts3EvalRestart(pCsr, pExpr: pRoot, pRc: &rc);
188432
188433 while( pCsr->isEof==0 && rc==SQLITE_OK ){
188434
188435 do {
188436 /* Ensure the %_content statement is reset. */
188437 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pStmt: pCsr->pStmt);
188438 assert( sqlite3_data_count(pCsr->pStmt)==0 );
188439
188440 /* Advance to the next document */
188441 fts3EvalNextRow(pCsr, pExpr: pRoot, pRc: &rc);
188442 pCsr->isEof = pRoot->bEof;
188443 pCsr->isRequireSeek = 1;
188444 pCsr->isMatchinfoNeeded = 1;
188445 pCsr->iPrevId = pRoot->iDocid;
188446 }while( pCsr->isEof==0
188447 && pRoot->eType==FTSQUERY_NEAR
188448 && sqlite3Fts3EvalTestDeferred(pCsr, pRc: &rc)
188449 );
188450
188451 if( rc==SQLITE_OK && pCsr->isEof==0 ){
188452 fts3EvalUpdateCounts(pExpr: pRoot, nCol: pTab->nColumn);
188453 }
188454 }
188455
188456 pCsr->isEof = 0;
188457 pCsr->iPrevId = iPrevId;
188458
188459 if( bEof ){
188460 pRoot->bEof = bEof;
188461 }else{
188462 /* Caution: pRoot may iterate through docids in ascending or descending
188463 ** order. For this reason, even though it seems more defensive, the
188464 ** do loop can not be written:
188465 **
188466 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
188467 */
188468 fts3EvalRestart(pCsr, pExpr: pRoot, pRc: &rc);
188469 do {
188470 fts3EvalNextRow(pCsr, pExpr: pRoot, pRc: &rc);
188471 assert_fts3_nc( pRoot->bEof==0 );
188472 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB;
188473 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
188474 }
188475 }
188476 return rc;
188477}
188478
188479/*
188480** This function is used by the matchinfo() module to query a phrase
188481** expression node for the following information:
188482**
188483** 1. The total number of occurrences of the phrase in each column of
188484** the FTS table (considering all rows), and
188485**
188486** 2. For each column, the number of rows in the table for which the
188487** column contains at least one instance of the phrase.
188488**
188489** If no error occurs, SQLITE_OK is returned and the values for each column
188490** written into the array aiOut as follows:
188491**
188492** aiOut[iCol*3 + 1] = Number of occurrences
188493** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
188494**
188495** Caveats:
188496**
188497** * If a phrase consists entirely of deferred tokens, then all output
188498** values are set to the number of documents in the table. In other
188499** words we assume that very common tokens occur exactly once in each
188500** column of each row of the table.
188501**
188502** * If a phrase contains some deferred tokens (and some non-deferred
188503** tokens), count the potential occurrence identified by considering
188504** the non-deferred tokens instead of actual phrase occurrences.
188505**
188506** * If the phrase is part of a NEAR expression, then only phrase instances
188507** that meet the NEAR constraint are included in the counts.
188508*/
188509SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
188510 Fts3Cursor *pCsr, /* FTS cursor handle */
188511 Fts3Expr *pExpr, /* Phrase expression */
188512 u32 *aiOut /* Array to write results into (see above) */
188513){
188514 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
188515 int rc = SQLITE_OK;
188516 int iCol;
188517
188518 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
188519 assert( pCsr->nDoc>0 );
188520 for(iCol=0; iCol<pTab->nColumn; iCol++){
188521 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
188522 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
188523 }
188524 }else{
188525 rc = fts3EvalGatherStats(pCsr, pExpr);
188526 if( rc==SQLITE_OK ){
188527 assert( pExpr->aMI );
188528 for(iCol=0; iCol<pTab->nColumn; iCol++){
188529 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
188530 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
188531 }
188532 }
188533 }
188534
188535 return rc;
188536}
188537
188538/*
188539** The expression pExpr passed as the second argument to this function
188540** must be of type FTSQUERY_PHRASE.
188541**
188542** The returned value is either NULL or a pointer to a buffer containing
188543** a position-list indicating the occurrences of the phrase in column iCol
188544** of the current row.
188545**
188546** More specifically, the returned buffer contains 1 varint for each
188547** occurrence of the phrase in the column, stored using the normal (delta+2)
188548** compression and is terminated by either an 0x01 or 0x00 byte. For example,
188549** if the requested column contains "a b X c d X X" and the position-list
188550** for 'X' is requested, the buffer returned may contain:
188551**
188552** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
188553**
188554** This function works regardless of whether or not the phrase is deferred,
188555** incremental, or neither.
188556*/
188557SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
188558 Fts3Cursor *pCsr, /* FTS3 cursor object */
188559 Fts3Expr *pExpr, /* Phrase to return doclist for */
188560 int iCol, /* Column to return position list for */
188561 char **ppOut /* OUT: Pointer to position list */
188562){
188563 Fts3Phrase *pPhrase = pExpr->pPhrase;
188564 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
188565 char *pIter;
188566 int iThis;
188567 sqlite3_int64 iDocid;
188568
188569 /* If this phrase is applies specifically to some column other than
188570 ** column iCol, return a NULL pointer. */
188571 *ppOut = 0;
188572 assert( iCol>=0 && iCol<pTab->nColumn );
188573 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
188574 return SQLITE_OK;
188575 }
188576
188577 iDocid = pExpr->iDocid;
188578 pIter = pPhrase->doclist.pList;
188579 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
188580 int rc = SQLITE_OK;
188581 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
188582 int bOr = 0;
188583 u8 bTreeEof = 0;
188584 Fts3Expr *p; /* Used to iterate from pExpr to root */
188585 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
188586 Fts3Expr *pRun; /* Closest non-deferred ancestor of pNear */
188587 int bMatch;
188588
188589 /* Check if this phrase descends from an OR expression node. If not,
188590 ** return NULL. Otherwise, the entry that corresponds to docid
188591 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
188592 ** tree that the node is part of has been marked as EOF, but the node
188593 ** itself is not EOF, then it may point to an earlier entry. */
188594 pNear = pExpr;
188595 for(p=pExpr->pParent; p; p=p->pParent){
188596 if( p->eType==FTSQUERY_OR ) bOr = 1;
188597 if( p->eType==FTSQUERY_NEAR ) pNear = p;
188598 if( p->bEof ) bTreeEof = 1;
188599 }
188600 if( bOr==0 ) return SQLITE_OK;
188601 pRun = pNear;
188602 while( pRun->bDeferred ){
188603 assert( pRun->pParent );
188604 pRun = pRun->pParent;
188605 }
188606
188607 /* This is the descendent of an OR node. In this case we cannot use
188608 ** an incremental phrase. Load the entire doclist for the phrase
188609 ** into memory in this case. */
188610 if( pPhrase->bIncr ){
188611 int bEofSave = pRun->bEof;
188612 fts3EvalRestart(pCsr, pExpr: pRun, pRc: &rc);
188613 while( rc==SQLITE_OK && !pRun->bEof ){
188614 fts3EvalNextRow(pCsr, pExpr: pRun, pRc: &rc);
188615 if( bEofSave==0 && pRun->iDocid==iDocid ) break;
188616 }
188617 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
188618 if( rc==SQLITE_OK && pRun->bEof!=bEofSave ){
188619 rc = FTS_CORRUPT_VTAB;
188620 }
188621 }
188622 if( bTreeEof ){
188623 while( rc==SQLITE_OK && !pRun->bEof ){
188624 fts3EvalNextRow(pCsr, pExpr: pRun, pRc: &rc);
188625 }
188626 }
188627 if( rc!=SQLITE_OK ) return rc;
188628
188629 bMatch = 1;
188630 for(p=pNear; p; p=p->pLeft){
188631 u8 bEof = 0;
188632 Fts3Expr *pTest = p;
188633 Fts3Phrase *pPh;
188634 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
188635 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
188636 assert( pTest->eType==FTSQUERY_PHRASE );
188637 pPh = pTest->pPhrase;
188638
188639 pIter = pPh->pOrPoslist;
188640 iDocid = pPh->iOrDocid;
188641 if( pCsr->bDesc==bDescDoclist ){
188642 bEof = !pPh->doclist.nAll ||
188643 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
188644 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
188645 sqlite3Fts3DoclistNext(
188646 bDescIdx: bDescDoclist, aDoclist: pPh->doclist.aAll, nDoclist: pPh->doclist.nAll,
188647 ppIter: &pIter, piDocid: &iDocid, pbEof: &bEof
188648 );
188649 }
188650 }else{
188651 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
188652 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
188653 int dummy;
188654 sqlite3Fts3DoclistPrev(
188655 bDescIdx: bDescDoclist, aDoclist: pPh->doclist.aAll, nDoclist: pPh->doclist.nAll,
188656 ppIter: &pIter, piDocid: &iDocid, pnList: &dummy, pbEof: &bEof
188657 );
188658 }
188659 }
188660 pPh->pOrPoslist = pIter;
188661 pPh->iOrDocid = iDocid;
188662 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
188663 }
188664
188665 if( bMatch ){
188666 pIter = pPhrase->pOrPoslist;
188667 }else{
188668 pIter = 0;
188669 }
188670 }
188671 if( pIter==0 ) return SQLITE_OK;
188672
188673 if( *pIter==0x01 ){
188674 pIter++;
188675 pIter += fts3GetVarint32(pIter, &iThis);
188676 }else{
188677 iThis = 0;
188678 }
188679 while( iThis<iCol ){
188680 fts3ColumnlistCopy(pp: 0, ppPoslist: &pIter);
188681 if( *pIter==0x00 ) return SQLITE_OK;
188682 pIter++;
188683 pIter += fts3GetVarint32(pIter, &iThis);
188684 }
188685 if( *pIter==0x00 ){
188686 pIter = 0;
188687 }
188688
188689 *ppOut = ((iCol==iThis)?pIter:0);
188690 return SQLITE_OK;
188691}
188692
188693/*
188694** Free all components of the Fts3Phrase structure that were allocated by
188695** the eval module. Specifically, this means to free:
188696**
188697** * the contents of pPhrase->doclist, and
188698** * any Fts3MultiSegReader objects held by phrase tokens.
188699*/
188700SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
188701 if( pPhrase ){
188702 int i;
188703 sqlite3_free(p: pPhrase->doclist.aAll);
188704 fts3EvalInvalidatePoslist(pPhrase);
188705 memset(s: &pPhrase->doclist, c: 0, n: sizeof(Fts3Doclist));
188706 for(i=0; i<pPhrase->nToken; i++){
188707 fts3SegReaderCursorFree(pSegcsr: pPhrase->aToken[i].pSegcsr);
188708 pPhrase->aToken[i].pSegcsr = 0;
188709 }
188710 }
188711}
188712
188713
188714/*
188715** Return SQLITE_CORRUPT_VTAB.
188716*/
188717#ifdef SQLITE_DEBUG
188718SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
188719 return SQLITE_CORRUPT_VTAB;
188720}
188721#endif
188722
188723#if !SQLITE_CORE
188724/*
188725** Initialize API pointer table, if required.
188726*/
188727#ifdef _WIN32
188728__declspec(dllexport)
188729#endif
188730SQLITE_API int sqlite3_fts3_init(
188731 sqlite3 *db,
188732 char **pzErrMsg,
188733 const sqlite3_api_routines *pApi
188734){
188735 SQLITE_EXTENSION_INIT2(pApi)
188736 return sqlite3Fts3Init(db);
188737}
188738#endif
188739
188740#endif
188741
188742/************** End of fts3.c ************************************************/
188743/************** Begin file fts3_aux.c ****************************************/
188744/*
188745** 2011 Jan 27
188746**
188747** The author disclaims copyright to this source code. In place of
188748** a legal notice, here is a blessing:
188749**
188750** May you do good and not evil.
188751** May you find forgiveness for yourself and forgive others.
188752** May you share freely, never taking more than you give.
188753**
188754******************************************************************************
188755**
188756*/
188757/* #include "fts3Int.h" */
188758#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
188759
188760/* #include <string.h> */
188761/* #include <assert.h> */
188762
188763typedef struct Fts3auxTable Fts3auxTable;
188764typedef struct Fts3auxCursor Fts3auxCursor;
188765
188766struct Fts3auxTable {
188767 sqlite3_vtab base; /* Base class used by SQLite core */
188768 Fts3Table *pFts3Tab;
188769};
188770
188771struct Fts3auxCursor {
188772 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
188773 Fts3MultiSegReader csr; /* Must be right after "base" */
188774 Fts3SegFilter filter;
188775 char *zStop;
188776 int nStop; /* Byte-length of string zStop */
188777 int iLangid; /* Language id to query */
188778 int isEof; /* True if cursor is at EOF */
188779 sqlite3_int64 iRowid; /* Current rowid */
188780
188781 int iCol; /* Current value of 'col' column */
188782 int nStat; /* Size of aStat[] array */
188783 struct Fts3auxColstats {
188784 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
188785 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
188786 } *aStat;
188787};
188788
188789/*
188790** Schema of the terms table.
188791*/
188792#define FTS3_AUX_SCHEMA \
188793 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
188794
188795/*
188796** This function does all the work for both the xConnect and xCreate methods.
188797** These tables have no persistent representation of their own, so xConnect
188798** and xCreate are identical operations.
188799*/
188800static int fts3auxConnectMethod(
188801 sqlite3 *db, /* Database connection */
188802 void *pUnused, /* Unused */
188803 int argc, /* Number of elements in argv array */
188804 const char * const *argv, /* xCreate/xConnect argument array */
188805 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
188806 char **pzErr /* OUT: sqlite3_malloc'd error message */
188807){
188808 char const *zDb; /* Name of database (e.g. "main") */
188809 char const *zFts3; /* Name of fts3 table */
188810 int nDb; /* Result of strlen(zDb) */
188811 int nFts3; /* Result of strlen(zFts3) */
188812 sqlite3_int64 nByte; /* Bytes of space to allocate here */
188813 int rc; /* value returned by declare_vtab() */
188814 Fts3auxTable *p; /* Virtual table object to return */
188815
188816 UNUSED_PARAMETER(pUnused);
188817
188818 /* The user should invoke this in one of two forms:
188819 **
188820 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
188821 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
188822 */
188823 if( argc!=4 && argc!=5 ) goto bad_args;
188824
188825 zDb = argv[1];
188826 nDb = (int)strlen(s: zDb);
188827 if( argc==5 ){
188828 if( nDb==4 && 0==sqlite3_strnicmp(zLeft: "temp", zRight: zDb, N: 4) ){
188829 zDb = argv[3];
188830 nDb = (int)strlen(s: zDb);
188831 zFts3 = argv[4];
188832 }else{
188833 goto bad_args;
188834 }
188835 }else{
188836 zFts3 = argv[3];
188837 }
188838 nFts3 = (int)strlen(s: zFts3);
188839
188840 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
188841 if( rc!=SQLITE_OK ) return rc;
188842
188843 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
188844 p = (Fts3auxTable *)sqlite3_malloc64(n: nByte);
188845 if( !p ) return SQLITE_NOMEM;
188846 memset(s: p, c: 0, n: nByte);
188847
188848 p->pFts3Tab = (Fts3Table *)&p[1];
188849 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
188850 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
188851 p->pFts3Tab->db = db;
188852 p->pFts3Tab->nIndex = 1;
188853
188854 memcpy(dest: (char *)p->pFts3Tab->zDb, src: zDb, n: nDb);
188855 memcpy(dest: (char *)p->pFts3Tab->zName, src: zFts3, n: nFts3);
188856 sqlite3Fts3Dequote(z: (char *)p->pFts3Tab->zName);
188857
188858 *ppVtab = (sqlite3_vtab *)p;
188859 return SQLITE_OK;
188860
188861 bad_args:
188862 sqlite3Fts3ErrMsg(pzErr, zFormat: "invalid arguments to fts4aux constructor");
188863 return SQLITE_ERROR;
188864}
188865
188866/*
188867** This function does the work for both the xDisconnect and xDestroy methods.
188868** These tables have no persistent representation of their own, so xDisconnect
188869** and xDestroy are identical operations.
188870*/
188871static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
188872 Fts3auxTable *p = (Fts3auxTable *)pVtab;
188873 Fts3Table *pFts3 = p->pFts3Tab;
188874 int i;
188875
188876 /* Free any prepared statements held */
188877 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
188878 sqlite3_finalize(pStmt: pFts3->aStmt[i]);
188879 }
188880 sqlite3_free(p: pFts3->zSegmentsTbl);
188881 sqlite3_free(p);
188882 return SQLITE_OK;
188883}
188884
188885#define FTS4AUX_EQ_CONSTRAINT 1
188886#define FTS4AUX_GE_CONSTRAINT 2
188887#define FTS4AUX_LE_CONSTRAINT 4
188888
188889/*
188890** xBestIndex - Analyze a WHERE and ORDER BY clause.
188891*/
188892static int fts3auxBestIndexMethod(
188893 sqlite3_vtab *pVTab,
188894 sqlite3_index_info *pInfo
188895){
188896 int i;
188897 int iEq = -1;
188898 int iGe = -1;
188899 int iLe = -1;
188900 int iLangid = -1;
188901 int iNext = 1; /* Next free argvIndex value */
188902
188903 UNUSED_PARAMETER(pVTab);
188904
188905 /* This vtab delivers always results in "ORDER BY term ASC" order. */
188906 if( pInfo->nOrderBy==1
188907 && pInfo->aOrderBy[0].iColumn==0
188908 && pInfo->aOrderBy[0].desc==0
188909 ){
188910 pInfo->orderByConsumed = 1;
188911 }
188912
188913 /* Search for equality and range constraints on the "term" column.
188914 ** And equality constraints on the hidden "languageid" column. */
188915 for(i=0; i<pInfo->nConstraint; i++){
188916 if( pInfo->aConstraint[i].usable ){
188917 int op = pInfo->aConstraint[i].op;
188918 int iCol = pInfo->aConstraint[i].iColumn;
188919
188920 if( iCol==0 ){
188921 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
188922 if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
188923 if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
188924 if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
188925 if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
188926 }
188927 if( iCol==4 ){
188928 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
188929 }
188930 }
188931 }
188932
188933 if( iEq>=0 ){
188934 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
188935 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
188936 pInfo->estimatedCost = 5;
188937 }else{
188938 pInfo->idxNum = 0;
188939 pInfo->estimatedCost = 20000;
188940 if( iGe>=0 ){
188941 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
188942 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
188943 pInfo->estimatedCost /= 2;
188944 }
188945 if( iLe>=0 ){
188946 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
188947 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
188948 pInfo->estimatedCost /= 2;
188949 }
188950 }
188951 if( iLangid>=0 ){
188952 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
188953 pInfo->estimatedCost--;
188954 }
188955
188956 return SQLITE_OK;
188957}
188958
188959/*
188960** xOpen - Open a cursor.
188961*/
188962static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
188963 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
188964
188965 UNUSED_PARAMETER(pVTab);
188966
188967 pCsr = (Fts3auxCursor *)sqlite3_malloc(n: sizeof(Fts3auxCursor));
188968 if( !pCsr ) return SQLITE_NOMEM;
188969 memset(s: pCsr, c: 0, n: sizeof(Fts3auxCursor));
188970
188971 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
188972 return SQLITE_OK;
188973}
188974
188975/*
188976** xClose - Close a cursor.
188977*/
188978static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
188979 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
188980 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
188981
188982 sqlite3Fts3SegmentsClose(pFts3);
188983 sqlite3Fts3SegReaderFinish(&pCsr->csr);
188984 sqlite3_free(p: (void *)pCsr->filter.zTerm);
188985 sqlite3_free(p: pCsr->zStop);
188986 sqlite3_free(p: pCsr->aStat);
188987 sqlite3_free(p: pCsr);
188988 return SQLITE_OK;
188989}
188990
188991static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
188992 if( nSize>pCsr->nStat ){
188993 struct Fts3auxColstats *aNew;
188994 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pOld: pCsr->aStat,
188995 n: sizeof(struct Fts3auxColstats) * nSize
188996 );
188997 if( aNew==0 ) return SQLITE_NOMEM;
188998 memset(s: &aNew[pCsr->nStat], c: 0,
188999 n: sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
189000 );
189001 pCsr->aStat = aNew;
189002 pCsr->nStat = nSize;
189003 }
189004 return SQLITE_OK;
189005}
189006
189007/*
189008** xNext - Advance the cursor to the next row, if any.
189009*/
189010static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
189011 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
189012 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
189013 int rc;
189014
189015 /* Increment our pretend rowid value. */
189016 pCsr->iRowid++;
189017
189018 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
189019 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
189020 }
189021
189022 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
189023 if( rc==SQLITE_ROW ){
189024 int i = 0;
189025 int nDoclist = pCsr->csr.nDoclist;
189026 char *aDoclist = pCsr->csr.aDoclist;
189027 int iCol;
189028
189029 int eState = 0;
189030
189031 if( pCsr->zStop ){
189032 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
189033 int mc = memcmp(s1: pCsr->zStop, s2: pCsr->csr.zTerm, n: n);
189034 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
189035 pCsr->isEof = 1;
189036 return SQLITE_OK;
189037 }
189038 }
189039
189040 if( fts3auxGrowStatArray(pCsr, nSize: 2) ) return SQLITE_NOMEM;
189041 memset(s: pCsr->aStat, c: 0, n: sizeof(struct Fts3auxColstats) * pCsr->nStat);
189042 iCol = 0;
189043 rc = SQLITE_OK;
189044
189045 while( i<nDoclist ){
189046 sqlite3_int64 v = 0;
189047
189048 i += sqlite3Fts3GetVarint(pBuf: &aDoclist[i], v: &v);
189049 switch( eState ){
189050 /* State 0. In this state the integer just read was a docid. */
189051 case 0:
189052 pCsr->aStat[0].nDoc++;
189053 eState = 1;
189054 iCol = 0;
189055 break;
189056
189057 /* State 1. In this state we are expecting either a 1, indicating
189058 ** that the following integer will be a column number, or the
189059 ** start of a position list for column 0.
189060 **
189061 ** The only difference between state 1 and state 2 is that if the
189062 ** integer encountered in state 1 is not 0 or 1, then we need to
189063 ** increment the column 0 "nDoc" count for this term.
189064 */
189065 case 1:
189066 assert( iCol==0 );
189067 if( v>1 ){
189068 pCsr->aStat[1].nDoc++;
189069 }
189070 eState = 2;
189071 /* fall through */
189072
189073 case 2:
189074 if( v==0 ){ /* 0x00. Next integer will be a docid. */
189075 eState = 0;
189076 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
189077 eState = 3;
189078 }else{ /* 2 or greater. A position. */
189079 pCsr->aStat[iCol+1].nOcc++;
189080 pCsr->aStat[0].nOcc++;
189081 }
189082 break;
189083
189084 /* State 3. The integer just read is a column number. */
189085 default: assert( eState==3 );
189086 iCol = (int)v;
189087 if( iCol<1 ){
189088 rc = SQLITE_CORRUPT_VTAB;
189089 break;
189090 }
189091 if( fts3auxGrowStatArray(pCsr, nSize: iCol+2) ) return SQLITE_NOMEM;
189092 pCsr->aStat[iCol+1].nDoc++;
189093 eState = 2;
189094 break;
189095 }
189096 }
189097
189098 pCsr->iCol = 0;
189099 }else{
189100 pCsr->isEof = 1;
189101 }
189102 return rc;
189103}
189104
189105/*
189106** xFilter - Initialize a cursor to point at the start of its data.
189107*/
189108static int fts3auxFilterMethod(
189109 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
189110 int idxNum, /* Strategy index */
189111 const char *idxStr, /* Unused */
189112 int nVal, /* Number of elements in apVal */
189113 sqlite3_value **apVal /* Arguments for the indexing scheme */
189114){
189115 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
189116 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
189117 int rc;
189118 int isScan = 0;
189119 int iLangVal = 0; /* Language id to query */
189120
189121 int iEq = -1; /* Index of term=? value in apVal */
189122 int iGe = -1; /* Index of term>=? value in apVal */
189123 int iLe = -1; /* Index of term<=? value in apVal */
189124 int iLangid = -1; /* Index of languageid=? value in apVal */
189125 int iNext = 0;
189126
189127 UNUSED_PARAMETER(nVal);
189128 UNUSED_PARAMETER(idxStr);
189129
189130 assert( idxStr==0 );
189131 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
189132 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
189133 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
189134 );
189135
189136 if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
189137 iEq = iNext++;
189138 }else{
189139 isScan = 1;
189140 if( idxNum & FTS4AUX_GE_CONSTRAINT ){
189141 iGe = iNext++;
189142 }
189143 if( idxNum & FTS4AUX_LE_CONSTRAINT ){
189144 iLe = iNext++;
189145 }
189146 }
189147 if( iNext<nVal ){
189148 iLangid = iNext++;
189149 }
189150
189151 /* In case this cursor is being reused, close and zero it. */
189152 testcase(pCsr->filter.zTerm);
189153 sqlite3Fts3SegReaderFinish(&pCsr->csr);
189154 sqlite3_free(p: (void *)pCsr->filter.zTerm);
189155 sqlite3_free(p: pCsr->aStat);
189156 sqlite3_free(p: pCsr->zStop);
189157 memset(s: &pCsr->csr, c: 0, n: ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
189158
189159 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
189160 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
189161
189162 if( iEq>=0 || iGe>=0 ){
189163 const unsigned char *zStr = sqlite3_value_text(pVal: apVal[0]);
189164 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
189165 if( zStr ){
189166 pCsr->filter.zTerm = sqlite3_mprintf(zFormat: "%s", zStr);
189167 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
189168 pCsr->filter.nTerm = (int)strlen(s: pCsr->filter.zTerm);
189169 }
189170 }
189171
189172 if( iLe>=0 ){
189173 pCsr->zStop = sqlite3_mprintf(zFormat: "%s", sqlite3_value_text(pVal: apVal[iLe]));
189174 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
189175 pCsr->nStop = (int)strlen(s: pCsr->zStop);
189176 }
189177
189178 if( iLangid>=0 ){
189179 iLangVal = sqlite3_value_int(pVal: apVal[iLangid]);
189180
189181 /* If the user specified a negative value for the languageid, use zero
189182 ** instead. This works, as the "languageid=?" constraint will also
189183 ** be tested by the VDBE layer. The test will always be false (since
189184 ** this module will not return a row with a negative languageid), and
189185 ** so the overall query will return zero rows. */
189186 if( iLangVal<0 ) iLangVal = 0;
189187 }
189188 pCsr->iLangid = iLangVal;
189189
189190 rc = sqlite3Fts3SegReaderCursor(p: pFts3, iLangid: iLangVal, iIndex: 0, FTS3_SEGCURSOR_ALL,
189191 zTerm: pCsr->filter.zTerm, nTerm: pCsr->filter.nTerm, isPrefix: 0, isScan, pCsr: &pCsr->csr
189192 );
189193 if( rc==SQLITE_OK ){
189194 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
189195 }
189196
189197 if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
189198 return rc;
189199}
189200
189201/*
189202** xEof - Return true if the cursor is at EOF, or false otherwise.
189203*/
189204static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
189205 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
189206 return pCsr->isEof;
189207}
189208
189209/*
189210** xColumn - Return a column value.
189211*/
189212static int fts3auxColumnMethod(
189213 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
189214 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
189215 int iCol /* Index of column to read value from */
189216){
189217 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
189218
189219 assert( p->isEof==0 );
189220 switch( iCol ){
189221 case 0: /* term */
189222 sqlite3_result_text(pCtx, z: p->csr.zTerm, n: p->csr.nTerm, SQLITE_TRANSIENT);
189223 break;
189224
189225 case 1: /* col */
189226 if( p->iCol ){
189227 sqlite3_result_int(pCtx, iVal: p->iCol-1);
189228 }else{
189229 sqlite3_result_text(pCtx, z: "*", n: -1, SQLITE_STATIC);
189230 }
189231 break;
189232
189233 case 2: /* documents */
189234 sqlite3_result_int64(pCtx, iVal: p->aStat[p->iCol].nDoc);
189235 break;
189236
189237 case 3: /* occurrences */
189238 sqlite3_result_int64(pCtx, iVal: p->aStat[p->iCol].nOcc);
189239 break;
189240
189241 default: /* languageid */
189242 assert( iCol==4 );
189243 sqlite3_result_int(pCtx, iVal: p->iLangid);
189244 break;
189245 }
189246
189247 return SQLITE_OK;
189248}
189249
189250/*
189251** xRowid - Return the current rowid for the cursor.
189252*/
189253static int fts3auxRowidMethod(
189254 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
189255 sqlite_int64 *pRowid /* OUT: Rowid value */
189256){
189257 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
189258 *pRowid = pCsr->iRowid;
189259 return SQLITE_OK;
189260}
189261
189262/*
189263** Register the fts3aux module with database connection db. Return SQLITE_OK
189264** if successful or an error code if sqlite3_create_module() fails.
189265*/
189266SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
189267 static const sqlite3_module fts3aux_module = {
189268 0, /* iVersion */
189269 fts3auxConnectMethod, /* xCreate */
189270 fts3auxConnectMethod, /* xConnect */
189271 fts3auxBestIndexMethod, /* xBestIndex */
189272 fts3auxDisconnectMethod, /* xDisconnect */
189273 fts3auxDisconnectMethod, /* xDestroy */
189274 fts3auxOpenMethod, /* xOpen */
189275 fts3auxCloseMethod, /* xClose */
189276 fts3auxFilterMethod, /* xFilter */
189277 fts3auxNextMethod, /* xNext */
189278 fts3auxEofMethod, /* xEof */
189279 fts3auxColumnMethod, /* xColumn */
189280 fts3auxRowidMethod, /* xRowid */
189281 0, /* xUpdate */
189282 0, /* xBegin */
189283 0, /* xSync */
189284 0, /* xCommit */
189285 0, /* xRollback */
189286 0, /* xFindFunction */
189287 0, /* xRename */
189288 0, /* xSavepoint */
189289 0, /* xRelease */
189290 0, /* xRollbackTo */
189291 0 /* xShadowName */
189292 };
189293 int rc; /* Return code */
189294
189295 rc = sqlite3_create_module(db, zName: "fts4aux", pModule: &fts3aux_module, pAux: 0);
189296 return rc;
189297}
189298
189299#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
189300
189301/************** End of fts3_aux.c ********************************************/
189302/************** Begin file fts3_expr.c ***************************************/
189303/*
189304** 2008 Nov 28
189305**
189306** The author disclaims copyright to this source code. In place of
189307** a legal notice, here is a blessing:
189308**
189309** May you do good and not evil.
189310** May you find forgiveness for yourself and forgive others.
189311** May you share freely, never taking more than you give.
189312**
189313******************************************************************************
189314**
189315** This module contains code that implements a parser for fts3 query strings
189316** (the right-hand argument to the MATCH operator). Because the supported
189317** syntax is relatively simple, the whole tokenizer/parser system is
189318** hand-coded.
189319*/
189320/* #include "fts3Int.h" */
189321#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
189322
189323/*
189324** By default, this module parses the legacy syntax that has been
189325** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
189326** is defined, then it uses the new syntax. The differences between
189327** the new and the old syntaxes are:
189328**
189329** a) The new syntax supports parenthesis. The old does not.
189330**
189331** b) The new syntax supports the AND and NOT operators. The old does not.
189332**
189333** c) The old syntax supports the "-" token qualifier. This is not
189334** supported by the new syntax (it is replaced by the NOT operator).
189335**
189336** d) When using the old syntax, the OR operator has a greater precedence
189337** than an implicit AND. When using the new, both implicity and explicit
189338** AND operators have a higher precedence than OR.
189339**
189340** If compiled with SQLITE_TEST defined, then this module exports the
189341** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
189342** to zero causes the module to use the old syntax. If it is set to
189343** non-zero the new syntax is activated. This is so both syntaxes can
189344** be tested using a single build of testfixture.
189345**
189346** The following describes the syntax supported by the fts3 MATCH
189347** operator in a similar format to that used by the lemon parser
189348** generator. This module does not use actually lemon, it uses a
189349** custom parser.
189350**
189351** query ::= andexpr (OR andexpr)*.
189352**
189353** andexpr ::= notexpr (AND? notexpr)*.
189354**
189355** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
189356** notexpr ::= LP query RP.
189357**
189358** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
189359**
189360** distance_opt ::= .
189361** distance_opt ::= / INTEGER.
189362**
189363** phrase ::= TOKEN.
189364** phrase ::= COLUMN:TOKEN.
189365** phrase ::= "TOKEN TOKEN TOKEN...".
189366*/
189367
189368#ifdef SQLITE_TEST
189369SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
189370#else
189371# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
189372# define sqlite3_fts3_enable_parentheses 1
189373# else
189374# define sqlite3_fts3_enable_parentheses 0
189375# endif
189376#endif
189377
189378/*
189379** Default span for NEAR operators.
189380*/
189381#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
189382
189383/* #include <string.h> */
189384/* #include <assert.h> */
189385
189386/*
189387** isNot:
189388** This variable is used by function getNextNode(). When getNextNode() is
189389** called, it sets ParseContext.isNot to true if the 'next node' is a
189390** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
189391** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
189392** zero.
189393*/
189394typedef struct ParseContext ParseContext;
189395struct ParseContext {
189396 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
189397 int iLangid; /* Language id used with tokenizer */
189398 const char **azCol; /* Array of column names for fts3 table */
189399 int bFts4; /* True to allow FTS4-only syntax */
189400 int nCol; /* Number of entries in azCol[] */
189401 int iDefaultCol; /* Default column to query */
189402 int isNot; /* True if getNextNode() sees a unary - */
189403 sqlite3_context *pCtx; /* Write error message here */
189404 int nNest; /* Number of nested brackets */
189405};
189406
189407/*
189408** This function is equivalent to the standard isspace() function.
189409**
189410** The standard isspace() can be awkward to use safely, because although it
189411** is defined to accept an argument of type int, its behavior when passed
189412** an integer that falls outside of the range of the unsigned char type
189413** is undefined (and sometimes, "undefined" means segfault). This wrapper
189414** is defined to accept an argument of type char, and always returns 0 for
189415** any values that fall outside of the range of the unsigned char type (i.e.
189416** negative values).
189417*/
189418static int fts3isspace(char c){
189419 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
189420}
189421
189422/*
189423** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
189424** zero the memory before returning a pointer to it. If unsuccessful,
189425** return NULL.
189426*/
189427SQLITE_PRIVATE void *sqlite3Fts3MallocZero(sqlite3_int64 nByte){
189428 void *pRet = sqlite3_malloc64(n: nByte);
189429 if( pRet ) memset(s: pRet, c: 0, n: nByte);
189430 return pRet;
189431}
189432
189433SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
189434 sqlite3_tokenizer *pTokenizer,
189435 int iLangid,
189436 const char *z,
189437 int n,
189438 sqlite3_tokenizer_cursor **ppCsr
189439){
189440 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
189441 sqlite3_tokenizer_cursor *pCsr = 0;
189442 int rc;
189443
189444 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
189445 assert( rc==SQLITE_OK || pCsr==0 );
189446 if( rc==SQLITE_OK ){
189447 pCsr->pTokenizer = pTokenizer;
189448 if( pModule->iVersion>=1 ){
189449 rc = pModule->xLanguageid(pCsr, iLangid);
189450 if( rc!=SQLITE_OK ){
189451 pModule->xClose(pCsr);
189452 pCsr = 0;
189453 }
189454 }
189455 }
189456 *ppCsr = pCsr;
189457 return rc;
189458}
189459
189460/*
189461** Function getNextNode(), which is called by fts3ExprParse(), may itself
189462** call fts3ExprParse(). So this forward declaration is required.
189463*/
189464static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
189465
189466/*
189467** Extract the next token from buffer z (length n) using the tokenizer
189468** and other information (column names etc.) in pParse. Create an Fts3Expr
189469** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
189470** single token and set *ppExpr to point to it. If the end of the buffer is
189471** reached before a token is found, set *ppExpr to zero. It is the
189472** responsibility of the caller to eventually deallocate the allocated
189473** Fts3Expr structure (if any) by passing it to sqlite3_free().
189474**
189475** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
189476** fails.
189477*/
189478static int getNextToken(
189479 ParseContext *pParse, /* fts3 query parse context */
189480 int iCol, /* Value for Fts3Phrase.iColumn */
189481 const char *z, int n, /* Input string */
189482 Fts3Expr **ppExpr, /* OUT: expression */
189483 int *pnConsumed /* OUT: Number of bytes consumed */
189484){
189485 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
189486 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
189487 int rc;
189488 sqlite3_tokenizer_cursor *pCursor;
189489 Fts3Expr *pRet = 0;
189490 int i = 0;
189491
189492 /* Set variable i to the maximum number of bytes of input to tokenize. */
189493 for(i=0; i<n; i++){
189494 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
189495 if( z[i]=='"' ) break;
189496 }
189497
189498 *pnConsumed = i;
189499 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid: pParse->iLangid, z, n: i, ppCsr: &pCursor);
189500 if( rc==SQLITE_OK ){
189501 const char *zToken;
189502 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
189503 sqlite3_int64 nByte; /* total space to allocate */
189504
189505 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
189506 if( rc==SQLITE_OK ){
189507 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
189508 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte);
189509 if( !pRet ){
189510 rc = SQLITE_NOMEM;
189511 }else{
189512 pRet->eType = FTSQUERY_PHRASE;
189513 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
189514 pRet->pPhrase->nToken = 1;
189515 pRet->pPhrase->iColumn = iCol;
189516 pRet->pPhrase->aToken[0].n = nToken;
189517 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
189518 memcpy(dest: pRet->pPhrase->aToken[0].z, src: zToken, n: nToken);
189519
189520 if( iEnd<n && z[iEnd]=='*' ){
189521 pRet->pPhrase->aToken[0].isPrefix = 1;
189522 iEnd++;
189523 }
189524
189525 while( 1 ){
189526 if( !sqlite3_fts3_enable_parentheses
189527 && iStart>0 && z[iStart-1]=='-'
189528 ){
189529 pParse->isNot = 1;
189530 iStart--;
189531 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
189532 pRet->pPhrase->aToken[0].bFirst = 1;
189533 iStart--;
189534 }else{
189535 break;
189536 }
189537 }
189538
189539 }
189540 *pnConsumed = iEnd;
189541 }else if( i && rc==SQLITE_DONE ){
189542 rc = SQLITE_OK;
189543 }
189544
189545 pModule->xClose(pCursor);
189546 }
189547
189548 *ppExpr = pRet;
189549 return rc;
189550}
189551
189552
189553/*
189554** Enlarge a memory allocation. If an out-of-memory allocation occurs,
189555** then free the old allocation.
189556*/
189557static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
189558 void *pRet = sqlite3_realloc64(pOld: pOrig, n: nNew);
189559 if( !pRet ){
189560 sqlite3_free(p: pOrig);
189561 }
189562 return pRet;
189563}
189564
189565/*
189566** Buffer zInput, length nInput, contains the contents of a quoted string
189567** that appeared as part of an fts3 query expression. Neither quote character
189568** is included in the buffer. This function attempts to tokenize the entire
189569** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
189570** containing the results.
189571**
189572** If successful, SQLITE_OK is returned and *ppExpr set to point at the
189573** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
189574** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
189575** to 0.
189576*/
189577static int getNextString(
189578 ParseContext *pParse, /* fts3 query parse context */
189579 const char *zInput, int nInput, /* Input string */
189580 Fts3Expr **ppExpr /* OUT: expression */
189581){
189582 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
189583 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
189584 int rc;
189585 Fts3Expr *p = 0;
189586 sqlite3_tokenizer_cursor *pCursor = 0;
189587 char *zTemp = 0;
189588 int nTemp = 0;
189589
189590 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
189591 int nToken = 0;
189592
189593 /* The final Fts3Expr data structure, including the Fts3Phrase,
189594 ** Fts3PhraseToken structures token buffers are all stored as a single
189595 ** allocation so that the expression can be freed with a single call to
189596 ** sqlite3_free(). Setting this up requires a two pass approach.
189597 **
189598 ** The first pass, in the block below, uses a tokenizer cursor to iterate
189599 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
189600 ** to assemble data in two dynamic buffers:
189601 **
189602 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
189603 ** structure, followed by the array of Fts3PhraseToken
189604 ** structures. This pass only populates the Fts3PhraseToken array.
189605 **
189606 ** Buffer zTemp: Contains copies of all tokens.
189607 **
189608 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
189609 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
189610 ** structures.
189611 */
189612 rc = sqlite3Fts3OpenTokenizer(
189613 pTokenizer, iLangid: pParse->iLangid, z: zInput, n: nInput, ppCsr: &pCursor);
189614 if( rc==SQLITE_OK ){
189615 int ii;
189616 for(ii=0; rc==SQLITE_OK; ii++){
189617 const char *zByte;
189618 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
189619 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
189620 if( rc==SQLITE_OK ){
189621 Fts3PhraseToken *pToken;
189622
189623 p = fts3ReallocOrFree(pOrig: p, nNew: nSpace + ii*sizeof(Fts3PhraseToken));
189624 if( !p ) goto no_mem;
189625
189626 zTemp = fts3ReallocOrFree(pOrig: zTemp, nNew: nTemp + nByte);
189627 if( !zTemp ) goto no_mem;
189628
189629 assert( nToken==ii );
189630 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
189631 memset(s: pToken, c: 0, n: sizeof(Fts3PhraseToken));
189632
189633 memcpy(dest: &zTemp[nTemp], src: zByte, n: nByte);
189634 nTemp += nByte;
189635
189636 pToken->n = nByte;
189637 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
189638 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
189639 nToken = ii+1;
189640 }
189641 }
189642
189643 pModule->xClose(pCursor);
189644 pCursor = 0;
189645 }
189646
189647 if( rc==SQLITE_DONE ){
189648 int jj;
189649 char *zBuf = 0;
189650
189651 p = fts3ReallocOrFree(pOrig: p, nNew: nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
189652 if( !p ) goto no_mem;
189653 memset(s: p, c: 0, n: (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
189654 p->eType = FTSQUERY_PHRASE;
189655 p->pPhrase = (Fts3Phrase *)&p[1];
189656 p->pPhrase->iColumn = pParse->iDefaultCol;
189657 p->pPhrase->nToken = nToken;
189658
189659 zBuf = (char *)&p->pPhrase->aToken[nToken];
189660 if( zTemp ){
189661 memcpy(dest: zBuf, src: zTemp, n: nTemp);
189662 sqlite3_free(p: zTemp);
189663 }else{
189664 assert( nTemp==0 );
189665 }
189666
189667 for(jj=0; jj<p->pPhrase->nToken; jj++){
189668 p->pPhrase->aToken[jj].z = zBuf;
189669 zBuf += p->pPhrase->aToken[jj].n;
189670 }
189671 rc = SQLITE_OK;
189672 }
189673
189674 *ppExpr = p;
189675 return rc;
189676no_mem:
189677
189678 if( pCursor ){
189679 pModule->xClose(pCursor);
189680 }
189681 sqlite3_free(p: zTemp);
189682 sqlite3_free(p);
189683 *ppExpr = 0;
189684 return SQLITE_NOMEM;
189685}
189686
189687/*
189688** The output variable *ppExpr is populated with an allocated Fts3Expr
189689** structure, or set to 0 if the end of the input buffer is reached.
189690**
189691** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
189692** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
189693** If SQLITE_ERROR is returned, pContext is populated with an error message.
189694*/
189695static int getNextNode(
189696 ParseContext *pParse, /* fts3 query parse context */
189697 const char *z, int n, /* Input string */
189698 Fts3Expr **ppExpr, /* OUT: expression */
189699 int *pnConsumed /* OUT: Number of bytes consumed */
189700){
189701 static const struct Fts3Keyword {
189702 char *z; /* Keyword text */
189703 unsigned char n; /* Length of the keyword */
189704 unsigned char parenOnly; /* Only valid in paren mode */
189705 unsigned char eType; /* Keyword code */
189706 } aKeyword[] = {
189707 { "OR" , 2, 0, FTSQUERY_OR },
189708 { "AND", 3, 1, FTSQUERY_AND },
189709 { "NOT", 3, 1, FTSQUERY_NOT },
189710 { "NEAR", 4, 0, FTSQUERY_NEAR }
189711 };
189712 int ii;
189713 int iCol;
189714 int iColLen;
189715 int rc;
189716 Fts3Expr *pRet = 0;
189717
189718 const char *zInput = z;
189719 int nInput = n;
189720
189721 pParse->isNot = 0;
189722
189723 /* Skip over any whitespace before checking for a keyword, an open or
189724 ** close bracket, or a quoted string.
189725 */
189726 while( nInput>0 && fts3isspace(c: *zInput) ){
189727 nInput--;
189728 zInput++;
189729 }
189730 if( nInput==0 ){
189731 return SQLITE_DONE;
189732 }
189733
189734 /* See if we are dealing with a keyword. */
189735 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
189736 const struct Fts3Keyword *pKey = &aKeyword[ii];
189737
189738 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
189739 continue;
189740 }
189741
189742 if( nInput>=pKey->n && 0==memcmp(s1: zInput, s2: pKey->z, n: pKey->n) ){
189743 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
189744 int nKey = pKey->n;
189745 char cNext;
189746
189747 /* If this is a "NEAR" keyword, check for an explicit nearness. */
189748 if( pKey->eType==FTSQUERY_NEAR ){
189749 assert( nKey==4 );
189750 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
189751 nKey += 1+sqlite3Fts3ReadInt(z: &zInput[nKey+1], pnOut: &nNear);
189752 }
189753 }
189754
189755 /* At this point this is probably a keyword. But for that to be true,
189756 ** the next byte must contain either whitespace, an open or close
189757 ** parenthesis, a quote character, or EOF.
189758 */
189759 cNext = zInput[nKey];
189760 if( fts3isspace(c: cNext)
189761 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
189762 ){
189763 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte: sizeof(Fts3Expr));
189764 if( !pRet ){
189765 return SQLITE_NOMEM;
189766 }
189767 pRet->eType = pKey->eType;
189768 pRet->nNear = nNear;
189769 *ppExpr = pRet;
189770 *pnConsumed = (int)((zInput - z) + nKey);
189771 return SQLITE_OK;
189772 }
189773
189774 /* Turns out that wasn't a keyword after all. This happens if the
189775 ** user has supplied a token such as "ORacle". Continue.
189776 */
189777 }
189778 }
189779
189780 /* See if we are dealing with a quoted phrase. If this is the case, then
189781 ** search for the closing quote and pass the whole string to getNextString()
189782 ** for processing. This is easy to do, as fts3 has no syntax for escaping
189783 ** a quote character embedded in a string.
189784 */
189785 if( *zInput=='"' ){
189786 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
189787 *pnConsumed = (int)((zInput - z) + ii + 1);
189788 if( ii==nInput ){
189789 return SQLITE_ERROR;
189790 }
189791 return getNextString(pParse, zInput: &zInput[1], nInput: ii-1, ppExpr);
189792 }
189793
189794 if( sqlite3_fts3_enable_parentheses ){
189795 if( *zInput=='(' ){
189796 int nConsumed = 0;
189797 pParse->nNest++;
189798#if !defined(SQLITE_MAX_EXPR_DEPTH)
189799 if( pParse->nNest>1000 ) return SQLITE_ERROR;
189800#elif SQLITE_MAX_EXPR_DEPTH>0
189801 if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH ) return SQLITE_ERROR;
189802#endif
189803 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
189804 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
189805 return rc;
189806 }else if( *zInput==')' ){
189807 pParse->nNest--;
189808 *pnConsumed = (int)((zInput - z) + 1);
189809 *ppExpr = 0;
189810 return SQLITE_DONE;
189811 }
189812 }
189813
189814 /* If control flows to this point, this must be a regular token, or
189815 ** the end of the input. Read a regular token using the sqlite3_tokenizer
189816 ** interface. Before doing so, figure out if there is an explicit
189817 ** column specifier for the token.
189818 **
189819 ** TODO: Strangely, it is not possible to associate a column specifier
189820 ** with a quoted phrase, only with a single token. Not sure if this was
189821 ** an implementation artifact or an intentional decision when fts3 was
189822 ** first implemented. Whichever it was, this module duplicates the
189823 ** limitation.
189824 */
189825 iCol = pParse->iDefaultCol;
189826 iColLen = 0;
189827 for(ii=0; ii<pParse->nCol; ii++){
189828 const char *zStr = pParse->azCol[ii];
189829 int nStr = (int)strlen(s: zStr);
189830 if( nInput>nStr && zInput[nStr]==':'
189831 && sqlite3_strnicmp(zLeft: zStr, zRight: zInput, N: nStr)==0
189832 ){
189833 iCol = ii;
189834 iColLen = (int)((zInput - z) + nStr + 1);
189835 break;
189836 }
189837 }
189838 rc = getNextToken(pParse, iCol, z: &z[iColLen], n: n-iColLen, ppExpr, pnConsumed);
189839 *pnConsumed += iColLen;
189840 return rc;
189841}
189842
189843/*
189844** The argument is an Fts3Expr structure for a binary operator (any type
189845** except an FTSQUERY_PHRASE). Return an integer value representing the
189846** precedence of the operator. Lower values have a higher precedence (i.e.
189847** group more tightly). For example, in the C language, the == operator
189848** groups more tightly than ||, and would therefore have a higher precedence.
189849**
189850** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
189851** is defined), the order of the operators in precedence from highest to
189852** lowest is:
189853**
189854** NEAR
189855** NOT
189856** AND (including implicit ANDs)
189857** OR
189858**
189859** Note that when using the old query syntax, the OR operator has a higher
189860** precedence than the AND operator.
189861*/
189862static int opPrecedence(Fts3Expr *p){
189863 assert( p->eType!=FTSQUERY_PHRASE );
189864 if( sqlite3_fts3_enable_parentheses ){
189865 return p->eType;
189866 }else if( p->eType==FTSQUERY_NEAR ){
189867 return 1;
189868 }else if( p->eType==FTSQUERY_OR ){
189869 return 2;
189870 }
189871 assert( p->eType==FTSQUERY_AND );
189872 return 3;
189873}
189874
189875/*
189876** Argument ppHead contains a pointer to the current head of a query
189877** expression tree being parsed. pPrev is the expression node most recently
189878** inserted into the tree. This function adds pNew, which is always a binary
189879** operator node, into the expression tree based on the relative precedence
189880** of pNew and the existing nodes of the tree. This may result in the head
189881** of the tree changing, in which case *ppHead is set to the new root node.
189882*/
189883static void insertBinaryOperator(
189884 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
189885 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
189886 Fts3Expr *pNew /* New binary node to insert into expression tree */
189887){
189888 Fts3Expr *pSplit = pPrev;
189889 while( pSplit->pParent && opPrecedence(p: pSplit->pParent)<=opPrecedence(p: pNew) ){
189890 pSplit = pSplit->pParent;
189891 }
189892
189893 if( pSplit->pParent ){
189894 assert( pSplit->pParent->pRight==pSplit );
189895 pSplit->pParent->pRight = pNew;
189896 pNew->pParent = pSplit->pParent;
189897 }else{
189898 *ppHead = pNew;
189899 }
189900 pNew->pLeft = pSplit;
189901 pSplit->pParent = pNew;
189902}
189903
189904/*
189905** Parse the fts3 query expression found in buffer z, length n. This function
189906** returns either when the end of the buffer is reached or an unmatched
189907** closing bracket - ')' - is encountered.
189908**
189909** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
189910** parsed form of the expression and *pnConsumed is set to the number of
189911** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
189912** (out of memory error) or SQLITE_ERROR (parse error) is returned.
189913*/
189914static int fts3ExprParse(
189915 ParseContext *pParse, /* fts3 query parse context */
189916 const char *z, int n, /* Text of MATCH query */
189917 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
189918 int *pnConsumed /* OUT: Number of bytes consumed */
189919){
189920 Fts3Expr *pRet = 0;
189921 Fts3Expr *pPrev = 0;
189922 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
189923 int nIn = n;
189924 const char *zIn = z;
189925 int rc = SQLITE_OK;
189926 int isRequirePhrase = 1;
189927
189928 while( rc==SQLITE_OK ){
189929 Fts3Expr *p = 0;
189930 int nByte = 0;
189931
189932 rc = getNextNode(pParse, z: zIn, n: nIn, ppExpr: &p, pnConsumed: &nByte);
189933 assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
189934 if( rc==SQLITE_OK ){
189935 if( p ){
189936 int isPhrase;
189937
189938 if( !sqlite3_fts3_enable_parentheses
189939 && p->eType==FTSQUERY_PHRASE && pParse->isNot
189940 ){
189941 /* Create an implicit NOT operator. */
189942 Fts3Expr *pNot = sqlite3Fts3MallocZero(nByte: sizeof(Fts3Expr));
189943 if( !pNot ){
189944 sqlite3Fts3ExprFree(p);
189945 rc = SQLITE_NOMEM;
189946 goto exprparse_out;
189947 }
189948 pNot->eType = FTSQUERY_NOT;
189949 pNot->pRight = p;
189950 p->pParent = pNot;
189951 if( pNotBranch ){
189952 pNot->pLeft = pNotBranch;
189953 pNotBranch->pParent = pNot;
189954 }
189955 pNotBranch = pNot;
189956 p = pPrev;
189957 }else{
189958 int eType = p->eType;
189959 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
189960
189961 /* The isRequirePhrase variable is set to true if a phrase or
189962 ** an expression contained in parenthesis is required. If a
189963 ** binary operator (AND, OR, NOT or NEAR) is encounted when
189964 ** isRequirePhrase is set, this is a syntax error.
189965 */
189966 if( !isPhrase && isRequirePhrase ){
189967 sqlite3Fts3ExprFree(p);
189968 rc = SQLITE_ERROR;
189969 goto exprparse_out;
189970 }
189971
189972 if( isPhrase && !isRequirePhrase ){
189973 /* Insert an implicit AND operator. */
189974 Fts3Expr *pAnd;
189975 assert( pRet && pPrev );
189976 pAnd = sqlite3Fts3MallocZero(nByte: sizeof(Fts3Expr));
189977 if( !pAnd ){
189978 sqlite3Fts3ExprFree(p);
189979 rc = SQLITE_NOMEM;
189980 goto exprparse_out;
189981 }
189982 pAnd->eType = FTSQUERY_AND;
189983 insertBinaryOperator(ppHead: &pRet, pPrev, pNew: pAnd);
189984 pPrev = pAnd;
189985 }
189986
189987 /* This test catches attempts to make either operand of a NEAR
189988 ** operator something other than a phrase. For example, either of
189989 ** the following:
189990 **
189991 ** (bracketed expression) NEAR phrase
189992 ** phrase NEAR (bracketed expression)
189993 **
189994 ** Return an error in either case.
189995 */
189996 if( pPrev && (
189997 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
189998 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
189999 )){
190000 sqlite3Fts3ExprFree(p);
190001 rc = SQLITE_ERROR;
190002 goto exprparse_out;
190003 }
190004
190005 if( isPhrase ){
190006 if( pRet ){
190007 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
190008 pPrev->pRight = p;
190009 p->pParent = pPrev;
190010 }else{
190011 pRet = p;
190012 }
190013 }else{
190014 insertBinaryOperator(ppHead: &pRet, pPrev, pNew: p);
190015 }
190016 isRequirePhrase = !isPhrase;
190017 }
190018 pPrev = p;
190019 }
190020 assert( nByte>0 );
190021 }
190022 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
190023 nIn -= nByte;
190024 zIn += nByte;
190025 }
190026
190027 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
190028 rc = SQLITE_ERROR;
190029 }
190030
190031 if( rc==SQLITE_DONE ){
190032 rc = SQLITE_OK;
190033 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
190034 if( !pRet ){
190035 rc = SQLITE_ERROR;
190036 }else{
190037 Fts3Expr *pIter = pNotBranch;
190038 while( pIter->pLeft ){
190039 pIter = pIter->pLeft;
190040 }
190041 pIter->pLeft = pRet;
190042 pRet->pParent = pIter;
190043 pRet = pNotBranch;
190044 }
190045 }
190046 }
190047 *pnConsumed = n - nIn;
190048
190049exprparse_out:
190050 if( rc!=SQLITE_OK ){
190051 sqlite3Fts3ExprFree(pRet);
190052 sqlite3Fts3ExprFree(pNotBranch);
190053 pRet = 0;
190054 }
190055 *ppExpr = pRet;
190056 return rc;
190057}
190058
190059/*
190060** Return SQLITE_ERROR if the maximum depth of the expression tree passed
190061** as the only argument is more than nMaxDepth.
190062*/
190063static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
190064 int rc = SQLITE_OK;
190065 if( p ){
190066 if( nMaxDepth<0 ){
190067 rc = SQLITE_TOOBIG;
190068 }else{
190069 rc = fts3ExprCheckDepth(p: p->pLeft, nMaxDepth: nMaxDepth-1);
190070 if( rc==SQLITE_OK ){
190071 rc = fts3ExprCheckDepth(p: p->pRight, nMaxDepth: nMaxDepth-1);
190072 }
190073 }
190074 }
190075 return rc;
190076}
190077
190078/*
190079** This function attempts to transform the expression tree at (*pp) to
190080** an equivalent but more balanced form. The tree is modified in place.
190081** If successful, SQLITE_OK is returned and (*pp) set to point to the
190082** new root expression node.
190083**
190084** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
190085**
190086** Otherwise, if an error occurs, an SQLite error code is returned and
190087** expression (*pp) freed.
190088*/
190089static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
190090 int rc = SQLITE_OK; /* Return code */
190091 Fts3Expr *pRoot = *pp; /* Initial root node */
190092 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
190093 int eType = pRoot->eType; /* Type of node in this tree */
190094
190095 if( nMaxDepth==0 ){
190096 rc = SQLITE_ERROR;
190097 }
190098
190099 if( rc==SQLITE_OK ){
190100 if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
190101 Fts3Expr **apLeaf;
190102 apLeaf = (Fts3Expr **)sqlite3_malloc64(n: sizeof(Fts3Expr *) * nMaxDepth);
190103 if( 0==apLeaf ){
190104 rc = SQLITE_NOMEM;
190105 }else{
190106 memset(s: apLeaf, c: 0, n: sizeof(Fts3Expr *) * nMaxDepth);
190107 }
190108
190109 if( rc==SQLITE_OK ){
190110 int i;
190111 Fts3Expr *p;
190112
190113 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
190114 for(p=pRoot; p->eType==eType; p=p->pLeft){
190115 assert( p->pParent==0 || p->pParent->pLeft==p );
190116 assert( p->pLeft && p->pRight );
190117 }
190118
190119 /* This loop runs once for each leaf in the tree of eType nodes. */
190120 while( 1 ){
190121 int iLvl;
190122 Fts3Expr *pParent = p->pParent; /* Current parent of p */
190123
190124 assert( pParent==0 || pParent->pLeft==p );
190125 p->pParent = 0;
190126 if( pParent ){
190127 pParent->pLeft = 0;
190128 }else{
190129 pRoot = 0;
190130 }
190131 rc = fts3ExprBalance(pp: &p, nMaxDepth: nMaxDepth-1);
190132 if( rc!=SQLITE_OK ) break;
190133
190134 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
190135 if( apLeaf[iLvl]==0 ){
190136 apLeaf[iLvl] = p;
190137 p = 0;
190138 }else{
190139 assert( pFree );
190140 pFree->pLeft = apLeaf[iLvl];
190141 pFree->pRight = p;
190142 pFree->pLeft->pParent = pFree;
190143 pFree->pRight->pParent = pFree;
190144
190145 p = pFree;
190146 pFree = pFree->pParent;
190147 p->pParent = 0;
190148 apLeaf[iLvl] = 0;
190149 }
190150 }
190151 if( p ){
190152 sqlite3Fts3ExprFree(p);
190153 rc = SQLITE_TOOBIG;
190154 break;
190155 }
190156
190157 /* If that was the last leaf node, break out of the loop */
190158 if( pParent==0 ) break;
190159
190160 /* Set $p to point to the next leaf in the tree of eType nodes */
190161 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
190162
190163 /* Remove pParent from the original tree. */
190164 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
190165 pParent->pRight->pParent = pParent->pParent;
190166 if( pParent->pParent ){
190167 pParent->pParent->pLeft = pParent->pRight;
190168 }else{
190169 assert( pParent==pRoot );
190170 pRoot = pParent->pRight;
190171 }
190172
190173 /* Link pParent into the free node list. It will be used as an
190174 ** internal node of the new tree. */
190175 pParent->pParent = pFree;
190176 pFree = pParent;
190177 }
190178
190179 if( rc==SQLITE_OK ){
190180 p = 0;
190181 for(i=0; i<nMaxDepth; i++){
190182 if( apLeaf[i] ){
190183 if( p==0 ){
190184 p = apLeaf[i];
190185 p->pParent = 0;
190186 }else{
190187 assert( pFree!=0 );
190188 pFree->pRight = p;
190189 pFree->pLeft = apLeaf[i];
190190 pFree->pLeft->pParent = pFree;
190191 pFree->pRight->pParent = pFree;
190192
190193 p = pFree;
190194 pFree = pFree->pParent;
190195 p->pParent = 0;
190196 }
190197 }
190198 }
190199 pRoot = p;
190200 }else{
190201 /* An error occurred. Delete the contents of the apLeaf[] array
190202 ** and pFree list. Everything else is cleaned up by the call to
190203 ** sqlite3Fts3ExprFree(pRoot) below. */
190204 Fts3Expr *pDel;
190205 for(i=0; i<nMaxDepth; i++){
190206 sqlite3Fts3ExprFree(apLeaf[i]);
190207 }
190208 while( (pDel=pFree)!=0 ){
190209 pFree = pDel->pParent;
190210 sqlite3_free(p: pDel);
190211 }
190212 }
190213
190214 assert( pFree==0 );
190215 sqlite3_free( p: apLeaf );
190216 }
190217 }else if( eType==FTSQUERY_NOT ){
190218 Fts3Expr *pLeft = pRoot->pLeft;
190219 Fts3Expr *pRight = pRoot->pRight;
190220
190221 pRoot->pLeft = 0;
190222 pRoot->pRight = 0;
190223 pLeft->pParent = 0;
190224 pRight->pParent = 0;
190225
190226 rc = fts3ExprBalance(pp: &pLeft, nMaxDepth: nMaxDepth-1);
190227 if( rc==SQLITE_OK ){
190228 rc = fts3ExprBalance(pp: &pRight, nMaxDepth: nMaxDepth-1);
190229 }
190230
190231 if( rc!=SQLITE_OK ){
190232 sqlite3Fts3ExprFree(pRight);
190233 sqlite3Fts3ExprFree(pLeft);
190234 }else{
190235 assert( pLeft && pRight );
190236 pRoot->pLeft = pLeft;
190237 pLeft->pParent = pRoot;
190238 pRoot->pRight = pRight;
190239 pRight->pParent = pRoot;
190240 }
190241 }
190242 }
190243
190244 if( rc!=SQLITE_OK ){
190245 sqlite3Fts3ExprFree(pRoot);
190246 pRoot = 0;
190247 }
190248 *pp = pRoot;
190249 return rc;
190250}
190251
190252/*
190253** This function is similar to sqlite3Fts3ExprParse(), with the following
190254** differences:
190255**
190256** 1. It does not do expression rebalancing.
190257** 2. It does not check that the expression does not exceed the
190258** maximum allowable depth.
190259** 3. Even if it fails, *ppExpr may still be set to point to an
190260** expression tree. It should be deleted using sqlite3Fts3ExprFree()
190261** in this case.
190262*/
190263static int fts3ExprParseUnbalanced(
190264 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
190265 int iLangid, /* Language id for tokenizer */
190266 char **azCol, /* Array of column names for fts3 table */
190267 int bFts4, /* True to allow FTS4-only syntax */
190268 int nCol, /* Number of entries in azCol[] */
190269 int iDefaultCol, /* Default column to query */
190270 const char *z, int n, /* Text of MATCH query */
190271 Fts3Expr **ppExpr /* OUT: Parsed query structure */
190272){
190273 int nParsed;
190274 int rc;
190275 ParseContext sParse;
190276
190277 memset(s: &sParse, c: 0, n: sizeof(ParseContext));
190278 sParse.pTokenizer = pTokenizer;
190279 sParse.iLangid = iLangid;
190280 sParse.azCol = (const char **)azCol;
190281 sParse.nCol = nCol;
190282 sParse.iDefaultCol = iDefaultCol;
190283 sParse.bFts4 = bFts4;
190284 if( z==0 ){
190285 *ppExpr = 0;
190286 return SQLITE_OK;
190287 }
190288 if( n<0 ){
190289 n = (int)strlen(s: z);
190290 }
190291 rc = fts3ExprParse(pParse: &sParse, z, n, ppExpr, pnConsumed: &nParsed);
190292 assert( rc==SQLITE_OK || *ppExpr==0 );
190293
190294 /* Check for mismatched parenthesis */
190295 if( rc==SQLITE_OK && sParse.nNest ){
190296 rc = SQLITE_ERROR;
190297 }
190298
190299 return rc;
190300}
190301
190302/*
190303** Parameters z and n contain a pointer to and length of a buffer containing
190304** an fts3 query expression, respectively. This function attempts to parse the
190305** query expression and create a tree of Fts3Expr structures representing the
190306** parsed expression. If successful, *ppExpr is set to point to the head
190307** of the parsed expression tree and SQLITE_OK is returned. If an error
190308** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
190309** error) is returned and *ppExpr is set to 0.
190310**
190311** If parameter n is a negative number, then z is assumed to point to a
190312** nul-terminated string and the length is determined using strlen().
190313**
190314** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
190315** use to normalize query tokens while parsing the expression. The azCol[]
190316** array, which is assumed to contain nCol entries, should contain the names
190317** of each column in the target fts3 table, in order from left to right.
190318** Column names must be nul-terminated strings.
190319**
190320** The iDefaultCol parameter should be passed the index of the table column
190321** that appears on the left-hand-side of the MATCH operator (the default
190322** column to match against for tokens for which a column name is not explicitly
190323** specified as part of the query string), or -1 if tokens may by default
190324** match any table column.
190325*/
190326SQLITE_PRIVATE int sqlite3Fts3ExprParse(
190327 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
190328 int iLangid, /* Language id for tokenizer */
190329 char **azCol, /* Array of column names for fts3 table */
190330 int bFts4, /* True to allow FTS4-only syntax */
190331 int nCol, /* Number of entries in azCol[] */
190332 int iDefaultCol, /* Default column to query */
190333 const char *z, int n, /* Text of MATCH query */
190334 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
190335 char **pzErr /* OUT: Error message (sqlite3_malloc) */
190336){
190337 int rc = fts3ExprParseUnbalanced(
190338 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
190339 );
190340
190341 /* Rebalance the expression. And check that its depth does not exceed
190342 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
190343 if( rc==SQLITE_OK && *ppExpr ){
190344 rc = fts3ExprBalance(pp: ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
190345 if( rc==SQLITE_OK ){
190346 rc = fts3ExprCheckDepth(p: *ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
190347 }
190348 }
190349
190350 if( rc!=SQLITE_OK ){
190351 sqlite3Fts3ExprFree(*ppExpr);
190352 *ppExpr = 0;
190353 if( rc==SQLITE_TOOBIG ){
190354 sqlite3Fts3ErrMsg(pzErr,
190355 zFormat: "FTS expression tree is too large (maximum depth %d)",
190356 SQLITE_FTS3_MAX_EXPR_DEPTH
190357 );
190358 rc = SQLITE_ERROR;
190359 }else if( rc==SQLITE_ERROR ){
190360 sqlite3Fts3ErrMsg(pzErr, zFormat: "malformed MATCH expression: [%s]", z);
190361 }
190362 }
190363
190364 return rc;
190365}
190366
190367/*
190368** Free a single node of an expression tree.
190369*/
190370static void fts3FreeExprNode(Fts3Expr *p){
190371 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
190372 sqlite3Fts3EvalPhraseCleanup(pPhrase: p->pPhrase);
190373 sqlite3_free(p: p->aMI);
190374 sqlite3_free(p);
190375}
190376
190377/*
190378** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
190379**
190380** This function would be simpler if it recursively called itself. But
190381** that would mean passing a sufficiently large expression to ExprParse()
190382** could cause a stack overflow.
190383*/
190384SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
190385 Fts3Expr *p;
190386 assert( pDel==0 || pDel->pParent==0 );
190387 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
190388 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
190389 }
190390 while( p ){
190391 Fts3Expr *pParent = p->pParent;
190392 fts3FreeExprNode(p);
190393 if( pParent && p==pParent->pLeft && pParent->pRight ){
190394 p = pParent->pRight;
190395 while( p && (p->pLeft || p->pRight) ){
190396 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
190397 p = (p->pLeft ? p->pLeft : p->pRight);
190398 }
190399 }else{
190400 p = pParent;
190401 }
190402 }
190403}
190404
190405/****************************************************************************
190406*****************************************************************************
190407** Everything after this point is just test code.
190408*/
190409
190410#ifdef SQLITE_TEST
190411
190412/* #include <stdio.h> */
190413
190414/*
190415** Return a pointer to a buffer containing a text representation of the
190416** expression passed as the first argument. The buffer is obtained from
190417** sqlite3_malloc(). It is the responsibility of the caller to use
190418** sqlite3_free() to release the memory. If an OOM condition is encountered,
190419** NULL is returned.
190420**
190421** If the second argument is not NULL, then its contents are prepended to
190422** the returned expression text and then freed using sqlite3_free().
190423*/
190424static char *exprToString(Fts3Expr *pExpr, char *zBuf){
190425 if( pExpr==0 ){
190426 return sqlite3_mprintf("");
190427 }
190428 switch( pExpr->eType ){
190429 case FTSQUERY_PHRASE: {
190430 Fts3Phrase *pPhrase = pExpr->pPhrase;
190431 int i;
190432 zBuf = sqlite3_mprintf(
190433 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
190434 for(i=0; zBuf && i<pPhrase->nToken; i++){
190435 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
190436 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
190437 (pPhrase->aToken[i].isPrefix?"+":"")
190438 );
190439 }
190440 return zBuf;
190441 }
190442
190443 case FTSQUERY_NEAR:
190444 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
190445 break;
190446 case FTSQUERY_NOT:
190447 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
190448 break;
190449 case FTSQUERY_AND:
190450 zBuf = sqlite3_mprintf("%zAND ", zBuf);
190451 break;
190452 case FTSQUERY_OR:
190453 zBuf = sqlite3_mprintf("%zOR ", zBuf);
190454 break;
190455 }
190456
190457 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
190458 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
190459 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
190460
190461 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
190462 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
190463
190464 return zBuf;
190465}
190466
190467/*
190468** This is the implementation of a scalar SQL function used to test the
190469** expression parser. It should be called as follows:
190470**
190471** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
190472**
190473** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
190474** to parse the query expression (see README.tokenizers). The second argument
190475** is the query expression to parse. Each subsequent argument is the name
190476** of a column of the fts3 table that the query expression may refer to.
190477** For example:
190478**
190479** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
190480*/
190481static void fts3ExprTestCommon(
190482 int bRebalance,
190483 sqlite3_context *context,
190484 int argc,
190485 sqlite3_value **argv
190486){
190487 sqlite3_tokenizer *pTokenizer = 0;
190488 int rc;
190489 char **azCol = 0;
190490 const char *zExpr;
190491 int nExpr;
190492 int nCol;
190493 int ii;
190494 Fts3Expr *pExpr;
190495 char *zBuf = 0;
190496 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
190497 const char *zTokenizer = 0;
190498 char *zErr = 0;
190499
190500 if( argc<3 ){
190501 sqlite3_result_error(context,
190502 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
190503 );
190504 return;
190505 }
190506
190507 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
190508 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
190509 if( rc!=SQLITE_OK ){
190510 if( rc==SQLITE_NOMEM ){
190511 sqlite3_result_error_nomem(context);
190512 }else{
190513 sqlite3_result_error(context, zErr, -1);
190514 }
190515 sqlite3_free(zErr);
190516 return;
190517 }
190518
190519 zExpr = (const char *)sqlite3_value_text(argv[1]);
190520 nExpr = sqlite3_value_bytes(argv[1]);
190521 nCol = argc-2;
190522 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
190523 if( !azCol ){
190524 sqlite3_result_error_nomem(context);
190525 goto exprtest_out;
190526 }
190527 for(ii=0; ii<nCol; ii++){
190528 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
190529 }
190530
190531 if( bRebalance ){
190532 char *zDummy = 0;
190533 rc = sqlite3Fts3ExprParse(
190534 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
190535 );
190536 assert( rc==SQLITE_OK || pExpr==0 );
190537 sqlite3_free(zDummy);
190538 }else{
190539 rc = fts3ExprParseUnbalanced(
190540 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
190541 );
190542 }
190543
190544 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
190545 sqlite3Fts3ExprFree(pExpr);
190546 sqlite3_result_error(context, "Error parsing expression", -1);
190547 }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
190548 sqlite3_result_error_nomem(context);
190549 }else{
190550 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
190551 sqlite3_free(zBuf);
190552 }
190553
190554 sqlite3Fts3ExprFree(pExpr);
190555
190556exprtest_out:
190557 if( pTokenizer ){
190558 rc = pTokenizer->pModule->xDestroy(pTokenizer);
190559 }
190560 sqlite3_free(azCol);
190561}
190562
190563static void fts3ExprTest(
190564 sqlite3_context *context,
190565 int argc,
190566 sqlite3_value **argv
190567){
190568 fts3ExprTestCommon(0, context, argc, argv);
190569}
190570static void fts3ExprTestRebalance(
190571 sqlite3_context *context,
190572 int argc,
190573 sqlite3_value **argv
190574){
190575 fts3ExprTestCommon(1, context, argc, argv);
190576}
190577
190578/*
190579** Register the query expression parser test function fts3_exprtest()
190580** with database connection db.
190581*/
190582SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
190583 int rc = sqlite3_create_function(
190584 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
190585 );
190586 if( rc==SQLITE_OK ){
190587 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
190588 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
190589 );
190590 }
190591 return rc;
190592}
190593
190594#endif
190595#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
190596
190597/************** End of fts3_expr.c *******************************************/
190598/************** Begin file fts3_hash.c ***************************************/
190599/*
190600** 2001 September 22
190601**
190602** The author disclaims copyright to this source code. In place of
190603** a legal notice, here is a blessing:
190604**
190605** May you do good and not evil.
190606** May you find forgiveness for yourself and forgive others.
190607** May you share freely, never taking more than you give.
190608**
190609*************************************************************************
190610** This is the implementation of generic hash-tables used in SQLite.
190611** We've modified it slightly to serve as a standalone hash table
190612** implementation for the full-text indexing module.
190613*/
190614
190615/*
190616** The code in this file is only compiled if:
190617**
190618** * The FTS3 module is being built as an extension
190619** (in which case SQLITE_CORE is not defined), or
190620**
190621** * The FTS3 module is being built into the core of
190622** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
190623*/
190624/* #include "fts3Int.h" */
190625#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
190626
190627/* #include <assert.h> */
190628/* #include <stdlib.h> */
190629/* #include <string.h> */
190630
190631/* #include "fts3_hash.h" */
190632
190633/*
190634** Malloc and Free functions
190635*/
190636static void *fts3HashMalloc(sqlite3_int64 n){
190637 void *p = sqlite3_malloc64(n);
190638 if( p ){
190639 memset(s: p, c: 0, n: n);
190640 }
190641 return p;
190642}
190643static void fts3HashFree(void *p){
190644 sqlite3_free(p);
190645}
190646
190647/* Turn bulk memory into a hash table object by initializing the
190648** fields of the Hash structure.
190649**
190650** "pNew" is a pointer to the hash table that is to be initialized.
190651** keyClass is one of the constants
190652** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
190653** determines what kind of key the hash table will use. "copyKey" is
190654** true if the hash table should make its own private copy of keys and
190655** false if it should just use the supplied pointer.
190656*/
190657SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
190658 assert( pNew!=0 );
190659 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
190660 pNew->keyClass = keyClass;
190661 pNew->copyKey = copyKey;
190662 pNew->first = 0;
190663 pNew->count = 0;
190664 pNew->htsize = 0;
190665 pNew->ht = 0;
190666}
190667
190668/* Remove all entries from a hash table. Reclaim all memory.
190669** Call this routine to delete a hash table or to reset a hash table
190670** to the empty state.
190671*/
190672SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
190673 Fts3HashElem *elem; /* For looping over all elements of the table */
190674
190675 assert( pH!=0 );
190676 elem = pH->first;
190677 pH->first = 0;
190678 fts3HashFree(p: pH->ht);
190679 pH->ht = 0;
190680 pH->htsize = 0;
190681 while( elem ){
190682 Fts3HashElem *next_elem = elem->next;
190683 if( pH->copyKey && elem->pKey ){
190684 fts3HashFree(p: elem->pKey);
190685 }
190686 fts3HashFree(p: elem);
190687 elem = next_elem;
190688 }
190689 pH->count = 0;
190690}
190691
190692/*
190693** Hash and comparison functions when the mode is FTS3_HASH_STRING
190694*/
190695static int fts3StrHash(const void *pKey, int nKey){
190696 const char *z = (const char *)pKey;
190697 unsigned h = 0;
190698 if( nKey<=0 ) nKey = (int) strlen(s: z);
190699 while( nKey > 0 ){
190700 h = (h<<3) ^ h ^ *z++;
190701 nKey--;
190702 }
190703 return (int)(h & 0x7fffffff);
190704}
190705static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
190706 if( n1!=n2 ) return 1;
190707 return strncmp(s1: (const char*)pKey1,s2: (const char*)pKey2,n: n1);
190708}
190709
190710/*
190711** Hash and comparison functions when the mode is FTS3_HASH_BINARY
190712*/
190713static int fts3BinHash(const void *pKey, int nKey){
190714 int h = 0;
190715 const char *z = (const char *)pKey;
190716 while( nKey-- > 0 ){
190717 h = (h<<3) ^ h ^ *(z++);
190718 }
190719 return h & 0x7fffffff;
190720}
190721static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
190722 if( n1!=n2 ) return 1;
190723 return memcmp(s1: pKey1,s2: pKey2,n: n1);
190724}
190725
190726/*
190727** Return a pointer to the appropriate hash function given the key class.
190728**
190729** The C syntax in this function definition may be unfamilar to some
190730** programmers, so we provide the following additional explanation:
190731**
190732** The name of the function is "ftsHashFunction". The function takes a
190733** single parameter "keyClass". The return value of ftsHashFunction()
190734** is a pointer to another function. Specifically, the return value
190735** of ftsHashFunction() is a pointer to a function that takes two parameters
190736** with types "const void*" and "int" and returns an "int".
190737*/
190738static int (*ftsHashFunction(int keyClass))(const void*,int){
190739 if( keyClass==FTS3_HASH_STRING ){
190740 return &fts3StrHash;
190741 }else{
190742 assert( keyClass==FTS3_HASH_BINARY );
190743 return &fts3BinHash;
190744 }
190745}
190746
190747/*
190748** Return a pointer to the appropriate hash function given the key class.
190749**
190750** For help in interpreted the obscure C code in the function definition,
190751** see the header comment on the previous function.
190752*/
190753static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
190754 if( keyClass==FTS3_HASH_STRING ){
190755 return &fts3StrCompare;
190756 }else{
190757 assert( keyClass==FTS3_HASH_BINARY );
190758 return &fts3BinCompare;
190759 }
190760}
190761
190762/* Link an element into the hash table
190763*/
190764static void fts3HashInsertElement(
190765 Fts3Hash *pH, /* The complete hash table */
190766 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
190767 Fts3HashElem *pNew /* The element to be inserted */
190768){
190769 Fts3HashElem *pHead; /* First element already in pEntry */
190770 pHead = pEntry->chain;
190771 if( pHead ){
190772 pNew->next = pHead;
190773 pNew->prev = pHead->prev;
190774 if( pHead->prev ){ pHead->prev->next = pNew; }
190775 else { pH->first = pNew; }
190776 pHead->prev = pNew;
190777 }else{
190778 pNew->next = pH->first;
190779 if( pH->first ){ pH->first->prev = pNew; }
190780 pNew->prev = 0;
190781 pH->first = pNew;
190782 }
190783 pEntry->count++;
190784 pEntry->chain = pNew;
190785}
190786
190787
190788/* Resize the hash table so that it cantains "new_size" buckets.
190789** "new_size" must be a power of 2. The hash table might fail
190790** to resize if sqliteMalloc() fails.
190791**
190792** Return non-zero if a memory allocation error occurs.
190793*/
190794static int fts3Rehash(Fts3Hash *pH, int new_size){
190795 struct _fts3ht *new_ht; /* The new hash table */
190796 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
190797 int (*xHash)(const void*,int); /* The hash function */
190798
190799 assert( (new_size & (new_size-1))==0 );
190800 new_ht = (struct _fts3ht *)fts3HashMalloc( n: new_size*sizeof(struct _fts3ht) );
190801 if( new_ht==0 ) return 1;
190802 fts3HashFree(p: pH->ht);
190803 pH->ht = new_ht;
190804 pH->htsize = new_size;
190805 xHash = ftsHashFunction(keyClass: pH->keyClass);
190806 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
190807 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
190808 next_elem = elem->next;
190809 fts3HashInsertElement(pH, pEntry: &new_ht[h], pNew: elem);
190810 }
190811 return 0;
190812}
190813
190814/* This function (for internal use only) locates an element in an
190815** hash table that matches the given key. The hash for this key has
190816** already been computed and is passed as the 4th parameter.
190817*/
190818static Fts3HashElem *fts3FindElementByHash(
190819 const Fts3Hash *pH, /* The pH to be searched */
190820 const void *pKey, /* The key we are searching for */
190821 int nKey,
190822 int h /* The hash for this key. */
190823){
190824 Fts3HashElem *elem; /* Used to loop thru the element list */
190825 int count; /* Number of elements left to test */
190826 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
190827
190828 if( pH->ht ){
190829 struct _fts3ht *pEntry = &pH->ht[h];
190830 elem = pEntry->chain;
190831 count = pEntry->count;
190832 xCompare = ftsCompareFunction(keyClass: pH->keyClass);
190833 while( count-- && elem ){
190834 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
190835 return elem;
190836 }
190837 elem = elem->next;
190838 }
190839 }
190840 return 0;
190841}
190842
190843/* Remove a single entry from the hash table given a pointer to that
190844** element and a hash on the element's key.
190845*/
190846static void fts3RemoveElementByHash(
190847 Fts3Hash *pH, /* The pH containing "elem" */
190848 Fts3HashElem* elem, /* The element to be removed from the pH */
190849 int h /* Hash value for the element */
190850){
190851 struct _fts3ht *pEntry;
190852 if( elem->prev ){
190853 elem->prev->next = elem->next;
190854 }else{
190855 pH->first = elem->next;
190856 }
190857 if( elem->next ){
190858 elem->next->prev = elem->prev;
190859 }
190860 pEntry = &pH->ht[h];
190861 if( pEntry->chain==elem ){
190862 pEntry->chain = elem->next;
190863 }
190864 pEntry->count--;
190865 if( pEntry->count<=0 ){
190866 pEntry->chain = 0;
190867 }
190868 if( pH->copyKey && elem->pKey ){
190869 fts3HashFree(p: elem->pKey);
190870 }
190871 fts3HashFree( p: elem );
190872 pH->count--;
190873 if( pH->count<=0 ){
190874 assert( pH->first==0 );
190875 assert( pH->count==0 );
190876 fts3HashClear(pH);
190877 }
190878}
190879
190880SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
190881 const Fts3Hash *pH,
190882 const void *pKey,
190883 int nKey
190884){
190885 int h; /* A hash on key */
190886 int (*xHash)(const void*,int); /* The hash function */
190887
190888 if( pH==0 || pH->ht==0 ) return 0;
190889 xHash = ftsHashFunction(keyClass: pH->keyClass);
190890 assert( xHash!=0 );
190891 h = (*xHash)(pKey,nKey);
190892 assert( (pH->htsize & (pH->htsize-1))==0 );
190893 return fts3FindElementByHash(pH,pKey,nKey, h: h & (pH->htsize-1));
190894}
190895
190896/*
190897** Attempt to locate an element of the hash table pH with a key
190898** that matches pKey,nKey. Return the data for this element if it is
190899** found, or NULL if there is no match.
190900*/
190901SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
190902 Fts3HashElem *pElem; /* The element that matches key (if any) */
190903
190904 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
190905 return pElem ? pElem->data : 0;
190906}
190907
190908/* Insert an element into the hash table pH. The key is pKey,nKey
190909** and the data is "data".
190910**
190911** If no element exists with a matching key, then a new
190912** element is created. A copy of the key is made if the copyKey
190913** flag is set. NULL is returned.
190914**
190915** If another element already exists with the same key, then the
190916** new data replaces the old data and the old data is returned.
190917** The key is not copied in this instance. If a malloc fails, then
190918** the new data is returned and the hash table is unchanged.
190919**
190920** If the "data" parameter to this function is NULL, then the
190921** element corresponding to "key" is removed from the hash table.
190922*/
190923SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
190924 Fts3Hash *pH, /* The hash table to insert into */
190925 const void *pKey, /* The key */
190926 int nKey, /* Number of bytes in the key */
190927 void *data /* The data */
190928){
190929 int hraw; /* Raw hash value of the key */
190930 int h; /* the hash of the key modulo hash table size */
190931 Fts3HashElem *elem; /* Used to loop thru the element list */
190932 Fts3HashElem *new_elem; /* New element added to the pH */
190933 int (*xHash)(const void*,int); /* The hash function */
190934
190935 assert( pH!=0 );
190936 xHash = ftsHashFunction(keyClass: pH->keyClass);
190937 assert( xHash!=0 );
190938 hraw = (*xHash)(pKey, nKey);
190939 assert( (pH->htsize & (pH->htsize-1))==0 );
190940 h = hraw & (pH->htsize-1);
190941 elem = fts3FindElementByHash(pH,pKey,nKey,h);
190942 if( elem ){
190943 void *old_data = elem->data;
190944 if( data==0 ){
190945 fts3RemoveElementByHash(pH,elem,h);
190946 }else{
190947 elem->data = data;
190948 }
190949 return old_data;
190950 }
190951 if( data==0 ) return 0;
190952 if( (pH->htsize==0 && fts3Rehash(pH,new_size: 8))
190953 || (pH->count>=pH->htsize && fts3Rehash(pH, new_size: pH->htsize*2))
190954 ){
190955 pH->count = 0;
190956 return data;
190957 }
190958 assert( pH->htsize>0 );
190959 new_elem = (Fts3HashElem*)fts3HashMalloc( n: sizeof(Fts3HashElem) );
190960 if( new_elem==0 ) return data;
190961 if( pH->copyKey && pKey!=0 ){
190962 new_elem->pKey = fts3HashMalloc( n: nKey );
190963 if( new_elem->pKey==0 ){
190964 fts3HashFree(p: new_elem);
190965 return data;
190966 }
190967 memcpy(dest: (void*)new_elem->pKey, src: pKey, n: nKey);
190968 }else{
190969 new_elem->pKey = (void*)pKey;
190970 }
190971 new_elem->nKey = nKey;
190972 pH->count++;
190973 assert( pH->htsize>0 );
190974 assert( (pH->htsize & (pH->htsize-1))==0 );
190975 h = hraw & (pH->htsize-1);
190976 fts3HashInsertElement(pH, pEntry: &pH->ht[h], pNew: new_elem);
190977 new_elem->data = data;
190978 return 0;
190979}
190980
190981#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
190982
190983/************** End of fts3_hash.c *******************************************/
190984/************** Begin file fts3_porter.c *************************************/
190985/*
190986** 2006 September 30
190987**
190988** The author disclaims copyright to this source code. In place of
190989** a legal notice, here is a blessing:
190990**
190991** May you do good and not evil.
190992** May you find forgiveness for yourself and forgive others.
190993** May you share freely, never taking more than you give.
190994**
190995*************************************************************************
190996** Implementation of the full-text-search tokenizer that implements
190997** a Porter stemmer.
190998*/
190999
191000/*
191001** The code in this file is only compiled if:
191002**
191003** * The FTS3 module is being built as an extension
191004** (in which case SQLITE_CORE is not defined), or
191005**
191006** * The FTS3 module is being built into the core of
191007** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
191008*/
191009/* #include "fts3Int.h" */
191010#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
191011
191012/* #include <assert.h> */
191013/* #include <stdlib.h> */
191014/* #include <stdio.h> */
191015/* #include <string.h> */
191016
191017/* #include "fts3_tokenizer.h" */
191018
191019/*
191020** Class derived from sqlite3_tokenizer
191021*/
191022typedef struct porter_tokenizer {
191023 sqlite3_tokenizer base; /* Base class */
191024} porter_tokenizer;
191025
191026/*
191027** Class derived from sqlite3_tokenizer_cursor
191028*/
191029typedef struct porter_tokenizer_cursor {
191030 sqlite3_tokenizer_cursor base;
191031 const char *zInput; /* input we are tokenizing */
191032 int nInput; /* size of the input */
191033 int iOffset; /* current position in zInput */
191034 int iToken; /* index of next token to be returned */
191035 char *zToken; /* storage for current token */
191036 int nAllocated; /* space allocated to zToken buffer */
191037} porter_tokenizer_cursor;
191038
191039
191040/*
191041** Create a new tokenizer instance.
191042*/
191043static int porterCreate(
191044 int argc, const char * const *argv,
191045 sqlite3_tokenizer **ppTokenizer
191046){
191047 porter_tokenizer *t;
191048
191049 UNUSED_PARAMETER(argc);
191050 UNUSED_PARAMETER(argv);
191051
191052 t = (porter_tokenizer *) sqlite3_malloc(n: sizeof(*t));
191053 if( t==NULL ) return SQLITE_NOMEM;
191054 memset(s: t, c: 0, n: sizeof(*t));
191055 *ppTokenizer = &t->base;
191056 return SQLITE_OK;
191057}
191058
191059/*
191060** Destroy a tokenizer
191061*/
191062static int porterDestroy(sqlite3_tokenizer *pTokenizer){
191063 sqlite3_free(p: pTokenizer);
191064 return SQLITE_OK;
191065}
191066
191067/*
191068** Prepare to begin tokenizing a particular string. The input
191069** string to be tokenized is zInput[0..nInput-1]. A cursor
191070** used to incrementally tokenize this string is returned in
191071** *ppCursor.
191072*/
191073static int porterOpen(
191074 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
191075 const char *zInput, int nInput, /* String to be tokenized */
191076 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
191077){
191078 porter_tokenizer_cursor *c;
191079
191080 UNUSED_PARAMETER(pTokenizer);
191081
191082 c = (porter_tokenizer_cursor *) sqlite3_malloc(n: sizeof(*c));
191083 if( c==NULL ) return SQLITE_NOMEM;
191084
191085 c->zInput = zInput;
191086 if( zInput==0 ){
191087 c->nInput = 0;
191088 }else if( nInput<0 ){
191089 c->nInput = (int)strlen(s: zInput);
191090 }else{
191091 c->nInput = nInput;
191092 }
191093 c->iOffset = 0; /* start tokenizing at the beginning */
191094 c->iToken = 0;
191095 c->zToken = NULL; /* no space allocated, yet. */
191096 c->nAllocated = 0;
191097
191098 *ppCursor = &c->base;
191099 return SQLITE_OK;
191100}
191101
191102/*
191103** Close a tokenization cursor previously opened by a call to
191104** porterOpen() above.
191105*/
191106static int porterClose(sqlite3_tokenizer_cursor *pCursor){
191107 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
191108 sqlite3_free(p: c->zToken);
191109 sqlite3_free(p: c);
191110 return SQLITE_OK;
191111}
191112/*
191113** Vowel or consonant
191114*/
191115static const char cType[] = {
191116 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
191117 1, 1, 1, 2, 1
191118};
191119
191120/*
191121** isConsonant() and isVowel() determine if their first character in
191122** the string they point to is a consonant or a vowel, according
191123** to Porter ruls.
191124**
191125** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
191126** 'Y' is a consonant unless it follows another consonant,
191127** in which case it is a vowel.
191128**
191129** In these routine, the letters are in reverse order. So the 'y' rule
191130** is that 'y' is a consonant unless it is followed by another
191131** consonent.
191132*/
191133static int isVowel(const char*);
191134static int isConsonant(const char *z){
191135 int j;
191136 char x = *z;
191137 if( x==0 ) return 0;
191138 assert( x>='a' && x<='z' );
191139 j = cType[x-'a'];
191140 if( j<2 ) return j;
191141 return z[1]==0 || isVowel(z + 1);
191142}
191143static int isVowel(const char *z){
191144 int j;
191145 char x = *z;
191146 if( x==0 ) return 0;
191147 assert( x>='a' && x<='z' );
191148 j = cType[x-'a'];
191149 if( j<2 ) return 1-j;
191150 return isConsonant(z: z + 1);
191151}
191152
191153/*
191154** Let any sequence of one or more vowels be represented by V and let
191155** C be sequence of one or more consonants. Then every word can be
191156** represented as:
191157**
191158** [C] (VC){m} [V]
191159**
191160** In prose: A word is an optional consonant followed by zero or
191161** vowel-consonant pairs followed by an optional vowel. "m" is the
191162** number of vowel consonant pairs. This routine computes the value
191163** of m for the first i bytes of a word.
191164**
191165** Return true if the m-value for z is 1 or more. In other words,
191166** return true if z contains at least one vowel that is followed
191167** by a consonant.
191168**
191169** In this routine z[] is in reverse order. So we are really looking
191170** for an instance of a consonant followed by a vowel.
191171*/
191172static int m_gt_0(const char *z){
191173 while( isVowel(z) ){ z++; }
191174 if( *z==0 ) return 0;
191175 while( isConsonant(z) ){ z++; }
191176 return *z!=0;
191177}
191178
191179/* Like mgt0 above except we are looking for a value of m which is
191180** exactly 1
191181*/
191182static int m_eq_1(const char *z){
191183 while( isVowel(z) ){ z++; }
191184 if( *z==0 ) return 0;
191185 while( isConsonant(z) ){ z++; }
191186 if( *z==0 ) return 0;
191187 while( isVowel(z) ){ z++; }
191188 if( *z==0 ) return 1;
191189 while( isConsonant(z) ){ z++; }
191190 return *z==0;
191191}
191192
191193/* Like mgt0 above except we are looking for a value of m>1 instead
191194** or m>0
191195*/
191196static int m_gt_1(const char *z){
191197 while( isVowel(z) ){ z++; }
191198 if( *z==0 ) return 0;
191199 while( isConsonant(z) ){ z++; }
191200 if( *z==0 ) return 0;
191201 while( isVowel(z) ){ z++; }
191202 if( *z==0 ) return 0;
191203 while( isConsonant(z) ){ z++; }
191204 return *z!=0;
191205}
191206
191207/*
191208** Return TRUE if there is a vowel anywhere within z[0..n-1]
191209*/
191210static int hasVowel(const char *z){
191211 while( isConsonant(z) ){ z++; }
191212 return *z!=0;
191213}
191214
191215/*
191216** Return TRUE if the word ends in a double consonant.
191217**
191218** The text is reversed here. So we are really looking at
191219** the first two characters of z[].
191220*/
191221static int doubleConsonant(const char *z){
191222 return isConsonant(z) && z[0]==z[1];
191223}
191224
191225/*
191226** Return TRUE if the word ends with three letters which
191227** are consonant-vowel-consonent and where the final consonant
191228** is not 'w', 'x', or 'y'.
191229**
191230** The word is reversed here. So we are really checking the
191231** first three letters and the first one cannot be in [wxy].
191232*/
191233static int star_oh(const char *z){
191234 return
191235 isConsonant(z) &&
191236 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
191237 isVowel(z: z+1) &&
191238 isConsonant(z: z+2);
191239}
191240
191241/*
191242** If the word ends with zFrom and xCond() is true for the stem
191243** of the word that preceeds the zFrom ending, then change the
191244** ending to zTo.
191245**
191246** The input word *pz and zFrom are both in reverse order. zTo
191247** is in normal order.
191248**
191249** Return TRUE if zFrom matches. Return FALSE if zFrom does not
191250** match. Not that TRUE is returned even if xCond() fails and
191251** no substitution occurs.
191252*/
191253static int stem(
191254 char **pz, /* The word being stemmed (Reversed) */
191255 const char *zFrom, /* If the ending matches this... (Reversed) */
191256 const char *zTo, /* ... change the ending to this (not reversed) */
191257 int (*xCond)(const char*) /* Condition that must be true */
191258){
191259 char *z = *pz;
191260 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
191261 if( *zFrom!=0 ) return 0;
191262 if( xCond && !xCond(z) ) return 1;
191263 while( *zTo ){
191264 *(--z) = *(zTo++);
191265 }
191266 *pz = z;
191267 return 1;
191268}
191269
191270/*
191271** This is the fallback stemmer used when the porter stemmer is
191272** inappropriate. The input word is copied into the output with
191273** US-ASCII case folding. If the input word is too long (more
191274** than 20 bytes if it contains no digits or more than 6 bytes if
191275** it contains digits) then word is truncated to 20 or 6 bytes
191276** by taking 10 or 3 bytes from the beginning and end.
191277*/
191278static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
191279 int i, mx, j;
191280 int hasDigit = 0;
191281 for(i=0; i<nIn; i++){
191282 char c = zIn[i];
191283 if( c>='A' && c<='Z' ){
191284 zOut[i] = c - 'A' + 'a';
191285 }else{
191286 if( c>='0' && c<='9' ) hasDigit = 1;
191287 zOut[i] = c;
191288 }
191289 }
191290 mx = hasDigit ? 3 : 10;
191291 if( nIn>mx*2 ){
191292 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
191293 zOut[j] = zOut[i];
191294 }
191295 i = j;
191296 }
191297 zOut[i] = 0;
191298 *pnOut = i;
191299}
191300
191301
191302/*
191303** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
191304** zOut is at least big enough to hold nIn bytes. Write the actual
191305** size of the output word (exclusive of the '\0' terminator) into *pnOut.
191306**
191307** Any upper-case characters in the US-ASCII character set ([A-Z])
191308** are converted to lower case. Upper-case UTF characters are
191309** unchanged.
191310**
191311** Words that are longer than about 20 bytes are stemmed by retaining
191312** a few bytes from the beginning and the end of the word. If the
191313** word contains digits, 3 bytes are taken from the beginning and
191314** 3 bytes from the end. For long words without digits, 10 bytes
191315** are taken from each end. US-ASCII case folding still applies.
191316**
191317** If the input word contains not digits but does characters not
191318** in [a-zA-Z] then no stemming is attempted and this routine just
191319** copies the input into the input into the output with US-ASCII
191320** case folding.
191321**
191322** Stemming never increases the length of the word. So there is
191323** no chance of overflowing the zOut buffer.
191324*/
191325static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
191326 int i, j;
191327 char zReverse[28];
191328 char *z, *z2;
191329 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
191330 /* The word is too big or too small for the porter stemmer.
191331 ** Fallback to the copy stemmer */
191332 copy_stemmer(zIn, nIn, zOut, pnOut);
191333 return;
191334 }
191335 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
191336 char c = zIn[i];
191337 if( c>='A' && c<='Z' ){
191338 zReverse[j] = c + 'a' - 'A';
191339 }else if( c>='a' && c<='z' ){
191340 zReverse[j] = c;
191341 }else{
191342 /* The use of a character not in [a-zA-Z] means that we fallback
191343 ** to the copy stemmer */
191344 copy_stemmer(zIn, nIn, zOut, pnOut);
191345 return;
191346 }
191347 }
191348 memset(s: &zReverse[sizeof(zReverse)-5], c: 0, n: 5);
191349 z = &zReverse[j+1];
191350
191351
191352 /* Step 1a */
191353 if( z[0]=='s' ){
191354 if(
191355 !stem(pz: &z, zFrom: "sess", zTo: "ss", xCond: 0) &&
191356 !stem(pz: &z, zFrom: "sei", zTo: "i", xCond: 0) &&
191357 !stem(pz: &z, zFrom: "ss", zTo: "ss", xCond: 0)
191358 ){
191359 z++;
191360 }
191361 }
191362
191363 /* Step 1b */
191364 z2 = z;
191365 if( stem(pz: &z, zFrom: "dee", zTo: "ee", xCond: m_gt_0) ){
191366 /* Do nothing. The work was all in the test */
191367 }else if(
191368 (stem(pz: &z, zFrom: "gni", zTo: "", xCond: hasVowel) || stem(pz: &z, zFrom: "de", zTo: "", xCond: hasVowel))
191369 && z!=z2
191370 ){
191371 if( stem(pz: &z, zFrom: "ta", zTo: "ate", xCond: 0) ||
191372 stem(pz: &z, zFrom: "lb", zTo: "ble", xCond: 0) ||
191373 stem(pz: &z, zFrom: "zi", zTo: "ize", xCond: 0) ){
191374 /* Do nothing. The work was all in the test */
191375 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
191376 z++;
191377 }else if( m_eq_1(z) && star_oh(z) ){
191378 *(--z) = 'e';
191379 }
191380 }
191381
191382 /* Step 1c */
191383 if( z[0]=='y' && hasVowel(z: z+1) ){
191384 z[0] = 'i';
191385 }
191386
191387 /* Step 2 */
191388 switch( z[1] ){
191389 case 'a':
191390 if( !stem(pz: &z, zFrom: "lanoita", zTo: "ate", xCond: m_gt_0) ){
191391 stem(pz: &z, zFrom: "lanoit", zTo: "tion", xCond: m_gt_0);
191392 }
191393 break;
191394 case 'c':
191395 if( !stem(pz: &z, zFrom: "icne", zTo: "ence", xCond: m_gt_0) ){
191396 stem(pz: &z, zFrom: "icna", zTo: "ance", xCond: m_gt_0);
191397 }
191398 break;
191399 case 'e':
191400 stem(pz: &z, zFrom: "rezi", zTo: "ize", xCond: m_gt_0);
191401 break;
191402 case 'g':
191403 stem(pz: &z, zFrom: "igol", zTo: "log", xCond: m_gt_0);
191404 break;
191405 case 'l':
191406 if( !stem(pz: &z, zFrom: "ilb", zTo: "ble", xCond: m_gt_0)
191407 && !stem(pz: &z, zFrom: "illa", zTo: "al", xCond: m_gt_0)
191408 && !stem(pz: &z, zFrom: "iltne", zTo: "ent", xCond: m_gt_0)
191409 && !stem(pz: &z, zFrom: "ile", zTo: "e", xCond: m_gt_0)
191410 ){
191411 stem(pz: &z, zFrom: "ilsuo", zTo: "ous", xCond: m_gt_0);
191412 }
191413 break;
191414 case 'o':
191415 if( !stem(pz: &z, zFrom: "noitazi", zTo: "ize", xCond: m_gt_0)
191416 && !stem(pz: &z, zFrom: "noita", zTo: "ate", xCond: m_gt_0)
191417 ){
191418 stem(pz: &z, zFrom: "rota", zTo: "ate", xCond: m_gt_0);
191419 }
191420 break;
191421 case 's':
191422 if( !stem(pz: &z, zFrom: "msila", zTo: "al", xCond: m_gt_0)
191423 && !stem(pz: &z, zFrom: "ssenevi", zTo: "ive", xCond: m_gt_0)
191424 && !stem(pz: &z, zFrom: "ssenluf", zTo: "ful", xCond: m_gt_0)
191425 ){
191426 stem(pz: &z, zFrom: "ssensuo", zTo: "ous", xCond: m_gt_0);
191427 }
191428 break;
191429 case 't':
191430 if( !stem(pz: &z, zFrom: "itila", zTo: "al", xCond: m_gt_0)
191431 && !stem(pz: &z, zFrom: "itivi", zTo: "ive", xCond: m_gt_0)
191432 ){
191433 stem(pz: &z, zFrom: "itilib", zTo: "ble", xCond: m_gt_0);
191434 }
191435 break;
191436 }
191437
191438 /* Step 3 */
191439 switch( z[0] ){
191440 case 'e':
191441 if( !stem(pz: &z, zFrom: "etaci", zTo: "ic", xCond: m_gt_0)
191442 && !stem(pz: &z, zFrom: "evita", zTo: "", xCond: m_gt_0)
191443 ){
191444 stem(pz: &z, zFrom: "ezila", zTo: "al", xCond: m_gt_0);
191445 }
191446 break;
191447 case 'i':
191448 stem(pz: &z, zFrom: "itici", zTo: "ic", xCond: m_gt_0);
191449 break;
191450 case 'l':
191451 if( !stem(pz: &z, zFrom: "laci", zTo: "ic", xCond: m_gt_0) ){
191452 stem(pz: &z, zFrom: "luf", zTo: "", xCond: m_gt_0);
191453 }
191454 break;
191455 case 's':
191456 stem(pz: &z, zFrom: "ssen", zTo: "", xCond: m_gt_0);
191457 break;
191458 }
191459
191460 /* Step 4 */
191461 switch( z[1] ){
191462 case 'a':
191463 if( z[0]=='l' && m_gt_1(z: z+2) ){
191464 z += 2;
191465 }
191466 break;
191467 case 'c':
191468 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z: z+4) ){
191469 z += 4;
191470 }
191471 break;
191472 case 'e':
191473 if( z[0]=='r' && m_gt_1(z: z+2) ){
191474 z += 2;
191475 }
191476 break;
191477 case 'i':
191478 if( z[0]=='c' && m_gt_1(z: z+2) ){
191479 z += 2;
191480 }
191481 break;
191482 case 'l':
191483 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z: z+4) ){
191484 z += 4;
191485 }
191486 break;
191487 case 'n':
191488 if( z[0]=='t' ){
191489 if( z[2]=='a' ){
191490 if( m_gt_1(z: z+3) ){
191491 z += 3;
191492 }
191493 }else if( z[2]=='e' ){
191494 if( !stem(pz: &z, zFrom: "tneme", zTo: "", xCond: m_gt_1)
191495 && !stem(pz: &z, zFrom: "tnem", zTo: "", xCond: m_gt_1)
191496 ){
191497 stem(pz: &z, zFrom: "tne", zTo: "", xCond: m_gt_1);
191498 }
191499 }
191500 }
191501 break;
191502 case 'o':
191503 if( z[0]=='u' ){
191504 if( m_gt_1(z: z+2) ){
191505 z += 2;
191506 }
191507 }else if( z[3]=='s' || z[3]=='t' ){
191508 stem(pz: &z, zFrom: "noi", zTo: "", xCond: m_gt_1);
191509 }
191510 break;
191511 case 's':
191512 if( z[0]=='m' && z[2]=='i' && m_gt_1(z: z+3) ){
191513 z += 3;
191514 }
191515 break;
191516 case 't':
191517 if( !stem(pz: &z, zFrom: "eta", zTo: "", xCond: m_gt_1) ){
191518 stem(pz: &z, zFrom: "iti", zTo: "", xCond: m_gt_1);
191519 }
191520 break;
191521 case 'u':
191522 if( z[0]=='s' && z[2]=='o' && m_gt_1(z: z+3) ){
191523 z += 3;
191524 }
191525 break;
191526 case 'v':
191527 case 'z':
191528 if( z[0]=='e' && z[2]=='i' && m_gt_1(z: z+3) ){
191529 z += 3;
191530 }
191531 break;
191532 }
191533
191534 /* Step 5a */
191535 if( z[0]=='e' ){
191536 if( m_gt_1(z: z+1) ){
191537 z++;
191538 }else if( m_eq_1(z: z+1) && !star_oh(z: z+1) ){
191539 z++;
191540 }
191541 }
191542
191543 /* Step 5b */
191544 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
191545 z++;
191546 }
191547
191548 /* z[] is now the stemmed word in reverse order. Flip it back
191549 ** around into forward order and return.
191550 */
191551 *pnOut = i = (int)strlen(s: z);
191552 zOut[i] = 0;
191553 while( *z ){
191554 zOut[--i] = *(z++);
191555 }
191556}
191557
191558/*
191559** Characters that can be part of a token. We assume any character
191560** whose value is greater than 0x80 (any UTF character) can be
191561** part of a token. In other words, delimiters all must have
191562** values of 0x7f or lower.
191563*/
191564static const char porterIdChar[] = {
191565/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
191566 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
191567 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
191568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
191569 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
191570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
191571};
191572#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
191573
191574/*
191575** Extract the next token from a tokenization cursor. The cursor must
191576** have been opened by a prior call to porterOpen().
191577*/
191578static int porterNext(
191579 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
191580 const char **pzToken, /* OUT: *pzToken is the token text */
191581 int *pnBytes, /* OUT: Number of bytes in token */
191582 int *piStartOffset, /* OUT: Starting offset of token */
191583 int *piEndOffset, /* OUT: Ending offset of token */
191584 int *piPosition /* OUT: Position integer of token */
191585){
191586 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
191587 const char *z = c->zInput;
191588
191589 while( c->iOffset<c->nInput ){
191590 int iStartOffset, ch;
191591
191592 /* Scan past delimiter characters */
191593 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
191594 c->iOffset++;
191595 }
191596
191597 /* Count non-delimiter characters. */
191598 iStartOffset = c->iOffset;
191599 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
191600 c->iOffset++;
191601 }
191602
191603 if( c->iOffset>iStartOffset ){
191604 int n = c->iOffset-iStartOffset;
191605 if( n>c->nAllocated ){
191606 char *pNew;
191607 c->nAllocated = n+20;
191608 pNew = sqlite3_realloc64(pOld: c->zToken, n: c->nAllocated);
191609 if( !pNew ) return SQLITE_NOMEM;
191610 c->zToken = pNew;
191611 }
191612 porter_stemmer(zIn: &z[iStartOffset], nIn: n, zOut: c->zToken, pnOut: pnBytes);
191613 *pzToken = c->zToken;
191614 *piStartOffset = iStartOffset;
191615 *piEndOffset = c->iOffset;
191616 *piPosition = c->iToken++;
191617 return SQLITE_OK;
191618 }
191619 }
191620 return SQLITE_DONE;
191621}
191622
191623/*
191624** The set of routines that implement the porter-stemmer tokenizer
191625*/
191626static const sqlite3_tokenizer_module porterTokenizerModule = {
191627 0,
191628 porterCreate,
191629 porterDestroy,
191630 porterOpen,
191631 porterClose,
191632 porterNext,
191633 0
191634};
191635
191636/*
191637** Allocate a new porter tokenizer. Return a pointer to the new
191638** tokenizer in *ppModule
191639*/
191640SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
191641 sqlite3_tokenizer_module const**ppModule
191642){
191643 *ppModule = &porterTokenizerModule;
191644}
191645
191646#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
191647
191648/************** End of fts3_porter.c *****************************************/
191649/************** Begin file fts3_tokenizer.c **********************************/
191650/*
191651** 2007 June 22
191652**
191653** The author disclaims copyright to this source code. In place of
191654** a legal notice, here is a blessing:
191655**
191656** May you do good and not evil.
191657** May you find forgiveness for yourself and forgive others.
191658** May you share freely, never taking more than you give.
191659**
191660******************************************************************************
191661**
191662** This is part of an SQLite module implementing full-text search.
191663** This particular file implements the generic tokenizer interface.
191664*/
191665
191666/*
191667** The code in this file is only compiled if:
191668**
191669** * The FTS3 module is being built as an extension
191670** (in which case SQLITE_CORE is not defined), or
191671**
191672** * The FTS3 module is being built into the core of
191673** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
191674*/
191675/* #include "fts3Int.h" */
191676#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
191677
191678/* #include <assert.h> */
191679/* #include <string.h> */
191680
191681/*
191682** Return true if the two-argument version of fts3_tokenizer()
191683** has been activated via a prior call to sqlite3_db_config(db,
191684** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
191685*/
191686static int fts3TokenizerEnabled(sqlite3_context *context){
191687 sqlite3 *db = sqlite3_context_db_handle(p: context);
191688 int isEnabled = 0;
191689 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
191690 return isEnabled;
191691}
191692
191693/*
191694** Implementation of the SQL scalar function for accessing the underlying
191695** hash table. This function may be called as follows:
191696**
191697** SELECT <function-name>(<key-name>);
191698** SELECT <function-name>(<key-name>, <pointer>);
191699**
191700** where <function-name> is the name passed as the second argument
191701** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
191702**
191703** If the <pointer> argument is specified, it must be a blob value
191704** containing a pointer to be stored as the hash data corresponding
191705** to the string <key-name>. If <pointer> is not specified, then
191706** the string <key-name> must already exist in the has table. Otherwise,
191707** an error is returned.
191708**
191709** Whether or not the <pointer> argument is specified, the value returned
191710** is a blob containing the pointer stored as the hash data corresponding
191711** to string <key-name> (after the hash-table is updated, if applicable).
191712*/
191713static void fts3TokenizerFunc(
191714 sqlite3_context *context,
191715 int argc,
191716 sqlite3_value **argv
191717){
191718 Fts3Hash *pHash;
191719 void *pPtr = 0;
191720 const unsigned char *zName;
191721 int nName;
191722
191723 assert( argc==1 || argc==2 );
191724
191725 pHash = (Fts3Hash *)sqlite3_user_data(p: context);
191726
191727 zName = sqlite3_value_text(pVal: argv[0]);
191728 nName = sqlite3_value_bytes(pVal: argv[0])+1;
191729
191730 if( argc==2 ){
191731 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(pVal: argv[1]) ){
191732 void *pOld;
191733 int n = sqlite3_value_bytes(pVal: argv[1]);
191734 if( zName==0 || n!=sizeof(pPtr) ){
191735 sqlite3_result_error(pCtx: context, z: "argument type mismatch", n: -1);
191736 return;
191737 }
191738 pPtr = *(void **)sqlite3_value_blob(pVal: argv[1]);
191739 pOld = sqlite3Fts3HashInsert(pH: pHash, pKey: (void *)zName, nKey: nName, data: pPtr);
191740 if( pOld==pPtr ){
191741 sqlite3_result_error(pCtx: context, z: "out of memory", n: -1);
191742 }
191743 }else{
191744 sqlite3_result_error(pCtx: context, z: "fts3tokenize disabled", n: -1);
191745 return;
191746 }
191747 }else{
191748 if( zName ){
191749 pPtr = sqlite3Fts3HashFind(pH: pHash, pKey: zName, nKey: nName);
191750 }
191751 if( !pPtr ){
191752 char *zErr = sqlite3_mprintf(zFormat: "unknown tokenizer: %s", zName);
191753 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
191754 sqlite3_free(p: zErr);
191755 return;
191756 }
191757 }
191758 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(pVal: argv[0]) ){
191759 sqlite3_result_blob(pCtx: context, z: (void *)&pPtr, n: sizeof(pPtr), SQLITE_TRANSIENT);
191760 }
191761}
191762
191763SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
191764 static const char isFtsIdChar[] = {
191765 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
191766 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
191767 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
191768 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
191769 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
191770 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
191771 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
191772 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
191773 };
191774 return (c&0x80 || isFtsIdChar[(int)(c)]);
191775}
191776
191777SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
191778 const char *z1;
191779 const char *z2 = 0;
191780
191781 /* Find the start of the next token. */
191782 z1 = zStr;
191783 while( z2==0 ){
191784 char c = *z1;
191785 switch( c ){
191786 case '\0': return 0; /* No more tokens here */
191787 case '\'':
191788 case '"':
191789 case '`': {
191790 z2 = z1;
191791 while( *++z2 && (*z2!=c || *++z2==c) );
191792 break;
191793 }
191794 case '[':
191795 z2 = &z1[1];
191796 while( *z2 && z2[0]!=']' ) z2++;
191797 if( *z2 ) z2++;
191798 break;
191799
191800 default:
191801 if( sqlite3Fts3IsIdChar(c: *z1) ){
191802 z2 = &z1[1];
191803 while( sqlite3Fts3IsIdChar(c: *z2) ) z2++;
191804 }else{
191805 z1++;
191806 }
191807 }
191808 }
191809
191810 *pn = (int)(z2-z1);
191811 return z1;
191812}
191813
191814SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
191815 Fts3Hash *pHash, /* Tokenizer hash table */
191816 const char *zArg, /* Tokenizer name */
191817 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
191818 char **pzErr /* OUT: Set to malloced error message */
191819){
191820 int rc;
191821 char *z = (char *)zArg;
191822 int n = 0;
191823 char *zCopy;
191824 char *zEnd; /* Pointer to nul-term of zCopy */
191825 sqlite3_tokenizer_module *m;
191826
191827 zCopy = sqlite3_mprintf(zFormat: "%s", zArg);
191828 if( !zCopy ) return SQLITE_NOMEM;
191829 zEnd = &zCopy[strlen(s: zCopy)];
191830
191831 z = (char *)sqlite3Fts3NextToken(zStr: zCopy, pn: &n);
191832 if( z==0 ){
191833 assert( n==0 );
191834 z = zCopy;
191835 }
191836 z[n] = '\0';
191837 sqlite3Fts3Dequote(z);
191838
191839 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pH: pHash,pKey: z,nKey: (int)strlen(s: z)+1);
191840 if( !m ){
191841 sqlite3Fts3ErrMsg(pzErr, zFormat: "unknown tokenizer: %s", z);
191842 rc = SQLITE_ERROR;
191843 }else{
191844 char const **aArg = 0;
191845 int iArg = 0;
191846 z = &z[n+1];
191847 while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(zStr: z, pn: &n))) ){
191848 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
191849 char const **aNew = (const char **)sqlite3_realloc64(pOld: (void *)aArg, n: nNew);
191850 if( !aNew ){
191851 sqlite3_free(p: zCopy);
191852 sqlite3_free(p: (void *)aArg);
191853 return SQLITE_NOMEM;
191854 }
191855 aArg = aNew;
191856 aArg[iArg++] = z;
191857 z[n] = '\0';
191858 sqlite3Fts3Dequote(z);
191859 z = &z[n+1];
191860 }
191861 rc = m->xCreate(iArg, aArg, ppTok);
191862 assert( rc!=SQLITE_OK || *ppTok );
191863 if( rc!=SQLITE_OK ){
191864 sqlite3Fts3ErrMsg(pzErr, zFormat: "unknown tokenizer");
191865 }else{
191866 (*ppTok)->pModule = m;
191867 }
191868 sqlite3_free(p: (void *)aArg);
191869 }
191870
191871 sqlite3_free(p: zCopy);
191872 return rc;
191873}
191874
191875
191876#ifdef SQLITE_TEST
191877
191878#if defined(INCLUDE_SQLITE_TCL_H)
191879# include "sqlite_tcl.h"
191880#else
191881# include "tcl.h"
191882#endif
191883/* #include <string.h> */
191884
191885/*
191886** Implementation of a special SQL scalar function for testing tokenizers
191887** designed to be used in concert with the Tcl testing framework. This
191888** function must be called with two or more arguments:
191889**
191890** SELECT <function-name>(<key-name>, ..., <input-string>);
191891**
191892** where <function-name> is the name passed as the second argument
191893** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
191894** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
191895**
191896** The return value is a string that may be interpreted as a Tcl
191897** list. For each token in the <input-string>, three elements are
191898** added to the returned list. The first is the token position, the
191899** second is the token text (folded, stemmed, etc.) and the third is the
191900** substring of <input-string> associated with the token. For example,
191901** using the built-in "simple" tokenizer:
191902**
191903** SELECT fts_tokenizer_test('simple', 'I don't see how');
191904**
191905** will return the string:
191906**
191907** "{0 i I 1 dont don't 2 see see 3 how how}"
191908**
191909*/
191910static void testFunc(
191911 sqlite3_context *context,
191912 int argc,
191913 sqlite3_value **argv
191914){
191915 Fts3Hash *pHash;
191916 sqlite3_tokenizer_module *p;
191917 sqlite3_tokenizer *pTokenizer = 0;
191918 sqlite3_tokenizer_cursor *pCsr = 0;
191919
191920 const char *zErr = 0;
191921
191922 const char *zName;
191923 int nName;
191924 const char *zInput;
191925 int nInput;
191926
191927 const char *azArg[64];
191928
191929 const char *zToken;
191930 int nToken = 0;
191931 int iStart = 0;
191932 int iEnd = 0;
191933 int iPos = 0;
191934 int i;
191935
191936 Tcl_Obj *pRet;
191937
191938 if( argc<2 ){
191939 sqlite3_result_error(context, "insufficient arguments", -1);
191940 return;
191941 }
191942
191943 nName = sqlite3_value_bytes(argv[0]);
191944 zName = (const char *)sqlite3_value_text(argv[0]);
191945 nInput = sqlite3_value_bytes(argv[argc-1]);
191946 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
191947
191948 pHash = (Fts3Hash *)sqlite3_user_data(context);
191949 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
191950
191951 if( !p ){
191952 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
191953 sqlite3_result_error(context, zErr2, -1);
191954 sqlite3_free(zErr2);
191955 return;
191956 }
191957
191958 pRet = Tcl_NewObj();
191959 Tcl_IncrRefCount(pRet);
191960
191961 for(i=1; i<argc-1; i++){
191962 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
191963 }
191964
191965 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
191966 zErr = "error in xCreate()";
191967 goto finish;
191968 }
191969 pTokenizer->pModule = p;
191970 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
191971 zErr = "error in xOpen()";
191972 goto finish;
191973 }
191974
191975 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
191976 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
191977 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
191978 zToken = &zInput[iStart];
191979 nToken = iEnd-iStart;
191980 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
191981 }
191982
191983 if( SQLITE_OK!=p->xClose(pCsr) ){
191984 zErr = "error in xClose()";
191985 goto finish;
191986 }
191987 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
191988 zErr = "error in xDestroy()";
191989 goto finish;
191990 }
191991
191992finish:
191993 if( zErr ){
191994 sqlite3_result_error(context, zErr, -1);
191995 }else{
191996 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
191997 }
191998 Tcl_DecrRefCount(pRet);
191999}
192000
192001static
192002int registerTokenizer(
192003 sqlite3 *db,
192004 char *zName,
192005 const sqlite3_tokenizer_module *p
192006){
192007 int rc;
192008 sqlite3_stmt *pStmt;
192009 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
192010
192011 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
192012 if( rc!=SQLITE_OK ){
192013 return rc;
192014 }
192015
192016 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
192017 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
192018 sqlite3_step(pStmt);
192019
192020 return sqlite3_finalize(pStmt);
192021}
192022
192023
192024static
192025int queryTokenizer(
192026 sqlite3 *db,
192027 char *zName,
192028 const sqlite3_tokenizer_module **pp
192029){
192030 int rc;
192031 sqlite3_stmt *pStmt;
192032 const char zSql[] = "SELECT fts3_tokenizer(?)";
192033
192034 *pp = 0;
192035 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
192036 if( rc!=SQLITE_OK ){
192037 return rc;
192038 }
192039
192040 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
192041 if( SQLITE_ROW==sqlite3_step(pStmt) ){
192042 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB
192043 && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
192044 ){
192045 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
192046 }
192047 }
192048
192049 return sqlite3_finalize(pStmt);
192050}
192051
192052SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
192053
192054/*
192055** Implementation of the scalar function fts3_tokenizer_internal_test().
192056** This function is used for testing only, it is not included in the
192057** build unless SQLITE_TEST is defined.
192058**
192059** The purpose of this is to test that the fts3_tokenizer() function
192060** can be used as designed by the C-code in the queryTokenizer and
192061** registerTokenizer() functions above. These two functions are repeated
192062** in the README.tokenizer file as an example, so it is important to
192063** test them.
192064**
192065** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
192066** function with no arguments. An assert() will fail if a problem is
192067** detected. i.e.:
192068**
192069** SELECT fts3_tokenizer_internal_test();
192070**
192071*/
192072static void intTestFunc(
192073 sqlite3_context *context,
192074 int argc,
192075 sqlite3_value **argv
192076){
192077 int rc;
192078 const sqlite3_tokenizer_module *p1;
192079 const sqlite3_tokenizer_module *p2;
192080 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
192081
192082 UNUSED_PARAMETER(argc);
192083 UNUSED_PARAMETER(argv);
192084
192085 /* Test the query function */
192086 sqlite3Fts3SimpleTokenizerModule(&p1);
192087 rc = queryTokenizer(db, "simple", &p2);
192088 assert( rc==SQLITE_OK );
192089 assert( p1==p2 );
192090 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
192091 assert( rc==SQLITE_ERROR );
192092 assert( p2==0 );
192093 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
192094
192095 /* Test the storage function */
192096 if( fts3TokenizerEnabled(context) ){
192097 rc = registerTokenizer(db, "nosuchtokenizer", p1);
192098 assert( rc==SQLITE_OK );
192099 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
192100 assert( rc==SQLITE_OK );
192101 assert( p2==p1 );
192102 }
192103
192104 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
192105}
192106
192107#endif
192108
192109/*
192110** Set up SQL objects in database db used to access the contents of
192111** the hash table pointed to by argument pHash. The hash table must
192112** been initialized to use string keys, and to take a private copy
192113** of the key when a value is inserted. i.e. by a call similar to:
192114**
192115** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
192116**
192117** This function adds a scalar function (see header comment above
192118** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
192119** defined at compilation time, a temporary virtual table (see header
192120** comment above struct HashTableVtab) to the database schema. Both
192121** provide read/write access to the contents of *pHash.
192122**
192123** The third argument to this function, zName, is used as the name
192124** of both the scalar and, if created, the virtual table.
192125*/
192126SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
192127 sqlite3 *db,
192128 Fts3Hash *pHash,
192129 const char *zName
192130){
192131 int rc = SQLITE_OK;
192132 void *p = (void *)pHash;
192133 const int any = SQLITE_UTF8|SQLITE_DIRECTONLY;
192134
192135#ifdef SQLITE_TEST
192136 char *zTest = 0;
192137 char *zTest2 = 0;
192138 void *pdb = (void *)db;
192139 zTest = sqlite3_mprintf("%s_test", zName);
192140 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
192141 if( !zTest || !zTest2 ){
192142 rc = SQLITE_NOMEM;
192143 }
192144#endif
192145
192146 if( SQLITE_OK==rc ){
192147 rc = sqlite3_create_function(db, zFunc: zName, nArg: 1, enc: any, p, xSFunc: fts3TokenizerFunc, xStep: 0, xFinal: 0);
192148 }
192149 if( SQLITE_OK==rc ){
192150 rc = sqlite3_create_function(db, zFunc: zName, nArg: 2, enc: any, p, xSFunc: fts3TokenizerFunc, xStep: 0, xFinal: 0);
192151 }
192152#ifdef SQLITE_TEST
192153 if( SQLITE_OK==rc ){
192154 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
192155 }
192156 if( SQLITE_OK==rc ){
192157 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
192158 }
192159#endif
192160
192161#ifdef SQLITE_TEST
192162 sqlite3_free(zTest);
192163 sqlite3_free(zTest2);
192164#endif
192165
192166 return rc;
192167}
192168
192169#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
192170
192171/************** End of fts3_tokenizer.c **************************************/
192172/************** Begin file fts3_tokenizer1.c *********************************/
192173/*
192174** 2006 Oct 10
192175**
192176** The author disclaims copyright to this source code. In place of
192177** a legal notice, here is a blessing:
192178**
192179** May you do good and not evil.
192180** May you find forgiveness for yourself and forgive others.
192181** May you share freely, never taking more than you give.
192182**
192183******************************************************************************
192184**
192185** Implementation of the "simple" full-text-search tokenizer.
192186*/
192187
192188/*
192189** The code in this file is only compiled if:
192190**
192191** * The FTS3 module is being built as an extension
192192** (in which case SQLITE_CORE is not defined), or
192193**
192194** * The FTS3 module is being built into the core of
192195** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
192196*/
192197/* #include "fts3Int.h" */
192198#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
192199
192200/* #include <assert.h> */
192201/* #include <stdlib.h> */
192202/* #include <stdio.h> */
192203/* #include <string.h> */
192204
192205/* #include "fts3_tokenizer.h" */
192206
192207typedef struct simple_tokenizer {
192208 sqlite3_tokenizer base;
192209 char delim[128]; /* flag ASCII delimiters */
192210} simple_tokenizer;
192211
192212typedef struct simple_tokenizer_cursor {
192213 sqlite3_tokenizer_cursor base;
192214 const char *pInput; /* input we are tokenizing */
192215 int nBytes; /* size of the input */
192216 int iOffset; /* current position in pInput */
192217 int iToken; /* index of next token to be returned */
192218 char *pToken; /* storage for current token */
192219 int nTokenAllocated; /* space allocated to zToken buffer */
192220} simple_tokenizer_cursor;
192221
192222
192223static int simpleDelim(simple_tokenizer *t, unsigned char c){
192224 return c<0x80 && t->delim[c];
192225}
192226static int fts3_isalnum(int x){
192227 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
192228}
192229
192230/*
192231** Create a new tokenizer instance.
192232*/
192233static int simpleCreate(
192234 int argc, const char * const *argv,
192235 sqlite3_tokenizer **ppTokenizer
192236){
192237 simple_tokenizer *t;
192238
192239 t = (simple_tokenizer *) sqlite3_malloc(n: sizeof(*t));
192240 if( t==NULL ) return SQLITE_NOMEM;
192241 memset(s: t, c: 0, n: sizeof(*t));
192242
192243 /* TODO(shess) Delimiters need to remain the same from run to run,
192244 ** else we need to reindex. One solution would be a meta-table to
192245 ** track such information in the database, then we'd only want this
192246 ** information on the initial create.
192247 */
192248 if( argc>1 ){
192249 int i, n = (int)strlen(s: argv[1]);
192250 for(i=0; i<n; i++){
192251 unsigned char ch = argv[1][i];
192252 /* We explicitly don't support UTF-8 delimiters for now. */
192253 if( ch>=0x80 ){
192254 sqlite3_free(p: t);
192255 return SQLITE_ERROR;
192256 }
192257 t->delim[ch] = 1;
192258 }
192259 } else {
192260 /* Mark non-alphanumeric ASCII characters as delimiters */
192261 int i;
192262 for(i=1; i<0x80; i++){
192263 t->delim[i] = !fts3_isalnum(x: i) ? -1 : 0;
192264 }
192265 }
192266
192267 *ppTokenizer = &t->base;
192268 return SQLITE_OK;
192269}
192270
192271/*
192272** Destroy a tokenizer
192273*/
192274static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
192275 sqlite3_free(p: pTokenizer);
192276 return SQLITE_OK;
192277}
192278
192279/*
192280** Prepare to begin tokenizing a particular string. The input
192281** string to be tokenized is pInput[0..nBytes-1]. A cursor
192282** used to incrementally tokenize this string is returned in
192283** *ppCursor.
192284*/
192285static int simpleOpen(
192286 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
192287 const char *pInput, int nBytes, /* String to be tokenized */
192288 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
192289){
192290 simple_tokenizer_cursor *c;
192291
192292 UNUSED_PARAMETER(pTokenizer);
192293
192294 c = (simple_tokenizer_cursor *) sqlite3_malloc(n: sizeof(*c));
192295 if( c==NULL ) return SQLITE_NOMEM;
192296
192297 c->pInput = pInput;
192298 if( pInput==0 ){
192299 c->nBytes = 0;
192300 }else if( nBytes<0 ){
192301 c->nBytes = (int)strlen(s: pInput);
192302 }else{
192303 c->nBytes = nBytes;
192304 }
192305 c->iOffset = 0; /* start tokenizing at the beginning */
192306 c->iToken = 0;
192307 c->pToken = NULL; /* no space allocated, yet. */
192308 c->nTokenAllocated = 0;
192309
192310 *ppCursor = &c->base;
192311 return SQLITE_OK;
192312}
192313
192314/*
192315** Close a tokenization cursor previously opened by a call to
192316** simpleOpen() above.
192317*/
192318static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
192319 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
192320 sqlite3_free(p: c->pToken);
192321 sqlite3_free(p: c);
192322 return SQLITE_OK;
192323}
192324
192325/*
192326** Extract the next token from a tokenization cursor. The cursor must
192327** have been opened by a prior call to simpleOpen().
192328*/
192329static int simpleNext(
192330 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
192331 const char **ppToken, /* OUT: *ppToken is the token text */
192332 int *pnBytes, /* OUT: Number of bytes in token */
192333 int *piStartOffset, /* OUT: Starting offset of token */
192334 int *piEndOffset, /* OUT: Ending offset of token */
192335 int *piPosition /* OUT: Position integer of token */
192336){
192337 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
192338 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
192339 unsigned char *p = (unsigned char *)c->pInput;
192340
192341 while( c->iOffset<c->nBytes ){
192342 int iStartOffset;
192343
192344 /* Scan past delimiter characters */
192345 while( c->iOffset<c->nBytes && simpleDelim(t, c: p[c->iOffset]) ){
192346 c->iOffset++;
192347 }
192348
192349 /* Count non-delimiter characters. */
192350 iStartOffset = c->iOffset;
192351 while( c->iOffset<c->nBytes && !simpleDelim(t, c: p[c->iOffset]) ){
192352 c->iOffset++;
192353 }
192354
192355 if( c->iOffset>iStartOffset ){
192356 int i, n = c->iOffset-iStartOffset;
192357 if( n>c->nTokenAllocated ){
192358 char *pNew;
192359 c->nTokenAllocated = n+20;
192360 pNew = sqlite3_realloc64(pOld: c->pToken, n: c->nTokenAllocated);
192361 if( !pNew ) return SQLITE_NOMEM;
192362 c->pToken = pNew;
192363 }
192364 for(i=0; i<n; i++){
192365 /* TODO(shess) This needs expansion to handle UTF-8
192366 ** case-insensitivity.
192367 */
192368 unsigned char ch = p[iStartOffset+i];
192369 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
192370 }
192371 *ppToken = c->pToken;
192372 *pnBytes = n;
192373 *piStartOffset = iStartOffset;
192374 *piEndOffset = c->iOffset;
192375 *piPosition = c->iToken++;
192376
192377 return SQLITE_OK;
192378 }
192379 }
192380 return SQLITE_DONE;
192381}
192382
192383/*
192384** The set of routines that implement the simple tokenizer
192385*/
192386static const sqlite3_tokenizer_module simpleTokenizerModule = {
192387 0,
192388 simpleCreate,
192389 simpleDestroy,
192390 simpleOpen,
192391 simpleClose,
192392 simpleNext,
192393 0,
192394};
192395
192396/*
192397** Allocate a new simple tokenizer. Return a pointer to the new
192398** tokenizer in *ppModule
192399*/
192400SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
192401 sqlite3_tokenizer_module const**ppModule
192402){
192403 *ppModule = &simpleTokenizerModule;
192404}
192405
192406#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
192407
192408/************** End of fts3_tokenizer1.c *************************************/
192409/************** Begin file fts3_tokenize_vtab.c ******************************/
192410/*
192411** 2013 Apr 22
192412**
192413** The author disclaims copyright to this source code. In place of
192414** a legal notice, here is a blessing:
192415**
192416** May you do good and not evil.
192417** May you find forgiveness for yourself and forgive others.
192418** May you share freely, never taking more than you give.
192419**
192420******************************************************************************
192421**
192422** This file contains code for the "fts3tokenize" virtual table module.
192423** An fts3tokenize virtual table is created as follows:
192424**
192425** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
192426** <tokenizer-name>, <arg-1>, ...
192427** );
192428**
192429** The table created has the following schema:
192430**
192431** CREATE TABLE <tbl>(input, token, start, end, position)
192432**
192433** When queried, the query must include a WHERE clause of type:
192434**
192435** input = <string>
192436**
192437** The virtual table module tokenizes this <string>, using the FTS3
192438** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
192439** statement and returns one row for each token in the result. With
192440** fields set as follows:
192441**
192442** input: Always set to a copy of <string>
192443** token: A token from the input.
192444** start: Byte offset of the token within the input <string>.
192445** end: Byte offset of the byte immediately following the end of the
192446** token within the input string.
192447** pos: Token offset of token within input.
192448**
192449*/
192450/* #include "fts3Int.h" */
192451#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
192452
192453/* #include <string.h> */
192454/* #include <assert.h> */
192455
192456typedef struct Fts3tokTable Fts3tokTable;
192457typedef struct Fts3tokCursor Fts3tokCursor;
192458
192459/*
192460** Virtual table structure.
192461*/
192462struct Fts3tokTable {
192463 sqlite3_vtab base; /* Base class used by SQLite core */
192464 const sqlite3_tokenizer_module *pMod;
192465 sqlite3_tokenizer *pTok;
192466};
192467
192468/*
192469** Virtual table cursor structure.
192470*/
192471struct Fts3tokCursor {
192472 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
192473 char *zInput; /* Input string */
192474 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
192475 int iRowid; /* Current 'rowid' value */
192476 const char *zToken; /* Current 'token' value */
192477 int nToken; /* Size of zToken in bytes */
192478 int iStart; /* Current 'start' value */
192479 int iEnd; /* Current 'end' value */
192480 int iPos; /* Current 'pos' value */
192481};
192482
192483/*
192484** Query FTS for the tokenizer implementation named zName.
192485*/
192486static int fts3tokQueryTokenizer(
192487 Fts3Hash *pHash,
192488 const char *zName,
192489 const sqlite3_tokenizer_module **pp,
192490 char **pzErr
192491){
192492 sqlite3_tokenizer_module *p;
192493 int nName = (int)strlen(s: zName);
192494
192495 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pH: pHash, pKey: zName, nKey: nName+1);
192496 if( !p ){
192497 sqlite3Fts3ErrMsg(pzErr, zFormat: "unknown tokenizer: %s", zName);
192498 return SQLITE_ERROR;
192499 }
192500
192501 *pp = p;
192502 return SQLITE_OK;
192503}
192504
192505/*
192506** The second argument, argv[], is an array of pointers to nul-terminated
192507** strings. This function makes a copy of the array and strings into a
192508** single block of memory. It then dequotes any of the strings that appear
192509** to be quoted.
192510**
192511** If successful, output parameter *pazDequote is set to point at the
192512** array of dequoted strings and SQLITE_OK is returned. The caller is
192513** responsible for eventually calling sqlite3_free() to free the array
192514** in this case. Or, if an error occurs, an SQLite error code is returned.
192515** The final value of *pazDequote is undefined in this case.
192516*/
192517static int fts3tokDequoteArray(
192518 int argc, /* Number of elements in argv[] */
192519 const char * const *argv, /* Input array */
192520 char ***pazDequote /* Output array */
192521){
192522 int rc = SQLITE_OK; /* Return code */
192523 if( argc==0 ){
192524 *pazDequote = 0;
192525 }else{
192526 int i;
192527 int nByte = 0;
192528 char **azDequote;
192529
192530 for(i=0; i<argc; i++){
192531 nByte += (int)(strlen(s: argv[i]) + 1);
192532 }
192533
192534 *pazDequote = azDequote = sqlite3_malloc64(n: sizeof(char *)*argc + nByte);
192535 if( azDequote==0 ){
192536 rc = SQLITE_NOMEM;
192537 }else{
192538 char *pSpace = (char *)&azDequote[argc];
192539 for(i=0; i<argc; i++){
192540 int n = (int)strlen(s: argv[i]);
192541 azDequote[i] = pSpace;
192542 memcpy(dest: pSpace, src: argv[i], n: n+1);
192543 sqlite3Fts3Dequote(z: pSpace);
192544 pSpace += (n+1);
192545 }
192546 }
192547 }
192548
192549 return rc;
192550}
192551
192552/*
192553** Schema of the tokenizer table.
192554*/
192555#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
192556
192557/*
192558** This function does all the work for both the xConnect and xCreate methods.
192559** These tables have no persistent representation of their own, so xConnect
192560** and xCreate are identical operations.
192561**
192562** argv[0]: module name
192563** argv[1]: database name
192564** argv[2]: table name
192565** argv[3]: first argument (tokenizer name)
192566*/
192567static int fts3tokConnectMethod(
192568 sqlite3 *db, /* Database connection */
192569 void *pHash, /* Hash table of tokenizers */
192570 int argc, /* Number of elements in argv array */
192571 const char * const *argv, /* xCreate/xConnect argument array */
192572 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
192573 char **pzErr /* OUT: sqlite3_malloc'd error message */
192574){
192575 Fts3tokTable *pTab = 0;
192576 const sqlite3_tokenizer_module *pMod = 0;
192577 sqlite3_tokenizer *pTok = 0;
192578 int rc;
192579 char **azDequote = 0;
192580 int nDequote;
192581
192582 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
192583 if( rc!=SQLITE_OK ) return rc;
192584
192585 nDequote = argc-3;
192586 rc = fts3tokDequoteArray(argc: nDequote, argv: &argv[3], pazDequote: &azDequote);
192587
192588 if( rc==SQLITE_OK ){
192589 const char *zModule;
192590 if( nDequote<1 ){
192591 zModule = "simple";
192592 }else{
192593 zModule = azDequote[0];
192594 }
192595 rc = fts3tokQueryTokenizer(pHash: (Fts3Hash*)pHash, zName: zModule, pp: &pMod, pzErr);
192596 }
192597
192598 assert( (rc==SQLITE_OK)==(pMod!=0) );
192599 if( rc==SQLITE_OK ){
192600 const char * const *azArg = 0;
192601 if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
192602 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
192603 }
192604
192605 if( rc==SQLITE_OK ){
192606 pTab = (Fts3tokTable *)sqlite3_malloc(n: sizeof(Fts3tokTable));
192607 if( pTab==0 ){
192608 rc = SQLITE_NOMEM;
192609 }
192610 }
192611
192612 if( rc==SQLITE_OK ){
192613 memset(s: pTab, c: 0, n: sizeof(Fts3tokTable));
192614 pTab->pMod = pMod;
192615 pTab->pTok = pTok;
192616 *ppVtab = &pTab->base;
192617 }else{
192618 if( pTok ){
192619 pMod->xDestroy(pTok);
192620 }
192621 }
192622
192623 sqlite3_free(p: azDequote);
192624 return rc;
192625}
192626
192627/*
192628** This function does the work for both the xDisconnect and xDestroy methods.
192629** These tables have no persistent representation of their own, so xDisconnect
192630** and xDestroy are identical operations.
192631*/
192632static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
192633 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
192634
192635 pTab->pMod->xDestroy(pTab->pTok);
192636 sqlite3_free(p: pTab);
192637 return SQLITE_OK;
192638}
192639
192640/*
192641** xBestIndex - Analyze a WHERE and ORDER BY clause.
192642*/
192643static int fts3tokBestIndexMethod(
192644 sqlite3_vtab *pVTab,
192645 sqlite3_index_info *pInfo
192646){
192647 int i;
192648 UNUSED_PARAMETER(pVTab);
192649
192650 for(i=0; i<pInfo->nConstraint; i++){
192651 if( pInfo->aConstraint[i].usable
192652 && pInfo->aConstraint[i].iColumn==0
192653 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
192654 ){
192655 pInfo->idxNum = 1;
192656 pInfo->aConstraintUsage[i].argvIndex = 1;
192657 pInfo->aConstraintUsage[i].omit = 1;
192658 pInfo->estimatedCost = 1;
192659 return SQLITE_OK;
192660 }
192661 }
192662
192663 pInfo->idxNum = 0;
192664 assert( pInfo->estimatedCost>1000000.0 );
192665
192666 return SQLITE_OK;
192667}
192668
192669/*
192670** xOpen - Open a cursor.
192671*/
192672static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
192673 Fts3tokCursor *pCsr;
192674 UNUSED_PARAMETER(pVTab);
192675
192676 pCsr = (Fts3tokCursor *)sqlite3_malloc(n: sizeof(Fts3tokCursor));
192677 if( pCsr==0 ){
192678 return SQLITE_NOMEM;
192679 }
192680 memset(s: pCsr, c: 0, n: sizeof(Fts3tokCursor));
192681
192682 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
192683 return SQLITE_OK;
192684}
192685
192686/*
192687** Reset the tokenizer cursor passed as the only argument. As if it had
192688** just been returned by fts3tokOpenMethod().
192689*/
192690static void fts3tokResetCursor(Fts3tokCursor *pCsr){
192691 if( pCsr->pCsr ){
192692 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
192693 pTab->pMod->xClose(pCsr->pCsr);
192694 pCsr->pCsr = 0;
192695 }
192696 sqlite3_free(p: pCsr->zInput);
192697 pCsr->zInput = 0;
192698 pCsr->zToken = 0;
192699 pCsr->nToken = 0;
192700 pCsr->iStart = 0;
192701 pCsr->iEnd = 0;
192702 pCsr->iPos = 0;
192703 pCsr->iRowid = 0;
192704}
192705
192706/*
192707** xClose - Close a cursor.
192708*/
192709static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
192710 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
192711
192712 fts3tokResetCursor(pCsr);
192713 sqlite3_free(p: pCsr);
192714 return SQLITE_OK;
192715}
192716
192717/*
192718** xNext - Advance the cursor to the next row, if any.
192719*/
192720static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
192721 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
192722 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
192723 int rc; /* Return code */
192724
192725 pCsr->iRowid++;
192726 rc = pTab->pMod->xNext(pCsr->pCsr,
192727 &pCsr->zToken, &pCsr->nToken,
192728 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
192729 );
192730
192731 if( rc!=SQLITE_OK ){
192732 fts3tokResetCursor(pCsr);
192733 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
192734 }
192735
192736 return rc;
192737}
192738
192739/*
192740** xFilter - Initialize a cursor to point at the start of its data.
192741*/
192742static int fts3tokFilterMethod(
192743 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
192744 int idxNum, /* Strategy index */
192745 const char *idxStr, /* Unused */
192746 int nVal, /* Number of elements in apVal */
192747 sqlite3_value **apVal /* Arguments for the indexing scheme */
192748){
192749 int rc = SQLITE_ERROR;
192750 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
192751 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
192752 UNUSED_PARAMETER(idxStr);
192753 UNUSED_PARAMETER(nVal);
192754
192755 fts3tokResetCursor(pCsr);
192756 if( idxNum==1 ){
192757 const char *zByte = (const char *)sqlite3_value_text(pVal: apVal[0]);
192758 int nByte = sqlite3_value_bytes(pVal: apVal[0]);
192759 pCsr->zInput = sqlite3_malloc64(n: nByte+1);
192760 if( pCsr->zInput==0 ){
192761 rc = SQLITE_NOMEM;
192762 }else{
192763 if( nByte>0 ) memcpy(dest: pCsr->zInput, src: zByte, n: nByte);
192764 pCsr->zInput[nByte] = 0;
192765 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
192766 if( rc==SQLITE_OK ){
192767 pCsr->pCsr->pTokenizer = pTab->pTok;
192768 }
192769 }
192770 }
192771
192772 if( rc!=SQLITE_OK ) return rc;
192773 return fts3tokNextMethod(pCursor);
192774}
192775
192776/*
192777** xEof - Return true if the cursor is at EOF, or false otherwise.
192778*/
192779static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
192780 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
192781 return (pCsr->zToken==0);
192782}
192783
192784/*
192785** xColumn - Return a column value.
192786*/
192787static int fts3tokColumnMethod(
192788 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
192789 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
192790 int iCol /* Index of column to read value from */
192791){
192792 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
192793
192794 /* CREATE TABLE x(input, token, start, end, position) */
192795 switch( iCol ){
192796 case 0:
192797 sqlite3_result_text(pCtx, z: pCsr->zInput, n: -1, SQLITE_TRANSIENT);
192798 break;
192799 case 1:
192800 sqlite3_result_text(pCtx, z: pCsr->zToken, n: pCsr->nToken, SQLITE_TRANSIENT);
192801 break;
192802 case 2:
192803 sqlite3_result_int(pCtx, iVal: pCsr->iStart);
192804 break;
192805 case 3:
192806 sqlite3_result_int(pCtx, iVal: pCsr->iEnd);
192807 break;
192808 default:
192809 assert( iCol==4 );
192810 sqlite3_result_int(pCtx, iVal: pCsr->iPos);
192811 break;
192812 }
192813 return SQLITE_OK;
192814}
192815
192816/*
192817** xRowid - Return the current rowid for the cursor.
192818*/
192819static int fts3tokRowidMethod(
192820 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
192821 sqlite_int64 *pRowid /* OUT: Rowid value */
192822){
192823 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
192824 *pRowid = (sqlite3_int64)pCsr->iRowid;
192825 return SQLITE_OK;
192826}
192827
192828/*
192829** Register the fts3tok module with database connection db. Return SQLITE_OK
192830** if successful or an error code if sqlite3_create_module() fails.
192831*/
192832SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
192833 static const sqlite3_module fts3tok_module = {
192834 0, /* iVersion */
192835 fts3tokConnectMethod, /* xCreate */
192836 fts3tokConnectMethod, /* xConnect */
192837 fts3tokBestIndexMethod, /* xBestIndex */
192838 fts3tokDisconnectMethod, /* xDisconnect */
192839 fts3tokDisconnectMethod, /* xDestroy */
192840 fts3tokOpenMethod, /* xOpen */
192841 fts3tokCloseMethod, /* xClose */
192842 fts3tokFilterMethod, /* xFilter */
192843 fts3tokNextMethod, /* xNext */
192844 fts3tokEofMethod, /* xEof */
192845 fts3tokColumnMethod, /* xColumn */
192846 fts3tokRowidMethod, /* xRowid */
192847 0, /* xUpdate */
192848 0, /* xBegin */
192849 0, /* xSync */
192850 0, /* xCommit */
192851 0, /* xRollback */
192852 0, /* xFindFunction */
192853 0, /* xRename */
192854 0, /* xSavepoint */
192855 0, /* xRelease */
192856 0, /* xRollbackTo */
192857 0 /* xShadowName */
192858 };
192859 int rc; /* Return code */
192860
192861 rc = sqlite3_create_module_v2(
192862 db, zName: "fts3tokenize", pModule: &fts3tok_module, pAux: (void*)pHash, xDestroy
192863 );
192864 return rc;
192865}
192866
192867#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
192868
192869/************** End of fts3_tokenize_vtab.c **********************************/
192870/************** Begin file fts3_write.c **************************************/
192871/*
192872** 2009 Oct 23
192873**
192874** The author disclaims copyright to this source code. In place of
192875** a legal notice, here is a blessing:
192876**
192877** May you do good and not evil.
192878** May you find forgiveness for yourself and forgive others.
192879** May you share freely, never taking more than you give.
192880**
192881******************************************************************************
192882**
192883** This file is part of the SQLite FTS3 extension module. Specifically,
192884** this file contains code to insert, update and delete rows from FTS3
192885** tables. It also contains code to merge FTS3 b-tree segments. Some
192886** of the sub-routines used to merge segments are also used by the query
192887** code in fts3.c.
192888*/
192889
192890/* #include "fts3Int.h" */
192891#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
192892
192893/* #include <string.h> */
192894/* #include <assert.h> */
192895/* #include <stdlib.h> */
192896/* #include <stdio.h> */
192897
192898#define FTS_MAX_APPENDABLE_HEIGHT 16
192899
192900/*
192901** When full-text index nodes are loaded from disk, the buffer that they
192902** are loaded into has the following number of bytes of padding at the end
192903** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
192904** of 920 bytes is allocated for it.
192905**
192906** This means that if we have a pointer into a buffer containing node data,
192907** it is always safe to read up to two varints from it without risking an
192908** overread, even if the node data is corrupted.
192909*/
192910#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
192911
192912/*
192913** Under certain circumstances, b-tree nodes (doclists) can be loaded into
192914** memory incrementally instead of all at once. This can be a big performance
192915** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
192916** method before retrieving all query results (as may happen, for example,
192917** if a query has a LIMIT clause).
192918**
192919** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
192920** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
192921** The code is written so that the hard lower-limit for each of these values
192922** is 1. Clearly such small values would be inefficient, but can be useful
192923** for testing purposes.
192924**
192925** If this module is built with SQLITE_TEST defined, these constants may
192926** be overridden at runtime for testing purposes. File fts3_test.c contains
192927** a Tcl interface to read and write the values.
192928*/
192929#ifdef SQLITE_TEST
192930int test_fts3_node_chunksize = (4*1024);
192931int test_fts3_node_chunk_threshold = (4*1024)*4;
192932# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
192933# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
192934#else
192935# define FTS3_NODE_CHUNKSIZE (4*1024)
192936# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
192937#endif
192938
192939/*
192940** The values that may be meaningfully bound to the :1 parameter in
192941** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
192942*/
192943#define FTS_STAT_DOCTOTAL 0
192944#define FTS_STAT_INCRMERGEHINT 1
192945#define FTS_STAT_AUTOINCRMERGE 2
192946
192947/*
192948** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
192949** and incremental merge operation that takes place. This is used for
192950** debugging FTS only, it should not usually be turned on in production
192951** systems.
192952*/
192953#ifdef FTS3_LOG_MERGES
192954static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
192955 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
192956}
192957#else
192958#define fts3LogMerge(x, y)
192959#endif
192960
192961
192962typedef struct PendingList PendingList;
192963typedef struct SegmentNode SegmentNode;
192964typedef struct SegmentWriter SegmentWriter;
192965
192966/*
192967** An instance of the following data structure is used to build doclists
192968** incrementally. See function fts3PendingListAppend() for details.
192969*/
192970struct PendingList {
192971 int nData;
192972 char *aData;
192973 int nSpace;
192974 sqlite3_int64 iLastDocid;
192975 sqlite3_int64 iLastCol;
192976 sqlite3_int64 iLastPos;
192977};
192978
192979
192980/*
192981** Each cursor has a (possibly empty) linked list of the following objects.
192982*/
192983struct Fts3DeferredToken {
192984 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
192985 int iCol; /* Column token must occur in */
192986 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
192987 PendingList *pList; /* Doclist is assembled here */
192988};
192989
192990/*
192991** An instance of this structure is used to iterate through the terms on
192992** a contiguous set of segment b-tree leaf nodes. Although the details of
192993** this structure are only manipulated by code in this file, opaque handles
192994** of type Fts3SegReader* are also used by code in fts3.c to iterate through
192995** terms when querying the full-text index. See functions:
192996**
192997** sqlite3Fts3SegReaderNew()
192998** sqlite3Fts3SegReaderFree()
192999** sqlite3Fts3SegReaderIterate()
193000**
193001** Methods used to manipulate Fts3SegReader structures:
193002**
193003** fts3SegReaderNext()
193004** fts3SegReaderFirstDocid()
193005** fts3SegReaderNextDocid()
193006*/
193007struct Fts3SegReader {
193008 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
193009 u8 bLookup; /* True for a lookup only */
193010 u8 rootOnly; /* True for a root-only reader */
193011
193012 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
193013 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
193014 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
193015 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
193016
193017 char *aNode; /* Pointer to node data (or NULL) */
193018 int nNode; /* Size of buffer at aNode (or 0) */
193019 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
193020 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
193021
193022 Fts3HashElem **ppNextElem;
193023
193024 /* Variables set by fts3SegReaderNext(). These may be read directly
193025 ** by the caller. They are valid from the time SegmentReaderNew() returns
193026 ** until SegmentReaderNext() returns something other than SQLITE_OK
193027 ** (i.e. SQLITE_DONE).
193028 */
193029 int nTerm; /* Number of bytes in current term */
193030 char *zTerm; /* Pointer to current term */
193031 int nTermAlloc; /* Allocated size of zTerm buffer */
193032 char *aDoclist; /* Pointer to doclist of current entry */
193033 int nDoclist; /* Size of doclist in current entry */
193034
193035 /* The following variables are used by fts3SegReaderNextDocid() to iterate
193036 ** through the current doclist (aDoclist/nDoclist).
193037 */
193038 char *pOffsetList;
193039 int nOffsetList; /* For descending pending seg-readers only */
193040 sqlite3_int64 iDocid;
193041};
193042
193043#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
193044#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
193045
193046/*
193047** An instance of this structure is used to create a segment b-tree in the
193048** database. The internal details of this type are only accessed by the
193049** following functions:
193050**
193051** fts3SegWriterAdd()
193052** fts3SegWriterFlush()
193053** fts3SegWriterFree()
193054*/
193055struct SegmentWriter {
193056 SegmentNode *pTree; /* Pointer to interior tree structure */
193057 sqlite3_int64 iFirst; /* First slot in %_segments written */
193058 sqlite3_int64 iFree; /* Next free slot in %_segments */
193059 char *zTerm; /* Pointer to previous term buffer */
193060 int nTerm; /* Number of bytes in zTerm */
193061 int nMalloc; /* Size of malloc'd buffer at zMalloc */
193062 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
193063 int nSize; /* Size of allocation at aData */
193064 int nData; /* Bytes of data in aData */
193065 char *aData; /* Pointer to block from malloc() */
193066 i64 nLeafData; /* Number of bytes of leaf data written */
193067};
193068
193069/*
193070** Type SegmentNode is used by the following three functions to create
193071** the interior part of the segment b+-tree structures (everything except
193072** the leaf nodes). These functions and type are only ever used by code
193073** within the fts3SegWriterXXX() family of functions described above.
193074**
193075** fts3NodeAddTerm()
193076** fts3NodeWrite()
193077** fts3NodeFree()
193078**
193079** When a b+tree is written to the database (either as a result of a merge
193080** or the pending-terms table being flushed), leaves are written into the
193081** database file as soon as they are completely populated. The interior of
193082** the tree is assembled in memory and written out only once all leaves have
193083** been populated and stored. This is Ok, as the b+-tree fanout is usually
193084** very large, meaning that the interior of the tree consumes relatively
193085** little memory.
193086*/
193087struct SegmentNode {
193088 SegmentNode *pParent; /* Parent node (or NULL for root node) */
193089 SegmentNode *pRight; /* Pointer to right-sibling */
193090 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
193091 int nEntry; /* Number of terms written to node so far */
193092 char *zTerm; /* Pointer to previous term buffer */
193093 int nTerm; /* Number of bytes in zTerm */
193094 int nMalloc; /* Size of malloc'd buffer at zMalloc */
193095 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
193096 int nData; /* Bytes of valid data so far */
193097 char *aData; /* Node data */
193098};
193099
193100/*
193101** Valid values for the second argument to fts3SqlStmt().
193102*/
193103#define SQL_DELETE_CONTENT 0
193104#define SQL_IS_EMPTY 1
193105#define SQL_DELETE_ALL_CONTENT 2
193106#define SQL_DELETE_ALL_SEGMENTS 3
193107#define SQL_DELETE_ALL_SEGDIR 4
193108#define SQL_DELETE_ALL_DOCSIZE 5
193109#define SQL_DELETE_ALL_STAT 6
193110#define SQL_SELECT_CONTENT_BY_ROWID 7
193111#define SQL_NEXT_SEGMENT_INDEX 8
193112#define SQL_INSERT_SEGMENTS 9
193113#define SQL_NEXT_SEGMENTS_ID 10
193114#define SQL_INSERT_SEGDIR 11
193115#define SQL_SELECT_LEVEL 12
193116#define SQL_SELECT_LEVEL_RANGE 13
193117#define SQL_SELECT_LEVEL_COUNT 14
193118#define SQL_SELECT_SEGDIR_MAX_LEVEL 15
193119#define SQL_DELETE_SEGDIR_LEVEL 16
193120#define SQL_DELETE_SEGMENTS_RANGE 17
193121#define SQL_CONTENT_INSERT 18
193122#define SQL_DELETE_DOCSIZE 19
193123#define SQL_REPLACE_DOCSIZE 20
193124#define SQL_SELECT_DOCSIZE 21
193125#define SQL_SELECT_STAT 22
193126#define SQL_REPLACE_STAT 23
193127
193128#define SQL_SELECT_ALL_PREFIX_LEVEL 24
193129#define SQL_DELETE_ALL_TERMS_SEGDIR 25
193130#define SQL_DELETE_SEGDIR_RANGE 26
193131#define SQL_SELECT_ALL_LANGID 27
193132#define SQL_FIND_MERGE_LEVEL 28
193133#define SQL_MAX_LEAF_NODE_ESTIMATE 29
193134#define SQL_DELETE_SEGDIR_ENTRY 30
193135#define SQL_SHIFT_SEGDIR_ENTRY 31
193136#define SQL_SELECT_SEGDIR 32
193137#define SQL_CHOMP_SEGDIR 33
193138#define SQL_SEGMENT_IS_APPENDABLE 34
193139#define SQL_SELECT_INDEXES 35
193140#define SQL_SELECT_MXLEVEL 36
193141
193142#define SQL_SELECT_LEVEL_RANGE2 37
193143#define SQL_UPDATE_LEVEL_IDX 38
193144#define SQL_UPDATE_LEVEL 39
193145
193146/*
193147** This function is used to obtain an SQLite prepared statement handle
193148** for the statement identified by the second argument. If successful,
193149** *pp is set to the requested statement handle and SQLITE_OK returned.
193150** Otherwise, an SQLite error code is returned and *pp is set to 0.
193151**
193152** If argument apVal is not NULL, then it must point to an array with
193153** at least as many entries as the requested statement has bound
193154** parameters. The values are bound to the statements parameters before
193155** returning.
193156*/
193157static int fts3SqlStmt(
193158 Fts3Table *p, /* Virtual table handle */
193159 int eStmt, /* One of the SQL_XXX constants above */
193160 sqlite3_stmt **pp, /* OUT: Statement handle */
193161 sqlite3_value **apVal /* Values to bind to statement */
193162){
193163 const char *azSql[] = {
193164/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
193165/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
193166/* 2 */ "DELETE FROM %Q.'%q_content'",
193167/* 3 */ "DELETE FROM %Q.'%q_segments'",
193168/* 4 */ "DELETE FROM %Q.'%q_segdir'",
193169/* 5 */ "DELETE FROM %Q.'%q_docsize'",
193170/* 6 */ "DELETE FROM %Q.'%q_stat'",
193171/* 7 */ "SELECT %s WHERE rowid=?",
193172/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
193173/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
193174/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
193175/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
193176
193177 /* Return segments in order from oldest to newest.*/
193178/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
193179 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
193180/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
193181 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
193182 "ORDER BY level DESC, idx ASC",
193183
193184/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
193185/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
193186
193187/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
193188/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
193189/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
193190/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
193191/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
193192/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
193193/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
193194/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
193195/* 24 */ "",
193196/* 25 */ "",
193197
193198/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
193199/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
193200
193201/* This statement is used to determine which level to read the input from
193202** when performing an incremental merge. It returns the absolute level number
193203** of the oldest level in the db that contains at least ? segments. Or,
193204** if no level in the FTS index contains more than ? segments, the statement
193205** returns zero rows. */
193206/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
193207 " GROUP BY level HAVING cnt>=?"
193208 " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
193209
193210/* Estimate the upper limit on the number of leaf nodes in a new segment
193211** created by merging the oldest :2 segments from absolute level :1. See
193212** function sqlite3Fts3Incrmerge() for details. */
193213/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
193214 " FROM (SELECT * FROM %Q.'%q_segdir' "
193215 " WHERE level = ? ORDER BY idx ASC LIMIT ?"
193216 " )",
193217
193218/* SQL_DELETE_SEGDIR_ENTRY
193219** Delete the %_segdir entry on absolute level :1 with index :2. */
193220/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
193221
193222/* SQL_SHIFT_SEGDIR_ENTRY
193223** Modify the idx value for the segment with idx=:3 on absolute level :2
193224** to :1. */
193225/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
193226
193227/* SQL_SELECT_SEGDIR
193228** Read a single entry from the %_segdir table. The entry from absolute
193229** level :1 with index value :2. */
193230/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
193231 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
193232
193233/* SQL_CHOMP_SEGDIR
193234** Update the start_block (:1) and root (:2) fields of the %_segdir
193235** entry located on absolute level :3 with index :4. */
193236/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
193237 "WHERE level = ? AND idx = ?",
193238
193239/* SQL_SEGMENT_IS_APPENDABLE
193240** Return a single row if the segment with end_block=? is appendable. Or
193241** no rows otherwise. */
193242/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
193243
193244/* SQL_SELECT_INDEXES
193245** Return the list of valid segment indexes for absolute level ? */
193246/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
193247
193248/* SQL_SELECT_MXLEVEL
193249** Return the largest relative level in the FTS index or indexes. */
193250/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
193251
193252 /* Return segments in order from oldest to newest.*/
193253/* 37 */ "SELECT level, idx, end_block "
193254 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
193255 "ORDER BY level DESC, idx ASC",
193256
193257 /* Update statements used while promoting segments */
193258/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
193259 "WHERE level=? AND idx=?",
193260/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
193261
193262 };
193263 int rc = SQLITE_OK;
193264 sqlite3_stmt *pStmt;
193265
193266 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
193267 assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
193268
193269 pStmt = p->aStmt[eStmt];
193270 if( !pStmt ){
193271 int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
193272 char *zSql;
193273 if( eStmt==SQL_CONTENT_INSERT ){
193274 zSql = sqlite3_mprintf(zFormat: azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
193275 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
193276 f &= ~SQLITE_PREPARE_NO_VTAB;
193277 zSql = sqlite3_mprintf(zFormat: azSql[eStmt], p->zReadExprlist);
193278 }else{
193279 zSql = sqlite3_mprintf(zFormat: azSql[eStmt], p->zDb, p->zName);
193280 }
193281 if( !zSql ){
193282 rc = SQLITE_NOMEM;
193283 }else{
193284 rc = sqlite3_prepare_v3(db: p->db, zSql, nBytes: -1, prepFlags: f, ppStmt: &pStmt, NULL);
193285 sqlite3_free(p: zSql);
193286 assert( rc==SQLITE_OK || pStmt==0 );
193287 p->aStmt[eStmt] = pStmt;
193288 }
193289 }
193290 if( apVal ){
193291 int i;
193292 int nParam = sqlite3_bind_parameter_count(pStmt);
193293 for(i=0; rc==SQLITE_OK && i<nParam; i++){
193294 rc = sqlite3_bind_value(pStmt, i: i+1, pValue: apVal[i]);
193295 }
193296 }
193297 *pp = pStmt;
193298 return rc;
193299}
193300
193301
193302static int fts3SelectDocsize(
193303 Fts3Table *pTab, /* FTS3 table handle */
193304 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
193305 sqlite3_stmt **ppStmt /* OUT: Statement handle */
193306){
193307 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
193308 int rc; /* Return code */
193309
193310 rc = fts3SqlStmt(p: pTab, SQL_SELECT_DOCSIZE, pp: &pStmt, apVal: 0);
193311 if( rc==SQLITE_OK ){
193312 sqlite3_bind_int64(pStmt, i: 1, iValue: iDocid);
193313 rc = sqlite3_step(pStmt);
193314 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, i: 0)!=SQLITE_BLOB ){
193315 rc = sqlite3_reset(pStmt);
193316 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
193317 pStmt = 0;
193318 }else{
193319 rc = SQLITE_OK;
193320 }
193321 }
193322
193323 *ppStmt = pStmt;
193324 return rc;
193325}
193326
193327SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
193328 Fts3Table *pTab, /* Fts3 table handle */
193329 sqlite3_stmt **ppStmt /* OUT: Statement handle */
193330){
193331 sqlite3_stmt *pStmt = 0;
193332 int rc;
193333 rc = fts3SqlStmt(p: pTab, SQL_SELECT_STAT, pp: &pStmt, apVal: 0);
193334 if( rc==SQLITE_OK ){
193335 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_DOCTOTAL);
193336 if( sqlite3_step(pStmt)!=SQLITE_ROW
193337 || sqlite3_column_type(pStmt, i: 0)!=SQLITE_BLOB
193338 ){
193339 rc = sqlite3_reset(pStmt);
193340 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
193341 pStmt = 0;
193342 }
193343 }
193344 *ppStmt = pStmt;
193345 return rc;
193346}
193347
193348SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
193349 Fts3Table *pTab, /* Fts3 table handle */
193350 sqlite3_int64 iDocid, /* Docid to read size data for */
193351 sqlite3_stmt **ppStmt /* OUT: Statement handle */
193352){
193353 return fts3SelectDocsize(pTab, iDocid, ppStmt);
193354}
193355
193356/*
193357** Similar to fts3SqlStmt(). Except, after binding the parameters in
193358** array apVal[] to the SQL statement identified by eStmt, the statement
193359** is executed.
193360**
193361** Returns SQLITE_OK if the statement is successfully executed, or an
193362** SQLite error code otherwise.
193363*/
193364static void fts3SqlExec(
193365 int *pRC, /* Result code */
193366 Fts3Table *p, /* The FTS3 table */
193367 int eStmt, /* Index of statement to evaluate */
193368 sqlite3_value **apVal /* Parameters to bind */
193369){
193370 sqlite3_stmt *pStmt;
193371 int rc;
193372 if( *pRC ) return;
193373 rc = fts3SqlStmt(p, eStmt, pp: &pStmt, apVal);
193374 if( rc==SQLITE_OK ){
193375 sqlite3_step(pStmt);
193376 rc = sqlite3_reset(pStmt);
193377 }
193378 *pRC = rc;
193379}
193380
193381
193382/*
193383** This function ensures that the caller has obtained an exclusive
193384** shared-cache table-lock on the %_segdir table. This is required before
193385** writing data to the fts3 table. If this lock is not acquired first, then
193386** the caller may end up attempting to take this lock as part of committing
193387** a transaction, causing SQLite to return SQLITE_LOCKED or
193388** LOCKED_SHAREDCACHEto a COMMIT command.
193389**
193390** It is best to avoid this because if FTS3 returns any error when
193391** committing a transaction, the whole transaction will be rolled back.
193392** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
193393** It can still happen if the user locks the underlying tables directly
193394** instead of accessing them via FTS.
193395*/
193396static int fts3Writelock(Fts3Table *p){
193397 int rc = SQLITE_OK;
193398
193399 if( p->nPendingData==0 ){
193400 sqlite3_stmt *pStmt;
193401 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, pp: &pStmt, apVal: 0);
193402 if( rc==SQLITE_OK ){
193403 sqlite3_bind_null(pStmt, i: 1);
193404 sqlite3_step(pStmt);
193405 rc = sqlite3_reset(pStmt);
193406 }
193407 }
193408
193409 return rc;
193410}
193411
193412/*
193413** FTS maintains a separate indexes for each language-id (a 32-bit integer).
193414** Within each language id, a separate index is maintained to store the
193415** document terms, and each configured prefix size (configured the FTS
193416** "prefix=" option). And each index consists of multiple levels ("relative
193417** levels").
193418**
193419** All three of these values (the language id, the specific index and the
193420** level within the index) are encoded in 64-bit integer values stored
193421** in the %_segdir table on disk. This function is used to convert three
193422** separate component values into the single 64-bit integer value that
193423** can be used to query the %_segdir table.
193424**
193425** Specifically, each language-id/index combination is allocated 1024
193426** 64-bit integer level values ("absolute levels"). The main terms index
193427** for language-id 0 is allocate values 0-1023. The first prefix index
193428** (if any) for language-id 0 is allocated values 1024-2047. And so on.
193429** Language 1 indexes are allocated immediately following language 0.
193430**
193431** So, for a system with nPrefix prefix indexes configured, the block of
193432** absolute levels that corresponds to language-id iLangid and index
193433** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
193434*/
193435static sqlite3_int64 getAbsoluteLevel(
193436 Fts3Table *p, /* FTS3 table handle */
193437 int iLangid, /* Language id */
193438 int iIndex, /* Index in p->aIndex[] */
193439 int iLevel /* Level of segments */
193440){
193441 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
193442 assert_fts3_nc( iLangid>=0 );
193443 assert( p->nIndex>0 );
193444 assert( iIndex>=0 && iIndex<p->nIndex );
193445
193446 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
193447 return iBase + iLevel;
193448}
193449
193450/*
193451** Set *ppStmt to a statement handle that may be used to iterate through
193452** all rows in the %_segdir table, from oldest to newest. If successful,
193453** return SQLITE_OK. If an error occurs while preparing the statement,
193454** return an SQLite error code.
193455**
193456** There is only ever one instance of this SQL statement compiled for
193457** each FTS3 table.
193458**
193459** The statement returns the following columns from the %_segdir table:
193460**
193461** 0: idx
193462** 1: start_block
193463** 2: leaves_end_block
193464** 3: end_block
193465** 4: root
193466*/
193467SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
193468 Fts3Table *p, /* FTS3 table */
193469 int iLangid, /* Language being queried */
193470 int iIndex, /* Index for p->aIndex[] */
193471 int iLevel, /* Level to select (relative level) */
193472 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
193473){
193474 int rc;
193475 sqlite3_stmt *pStmt = 0;
193476
193477 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
193478 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
193479 assert( iIndex>=0 && iIndex<p->nIndex );
193480
193481 if( iLevel<0 ){
193482 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
193483 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, pp: &pStmt, apVal: 0);
193484 if( rc==SQLITE_OK ){
193485 sqlite3_bind_int64(pStmt, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel: 0));
193486 sqlite3_bind_int64(pStmt, i: 2,
193487 iValue: getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
193488 );
193489 }
193490 }else{
193491 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
193492 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, pp: &pStmt, apVal: 0);
193493 if( rc==SQLITE_OK ){
193494 sqlite3_bind_int64(pStmt, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex,iLevel));
193495 }
193496 }
193497 *ppStmt = pStmt;
193498 return rc;
193499}
193500
193501
193502/*
193503** Append a single varint to a PendingList buffer. SQLITE_OK is returned
193504** if successful, or an SQLite error code otherwise.
193505**
193506** This function also serves to allocate the PendingList structure itself.
193507** For example, to create a new PendingList structure containing two
193508** varints:
193509**
193510** PendingList *p = 0;
193511** fts3PendingListAppendVarint(&p, 1);
193512** fts3PendingListAppendVarint(&p, 2);
193513*/
193514static int fts3PendingListAppendVarint(
193515 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
193516 sqlite3_int64 i /* Value to append to data */
193517){
193518 PendingList *p = *pp;
193519
193520 /* Allocate or grow the PendingList as required. */
193521 if( !p ){
193522 p = sqlite3_malloc64(n: sizeof(*p) + 100);
193523 if( !p ){
193524 return SQLITE_NOMEM;
193525 }
193526 p->nSpace = 100;
193527 p->aData = (char *)&p[1];
193528 p->nData = 0;
193529 }
193530 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
193531 i64 nNew = p->nSpace * 2;
193532 p = sqlite3_realloc64(pOld: p, n: sizeof(*p) + nNew);
193533 if( !p ){
193534 sqlite3_free(p: *pp);
193535 *pp = 0;
193536 return SQLITE_NOMEM;
193537 }
193538 p->nSpace = (int)nNew;
193539 p->aData = (char *)&p[1];
193540 }
193541
193542 /* Append the new serialized varint to the end of the list. */
193543 p->nData += sqlite3Fts3PutVarint(p: &p->aData[p->nData], v: i);
193544 p->aData[p->nData] = '\0';
193545 *pp = p;
193546 return SQLITE_OK;
193547}
193548
193549/*
193550** Add a docid/column/position entry to a PendingList structure. Non-zero
193551** is returned if the structure is sqlite3_realloced as part of adding
193552** the entry. Otherwise, zero.
193553**
193554** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
193555** Zero is always returned in this case. Otherwise, if no OOM error occurs,
193556** it is set to SQLITE_OK.
193557*/
193558static int fts3PendingListAppend(
193559 PendingList **pp, /* IN/OUT: PendingList structure */
193560 sqlite3_int64 iDocid, /* Docid for entry to add */
193561 sqlite3_int64 iCol, /* Column for entry to add */
193562 sqlite3_int64 iPos, /* Position of term for entry to add */
193563 int *pRc /* OUT: Return code */
193564){
193565 PendingList *p = *pp;
193566 int rc = SQLITE_OK;
193567
193568 assert( !p || p->iLastDocid<=iDocid );
193569
193570 if( !p || p->iLastDocid!=iDocid ){
193571 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
193572 if( p ){
193573 assert( p->nData<p->nSpace );
193574 assert( p->aData[p->nData]==0 );
193575 p->nData++;
193576 }
193577 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(pp: &p, i: iDelta)) ){
193578 goto pendinglistappend_out;
193579 }
193580 p->iLastCol = -1;
193581 p->iLastPos = 0;
193582 p->iLastDocid = iDocid;
193583 }
193584 if( iCol>0 && p->iLastCol!=iCol ){
193585 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(pp: &p, i: 1))
193586 || SQLITE_OK!=(rc = fts3PendingListAppendVarint(pp: &p, i: iCol))
193587 ){
193588 goto pendinglistappend_out;
193589 }
193590 p->iLastCol = iCol;
193591 p->iLastPos = 0;
193592 }
193593 if( iCol>=0 ){
193594 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
193595 rc = fts3PendingListAppendVarint(pp: &p, i: 2+iPos-p->iLastPos);
193596 if( rc==SQLITE_OK ){
193597 p->iLastPos = iPos;
193598 }
193599 }
193600
193601 pendinglistappend_out:
193602 *pRc = rc;
193603 if( p!=*pp ){
193604 *pp = p;
193605 return 1;
193606 }
193607 return 0;
193608}
193609
193610/*
193611** Free a PendingList object allocated by fts3PendingListAppend().
193612*/
193613static void fts3PendingListDelete(PendingList *pList){
193614 sqlite3_free(p: pList);
193615}
193616
193617/*
193618** Add an entry to one of the pending-terms hash tables.
193619*/
193620static int fts3PendingTermsAddOne(
193621 Fts3Table *p,
193622 int iCol,
193623 int iPos,
193624 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
193625 const char *zToken,
193626 int nToken
193627){
193628 PendingList *pList;
193629 int rc = SQLITE_OK;
193630
193631 pList = (PendingList *)fts3HashFind(pH: pHash, pKey: zToken, nKey: nToken);
193632 if( pList ){
193633 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
193634 }
193635 if( fts3PendingListAppend(pp: &pList, iDocid: p->iPrevDocid, iCol, iPos, pRc: &rc) ){
193636 if( pList==fts3HashInsert(pH: pHash, pKey: zToken, nKey: nToken, data: pList) ){
193637 /* Malloc failed while inserting the new entry. This can only
193638 ** happen if there was no previous entry for this token.
193639 */
193640 assert( 0==fts3HashFind(pHash, zToken, nToken) );
193641 sqlite3_free(p: pList);
193642 rc = SQLITE_NOMEM;
193643 }
193644 }
193645 if( rc==SQLITE_OK ){
193646 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
193647 }
193648 return rc;
193649}
193650
193651/*
193652** Tokenize the nul-terminated string zText and add all tokens to the
193653** pending-terms hash-table. The docid used is that currently stored in
193654** p->iPrevDocid, and the column is specified by argument iCol.
193655**
193656** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
193657*/
193658static int fts3PendingTermsAdd(
193659 Fts3Table *p, /* Table into which text will be inserted */
193660 int iLangid, /* Language id to use */
193661 const char *zText, /* Text of document to be inserted */
193662 int iCol, /* Column into which text is being inserted */
193663 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
193664){
193665 int rc;
193666 int iStart = 0;
193667 int iEnd = 0;
193668 int iPos = 0;
193669 int nWord = 0;
193670
193671 char const *zToken;
193672 int nToken = 0;
193673
193674 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
193675 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
193676 sqlite3_tokenizer_cursor *pCsr;
193677 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
193678 const char**,int*,int*,int*,int*);
193679
193680 assert( pTokenizer && pModule );
193681
193682 /* If the user has inserted a NULL value, this function may be called with
193683 ** zText==0. In this case, add zero token entries to the hash table and
193684 ** return early. */
193685 if( zText==0 ){
193686 *pnWord = 0;
193687 return SQLITE_OK;
193688 }
193689
193690 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, z: zText, n: -1, ppCsr: &pCsr);
193691 if( rc!=SQLITE_OK ){
193692 return rc;
193693 }
193694
193695 xNext = pModule->xNext;
193696 while( SQLITE_OK==rc
193697 && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
193698 ){
193699 int i;
193700 if( iPos>=nWord ) nWord = iPos+1;
193701
193702 /* Positions cannot be negative; we use -1 as a terminator internally.
193703 ** Tokens must have a non-zero length.
193704 */
193705 if( iPos<0 || !zToken || nToken<=0 ){
193706 rc = SQLITE_ERROR;
193707 break;
193708 }
193709
193710 /* Add the term to the terms index */
193711 rc = fts3PendingTermsAddOne(
193712 p, iCol, iPos, pHash: &p->aIndex[0].hPending, zToken, nToken
193713 );
193714
193715 /* Add the term to each of the prefix indexes that it is not too
193716 ** short for. */
193717 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
193718 struct Fts3Index *pIndex = &p->aIndex[i];
193719 if( nToken<pIndex->nPrefix ) continue;
193720 rc = fts3PendingTermsAddOne(
193721 p, iCol, iPos, pHash: &pIndex->hPending, zToken, nToken: pIndex->nPrefix
193722 );
193723 }
193724 }
193725
193726 pModule->xClose(pCsr);
193727 *pnWord += nWord;
193728 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
193729}
193730
193731/*
193732** Calling this function indicates that subsequent calls to
193733** fts3PendingTermsAdd() are to add term/position-list pairs for the
193734** contents of the document with docid iDocid.
193735*/
193736static int fts3PendingTermsDocid(
193737 Fts3Table *p, /* Full-text table handle */
193738 int bDelete, /* True if this op is a delete */
193739 int iLangid, /* Language id of row being written */
193740 sqlite_int64 iDocid /* Docid of row being written */
193741){
193742 assert( iLangid>=0 );
193743 assert( bDelete==1 || bDelete==0 );
193744
193745 /* TODO(shess) Explore whether partially flushing the buffer on
193746 ** forced-flush would provide better performance. I suspect that if
193747 ** we ordered the doclists by size and flushed the largest until the
193748 ** buffer was half empty, that would let the less frequent terms
193749 ** generate longer doclists.
193750 */
193751 if( iDocid<p->iPrevDocid
193752 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
193753 || p->iPrevLangid!=iLangid
193754 || p->nPendingData>p->nMaxPendingData
193755 ){
193756 int rc = sqlite3Fts3PendingTermsFlush(p);
193757 if( rc!=SQLITE_OK ) return rc;
193758 }
193759 p->iPrevDocid = iDocid;
193760 p->iPrevLangid = iLangid;
193761 p->bPrevDelete = bDelete;
193762 return SQLITE_OK;
193763}
193764
193765/*
193766** Discard the contents of the pending-terms hash tables.
193767*/
193768SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
193769 int i;
193770 for(i=0; i<p->nIndex; i++){
193771 Fts3HashElem *pElem;
193772 Fts3Hash *pHash = &p->aIndex[i].hPending;
193773 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
193774 PendingList *pList = (PendingList *)fts3HashData(pElem);
193775 fts3PendingListDelete(pList);
193776 }
193777 fts3HashClear(pH: pHash);
193778 }
193779 p->nPendingData = 0;
193780}
193781
193782/*
193783** This function is called by the xUpdate() method as part of an INSERT
193784** operation. It adds entries for each term in the new record to the
193785** pendingTerms hash table.
193786**
193787** Argument apVal is the same as the similarly named argument passed to
193788** fts3InsertData(). Parameter iDocid is the docid of the new row.
193789*/
193790static int fts3InsertTerms(
193791 Fts3Table *p,
193792 int iLangid,
193793 sqlite3_value **apVal,
193794 u32 *aSz
193795){
193796 int i; /* Iterator variable */
193797 for(i=2; i<p->nColumn+2; i++){
193798 int iCol = i-2;
193799 if( p->abNotindexed[iCol]==0 ){
193800 const char *zText = (const char *)sqlite3_value_text(pVal: apVal[i]);
193801 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, pnWord: &aSz[iCol]);
193802 if( rc!=SQLITE_OK ){
193803 return rc;
193804 }
193805 aSz[p->nColumn] += sqlite3_value_bytes(pVal: apVal[i]);
193806 }
193807 }
193808 return SQLITE_OK;
193809}
193810
193811/*
193812** This function is called by the xUpdate() method for an INSERT operation.
193813** The apVal parameter is passed a copy of the apVal argument passed by
193814** SQLite to the xUpdate() method. i.e:
193815**
193816** apVal[0] Not used for INSERT.
193817** apVal[1] rowid
193818** apVal[2] Left-most user-defined column
193819** ...
193820** apVal[p->nColumn+1] Right-most user-defined column
193821** apVal[p->nColumn+2] Hidden column with same name as table
193822** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
193823** apVal[p->nColumn+4] Hidden languageid column
193824*/
193825static int fts3InsertData(
193826 Fts3Table *p, /* Full-text table */
193827 sqlite3_value **apVal, /* Array of values to insert */
193828 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
193829){
193830 int rc; /* Return code */
193831 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
193832
193833 if( p->zContentTbl ){
193834 sqlite3_value *pRowid = apVal[p->nColumn+3];
193835 if( sqlite3_value_type(pVal: pRowid)==SQLITE_NULL ){
193836 pRowid = apVal[1];
193837 }
193838 if( sqlite3_value_type(pVal: pRowid)!=SQLITE_INTEGER ){
193839 return SQLITE_CONSTRAINT;
193840 }
193841 *piDocid = sqlite3_value_int64(pVal: pRowid);
193842 return SQLITE_OK;
193843 }
193844
193845 /* Locate the statement handle used to insert data into the %_content
193846 ** table. The SQL for this statement is:
193847 **
193848 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
193849 **
193850 ** The statement features N '?' variables, where N is the number of user
193851 ** defined columns in the FTS3 table, plus one for the docid field.
193852 */
193853 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, pp: &pContentInsert, apVal: &apVal[1]);
193854 if( rc==SQLITE_OK && p->zLanguageid ){
193855 rc = sqlite3_bind_int(
193856 p: pContentInsert, i: p->nColumn+2,
193857 iValue: sqlite3_value_int(pVal: apVal[p->nColumn+4])
193858 );
193859 }
193860 if( rc!=SQLITE_OK ) return rc;
193861
193862 /* There is a quirk here. The users INSERT statement may have specified
193863 ** a value for the "rowid" field, for the "docid" field, or for both.
193864 ** Which is a problem, since "rowid" and "docid" are aliases for the
193865 ** same value. For example:
193866 **
193867 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
193868 **
193869 ** In FTS3, this is an error. It is an error to specify non-NULL values
193870 ** for both docid and some other rowid alias.
193871 */
193872 if( SQLITE_NULL!=sqlite3_value_type(pVal: apVal[3+p->nColumn]) ){
193873 if( SQLITE_NULL==sqlite3_value_type(pVal: apVal[0])
193874 && SQLITE_NULL!=sqlite3_value_type(pVal: apVal[1])
193875 ){
193876 /* A rowid/docid conflict. */
193877 return SQLITE_ERROR;
193878 }
193879 rc = sqlite3_bind_value(pStmt: pContentInsert, i: 1, pValue: apVal[3+p->nColumn]);
193880 if( rc!=SQLITE_OK ) return rc;
193881 }
193882
193883 /* Execute the statement to insert the record. Set *piDocid to the
193884 ** new docid value.
193885 */
193886 sqlite3_step(pStmt: pContentInsert);
193887 rc = sqlite3_reset(pStmt: pContentInsert);
193888
193889 *piDocid = sqlite3_last_insert_rowid(db: p->db);
193890 return rc;
193891}
193892
193893
193894
193895/*
193896** Remove all data from the FTS3 table. Clear the hash table containing
193897** pending terms.
193898*/
193899static int fts3DeleteAll(Fts3Table *p, int bContent){
193900 int rc = SQLITE_OK; /* Return code */
193901
193902 /* Discard the contents of the pending-terms hash table. */
193903 sqlite3Fts3PendingTermsClear(p);
193904
193905 /* Delete everything from the shadow tables. Except, leave %_content as
193906 ** is if bContent is false. */
193907 assert( p->zContentTbl==0 || bContent==0 );
193908 if( bContent ) fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_CONTENT, apVal: 0);
193909 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_SEGMENTS, apVal: 0);
193910 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_SEGDIR, apVal: 0);
193911 if( p->bHasDocsize ){
193912 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_DOCSIZE, apVal: 0);
193913 }
193914 if( p->bHasStat ){
193915 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_STAT, apVal: 0);
193916 }
193917 return rc;
193918}
193919
193920/*
193921**
193922*/
193923static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
193924 int iLangid = 0;
193925 if( p->zLanguageid ) iLangid = sqlite3_column_int(pStmt: pSelect, i: p->nColumn+1);
193926 return iLangid;
193927}
193928
193929/*
193930** The first element in the apVal[] array is assumed to contain the docid
193931** (an integer) of a row about to be deleted. Remove all terms from the
193932** full-text index.
193933*/
193934static void fts3DeleteTerms(
193935 int *pRC, /* Result code */
193936 Fts3Table *p, /* The FTS table to delete from */
193937 sqlite3_value *pRowid, /* The docid to be deleted */
193938 u32 *aSz, /* Sizes of deleted document written here */
193939 int *pbFound /* OUT: Set to true if row really does exist */
193940){
193941 int rc;
193942 sqlite3_stmt *pSelect;
193943
193944 assert( *pbFound==0 );
193945 if( *pRC ) return;
193946 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, pp: &pSelect, apVal: &pRowid);
193947 if( rc==SQLITE_OK ){
193948 if( SQLITE_ROW==sqlite3_step(pStmt: pSelect) ){
193949 int i;
193950 int iLangid = langidFromSelect(p, pSelect);
193951 i64 iDocid = sqlite3_column_int64(pStmt: pSelect, i: 0);
193952 rc = fts3PendingTermsDocid(p, bDelete: 1, iLangid, iDocid);
193953 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
193954 int iCol = i-1;
193955 if( p->abNotindexed[iCol]==0 ){
193956 const char *zText = (const char *)sqlite3_column_text(pStmt: pSelect, i);
193957 rc = fts3PendingTermsAdd(p, iLangid, zText, iCol: -1, pnWord: &aSz[iCol]);
193958 aSz[p->nColumn] += sqlite3_column_bytes(pStmt: pSelect, i);
193959 }
193960 }
193961 if( rc!=SQLITE_OK ){
193962 sqlite3_reset(pStmt: pSelect);
193963 *pRC = rc;
193964 return;
193965 }
193966 *pbFound = 1;
193967 }
193968 rc = sqlite3_reset(pStmt: pSelect);
193969 }else{
193970 sqlite3_reset(pStmt: pSelect);
193971 }
193972 *pRC = rc;
193973}
193974
193975/*
193976** Forward declaration to account for the circular dependency between
193977** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
193978*/
193979static int fts3SegmentMerge(Fts3Table *, int, int, int);
193980
193981/*
193982** This function allocates a new level iLevel index in the segdir table.
193983** Usually, indexes are allocated within a level sequentially starting
193984** with 0, so the allocated index is one greater than the value returned
193985** by:
193986**
193987** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
193988**
193989** However, if there are already FTS3_MERGE_COUNT indexes at the requested
193990** level, they are merged into a single level (iLevel+1) segment and the
193991** allocated index is 0.
193992**
193993** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
193994** returned. Otherwise, an SQLite error code is returned.
193995*/
193996static int fts3AllocateSegdirIdx(
193997 Fts3Table *p,
193998 int iLangid, /* Language id */
193999 int iIndex, /* Index for p->aIndex */
194000 int iLevel,
194001 int *piIdx
194002){
194003 int rc; /* Return Code */
194004 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
194005 int iNext = 0; /* Result of query pNextIdx */
194006
194007 assert( iLangid>=0 );
194008 assert( p->nIndex>=1 );
194009
194010 /* Set variable iNext to the next available segdir index at level iLevel. */
194011 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, pp: &pNextIdx, apVal: 0);
194012 if( rc==SQLITE_OK ){
194013 sqlite3_bind_int64(
194014 pStmt: pNextIdx, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel)
194015 );
194016 if( SQLITE_ROW==sqlite3_step(pStmt: pNextIdx) ){
194017 iNext = sqlite3_column_int(pStmt: pNextIdx, i: 0);
194018 }
194019 rc = sqlite3_reset(pStmt: pNextIdx);
194020 }
194021
194022 if( rc==SQLITE_OK ){
194023 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
194024 ** full, merge all segments in level iLevel into a single iLevel+1
194025 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
194026 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
194027 */
194028 if( iNext>=MergeCount(p) ){
194029 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
194030 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
194031 *piIdx = 0;
194032 }else{
194033 *piIdx = iNext;
194034 }
194035 }
194036
194037 return rc;
194038}
194039
194040/*
194041** The %_segments table is declared as follows:
194042**
194043** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
194044**
194045** This function reads data from a single row of the %_segments table. The
194046** specific row is identified by the iBlockid parameter. If paBlob is not
194047** NULL, then a buffer is allocated using sqlite3_malloc() and populated
194048** with the contents of the blob stored in the "block" column of the
194049** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
194050** to the size of the blob in bytes before returning.
194051**
194052** If an error occurs, or the table does not contain the specified row,
194053** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
194054** paBlob is non-NULL, then it is the responsibility of the caller to
194055** eventually free the returned buffer.
194056**
194057** This function may leave an open sqlite3_blob* handle in the
194058** Fts3Table.pSegments variable. This handle is reused by subsequent calls
194059** to this function. The handle may be closed by calling the
194060** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
194061** performance improvement, but the blob handle should always be closed
194062** before control is returned to the user (to prevent a lock being held
194063** on the database file for longer than necessary). Thus, any virtual table
194064** method (xFilter etc.) that may directly or indirectly call this function
194065** must call sqlite3Fts3SegmentsClose() before returning.
194066*/
194067SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
194068 Fts3Table *p, /* FTS3 table handle */
194069 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
194070 char **paBlob, /* OUT: Blob data in malloc'd buffer */
194071 int *pnBlob, /* OUT: Size of blob data */
194072 int *pnLoad /* OUT: Bytes actually loaded */
194073){
194074 int rc; /* Return code */
194075
194076 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
194077 assert( pnBlob );
194078
194079 if( p->pSegments ){
194080 rc = sqlite3_blob_reopen(pBlob: p->pSegments, iRow: iBlockid);
194081 }else{
194082 if( 0==p->zSegmentsTbl ){
194083 p->zSegmentsTbl = sqlite3_mprintf(zFormat: "%s_segments", p->zName);
194084 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
194085 }
194086 rc = sqlite3_blob_open(
194087 db: p->db, zDb: p->zDb, zTable: p->zSegmentsTbl, zColumn: "block", iRow: iBlockid, wrFlag: 0, ppBlob: &p->pSegments
194088 );
194089 }
194090
194091 if( rc==SQLITE_OK ){
194092 int nByte = sqlite3_blob_bytes(pBlob: p->pSegments);
194093 *pnBlob = nByte;
194094 if( paBlob ){
194095 char *aByte = sqlite3_malloc64(n: (i64)nByte + FTS3_NODE_PADDING);
194096 if( !aByte ){
194097 rc = SQLITE_NOMEM;
194098 }else{
194099 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
194100 nByte = FTS3_NODE_CHUNKSIZE;
194101 *pnLoad = nByte;
194102 }
194103 rc = sqlite3_blob_read(pBlob: p->pSegments, z: aByte, n: nByte, iOffset: 0);
194104 memset(s: &aByte[nByte], c: 0, FTS3_NODE_PADDING);
194105 if( rc!=SQLITE_OK ){
194106 sqlite3_free(p: aByte);
194107 aByte = 0;
194108 }
194109 }
194110 *paBlob = aByte;
194111 }
194112 }else if( rc==SQLITE_ERROR ){
194113 rc = FTS_CORRUPT_VTAB;
194114 }
194115
194116 return rc;
194117}
194118
194119/*
194120** Close the blob handle at p->pSegments, if it is open. See comments above
194121** the sqlite3Fts3ReadBlock() function for details.
194122*/
194123SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
194124 sqlite3_blob_close(pBlob: p->pSegments);
194125 p->pSegments = 0;
194126}
194127
194128static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
194129 int nRead; /* Number of bytes to read */
194130 int rc; /* Return code */
194131
194132 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
194133 rc = sqlite3_blob_read(
194134 pBlob: pReader->pBlob,
194135 z: &pReader->aNode[pReader->nPopulate],
194136 n: nRead,
194137 iOffset: pReader->nPopulate
194138 );
194139
194140 if( rc==SQLITE_OK ){
194141 pReader->nPopulate += nRead;
194142 memset(s: &pReader->aNode[pReader->nPopulate], c: 0, FTS3_NODE_PADDING);
194143 if( pReader->nPopulate==pReader->nNode ){
194144 sqlite3_blob_close(pBlob: pReader->pBlob);
194145 pReader->pBlob = 0;
194146 pReader->nPopulate = 0;
194147 }
194148 }
194149 return rc;
194150}
194151
194152static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
194153 int rc = SQLITE_OK;
194154 assert( !pReader->pBlob
194155 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
194156 );
194157 while( pReader->pBlob && rc==SQLITE_OK
194158 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
194159 ){
194160 rc = fts3SegReaderIncrRead(pReader);
194161 }
194162 return rc;
194163}
194164
194165/*
194166** Set an Fts3SegReader cursor to point at EOF.
194167*/
194168static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
194169 if( !fts3SegReaderIsRootOnly(pSeg) ){
194170 sqlite3_free(p: pSeg->aNode);
194171 sqlite3_blob_close(pBlob: pSeg->pBlob);
194172 pSeg->pBlob = 0;
194173 }
194174 pSeg->aNode = 0;
194175}
194176
194177/*
194178** Move the iterator passed as the first argument to the next term in the
194179** segment. If successful, SQLITE_OK is returned. If there is no next term,
194180** SQLITE_DONE. Otherwise, an SQLite error code.
194181*/
194182static int fts3SegReaderNext(
194183 Fts3Table *p,
194184 Fts3SegReader *pReader,
194185 int bIncr
194186){
194187 int rc; /* Return code of various sub-routines */
194188 char *pNext; /* Cursor variable */
194189 int nPrefix; /* Number of bytes in term prefix */
194190 int nSuffix; /* Number of bytes in term suffix */
194191
194192 if( !pReader->aDoclist ){
194193 pNext = pReader->aNode;
194194 }else{
194195 pNext = &pReader->aDoclist[pReader->nDoclist];
194196 }
194197
194198 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
194199
194200 if( fts3SegReaderIsPending(pReader) ){
194201 Fts3HashElem *pElem = *(pReader->ppNextElem);
194202 sqlite3_free(p: pReader->aNode);
194203 pReader->aNode = 0;
194204 if( pElem ){
194205 char *aCopy;
194206 PendingList *pList = (PendingList *)fts3HashData(pElem);
194207 int nCopy = pList->nData+1;
194208
194209 int nTerm = fts3HashKeysize(pElem);
194210 if( (nTerm+1)>pReader->nTermAlloc ){
194211 sqlite3_free(p: pReader->zTerm);
194212 pReader->zTerm = (char*)sqlite3_malloc64(n: ((i64)nTerm+1)*2);
194213 if( !pReader->zTerm ) return SQLITE_NOMEM;
194214 pReader->nTermAlloc = (nTerm+1)*2;
194215 }
194216 memcpy(dest: pReader->zTerm, fts3HashKey(pElem), n: nTerm);
194217 pReader->zTerm[nTerm] = '\0';
194218 pReader->nTerm = nTerm;
194219
194220 aCopy = (char*)sqlite3_malloc64(n: nCopy);
194221 if( !aCopy ) return SQLITE_NOMEM;
194222 memcpy(dest: aCopy, src: pList->aData, n: nCopy);
194223 pReader->nNode = pReader->nDoclist = nCopy;
194224 pReader->aNode = pReader->aDoclist = aCopy;
194225 pReader->ppNextElem++;
194226 assert( pReader->aNode );
194227 }
194228 return SQLITE_OK;
194229 }
194230
194231 fts3SegReaderSetEof(pSeg: pReader);
194232
194233 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
194234 ** blocks have already been traversed. */
194235#ifdef CORRUPT_DB
194236 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
194237#endif
194238 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
194239 return SQLITE_OK;
194240 }
194241
194242 rc = sqlite3Fts3ReadBlock(
194243 p, iBlockid: ++pReader->iCurrentBlock, paBlob: &pReader->aNode, pnBlob: &pReader->nNode,
194244 pnLoad: (bIncr ? &pReader->nPopulate : 0)
194245 );
194246 if( rc!=SQLITE_OK ) return rc;
194247 assert( pReader->pBlob==0 );
194248 if( bIncr && pReader->nPopulate<pReader->nNode ){
194249 pReader->pBlob = p->pSegments;
194250 p->pSegments = 0;
194251 }
194252 pNext = pReader->aNode;
194253 }
194254
194255 assert( !fts3SegReaderIsPending(pReader) );
194256
194257 rc = fts3SegReaderRequire(pReader, pFrom: pNext, FTS3_VARINT_MAX*2);
194258 if( rc!=SQLITE_OK ) return rc;
194259
194260 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
194261 ** safe (no risk of overread) even if the node data is corrupted. */
194262 pNext += fts3GetVarint32(pNext, &nPrefix);
194263 pNext += fts3GetVarint32(pNext, &nSuffix);
194264 if( nSuffix<=0
194265 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
194266 || nPrefix>pReader->nTerm
194267 ){
194268 return FTS_CORRUPT_VTAB;
194269 }
194270
194271 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
194272 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
194273 ** overflow - hence the (i64) casts. */
194274 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
194275 i64 nNew = ((i64)nPrefix+nSuffix)*2;
194276 char *zNew = sqlite3_realloc64(pOld: pReader->zTerm, n: nNew);
194277 if( !zNew ){
194278 return SQLITE_NOMEM;
194279 }
194280 pReader->zTerm = zNew;
194281 pReader->nTermAlloc = nNew;
194282 }
194283
194284 rc = fts3SegReaderRequire(pReader, pFrom: pNext, nByte: nSuffix+FTS3_VARINT_MAX);
194285 if( rc!=SQLITE_OK ) return rc;
194286
194287 memcpy(dest: &pReader->zTerm[nPrefix], src: pNext, n: nSuffix);
194288 pReader->nTerm = nPrefix+nSuffix;
194289 pNext += nSuffix;
194290 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
194291 pReader->aDoclist = pNext;
194292 pReader->pOffsetList = 0;
194293
194294 /* Check that the doclist does not appear to extend past the end of the
194295 ** b-tree node. And that the final byte of the doclist is 0x00. If either
194296 ** of these statements is untrue, then the data structure is corrupt.
194297 */
194298 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
194299 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
194300 || pReader->nDoclist==0
194301 ){
194302 return FTS_CORRUPT_VTAB;
194303 }
194304 return SQLITE_OK;
194305}
194306
194307/*
194308** Set the SegReader to point to the first docid in the doclist associated
194309** with the current term.
194310*/
194311static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
194312 int rc = SQLITE_OK;
194313 assert( pReader->aDoclist );
194314 assert( !pReader->pOffsetList );
194315 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
194316 u8 bEof = 0;
194317 pReader->iDocid = 0;
194318 pReader->nOffsetList = 0;
194319 sqlite3Fts3DoclistPrev(bDescIdx: 0,
194320 aDoclist: pReader->aDoclist, nDoclist: pReader->nDoclist, ppIter: &pReader->pOffsetList,
194321 piDocid: &pReader->iDocid, pnList: &pReader->nOffsetList, pbEof: &bEof
194322 );
194323 }else{
194324 rc = fts3SegReaderRequire(pReader, pFrom: pReader->aDoclist, FTS3_VARINT_MAX);
194325 if( rc==SQLITE_OK ){
194326 int n = sqlite3Fts3GetVarint(pBuf: pReader->aDoclist, v: &pReader->iDocid);
194327 pReader->pOffsetList = &pReader->aDoclist[n];
194328 }
194329 }
194330 return rc;
194331}
194332
194333/*
194334** Advance the SegReader to point to the next docid in the doclist
194335** associated with the current term.
194336**
194337** If arguments ppOffsetList and pnOffsetList are not NULL, then
194338** *ppOffsetList is set to point to the first column-offset list
194339** in the doclist entry (i.e. immediately past the docid varint).
194340** *pnOffsetList is set to the length of the set of column-offset
194341** lists, not including the nul-terminator byte. For example:
194342*/
194343static int fts3SegReaderNextDocid(
194344 Fts3Table *pTab,
194345 Fts3SegReader *pReader, /* Reader to advance to next docid */
194346 char **ppOffsetList, /* OUT: Pointer to current position-list */
194347 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
194348){
194349 int rc = SQLITE_OK;
194350 char *p = pReader->pOffsetList;
194351 char c = 0;
194352
194353 assert( p );
194354
194355 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
194356 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
194357 ** Pending-terms doclists are always built up in ascending order, so
194358 ** we have to iterate through them backwards here. */
194359 u8 bEof = 0;
194360 if( ppOffsetList ){
194361 *ppOffsetList = pReader->pOffsetList;
194362 *pnOffsetList = pReader->nOffsetList - 1;
194363 }
194364 sqlite3Fts3DoclistPrev(bDescIdx: 0,
194365 aDoclist: pReader->aDoclist, nDoclist: pReader->nDoclist, ppIter: &p, piDocid: &pReader->iDocid,
194366 pnList: &pReader->nOffsetList, pbEof: &bEof
194367 );
194368 if( bEof ){
194369 pReader->pOffsetList = 0;
194370 }else{
194371 pReader->pOffsetList = p;
194372 }
194373 }else{
194374 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
194375
194376 /* Pointer p currently points at the first byte of an offset list. The
194377 ** following block advances it to point one byte past the end of
194378 ** the same offset list. */
194379 while( 1 ){
194380
194381 /* The following line of code (and the "p++" below the while() loop) is
194382 ** normally all that is required to move pointer p to the desired
194383 ** position. The exception is if this node is being loaded from disk
194384 ** incrementally and pointer "p" now points to the first byte past
194385 ** the populated part of pReader->aNode[].
194386 */
194387 while( *p | c ) c = *p++ & 0x80;
194388 assert( *p==0 );
194389
194390 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
194391 rc = fts3SegReaderIncrRead(pReader);
194392 if( rc!=SQLITE_OK ) return rc;
194393 }
194394 p++;
194395
194396 /* If required, populate the output variables with a pointer to and the
194397 ** size of the previous offset-list.
194398 */
194399 if( ppOffsetList ){
194400 *ppOffsetList = pReader->pOffsetList;
194401 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
194402 }
194403
194404 /* List may have been edited in place by fts3EvalNearTrim() */
194405 while( p<pEnd && *p==0 ) p++;
194406
194407 /* If there are no more entries in the doclist, set pOffsetList to
194408 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
194409 ** Fts3SegReader.pOffsetList to point to the next offset list before
194410 ** returning.
194411 */
194412 if( p>=pEnd ){
194413 pReader->pOffsetList = 0;
194414 }else{
194415 rc = fts3SegReaderRequire(pReader, pFrom: p, FTS3_VARINT_MAX);
194416 if( rc==SQLITE_OK ){
194417 u64 iDelta;
194418 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(pBuf: p, v: &iDelta);
194419 if( pTab->bDescIdx ){
194420 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
194421 }else{
194422 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
194423 }
194424 }
194425 }
194426 }
194427
194428 return rc;
194429}
194430
194431
194432SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
194433 Fts3Cursor *pCsr,
194434 Fts3MultiSegReader *pMsr,
194435 int *pnOvfl
194436){
194437 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
194438 int nOvfl = 0;
194439 int ii;
194440 int rc = SQLITE_OK;
194441 int pgsz = p->nPgsz;
194442
194443 assert( p->bFts4 );
194444 assert( pgsz>0 );
194445
194446 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
194447 Fts3SegReader *pReader = pMsr->apSegment[ii];
194448 if( !fts3SegReaderIsPending(pReader)
194449 && !fts3SegReaderIsRootOnly(pReader)
194450 ){
194451 sqlite3_int64 jj;
194452 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
194453 int nBlob;
194454 rc = sqlite3Fts3ReadBlock(p, iBlockid: jj, paBlob: 0, pnBlob: &nBlob, pnLoad: 0);
194455 if( rc!=SQLITE_OK ) break;
194456 if( (nBlob+35)>pgsz ){
194457 nOvfl += (nBlob + 34)/pgsz;
194458 }
194459 }
194460 }
194461 }
194462 *pnOvfl = nOvfl;
194463 return rc;
194464}
194465
194466/*
194467** Free all allocations associated with the iterator passed as the
194468** second argument.
194469*/
194470SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
194471 if( pReader ){
194472 sqlite3_free(p: pReader->zTerm);
194473 if( !fts3SegReaderIsRootOnly(pReader) ){
194474 sqlite3_free(p: pReader->aNode);
194475 }
194476 sqlite3_blob_close(pBlob: pReader->pBlob);
194477 }
194478 sqlite3_free(p: pReader);
194479}
194480
194481/*
194482** Allocate a new SegReader object.
194483*/
194484SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
194485 int iAge, /* Segment "age". */
194486 int bLookup, /* True for a lookup only */
194487 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
194488 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
194489 sqlite3_int64 iEndBlock, /* Final block of segment */
194490 const char *zRoot, /* Buffer containing root node */
194491 int nRoot, /* Size of buffer containing root node */
194492 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
194493){
194494 Fts3SegReader *pReader; /* Newly allocated SegReader object */
194495 int nExtra = 0; /* Bytes to allocate segment root node */
194496
194497 assert( zRoot!=0 || nRoot==0 );
194498#ifdef CORRUPT_DB
194499 assert( zRoot!=0 || CORRUPT_DB );
194500#endif
194501
194502 if( iStartLeaf==0 ){
194503 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB;
194504 nExtra = nRoot + FTS3_NODE_PADDING;
194505 }
194506
194507 pReader = (Fts3SegReader *)sqlite3_malloc64(n: sizeof(Fts3SegReader) + nExtra);
194508 if( !pReader ){
194509 return SQLITE_NOMEM;
194510 }
194511 memset(s: pReader, c: 0, n: sizeof(Fts3SegReader));
194512 pReader->iIdx = iAge;
194513 pReader->bLookup = bLookup!=0;
194514 pReader->iStartBlock = iStartLeaf;
194515 pReader->iLeafEndBlock = iEndLeaf;
194516 pReader->iEndBlock = iEndBlock;
194517
194518 if( nExtra ){
194519 /* The entire segment is stored in the root node. */
194520 pReader->aNode = (char *)&pReader[1];
194521 pReader->rootOnly = 1;
194522 pReader->nNode = nRoot;
194523 if( nRoot ) memcpy(dest: pReader->aNode, src: zRoot, n: nRoot);
194524 memset(s: &pReader->aNode[nRoot], c: 0, FTS3_NODE_PADDING);
194525 }else{
194526 pReader->iCurrentBlock = iStartLeaf-1;
194527 }
194528 *ppReader = pReader;
194529 return SQLITE_OK;
194530}
194531
194532/*
194533** This is a comparison function used as a qsort() callback when sorting
194534** an array of pending terms by term. This occurs as part of flushing
194535** the contents of the pending-terms hash table to the database.
194536*/
194537static int SQLITE_CDECL fts3CompareElemByTerm(
194538 const void *lhs,
194539 const void *rhs
194540){
194541 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
194542 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
194543 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
194544 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
194545
194546 int n = (n1<n2 ? n1 : n2);
194547 int c = memcmp(s1: z1, s2: z2, n: n);
194548 if( c==0 ){
194549 c = n1 - n2;
194550 }
194551 return c;
194552}
194553
194554/*
194555** This function is used to allocate an Fts3SegReader that iterates through
194556** a subset of the terms stored in the Fts3Table.pendingTerms array.
194557**
194558** If the isPrefixIter parameter is zero, then the returned SegReader iterates
194559** through each term in the pending-terms table. Or, if isPrefixIter is
194560** non-zero, it iterates through each term and its prefixes. For example, if
194561** the pending terms hash table contains the terms "sqlite", "mysql" and
194562** "firebird", then the iterator visits the following 'terms' (in the order
194563** shown):
194564**
194565** f fi fir fire fireb firebi firebir firebird
194566** m my mys mysq mysql
194567** s sq sql sqli sqlit sqlite
194568**
194569** Whereas if isPrefixIter is zero, the terms visited are:
194570**
194571** firebird mysql sqlite
194572*/
194573SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
194574 Fts3Table *p, /* Virtual table handle */
194575 int iIndex, /* Index for p->aIndex */
194576 const char *zTerm, /* Term to search for */
194577 int nTerm, /* Size of buffer zTerm */
194578 int bPrefix, /* True for a prefix iterator */
194579 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
194580){
194581 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
194582 Fts3HashElem *pE; /* Iterator variable */
194583 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
194584 int nElem = 0; /* Size of array at aElem */
194585 int rc = SQLITE_OK; /* Return Code */
194586 Fts3Hash *pHash;
194587
194588 pHash = &p->aIndex[iIndex].hPending;
194589 if( bPrefix ){
194590 int nAlloc = 0; /* Size of allocated array at aElem */
194591
194592 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
194593 char *zKey = (char *)fts3HashKey(pE);
194594 int nKey = fts3HashKeysize(pE);
194595 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(s1: zKey, s2: zTerm, n: nTerm)) ){
194596 if( nElem==nAlloc ){
194597 Fts3HashElem **aElem2;
194598 nAlloc += 16;
194599 aElem2 = (Fts3HashElem **)sqlite3_realloc64(
194600 pOld: aElem, n: nAlloc*sizeof(Fts3HashElem *)
194601 );
194602 if( !aElem2 ){
194603 rc = SQLITE_NOMEM;
194604 nElem = 0;
194605 break;
194606 }
194607 aElem = aElem2;
194608 }
194609
194610 aElem[nElem++] = pE;
194611 }
194612 }
194613
194614 /* If more than one term matches the prefix, sort the Fts3HashElem
194615 ** objects in term order using qsort(). This uses the same comparison
194616 ** callback as is used when flushing terms to disk.
194617 */
194618 if( nElem>1 ){
194619 qsort(base: aElem, nmemb: nElem, size: sizeof(Fts3HashElem *), compar: fts3CompareElemByTerm);
194620 }
194621
194622 }else{
194623 /* The query is a simple term lookup that matches at most one term in
194624 ** the index. All that is required is a straight hash-lookup.
194625 **
194626 ** Because the stack address of pE may be accessed via the aElem pointer
194627 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
194628 ** within this entire function, not just this "else{...}" block.
194629 */
194630 pE = fts3HashFindElem(pH: pHash, pKey: zTerm, nKey: nTerm);
194631 if( pE ){
194632 aElem = &pE;
194633 nElem = 1;
194634 }
194635 }
194636
194637 if( nElem>0 ){
194638 sqlite3_int64 nByte;
194639 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
194640 pReader = (Fts3SegReader *)sqlite3_malloc64(n: nByte);
194641 if( !pReader ){
194642 rc = SQLITE_NOMEM;
194643 }else{
194644 memset(s: pReader, c: 0, n: nByte);
194645 pReader->iIdx = 0x7FFFFFFF;
194646 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
194647 memcpy(dest: pReader->ppNextElem, src: aElem, n: nElem*sizeof(Fts3HashElem *));
194648 }
194649 }
194650
194651 if( bPrefix ){
194652 sqlite3_free(p: aElem);
194653 }
194654 *ppReader = pReader;
194655 return rc;
194656}
194657
194658/*
194659** Compare the entries pointed to by two Fts3SegReader structures.
194660** Comparison is as follows:
194661**
194662** 1) EOF is greater than not EOF.
194663**
194664** 2) The current terms (if any) are compared using memcmp(). If one
194665** term is a prefix of another, the longer term is considered the
194666** larger.
194667**
194668** 3) By segment age. An older segment is considered larger.
194669*/
194670static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
194671 int rc;
194672 if( pLhs->aNode && pRhs->aNode ){
194673 int rc2 = pLhs->nTerm - pRhs->nTerm;
194674 if( rc2<0 ){
194675 rc = memcmp(s1: pLhs->zTerm, s2: pRhs->zTerm, n: pLhs->nTerm);
194676 }else{
194677 rc = memcmp(s1: pLhs->zTerm, s2: pRhs->zTerm, n: pRhs->nTerm);
194678 }
194679 if( rc==0 ){
194680 rc = rc2;
194681 }
194682 }else{
194683 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
194684 }
194685 if( rc==0 ){
194686 rc = pRhs->iIdx - pLhs->iIdx;
194687 }
194688 assert_fts3_nc( rc!=0 );
194689 return rc;
194690}
194691
194692/*
194693** A different comparison function for SegReader structures. In this
194694** version, it is assumed that each SegReader points to an entry in
194695** a doclist for identical terms. Comparison is made as follows:
194696**
194697** 1) EOF (end of doclist in this case) is greater than not EOF.
194698**
194699** 2) By current docid.
194700**
194701** 3) By segment age. An older segment is considered larger.
194702*/
194703static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
194704 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
194705 if( rc==0 ){
194706 if( pLhs->iDocid==pRhs->iDocid ){
194707 rc = pRhs->iIdx - pLhs->iIdx;
194708 }else{
194709 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
194710 }
194711 }
194712 assert( pLhs->aNode && pRhs->aNode );
194713 return rc;
194714}
194715static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
194716 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
194717 if( rc==0 ){
194718 if( pLhs->iDocid==pRhs->iDocid ){
194719 rc = pRhs->iIdx - pLhs->iIdx;
194720 }else{
194721 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
194722 }
194723 }
194724 assert( pLhs->aNode && pRhs->aNode );
194725 return rc;
194726}
194727
194728/*
194729** Compare the term that the Fts3SegReader object passed as the first argument
194730** points to with the term specified by arguments zTerm and nTerm.
194731**
194732** If the pSeg iterator is already at EOF, return 0. Otherwise, return
194733** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
194734** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
194735*/
194736static int fts3SegReaderTermCmp(
194737 Fts3SegReader *pSeg, /* Segment reader object */
194738 const char *zTerm, /* Term to compare to */
194739 int nTerm /* Size of term zTerm in bytes */
194740){
194741 int res = 0;
194742 if( pSeg->aNode ){
194743 if( pSeg->nTerm>nTerm ){
194744 res = memcmp(s1: pSeg->zTerm, s2: zTerm, n: nTerm);
194745 }else{
194746 res = memcmp(s1: pSeg->zTerm, s2: zTerm, n: pSeg->nTerm);
194747 }
194748 if( res==0 ){
194749 res = pSeg->nTerm-nTerm;
194750 }
194751 }
194752 return res;
194753}
194754
194755/*
194756** Argument apSegment is an array of nSegment elements. It is known that
194757** the final (nSegment-nSuspect) members are already in sorted order
194758** (according to the comparison function provided). This function shuffles
194759** the array around until all entries are in sorted order.
194760*/
194761static void fts3SegReaderSort(
194762 Fts3SegReader **apSegment, /* Array to sort entries of */
194763 int nSegment, /* Size of apSegment array */
194764 int nSuspect, /* Unsorted entry count */
194765 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
194766){
194767 int i; /* Iterator variable */
194768
194769 assert( nSuspect<=nSegment );
194770
194771 if( nSuspect==nSegment ) nSuspect--;
194772 for(i=nSuspect-1; i>=0; i--){
194773 int j;
194774 for(j=i; j<(nSegment-1); j++){
194775 Fts3SegReader *pTmp;
194776 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
194777 pTmp = apSegment[j+1];
194778 apSegment[j+1] = apSegment[j];
194779 apSegment[j] = pTmp;
194780 }
194781 }
194782
194783#ifndef NDEBUG
194784 /* Check that the list really is sorted now. */
194785 for(i=0; i<(nSuspect-1); i++){
194786 assert( xCmp(apSegment[i], apSegment[i+1])<0 );
194787 }
194788#endif
194789}
194790
194791/*
194792** Insert a record into the %_segments table.
194793*/
194794static int fts3WriteSegment(
194795 Fts3Table *p, /* Virtual table handle */
194796 sqlite3_int64 iBlock, /* Block id for new block */
194797 char *z, /* Pointer to buffer containing block data */
194798 int n /* Size of buffer z in bytes */
194799){
194800 sqlite3_stmt *pStmt;
194801 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, pp: &pStmt, apVal: 0);
194802 if( rc==SQLITE_OK ){
194803 sqlite3_bind_int64(pStmt, i: 1, iValue: iBlock);
194804 sqlite3_bind_blob(pStmt, i: 2, zData: z, nData: n, SQLITE_STATIC);
194805 sqlite3_step(pStmt);
194806 rc = sqlite3_reset(pStmt);
194807 sqlite3_bind_null(pStmt, i: 2);
194808 }
194809 return rc;
194810}
194811
194812/*
194813** Find the largest relative level number in the table. If successful, set
194814** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
194815** set *pnMax to zero and return an SQLite error code.
194816*/
194817SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
194818 int rc;
194819 int mxLevel = 0;
194820 sqlite3_stmt *pStmt = 0;
194821
194822 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, pp: &pStmt, apVal: 0);
194823 if( rc==SQLITE_OK ){
194824 if( SQLITE_ROW==sqlite3_step(pStmt) ){
194825 mxLevel = sqlite3_column_int(pStmt, i: 0);
194826 }
194827 rc = sqlite3_reset(pStmt);
194828 }
194829 *pnMax = mxLevel;
194830 return rc;
194831}
194832
194833/*
194834** Insert a record into the %_segdir table.
194835*/
194836static int fts3WriteSegdir(
194837 Fts3Table *p, /* Virtual table handle */
194838 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
194839 int iIdx, /* Value for "idx" field */
194840 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
194841 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
194842 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
194843 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
194844 char *zRoot, /* Blob value for "root" field */
194845 int nRoot /* Number of bytes in buffer zRoot */
194846){
194847 sqlite3_stmt *pStmt;
194848 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, pp: &pStmt, apVal: 0);
194849 if( rc==SQLITE_OK ){
194850 sqlite3_bind_int64(pStmt, i: 1, iValue: iLevel);
194851 sqlite3_bind_int(p: pStmt, i: 2, iValue: iIdx);
194852 sqlite3_bind_int64(pStmt, i: 3, iValue: iStartBlock);
194853 sqlite3_bind_int64(pStmt, i: 4, iValue: iLeafEndBlock);
194854 if( nLeafData==0 ){
194855 sqlite3_bind_int64(pStmt, i: 5, iValue: iEndBlock);
194856 }else{
194857 char *zEnd = sqlite3_mprintf(zFormat: "%lld %lld", iEndBlock, nLeafData);
194858 if( !zEnd ) return SQLITE_NOMEM;
194859 sqlite3_bind_text(pStmt, i: 5, zData: zEnd, nData: -1, xDel: sqlite3_free);
194860 }
194861 sqlite3_bind_blob(pStmt, i: 6, zData: zRoot, nData: nRoot, SQLITE_STATIC);
194862 sqlite3_step(pStmt);
194863 rc = sqlite3_reset(pStmt);
194864 sqlite3_bind_null(pStmt, i: 6);
194865 }
194866 return rc;
194867}
194868
194869/*
194870** Return the size of the common prefix (if any) shared by zPrev and
194871** zNext, in bytes. For example,
194872**
194873** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
194874** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
194875** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
194876*/
194877static int fts3PrefixCompress(
194878 const char *zPrev, /* Buffer containing previous term */
194879 int nPrev, /* Size of buffer zPrev in bytes */
194880 const char *zNext, /* Buffer containing next term */
194881 int nNext /* Size of buffer zNext in bytes */
194882){
194883 int n;
194884 for(n=0; n<nPrev && n<nNext && zPrev[n]==zNext[n]; n++);
194885 assert_fts3_nc( n<nNext );
194886 return n;
194887}
194888
194889/*
194890** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
194891** (according to memcmp) than the previous term.
194892*/
194893static int fts3NodeAddTerm(
194894 Fts3Table *p, /* Virtual table handle */
194895 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
194896 int isCopyTerm, /* True if zTerm/nTerm is transient */
194897 const char *zTerm, /* Pointer to buffer containing term */
194898 int nTerm /* Size of term in bytes */
194899){
194900 SegmentNode *pTree = *ppTree;
194901 int rc;
194902 SegmentNode *pNew;
194903
194904 /* First try to append the term to the current node. Return early if
194905 ** this is possible.
194906 */
194907 if( pTree ){
194908 int nData = pTree->nData; /* Current size of node in bytes */
194909 int nReq = nData; /* Required space after adding zTerm */
194910 int nPrefix; /* Number of bytes of prefix compression */
194911 int nSuffix; /* Suffix length */
194912
194913 nPrefix = fts3PrefixCompress(zPrev: pTree->zTerm, nPrev: pTree->nTerm, zNext: zTerm, nNext: nTerm);
194914 nSuffix = nTerm-nPrefix;
194915
194916 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
194917 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
194918 ** compared with BINARY collation. This indicates corruption. */
194919 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
194920
194921 nReq += sqlite3Fts3VarintLen(v: nPrefix)+sqlite3Fts3VarintLen(v: nSuffix)+nSuffix;
194922 if( nReq<=p->nNodeSize || !pTree->zTerm ){
194923
194924 if( nReq>p->nNodeSize ){
194925 /* An unusual case: this is the first term to be added to the node
194926 ** and the static node buffer (p->nNodeSize bytes) is not large
194927 ** enough. Use a separately malloced buffer instead This wastes
194928 ** p->nNodeSize bytes, but since this scenario only comes about when
194929 ** the database contain two terms that share a prefix of almost 2KB,
194930 ** this is not expected to be a serious problem.
194931 */
194932 assert( pTree->aData==(char *)&pTree[1] );
194933 pTree->aData = (char *)sqlite3_malloc64(n: nReq);
194934 if( !pTree->aData ){
194935 return SQLITE_NOMEM;
194936 }
194937 }
194938
194939 if( pTree->zTerm ){
194940 /* There is no prefix-length field for first term in a node */
194941 nData += sqlite3Fts3PutVarint(p: &pTree->aData[nData], v: nPrefix);
194942 }
194943
194944 nData += sqlite3Fts3PutVarint(p: &pTree->aData[nData], v: nSuffix);
194945 memcpy(dest: &pTree->aData[nData], src: &zTerm[nPrefix], n: nSuffix);
194946 pTree->nData = nData + nSuffix;
194947 pTree->nEntry++;
194948
194949 if( isCopyTerm ){
194950 if( pTree->nMalloc<nTerm ){
194951 char *zNew = sqlite3_realloc64(pOld: pTree->zMalloc, n: (i64)nTerm*2);
194952 if( !zNew ){
194953 return SQLITE_NOMEM;
194954 }
194955 pTree->nMalloc = nTerm*2;
194956 pTree->zMalloc = zNew;
194957 }
194958 pTree->zTerm = pTree->zMalloc;
194959 memcpy(dest: pTree->zTerm, src: zTerm, n: nTerm);
194960 pTree->nTerm = nTerm;
194961 }else{
194962 pTree->zTerm = (char *)zTerm;
194963 pTree->nTerm = nTerm;
194964 }
194965 return SQLITE_OK;
194966 }
194967 }
194968
194969 /* If control flows to here, it was not possible to append zTerm to the
194970 ** current node. Create a new node (a right-sibling of the current node).
194971 ** If this is the first node in the tree, the term is added to it.
194972 **
194973 ** Otherwise, the term is not added to the new node, it is left empty for
194974 ** now. Instead, the term is inserted into the parent of pTree. If pTree
194975 ** has no parent, one is created here.
194976 */
194977 pNew = (SegmentNode *)sqlite3_malloc64(n: sizeof(SegmentNode) + p->nNodeSize);
194978 if( !pNew ){
194979 return SQLITE_NOMEM;
194980 }
194981 memset(s: pNew, c: 0, n: sizeof(SegmentNode));
194982 pNew->nData = 1 + FTS3_VARINT_MAX;
194983 pNew->aData = (char *)&pNew[1];
194984
194985 if( pTree ){
194986 SegmentNode *pParent = pTree->pParent;
194987 rc = fts3NodeAddTerm(p, ppTree: &pParent, isCopyTerm, zTerm, nTerm);
194988 if( pTree->pParent==0 ){
194989 pTree->pParent = pParent;
194990 }
194991 pTree->pRight = pNew;
194992 pNew->pLeftmost = pTree->pLeftmost;
194993 pNew->pParent = pParent;
194994 pNew->zMalloc = pTree->zMalloc;
194995 pNew->nMalloc = pTree->nMalloc;
194996 pTree->zMalloc = 0;
194997 }else{
194998 pNew->pLeftmost = pNew;
194999 rc = fts3NodeAddTerm(p, ppTree: &pNew, isCopyTerm, zTerm, nTerm);
195000 }
195001
195002 *ppTree = pNew;
195003 return rc;
195004}
195005
195006/*
195007** Helper function for fts3NodeWrite().
195008*/
195009static int fts3TreeFinishNode(
195010 SegmentNode *pTree,
195011 int iHeight,
195012 sqlite3_int64 iLeftChild
195013){
195014 int nStart;
195015 assert( iHeight>=1 && iHeight<128 );
195016 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(v: iLeftChild);
195017 pTree->aData[nStart] = (char)iHeight;
195018 sqlite3Fts3PutVarint(p: &pTree->aData[nStart+1], v: iLeftChild);
195019 return nStart;
195020}
195021
195022/*
195023** Write the buffer for the segment node pTree and all of its peers to the
195024** database. Then call this function recursively to write the parent of
195025** pTree and its peers to the database.
195026**
195027** Except, if pTree is a root node, do not write it to the database. Instead,
195028** set output variables *paRoot and *pnRoot to contain the root node.
195029**
195030** If successful, SQLITE_OK is returned and output variable *piLast is
195031** set to the largest blockid written to the database (or zero if no
195032** blocks were written to the db). Otherwise, an SQLite error code is
195033** returned.
195034*/
195035static int fts3NodeWrite(
195036 Fts3Table *p, /* Virtual table handle */
195037 SegmentNode *pTree, /* SegmentNode handle */
195038 int iHeight, /* Height of this node in tree */
195039 sqlite3_int64 iLeaf, /* Block id of first leaf node */
195040 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
195041 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
195042 char **paRoot, /* OUT: Data for root node */
195043 int *pnRoot /* OUT: Size of root node in bytes */
195044){
195045 int rc = SQLITE_OK;
195046
195047 if( !pTree->pParent ){
195048 /* Root node of the tree. */
195049 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeftChild: iLeaf);
195050 *piLast = iFree-1;
195051 *pnRoot = pTree->nData - nStart;
195052 *paRoot = &pTree->aData[nStart];
195053 }else{
195054 SegmentNode *pIter;
195055 sqlite3_int64 iNextFree = iFree;
195056 sqlite3_int64 iNextLeaf = iLeaf;
195057 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
195058 int nStart = fts3TreeFinishNode(pTree: pIter, iHeight, iLeftChild: iNextLeaf);
195059 int nWrite = pIter->nData - nStart;
195060
195061 rc = fts3WriteSegment(p, iBlock: iNextFree, z: &pIter->aData[nStart], n: nWrite);
195062 iNextFree++;
195063 iNextLeaf += (pIter->nEntry+1);
195064 }
195065 if( rc==SQLITE_OK ){
195066 assert( iNextLeaf==iFree );
195067 rc = fts3NodeWrite(
195068 p, pTree: pTree->pParent, iHeight: iHeight+1, iLeaf: iFree, iFree: iNextFree, piLast, paRoot, pnRoot
195069 );
195070 }
195071 }
195072
195073 return rc;
195074}
195075
195076/*
195077** Free all memory allocations associated with the tree pTree.
195078*/
195079static void fts3NodeFree(SegmentNode *pTree){
195080 if( pTree ){
195081 SegmentNode *p = pTree->pLeftmost;
195082 fts3NodeFree(pTree: p->pParent);
195083 while( p ){
195084 SegmentNode *pRight = p->pRight;
195085 if( p->aData!=(char *)&p[1] ){
195086 sqlite3_free(p: p->aData);
195087 }
195088 assert( pRight==0 || p->zMalloc==0 );
195089 sqlite3_free(p: p->zMalloc);
195090 sqlite3_free(p);
195091 p = pRight;
195092 }
195093 }
195094}
195095
195096/*
195097** Add a term to the segment being constructed by the SegmentWriter object
195098** *ppWriter. When adding the first term to a segment, *ppWriter should
195099** be passed NULL. This function will allocate a new SegmentWriter object
195100** and return it via the input/output variable *ppWriter in this case.
195101**
195102** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
195103*/
195104static int fts3SegWriterAdd(
195105 Fts3Table *p, /* Virtual table handle */
195106 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
195107 int isCopyTerm, /* True if buffer zTerm must be copied */
195108 const char *zTerm, /* Pointer to buffer containing term */
195109 int nTerm, /* Size of term in bytes */
195110 const char *aDoclist, /* Pointer to buffer containing doclist */
195111 int nDoclist /* Size of doclist in bytes */
195112){
195113 int nPrefix; /* Size of term prefix in bytes */
195114 int nSuffix; /* Size of term suffix in bytes */
195115 i64 nReq; /* Number of bytes required on leaf page */
195116 int nData;
195117 SegmentWriter *pWriter = *ppWriter;
195118
195119 if( !pWriter ){
195120 int rc;
195121 sqlite3_stmt *pStmt;
195122
195123 /* Allocate the SegmentWriter structure */
195124 pWriter = (SegmentWriter *)sqlite3_malloc64(n: sizeof(SegmentWriter));
195125 if( !pWriter ) return SQLITE_NOMEM;
195126 memset(s: pWriter, c: 0, n: sizeof(SegmentWriter));
195127 *ppWriter = pWriter;
195128
195129 /* Allocate a buffer in which to accumulate data */
195130 pWriter->aData = (char *)sqlite3_malloc64(n: p->nNodeSize);
195131 if( !pWriter->aData ) return SQLITE_NOMEM;
195132 pWriter->nSize = p->nNodeSize;
195133
195134 /* Find the next free blockid in the %_segments table */
195135 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, pp: &pStmt, apVal: 0);
195136 if( rc!=SQLITE_OK ) return rc;
195137 if( SQLITE_ROW==sqlite3_step(pStmt) ){
195138 pWriter->iFree = sqlite3_column_int64(pStmt, i: 0);
195139 pWriter->iFirst = pWriter->iFree;
195140 }
195141 rc = sqlite3_reset(pStmt);
195142 if( rc!=SQLITE_OK ) return rc;
195143 }
195144 nData = pWriter->nData;
195145
195146 nPrefix = fts3PrefixCompress(zPrev: pWriter->zTerm, nPrev: pWriter->nTerm, zNext: zTerm, nNext: nTerm);
195147 nSuffix = nTerm-nPrefix;
195148
195149 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
195150 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
195151 ** compared with BINARY collation. This indicates corruption. */
195152 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
195153
195154 /* Figure out how many bytes are required by this new entry */
195155 nReq = sqlite3Fts3VarintLen(v: nPrefix) + /* varint containing prefix size */
195156 sqlite3Fts3VarintLen(v: nSuffix) + /* varint containing suffix size */
195157 nSuffix + /* Term suffix */
195158 sqlite3Fts3VarintLen(v: nDoclist) + /* Size of doclist */
195159 nDoclist; /* Doclist data */
195160
195161 if( nData>0 && nData+nReq>p->nNodeSize ){
195162 int rc;
195163
195164 /* The current leaf node is full. Write it out to the database. */
195165 if( pWriter->iFree==LARGEST_INT64 ) return FTS_CORRUPT_VTAB;
195166 rc = fts3WriteSegment(p, iBlock: pWriter->iFree++, z: pWriter->aData, n: nData);
195167 if( rc!=SQLITE_OK ) return rc;
195168 p->nLeafAdd++;
195169
195170 /* Add the current term to the interior node tree. The term added to
195171 ** the interior tree must:
195172 **
195173 ** a) be greater than the largest term on the leaf node just written
195174 ** to the database (still available in pWriter->zTerm), and
195175 **
195176 ** b) be less than or equal to the term about to be added to the new
195177 ** leaf node (zTerm/nTerm).
195178 **
195179 ** In other words, it must be the prefix of zTerm 1 byte longer than
195180 ** the common prefix (if any) of zTerm and pWriter->zTerm.
195181 */
195182 assert( nPrefix<nTerm );
195183 rc = fts3NodeAddTerm(p, ppTree: &pWriter->pTree, isCopyTerm, zTerm, nTerm: nPrefix+1);
195184 if( rc!=SQLITE_OK ) return rc;
195185
195186 nData = 0;
195187 pWriter->nTerm = 0;
195188
195189 nPrefix = 0;
195190 nSuffix = nTerm;
195191 nReq = 1 + /* varint containing prefix size */
195192 sqlite3Fts3VarintLen(v: nTerm) + /* varint containing suffix size */
195193 nTerm + /* Term suffix */
195194 sqlite3Fts3VarintLen(v: nDoclist) + /* Size of doclist */
195195 nDoclist; /* Doclist data */
195196 }
195197
195198 /* Increase the total number of bytes written to account for the new entry. */
195199 pWriter->nLeafData += nReq;
195200
195201 /* If the buffer currently allocated is too small for this entry, realloc
195202 ** the buffer to make it large enough.
195203 */
195204 if( nReq>pWriter->nSize ){
195205 char *aNew = sqlite3_realloc64(pOld: pWriter->aData, n: nReq);
195206 if( !aNew ) return SQLITE_NOMEM;
195207 pWriter->aData = aNew;
195208 pWriter->nSize = nReq;
195209 }
195210 assert( nData+nReq<=pWriter->nSize );
195211
195212 /* Append the prefix-compressed term and doclist to the buffer. */
195213 nData += sqlite3Fts3PutVarint(p: &pWriter->aData[nData], v: nPrefix);
195214 nData += sqlite3Fts3PutVarint(p: &pWriter->aData[nData], v: nSuffix);
195215 assert( nSuffix>0 );
195216 memcpy(dest: &pWriter->aData[nData], src: &zTerm[nPrefix], n: nSuffix);
195217 nData += nSuffix;
195218 nData += sqlite3Fts3PutVarint(p: &pWriter->aData[nData], v: nDoclist);
195219 assert( nDoclist>0 );
195220 memcpy(dest: &pWriter->aData[nData], src: aDoclist, n: nDoclist);
195221 pWriter->nData = nData + nDoclist;
195222
195223 /* Save the current term so that it can be used to prefix-compress the next.
195224 ** If the isCopyTerm parameter is true, then the buffer pointed to by
195225 ** zTerm is transient, so take a copy of the term data. Otherwise, just
195226 ** store a copy of the pointer.
195227 */
195228 if( isCopyTerm ){
195229 if( nTerm>pWriter->nMalloc ){
195230 char *zNew = sqlite3_realloc64(pOld: pWriter->zMalloc, n: (i64)nTerm*2);
195231 if( !zNew ){
195232 return SQLITE_NOMEM;
195233 }
195234 pWriter->nMalloc = nTerm*2;
195235 pWriter->zMalloc = zNew;
195236 pWriter->zTerm = zNew;
195237 }
195238 assert( pWriter->zTerm==pWriter->zMalloc );
195239 assert( nTerm>0 );
195240 memcpy(dest: pWriter->zTerm, src: zTerm, n: nTerm);
195241 }else{
195242 pWriter->zTerm = (char *)zTerm;
195243 }
195244 pWriter->nTerm = nTerm;
195245
195246 return SQLITE_OK;
195247}
195248
195249/*
195250** Flush all data associated with the SegmentWriter object pWriter to the
195251** database. This function must be called after all terms have been added
195252** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
195253** returned. Otherwise, an SQLite error code.
195254*/
195255static int fts3SegWriterFlush(
195256 Fts3Table *p, /* Virtual table handle */
195257 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
195258 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
195259 int iIdx /* Value for 'idx' column of %_segdir */
195260){
195261 int rc; /* Return code */
195262 if( pWriter->pTree ){
195263 sqlite3_int64 iLast = 0; /* Largest block id written to database */
195264 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
195265 char *zRoot = NULL; /* Pointer to buffer containing root node */
195266 int nRoot = 0; /* Size of buffer zRoot */
195267
195268 iLastLeaf = pWriter->iFree;
195269 rc = fts3WriteSegment(p, iBlock: pWriter->iFree++, z: pWriter->aData, n: pWriter->nData);
195270 if( rc==SQLITE_OK ){
195271 rc = fts3NodeWrite(p, pTree: pWriter->pTree, iHeight: 1,
195272 iLeaf: pWriter->iFirst, iFree: pWriter->iFree, piLast: &iLast, paRoot: &zRoot, pnRoot: &nRoot);
195273 }
195274 if( rc==SQLITE_OK ){
195275 rc = fts3WriteSegdir(p, iLevel, iIdx,
195276 iStartBlock: pWriter->iFirst, iLeafEndBlock: iLastLeaf, iEndBlock: iLast, nLeafData: pWriter->nLeafData, zRoot, nRoot);
195277 }
195278 }else{
195279 /* The entire tree fits on the root node. Write it to the segdir table. */
195280 rc = fts3WriteSegdir(p, iLevel, iIdx,
195281 iStartBlock: 0, iLeafEndBlock: 0, iEndBlock: 0, nLeafData: pWriter->nLeafData, zRoot: pWriter->aData, nRoot: pWriter->nData);
195282 }
195283 p->nLeafAdd++;
195284 return rc;
195285}
195286
195287/*
195288** Release all memory held by the SegmentWriter object passed as the
195289** first argument.
195290*/
195291static void fts3SegWriterFree(SegmentWriter *pWriter){
195292 if( pWriter ){
195293 sqlite3_free(p: pWriter->aData);
195294 sqlite3_free(p: pWriter->zMalloc);
195295 fts3NodeFree(pTree: pWriter->pTree);
195296 sqlite3_free(p: pWriter);
195297 }
195298}
195299
195300/*
195301** The first value in the apVal[] array is assumed to contain an integer.
195302** This function tests if there exist any documents with docid values that
195303** are different from that integer. i.e. if deleting the document with docid
195304** pRowid would mean the FTS3 table were empty.
195305**
195306** If successful, *pisEmpty is set to true if the table is empty except for
195307** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
195308** error occurs, an SQLite error code is returned.
195309*/
195310static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
195311 sqlite3_stmt *pStmt;
195312 int rc;
195313 if( p->zContentTbl ){
195314 /* If using the content=xxx option, assume the table is never empty */
195315 *pisEmpty = 0;
195316 rc = SQLITE_OK;
195317 }else{
195318 rc = fts3SqlStmt(p, SQL_IS_EMPTY, pp: &pStmt, apVal: &pRowid);
195319 if( rc==SQLITE_OK ){
195320 if( SQLITE_ROW==sqlite3_step(pStmt) ){
195321 *pisEmpty = sqlite3_column_int(pStmt, i: 0);
195322 }
195323 rc = sqlite3_reset(pStmt);
195324 }
195325 }
195326 return rc;
195327}
195328
195329/*
195330** Set *pnMax to the largest segment level in the database for the index
195331** iIndex.
195332**
195333** Segment levels are stored in the 'level' column of the %_segdir table.
195334**
195335** Return SQLITE_OK if successful, or an SQLite error code if not.
195336*/
195337static int fts3SegmentMaxLevel(
195338 Fts3Table *p,
195339 int iLangid,
195340 int iIndex,
195341 sqlite3_int64 *pnMax
195342){
195343 sqlite3_stmt *pStmt;
195344 int rc;
195345 assert( iIndex>=0 && iIndex<p->nIndex );
195346
195347 /* Set pStmt to the compiled version of:
195348 **
195349 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
195350 **
195351 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
195352 */
195353 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, pp: &pStmt, apVal: 0);
195354 if( rc!=SQLITE_OK ) return rc;
195355 sqlite3_bind_int64(pStmt, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel: 0));
195356 sqlite3_bind_int64(pStmt, i: 2,
195357 iValue: getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
195358 );
195359 if( SQLITE_ROW==sqlite3_step(pStmt) ){
195360 *pnMax = sqlite3_column_int64(pStmt, i: 0);
195361 }
195362 return sqlite3_reset(pStmt);
195363}
195364
195365/*
195366** iAbsLevel is an absolute level that may be assumed to exist within
195367** the database. This function checks if it is the largest level number
195368** within its index. Assuming no error occurs, *pbMax is set to 1 if
195369** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
195370** is returned. If an error occurs, an error code is returned and the
195371** final value of *pbMax is undefined.
195372*/
195373static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
195374
195375 /* Set pStmt to the compiled version of:
195376 **
195377 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
195378 **
195379 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
195380 */
195381 sqlite3_stmt *pStmt;
195382 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, pp: &pStmt, apVal: 0);
195383 if( rc!=SQLITE_OK ) return rc;
195384 sqlite3_bind_int64(pStmt, i: 1, iValue: iAbsLevel+1);
195385 sqlite3_bind_int64(pStmt, i: 2,
195386 iValue: (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
195387 );
195388
195389 *pbMax = 0;
195390 if( SQLITE_ROW==sqlite3_step(pStmt) ){
195391 *pbMax = sqlite3_column_type(pStmt, i: 0)==SQLITE_NULL;
195392 }
195393 return sqlite3_reset(pStmt);
195394}
195395
195396/*
195397** Delete all entries in the %_segments table associated with the segment
195398** opened with seg-reader pSeg. This function does not affect the contents
195399** of the %_segdir table.
195400*/
195401static int fts3DeleteSegment(
195402 Fts3Table *p, /* FTS table handle */
195403 Fts3SegReader *pSeg /* Segment to delete */
195404){
195405 int rc = SQLITE_OK; /* Return code */
195406 if( pSeg->iStartBlock ){
195407 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
195408 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, pp: &pDelete, apVal: 0);
195409 if( rc==SQLITE_OK ){
195410 sqlite3_bind_int64(pStmt: pDelete, i: 1, iValue: pSeg->iStartBlock);
195411 sqlite3_bind_int64(pStmt: pDelete, i: 2, iValue: pSeg->iEndBlock);
195412 sqlite3_step(pStmt: pDelete);
195413 rc = sqlite3_reset(pStmt: pDelete);
195414 }
195415 }
195416 return rc;
195417}
195418
195419/*
195420** This function is used after merging multiple segments into a single large
195421** segment to delete the old, now redundant, segment b-trees. Specifically,
195422** it:
195423**
195424** 1) Deletes all %_segments entries for the segments associated with
195425** each of the SegReader objects in the array passed as the third
195426** argument, and
195427**
195428** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
195429** entries regardless of level if (iLevel<0).
195430**
195431** SQLITE_OK is returned if successful, otherwise an SQLite error code.
195432*/
195433static int fts3DeleteSegdir(
195434 Fts3Table *p, /* Virtual table handle */
195435 int iLangid, /* Language id */
195436 int iIndex, /* Index for p->aIndex */
195437 int iLevel, /* Level of %_segdir entries to delete */
195438 Fts3SegReader **apSegment, /* Array of SegReader objects */
195439 int nReader /* Size of array apSegment */
195440){
195441 int rc = SQLITE_OK; /* Return Code */
195442 int i; /* Iterator variable */
195443 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
195444
195445 for(i=0; rc==SQLITE_OK && i<nReader; i++){
195446 rc = fts3DeleteSegment(p, pSeg: apSegment[i]);
195447 }
195448 if( rc!=SQLITE_OK ){
195449 return rc;
195450 }
195451
195452 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
195453 if( iLevel==FTS3_SEGCURSOR_ALL ){
195454 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, pp: &pDelete, apVal: 0);
195455 if( rc==SQLITE_OK ){
195456 sqlite3_bind_int64(pStmt: pDelete, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel: 0));
195457 sqlite3_bind_int64(pStmt: pDelete, i: 2,
195458 iValue: getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
195459 );
195460 }
195461 }else{
195462 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, pp: &pDelete, apVal: 0);
195463 if( rc==SQLITE_OK ){
195464 sqlite3_bind_int64(
195465 pStmt: pDelete, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel)
195466 );
195467 }
195468 }
195469
195470 if( rc==SQLITE_OK ){
195471 sqlite3_step(pStmt: pDelete);
195472 rc = sqlite3_reset(pStmt: pDelete);
195473 }
195474
195475 return rc;
195476}
195477
195478/*
195479** When this function is called, buffer *ppList (size *pnList bytes) contains
195480** a position list that may (or may not) feature multiple columns. This
195481** function adjusts the pointer *ppList and the length *pnList so that they
195482** identify the subset of the position list that corresponds to column iCol.
195483**
195484** If there are no entries in the input position list for column iCol, then
195485** *pnList is set to zero before returning.
195486**
195487** If parameter bZero is non-zero, then any part of the input list following
195488** the end of the output list is zeroed before returning.
195489*/
195490static void fts3ColumnFilter(
195491 int iCol, /* Column to filter on */
195492 int bZero, /* Zero out anything following *ppList */
195493 char **ppList, /* IN/OUT: Pointer to position list */
195494 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
195495){
195496 char *pList = *ppList;
195497 int nList = *pnList;
195498 char *pEnd = &pList[nList];
195499 int iCurrent = 0;
195500 char *p = pList;
195501
195502 assert( iCol>=0 );
195503 while( 1 ){
195504 char c = 0;
195505 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
195506
195507 if( iCol==iCurrent ){
195508 nList = (int)(p - pList);
195509 break;
195510 }
195511
195512 nList -= (int)(p - pList);
195513 pList = p;
195514 if( nList<=0 ){
195515 break;
195516 }
195517 p = &pList[1];
195518 p += fts3GetVarint32(p, &iCurrent);
195519 }
195520
195521 if( bZero && (pEnd - &pList[nList])>0){
195522 memset(s: &pList[nList], c: 0, n: pEnd - &pList[nList]);
195523 }
195524 *ppList = pList;
195525 *pnList = nList;
195526}
195527
195528/*
195529** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
195530** existing data). Grow the buffer if required.
195531**
195532** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
195533** trying to resize the buffer, return SQLITE_NOMEM.
195534*/
195535static int fts3MsrBufferData(
195536 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
195537 char *pList,
195538 i64 nList
195539){
195540 if( (nList+FTS3_NODE_PADDING)>pMsr->nBuffer ){
195541 char *pNew;
195542 int nNew = nList*2 + FTS3_NODE_PADDING;
195543 pNew = (char *)sqlite3_realloc64(pOld: pMsr->aBuffer, n: nNew);
195544 if( !pNew ) return SQLITE_NOMEM;
195545 pMsr->aBuffer = pNew;
195546 pMsr->nBuffer = nNew;
195547 }
195548
195549 assert( nList>0 );
195550 memcpy(dest: pMsr->aBuffer, src: pList, n: nList);
195551 memset(s: &pMsr->aBuffer[nList], c: 0, FTS3_NODE_PADDING);
195552 return SQLITE_OK;
195553}
195554
195555SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
195556 Fts3Table *p, /* Virtual table handle */
195557 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
195558 sqlite3_int64 *piDocid, /* OUT: Docid value */
195559 char **paPoslist, /* OUT: Pointer to position list */
195560 int *pnPoslist /* OUT: Size of position list in bytes */
195561){
195562 int nMerge = pMsr->nAdvance;
195563 Fts3SegReader **apSegment = pMsr->apSegment;
195564 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
195565 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
195566 );
195567
195568 if( nMerge==0 ){
195569 *paPoslist = 0;
195570 return SQLITE_OK;
195571 }
195572
195573 while( 1 ){
195574 Fts3SegReader *pSeg;
195575 pSeg = pMsr->apSegment[0];
195576
195577 if( pSeg->pOffsetList==0 ){
195578 *paPoslist = 0;
195579 break;
195580 }else{
195581 int rc;
195582 char *pList;
195583 int nList;
195584 int j;
195585 sqlite3_int64 iDocid = apSegment[0]->iDocid;
195586
195587 rc = fts3SegReaderNextDocid(pTab: p, pReader: apSegment[0], ppOffsetList: &pList, pnOffsetList: &nList);
195588 j = 1;
195589 while( rc==SQLITE_OK
195590 && j<nMerge
195591 && apSegment[j]->pOffsetList
195592 && apSegment[j]->iDocid==iDocid
195593 ){
195594 rc = fts3SegReaderNextDocid(pTab: p, pReader: apSegment[j], ppOffsetList: 0, pnOffsetList: 0);
195595 j++;
195596 }
195597 if( rc!=SQLITE_OK ) return rc;
195598 fts3SegReaderSort(apSegment: pMsr->apSegment, nSegment: nMerge, nSuspect: j, xCmp);
195599
195600 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
195601 rc = fts3MsrBufferData(pMsr, pList, nList: (i64)nList+1);
195602 if( rc!=SQLITE_OK ) return rc;
195603 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
195604 pList = pMsr->aBuffer;
195605 }
195606
195607 if( pMsr->iColFilter>=0 ){
195608 fts3ColumnFilter(iCol: pMsr->iColFilter, bZero: 1, ppList: &pList, pnList: &nList);
195609 }
195610
195611 if( nList>0 ){
195612 *paPoslist = pList;
195613 *piDocid = iDocid;
195614 *pnPoslist = nList;
195615 break;
195616 }
195617 }
195618 }
195619
195620 return SQLITE_OK;
195621}
195622
195623static int fts3SegReaderStart(
195624 Fts3Table *p, /* Virtual table handle */
195625 Fts3MultiSegReader *pCsr, /* Cursor object */
195626 const char *zTerm, /* Term searched for (or NULL) */
195627 int nTerm /* Length of zTerm in bytes */
195628){
195629 int i;
195630 int nSeg = pCsr->nSegment;
195631
195632 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
195633 ** for, then advance each segment iterator until it points to a term of
195634 ** equal or greater value than the specified term. This prevents many
195635 ** unnecessary merge/sort operations for the case where single segment
195636 ** b-tree leaf nodes contain more than one term.
195637 */
195638 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
195639 int res = 0;
195640 Fts3SegReader *pSeg = pCsr->apSegment[i];
195641 do {
195642 int rc = fts3SegReaderNext(p, pReader: pSeg, bIncr: 0);
195643 if( rc!=SQLITE_OK ) return rc;
195644 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
195645
195646 if( pSeg->bLookup && res!=0 ){
195647 fts3SegReaderSetEof(pSeg);
195648 }
195649 }
195650 fts3SegReaderSort(apSegment: pCsr->apSegment, nSegment: nSeg, nSuspect: nSeg, xCmp: fts3SegReaderCmp);
195651
195652 return SQLITE_OK;
195653}
195654
195655SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
195656 Fts3Table *p, /* Virtual table handle */
195657 Fts3MultiSegReader *pCsr, /* Cursor object */
195658 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
195659){
195660 pCsr->pFilter = pFilter;
195661 return fts3SegReaderStart(p, pCsr, zTerm: pFilter->zTerm, nTerm: pFilter->nTerm);
195662}
195663
195664SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
195665 Fts3Table *p, /* Virtual table handle */
195666 Fts3MultiSegReader *pCsr, /* Cursor object */
195667 int iCol, /* Column to match on. */
195668 const char *zTerm, /* Term to iterate through a doclist for */
195669 int nTerm /* Number of bytes in zTerm */
195670){
195671 int i;
195672 int rc;
195673 int nSegment = pCsr->nSegment;
195674 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
195675 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
195676 );
195677
195678 assert( pCsr->pFilter==0 );
195679 assert( zTerm && nTerm>0 );
195680
195681 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
195682 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
195683 if( rc!=SQLITE_OK ) return rc;
195684
195685 /* Determine how many of the segments actually point to zTerm/nTerm. */
195686 for(i=0; i<nSegment; i++){
195687 Fts3SegReader *pSeg = pCsr->apSegment[i];
195688 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
195689 break;
195690 }
195691 }
195692 pCsr->nAdvance = i;
195693
195694 /* Advance each of the segments to point to the first docid. */
195695 for(i=0; i<pCsr->nAdvance; i++){
195696 rc = fts3SegReaderFirstDocid(pTab: p, pReader: pCsr->apSegment[i]);
195697 if( rc!=SQLITE_OK ) return rc;
195698 }
195699 fts3SegReaderSort(apSegment: pCsr->apSegment, nSegment: i, nSuspect: i, xCmp);
195700
195701 assert( iCol<0 || iCol<p->nColumn );
195702 pCsr->iColFilter = iCol;
195703
195704 return SQLITE_OK;
195705}
195706
195707/*
195708** This function is called on a MultiSegReader that has been started using
195709** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
195710** have been made. Calling this function puts the MultiSegReader in such
195711** a state that if the next two calls are:
195712**
195713** sqlite3Fts3SegReaderStart()
195714** sqlite3Fts3SegReaderStep()
195715**
195716** then the entire doclist for the term is available in
195717** MultiSegReader.aDoclist/nDoclist.
195718*/
195719SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
195720 int i; /* Used to iterate through segment-readers */
195721
195722 assert( pCsr->zTerm==0 );
195723 assert( pCsr->nTerm==0 );
195724 assert( pCsr->aDoclist==0 );
195725 assert( pCsr->nDoclist==0 );
195726
195727 pCsr->nAdvance = 0;
195728 pCsr->bRestart = 1;
195729 for(i=0; i<pCsr->nSegment; i++){
195730 pCsr->apSegment[i]->pOffsetList = 0;
195731 pCsr->apSegment[i]->nOffsetList = 0;
195732 pCsr->apSegment[i]->iDocid = 0;
195733 }
195734
195735 return SQLITE_OK;
195736}
195737
195738static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, i64 nReq){
195739 if( nReq>pCsr->nBuffer ){
195740 char *aNew;
195741 pCsr->nBuffer = nReq*2;
195742 aNew = sqlite3_realloc64(pOld: pCsr->aBuffer, n: pCsr->nBuffer);
195743 if( !aNew ){
195744 return SQLITE_NOMEM;
195745 }
195746 pCsr->aBuffer = aNew;
195747 }
195748 return SQLITE_OK;
195749}
195750
195751
195752SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
195753 Fts3Table *p, /* Virtual table handle */
195754 Fts3MultiSegReader *pCsr /* Cursor object */
195755){
195756 int rc = SQLITE_OK;
195757
195758 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
195759 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
195760 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
195761 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
195762 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
195763 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
195764
195765 Fts3SegReader **apSegment = pCsr->apSegment;
195766 int nSegment = pCsr->nSegment;
195767 Fts3SegFilter *pFilter = pCsr->pFilter;
195768 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
195769 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
195770 );
195771
195772 if( pCsr->nSegment==0 ) return SQLITE_OK;
195773
195774 do {
195775 int nMerge;
195776 int i;
195777
195778 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
195779 ** forward. Then sort the list in order of current term again.
195780 */
195781 for(i=0; i<pCsr->nAdvance; i++){
195782 Fts3SegReader *pSeg = apSegment[i];
195783 if( pSeg->bLookup ){
195784 fts3SegReaderSetEof(pSeg);
195785 }else{
195786 rc = fts3SegReaderNext(p, pReader: pSeg, bIncr: 0);
195787 }
195788 if( rc!=SQLITE_OK ) return rc;
195789 }
195790 fts3SegReaderSort(apSegment, nSegment, nSuspect: pCsr->nAdvance, xCmp: fts3SegReaderCmp);
195791 pCsr->nAdvance = 0;
195792
195793 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
195794 assert( rc==SQLITE_OK );
195795 if( apSegment[0]->aNode==0 ) break;
195796
195797 pCsr->nTerm = apSegment[0]->nTerm;
195798 pCsr->zTerm = apSegment[0]->zTerm;
195799
195800 /* If this is a prefix-search, and if the term that apSegment[0] points
195801 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
195802 ** required callbacks have been made. In this case exit early.
195803 **
195804 ** Similarly, if this is a search for an exact match, and the first term
195805 ** of segment apSegment[0] is not a match, exit early.
195806 */
195807 if( pFilter->zTerm && !isScan ){
195808 if( pCsr->nTerm<pFilter->nTerm
195809 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
195810 || memcmp(s1: pCsr->zTerm, s2: pFilter->zTerm, n: pFilter->nTerm)
195811 ){
195812 break;
195813 }
195814 }
195815
195816 nMerge = 1;
195817 while( nMerge<nSegment
195818 && apSegment[nMerge]->aNode
195819 && apSegment[nMerge]->nTerm==pCsr->nTerm
195820 && 0==memcmp(s1: pCsr->zTerm, s2: apSegment[nMerge]->zTerm, n: pCsr->nTerm)
195821 ){
195822 nMerge++;
195823 }
195824
195825 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
195826 if( nMerge==1
195827 && !isIgnoreEmpty
195828 && !isFirst
195829 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
195830 ){
195831 pCsr->nDoclist = apSegment[0]->nDoclist;
195832 if( fts3SegReaderIsPending(apSegment[0]) ){
195833 rc = fts3MsrBufferData(pMsr: pCsr, pList: apSegment[0]->aDoclist,
195834 nList: (i64)pCsr->nDoclist);
195835 pCsr->aDoclist = pCsr->aBuffer;
195836 }else{
195837 pCsr->aDoclist = apSegment[0]->aDoclist;
195838 }
195839 if( rc==SQLITE_OK ) rc = SQLITE_ROW;
195840 }else{
195841 int nDoclist = 0; /* Size of doclist */
195842 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
195843
195844 /* The current term of the first nMerge entries in the array
195845 ** of Fts3SegReader objects is the same. The doclists must be merged
195846 ** and a single term returned with the merged doclist.
195847 */
195848 for(i=0; i<nMerge; i++){
195849 fts3SegReaderFirstDocid(pTab: p, pReader: apSegment[i]);
195850 }
195851 fts3SegReaderSort(apSegment, nSegment: nMerge, nSuspect: nMerge, xCmp);
195852 while( apSegment[0]->pOffsetList ){
195853 int j; /* Number of segments that share a docid */
195854 char *pList = 0;
195855 int nList = 0;
195856 int nByte;
195857 sqlite3_int64 iDocid = apSegment[0]->iDocid;
195858 fts3SegReaderNextDocid(pTab: p, pReader: apSegment[0], ppOffsetList: &pList, pnOffsetList: &nList);
195859 j = 1;
195860 while( j<nMerge
195861 && apSegment[j]->pOffsetList
195862 && apSegment[j]->iDocid==iDocid
195863 ){
195864 fts3SegReaderNextDocid(pTab: p, pReader: apSegment[j], ppOffsetList: 0, pnOffsetList: 0);
195865 j++;
195866 }
195867
195868 if( isColFilter ){
195869 fts3ColumnFilter(iCol: pFilter->iCol, bZero: 0, ppList: &pList, pnList: &nList);
195870 }
195871
195872 if( !isIgnoreEmpty || nList>0 ){
195873
195874 /* Calculate the 'docid' delta value to write into the merged
195875 ** doclist. */
195876 sqlite3_int64 iDelta;
195877 if( p->bDescIdx && nDoclist>0 ){
195878 if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB;
195879 iDelta = (i64)((u64)iPrev - (u64)iDocid);
195880 }else{
195881 if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB;
195882 iDelta = (i64)((u64)iDocid - (u64)iPrev);
195883 }
195884
195885 nByte = sqlite3Fts3VarintLen(v: iDelta) + (isRequirePos?nList+1:0);
195886
195887 rc = fts3GrowSegReaderBuffer(pCsr,
195888 nReq: (i64)nByte+nDoclist+FTS3_NODE_PADDING);
195889 if( rc ) return rc;
195890
195891 if( isFirst ){
195892 char *a = &pCsr->aBuffer[nDoclist];
195893 int nWrite;
195894
195895 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, pOut: a);
195896 if( nWrite ){
195897 iPrev = iDocid;
195898 nDoclist += nWrite;
195899 }
195900 }else{
195901 nDoclist += sqlite3Fts3PutVarint(p: &pCsr->aBuffer[nDoclist], v: iDelta);
195902 iPrev = iDocid;
195903 if( isRequirePos ){
195904 memcpy(dest: &pCsr->aBuffer[nDoclist], src: pList, n: nList);
195905 nDoclist += nList;
195906 pCsr->aBuffer[nDoclist++] = '\0';
195907 }
195908 }
195909 }
195910
195911 fts3SegReaderSort(apSegment, nSegment: nMerge, nSuspect: j, xCmp);
195912 }
195913 if( nDoclist>0 ){
195914 rc = fts3GrowSegReaderBuffer(pCsr, nReq: (i64)nDoclist+FTS3_NODE_PADDING);
195915 if( rc ) return rc;
195916 memset(s: &pCsr->aBuffer[nDoclist], c: 0, FTS3_NODE_PADDING);
195917 pCsr->aDoclist = pCsr->aBuffer;
195918 pCsr->nDoclist = nDoclist;
195919 rc = SQLITE_ROW;
195920 }
195921 }
195922 pCsr->nAdvance = nMerge;
195923 }while( rc==SQLITE_OK );
195924
195925 return rc;
195926}
195927
195928
195929SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
195930 Fts3MultiSegReader *pCsr /* Cursor object */
195931){
195932 if( pCsr ){
195933 int i;
195934 for(i=0; i<pCsr->nSegment; i++){
195935 sqlite3Fts3SegReaderFree(pReader: pCsr->apSegment[i]);
195936 }
195937 sqlite3_free(p: pCsr->apSegment);
195938 sqlite3_free(p: pCsr->aBuffer);
195939
195940 pCsr->nSegment = 0;
195941 pCsr->apSegment = 0;
195942 pCsr->aBuffer = 0;
195943 }
195944}
195945
195946/*
195947** Decode the "end_block" field, selected by column iCol of the SELECT
195948** statement passed as the first argument.
195949**
195950** The "end_block" field may contain either an integer, or a text field
195951** containing the text representation of two non-negative integers separated
195952** by one or more space (0x20) characters. In the first case, set *piEndBlock
195953** to the integer value and *pnByte to zero before returning. In the second,
195954** set *piEndBlock to the first value and *pnByte to the second.
195955*/
195956static void fts3ReadEndBlockField(
195957 sqlite3_stmt *pStmt,
195958 int iCol,
195959 i64 *piEndBlock,
195960 i64 *pnByte
195961){
195962 const unsigned char *zText = sqlite3_column_text(pStmt, i: iCol);
195963 if( zText ){
195964 int i;
195965 int iMul = 1;
195966 u64 iVal = 0;
195967 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
195968 iVal = iVal*10 + (zText[i] - '0');
195969 }
195970 *piEndBlock = (i64)iVal;
195971 while( zText[i]==' ' ) i++;
195972 iVal = 0;
195973 if( zText[i]=='-' ){
195974 i++;
195975 iMul = -1;
195976 }
195977 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
195978 iVal = iVal*10 + (zText[i] - '0');
195979 }
195980 *pnByte = ((i64)iVal * (i64)iMul);
195981 }
195982}
195983
195984
195985/*
195986** A segment of size nByte bytes has just been written to absolute level
195987** iAbsLevel. Promote any segments that should be promoted as a result.
195988*/
195989static int fts3PromoteSegments(
195990 Fts3Table *p, /* FTS table handle */
195991 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
195992 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
195993){
195994 int rc = SQLITE_OK;
195995 sqlite3_stmt *pRange;
195996
195997 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, pp: &pRange, apVal: 0);
195998
195999 if( rc==SQLITE_OK ){
196000 int bOk = 0;
196001 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
196002 i64 nLimit = (nByte*3)/2;
196003
196004 /* Loop through all entries in the %_segdir table corresponding to
196005 ** segments in this index on levels greater than iAbsLevel. If there is
196006 ** at least one such segment, and it is possible to determine that all
196007 ** such segments are smaller than nLimit bytes in size, they will be
196008 ** promoted to level iAbsLevel. */
196009 sqlite3_bind_int64(pStmt: pRange, i: 1, iValue: iAbsLevel+1);
196010 sqlite3_bind_int64(pStmt: pRange, i: 2, iValue: iLast);
196011 while( SQLITE_ROW==sqlite3_step(pStmt: pRange) ){
196012 i64 nSize = 0, dummy;
196013 fts3ReadEndBlockField(pStmt: pRange, iCol: 2, piEndBlock: &dummy, pnByte: &nSize);
196014 if( nSize<=0 || nSize>nLimit ){
196015 /* If nSize==0, then the %_segdir.end_block field does not not
196016 ** contain a size value. This happens if it was written by an
196017 ** old version of FTS. In this case it is not possible to determine
196018 ** the size of the segment, and so segment promotion does not
196019 ** take place. */
196020 bOk = 0;
196021 break;
196022 }
196023 bOk = 1;
196024 }
196025 rc = sqlite3_reset(pStmt: pRange);
196026
196027 if( bOk ){
196028 int iIdx = 0;
196029 sqlite3_stmt *pUpdate1 = 0;
196030 sqlite3_stmt *pUpdate2 = 0;
196031
196032 if( rc==SQLITE_OK ){
196033 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, pp: &pUpdate1, apVal: 0);
196034 }
196035 if( rc==SQLITE_OK ){
196036 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, pp: &pUpdate2, apVal: 0);
196037 }
196038
196039 if( rc==SQLITE_OK ){
196040
196041 /* Loop through all %_segdir entries for segments in this index with
196042 ** levels equal to or greater than iAbsLevel. As each entry is visited,
196043 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
196044 ** oldest segment in the range, 1 for the next oldest, and so on.
196045 **
196046 ** In other words, move all segments being promoted to level -1,
196047 ** setting the "idx" fields as appropriate to keep them in the same
196048 ** order. The contents of level -1 (which is never used, except
196049 ** transiently here), will be moved back to level iAbsLevel below. */
196050 sqlite3_bind_int64(pStmt: pRange, i: 1, iValue: iAbsLevel);
196051 while( SQLITE_ROW==sqlite3_step(pStmt: pRange) ){
196052 sqlite3_bind_int(p: pUpdate1, i: 1, iValue: iIdx++);
196053 sqlite3_bind_int(p: pUpdate1, i: 2, iValue: sqlite3_column_int(pStmt: pRange, i: 0));
196054 sqlite3_bind_int(p: pUpdate1, i: 3, iValue: sqlite3_column_int(pStmt: pRange, i: 1));
196055 sqlite3_step(pStmt: pUpdate1);
196056 rc = sqlite3_reset(pStmt: pUpdate1);
196057 if( rc!=SQLITE_OK ){
196058 sqlite3_reset(pStmt: pRange);
196059 break;
196060 }
196061 }
196062 }
196063 if( rc==SQLITE_OK ){
196064 rc = sqlite3_reset(pStmt: pRange);
196065 }
196066
196067 /* Move level -1 to level iAbsLevel */
196068 if( rc==SQLITE_OK ){
196069 sqlite3_bind_int64(pStmt: pUpdate2, i: 1, iValue: iAbsLevel);
196070 sqlite3_step(pStmt: pUpdate2);
196071 rc = sqlite3_reset(pStmt: pUpdate2);
196072 }
196073 }
196074 }
196075
196076
196077 return rc;
196078}
196079
196080/*
196081** Merge all level iLevel segments in the database into a single
196082** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
196083** single segment with a level equal to the numerically largest level
196084** currently present in the database.
196085**
196086** If this function is called with iLevel<0, but there is only one
196087** segment in the database, SQLITE_DONE is returned immediately.
196088** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
196089** an SQLite error code is returned.
196090*/
196091static int fts3SegmentMerge(
196092 Fts3Table *p,
196093 int iLangid, /* Language id to merge */
196094 int iIndex, /* Index in p->aIndex[] to merge */
196095 int iLevel /* Level to merge */
196096){
196097 int rc; /* Return code */
196098 int iIdx = 0; /* Index of new segment */
196099 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
196100 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
196101 Fts3SegFilter filter; /* Segment term filter condition */
196102 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
196103 int bIgnoreEmpty = 0; /* True to ignore empty segments */
196104 i64 iMaxLevel = 0; /* Max level number for this index/langid */
196105
196106 assert( iLevel==FTS3_SEGCURSOR_ALL
196107 || iLevel==FTS3_SEGCURSOR_PENDING
196108 || iLevel>=0
196109 );
196110 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
196111 assert( iIndex>=0 && iIndex<p->nIndex );
196112
196113 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, zTerm: 0, nTerm: 0, isPrefix: 1, isScan: 0, pCsr: &csr);
196114 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
196115
196116 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
196117 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, pnMax: &iMaxLevel);
196118 if( rc!=SQLITE_OK ) goto finished;
196119 }
196120
196121 if( iLevel==FTS3_SEGCURSOR_ALL ){
196122 /* This call is to merge all segments in the database to a single
196123 ** segment. The level of the new segment is equal to the numerically
196124 ** greatest segment level currently present in the database for this
196125 ** index. The idx of the new segment is always 0. */
196126 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
196127 rc = SQLITE_DONE;
196128 goto finished;
196129 }
196130 iNewLevel = iMaxLevel;
196131 bIgnoreEmpty = 1;
196132
196133 }else{
196134 /* This call is to merge all segments at level iLevel. find the next
196135 ** available segment index at level iLevel+1. The call to
196136 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
196137 ** a single iLevel+2 segment if necessary. */
196138 assert( FTS3_SEGCURSOR_PENDING==-1 );
196139 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel: iLevel+1);
196140 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel: iLevel+1, piIdx: &iIdx);
196141 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
196142 }
196143 if( rc!=SQLITE_OK ) goto finished;
196144
196145 assert( csr.nSegment>0 );
196146 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
196147 assert_fts3_nc(
196148 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)
196149 );
196150
196151 memset(s: &filter, c: 0, n: sizeof(Fts3SegFilter));
196152 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
196153 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
196154
196155 rc = sqlite3Fts3SegReaderStart(p, pCsr: &csr, pFilter: &filter);
196156 while( SQLITE_OK==rc ){
196157 rc = sqlite3Fts3SegReaderStep(p, pCsr: &csr);
196158 if( rc!=SQLITE_ROW ) break;
196159 rc = fts3SegWriterAdd(p, ppWriter: &pWriter, isCopyTerm: 1,
196160 zTerm: csr.zTerm, nTerm: csr.nTerm, aDoclist: csr.aDoclist, nDoclist: csr.nDoclist);
196161 }
196162 if( rc!=SQLITE_OK ) goto finished;
196163 assert_fts3_nc( pWriter || bIgnoreEmpty );
196164
196165 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
196166 rc = fts3DeleteSegdir(
196167 p, iLangid, iIndex, iLevel, apSegment: csr.apSegment, nReader: csr.nSegment
196168 );
196169 if( rc!=SQLITE_OK ) goto finished;
196170 }
196171 if( pWriter ){
196172 rc = fts3SegWriterFlush(p, pWriter, iLevel: iNewLevel, iIdx);
196173 if( rc==SQLITE_OK ){
196174 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
196175 rc = fts3PromoteSegments(p, iAbsLevel: iNewLevel, nByte: pWriter->nLeafData);
196176 }
196177 }
196178 }
196179
196180 finished:
196181 fts3SegWriterFree(pWriter);
196182 sqlite3Fts3SegReaderFinish(pCsr: &csr);
196183 return rc;
196184}
196185
196186
196187/*
196188** Flush the contents of pendingTerms to level 0 segments.
196189*/
196190SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
196191 int rc = SQLITE_OK;
196192 int i;
196193
196194 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
196195 rc = fts3SegmentMerge(p, iLangid: p->iPrevLangid, iIndex: i, FTS3_SEGCURSOR_PENDING);
196196 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
196197 }
196198 sqlite3Fts3PendingTermsClear(p);
196199
196200 /* Determine the auto-incr-merge setting if unknown. If enabled,
196201 ** estimate the number of leaf blocks of content to be written
196202 */
196203 if( rc==SQLITE_OK && p->bHasStat
196204 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
196205 ){
196206 sqlite3_stmt *pStmt = 0;
196207 rc = fts3SqlStmt(p, SQL_SELECT_STAT, pp: &pStmt, apVal: 0);
196208 if( rc==SQLITE_OK ){
196209 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_AUTOINCRMERGE);
196210 rc = sqlite3_step(pStmt);
196211 if( rc==SQLITE_ROW ){
196212 p->nAutoincrmerge = sqlite3_column_int(pStmt, i: 0);
196213 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
196214 }else if( rc==SQLITE_DONE ){
196215 p->nAutoincrmerge = 0;
196216 }
196217 rc = sqlite3_reset(pStmt);
196218 }
196219 }
196220 return rc;
196221}
196222
196223/*
196224** Encode N integers as varints into a blob.
196225*/
196226static void fts3EncodeIntArray(
196227 int N, /* The number of integers to encode */
196228 u32 *a, /* The integer values */
196229 char *zBuf, /* Write the BLOB here */
196230 int *pNBuf /* Write number of bytes if zBuf[] used here */
196231){
196232 int i, j;
196233 for(i=j=0; i<N; i++){
196234 j += sqlite3Fts3PutVarint(p: &zBuf[j], v: (sqlite3_int64)a[i]);
196235 }
196236 *pNBuf = j;
196237}
196238
196239/*
196240** Decode a blob of varints into N integers
196241*/
196242static void fts3DecodeIntArray(
196243 int N, /* The number of integers to decode */
196244 u32 *a, /* Write the integer values */
196245 const char *zBuf, /* The BLOB containing the varints */
196246 int nBuf /* size of the BLOB */
196247){
196248 int i = 0;
196249 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
196250 int j;
196251 for(i=j=0; i<N && j<nBuf; i++){
196252 sqlite3_int64 x;
196253 j += sqlite3Fts3GetVarint(pBuf: &zBuf[j], v: &x);
196254 a[i] = (u32)(x & 0xffffffff);
196255 }
196256 }
196257 while( i<N ) a[i++] = 0;
196258}
196259
196260/*
196261** Insert the sizes (in tokens) for each column of the document
196262** with docid equal to p->iPrevDocid. The sizes are encoded as
196263** a blob of varints.
196264*/
196265static void fts3InsertDocsize(
196266 int *pRC, /* Result code */
196267 Fts3Table *p, /* Table into which to insert */
196268 u32 *aSz /* Sizes of each column, in tokens */
196269){
196270 char *pBlob; /* The BLOB encoding of the document size */
196271 int nBlob; /* Number of bytes in the BLOB */
196272 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
196273 int rc; /* Result code from subfunctions */
196274
196275 if( *pRC ) return;
196276 pBlob = sqlite3_malloc64( n: 10*(sqlite3_int64)p->nColumn );
196277 if( pBlob==0 ){
196278 *pRC = SQLITE_NOMEM;
196279 return;
196280 }
196281 fts3EncodeIntArray(N: p->nColumn, a: aSz, zBuf: pBlob, pNBuf: &nBlob);
196282 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, pp: &pStmt, apVal: 0);
196283 if( rc ){
196284 sqlite3_free(p: pBlob);
196285 *pRC = rc;
196286 return;
196287 }
196288 sqlite3_bind_int64(pStmt, i: 1, iValue: p->iPrevDocid);
196289 sqlite3_bind_blob(pStmt, i: 2, zData: pBlob, nData: nBlob, xDel: sqlite3_free);
196290 sqlite3_step(pStmt);
196291 *pRC = sqlite3_reset(pStmt);
196292}
196293
196294/*
196295** Record 0 of the %_stat table contains a blob consisting of N varints,
196296** where N is the number of user defined columns in the fts3 table plus
196297** two. If nCol is the number of user defined columns, then values of the
196298** varints are set as follows:
196299**
196300** Varint 0: Total number of rows in the table.
196301**
196302** Varint 1..nCol: For each column, the total number of tokens stored in
196303** the column for all rows of the table.
196304**
196305** Varint 1+nCol: The total size, in bytes, of all text values in all
196306** columns of all rows of the table.
196307**
196308*/
196309static void fts3UpdateDocTotals(
196310 int *pRC, /* The result code */
196311 Fts3Table *p, /* Table being updated */
196312 u32 *aSzIns, /* Size increases */
196313 u32 *aSzDel, /* Size decreases */
196314 int nChng /* Change in the number of documents */
196315){
196316 char *pBlob; /* Storage for BLOB written into %_stat */
196317 int nBlob; /* Size of BLOB written into %_stat */
196318 u32 *a; /* Array of integers that becomes the BLOB */
196319 sqlite3_stmt *pStmt; /* Statement for reading and writing */
196320 int i; /* Loop counter */
196321 int rc; /* Result code from subfunctions */
196322
196323 const int nStat = p->nColumn+2;
196324
196325 if( *pRC ) return;
196326 a = sqlite3_malloc64( n: (sizeof(u32)+10)*(sqlite3_int64)nStat );
196327 if( a==0 ){
196328 *pRC = SQLITE_NOMEM;
196329 return;
196330 }
196331 pBlob = (char*)&a[nStat];
196332 rc = fts3SqlStmt(p, SQL_SELECT_STAT, pp: &pStmt, apVal: 0);
196333 if( rc ){
196334 sqlite3_free(p: a);
196335 *pRC = rc;
196336 return;
196337 }
196338 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_DOCTOTAL);
196339 if( sqlite3_step(pStmt)==SQLITE_ROW ){
196340 fts3DecodeIntArray(N: nStat, a,
196341 zBuf: sqlite3_column_blob(pStmt, i: 0),
196342 nBuf: sqlite3_column_bytes(pStmt, i: 0));
196343 }else{
196344 memset(s: a, c: 0, n: sizeof(u32)*(nStat) );
196345 }
196346 rc = sqlite3_reset(pStmt);
196347 if( rc!=SQLITE_OK ){
196348 sqlite3_free(p: a);
196349 *pRC = rc;
196350 return;
196351 }
196352 if( nChng<0 && a[0]<(u32)(-nChng) ){
196353 a[0] = 0;
196354 }else{
196355 a[0] += nChng;
196356 }
196357 for(i=0; i<p->nColumn+1; i++){
196358 u32 x = a[i+1];
196359 if( x+aSzIns[i] < aSzDel[i] ){
196360 x = 0;
196361 }else{
196362 x = x + aSzIns[i] - aSzDel[i];
196363 }
196364 a[i+1] = x;
196365 }
196366 fts3EncodeIntArray(N: nStat, a, zBuf: pBlob, pNBuf: &nBlob);
196367 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, pp: &pStmt, apVal: 0);
196368 if( rc ){
196369 sqlite3_free(p: a);
196370 *pRC = rc;
196371 return;
196372 }
196373 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_DOCTOTAL);
196374 sqlite3_bind_blob(pStmt, i: 2, zData: pBlob, nData: nBlob, SQLITE_STATIC);
196375 sqlite3_step(pStmt);
196376 *pRC = sqlite3_reset(pStmt);
196377 sqlite3_bind_null(pStmt, i: 2);
196378 sqlite3_free(p: a);
196379}
196380
196381/*
196382** Merge the entire database so that there is one segment for each
196383** iIndex/iLangid combination.
196384*/
196385static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
196386 int bSeenDone = 0;
196387 int rc;
196388 sqlite3_stmt *pAllLangid = 0;
196389
196390 rc = sqlite3Fts3PendingTermsFlush(p);
196391 if( rc==SQLITE_OK ){
196392 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, pp: &pAllLangid, apVal: 0);
196393 }
196394 if( rc==SQLITE_OK ){
196395 int rc2;
196396 sqlite3_bind_int(p: pAllLangid, i: 1, iValue: p->iPrevLangid);
196397 sqlite3_bind_int(p: pAllLangid, i: 2, iValue: p->nIndex);
196398 while( sqlite3_step(pStmt: pAllLangid)==SQLITE_ROW ){
196399 int i;
196400 int iLangid = sqlite3_column_int(pStmt: pAllLangid, i: 0);
196401 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
196402 rc = fts3SegmentMerge(p, iLangid, iIndex: i, FTS3_SEGCURSOR_ALL);
196403 if( rc==SQLITE_DONE ){
196404 bSeenDone = 1;
196405 rc = SQLITE_OK;
196406 }
196407 }
196408 }
196409 rc2 = sqlite3_reset(pStmt: pAllLangid);
196410 if( rc==SQLITE_OK ) rc = rc2;
196411 }
196412
196413 sqlite3Fts3SegmentsClose(p);
196414
196415 return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
196416}
196417
196418/*
196419** This function is called when the user executes the following statement:
196420**
196421** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
196422**
196423** The entire FTS index is discarded and rebuilt. If the table is one
196424** created using the content=xxx option, then the new index is based on
196425** the current contents of the xxx table. Otherwise, it is rebuilt based
196426** on the contents of the %_content table.
196427*/
196428static int fts3DoRebuild(Fts3Table *p){
196429 int rc; /* Return Code */
196430
196431 rc = fts3DeleteAll(p, bContent: 0);
196432 if( rc==SQLITE_OK ){
196433 u32 *aSz = 0;
196434 u32 *aSzIns = 0;
196435 u32 *aSzDel = 0;
196436 sqlite3_stmt *pStmt = 0;
196437 int nEntry = 0;
196438
196439 /* Compose and prepare an SQL statement to loop through the content table */
196440 char *zSql = sqlite3_mprintf(zFormat: "SELECT %s" , p->zReadExprlist);
196441 if( !zSql ){
196442 rc = SQLITE_NOMEM;
196443 }else{
196444 rc = sqlite3_prepare_v2(db: p->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
196445 sqlite3_free(p: zSql);
196446 }
196447
196448 if( rc==SQLITE_OK ){
196449 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
196450 aSz = (u32 *)sqlite3_malloc64(n: nByte);
196451 if( aSz==0 ){
196452 rc = SQLITE_NOMEM;
196453 }else{
196454 memset(s: aSz, c: 0, n: nByte);
196455 aSzIns = &aSz[p->nColumn+1];
196456 aSzDel = &aSzIns[p->nColumn+1];
196457 }
196458 }
196459
196460 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
196461 int iCol;
196462 int iLangid = langidFromSelect(p, pSelect: pStmt);
196463 rc = fts3PendingTermsDocid(p, bDelete: 0, iLangid, iDocid: sqlite3_column_int64(pStmt, i: 0));
196464 memset(s: aSz, c: 0, n: sizeof(aSz[0]) * (p->nColumn+1));
196465 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
196466 if( p->abNotindexed[iCol]==0 ){
196467 const char *z = (const char *) sqlite3_column_text(pStmt, i: iCol+1);
196468 rc = fts3PendingTermsAdd(p, iLangid, zText: z, iCol, pnWord: &aSz[iCol]);
196469 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, i: iCol+1);
196470 }
196471 }
196472 if( p->bHasDocsize ){
196473 fts3InsertDocsize(pRC: &rc, p, aSz);
196474 }
196475 if( rc!=SQLITE_OK ){
196476 sqlite3_finalize(pStmt);
196477 pStmt = 0;
196478 }else{
196479 nEntry++;
196480 for(iCol=0; iCol<=p->nColumn; iCol++){
196481 aSzIns[iCol] += aSz[iCol];
196482 }
196483 }
196484 }
196485 if( p->bFts4 ){
196486 fts3UpdateDocTotals(pRC: &rc, p, aSzIns, aSzDel, nChng: nEntry);
196487 }
196488 sqlite3_free(p: aSz);
196489
196490 if( pStmt ){
196491 int rc2 = sqlite3_finalize(pStmt);
196492 if( rc==SQLITE_OK ){
196493 rc = rc2;
196494 }
196495 }
196496 }
196497
196498 return rc;
196499}
196500
196501
196502/*
196503** This function opens a cursor used to read the input data for an
196504** incremental merge operation. Specifically, it opens a cursor to scan
196505** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
196506** level iAbsLevel.
196507*/
196508static int fts3IncrmergeCsr(
196509 Fts3Table *p, /* FTS3 table handle */
196510 sqlite3_int64 iAbsLevel, /* Absolute level to open */
196511 int nSeg, /* Number of segments to merge */
196512 Fts3MultiSegReader *pCsr /* Cursor object to populate */
196513){
196514 int rc; /* Return Code */
196515 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
196516 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
196517
196518 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
196519 memset(s: pCsr, c: 0, n: sizeof(*pCsr));
196520 nByte = sizeof(Fts3SegReader *) * nSeg;
196521 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(n: nByte);
196522
196523 if( pCsr->apSegment==0 ){
196524 rc = SQLITE_NOMEM;
196525 }else{
196526 memset(s: pCsr->apSegment, c: 0, n: nByte);
196527 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, pp: &pStmt, apVal: 0);
196528 }
196529 if( rc==SQLITE_OK ){
196530 int i;
196531 int rc2;
196532 sqlite3_bind_int64(pStmt, i: 1, iValue: iAbsLevel);
196533 assert( pCsr->nSegment==0 );
196534 for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
196535 rc = sqlite3Fts3SegReaderNew(iAge: i, bLookup: 0,
196536 iStartLeaf: sqlite3_column_int64(pStmt, i: 1), /* segdir.start_block */
196537 iEndLeaf: sqlite3_column_int64(pStmt, i: 2), /* segdir.leaves_end_block */
196538 iEndBlock: sqlite3_column_int64(pStmt, i: 3), /* segdir.end_block */
196539 zRoot: sqlite3_column_blob(pStmt, i: 4), /* segdir.root */
196540 nRoot: sqlite3_column_bytes(pStmt, i: 4), /* segdir.root */
196541 ppReader: &pCsr->apSegment[i]
196542 );
196543 pCsr->nSegment++;
196544 }
196545 rc2 = sqlite3_reset(pStmt);
196546 if( rc==SQLITE_OK ) rc = rc2;
196547 }
196548
196549 return rc;
196550}
196551
196552typedef struct IncrmergeWriter IncrmergeWriter;
196553typedef struct NodeWriter NodeWriter;
196554typedef struct Blob Blob;
196555typedef struct NodeReader NodeReader;
196556
196557/*
196558** An instance of the following structure is used as a dynamic buffer
196559** to build up nodes or other blobs of data in.
196560**
196561** The function blobGrowBuffer() is used to extend the allocation.
196562*/
196563struct Blob {
196564 char *a; /* Pointer to allocation */
196565 int n; /* Number of valid bytes of data in a[] */
196566 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
196567};
196568
196569/*
196570** This structure is used to build up buffers containing segment b-tree
196571** nodes (blocks).
196572*/
196573struct NodeWriter {
196574 sqlite3_int64 iBlock; /* Current block id */
196575 Blob key; /* Last key written to the current block */
196576 Blob block; /* Current block image */
196577};
196578
196579/*
196580** An object of this type contains the state required to create or append
196581** to an appendable b-tree segment.
196582*/
196583struct IncrmergeWriter {
196584 int nLeafEst; /* Space allocated for leaf blocks */
196585 int nWork; /* Number of leaf pages flushed */
196586 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
196587 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
196588 sqlite3_int64 iStart; /* Block number of first allocated block */
196589 sqlite3_int64 iEnd; /* Block number of last allocated block */
196590 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
196591 u8 bNoLeafData; /* If true, store 0 for segment size */
196592 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
196593};
196594
196595/*
196596** An object of the following type is used to read data from a single
196597** FTS segment node. See the following functions:
196598**
196599** nodeReaderInit()
196600** nodeReaderNext()
196601** nodeReaderRelease()
196602*/
196603struct NodeReader {
196604 const char *aNode;
196605 int nNode;
196606 int iOff; /* Current offset within aNode[] */
196607
196608 /* Output variables. Containing the current node entry. */
196609 sqlite3_int64 iChild; /* Pointer to child node */
196610 Blob term; /* Current term */
196611 const char *aDoclist; /* Pointer to doclist */
196612 int nDoclist; /* Size of doclist in bytes */
196613};
196614
196615/*
196616** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
196617** Otherwise, if the allocation at pBlob->a is not already at least nMin
196618** bytes in size, extend (realloc) it to be so.
196619**
196620** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
196621** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
196622** to reflect the new size of the pBlob->a[] buffer.
196623*/
196624static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
196625 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
196626 int nAlloc = nMin;
196627 char *a = (char *)sqlite3_realloc64(pOld: pBlob->a, n: nAlloc);
196628 if( a ){
196629 pBlob->nAlloc = nAlloc;
196630 pBlob->a = a;
196631 }else{
196632 *pRc = SQLITE_NOMEM;
196633 }
196634 }
196635}
196636
196637/*
196638** Attempt to advance the node-reader object passed as the first argument to
196639** the next entry on the node.
196640**
196641** Return an error code if an error occurs (SQLITE_NOMEM is possible).
196642** Otherwise return SQLITE_OK. If there is no next entry on the node
196643** (e.g. because the current entry is the last) set NodeReader->aNode to
196644** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
196645** variables for the new entry.
196646*/
196647static int nodeReaderNext(NodeReader *p){
196648 int bFirst = (p->term.n==0); /* True for first term on the node */
196649 int nPrefix = 0; /* Bytes to copy from previous term */
196650 int nSuffix = 0; /* Bytes to append to the prefix */
196651 int rc = SQLITE_OK; /* Return code */
196652
196653 assert( p->aNode );
196654 if( p->iChild && bFirst==0 ) p->iChild++;
196655 if( p->iOff>=p->nNode ){
196656 /* EOF */
196657 p->aNode = 0;
196658 }else{
196659 if( bFirst==0 ){
196660 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
196661 }
196662 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
196663
196664 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
196665 return FTS_CORRUPT_VTAB;
196666 }
196667 blobGrowBuffer(pBlob: &p->term, nMin: nPrefix+nSuffix, pRc: &rc);
196668 if( rc==SQLITE_OK && ALWAYS(p->term.a!=0) ){
196669 memcpy(dest: &p->term.a[nPrefix], src: &p->aNode[p->iOff], n: nSuffix);
196670 p->term.n = nPrefix+nSuffix;
196671 p->iOff += nSuffix;
196672 if( p->iChild==0 ){
196673 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
196674 if( (p->nNode-p->iOff)<p->nDoclist ){
196675 return FTS_CORRUPT_VTAB;
196676 }
196677 p->aDoclist = &p->aNode[p->iOff];
196678 p->iOff += p->nDoclist;
196679 }
196680 }
196681 }
196682
196683 assert_fts3_nc( p->iOff<=p->nNode );
196684 return rc;
196685}
196686
196687/*
196688** Release all dynamic resources held by node-reader object *p.
196689*/
196690static void nodeReaderRelease(NodeReader *p){
196691 sqlite3_free(p: p->term.a);
196692}
196693
196694/*
196695** Initialize a node-reader object to read the node in buffer aNode/nNode.
196696**
196697** If successful, SQLITE_OK is returned and the NodeReader object set to
196698** point to the first entry on the node (if any). Otherwise, an SQLite
196699** error code is returned.
196700*/
196701static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
196702 memset(s: p, c: 0, n: sizeof(NodeReader));
196703 p->aNode = aNode;
196704 p->nNode = nNode;
196705
196706 /* Figure out if this is a leaf or an internal node. */
196707 if( aNode && aNode[0] ){
196708 /* An internal node. */
196709 p->iOff = 1 + sqlite3Fts3GetVarint(pBuf: &p->aNode[1], v: &p->iChild);
196710 }else{
196711 p->iOff = 1;
196712 }
196713
196714 return aNode ? nodeReaderNext(p) : SQLITE_OK;
196715}
196716
196717/*
196718** This function is called while writing an FTS segment each time a leaf o
196719** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
196720** to be greater than the largest key on the node just written, but smaller
196721** than or equal to the first key that will be written to the next leaf
196722** node.
196723**
196724** The block id of the leaf node just written to disk may be found in
196725** (pWriter->aNodeWriter[0].iBlock) when this function is called.
196726*/
196727static int fts3IncrmergePush(
196728 Fts3Table *p, /* Fts3 table handle */
196729 IncrmergeWriter *pWriter, /* Writer object */
196730 const char *zTerm, /* Term to write to internal node */
196731 int nTerm /* Bytes at zTerm */
196732){
196733 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
196734 int iLayer;
196735
196736 assert( nTerm>0 );
196737 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
196738 sqlite3_int64 iNextPtr = 0;
196739 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
196740 int rc = SQLITE_OK;
196741 int nPrefix;
196742 int nSuffix;
196743 int nSpace;
196744
196745 /* Figure out how much space the key will consume if it is written to
196746 ** the current node of layer iLayer. Due to the prefix compression,
196747 ** the space required changes depending on which node the key is to
196748 ** be added to. */
196749 nPrefix = fts3PrefixCompress(zPrev: pNode->key.a, nPrev: pNode->key.n, zNext: zTerm, nNext: nTerm);
196750 nSuffix = nTerm - nPrefix;
196751 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB;
196752 nSpace = sqlite3Fts3VarintLen(v: nPrefix);
196753 nSpace += sqlite3Fts3VarintLen(v: nSuffix) + nSuffix;
196754
196755 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
196756 /* If the current node of layer iLayer contains zero keys, or if adding
196757 ** the key to it will not cause it to grow to larger than nNodeSize
196758 ** bytes in size, write the key here. */
196759
196760 Blob *pBlk = &pNode->block;
196761 if( pBlk->n==0 ){
196762 blobGrowBuffer(pBlob: pBlk, nMin: p->nNodeSize, pRc: &rc);
196763 if( rc==SQLITE_OK ){
196764 pBlk->a[0] = (char)iLayer;
196765 pBlk->n = 1 + sqlite3Fts3PutVarint(p: &pBlk->a[1], v: iPtr);
196766 }
196767 }
196768 blobGrowBuffer(pBlob: pBlk, nMin: pBlk->n + nSpace, pRc: &rc);
196769 blobGrowBuffer(pBlob: &pNode->key, nMin: nTerm, pRc: &rc);
196770
196771 if( rc==SQLITE_OK ){
196772 if( pNode->key.n ){
196773 pBlk->n += sqlite3Fts3PutVarint(p: &pBlk->a[pBlk->n], v: nPrefix);
196774 }
196775 pBlk->n += sqlite3Fts3PutVarint(p: &pBlk->a[pBlk->n], v: nSuffix);
196776 assert( nPrefix+nSuffix<=nTerm );
196777 assert( nPrefix>=0 );
196778 memcpy(dest: &pBlk->a[pBlk->n], src: &zTerm[nPrefix], n: nSuffix);
196779 pBlk->n += nSuffix;
196780
196781 memcpy(dest: pNode->key.a, src: zTerm, n: nTerm);
196782 pNode->key.n = nTerm;
196783 }
196784 }else{
196785 /* Otherwise, flush the current node of layer iLayer to disk.
196786 ** Then allocate a new, empty sibling node. The key will be written
196787 ** into the parent of this node. */
196788 rc = fts3WriteSegment(p, iBlock: pNode->iBlock, z: pNode->block.a, n: pNode->block.n);
196789
196790 assert( pNode->block.nAlloc>=p->nNodeSize );
196791 pNode->block.a[0] = (char)iLayer;
196792 pNode->block.n = 1 + sqlite3Fts3PutVarint(p: &pNode->block.a[1], v: iPtr+1);
196793
196794 iNextPtr = pNode->iBlock;
196795 pNode->iBlock++;
196796 pNode->key.n = 0;
196797 }
196798
196799 if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
196800 iPtr = iNextPtr;
196801 }
196802
196803 assert( 0 );
196804 return 0;
196805}
196806
196807/*
196808** Append a term and (optionally) doclist to the FTS segment node currently
196809** stored in blob *pNode. The node need not contain any terms, but the
196810** header must be written before this function is called.
196811**
196812** A node header is a single 0x00 byte for a leaf node, or a height varint
196813** followed by the left-hand-child varint for an internal node.
196814**
196815** The term to be appended is passed via arguments zTerm/nTerm. For a
196816** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
196817** node, both aDoclist and nDoclist must be passed 0.
196818**
196819** If the size of the value in blob pPrev is zero, then this is the first
196820** term written to the node. Otherwise, pPrev contains a copy of the
196821** previous term. Before this function returns, it is updated to contain a
196822** copy of zTerm/nTerm.
196823**
196824** It is assumed that the buffer associated with pNode is already large
196825** enough to accommodate the new entry. The buffer associated with pPrev
196826** is extended by this function if requrired.
196827**
196828** If an error (i.e. OOM condition) occurs, an SQLite error code is
196829** returned. Otherwise, SQLITE_OK.
196830*/
196831static int fts3AppendToNode(
196832 Blob *pNode, /* Current node image to append to */
196833 Blob *pPrev, /* Buffer containing previous term written */
196834 const char *zTerm, /* New term to write */
196835 int nTerm, /* Size of zTerm in bytes */
196836 const char *aDoclist, /* Doclist (or NULL) to write */
196837 int nDoclist /* Size of aDoclist in bytes */
196838){
196839 int rc = SQLITE_OK; /* Return code */
196840 int bFirst = (pPrev->n==0); /* True if this is the first term written */
196841 int nPrefix; /* Size of term prefix in bytes */
196842 int nSuffix; /* Size of term suffix in bytes */
196843
196844 /* Node must have already been started. There must be a doclist for a
196845 ** leaf node, and there must not be a doclist for an internal node. */
196846 assert( pNode->n>0 );
196847 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
196848
196849 blobGrowBuffer(pBlob: pPrev, nMin: nTerm, pRc: &rc);
196850 if( rc!=SQLITE_OK ) return rc;
196851
196852 nPrefix = fts3PrefixCompress(zPrev: pPrev->a, nPrev: pPrev->n, zNext: zTerm, nNext: nTerm);
196853 nSuffix = nTerm - nPrefix;
196854 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
196855 memcpy(dest: pPrev->a, src: zTerm, n: nTerm);
196856 pPrev->n = nTerm;
196857
196858 if( bFirst==0 ){
196859 pNode->n += sqlite3Fts3PutVarint(p: &pNode->a[pNode->n], v: nPrefix);
196860 }
196861 pNode->n += sqlite3Fts3PutVarint(p: &pNode->a[pNode->n], v: nSuffix);
196862 memcpy(dest: &pNode->a[pNode->n], src: &zTerm[nPrefix], n: nSuffix);
196863 pNode->n += nSuffix;
196864
196865 if( aDoclist ){
196866 pNode->n += sqlite3Fts3PutVarint(p: &pNode->a[pNode->n], v: nDoclist);
196867 memcpy(dest: &pNode->a[pNode->n], src: aDoclist, n: nDoclist);
196868 pNode->n += nDoclist;
196869 }
196870
196871 assert( pNode->n<=pNode->nAlloc );
196872
196873 return SQLITE_OK;
196874}
196875
196876/*
196877** Append the current term and doclist pointed to by cursor pCsr to the
196878** appendable b-tree segment opened for writing by pWriter.
196879**
196880** Return SQLITE_OK if successful, or an SQLite error code otherwise.
196881*/
196882static int fts3IncrmergeAppend(
196883 Fts3Table *p, /* Fts3 table handle */
196884 IncrmergeWriter *pWriter, /* Writer object */
196885 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
196886){
196887 const char *zTerm = pCsr->zTerm;
196888 int nTerm = pCsr->nTerm;
196889 const char *aDoclist = pCsr->aDoclist;
196890 int nDoclist = pCsr->nDoclist;
196891 int rc = SQLITE_OK; /* Return code */
196892 int nSpace; /* Total space in bytes required on leaf */
196893 int nPrefix; /* Size of prefix shared with previous term */
196894 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
196895 NodeWriter *pLeaf; /* Object used to write leaf nodes */
196896
196897 pLeaf = &pWriter->aNodeWriter[0];
196898 nPrefix = fts3PrefixCompress(zPrev: pLeaf->key.a, nPrev: pLeaf->key.n, zNext: zTerm, nNext: nTerm);
196899 nSuffix = nTerm - nPrefix;
196900 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB;
196901
196902 nSpace = sqlite3Fts3VarintLen(v: nPrefix);
196903 nSpace += sqlite3Fts3VarintLen(v: nSuffix) + nSuffix;
196904 nSpace += sqlite3Fts3VarintLen(v: nDoclist) + nDoclist;
196905
196906 /* If the current block is not empty, and if adding this term/doclist
196907 ** to the current block would make it larger than Fts3Table.nNodeSize
196908 ** bytes, write this block out to the database. */
196909 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
196910 rc = fts3WriteSegment(p, iBlock: pLeaf->iBlock, z: pLeaf->block.a, n: pLeaf->block.n);
196911 pWriter->nWork++;
196912
196913 /* Add the current term to the parent node. The term added to the
196914 ** parent must:
196915 **
196916 ** a) be greater than the largest term on the leaf node just written
196917 ** to the database (still available in pLeaf->key), and
196918 **
196919 ** b) be less than or equal to the term about to be added to the new
196920 ** leaf node (zTerm/nTerm).
196921 **
196922 ** In other words, it must be the prefix of zTerm 1 byte longer than
196923 ** the common prefix (if any) of zTerm and pWriter->zTerm.
196924 */
196925 if( rc==SQLITE_OK ){
196926 rc = fts3IncrmergePush(p, pWriter, zTerm, nTerm: nPrefix+1);
196927 }
196928
196929 /* Advance to the next output block */
196930 pLeaf->iBlock++;
196931 pLeaf->key.n = 0;
196932 pLeaf->block.n = 0;
196933
196934 nSuffix = nTerm;
196935 nSpace = 1;
196936 nSpace += sqlite3Fts3VarintLen(v: nSuffix) + nSuffix;
196937 nSpace += sqlite3Fts3VarintLen(v: nDoclist) + nDoclist;
196938 }
196939
196940 pWriter->nLeafData += nSpace;
196941 blobGrowBuffer(pBlob: &pLeaf->block, nMin: pLeaf->block.n + nSpace, pRc: &rc);
196942 if( rc==SQLITE_OK ){
196943 if( pLeaf->block.n==0 ){
196944 pLeaf->block.n = 1;
196945 pLeaf->block.a[0] = '\0';
196946 }
196947 rc = fts3AppendToNode(
196948 pNode: &pLeaf->block, pPrev: &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
196949 );
196950 }
196951
196952 return rc;
196953}
196954
196955/*
196956** This function is called to release all dynamic resources held by the
196957** merge-writer object pWriter, and if no error has occurred, to flush
196958** all outstanding node buffers held by pWriter to disk.
196959**
196960** If *pRc is not SQLITE_OK when this function is called, then no attempt
196961** is made to write any data to disk. Instead, this function serves only
196962** to release outstanding resources.
196963**
196964** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
196965** flushing buffers to disk, *pRc is set to an SQLite error code before
196966** returning.
196967*/
196968static void fts3IncrmergeRelease(
196969 Fts3Table *p, /* FTS3 table handle */
196970 IncrmergeWriter *pWriter, /* Merge-writer object */
196971 int *pRc /* IN/OUT: Error code */
196972){
196973 int i; /* Used to iterate through non-root layers */
196974 int iRoot; /* Index of root in pWriter->aNodeWriter */
196975 NodeWriter *pRoot; /* NodeWriter for root node */
196976 int rc = *pRc; /* Error code */
196977
196978 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
196979 ** root node. If the segment fits entirely on a single leaf node, iRoot
196980 ** will be set to 0. If the root node is the parent of the leaves, iRoot
196981 ** will be 1. And so on. */
196982 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
196983 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
196984 if( pNode->block.n>0 ) break;
196985 assert( *pRc || pNode->block.nAlloc==0 );
196986 assert( *pRc || pNode->key.nAlloc==0 );
196987 sqlite3_free(p: pNode->block.a);
196988 sqlite3_free(p: pNode->key.a);
196989 }
196990
196991 /* Empty output segment. This is a no-op. */
196992 if( iRoot<0 ) return;
196993
196994 /* The entire output segment fits on a single node. Normally, this means
196995 ** the node would be stored as a blob in the "root" column of the %_segdir
196996 ** table. However, this is not permitted in this case. The problem is that
196997 ** space has already been reserved in the %_segments table, and so the
196998 ** start_block and end_block fields of the %_segdir table must be populated.
196999 ** And, by design or by accident, released versions of FTS cannot handle
197000 ** segments that fit entirely on the root node with start_block!=0.
197001 **
197002 ** Instead, create a synthetic root node that contains nothing but a
197003 ** pointer to the single content node. So that the segment consists of a
197004 ** single leaf and a single interior (root) node.
197005 **
197006 ** Todo: Better might be to defer allocating space in the %_segments
197007 ** table until we are sure it is needed.
197008 */
197009 if( iRoot==0 ){
197010 Blob *pBlock = &pWriter->aNodeWriter[1].block;
197011 blobGrowBuffer(pBlob: pBlock, nMin: 1 + FTS3_VARINT_MAX, pRc: &rc);
197012 if( rc==SQLITE_OK ){
197013 pBlock->a[0] = 0x01;
197014 pBlock->n = 1 + sqlite3Fts3PutVarint(
197015 p: &pBlock->a[1], v: pWriter->aNodeWriter[0].iBlock
197016 );
197017 }
197018 iRoot = 1;
197019 }
197020 pRoot = &pWriter->aNodeWriter[iRoot];
197021
197022 /* Flush all currently outstanding nodes to disk. */
197023 for(i=0; i<iRoot; i++){
197024 NodeWriter *pNode = &pWriter->aNodeWriter[i];
197025 if( pNode->block.n>0 && rc==SQLITE_OK ){
197026 rc = fts3WriteSegment(p, iBlock: pNode->iBlock, z: pNode->block.a, n: pNode->block.n);
197027 }
197028 sqlite3_free(p: pNode->block.a);
197029 sqlite3_free(p: pNode->key.a);
197030 }
197031
197032 /* Write the %_segdir record. */
197033 if( rc==SQLITE_OK ){
197034 rc = fts3WriteSegdir(p,
197035 iLevel: pWriter->iAbsLevel+1, /* level */
197036 iIdx: pWriter->iIdx, /* idx */
197037 iStartBlock: pWriter->iStart, /* start_block */
197038 iLeafEndBlock: pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
197039 iEndBlock: pWriter->iEnd, /* end_block */
197040 nLeafData: (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
197041 zRoot: pRoot->block.a, nRoot: pRoot->block.n /* root */
197042 );
197043 }
197044 sqlite3_free(p: pRoot->block.a);
197045 sqlite3_free(p: pRoot->key.a);
197046
197047 *pRc = rc;
197048}
197049
197050/*
197051** Compare the term in buffer zLhs (size in bytes nLhs) with that in
197052** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
197053** the other, it is considered to be smaller than the other.
197054**
197055** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
197056** if it is greater.
197057*/
197058static int fts3TermCmp(
197059 const char *zLhs, int nLhs, /* LHS of comparison */
197060 const char *zRhs, int nRhs /* RHS of comparison */
197061){
197062 int nCmp = MIN(nLhs, nRhs);
197063 int res;
197064
197065 if( nCmp && ALWAYS(zLhs) && ALWAYS(zRhs) ){
197066 res = memcmp(s1: zLhs, s2: zRhs, n: nCmp);
197067 }else{
197068 res = 0;
197069 }
197070 if( res==0 ) res = nLhs - nRhs;
197071
197072 return res;
197073}
197074
197075
197076/*
197077** Query to see if the entry in the %_segments table with blockid iEnd is
197078** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
197079** returning. Otherwise, set *pbRes to 0.
197080**
197081** Or, if an error occurs while querying the database, return an SQLite
197082** error code. The final value of *pbRes is undefined in this case.
197083**
197084** This is used to test if a segment is an "appendable" segment. If it
197085** is, then a NULL entry has been inserted into the %_segments table
197086** with blockid %_segdir.end_block.
197087*/
197088static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
197089 int bRes = 0; /* Result to set *pbRes to */
197090 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
197091 int rc; /* Return code */
197092
197093 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, pp: &pCheck, apVal: 0);
197094 if( rc==SQLITE_OK ){
197095 sqlite3_bind_int64(pStmt: pCheck, i: 1, iValue: iEnd);
197096 if( SQLITE_ROW==sqlite3_step(pStmt: pCheck) ) bRes = 1;
197097 rc = sqlite3_reset(pStmt: pCheck);
197098 }
197099
197100 *pbRes = bRes;
197101 return rc;
197102}
197103
197104/*
197105** This function is called when initializing an incremental-merge operation.
197106** It checks if the existing segment with index value iIdx at absolute level
197107** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
197108** merge-writer object *pWriter is initialized to write to it.
197109**
197110** An existing segment can be appended to by an incremental merge if:
197111**
197112** * It was initially created as an appendable segment (with all required
197113** space pre-allocated), and
197114**
197115** * The first key read from the input (arguments zKey and nKey) is
197116** greater than the largest key currently stored in the potential
197117** output segment.
197118*/
197119static int fts3IncrmergeLoad(
197120 Fts3Table *p, /* Fts3 table handle */
197121 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
197122 int iIdx, /* Index of candidate output segment */
197123 const char *zKey, /* First key to write */
197124 int nKey, /* Number of bytes in nKey */
197125 IncrmergeWriter *pWriter /* Populate this object */
197126){
197127 int rc; /* Return code */
197128 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
197129
197130 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, pp: &pSelect, apVal: 0);
197131 if( rc==SQLITE_OK ){
197132 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
197133 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
197134 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
197135 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
197136 int nRoot = 0; /* Size of aRoot[] in bytes */
197137 int rc2; /* Return code from sqlite3_reset() */
197138 int bAppendable = 0; /* Set to true if segment is appendable */
197139
197140 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
197141 sqlite3_bind_int64(pStmt: pSelect, i: 1, iValue: iAbsLevel+1);
197142 sqlite3_bind_int(p: pSelect, i: 2, iValue: iIdx);
197143 if( sqlite3_step(pStmt: pSelect)==SQLITE_ROW ){
197144 iStart = sqlite3_column_int64(pStmt: pSelect, i: 1);
197145 iLeafEnd = sqlite3_column_int64(pStmt: pSelect, i: 2);
197146 fts3ReadEndBlockField(pStmt: pSelect, iCol: 3, piEndBlock: &iEnd, pnByte: &pWriter->nLeafData);
197147 if( pWriter->nLeafData<0 ){
197148 pWriter->nLeafData = pWriter->nLeafData * -1;
197149 }
197150 pWriter->bNoLeafData = (pWriter->nLeafData==0);
197151 nRoot = sqlite3_column_bytes(pStmt: pSelect, i: 4);
197152 aRoot = sqlite3_column_blob(pStmt: pSelect, i: 4);
197153 if( aRoot==0 ){
197154 sqlite3_reset(pStmt: pSelect);
197155 return nRoot ? SQLITE_NOMEM : FTS_CORRUPT_VTAB;
197156 }
197157 }else{
197158 return sqlite3_reset(pStmt: pSelect);
197159 }
197160
197161 /* Check for the zero-length marker in the %_segments table */
197162 rc = fts3IsAppendable(p, iEnd, pbRes: &bAppendable);
197163
197164 /* Check that zKey/nKey is larger than the largest key the candidate */
197165 if( rc==SQLITE_OK && bAppendable ){
197166 char *aLeaf = 0;
197167 int nLeaf = 0;
197168
197169 rc = sqlite3Fts3ReadBlock(p, iBlockid: iLeafEnd, paBlob: &aLeaf, pnBlob: &nLeaf, pnLoad: 0);
197170 if( rc==SQLITE_OK ){
197171 NodeReader reader;
197172 for(rc = nodeReaderInit(p: &reader, aNode: aLeaf, nNode: nLeaf);
197173 rc==SQLITE_OK && reader.aNode;
197174 rc = nodeReaderNext(p: &reader)
197175 ){
197176 assert( reader.aNode );
197177 }
197178 if( fts3TermCmp(zLhs: zKey, nLhs: nKey, zRhs: reader.term.a, nRhs: reader.term.n)<=0 ){
197179 bAppendable = 0;
197180 }
197181 nodeReaderRelease(p: &reader);
197182 }
197183 sqlite3_free(p: aLeaf);
197184 }
197185
197186 if( rc==SQLITE_OK && bAppendable ){
197187 /* It is possible to append to this segment. Set up the IncrmergeWriter
197188 ** object to do so. */
197189 int i;
197190 int nHeight = (int)aRoot[0];
197191 NodeWriter *pNode;
197192 if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT ){
197193 sqlite3_reset(pStmt: pSelect);
197194 return FTS_CORRUPT_VTAB;
197195 }
197196
197197 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
197198 pWriter->iStart = iStart;
197199 pWriter->iEnd = iEnd;
197200 pWriter->iAbsLevel = iAbsLevel;
197201 pWriter->iIdx = iIdx;
197202
197203 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
197204 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
197205 }
197206
197207 pNode = &pWriter->aNodeWriter[nHeight];
197208 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
197209 blobGrowBuffer(pBlob: &pNode->block,
197210 MAX(nRoot, p->nNodeSize)+FTS3_NODE_PADDING, pRc: &rc
197211 );
197212 if( rc==SQLITE_OK ){
197213 memcpy(dest: pNode->block.a, src: aRoot, n: nRoot);
197214 pNode->block.n = nRoot;
197215 memset(s: &pNode->block.a[nRoot], c: 0, FTS3_NODE_PADDING);
197216 }
197217
197218 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
197219 NodeReader reader;
197220 memset(s: &reader, c: 0, n: sizeof(reader));
197221 pNode = &pWriter->aNodeWriter[i];
197222
197223 if( pNode->block.a){
197224 rc = nodeReaderInit(p: &reader, aNode: pNode->block.a, nNode: pNode->block.n);
197225 while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(p: &reader);
197226 blobGrowBuffer(pBlob: &pNode->key, nMin: reader.term.n, pRc: &rc);
197227 if( rc==SQLITE_OK ){
197228 assert_fts3_nc( reader.term.n>0 || reader.aNode==0 );
197229 if( reader.term.n>0 ){
197230 memcpy(dest: pNode->key.a, src: reader.term.a, n: reader.term.n);
197231 }
197232 pNode->key.n = reader.term.n;
197233 if( i>0 ){
197234 char *aBlock = 0;
197235 int nBlock = 0;
197236 pNode = &pWriter->aNodeWriter[i-1];
197237 pNode->iBlock = reader.iChild;
197238 rc = sqlite3Fts3ReadBlock(p, iBlockid: reader.iChild, paBlob: &aBlock, pnBlob: &nBlock,pnLoad: 0);
197239 blobGrowBuffer(pBlob: &pNode->block,
197240 MAX(nBlock, p->nNodeSize)+FTS3_NODE_PADDING, pRc: &rc
197241 );
197242 if( rc==SQLITE_OK ){
197243 memcpy(dest: pNode->block.a, src: aBlock, n: nBlock);
197244 pNode->block.n = nBlock;
197245 memset(s: &pNode->block.a[nBlock], c: 0, FTS3_NODE_PADDING);
197246 }
197247 sqlite3_free(p: aBlock);
197248 }
197249 }
197250 }
197251 nodeReaderRelease(p: &reader);
197252 }
197253 }
197254
197255 rc2 = sqlite3_reset(pStmt: pSelect);
197256 if( rc==SQLITE_OK ) rc = rc2;
197257 }
197258
197259 return rc;
197260}
197261
197262/*
197263** Determine the largest segment index value that exists within absolute
197264** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
197265** one before returning SQLITE_OK. Or, if there are no segments at all
197266** within level iAbsLevel, set *piIdx to zero.
197267**
197268** If an error occurs, return an SQLite error code. The final value of
197269** *piIdx is undefined in this case.
197270*/
197271static int fts3IncrmergeOutputIdx(
197272 Fts3Table *p, /* FTS Table handle */
197273 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
197274 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
197275){
197276 int rc;
197277 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
197278
197279 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, pp: &pOutputIdx, apVal: 0);
197280 if( rc==SQLITE_OK ){
197281 sqlite3_bind_int64(pStmt: pOutputIdx, i: 1, iValue: iAbsLevel+1);
197282 sqlite3_step(pStmt: pOutputIdx);
197283 *piIdx = sqlite3_column_int(pStmt: pOutputIdx, i: 0);
197284 rc = sqlite3_reset(pStmt: pOutputIdx);
197285 }
197286
197287 return rc;
197288}
197289
197290/*
197291** Allocate an appendable output segment on absolute level iAbsLevel+1
197292** with idx value iIdx.
197293**
197294** In the %_segdir table, a segment is defined by the values in three
197295** columns:
197296**
197297** start_block
197298** leaves_end_block
197299** end_block
197300**
197301** When an appendable segment is allocated, it is estimated that the
197302** maximum number of leaf blocks that may be required is the sum of the
197303** number of leaf blocks consumed by the input segments, plus the number
197304** of input segments, multiplied by two. This value is stored in stack
197305** variable nLeafEst.
197306**
197307** A total of 16*nLeafEst blocks are allocated when an appendable segment
197308** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
197309** array of leaf nodes starts at the first block allocated. The array
197310** of interior nodes that are parents of the leaf nodes start at block
197311** (start_block + (1 + end_block - start_block) / 16). And so on.
197312**
197313** In the actual code below, the value "16" is replaced with the
197314** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
197315*/
197316static int fts3IncrmergeWriter(
197317 Fts3Table *p, /* Fts3 table handle */
197318 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
197319 int iIdx, /* Index of new output segment */
197320 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
197321 IncrmergeWriter *pWriter /* Populate this object */
197322){
197323 int rc; /* Return Code */
197324 int i; /* Iterator variable */
197325 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
197326 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
197327 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
197328
197329 /* Calculate nLeafEst. */
197330 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, pp: &pLeafEst, apVal: 0);
197331 if( rc==SQLITE_OK ){
197332 sqlite3_bind_int64(pStmt: pLeafEst, i: 1, iValue: iAbsLevel);
197333 sqlite3_bind_int64(pStmt: pLeafEst, i: 2, iValue: pCsr->nSegment);
197334 if( SQLITE_ROW==sqlite3_step(pStmt: pLeafEst) ){
197335 nLeafEst = sqlite3_column_int(pStmt: pLeafEst, i: 0);
197336 }
197337 rc = sqlite3_reset(pStmt: pLeafEst);
197338 }
197339 if( rc!=SQLITE_OK ) return rc;
197340
197341 /* Calculate the first block to use in the output segment */
197342 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, pp: &pFirstBlock, apVal: 0);
197343 if( rc==SQLITE_OK ){
197344 if( SQLITE_ROW==sqlite3_step(pStmt: pFirstBlock) ){
197345 pWriter->iStart = sqlite3_column_int64(pStmt: pFirstBlock, i: 0);
197346 pWriter->iEnd = pWriter->iStart - 1;
197347 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
197348 }
197349 rc = sqlite3_reset(pStmt: pFirstBlock);
197350 }
197351 if( rc!=SQLITE_OK ) return rc;
197352
197353 /* Insert the marker in the %_segments table to make sure nobody tries
197354 ** to steal the space just allocated. This is also used to identify
197355 ** appendable segments. */
197356 rc = fts3WriteSegment(p, iBlock: pWriter->iEnd, z: 0, n: 0);
197357 if( rc!=SQLITE_OK ) return rc;
197358
197359 pWriter->iAbsLevel = iAbsLevel;
197360 pWriter->nLeafEst = nLeafEst;
197361 pWriter->iIdx = iIdx;
197362
197363 /* Set up the array of NodeWriter objects */
197364 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
197365 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
197366 }
197367 return SQLITE_OK;
197368}
197369
197370/*
197371** Remove an entry from the %_segdir table. This involves running the
197372** following two statements:
197373**
197374** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
197375** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
197376**
197377** The DELETE statement removes the specific %_segdir level. The UPDATE
197378** statement ensures that the remaining segments have contiguously allocated
197379** idx values.
197380*/
197381static int fts3RemoveSegdirEntry(
197382 Fts3Table *p, /* FTS3 table handle */
197383 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
197384 int iIdx /* Index of %_segdir entry to delete */
197385){
197386 int rc; /* Return code */
197387 sqlite3_stmt *pDelete = 0; /* DELETE statement */
197388
197389 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, pp: &pDelete, apVal: 0);
197390 if( rc==SQLITE_OK ){
197391 sqlite3_bind_int64(pStmt: pDelete, i: 1, iValue: iAbsLevel);
197392 sqlite3_bind_int(p: pDelete, i: 2, iValue: iIdx);
197393 sqlite3_step(pStmt: pDelete);
197394 rc = sqlite3_reset(pStmt: pDelete);
197395 }
197396
197397 return rc;
197398}
197399
197400/*
197401** One or more segments have just been removed from absolute level iAbsLevel.
197402** Update the 'idx' values of the remaining segments in the level so that
197403** the idx values are a contiguous sequence starting from 0.
197404*/
197405static int fts3RepackSegdirLevel(
197406 Fts3Table *p, /* FTS3 table handle */
197407 sqlite3_int64 iAbsLevel /* Absolute level to repack */
197408){
197409 int rc; /* Return code */
197410 int *aIdx = 0; /* Array of remaining idx values */
197411 int nIdx = 0; /* Valid entries in aIdx[] */
197412 int nAlloc = 0; /* Allocated size of aIdx[] */
197413 int i; /* Iterator variable */
197414 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
197415 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
197416
197417 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, pp: &pSelect, apVal: 0);
197418 if( rc==SQLITE_OK ){
197419 int rc2;
197420 sqlite3_bind_int64(pStmt: pSelect, i: 1, iValue: iAbsLevel);
197421 while( SQLITE_ROW==sqlite3_step(pStmt: pSelect) ){
197422 if( nIdx>=nAlloc ){
197423 int *aNew;
197424 nAlloc += 16;
197425 aNew = sqlite3_realloc64(pOld: aIdx, n: nAlloc*sizeof(int));
197426 if( !aNew ){
197427 rc = SQLITE_NOMEM;
197428 break;
197429 }
197430 aIdx = aNew;
197431 }
197432 aIdx[nIdx++] = sqlite3_column_int(pStmt: pSelect, i: 0);
197433 }
197434 rc2 = sqlite3_reset(pStmt: pSelect);
197435 if( rc==SQLITE_OK ) rc = rc2;
197436 }
197437
197438 if( rc==SQLITE_OK ){
197439 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, pp: &pUpdate, apVal: 0);
197440 }
197441 if( rc==SQLITE_OK ){
197442 sqlite3_bind_int64(pStmt: pUpdate, i: 2, iValue: iAbsLevel);
197443 }
197444
197445 assert( p->bIgnoreSavepoint==0 );
197446 p->bIgnoreSavepoint = 1;
197447 for(i=0; rc==SQLITE_OK && i<nIdx; i++){
197448 if( aIdx[i]!=i ){
197449 sqlite3_bind_int(p: pUpdate, i: 3, iValue: aIdx[i]);
197450 sqlite3_bind_int(p: pUpdate, i: 1, iValue: i);
197451 sqlite3_step(pStmt: pUpdate);
197452 rc = sqlite3_reset(pStmt: pUpdate);
197453 }
197454 }
197455 p->bIgnoreSavepoint = 0;
197456
197457 sqlite3_free(p: aIdx);
197458 return rc;
197459}
197460
197461static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
197462 pNode->a[0] = (char)iHeight;
197463 if( iChild ){
197464 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
197465 pNode->n = 1 + sqlite3Fts3PutVarint(p: &pNode->a[1], v: iChild);
197466 }else{
197467 assert( pNode->nAlloc>=1 );
197468 pNode->n = 1;
197469 }
197470}
197471
197472/*
197473** The first two arguments are a pointer to and the size of a segment b-tree
197474** node. The node may be a leaf or an internal node.
197475**
197476** This function creates a new node image in blob object *pNew by copying
197477** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
197478** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
197479*/
197480static int fts3TruncateNode(
197481 const char *aNode, /* Current node image */
197482 int nNode, /* Size of aNode in bytes */
197483 Blob *pNew, /* OUT: Write new node image here */
197484 const char *zTerm, /* Omit all terms smaller than this */
197485 int nTerm, /* Size of zTerm in bytes */
197486 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
197487){
197488 NodeReader reader; /* Reader object */
197489 Blob prev = {0, 0, 0}; /* Previous term written to new node */
197490 int rc = SQLITE_OK; /* Return code */
197491 int bLeaf; /* True for a leaf node */
197492
197493 if( nNode<1 ) return FTS_CORRUPT_VTAB;
197494 bLeaf = aNode[0]=='\0';
197495
197496 /* Allocate required output space */
197497 blobGrowBuffer(pBlob: pNew, nMin: nNode, pRc: &rc);
197498 if( rc!=SQLITE_OK ) return rc;
197499 pNew->n = 0;
197500
197501 /* Populate new node buffer */
197502 for(rc = nodeReaderInit(p: &reader, aNode, nNode);
197503 rc==SQLITE_OK && reader.aNode;
197504 rc = nodeReaderNext(p: &reader)
197505 ){
197506 if( pNew->n==0 ){
197507 int res = fts3TermCmp(zLhs: reader.term.a, nLhs: reader.term.n, zRhs: zTerm, nRhs: nTerm);
197508 if( res<0 || (bLeaf==0 && res==0) ) continue;
197509 fts3StartNode(pNode: pNew, iHeight: (int)aNode[0], iChild: reader.iChild);
197510 *piBlock = reader.iChild;
197511 }
197512 rc = fts3AppendToNode(
197513 pNode: pNew, pPrev: &prev, zTerm: reader.term.a, nTerm: reader.term.n,
197514 aDoclist: reader.aDoclist, nDoclist: reader.nDoclist
197515 );
197516 if( rc!=SQLITE_OK ) break;
197517 }
197518 if( pNew->n==0 ){
197519 fts3StartNode(pNode: pNew, iHeight: (int)aNode[0], iChild: reader.iChild);
197520 *piBlock = reader.iChild;
197521 }
197522 assert( pNew->n<=pNew->nAlloc );
197523
197524 nodeReaderRelease(p: &reader);
197525 sqlite3_free(p: prev.a);
197526 return rc;
197527}
197528
197529/*
197530** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
197531** level iAbsLevel. This may involve deleting entries from the %_segments
197532** table, and modifying existing entries in both the %_segments and %_segdir
197533** tables.
197534**
197535** SQLITE_OK is returned if the segment is updated successfully. Or an
197536** SQLite error code otherwise.
197537*/
197538static int fts3TruncateSegment(
197539 Fts3Table *p, /* FTS3 table handle */
197540 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
197541 int iIdx, /* Index within level of segment to modify */
197542 const char *zTerm, /* Remove terms smaller than this */
197543 int nTerm /* Number of bytes in buffer zTerm */
197544){
197545 int rc = SQLITE_OK; /* Return code */
197546 Blob root = {0,0,0}; /* New root page image */
197547 Blob block = {0,0,0}; /* Buffer used for any other block */
197548 sqlite3_int64 iBlock = 0; /* Block id */
197549 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
197550 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
197551 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
197552
197553 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, pp: &pFetch, apVal: 0);
197554 if( rc==SQLITE_OK ){
197555 int rc2; /* sqlite3_reset() return code */
197556 sqlite3_bind_int64(pStmt: pFetch, i: 1, iValue: iAbsLevel);
197557 sqlite3_bind_int(p: pFetch, i: 2, iValue: iIdx);
197558 if( SQLITE_ROW==sqlite3_step(pStmt: pFetch) ){
197559 const char *aRoot = sqlite3_column_blob(pStmt: pFetch, i: 4);
197560 int nRoot = sqlite3_column_bytes(pStmt: pFetch, i: 4);
197561 iOldStart = sqlite3_column_int64(pStmt: pFetch, i: 1);
197562 rc = fts3TruncateNode(aNode: aRoot, nNode: nRoot, pNew: &root, zTerm, nTerm, piBlock: &iBlock);
197563 }
197564 rc2 = sqlite3_reset(pStmt: pFetch);
197565 if( rc==SQLITE_OK ) rc = rc2;
197566 }
197567
197568 while( rc==SQLITE_OK && iBlock ){
197569 char *aBlock = 0;
197570 int nBlock = 0;
197571 iNewStart = iBlock;
197572
197573 rc = sqlite3Fts3ReadBlock(p, iBlockid: iBlock, paBlob: &aBlock, pnBlob: &nBlock, pnLoad: 0);
197574 if( rc==SQLITE_OK ){
197575 rc = fts3TruncateNode(aNode: aBlock, nNode: nBlock, pNew: &block, zTerm, nTerm, piBlock: &iBlock);
197576 }
197577 if( rc==SQLITE_OK ){
197578 rc = fts3WriteSegment(p, iBlock: iNewStart, z: block.a, n: block.n);
197579 }
197580 sqlite3_free(p: aBlock);
197581 }
197582
197583 /* Variable iNewStart now contains the first valid leaf node. */
197584 if( rc==SQLITE_OK && iNewStart ){
197585 sqlite3_stmt *pDel = 0;
197586 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, pp: &pDel, apVal: 0);
197587 if( rc==SQLITE_OK ){
197588 sqlite3_bind_int64(pStmt: pDel, i: 1, iValue: iOldStart);
197589 sqlite3_bind_int64(pStmt: pDel, i: 2, iValue: iNewStart-1);
197590 sqlite3_step(pStmt: pDel);
197591 rc = sqlite3_reset(pStmt: pDel);
197592 }
197593 }
197594
197595 if( rc==SQLITE_OK ){
197596 sqlite3_stmt *pChomp = 0;
197597 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, pp: &pChomp, apVal: 0);
197598 if( rc==SQLITE_OK ){
197599 sqlite3_bind_int64(pStmt: pChomp, i: 1, iValue: iNewStart);
197600 sqlite3_bind_blob(pStmt: pChomp, i: 2, zData: root.a, nData: root.n, SQLITE_STATIC);
197601 sqlite3_bind_int64(pStmt: pChomp, i: 3, iValue: iAbsLevel);
197602 sqlite3_bind_int(p: pChomp, i: 4, iValue: iIdx);
197603 sqlite3_step(pStmt: pChomp);
197604 rc = sqlite3_reset(pStmt: pChomp);
197605 sqlite3_bind_null(pStmt: pChomp, i: 2);
197606 }
197607 }
197608
197609 sqlite3_free(p: root.a);
197610 sqlite3_free(p: block.a);
197611 return rc;
197612}
197613
197614/*
197615** This function is called after an incrmental-merge operation has run to
197616** merge (or partially merge) two or more segments from absolute level
197617** iAbsLevel.
197618**
197619** Each input segment is either removed from the db completely (if all of
197620** its data was copied to the output segment by the incrmerge operation)
197621** or modified in place so that it no longer contains those entries that
197622** have been duplicated in the output segment.
197623*/
197624static int fts3IncrmergeChomp(
197625 Fts3Table *p, /* FTS table handle */
197626 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
197627 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
197628 int *pnRem /* Number of segments not deleted */
197629){
197630 int i;
197631 int nRem = 0;
197632 int rc = SQLITE_OK;
197633
197634 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
197635 Fts3SegReader *pSeg = 0;
197636 int j;
197637
197638 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
197639 ** somewhere in the pCsr->apSegment[] array. */
197640 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
197641 pSeg = pCsr->apSegment[j];
197642 if( pSeg->iIdx==i ) break;
197643 }
197644 assert( j<pCsr->nSegment && pSeg->iIdx==i );
197645
197646 if( pSeg->aNode==0 ){
197647 /* Seg-reader is at EOF. Remove the entire input segment. */
197648 rc = fts3DeleteSegment(p, pSeg);
197649 if( rc==SQLITE_OK ){
197650 rc = fts3RemoveSegdirEntry(p, iAbsLevel, iIdx: pSeg->iIdx);
197651 }
197652 *pnRem = 0;
197653 }else{
197654 /* The incremental merge did not copy all the data from this
197655 ** segment to the upper level. The segment is modified in place
197656 ** so that it contains no keys smaller than zTerm/nTerm. */
197657 const char *zTerm = pSeg->zTerm;
197658 int nTerm = pSeg->nTerm;
197659 rc = fts3TruncateSegment(p, iAbsLevel, iIdx: pSeg->iIdx, zTerm, nTerm);
197660 nRem++;
197661 }
197662 }
197663
197664 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
197665 rc = fts3RepackSegdirLevel(p, iAbsLevel);
197666 }
197667
197668 *pnRem = nRem;
197669 return rc;
197670}
197671
197672/*
197673** Store an incr-merge hint in the database.
197674*/
197675static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
197676 sqlite3_stmt *pReplace = 0;
197677 int rc; /* Return code */
197678
197679 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, pp: &pReplace, apVal: 0);
197680 if( rc==SQLITE_OK ){
197681 sqlite3_bind_int(p: pReplace, i: 1, FTS_STAT_INCRMERGEHINT);
197682 sqlite3_bind_blob(pStmt: pReplace, i: 2, zData: pHint->a, nData: pHint->n, SQLITE_STATIC);
197683 sqlite3_step(pStmt: pReplace);
197684 rc = sqlite3_reset(pStmt: pReplace);
197685 sqlite3_bind_null(pStmt: pReplace, i: 2);
197686 }
197687
197688 return rc;
197689}
197690
197691/*
197692** Load an incr-merge hint from the database. The incr-merge hint, if one
197693** exists, is stored in the rowid==1 row of the %_stat table.
197694**
197695** If successful, populate blob *pHint with the value read from the %_stat
197696** table and return SQLITE_OK. Otherwise, if an error occurs, return an
197697** SQLite error code.
197698*/
197699static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
197700 sqlite3_stmt *pSelect = 0;
197701 int rc;
197702
197703 pHint->n = 0;
197704 rc = fts3SqlStmt(p, SQL_SELECT_STAT, pp: &pSelect, apVal: 0);
197705 if( rc==SQLITE_OK ){
197706 int rc2;
197707 sqlite3_bind_int(p: pSelect, i: 1, FTS_STAT_INCRMERGEHINT);
197708 if( SQLITE_ROW==sqlite3_step(pStmt: pSelect) ){
197709 const char *aHint = sqlite3_column_blob(pStmt: pSelect, i: 0);
197710 int nHint = sqlite3_column_bytes(pStmt: pSelect, i: 0);
197711 if( aHint ){
197712 blobGrowBuffer(pBlob: pHint, nMin: nHint, pRc: &rc);
197713 if( rc==SQLITE_OK ){
197714 if( ALWAYS(pHint->a!=0) ) memcpy(dest: pHint->a, src: aHint, n: nHint);
197715 pHint->n = nHint;
197716 }
197717 }
197718 }
197719 rc2 = sqlite3_reset(pStmt: pSelect);
197720 if( rc==SQLITE_OK ) rc = rc2;
197721 }
197722
197723 return rc;
197724}
197725
197726/*
197727** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
197728** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
197729** consists of two varints, the absolute level number of the input segments
197730** and the number of input segments.
197731**
197732** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
197733** set *pRc to an SQLite error code before returning.
197734*/
197735static void fts3IncrmergeHintPush(
197736 Blob *pHint, /* Hint blob to append to */
197737 i64 iAbsLevel, /* First varint to store in hint */
197738 int nInput, /* Second varint to store in hint */
197739 int *pRc /* IN/OUT: Error code */
197740){
197741 blobGrowBuffer(pBlob: pHint, nMin: pHint->n + 2*FTS3_VARINT_MAX, pRc);
197742 if( *pRc==SQLITE_OK ){
197743 pHint->n += sqlite3Fts3PutVarint(p: &pHint->a[pHint->n], v: iAbsLevel);
197744 pHint->n += sqlite3Fts3PutVarint(p: &pHint->a[pHint->n], v: (i64)nInput);
197745 }
197746}
197747
197748/*
197749** Read the last entry (most recently pushed) from the hint blob *pHint
197750** and then remove the entry. Write the two values read to *piAbsLevel and
197751** *pnInput before returning.
197752**
197753** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
197754** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
197755*/
197756static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
197757 const int nHint = pHint->n;
197758 int i;
197759
197760 i = pHint->n-1;
197761 if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB;
197762 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
197763 if( i==0 ) return FTS_CORRUPT_VTAB;
197764 i--;
197765 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
197766
197767 pHint->n = i;
197768 i += sqlite3Fts3GetVarint(pBuf: &pHint->a[i], v: piAbsLevel);
197769 i += fts3GetVarint32(&pHint->a[i], pnInput);
197770 assert( i<=nHint );
197771 if( i!=nHint ) return FTS_CORRUPT_VTAB;
197772
197773 return SQLITE_OK;
197774}
197775
197776
197777/*
197778** Attempt an incremental merge that writes nMerge leaf blocks.
197779**
197780** Incremental merges happen nMin segments at a time. The segments
197781** to be merged are the nMin oldest segments (the ones with the smallest
197782** values for the _segdir.idx field) in the highest level that contains
197783** at least nMin segments. Multiple merges might occur in an attempt to
197784** write the quota of nMerge leaf blocks.
197785*/
197786SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
197787 int rc; /* Return code */
197788 int nRem = nMerge; /* Number of leaf pages yet to be written */
197789 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
197790 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
197791 IncrmergeWriter *pWriter; /* Writer object */
197792 int nSeg = 0; /* Number of input segments */
197793 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
197794 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
197795 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
197796
197797 /* Allocate space for the cursor, filter and writer objects */
197798 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
197799 pWriter = (IncrmergeWriter *)sqlite3_malloc64(n: nAlloc);
197800 if( !pWriter ) return SQLITE_NOMEM;
197801 pFilter = (Fts3SegFilter *)&pWriter[1];
197802 pCsr = (Fts3MultiSegReader *)&pFilter[1];
197803
197804 rc = fts3IncrmergeHintLoad(p, pHint: &hint);
197805 while( rc==SQLITE_OK && nRem>0 ){
197806 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
197807 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
197808 int bUseHint = 0; /* True if attempting to append */
197809 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
197810
197811 /* Search the %_segdir table for the absolute level with the smallest
197812 ** relative level number that contains at least nMin segments, if any.
197813 ** If one is found, set iAbsLevel to the absolute level number and
197814 ** nSeg to nMin. If no level with at least nMin segments can be found,
197815 ** set nSeg to -1.
197816 */
197817 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, pp: &pFindLevel, apVal: 0);
197818 sqlite3_bind_int(p: pFindLevel, i: 1, MAX(2, nMin));
197819 if( sqlite3_step(pStmt: pFindLevel)==SQLITE_ROW ){
197820 iAbsLevel = sqlite3_column_int64(pStmt: pFindLevel, i: 0);
197821 nSeg = sqlite3_column_int(pStmt: pFindLevel, i: 1);
197822 assert( nSeg>=2 );
197823 }else{
197824 nSeg = -1;
197825 }
197826 rc = sqlite3_reset(pStmt: pFindLevel);
197827
197828 /* If the hint read from the %_stat table is not empty, check if the
197829 ** last entry in it specifies a relative level smaller than or equal
197830 ** to the level identified by the block above (if any). If so, this
197831 ** iteration of the loop will work on merging at the hinted level.
197832 */
197833 if( rc==SQLITE_OK && hint.n ){
197834 int nHint = hint.n;
197835 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
197836 int nHintSeg = 0; /* Hint number of segments */
197837
197838 rc = fts3IncrmergeHintPop(pHint: &hint, piAbsLevel: &iHintAbsLevel, pnInput: &nHintSeg);
197839 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
197840 /* Based on the scan in the block above, it is known that there
197841 ** are no levels with a relative level smaller than that of
197842 ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
197843 ** no levels with more than nMin segments. Use this to limit the
197844 ** value of nHintSeg to avoid a large memory allocation in case the
197845 ** merge-hint is corrupt*/
197846 iAbsLevel = iHintAbsLevel;
197847 nSeg = MIN(MAX(nMin,nSeg), nHintSeg);
197848 bUseHint = 1;
197849 bDirtyHint = 1;
197850 }else{
197851 /* This undoes the effect of the HintPop() above - so that no entry
197852 ** is removed from the hint blob. */
197853 hint.n = nHint;
197854 }
197855 }
197856
197857 /* If nSeg is less that zero, then there is no level with at least
197858 ** nMin segments and no hint in the %_stat table. No work to do.
197859 ** Exit early in this case. */
197860 if( nSeg<=0 ) break;
197861
197862 assert( nMod<=0x7FFFFFFF );
197863 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
197864 rc = FTS_CORRUPT_VTAB;
197865 break;
197866 }
197867
197868 /* Open a cursor to iterate through the contents of the oldest nSeg
197869 ** indexes of absolute level iAbsLevel. If this cursor is opened using
197870 ** the 'hint' parameters, it is possible that there are less than nSeg
197871 ** segments available in level iAbsLevel. In this case, no work is
197872 ** done on iAbsLevel - fall through to the next iteration of the loop
197873 ** to start work on some other level. */
197874 memset(s: pWriter, c: 0, n: nAlloc);
197875 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
197876
197877 if( rc==SQLITE_OK ){
197878 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, piIdx: &iIdx);
197879 assert( bUseHint==1 || bUseHint==0 );
197880 if( iIdx==0 || (bUseHint && iIdx==1) ){
197881 int bIgnore = 0;
197882 rc = fts3SegmentIsMaxLevel(p, iAbsLevel: iAbsLevel+1, pbMax: &bIgnore);
197883 if( bIgnore ){
197884 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
197885 }
197886 }
197887 }
197888
197889 if( rc==SQLITE_OK ){
197890 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
197891 }
197892 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
197893 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
197894 ){
197895 int bEmpty = 0;
197896 rc = sqlite3Fts3SegReaderStep(p, pCsr);
197897 if( rc==SQLITE_OK ){
197898 bEmpty = 1;
197899 }else if( rc!=SQLITE_ROW ){
197900 sqlite3Fts3SegReaderFinish(pCsr);
197901 break;
197902 }
197903 if( bUseHint && iIdx>0 ){
197904 const char *zKey = pCsr->zTerm;
197905 int nKey = pCsr->nTerm;
197906 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx: iIdx-1, zKey, nKey, pWriter);
197907 }else{
197908 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
197909 }
197910
197911 if( rc==SQLITE_OK && pWriter->nLeafEst ){
197912 fts3LogMerge(nSeg, iAbsLevel);
197913 if( bEmpty==0 ){
197914 do {
197915 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
197916 if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
197917 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
197918 }while( rc==SQLITE_ROW );
197919 }
197920
197921 /* Update or delete the input segments */
197922 if( rc==SQLITE_OK ){
197923 nRem -= (1 + pWriter->nWork);
197924 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, pnRem: &nSeg);
197925 if( nSeg!=0 ){
197926 bDirtyHint = 1;
197927 fts3IncrmergeHintPush(pHint: &hint, iAbsLevel, nInput: nSeg, pRc: &rc);
197928 }
197929 }
197930 }
197931
197932 if( nSeg!=0 ){
197933 pWriter->nLeafData = pWriter->nLeafData * -1;
197934 }
197935 fts3IncrmergeRelease(p, pWriter, pRc: &rc);
197936 if( nSeg==0 && pWriter->bNoLeafData==0 ){
197937 fts3PromoteSegments(p, iAbsLevel: iAbsLevel+1, nByte: pWriter->nLeafData);
197938 }
197939 }
197940
197941 sqlite3Fts3SegReaderFinish(pCsr);
197942 }
197943
197944 /* Write the hint values into the %_stat table for the next incr-merger */
197945 if( bDirtyHint && rc==SQLITE_OK ){
197946 rc = fts3IncrmergeHintStore(p, pHint: &hint);
197947 }
197948
197949 sqlite3_free(p: pWriter);
197950 sqlite3_free(p: hint.a);
197951 return rc;
197952}
197953
197954/*
197955** Convert the text beginning at *pz into an integer and return
197956** its value. Advance *pz to point to the first character past
197957** the integer.
197958**
197959** This function used for parameters to merge= and incrmerge=
197960** commands.
197961*/
197962static int fts3Getint(const char **pz){
197963 const char *z = *pz;
197964 int i = 0;
197965 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
197966 *pz = z;
197967 return i;
197968}
197969
197970/*
197971** Process statements of the form:
197972**
197973** INSERT INTO table(table) VALUES('merge=A,B');
197974**
197975** A and B are integers that decode to be the number of leaf pages
197976** written for the merge, and the minimum number of segments on a level
197977** before it will be selected for a merge, respectively.
197978*/
197979static int fts3DoIncrmerge(
197980 Fts3Table *p, /* FTS3 table handle */
197981 const char *zParam /* Nul-terminated string containing "A,B" */
197982){
197983 int rc;
197984 int nMin = (MergeCount(p) / 2);
197985 int nMerge = 0;
197986 const char *z = zParam;
197987
197988 /* Read the first integer value */
197989 nMerge = fts3Getint(pz: &z);
197990
197991 /* If the first integer value is followed by a ',', read the second
197992 ** integer value. */
197993 if( z[0]==',' && z[1]!='\0' ){
197994 z++;
197995 nMin = fts3Getint(pz: &z);
197996 }
197997
197998 if( z[0]!='\0' || nMin<2 ){
197999 rc = SQLITE_ERROR;
198000 }else{
198001 rc = SQLITE_OK;
198002 if( !p->bHasStat ){
198003 assert( p->bFts4==0 );
198004 sqlite3Fts3CreateStatTable(pRc: &rc, p);
198005 }
198006 if( rc==SQLITE_OK ){
198007 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
198008 }
198009 sqlite3Fts3SegmentsClose(p);
198010 }
198011 return rc;
198012}
198013
198014/*
198015** Process statements of the form:
198016**
198017** INSERT INTO table(table) VALUES('automerge=X');
198018**
198019** where X is an integer. X==0 means to turn automerge off. X!=0 means
198020** turn it on. The setting is persistent.
198021*/
198022static int fts3DoAutoincrmerge(
198023 Fts3Table *p, /* FTS3 table handle */
198024 const char *zParam /* Nul-terminated string containing boolean */
198025){
198026 int rc = SQLITE_OK;
198027 sqlite3_stmt *pStmt = 0;
198028 p->nAutoincrmerge = fts3Getint(pz: &zParam);
198029 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p) ){
198030 p->nAutoincrmerge = 8;
198031 }
198032 if( !p->bHasStat ){
198033 assert( p->bFts4==0 );
198034 sqlite3Fts3CreateStatTable(pRc: &rc, p);
198035 if( rc ) return rc;
198036 }
198037 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, pp: &pStmt, apVal: 0);
198038 if( rc ) return rc;
198039 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_AUTOINCRMERGE);
198040 sqlite3_bind_int(p: pStmt, i: 2, iValue: p->nAutoincrmerge);
198041 sqlite3_step(pStmt);
198042 rc = sqlite3_reset(pStmt);
198043 return rc;
198044}
198045
198046/*
198047** Return a 64-bit checksum for the FTS index entry specified by the
198048** arguments to this function.
198049*/
198050static u64 fts3ChecksumEntry(
198051 const char *zTerm, /* Pointer to buffer containing term */
198052 int nTerm, /* Size of zTerm in bytes */
198053 int iLangid, /* Language id for current row */
198054 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
198055 i64 iDocid, /* Docid for current row. */
198056 int iCol, /* Column number */
198057 int iPos /* Position */
198058){
198059 int i;
198060 u64 ret = (u64)iDocid;
198061
198062 ret += (ret<<3) + iLangid;
198063 ret += (ret<<3) + iIndex;
198064 ret += (ret<<3) + iCol;
198065 ret += (ret<<3) + iPos;
198066 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
198067
198068 return ret;
198069}
198070
198071/*
198072** Return a checksum of all entries in the FTS index that correspond to
198073** language id iLangid. The checksum is calculated by XORing the checksums
198074** of each individual entry (see fts3ChecksumEntry()) together.
198075**
198076** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
198077** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
198078** return value is undefined in this case.
198079*/
198080static u64 fts3ChecksumIndex(
198081 Fts3Table *p, /* FTS3 table handle */
198082 int iLangid, /* Language id to return cksum for */
198083 int iIndex, /* Index to cksum (0..p->nIndex-1) */
198084 int *pRc /* OUT: Return code */
198085){
198086 Fts3SegFilter filter;
198087 Fts3MultiSegReader csr;
198088 int rc;
198089 u64 cksum = 0;
198090
198091 if( *pRc ) return 0;
198092
198093 memset(s: &filter, c: 0, n: sizeof(filter));
198094 memset(s: &csr, c: 0, n: sizeof(csr));
198095 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
198096 filter.flags |= FTS3_SEGMENT_SCAN;
198097
198098 rc = sqlite3Fts3SegReaderCursor(
198099 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, zTerm: 0, nTerm: 0, isPrefix: 0, isScan: 1,pCsr: &csr
198100 );
198101 if( rc==SQLITE_OK ){
198102 rc = sqlite3Fts3SegReaderStart(p, pCsr: &csr, pFilter: &filter);
198103 }
198104
198105 if( rc==SQLITE_OK ){
198106 while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr: &csr)) ){
198107 char *pCsr = csr.aDoclist;
198108 char *pEnd = &pCsr[csr.nDoclist];
198109
198110 i64 iDocid = 0;
198111 i64 iCol = 0;
198112 u64 iPos = 0;
198113
198114 pCsr += sqlite3Fts3GetVarint(pBuf: pCsr, v: &iDocid);
198115 while( pCsr<pEnd ){
198116 u64 iVal = 0;
198117 pCsr += sqlite3Fts3GetVarintU(pBuf: pCsr, v: &iVal);
198118 if( pCsr<pEnd ){
198119 if( iVal==0 || iVal==1 ){
198120 iCol = 0;
198121 iPos = 0;
198122 if( iVal ){
198123 pCsr += sqlite3Fts3GetVarint(pBuf: pCsr, v: &iCol);
198124 }else{
198125 pCsr += sqlite3Fts3GetVarintU(pBuf: pCsr, v: &iVal);
198126 if( p->bDescIdx ){
198127 iDocid = (i64)((u64)iDocid - iVal);
198128 }else{
198129 iDocid = (i64)((u64)iDocid + iVal);
198130 }
198131 }
198132 }else{
198133 iPos += (iVal - 2);
198134 cksum = cksum ^ fts3ChecksumEntry(
198135 zTerm: csr.zTerm, nTerm: csr.nTerm, iLangid, iIndex, iDocid,
198136 iCol: (int)iCol, iPos: (int)iPos
198137 );
198138 }
198139 }
198140 }
198141 }
198142 }
198143 sqlite3Fts3SegReaderFinish(pCsr: &csr);
198144
198145 *pRc = rc;
198146 return cksum;
198147}
198148
198149/*
198150** Check if the contents of the FTS index match the current contents of the
198151** content table. If no error occurs and the contents do match, set *pbOk
198152** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
198153** to false before returning.
198154**
198155** If an error occurs (e.g. an OOM or IO error), return an SQLite error
198156** code. The final value of *pbOk is undefined in this case.
198157*/
198158static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
198159 int rc = SQLITE_OK; /* Return code */
198160 u64 cksum1 = 0; /* Checksum based on FTS index contents */
198161 u64 cksum2 = 0; /* Checksum based on %_content contents */
198162 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
198163
198164 /* This block calculates the checksum according to the FTS index. */
198165 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, pp: &pAllLangid, apVal: 0);
198166 if( rc==SQLITE_OK ){
198167 int rc2;
198168 sqlite3_bind_int(p: pAllLangid, i: 1, iValue: p->iPrevLangid);
198169 sqlite3_bind_int(p: pAllLangid, i: 2, iValue: p->nIndex);
198170 while( rc==SQLITE_OK && sqlite3_step(pStmt: pAllLangid)==SQLITE_ROW ){
198171 int iLangid = sqlite3_column_int(pStmt: pAllLangid, i: 0);
198172 int i;
198173 for(i=0; i<p->nIndex; i++){
198174 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, iIndex: i, pRc: &rc);
198175 }
198176 }
198177 rc2 = sqlite3_reset(pStmt: pAllLangid);
198178 if( rc==SQLITE_OK ) rc = rc2;
198179 }
198180
198181 /* This block calculates the checksum according to the %_content table */
198182 if( rc==SQLITE_OK ){
198183 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
198184 sqlite3_stmt *pStmt = 0;
198185 char *zSql;
198186
198187 zSql = sqlite3_mprintf(zFormat: "SELECT %s" , p->zReadExprlist);
198188 if( !zSql ){
198189 rc = SQLITE_NOMEM;
198190 }else{
198191 rc = sqlite3_prepare_v2(db: p->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
198192 sqlite3_free(p: zSql);
198193 }
198194
198195 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
198196 i64 iDocid = sqlite3_column_int64(pStmt, i: 0);
198197 int iLang = langidFromSelect(p, pSelect: pStmt);
198198 int iCol;
198199
198200 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
198201 if( p->abNotindexed[iCol]==0 ){
198202 const char *zText = (const char *)sqlite3_column_text(pStmt, i: iCol+1);
198203 sqlite3_tokenizer_cursor *pT = 0;
198204
198205 rc = sqlite3Fts3OpenTokenizer(pTokenizer: p->pTokenizer, iLangid: iLang, z: zText, n: -1, ppCsr: &pT);
198206 while( rc==SQLITE_OK ){
198207 char const *zToken; /* Buffer containing token */
198208 int nToken = 0; /* Number of bytes in token */
198209 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
198210 int iPos = 0; /* Position of token in zText */
198211
198212 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
198213 if( rc==SQLITE_OK ){
198214 int i;
198215 cksum2 = cksum2 ^ fts3ChecksumEntry(
198216 zTerm: zToken, nTerm: nToken, iLangid: iLang, iIndex: 0, iDocid, iCol, iPos
198217 );
198218 for(i=1; i<p->nIndex; i++){
198219 if( p->aIndex[i].nPrefix<=nToken ){
198220 cksum2 = cksum2 ^ fts3ChecksumEntry(
198221 zTerm: zToken, nTerm: p->aIndex[i].nPrefix, iLangid: iLang, iIndex: i, iDocid, iCol, iPos
198222 );
198223 }
198224 }
198225 }
198226 }
198227 if( pT ) pModule->xClose(pT);
198228 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
198229 }
198230 }
198231 }
198232
198233 sqlite3_finalize(pStmt);
198234 }
198235
198236 *pbOk = (cksum1==cksum2);
198237 return rc;
198238}
198239
198240/*
198241** Run the integrity-check. If no error occurs and the current contents of
198242** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
198243** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
198244**
198245** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
198246** error code.
198247**
198248** The integrity-check works as follows. For each token and indexed token
198249** prefix in the document set, a 64-bit checksum is calculated (by code
198250** in fts3ChecksumEntry()) based on the following:
198251**
198252** + The index number (0 for the main index, 1 for the first prefix
198253** index etc.),
198254** + The token (or token prefix) text itself,
198255** + The language-id of the row it appears in,
198256** + The docid of the row it appears in,
198257** + The column it appears in, and
198258** + The tokens position within that column.
198259**
198260** The checksums for all entries in the index are XORed together to create
198261** a single checksum for the entire index.
198262**
198263** The integrity-check code calculates the same checksum in two ways:
198264**
198265** 1. By scanning the contents of the FTS index, and
198266** 2. By scanning and tokenizing the content table.
198267**
198268** If the two checksums are identical, the integrity-check is deemed to have
198269** passed.
198270*/
198271static int fts3DoIntegrityCheck(
198272 Fts3Table *p /* FTS3 table handle */
198273){
198274 int rc;
198275 int bOk = 0;
198276 rc = fts3IntegrityCheck(p, pbOk: &bOk);
198277 if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
198278 return rc;
198279}
198280
198281/*
198282** Handle a 'special' INSERT of the form:
198283**
198284** "INSERT INTO tbl(tbl) VALUES(<expr>)"
198285**
198286** Argument pVal contains the result of <expr>. Currently the only
198287** meaningful value to insert is the text 'optimize'.
198288*/
198289static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
198290 int rc = SQLITE_ERROR; /* Return Code */
198291 const char *zVal = (const char *)sqlite3_value_text(pVal);
198292 int nVal = sqlite3_value_bytes(pVal);
198293
198294 if( !zVal ){
198295 return SQLITE_NOMEM;
198296 }else if( nVal==8 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "optimize", N: 8) ){
198297 rc = fts3DoOptimize(p, bReturnDone: 0);
198298 }else if( nVal==7 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "rebuild", N: 7) ){
198299 rc = fts3DoRebuild(p);
198300 }else if( nVal==15 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "integrity-check", N: 15) ){
198301 rc = fts3DoIntegrityCheck(p);
198302 }else if( nVal>6 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "merge=", N: 6) ){
198303 rc = fts3DoIncrmerge(p, zParam: &zVal[6]);
198304 }else if( nVal>10 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "automerge=", N: 10) ){
198305 rc = fts3DoAutoincrmerge(p, zParam: &zVal[10]);
198306#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
198307 }else{
198308 int v;
198309 if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
198310 v = atoi(&zVal[9]);
198311 if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
198312 rc = SQLITE_OK;
198313 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
198314 v = atoi(&zVal[11]);
198315 if( v>=64 && v<=FTS3_MAX_PENDING_DATA ) p->nMaxPendingData = v;
198316 rc = SQLITE_OK;
198317 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
198318 p->bNoIncrDoclist = atoi(&zVal[21]);
198319 rc = SQLITE_OK;
198320 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
198321 v = atoi(&zVal[11]);
198322 if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v;
198323 rc = SQLITE_OK;
198324 }
198325#endif
198326 }
198327 return rc;
198328}
198329
198330#ifndef SQLITE_DISABLE_FTS4_DEFERRED
198331/*
198332** Delete all cached deferred doclists. Deferred doclists are cached
198333** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
198334*/
198335SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
198336 Fts3DeferredToken *pDef;
198337 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
198338 fts3PendingListDelete(pList: pDef->pList);
198339 pDef->pList = 0;
198340 }
198341}
198342
198343/*
198344** Free all entries in the pCsr->pDeffered list. Entries are added to
198345** this list using sqlite3Fts3DeferToken().
198346*/
198347SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
198348 Fts3DeferredToken *pDef;
198349 Fts3DeferredToken *pNext;
198350 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
198351 pNext = pDef->pNext;
198352 fts3PendingListDelete(pList: pDef->pList);
198353 sqlite3_free(p: pDef);
198354 }
198355 pCsr->pDeferred = 0;
198356}
198357
198358/*
198359** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
198360** based on the row that pCsr currently points to.
198361**
198362** A deferred-doclist is like any other doclist with position information
198363** included, except that it only contains entries for a single row of the
198364** table, not for all rows.
198365*/
198366SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
198367 int rc = SQLITE_OK; /* Return code */
198368 if( pCsr->pDeferred ){
198369 int i; /* Used to iterate through table columns */
198370 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
198371 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
198372
198373 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
198374 sqlite3_tokenizer *pT = p->pTokenizer;
198375 sqlite3_tokenizer_module const *pModule = pT->pModule;
198376
198377 assert( pCsr->isRequireSeek==0 );
198378 iDocid = sqlite3_column_int64(pStmt: pCsr->pStmt, i: 0);
198379
198380 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
198381 if( p->abNotindexed[i]==0 ){
198382 const char *zText = (const char *)sqlite3_column_text(pStmt: pCsr->pStmt, i: i+1);
198383 sqlite3_tokenizer_cursor *pTC = 0;
198384
198385 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pT, iLangid: pCsr->iLangid, z: zText, n: -1, ppCsr: &pTC);
198386 while( rc==SQLITE_OK ){
198387 char const *zToken; /* Buffer containing token */
198388 int nToken = 0; /* Number of bytes in token */
198389 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
198390 int iPos = 0; /* Position of token in zText */
198391
198392 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
198393 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
198394 Fts3PhraseToken *pPT = pDef->pToken;
198395 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
198396 && (pPT->bFirst==0 || iPos==0)
198397 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
198398 && (0==memcmp(s1: zToken, s2: pPT->z, n: pPT->n))
198399 ){
198400 fts3PendingListAppend(pp: &pDef->pList, iDocid, iCol: i, iPos, pRc: &rc);
198401 }
198402 }
198403 }
198404 if( pTC ) pModule->xClose(pTC);
198405 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
198406 }
198407 }
198408
198409 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
198410 if( pDef->pList ){
198411 rc = fts3PendingListAppendVarint(pp: &pDef->pList, i: 0);
198412 }
198413 }
198414 }
198415
198416 return rc;
198417}
198418
198419SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
198420 Fts3DeferredToken *p,
198421 char **ppData,
198422 int *pnData
198423){
198424 char *pRet;
198425 int nSkip;
198426 sqlite3_int64 dummy;
198427
198428 *ppData = 0;
198429 *pnData = 0;
198430
198431 if( p->pList==0 ){
198432 return SQLITE_OK;
198433 }
198434
198435 pRet = (char *)sqlite3_malloc64(n: p->pList->nData);
198436 if( !pRet ) return SQLITE_NOMEM;
198437
198438 nSkip = sqlite3Fts3GetVarint(pBuf: p->pList->aData, v: &dummy);
198439 *pnData = p->pList->nData - nSkip;
198440 *ppData = pRet;
198441
198442 memcpy(dest: pRet, src: &p->pList->aData[nSkip], n: *pnData);
198443 return SQLITE_OK;
198444}
198445
198446/*
198447** Add an entry for token pToken to the pCsr->pDeferred list.
198448*/
198449SQLITE_PRIVATE int sqlite3Fts3DeferToken(
198450 Fts3Cursor *pCsr, /* Fts3 table cursor */
198451 Fts3PhraseToken *pToken, /* Token to defer */
198452 int iCol /* Column that token must appear in (or -1) */
198453){
198454 Fts3DeferredToken *pDeferred;
198455 pDeferred = sqlite3_malloc64(n: sizeof(*pDeferred));
198456 if( !pDeferred ){
198457 return SQLITE_NOMEM;
198458 }
198459 memset(s: pDeferred, c: 0, n: sizeof(*pDeferred));
198460 pDeferred->pToken = pToken;
198461 pDeferred->pNext = pCsr->pDeferred;
198462 pDeferred->iCol = iCol;
198463 pCsr->pDeferred = pDeferred;
198464
198465 assert( pToken->pDeferred==0 );
198466 pToken->pDeferred = pDeferred;
198467
198468 return SQLITE_OK;
198469}
198470#endif
198471
198472/*
198473** SQLite value pRowid contains the rowid of a row that may or may not be
198474** present in the FTS3 table. If it is, delete it and adjust the contents
198475** of subsiduary data structures accordingly.
198476*/
198477static int fts3DeleteByRowid(
198478 Fts3Table *p,
198479 sqlite3_value *pRowid,
198480 int *pnChng, /* IN/OUT: Decrement if row is deleted */
198481 u32 *aSzDel
198482){
198483 int rc = SQLITE_OK; /* Return code */
198484 int bFound = 0; /* True if *pRowid really is in the table */
198485
198486 fts3DeleteTerms(pRC: &rc, p, pRowid, aSz: aSzDel, pbFound: &bFound);
198487 if( bFound && rc==SQLITE_OK ){
198488 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
198489 rc = fts3IsEmpty(p, pRowid, pisEmpty: &isEmpty);
198490 if( rc==SQLITE_OK ){
198491 if( isEmpty ){
198492 /* Deleting this row means the whole table is empty. In this case
198493 ** delete the contents of all three tables and throw away any
198494 ** data in the pendingTerms hash table. */
198495 rc = fts3DeleteAll(p, bContent: 1);
198496 *pnChng = 0;
198497 memset(s: aSzDel, c: 0, n: sizeof(u32) * (p->nColumn+1) * 2);
198498 }else{
198499 *pnChng = *pnChng - 1;
198500 if( p->zContentTbl==0 ){
198501 fts3SqlExec(pRC: &rc, p, SQL_DELETE_CONTENT, apVal: &pRowid);
198502 }
198503 if( p->bHasDocsize ){
198504 fts3SqlExec(pRC: &rc, p, SQL_DELETE_DOCSIZE, apVal: &pRowid);
198505 }
198506 }
198507 }
198508 }
198509
198510 return rc;
198511}
198512
198513/*
198514** This function does the work for the xUpdate method of FTS3 virtual
198515** tables. The schema of the virtual table being:
198516**
198517** CREATE TABLE <table name>(
198518** <user columns>,
198519** <table name> HIDDEN,
198520** docid HIDDEN,
198521** <langid> HIDDEN
198522** );
198523**
198524**
198525*/
198526SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
198527 sqlite3_vtab *pVtab, /* FTS3 vtab object */
198528 int nArg, /* Size of argument array */
198529 sqlite3_value **apVal, /* Array of arguments */
198530 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
198531){
198532 Fts3Table *p = (Fts3Table *)pVtab;
198533 int rc = SQLITE_OK; /* Return Code */
198534 u32 *aSzIns = 0; /* Sizes of inserted documents */
198535 u32 *aSzDel = 0; /* Sizes of deleted documents */
198536 int nChng = 0; /* Net change in number of documents */
198537 int bInsertDone = 0;
198538
198539 /* At this point it must be known if the %_stat table exists or not.
198540 ** So bHasStat may not be 2. */
198541 assert( p->bHasStat==0 || p->bHasStat==1 );
198542
198543 assert( p->pSegments==0 );
198544 assert(
198545 nArg==1 /* DELETE operations */
198546 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
198547 );
198548
198549 /* Check for a "special" INSERT operation. One of the form:
198550 **
198551 ** INSERT INTO xyz(xyz) VALUES('command');
198552 */
198553 if( nArg>1
198554 && sqlite3_value_type(pVal: apVal[0])==SQLITE_NULL
198555 && sqlite3_value_type(pVal: apVal[p->nColumn+2])!=SQLITE_NULL
198556 ){
198557 rc = fts3SpecialInsert(p, pVal: apVal[p->nColumn+2]);
198558 goto update_out;
198559 }
198560
198561 if( nArg>1 && sqlite3_value_int(pVal: apVal[2 + p->nColumn + 2])<0 ){
198562 rc = SQLITE_CONSTRAINT;
198563 goto update_out;
198564 }
198565
198566 /* Allocate space to hold the change in document sizes */
198567 aSzDel = sqlite3_malloc64(n: sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
198568 if( aSzDel==0 ){
198569 rc = SQLITE_NOMEM;
198570 goto update_out;
198571 }
198572 aSzIns = &aSzDel[p->nColumn+1];
198573 memset(s: aSzDel, c: 0, n: sizeof(aSzDel[0])*(p->nColumn+1)*2);
198574
198575 rc = fts3Writelock(p);
198576 if( rc!=SQLITE_OK ) goto update_out;
198577
198578 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
198579 ** value, then this operation requires constraint handling.
198580 **
198581 ** If the on-conflict mode is REPLACE, this means that the existing row
198582 ** should be deleted from the database before inserting the new row. Or,
198583 ** if the on-conflict mode is other than REPLACE, then this method must
198584 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
198585 ** modify the database file.
198586 */
198587 if( nArg>1 && p->zContentTbl==0 ){
198588 /* Find the value object that holds the new rowid value. */
198589 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
198590 if( sqlite3_value_type(pVal: pNewRowid)==SQLITE_NULL ){
198591 pNewRowid = apVal[1];
198592 }
198593
198594 if( sqlite3_value_type(pVal: pNewRowid)!=SQLITE_NULL && (
198595 sqlite3_value_type(pVal: apVal[0])==SQLITE_NULL
198596 || sqlite3_value_int64(pVal: apVal[0])!=sqlite3_value_int64(pVal: pNewRowid)
198597 )){
198598 /* The new rowid is not NULL (in this case the rowid will be
198599 ** automatically assigned and there is no chance of a conflict), and
198600 ** the statement is either an INSERT or an UPDATE that modifies the
198601 ** rowid column. So if the conflict mode is REPLACE, then delete any
198602 ** existing row with rowid=pNewRowid.
198603 **
198604 ** Or, if the conflict mode is not REPLACE, insert the new record into
198605 ** the %_content table. If we hit the duplicate rowid constraint (or any
198606 ** other error) while doing so, return immediately.
198607 **
198608 ** This branch may also run if pNewRowid contains a value that cannot
198609 ** be losslessly converted to an integer. In this case, the eventual
198610 ** call to fts3InsertData() (either just below or further on in this
198611 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
198612 ** invoked, it will delete zero rows (since no row will have
198613 ** docid=$pNewRowid if $pNewRowid is not an integer value).
198614 */
198615 if( sqlite3_vtab_on_conflict(db: p->db)==SQLITE_REPLACE ){
198616 rc = fts3DeleteByRowid(p, pRowid: pNewRowid, pnChng: &nChng, aSzDel);
198617 }else{
198618 rc = fts3InsertData(p, apVal, piDocid: pRowid);
198619 bInsertDone = 1;
198620 }
198621 }
198622 }
198623 if( rc!=SQLITE_OK ){
198624 goto update_out;
198625 }
198626
198627 /* If this is a DELETE or UPDATE operation, remove the old record. */
198628 if( sqlite3_value_type(pVal: apVal[0])!=SQLITE_NULL ){
198629 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
198630 rc = fts3DeleteByRowid(p, pRowid: apVal[0], pnChng: &nChng, aSzDel);
198631 }
198632
198633 /* If this is an INSERT or UPDATE operation, insert the new record. */
198634 if( nArg>1 && rc==SQLITE_OK ){
198635 int iLangid = sqlite3_value_int(pVal: apVal[2 + p->nColumn + 2]);
198636 if( bInsertDone==0 ){
198637 rc = fts3InsertData(p, apVal, piDocid: pRowid);
198638 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
198639 rc = FTS_CORRUPT_VTAB;
198640 }
198641 }
198642 if( rc==SQLITE_OK ){
198643 rc = fts3PendingTermsDocid(p, bDelete: 0, iLangid, iDocid: *pRowid);
198644 }
198645 if( rc==SQLITE_OK ){
198646 assert( p->iPrevDocid==*pRowid );
198647 rc = fts3InsertTerms(p, iLangid, apVal, aSz: aSzIns);
198648 }
198649 if( p->bHasDocsize ){
198650 fts3InsertDocsize(pRC: &rc, p, aSz: aSzIns);
198651 }
198652 nChng++;
198653 }
198654
198655 if( p->bFts4 ){
198656 fts3UpdateDocTotals(pRC: &rc, p, aSzIns, aSzDel, nChng);
198657 }
198658
198659 update_out:
198660 sqlite3_free(p: aSzDel);
198661 sqlite3Fts3SegmentsClose(p);
198662 return rc;
198663}
198664
198665/*
198666** Flush any data in the pending-terms hash table to disk. If successful,
198667** merge all segments in the database (including the new segment, if
198668** there was any data to flush) into a single segment.
198669*/
198670SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
198671 int rc;
198672 rc = sqlite3_exec(db: p->db, zSql: "SAVEPOINT fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
198673 if( rc==SQLITE_OK ){
198674 rc = fts3DoOptimize(p, bReturnDone: 1);
198675 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
198676 int rc2 = sqlite3_exec(db: p->db, zSql: "RELEASE fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
198677 if( rc2!=SQLITE_OK ) rc = rc2;
198678 }else{
198679 sqlite3_exec(db: p->db, zSql: "ROLLBACK TO fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
198680 sqlite3_exec(db: p->db, zSql: "RELEASE fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
198681 }
198682 }
198683 sqlite3Fts3SegmentsClose(p);
198684 return rc;
198685}
198686
198687#endif
198688
198689/************** End of fts3_write.c ******************************************/
198690/************** Begin file fts3_snippet.c ************************************/
198691/*
198692** 2009 Oct 23
198693**
198694** The author disclaims copyright to this source code. In place of
198695** a legal notice, here is a blessing:
198696**
198697** May you do good and not evil.
198698** May you find forgiveness for yourself and forgive others.
198699** May you share freely, never taking more than you give.
198700**
198701******************************************************************************
198702*/
198703
198704/* #include "fts3Int.h" */
198705#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
198706
198707/* #include <string.h> */
198708/* #include <assert.h> */
198709
198710#ifndef SQLITE_AMALGAMATION
198711typedef sqlite3_int64 i64;
198712#endif
198713
198714/*
198715** Characters that may appear in the second argument to matchinfo().
198716*/
198717#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
198718#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
198719#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
198720#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
198721#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
198722#define FTS3_MATCHINFO_LCS 's' /* nCol values */
198723#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
198724#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
198725#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
198726
198727/*
198728** The default value for the second argument to matchinfo().
198729*/
198730#define FTS3_MATCHINFO_DEFAULT "pcx"
198731
198732
198733/*
198734** Used as an sqlite3Fts3ExprIterate() context when loading phrase doclists to
198735** Fts3Expr.aDoclist[]/nDoclist.
198736*/
198737typedef struct LoadDoclistCtx LoadDoclistCtx;
198738struct LoadDoclistCtx {
198739 Fts3Cursor *pCsr; /* FTS3 Cursor */
198740 int nPhrase; /* Number of phrases seen so far */
198741 int nToken; /* Number of tokens seen so far */
198742};
198743
198744/*
198745** The following types are used as part of the implementation of the
198746** fts3BestSnippet() routine.
198747*/
198748typedef struct SnippetIter SnippetIter;
198749typedef struct SnippetPhrase SnippetPhrase;
198750typedef struct SnippetFragment SnippetFragment;
198751
198752struct SnippetIter {
198753 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
198754 int iCol; /* Extract snippet from this column */
198755 int nSnippet; /* Requested snippet length (in tokens) */
198756 int nPhrase; /* Number of phrases in query */
198757 SnippetPhrase *aPhrase; /* Array of size nPhrase */
198758 int iCurrent; /* First token of current snippet */
198759};
198760
198761struct SnippetPhrase {
198762 int nToken; /* Number of tokens in phrase */
198763 char *pList; /* Pointer to start of phrase position list */
198764 i64 iHead; /* Next value in position list */
198765 char *pHead; /* Position list data following iHead */
198766 i64 iTail; /* Next value in trailing position list */
198767 char *pTail; /* Position list data following iTail */
198768};
198769
198770struct SnippetFragment {
198771 int iCol; /* Column snippet is extracted from */
198772 int iPos; /* Index of first token in snippet */
198773 u64 covered; /* Mask of query phrases covered */
198774 u64 hlmask; /* Mask of snippet terms to highlight */
198775};
198776
198777/*
198778** This type is used as an sqlite3Fts3ExprIterate() context object while
198779** accumulating the data returned by the matchinfo() function.
198780*/
198781typedef struct MatchInfo MatchInfo;
198782struct MatchInfo {
198783 Fts3Cursor *pCursor; /* FTS3 Cursor */
198784 int nCol; /* Number of columns in table */
198785 int nPhrase; /* Number of matchable phrases in query */
198786 sqlite3_int64 nDoc; /* Number of docs in database */
198787 char flag;
198788 u32 *aMatchinfo; /* Pre-allocated buffer */
198789};
198790
198791/*
198792** An instance of this structure is used to manage a pair of buffers, each
198793** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
198794** for details.
198795*/
198796struct MatchinfoBuffer {
198797 u8 aRef[3];
198798 int nElem;
198799 int bGlobal; /* Set if global data is loaded */
198800 char *zMatchinfo;
198801 u32 aMatchinfo[1];
198802};
198803
198804
198805/*
198806** The snippet() and offsets() functions both return text values. An instance
198807** of the following structure is used to accumulate those values while the
198808** functions are running. See fts3StringAppend() for details.
198809*/
198810typedef struct StrBuffer StrBuffer;
198811struct StrBuffer {
198812 char *z; /* Pointer to buffer containing string */
198813 int n; /* Length of z in bytes (excl. nul-term) */
198814 int nAlloc; /* Allocated size of buffer z in bytes */
198815};
198816
198817
198818/*************************************************************************
198819** Start of MatchinfoBuffer code.
198820*/
198821
198822/*
198823** Allocate a two-slot MatchinfoBuffer object.
198824*/
198825static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
198826 MatchinfoBuffer *pRet;
198827 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
198828 + sizeof(MatchinfoBuffer);
198829 sqlite3_int64 nStr = strlen(s: zMatchinfo);
198830
198831 pRet = sqlite3Fts3MallocZero(nByte: nByte + nStr+1);
198832 if( pRet ){
198833 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
198834 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
198835 + sizeof(u32)*((int)nElem+1);
198836 pRet->nElem = (int)nElem;
198837 pRet->zMatchinfo = ((char*)pRet) + nByte;
198838 memcpy(dest: pRet->zMatchinfo, src: zMatchinfo, n: nStr+1);
198839 pRet->aRef[0] = 1;
198840 }
198841
198842 return pRet;
198843}
198844
198845static void fts3MIBufferFree(void *p){
198846 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
198847
198848 assert( (u32*)p==&pBuf->aMatchinfo[1]
198849 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
198850 );
198851 if( (u32*)p==&pBuf->aMatchinfo[1] ){
198852 pBuf->aRef[1] = 0;
198853 }else{
198854 pBuf->aRef[2] = 0;
198855 }
198856
198857 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
198858 sqlite3_free(p: pBuf);
198859 }
198860}
198861
198862static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
198863 void (*xRet)(void*) = 0;
198864 u32 *aOut = 0;
198865
198866 if( p->aRef[1]==0 ){
198867 p->aRef[1] = 1;
198868 aOut = &p->aMatchinfo[1];
198869 xRet = fts3MIBufferFree;
198870 }
198871 else if( p->aRef[2]==0 ){
198872 p->aRef[2] = 1;
198873 aOut = &p->aMatchinfo[p->nElem+2];
198874 xRet = fts3MIBufferFree;
198875 }else{
198876 aOut = (u32*)sqlite3_malloc64(n: p->nElem * sizeof(u32));
198877 if( aOut ){
198878 xRet = sqlite3_free;
198879 if( p->bGlobal ) memcpy(dest: aOut, src: &p->aMatchinfo[1], n: p->nElem*sizeof(u32));
198880 }
198881 }
198882
198883 *paOut = aOut;
198884 return xRet;
198885}
198886
198887static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
198888 p->bGlobal = 1;
198889 memcpy(dest: &p->aMatchinfo[2+p->nElem], src: &p->aMatchinfo[1], n: p->nElem*sizeof(u32));
198890}
198891
198892/*
198893** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
198894*/
198895SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
198896 if( p ){
198897 assert( p->aRef[0]==1 );
198898 p->aRef[0] = 0;
198899 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
198900 sqlite3_free(p);
198901 }
198902 }
198903}
198904
198905/*
198906** End of MatchinfoBuffer code.
198907*************************************************************************/
198908
198909
198910/*
198911** This function is used to help iterate through a position-list. A position
198912** list is a list of unique integers, sorted from smallest to largest. Each
198913** element of the list is represented by an FTS3 varint that takes the value
198914** of the difference between the current element and the previous one plus
198915** two. For example, to store the position-list:
198916**
198917** 4 9 113
198918**
198919** the three varints:
198920**
198921** 6 7 106
198922**
198923** are encoded.
198924**
198925** When this function is called, *pp points to the start of an element of
198926** the list. *piPos contains the value of the previous entry in the list.
198927** After it returns, *piPos contains the value of the next element of the
198928** list and *pp is advanced to the following varint.
198929*/
198930static void fts3GetDeltaPosition(char **pp, i64 *piPos){
198931 int iVal;
198932 *pp += fts3GetVarint32(*pp, &iVal);
198933 *piPos += (iVal-2);
198934}
198935
198936/*
198937** Helper function for sqlite3Fts3ExprIterate() (see below).
198938*/
198939static int fts3ExprIterate2(
198940 Fts3Expr *pExpr, /* Expression to iterate phrases of */
198941 int *piPhrase, /* Pointer to phrase counter */
198942 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
198943 void *pCtx /* Second argument to pass to callback */
198944){
198945 int rc; /* Return code */
198946 int eType = pExpr->eType; /* Type of expression node pExpr */
198947
198948 if( eType!=FTSQUERY_PHRASE ){
198949 assert( pExpr->pLeft && pExpr->pRight );
198950 rc = fts3ExprIterate2(pExpr: pExpr->pLeft, piPhrase, x, pCtx);
198951 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
198952 rc = fts3ExprIterate2(pExpr: pExpr->pRight, piPhrase, x, pCtx);
198953 }
198954 }else{
198955 rc = x(pExpr, *piPhrase, pCtx);
198956 (*piPhrase)++;
198957 }
198958 return rc;
198959}
198960
198961/*
198962** Iterate through all phrase nodes in an FTS3 query, except those that
198963** are part of a sub-tree that is the right-hand-side of a NOT operator.
198964** For each phrase node found, the supplied callback function is invoked.
198965**
198966** If the callback function returns anything other than SQLITE_OK,
198967** the iteration is abandoned and the error code returned immediately.
198968** Otherwise, SQLITE_OK is returned after a callback has been made for
198969** all eligible phrase nodes.
198970*/
198971SQLITE_PRIVATE int sqlite3Fts3ExprIterate(
198972 Fts3Expr *pExpr, /* Expression to iterate phrases of */
198973 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
198974 void *pCtx /* Second argument to pass to callback */
198975){
198976 int iPhrase = 0; /* Variable used as the phrase counter */
198977 return fts3ExprIterate2(pExpr, piPhrase: &iPhrase, x, pCtx);
198978}
198979
198980/*
198981** This is an sqlite3Fts3ExprIterate() callback used while loading the
198982** doclists for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
198983** fts3ExprLoadDoclists().
198984*/
198985static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
198986 int rc = SQLITE_OK;
198987 Fts3Phrase *pPhrase = pExpr->pPhrase;
198988 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
198989
198990 UNUSED_PARAMETER(iPhrase);
198991
198992 p->nPhrase++;
198993 p->nToken += pPhrase->nToken;
198994
198995 return rc;
198996}
198997
198998/*
198999** Load the doclists for each phrase in the query associated with FTS3 cursor
199000** pCsr.
199001**
199002** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
199003** phrases in the expression (all phrases except those directly or
199004** indirectly descended from the right-hand-side of a NOT operator). If
199005** pnToken is not NULL, then it is set to the number of tokens in all
199006** matchable phrases of the expression.
199007*/
199008static int fts3ExprLoadDoclists(
199009 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
199010 int *pnPhrase, /* OUT: Number of phrases in query */
199011 int *pnToken /* OUT: Number of tokens in query */
199012){
199013 int rc; /* Return Code */
199014 LoadDoclistCtx sCtx = {0,0,0}; /* Context for sqlite3Fts3ExprIterate() */
199015 sCtx.pCsr = pCsr;
199016 rc = sqlite3Fts3ExprIterate(pExpr: pCsr->pExpr,x: fts3ExprLoadDoclistsCb,pCtx: (void*)&sCtx);
199017 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
199018 if( pnToken ) *pnToken = sCtx.nToken;
199019 return rc;
199020}
199021
199022static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
199023 (*(int *)ctx)++;
199024 pExpr->iPhrase = iPhrase;
199025 return SQLITE_OK;
199026}
199027static int fts3ExprPhraseCount(Fts3Expr *pExpr){
199028 int nPhrase = 0;
199029 (void)sqlite3Fts3ExprIterate(pExpr, x: fts3ExprPhraseCountCb, pCtx: (void *)&nPhrase);
199030 return nPhrase;
199031}
199032
199033/*
199034** Advance the position list iterator specified by the first two
199035** arguments so that it points to the first element with a value greater
199036** than or equal to parameter iNext.
199037*/
199038static void fts3SnippetAdvance(char **ppIter, i64 *piIter, int iNext){
199039 char *pIter = *ppIter;
199040 if( pIter ){
199041 i64 iIter = *piIter;
199042
199043 while( iIter<iNext ){
199044 if( 0==(*pIter & 0xFE) ){
199045 iIter = -1;
199046 pIter = 0;
199047 break;
199048 }
199049 fts3GetDeltaPosition(pp: &pIter, piPos: &iIter);
199050 }
199051
199052 *piIter = iIter;
199053 *ppIter = pIter;
199054 }
199055}
199056
199057/*
199058** Advance the snippet iterator to the next candidate snippet.
199059*/
199060static int fts3SnippetNextCandidate(SnippetIter *pIter){
199061 int i; /* Loop counter */
199062
199063 if( pIter->iCurrent<0 ){
199064 /* The SnippetIter object has just been initialized. The first snippet
199065 ** candidate always starts at offset 0 (even if this candidate has a
199066 ** score of 0.0).
199067 */
199068 pIter->iCurrent = 0;
199069
199070 /* Advance the 'head' iterator of each phrase to the first offset that
199071 ** is greater than or equal to (iNext+nSnippet).
199072 */
199073 for(i=0; i<pIter->nPhrase; i++){
199074 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
199075 fts3SnippetAdvance(ppIter: &pPhrase->pHead, piIter: &pPhrase->iHead, iNext: pIter->nSnippet);
199076 }
199077 }else{
199078 int iStart;
199079 int iEnd = 0x7FFFFFFF;
199080
199081 for(i=0; i<pIter->nPhrase; i++){
199082 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
199083 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
199084 iEnd = pPhrase->iHead;
199085 }
199086 }
199087 if( iEnd==0x7FFFFFFF ){
199088 return 1;
199089 }
199090
199091 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
199092 for(i=0; i<pIter->nPhrase; i++){
199093 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
199094 fts3SnippetAdvance(ppIter: &pPhrase->pHead, piIter: &pPhrase->iHead, iNext: iEnd+1);
199095 fts3SnippetAdvance(ppIter: &pPhrase->pTail, piIter: &pPhrase->iTail, iNext: iStart);
199096 }
199097 }
199098
199099 return 0;
199100}
199101
199102/*
199103** Retrieve information about the current candidate snippet of snippet
199104** iterator pIter.
199105*/
199106static void fts3SnippetDetails(
199107 SnippetIter *pIter, /* Snippet iterator */
199108 u64 mCovered, /* Bitmask of phrases already covered */
199109 int *piToken, /* OUT: First token of proposed snippet */
199110 int *piScore, /* OUT: "Score" for this snippet */
199111 u64 *pmCover, /* OUT: Bitmask of phrases covered */
199112 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
199113){
199114 int iStart = pIter->iCurrent; /* First token of snippet */
199115 int iScore = 0; /* Score of this snippet */
199116 int i; /* Loop counter */
199117 u64 mCover = 0; /* Mask of phrases covered by this snippet */
199118 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
199119
199120 for(i=0; i<pIter->nPhrase; i++){
199121 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
199122 if( pPhrase->pTail ){
199123 char *pCsr = pPhrase->pTail;
199124 i64 iCsr = pPhrase->iTail;
199125
199126 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
199127 int j;
199128 u64 mPhrase = (u64)1 << (i%64);
199129 u64 mPos = (u64)1 << (iCsr - iStart);
199130 assert( iCsr>=iStart && (iCsr - iStart)<=64 );
199131 assert( i>=0 );
199132 if( (mCover|mCovered)&mPhrase ){
199133 iScore++;
199134 }else{
199135 iScore += 1000;
199136 }
199137 mCover |= mPhrase;
199138
199139 for(j=0; j<pPhrase->nToken; j++){
199140 mHighlight |= (mPos>>j);
199141 }
199142
199143 if( 0==(*pCsr & 0x0FE) ) break;
199144 fts3GetDeltaPosition(pp: &pCsr, piPos: &iCsr);
199145 }
199146 }
199147 }
199148
199149 /* Set the output variables before returning. */
199150 *piToken = iStart;
199151 *piScore = iScore;
199152 *pmCover = mCover;
199153 *pmHighlight = mHighlight;
199154}
199155
199156/*
199157** This function is an sqlite3Fts3ExprIterate() callback used by
199158** fts3BestSnippet(). Each invocation populates an element of the
199159** SnippetIter.aPhrase[] array.
199160*/
199161static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
199162 SnippetIter *p = (SnippetIter *)ctx;
199163 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
199164 char *pCsr;
199165 int rc;
199166
199167 pPhrase->nToken = pExpr->pPhrase->nToken;
199168 rc = sqlite3Fts3EvalPhrasePoslist(pCsr: p->pCsr, pExpr, iCol: p->iCol, ppOut: &pCsr);
199169 assert( rc==SQLITE_OK || pCsr==0 );
199170 if( pCsr ){
199171 i64 iFirst = 0;
199172 pPhrase->pList = pCsr;
199173 fts3GetDeltaPosition(pp: &pCsr, piPos: &iFirst);
199174 if( iFirst<0 ){
199175 rc = FTS_CORRUPT_VTAB;
199176 }else{
199177 pPhrase->pHead = pCsr;
199178 pPhrase->pTail = pCsr;
199179 pPhrase->iHead = iFirst;
199180 pPhrase->iTail = iFirst;
199181 }
199182 }else{
199183 assert( rc!=SQLITE_OK || (
199184 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
199185 ));
199186 }
199187
199188 return rc;
199189}
199190
199191/*
199192** Select the fragment of text consisting of nFragment contiguous tokens
199193** from column iCol that represent the "best" snippet. The best snippet
199194** is the snippet with the highest score, where scores are calculated
199195** by adding:
199196**
199197** (a) +1 point for each occurrence of a matchable phrase in the snippet.
199198**
199199** (b) +1000 points for the first occurrence of each matchable phrase in
199200** the snippet for which the corresponding mCovered bit is not set.
199201**
199202** The selected snippet parameters are stored in structure *pFragment before
199203** returning. The score of the selected snippet is stored in *piScore
199204** before returning.
199205*/
199206static int fts3BestSnippet(
199207 int nSnippet, /* Desired snippet length */
199208 Fts3Cursor *pCsr, /* Cursor to create snippet for */
199209 int iCol, /* Index of column to create snippet from */
199210 u64 mCovered, /* Mask of phrases already covered */
199211 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
199212 SnippetFragment *pFragment, /* OUT: Best snippet found */
199213 int *piScore /* OUT: Score of snippet pFragment */
199214){
199215 int rc; /* Return Code */
199216 int nList; /* Number of phrases in expression */
199217 SnippetIter sIter; /* Iterates through snippet candidates */
199218 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
199219 int iBestScore = -1; /* Best snippet score found so far */
199220 int i; /* Loop counter */
199221
199222 memset(s: &sIter, c: 0, n: sizeof(sIter));
199223
199224 /* Iterate through the phrases in the expression to count them. The same
199225 ** callback makes sure the doclists are loaded for each phrase.
199226 */
199227 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: &nList, pnToken: 0);
199228 if( rc!=SQLITE_OK ){
199229 return rc;
199230 }
199231
199232 /* Now that it is known how many phrases there are, allocate and zero
199233 ** the required space using malloc().
199234 */
199235 nByte = sizeof(SnippetPhrase) * nList;
199236 sIter.aPhrase = (SnippetPhrase *)sqlite3Fts3MallocZero(nByte);
199237 if( !sIter.aPhrase ){
199238 return SQLITE_NOMEM;
199239 }
199240
199241 /* Initialize the contents of the SnippetIter object. Then iterate through
199242 ** the set of phrases in the expression to populate the aPhrase[] array.
199243 */
199244 sIter.pCsr = pCsr;
199245 sIter.iCol = iCol;
199246 sIter.nSnippet = nSnippet;
199247 sIter.nPhrase = nList;
199248 sIter.iCurrent = -1;
199249 rc = sqlite3Fts3ExprIterate(
199250 pExpr: pCsr->pExpr, x: fts3SnippetFindPositions, pCtx: (void*)&sIter
199251 );
199252 if( rc==SQLITE_OK ){
199253
199254 /* Set the *pmSeen output variable. */
199255 for(i=0; i<nList; i++){
199256 if( sIter.aPhrase[i].pHead ){
199257 *pmSeen |= (u64)1 << (i%64);
199258 }
199259 }
199260
199261 /* Loop through all candidate snippets. Store the best snippet in
199262 ** *pFragment. Store its associated 'score' in iBestScore.
199263 */
199264 pFragment->iCol = iCol;
199265 while( !fts3SnippetNextCandidate(pIter: &sIter) ){
199266 int iPos;
199267 int iScore;
199268 u64 mCover;
199269 u64 mHighlite;
199270 fts3SnippetDetails(pIter: &sIter, mCovered, piToken: &iPos, piScore: &iScore, pmCover: &mCover,pmHighlight: &mHighlite);
199271 assert( iScore>=0 );
199272 if( iScore>iBestScore ){
199273 pFragment->iPos = iPos;
199274 pFragment->hlmask = mHighlite;
199275 pFragment->covered = mCover;
199276 iBestScore = iScore;
199277 }
199278 }
199279
199280 *piScore = iBestScore;
199281 }
199282 sqlite3_free(p: sIter.aPhrase);
199283 return rc;
199284}
199285
199286
199287/*
199288** Append a string to the string-buffer passed as the first argument.
199289**
199290** If nAppend is negative, then the length of the string zAppend is
199291** determined using strlen().
199292*/
199293static int fts3StringAppend(
199294 StrBuffer *pStr, /* Buffer to append to */
199295 const char *zAppend, /* Pointer to data to append to buffer */
199296 int nAppend /* Size of zAppend in bytes (or -1) */
199297){
199298 if( nAppend<0 ){
199299 nAppend = (int)strlen(s: zAppend);
199300 }
199301
199302 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
199303 ** to grow the buffer until so that it is big enough to accomadate the
199304 ** appended data.
199305 */
199306 if( pStr->n+nAppend+1>=pStr->nAlloc ){
199307 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
199308 char *zNew = sqlite3_realloc64(pOld: pStr->z, n: nAlloc);
199309 if( !zNew ){
199310 return SQLITE_NOMEM;
199311 }
199312 pStr->z = zNew;
199313 pStr->nAlloc = nAlloc;
199314 }
199315 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
199316
199317 /* Append the data to the string buffer. */
199318 memcpy(dest: &pStr->z[pStr->n], src: zAppend, n: nAppend);
199319 pStr->n += nAppend;
199320 pStr->z[pStr->n] = '\0';
199321
199322 return SQLITE_OK;
199323}
199324
199325/*
199326** The fts3BestSnippet() function often selects snippets that end with a
199327** query term. That is, the final term of the snippet is always a term
199328** that requires highlighting. For example, if 'X' is a highlighted term
199329** and '.' is a non-highlighted term, BestSnippet() may select:
199330**
199331** ........X.....X
199332**
199333** This function "shifts" the beginning of the snippet forward in the
199334** document so that there are approximately the same number of
199335** non-highlighted terms to the right of the final highlighted term as there
199336** are to the left of the first highlighted term. For example, to this:
199337**
199338** ....X.....X....
199339**
199340** This is done as part of extracting the snippet text, not when selecting
199341** the snippet. Snippet selection is done based on doclists only, so there
199342** is no way for fts3BestSnippet() to know whether or not the document
199343** actually contains terms that follow the final highlighted term.
199344*/
199345static int fts3SnippetShift(
199346 Fts3Table *pTab, /* FTS3 table snippet comes from */
199347 int iLangid, /* Language id to use in tokenizing */
199348 int nSnippet, /* Number of tokens desired for snippet */
199349 const char *zDoc, /* Document text to extract snippet from */
199350 int nDoc, /* Size of buffer zDoc in bytes */
199351 int *piPos, /* IN/OUT: First token of snippet */
199352 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
199353){
199354 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
199355
199356 if( hlmask ){
199357 int nLeft; /* Tokens to the left of first highlight */
199358 int nRight; /* Tokens to the right of last highlight */
199359 int nDesired; /* Ideal number of tokens to shift forward */
199360
199361 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
199362 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
199363 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 );
199364 nDesired = (nLeft-nRight)/2;
199365
199366 /* Ideally, the start of the snippet should be pushed forward in the
199367 ** document nDesired tokens. This block checks if there are actually
199368 ** nDesired tokens to the right of the snippet. If so, *piPos and
199369 ** *pHlMask are updated to shift the snippet nDesired tokens to the
199370 ** right. Otherwise, the snippet is shifted by the number of tokens
199371 ** available.
199372 */
199373 if( nDesired>0 ){
199374 int nShift; /* Number of tokens to shift snippet by */
199375 int iCurrent = 0; /* Token counter */
199376 int rc; /* Return Code */
199377 sqlite3_tokenizer_module *pMod;
199378 sqlite3_tokenizer_cursor *pC;
199379 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
199380
199381 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
199382 ** or more tokens in zDoc/nDoc.
199383 */
199384 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pTab->pTokenizer, iLangid, z: zDoc, n: nDoc, ppCsr: &pC);
199385 if( rc!=SQLITE_OK ){
199386 return rc;
199387 }
199388 while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
199389 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
199390 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
199391 }
199392 pMod->xClose(pC);
199393 if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
199394
199395 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
199396 assert( nShift<=nDesired );
199397 if( nShift>0 ){
199398 *piPos += nShift;
199399 *pHlmask = hlmask >> nShift;
199400 }
199401 }
199402 }
199403 return SQLITE_OK;
199404}
199405
199406/*
199407** Extract the snippet text for fragment pFragment from cursor pCsr and
199408** append it to string buffer pOut.
199409*/
199410static int fts3SnippetText(
199411 Fts3Cursor *pCsr, /* FTS3 Cursor */
199412 SnippetFragment *pFragment, /* Snippet to extract */
199413 int iFragment, /* Fragment number */
199414 int isLast, /* True for final fragment in snippet */
199415 int nSnippet, /* Number of tokens in extracted snippet */
199416 const char *zOpen, /* String inserted before highlighted term */
199417 const char *zClose, /* String inserted after highlighted term */
199418 const char *zEllipsis, /* String inserted between snippets */
199419 StrBuffer *pOut /* Write output here */
199420){
199421 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
199422 int rc; /* Return code */
199423 const char *zDoc; /* Document text to extract snippet from */
199424 int nDoc; /* Size of zDoc in bytes */
199425 int iCurrent = 0; /* Current token number of document */
199426 int iEnd = 0; /* Byte offset of end of current token */
199427 int isShiftDone = 0; /* True after snippet is shifted */
199428 int iPos = pFragment->iPos; /* First token of snippet */
199429 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
199430 int iCol = pFragment->iCol+1; /* Query column to extract text from */
199431 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
199432 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
199433
199434 zDoc = (const char *)sqlite3_column_text(pStmt: pCsr->pStmt, i: iCol);
199435 if( zDoc==0 ){
199436 if( sqlite3_column_type(pStmt: pCsr->pStmt, i: iCol)!=SQLITE_NULL ){
199437 return SQLITE_NOMEM;
199438 }
199439 return SQLITE_OK;
199440 }
199441 nDoc = sqlite3_column_bytes(pStmt: pCsr->pStmt, i: iCol);
199442
199443 /* Open a token cursor on the document. */
199444 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
199445 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pTab->pTokenizer, iLangid: pCsr->iLangid, z: zDoc,n: nDoc,ppCsr: &pC);
199446 if( rc!=SQLITE_OK ){
199447 return rc;
199448 }
199449
199450 while( rc==SQLITE_OK ){
199451 const char *ZDUMMY; /* Dummy argument used with tokenizer */
199452 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
199453 int iBegin = 0; /* Offset in zDoc of start of token */
199454 int iFin = 0; /* Offset in zDoc of end of token */
199455 int isHighlight = 0; /* True for highlighted terms */
199456
199457 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
199458 ** in the FTS code the variable that the third argument to xNext points to
199459 ** is initialized to zero before the first (*but not necessarily
199460 ** subsequent*) call to xNext(). This is done for a particular application
199461 ** that needs to know whether or not the tokenizer is being used for
199462 ** snippet generation or for some other purpose.
199463 **
199464 ** Extreme care is required when writing code to depend on this
199465 ** initialization. It is not a documented part of the tokenizer interface.
199466 ** If a tokenizer is used directly by any code outside of FTS, this
199467 ** convention might not be respected. */
199468 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
199469 if( rc!=SQLITE_OK ){
199470 if( rc==SQLITE_DONE ){
199471 /* Special case - the last token of the snippet is also the last token
199472 ** of the column. Append any punctuation that occurred between the end
199473 ** of the previous token and the end of the document to the output.
199474 ** Then break out of the loop. */
199475 rc = fts3StringAppend(pStr: pOut, zAppend: &zDoc[iEnd], nAppend: -1);
199476 }
199477 break;
199478 }
199479 if( iCurrent<iPos ){ continue; }
199480
199481 if( !isShiftDone ){
199482 int n = nDoc - iBegin;
199483 rc = fts3SnippetShift(
199484 pTab, iLangid: pCsr->iLangid, nSnippet, zDoc: &zDoc[iBegin], nDoc: n, piPos: &iPos, pHlmask: &hlmask
199485 );
199486 isShiftDone = 1;
199487
199488 /* Now that the shift has been done, check if the initial "..." are
199489 ** required. They are required if (a) this is not the first fragment,
199490 ** or (b) this fragment does not begin at position 0 of its column.
199491 */
199492 if( rc==SQLITE_OK ){
199493 if( iPos>0 || iFragment>0 ){
199494 rc = fts3StringAppend(pStr: pOut, zAppend: zEllipsis, nAppend: -1);
199495 }else if( iBegin ){
199496 rc = fts3StringAppend(pStr: pOut, zAppend: zDoc, nAppend: iBegin);
199497 }
199498 }
199499 if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
199500 }
199501
199502 if( iCurrent>=(iPos+nSnippet) ){
199503 if( isLast ){
199504 rc = fts3StringAppend(pStr: pOut, zAppend: zEllipsis, nAppend: -1);
199505 }
199506 break;
199507 }
199508
199509 /* Set isHighlight to true if this term should be highlighted. */
199510 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
199511
199512 if( iCurrent>iPos ) rc = fts3StringAppend(pStr: pOut, zAppend: &zDoc[iEnd], nAppend: iBegin-iEnd);
199513 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pStr: pOut, zAppend: zOpen, nAppend: -1);
199514 if( rc==SQLITE_OK ) rc = fts3StringAppend(pStr: pOut, zAppend: &zDoc[iBegin], nAppend: iFin-iBegin);
199515 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pStr: pOut, zAppend: zClose, nAppend: -1);
199516
199517 iEnd = iFin;
199518 }
199519
199520 pMod->xClose(pC);
199521 return rc;
199522}
199523
199524
199525/*
199526** This function is used to count the entries in a column-list (a
199527** delta-encoded list of term offsets within a single column of a single
199528** row). When this function is called, *ppCollist should point to the
199529** beginning of the first varint in the column-list (the varint that
199530** contains the position of the first matching term in the column data).
199531** Before returning, *ppCollist is set to point to the first byte after
199532** the last varint in the column-list (either the 0x00 signifying the end
199533** of the position-list, or the 0x01 that precedes the column number of
199534** the next column in the position-list).
199535**
199536** The number of elements in the column-list is returned.
199537*/
199538static int fts3ColumnlistCount(char **ppCollist){
199539 char *pEnd = *ppCollist;
199540 char c = 0;
199541 int nEntry = 0;
199542
199543 /* A column-list is terminated by either a 0x01 or 0x00. */
199544 while( 0xFE & (*pEnd | c) ){
199545 c = *pEnd++ & 0x80;
199546 if( !c ) nEntry++;
199547 }
199548
199549 *ppCollist = pEnd;
199550 return nEntry;
199551}
199552
199553/*
199554** This function gathers 'y' or 'b' data for a single phrase.
199555*/
199556static int fts3ExprLHits(
199557 Fts3Expr *pExpr, /* Phrase expression node */
199558 MatchInfo *p /* Matchinfo context */
199559){
199560 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
199561 int iStart;
199562 Fts3Phrase *pPhrase = pExpr->pPhrase;
199563 char *pIter = pPhrase->doclist.pList;
199564 int iCol = 0;
199565
199566 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
199567 if( p->flag==FTS3_MATCHINFO_LHITS ){
199568 iStart = pExpr->iPhrase * p->nCol;
199569 }else{
199570 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
199571 }
199572
199573 if( pIter ) while( 1 ){
199574 int nHit = fts3ColumnlistCount(ppCollist: &pIter);
199575 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
199576 if( p->flag==FTS3_MATCHINFO_LHITS ){
199577 p->aMatchinfo[iStart + iCol] = (u32)nHit;
199578 }else if( nHit ){
199579 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
199580 }
199581 }
199582 assert( *pIter==0x00 || *pIter==0x01 );
199583 if( *pIter!=0x01 ) break;
199584 pIter++;
199585 pIter += fts3GetVarint32(pIter, &iCol);
199586 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB;
199587 }
199588 return SQLITE_OK;
199589}
199590
199591/*
199592** Gather the results for matchinfo directives 'y' and 'b'.
199593*/
199594static int fts3ExprLHitGather(
199595 Fts3Expr *pExpr,
199596 MatchInfo *p
199597){
199598 int rc = SQLITE_OK;
199599 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
199600 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
199601 if( pExpr->pLeft ){
199602 rc = fts3ExprLHitGather(pExpr: pExpr->pLeft, p);
199603 if( rc==SQLITE_OK ) rc = fts3ExprLHitGather(pExpr: pExpr->pRight, p);
199604 }else{
199605 rc = fts3ExprLHits(pExpr, p);
199606 }
199607 }
199608 return rc;
199609}
199610
199611/*
199612** sqlite3Fts3ExprIterate() callback used to collect the "global" matchinfo
199613** stats for a single query.
199614**
199615** sqlite3Fts3ExprIterate() callback to load the 'global' elements of a
199616** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
199617** of the matchinfo array that are constant for all rows returned by the
199618** current query.
199619**
199620** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
199621** function populates Matchinfo.aMatchinfo[] as follows:
199622**
199623** for(iCol=0; iCol<nCol; iCol++){
199624** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
199625** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
199626** }
199627**
199628** where X is the number of matches for phrase iPhrase is column iCol of all
199629** rows of the table. Y is the number of rows for which column iCol contains
199630** at least one instance of phrase iPhrase.
199631**
199632** If the phrase pExpr consists entirely of deferred tokens, then all X and
199633** Y values are set to nDoc, where nDoc is the number of documents in the
199634** file system. This is done because the full-text index doclist is required
199635** to calculate these values properly, and the full-text index doclist is
199636** not available for deferred tokens.
199637*/
199638static int fts3ExprGlobalHitsCb(
199639 Fts3Expr *pExpr, /* Phrase expression node */
199640 int iPhrase, /* Phrase number (numbered from zero) */
199641 void *pCtx /* Pointer to MatchInfo structure */
199642){
199643 MatchInfo *p = (MatchInfo *)pCtx;
199644 return sqlite3Fts3EvalPhraseStats(
199645 pCsr: p->pCursor, pExpr, aiOut: &p->aMatchinfo[3*iPhrase*p->nCol]
199646 );
199647}
199648
199649/*
199650** sqlite3Fts3ExprIterate() callback used to collect the "local" part of the
199651** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
199652** array that are different for each row returned by the query.
199653*/
199654static int fts3ExprLocalHitsCb(
199655 Fts3Expr *pExpr, /* Phrase expression node */
199656 int iPhrase, /* Phrase number */
199657 void *pCtx /* Pointer to MatchInfo structure */
199658){
199659 int rc = SQLITE_OK;
199660 MatchInfo *p = (MatchInfo *)pCtx;
199661 int iStart = iPhrase * p->nCol * 3;
199662 int i;
199663
199664 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
199665 char *pCsr;
199666 rc = sqlite3Fts3EvalPhrasePoslist(pCsr: p->pCursor, pExpr, iCol: i, ppOut: &pCsr);
199667 if( pCsr ){
199668 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(ppCollist: &pCsr);
199669 }else{
199670 p->aMatchinfo[iStart+i*3] = 0;
199671 }
199672 }
199673
199674 return rc;
199675}
199676
199677static int fts3MatchinfoCheck(
199678 Fts3Table *pTab,
199679 char cArg,
199680 char **pzErr
199681){
199682 if( (cArg==FTS3_MATCHINFO_NPHRASE)
199683 || (cArg==FTS3_MATCHINFO_NCOL)
199684 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
199685 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
199686 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
199687 || (cArg==FTS3_MATCHINFO_LCS)
199688 || (cArg==FTS3_MATCHINFO_HITS)
199689 || (cArg==FTS3_MATCHINFO_LHITS)
199690 || (cArg==FTS3_MATCHINFO_LHITS_BM)
199691 ){
199692 return SQLITE_OK;
199693 }
199694 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized matchinfo request: %c", cArg);
199695 return SQLITE_ERROR;
199696}
199697
199698static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
199699 size_t nVal; /* Number of integers output by cArg */
199700
199701 switch( cArg ){
199702 case FTS3_MATCHINFO_NDOC:
199703 case FTS3_MATCHINFO_NPHRASE:
199704 case FTS3_MATCHINFO_NCOL:
199705 nVal = 1;
199706 break;
199707
199708 case FTS3_MATCHINFO_AVGLENGTH:
199709 case FTS3_MATCHINFO_LENGTH:
199710 case FTS3_MATCHINFO_LCS:
199711 nVal = pInfo->nCol;
199712 break;
199713
199714 case FTS3_MATCHINFO_LHITS:
199715 nVal = pInfo->nCol * pInfo->nPhrase;
199716 break;
199717
199718 case FTS3_MATCHINFO_LHITS_BM:
199719 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
199720 break;
199721
199722 default:
199723 assert( cArg==FTS3_MATCHINFO_HITS );
199724 nVal = pInfo->nCol * pInfo->nPhrase * 3;
199725 break;
199726 }
199727
199728 return nVal;
199729}
199730
199731static int fts3MatchinfoSelectDoctotal(
199732 Fts3Table *pTab,
199733 sqlite3_stmt **ppStmt,
199734 sqlite3_int64 *pnDoc,
199735 const char **paLen,
199736 const char **ppEnd
199737){
199738 sqlite3_stmt *pStmt;
199739 const char *a;
199740 const char *pEnd;
199741 sqlite3_int64 nDoc;
199742 int n;
199743
199744
199745 if( !*ppStmt ){
199746 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
199747 if( rc!=SQLITE_OK ) return rc;
199748 }
199749 pStmt = *ppStmt;
199750 assert( sqlite3_data_count(pStmt)==1 );
199751
199752 n = sqlite3_column_bytes(pStmt, i: 0);
199753 a = sqlite3_column_blob(pStmt, i: 0);
199754 if( a==0 ){
199755 return FTS_CORRUPT_VTAB;
199756 }
199757 pEnd = a + n;
199758 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nDoc);
199759 if( nDoc<=0 || a>pEnd ){
199760 return FTS_CORRUPT_VTAB;
199761 }
199762 *pnDoc = nDoc;
199763
199764 if( paLen ) *paLen = a;
199765 if( ppEnd ) *ppEnd = pEnd;
199766 return SQLITE_OK;
199767}
199768
199769/*
199770** An instance of the following structure is used to store state while
199771** iterating through a multi-column position-list corresponding to the
199772** hits for a single phrase on a single row in order to calculate the
199773** values for a matchinfo() FTS3_MATCHINFO_LCS request.
199774*/
199775typedef struct LcsIterator LcsIterator;
199776struct LcsIterator {
199777 Fts3Expr *pExpr; /* Pointer to phrase expression */
199778 int iPosOffset; /* Tokens count up to end of this phrase */
199779 char *pRead; /* Cursor used to iterate through aDoclist */
199780 int iPos; /* Current position */
199781};
199782
199783/*
199784** If LcsIterator.iCol is set to the following value, the iterator has
199785** finished iterating through all offsets for all columns.
199786*/
199787#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
199788
199789static int fts3MatchinfoLcsCb(
199790 Fts3Expr *pExpr, /* Phrase expression node */
199791 int iPhrase, /* Phrase number (numbered from zero) */
199792 void *pCtx /* Pointer to MatchInfo structure */
199793){
199794 LcsIterator *aIter = (LcsIterator *)pCtx;
199795 aIter[iPhrase].pExpr = pExpr;
199796 return SQLITE_OK;
199797}
199798
199799/*
199800** Advance the iterator passed as an argument to the next position. Return
199801** 1 if the iterator is at EOF or if it now points to the start of the
199802** position list for the next column.
199803*/
199804static int fts3LcsIteratorAdvance(LcsIterator *pIter){
199805 char *pRead;
199806 sqlite3_int64 iRead;
199807 int rc = 0;
199808
199809 if( NEVER(pIter==0) ) return 1;
199810 pRead = pIter->pRead;
199811 pRead += sqlite3Fts3GetVarint(pBuf: pRead, v: &iRead);
199812 if( iRead==0 || iRead==1 ){
199813 pRead = 0;
199814 rc = 1;
199815 }else{
199816 pIter->iPos += (int)(iRead-2);
199817 }
199818
199819 pIter->pRead = pRead;
199820 return rc;
199821}
199822
199823/*
199824** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
199825**
199826** If the call is successful, the longest-common-substring lengths for each
199827** column are written into the first nCol elements of the pInfo->aMatchinfo[]
199828** array before returning. SQLITE_OK is returned in this case.
199829**
199830** Otherwise, if an error occurs, an SQLite error code is returned and the
199831** data written to the first nCol elements of pInfo->aMatchinfo[] is
199832** undefined.
199833*/
199834static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
199835 LcsIterator *aIter;
199836 int i;
199837 int iCol;
199838 int nToken = 0;
199839 int rc = SQLITE_OK;
199840
199841 /* Allocate and populate the array of LcsIterator objects. The array
199842 ** contains one element for each matchable phrase in the query.
199843 **/
199844 aIter = sqlite3Fts3MallocZero(nByte: sizeof(LcsIterator) * pCsr->nPhrase);
199845 if( !aIter ) return SQLITE_NOMEM;
199846 (void)sqlite3Fts3ExprIterate(pExpr: pCsr->pExpr, x: fts3MatchinfoLcsCb, pCtx: (void*)aIter);
199847
199848 for(i=0; i<pInfo->nPhrase; i++){
199849 LcsIterator *pIter = &aIter[i];
199850 nToken -= pIter->pExpr->pPhrase->nToken;
199851 pIter->iPosOffset = nToken;
199852 }
199853
199854 for(iCol=0; iCol<pInfo->nCol; iCol++){
199855 int nLcs = 0; /* LCS value for this column */
199856 int nLive = 0; /* Number of iterators in aIter not at EOF */
199857
199858 for(i=0; i<pInfo->nPhrase; i++){
199859 LcsIterator *pIt = &aIter[i];
199860 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pExpr: pIt->pExpr, iCol, ppOut: &pIt->pRead);
199861 if( rc!=SQLITE_OK ) goto matchinfo_lcs_out;
199862 if( pIt->pRead ){
199863 pIt->iPos = pIt->iPosOffset;
199864 fts3LcsIteratorAdvance(pIter: pIt);
199865 if( pIt->pRead==0 ){
199866 rc = FTS_CORRUPT_VTAB;
199867 goto matchinfo_lcs_out;
199868 }
199869 nLive++;
199870 }
199871 }
199872
199873 while( nLive>0 ){
199874 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
199875 int nThisLcs = 0; /* LCS for the current iterator positions */
199876
199877 for(i=0; i<pInfo->nPhrase; i++){
199878 LcsIterator *pIter = &aIter[i];
199879 if( pIter->pRead==0 ){
199880 /* This iterator is already at EOF for this column. */
199881 nThisLcs = 0;
199882 }else{
199883 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
199884 pAdv = pIter;
199885 }
199886 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
199887 nThisLcs++;
199888 }else{
199889 nThisLcs = 1;
199890 }
199891 if( nThisLcs>nLcs ) nLcs = nThisLcs;
199892 }
199893 }
199894 if( fts3LcsIteratorAdvance(pIter: pAdv) ) nLive--;
199895 }
199896
199897 pInfo->aMatchinfo[iCol] = nLcs;
199898 }
199899
199900 matchinfo_lcs_out:
199901 sqlite3_free(p: aIter);
199902 return rc;
199903}
199904
199905/*
199906** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
199907** be returned by the matchinfo() function. Argument zArg contains the
199908** format string passed as the second argument to matchinfo (or the
199909** default value "pcx" if no second argument was specified). The format
199910** string has already been validated and the pInfo->aMatchinfo[] array
199911** is guaranteed to be large enough for the output.
199912**
199913** If bGlobal is true, then populate all fields of the matchinfo() output.
199914** If it is false, then assume that those fields that do not change between
199915** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
199916** have already been populated.
199917**
199918** Return SQLITE_OK if successful, or an SQLite error code if an error
199919** occurs. If a value other than SQLITE_OK is returned, the state the
199920** pInfo->aMatchinfo[] buffer is left in is undefined.
199921*/
199922static int fts3MatchinfoValues(
199923 Fts3Cursor *pCsr, /* FTS3 cursor object */
199924 int bGlobal, /* True to grab the global stats */
199925 MatchInfo *pInfo, /* Matchinfo context object */
199926 const char *zArg /* Matchinfo format string */
199927){
199928 int rc = SQLITE_OK;
199929 int i;
199930 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
199931 sqlite3_stmt *pSelect = 0;
199932
199933 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
199934 pInfo->flag = zArg[i];
199935 switch( zArg[i] ){
199936 case FTS3_MATCHINFO_NPHRASE:
199937 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
199938 break;
199939
199940 case FTS3_MATCHINFO_NCOL:
199941 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
199942 break;
199943
199944 case FTS3_MATCHINFO_NDOC:
199945 if( bGlobal ){
199946 sqlite3_int64 nDoc = 0;
199947 rc = fts3MatchinfoSelectDoctotal(pTab, ppStmt: &pSelect, pnDoc: &nDoc, paLen: 0, ppEnd: 0);
199948 pInfo->aMatchinfo[0] = (u32)nDoc;
199949 }
199950 break;
199951
199952 case FTS3_MATCHINFO_AVGLENGTH:
199953 if( bGlobal ){
199954 sqlite3_int64 nDoc; /* Number of rows in table */
199955 const char *a; /* Aggregate column length array */
199956 const char *pEnd; /* First byte past end of length array */
199957
199958 rc = fts3MatchinfoSelectDoctotal(pTab, ppStmt: &pSelect, pnDoc: &nDoc, paLen: &a, ppEnd: &pEnd);
199959 if( rc==SQLITE_OK ){
199960 int iCol;
199961 for(iCol=0; iCol<pInfo->nCol; iCol++){
199962 u32 iVal;
199963 sqlite3_int64 nToken;
199964 a += sqlite3Fts3GetVarint(pBuf: a, v: &nToken);
199965 if( a>pEnd ){
199966 rc = SQLITE_CORRUPT_VTAB;
199967 break;
199968 }
199969 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
199970 pInfo->aMatchinfo[iCol] = iVal;
199971 }
199972 }
199973 }
199974 break;
199975
199976 case FTS3_MATCHINFO_LENGTH: {
199977 sqlite3_stmt *pSelectDocsize = 0;
199978 rc = sqlite3Fts3SelectDocsize(pTab, iDocid: pCsr->iPrevId, ppStmt: &pSelectDocsize);
199979 if( rc==SQLITE_OK ){
199980 int iCol;
199981 const char *a = sqlite3_column_blob(pStmt: pSelectDocsize, i: 0);
199982 const char *pEnd = a + sqlite3_column_bytes(pStmt: pSelectDocsize, i: 0);
199983 for(iCol=0; iCol<pInfo->nCol; iCol++){
199984 sqlite3_int64 nToken;
199985 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nToken);
199986 if( a>pEnd ){
199987 rc = SQLITE_CORRUPT_VTAB;
199988 break;
199989 }
199990 pInfo->aMatchinfo[iCol] = (u32)nToken;
199991 }
199992 }
199993 sqlite3_reset(pStmt: pSelectDocsize);
199994 break;
199995 }
199996
199997 case FTS3_MATCHINFO_LCS:
199998 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: 0, pnToken: 0);
199999 if( rc==SQLITE_OK ){
200000 rc = fts3MatchinfoLcs(pCsr, pInfo);
200001 }
200002 break;
200003
200004 case FTS3_MATCHINFO_LHITS_BM:
200005 case FTS3_MATCHINFO_LHITS: {
200006 size_t nZero = fts3MatchinfoSize(pInfo, cArg: zArg[i]) * sizeof(u32);
200007 memset(s: pInfo->aMatchinfo, c: 0, n: nZero);
200008 rc = fts3ExprLHitGather(pExpr: pCsr->pExpr, p: pInfo);
200009 break;
200010 }
200011
200012 default: {
200013 Fts3Expr *pExpr;
200014 assert( zArg[i]==FTS3_MATCHINFO_HITS );
200015 pExpr = pCsr->pExpr;
200016 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: 0, pnToken: 0);
200017 if( rc!=SQLITE_OK ) break;
200018 if( bGlobal ){
200019 if( pCsr->pDeferred ){
200020 rc = fts3MatchinfoSelectDoctotal(pTab, ppStmt: &pSelect, pnDoc: &pInfo->nDoc,paLen: 0,ppEnd: 0);
200021 if( rc!=SQLITE_OK ) break;
200022 }
200023 rc = sqlite3Fts3ExprIterate(pExpr, x: fts3ExprGlobalHitsCb,pCtx: (void*)pInfo);
200024 sqlite3Fts3EvalTestDeferred(pCsr, pRc: &rc);
200025 if( rc!=SQLITE_OK ) break;
200026 }
200027 (void)sqlite3Fts3ExprIterate(pExpr, x: fts3ExprLocalHitsCb,pCtx: (void*)pInfo);
200028 break;
200029 }
200030 }
200031
200032 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, cArg: zArg[i]);
200033 }
200034
200035 sqlite3_reset(pStmt: pSelect);
200036 return rc;
200037}
200038
200039
200040/*
200041** Populate pCsr->aMatchinfo[] with data for the current row. The
200042** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
200043*/
200044static void fts3GetMatchinfo(
200045 sqlite3_context *pCtx, /* Return results here */
200046 Fts3Cursor *pCsr, /* FTS3 Cursor object */
200047 const char *zArg /* Second argument to matchinfo() function */
200048){
200049 MatchInfo sInfo;
200050 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
200051 int rc = SQLITE_OK;
200052 int bGlobal = 0; /* Collect 'global' stats as well as local */
200053
200054 u32 *aOut = 0;
200055 void (*xDestroyOut)(void*) = 0;
200056
200057 memset(s: &sInfo, c: 0, n: sizeof(MatchInfo));
200058 sInfo.pCursor = pCsr;
200059 sInfo.nCol = pTab->nColumn;
200060
200061 /* If there is cached matchinfo() data, but the format string for the
200062 ** cache does not match the format string for this request, discard
200063 ** the cached data. */
200064 if( pCsr->pMIBuffer && strcmp(s1: pCsr->pMIBuffer->zMatchinfo, s2: zArg) ){
200065 sqlite3Fts3MIBufferFree(p: pCsr->pMIBuffer);
200066 pCsr->pMIBuffer = 0;
200067 }
200068
200069 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
200070 ** matchinfo function has been called for this query. In this case
200071 ** allocate the array used to accumulate the matchinfo data and
200072 ** initialize those elements that are constant for every row.
200073 */
200074 if( pCsr->pMIBuffer==0 ){
200075 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
200076 int i; /* Used to iterate through zArg */
200077
200078 /* Determine the number of phrases in the query */
200079 pCsr->nPhrase = fts3ExprPhraseCount(pExpr: pCsr->pExpr);
200080 sInfo.nPhrase = pCsr->nPhrase;
200081
200082 /* Determine the number of integers in the buffer returned by this call. */
200083 for(i=0; zArg[i]; i++){
200084 char *zErr = 0;
200085 if( fts3MatchinfoCheck(pTab, cArg: zArg[i], pzErr: &zErr) ){
200086 sqlite3_result_error(pCtx, z: zErr, n: -1);
200087 sqlite3_free(p: zErr);
200088 return;
200089 }
200090 nMatchinfo += fts3MatchinfoSize(pInfo: &sInfo, cArg: zArg[i]);
200091 }
200092
200093 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
200094 pCsr->pMIBuffer = fts3MIBufferNew(nElem: nMatchinfo, zMatchinfo: zArg);
200095 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
200096
200097 pCsr->isMatchinfoNeeded = 1;
200098 bGlobal = 1;
200099 }
200100
200101 if( rc==SQLITE_OK ){
200102 xDestroyOut = fts3MIBufferAlloc(p: pCsr->pMIBuffer, paOut: &aOut);
200103 if( xDestroyOut==0 ){
200104 rc = SQLITE_NOMEM;
200105 }
200106 }
200107
200108 if( rc==SQLITE_OK ){
200109 sInfo.aMatchinfo = aOut;
200110 sInfo.nPhrase = pCsr->nPhrase;
200111 rc = fts3MatchinfoValues(pCsr, bGlobal, pInfo: &sInfo, zArg);
200112 if( bGlobal ){
200113 fts3MIBufferSetGlobal(p: pCsr->pMIBuffer);
200114 }
200115 }
200116
200117 if( rc!=SQLITE_OK ){
200118 sqlite3_result_error_code(pCtx, errCode: rc);
200119 if( xDestroyOut ) xDestroyOut(aOut);
200120 }else{
200121 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
200122 sqlite3_result_blob(pCtx, z: aOut, n, xDel: xDestroyOut);
200123 }
200124}
200125
200126/*
200127** Implementation of snippet() function.
200128*/
200129SQLITE_PRIVATE void sqlite3Fts3Snippet(
200130 sqlite3_context *pCtx, /* SQLite function call context */
200131 Fts3Cursor *pCsr, /* Cursor object */
200132 const char *zStart, /* Snippet start text - "<b>" */
200133 const char *zEnd, /* Snippet end text - "</b>" */
200134 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
200135 int iCol, /* Extract snippet from this column */
200136 int nToken /* Approximate number of tokens in snippet */
200137){
200138 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
200139 int rc = SQLITE_OK;
200140 int i;
200141 StrBuffer res = {0, 0, 0};
200142
200143 /* The returned text includes up to four fragments of text extracted from
200144 ** the data in the current row. The first iteration of the for(...) loop
200145 ** below attempts to locate a single fragment of text nToken tokens in
200146 ** size that contains at least one instance of all phrases in the query
200147 ** expression that appear in the current row. If such a fragment of text
200148 ** cannot be found, the second iteration of the loop attempts to locate
200149 ** a pair of fragments, and so on.
200150 */
200151 int nSnippet = 0; /* Number of fragments in this snippet */
200152 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
200153 int nFToken = -1; /* Number of tokens in each fragment */
200154
200155 if( !pCsr->pExpr ){
200156 sqlite3_result_text(pCtx, z: "", n: 0, SQLITE_STATIC);
200157 return;
200158 }
200159
200160 /* Limit the snippet length to 64 tokens. */
200161 if( nToken<-64 ) nToken = -64;
200162 if( nToken>+64 ) nToken = +64;
200163
200164 for(nSnippet=1; 1; nSnippet++){
200165
200166 int iSnip; /* Loop counter 0..nSnippet-1 */
200167 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
200168 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
200169
200170 if( nToken>=0 ){
200171 nFToken = (nToken+nSnippet-1) / nSnippet;
200172 }else{
200173 nFToken = -1 * nToken;
200174 }
200175
200176 for(iSnip=0; iSnip<nSnippet; iSnip++){
200177 int iBestScore = -1; /* Best score of columns checked so far */
200178 int iRead; /* Used to iterate through columns */
200179 SnippetFragment *pFragment = &aSnippet[iSnip];
200180
200181 memset(s: pFragment, c: 0, n: sizeof(*pFragment));
200182
200183 /* Loop through all columns of the table being considered for snippets.
200184 ** If the iCol argument to this function was negative, this means all
200185 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
200186 */
200187 for(iRead=0; iRead<pTab->nColumn; iRead++){
200188 SnippetFragment sF = {0, 0, 0, 0};
200189 int iS = 0;
200190 if( iCol>=0 && iRead!=iCol ) continue;
200191
200192 /* Find the best snippet of nFToken tokens in column iRead. */
200193 rc = fts3BestSnippet(nSnippet: nFToken, pCsr, iCol: iRead, mCovered, pmSeen: &mSeen, pFragment: &sF, piScore: &iS);
200194 if( rc!=SQLITE_OK ){
200195 goto snippet_out;
200196 }
200197 if( iS>iBestScore ){
200198 *pFragment = sF;
200199 iBestScore = iS;
200200 }
200201 }
200202
200203 mCovered |= pFragment->covered;
200204 }
200205
200206 /* If all query phrases seen by fts3BestSnippet() are present in at least
200207 ** one of the nSnippet snippet fragments, break out of the loop.
200208 */
200209 assert( (mCovered&mSeen)==mCovered );
200210 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
200211 }
200212
200213 assert( nFToken>0 );
200214
200215 for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
200216 rc = fts3SnippetText(pCsr, pFragment: &aSnippet[i],
200217 iFragment: i, isLast: (i==nSnippet-1), nSnippet: nFToken, zOpen: zStart, zClose: zEnd, zEllipsis, pOut: &res
200218 );
200219 }
200220
200221 snippet_out:
200222 sqlite3Fts3SegmentsClose(p: pTab);
200223 if( rc!=SQLITE_OK ){
200224 sqlite3_result_error_code(pCtx, errCode: rc);
200225 sqlite3_free(p: res.z);
200226 }else{
200227 sqlite3_result_text(pCtx, z: res.z, n: -1, xDel: sqlite3_free);
200228 }
200229}
200230
200231
200232typedef struct TermOffset TermOffset;
200233typedef struct TermOffsetCtx TermOffsetCtx;
200234
200235struct TermOffset {
200236 char *pList; /* Position-list */
200237 i64 iPos; /* Position just read from pList */
200238 i64 iOff; /* Offset of this term from read positions */
200239};
200240
200241struct TermOffsetCtx {
200242 Fts3Cursor *pCsr;
200243 int iCol; /* Column of table to populate aTerm for */
200244 int iTerm;
200245 sqlite3_int64 iDocid;
200246 TermOffset *aTerm;
200247};
200248
200249/*
200250** This function is an sqlite3Fts3ExprIterate() callback used by sqlite3Fts3Offsets().
200251*/
200252static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
200253 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
200254 int nTerm; /* Number of tokens in phrase */
200255 int iTerm; /* For looping through nTerm phrase terms */
200256 char *pList; /* Pointer to position list for phrase */
200257 i64 iPos = 0; /* First position in position-list */
200258 int rc;
200259
200260 UNUSED_PARAMETER(iPhrase);
200261 rc = sqlite3Fts3EvalPhrasePoslist(pCsr: p->pCsr, pExpr, iCol: p->iCol, ppOut: &pList);
200262 nTerm = pExpr->pPhrase->nToken;
200263 if( pList ){
200264 fts3GetDeltaPosition(pp: &pList, piPos: &iPos);
200265 assert_fts3_nc( iPos>=0 );
200266 }
200267
200268 for(iTerm=0; iTerm<nTerm; iTerm++){
200269 TermOffset *pT = &p->aTerm[p->iTerm++];
200270 pT->iOff = nTerm-iTerm-1;
200271 pT->pList = pList;
200272 pT->iPos = iPos;
200273 }
200274
200275 return rc;
200276}
200277
200278/*
200279** Implementation of offsets() function.
200280*/
200281SQLITE_PRIVATE void sqlite3Fts3Offsets(
200282 sqlite3_context *pCtx, /* SQLite function call context */
200283 Fts3Cursor *pCsr /* Cursor object */
200284){
200285 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
200286 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
200287 int rc; /* Return Code */
200288 int nToken; /* Number of tokens in query */
200289 int iCol; /* Column currently being processed */
200290 StrBuffer res = {0, 0, 0}; /* Result string */
200291 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
200292
200293 if( !pCsr->pExpr ){
200294 sqlite3_result_text(pCtx, z: "", n: 0, SQLITE_STATIC);
200295 return;
200296 }
200297
200298 memset(s: &sCtx, c: 0, n: sizeof(sCtx));
200299 assert( pCsr->isRequireSeek==0 );
200300
200301 /* Count the number of terms in the query */
200302 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: 0, pnToken: &nToken);
200303 if( rc!=SQLITE_OK ) goto offsets_out;
200304
200305 /* Allocate the array of TermOffset iterators. */
200306 sCtx.aTerm = (TermOffset *)sqlite3Fts3MallocZero(nByte: sizeof(TermOffset)*nToken);
200307 if( 0==sCtx.aTerm ){
200308 rc = SQLITE_NOMEM;
200309 goto offsets_out;
200310 }
200311 sCtx.iDocid = pCsr->iPrevId;
200312 sCtx.pCsr = pCsr;
200313
200314 /* Loop through the table columns, appending offset information to
200315 ** string-buffer res for each column.
200316 */
200317 for(iCol=0; iCol<pTab->nColumn; iCol++){
200318 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
200319 const char *ZDUMMY; /* Dummy argument used with xNext() */
200320 int NDUMMY = 0; /* Dummy argument used with xNext() */
200321 int iStart = 0;
200322 int iEnd = 0;
200323 int iCurrent = 0;
200324 const char *zDoc;
200325 int nDoc;
200326
200327 /* Initialize the contents of sCtx.aTerm[] for column iCol. This
200328 ** operation may fail if the database contains corrupt records.
200329 */
200330 sCtx.iCol = iCol;
200331 sCtx.iTerm = 0;
200332 rc = sqlite3Fts3ExprIterate(
200333 pExpr: pCsr->pExpr, x: fts3ExprTermOffsetInit, pCtx: (void*)&sCtx
200334 );
200335 if( rc!=SQLITE_OK ) goto offsets_out;
200336
200337 /* Retreive the text stored in column iCol. If an SQL NULL is stored
200338 ** in column iCol, jump immediately to the next iteration of the loop.
200339 ** If an OOM occurs while retrieving the data (this can happen if SQLite
200340 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
200341 ** to the caller.
200342 */
200343 zDoc = (const char *)sqlite3_column_text(pStmt: pCsr->pStmt, i: iCol+1);
200344 nDoc = sqlite3_column_bytes(pStmt: pCsr->pStmt, i: iCol+1);
200345 if( zDoc==0 ){
200346 if( sqlite3_column_type(pStmt: pCsr->pStmt, i: iCol+1)==SQLITE_NULL ){
200347 continue;
200348 }
200349 rc = SQLITE_NOMEM;
200350 goto offsets_out;
200351 }
200352
200353 /* Initialize a tokenizer iterator to iterate through column iCol. */
200354 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pTab->pTokenizer, iLangid: pCsr->iLangid,
200355 z: zDoc, n: nDoc, ppCsr: &pC
200356 );
200357 if( rc!=SQLITE_OK ) goto offsets_out;
200358
200359 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
200360 while( rc==SQLITE_OK ){
200361 int i; /* Used to loop through terms */
200362 int iMinPos = 0x7FFFFFFF; /* Position of next token */
200363 TermOffset *pTerm = 0; /* TermOffset associated with next token */
200364
200365 for(i=0; i<nToken; i++){
200366 TermOffset *pT = &sCtx.aTerm[i];
200367 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
200368 iMinPos = pT->iPos-pT->iOff;
200369 pTerm = pT;
200370 }
200371 }
200372
200373 if( !pTerm ){
200374 /* All offsets for this column have been gathered. */
200375 rc = SQLITE_DONE;
200376 }else{
200377 assert_fts3_nc( iCurrent<=iMinPos );
200378 if( 0==(0xFE&*pTerm->pList) ){
200379 pTerm->pList = 0;
200380 }else{
200381 fts3GetDeltaPosition(pp: &pTerm->pList, piPos: &pTerm->iPos);
200382 }
200383 while( rc==SQLITE_OK && iCurrent<iMinPos ){
200384 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
200385 }
200386 if( rc==SQLITE_OK ){
200387 char aBuffer[64];
200388 sqlite3_snprintf(n: sizeof(aBuffer), zBuf: aBuffer,
200389 zFormat: "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
200390 );
200391 rc = fts3StringAppend(pStr: &res, zAppend: aBuffer, nAppend: -1);
200392 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
200393 rc = FTS_CORRUPT_VTAB;
200394 }
200395 }
200396 }
200397 if( rc==SQLITE_DONE ){
200398 rc = SQLITE_OK;
200399 }
200400
200401 pMod->xClose(pC);
200402 if( rc!=SQLITE_OK ) goto offsets_out;
200403 }
200404
200405 offsets_out:
200406 sqlite3_free(p: sCtx.aTerm);
200407 assert( rc!=SQLITE_DONE );
200408 sqlite3Fts3SegmentsClose(p: pTab);
200409 if( rc!=SQLITE_OK ){
200410 sqlite3_result_error_code(pCtx, errCode: rc);
200411 sqlite3_free(p: res.z);
200412 }else{
200413 sqlite3_result_text(pCtx, z: res.z, n: res.n-1, xDel: sqlite3_free);
200414 }
200415 return;
200416}
200417
200418/*
200419** Implementation of matchinfo() function.
200420*/
200421SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
200422 sqlite3_context *pContext, /* Function call context */
200423 Fts3Cursor *pCsr, /* FTS3 table cursor */
200424 const char *zArg /* Second arg to matchinfo() function */
200425){
200426 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
200427 const char *zFormat;
200428
200429 if( zArg ){
200430 zFormat = zArg;
200431 }else{
200432 zFormat = FTS3_MATCHINFO_DEFAULT;
200433 }
200434
200435 if( !pCsr->pExpr ){
200436 sqlite3_result_blob(pCtx: pContext, z: "", n: 0, SQLITE_STATIC);
200437 return;
200438 }else{
200439 /* Retrieve matchinfo() data. */
200440 fts3GetMatchinfo(pCtx: pContext, pCsr, zArg: zFormat);
200441 sqlite3Fts3SegmentsClose(p: pTab);
200442 }
200443}
200444
200445#endif
200446
200447/************** End of fts3_snippet.c ****************************************/
200448/************** Begin file fts3_unicode.c ************************************/
200449/*
200450** 2012 May 24
200451**
200452** The author disclaims copyright to this source code. In place of
200453** a legal notice, here is a blessing:
200454**
200455** May you do good and not evil.
200456** May you find forgiveness for yourself and forgive others.
200457** May you share freely, never taking more than you give.
200458**
200459******************************************************************************
200460**
200461** Implementation of the "unicode" full-text-search tokenizer.
200462*/
200463
200464#ifndef SQLITE_DISABLE_FTS3_UNICODE
200465
200466/* #include "fts3Int.h" */
200467#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
200468
200469/* #include <assert.h> */
200470/* #include <stdlib.h> */
200471/* #include <stdio.h> */
200472/* #include <string.h> */
200473
200474/* #include "fts3_tokenizer.h" */
200475
200476/*
200477** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
200478** from the sqlite3 source file utf.c. If this file is compiled as part
200479** of the amalgamation, they are not required.
200480*/
200481#ifndef SQLITE_AMALGAMATION
200482
200483static const unsigned char sqlite3Utf8Trans1[] = {
200484 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
200485 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
200486 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
200487 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
200488 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
200489 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
200490 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
200491 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
200492};
200493
200494#define READ_UTF8(zIn, zTerm, c) \
200495 c = *(zIn++); \
200496 if( c>=0xc0 ){ \
200497 c = sqlite3Utf8Trans1[c-0xc0]; \
200498 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
200499 c = (c<<6) + (0x3f & *(zIn++)); \
200500 } \
200501 if( c<0x80 \
200502 || (c&0xFFFFF800)==0xD800 \
200503 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
200504 }
200505
200506#define WRITE_UTF8(zOut, c) { \
200507 if( c<0x00080 ){ \
200508 *zOut++ = (u8)(c&0xFF); \
200509 } \
200510 else if( c<0x00800 ){ \
200511 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
200512 *zOut++ = 0x80 + (u8)(c & 0x3F); \
200513 } \
200514 else if( c<0x10000 ){ \
200515 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
200516 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
200517 *zOut++ = 0x80 + (u8)(c & 0x3F); \
200518 }else{ \
200519 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
200520 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
200521 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
200522 *zOut++ = 0x80 + (u8)(c & 0x3F); \
200523 } \
200524}
200525
200526#endif /* ifndef SQLITE_AMALGAMATION */
200527
200528typedef struct unicode_tokenizer unicode_tokenizer;
200529typedef struct unicode_cursor unicode_cursor;
200530
200531struct unicode_tokenizer {
200532 sqlite3_tokenizer base;
200533 int eRemoveDiacritic;
200534 int nException;
200535 int *aiException;
200536};
200537
200538struct unicode_cursor {
200539 sqlite3_tokenizer_cursor base;
200540 const unsigned char *aInput; /* Input text being tokenized */
200541 int nInput; /* Size of aInput[] in bytes */
200542 int iOff; /* Current offset within aInput[] */
200543 int iToken; /* Index of next token to be returned */
200544 char *zToken; /* storage for current token */
200545 int nAlloc; /* space allocated at zToken */
200546};
200547
200548
200549/*
200550** Destroy a tokenizer allocated by unicodeCreate().
200551*/
200552static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
200553 if( pTokenizer ){
200554 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
200555 sqlite3_free(p: p->aiException);
200556 sqlite3_free(p);
200557 }
200558 return SQLITE_OK;
200559}
200560
200561/*
200562** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
200563** statement has specified that the tokenizer for this table shall consider
200564** all characters in string zIn/nIn to be separators (if bAlnum==0) or
200565** token characters (if bAlnum==1).
200566**
200567** For each codepoint in the zIn/nIn string, this function checks if the
200568** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
200569** If so, no action is taken. Otherwise, the codepoint is added to the
200570** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
200571** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
200572** codepoints in the aiException[] array.
200573**
200574** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
200575** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
200576** It is not possible to change the behavior of the tokenizer with respect
200577** to these codepoints.
200578*/
200579static int unicodeAddExceptions(
200580 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
200581 int bAlnum, /* Replace Isalnum() return value with this */
200582 const char *zIn, /* Array of characters to make exceptions */
200583 int nIn /* Length of z in bytes */
200584){
200585 const unsigned char *z = (const unsigned char *)zIn;
200586 const unsigned char *zTerm = &z[nIn];
200587 unsigned int iCode;
200588 int nEntry = 0;
200589
200590 assert( bAlnum==0 || bAlnum==1 );
200591
200592 while( z<zTerm ){
200593 READ_UTF8(z, zTerm, iCode);
200594 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
200595 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
200596 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
200597 ){
200598 nEntry++;
200599 }
200600 }
200601
200602 if( nEntry ){
200603 int *aNew; /* New aiException[] array */
200604 int nNew; /* Number of valid entries in array aNew[] */
200605
200606 aNew = sqlite3_realloc64(pOld: p->aiException,n: (p->nException+nEntry)*sizeof(int));
200607 if( aNew==0 ) return SQLITE_NOMEM;
200608 nNew = p->nException;
200609
200610 z = (const unsigned char *)zIn;
200611 while( z<zTerm ){
200612 READ_UTF8(z, zTerm, iCode);
200613 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
200614 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
200615 ){
200616 int i, j;
200617 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
200618 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
200619 aNew[i] = (int)iCode;
200620 nNew++;
200621 }
200622 }
200623 p->aiException = aNew;
200624 p->nException = nNew;
200625 }
200626
200627 return SQLITE_OK;
200628}
200629
200630/*
200631** Return true if the p->aiException[] array contains the value iCode.
200632*/
200633static int unicodeIsException(unicode_tokenizer *p, int iCode){
200634 if( p->nException>0 ){
200635 int *a = p->aiException;
200636 int iLo = 0;
200637 int iHi = p->nException-1;
200638
200639 while( iHi>=iLo ){
200640 int iTest = (iHi + iLo) / 2;
200641 if( iCode==a[iTest] ){
200642 return 1;
200643 }else if( iCode>a[iTest] ){
200644 iLo = iTest+1;
200645 }else{
200646 iHi = iTest-1;
200647 }
200648 }
200649 }
200650
200651 return 0;
200652}
200653
200654/*
200655** Return true if, for the purposes of tokenization, codepoint iCode is
200656** considered a token character (not a separator).
200657*/
200658static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
200659 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
200660 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
200661}
200662
200663/*
200664** Create a new tokenizer instance.
200665*/
200666static int unicodeCreate(
200667 int nArg, /* Size of array argv[] */
200668 const char * const *azArg, /* Tokenizer creation arguments */
200669 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
200670){
200671 unicode_tokenizer *pNew; /* New tokenizer object */
200672 int i;
200673 int rc = SQLITE_OK;
200674
200675 pNew = (unicode_tokenizer *) sqlite3_malloc(n: sizeof(unicode_tokenizer));
200676 if( pNew==NULL ) return SQLITE_NOMEM;
200677 memset(s: pNew, c: 0, n: sizeof(unicode_tokenizer));
200678 pNew->eRemoveDiacritic = 1;
200679
200680 for(i=0; rc==SQLITE_OK && i<nArg; i++){
200681 const char *z = azArg[i];
200682 int n = (int)strlen(s: z);
200683
200684 if( n==19 && memcmp(s1: "remove_diacritics=1", s2: z, n: 19)==0 ){
200685 pNew->eRemoveDiacritic = 1;
200686 }
200687 else if( n==19 && memcmp(s1: "remove_diacritics=0", s2: z, n: 19)==0 ){
200688 pNew->eRemoveDiacritic = 0;
200689 }
200690 else if( n==19 && memcmp(s1: "remove_diacritics=2", s2: z, n: 19)==0 ){
200691 pNew->eRemoveDiacritic = 2;
200692 }
200693 else if( n>=11 && memcmp(s1: "tokenchars=", s2: z, n: 11)==0 ){
200694 rc = unicodeAddExceptions(p: pNew, bAlnum: 1, zIn: &z[11], nIn: n-11);
200695 }
200696 else if( n>=11 && memcmp(s1: "separators=", s2: z, n: 11)==0 ){
200697 rc = unicodeAddExceptions(p: pNew, bAlnum: 0, zIn: &z[11], nIn: n-11);
200698 }
200699 else{
200700 /* Unrecognized argument */
200701 rc = SQLITE_ERROR;
200702 }
200703 }
200704
200705 if( rc!=SQLITE_OK ){
200706 unicodeDestroy(pTokenizer: (sqlite3_tokenizer *)pNew);
200707 pNew = 0;
200708 }
200709 *pp = (sqlite3_tokenizer *)pNew;
200710 return rc;
200711}
200712
200713/*
200714** Prepare to begin tokenizing a particular string. The input
200715** string to be tokenized is pInput[0..nBytes-1]. A cursor
200716** used to incrementally tokenize this string is returned in
200717** *ppCursor.
200718*/
200719static int unicodeOpen(
200720 sqlite3_tokenizer *p, /* The tokenizer */
200721 const char *aInput, /* Input string */
200722 int nInput, /* Size of string aInput in bytes */
200723 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
200724){
200725 unicode_cursor *pCsr;
200726
200727 pCsr = (unicode_cursor *)sqlite3_malloc(n: sizeof(unicode_cursor));
200728 if( pCsr==0 ){
200729 return SQLITE_NOMEM;
200730 }
200731 memset(s: pCsr, c: 0, n: sizeof(unicode_cursor));
200732
200733 pCsr->aInput = (const unsigned char *)aInput;
200734 if( aInput==0 ){
200735 pCsr->nInput = 0;
200736 pCsr->aInput = (const unsigned char*)"";
200737 }else if( nInput<0 ){
200738 pCsr->nInput = (int)strlen(s: aInput);
200739 }else{
200740 pCsr->nInput = nInput;
200741 }
200742
200743 *pp = &pCsr->base;
200744 UNUSED_PARAMETER(p);
200745 return SQLITE_OK;
200746}
200747
200748/*
200749** Close a tokenization cursor previously opened by a call to
200750** simpleOpen() above.
200751*/
200752static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
200753 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
200754 sqlite3_free(p: pCsr->zToken);
200755 sqlite3_free(p: pCsr);
200756 return SQLITE_OK;
200757}
200758
200759/*
200760** Extract the next token from a tokenization cursor. The cursor must
200761** have been opened by a prior call to simpleOpen().
200762*/
200763static int unicodeNext(
200764 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
200765 const char **paToken, /* OUT: Token text */
200766 int *pnToken, /* OUT: Number of bytes at *paToken */
200767 int *piStart, /* OUT: Starting offset of token */
200768 int *piEnd, /* OUT: Ending offset of token */
200769 int *piPos /* OUT: Position integer of token */
200770){
200771 unicode_cursor *pCsr = (unicode_cursor *)pC;
200772 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
200773 unsigned int iCode = 0;
200774 char *zOut;
200775 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
200776 const unsigned char *zStart = z;
200777 const unsigned char *zEnd;
200778 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
200779
200780 /* Scan past any delimiter characters before the start of the next token.
200781 ** Return SQLITE_DONE early if this takes us all the way to the end of
200782 ** the input. */
200783 while( z<zTerm ){
200784 READ_UTF8(z, zTerm, iCode);
200785 if( unicodeIsAlnum(p, iCode: (int)iCode) ) break;
200786 zStart = z;
200787 }
200788 if( zStart>=zTerm ) return SQLITE_DONE;
200789
200790 zOut = pCsr->zToken;
200791 do {
200792 int iOut;
200793
200794 /* Grow the output buffer if required. */
200795 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
200796 char *zNew = sqlite3_realloc64(pOld: pCsr->zToken, n: pCsr->nAlloc+64);
200797 if( !zNew ) return SQLITE_NOMEM;
200798 zOut = &zNew[zOut - pCsr->zToken];
200799 pCsr->zToken = zNew;
200800 pCsr->nAlloc += 64;
200801 }
200802
200803 /* Write the folded case of the last character read to the output */
200804 zEnd = z;
200805 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
200806 if( iOut ){
200807 WRITE_UTF8(zOut, iOut);
200808 }
200809
200810 /* If the cursor is not at EOF, read the next character */
200811 if( z>=zTerm ) break;
200812 READ_UTF8(z, zTerm, iCode);
200813 }while( unicodeIsAlnum(p, iCode: (int)iCode)
200814 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
200815 );
200816
200817 /* Set the output variables and return. */
200818 pCsr->iOff = (int)(z - pCsr->aInput);
200819 *paToken = pCsr->zToken;
200820 *pnToken = (int)(zOut - pCsr->zToken);
200821 *piStart = (int)(zStart - pCsr->aInput);
200822 *piEnd = (int)(zEnd - pCsr->aInput);
200823 *piPos = pCsr->iToken++;
200824 return SQLITE_OK;
200825}
200826
200827/*
200828** Set *ppModule to a pointer to the sqlite3_tokenizer_module
200829** structure for the unicode tokenizer.
200830*/
200831SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
200832 static const sqlite3_tokenizer_module module = {
200833 0,
200834 unicodeCreate,
200835 unicodeDestroy,
200836 unicodeOpen,
200837 unicodeClose,
200838 unicodeNext,
200839 0,
200840 };
200841 *ppModule = &module;
200842}
200843
200844#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
200845#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
200846
200847/************** End of fts3_unicode.c ****************************************/
200848/************** Begin file fts3_unicode2.c ***********************************/
200849/*
200850** 2012-05-25
200851**
200852** The author disclaims copyright to this source code. In place of
200853** a legal notice, here is a blessing:
200854**
200855** May you do good and not evil.
200856** May you find forgiveness for yourself and forgive others.
200857** May you share freely, never taking more than you give.
200858**
200859******************************************************************************
200860*/
200861
200862/*
200863** DO NOT EDIT THIS MACHINE GENERATED FILE.
200864*/
200865
200866#ifndef SQLITE_DISABLE_FTS3_UNICODE
200867#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
200868
200869/* #include <assert.h> */
200870
200871/*
200872** Return true if the argument corresponds to a unicode codepoint
200873** classified as either a letter or a number. Otherwise false.
200874**
200875** The results are undefined if the value passed to this function
200876** is less than zero.
200877*/
200878SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
200879 /* Each unsigned integer in the following array corresponds to a contiguous
200880 ** range of unicode codepoints that are not either letters or numbers (i.e.
200881 ** codepoints for which this function should return 0).
200882 **
200883 ** The most significant 22 bits in each 32-bit value contain the first
200884 ** codepoint in the range. The least significant 10 bits are used to store
200885 ** the size of the range (always at least 1). In other words, the value
200886 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
200887 ** C. It is not possible to represent a range larger than 1023 codepoints
200888 ** using this format.
200889 */
200890 static const unsigned int aEntry[] = {
200891 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
200892 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
200893 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
200894 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
200895 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
200896 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
200897 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
200898 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
200899 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
200900 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
200901 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
200902 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
200903 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
200904 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
200905 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
200906 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
200907 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
200908 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
200909 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
200910 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
200911 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
200912 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
200913 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
200914 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
200915 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
200916 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
200917 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
200918 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
200919 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
200920 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
200921 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
200922 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
200923 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
200924 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
200925 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
200926 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
200927 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
200928 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
200929 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
200930 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
200931 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
200932 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
200933 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
200934 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
200935 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
200936 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
200937 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
200938 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
200939 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
200940 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
200941 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
200942 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
200943 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
200944 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
200945 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
200946 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
200947 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
200948 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
200949 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
200950 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
200951 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
200952 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
200953 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
200954 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
200955 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
200956 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
200957 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
200958 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
200959 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
200960 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
200961 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
200962 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
200963 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
200964 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
200965 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
200966 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
200967 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
200968 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
200969 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
200970 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
200971 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
200972 0x380400F0,
200973 };
200974 static const unsigned int aAscii[4] = {
200975 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
200976 };
200977
200978 if( (unsigned int)c<128 ){
200979 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
200980 }else if( (unsigned int)c<(1<<22) ){
200981 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
200982 int iRes = 0;
200983 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
200984 int iLo = 0;
200985 while( iHi>=iLo ){
200986 int iTest = (iHi + iLo) / 2;
200987 if( key >= aEntry[iTest] ){
200988 iRes = iTest;
200989 iLo = iTest+1;
200990 }else{
200991 iHi = iTest-1;
200992 }
200993 }
200994 assert( aEntry[0]<key );
200995 assert( key>=aEntry[iRes] );
200996 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
200997 }
200998 return 1;
200999}
201000
201001
201002/*
201003** If the argument is a codepoint corresponding to a lowercase letter
201004** in the ASCII range with a diacritic added, return the codepoint
201005** of the ASCII letter only. For example, if passed 235 - "LATIN
201006** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
201007** E"). The resuls of passing a codepoint that corresponds to an
201008** uppercase letter are undefined.
201009*/
201010static int remove_diacritic(int c, int bComplex){
201011 unsigned short aDia[] = {
201012 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
201013 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
201014 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
201015 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
201016 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
201017 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
201018 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
201019 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
201020 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
201021 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
201022 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
201023 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
201024 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
201025 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
201026 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
201027 63182, 63242, 63274, 63310, 63368, 63390,
201028 };
201029#define HIBIT ((unsigned char)0x80)
201030 unsigned char aChar[] = {
201031 '\0', 'a', 'c', 'e', 'i', 'n',
201032 'o', 'u', 'y', 'y', 'a', 'c',
201033 'd', 'e', 'e', 'g', 'h', 'i',
201034 'j', 'k', 'l', 'n', 'o', 'r',
201035 's', 't', 'u', 'u', 'w', 'y',
201036 'z', 'o', 'u', 'a', 'i', 'o',
201037 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
201038 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
201039 'e', 'i', 'o', 'r', 'u', 's',
201040 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
201041 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
201042 '\0', '\0', '\0', '\0', 'a', 'b',
201043 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
201044 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
201045 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
201046 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
201047 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
201048 'w', 'x', 'y', 'z', 'h', 't',
201049 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
201050 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
201051 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
201052 };
201053
201054 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
201055 int iRes = 0;
201056 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
201057 int iLo = 0;
201058 while( iHi>=iLo ){
201059 int iTest = (iHi + iLo) / 2;
201060 if( key >= aDia[iTest] ){
201061 iRes = iTest;
201062 iLo = iTest+1;
201063 }else{
201064 iHi = iTest-1;
201065 }
201066 }
201067 assert( key>=aDia[iRes] );
201068 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
201069 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
201070}
201071
201072
201073/*
201074** Return true if the argument interpreted as a unicode codepoint
201075** is a diacritical modifier character.
201076*/
201077SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
201078 unsigned int mask0 = 0x08029FDF;
201079 unsigned int mask1 = 0x000361F8;
201080 if( c<768 || c>817 ) return 0;
201081 return (c < 768+32) ?
201082 (mask0 & ((unsigned int)1 << (c-768))) :
201083 (mask1 & ((unsigned int)1 << (c-768-32)));
201084}
201085
201086
201087/*
201088** Interpret the argument as a unicode codepoint. If the codepoint
201089** is an upper case character that has a lower case equivalent,
201090** return the codepoint corresponding to the lower case version.
201091** Otherwise, return a copy of the argument.
201092**
201093** The results are undefined if the value passed to this function
201094** is less than zero.
201095*/
201096SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
201097 /* Each entry in the following array defines a rule for folding a range
201098 ** of codepoints to lower case. The rule applies to a range of nRange
201099 ** codepoints starting at codepoint iCode.
201100 **
201101 ** If the least significant bit in flags is clear, then the rule applies
201102 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
201103 ** need to be folded). Or, if it is set, then the rule only applies to
201104 ** every second codepoint in the range, starting with codepoint C.
201105 **
201106 ** The 7 most significant bits in flags are an index into the aiOff[]
201107 ** array. If a specific codepoint C does require folding, then its lower
201108 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
201109 **
201110 ** The contents of this array are generated by parsing the CaseFolding.txt
201111 ** file distributed as part of the "Unicode Character Database". See
201112 ** http://www.unicode.org for details.
201113 */
201114 static const struct TableEntry {
201115 unsigned short iCode;
201116 unsigned char flags;
201117 unsigned char nRange;
201118 } aEntry[] = {
201119 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
201120 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
201121 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
201122 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
201123 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
201124 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
201125 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
201126 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
201127 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
201128 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
201129 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
201130 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
201131 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
201132 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
201133 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
201134 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
201135 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
201136 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
201137 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
201138 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
201139 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
201140 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
201141 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
201142 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
201143 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
201144 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
201145 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
201146 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
201147 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
201148 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
201149 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
201150 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
201151 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
201152 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
201153 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
201154 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
201155 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
201156 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
201157 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
201158 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
201159 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
201160 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
201161 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
201162 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
201163 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
201164 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
201165 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
201166 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
201167 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
201168 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
201169 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
201170 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
201171 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
201172 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
201173 {65313, 14, 26},
201174 };
201175 static const unsigned short aiOff[] = {
201176 1, 2, 8, 15, 16, 26, 28, 32,
201177 37, 38, 40, 48, 63, 64, 69, 71,
201178 79, 80, 116, 202, 203, 205, 206, 207,
201179 209, 210, 211, 213, 214, 217, 218, 219,
201180 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
201181 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
201182 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
201183 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
201184 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
201185 65514, 65521, 65527, 65528, 65529,
201186 };
201187
201188 int ret = c;
201189
201190 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
201191
201192 if( c<128 ){
201193 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
201194 }else if( c<65536 ){
201195 const struct TableEntry *p;
201196 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
201197 int iLo = 0;
201198 int iRes = -1;
201199
201200 assert( c>aEntry[0].iCode );
201201 while( iHi>=iLo ){
201202 int iTest = (iHi + iLo) / 2;
201203 int cmp = (c - aEntry[iTest].iCode);
201204 if( cmp>=0 ){
201205 iRes = iTest;
201206 iLo = iTest+1;
201207 }else{
201208 iHi = iTest-1;
201209 }
201210 }
201211
201212 assert( iRes>=0 && c>=aEntry[iRes].iCode );
201213 p = &aEntry[iRes];
201214 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
201215 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
201216 assert( ret>0 );
201217 }
201218
201219 if( eRemoveDiacritic ){
201220 ret = remove_diacritic(c: ret, bComplex: eRemoveDiacritic==2);
201221 }
201222 }
201223
201224 else if( c>=66560 && c<66600 ){
201225 ret = c + 40;
201226 }
201227
201228 return ret;
201229}
201230#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
201231#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
201232
201233/************** End of fts3_unicode2.c ***************************************/
201234/************** Begin file json.c ********************************************/
201235/*
201236** 2015-08-12
201237**
201238** The author disclaims copyright to this source code. In place of
201239** a legal notice, here is a blessing:
201240**
201241** May you do good and not evil.
201242** May you find forgiveness for yourself and forgive others.
201243** May you share freely, never taking more than you give.
201244**
201245******************************************************************************
201246**
201247** This SQLite JSON functions.
201248**
201249** This file began as an extension in ext/misc/json1.c in 2015. That
201250** extension proved so useful that it has now been moved into the core.
201251**
201252** For the time being, all JSON is stored as pure text. (We might add
201253** a JSONB type in the future which stores a binary encoding of JSON in
201254** a BLOB, but there is no support for JSONB in the current implementation.
201255** This implementation parses JSON text at 250 MB/s, so it is hard to see
201256** how JSONB might improve on that.)
201257*/
201258#ifndef SQLITE_OMIT_JSON
201259/* #include "sqliteInt.h" */
201260
201261/*
201262** Growing our own isspace() routine this way is twice as fast as
201263** the library isspace() function, resulting in a 7% overall performance
201264** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
201265*/
201266static const char jsonIsSpace[] = {
201267 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
201268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201269 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201275
201276 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201278 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201284};
201285#define fast_isspace(x) (jsonIsSpace[(unsigned char)x])
201286
201287/*
201288** Characters that are special to JSON. Control charaters,
201289** '"' and '\\'.
201290*/
201291static const char jsonIsOk[256] = {
201292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201294 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
201295 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201296 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201297 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
201298 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201299 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201300
201301 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201302 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201303 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201304 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201305 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201306 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201307 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201308 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
201309};
201310
201311
201312#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
201313# define VVA(X)
201314#else
201315# define VVA(X) X
201316#endif
201317
201318/* Objects */
201319typedef struct JsonString JsonString;
201320typedef struct JsonNode JsonNode;
201321typedef struct JsonParse JsonParse;
201322typedef struct JsonCleanup JsonCleanup;
201323
201324/* An instance of this object represents a JSON string
201325** under construction. Really, this is a generic string accumulator
201326** that can be and is used to create strings other than JSON.
201327*/
201328struct JsonString {
201329 sqlite3_context *pCtx; /* Function context - put error messages here */
201330 char *zBuf; /* Append JSON content here */
201331 u64 nAlloc; /* Bytes of storage available in zBuf[] */
201332 u64 nUsed; /* Bytes of zBuf[] currently used */
201333 u8 bStatic; /* True if zBuf is static space */
201334 u8 bErr; /* True if an error has been encountered */
201335 char zSpace[100]; /* Initial static space */
201336};
201337
201338/* A deferred cleanup task. A list of JsonCleanup objects might be
201339** run when the JsonParse object is destroyed.
201340*/
201341struct JsonCleanup {
201342 JsonCleanup *pJCNext; /* Next in a list */
201343 void (*xOp)(void*); /* Routine to run */
201344 void *pArg; /* Argument to xOp() */
201345};
201346
201347/* JSON type values
201348*/
201349#define JSON_SUBST 0 /* Special edit node. Uses u.iPrev */
201350#define JSON_NULL 1
201351#define JSON_TRUE 2
201352#define JSON_FALSE 3
201353#define JSON_INT 4
201354#define JSON_REAL 5
201355#define JSON_STRING 6
201356#define JSON_ARRAY 7
201357#define JSON_OBJECT 8
201358
201359/* The "subtype" set for JSON values */
201360#define JSON_SUBTYPE 74 /* Ascii for "J" */
201361
201362/*
201363** Names of the various JSON types:
201364*/
201365static const char * const jsonType[] = {
201366 "subst",
201367 "null", "true", "false", "integer", "real", "text", "array", "object"
201368};
201369
201370/* Bit values for the JsonNode.jnFlag field
201371*/
201372#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
201373#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
201374#define JNODE_REMOVE 0x04 /* Do not output */
201375#define JNODE_REPLACE 0x08 /* Target of a JSON_SUBST node */
201376#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
201377#define JNODE_LABEL 0x20 /* Is a label of an object */
201378#define JNODE_JSON5 0x40 /* Node contains JSON5 enhancements */
201379
201380
201381/* A single node of parsed JSON. An array of these nodes describes
201382** a parse of JSON + edits.
201383**
201384** Use the json_parse() SQL function (available when compiled with
201385** -DSQLITE_DEBUG) to see a dump of complete JsonParse objects, including
201386** a complete listing and decoding of the array of JsonNodes.
201387*/
201388struct JsonNode {
201389 u8 eType; /* One of the JSON_ type values */
201390 u8 jnFlags; /* JNODE flags */
201391 u8 eU; /* Which union element to use */
201392 u32 n; /* Bytes of content for INT, REAL or STRING
201393 ** Number of sub-nodes for ARRAY and OBJECT
201394 ** Node that SUBST applies to */
201395 union {
201396 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
201397 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
201398 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
201399 u32 iPrev; /* 4: Previous SUBST node, or 0 */
201400 } u;
201401};
201402
201403
201404/* A parsed and possibly edited JSON string. Lifecycle:
201405**
201406** 1. JSON comes in and is parsed into an array aNode[]. The original
201407** JSON text is stored in zJson.
201408**
201409** 2. Zero or more changes are made (via json_remove() or json_replace()
201410** or similar) to the aNode[] array.
201411**
201412** 3. A new, edited and mimified JSON string is generated from aNode
201413** and stored in zAlt. The JsonParse object always owns zAlt.
201414**
201415** Step 1 always happens. Step 2 and 3 may or may not happen, depending
201416** on the operation.
201417**
201418** aNode[].u.zJContent entries typically point into zJson. Hence zJson
201419** must remain valid for the lifespan of the parse. For edits,
201420** aNode[].u.zJContent might point to malloced space other than zJson.
201421** Entries in pClup are responsible for freeing that extra malloced space.
201422**
201423** When walking the parse tree in aNode[], edits are ignored if useMod is
201424** false.
201425*/
201426struct JsonParse {
201427 u32 nNode; /* Number of slots of aNode[] used */
201428 u32 nAlloc; /* Number of slots of aNode[] allocated */
201429 JsonNode *aNode; /* Array of nodes containing the parse */
201430 char *zJson; /* Original JSON string (before edits) */
201431 char *zAlt; /* Revised and/or mimified JSON */
201432 u32 *aUp; /* Index of parent of each node */
201433 JsonCleanup *pClup;/* Cleanup operations prior to freeing this object */
201434 u16 iDepth; /* Nesting depth */
201435 u8 nErr; /* Number of errors seen */
201436 u8 oom; /* Set to true if out of memory */
201437 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
201438 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
201439 u8 useMod; /* Actually use the edits contain inside aNode */
201440 u8 hasMod; /* aNode contains edits from the original zJson */
201441 u32 nJPRef; /* Number of references to this object */
201442 int nJson; /* Length of the zJson string in bytes */
201443 int nAlt; /* Length of alternative JSON string zAlt, in bytes */
201444 u32 iErr; /* Error location in zJson[] */
201445 u32 iSubst; /* Last JSON_SUBST entry in aNode[] */
201446 u32 iHold; /* Age of this entry in the cache for LRU replacement */
201447};
201448
201449/*
201450** Maximum nesting depth of JSON for this implementation.
201451**
201452** This limit is needed to avoid a stack overflow in the recursive
201453** descent parser. A depth of 1000 is far deeper than any sane JSON
201454** should go. Historical note: This limit was 2000 prior to version 3.42.0
201455*/
201456#define JSON_MAX_DEPTH 1000
201457
201458/**************************************************************************
201459** Utility routines for dealing with JsonString objects
201460**************************************************************************/
201461
201462/* Set the JsonString object to an empty string
201463*/
201464static void jsonZero(JsonString *p){
201465 p->zBuf = p->zSpace;
201466 p->nAlloc = sizeof(p->zSpace);
201467 p->nUsed = 0;
201468 p->bStatic = 1;
201469}
201470
201471/* Initialize the JsonString object
201472*/
201473static void jsonInit(JsonString *p, sqlite3_context *pCtx){
201474 p->pCtx = pCtx;
201475 p->bErr = 0;
201476 jsonZero(p);
201477}
201478
201479/* Free all allocated memory and reset the JsonString object back to its
201480** initial state.
201481*/
201482static void jsonReset(JsonString *p){
201483 if( !p->bStatic ) sqlite3RCStrUnref(z: p->zBuf);
201484 jsonZero(p);
201485}
201486
201487/* Report an out-of-memory (OOM) condition
201488*/
201489static void jsonOom(JsonString *p){
201490 p->bErr = 1;
201491 sqlite3_result_error_nomem(pCtx: p->pCtx);
201492 jsonReset(p);
201493}
201494
201495/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
201496** Return zero on success. Return non-zero on an OOM error
201497*/
201498static int jsonGrow(JsonString *p, u32 N){
201499 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
201500 char *zNew;
201501 if( p->bStatic ){
201502 if( p->bErr ) return 1;
201503 zNew = sqlite3RCStrNew(N: nTotal);
201504 if( zNew==0 ){
201505 jsonOom(p);
201506 return SQLITE_NOMEM;
201507 }
201508 memcpy(dest: zNew, src: p->zBuf, n: (size_t)p->nUsed);
201509 p->zBuf = zNew;
201510 p->bStatic = 0;
201511 }else{
201512 p->zBuf = sqlite3RCStrResize(z: p->zBuf, N: nTotal);
201513 if( p->zBuf==0 ){
201514 p->bErr = 1;
201515 jsonZero(p);
201516 return SQLITE_NOMEM;
201517 }
201518 }
201519 p->nAlloc = nTotal;
201520 return SQLITE_OK;
201521}
201522
201523/* Append N bytes from zIn onto the end of the JsonString string.
201524*/
201525static SQLITE_NOINLINE void jsonAppendExpand(
201526 JsonString *p,
201527 const char *zIn,
201528 u32 N
201529){
201530 assert( N>0 );
201531 if( jsonGrow(p,N) ) return;
201532 memcpy(dest: p->zBuf+p->nUsed, src: zIn, n: N);
201533 p->nUsed += N;
201534}
201535static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
201536 if( N==0 ) return;
201537 if( N+p->nUsed >= p->nAlloc ){
201538 jsonAppendExpand(p,zIn,N);
201539 }else{
201540 memcpy(dest: p->zBuf+p->nUsed, src: zIn, n: N);
201541 p->nUsed += N;
201542 }
201543}
201544static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
201545 assert( N>0 );
201546 if( N+p->nUsed >= p->nAlloc ){
201547 jsonAppendExpand(p,zIn,N);
201548 }else{
201549 memcpy(dest: p->zBuf+p->nUsed, src: zIn, n: N);
201550 p->nUsed += N;
201551 }
201552}
201553
201554
201555/* Append formatted text (not to exceed N bytes) to the JsonString.
201556*/
201557static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
201558 va_list ap;
201559 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
201560 va_start(ap, zFormat);
201561 sqlite3_vsnprintf(n: N, zBuf: p->zBuf+p->nUsed, zFormat, ap);
201562 va_end(ap);
201563 p->nUsed += (int)strlen(s: p->zBuf+p->nUsed);
201564}
201565
201566/* Append a single character
201567*/
201568static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){
201569 if( jsonGrow(p,N: 1) ) return;
201570 p->zBuf[p->nUsed++] = c;
201571}
201572static void jsonAppendChar(JsonString *p, char c){
201573 if( p->nUsed>=p->nAlloc ){
201574 jsonAppendCharExpand(p,c);
201575 }else{
201576 p->zBuf[p->nUsed++] = c;
201577 }
201578}
201579
201580/* Try to force the string to be a zero-terminated RCStr string.
201581**
201582** Return true on success. Return false if an OOM prevents this
201583** from happening.
201584*/
201585static int jsonForceRCStr(JsonString *p){
201586 jsonAppendChar(p, c: 0);
201587 if( p->bErr ) return 0;
201588 p->nUsed--;
201589 if( p->bStatic==0 ) return 1;
201590 p->nAlloc = 0;
201591 p->nUsed++;
201592 jsonGrow(p, N: p->nUsed);
201593 p->nUsed--;
201594 return p->bStatic==0;
201595}
201596
201597
201598/* Append a comma separator to the output buffer, if the previous
201599** character is not '[' or '{'.
201600*/
201601static void jsonAppendSeparator(JsonString *p){
201602 char c;
201603 if( p->nUsed==0 ) return;
201604 c = p->zBuf[p->nUsed-1];
201605 if( c=='[' || c=='{' ) return;
201606 jsonAppendChar(p, c: ',');
201607}
201608
201609/* Append the N-byte string in zIn to the end of the JsonString string
201610** under construction. Enclose the string in "..." and escape
201611** any double-quotes or backslash characters contained within the
201612** string.
201613*/
201614static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
201615 u32 i;
201616 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N: N+2)!=0) ) return;
201617 p->zBuf[p->nUsed++] = '"';
201618 for(i=0; i<N; i++){
201619 unsigned char c = ((unsigned const char*)zIn)[i];
201620 if( jsonIsOk[c] ){
201621 p->zBuf[p->nUsed++] = c;
201622 }else if( c=='"' || c=='\\' ){
201623 json_simple_escape:
201624 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N: N+3-i)!=0 ) return;
201625 p->zBuf[p->nUsed++] = '\\';
201626 p->zBuf[p->nUsed++] = c;
201627 }else if( c=='\'' ){
201628 p->zBuf[p->nUsed++] = c;
201629 }else{
201630 static const char aSpecial[] = {
201631 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
201632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
201633 };
201634 assert( sizeof(aSpecial)==32 );
201635 assert( aSpecial['\b']=='b' );
201636 assert( aSpecial['\f']=='f' );
201637 assert( aSpecial['\n']=='n' );
201638 assert( aSpecial['\r']=='r' );
201639 assert( aSpecial['\t']=='t' );
201640 assert( c>=0 && c<sizeof(aSpecial) );
201641 if( aSpecial[c] ){
201642 c = aSpecial[c];
201643 goto json_simple_escape;
201644 }
201645 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N: N+7-i)!=0 ) return;
201646 p->zBuf[p->nUsed++] = '\\';
201647 p->zBuf[p->nUsed++] = 'u';
201648 p->zBuf[p->nUsed++] = '0';
201649 p->zBuf[p->nUsed++] = '0';
201650 p->zBuf[p->nUsed++] = "0123456789abcdef"[c>>4];
201651 p->zBuf[p->nUsed++] = "0123456789abcdef"[c&0xf];
201652 }
201653 }
201654 p->zBuf[p->nUsed++] = '"';
201655 assert( p->nUsed<p->nAlloc );
201656}
201657
201658/*
201659** The zIn[0..N] string is a JSON5 string literal. Append to p a translation
201660** of the string literal that standard JSON and that omits all JSON5
201661** features.
201662*/
201663static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
201664 u32 i;
201665 jsonAppendChar(p, c: '"');
201666 zIn++;
201667 N -= 2;
201668 while( N>0 ){
201669 for(i=0; i<N && zIn[i]!='\\'; i++){}
201670 if( i>0 ){
201671 jsonAppendRawNZ(p, zIn, N: i);
201672 zIn += i;
201673 N -= i;
201674 if( N==0 ) break;
201675 }
201676 assert( zIn[0]=='\\' );
201677 switch( (u8)zIn[1] ){
201678 case '\'':
201679 jsonAppendChar(p, c: '\'');
201680 break;
201681 case 'v':
201682 jsonAppendRawNZ(p, zIn: "\\u0009", N: 6);
201683 break;
201684 case 'x':
201685 jsonAppendRawNZ(p, zIn: "\\u00", N: 4);
201686 jsonAppendRawNZ(p, zIn: &zIn[2], N: 2);
201687 zIn += 2;
201688 N -= 2;
201689 break;
201690 case '0':
201691 jsonAppendRawNZ(p, zIn: "\\u0000", N: 6);
201692 break;
201693 case '\r':
201694 if( zIn[2]=='\n' ){
201695 zIn++;
201696 N--;
201697 }
201698 break;
201699 case '\n':
201700 break;
201701 case 0xe2:
201702 assert( N>=4 );
201703 assert( 0x80==(u8)zIn[2] );
201704 assert( 0xa8==(u8)zIn[3] || 0xa9==(u8)zIn[3] );
201705 zIn += 2;
201706 N -= 2;
201707 break;
201708 default:
201709 jsonAppendRawNZ(p, zIn, N: 2);
201710 break;
201711 }
201712 zIn += 2;
201713 N -= 2;
201714 }
201715 jsonAppendChar(p, c: '"');
201716}
201717
201718/*
201719** The zIn[0..N] string is a JSON5 integer literal. Append to p a translation
201720** of the string literal that standard JSON and that omits all JSON5
201721** features.
201722*/
201723static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){
201724 if( zIn[0]=='+' ){
201725 zIn++;
201726 N--;
201727 }else if( zIn[0]=='-' ){
201728 jsonAppendChar(p, c: '-');
201729 zIn++;
201730 N--;
201731 }
201732 if( zIn[0]=='0' && (zIn[1]=='x' || zIn[1]=='X') ){
201733 sqlite3_int64 i = 0;
201734 int rc = sqlite3DecOrHexToI64(z: zIn, pOut: &i);
201735 if( rc<=1 ){
201736 jsonPrintf(N: 100,p,zFormat: "%lld",i);
201737 }else{
201738 assert( rc==2 );
201739 jsonAppendRawNZ(p, zIn: "9.0e999", N: 7);
201740 }
201741 return;
201742 }
201743 assert( N>0 );
201744 jsonAppendRawNZ(p, zIn, N);
201745}
201746
201747/*
201748** The zIn[0..N] string is a JSON5 real literal. Append to p a translation
201749** of the string literal that standard JSON and that omits all JSON5
201750** features.
201751*/
201752static void jsonAppendNormalizedReal(JsonString *p, const char *zIn, u32 N){
201753 u32 i;
201754 if( zIn[0]=='+' ){
201755 zIn++;
201756 N--;
201757 }else if( zIn[0]=='-' ){
201758 jsonAppendChar(p, c: '-');
201759 zIn++;
201760 N--;
201761 }
201762 if( zIn[0]=='.' ){
201763 jsonAppendChar(p, c: '0');
201764 }
201765 for(i=0; i<N; i++){
201766 if( zIn[i]=='.' && (i+1==N || !sqlite3Isdigit(zIn[i+1])) ){
201767 i++;
201768 jsonAppendRaw(p, zIn, N: i);
201769 zIn += i;
201770 N -= i;
201771 jsonAppendChar(p, c: '0');
201772 break;
201773 }
201774 }
201775 if( N>0 ){
201776 jsonAppendRawNZ(p, zIn, N);
201777 }
201778}
201779
201780
201781
201782/*
201783** Append a function parameter value to the JSON string under
201784** construction.
201785*/
201786static void jsonAppendValue(
201787 JsonString *p, /* Append to this JSON string */
201788 sqlite3_value *pValue /* Value to append */
201789){
201790 switch( sqlite3_value_type(pVal: pValue) ){
201791 case SQLITE_NULL: {
201792 jsonAppendRawNZ(p, zIn: "null", N: 4);
201793 break;
201794 }
201795 case SQLITE_FLOAT: {
201796 jsonPrintf(N: 100, p, zFormat: "%!0.15g", sqlite3_value_double(pVal: pValue));
201797 break;
201798 }
201799 case SQLITE_INTEGER: {
201800 const char *z = (const char*)sqlite3_value_text(pVal: pValue);
201801 u32 n = (u32)sqlite3_value_bytes(pVal: pValue);
201802 jsonAppendRaw(p, zIn: z, N: n);
201803 break;
201804 }
201805 case SQLITE_TEXT: {
201806 const char *z = (const char*)sqlite3_value_text(pVal: pValue);
201807 u32 n = (u32)sqlite3_value_bytes(pVal: pValue);
201808 if( sqlite3_value_subtype(pVal: pValue)==JSON_SUBTYPE ){
201809 jsonAppendRaw(p, zIn: z, N: n);
201810 }else{
201811 jsonAppendString(p, zIn: z, N: n);
201812 }
201813 break;
201814 }
201815 default: {
201816 if( p->bErr==0 ){
201817 sqlite3_result_error(pCtx: p->pCtx, z: "JSON cannot hold BLOB values", n: -1);
201818 p->bErr = 2;
201819 jsonReset(p);
201820 }
201821 break;
201822 }
201823 }
201824}
201825
201826
201827/* Make the JSON in p the result of the SQL function.
201828**
201829** The JSON string is reset.
201830*/
201831static void jsonResult(JsonString *p){
201832 if( p->bErr==0 ){
201833 if( p->bStatic ){
201834 sqlite3_result_text64(pCtx: p->pCtx, z: p->zBuf, n: p->nUsed,
201835 SQLITE_TRANSIENT, SQLITE_UTF8);
201836 }else if( jsonForceRCStr(p) ){
201837 sqlite3RCStrRef(z: p->zBuf);
201838 sqlite3_result_text64(pCtx: p->pCtx, z: p->zBuf, n: p->nUsed,
201839 xDel: (void(*)(void*))sqlite3RCStrUnref,
201840 SQLITE_UTF8);
201841 }
201842 }
201843 if( p->bErr==1 ){
201844 sqlite3_result_error_nomem(pCtx: p->pCtx);
201845 }
201846 jsonReset(p);
201847}
201848
201849/**************************************************************************
201850** Utility routines for dealing with JsonNode and JsonParse objects
201851**************************************************************************/
201852
201853/*
201854** Return the number of consecutive JsonNode slots need to represent
201855** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
201856** OBJECT types, the number might be larger.
201857**
201858** Appended elements are not counted. The value returned is the number
201859** by which the JsonNode counter should increment in order to go to the
201860** next peer value.
201861*/
201862static u32 jsonNodeSize(JsonNode *pNode){
201863 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
201864}
201865
201866/*
201867** Reclaim all memory allocated by a JsonParse object. But do not
201868** delete the JsonParse object itself.
201869*/
201870static void jsonParseReset(JsonParse *pParse){
201871 while( pParse->pClup ){
201872 JsonCleanup *pTask = pParse->pClup;
201873 pParse->pClup = pTask->pJCNext;
201874 pTask->xOp(pTask->pArg);
201875 sqlite3_free(p: pTask);
201876 }
201877 assert( pParse->nJPRef<=1 );
201878 if( pParse->aNode ){
201879 sqlite3_free(p: pParse->aNode);
201880 pParse->aNode = 0;
201881 }
201882 pParse->nNode = 0;
201883 pParse->nAlloc = 0;
201884 if( pParse->aUp ){
201885 sqlite3_free(p: pParse->aUp);
201886 pParse->aUp = 0;
201887 }
201888 if( pParse->bJsonIsRCStr ){
201889 sqlite3RCStrUnref(z: pParse->zJson);
201890 pParse->zJson = 0;
201891 pParse->bJsonIsRCStr = 0;
201892 }
201893 if( pParse->zAlt ){
201894 sqlite3RCStrUnref(z: pParse->zAlt);
201895 pParse->zAlt = 0;
201896 }
201897}
201898
201899/*
201900** Free a JsonParse object that was obtained from sqlite3_malloc().
201901**
201902** Note that destroying JsonParse might call sqlite3RCStrUnref() to
201903** destroy the zJson value. The RCStr object might recursively invoke
201904** JsonParse to destroy this pParse object again. Take care to ensure
201905** that this recursive destructor sequence terminates harmlessly.
201906*/
201907static void jsonParseFree(JsonParse *pParse){
201908 if( pParse->nJPRef>1 ){
201909 pParse->nJPRef--;
201910 }else{
201911 jsonParseReset(pParse);
201912 sqlite3_free(p: pParse);
201913 }
201914}
201915
201916/*
201917** Add a cleanup task to the JsonParse object.
201918**
201919** If an OOM occurs, the cleanup operation happens immediately
201920** and this function returns SQLITE_NOMEM.
201921*/
201922static int jsonParseAddCleanup(
201923 JsonParse *pParse, /* Add the cleanup task to this parser */
201924 void(*xOp)(void*), /* The cleanup task */
201925 void *pArg /* Argument to the cleanup */
201926){
201927 JsonCleanup *pTask = sqlite3_malloc64( n: sizeof(*pTask) );
201928 if( pTask==0 ){
201929 pParse->oom = 1;
201930 xOp(pArg);
201931 return SQLITE_ERROR;
201932 }
201933 pTask->pJCNext = pParse->pClup;
201934 pParse->pClup = pTask;
201935 pTask->xOp = xOp;
201936 pTask->pArg = pArg;
201937 return SQLITE_OK;
201938}
201939
201940/*
201941** Convert the JsonNode pNode into a pure JSON string and
201942** append to pOut. Subsubstructure is also included. Return
201943** the number of JsonNode objects that are encoded.
201944*/
201945static void jsonRenderNode(
201946 JsonParse *pParse, /* the complete parse of the JSON */
201947 JsonNode *pNode, /* The node to render */
201948 JsonString *pOut /* Write JSON here */
201949){
201950 assert( pNode!=0 );
201951 while( (pNode->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){
201952 u32 idx = (u32)(pNode - pParse->aNode);
201953 u32 i = pParse->iSubst;
201954 while( 1 /*exit-by-break*/ ){
201955 assert( i<pParse->nNode );
201956 assert( pParse->aNode[i].eType==JSON_SUBST );
201957 assert( pParse->aNode[i].eU==4 );
201958 assert( pParse->aNode[i].u.iPrev<i );
201959 if( pParse->aNode[i].n==idx ){
201960 pNode = &pParse->aNode[i+1];
201961 break;
201962 }
201963 i = pParse->aNode[i].u.iPrev;
201964 }
201965 }
201966 switch( pNode->eType ){
201967 default: {
201968 assert( pNode->eType==JSON_NULL );
201969 jsonAppendRawNZ(p: pOut, zIn: "null", N: 4);
201970 break;
201971 }
201972 case JSON_TRUE: {
201973 jsonAppendRawNZ(p: pOut, zIn: "true", N: 4);
201974 break;
201975 }
201976 case JSON_FALSE: {
201977 jsonAppendRawNZ(p: pOut, zIn: "false", N: 5);
201978 break;
201979 }
201980 case JSON_STRING: {
201981 assert( pNode->eU==1 );
201982 if( pNode->jnFlags & JNODE_RAW ){
201983 if( pNode->jnFlags & JNODE_LABEL ){
201984 jsonAppendChar(p: pOut, c: '"');
201985 jsonAppendRaw(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
201986 jsonAppendChar(p: pOut, c: '"');
201987 }else{
201988 jsonAppendString(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
201989 }
201990 }else if( pNode->jnFlags & JNODE_JSON5 ){
201991 jsonAppendNormalizedString(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
201992 }else{
201993 assert( pNode->n>0 );
201994 jsonAppendRawNZ(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
201995 }
201996 break;
201997 }
201998 case JSON_REAL: {
201999 assert( pNode->eU==1 );
202000 if( pNode->jnFlags & JNODE_JSON5 ){
202001 jsonAppendNormalizedReal(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
202002 }else{
202003 assert( pNode->n>0 );
202004 jsonAppendRawNZ(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
202005 }
202006 break;
202007 }
202008 case JSON_INT: {
202009 assert( pNode->eU==1 );
202010 if( pNode->jnFlags & JNODE_JSON5 ){
202011 jsonAppendNormalizedInt(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
202012 }else{
202013 assert( pNode->n>0 );
202014 jsonAppendRawNZ(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
202015 }
202016 break;
202017 }
202018 case JSON_ARRAY: {
202019 u32 j = 1;
202020 jsonAppendChar(p: pOut, c: '[');
202021 for(;;){
202022 while( j<=pNode->n ){
202023 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
202024 jsonAppendSeparator(p: pOut);
202025 jsonRenderNode(pParse, pNode: &pNode[j], pOut);
202026 }
202027 j += jsonNodeSize(pNode: &pNode[j]);
202028 }
202029 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
202030 if( pParse->useMod==0 ) break;
202031 assert( pNode->eU==2 );
202032 pNode = &pParse->aNode[pNode->u.iAppend];
202033 j = 1;
202034 }
202035 jsonAppendChar(p: pOut, c: ']');
202036 break;
202037 }
202038 case JSON_OBJECT: {
202039 u32 j = 1;
202040 jsonAppendChar(p: pOut, c: '{');
202041 for(;;){
202042 while( j<=pNode->n ){
202043 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
202044 jsonAppendSeparator(p: pOut);
202045 jsonRenderNode(pParse, pNode: &pNode[j], pOut);
202046 jsonAppendChar(p: pOut, c: ':');
202047 jsonRenderNode(pParse, pNode: &pNode[j+1], pOut);
202048 }
202049 j += 1 + jsonNodeSize(pNode: &pNode[j+1]);
202050 }
202051 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
202052 if( pParse->useMod==0 ) break;
202053 assert( pNode->eU==2 );
202054 pNode = &pParse->aNode[pNode->u.iAppend];
202055 j = 1;
202056 }
202057 jsonAppendChar(p: pOut, c: '}');
202058 break;
202059 }
202060 }
202061}
202062
202063/*
202064** Return a JsonNode and all its descendants as a JSON string.
202065*/
202066static void jsonReturnJson(
202067 JsonParse *pParse, /* The complete JSON */
202068 JsonNode *pNode, /* Node to return */
202069 sqlite3_context *pCtx, /* Return value for this function */
202070 int bGenerateAlt /* Also store the rendered text in zAlt */
202071){
202072 JsonString s;
202073 if( pParse->oom ){
202074 sqlite3_result_error_nomem(pCtx);
202075 return;
202076 }
202077 if( pParse->nErr==0 ){
202078 jsonInit(p: &s, pCtx);
202079 jsonRenderNode(pParse, pNode, pOut: &s);
202080 if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(p: &s) ){
202081 pParse->zAlt = sqlite3RCStrRef(z: s.zBuf);
202082 pParse->nAlt = s.nUsed;
202083 }
202084 jsonResult(p: &s);
202085 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
202086 }
202087}
202088
202089/*
202090** Translate a single byte of Hex into an integer.
202091** This routine only works if h really is a valid hexadecimal
202092** character: 0..9a..fA..F
202093*/
202094static u8 jsonHexToInt(int h){
202095 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
202096#ifdef SQLITE_EBCDIC
202097 h += 9*(1&~(h>>4));
202098#else
202099 h += 9*(1&(h>>6));
202100#endif
202101 return (u8)(h & 0xf);
202102}
202103
202104/*
202105** Convert a 4-byte hex string into an integer
202106*/
202107static u32 jsonHexToInt4(const char *z){
202108 u32 v;
202109 assert( sqlite3Isxdigit(z[0]) );
202110 assert( sqlite3Isxdigit(z[1]) );
202111 assert( sqlite3Isxdigit(z[2]) );
202112 assert( sqlite3Isxdigit(z[3]) );
202113 v = (jsonHexToInt(h: z[0])<<12)
202114 + (jsonHexToInt(h: z[1])<<8)
202115 + (jsonHexToInt(h: z[2])<<4)
202116 + jsonHexToInt(h: z[3]);
202117 return v;
202118}
202119
202120/*
202121** Make the JsonNode the return value of the function.
202122*/
202123static void jsonReturn(
202124 JsonParse *pParse, /* Complete JSON parse tree */
202125 JsonNode *pNode, /* Node to return */
202126 sqlite3_context *pCtx /* Return value for this function */
202127){
202128 switch( pNode->eType ){
202129 default: {
202130 assert( pNode->eType==JSON_NULL );
202131 sqlite3_result_null(pCtx);
202132 break;
202133 }
202134 case JSON_TRUE: {
202135 sqlite3_result_int(pCtx, iVal: 1);
202136 break;
202137 }
202138 case JSON_FALSE: {
202139 sqlite3_result_int(pCtx, iVal: 0);
202140 break;
202141 }
202142 case JSON_INT: {
202143 sqlite3_int64 i = 0;
202144 int rc;
202145 int bNeg = 0;
202146 const char *z;
202147
202148 assert( pNode->eU==1 );
202149 z = pNode->u.zJContent;
202150 if( z[0]=='-' ){ z++; bNeg = 1; }
202151 else if( z[0]=='+' ){ z++; }
202152 rc = sqlite3DecOrHexToI64(z, pOut: &i);
202153 if( rc<=1 ){
202154 sqlite3_result_int64(pCtx, iVal: bNeg ? -i : i);
202155 }else if( rc==3 && bNeg ){
202156 sqlite3_result_int64(pCtx, SMALLEST_INT64);
202157 }else{
202158 goto to_double;
202159 }
202160 break;
202161 }
202162 case JSON_REAL: {
202163 double r;
202164 const char *z;
202165 assert( pNode->eU==1 );
202166 to_double:
202167 z = pNode->u.zJContent;
202168 sqlite3AtoF(z, pResult: &r, length: sqlite3Strlen30(z), SQLITE_UTF8);
202169 sqlite3_result_double(pCtx, rVal: r);
202170 break;
202171 }
202172 case JSON_STRING: {
202173 if( pNode->jnFlags & JNODE_RAW ){
202174 assert( pNode->eU==1 );
202175 sqlite3_result_text(pCtx, z: pNode->u.zJContent, n: pNode->n,
202176 SQLITE_TRANSIENT);
202177 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
202178 /* JSON formatted without any backslash-escapes */
202179 assert( pNode->eU==1 );
202180 sqlite3_result_text(pCtx, z: pNode->u.zJContent+1, n: pNode->n-2,
202181 SQLITE_TRANSIENT);
202182 }else{
202183 /* Translate JSON formatted string into raw text */
202184 u32 i;
202185 u32 n = pNode->n;
202186 const char *z;
202187 char *zOut;
202188 u32 j;
202189 u32 nOut = n;
202190 assert( pNode->eU==1 );
202191 z = pNode->u.zJContent;
202192 zOut = sqlite3_malloc( n: nOut+1 );
202193 if( zOut==0 ){
202194 sqlite3_result_error_nomem(pCtx);
202195 break;
202196 }
202197 for(i=1, j=0; i<n-1; i++){
202198 char c = z[i];
202199 if( c=='\\' ){
202200 c = z[++i];
202201 if( c=='u' ){
202202 u32 v = jsonHexToInt4(z: z+i+1);
202203 i += 4;
202204 if( v==0 ) break;
202205 if( v<=0x7f ){
202206 zOut[j++] = (char)v;
202207 }else if( v<=0x7ff ){
202208 zOut[j++] = (char)(0xc0 | (v>>6));
202209 zOut[j++] = 0x80 | (v&0x3f);
202210 }else{
202211 u32 vlo;
202212 if( (v&0xfc00)==0xd800
202213 && i<n-6
202214 && z[i+1]=='\\'
202215 && z[i+2]=='u'
202216 && ((vlo = jsonHexToInt4(z: z+i+3))&0xfc00)==0xdc00
202217 ){
202218 /* We have a surrogate pair */
202219 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
202220 i += 6;
202221 zOut[j++] = 0xf0 | (v>>18);
202222 zOut[j++] = 0x80 | ((v>>12)&0x3f);
202223 zOut[j++] = 0x80 | ((v>>6)&0x3f);
202224 zOut[j++] = 0x80 | (v&0x3f);
202225 }else{
202226 zOut[j++] = 0xe0 | (v>>12);
202227 zOut[j++] = 0x80 | ((v>>6)&0x3f);
202228 zOut[j++] = 0x80 | (v&0x3f);
202229 }
202230 }
202231 continue;
202232 }else if( c=='b' ){
202233 c = '\b';
202234 }else if( c=='f' ){
202235 c = '\f';
202236 }else if( c=='n' ){
202237 c = '\n';
202238 }else if( c=='r' ){
202239 c = '\r';
202240 }else if( c=='t' ){
202241 c = '\t';
202242 }else if( c=='v' ){
202243 c = '\v';
202244 }else if( c=='\'' || c=='"' || c=='/' || c=='\\' ){
202245 /* pass through unchanged */
202246 }else if( c=='0' ){
202247 c = 0;
202248 }else if( c=='x' ){
202249 c = (jsonHexToInt(h: z[i+1])<<4) | jsonHexToInt(h: z[i+2]);
202250 i += 2;
202251 }else if( c=='\r' && z[i+1]=='\n' ){
202252 i++;
202253 continue;
202254 }else if( 0xe2==(u8)c ){
202255 assert( 0x80==(u8)z[i+1] );
202256 assert( 0xa8==(u8)z[i+2] || 0xa9==(u8)z[i+2] );
202257 i += 2;
202258 continue;
202259 }else{
202260 continue;
202261 }
202262 } /* end if( c=='\\' ) */
202263 zOut[j++] = c;
202264 } /* end for() */
202265 zOut[j] = 0;
202266 sqlite3_result_text(pCtx, z: zOut, n: j, xDel: sqlite3_free);
202267 }
202268 break;
202269 }
202270 case JSON_ARRAY:
202271 case JSON_OBJECT: {
202272 jsonReturnJson(pParse, pNode, pCtx, bGenerateAlt: 0);
202273 break;
202274 }
202275 }
202276}
202277
202278/* Forward reference */
202279static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
202280
202281/*
202282** A macro to hint to the compiler that a function should not be
202283** inlined.
202284*/
202285#if defined(__GNUC__)
202286# define JSON_NOINLINE __attribute__((noinline))
202287#elif defined(_MSC_VER) && _MSC_VER>=1310
202288# define JSON_NOINLINE __declspec(noinline)
202289#else
202290# define JSON_NOINLINE
202291#endif
202292
202293
202294/*
202295** Add a single node to pParse->aNode after first expanding the
202296** size of the aNode array. Return the index of the new node.
202297**
202298** If an OOM error occurs, set pParse->oom and return -1.
202299*/
202300static JSON_NOINLINE int jsonParseAddNodeExpand(
202301 JsonParse *pParse, /* Append the node to this object */
202302 u32 eType, /* Node type */
202303 u32 n, /* Content size or sub-node count */
202304 const char *zContent /* Content */
202305){
202306 u32 nNew;
202307 JsonNode *pNew;
202308 assert( pParse->nNode>=pParse->nAlloc );
202309 if( pParse->oom ) return -1;
202310 nNew = pParse->nAlloc*2 + 10;
202311 pNew = sqlite3_realloc64(pOld: pParse->aNode, n: sizeof(JsonNode)*nNew);
202312 if( pNew==0 ){
202313 pParse->oom = 1;
202314 return -1;
202315 }
202316 pParse->nAlloc = sqlite3_msize(p: pNew)/sizeof(JsonNode);
202317 pParse->aNode = pNew;
202318 assert( pParse->nNode<pParse->nAlloc );
202319 return jsonParseAddNode(pParse, eType, n, zContent);
202320}
202321
202322/*
202323** Create a new JsonNode instance based on the arguments and append that
202324** instance to the JsonParse. Return the index in pParse->aNode[] of the
202325** new node, or -1 if a memory allocation fails.
202326*/
202327static int jsonParseAddNode(
202328 JsonParse *pParse, /* Append the node to this object */
202329 u32 eType, /* Node type */
202330 u32 n, /* Content size or sub-node count */
202331 const char *zContent /* Content */
202332){
202333 JsonNode *p;
202334 assert( pParse->aNode!=0 || pParse->nNode>=pParse->nAlloc );
202335 if( pParse->nNode>=pParse->nAlloc ){
202336 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
202337 }
202338 assert( pParse->aNode!=0 );
202339 p = &pParse->aNode[pParse->nNode];
202340 assert( p!=0 );
202341 p->eType = (u8)(eType & 0xff);
202342 p->jnFlags = (u8)(eType >> 8);
202343 VVA( p->eU = zContent ? 1 : 0 );
202344 p->n = n;
202345 p->u.zJContent = zContent;
202346 return pParse->nNode++;
202347}
202348
202349/*
202350** Add an array of new nodes to the current pParse->aNode array.
202351** Return the index of the first node added.
202352**
202353** If an OOM error occurs, set pParse->oom.
202354*/
202355static void jsonParseAddNodeArray(
202356 JsonParse *pParse, /* Append the node to this object */
202357 JsonNode *aNode, /* Array of nodes to add */
202358 u32 nNode /* Number of elements in aNew */
202359){
202360 assert( aNode!=0 );
202361 assert( nNode>=1 );
202362 if( pParse->nNode + nNode > pParse->nAlloc ){
202363 u32 nNew = pParse->nNode + nNode;
202364 JsonNode *aNew = sqlite3_realloc64(pOld: pParse->aNode, n: nNew*sizeof(JsonNode));
202365 if( aNew==0 ){
202366 pParse->oom = 1;
202367 return;
202368 }
202369 pParse->nAlloc = sqlite3_msize(p: aNew)/sizeof(JsonNode);
202370 pParse->aNode = aNew;
202371 }
202372 memcpy(dest: &pParse->aNode[pParse->nNode], src: aNode, n: nNode*sizeof(JsonNode));
202373 pParse->nNode += nNode;
202374}
202375
202376/*
202377** Add a new JSON_SUBST node. The node immediately following
202378** this new node will be the substitute content for iNode.
202379*/
202380static int jsonParseAddSubstNode(
202381 JsonParse *pParse, /* Add the JSON_SUBST here */
202382 u32 iNode /* References this node */
202383){
202384 int idx = jsonParseAddNode(pParse, JSON_SUBST, n: iNode, zContent: 0);
202385 if( pParse->oom ) return -1;
202386 pParse->aNode[iNode].jnFlags |= JNODE_REPLACE;
202387 pParse->aNode[idx].eU = 4;
202388 pParse->aNode[idx].u.iPrev = pParse->iSubst;
202389 pParse->iSubst = idx;
202390 pParse->hasMod = 1;
202391 pParse->useMod = 1;
202392 return idx;
202393}
202394
202395/*
202396** Return true if z[] begins with 2 (or more) hexadecimal digits
202397*/
202398static int jsonIs2Hex(const char *z){
202399 return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]);
202400}
202401
202402/*
202403** Return true if z[] begins with 4 (or more) hexadecimal digits
202404*/
202405static int jsonIs4Hex(const char *z){
202406 return jsonIs2Hex(z) && jsonIs2Hex(z: &z[2]);
202407}
202408
202409/*
202410** Return the number of bytes of JSON5 whitespace at the beginning of
202411** the input string z[].
202412**
202413** JSON5 whitespace consists of any of the following characters:
202414**
202415** Unicode UTF-8 Name
202416** U+0009 09 horizontal tab
202417** U+000a 0a line feed
202418** U+000b 0b vertical tab
202419** U+000c 0c form feed
202420** U+000d 0d carriage return
202421** U+0020 20 space
202422** U+00a0 c2 a0 non-breaking space
202423** U+1680 e1 9a 80 ogham space mark
202424** U+2000 e2 80 80 en quad
202425** U+2001 e2 80 81 em quad
202426** U+2002 e2 80 82 en space
202427** U+2003 e2 80 83 em space
202428** U+2004 e2 80 84 three-per-em space
202429** U+2005 e2 80 85 four-per-em space
202430** U+2006 e2 80 86 six-per-em space
202431** U+2007 e2 80 87 figure space
202432** U+2008 e2 80 88 punctuation space
202433** U+2009 e2 80 89 thin space
202434** U+200a e2 80 8a hair space
202435** U+2028 e2 80 a8 line separator
202436** U+2029 e2 80 a9 paragraph separator
202437** U+202f e2 80 af narrow no-break space (NNBSP)
202438** U+205f e2 81 9f medium mathematical space (MMSP)
202439** U+3000 e3 80 80 ideographical space
202440** U+FEFF ef bb bf byte order mark
202441**
202442** In addition, comments between '/', '*' and '*', '/' and
202443** from '/', '/' to end-of-line are also considered to be whitespace.
202444*/
202445static int json5Whitespace(const char *zIn){
202446 int n = 0;
202447 const u8 *z = (u8*)zIn;
202448 while( 1 /*exit by "goto whitespace_done"*/ ){
202449 switch( z[n] ){
202450 case 0x09:
202451 case 0x0a:
202452 case 0x0b:
202453 case 0x0c:
202454 case 0x0d:
202455 case 0x20: {
202456 n++;
202457 break;
202458 }
202459 case '/': {
202460 if( z[n+1]=='*' && z[n+2]!=0 ){
202461 int j;
202462 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
202463 if( z[j]==0 ) goto whitespace_done;
202464 }
202465 n = j+1;
202466 break;
202467 }else if( z[n+1]=='/' ){
202468 int j;
202469 char c;
202470 for(j=n+2; (c = z[j])!=0; j++){
202471 if( c=='\n' || c=='\r' ) break;
202472 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
202473 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
202474 ){
202475 j += 2;
202476 break;
202477 }
202478 }
202479 n = j;
202480 if( z[n] ) n++;
202481 break;
202482 }
202483 goto whitespace_done;
202484 }
202485 case 0xc2: {
202486 if( z[n+1]==0xa0 ){
202487 n += 2;
202488 break;
202489 }
202490 goto whitespace_done;
202491 }
202492 case 0xe1: {
202493 if( z[n+1]==0x9a && z[n+2]==0x80 ){
202494 n += 3;
202495 break;
202496 }
202497 goto whitespace_done;
202498 }
202499 case 0xe2: {
202500 if( z[n+1]==0x80 ){
202501 u8 c = z[n+2];
202502 if( c<0x80 ) goto whitespace_done;
202503 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
202504 n += 3;
202505 break;
202506 }
202507 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
202508 n += 3;
202509 break;
202510 }
202511 goto whitespace_done;
202512 }
202513 case 0xe3: {
202514 if( z[n+1]==0x80 && z[n+2]==0x80 ){
202515 n += 3;
202516 break;
202517 }
202518 goto whitespace_done;
202519 }
202520 case 0xef: {
202521 if( z[n+1]==0xbb && z[n+2]==0xbf ){
202522 n += 3;
202523 break;
202524 }
202525 goto whitespace_done;
202526 }
202527 default: {
202528 goto whitespace_done;
202529 }
202530 }
202531 }
202532 whitespace_done:
202533 return n;
202534}
202535
202536/*
202537** Extra floating-point literals to allow in JSON.
202538*/
202539static const struct NanInfName {
202540 char c1;
202541 char c2;
202542 char n;
202543 char eType;
202544 char nRepl;
202545 char *zMatch;
202546 char *zRepl;
202547} aNanInfName[] = {
202548 { 'i', 'I', 3, JSON_REAL, 7, "inf", "9.0e999" },
202549 { 'i', 'I', 8, JSON_REAL, 7, "infinity", "9.0e999" },
202550 { 'n', 'N', 3, JSON_NULL, 4, "NaN", "null" },
202551 { 'q', 'Q', 4, JSON_NULL, 4, "QNaN", "null" },
202552 { 's', 'S', 4, JSON_NULL, 4, "SNaN", "null" },
202553};
202554
202555/*
202556** Parse a single JSON value which begins at pParse->zJson[i]. Return the
202557** index of the first character past the end of the value parsed.
202558**
202559** Special return values:
202560**
202561** 0 End of input
202562** -1 Syntax error
202563** -2 '}' seen
202564** -3 ']' seen
202565** -4 ',' seen
202566** -5 ':' seen
202567*/
202568static int jsonParseValue(JsonParse *pParse, u32 i){
202569 char c;
202570 u32 j;
202571 int iThis;
202572 int x;
202573 JsonNode *pNode;
202574 const char *z = pParse->zJson;
202575json_parse_restart:
202576 switch( (u8)z[i] ){
202577 case '{': {
202578 /* Parse object */
202579 iThis = jsonParseAddNode(pParse, JSON_OBJECT, n: 0, zContent: 0);
202580 if( iThis<0 ) return -1;
202581 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
202582 pParse->iErr = i;
202583 return -1;
202584 }
202585 for(j=i+1;;j++){
202586 u32 nNode = pParse->nNode;
202587 x = jsonParseValue(pParse, i: j);
202588 if( x<=0 ){
202589 if( x==(-2) ){
202590 j = pParse->iErr;
202591 if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1;
202592 break;
202593 }
202594 j += json5Whitespace(zIn: &z[j]);
202595 if( sqlite3JsonId1(z[j])
202596 || (z[j]=='\\' && z[j+1]=='u' && jsonIs4Hex(z: &z[j+2]))
202597 ){
202598 int k = j+1;
202599 while( (sqlite3JsonId2(z[k]) && json5Whitespace(zIn: &z[k])==0)
202600 || (z[k]=='\\' && z[k+1]=='u' && jsonIs4Hex(z: &z[k+2]))
202601 ){
202602 k++;
202603 }
202604 jsonParseAddNode(pParse, JSON_STRING | (JNODE_RAW<<8), n: k-j, zContent: &z[j]);
202605 pParse->hasNonstd = 1;
202606 x = k;
202607 }else{
202608 if( x!=-1 ) pParse->iErr = j;
202609 return -1;
202610 }
202611 }
202612 if( pParse->oom ) return -1;
202613 pNode = &pParse->aNode[nNode];
202614 if( pNode->eType!=JSON_STRING ){
202615 pParse->iErr = j;
202616 return -1;
202617 }
202618 pNode->jnFlags |= JNODE_LABEL;
202619 j = x;
202620 if( z[j]==':' ){
202621 j++;
202622 }else{
202623 if( fast_isspace(z[j]) ){
202624 do{ j++; }while( fast_isspace(z[j]) );
202625 if( z[j]==':' ){
202626 j++;
202627 goto parse_object_value;
202628 }
202629 }
202630 x = jsonParseValue(pParse, i: j);
202631 if( x!=(-5) ){
202632 if( x!=(-1) ) pParse->iErr = j;
202633 return -1;
202634 }
202635 j = pParse->iErr+1;
202636 }
202637 parse_object_value:
202638 x = jsonParseValue(pParse, i: j);
202639 if( x<=0 ){
202640 if( x!=(-1) ) pParse->iErr = j;
202641 return -1;
202642 }
202643 j = x;
202644 if( z[j]==',' ){
202645 continue;
202646 }else if( z[j]=='}' ){
202647 break;
202648 }else{
202649 if( fast_isspace(z[j]) ){
202650 do{ j++; }while( fast_isspace(z[j]) );
202651 if( z[j]==',' ){
202652 continue;
202653 }else if( z[j]=='}' ){
202654 break;
202655 }
202656 }
202657 x = jsonParseValue(pParse, i: j);
202658 if( x==(-4) ){
202659 j = pParse->iErr;
202660 continue;
202661 }
202662 if( x==(-2) ){
202663 j = pParse->iErr;
202664 break;
202665 }
202666 }
202667 pParse->iErr = j;
202668 return -1;
202669 }
202670 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
202671 pParse->iDepth--;
202672 return j+1;
202673 }
202674 case '[': {
202675 /* Parse array */
202676 iThis = jsonParseAddNode(pParse, JSON_ARRAY, n: 0, zContent: 0);
202677 if( iThis<0 ) return -1;
202678 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
202679 pParse->iErr = i;
202680 return -1;
202681 }
202682 memset(s: &pParse->aNode[iThis].u, c: 0, n: sizeof(pParse->aNode[iThis].u));
202683 for(j=i+1;;j++){
202684 x = jsonParseValue(pParse, i: j);
202685 if( x<=0 ){
202686 if( x==(-3) ){
202687 j = pParse->iErr;
202688 if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1;
202689 break;
202690 }
202691 if( x!=(-1) ) pParse->iErr = j;
202692 return -1;
202693 }
202694 j = x;
202695 if( z[j]==',' ){
202696 continue;
202697 }else if( z[j]==']' ){
202698 break;
202699 }else{
202700 if( fast_isspace(z[j]) ){
202701 do{ j++; }while( fast_isspace(z[j]) );
202702 if( z[j]==',' ){
202703 continue;
202704 }else if( z[j]==']' ){
202705 break;
202706 }
202707 }
202708 x = jsonParseValue(pParse, i: j);
202709 if( x==(-4) ){
202710 j = pParse->iErr;
202711 continue;
202712 }
202713 if( x==(-3) ){
202714 j = pParse->iErr;
202715 break;
202716 }
202717 }
202718 pParse->iErr = j;
202719 return -1;
202720 }
202721 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
202722 pParse->iDepth--;
202723 return j+1;
202724 }
202725 case '\'': {
202726 u8 jnFlags;
202727 char cDelim;
202728 pParse->hasNonstd = 1;
202729 jnFlags = JNODE_JSON5;
202730 goto parse_string;
202731 case '"':
202732 /* Parse string */
202733 jnFlags = 0;
202734 parse_string:
202735 cDelim = z[i];
202736 for(j=i+1; 1; j++){
202737 if( jsonIsOk[(unsigned char)z[j]] ) continue;
202738 c = z[j];
202739 if( c==cDelim ){
202740 break;
202741 }else if( c=='\\' ){
202742 c = z[++j];
202743 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
202744 || c=='n' || c=='r' || c=='t'
202745 || (c=='u' && jsonIs4Hex(z: &z[j+1])) ){
202746 jnFlags |= JNODE_ESCAPE;
202747 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
202748 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
202749 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
202750 || (c=='x' && jsonIs2Hex(z: &z[j+1])) ){
202751 jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
202752 pParse->hasNonstd = 1;
202753 }else if( c=='\r' ){
202754 if( z[j+1]=='\n' ) j++;
202755 jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
202756 pParse->hasNonstd = 1;
202757 }else{
202758 pParse->iErr = j;
202759 return -1;
202760 }
202761 }else if( c<=0x1f ){
202762 /* Control characters are not allowed in strings */
202763 pParse->iErr = j;
202764 return -1;
202765 }
202766 }
202767 jsonParseAddNode(pParse, JSON_STRING | (jnFlags<<8), n: j+1-i, zContent: &z[i]);
202768 return j+1;
202769 }
202770 case 't': {
202771 if( strncmp(s1: z+i,s2: "true",n: 4)==0 && !sqlite3Isalnum(z[i+4]) ){
202772 jsonParseAddNode(pParse, JSON_TRUE, n: 0, zContent: 0);
202773 return i+4;
202774 }
202775 pParse->iErr = i;
202776 return -1;
202777 }
202778 case 'f': {
202779 if( strncmp(s1: z+i,s2: "false",n: 5)==0 && !sqlite3Isalnum(z[i+5]) ){
202780 jsonParseAddNode(pParse, JSON_FALSE, n: 0, zContent: 0);
202781 return i+5;
202782 }
202783 pParse->iErr = i;
202784 return -1;
202785 }
202786 case '+': {
202787 u8 seenDP, seenE, jnFlags;
202788 pParse->hasNonstd = 1;
202789 jnFlags = JNODE_JSON5;
202790 goto parse_number;
202791 case '.':
202792 if( sqlite3Isdigit(z[i+1]) ){
202793 pParse->hasNonstd = 1;
202794 jnFlags = JNODE_JSON5;
202795 seenE = 0;
202796 seenDP = JSON_REAL;
202797 goto parse_number_2;
202798 }
202799 pParse->iErr = i;
202800 return -1;
202801 case '-':
202802 case '0':
202803 case '1':
202804 case '2':
202805 case '3':
202806 case '4':
202807 case '5':
202808 case '6':
202809 case '7':
202810 case '8':
202811 case '9':
202812 /* Parse number */
202813 jnFlags = 0;
202814 parse_number:
202815 seenDP = JSON_INT;
202816 seenE = 0;
202817 assert( '-' < '0' );
202818 assert( '+' < '0' );
202819 assert( '.' < '0' );
202820 c = z[i];
202821
202822 if( c<='0' ){
202823 if( c=='0' ){
202824 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
202825 assert( seenDP==JSON_INT );
202826 pParse->hasNonstd = 1;
202827 jnFlags |= JNODE_JSON5;
202828 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
202829 goto parse_number_finish;
202830 }else if( sqlite3Isdigit(z[i+1]) ){
202831 pParse->iErr = i+1;
202832 return -1;
202833 }
202834 }else{
202835 if( !sqlite3Isdigit(z[i+1]) ){
202836 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
202837 ** that case. SQLite also allows these in any case and it allows
202838 ** "+inf" and "-inf". */
202839 if( (z[i+1]=='I' || z[i+1]=='i')
202840 && sqlite3StrNICmp(zLeft: &z[i+1], zRight: "inf",N: 3)==0
202841 ){
202842 pParse->hasNonstd = 1;
202843 if( z[i]=='-' ){
202844 jsonParseAddNode(pParse, JSON_REAL, n: 8, zContent: "-9.0e999");
202845 }else{
202846 jsonParseAddNode(pParse, JSON_REAL, n: 7, zContent: "9.0e999");
202847 }
202848 return i + (sqlite3StrNICmp(zLeft: &z[i+4],zRight: "inity",N: 5)==0 ? 9 : 4);
202849 }
202850 if( z[i+1]=='.' ){
202851 pParse->hasNonstd = 1;
202852 jnFlags |= JNODE_JSON5;
202853 goto parse_number_2;
202854 }
202855 pParse->iErr = i;
202856 return -1;
202857 }
202858 if( z[i+1]=='0' ){
202859 if( sqlite3Isdigit(z[i+2]) ){
202860 pParse->iErr = i+1;
202861 return -1;
202862 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
202863 pParse->hasNonstd = 1;
202864 jnFlags |= JNODE_JSON5;
202865 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
202866 goto parse_number_finish;
202867 }
202868 }
202869 }
202870 }
202871 parse_number_2:
202872 for(j=i+1;; j++){
202873 c = z[j];
202874 if( sqlite3Isdigit(c) ) continue;
202875 if( c=='.' ){
202876 if( seenDP==JSON_REAL ){
202877 pParse->iErr = j;
202878 return -1;
202879 }
202880 seenDP = JSON_REAL;
202881 continue;
202882 }
202883 if( c=='e' || c=='E' ){
202884 if( z[j-1]<'0' ){
202885 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
202886 pParse->hasNonstd = 1;
202887 jnFlags |= JNODE_JSON5;
202888 }else{
202889 pParse->iErr = j;
202890 return -1;
202891 }
202892 }
202893 if( seenE ){
202894 pParse->iErr = j;
202895 return -1;
202896 }
202897 seenDP = JSON_REAL;
202898 seenE = 1;
202899 c = z[j+1];
202900 if( c=='+' || c=='-' ){
202901 j++;
202902 c = z[j+1];
202903 }
202904 if( c<'0' || c>'9' ){
202905 pParse->iErr = j;
202906 return -1;
202907 }
202908 continue;
202909 }
202910 break;
202911 }
202912 if( z[j-1]<'0' ){
202913 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
202914 pParse->hasNonstd = 1;
202915 jnFlags |= JNODE_JSON5;
202916 }else{
202917 pParse->iErr = j;
202918 return -1;
202919 }
202920 }
202921 parse_number_finish:
202922 jsonParseAddNode(pParse, eType: seenDP | (jnFlags<<8), n: j - i, zContent: &z[i]);
202923 return j;
202924 }
202925 case '}': {
202926 pParse->iErr = i;
202927 return -2; /* End of {...} */
202928 }
202929 case ']': {
202930 pParse->iErr = i;
202931 return -3; /* End of [...] */
202932 }
202933 case ',': {
202934 pParse->iErr = i;
202935 return -4; /* List separator */
202936 }
202937 case ':': {
202938 pParse->iErr = i;
202939 return -5; /* Object label/value separator */
202940 }
202941 case 0: {
202942 return 0; /* End of file */
202943 }
202944 case 0x09:
202945 case 0x0a:
202946 case 0x0d:
202947 case 0x20: {
202948 do{
202949 i++;
202950 }while( fast_isspace(z[i]) );
202951 goto json_parse_restart;
202952 }
202953 case 0x0b:
202954 case 0x0c:
202955 case '/':
202956 case 0xc2:
202957 case 0xe1:
202958 case 0xe2:
202959 case 0xe3:
202960 case 0xef: {
202961 j = json5Whitespace(zIn: &z[i]);
202962 if( j>0 ){
202963 i += j;
202964 pParse->hasNonstd = 1;
202965 goto json_parse_restart;
202966 }
202967 pParse->iErr = i;
202968 return -1;
202969 }
202970 case 'n': {
202971 if( strncmp(s1: z+i,s2: "null",n: 4)==0 && !sqlite3Isalnum(z[i+4]) ){
202972 jsonParseAddNode(pParse, JSON_NULL, n: 0, zContent: 0);
202973 return i+4;
202974 }
202975 /* fall-through into the default case that checks for NaN */
202976 }
202977 default: {
202978 u32 k;
202979 int nn;
202980 c = z[i];
202981 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
202982 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
202983 nn = aNanInfName[k].n;
202984 if( sqlite3StrNICmp(zLeft: &z[i], zRight: aNanInfName[k].zMatch, N: nn)!=0 ){
202985 continue;
202986 }
202987 if( sqlite3Isalnum(z[i+nn]) ) continue;
202988 jsonParseAddNode(pParse, eType: aNanInfName[k].eType,
202989 n: aNanInfName[k].nRepl, zContent: aNanInfName[k].zRepl);
202990 pParse->hasNonstd = 1;
202991 return i + nn;
202992 }
202993 pParse->iErr = i;
202994 return -1; /* Syntax error */
202995 }
202996 } /* End switch(z[i]) */
202997}
202998
202999/*
203000** Parse a complete JSON string. Return 0 on success or non-zero if there
203001** are any errors. If an error occurs, free all memory held by pParse,
203002** but not pParse itself.
203003**
203004** pParse must be initialized to an empty parse object prior to calling
203005** this routine.
203006*/
203007static int jsonParse(
203008 JsonParse *pParse, /* Initialize and fill this JsonParse object */
203009 sqlite3_context *pCtx /* Report errors here */
203010){
203011 int i;
203012 const char *zJson = pParse->zJson;
203013 i = jsonParseValue(pParse, i: 0);
203014 if( pParse->oom ) i = -1;
203015 if( i>0 ){
203016 assert( pParse->iDepth==0 );
203017 while( fast_isspace(zJson[i]) ) i++;
203018 if( zJson[i] ){
203019 i += json5Whitespace(zIn: &zJson[i]);
203020 if( zJson[i] ){
203021 jsonParseReset(pParse);
203022 return 1;
203023 }
203024 pParse->hasNonstd = 1;
203025 }
203026 }
203027 if( i<=0 ){
203028 if( pCtx!=0 ){
203029 if( pParse->oom ){
203030 sqlite3_result_error_nomem(pCtx);
203031 }else{
203032 sqlite3_result_error(pCtx, z: "malformed JSON", n: -1);
203033 }
203034 }
203035 jsonParseReset(pParse);
203036 return 1;
203037 }
203038 return 0;
203039}
203040
203041
203042/* Mark node i of pParse as being a child of iParent. Call recursively
203043** to fill in all the descendants of node i.
203044*/
203045static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
203046 JsonNode *pNode = &pParse->aNode[i];
203047 u32 j;
203048 pParse->aUp[i] = iParent;
203049 switch( pNode->eType ){
203050 case JSON_ARRAY: {
203051 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode: pNode+j)){
203052 jsonParseFillInParentage(pParse, i: i+j, iParent: i);
203053 }
203054 break;
203055 }
203056 case JSON_OBJECT: {
203057 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode: pNode+j+1)+1){
203058 pParse->aUp[i+j] = i;
203059 jsonParseFillInParentage(pParse, i: i+j+1, iParent: i);
203060 }
203061 break;
203062 }
203063 default: {
203064 break;
203065 }
203066 }
203067}
203068
203069/*
203070** Compute the parentage of all nodes in a completed parse.
203071*/
203072static int jsonParseFindParents(JsonParse *pParse){
203073 u32 *aUp;
203074 assert( pParse->aUp==0 );
203075 aUp = pParse->aUp = sqlite3_malloc64( n: sizeof(u32)*pParse->nNode );
203076 if( aUp==0 ){
203077 pParse->oom = 1;
203078 return SQLITE_NOMEM;
203079 }
203080 jsonParseFillInParentage(pParse, i: 0, iParent: 0);
203081 return SQLITE_OK;
203082}
203083
203084/*
203085** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
203086*/
203087#define JSON_CACHE_ID (-429938) /* First cache entry */
203088#define JSON_CACHE_SZ 4 /* Max number of cache entries */
203089
203090/*
203091** Obtain a complete parse of the JSON found in the pJson argument
203092**
203093** Use the sqlite3_get_auxdata() cache to find a preexisting parse
203094** if it is available. If the cache is not available or if it
203095** is no longer valid, parse the JSON again and return the new parse.
203096** Also register the new parse so that it will be available for
203097** future sqlite3_get_auxdata() calls.
203098**
203099** If an error occurs and pErrCtx!=0 then report the error on pErrCtx
203100** and return NULL.
203101**
203102** The returned pointer (if it is not NULL) is owned by the cache in
203103** most cases, not the caller. The caller does NOT need to invoke
203104** jsonParseFree(), in most cases.
203105**
203106** Except, if an error occurs and pErrCtx==0 then return the JsonParse
203107** object with JsonParse.nErr non-zero and the caller will own the JsonParse
203108** object. In that case, it will be the responsibility of the caller to
203109** invoke jsonParseFree(). To summarize:
203110**
203111** pErrCtx!=0 || p->nErr==0 ==> Return value p is owned by the
203112** cache. Call does not need to
203113** free it.
203114**
203115** pErrCtx==0 && p->nErr!=0 ==> Return value is owned by the caller
203116** and so the caller must free it.
203117*/
203118static JsonParse *jsonParseCached(
203119 sqlite3_context *pCtx, /* Context to use for cache search */
203120 sqlite3_value *pJson, /* Function param containing JSON text */
203121 sqlite3_context *pErrCtx, /* Write parse errors here if not NULL */
203122 int bUnedited /* No prior edits allowed */
203123){
203124 char *zJson = (char*)sqlite3_value_text(pVal: pJson);
203125 int nJson = sqlite3_value_bytes(pVal: pJson);
203126 JsonParse *p;
203127 JsonParse *pMatch = 0;
203128 int iKey;
203129 int iMinKey = 0;
203130 u32 iMinHold = 0xffffffff;
203131 u32 iMaxHold = 0;
203132 int bJsonRCStr;
203133
203134 if( zJson==0 ) return 0;
203135 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
203136 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
203137 if( p==0 ){
203138 iMinKey = iKey;
203139 break;
203140 }
203141 if( pMatch==0
203142 && p->nJson==nJson
203143 && (p->hasMod==0 || bUnedited==0)
203144 && (p->zJson==zJson || memcmp(s1: p->zJson,s2: zJson,n: nJson)==0)
203145 ){
203146 p->nErr = 0;
203147 p->useMod = 0;
203148 pMatch = p;
203149 }else
203150 if( pMatch==0
203151 && p->zAlt!=0
203152 && bUnedited==0
203153 && p->nAlt==nJson
203154 && memcmp(s1: p->zAlt, s2: zJson, n: nJson)==0
203155 ){
203156 p->nErr = 0;
203157 p->useMod = 1;
203158 pMatch = p;
203159 }else if( p->iHold<iMinHold ){
203160 iMinHold = p->iHold;
203161 iMinKey = iKey;
203162 }
203163 if( p->iHold>iMaxHold ){
203164 iMaxHold = p->iHold;
203165 }
203166 }
203167 if( pMatch ){
203168 /* The input JSON text was found in the cache. Use the preexisting
203169 ** parse of this JSON */
203170 pMatch->nErr = 0;
203171 pMatch->iHold = iMaxHold+1;
203172 assert( pMatch->nJPRef>0 ); /* pMatch is owned by the cache */
203173 return pMatch;
203174 }
203175
203176 /* The input JSON was not found anywhere in the cache. We will need
203177 ** to parse it ourselves and generate a new JsonParse object.
203178 */
203179 bJsonRCStr = sqlite3ValueIsOfClass(pVal: pJson,xFree: (void(*)(void*))sqlite3RCStrUnref);
203180 p = sqlite3_malloc64( n: sizeof(*p) + (bJsonRCStr ? 0 : nJson+1) );
203181 if( p==0 ){
203182 sqlite3_result_error_nomem(pCtx);
203183 return 0;
203184 }
203185 memset(s: p, c: 0, n: sizeof(*p));
203186 if( bJsonRCStr ){
203187 p->zJson = sqlite3RCStrRef(z: zJson);
203188 p->bJsonIsRCStr = 1;
203189 }else{
203190 p->zJson = (char*)&p[1];
203191 memcpy(dest: p->zJson, src: zJson, n: nJson+1);
203192 }
203193 p->nJPRef = 1;
203194 if( jsonParse(pParse: p, pCtx: pErrCtx) ){
203195 if( pErrCtx==0 ){
203196 p->nErr = 1;
203197 assert( p->nJPRef==1 ); /* Caller will own the new JsonParse object p */
203198 return p;
203199 }
203200 jsonParseFree(pParse: p);
203201 return 0;
203202 }
203203 p->nJson = nJson;
203204 p->iHold = iMaxHold+1;
203205 /* Transfer ownership of the new JsonParse to the cache */
203206 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, pAux: p,
203207 xDelete: (void(*)(void*))jsonParseFree);
203208 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
203209}
203210
203211/*
203212** Compare the OBJECT label at pNode against zKey,nKey. Return true on
203213** a match.
203214*/
203215static int jsonLabelCompare(const JsonNode *pNode, const char *zKey, u32 nKey){
203216 assert( pNode->eU==1 );
203217 if( pNode->jnFlags & JNODE_RAW ){
203218 if( pNode->n!=nKey ) return 0;
203219 return strncmp(s1: pNode->u.zJContent, s2: zKey, n: nKey)==0;
203220 }else{
203221 if( pNode->n!=nKey+2 ) return 0;
203222 return strncmp(s1: pNode->u.zJContent+1, s2: zKey, n: nKey)==0;
203223 }
203224}
203225static int jsonSameLabel(const JsonNode *p1, const JsonNode *p2){
203226 if( p1->jnFlags & JNODE_RAW ){
203227 return jsonLabelCompare(pNode: p2, zKey: p1->u.zJContent, nKey: p1->n);
203228 }else if( p2->jnFlags & JNODE_RAW ){
203229 return jsonLabelCompare(pNode: p1, zKey: p2->u.zJContent, nKey: p2->n);
203230 }else{
203231 return p1->n==p2->n && strncmp(s1: p1->u.zJContent,s2: p2->u.zJContent,n: p1->n)==0;
203232 }
203233}
203234
203235/* forward declaration */
203236static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
203237
203238/*
203239** Search along zPath to find the node specified. Return a pointer
203240** to that node, or NULL if zPath is malformed or if there is no such
203241** node.
203242**
203243** If pApnd!=0, then try to append new nodes to complete zPath if it is
203244** possible to do so and if no existing node corresponds to zPath. If
203245** new nodes are appended *pApnd is set to 1.
203246*/
203247static JsonNode *jsonLookupStep(
203248 JsonParse *pParse, /* The JSON to search */
203249 u32 iRoot, /* Begin the search at this node */
203250 const char *zPath, /* The path to search */
203251 int *pApnd, /* Append nodes to complete path if not NULL */
203252 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
203253){
203254 u32 i, j, nKey;
203255 const char *zKey;
203256 JsonNode *pRoot;
203257 if( pParse->oom ) return 0;
203258 pRoot = &pParse->aNode[iRoot];
203259 if( pRoot->jnFlags & (JNODE_REPLACE|JNODE_REMOVE) && pParse->useMod ){
203260 while( (pRoot->jnFlags & JNODE_REPLACE)!=0 ){
203261 u32 idx = (u32)(pRoot - pParse->aNode);
203262 i = pParse->iSubst;
203263 while( 1 /*exit-by-break*/ ){
203264 assert( i<pParse->nNode );
203265 assert( pParse->aNode[i].eType==JSON_SUBST );
203266 assert( pParse->aNode[i].eU==4 );
203267 assert( pParse->aNode[i].u.iPrev<i );
203268 if( pParse->aNode[i].n==idx ){
203269 pRoot = &pParse->aNode[i+1];
203270 iRoot = i+1;
203271 break;
203272 }
203273 i = pParse->aNode[i].u.iPrev;
203274 }
203275 }
203276 if( pRoot->jnFlags & JNODE_REMOVE ){
203277 return 0;
203278 }
203279 }
203280 if( zPath[0]==0 ) return pRoot;
203281 if( zPath[0]=='.' ){
203282 if( pRoot->eType!=JSON_OBJECT ) return 0;
203283 zPath++;
203284 if( zPath[0]=='"' ){
203285 zKey = zPath + 1;
203286 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
203287 nKey = i-1;
203288 if( zPath[i] ){
203289 i++;
203290 }else{
203291 *pzErr = zPath;
203292 return 0;
203293 }
203294 testcase( nKey==0 );
203295 }else{
203296 zKey = zPath;
203297 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
203298 nKey = i;
203299 if( nKey==0 ){
203300 *pzErr = zPath;
203301 return 0;
203302 }
203303 }
203304 j = 1;
203305 for(;;){
203306 while( j<=pRoot->n ){
203307 if( jsonLabelCompare(pNode: pRoot+j, zKey, nKey) ){
203308 return jsonLookupStep(pParse, iRoot: iRoot+j+1, zPath: &zPath[i], pApnd, pzErr);
203309 }
203310 j++;
203311 j += jsonNodeSize(pNode: &pRoot[j]);
203312 }
203313 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
203314 if( pParse->useMod==0 ) break;
203315 assert( pRoot->eU==2 );
203316 iRoot = pRoot->u.iAppend;
203317 pRoot = &pParse->aNode[iRoot];
203318 j = 1;
203319 }
203320 if( pApnd ){
203321 u32 iStart, iLabel;
203322 JsonNode *pNode;
203323 assert( pParse->useMod );
203324 iStart = jsonParseAddNode(pParse, JSON_OBJECT, n: 2, zContent: 0);
203325 iLabel = jsonParseAddNode(pParse, JSON_STRING, n: nKey, zContent: zKey);
203326 zPath += i;
203327 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
203328 if( pParse->oom ) return 0;
203329 if( pNode ){
203330 pRoot = &pParse->aNode[iRoot];
203331 assert( pRoot->eU==0 );
203332 pRoot->u.iAppend = iStart;
203333 pRoot->jnFlags |= JNODE_APPEND;
203334 VVA( pRoot->eU = 2 );
203335 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
203336 }
203337 return pNode;
203338 }
203339 }else if( zPath[0]=='[' ){
203340 i = 0;
203341 j = 1;
203342 while( sqlite3Isdigit(zPath[j]) ){
203343 i = i*10 + zPath[j] - '0';
203344 j++;
203345 }
203346 if( j<2 || zPath[j]!=']' ){
203347 if( zPath[1]=='#' ){
203348 JsonNode *pBase = pRoot;
203349 int iBase = iRoot;
203350 if( pRoot->eType!=JSON_ARRAY ) return 0;
203351 for(;;){
203352 while( j<=pBase->n ){
203353 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i++;
203354 j += jsonNodeSize(pNode: &pBase[j]);
203355 }
203356 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
203357 if( pParse->useMod==0 ) break;
203358 assert( pBase->eU==2 );
203359 iBase = pBase->u.iAppend;
203360 pBase = &pParse->aNode[iBase];
203361 j = 1;
203362 }
203363 j = 2;
203364 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
203365 unsigned int x = 0;
203366 j = 3;
203367 do{
203368 x = x*10 + zPath[j] - '0';
203369 j++;
203370 }while( sqlite3Isdigit(zPath[j]) );
203371 if( x>i ) return 0;
203372 i -= x;
203373 }
203374 if( zPath[j]!=']' ){
203375 *pzErr = zPath;
203376 return 0;
203377 }
203378 }else{
203379 *pzErr = zPath;
203380 return 0;
203381 }
203382 }
203383 if( pRoot->eType!=JSON_ARRAY ) return 0;
203384 zPath += j + 1;
203385 j = 1;
203386 for(;;){
203387 while( j<=pRoot->n
203388 && (i>0 || ((pRoot[j].jnFlags & JNODE_REMOVE)!=0 && pParse->useMod))
203389 ){
203390 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i--;
203391 j += jsonNodeSize(pNode: &pRoot[j]);
203392 }
203393 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
203394 if( pParse->useMod==0 ) break;
203395 assert( pRoot->eU==2 );
203396 iRoot = pRoot->u.iAppend;
203397 pRoot = &pParse->aNode[iRoot];
203398 j = 1;
203399 }
203400 if( j<=pRoot->n ){
203401 return jsonLookupStep(pParse, iRoot: iRoot+j, zPath, pApnd, pzErr);
203402 }
203403 if( i==0 && pApnd ){
203404 u32 iStart;
203405 JsonNode *pNode;
203406 assert( pParse->useMod );
203407 iStart = jsonParseAddNode(pParse, JSON_ARRAY, n: 1, zContent: 0);
203408 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
203409 if( pParse->oom ) return 0;
203410 if( pNode ){
203411 pRoot = &pParse->aNode[iRoot];
203412 assert( pRoot->eU==0 );
203413 pRoot->u.iAppend = iStart;
203414 pRoot->jnFlags |= JNODE_APPEND;
203415 VVA( pRoot->eU = 2 );
203416 }
203417 return pNode;
203418 }
203419 }else{
203420 *pzErr = zPath;
203421 }
203422 return 0;
203423}
203424
203425/*
203426** Append content to pParse that will complete zPath. Return a pointer
203427** to the inserted node, or return NULL if the append fails.
203428*/
203429static JsonNode *jsonLookupAppend(
203430 JsonParse *pParse, /* Append content to the JSON parse */
203431 const char *zPath, /* Description of content to append */
203432 int *pApnd, /* Set this flag to 1 */
203433 const char **pzErr /* Make this point to any syntax error */
203434){
203435 *pApnd = 1;
203436 if( zPath[0]==0 ){
203437 jsonParseAddNode(pParse, JSON_NULL, n: 0, zContent: 0);
203438 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
203439 }
203440 if( zPath[0]=='.' ){
203441 jsonParseAddNode(pParse, JSON_OBJECT, n: 0, zContent: 0);
203442 }else if( strncmp(s1: zPath,s2: "[0]",n: 3)==0 ){
203443 jsonParseAddNode(pParse, JSON_ARRAY, n: 0, zContent: 0);
203444 }else{
203445 return 0;
203446 }
203447 if( pParse->oom ) return 0;
203448 return jsonLookupStep(pParse, iRoot: pParse->nNode-1, zPath, pApnd, pzErr);
203449}
203450
203451/*
203452** Return the text of a syntax error message on a JSON path. Space is
203453** obtained from sqlite3_malloc().
203454*/
203455static char *jsonPathSyntaxError(const char *zErr){
203456 return sqlite3_mprintf(zFormat: "JSON path error near '%q'", zErr);
203457}
203458
203459/*
203460** Do a node lookup using zPath. Return a pointer to the node on success.
203461** Return NULL if not found or if there is an error.
203462**
203463** On an error, write an error message into pCtx and increment the
203464** pParse->nErr counter.
203465**
203466** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
203467** nodes are appended.
203468*/
203469static JsonNode *jsonLookup(
203470 JsonParse *pParse, /* The JSON to search */
203471 const char *zPath, /* The path to search */
203472 int *pApnd, /* Append nodes to complete path if not NULL */
203473 sqlite3_context *pCtx /* Report errors here, if not NULL */
203474){
203475 const char *zErr = 0;
203476 JsonNode *pNode = 0;
203477 char *zMsg;
203478
203479 if( zPath==0 ) return 0;
203480 if( zPath[0]!='$' ){
203481 zErr = zPath;
203482 goto lookup_err;
203483 }
203484 zPath++;
203485 pNode = jsonLookupStep(pParse, iRoot: 0, zPath, pApnd, pzErr: &zErr);
203486 if( zErr==0 ) return pNode;
203487
203488lookup_err:
203489 pParse->nErr++;
203490 assert( zErr!=0 && pCtx!=0 );
203491 zMsg = jsonPathSyntaxError(zErr);
203492 if( zMsg ){
203493 sqlite3_result_error(pCtx, z: zMsg, n: -1);
203494 sqlite3_free(p: zMsg);
203495 }else{
203496 sqlite3_result_error_nomem(pCtx);
203497 }
203498 return 0;
203499}
203500
203501
203502/*
203503** Report the wrong number of arguments for json_insert(), json_replace()
203504** or json_set().
203505*/
203506static void jsonWrongNumArgs(
203507 sqlite3_context *pCtx,
203508 const char *zFuncName
203509){
203510 char *zMsg = sqlite3_mprintf(zFormat: "json_%s() needs an odd number of arguments",
203511 zFuncName);
203512 sqlite3_result_error(pCtx, z: zMsg, n: -1);
203513 sqlite3_free(p: zMsg);
203514}
203515
203516/*
203517** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
203518*/
203519static void jsonRemoveAllNulls(JsonNode *pNode){
203520 int i, n;
203521 assert( pNode->eType==JSON_OBJECT );
203522 n = pNode->n;
203523 for(i=2; i<=n; i += jsonNodeSize(pNode: &pNode[i])+1){
203524 switch( pNode[i].eType ){
203525 case JSON_NULL:
203526 pNode[i].jnFlags |= JNODE_REMOVE;
203527 break;
203528 case JSON_OBJECT:
203529 jsonRemoveAllNulls(pNode: &pNode[i]);
203530 break;
203531 }
203532 }
203533}
203534
203535
203536/****************************************************************************
203537** SQL functions used for testing and debugging
203538****************************************************************************/
203539
203540#if SQLITE_DEBUG
203541/*
203542** Print N node entries.
203543*/
203544static void jsonDebugPrintNodeEntries(
203545 JsonNode *aNode, /* First node entry to print */
203546 int N /* Number of node entries to print */
203547){
203548 int i;
203549 for(i=0; i<N; i++){
203550 const char *zType;
203551 if( aNode[i].jnFlags & JNODE_LABEL ){
203552 zType = "label";
203553 }else{
203554 zType = jsonType[aNode[i].eType];
203555 }
203556 printf("node %4u: %-7s n=%-5d", i, zType, aNode[i].n);
203557 if( (aNode[i].jnFlags & ~JNODE_LABEL)!=0 ){
203558 u8 f = aNode[i].jnFlags;
203559 if( f & JNODE_RAW ) printf(" RAW");
203560 if( f & JNODE_ESCAPE ) printf(" ESCAPE");
203561 if( f & JNODE_REMOVE ) printf(" REMOVE");
203562 if( f & JNODE_REPLACE ) printf(" REPLACE");
203563 if( f & JNODE_APPEND ) printf(" APPEND");
203564 if( f & JNODE_JSON5 ) printf(" JSON5");
203565 }
203566 switch( aNode[i].eU ){
203567 case 1: printf(" zJContent=[%.*s]\n",
203568 aNode[i].n, aNode[i].u.zJContent); break;
203569 case 2: printf(" iAppend=%u\n", aNode[i].u.iAppend); break;
203570 case 3: printf(" iKey=%u\n", aNode[i].u.iKey); break;
203571 case 4: printf(" iPrev=%u\n", aNode[i].u.iPrev); break;
203572 default: printf("\n");
203573 }
203574 }
203575}
203576#endif /* SQLITE_DEBUG */
203577
203578
203579#if 0 /* 1 for debugging. 0 normally. Requires -DSQLITE_DEBUG too */
203580static void jsonDebugPrintParse(JsonParse *p){
203581 jsonDebugPrintNodeEntries(p->aNode, p->nNode);
203582}
203583static void jsonDebugPrintNode(JsonNode *pNode){
203584 jsonDebugPrintNodeEntries(pNode, jsonNodeSize(pNode));
203585}
203586#else
203587 /* The usual case */
203588# define jsonDebugPrintNode(X)
203589# define jsonDebugPrintParse(X)
203590#endif
203591
203592#ifdef SQLITE_DEBUG
203593/*
203594** SQL function: json_parse(JSON)
203595**
203596** Parse JSON using jsonParseCached(). Then print a dump of that
203597** parse on standard output. Return the mimified JSON result, just
203598** like the json() function.
203599*/
203600static void jsonParseFunc(
203601 sqlite3_context *ctx,
203602 int argc,
203603 sqlite3_value **argv
203604){
203605 JsonParse *p; /* The parse */
203606
203607 assert( argc==1 );
203608 p = jsonParseCached(ctx, argv[0], ctx, 0);
203609 if( p==0 ) return;
203610 printf("nNode = %u\n", p->nNode);
203611 printf("nAlloc = %u\n", p->nAlloc);
203612 printf("nJson = %d\n", p->nJson);
203613 printf("nAlt = %d\n", p->nAlt);
203614 printf("nErr = %u\n", p->nErr);
203615 printf("oom = %u\n", p->oom);
203616 printf("hasNonstd = %u\n", p->hasNonstd);
203617 printf("useMod = %u\n", p->useMod);
203618 printf("hasMod = %u\n", p->hasMod);
203619 printf("nJPRef = %u\n", p->nJPRef);
203620 printf("iSubst = %u\n", p->iSubst);
203621 printf("iHold = %u\n", p->iHold);
203622 jsonDebugPrintNodeEntries(p->aNode, p->nNode);
203623 jsonReturnJson(p, p->aNode, ctx, 1);
203624}
203625
203626/*
203627** The json_test1(JSON) function return true (1) if the input is JSON
203628** text generated by another json function. It returns (0) if the input
203629** is not known to be JSON.
203630*/
203631static void jsonTest1Func(
203632 sqlite3_context *ctx,
203633 int argc,
203634 sqlite3_value **argv
203635){
203636 UNUSED_PARAMETER(argc);
203637 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
203638}
203639#endif /* SQLITE_DEBUG */
203640
203641/****************************************************************************
203642** Scalar SQL function implementations
203643****************************************************************************/
203644
203645/*
203646** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
203647** corresponding to the SQL value input. Mostly this means putting
203648** double-quotes around strings and returning the unquoted string "null"
203649** when given a NULL input.
203650*/
203651static void jsonQuoteFunc(
203652 sqlite3_context *ctx,
203653 int argc,
203654 sqlite3_value **argv
203655){
203656 JsonString jx;
203657 UNUSED_PARAMETER(argc);
203658
203659 jsonInit(p: &jx, pCtx: ctx);
203660 jsonAppendValue(p: &jx, pValue: argv[0]);
203661 jsonResult(p: &jx);
203662 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
203663}
203664
203665/*
203666** Implementation of the json_array(VALUE,...) function. Return a JSON
203667** array that contains all values given in arguments. Or if any argument
203668** is a BLOB, throw an error.
203669*/
203670static void jsonArrayFunc(
203671 sqlite3_context *ctx,
203672 int argc,
203673 sqlite3_value **argv
203674){
203675 int i;
203676 JsonString jx;
203677
203678 jsonInit(p: &jx, pCtx: ctx);
203679 jsonAppendChar(p: &jx, c: '[');
203680 for(i=0; i<argc; i++){
203681 jsonAppendSeparator(p: &jx);
203682 jsonAppendValue(p: &jx, pValue: argv[i]);
203683 }
203684 jsonAppendChar(p: &jx, c: ']');
203685 jsonResult(p: &jx);
203686 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
203687}
203688
203689
203690/*
203691** json_array_length(JSON)
203692** json_array_length(JSON, PATH)
203693**
203694** Return the number of elements in the top-level JSON array.
203695** Return 0 if the input is not a well-formed JSON array.
203696*/
203697static void jsonArrayLengthFunc(
203698 sqlite3_context *ctx,
203699 int argc,
203700 sqlite3_value **argv
203701){
203702 JsonParse *p; /* The parse */
203703 sqlite3_int64 n = 0;
203704 u32 i;
203705 JsonNode *pNode;
203706
203707 p = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: 0);
203708 if( p==0 ) return;
203709 assert( p->nNode );
203710 if( argc==2 ){
203711 const char *zPath = (const char*)sqlite3_value_text(pVal: argv[1]);
203712 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
203713 }else{
203714 pNode = p->aNode;
203715 }
203716 if( pNode==0 ){
203717 return;
203718 }
203719 if( pNode->eType==JSON_ARRAY ){
203720 while( 1 /*exit-by-break*/ ){
203721 i = 1;
203722 while( i<=pNode->n ){
203723 if( (pNode[i].jnFlags & JNODE_REMOVE)==0 ) n++;
203724 i += jsonNodeSize(pNode: &pNode[i]);
203725 }
203726 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
203727 if( p->useMod==0 ) break;
203728 assert( pNode->eU==2 );
203729 pNode = &p->aNode[pNode->u.iAppend];
203730 }
203731 }
203732 sqlite3_result_int64(pCtx: ctx, iVal: n);
203733}
203734
203735/*
203736** Bit values for the flags passed into jsonExtractFunc() or
203737** jsonSetFunc() via the user-data value.
203738*/
203739#define JSON_JSON 0x01 /* Result is always JSON */
203740#define JSON_SQL 0x02 /* Result is always SQL */
203741#define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
203742#define JSON_ISSET 0x04 /* json_set(), not json_insert() */
203743
203744/*
203745** json_extract(JSON, PATH, ...)
203746** "->"(JSON,PATH)
203747** "->>"(JSON,PATH)
203748**
203749** Return the element described by PATH. Return NULL if that PATH element
203750** is not found.
203751**
203752** If JSON_JSON is set or if more that one PATH argument is supplied then
203753** always return a JSON representation of the result. If JSON_SQL is set,
203754** then always return an SQL representation of the result. If neither flag
203755** is present and argc==2, then return JSON for objects and arrays and SQL
203756** for all other values.
203757**
203758** When multiple PATH arguments are supplied, the result is a JSON array
203759** containing the result of each PATH.
203760**
203761** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
203762** compatibility with PG.
203763*/
203764static void jsonExtractFunc(
203765 sqlite3_context *ctx,
203766 int argc,
203767 sqlite3_value **argv
203768){
203769 JsonParse *p; /* The parse */
203770 JsonNode *pNode;
203771 const char *zPath;
203772 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
203773 JsonString jx;
203774
203775 if( argc<2 ) return;
203776 p = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: 0);
203777 if( p==0 ) return;
203778 if( argc==2 ){
203779 /* With a single PATH argument */
203780 zPath = (const char*)sqlite3_value_text(pVal: argv[1]);
203781 if( zPath==0 ) return;
203782 if( flags & JSON_ABPATH ){
203783 if( zPath[0]!='$' || (zPath[1]!='.' && zPath[1]!='[' && zPath[1]!=0) ){
203784 /* The -> and ->> operators accept abbreviated PATH arguments. This
203785 ** is mostly for compatibility with PostgreSQL, but also for
203786 ** convenience.
203787 **
203788 ** NUMBER ==> $[NUMBER] // PG compatible
203789 ** LABEL ==> $.LABEL // PG compatible
203790 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
203791 */
203792 jsonInit(p: &jx, pCtx: ctx);
203793 if( sqlite3Isdigit(zPath[0]) ){
203794 jsonAppendRawNZ(p: &jx, zIn: "$[", N: 2);
203795 jsonAppendRaw(p: &jx, zIn: zPath, N: (int)strlen(s: zPath));
203796 jsonAppendRawNZ(p: &jx, zIn: "]", N: 2);
203797 }else{
203798 jsonAppendRawNZ(p: &jx, zIn: "$.", N: 1 + (zPath[0]!='['));
203799 jsonAppendRaw(p: &jx, zIn: zPath, N: (int)strlen(s: zPath));
203800 jsonAppendChar(p: &jx, c: 0);
203801 }
203802 pNode = jx.bErr ? 0 : jsonLookup(pParse: p, zPath: jx.zBuf, pApnd: 0, pCtx: ctx);
203803 jsonReset(p: &jx);
203804 }else{
203805 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
203806 }
203807 if( pNode ){
203808 if( flags & JSON_JSON ){
203809 jsonReturnJson(pParse: p, pNode, pCtx: ctx, bGenerateAlt: 0);
203810 }else{
203811 jsonReturn(pParse: p, pNode, pCtx: ctx);
203812 sqlite3_result_subtype(pCtx: ctx, eSubtype: 0);
203813 }
203814 }
203815 }else{
203816 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
203817 if( p->nErr==0 && pNode ) jsonReturn(pParse: p, pNode, pCtx: ctx);
203818 }
203819 }else{
203820 /* Two or more PATH arguments results in a JSON array with each
203821 ** element of the array being the value selected by one of the PATHs */
203822 int i;
203823 jsonInit(p: &jx, pCtx: ctx);
203824 jsonAppendChar(p: &jx, c: '[');
203825 for(i=1; i<argc; i++){
203826 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
203827 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
203828 if( p->nErr ) break;
203829 jsonAppendSeparator(p: &jx);
203830 if( pNode ){
203831 jsonRenderNode(pParse: p, pNode, pOut: &jx);
203832 }else{
203833 jsonAppendRawNZ(p: &jx, zIn: "null", N: 4);
203834 }
203835 }
203836 if( i==argc ){
203837 jsonAppendChar(p: &jx, c: ']');
203838 jsonResult(p: &jx);
203839 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
203840 }
203841 jsonReset(p: &jx);
203842 }
203843}
203844
203845/* This is the RFC 7396 MergePatch algorithm.
203846*/
203847static JsonNode *jsonMergePatch(
203848 JsonParse *pParse, /* The JSON parser that contains the TARGET */
203849 u32 iTarget, /* Node of the TARGET in pParse */
203850 JsonNode *pPatch /* The PATCH */
203851){
203852 u32 i, j;
203853 u32 iRoot;
203854 JsonNode *pTarget;
203855 if( pPatch->eType!=JSON_OBJECT ){
203856 return pPatch;
203857 }
203858 assert( iTarget<pParse->nNode );
203859 pTarget = &pParse->aNode[iTarget];
203860 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
203861 if( pTarget->eType!=JSON_OBJECT ){
203862 jsonRemoveAllNulls(pNode: pPatch);
203863 return pPatch;
203864 }
203865 iRoot = iTarget;
203866 for(i=1; i<pPatch->n; i += jsonNodeSize(pNode: &pPatch[i+1])+1){
203867 u32 nKey;
203868 const char *zKey;
203869 assert( pPatch[i].eType==JSON_STRING );
203870 assert( pPatch[i].jnFlags & JNODE_LABEL );
203871 assert( pPatch[i].eU==1 );
203872 nKey = pPatch[i].n;
203873 zKey = pPatch[i].u.zJContent;
203874 for(j=1; j<pTarget->n; j += jsonNodeSize(pNode: &pTarget[j+1])+1 ){
203875 assert( pTarget[j].eType==JSON_STRING );
203876 assert( pTarget[j].jnFlags & JNODE_LABEL );
203877 if( jsonSameLabel(p1: &pPatch[i], p2: &pTarget[j]) ){
203878 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ) break;
203879 if( pPatch[i+1].eType==JSON_NULL ){
203880 pTarget[j+1].jnFlags |= JNODE_REMOVE;
203881 }else{
203882 JsonNode *pNew = jsonMergePatch(pParse, iTarget: iTarget+j+1, pPatch: &pPatch[i+1]);
203883 if( pNew==0 ) return 0;
203884 if( pNew!=&pParse->aNode[iTarget+j+1] ){
203885 jsonParseAddSubstNode(pParse, iNode: iTarget+j+1);
203886 jsonParseAddNodeArray(pParse, aNode: pNew, nNode: jsonNodeSize(pNode: pNew));
203887 }
203888 pTarget = &pParse->aNode[iTarget];
203889 }
203890 break;
203891 }
203892 }
203893 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
203894 int iStart;
203895 JsonNode *pApnd;
203896 u32 nApnd;
203897 iStart = jsonParseAddNode(pParse, JSON_OBJECT, n: 0, zContent: 0);
203898 jsonParseAddNode(pParse, JSON_STRING, n: nKey, zContent: zKey);
203899 pApnd = &pPatch[i+1];
203900 if( pApnd->eType==JSON_OBJECT ) jsonRemoveAllNulls(pNode: pApnd);
203901 nApnd = jsonNodeSize(pNode: pApnd);
203902 jsonParseAddNodeArray(pParse, aNode: pApnd, nNode: jsonNodeSize(pNode: pApnd));
203903 if( pParse->oom ) return 0;
203904 pParse->aNode[iStart].n = 1+nApnd;
203905 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
203906 pParse->aNode[iRoot].u.iAppend = iStart;
203907 VVA( pParse->aNode[iRoot].eU = 2 );
203908 iRoot = iStart;
203909 pTarget = &pParse->aNode[iTarget];
203910 }
203911 }
203912 return pTarget;
203913}
203914
203915/*
203916** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
203917** object that is the result of running the RFC 7396 MergePatch() algorithm
203918** on the two arguments.
203919*/
203920static void jsonPatchFunc(
203921 sqlite3_context *ctx,
203922 int argc,
203923 sqlite3_value **argv
203924){
203925 JsonParse *pX; /* The JSON that is being patched */
203926 JsonParse *pY; /* The patch */
203927 JsonNode *pResult; /* The result of the merge */
203928
203929 UNUSED_PARAMETER(argc);
203930 pX = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: 1);
203931 if( pX==0 ) return;
203932 assert( pX->hasMod==0 );
203933 pX->hasMod = 1;
203934 pY = jsonParseCached(pCtx: ctx, pJson: argv[1], pErrCtx: ctx, bUnedited: 1);
203935 if( pY==0 ) return;
203936 pX->useMod = 1;
203937 pY->useMod = 1;
203938 pResult = jsonMergePatch(pParse: pX, iTarget: 0, pPatch: pY->aNode);
203939 assert( pResult!=0 || pX->oom );
203940 if( pResult && pX->oom==0 ){
203941 jsonDebugPrintParse(pX);
203942 jsonDebugPrintNode(pResult);
203943 jsonReturnJson(pParse: pX, pNode: pResult, pCtx: ctx, bGenerateAlt: 0);
203944 }else{
203945 sqlite3_result_error_nomem(pCtx: ctx);
203946 }
203947}
203948
203949
203950/*
203951** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
203952** object that contains all name/value given in arguments. Or if any name
203953** is not a string or if any value is a BLOB, throw an error.
203954*/
203955static void jsonObjectFunc(
203956 sqlite3_context *ctx,
203957 int argc,
203958 sqlite3_value **argv
203959){
203960 int i;
203961 JsonString jx;
203962 const char *z;
203963 u32 n;
203964
203965 if( argc&1 ){
203966 sqlite3_result_error(pCtx: ctx, z: "json_object() requires an even number "
203967 "of arguments", n: -1);
203968 return;
203969 }
203970 jsonInit(p: &jx, pCtx: ctx);
203971 jsonAppendChar(p: &jx, c: '{');
203972 for(i=0; i<argc; i+=2){
203973 if( sqlite3_value_type(pVal: argv[i])!=SQLITE_TEXT ){
203974 sqlite3_result_error(pCtx: ctx, z: "json_object() labels must be TEXT", n: -1);
203975 jsonReset(p: &jx);
203976 return;
203977 }
203978 jsonAppendSeparator(p: &jx);
203979 z = (const char*)sqlite3_value_text(pVal: argv[i]);
203980 n = (u32)sqlite3_value_bytes(pVal: argv[i]);
203981 jsonAppendString(p: &jx, zIn: z, N: n);
203982 jsonAppendChar(p: &jx, c: ':');
203983 jsonAppendValue(p: &jx, pValue: argv[i+1]);
203984 }
203985 jsonAppendChar(p: &jx, c: '}');
203986 jsonResult(p: &jx);
203987 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
203988}
203989
203990
203991/*
203992** json_remove(JSON, PATH, ...)
203993**
203994** Remove the named elements from JSON and return the result. malformed
203995** JSON or PATH arguments result in an error.
203996*/
203997static void jsonRemoveFunc(
203998 sqlite3_context *ctx,
203999 int argc,
204000 sqlite3_value **argv
204001){
204002 JsonParse *pParse; /* The parse */
204003 JsonNode *pNode;
204004 const char *zPath;
204005 u32 i;
204006
204007 if( argc<1 ) return;
204008 pParse = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: argc>1);
204009 if( pParse==0 ) return;
204010 for(i=1; i<(u32)argc; i++){
204011 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
204012 if( zPath==0 ) goto remove_done;
204013 pNode = jsonLookup(pParse, zPath, pApnd: 0, pCtx: ctx);
204014 if( pParse->nErr ) goto remove_done;
204015 if( pNode ){
204016 pNode->jnFlags |= JNODE_REMOVE;
204017 pParse->hasMod = 1;
204018 pParse->useMod = 1;
204019 }
204020 }
204021 if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
204022 jsonReturnJson(pParse, pNode: pParse->aNode, pCtx: ctx, bGenerateAlt: 1);
204023 }
204024remove_done:
204025 jsonDebugPrintParse(p);
204026}
204027
204028/*
204029** Substitute the value at iNode with the pValue parameter.
204030*/
204031static void jsonReplaceNode(
204032 sqlite3_context *pCtx,
204033 JsonParse *p,
204034 int iNode,
204035 sqlite3_value *pValue
204036){
204037 int idx = jsonParseAddSubstNode(pParse: p, iNode);
204038 if( idx<=0 ){
204039 assert( p->oom );
204040 return;
204041 }
204042 switch( sqlite3_value_type(pVal: pValue) ){
204043 case SQLITE_NULL: {
204044 jsonParseAddNode(pParse: p, JSON_NULL, n: 0, zContent: 0);
204045 break;
204046 }
204047 case SQLITE_FLOAT: {
204048 char *z = sqlite3_mprintf(zFormat: "%!0.15g", sqlite3_value_double(pVal: pValue));
204049 int n;
204050 if( z==0 ){
204051 p->oom = 1;
204052 break;
204053 }
204054 n = sqlite3Strlen30(z);
204055 jsonParseAddNode(pParse: p, JSON_REAL, n, zContent: z);
204056 jsonParseAddCleanup(pParse: p, xOp: sqlite3_free, pArg: z);
204057 break;
204058 }
204059 case SQLITE_INTEGER: {
204060 char *z = sqlite3_mprintf(zFormat: "%lld", sqlite3_value_int64(pVal: pValue));
204061 int n;
204062 if( z==0 ){
204063 p->oom = 1;
204064 break;
204065 }
204066 n = sqlite3Strlen30(z);
204067 jsonParseAddNode(pParse: p, JSON_INT, n, zContent: z);
204068 jsonParseAddCleanup(pParse: p, xOp: sqlite3_free, pArg: z);
204069
204070 break;
204071 }
204072 case SQLITE_TEXT: {
204073 const char *z = (const char*)sqlite3_value_text(pVal: pValue);
204074 u32 n = (u32)sqlite3_value_bytes(pVal: pValue);
204075 if( z==0 ){
204076 p->oom = 1;
204077 break;
204078 }
204079 if( sqlite3_value_subtype(pVal: pValue)!=JSON_SUBTYPE ){
204080 char *zCopy = sqlite3DbStrDup(db: 0, z);
204081 int k;
204082 if( zCopy ){
204083 jsonParseAddCleanup(pParse: p, xOp: sqlite3_free, pArg: zCopy);
204084 }else{
204085 p->oom = 1;
204086 sqlite3_result_error_nomem(pCtx);
204087 }
204088 k = jsonParseAddNode(pParse: p, JSON_STRING, n, zContent: zCopy);
204089 assert( k>0 || p->oom );
204090 if( p->oom==0 ) p->aNode[k].jnFlags |= JNODE_RAW;
204091 }else{
204092 JsonParse *pPatch = jsonParseCached(pCtx, pJson: pValue, pErrCtx: pCtx, bUnedited: 1);
204093 if( pPatch==0 ){
204094 p->oom = 1;
204095 break;
204096 }
204097 jsonParseAddNodeArray(pParse: p, aNode: pPatch->aNode, nNode: pPatch->nNode);
204098 /* The nodes copied out of pPatch and into p likely contain
204099 ** u.zJContent pointers into pPatch->zJson. So preserve the
204100 ** content of pPatch until p is destroyed. */
204101 assert( pPatch->nJPRef>=1 );
204102 pPatch->nJPRef++;
204103 jsonParseAddCleanup(pParse: p, xOp: (void(*)(void*))jsonParseFree, pArg: pPatch);
204104 }
204105 break;
204106 }
204107 default: {
204108 jsonParseAddNode(pParse: p, JSON_NULL, n: 0, zContent: 0);
204109 sqlite3_result_error(pCtx, z: "JSON cannot hold BLOB values", n: -1);
204110 p->nErr++;
204111 break;
204112 }
204113 }
204114}
204115
204116/*
204117** json_replace(JSON, PATH, VALUE, ...)
204118**
204119** Replace the value at PATH with VALUE. If PATH does not already exist,
204120** this routine is a no-op. If JSON or PATH is malformed, throw an error.
204121*/
204122static void jsonReplaceFunc(
204123 sqlite3_context *ctx,
204124 int argc,
204125 sqlite3_value **argv
204126){
204127 JsonParse *pParse; /* The parse */
204128 JsonNode *pNode;
204129 const char *zPath;
204130 u32 i;
204131
204132 if( argc<1 ) return;
204133 if( (argc&1)==0 ) {
204134 jsonWrongNumArgs(pCtx: ctx, zFuncName: "replace");
204135 return;
204136 }
204137 pParse = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: argc>1);
204138 if( pParse==0 ) return;
204139 for(i=1; i<(u32)argc; i+=2){
204140 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
204141 pParse->useMod = 1;
204142 pNode = jsonLookup(pParse, zPath, pApnd: 0, pCtx: ctx);
204143 if( pParse->nErr ) goto replace_err;
204144 if( pNode ){
204145 jsonReplaceNode(pCtx: ctx, p: pParse, iNode: (u32)(pNode - pParse->aNode), pValue: argv[i+1]);
204146 }
204147 }
204148 jsonReturnJson(pParse, pNode: pParse->aNode, pCtx: ctx, bGenerateAlt: 1);
204149replace_err:
204150 jsonDebugPrintParse(pParse);
204151}
204152
204153
204154/*
204155** json_set(JSON, PATH, VALUE, ...)
204156**
204157** Set the value at PATH to VALUE. Create the PATH if it does not already
204158** exist. Overwrite existing values that do exist.
204159** If JSON or PATH is malformed, throw an error.
204160**
204161** json_insert(JSON, PATH, VALUE, ...)
204162**
204163** Create PATH and initialize it to VALUE. If PATH already exists, this
204164** routine is a no-op. If JSON or PATH is malformed, throw an error.
204165*/
204166static void jsonSetFunc(
204167 sqlite3_context *ctx,
204168 int argc,
204169 sqlite3_value **argv
204170){
204171 JsonParse *pParse; /* The parse */
204172 JsonNode *pNode;
204173 const char *zPath;
204174 u32 i;
204175 int bApnd;
204176 int bIsSet = sqlite3_user_data(p: ctx)!=0;
204177
204178 if( argc<1 ) return;
204179 if( (argc&1)==0 ) {
204180 jsonWrongNumArgs(pCtx: ctx, zFuncName: bIsSet ? "set" : "insert");
204181 return;
204182 }
204183 pParse = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: argc>1);
204184 if( pParse==0 ) return;
204185 for(i=1; i<(u32)argc; i+=2){
204186 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
204187 bApnd = 0;
204188 pParse->useMod = 1;
204189 pNode = jsonLookup(pParse, zPath, pApnd: &bApnd, pCtx: ctx);
204190 if( pParse->oom ){
204191 sqlite3_result_error_nomem(pCtx: ctx);
204192 goto jsonSetDone;
204193 }else if( pParse->nErr ){
204194 goto jsonSetDone;
204195 }else if( pNode && (bApnd || bIsSet) ){
204196 jsonReplaceNode(pCtx: ctx, p: pParse, iNode: (u32)(pNode - pParse->aNode), pValue: argv[i+1]);
204197 }
204198 }
204199 jsonDebugPrintParse(pParse);
204200 jsonReturnJson(pParse, pNode: pParse->aNode, pCtx: ctx, bGenerateAlt: 1);
204201
204202jsonSetDone:
204203 /* no cleanup required */;
204204}
204205
204206/*
204207** json_type(JSON)
204208** json_type(JSON, PATH)
204209**
204210** Return the top-level "type" of a JSON string. json_type() raises an
204211** error if either the JSON or PATH inputs are not well-formed.
204212*/
204213static void jsonTypeFunc(
204214 sqlite3_context *ctx,
204215 int argc,
204216 sqlite3_value **argv
204217){
204218 JsonParse *p; /* The parse */
204219 const char *zPath;
204220 JsonNode *pNode;
204221
204222 p = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: ctx, bUnedited: 0);
204223 if( p==0 ) return;
204224 if( argc==2 ){
204225 zPath = (const char*)sqlite3_value_text(pVal: argv[1]);
204226 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
204227 }else{
204228 pNode = p->aNode;
204229 }
204230 if( pNode ){
204231 sqlite3_result_text(pCtx: ctx, z: jsonType[pNode->eType], n: -1, SQLITE_STATIC);
204232 }
204233}
204234
204235/*
204236** json_valid(JSON)
204237**
204238** Return 1 if JSON is a well-formed canonical JSON string according
204239** to RFC-7159. Return 0 otherwise.
204240*/
204241static void jsonValidFunc(
204242 sqlite3_context *ctx,
204243 int argc,
204244 sqlite3_value **argv
204245){
204246 JsonParse *p; /* The parse */
204247 UNUSED_PARAMETER(argc);
204248 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ){
204249#ifdef SQLITE_LEGACY_JSON_VALID
204250 /* Incorrect legacy behavior was to return FALSE for a NULL input */
204251 sqlite3_result_int(ctx, 0);
204252#endif
204253 return;
204254 }
204255 p = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: 0, bUnedited: 0);
204256 if( p==0 || p->oom ){
204257 sqlite3_result_error_nomem(pCtx: ctx);
204258 sqlite3_free(p);
204259 }else{
204260 sqlite3_result_int(pCtx: ctx, iVal: p->nErr==0 && (p->hasNonstd==0 || p->useMod));
204261 if( p->nErr ) jsonParseFree(pParse: p);
204262 }
204263}
204264
204265/*
204266** json_error_position(JSON)
204267**
204268** If the argument is not an interpretable JSON string, then return the 1-based
204269** character position at which the parser first recognized that the input
204270** was in error. The left-most character is 1. If the string is valid
204271** JSON, then return 0.
204272**
204273** Note that json_valid() is only true for strictly conforming canonical JSON.
204274** But this routine returns zero if the input contains extension. Thus:
204275**
204276** (1) If the input X is strictly conforming canonical JSON:
204277**
204278** json_valid(X) returns true
204279** json_error_position(X) returns 0
204280**
204281** (2) If the input X is JSON but it includes extension (such as JSON5) that
204282** are not part of RFC-8259:
204283**
204284** json_valid(X) returns false
204285** json_error_position(X) return 0
204286**
204287** (3) If the input X cannot be interpreted as JSON even taking extensions
204288** into account:
204289**
204290** json_valid(X) return false
204291** json_error_position(X) returns 1 or more
204292*/
204293static void jsonErrorFunc(
204294 sqlite3_context *ctx,
204295 int argc,
204296 sqlite3_value **argv
204297){
204298 JsonParse *p; /* The parse */
204299 UNUSED_PARAMETER(argc);
204300 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
204301 p = jsonParseCached(pCtx: ctx, pJson: argv[0], pErrCtx: 0, bUnedited: 0);
204302 if( p==0 || p->oom ){
204303 sqlite3_result_error_nomem(pCtx: ctx);
204304 sqlite3_free(p);
204305 }else if( p->nErr==0 ){
204306 sqlite3_result_int(pCtx: ctx, iVal: 0);
204307 }else{
204308 int n = 1;
204309 u32 i;
204310 const char *z = (const char*)sqlite3_value_text(pVal: argv[0]);
204311 for(i=0; i<p->iErr && ALWAYS(z[i]); i++){
204312 if( (z[i]&0xc0)!=0x80 ) n++;
204313 }
204314 sqlite3_result_int(pCtx: ctx, iVal: n);
204315 jsonParseFree(pParse: p);
204316 }
204317}
204318
204319
204320/****************************************************************************
204321** Aggregate SQL function implementations
204322****************************************************************************/
204323/*
204324** json_group_array(VALUE)
204325**
204326** Return a JSON array composed of all values in the aggregate.
204327*/
204328static void jsonArrayStep(
204329 sqlite3_context *ctx,
204330 int argc,
204331 sqlite3_value **argv
204332){
204333 JsonString *pStr;
204334 UNUSED_PARAMETER(argc);
204335 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: sizeof(*pStr));
204336 if( pStr ){
204337 if( pStr->zBuf==0 ){
204338 jsonInit(p: pStr, pCtx: ctx);
204339 jsonAppendChar(p: pStr, c: '[');
204340 }else if( pStr->nUsed>1 ){
204341 jsonAppendChar(p: pStr, c: ',');
204342 }
204343 pStr->pCtx = ctx;
204344 jsonAppendValue(p: pStr, pValue: argv[0]);
204345 }
204346}
204347static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
204348 JsonString *pStr;
204349 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: 0);
204350 if( pStr ){
204351 pStr->pCtx = ctx;
204352 jsonAppendChar(p: pStr, c: ']');
204353 if( pStr->bErr ){
204354 if( pStr->bErr==1 ) sqlite3_result_error_nomem(pCtx: ctx);
204355 assert( pStr->bStatic );
204356 }else if( isFinal ){
204357 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed,
204358 xDel: pStr->bStatic ? SQLITE_TRANSIENT :
204359 (void(*)(void*))sqlite3RCStrUnref);
204360 pStr->bStatic = 1;
204361 }else{
204362 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed, SQLITE_TRANSIENT);
204363 pStr->nUsed--;
204364 }
204365 }else{
204366 sqlite3_result_text(pCtx: ctx, z: "[]", n: 2, SQLITE_STATIC);
204367 }
204368 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
204369}
204370static void jsonArrayValue(sqlite3_context *ctx){
204371 jsonArrayCompute(ctx, isFinal: 0);
204372}
204373static void jsonArrayFinal(sqlite3_context *ctx){
204374 jsonArrayCompute(ctx, isFinal: 1);
204375}
204376
204377#ifndef SQLITE_OMIT_WINDOWFUNC
204378/*
204379** This method works for both json_group_array() and json_group_object().
204380** It works by removing the first element of the group by searching forward
204381** to the first comma (",") that is not within a string and deleting all
204382** text through that comma.
204383*/
204384static void jsonGroupInverse(
204385 sqlite3_context *ctx,
204386 int argc,
204387 sqlite3_value **argv
204388){
204389 unsigned int i;
204390 int inStr = 0;
204391 int nNest = 0;
204392 char *z;
204393 char c;
204394 JsonString *pStr;
204395 UNUSED_PARAMETER(argc);
204396 UNUSED_PARAMETER(argv);
204397 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: 0);
204398#ifdef NEVER
204399 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
204400 ** always have been called to initialize it */
204401 if( NEVER(!pStr) ) return;
204402#endif
204403 z = pStr->zBuf;
204404 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
204405 if( c=='"' ){
204406 inStr = !inStr;
204407 }else if( c=='\\' ){
204408 i++;
204409 }else if( !inStr ){
204410 if( c=='{' || c=='[' ) nNest++;
204411 if( c=='}' || c==']' ) nNest--;
204412 }
204413 }
204414 if( i<pStr->nUsed ){
204415 pStr->nUsed -= i;
204416 memmove(dest: &z[1], src: &z[i+1], n: (size_t)pStr->nUsed-1);
204417 z[pStr->nUsed] = 0;
204418 }else{
204419 pStr->nUsed = 1;
204420 }
204421}
204422#else
204423# define jsonGroupInverse 0
204424#endif
204425
204426
204427/*
204428** json_group_obj(NAME,VALUE)
204429**
204430** Return a JSON object composed of all names and values in the aggregate.
204431*/
204432static void jsonObjectStep(
204433 sqlite3_context *ctx,
204434 int argc,
204435 sqlite3_value **argv
204436){
204437 JsonString *pStr;
204438 const char *z;
204439 u32 n;
204440 UNUSED_PARAMETER(argc);
204441 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: sizeof(*pStr));
204442 if( pStr ){
204443 if( pStr->zBuf==0 ){
204444 jsonInit(p: pStr, pCtx: ctx);
204445 jsonAppendChar(p: pStr, c: '{');
204446 }else if( pStr->nUsed>1 ){
204447 jsonAppendChar(p: pStr, c: ',');
204448 }
204449 pStr->pCtx = ctx;
204450 z = (const char*)sqlite3_value_text(pVal: argv[0]);
204451 n = (u32)sqlite3_value_bytes(pVal: argv[0]);
204452 jsonAppendString(p: pStr, zIn: z, N: n);
204453 jsonAppendChar(p: pStr, c: ':');
204454 jsonAppendValue(p: pStr, pValue: argv[1]);
204455 }
204456}
204457static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
204458 JsonString *pStr;
204459 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: 0);
204460 if( pStr ){
204461 jsonAppendChar(p: pStr, c: '}');
204462 if( pStr->bErr ){
204463 if( pStr->bErr==1 ) sqlite3_result_error_nomem(pCtx: ctx);
204464 assert( pStr->bStatic );
204465 }else if( isFinal ){
204466 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed,
204467 xDel: pStr->bStatic ? SQLITE_TRANSIENT :
204468 (void(*)(void*))sqlite3RCStrUnref);
204469 pStr->bStatic = 1;
204470 }else{
204471 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed, SQLITE_TRANSIENT);
204472 pStr->nUsed--;
204473 }
204474 }else{
204475 sqlite3_result_text(pCtx: ctx, z: "{}", n: 2, SQLITE_STATIC);
204476 }
204477 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
204478}
204479static void jsonObjectValue(sqlite3_context *ctx){
204480 jsonObjectCompute(ctx, isFinal: 0);
204481}
204482static void jsonObjectFinal(sqlite3_context *ctx){
204483 jsonObjectCompute(ctx, isFinal: 1);
204484}
204485
204486
204487
204488#ifndef SQLITE_OMIT_VIRTUALTABLE
204489/****************************************************************************
204490** The json_each virtual table
204491****************************************************************************/
204492typedef struct JsonEachCursor JsonEachCursor;
204493struct JsonEachCursor {
204494 sqlite3_vtab_cursor base; /* Base class - must be first */
204495 u32 iRowid; /* The rowid */
204496 u32 iBegin; /* The first node of the scan */
204497 u32 i; /* Index in sParse.aNode[] of current row */
204498 u32 iEnd; /* EOF when i equals or exceeds this value */
204499 u8 eType; /* Type of top-level element */
204500 u8 bRecursive; /* True for json_tree(). False for json_each() */
204501 char *zJson; /* Input JSON */
204502 char *zRoot; /* Path by which to filter zJson */
204503 JsonParse sParse; /* Parse of the input JSON */
204504};
204505
204506/* Constructor for the json_each virtual table */
204507static int jsonEachConnect(
204508 sqlite3 *db,
204509 void *pAux,
204510 int argc, const char *const*argv,
204511 sqlite3_vtab **ppVtab,
204512 char **pzErr
204513){
204514 sqlite3_vtab *pNew;
204515 int rc;
204516
204517/* Column numbers */
204518#define JEACH_KEY 0
204519#define JEACH_VALUE 1
204520#define JEACH_TYPE 2
204521#define JEACH_ATOM 3
204522#define JEACH_ID 4
204523#define JEACH_PARENT 5
204524#define JEACH_FULLKEY 6
204525#define JEACH_PATH 7
204526/* The xBestIndex method assumes that the JSON and ROOT columns are
204527** the last two columns in the table. Should this ever changes, be
204528** sure to update the xBestIndex method. */
204529#define JEACH_JSON 8
204530#define JEACH_ROOT 9
204531
204532 UNUSED_PARAMETER(pzErr);
204533 UNUSED_PARAMETER(argv);
204534 UNUSED_PARAMETER(argc);
204535 UNUSED_PARAMETER(pAux);
204536 rc = sqlite3_declare_vtab(db,
204537 zCreateTable: "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
204538 "json HIDDEN,root HIDDEN)");
204539 if( rc==SQLITE_OK ){
204540 pNew = *ppVtab = sqlite3_malloc( n: sizeof(*pNew) );
204541 if( pNew==0 ) return SQLITE_NOMEM;
204542 memset(s: pNew, c: 0, n: sizeof(*pNew));
204543 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
204544 }
204545 return rc;
204546}
204547
204548/* destructor for json_each virtual table */
204549static int jsonEachDisconnect(sqlite3_vtab *pVtab){
204550 sqlite3_free(p: pVtab);
204551 return SQLITE_OK;
204552}
204553
204554/* constructor for a JsonEachCursor object for json_each(). */
204555static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
204556 JsonEachCursor *pCur;
204557
204558 UNUSED_PARAMETER(p);
204559 pCur = sqlite3_malloc( n: sizeof(*pCur) );
204560 if( pCur==0 ) return SQLITE_NOMEM;
204561 memset(s: pCur, c: 0, n: sizeof(*pCur));
204562 *ppCursor = &pCur->base;
204563 return SQLITE_OK;
204564}
204565
204566/* constructor for a JsonEachCursor object for json_tree(). */
204567static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
204568 int rc = jsonEachOpenEach(p, ppCursor);
204569 if( rc==SQLITE_OK ){
204570 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
204571 pCur->bRecursive = 1;
204572 }
204573 return rc;
204574}
204575
204576/* Reset a JsonEachCursor back to its original state. Free any memory
204577** held. */
204578static void jsonEachCursorReset(JsonEachCursor *p){
204579 sqlite3_free(p: p->zRoot);
204580 jsonParseReset(pParse: &p->sParse);
204581 p->iRowid = 0;
204582 p->i = 0;
204583 p->iEnd = 0;
204584 p->eType = 0;
204585 p->zJson = 0;
204586 p->zRoot = 0;
204587}
204588
204589/* Destructor for a jsonEachCursor object */
204590static int jsonEachClose(sqlite3_vtab_cursor *cur){
204591 JsonEachCursor *p = (JsonEachCursor*)cur;
204592 jsonEachCursorReset(p);
204593 sqlite3_free(p: cur);
204594 return SQLITE_OK;
204595}
204596
204597/* Return TRUE if the jsonEachCursor object has been advanced off the end
204598** of the JSON object */
204599static int jsonEachEof(sqlite3_vtab_cursor *cur){
204600 JsonEachCursor *p = (JsonEachCursor*)cur;
204601 return p->i >= p->iEnd;
204602}
204603
204604/* Advance the cursor to the next element for json_tree() */
204605static int jsonEachNext(sqlite3_vtab_cursor *cur){
204606 JsonEachCursor *p = (JsonEachCursor*)cur;
204607 if( p->bRecursive ){
204608 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
204609 p->i++;
204610 p->iRowid++;
204611 if( p->i<p->iEnd ){
204612 u32 iUp = p->sParse.aUp[p->i];
204613 JsonNode *pUp = &p->sParse.aNode[iUp];
204614 p->eType = pUp->eType;
204615 if( pUp->eType==JSON_ARRAY ){
204616 assert( pUp->eU==0 || pUp->eU==3 );
204617 testcase( pUp->eU==3 );
204618 VVA( pUp->eU = 3 );
204619 if( iUp==p->i-1 ){
204620 pUp->u.iKey = 0;
204621 }else{
204622 pUp->u.iKey++;
204623 }
204624 }
204625 }
204626 }else{
204627 switch( p->eType ){
204628 case JSON_ARRAY: {
204629 p->i += jsonNodeSize(pNode: &p->sParse.aNode[p->i]);
204630 p->iRowid++;
204631 break;
204632 }
204633 case JSON_OBJECT: {
204634 p->i += 1 + jsonNodeSize(pNode: &p->sParse.aNode[p->i+1]);
204635 p->iRowid++;
204636 break;
204637 }
204638 default: {
204639 p->i = p->iEnd;
204640 break;
204641 }
204642 }
204643 }
204644 return SQLITE_OK;
204645}
204646
204647/* Append an object label to the JSON Path being constructed
204648** in pStr.
204649*/
204650static void jsonAppendObjectPathElement(
204651 JsonString *pStr,
204652 JsonNode *pNode
204653){
204654 int jj, nn;
204655 const char *z;
204656 assert( pNode->eType==JSON_STRING );
204657 assert( pNode->jnFlags & JNODE_LABEL );
204658 assert( pNode->eU==1 );
204659 z = pNode->u.zJContent;
204660 nn = pNode->n;
204661 if( (pNode->jnFlags & JNODE_RAW)==0 ){
204662 assert( nn>=2 );
204663 assert( z[0]=='"' || z[0]=='\'' );
204664 assert( z[nn-1]=='"' || z[0]=='\'' );
204665 if( nn>2 && sqlite3Isalpha(z[1]) ){
204666 for(jj=2; jj<nn-1 && sqlite3Isalnum(z[jj]); jj++){}
204667 if( jj==nn-1 ){
204668 z++;
204669 nn -= 2;
204670 }
204671 }
204672 }
204673 jsonPrintf(N: nn+2, p: pStr, zFormat: ".%.*s", nn, z);
204674}
204675
204676/* Append the name of the path for element i to pStr
204677*/
204678static void jsonEachComputePath(
204679 JsonEachCursor *p, /* The cursor */
204680 JsonString *pStr, /* Write the path here */
204681 u32 i /* Path to this element */
204682){
204683 JsonNode *pNode, *pUp;
204684 u32 iUp;
204685 if( i==0 ){
204686 jsonAppendChar(p: pStr, c: '$');
204687 return;
204688 }
204689 iUp = p->sParse.aUp[i];
204690 jsonEachComputePath(p, pStr, i: iUp);
204691 pNode = &p->sParse.aNode[i];
204692 pUp = &p->sParse.aNode[iUp];
204693 if( pUp->eType==JSON_ARRAY ){
204694 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
204695 testcase( pUp->eU==0 );
204696 jsonPrintf(N: 30, p: pStr, zFormat: "[%d]", pUp->u.iKey);
204697 }else{
204698 assert( pUp->eType==JSON_OBJECT );
204699 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
204700 jsonAppendObjectPathElement(pStr, pNode);
204701 }
204702}
204703
204704/* Return the value of a column */
204705static int jsonEachColumn(
204706 sqlite3_vtab_cursor *cur, /* The cursor */
204707 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
204708 int i /* Which column to return */
204709){
204710 JsonEachCursor *p = (JsonEachCursor*)cur;
204711 JsonNode *pThis = &p->sParse.aNode[p->i];
204712 switch( i ){
204713 case JEACH_KEY: {
204714 if( p->i==0 ) break;
204715 if( p->eType==JSON_OBJECT ){
204716 jsonReturn(pParse: &p->sParse, pNode: pThis, pCtx: ctx);
204717 }else if( p->eType==JSON_ARRAY ){
204718 u32 iKey;
204719 if( p->bRecursive ){
204720 if( p->iRowid==0 ) break;
204721 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
204722 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
204723 }else{
204724 iKey = p->iRowid;
204725 }
204726 sqlite3_result_int64(pCtx: ctx, iVal: (sqlite3_int64)iKey);
204727 }
204728 break;
204729 }
204730 case JEACH_VALUE: {
204731 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
204732 jsonReturn(pParse: &p->sParse, pNode: pThis, pCtx: ctx);
204733 break;
204734 }
204735 case JEACH_TYPE: {
204736 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
204737 sqlite3_result_text(pCtx: ctx, z: jsonType[pThis->eType], n: -1, SQLITE_STATIC);
204738 break;
204739 }
204740 case JEACH_ATOM: {
204741 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
204742 if( pThis->eType>=JSON_ARRAY ) break;
204743 jsonReturn(pParse: &p->sParse, pNode: pThis, pCtx: ctx);
204744 break;
204745 }
204746 case JEACH_ID: {
204747 sqlite3_result_int64(pCtx: ctx,
204748 iVal: (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
204749 break;
204750 }
204751 case JEACH_PARENT: {
204752 if( p->i>p->iBegin && p->bRecursive ){
204753 sqlite3_result_int64(pCtx: ctx, iVal: (sqlite3_int64)p->sParse.aUp[p->i]);
204754 }
204755 break;
204756 }
204757 case JEACH_FULLKEY: {
204758 JsonString x;
204759 jsonInit(p: &x, pCtx: ctx);
204760 if( p->bRecursive ){
204761 jsonEachComputePath(p, pStr: &x, i: p->i);
204762 }else{
204763 if( p->zRoot ){
204764 jsonAppendRaw(p: &x, zIn: p->zRoot, N: (int)strlen(s: p->zRoot));
204765 }else{
204766 jsonAppendChar(p: &x, c: '$');
204767 }
204768 if( p->eType==JSON_ARRAY ){
204769 jsonPrintf(N: 30, p: &x, zFormat: "[%d]", p->iRowid);
204770 }else if( p->eType==JSON_OBJECT ){
204771 jsonAppendObjectPathElement(pStr: &x, pNode: pThis);
204772 }
204773 }
204774 jsonResult(p: &x);
204775 break;
204776 }
204777 case JEACH_PATH: {
204778 if( p->bRecursive ){
204779 JsonString x;
204780 jsonInit(p: &x, pCtx: ctx);
204781 jsonEachComputePath(p, pStr: &x, i: p->sParse.aUp[p->i]);
204782 jsonResult(p: &x);
204783 break;
204784 }
204785 /* For json_each() path and root are the same so fall through
204786 ** into the root case */
204787 /* no break */ deliberate_fall_through
204788 }
204789 default: {
204790 const char *zRoot = p->zRoot;
204791 if( zRoot==0 ) zRoot = "$";
204792 sqlite3_result_text(pCtx: ctx, z: zRoot, n: -1, SQLITE_STATIC);
204793 break;
204794 }
204795 case JEACH_JSON: {
204796 assert( i==JEACH_JSON );
204797 sqlite3_result_text(pCtx: ctx, z: p->sParse.zJson, n: -1, SQLITE_STATIC);
204798 break;
204799 }
204800 }
204801 return SQLITE_OK;
204802}
204803
204804/* Return the current rowid value */
204805static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
204806 JsonEachCursor *p = (JsonEachCursor*)cur;
204807 *pRowid = p->iRowid;
204808 return SQLITE_OK;
204809}
204810
204811/* The query strategy is to look for an equality constraint on the json
204812** column. Without such a constraint, the table cannot operate. idxNum is
204813** 1 if the constraint is found, 3 if the constraint and zRoot are found,
204814** and 0 otherwise.
204815*/
204816static int jsonEachBestIndex(
204817 sqlite3_vtab *tab,
204818 sqlite3_index_info *pIdxInfo
204819){
204820 int i; /* Loop counter or computed array index */
204821 int aIdx[2]; /* Index of constraints for JSON and ROOT */
204822 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
204823 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
204824 const struct sqlite3_index_constraint *pConstraint;
204825
204826 /* This implementation assumes that JSON and ROOT are the last two
204827 ** columns in the table */
204828 assert( JEACH_ROOT == JEACH_JSON+1 );
204829 UNUSED_PARAMETER(tab);
204830 aIdx[0] = aIdx[1] = -1;
204831 pConstraint = pIdxInfo->aConstraint;
204832 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
204833 int iCol;
204834 int iMask;
204835 if( pConstraint->iColumn < JEACH_JSON ) continue;
204836 iCol = pConstraint->iColumn - JEACH_JSON;
204837 assert( iCol==0 || iCol==1 );
204838 testcase( iCol==0 );
204839 iMask = 1 << iCol;
204840 if( pConstraint->usable==0 ){
204841 unusableMask |= iMask;
204842 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
204843 aIdx[iCol] = i;
204844 idxMask |= iMask;
204845 }
204846 }
204847 if( pIdxInfo->nOrderBy>0
204848 && pIdxInfo->aOrderBy[0].iColumn<0
204849 && pIdxInfo->aOrderBy[0].desc==0
204850 ){
204851 pIdxInfo->orderByConsumed = 1;
204852 }
204853
204854 if( (unusableMask & ~idxMask)!=0 ){
204855 /* If there are any unusable constraints on JSON or ROOT, then reject
204856 ** this entire plan */
204857 return SQLITE_CONSTRAINT;
204858 }
204859 if( aIdx[0]<0 ){
204860 /* No JSON input. Leave estimatedCost at the huge value that it was
204861 ** initialized to to discourage the query planner from selecting this
204862 ** plan. */
204863 pIdxInfo->idxNum = 0;
204864 }else{
204865 pIdxInfo->estimatedCost = 1.0;
204866 i = aIdx[0];
204867 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
204868 pIdxInfo->aConstraintUsage[i].omit = 1;
204869 if( aIdx[1]<0 ){
204870 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
204871 }else{
204872 i = aIdx[1];
204873 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
204874 pIdxInfo->aConstraintUsage[i].omit = 1;
204875 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
204876 }
204877 }
204878 return SQLITE_OK;
204879}
204880
204881/* Start a search on a new JSON string */
204882static int jsonEachFilter(
204883 sqlite3_vtab_cursor *cur,
204884 int idxNum, const char *idxStr,
204885 int argc, sqlite3_value **argv
204886){
204887 JsonEachCursor *p = (JsonEachCursor*)cur;
204888 const char *z;
204889 const char *zRoot = 0;
204890 sqlite3_int64 n;
204891
204892 UNUSED_PARAMETER(idxStr);
204893 UNUSED_PARAMETER(argc);
204894 jsonEachCursorReset(p);
204895 if( idxNum==0 ) return SQLITE_OK;
204896 z = (const char*)sqlite3_value_text(pVal: argv[0]);
204897 if( z==0 ) return SQLITE_OK;
204898 memset(s: &p->sParse, c: 0, n: sizeof(p->sParse));
204899 p->sParse.nJPRef = 1;
204900 if( sqlite3ValueIsOfClass(pVal: argv[0], xFree: (void(*)(void*))sqlite3RCStrUnref) ){
204901 p->sParse.zJson = sqlite3RCStrRef(z: (char*)z);
204902 }else{
204903 n = sqlite3_value_bytes(pVal: argv[0]);
204904 p->sParse.zJson = sqlite3RCStrNew( N: n+1 );
204905 if( p->sParse.zJson==0 ) return SQLITE_NOMEM;
204906 memcpy(dest: p->sParse.zJson, src: z, n: (size_t)n+1);
204907 }
204908 p->sParse.bJsonIsRCStr = 1;
204909 p->zJson = p->sParse.zJson;
204910 if( jsonParse(pParse: &p->sParse, pCtx: 0) ){
204911 int rc = SQLITE_NOMEM;
204912 if( p->sParse.oom==0 ){
204913 sqlite3_free(p: cur->pVtab->zErrMsg);
204914 cur->pVtab->zErrMsg = sqlite3_mprintf(zFormat: "malformed JSON");
204915 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
204916 }
204917 jsonEachCursorReset(p);
204918 return rc;
204919 }else if( p->bRecursive && jsonParseFindParents(pParse: &p->sParse) ){
204920 jsonEachCursorReset(p);
204921 return SQLITE_NOMEM;
204922 }else{
204923 JsonNode *pNode = 0;
204924 if( idxNum==3 ){
204925 const char *zErr = 0;
204926 zRoot = (const char*)sqlite3_value_text(pVal: argv[1]);
204927 if( zRoot==0 ) return SQLITE_OK;
204928 n = sqlite3_value_bytes(pVal: argv[1]);
204929 p->zRoot = sqlite3_malloc64( n: n+1 );
204930 if( p->zRoot==0 ) return SQLITE_NOMEM;
204931 memcpy(dest: p->zRoot, src: zRoot, n: (size_t)n+1);
204932 if( zRoot[0]!='$' ){
204933 zErr = zRoot;
204934 }else{
204935 pNode = jsonLookupStep(pParse: &p->sParse, iRoot: 0, zPath: p->zRoot+1, pApnd: 0, pzErr: &zErr);
204936 }
204937 if( zErr ){
204938 sqlite3_free(p: cur->pVtab->zErrMsg);
204939 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
204940 jsonEachCursorReset(p);
204941 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
204942 }else if( pNode==0 ){
204943 return SQLITE_OK;
204944 }
204945 }else{
204946 pNode = p->sParse.aNode;
204947 }
204948 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
204949 p->eType = pNode->eType;
204950 if( p->eType>=JSON_ARRAY ){
204951 assert( pNode->eU==0 );
204952 VVA( pNode->eU = 3 );
204953 pNode->u.iKey = 0;
204954 p->iEnd = p->i + pNode->n + 1;
204955 if( p->bRecursive ){
204956 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
204957 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
204958 p->i--;
204959 }
204960 }else{
204961 p->i++;
204962 }
204963 }else{
204964 p->iEnd = p->i+1;
204965 }
204966 }
204967 return SQLITE_OK;
204968}
204969
204970/* The methods of the json_each virtual table */
204971static sqlite3_module jsonEachModule = {
204972 0, /* iVersion */
204973 0, /* xCreate */
204974 jsonEachConnect, /* xConnect */
204975 jsonEachBestIndex, /* xBestIndex */
204976 jsonEachDisconnect, /* xDisconnect */
204977 0, /* xDestroy */
204978 jsonEachOpenEach, /* xOpen - open a cursor */
204979 jsonEachClose, /* xClose - close a cursor */
204980 jsonEachFilter, /* xFilter - configure scan constraints */
204981 jsonEachNext, /* xNext - advance a cursor */
204982 jsonEachEof, /* xEof - check for end of scan */
204983 jsonEachColumn, /* xColumn - read data */
204984 jsonEachRowid, /* xRowid - read data */
204985 0, /* xUpdate */
204986 0, /* xBegin */
204987 0, /* xSync */
204988 0, /* xCommit */
204989 0, /* xRollback */
204990 0, /* xFindMethod */
204991 0, /* xRename */
204992 0, /* xSavepoint */
204993 0, /* xRelease */
204994 0, /* xRollbackTo */
204995 0 /* xShadowName */
204996};
204997
204998/* The methods of the json_tree virtual table. */
204999static sqlite3_module jsonTreeModule = {
205000 0, /* iVersion */
205001 0, /* xCreate */
205002 jsonEachConnect, /* xConnect */
205003 jsonEachBestIndex, /* xBestIndex */
205004 jsonEachDisconnect, /* xDisconnect */
205005 0, /* xDestroy */
205006 jsonEachOpenTree, /* xOpen - open a cursor */
205007 jsonEachClose, /* xClose - close a cursor */
205008 jsonEachFilter, /* xFilter - configure scan constraints */
205009 jsonEachNext, /* xNext - advance a cursor */
205010 jsonEachEof, /* xEof - check for end of scan */
205011 jsonEachColumn, /* xColumn - read data */
205012 jsonEachRowid, /* xRowid - read data */
205013 0, /* xUpdate */
205014 0, /* xBegin */
205015 0, /* xSync */
205016 0, /* xCommit */
205017 0, /* xRollback */
205018 0, /* xFindMethod */
205019 0, /* xRename */
205020 0, /* xSavepoint */
205021 0, /* xRelease */
205022 0, /* xRollbackTo */
205023 0 /* xShadowName */
205024};
205025#endif /* SQLITE_OMIT_VIRTUALTABLE */
205026#endif /* !defined(SQLITE_OMIT_JSON) */
205027
205028/*
205029** Register JSON functions.
205030*/
205031SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void){
205032#ifndef SQLITE_OMIT_JSON
205033 static FuncDef aJsonFunc[] = {
205034 JFUNCTION(json, 1, 0, jsonRemoveFunc),
205035 JFUNCTION(json_array, -1, 0, jsonArrayFunc),
205036 JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
205037 JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
205038 JFUNCTION(json_error_position,1, 0, jsonErrorFunc),
205039 JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
205040 JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
205041 JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),
205042 JFUNCTION(json_insert, -1, 0, jsonSetFunc),
205043 JFUNCTION(json_object, -1, 0, jsonObjectFunc),
205044 JFUNCTION(json_patch, 2, 0, jsonPatchFunc),
205045 JFUNCTION(json_quote, 1, 0, jsonQuoteFunc),
205046 JFUNCTION(json_remove, -1, 0, jsonRemoveFunc),
205047 JFUNCTION(json_replace, -1, 0, jsonReplaceFunc),
205048 JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc),
205049 JFUNCTION(json_type, 1, 0, jsonTypeFunc),
205050 JFUNCTION(json_type, 2, 0, jsonTypeFunc),
205051 JFUNCTION(json_valid, 1, 0, jsonValidFunc),
205052#if SQLITE_DEBUG
205053 JFUNCTION(json_parse, 1, 0, jsonParseFunc),
205054 JFUNCTION(json_test1, 1, 0, jsonTest1Func),
205055#endif
205056 WAGGREGATE(json_group_array, 1, 0, 0,
205057 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
205058 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
205059 WAGGREGATE(json_group_object, 2, 0, 0,
205060 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
205061 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
205062 };
205063 sqlite3InsertBuiltinFuncs(aDef: aJsonFunc, ArraySize(aJsonFunc));
205064#endif
205065}
205066
205067#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
205068/*
205069** Register the JSON table-valued functions
205070*/
205071SQLITE_PRIVATE int sqlite3JsonTableFunctions(sqlite3 *db){
205072 int rc = SQLITE_OK;
205073 static const struct {
205074 const char *zName;
205075 sqlite3_module *pModule;
205076 } aMod[] = {
205077 { "json_each", &jsonEachModule },
205078 { "json_tree", &jsonTreeModule },
205079 };
205080 unsigned int i;
205081 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
205082 rc = sqlite3_create_module(db, zName: aMod[i].zName, pModule: aMod[i].pModule, pAux: 0);
205083 }
205084 return rc;
205085}
205086#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */
205087
205088/************** End of json.c ************************************************/
205089/************** Begin file rtree.c *******************************************/
205090/*
205091** 2001 September 15
205092**
205093** The author disclaims copyright to this source code. In place of
205094** a legal notice, here is a blessing:
205095**
205096** May you do good and not evil.
205097** May you find forgiveness for yourself and forgive others.
205098** May you share freely, never taking more than you give.
205099**
205100*************************************************************************
205101** This file contains code for implementations of the r-tree and r*-tree
205102** algorithms packaged as an SQLite virtual table module.
205103*/
205104
205105/*
205106** Database Format of R-Tree Tables
205107** --------------------------------
205108**
205109** The data structure for a single virtual r-tree table is stored in three
205110** native SQLite tables declared as follows. In each case, the '%' character
205111** in the table name is replaced with the user-supplied name of the r-tree
205112** table.
205113**
205114** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
205115** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
205116** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
205117**
205118** The data for each node of the r-tree structure is stored in the %_node
205119** table. For each node that is not the root node of the r-tree, there is
205120** an entry in the %_parent table associating the node with its parent.
205121** And for each row of data in the table, there is an entry in the %_rowid
205122** table that maps from the entries rowid to the id of the node that it
205123** is stored on. If the r-tree contains auxiliary columns, those are stored
205124** on the end of the %_rowid table.
205125**
205126** The root node of an r-tree always exists, even if the r-tree table is
205127** empty. The nodeno of the root node is always 1. All other nodes in the
205128** table must be the same size as the root node. The content of each node
205129** is formatted as follows:
205130**
205131** 1. If the node is the root node (node 1), then the first 2 bytes
205132** of the node contain the tree depth as a big-endian integer.
205133** For non-root nodes, the first 2 bytes are left unused.
205134**
205135** 2. The next 2 bytes contain the number of entries currently
205136** stored in the node.
205137**
205138** 3. The remainder of the node contains the node entries. Each entry
205139** consists of a single 8-byte integer followed by an even number
205140** of 4-byte coordinates. For leaf nodes the integer is the rowid
205141** of a record. For internal nodes it is the node number of a
205142** child page.
205143*/
205144
205145#if !defined(SQLITE_CORE) \
205146 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
205147
205148#ifndef SQLITE_CORE
205149/* #include "sqlite3ext.h" */
205150 SQLITE_EXTENSION_INIT1
205151#else
205152/* #include "sqlite3.h" */
205153#endif
205154SQLITE_PRIVATE int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
205155
205156/*
205157** If building separately, we will need some setup that is normally
205158** found in sqliteInt.h
205159*/
205160#if !defined(SQLITE_AMALGAMATION)
205161#include "sqlite3rtree.h"
205162typedef sqlite3_int64 i64;
205163typedef sqlite3_uint64 u64;
205164typedef unsigned char u8;
205165typedef unsigned short u16;
205166typedef unsigned int u32;
205167#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
205168# define NDEBUG 1
205169#endif
205170#if defined(NDEBUG) && defined(SQLITE_DEBUG)
205171# undef NDEBUG
205172#endif
205173#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
205174# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
205175#endif
205176#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
205177# define ALWAYS(X) (1)
205178# define NEVER(X) (0)
205179#elif !defined(NDEBUG)
205180# define ALWAYS(X) ((X)?1:(assert(0),0))
205181# define NEVER(X) ((X)?(assert(0),1):0)
205182#else
205183# define ALWAYS(X) (X)
205184# define NEVER(X) (X)
205185#endif
205186#endif /* !defined(SQLITE_AMALGAMATION) */
205187
205188/* Macro to check for 4-byte alignment. Only used inside of assert() */
205189#ifdef SQLITE_DEBUG
205190# define FOUR_BYTE_ALIGNED(X) ((((char*)(X) - (char*)0) & 3)==0)
205191#endif
205192
205193/* #include <string.h> */
205194/* #include <stdio.h> */
205195/* #include <assert.h> */
205196/* #include <stdlib.h> */
205197
205198/* The following macro is used to suppress compiler warnings.
205199*/
205200#ifndef UNUSED_PARAMETER
205201# define UNUSED_PARAMETER(x) (void)(x)
205202#endif
205203
205204typedef struct Rtree Rtree;
205205typedef struct RtreeCursor RtreeCursor;
205206typedef struct RtreeNode RtreeNode;
205207typedef struct RtreeCell RtreeCell;
205208typedef struct RtreeConstraint RtreeConstraint;
205209typedef struct RtreeMatchArg RtreeMatchArg;
205210typedef struct RtreeGeomCallback RtreeGeomCallback;
205211typedef union RtreeCoord RtreeCoord;
205212typedef struct RtreeSearchPoint RtreeSearchPoint;
205213
205214/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
205215#define RTREE_MAX_DIMENSIONS 5
205216
205217/* Maximum number of auxiliary columns */
205218#define RTREE_MAX_AUX_COLUMN 100
205219
205220/* Size of hash table Rtree.aHash. This hash table is not expected to
205221** ever contain very many entries, so a fixed number of buckets is
205222** used.
205223*/
205224#define HASHSIZE 97
205225
205226/* The xBestIndex method of this virtual table requires an estimate of
205227** the number of rows in the virtual table to calculate the costs of
205228** various strategies. If possible, this estimate is loaded from the
205229** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
205230** Otherwise, if no sqlite_stat1 entry is available, use
205231** RTREE_DEFAULT_ROWEST.
205232*/
205233#define RTREE_DEFAULT_ROWEST 1048576
205234#define RTREE_MIN_ROWEST 100
205235
205236/*
205237** An rtree virtual-table object.
205238*/
205239struct Rtree {
205240 sqlite3_vtab base; /* Base class. Must be first */
205241 sqlite3 *db; /* Host database connection */
205242 int iNodeSize; /* Size in bytes of each node in the node table */
205243 u8 nDim; /* Number of dimensions */
205244 u8 nDim2; /* Twice the number of dimensions */
205245 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
205246 u8 nBytesPerCell; /* Bytes consumed per cell */
205247 u8 inWrTrans; /* True if inside write transaction */
205248 u8 nAux; /* # of auxiliary columns in %_rowid */
205249#ifdef SQLITE_ENABLE_GEOPOLY
205250 u8 nAuxNotNull; /* Number of initial not-null aux columns */
205251#endif
205252#ifdef SQLITE_DEBUG
205253 u8 bCorrupt; /* Shadow table corruption detected */
205254#endif
205255 int iDepth; /* Current depth of the r-tree structure */
205256 char *zDb; /* Name of database containing r-tree table */
205257 char *zName; /* Name of r-tree table */
205258 u32 nBusy; /* Current number of users of this structure */
205259 i64 nRowEst; /* Estimated number of rows in this table */
205260 u32 nCursor; /* Number of open cursors */
205261 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
205262 char *zReadAuxSql; /* SQL for statement to read aux data */
205263
205264 /* List of nodes removed during a CondenseTree operation. List is
205265 ** linked together via the pointer normally used for hash chains -
205266 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
205267 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
205268 */
205269 RtreeNode *pDeleted;
205270 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
205271
205272 /* Blob I/O on xxx_node */
205273 sqlite3_blob *pNodeBlob;
205274
205275 /* Statements to read/write/delete a record from xxx_node */
205276 sqlite3_stmt *pWriteNode;
205277 sqlite3_stmt *pDeleteNode;
205278
205279 /* Statements to read/write/delete a record from xxx_rowid */
205280 sqlite3_stmt *pReadRowid;
205281 sqlite3_stmt *pWriteRowid;
205282 sqlite3_stmt *pDeleteRowid;
205283
205284 /* Statements to read/write/delete a record from xxx_parent */
205285 sqlite3_stmt *pReadParent;
205286 sqlite3_stmt *pWriteParent;
205287 sqlite3_stmt *pDeleteParent;
205288
205289 /* Statement for writing to the "aux:" fields, if there are any */
205290 sqlite3_stmt *pWriteAux;
205291
205292 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
205293};
205294
205295/* Possible values for Rtree.eCoordType: */
205296#define RTREE_COORD_REAL32 0
205297#define RTREE_COORD_INT32 1
205298
205299/*
205300** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
205301** only deal with integer coordinates. No floating point operations
205302** will be done.
205303*/
205304#ifdef SQLITE_RTREE_INT_ONLY
205305 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
205306 typedef int RtreeValue; /* Low accuracy coordinate */
205307# define RTREE_ZERO 0
205308#else
205309 typedef double RtreeDValue; /* High accuracy coordinate */
205310 typedef float RtreeValue; /* Low accuracy coordinate */
205311# define RTREE_ZERO 0.0
205312#endif
205313
205314/*
205315** Set the Rtree.bCorrupt flag
205316*/
205317#ifdef SQLITE_DEBUG
205318# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
205319#else
205320# define RTREE_IS_CORRUPT(X)
205321#endif
205322
205323/*
205324** When doing a search of an r-tree, instances of the following structure
205325** record intermediate results from the tree walk.
205326**
205327** The id is always a node-id. For iLevel>=1 the id is the node-id of
205328** the node that the RtreeSearchPoint represents. When iLevel==0, however,
205329** the id is of the parent node and the cell that RtreeSearchPoint
205330** represents is the iCell-th entry in the parent node.
205331*/
205332struct RtreeSearchPoint {
205333 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
205334 sqlite3_int64 id; /* Node ID */
205335 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
205336 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
205337 u8 iCell; /* Cell index within the node */
205338};
205339
205340/*
205341** The minimum number of cells allowed for a node is a third of the
205342** maximum. In Gutman's notation:
205343**
205344** m = M/3
205345**
205346** If an R*-tree "Reinsert" operation is required, the same number of
205347** cells are removed from the overfull node and reinserted into the tree.
205348*/
205349#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
205350#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
205351#define RTREE_MAXCELLS 51
205352
205353/*
205354** The smallest possible node-size is (512-64)==448 bytes. And the largest
205355** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
205356** Therefore all non-root nodes must contain at least 3 entries. Since
205357** 3^40 is greater than 2^64, an r-tree structure always has a depth of
205358** 40 or less.
205359*/
205360#define RTREE_MAX_DEPTH 40
205361
205362
205363/*
205364** Number of entries in the cursor RtreeNode cache. The first entry is
205365** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
205366** entries cache the RtreeNode for the first elements of the priority queue.
205367*/
205368#define RTREE_CACHE_SZ 5
205369
205370/*
205371** An rtree cursor object.
205372*/
205373struct RtreeCursor {
205374 sqlite3_vtab_cursor base; /* Base class. Must be first */
205375 u8 atEOF; /* True if at end of search */
205376 u8 bPoint; /* True if sPoint is valid */
205377 u8 bAuxValid; /* True if pReadAux is valid */
205378 int iStrategy; /* Copy of idxNum search parameter */
205379 int nConstraint; /* Number of entries in aConstraint */
205380 RtreeConstraint *aConstraint; /* Search constraints. */
205381 int nPointAlloc; /* Number of slots allocated for aPoint[] */
205382 int nPoint; /* Number of slots used in aPoint[] */
205383 int mxLevel; /* iLevel value for root of the tree */
205384 RtreeSearchPoint *aPoint; /* Priority queue for search points */
205385 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
205386 RtreeSearchPoint sPoint; /* Cached next search point */
205387 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
205388 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
205389};
205390
205391/* Return the Rtree of a RtreeCursor */
205392#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
205393
205394/*
205395** A coordinate can be either a floating point number or a integer. All
205396** coordinates within a single R-Tree are always of the same time.
205397*/
205398union RtreeCoord {
205399 RtreeValue f; /* Floating point value */
205400 int i; /* Integer value */
205401 u32 u; /* Unsigned for byte-order conversions */
205402};
205403
205404/*
205405** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
205406** formatted as a RtreeDValue (double or int64). This macro assumes that local
205407** variable pRtree points to the Rtree structure associated with the
205408** RtreeCoord.
205409*/
205410#ifdef SQLITE_RTREE_INT_ONLY
205411# define DCOORD(coord) ((RtreeDValue)coord.i)
205412#else
205413# define DCOORD(coord) ( \
205414 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
205415 ((double)coord.f) : \
205416 ((double)coord.i) \
205417 )
205418#endif
205419
205420/*
205421** A search constraint.
205422*/
205423struct RtreeConstraint {
205424 int iCoord; /* Index of constrained coordinate */
205425 int op; /* Constraining operation */
205426 union {
205427 RtreeDValue rValue; /* Constraint value. */
205428 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
205429 int (*xQueryFunc)(sqlite3_rtree_query_info*);
205430 } u;
205431 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
205432};
205433
205434/* Possible values for RtreeConstraint.op */
205435#define RTREE_EQ 0x41 /* A */
205436#define RTREE_LE 0x42 /* B */
205437#define RTREE_LT 0x43 /* C */
205438#define RTREE_GE 0x44 /* D */
205439#define RTREE_GT 0x45 /* E */
205440#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
205441#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
205442
205443/* Special operators available only on cursors. Needs to be consecutive
205444** with the normal values above, but must be less than RTREE_MATCH. These
205445** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
205446** x<'xyz' (RTREE_TRUE) */
205447#define RTREE_TRUE 0x3f /* ? */
205448#define RTREE_FALSE 0x40 /* @ */
205449
205450/*
205451** An rtree structure node.
205452*/
205453struct RtreeNode {
205454 RtreeNode *pParent; /* Parent node */
205455 i64 iNode; /* The node number */
205456 int nRef; /* Number of references to this node */
205457 int isDirty; /* True if the node needs to be written to disk */
205458 u8 *zData; /* Content of the node, as should be on disk */
205459 RtreeNode *pNext; /* Next node in this hash collision chain */
205460};
205461
205462/* Return the number of cells in a node */
205463#define NCELL(pNode) readInt16(&(pNode)->zData[2])
205464
205465/*
205466** A single cell from a node, deserialized
205467*/
205468struct RtreeCell {
205469 i64 iRowid; /* Node or entry ID */
205470 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
205471};
205472
205473
205474/*
205475** This object becomes the sqlite3_user_data() for the SQL functions
205476** that are created by sqlite3_rtree_geometry_callback() and
205477** sqlite3_rtree_query_callback() and which appear on the right of MATCH
205478** operators in order to constrain a search.
205479**
205480** xGeom and xQueryFunc are the callback functions. Exactly one of
205481** xGeom and xQueryFunc fields is non-NULL, depending on whether the
205482** SQL function was created using sqlite3_rtree_geometry_callback() or
205483** sqlite3_rtree_query_callback().
205484**
205485** This object is deleted automatically by the destructor mechanism in
205486** sqlite3_create_function_v2().
205487*/
205488struct RtreeGeomCallback {
205489 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
205490 int (*xQueryFunc)(sqlite3_rtree_query_info*);
205491 void (*xDestructor)(void*);
205492 void *pContext;
205493};
205494
205495/*
205496** An instance of this structure (in the form of a BLOB) is returned by
205497** the SQL functions that sqlite3_rtree_geometry_callback() and
205498** sqlite3_rtree_query_callback() create, and is read as the right-hand
205499** operand to the MATCH operator of an R-Tree.
205500*/
205501struct RtreeMatchArg {
205502 u32 iSize; /* Size of this object */
205503 RtreeGeomCallback cb; /* Info about the callback functions */
205504 int nParam; /* Number of parameters to the SQL function */
205505 sqlite3_value **apSqlParam; /* Original SQL parameter values */
205506 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
205507};
205508
205509#ifndef MAX
205510# define MAX(x,y) ((x) < (y) ? (y) : (x))
205511#endif
205512#ifndef MIN
205513# define MIN(x,y) ((x) > (y) ? (y) : (x))
205514#endif
205515
205516/* What version of GCC is being used. 0 means GCC is not being used .
205517** Note that the GCC_VERSION macro will also be set correctly when using
205518** clang, since clang works hard to be gcc compatible. So the gcc
205519** optimizations will also work when compiling with clang.
205520*/
205521#ifndef GCC_VERSION
205522#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
205523# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
205524#else
205525# define GCC_VERSION 0
205526#endif
205527#endif
205528
205529/* The testcase() macro should already be defined in the amalgamation. If
205530** it is not, make it a no-op.
205531*/
205532#ifndef SQLITE_AMALGAMATION
205533# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
205534 unsigned int sqlite3RtreeTestcase = 0;
205535# define testcase(X) if( X ){ sqlite3RtreeTestcase += __LINE__; }
205536# else
205537# define testcase(X)
205538# endif
205539#endif
205540
205541/*
205542** Make sure that the compiler intrinsics we desire are enabled when
205543** compiling with an appropriate version of MSVC unless prevented by
205544** the SQLITE_DISABLE_INTRINSIC define.
205545*/
205546#if !defined(SQLITE_DISABLE_INTRINSIC)
205547# if defined(_MSC_VER) && _MSC_VER>=1400
205548# if !defined(_WIN32_WCE)
205549/* # include <intrin.h> */
205550# pragma intrinsic(_byteswap_ulong)
205551# pragma intrinsic(_byteswap_uint64)
205552# else
205553/* # include <cmnintrin.h> */
205554# endif
205555# endif
205556#endif
205557
205558/*
205559** Macros to determine whether the machine is big or little endian,
205560** and whether or not that determination is run-time or compile-time.
205561**
205562** For best performance, an attempt is made to guess at the byte-order
205563** using C-preprocessor macros. If that is unsuccessful, or if
205564** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
205565** at run-time.
205566*/
205567#ifndef SQLITE_BYTEORDER
205568# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
205569 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
205570 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
205571 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
205572# define SQLITE_BYTEORDER 1234
205573# elif defined(sparc) || defined(__ppc__) || \
205574 defined(__ARMEB__) || defined(__AARCH64EB__)
205575# define SQLITE_BYTEORDER 4321
205576# else
205577# define SQLITE_BYTEORDER 0
205578# endif
205579#endif
205580
205581
205582/* What version of MSVC is being used. 0 means MSVC is not being used */
205583#ifndef MSVC_VERSION
205584#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
205585# define MSVC_VERSION _MSC_VER
205586#else
205587# define MSVC_VERSION 0
205588#endif
205589#endif
205590
205591/*
205592** Functions to deserialize a 16 bit integer, 32 bit real number and
205593** 64 bit integer. The deserialized value is returned.
205594*/
205595static int readInt16(u8 *p){
205596 return (p[0]<<8) + p[1];
205597}
205598static void readCoord(u8 *p, RtreeCoord *pCoord){
205599 assert( FOUR_BYTE_ALIGNED(p) );
205600#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
205601 pCoord->u = _byteswap_ulong(*(u32*)p);
205602#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
205603 pCoord->u = __builtin_bswap32(*(u32*)p);
205604#elif SQLITE_BYTEORDER==4321
205605 pCoord->u = *(u32*)p;
205606#else
205607 pCoord->u = (
205608 (((u32)p[0]) << 24) +
205609 (((u32)p[1]) << 16) +
205610 (((u32)p[2]) << 8) +
205611 (((u32)p[3]) << 0)
205612 );
205613#endif
205614}
205615static i64 readInt64(u8 *p){
205616#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
205617 u64 x;
205618 memcpy(&x, p, 8);
205619 return (i64)_byteswap_uint64(x);
205620#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
205621 u64 x;
205622 memcpy(&x, p, 8);
205623 return (i64)__builtin_bswap64(x);
205624#elif SQLITE_BYTEORDER==4321
205625 i64 x;
205626 memcpy(&x, p, 8);
205627 return x;
205628#else
205629 return (i64)(
205630 (((u64)p[0]) << 56) +
205631 (((u64)p[1]) << 48) +
205632 (((u64)p[2]) << 40) +
205633 (((u64)p[3]) << 32) +
205634 (((u64)p[4]) << 24) +
205635 (((u64)p[5]) << 16) +
205636 (((u64)p[6]) << 8) +
205637 (((u64)p[7]) << 0)
205638 );
205639#endif
205640}
205641
205642/*
205643** Functions to serialize a 16 bit integer, 32 bit real number and
205644** 64 bit integer. The value returned is the number of bytes written
205645** to the argument buffer (always 2, 4 and 8 respectively).
205646*/
205647static void writeInt16(u8 *p, int i){
205648 p[0] = (i>> 8)&0xFF;
205649 p[1] = (i>> 0)&0xFF;
205650}
205651static int writeCoord(u8 *p, RtreeCoord *pCoord){
205652 u32 i;
205653 assert( FOUR_BYTE_ALIGNED(p) );
205654 assert( sizeof(RtreeCoord)==4 );
205655 assert( sizeof(u32)==4 );
205656#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
205657 i = __builtin_bswap32(pCoord->u);
205658 memcpy(p, &i, 4);
205659#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
205660 i = _byteswap_ulong(pCoord->u);
205661 memcpy(p, &i, 4);
205662#elif SQLITE_BYTEORDER==4321
205663 i = pCoord->u;
205664 memcpy(p, &i, 4);
205665#else
205666 i = pCoord->u;
205667 p[0] = (i>>24)&0xFF;
205668 p[1] = (i>>16)&0xFF;
205669 p[2] = (i>> 8)&0xFF;
205670 p[3] = (i>> 0)&0xFF;
205671#endif
205672 return 4;
205673}
205674static int writeInt64(u8 *p, i64 i){
205675#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
205676 i = (i64)__builtin_bswap64((u64)i);
205677 memcpy(p, &i, 8);
205678#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
205679 i = (i64)_byteswap_uint64((u64)i);
205680 memcpy(p, &i, 8);
205681#elif SQLITE_BYTEORDER==4321
205682 memcpy(p, &i, 8);
205683#else
205684 p[0] = (i>>56)&0xFF;
205685 p[1] = (i>>48)&0xFF;
205686 p[2] = (i>>40)&0xFF;
205687 p[3] = (i>>32)&0xFF;
205688 p[4] = (i>>24)&0xFF;
205689 p[5] = (i>>16)&0xFF;
205690 p[6] = (i>> 8)&0xFF;
205691 p[7] = (i>> 0)&0xFF;
205692#endif
205693 return 8;
205694}
205695
205696/*
205697** Increment the reference count of node p.
205698*/
205699static void nodeReference(RtreeNode *p){
205700 if( p ){
205701 assert( p->nRef>0 );
205702 p->nRef++;
205703 }
205704}
205705
205706/*
205707** Clear the content of node p (set all bytes to 0x00).
205708*/
205709static void nodeZero(Rtree *pRtree, RtreeNode *p){
205710 memset(s: &p->zData[2], c: 0, n: pRtree->iNodeSize-2);
205711 p->isDirty = 1;
205712}
205713
205714/*
205715** Given a node number iNode, return the corresponding key to use
205716** in the Rtree.aHash table.
205717*/
205718static unsigned int nodeHash(i64 iNode){
205719 return ((unsigned)iNode) % HASHSIZE;
205720}
205721
205722/*
205723** Search the node hash table for node iNode. If found, return a pointer
205724** to it. Otherwise, return 0.
205725*/
205726static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
205727 RtreeNode *p;
205728 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
205729 return p;
205730}
205731
205732/*
205733** Add node pNode to the node hash table.
205734*/
205735static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
205736 int iHash;
205737 assert( pNode->pNext==0 );
205738 iHash = nodeHash(iNode: pNode->iNode);
205739 pNode->pNext = pRtree->aHash[iHash];
205740 pRtree->aHash[iHash] = pNode;
205741}
205742
205743/*
205744** Remove node pNode from the node hash table.
205745*/
205746static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
205747 RtreeNode **pp;
205748 if( pNode->iNode!=0 ){
205749 pp = &pRtree->aHash[nodeHash(iNode: pNode->iNode)];
205750 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
205751 *pp = pNode->pNext;
205752 pNode->pNext = 0;
205753 }
205754}
205755
205756/*
205757** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
205758** indicating that node has not yet been assigned a node number. It is
205759** assigned a node number when nodeWrite() is called to write the
205760** node contents out to the database.
205761*/
205762static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
205763 RtreeNode *pNode;
205764 pNode = (RtreeNode *)sqlite3_malloc64(n: sizeof(RtreeNode) + pRtree->iNodeSize);
205765 if( pNode ){
205766 memset(s: pNode, c: 0, n: sizeof(RtreeNode) + pRtree->iNodeSize);
205767 pNode->zData = (u8 *)&pNode[1];
205768 pNode->nRef = 1;
205769 pRtree->nNodeRef++;
205770 pNode->pParent = pParent;
205771 pNode->isDirty = 1;
205772 nodeReference(p: pParent);
205773 }
205774 return pNode;
205775}
205776
205777/*
205778** Clear the Rtree.pNodeBlob object
205779*/
205780static void nodeBlobReset(Rtree *pRtree){
205781 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
205782 sqlite3_blob *pBlob = pRtree->pNodeBlob;
205783 pRtree->pNodeBlob = 0;
205784 sqlite3_blob_close(pBlob);
205785 }
205786}
205787
205788/*
205789** Obtain a reference to an r-tree node.
205790*/
205791static int nodeAcquire(
205792 Rtree *pRtree, /* R-tree structure */
205793 i64 iNode, /* Node number to load */
205794 RtreeNode *pParent, /* Either the parent node or NULL */
205795 RtreeNode **ppNode /* OUT: Acquired node */
205796){
205797 int rc = SQLITE_OK;
205798 RtreeNode *pNode = 0;
205799
205800 /* Check if the requested node is already in the hash table. If so,
205801 ** increase its reference count and return it.
205802 */
205803 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
205804 if( pParent && pParent!=pNode->pParent ){
205805 RTREE_IS_CORRUPT(pRtree);
205806 return SQLITE_CORRUPT_VTAB;
205807 }
205808 pNode->nRef++;
205809 *ppNode = pNode;
205810 return SQLITE_OK;
205811 }
205812
205813 if( pRtree->pNodeBlob ){
205814 sqlite3_blob *pBlob = pRtree->pNodeBlob;
205815 pRtree->pNodeBlob = 0;
205816 rc = sqlite3_blob_reopen(pBlob, iRow: iNode);
205817 pRtree->pNodeBlob = pBlob;
205818 if( rc ){
205819 nodeBlobReset(pRtree);
205820 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
205821 }
205822 }
205823 if( pRtree->pNodeBlob==0 ){
205824 char *zTab = sqlite3_mprintf(zFormat: "%s_node", pRtree->zName);
205825 if( zTab==0 ) return SQLITE_NOMEM;
205826 rc = sqlite3_blob_open(db: pRtree->db, zDb: pRtree->zDb, zTable: zTab, zColumn: "data", iRow: iNode, wrFlag: 0,
205827 ppBlob: &pRtree->pNodeBlob);
205828 sqlite3_free(p: zTab);
205829 }
205830 if( rc ){
205831 nodeBlobReset(pRtree);
205832 *ppNode = 0;
205833 /* If unable to open an sqlite3_blob on the desired row, that can only
205834 ** be because the shadow tables hold erroneous data. */
205835 if( rc==SQLITE_ERROR ){
205836 rc = SQLITE_CORRUPT_VTAB;
205837 RTREE_IS_CORRUPT(pRtree);
205838 }
205839 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pBlob: pRtree->pNodeBlob) ){
205840 pNode = (RtreeNode *)sqlite3_malloc64(n: sizeof(RtreeNode)+pRtree->iNodeSize);
205841 if( !pNode ){
205842 rc = SQLITE_NOMEM;
205843 }else{
205844 pNode->pParent = pParent;
205845 pNode->zData = (u8 *)&pNode[1];
205846 pNode->nRef = 1;
205847 pRtree->nNodeRef++;
205848 pNode->iNode = iNode;
205849 pNode->isDirty = 0;
205850 pNode->pNext = 0;
205851 rc = sqlite3_blob_read(pBlob: pRtree->pNodeBlob, z: pNode->zData,
205852 n: pRtree->iNodeSize, iOffset: 0);
205853 }
205854 }
205855
205856 /* If the root node was just loaded, set pRtree->iDepth to the height
205857 ** of the r-tree structure. A height of zero means all data is stored on
205858 ** the root node. A height of one means the children of the root node
205859 ** are the leaves, and so on. If the depth as specified on the root node
205860 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
205861 */
205862 if( rc==SQLITE_OK && pNode && iNode==1 ){
205863 pRtree->iDepth = readInt16(p: pNode->zData);
205864 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
205865 rc = SQLITE_CORRUPT_VTAB;
205866 RTREE_IS_CORRUPT(pRtree);
205867 }
205868 }
205869
205870 /* If no error has occurred so far, check if the "number of entries"
205871 ** field on the node is too large. If so, set the return code to
205872 ** SQLITE_CORRUPT_VTAB.
205873 */
205874 if( pNode && rc==SQLITE_OK ){
205875 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
205876 rc = SQLITE_CORRUPT_VTAB;
205877 RTREE_IS_CORRUPT(pRtree);
205878 }
205879 }
205880
205881 if( rc==SQLITE_OK ){
205882 if( pNode!=0 ){
205883 nodeReference(p: pParent);
205884 nodeHashInsert(pRtree, pNode);
205885 }else{
205886 rc = SQLITE_CORRUPT_VTAB;
205887 RTREE_IS_CORRUPT(pRtree);
205888 }
205889 *ppNode = pNode;
205890 }else{
205891 if( pNode ){
205892 pRtree->nNodeRef--;
205893 sqlite3_free(p: pNode);
205894 }
205895 *ppNode = 0;
205896 }
205897
205898 return rc;
205899}
205900
205901/*
205902** Overwrite cell iCell of node pNode with the contents of pCell.
205903*/
205904static void nodeOverwriteCell(
205905 Rtree *pRtree, /* The overall R-Tree */
205906 RtreeNode *pNode, /* The node into which the cell is to be written */
205907 RtreeCell *pCell, /* The cell to write */
205908 int iCell /* Index into pNode into which pCell is written */
205909){
205910 int ii;
205911 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
205912 p += writeInt64(p, i: pCell->iRowid);
205913 for(ii=0; ii<pRtree->nDim2; ii++){
205914 p += writeCoord(p, pCoord: &pCell->aCoord[ii]);
205915 }
205916 pNode->isDirty = 1;
205917}
205918
205919/*
205920** Remove the cell with index iCell from node pNode.
205921*/
205922static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
205923 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
205924 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
205925 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
205926 memmove(dest: pDst, src: pSrc, n: nByte);
205927 writeInt16(p: &pNode->zData[2], NCELL(pNode)-1);
205928 pNode->isDirty = 1;
205929}
205930
205931/*
205932** Insert the contents of cell pCell into node pNode. If the insert
205933** is successful, return SQLITE_OK.
205934**
205935** If there is not enough free space in pNode, return SQLITE_FULL.
205936*/
205937static int nodeInsertCell(
205938 Rtree *pRtree, /* The overall R-Tree */
205939 RtreeNode *pNode, /* Write new cell into this node */
205940 RtreeCell *pCell /* The cell to be inserted */
205941){
205942 int nCell; /* Current number of cells in pNode */
205943 int nMaxCell; /* Maximum number of cells for pNode */
205944
205945 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
205946 nCell = NCELL(pNode);
205947
205948 assert( nCell<=nMaxCell );
205949 if( nCell<nMaxCell ){
205950 nodeOverwriteCell(pRtree, pNode, pCell, iCell: nCell);
205951 writeInt16(p: &pNode->zData[2], i: nCell+1);
205952 pNode->isDirty = 1;
205953 }
205954
205955 return (nCell==nMaxCell);
205956}
205957
205958/*
205959** If the node is dirty, write it out to the database.
205960*/
205961static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
205962 int rc = SQLITE_OK;
205963 if( pNode->isDirty ){
205964 sqlite3_stmt *p = pRtree->pWriteNode;
205965 if( pNode->iNode ){
205966 sqlite3_bind_int64(pStmt: p, i: 1, iValue: pNode->iNode);
205967 }else{
205968 sqlite3_bind_null(pStmt: p, i: 1);
205969 }
205970 sqlite3_bind_blob(pStmt: p, i: 2, zData: pNode->zData, nData: pRtree->iNodeSize, SQLITE_STATIC);
205971 sqlite3_step(pStmt: p);
205972 pNode->isDirty = 0;
205973 rc = sqlite3_reset(pStmt: p);
205974 sqlite3_bind_null(pStmt: p, i: 2);
205975 if( pNode->iNode==0 && rc==SQLITE_OK ){
205976 pNode->iNode = sqlite3_last_insert_rowid(db: pRtree->db);
205977 nodeHashInsert(pRtree, pNode);
205978 }
205979 }
205980 return rc;
205981}
205982
205983/*
205984** Release a reference to a node. If the node is dirty and the reference
205985** count drops to zero, the node data is written to the database.
205986*/
205987static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
205988 int rc = SQLITE_OK;
205989 if( pNode ){
205990 assert( pNode->nRef>0 );
205991 assert( pRtree->nNodeRef>0 );
205992 pNode->nRef--;
205993 if( pNode->nRef==0 ){
205994 pRtree->nNodeRef--;
205995 if( pNode->iNode==1 ){
205996 pRtree->iDepth = -1;
205997 }
205998 if( pNode->pParent ){
205999 rc = nodeRelease(pRtree, pNode: pNode->pParent);
206000 }
206001 if( rc==SQLITE_OK ){
206002 rc = nodeWrite(pRtree, pNode);
206003 }
206004 nodeHashDelete(pRtree, pNode);
206005 sqlite3_free(p: pNode);
206006 }
206007 }
206008 return rc;
206009}
206010
206011/*
206012** Return the 64-bit integer value associated with cell iCell of
206013** node pNode. If pNode is a leaf node, this is a rowid. If it is
206014** an internal node, then the 64-bit integer is a child page number.
206015*/
206016static i64 nodeGetRowid(
206017 Rtree *pRtree, /* The overall R-Tree */
206018 RtreeNode *pNode, /* The node from which to extract the ID */
206019 int iCell /* The cell index from which to extract the ID */
206020){
206021 assert( iCell<NCELL(pNode) );
206022 return readInt64(p: &pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
206023}
206024
206025/*
206026** Return coordinate iCoord from cell iCell in node pNode.
206027*/
206028static void nodeGetCoord(
206029 Rtree *pRtree, /* The overall R-Tree */
206030 RtreeNode *pNode, /* The node from which to extract a coordinate */
206031 int iCell, /* The index of the cell within the node */
206032 int iCoord, /* Which coordinate to extract */
206033 RtreeCoord *pCoord /* OUT: Space to write result to */
206034){
206035 readCoord(p: &pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
206036}
206037
206038/*
206039** Deserialize cell iCell of node pNode. Populate the structure pointed
206040** to by pCell with the results.
206041*/
206042static void nodeGetCell(
206043 Rtree *pRtree, /* The overall R-Tree */
206044 RtreeNode *pNode, /* The node containing the cell to be read */
206045 int iCell, /* Index of the cell within the node */
206046 RtreeCell *pCell /* OUT: Write the cell contents here */
206047){
206048 u8 *pData;
206049 RtreeCoord *pCoord;
206050 int ii = 0;
206051 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
206052 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
206053 pCoord = pCell->aCoord;
206054 do{
206055 readCoord(p: pData, pCoord: &pCoord[ii]);
206056 readCoord(p: pData+4, pCoord: &pCoord[ii+1]);
206057 pData += 8;
206058 ii += 2;
206059 }while( ii<pRtree->nDim2 );
206060}
206061
206062
206063/* Forward declaration for the function that does the work of
206064** the virtual table module xCreate() and xConnect() methods.
206065*/
206066static int rtreeInit(
206067 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
206068);
206069
206070/*
206071** Rtree virtual table module xCreate method.
206072*/
206073static int rtreeCreate(
206074 sqlite3 *db,
206075 void *pAux,
206076 int argc, const char *const*argv,
206077 sqlite3_vtab **ppVtab,
206078 char **pzErr
206079){
206080 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
206081}
206082
206083/*
206084** Rtree virtual table module xConnect method.
206085*/
206086static int rtreeConnect(
206087 sqlite3 *db,
206088 void *pAux,
206089 int argc, const char *const*argv,
206090 sqlite3_vtab **ppVtab,
206091 char **pzErr
206092){
206093 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
206094}
206095
206096/*
206097** Increment the r-tree reference count.
206098*/
206099static void rtreeReference(Rtree *pRtree){
206100 pRtree->nBusy++;
206101}
206102
206103/*
206104** Decrement the r-tree reference count. When the reference count reaches
206105** zero the structure is deleted.
206106*/
206107static void rtreeRelease(Rtree *pRtree){
206108 pRtree->nBusy--;
206109 if( pRtree->nBusy==0 ){
206110 pRtree->inWrTrans = 0;
206111 assert( pRtree->nCursor==0 );
206112 nodeBlobReset(pRtree);
206113 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );
206114 sqlite3_finalize(pStmt: pRtree->pWriteNode);
206115 sqlite3_finalize(pStmt: pRtree->pDeleteNode);
206116 sqlite3_finalize(pStmt: pRtree->pReadRowid);
206117 sqlite3_finalize(pStmt: pRtree->pWriteRowid);
206118 sqlite3_finalize(pStmt: pRtree->pDeleteRowid);
206119 sqlite3_finalize(pStmt: pRtree->pReadParent);
206120 sqlite3_finalize(pStmt: pRtree->pWriteParent);
206121 sqlite3_finalize(pStmt: pRtree->pDeleteParent);
206122 sqlite3_finalize(pStmt: pRtree->pWriteAux);
206123 sqlite3_free(p: pRtree->zReadAuxSql);
206124 sqlite3_free(p: pRtree);
206125 }
206126}
206127
206128/*
206129** Rtree virtual table module xDisconnect method.
206130*/
206131static int rtreeDisconnect(sqlite3_vtab *pVtab){
206132 rtreeRelease(pRtree: (Rtree *)pVtab);
206133 return SQLITE_OK;
206134}
206135
206136/*
206137** Rtree virtual table module xDestroy method.
206138*/
206139static int rtreeDestroy(sqlite3_vtab *pVtab){
206140 Rtree *pRtree = (Rtree *)pVtab;
206141 int rc;
206142 char *zCreate = sqlite3_mprintf(
206143 zFormat: "DROP TABLE '%q'.'%q_node';"
206144 "DROP TABLE '%q'.'%q_rowid';"
206145 "DROP TABLE '%q'.'%q_parent';",
206146 pRtree->zDb, pRtree->zName,
206147 pRtree->zDb, pRtree->zName,
206148 pRtree->zDb, pRtree->zName
206149 );
206150 if( !zCreate ){
206151 rc = SQLITE_NOMEM;
206152 }else{
206153 nodeBlobReset(pRtree);
206154 rc = sqlite3_exec(db: pRtree->db, zSql: zCreate, xCallback: 0, pArg: 0, pzErrMsg: 0);
206155 sqlite3_free(p: zCreate);
206156 }
206157 if( rc==SQLITE_OK ){
206158 rtreeRelease(pRtree);
206159 }
206160
206161 return rc;
206162}
206163
206164/*
206165** Rtree virtual table module xOpen method.
206166*/
206167static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
206168 int rc = SQLITE_NOMEM;
206169 Rtree *pRtree = (Rtree *)pVTab;
206170 RtreeCursor *pCsr;
206171
206172 pCsr = (RtreeCursor *)sqlite3_malloc64(n: sizeof(RtreeCursor));
206173 if( pCsr ){
206174 memset(s: pCsr, c: 0, n: sizeof(RtreeCursor));
206175 pCsr->base.pVtab = pVTab;
206176 rc = SQLITE_OK;
206177 pRtree->nCursor++;
206178 }
206179 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
206180
206181 return rc;
206182}
206183
206184
206185/*
206186** Reset a cursor back to its initial state.
206187*/
206188static void resetCursor(RtreeCursor *pCsr){
206189 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
206190 int ii;
206191 sqlite3_stmt *pStmt;
206192 if( pCsr->aConstraint ){
206193 int i; /* Used to iterate through constraint array */
206194 for(i=0; i<pCsr->nConstraint; i++){
206195 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
206196 if( pInfo ){
206197 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
206198 sqlite3_free(p: pInfo);
206199 }
206200 }
206201 sqlite3_free(p: pCsr->aConstraint);
206202 pCsr->aConstraint = 0;
206203 }
206204 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pNode: pCsr->aNode[ii]);
206205 sqlite3_free(p: pCsr->aPoint);
206206 pStmt = pCsr->pReadAux;
206207 memset(s: pCsr, c: 0, n: sizeof(RtreeCursor));
206208 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
206209 pCsr->pReadAux = pStmt;
206210
206211}
206212
206213/*
206214** Rtree virtual table module xClose method.
206215*/
206216static int rtreeClose(sqlite3_vtab_cursor *cur){
206217 Rtree *pRtree = (Rtree *)(cur->pVtab);
206218 RtreeCursor *pCsr = (RtreeCursor *)cur;
206219 assert( pRtree->nCursor>0 );
206220 resetCursor(pCsr);
206221 sqlite3_finalize(pStmt: pCsr->pReadAux);
206222 sqlite3_free(p: pCsr);
206223 pRtree->nCursor--;
206224 nodeBlobReset(pRtree);
206225 return SQLITE_OK;
206226}
206227
206228/*
206229** Rtree virtual table module xEof method.
206230**
206231** Return non-zero if the cursor does not currently point to a valid
206232** record (i.e if the scan has finished), or zero otherwise.
206233*/
206234static int rtreeEof(sqlite3_vtab_cursor *cur){
206235 RtreeCursor *pCsr = (RtreeCursor *)cur;
206236 return pCsr->atEOF;
206237}
206238
206239/*
206240** Convert raw bits from the on-disk RTree record into a coordinate value.
206241** The on-disk format is big-endian and needs to be converted for little-
206242** endian platforms. The on-disk record stores integer coordinates if
206243** eInt is true and it stores 32-bit floating point records if eInt is
206244** false. a[] is the four bytes of the on-disk record to be decoded.
206245** Store the results in "r".
206246**
206247** There are five versions of this macro. The last one is generic. The
206248** other four are various architectures-specific optimizations.
206249*/
206250#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
206251#define RTREE_DECODE_COORD(eInt, a, r) { \
206252 RtreeCoord c; /* Coordinate decoded */ \
206253 c.u = _byteswap_ulong(*(u32*)a); \
206254 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
206255}
206256#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
206257#define RTREE_DECODE_COORD(eInt, a, r) { \
206258 RtreeCoord c; /* Coordinate decoded */ \
206259 c.u = __builtin_bswap32(*(u32*)a); \
206260 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
206261}
206262#elif SQLITE_BYTEORDER==1234
206263#define RTREE_DECODE_COORD(eInt, a, r) { \
206264 RtreeCoord c; /* Coordinate decoded */ \
206265 memcpy(&c.u,a,4); \
206266 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
206267 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
206268 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
206269}
206270#elif SQLITE_BYTEORDER==4321
206271#define RTREE_DECODE_COORD(eInt, a, r) { \
206272 RtreeCoord c; /* Coordinate decoded */ \
206273 memcpy(&c.u,a,4); \
206274 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
206275}
206276#else
206277#define RTREE_DECODE_COORD(eInt, a, r) { \
206278 RtreeCoord c; /* Coordinate decoded */ \
206279 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
206280 +((u32)a[2]<<8) + a[3]; \
206281 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
206282}
206283#endif
206284
206285/*
206286** Check the RTree node or entry given by pCellData and p against the MATCH
206287** constraint pConstraint.
206288*/
206289static int rtreeCallbackConstraint(
206290 RtreeConstraint *pConstraint, /* The constraint to test */
206291 int eInt, /* True if RTree holding integer coordinates */
206292 u8 *pCellData, /* Raw cell content */
206293 RtreeSearchPoint *pSearch, /* Container of this cell */
206294 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
206295 int *peWithin /* OUT: visibility of the cell */
206296){
206297 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
206298 int nCoord = pInfo->nCoord; /* No. of coordinates */
206299 int rc; /* Callback return code */
206300 RtreeCoord c; /* Translator union */
206301 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
206302
206303 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
206304 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
206305
206306 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
206307 pInfo->iRowid = readInt64(p: pCellData);
206308 }
206309 pCellData += 8;
206310#ifndef SQLITE_RTREE_INT_ONLY
206311 if( eInt==0 ){
206312 switch( nCoord ){
206313 case 10: readCoord(p: pCellData+36, pCoord: &c); aCoord[9] = c.f;
206314 readCoord(p: pCellData+32, pCoord: &c); aCoord[8] = c.f;
206315 case 8: readCoord(p: pCellData+28, pCoord: &c); aCoord[7] = c.f;
206316 readCoord(p: pCellData+24, pCoord: &c); aCoord[6] = c.f;
206317 case 6: readCoord(p: pCellData+20, pCoord: &c); aCoord[5] = c.f;
206318 readCoord(p: pCellData+16, pCoord: &c); aCoord[4] = c.f;
206319 case 4: readCoord(p: pCellData+12, pCoord: &c); aCoord[3] = c.f;
206320 readCoord(p: pCellData+8, pCoord: &c); aCoord[2] = c.f;
206321 default: readCoord(p: pCellData+4, pCoord: &c); aCoord[1] = c.f;
206322 readCoord(p: pCellData, pCoord: &c); aCoord[0] = c.f;
206323 }
206324 }else
206325#endif
206326 {
206327 switch( nCoord ){
206328 case 10: readCoord(p: pCellData+36, pCoord: &c); aCoord[9] = c.i;
206329 readCoord(p: pCellData+32, pCoord: &c); aCoord[8] = c.i;
206330 case 8: readCoord(p: pCellData+28, pCoord: &c); aCoord[7] = c.i;
206331 readCoord(p: pCellData+24, pCoord: &c); aCoord[6] = c.i;
206332 case 6: readCoord(p: pCellData+20, pCoord: &c); aCoord[5] = c.i;
206333 readCoord(p: pCellData+16, pCoord: &c); aCoord[4] = c.i;
206334 case 4: readCoord(p: pCellData+12, pCoord: &c); aCoord[3] = c.i;
206335 readCoord(p: pCellData+8, pCoord: &c); aCoord[2] = c.i;
206336 default: readCoord(p: pCellData+4, pCoord: &c); aCoord[1] = c.i;
206337 readCoord(p: pCellData, pCoord: &c); aCoord[0] = c.i;
206338 }
206339 }
206340 if( pConstraint->op==RTREE_MATCH ){
206341 int eWithin = 0;
206342 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
206343 nCoord, aCoord, &eWithin);
206344 if( eWithin==0 ) *peWithin = NOT_WITHIN;
206345 *prScore = RTREE_ZERO;
206346 }else{
206347 pInfo->aCoord = aCoord;
206348 pInfo->iLevel = pSearch->iLevel - 1;
206349 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
206350 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
206351 rc = pConstraint->u.xQueryFunc(pInfo);
206352 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
206353 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
206354 *prScore = pInfo->rScore;
206355 }
206356 }
206357 return rc;
206358}
206359
206360/*
206361** Check the internal RTree node given by pCellData against constraint p.
206362** If this constraint cannot be satisfied by any child within the node,
206363** set *peWithin to NOT_WITHIN.
206364*/
206365static void rtreeNonleafConstraint(
206366 RtreeConstraint *p, /* The constraint to test */
206367 int eInt, /* True if RTree holds integer coordinates */
206368 u8 *pCellData, /* Raw cell content as appears on disk */
206369 int *peWithin /* Adjust downward, as appropriate */
206370){
206371 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
206372
206373 /* p->iCoord might point to either a lower or upper bound coordinate
206374 ** in a coordinate pair. But make pCellData point to the lower bound.
206375 */
206376 pCellData += 8 + 4*(p->iCoord&0xfe);
206377
206378 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
206379 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
206380 || p->op==RTREE_FALSE );
206381 assert( FOUR_BYTE_ALIGNED(pCellData) );
206382 switch( p->op ){
206383 case RTREE_TRUE: return; /* Always satisfied */
206384 case RTREE_FALSE: break; /* Never satisfied */
206385 case RTREE_EQ:
206386 RTREE_DECODE_COORD(eInt, pCellData, val);
206387 /* val now holds the lower bound of the coordinate pair */
206388 if( p->u.rValue>=val ){
206389 pCellData += 4;
206390 RTREE_DECODE_COORD(eInt, pCellData, val);
206391 /* val now holds the upper bound of the coordinate pair */
206392 if( p->u.rValue<=val ) return;
206393 }
206394 break;
206395 case RTREE_LE:
206396 case RTREE_LT:
206397 RTREE_DECODE_COORD(eInt, pCellData, val);
206398 /* val now holds the lower bound of the coordinate pair */
206399 if( p->u.rValue>=val ) return;
206400 break;
206401
206402 default:
206403 pCellData += 4;
206404 RTREE_DECODE_COORD(eInt, pCellData, val);
206405 /* val now holds the upper bound of the coordinate pair */
206406 if( p->u.rValue<=val ) return;
206407 break;
206408 }
206409 *peWithin = NOT_WITHIN;
206410}
206411
206412/*
206413** Check the leaf RTree cell given by pCellData against constraint p.
206414** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
206415** If the constraint is satisfied, leave *peWithin unchanged.
206416**
206417** The constraint is of the form: xN op $val
206418**
206419** The op is given by p->op. The xN is p->iCoord-th coordinate in
206420** pCellData. $val is given by p->u.rValue.
206421*/
206422static void rtreeLeafConstraint(
206423 RtreeConstraint *p, /* The constraint to test */
206424 int eInt, /* True if RTree holds integer coordinates */
206425 u8 *pCellData, /* Raw cell content as appears on disk */
206426 int *peWithin /* Adjust downward, as appropriate */
206427){
206428 RtreeDValue xN; /* Coordinate value converted to a double */
206429
206430 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
206431 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
206432 || p->op==RTREE_FALSE );
206433 pCellData += 8 + p->iCoord*4;
206434 assert( FOUR_BYTE_ALIGNED(pCellData) );
206435 RTREE_DECODE_COORD(eInt, pCellData, xN);
206436 switch( p->op ){
206437 case RTREE_TRUE: return; /* Always satisfied */
206438 case RTREE_FALSE: break; /* Never satisfied */
206439 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
206440 case RTREE_LT: if( xN < p->u.rValue ) return; break;
206441 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
206442 case RTREE_GT: if( xN > p->u.rValue ) return; break;
206443 default: if( xN == p->u.rValue ) return; break;
206444 }
206445 *peWithin = NOT_WITHIN;
206446}
206447
206448/*
206449** One of the cells in node pNode is guaranteed to have a 64-bit
206450** integer value equal to iRowid. Return the index of this cell.
206451*/
206452static int nodeRowidIndex(
206453 Rtree *pRtree,
206454 RtreeNode *pNode,
206455 i64 iRowid,
206456 int *piIndex
206457){
206458 int ii;
206459 int nCell = NCELL(pNode);
206460 assert( nCell<200 );
206461 for(ii=0; ii<nCell; ii++){
206462 if( nodeGetRowid(pRtree, pNode, iCell: ii)==iRowid ){
206463 *piIndex = ii;
206464 return SQLITE_OK;
206465 }
206466 }
206467 RTREE_IS_CORRUPT(pRtree);
206468 return SQLITE_CORRUPT_VTAB;
206469}
206470
206471/*
206472** Return the index of the cell containing a pointer to node pNode
206473** in its parent. If pNode is the root node, return -1.
206474*/
206475static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
206476 RtreeNode *pParent = pNode->pParent;
206477 if( ALWAYS(pParent) ){
206478 return nodeRowidIndex(pRtree, pNode: pParent, iRowid: pNode->iNode, piIndex);
206479 }else{
206480 *piIndex = -1;
206481 return SQLITE_OK;
206482 }
206483}
206484
206485/*
206486** Compare two search points. Return negative, zero, or positive if the first
206487** is less than, equal to, or greater than the second.
206488**
206489** The rScore is the primary key. Smaller rScore values come first.
206490** If the rScore is a tie, then use iLevel as the tie breaker with smaller
206491** iLevel values coming first. In this way, if rScore is the same for all
206492** SearchPoints, then iLevel becomes the deciding factor and the result
206493** is a depth-first search, which is the desired default behavior.
206494*/
206495static int rtreeSearchPointCompare(
206496 const RtreeSearchPoint *pA,
206497 const RtreeSearchPoint *pB
206498){
206499 if( pA->rScore<pB->rScore ) return -1;
206500 if( pA->rScore>pB->rScore ) return +1;
206501 if( pA->iLevel<pB->iLevel ) return -1;
206502 if( pA->iLevel>pB->iLevel ) return +1;
206503 return 0;
206504}
206505
206506/*
206507** Interchange two search points in a cursor.
206508*/
206509static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
206510 RtreeSearchPoint t = p->aPoint[i];
206511 assert( i<j );
206512 p->aPoint[i] = p->aPoint[j];
206513 p->aPoint[j] = t;
206514 i++; j++;
206515 if( i<RTREE_CACHE_SZ ){
206516 if( j>=RTREE_CACHE_SZ ){
206517 nodeRelease(RTREE_OF_CURSOR(p), pNode: p->aNode[i]);
206518 p->aNode[i] = 0;
206519 }else{
206520 RtreeNode *pTemp = p->aNode[i];
206521 p->aNode[i] = p->aNode[j];
206522 p->aNode[j] = pTemp;
206523 }
206524 }
206525}
206526
206527/*
206528** Return the search point with the lowest current score.
206529*/
206530static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
206531 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
206532}
206533
206534/*
206535** Get the RtreeNode for the search point with the lowest score.
206536*/
206537static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
206538 sqlite3_int64 id;
206539 int ii = 1 - pCur->bPoint;
206540 assert( ii==0 || ii==1 );
206541 assert( pCur->bPoint || pCur->nPoint );
206542 if( pCur->aNode[ii]==0 ){
206543 assert( pRC!=0 );
206544 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
206545 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), iNode: id, pParent: 0, ppNode: &pCur->aNode[ii]);
206546 }
206547 return pCur->aNode[ii];
206548}
206549
206550/*
206551** Push a new element onto the priority queue
206552*/
206553static RtreeSearchPoint *rtreeEnqueue(
206554 RtreeCursor *pCur, /* The cursor */
206555 RtreeDValue rScore, /* Score for the new search point */
206556 u8 iLevel /* Level for the new search point */
206557){
206558 int i, j;
206559 RtreeSearchPoint *pNew;
206560 if( pCur->nPoint>=pCur->nPointAlloc ){
206561 int nNew = pCur->nPointAlloc*2 + 8;
206562 pNew = sqlite3_realloc64(pOld: pCur->aPoint, n: nNew*sizeof(pCur->aPoint[0]));
206563 if( pNew==0 ) return 0;
206564 pCur->aPoint = pNew;
206565 pCur->nPointAlloc = nNew;
206566 }
206567 i = pCur->nPoint++;
206568 pNew = pCur->aPoint + i;
206569 pNew->rScore = rScore;
206570 pNew->iLevel = iLevel;
206571 assert( iLevel<=RTREE_MAX_DEPTH );
206572 while( i>0 ){
206573 RtreeSearchPoint *pParent;
206574 j = (i-1)/2;
206575 pParent = pCur->aPoint + j;
206576 if( rtreeSearchPointCompare(pA: pNew, pB: pParent)>=0 ) break;
206577 rtreeSearchPointSwap(p: pCur, i: j, j: i);
206578 i = j;
206579 pNew = pParent;
206580 }
206581 return pNew;
206582}
206583
206584/*
206585** Allocate a new RtreeSearchPoint and return a pointer to it. Return
206586** NULL if malloc fails.
206587*/
206588static RtreeSearchPoint *rtreeSearchPointNew(
206589 RtreeCursor *pCur, /* The cursor */
206590 RtreeDValue rScore, /* Score for the new search point */
206591 u8 iLevel /* Level for the new search point */
206592){
206593 RtreeSearchPoint *pNew, *pFirst;
206594 pFirst = rtreeSearchPointFirst(pCur);
206595 pCur->anQueue[iLevel]++;
206596 if( pFirst==0
206597 || pFirst->rScore>rScore
206598 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
206599 ){
206600 if( pCur->bPoint ){
206601 int ii;
206602 pNew = rtreeEnqueue(pCur, rScore, iLevel);
206603 if( pNew==0 ) return 0;
206604 ii = (int)(pNew - pCur->aPoint) + 1;
206605 assert( ii==1 );
206606 if( ALWAYS(ii<RTREE_CACHE_SZ) ){
206607 assert( pCur->aNode[ii]==0 );
206608 pCur->aNode[ii] = pCur->aNode[0];
206609 }else{
206610 nodeRelease(RTREE_OF_CURSOR(pCur), pNode: pCur->aNode[0]);
206611 }
206612 pCur->aNode[0] = 0;
206613 *pNew = pCur->sPoint;
206614 }
206615 pCur->sPoint.rScore = rScore;
206616 pCur->sPoint.iLevel = iLevel;
206617 pCur->bPoint = 1;
206618 return &pCur->sPoint;
206619 }else{
206620 return rtreeEnqueue(pCur, rScore, iLevel);
206621 }
206622}
206623
206624#if 0
206625/* Tracing routines for the RtreeSearchPoint queue */
206626static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
206627 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
206628 printf(" %d.%05lld.%02d %g %d",
206629 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
206630 );
206631 idx++;
206632 if( idx<RTREE_CACHE_SZ ){
206633 printf(" %p\n", pCur->aNode[idx]);
206634 }else{
206635 printf("\n");
206636 }
206637}
206638static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
206639 int ii;
206640 printf("=== %9s ", zPrefix);
206641 if( pCur->bPoint ){
206642 tracePoint(&pCur->sPoint, -1, pCur);
206643 }
206644 for(ii=0; ii<pCur->nPoint; ii++){
206645 if( ii>0 || pCur->bPoint ) printf(" ");
206646 tracePoint(&pCur->aPoint[ii], ii, pCur);
206647 }
206648}
206649# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
206650#else
206651# define RTREE_QUEUE_TRACE(A,B) /* no-op */
206652#endif
206653
206654/* Remove the search point with the lowest current score.
206655*/
206656static void rtreeSearchPointPop(RtreeCursor *p){
206657 int i, j, k, n;
206658 i = 1 - p->bPoint;
206659 assert( i==0 || i==1 );
206660 if( p->aNode[i] ){
206661 nodeRelease(RTREE_OF_CURSOR(p), pNode: p->aNode[i]);
206662 p->aNode[i] = 0;
206663 }
206664 if( p->bPoint ){
206665 p->anQueue[p->sPoint.iLevel]--;
206666 p->bPoint = 0;
206667 }else if( ALWAYS(p->nPoint) ){
206668 p->anQueue[p->aPoint[0].iLevel]--;
206669 n = --p->nPoint;
206670 p->aPoint[0] = p->aPoint[n];
206671 if( n<RTREE_CACHE_SZ-1 ){
206672 p->aNode[1] = p->aNode[n+1];
206673 p->aNode[n+1] = 0;
206674 }
206675 i = 0;
206676 while( (j = i*2+1)<n ){
206677 k = j+1;
206678 if( k<n && rtreeSearchPointCompare(pA: &p->aPoint[k], pB: &p->aPoint[j])<0 ){
206679 if( rtreeSearchPointCompare(pA: &p->aPoint[k], pB: &p->aPoint[i])<0 ){
206680 rtreeSearchPointSwap(p, i, j: k);
206681 i = k;
206682 }else{
206683 break;
206684 }
206685 }else{
206686 if( rtreeSearchPointCompare(pA: &p->aPoint[j], pB: &p->aPoint[i])<0 ){
206687 rtreeSearchPointSwap(p, i, j);
206688 i = j;
206689 }else{
206690 break;
206691 }
206692 }
206693 }
206694 }
206695}
206696
206697
206698/*
206699** Continue the search on cursor pCur until the front of the queue
206700** contains an entry suitable for returning as a result-set row,
206701** or until the RtreeSearchPoint queue is empty, indicating that the
206702** query has completed.
206703*/
206704static int rtreeStepToLeaf(RtreeCursor *pCur){
206705 RtreeSearchPoint *p;
206706 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
206707 RtreeNode *pNode;
206708 int eWithin;
206709 int rc = SQLITE_OK;
206710 int nCell;
206711 int nConstraint = pCur->nConstraint;
206712 int ii;
206713 int eInt;
206714 RtreeSearchPoint x;
206715
206716 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
206717 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
206718 u8 *pCellData;
206719 pNode = rtreeNodeOfFirstSearchPoint(pCur, pRC: &rc);
206720 if( rc ) return rc;
206721 nCell = NCELL(pNode);
206722 assert( nCell<200 );
206723 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
206724 while( p->iCell<nCell ){
206725 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
206726 eWithin = FULLY_WITHIN;
206727 for(ii=0; ii<nConstraint; ii++){
206728 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
206729 if( pConstraint->op>=RTREE_MATCH ){
206730 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, pSearch: p,
206731 prScore: &rScore, peWithin: &eWithin);
206732 if( rc ) return rc;
206733 }else if( p->iLevel==1 ){
206734 rtreeLeafConstraint(p: pConstraint, eInt, pCellData, peWithin: &eWithin);
206735 }else{
206736 rtreeNonleafConstraint(p: pConstraint, eInt, pCellData, peWithin: &eWithin);
206737 }
206738 if( eWithin==NOT_WITHIN ){
206739 p->iCell++;
206740 pCellData += pRtree->nBytesPerCell;
206741 break;
206742 }
206743 }
206744 if( eWithin==NOT_WITHIN ) continue;
206745 p->iCell++;
206746 x.iLevel = p->iLevel - 1;
206747 if( x.iLevel ){
206748 x.id = readInt64(p: pCellData);
206749 for(ii=0; ii<pCur->nPoint; ii++){
206750 if( pCur->aPoint[ii].id==x.id ){
206751 RTREE_IS_CORRUPT(pRtree);
206752 return SQLITE_CORRUPT_VTAB;
206753 }
206754 }
206755 x.iCell = 0;
206756 }else{
206757 x.id = p->id;
206758 x.iCell = p->iCell - 1;
206759 }
206760 if( p->iCell>=nCell ){
206761 RTREE_QUEUE_TRACE(pCur, "POP-S:");
206762 rtreeSearchPointPop(p: pCur);
206763 }
206764 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
206765 p = rtreeSearchPointNew(pCur, rScore, iLevel: x.iLevel);
206766 if( p==0 ) return SQLITE_NOMEM;
206767 p->eWithin = (u8)eWithin;
206768 p->id = x.id;
206769 p->iCell = x.iCell;
206770 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
206771 break;
206772 }
206773 if( p->iCell>=nCell ){
206774 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
206775 rtreeSearchPointPop(p: pCur);
206776 }
206777 }
206778 pCur->atEOF = p==0;
206779 return SQLITE_OK;
206780}
206781
206782/*
206783** Rtree virtual table module xNext method.
206784*/
206785static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
206786 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
206787 int rc = SQLITE_OK;
206788
206789 /* Move to the next entry that matches the configured constraints. */
206790 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
206791 if( pCsr->bAuxValid ){
206792 pCsr->bAuxValid = 0;
206793 sqlite3_reset(pStmt: pCsr->pReadAux);
206794 }
206795 rtreeSearchPointPop(p: pCsr);
206796 rc = rtreeStepToLeaf(pCur: pCsr);
206797 return rc;
206798}
206799
206800/*
206801** Rtree virtual table module xRowid method.
206802*/
206803static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
206804 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
206805 RtreeSearchPoint *p = rtreeSearchPointFirst(pCur: pCsr);
206806 int rc = SQLITE_OK;
206807 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCur: pCsr, pRC: &rc);
206808 if( rc==SQLITE_OK && ALWAYS(p) ){
206809 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, iCell: p->iCell);
206810 }
206811 return rc;
206812}
206813
206814/*
206815** Rtree virtual table module xColumn method.
206816*/
206817static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
206818 Rtree *pRtree = (Rtree *)cur->pVtab;
206819 RtreeCursor *pCsr = (RtreeCursor *)cur;
206820 RtreeSearchPoint *p = rtreeSearchPointFirst(pCur: pCsr);
206821 RtreeCoord c;
206822 int rc = SQLITE_OK;
206823 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCur: pCsr, pRC: &rc);
206824
206825 if( rc ) return rc;
206826 if( NEVER(p==0) ) return SQLITE_OK;
206827 if( i==0 ){
206828 sqlite3_result_int64(pCtx: ctx, iVal: nodeGetRowid(pRtree, pNode, iCell: p->iCell));
206829 }else if( i<=pRtree->nDim2 ){
206830 nodeGetCoord(pRtree, pNode, iCell: p->iCell, iCoord: i-1, pCoord: &c);
206831#ifndef SQLITE_RTREE_INT_ONLY
206832 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
206833 sqlite3_result_double(pCtx: ctx, rVal: c.f);
206834 }else
206835#endif
206836 {
206837 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
206838 sqlite3_result_int(pCtx: ctx, iVal: c.i);
206839 }
206840 }else{
206841 if( !pCsr->bAuxValid ){
206842 if( pCsr->pReadAux==0 ){
206843 rc = sqlite3_prepare_v3(db: pRtree->db, zSql: pRtree->zReadAuxSql, nBytes: -1, prepFlags: 0,
206844 ppStmt: &pCsr->pReadAux, pzTail: 0);
206845 if( rc ) return rc;
206846 }
206847 sqlite3_bind_int64(pStmt: pCsr->pReadAux, i: 1,
206848 iValue: nodeGetRowid(pRtree, pNode, iCell: p->iCell));
206849 rc = sqlite3_step(pStmt: pCsr->pReadAux);
206850 if( rc==SQLITE_ROW ){
206851 pCsr->bAuxValid = 1;
206852 }else{
206853 sqlite3_reset(pStmt: pCsr->pReadAux);
206854 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
206855 return rc;
206856 }
206857 }
206858 sqlite3_result_value(pCtx: ctx,
206859 pValue: sqlite3_column_value(pStmt: pCsr->pReadAux, i: i - pRtree->nDim2 + 1));
206860 }
206861 return SQLITE_OK;
206862}
206863
206864/*
206865** Use nodeAcquire() to obtain the leaf node containing the record with
206866** rowid iRowid. If successful, set *ppLeaf to point to the node and
206867** return SQLITE_OK. If there is no such record in the table, set
206868** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
206869** to zero and return an SQLite error code.
206870*/
206871static int findLeafNode(
206872 Rtree *pRtree, /* RTree to search */
206873 i64 iRowid, /* The rowid searching for */
206874 RtreeNode **ppLeaf, /* Write the node here */
206875 sqlite3_int64 *piNode /* Write the node-id here */
206876){
206877 int rc;
206878 *ppLeaf = 0;
206879 sqlite3_bind_int64(pStmt: pRtree->pReadRowid, i: 1, iValue: iRowid);
206880 if( sqlite3_step(pStmt: pRtree->pReadRowid)==SQLITE_ROW ){
206881 i64 iNode = sqlite3_column_int64(pStmt: pRtree->pReadRowid, i: 0);
206882 if( piNode ) *piNode = iNode;
206883 rc = nodeAcquire(pRtree, iNode, pParent: 0, ppNode: ppLeaf);
206884 sqlite3_reset(pStmt: pRtree->pReadRowid);
206885 }else{
206886 rc = sqlite3_reset(pStmt: pRtree->pReadRowid);
206887 }
206888 return rc;
206889}
206890
206891/*
206892** This function is called to configure the RtreeConstraint object passed
206893** as the second argument for a MATCH constraint. The value passed as the
206894** first argument to this function is the right-hand operand to the MATCH
206895** operator.
206896*/
206897static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
206898 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
206899 sqlite3_rtree_query_info *pInfo; /* Callback information */
206900
206901 pSrc = sqlite3_value_pointer(pVal: pValue, zPType: "RtreeMatchArg");
206902 if( pSrc==0 ) return SQLITE_ERROR;
206903 pInfo = (sqlite3_rtree_query_info*)
206904 sqlite3_malloc64( n: sizeof(*pInfo)+pSrc->iSize );
206905 if( !pInfo ) return SQLITE_NOMEM;
206906 memset(s: pInfo, c: 0, n: sizeof(*pInfo));
206907 pBlob = (RtreeMatchArg*)&pInfo[1];
206908 memcpy(dest: pBlob, src: pSrc, n: pSrc->iSize);
206909 pInfo->pContext = pBlob->cb.pContext;
206910 pInfo->nParam = pBlob->nParam;
206911 pInfo->aParam = pBlob->aParam;
206912 pInfo->apSqlParam = pBlob->apSqlParam;
206913
206914 if( pBlob->cb.xGeom ){
206915 pCons->u.xGeom = pBlob->cb.xGeom;
206916 }else{
206917 pCons->op = RTREE_QUERY;
206918 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
206919 }
206920 pCons->pInfo = pInfo;
206921 return SQLITE_OK;
206922}
206923
206924/*
206925** Rtree virtual table module xFilter method.
206926*/
206927static int rtreeFilter(
206928 sqlite3_vtab_cursor *pVtabCursor,
206929 int idxNum, const char *idxStr,
206930 int argc, sqlite3_value **argv
206931){
206932 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
206933 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
206934 RtreeNode *pRoot = 0;
206935 int ii;
206936 int rc = SQLITE_OK;
206937 int iCell = 0;
206938
206939 rtreeReference(pRtree);
206940
206941 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
206942 resetCursor(pCsr);
206943
206944 pCsr->iStrategy = idxNum;
206945 if( idxNum==1 ){
206946 /* Special case - lookup by rowid. */
206947 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
206948 RtreeSearchPoint *p; /* Search point for the leaf */
206949 i64 iRowid = sqlite3_value_int64(pVal: argv[0]);
206950 i64 iNode = 0;
206951 int eType = sqlite3_value_numeric_type(pVal: argv[0]);
206952 if( eType==SQLITE_INTEGER
206953 || (eType==SQLITE_FLOAT && sqlite3_value_double(pVal: argv[0])==iRowid)
206954 ){
206955 rc = findLeafNode(pRtree, iRowid, ppLeaf: &pLeaf, piNode: &iNode);
206956 }else{
206957 rc = SQLITE_OK;
206958 pLeaf = 0;
206959 }
206960 if( rc==SQLITE_OK && pLeaf!=0 ){
206961 p = rtreeSearchPointNew(pCur: pCsr, RTREE_ZERO, iLevel: 0);
206962 assert( p!=0 ); /* Always returns pCsr->sPoint */
206963 pCsr->aNode[0] = pLeaf;
206964 p->id = iNode;
206965 p->eWithin = PARTLY_WITHIN;
206966 rc = nodeRowidIndex(pRtree, pNode: pLeaf, iRowid, piIndex: &iCell);
206967 p->iCell = (u8)iCell;
206968 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
206969 }else{
206970 pCsr->atEOF = 1;
206971 }
206972 }else{
206973 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
206974 ** with the configured constraints.
206975 */
206976 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pRoot);
206977 if( rc==SQLITE_OK && argc>0 ){
206978 pCsr->aConstraint = sqlite3_malloc64(n: sizeof(RtreeConstraint)*argc);
206979 pCsr->nConstraint = argc;
206980 if( !pCsr->aConstraint ){
206981 rc = SQLITE_NOMEM;
206982 }else{
206983 memset(s: pCsr->aConstraint, c: 0, n: sizeof(RtreeConstraint)*argc);
206984 memset(s: pCsr->anQueue, c: 0, n: sizeof(u32)*(pRtree->iDepth + 1));
206985 assert( (idxStr==0 && argc==0)
206986 || (idxStr && (int)strlen(idxStr)==argc*2) );
206987 for(ii=0; ii<argc; ii++){
206988 RtreeConstraint *p = &pCsr->aConstraint[ii];
206989 int eType = sqlite3_value_numeric_type(pVal: argv[ii]);
206990 p->op = idxStr[ii*2];
206991 p->iCoord = idxStr[ii*2+1]-'0';
206992 if( p->op>=RTREE_MATCH ){
206993 /* A MATCH operator. The right-hand-side must be a blob that
206994 ** can be cast into an RtreeMatchArg object. One created using
206995 ** an sqlite3_rtree_geometry_callback() SQL user function.
206996 */
206997 rc = deserializeGeometry(pValue: argv[ii], pCons: p);
206998 if( rc!=SQLITE_OK ){
206999 break;
207000 }
207001 p->pInfo->nCoord = pRtree->nDim2;
207002 p->pInfo->anQueue = pCsr->anQueue;
207003 p->pInfo->mxLevel = pRtree->iDepth + 1;
207004 }else if( eType==SQLITE_INTEGER ){
207005 sqlite3_int64 iVal = sqlite3_value_int64(pVal: argv[ii]);
207006#ifdef SQLITE_RTREE_INT_ONLY
207007 p->u.rValue = iVal;
207008#else
207009 p->u.rValue = (double)iVal;
207010 if( iVal>=((sqlite3_int64)1)<<48
207011 || iVal<=-(((sqlite3_int64)1)<<48)
207012 ){
207013 if( p->op==RTREE_LT ) p->op = RTREE_LE;
207014 if( p->op==RTREE_GT ) p->op = RTREE_GE;
207015 }
207016#endif
207017 }else if( eType==SQLITE_FLOAT ){
207018#ifdef SQLITE_RTREE_INT_ONLY
207019 p->u.rValue = sqlite3_value_int64(argv[ii]);
207020#else
207021 p->u.rValue = sqlite3_value_double(pVal: argv[ii]);
207022#endif
207023 }else{
207024 p->u.rValue = RTREE_ZERO;
207025 if( eType==SQLITE_NULL ){
207026 p->op = RTREE_FALSE;
207027 }else if( p->op==RTREE_LT || p->op==RTREE_LE ){
207028 p->op = RTREE_TRUE;
207029 }else{
207030 p->op = RTREE_FALSE;
207031 }
207032 }
207033 }
207034 }
207035 }
207036 if( rc==SQLITE_OK ){
207037 RtreeSearchPoint *pNew;
207038 assert( pCsr->bPoint==0 ); /* Due to the resetCursor() call above */
207039 pNew = rtreeSearchPointNew(pCur: pCsr, RTREE_ZERO, iLevel: (u8)(pRtree->iDepth+1));
207040 if( NEVER(pNew==0) ){ /* Because pCsr->bPoint was FALSE */
207041 return SQLITE_NOMEM;
207042 }
207043 pNew->id = 1;
207044 pNew->iCell = 0;
207045 pNew->eWithin = PARTLY_WITHIN;
207046 assert( pCsr->bPoint==1 );
207047 pCsr->aNode[0] = pRoot;
207048 pRoot = 0;
207049 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
207050 rc = rtreeStepToLeaf(pCur: pCsr);
207051 }
207052 }
207053
207054 nodeRelease(pRtree, pNode: pRoot);
207055 rtreeRelease(pRtree);
207056 return rc;
207057}
207058
207059/*
207060** Rtree virtual table module xBestIndex method. There are three
207061** table scan strategies to choose from (in order from most to
207062** least desirable):
207063**
207064** idxNum idxStr Strategy
207065** ------------------------------------------------
207066** 1 Unused Direct lookup by rowid.
207067** 2 See below R-tree query or full-table scan.
207068** ------------------------------------------------
207069**
207070** If strategy 1 is used, then idxStr is not meaningful. If strategy
207071** 2 is used, idxStr is formatted to contain 2 bytes for each
207072** constraint used. The first two bytes of idxStr correspond to
207073** the constraint in sqlite3_index_info.aConstraintUsage[] with
207074** (argvIndex==1) etc.
207075**
207076** The first of each pair of bytes in idxStr identifies the constraint
207077** operator as follows:
207078**
207079** Operator Byte Value
207080** ----------------------
207081** = 0x41 ('A')
207082** <= 0x42 ('B')
207083** < 0x43 ('C')
207084** >= 0x44 ('D')
207085** > 0x45 ('E')
207086** MATCH 0x46 ('F')
207087** ----------------------
207088**
207089** The second of each pair of bytes identifies the coordinate column
207090** to which the constraint applies. The leftmost coordinate column
207091** is 'a', the second from the left 'b' etc.
207092*/
207093static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
207094 Rtree *pRtree = (Rtree*)tab;
207095 int rc = SQLITE_OK;
207096 int ii;
207097 int bMatch = 0; /* True if there exists a MATCH constraint */
207098 i64 nRow; /* Estimated rows returned by this scan */
207099
207100 int iIdx = 0;
207101 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
207102 memset(s: zIdxStr, c: 0, n: sizeof(zIdxStr));
207103
207104 /* Check if there exists a MATCH constraint - even an unusable one. If there
207105 ** is, do not consider the lookup-by-rowid plan as using such a plan would
207106 ** require the VDBE to evaluate the MATCH constraint, which is not currently
207107 ** possible. */
207108 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
207109 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
207110 bMatch = 1;
207111 }
207112 }
207113
207114 assert( pIdxInfo->idxStr==0 );
207115 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
207116 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
207117
207118 if( bMatch==0 && p->usable
207119 && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
207120 ){
207121 /* We have an equality constraint on the rowid. Use strategy 1. */
207122 int jj;
207123 for(jj=0; jj<ii; jj++){
207124 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
207125 pIdxInfo->aConstraintUsage[jj].omit = 0;
207126 }
207127 pIdxInfo->idxNum = 1;
207128 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
207129 pIdxInfo->aConstraintUsage[jj].omit = 1;
207130
207131 /* This strategy involves a two rowid lookups on an B-Tree structures
207132 ** and then a linear search of an R-Tree node. This should be
207133 ** considered almost as quick as a direct rowid lookup (for which
207134 ** sqlite uses an internal cost of 0.0). It is expected to return
207135 ** a single row.
207136 */
207137 pIdxInfo->estimatedCost = 30.0;
207138 pIdxInfo->estimatedRows = 1;
207139 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
207140 return SQLITE_OK;
207141 }
207142
207143 if( p->usable
207144 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
207145 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
207146 ){
207147 u8 op;
207148 u8 doOmit = 1;
207149 switch( p->op ){
207150 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; doOmit = 0; break;
207151 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; doOmit = 0; break;
207152 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
207153 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; doOmit = 0; break;
207154 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
207155 case SQLITE_INDEX_CONSTRAINT_MATCH: op = RTREE_MATCH; break;
207156 default: op = 0; break;
207157 }
207158 if( op ){
207159 zIdxStr[iIdx++] = op;
207160 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
207161 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
207162 pIdxInfo->aConstraintUsage[ii].omit = doOmit;
207163 }
207164 }
207165 }
207166
207167 pIdxInfo->idxNum = 2;
207168 pIdxInfo->needToFreeIdxStr = 1;
207169 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf(zFormat: "%s", zIdxStr)) ){
207170 return SQLITE_NOMEM;
207171 }
207172
207173 nRow = pRtree->nRowEst >> (iIdx/2);
207174 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
207175 pIdxInfo->estimatedRows = nRow;
207176
207177 return rc;
207178}
207179
207180/*
207181** Return the N-dimensional volumn of the cell stored in *p.
207182*/
207183static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
207184 RtreeDValue area = (RtreeDValue)1;
207185 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
207186#ifndef SQLITE_RTREE_INT_ONLY
207187 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
207188 switch( pRtree->nDim ){
207189 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
207190 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
207191 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
207192 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
207193 default: area *= p->aCoord[1].f - p->aCoord[0].f;
207194 }
207195 }else
207196#endif
207197 {
207198 switch( pRtree->nDim ){
207199 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
207200 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
207201 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
207202 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
207203 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
207204 }
207205 }
207206 return area;
207207}
207208
207209/*
207210** Return the margin length of cell p. The margin length is the sum
207211** of the objects size in each dimension.
207212*/
207213static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
207214 RtreeDValue margin = 0;
207215 int ii = pRtree->nDim2 - 2;
207216 do{
207217 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
207218 ii -= 2;
207219 }while( ii>=0 );
207220 return margin;
207221}
207222
207223/*
207224** Store the union of cells p1 and p2 in p1.
207225*/
207226static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
207227 int ii = 0;
207228 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
207229 do{
207230 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
207231 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
207232 ii += 2;
207233 }while( ii<pRtree->nDim2 );
207234 }else{
207235 do{
207236 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
207237 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
207238 ii += 2;
207239 }while( ii<pRtree->nDim2 );
207240 }
207241}
207242
207243/*
207244** Return true if the area covered by p2 is a subset of the area covered
207245** by p1. False otherwise.
207246*/
207247static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
207248 int ii;
207249 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
207250 for(ii=0; ii<pRtree->nDim2; ii+=2){
207251 RtreeCoord *a1 = &p1->aCoord[ii];
207252 RtreeCoord *a2 = &p2->aCoord[ii];
207253 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
207254 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
207255 ){
207256 return 0;
207257 }
207258 }
207259 return 1;
207260}
207261
207262/*
207263** Return the amount cell p would grow by if it were unioned with pCell.
207264*/
207265static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
207266 RtreeDValue area;
207267 RtreeCell cell;
207268 memcpy(dest: &cell, src: p, n: sizeof(RtreeCell));
207269 area = cellArea(pRtree, p: &cell);
207270 cellUnion(pRtree, p1: &cell, p2: pCell);
207271 return (cellArea(pRtree, p: &cell)-area);
207272}
207273
207274static RtreeDValue cellOverlap(
207275 Rtree *pRtree,
207276 RtreeCell *p,
207277 RtreeCell *aCell,
207278 int nCell
207279){
207280 int ii;
207281 RtreeDValue overlap = RTREE_ZERO;
207282 for(ii=0; ii<nCell; ii++){
207283 int jj;
207284 RtreeDValue o = (RtreeDValue)1;
207285 for(jj=0; jj<pRtree->nDim2; jj+=2){
207286 RtreeDValue x1, x2;
207287 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
207288 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
207289 if( x2<x1 ){
207290 o = (RtreeDValue)0;
207291 break;
207292 }else{
207293 o = o * (x2-x1);
207294 }
207295 }
207296 overlap += o;
207297 }
207298 return overlap;
207299}
207300
207301
207302/*
207303** This function implements the ChooseLeaf algorithm from Gutman[84].
207304** ChooseSubTree in r*tree terminology.
207305*/
207306static int ChooseLeaf(
207307 Rtree *pRtree, /* Rtree table */
207308 RtreeCell *pCell, /* Cell to insert into rtree */
207309 int iHeight, /* Height of sub-tree rooted at pCell */
207310 RtreeNode **ppLeaf /* OUT: Selected leaf page */
207311){
207312 int rc;
207313 int ii;
207314 RtreeNode *pNode = 0;
207315 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pNode);
207316
207317 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
207318 int iCell;
207319 sqlite3_int64 iBest = 0;
207320
207321 RtreeDValue fMinGrowth = RTREE_ZERO;
207322 RtreeDValue fMinArea = RTREE_ZERO;
207323
207324 int nCell = NCELL(pNode);
207325 RtreeCell cell;
207326 RtreeNode *pChild = 0;
207327
207328 RtreeCell *aCell = 0;
207329
207330 /* Select the child node which will be enlarged the least if pCell
207331 ** is inserted into it. Resolve ties by choosing the entry with
207332 ** the smallest area.
207333 */
207334 for(iCell=0; iCell<nCell; iCell++){
207335 int bBest = 0;
207336 RtreeDValue growth;
207337 RtreeDValue area;
207338 nodeGetCell(pRtree, pNode, iCell, pCell: &cell);
207339 growth = cellGrowth(pRtree, p: &cell, pCell);
207340 area = cellArea(pRtree, p: &cell);
207341 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
207342 bBest = 1;
207343 }
207344 if( bBest ){
207345 fMinGrowth = growth;
207346 fMinArea = area;
207347 iBest = cell.iRowid;
207348 }
207349 }
207350
207351 sqlite3_free(p: aCell);
207352 rc = nodeAcquire(pRtree, iNode: iBest, pParent: pNode, ppNode: &pChild);
207353 nodeRelease(pRtree, pNode);
207354 pNode = pChild;
207355 }
207356
207357 *ppLeaf = pNode;
207358 return rc;
207359}
207360
207361/*
207362** A cell with the same content as pCell has just been inserted into
207363** the node pNode. This function updates the bounding box cells in
207364** all ancestor elements.
207365*/
207366static int AdjustTree(
207367 Rtree *pRtree, /* Rtree table */
207368 RtreeNode *pNode, /* Adjust ancestry of this node. */
207369 RtreeCell *pCell /* This cell was just inserted */
207370){
207371 RtreeNode *p = pNode;
207372 int cnt = 0;
207373 int rc;
207374 while( p->pParent ){
207375 RtreeNode *pParent = p->pParent;
207376 RtreeCell cell;
207377 int iCell;
207378
207379 cnt++;
207380 if( NEVER(cnt>100) ){
207381 RTREE_IS_CORRUPT(pRtree);
207382 return SQLITE_CORRUPT_VTAB;
207383 }
207384 rc = nodeParentIndex(pRtree, pNode: p, piIndex: &iCell);
207385 if( NEVER(rc!=SQLITE_OK) ){
207386 RTREE_IS_CORRUPT(pRtree);
207387 return SQLITE_CORRUPT_VTAB;
207388 }
207389
207390 nodeGetCell(pRtree, pNode: pParent, iCell, pCell: &cell);
207391 if( !cellContains(pRtree, p1: &cell, p2: pCell) ){
207392 cellUnion(pRtree, p1: &cell, p2: pCell);
207393 nodeOverwriteCell(pRtree, pNode: pParent, pCell: &cell, iCell);
207394 }
207395
207396 p = pParent;
207397 }
207398 return SQLITE_OK;
207399}
207400
207401/*
207402** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
207403*/
207404static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
207405 sqlite3_bind_int64(pStmt: pRtree->pWriteRowid, i: 1, iValue: iRowid);
207406 sqlite3_bind_int64(pStmt: pRtree->pWriteRowid, i: 2, iValue: iNode);
207407 sqlite3_step(pStmt: pRtree->pWriteRowid);
207408 return sqlite3_reset(pStmt: pRtree->pWriteRowid);
207409}
207410
207411/*
207412** Write mapping (iNode->iPar) to the <rtree>_parent table.
207413*/
207414static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
207415 sqlite3_bind_int64(pStmt: pRtree->pWriteParent, i: 1, iValue: iNode);
207416 sqlite3_bind_int64(pStmt: pRtree->pWriteParent, i: 2, iValue: iPar);
207417 sqlite3_step(pStmt: pRtree->pWriteParent);
207418 return sqlite3_reset(pStmt: pRtree->pWriteParent);
207419}
207420
207421static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
207422
207423
207424/*
207425** Arguments aIdx, aDistance and aSpare all point to arrays of size
207426** nIdx. The aIdx array contains the set of integers from 0 to
207427** (nIdx-1) in no particular order. This function sorts the values
207428** in aIdx according to the indexed values in aDistance. For
207429** example, assuming the inputs:
207430**
207431** aIdx = { 0, 1, 2, 3 }
207432** aDistance = { 5.0, 2.0, 7.0, 6.0 }
207433**
207434** this function sets the aIdx array to contain:
207435**
207436** aIdx = { 0, 1, 2, 3 }
207437**
207438** The aSpare array is used as temporary working space by the
207439** sorting algorithm.
207440*/
207441static void SortByDistance(
207442 int *aIdx,
207443 int nIdx,
207444 RtreeDValue *aDistance,
207445 int *aSpare
207446){
207447 if( nIdx>1 ){
207448 int iLeft = 0;
207449 int iRight = 0;
207450
207451 int nLeft = nIdx/2;
207452 int nRight = nIdx-nLeft;
207453 int *aLeft = aIdx;
207454 int *aRight = &aIdx[nLeft];
207455
207456 SortByDistance(aIdx: aLeft, nIdx: nLeft, aDistance, aSpare);
207457 SortByDistance(aIdx: aRight, nIdx: nRight, aDistance, aSpare);
207458
207459 memcpy(dest: aSpare, src: aLeft, n: sizeof(int)*nLeft);
207460 aLeft = aSpare;
207461
207462 while( iLeft<nLeft || iRight<nRight ){
207463 if( iLeft==nLeft ){
207464 aIdx[iLeft+iRight] = aRight[iRight];
207465 iRight++;
207466 }else if( iRight==nRight ){
207467 aIdx[iLeft+iRight] = aLeft[iLeft];
207468 iLeft++;
207469 }else{
207470 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
207471 RtreeDValue fRight = aDistance[aRight[iRight]];
207472 if( fLeft<fRight ){
207473 aIdx[iLeft+iRight] = aLeft[iLeft];
207474 iLeft++;
207475 }else{
207476 aIdx[iLeft+iRight] = aRight[iRight];
207477 iRight++;
207478 }
207479 }
207480 }
207481
207482#if 0
207483 /* Check that the sort worked */
207484 {
207485 int jj;
207486 for(jj=1; jj<nIdx; jj++){
207487 RtreeDValue left = aDistance[aIdx[jj-1]];
207488 RtreeDValue right = aDistance[aIdx[jj]];
207489 assert( left<=right );
207490 }
207491 }
207492#endif
207493 }
207494}
207495
207496/*
207497** Arguments aIdx, aCell and aSpare all point to arrays of size
207498** nIdx. The aIdx array contains the set of integers from 0 to
207499** (nIdx-1) in no particular order. This function sorts the values
207500** in aIdx according to dimension iDim of the cells in aCell. The
207501** minimum value of dimension iDim is considered first, the
207502** maximum used to break ties.
207503**
207504** The aSpare array is used as temporary working space by the
207505** sorting algorithm.
207506*/
207507static void SortByDimension(
207508 Rtree *pRtree,
207509 int *aIdx,
207510 int nIdx,
207511 int iDim,
207512 RtreeCell *aCell,
207513 int *aSpare
207514){
207515 if( nIdx>1 ){
207516
207517 int iLeft = 0;
207518 int iRight = 0;
207519
207520 int nLeft = nIdx/2;
207521 int nRight = nIdx-nLeft;
207522 int *aLeft = aIdx;
207523 int *aRight = &aIdx[nLeft];
207524
207525 SortByDimension(pRtree, aIdx: aLeft, nIdx: nLeft, iDim, aCell, aSpare);
207526 SortByDimension(pRtree, aIdx: aRight, nIdx: nRight, iDim, aCell, aSpare);
207527
207528 memcpy(dest: aSpare, src: aLeft, n: sizeof(int)*nLeft);
207529 aLeft = aSpare;
207530 while( iLeft<nLeft || iRight<nRight ){
207531 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
207532 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
207533 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
207534 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
207535 if( (iLeft!=nLeft) && ((iRight==nRight)
207536 || (xleft1<xright1)
207537 || (xleft1==xright1 && xleft2<xright2)
207538 )){
207539 aIdx[iLeft+iRight] = aLeft[iLeft];
207540 iLeft++;
207541 }else{
207542 aIdx[iLeft+iRight] = aRight[iRight];
207543 iRight++;
207544 }
207545 }
207546
207547#if 0
207548 /* Check that the sort worked */
207549 {
207550 int jj;
207551 for(jj=1; jj<nIdx; jj++){
207552 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
207553 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
207554 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
207555 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
207556 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
207557 }
207558 }
207559#endif
207560 }
207561}
207562
207563/*
207564** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
207565*/
207566static int splitNodeStartree(
207567 Rtree *pRtree,
207568 RtreeCell *aCell,
207569 int nCell,
207570 RtreeNode *pLeft,
207571 RtreeNode *pRight,
207572 RtreeCell *pBboxLeft,
207573 RtreeCell *pBboxRight
207574){
207575 int **aaSorted;
207576 int *aSpare;
207577 int ii;
207578
207579 int iBestDim = 0;
207580 int iBestSplit = 0;
207581 RtreeDValue fBestMargin = RTREE_ZERO;
207582
207583 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
207584
207585 aaSorted = (int **)sqlite3_malloc64(n: nByte);
207586 if( !aaSorted ){
207587 return SQLITE_NOMEM;
207588 }
207589
207590 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
207591 memset(s: aaSorted, c: 0, n: nByte);
207592 for(ii=0; ii<pRtree->nDim; ii++){
207593 int jj;
207594 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
207595 for(jj=0; jj<nCell; jj++){
207596 aaSorted[ii][jj] = jj;
207597 }
207598 SortByDimension(pRtree, aIdx: aaSorted[ii], nIdx: nCell, iDim: ii, aCell, aSpare);
207599 }
207600
207601 for(ii=0; ii<pRtree->nDim; ii++){
207602 RtreeDValue margin = RTREE_ZERO;
207603 RtreeDValue fBestOverlap = RTREE_ZERO;
207604 RtreeDValue fBestArea = RTREE_ZERO;
207605 int iBestLeft = 0;
207606 int nLeft;
207607
207608 for(
207609 nLeft=RTREE_MINCELLS(pRtree);
207610 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
207611 nLeft++
207612 ){
207613 RtreeCell left;
207614 RtreeCell right;
207615 int kk;
207616 RtreeDValue overlap;
207617 RtreeDValue area;
207618
207619 memcpy(dest: &left, src: &aCell[aaSorted[ii][0]], n: sizeof(RtreeCell));
207620 memcpy(dest: &right, src: &aCell[aaSorted[ii][nCell-1]], n: sizeof(RtreeCell));
207621 for(kk=1; kk<(nCell-1); kk++){
207622 if( kk<nLeft ){
207623 cellUnion(pRtree, p1: &left, p2: &aCell[aaSorted[ii][kk]]);
207624 }else{
207625 cellUnion(pRtree, p1: &right, p2: &aCell[aaSorted[ii][kk]]);
207626 }
207627 }
207628 margin += cellMargin(pRtree, p: &left);
207629 margin += cellMargin(pRtree, p: &right);
207630 overlap = cellOverlap(pRtree, p: &left, aCell: &right, nCell: 1);
207631 area = cellArea(pRtree, p: &left) + cellArea(pRtree, p: &right);
207632 if( (nLeft==RTREE_MINCELLS(pRtree))
207633 || (overlap<fBestOverlap)
207634 || (overlap==fBestOverlap && area<fBestArea)
207635 ){
207636 iBestLeft = nLeft;
207637 fBestOverlap = overlap;
207638 fBestArea = area;
207639 }
207640 }
207641
207642 if( ii==0 || margin<fBestMargin ){
207643 iBestDim = ii;
207644 fBestMargin = margin;
207645 iBestSplit = iBestLeft;
207646 }
207647 }
207648
207649 memcpy(dest: pBboxLeft, src: &aCell[aaSorted[iBestDim][0]], n: sizeof(RtreeCell));
207650 memcpy(dest: pBboxRight, src: &aCell[aaSorted[iBestDim][iBestSplit]], n: sizeof(RtreeCell));
207651 for(ii=0; ii<nCell; ii++){
207652 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
207653 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
207654 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
207655 nodeInsertCell(pRtree, pNode: pTarget, pCell);
207656 cellUnion(pRtree, p1: pBbox, p2: pCell);
207657 }
207658
207659 sqlite3_free(p: aaSorted);
207660 return SQLITE_OK;
207661}
207662
207663
207664static int updateMapping(
207665 Rtree *pRtree,
207666 i64 iRowid,
207667 RtreeNode *pNode,
207668 int iHeight
207669){
207670 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
207671 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
207672 if( iHeight>0 ){
207673 RtreeNode *pChild = nodeHashLookup(pRtree, iNode: iRowid);
207674 RtreeNode *p;
207675 for(p=pNode; p; p=p->pParent){
207676 if( p==pChild ) return SQLITE_CORRUPT_VTAB;
207677 }
207678 if( pChild ){
207679 nodeRelease(pRtree, pNode: pChild->pParent);
207680 nodeReference(p: pNode);
207681 pChild->pParent = pNode;
207682 }
207683 }
207684 if( NEVER(pNode==0) ) return SQLITE_ERROR;
207685 return xSetMapping(pRtree, iRowid, pNode->iNode);
207686}
207687
207688static int SplitNode(
207689 Rtree *pRtree,
207690 RtreeNode *pNode,
207691 RtreeCell *pCell,
207692 int iHeight
207693){
207694 int i;
207695 int newCellIsRight = 0;
207696
207697 int rc = SQLITE_OK;
207698 int nCell = NCELL(pNode);
207699 RtreeCell *aCell;
207700 int *aiUsed;
207701
207702 RtreeNode *pLeft = 0;
207703 RtreeNode *pRight = 0;
207704
207705 RtreeCell leftbbox;
207706 RtreeCell rightbbox;
207707
207708 /* Allocate an array and populate it with a copy of pCell and
207709 ** all cells from node pLeft. Then zero the original node.
207710 */
207711 aCell = sqlite3_malloc64(n: (sizeof(RtreeCell)+sizeof(int))*(nCell+1));
207712 if( !aCell ){
207713 rc = SQLITE_NOMEM;
207714 goto splitnode_out;
207715 }
207716 aiUsed = (int *)&aCell[nCell+1];
207717 memset(s: aiUsed, c: 0, n: sizeof(int)*(nCell+1));
207718 for(i=0; i<nCell; i++){
207719 nodeGetCell(pRtree, pNode, iCell: i, pCell: &aCell[i]);
207720 }
207721 nodeZero(pRtree, p: pNode);
207722 memcpy(dest: &aCell[nCell], src: pCell, n: sizeof(RtreeCell));
207723 nCell++;
207724
207725 if( pNode->iNode==1 ){
207726 pRight = nodeNew(pRtree, pParent: pNode);
207727 pLeft = nodeNew(pRtree, pParent: pNode);
207728 pRtree->iDepth++;
207729 pNode->isDirty = 1;
207730 writeInt16(p: pNode->zData, i: pRtree->iDepth);
207731 }else{
207732 pLeft = pNode;
207733 pRight = nodeNew(pRtree, pParent: pLeft->pParent);
207734 pLeft->nRef++;
207735 }
207736
207737 if( !pLeft || !pRight ){
207738 rc = SQLITE_NOMEM;
207739 goto splitnode_out;
207740 }
207741
207742 memset(s: pLeft->zData, c: 0, n: pRtree->iNodeSize);
207743 memset(s: pRight->zData, c: 0, n: pRtree->iNodeSize);
207744
207745 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
207746 pBboxLeft: &leftbbox, pBboxRight: &rightbbox);
207747 if( rc!=SQLITE_OK ){
207748 goto splitnode_out;
207749 }
207750
207751 /* Ensure both child nodes have node numbers assigned to them by calling
207752 ** nodeWrite(). Node pRight always needs a node number, as it was created
207753 ** by nodeNew() above. But node pLeft sometimes already has a node number.
207754 ** In this case avoid the all to nodeWrite().
207755 */
207756 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pNode: pRight))
207757 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pNode: pLeft)))
207758 ){
207759 goto splitnode_out;
207760 }
207761
207762 rightbbox.iRowid = pRight->iNode;
207763 leftbbox.iRowid = pLeft->iNode;
207764
207765 if( pNode->iNode==1 ){
207766 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
207767 if( rc!=SQLITE_OK ){
207768 goto splitnode_out;
207769 }
207770 }else{
207771 RtreeNode *pParent = pLeft->pParent;
207772 int iCell;
207773 rc = nodeParentIndex(pRtree, pNode: pLeft, piIndex: &iCell);
207774 if( ALWAYS(rc==SQLITE_OK) ){
207775 nodeOverwriteCell(pRtree, pNode: pParent, pCell: &leftbbox, iCell);
207776 rc = AdjustTree(pRtree, pNode: pParent, pCell: &leftbbox);
207777 assert( rc==SQLITE_OK );
207778 }
207779 if( NEVER(rc!=SQLITE_OK) ){
207780 goto splitnode_out;
207781 }
207782 }
207783 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
207784 goto splitnode_out;
207785 }
207786
207787 for(i=0; i<NCELL(pRight); i++){
207788 i64 iRowid = nodeGetRowid(pRtree, pNode: pRight, iCell: i);
207789 rc = updateMapping(pRtree, iRowid, pNode: pRight, iHeight);
207790 if( iRowid==pCell->iRowid ){
207791 newCellIsRight = 1;
207792 }
207793 if( rc!=SQLITE_OK ){
207794 goto splitnode_out;
207795 }
207796 }
207797 if( pNode->iNode==1 ){
207798 for(i=0; i<NCELL(pLeft); i++){
207799 i64 iRowid = nodeGetRowid(pRtree, pNode: pLeft, iCell: i);
207800 rc = updateMapping(pRtree, iRowid, pNode: pLeft, iHeight);
207801 if( rc!=SQLITE_OK ){
207802 goto splitnode_out;
207803 }
207804 }
207805 }else if( newCellIsRight==0 ){
207806 rc = updateMapping(pRtree, iRowid: pCell->iRowid, pNode: pLeft, iHeight);
207807 }
207808
207809 if( rc==SQLITE_OK ){
207810 rc = nodeRelease(pRtree, pNode: pRight);
207811 pRight = 0;
207812 }
207813 if( rc==SQLITE_OK ){
207814 rc = nodeRelease(pRtree, pNode: pLeft);
207815 pLeft = 0;
207816 }
207817
207818splitnode_out:
207819 nodeRelease(pRtree, pNode: pRight);
207820 nodeRelease(pRtree, pNode: pLeft);
207821 sqlite3_free(p: aCell);
207822 return rc;
207823}
207824
207825/*
207826** If node pLeaf is not the root of the r-tree and its pParent pointer is
207827** still NULL, load all ancestor nodes of pLeaf into memory and populate
207828** the pLeaf->pParent chain all the way up to the root node.
207829**
207830** This operation is required when a row is deleted (or updated - an update
207831** is implemented as a delete followed by an insert). SQLite provides the
207832** rowid of the row to delete, which can be used to find the leaf on which
207833** the entry resides (argument pLeaf). Once the leaf is located, this
207834** function is called to determine its ancestry.
207835*/
207836static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
207837 int rc = SQLITE_OK;
207838 RtreeNode *pChild = pLeaf;
207839 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
207840 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
207841 sqlite3_bind_int64(pStmt: pRtree->pReadParent, i: 1, iValue: pChild->iNode);
207842 rc = sqlite3_step(pStmt: pRtree->pReadParent);
207843 if( rc==SQLITE_ROW ){
207844 RtreeNode *pTest; /* Used to test for reference loops */
207845 i64 iNode; /* Node number of parent node */
207846
207847 /* Before setting pChild->pParent, test that we are not creating a
207848 ** loop of references (as we would if, say, pChild==pParent). We don't
207849 ** want to do this as it leads to a memory leak when trying to delete
207850 ** the referenced counted node structures.
207851 */
207852 iNode = sqlite3_column_int64(pStmt: pRtree->pReadParent, i: 0);
207853 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
207854 if( pTest==0 ){
207855 rc2 = nodeAcquire(pRtree, iNode, pParent: 0, ppNode: &pChild->pParent);
207856 }
207857 }
207858 rc = sqlite3_reset(pStmt: pRtree->pReadParent);
207859 if( rc==SQLITE_OK ) rc = rc2;
207860 if( rc==SQLITE_OK && !pChild->pParent ){
207861 RTREE_IS_CORRUPT(pRtree);
207862 rc = SQLITE_CORRUPT_VTAB;
207863 }
207864 pChild = pChild->pParent;
207865 }
207866 return rc;
207867}
207868
207869static int deleteCell(Rtree *, RtreeNode *, int, int);
207870
207871static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
207872 int rc;
207873 int rc2;
207874 RtreeNode *pParent = 0;
207875 int iCell;
207876
207877 assert( pNode->nRef==1 );
207878
207879 /* Remove the entry in the parent cell. */
207880 rc = nodeParentIndex(pRtree, pNode, piIndex: &iCell);
207881 if( rc==SQLITE_OK ){
207882 pParent = pNode->pParent;
207883 pNode->pParent = 0;
207884 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
207885 testcase( rc!=SQLITE_OK );
207886 }
207887 rc2 = nodeRelease(pRtree, pNode: pParent);
207888 if( rc==SQLITE_OK ){
207889 rc = rc2;
207890 }
207891 if( rc!=SQLITE_OK ){
207892 return rc;
207893 }
207894
207895 /* Remove the xxx_node entry. */
207896 sqlite3_bind_int64(pStmt: pRtree->pDeleteNode, i: 1, iValue: pNode->iNode);
207897 sqlite3_step(pStmt: pRtree->pDeleteNode);
207898 if( SQLITE_OK!=(rc = sqlite3_reset(pStmt: pRtree->pDeleteNode)) ){
207899 return rc;
207900 }
207901
207902 /* Remove the xxx_parent entry. */
207903 sqlite3_bind_int64(pStmt: pRtree->pDeleteParent, i: 1, iValue: pNode->iNode);
207904 sqlite3_step(pStmt: pRtree->pDeleteParent);
207905 if( SQLITE_OK!=(rc = sqlite3_reset(pStmt: pRtree->pDeleteParent)) ){
207906 return rc;
207907 }
207908
207909 /* Remove the node from the in-memory hash table and link it into
207910 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
207911 */
207912 nodeHashDelete(pRtree, pNode);
207913 pNode->iNode = iHeight;
207914 pNode->pNext = pRtree->pDeleted;
207915 pNode->nRef++;
207916 pRtree->pDeleted = pNode;
207917
207918 return SQLITE_OK;
207919}
207920
207921static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
207922 RtreeNode *pParent = pNode->pParent;
207923 int rc = SQLITE_OK;
207924 if( pParent ){
207925 int ii;
207926 int nCell = NCELL(pNode);
207927 RtreeCell box; /* Bounding box for pNode */
207928 nodeGetCell(pRtree, pNode, iCell: 0, pCell: &box);
207929 for(ii=1; ii<nCell; ii++){
207930 RtreeCell cell;
207931 nodeGetCell(pRtree, pNode, iCell: ii, pCell: &cell);
207932 cellUnion(pRtree, p1: &box, p2: &cell);
207933 }
207934 box.iRowid = pNode->iNode;
207935 rc = nodeParentIndex(pRtree, pNode, piIndex: &ii);
207936 if( rc==SQLITE_OK ){
207937 nodeOverwriteCell(pRtree, pNode: pParent, pCell: &box, iCell: ii);
207938 rc = fixBoundingBox(pRtree, pNode: pParent);
207939 }
207940 }
207941 return rc;
207942}
207943
207944/*
207945** Delete the cell at index iCell of node pNode. After removing the
207946** cell, adjust the r-tree data structure if required.
207947*/
207948static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
207949 RtreeNode *pParent;
207950 int rc;
207951
207952 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pLeaf: pNode)) ){
207953 return rc;
207954 }
207955
207956 /* Remove the cell from the node. This call just moves bytes around
207957 ** the in-memory node image, so it cannot fail.
207958 */
207959 nodeDeleteCell(pRtree, pNode, iCell);
207960
207961 /* If the node is not the tree root and now has less than the minimum
207962 ** number of cells, remove it from the tree. Otherwise, update the
207963 ** cell in the parent node so that it tightly contains the updated
207964 ** node.
207965 */
207966 pParent = pNode->pParent;
207967 assert( pParent || pNode->iNode==1 );
207968 if( pParent ){
207969 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
207970 rc = removeNode(pRtree, pNode, iHeight);
207971 }else{
207972 rc = fixBoundingBox(pRtree, pNode);
207973 }
207974 }
207975
207976 return rc;
207977}
207978
207979static int Reinsert(
207980 Rtree *pRtree,
207981 RtreeNode *pNode,
207982 RtreeCell *pCell,
207983 int iHeight
207984){
207985 int *aOrder;
207986 int *aSpare;
207987 RtreeCell *aCell;
207988 RtreeDValue *aDistance;
207989 int nCell;
207990 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
207991 int iDim;
207992 int ii;
207993 int rc = SQLITE_OK;
207994 int n;
207995
207996 memset(s: aCenterCoord, c: 0, n: sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
207997
207998 nCell = NCELL(pNode)+1;
207999 n = (nCell+1)&(~1);
208000
208001 /* Allocate the buffers used by this operation. The allocation is
208002 ** relinquished before this function returns.
208003 */
208004 aCell = (RtreeCell *)sqlite3_malloc64(n: n * (
208005 sizeof(RtreeCell) + /* aCell array */
208006 sizeof(int) + /* aOrder array */
208007 sizeof(int) + /* aSpare array */
208008 sizeof(RtreeDValue) /* aDistance array */
208009 ));
208010 if( !aCell ){
208011 return SQLITE_NOMEM;
208012 }
208013 aOrder = (int *)&aCell[n];
208014 aSpare = (int *)&aOrder[n];
208015 aDistance = (RtreeDValue *)&aSpare[n];
208016
208017 for(ii=0; ii<nCell; ii++){
208018 if( ii==(nCell-1) ){
208019 memcpy(dest: &aCell[ii], src: pCell, n: sizeof(RtreeCell));
208020 }else{
208021 nodeGetCell(pRtree, pNode, iCell: ii, pCell: &aCell[ii]);
208022 }
208023 aOrder[ii] = ii;
208024 for(iDim=0; iDim<pRtree->nDim; iDim++){
208025 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
208026 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
208027 }
208028 }
208029 for(iDim=0; iDim<pRtree->nDim; iDim++){
208030 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
208031 }
208032
208033 for(ii=0; ii<nCell; ii++){
208034 aDistance[ii] = RTREE_ZERO;
208035 for(iDim=0; iDim<pRtree->nDim; iDim++){
208036 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
208037 DCOORD(aCell[ii].aCoord[iDim*2]));
208038 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
208039 }
208040 }
208041
208042 SortByDistance(aIdx: aOrder, nIdx: nCell, aDistance, aSpare);
208043 nodeZero(pRtree, p: pNode);
208044
208045 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
208046 RtreeCell *p = &aCell[aOrder[ii]];
208047 nodeInsertCell(pRtree, pNode, pCell: p);
208048 if( p->iRowid==pCell->iRowid ){
208049 if( iHeight==0 ){
208050 rc = rowidWrite(pRtree, iRowid: p->iRowid, iNode: pNode->iNode);
208051 }else{
208052 rc = parentWrite(pRtree, iNode: p->iRowid, iPar: pNode->iNode);
208053 }
208054 }
208055 }
208056 if( rc==SQLITE_OK ){
208057 rc = fixBoundingBox(pRtree, pNode);
208058 }
208059 for(; rc==SQLITE_OK && ii<nCell; ii++){
208060 /* Find a node to store this cell in. pNode->iNode currently contains
208061 ** the height of the sub-tree headed by the cell.
208062 */
208063 RtreeNode *pInsert;
208064 RtreeCell *p = &aCell[aOrder[ii]];
208065 rc = ChooseLeaf(pRtree, pCell: p, iHeight, ppLeaf: &pInsert);
208066 if( rc==SQLITE_OK ){
208067 int rc2;
208068 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
208069 rc2 = nodeRelease(pRtree, pNode: pInsert);
208070 if( rc==SQLITE_OK ){
208071 rc = rc2;
208072 }
208073 }
208074 }
208075
208076 sqlite3_free(p: aCell);
208077 return rc;
208078}
208079
208080/*
208081** Insert cell pCell into node pNode. Node pNode is the head of a
208082** subtree iHeight high (leaf nodes have iHeight==0).
208083*/
208084static int rtreeInsertCell(
208085 Rtree *pRtree,
208086 RtreeNode *pNode,
208087 RtreeCell *pCell,
208088 int iHeight
208089){
208090 int rc = SQLITE_OK;
208091 if( iHeight>0 ){
208092 RtreeNode *pChild = nodeHashLookup(pRtree, iNode: pCell->iRowid);
208093 if( pChild ){
208094 nodeRelease(pRtree, pNode: pChild->pParent);
208095 nodeReference(p: pNode);
208096 pChild->pParent = pNode;
208097 }
208098 }
208099 if( nodeInsertCell(pRtree, pNode, pCell) ){
208100 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
208101 rc = SplitNode(pRtree, pNode, pCell, iHeight);
208102 }else{
208103 pRtree->iReinsertHeight = iHeight;
208104 rc = Reinsert(pRtree, pNode, pCell, iHeight);
208105 }
208106 }else{
208107 rc = AdjustTree(pRtree, pNode, pCell);
208108 if( ALWAYS(rc==SQLITE_OK) ){
208109 if( iHeight==0 ){
208110 rc = rowidWrite(pRtree, iRowid: pCell->iRowid, iNode: pNode->iNode);
208111 }else{
208112 rc = parentWrite(pRtree, iNode: pCell->iRowid, iPar: pNode->iNode);
208113 }
208114 }
208115 }
208116 return rc;
208117}
208118
208119static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
208120 int ii;
208121 int rc = SQLITE_OK;
208122 int nCell = NCELL(pNode);
208123
208124 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
208125 RtreeNode *pInsert;
208126 RtreeCell cell;
208127 nodeGetCell(pRtree, pNode, iCell: ii, pCell: &cell);
208128
208129 /* Find a node to store this cell in. pNode->iNode currently contains
208130 ** the height of the sub-tree headed by the cell.
208131 */
208132 rc = ChooseLeaf(pRtree, pCell: &cell, iHeight: (int)pNode->iNode, ppLeaf: &pInsert);
208133 if( rc==SQLITE_OK ){
208134 int rc2;
208135 rc = rtreeInsertCell(pRtree, pNode: pInsert, pCell: &cell, iHeight: (int)pNode->iNode);
208136 rc2 = nodeRelease(pRtree, pNode: pInsert);
208137 if( rc==SQLITE_OK ){
208138 rc = rc2;
208139 }
208140 }
208141 }
208142 return rc;
208143}
208144
208145/*
208146** Select a currently unused rowid for a new r-tree record.
208147*/
208148static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
208149 int rc;
208150 sqlite3_bind_null(pStmt: pRtree->pWriteRowid, i: 1);
208151 sqlite3_bind_null(pStmt: pRtree->pWriteRowid, i: 2);
208152 sqlite3_step(pStmt: pRtree->pWriteRowid);
208153 rc = sqlite3_reset(pStmt: pRtree->pWriteRowid);
208154 *piRowid = sqlite3_last_insert_rowid(db: pRtree->db);
208155 return rc;
208156}
208157
208158/*
208159** Remove the entry with rowid=iDelete from the r-tree structure.
208160*/
208161static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
208162 int rc; /* Return code */
208163 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
208164 int iCell; /* Index of iDelete cell in pLeaf */
208165 RtreeNode *pRoot = 0; /* Root node of rtree structure */
208166
208167
208168 /* Obtain a reference to the root node to initialize Rtree.iDepth */
208169 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pRoot);
208170
208171 /* Obtain a reference to the leaf node that contains the entry
208172 ** about to be deleted.
208173 */
208174 if( rc==SQLITE_OK ){
208175 rc = findLeafNode(pRtree, iRowid: iDelete, ppLeaf: &pLeaf, piNode: 0);
208176 }
208177
208178#ifdef CORRUPT_DB
208179 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB );
208180#endif
208181
208182 /* Delete the cell in question from the leaf node. */
208183 if( rc==SQLITE_OK && pLeaf ){
208184 int rc2;
208185 rc = nodeRowidIndex(pRtree, pNode: pLeaf, iRowid: iDelete, piIndex: &iCell);
208186 if( rc==SQLITE_OK ){
208187 rc = deleteCell(pRtree, pNode: pLeaf, iCell, iHeight: 0);
208188 }
208189 rc2 = nodeRelease(pRtree, pNode: pLeaf);
208190 if( rc==SQLITE_OK ){
208191 rc = rc2;
208192 }
208193 }
208194
208195 /* Delete the corresponding entry in the <rtree>_rowid table. */
208196 if( rc==SQLITE_OK ){
208197 sqlite3_bind_int64(pStmt: pRtree->pDeleteRowid, i: 1, iValue: iDelete);
208198 sqlite3_step(pStmt: pRtree->pDeleteRowid);
208199 rc = sqlite3_reset(pStmt: pRtree->pDeleteRowid);
208200 }
208201
208202 /* Check if the root node now has exactly one child. If so, remove
208203 ** it, schedule the contents of the child for reinsertion and
208204 ** reduce the tree height by one.
208205 **
208206 ** This is equivalent to copying the contents of the child into
208207 ** the root node (the operation that Gutman's paper says to perform
208208 ** in this scenario).
208209 */
208210 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
208211 int rc2;
208212 RtreeNode *pChild = 0;
208213 i64 iChild = nodeGetRowid(pRtree, pNode: pRoot, iCell: 0);
208214 rc = nodeAcquire(pRtree, iNode: iChild, pParent: pRoot, ppNode: &pChild); /* tag-20210916a */
208215 if( rc==SQLITE_OK ){
208216 rc = removeNode(pRtree, pNode: pChild, iHeight: pRtree->iDepth-1);
208217 }
208218 rc2 = nodeRelease(pRtree, pNode: pChild);
208219 if( rc==SQLITE_OK ) rc = rc2;
208220 if( rc==SQLITE_OK ){
208221 pRtree->iDepth--;
208222 writeInt16(p: pRoot->zData, i: pRtree->iDepth);
208223 pRoot->isDirty = 1;
208224 }
208225 }
208226
208227 /* Re-insert the contents of any underfull nodes removed from the tree. */
208228 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
208229 if( rc==SQLITE_OK ){
208230 rc = reinsertNodeContent(pRtree, pNode: pLeaf);
208231 }
208232 pRtree->pDeleted = pLeaf->pNext;
208233 pRtree->nNodeRef--;
208234 sqlite3_free(p: pLeaf);
208235 }
208236
208237 /* Release the reference to the root node. */
208238 if( rc==SQLITE_OK ){
208239 rc = nodeRelease(pRtree, pNode: pRoot);
208240 }else{
208241 nodeRelease(pRtree, pNode: pRoot);
208242 }
208243
208244 return rc;
208245}
208246
208247/*
208248** Rounding constants for float->double conversion.
208249*/
208250#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
208251#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
208252
208253#if !defined(SQLITE_RTREE_INT_ONLY)
208254/*
208255** Convert an sqlite3_value into an RtreeValue (presumably a float)
208256** while taking care to round toward negative or positive, respectively.
208257*/
208258static RtreeValue rtreeValueDown(sqlite3_value *v){
208259 double d = sqlite3_value_double(pVal: v);
208260 float f = (float)d;
208261 if( f>d ){
208262 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
208263 }
208264 return f;
208265}
208266static RtreeValue rtreeValueUp(sqlite3_value *v){
208267 double d = sqlite3_value_double(pVal: v);
208268 float f = (float)d;
208269 if( f<d ){
208270 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
208271 }
208272 return f;
208273}
208274#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
208275
208276/*
208277** A constraint has failed while inserting a row into an rtree table.
208278** Assuming no OOM error occurs, this function sets the error message
208279** (at pRtree->base.zErrMsg) to an appropriate value and returns
208280** SQLITE_CONSTRAINT.
208281**
208282** Parameter iCol is the index of the leftmost column involved in the
208283** constraint failure. If it is 0, then the constraint that failed is
208284** the unique constraint on the id column. Otherwise, it is the rtree
208285** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
208286**
208287** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
208288*/
208289static int rtreeConstraintError(Rtree *pRtree, int iCol){
208290 sqlite3_stmt *pStmt = 0;
208291 char *zSql;
208292 int rc;
208293
208294 assert( iCol==0 || iCol%2 );
208295 zSql = sqlite3_mprintf(zFormat: "SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
208296 if( zSql ){
208297 rc = sqlite3_prepare_v2(db: pRtree->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
208298 }else{
208299 rc = SQLITE_NOMEM;
208300 }
208301 sqlite3_free(p: zSql);
208302
208303 if( rc==SQLITE_OK ){
208304 if( iCol==0 ){
208305 const char *zCol = sqlite3_column_name(pStmt, N: 0);
208306 pRtree->base.zErrMsg = sqlite3_mprintf(
208307 zFormat: "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
208308 );
208309 }else{
208310 const char *zCol1 = sqlite3_column_name(pStmt, N: iCol);
208311 const char *zCol2 = sqlite3_column_name(pStmt, N: iCol+1);
208312 pRtree->base.zErrMsg = sqlite3_mprintf(
208313 zFormat: "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
208314 );
208315 }
208316 }
208317
208318 sqlite3_finalize(pStmt);
208319 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
208320}
208321
208322
208323
208324/*
208325** The xUpdate method for rtree module virtual tables.
208326*/
208327static int rtreeUpdate(
208328 sqlite3_vtab *pVtab,
208329 int nData,
208330 sqlite3_value **aData,
208331 sqlite_int64 *pRowid
208332){
208333 Rtree *pRtree = (Rtree *)pVtab;
208334 int rc = SQLITE_OK;
208335 RtreeCell cell; /* New cell to insert if nData>1 */
208336 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
208337
208338 if( pRtree->nNodeRef ){
208339 /* Unable to write to the btree while another cursor is reading from it,
208340 ** since the write might do a rebalance which would disrupt the read
208341 ** cursor. */
208342 return SQLITE_LOCKED_VTAB;
208343 }
208344 rtreeReference(pRtree);
208345 assert(nData>=1);
208346
208347 memset(s: &cell, c: 0, n: sizeof(cell));
208348
208349 /* Constraint handling. A write operation on an r-tree table may return
208350 ** SQLITE_CONSTRAINT for two reasons:
208351 **
208352 ** 1. A duplicate rowid value, or
208353 ** 2. The supplied data violates the "x2>=x1" constraint.
208354 **
208355 ** In the first case, if the conflict-handling mode is REPLACE, then
208356 ** the conflicting row can be removed before proceeding. In the second
208357 ** case, SQLITE_CONSTRAINT must be returned regardless of the
208358 ** conflict-handling mode specified by the user.
208359 */
208360 if( nData>1 ){
208361 int ii;
208362 int nn = nData - 4;
208363
208364 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
208365 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
208366 **
208367 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
208368 ** with "column" that are interpreted as table constraints.
208369 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
208370 ** This problem was discovered after years of use, so we silently ignore
208371 ** these kinds of misdeclared tables to avoid breaking any legacy.
208372 */
208373
208374#ifndef SQLITE_RTREE_INT_ONLY
208375 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
208376 for(ii=0; ii<nn; ii+=2){
208377 cell.aCoord[ii].f = rtreeValueDown(v: aData[ii+3]);
208378 cell.aCoord[ii+1].f = rtreeValueUp(v: aData[ii+4]);
208379 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
208380 rc = rtreeConstraintError(pRtree, iCol: ii+1);
208381 goto constraint;
208382 }
208383 }
208384 }else
208385#endif
208386 {
208387 for(ii=0; ii<nn; ii+=2){
208388 cell.aCoord[ii].i = sqlite3_value_int(pVal: aData[ii+3]);
208389 cell.aCoord[ii+1].i = sqlite3_value_int(pVal: aData[ii+4]);
208390 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
208391 rc = rtreeConstraintError(pRtree, iCol: ii+1);
208392 goto constraint;
208393 }
208394 }
208395 }
208396
208397 /* If a rowid value was supplied, check if it is already present in
208398 ** the table. If so, the constraint has failed. */
208399 if( sqlite3_value_type(pVal: aData[2])!=SQLITE_NULL ){
208400 cell.iRowid = sqlite3_value_int64(pVal: aData[2]);
208401 if( sqlite3_value_type(pVal: aData[0])==SQLITE_NULL
208402 || sqlite3_value_int64(pVal: aData[0])!=cell.iRowid
208403 ){
208404 int steprc;
208405 sqlite3_bind_int64(pStmt: pRtree->pReadRowid, i: 1, iValue: cell.iRowid);
208406 steprc = sqlite3_step(pStmt: pRtree->pReadRowid);
208407 rc = sqlite3_reset(pStmt: pRtree->pReadRowid);
208408 if( SQLITE_ROW==steprc ){
208409 if( sqlite3_vtab_on_conflict(db: pRtree->db)==SQLITE_REPLACE ){
208410 rc = rtreeDeleteRowid(pRtree, iDelete: cell.iRowid);
208411 }else{
208412 rc = rtreeConstraintError(pRtree, iCol: 0);
208413 goto constraint;
208414 }
208415 }
208416 }
208417 bHaveRowid = 1;
208418 }
208419 }
208420
208421 /* If aData[0] is not an SQL NULL value, it is the rowid of a
208422 ** record to delete from the r-tree table. The following block does
208423 ** just that.
208424 */
208425 if( sqlite3_value_type(pVal: aData[0])!=SQLITE_NULL ){
208426 rc = rtreeDeleteRowid(pRtree, iDelete: sqlite3_value_int64(pVal: aData[0]));
208427 }
208428
208429 /* If the aData[] array contains more than one element, elements
208430 ** (aData[2]..aData[argc-1]) contain a new record to insert into
208431 ** the r-tree structure.
208432 */
208433 if( rc==SQLITE_OK && nData>1 ){
208434 /* Insert the new record into the r-tree */
208435 RtreeNode *pLeaf = 0;
208436
208437 /* Figure out the rowid of the new row. */
208438 if( bHaveRowid==0 ){
208439 rc = rtreeNewRowid(pRtree, piRowid: &cell.iRowid);
208440 }
208441 *pRowid = cell.iRowid;
208442
208443 if( rc==SQLITE_OK ){
208444 rc = ChooseLeaf(pRtree, pCell: &cell, iHeight: 0, ppLeaf: &pLeaf);
208445 }
208446 if( rc==SQLITE_OK ){
208447 int rc2;
208448 pRtree->iReinsertHeight = -1;
208449 rc = rtreeInsertCell(pRtree, pNode: pLeaf, pCell: &cell, iHeight: 0);
208450 rc2 = nodeRelease(pRtree, pNode: pLeaf);
208451 if( rc==SQLITE_OK ){
208452 rc = rc2;
208453 }
208454 }
208455 if( rc==SQLITE_OK && pRtree->nAux ){
208456 sqlite3_stmt *pUp = pRtree->pWriteAux;
208457 int jj;
208458 sqlite3_bind_int64(pStmt: pUp, i: 1, iValue: *pRowid);
208459 for(jj=0; jj<pRtree->nAux; jj++){
208460 sqlite3_bind_value(pStmt: pUp, i: jj+2, pValue: aData[pRtree->nDim2+3+jj]);
208461 }
208462 sqlite3_step(pStmt: pUp);
208463 rc = sqlite3_reset(pStmt: pUp);
208464 }
208465 }
208466
208467constraint:
208468 rtreeRelease(pRtree);
208469 return rc;
208470}
208471
208472/*
208473** Called when a transaction starts.
208474*/
208475static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
208476 Rtree *pRtree = (Rtree *)pVtab;
208477 assert( pRtree->inWrTrans==0 );
208478 pRtree->inWrTrans++;
208479 return SQLITE_OK;
208480}
208481
208482/*
208483** Called when a transaction completes (either by COMMIT or ROLLBACK).
208484** The sqlite3_blob object should be released at this point.
208485*/
208486static int rtreeEndTransaction(sqlite3_vtab *pVtab){
208487 Rtree *pRtree = (Rtree *)pVtab;
208488 pRtree->inWrTrans = 0;
208489 nodeBlobReset(pRtree);
208490 return SQLITE_OK;
208491}
208492
208493/*
208494** The xRename method for rtree module virtual tables.
208495*/
208496static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
208497 Rtree *pRtree = (Rtree *)pVtab;
208498 int rc = SQLITE_NOMEM;
208499 char *zSql = sqlite3_mprintf(
208500 zFormat: "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
208501 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
208502 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
208503 , pRtree->zDb, pRtree->zName, zNewName
208504 , pRtree->zDb, pRtree->zName, zNewName
208505 , pRtree->zDb, pRtree->zName, zNewName
208506 );
208507 if( zSql ){
208508 nodeBlobReset(pRtree);
208509 rc = sqlite3_exec(db: pRtree->db, zSql, xCallback: 0, pArg: 0, pzErrMsg: 0);
208510 sqlite3_free(p: zSql);
208511 }
208512 return rc;
208513}
208514
208515/*
208516** The xSavepoint method.
208517**
208518** This module does not need to do anything to support savepoints. However,
208519** it uses this hook to close any open blob handle. This is done because a
208520** DROP TABLE command - which fortunately always opens a savepoint - cannot
208521** succeed if there are any open blob handles. i.e. if the blob handle were
208522** not closed here, the following would fail:
208523**
208524** BEGIN;
208525** INSERT INTO rtree...
208526** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
208527** COMMIT;
208528*/
208529static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
208530 Rtree *pRtree = (Rtree *)pVtab;
208531 u8 iwt = pRtree->inWrTrans;
208532 UNUSED_PARAMETER(iSavepoint);
208533 pRtree->inWrTrans = 0;
208534 nodeBlobReset(pRtree);
208535 pRtree->inWrTrans = iwt;
208536 return SQLITE_OK;
208537}
208538
208539/*
208540** This function populates the pRtree->nRowEst variable with an estimate
208541** of the number of rows in the virtual table. If possible, this is based
208542** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
208543*/
208544static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
208545 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
208546 char *zSql;
208547 sqlite3_stmt *p;
208548 int rc;
208549 i64 nRow = RTREE_MIN_ROWEST;
208550
208551 rc = sqlite3_table_column_metadata(
208552 db, zDbName: pRtree->zDb, zTableName: "sqlite_stat1",zColumnName: 0,pzDataType: 0,pzCollSeq: 0,pNotNull: 0,pPrimaryKey: 0,pAutoinc: 0
208553 );
208554 if( rc!=SQLITE_OK ){
208555 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
208556 return rc==SQLITE_ERROR ? SQLITE_OK : rc;
208557 }
208558 zSql = sqlite3_mprintf(zFormat: zFmt, pRtree->zDb, pRtree->zName);
208559 if( zSql==0 ){
208560 rc = SQLITE_NOMEM;
208561 }else{
208562 rc = sqlite3_prepare_v2(db, zSql, nBytes: -1, ppStmt: &p, pzTail: 0);
208563 if( rc==SQLITE_OK ){
208564 if( sqlite3_step(pStmt: p)==SQLITE_ROW ) nRow = sqlite3_column_int64(pStmt: p, i: 0);
208565 rc = sqlite3_finalize(pStmt: p);
208566 }
208567 sqlite3_free(p: zSql);
208568 }
208569 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
208570 return rc;
208571}
208572
208573
208574/*
208575** Return true if zName is the extension on one of the shadow tables used
208576** by this module.
208577*/
208578static int rtreeShadowName(const char *zName){
208579 static const char *azName[] = {
208580 "node", "parent", "rowid"
208581 };
208582 unsigned int i;
208583 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
208584 if( sqlite3_stricmp(zLeft: zName, zRight: azName[i])==0 ) return 1;
208585 }
208586 return 0;
208587}
208588
208589static sqlite3_module rtreeModule = {
208590 3, /* iVersion */
208591 rtreeCreate, /* xCreate - create a table */
208592 rtreeConnect, /* xConnect - connect to an existing table */
208593 rtreeBestIndex, /* xBestIndex - Determine search strategy */
208594 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
208595 rtreeDestroy, /* xDestroy - Drop a table */
208596 rtreeOpen, /* xOpen - open a cursor */
208597 rtreeClose, /* xClose - close a cursor */
208598 rtreeFilter, /* xFilter - configure scan constraints */
208599 rtreeNext, /* xNext - advance a cursor */
208600 rtreeEof, /* xEof */
208601 rtreeColumn, /* xColumn - read data */
208602 rtreeRowid, /* xRowid - read data */
208603 rtreeUpdate, /* xUpdate - write data */
208604 rtreeBeginTransaction, /* xBegin - begin transaction */
208605 rtreeEndTransaction, /* xSync - sync transaction */
208606 rtreeEndTransaction, /* xCommit - commit transaction */
208607 rtreeEndTransaction, /* xRollback - rollback transaction */
208608 0, /* xFindFunction - function overloading */
208609 rtreeRename, /* xRename - rename the table */
208610 rtreeSavepoint, /* xSavepoint */
208611 0, /* xRelease */
208612 0, /* xRollbackTo */
208613 rtreeShadowName /* xShadowName */
208614};
208615
208616static int rtreeSqlInit(
208617 Rtree *pRtree,
208618 sqlite3 *db,
208619 const char *zDb,
208620 const char *zPrefix,
208621 int isCreate
208622){
208623 int rc = SQLITE_OK;
208624
208625 #define N_STATEMENT 8
208626 static const char *azSql[N_STATEMENT] = {
208627 /* Write the xxx_node table */
208628 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
208629 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
208630
208631 /* Read and write the xxx_rowid table */
208632 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
208633 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
208634 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
208635
208636 /* Read and write the xxx_parent table */
208637 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
208638 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
208639 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
208640 };
208641 sqlite3_stmt **appStmt[N_STATEMENT];
208642 int i;
208643 const int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
208644
208645 pRtree->db = db;
208646
208647 if( isCreate ){
208648 char *zCreate;
208649 sqlite3_str *p = sqlite3_str_new(db);
208650 int ii;
208651 sqlite3_str_appendf(p,
208652 zFormat: "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
208653 zDb, zPrefix);
208654 for(ii=0; ii<pRtree->nAux; ii++){
208655 sqlite3_str_appendf(p,zFormat: ",a%d",ii);
208656 }
208657 sqlite3_str_appendf(p,
208658 zFormat: ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
208659 zDb, zPrefix);
208660 sqlite3_str_appendf(p,
208661 zFormat: "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
208662 zDb, zPrefix);
208663 sqlite3_str_appendf(p,
208664 zFormat: "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
208665 zDb, zPrefix, pRtree->iNodeSize);
208666 zCreate = sqlite3_str_finish(p);
208667 if( !zCreate ){
208668 return SQLITE_NOMEM;
208669 }
208670 rc = sqlite3_exec(db, zSql: zCreate, xCallback: 0, pArg: 0, pzErrMsg: 0);
208671 sqlite3_free(p: zCreate);
208672 if( rc!=SQLITE_OK ){
208673 return rc;
208674 }
208675 }
208676
208677 appStmt[0] = &pRtree->pWriteNode;
208678 appStmt[1] = &pRtree->pDeleteNode;
208679 appStmt[2] = &pRtree->pReadRowid;
208680 appStmt[3] = &pRtree->pWriteRowid;
208681 appStmt[4] = &pRtree->pDeleteRowid;
208682 appStmt[5] = &pRtree->pReadParent;
208683 appStmt[6] = &pRtree->pWriteParent;
208684 appStmt[7] = &pRtree->pDeleteParent;
208685
208686 rc = rtreeQueryStat1(db, pRtree);
208687 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
208688 char *zSql;
208689 const char *zFormat;
208690 if( i!=3 || pRtree->nAux==0 ){
208691 zFormat = azSql[i];
208692 }else {
208693 /* An UPSERT is very slightly slower than REPLACE, but it is needed
208694 ** if there are auxiliary columns */
208695 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
208696 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
208697 }
208698 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
208699 if( zSql ){
208700 rc = sqlite3_prepare_v3(db, zSql, nBytes: -1, prepFlags: f, ppStmt: appStmt[i], pzTail: 0);
208701 }else{
208702 rc = SQLITE_NOMEM;
208703 }
208704 sqlite3_free(p: zSql);
208705 }
208706 if( pRtree->nAux ){
208707 pRtree->zReadAuxSql = sqlite3_mprintf(
208708 zFormat: "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
208709 zDb, zPrefix);
208710 if( pRtree->zReadAuxSql==0 ){
208711 rc = SQLITE_NOMEM;
208712 }else{
208713 sqlite3_str *p = sqlite3_str_new(db);
208714 int ii;
208715 char *zSql;
208716 sqlite3_str_appendf(p, zFormat: "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
208717 for(ii=0; ii<pRtree->nAux; ii++){
208718 if( ii ) sqlite3_str_append(p, z: ",", N: 1);
208719#ifdef SQLITE_ENABLE_GEOPOLY
208720 if( ii<pRtree->nAuxNotNull ){
208721 sqlite3_str_appendf(p,zFormat: "a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
208722 }else
208723#endif
208724 {
208725 sqlite3_str_appendf(p,zFormat: "a%d=?%d",ii,ii+2);
208726 }
208727 }
208728 sqlite3_str_appendf(p, zFormat: " WHERE rowid=?1");
208729 zSql = sqlite3_str_finish(p);
208730 if( zSql==0 ){
208731 rc = SQLITE_NOMEM;
208732 }else{
208733 rc = sqlite3_prepare_v3(db, zSql, nBytes: -1, prepFlags: f, ppStmt: &pRtree->pWriteAux, pzTail: 0);
208734 sqlite3_free(p: zSql);
208735 }
208736 }
208737 }
208738
208739 return rc;
208740}
208741
208742/*
208743** The second argument to this function contains the text of an SQL statement
208744** that returns a single integer value. The statement is compiled and executed
208745** using database connection db. If successful, the integer value returned
208746** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
208747** code is returned and the value of *piVal after returning is not defined.
208748*/
208749static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
208750 int rc = SQLITE_NOMEM;
208751 if( zSql ){
208752 sqlite3_stmt *pStmt = 0;
208753 rc = sqlite3_prepare_v2(db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
208754 if( rc==SQLITE_OK ){
208755 if( SQLITE_ROW==sqlite3_step(pStmt) ){
208756 *piVal = sqlite3_column_int(pStmt, i: 0);
208757 }
208758 rc = sqlite3_finalize(pStmt);
208759 }
208760 }
208761 return rc;
208762}
208763
208764/*
208765** This function is called from within the xConnect() or xCreate() method to
208766** determine the node-size used by the rtree table being created or connected
208767** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
208768** Otherwise, an SQLite error code is returned.
208769**
208770** If this function is being called as part of an xConnect(), then the rtree
208771** table already exists. In this case the node-size is determined by inspecting
208772** the root node of the tree.
208773**
208774** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
208775** This ensures that each node is stored on a single database page. If the
208776** database page-size is so large that more than RTREE_MAXCELLS entries
208777** would fit in a single node, use a smaller node-size.
208778*/
208779static int getNodeSize(
208780 sqlite3 *db, /* Database handle */
208781 Rtree *pRtree, /* Rtree handle */
208782 int isCreate, /* True for xCreate, false for xConnect */
208783 char **pzErr /* OUT: Error message, if any */
208784){
208785 int rc;
208786 char *zSql;
208787 if( isCreate ){
208788 int iPageSize = 0;
208789 zSql = sqlite3_mprintf(zFormat: "PRAGMA %Q.page_size", pRtree->zDb);
208790 rc = getIntFromStmt(db, zSql, piVal: &iPageSize);
208791 if( rc==SQLITE_OK ){
208792 pRtree->iNodeSize = iPageSize-64;
208793 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
208794 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
208795 }
208796 }else{
208797 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
208798 }
208799 }else{
208800 zSql = sqlite3_mprintf(
208801 zFormat: "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
208802 pRtree->zDb, pRtree->zName
208803 );
208804 rc = getIntFromStmt(db, zSql, piVal: &pRtree->iNodeSize);
208805 if( rc!=SQLITE_OK ){
208806 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
208807 }else if( pRtree->iNodeSize<(512-64) ){
208808 rc = SQLITE_CORRUPT_VTAB;
208809 RTREE_IS_CORRUPT(pRtree);
208810 *pzErr = sqlite3_mprintf(zFormat: "undersize RTree blobs in \"%q_node\"",
208811 pRtree->zName);
208812 }
208813 }
208814
208815 sqlite3_free(p: zSql);
208816 return rc;
208817}
208818
208819/*
208820** Return the length of a token
208821*/
208822static int rtreeTokenLength(const char *z){
208823 int dummy = 0;
208824 return sqlite3GetToken((const unsigned char*)z,&dummy);
208825}
208826
208827/*
208828** This function is the implementation of both the xConnect and xCreate
208829** methods of the r-tree virtual table.
208830**
208831** argv[0] -> module name
208832** argv[1] -> database name
208833** argv[2] -> table name
208834** argv[...] -> column names...
208835*/
208836static int rtreeInit(
208837 sqlite3 *db, /* Database connection */
208838 void *pAux, /* One of the RTREE_COORD_* constants */
208839 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
208840 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
208841 char **pzErr, /* OUT: Error message, if any */
208842 int isCreate /* True for xCreate, false for xConnect */
208843){
208844 int rc = SQLITE_OK;
208845 Rtree *pRtree;
208846 int nDb; /* Length of string argv[1] */
208847 int nName; /* Length of string argv[2] */
208848 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
208849 sqlite3_str *pSql;
208850 char *zSql;
208851 int ii = 4;
208852 int iErr;
208853
208854 const char *aErrMsg[] = {
208855 0, /* 0 */
208856 "Wrong number of columns for an rtree table", /* 1 */
208857 "Too few columns for an rtree table", /* 2 */
208858 "Too many columns for an rtree table", /* 3 */
208859 "Auxiliary rtree columns must be last" /* 4 */
208860 };
208861
208862 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
208863 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN+3 ){
208864 *pzErr = sqlite3_mprintf(zFormat: "%s", aErrMsg[2 + (argc>=6)]);
208865 return SQLITE_ERROR;
208866 }
208867
208868 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
208869
208870 /* Allocate the sqlite3_vtab structure */
208871 nDb = (int)strlen(s: argv[1]);
208872 nName = (int)strlen(s: argv[2]);
208873 pRtree = (Rtree *)sqlite3_malloc64(n: sizeof(Rtree)+nDb+nName+2);
208874 if( !pRtree ){
208875 return SQLITE_NOMEM;
208876 }
208877 memset(s: pRtree, c: 0, n: sizeof(Rtree)+nDb+nName+2);
208878 pRtree->nBusy = 1;
208879 pRtree->base.pModule = &rtreeModule;
208880 pRtree->zDb = (char *)&pRtree[1];
208881 pRtree->zName = &pRtree->zDb[nDb+1];
208882 pRtree->eCoordType = (u8)eCoordType;
208883 memcpy(dest: pRtree->zDb, src: argv[1], n: nDb);
208884 memcpy(dest: pRtree->zName, src: argv[2], n: nName);
208885
208886
208887 /* Create/Connect to the underlying relational database schema. If
208888 ** that is successful, call sqlite3_declare_vtab() to configure
208889 ** the r-tree table schema.
208890 */
208891 pSql = sqlite3_str_new(db);
208892 sqlite3_str_appendf(p: pSql, zFormat: "CREATE TABLE x(%.*s INT",
208893 rtreeTokenLength(z: argv[3]), argv[3]);
208894 for(ii=4; ii<argc; ii++){
208895 const char *zArg = argv[ii];
208896 if( zArg[0]=='+' ){
208897 pRtree->nAux++;
208898 sqlite3_str_appendf(p: pSql, zFormat: ",%.*s", rtreeTokenLength(z: zArg+1), zArg+1);
208899 }else if( pRtree->nAux>0 ){
208900 break;
208901 }else{
208902 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
208903 pRtree->nDim2++;
208904 sqlite3_str_appendf(p: pSql, zFormat: azFormat[eCoordType],
208905 rtreeTokenLength(z: zArg), zArg);
208906 }
208907 }
208908 sqlite3_str_appendf(p: pSql, zFormat: ");");
208909 zSql = sqlite3_str_finish(p: pSql);
208910 if( !zSql ){
208911 rc = SQLITE_NOMEM;
208912 }else if( ii<argc ){
208913 *pzErr = sqlite3_mprintf(zFormat: "%s", aErrMsg[4]);
208914 rc = SQLITE_ERROR;
208915 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zCreateTable: zSql)) ){
208916 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
208917 }
208918 sqlite3_free(p: zSql);
208919 if( rc ) goto rtreeInit_fail;
208920 pRtree->nDim = pRtree->nDim2/2;
208921 if( pRtree->nDim<1 ){
208922 iErr = 2;
208923 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
208924 iErr = 3;
208925 }else if( pRtree->nDim2 % 2 ){
208926 iErr = 1;
208927 }else{
208928 iErr = 0;
208929 }
208930 if( iErr ){
208931 *pzErr = sqlite3_mprintf(zFormat: "%s", aErrMsg[iErr]);
208932 goto rtreeInit_fail;
208933 }
208934 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
208935
208936 /* Figure out the node size to use. */
208937 rc = getNodeSize(db, pRtree, isCreate, pzErr);
208938 if( rc ) goto rtreeInit_fail;
208939 rc = rtreeSqlInit(pRtree, db, zDb: argv[1], zPrefix: argv[2], isCreate);
208940 if( rc ){
208941 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
208942 goto rtreeInit_fail;
208943 }
208944
208945 *ppVtab = (sqlite3_vtab *)pRtree;
208946 return SQLITE_OK;
208947
208948rtreeInit_fail:
208949 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
208950 assert( *ppVtab==0 );
208951 assert( pRtree->nBusy==1 );
208952 rtreeRelease(pRtree);
208953 return rc;
208954}
208955
208956
208957/*
208958** Implementation of a scalar function that decodes r-tree nodes to
208959** human readable strings. This can be used for debugging and analysis.
208960**
208961** The scalar function takes two arguments: (1) the number of dimensions
208962** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
208963** an r-tree node. For a two-dimensional r-tree structure called "rt", to
208964** deserialize all nodes, a statement like:
208965**
208966** SELECT rtreenode(2, data) FROM rt_node;
208967**
208968** The human readable string takes the form of a Tcl list with one
208969** entry for each cell in the r-tree node. Each entry is itself a
208970** list, containing the 8-byte rowid/pageno followed by the
208971** <num-dimension>*2 coordinates.
208972*/
208973static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
208974 RtreeNode node;
208975 Rtree tree;
208976 int ii;
208977 int nData;
208978 int errCode;
208979 sqlite3_str *pOut;
208980
208981 UNUSED_PARAMETER(nArg);
208982 memset(s: &node, c: 0, n: sizeof(RtreeNode));
208983 memset(s: &tree, c: 0, n: sizeof(Rtree));
208984 tree.nDim = (u8)sqlite3_value_int(pVal: apArg[0]);
208985 if( tree.nDim<1 || tree.nDim>5 ) return;
208986 tree.nDim2 = tree.nDim*2;
208987 tree.nBytesPerCell = 8 + 8 * tree.nDim;
208988 node.zData = (u8 *)sqlite3_value_blob(pVal: apArg[1]);
208989 if( node.zData==0 ) return;
208990 nData = sqlite3_value_bytes(pVal: apArg[1]);
208991 if( nData<4 ) return;
208992 if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
208993
208994 pOut = sqlite3_str_new(db: 0);
208995 for(ii=0; ii<NCELL(&node); ii++){
208996 RtreeCell cell;
208997 int jj;
208998
208999 nodeGetCell(pRtree: &tree, pNode: &node, iCell: ii, pCell: &cell);
209000 if( ii>0 ) sqlite3_str_append(p: pOut, z: " ", N: 1);
209001 sqlite3_str_appendf(p: pOut, zFormat: "{%lld", cell.iRowid);
209002 for(jj=0; jj<tree.nDim2; jj++){
209003#ifndef SQLITE_RTREE_INT_ONLY
209004 sqlite3_str_appendf(p: pOut, zFormat: " %g", (double)cell.aCoord[jj].f);
209005#else
209006 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
209007#endif
209008 }
209009 sqlite3_str_append(p: pOut, z: "}", N: 1);
209010 }
209011 errCode = sqlite3_str_errcode(p: pOut);
209012 sqlite3_result_text(pCtx: ctx, z: sqlite3_str_finish(p: pOut), n: -1, xDel: sqlite3_free);
209013 sqlite3_result_error_code(pCtx: ctx, errCode);
209014}
209015
209016/* This routine implements an SQL function that returns the "depth" parameter
209017** from the front of a blob that is an r-tree node. For example:
209018**
209019** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
209020**
209021** The depth value is 0 for all nodes other than the root node, and the root
209022** node always has nodeno=1, so the example above is the primary use for this
209023** routine. This routine is intended for testing and analysis only.
209024*/
209025static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
209026 UNUSED_PARAMETER(nArg);
209027 if( sqlite3_value_type(pVal: apArg[0])!=SQLITE_BLOB
209028 || sqlite3_value_bytes(pVal: apArg[0])<2
209029
209030 ){
209031 sqlite3_result_error(pCtx: ctx, z: "Invalid argument to rtreedepth()", n: -1);
209032 }else{
209033 u8 *zBlob = (u8 *)sqlite3_value_blob(pVal: apArg[0]);
209034 if( zBlob ){
209035 sqlite3_result_int(pCtx: ctx, iVal: readInt16(p: zBlob));
209036 }else{
209037 sqlite3_result_error_nomem(pCtx: ctx);
209038 }
209039 }
209040}
209041
209042/*
209043** Context object passed between the various routines that make up the
209044** implementation of integrity-check function rtreecheck().
209045*/
209046typedef struct RtreeCheck RtreeCheck;
209047struct RtreeCheck {
209048 sqlite3 *db; /* Database handle */
209049 const char *zDb; /* Database containing rtree table */
209050 const char *zTab; /* Name of rtree table */
209051 int bInt; /* True for rtree_i32 table */
209052 int nDim; /* Number of dimensions for this rtree tbl */
209053 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
209054 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
209055 int nLeaf; /* Number of leaf cells in table */
209056 int nNonLeaf; /* Number of non-leaf cells in table */
209057 int rc; /* Return code */
209058 char *zReport; /* Message to report */
209059 int nErr; /* Number of lines in zReport */
209060};
209061
209062#define RTREE_CHECK_MAX_ERROR 100
209063
209064/*
209065** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
209066** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
209067*/
209068static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
209069 int rc = sqlite3_reset(pStmt);
209070 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
209071}
209072
209073/*
209074** The second and subsequent arguments to this function are a format string
209075** and printf style arguments. This function formats the string and attempts
209076** to compile it as an SQL statement.
209077**
209078** If successful, a pointer to the new SQL statement is returned. Otherwise,
209079** NULL is returned and an error code left in RtreeCheck.rc.
209080*/
209081static sqlite3_stmt *rtreeCheckPrepare(
209082 RtreeCheck *pCheck, /* RtreeCheck object */
209083 const char *zFmt, ... /* Format string and trailing args */
209084){
209085 va_list ap;
209086 char *z;
209087 sqlite3_stmt *pRet = 0;
209088
209089 va_start(ap, zFmt);
209090 z = sqlite3_vmprintf(zFormat: zFmt, ap);
209091
209092 if( pCheck->rc==SQLITE_OK ){
209093 if( z==0 ){
209094 pCheck->rc = SQLITE_NOMEM;
209095 }else{
209096 pCheck->rc = sqlite3_prepare_v2(db: pCheck->db, zSql: z, nBytes: -1, ppStmt: &pRet, pzTail: 0);
209097 }
209098 }
209099
209100 sqlite3_free(p: z);
209101 va_end(ap);
209102 return pRet;
209103}
209104
209105/*
209106** The second and subsequent arguments to this function are a printf()
209107** style format string and arguments. This function formats the string and
209108** appends it to the report being accumuated in pCheck.
209109*/
209110static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
209111 va_list ap;
209112 va_start(ap, zFmt);
209113 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
209114 char *z = sqlite3_vmprintf(zFormat: zFmt, ap);
209115 if( z==0 ){
209116 pCheck->rc = SQLITE_NOMEM;
209117 }else{
209118 pCheck->zReport = sqlite3_mprintf(zFormat: "%z%s%z",
209119 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
209120 );
209121 if( pCheck->zReport==0 ){
209122 pCheck->rc = SQLITE_NOMEM;
209123 }
209124 }
209125 pCheck->nErr++;
209126 }
209127 va_end(ap);
209128}
209129
209130/*
209131** This function is a no-op if there is already an error code stored
209132** in the RtreeCheck object indicated by the first argument. NULL is
209133** returned in this case.
209134**
209135** Otherwise, the contents of rtree table node iNode are loaded from
209136** the database and copied into a buffer obtained from sqlite3_malloc().
209137** If no error occurs, a pointer to the buffer is returned and (*pnNode)
209138** is set to the size of the buffer in bytes.
209139**
209140** Or, if an error does occur, NULL is returned and an error code left
209141** in the RtreeCheck object. The final value of *pnNode is undefined in
209142** this case.
209143*/
209144static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
209145 u8 *pRet = 0; /* Return value */
209146
209147 if( pCheck->rc==SQLITE_OK && pCheck->pGetNode==0 ){
209148 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
209149 zFmt: "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
209150 pCheck->zDb, pCheck->zTab
209151 );
209152 }
209153
209154 if( pCheck->rc==SQLITE_OK ){
209155 sqlite3_bind_int64(pStmt: pCheck->pGetNode, i: 1, iValue: iNode);
209156 if( sqlite3_step(pStmt: pCheck->pGetNode)==SQLITE_ROW ){
209157 int nNode = sqlite3_column_bytes(pStmt: pCheck->pGetNode, i: 0);
209158 const u8 *pNode = (const u8*)sqlite3_column_blob(pStmt: pCheck->pGetNode, i: 0);
209159 pRet = sqlite3_malloc64(n: nNode);
209160 if( pRet==0 ){
209161 pCheck->rc = SQLITE_NOMEM;
209162 }else{
209163 memcpy(dest: pRet, src: pNode, n: nNode);
209164 *pnNode = nNode;
209165 }
209166 }
209167 rtreeCheckReset(pCheck, pStmt: pCheck->pGetNode);
209168 if( pCheck->rc==SQLITE_OK && pRet==0 ){
209169 rtreeCheckAppendMsg(pCheck, zFmt: "Node %lld missing from database", iNode);
209170 }
209171 }
209172
209173 return pRet;
209174}
209175
209176/*
209177** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
209178** (if bLeaf==1) table contains a specified entry. The schemas of the
209179** two tables are:
209180**
209181** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
209182** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
209183**
209184** In both cases, this function checks that there exists an entry with
209185** IPK value iKey and the second column set to iVal.
209186**
209187*/
209188static void rtreeCheckMapping(
209189 RtreeCheck *pCheck, /* RtreeCheck object */
209190 int bLeaf, /* True for a leaf cell, false for interior */
209191 i64 iKey, /* Key for mapping */
209192 i64 iVal /* Expected value for mapping */
209193){
209194 int rc;
209195 sqlite3_stmt *pStmt;
209196 const char *azSql[2] = {
209197 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
209198 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
209199 };
209200
209201 assert( bLeaf==0 || bLeaf==1 );
209202 if( pCheck->aCheckMapping[bLeaf]==0 ){
209203 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
209204 zFmt: azSql[bLeaf], pCheck->zDb, pCheck->zTab
209205 );
209206 }
209207 if( pCheck->rc!=SQLITE_OK ) return;
209208
209209 pStmt = pCheck->aCheckMapping[bLeaf];
209210 sqlite3_bind_int64(pStmt, i: 1, iValue: iKey);
209211 rc = sqlite3_step(pStmt);
209212 if( rc==SQLITE_DONE ){
209213 rtreeCheckAppendMsg(pCheck, zFmt: "Mapping (%lld -> %lld) missing from %s table",
209214 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
209215 );
209216 }else if( rc==SQLITE_ROW ){
209217 i64 ii = sqlite3_column_int64(pStmt, i: 0);
209218 if( ii!=iVal ){
209219 rtreeCheckAppendMsg(pCheck,
209220 zFmt: "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
209221 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
209222 );
209223 }
209224 }
209225 rtreeCheckReset(pCheck, pStmt);
209226}
209227
209228/*
209229** Argument pCell points to an array of coordinates stored on an rtree page.
209230** This function checks that the coordinates are internally consistent (no
209231** x1>x2 conditions) and adds an error message to the RtreeCheck object
209232** if they are not.
209233**
209234** Additionally, if pParent is not NULL, then it is assumed to point to
209235** the array of coordinates on the parent page that bound the page
209236** containing pCell. In this case it is also verified that the two
209237** sets of coordinates are mutually consistent and an error message added
209238** to the RtreeCheck object if they are not.
209239*/
209240static void rtreeCheckCellCoord(
209241 RtreeCheck *pCheck,
209242 i64 iNode, /* Node id to use in error messages */
209243 int iCell, /* Cell number to use in error messages */
209244 u8 *pCell, /* Pointer to cell coordinates */
209245 u8 *pParent /* Pointer to parent coordinates */
209246){
209247 RtreeCoord c1, c2;
209248 RtreeCoord p1, p2;
209249 int i;
209250
209251 for(i=0; i<pCheck->nDim; i++){
209252 readCoord(p: &pCell[4*2*i], pCoord: &c1);
209253 readCoord(p: &pCell[4*(2*i + 1)], pCoord: &c2);
209254
209255 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
209256 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
209257 rtreeCheckAppendMsg(pCheck,
209258 zFmt: "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
209259 );
209260 }
209261
209262 if( pParent ){
209263 readCoord(p: &pParent[4*2*i], pCoord: &p1);
209264 readCoord(p: &pParent[4*(2*i + 1)], pCoord: &p2);
209265
209266 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
209267 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
209268 ){
209269 rtreeCheckAppendMsg(pCheck,
209270 zFmt: "Dimension %d of cell %d on node %lld is corrupt relative to parent"
209271 , i, iCell, iNode
209272 );
209273 }
209274 }
209275 }
209276}
209277
209278/*
209279** Run rtreecheck() checks on node iNode, which is at depth iDepth within
209280** the r-tree structure. Argument aParent points to the array of coordinates
209281** that bound node iNode on the parent node.
209282**
209283** If any problems are discovered, an error message is appended to the
209284** report accumulated in the RtreeCheck object.
209285*/
209286static void rtreeCheckNode(
209287 RtreeCheck *pCheck,
209288 int iDepth, /* Depth of iNode (0==leaf) */
209289 u8 *aParent, /* Buffer containing parent coords */
209290 i64 iNode /* Node to check */
209291){
209292 u8 *aNode = 0;
209293 int nNode = 0;
209294
209295 assert( iNode==1 || aParent!=0 );
209296 assert( pCheck->nDim>0 );
209297
209298 aNode = rtreeCheckGetNode(pCheck, iNode, pnNode: &nNode);
209299 if( aNode ){
209300 if( nNode<4 ){
209301 rtreeCheckAppendMsg(pCheck,
209302 zFmt: "Node %lld is too small (%d bytes)", iNode, nNode
209303 );
209304 }else{
209305 int nCell; /* Number of cells on page */
209306 int i; /* Used to iterate through cells */
209307 if( aParent==0 ){
209308 iDepth = readInt16(p: aNode);
209309 if( iDepth>RTREE_MAX_DEPTH ){
209310 rtreeCheckAppendMsg(pCheck, zFmt: "Rtree depth out of range (%d)", iDepth);
209311 sqlite3_free(p: aNode);
209312 return;
209313 }
209314 }
209315 nCell = readInt16(p: &aNode[2]);
209316 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
209317 rtreeCheckAppendMsg(pCheck,
209318 zFmt: "Node %lld is too small for cell count of %d (%d bytes)",
209319 iNode, nCell, nNode
209320 );
209321 }else{
209322 for(i=0; i<nCell; i++){
209323 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
209324 i64 iVal = readInt64(p: pCell);
209325 rtreeCheckCellCoord(pCheck, iNode, iCell: i, pCell: &pCell[8], pParent: aParent);
209326
209327 if( iDepth>0 ){
209328 rtreeCheckMapping(pCheck, bLeaf: 0, iKey: iVal, iVal: iNode);
209329 rtreeCheckNode(pCheck, iDepth: iDepth-1, aParent: &pCell[8], iNode: iVal);
209330 pCheck->nNonLeaf++;
209331 }else{
209332 rtreeCheckMapping(pCheck, bLeaf: 1, iKey: iVal, iVal: iNode);
209333 pCheck->nLeaf++;
209334 }
209335 }
209336 }
209337 }
209338 sqlite3_free(p: aNode);
209339 }
209340}
209341
209342/*
209343** The second argument to this function must be either "_rowid" or
209344** "_parent". This function checks that the number of entries in the
209345** %_rowid or %_parent table is exactly nExpect. If not, it adds
209346** an error message to the report in the RtreeCheck object indicated
209347** by the first argument.
209348*/
209349static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
209350 if( pCheck->rc==SQLITE_OK ){
209351 sqlite3_stmt *pCount;
209352 pCount = rtreeCheckPrepare(pCheck, zFmt: "SELECT count(*) FROM %Q.'%q%s'",
209353 pCheck->zDb, pCheck->zTab, zTbl
209354 );
209355 if( pCount ){
209356 if( sqlite3_step(pStmt: pCount)==SQLITE_ROW ){
209357 i64 nActual = sqlite3_column_int64(pStmt: pCount, i: 0);
209358 if( nActual!=nExpect ){
209359 rtreeCheckAppendMsg(pCheck, zFmt: "Wrong number of entries in %%%s table"
209360 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
209361 );
209362 }
209363 }
209364 pCheck->rc = sqlite3_finalize(pStmt: pCount);
209365 }
209366 }
209367}
209368
209369/*
209370** This function does the bulk of the work for the rtree integrity-check.
209371** It is called by rtreecheck(), which is the SQL function implementation.
209372*/
209373static int rtreeCheckTable(
209374 sqlite3 *db, /* Database handle to access db through */
209375 const char *zDb, /* Name of db ("main", "temp" etc.) */
209376 const char *zTab, /* Name of rtree table to check */
209377 char **pzReport /* OUT: sqlite3_malloc'd report text */
209378){
209379 RtreeCheck check; /* Common context for various routines */
209380 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
209381 int bEnd = 0; /* True if transaction should be closed */
209382 int nAux = 0; /* Number of extra columns. */
209383
209384 /* Initialize the context object */
209385 memset(s: &check, c: 0, n: sizeof(check));
209386 check.db = db;
209387 check.zDb = zDb;
209388 check.zTab = zTab;
209389
209390 /* If there is not already an open transaction, open one now. This is
209391 ** to ensure that the queries run as part of this integrity-check operate
209392 ** on a consistent snapshot. */
209393 if( sqlite3_get_autocommit(db) ){
209394 check.rc = sqlite3_exec(db, zSql: "BEGIN", xCallback: 0, pArg: 0, pzErrMsg: 0);
209395 bEnd = 1;
209396 }
209397
209398 /* Find the number of auxiliary columns */
209399 if( check.rc==SQLITE_OK ){
209400 pStmt = rtreeCheckPrepare(pCheck: &check, zFmt: "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
209401 if( pStmt ){
209402 nAux = sqlite3_column_count(pStmt) - 2;
209403 sqlite3_finalize(pStmt);
209404 }else
209405 if( check.rc!=SQLITE_NOMEM ){
209406 check.rc = SQLITE_OK;
209407 }
209408 }
209409
209410 /* Find number of dimensions in the rtree table. */
209411 pStmt = rtreeCheckPrepare(pCheck: &check, zFmt: "SELECT * FROM %Q.%Q", zDb, zTab);
209412 if( pStmt ){
209413 int rc;
209414 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
209415 if( check.nDim<1 ){
209416 rtreeCheckAppendMsg(pCheck: &check, zFmt: "Schema corrupt or not an rtree");
209417 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
209418 check.bInt = (sqlite3_column_type(pStmt, i: 1)==SQLITE_INTEGER);
209419 }
209420 rc = sqlite3_finalize(pStmt);
209421 if( rc!=SQLITE_CORRUPT ) check.rc = rc;
209422 }
209423
209424 /* Do the actual integrity-check */
209425 if( check.nDim>=1 ){
209426 if( check.rc==SQLITE_OK ){
209427 rtreeCheckNode(pCheck: &check, iDepth: 0, aParent: 0, iNode: 1);
209428 }
209429 rtreeCheckCount(pCheck: &check, zTbl: "_rowid", nExpect: check.nLeaf);
209430 rtreeCheckCount(pCheck: &check, zTbl: "_parent", nExpect: check.nNonLeaf);
209431 }
209432
209433 /* Finalize SQL statements used by the integrity-check */
209434 sqlite3_finalize(pStmt: check.pGetNode);
209435 sqlite3_finalize(pStmt: check.aCheckMapping[0]);
209436 sqlite3_finalize(pStmt: check.aCheckMapping[1]);
209437
209438 /* If one was opened, close the transaction */
209439 if( bEnd ){
209440 int rc = sqlite3_exec(db, zSql: "END", xCallback: 0, pArg: 0, pzErrMsg: 0);
209441 if( check.rc==SQLITE_OK ) check.rc = rc;
209442 }
209443 *pzReport = check.zReport;
209444 return check.rc;
209445}
209446
209447/*
209448** Usage:
209449**
209450** rtreecheck(<rtree-table>);
209451** rtreecheck(<database>, <rtree-table>);
209452**
209453** Invoking this SQL function runs an integrity-check on the named rtree
209454** table. The integrity-check verifies the following:
209455**
209456** 1. For each cell in the r-tree structure (%_node table), that:
209457**
209458** a) for each dimension, (coord1 <= coord2).
209459**
209460** b) unless the cell is on the root node, that the cell is bounded
209461** by the parent cell on the parent node.
209462**
209463** c) for leaf nodes, that there is an entry in the %_rowid
209464** table corresponding to the cell's rowid value that
209465** points to the correct node.
209466**
209467** d) for cells on non-leaf nodes, that there is an entry in the
209468** %_parent table mapping from the cell's child node to the
209469** node that it resides on.
209470**
209471** 2. That there are the same number of entries in the %_rowid table
209472** as there are leaf cells in the r-tree structure, and that there
209473** is a leaf cell that corresponds to each entry in the %_rowid table.
209474**
209475** 3. That there are the same number of entries in the %_parent table
209476** as there are non-leaf cells in the r-tree structure, and that
209477** there is a non-leaf cell that corresponds to each entry in the
209478** %_parent table.
209479*/
209480static void rtreecheck(
209481 sqlite3_context *ctx,
209482 int nArg,
209483 sqlite3_value **apArg
209484){
209485 if( nArg!=1 && nArg!=2 ){
209486 sqlite3_result_error(pCtx: ctx,
209487 z: "wrong number of arguments to function rtreecheck()", n: -1
209488 );
209489 }else{
209490 int rc;
209491 char *zReport = 0;
209492 const char *zDb = (const char*)sqlite3_value_text(pVal: apArg[0]);
209493 const char *zTab;
209494 if( nArg==1 ){
209495 zTab = zDb;
209496 zDb = "main";
209497 }else{
209498 zTab = (const char*)sqlite3_value_text(pVal: apArg[1]);
209499 }
209500 rc = rtreeCheckTable(db: sqlite3_context_db_handle(p: ctx), zDb, zTab, pzReport: &zReport);
209501 if( rc==SQLITE_OK ){
209502 sqlite3_result_text(pCtx: ctx, z: zReport ? zReport : "ok", n: -1, SQLITE_TRANSIENT);
209503 }else{
209504 sqlite3_result_error_code(pCtx: ctx, errCode: rc);
209505 }
209506 sqlite3_free(p: zReport);
209507 }
209508}
209509
209510/* Conditionally include the geopoly code */
209511#ifdef SQLITE_ENABLE_GEOPOLY
209512/************** Include geopoly.c in the middle of rtree.c *******************/
209513/************** Begin file geopoly.c *****************************************/
209514/*
209515** 2018-05-25
209516**
209517** The author disclaims copyright to this source code. In place of
209518** a legal notice, here is a blessing:
209519**
209520** May you do good and not evil.
209521** May you find forgiveness for yourself and forgive others.
209522** May you share freely, never taking more than you give.
209523**
209524******************************************************************************
209525**
209526** This file implements an alternative R-Tree virtual table that
209527** uses polygons to express the boundaries of 2-dimensional objects.
209528**
209529** This file is #include-ed onto the end of "rtree.c" so that it has
209530** access to all of the R-Tree internals.
209531*/
209532/* #include <stdlib.h> */
209533
209534/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
209535#ifdef GEOPOLY_ENABLE_DEBUG
209536 static int geo_debug = 0;
209537# define GEODEBUG(X) if(geo_debug)printf X
209538#else
209539# define GEODEBUG(X)
209540#endif
209541
209542/* Character class routines */
209543#ifdef sqlite3Isdigit
209544 /* Use the SQLite core versions if this routine is part of the
209545 ** SQLite amalgamation */
209546# define safe_isdigit(x) sqlite3Isdigit(x)
209547# define safe_isalnum(x) sqlite3Isalnum(x)
209548# define safe_isxdigit(x) sqlite3Isxdigit(x)
209549#else
209550 /* Use the standard library for separate compilation */
209551#include <ctype.h> /* amalgamator: keep */
209552# define safe_isdigit(x) isdigit((unsigned char)(x))
209553# define safe_isalnum(x) isalnum((unsigned char)(x))
209554# define safe_isxdigit(x) isxdigit((unsigned char)(x))
209555#endif
209556
209557#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
209558/*
209559** Growing our own isspace() routine this way is twice as fast as
209560** the library isspace() function.
209561*/
209562static const char geopolyIsSpace[] = {
209563 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
209564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209565 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209566 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209567 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209568 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209570 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209571 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209572 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209573 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209574 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209575 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209576 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209579};
209580#define fast_isspace(x) (geopolyIsSpace[(unsigned char)x])
209581#endif /* JSON NULL - back to original code */
209582
209583/* Compiler and version */
209584#ifndef GCC_VERSION
209585#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
209586# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
209587#else
209588# define GCC_VERSION 0
209589#endif
209590#endif
209591#ifndef MSVC_VERSION
209592#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
209593# define MSVC_VERSION _MSC_VER
209594#else
209595# define MSVC_VERSION 0
209596#endif
209597#endif
209598
209599/* Datatype for coordinates
209600*/
209601typedef float GeoCoord;
209602
209603/*
209604** Internal representation of a polygon.
209605**
209606** The polygon consists of a sequence of vertexes. There is a line
209607** segment between each pair of vertexes, and one final segment from
209608** the last vertex back to the first. (This differs from the GeoJSON
209609** standard in which the final vertex is a repeat of the first.)
209610**
209611** The polygon follows the right-hand rule. The area to the right of
209612** each segment is "outside" and the area to the left is "inside".
209613**
209614** The on-disk representation consists of a 4-byte header followed by
209615** the values. The 4-byte header is:
209616**
209617** encoding (1 byte) 0=big-endian, 1=little-endian
209618** nvertex (3 bytes) Number of vertexes as a big-endian integer
209619**
209620** Enough space is allocated for 4 coordinates, to work around over-zealous
209621** warnings coming from some compiler (notably, clang). In reality, the size
209622** of each GeoPoly memory allocate is adjusted as necessary so that the
209623** GeoPoly.a[] array at the end is the appropriate size.
209624*/
209625typedef struct GeoPoly GeoPoly;
209626struct GeoPoly {
209627 int nVertex; /* Number of vertexes */
209628 unsigned char hdr[4]; /* Header for on-disk representation */
209629 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
209630};
209631
209632/* The size of a memory allocation needed for a GeoPoly object sufficient
209633** to hold N coordinate pairs.
209634*/
209635#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
209636
209637/* Macros to access coordinates of a GeoPoly.
209638** We have to use these macros, rather than just say p->a[i] in order
209639** to silence (incorrect) UBSAN warnings if the array index is too large.
209640*/
209641#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
209642#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
209643
209644
209645/*
209646** State of a parse of a GeoJSON input.
209647*/
209648typedef struct GeoParse GeoParse;
209649struct GeoParse {
209650 const unsigned char *z; /* Unparsed input */
209651 int nVertex; /* Number of vertexes in a[] */
209652 int nAlloc; /* Space allocated to a[] */
209653 int nErr; /* Number of errors encountered */
209654 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
209655};
209656
209657/* Do a 4-byte byte swap */
209658static void geopolySwab32(unsigned char *a){
209659 unsigned char t = a[0];
209660 a[0] = a[3];
209661 a[3] = t;
209662 t = a[1];
209663 a[1] = a[2];
209664 a[2] = t;
209665}
209666
209667/* Skip whitespace. Return the next non-whitespace character. */
209668static char geopolySkipSpace(GeoParse *p){
209669 while( fast_isspace(p->z[0]) ) p->z++;
209670 return p->z[0];
209671}
209672
209673/* Parse out a number. Write the value into *pVal if pVal!=0.
209674** return non-zero on success and zero if the next token is not a number.
209675*/
209676static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
209677 char c = geopolySkipSpace(p);
209678 const unsigned char *z = p->z;
209679 int j = 0;
209680 int seenDP = 0;
209681 int seenE = 0;
209682 if( c=='-' ){
209683 j = 1;
209684 c = z[j];
209685 }
209686 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
209687 for(;; j++){
209688 c = z[j];
209689 if( safe_isdigit(c) ) continue;
209690 if( c=='.' ){
209691 if( z[j-1]=='-' ) return 0;
209692 if( seenDP ) return 0;
209693 seenDP = 1;
209694 continue;
209695 }
209696 if( c=='e' || c=='E' ){
209697 if( z[j-1]<'0' ) return 0;
209698 if( seenE ) return -1;
209699 seenDP = seenE = 1;
209700 c = z[j+1];
209701 if( c=='+' || c=='-' ){
209702 j++;
209703 c = z[j+1];
209704 }
209705 if( c<'0' || c>'9' ) return 0;
209706 continue;
209707 }
209708 break;
209709 }
209710 if( z[j-1]<'0' ) return 0;
209711 if( pVal ){
209712#ifdef SQLITE_AMALGAMATION
209713 /* The sqlite3AtoF() routine is much much faster than atof(), if it
209714 ** is available */
209715 double r;
209716 (void)sqlite3AtoF(z: (const char*)p->z, pResult: &r, length: j, SQLITE_UTF8);
209717 *pVal = r;
209718#else
209719 *pVal = (GeoCoord)atof((const char*)p->z);
209720#endif
209721 }
209722 p->z += j;
209723 return 1;
209724}
209725
209726/*
209727** If the input is a well-formed JSON array of coordinates with at least
209728** four coordinates and where each coordinate is itself a two-value array,
209729** then convert the JSON into a GeoPoly object and return a pointer to
209730** that object.
209731**
209732** If any error occurs, return NULL.
209733*/
209734static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
209735 GeoParse s;
209736 int rc = SQLITE_OK;
209737 memset(s: &s, c: 0, n: sizeof(s));
209738 s.z = z;
209739 if( geopolySkipSpace(p: &s)=='[' ){
209740 s.z++;
209741 while( geopolySkipSpace(p: &s)=='[' ){
209742 int ii = 0;
209743 char c;
209744 s.z++;
209745 if( s.nVertex>=s.nAlloc ){
209746 GeoCoord *aNew;
209747 s.nAlloc = s.nAlloc*2 + 16;
209748 aNew = sqlite3_realloc64(pOld: s.a, n: s.nAlloc*sizeof(GeoCoord)*2 );
209749 if( aNew==0 ){
209750 rc = SQLITE_NOMEM;
209751 s.nErr++;
209752 break;
209753 }
209754 s.a = aNew;
209755 }
209756 while( geopolyParseNumber(p: &s, pVal: ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
209757 ii++;
209758 if( ii==2 ) s.nVertex++;
209759 c = geopolySkipSpace(p: &s);
209760 s.z++;
209761 if( c==',' ) continue;
209762 if( c==']' && ii>=2 ) break;
209763 s.nErr++;
209764 rc = SQLITE_ERROR;
209765 goto parse_json_err;
209766 }
209767 if( geopolySkipSpace(p: &s)==',' ){
209768 s.z++;
209769 continue;
209770 }
209771 break;
209772 }
209773 if( geopolySkipSpace(p: &s)==']'
209774 && s.nVertex>=4
209775 && s.a[0]==s.a[s.nVertex*2-2]
209776 && s.a[1]==s.a[s.nVertex*2-1]
209777 && (s.z++, geopolySkipSpace(p: &s)==0)
209778 ){
209779 GeoPoly *pOut;
209780 int x = 1;
209781 s.nVertex--; /* Remove the redundant vertex at the end */
209782 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
209783 x = 1;
209784 if( pOut==0 ) goto parse_json_err;
209785 pOut->nVertex = s.nVertex;
209786 memcpy(dest: pOut->a, src: s.a, n: s.nVertex*2*sizeof(GeoCoord));
209787 pOut->hdr[0] = *(unsigned char*)&x;
209788 pOut->hdr[1] = (s.nVertex>>16)&0xff;
209789 pOut->hdr[2] = (s.nVertex>>8)&0xff;
209790 pOut->hdr[3] = s.nVertex&0xff;
209791 sqlite3_free(p: s.a);
209792 if( pRc ) *pRc = SQLITE_OK;
209793 return pOut;
209794 }else{
209795 s.nErr++;
209796 rc = SQLITE_ERROR;
209797 }
209798 }
209799parse_json_err:
209800 if( pRc ) *pRc = rc;
209801 sqlite3_free(p: s.a);
209802 return 0;
209803}
209804
209805/*
209806** Given a function parameter, try to interpret it as a polygon, either
209807** in the binary format or JSON text. Compute a GeoPoly object and
209808** return a pointer to that object. Or if the input is not a well-formed
209809** polygon, put an error message in sqlite3_context and return NULL.
209810*/
209811static GeoPoly *geopolyFuncParam(
209812 sqlite3_context *pCtx, /* Context for error messages */
209813 sqlite3_value *pVal, /* The value to decode */
209814 int *pRc /* Write error here */
209815){
209816 GeoPoly *p = 0;
209817 int nByte;
209818 testcase( pCtx==0 );
209819 if( sqlite3_value_type(pVal)==SQLITE_BLOB
209820 && (nByte = sqlite3_value_bytes(pVal))>=(int)(4+6*sizeof(GeoCoord))
209821 ){
209822 const unsigned char *a = sqlite3_value_blob(pVal);
209823 int nVertex;
209824 if( a==0 ){
209825 if( pCtx ) sqlite3_result_error_nomem(pCtx);
209826 return 0;
209827 }
209828 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
209829 if( (a[0]==0 || a[0]==1)
209830 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
209831 ){
209832 p = sqlite3_malloc64( n: sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
209833 if( p==0 ){
209834 if( pRc ) *pRc = SQLITE_NOMEM;
209835 if( pCtx ) sqlite3_result_error_nomem(pCtx);
209836 }else{
209837 int x = 1;
209838 p->nVertex = nVertex;
209839 memcpy(dest: p->hdr, src: a, n: nByte);
209840 if( a[0] != *(unsigned char*)&x ){
209841 int ii;
209842 for(ii=0; ii<nVertex; ii++){
209843 geopolySwab32(a: (unsigned char*)&GeoX(p,ii));
209844 geopolySwab32(a: (unsigned char*)&GeoY(p,ii));
209845 }
209846 p->hdr[0] ^= 1;
209847 }
209848 }
209849 }
209850 if( pRc ) *pRc = SQLITE_OK;
209851 return p;
209852 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
209853 const unsigned char *zJson = sqlite3_value_text(pVal);
209854 if( zJson==0 ){
209855 if( pRc ) *pRc = SQLITE_NOMEM;
209856 return 0;
209857 }
209858 return geopolyParseJson(z: zJson, pRc);
209859 }else{
209860 if( pRc ) *pRc = SQLITE_ERROR;
209861 return 0;
209862 }
209863}
209864
209865/*
209866** Implementation of the geopoly_blob(X) function.
209867**
209868** If the input is a well-formed Geopoly BLOB or JSON string
209869** then return the BLOB representation of the polygon. Otherwise
209870** return NULL.
209871*/
209872static void geopolyBlobFunc(
209873 sqlite3_context *context,
209874 int argc,
209875 sqlite3_value **argv
209876){
209877 GeoPoly *p = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
209878 (void)argc;
209879 if( p ){
209880 sqlite3_result_blob(pCtx: context, z: p->hdr,
209881 n: 4+8*p->nVertex, SQLITE_TRANSIENT);
209882 sqlite3_free(p);
209883 }
209884}
209885
209886/*
209887** SQL function: geopoly_json(X)
209888**
209889** Interpret X as a polygon and render it as a JSON array
209890** of coordinates. Or, if X is not a valid polygon, return NULL.
209891*/
209892static void geopolyJsonFunc(
209893 sqlite3_context *context,
209894 int argc,
209895 sqlite3_value **argv
209896){
209897 GeoPoly *p = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
209898 (void)argc;
209899 if( p ){
209900 sqlite3 *db = sqlite3_context_db_handle(p: context);
209901 sqlite3_str *x = sqlite3_str_new(db);
209902 int i;
209903 sqlite3_str_append(p: x, z: "[", N: 1);
209904 for(i=0; i<p->nVertex; i++){
209905 sqlite3_str_appendf(p: x, zFormat: "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
209906 }
209907 sqlite3_str_appendf(p: x, zFormat: "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
209908 sqlite3_result_text(pCtx: context, z: sqlite3_str_finish(p: x), n: -1, xDel: sqlite3_free);
209909 sqlite3_free(p);
209910 }
209911}
209912
209913/*
209914** SQL function: geopoly_svg(X, ....)
209915**
209916** Interpret X as a polygon and render it as a SVG <polyline>.
209917** Additional arguments are added as attributes to the <polyline>.
209918*/
209919static void geopolySvgFunc(
209920 sqlite3_context *context,
209921 int argc,
209922 sqlite3_value **argv
209923){
209924 GeoPoly *p;
209925 if( argc<1 ) return;
209926 p = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
209927 if( p ){
209928 sqlite3 *db = sqlite3_context_db_handle(p: context);
209929 sqlite3_str *x = sqlite3_str_new(db);
209930 int i;
209931 char cSep = '\'';
209932 sqlite3_str_appendf(p: x, zFormat: "<polyline points=");
209933 for(i=0; i<p->nVertex; i++){
209934 sqlite3_str_appendf(p: x, zFormat: "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
209935 cSep = ' ';
209936 }
209937 sqlite3_str_appendf(p: x, zFormat: " %g,%g'", GeoX(p,0), GeoY(p,0));
209938 for(i=1; i<argc; i++){
209939 const char *z = (const char*)sqlite3_value_text(pVal: argv[i]);
209940 if( z && z[0] ){
209941 sqlite3_str_appendf(p: x, zFormat: " %s", z);
209942 }
209943 }
209944 sqlite3_str_appendf(p: x, zFormat: "></polyline>");
209945 sqlite3_result_text(pCtx: context, z: sqlite3_str_finish(p: x), n: -1, xDel: sqlite3_free);
209946 sqlite3_free(p);
209947 }
209948}
209949
209950/*
209951** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
209952**
209953** Transform and/or translate a polygon as follows:
209954**
209955** x1 = A*x0 + B*y0 + E
209956** y1 = C*x0 + D*y0 + F
209957**
209958** For a translation:
209959**
209960** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
209961**
209962** Rotate by R around the point (0,0):
209963**
209964** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
209965*/
209966static void geopolyXformFunc(
209967 sqlite3_context *context,
209968 int argc,
209969 sqlite3_value **argv
209970){
209971 GeoPoly *p = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
209972 double A = sqlite3_value_double(pVal: argv[1]);
209973 double B = sqlite3_value_double(pVal: argv[2]);
209974 double C = sqlite3_value_double(pVal: argv[3]);
209975 double D = sqlite3_value_double(pVal: argv[4]);
209976 double E = sqlite3_value_double(pVal: argv[5]);
209977 double F = sqlite3_value_double(pVal: argv[6]);
209978 GeoCoord x1, y1, x0, y0;
209979 int ii;
209980 (void)argc;
209981 if( p ){
209982 for(ii=0; ii<p->nVertex; ii++){
209983 x0 = GeoX(p,ii);
209984 y0 = GeoY(p,ii);
209985 x1 = (GeoCoord)(A*x0 + B*y0 + E);
209986 y1 = (GeoCoord)(C*x0 + D*y0 + F);
209987 GeoX(p,ii) = x1;
209988 GeoY(p,ii) = y1;
209989 }
209990 sqlite3_result_blob(pCtx: context, z: p->hdr,
209991 n: 4+8*p->nVertex, SQLITE_TRANSIENT);
209992 sqlite3_free(p);
209993 }
209994}
209995
209996/*
209997** Compute the area enclosed by the polygon.
209998**
209999** This routine can also be used to detect polygons that rotate in
210000** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
210001** This routine returns a negative value for clockwise (CW) polygons.
210002*/
210003static double geopolyArea(GeoPoly *p){
210004 double rArea = 0.0;
210005 int ii;
210006 for(ii=0; ii<p->nVertex-1; ii++){
210007 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
210008 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
210009 * 0.5;
210010 }
210011 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
210012 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
210013 * 0.5;
210014 return rArea;
210015}
210016
210017/*
210018** Implementation of the geopoly_area(X) function.
210019**
210020** If the input is a well-formed Geopoly BLOB then return the area
210021** enclosed by the polygon. If the polygon circulates clockwise instead
210022** of counterclockwise (as it should) then return the negative of the
210023** enclosed area. Otherwise return NULL.
210024*/
210025static void geopolyAreaFunc(
210026 sqlite3_context *context,
210027 int argc,
210028 sqlite3_value **argv
210029){
210030 GeoPoly *p = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
210031 (void)argc;
210032 if( p ){
210033 sqlite3_result_double(pCtx: context, rVal: geopolyArea(p));
210034 sqlite3_free(p);
210035 }
210036}
210037
210038/*
210039** Implementation of the geopoly_ccw(X) function.
210040**
210041** If the rotation of polygon X is clockwise (incorrect) instead of
210042** counter-clockwise (the correct winding order according to RFC7946)
210043** then reverse the order of the vertexes in polygon X.
210044**
210045** In other words, this routine returns a CCW polygon regardless of the
210046** winding order of its input.
210047**
210048** Use this routine to sanitize historical inputs that that sometimes
210049** contain polygons that wind in the wrong direction.
210050*/
210051static void geopolyCcwFunc(
210052 sqlite3_context *context,
210053 int argc,
210054 sqlite3_value **argv
210055){
210056 GeoPoly *p = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
210057 (void)argc;
210058 if( p ){
210059 if( geopolyArea(p)<0.0 ){
210060 int ii, jj;
210061 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
210062 GeoCoord t = GeoX(p,ii);
210063 GeoX(p,ii) = GeoX(p,jj);
210064 GeoX(p,jj) = t;
210065 t = GeoY(p,ii);
210066 GeoY(p,ii) = GeoY(p,jj);
210067 GeoY(p,jj) = t;
210068 }
210069 }
210070 sqlite3_result_blob(pCtx: context, z: p->hdr,
210071 n: 4+8*p->nVertex, SQLITE_TRANSIENT);
210072 sqlite3_free(p);
210073 }
210074}
210075
210076#define GEOPOLY_PI 3.1415926535897932385
210077
210078/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
210079*/
210080static double geopolySine(double r){
210081 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
210082 if( r>=1.5*GEOPOLY_PI ){
210083 r -= 2.0*GEOPOLY_PI;
210084 }
210085 if( r>=0.5*GEOPOLY_PI ){
210086 return -geopolySine(r: r-GEOPOLY_PI);
210087 }else{
210088 double r2 = r*r;
210089 double r3 = r2*r;
210090 double r5 = r3*r2;
210091 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
210092 }
210093}
210094
210095/*
210096** Function: geopoly_regular(X,Y,R,N)
210097**
210098** Construct a simple, convex, regular polygon centered at X, Y
210099** with circumradius R and with N sides.
210100*/
210101static void geopolyRegularFunc(
210102 sqlite3_context *context,
210103 int argc,
210104 sqlite3_value **argv
210105){
210106 double x = sqlite3_value_double(pVal: argv[0]);
210107 double y = sqlite3_value_double(pVal: argv[1]);
210108 double r = sqlite3_value_double(pVal: argv[2]);
210109 int n = sqlite3_value_int(pVal: argv[3]);
210110 int i;
210111 GeoPoly *p;
210112 (void)argc;
210113
210114 if( n<3 || r<=0.0 ) return;
210115 if( n>1000 ) n = 1000;
210116 p = sqlite3_malloc64( n: sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
210117 if( p==0 ){
210118 sqlite3_result_error_nomem(pCtx: context);
210119 return;
210120 }
210121 i = 1;
210122 p->hdr[0] = *(unsigned char*)&i;
210123 p->hdr[1] = 0;
210124 p->hdr[2] = (n>>8)&0xff;
210125 p->hdr[3] = n&0xff;
210126 for(i=0; i<n; i++){
210127 double rAngle = 2.0*GEOPOLY_PI*i/n;
210128 GeoX(p,i) = x - r*geopolySine(r: rAngle-0.5*GEOPOLY_PI);
210129 GeoY(p,i) = y + r*geopolySine(r: rAngle);
210130 }
210131 sqlite3_result_blob(pCtx: context, z: p->hdr, n: 4+8*n, SQLITE_TRANSIENT);
210132 sqlite3_free(p);
210133}
210134
210135/*
210136** If pPoly is a polygon, compute its bounding box. Then:
210137**
210138** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
210139** (2) otherwise, compute a GeoPoly for the bounding box and return the
210140** new GeoPoly
210141**
210142** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
210143** the bounding box in aCoord and return a pointer to that GeoPoly.
210144*/
210145static GeoPoly *geopolyBBox(
210146 sqlite3_context *context, /* For recording the error */
210147 sqlite3_value *pPoly, /* The polygon */
210148 RtreeCoord *aCoord, /* Results here */
210149 int *pRc /* Error code here */
210150){
210151 GeoPoly *pOut = 0;
210152 GeoPoly *p;
210153 float mnX, mxX, mnY, mxY;
210154 if( pPoly==0 && aCoord!=0 ){
210155 p = 0;
210156 mnX = aCoord[0].f;
210157 mxX = aCoord[1].f;
210158 mnY = aCoord[2].f;
210159 mxY = aCoord[3].f;
210160 goto geopolyBboxFill;
210161 }else{
210162 p = geopolyFuncParam(pCtx: context, pVal: pPoly, pRc);
210163 }
210164 if( p ){
210165 int ii;
210166 mnX = mxX = GeoX(p,0);
210167 mnY = mxY = GeoY(p,0);
210168 for(ii=1; ii<p->nVertex; ii++){
210169 double r = GeoX(p,ii);
210170 if( r<mnX ) mnX = (float)r;
210171 else if( r>mxX ) mxX = (float)r;
210172 r = GeoY(p,ii);
210173 if( r<mnY ) mnY = (float)r;
210174 else if( r>mxY ) mxY = (float)r;
210175 }
210176 if( pRc ) *pRc = SQLITE_OK;
210177 if( aCoord==0 ){
210178 geopolyBboxFill:
210179 pOut = sqlite3_realloc64(pOld: p, GEOPOLY_SZ(4));
210180 if( pOut==0 ){
210181 sqlite3_free(p);
210182 if( context ) sqlite3_result_error_nomem(pCtx: context);
210183 if( pRc ) *pRc = SQLITE_NOMEM;
210184 return 0;
210185 }
210186 pOut->nVertex = 4;
210187 ii = 1;
210188 pOut->hdr[0] = *(unsigned char*)&ii;
210189 pOut->hdr[1] = 0;
210190 pOut->hdr[2] = 0;
210191 pOut->hdr[3] = 4;
210192 GeoX(pOut,0) = mnX;
210193 GeoY(pOut,0) = mnY;
210194 GeoX(pOut,1) = mxX;
210195 GeoY(pOut,1) = mnY;
210196 GeoX(pOut,2) = mxX;
210197 GeoY(pOut,2) = mxY;
210198 GeoX(pOut,3) = mnX;
210199 GeoY(pOut,3) = mxY;
210200 }else{
210201 sqlite3_free(p);
210202 aCoord[0].f = mnX;
210203 aCoord[1].f = mxX;
210204 aCoord[2].f = mnY;
210205 aCoord[3].f = mxY;
210206 }
210207 }else if( aCoord ){
210208 memset(s: aCoord, c: 0, n: sizeof(RtreeCoord)*4);
210209 }
210210 return pOut;
210211}
210212
210213/*
210214** Implementation of the geopoly_bbox(X) SQL function.
210215*/
210216static void geopolyBBoxFunc(
210217 sqlite3_context *context,
210218 int argc,
210219 sqlite3_value **argv
210220){
210221 GeoPoly *p = geopolyBBox(context, pPoly: argv[0], aCoord: 0, pRc: 0);
210222 (void)argc;
210223 if( p ){
210224 sqlite3_result_blob(pCtx: context, z: p->hdr,
210225 n: 4+8*p->nVertex, SQLITE_TRANSIENT);
210226 sqlite3_free(p);
210227 }
210228}
210229
210230/*
210231** State vector for the geopoly_group_bbox() aggregate function.
210232*/
210233typedef struct GeoBBox GeoBBox;
210234struct GeoBBox {
210235 int isInit;
210236 RtreeCoord a[4];
210237};
210238
210239
210240/*
210241** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
210242*/
210243static void geopolyBBoxStep(
210244 sqlite3_context *context,
210245 int argc,
210246 sqlite3_value **argv
210247){
210248 RtreeCoord a[4];
210249 int rc = SQLITE_OK;
210250 (void)argc;
210251 (void)geopolyBBox(context, pPoly: argv[0], aCoord: a, pRc: &rc);
210252 if( rc==SQLITE_OK ){
210253 GeoBBox *pBBox;
210254 pBBox = (GeoBBox*)sqlite3_aggregate_context(p: context, nByte: sizeof(*pBBox));
210255 if( pBBox==0 ) return;
210256 if( pBBox->isInit==0 ){
210257 pBBox->isInit = 1;
210258 memcpy(dest: pBBox->a, src: a, n: sizeof(RtreeCoord)*4);
210259 }else{
210260 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
210261 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
210262 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
210263 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
210264 }
210265 }
210266}
210267static void geopolyBBoxFinal(
210268 sqlite3_context *context
210269){
210270 GeoPoly *p;
210271 GeoBBox *pBBox;
210272 pBBox = (GeoBBox*)sqlite3_aggregate_context(p: context, nByte: 0);
210273 if( pBBox==0 ) return;
210274 p = geopolyBBox(context, pPoly: 0, aCoord: pBBox->a, pRc: 0);
210275 if( p ){
210276 sqlite3_result_blob(pCtx: context, z: p->hdr,
210277 n: 4+8*p->nVertex, SQLITE_TRANSIENT);
210278 sqlite3_free(p);
210279 }
210280}
210281
210282
210283/*
210284** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
210285** Returns:
210286**
210287** +2 x0,y0 is on the line segement
210288**
210289** +1 x0,y0 is beneath line segment
210290**
210291** 0 x0,y0 is not on or beneath the line segment or the line segment
210292** is vertical and x0,y0 is not on the line segment
210293**
210294** The left-most coordinate min(x1,x2) is not considered to be part of
210295** the line segment for the purposes of this analysis.
210296*/
210297static int pointBeneathLine(
210298 double x0, double y0,
210299 double x1, double y1,
210300 double x2, double y2
210301){
210302 double y;
210303 if( x0==x1 && y0==y1 ) return 2;
210304 if( x1<x2 ){
210305 if( x0<=x1 || x0>x2 ) return 0;
210306 }else if( x1>x2 ){
210307 if( x0<=x2 || x0>x1 ) return 0;
210308 }else{
210309 /* Vertical line segment */
210310 if( x0!=x1 ) return 0;
210311 if( y0<y1 && y0<y2 ) return 0;
210312 if( y0>y1 && y0>y2 ) return 0;
210313 return 2;
210314 }
210315 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
210316 if( y0==y ) return 2;
210317 if( y0<y ) return 1;
210318 return 0;
210319}
210320
210321/*
210322** SQL function: geopoly_contains_point(P,X,Y)
210323**
210324** Return +2 if point X,Y is within polygon P.
210325** Return +1 if point X,Y is on the polygon boundary.
210326** Return 0 if point X,Y is outside the polygon
210327*/
210328static void geopolyContainsPointFunc(
210329 sqlite3_context *context,
210330 int argc,
210331 sqlite3_value **argv
210332){
210333 GeoPoly *p1 = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
210334 double x0 = sqlite3_value_double(pVal: argv[1]);
210335 double y0 = sqlite3_value_double(pVal: argv[2]);
210336 int v = 0;
210337 int cnt = 0;
210338 int ii;
210339 (void)argc;
210340
210341 if( p1==0 ) return;
210342 for(ii=0; ii<p1->nVertex-1; ii++){
210343 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
210344 GeoX(p1,ii+1),GeoY(p1,ii+1));
210345 if( v==2 ) break;
210346 cnt += v;
210347 }
210348 if( v!=2 ){
210349 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
210350 GeoX(p1,0), GeoY(p1,0));
210351 }
210352 if( v==2 ){
210353 sqlite3_result_int(pCtx: context, iVal: 1);
210354 }else if( ((v+cnt)&1)==0 ){
210355 sqlite3_result_int(pCtx: context, iVal: 0);
210356 }else{
210357 sqlite3_result_int(pCtx: context, iVal: 2);
210358 }
210359 sqlite3_free(p: p1);
210360}
210361
210362/* Forward declaration */
210363static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
210364
210365/*
210366** SQL function: geopoly_within(P1,P2)
210367**
210368** Return +2 if P1 and P2 are the same polygon
210369** Return +1 if P2 is contained within P1
210370** Return 0 if any part of P2 is on the outside of P1
210371**
210372*/
210373static void geopolyWithinFunc(
210374 sqlite3_context *context,
210375 int argc,
210376 sqlite3_value **argv
210377){
210378 GeoPoly *p1 = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
210379 GeoPoly *p2 = geopolyFuncParam(pCtx: context, pVal: argv[1], pRc: 0);
210380 (void)argc;
210381 if( p1 && p2 ){
210382 int x = geopolyOverlap(p1, p2);
210383 if( x<0 ){
210384 sqlite3_result_error_nomem(pCtx: context);
210385 }else{
210386 sqlite3_result_int(pCtx: context, iVal: x==2 ? 1 : x==4 ? 2 : 0);
210387 }
210388 }
210389 sqlite3_free(p: p1);
210390 sqlite3_free(p: p2);
210391}
210392
210393/* Objects used by the overlap algorihm. */
210394typedef struct GeoEvent GeoEvent;
210395typedef struct GeoSegment GeoSegment;
210396typedef struct GeoOverlap GeoOverlap;
210397struct GeoEvent {
210398 double x; /* X coordinate at which event occurs */
210399 int eType; /* 0 for ADD, 1 for REMOVE */
210400 GeoSegment *pSeg; /* The segment to be added or removed */
210401 GeoEvent *pNext; /* Next event in the sorted list */
210402};
210403struct GeoSegment {
210404 double C, B; /* y = C*x + B */
210405 double y; /* Current y value */
210406 float y0; /* Initial y value */
210407 unsigned char side; /* 1 for p1, 2 for p2 */
210408 unsigned int idx; /* Which segment within the side */
210409 GeoSegment *pNext; /* Next segment in a list sorted by y */
210410};
210411struct GeoOverlap {
210412 GeoEvent *aEvent; /* Array of all events */
210413 GeoSegment *aSegment; /* Array of all segments */
210414 int nEvent; /* Number of events */
210415 int nSegment; /* Number of segments */
210416};
210417
210418/*
210419** Add a single segment and its associated events.
210420*/
210421static void geopolyAddOneSegment(
210422 GeoOverlap *p,
210423 GeoCoord x0,
210424 GeoCoord y0,
210425 GeoCoord x1,
210426 GeoCoord y1,
210427 unsigned char side,
210428 unsigned int idx
210429){
210430 GeoSegment *pSeg;
210431 GeoEvent *pEvent;
210432 if( x0==x1 ) return; /* Ignore vertical segments */
210433 if( x0>x1 ){
210434 GeoCoord t = x0;
210435 x0 = x1;
210436 x1 = t;
210437 t = y0;
210438 y0 = y1;
210439 y1 = t;
210440 }
210441 pSeg = p->aSegment + p->nSegment;
210442 p->nSegment++;
210443 pSeg->C = (y1-y0)/(x1-x0);
210444 pSeg->B = y1 - x1*pSeg->C;
210445 pSeg->y0 = y0;
210446 pSeg->side = side;
210447 pSeg->idx = idx;
210448 pEvent = p->aEvent + p->nEvent;
210449 p->nEvent++;
210450 pEvent->x = x0;
210451 pEvent->eType = 0;
210452 pEvent->pSeg = pSeg;
210453 pEvent = p->aEvent + p->nEvent;
210454 p->nEvent++;
210455 pEvent->x = x1;
210456 pEvent->eType = 1;
210457 pEvent->pSeg = pSeg;
210458}
210459
210460
210461
210462/*
210463** Insert all segments and events for polygon pPoly.
210464*/
210465static void geopolyAddSegments(
210466 GeoOverlap *p, /* Add segments to this Overlap object */
210467 GeoPoly *pPoly, /* Take all segments from this polygon */
210468 unsigned char side /* The side of pPoly */
210469){
210470 unsigned int i;
210471 GeoCoord *x;
210472 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
210473 x = &GeoX(pPoly,i);
210474 geopolyAddOneSegment(p, x0: x[0], y0: x[1], x1: x[2], y1: x[3], side, idx: i);
210475 }
210476 x = &GeoX(pPoly,i);
210477 geopolyAddOneSegment(p, x0: x[0], y0: x[1], x1: pPoly->a[0], y1: pPoly->a[1], side, idx: i);
210478}
210479
210480/*
210481** Merge two lists of sorted events by X coordinate
210482*/
210483static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
210484 GeoEvent head, *pLast;
210485 head.pNext = 0;
210486 pLast = &head;
210487 while( pRight && pLeft ){
210488 if( pRight->x <= pLeft->x ){
210489 pLast->pNext = pRight;
210490 pLast = pRight;
210491 pRight = pRight->pNext;
210492 }else{
210493 pLast->pNext = pLeft;
210494 pLast = pLeft;
210495 pLeft = pLeft->pNext;
210496 }
210497 }
210498 pLast->pNext = pRight ? pRight : pLeft;
210499 return head.pNext;
210500}
210501
210502/*
210503** Sort an array of nEvent event objects into a list.
210504*/
210505static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
210506 int mx = 0;
210507 int i, j;
210508 GeoEvent *p;
210509 GeoEvent *a[50];
210510 for(i=0; i<nEvent; i++){
210511 p = &aEvent[i];
210512 p->pNext = 0;
210513 for(j=0; j<mx && a[j]; j++){
210514 p = geopolyEventMerge(pLeft: a[j], pRight: p);
210515 a[j] = 0;
210516 }
210517 a[j] = p;
210518 if( j>=mx ) mx = j+1;
210519 }
210520 p = 0;
210521 for(i=0; i<mx; i++){
210522 p = geopolyEventMerge(pLeft: a[i], pRight: p);
210523 }
210524 return p;
210525}
210526
210527/*
210528** Merge two lists of sorted segments by Y, and then by C.
210529*/
210530static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
210531 GeoSegment head, *pLast;
210532 head.pNext = 0;
210533 pLast = &head;
210534 while( pRight && pLeft ){
210535 double r = pRight->y - pLeft->y;
210536 if( r==0.0 ) r = pRight->C - pLeft->C;
210537 if( r<0.0 ){
210538 pLast->pNext = pRight;
210539 pLast = pRight;
210540 pRight = pRight->pNext;
210541 }else{
210542 pLast->pNext = pLeft;
210543 pLast = pLeft;
210544 pLeft = pLeft->pNext;
210545 }
210546 }
210547 pLast->pNext = pRight ? pRight : pLeft;
210548 return head.pNext;
210549}
210550
210551/*
210552** Sort a list of GeoSegments in order of increasing Y and in the event of
210553** a tie, increasing C (slope).
210554*/
210555static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
210556 int mx = 0;
210557 int i;
210558 GeoSegment *p;
210559 GeoSegment *a[50];
210560 while( pList ){
210561 p = pList;
210562 pList = pList->pNext;
210563 p->pNext = 0;
210564 for(i=0; i<mx && a[i]; i++){
210565 p = geopolySegmentMerge(pLeft: a[i], pRight: p);
210566 a[i] = 0;
210567 }
210568 a[i] = p;
210569 if( i>=mx ) mx = i+1;
210570 }
210571 p = 0;
210572 for(i=0; i<mx; i++){
210573 p = geopolySegmentMerge(pLeft: a[i], pRight: p);
210574 }
210575 return p;
210576}
210577
210578/*
210579** Determine the overlap between two polygons
210580*/
210581static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
210582 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
210583 GeoOverlap *p;
210584 sqlite3_int64 nByte;
210585 GeoEvent *pThisEvent;
210586 double rX;
210587 int rc = 0;
210588 int needSort = 0;
210589 GeoSegment *pActive = 0;
210590 GeoSegment *pSeg;
210591 unsigned char aOverlap[4];
210592
210593 nByte = sizeof(GeoEvent)*nVertex*2
210594 + sizeof(GeoSegment)*nVertex
210595 + sizeof(GeoOverlap);
210596 p = sqlite3_malloc64( n: nByte );
210597 if( p==0 ) return -1;
210598 p->aEvent = (GeoEvent*)&p[1];
210599 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
210600 p->nEvent = p->nSegment = 0;
210601 geopolyAddSegments(p, pPoly: p1, side: 1);
210602 geopolyAddSegments(p, pPoly: p2, side: 2);
210603 pThisEvent = geopolySortEventsByX(aEvent: p->aEvent, nEvent: p->nEvent);
210604 rX = pThisEvent && pThisEvent->x==0.0 ? -1.0 : 0.0;
210605 memset(s: aOverlap, c: 0, n: sizeof(aOverlap));
210606 while( pThisEvent ){
210607 if( pThisEvent->x!=rX ){
210608 GeoSegment *pPrev = 0;
210609 int iMask = 0;
210610 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
210611 rX = pThisEvent->x;
210612 if( needSort ){
210613 GEODEBUG(("SORT\n"));
210614 pActive = geopolySortSegmentsByYAndC(pList: pActive);
210615 needSort = 0;
210616 }
210617 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
210618 if( pPrev ){
210619 if( pPrev->y!=pSeg->y ){
210620 GEODEBUG(("MASK: %d\n", iMask));
210621 aOverlap[iMask] = 1;
210622 }
210623 }
210624 iMask ^= pSeg->side;
210625 pPrev = pSeg;
210626 }
210627 pPrev = 0;
210628 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
210629 double y = pSeg->C*rX + pSeg->B;
210630 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
210631 pSeg->y = y;
210632 if( pPrev ){
210633 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
210634 rc = 1;
210635 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
210636 pPrev->side, pPrev->idx,
210637 pSeg->side, pSeg->idx));
210638 goto geopolyOverlapDone;
210639 }else if( pPrev->y!=pSeg->y ){
210640 GEODEBUG(("MASK: %d\n", iMask));
210641 aOverlap[iMask] = 1;
210642 }
210643 }
210644 iMask ^= pSeg->side;
210645 pPrev = pSeg;
210646 }
210647 }
210648 GEODEBUG(("%s %d.%d C=%g B=%g\n",
210649 pThisEvent->eType ? "RM " : "ADD",
210650 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
210651 pThisEvent->pSeg->C,
210652 pThisEvent->pSeg->B));
210653 if( pThisEvent->eType==0 ){
210654 /* Add a segment */
210655 pSeg = pThisEvent->pSeg;
210656 pSeg->y = pSeg->y0;
210657 pSeg->pNext = pActive;
210658 pActive = pSeg;
210659 needSort = 1;
210660 }else{
210661 /* Remove a segment */
210662 if( pActive==pThisEvent->pSeg ){
210663 pActive = ALWAYS(pActive) ? pActive->pNext : 0;
210664 }else{
210665 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
210666 if( pSeg->pNext==pThisEvent->pSeg ){
210667 pSeg->pNext = ALWAYS(pSeg->pNext) ? pSeg->pNext->pNext : 0;
210668 break;
210669 }
210670 }
210671 }
210672 }
210673 pThisEvent = pThisEvent->pNext;
210674 }
210675 if( aOverlap[3]==0 ){
210676 rc = 0;
210677 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
210678 rc = 3;
210679 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
210680 rc = 2;
210681 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
210682 rc = 4;
210683 }else{
210684 rc = 1;
210685 }
210686
210687geopolyOverlapDone:
210688 sqlite3_free(p);
210689 return rc;
210690}
210691
210692/*
210693** SQL function: geopoly_overlap(P1,P2)
210694**
210695** Determine whether or not P1 and P2 overlap. Return value:
210696**
210697** 0 The two polygons are disjoint
210698** 1 They overlap
210699** 2 P1 is completely contained within P2
210700** 3 P2 is completely contained within P1
210701** 4 P1 and P2 are the same polygon
210702** NULL Either P1 or P2 or both are not valid polygons
210703*/
210704static void geopolyOverlapFunc(
210705 sqlite3_context *context,
210706 int argc,
210707 sqlite3_value **argv
210708){
210709 GeoPoly *p1 = geopolyFuncParam(pCtx: context, pVal: argv[0], pRc: 0);
210710 GeoPoly *p2 = geopolyFuncParam(pCtx: context, pVal: argv[1], pRc: 0);
210711 (void)argc;
210712 if( p1 && p2 ){
210713 int x = geopolyOverlap(p1, p2);
210714 if( x<0 ){
210715 sqlite3_result_error_nomem(pCtx: context);
210716 }else{
210717 sqlite3_result_int(pCtx: context, iVal: x);
210718 }
210719 }
210720 sqlite3_free(p: p1);
210721 sqlite3_free(p: p2);
210722}
210723
210724/*
210725** Enable or disable debugging output
210726*/
210727static void geopolyDebugFunc(
210728 sqlite3_context *context,
210729 int argc,
210730 sqlite3_value **argv
210731){
210732 (void)context;
210733 (void)argc;
210734#ifdef GEOPOLY_ENABLE_DEBUG
210735 geo_debug = sqlite3_value_int(argv[0]);
210736#else
210737 (void)argv;
210738#endif
210739}
210740
210741/*
210742** This function is the implementation of both the xConnect and xCreate
210743** methods of the geopoly virtual table.
210744**
210745** argv[0] -> module name
210746** argv[1] -> database name
210747** argv[2] -> table name
210748** argv[...] -> column names...
210749*/
210750static int geopolyInit(
210751 sqlite3 *db, /* Database connection */
210752 void *pAux, /* One of the RTREE_COORD_* constants */
210753 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
210754 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
210755 char **pzErr, /* OUT: Error message, if any */
210756 int isCreate /* True for xCreate, false for xConnect */
210757){
210758 int rc = SQLITE_OK;
210759 Rtree *pRtree;
210760 sqlite3_int64 nDb; /* Length of string argv[1] */
210761 sqlite3_int64 nName; /* Length of string argv[2] */
210762 sqlite3_str *pSql;
210763 char *zSql;
210764 int ii;
210765 (void)pAux;
210766
210767 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
210768
210769 /* Allocate the sqlite3_vtab structure */
210770 nDb = strlen(s: argv[1]);
210771 nName = strlen(s: argv[2]);
210772 pRtree = (Rtree *)sqlite3_malloc64(n: sizeof(Rtree)+nDb+nName+2);
210773 if( !pRtree ){
210774 return SQLITE_NOMEM;
210775 }
210776 memset(s: pRtree, c: 0, n: sizeof(Rtree)+nDb+nName+2);
210777 pRtree->nBusy = 1;
210778 pRtree->base.pModule = &rtreeModule;
210779 pRtree->zDb = (char *)&pRtree[1];
210780 pRtree->zName = &pRtree->zDb[nDb+1];
210781 pRtree->eCoordType = RTREE_COORD_REAL32;
210782 pRtree->nDim = 2;
210783 pRtree->nDim2 = 4;
210784 memcpy(dest: pRtree->zDb, src: argv[1], n: nDb);
210785 memcpy(dest: pRtree->zName, src: argv[2], n: nName);
210786
210787
210788 /* Create/Connect to the underlying relational database schema. If
210789 ** that is successful, call sqlite3_declare_vtab() to configure
210790 ** the r-tree table schema.
210791 */
210792 pSql = sqlite3_str_new(db);
210793 sqlite3_str_appendf(p: pSql, zFormat: "CREATE TABLE x(_shape");
210794 pRtree->nAux = 1; /* Add one for _shape */
210795 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
210796 for(ii=3; ii<argc; ii++){
210797 pRtree->nAux++;
210798 sqlite3_str_appendf(p: pSql, zFormat: ",%s", argv[ii]);
210799 }
210800 sqlite3_str_appendf(p: pSql, zFormat: ");");
210801 zSql = sqlite3_str_finish(p: pSql);
210802 if( !zSql ){
210803 rc = SQLITE_NOMEM;
210804 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zCreateTable: zSql)) ){
210805 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
210806 }
210807 sqlite3_free(p: zSql);
210808 if( rc ) goto geopolyInit_fail;
210809 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
210810
210811 /* Figure out the node size to use. */
210812 rc = getNodeSize(db, pRtree, isCreate, pzErr);
210813 if( rc ) goto geopolyInit_fail;
210814 rc = rtreeSqlInit(pRtree, db, zDb: argv[1], zPrefix: argv[2], isCreate);
210815 if( rc ){
210816 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
210817 goto geopolyInit_fail;
210818 }
210819
210820 *ppVtab = (sqlite3_vtab *)pRtree;
210821 return SQLITE_OK;
210822
210823geopolyInit_fail:
210824 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
210825 assert( *ppVtab==0 );
210826 assert( pRtree->nBusy==1 );
210827 rtreeRelease(pRtree);
210828 return rc;
210829}
210830
210831
210832/*
210833** GEOPOLY virtual table module xCreate method.
210834*/
210835static int geopolyCreate(
210836 sqlite3 *db,
210837 void *pAux,
210838 int argc, const char *const*argv,
210839 sqlite3_vtab **ppVtab,
210840 char **pzErr
210841){
210842 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, isCreate: 1);
210843}
210844
210845/*
210846** GEOPOLY virtual table module xConnect method.
210847*/
210848static int geopolyConnect(
210849 sqlite3 *db,
210850 void *pAux,
210851 int argc, const char *const*argv,
210852 sqlite3_vtab **ppVtab,
210853 char **pzErr
210854){
210855 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, isCreate: 0);
210856}
210857
210858
210859/*
210860** GEOPOLY virtual table module xFilter method.
210861**
210862** Query plans:
210863**
210864** 1 rowid lookup
210865** 2 search for objects overlapping the same bounding box
210866** that contains polygon argv[0]
210867** 3 search for objects overlapping the same bounding box
210868** that contains polygon argv[0]
210869** 4 full table scan
210870*/
210871static int geopolyFilter(
210872 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
210873 int idxNum, /* Query plan */
210874 const char *idxStr, /* Not Used */
210875 int argc, sqlite3_value **argv /* Parameters to the query plan */
210876){
210877 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
210878 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
210879 RtreeNode *pRoot = 0;
210880 int rc = SQLITE_OK;
210881 int iCell = 0;
210882 (void)idxStr;
210883
210884 rtreeReference(pRtree);
210885
210886 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
210887 resetCursor(pCsr);
210888
210889 pCsr->iStrategy = idxNum;
210890 if( idxNum==1 ){
210891 /* Special case - lookup by rowid. */
210892 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
210893 RtreeSearchPoint *p; /* Search point for the leaf */
210894 i64 iRowid = sqlite3_value_int64(pVal: argv[0]);
210895 i64 iNode = 0;
210896 rc = findLeafNode(pRtree, iRowid, ppLeaf: &pLeaf, piNode: &iNode);
210897 if( rc==SQLITE_OK && pLeaf!=0 ){
210898 p = rtreeSearchPointNew(pCur: pCsr, RTREE_ZERO, iLevel: 0);
210899 assert( p!=0 ); /* Always returns pCsr->sPoint */
210900 pCsr->aNode[0] = pLeaf;
210901 p->id = iNode;
210902 p->eWithin = PARTLY_WITHIN;
210903 rc = nodeRowidIndex(pRtree, pNode: pLeaf, iRowid, piIndex: &iCell);
210904 p->iCell = (u8)iCell;
210905 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
210906 }else{
210907 pCsr->atEOF = 1;
210908 }
210909 }else{
210910 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
210911 ** with the configured constraints.
210912 */
210913 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pRoot);
210914 if( rc==SQLITE_OK && idxNum<=3 ){
210915 RtreeCoord bbox[4];
210916 RtreeConstraint *p;
210917 assert( argc==1 );
210918 assert( argv[0]!=0 );
210919 geopolyBBox(context: 0, pPoly: argv[0], aCoord: bbox, pRc: &rc);
210920 if( rc ){
210921 goto geopoly_filter_end;
210922 }
210923 pCsr->aConstraint = p = sqlite3_malloc(n: sizeof(RtreeConstraint)*4);
210924 pCsr->nConstraint = 4;
210925 if( p==0 ){
210926 rc = SQLITE_NOMEM;
210927 }else{
210928 memset(s: pCsr->aConstraint, c: 0, n: sizeof(RtreeConstraint)*4);
210929 memset(s: pCsr->anQueue, c: 0, n: sizeof(u32)*(pRtree->iDepth + 1));
210930 if( idxNum==2 ){
210931 /* Overlap query */
210932 p->op = 'B';
210933 p->iCoord = 0;
210934 p->u.rValue = bbox[1].f;
210935 p++;
210936 p->op = 'D';
210937 p->iCoord = 1;
210938 p->u.rValue = bbox[0].f;
210939 p++;
210940 p->op = 'B';
210941 p->iCoord = 2;
210942 p->u.rValue = bbox[3].f;
210943 p++;
210944 p->op = 'D';
210945 p->iCoord = 3;
210946 p->u.rValue = bbox[2].f;
210947 }else{
210948 /* Within query */
210949 p->op = 'D';
210950 p->iCoord = 0;
210951 p->u.rValue = bbox[0].f;
210952 p++;
210953 p->op = 'B';
210954 p->iCoord = 1;
210955 p->u.rValue = bbox[1].f;
210956 p++;
210957 p->op = 'D';
210958 p->iCoord = 2;
210959 p->u.rValue = bbox[2].f;
210960 p++;
210961 p->op = 'B';
210962 p->iCoord = 3;
210963 p->u.rValue = bbox[3].f;
210964 }
210965 }
210966 }
210967 if( rc==SQLITE_OK ){
210968 RtreeSearchPoint *pNew;
210969 pNew = rtreeSearchPointNew(pCur: pCsr, RTREE_ZERO, iLevel: (u8)(pRtree->iDepth+1));
210970 if( pNew==0 ){
210971 rc = SQLITE_NOMEM;
210972 goto geopoly_filter_end;
210973 }
210974 pNew->id = 1;
210975 pNew->iCell = 0;
210976 pNew->eWithin = PARTLY_WITHIN;
210977 assert( pCsr->bPoint==1 );
210978 pCsr->aNode[0] = pRoot;
210979 pRoot = 0;
210980 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
210981 rc = rtreeStepToLeaf(pCur: pCsr);
210982 }
210983 }
210984
210985geopoly_filter_end:
210986 nodeRelease(pRtree, pNode: pRoot);
210987 rtreeRelease(pRtree);
210988 return rc;
210989}
210990
210991/*
210992** Rtree virtual table module xBestIndex method. There are three
210993** table scan strategies to choose from (in order from most to
210994** least desirable):
210995**
210996** idxNum idxStr Strategy
210997** ------------------------------------------------
210998** 1 "rowid" Direct lookup by rowid.
210999** 2 "rtree" R-tree overlap query using geopoly_overlap()
211000** 3 "rtree" R-tree within query using geopoly_within()
211001** 4 "fullscan" full-table scan.
211002** ------------------------------------------------
211003*/
211004static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
211005 int ii;
211006 int iRowidTerm = -1;
211007 int iFuncTerm = -1;
211008 int idxNum = 0;
211009 (void)tab;
211010
211011 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
211012 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
211013 if( !p->usable ) continue;
211014 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
211015 iRowidTerm = ii;
211016 break;
211017 }
211018 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
211019 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
211020 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
211021 ** See geopolyFindFunction() */
211022 iFuncTerm = ii;
211023 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
211024 }
211025 }
211026
211027 if( iRowidTerm>=0 ){
211028 pIdxInfo->idxNum = 1;
211029 pIdxInfo->idxStr = "rowid";
211030 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
211031 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
211032 pIdxInfo->estimatedCost = 30.0;
211033 pIdxInfo->estimatedRows = 1;
211034 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
211035 return SQLITE_OK;
211036 }
211037 if( iFuncTerm>=0 ){
211038 pIdxInfo->idxNum = idxNum;
211039 pIdxInfo->idxStr = "rtree";
211040 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
211041 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
211042 pIdxInfo->estimatedCost = 300.0;
211043 pIdxInfo->estimatedRows = 10;
211044 return SQLITE_OK;
211045 }
211046 pIdxInfo->idxNum = 4;
211047 pIdxInfo->idxStr = "fullscan";
211048 pIdxInfo->estimatedCost = 3000000.0;
211049 pIdxInfo->estimatedRows = 100000;
211050 return SQLITE_OK;
211051}
211052
211053
211054/*
211055** GEOPOLY virtual table module xColumn method.
211056*/
211057static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
211058 Rtree *pRtree = (Rtree *)cur->pVtab;
211059 RtreeCursor *pCsr = (RtreeCursor *)cur;
211060 RtreeSearchPoint *p = rtreeSearchPointFirst(pCur: pCsr);
211061 int rc = SQLITE_OK;
211062 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCur: pCsr, pRC: &rc);
211063
211064 if( rc ) return rc;
211065 if( p==0 ) return SQLITE_OK;
211066 if( i==0 && sqlite3_vtab_nochange(p: ctx) ) return SQLITE_OK;
211067 if( i<=pRtree->nAux ){
211068 if( !pCsr->bAuxValid ){
211069 if( pCsr->pReadAux==0 ){
211070 rc = sqlite3_prepare_v3(db: pRtree->db, zSql: pRtree->zReadAuxSql, nBytes: -1, prepFlags: 0,
211071 ppStmt: &pCsr->pReadAux, pzTail: 0);
211072 if( rc ) return rc;
211073 }
211074 sqlite3_bind_int64(pStmt: pCsr->pReadAux, i: 1,
211075 iValue: nodeGetRowid(pRtree, pNode, iCell: p->iCell));
211076 rc = sqlite3_step(pStmt: pCsr->pReadAux);
211077 if( rc==SQLITE_ROW ){
211078 pCsr->bAuxValid = 1;
211079 }else{
211080 sqlite3_reset(pStmt: pCsr->pReadAux);
211081 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
211082 return rc;
211083 }
211084 }
211085 sqlite3_result_value(pCtx: ctx, pValue: sqlite3_column_value(pStmt: pCsr->pReadAux, i: i+2));
211086 }
211087 return SQLITE_OK;
211088}
211089
211090
211091/*
211092** The xUpdate method for GEOPOLY module virtual tables.
211093**
211094** For DELETE:
211095**
211096** argv[0] = the rowid to be deleted
211097**
211098** For INSERT:
211099**
211100** argv[0] = SQL NULL
211101** argv[1] = rowid to insert, or an SQL NULL to select automatically
211102** argv[2] = _shape column
211103** argv[3] = first application-defined column....
211104**
211105** For UPDATE:
211106**
211107** argv[0] = rowid to modify. Never NULL
211108** argv[1] = rowid after the change. Never NULL
211109** argv[2] = new value for _shape
211110** argv[3] = new value for first application-defined column....
211111*/
211112static int geopolyUpdate(
211113 sqlite3_vtab *pVtab,
211114 int nData,
211115 sqlite3_value **aData,
211116 sqlite_int64 *pRowid
211117){
211118 Rtree *pRtree = (Rtree *)pVtab;
211119 int rc = SQLITE_OK;
211120 RtreeCell cell; /* New cell to insert if nData>1 */
211121 i64 oldRowid; /* The old rowid */
211122 int oldRowidValid; /* True if oldRowid is valid */
211123 i64 newRowid; /* The new rowid */
211124 int newRowidValid; /* True if newRowid is valid */
211125 int coordChange = 0; /* Change in coordinates */
211126
211127 if( pRtree->nNodeRef ){
211128 /* Unable to write to the btree while another cursor is reading from it,
211129 ** since the write might do a rebalance which would disrupt the read
211130 ** cursor. */
211131 return SQLITE_LOCKED_VTAB;
211132 }
211133 rtreeReference(pRtree);
211134 assert(nData>=1);
211135
211136 oldRowidValid = sqlite3_value_type(pVal: aData[0])!=SQLITE_NULL;;
211137 oldRowid = oldRowidValid ? sqlite3_value_int64(pVal: aData[0]) : 0;
211138 newRowidValid = nData>1 && sqlite3_value_type(pVal: aData[1])!=SQLITE_NULL;
211139 newRowid = newRowidValid ? sqlite3_value_int64(pVal: aData[1]) : 0;
211140 cell.iRowid = newRowid;
211141
211142 if( nData>1 /* not a DELETE */
211143 && (!oldRowidValid /* INSERT */
211144 || !sqlite3_value_nochange(pVal: aData[2]) /* UPDATE _shape */
211145 || oldRowid!=newRowid) /* Rowid change */
211146 ){
211147 assert( aData[2]!=0 );
211148 geopolyBBox(context: 0, pPoly: aData[2], aCoord: cell.aCoord, pRc: &rc);
211149 if( rc ){
211150 if( rc==SQLITE_ERROR ){
211151 pVtab->zErrMsg =
211152 sqlite3_mprintf(zFormat: "_shape does not contain a valid polygon");
211153 }
211154 goto geopoly_update_end;
211155 }
211156 coordChange = 1;
211157
211158 /* If a rowid value was supplied, check if it is already present in
211159 ** the table. If so, the constraint has failed. */
211160 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
211161 int steprc;
211162 sqlite3_bind_int64(pStmt: pRtree->pReadRowid, i: 1, iValue: cell.iRowid);
211163 steprc = sqlite3_step(pStmt: pRtree->pReadRowid);
211164 rc = sqlite3_reset(pStmt: pRtree->pReadRowid);
211165 if( SQLITE_ROW==steprc ){
211166 if( sqlite3_vtab_on_conflict(db: pRtree->db)==SQLITE_REPLACE ){
211167 rc = rtreeDeleteRowid(pRtree, iDelete: cell.iRowid);
211168 }else{
211169 rc = rtreeConstraintError(pRtree, iCol: 0);
211170 }
211171 }
211172 }
211173 }
211174
211175 /* If aData[0] is not an SQL NULL value, it is the rowid of a
211176 ** record to delete from the r-tree table. The following block does
211177 ** just that.
211178 */
211179 if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
211180 rc = rtreeDeleteRowid(pRtree, iDelete: oldRowid);
211181 }
211182
211183 /* If the aData[] array contains more than one element, elements
211184 ** (aData[2]..aData[argc-1]) contain a new record to insert into
211185 ** the r-tree structure.
211186 */
211187 if( rc==SQLITE_OK && nData>1 && coordChange ){
211188 /* Insert the new record into the r-tree */
211189 RtreeNode *pLeaf = 0;
211190 if( !newRowidValid ){
211191 rc = rtreeNewRowid(pRtree, piRowid: &cell.iRowid);
211192 }
211193 *pRowid = cell.iRowid;
211194 if( rc==SQLITE_OK ){
211195 rc = ChooseLeaf(pRtree, pCell: &cell, iHeight: 0, ppLeaf: &pLeaf);
211196 }
211197 if( rc==SQLITE_OK ){
211198 int rc2;
211199 pRtree->iReinsertHeight = -1;
211200 rc = rtreeInsertCell(pRtree, pNode: pLeaf, pCell: &cell, iHeight: 0);
211201 rc2 = nodeRelease(pRtree, pNode: pLeaf);
211202 if( rc==SQLITE_OK ){
211203 rc = rc2;
211204 }
211205 }
211206 }
211207
211208 /* Change the data */
211209 if( rc==SQLITE_OK && nData>1 ){
211210 sqlite3_stmt *pUp = pRtree->pWriteAux;
211211 int jj;
211212 int nChange = 0;
211213 sqlite3_bind_int64(pStmt: pUp, i: 1, iValue: cell.iRowid);
211214 assert( pRtree->nAux>=1 );
211215 if( sqlite3_value_nochange(pVal: aData[2]) ){
211216 sqlite3_bind_null(pStmt: pUp, i: 2);
211217 }else{
211218 GeoPoly *p = 0;
211219 if( sqlite3_value_type(pVal: aData[2])==SQLITE_TEXT
211220 && (p = geopolyFuncParam(pCtx: 0, pVal: aData[2], pRc: &rc))!=0
211221 && rc==SQLITE_OK
211222 ){
211223 sqlite3_bind_blob(pStmt: pUp, i: 2, zData: p->hdr, nData: 4+8*p->nVertex, SQLITE_TRANSIENT);
211224 }else{
211225 sqlite3_bind_value(pStmt: pUp, i: 2, pValue: aData[2]);
211226 }
211227 sqlite3_free(p);
211228 nChange = 1;
211229 }
211230 for(jj=1; jj<nData-2; jj++){
211231 nChange++;
211232 sqlite3_bind_value(pStmt: pUp, i: jj+2, pValue: aData[jj+2]);
211233 }
211234 if( nChange ){
211235 sqlite3_step(pStmt: pUp);
211236 rc = sqlite3_reset(pStmt: pUp);
211237 }
211238 }
211239
211240geopoly_update_end:
211241 rtreeRelease(pRtree);
211242 return rc;
211243}
211244
211245/*
211246** Report that geopoly_overlap() is an overloaded function suitable
211247** for use in xBestIndex.
211248*/
211249static int geopolyFindFunction(
211250 sqlite3_vtab *pVtab,
211251 int nArg,
211252 const char *zName,
211253 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
211254 void **ppArg
211255){
211256 (void)pVtab;
211257 (void)nArg;
211258 if( sqlite3_stricmp(zLeft: zName, zRight: "geopoly_overlap")==0 ){
211259 *pxFunc = geopolyOverlapFunc;
211260 *ppArg = 0;
211261 return SQLITE_INDEX_CONSTRAINT_FUNCTION;
211262 }
211263 if( sqlite3_stricmp(zLeft: zName, zRight: "geopoly_within")==0 ){
211264 *pxFunc = geopolyWithinFunc;
211265 *ppArg = 0;
211266 return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
211267 }
211268 return 0;
211269}
211270
211271
211272static sqlite3_module geopolyModule = {
211273 3, /* iVersion */
211274 geopolyCreate, /* xCreate - create a table */
211275 geopolyConnect, /* xConnect - connect to an existing table */
211276 geopolyBestIndex, /* xBestIndex - Determine search strategy */
211277 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
211278 rtreeDestroy, /* xDestroy - Drop a table */
211279 rtreeOpen, /* xOpen - open a cursor */
211280 rtreeClose, /* xClose - close a cursor */
211281 geopolyFilter, /* xFilter - configure scan constraints */
211282 rtreeNext, /* xNext - advance a cursor */
211283 rtreeEof, /* xEof */
211284 geopolyColumn, /* xColumn - read data */
211285 rtreeRowid, /* xRowid - read data */
211286 geopolyUpdate, /* xUpdate - write data */
211287 rtreeBeginTransaction, /* xBegin - begin transaction */
211288 rtreeEndTransaction, /* xSync - sync transaction */
211289 rtreeEndTransaction, /* xCommit - commit transaction */
211290 rtreeEndTransaction, /* xRollback - rollback transaction */
211291 geopolyFindFunction, /* xFindFunction - function overloading */
211292 rtreeRename, /* xRename - rename the table */
211293 rtreeSavepoint, /* xSavepoint */
211294 0, /* xRelease */
211295 0, /* xRollbackTo */
211296 rtreeShadowName /* xShadowName */
211297};
211298
211299static int sqlite3_geopoly_init(sqlite3 *db){
211300 int rc = SQLITE_OK;
211301 static const struct {
211302 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
211303 signed char nArg;
211304 unsigned char bPure;
211305 const char *zName;
211306 } aFunc[] = {
211307 { geopolyAreaFunc, 1, 1, "geopoly_area" },
211308 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
211309 { geopolyJsonFunc, 1, 1, "geopoly_json" },
211310 { geopolySvgFunc, -1, 1, "geopoly_svg" },
211311 { geopolyWithinFunc, 2, 1, "geopoly_within" },
211312 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
211313 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
211314 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
211315 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
211316 { geopolyXformFunc, 7, 1, "geopoly_xform" },
211317 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
211318 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
211319 };
211320 static const struct {
211321 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
211322 void (*xFinal)(sqlite3_context*);
211323 const char *zName;
211324 } aAgg[] = {
211325 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
211326 };
211327 unsigned int i;
211328 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
211329 int enc;
211330 if( aFunc[i].bPure ){
211331 enc = SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS;
211332 }else{
211333 enc = SQLITE_UTF8|SQLITE_DIRECTONLY;
211334 }
211335 rc = sqlite3_create_function(db, zFunc: aFunc[i].zName, nArg: aFunc[i].nArg,
211336 enc, p: 0,
211337 xSFunc: aFunc[i].xFunc, xStep: 0, xFinal: 0);
211338 }
211339 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
211340 rc = sqlite3_create_function(db, zFunc: aAgg[i].zName, nArg: 1,
211341 SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS, p: 0,
211342 xSFunc: 0, xStep: aAgg[i].xStep, xFinal: aAgg[i].xFinal);
211343 }
211344 if( rc==SQLITE_OK ){
211345 rc = sqlite3_create_module_v2(db, zName: "geopoly", pModule: &geopolyModule, pAux: 0, xDestroy: 0);
211346 }
211347 return rc;
211348}
211349
211350/************** End of geopoly.c *********************************************/
211351/************** Continuing where we left off in rtree.c **********************/
211352#endif
211353
211354/*
211355** Register the r-tree module with database handle db. This creates the
211356** virtual table module "rtree" and the debugging/analysis scalar
211357** function "rtreenode".
211358*/
211359SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
211360 const int utf8 = SQLITE_UTF8;
211361 int rc;
211362
211363 rc = sqlite3_create_function(db, zFunc: "rtreenode", nArg: 2, enc: utf8, p: 0, xSFunc: rtreenode, xStep: 0, xFinal: 0);
211364 if( rc==SQLITE_OK ){
211365 rc = sqlite3_create_function(db, zFunc: "rtreedepth", nArg: 1, enc: utf8, p: 0,xSFunc: rtreedepth, xStep: 0, xFinal: 0);
211366 }
211367 if( rc==SQLITE_OK ){
211368 rc = sqlite3_create_function(db, zFunc: "rtreecheck", nArg: -1, enc: utf8, p: 0,xSFunc: rtreecheck, xStep: 0,xFinal: 0);
211369 }
211370 if( rc==SQLITE_OK ){
211371#ifdef SQLITE_RTREE_INT_ONLY
211372 void *c = (void *)RTREE_COORD_INT32;
211373#else
211374 void *c = (void *)RTREE_COORD_REAL32;
211375#endif
211376 rc = sqlite3_create_module_v2(db, zName: "rtree", pModule: &rtreeModule, pAux: c, xDestroy: 0);
211377 }
211378 if( rc==SQLITE_OK ){
211379 void *c = (void *)RTREE_COORD_INT32;
211380 rc = sqlite3_create_module_v2(db, zName: "rtree_i32", pModule: &rtreeModule, pAux: c, xDestroy: 0);
211381 }
211382#ifdef SQLITE_ENABLE_GEOPOLY
211383 if( rc==SQLITE_OK ){
211384 rc = sqlite3_geopoly_init(db);
211385 }
211386#endif
211387
211388 return rc;
211389}
211390
211391/*
211392** This routine deletes the RtreeGeomCallback object that was attached
211393** one of the SQL functions create by sqlite3_rtree_geometry_callback()
211394** or sqlite3_rtree_query_callback(). In other words, this routine is the
211395** destructor for an RtreeGeomCallback objecct. This routine is called when
211396** the corresponding SQL function is deleted.
211397*/
211398static void rtreeFreeCallback(void *p){
211399 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
211400 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
211401 sqlite3_free(p);
211402}
211403
211404/*
211405** This routine frees the BLOB that is returned by geomCallback().
211406*/
211407static void rtreeMatchArgFree(void *pArg){
211408 int i;
211409 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
211410 for(i=0; i<p->nParam; i++){
211411 sqlite3_value_free(pOld: p->apSqlParam[i]);
211412 }
211413 sqlite3_free(p);
211414}
211415
211416/*
211417** Each call to sqlite3_rtree_geometry_callback() or
211418** sqlite3_rtree_query_callback() creates an ordinary SQLite
211419** scalar function that is implemented by this routine.
211420**
211421** All this function does is construct an RtreeMatchArg object that
211422** contains the geometry-checking callback routines and a list of
211423** parameters to this function, then return that RtreeMatchArg object
211424** as a BLOB.
211425**
211426** The R-Tree MATCH operator will read the returned BLOB, deserialize
211427** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
211428** out which elements of the R-Tree should be returned by the query.
211429*/
211430static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
211431 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(p: ctx);
211432 RtreeMatchArg *pBlob;
211433 sqlite3_int64 nBlob;
211434 int memErr = 0;
211435
211436 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
211437 + nArg*sizeof(sqlite3_value*);
211438 pBlob = (RtreeMatchArg *)sqlite3_malloc64(n: nBlob);
211439 if( !pBlob ){
211440 sqlite3_result_error_nomem(pCtx: ctx);
211441 }else{
211442 int i;
211443 pBlob->iSize = nBlob;
211444 pBlob->cb = pGeomCtx[0];
211445 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
211446 pBlob->nParam = nArg;
211447 for(i=0; i<nArg; i++){
211448 pBlob->apSqlParam[i] = sqlite3_value_dup(pOrig: aArg[i]);
211449 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
211450#ifdef SQLITE_RTREE_INT_ONLY
211451 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
211452#else
211453 pBlob->aParam[i] = sqlite3_value_double(pVal: aArg[i]);
211454#endif
211455 }
211456 if( memErr ){
211457 sqlite3_result_error_nomem(pCtx: ctx);
211458 rtreeMatchArgFree(pArg: pBlob);
211459 }else{
211460 sqlite3_result_pointer(pCtx: ctx, pPtr: pBlob, zPType: "RtreeMatchArg", xDestructor: rtreeMatchArgFree);
211461 }
211462 }
211463}
211464
211465/*
211466** Register a new geometry function for use with the r-tree MATCH operator.
211467*/
211468SQLITE_API int sqlite3_rtree_geometry_callback(
211469 sqlite3 *db, /* Register SQL function on this connection */
211470 const char *zGeom, /* Name of the new SQL function */
211471 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
211472 void *pContext /* Extra data associated with the callback */
211473){
211474 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
211475
211476 /* Allocate and populate the context object. */
211477 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(n: sizeof(RtreeGeomCallback));
211478 if( !pGeomCtx ) return SQLITE_NOMEM;
211479 pGeomCtx->xGeom = xGeom;
211480 pGeomCtx->xQueryFunc = 0;
211481 pGeomCtx->xDestructor = 0;
211482 pGeomCtx->pContext = pContext;
211483 return sqlite3_create_function_v2(db, zFunc: zGeom, nArg: -1, SQLITE_ANY,
211484 p: (void *)pGeomCtx, xSFunc: geomCallback, xStep: 0, xFinal: 0, xDestroy: rtreeFreeCallback
211485 );
211486}
211487
211488/*
211489** Register a new 2nd-generation geometry function for use with the
211490** r-tree MATCH operator.
211491*/
211492SQLITE_API int sqlite3_rtree_query_callback(
211493 sqlite3 *db, /* Register SQL function on this connection */
211494 const char *zQueryFunc, /* Name of new SQL function */
211495 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
211496 void *pContext, /* Extra data passed into the callback */
211497 void (*xDestructor)(void*) /* Destructor for the extra data */
211498){
211499 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
211500
211501 /* Allocate and populate the context object. */
211502 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(n: sizeof(RtreeGeomCallback));
211503 if( !pGeomCtx ){
211504 if( xDestructor ) xDestructor(pContext);
211505 return SQLITE_NOMEM;
211506 }
211507 pGeomCtx->xGeom = 0;
211508 pGeomCtx->xQueryFunc = xQueryFunc;
211509 pGeomCtx->xDestructor = xDestructor;
211510 pGeomCtx->pContext = pContext;
211511 return sqlite3_create_function_v2(db, zFunc: zQueryFunc, nArg: -1, SQLITE_ANY,
211512 p: (void *)pGeomCtx, xSFunc: geomCallback, xStep: 0, xFinal: 0, xDestroy: rtreeFreeCallback
211513 );
211514}
211515
211516#if !SQLITE_CORE
211517#ifdef _WIN32
211518__declspec(dllexport)
211519#endif
211520SQLITE_API int sqlite3_rtree_init(
211521 sqlite3 *db,
211522 char **pzErrMsg,
211523 const sqlite3_api_routines *pApi
211524){
211525 SQLITE_EXTENSION_INIT2(pApi)
211526 return sqlite3RtreeInit(db);
211527}
211528#endif
211529
211530#endif
211531
211532/************** End of rtree.c ***********************************************/
211533/************** Begin file icu.c *********************************************/
211534/*
211535** 2007 May 6
211536**
211537** The author disclaims copyright to this source code. In place of
211538** a legal notice, here is a blessing:
211539**
211540** May you do good and not evil.
211541** May you find forgiveness for yourself and forgive others.
211542** May you share freely, never taking more than you give.
211543**
211544*************************************************************************
211545** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
211546**
211547** This file implements an integration between the ICU library
211548** ("International Components for Unicode", an open-source library
211549** for handling unicode data) and SQLite. The integration uses
211550** ICU to provide the following to SQLite:
211551**
211552** * An implementation of the SQL regexp() function (and hence REGEXP
211553** operator) using the ICU uregex_XX() APIs.
211554**
211555** * Implementations of the SQL scalar upper() and lower() functions
211556** for case mapping.
211557**
211558** * Integration of ICU and SQLite collation sequences.
211559**
211560** * An implementation of the LIKE operator that uses ICU to
211561** provide case-independent matching.
211562*/
211563
211564#if !defined(SQLITE_CORE) \
211565 || defined(SQLITE_ENABLE_ICU) \
211566 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
211567
211568/* Include ICU headers */
211569#include <unicode/utypes.h>
211570#include <unicode/uregex.h>
211571#include <unicode/ustring.h>
211572#include <unicode/ucol.h>
211573
211574/* #include <assert.h> */
211575
211576#ifndef SQLITE_CORE
211577/* #include "sqlite3ext.h" */
211578 SQLITE_EXTENSION_INIT1
211579#else
211580/* #include "sqlite3.h" */
211581#endif
211582
211583/*
211584** This function is called when an ICU function called from within
211585** the implementation of an SQL scalar function returns an error.
211586**
211587** The scalar function context passed as the first argument is
211588** loaded with an error message based on the following two args.
211589*/
211590static void icuFunctionError(
211591 sqlite3_context *pCtx, /* SQLite scalar function context */
211592 const char *zName, /* Name of ICU function that failed */
211593 UErrorCode e /* Error code returned by ICU function */
211594){
211595 char zBuf[128];
211596 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
211597 zBuf[127] = '\0';
211598 sqlite3_result_error(pCtx, zBuf, -1);
211599}
211600
211601#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
211602
211603/*
211604** Maximum length (in bytes) of the pattern in a LIKE or GLOB
211605** operator.
211606*/
211607#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
211608# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
211609#endif
211610
211611/*
211612** Version of sqlite3_free() that is always a function, never a macro.
211613*/
211614static void xFree(void *p){
211615 sqlite3_free(p);
211616}
211617
211618/*
211619** This lookup table is used to help decode the first byte of
211620** a multi-byte UTF8 character. It is copied here from SQLite source
211621** code file utf8.c.
211622*/
211623static const unsigned char icuUtf8Trans1[] = {
211624 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
211625 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
211626 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
211627 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
211628 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
211629 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
211630 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
211631 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
211632};
211633
211634#define SQLITE_ICU_READ_UTF8(zIn, c) \
211635 c = *(zIn++); \
211636 if( c>=0xc0 ){ \
211637 c = icuUtf8Trans1[c-0xc0]; \
211638 while( (*zIn & 0xc0)==0x80 ){ \
211639 c = (c<<6) + (0x3f & *(zIn++)); \
211640 } \
211641 }
211642
211643#define SQLITE_ICU_SKIP_UTF8(zIn) \
211644 assert( *zIn ); \
211645 if( *(zIn++)>=0xc0 ){ \
211646 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
211647 }
211648
211649
211650/*
211651** Compare two UTF-8 strings for equality where the first string is
211652** a "LIKE" expression. Return true (1) if they are the same and
211653** false (0) if they are different.
211654*/
211655static int icuLikeCompare(
211656 const uint8_t *zPattern, /* LIKE pattern */
211657 const uint8_t *zString, /* The UTF-8 string to compare against */
211658 const UChar32 uEsc /* The escape character */
211659){
211660 static const uint32_t MATCH_ONE = (uint32_t)'_';
211661 static const uint32_t MATCH_ALL = (uint32_t)'%';
211662
211663 int prevEscape = 0; /* True if the previous character was uEsc */
211664
211665 while( 1 ){
211666
211667 /* Read (and consume) the next character from the input pattern. */
211668 uint32_t uPattern;
211669 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
211670 if( uPattern==0 ) break;
211671
211672 /* There are now 4 possibilities:
211673 **
211674 ** 1. uPattern is an unescaped match-all character "%",
211675 ** 2. uPattern is an unescaped match-one character "_",
211676 ** 3. uPattern is an unescaped escape character, or
211677 ** 4. uPattern is to be handled as an ordinary character
211678 */
211679 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
211680 /* Case 1. */
211681 uint8_t c;
211682
211683 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
211684 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
211685 ** test string.
211686 */
211687 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
211688 if( c==MATCH_ONE ){
211689 if( *zString==0 ) return 0;
211690 SQLITE_ICU_SKIP_UTF8(zString);
211691 }
211692 zPattern++;
211693 }
211694
211695 if( *zPattern==0 ) return 1;
211696
211697 while( *zString ){
211698 if( icuLikeCompare(zPattern, zString, uEsc) ){
211699 return 1;
211700 }
211701 SQLITE_ICU_SKIP_UTF8(zString);
211702 }
211703 return 0;
211704
211705 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
211706 /* Case 2. */
211707 if( *zString==0 ) return 0;
211708 SQLITE_ICU_SKIP_UTF8(zString);
211709
211710 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
211711 /* Case 3. */
211712 prevEscape = 1;
211713
211714 }else{
211715 /* Case 4. */
211716 uint32_t uString;
211717 SQLITE_ICU_READ_UTF8(zString, uString);
211718 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
211719 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
211720 if( uString!=uPattern ){
211721 return 0;
211722 }
211723 prevEscape = 0;
211724 }
211725 }
211726
211727 return *zString==0;
211728}
211729
211730/*
211731** Implementation of the like() SQL function. This function implements
211732** the build-in LIKE operator. The first argument to the function is the
211733** pattern and the second argument is the string. So, the SQL statements:
211734**
211735** A LIKE B
211736**
211737** is implemented as like(B, A). If there is an escape character E,
211738**
211739** A LIKE B ESCAPE E
211740**
211741** is mapped to like(B, A, E).
211742*/
211743static void icuLikeFunc(
211744 sqlite3_context *context,
211745 int argc,
211746 sqlite3_value **argv
211747){
211748 const unsigned char *zA = sqlite3_value_text(argv[0]);
211749 const unsigned char *zB = sqlite3_value_text(argv[1]);
211750 UChar32 uEsc = 0;
211751
211752 /* Limit the length of the LIKE or GLOB pattern to avoid problems
211753 ** of deep recursion and N*N behavior in patternCompare().
211754 */
211755 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
211756 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
211757 return;
211758 }
211759
211760
211761 if( argc==3 ){
211762 /* The escape character string must consist of a single UTF-8 character.
211763 ** Otherwise, return an error.
211764 */
211765 int nE= sqlite3_value_bytes(argv[2]);
211766 const unsigned char *zE = sqlite3_value_text(argv[2]);
211767 int i = 0;
211768 if( zE==0 ) return;
211769 U8_NEXT(zE, i, nE, uEsc);
211770 if( i!=nE){
211771 sqlite3_result_error(context,
211772 "ESCAPE expression must be a single character", -1);
211773 return;
211774 }
211775 }
211776
211777 if( zA && zB ){
211778 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
211779 }
211780}
211781
211782/*
211783** Function to delete compiled regexp objects. Registered as
211784** a destructor function with sqlite3_set_auxdata().
211785*/
211786static void icuRegexpDelete(void *p){
211787 URegularExpression *pExpr = (URegularExpression *)p;
211788 uregex_close(pExpr);
211789}
211790
211791/*
211792** Implementation of SQLite REGEXP operator. This scalar function takes
211793** two arguments. The first is a regular expression pattern to compile
211794** the second is a string to match against that pattern. If either
211795** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
211796** is 1 if the string matches the pattern, or 0 otherwise.
211797**
211798** SQLite maps the regexp() function to the regexp() operator such
211799** that the following two are equivalent:
211800**
211801** zString REGEXP zPattern
211802** regexp(zPattern, zString)
211803**
211804** Uses the following ICU regexp APIs:
211805**
211806** uregex_open()
211807** uregex_matches()
211808** uregex_close()
211809*/
211810static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
211811 UErrorCode status = U_ZERO_ERROR;
211812 URegularExpression *pExpr;
211813 UBool res;
211814 const UChar *zString = sqlite3_value_text16(apArg[1]);
211815
211816 (void)nArg; /* Unused parameter */
211817
211818 /* If the left hand side of the regexp operator is NULL,
211819 ** then the result is also NULL.
211820 */
211821 if( !zString ){
211822 return;
211823 }
211824
211825 pExpr = sqlite3_get_auxdata(p, 0);
211826 if( !pExpr ){
211827 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
211828 if( !zPattern ){
211829 return;
211830 }
211831 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
211832
211833 if( U_SUCCESS(status) ){
211834 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
211835 pExpr = sqlite3_get_auxdata(p, 0);
211836 }
211837 if( !pExpr ){
211838 icuFunctionError(p, "uregex_open", status);
211839 return;
211840 }
211841 }
211842
211843 /* Configure the text that the regular expression operates on. */
211844 uregex_setText(pExpr, zString, -1, &status);
211845 if( !U_SUCCESS(status) ){
211846 icuFunctionError(p, "uregex_setText", status);
211847 return;
211848 }
211849
211850 /* Attempt the match */
211851 res = uregex_matches(pExpr, 0, &status);
211852 if( !U_SUCCESS(status) ){
211853 icuFunctionError(p, "uregex_matches", status);
211854 return;
211855 }
211856
211857 /* Set the text that the regular expression operates on to a NULL
211858 ** pointer. This is not really necessary, but it is tidier than
211859 ** leaving the regular expression object configured with an invalid
211860 ** pointer after this function returns.
211861 */
211862 uregex_setText(pExpr, 0, 0, &status);
211863
211864 /* Return 1 or 0. */
211865 sqlite3_result_int(p, res ? 1 : 0);
211866}
211867
211868/*
211869** Implementations of scalar functions for case mapping - upper() and
211870** lower(). Function upper() converts its input to upper-case (ABC).
211871** Function lower() converts to lower-case (abc).
211872**
211873** ICU provides two types of case mapping, "general" case mapping and
211874** "language specific". Refer to ICU documentation for the differences
211875** between the two.
211876**
211877** To utilise "general" case mapping, the upper() or lower() scalar
211878** functions are invoked with one argument:
211879**
211880** upper('ABC') -> 'abc'
211881** lower('abc') -> 'ABC'
211882**
211883** To access ICU "language specific" case mapping, upper() or lower()
211884** should be invoked with two arguments. The second argument is the name
211885** of the locale to use. Passing an empty string ("") or SQL NULL value
211886** as the second argument is the same as invoking the 1 argument version
211887** of upper() or lower().
211888**
211889** lower('I', 'en_us') -> 'i'
211890** lower('I', 'tr_tr') -> '\u131' (small dotless i)
211891**
211892** http://www.icu-project.org/userguide/posix.html#case_mappings
211893*/
211894static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
211895 const UChar *zInput; /* Pointer to input string */
211896 UChar *zOutput = 0; /* Pointer to output buffer */
211897 int nInput; /* Size of utf-16 input string in bytes */
211898 int nOut; /* Size of output buffer in bytes */
211899 int cnt;
211900 int bToUpper; /* True for toupper(), false for tolower() */
211901 UErrorCode status;
211902 const char *zLocale = 0;
211903
211904 assert(nArg==1 || nArg==2);
211905 bToUpper = (sqlite3_user_data(p)!=0);
211906 if( nArg==2 ){
211907 zLocale = (const char *)sqlite3_value_text(apArg[1]);
211908 }
211909
211910 zInput = sqlite3_value_text16(apArg[0]);
211911 if( !zInput ){
211912 return;
211913 }
211914 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
211915 if( nOut==0 ){
211916 sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
211917 return;
211918 }
211919
211920 for(cnt=0; cnt<2; cnt++){
211921 UChar *zNew = sqlite3_realloc(zOutput, nOut);
211922 if( zNew==0 ){
211923 sqlite3_free(zOutput);
211924 sqlite3_result_error_nomem(p);
211925 return;
211926 }
211927 zOutput = zNew;
211928 status = U_ZERO_ERROR;
211929 if( bToUpper ){
211930 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
211931 }else{
211932 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
211933 }
211934
211935 if( U_SUCCESS(status) ){
211936 sqlite3_result_text16(p, zOutput, nOut, xFree);
211937 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
211938 assert( cnt==0 );
211939 continue;
211940 }else{
211941 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
211942 }
211943 return;
211944 }
211945 assert( 0 ); /* Unreachable */
211946}
211947
211948#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
211949
211950/*
211951** Collation sequence destructor function. The pCtx argument points to
211952** a UCollator structure previously allocated using ucol_open().
211953*/
211954static void icuCollationDel(void *pCtx){
211955 UCollator *p = (UCollator *)pCtx;
211956 ucol_close(p);
211957}
211958
211959/*
211960** Collation sequence comparison function. The pCtx argument points to
211961** a UCollator structure previously allocated using ucol_open().
211962*/
211963static int icuCollationColl(
211964 void *pCtx,
211965 int nLeft,
211966 const void *zLeft,
211967 int nRight,
211968 const void *zRight
211969){
211970 UCollationResult res;
211971 UCollator *p = (UCollator *)pCtx;
211972 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
211973 switch( res ){
211974 case UCOL_LESS: return -1;
211975 case UCOL_GREATER: return +1;
211976 case UCOL_EQUAL: return 0;
211977 }
211978 assert(!"Unexpected return value from ucol_strcoll()");
211979 return 0;
211980}
211981
211982/*
211983** Implementation of the scalar function icu_load_collation().
211984**
211985** This scalar function is used to add ICU collation based collation
211986** types to an SQLite database connection. It is intended to be called
211987** as follows:
211988**
211989** SELECT icu_load_collation(<locale>, <collation-name>);
211990**
211991** Where <locale> is a string containing an ICU locale identifier (i.e.
211992** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
211993** collation sequence to create.
211994*/
211995static void icuLoadCollation(
211996 sqlite3_context *p,
211997 int nArg,
211998 sqlite3_value **apArg
211999){
212000 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
212001 UErrorCode status = U_ZERO_ERROR;
212002 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
212003 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
212004 UCollator *pUCollator; /* ICU library collation object */
212005 int rc; /* Return code from sqlite3_create_collation_x() */
212006
212007 assert(nArg==2);
212008 (void)nArg; /* Unused parameter */
212009 zLocale = (const char *)sqlite3_value_text(apArg[0]);
212010 zName = (const char *)sqlite3_value_text(apArg[1]);
212011
212012 if( !zLocale || !zName ){
212013 return;
212014 }
212015
212016 pUCollator = ucol_open(zLocale, &status);
212017 if( !U_SUCCESS(status) ){
212018 icuFunctionError(p, "ucol_open", status);
212019 return;
212020 }
212021 assert(p);
212022
212023 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
212024 icuCollationColl, icuCollationDel
212025 );
212026 if( rc!=SQLITE_OK ){
212027 ucol_close(pUCollator);
212028 sqlite3_result_error(p, "Error registering collation function", -1);
212029 }
212030}
212031
212032/*
212033** Register the ICU extension functions with database db.
212034*/
212035SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
212036# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS)
212037 static const struct IcuScalar {
212038 const char *zName; /* Function name */
212039 unsigned char nArg; /* Number of arguments */
212040 unsigned int enc; /* Optimal text encoding */
212041 unsigned char iContext; /* sqlite3_user_data() context */
212042 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
212043 } scalars[] = {
212044 {"icu_load_collation",2,SQLITE_UTF8|SQLITE_DIRECTONLY,1, icuLoadCollation},
212045#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
212046 {"regexp", 2, SQLITE_ANY|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
212047 {"lower", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
212048 {"lower", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
212049 {"upper", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
212050 {"upper", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
212051 {"lower", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
212052 {"lower", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
212053 {"upper", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
212054 {"upper", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
212055 {"like", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
212056 {"like", 3, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
212057#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
212058 };
212059 int rc = SQLITE_OK;
212060 int i;
212061
212062 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
212063 const struct IcuScalar *p = &scalars[i];
212064 rc = sqlite3_create_function(
212065 db, p->zName, p->nArg, p->enc,
212066 p->iContext ? (void*)db : (void*)0,
212067 p->xFunc, 0, 0
212068 );
212069 }
212070
212071 return rc;
212072}
212073
212074#if !SQLITE_CORE
212075#ifdef _WIN32
212076__declspec(dllexport)
212077#endif
212078SQLITE_API int sqlite3_icu_init(
212079 sqlite3 *db,
212080 char **pzErrMsg,
212081 const sqlite3_api_routines *pApi
212082){
212083 SQLITE_EXTENSION_INIT2(pApi)
212084 return sqlite3IcuInit(db);
212085}
212086#endif
212087
212088#endif
212089
212090/************** End of icu.c *************************************************/
212091/************** Begin file fts3_icu.c ****************************************/
212092/*
212093** 2007 June 22
212094**
212095** The author disclaims copyright to this source code. In place of
212096** a legal notice, here is a blessing:
212097**
212098** May you do good and not evil.
212099** May you find forgiveness for yourself and forgive others.
212100** May you share freely, never taking more than you give.
212101**
212102*************************************************************************
212103** This file implements a tokenizer for fts3 based on the ICU library.
212104*/
212105/* #include "fts3Int.h" */
212106#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
212107#ifdef SQLITE_ENABLE_ICU
212108
212109/* #include <assert.h> */
212110/* #include <string.h> */
212111/* #include "fts3_tokenizer.h" */
212112
212113#include <unicode/ubrk.h>
212114/* #include <unicode/ucol.h> */
212115/* #include <unicode/ustring.h> */
212116#include <unicode/utf16.h>
212117
212118typedef struct IcuTokenizer IcuTokenizer;
212119typedef struct IcuCursor IcuCursor;
212120
212121struct IcuTokenizer {
212122 sqlite3_tokenizer base;
212123 char *zLocale;
212124};
212125
212126struct IcuCursor {
212127 sqlite3_tokenizer_cursor base;
212128
212129 UBreakIterator *pIter; /* ICU break-iterator object */
212130 int nChar; /* Number of UChar elements in pInput */
212131 UChar *aChar; /* Copy of input using utf-16 encoding */
212132 int *aOffset; /* Offsets of each character in utf-8 input */
212133
212134 int nBuffer;
212135 char *zBuffer;
212136
212137 int iToken;
212138};
212139
212140/*
212141** Create a new tokenizer instance.
212142*/
212143static int icuCreate(
212144 int argc, /* Number of entries in argv[] */
212145 const char * const *argv, /* Tokenizer creation arguments */
212146 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
212147){
212148 IcuTokenizer *p;
212149 int n = 0;
212150
212151 if( argc>0 ){
212152 n = strlen(argv[0])+1;
212153 }
212154 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
212155 if( !p ){
212156 return SQLITE_NOMEM;
212157 }
212158 memset(p, 0, sizeof(IcuTokenizer));
212159
212160 if( n ){
212161 p->zLocale = (char *)&p[1];
212162 memcpy(p->zLocale, argv[0], n);
212163 }
212164
212165 *ppTokenizer = (sqlite3_tokenizer *)p;
212166
212167 return SQLITE_OK;
212168}
212169
212170/*
212171** Destroy a tokenizer
212172*/
212173static int icuDestroy(sqlite3_tokenizer *pTokenizer){
212174 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
212175 sqlite3_free(p);
212176 return SQLITE_OK;
212177}
212178
212179/*
212180** Prepare to begin tokenizing a particular string. The input
212181** string to be tokenized is pInput[0..nBytes-1]. A cursor
212182** used to incrementally tokenize this string is returned in
212183** *ppCursor.
212184*/
212185static int icuOpen(
212186 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
212187 const char *zInput, /* Input string */
212188 int nInput, /* Length of zInput in bytes */
212189 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
212190){
212191 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
212192 IcuCursor *pCsr;
212193
212194 const int32_t opt = U_FOLD_CASE_DEFAULT;
212195 UErrorCode status = U_ZERO_ERROR;
212196 int nChar;
212197
212198 UChar32 c;
212199 int iInput = 0;
212200 int iOut = 0;
212201
212202 *ppCursor = 0;
212203
212204 if( zInput==0 ){
212205 nInput = 0;
212206 zInput = "";
212207 }else if( nInput<0 ){
212208 nInput = strlen(zInput);
212209 }
212210 nChar = nInput+1;
212211 pCsr = (IcuCursor *)sqlite3_malloc64(
212212 sizeof(IcuCursor) + /* IcuCursor */
212213 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
212214 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
212215 );
212216 if( !pCsr ){
212217 return SQLITE_NOMEM;
212218 }
212219 memset(pCsr, 0, sizeof(IcuCursor));
212220 pCsr->aChar = (UChar *)&pCsr[1];
212221 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
212222
212223 pCsr->aOffset[iOut] = iInput;
212224 U8_NEXT(zInput, iInput, nInput, c);
212225 while( c>0 ){
212226 int isError = 0;
212227 c = u_foldCase(c, opt);
212228 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
212229 if( isError ){
212230 sqlite3_free(pCsr);
212231 return SQLITE_ERROR;
212232 }
212233 pCsr->aOffset[iOut] = iInput;
212234
212235 if( iInput<nInput ){
212236 U8_NEXT(zInput, iInput, nInput, c);
212237 }else{
212238 c = 0;
212239 }
212240 }
212241
212242 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
212243 if( !U_SUCCESS(status) ){
212244 sqlite3_free(pCsr);
212245 return SQLITE_ERROR;
212246 }
212247 pCsr->nChar = iOut;
212248
212249 ubrk_first(pCsr->pIter);
212250 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
212251 return SQLITE_OK;
212252}
212253
212254/*
212255** Close a tokenization cursor previously opened by a call to icuOpen().
212256*/
212257static int icuClose(sqlite3_tokenizer_cursor *pCursor){
212258 IcuCursor *pCsr = (IcuCursor *)pCursor;
212259 ubrk_close(pCsr->pIter);
212260 sqlite3_free(pCsr->zBuffer);
212261 sqlite3_free(pCsr);
212262 return SQLITE_OK;
212263}
212264
212265/*
212266** Extract the next token from a tokenization cursor.
212267*/
212268static int icuNext(
212269 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
212270 const char **ppToken, /* OUT: *ppToken is the token text */
212271 int *pnBytes, /* OUT: Number of bytes in token */
212272 int *piStartOffset, /* OUT: Starting offset of token */
212273 int *piEndOffset, /* OUT: Ending offset of token */
212274 int *piPosition /* OUT: Position integer of token */
212275){
212276 IcuCursor *pCsr = (IcuCursor *)pCursor;
212277
212278 int iStart = 0;
212279 int iEnd = 0;
212280 int nByte = 0;
212281
212282 while( iStart==iEnd ){
212283 UChar32 c;
212284
212285 iStart = ubrk_current(pCsr->pIter);
212286 iEnd = ubrk_next(pCsr->pIter);
212287 if( iEnd==UBRK_DONE ){
212288 return SQLITE_DONE;
212289 }
212290
212291 while( iStart<iEnd ){
212292 int iWhite = iStart;
212293 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
212294 if( u_isspace(c) ){
212295 iStart = iWhite;
212296 }else{
212297 break;
212298 }
212299 }
212300 assert(iStart<=iEnd);
212301 }
212302
212303 do {
212304 UErrorCode status = U_ZERO_ERROR;
212305 if( nByte ){
212306 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
212307 if( !zNew ){
212308 return SQLITE_NOMEM;
212309 }
212310 pCsr->zBuffer = zNew;
212311 pCsr->nBuffer = nByte;
212312 }
212313
212314 u_strToUTF8(
212315 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
212316 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
212317 &status /* Output success/failure */
212318 );
212319 } while( nByte>pCsr->nBuffer );
212320
212321 *ppToken = pCsr->zBuffer;
212322 *pnBytes = nByte;
212323 *piStartOffset = pCsr->aOffset[iStart];
212324 *piEndOffset = pCsr->aOffset[iEnd];
212325 *piPosition = pCsr->iToken++;
212326
212327 return SQLITE_OK;
212328}
212329
212330/*
212331** The set of routines that implement the simple tokenizer
212332*/
212333static const sqlite3_tokenizer_module icuTokenizerModule = {
212334 0, /* iVersion */
212335 icuCreate, /* xCreate */
212336 icuDestroy, /* xCreate */
212337 icuOpen, /* xOpen */
212338 icuClose, /* xClose */
212339 icuNext, /* xNext */
212340 0, /* xLanguageid */
212341};
212342
212343/*
212344** Set *ppModule to point at the implementation of the ICU tokenizer.
212345*/
212346SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
212347 sqlite3_tokenizer_module const**ppModule
212348){
212349 *ppModule = &icuTokenizerModule;
212350}
212351
212352#endif /* defined(SQLITE_ENABLE_ICU) */
212353#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
212354
212355/************** End of fts3_icu.c ********************************************/
212356/************** Begin file sqlite3rbu.c **************************************/
212357/*
212358** 2014 August 30
212359**
212360** The author disclaims copyright to this source code. In place of
212361** a legal notice, here is a blessing:
212362**
212363** May you do good and not evil.
212364** May you find forgiveness for yourself and forgive others.
212365** May you share freely, never taking more than you give.
212366**
212367*************************************************************************
212368**
212369**
212370** OVERVIEW
212371**
212372** The RBU extension requires that the RBU update be packaged as an
212373** SQLite database. The tables it expects to find are described in
212374** sqlite3rbu.h. Essentially, for each table xyz in the target database
212375** that the user wishes to write to, a corresponding data_xyz table is
212376** created in the RBU database and populated with one row for each row to
212377** update, insert or delete from the target table.
212378**
212379** The update proceeds in three stages:
212380**
212381** 1) The database is updated. The modified database pages are written
212382** to a *-oal file. A *-oal file is just like a *-wal file, except
212383** that it is named "<database>-oal" instead of "<database>-wal".
212384** Because regular SQLite clients do not look for file named
212385** "<database>-oal", they go on using the original database in
212386** rollback mode while the *-oal file is being generated.
212387**
212388** During this stage RBU does not update the database by writing
212389** directly to the target tables. Instead it creates "imposter"
212390** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
212391** to update each b-tree individually. All updates required by each
212392** b-tree are completed before moving on to the next, and all
212393** updates are done in sorted key order.
212394**
212395** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
212396** location using a call to rename(2). Before doing this the RBU
212397** module takes an EXCLUSIVE lock on the database file, ensuring
212398** that there are no other active readers.
212399**
212400** Once the EXCLUSIVE lock is released, any other database readers
212401** detect the new *-wal file and read the database in wal mode. At
212402** this point they see the new version of the database - including
212403** the updates made as part of the RBU update.
212404**
212405** 3) The new *-wal file is checkpointed. This proceeds in the same way
212406** as a regular database checkpoint, except that a single frame is
212407** checkpointed each time sqlite3rbu_step() is called. If the RBU
212408** handle is closed before the entire *-wal file is checkpointed,
212409** the checkpoint progress is saved in the RBU database and the
212410** checkpoint can be resumed by another RBU client at some point in
212411** the future.
212412**
212413** POTENTIAL PROBLEMS
212414**
212415** The rename() call might not be portable. And RBU is not currently
212416** syncing the directory after renaming the file.
212417**
212418** When state is saved, any commit to the *-oal file and the commit to
212419** the RBU update database are not atomic. So if the power fails at the
212420** wrong moment they might get out of sync. As the main database will be
212421** committed before the RBU update database this will likely either just
212422** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
212423** constraint violations).
212424**
212425** If some client does modify the target database mid RBU update, or some
212426** other error occurs, the RBU extension will keep throwing errors. It's
212427** not really clear how to get out of this state. The system could just
212428** by delete the RBU update database and *-oal file and have the device
212429** download the update again and start over.
212430**
212431** At present, for an UPDATE, both the new.* and old.* records are
212432** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
212433** fields are collected. This means we're probably writing a lot more
212434** data to disk when saving the state of an ongoing update to the RBU
212435** update database than is strictly necessary.
212436**
212437*/
212438
212439/* #include <assert.h> */
212440/* #include <string.h> */
212441/* #include <stdio.h> */
212442
212443/* #include "sqlite3.h" */
212444
212445#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
212446/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
212447/************** Begin file sqlite3rbu.h **************************************/
212448/*
212449** 2014 August 30
212450**
212451** The author disclaims copyright to this source code. In place of
212452** a legal notice, here is a blessing:
212453**
212454** May you do good and not evil.
212455** May you find forgiveness for yourself and forgive others.
212456** May you share freely, never taking more than you give.
212457**
212458*************************************************************************
212459**
212460** This file contains the public interface for the RBU extension.
212461*/
212462
212463/*
212464** SUMMARY
212465**
212466** Writing a transaction containing a large number of operations on
212467** b-tree indexes that are collectively larger than the available cache
212468** memory can be very inefficient.
212469**
212470** The problem is that in order to update a b-tree, the leaf page (at least)
212471** containing the entry being inserted or deleted must be modified. If the
212472** working set of leaves is larger than the available cache memory, then a
212473** single leaf that is modified more than once as part of the transaction
212474** may be loaded from or written to the persistent media multiple times.
212475** Additionally, because the index updates are likely to be applied in
212476** random order, access to pages within the database is also likely to be in
212477** random order, which is itself quite inefficient.
212478**
212479** One way to improve the situation is to sort the operations on each index
212480** by index key before applying them to the b-tree. This leads to an IO
212481** pattern that resembles a single linear scan through the index b-tree,
212482** and all but guarantees each modified leaf page is loaded and stored
212483** exactly once. SQLite uses this trick to improve the performance of
212484** CREATE INDEX commands. This extension allows it to be used to improve
212485** the performance of large transactions on existing databases.
212486**
212487** Additionally, this extension allows the work involved in writing the
212488** large transaction to be broken down into sub-transactions performed
212489** sequentially by separate processes. This is useful if the system cannot
212490** guarantee that a single update process will run for long enough to apply
212491** the entire update, for example because the update is being applied on a
212492** mobile device that is frequently rebooted. Even after the writer process
212493** has committed one or more sub-transactions, other database clients continue
212494** to read from the original database snapshot. In other words, partially
212495** applied transactions are not visible to other clients.
212496**
212497** "RBU" stands for "Resumable Bulk Update". As in a large database update
212498** transmitted via a wireless network to a mobile device. A transaction
212499** applied using this extension is hence refered to as an "RBU update".
212500**
212501**
212502** LIMITATIONS
212503**
212504** An "RBU update" transaction is subject to the following limitations:
212505**
212506** * The transaction must consist of INSERT, UPDATE and DELETE operations
212507** only.
212508**
212509** * INSERT statements may not use any default values.
212510**
212511** * UPDATE and DELETE statements must identify their target rows by
212512** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
212513** KEY fields may not be updated or deleted. If the table being written
212514** has no PRIMARY KEY, affected rows must be identified by rowid.
212515**
212516** * UPDATE statements may not modify PRIMARY KEY columns.
212517**
212518** * No triggers will be fired.
212519**
212520** * No foreign key violations are detected or reported.
212521**
212522** * CHECK constraints are not enforced.
212523**
212524** * No constraint handling mode except for "OR ROLLBACK" is supported.
212525**
212526**
212527** PREPARATION
212528**
212529** An "RBU update" is stored as a separate SQLite database. A database
212530** containing an RBU update is an "RBU database". For each table in the
212531** target database to be updated, the RBU database should contain a table
212532** named "data_<target name>" containing the same set of columns as the
212533** target table, and one more - "rbu_control". The data_% table should
212534** have no PRIMARY KEY or UNIQUE constraints, but each column should have
212535** the same type as the corresponding column in the target database.
212536** The "rbu_control" column should have no type at all. For example, if
212537** the target database contains:
212538**
212539** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
212540**
212541** Then the RBU database should contain:
212542**
212543** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
212544**
212545** The order of the columns in the data_% table does not matter.
212546**
212547** Instead of a regular table, the RBU database may also contain virtual
212548** tables or views named using the data_<target> naming scheme.
212549**
212550** Instead of the plain data_<target> naming scheme, RBU database tables
212551** may also be named data<integer>_<target>, where <integer> is any sequence
212552** of zero or more numeric characters (0-9). This can be significant because
212553** tables within the RBU database are always processed in order sorted by
212554** name. By judicious selection of the <integer> portion of the names
212555** of the RBU tables the user can therefore control the order in which they
212556** are processed. This can be useful, for example, to ensure that "external
212557** content" FTS4 tables are updated before their underlying content tables.
212558**
212559** If the target database table is a virtual table or a table that has no
212560** PRIMARY KEY declaration, the data_% table must also contain a column
212561** named "rbu_rowid". This column is mapped to the table's implicit primary
212562** key column - "rowid". Virtual tables for which the "rowid" column does
212563** not function like a primary key value cannot be updated using RBU. For
212564** example, if the target db contains either of the following:
212565**
212566** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
212567** CREATE TABLE x1(a, b)
212568**
212569** then the RBU database should contain:
212570**
212571** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
212572**
212573** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
212574** target table must be present in the input table. For virtual tables,
212575** hidden columns are optional - they are updated by RBU if present in
212576** the input table, or not otherwise. For example, to write to an fts4
212577** table with a hidden languageid column such as:
212578**
212579** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
212580**
212581** Either of the following input table schemas may be used:
212582**
212583** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
212584** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
212585**
212586** For each row to INSERT into the target database as part of the RBU
212587** update, the corresponding data_% table should contain a single record
212588** with the "rbu_control" column set to contain integer value 0. The
212589** other columns should be set to the values that make up the new record
212590** to insert.
212591**
212592** If the target database table has an INTEGER PRIMARY KEY, it is not
212593** possible to insert a NULL value into the IPK column. Attempting to
212594** do so results in an SQLITE_MISMATCH error.
212595**
212596** For each row to DELETE from the target database as part of the RBU
212597** update, the corresponding data_% table should contain a single record
212598** with the "rbu_control" column set to contain integer value 1. The
212599** real primary key values of the row to delete should be stored in the
212600** corresponding columns of the data_% table. The values stored in the
212601** other columns are not used.
212602**
212603** For each row to UPDATE from the target database as part of the RBU
212604** update, the corresponding data_% table should contain a single record
212605** with the "rbu_control" column set to contain a value of type text.
212606** The real primary key values identifying the row to update should be
212607** stored in the corresponding columns of the data_% table row, as should
212608** the new values of all columns being update. The text value in the
212609** "rbu_control" column must contain the same number of characters as
212610** there are columns in the target database table, and must consist entirely
212611** of 'x' and '.' characters (or in some special cases 'd' - see below). For
212612** each column that is being updated, the corresponding character is set to
212613** 'x'. For those that remain as they are, the corresponding character of the
212614** rbu_control value should be set to '.'. For example, given the tables
212615** above, the update statement:
212616**
212617** UPDATE t1 SET c = 'usa' WHERE a = 4;
212618**
212619** is represented by the data_t1 row created by:
212620**
212621** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
212622**
212623** Instead of an 'x' character, characters of the rbu_control value specified
212624** for UPDATEs may also be set to 'd'. In this case, instead of updating the
212625** target table with the value stored in the corresponding data_% column, the
212626** user-defined SQL function "rbu_delta()" is invoked and the result stored in
212627** the target table column. rbu_delta() is invoked with two arguments - the
212628** original value currently stored in the target table column and the
212629** value specified in the data_xxx table.
212630**
212631** For example, this row:
212632**
212633** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
212634**
212635** is similar to an UPDATE statement such as:
212636**
212637** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
212638**
212639** Finally, if an 'f' character appears in place of a 'd' or 's' in an
212640** ota_control string, the contents of the data_xxx table column is assumed
212641** to be a "fossil delta" - a patch to be applied to a blob value in the
212642** format used by the fossil source-code management system. In this case
212643** the existing value within the target database table must be of type BLOB.
212644** It is replaced by the result of applying the specified fossil delta to
212645** itself.
212646**
212647** If the target database table is a virtual table or a table with no PRIMARY
212648** KEY, the rbu_control value should not include a character corresponding
212649** to the rbu_rowid value. For example, this:
212650**
212651** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
212652** VALUES(NULL, 'usa', 12, '.x');
212653**
212654** causes a result similar to:
212655**
212656** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
212657**
212658** The data_xxx tables themselves should have no PRIMARY KEY declarations.
212659** However, RBU is more efficient if reading the rows in from each data_xxx
212660** table in "rowid" order is roughly the same as reading them sorted by
212661** the PRIMARY KEY of the corresponding target database table. In other
212662** words, rows should be sorted using the destination table PRIMARY KEY
212663** fields before they are inserted into the data_xxx tables.
212664**
212665** USAGE
212666**
212667** The API declared below allows an application to apply an RBU update
212668** stored on disk to an existing target database. Essentially, the
212669** application:
212670**
212671** 1) Opens an RBU handle using the sqlite3rbu_open() function.
212672**
212673** 2) Registers any required virtual table modules with the database
212674** handle returned by sqlite3rbu_db(). Also, if required, register
212675** the rbu_delta() implementation.
212676**
212677** 3) Calls the sqlite3rbu_step() function one or more times on
212678** the new handle. Each call to sqlite3rbu_step() performs a single
212679** b-tree operation, so thousands of calls may be required to apply
212680** a complete update.
212681**
212682** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
212683** sqlite3rbu_step() has been called enough times to completely
212684** apply the update to the target database, then the RBU database
212685** is marked as fully applied. Otherwise, the state of the RBU
212686** update application is saved in the RBU database for later
212687** resumption.
212688**
212689** See comments below for more detail on APIs.
212690**
212691** If an update is only partially applied to the target database by the
212692** time sqlite3rbu_close() is called, various state information is saved
212693** within the RBU database. This allows subsequent processes to automatically
212694** resume the RBU update from where it left off.
212695**
212696** To remove all RBU extension state information, returning an RBU database
212697** to its original contents, it is sufficient to drop all tables that begin
212698** with the prefix "rbu_"
212699**
212700** DATABASE LOCKING
212701**
212702** An RBU update may not be applied to a database in WAL mode. Attempting
212703** to do so is an error (SQLITE_ERROR).
212704**
212705** While an RBU handle is open, a SHARED lock may be held on the target
212706** database file. This means it is possible for other clients to read the
212707** database, but not to write it.
212708**
212709** If an RBU update is started and then suspended before it is completed,
212710** then an external client writes to the database, then attempting to resume
212711** the suspended RBU update is also an error (SQLITE_BUSY).
212712*/
212713
212714#ifndef _SQLITE3RBU_H
212715#define _SQLITE3RBU_H
212716
212717/* #include "sqlite3.h" ** Required for error code definitions ** */
212718
212719#if 0
212720extern "C" {
212721#endif
212722
212723typedef struct sqlite3rbu sqlite3rbu;
212724
212725/*
212726** Open an RBU handle.
212727**
212728** Argument zTarget is the path to the target database. Argument zRbu is
212729** the path to the RBU database. Each call to this function must be matched
212730** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
212731** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
212732** or zRbu begin with "file:", it will be interpreted as an SQLite
212733** database URI, not a regular file name.
212734**
212735** If the zState argument is passed a NULL value, the RBU extension stores
212736** the current state of the update (how many rows have been updated, which
212737** indexes are yet to be updated etc.) within the RBU database itself. This
212738** can be convenient, as it means that the RBU application does not need to
212739** organize removing a separate state file after the update is concluded.
212740** Or, if zState is non-NULL, it must be a path to a database file in which
212741** the RBU extension can store the state of the update.
212742**
212743** When resuming an RBU update, the zState argument must be passed the same
212744** value as when the RBU update was started.
212745**
212746** Once the RBU update is finished, the RBU extension does not
212747** automatically remove any zState database file, even if it created it.
212748**
212749** By default, RBU uses the default VFS to access the files on disk. To
212750** use a VFS other than the default, an SQLite "file:" URI containing a
212751** "vfs=..." option may be passed as the zTarget option.
212752**
212753** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
212754** SQLite's built-in VFSs, including the multiplexor VFS. However it does
212755** not work out of the box with zipvfs. Refer to the comment describing
212756** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
212757*/
212758SQLITE_API sqlite3rbu *sqlite3rbu_open(
212759 const char *zTarget,
212760 const char *zRbu,
212761 const char *zState
212762);
212763
212764/*
212765** Open an RBU handle to perform an RBU vacuum on database file zTarget.
212766** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
212767** that it can be suspended and resumed like an RBU update.
212768**
212769** The second argument to this function identifies a database in which
212770** to store the state of the RBU vacuum operation if it is suspended. The
212771** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
212772** operation, the state database should either not exist or be empty
212773** (contain no tables). If an RBU vacuum is suspended by calling
212774** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
212775** returned SQLITE_DONE, the vacuum state is stored in the state database.
212776** The vacuum can be resumed by calling this function to open a new RBU
212777** handle specifying the same target and state databases.
212778**
212779** If the second argument passed to this function is NULL, then the
212780** name of the state database is "<database>-vacuum", where <database>
212781** is the name of the target database file. In this case, on UNIX, if the
212782** state database is not already present in the file-system, it is created
212783** with the same permissions as the target db is made.
212784**
212785** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
212786** state database ends with "-vactmp". This name is reserved for internal
212787** use.
212788**
212789** This function does not delete the state database after an RBU vacuum
212790** is completed, even if it created it. However, if the call to
212791** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
212792** of the state tables within the state database are zeroed. This way,
212793** the next call to sqlite3rbu_vacuum() opens a handle that starts a
212794** new RBU vacuum operation.
212795**
212796** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
212797** describing the sqlite3rbu_create_vfs() API function below for
212798** a description of the complications associated with using RBU with
212799** zipvfs databases.
212800*/
212801SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
212802 const char *zTarget,
212803 const char *zState
212804);
212805
212806/*
212807** Configure a limit for the amount of temp space that may be used by
212808** the RBU handle passed as the first argument. The new limit is specified
212809** in bytes by the second parameter. If it is positive, the limit is updated.
212810** If the second parameter to this function is passed zero, then the limit
212811** is removed entirely. If the second parameter is negative, the limit is
212812** not modified (this is useful for querying the current limit).
212813**
212814** In all cases the returned value is the current limit in bytes (zero
212815** indicates unlimited).
212816**
212817** If the temp space limit is exceeded during operation, an SQLITE_FULL
212818** error is returned.
212819*/
212820SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
212821
212822/*
212823** Return the current amount of temp file space, in bytes, currently used by
212824** the RBU handle passed as the only argument.
212825*/
212826SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
212827
212828/*
212829** Internally, each RBU connection uses a separate SQLite database
212830** connection to access the target and rbu update databases. This
212831** API allows the application direct access to these database handles.
212832**
212833** The first argument passed to this function must be a valid, open, RBU
212834** handle. The second argument should be passed zero to access the target
212835** database handle, or non-zero to access the rbu update database handle.
212836** Accessing the underlying database handles may be useful in the
212837** following scenarios:
212838**
212839** * If any target tables are virtual tables, it may be necessary to
212840** call sqlite3_create_module() on the target database handle to
212841** register the required virtual table implementations.
212842**
212843** * If the data_xxx tables in the RBU source database are virtual
212844** tables, the application may need to call sqlite3_create_module() on
212845** the rbu update db handle to any required virtual table
212846** implementations.
212847**
212848** * If the application uses the "rbu_delta()" feature described above,
212849** it must use sqlite3_create_function() or similar to register the
212850** rbu_delta() implementation with the target database handle.
212851**
212852** If an error has occurred, either while opening or stepping the RBU object,
212853** this function may return NULL. The error code and message may be collected
212854** when sqlite3rbu_close() is called.
212855**
212856** Database handles returned by this function remain valid until the next
212857** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
212858*/
212859SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
212860
212861/*
212862** Do some work towards applying the RBU update to the target db.
212863**
212864** Return SQLITE_DONE if the update has been completely applied, or
212865** SQLITE_OK if no error occurs but there remains work to do to apply
212866** the RBU update. If an error does occur, some other error code is
212867** returned.
212868**
212869** Once a call to sqlite3rbu_step() has returned a value other than
212870** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
212871** that immediately return the same value.
212872*/
212873SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
212874
212875/*
212876** Force RBU to save its state to disk.
212877**
212878** If a power failure or application crash occurs during an update, following
212879** system recovery RBU may resume the update from the point at which the state
212880** was last saved. In other words, from the most recent successful call to
212881** sqlite3rbu_close() or this function.
212882**
212883** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
212884*/
212885SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
212886
212887/*
212888** Close an RBU handle.
212889**
212890** If the RBU update has been completely applied, mark the RBU database
212891** as fully applied. Otherwise, assuming no error has occurred, save the
212892** current state of the RBU update appliation to the RBU database.
212893**
212894** If an error has already occurred as part of an sqlite3rbu_step()
212895** or sqlite3rbu_open() call, or if one occurs within this function, an
212896** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
212897** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
212898** English language error message. It is the responsibility of the caller to
212899** eventually free any such buffer using sqlite3_free().
212900**
212901** Otherwise, if no error occurs, this function returns SQLITE_OK if the
212902** update has been partially applied, or SQLITE_DONE if it has been
212903** completely applied.
212904*/
212905SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
212906
212907/*
212908** Return the total number of key-value operations (inserts, deletes or
212909** updates) that have been performed on the target database since the
212910** current RBU update was started.
212911*/
212912SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
212913
212914/*
212915** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
212916** progress indications for the two stages of an RBU update. This API may
212917** be useful for driving GUI progress indicators and similar.
212918**
212919** An RBU update is divided into two stages:
212920**
212921** * Stage 1, in which changes are accumulated in an oal/wal file, and
212922** * Stage 2, in which the contents of the wal file are copied into the
212923** main database.
212924**
212925** The update is visible to non-RBU clients during stage 2. During stage 1
212926** non-RBU reader clients may see the original database.
212927**
212928** If this API is called during stage 2 of the update, output variable
212929** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
212930** to a value between 0 and 10000 to indicate the permyriadage progress of
212931** stage 2. A value of 5000 indicates that stage 2 is half finished,
212932** 9000 indicates that it is 90% finished, and so on.
212933**
212934** If this API is called during stage 1 of the update, output variable
212935** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
212936** value to which (*pnOne) is set depends on whether or not the RBU
212937** database contains an "rbu_count" table. The rbu_count table, if it
212938** exists, must contain the same columns as the following:
212939**
212940** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
212941**
212942** There must be one row in the table for each source (data_xxx) table within
212943** the RBU database. The 'tbl' column should contain the name of the source
212944** table. The 'cnt' column should contain the number of rows within the
212945** source table.
212946**
212947** If the rbu_count table is present and populated correctly and this
212948** API is called during stage 1, the *pnOne output variable is set to the
212949** permyriadage progress of the same stage. If the rbu_count table does
212950** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
212951** table exists but is not correctly populated, the value of the *pnOne
212952** output variable during stage 1 is undefined.
212953*/
212954SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
212955
212956/*
212957** Obtain an indication as to the current stage of an RBU update or vacuum.
212958** This function always returns one of the SQLITE_RBU_STATE_XXX constants
212959** defined in this file. Return values should be interpreted as follows:
212960**
212961** SQLITE_RBU_STATE_OAL:
212962** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
212963** may either add further data to the *-oal file, or compute data that will
212964** be added by a subsequent call.
212965**
212966** SQLITE_RBU_STATE_MOVE:
212967** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
212968** will move the *-oal file to the equivalent *-wal path. If the current
212969** operation is an RBU update, then the updated version of the database
212970** file will become visible to ordinary SQLite clients following the next
212971** call to sqlite3rbu_step().
212972**
212973** SQLITE_RBU_STATE_CHECKPOINT:
212974** RBU is currently performing an incremental checkpoint. The next call to
212975** sqlite3rbu_step() will copy a page of data from the *-wal file into
212976** the target database file.
212977**
212978** SQLITE_RBU_STATE_DONE:
212979** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
212980** will immediately return SQLITE_DONE.
212981**
212982** SQLITE_RBU_STATE_ERROR:
212983** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
212984** immediately return the SQLite error code associated with the error.
212985*/
212986#define SQLITE_RBU_STATE_OAL 1
212987#define SQLITE_RBU_STATE_MOVE 2
212988#define SQLITE_RBU_STATE_CHECKPOINT 3
212989#define SQLITE_RBU_STATE_DONE 4
212990#define SQLITE_RBU_STATE_ERROR 5
212991
212992SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
212993
212994/*
212995** As part of applying an RBU update or performing an RBU vacuum operation,
212996** the system must at one point move the *-oal file to the equivalent *-wal
212997** path. Normally, it does this by invoking POSIX function rename(2) directly.
212998** Except on WINCE platforms, where it uses win32 API MoveFileW(). This
212999** function may be used to register a callback that the RBU module will invoke
213000** instead of one of these APIs.
213001**
213002** If a callback is registered with an RBU handle, it invokes it instead
213003** of rename(2) when it needs to move a file within the file-system. The
213004** first argument passed to the xRename() callback is a copy of the second
213005** argument (pArg) passed to this function. The second is the full path
213006** to the file to move and the third the full path to which it should be
213007** moved. The callback function should return SQLITE_OK to indicate
213008** success. If an error occurs, it should return an SQLite error code.
213009** In this case the RBU operation will be abandoned and the error returned
213010** to the RBU user.
213011**
213012** Passing a NULL pointer in place of the xRename argument to this function
213013** restores the default behaviour.
213014*/
213015SQLITE_API void sqlite3rbu_rename_handler(
213016 sqlite3rbu *pRbu,
213017 void *pArg,
213018 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
213019);
213020
213021
213022/*
213023** Create an RBU VFS named zName that accesses the underlying file-system
213024** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
213025** then the new RBU VFS uses the default system VFS to access the file-system.
213026** The new object is registered as a non-default VFS with SQLite before
213027** returning.
213028**
213029** Part of the RBU implementation uses a custom VFS object. Usually, this
213030** object is created and deleted automatically by RBU.
213031**
213032** The exception is for applications that also use zipvfs. In this case,
213033** the custom VFS must be explicitly created by the user before the RBU
213034** handle is opened. The RBU VFS should be installed so that the zipvfs
213035** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
213036** (for example multiplexor) to access the file-system. For example,
213037** to assemble an RBU enabled VFS stack that uses both zipvfs and
213038** multiplexor (error checking omitted):
213039**
213040** // Create a VFS named "multiplex" (not the default).
213041** sqlite3_multiplex_initialize(0, 0);
213042**
213043** // Create an rbu VFS named "rbu" that uses multiplexor. If the
213044** // second argument were replaced with NULL, the "rbu" VFS would
213045** // access the file-system via the system default VFS, bypassing the
213046** // multiplexor.
213047** sqlite3rbu_create_vfs("rbu", "multiplex");
213048**
213049** // Create a zipvfs VFS named "zipvfs" that uses rbu.
213050** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
213051**
213052** // Make zipvfs the default VFS.
213053** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
213054**
213055** Because the default VFS created above includes a RBU functionality, it
213056** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
213057** that does not include the RBU layer results in an error.
213058**
213059** The overhead of adding the "rbu" VFS to the system is negligible for
213060** non-RBU users. There is no harm in an application accessing the
213061** file-system via "rbu" all the time, even if it only uses RBU functionality
213062** occasionally.
213063*/
213064SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
213065
213066/*
213067** Deregister and destroy an RBU vfs created by an earlier call to
213068** sqlite3rbu_create_vfs().
213069**
213070** VFS objects are not reference counted. If a VFS object is destroyed
213071** before all database handles that use it have been closed, the results
213072** are undefined.
213073*/
213074SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
213075
213076#if 0
213077} /* end of the 'extern "C"' block */
213078#endif
213079
213080#endif /* _SQLITE3RBU_H */
213081
213082/************** End of sqlite3rbu.h ******************************************/
213083/************** Continuing where we left off in sqlite3rbu.c *****************/
213084
213085#if defined(_WIN32_WCE)
213086/* #include "windows.h" */
213087#endif
213088
213089/* Maximum number of prepared UPDATE statements held by this module */
213090#define SQLITE_RBU_UPDATE_CACHESIZE 16
213091
213092/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
213093** to enable checksum verification.
213094*/
213095#ifndef RBU_ENABLE_DELTA_CKSUM
213096# define RBU_ENABLE_DELTA_CKSUM 0
213097#endif
213098
213099/*
213100** Swap two objects of type TYPE.
213101*/
213102#if !defined(SQLITE_AMALGAMATION)
213103# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
213104#endif
213105
213106/*
213107** Name of the URI option that causes RBU to take an exclusive lock as
213108** part of the incremental checkpoint operation.
213109*/
213110#define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
213111
213112
213113/*
213114** The rbu_state table is used to save the state of a partially applied
213115** update so that it can be resumed later. The table consists of integer
213116** keys mapped to values as follows:
213117**
213118** RBU_STATE_STAGE:
213119** May be set to integer values 1, 2, 4 or 5. As follows:
213120** 1: the *-rbu file is currently under construction.
213121** 2: the *-rbu file has been constructed, but not yet moved
213122** to the *-wal path.
213123** 4: the checkpoint is underway.
213124** 5: the rbu update has been checkpointed.
213125**
213126** RBU_STATE_TBL:
213127** Only valid if STAGE==1. The target database name of the table
213128** currently being written.
213129**
213130** RBU_STATE_IDX:
213131** Only valid if STAGE==1. The target database name of the index
213132** currently being written, or NULL if the main table is currently being
213133** updated.
213134**
213135** RBU_STATE_ROW:
213136** Only valid if STAGE==1. Number of rows already processed for the current
213137** table/index.
213138**
213139** RBU_STATE_PROGRESS:
213140** Trbul number of sqlite3rbu_step() calls made so far as part of this
213141** rbu update.
213142**
213143** RBU_STATE_CKPT:
213144** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
213145** header created by recovering the *-wal file. This is used to detect
213146** cases when another client appends frames to the *-wal file in the
213147** middle of an incremental checkpoint (an incremental checkpoint cannot
213148** be continued if this happens).
213149**
213150** RBU_STATE_COOKIE:
213151** Valid if STAGE==1. The current change-counter cookie value in the
213152** target db file.
213153**
213154** RBU_STATE_OALSZ:
213155** Valid if STAGE==1. The size in bytes of the *-oal file.
213156**
213157** RBU_STATE_DATATBL:
213158** Only valid if STAGE==1. The RBU database name of the table
213159** currently being read.
213160*/
213161#define RBU_STATE_STAGE 1
213162#define RBU_STATE_TBL 2
213163#define RBU_STATE_IDX 3
213164#define RBU_STATE_ROW 4
213165#define RBU_STATE_PROGRESS 5
213166#define RBU_STATE_CKPT 6
213167#define RBU_STATE_COOKIE 7
213168#define RBU_STATE_OALSZ 8
213169#define RBU_STATE_PHASEONESTEP 9
213170#define RBU_STATE_DATATBL 10
213171
213172#define RBU_STAGE_OAL 1
213173#define RBU_STAGE_MOVE 2
213174#define RBU_STAGE_CAPTURE 3
213175#define RBU_STAGE_CKPT 4
213176#define RBU_STAGE_DONE 5
213177
213178
213179#define RBU_CREATE_STATE \
213180 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
213181
213182typedef struct RbuFrame RbuFrame;
213183typedef struct RbuObjIter RbuObjIter;
213184typedef struct RbuState RbuState;
213185typedef struct RbuSpan RbuSpan;
213186typedef struct rbu_vfs rbu_vfs;
213187typedef struct rbu_file rbu_file;
213188typedef struct RbuUpdateStmt RbuUpdateStmt;
213189
213190#if !defined(SQLITE_AMALGAMATION)
213191typedef unsigned int u32;
213192typedef unsigned short u16;
213193typedef unsigned char u8;
213194typedef sqlite3_int64 i64;
213195#endif
213196
213197/*
213198** These values must match the values defined in wal.c for the equivalent
213199** locks. These are not magic numbers as they are part of the SQLite file
213200** format.
213201*/
213202#define WAL_LOCK_WRITE 0
213203#define WAL_LOCK_CKPT 1
213204#define WAL_LOCK_READ0 3
213205
213206#define SQLITE_FCNTL_RBUCNT 5149216
213207
213208/*
213209** A structure to store values read from the rbu_state table in memory.
213210*/
213211struct RbuState {
213212 int eStage;
213213 char *zTbl;
213214 char *zDataTbl;
213215 char *zIdx;
213216 i64 iWalCksum;
213217 int nRow;
213218 i64 nProgress;
213219 u32 iCookie;
213220 i64 iOalSz;
213221 i64 nPhaseOneStep;
213222};
213223
213224struct RbuUpdateStmt {
213225 char *zMask; /* Copy of update mask used with pUpdate */
213226 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
213227 RbuUpdateStmt *pNext;
213228};
213229
213230struct RbuSpan {
213231 const char *zSpan;
213232 int nSpan;
213233};
213234
213235/*
213236** An iterator of this type is used to iterate through all objects in
213237** the target database that require updating. For each such table, the
213238** iterator visits, in order:
213239**
213240** * the table itself,
213241** * each index of the table (zero or more points to visit), and
213242** * a special "cleanup table" state.
213243**
213244** abIndexed:
213245** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
213246** it points to an array of flags nTblCol elements in size. The flag is
213247** set for each column that is either a part of the PK or a part of an
213248** index. Or clear otherwise.
213249**
213250** If there are one or more partial indexes on the table, all fields of
213251** this array set set to 1. This is because in that case, the module has
213252** no way to tell which fields will be required to add and remove entries
213253** from the partial indexes.
213254**
213255*/
213256struct RbuObjIter {
213257 sqlite3_stmt *pTblIter; /* Iterate through tables */
213258 sqlite3_stmt *pIdxIter; /* Index iterator */
213259 int nTblCol; /* Size of azTblCol[] array */
213260 char **azTblCol; /* Array of unquoted target column names */
213261 char **azTblType; /* Array of target column types */
213262 int *aiSrcOrder; /* src table col -> target table col */
213263 u8 *abTblPk; /* Array of flags, set on target PK columns */
213264 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
213265 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
213266 int eType; /* Table type - an RBU_PK_XXX value */
213267
213268 /* Output variables. zTbl==0 implies EOF. */
213269 int bCleanup; /* True in "cleanup" state */
213270 const char *zTbl; /* Name of target db table */
213271 const char *zDataTbl; /* Name of rbu db table (or null) */
213272 const char *zIdx; /* Name of target db index (or null) */
213273 int iTnum; /* Root page of current object */
213274 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
213275 int bUnique; /* Current index is unique */
213276 int nIndex; /* Number of aux. indexes on table zTbl */
213277
213278 /* Statements created by rbuObjIterPrepareAll() */
213279 int nCol; /* Number of columns in current object */
213280 sqlite3_stmt *pSelect; /* Source data */
213281 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
213282 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
213283 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
213284 int nIdxCol;
213285 RbuSpan *aIdxCol;
213286 char *zIdxSql;
213287
213288 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
213289 RbuUpdateStmt *pRbuUpdate;
213290};
213291
213292/*
213293** Values for RbuObjIter.eType
213294**
213295** 0: Table does not exist (error)
213296** 1: Table has an implicit rowid.
213297** 2: Table has an explicit IPK column.
213298** 3: Table has an external PK index.
213299** 4: Table is WITHOUT ROWID.
213300** 5: Table is a virtual table.
213301*/
213302#define RBU_PK_NOTABLE 0
213303#define RBU_PK_NONE 1
213304#define RBU_PK_IPK 2
213305#define RBU_PK_EXTERNAL 3
213306#define RBU_PK_WITHOUT_ROWID 4
213307#define RBU_PK_VTAB 5
213308
213309
213310/*
213311** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
213312** one of the following operations.
213313*/
213314#define RBU_INSERT 1 /* Insert on a main table b-tree */
213315#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
213316#define RBU_REPLACE 3 /* Delete and then insert a row */
213317#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
213318#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
213319
213320#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
213321
213322/*
213323** A single step of an incremental checkpoint - frame iWalFrame of the wal
213324** file should be copied to page iDbPage of the database file.
213325*/
213326struct RbuFrame {
213327 u32 iDbPage;
213328 u32 iWalFrame;
213329};
213330
213331/*
213332** RBU handle.
213333**
213334** nPhaseOneStep:
213335** If the RBU database contains an rbu_count table, this value is set to
213336** a running estimate of the number of b-tree operations required to
213337** finish populating the *-oal file. This allows the sqlite3_bp_progress()
213338** API to calculate the permyriadage progress of populating the *-oal file
213339** using the formula:
213340**
213341** permyriadage = (10000 * nProgress) / nPhaseOneStep
213342**
213343** nPhaseOneStep is initialized to the sum of:
213344**
213345** nRow * (nIndex + 1)
213346**
213347** for all source tables in the RBU database, where nRow is the number
213348** of rows in the source table and nIndex the number of indexes on the
213349** corresponding target database table.
213350**
213351** This estimate is accurate if the RBU update consists entirely of
213352** INSERT operations. However, it is inaccurate if:
213353**
213354** * the RBU update contains any UPDATE operations. If the PK specified
213355** for an UPDATE operation does not exist in the target table, then
213356** no b-tree operations are required on index b-trees. Or if the
213357** specified PK does exist, then (nIndex*2) such operations are
213358** required (one delete and one insert on each index b-tree).
213359**
213360** * the RBU update contains any DELETE operations for which the specified
213361** PK does not exist. In this case no operations are required on index
213362** b-trees.
213363**
213364** * the RBU update contains REPLACE operations. These are similar to
213365** UPDATE operations.
213366**
213367** nPhaseOneStep is updated to account for the conditions above during the
213368** first pass of each source table. The updated nPhaseOneStep value is
213369** stored in the rbu_state table if the RBU update is suspended.
213370*/
213371struct sqlite3rbu {
213372 int eStage; /* Value of RBU_STATE_STAGE field */
213373 sqlite3 *dbMain; /* target database handle */
213374 sqlite3 *dbRbu; /* rbu database handle */
213375 char *zTarget; /* Path to target db */
213376 char *zRbu; /* Path to rbu db */
213377 char *zState; /* Path to state db (or NULL if zRbu) */
213378 char zStateDb[5]; /* Db name for state ("stat" or "main") */
213379 int rc; /* Value returned by last rbu_step() call */
213380 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
213381 int nStep; /* Rows processed for current object */
213382 int nProgress; /* Rows processed for all objects */
213383 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
213384 const char *zVfsName; /* Name of automatically created rbu vfs */
213385 rbu_file *pTargetFd; /* File handle open on target db */
213386 int nPagePerSector; /* Pages per sector for pTargetFd */
213387 i64 iOalSz;
213388 i64 nPhaseOneStep;
213389 void *pRenameArg;
213390 int (*xRename)(void*, const char*, const char*);
213391
213392 /* The following state variables are used as part of the incremental
213393 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
213394 ** function rbuSetupCheckpoint() for details. */
213395 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
213396 u32 mLock;
213397 int nFrame; /* Entries in aFrame[] array */
213398 int nFrameAlloc; /* Allocated size of aFrame[] array */
213399 RbuFrame *aFrame;
213400 int pgsz;
213401 u8 *aBuf;
213402 i64 iWalCksum;
213403 i64 szTemp; /* Current size of all temp files in use */
213404 i64 szTempLimit; /* Total size limit for temp files */
213405
213406 /* Used in RBU vacuum mode only */
213407 int nRbu; /* Number of RBU VFS in the stack */
213408 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
213409};
213410
213411/*
213412** An rbu VFS is implemented using an instance of this structure.
213413**
213414** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
213415** It is NULL for RBU VFS objects created explicitly using
213416** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
213417** space used by the RBU handle.
213418*/
213419struct rbu_vfs {
213420 sqlite3_vfs base; /* rbu VFS shim methods */
213421 sqlite3_vfs *pRealVfs; /* Underlying VFS */
213422 sqlite3_mutex *mutex; /* Mutex to protect pMain */
213423 sqlite3rbu *pRbu; /* Owner RBU object */
213424 rbu_file *pMain; /* List of main db files */
213425 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
213426};
213427
213428/*
213429** Each file opened by an rbu VFS is represented by an instance of
213430** the following structure.
213431**
213432** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
213433** "sz" is set to the current size of the database file.
213434*/
213435struct rbu_file {
213436 sqlite3_file base; /* sqlite3_file methods */
213437 sqlite3_file *pReal; /* Underlying file handle */
213438 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
213439 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
213440 i64 sz; /* Size of file in bytes (temp only) */
213441
213442 int openFlags; /* Flags this file was opened with */
213443 u32 iCookie; /* Cookie value for main db files */
213444 u8 iWriteVer; /* "write-version" value for main db files */
213445 u8 bNolock; /* True to fail EXCLUSIVE locks */
213446
213447 int nShm; /* Number of entries in apShm[] array */
213448 char **apShm; /* Array of mmap'd *-shm regions */
213449 char *zDel; /* Delete this when closing file */
213450
213451 const char *zWal; /* Wal filename for this main db file */
213452 rbu_file *pWalFd; /* Wal file descriptor for this main db */
213453 rbu_file *pMainNext; /* Next MAIN_DB file */
213454 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
213455};
213456
213457/*
213458** True for an RBU vacuum handle, or false otherwise.
213459*/
213460#define rbuIsVacuum(p) ((p)->zTarget==0)
213461
213462
213463/*************************************************************************
213464** The following three functions, found below:
213465**
213466** rbuDeltaGetInt()
213467** rbuDeltaChecksum()
213468** rbuDeltaApply()
213469**
213470** are lifted from the fossil source code (http://fossil-scm.org). They
213471** are used to implement the scalar SQL function rbu_fossil_delta().
213472*/
213473
213474/*
213475** Read bytes from *pz and convert them into a positive integer. When
213476** finished, leave *pz pointing to the first character past the end of
213477** the integer. The *pLen parameter holds the length of the string
213478** in *pz and is decremented once for each character in the integer.
213479*/
213480static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
213481 static const signed char zValue[] = {
213482 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
213483 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
213484 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
213485 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
213486 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
213487 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
213488 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
213489 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
213490 };
213491 unsigned int v = 0;
213492 int c;
213493 unsigned char *z = (unsigned char*)*pz;
213494 unsigned char *zStart = z;
213495 while( (c = zValue[0x7f&*(z++)])>=0 ){
213496 v = (v<<6) + c;
213497 }
213498 z--;
213499 *pLen -= z - zStart;
213500 *pz = (char*)z;
213501 return v;
213502}
213503
213504#if RBU_ENABLE_DELTA_CKSUM
213505/*
213506** Compute a 32-bit checksum on the N-byte buffer. Return the result.
213507*/
213508static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
213509 const unsigned char *z = (const unsigned char *)zIn;
213510 unsigned sum0 = 0;
213511 unsigned sum1 = 0;
213512 unsigned sum2 = 0;
213513 unsigned sum3 = 0;
213514 while(N >= 16){
213515 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
213516 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
213517 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
213518 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
213519 z += 16;
213520 N -= 16;
213521 }
213522 while(N >= 4){
213523 sum0 += z[0];
213524 sum1 += z[1];
213525 sum2 += z[2];
213526 sum3 += z[3];
213527 z += 4;
213528 N -= 4;
213529 }
213530 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
213531 switch(N){
213532 case 3: sum3 += (z[2] << 8);
213533 case 2: sum3 += (z[1] << 16);
213534 case 1: sum3 += (z[0] << 24);
213535 default: ;
213536 }
213537 return sum3;
213538}
213539#endif
213540
213541/*
213542** Apply a delta.
213543**
213544** The output buffer should be big enough to hold the whole output
213545** file and a NUL terminator at the end. The delta_output_size()
213546** routine will determine this size for you.
213547**
213548** The delta string should be null-terminated. But the delta string
213549** may contain embedded NUL characters (if the input and output are
213550** binary files) so we also have to pass in the length of the delta in
213551** the lenDelta parameter.
213552**
213553** This function returns the size of the output file in bytes (excluding
213554** the final NUL terminator character). Except, if the delta string is
213555** malformed or intended for use with a source file other than zSrc,
213556** then this routine returns -1.
213557**
213558** Refer to the delta_create() documentation above for a description
213559** of the delta file format.
213560*/
213561static int rbuDeltaApply(
213562 const char *zSrc, /* The source or pattern file */
213563 int lenSrc, /* Length of the source file */
213564 const char *zDelta, /* Delta to apply to the pattern */
213565 int lenDelta, /* Length of the delta */
213566 char *zOut /* Write the output into this preallocated buffer */
213567){
213568 unsigned int limit;
213569 unsigned int total = 0;
213570#if RBU_ENABLE_DELTA_CKSUM
213571 char *zOrigOut = zOut;
213572#endif
213573
213574 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
213575 if( *zDelta!='\n' ){
213576 /* ERROR: size integer not terminated by "\n" */
213577 return -1;
213578 }
213579 zDelta++; lenDelta--;
213580 while( *zDelta && lenDelta>0 ){
213581 unsigned int cnt, ofst;
213582 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
213583 switch( zDelta[0] ){
213584 case '@': {
213585 zDelta++; lenDelta--;
213586 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
213587 if( lenDelta>0 && zDelta[0]!=',' ){
213588 /* ERROR: copy command not terminated by ',' */
213589 return -1;
213590 }
213591 zDelta++; lenDelta--;
213592 total += cnt;
213593 if( total>limit ){
213594 /* ERROR: copy exceeds output file size */
213595 return -1;
213596 }
213597 if( (int)(ofst+cnt) > lenSrc ){
213598 /* ERROR: copy extends past end of input */
213599 return -1;
213600 }
213601 memcpy(zOut, &zSrc[ofst], cnt);
213602 zOut += cnt;
213603 break;
213604 }
213605 case ':': {
213606 zDelta++; lenDelta--;
213607 total += cnt;
213608 if( total>limit ){
213609 /* ERROR: insert command gives an output larger than predicted */
213610 return -1;
213611 }
213612 if( (int)cnt>lenDelta ){
213613 /* ERROR: insert count exceeds size of delta */
213614 return -1;
213615 }
213616 memcpy(zOut, zDelta, cnt);
213617 zOut += cnt;
213618 zDelta += cnt;
213619 lenDelta -= cnt;
213620 break;
213621 }
213622 case ';': {
213623 zDelta++; lenDelta--;
213624 zOut[0] = 0;
213625#if RBU_ENABLE_DELTA_CKSUM
213626 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
213627 /* ERROR: bad checksum */
213628 return -1;
213629 }
213630#endif
213631 if( total!=limit ){
213632 /* ERROR: generated size does not match predicted size */
213633 return -1;
213634 }
213635 return total;
213636 }
213637 default: {
213638 /* ERROR: unknown delta operator */
213639 return -1;
213640 }
213641 }
213642 }
213643 /* ERROR: unterminated delta */
213644 return -1;
213645}
213646
213647static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
213648 int size;
213649 size = rbuDeltaGetInt(&zDelta, &lenDelta);
213650 if( *zDelta!='\n' ){
213651 /* ERROR: size integer not terminated by "\n" */
213652 return -1;
213653 }
213654 return size;
213655}
213656
213657/*
213658** End of code taken from fossil.
213659*************************************************************************/
213660
213661/*
213662** Implementation of SQL scalar function rbu_fossil_delta().
213663**
213664** This function applies a fossil delta patch to a blob. Exactly two
213665** arguments must be passed to this function. The first is the blob to
213666** patch and the second the patch to apply. If no error occurs, this
213667** function returns the patched blob.
213668*/
213669static void rbuFossilDeltaFunc(
213670 sqlite3_context *context,
213671 int argc,
213672 sqlite3_value **argv
213673){
213674 const char *aDelta;
213675 int nDelta;
213676 const char *aOrig;
213677 int nOrig;
213678
213679 int nOut;
213680 int nOut2;
213681 char *aOut;
213682
213683 assert( argc==2 );
213684
213685 nOrig = sqlite3_value_bytes(argv[0]);
213686 aOrig = (const char*)sqlite3_value_blob(argv[0]);
213687 nDelta = sqlite3_value_bytes(argv[1]);
213688 aDelta = (const char*)sqlite3_value_blob(argv[1]);
213689
213690 /* Figure out the size of the output */
213691 nOut = rbuDeltaOutputSize(aDelta, nDelta);
213692 if( nOut<0 ){
213693 sqlite3_result_error(context, "corrupt fossil delta", -1);
213694 return;
213695 }
213696
213697 aOut = sqlite3_malloc(nOut+1);
213698 if( aOut==0 ){
213699 sqlite3_result_error_nomem(context);
213700 }else{
213701 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
213702 if( nOut2!=nOut ){
213703 sqlite3_free(aOut);
213704 sqlite3_result_error(context, "corrupt fossil delta", -1);
213705 }else{
213706 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
213707 }
213708 }
213709}
213710
213711
213712/*
213713** Prepare the SQL statement in buffer zSql against database handle db.
213714** If successful, set *ppStmt to point to the new statement and return
213715** SQLITE_OK.
213716**
213717** Otherwise, if an error does occur, set *ppStmt to NULL and return
213718** an SQLite error code. Additionally, set output variable *pzErrmsg to
213719** point to a buffer containing an error message. It is the responsibility
213720** of the caller to (eventually) free this buffer using sqlite3_free().
213721*/
213722static int prepareAndCollectError(
213723 sqlite3 *db,
213724 sqlite3_stmt **ppStmt,
213725 char **pzErrmsg,
213726 const char *zSql
213727){
213728 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
213729 if( rc!=SQLITE_OK ){
213730 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
213731 *ppStmt = 0;
213732 }
213733 return rc;
213734}
213735
213736/*
213737** Reset the SQL statement passed as the first argument. Return a copy
213738** of the value returned by sqlite3_reset().
213739**
213740** If an error has occurred, then set *pzErrmsg to point to a buffer
213741** containing an error message. It is the responsibility of the caller
213742** to eventually free this buffer using sqlite3_free().
213743*/
213744static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
213745 int rc = sqlite3_reset(pStmt);
213746 if( rc!=SQLITE_OK ){
213747 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
213748 }
213749 return rc;
213750}
213751
213752/*
213753** Unless it is NULL, argument zSql points to a buffer allocated using
213754** sqlite3_malloc containing an SQL statement. This function prepares the SQL
213755** statement against database db and frees the buffer. If statement
213756** compilation is successful, *ppStmt is set to point to the new statement
213757** handle and SQLITE_OK is returned.
213758**
213759** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
213760** returned. In this case, *pzErrmsg may also be set to point to an error
213761** message. It is the responsibility of the caller to free this error message
213762** buffer using sqlite3_free().
213763**
213764** If argument zSql is NULL, this function assumes that an OOM has occurred.
213765** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
213766*/
213767static int prepareFreeAndCollectError(
213768 sqlite3 *db,
213769 sqlite3_stmt **ppStmt,
213770 char **pzErrmsg,
213771 char *zSql
213772){
213773 int rc;
213774 assert( *pzErrmsg==0 );
213775 if( zSql==0 ){
213776 rc = SQLITE_NOMEM;
213777 *ppStmt = 0;
213778 }else{
213779 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
213780 sqlite3_free(zSql);
213781 }
213782 return rc;
213783}
213784
213785/*
213786** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
213787** by an earlier call to rbuObjIterCacheTableInfo().
213788*/
213789static void rbuObjIterFreeCols(RbuObjIter *pIter){
213790 int i;
213791 for(i=0; i<pIter->nTblCol; i++){
213792 sqlite3_free(pIter->azTblCol[i]);
213793 sqlite3_free(pIter->azTblType[i]);
213794 }
213795 sqlite3_free(pIter->azTblCol);
213796 pIter->azTblCol = 0;
213797 pIter->azTblType = 0;
213798 pIter->aiSrcOrder = 0;
213799 pIter->abTblPk = 0;
213800 pIter->abNotNull = 0;
213801 pIter->nTblCol = 0;
213802 pIter->eType = 0; /* Invalid value */
213803}
213804
213805/*
213806** Finalize all statements and free all allocations that are specific to
213807** the current object (table/index pair).
213808*/
213809static void rbuObjIterClearStatements(RbuObjIter *pIter){
213810 RbuUpdateStmt *pUp;
213811
213812 sqlite3_finalize(pIter->pSelect);
213813 sqlite3_finalize(pIter->pInsert);
213814 sqlite3_finalize(pIter->pDelete);
213815 sqlite3_finalize(pIter->pTmpInsert);
213816 pUp = pIter->pRbuUpdate;
213817 while( pUp ){
213818 RbuUpdateStmt *pTmp = pUp->pNext;
213819 sqlite3_finalize(pUp->pUpdate);
213820 sqlite3_free(pUp);
213821 pUp = pTmp;
213822 }
213823 sqlite3_free(pIter->aIdxCol);
213824 sqlite3_free(pIter->zIdxSql);
213825
213826 pIter->pSelect = 0;
213827 pIter->pInsert = 0;
213828 pIter->pDelete = 0;
213829 pIter->pRbuUpdate = 0;
213830 pIter->pTmpInsert = 0;
213831 pIter->nCol = 0;
213832 pIter->nIdxCol = 0;
213833 pIter->aIdxCol = 0;
213834 pIter->zIdxSql = 0;
213835}
213836
213837/*
213838** Clean up any resources allocated as part of the iterator object passed
213839** as the only argument.
213840*/
213841static void rbuObjIterFinalize(RbuObjIter *pIter){
213842 rbuObjIterClearStatements(pIter);
213843 sqlite3_finalize(pIter->pTblIter);
213844 sqlite3_finalize(pIter->pIdxIter);
213845 rbuObjIterFreeCols(pIter);
213846 memset(pIter, 0, sizeof(RbuObjIter));
213847}
213848
213849/*
213850** Advance the iterator to the next position.
213851**
213852** If no error occurs, SQLITE_OK is returned and the iterator is left
213853** pointing to the next entry. Otherwise, an error code and message is
213854** left in the RBU handle passed as the first argument. A copy of the
213855** error code is returned.
213856*/
213857static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
213858 int rc = p->rc;
213859 if( rc==SQLITE_OK ){
213860
213861 /* Free any SQLite statements used while processing the previous object */
213862 rbuObjIterClearStatements(pIter);
213863 if( pIter->zIdx==0 ){
213864 rc = sqlite3_exec(p->dbMain,
213865 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
213866 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
213867 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
213868 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
213869 , 0, 0, &p->zErrmsg
213870 );
213871 }
213872
213873 if( rc==SQLITE_OK ){
213874 if( pIter->bCleanup ){
213875 rbuObjIterFreeCols(pIter);
213876 pIter->bCleanup = 0;
213877 rc = sqlite3_step(pIter->pTblIter);
213878 if( rc!=SQLITE_ROW ){
213879 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
213880 pIter->zTbl = 0;
213881 }else{
213882 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
213883 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
213884 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
213885 }
213886 }else{
213887 if( pIter->zIdx==0 ){
213888 sqlite3_stmt *pIdx = pIter->pIdxIter;
213889 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
213890 }
213891 if( rc==SQLITE_OK ){
213892 rc = sqlite3_step(pIter->pIdxIter);
213893 if( rc!=SQLITE_ROW ){
213894 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
213895 pIter->bCleanup = 1;
213896 pIter->zIdx = 0;
213897 }else{
213898 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
213899 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
213900 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
213901 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
213902 }
213903 }
213904 }
213905 }
213906 }
213907
213908 if( rc!=SQLITE_OK ){
213909 rbuObjIterFinalize(pIter);
213910 p->rc = rc;
213911 }
213912 return rc;
213913}
213914
213915
213916/*
213917** The implementation of the rbu_target_name() SQL function. This function
213918** accepts one or two arguments. The first argument is the name of a table -
213919** the name of a table in the RBU database. The second, if it is present, is 1
213920** for a view or 0 for a table.
213921**
213922** For a non-vacuum RBU handle, if the table name matches the pattern:
213923**
213924** data[0-9]_<name>
213925**
213926** where <name> is any sequence of 1 or more characters, <name> is returned.
213927** Otherwise, if the only argument does not match the above pattern, an SQL
213928** NULL is returned.
213929**
213930** "data_t1" -> "t1"
213931** "data0123_t2" -> "t2"
213932** "dataAB_t3" -> NULL
213933**
213934** For an rbu vacuum handle, a copy of the first argument is returned if
213935** the second argument is either missing or 0 (not a view).
213936*/
213937static void rbuTargetNameFunc(
213938 sqlite3_context *pCtx,
213939 int argc,
213940 sqlite3_value **argv
213941){
213942 sqlite3rbu *p = sqlite3_user_data(pCtx);
213943 const char *zIn;
213944 assert( argc==1 || argc==2 );
213945
213946 zIn = (const char*)sqlite3_value_text(argv[0]);
213947 if( zIn ){
213948 if( rbuIsVacuum(p) ){
213949 assert( argc==2 || argc==1 );
213950 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
213951 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
213952 }
213953 }else{
213954 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
213955 int i;
213956 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
213957 if( zIn[i]=='_' && zIn[i+1] ){
213958 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
213959 }
213960 }
213961 }
213962 }
213963}
213964
213965/*
213966** Initialize the iterator structure passed as the second argument.
213967**
213968** If no error occurs, SQLITE_OK is returned and the iterator is left
213969** pointing to the first entry. Otherwise, an error code and message is
213970** left in the RBU handle passed as the first argument. A copy of the
213971** error code is returned.
213972*/
213973static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
213974 int rc;
213975 memset(pIter, 0, sizeof(RbuObjIter));
213976
213977 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
213978 sqlite3_mprintf(
213979 "SELECT rbu_target_name(name, type='view') AS target, name "
213980 "FROM sqlite_schema "
213981 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
213982 " %s "
213983 "ORDER BY name"
213984 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
213985
213986 if( rc==SQLITE_OK ){
213987 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
213988 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
213989 " FROM main.sqlite_schema "
213990 " WHERE type='index' AND tbl_name = ?"
213991 );
213992 }
213993
213994 pIter->bCleanup = 1;
213995 p->rc = rc;
213996 return rbuObjIterNext(p, pIter);
213997}
213998
213999/*
214000** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
214001** an error code is stored in the RBU handle passed as the first argument.
214002**
214003** If an error has already occurred (p->rc is already set to something other
214004** than SQLITE_OK), then this function returns NULL without modifying the
214005** stored error code. In this case it still calls sqlite3_free() on any
214006** printf() parameters associated with %z conversions.
214007*/
214008static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
214009 char *zSql = 0;
214010 va_list ap;
214011 va_start(ap, zFmt);
214012 zSql = sqlite3_vmprintf(zFmt, ap);
214013 if( p->rc==SQLITE_OK ){
214014 if( zSql==0 ) p->rc = SQLITE_NOMEM;
214015 }else{
214016 sqlite3_free(zSql);
214017 zSql = 0;
214018 }
214019 va_end(ap);
214020 return zSql;
214021}
214022
214023/*
214024** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
214025** arguments are the usual subsitution values. This function performs
214026** the printf() style substitutions and executes the result as an SQL
214027** statement on the RBU handles database.
214028**
214029** If an error occurs, an error code and error message is stored in the
214030** RBU handle. If an error has already occurred when this function is
214031** called, it is a no-op.
214032*/
214033static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
214034 va_list ap;
214035 char *zSql;
214036 va_start(ap, zFmt);
214037 zSql = sqlite3_vmprintf(zFmt, ap);
214038 if( p->rc==SQLITE_OK ){
214039 if( zSql==0 ){
214040 p->rc = SQLITE_NOMEM;
214041 }else{
214042 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
214043 }
214044 }
214045 sqlite3_free(zSql);
214046 va_end(ap);
214047 return p->rc;
214048}
214049
214050/*
214051** Attempt to allocate and return a pointer to a zeroed block of nByte
214052** bytes.
214053**
214054** If an error (i.e. an OOM condition) occurs, return NULL and leave an
214055** error code in the rbu handle passed as the first argument. Or, if an
214056** error has already occurred when this function is called, return NULL
214057** immediately without attempting the allocation or modifying the stored
214058** error code.
214059*/
214060static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
214061 void *pRet = 0;
214062 if( p->rc==SQLITE_OK ){
214063 assert( nByte>0 );
214064 pRet = sqlite3_malloc64(nByte);
214065 if( pRet==0 ){
214066 p->rc = SQLITE_NOMEM;
214067 }else{
214068 memset(pRet, 0, nByte);
214069 }
214070 }
214071 return pRet;
214072}
214073
214074
214075/*
214076** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
214077** there is room for at least nCol elements. If an OOM occurs, store an
214078** error code in the RBU handle passed as the first argument.
214079*/
214080static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
214081 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
214082 char **azNew;
214083
214084 azNew = (char**)rbuMalloc(p, nByte);
214085 if( azNew ){
214086 pIter->azTblCol = azNew;
214087 pIter->azTblType = &azNew[nCol];
214088 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
214089 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
214090 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
214091 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
214092 }
214093}
214094
214095/*
214096** The first argument must be a nul-terminated string. This function
214097** returns a copy of the string in memory obtained from sqlite3_malloc().
214098** It is the responsibility of the caller to eventually free this memory
214099** using sqlite3_free().
214100**
214101** If an OOM condition is encountered when attempting to allocate memory,
214102** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
214103** if the allocation succeeds, (*pRc) is left unchanged.
214104*/
214105static char *rbuStrndup(const char *zStr, int *pRc){
214106 char *zRet = 0;
214107
214108 if( *pRc==SQLITE_OK ){
214109 if( zStr ){
214110 size_t nCopy = strlen(zStr) + 1;
214111 zRet = (char*)sqlite3_malloc64(nCopy);
214112 if( zRet ){
214113 memcpy(zRet, zStr, nCopy);
214114 }else{
214115 *pRc = SQLITE_NOMEM;
214116 }
214117 }
214118 }
214119
214120 return zRet;
214121}
214122
214123/*
214124** Finalize the statement passed as the second argument.
214125**
214126** If the sqlite3_finalize() call indicates that an error occurs, and the
214127** rbu handle error code is not already set, set the error code and error
214128** message accordingly.
214129*/
214130static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
214131 sqlite3 *db = sqlite3_db_handle(pStmt);
214132 int rc = sqlite3_finalize(pStmt);
214133 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
214134 p->rc = rc;
214135 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
214136 }
214137}
214138
214139/* Determine the type of a table.
214140**
214141** peType is of type (int*), a pointer to an output parameter of type
214142** (int). This call sets the output parameter as follows, depending
214143** on the type of the table specified by parameters dbName and zTbl.
214144**
214145** RBU_PK_NOTABLE: No such table.
214146** RBU_PK_NONE: Table has an implicit rowid.
214147** RBU_PK_IPK: Table has an explicit IPK column.
214148** RBU_PK_EXTERNAL: Table has an external PK index.
214149** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
214150** RBU_PK_VTAB: Table is a virtual table.
214151**
214152** Argument *piPk is also of type (int*), and also points to an output
214153** parameter. Unless the table has an external primary key index
214154** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
214155** if the table does have an external primary key index, then *piPk
214156** is set to the root page number of the primary key index before
214157** returning.
214158**
214159** ALGORITHM:
214160**
214161** if( no entry exists in sqlite_schema ){
214162** return RBU_PK_NOTABLE
214163** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
214164** return RBU_PK_VTAB
214165** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
214166** if( the index that is the pk exists in sqlite_schema ){
214167** *piPK = rootpage of that index.
214168** return RBU_PK_EXTERNAL
214169** }else{
214170** return RBU_PK_WITHOUT_ROWID
214171** }
214172** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
214173** return RBU_PK_IPK
214174** }else{
214175** return RBU_PK_NONE
214176** }
214177*/
214178static void rbuTableType(
214179 sqlite3rbu *p,
214180 const char *zTab,
214181 int *peType,
214182 int *piTnum,
214183 int *piPk
214184){
214185 /*
214186 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
214187 ** 1) PRAGMA index_list = ?
214188 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
214189 ** 3) PRAGMA table_info = ?
214190 */
214191 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
214192
214193 *peType = RBU_PK_NOTABLE;
214194 *piPk = 0;
214195
214196 assert( p->rc==SQLITE_OK );
214197 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
214198 sqlite3_mprintf(
214199 "SELECT "
214200 " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
214201 " rootpage"
214202 " FROM sqlite_schema"
214203 " WHERE name=%Q", zTab
214204 ));
214205 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
214206 /* Either an error, or no such table. */
214207 goto rbuTableType_end;
214208 }
214209 if( sqlite3_column_int(aStmt[0], 0) ){
214210 *peType = RBU_PK_VTAB; /* virtual table */
214211 goto rbuTableType_end;
214212 }
214213 *piTnum = sqlite3_column_int(aStmt[0], 1);
214214
214215 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
214216 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
214217 );
214218 if( p->rc ) goto rbuTableType_end;
214219 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
214220 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
214221 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
214222 if( zOrig && zIdx && zOrig[0]=='p' ){
214223 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
214224 sqlite3_mprintf(
214225 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
214226 ));
214227 if( p->rc==SQLITE_OK ){
214228 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
214229 *piPk = sqlite3_column_int(aStmt[2], 0);
214230 *peType = RBU_PK_EXTERNAL;
214231 }else{
214232 *peType = RBU_PK_WITHOUT_ROWID;
214233 }
214234 }
214235 goto rbuTableType_end;
214236 }
214237 }
214238
214239 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
214240 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
214241 );
214242 if( p->rc==SQLITE_OK ){
214243 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
214244 if( sqlite3_column_int(aStmt[3],5)>0 ){
214245 *peType = RBU_PK_IPK; /* explicit IPK column */
214246 goto rbuTableType_end;
214247 }
214248 }
214249 *peType = RBU_PK_NONE;
214250 }
214251
214252rbuTableType_end: {
214253 unsigned int i;
214254 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
214255 rbuFinalize(p, aStmt[i]);
214256 }
214257 }
214258}
214259
214260/*
214261** This is a helper function for rbuObjIterCacheTableInfo(). It populates
214262** the pIter->abIndexed[] array.
214263*/
214264static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
214265 sqlite3_stmt *pList = 0;
214266 int bIndex = 0;
214267
214268 if( p->rc==SQLITE_OK ){
214269 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
214270 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
214271 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
214272 );
214273 }
214274
214275 pIter->nIndex = 0;
214276 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
214277 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
214278 int bPartial = sqlite3_column_int(pList, 4);
214279 sqlite3_stmt *pXInfo = 0;
214280 if( zIdx==0 ) break;
214281 if( bPartial ){
214282 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
214283 }
214284 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
214285 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
214286 );
214287 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
214288 int iCid = sqlite3_column_int(pXInfo, 1);
214289 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
214290 if( iCid==-2 ){
214291 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
214292 }
214293 }
214294 rbuFinalize(p, pXInfo);
214295 bIndex = 1;
214296 pIter->nIndex++;
214297 }
214298
214299 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
214300 /* "PRAGMA index_list" includes the main PK b-tree */
214301 pIter->nIndex--;
214302 }
214303
214304 rbuFinalize(p, pList);
214305 if( bIndex==0 ) pIter->abIndexed = 0;
214306}
214307
214308
214309/*
214310** If they are not already populated, populate the pIter->azTblCol[],
214311** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
214312** the table (not index) that the iterator currently points to.
214313**
214314** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
214315** an error does occur, an error code and error message are also left in
214316** the RBU handle.
214317*/
214318static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
214319 if( pIter->azTblCol==0 ){
214320 sqlite3_stmt *pStmt = 0;
214321 int nCol = 0;
214322 int i; /* for() loop iterator variable */
214323 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
214324 int iOrder = 0;
214325 int iTnum = 0;
214326
214327 /* Figure out the type of table this step will deal with. */
214328 assert( pIter->eType==0 );
214329 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
214330 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
214331 p->rc = SQLITE_ERROR;
214332 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
214333 }
214334 if( p->rc ) return p->rc;
214335 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
214336
214337 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
214338 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
214339 || pIter->eType==RBU_PK_VTAB
214340 );
214341
214342 /* Populate the azTblCol[] and nTblCol variables based on the columns
214343 ** of the input table. Ignore any input table columns that begin with
214344 ** "rbu_". */
214345 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
214346 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
214347 );
214348 if( p->rc==SQLITE_OK ){
214349 nCol = sqlite3_column_count(pStmt);
214350 rbuAllocateIterArrays(p, pIter, nCol);
214351 }
214352 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
214353 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
214354 if( sqlite3_strnicmp("rbu_", zName, 4) ){
214355 char *zCopy = rbuStrndup(zName, &p->rc);
214356 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
214357 pIter->azTblCol[pIter->nTblCol++] = zCopy;
214358 }
214359 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
214360 bRbuRowid = 1;
214361 }
214362 }
214363 sqlite3_finalize(pStmt);
214364 pStmt = 0;
214365
214366 if( p->rc==SQLITE_OK
214367 && rbuIsVacuum(p)==0
214368 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
214369 ){
214370 p->rc = SQLITE_ERROR;
214371 p->zErrmsg = sqlite3_mprintf(
214372 "table %q %s rbu_rowid column", pIter->zDataTbl,
214373 (bRbuRowid ? "may not have" : "requires")
214374 );
214375 }
214376
214377 /* Check that all non-HIDDEN columns in the destination table are also
214378 ** present in the input table. Populate the abTblPk[], azTblType[] and
214379 ** aiTblOrder[] arrays at the same time. */
214380 if( p->rc==SQLITE_OK ){
214381 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
214382 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
214383 );
214384 }
214385 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
214386 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
214387 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
214388 for(i=iOrder; i<pIter->nTblCol; i++){
214389 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
214390 }
214391 if( i==pIter->nTblCol ){
214392 p->rc = SQLITE_ERROR;
214393 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
214394 pIter->zDataTbl, zName
214395 );
214396 }else{
214397 int iPk = sqlite3_column_int(pStmt, 5);
214398 int bNotNull = sqlite3_column_int(pStmt, 3);
214399 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
214400
214401 if( i!=iOrder ){
214402 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
214403 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
214404 }
214405
214406 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
214407 assert( iPk>=0 );
214408 pIter->abTblPk[iOrder] = (u8)iPk;
214409 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
214410 iOrder++;
214411 }
214412 }
214413
214414 rbuFinalize(p, pStmt);
214415 rbuObjIterCacheIndexedCols(p, pIter);
214416 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
214417 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
214418 }
214419
214420 return p->rc;
214421}
214422
214423/*
214424** This function constructs and returns a pointer to a nul-terminated
214425** string containing some SQL clause or list based on one or more of the
214426** column names currently stored in the pIter->azTblCol[] array.
214427*/
214428static char *rbuObjIterGetCollist(
214429 sqlite3rbu *p, /* RBU object */
214430 RbuObjIter *pIter /* Object iterator for column names */
214431){
214432 char *zList = 0;
214433 const char *zSep = "";
214434 int i;
214435 for(i=0; i<pIter->nTblCol; i++){
214436 const char *z = pIter->azTblCol[i];
214437 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
214438 zSep = ", ";
214439 }
214440 return zList;
214441}
214442
214443/*
214444** Return a comma separated list of the quoted PRIMARY KEY column names,
214445** in order, for the current table. Before each column name, add the text
214446** zPre. After each column name, add the zPost text. Use zSeparator as
214447** the separator text (usually ", ").
214448*/
214449static char *rbuObjIterGetPkList(
214450 sqlite3rbu *p, /* RBU object */
214451 RbuObjIter *pIter, /* Object iterator for column names */
214452 const char *zPre, /* Before each quoted column name */
214453 const char *zSeparator, /* Separator to use between columns */
214454 const char *zPost /* After each quoted column name */
214455){
214456 int iPk = 1;
214457 char *zRet = 0;
214458 const char *zSep = "";
214459 while( 1 ){
214460 int i;
214461 for(i=0; i<pIter->nTblCol; i++){
214462 if( (int)pIter->abTblPk[i]==iPk ){
214463 const char *zCol = pIter->azTblCol[i];
214464 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
214465 zSep = zSeparator;
214466 break;
214467 }
214468 }
214469 if( i==pIter->nTblCol ) break;
214470 iPk++;
214471 }
214472 return zRet;
214473}
214474
214475/*
214476** This function is called as part of restarting an RBU vacuum within
214477** stage 1 of the process (while the *-oal file is being built) while
214478** updating a table (not an index). The table may be a rowid table or
214479** a WITHOUT ROWID table. It queries the target database to find the
214480** largest key that has already been written to the target table and
214481** constructs a WHERE clause that can be used to extract the remaining
214482** rows from the source table. For a rowid table, the WHERE clause
214483** is of the form:
214484**
214485** "WHERE _rowid_ > ?"
214486**
214487** and for WITHOUT ROWID tables:
214488**
214489** "WHERE (key1, key2) > (?, ?)"
214490**
214491** Instead of "?" placeholders, the actual WHERE clauses created by
214492** this function contain literal SQL values.
214493*/
214494static char *rbuVacuumTableStart(
214495 sqlite3rbu *p, /* RBU handle */
214496 RbuObjIter *pIter, /* RBU iterator object */
214497 int bRowid, /* True for a rowid table */
214498 const char *zWrite /* Target table name prefix */
214499){
214500 sqlite3_stmt *pMax = 0;
214501 char *zRet = 0;
214502 if( bRowid ){
214503 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
214504 sqlite3_mprintf(
214505 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
214506 )
214507 );
214508 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
214509 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
214510 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
214511 }
214512 rbuFinalize(p, pMax);
214513 }else{
214514 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
214515 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
214516 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
214517
214518 if( p->rc==SQLITE_OK ){
214519 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
214520 sqlite3_mprintf(
214521 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
214522 zSelect, zWrite, pIter->zTbl, zOrder
214523 )
214524 );
214525 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
214526 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
214527 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
214528 }
214529 rbuFinalize(p, pMax);
214530 }
214531
214532 sqlite3_free(zOrder);
214533 sqlite3_free(zSelect);
214534 sqlite3_free(zList);
214535 }
214536 return zRet;
214537}
214538
214539/*
214540** This function is called as part of restating an RBU vacuum when the
214541** current operation is writing content to an index. If possible, it
214542** queries the target index b-tree for the largest key already written to
214543** it, then composes and returns an expression that can be used in a WHERE
214544** clause to select the remaining required rows from the source table.
214545** It is only possible to return such an expression if:
214546**
214547** * The index contains no DESC columns, and
214548** * The last key written to the index before the operation was
214549** suspended does not contain any NULL values.
214550**
214551** The expression is of the form:
214552**
214553** (index-field1, index-field2, ...) > (?, ?, ...)
214554**
214555** except that the "?" placeholders are replaced with literal values.
214556**
214557** If the expression cannot be created, NULL is returned. In this case,
214558** the caller has to use an OFFSET clause to extract only the required
214559** rows from the sourct table, just as it does for an RBU update operation.
214560*/
214561static char *rbuVacuumIndexStart(
214562 sqlite3rbu *p, /* RBU handle */
214563 RbuObjIter *pIter /* RBU iterator object */
214564){
214565 char *zOrder = 0;
214566 char *zLhs = 0;
214567 char *zSelect = 0;
214568 char *zVector = 0;
214569 char *zRet = 0;
214570 int bFailed = 0;
214571 const char *zSep = "";
214572 int iCol = 0;
214573 sqlite3_stmt *pXInfo = 0;
214574
214575 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
214576 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
214577 );
214578 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
214579 int iCid = sqlite3_column_int(pXInfo, 1);
214580 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
214581 const char *zCol;
214582 if( sqlite3_column_int(pXInfo, 3) ){
214583 bFailed = 1;
214584 break;
214585 }
214586
214587 if( iCid<0 ){
214588 if( pIter->eType==RBU_PK_IPK ){
214589 int i;
214590 for(i=0; pIter->abTblPk[i]==0; i++);
214591 assert( i<pIter->nTblCol );
214592 zCol = pIter->azTblCol[i];
214593 }else{
214594 zCol = "_rowid_";
214595 }
214596 }else{
214597 zCol = pIter->azTblCol[iCid];
214598 }
214599
214600 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
214601 zLhs, zSep, zCol, zCollate
214602 );
214603 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
214604 zOrder, zSep, iCol, zCol, zCollate
214605 );
214606 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
214607 zSelect, zSep, iCol, zCol
214608 );
214609 zSep = ", ";
214610 iCol++;
214611 }
214612 rbuFinalize(p, pXInfo);
214613 if( bFailed ) goto index_start_out;
214614
214615 if( p->rc==SQLITE_OK ){
214616 sqlite3_stmt *pSel = 0;
214617
214618 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
214619 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
214620 zSelect, pIter->zTbl, zOrder
214621 )
214622 );
214623 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){
214624 zSep = "";
214625 for(iCol=0; iCol<pIter->nCol; iCol++){
214626 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
214627 if( zQuoted==0 ){
214628 p->rc = SQLITE_NOMEM;
214629 }else if( zQuoted[0]=='N' ){
214630 bFailed = 1;
214631 break;
214632 }
214633 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
214634 zSep = ", ";
214635 }
214636
214637 if( !bFailed ){
214638 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
214639 }
214640 }
214641 rbuFinalize(p, pSel);
214642 }
214643
214644 index_start_out:
214645 sqlite3_free(zOrder);
214646 sqlite3_free(zSelect);
214647 sqlite3_free(zVector);
214648 sqlite3_free(zLhs);
214649 return zRet;
214650}
214651
214652/*
214653** This function is used to create a SELECT list (the list of SQL
214654** expressions that follows a SELECT keyword) for a SELECT statement
214655** used to read from an data_xxx or rbu_tmp_xxx table while updating the
214656** index object currently indicated by the iterator object passed as the
214657** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
214658** to obtain the required information.
214659**
214660** If the index is of the following form:
214661**
214662** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
214663**
214664** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
214665** "ipk", the returned string is:
214666**
214667** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
214668**
214669** As well as the returned string, three other malloc'd strings are
214670** returned via output parameters. As follows:
214671**
214672** pzImposterCols: ...
214673** pzImposterPk: ...
214674** pzWhere: ...
214675*/
214676static char *rbuObjIterGetIndexCols(
214677 sqlite3rbu *p, /* RBU object */
214678 RbuObjIter *pIter, /* Object iterator for column names */
214679 char **pzImposterCols, /* OUT: Columns for imposter table */
214680 char **pzImposterPk, /* OUT: Imposter PK clause */
214681 char **pzWhere, /* OUT: WHERE clause */
214682 int *pnBind /* OUT: Trbul number of columns */
214683){
214684 int rc = p->rc; /* Error code */
214685 int rc2; /* sqlite3_finalize() return code */
214686 char *zRet = 0; /* String to return */
214687 char *zImpCols = 0; /* String to return via *pzImposterCols */
214688 char *zImpPK = 0; /* String to return via *pzImposterPK */
214689 char *zWhere = 0; /* String to return via *pzWhere */
214690 int nBind = 0; /* Value to return via *pnBind */
214691 const char *zCom = ""; /* Set to ", " later on */
214692 const char *zAnd = ""; /* Set to " AND " later on */
214693 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
214694
214695 if( rc==SQLITE_OK ){
214696 assert( p->zErrmsg==0 );
214697 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
214698 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
214699 );
214700 }
214701
214702 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
214703 int iCid = sqlite3_column_int(pXInfo, 1);
214704 int bDesc = sqlite3_column_int(pXInfo, 3);
214705 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
214706 const char *zCol = 0;
214707 const char *zType;
214708
214709 if( iCid==-2 ){
214710 int iSeq = sqlite3_column_int(pXInfo, 0);
214711 zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
214712 pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
214713 );
214714 zType = "";
214715 }else {
214716 if( iCid<0 ){
214717 /* An integer primary key. If the table has an explicit IPK, use
214718 ** its name. Otherwise, use "rbu_rowid". */
214719 if( pIter->eType==RBU_PK_IPK ){
214720 int i;
214721 for(i=0; pIter->abTblPk[i]==0; i++);
214722 assert( i<pIter->nTblCol );
214723 zCol = pIter->azTblCol[i];
214724 }else if( rbuIsVacuum(p) ){
214725 zCol = "_rowid_";
214726 }else{
214727 zCol = "rbu_rowid";
214728 }
214729 zType = "INTEGER";
214730 }else{
214731 zCol = pIter->azTblCol[iCid];
214732 zType = pIter->azTblType[iCid];
214733 }
214734 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
214735 }
214736
214737 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
214738 const char *zOrder = (bDesc ? " DESC" : "");
214739 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
214740 zImpPK, zCom, nBind, zCol, zOrder
214741 );
214742 }
214743 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
214744 zImpCols, zCom, nBind, zCol, zType, zCollate
214745 );
214746 zWhere = sqlite3_mprintf(
214747 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
214748 );
214749 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
214750 zCom = ", ";
214751 zAnd = " AND ";
214752 nBind++;
214753 }
214754
214755 rc2 = sqlite3_finalize(pXInfo);
214756 if( rc==SQLITE_OK ) rc = rc2;
214757
214758 if( rc!=SQLITE_OK ){
214759 sqlite3_free(zRet);
214760 sqlite3_free(zImpCols);
214761 sqlite3_free(zImpPK);
214762 sqlite3_free(zWhere);
214763 zRet = 0;
214764 zImpCols = 0;
214765 zImpPK = 0;
214766 zWhere = 0;
214767 p->rc = rc;
214768 }
214769
214770 *pzImposterCols = zImpCols;
214771 *pzImposterPk = zImpPK;
214772 *pzWhere = zWhere;
214773 *pnBind = nBind;
214774 return zRet;
214775}
214776
214777/*
214778** Assuming the current table columns are "a", "b" and "c", and the zObj
214779** paramter is passed "old", return a string of the form:
214780**
214781** "old.a, old.b, old.b"
214782**
214783** With the column names escaped.
214784**
214785** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
214786** the text ", old._rowid_" to the returned value.
214787*/
214788static char *rbuObjIterGetOldlist(
214789 sqlite3rbu *p,
214790 RbuObjIter *pIter,
214791 const char *zObj
214792){
214793 char *zList = 0;
214794 if( p->rc==SQLITE_OK && pIter->abIndexed ){
214795 const char *zS = "";
214796 int i;
214797 for(i=0; i<pIter->nTblCol; i++){
214798 if( pIter->abIndexed[i] ){
214799 const char *zCol = pIter->azTblCol[i];
214800 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
214801 }else{
214802 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
214803 }
214804 zS = ", ";
214805 if( zList==0 ){
214806 p->rc = SQLITE_NOMEM;
214807 break;
214808 }
214809 }
214810
214811 /* For a table with implicit rowids, append "old._rowid_" to the list. */
214812 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
214813 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
214814 }
214815 }
214816 return zList;
214817}
214818
214819/*
214820** Return an expression that can be used in a WHERE clause to match the
214821** primary key of the current table. For example, if the table is:
214822**
214823** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
214824**
214825** Return the string:
214826**
214827** "b = ?1 AND c = ?2"
214828*/
214829static char *rbuObjIterGetWhere(
214830 sqlite3rbu *p,
214831 RbuObjIter *pIter
214832){
214833 char *zList = 0;
214834 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
214835 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
214836 }else if( pIter->eType==RBU_PK_EXTERNAL ){
214837 const char *zSep = "";
214838 int i;
214839 for(i=0; i<pIter->nTblCol; i++){
214840 if( pIter->abTblPk[i] ){
214841 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
214842 zSep = " AND ";
214843 }
214844 }
214845 zList = rbuMPrintf(p,
214846 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
214847 );
214848
214849 }else{
214850 const char *zSep = "";
214851 int i;
214852 for(i=0; i<pIter->nTblCol; i++){
214853 if( pIter->abTblPk[i] ){
214854 const char *zCol = pIter->azTblCol[i];
214855 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
214856 zSep = " AND ";
214857 }
214858 }
214859 }
214860 return zList;
214861}
214862
214863/*
214864** The SELECT statement iterating through the keys for the current object
214865** (p->objiter.pSelect) currently points to a valid row. However, there
214866** is something wrong with the rbu_control value in the rbu_control value
214867** stored in the (p->nCol+1)'th column. Set the error code and error message
214868** of the RBU handle to something reflecting this.
214869*/
214870static void rbuBadControlError(sqlite3rbu *p){
214871 p->rc = SQLITE_ERROR;
214872 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
214873}
214874
214875
214876/*
214877** Return a nul-terminated string containing the comma separated list of
214878** assignments that should be included following the "SET" keyword of
214879** an UPDATE statement used to update the table object that the iterator
214880** passed as the second argument currently points to if the rbu_control
214881** column of the data_xxx table entry is set to zMask.
214882**
214883** The memory for the returned string is obtained from sqlite3_malloc().
214884** It is the responsibility of the caller to eventually free it using
214885** sqlite3_free().
214886**
214887** If an OOM error is encountered when allocating space for the new
214888** string, an error code is left in the rbu handle passed as the first
214889** argument and NULL is returned. Or, if an error has already occurred
214890** when this function is called, NULL is returned immediately, without
214891** attempting the allocation or modifying the stored error code.
214892*/
214893static char *rbuObjIterGetSetlist(
214894 sqlite3rbu *p,
214895 RbuObjIter *pIter,
214896 const char *zMask
214897){
214898 char *zList = 0;
214899 if( p->rc==SQLITE_OK ){
214900 int i;
214901
214902 if( (int)strlen(zMask)!=pIter->nTblCol ){
214903 rbuBadControlError(p);
214904 }else{
214905 const char *zSep = "";
214906 for(i=0; i<pIter->nTblCol; i++){
214907 char c = zMask[pIter->aiSrcOrder[i]];
214908 if( c=='x' ){
214909 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
214910 zList, zSep, pIter->azTblCol[i], i+1
214911 );
214912 zSep = ", ";
214913 }
214914 else if( c=='d' ){
214915 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
214916 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
214917 );
214918 zSep = ", ";
214919 }
214920 else if( c=='f' ){
214921 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
214922 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
214923 );
214924 zSep = ", ";
214925 }
214926 }
214927 }
214928 }
214929 return zList;
214930}
214931
214932/*
214933** Return a nul-terminated string consisting of nByte comma separated
214934** "?" expressions. For example, if nByte is 3, return a pointer to
214935** a buffer containing the string "?,?,?".
214936**
214937** The memory for the returned string is obtained from sqlite3_malloc().
214938** It is the responsibility of the caller to eventually free it using
214939** sqlite3_free().
214940**
214941** If an OOM error is encountered when allocating space for the new
214942** string, an error code is left in the rbu handle passed as the first
214943** argument and NULL is returned. Or, if an error has already occurred
214944** when this function is called, NULL is returned immediately, without
214945** attempting the allocation or modifying the stored error code.
214946*/
214947static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
214948 char *zRet = 0;
214949 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
214950
214951 zRet = (char*)rbuMalloc(p, nByte);
214952 if( zRet ){
214953 int i;
214954 for(i=0; i<nBind; i++){
214955 zRet[i*2] = '?';
214956 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
214957 }
214958 }
214959 return zRet;
214960}
214961
214962/*
214963** The iterator currently points to a table (not index) of type
214964** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
214965** declaration for the corresponding imposter table. For example,
214966** if the iterator points to a table created as:
214967**
214968** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
214969**
214970** this function returns:
214971**
214972** PRIMARY KEY("b", "a" DESC)
214973*/
214974static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
214975 char *z = 0;
214976 assert( pIter->zIdx==0 );
214977 if( p->rc==SQLITE_OK ){
214978 const char *zSep = "PRIMARY KEY(";
214979 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
214980 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
214981
214982 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
214983 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
214984 );
214985 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
214986 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
214987 if( zOrig && strcmp(zOrig, "pk")==0 ){
214988 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
214989 if( zIdx ){
214990 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
214991 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
214992 );
214993 }
214994 break;
214995 }
214996 }
214997 rbuFinalize(p, pXList);
214998
214999 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
215000 if( sqlite3_column_int(pXInfo, 5) ){
215001 /* int iCid = sqlite3_column_int(pXInfo, 0); */
215002 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
215003 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
215004 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
215005 zSep = ", ";
215006 }
215007 }
215008 z = rbuMPrintf(p, "%z)", z);
215009 rbuFinalize(p, pXInfo);
215010 }
215011 return z;
215012}
215013
215014/*
215015** This function creates the second imposter table used when writing to
215016** a table b-tree where the table has an external primary key. If the
215017** iterator passed as the second argument does not currently point to
215018** a table (not index) with an external primary key, this function is a
215019** no-op.
215020**
215021** Assuming the iterator does point to a table with an external PK, this
215022** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
215023** used to access that PK index. For example, if the target table is
215024** declared as follows:
215025**
215026** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
215027**
215028** then the imposter table schema is:
215029**
215030** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
215031**
215032*/
215033static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
215034 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
215035 int tnum = pIter->iPkTnum; /* Root page of PK index */
215036 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
215037 const char *zIdx = 0; /* Name of PK index */
215038 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
215039 const char *zComma = "";
215040 char *zCols = 0; /* Used to build up list of table cols */
215041 char *zPk = 0; /* Used to build up table PK declaration */
215042
215043 /* Figure out the name of the primary key index for the current table.
215044 ** This is needed for the argument to "PRAGMA index_xinfo". Set
215045 ** zIdx to point to a nul-terminated string containing this name. */
215046 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
215047 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
215048 );
215049 if( p->rc==SQLITE_OK ){
215050 sqlite3_bind_int(pQuery, 1, tnum);
215051 if( SQLITE_ROW==sqlite3_step(pQuery) ){
215052 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
215053 }
215054 }
215055 if( zIdx ){
215056 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
215057 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
215058 );
215059 }
215060 rbuFinalize(p, pQuery);
215061
215062 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
215063 int bKey = sqlite3_column_int(pXInfo, 5);
215064 if( bKey ){
215065 int iCid = sqlite3_column_int(pXInfo, 1);
215066 int bDesc = sqlite3_column_int(pXInfo, 3);
215067 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
215068 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
215069 iCid, pIter->azTblType[iCid], zCollate
215070 );
215071 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
215072 zComma = ", ";
215073 }
215074 }
215075 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
215076 rbuFinalize(p, pXInfo);
215077
215078 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
215079 rbuMPrintfExec(p, p->dbMain,
215080 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
215081 zCols, zPk
215082 );
215083 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
215084 }
215085}
215086
215087/*
215088** If an error has already occurred when this function is called, it
215089** immediately returns zero (without doing any work). Or, if an error
215090** occurs during the execution of this function, it sets the error code
215091** in the sqlite3rbu object indicated by the first argument and returns
215092** zero.
215093**
215094** The iterator passed as the second argument is guaranteed to point to
215095** a table (not an index) when this function is called. This function
215096** attempts to create any imposter table required to write to the main
215097** table b-tree of the table before returning. Non-zero is returned if
215098** an imposter table are created, or zero otherwise.
215099**
215100** An imposter table is required in all cases except RBU_PK_VTAB. Only
215101** virtual tables are written to directly. The imposter table has the
215102** same schema as the actual target table (less any UNIQUE constraints).
215103** More precisely, the "same schema" means the same columns, types,
215104** collation sequences. For tables that do not have an external PRIMARY
215105** KEY, it also means the same PRIMARY KEY declaration.
215106*/
215107static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
215108 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
215109 int tnum = pIter->iTnum;
215110 const char *zComma = "";
215111 char *zSql = 0;
215112 int iCol;
215113 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
215114
215115 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
215116 const char *zPk = "";
215117 const char *zCol = pIter->azTblCol[iCol];
215118 const char *zColl = 0;
215119
215120 p->rc = sqlite3_table_column_metadata(
215121 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
215122 );
215123
215124 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
215125 /* If the target table column is an "INTEGER PRIMARY KEY", add
215126 ** "PRIMARY KEY" to the imposter table column declaration. */
215127 zPk = "PRIMARY KEY ";
215128 }
215129 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
215130 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
215131 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
215132 );
215133 zComma = ", ";
215134 }
215135
215136 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
215137 char *zPk = rbuWithoutRowidPK(p, pIter);
215138 if( zPk ){
215139 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
215140 }
215141 }
215142
215143 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
215144 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
215145 pIter->zTbl, zSql,
215146 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
215147 );
215148 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
215149 }
215150}
215151
215152/*
215153** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
215154** Specifically a statement of the form:
215155**
215156** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
215157**
215158** The number of bound variables is equal to the number of columns in
215159** the target table, plus one (for the rbu_control column), plus one more
215160** (for the rbu_rowid column) if the target table is an implicit IPK or
215161** virtual table.
215162*/
215163static void rbuObjIterPrepareTmpInsert(
215164 sqlite3rbu *p,
215165 RbuObjIter *pIter,
215166 const char *zCollist,
215167 const char *zRbuRowid
215168){
215169 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
215170 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
215171 if( zBind ){
215172 assert( pIter->pTmpInsert==0 );
215173 p->rc = prepareFreeAndCollectError(
215174 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
215175 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
215176 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
215177 ));
215178 }
215179}
215180
215181static void rbuTmpInsertFunc(
215182 sqlite3_context *pCtx,
215183 int nVal,
215184 sqlite3_value **apVal
215185){
215186 sqlite3rbu *p = sqlite3_user_data(pCtx);
215187 int rc = SQLITE_OK;
215188 int i;
215189
215190 assert( sqlite3_value_int(apVal[0])!=0
215191 || p->objiter.eType==RBU_PK_EXTERNAL
215192 || p->objiter.eType==RBU_PK_NONE
215193 );
215194 if( sqlite3_value_int(apVal[0])!=0 ){
215195 p->nPhaseOneStep += p->objiter.nIndex;
215196 }
215197
215198 for(i=0; rc==SQLITE_OK && i<nVal; i++){
215199 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
215200 }
215201 if( rc==SQLITE_OK ){
215202 sqlite3_step(p->objiter.pTmpInsert);
215203 rc = sqlite3_reset(p->objiter.pTmpInsert);
215204 }
215205
215206 if( rc!=SQLITE_OK ){
215207 sqlite3_result_error_code(pCtx, rc);
215208 }
215209}
215210
215211static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
215212 sqlite3_stmt *pStmt = 0;
215213 int rc = p->rc;
215214 char *zRet = 0;
215215
215216 assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 );
215217
215218 if( rc==SQLITE_OK ){
215219 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
215220 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
215221 );
215222 }
215223 if( rc==SQLITE_OK ){
215224 int rc2;
215225 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
215226 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
215227 char *zSql = (char*)sqlite3_column_text(pStmt, 0);
215228 if( zSql ){
215229 pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
215230 }
215231 if( zSql ){
215232 int nParen = 0; /* Number of open parenthesis */
215233 int i;
215234 int iIdxCol = 0;
215235 int nIdxAlloc = 0;
215236 for(i=0; zSql[i]; i++){
215237 char c = zSql[i];
215238
215239 /* If necessary, grow the pIter->aIdxCol[] array */
215240 if( iIdxCol==nIdxAlloc ){
215241 RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
215242 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
215243 );
215244 if( aIdxCol==0 ){
215245 rc = SQLITE_NOMEM;
215246 break;
215247 }
215248 pIter->aIdxCol = aIdxCol;
215249 nIdxAlloc += 16;
215250 }
215251
215252 if( c=='(' ){
215253 if( nParen==0 ){
215254 assert( iIdxCol==0 );
215255 pIter->aIdxCol[0].zSpan = &zSql[i+1];
215256 }
215257 nParen++;
215258 }
215259 else if( c==')' ){
215260 nParen--;
215261 if( nParen==0 ){
215262 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
215263 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
215264 i++;
215265 break;
215266 }
215267 }else if( c==',' && nParen==1 ){
215268 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
215269 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
215270 pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
215271 }else if( c=='"' || c=='\'' || c=='`' ){
215272 for(i++; 1; i++){
215273 if( zSql[i]==c ){
215274 if( zSql[i+1]!=c ) break;
215275 i++;
215276 }
215277 }
215278 }else if( c=='[' ){
215279 for(i++; 1; i++){
215280 if( zSql[i]==']' ) break;
215281 }
215282 }else if( c=='-' && zSql[i+1]=='-' ){
215283 for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
215284 if( zSql[i]=='\0' ) break;
215285 }else if( c=='/' && zSql[i+1]=='*' ){
215286 for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
215287 if( zSql[i]=='\0' ) break;
215288 i++;
215289 }
215290 }
215291 if( zSql[i] ){
215292 zRet = rbuStrndup(&zSql[i], &rc);
215293 }
215294 pIter->nIdxCol = iIdxCol;
215295 }
215296 }
215297
215298 rc2 = sqlite3_finalize(pStmt);
215299 if( rc==SQLITE_OK ) rc = rc2;
215300 }
215301
215302 p->rc = rc;
215303 return zRet;
215304}
215305
215306/*
215307** Ensure that the SQLite statement handles required to update the
215308** target database object currently indicated by the iterator passed
215309** as the second argument are available.
215310*/
215311static int rbuObjIterPrepareAll(
215312 sqlite3rbu *p,
215313 RbuObjIter *pIter,
215314 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
215315){
215316 assert( pIter->bCleanup==0 );
215317 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
215318 const int tnum = pIter->iTnum;
215319 char *zCollist = 0; /* List of indexed columns */
215320 char **pz = &p->zErrmsg;
215321 const char *zIdx = pIter->zIdx;
215322 char *zLimit = 0;
215323
215324 if( nOffset ){
215325 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
215326 if( !zLimit ) p->rc = SQLITE_NOMEM;
215327 }
215328
215329 if( zIdx ){
215330 const char *zTbl = pIter->zTbl;
215331 char *zImposterCols = 0; /* Columns for imposter table */
215332 char *zImposterPK = 0; /* Primary key declaration for imposter */
215333 char *zWhere = 0; /* WHERE clause on PK columns */
215334 char *zBind = 0;
215335 char *zPart = 0;
215336 int nBind = 0;
215337
215338 assert( pIter->eType!=RBU_PK_VTAB );
215339 zPart = rbuObjIterGetIndexWhere(p, pIter);
215340 zCollist = rbuObjIterGetIndexCols(
215341 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
215342 );
215343 zBind = rbuObjIterGetBindlist(p, nBind);
215344
215345 /* Create the imposter table used to write to this index. */
215346 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
215347 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
215348 rbuMPrintfExec(p, p->dbMain,
215349 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
215350 zTbl, zImposterCols, zImposterPK
215351 );
215352 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
215353
215354 /* Create the statement to insert index entries */
215355 pIter->nCol = nBind;
215356 if( p->rc==SQLITE_OK ){
215357 p->rc = prepareFreeAndCollectError(
215358 p->dbMain, &pIter->pInsert, &p->zErrmsg,
215359 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
215360 );
215361 }
215362
215363 /* And to delete index entries */
215364 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
215365 p->rc = prepareFreeAndCollectError(
215366 p->dbMain, &pIter->pDelete, &p->zErrmsg,
215367 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
215368 );
215369 }
215370
215371 /* Create the SELECT statement to read keys in sorted order */
215372 if( p->rc==SQLITE_OK ){
215373 char *zSql;
215374 if( rbuIsVacuum(p) ){
215375 char *zStart = 0;
215376 if( nOffset ){
215377 zStart = rbuVacuumIndexStart(p, pIter);
215378 if( zStart ){
215379 sqlite3_free(zLimit);
215380 zLimit = 0;
215381 }
215382 }
215383
215384 zSql = sqlite3_mprintf(
215385 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
215386 zCollist,
215387 pIter->zDataTbl,
215388 zPart,
215389 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
215390 zCollist, zLimit
215391 );
215392 sqlite3_free(zStart);
215393 }else
215394
215395 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
215396 zSql = sqlite3_mprintf(
215397 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
215398 zCollist, p->zStateDb, pIter->zDataTbl,
215399 zPart, zCollist, zLimit
215400 );
215401 }else{
215402 zSql = sqlite3_mprintf(
215403 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
215404 "UNION ALL "
215405 "SELECT %s, rbu_control FROM '%q' "
215406 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
215407 "ORDER BY %s%s",
215408 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
215409 zCollist, pIter->zDataTbl,
215410 zPart,
215411 (zPart ? "AND" : "WHERE"),
215412 zCollist, zLimit
215413 );
215414 }
215415 if( p->rc==SQLITE_OK ){
215416 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
215417 }else{
215418 sqlite3_free(zSql);
215419 }
215420 }
215421
215422 sqlite3_free(zImposterCols);
215423 sqlite3_free(zImposterPK);
215424 sqlite3_free(zWhere);
215425 sqlite3_free(zBind);
215426 sqlite3_free(zPart);
215427 }else{
215428 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
215429 ||(pIter->eType==RBU_PK_NONE)
215430 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
215431 const char *zTbl = pIter->zTbl; /* Table this step applies to */
215432 const char *zWrite; /* Imposter table name */
215433
215434 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
215435 char *zWhere = rbuObjIterGetWhere(p, pIter);
215436 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
215437 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
215438
215439 zCollist = rbuObjIterGetCollist(p, pIter);
215440 pIter->nCol = pIter->nTblCol;
215441
215442 /* Create the imposter table or tables (if required). */
215443 rbuCreateImposterTable(p, pIter);
215444 rbuCreateImposterTable2(p, pIter);
215445 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
215446
215447 /* Create the INSERT statement to write to the target PK b-tree */
215448 if( p->rc==SQLITE_OK ){
215449 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
215450 sqlite3_mprintf(
215451 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
215452 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
215453 )
215454 );
215455 }
215456
215457 /* Create the DELETE statement to write to the target PK b-tree.
215458 ** Because it only performs INSERT operations, this is not required for
215459 ** an rbu vacuum handle. */
215460 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
215461 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
215462 sqlite3_mprintf(
215463 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
215464 )
215465 );
215466 }
215467
215468 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
215469 const char *zRbuRowid = "";
215470 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
215471 zRbuRowid = ", rbu_rowid";
215472 }
215473
215474 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
215475 rbuMPrintfExec(p, p->dbRbu,
215476 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
215477 "SELECT *%s FROM '%q' WHERE 0;"
215478 , p->zStateDb, pIter->zDataTbl
215479 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
215480 , pIter->zDataTbl
215481 );
215482
215483 rbuMPrintfExec(p, p->dbMain,
215484 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
215485 "BEGIN "
215486 " SELECT rbu_tmp_insert(3, %s);"
215487 "END;"
215488
215489 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
215490 "BEGIN "
215491 " SELECT rbu_tmp_insert(3, %s);"
215492 "END;"
215493
215494 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
215495 "BEGIN "
215496 " SELECT rbu_tmp_insert(4, %s);"
215497 "END;",
215498 zWrite, zTbl, zOldlist,
215499 zWrite, zTbl, zOldlist,
215500 zWrite, zTbl, zNewlist
215501 );
215502
215503 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
215504 rbuMPrintfExec(p, p->dbMain,
215505 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
215506 "BEGIN "
215507 " SELECT rbu_tmp_insert(0, %s);"
215508 "END;",
215509 zWrite, zTbl, zNewlist
215510 );
215511 }
215512
215513 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
215514 }
215515
215516 /* Create the SELECT statement to read keys from data_xxx */
215517 if( p->rc==SQLITE_OK ){
215518 const char *zRbuRowid = "";
215519 char *zStart = 0;
215520 char *zOrder = 0;
215521 if( bRbuRowid ){
215522 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
215523 }
215524
215525 if( rbuIsVacuum(p) ){
215526 if( nOffset ){
215527 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
215528 if( zStart ){
215529 sqlite3_free(zLimit);
215530 zLimit = 0;
215531 }
215532 }
215533 if( bRbuRowid ){
215534 zOrder = rbuMPrintf(p, "_rowid_");
215535 }else{
215536 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
215537 }
215538 }
215539
215540 if( p->rc==SQLITE_OK ){
215541 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
215542 sqlite3_mprintf(
215543 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
215544 zCollist,
215545 (rbuIsVacuum(p) ? "0 AS " : ""),
215546 zRbuRowid,
215547 pIter->zDataTbl, (zStart ? zStart : ""),
215548 (zOrder ? "ORDER BY" : ""), zOrder,
215549 zLimit
215550 )
215551 );
215552 }
215553 sqlite3_free(zStart);
215554 sqlite3_free(zOrder);
215555 }
215556
215557 sqlite3_free(zWhere);
215558 sqlite3_free(zOldlist);
215559 sqlite3_free(zNewlist);
215560 sqlite3_free(zBindings);
215561 }
215562 sqlite3_free(zCollist);
215563 sqlite3_free(zLimit);
215564 }
215565
215566 return p->rc;
215567}
215568
215569/*
215570** Set output variable *ppStmt to point to an UPDATE statement that may
215571** be used to update the imposter table for the main table b-tree of the
215572** table object that pIter currently points to, assuming that the
215573** rbu_control column of the data_xyz table contains zMask.
215574**
215575** If the zMask string does not specify any columns to update, then this
215576** is not an error. Output variable *ppStmt is set to NULL in this case.
215577*/
215578static int rbuGetUpdateStmt(
215579 sqlite3rbu *p, /* RBU handle */
215580 RbuObjIter *pIter, /* Object iterator */
215581 const char *zMask, /* rbu_control value ('x.x.') */
215582 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
215583){
215584 RbuUpdateStmt **pp;
215585 RbuUpdateStmt *pUp = 0;
215586 int nUp = 0;
215587
215588 /* In case an error occurs */
215589 *ppStmt = 0;
215590
215591 /* Search for an existing statement. If one is found, shift it to the front
215592 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
215593 ** to the number of statements currently in the cache and pUp to the
215594 ** last object in the list. */
215595 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
215596 pUp = *pp;
215597 if( strcmp(pUp->zMask, zMask)==0 ){
215598 *pp = pUp->pNext;
215599 pUp->pNext = pIter->pRbuUpdate;
215600 pIter->pRbuUpdate = pUp;
215601 *ppStmt = pUp->pUpdate;
215602 return SQLITE_OK;
215603 }
215604 nUp++;
215605 }
215606 assert( pUp==0 || pUp->pNext==0 );
215607
215608 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
215609 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
215610 *pp = 0;
215611 sqlite3_finalize(pUp->pUpdate);
215612 pUp->pUpdate = 0;
215613 }else{
215614 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
215615 }
215616
215617 if( pUp ){
215618 char *zWhere = rbuObjIterGetWhere(p, pIter);
215619 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
215620 char *zUpdate = 0;
215621
215622 pUp->zMask = (char*)&pUp[1];
215623 memcpy(pUp->zMask, zMask, pIter->nTblCol);
215624 pUp->pNext = pIter->pRbuUpdate;
215625 pIter->pRbuUpdate = pUp;
215626
215627 if( zSet ){
215628 const char *zPrefix = "";
215629
215630 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
215631 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
215632 zPrefix, pIter->zTbl, zSet, zWhere
215633 );
215634 p->rc = prepareFreeAndCollectError(
215635 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
215636 );
215637 *ppStmt = pUp->pUpdate;
215638 }
215639 sqlite3_free(zWhere);
215640 sqlite3_free(zSet);
215641 }
215642
215643 return p->rc;
215644}
215645
215646static sqlite3 *rbuOpenDbhandle(
215647 sqlite3rbu *p,
215648 const char *zName,
215649 int bUseVfs
215650){
215651 sqlite3 *db = 0;
215652 if( p->rc==SQLITE_OK ){
215653 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
215654 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
215655 if( p->rc ){
215656 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
215657 sqlite3_close(db);
215658 db = 0;
215659 }
215660 }
215661 return db;
215662}
215663
215664/*
215665** Free an RbuState object allocated by rbuLoadState().
215666*/
215667static void rbuFreeState(RbuState *p){
215668 if( p ){
215669 sqlite3_free(p->zTbl);
215670 sqlite3_free(p->zDataTbl);
215671 sqlite3_free(p->zIdx);
215672 sqlite3_free(p);
215673 }
215674}
215675
215676/*
215677** Allocate an RbuState object and load the contents of the rbu_state
215678** table into it. Return a pointer to the new object. It is the
215679** responsibility of the caller to eventually free the object using
215680** sqlite3_free().
215681**
215682** If an error occurs, leave an error code and message in the rbu handle
215683** and return NULL.
215684*/
215685static RbuState *rbuLoadState(sqlite3rbu *p){
215686 RbuState *pRet = 0;
215687 sqlite3_stmt *pStmt = 0;
215688 int rc;
215689 int rc2;
215690
215691 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
215692 if( pRet==0 ) return 0;
215693
215694 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
215695 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
215696 );
215697 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
215698 switch( sqlite3_column_int(pStmt, 0) ){
215699 case RBU_STATE_STAGE:
215700 pRet->eStage = sqlite3_column_int(pStmt, 1);
215701 if( pRet->eStage!=RBU_STAGE_OAL
215702 && pRet->eStage!=RBU_STAGE_MOVE
215703 && pRet->eStage!=RBU_STAGE_CKPT
215704 ){
215705 p->rc = SQLITE_CORRUPT;
215706 }
215707 break;
215708
215709 case RBU_STATE_TBL:
215710 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
215711 break;
215712
215713 case RBU_STATE_IDX:
215714 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
215715 break;
215716
215717 case RBU_STATE_ROW:
215718 pRet->nRow = sqlite3_column_int(pStmt, 1);
215719 break;
215720
215721 case RBU_STATE_PROGRESS:
215722 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
215723 break;
215724
215725 case RBU_STATE_CKPT:
215726 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
215727 break;
215728
215729 case RBU_STATE_COOKIE:
215730 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
215731 break;
215732
215733 case RBU_STATE_OALSZ:
215734 pRet->iOalSz = sqlite3_column_int64(pStmt, 1);
215735 break;
215736
215737 case RBU_STATE_PHASEONESTEP:
215738 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
215739 break;
215740
215741 case RBU_STATE_DATATBL:
215742 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
215743 break;
215744
215745 default:
215746 rc = SQLITE_CORRUPT;
215747 break;
215748 }
215749 }
215750 rc2 = sqlite3_finalize(pStmt);
215751 if( rc==SQLITE_OK ) rc = rc2;
215752
215753 p->rc = rc;
215754 return pRet;
215755}
215756
215757
215758/*
215759** Open the database handle and attach the RBU database as "rbu". If an
215760** error occurs, leave an error code and message in the RBU handle.
215761**
215762** If argument dbMain is not NULL, then it is a database handle already
215763** open on the target database. Use this handle instead of opening a new
215764** one.
215765*/
215766static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){
215767 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
215768 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
215769 assert( dbMain==0 || rbuIsVacuum(p)==0 );
215770
215771 /* Open the RBU database */
215772 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
215773 p->dbMain = dbMain;
215774
215775 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
215776 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
215777 if( p->zState==0 ){
215778 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
215779 p->zState = rbuMPrintf(p, "file:///%s-vacuum?modeof=%s", zFile, zFile);
215780 }
215781 }
215782
215783 /* If using separate RBU and state databases, attach the state database to
215784 ** the RBU db handle now. */
215785 if( p->zState ){
215786 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
215787 memcpy(p->zStateDb, "stat", 4);
215788 }else{
215789 memcpy(p->zStateDb, "main", 4);
215790 }
215791
215792#if 0
215793 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
215794 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
215795 }
215796#endif
215797
215798 /* If it has not already been created, create the rbu_state table */
215799 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
215800
215801#if 0
215802 if( rbuIsVacuum(p) ){
215803 if( p->rc==SQLITE_OK ){
215804 int rc2;
215805 int bOk = 0;
215806 sqlite3_stmt *pCnt = 0;
215807 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
215808 "SELECT count(*) FROM stat.sqlite_schema"
215809 );
215810 if( p->rc==SQLITE_OK
215811 && sqlite3_step(pCnt)==SQLITE_ROW
215812 && 1==sqlite3_column_int(pCnt, 0)
215813 ){
215814 bOk = 1;
215815 }
215816 rc2 = sqlite3_finalize(pCnt);
215817 if( p->rc==SQLITE_OK ) p->rc = rc2;
215818
215819 if( p->rc==SQLITE_OK && bOk==0 ){
215820 p->rc = SQLITE_ERROR;
215821 p->zErrmsg = sqlite3_mprintf("invalid state database");
215822 }
215823
215824 if( p->rc==SQLITE_OK ){
215825 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
215826 }
215827 }
215828 }
215829#endif
215830
215831 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
215832 int bOpen = 0;
215833 int rc;
215834 p->nRbu = 0;
215835 p->pRbuFd = 0;
215836 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
215837 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
215838 if( p->eStage>=RBU_STAGE_MOVE ){
215839 bOpen = 1;
215840 }else{
215841 RbuState *pState = rbuLoadState(p);
215842 if( pState ){
215843 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
215844 rbuFreeState(pState);
215845 }
215846 }
215847 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
215848 }
215849
215850 p->eStage = 0;
215851 if( p->rc==SQLITE_OK && p->dbMain==0 ){
215852 if( !rbuIsVacuum(p) ){
215853 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
215854 }else if( p->pRbuFd->pWalFd ){
215855 if( pbRetry ){
215856 p->pRbuFd->bNolock = 0;
215857 sqlite3_close(p->dbRbu);
215858 sqlite3_close(p->dbMain);
215859 p->dbMain = 0;
215860 p->dbRbu = 0;
215861 *pbRetry = 1;
215862 return;
215863 }
215864 p->rc = SQLITE_ERROR;
215865 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
215866 }else{
215867 char *zTarget;
215868 char *zExtra = 0;
215869 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
215870 zExtra = &p->zRbu[5];
215871 while( *zExtra ){
215872 if( *zExtra++=='?' ) break;
215873 }
215874 if( *zExtra=='\0' ) zExtra = 0;
215875 }
215876
215877 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
215878 sqlite3_db_filename(p->dbRbu, "main"),
215879 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
215880 );
215881
215882 if( zTarget==0 ){
215883 p->rc = SQLITE_NOMEM;
215884 return;
215885 }
215886 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
215887 sqlite3_free(zTarget);
215888 }
215889 }
215890
215891 if( p->rc==SQLITE_OK ){
215892 p->rc = sqlite3_create_function(p->dbMain,
215893 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
215894 );
215895 }
215896
215897 if( p->rc==SQLITE_OK ){
215898 p->rc = sqlite3_create_function(p->dbMain,
215899 "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
215900 );
215901 }
215902
215903 if( p->rc==SQLITE_OK ){
215904 p->rc = sqlite3_create_function(p->dbRbu,
215905 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
215906 );
215907 }
215908
215909 if( p->rc==SQLITE_OK ){
215910 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
215911 }
215912 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
215913
215914 /* Mark the database file just opened as an RBU target database. If
215915 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
215916 ** This is an error. */
215917 if( p->rc==SQLITE_OK ){
215918 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
215919 }
215920
215921 if( p->rc==SQLITE_NOTFOUND ){
215922 p->rc = SQLITE_ERROR;
215923 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
215924 }
215925}
215926
215927/*
215928** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
215929** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
215930**
215931** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
215932** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
215933** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
215934** three characters, then shorten the suffix on z[] to be the last three
215935** characters of the original suffix.
215936**
215937** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
215938** do the suffix shortening regardless of URI parameter.
215939**
215940** Examples:
215941**
215942** test.db-journal => test.nal
215943** test.db-wal => test.wal
215944** test.db-shm => test.shm
215945** test.db-mj7f3319fa => test.9fa
215946*/
215947static void rbuFileSuffix3(const char *zBase, char *z){
215948#ifdef SQLITE_ENABLE_8_3_NAMES
215949#if SQLITE_ENABLE_8_3_NAMES<2
215950 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
215951#endif
215952 {
215953 int i, sz;
215954 sz = (int)strlen(z)&0xffffff;
215955 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
215956 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
215957 }
215958#endif
215959}
215960
215961/*
215962** Return the current wal-index header checksum for the target database
215963** as a 64-bit integer.
215964**
215965** The checksum is store in the first page of xShmMap memory as an 8-byte
215966** blob starting at byte offset 40.
215967*/
215968static i64 rbuShmChecksum(sqlite3rbu *p){
215969 i64 iRet = 0;
215970 if( p->rc==SQLITE_OK ){
215971 sqlite3_file *pDb = p->pTargetFd->pReal;
215972 u32 volatile *ptr;
215973 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
215974 if( p->rc==SQLITE_OK ){
215975 iRet = ((i64)ptr[10] << 32) + ptr[11];
215976 }
215977 }
215978 return iRet;
215979}
215980
215981/*
215982** This function is called as part of initializing or reinitializing an
215983** incremental checkpoint.
215984**
215985** It populates the sqlite3rbu.aFrame[] array with the set of
215986** (wal frame -> db page) copy operations required to checkpoint the
215987** current wal file, and obtains the set of shm locks required to safely
215988** perform the copy operations directly on the file-system.
215989**
215990** If argument pState is not NULL, then the incremental checkpoint is
215991** being resumed. In this case, if the checksum of the wal-index-header
215992** following recovery is not the same as the checksum saved in the RbuState
215993** object, then the rbu handle is set to DONE state. This occurs if some
215994** other client appends a transaction to the wal file in the middle of
215995** an incremental checkpoint.
215996*/
215997static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
215998
215999 /* If pState is NULL, then the wal file may not have been opened and
216000 ** recovered. Running a read-statement here to ensure that doing so
216001 ** does not interfere with the "capture" process below. */
216002 if( pState==0 ){
216003 p->eStage = 0;
216004 if( p->rc==SQLITE_OK ){
216005 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
216006 }
216007 }
216008
216009 /* Assuming no error has occurred, run a "restart" checkpoint with the
216010 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
216011 ** special behaviour in the rbu VFS:
216012 **
216013 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
216014 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
216015 ** proceed with running a passive checkpoint instead of failing).
216016 **
216017 ** * Attempts to read from the *-wal file or write to the database file
216018 ** do not perform any IO. Instead, the frame/page combinations that
216019 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
216020 ** array.
216021 **
216022 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
216023 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
216024 ** no-ops. These locks will not be released until the connection
216025 ** is closed.
216026 **
216027 ** * Attempting to xSync() the database file causes an SQLITE_NOTICE
216028 ** error.
216029 **
216030 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
216031 ** checkpoint below fails with SQLITE_NOTICE, and leaves the aFrame[]
216032 ** array populated with a set of (frame -> page) mappings. Because the
216033 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
216034 ** data from the wal file into the database file according to the
216035 ** contents of aFrame[].
216036 */
216037 if( p->rc==SQLITE_OK ){
216038 int rc2;
216039 p->eStage = RBU_STAGE_CAPTURE;
216040 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
216041 if( rc2!=SQLITE_NOTICE ) p->rc = rc2;
216042 }
216043
216044 if( p->rc==SQLITE_OK && p->nFrame>0 ){
216045 p->eStage = RBU_STAGE_CKPT;
216046 p->nStep = (pState ? pState->nRow : 0);
216047 p->aBuf = rbuMalloc(p, p->pgsz);
216048 p->iWalCksum = rbuShmChecksum(p);
216049 }
216050
216051 if( p->rc==SQLITE_OK ){
216052 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
216053 p->rc = SQLITE_DONE;
216054 p->eStage = RBU_STAGE_DONE;
216055 }else{
216056 int nSectorSize;
216057 sqlite3_file *pDb = p->pTargetFd->pReal;
216058 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
216059 assert( p->nPagePerSector==0 );
216060 nSectorSize = pDb->pMethods->xSectorSize(pDb);
216061 if( nSectorSize>p->pgsz ){
216062 p->nPagePerSector = nSectorSize / p->pgsz;
216063 }else{
216064 p->nPagePerSector = 1;
216065 }
216066
216067 /* Call xSync() on the wal file. This causes SQLite to sync the
216068 ** directory in which the target database and the wal file reside, in
216069 ** case it has not been synced since the rename() call in
216070 ** rbuMoveOalFile(). */
216071 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
216072 }
216073 }
216074}
216075
216076/*
216077** Called when iAmt bytes are read from offset iOff of the wal file while
216078** the rbu object is in capture mode. Record the frame number of the frame
216079** being read in the aFrame[] array.
216080*/
216081static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
216082 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
216083 u32 iFrame;
216084
216085 if( pRbu->mLock!=mReq ){
216086 pRbu->rc = SQLITE_BUSY;
216087 return SQLITE_NOTICE_RBU;
216088 }
216089
216090 pRbu->pgsz = iAmt;
216091 if( pRbu->nFrame==pRbu->nFrameAlloc ){
216092 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
216093 RbuFrame *aNew;
216094 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
216095 if( aNew==0 ) return SQLITE_NOMEM;
216096 pRbu->aFrame = aNew;
216097 pRbu->nFrameAlloc = nNew;
216098 }
216099
216100 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
216101 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
216102 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
216103 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
216104 pRbu->nFrame++;
216105 return SQLITE_OK;
216106}
216107
216108/*
216109** Called when a page of data is written to offset iOff of the database
216110** file while the rbu handle is in capture mode. Record the page number
216111** of the page being written in the aFrame[] array.
216112*/
216113static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
216114 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
216115 return SQLITE_OK;
216116}
216117
216118/*
216119** This is called as part of an incremental checkpoint operation. Copy
216120** a single frame of data from the wal file into the database file, as
216121** indicated by the RbuFrame object.
216122*/
216123static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
216124 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
216125 sqlite3_file *pDb = p->pTargetFd->pReal;
216126 i64 iOff;
216127
216128 assert( p->rc==SQLITE_OK );
216129 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
216130 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
216131 if( p->rc ) return;
216132
216133 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
216134 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
216135}
216136
216137/*
216138** This value is copied from the definition of ZIPVFS_CTRL_FILE_POINTER
216139** in zipvfs.h.
216140*/
216141#define RBU_ZIPVFS_CTRL_FILE_POINTER 230439
216142
216143/*
216144** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
216145** successful, or an SQLite error code otherwise.
216146*/
216147static int rbuLockDatabase(sqlite3 *db){
216148 int rc = SQLITE_OK;
216149 sqlite3_file *fd = 0;
216150
216151 sqlite3_file_control(db, "main", RBU_ZIPVFS_CTRL_FILE_POINTER, &fd);
216152 if( fd ){
216153 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &fd);
216154 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED);
216155 if( rc==SQLITE_OK ){
216156 rc = fd->pMethods->xUnlock(fd, SQLITE_LOCK_NONE);
216157 }
216158 sqlite3_file_control(db, "main", RBU_ZIPVFS_CTRL_FILE_POINTER, &fd);
216159 }else{
216160 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &fd);
216161 }
216162
216163 if( rc==SQLITE_OK && fd->pMethods ){
216164 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED);
216165 if( rc==SQLITE_OK ){
216166 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE);
216167 }
216168 }
216169 return rc;
216170}
216171
216172/*
216173** Return true if the database handle passed as the only argument
216174** was opened with the rbu_exclusive_checkpoint=1 URI parameter
216175** specified. Or false otherwise.
216176*/
216177static int rbuExclusiveCheckpoint(sqlite3 *db){
216178 const char *zUri = sqlite3_db_filename(db, 0);
216179 return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0);
216180}
216181
216182#if defined(_WIN32_WCE)
216183static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
216184 int nChar;
216185 LPWSTR zWideFilename;
216186
216187 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
216188 if( nChar==0 ){
216189 return 0;
216190 }
216191 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
216192 if( zWideFilename==0 ){
216193 return 0;
216194 }
216195 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
216196 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
216197 nChar);
216198 if( nChar==0 ){
216199 sqlite3_free(zWideFilename);
216200 zWideFilename = 0;
216201 }
216202 return zWideFilename;
216203}
216204#endif
216205
216206/*
216207** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
216208** on the database file. This proc moves the *-oal file to the *-wal path,
216209** then reopens the database file (this time in vanilla, non-oal, WAL mode).
216210** If an error occurs, leave an error code and error message in the rbu
216211** handle.
216212*/
216213static void rbuMoveOalFile(sqlite3rbu *p){
216214 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
216215 const char *zMove = zBase;
216216 char *zOal;
216217 char *zWal;
216218
216219 if( rbuIsVacuum(p) ){
216220 zMove = sqlite3_db_filename(p->dbRbu, "main");
216221 }
216222 zOal = sqlite3_mprintf("%s-oal", zMove);
216223 zWal = sqlite3_mprintf("%s-wal", zMove);
216224
216225 assert( p->eStage==RBU_STAGE_MOVE );
216226 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
216227 if( zWal==0 || zOal==0 ){
216228 p->rc = SQLITE_NOMEM;
216229 }else{
216230 /* Move the *-oal file to *-wal. At this point connection p->db is
216231 ** holding a SHARED lock on the target database file (because it is
216232 ** in WAL mode). So no other connection may be writing the db.
216233 **
216234 ** In order to ensure that there are no database readers, an EXCLUSIVE
216235 ** lock is obtained here before the *-oal is moved to *-wal.
216236 */
216237 sqlite3 *dbMain = 0;
216238 rbuFileSuffix3(zBase, zWal);
216239 rbuFileSuffix3(zBase, zOal);
216240
216241 /* Re-open the databases. */
216242 rbuObjIterFinalize(&p->objiter);
216243 sqlite3_close(p->dbRbu);
216244 sqlite3_close(p->dbMain);
216245 p->dbMain = 0;
216246 p->dbRbu = 0;
216247
216248 dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
216249 if( dbMain ){
216250 assert( p->rc==SQLITE_OK );
216251 p->rc = rbuLockDatabase(dbMain);
216252 }
216253
216254 if( p->rc==SQLITE_OK ){
216255 p->rc = p->xRename(p->pRenameArg, zOal, zWal);
216256 }
216257
216258 if( p->rc!=SQLITE_OK
216259 || rbuIsVacuum(p)
216260 || rbuExclusiveCheckpoint(dbMain)==0
216261 ){
216262 sqlite3_close(dbMain);
216263 dbMain = 0;
216264 }
216265
216266 if( p->rc==SQLITE_OK ){
216267 rbuOpenDatabase(p, dbMain, 0);
216268 rbuSetupCheckpoint(p, 0);
216269 }
216270 }
216271
216272 sqlite3_free(zWal);
216273 sqlite3_free(zOal);
216274}
216275
216276/*
216277** The SELECT statement iterating through the keys for the current object
216278** (p->objiter.pSelect) currently points to a valid row. This function
216279** determines the type of operation requested by this row and returns
216280** one of the following values to indicate the result:
216281**
216282** * RBU_INSERT
216283** * RBU_DELETE
216284** * RBU_IDX_DELETE
216285** * RBU_UPDATE
216286**
216287** If RBU_UPDATE is returned, then output variable *pzMask is set to
216288** point to the text value indicating the columns to update.
216289**
216290** If the rbu_control field contains an invalid value, an error code and
216291** message are left in the RBU handle and zero returned.
216292*/
216293static int rbuStepType(sqlite3rbu *p, const char **pzMask){
216294 int iCol = p->objiter.nCol; /* Index of rbu_control column */
216295 int res = 0; /* Return value */
216296
216297 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
216298 case SQLITE_INTEGER: {
216299 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
216300 switch( iVal ){
216301 case 0: res = RBU_INSERT; break;
216302 case 1: res = RBU_DELETE; break;
216303 case 2: res = RBU_REPLACE; break;
216304 case 3: res = RBU_IDX_DELETE; break;
216305 case 4: res = RBU_IDX_INSERT; break;
216306 }
216307 break;
216308 }
216309
216310 case SQLITE_TEXT: {
216311 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
216312 if( z==0 ){
216313 p->rc = SQLITE_NOMEM;
216314 }else{
216315 *pzMask = (const char*)z;
216316 }
216317 res = RBU_UPDATE;
216318
216319 break;
216320 }
216321
216322 default:
216323 break;
216324 }
216325
216326 if( res==0 ){
216327 rbuBadControlError(p);
216328 }
216329 return res;
216330}
216331
216332#ifdef SQLITE_DEBUG
216333/*
216334** Assert that column iCol of statement pStmt is named zName.
216335*/
216336static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
216337 const char *zCol = sqlite3_column_name(pStmt, iCol);
216338 assert( 0==sqlite3_stricmp(zName, zCol) );
216339}
216340#else
216341# define assertColumnName(x,y,z)
216342#endif
216343
216344/*
216345** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
216346** RBU_IDX_DELETE. This function performs the work of a single
216347** sqlite3rbu_step() call for the type of operation specified by eType.
216348*/
216349static void rbuStepOneOp(sqlite3rbu *p, int eType){
216350 RbuObjIter *pIter = &p->objiter;
216351 sqlite3_value *pVal;
216352 sqlite3_stmt *pWriter;
216353 int i;
216354
216355 assert( p->rc==SQLITE_OK );
216356 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
216357 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
216358 || eType==RBU_INSERT || eType==RBU_IDX_INSERT
216359 );
216360
216361 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
216362 ** statement below does actually delete a row, nPhaseOneStep will be
216363 ** incremented by the same amount when SQL function rbu_tmp_insert()
216364 ** is invoked by the trigger. */
216365 if( eType==RBU_DELETE ){
216366 p->nPhaseOneStep -= p->objiter.nIndex;
216367 }
216368
216369 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
216370 pWriter = pIter->pDelete;
216371 }else{
216372 pWriter = pIter->pInsert;
216373 }
216374
216375 for(i=0; i<pIter->nCol; i++){
216376 /* If this is an INSERT into a table b-tree and the table has an
216377 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
216378 ** to write a NULL into the IPK column. That is not permitted. */
216379 if( eType==RBU_INSERT
216380 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
216381 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
216382 ){
216383 p->rc = SQLITE_MISMATCH;
216384 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
216385 return;
216386 }
216387
216388 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
216389 continue;
216390 }
216391
216392 pVal = sqlite3_column_value(pIter->pSelect, i);
216393 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
216394 if( p->rc ) return;
216395 }
216396 if( pIter->zIdx==0 ){
216397 if( pIter->eType==RBU_PK_VTAB
216398 || pIter->eType==RBU_PK_NONE
216399 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
216400 ){
216401 /* For a virtual table, or a table with no primary key, the
216402 ** SELECT statement is:
216403 **
216404 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
216405 **
216406 ** Hence column_value(pIter->nCol+1).
216407 */
216408 assertColumnName(pIter->pSelect, pIter->nCol+1,
216409 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
216410 );
216411 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
216412 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
216413 }
216414 }
216415 if( p->rc==SQLITE_OK ){
216416 sqlite3_step(pWriter);
216417 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
216418 }
216419}
216420
216421/*
216422** This function does the work for an sqlite3rbu_step() call.
216423**
216424** The object-iterator (p->objiter) currently points to a valid object,
216425** and the input cursor (p->objiter.pSelect) currently points to a valid
216426** input row. Perform whatever processing is required and return.
216427**
216428** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
216429** and message is left in the RBU handle and a copy of the error code
216430** returned.
216431*/
216432static int rbuStep(sqlite3rbu *p){
216433 RbuObjIter *pIter = &p->objiter;
216434 const char *zMask = 0;
216435 int eType = rbuStepType(p, &zMask);
216436
216437 if( eType ){
216438 assert( eType==RBU_INSERT || eType==RBU_DELETE
216439 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE
216440 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
216441 );
216442 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
216443
216444 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
216445 rbuBadControlError(p);
216446 }
216447 else if( eType==RBU_REPLACE ){
216448 if( pIter->zIdx==0 ){
216449 p->nPhaseOneStep += p->objiter.nIndex;
216450 rbuStepOneOp(p, RBU_DELETE);
216451 }
216452 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
216453 }
216454 else if( eType!=RBU_UPDATE ){
216455 rbuStepOneOp(p, eType);
216456 }
216457 else{
216458 sqlite3_value *pVal;
216459 sqlite3_stmt *pUpdate = 0;
216460 assert( eType==RBU_UPDATE );
216461 p->nPhaseOneStep -= p->objiter.nIndex;
216462 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
216463 if( pUpdate ){
216464 int i;
216465 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
216466 char c = zMask[pIter->aiSrcOrder[i]];
216467 pVal = sqlite3_column_value(pIter->pSelect, i);
216468 if( pIter->abTblPk[i] || c!='.' ){
216469 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
216470 }
216471 }
216472 if( p->rc==SQLITE_OK
216473 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
216474 ){
216475 /* Bind the rbu_rowid value to column _rowid_ */
216476 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
216477 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
216478 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
216479 }
216480 if( p->rc==SQLITE_OK ){
216481 sqlite3_step(pUpdate);
216482 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
216483 }
216484 }
216485 }
216486 }
216487 return p->rc;
216488}
216489
216490/*
216491** Increment the schema cookie of the main database opened by p->dbMain.
216492**
216493** Or, if this is an RBU vacuum, set the schema cookie of the main db
216494** opened by p->dbMain to one more than the schema cookie of the main
216495** db opened by p->dbRbu.
216496*/
216497static void rbuIncrSchemaCookie(sqlite3rbu *p){
216498 if( p->rc==SQLITE_OK ){
216499 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
216500 int iCookie = 1000000;
216501 sqlite3_stmt *pStmt;
216502
216503 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
216504 "PRAGMA schema_version"
216505 );
216506 if( p->rc==SQLITE_OK ){
216507 /* Coverage: it may be that this sqlite3_step() cannot fail. There
216508 ** is already a transaction open, so the prepared statement cannot
216509 ** throw an SQLITE_SCHEMA exception. The only database page the
216510 ** statement reads is page 1, which is guaranteed to be in the cache.
216511 ** And no memory allocations are required. */
216512 if( SQLITE_ROW==sqlite3_step(pStmt) ){
216513 iCookie = sqlite3_column_int(pStmt, 0);
216514 }
216515 rbuFinalize(p, pStmt);
216516 }
216517 if( p->rc==SQLITE_OK ){
216518 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
216519 }
216520 }
216521}
216522
216523/*
216524** Update the contents of the rbu_state table within the rbu database. The
216525** value stored in the RBU_STATE_STAGE column is eStage. All other values
216526** are determined by inspecting the rbu handle passed as the first argument.
216527*/
216528static void rbuSaveState(sqlite3rbu *p, int eStage){
216529 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
216530 sqlite3_stmt *pInsert = 0;
216531 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
216532 int rc;
216533
216534 assert( p->zErrmsg==0 );
216535 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
216536 sqlite3_mprintf(
216537 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
216538 "(%d, %d), "
216539 "(%d, %Q), "
216540 "(%d, %Q), "
216541 "(%d, %d), "
216542 "(%d, %d), "
216543 "(%d, %lld), "
216544 "(%d, %lld), "
216545 "(%d, %lld), "
216546 "(%d, %lld), "
216547 "(%d, %Q) ",
216548 p->zStateDb,
216549 RBU_STATE_STAGE, eStage,
216550 RBU_STATE_TBL, p->objiter.zTbl,
216551 RBU_STATE_IDX, p->objiter.zIdx,
216552 RBU_STATE_ROW, p->nStep,
216553 RBU_STATE_PROGRESS, p->nProgress,
216554 RBU_STATE_CKPT, p->iWalCksum,
216555 RBU_STATE_COOKIE, (i64)pFd->iCookie,
216556 RBU_STATE_OALSZ, p->iOalSz,
216557 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
216558 RBU_STATE_DATATBL, p->objiter.zDataTbl
216559 )
216560 );
216561 assert( pInsert==0 || rc==SQLITE_OK );
216562
216563 if( rc==SQLITE_OK ){
216564 sqlite3_step(pInsert);
216565 rc = sqlite3_finalize(pInsert);
216566 }
216567 if( rc!=SQLITE_OK ) p->rc = rc;
216568 }
216569}
216570
216571
216572/*
216573** The second argument passed to this function is the name of a PRAGMA
216574** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
216575** This function executes the following on sqlite3rbu.dbRbu:
216576**
216577** "PRAGMA main.$zPragma"
216578**
216579** where $zPragma is the string passed as the second argument, then
216580** on sqlite3rbu.dbMain:
216581**
216582** "PRAGMA main.$zPragma = $val"
216583**
216584** where $val is the value returned by the first PRAGMA invocation.
216585**
216586** In short, it copies the value of the specified PRAGMA setting from
216587** dbRbu to dbMain.
216588*/
216589static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
216590 if( p->rc==SQLITE_OK ){
216591 sqlite3_stmt *pPragma = 0;
216592 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
216593 sqlite3_mprintf("PRAGMA main.%s", zPragma)
216594 );
216595 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
216596 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
216597 zPragma, sqlite3_column_int(pPragma, 0)
216598 );
216599 }
216600 rbuFinalize(p, pPragma);
216601 }
216602}
216603
216604/*
216605** The RBU handle passed as the only argument has just been opened and
216606** the state database is empty. If this RBU handle was opened for an
216607** RBU vacuum operation, create the schema in the target db.
216608*/
216609static void rbuCreateTargetSchema(sqlite3rbu *p){
216610 sqlite3_stmt *pSql = 0;
216611 sqlite3_stmt *pInsert = 0;
216612
216613 assert( rbuIsVacuum(p) );
216614 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
216615 if( p->rc==SQLITE_OK ){
216616 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
216617 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
216618 " AND name!='sqlite_sequence' "
216619 " ORDER BY type DESC"
216620 );
216621 }
216622
216623 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
216624 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
216625 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
216626 }
216627 rbuFinalize(p, pSql);
216628 if( p->rc!=SQLITE_OK ) return;
216629
216630 if( p->rc==SQLITE_OK ){
216631 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
216632 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
216633 );
216634 }
216635
216636 if( p->rc==SQLITE_OK ){
216637 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
216638 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
216639 );
216640 }
216641
216642 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
216643 int i;
216644 for(i=0; i<5; i++){
216645 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
216646 }
216647 sqlite3_step(pInsert);
216648 p->rc = sqlite3_reset(pInsert);
216649 }
216650 if( p->rc==SQLITE_OK ){
216651 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
216652 }
216653
216654 rbuFinalize(p, pSql);
216655 rbuFinalize(p, pInsert);
216656}
216657
216658/*
216659** Step the RBU object.
216660*/
216661SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
216662 if( p ){
216663 switch( p->eStage ){
216664 case RBU_STAGE_OAL: {
216665 RbuObjIter *pIter = &p->objiter;
216666
216667 /* If this is an RBU vacuum operation and the state table was empty
216668 ** when this handle was opened, create the target database schema. */
216669 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
216670 rbuCreateTargetSchema(p);
216671 rbuCopyPragma(p, "user_version");
216672 rbuCopyPragma(p, "application_id");
216673 }
216674
216675 while( p->rc==SQLITE_OK && pIter->zTbl ){
216676
216677 if( pIter->bCleanup ){
216678 /* Clean up the rbu_tmp_xxx table for the previous table. It
216679 ** cannot be dropped as there are currently active SQL statements.
216680 ** But the contents can be deleted. */
216681 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
216682 rbuMPrintfExec(p, p->dbRbu,
216683 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
216684 );
216685 }
216686 }else{
216687 rbuObjIterPrepareAll(p, pIter, 0);
216688
216689 /* Advance to the next row to process. */
216690 if( p->rc==SQLITE_OK ){
216691 int rc = sqlite3_step(pIter->pSelect);
216692 if( rc==SQLITE_ROW ){
216693 p->nProgress++;
216694 p->nStep++;
216695 return rbuStep(p);
216696 }
216697 p->rc = sqlite3_reset(pIter->pSelect);
216698 p->nStep = 0;
216699 }
216700 }
216701
216702 rbuObjIterNext(p, pIter);
216703 }
216704
216705 if( p->rc==SQLITE_OK ){
216706 assert( pIter->zTbl==0 );
216707 rbuSaveState(p, RBU_STAGE_MOVE);
216708 rbuIncrSchemaCookie(p);
216709 if( p->rc==SQLITE_OK ){
216710 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
216711 }
216712 if( p->rc==SQLITE_OK ){
216713 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
216714 }
216715 p->eStage = RBU_STAGE_MOVE;
216716 }
216717 break;
216718 }
216719
216720 case RBU_STAGE_MOVE: {
216721 if( p->rc==SQLITE_OK ){
216722 rbuMoveOalFile(p);
216723 p->nProgress++;
216724 }
216725 break;
216726 }
216727
216728 case RBU_STAGE_CKPT: {
216729 if( p->rc==SQLITE_OK ){
216730 if( p->nStep>=p->nFrame ){
216731 sqlite3_file *pDb = p->pTargetFd->pReal;
216732
216733 /* Sync the db file */
216734 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
216735
216736 /* Update nBackfill */
216737 if( p->rc==SQLITE_OK ){
216738 void volatile *ptr;
216739 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
216740 if( p->rc==SQLITE_OK ){
216741 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
216742 }
216743 }
216744
216745 if( p->rc==SQLITE_OK ){
216746 p->eStage = RBU_STAGE_DONE;
216747 p->rc = SQLITE_DONE;
216748 }
216749 }else{
216750 /* At one point the following block copied a single frame from the
216751 ** wal file to the database file. So that one call to sqlite3rbu_step()
216752 ** checkpointed a single frame.
216753 **
216754 ** However, if the sector-size is larger than the page-size, and the
216755 ** application calls sqlite3rbu_savestate() or close() immediately
216756 ** after this step, then rbu_step() again, then a power failure occurs,
216757 ** then the database page written here may be damaged. Work around
216758 ** this by checkpointing frames until the next page in the aFrame[]
216759 ** lies on a different disk sector to the current one. */
216760 u32 iSector;
216761 do{
216762 RbuFrame *pFrame = &p->aFrame[p->nStep];
216763 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
216764 rbuCheckpointFrame(p, pFrame);
216765 p->nStep++;
216766 }while( p->nStep<p->nFrame
216767 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
216768 && p->rc==SQLITE_OK
216769 );
216770 }
216771 p->nProgress++;
216772 }
216773 break;
216774 }
216775
216776 default:
216777 break;
216778 }
216779 return p->rc;
216780 }else{
216781 return SQLITE_NOMEM;
216782 }
216783}
216784
216785/*
216786** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
216787** otherwise. Either or both argument may be NULL. Two NULL values are
216788** considered equal, and NULL is considered distinct from all other values.
216789*/
216790static int rbuStrCompare(const char *z1, const char *z2){
216791 if( z1==0 && z2==0 ) return 0;
216792 if( z1==0 || z2==0 ) return 1;
216793 return (sqlite3_stricmp(z1, z2)!=0);
216794}
216795
216796/*
216797** This function is called as part of sqlite3rbu_open() when initializing
216798** an rbu handle in OAL stage. If the rbu update has not started (i.e.
216799** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
216800** things so that the next call to sqlite3rbu_step() continues on from
216801** where the previous rbu handle left off.
216802**
216803** If an error occurs, an error code and error message are left in the
216804** rbu handle passed as the first argument.
216805*/
216806static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
216807 assert( p->rc==SQLITE_OK );
216808 if( pState->zTbl ){
216809 RbuObjIter *pIter = &p->objiter;
216810 int rc = SQLITE_OK;
216811
216812 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
216813 || rbuStrCompare(pIter->zIdx, pState->zIdx)
216814 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
216815 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
216816 )){
216817 rc = rbuObjIterNext(p, pIter);
216818 }
216819
216820 if( rc==SQLITE_OK && !pIter->zTbl ){
216821 rc = SQLITE_ERROR;
216822 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
216823 }
216824
216825 if( rc==SQLITE_OK ){
216826 p->nStep = pState->nRow;
216827 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
216828 }
216829
216830 p->rc = rc;
216831 }
216832}
216833
216834/*
216835** If there is a "*-oal" file in the file-system corresponding to the
216836** target database in the file-system, delete it. If an error occurs,
216837** leave an error code and error message in the rbu handle.
216838*/
216839static void rbuDeleteOalFile(sqlite3rbu *p){
216840 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
216841 if( zOal ){
216842 sqlite3_vfs *pVfs = 0;
216843 sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_VFS_POINTER, &pVfs);
216844 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
216845 pVfs->xDelete(pVfs, zOal, 0);
216846 sqlite3_free(zOal);
216847 }
216848}
216849
216850/*
216851** Allocate a private rbu VFS for the rbu handle passed as the only
216852** argument. This VFS will be used unless the call to sqlite3rbu_open()
216853** specified a URI with a vfs=? option in place of a target database
216854** file name.
216855*/
216856static void rbuCreateVfs(sqlite3rbu *p){
216857 int rnd;
216858 char zRnd[64];
216859
216860 assert( p->rc==SQLITE_OK );
216861 sqlite3_randomness(sizeof(int), (void*)&rnd);
216862 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
216863 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
216864 if( p->rc==SQLITE_OK ){
216865 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
216866 assert( pVfs );
216867 p->zVfsName = pVfs->zName;
216868 ((rbu_vfs*)pVfs)->pRbu = p;
216869 }
216870}
216871
216872/*
216873** Destroy the private VFS created for the rbu handle passed as the only
216874** argument by an earlier call to rbuCreateVfs().
216875*/
216876static void rbuDeleteVfs(sqlite3rbu *p){
216877 if( p->zVfsName ){
216878 sqlite3rbu_destroy_vfs(p->zVfsName);
216879 p->zVfsName = 0;
216880 }
216881}
216882
216883/*
216884** This user-defined SQL function is invoked with a single argument - the
216885** name of a table expected to appear in the target database. It returns
216886** the number of auxilliary indexes on the table.
216887*/
216888static void rbuIndexCntFunc(
216889 sqlite3_context *pCtx,
216890 int nVal,
216891 sqlite3_value **apVal
216892){
216893 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
216894 sqlite3_stmt *pStmt = 0;
216895 char *zErrmsg = 0;
216896 int rc;
216897 sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
216898
216899 assert( nVal==1 );
216900
216901 rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
216902 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
216903 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
216904 );
216905 if( rc!=SQLITE_OK ){
216906 sqlite3_result_error(pCtx, zErrmsg, -1);
216907 }else{
216908 int nIndex = 0;
216909 if( SQLITE_ROW==sqlite3_step(pStmt) ){
216910 nIndex = sqlite3_column_int(pStmt, 0);
216911 }
216912 rc = sqlite3_finalize(pStmt);
216913 if( rc==SQLITE_OK ){
216914 sqlite3_result_int(pCtx, nIndex);
216915 }else{
216916 sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
216917 }
216918 }
216919
216920 sqlite3_free(zErrmsg);
216921}
216922
216923/*
216924** If the RBU database contains the rbu_count table, use it to initialize
216925** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
216926** is assumed to contain the same columns as:
216927**
216928** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
216929**
216930** There should be one row in the table for each data_xxx table in the
216931** database. The 'tbl' column should contain the name of a data_xxx table,
216932** and the cnt column the number of rows it contains.
216933**
216934** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
216935** for all rows in the rbu_count table, where nIndex is the number of
216936** indexes on the corresponding target database table.
216937*/
216938static void rbuInitPhaseOneSteps(sqlite3rbu *p){
216939 if( p->rc==SQLITE_OK ){
216940 sqlite3_stmt *pStmt = 0;
216941 int bExists = 0; /* True if rbu_count exists */
216942
216943 p->nPhaseOneStep = -1;
216944
216945 p->rc = sqlite3_create_function(p->dbRbu,
216946 "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
216947 );
216948
216949 /* Check for the rbu_count table. If it does not exist, or if an error
216950 ** occurs, nPhaseOneStep will be left set to -1. */
216951 if( p->rc==SQLITE_OK ){
216952 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
216953 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
216954 );
216955 }
216956 if( p->rc==SQLITE_OK ){
216957 if( SQLITE_ROW==sqlite3_step(pStmt) ){
216958 bExists = 1;
216959 }
216960 p->rc = sqlite3_finalize(pStmt);
216961 }
216962
216963 if( p->rc==SQLITE_OK && bExists ){
216964 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
216965 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
216966 "FROM rbu_count"
216967 );
216968 if( p->rc==SQLITE_OK ){
216969 if( SQLITE_ROW==sqlite3_step(pStmt) ){
216970 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
216971 }
216972 p->rc = sqlite3_finalize(pStmt);
216973 }
216974 }
216975 }
216976}
216977
216978
216979static sqlite3rbu *openRbuHandle(
216980 const char *zTarget,
216981 const char *zRbu,
216982 const char *zState
216983){
216984 sqlite3rbu *p;
216985 size_t nTarget = zTarget ? strlen(zTarget) : 0;
216986 size_t nRbu = strlen(zRbu);
216987 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
216988
216989 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
216990 if( p ){
216991 RbuState *pState = 0;
216992
216993 /* Create the custom VFS. */
216994 memset(p, 0, sizeof(sqlite3rbu));
216995 sqlite3rbu_rename_handler(p, 0, 0);
216996 rbuCreateVfs(p);
216997
216998 /* Open the target, RBU and state databases */
216999 if( p->rc==SQLITE_OK ){
217000 char *pCsr = (char*)&p[1];
217001 int bRetry = 0;
217002 if( zTarget ){
217003 p->zTarget = pCsr;
217004 memcpy(p->zTarget, zTarget, nTarget+1);
217005 pCsr += nTarget+1;
217006 }
217007 p->zRbu = pCsr;
217008 memcpy(p->zRbu, zRbu, nRbu+1);
217009 pCsr += nRbu+1;
217010 if( zState ){
217011 p->zState = rbuMPrintf(p, "%s", zState);
217012 }
217013
217014 /* If the first attempt to open the database file fails and the bRetry
217015 ** flag it set, this means that the db was not opened because it seemed
217016 ** to be a wal-mode db. But, this may have happened due to an earlier
217017 ** RBU vacuum operation leaving an old wal file in the directory.
217018 ** If this is the case, it will have been checkpointed and deleted
217019 ** when the handle was closed and a second attempt to open the
217020 ** database may succeed. */
217021 rbuOpenDatabase(p, 0, &bRetry);
217022 if( bRetry ){
217023 rbuOpenDatabase(p, 0, 0);
217024 }
217025 }
217026
217027 if( p->rc==SQLITE_OK ){
217028 pState = rbuLoadState(p);
217029 assert( pState || p->rc!=SQLITE_OK );
217030 if( p->rc==SQLITE_OK ){
217031
217032 if( pState->eStage==0 ){
217033 rbuDeleteOalFile(p);
217034 rbuInitPhaseOneSteps(p);
217035 p->eStage = RBU_STAGE_OAL;
217036 }else{
217037 p->eStage = pState->eStage;
217038 p->nPhaseOneStep = pState->nPhaseOneStep;
217039 }
217040 p->nProgress = pState->nProgress;
217041 p->iOalSz = pState->iOalSz;
217042 }
217043 }
217044 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
217045
217046 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
217047 if( p->eStage==RBU_STAGE_OAL ){
217048 p->rc = SQLITE_ERROR;
217049 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
217050 }else if( p->eStage==RBU_STAGE_MOVE ){
217051 p->eStage = RBU_STAGE_CKPT;
217052 p->nStep = 0;
217053 }
217054 }
217055
217056 if( p->rc==SQLITE_OK
217057 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
217058 && pState->eStage!=0
217059 ){
217060 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
217061 if( pFd->iCookie!=pState->iCookie ){
217062 /* At this point (pTargetFd->iCookie) contains the value of the
217063 ** change-counter cookie (the thing that gets incremented when a
217064 ** transaction is committed in rollback mode) currently stored on
217065 ** page 1 of the database file. */
217066 p->rc = SQLITE_BUSY;
217067 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
217068 (rbuIsVacuum(p) ? "vacuum" : "update")
217069 );
217070 }
217071 }
217072
217073 if( p->rc==SQLITE_OK ){
217074 if( p->eStage==RBU_STAGE_OAL ){
217075 sqlite3 *db = p->dbMain;
217076 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
217077
217078 /* Point the object iterator at the first object */
217079 if( p->rc==SQLITE_OK ){
217080 p->rc = rbuObjIterFirst(p, &p->objiter);
217081 }
217082
217083 /* If the RBU database contains no data_xxx tables, declare the RBU
217084 ** update finished. */
217085 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
217086 p->rc = SQLITE_DONE;
217087 p->eStage = RBU_STAGE_DONE;
217088 }else{
217089 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
217090 rbuCopyPragma(p, "page_size");
217091 rbuCopyPragma(p, "auto_vacuum");
217092 }
217093
217094 /* Open transactions both databases. The *-oal file is opened or
217095 ** created at this point. */
217096 if( p->rc==SQLITE_OK ){
217097 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
217098 }
217099
217100 /* Check if the main database is a zipvfs db. If it is, set the upper
217101 ** level pager to use "journal_mode=off". This prevents it from
217102 ** generating a large journal using a temp file. */
217103 if( p->rc==SQLITE_OK ){
217104 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
217105 if( frc==SQLITE_OK ){
217106 p->rc = sqlite3_exec(
217107 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
217108 }
217109 }
217110
217111 if( p->rc==SQLITE_OK ){
217112 rbuSetupOal(p, pState);
217113 }
217114 }
217115 }else if( p->eStage==RBU_STAGE_MOVE ){
217116 /* no-op */
217117 }else if( p->eStage==RBU_STAGE_CKPT ){
217118 if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){
217119 /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
217120 ** and an incremental checkpoint is being resumed, attempt an
217121 ** exclusive lock on the db file. If this fails, so be it. */
217122 p->eStage = RBU_STAGE_DONE;
217123 rbuLockDatabase(p->dbMain);
217124 p->eStage = RBU_STAGE_CKPT;
217125 }
217126 rbuSetupCheckpoint(p, pState);
217127 }else if( p->eStage==RBU_STAGE_DONE ){
217128 p->rc = SQLITE_DONE;
217129 }else{
217130 p->rc = SQLITE_CORRUPT;
217131 }
217132 }
217133
217134 rbuFreeState(pState);
217135 }
217136
217137 return p;
217138}
217139
217140/*
217141** Allocate and return an RBU handle with all fields zeroed except for the
217142** error code, which is set to SQLITE_MISUSE.
217143*/
217144static sqlite3rbu *rbuMisuseError(void){
217145 sqlite3rbu *pRet;
217146 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
217147 if( pRet ){
217148 memset(pRet, 0, sizeof(sqlite3rbu));
217149 pRet->rc = SQLITE_MISUSE;
217150 }
217151 return pRet;
217152}
217153
217154/*
217155** Open and return a new RBU handle.
217156*/
217157SQLITE_API sqlite3rbu *sqlite3rbu_open(
217158 const char *zTarget,
217159 const char *zRbu,
217160 const char *zState
217161){
217162 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
217163 return openRbuHandle(zTarget, zRbu, zState);
217164}
217165
217166/*
217167** Open a handle to begin or resume an RBU VACUUM operation.
217168*/
217169SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
217170 const char *zTarget,
217171 const char *zState
217172){
217173 if( zTarget==0 ){ return rbuMisuseError(); }
217174 if( zState ){
217175 int n = strlen(zState);
217176 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
217177 return rbuMisuseError();
217178 }
217179 }
217180 /* TODO: Check that both arguments are non-NULL */
217181 return openRbuHandle(0, zTarget, zState);
217182}
217183
217184/*
217185** Return the database handle used by pRbu.
217186*/
217187SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
217188 sqlite3 *db = 0;
217189 if( pRbu ){
217190 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
217191 }
217192 return db;
217193}
217194
217195
217196/*
217197** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
217198** then edit any error message string so as to remove all occurrences of
217199** the pattern "rbu_imp_[0-9]*".
217200*/
217201static void rbuEditErrmsg(sqlite3rbu *p){
217202 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
217203 unsigned int i;
217204 size_t nErrmsg = strlen(p->zErrmsg);
217205 for(i=0; i<(nErrmsg-8); i++){
217206 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
217207 int nDel = 8;
217208 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
217209 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
217210 nErrmsg -= nDel;
217211 }
217212 }
217213 }
217214}
217215
217216/*
217217** Close the RBU handle.
217218*/
217219SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
217220 int rc;
217221 if( p ){
217222
217223 /* Commit the transaction to the *-oal file. */
217224 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
217225 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
217226 }
217227
217228 /* Sync the db file if currently doing an incremental checkpoint */
217229 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
217230 sqlite3_file *pDb = p->pTargetFd->pReal;
217231 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
217232 }
217233
217234 rbuSaveState(p, p->eStage);
217235
217236 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
217237 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
217238 }
217239
217240 /* Close any open statement handles. */
217241 rbuObjIterFinalize(&p->objiter);
217242
217243 /* If this is an RBU vacuum handle and the vacuum has either finished
217244 ** successfully or encountered an error, delete the contents of the
217245 ** state table. This causes the next call to sqlite3rbu_vacuum()
217246 ** specifying the current target and state databases to start a new
217247 ** vacuum from scratch. */
217248 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
217249 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
217250 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
217251 }
217252
217253 /* Close the open database handle and VFS object. */
217254 sqlite3_close(p->dbRbu);
217255 sqlite3_close(p->dbMain);
217256 assert( p->szTemp==0 );
217257 rbuDeleteVfs(p);
217258 sqlite3_free(p->aBuf);
217259 sqlite3_free(p->aFrame);
217260
217261 rbuEditErrmsg(p);
217262 rc = p->rc;
217263 if( pzErrmsg ){
217264 *pzErrmsg = p->zErrmsg;
217265 }else{
217266 sqlite3_free(p->zErrmsg);
217267 }
217268 sqlite3_free(p->zState);
217269 sqlite3_free(p);
217270 }else{
217271 rc = SQLITE_NOMEM;
217272 *pzErrmsg = 0;
217273 }
217274 return rc;
217275}
217276
217277/*
217278** Return the total number of key-value operations (inserts, deletes or
217279** updates) that have been performed on the target database since the
217280** current RBU update was started.
217281*/
217282SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
217283 return pRbu->nProgress;
217284}
217285
217286/*
217287** Return permyriadage progress indications for the two main stages of
217288** an RBU update.
217289*/
217290SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
217291 const int MAX_PROGRESS = 10000;
217292 switch( p->eStage ){
217293 case RBU_STAGE_OAL:
217294 if( p->nPhaseOneStep>0 ){
217295 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
217296 }else{
217297 *pnOne = -1;
217298 }
217299 *pnTwo = 0;
217300 break;
217301
217302 case RBU_STAGE_MOVE:
217303 *pnOne = MAX_PROGRESS;
217304 *pnTwo = 0;
217305 break;
217306
217307 case RBU_STAGE_CKPT:
217308 *pnOne = MAX_PROGRESS;
217309 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
217310 break;
217311
217312 case RBU_STAGE_DONE:
217313 *pnOne = MAX_PROGRESS;
217314 *pnTwo = MAX_PROGRESS;
217315 break;
217316
217317 default:
217318 assert( 0 );
217319 }
217320}
217321
217322/*
217323** Return the current state of the RBU vacuum or update operation.
217324*/
217325SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
217326 int aRes[] = {
217327 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
217328 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
217329 };
217330
217331 assert( RBU_STAGE_OAL==1 );
217332 assert( RBU_STAGE_MOVE==2 );
217333 assert( RBU_STAGE_CKPT==4 );
217334 assert( RBU_STAGE_DONE==5 );
217335 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
217336 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
217337 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
217338 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
217339
217340 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
217341 return SQLITE_RBU_STATE_ERROR;
217342 }else{
217343 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
217344 assert( p->eStage==RBU_STAGE_OAL
217345 || p->eStage==RBU_STAGE_MOVE
217346 || p->eStage==RBU_STAGE_CKPT
217347 || p->eStage==RBU_STAGE_DONE
217348 );
217349 return aRes[p->eStage];
217350 }
217351}
217352
217353SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
217354 int rc = p->rc;
217355 if( rc==SQLITE_DONE ) return SQLITE_OK;
217356
217357 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
217358 if( p->eStage==RBU_STAGE_OAL ){
217359 assert( rc!=SQLITE_DONE );
217360 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
217361 }
217362
217363 /* Sync the db file */
217364 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
217365 sqlite3_file *pDb = p->pTargetFd->pReal;
217366 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
217367 }
217368
217369 p->rc = rc;
217370 rbuSaveState(p, p->eStage);
217371 rc = p->rc;
217372
217373 if( p->eStage==RBU_STAGE_OAL ){
217374 assert( rc!=SQLITE_DONE );
217375 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
217376 if( rc==SQLITE_OK ){
217377 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
217378 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
217379 }
217380 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
217381 }
217382
217383 p->rc = rc;
217384 return rc;
217385}
217386
217387/*
217388** Default xRename callback for RBU.
217389*/
217390static int xDefaultRename(void *pArg, const char *zOld, const char *zNew){
217391 int rc = SQLITE_OK;
217392#if defined(_WIN32_WCE)
217393 {
217394 LPWSTR zWideOld;
217395 LPWSTR zWideNew;
217396
217397 zWideOld = rbuWinUtf8ToUnicode(zOld);
217398 if( zWideOld ){
217399 zWideNew = rbuWinUtf8ToUnicode(zNew);
217400 if( zWideNew ){
217401 if( MoveFileW(zWideOld, zWideNew) ){
217402 rc = SQLITE_OK;
217403 }else{
217404 rc = SQLITE_IOERR;
217405 }
217406 sqlite3_free(zWideNew);
217407 }else{
217408 rc = SQLITE_IOERR_NOMEM;
217409 }
217410 sqlite3_free(zWideOld);
217411 }else{
217412 rc = SQLITE_IOERR_NOMEM;
217413 }
217414 }
217415#else
217416 rc = rename(zOld, zNew) ? SQLITE_IOERR : SQLITE_OK;
217417#endif
217418 return rc;
217419}
217420
217421SQLITE_API void sqlite3rbu_rename_handler(
217422 sqlite3rbu *pRbu,
217423 void *pArg,
217424 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
217425){
217426 if( xRename ){
217427 pRbu->xRename = xRename;
217428 pRbu->pRenameArg = pArg;
217429 }else{
217430 pRbu->xRename = xDefaultRename;
217431 pRbu->pRenameArg = 0;
217432 }
217433}
217434
217435/**************************************************************************
217436** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
217437** of a standard VFS in the following ways:
217438**
217439** 1. Whenever the first page of a main database file is read or
217440** written, the value of the change-counter cookie is stored in
217441** rbu_file.iCookie. Similarly, the value of the "write-version"
217442** database header field is stored in rbu_file.iWriteVer. This ensures
217443** that the values are always trustworthy within an open transaction.
217444**
217445** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
217446** member variable of the associated database file descriptor is set
217447** to point to the new file. A mutex protected linked list of all main
217448** db fds opened using a particular RBU VFS is maintained at
217449** rbu_vfs.pMain to facilitate this.
217450**
217451** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
217452** object can be marked as the target database of an RBU update. This
217453** turns on the following extra special behaviour:
217454**
217455** 3a. If xAccess() is called to check if there exists a *-wal file
217456** associated with an RBU target database currently in RBU_STAGE_OAL
217457** stage (preparing the *-oal file), the following special handling
217458** applies:
217459**
217460** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
217461** target database may not be in wal mode already.
217462**
217463** * if the *-wal file does not exist, set the output parameter to
217464** non-zero (to tell SQLite that it does exist) anyway.
217465**
217466** Then, when xOpen() is called to open the *-wal file associated with
217467** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
217468** file, the rbu vfs opens the corresponding *-oal file instead.
217469**
217470** 3b. The *-shm pages returned by xShmMap() for a target db file in
217471** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
217472** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
217473** are no-ops on target database files in RBU_STAGE_OAL mode. This is
217474** because assert() statements in some VFS implementations fail if
217475** xShmLock() is called before xShmMap().
217476**
217477** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
217478** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
217479** fails with an SQLITE_BUSY error. This is to stop RBU connections
217480** from automatically checkpointing a *-wal (or *-oal) file from within
217481** sqlite3_close().
217482**
217483** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
217484** all xWrite() calls on the target database file perform no IO.
217485** Instead the frame and page numbers that would be read and written
217486** are recorded. Additionally, successful attempts to obtain exclusive
217487** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
217488** database file are recorded. xShmLock() calls to unlock the same
217489** locks are no-ops (so that once obtained, these locks are never
217490** relinquished). Finally, calls to xSync() on the target database
217491** file fail with SQLITE_NOTICE errors.
217492*/
217493
217494static void rbuUnlockShm(rbu_file *p){
217495 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
217496 if( p->pRbu ){
217497 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
217498 int i;
217499 for(i=0; i<SQLITE_SHM_NLOCK;i++){
217500 if( (1<<i) & p->pRbu->mLock ){
217501 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
217502 }
217503 }
217504 p->pRbu->mLock = 0;
217505 }
217506}
217507
217508/*
217509*/
217510static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
217511 sqlite3rbu *pRbu = pFd->pRbu;
217512 i64 nDiff = nNew - pFd->sz;
217513 pRbu->szTemp += nDiff;
217514 pFd->sz = nNew;
217515 assert( pRbu->szTemp>=0 );
217516 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
217517 return SQLITE_OK;
217518}
217519
217520/*
217521** Add an item to the main-db lists, if it is not already present.
217522**
217523** There are two main-db lists. One for all file descriptors, and one
217524** for all file descriptors with rbu_file.pDb!=0. If the argument has
217525** rbu_file.pDb!=0, then it is assumed to already be present on the
217526** main list and is only added to the pDb!=0 list.
217527*/
217528static void rbuMainlistAdd(rbu_file *p){
217529 rbu_vfs *pRbuVfs = p->pRbuVfs;
217530 rbu_file *pIter;
217531 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
217532 sqlite3_mutex_enter(pRbuVfs->mutex);
217533 if( p->pRbu==0 ){
217534 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
217535 p->pMainNext = pRbuVfs->pMain;
217536 pRbuVfs->pMain = p;
217537 }else{
217538 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
217539 if( pIter==0 ){
217540 p->pMainRbuNext = pRbuVfs->pMainRbu;
217541 pRbuVfs->pMainRbu = p;
217542 }
217543 }
217544 sqlite3_mutex_leave(pRbuVfs->mutex);
217545}
217546
217547/*
217548** Remove an item from the main-db lists.
217549*/
217550static void rbuMainlistRemove(rbu_file *p){
217551 rbu_file **pp;
217552 sqlite3_mutex_enter(p->pRbuVfs->mutex);
217553 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
217554 if( *pp ) *pp = p->pMainNext;
217555 p->pMainNext = 0;
217556 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
217557 if( *pp ) *pp = p->pMainRbuNext;
217558 p->pMainRbuNext = 0;
217559 sqlite3_mutex_leave(p->pRbuVfs->mutex);
217560}
217561
217562/*
217563** Given that zWal points to a buffer containing a wal file name passed to
217564** either the xOpen() or xAccess() VFS method, search the main-db list for
217565** a file-handle opened by the same database connection on the corresponding
217566** database file.
217567**
217568** If parameter bRbu is true, only search for file-descriptors with
217569** rbu_file.pDb!=0.
217570*/
217571static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
217572 rbu_file *pDb;
217573 sqlite3_mutex_enter(pRbuVfs->mutex);
217574 if( bRbu ){
217575 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
217576 }else{
217577 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
217578 }
217579 sqlite3_mutex_leave(pRbuVfs->mutex);
217580 return pDb;
217581}
217582
217583/*
217584** Close an rbu file.
217585*/
217586static int rbuVfsClose(sqlite3_file *pFile){
217587 rbu_file *p = (rbu_file*)pFile;
217588 int rc;
217589 int i;
217590
217591 /* Free the contents of the apShm[] array. And the array itself. */
217592 for(i=0; i<p->nShm; i++){
217593 sqlite3_free(p->apShm[i]);
217594 }
217595 sqlite3_free(p->apShm);
217596 p->apShm = 0;
217597 sqlite3_free(p->zDel);
217598
217599 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
217600 const sqlite3_io_methods *pMeth = p->pReal->pMethods;
217601 rbuMainlistRemove(p);
217602 rbuUnlockShm(p);
217603 if( pMeth->iVersion>1 && pMeth->xShmUnmap ){
217604 pMeth->xShmUnmap(p->pReal, 0);
217605 }
217606 }
217607 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
217608 rbuUpdateTempSize(p, 0);
217609 }
217610 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
217611
217612 /* Close the underlying file handle */
217613 rc = p->pReal->pMethods->xClose(p->pReal);
217614 return rc;
217615}
217616
217617
217618/*
217619** Read and return an unsigned 32-bit big-endian integer from the buffer
217620** passed as the only argument.
217621*/
217622static u32 rbuGetU32(u8 *aBuf){
217623 return ((u32)aBuf[0] << 24)
217624 + ((u32)aBuf[1] << 16)
217625 + ((u32)aBuf[2] << 8)
217626 + ((u32)aBuf[3]);
217627}
217628
217629/*
217630** Write an unsigned 32-bit value in big-endian format to the supplied
217631** buffer.
217632*/
217633static void rbuPutU32(u8 *aBuf, u32 iVal){
217634 aBuf[0] = (iVal >> 24) & 0xFF;
217635 aBuf[1] = (iVal >> 16) & 0xFF;
217636 aBuf[2] = (iVal >> 8) & 0xFF;
217637 aBuf[3] = (iVal >> 0) & 0xFF;
217638}
217639
217640static void rbuPutU16(u8 *aBuf, u16 iVal){
217641 aBuf[0] = (iVal >> 8) & 0xFF;
217642 aBuf[1] = (iVal >> 0) & 0xFF;
217643}
217644
217645/*
217646** Read data from an rbuVfs-file.
217647*/
217648static int rbuVfsRead(
217649 sqlite3_file *pFile,
217650 void *zBuf,
217651 int iAmt,
217652 sqlite_int64 iOfst
217653){
217654 rbu_file *p = (rbu_file*)pFile;
217655 sqlite3rbu *pRbu = p->pRbu;
217656 int rc;
217657
217658 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
217659 assert( p->openFlags & SQLITE_OPEN_WAL );
217660 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
217661 }else{
217662 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
217663 && (p->openFlags & SQLITE_OPEN_WAL)
217664 && iOfst>=pRbu->iOalSz
217665 ){
217666 rc = SQLITE_OK;
217667 memset(zBuf, 0, iAmt);
217668 }else{
217669 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
217670#if 1
217671 /* If this is being called to read the first page of the target
217672 ** database as part of an rbu vacuum operation, synthesize the
217673 ** contents of the first page if it does not yet exist. Otherwise,
217674 ** SQLite will not check for a *-wal file. */
217675 if( pRbu && rbuIsVacuum(pRbu)
217676 && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
217677 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
217678 && pRbu->rc==SQLITE_OK
217679 ){
217680 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
217681 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
217682 if( rc==SQLITE_OK ){
217683 u8 *aBuf = (u8*)zBuf;
217684 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
217685 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
217686 rbuPutU32(&aBuf[36], 0); /* number of free pages */
217687 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
217688 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
217689 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
217690
217691 if( iAmt>100 ){
217692 memset(&aBuf[100], 0, iAmt-100);
217693 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
217694 aBuf[100] = 0x0D;
217695 }
217696 }
217697 }
217698#endif
217699 }
217700 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
217701 /* These look like magic numbers. But they are stable, as they are part
217702 ** of the definition of the SQLite file format, which may not change. */
217703 u8 *pBuf = (u8*)zBuf;
217704 p->iCookie = rbuGetU32(&pBuf[24]);
217705 p->iWriteVer = pBuf[19];
217706 }
217707 }
217708 return rc;
217709}
217710
217711/*
217712** Write data to an rbuVfs-file.
217713*/
217714static int rbuVfsWrite(
217715 sqlite3_file *pFile,
217716 const void *zBuf,
217717 int iAmt,
217718 sqlite_int64 iOfst
217719){
217720 rbu_file *p = (rbu_file*)pFile;
217721 sqlite3rbu *pRbu = p->pRbu;
217722 int rc;
217723
217724 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
217725 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
217726 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
217727 }else{
217728 if( pRbu ){
217729 if( pRbu->eStage==RBU_STAGE_OAL
217730 && (p->openFlags & SQLITE_OPEN_WAL)
217731 && iOfst>=pRbu->iOalSz
217732 ){
217733 pRbu->iOalSz = iAmt + iOfst;
217734 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
217735 i64 szNew = iAmt+iOfst;
217736 if( szNew>p->sz ){
217737 rc = rbuUpdateTempSize(p, szNew);
217738 if( rc!=SQLITE_OK ) return rc;
217739 }
217740 }
217741 }
217742 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
217743 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
217744 /* These look like magic numbers. But they are stable, as they are part
217745 ** of the definition of the SQLite file format, which may not change. */
217746 u8 *pBuf = (u8*)zBuf;
217747 p->iCookie = rbuGetU32(&pBuf[24]);
217748 p->iWriteVer = pBuf[19];
217749 }
217750 }
217751 return rc;
217752}
217753
217754/*
217755** Truncate an rbuVfs-file.
217756*/
217757static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
217758 rbu_file *p = (rbu_file*)pFile;
217759 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
217760 int rc = rbuUpdateTempSize(p, size);
217761 if( rc!=SQLITE_OK ) return rc;
217762 }
217763 return p->pReal->pMethods->xTruncate(p->pReal, size);
217764}
217765
217766/*
217767** Sync an rbuVfs-file.
217768*/
217769static int rbuVfsSync(sqlite3_file *pFile, int flags){
217770 rbu_file *p = (rbu_file *)pFile;
217771 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
217772 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
217773 return SQLITE_NOTICE_RBU;
217774 }
217775 return SQLITE_OK;
217776 }
217777 return p->pReal->pMethods->xSync(p->pReal, flags);
217778}
217779
217780/*
217781** Return the current file-size of an rbuVfs-file.
217782*/
217783static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
217784 rbu_file *p = (rbu_file *)pFile;
217785 int rc;
217786 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
217787
217788 /* If this is an RBU vacuum operation and this is the target database,
217789 ** pretend that it has at least one page. Otherwise, SQLite will not
217790 ** check for the existance of a *-wal file. rbuVfsRead() contains
217791 ** similar logic. */
217792 if( rc==SQLITE_OK && *pSize==0
217793 && p->pRbu && rbuIsVacuum(p->pRbu)
217794 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
217795 ){
217796 *pSize = 1024;
217797 }
217798 return rc;
217799}
217800
217801/*
217802** Lock an rbuVfs-file.
217803*/
217804static int rbuVfsLock(sqlite3_file *pFile, int eLock){
217805 rbu_file *p = (rbu_file*)pFile;
217806 sqlite3rbu *pRbu = p->pRbu;
217807 int rc = SQLITE_OK;
217808
217809 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
217810 if( eLock==SQLITE_LOCK_EXCLUSIVE
217811 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
217812 ){
217813 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
217814 ** prevents it from checkpointing the database from sqlite3_close(). */
217815 rc = SQLITE_BUSY;
217816 }else{
217817 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
217818 }
217819
217820 return rc;
217821}
217822
217823/*
217824** Unlock an rbuVfs-file.
217825*/
217826static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
217827 rbu_file *p = (rbu_file *)pFile;
217828 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
217829}
217830
217831/*
217832** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
217833*/
217834static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
217835 rbu_file *p = (rbu_file *)pFile;
217836 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
217837}
217838
217839/*
217840** File control method. For custom operations on an rbuVfs-file.
217841*/
217842static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
217843 rbu_file *p = (rbu_file *)pFile;
217844 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
217845 int rc;
217846
217847 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
217848 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
217849 );
217850 if( op==SQLITE_FCNTL_RBU ){
217851 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
217852
217853 /* First try to find another RBU vfs lower down in the vfs stack. If
217854 ** one is found, this vfs will operate in pass-through mode. The lower
217855 ** level vfs will do the special RBU handling. */
217856 rc = xControl(p->pReal, op, pArg);
217857
217858 if( rc==SQLITE_NOTFOUND ){
217859 /* Now search for a zipvfs instance lower down in the VFS stack. If
217860 ** one is found, this is an error. */
217861 void *dummy = 0;
217862 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
217863 if( rc==SQLITE_OK ){
217864 rc = SQLITE_ERROR;
217865 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
217866 }else if( rc==SQLITE_NOTFOUND ){
217867 pRbu->pTargetFd = p;
217868 p->pRbu = pRbu;
217869 rbuMainlistAdd(p);
217870 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
217871 rc = SQLITE_OK;
217872 }
217873 }
217874 return rc;
217875 }
217876 else if( op==SQLITE_FCNTL_RBUCNT ){
217877 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
217878 pRbu->nRbu++;
217879 pRbu->pRbuFd = p;
217880 p->bNolock = 1;
217881 }
217882
217883 rc = xControl(p->pReal, op, pArg);
217884 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
217885 rbu_vfs *pRbuVfs = p->pRbuVfs;
217886 char *zIn = *(char**)pArg;
217887 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
217888 *(char**)pArg = zOut;
217889 if( zOut==0 ) rc = SQLITE_NOMEM;
217890 }
217891
217892 return rc;
217893}
217894
217895/*
217896** Return the sector-size in bytes for an rbuVfs-file.
217897*/
217898static int rbuVfsSectorSize(sqlite3_file *pFile){
217899 rbu_file *p = (rbu_file *)pFile;
217900 return p->pReal->pMethods->xSectorSize(p->pReal);
217901}
217902
217903/*
217904** Return the device characteristic flags supported by an rbuVfs-file.
217905*/
217906static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
217907 rbu_file *p = (rbu_file *)pFile;
217908 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
217909}
217910
217911/*
217912** Take or release a shared-memory lock.
217913*/
217914static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
217915 rbu_file *p = (rbu_file*)pFile;
217916 sqlite3rbu *pRbu = p->pRbu;
217917 int rc = SQLITE_OK;
217918
217919#ifdef SQLITE_AMALGAMATION
217920 assert( WAL_CKPT_LOCK==1 );
217921#endif
217922
217923 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
217924 if( pRbu && (
217925 pRbu->eStage==RBU_STAGE_OAL
217926 || pRbu->eStage==RBU_STAGE_MOVE
217927 || pRbu->eStage==RBU_STAGE_DONE
217928 )){
217929 /* Prevent SQLite from taking a shm-lock on the target file when it
217930 ** is supplying heap memory to the upper layer in place of *-shm
217931 ** segments. */
217932 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
217933 }else{
217934 int bCapture = 0;
217935 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
217936 bCapture = 1;
217937 }
217938 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
217939 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
217940 if( bCapture && rc==SQLITE_OK ){
217941 pRbu->mLock |= ((1<<n) - 1) << ofst;
217942 }
217943 }
217944 }
217945
217946 return rc;
217947}
217948
217949/*
217950** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
217951*/
217952static int rbuVfsShmMap(
217953 sqlite3_file *pFile,
217954 int iRegion,
217955 int szRegion,
217956 int isWrite,
217957 void volatile **pp
217958){
217959 rbu_file *p = (rbu_file*)pFile;
217960 int rc = SQLITE_OK;
217961 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
217962
217963 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
217964 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
217965 ** instead of a file on disk. */
217966 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
217967 if( eStage==RBU_STAGE_OAL ){
217968 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
217969 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
217970
217971 /* This is an RBU connection that uses its own heap memory for the
217972 ** pages of the *-shm file. Since no other process can have run
217973 ** recovery, the connection must request *-shm pages in order
217974 ** from start to finish. */
217975 assert( iRegion==p->nShm );
217976 if( apNew==0 ){
217977 rc = SQLITE_NOMEM;
217978 }else{
217979 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
217980 p->apShm = apNew;
217981 p->nShm = iRegion+1;
217982 }
217983
217984 if( rc==SQLITE_OK ){
217985 char *pNew = (char*)sqlite3_malloc64(szRegion);
217986 if( pNew==0 ){
217987 rc = SQLITE_NOMEM;
217988 }else{
217989 memset(pNew, 0, szRegion);
217990 p->apShm[iRegion] = pNew;
217991 }
217992 }
217993
217994 if( rc==SQLITE_OK ){
217995 *pp = p->apShm[iRegion];
217996 }else{
217997 *pp = 0;
217998 }
217999 }else{
218000 assert( p->apShm==0 );
218001 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
218002 }
218003
218004 return rc;
218005}
218006
218007/*
218008** Memory barrier.
218009*/
218010static void rbuVfsShmBarrier(sqlite3_file *pFile){
218011 rbu_file *p = (rbu_file *)pFile;
218012 p->pReal->pMethods->xShmBarrier(p->pReal);
218013}
218014
218015/*
218016** The xShmUnmap method.
218017*/
218018static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
218019 rbu_file *p = (rbu_file*)pFile;
218020 int rc = SQLITE_OK;
218021 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
218022
218023 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
218024 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
218025 /* no-op */
218026 }else{
218027 /* Release the checkpointer and writer locks */
218028 rbuUnlockShm(p);
218029 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
218030 }
218031 return rc;
218032}
218033
218034/*
218035** Open an rbu file handle.
218036*/
218037static int rbuVfsOpen(
218038 sqlite3_vfs *pVfs,
218039 const char *zName,
218040 sqlite3_file *pFile,
218041 int flags,
218042 int *pOutFlags
218043){
218044 static sqlite3_io_methods rbuvfs_io_methods = {
218045 2, /* iVersion */
218046 rbuVfsClose, /* xClose */
218047 rbuVfsRead, /* xRead */
218048 rbuVfsWrite, /* xWrite */
218049 rbuVfsTruncate, /* xTruncate */
218050 rbuVfsSync, /* xSync */
218051 rbuVfsFileSize, /* xFileSize */
218052 rbuVfsLock, /* xLock */
218053 rbuVfsUnlock, /* xUnlock */
218054 rbuVfsCheckReservedLock, /* xCheckReservedLock */
218055 rbuVfsFileControl, /* xFileControl */
218056 rbuVfsSectorSize, /* xSectorSize */
218057 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
218058 rbuVfsShmMap, /* xShmMap */
218059 rbuVfsShmLock, /* xShmLock */
218060 rbuVfsShmBarrier, /* xShmBarrier */
218061 rbuVfsShmUnmap, /* xShmUnmap */
218062 0, 0 /* xFetch, xUnfetch */
218063 };
218064 static sqlite3_io_methods rbuvfs_io_methods1 = {
218065 1, /* iVersion */
218066 rbuVfsClose, /* xClose */
218067 rbuVfsRead, /* xRead */
218068 rbuVfsWrite, /* xWrite */
218069 rbuVfsTruncate, /* xTruncate */
218070 rbuVfsSync, /* xSync */
218071 rbuVfsFileSize, /* xFileSize */
218072 rbuVfsLock, /* xLock */
218073 rbuVfsUnlock, /* xUnlock */
218074 rbuVfsCheckReservedLock, /* xCheckReservedLock */
218075 rbuVfsFileControl, /* xFileControl */
218076 rbuVfsSectorSize, /* xSectorSize */
218077 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
218078 0, 0, 0, 0, 0, 0
218079 };
218080
218081
218082
218083 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
218084 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
218085 rbu_file *pFd = (rbu_file *)pFile;
218086 int rc = SQLITE_OK;
218087 const char *zOpen = zName;
218088 int oflags = flags;
218089
218090 memset(pFd, 0, sizeof(rbu_file));
218091 pFd->pReal = (sqlite3_file*)&pFd[1];
218092 pFd->pRbuVfs = pRbuVfs;
218093 pFd->openFlags = flags;
218094 if( zName ){
218095 if( flags & SQLITE_OPEN_MAIN_DB ){
218096 /* A main database has just been opened. The following block sets
218097 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
218098 ** the name of the *-wal file this db connection will use. SQLite
218099 ** happens to pass a pointer to this buffer when using xAccess()
218100 ** or xOpen() to operate on the *-wal file. */
218101 pFd->zWal = sqlite3_filename_wal(zName);
218102 }
218103 else if( flags & SQLITE_OPEN_WAL ){
218104 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
218105 if( pDb ){
218106 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
218107 /* This call is to open a *-wal file. Intead, open the *-oal. */
218108 size_t nOpen;
218109 if( rbuIsVacuum(pDb->pRbu) ){
218110 zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
218111 zOpen = sqlite3_filename_wal(zOpen);
218112 }
218113 nOpen = strlen(zOpen);
218114 ((char*)zOpen)[nOpen-3] = 'o';
218115 pFd->pRbu = pDb->pRbu;
218116 }
218117 pDb->pWalFd = pFd;
218118 }
218119 }
218120 }else{
218121 pFd->pRbu = pRbuVfs->pRbu;
218122 }
218123
218124 if( oflags & SQLITE_OPEN_MAIN_DB
218125 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
218126 ){
218127 assert( oflags & SQLITE_OPEN_MAIN_DB );
218128 oflags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
218129 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
218130 zOpen = 0;
218131 }
218132
218133 if( rc==SQLITE_OK ){
218134 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
218135 }
218136 if( pFd->pReal->pMethods ){
218137 const sqlite3_io_methods *pMeth = pFd->pReal->pMethods;
218138 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
218139 ** pointer and, if the file is a main database file, link it into the
218140 ** mutex protected linked list of all such files. */
218141 if( pMeth->iVersion<2 || pMeth->xShmLock==0 ){
218142 pFile->pMethods = &rbuvfs_io_methods1;
218143 }else{
218144 pFile->pMethods = &rbuvfs_io_methods;
218145 }
218146 if( flags & SQLITE_OPEN_MAIN_DB ){
218147 rbuMainlistAdd(pFd);
218148 }
218149 }else{
218150 sqlite3_free(pFd->zDel);
218151 }
218152
218153 return rc;
218154}
218155
218156/*
218157** Delete the file located at zPath.
218158*/
218159static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
218160 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218161 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
218162}
218163
218164/*
218165** Test for access permissions. Return true if the requested permission
218166** is available, or false otherwise.
218167*/
218168static int rbuVfsAccess(
218169 sqlite3_vfs *pVfs,
218170 const char *zPath,
218171 int flags,
218172 int *pResOut
218173){
218174 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
218175 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
218176 int rc;
218177
218178 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
218179
218180 /* If this call is to check if a *-wal file associated with an RBU target
218181 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
218182 ** the following special handling is activated:
218183 **
218184 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
218185 ** ensures that the RBU extension never tries to update a database
218186 ** in wal mode, even if the first page of the database file has
218187 ** been damaged.
218188 **
218189 ** b) if the *-wal file does not exist, claim that it does anyway,
218190 ** causing SQLite to call xOpen() to open it. This call will also
218191 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
218192 ** file opened instead.
218193 */
218194 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
218195 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
218196 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
218197 assert( pDb->pRbu );
218198 if( *pResOut ){
218199 rc = SQLITE_CANTOPEN;
218200 }else{
218201 sqlite3_int64 sz = 0;
218202 rc = rbuVfsFileSize(&pDb->base, &sz);
218203 *pResOut = (sz>0);
218204 }
218205 }
218206 }
218207
218208 return rc;
218209}
218210
218211/*
218212** Populate buffer zOut with the full canonical pathname corresponding
218213** to the pathname in zPath. zOut is guaranteed to point to a buffer
218214** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
218215*/
218216static int rbuVfsFullPathname(
218217 sqlite3_vfs *pVfs,
218218 const char *zPath,
218219 int nOut,
218220 char *zOut
218221){
218222 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218223 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
218224}
218225
218226#ifndef SQLITE_OMIT_LOAD_EXTENSION
218227/*
218228** Open the dynamic library located at zPath and return a handle.
218229*/
218230static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
218231 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218232 return pRealVfs->xDlOpen(pRealVfs, zPath);
218233}
218234
218235/*
218236** Populate the buffer zErrMsg (size nByte bytes) with a human readable
218237** utf-8 string describing the most recent error encountered associated
218238** with dynamic libraries.
218239*/
218240static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
218241 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218242 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
218243}
218244
218245/*
218246** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
218247*/
218248static void (*rbuVfsDlSym(
218249 sqlite3_vfs *pVfs,
218250 void *pArg,
218251 const char *zSym
218252))(void){
218253 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218254 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
218255}
218256
218257/*
218258** Close the dynamic library handle pHandle.
218259*/
218260static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
218261 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218262 pRealVfs->xDlClose(pRealVfs, pHandle);
218263}
218264#endif /* SQLITE_OMIT_LOAD_EXTENSION */
218265
218266/*
218267** Populate the buffer pointed to by zBufOut with nByte bytes of
218268** random data.
218269*/
218270static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
218271 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218272 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
218273}
218274
218275/*
218276** Sleep for nMicro microseconds. Return the number of microseconds
218277** actually slept.
218278*/
218279static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
218280 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218281 return pRealVfs->xSleep(pRealVfs, nMicro);
218282}
218283
218284/*
218285** Return the current time as a Julian Day number in *pTimeOut.
218286*/
218287static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
218288 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
218289 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
218290}
218291
218292/*
218293** No-op.
218294*/
218295static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
218296 return 0;
218297}
218298
218299/*
218300** Deregister and destroy an RBU vfs created by an earlier call to
218301** sqlite3rbu_create_vfs().
218302*/
218303SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
218304 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
218305 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
218306 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
218307 sqlite3_vfs_unregister(pVfs);
218308 sqlite3_free(pVfs);
218309 }
218310}
218311
218312/*
218313** Create an RBU VFS named zName that accesses the underlying file-system
218314** via existing VFS zParent. The new object is registered as a non-default
218315** VFS with SQLite before returning.
218316*/
218317SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
218318
218319 /* Template for VFS */
218320 static sqlite3_vfs vfs_template = {
218321 1, /* iVersion */
218322 0, /* szOsFile */
218323 0, /* mxPathname */
218324 0, /* pNext */
218325 0, /* zName */
218326 0, /* pAppData */
218327 rbuVfsOpen, /* xOpen */
218328 rbuVfsDelete, /* xDelete */
218329 rbuVfsAccess, /* xAccess */
218330 rbuVfsFullPathname, /* xFullPathname */
218331
218332#ifndef SQLITE_OMIT_LOAD_EXTENSION
218333 rbuVfsDlOpen, /* xDlOpen */
218334 rbuVfsDlError, /* xDlError */
218335 rbuVfsDlSym, /* xDlSym */
218336 rbuVfsDlClose, /* xDlClose */
218337#else
218338 0, 0, 0, 0,
218339#endif
218340
218341 rbuVfsRandomness, /* xRandomness */
218342 rbuVfsSleep, /* xSleep */
218343 rbuVfsCurrentTime, /* xCurrentTime */
218344 rbuVfsGetLastError, /* xGetLastError */
218345 0, /* xCurrentTimeInt64 (version 2) */
218346 0, 0, 0 /* Unimplemented version 3 methods */
218347 };
218348
218349 rbu_vfs *pNew = 0; /* Newly allocated VFS */
218350 int rc = SQLITE_OK;
218351 size_t nName;
218352 size_t nByte;
218353
218354 nName = strlen(zName);
218355 nByte = sizeof(rbu_vfs) + nName + 1;
218356 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
218357 if( pNew==0 ){
218358 rc = SQLITE_NOMEM;
218359 }else{
218360 sqlite3_vfs *pParent; /* Parent VFS */
218361 memset(pNew, 0, nByte);
218362 pParent = sqlite3_vfs_find(zParent);
218363 if( pParent==0 ){
218364 rc = SQLITE_NOTFOUND;
218365 }else{
218366 char *zSpace;
218367 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
218368 pNew->base.mxPathname = pParent->mxPathname;
218369 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
218370 pNew->pRealVfs = pParent;
218371 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
218372 memcpy(zSpace, zName, nName);
218373
218374 /* Allocate the mutex and register the new VFS (not as the default) */
218375 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
218376 if( pNew->mutex==0 ){
218377 rc = SQLITE_NOMEM;
218378 }else{
218379 rc = sqlite3_vfs_register(&pNew->base, 0);
218380 }
218381 }
218382
218383 if( rc!=SQLITE_OK ){
218384 sqlite3_mutex_free(pNew->mutex);
218385 sqlite3_free(pNew);
218386 }
218387 }
218388
218389 return rc;
218390}
218391
218392/*
218393** Configure the aggregate temp file size limit for this RBU handle.
218394*/
218395SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
218396 if( n>=0 ){
218397 pRbu->szTempLimit = n;
218398 }
218399 return pRbu->szTempLimit;
218400}
218401
218402SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
218403 return pRbu->szTemp;
218404}
218405
218406
218407/**************************************************************************/
218408
218409#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
218410
218411/************** End of sqlite3rbu.c ******************************************/
218412/************** Begin file dbstat.c ******************************************/
218413/*
218414** 2010 July 12
218415**
218416** The author disclaims copyright to this source code. In place of
218417** a legal notice, here is a blessing:
218418**
218419** May you do good and not evil.
218420** May you find forgiveness for yourself and forgive others.
218421** May you share freely, never taking more than you give.
218422**
218423******************************************************************************
218424**
218425** This file contains an implementation of the "dbstat" virtual table.
218426**
218427** The dbstat virtual table is used to extract low-level storage
218428** information from an SQLite database in order to implement the
218429** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
218430** for an example implementation.
218431**
218432** Additional information is available on the "dbstat.html" page of the
218433** official SQLite documentation.
218434*/
218435
218436/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
218437#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
218438 && !defined(SQLITE_OMIT_VIRTUALTABLE)
218439
218440/*
218441** The pager and btree modules arrange objects in memory so that there are
218442** always approximately 200 bytes of addressable memory following each page
218443** buffer. This way small buffer overreads caused by corrupt database pages
218444** do not cause undefined behaviour. This module pads each page buffer
218445** by the following number of bytes for the same purpose.
218446*/
218447#define DBSTAT_PAGE_PADDING_BYTES 256
218448
218449/*
218450** Page paths:
218451**
218452** The value of the 'path' column describes the path taken from the
218453** root-node of the b-tree structure to each page. The value of the
218454** root-node path is '/'.
218455**
218456** The value of the path for the left-most child page of the root of
218457** a b-tree is '/000/'. (Btrees store content ordered from left to right
218458** so the pages to the left have smaller keys than the pages to the right.)
218459** The next to left-most child of the root page is
218460** '/001', and so on, each sibling page identified by a 3-digit hex
218461** value. The children of the 451st left-most sibling have paths such
218462** as '/1c2/000/, '/1c2/001/' etc.
218463**
218464** Overflow pages are specified by appending a '+' character and a
218465** six-digit hexadecimal value to the path to the cell they are linked
218466** from. For example, the three overflow pages in a chain linked from
218467** the left-most cell of the 450th child of the root page are identified
218468** by the paths:
218469**
218470** '/1c2/000+000000' // First page in overflow chain
218471** '/1c2/000+000001' // Second page in overflow chain
218472** '/1c2/000+000002' // Third page in overflow chain
218473**
218474** If the paths are sorted using the BINARY collation sequence, then
218475** the overflow pages associated with a cell will appear earlier in the
218476** sort-order than its child page:
218477**
218478** '/1c2/000/' // Left-most child of 451st child of root
218479*/
218480static const char zDbstatSchema[] =
218481 "CREATE TABLE x("
218482 " name TEXT," /* 0 Name of table or index */
218483 " path TEXT," /* 1 Path to page from root (NULL for agg) */
218484 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
218485 " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */
218486 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
218487 " payload INTEGER," /* 5 Bytes of payload on this page */
218488 " unused INTEGER," /* 6 Bytes of unused space on this page */
218489 " mx_payload INTEGER," /* 7 Largest payload size of all cells */
218490 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
218491 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
218492 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */
218493 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */
218494 ")"
218495;
218496
218497/* Forward reference to data structured used in this module */
218498typedef struct StatTable StatTable;
218499typedef struct StatCursor StatCursor;
218500typedef struct StatPage StatPage;
218501typedef struct StatCell StatCell;
218502
218503/* Size information for a single cell within a btree page */
218504struct StatCell {
218505 int nLocal; /* Bytes of local payload */
218506 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
218507 int nOvfl; /* Entries in aOvfl[] */
218508 u32 *aOvfl; /* Array of overflow page numbers */
218509 int nLastOvfl; /* Bytes of payload on final overflow page */
218510 int iOvfl; /* Iterates through aOvfl[] */
218511};
218512
218513/* Size information for a single btree page */
218514struct StatPage {
218515 u32 iPgno; /* Page number */
218516 u8 *aPg; /* Page buffer from sqlite3_malloc() */
218517 int iCell; /* Current cell */
218518 char *zPath; /* Path to this page */
218519
218520 /* Variables populated by statDecodePage(): */
218521 u8 flags; /* Copy of flags byte */
218522 int nCell; /* Number of cells on page */
218523 int nUnused; /* Number of unused bytes on page */
218524 StatCell *aCell; /* Array of parsed cells */
218525 u32 iRightChildPg; /* Right-child page number (or 0) */
218526 int nMxPayload; /* Largest payload of any cell on the page */
218527};
218528
218529/* The cursor for scanning the dbstat virtual table */
218530struct StatCursor {
218531 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
218532 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
218533 u8 isEof; /* After pStmt has returned SQLITE_DONE */
218534 u8 isAgg; /* Aggregate results for each table */
218535 int iDb; /* Schema used for this query */
218536
218537 StatPage aPage[32]; /* Pages in path to current page */
218538 int iPage; /* Current entry in aPage[] */
218539
218540 /* Values to return. */
218541 u32 iPageno; /* Value of 'pageno' column */
218542 char *zName; /* Value of 'name' column */
218543 char *zPath; /* Value of 'path' column */
218544 char *zPagetype; /* Value of 'pagetype' column */
218545 int nPage; /* Number of pages in current btree */
218546 int nCell; /* Value of 'ncell' column */
218547 int nMxPayload; /* Value of 'mx_payload' column */
218548 i64 nUnused; /* Value of 'unused' column */
218549 i64 nPayload; /* Value of 'payload' column */
218550 i64 iOffset; /* Value of 'pgOffset' column */
218551 i64 szPage; /* Value of 'pgSize' column */
218552};
218553
218554/* An instance of the DBSTAT virtual table */
218555struct StatTable {
218556 sqlite3_vtab base; /* base class. MUST BE FIRST! */
218557 sqlite3 *db; /* Database connection that owns this vtab */
218558 int iDb; /* Index of database to analyze */
218559};
218560
218561#ifndef get2byte
218562# define get2byte(x) ((x)[0]<<8 | (x)[1])
218563#endif
218564
218565/*
218566** Connect to or create a new DBSTAT virtual table.
218567*/
218568static int statConnect(
218569 sqlite3 *db,
218570 void *pAux,
218571 int argc, const char *const*argv,
218572 sqlite3_vtab **ppVtab,
218573 char **pzErr
218574){
218575 StatTable *pTab = 0;
218576 int rc = SQLITE_OK;
218577 int iDb;
218578 (void)pAux;
218579
218580 if( argc>=4 ){
218581 Token nm;
218582 sqlite3TokenInit(&nm, (char*)argv[3]);
218583 iDb = sqlite3FindDb(db, &nm);
218584 if( iDb<0 ){
218585 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
218586 return SQLITE_ERROR;
218587 }
218588 }else{
218589 iDb = 0;
218590 }
218591 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
218592 rc = sqlite3_declare_vtab(db, zDbstatSchema);
218593 if( rc==SQLITE_OK ){
218594 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
218595 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
218596 }
218597
218598 assert( rc==SQLITE_OK || pTab==0 );
218599 if( rc==SQLITE_OK ){
218600 memset(pTab, 0, sizeof(StatTable));
218601 pTab->db = db;
218602 pTab->iDb = iDb;
218603 }
218604
218605 *ppVtab = (sqlite3_vtab*)pTab;
218606 return rc;
218607}
218608
218609/*
218610** Disconnect from or destroy the DBSTAT virtual table.
218611*/
218612static int statDisconnect(sqlite3_vtab *pVtab){
218613 sqlite3_free(pVtab);
218614 return SQLITE_OK;
218615}
218616
218617/*
218618** Compute the best query strategy and return the result in idxNum.
218619**
218620** idxNum-Bit Meaning
218621** ---------- ----------------------------------------------
218622** 0x01 There is a schema=? term in the WHERE clause
218623** 0x02 There is a name=? term in the WHERE clause
218624** 0x04 There is an aggregate=? term in the WHERE clause
218625** 0x08 Output should be ordered by name and path
218626*/
218627static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
218628 int i;
218629 int iSchema = -1;
218630 int iName = -1;
218631 int iAgg = -1;
218632 (void)tab;
218633
218634 /* Look for a valid schema=? constraint. If found, change the idxNum to
218635 ** 1 and request the value of that constraint be sent to xFilter. And
218636 ** lower the cost estimate to encourage the constrained version to be
218637 ** used.
218638 */
218639 for(i=0; i<pIdxInfo->nConstraint; i++){
218640 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
218641 if( pIdxInfo->aConstraint[i].usable==0 ){
218642 /* Force DBSTAT table should always be the right-most table in a join */
218643 return SQLITE_CONSTRAINT;
218644 }
218645 switch( pIdxInfo->aConstraint[i].iColumn ){
218646 case 0: { /* name */
218647 iName = i;
218648 break;
218649 }
218650 case 10: { /* schema */
218651 iSchema = i;
218652 break;
218653 }
218654 case 11: { /* aggregate */
218655 iAgg = i;
218656 break;
218657 }
218658 }
218659 }
218660 i = 0;
218661 if( iSchema>=0 ){
218662 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
218663 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
218664 pIdxInfo->idxNum |= 0x01;
218665 }
218666 if( iName>=0 ){
218667 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
218668 pIdxInfo->idxNum |= 0x02;
218669 }
218670 if( iAgg>=0 ){
218671 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
218672 pIdxInfo->idxNum |= 0x04;
218673 }
218674 pIdxInfo->estimatedCost = 1.0;
218675
218676 /* Records are always returned in ascending order of (name, path).
218677 ** If this will satisfy the client, set the orderByConsumed flag so that
218678 ** SQLite does not do an external sort.
218679 */
218680 if( ( pIdxInfo->nOrderBy==1
218681 && pIdxInfo->aOrderBy[0].iColumn==0
218682 && pIdxInfo->aOrderBy[0].desc==0
218683 ) ||
218684 ( pIdxInfo->nOrderBy==2
218685 && pIdxInfo->aOrderBy[0].iColumn==0
218686 && pIdxInfo->aOrderBy[0].desc==0
218687 && pIdxInfo->aOrderBy[1].iColumn==1
218688 && pIdxInfo->aOrderBy[1].desc==0
218689 )
218690 ){
218691 pIdxInfo->orderByConsumed = 1;
218692 pIdxInfo->idxNum |= 0x08;
218693 }
218694
218695 return SQLITE_OK;
218696}
218697
218698/*
218699** Open a new DBSTAT cursor.
218700*/
218701static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
218702 StatTable *pTab = (StatTable *)pVTab;
218703 StatCursor *pCsr;
218704
218705 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
218706 if( pCsr==0 ){
218707 return SQLITE_NOMEM_BKPT;
218708 }else{
218709 memset(pCsr, 0, sizeof(StatCursor));
218710 pCsr->base.pVtab = pVTab;
218711 pCsr->iDb = pTab->iDb;
218712 }
218713
218714 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
218715 return SQLITE_OK;
218716}
218717
218718static void statClearCells(StatPage *p){
218719 int i;
218720 if( p->aCell ){
218721 for(i=0; i<p->nCell; i++){
218722 sqlite3_free(p->aCell[i].aOvfl);
218723 }
218724 sqlite3_free(p->aCell);
218725 }
218726 p->nCell = 0;
218727 p->aCell = 0;
218728}
218729
218730static void statClearPage(StatPage *p){
218731 u8 *aPg = p->aPg;
218732 statClearCells(p);
218733 sqlite3_free(p->zPath);
218734 memset(p, 0, sizeof(StatPage));
218735 p->aPg = aPg;
218736}
218737
218738static void statResetCsr(StatCursor *pCsr){
218739 int i;
218740 /* In some circumstances, specifically if an OOM has occurred, the call
218741 ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
218742 ** important that statClearPage() is called to free any page refs before
218743 ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
218744 for(i=0; i<ArraySize(pCsr->aPage); i++){
218745 statClearPage(&pCsr->aPage[i]);
218746 sqlite3_free(pCsr->aPage[i].aPg);
218747 pCsr->aPage[i].aPg = 0;
218748 }
218749 sqlite3_reset(pCsr->pStmt);
218750 pCsr->iPage = 0;
218751 sqlite3_free(pCsr->zPath);
218752 pCsr->zPath = 0;
218753 pCsr->isEof = 0;
218754}
218755
218756/* Resize the space-used counters inside of the cursor */
218757static void statResetCounts(StatCursor *pCsr){
218758 pCsr->nCell = 0;
218759 pCsr->nMxPayload = 0;
218760 pCsr->nUnused = 0;
218761 pCsr->nPayload = 0;
218762 pCsr->szPage = 0;
218763 pCsr->nPage = 0;
218764}
218765
218766/*
218767** Close a DBSTAT cursor.
218768*/
218769static int statClose(sqlite3_vtab_cursor *pCursor){
218770 StatCursor *pCsr = (StatCursor *)pCursor;
218771 statResetCsr(pCsr);
218772 sqlite3_finalize(pCsr->pStmt);
218773 sqlite3_free(pCsr);
218774 return SQLITE_OK;
218775}
218776
218777/*
218778** For a single cell on a btree page, compute the number of bytes of
218779** content (payload) stored on that page. That is to say, compute the
218780** number of bytes of content not found on overflow pages.
218781*/
218782static int getLocalPayload(
218783 int nUsable, /* Usable bytes per page */
218784 u8 flags, /* Page flags */
218785 int nTotal /* Total record (payload) size */
218786){
218787 int nLocal;
218788 int nMinLocal;
218789 int nMaxLocal;
218790
218791 if( flags==0x0D ){ /* Table leaf node */
218792 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
218793 nMaxLocal = nUsable - 35;
218794 }else{ /* Index interior and leaf nodes */
218795 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
218796 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
218797 }
218798
218799 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
218800 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
218801 return nLocal;
218802}
218803
218804/* Populate the StatPage object with information about the all
218805** cells found on the page currently under analysis.
218806*/
218807static int statDecodePage(Btree *pBt, StatPage *p){
218808 int nUnused;
218809 int iOff;
218810 int nHdr;
218811 int isLeaf;
218812 int szPage;
218813
218814 u8 *aData = p->aPg;
218815 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
218816
218817 p->flags = aHdr[0];
218818 if( p->flags==0x0A || p->flags==0x0D ){
218819 isLeaf = 1;
218820 nHdr = 8;
218821 }else if( p->flags==0x05 || p->flags==0x02 ){
218822 isLeaf = 0;
218823 nHdr = 12;
218824 }else{
218825 goto statPageIsCorrupt;
218826 }
218827 if( p->iPgno==1 ) nHdr += 100;
218828 p->nCell = get2byte(&aHdr[3]);
218829 p->nMxPayload = 0;
218830 szPage = sqlite3BtreeGetPageSize(pBt);
218831
218832 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
218833 nUnused += (int)aHdr[7];
218834 iOff = get2byte(&aHdr[1]);
218835 while( iOff ){
218836 int iNext;
218837 if( iOff>=szPage ) goto statPageIsCorrupt;
218838 nUnused += get2byte(&aData[iOff+2]);
218839 iNext = get2byte(&aData[iOff]);
218840 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
218841 iOff = iNext;
218842 }
218843 p->nUnused = nUnused;
218844 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
218845
218846 if( p->nCell ){
218847 int i; /* Used to iterate through cells */
218848 int nUsable; /* Usable bytes per page */
218849
218850 sqlite3BtreeEnter(pBt);
218851 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
218852 sqlite3BtreeLeave(pBt);
218853 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
218854 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
218855 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
218856
218857 for(i=0; i<p->nCell; i++){
218858 StatCell *pCell = &p->aCell[i];
218859
218860 iOff = get2byte(&aData[nHdr+i*2]);
218861 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
218862 if( !isLeaf ){
218863 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
218864 iOff += 4;
218865 }
218866 if( p->flags==0x05 ){
218867 /* A table interior node. nPayload==0. */
218868 }else{
218869 u32 nPayload; /* Bytes of payload total (local+overflow) */
218870 int nLocal; /* Bytes of payload stored locally */
218871 iOff += getVarint32(&aData[iOff], nPayload);
218872 if( p->flags==0x0D ){
218873 u64 dummy;
218874 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
218875 }
218876 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
218877 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
218878 if( nLocal<0 ) goto statPageIsCorrupt;
218879 pCell->nLocal = nLocal;
218880 assert( nPayload>=(u32)nLocal );
218881 assert( nLocal<=(nUsable-35) );
218882 if( nPayload>(u32)nLocal ){
218883 int j;
218884 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
218885 if( iOff+nLocal+4>nUsable || nPayload>0x7fffffff ){
218886 goto statPageIsCorrupt;
218887 }
218888 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
218889 pCell->nOvfl = nOvfl;
218890 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
218891 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
218892 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
218893 for(j=1; j<nOvfl; j++){
218894 int rc;
218895 u32 iPrev = pCell->aOvfl[j-1];
218896 DbPage *pPg = 0;
218897 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
218898 if( rc!=SQLITE_OK ){
218899 assert( pPg==0 );
218900 return rc;
218901 }
218902 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
218903 sqlite3PagerUnref(pPg);
218904 }
218905 }
218906 }
218907 }
218908 }
218909
218910 return SQLITE_OK;
218911
218912statPageIsCorrupt:
218913 p->flags = 0;
218914 statClearCells(p);
218915 return SQLITE_OK;
218916}
218917
218918/*
218919** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
218920** the current value of pCsr->iPageno.
218921*/
218922static void statSizeAndOffset(StatCursor *pCsr){
218923 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
218924 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
218925 Pager *pPager = sqlite3BtreePager(pBt);
218926 sqlite3_file *fd;
218927 sqlite3_int64 x[2];
218928
218929 /* If connected to a ZIPVFS backend, find the page size and
218930 ** offset from ZIPVFS.
218931 */
218932 fd = sqlite3PagerFile(pPager);
218933 x[0] = pCsr->iPageno;
218934 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
218935 pCsr->iOffset = x[0];
218936 pCsr->szPage += x[1];
218937 }else{
218938 /* Not ZIPVFS: The default page size and offset */
218939 pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
218940 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
218941 }
218942}
218943
218944/*
218945** Load a copy of the page data for page iPg into the buffer belonging
218946** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
218947** if successful, or an SQLite error code otherwise.
218948*/
218949static int statGetPage(
218950 Btree *pBt, /* Load page from this b-tree */
218951 u32 iPg, /* Page number to load */
218952 StatPage *pPg /* Load page into this object */
218953){
218954 int pgsz = sqlite3BtreeGetPageSize(pBt);
218955 DbPage *pDbPage = 0;
218956 int rc;
218957
218958 if( pPg->aPg==0 ){
218959 pPg->aPg = (u8*)sqlite3_malloc(pgsz + DBSTAT_PAGE_PADDING_BYTES);
218960 if( pPg->aPg==0 ){
218961 return SQLITE_NOMEM_BKPT;
218962 }
218963 memset(&pPg->aPg[pgsz], 0, DBSTAT_PAGE_PADDING_BYTES);
218964 }
218965
218966 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPg, &pDbPage, 0);
218967 if( rc==SQLITE_OK ){
218968 const u8 *a = sqlite3PagerGetData(pDbPage);
218969 memcpy(pPg->aPg, a, pgsz);
218970 sqlite3PagerUnref(pDbPage);
218971 }
218972
218973 return rc;
218974}
218975
218976/*
218977** Move a DBSTAT cursor to the next entry. Normally, the next
218978** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
218979** the next entry is the next btree.
218980*/
218981static int statNext(sqlite3_vtab_cursor *pCursor){
218982 int rc;
218983 int nPayload;
218984 char *z;
218985 StatCursor *pCsr = (StatCursor *)pCursor;
218986 StatTable *pTab = (StatTable *)pCursor->pVtab;
218987 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
218988 Pager *pPager = sqlite3BtreePager(pBt);
218989
218990 sqlite3_free(pCsr->zPath);
218991 pCsr->zPath = 0;
218992
218993statNextRestart:
218994 if( pCsr->iPage<0 ){
218995 /* Start measuring space on the next btree */
218996 statResetCounts(pCsr);
218997 rc = sqlite3_step(pCsr->pStmt);
218998 if( rc==SQLITE_ROW ){
218999 int nPage;
219000 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
219001 sqlite3PagerPagecount(pPager, &nPage);
219002 if( nPage==0 ){
219003 pCsr->isEof = 1;
219004 return sqlite3_reset(pCsr->pStmt);
219005 }
219006 rc = statGetPage(pBt, iRoot, &pCsr->aPage[0]);
219007 pCsr->aPage[0].iPgno = iRoot;
219008 pCsr->aPage[0].iCell = 0;
219009 if( !pCsr->isAgg ){
219010 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
219011 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
219012 }
219013 pCsr->iPage = 0;
219014 pCsr->nPage = 1;
219015 }else{
219016 pCsr->isEof = 1;
219017 return sqlite3_reset(pCsr->pStmt);
219018 }
219019 }else{
219020 /* Continue analyzing the btree previously started */
219021 StatPage *p = &pCsr->aPage[pCsr->iPage];
219022 if( !pCsr->isAgg ) statResetCounts(pCsr);
219023 while( p->iCell<p->nCell ){
219024 StatCell *pCell = &p->aCell[p->iCell];
219025 while( pCell->iOvfl<pCell->nOvfl ){
219026 int nUsable, iOvfl;
219027 sqlite3BtreeEnter(pBt);
219028 nUsable = sqlite3BtreeGetPageSize(pBt) -
219029 sqlite3BtreeGetReserveNoMutex(pBt);
219030 sqlite3BtreeLeave(pBt);
219031 pCsr->nPage++;
219032 statSizeAndOffset(pCsr);
219033 if( pCell->iOvfl<pCell->nOvfl-1 ){
219034 pCsr->nPayload += nUsable - 4;
219035 }else{
219036 pCsr->nPayload += pCell->nLastOvfl;
219037 pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
219038 }
219039 iOvfl = pCell->iOvfl;
219040 pCell->iOvfl++;
219041 if( !pCsr->isAgg ){
219042 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
219043 pCsr->iPageno = pCell->aOvfl[iOvfl];
219044 pCsr->zPagetype = "overflow";
219045 pCsr->zPath = z = sqlite3_mprintf(
219046 "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
219047 );
219048 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
219049 }
219050 }
219051 if( p->iRightChildPg ) break;
219052 p->iCell++;
219053 }
219054
219055 if( !p->iRightChildPg || p->iCell>p->nCell ){
219056 statClearPage(p);
219057 pCsr->iPage--;
219058 if( pCsr->isAgg && pCsr->iPage<0 ){
219059 /* label-statNext-done: When computing aggregate space usage over
219060 ** an entire btree, this is the exit point from this function */
219061 return SQLITE_OK;
219062 }
219063 goto statNextRestart; /* Tail recursion */
219064 }
219065 pCsr->iPage++;
219066 if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
219067 statResetCsr(pCsr);
219068 return SQLITE_CORRUPT_BKPT;
219069 }
219070 assert( p==&pCsr->aPage[pCsr->iPage-1] );
219071
219072 if( p->iCell==p->nCell ){
219073 p[1].iPgno = p->iRightChildPg;
219074 }else{
219075 p[1].iPgno = p->aCell[p->iCell].iChildPg;
219076 }
219077 rc = statGetPage(pBt, p[1].iPgno, &p[1]);
219078 pCsr->nPage++;
219079 p[1].iCell = 0;
219080 if( !pCsr->isAgg ){
219081 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
219082 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
219083 }
219084 p->iCell++;
219085 }
219086
219087
219088 /* Populate the StatCursor fields with the values to be returned
219089 ** by the xColumn() and xRowid() methods.
219090 */
219091 if( rc==SQLITE_OK ){
219092 int i;
219093 StatPage *p = &pCsr->aPage[pCsr->iPage];
219094 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
219095 pCsr->iPageno = p->iPgno;
219096
219097 rc = statDecodePage(pBt, p);
219098 if( rc==SQLITE_OK ){
219099 statSizeAndOffset(pCsr);
219100
219101 switch( p->flags ){
219102 case 0x05: /* table internal */
219103 case 0x02: /* index internal */
219104 pCsr->zPagetype = "internal";
219105 break;
219106 case 0x0D: /* table leaf */
219107 case 0x0A: /* index leaf */
219108 pCsr->zPagetype = "leaf";
219109 break;
219110 default:
219111 pCsr->zPagetype = "corrupted";
219112 break;
219113 }
219114 pCsr->nCell += p->nCell;
219115 pCsr->nUnused += p->nUnused;
219116 if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
219117 if( !pCsr->isAgg ){
219118 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
219119 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
219120 }
219121 nPayload = 0;
219122 for(i=0; i<p->nCell; i++){
219123 nPayload += p->aCell[i].nLocal;
219124 }
219125 pCsr->nPayload += nPayload;
219126
219127 /* If computing aggregate space usage by btree, continue with the
219128 ** next page. The loop will exit via the return at label-statNext-done
219129 */
219130 if( pCsr->isAgg ) goto statNextRestart;
219131 }
219132 }
219133
219134 return rc;
219135}
219136
219137static int statEof(sqlite3_vtab_cursor *pCursor){
219138 StatCursor *pCsr = (StatCursor *)pCursor;
219139 return pCsr->isEof;
219140}
219141
219142/* Initialize a cursor according to the query plan idxNum using the
219143** arguments in argv[0]. See statBestIndex() for a description of the
219144** meaning of the bits in idxNum.
219145*/
219146static int statFilter(
219147 sqlite3_vtab_cursor *pCursor,
219148 int idxNum, const char *idxStr,
219149 int argc, sqlite3_value **argv
219150){
219151 StatCursor *pCsr = (StatCursor *)pCursor;
219152 StatTable *pTab = (StatTable*)(pCursor->pVtab);
219153 sqlite3_str *pSql; /* Query of btrees to analyze */
219154 char *zSql; /* String value of pSql */
219155 int iArg = 0; /* Count of argv[] parameters used so far */
219156 int rc = SQLITE_OK; /* Result of this operation */
219157 const char *zName = 0; /* Only provide analysis of this table */
219158 (void)argc;
219159 (void)idxStr;
219160
219161 statResetCsr(pCsr);
219162 sqlite3_finalize(pCsr->pStmt);
219163 pCsr->pStmt = 0;
219164 if( idxNum & 0x01 ){
219165 /* schema=? constraint is present. Get its value */
219166 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
219167 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
219168 if( pCsr->iDb<0 ){
219169 pCsr->iDb = 0;
219170 pCsr->isEof = 1;
219171 return SQLITE_OK;
219172 }
219173 }else{
219174 pCsr->iDb = pTab->iDb;
219175 }
219176 if( idxNum & 0x02 ){
219177 /* name=? constraint is present */
219178 zName = (const char*)sqlite3_value_text(argv[iArg++]);
219179 }
219180 if( idxNum & 0x04 ){
219181 /* aggregate=? constraint is present */
219182 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
219183 }else{
219184 pCsr->isAgg = 0;
219185 }
219186 pSql = sqlite3_str_new(pTab->db);
219187 sqlite3_str_appendf(pSql,
219188 "SELECT * FROM ("
219189 "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
219190 " UNION ALL "
219191 "SELECT name,rootpage,type"
219192 " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
219193 pTab->db->aDb[pCsr->iDb].zDbSName);
219194 if( zName ){
219195 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
219196 }
219197 if( idxNum & 0x08 ){
219198 sqlite3_str_appendf(pSql, " ORDER BY name");
219199 }
219200 zSql = sqlite3_str_finish(pSql);
219201 if( zSql==0 ){
219202 return SQLITE_NOMEM_BKPT;
219203 }else{
219204 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
219205 sqlite3_free(zSql);
219206 }
219207
219208 if( rc==SQLITE_OK ){
219209 pCsr->iPage = -1;
219210 rc = statNext(pCursor);
219211 }
219212 return rc;
219213}
219214
219215static int statColumn(
219216 sqlite3_vtab_cursor *pCursor,
219217 sqlite3_context *ctx,
219218 int i
219219){
219220 StatCursor *pCsr = (StatCursor *)pCursor;
219221 switch( i ){
219222 case 0: /* name */
219223 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
219224 break;
219225 case 1: /* path */
219226 if( !pCsr->isAgg ){
219227 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
219228 }
219229 break;
219230 case 2: /* pageno */
219231 if( pCsr->isAgg ){
219232 sqlite3_result_int64(ctx, pCsr->nPage);
219233 }else{
219234 sqlite3_result_int64(ctx, pCsr->iPageno);
219235 }
219236 break;
219237 case 3: /* pagetype */
219238 if( !pCsr->isAgg ){
219239 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
219240 }
219241 break;
219242 case 4: /* ncell */
219243 sqlite3_result_int64(ctx, pCsr->nCell);
219244 break;
219245 case 5: /* payload */
219246 sqlite3_result_int64(ctx, pCsr->nPayload);
219247 break;
219248 case 6: /* unused */
219249 sqlite3_result_int64(ctx, pCsr->nUnused);
219250 break;
219251 case 7: /* mx_payload */
219252 sqlite3_result_int64(ctx, pCsr->nMxPayload);
219253 break;
219254 case 8: /* pgoffset */
219255 if( !pCsr->isAgg ){
219256 sqlite3_result_int64(ctx, pCsr->iOffset);
219257 }
219258 break;
219259 case 9: /* pgsize */
219260 sqlite3_result_int64(ctx, pCsr->szPage);
219261 break;
219262 case 10: { /* schema */
219263 sqlite3 *db = sqlite3_context_db_handle(ctx);
219264 int iDb = pCsr->iDb;
219265 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
219266 break;
219267 }
219268 default: { /* aggregate */
219269 sqlite3_result_int(ctx, pCsr->isAgg);
219270 break;
219271 }
219272 }
219273 return SQLITE_OK;
219274}
219275
219276static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
219277 StatCursor *pCsr = (StatCursor *)pCursor;
219278 *pRowid = pCsr->iPageno;
219279 return SQLITE_OK;
219280}
219281
219282/*
219283** Invoke this routine to register the "dbstat" virtual table module
219284*/
219285SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
219286 static sqlite3_module dbstat_module = {
219287 0, /* iVersion */
219288 statConnect, /* xCreate */
219289 statConnect, /* xConnect */
219290 statBestIndex, /* xBestIndex */
219291 statDisconnect, /* xDisconnect */
219292 statDisconnect, /* xDestroy */
219293 statOpen, /* xOpen - open a cursor */
219294 statClose, /* xClose - close a cursor */
219295 statFilter, /* xFilter - configure scan constraints */
219296 statNext, /* xNext - advance a cursor */
219297 statEof, /* xEof - check for end of scan */
219298 statColumn, /* xColumn - read data */
219299 statRowid, /* xRowid - read data */
219300 0, /* xUpdate */
219301 0, /* xBegin */
219302 0, /* xSync */
219303 0, /* xCommit */
219304 0, /* xRollback */
219305 0, /* xFindMethod */
219306 0, /* xRename */
219307 0, /* xSavepoint */
219308 0, /* xRelease */
219309 0, /* xRollbackTo */
219310 0 /* xShadowName */
219311 };
219312 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
219313}
219314#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
219315SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
219316#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
219317
219318/************** End of dbstat.c **********************************************/
219319/************** Begin file dbpage.c ******************************************/
219320/*
219321** 2017-10-11
219322**
219323** The author disclaims copyright to this source code. In place of
219324** a legal notice, here is a blessing:
219325**
219326** May you do good and not evil.
219327** May you find forgiveness for yourself and forgive others.
219328** May you share freely, never taking more than you give.
219329**
219330******************************************************************************
219331**
219332** This file contains an implementation of the "sqlite_dbpage" virtual table.
219333**
219334** The sqlite_dbpage virtual table is used to read or write whole raw
219335** pages of the database file. The pager interface is used so that
219336** uncommitted changes and changes recorded in the WAL file are correctly
219337** retrieved.
219338**
219339** Usage example:
219340**
219341** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
219342**
219343** This is an eponymous virtual table so it does not need to be created before
219344** use. The optional argument to the sqlite_dbpage() table name is the
219345** schema for the database file that is to be read. The default schema is
219346** "main".
219347**
219348** The data field of sqlite_dbpage table can be updated. The new
219349** value must be a BLOB which is the correct page size, otherwise the
219350** update fails. Rows may not be deleted or inserted.
219351*/
219352
219353/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
219354#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
219355 && !defined(SQLITE_OMIT_VIRTUALTABLE)
219356
219357typedef struct DbpageTable DbpageTable;
219358typedef struct DbpageCursor DbpageCursor;
219359
219360struct DbpageCursor {
219361 sqlite3_vtab_cursor base; /* Base class. Must be first */
219362 int pgno; /* Current page number */
219363 int mxPgno; /* Last page to visit on this scan */
219364 Pager *pPager; /* Pager being read/written */
219365 DbPage *pPage1; /* Page 1 of the database */
219366 int iDb; /* Index of database to analyze */
219367 int szPage; /* Size of each page in bytes */
219368};
219369
219370struct DbpageTable {
219371 sqlite3_vtab base; /* Base class. Must be first */
219372 sqlite3 *db; /* The database */
219373};
219374
219375/* Columns */
219376#define DBPAGE_COLUMN_PGNO 0
219377#define DBPAGE_COLUMN_DATA 1
219378#define DBPAGE_COLUMN_SCHEMA 2
219379
219380
219381
219382/*
219383** Connect to or create a dbpagevfs virtual table.
219384*/
219385static int dbpageConnect(
219386 sqlite3 *db,
219387 void *pAux,
219388 int argc, const char *const*argv,
219389 sqlite3_vtab **ppVtab,
219390 char **pzErr
219391){
219392 DbpageTable *pTab = 0;
219393 int rc = SQLITE_OK;
219394 (void)pAux;
219395 (void)argc;
219396 (void)argv;
219397 (void)pzErr;
219398
219399 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
219400 sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS);
219401 rc = sqlite3_declare_vtab(db,
219402 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
219403 if( rc==SQLITE_OK ){
219404 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
219405 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
219406 }
219407
219408 assert( rc==SQLITE_OK || pTab==0 );
219409 if( rc==SQLITE_OK ){
219410 memset(pTab, 0, sizeof(DbpageTable));
219411 pTab->db = db;
219412 }
219413
219414 *ppVtab = (sqlite3_vtab*)pTab;
219415 return rc;
219416}
219417
219418/*
219419** Disconnect from or destroy a dbpagevfs virtual table.
219420*/
219421static int dbpageDisconnect(sqlite3_vtab *pVtab){
219422 sqlite3_free(pVtab);
219423 return SQLITE_OK;
219424}
219425
219426/*
219427** idxNum:
219428**
219429** 0 schema=main, full table scan
219430** 1 schema=main, pgno=?1
219431** 2 schema=?1, full table scan
219432** 3 schema=?1, pgno=?2
219433*/
219434static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
219435 int i;
219436 int iPlan = 0;
219437 (void)tab;
219438
219439 /* If there is a schema= constraint, it must be honored. Report a
219440 ** ridiculously large estimated cost if the schema= constraint is
219441 ** unavailable
219442 */
219443 for(i=0; i<pIdxInfo->nConstraint; i++){
219444 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
219445 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
219446 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
219447 if( !p->usable ){
219448 /* No solution. */
219449 return SQLITE_CONSTRAINT;
219450 }
219451 iPlan = 2;
219452 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
219453 pIdxInfo->aConstraintUsage[i].omit = 1;
219454 break;
219455 }
219456
219457 /* If we reach this point, it means that either there is no schema=
219458 ** constraint (in which case we use the "main" schema) or else the
219459 ** schema constraint was accepted. Lower the estimated cost accordingly
219460 */
219461 pIdxInfo->estimatedCost = 1.0e6;
219462
219463 /* Check for constraints against pgno */
219464 for(i=0; i<pIdxInfo->nConstraint; i++){
219465 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
219466 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
219467 pIdxInfo->estimatedRows = 1;
219468 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
219469 pIdxInfo->estimatedCost = 1.0;
219470 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
219471 pIdxInfo->aConstraintUsage[i].omit = 1;
219472 iPlan |= 1;
219473 break;
219474 }
219475 }
219476 pIdxInfo->idxNum = iPlan;
219477
219478 if( pIdxInfo->nOrderBy>=1
219479 && pIdxInfo->aOrderBy[0].iColumn<=0
219480 && pIdxInfo->aOrderBy[0].desc==0
219481 ){
219482 pIdxInfo->orderByConsumed = 1;
219483 }
219484 return SQLITE_OK;
219485}
219486
219487/*
219488** Open a new dbpagevfs cursor.
219489*/
219490static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
219491 DbpageCursor *pCsr;
219492
219493 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
219494 if( pCsr==0 ){
219495 return SQLITE_NOMEM_BKPT;
219496 }else{
219497 memset(pCsr, 0, sizeof(DbpageCursor));
219498 pCsr->base.pVtab = pVTab;
219499 pCsr->pgno = -1;
219500 }
219501
219502 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
219503 return SQLITE_OK;
219504}
219505
219506/*
219507** Close a dbpagevfs cursor.
219508*/
219509static int dbpageClose(sqlite3_vtab_cursor *pCursor){
219510 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
219511 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
219512 sqlite3_free(pCsr);
219513 return SQLITE_OK;
219514}
219515
219516/*
219517** Move a dbpagevfs cursor to the next entry in the file.
219518*/
219519static int dbpageNext(sqlite3_vtab_cursor *pCursor){
219520 int rc = SQLITE_OK;
219521 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
219522 pCsr->pgno++;
219523 return rc;
219524}
219525
219526static int dbpageEof(sqlite3_vtab_cursor *pCursor){
219527 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
219528 return pCsr->pgno > pCsr->mxPgno;
219529}
219530
219531/*
219532** idxNum:
219533**
219534** 0 schema=main, full table scan
219535** 1 schema=main, pgno=?1
219536** 2 schema=?1, full table scan
219537** 3 schema=?1, pgno=?2
219538**
219539** idxStr is not used
219540*/
219541static int dbpageFilter(
219542 sqlite3_vtab_cursor *pCursor,
219543 int idxNum, const char *idxStr,
219544 int argc, sqlite3_value **argv
219545){
219546 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
219547 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
219548 int rc;
219549 sqlite3 *db = pTab->db;
219550 Btree *pBt;
219551
219552 (void)idxStr;
219553
219554 /* Default setting is no rows of result */
219555 pCsr->pgno = 1;
219556 pCsr->mxPgno = 0;
219557
219558 if( idxNum & 2 ){
219559 const char *zSchema;
219560 assert( argc>=1 );
219561 zSchema = (const char*)sqlite3_value_text(argv[0]);
219562 pCsr->iDb = sqlite3FindDbName(db, zSchema);
219563 if( pCsr->iDb<0 ) return SQLITE_OK;
219564 }else{
219565 pCsr->iDb = 0;
219566 }
219567 pBt = db->aDb[pCsr->iDb].pBt;
219568 if( NEVER(pBt==0) ) return SQLITE_OK;
219569 pCsr->pPager = sqlite3BtreePager(pBt);
219570 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
219571 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
219572 if( idxNum & 1 ){
219573 assert( argc>(idxNum>>1) );
219574 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
219575 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
219576 pCsr->pgno = 1;
219577 pCsr->mxPgno = 0;
219578 }else{
219579 pCsr->mxPgno = pCsr->pgno;
219580 }
219581 }else{
219582 assert( pCsr->pgno==1 );
219583 }
219584 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
219585 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
219586 return rc;
219587}
219588
219589static int dbpageColumn(
219590 sqlite3_vtab_cursor *pCursor,
219591 sqlite3_context *ctx,
219592 int i
219593){
219594 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
219595 int rc = SQLITE_OK;
219596 switch( i ){
219597 case 0: { /* pgno */
219598 sqlite3_result_int(ctx, pCsr->pgno);
219599 break;
219600 }
219601 case 1: { /* data */
219602 DbPage *pDbPage = 0;
219603 if( pCsr->pgno==((PENDING_BYTE/pCsr->szPage)+1) ){
219604 /* The pending byte page. Assume it is zeroed out. Attempting to
219605 ** request this page from the page is an SQLITE_CORRUPT error. */
219606 sqlite3_result_zeroblob(ctx, pCsr->szPage);
219607 }else{
219608 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
219609 if( rc==SQLITE_OK ){
219610 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
219611 SQLITE_TRANSIENT);
219612 }
219613 sqlite3PagerUnref(pDbPage);
219614 }
219615 break;
219616 }
219617 default: { /* schema */
219618 sqlite3 *db = sqlite3_context_db_handle(ctx);
219619 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
219620 break;
219621 }
219622 }
219623 return rc;
219624}
219625
219626static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
219627 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
219628 *pRowid = pCsr->pgno;
219629 return SQLITE_OK;
219630}
219631
219632static int dbpageUpdate(
219633 sqlite3_vtab *pVtab,
219634 int argc,
219635 sqlite3_value **argv,
219636 sqlite_int64 *pRowid
219637){
219638 DbpageTable *pTab = (DbpageTable *)pVtab;
219639 Pgno pgno;
219640 DbPage *pDbPage = 0;
219641 int rc = SQLITE_OK;
219642 char *zErr = 0;
219643 const char *zSchema;
219644 int iDb;
219645 Btree *pBt;
219646 Pager *pPager;
219647 int szPage;
219648
219649 (void)pRowid;
219650 if( pTab->db->flags & SQLITE_Defensive ){
219651 zErr = "read-only";
219652 goto update_fail;
219653 }
219654 if( argc==1 ){
219655 zErr = "cannot delete";
219656 goto update_fail;
219657 }
219658 pgno = sqlite3_value_int(argv[0]);
219659 if( sqlite3_value_type(argv[0])==SQLITE_NULL
219660 || (Pgno)sqlite3_value_int(argv[1])!=pgno
219661 ){
219662 zErr = "cannot insert";
219663 goto update_fail;
219664 }
219665 zSchema = (const char*)sqlite3_value_text(argv[4]);
219666 iDb = ALWAYS(zSchema) ? sqlite3FindDbName(pTab->db, zSchema) : -1;
219667 if( NEVER(iDb<0) ){
219668 zErr = "no such schema";
219669 goto update_fail;
219670 }
219671 pBt = pTab->db->aDb[iDb].pBt;
219672 if( NEVER(pgno<1) || NEVER(pBt==0) || NEVER(pgno>sqlite3BtreeLastPage(pBt)) ){
219673 zErr = "bad page number";
219674 goto update_fail;
219675 }
219676 szPage = sqlite3BtreeGetPageSize(pBt);
219677 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB
219678 || sqlite3_value_bytes(argv[3])!=szPage
219679 ){
219680 zErr = "bad page value";
219681 goto update_fail;
219682 }
219683 pPager = sqlite3BtreePager(pBt);
219684 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
219685 if( rc==SQLITE_OK ){
219686 const void *pData = sqlite3_value_blob(argv[3]);
219687 assert( pData!=0 || pTab->db->mallocFailed );
219688 if( pData
219689 && (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK
219690 ){
219691 memcpy(sqlite3PagerGetData(pDbPage), pData, szPage);
219692 }
219693 }
219694 sqlite3PagerUnref(pDbPage);
219695 return rc;
219696
219697update_fail:
219698 sqlite3_free(pVtab->zErrMsg);
219699 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
219700 return SQLITE_ERROR;
219701}
219702
219703/* Since we do not know in advance which database files will be
219704** written by the sqlite_dbpage virtual table, start a write transaction
219705** on them all.
219706*/
219707static int dbpageBegin(sqlite3_vtab *pVtab){
219708 DbpageTable *pTab = (DbpageTable *)pVtab;
219709 sqlite3 *db = pTab->db;
219710 int i;
219711 for(i=0; i<db->nDb; i++){
219712 Btree *pBt = db->aDb[i].pBt;
219713 if( pBt ) (void)sqlite3BtreeBeginTrans(pBt, 1, 0);
219714 }
219715 return SQLITE_OK;
219716}
219717
219718
219719/*
219720** Invoke this routine to register the "dbpage" virtual table module
219721*/
219722SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
219723 static sqlite3_module dbpage_module = {
219724 0, /* iVersion */
219725 dbpageConnect, /* xCreate */
219726 dbpageConnect, /* xConnect */
219727 dbpageBestIndex, /* xBestIndex */
219728 dbpageDisconnect, /* xDisconnect */
219729 dbpageDisconnect, /* xDestroy */
219730 dbpageOpen, /* xOpen - open a cursor */
219731 dbpageClose, /* xClose - close a cursor */
219732 dbpageFilter, /* xFilter - configure scan constraints */
219733 dbpageNext, /* xNext - advance a cursor */
219734 dbpageEof, /* xEof - check for end of scan */
219735 dbpageColumn, /* xColumn - read data */
219736 dbpageRowid, /* xRowid - read data */
219737 dbpageUpdate, /* xUpdate */
219738 dbpageBegin, /* xBegin */
219739 0, /* xSync */
219740 0, /* xCommit */
219741 0, /* xRollback */
219742 0, /* xFindMethod */
219743 0, /* xRename */
219744 0, /* xSavepoint */
219745 0, /* xRelease */
219746 0, /* xRollbackTo */
219747 0 /* xShadowName */
219748 };
219749 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
219750}
219751#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
219752SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
219753#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
219754
219755/************** End of dbpage.c **********************************************/
219756/************** Begin file sqlite3session.c **********************************/
219757
219758#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
219759/* #include "sqlite3session.h" */
219760/* #include <assert.h> */
219761/* #include <string.h> */
219762
219763#ifndef SQLITE_AMALGAMATION
219764/* # include "sqliteInt.h" */
219765/* # include "vdbeInt.h" */
219766#endif
219767
219768typedef struct SessionTable SessionTable;
219769typedef struct SessionChange SessionChange;
219770typedef struct SessionBuffer SessionBuffer;
219771typedef struct SessionInput SessionInput;
219772
219773/*
219774** Minimum chunk size used by streaming versions of functions.
219775*/
219776#ifndef SESSIONS_STRM_CHUNK_SIZE
219777# ifdef SQLITE_TEST
219778# define SESSIONS_STRM_CHUNK_SIZE 64
219779# else
219780# define SESSIONS_STRM_CHUNK_SIZE 1024
219781# endif
219782#endif
219783
219784#define SESSIONS_ROWID "_rowid_"
219785
219786static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
219787
219788typedef struct SessionHook SessionHook;
219789struct SessionHook {
219790 void *pCtx;
219791 int (*xOld)(void*,int,sqlite3_value**);
219792 int (*xNew)(void*,int,sqlite3_value**);
219793 int (*xCount)(void*);
219794 int (*xDepth)(void*);
219795};
219796
219797/*
219798** Session handle structure.
219799*/
219800struct sqlite3_session {
219801 sqlite3 *db; /* Database handle session is attached to */
219802 char *zDb; /* Name of database session is attached to */
219803 int bEnableSize; /* True if changeset_size() enabled */
219804 int bEnable; /* True if currently recording */
219805 int bIndirect; /* True if all changes are indirect */
219806 int bAutoAttach; /* True to auto-attach tables */
219807 int bImplicitPK; /* True to handle tables with implicit PK */
219808 int rc; /* Non-zero if an error has occurred */
219809 void *pFilterCtx; /* First argument to pass to xTableFilter */
219810 int (*xTableFilter)(void *pCtx, const char *zTab);
219811 i64 nMalloc; /* Number of bytes of data allocated */
219812 i64 nMaxChangesetSize;
219813 sqlite3_value *pZeroBlob; /* Value containing X'' */
219814 sqlite3_session *pNext; /* Next session object on same db. */
219815 SessionTable *pTable; /* List of attached tables */
219816 SessionHook hook; /* APIs to grab new and old data with */
219817};
219818
219819/*
219820** Instances of this structure are used to build strings or binary records.
219821*/
219822struct SessionBuffer {
219823 u8 *aBuf; /* Pointer to changeset buffer */
219824 int nBuf; /* Size of buffer aBuf */
219825 int nAlloc; /* Size of allocation containing aBuf */
219826};
219827
219828/*
219829** An object of this type is used internally as an abstraction for
219830** input data. Input data may be supplied either as a single large buffer
219831** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
219832** sqlite3changeset_start_strm()).
219833*/
219834struct SessionInput {
219835 int bNoDiscard; /* If true, do not discard in InputBuffer() */
219836 int iCurrent; /* Offset in aData[] of current change */
219837 int iNext; /* Offset in aData[] of next change */
219838 u8 *aData; /* Pointer to buffer containing changeset */
219839 int nData; /* Number of bytes in aData */
219840
219841 SessionBuffer buf; /* Current read buffer */
219842 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
219843 void *pIn; /* First argument to xInput */
219844 int bEof; /* Set to true after xInput finished */
219845};
219846
219847/*
219848** Structure for changeset iterators.
219849*/
219850struct sqlite3_changeset_iter {
219851 SessionInput in; /* Input buffer or stream */
219852 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
219853 int bPatchset; /* True if this is a patchset */
219854 int bInvert; /* True to invert changeset */
219855 int bSkipEmpty; /* Skip noop UPDATE changes */
219856 int rc; /* Iterator error code */
219857 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
219858 char *zTab; /* Current table */
219859 int nCol; /* Number of columns in zTab */
219860 int op; /* Current operation */
219861 int bIndirect; /* True if current change was indirect */
219862 u8 *abPK; /* Primary key array */
219863 sqlite3_value **apValue; /* old.* and new.* values */
219864};
219865
219866/*
219867** Each session object maintains a set of the following structures, one
219868** for each table the session object is monitoring. The structures are
219869** stored in a linked list starting at sqlite3_session.pTable.
219870**
219871** The keys of the SessionTable.aChange[] hash table are all rows that have
219872** been modified in any way since the session object was attached to the
219873** table.
219874**
219875** The data associated with each hash-table entry is a structure containing
219876** a subset of the initial values that the modified row contained at the
219877** start of the session. Or no initial values if the row was inserted.
219878*/
219879struct SessionTable {
219880 SessionTable *pNext;
219881 char *zName; /* Local name of table */
219882 int nCol; /* Number of columns in table zName */
219883 int bStat1; /* True if this is sqlite_stat1 */
219884 int bRowid; /* True if this table uses rowid for PK */
219885 const char **azCol; /* Column names */
219886 u8 *abPK; /* Array of primary key flags */
219887 int nEntry; /* Total number of entries in hash table */
219888 int nChange; /* Size of apChange[] array */
219889 SessionChange **apChange; /* Hash table buckets */
219890};
219891
219892/*
219893** RECORD FORMAT:
219894**
219895** The following record format is similar to (but not compatible with) that
219896** used in SQLite database files. This format is used as part of the
219897** change-set binary format, and so must be architecture independent.
219898**
219899** Unlike the SQLite database record format, each field is self-contained -
219900** there is no separation of header and data. Each field begins with a
219901** single byte describing its type, as follows:
219902**
219903** 0x00: Undefined value.
219904** 0x01: Integer value.
219905** 0x02: Real value.
219906** 0x03: Text value.
219907** 0x04: Blob value.
219908** 0x05: SQL NULL value.
219909**
219910** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
219911** and so on in sqlite3.h. For undefined and NULL values, the field consists
219912** only of the single type byte. For other types of values, the type byte
219913** is followed by:
219914**
219915** Text values:
219916** A varint containing the number of bytes in the value (encoded using
219917** UTF-8). Followed by a buffer containing the UTF-8 representation
219918** of the text value. There is no nul terminator.
219919**
219920** Blob values:
219921** A varint containing the number of bytes in the value, followed by
219922** a buffer containing the value itself.
219923**
219924** Integer values:
219925** An 8-byte big-endian integer value.
219926**
219927** Real values:
219928** An 8-byte big-endian IEEE 754-2008 real value.
219929**
219930** Varint values are encoded in the same way as varints in the SQLite
219931** record format.
219932**
219933** CHANGESET FORMAT:
219934**
219935** A changeset is a collection of DELETE, UPDATE and INSERT operations on
219936** one or more tables. Operations on a single table are grouped together,
219937** but may occur in any order (i.e. deletes, updates and inserts are all
219938** mixed together).
219939**
219940** Each group of changes begins with a table header:
219941**
219942** 1 byte: Constant 0x54 (capital 'T')
219943** Varint: Number of columns in the table.
219944** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
219945** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
219946**
219947** Followed by one or more changes to the table.
219948**
219949** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
219950** 1 byte: The "indirect-change" flag.
219951** old.* record: (delete and update only)
219952** new.* record: (insert and update only)
219953**
219954** The "old.*" and "new.*" records, if present, are N field records in the
219955** format described above under "RECORD FORMAT", where N is the number of
219956** columns in the table. The i'th field of each record is associated with
219957** the i'th column of the table, counting from left to right in the order
219958** in which columns were declared in the CREATE TABLE statement.
219959**
219960** The new.* record that is part of each INSERT change contains the values
219961** that make up the new row. Similarly, the old.* record that is part of each
219962** DELETE change contains the values that made up the row that was deleted
219963** from the database. In the changeset format, the records that are part
219964** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
219965** fields.
219966**
219967** Within the old.* record associated with an UPDATE change, all fields
219968** associated with table columns that are not PRIMARY KEY columns and are
219969** not modified by the UPDATE change are set to "undefined". Other fields
219970** are set to the values that made up the row before the UPDATE that the
219971** change records took place. Within the new.* record, fields associated
219972** with table columns modified by the UPDATE change contain the new
219973** values. Fields associated with table columns that are not modified
219974** are set to "undefined".
219975**
219976** PATCHSET FORMAT:
219977**
219978** A patchset is also a collection of changes. It is similar to a changeset,
219979** but leaves undefined those fields that are not useful if no conflict
219980** resolution is required when applying the changeset.
219981**
219982** Each group of changes begins with a table header:
219983**
219984** 1 byte: Constant 0x50 (capital 'P')
219985** Varint: Number of columns in the table.
219986** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
219987** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
219988**
219989** Followed by one or more changes to the table.
219990**
219991** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
219992** 1 byte: The "indirect-change" flag.
219993** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
219994** full record for INSERT).
219995**
219996** As in the changeset format, each field of the single record that is part
219997** of a patchset change is associated with the correspondingly positioned
219998** table column, counting from left to right within the CREATE TABLE
219999** statement.
220000**
220001** For a DELETE change, all fields within the record except those associated
220002** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
220003** values identifying the row to delete.
220004**
220005** For an UPDATE change, all fields except those associated with PRIMARY KEY
220006** columns and columns that are modified by the UPDATE are set to "undefined".
220007** PRIMARY KEY fields contain the values identifying the table row to update,
220008** and fields associated with modified columns contain the new column values.
220009**
220010** The records associated with INSERT changes are in the same format as for
220011** changesets. It is not possible for a record associated with an INSERT
220012** change to contain a field set to "undefined".
220013**
220014** REBASE BLOB FORMAT:
220015**
220016** A rebase blob may be output by sqlite3changeset_apply_v2() and its
220017** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
220018** existing changesets. A rebase blob contains one entry for each conflict
220019** resolved using either the OMIT or REPLACE strategies within the apply_v2()
220020** call.
220021**
220022** The format used for a rebase blob is very similar to that used for
220023** changesets. All entries related to a single table are grouped together.
220024**
220025** Each group of entries begins with a table header in changeset format:
220026**
220027** 1 byte: Constant 0x54 (capital 'T')
220028** Varint: Number of columns in the table.
220029** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
220030** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
220031**
220032** Followed by one or more entries associated with the table.
220033**
220034** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
220035** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
220036** record: (in the record format defined above).
220037**
220038** In a rebase blob, the first field is set to SQLITE_INSERT if the change
220039** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
220040** it was a DELETE. The second field is set to 0x01 if the conflict
220041** resolution strategy was REPLACE, or 0x00 if it was OMIT.
220042**
220043** If the change that caused the conflict was a DELETE, then the single
220044** record is a copy of the old.* record from the original changeset. If it
220045** was an INSERT, then the single record is a copy of the new.* record. If
220046** the conflicting change was an UPDATE, then the single record is a copy
220047** of the new.* record with the PK fields filled in based on the original
220048** old.* record.
220049*/
220050
220051/*
220052** For each row modified during a session, there exists a single instance of
220053** this structure stored in a SessionTable.aChange[] hash table.
220054*/
220055struct SessionChange {
220056 u8 op; /* One of UPDATE, DELETE, INSERT */
220057 u8 bIndirect; /* True if this change is "indirect" */
220058 int nMaxSize; /* Max size of eventual changeset record */
220059 int nRecord; /* Number of bytes in buffer aRecord[] */
220060 u8 *aRecord; /* Buffer containing old.* record */
220061 SessionChange *pNext; /* For hash-table collisions */
220062};
220063
220064/*
220065** Write a varint with value iVal into the buffer at aBuf. Return the
220066** number of bytes written.
220067*/
220068static int sessionVarintPut(u8 *aBuf, int iVal){
220069 return putVarint32(aBuf, iVal);
220070}
220071
220072/*
220073** Return the number of bytes required to store value iVal as a varint.
220074*/
220075static int sessionVarintLen(int iVal){
220076 return sqlite3VarintLen(iVal);
220077}
220078
220079/*
220080** Read a varint value from aBuf[] into *piVal. Return the number of
220081** bytes read.
220082*/
220083static int sessionVarintGet(u8 *aBuf, int *piVal){
220084 return getVarint32(aBuf, *piVal);
220085}
220086
220087/* Load an unaligned and unsigned 32-bit integer */
220088#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
220089
220090/*
220091** Read a 64-bit big-endian integer value from buffer aRec[]. Return
220092** the value read.
220093*/
220094static sqlite3_int64 sessionGetI64(u8 *aRec){
220095 u64 x = SESSION_UINT32(aRec);
220096 u32 y = SESSION_UINT32(aRec+4);
220097 x = (x<<32) + y;
220098 return (sqlite3_int64)x;
220099}
220100
220101/*
220102** Write a 64-bit big-endian integer value to the buffer aBuf[].
220103*/
220104static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
220105 aBuf[0] = (i>>56) & 0xFF;
220106 aBuf[1] = (i>>48) & 0xFF;
220107 aBuf[2] = (i>>40) & 0xFF;
220108 aBuf[3] = (i>>32) & 0xFF;
220109 aBuf[4] = (i>>24) & 0xFF;
220110 aBuf[5] = (i>>16) & 0xFF;
220111 aBuf[6] = (i>> 8) & 0xFF;
220112 aBuf[7] = (i>> 0) & 0xFF;
220113}
220114
220115/*
220116** This function is used to serialize the contents of value pValue (see
220117** comment titled "RECORD FORMAT" above).
220118**
220119** If it is non-NULL, the serialized form of the value is written to
220120** buffer aBuf. *pnWrite is set to the number of bytes written before
220121** returning. Or, if aBuf is NULL, the only thing this function does is
220122** set *pnWrite.
220123**
220124** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
220125** within a call to sqlite3_value_text() (may fail if the db is utf-16))
220126** SQLITE_NOMEM is returned.
220127*/
220128static int sessionSerializeValue(
220129 u8 *aBuf, /* If non-NULL, write serialized value here */
220130 sqlite3_value *pValue, /* Value to serialize */
220131 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
220132){
220133 int nByte; /* Size of serialized value in bytes */
220134
220135 if( pValue ){
220136 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
220137
220138 eType = sqlite3_value_type(pValue);
220139 if( aBuf ) aBuf[0] = eType;
220140
220141 switch( eType ){
220142 case SQLITE_NULL:
220143 nByte = 1;
220144 break;
220145
220146 case SQLITE_INTEGER:
220147 case SQLITE_FLOAT:
220148 if( aBuf ){
220149 /* TODO: SQLite does something special to deal with mixed-endian
220150 ** floating point values (e.g. ARM7). This code probably should
220151 ** too. */
220152 u64 i;
220153 if( eType==SQLITE_INTEGER ){
220154 i = (u64)sqlite3_value_int64(pValue);
220155 }else{
220156 double r;
220157 assert( sizeof(double)==8 && sizeof(u64)==8 );
220158 r = sqlite3_value_double(pValue);
220159 memcpy(&i, &r, 8);
220160 }
220161 sessionPutI64(&aBuf[1], i);
220162 }
220163 nByte = 9;
220164 break;
220165
220166 default: {
220167 u8 *z;
220168 int n;
220169 int nVarint;
220170
220171 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
220172 if( eType==SQLITE_TEXT ){
220173 z = (u8 *)sqlite3_value_text(pValue);
220174 }else{
220175 z = (u8 *)sqlite3_value_blob(pValue);
220176 }
220177 n = sqlite3_value_bytes(pValue);
220178 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
220179 nVarint = sessionVarintLen(n);
220180
220181 if( aBuf ){
220182 sessionVarintPut(&aBuf[1], n);
220183 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
220184 }
220185
220186 nByte = 1 + nVarint + n;
220187 break;
220188 }
220189 }
220190 }else{
220191 nByte = 1;
220192 if( aBuf ) aBuf[0] = '\0';
220193 }
220194
220195 if( pnWrite ) *pnWrite += nByte;
220196 return SQLITE_OK;
220197}
220198
220199/*
220200** Allocate and return a pointer to a buffer nByte bytes in size. If
220201** pSession is not NULL, increase the sqlite3_session.nMalloc variable
220202** by the number of bytes allocated.
220203*/
220204static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
220205 void *pRet = sqlite3_malloc64(nByte);
220206 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
220207 return pRet;
220208}
220209
220210/*
220211** Free buffer pFree, which must have been allocated by an earlier
220212** call to sessionMalloc64(). If pSession is not NULL, decrease the
220213** sqlite3_session.nMalloc counter by the number of bytes freed.
220214*/
220215static void sessionFree(sqlite3_session *pSession, void *pFree){
220216 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
220217 sqlite3_free(pFree);
220218}
220219
220220/*
220221** This macro is used to calculate hash key values for data structures. In
220222** order to use this macro, the entire data structure must be represented
220223** as a series of unsigned integers. In order to calculate a hash-key value
220224** for a data structure represented as three such integers, the macro may
220225** then be used as follows:
220226**
220227** int hash_key_value;
220228** hash_key_value = HASH_APPEND(0, <value 1>);
220229** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
220230** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
220231**
220232** In practice, the data structures this macro is used for are the primary
220233** key values of modified rows.
220234*/
220235#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
220236
220237/*
220238** Append the hash of the 64-bit integer passed as the second argument to the
220239** hash-key value passed as the first. Return the new hash-key value.
220240*/
220241static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
220242 h = HASH_APPEND(h, i & 0xFFFFFFFF);
220243 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
220244}
220245
220246/*
220247** Append the hash of the blob passed via the second and third arguments to
220248** the hash-key value passed as the first. Return the new hash-key value.
220249*/
220250static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
220251 int i;
220252 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
220253 return h;
220254}
220255
220256/*
220257** Append the hash of the data type passed as the second argument to the
220258** hash-key value passed as the first. Return the new hash-key value.
220259*/
220260static unsigned int sessionHashAppendType(unsigned int h, int eType){
220261 return HASH_APPEND(h, eType);
220262}
220263
220264/*
220265** This function may only be called from within a pre-update callback.
220266** It calculates a hash based on the primary key values of the old.* or
220267** new.* row currently available and, assuming no error occurs, writes it to
220268** *piHash before returning. If the primary key contains one or more NULL
220269** values, *pbNullPK is set to true before returning.
220270**
220271** If an error occurs, an SQLite error code is returned and the final values
220272** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
220273** and the output variables are set as described above.
220274*/
220275static int sessionPreupdateHash(
220276 sqlite3_session *pSession, /* Session object that owns pTab */
220277 i64 iRowid,
220278 SessionTable *pTab, /* Session table handle */
220279 int bNew, /* True to hash the new.* PK */
220280 int *piHash, /* OUT: Hash value */
220281 int *pbNullPK /* OUT: True if there are NULL values in PK */
220282){
220283 unsigned int h = 0; /* Hash value to return */
220284 int i; /* Used to iterate through columns */
220285
220286 if( pTab->bRowid ){
220287 assert( pTab->nCol-1==pSession->hook.xCount(pSession->hook.pCtx) );
220288 h = sessionHashAppendI64(h, iRowid);
220289 }else{
220290 assert( *pbNullPK==0 );
220291 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
220292 for(i=0; i<pTab->nCol; i++){
220293 if( pTab->abPK[i] ){
220294 int rc;
220295 int eType;
220296 sqlite3_value *pVal;
220297
220298 if( bNew ){
220299 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
220300 }else{
220301 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
220302 }
220303 if( rc!=SQLITE_OK ) return rc;
220304
220305 eType = sqlite3_value_type(pVal);
220306 h = sessionHashAppendType(h, eType);
220307 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
220308 i64 iVal;
220309 if( eType==SQLITE_INTEGER ){
220310 iVal = sqlite3_value_int64(pVal);
220311 }else{
220312 double rVal = sqlite3_value_double(pVal);
220313 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
220314 memcpy(&iVal, &rVal, 8);
220315 }
220316 h = sessionHashAppendI64(h, iVal);
220317 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
220318 const u8 *z;
220319 int n;
220320 if( eType==SQLITE_TEXT ){
220321 z = (const u8 *)sqlite3_value_text(pVal);
220322 }else{
220323 z = (const u8 *)sqlite3_value_blob(pVal);
220324 }
220325 n = sqlite3_value_bytes(pVal);
220326 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
220327 h = sessionHashAppendBlob(h, n, z);
220328 }else{
220329 assert( eType==SQLITE_NULL );
220330 assert( pTab->bStat1==0 || i!=1 );
220331 *pbNullPK = 1;
220332 }
220333 }
220334 }
220335 }
220336
220337 *piHash = (h % pTab->nChange);
220338 return SQLITE_OK;
220339}
220340
220341/*
220342** The buffer that the argument points to contains a serialized SQL value.
220343** Return the number of bytes of space occupied by the value (including
220344** the type byte).
220345*/
220346static int sessionSerialLen(u8 *a){
220347 int e = *a;
220348 int n;
220349 if( e==0 || e==0xFF ) return 1;
220350 if( e==SQLITE_NULL ) return 1;
220351 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
220352 return sessionVarintGet(&a[1], &n) + 1 + n;
220353}
220354
220355/*
220356** Based on the primary key values stored in change aRecord, calculate a
220357** hash key. Assume the has table has nBucket buckets. The hash keys
220358** calculated by this function are compatible with those calculated by
220359** sessionPreupdateHash().
220360**
220361** The bPkOnly argument is non-zero if the record at aRecord[] is from
220362** a patchset DELETE. In this case the non-PK fields are omitted entirely.
220363*/
220364static unsigned int sessionChangeHash(
220365 SessionTable *pTab, /* Table handle */
220366 int bPkOnly, /* Record consists of PK fields only */
220367 u8 *aRecord, /* Change record */
220368 int nBucket /* Assume this many buckets in hash table */
220369){
220370 unsigned int h = 0; /* Value to return */
220371 int i; /* Used to iterate through columns */
220372 u8 *a = aRecord; /* Used to iterate through change record */
220373
220374 for(i=0; i<pTab->nCol; i++){
220375 int eType = *a;
220376 int isPK = pTab->abPK[i];
220377 if( bPkOnly && isPK==0 ) continue;
220378
220379 /* It is not possible for eType to be SQLITE_NULL here. The session
220380 ** module does not record changes for rows with NULL values stored in
220381 ** primary key columns. */
220382 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
220383 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
220384 || eType==SQLITE_NULL || eType==0
220385 );
220386 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
220387
220388 if( isPK ){
220389 a++;
220390 h = sessionHashAppendType(h, eType);
220391 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
220392 h = sessionHashAppendI64(h, sessionGetI64(a));
220393 a += 8;
220394 }else{
220395 int n;
220396 a += sessionVarintGet(a, &n);
220397 h = sessionHashAppendBlob(h, n, a);
220398 a += n;
220399 }
220400 }else{
220401 a += sessionSerialLen(a);
220402 }
220403 }
220404 return (h % nBucket);
220405}
220406
220407/*
220408** Arguments aLeft and aRight are pointers to change records for table pTab.
220409** This function returns true if the two records apply to the same row (i.e.
220410** have the same values stored in the primary key columns), or false
220411** otherwise.
220412*/
220413static int sessionChangeEqual(
220414 SessionTable *pTab, /* Table used for PK definition */
220415 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
220416 u8 *aLeft, /* Change record */
220417 int bRightPkOnly, /* True if aRight[] contains PK fields only */
220418 u8 *aRight /* Change record */
220419){
220420 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
220421 u8 *a2 = aRight; /* Cursor to iterate through aRight */
220422 int iCol; /* Used to iterate through table columns */
220423
220424 for(iCol=0; iCol<pTab->nCol; iCol++){
220425 if( pTab->abPK[iCol] ){
220426 int n1 = sessionSerialLen(a1);
220427 int n2 = sessionSerialLen(a2);
220428
220429 if( n1!=n2 || memcmp(a1, a2, n1) ){
220430 return 0;
220431 }
220432 a1 += n1;
220433 a2 += n2;
220434 }else{
220435 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
220436 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
220437 }
220438 }
220439
220440 return 1;
220441}
220442
220443/*
220444** Arguments aLeft and aRight both point to buffers containing change
220445** records with nCol columns. This function "merges" the two records into
220446** a single records which is written to the buffer at *paOut. *paOut is
220447** then set to point to one byte after the last byte written before
220448** returning.
220449**
220450** The merging of records is done as follows: For each column, if the
220451** aRight record contains a value for the column, copy the value from
220452** their. Otherwise, if aLeft contains a value, copy it. If neither
220453** record contains a value for a given column, then neither does the
220454** output record.
220455*/
220456static void sessionMergeRecord(
220457 u8 **paOut,
220458 int nCol,
220459 u8 *aLeft,
220460 u8 *aRight
220461){
220462 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
220463 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
220464 u8 *aOut = *paOut; /* Output cursor */
220465 int iCol; /* Used to iterate from 0 to nCol */
220466
220467 for(iCol=0; iCol<nCol; iCol++){
220468 int n1 = sessionSerialLen(a1);
220469 int n2 = sessionSerialLen(a2);
220470 if( *a2 ){
220471 memcpy(aOut, a2, n2);
220472 aOut += n2;
220473 }else{
220474 memcpy(aOut, a1, n1);
220475 aOut += n1;
220476 }
220477 a1 += n1;
220478 a2 += n2;
220479 }
220480
220481 *paOut = aOut;
220482}
220483
220484/*
220485** This is a helper function used by sessionMergeUpdate().
220486**
220487** When this function is called, both *paOne and *paTwo point to a value
220488** within a change record. Before it returns, both have been advanced so
220489** as to point to the next value in the record.
220490**
220491** If, when this function is called, *paTwo points to a valid value (i.e.
220492** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
220493** pointer is returned and *pnVal is set to the number of bytes in the
220494** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
220495** set to the number of bytes in the value at *paOne. If *paOne points
220496** to the "no value" placeholder, *pnVal is set to 1. In other words:
220497**
220498** if( *paTwo is valid ) return *paTwo;
220499** return *paOne;
220500**
220501*/
220502static u8 *sessionMergeValue(
220503 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
220504 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
220505 int *pnVal /* OUT: Bytes in returned value */
220506){
220507 u8 *a1 = *paOne;
220508 u8 *a2 = *paTwo;
220509 u8 *pRet = 0;
220510 int n1;
220511
220512 assert( a1 );
220513 if( a2 ){
220514 int n2 = sessionSerialLen(a2);
220515 if( *a2 ){
220516 *pnVal = n2;
220517 pRet = a2;
220518 }
220519 *paTwo = &a2[n2];
220520 }
220521
220522 n1 = sessionSerialLen(a1);
220523 if( pRet==0 ){
220524 *pnVal = n1;
220525 pRet = a1;
220526 }
220527 *paOne = &a1[n1];
220528
220529 return pRet;
220530}
220531
220532/*
220533** This function is used by changeset_concat() to merge two UPDATE changes
220534** on the same row.
220535*/
220536static int sessionMergeUpdate(
220537 u8 **paOut, /* IN/OUT: Pointer to output buffer */
220538 SessionTable *pTab, /* Table change pertains to */
220539 int bPatchset, /* True if records are patchset records */
220540 u8 *aOldRecord1, /* old.* record for first change */
220541 u8 *aOldRecord2, /* old.* record for second change */
220542 u8 *aNewRecord1, /* new.* record for first change */
220543 u8 *aNewRecord2 /* new.* record for second change */
220544){
220545 u8 *aOld1 = aOldRecord1;
220546 u8 *aOld2 = aOldRecord2;
220547 u8 *aNew1 = aNewRecord1;
220548 u8 *aNew2 = aNewRecord2;
220549
220550 u8 *aOut = *paOut;
220551 int i;
220552
220553 if( bPatchset==0 ){
220554 int bRequired = 0;
220555
220556 assert( aOldRecord1 && aNewRecord1 );
220557
220558 /* Write the old.* vector first. */
220559 for(i=0; i<pTab->nCol; i++){
220560 int nOld;
220561 u8 *aOld;
220562 int nNew;
220563 u8 *aNew;
220564
220565 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
220566 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
220567 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
220568 if( pTab->abPK[i]==0 ) bRequired = 1;
220569 memcpy(aOut, aOld, nOld);
220570 aOut += nOld;
220571 }else{
220572 *(aOut++) = '\0';
220573 }
220574 }
220575
220576 if( !bRequired ) return 0;
220577 }
220578
220579 /* Write the new.* vector */
220580 aOld1 = aOldRecord1;
220581 aOld2 = aOldRecord2;
220582 aNew1 = aNewRecord1;
220583 aNew2 = aNewRecord2;
220584 for(i=0; i<pTab->nCol; i++){
220585 int nOld;
220586 u8 *aOld;
220587 int nNew;
220588 u8 *aNew;
220589
220590 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
220591 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
220592 if( bPatchset==0
220593 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
220594 ){
220595 *(aOut++) = '\0';
220596 }else{
220597 memcpy(aOut, aNew, nNew);
220598 aOut += nNew;
220599 }
220600 }
220601
220602 *paOut = aOut;
220603 return 1;
220604}
220605
220606/*
220607** This function is only called from within a pre-update-hook callback.
220608** It determines if the current pre-update-hook change affects the same row
220609** as the change stored in argument pChange. If so, it returns true. Otherwise
220610** if the pre-update-hook does not affect the same row as pChange, it returns
220611** false.
220612*/
220613static int sessionPreupdateEqual(
220614 sqlite3_session *pSession, /* Session object that owns SessionTable */
220615 i64 iRowid, /* Rowid value if pTab->bRowid */
220616 SessionTable *pTab, /* Table associated with change */
220617 SessionChange *pChange, /* Change to compare to */
220618 int op /* Current pre-update operation */
220619){
220620 int iCol; /* Used to iterate through columns */
220621 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
220622
220623 if( pTab->bRowid ){
220624 if( a[0]!=SQLITE_INTEGER ) return 0;
220625 return sessionGetI64(&a[1])==iRowid;
220626 }
220627
220628 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
220629 for(iCol=0; iCol<pTab->nCol; iCol++){
220630 if( !pTab->abPK[iCol] ){
220631 a += sessionSerialLen(a);
220632 }else{
220633 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
220634 int rc; /* Error code from preupdate_new/old */
220635 int eType = *a++; /* Type of value from change record */
220636
220637 /* The following calls to preupdate_new() and preupdate_old() can not
220638 ** fail. This is because they cache their return values, and by the
220639 ** time control flows to here they have already been called once from
220640 ** within sessionPreupdateHash(). The first two asserts below verify
220641 ** this (that the method has already been called). */
220642 if( op==SQLITE_INSERT ){
220643 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
220644 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
220645 }else{
220646 /* assert( db->pPreUpdate->pUnpacked ); */
220647 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
220648 }
220649 assert( rc==SQLITE_OK );
220650 (void)rc; /* Suppress warning about unused variable */
220651 if( sqlite3_value_type(pVal)!=eType ) return 0;
220652
220653 /* A SessionChange object never has a NULL value in a PK column */
220654 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
220655 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
220656 );
220657
220658 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
220659 i64 iVal = sessionGetI64(a);
220660 a += 8;
220661 if( eType==SQLITE_INTEGER ){
220662 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
220663 }else{
220664 double rVal;
220665 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
220666 memcpy(&rVal, &iVal, 8);
220667 if( sqlite3_value_double(pVal)!=rVal ) return 0;
220668 }
220669 }else{
220670 int n;
220671 const u8 *z;
220672 a += sessionVarintGet(a, &n);
220673 if( sqlite3_value_bytes(pVal)!=n ) return 0;
220674 if( eType==SQLITE_TEXT ){
220675 z = sqlite3_value_text(pVal);
220676 }else{
220677 z = sqlite3_value_blob(pVal);
220678 }
220679 if( n>0 && memcmp(a, z, n) ) return 0;
220680 a += n;
220681 }
220682 }
220683 }
220684
220685 return 1;
220686}
220687
220688/*
220689** If required, grow the hash table used to store changes on table pTab
220690** (part of the session pSession). If a fatal OOM error occurs, set the
220691** session object to failed and return SQLITE_ERROR. Otherwise, return
220692** SQLITE_OK.
220693**
220694** It is possible that a non-fatal OOM error occurs in this function. In
220695** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
220696** Growing the hash table in this case is a performance optimization only,
220697** it is not required for correct operation.
220698*/
220699static int sessionGrowHash(
220700 sqlite3_session *pSession, /* For memory accounting. May be NULL */
220701 int bPatchset,
220702 SessionTable *pTab
220703){
220704 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
220705 int i;
220706 SessionChange **apNew;
220707 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
220708
220709 apNew = (SessionChange**)sessionMalloc64(
220710 pSession, sizeof(SessionChange*) * nNew
220711 );
220712 if( apNew==0 ){
220713 if( pTab->nChange==0 ){
220714 return SQLITE_ERROR;
220715 }
220716 return SQLITE_OK;
220717 }
220718 memset(apNew, 0, sizeof(SessionChange *) * nNew);
220719
220720 for(i=0; i<pTab->nChange; i++){
220721 SessionChange *p;
220722 SessionChange *pNext;
220723 for(p=pTab->apChange[i]; p; p=pNext){
220724 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
220725 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
220726 pNext = p->pNext;
220727 p->pNext = apNew[iHash];
220728 apNew[iHash] = p;
220729 }
220730 }
220731
220732 sessionFree(pSession, pTab->apChange);
220733 pTab->nChange = nNew;
220734 pTab->apChange = apNew;
220735 }
220736
220737 return SQLITE_OK;
220738}
220739
220740/*
220741** This function queries the database for the names of the columns of table
220742** zThis, in schema zDb.
220743**
220744** Otherwise, if they are not NULL, variable *pnCol is set to the number
220745** of columns in the database table and variable *pzTab is set to point to a
220746** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
220747** point to an array of pointers to column names. And *pabPK (again, if not
220748** NULL) is set to point to an array of booleans - true if the corresponding
220749** column is part of the primary key.
220750**
220751** For example, if the table is declared as:
220752**
220753** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
220754**
220755** Then the four output variables are populated as follows:
220756**
220757** *pnCol = 4
220758** *pzTab = "tbl1"
220759** *pazCol = {"w", "x", "y", "z"}
220760** *pabPK = {1, 0, 0, 1}
220761**
220762** All returned buffers are part of the same single allocation, which must
220763** be freed using sqlite3_free() by the caller
220764*/
220765static int sessionTableInfo(
220766 sqlite3_session *pSession, /* For memory accounting. May be NULL */
220767 sqlite3 *db, /* Database connection */
220768 const char *zDb, /* Name of attached database (e.g. "main") */
220769 const char *zThis, /* Table name */
220770 int *pnCol, /* OUT: number of columns */
220771 const char **pzTab, /* OUT: Copy of zThis */
220772 const char ***pazCol, /* OUT: Array of column names for table */
220773 u8 **pabPK, /* OUT: Array of booleans - true for PK col */
220774 int *pbRowid /* OUT: True if only PK is a rowid */
220775){
220776 char *zPragma;
220777 sqlite3_stmt *pStmt;
220778 int rc;
220779 sqlite3_int64 nByte;
220780 int nDbCol = 0;
220781 int nThis;
220782 int i;
220783 u8 *pAlloc = 0;
220784 char **azCol = 0;
220785 u8 *abPK = 0;
220786 int bRowid = 0; /* Set to true to use rowid as PK */
220787
220788 assert( pazCol && pabPK );
220789
220790 nThis = sqlite3Strlen30(zThis);
220791 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
220792 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
220793 if( rc==SQLITE_OK ){
220794 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
220795 zPragma = sqlite3_mprintf(
220796 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
220797 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
220798 "SELECT 2, 'stat', '', 0, '', 0"
220799 );
220800 }else if( rc==SQLITE_ERROR ){
220801 zPragma = sqlite3_mprintf("");
220802 }else{
220803 *pazCol = 0;
220804 *pabPK = 0;
220805 *pnCol = 0;
220806 if( pzTab ) *pzTab = 0;
220807 return rc;
220808 }
220809 }else{
220810 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
220811 }
220812 if( !zPragma ){
220813 *pazCol = 0;
220814 *pabPK = 0;
220815 *pnCol = 0;
220816 if( pzTab ) *pzTab = 0;
220817 return SQLITE_NOMEM;
220818 }
220819
220820 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
220821 sqlite3_free(zPragma);
220822 if( rc!=SQLITE_OK ){
220823 *pazCol = 0;
220824 *pabPK = 0;
220825 *pnCol = 0;
220826 if( pzTab ) *pzTab = 0;
220827 return rc;
220828 }
220829
220830 nByte = nThis + 1;
220831 bRowid = (pbRowid!=0);
220832 while( SQLITE_ROW==sqlite3_step(pStmt) ){
220833 nByte += sqlite3_column_bytes(pStmt, 1);
220834 nDbCol++;
220835 if( sqlite3_column_int(pStmt, 5) ) bRowid = 0;
220836 }
220837 if( nDbCol==0 ) bRowid = 0;
220838 nDbCol += bRowid;
220839 nByte += strlen(SESSIONS_ROWID);
220840 rc = sqlite3_reset(pStmt);
220841
220842 if( rc==SQLITE_OK ){
220843 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
220844 pAlloc = sessionMalloc64(pSession, nByte);
220845 if( pAlloc==0 ){
220846 rc = SQLITE_NOMEM;
220847 }
220848 }
220849 if( rc==SQLITE_OK ){
220850 azCol = (char **)pAlloc;
220851 pAlloc = (u8 *)&azCol[nDbCol];
220852 abPK = (u8 *)pAlloc;
220853 pAlloc = &abPK[nDbCol];
220854 if( pzTab ){
220855 memcpy(pAlloc, zThis, nThis+1);
220856 *pzTab = (char *)pAlloc;
220857 pAlloc += nThis+1;
220858 }
220859
220860 i = 0;
220861 if( bRowid ){
220862 size_t nName = strlen(SESSIONS_ROWID);
220863 memcpy(pAlloc, SESSIONS_ROWID, nName+1);
220864 azCol[i] = (char*)pAlloc;
220865 pAlloc += nName+1;
220866 abPK[i] = 1;
220867 i++;
220868 }
220869 while( SQLITE_ROW==sqlite3_step(pStmt) ){
220870 int nName = sqlite3_column_bytes(pStmt, 1);
220871 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
220872 if( zName==0 ) break;
220873 memcpy(pAlloc, zName, nName+1);
220874 azCol[i] = (char *)pAlloc;
220875 pAlloc += nName+1;
220876 abPK[i] = sqlite3_column_int(pStmt, 5);
220877 i++;
220878 }
220879 rc = sqlite3_reset(pStmt);
220880 }
220881
220882 /* If successful, populate the output variables. Otherwise, zero them and
220883 ** free any allocation made. An error code will be returned in this case.
220884 */
220885 if( rc==SQLITE_OK ){
220886 *pazCol = (const char **)azCol;
220887 *pabPK = abPK;
220888 *pnCol = nDbCol;
220889 }else{
220890 *pazCol = 0;
220891 *pabPK = 0;
220892 *pnCol = 0;
220893 if( pzTab ) *pzTab = 0;
220894 sessionFree(pSession, azCol);
220895 }
220896 if( pbRowid ) *pbRowid = bRowid;
220897 sqlite3_finalize(pStmt);
220898 return rc;
220899}
220900
220901/*
220902** This function is only called from within a pre-update handler for a
220903** write to table pTab, part of session pSession. If this is the first
220904** write to this table, initalize the SessionTable.nCol, azCol[] and
220905** abPK[] arrays accordingly.
220906**
220907** If an error occurs, an error code is stored in sqlite3_session.rc and
220908** non-zero returned. Or, if no error occurs but the table has no primary
220909** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
220910** indicate that updates on this table should be ignored. SessionTable.abPK
220911** is set to NULL in this case.
220912*/
220913static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
220914 if( pTab->nCol==0 ){
220915 u8 *abPK;
220916 assert( pTab->azCol==0 || pTab->abPK==0 );
220917 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
220918 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK,
220919 (pSession->bImplicitPK ? &pTab->bRowid : 0)
220920 );
220921 if( pSession->rc==SQLITE_OK ){
220922 int i;
220923 for(i=0; i<pTab->nCol; i++){
220924 if( abPK[i] ){
220925 pTab->abPK = abPK;
220926 break;
220927 }
220928 }
220929 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
220930 pTab->bStat1 = 1;
220931 }
220932
220933 if( pSession->bEnableSize ){
220934 pSession->nMaxChangesetSize += (
220935 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
220936 );
220937 }
220938 }
220939 }
220940 return (pSession->rc || pTab->abPK==0);
220941}
220942
220943/*
220944** Versions of the four methods in object SessionHook for use with the
220945** sqlite_stat1 table. The purpose of this is to substitute a zero-length
220946** blob each time a NULL value is read from the "idx" column of the
220947** sqlite_stat1 table.
220948*/
220949typedef struct SessionStat1Ctx SessionStat1Ctx;
220950struct SessionStat1Ctx {
220951 SessionHook hook;
220952 sqlite3_session *pSession;
220953};
220954static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
220955 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
220956 sqlite3_value *pVal = 0;
220957 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
220958 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
220959 pVal = p->pSession->pZeroBlob;
220960 }
220961 *ppVal = pVal;
220962 return rc;
220963}
220964static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
220965 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
220966 sqlite3_value *pVal = 0;
220967 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
220968 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
220969 pVal = p->pSession->pZeroBlob;
220970 }
220971 *ppVal = pVal;
220972 return rc;
220973}
220974static int sessionStat1Count(void *pCtx){
220975 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
220976 return p->hook.xCount(p->hook.pCtx);
220977}
220978static int sessionStat1Depth(void *pCtx){
220979 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
220980 return p->hook.xDepth(p->hook.pCtx);
220981}
220982
220983static int sessionUpdateMaxSize(
220984 int op,
220985 sqlite3_session *pSession, /* Session object pTab is attached to */
220986 SessionTable *pTab, /* Table that change applies to */
220987 SessionChange *pC /* Update pC->nMaxSize */
220988){
220989 i64 nNew = 2;
220990 if( pC->op==SQLITE_INSERT ){
220991 if( pTab->bRowid ) nNew += 9;
220992 if( op!=SQLITE_DELETE ){
220993 int ii;
220994 for(ii=0; ii<pTab->nCol; ii++){
220995 sqlite3_value *p = 0;
220996 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
220997 sessionSerializeValue(0, p, &nNew);
220998 }
220999 }
221000 }else if( op==SQLITE_DELETE ){
221001 nNew += pC->nRecord;
221002 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
221003 nNew += pC->nRecord;
221004 }
221005 }else{
221006 int ii;
221007 u8 *pCsr = pC->aRecord;
221008 if( pTab->bRowid ){
221009 nNew += 9 + 1;
221010 pCsr += 9;
221011 }
221012 for(ii=pTab->bRowid; ii<pTab->nCol; ii++){
221013 int bChanged = 1;
221014 int nOld = 0;
221015 int eType;
221016 sqlite3_value *p = 0;
221017 pSession->hook.xNew(pSession->hook.pCtx, ii-pTab->bRowid, &p);
221018 if( p==0 ){
221019 return SQLITE_NOMEM;
221020 }
221021
221022 eType = *pCsr++;
221023 switch( eType ){
221024 case SQLITE_NULL:
221025 bChanged = sqlite3_value_type(p)!=SQLITE_NULL;
221026 break;
221027
221028 case SQLITE_FLOAT:
221029 case SQLITE_INTEGER: {
221030 if( eType==sqlite3_value_type(p) ){
221031 sqlite3_int64 iVal = sessionGetI64(pCsr);
221032 if( eType==SQLITE_INTEGER ){
221033 bChanged = (iVal!=sqlite3_value_int64(p));
221034 }else{
221035 double dVal;
221036 memcpy(&dVal, &iVal, 8);
221037 bChanged = (dVal!=sqlite3_value_double(p));
221038 }
221039 }
221040 nOld = 8;
221041 pCsr += 8;
221042 break;
221043 }
221044
221045 default: {
221046 int nByte;
221047 nOld = sessionVarintGet(pCsr, &nByte);
221048 pCsr += nOld;
221049 nOld += nByte;
221050 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
221051 if( eType==sqlite3_value_type(p)
221052 && nByte==sqlite3_value_bytes(p)
221053 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
221054 ){
221055 bChanged = 0;
221056 }
221057 pCsr += nByte;
221058 break;
221059 }
221060 }
221061
221062 if( bChanged && pTab->abPK[ii] ){
221063 nNew = pC->nRecord + 2;
221064 break;
221065 }
221066
221067 if( bChanged ){
221068 nNew += 1 + nOld;
221069 sessionSerializeValue(0, p, &nNew);
221070 }else if( pTab->abPK[ii] ){
221071 nNew += 2 + nOld;
221072 }else{
221073 nNew += 2;
221074 }
221075 }
221076 }
221077
221078 if( nNew>pC->nMaxSize ){
221079 int nIncr = nNew - pC->nMaxSize;
221080 pC->nMaxSize = nNew;
221081 pSession->nMaxChangesetSize += nIncr;
221082 }
221083 return SQLITE_OK;
221084}
221085
221086/*
221087** This function is only called from with a pre-update-hook reporting a
221088** change on table pTab (attached to session pSession). The type of change
221089** (UPDATE, INSERT, DELETE) is specified by the first argument.
221090**
221091** Unless one is already present or an error occurs, an entry is added
221092** to the changed-rows hash table associated with table pTab.
221093*/
221094static void sessionPreupdateOneChange(
221095 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
221096 i64 iRowid,
221097 sqlite3_session *pSession, /* Session object pTab is attached to */
221098 SessionTable *pTab /* Table that change applies to */
221099){
221100 int iHash;
221101 int bNull = 0;
221102 int rc = SQLITE_OK;
221103 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
221104
221105 if( pSession->rc ) return;
221106
221107 /* Load table details if required */
221108 if( sessionInitTable(pSession, pTab) ) return;
221109
221110 /* Check the number of columns in this xPreUpdate call matches the
221111 ** number of columns in the table. */
221112 if( (pTab->nCol-pTab->bRowid)!=pSession->hook.xCount(pSession->hook.pCtx) ){
221113 pSession->rc = SQLITE_SCHEMA;
221114 return;
221115 }
221116
221117 /* Grow the hash table if required */
221118 if( sessionGrowHash(pSession, 0, pTab) ){
221119 pSession->rc = SQLITE_NOMEM;
221120 return;
221121 }
221122
221123 if( pTab->bStat1 ){
221124 stat1.hook = pSession->hook;
221125 stat1.pSession = pSession;
221126 pSession->hook.pCtx = (void*)&stat1;
221127 pSession->hook.xNew = sessionStat1New;
221128 pSession->hook.xOld = sessionStat1Old;
221129 pSession->hook.xCount = sessionStat1Count;
221130 pSession->hook.xDepth = sessionStat1Depth;
221131 if( pSession->pZeroBlob==0 ){
221132 sqlite3_value *p = sqlite3ValueNew(0);
221133 if( p==0 ){
221134 rc = SQLITE_NOMEM;
221135 goto error_out;
221136 }
221137 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
221138 pSession->pZeroBlob = p;
221139 }
221140 }
221141
221142 /* Calculate the hash-key for this change. If the primary key of the row
221143 ** includes a NULL value, exit early. Such changes are ignored by the
221144 ** session module. */
221145 rc = sessionPreupdateHash(
221146 pSession, iRowid, pTab, op==SQLITE_INSERT, &iHash, &bNull
221147 );
221148 if( rc!=SQLITE_OK ) goto error_out;
221149
221150 if( bNull==0 ){
221151 /* Search the hash table for an existing record for this row. */
221152 SessionChange *pC;
221153 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
221154 if( sessionPreupdateEqual(pSession, iRowid, pTab, pC, op) ) break;
221155 }
221156
221157 if( pC==0 ){
221158 /* Create a new change object containing all the old values (if
221159 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
221160 ** values (if this is an INSERT). */
221161 sqlite3_int64 nByte; /* Number of bytes to allocate */
221162 int i; /* Used to iterate through columns */
221163
221164 assert( rc==SQLITE_OK );
221165 pTab->nEntry++;
221166
221167 /* Figure out how large an allocation is required */
221168 nByte = sizeof(SessionChange);
221169 for(i=0; i<(pTab->nCol-pTab->bRowid); i++){
221170 sqlite3_value *p = 0;
221171 if( op!=SQLITE_INSERT ){
221172 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
221173 assert( trc==SQLITE_OK );
221174 }else if( pTab->abPK[i] ){
221175 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
221176 assert( trc==SQLITE_OK );
221177 }
221178
221179 /* This may fail if SQLite value p contains a utf-16 string that must
221180 ** be converted to utf-8 and an OOM error occurs while doing so. */
221181 rc = sessionSerializeValue(0, p, &nByte);
221182 if( rc!=SQLITE_OK ) goto error_out;
221183 }
221184 if( pTab->bRowid ){
221185 nByte += 9; /* Size of rowid field - an integer */
221186 }
221187
221188 /* Allocate the change object */
221189 pC = (SessionChange *)sessionMalloc64(pSession, nByte);
221190 if( !pC ){
221191 rc = SQLITE_NOMEM;
221192 goto error_out;
221193 }else{
221194 memset(pC, 0, sizeof(SessionChange));
221195 pC->aRecord = (u8 *)&pC[1];
221196 }
221197
221198 /* Populate the change object. None of the preupdate_old(),
221199 ** preupdate_new() or SerializeValue() calls below may fail as all
221200 ** required values and encodings have already been cached in memory.
221201 ** It is not possible for an OOM to occur in this block. */
221202 nByte = 0;
221203 if( pTab->bRowid ){
221204 pC->aRecord[0] = SQLITE_INTEGER;
221205 sessionPutI64(&pC->aRecord[1], iRowid);
221206 nByte = 9;
221207 }
221208 for(i=0; i<(pTab->nCol-pTab->bRowid); i++){
221209 sqlite3_value *p = 0;
221210 if( op!=SQLITE_INSERT ){
221211 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
221212 }else if( pTab->abPK[i] ){
221213 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
221214 }
221215 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
221216 }
221217
221218 /* Add the change to the hash-table */
221219 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
221220 pC->bIndirect = 1;
221221 }
221222 pC->nRecord = nByte;
221223 pC->op = op;
221224 pC->pNext = pTab->apChange[iHash];
221225 pTab->apChange[iHash] = pC;
221226
221227 }else if( pC->bIndirect ){
221228 /* If the existing change is considered "indirect", but this current
221229 ** change is "direct", mark the change object as direct. */
221230 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
221231 && pSession->bIndirect==0
221232 ){
221233 pC->bIndirect = 0;
221234 }
221235 }
221236
221237 assert( rc==SQLITE_OK );
221238 if( pSession->bEnableSize ){
221239 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
221240 }
221241 }
221242
221243
221244 /* If an error has occurred, mark the session object as failed. */
221245 error_out:
221246 if( pTab->bStat1 ){
221247 pSession->hook = stat1.hook;
221248 }
221249 if( rc!=SQLITE_OK ){
221250 pSession->rc = rc;
221251 }
221252}
221253
221254static int sessionFindTable(
221255 sqlite3_session *pSession,
221256 const char *zName,
221257 SessionTable **ppTab
221258){
221259 int rc = SQLITE_OK;
221260 int nName = sqlite3Strlen30(zName);
221261 SessionTable *pRet;
221262
221263 /* Search for an existing table */
221264 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
221265 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
221266 }
221267
221268 if( pRet==0 && pSession->bAutoAttach ){
221269 /* If there is a table-filter configured, invoke it. If it returns 0,
221270 ** do not automatically add the new table. */
221271 if( pSession->xTableFilter==0
221272 || pSession->xTableFilter(pSession->pFilterCtx, zName)
221273 ){
221274 rc = sqlite3session_attach(pSession, zName);
221275 if( rc==SQLITE_OK ){
221276 pRet = pSession->pTable;
221277 while( ALWAYS(pRet) && pRet->pNext ){
221278 pRet = pRet->pNext;
221279 }
221280 assert( pRet!=0 );
221281 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
221282 }
221283 }
221284 }
221285
221286 assert( rc==SQLITE_OK || pRet==0 );
221287 *ppTab = pRet;
221288 return rc;
221289}
221290
221291/*
221292** The 'pre-update' hook registered by this module with SQLite databases.
221293*/
221294static void xPreUpdate(
221295 void *pCtx, /* Copy of third arg to preupdate_hook() */
221296 sqlite3 *db, /* Database handle */
221297 int op, /* SQLITE_UPDATE, DELETE or INSERT */
221298 char const *zDb, /* Database name */
221299 char const *zName, /* Table name */
221300 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
221301 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
221302){
221303 sqlite3_session *pSession;
221304 int nDb = sqlite3Strlen30(zDb);
221305
221306 assert( sqlite3_mutex_held(db->mutex) );
221307 (void)iKey1;
221308 (void)iKey2;
221309
221310 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
221311 SessionTable *pTab;
221312
221313 /* If this session is attached to a different database ("main", "temp"
221314 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
221315 ** to the next session object attached to this database. */
221316 if( pSession->bEnable==0 ) continue;
221317 if( pSession->rc ) continue;
221318 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
221319
221320 pSession->rc = sessionFindTable(pSession, zName, &pTab);
221321 if( pTab ){
221322 assert( pSession->rc==SQLITE_OK );
221323 assert( op==SQLITE_UPDATE || iKey1==iKey2 );
221324 sessionPreupdateOneChange(op, iKey1, pSession, pTab);
221325 if( op==SQLITE_UPDATE ){
221326 sessionPreupdateOneChange(SQLITE_INSERT, iKey2, pSession, pTab);
221327 }
221328 }
221329 }
221330}
221331
221332/*
221333** The pre-update hook implementations.
221334*/
221335static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
221336 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
221337}
221338static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
221339 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
221340}
221341static int sessionPreupdateCount(void *pCtx){
221342 return sqlite3_preupdate_count((sqlite3*)pCtx);
221343}
221344static int sessionPreupdateDepth(void *pCtx){
221345 return sqlite3_preupdate_depth((sqlite3*)pCtx);
221346}
221347
221348/*
221349** Install the pre-update hooks on the session object passed as the only
221350** argument.
221351*/
221352static void sessionPreupdateHooks(
221353 sqlite3_session *pSession
221354){
221355 pSession->hook.pCtx = (void*)pSession->db;
221356 pSession->hook.xOld = sessionPreupdateOld;
221357 pSession->hook.xNew = sessionPreupdateNew;
221358 pSession->hook.xCount = sessionPreupdateCount;
221359 pSession->hook.xDepth = sessionPreupdateDepth;
221360}
221361
221362typedef struct SessionDiffCtx SessionDiffCtx;
221363struct SessionDiffCtx {
221364 sqlite3_stmt *pStmt;
221365 int bRowid;
221366 int nOldOff;
221367};
221368
221369/*
221370** The diff hook implementations.
221371*/
221372static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
221373 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
221374 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff+p->bRowid);
221375 return SQLITE_OK;
221376}
221377static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
221378 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
221379 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->bRowid);
221380 return SQLITE_OK;
221381}
221382static int sessionDiffCount(void *pCtx){
221383 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
221384 return (p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt)) - p->bRowid;
221385}
221386static int sessionDiffDepth(void *pCtx){
221387 (void)pCtx;
221388 return 0;
221389}
221390
221391/*
221392** Install the diff hooks on the session object passed as the only
221393** argument.
221394*/
221395static void sessionDiffHooks(
221396 sqlite3_session *pSession,
221397 SessionDiffCtx *pDiffCtx
221398){
221399 pSession->hook.pCtx = (void*)pDiffCtx;
221400 pSession->hook.xOld = sessionDiffOld;
221401 pSession->hook.xNew = sessionDiffNew;
221402 pSession->hook.xCount = sessionDiffCount;
221403 pSession->hook.xDepth = sessionDiffDepth;
221404}
221405
221406static char *sessionExprComparePK(
221407 int nCol,
221408 const char *zDb1, const char *zDb2,
221409 const char *zTab,
221410 const char **azCol, u8 *abPK
221411){
221412 int i;
221413 const char *zSep = "";
221414 char *zRet = 0;
221415
221416 for(i=0; i<nCol; i++){
221417 if( abPK[i] ){
221418 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
221419 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
221420 );
221421 zSep = " AND ";
221422 if( zRet==0 ) break;
221423 }
221424 }
221425
221426 return zRet;
221427}
221428
221429static char *sessionExprCompareOther(
221430 int nCol,
221431 const char *zDb1, const char *zDb2,
221432 const char *zTab,
221433 const char **azCol, u8 *abPK
221434){
221435 int i;
221436 const char *zSep = "";
221437 char *zRet = 0;
221438 int bHave = 0;
221439
221440 for(i=0; i<nCol; i++){
221441 if( abPK[i]==0 ){
221442 bHave = 1;
221443 zRet = sqlite3_mprintf(
221444 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
221445 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
221446 );
221447 zSep = " OR ";
221448 if( zRet==0 ) break;
221449 }
221450 }
221451
221452 if( bHave==0 ){
221453 assert( zRet==0 );
221454 zRet = sqlite3_mprintf("0");
221455 }
221456
221457 return zRet;
221458}
221459
221460static char *sessionSelectFindNew(
221461 const char *zDb1, /* Pick rows in this db only */
221462 const char *zDb2, /* But not in this one */
221463 int bRowid,
221464 const char *zTbl, /* Table name */
221465 const char *zExpr
221466){
221467 const char *zSel = (bRowid ? SESSIONS_ROWID ", *" : "*");
221468 char *zRet = sqlite3_mprintf(
221469 "SELECT %s FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
221470 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
221471 ")",
221472 zSel, zDb1, zTbl, zDb2, zTbl, zExpr
221473 );
221474 return zRet;
221475}
221476
221477static int sessionDiffFindNew(
221478 int op,
221479 sqlite3_session *pSession,
221480 SessionTable *pTab,
221481 const char *zDb1,
221482 const char *zDb2,
221483 char *zExpr
221484){
221485 int rc = SQLITE_OK;
221486 char *zStmt = sessionSelectFindNew(
221487 zDb1, zDb2, pTab->bRowid, pTab->zName, zExpr
221488 );
221489
221490 if( zStmt==0 ){
221491 rc = SQLITE_NOMEM;
221492 }else{
221493 sqlite3_stmt *pStmt;
221494 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
221495 if( rc==SQLITE_OK ){
221496 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
221497 pDiffCtx->pStmt = pStmt;
221498 pDiffCtx->nOldOff = 0;
221499 pDiffCtx->bRowid = pTab->bRowid;
221500 while( SQLITE_ROW==sqlite3_step(pStmt) ){
221501 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
221502 sessionPreupdateOneChange(op, iRowid, pSession, pTab);
221503 }
221504 rc = sqlite3_finalize(pStmt);
221505 }
221506 sqlite3_free(zStmt);
221507 }
221508
221509 return rc;
221510}
221511
221512/*
221513** Return a comma-separated list of the fully-qualified (with both database
221514** and table name) column names from table pTab. e.g.
221515**
221516** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c"
221517*/
221518static char *sessionAllCols(
221519 const char *zDb,
221520 SessionTable *pTab
221521){
221522 int ii;
221523 char *zRet = 0;
221524 for(ii=0; ii<pTab->nCol; ii++){
221525 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"",
221526 zRet, (zRet ? ", " : ""), zDb, pTab->zName, pTab->azCol[ii]
221527 );
221528 if( !zRet ) break;
221529 }
221530 return zRet;
221531}
221532
221533static int sessionDiffFindModified(
221534 sqlite3_session *pSession,
221535 SessionTable *pTab,
221536 const char *zFrom,
221537 const char *zExpr
221538){
221539 int rc = SQLITE_OK;
221540
221541 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
221542 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
221543 );
221544 if( zExpr2==0 ){
221545 rc = SQLITE_NOMEM;
221546 }else{
221547 char *z1 = sessionAllCols(pSession->zDb, pTab);
221548 char *z2 = sessionAllCols(zFrom, pTab);
221549 char *zStmt = sqlite3_mprintf(
221550 "SELECT %s,%s FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
221551 z1, z2, pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
221552 );
221553 if( zStmt==0 || z1==0 || z2==0 ){
221554 rc = SQLITE_NOMEM;
221555 }else{
221556 sqlite3_stmt *pStmt;
221557 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
221558
221559 if( rc==SQLITE_OK ){
221560 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
221561 pDiffCtx->pStmt = pStmt;
221562 pDiffCtx->nOldOff = pTab->nCol;
221563 while( SQLITE_ROW==sqlite3_step(pStmt) ){
221564 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
221565 sessionPreupdateOneChange(SQLITE_UPDATE, iRowid, pSession, pTab);
221566 }
221567 rc = sqlite3_finalize(pStmt);
221568 }
221569 }
221570 sqlite3_free(zStmt);
221571 sqlite3_free(z1);
221572 sqlite3_free(z2);
221573 }
221574
221575 return rc;
221576}
221577
221578SQLITE_API int sqlite3session_diff(
221579 sqlite3_session *pSession,
221580 const char *zFrom,
221581 const char *zTbl,
221582 char **pzErrMsg
221583){
221584 const char *zDb = pSession->zDb;
221585 int rc = pSession->rc;
221586 SessionDiffCtx d;
221587
221588 memset(&d, 0, sizeof(d));
221589 sessionDiffHooks(pSession, &d);
221590
221591 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
221592 if( pzErrMsg ) *pzErrMsg = 0;
221593 if( rc==SQLITE_OK ){
221594 char *zExpr = 0;
221595 sqlite3 *db = pSession->db;
221596 SessionTable *pTo; /* Table zTbl */
221597
221598 /* Locate and if necessary initialize the target table object */
221599 rc = sessionFindTable(pSession, zTbl, &pTo);
221600 if( pTo==0 ) goto diff_out;
221601 if( sessionInitTable(pSession, pTo) ){
221602 rc = pSession->rc;
221603 goto diff_out;
221604 }
221605
221606 /* Check the table schemas match */
221607 if( rc==SQLITE_OK ){
221608 int bHasPk = 0;
221609 int bMismatch = 0;
221610 int nCol; /* Columns in zFrom.zTbl */
221611 int bRowid = 0;
221612 u8 *abPK;
221613 const char **azCol = 0;
221614 rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK,
221615 pSession->bImplicitPK ? &bRowid : 0
221616 );
221617 if( rc==SQLITE_OK ){
221618 if( pTo->nCol!=nCol ){
221619 bMismatch = 1;
221620 }else{
221621 int i;
221622 for(i=0; i<nCol; i++){
221623 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
221624 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
221625 if( abPK[i] ) bHasPk = 1;
221626 }
221627 }
221628 }
221629 sqlite3_free((char*)azCol);
221630 if( bMismatch ){
221631 if( pzErrMsg ){
221632 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
221633 }
221634 rc = SQLITE_SCHEMA;
221635 }
221636 if( bHasPk==0 ){
221637 /* Ignore tables with no primary keys */
221638 goto diff_out;
221639 }
221640 }
221641
221642 if( rc==SQLITE_OK ){
221643 zExpr = sessionExprComparePK(pTo->nCol,
221644 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
221645 );
221646 }
221647
221648 /* Find new rows */
221649 if( rc==SQLITE_OK ){
221650 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
221651 }
221652
221653 /* Find old rows */
221654 if( rc==SQLITE_OK ){
221655 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
221656 }
221657
221658 /* Find modified rows */
221659 if( rc==SQLITE_OK ){
221660 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
221661 }
221662
221663 sqlite3_free(zExpr);
221664 }
221665
221666 diff_out:
221667 sessionPreupdateHooks(pSession);
221668 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
221669 return rc;
221670}
221671
221672/*
221673** Create a session object. This session object will record changes to
221674** database zDb attached to connection db.
221675*/
221676SQLITE_API int sqlite3session_create(
221677 sqlite3 *db, /* Database handle */
221678 const char *zDb, /* Name of db (e.g. "main") */
221679 sqlite3_session **ppSession /* OUT: New session object */
221680){
221681 sqlite3_session *pNew; /* Newly allocated session object */
221682 sqlite3_session *pOld; /* Session object already attached to db */
221683 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
221684
221685 /* Zero the output value in case an error occurs. */
221686 *ppSession = 0;
221687
221688 /* Allocate and populate the new session object. */
221689 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
221690 if( !pNew ) return SQLITE_NOMEM;
221691 memset(pNew, 0, sizeof(sqlite3_session));
221692 pNew->db = db;
221693 pNew->zDb = (char *)&pNew[1];
221694 pNew->bEnable = 1;
221695 memcpy(pNew->zDb, zDb, nDb+1);
221696 sessionPreupdateHooks(pNew);
221697
221698 /* Add the new session object to the linked list of session objects
221699 ** attached to database handle $db. Do this under the cover of the db
221700 ** handle mutex. */
221701 sqlite3_mutex_enter(sqlite3_db_mutex(db));
221702 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
221703 pNew->pNext = pOld;
221704 sqlite3_mutex_leave(sqlite3_db_mutex(db));
221705
221706 *ppSession = pNew;
221707 return SQLITE_OK;
221708}
221709
221710/*
221711** Free the list of table objects passed as the first argument. The contents
221712** of the changed-rows hash tables are also deleted.
221713*/
221714static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
221715 SessionTable *pNext;
221716 SessionTable *pTab;
221717
221718 for(pTab=pList; pTab; pTab=pNext){
221719 int i;
221720 pNext = pTab->pNext;
221721 for(i=0; i<pTab->nChange; i++){
221722 SessionChange *p;
221723 SessionChange *pNextChange;
221724 for(p=pTab->apChange[i]; p; p=pNextChange){
221725 pNextChange = p->pNext;
221726 sessionFree(pSession, p);
221727 }
221728 }
221729 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
221730 sessionFree(pSession, pTab->apChange);
221731 sessionFree(pSession, pTab);
221732 }
221733}
221734
221735/*
221736** Delete a session object previously allocated using sqlite3session_create().
221737*/
221738SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
221739 sqlite3 *db = pSession->db;
221740 sqlite3_session *pHead;
221741 sqlite3_session **pp;
221742
221743 /* Unlink the session from the linked list of sessions attached to the
221744 ** database handle. Hold the db mutex while doing so. */
221745 sqlite3_mutex_enter(sqlite3_db_mutex(db));
221746 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
221747 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
221748 if( (*pp)==pSession ){
221749 *pp = (*pp)->pNext;
221750 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
221751 break;
221752 }
221753 }
221754 sqlite3_mutex_leave(sqlite3_db_mutex(db));
221755 sqlite3ValueFree(pSession->pZeroBlob);
221756
221757 /* Delete all attached table objects. And the contents of their
221758 ** associated hash-tables. */
221759 sessionDeleteTable(pSession, pSession->pTable);
221760
221761 /* Assert that all allocations have been freed and then free the
221762 ** session object itself. */
221763 assert( pSession->nMalloc==0 );
221764 sqlite3_free(pSession);
221765}
221766
221767/*
221768** Set a table filter on a Session Object.
221769*/
221770SQLITE_API void sqlite3session_table_filter(
221771 sqlite3_session *pSession,
221772 int(*xFilter)(void*, const char*),
221773 void *pCtx /* First argument passed to xFilter */
221774){
221775 pSession->bAutoAttach = 1;
221776 pSession->pFilterCtx = pCtx;
221777 pSession->xTableFilter = xFilter;
221778}
221779
221780/*
221781** Attach a table to a session. All subsequent changes made to the table
221782** while the session object is enabled will be recorded.
221783**
221784** Only tables that have a PRIMARY KEY defined may be attached. It does
221785** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
221786** or not.
221787*/
221788SQLITE_API int sqlite3session_attach(
221789 sqlite3_session *pSession, /* Session object */
221790 const char *zName /* Table name */
221791){
221792 int rc = SQLITE_OK;
221793 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
221794
221795 if( !zName ){
221796 pSession->bAutoAttach = 1;
221797 }else{
221798 SessionTable *pTab; /* New table object (if required) */
221799 int nName; /* Number of bytes in string zName */
221800
221801 /* First search for an existing entry. If one is found, this call is
221802 ** a no-op. Return early. */
221803 nName = sqlite3Strlen30(zName);
221804 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
221805 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
221806 }
221807
221808 if( !pTab ){
221809 /* Allocate new SessionTable object. */
221810 int nByte = sizeof(SessionTable) + nName + 1;
221811 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
221812 if( !pTab ){
221813 rc = SQLITE_NOMEM;
221814 }else{
221815 /* Populate the new SessionTable object and link it into the list.
221816 ** The new object must be linked onto the end of the list, not
221817 ** simply added to the start of it in order to ensure that tables
221818 ** appear in the correct order when a changeset or patchset is
221819 ** eventually generated. */
221820 SessionTable **ppTab;
221821 memset(pTab, 0, sizeof(SessionTable));
221822 pTab->zName = (char *)&pTab[1];
221823 memcpy(pTab->zName, zName, nName+1);
221824 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
221825 *ppTab = pTab;
221826 }
221827 }
221828 }
221829
221830 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
221831 return rc;
221832}
221833
221834/*
221835** Ensure that there is room in the buffer to append nByte bytes of data.
221836** If not, use sqlite3_realloc() to grow the buffer so that there is.
221837**
221838** If successful, return zero. Otherwise, if an OOM condition is encountered,
221839** set *pRc to SQLITE_NOMEM and return non-zero.
221840*/
221841static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
221842#define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1)
221843 i64 nReq = p->nBuf + nByte;
221844 if( *pRc==SQLITE_OK && nReq>p->nAlloc ){
221845 u8 *aNew;
221846 i64 nNew = p->nAlloc ? p->nAlloc : 128;
221847
221848 do {
221849 nNew = nNew*2;
221850 }while( nNew<nReq );
221851
221852 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
221853 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
221854 ** always fail. It is used here to ensure that this routine can always
221855 ** allocate up to this limit - instead of up to the largest power of
221856 ** two smaller than the limit. */
221857 if( nNew>SESSION_MAX_BUFFER_SZ ){
221858 nNew = SESSION_MAX_BUFFER_SZ;
221859 if( nNew<nReq ){
221860 *pRc = SQLITE_NOMEM;
221861 return 1;
221862 }
221863 }
221864
221865 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
221866 if( 0==aNew ){
221867 *pRc = SQLITE_NOMEM;
221868 }else{
221869 p->aBuf = aNew;
221870 p->nAlloc = nNew;
221871 }
221872 }
221873 return (*pRc!=SQLITE_OK);
221874}
221875
221876/*
221877** Append the value passed as the second argument to the buffer passed
221878** as the first.
221879**
221880** This function is a no-op if *pRc is non-zero when it is called.
221881** Otherwise, if an error occurs, *pRc is set to an SQLite error code
221882** before returning.
221883*/
221884static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
221885 int rc = *pRc;
221886 if( rc==SQLITE_OK ){
221887 sqlite3_int64 nByte = 0;
221888 rc = sessionSerializeValue(0, pVal, &nByte);
221889 sessionBufferGrow(p, nByte, &rc);
221890 if( rc==SQLITE_OK ){
221891 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
221892 p->nBuf += nByte;
221893 }else{
221894 *pRc = rc;
221895 }
221896 }
221897}
221898
221899/*
221900** This function is a no-op if *pRc is other than SQLITE_OK when it is
221901** called. Otherwise, append a single byte to the buffer.
221902**
221903** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
221904** returning.
221905*/
221906static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
221907 if( 0==sessionBufferGrow(p, 1, pRc) ){
221908 p->aBuf[p->nBuf++] = v;
221909 }
221910}
221911
221912/*
221913** This function is a no-op if *pRc is other than SQLITE_OK when it is
221914** called. Otherwise, append a single varint to the buffer.
221915**
221916** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
221917** returning.
221918*/
221919static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
221920 if( 0==sessionBufferGrow(p, 9, pRc) ){
221921 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
221922 }
221923}
221924
221925/*
221926** This function is a no-op if *pRc is other than SQLITE_OK when it is
221927** called. Otherwise, append a blob of data to the buffer.
221928**
221929** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
221930** returning.
221931*/
221932static void sessionAppendBlob(
221933 SessionBuffer *p,
221934 const u8 *aBlob,
221935 int nBlob,
221936 int *pRc
221937){
221938 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
221939 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
221940 p->nBuf += nBlob;
221941 }
221942}
221943
221944/*
221945** This function is a no-op if *pRc is other than SQLITE_OK when it is
221946** called. Otherwise, append a string to the buffer. All bytes in the string
221947** up to (but not including) the nul-terminator are written to the buffer.
221948**
221949** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
221950** returning.
221951*/
221952static void sessionAppendStr(
221953 SessionBuffer *p,
221954 const char *zStr,
221955 int *pRc
221956){
221957 int nStr = sqlite3Strlen30(zStr);
221958 if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
221959 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
221960 p->nBuf += nStr;
221961 p->aBuf[p->nBuf] = 0x00;
221962 }
221963}
221964
221965/*
221966** This function is a no-op if *pRc is other than SQLITE_OK when it is
221967** called. Otherwise, append the string representation of integer iVal
221968** to the buffer. No nul-terminator is written.
221969**
221970** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
221971** returning.
221972*/
221973static void sessionAppendInteger(
221974 SessionBuffer *p, /* Buffer to append to */
221975 int iVal, /* Value to write the string rep. of */
221976 int *pRc /* IN/OUT: Error code */
221977){
221978 char aBuf[24];
221979 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
221980 sessionAppendStr(p, aBuf, pRc);
221981}
221982
221983static void sessionAppendPrintf(
221984 SessionBuffer *p, /* Buffer to append to */
221985 int *pRc,
221986 const char *zFmt,
221987 ...
221988){
221989 if( *pRc==SQLITE_OK ){
221990 char *zApp = 0;
221991 va_list ap;
221992 va_start(ap, zFmt);
221993 zApp = sqlite3_vmprintf(zFmt, ap);
221994 if( zApp==0 ){
221995 *pRc = SQLITE_NOMEM;
221996 }else{
221997 sessionAppendStr(p, zApp, pRc);
221998 }
221999 va_end(ap);
222000 sqlite3_free(zApp);
222001 }
222002}
222003
222004/*
222005** This function is a no-op if *pRc is other than SQLITE_OK when it is
222006** called. Otherwise, append the string zStr enclosed in quotes (") and
222007** with any embedded quote characters escaped to the buffer. No
222008** nul-terminator byte is written.
222009**
222010** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
222011** returning.
222012*/
222013static void sessionAppendIdent(
222014 SessionBuffer *p, /* Buffer to a append to */
222015 const char *zStr, /* String to quote, escape and append */
222016 int *pRc /* IN/OUT: Error code */
222017){
222018 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 2;
222019 if( 0==sessionBufferGrow(p, nStr, pRc) ){
222020 char *zOut = (char *)&p->aBuf[p->nBuf];
222021 const char *zIn = zStr;
222022 *zOut++ = '"';
222023 while( *zIn ){
222024 if( *zIn=='"' ) *zOut++ = '"';
222025 *zOut++ = *(zIn++);
222026 }
222027 *zOut++ = '"';
222028 p->nBuf = (int)((u8 *)zOut - p->aBuf);
222029 p->aBuf[p->nBuf] = 0x00;
222030 }
222031}
222032
222033/*
222034** This function is a no-op if *pRc is other than SQLITE_OK when it is
222035** called. Otherwse, it appends the serialized version of the value stored
222036** in column iCol of the row that SQL statement pStmt currently points
222037** to to the buffer.
222038*/
222039static void sessionAppendCol(
222040 SessionBuffer *p, /* Buffer to append to */
222041 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
222042 int iCol, /* Column to read value from */
222043 int *pRc /* IN/OUT: Error code */
222044){
222045 if( *pRc==SQLITE_OK ){
222046 int eType = sqlite3_column_type(pStmt, iCol);
222047 sessionAppendByte(p, (u8)eType, pRc);
222048 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
222049 sqlite3_int64 i;
222050 u8 aBuf[8];
222051 if( eType==SQLITE_INTEGER ){
222052 i = sqlite3_column_int64(pStmt, iCol);
222053 }else{
222054 double r = sqlite3_column_double(pStmt, iCol);
222055 memcpy(&i, &r, 8);
222056 }
222057 sessionPutI64(aBuf, i);
222058 sessionAppendBlob(p, aBuf, 8, pRc);
222059 }
222060 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
222061 u8 *z;
222062 int nByte;
222063 if( eType==SQLITE_BLOB ){
222064 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
222065 }else{
222066 z = (u8 *)sqlite3_column_text(pStmt, iCol);
222067 }
222068 nByte = sqlite3_column_bytes(pStmt, iCol);
222069 if( z || (eType==SQLITE_BLOB && nByte==0) ){
222070 sessionAppendVarint(p, nByte, pRc);
222071 sessionAppendBlob(p, z, nByte, pRc);
222072 }else{
222073 *pRc = SQLITE_NOMEM;
222074 }
222075 }
222076 }
222077}
222078
222079/*
222080**
222081** This function appends an update change to the buffer (see the comments
222082** under "CHANGESET FORMAT" at the top of the file). An update change
222083** consists of:
222084**
222085** 1 byte: SQLITE_UPDATE (0x17)
222086** n bytes: old.* record (see RECORD FORMAT)
222087** m bytes: new.* record (see RECORD FORMAT)
222088**
222089** The SessionChange object passed as the third argument contains the
222090** values that were stored in the row when the session began (the old.*
222091** values). The statement handle passed as the second argument points
222092** at the current version of the row (the new.* values).
222093**
222094** If all of the old.* values are equal to their corresponding new.* value
222095** (i.e. nothing has changed), then no data at all is appended to the buffer.
222096**
222097** Otherwise, the old.* record contains all primary key values and the
222098** original values of any fields that have been modified. The new.* record
222099** contains the new values of only those fields that have been modified.
222100*/
222101static int sessionAppendUpdate(
222102 SessionBuffer *pBuf, /* Buffer to append to */
222103 int bPatchset, /* True for "patchset", 0 for "changeset" */
222104 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
222105 SessionChange *p, /* Object containing old values */
222106 u8 *abPK /* Boolean array - true for PK columns */
222107){
222108 int rc = SQLITE_OK;
222109 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
222110 int bNoop = 1; /* Set to zero if any values are modified */
222111 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
222112 int i; /* Used to iterate through columns */
222113 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
222114
222115 assert( abPK!=0 );
222116 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
222117 sessionAppendByte(pBuf, p->bIndirect, &rc);
222118 for(i=0; i<sqlite3_column_count(pStmt); i++){
222119 int bChanged = 0;
222120 int nAdvance;
222121 int eType = *pCsr;
222122 switch( eType ){
222123 case SQLITE_NULL:
222124 nAdvance = 1;
222125 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
222126 bChanged = 1;
222127 }
222128 break;
222129
222130 case SQLITE_FLOAT:
222131 case SQLITE_INTEGER: {
222132 nAdvance = 9;
222133 if( eType==sqlite3_column_type(pStmt, i) ){
222134 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
222135 if( eType==SQLITE_INTEGER ){
222136 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
222137 }else{
222138 double dVal;
222139 memcpy(&dVal, &iVal, 8);
222140 if( dVal==sqlite3_column_double(pStmt, i) ) break;
222141 }
222142 }
222143 bChanged = 1;
222144 break;
222145 }
222146
222147 default: {
222148 int n;
222149 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
222150 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
222151 nAdvance = nHdr + n;
222152 if( eType==sqlite3_column_type(pStmt, i)
222153 && n==sqlite3_column_bytes(pStmt, i)
222154 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
222155 ){
222156 break;
222157 }
222158 bChanged = 1;
222159 }
222160 }
222161
222162 /* If at least one field has been modified, this is not a no-op. */
222163 if( bChanged ) bNoop = 0;
222164
222165 /* Add a field to the old.* record. This is omitted if this module is
222166 ** currently generating a patchset. */
222167 if( bPatchset==0 ){
222168 if( bChanged || abPK[i] ){
222169 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
222170 }else{
222171 sessionAppendByte(pBuf, 0, &rc);
222172 }
222173 }
222174
222175 /* Add a field to the new.* record. Or the only record if currently
222176 ** generating a patchset. */
222177 if( bChanged || (bPatchset && abPK[i]) ){
222178 sessionAppendCol(&buf2, pStmt, i, &rc);
222179 }else{
222180 sessionAppendByte(&buf2, 0, &rc);
222181 }
222182
222183 pCsr += nAdvance;
222184 }
222185
222186 if( bNoop ){
222187 pBuf->nBuf = nRewind;
222188 }else{
222189 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
222190 }
222191 sqlite3_free(buf2.aBuf);
222192
222193 return rc;
222194}
222195
222196/*
222197** Append a DELETE change to the buffer passed as the first argument. Use
222198** the changeset format if argument bPatchset is zero, or the patchset
222199** format otherwise.
222200*/
222201static int sessionAppendDelete(
222202 SessionBuffer *pBuf, /* Buffer to append to */
222203 int bPatchset, /* True for "patchset", 0 for "changeset" */
222204 SessionChange *p, /* Object containing old values */
222205 int nCol, /* Number of columns in table */
222206 u8 *abPK /* Boolean array - true for PK columns */
222207){
222208 int rc = SQLITE_OK;
222209
222210 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
222211 sessionAppendByte(pBuf, p->bIndirect, &rc);
222212
222213 if( bPatchset==0 ){
222214 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
222215 }else{
222216 int i;
222217 u8 *a = p->aRecord;
222218 for(i=0; i<nCol; i++){
222219 u8 *pStart = a;
222220 int eType = *a++;
222221
222222 switch( eType ){
222223 case 0:
222224 case SQLITE_NULL:
222225 assert( abPK[i]==0 );
222226 break;
222227
222228 case SQLITE_FLOAT:
222229 case SQLITE_INTEGER:
222230 a += 8;
222231 break;
222232
222233 default: {
222234 int n;
222235 a += sessionVarintGet(a, &n);
222236 a += n;
222237 break;
222238 }
222239 }
222240 if( abPK[i] ){
222241 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
222242 }
222243 }
222244 assert( (a - p->aRecord)==p->nRecord );
222245 }
222246
222247 return rc;
222248}
222249
222250/*
222251** Formulate and prepare a SELECT statement to retrieve a row from table
222252** zTab in database zDb based on its primary key. i.e.
222253**
222254** SELECT *, <noop-test> FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...)
222255**
222256** where <noop-test> is:
222257**
222258** 1 AND (?A OR ?1 IS <column>) AND ...
222259**
222260** for each non-pk <column>.
222261*/
222262static int sessionSelectStmt(
222263 sqlite3 *db, /* Database handle */
222264 int bIgnoreNoop,
222265 const char *zDb, /* Database name */
222266 const char *zTab, /* Table name */
222267 int bRowid,
222268 int nCol, /* Number of columns in table */
222269 const char **azCol, /* Names of table columns */
222270 u8 *abPK, /* PRIMARY KEY array */
222271 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
222272){
222273 int rc = SQLITE_OK;
222274 char *zSql = 0;
222275 const char *zSep = "";
222276 const char *zCols = bRowid ? SESSIONS_ROWID ", *" : "*";
222277 int nSql = -1;
222278 int i;
222279
222280 SessionBuffer nooptest = {0, 0, 0};
222281 SessionBuffer pkfield = {0, 0, 0};
222282 SessionBuffer pkvar = {0, 0, 0};
222283
222284 sessionAppendStr(&nooptest, ", 1", &rc);
222285
222286 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
222287 sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc);
222288 sessionAppendStr(&pkfield, "tbl, idx", &rc);
222289 sessionAppendStr(&pkvar,
222290 "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc
222291 );
222292 zCols = "tbl, ?2, stat";
222293 }else{
222294 for(i=0; i<nCol; i++){
222295 if( abPK[i] ){
222296 sessionAppendStr(&pkfield, zSep, &rc);
222297 sessionAppendStr(&pkvar, zSep, &rc);
222298 zSep = ", ";
222299 sessionAppendIdent(&pkfield, azCol[i], &rc);
222300 sessionAppendPrintf(&pkvar, &rc, "?%d", i+1);
222301 }else{
222302 sessionAppendPrintf(&nooptest, &rc,
222303 " AND (?%d OR ?%d IS %w.%w)", i+1+nCol, i+1, zTab, azCol[i]
222304 );
222305 }
222306 }
222307 }
222308
222309 if( rc==SQLITE_OK ){
222310 zSql = sqlite3_mprintf(
222311 "SELECT %s%s FROM %Q.%Q WHERE (%s) IS (%s)",
222312 zCols, (bIgnoreNoop ? (char*)nooptest.aBuf : ""),
222313 zDb, zTab, (char*)pkfield.aBuf, (char*)pkvar.aBuf
222314 );
222315 if( zSql==0 ) rc = SQLITE_NOMEM;
222316 }
222317
222318#if 0
222319 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
222320 zSql = sqlite3_mprintf(
222321 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
222322 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
222323 );
222324 if( zSql==0 ) rc = SQLITE_NOMEM;
222325 }else{
222326 const char *zSep = "";
222327 SessionBuffer buf = {0, 0, 0};
222328
222329 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
222330 sessionAppendIdent(&buf, zDb, &rc);
222331 sessionAppendStr(&buf, ".", &rc);
222332 sessionAppendIdent(&buf, zTab, &rc);
222333 sessionAppendStr(&buf, " WHERE ", &rc);
222334 for(i=0; i<nCol; i++){
222335 if( abPK[i] ){
222336 sessionAppendStr(&buf, zSep, &rc);
222337 sessionAppendIdent(&buf, azCol[i], &rc);
222338 sessionAppendStr(&buf, " IS ?", &rc);
222339 sessionAppendInteger(&buf, i+1, &rc);
222340 zSep = " AND ";
222341 }
222342 }
222343 zSql = (char*)buf.aBuf;
222344 nSql = buf.nBuf;
222345 }
222346#endif
222347
222348 if( rc==SQLITE_OK ){
222349 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
222350 }
222351 sqlite3_free(zSql);
222352 sqlite3_free(nooptest.aBuf);
222353 sqlite3_free(pkfield.aBuf);
222354 sqlite3_free(pkvar.aBuf);
222355 return rc;
222356}
222357
222358/*
222359** Bind the PRIMARY KEY values from the change passed in argument pChange
222360** to the SELECT statement passed as the first argument. The SELECT statement
222361** is as prepared by function sessionSelectStmt().
222362**
222363** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
222364** error code (e.g. SQLITE_NOMEM) otherwise.
222365*/
222366static int sessionSelectBind(
222367 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
222368 int nCol, /* Number of columns in table */
222369 u8 *abPK, /* PRIMARY KEY array */
222370 SessionChange *pChange /* Change structure */
222371){
222372 int i;
222373 int rc = SQLITE_OK;
222374 u8 *a = pChange->aRecord;
222375
222376 for(i=0; i<nCol && rc==SQLITE_OK; i++){
222377 int eType = *a++;
222378
222379 switch( eType ){
222380 case 0:
222381 case SQLITE_NULL:
222382 assert( abPK[i]==0 );
222383 break;
222384
222385 case SQLITE_INTEGER: {
222386 if( abPK[i] ){
222387 i64 iVal = sessionGetI64(a);
222388 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
222389 }
222390 a += 8;
222391 break;
222392 }
222393
222394 case SQLITE_FLOAT: {
222395 if( abPK[i] ){
222396 double rVal;
222397 i64 iVal = sessionGetI64(a);
222398 memcpy(&rVal, &iVal, 8);
222399 rc = sqlite3_bind_double(pSelect, i+1, rVal);
222400 }
222401 a += 8;
222402 break;
222403 }
222404
222405 case SQLITE_TEXT: {
222406 int n;
222407 a += sessionVarintGet(a, &n);
222408 if( abPK[i] ){
222409 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
222410 }
222411 a += n;
222412 break;
222413 }
222414
222415 default: {
222416 int n;
222417 assert( eType==SQLITE_BLOB );
222418 a += sessionVarintGet(a, &n);
222419 if( abPK[i] ){
222420 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
222421 }
222422 a += n;
222423 break;
222424 }
222425 }
222426 }
222427
222428 return rc;
222429}
222430
222431/*
222432** This function is a no-op if *pRc is set to other than SQLITE_OK when it
222433** is called. Otherwise, append a serialized table header (part of the binary
222434** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
222435** SQLite error code before returning.
222436*/
222437static void sessionAppendTableHdr(
222438 SessionBuffer *pBuf, /* Append header to this buffer */
222439 int bPatchset, /* Use the patchset format if true */
222440 SessionTable *pTab, /* Table object to append header for */
222441 int *pRc /* IN/OUT: Error code */
222442){
222443 /* Write a table header */
222444 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
222445 sessionAppendVarint(pBuf, pTab->nCol, pRc);
222446 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
222447 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
222448}
222449
222450/*
222451** Generate either a changeset (if argument bPatchset is zero) or a patchset
222452** (if it is non-zero) based on the current contents of the session object
222453** passed as the first argument.
222454**
222455** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
222456** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
222457** occurs, an SQLite error code is returned and both output variables set
222458** to 0.
222459*/
222460static int sessionGenerateChangeset(
222461 sqlite3_session *pSession, /* Session object */
222462 int bPatchset, /* True for patchset, false for changeset */
222463 int (*xOutput)(void *pOut, const void *pData, int nData),
222464 void *pOut, /* First argument for xOutput */
222465 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
222466 void **ppChangeset /* OUT: Buffer containing changeset */
222467){
222468 sqlite3 *db = pSession->db; /* Source database handle */
222469 SessionTable *pTab; /* Used to iterate through attached tables */
222470 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
222471 int rc; /* Return code */
222472
222473 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) );
222474 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) );
222475
222476 /* Zero the output variables in case an error occurs. If this session
222477 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
222478 ** this call will be a no-op. */
222479 if( xOutput==0 ){
222480 assert( pnChangeset!=0 && ppChangeset!=0 );
222481 *pnChangeset = 0;
222482 *ppChangeset = 0;
222483 }
222484
222485 if( pSession->rc ) return pSession->rc;
222486 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
222487 if( rc!=SQLITE_OK ) return rc;
222488
222489 sqlite3_mutex_enter(sqlite3_db_mutex(db));
222490
222491 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
222492 if( pTab->nEntry ){
222493 const char *zName = pTab->zName;
222494 int nCol = 0; /* Number of columns in table */
222495 u8 *abPK = 0; /* Primary key array */
222496 const char **azCol = 0; /* Table columns */
222497 int i; /* Used to iterate through hash buckets */
222498 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
222499 int nRewind = buf.nBuf; /* Initial size of write buffer */
222500 int nNoop; /* Size of buffer after writing tbl header */
222501 int bRowid = 0;
222502
222503 /* Check the table schema is still Ok. */
222504 rc = sessionTableInfo(
222505 0, db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK,
222506 (pSession->bImplicitPK ? &bRowid : 0)
222507 );
222508 if( rc==SQLITE_OK && (
222509 pTab->nCol!=nCol
222510 || pTab->bRowid!=bRowid
222511 || memcmp(abPK, pTab->abPK, nCol)
222512 )){
222513 rc = SQLITE_SCHEMA;
222514 }
222515
222516 /* Write a table header */
222517 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
222518
222519 /* Build and compile a statement to execute: */
222520 if( rc==SQLITE_OK ){
222521 rc = sessionSelectStmt(
222522 db, 0, pSession->zDb, zName, bRowid, nCol, azCol, abPK, &pSel
222523 );
222524 }
222525
222526 nNoop = buf.nBuf;
222527 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
222528 SessionChange *p; /* Used to iterate through changes */
222529
222530 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
222531 rc = sessionSelectBind(pSel, nCol, abPK, p);
222532 if( rc!=SQLITE_OK ) continue;
222533 if( sqlite3_step(pSel)==SQLITE_ROW ){
222534 if( p->op==SQLITE_INSERT ){
222535 int iCol;
222536 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
222537 sessionAppendByte(&buf, p->bIndirect, &rc);
222538 for(iCol=0; iCol<nCol; iCol++){
222539 sessionAppendCol(&buf, pSel, iCol, &rc);
222540 }
222541 }else{
222542 assert( abPK!=0 ); /* Because sessionSelectStmt() returned ok */
222543 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
222544 }
222545 }else if( p->op!=SQLITE_INSERT ){
222546 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
222547 }
222548 if( rc==SQLITE_OK ){
222549 rc = sqlite3_reset(pSel);
222550 }
222551
222552 /* If the buffer is now larger than sessions_strm_chunk_size, pass
222553 ** its contents to the xOutput() callback. */
222554 if( xOutput
222555 && rc==SQLITE_OK
222556 && buf.nBuf>nNoop
222557 && buf.nBuf>sessions_strm_chunk_size
222558 ){
222559 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
222560 nNoop = -1;
222561 buf.nBuf = 0;
222562 }
222563
222564 }
222565 }
222566
222567 sqlite3_finalize(pSel);
222568 if( buf.nBuf==nNoop ){
222569 buf.nBuf = nRewind;
222570 }
222571 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
222572 }
222573 }
222574
222575 if( rc==SQLITE_OK ){
222576 if( xOutput==0 ){
222577 *pnChangeset = buf.nBuf;
222578 *ppChangeset = buf.aBuf;
222579 buf.aBuf = 0;
222580 }else if( buf.nBuf>0 ){
222581 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
222582 }
222583 }
222584
222585 sqlite3_free(buf.aBuf);
222586 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
222587 sqlite3_mutex_leave(sqlite3_db_mutex(db));
222588 return rc;
222589}
222590
222591/*
222592** Obtain a changeset object containing all changes recorded by the
222593** session object passed as the first argument.
222594**
222595** It is the responsibility of the caller to eventually free the buffer
222596** using sqlite3_free().
222597*/
222598SQLITE_API int sqlite3session_changeset(
222599 sqlite3_session *pSession, /* Session object */
222600 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
222601 void **ppChangeset /* OUT: Buffer containing changeset */
222602){
222603 int rc;
222604
222605 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE;
222606 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
222607 assert( rc || pnChangeset==0
222608 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize
222609 );
222610 return rc;
222611}
222612
222613/*
222614** Streaming version of sqlite3session_changeset().
222615*/
222616SQLITE_API int sqlite3session_changeset_strm(
222617 sqlite3_session *pSession,
222618 int (*xOutput)(void *pOut, const void *pData, int nData),
222619 void *pOut
222620){
222621 if( xOutput==0 ) return SQLITE_MISUSE;
222622 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
222623}
222624
222625/*
222626** Streaming version of sqlite3session_patchset().
222627*/
222628SQLITE_API int sqlite3session_patchset_strm(
222629 sqlite3_session *pSession,
222630 int (*xOutput)(void *pOut, const void *pData, int nData),
222631 void *pOut
222632){
222633 if( xOutput==0 ) return SQLITE_MISUSE;
222634 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
222635}
222636
222637/*
222638** Obtain a patchset object containing all changes recorded by the
222639** session object passed as the first argument.
222640**
222641** It is the responsibility of the caller to eventually free the buffer
222642** using sqlite3_free().
222643*/
222644SQLITE_API int sqlite3session_patchset(
222645 sqlite3_session *pSession, /* Session object */
222646 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
222647 void **ppPatchset /* OUT: Buffer containing changeset */
222648){
222649 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE;
222650 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
222651}
222652
222653/*
222654** Enable or disable the session object passed as the first argument.
222655*/
222656SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
222657 int ret;
222658 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
222659 if( bEnable>=0 ){
222660 pSession->bEnable = bEnable;
222661 }
222662 ret = pSession->bEnable;
222663 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
222664 return ret;
222665}
222666
222667/*
222668** Enable or disable the session object passed as the first argument.
222669*/
222670SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
222671 int ret;
222672 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
222673 if( bIndirect>=0 ){
222674 pSession->bIndirect = bIndirect;
222675 }
222676 ret = pSession->bIndirect;
222677 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
222678 return ret;
222679}
222680
222681/*
222682** Return true if there have been no changes to monitored tables recorded
222683** by the session object passed as the only argument.
222684*/
222685SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
222686 int ret = 0;
222687 SessionTable *pTab;
222688
222689 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
222690 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
222691 ret = (pTab->nEntry>0);
222692 }
222693 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
222694
222695 return (ret==0);
222696}
222697
222698/*
222699** Return the amount of heap memory in use.
222700*/
222701SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
222702 return pSession->nMalloc;
222703}
222704
222705/*
222706** Configure the session object passed as the first argument.
222707*/
222708SQLITE_API int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
222709 int rc = SQLITE_OK;
222710 switch( op ){
222711 case SQLITE_SESSION_OBJCONFIG_SIZE: {
222712 int iArg = *(int*)pArg;
222713 if( iArg>=0 ){
222714 if( pSession->pTable ){
222715 rc = SQLITE_MISUSE;
222716 }else{
222717 pSession->bEnableSize = (iArg!=0);
222718 }
222719 }
222720 *(int*)pArg = pSession->bEnableSize;
222721 break;
222722 }
222723
222724 case SQLITE_SESSION_OBJCONFIG_ROWID: {
222725 int iArg = *(int*)pArg;
222726 if( iArg>=0 ){
222727 if( pSession->pTable ){
222728 rc = SQLITE_MISUSE;
222729 }else{
222730 pSession->bImplicitPK = (iArg!=0);
222731 }
222732 }
222733 *(int*)pArg = pSession->bImplicitPK;
222734 break;
222735 }
222736
222737 default:
222738 rc = SQLITE_MISUSE;
222739 }
222740
222741 return rc;
222742}
222743
222744/*
222745** Return the maximum size of sqlite3session_changeset() output.
222746*/
222747SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
222748 return pSession->nMaxChangesetSize;
222749}
222750
222751/*
222752** Do the work for either sqlite3changeset_start() or start_strm().
222753*/
222754static int sessionChangesetStart(
222755 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
222756 int (*xInput)(void *pIn, void *pData, int *pnData),
222757 void *pIn,
222758 int nChangeset, /* Size of buffer pChangeset in bytes */
222759 void *pChangeset, /* Pointer to buffer containing changeset */
222760 int bInvert, /* True to invert changeset */
222761 int bSkipEmpty /* True to skip empty UPDATE changes */
222762){
222763 sqlite3_changeset_iter *pRet; /* Iterator to return */
222764 int nByte; /* Number of bytes to allocate for iterator */
222765
222766 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
222767
222768 /* Zero the output variable in case an error occurs. */
222769 *pp = 0;
222770
222771 /* Allocate and initialize the iterator structure. */
222772 nByte = sizeof(sqlite3_changeset_iter);
222773 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
222774 if( !pRet ) return SQLITE_NOMEM;
222775 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
222776 pRet->in.aData = (u8 *)pChangeset;
222777 pRet->in.nData = nChangeset;
222778 pRet->in.xInput = xInput;
222779 pRet->in.pIn = pIn;
222780 pRet->in.bEof = (xInput ? 0 : 1);
222781 pRet->bInvert = bInvert;
222782 pRet->bSkipEmpty = bSkipEmpty;
222783
222784 /* Populate the output variable and return success. */
222785 *pp = pRet;
222786 return SQLITE_OK;
222787}
222788
222789/*
222790** Create an iterator used to iterate through the contents of a changeset.
222791*/
222792SQLITE_API int sqlite3changeset_start(
222793 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
222794 int nChangeset, /* Size of buffer pChangeset in bytes */
222795 void *pChangeset /* Pointer to buffer containing changeset */
222796){
222797 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
222798}
222799SQLITE_API int sqlite3changeset_start_v2(
222800 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
222801 int nChangeset, /* Size of buffer pChangeset in bytes */
222802 void *pChangeset, /* Pointer to buffer containing changeset */
222803 int flags
222804){
222805 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
222806 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
222807}
222808
222809/*
222810** Streaming version of sqlite3changeset_start().
222811*/
222812SQLITE_API int sqlite3changeset_start_strm(
222813 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
222814 int (*xInput)(void *pIn, void *pData, int *pnData),
222815 void *pIn
222816){
222817 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
222818}
222819SQLITE_API int sqlite3changeset_start_v2_strm(
222820 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
222821 int (*xInput)(void *pIn, void *pData, int *pnData),
222822 void *pIn,
222823 int flags
222824){
222825 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
222826 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
222827}
222828
222829/*
222830** If the SessionInput object passed as the only argument is a streaming
222831** object and the buffer is full, discard some data to free up space.
222832*/
222833static void sessionDiscardData(SessionInput *pIn){
222834 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
222835 int nMove = pIn->buf.nBuf - pIn->iNext;
222836 assert( nMove>=0 );
222837 if( nMove>0 ){
222838 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
222839 }
222840 pIn->buf.nBuf -= pIn->iNext;
222841 pIn->iNext = 0;
222842 pIn->nData = pIn->buf.nBuf;
222843 }
222844}
222845
222846/*
222847** Ensure that there are at least nByte bytes available in the buffer. Or,
222848** if there are not nByte bytes remaining in the input, that all available
222849** data is in the buffer.
222850**
222851** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
222852*/
222853static int sessionInputBuffer(SessionInput *pIn, int nByte){
222854 int rc = SQLITE_OK;
222855 if( pIn->xInput ){
222856 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
222857 int nNew = sessions_strm_chunk_size;
222858
222859 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
222860 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
222861 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
222862 if( nNew==0 ){
222863 pIn->bEof = 1;
222864 }else{
222865 pIn->buf.nBuf += nNew;
222866 }
222867 }
222868
222869 pIn->aData = pIn->buf.aBuf;
222870 pIn->nData = pIn->buf.nBuf;
222871 }
222872 }
222873 return rc;
222874}
222875
222876/*
222877** When this function is called, *ppRec points to the start of a record
222878** that contains nCol values. This function advances the pointer *ppRec
222879** until it points to the byte immediately following that record.
222880*/
222881static void sessionSkipRecord(
222882 u8 **ppRec, /* IN/OUT: Record pointer */
222883 int nCol /* Number of values in record */
222884){
222885 u8 *aRec = *ppRec;
222886 int i;
222887 for(i=0; i<nCol; i++){
222888 int eType = *aRec++;
222889 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
222890 int nByte;
222891 aRec += sessionVarintGet((u8*)aRec, &nByte);
222892 aRec += nByte;
222893 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
222894 aRec += 8;
222895 }
222896 }
222897
222898 *ppRec = aRec;
222899}
222900
222901/*
222902** This function sets the value of the sqlite3_value object passed as the
222903** first argument to a copy of the string or blob held in the aData[]
222904** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
222905** error occurs.
222906*/
222907static int sessionValueSetStr(
222908 sqlite3_value *pVal, /* Set the value of this object */
222909 u8 *aData, /* Buffer containing string or blob data */
222910 int nData, /* Size of buffer aData[] in bytes */
222911 u8 enc /* String encoding (0 for blobs) */
222912){
222913 /* In theory this code could just pass SQLITE_TRANSIENT as the final
222914 ** argument to sqlite3ValueSetStr() and have the copy created
222915 ** automatically. But doing so makes it difficult to detect any OOM
222916 ** error. Hence the code to create the copy externally. */
222917 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
222918 if( aCopy==0 ) return SQLITE_NOMEM;
222919 memcpy(aCopy, aData, nData);
222920 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
222921 return SQLITE_OK;
222922}
222923
222924/*
222925** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
222926** for details.
222927**
222928** When this function is called, *paChange points to the start of the record
222929** to deserialize. Assuming no error occurs, *paChange is set to point to
222930** one byte after the end of the same record before this function returns.
222931** If the argument abPK is NULL, then the record contains nCol values. Or,
222932** if abPK is other than NULL, then the record contains only the PK fields
222933** (in other words, it is a patchset DELETE record).
222934**
222935** If successful, each element of the apOut[] array (allocated by the caller)
222936** is set to point to an sqlite3_value object containing the value read
222937** from the corresponding position in the record. If that value is not
222938** included in the record (i.e. because the record is part of an UPDATE change
222939** and the field was not modified), the corresponding element of apOut[] is
222940** set to NULL.
222941**
222942** It is the responsibility of the caller to free all sqlite_value structures
222943** using sqlite3_free().
222944**
222945** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
222946** The apOut[] array may have been partially populated in this case.
222947*/
222948static int sessionReadRecord(
222949 SessionInput *pIn, /* Input data */
222950 int nCol, /* Number of values in record */
222951 u8 *abPK, /* Array of primary key flags, or NULL */
222952 sqlite3_value **apOut, /* Write values to this array */
222953 int *pbEmpty
222954){
222955 int i; /* Used to iterate through columns */
222956 int rc = SQLITE_OK;
222957
222958 assert( pbEmpty==0 || *pbEmpty==0 );
222959 if( pbEmpty ) *pbEmpty = 1;
222960 for(i=0; i<nCol && rc==SQLITE_OK; i++){
222961 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
222962 if( abPK && abPK[i]==0 ) continue;
222963 rc = sessionInputBuffer(pIn, 9);
222964 if( rc==SQLITE_OK ){
222965 if( pIn->iNext>=pIn->nData ){
222966 rc = SQLITE_CORRUPT_BKPT;
222967 }else{
222968 eType = pIn->aData[pIn->iNext++];
222969 assert( apOut[i]==0 );
222970 if( eType ){
222971 if( pbEmpty ) *pbEmpty = 0;
222972 apOut[i] = sqlite3ValueNew(0);
222973 if( !apOut[i] ) rc = SQLITE_NOMEM;
222974 }
222975 }
222976 }
222977
222978 if( rc==SQLITE_OK ){
222979 u8 *aVal = &pIn->aData[pIn->iNext];
222980 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
222981 int nByte;
222982 pIn->iNext += sessionVarintGet(aVal, &nByte);
222983 rc = sessionInputBuffer(pIn, nByte);
222984 if( rc==SQLITE_OK ){
222985 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
222986 rc = SQLITE_CORRUPT_BKPT;
222987 }else{
222988 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
222989 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
222990 pIn->iNext += nByte;
222991 }
222992 }
222993 }
222994 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
222995 if( (pIn->nData-pIn->iNext)<8 ){
222996 rc = SQLITE_CORRUPT_BKPT;
222997 }else{
222998 sqlite3_int64 v = sessionGetI64(aVal);
222999 if( eType==SQLITE_INTEGER ){
223000 sqlite3VdbeMemSetInt64(apOut[i], v);
223001 }else{
223002 double d;
223003 memcpy(&d, &v, 8);
223004 sqlite3VdbeMemSetDouble(apOut[i], d);
223005 }
223006 pIn->iNext += 8;
223007 }
223008 }
223009 }
223010 }
223011
223012 return rc;
223013}
223014
223015/*
223016** The input pointer currently points to the second byte of a table-header.
223017** Specifically, to the following:
223018**
223019** + number of columns in table (varint)
223020** + array of PK flags (1 byte per column),
223021** + table name (nul terminated).
223022**
223023** This function ensures that all of the above is present in the input
223024** buffer (i.e. that it can be accessed without any calls to xInput()).
223025** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
223026** The input pointer is not moved.
223027*/
223028static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
223029 int rc = SQLITE_OK;
223030 int nCol = 0;
223031 int nRead = 0;
223032
223033 rc = sessionInputBuffer(pIn, 9);
223034 if( rc==SQLITE_OK ){
223035 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
223036 /* The hard upper limit for the number of columns in an SQLite
223037 ** database table is, according to sqliteLimit.h, 32676. So
223038 ** consider any table-header that purports to have more than 65536
223039 ** columns to be corrupt. This is convenient because otherwise,
223040 ** if the (nCol>65536) condition below were omitted, a sufficiently
223041 ** large value for nCol may cause nRead to wrap around and become
223042 ** negative. Leading to a crash. */
223043 if( nCol<0 || nCol>65536 ){
223044 rc = SQLITE_CORRUPT_BKPT;
223045 }else{
223046 rc = sessionInputBuffer(pIn, nRead+nCol+100);
223047 nRead += nCol;
223048 }
223049 }
223050
223051 while( rc==SQLITE_OK ){
223052 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
223053 nRead++;
223054 }
223055 if( (pIn->iNext + nRead)<pIn->nData ) break;
223056 rc = sessionInputBuffer(pIn, nRead + 100);
223057 }
223058 *pnByte = nRead+1;
223059 return rc;
223060}
223061
223062/*
223063** The input pointer currently points to the first byte of the first field
223064** of a record consisting of nCol columns. This function ensures the entire
223065** record is buffered. It does not move the input pointer.
223066**
223067** If successful, SQLITE_OK is returned and *pnByte is set to the size of
223068** the record in bytes. Otherwise, an SQLite error code is returned. The
223069** final value of *pnByte is undefined in this case.
223070*/
223071static int sessionChangesetBufferRecord(
223072 SessionInput *pIn, /* Input data */
223073 int nCol, /* Number of columns in record */
223074 int *pnByte /* OUT: Size of record in bytes */
223075){
223076 int rc = SQLITE_OK;
223077 int nByte = 0;
223078 int i;
223079 for(i=0; rc==SQLITE_OK && i<nCol; i++){
223080 int eType;
223081 rc = sessionInputBuffer(pIn, nByte + 10);
223082 if( rc==SQLITE_OK ){
223083 eType = pIn->aData[pIn->iNext + nByte++];
223084 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
223085 int n;
223086 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
223087 nByte += n;
223088 rc = sessionInputBuffer(pIn, nByte);
223089 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
223090 nByte += 8;
223091 }
223092 }
223093 }
223094 *pnByte = nByte;
223095 return rc;
223096}
223097
223098/*
223099** The input pointer currently points to the second byte of a table-header.
223100** Specifically, to the following:
223101**
223102** + number of columns in table (varint)
223103** + array of PK flags (1 byte per column),
223104** + table name (nul terminated).
223105**
223106** This function decodes the table-header and populates the p->nCol,
223107** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
223108** also allocated or resized according to the new value of p->nCol. The
223109** input pointer is left pointing to the byte following the table header.
223110**
223111** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
223112** is returned and the final values of the various fields enumerated above
223113** are undefined.
223114*/
223115static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
223116 int rc;
223117 int nCopy;
223118 assert( p->rc==SQLITE_OK );
223119
223120 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
223121 if( rc==SQLITE_OK ){
223122 int nByte;
223123 int nVarint;
223124 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
223125 if( p->nCol>0 ){
223126 nCopy -= nVarint;
223127 p->in.iNext += nVarint;
223128 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
223129 p->tblhdr.nBuf = 0;
223130 sessionBufferGrow(&p->tblhdr, nByte, &rc);
223131 }else{
223132 rc = SQLITE_CORRUPT_BKPT;
223133 }
223134 }
223135
223136 if( rc==SQLITE_OK ){
223137 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
223138 memset(p->tblhdr.aBuf, 0, iPK);
223139 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
223140 p->in.iNext += nCopy;
223141 }
223142
223143 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
223144 if( p->apValue==0 ){
223145 p->abPK = 0;
223146 p->zTab = 0;
223147 }else{
223148 p->abPK = (u8*)&p->apValue[p->nCol*2];
223149 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
223150 }
223151 return (p->rc = rc);
223152}
223153
223154/*
223155** Advance the changeset iterator to the next change. The differences between
223156** this function and sessionChangesetNext() are that
223157**
223158** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
223159** that modifies no columns), this function sets (*pbEmpty) to 1.
223160**
223161** * If the iterator is configured to skip no-op UPDATEs,
223162** sessionChangesetNext() does that. This function does not.
223163*/
223164static int sessionChangesetNextOne(
223165 sqlite3_changeset_iter *p, /* Changeset iterator */
223166 u8 **paRec, /* If non-NULL, store record pointer here */
223167 int *pnRec, /* If non-NULL, store size of record here */
223168 int *pbNew, /* If non-NULL, true if new table */
223169 int *pbEmpty
223170){
223171 int i;
223172 u8 op;
223173
223174 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
223175 assert( pbEmpty==0 || *pbEmpty==0 );
223176
223177 /* If the iterator is in the error-state, return immediately. */
223178 if( p->rc!=SQLITE_OK ) return p->rc;
223179
223180 /* Free the current contents of p->apValue[], if any. */
223181 if( p->apValue ){
223182 for(i=0; i<p->nCol*2; i++){
223183 sqlite3ValueFree(p->apValue[i]);
223184 }
223185 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
223186 }
223187
223188 /* Make sure the buffer contains at least 10 bytes of input data, or all
223189 ** remaining data if there are less than 10 bytes available. This is
223190 ** sufficient either for the 'T' or 'P' byte and the varint that follows
223191 ** it, or for the two single byte values otherwise. */
223192 p->rc = sessionInputBuffer(&p->in, 2);
223193 if( p->rc!=SQLITE_OK ) return p->rc;
223194
223195 /* If the iterator is already at the end of the changeset, return DONE. */
223196 if( p->in.iNext>=p->in.nData ){
223197 return SQLITE_DONE;
223198 }
223199
223200 sessionDiscardData(&p->in);
223201 p->in.iCurrent = p->in.iNext;
223202
223203 op = p->in.aData[p->in.iNext++];
223204 while( op=='T' || op=='P' ){
223205 if( pbNew ) *pbNew = 1;
223206 p->bPatchset = (op=='P');
223207 if( sessionChangesetReadTblhdr(p) ) return p->rc;
223208 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
223209 p->in.iCurrent = p->in.iNext;
223210 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
223211 op = p->in.aData[p->in.iNext++];
223212 }
223213
223214 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
223215 /* The first record in the changeset is not a table header. Must be a
223216 ** corrupt changeset. */
223217 assert( p->in.iNext==1 || p->zTab );
223218 return (p->rc = SQLITE_CORRUPT_BKPT);
223219 }
223220
223221 p->op = op;
223222 p->bIndirect = p->in.aData[p->in.iNext++];
223223 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
223224 return (p->rc = SQLITE_CORRUPT_BKPT);
223225 }
223226
223227 if( paRec ){
223228 int nVal; /* Number of values to buffer */
223229 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
223230 nVal = p->nCol * 2;
223231 }else if( p->bPatchset && op==SQLITE_DELETE ){
223232 nVal = 0;
223233 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
223234 }else{
223235 nVal = p->nCol;
223236 }
223237 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
223238 if( p->rc!=SQLITE_OK ) return p->rc;
223239 *paRec = &p->in.aData[p->in.iNext];
223240 p->in.iNext += *pnRec;
223241 }else{
223242 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
223243 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
223244
223245 /* If this is an UPDATE or DELETE, read the old.* record. */
223246 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
223247 u8 *abPK = p->bPatchset ? p->abPK : 0;
223248 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
223249 if( p->rc!=SQLITE_OK ) return p->rc;
223250 }
223251
223252 /* If this is an INSERT or UPDATE, read the new.* record. */
223253 if( p->op!=SQLITE_DELETE ){
223254 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
223255 if( p->rc!=SQLITE_OK ) return p->rc;
223256 }
223257
223258 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
223259 /* If this is an UPDATE that is part of a patchset, then all PK and
223260 ** modified fields are present in the new.* record. The old.* record
223261 ** is currently completely empty. This block shifts the PK fields from
223262 ** new.* to old.*, to accommodate the code that reads these arrays. */
223263 for(i=0; i<p->nCol; i++){
223264 assert( p->bPatchset==0 || p->apValue[i]==0 );
223265 if( p->abPK[i] ){
223266 assert( p->apValue[i]==0 );
223267 p->apValue[i] = p->apValue[i+p->nCol];
223268 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
223269 p->apValue[i+p->nCol] = 0;
223270 }
223271 }
223272 }else if( p->bInvert ){
223273 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
223274 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
223275 }
223276
223277 /* If this is an UPDATE that is part of a changeset, then check that
223278 ** there are no fields in the old.* record that are not (a) PK fields,
223279 ** or (b) also present in the new.* record.
223280 **
223281 ** Such records are technically corrupt, but the rebaser was at one
223282 ** point generating them. Under most circumstances this is benign, but
223283 ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
223284 if( p->bPatchset==0 && p->op==SQLITE_UPDATE){
223285 for(i=0; i<p->nCol; i++){
223286 if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){
223287 sqlite3ValueFree(p->apValue[i]);
223288 p->apValue[i] = 0;
223289 }
223290 }
223291 }
223292 }
223293
223294 return SQLITE_ROW;
223295}
223296
223297/*
223298** Advance the changeset iterator to the next change.
223299**
223300** If both paRec and pnRec are NULL, then this function works like the public
223301** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
223302** sqlite3changeset_new() and old() APIs may be used to query for values.
223303**
223304** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
223305** record is written to *paRec before returning and the number of bytes in
223306** the record to *pnRec.
223307**
223308** Either way, this function returns SQLITE_ROW if the iterator is
223309** successfully advanced to the next change in the changeset, an SQLite
223310** error code if an error occurs, or SQLITE_DONE if there are no further
223311** changes in the changeset.
223312*/
223313static int sessionChangesetNext(
223314 sqlite3_changeset_iter *p, /* Changeset iterator */
223315 u8 **paRec, /* If non-NULL, store record pointer here */
223316 int *pnRec, /* If non-NULL, store size of record here */
223317 int *pbNew /* If non-NULL, true if new table */
223318){
223319 int bEmpty;
223320 int rc;
223321 do {
223322 bEmpty = 0;
223323 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
223324 }while( rc==SQLITE_ROW && p->bSkipEmpty && bEmpty);
223325 return rc;
223326}
223327
223328/*
223329** Advance an iterator created by sqlite3changeset_start() to the next
223330** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
223331** or SQLITE_CORRUPT.
223332**
223333** This function may not be called on iterators passed to a conflict handler
223334** callback by changeset_apply().
223335*/
223336SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
223337 return sessionChangesetNext(p, 0, 0, 0);
223338}
223339
223340/*
223341** The following function extracts information on the current change
223342** from a changeset iterator. It may only be called after changeset_next()
223343** has returned SQLITE_ROW.
223344*/
223345SQLITE_API int sqlite3changeset_op(
223346 sqlite3_changeset_iter *pIter, /* Iterator handle */
223347 const char **pzTab, /* OUT: Pointer to table name */
223348 int *pnCol, /* OUT: Number of columns in table */
223349 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
223350 int *pbIndirect /* OUT: True if change is indirect */
223351){
223352 *pOp = pIter->op;
223353 *pnCol = pIter->nCol;
223354 *pzTab = pIter->zTab;
223355 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
223356 return SQLITE_OK;
223357}
223358
223359/*
223360** Return information regarding the PRIMARY KEY and number of columns in
223361** the database table affected by the change that pIter currently points
223362** to. This function may only be called after changeset_next() returns
223363** SQLITE_ROW.
223364*/
223365SQLITE_API int sqlite3changeset_pk(
223366 sqlite3_changeset_iter *pIter, /* Iterator object */
223367 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
223368 int *pnCol /* OUT: Number of entries in output array */
223369){
223370 *pabPK = pIter->abPK;
223371 if( pnCol ) *pnCol = pIter->nCol;
223372 return SQLITE_OK;
223373}
223374
223375/*
223376** This function may only be called while the iterator is pointing to an
223377** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
223378** Otherwise, SQLITE_MISUSE is returned.
223379**
223380** It sets *ppValue to point to an sqlite3_value structure containing the
223381** iVal'th value in the old.* record. Or, if that particular value is not
223382** included in the record (because the change is an UPDATE and the field
223383** was not modified and is not a PK column), set *ppValue to NULL.
223384**
223385** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
223386** not modified. Otherwise, SQLITE_OK.
223387*/
223388SQLITE_API int sqlite3changeset_old(
223389 sqlite3_changeset_iter *pIter, /* Changeset iterator */
223390 int iVal, /* Index of old.* value to retrieve */
223391 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
223392){
223393 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
223394 return SQLITE_MISUSE;
223395 }
223396 if( iVal<0 || iVal>=pIter->nCol ){
223397 return SQLITE_RANGE;
223398 }
223399 *ppValue = pIter->apValue[iVal];
223400 return SQLITE_OK;
223401}
223402
223403/*
223404** This function may only be called while the iterator is pointing to an
223405** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
223406** Otherwise, SQLITE_MISUSE is returned.
223407**
223408** It sets *ppValue to point to an sqlite3_value structure containing the
223409** iVal'th value in the new.* record. Or, if that particular value is not
223410** included in the record (because the change is an UPDATE and the field
223411** was not modified), set *ppValue to NULL.
223412**
223413** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
223414** not modified. Otherwise, SQLITE_OK.
223415*/
223416SQLITE_API int sqlite3changeset_new(
223417 sqlite3_changeset_iter *pIter, /* Changeset iterator */
223418 int iVal, /* Index of new.* value to retrieve */
223419 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
223420){
223421 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
223422 return SQLITE_MISUSE;
223423 }
223424 if( iVal<0 || iVal>=pIter->nCol ){
223425 return SQLITE_RANGE;
223426 }
223427 *ppValue = pIter->apValue[pIter->nCol+iVal];
223428 return SQLITE_OK;
223429}
223430
223431/*
223432** The following two macros are used internally. They are similar to the
223433** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
223434** they omit all error checking and return a pointer to the requested value.
223435*/
223436#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
223437#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
223438
223439/*
223440** This function may only be called with a changeset iterator that has been
223441** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
223442** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
223443**
223444** If successful, *ppValue is set to point to an sqlite3_value structure
223445** containing the iVal'th value of the conflicting record.
223446**
223447** If value iVal is out-of-range or some other error occurs, an SQLite error
223448** code is returned. Otherwise, SQLITE_OK.
223449*/
223450SQLITE_API int sqlite3changeset_conflict(
223451 sqlite3_changeset_iter *pIter, /* Changeset iterator */
223452 int iVal, /* Index of conflict record value to fetch */
223453 sqlite3_value **ppValue /* OUT: Value from conflicting row */
223454){
223455 if( !pIter->pConflict ){
223456 return SQLITE_MISUSE;
223457 }
223458 if( iVal<0 || iVal>=pIter->nCol ){
223459 return SQLITE_RANGE;
223460 }
223461 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
223462 return SQLITE_OK;
223463}
223464
223465/*
223466** This function may only be called with an iterator passed to an
223467** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
223468** it sets the output variable to the total number of known foreign key
223469** violations in the destination database and returns SQLITE_OK.
223470**
223471** In all other cases this function returns SQLITE_MISUSE.
223472*/
223473SQLITE_API int sqlite3changeset_fk_conflicts(
223474 sqlite3_changeset_iter *pIter, /* Changeset iterator */
223475 int *pnOut /* OUT: Number of FK violations */
223476){
223477 if( pIter->pConflict || pIter->apValue ){
223478 return SQLITE_MISUSE;
223479 }
223480 *pnOut = pIter->nCol;
223481 return SQLITE_OK;
223482}
223483
223484
223485/*
223486** Finalize an iterator allocated with sqlite3changeset_start().
223487**
223488** This function may not be called on iterators passed to a conflict handler
223489** callback by changeset_apply().
223490*/
223491SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
223492 int rc = SQLITE_OK;
223493 if( p ){
223494 int i; /* Used to iterate through p->apValue[] */
223495 rc = p->rc;
223496 if( p->apValue ){
223497 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
223498 }
223499 sqlite3_free(p->tblhdr.aBuf);
223500 sqlite3_free(p->in.buf.aBuf);
223501 sqlite3_free(p);
223502 }
223503 return rc;
223504}
223505
223506static int sessionChangesetInvert(
223507 SessionInput *pInput, /* Input changeset */
223508 int (*xOutput)(void *pOut, const void *pData, int nData),
223509 void *pOut,
223510 int *pnInverted, /* OUT: Number of bytes in output changeset */
223511 void **ppInverted /* OUT: Inverse of pChangeset */
223512){
223513 int rc = SQLITE_OK; /* Return value */
223514 SessionBuffer sOut; /* Output buffer */
223515 int nCol = 0; /* Number of cols in current table */
223516 u8 *abPK = 0; /* PK array for current table */
223517 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
223518 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
223519
223520 /* Initialize the output buffer */
223521 memset(&sOut, 0, sizeof(SessionBuffer));
223522
223523 /* Zero the output variables in case an error occurs. */
223524 if( ppInverted ){
223525 *ppInverted = 0;
223526 *pnInverted = 0;
223527 }
223528
223529 while( 1 ){
223530 u8 eType;
223531
223532 /* Test for EOF. */
223533 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
223534 if( pInput->iNext>=pInput->nData ) break;
223535 eType = pInput->aData[pInput->iNext];
223536
223537 switch( eType ){
223538 case 'T': {
223539 /* A 'table' record consists of:
223540 **
223541 ** * A constant 'T' character,
223542 ** * Number of columns in said table (a varint),
223543 ** * An array of nCol bytes (sPK),
223544 ** * A nul-terminated table name.
223545 */
223546 int nByte;
223547 int nVar;
223548 pInput->iNext++;
223549 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
223550 goto finished_invert;
223551 }
223552 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
223553 sPK.nBuf = 0;
223554 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
223555 sessionAppendByte(&sOut, eType, &rc);
223556 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
223557 if( rc ) goto finished_invert;
223558
223559 pInput->iNext += nByte;
223560 sqlite3_free(apVal);
223561 apVal = 0;
223562 abPK = sPK.aBuf;
223563 break;
223564 }
223565
223566 case SQLITE_INSERT:
223567 case SQLITE_DELETE: {
223568 int nByte;
223569 int bIndirect = pInput->aData[pInput->iNext+1];
223570 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
223571 pInput->iNext += 2;
223572 assert( rc==SQLITE_OK );
223573 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
223574 sessionAppendByte(&sOut, eType2, &rc);
223575 sessionAppendByte(&sOut, bIndirect, &rc);
223576 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
223577 pInput->iNext += nByte;
223578 if( rc ) goto finished_invert;
223579 break;
223580 }
223581
223582 case SQLITE_UPDATE: {
223583 int iCol;
223584
223585 if( 0==apVal ){
223586 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
223587 if( 0==apVal ){
223588 rc = SQLITE_NOMEM;
223589 goto finished_invert;
223590 }
223591 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
223592 }
223593
223594 /* Write the header for the new UPDATE change. Same as the original. */
223595 sessionAppendByte(&sOut, eType, &rc);
223596 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
223597
223598 /* Read the old.* and new.* records for the update change. */
223599 pInput->iNext += 2;
223600 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
223601 if( rc==SQLITE_OK ){
223602 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
223603 }
223604
223605 /* Write the new old.* record. Consists of the PK columns from the
223606 ** original old.* record, and the other values from the original
223607 ** new.* record. */
223608 for(iCol=0; iCol<nCol; iCol++){
223609 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
223610 sessionAppendValue(&sOut, pVal, &rc);
223611 }
223612
223613 /* Write the new new.* record. Consists of a copy of all values
223614 ** from the original old.* record, except for the PK columns, which
223615 ** are set to "undefined". */
223616 for(iCol=0; iCol<nCol; iCol++){
223617 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
223618 sessionAppendValue(&sOut, pVal, &rc);
223619 }
223620
223621 for(iCol=0; iCol<nCol*2; iCol++){
223622 sqlite3ValueFree(apVal[iCol]);
223623 }
223624 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
223625 if( rc!=SQLITE_OK ){
223626 goto finished_invert;
223627 }
223628
223629 break;
223630 }
223631
223632 default:
223633 rc = SQLITE_CORRUPT_BKPT;
223634 goto finished_invert;
223635 }
223636
223637 assert( rc==SQLITE_OK );
223638 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
223639 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
223640 sOut.nBuf = 0;
223641 if( rc!=SQLITE_OK ) goto finished_invert;
223642 }
223643 }
223644
223645 assert( rc==SQLITE_OK );
223646 if( pnInverted && ALWAYS(ppInverted) ){
223647 *pnInverted = sOut.nBuf;
223648 *ppInverted = sOut.aBuf;
223649 sOut.aBuf = 0;
223650 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0) ){
223651 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
223652 }
223653
223654 finished_invert:
223655 sqlite3_free(sOut.aBuf);
223656 sqlite3_free(apVal);
223657 sqlite3_free(sPK.aBuf);
223658 return rc;
223659}
223660
223661
223662/*
223663** Invert a changeset object.
223664*/
223665SQLITE_API int sqlite3changeset_invert(
223666 int nChangeset, /* Number of bytes in input */
223667 const void *pChangeset, /* Input changeset */
223668 int *pnInverted, /* OUT: Number of bytes in output changeset */
223669 void **ppInverted /* OUT: Inverse of pChangeset */
223670){
223671 SessionInput sInput;
223672
223673 /* Set up the input stream */
223674 memset(&sInput, 0, sizeof(SessionInput));
223675 sInput.nData = nChangeset;
223676 sInput.aData = (u8*)pChangeset;
223677
223678 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
223679}
223680
223681/*
223682** Streaming version of sqlite3changeset_invert().
223683*/
223684SQLITE_API int sqlite3changeset_invert_strm(
223685 int (*xInput)(void *pIn, void *pData, int *pnData),
223686 void *pIn,
223687 int (*xOutput)(void *pOut, const void *pData, int nData),
223688 void *pOut
223689){
223690 SessionInput sInput;
223691 int rc;
223692
223693 /* Set up the input stream */
223694 memset(&sInput, 0, sizeof(SessionInput));
223695 sInput.xInput = xInput;
223696 sInput.pIn = pIn;
223697
223698 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
223699 sqlite3_free(sInput.buf.aBuf);
223700 return rc;
223701}
223702
223703
223704typedef struct SessionUpdate SessionUpdate;
223705struct SessionUpdate {
223706 sqlite3_stmt *pStmt;
223707 u32 *aMask;
223708 SessionUpdate *pNext;
223709};
223710
223711typedef struct SessionApplyCtx SessionApplyCtx;
223712struct SessionApplyCtx {
223713 sqlite3 *db;
223714 sqlite3_stmt *pDelete; /* DELETE statement */
223715 sqlite3_stmt *pInsert; /* INSERT statement */
223716 sqlite3_stmt *pSelect; /* SELECT statement */
223717 int nCol; /* Size of azCol[] and abPK[] arrays */
223718 const char **azCol; /* Array of column names */
223719 u8 *abPK; /* Boolean array - true if column is in PK */
223720 u32 *aUpdateMask; /* Used by sessionUpdateFind */
223721 SessionUpdate *pUp;
223722 int bStat1; /* True if table is sqlite_stat1 */
223723 int bDeferConstraints; /* True to defer constraints */
223724 int bInvertConstraints; /* Invert when iterating constraints buffer */
223725 SessionBuffer constraints; /* Deferred constraints are stored here */
223726 SessionBuffer rebase; /* Rebase information (if any) here */
223727 u8 bRebaseStarted; /* If table header is already in rebase */
223728 u8 bRebase; /* True to collect rebase information */
223729 u8 bIgnoreNoop; /* True to ignore no-op conflicts */
223730 int bRowid;
223731};
223732
223733/* Number of prepared UPDATE statements to cache. */
223734#define SESSION_UPDATE_CACHE_SZ 12
223735
223736/*
223737** Find a prepared UPDATE statement suitable for the UPDATE step currently
223738** being visited by the iterator. The UPDATE is of the form:
223739**
223740** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
223741*/
223742static int sessionUpdateFind(
223743 sqlite3_changeset_iter *pIter,
223744 SessionApplyCtx *p,
223745 int bPatchset,
223746 sqlite3_stmt **ppStmt
223747){
223748 int rc = SQLITE_OK;
223749 SessionUpdate *pUp = 0;
223750 int nCol = pIter->nCol;
223751 int nU32 = (pIter->nCol+33)/32;
223752 int ii;
223753
223754 if( p->aUpdateMask==0 ){
223755 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
223756 if( p->aUpdateMask==0 ){
223757 rc = SQLITE_NOMEM;
223758 }
223759 }
223760
223761 if( rc==SQLITE_OK ){
223762 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
223763 rc = SQLITE_CORRUPT;
223764 for(ii=0; ii<pIter->nCol; ii++){
223765 if( sessionChangesetNew(pIter, ii) ){
223766 p->aUpdateMask[ii/32] |= (1<<(ii%32));
223767 rc = SQLITE_OK;
223768 }
223769 }
223770 }
223771
223772 if( rc==SQLITE_OK ){
223773 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
223774
223775 if( p->pUp ){
223776 int nUp = 0;
223777 SessionUpdate **pp = &p->pUp;
223778 while( 1 ){
223779 nUp++;
223780 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
223781 pUp = *pp;
223782 *pp = pUp->pNext;
223783 pUp->pNext = p->pUp;
223784 p->pUp = pUp;
223785 break;
223786 }
223787
223788 if( (*pp)->pNext ){
223789 pp = &(*pp)->pNext;
223790 }else{
223791 if( nUp>=SESSION_UPDATE_CACHE_SZ ){
223792 sqlite3_finalize((*pp)->pStmt);
223793 sqlite3_free(*pp);
223794 *pp = 0;
223795 }
223796 break;
223797 }
223798 }
223799 }
223800
223801 if( pUp==0 ){
223802 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
223803 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
223804 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
223805 if( pUp==0 ){
223806 rc = SQLITE_NOMEM;
223807 }else{
223808 const char *zSep = "";
223809 SessionBuffer buf;
223810
223811 memset(&buf, 0, sizeof(buf));
223812 pUp->aMask = (u32*)&pUp[1];
223813 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
223814
223815 sessionAppendStr(&buf, "UPDATE main.", &rc);
223816 sessionAppendIdent(&buf, pIter->zTab, &rc);
223817 sessionAppendStr(&buf, " SET ", &rc);
223818
223819 /* Create the assignments part of the UPDATE */
223820 for(ii=0; ii<pIter->nCol; ii++){
223821 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii) ){
223822 sessionAppendStr(&buf, zSep, &rc);
223823 sessionAppendIdent(&buf, p->azCol[ii], &rc);
223824 sessionAppendStr(&buf, " = ?", &rc);
223825 sessionAppendInteger(&buf, ii*2+1, &rc);
223826 zSep = ", ";
223827 }
223828 }
223829
223830 /* Create the WHERE clause part of the UPDATE */
223831 zSep = "";
223832 sessionAppendStr(&buf, " WHERE ", &rc);
223833 for(ii=0; ii<pIter->nCol; ii++){
223834 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)) ){
223835 sessionAppendStr(&buf, zSep, &rc);
223836 if( bStat1 && ii==1 ){
223837 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 );
223838 sessionAppendStr(&buf,
223839 "idx IS CASE "
223840 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
223841 "ELSE ?4 END ", &rc
223842 );
223843 }else{
223844 sessionAppendIdent(&buf, p->azCol[ii], &rc);
223845 sessionAppendStr(&buf, " IS ?", &rc);
223846 sessionAppendInteger(&buf, ii*2+2, &rc);
223847 }
223848 zSep = " AND ";
223849 }
223850 }
223851
223852 if( rc==SQLITE_OK ){
223853 char *zSql = (char*)buf.aBuf;
223854 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
223855 }
223856
223857 if( rc!=SQLITE_OK ){
223858 sqlite3_free(pUp);
223859 pUp = 0;
223860 }else{
223861 pUp->pNext = p->pUp;
223862 p->pUp = pUp;
223863 }
223864 sqlite3_free(buf.aBuf);
223865 }
223866 }
223867 }
223868
223869 assert( (rc==SQLITE_OK)==(pUp!=0) );
223870 if( pUp ){
223871 *ppStmt = pUp->pStmt;
223872 }else{
223873 *ppStmt = 0;
223874 }
223875 return rc;
223876}
223877
223878/*
223879** Free all cached UPDATE statements.
223880*/
223881static void sessionUpdateFree(SessionApplyCtx *p){
223882 SessionUpdate *pUp;
223883 SessionUpdate *pNext;
223884 for(pUp=p->pUp; pUp; pUp=pNext){
223885 pNext = pUp->pNext;
223886 sqlite3_finalize(pUp->pStmt);
223887 sqlite3_free(pUp);
223888 }
223889 p->pUp = 0;
223890 sqlite3_free(p->aUpdateMask);
223891 p->aUpdateMask = 0;
223892}
223893
223894/*
223895** Formulate a statement to DELETE a row from database db. Assuming a table
223896** structure like this:
223897**
223898** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
223899**
223900** The DELETE statement looks like this:
223901**
223902** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
223903**
223904** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
223905** matching b and d values, or 1 otherwise. The second case comes up if the
223906** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
223907**
223908** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
223909** pointing to the prepared version of the SQL statement.
223910*/
223911static int sessionDeleteRow(
223912 sqlite3 *db, /* Database handle */
223913 const char *zTab, /* Table name */
223914 SessionApplyCtx *p /* Session changeset-apply context */
223915){
223916 int i;
223917 const char *zSep = "";
223918 int rc = SQLITE_OK;
223919 SessionBuffer buf = {0, 0, 0};
223920 int nPk = 0;
223921
223922 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
223923 sessionAppendIdent(&buf, zTab, &rc);
223924 sessionAppendStr(&buf, " WHERE ", &rc);
223925
223926 for(i=0; i<p->nCol; i++){
223927 if( p->abPK[i] ){
223928 nPk++;
223929 sessionAppendStr(&buf, zSep, &rc);
223930 sessionAppendIdent(&buf, p->azCol[i], &rc);
223931 sessionAppendStr(&buf, " = ?", &rc);
223932 sessionAppendInteger(&buf, i+1, &rc);
223933 zSep = " AND ";
223934 }
223935 }
223936
223937 if( nPk<p->nCol ){
223938 sessionAppendStr(&buf, " AND (?", &rc);
223939 sessionAppendInteger(&buf, p->nCol+1, &rc);
223940 sessionAppendStr(&buf, " OR ", &rc);
223941
223942 zSep = "";
223943 for(i=0; i<p->nCol; i++){
223944 if( !p->abPK[i] ){
223945 sessionAppendStr(&buf, zSep, &rc);
223946 sessionAppendIdent(&buf, p->azCol[i], &rc);
223947 sessionAppendStr(&buf, " IS ?", &rc);
223948 sessionAppendInteger(&buf, i+1, &rc);
223949 zSep = "AND ";
223950 }
223951 }
223952 sessionAppendStr(&buf, ")", &rc);
223953 }
223954
223955 if( rc==SQLITE_OK ){
223956 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
223957 }
223958 sqlite3_free(buf.aBuf);
223959
223960 return rc;
223961}
223962
223963/*
223964** Formulate and prepare an SQL statement to query table zTab by primary
223965** key. Assuming the following table structure:
223966**
223967** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
223968**
223969** The SELECT statement looks like this:
223970**
223971** SELECT * FROM x WHERE a = ?1 AND c = ?3
223972**
223973** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
223974** pointing to the prepared version of the SQL statement.
223975*/
223976static int sessionSelectRow(
223977 sqlite3 *db, /* Database handle */
223978 const char *zTab, /* Table name */
223979 SessionApplyCtx *p /* Session changeset-apply context */
223980){
223981 /* TODO */
223982 return sessionSelectStmt(db, p->bIgnoreNoop,
223983 "main", zTab, p->bRowid, p->nCol, p->azCol, p->abPK, &p->pSelect
223984 );
223985}
223986
223987/*
223988** Formulate and prepare an INSERT statement to add a record to table zTab.
223989** For example:
223990**
223991** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
223992**
223993** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
223994** pointing to the prepared version of the SQL statement.
223995*/
223996static int sessionInsertRow(
223997 sqlite3 *db, /* Database handle */
223998 const char *zTab, /* Table name */
223999 SessionApplyCtx *p /* Session changeset-apply context */
224000){
224001 int rc = SQLITE_OK;
224002 int i;
224003 SessionBuffer buf = {0, 0, 0};
224004
224005 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
224006 sessionAppendIdent(&buf, zTab, &rc);
224007 sessionAppendStr(&buf, "(", &rc);
224008 for(i=0; i<p->nCol; i++){
224009 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
224010 sessionAppendIdent(&buf, p->azCol[i], &rc);
224011 }
224012
224013 sessionAppendStr(&buf, ") VALUES(?", &rc);
224014 for(i=1; i<p->nCol; i++){
224015 sessionAppendStr(&buf, ", ?", &rc);
224016 }
224017 sessionAppendStr(&buf, ")", &rc);
224018
224019 if( rc==SQLITE_OK ){
224020 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
224021 }
224022 sqlite3_free(buf.aBuf);
224023 return rc;
224024}
224025
224026static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
224027 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
224028}
224029
224030/*
224031** Prepare statements for applying changes to the sqlite_stat1 table.
224032** These are similar to those created by sessionSelectRow(),
224033** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
224034** other tables.
224035*/
224036static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
224037 int rc = sessionSelectRow(db, "sqlite_stat1", p);
224038 if( rc==SQLITE_OK ){
224039 rc = sessionPrepare(db, &p->pInsert,
224040 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
224041 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
224042 "?3)"
224043 );
224044 }
224045 if( rc==SQLITE_OK ){
224046 rc = sessionPrepare(db, &p->pDelete,
224047 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
224048 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
224049 "AND (?4 OR stat IS ?3)"
224050 );
224051 }
224052 return rc;
224053}
224054
224055/*
224056** A wrapper around sqlite3_bind_value() that detects an extra problem.
224057** See comments in the body of this function for details.
224058*/
224059static int sessionBindValue(
224060 sqlite3_stmt *pStmt, /* Statement to bind value to */
224061 int i, /* Parameter number to bind to */
224062 sqlite3_value *pVal /* Value to bind */
224063){
224064 int eType = sqlite3_value_type(pVal);
224065 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
224066 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
224067 ** the (pVal->z) variable remains as it was or the type of the value is
224068 ** set to SQLITE_NULL. */
224069 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
224070 /* This condition occurs when an earlier OOM in a call to
224071 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
224072 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
224073 return SQLITE_NOMEM;
224074 }
224075 return sqlite3_bind_value(pStmt, i, pVal);
224076}
224077
224078/*
224079** Iterator pIter must point to an SQLITE_INSERT entry. This function
224080** transfers new.* values from the current iterator entry to statement
224081** pStmt. The table being inserted into has nCol columns.
224082**
224083** New.* value $i from the iterator is bound to variable ($i+1) of
224084** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
224085** are transfered to the statement. Otherwise, if abPK is not NULL, it points
224086** to an array nCol elements in size. In this case only those values for
224087** which abPK[$i] is true are read from the iterator and bound to the
224088** statement.
224089**
224090** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
224091*/
224092static int sessionBindRow(
224093 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
224094 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
224095 int nCol, /* Number of columns */
224096 u8 *abPK, /* If not NULL, bind only if true */
224097 sqlite3_stmt *pStmt /* Bind values to this statement */
224098){
224099 int i;
224100 int rc = SQLITE_OK;
224101
224102 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
224103 ** argument iterator points to a suitable entry. Make sure that xValue
224104 ** is one of these to guarantee that it is safe to ignore the return
224105 ** in the code below. */
224106 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
224107
224108 for(i=0; rc==SQLITE_OK && i<nCol; i++){
224109 if( !abPK || abPK[i] ){
224110 sqlite3_value *pVal = 0;
224111 (void)xValue(pIter, i, &pVal);
224112 if( pVal==0 ){
224113 /* The value in the changeset was "undefined". This indicates a
224114 ** corrupt changeset blob. */
224115 rc = SQLITE_CORRUPT_BKPT;
224116 }else{
224117 rc = sessionBindValue(pStmt, i+1, pVal);
224118 }
224119 }
224120 }
224121 return rc;
224122}
224123
224124/*
224125** SQL statement pSelect is as generated by the sessionSelectRow() function.
224126** This function binds the primary key values from the change that changeset
224127** iterator pIter points to to the SELECT and attempts to seek to the table
224128** entry. If a row is found, the SELECT statement left pointing at the row
224129** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
224130** has occured, the statement is reset and SQLITE_OK is returned. If an
224131** error occurs, the statement is reset and an SQLite error code is returned.
224132**
224133** If this function returns SQLITE_ROW, the caller must eventually reset()
224134** statement pSelect. If any other value is returned, the statement does
224135** not require a reset().
224136**
224137** If the iterator currently points to an INSERT record, bind values from the
224138** new.* record to the SELECT statement. Or, if it points to a DELETE or
224139** UPDATE, bind values from the old.* record.
224140*/
224141static int sessionSeekToRow(
224142 sqlite3_changeset_iter *pIter, /* Changeset iterator */
224143 SessionApplyCtx *p
224144){
224145 sqlite3_stmt *pSelect = p->pSelect;
224146 int rc; /* Return code */
224147 int nCol; /* Number of columns in table */
224148 int op; /* Changset operation (SQLITE_UPDATE etc.) */
224149 const char *zDummy; /* Unused */
224150
224151 sqlite3_clear_bindings(pSelect);
224152 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
224153 rc = sessionBindRow(pIter,
224154 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
224155 nCol, p->abPK, pSelect
224156 );
224157
224158 if( op!=SQLITE_DELETE && p->bIgnoreNoop ){
224159 int ii;
224160 for(ii=0; rc==SQLITE_OK && ii<nCol; ii++){
224161 if( p->abPK[ii]==0 ){
224162 sqlite3_value *pVal = 0;
224163 sqlite3changeset_new(pIter, ii, &pVal);
224164 sqlite3_bind_int(pSelect, ii+1+nCol, (pVal==0));
224165 if( pVal ) rc = sessionBindValue(pSelect, ii+1, pVal);
224166 }
224167 }
224168 }
224169
224170 if( rc==SQLITE_OK ){
224171 rc = sqlite3_step(pSelect);
224172 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
224173 }
224174
224175 return rc;
224176}
224177
224178/*
224179** This function is called from within sqlite3changeset_apply_v2() when
224180** a conflict is encountered and resolved using conflict resolution
224181** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
224182** It adds a conflict resolution record to the buffer in
224183** SessionApplyCtx.rebase, which will eventually be returned to the caller
224184** of apply_v2() as the "rebase" buffer.
224185**
224186** Return SQLITE_OK if successful, or an SQLite error code otherwise.
224187*/
224188static int sessionRebaseAdd(
224189 SessionApplyCtx *p, /* Apply context */
224190 int eType, /* Conflict resolution (OMIT or REPLACE) */
224191 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
224192){
224193 int rc = SQLITE_OK;
224194 if( p->bRebase ){
224195 int i;
224196 int eOp = pIter->op;
224197 if( p->bRebaseStarted==0 ){
224198 /* Append a table-header to the rebase buffer */
224199 const char *zTab = pIter->zTab;
224200 sessionAppendByte(&p->rebase, 'T', &rc);
224201 sessionAppendVarint(&p->rebase, p->nCol, &rc);
224202 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
224203 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
224204 p->bRebaseStarted = 1;
224205 }
224206
224207 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
224208 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
224209
224210 sessionAppendByte(&p->rebase,
224211 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
224212 );
224213 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
224214 for(i=0; i<p->nCol; i++){
224215 sqlite3_value *pVal = 0;
224216 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
224217 sqlite3changeset_old(pIter, i, &pVal);
224218 }else{
224219 sqlite3changeset_new(pIter, i, &pVal);
224220 }
224221 sessionAppendValue(&p->rebase, pVal, &rc);
224222 }
224223 }
224224 return rc;
224225}
224226
224227/*
224228** Invoke the conflict handler for the change that the changeset iterator
224229** currently points to.
224230**
224231** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
224232** If argument pbReplace is NULL, then the type of conflict handler invoked
224233** depends solely on eType, as follows:
224234**
224235** eType value Value passed to xConflict
224236** -------------------------------------------------
224237** CHANGESET_DATA CHANGESET_NOTFOUND
224238** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
224239**
224240** Or, if pbReplace is not NULL, then an attempt is made to find an existing
224241** record with the same primary key as the record about to be deleted, updated
224242** or inserted. If such a record can be found, it is available to the conflict
224243** handler as the "conflicting" record. In this case the type of conflict
224244** handler invoked is as follows:
224245**
224246** eType value PK Record found? Value passed to xConflict
224247** ----------------------------------------------------------------
224248** CHANGESET_DATA Yes CHANGESET_DATA
224249** CHANGESET_DATA No CHANGESET_NOTFOUND
224250** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
224251** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
224252**
224253** If pbReplace is not NULL, and a record with a matching PK is found, and
224254** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
224255** is set to non-zero before returning SQLITE_OK.
224256**
224257** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
224258** returned. Or, if the conflict handler returns an invalid value,
224259** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
224260** this function returns SQLITE_OK.
224261*/
224262static int sessionConflictHandler(
224263 int eType, /* Either CHANGESET_DATA or CONFLICT */
224264 SessionApplyCtx *p, /* changeset_apply() context */
224265 sqlite3_changeset_iter *pIter, /* Changeset iterator */
224266 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
224267 void *pCtx, /* First argument for conflict handler */
224268 int *pbReplace /* OUT: Set to true if PK row is found */
224269){
224270 int res = 0; /* Value returned by conflict handler */
224271 int rc;
224272 int nCol;
224273 int op;
224274 const char *zDummy;
224275
224276 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
224277
224278 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
224279 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
224280 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
224281
224282 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
224283 if( pbReplace ){
224284 rc = sessionSeekToRow(pIter, p);
224285 }else{
224286 rc = SQLITE_OK;
224287 }
224288
224289 if( rc==SQLITE_ROW ){
224290 /* There exists another row with the new.* primary key. */
224291 if( p->bIgnoreNoop
224292 && sqlite3_column_int(p->pSelect, sqlite3_column_count(p->pSelect)-1)
224293 ){
224294 res = SQLITE_CHANGESET_OMIT;
224295 }else{
224296 pIter->pConflict = p->pSelect;
224297 res = xConflict(pCtx, eType, pIter);
224298 pIter->pConflict = 0;
224299 }
224300 rc = sqlite3_reset(p->pSelect);
224301 }else if( rc==SQLITE_OK ){
224302 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
224303 /* Instead of invoking the conflict handler, append the change blob
224304 ** to the SessionApplyCtx.constraints buffer. */
224305 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
224306 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
224307 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
224308 return SQLITE_OK;
224309 }else{
224310 /* No other row with the new.* primary key. */
224311 res = xConflict(pCtx, eType+1, pIter);
224312 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
224313 }
224314 }
224315
224316 if( rc==SQLITE_OK ){
224317 switch( res ){
224318 case SQLITE_CHANGESET_REPLACE:
224319 assert( pbReplace );
224320 *pbReplace = 1;
224321 break;
224322
224323 case SQLITE_CHANGESET_OMIT:
224324 break;
224325
224326 case SQLITE_CHANGESET_ABORT:
224327 rc = SQLITE_ABORT;
224328 break;
224329
224330 default:
224331 rc = SQLITE_MISUSE;
224332 break;
224333 }
224334 if( rc==SQLITE_OK ){
224335 rc = sessionRebaseAdd(p, res, pIter);
224336 }
224337 }
224338
224339 return rc;
224340}
224341
224342/*
224343** Attempt to apply the change that the iterator passed as the first argument
224344** currently points to to the database. If a conflict is encountered, invoke
224345** the conflict handler callback.
224346**
224347** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
224348** one is encountered, update or delete the row with the matching primary key
224349** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
224350** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
224351** to true before returning. In this case the caller will invoke this function
224352** again, this time with pbRetry set to NULL.
224353**
224354** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
224355** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
224356** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
224357** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
224358** before retrying. In this case the caller attempts to remove the conflicting
224359** row before invoking this function again, this time with pbReplace set
224360** to NULL.
224361**
224362** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
224363** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
224364** returned.
224365*/
224366static int sessionApplyOneOp(
224367 sqlite3_changeset_iter *pIter, /* Changeset iterator */
224368 SessionApplyCtx *p, /* changeset_apply() context */
224369 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
224370 void *pCtx, /* First argument for the conflict handler */
224371 int *pbReplace, /* OUT: True to remove PK row and retry */
224372 int *pbRetry /* OUT: True to retry. */
224373){
224374 const char *zDummy;
224375 int op;
224376 int nCol;
224377 int rc = SQLITE_OK;
224378
224379 assert( p->pDelete && p->pInsert && p->pSelect );
224380 assert( p->azCol && p->abPK );
224381 assert( !pbReplace || *pbReplace==0 );
224382
224383 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
224384
224385 if( op==SQLITE_DELETE ){
224386
224387 /* Bind values to the DELETE statement. If conflict handling is required,
224388 ** bind values for all columns and set bound variable (nCol+1) to true.
224389 ** Or, if conflict handling is not required, bind just the PK column
224390 ** values and, if it exists, set (nCol+1) to false. Conflict handling
224391 ** is not required if:
224392 **
224393 ** * this is a patchset, or
224394 ** * (pbRetry==0), or
224395 ** * all columns of the table are PK columns (in this case there is
224396 ** no (nCol+1) variable to bind to).
224397 */
224398 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
224399 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
224400 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
224401 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
224402 }
224403 if( rc!=SQLITE_OK ) return rc;
224404
224405 sqlite3_step(p->pDelete);
224406 rc = sqlite3_reset(p->pDelete);
224407 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 && p->bIgnoreNoop==0 ){
224408 rc = sessionConflictHandler(
224409 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
224410 );
224411 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
224412 rc = sessionConflictHandler(
224413 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
224414 );
224415 }
224416
224417 }else if( op==SQLITE_UPDATE ){
224418 int i;
224419 sqlite3_stmt *pUp = 0;
224420 int bPatchset = (pbRetry==0 || pIter->bPatchset);
224421
224422 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
224423
224424 /* Bind values to the UPDATE statement. */
224425 for(i=0; rc==SQLITE_OK && i<nCol; i++){
224426 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
224427 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
224428 if( p->abPK[i] || (bPatchset==0 && pOld) ){
224429 rc = sessionBindValue(pUp, i*2+2, pOld);
224430 }
224431 if( rc==SQLITE_OK && pNew ){
224432 rc = sessionBindValue(pUp, i*2+1, pNew);
224433 }
224434 }
224435 if( rc!=SQLITE_OK ) return rc;
224436
224437 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
224438 ** the result will be SQLITE_OK with 0 rows modified. */
224439 sqlite3_step(pUp);
224440 rc = sqlite3_reset(pUp);
224441
224442 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
224443 /* A NOTFOUND or DATA error. Search the table to see if it contains
224444 ** a row with a matching primary key. If so, this is a DATA conflict.
224445 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
224446
224447 rc = sessionConflictHandler(
224448 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
224449 );
224450
224451 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
224452 /* This is always a CONSTRAINT conflict. */
224453 rc = sessionConflictHandler(
224454 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
224455 );
224456 }
224457
224458 }else{
224459 assert( op==SQLITE_INSERT );
224460 if( p->bStat1 ){
224461 /* Check if there is a conflicting row. For sqlite_stat1, this needs
224462 ** to be done using a SELECT, as there is no PRIMARY KEY in the
224463 ** database schema to throw an exception if a duplicate is inserted. */
224464 rc = sessionSeekToRow(pIter, p);
224465 if( rc==SQLITE_ROW ){
224466 rc = SQLITE_CONSTRAINT;
224467 sqlite3_reset(p->pSelect);
224468 }
224469 }
224470
224471 if( rc==SQLITE_OK ){
224472 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
224473 if( rc!=SQLITE_OK ) return rc;
224474
224475 sqlite3_step(p->pInsert);
224476 rc = sqlite3_reset(p->pInsert);
224477 }
224478
224479 if( (rc&0xff)==SQLITE_CONSTRAINT ){
224480 rc = sessionConflictHandler(
224481 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
224482 );
224483 }
224484 }
224485
224486 return rc;
224487}
224488
224489/*
224490** Attempt to apply the change that the iterator passed as the first argument
224491** currently points to to the database. If a conflict is encountered, invoke
224492** the conflict handler callback.
224493**
224494** The difference between this function and sessionApplyOne() is that this
224495** function handles the case where the conflict-handler is invoked and
224496** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
224497** retried in some manner.
224498*/
224499static int sessionApplyOneWithRetry(
224500 sqlite3 *db, /* Apply change to "main" db of this handle */
224501 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
224502 SessionApplyCtx *pApply, /* Apply context */
224503 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
224504 void *pCtx /* First argument passed to xConflict */
224505){
224506 int bReplace = 0;
224507 int bRetry = 0;
224508 int rc;
224509
224510 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
224511 if( rc==SQLITE_OK ){
224512 /* If the bRetry flag is set, the change has not been applied due to an
224513 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
224514 ** a row with the correct PK is present in the db, but one or more other
224515 ** fields do not contain the expected values) and the conflict handler
224516 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
224517 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
224518 ** the SQLITE_CHANGESET_DATA problem. */
224519 if( bRetry ){
224520 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
224521 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
224522 }
224523
224524 /* If the bReplace flag is set, the change is an INSERT that has not
224525 ** been performed because the database already contains a row with the
224526 ** specified primary key and the conflict handler returned
224527 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
224528 ** before reattempting the INSERT. */
224529 else if( bReplace ){
224530 assert( pIter->op==SQLITE_INSERT );
224531 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
224532 if( rc==SQLITE_OK ){
224533 rc = sessionBindRow(pIter,
224534 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
224535 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
224536 }
224537 if( rc==SQLITE_OK ){
224538 sqlite3_step(pApply->pDelete);
224539 rc = sqlite3_reset(pApply->pDelete);
224540 }
224541 if( rc==SQLITE_OK ){
224542 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
224543 }
224544 if( rc==SQLITE_OK ){
224545 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
224546 }
224547 }
224548 }
224549
224550 return rc;
224551}
224552
224553/*
224554** Retry the changes accumulated in the pApply->constraints buffer.
224555*/
224556static int sessionRetryConstraints(
224557 sqlite3 *db,
224558 int bPatchset,
224559 const char *zTab,
224560 SessionApplyCtx *pApply,
224561 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
224562 void *pCtx /* First argument passed to xConflict */
224563){
224564 int rc = SQLITE_OK;
224565
224566 while( pApply->constraints.nBuf ){
224567 sqlite3_changeset_iter *pIter2 = 0;
224568 SessionBuffer cons = pApply->constraints;
224569 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
224570
224571 rc = sessionChangesetStart(
224572 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1
224573 );
224574 if( rc==SQLITE_OK ){
224575 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
224576 int rc2;
224577 pIter2->bPatchset = bPatchset;
224578 pIter2->zTab = (char*)zTab;
224579 pIter2->nCol = pApply->nCol;
224580 pIter2->abPK = pApply->abPK;
224581 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
224582 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
224583 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
224584
224585 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
224586 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
224587 }
224588
224589 rc2 = sqlite3changeset_finalize(pIter2);
224590 if( rc==SQLITE_OK ) rc = rc2;
224591 }
224592 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
224593
224594 sqlite3_free(cons.aBuf);
224595 if( rc!=SQLITE_OK ) break;
224596 if( pApply->constraints.nBuf>=cons.nBuf ){
224597 /* No progress was made on the last round. */
224598 pApply->bDeferConstraints = 0;
224599 }
224600 }
224601
224602 return rc;
224603}
224604
224605/*
224606** Argument pIter is a changeset iterator that has been initialized, but
224607** not yet passed to sqlite3changeset_next(). This function applies the
224608** changeset to the main database attached to handle "db". The supplied
224609** conflict handler callback is invoked to resolve any conflicts encountered
224610** while applying the change.
224611*/
224612static int sessionChangesetApply(
224613 sqlite3 *db, /* Apply change to "main" db of this handle */
224614 sqlite3_changeset_iter *pIter, /* Changeset to apply */
224615 int(*xFilter)(
224616 void *pCtx, /* Copy of sixth arg to _apply() */
224617 const char *zTab /* Table name */
224618 ),
224619 int(*xConflict)(
224620 void *pCtx, /* Copy of fifth arg to _apply() */
224621 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
224622 sqlite3_changeset_iter *p /* Handle describing change and conflict */
224623 ),
224624 void *pCtx, /* First argument passed to xConflict */
224625 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
224626 int flags /* SESSION_APPLY_XXX flags */
224627){
224628 int schemaMismatch = 0;
224629 int rc = SQLITE_OK; /* Return code */
224630 const char *zTab = 0; /* Name of current table */
224631 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
224632 SessionApplyCtx sApply; /* changeset_apply() context object */
224633 int bPatchset;
224634
224635 assert( xConflict!=0 );
224636
224637 pIter->in.bNoDiscard = 1;
224638 memset(&sApply, 0, sizeof(sApply));
224639 sApply.bRebase = (ppRebase && pnRebase);
224640 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
224641 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP);
224642 sqlite3_mutex_enter(sqlite3_db_mutex(db));
224643 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
224644 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
224645 }
224646 if( rc==SQLITE_OK ){
224647 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
224648 }
224649 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
224650 int nCol;
224651 int op;
224652 const char *zNew;
224653
224654 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
224655
224656 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
224657 u8 *abPK;
224658
224659 rc = sessionRetryConstraints(
224660 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
224661 );
224662 if( rc!=SQLITE_OK ) break;
224663
224664 sessionUpdateFree(&sApply);
224665 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
224666 sqlite3_finalize(sApply.pDelete);
224667 sqlite3_finalize(sApply.pInsert);
224668 sqlite3_finalize(sApply.pSelect);
224669 sApply.db = db;
224670 sApply.pDelete = 0;
224671 sApply.pInsert = 0;
224672 sApply.pSelect = 0;
224673 sApply.nCol = 0;
224674 sApply.azCol = 0;
224675 sApply.abPK = 0;
224676 sApply.bStat1 = 0;
224677 sApply.bDeferConstraints = 1;
224678 sApply.bRebaseStarted = 0;
224679 sApply.bRowid = 0;
224680 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
224681
224682 /* If an xFilter() callback was specified, invoke it now. If the
224683 ** xFilter callback returns zero, skip this table. If it returns
224684 ** non-zero, proceed. */
224685 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
224686 if( schemaMismatch ){
224687 zTab = sqlite3_mprintf("%s", zNew);
224688 if( zTab==0 ){
224689 rc = SQLITE_NOMEM;
224690 break;
224691 }
224692 nTab = (int)strlen(zTab);
224693 sApply.azCol = (const char **)zTab;
224694 }else{
224695 int nMinCol = 0;
224696 int i;
224697
224698 sqlite3changeset_pk(pIter, &abPK, 0);
224699 rc = sessionTableInfo(0, db, "main", zNew,
224700 &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK, &sApply.bRowid
224701 );
224702 if( rc!=SQLITE_OK ) break;
224703 for(i=0; i<sApply.nCol; i++){
224704 if( sApply.abPK[i] ) nMinCol = i+1;
224705 }
224706
224707 if( sApply.nCol==0 ){
224708 schemaMismatch = 1;
224709 sqlite3_log(SQLITE_SCHEMA,
224710 "sqlite3changeset_apply(): no such table: %s", zTab
224711 );
224712 }
224713 else if( sApply.nCol<nCol ){
224714 schemaMismatch = 1;
224715 sqlite3_log(SQLITE_SCHEMA,
224716 "sqlite3changeset_apply(): table %s has %d columns, "
224717 "expected %d or more",
224718 zTab, sApply.nCol, nCol
224719 );
224720 }
224721 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
224722 schemaMismatch = 1;
224723 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
224724 "primary key mismatch for table %s", zTab
224725 );
224726 }
224727 else{
224728 sApply.nCol = nCol;
224729 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
224730 if( (rc = sessionStat1Sql(db, &sApply) ) ){
224731 break;
224732 }
224733 sApply.bStat1 = 1;
224734 }else{
224735 if( (rc = sessionSelectRow(db, zTab, &sApply))
224736 || (rc = sessionDeleteRow(db, zTab, &sApply))
224737 || (rc = sessionInsertRow(db, zTab, &sApply))
224738 ){
224739 break;
224740 }
224741 sApply.bStat1 = 0;
224742 }
224743 }
224744 nTab = sqlite3Strlen30(zTab);
224745 }
224746 }
224747
224748 /* If there is a schema mismatch on the current table, proceed to the
224749 ** next change. A log message has already been issued. */
224750 if( schemaMismatch ) continue;
224751
224752 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
224753 }
224754
224755 bPatchset = pIter->bPatchset;
224756 if( rc==SQLITE_OK ){
224757 rc = sqlite3changeset_finalize(pIter);
224758 }else{
224759 sqlite3changeset_finalize(pIter);
224760 }
224761
224762 if( rc==SQLITE_OK ){
224763 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
224764 }
224765
224766 if( rc==SQLITE_OK ){
224767 int nFk, notUsed;
224768 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
224769 if( nFk!=0 ){
224770 int res = SQLITE_CHANGESET_ABORT;
224771 sqlite3_changeset_iter sIter;
224772 memset(&sIter, 0, sizeof(sIter));
224773 sIter.nCol = nFk;
224774 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
224775 if( res!=SQLITE_CHANGESET_OMIT ){
224776 rc = SQLITE_CONSTRAINT;
224777 }
224778 }
224779 }
224780 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
224781
224782 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
224783 if( rc==SQLITE_OK ){
224784 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
224785 }else{
224786 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
224787 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
224788 }
224789 }
224790
224791 assert( sApply.bRebase || sApply.rebase.nBuf==0 );
224792 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
224793 *ppRebase = (void*)sApply.rebase.aBuf;
224794 *pnRebase = sApply.rebase.nBuf;
224795 sApply.rebase.aBuf = 0;
224796 }
224797 sessionUpdateFree(&sApply);
224798 sqlite3_finalize(sApply.pInsert);
224799 sqlite3_finalize(sApply.pDelete);
224800 sqlite3_finalize(sApply.pSelect);
224801 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
224802 sqlite3_free((char*)sApply.constraints.aBuf);
224803 sqlite3_free((char*)sApply.rebase.aBuf);
224804 sqlite3_mutex_leave(sqlite3_db_mutex(db));
224805 return rc;
224806}
224807
224808/*
224809** Apply the changeset passed via pChangeset/nChangeset to the main
224810** database attached to handle "db".
224811*/
224812SQLITE_API int sqlite3changeset_apply_v2(
224813 sqlite3 *db, /* Apply change to "main" db of this handle */
224814 int nChangeset, /* Size of changeset in bytes */
224815 void *pChangeset, /* Changeset blob */
224816 int(*xFilter)(
224817 void *pCtx, /* Copy of sixth arg to _apply() */
224818 const char *zTab /* Table name */
224819 ),
224820 int(*xConflict)(
224821 void *pCtx, /* Copy of sixth arg to _apply() */
224822 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
224823 sqlite3_changeset_iter *p /* Handle describing change and conflict */
224824 ),
224825 void *pCtx, /* First argument passed to xConflict */
224826 void **ppRebase, int *pnRebase,
224827 int flags
224828){
224829 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
224830 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
224831 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
224832 if( rc==SQLITE_OK ){
224833 rc = sessionChangesetApply(
224834 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
224835 );
224836 }
224837 return rc;
224838}
224839
224840/*
224841** Apply the changeset passed via pChangeset/nChangeset to the main database
224842** attached to handle "db". Invoke the supplied conflict handler callback
224843** to resolve any conflicts encountered while applying the change.
224844*/
224845SQLITE_API int sqlite3changeset_apply(
224846 sqlite3 *db, /* Apply change to "main" db of this handle */
224847 int nChangeset, /* Size of changeset in bytes */
224848 void *pChangeset, /* Changeset blob */
224849 int(*xFilter)(
224850 void *pCtx, /* Copy of sixth arg to _apply() */
224851 const char *zTab /* Table name */
224852 ),
224853 int(*xConflict)(
224854 void *pCtx, /* Copy of fifth arg to _apply() */
224855 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
224856 sqlite3_changeset_iter *p /* Handle describing change and conflict */
224857 ),
224858 void *pCtx /* First argument passed to xConflict */
224859){
224860 return sqlite3changeset_apply_v2(
224861 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
224862 );
224863}
224864
224865/*
224866** Apply the changeset passed via xInput/pIn to the main database
224867** attached to handle "db". Invoke the supplied conflict handler callback
224868** to resolve any conflicts encountered while applying the change.
224869*/
224870SQLITE_API int sqlite3changeset_apply_v2_strm(
224871 sqlite3 *db, /* Apply change to "main" db of this handle */
224872 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
224873 void *pIn, /* First arg for xInput */
224874 int(*xFilter)(
224875 void *pCtx, /* Copy of sixth arg to _apply() */
224876 const char *zTab /* Table name */
224877 ),
224878 int(*xConflict)(
224879 void *pCtx, /* Copy of sixth arg to _apply() */
224880 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
224881 sqlite3_changeset_iter *p /* Handle describing change and conflict */
224882 ),
224883 void *pCtx, /* First argument passed to xConflict */
224884 void **ppRebase, int *pnRebase,
224885 int flags
224886){
224887 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
224888 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
224889 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse, 1);
224890 if( rc==SQLITE_OK ){
224891 rc = sessionChangesetApply(
224892 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
224893 );
224894 }
224895 return rc;
224896}
224897SQLITE_API int sqlite3changeset_apply_strm(
224898 sqlite3 *db, /* Apply change to "main" db of this handle */
224899 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
224900 void *pIn, /* First arg for xInput */
224901 int(*xFilter)(
224902 void *pCtx, /* Copy of sixth arg to _apply() */
224903 const char *zTab /* Table name */
224904 ),
224905 int(*xConflict)(
224906 void *pCtx, /* Copy of sixth arg to _apply() */
224907 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
224908 sqlite3_changeset_iter *p /* Handle describing change and conflict */
224909 ),
224910 void *pCtx /* First argument passed to xConflict */
224911){
224912 return sqlite3changeset_apply_v2_strm(
224913 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
224914 );
224915}
224916
224917/*
224918** sqlite3_changegroup handle.
224919*/
224920struct sqlite3_changegroup {
224921 int rc; /* Error code */
224922 int bPatch; /* True to accumulate patchsets */
224923 SessionTable *pList; /* List of tables in current patch */
224924};
224925
224926/*
224927** This function is called to merge two changes to the same row together as
224928** part of an sqlite3changeset_concat() operation. A new change object is
224929** allocated and a pointer to it stored in *ppNew.
224930*/
224931static int sessionChangeMerge(
224932 SessionTable *pTab, /* Table structure */
224933 int bRebase, /* True for a rebase hash-table */
224934 int bPatchset, /* True for patchsets */
224935 SessionChange *pExist, /* Existing change */
224936 int op2, /* Second change operation */
224937 int bIndirect, /* True if second change is indirect */
224938 u8 *aRec, /* Second change record */
224939 int nRec, /* Number of bytes in aRec */
224940 SessionChange **ppNew /* OUT: Merged change */
224941){
224942 SessionChange *pNew = 0;
224943 int rc = SQLITE_OK;
224944
224945 if( !pExist ){
224946 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
224947 if( !pNew ){
224948 return SQLITE_NOMEM;
224949 }
224950 memset(pNew, 0, sizeof(SessionChange));
224951 pNew->op = op2;
224952 pNew->bIndirect = bIndirect;
224953 pNew->aRecord = (u8*)&pNew[1];
224954 if( bIndirect==0 || bRebase==0 ){
224955 pNew->nRecord = nRec;
224956 memcpy(pNew->aRecord, aRec, nRec);
224957 }else{
224958 int i;
224959 u8 *pIn = aRec;
224960 u8 *pOut = pNew->aRecord;
224961 for(i=0; i<pTab->nCol; i++){
224962 int nIn = sessionSerialLen(pIn);
224963 if( *pIn==0 ){
224964 *pOut++ = 0;
224965 }else if( pTab->abPK[i]==0 ){
224966 *pOut++ = 0xFF;
224967 }else{
224968 memcpy(pOut, pIn, nIn);
224969 pOut += nIn;
224970 }
224971 pIn += nIn;
224972 }
224973 pNew->nRecord = pOut - pNew->aRecord;
224974 }
224975 }else if( bRebase ){
224976 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
224977 *ppNew = pExist;
224978 }else{
224979 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
224980 pNew = (SessionChange*)sqlite3_malloc64(nByte);
224981 if( pNew==0 ){
224982 rc = SQLITE_NOMEM;
224983 }else{
224984 int i;
224985 u8 *a1 = pExist->aRecord;
224986 u8 *a2 = aRec;
224987 u8 *pOut;
224988
224989 memset(pNew, 0, nByte);
224990 pNew->bIndirect = bIndirect || pExist->bIndirect;
224991 pNew->op = op2;
224992 pOut = pNew->aRecord = (u8*)&pNew[1];
224993
224994 for(i=0; i<pTab->nCol; i++){
224995 int n1 = sessionSerialLen(a1);
224996 int n2 = sessionSerialLen(a2);
224997 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
224998 *pOut++ = 0xFF;
224999 }else if( *a2==0 ){
225000 memcpy(pOut, a1, n1);
225001 pOut += n1;
225002 }else{
225003 memcpy(pOut, a2, n2);
225004 pOut += n2;
225005 }
225006 a1 += n1;
225007 a2 += n2;
225008 }
225009 pNew->nRecord = pOut - pNew->aRecord;
225010 }
225011 sqlite3_free(pExist);
225012 }
225013 }else{
225014 int op1 = pExist->op;
225015
225016 /*
225017 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
225018 ** op1=INSERT, op2=UPDATE -> INSERT.
225019 ** op1=INSERT, op2=DELETE -> (none)
225020 **
225021 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
225022 ** op1=UPDATE, op2=UPDATE -> UPDATE.
225023 ** op1=UPDATE, op2=DELETE -> DELETE.
225024 **
225025 ** op1=DELETE, op2=INSERT -> UPDATE.
225026 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
225027 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
225028 */
225029 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
225030 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
225031 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
225032 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
225033 ){
225034 pNew = pExist;
225035 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
225036 sqlite3_free(pExist);
225037 assert( pNew==0 );
225038 }else{
225039 u8 *aExist = pExist->aRecord;
225040 sqlite3_int64 nByte;
225041 u8 *aCsr;
225042
225043 /* Allocate a new SessionChange object. Ensure that the aRecord[]
225044 ** buffer of the new object is large enough to hold any record that
225045 ** may be generated by combining the input records. */
225046 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
225047 pNew = (SessionChange *)sqlite3_malloc64(nByte);
225048 if( !pNew ){
225049 sqlite3_free(pExist);
225050 return SQLITE_NOMEM;
225051 }
225052 memset(pNew, 0, sizeof(SessionChange));
225053 pNew->bIndirect = (bIndirect && pExist->bIndirect);
225054 aCsr = pNew->aRecord = (u8 *)&pNew[1];
225055
225056 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
225057 u8 *a1 = aRec;
225058 assert( op2==SQLITE_UPDATE );
225059 pNew->op = SQLITE_INSERT;
225060 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
225061 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
225062 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
225063 assert( op2==SQLITE_INSERT );
225064 pNew->op = SQLITE_UPDATE;
225065 if( bPatchset ){
225066 memcpy(aCsr, aRec, nRec);
225067 aCsr += nRec;
225068 }else{
225069 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
225070 sqlite3_free(pNew);
225071 pNew = 0;
225072 }
225073 }
225074 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
225075 u8 *a1 = aExist;
225076 u8 *a2 = aRec;
225077 assert( op1==SQLITE_UPDATE );
225078 if( bPatchset==0 ){
225079 sessionSkipRecord(&a1, pTab->nCol);
225080 sessionSkipRecord(&a2, pTab->nCol);
225081 }
225082 pNew->op = SQLITE_UPDATE;
225083 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
225084 sqlite3_free(pNew);
225085 pNew = 0;
225086 }
225087 }else{ /* UPDATE + DELETE */
225088 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
225089 pNew->op = SQLITE_DELETE;
225090 if( bPatchset ){
225091 memcpy(aCsr, aRec, nRec);
225092 aCsr += nRec;
225093 }else{
225094 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
225095 }
225096 }
225097
225098 if( pNew ){
225099 pNew->nRecord = (int)(aCsr - pNew->aRecord);
225100 }
225101 sqlite3_free(pExist);
225102 }
225103 }
225104
225105 *ppNew = pNew;
225106 return rc;
225107}
225108
225109/*
225110** Add all changes in the changeset traversed by the iterator passed as
225111** the first argument to the changegroup hash tables.
225112*/
225113static int sessionChangesetToHash(
225114 sqlite3_changeset_iter *pIter, /* Iterator to read from */
225115 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
225116 int bRebase /* True if hash table is for rebasing */
225117){
225118 u8 *aRec;
225119 int nRec;
225120 int rc = SQLITE_OK;
225121 SessionTable *pTab = 0;
225122
225123 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
225124 const char *zNew;
225125 int nCol;
225126 int op;
225127 int iHash;
225128 int bIndirect;
225129 SessionChange *pChange;
225130 SessionChange *pExist = 0;
225131 SessionChange **pp;
225132
225133 if( pGrp->pList==0 ){
225134 pGrp->bPatch = pIter->bPatchset;
225135 }else if( pIter->bPatchset!=pGrp->bPatch ){
225136 rc = SQLITE_ERROR;
225137 break;
225138 }
225139
225140 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
225141 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
225142 /* Search the list for a matching table */
225143 int nNew = (int)strlen(zNew);
225144 u8 *abPK;
225145
225146 sqlite3changeset_pk(pIter, &abPK, 0);
225147 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
225148 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
225149 }
225150 if( !pTab ){
225151 SessionTable **ppTab;
225152
225153 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
225154 if( !pTab ){
225155 rc = SQLITE_NOMEM;
225156 break;
225157 }
225158 memset(pTab, 0, sizeof(SessionTable));
225159 pTab->nCol = nCol;
225160 pTab->abPK = (u8*)&pTab[1];
225161 memcpy(pTab->abPK, abPK, nCol);
225162 pTab->zName = (char*)&pTab->abPK[nCol];
225163 memcpy(pTab->zName, zNew, nNew+1);
225164
225165 /* The new object must be linked on to the end of the list, not
225166 ** simply added to the start of it. This is to ensure that the
225167 ** tables within the output of sqlite3changegroup_output() are in
225168 ** the right order. */
225169 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
225170 *ppTab = pTab;
225171 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
225172 rc = SQLITE_SCHEMA;
225173 break;
225174 }
225175 }
225176
225177 if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
225178 rc = SQLITE_NOMEM;
225179 break;
225180 }
225181 iHash = sessionChangeHash(
225182 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
225183 );
225184
225185 /* Search for existing entry. If found, remove it from the hash table.
225186 ** Code below may link it back in.
225187 */
225188 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
225189 int bPkOnly1 = 0;
225190 int bPkOnly2 = 0;
225191 if( pIter->bPatchset ){
225192 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
225193 bPkOnly2 = op==SQLITE_DELETE;
225194 }
225195 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
225196 pExist = *pp;
225197 *pp = (*pp)->pNext;
225198 pTab->nEntry--;
225199 break;
225200 }
225201 }
225202
225203 rc = sessionChangeMerge(pTab, bRebase,
225204 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
225205 );
225206 if( rc ) break;
225207 if( pChange ){
225208 pChange->pNext = pTab->apChange[iHash];
225209 pTab->apChange[iHash] = pChange;
225210 pTab->nEntry++;
225211 }
225212 }
225213
225214 if( rc==SQLITE_OK ) rc = pIter->rc;
225215 return rc;
225216}
225217
225218/*
225219** Serialize a changeset (or patchset) based on all changesets (or patchsets)
225220** added to the changegroup object passed as the first argument.
225221**
225222** If xOutput is not NULL, then the changeset/patchset is returned to the
225223** user via one or more calls to xOutput, as with the other streaming
225224** interfaces.
225225**
225226** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
225227** buffer containing the output changeset before this function returns. In
225228** this case (*pnOut) is set to the size of the output buffer in bytes. It
225229** is the responsibility of the caller to free the output buffer using
225230** sqlite3_free() when it is no longer required.
225231**
225232** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
225233** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
225234** are both set to 0 before returning.
225235*/
225236static int sessionChangegroupOutput(
225237 sqlite3_changegroup *pGrp,
225238 int (*xOutput)(void *pOut, const void *pData, int nData),
225239 void *pOut,
225240 int *pnOut,
225241 void **ppOut
225242){
225243 int rc = SQLITE_OK;
225244 SessionBuffer buf = {0, 0, 0};
225245 SessionTable *pTab;
225246 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
225247
225248 /* Create the serialized output changeset based on the contents of the
225249 ** hash tables attached to the SessionTable objects in list p->pList.
225250 */
225251 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
225252 int i;
225253 if( pTab->nEntry==0 ) continue;
225254
225255 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
225256 for(i=0; i<pTab->nChange; i++){
225257 SessionChange *p;
225258 for(p=pTab->apChange[i]; p; p=p->pNext){
225259 sessionAppendByte(&buf, p->op, &rc);
225260 sessionAppendByte(&buf, p->bIndirect, &rc);
225261 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
225262 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
225263 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
225264 buf.nBuf = 0;
225265 }
225266 }
225267 }
225268 }
225269
225270 if( rc==SQLITE_OK ){
225271 if( xOutput ){
225272 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
225273 }else if( ppOut ){
225274 *ppOut = buf.aBuf;
225275 if( pnOut ) *pnOut = buf.nBuf;
225276 buf.aBuf = 0;
225277 }
225278 }
225279 sqlite3_free(buf.aBuf);
225280
225281 return rc;
225282}
225283
225284/*
225285** Allocate a new, empty, sqlite3_changegroup.
225286*/
225287SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
225288 int rc = SQLITE_OK; /* Return code */
225289 sqlite3_changegroup *p; /* New object */
225290 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
225291 if( p==0 ){
225292 rc = SQLITE_NOMEM;
225293 }else{
225294 memset(p, 0, sizeof(sqlite3_changegroup));
225295 }
225296 *pp = p;
225297 return rc;
225298}
225299
225300/*
225301** Add the changeset currently stored in buffer pData, size nData bytes,
225302** to changeset-group p.
225303*/
225304SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
225305 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
225306 int rc; /* Return code */
225307
225308 rc = sqlite3changeset_start(&pIter, nData, pData);
225309 if( rc==SQLITE_OK ){
225310 rc = sessionChangesetToHash(pIter, pGrp, 0);
225311 }
225312 sqlite3changeset_finalize(pIter);
225313 return rc;
225314}
225315
225316/*
225317** Obtain a buffer containing a changeset representing the concatenation
225318** of all changesets added to the group so far.
225319*/
225320SQLITE_API int sqlite3changegroup_output(
225321 sqlite3_changegroup *pGrp,
225322 int *pnData,
225323 void **ppData
225324){
225325 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
225326}
225327
225328/*
225329** Streaming versions of changegroup_add().
225330*/
225331SQLITE_API int sqlite3changegroup_add_strm(
225332 sqlite3_changegroup *pGrp,
225333 int (*xInput)(void *pIn, void *pData, int *pnData),
225334 void *pIn
225335){
225336 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
225337 int rc; /* Return code */
225338
225339 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
225340 if( rc==SQLITE_OK ){
225341 rc = sessionChangesetToHash(pIter, pGrp, 0);
225342 }
225343 sqlite3changeset_finalize(pIter);
225344 return rc;
225345}
225346
225347/*
225348** Streaming versions of changegroup_output().
225349*/
225350SQLITE_API int sqlite3changegroup_output_strm(
225351 sqlite3_changegroup *pGrp,
225352 int (*xOutput)(void *pOut, const void *pData, int nData),
225353 void *pOut
225354){
225355 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
225356}
225357
225358/*
225359** Delete a changegroup object.
225360*/
225361SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
225362 if( pGrp ){
225363 sessionDeleteTable(0, pGrp->pList);
225364 sqlite3_free(pGrp);
225365 }
225366}
225367
225368/*
225369** Combine two changesets together.
225370*/
225371SQLITE_API int sqlite3changeset_concat(
225372 int nLeft, /* Number of bytes in lhs input */
225373 void *pLeft, /* Lhs input changeset */
225374 int nRight /* Number of bytes in rhs input */,
225375 void *pRight, /* Rhs input changeset */
225376 int *pnOut, /* OUT: Number of bytes in output changeset */
225377 void **ppOut /* OUT: changeset (left <concat> right) */
225378){
225379 sqlite3_changegroup *pGrp;
225380 int rc;
225381
225382 rc = sqlite3changegroup_new(&pGrp);
225383 if( rc==SQLITE_OK ){
225384 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
225385 }
225386 if( rc==SQLITE_OK ){
225387 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
225388 }
225389 if( rc==SQLITE_OK ){
225390 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
225391 }
225392 sqlite3changegroup_delete(pGrp);
225393
225394 return rc;
225395}
225396
225397/*
225398** Streaming version of sqlite3changeset_concat().
225399*/
225400SQLITE_API int sqlite3changeset_concat_strm(
225401 int (*xInputA)(void *pIn, void *pData, int *pnData),
225402 void *pInA,
225403 int (*xInputB)(void *pIn, void *pData, int *pnData),
225404 void *pInB,
225405 int (*xOutput)(void *pOut, const void *pData, int nData),
225406 void *pOut
225407){
225408 sqlite3_changegroup *pGrp;
225409 int rc;
225410
225411 rc = sqlite3changegroup_new(&pGrp);
225412 if( rc==SQLITE_OK ){
225413 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
225414 }
225415 if( rc==SQLITE_OK ){
225416 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
225417 }
225418 if( rc==SQLITE_OK ){
225419 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
225420 }
225421 sqlite3changegroup_delete(pGrp);
225422
225423 return rc;
225424}
225425
225426/*
225427** Changeset rebaser handle.
225428*/
225429struct sqlite3_rebaser {
225430 sqlite3_changegroup grp; /* Hash table */
225431};
225432
225433/*
225434** Buffers a1 and a2 must both contain a sessions module record nCol
225435** fields in size. This function appends an nCol sessions module
225436** record to buffer pBuf that is a copy of a1, except that for
225437** each field that is undefined in a1[], swap in the field from a2[].
225438*/
225439static void sessionAppendRecordMerge(
225440 SessionBuffer *pBuf, /* Buffer to append to */
225441 int nCol, /* Number of columns in each record */
225442 u8 *a1, int n1, /* Record 1 */
225443 u8 *a2, int n2, /* Record 2 */
225444 int *pRc /* IN/OUT: error code */
225445){
225446 sessionBufferGrow(pBuf, n1+n2, pRc);
225447 if( *pRc==SQLITE_OK ){
225448 int i;
225449 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
225450 for(i=0; i<nCol; i++){
225451 int nn1 = sessionSerialLen(a1);
225452 int nn2 = sessionSerialLen(a2);
225453 if( *a1==0 || *a1==0xFF ){
225454 memcpy(pOut, a2, nn2);
225455 pOut += nn2;
225456 }else{
225457 memcpy(pOut, a1, nn1);
225458 pOut += nn1;
225459 }
225460 a1 += nn1;
225461 a2 += nn2;
225462 }
225463
225464 pBuf->nBuf = pOut-pBuf->aBuf;
225465 assert( pBuf->nBuf<=pBuf->nAlloc );
225466 }
225467}
225468
225469/*
225470** This function is called when rebasing a local UPDATE change against one
225471** or more remote UPDATE changes. The aRec/nRec buffer contains the current
225472** old.* and new.* records for the change. The rebase buffer (a single
225473** record) is in aChange/nChange. The rebased change is appended to buffer
225474** pBuf.
225475**
225476** Rebasing the UPDATE involves:
225477**
225478** * Removing any changes to fields for which the corresponding field
225479** in the rebase buffer is set to "replaced" (type 0xFF). If this
225480** means the UPDATE change updates no fields, nothing is appended
225481** to the output buffer.
225482**
225483** * For each field modified by the local change for which the
225484** corresponding field in the rebase buffer is not "undefined" (0x00)
225485** or "replaced" (0xFF), the old.* value is replaced by the value
225486** in the rebase buffer.
225487*/
225488static void sessionAppendPartialUpdate(
225489 SessionBuffer *pBuf, /* Append record here */
225490 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
225491 u8 *aRec, int nRec, /* Local change */
225492 u8 *aChange, int nChange, /* Record to rebase against */
225493 int *pRc /* IN/OUT: Return Code */
225494){
225495 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
225496 if( *pRc==SQLITE_OK ){
225497 int bData = 0;
225498 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
225499 int i;
225500 u8 *a1 = aRec;
225501 u8 *a2 = aChange;
225502
225503 *pOut++ = SQLITE_UPDATE;
225504 *pOut++ = pIter->bIndirect;
225505 for(i=0; i<pIter->nCol; i++){
225506 int n1 = sessionSerialLen(a1);
225507 int n2 = sessionSerialLen(a2);
225508 if( pIter->abPK[i] || a2[0]==0 ){
225509 if( !pIter->abPK[i] && a1[0] ) bData = 1;
225510 memcpy(pOut, a1, n1);
225511 pOut += n1;
225512 }else if( a2[0]!=0xFF && a1[0] ){
225513 bData = 1;
225514 memcpy(pOut, a2, n2);
225515 pOut += n2;
225516 }else{
225517 *pOut++ = '\0';
225518 }
225519 a1 += n1;
225520 a2 += n2;
225521 }
225522 if( bData ){
225523 a2 = aChange;
225524 for(i=0; i<pIter->nCol; i++){
225525 int n1 = sessionSerialLen(a1);
225526 int n2 = sessionSerialLen(a2);
225527 if( pIter->abPK[i] || a2[0]!=0xFF ){
225528 memcpy(pOut, a1, n1);
225529 pOut += n1;
225530 }else{
225531 *pOut++ = '\0';
225532 }
225533 a1 += n1;
225534 a2 += n2;
225535 }
225536 pBuf->nBuf = (pOut - pBuf->aBuf);
225537 }
225538 }
225539}
225540
225541/*
225542** pIter is configured to iterate through a changeset. This function rebases
225543** that changeset according to the current configuration of the rebaser
225544** object passed as the first argument. If no error occurs and argument xOutput
225545** is not NULL, then the changeset is returned to the caller by invoking
225546** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
225547** then (*ppOut) is set to point to a buffer containing the rebased changeset
225548** before this function returns. In this case (*pnOut) is set to the size of
225549** the buffer in bytes. It is the responsibility of the caller to eventually
225550** free the (*ppOut) buffer using sqlite3_free().
225551**
225552** If an error occurs, an SQLite error code is returned. If ppOut and
225553** pnOut are not NULL, then the two output parameters are set to 0 before
225554** returning.
225555*/
225556static int sessionRebase(
225557 sqlite3_rebaser *p, /* Rebaser hash table */
225558 sqlite3_changeset_iter *pIter, /* Input data */
225559 int (*xOutput)(void *pOut, const void *pData, int nData),
225560 void *pOut, /* Context for xOutput callback */
225561 int *pnOut, /* OUT: Number of bytes in output changeset */
225562 void **ppOut /* OUT: Inverse of pChangeset */
225563){
225564 int rc = SQLITE_OK;
225565 u8 *aRec = 0;
225566 int nRec = 0;
225567 int bNew = 0;
225568 SessionTable *pTab = 0;
225569 SessionBuffer sOut = {0,0,0};
225570
225571 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
225572 SessionChange *pChange = 0;
225573 int bDone = 0;
225574
225575 if( bNew ){
225576 const char *zTab = pIter->zTab;
225577 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
225578 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
225579 }
225580 bNew = 0;
225581
225582 /* A patchset may not be rebased */
225583 if( pIter->bPatchset ){
225584 rc = SQLITE_ERROR;
225585 }
225586
225587 /* Append a table header to the output for this new table */
225588 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
225589 sessionAppendVarint(&sOut, pIter->nCol, &rc);
225590 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
225591 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
225592 }
225593
225594 if( pTab && rc==SQLITE_OK ){
225595 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
225596
225597 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
225598 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
225599 break;
225600 }
225601 }
225602 }
225603
225604 if( pChange ){
225605 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
225606 switch( pIter->op ){
225607 case SQLITE_INSERT:
225608 if( pChange->op==SQLITE_INSERT ){
225609 bDone = 1;
225610 if( pChange->bIndirect==0 ){
225611 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
225612 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
225613 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
225614 sessionAppendBlob(&sOut, aRec, nRec, &rc);
225615 }
225616 }
225617 break;
225618
225619 case SQLITE_UPDATE:
225620 bDone = 1;
225621 if( pChange->op==SQLITE_DELETE ){
225622 if( pChange->bIndirect==0 ){
225623 u8 *pCsr = aRec;
225624 sessionSkipRecord(&pCsr, pIter->nCol);
225625 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
225626 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
225627 sessionAppendRecordMerge(&sOut, pIter->nCol,
225628 pCsr, nRec-(pCsr-aRec),
225629 pChange->aRecord, pChange->nRecord, &rc
225630 );
225631 }
225632 }else{
225633 sessionAppendPartialUpdate(&sOut, pIter,
225634 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
225635 );
225636 }
225637 break;
225638
225639 default:
225640 assert( pIter->op==SQLITE_DELETE );
225641 bDone = 1;
225642 if( pChange->op==SQLITE_INSERT ){
225643 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
225644 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
225645 sessionAppendRecordMerge(&sOut, pIter->nCol,
225646 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
225647 );
225648 }
225649 break;
225650 }
225651 }
225652
225653 if( bDone==0 ){
225654 sessionAppendByte(&sOut, pIter->op, &rc);
225655 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
225656 sessionAppendBlob(&sOut, aRec, nRec, &rc);
225657 }
225658 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
225659 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
225660 sOut.nBuf = 0;
225661 }
225662 if( rc ) break;
225663 }
225664
225665 if( rc!=SQLITE_OK ){
225666 sqlite3_free(sOut.aBuf);
225667 memset(&sOut, 0, sizeof(sOut));
225668 }
225669
225670 if( rc==SQLITE_OK ){
225671 if( xOutput ){
225672 if( sOut.nBuf>0 ){
225673 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
225674 }
225675 }else if( ppOut ){
225676 *ppOut = (void*)sOut.aBuf;
225677 *pnOut = sOut.nBuf;
225678 sOut.aBuf = 0;
225679 }
225680 }
225681 sqlite3_free(sOut.aBuf);
225682 return rc;
225683}
225684
225685/*
225686** Create a new rebaser object.
225687*/
225688SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
225689 int rc = SQLITE_OK;
225690 sqlite3_rebaser *pNew;
225691
225692 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
225693 if( pNew==0 ){
225694 rc = SQLITE_NOMEM;
225695 }else{
225696 memset(pNew, 0, sizeof(sqlite3_rebaser));
225697 }
225698 *ppNew = pNew;
225699 return rc;
225700}
225701
225702/*
225703** Call this one or more times to configure a rebaser.
225704*/
225705SQLITE_API int sqlite3rebaser_configure(
225706 sqlite3_rebaser *p,
225707 int nRebase, const void *pRebase
225708){
225709 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
225710 int rc; /* Return code */
225711 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
225712 if( rc==SQLITE_OK ){
225713 rc = sessionChangesetToHash(pIter, &p->grp, 1);
225714 }
225715 sqlite3changeset_finalize(pIter);
225716 return rc;
225717}
225718
225719/*
225720** Rebase a changeset according to current rebaser configuration
225721*/
225722SQLITE_API int sqlite3rebaser_rebase(
225723 sqlite3_rebaser *p,
225724 int nIn, const void *pIn,
225725 int *pnOut, void **ppOut
225726){
225727 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
225728 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
225729
225730 if( rc==SQLITE_OK ){
225731 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
225732 sqlite3changeset_finalize(pIter);
225733 }
225734
225735 return rc;
225736}
225737
225738/*
225739** Rebase a changeset according to current rebaser configuration
225740*/
225741SQLITE_API int sqlite3rebaser_rebase_strm(
225742 sqlite3_rebaser *p,
225743 int (*xInput)(void *pIn, void *pData, int *pnData),
225744 void *pIn,
225745 int (*xOutput)(void *pOut, const void *pData, int nData),
225746 void *pOut
225747){
225748 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
225749 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
225750
225751 if( rc==SQLITE_OK ){
225752 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
225753 sqlite3changeset_finalize(pIter);
225754 }
225755
225756 return rc;
225757}
225758
225759/*
225760** Destroy a rebaser object
225761*/
225762SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
225763 if( p ){
225764 sessionDeleteTable(0, p->grp.pList);
225765 sqlite3_free(p);
225766 }
225767}
225768
225769/*
225770** Global configuration
225771*/
225772SQLITE_API int sqlite3session_config(int op, void *pArg){
225773 int rc = SQLITE_OK;
225774 switch( op ){
225775 case SQLITE_SESSION_CONFIG_STRMSIZE: {
225776 int *pInt = (int*)pArg;
225777 if( *pInt>0 ){
225778 sessions_strm_chunk_size = *pInt;
225779 }
225780 *pInt = sessions_strm_chunk_size;
225781 break;
225782 }
225783 default:
225784 rc = SQLITE_MISUSE;
225785 break;
225786 }
225787 return rc;
225788}
225789
225790#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
225791
225792/************** End of sqlite3session.c **************************************/
225793/************** Begin file fts5.c ********************************************/
225794
225795
225796#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
225797
225798#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
225799# define NDEBUG 1
225800#endif
225801#if defined(NDEBUG) && defined(SQLITE_DEBUG)
225802# undef NDEBUG
225803#endif
225804
225805/*
225806** 2014 May 31
225807**
225808** The author disclaims copyright to this source code. In place of
225809** a legal notice, here is a blessing:
225810**
225811** May you do good and not evil.
225812** May you find forgiveness for yourself and forgive others.
225813** May you share freely, never taking more than you give.
225814**
225815******************************************************************************
225816**
225817** Interfaces to extend FTS5. Using the interfaces defined in this file,
225818** FTS5 may be extended with:
225819**
225820** * custom tokenizers, and
225821** * custom auxiliary functions.
225822*/
225823
225824
225825#ifndef _FTS5_H
225826#define _FTS5_H
225827
225828/* #include "sqlite3.h" */
225829
225830#if 0
225831extern "C" {
225832#endif
225833
225834/*************************************************************************
225835** CUSTOM AUXILIARY FUNCTIONS
225836**
225837** Virtual table implementations may overload SQL functions by implementing
225838** the sqlite3_module.xFindFunction() method.
225839*/
225840
225841typedef struct Fts5ExtensionApi Fts5ExtensionApi;
225842typedef struct Fts5Context Fts5Context;
225843typedef struct Fts5PhraseIter Fts5PhraseIter;
225844
225845typedef void (*fts5_extension_function)(
225846 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
225847 Fts5Context *pFts, /* First arg to pass to pApi functions */
225848 sqlite3_context *pCtx, /* Context for returning result/error */
225849 int nVal, /* Number of values in apVal[] array */
225850 sqlite3_value **apVal /* Array of trailing arguments */
225851);
225852
225853struct Fts5PhraseIter {
225854 const unsigned char *a;
225855 const unsigned char *b;
225856};
225857
225858/*
225859** EXTENSION API FUNCTIONS
225860**
225861** xUserData(pFts):
225862** Return a copy of the context pointer the extension function was
225863** registered with.
225864**
225865** xColumnTotalSize(pFts, iCol, pnToken):
225866** If parameter iCol is less than zero, set output variable *pnToken
225867** to the total number of tokens in the FTS5 table. Or, if iCol is
225868** non-negative but less than the number of columns in the table, return
225869** the total number of tokens in column iCol, considering all rows in
225870** the FTS5 table.
225871**
225872** If parameter iCol is greater than or equal to the number of columns
225873** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
225874** an OOM condition or IO error), an appropriate SQLite error code is
225875** returned.
225876**
225877** xColumnCount(pFts):
225878** Return the number of columns in the table.
225879**
225880** xColumnSize(pFts, iCol, pnToken):
225881** If parameter iCol is less than zero, set output variable *pnToken
225882** to the total number of tokens in the current row. Or, if iCol is
225883** non-negative but less than the number of columns in the table, set
225884** *pnToken to the number of tokens in column iCol of the current row.
225885**
225886** If parameter iCol is greater than or equal to the number of columns
225887** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
225888** an OOM condition or IO error), an appropriate SQLite error code is
225889** returned.
225890**
225891** This function may be quite inefficient if used with an FTS5 table
225892** created with the "columnsize=0" option.
225893**
225894** xColumnText:
225895** This function attempts to retrieve the text of column iCol of the
225896** current document. If successful, (*pz) is set to point to a buffer
225897** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
225898** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
225899** if an error occurs, an SQLite error code is returned and the final values
225900** of (*pz) and (*pn) are undefined.
225901**
225902** xPhraseCount:
225903** Returns the number of phrases in the current query expression.
225904**
225905** xPhraseSize:
225906** Returns the number of tokens in phrase iPhrase of the query. Phrases
225907** are numbered starting from zero.
225908**
225909** xInstCount:
225910** Set *pnInst to the total number of occurrences of all phrases within
225911** the query within the current row. Return SQLITE_OK if successful, or
225912** an error code (i.e. SQLITE_NOMEM) if an error occurs.
225913**
225914** This API can be quite slow if used with an FTS5 table created with the
225915** "detail=none" or "detail=column" option. If the FTS5 table is created
225916** with either "detail=none" or "detail=column" and "content=" option
225917** (i.e. if it is a contentless table), then this API always returns 0.
225918**
225919** xInst:
225920** Query for the details of phrase match iIdx within the current row.
225921** Phrase matches are numbered starting from zero, so the iIdx argument
225922** should be greater than or equal to zero and smaller than the value
225923** output by xInstCount().
225924**
225925** Usually, output parameter *piPhrase is set to the phrase number, *piCol
225926** to the column in which it occurs and *piOff the token offset of the
225927** first token of the phrase. Returns SQLITE_OK if successful, or an error
225928** code (i.e. SQLITE_NOMEM) if an error occurs.
225929**
225930** This API can be quite slow if used with an FTS5 table created with the
225931** "detail=none" or "detail=column" option.
225932**
225933** xRowid:
225934** Returns the rowid of the current row.
225935**
225936** xTokenize:
225937** Tokenize text using the tokenizer belonging to the FTS5 table.
225938**
225939** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
225940** This API function is used to query the FTS table for phrase iPhrase
225941** of the current query. Specifically, a query equivalent to:
225942**
225943** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
225944**
225945** with $p set to a phrase equivalent to the phrase iPhrase of the
225946** current query is executed. Any column filter that applies to
225947** phrase iPhrase of the current query is included in $p. For each
225948** row visited, the callback function passed as the fourth argument
225949** is invoked. The context and API objects passed to the callback
225950** function may be used to access the properties of each matched row.
225951** Invoking Api.xUserData() returns a copy of the pointer passed as
225952** the third argument to pUserData.
225953**
225954** If the callback function returns any value other than SQLITE_OK, the
225955** query is abandoned and the xQueryPhrase function returns immediately.
225956** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
225957** Otherwise, the error code is propagated upwards.
225958**
225959** If the query runs to completion without incident, SQLITE_OK is returned.
225960** Or, if some error occurs before the query completes or is aborted by
225961** the callback, an SQLite error code is returned.
225962**
225963**
225964** xSetAuxdata(pFts5, pAux, xDelete)
225965**
225966** Save the pointer passed as the second argument as the extension function's
225967** "auxiliary data". The pointer may then be retrieved by the current or any
225968** future invocation of the same fts5 extension function made as part of
225969** the same MATCH query using the xGetAuxdata() API.
225970**
225971** Each extension function is allocated a single auxiliary data slot for
225972** each FTS query (MATCH expression). If the extension function is invoked
225973** more than once for a single FTS query, then all invocations share a
225974** single auxiliary data context.
225975**
225976** If there is already an auxiliary data pointer when this function is
225977** invoked, then it is replaced by the new pointer. If an xDelete callback
225978** was specified along with the original pointer, it is invoked at this
225979** point.
225980**
225981** The xDelete callback, if one is specified, is also invoked on the
225982** auxiliary data pointer after the FTS5 query has finished.
225983**
225984** If an error (e.g. an OOM condition) occurs within this function,
225985** the auxiliary data is set to NULL and an error code returned. If the
225986** xDelete parameter was not NULL, it is invoked on the auxiliary data
225987** pointer before returning.
225988**
225989**
225990** xGetAuxdata(pFts5, bClear)
225991**
225992** Returns the current auxiliary data pointer for the fts5 extension
225993** function. See the xSetAuxdata() method for details.
225994**
225995** If the bClear argument is non-zero, then the auxiliary data is cleared
225996** (set to NULL) before this function returns. In this case the xDelete,
225997** if any, is not invoked.
225998**
225999**
226000** xRowCount(pFts5, pnRow)
226001**
226002** This function is used to retrieve the total number of rows in the table.
226003** In other words, the same value that would be returned by:
226004**
226005** SELECT count(*) FROM ftstable;
226006**
226007** xPhraseFirst()
226008** This function is used, along with type Fts5PhraseIter and the xPhraseNext
226009** method, to iterate through all instances of a single query phrase within
226010** the current row. This is the same information as is accessible via the
226011** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
226012** to use, this API may be faster under some circumstances. To iterate
226013** through instances of phrase iPhrase, use the following code:
226014**
226015** Fts5PhraseIter iter;
226016** int iCol, iOff;
226017** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
226018** iCol>=0;
226019** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
226020** ){
226021** // An instance of phrase iPhrase at offset iOff of column iCol
226022** }
226023**
226024** The Fts5PhraseIter structure is defined above. Applications should not
226025** modify this structure directly - it should only be used as shown above
226026** with the xPhraseFirst() and xPhraseNext() API methods (and by
226027** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
226028**
226029** This API can be quite slow if used with an FTS5 table created with the
226030** "detail=none" or "detail=column" option. If the FTS5 table is created
226031** with either "detail=none" or "detail=column" and "content=" option
226032** (i.e. if it is a contentless table), then this API always iterates
226033** through an empty set (all calls to xPhraseFirst() set iCol to -1).
226034**
226035** xPhraseNext()
226036** See xPhraseFirst above.
226037**
226038** xPhraseFirstColumn()
226039** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
226040** and xPhraseNext() APIs described above. The difference is that instead
226041** of iterating through all instances of a phrase in the current row, these
226042** APIs are used to iterate through the set of columns in the current row
226043** that contain one or more instances of a specified phrase. For example:
226044**
226045** Fts5PhraseIter iter;
226046** int iCol;
226047** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
226048** iCol>=0;
226049** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
226050** ){
226051** // Column iCol contains at least one instance of phrase iPhrase
226052** }
226053**
226054** This API can be quite slow if used with an FTS5 table created with the
226055** "detail=none" option. If the FTS5 table is created with either
226056** "detail=none" "content=" option (i.e. if it is a contentless table),
226057** then this API always iterates through an empty set (all calls to
226058** xPhraseFirstColumn() set iCol to -1).
226059**
226060** The information accessed using this API and its companion
226061** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
226062** (or xInst/xInstCount). The chief advantage of this API is that it is
226063** significantly more efficient than those alternatives when used with
226064** "detail=column" tables.
226065**
226066** xPhraseNextColumn()
226067** See xPhraseFirstColumn above.
226068*/
226069struct Fts5ExtensionApi {
226070 int iVersion; /* Currently always set to 2 */
226071
226072 void *(*xUserData)(Fts5Context*);
226073
226074 int (*xColumnCount)(Fts5Context*);
226075 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
226076 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
226077
226078 int (*xTokenize)(Fts5Context*,
226079 const char *pText, int nText, /* Text to tokenize */
226080 void *pCtx, /* Context passed to xToken() */
226081 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
226082 );
226083
226084 int (*xPhraseCount)(Fts5Context*);
226085 int (*xPhraseSize)(Fts5Context*, int iPhrase);
226086
226087 int (*xInstCount)(Fts5Context*, int *pnInst);
226088 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
226089
226090 sqlite3_int64 (*xRowid)(Fts5Context*);
226091 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
226092 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
226093
226094 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
226095 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
226096 );
226097 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
226098 void *(*xGetAuxdata)(Fts5Context*, int bClear);
226099
226100 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
226101 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
226102
226103 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
226104 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
226105};
226106
226107/*
226108** CUSTOM AUXILIARY FUNCTIONS
226109*************************************************************************/
226110
226111/*************************************************************************
226112** CUSTOM TOKENIZERS
226113**
226114** Applications may also register custom tokenizer types. A tokenizer
226115** is registered by providing fts5 with a populated instance of the
226116** following structure. All structure methods must be defined, setting
226117** any member of the fts5_tokenizer struct to NULL leads to undefined
226118** behaviour. The structure methods are expected to function as follows:
226119**
226120** xCreate:
226121** This function is used to allocate and initialize a tokenizer instance.
226122** A tokenizer instance is required to actually tokenize text.
226123**
226124** The first argument passed to this function is a copy of the (void*)
226125** pointer provided by the application when the fts5_tokenizer object
226126** was registered with FTS5 (the third argument to xCreateTokenizer()).
226127** The second and third arguments are an array of nul-terminated strings
226128** containing the tokenizer arguments, if any, specified following the
226129** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
226130** to create the FTS5 table.
226131**
226132** The final argument is an output variable. If successful, (*ppOut)
226133** should be set to point to the new tokenizer handle and SQLITE_OK
226134** returned. If an error occurs, some value other than SQLITE_OK should
226135** be returned. In this case, fts5 assumes that the final value of *ppOut
226136** is undefined.
226137**
226138** xDelete:
226139** This function is invoked to delete a tokenizer handle previously
226140** allocated using xCreate(). Fts5 guarantees that this function will
226141** be invoked exactly once for each successful call to xCreate().
226142**
226143** xTokenize:
226144** This function is expected to tokenize the nText byte string indicated
226145** by argument pText. pText may or may not be nul-terminated. The first
226146** argument passed to this function is a pointer to an Fts5Tokenizer object
226147** returned by an earlier call to xCreate().
226148**
226149** The second argument indicates the reason that FTS5 is requesting
226150** tokenization of the supplied text. This is always one of the following
226151** four values:
226152**
226153** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
226154** or removed from the FTS table. The tokenizer is being invoked to
226155** determine the set of tokens to add to (or delete from) the
226156** FTS index.
226157**
226158** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
226159** against the FTS index. The tokenizer is being called to tokenize
226160** a bareword or quoted string specified as part of the query.
226161**
226162** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
226163** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
226164** followed by a "*" character, indicating that the last token
226165** returned by the tokenizer will be treated as a token prefix.
226166**
226167** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
226168** satisfy an fts5_api.xTokenize() request made by an auxiliary
226169** function. Or an fts5_api.xColumnSize() request made by the same
226170** on a columnsize=0 database.
226171** </ul>
226172**
226173** For each token in the input string, the supplied callback xToken() must
226174** be invoked. The first argument to it should be a copy of the pointer
226175** passed as the second argument to xTokenize(). The third and fourth
226176** arguments are a pointer to a buffer containing the token text, and the
226177** size of the token in bytes. The 4th and 5th arguments are the byte offsets
226178** of the first byte of and first byte immediately following the text from
226179** which the token is derived within the input.
226180**
226181** The second argument passed to the xToken() callback ("tflags") should
226182** normally be set to 0. The exception is if the tokenizer supports
226183** synonyms. In this case see the discussion below for details.
226184**
226185** FTS5 assumes the xToken() callback is invoked for each token in the
226186** order that they occur within the input text.
226187**
226188** If an xToken() callback returns any value other than SQLITE_OK, then
226189** the tokenization should be abandoned and the xTokenize() method should
226190** immediately return a copy of the xToken() return value. Or, if the
226191** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
226192** if an error occurs with the xTokenize() implementation itself, it
226193** may abandon the tokenization and return any error code other than
226194** SQLITE_OK or SQLITE_DONE.
226195**
226196** SYNONYM SUPPORT
226197**
226198** Custom tokenizers may also support synonyms. Consider a case in which a
226199** user wishes to query for a phrase such as "first place". Using the
226200** built-in tokenizers, the FTS5 query 'first + place' will match instances
226201** of "first place" within the document set, but not alternative forms
226202** such as "1st place". In some applications, it would be better to match
226203** all instances of "first place" or "1st place" regardless of which form
226204** the user specified in the MATCH query text.
226205**
226206** There are several ways to approach this in FTS5:
226207**
226208** <ol><li> By mapping all synonyms to a single token. In this case, using
226209** the above example, this means that the tokenizer returns the
226210** same token for inputs "first" and "1st". Say that token is in
226211** fact "first", so that when the user inserts the document "I won
226212** 1st place" entries are added to the index for tokens "i", "won",
226213** "first" and "place". If the user then queries for '1st + place',
226214** the tokenizer substitutes "first" for "1st" and the query works
226215** as expected.
226216**
226217** <li> By querying the index for all synonyms of each query term
226218** separately. In this case, when tokenizing query text, the
226219** tokenizer may provide multiple synonyms for a single term
226220** within the document. FTS5 then queries the index for each
226221** synonym individually. For example, faced with the query:
226222**
226223** <codeblock>
226224** ... MATCH 'first place'</codeblock>
226225**
226226** the tokenizer offers both "1st" and "first" as synonyms for the
226227** first token in the MATCH query and FTS5 effectively runs a query
226228** similar to:
226229**
226230** <codeblock>
226231** ... MATCH '(first OR 1st) place'</codeblock>
226232**
226233** except that, for the purposes of auxiliary functions, the query
226234** still appears to contain just two phrases - "(first OR 1st)"
226235** being treated as a single phrase.
226236**
226237** <li> By adding multiple synonyms for a single term to the FTS index.
226238** Using this method, when tokenizing document text, the tokenizer
226239** provides multiple synonyms for each token. So that when a
226240** document such as "I won first place" is tokenized, entries are
226241** added to the FTS index for "i", "won", "first", "1st" and
226242** "place".
226243**
226244** This way, even if the tokenizer does not provide synonyms
226245** when tokenizing query text (it should not - to do so would be
226246** inefficient), it doesn't matter if the user queries for
226247** 'first + place' or '1st + place', as there are entries in the
226248** FTS index corresponding to both forms of the first token.
226249** </ol>
226250**
226251** Whether it is parsing document or query text, any call to xToken that
226252** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
226253** is considered to supply a synonym for the previous token. For example,
226254** when parsing the document "I won first place", a tokenizer that supports
226255** synonyms would call xToken() 5 times, as follows:
226256**
226257** <codeblock>
226258** xToken(pCtx, 0, "i", 1, 0, 1);
226259** xToken(pCtx, 0, "won", 3, 2, 5);
226260** xToken(pCtx, 0, "first", 5, 6, 11);
226261** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
226262** xToken(pCtx, 0, "place", 5, 12, 17);
226263**</codeblock>
226264**
226265** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
226266** xToken() is called. Multiple synonyms may be specified for a single token
226267** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
226268** There is no limit to the number of synonyms that may be provided for a
226269** single token.
226270**
226271** In many cases, method (1) above is the best approach. It does not add
226272** extra data to the FTS index or require FTS5 to query for multiple terms,
226273** so it is efficient in terms of disk space and query speed. However, it
226274** does not support prefix queries very well. If, as suggested above, the
226275** token "first" is substituted for "1st" by the tokenizer, then the query:
226276**
226277** <codeblock>
226278** ... MATCH '1s*'</codeblock>
226279**
226280** will not match documents that contain the token "1st" (as the tokenizer
226281** will probably not map "1s" to any prefix of "first").
226282**
226283** For full prefix support, method (3) may be preferred. In this case,
226284** because the index contains entries for both "first" and "1st", prefix
226285** queries such as 'fi*' or '1s*' will match correctly. However, because
226286** extra entries are added to the FTS index, this method uses more space
226287** within the database.
226288**
226289** Method (2) offers a midpoint between (1) and (3). Using this method,
226290** a query such as '1s*' will match documents that contain the literal
226291** token "1st", but not "first" (assuming the tokenizer is not able to
226292** provide synonyms for prefixes). However, a non-prefix query like '1st'
226293** will match against "1st" and "first". This method does not require
226294** extra disk space, as no extra entries are added to the FTS index.
226295** On the other hand, it may require more CPU cycles to run MATCH queries,
226296** as separate queries of the FTS index are required for each synonym.
226297**
226298** When using methods (2) or (3), it is important that the tokenizer only
226299** provide synonyms when tokenizing document text (method (3)) or query
226300** text (method (2)), not both. Doing so will not cause any errors, but is
226301** inefficient.
226302*/
226303typedef struct Fts5Tokenizer Fts5Tokenizer;
226304typedef struct fts5_tokenizer fts5_tokenizer;
226305struct fts5_tokenizer {
226306 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
226307 void (*xDelete)(Fts5Tokenizer*);
226308 int (*xTokenize)(Fts5Tokenizer*,
226309 void *pCtx,
226310 int flags, /* Mask of FTS5_TOKENIZE_* flags */
226311 const char *pText, int nText,
226312 int (*xToken)(
226313 void *pCtx, /* Copy of 2nd argument to xTokenize() */
226314 int tflags, /* Mask of FTS5_TOKEN_* flags */
226315 const char *pToken, /* Pointer to buffer containing token */
226316 int nToken, /* Size of token in bytes */
226317 int iStart, /* Byte offset of token within input text */
226318 int iEnd /* Byte offset of end of token within input text */
226319 )
226320 );
226321};
226322
226323/* Flags that may be passed as the third argument to xTokenize() */
226324#define FTS5_TOKENIZE_QUERY 0x0001
226325#define FTS5_TOKENIZE_PREFIX 0x0002
226326#define FTS5_TOKENIZE_DOCUMENT 0x0004
226327#define FTS5_TOKENIZE_AUX 0x0008
226328
226329/* Flags that may be passed by the tokenizer implementation back to FTS5
226330** as the third argument to the supplied xToken callback. */
226331#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
226332
226333/*
226334** END OF CUSTOM TOKENIZERS
226335*************************************************************************/
226336
226337/*************************************************************************
226338** FTS5 EXTENSION REGISTRATION API
226339*/
226340typedef struct fts5_api fts5_api;
226341struct fts5_api {
226342 int iVersion; /* Currently always set to 2 */
226343
226344 /* Create a new tokenizer */
226345 int (*xCreateTokenizer)(
226346 fts5_api *pApi,
226347 const char *zName,
226348 void *pUserData,
226349 fts5_tokenizer *pTokenizer,
226350 void (*xDestroy)(void*)
226351 );
226352
226353 /* Find an existing tokenizer */
226354 int (*xFindTokenizer)(
226355 fts5_api *pApi,
226356 const char *zName,
226357 void **ppUserData,
226358 fts5_tokenizer *pTokenizer
226359 );
226360
226361 /* Create a new auxiliary function */
226362 int (*xCreateFunction)(
226363 fts5_api *pApi,
226364 const char *zName,
226365 void *pUserData,
226366 fts5_extension_function xFunction,
226367 void (*xDestroy)(void*)
226368 );
226369};
226370
226371/*
226372** END OF REGISTRATION API
226373*************************************************************************/
226374
226375#if 0
226376} /* end of the 'extern "C"' block */
226377#endif
226378
226379#endif /* _FTS5_H */
226380
226381/*
226382** 2014 May 31
226383**
226384** The author disclaims copyright to this source code. In place of
226385** a legal notice, here is a blessing:
226386**
226387** May you do good and not evil.
226388** May you find forgiveness for yourself and forgive others.
226389** May you share freely, never taking more than you give.
226390**
226391******************************************************************************
226392**
226393*/
226394#ifndef _FTS5INT_H
226395#define _FTS5INT_H
226396
226397/* #include "fts5.h" */
226398/* #include "sqlite3ext.h" */
226399SQLITE_EXTENSION_INIT1
226400
226401/* #include <string.h> */
226402/* #include <assert.h> */
226403
226404#ifndef SQLITE_AMALGAMATION
226405
226406typedef unsigned char u8;
226407typedef unsigned int u32;
226408typedef unsigned short u16;
226409typedef short i16;
226410typedef sqlite3_int64 i64;
226411typedef sqlite3_uint64 u64;
226412
226413#ifndef ArraySize
226414# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
226415#endif
226416
226417#define testcase(x)
226418
226419#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
226420# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
226421#endif
226422#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
226423# define ALWAYS(X) (1)
226424# define NEVER(X) (0)
226425#elif !defined(NDEBUG)
226426# define ALWAYS(X) ((X)?1:(assert(0),0))
226427# define NEVER(X) ((X)?(assert(0),1):0)
226428#else
226429# define ALWAYS(X) (X)
226430# define NEVER(X) (X)
226431#endif
226432
226433#define MIN(x,y) (((x) < (y)) ? (x) : (y))
226434#define MAX(x,y) (((x) > (y)) ? (x) : (y))
226435
226436/*
226437** Constants for the largest and smallest possible 64-bit signed integers.
226438*/
226439# define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
226440# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
226441
226442#endif
226443
226444/* Truncate very long tokens to this many bytes. Hard limit is
226445** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
226446** field that occurs at the start of each leaf page (see fts5_index.c). */
226447#define FTS5_MAX_TOKEN_SIZE 32768
226448
226449/*
226450** Maximum number of prefix indexes on single FTS5 table. This must be
226451** less than 32. If it is set to anything large than that, an #error
226452** directive in fts5_index.c will cause the build to fail.
226453*/
226454#define FTS5_MAX_PREFIX_INDEXES 31
226455
226456/*
226457** Maximum segments permitted in a single index
226458*/
226459#define FTS5_MAX_SEGMENT 2000
226460
226461#define FTS5_DEFAULT_NEARDIST 10
226462#define FTS5_DEFAULT_RANK "bm25"
226463
226464/* Name of rank and rowid columns */
226465#define FTS5_RANK_NAME "rank"
226466#define FTS5_ROWID_NAME "rowid"
226467
226468#ifdef SQLITE_DEBUG
226469# define FTS5_CORRUPT sqlite3Fts5Corrupt()
226470static int sqlite3Fts5Corrupt(void);
226471#else
226472# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
226473#endif
226474
226475/*
226476** The assert_nc() macro is similar to the assert() macro, except that it
226477** is used for assert() conditions that are true only if it can be
226478** guranteed that the database is not corrupt.
226479*/
226480#ifdef SQLITE_DEBUG
226481SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
226482# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
226483#else
226484# define assert_nc(x) assert(x)
226485#endif
226486
226487/*
226488** A version of memcmp() that does not cause asan errors if one of the pointer
226489** parameters is NULL and the number of bytes to compare is zero.
226490*/
226491#define fts5Memcmp(s1, s2, n) ((n)<=0 ? 0 : memcmp((s1), (s2), (n)))
226492
226493/* Mark a function parameter as unused, to suppress nuisance compiler
226494** warnings. */
226495#ifndef UNUSED_PARAM
226496# define UNUSED_PARAM(X) (void)(X)
226497#endif
226498
226499#ifndef UNUSED_PARAM2
226500# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
226501#endif
226502
226503typedef struct Fts5Global Fts5Global;
226504typedef struct Fts5Colset Fts5Colset;
226505
226506/* If a NEAR() clump or phrase may only match a specific set of columns,
226507** then an object of the following type is used to record the set of columns.
226508** Each entry in the aiCol[] array is a column that may be matched.
226509**
226510** This object is used by fts5_expr.c and fts5_index.c.
226511*/
226512struct Fts5Colset {
226513 int nCol;
226514 int aiCol[1];
226515};
226516
226517
226518
226519/**************************************************************************
226520** Interface to code in fts5_config.c. fts5_config.c contains contains code
226521** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
226522*/
226523
226524typedef struct Fts5Config Fts5Config;
226525
226526/*
226527** An instance of the following structure encodes all information that can
226528** be gleaned from the CREATE VIRTUAL TABLE statement.
226529**
226530** And all information loaded from the %_config table.
226531**
226532** nAutomerge:
226533** The minimum number of segments that an auto-merge operation should
226534** attempt to merge together. A value of 1 sets the object to use the
226535** compile time default. Zero disables auto-merge altogether.
226536**
226537** bContentlessDelete:
226538** True if the contentless_delete option was present in the CREATE
226539** VIRTUAL TABLE statement.
226540**
226541** zContent:
226542**
226543** zContentRowid:
226544** The value of the content_rowid= option, if one was specified. Or
226545** the string "rowid" otherwise. This text is not quoted - if it is
226546** used as part of an SQL statement it needs to be quoted appropriately.
226547**
226548** zContentExprlist:
226549**
226550** pzErrmsg:
226551** This exists in order to allow the fts5_index.c module to return a
226552** decent error message if it encounters a file-format version it does
226553** not understand.
226554**
226555** bColumnsize:
226556** True if the %_docsize table is created.
226557**
226558** bPrefixIndex:
226559** This is only used for debugging. If set to false, any prefix indexes
226560** are ignored. This value is configured using:
226561**
226562** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
226563**
226564*/
226565struct Fts5Config {
226566 sqlite3 *db; /* Database handle */
226567 char *zDb; /* Database holding FTS index (e.g. "main") */
226568 char *zName; /* Name of FTS index */
226569 int nCol; /* Number of columns */
226570 char **azCol; /* Column names */
226571 u8 *abUnindexed; /* True for unindexed columns */
226572 int nPrefix; /* Number of prefix indexes */
226573 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
226574 int eContent; /* An FTS5_CONTENT value */
226575 int bContentlessDelete; /* "contentless_delete=" option (dflt==0) */
226576 char *zContent; /* content table */
226577 char *zContentRowid; /* "content_rowid=" option value */
226578 int bColumnsize; /* "columnsize=" option value (dflt==1) */
226579 int eDetail; /* FTS5_DETAIL_XXX value */
226580 char *zContentExprlist;
226581 Fts5Tokenizer *pTok;
226582 fts5_tokenizer *pTokApi;
226583 int bLock; /* True when table is preparing statement */
226584 int ePattern; /* FTS_PATTERN_XXX constant */
226585
226586 /* Values loaded from the %_config table */
226587 int iVersion; /* fts5 file format 'version' */
226588 int iCookie; /* Incremented when %_config is modified */
226589 int pgsz; /* Approximate page size used in %_data */
226590 int nAutomerge; /* 'automerge' setting */
226591 int nCrisisMerge; /* Maximum allowed segments per level */
226592 int nUsermerge; /* 'usermerge' setting */
226593 int nHashSize; /* Bytes of memory for in-memory hash */
226594 char *zRank; /* Name of rank function */
226595 char *zRankArgs; /* Arguments to rank function */
226596 int bSecureDelete; /* 'secure-delete' */
226597 int nDeleteMerge; /* 'deletemerge' */
226598
226599 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
226600 char **pzErrmsg;
226601
226602#ifdef SQLITE_DEBUG
226603 int bPrefixIndex; /* True to use prefix-indexes */
226604#endif
226605};
226606
226607/* Current expected value of %_config table 'version' field. And
226608** the expected version if the 'secure-delete' option has ever been
226609** set on the table. */
226610#define FTS5_CURRENT_VERSION 4
226611#define FTS5_CURRENT_VERSION_SECUREDELETE 5
226612
226613#define FTS5_CONTENT_NORMAL 0
226614#define FTS5_CONTENT_NONE 1
226615#define FTS5_CONTENT_EXTERNAL 2
226616
226617#define FTS5_DETAIL_FULL 0
226618#define FTS5_DETAIL_NONE 1
226619#define FTS5_DETAIL_COLUMNS 2
226620
226621#define FTS5_PATTERN_NONE 0
226622#define FTS5_PATTERN_LIKE 65 /* matches SQLITE_INDEX_CONSTRAINT_LIKE */
226623#define FTS5_PATTERN_GLOB 66 /* matches SQLITE_INDEX_CONSTRAINT_GLOB */
226624
226625static int sqlite3Fts5ConfigParse(
226626 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
226627);
226628static void sqlite3Fts5ConfigFree(Fts5Config*);
226629
226630static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
226631
226632static int sqlite3Fts5Tokenize(
226633 Fts5Config *pConfig, /* FTS5 Configuration object */
226634 int flags, /* FTS5_TOKENIZE_* flags */
226635 const char *pText, int nText, /* Text to tokenize */
226636 void *pCtx, /* Context passed to xToken() */
226637 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
226638);
226639
226640static void sqlite3Fts5Dequote(char *z);
226641
226642/* Load the contents of the %_config table */
226643static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
226644
226645/* Set the value of a single config attribute */
226646static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
226647
226648static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
226649
226650/*
226651** End of interface to code in fts5_config.c.
226652**************************************************************************/
226653
226654/**************************************************************************
226655** Interface to code in fts5_buffer.c.
226656*/
226657
226658/*
226659** Buffer object for the incremental building of string data.
226660*/
226661typedef struct Fts5Buffer Fts5Buffer;
226662struct Fts5Buffer {
226663 u8 *p;
226664 int n;
226665 int nSpace;
226666};
226667
226668static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
226669static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
226670static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
226671static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
226672static void sqlite3Fts5BufferFree(Fts5Buffer*);
226673static void sqlite3Fts5BufferZero(Fts5Buffer*);
226674static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
226675static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
226676
226677static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
226678
226679#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
226680#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c)
226681#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
226682#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
226683#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
226684
226685#define fts5BufferGrow(pRc,pBuf,nn) ( \
226686 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
226687 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
226688)
226689
226690/* Write and decode big-endian 32-bit integer values */
226691static void sqlite3Fts5Put32(u8*, int);
226692static int sqlite3Fts5Get32(const u8*);
226693
226694#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
226695#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF)
226696
226697typedef struct Fts5PoslistReader Fts5PoslistReader;
226698struct Fts5PoslistReader {
226699 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
226700 const u8 *a; /* Position list to iterate through */
226701 int n; /* Size of buffer at a[] in bytes */
226702 int i; /* Current offset in a[] */
226703
226704 u8 bFlag; /* For client use (any custom purpose) */
226705
226706 /* Output variables */
226707 u8 bEof; /* Set to true at EOF */
226708 i64 iPos; /* (iCol<<32) + iPos */
226709};
226710static int sqlite3Fts5PoslistReaderInit(
226711 const u8 *a, int n, /* Poslist buffer to iterate through */
226712 Fts5PoslistReader *pIter /* Iterator object to initialize */
226713);
226714static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
226715
226716typedef struct Fts5PoslistWriter Fts5PoslistWriter;
226717struct Fts5PoslistWriter {
226718 i64 iPrev;
226719};
226720static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
226721static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
226722
226723static int sqlite3Fts5PoslistNext64(
226724 const u8 *a, int n, /* Buffer containing poslist */
226725 int *pi, /* IN/OUT: Offset within a[] */
226726 i64 *piOff /* IN/OUT: Current offset */
226727);
226728
226729/* Malloc utility */
226730static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
226731static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
226732
226733/* Character set tests (like isspace(), isalpha() etc.) */
226734static int sqlite3Fts5IsBareword(char t);
226735
226736
226737/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
226738typedef struct Fts5Termset Fts5Termset;
226739static int sqlite3Fts5TermsetNew(Fts5Termset**);
226740static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
226741static void sqlite3Fts5TermsetFree(Fts5Termset*);
226742
226743/*
226744** End of interface to code in fts5_buffer.c.
226745**************************************************************************/
226746
226747/**************************************************************************
226748** Interface to code in fts5_index.c. fts5_index.c contains contains code
226749** to access the data stored in the %_data table.
226750*/
226751
226752typedef struct Fts5Index Fts5Index;
226753typedef struct Fts5IndexIter Fts5IndexIter;
226754
226755struct Fts5IndexIter {
226756 i64 iRowid;
226757 const u8 *pData;
226758 int nData;
226759 u8 bEof;
226760};
226761
226762#define sqlite3Fts5IterEof(x) ((x)->bEof)
226763
226764/*
226765** Values used as part of the flags argument passed to IndexQuery().
226766*/
226767#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
226768#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
226769#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
226770#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
226771
226772/* The following are used internally by the fts5_index.c module. They are
226773** defined here only to make it easier to avoid clashes with the flags
226774** above. */
226775#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
226776#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
226777#define FTS5INDEX_QUERY_SKIPHASH 0x0040
226778
226779/*
226780** Create/destroy an Fts5Index object.
226781*/
226782static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
226783static int sqlite3Fts5IndexClose(Fts5Index *p);
226784
226785/*
226786** Return a simple checksum value based on the arguments.
226787*/
226788static u64 sqlite3Fts5IndexEntryCksum(
226789 i64 iRowid,
226790 int iCol,
226791 int iPos,
226792 int iIdx,
226793 const char *pTerm,
226794 int nTerm
226795);
226796
226797/*
226798** Argument p points to a buffer containing utf-8 text that is n bytes in
226799** size. Return the number of bytes in the nChar character prefix of the
226800** buffer, or 0 if there are less than nChar characters in total.
226801*/
226802static int sqlite3Fts5IndexCharlenToBytelen(
226803 const char *p,
226804 int nByte,
226805 int nChar
226806);
226807
226808/*
226809** Open a new iterator to iterate though all rowids that match the
226810** specified token or token prefix.
226811*/
226812static int sqlite3Fts5IndexQuery(
226813 Fts5Index *p, /* FTS index to query */
226814 const char *pToken, int nToken, /* Token (or prefix) to query for */
226815 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
226816 Fts5Colset *pColset, /* Match these columns only */
226817 Fts5IndexIter **ppIter /* OUT: New iterator object */
226818);
226819
226820/*
226821** The various operations on open token or token prefix iterators opened
226822** using sqlite3Fts5IndexQuery().
226823*/
226824static int sqlite3Fts5IterNext(Fts5IndexIter*);
226825static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
226826
226827/*
226828** Close an iterator opened by sqlite3Fts5IndexQuery().
226829*/
226830static void sqlite3Fts5IterClose(Fts5IndexIter*);
226831
226832/*
226833** Close the reader blob handle, if it is open.
226834*/
226835static void sqlite3Fts5IndexCloseReader(Fts5Index*);
226836
226837/*
226838** This interface is used by the fts5vocab module.
226839*/
226840static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
226841static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
226842static void *sqlite3Fts5StructureRef(Fts5Index*);
226843static void sqlite3Fts5StructureRelease(void*);
226844static int sqlite3Fts5StructureTest(Fts5Index*, void*);
226845
226846
226847/*
226848** Insert or remove data to or from the index. Each time a document is
226849** added to or removed from the index, this function is called one or more
226850** times.
226851**
226852** For an insert, it must be called once for each token in the new document.
226853** If the operation is a delete, it must be called (at least) once for each
226854** unique token in the document with an iCol value less than zero. The iPos
226855** argument is ignored for a delete.
226856*/
226857static int sqlite3Fts5IndexWrite(
226858 Fts5Index *p, /* Index to write to */
226859 int iCol, /* Column token appears in (-ve -> delete) */
226860 int iPos, /* Position of token within column */
226861 const char *pToken, int nToken /* Token to add or remove to or from index */
226862);
226863
226864/*
226865** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
226866** document iDocid.
226867*/
226868static int sqlite3Fts5IndexBeginWrite(
226869 Fts5Index *p, /* Index to write to */
226870 int bDelete, /* True if current operation is a delete */
226871 i64 iDocid /* Docid to add or remove data from */
226872);
226873
226874/*
226875** Flush any data stored in the in-memory hash tables to the database.
226876** Also close any open blob handles.
226877*/
226878static int sqlite3Fts5IndexSync(Fts5Index *p);
226879
226880/*
226881** Discard any data stored in the in-memory hash tables. Do not write it
226882** to the database. Additionally, assume that the contents of the %_data
226883** table may have changed on disk. So any in-memory caches of %_data
226884** records must be invalidated.
226885*/
226886static int sqlite3Fts5IndexRollback(Fts5Index *p);
226887
226888/*
226889** Get or set the "averages" values.
226890*/
226891static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
226892static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
226893
226894/*
226895** Functions called by the storage module as part of integrity-check.
226896*/
226897static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum, int bUseCksum);
226898
226899/*
226900** Called during virtual module initialization to register UDF
226901** fts5_decode() with SQLite
226902*/
226903static int sqlite3Fts5IndexInit(sqlite3*);
226904
226905static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
226906
226907/*
226908** Return the total number of entries read from the %_data table by
226909** this connection since it was created.
226910*/
226911static int sqlite3Fts5IndexReads(Fts5Index *p);
226912
226913static int sqlite3Fts5IndexReinit(Fts5Index *p);
226914static int sqlite3Fts5IndexOptimize(Fts5Index *p);
226915static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
226916static int sqlite3Fts5IndexReset(Fts5Index *p);
226917
226918static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
226919
226920static int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin);
226921static int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid);
226922
226923/*
226924** End of interface to code in fts5_index.c.
226925**************************************************************************/
226926
226927/**************************************************************************
226928** Interface to code in fts5_varint.c.
226929*/
226930static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
226931static int sqlite3Fts5GetVarintLen(u32 iVal);
226932static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
226933static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
226934
226935#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&(b))
226936#define fts5GetVarint sqlite3Fts5GetVarint
226937
226938#define fts5FastGetVarint32(a, iOff, nVal) { \
226939 nVal = (a)[iOff++]; \
226940 if( nVal & 0x80 ){ \
226941 iOff--; \
226942 iOff += fts5GetVarint32(&(a)[iOff], nVal); \
226943 } \
226944}
226945
226946
226947/*
226948** End of interface to code in fts5_varint.c.
226949**************************************************************************/
226950
226951
226952/**************************************************************************
226953** Interface to code in fts5_main.c.
226954*/
226955
226956/*
226957** Virtual-table object.
226958*/
226959typedef struct Fts5Table Fts5Table;
226960struct Fts5Table {
226961 sqlite3_vtab base; /* Base class used by SQLite core */
226962 Fts5Config *pConfig; /* Virtual table configuration */
226963 Fts5Index *pIndex; /* Full-text index */
226964};
226965
226966static int sqlite3Fts5GetTokenizer(
226967 Fts5Global*,
226968 const char **azArg,
226969 int nArg,
226970 Fts5Config*,
226971 char **pzErr
226972);
226973
226974static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
226975
226976static int sqlite3Fts5FlushToDisk(Fts5Table*);
226977
226978/*
226979** End of interface to code in fts5.c.
226980**************************************************************************/
226981
226982/**************************************************************************
226983** Interface to code in fts5_hash.c.
226984*/
226985typedef struct Fts5Hash Fts5Hash;
226986
226987/*
226988** Create a hash table, free a hash table.
226989*/
226990static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
226991static void sqlite3Fts5HashFree(Fts5Hash*);
226992
226993static int sqlite3Fts5HashWrite(
226994 Fts5Hash*,
226995 i64 iRowid, /* Rowid for this entry */
226996 int iCol, /* Column token appears in (-ve -> delete) */
226997 int iPos, /* Position of token within column */
226998 char bByte,
226999 const char *pToken, int nToken /* Token to add or remove to or from index */
227000);
227001
227002/*
227003** Empty (but do not delete) a hash table.
227004*/
227005static void sqlite3Fts5HashClear(Fts5Hash*);
227006
227007/*
227008** Return true if the hash is empty, false otherwise.
227009*/
227010static int sqlite3Fts5HashIsEmpty(Fts5Hash*);
227011
227012static int sqlite3Fts5HashQuery(
227013 Fts5Hash*, /* Hash table to query */
227014 int nPre,
227015 const char *pTerm, int nTerm, /* Query term */
227016 void **ppObj, /* OUT: Pointer to doclist for pTerm */
227017 int *pnDoclist /* OUT: Size of doclist in bytes */
227018);
227019
227020static int sqlite3Fts5HashScanInit(
227021 Fts5Hash*, /* Hash table to query */
227022 const char *pTerm, int nTerm /* Query prefix */
227023);
227024static void sqlite3Fts5HashScanNext(Fts5Hash*);
227025static int sqlite3Fts5HashScanEof(Fts5Hash*);
227026static void sqlite3Fts5HashScanEntry(Fts5Hash *,
227027 const char **pzTerm, /* OUT: term (nul-terminated) */
227028 const u8 **ppDoclist, /* OUT: pointer to doclist */
227029 int *pnDoclist /* OUT: size of doclist in bytes */
227030);
227031
227032
227033
227034/*
227035** End of interface to code in fts5_hash.c.
227036**************************************************************************/
227037
227038/**************************************************************************
227039** Interface to code in fts5_storage.c. fts5_storage.c contains contains
227040** code to access the data stored in the %_content and %_docsize tables.
227041*/
227042
227043#define FTS5_STMT_SCAN_ASC 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
227044#define FTS5_STMT_SCAN_DESC 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
227045#define FTS5_STMT_LOOKUP 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
227046
227047typedef struct Fts5Storage Fts5Storage;
227048
227049static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
227050static int sqlite3Fts5StorageClose(Fts5Storage *p);
227051static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
227052
227053static int sqlite3Fts5DropAll(Fts5Config*);
227054static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
227055
227056static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
227057static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
227058static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
227059
227060static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg);
227061
227062static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
227063static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
227064
227065static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
227066static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
227067static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
227068
227069static int sqlite3Fts5StorageSync(Fts5Storage *p);
227070static int sqlite3Fts5StorageRollback(Fts5Storage *p);
227071
227072static int sqlite3Fts5StorageConfigValue(
227073 Fts5Storage *p, const char*, sqlite3_value*, int
227074);
227075
227076static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
227077static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
227078static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
227079static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
227080static int sqlite3Fts5StorageReset(Fts5Storage *p);
227081
227082/*
227083** End of interface to code in fts5_storage.c.
227084**************************************************************************/
227085
227086
227087/**************************************************************************
227088** Interface to code in fts5_expr.c.
227089*/
227090typedef struct Fts5Expr Fts5Expr;
227091typedef struct Fts5ExprNode Fts5ExprNode;
227092typedef struct Fts5Parse Fts5Parse;
227093typedef struct Fts5Token Fts5Token;
227094typedef struct Fts5ExprPhrase Fts5ExprPhrase;
227095typedef struct Fts5ExprNearset Fts5ExprNearset;
227096
227097struct Fts5Token {
227098 const char *p; /* Token text (not NULL terminated) */
227099 int n; /* Size of buffer p in bytes */
227100};
227101
227102/* Parse a MATCH expression. */
227103static int sqlite3Fts5ExprNew(
227104 Fts5Config *pConfig,
227105 int bPhraseToAnd,
227106 int iCol, /* Column on LHS of MATCH operator */
227107 const char *zExpr,
227108 Fts5Expr **ppNew,
227109 char **pzErr
227110);
227111static int sqlite3Fts5ExprPattern(
227112 Fts5Config *pConfig,
227113 int bGlob,
227114 int iCol,
227115 const char *zText,
227116 Fts5Expr **pp
227117);
227118
227119/*
227120** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
227121** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
227122** rc = sqlite3Fts5ExprNext(pExpr)
227123** ){
227124** // The document with rowid iRowid matches the expression!
227125** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
227126** }
227127*/
227128static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
227129static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
227130static int sqlite3Fts5ExprEof(Fts5Expr*);
227131static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
227132
227133static void sqlite3Fts5ExprFree(Fts5Expr*);
227134static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
227135
227136/* Called during startup to register a UDF with SQLite */
227137static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
227138
227139static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
227140static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
227141static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
227142
227143typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
227144static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
227145static int sqlite3Fts5ExprPopulatePoslists(
227146 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
227147);
227148static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
227149
227150static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
227151
227152static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
227153
227154/*******************************************
227155** The fts5_expr.c API above this point is used by the other hand-written
227156** C code in this module. The interfaces below this point are called by
227157** the parser code in fts5parse.y. */
227158
227159static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
227160
227161static Fts5ExprNode *sqlite3Fts5ParseNode(
227162 Fts5Parse *pParse,
227163 int eType,
227164 Fts5ExprNode *pLeft,
227165 Fts5ExprNode *pRight,
227166 Fts5ExprNearset *pNear
227167);
227168
227169static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
227170 Fts5Parse *pParse,
227171 Fts5ExprNode *pLeft,
227172 Fts5ExprNode *pRight
227173);
227174
227175static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
227176 Fts5Parse *pParse,
227177 Fts5ExprPhrase *pPhrase,
227178 Fts5Token *pToken,
227179 int bPrefix
227180);
227181
227182static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
227183
227184static Fts5ExprNearset *sqlite3Fts5ParseNearset(
227185 Fts5Parse*,
227186 Fts5ExprNearset*,
227187 Fts5ExprPhrase*
227188);
227189
227190static Fts5Colset *sqlite3Fts5ParseColset(
227191 Fts5Parse*,
227192 Fts5Colset*,
227193 Fts5Token *
227194);
227195
227196static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
227197static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
227198static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
227199
227200static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
227201static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
227202static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
227203static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
227204static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
227205
227206/*
227207** End of interface to code in fts5_expr.c.
227208**************************************************************************/
227209
227210
227211
227212/**************************************************************************
227213** Interface to code in fts5_aux.c.
227214*/
227215
227216static int sqlite3Fts5AuxInit(fts5_api*);
227217/*
227218** End of interface to code in fts5_aux.c.
227219**************************************************************************/
227220
227221/**************************************************************************
227222** Interface to code in fts5_tokenizer.c.
227223*/
227224
227225static int sqlite3Fts5TokenizerInit(fts5_api*);
227226static int sqlite3Fts5TokenizerPattern(
227227 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
227228 Fts5Tokenizer *pTok
227229);
227230/*
227231** End of interface to code in fts5_tokenizer.c.
227232**************************************************************************/
227233
227234/**************************************************************************
227235** Interface to code in fts5_vocab.c.
227236*/
227237
227238static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
227239
227240/*
227241** End of interface to code in fts5_vocab.c.
227242**************************************************************************/
227243
227244
227245/**************************************************************************
227246** Interface to automatically generated code in fts5_unicode2.c.
227247*/
227248static int sqlite3Fts5UnicodeIsdiacritic(int c);
227249static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
227250
227251static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
227252static int sqlite3Fts5UnicodeCategory(u32 iCode);
227253static void sqlite3Fts5UnicodeAscii(u8*, u8*);
227254/*
227255** End of interface to code in fts5_unicode2.c.
227256**************************************************************************/
227257
227258#endif
227259
227260#define FTS5_OR 1
227261#define FTS5_AND 2
227262#define FTS5_NOT 3
227263#define FTS5_TERM 4
227264#define FTS5_COLON 5
227265#define FTS5_MINUS 6
227266#define FTS5_LCP 7
227267#define FTS5_RCP 8
227268#define FTS5_STRING 9
227269#define FTS5_LP 10
227270#define FTS5_RP 11
227271#define FTS5_CARET 12
227272#define FTS5_COMMA 13
227273#define FTS5_PLUS 14
227274#define FTS5_STAR 15
227275
227276/* This file is automatically generated by Lemon from input grammar
227277** source file "fts5parse.y".
227278*/
227279/*
227280** 2000-05-29
227281**
227282** The author disclaims copyright to this source code. In place of
227283** a legal notice, here is a blessing:
227284**
227285** May you do good and not evil.
227286** May you find forgiveness for yourself and forgive others.
227287** May you share freely, never taking more than you give.
227288**
227289*************************************************************************
227290** Driver template for the LEMON parser generator.
227291**
227292** The "lemon" program processes an LALR(1) input grammar file, then uses
227293** this template to construct a parser. The "lemon" program inserts text
227294** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
227295** interstitial "-" characters) contained in this template is changed into
227296** the value of the %name directive from the grammar. Otherwise, the content
227297** of this template is copied straight through into the generate parser
227298** source file.
227299**
227300** The following is the concatenation of all %include directives from the
227301** input grammar file:
227302*/
227303/************ Begin %include sections from the grammar ************************/
227304
227305/* #include "fts5Int.h" */
227306/* #include "fts5parse.h" */
227307
227308/*
227309** Disable all error recovery processing in the parser push-down
227310** automaton.
227311*/
227312#define fts5YYNOERRORRECOVERY 1
227313
227314/*
227315** Make fts5yytestcase() the same as testcase()
227316*/
227317#define fts5yytestcase(X) testcase(X)
227318
227319/*
227320** Indicate that sqlite3ParserFree() will never be called with a null
227321** pointer.
227322*/
227323#define fts5YYPARSEFREENOTNULL 1
227324
227325/*
227326** Alternative datatype for the argument to the malloc() routine passed
227327** into sqlite3ParserAlloc(). The default is size_t.
227328*/
227329#define fts5YYMALLOCARGTYPE u64
227330
227331/**************** End of %include directives **********************************/
227332/* These constants specify the various numeric values for terminal symbols.
227333***************** Begin token definitions *************************************/
227334#ifndef FTS5_OR
227335#define FTS5_OR 1
227336#define FTS5_AND 2
227337#define FTS5_NOT 3
227338#define FTS5_TERM 4
227339#define FTS5_COLON 5
227340#define FTS5_MINUS 6
227341#define FTS5_LCP 7
227342#define FTS5_RCP 8
227343#define FTS5_STRING 9
227344#define FTS5_LP 10
227345#define FTS5_RP 11
227346#define FTS5_CARET 12
227347#define FTS5_COMMA 13
227348#define FTS5_PLUS 14
227349#define FTS5_STAR 15
227350#endif
227351/**************** End token definitions ***************************************/
227352
227353/* The next sections is a series of control #defines.
227354** various aspects of the generated parser.
227355** fts5YYCODETYPE is the data type used to store the integer codes
227356** that represent terminal and non-terminal symbols.
227357** "unsigned char" is used if there are fewer than
227358** 256 symbols. Larger types otherwise.
227359** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
227360** any terminal or nonterminal symbol.
227361** fts5YYFALLBACK If defined, this indicates that one or more tokens
227362** (also known as: "terminal symbols") have fall-back
227363** values which should be used if the original symbol
227364** would not parse. This permits keywords to sometimes
227365** be used as identifiers, for example.
227366** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
227367** that indicate what to do in response to the next
227368** token.
227369** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
227370** symbols. Background: A "minor type" is a semantic
227371** value associated with a terminal or non-terminal
227372** symbols. For example, for an "ID" terminal symbol,
227373** the minor type might be the name of the identifier.
227374** Each non-terminal can have a different minor type.
227375** Terminal symbols all have the same minor type, though.
227376** This macros defines the minor type for terminal
227377** symbols.
227378** fts5YYMINORTYPE is the data type used for all minor types.
227379** This is typically a union of many types, one of
227380** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
227381** for terminal symbols is called "fts5yy0".
227382** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
227383** zero the stack is dynamically sized using realloc()
227384** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
227385** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
227386** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
227387** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
227388** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
227389** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
227390** fts5YYERRORSYMBOL is the code number of the error symbol. If not
227391** defined, then do no error processing.
227392** fts5YYNSTATE the combined number of states.
227393** fts5YYNRULE the number of rules in the grammar
227394** fts5YYNFTS5TOKEN Number of terminal symbols
227395** fts5YY_MAX_SHIFT Maximum value for shift actions
227396** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
227397** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
227398** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
227399** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
227400** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
227401** fts5YY_MIN_REDUCE Minimum value for reduce actions
227402** fts5YY_MAX_REDUCE Maximum value for reduce actions
227403*/
227404#ifndef INTERFACE
227405# define INTERFACE 1
227406#endif
227407/************* Begin control #defines *****************************************/
227408#define fts5YYCODETYPE unsigned char
227409#define fts5YYNOCODE 27
227410#define fts5YYACTIONTYPE unsigned char
227411#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
227412typedef union {
227413 int fts5yyinit;
227414 sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
227415 int fts5yy4;
227416 Fts5Colset* fts5yy11;
227417 Fts5ExprNode* fts5yy24;
227418 Fts5ExprNearset* fts5yy46;
227419 Fts5ExprPhrase* fts5yy53;
227420} fts5YYMINORTYPE;
227421#ifndef fts5YYSTACKDEPTH
227422#define fts5YYSTACKDEPTH 100
227423#endif
227424#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
227425#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
227426#define sqlite3Fts5ParserARG_PARAM ,pParse
227427#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
227428#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
227429#define sqlite3Fts5ParserCTX_SDECL
227430#define sqlite3Fts5ParserCTX_PDECL
227431#define sqlite3Fts5ParserCTX_PARAM
227432#define sqlite3Fts5ParserCTX_FETCH
227433#define sqlite3Fts5ParserCTX_STORE
227434#define fts5YYNSTATE 35
227435#define fts5YYNRULE 28
227436#define fts5YYNRULE_WITH_ACTION 28
227437#define fts5YYNFTS5TOKEN 16
227438#define fts5YY_MAX_SHIFT 34
227439#define fts5YY_MIN_SHIFTREDUCE 52
227440#define fts5YY_MAX_SHIFTREDUCE 79
227441#define fts5YY_ERROR_ACTION 80
227442#define fts5YY_ACCEPT_ACTION 81
227443#define fts5YY_NO_ACTION 82
227444#define fts5YY_MIN_REDUCE 83
227445#define fts5YY_MAX_REDUCE 110
227446/************* End control #defines *******************************************/
227447#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
227448
227449/* Define the fts5yytestcase() macro to be a no-op if is not already defined
227450** otherwise.
227451**
227452** Applications can choose to define fts5yytestcase() in the %include section
227453** to a macro that can assist in verifying code coverage. For production
227454** code the fts5yytestcase() macro should be turned off. But it is useful
227455** for testing.
227456*/
227457#ifndef fts5yytestcase
227458# define fts5yytestcase(X)
227459#endif
227460
227461
227462/* Next are the tables used to determine what action to take based on the
227463** current state and lookahead token. These tables are used to implement
227464** functions that take a state number and lookahead value and return an
227465** action integer.
227466**
227467** Suppose the action integer is N. Then the action is determined as
227468** follows
227469**
227470** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
227471** token onto the stack and goto state N.
227472**
227473** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
227474** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
227475**
227476** N == fts5YY_ERROR_ACTION A syntax error has occurred.
227477**
227478** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
227479**
227480** N == fts5YY_NO_ACTION No such action. Denotes unused
227481** slots in the fts5yy_action[] table.
227482**
227483** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
227484** and fts5YY_MAX_REDUCE
227485**
227486** The action table is constructed as a single large table named fts5yy_action[].
227487** Given state S and lookahead X, the action is computed as either:
227488**
227489** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
227490** (B) N = fts5yy_default[S]
227491**
227492** The (A) formula is preferred. The B formula is used instead if
227493** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
227494**
227495** The formulas above are for computing the action when the lookahead is
227496** a terminal symbol. If the lookahead is a non-terminal (as occurs after
227497** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
227498** the fts5yy_shift_ofst[] array.
227499**
227500** The following are the tables generated in this section:
227501**
227502** fts5yy_action[] A single table containing all actions.
227503** fts5yy_lookahead[] A table containing the lookahead for each entry in
227504** fts5yy_action. Used to detect hash collisions.
227505** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
227506** shifting terminals.
227507** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
227508** shifting non-terminals after a reduce.
227509** fts5yy_default[] Default action for each state.
227510**
227511*********** Begin parsing tables **********************************************/
227512#define fts5YY_ACTTAB_COUNT (105)
227513static const fts5YYACTIONTYPE fts5yy_action[] = {
227514 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
227515 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
227516 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
227517 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
227518 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
227519 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
227520 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
227521 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
227522 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
227523 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
227524 /* 100 */ 57, 15, 82, 82, 71,
227525};
227526static const fts5YYCODETYPE fts5yy_lookahead[] = {
227527 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
227528 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
227529 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
227530 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
227531 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
227532 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
227533 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
227534 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
227535 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
227536 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
227537 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
227538 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
227539 /* 120 */ 27,
227540};
227541#define fts5YY_SHIFT_COUNT (34)
227542#define fts5YY_SHIFT_MIN (0)
227543#define fts5YY_SHIFT_MAX (93)
227544static const unsigned char fts5yy_shift_ofst[] = {
227545 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
227546 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
227547 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
227548 /* 30 */ 53, 87, 92, 53, 93,
227549};
227550#define fts5YY_REDUCE_COUNT (17)
227551#define fts5YY_REDUCE_MIN (-17)
227552#define fts5YY_REDUCE_MAX (67)
227553static const signed char fts5yy_reduce_ofst[] = {
227554 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
227555 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
227556};
227557static const fts5YYACTIONTYPE fts5yy_default[] = {
227558 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
227559 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
227560 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
227561 /* 30 */ 103, 80, 80, 104, 80,
227562};
227563/********** End of lemon-generated parsing tables *****************************/
227564
227565/* The next table maps tokens (terminal symbols) into fallback tokens.
227566** If a construct like the following:
227567**
227568** %fallback ID X Y Z.
227569**
227570** appears in the grammar, then ID becomes a fallback token for X, Y,
227571** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
227572** but it does not parse, the type of the token is changed to ID and
227573** the parse is retried before an error is thrown.
227574**
227575** This feature can be used, for example, to cause some keywords in a language
227576** to revert to identifiers if they keyword does not apply in the context where
227577** it appears.
227578*/
227579#ifdef fts5YYFALLBACK
227580static const fts5YYCODETYPE fts5yyFallback[] = {
227581};
227582#endif /* fts5YYFALLBACK */
227583
227584/* The following structure represents a single element of the
227585** parser's stack. Information stored includes:
227586**
227587** + The state number for the parser at this level of the stack.
227588**
227589** + The value of the token stored at this level of the stack.
227590** (In other words, the "major" token.)
227591**
227592** + The semantic value stored at this level of the stack. This is
227593** the information used by the action routines in the grammar.
227594** It is sometimes called the "minor" token.
227595**
227596** After the "shift" half of a SHIFTREDUCE action, the stateno field
227597** actually contains the reduce action for the second half of the
227598** SHIFTREDUCE.
227599*/
227600struct fts5yyStackEntry {
227601 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
227602 fts5YYCODETYPE major; /* The major token value. This is the code
227603 ** number for the token at this stack level */
227604 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
227605 ** is the value of the token */
227606};
227607typedef struct fts5yyStackEntry fts5yyStackEntry;
227608
227609/* The state of the parser is completely contained in an instance of
227610** the following structure */
227611struct fts5yyParser {
227612 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
227613#ifdef fts5YYTRACKMAXSTACKDEPTH
227614 int fts5yyhwm; /* High-water mark of the stack */
227615#endif
227616#ifndef fts5YYNOERRORRECOVERY
227617 int fts5yyerrcnt; /* Shifts left before out of the error */
227618#endif
227619 sqlite3Fts5ParserARG_SDECL /* A place to hold %extra_argument */
227620 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
227621#if fts5YYSTACKDEPTH<=0
227622 int fts5yystksz; /* Current side of the stack */
227623 fts5yyStackEntry *fts5yystack; /* The parser's stack */
227624 fts5yyStackEntry fts5yystk0; /* First stack entry */
227625#else
227626 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH]; /* The parser's stack */
227627 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
227628#endif
227629};
227630typedef struct fts5yyParser fts5yyParser;
227631
227632/* #include <assert.h> */
227633#ifndef NDEBUG
227634/* #include <stdio.h> */
227635static FILE *fts5yyTraceFILE = 0;
227636static char *fts5yyTracePrompt = 0;
227637#endif /* NDEBUG */
227638
227639#ifndef NDEBUG
227640/*
227641** Turn parser tracing on by giving a stream to which to write the trace
227642** and a prompt to preface each trace message. Tracing is turned off
227643** by making either argument NULL
227644**
227645** Inputs:
227646** <ul>
227647** <li> A FILE* to which trace output should be written.
227648** If NULL, then tracing is turned off.
227649** <li> A prefix string written at the beginning of every
227650** line of trace output. If NULL, then tracing is
227651** turned off.
227652** </ul>
227653**
227654** Outputs:
227655** None.
227656*/
227657static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
227658 fts5yyTraceFILE = TraceFILE;
227659 fts5yyTracePrompt = zTracePrompt;
227660 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
227661 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
227662}
227663#endif /* NDEBUG */
227664
227665#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
227666/* For tracing shifts, the names of all terminals and nonterminals
227667** are required. The following table supplies these names */
227668static const char *const fts5yyTokenName[] = {
227669 /* 0 */ "$",
227670 /* 1 */ "OR",
227671 /* 2 */ "AND",
227672 /* 3 */ "NOT",
227673 /* 4 */ "TERM",
227674 /* 5 */ "COLON",
227675 /* 6 */ "MINUS",
227676 /* 7 */ "LCP",
227677 /* 8 */ "RCP",
227678 /* 9 */ "STRING",
227679 /* 10 */ "LP",
227680 /* 11 */ "RP",
227681 /* 12 */ "CARET",
227682 /* 13 */ "COMMA",
227683 /* 14 */ "PLUS",
227684 /* 15 */ "STAR",
227685 /* 16 */ "input",
227686 /* 17 */ "expr",
227687 /* 18 */ "cnearset",
227688 /* 19 */ "exprlist",
227689 /* 20 */ "colset",
227690 /* 21 */ "colsetlist",
227691 /* 22 */ "nearset",
227692 /* 23 */ "nearphrases",
227693 /* 24 */ "phrase",
227694 /* 25 */ "neardist_opt",
227695 /* 26 */ "star_opt",
227696};
227697#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
227698
227699#ifndef NDEBUG
227700/* For tracing reduce actions, the names of all rules are required.
227701*/
227702static const char *const fts5yyRuleName[] = {
227703 /* 0 */ "input ::= expr",
227704 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
227705 /* 2 */ "colset ::= LCP colsetlist RCP",
227706 /* 3 */ "colset ::= STRING",
227707 /* 4 */ "colset ::= MINUS STRING",
227708 /* 5 */ "colsetlist ::= colsetlist STRING",
227709 /* 6 */ "colsetlist ::= STRING",
227710 /* 7 */ "expr ::= expr AND expr",
227711 /* 8 */ "expr ::= expr OR expr",
227712 /* 9 */ "expr ::= expr NOT expr",
227713 /* 10 */ "expr ::= colset COLON LP expr RP",
227714 /* 11 */ "expr ::= LP expr RP",
227715 /* 12 */ "expr ::= exprlist",
227716 /* 13 */ "exprlist ::= cnearset",
227717 /* 14 */ "exprlist ::= exprlist cnearset",
227718 /* 15 */ "cnearset ::= nearset",
227719 /* 16 */ "cnearset ::= colset COLON nearset",
227720 /* 17 */ "nearset ::= phrase",
227721 /* 18 */ "nearset ::= CARET phrase",
227722 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
227723 /* 20 */ "nearphrases ::= phrase",
227724 /* 21 */ "nearphrases ::= nearphrases phrase",
227725 /* 22 */ "neardist_opt ::=",
227726 /* 23 */ "neardist_opt ::= COMMA STRING",
227727 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
227728 /* 25 */ "phrase ::= STRING star_opt",
227729 /* 26 */ "star_opt ::= STAR",
227730 /* 27 */ "star_opt ::=",
227731};
227732#endif /* NDEBUG */
227733
227734
227735#if fts5YYSTACKDEPTH<=0
227736/*
227737** Try to increase the size of the parser stack. Return the number
227738** of errors. Return 0 on success.
227739*/
227740static int fts5yyGrowStack(fts5yyParser *p){
227741 int newSize;
227742 int idx;
227743 fts5yyStackEntry *pNew;
227744
227745 newSize = p->fts5yystksz*2 + 100;
227746 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
227747 if( p->fts5yystack==&p->fts5yystk0 ){
227748 pNew = malloc(newSize*sizeof(pNew[0]));
227749 if( pNew ) pNew[0] = p->fts5yystk0;
227750 }else{
227751 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
227752 }
227753 if( pNew ){
227754 p->fts5yystack = pNew;
227755 p->fts5yytos = &p->fts5yystack[idx];
227756#ifndef NDEBUG
227757 if( fts5yyTraceFILE ){
227758 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
227759 fts5yyTracePrompt, p->fts5yystksz, newSize);
227760 }
227761#endif
227762 p->fts5yystksz = newSize;
227763 }
227764 return pNew==0;
227765}
227766#endif
227767
227768/* Datatype of the argument to the memory allocated passed as the
227769** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
227770** putting an appropriate #define in the %include section of the input
227771** grammar.
227772*/
227773#ifndef fts5YYMALLOCARGTYPE
227774# define fts5YYMALLOCARGTYPE size_t
227775#endif
227776
227777/* Initialize a new parser that has already been allocated.
227778*/
227779static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
227780 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
227781 sqlite3Fts5ParserCTX_STORE
227782#ifdef fts5YYTRACKMAXSTACKDEPTH
227783 fts5yypParser->fts5yyhwm = 0;
227784#endif
227785#if fts5YYSTACKDEPTH<=0
227786 fts5yypParser->fts5yytos = NULL;
227787 fts5yypParser->fts5yystack = NULL;
227788 fts5yypParser->fts5yystksz = 0;
227789 if( fts5yyGrowStack(fts5yypParser) ){
227790 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
227791 fts5yypParser->fts5yystksz = 1;
227792 }
227793#endif
227794#ifndef fts5YYNOERRORRECOVERY
227795 fts5yypParser->fts5yyerrcnt = -1;
227796#endif
227797 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
227798 fts5yypParser->fts5yystack[0].stateno = 0;
227799 fts5yypParser->fts5yystack[0].major = 0;
227800#if fts5YYSTACKDEPTH>0
227801 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
227802#endif
227803}
227804
227805#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
227806/*
227807** This function allocates a new parser.
227808** The only argument is a pointer to a function which works like
227809** malloc.
227810**
227811** Inputs:
227812** A pointer to the function used to allocate memory.
227813**
227814** Outputs:
227815** A pointer to a parser. This pointer is used in subsequent calls
227816** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
227817*/
227818static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
227819 fts5yyParser *fts5yypParser;
227820 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
227821 if( fts5yypParser ){
227822 sqlite3Fts5ParserCTX_STORE
227823 sqlite3Fts5ParserInit(fts5yypRawParser: fts5yypParser sqlite3Fts5ParserCTX_PARAM);
227824 }
227825 return (void*)fts5yypParser;
227826}
227827#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
227828
227829
227830/* The following function deletes the "minor type" or semantic value
227831** associated with a symbol. The symbol can be either a terminal
227832** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
227833** a pointer to the value to be deleted. The code used to do the
227834** deletions is derived from the %destructor and/or %token_destructor
227835** directives of the input grammar.
227836*/
227837static void fts5yy_destructor(
227838 fts5yyParser *fts5yypParser, /* The parser */
227839 fts5YYCODETYPE fts5yymajor, /* Type code for object to destroy */
227840 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
227841){
227842 sqlite3Fts5ParserARG_FETCH
227843 sqlite3Fts5ParserCTX_FETCH
227844 switch( fts5yymajor ){
227845 /* Here is inserted the actions which take place when a
227846 ** terminal or non-terminal is destroyed. This can happen
227847 ** when the symbol is popped from the stack during a
227848 ** reduce or during error processing or when a parser is
227849 ** being destroyed before it is finished parsing.
227850 **
227851 ** Note: during a reduce, the only symbols destroyed are those
227852 ** which appear on the RHS of the rule, but which are *not* used
227853 ** inside the C code.
227854 */
227855/********* Begin destructor definitions ***************************************/
227856 case 16: /* input */
227857{
227858 (void)pParse;
227859}
227860 break;
227861 case 17: /* expr */
227862 case 18: /* cnearset */
227863 case 19: /* exprlist */
227864{
227865 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
227866}
227867 break;
227868 case 20: /* colset */
227869 case 21: /* colsetlist */
227870{
227871 sqlite3_free(p: (fts5yypminor->fts5yy11));
227872}
227873 break;
227874 case 22: /* nearset */
227875 case 23: /* nearphrases */
227876{
227877 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
227878}
227879 break;
227880 case 24: /* phrase */
227881{
227882 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
227883}
227884 break;
227885/********* End destructor definitions *****************************************/
227886 default: break; /* If no destructor action specified: do nothing */
227887 }
227888}
227889
227890/*
227891** Pop the parser's stack once.
227892**
227893** If there is a destructor routine associated with the token which
227894** is popped from the stack, then call it.
227895*/
227896static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
227897 fts5yyStackEntry *fts5yytos;
227898 assert( pParser->fts5yytos!=0 );
227899 assert( pParser->fts5yytos > pParser->fts5yystack );
227900 fts5yytos = pParser->fts5yytos--;
227901#ifndef NDEBUG
227902 if( fts5yyTraceFILE ){
227903 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
227904 fts5yyTracePrompt,
227905 fts5yyTokenName[fts5yytos->major]);
227906 }
227907#endif
227908 fts5yy_destructor(fts5yypParser: pParser, fts5yymajor: fts5yytos->major, fts5yypminor: &fts5yytos->minor);
227909}
227910
227911/*
227912** Clear all secondary memory allocations from the parser
227913*/
227914static void sqlite3Fts5ParserFinalize(void *p){
227915 fts5yyParser *pParser = (fts5yyParser*)p;
227916 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
227917#if fts5YYSTACKDEPTH<=0
227918 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
227919#endif
227920}
227921
227922#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
227923/*
227924** Deallocate and destroy a parser. Destructors are called for
227925** all stack elements before shutting the parser down.
227926**
227927** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
227928** is defined in a %include section of the input grammar) then it is
227929** assumed that the input pointer is never NULL.
227930*/
227931static void sqlite3Fts5ParserFree(
227932 void *p, /* The parser to be deleted */
227933 void (*freeProc)(void*) /* Function used to reclaim memory */
227934){
227935#ifndef fts5YYPARSEFREENEVERNULL
227936 if( p==0 ) return;
227937#endif
227938 sqlite3Fts5ParserFinalize(p);
227939 (*freeProc)(p);
227940}
227941#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
227942
227943/*
227944** Return the peak depth of the stack for a parser.
227945*/
227946#ifdef fts5YYTRACKMAXSTACKDEPTH
227947static int sqlite3Fts5ParserStackPeak(void *p){
227948 fts5yyParser *pParser = (fts5yyParser*)p;
227949 return pParser->fts5yyhwm;
227950}
227951#endif
227952
227953/* This array of booleans keeps track of the parser statement
227954** coverage. The element fts5yycoverage[X][Y] is set when the parser
227955** is in state X and has a lookahead token Y. In a well-tested
227956** systems, every element of this matrix should end up being set.
227957*/
227958#if defined(fts5YYCOVERAGE)
227959static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
227960#endif
227961
227962/*
227963** Write into out a description of every state/lookahead combination that
227964**
227965** (1) has not been used by the parser, and
227966** (2) is not a syntax error.
227967**
227968** Return the number of missed state/lookahead combinations.
227969*/
227970#if defined(fts5YYCOVERAGE)
227971static int sqlite3Fts5ParserCoverage(FILE *out){
227972 int stateno, iLookAhead, i;
227973 int nMissed = 0;
227974 for(stateno=0; stateno<fts5YYNSTATE; stateno++){
227975 i = fts5yy_shift_ofst[stateno];
227976 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
227977 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
227978 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
227979 if( out ){
227980 fprintf(out,"State %d lookahead %s %s\n", stateno,
227981 fts5yyTokenName[iLookAhead],
227982 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
227983 }
227984 }
227985 }
227986 return nMissed;
227987}
227988#endif
227989
227990/*
227991** Find the appropriate action for a parser given the terminal
227992** look-ahead token iLookAhead.
227993*/
227994static fts5YYACTIONTYPE fts5yy_find_shift_action(
227995 fts5YYCODETYPE iLookAhead, /* The look-ahead token */
227996 fts5YYACTIONTYPE stateno /* Current state number */
227997){
227998 int i;
227999
228000 if( stateno>fts5YY_MAX_SHIFT ) return stateno;
228001 assert( stateno <= fts5YY_SHIFT_COUNT );
228002#if defined(fts5YYCOVERAGE)
228003 fts5yycoverage[stateno][iLookAhead] = 1;
228004#endif
228005 do{
228006 i = fts5yy_shift_ofst[stateno];
228007 assert( i>=0 );
228008 assert( i<=fts5YY_ACTTAB_COUNT );
228009 assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD );
228010 assert( iLookAhead!=fts5YYNOCODE );
228011 assert( iLookAhead < fts5YYNFTS5TOKEN );
228012 i += iLookAhead;
228013 assert( i<(int)fts5YY_NLOOKAHEAD );
228014 if( fts5yy_lookahead[i]!=iLookAhead ){
228015#ifdef fts5YYFALLBACK
228016 fts5YYCODETYPE iFallback; /* Fallback token */
228017 assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) );
228018 iFallback = fts5yyFallback[iLookAhead];
228019 if( iFallback!=0 ){
228020#ifndef NDEBUG
228021 if( fts5yyTraceFILE ){
228022 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
228023 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
228024 }
228025#endif
228026 assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
228027 iLookAhead = iFallback;
228028 continue;
228029 }
228030#endif
228031#ifdef fts5YYWILDCARD
228032 {
228033 int j = i - iLookAhead + fts5YYWILDCARD;
228034 assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) );
228035 if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
228036#ifndef NDEBUG
228037 if( fts5yyTraceFILE ){
228038 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
228039 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
228040 fts5yyTokenName[fts5YYWILDCARD]);
228041 }
228042#endif /* NDEBUG */
228043 return fts5yy_action[j];
228044 }
228045 }
228046#endif /* fts5YYWILDCARD */
228047 return fts5yy_default[stateno];
228048 }else{
228049 assert( i>=0 && i<(int)(sizeof(fts5yy_action)/sizeof(fts5yy_action[0])) );
228050 return fts5yy_action[i];
228051 }
228052 }while(1);
228053}
228054
228055/*
228056** Find the appropriate action for a parser given the non-terminal
228057** look-ahead token iLookAhead.
228058*/
228059static fts5YYACTIONTYPE fts5yy_find_reduce_action(
228060 fts5YYACTIONTYPE stateno, /* Current state number */
228061 fts5YYCODETYPE iLookAhead /* The look-ahead token */
228062){
228063 int i;
228064#ifdef fts5YYERRORSYMBOL
228065 if( stateno>fts5YY_REDUCE_COUNT ){
228066 return fts5yy_default[stateno];
228067 }
228068#else
228069 assert( stateno<=fts5YY_REDUCE_COUNT );
228070#endif
228071 i = fts5yy_reduce_ofst[stateno];
228072 assert( iLookAhead!=fts5YYNOCODE );
228073 i += iLookAhead;
228074#ifdef fts5YYERRORSYMBOL
228075 if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
228076 return fts5yy_default[stateno];
228077 }
228078#else
228079 assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
228080 assert( fts5yy_lookahead[i]==iLookAhead );
228081#endif
228082 return fts5yy_action[i];
228083}
228084
228085/*
228086** The following routine is called if the stack overflows.
228087*/
228088static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
228089 sqlite3Fts5ParserARG_FETCH
228090 sqlite3Fts5ParserCTX_FETCH
228091#ifndef NDEBUG
228092 if( fts5yyTraceFILE ){
228093 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
228094 }
228095#endif
228096 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(pParser: fts5yypParser);
228097 /* Here code is inserted which will execute if the parser
228098 ** stack every overflows */
228099/******** Begin %stack_overflow code ******************************************/
228100
228101 sqlite3Fts5ParseError(pParse, zFmt: "fts5: parser stack overflow");
228102/******** End %stack_overflow code ********************************************/
228103 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
228104 sqlite3Fts5ParserCTX_STORE
228105}
228106
228107/*
228108** Print tracing information for a SHIFT action
228109*/
228110#ifndef NDEBUG
228111static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
228112 if( fts5yyTraceFILE ){
228113 if( fts5yyNewState<fts5YYNSTATE ){
228114 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
228115 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
228116 fts5yyNewState);
228117 }else{
228118 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
228119 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
228120 fts5yyNewState - fts5YY_MIN_REDUCE);
228121 }
228122 }
228123}
228124#else
228125# define fts5yyTraceShift(X,Y,Z)
228126#endif
228127
228128/*
228129** Perform a shift action.
228130*/
228131static void fts5yy_shift(
228132 fts5yyParser *fts5yypParser, /* The parser to be shifted */
228133 fts5YYACTIONTYPE fts5yyNewState, /* The new state to shift in */
228134 fts5YYCODETYPE fts5yyMajor, /* The major token to shift in */
228135 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor /* The minor token to shift in */
228136){
228137 fts5yyStackEntry *fts5yytos;
228138 fts5yypParser->fts5yytos++;
228139#ifdef fts5YYTRACKMAXSTACKDEPTH
228140 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
228141 fts5yypParser->fts5yyhwm++;
228142 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
228143 }
228144#endif
228145#if fts5YYSTACKDEPTH>0
228146 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
228147 fts5yypParser->fts5yytos--;
228148 fts5yyStackOverflow(fts5yypParser);
228149 return;
228150 }
228151#else
228152 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
228153 if( fts5yyGrowStack(fts5yypParser) ){
228154 fts5yypParser->fts5yytos--;
228155 fts5yyStackOverflow(fts5yypParser);
228156 return;
228157 }
228158 }
228159#endif
228160 if( fts5yyNewState > fts5YY_MAX_SHIFT ){
228161 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
228162 }
228163 fts5yytos = fts5yypParser->fts5yytos;
228164 fts5yytos->stateno = fts5yyNewState;
228165 fts5yytos->major = fts5yyMajor;
228166 fts5yytos->minor.fts5yy0 = fts5yyMinor;
228167 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
228168}
228169
228170/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
228171** of that rule */
228172static const fts5YYCODETYPE fts5yyRuleInfoLhs[] = {
228173 16, /* (0) input ::= expr */
228174 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
228175 20, /* (2) colset ::= LCP colsetlist RCP */
228176 20, /* (3) colset ::= STRING */
228177 20, /* (4) colset ::= MINUS STRING */
228178 21, /* (5) colsetlist ::= colsetlist STRING */
228179 21, /* (6) colsetlist ::= STRING */
228180 17, /* (7) expr ::= expr AND expr */
228181 17, /* (8) expr ::= expr OR expr */
228182 17, /* (9) expr ::= expr NOT expr */
228183 17, /* (10) expr ::= colset COLON LP expr RP */
228184 17, /* (11) expr ::= LP expr RP */
228185 17, /* (12) expr ::= exprlist */
228186 19, /* (13) exprlist ::= cnearset */
228187 19, /* (14) exprlist ::= exprlist cnearset */
228188 18, /* (15) cnearset ::= nearset */
228189 18, /* (16) cnearset ::= colset COLON nearset */
228190 22, /* (17) nearset ::= phrase */
228191 22, /* (18) nearset ::= CARET phrase */
228192 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
228193 23, /* (20) nearphrases ::= phrase */
228194 23, /* (21) nearphrases ::= nearphrases phrase */
228195 25, /* (22) neardist_opt ::= */
228196 25, /* (23) neardist_opt ::= COMMA STRING */
228197 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
228198 24, /* (25) phrase ::= STRING star_opt */
228199 26, /* (26) star_opt ::= STAR */
228200 26, /* (27) star_opt ::= */
228201};
228202
228203/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
228204** of symbols on the right-hand side of that rule. */
228205static const signed char fts5yyRuleInfoNRhs[] = {
228206 -1, /* (0) input ::= expr */
228207 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
228208 -3, /* (2) colset ::= LCP colsetlist RCP */
228209 -1, /* (3) colset ::= STRING */
228210 -2, /* (4) colset ::= MINUS STRING */
228211 -2, /* (5) colsetlist ::= colsetlist STRING */
228212 -1, /* (6) colsetlist ::= STRING */
228213 -3, /* (7) expr ::= expr AND expr */
228214 -3, /* (8) expr ::= expr OR expr */
228215 -3, /* (9) expr ::= expr NOT expr */
228216 -5, /* (10) expr ::= colset COLON LP expr RP */
228217 -3, /* (11) expr ::= LP expr RP */
228218 -1, /* (12) expr ::= exprlist */
228219 -1, /* (13) exprlist ::= cnearset */
228220 -2, /* (14) exprlist ::= exprlist cnearset */
228221 -1, /* (15) cnearset ::= nearset */
228222 -3, /* (16) cnearset ::= colset COLON nearset */
228223 -1, /* (17) nearset ::= phrase */
228224 -2, /* (18) nearset ::= CARET phrase */
228225 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
228226 -1, /* (20) nearphrases ::= phrase */
228227 -2, /* (21) nearphrases ::= nearphrases phrase */
228228 0, /* (22) neardist_opt ::= */
228229 -2, /* (23) neardist_opt ::= COMMA STRING */
228230 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
228231 -2, /* (25) phrase ::= STRING star_opt */
228232 -1, /* (26) star_opt ::= STAR */
228233 0, /* (27) star_opt ::= */
228234};
228235
228236static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
228237
228238/*
228239** Perform a reduce action and the shift that must immediately
228240** follow the reduce.
228241**
228242** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
228243** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
228244** if the lookahead token has already been consumed. As this procedure is
228245** only called from one place, optimizing compilers will in-line it, which
228246** means that the extra parameters have no performance impact.
228247*/
228248static fts5YYACTIONTYPE fts5yy_reduce(
228249 fts5yyParser *fts5yypParser, /* The parser */
228250 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
228251 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
228252 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken /* Value of the lookahead token */
228253 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
228254){
228255 int fts5yygoto; /* The next state */
228256 fts5YYACTIONTYPE fts5yyact; /* The next action */
228257 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
228258 int fts5yysize; /* Amount to pop the stack */
228259 sqlite3Fts5ParserARG_FETCH
228260 (void)fts5yyLookahead;
228261 (void)fts5yyLookaheadToken;
228262 fts5yymsp = fts5yypParser->fts5yytos;
228263
228264 switch( fts5yyruleno ){
228265 /* Beginning here are the reduction cases. A typical example
228266 ** follows:
228267 ** case 0:
228268 ** #line <lineno> <grammarfile>
228269 ** { ... } // User supplied code
228270 ** #line <lineno> <thisfile>
228271 ** break;
228272 */
228273/********** Begin reduce actions **********************************************/
228274 fts5YYMINORTYPE fts5yylhsminor;
228275 case 0: /* input ::= expr */
228276{ sqlite3Fts5ParseFinished(pParse, p: fts5yymsp[0].minor.fts5yy24); }
228277 break;
228278 case 1: /* colset ::= MINUS LCP colsetlist RCP */
228279{
228280 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
228281}
228282 break;
228283 case 2: /* colset ::= LCP colsetlist RCP */
228284{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
228285 break;
228286 case 3: /* colset ::= STRING */
228287{
228288 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
228289}
228290 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
228291 break;
228292 case 4: /* colset ::= MINUS STRING */
228293{
228294 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
228295 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
228296}
228297 break;
228298 case 5: /* colsetlist ::= colsetlist STRING */
228299{
228300 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
228301 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
228302 break;
228303 case 6: /* colsetlist ::= STRING */
228304{
228305 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
228306}
228307 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
228308 break;
228309 case 7: /* expr ::= expr AND expr */
228310{
228311 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft: fts5yymsp[-2].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24, pNear: 0);
228312}
228313 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228314 break;
228315 case 8: /* expr ::= expr OR expr */
228316{
228317 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, pLeft: fts5yymsp[-2].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24, pNear: 0);
228318}
228319 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228320 break;
228321 case 9: /* expr ::= expr NOT expr */
228322{
228323 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, pLeft: fts5yymsp[-2].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24, pNear: 0);
228324}
228325 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228326 break;
228327 case 10: /* expr ::= colset COLON LP expr RP */
228328{
228329 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
228330 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
228331}
228332 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228333 break;
228334 case 11: /* expr ::= LP expr RP */
228335{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
228336 break;
228337 case 12: /* expr ::= exprlist */
228338 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
228339{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
228340 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228341 break;
228342 case 14: /* exprlist ::= exprlist cnearset */
228343{
228344 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, pLeft: fts5yymsp[-1].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24);
228345}
228346 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228347 break;
228348 case 15: /* cnearset ::= nearset */
228349{
228350 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, pLeft: 0, pRight: 0, pNear: fts5yymsp[0].minor.fts5yy46);
228351}
228352 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228353 break;
228354 case 16: /* cnearset ::= colset COLON nearset */
228355{
228356 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, pLeft: 0, pRight: 0, pNear: fts5yymsp[0].minor.fts5yy46);
228357 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
228358}
228359 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
228360 break;
228361 case 17: /* nearset ::= phrase */
228362{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
228363 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
228364 break;
228365 case 18: /* nearset ::= CARET phrase */
228366{
228367 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
228368 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
228369}
228370 break;
228371 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
228372{
228373 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
228374 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
228375 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
228376}
228377 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
228378 break;
228379 case 20: /* nearphrases ::= phrase */
228380{
228381 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
228382}
228383 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
228384 break;
228385 case 21: /* nearphrases ::= nearphrases phrase */
228386{
228387 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
228388}
228389 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
228390 break;
228391 case 22: /* neardist_opt ::= */
228392{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
228393 break;
228394 case 23: /* neardist_opt ::= COMMA STRING */
228395{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
228396 break;
228397 case 24: /* phrase ::= phrase PLUS STRING star_opt */
228398{
228399 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, pPhrase: fts5yymsp[-3].minor.fts5yy53, pToken: &fts5yymsp[-1].minor.fts5yy0, bPrefix: fts5yymsp[0].minor.fts5yy4);
228400}
228401 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
228402 break;
228403 case 25: /* phrase ::= STRING star_opt */
228404{
228405 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, pPhrase: 0, pToken: &fts5yymsp[-1].minor.fts5yy0, bPrefix: fts5yymsp[0].minor.fts5yy4);
228406}
228407 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
228408 break;
228409 case 26: /* star_opt ::= STAR */
228410{ fts5yymsp[0].minor.fts5yy4 = 1; }
228411 break;
228412 case 27: /* star_opt ::= */
228413{ fts5yymsp[1].minor.fts5yy4 = 0; }
228414 break;
228415 default:
228416 break;
228417/********** End reduce actions ************************************************/
228418 };
228419 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) );
228420 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
228421 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
228422 fts5yyact = fts5yy_find_reduce_action(stateno: fts5yymsp[fts5yysize].stateno,iLookAhead: (fts5YYCODETYPE)fts5yygoto);
228423
228424 /* There are no SHIFTREDUCE actions on nonterminals because the table
228425 ** generator has simplified them to pure REDUCE actions. */
228426 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
228427
228428 /* It is not possible for a REDUCE to be followed by an error */
228429 assert( fts5yyact!=fts5YY_ERROR_ACTION );
228430
228431 fts5yymsp += fts5yysize+1;
228432 fts5yypParser->fts5yytos = fts5yymsp;
228433 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
228434 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
228435 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
228436 return fts5yyact;
228437}
228438
228439/*
228440** The following code executes when the parse fails
228441*/
228442#ifndef fts5YYNOERRORRECOVERY
228443static void fts5yy_parse_failed(
228444 fts5yyParser *fts5yypParser /* The parser */
228445){
228446 sqlite3Fts5ParserARG_FETCH
228447 sqlite3Fts5ParserCTX_FETCH
228448#ifndef NDEBUG
228449 if( fts5yyTraceFILE ){
228450 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
228451 }
228452#endif
228453 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
228454 /* Here code is inserted which will be executed whenever the
228455 ** parser fails */
228456/************ Begin %parse_failure code ***************************************/
228457/************ End %parse_failure code *****************************************/
228458 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
228459 sqlite3Fts5ParserCTX_STORE
228460}
228461#endif /* fts5YYNOERRORRECOVERY */
228462
228463/*
228464** The following code executes when a syntax error first occurs.
228465*/
228466static void fts5yy_syntax_error(
228467 fts5yyParser *fts5yypParser, /* The parser */
228468 int fts5yymajor, /* The major type of the error token */
228469 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The minor type of the error token */
228470){
228471 sqlite3Fts5ParserARG_FETCH
228472 sqlite3Fts5ParserCTX_FETCH
228473#define FTS5TOKEN fts5yyminor
228474/************ Begin %syntax_error code ****************************************/
228475
228476 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
228477 sqlite3Fts5ParseError(
228478 pParse, zFmt: "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
228479 );
228480/************ End %syntax_error code ******************************************/
228481 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
228482 sqlite3Fts5ParserCTX_STORE
228483}
228484
228485/*
228486** The following is executed when the parser accepts
228487*/
228488static void fts5yy_accept(
228489 fts5yyParser *fts5yypParser /* The parser */
228490){
228491 sqlite3Fts5ParserARG_FETCH
228492 sqlite3Fts5ParserCTX_FETCH
228493#ifndef NDEBUG
228494 if( fts5yyTraceFILE ){
228495 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
228496 }
228497#endif
228498#ifndef fts5YYNOERRORRECOVERY
228499 fts5yypParser->fts5yyerrcnt = -1;
228500#endif
228501 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
228502 /* Here code is inserted which will be executed whenever the
228503 ** parser accepts */
228504/*********** Begin %parse_accept code *****************************************/
228505/*********** End %parse_accept code *******************************************/
228506 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
228507 sqlite3Fts5ParserCTX_STORE
228508}
228509
228510/* The main parser program.
228511** The first argument is a pointer to a structure obtained from
228512** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
228513** The second argument is the major token number. The third is
228514** the minor token. The fourth optional argument is whatever the
228515** user wants (and specified in the grammar) and is available for
228516** use by the action routines.
228517**
228518** Inputs:
228519** <ul>
228520** <li> A pointer to the parser (an opaque structure.)
228521** <li> The major token number.
228522** <li> The minor token number.
228523** <li> An option argument of a grammar-specified type.
228524** </ul>
228525**
228526** Outputs:
228527** None.
228528*/
228529static void sqlite3Fts5Parser(
228530 void *fts5yyp, /* The parser */
228531 int fts5yymajor, /* The major token code number */
228532 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The value for the token */
228533 sqlite3Fts5ParserARG_PDECL /* Optional %extra_argument parameter */
228534){
228535 fts5YYMINORTYPE fts5yyminorunion;
228536 fts5YYACTIONTYPE fts5yyact; /* The parser action. */
228537#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
228538 int fts5yyendofinput; /* True if we are at the end of input */
228539#endif
228540#ifdef fts5YYERRORSYMBOL
228541 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
228542#endif
228543 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
228544 sqlite3Fts5ParserCTX_FETCH
228545 sqlite3Fts5ParserARG_STORE
228546
228547 assert( fts5yypParser->fts5yytos!=0 );
228548#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
228549 fts5yyendofinput = (fts5yymajor==0);
228550#endif
228551
228552 fts5yyact = fts5yypParser->fts5yytos->stateno;
228553#ifndef NDEBUG
228554 if( fts5yyTraceFILE ){
228555 if( fts5yyact < fts5YY_MIN_REDUCE ){
228556 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
228557 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
228558 }else{
228559 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
228560 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
228561 }
228562 }
228563#endif
228564
228565 while(1){ /* Exit by "break" */
228566 assert( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystack );
228567 assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
228568 fts5yyact = fts5yy_find_shift_action(iLookAhead: (fts5YYCODETYPE)fts5yymajor,stateno: fts5yyact);
228569 if( fts5yyact >= fts5YY_MIN_REDUCE ){
228570 unsigned int fts5yyruleno = fts5yyact - fts5YY_MIN_REDUCE; /* Reduce by this rule */
228571#ifndef NDEBUG
228572 assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) );
228573 if( fts5yyTraceFILE ){
228574 int fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
228575 if( fts5yysize ){
228576 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
228577 fts5yyTracePrompt,
228578 fts5yyruleno, fts5yyRuleName[fts5yyruleno],
228579 fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action",
228580 fts5yypParser->fts5yytos[fts5yysize].stateno);
228581 }else{
228582 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
228583 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
228584 fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action");
228585 }
228586 }
228587#endif /* NDEBUG */
228588
228589 /* Check that the stack is large enough to grow by a single entry
228590 ** if the RHS of the rule is empty. This ensures that there is room
228591 ** enough on the stack to push the LHS value */
228592 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
228593#ifdef fts5YYTRACKMAXSTACKDEPTH
228594 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
228595 fts5yypParser->fts5yyhwm++;
228596 assert( fts5yypParser->fts5yyhwm ==
228597 (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
228598 }
228599#endif
228600#if fts5YYSTACKDEPTH>0
228601 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
228602 fts5yyStackOverflow(fts5yypParser);
228603 break;
228604 }
228605#else
228606 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
228607 if( fts5yyGrowStack(fts5yypParser) ){
228608 fts5yyStackOverflow(fts5yypParser);
228609 break;
228610 }
228611 }
228612#endif
228613 }
228614 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyruleno,fts5yyLookahead: fts5yymajor,fts5yyLookaheadToken: fts5yyminor sqlite3Fts5ParserCTX_PARAM);
228615 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
228616 fts5yy_shift(fts5yypParser,fts5yyNewState: fts5yyact,fts5yyMajor: (fts5YYCODETYPE)fts5yymajor,fts5yyMinor: fts5yyminor);
228617#ifndef fts5YYNOERRORRECOVERY
228618 fts5yypParser->fts5yyerrcnt--;
228619#endif
228620 break;
228621 }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
228622 fts5yypParser->fts5yytos--;
228623 fts5yy_accept(fts5yypParser);
228624 return;
228625 }else{
228626 assert( fts5yyact == fts5YY_ERROR_ACTION );
228627 fts5yyminorunion.fts5yy0 = fts5yyminor;
228628#ifdef fts5YYERRORSYMBOL
228629 int fts5yymx;
228630#endif
228631#ifndef NDEBUG
228632 if( fts5yyTraceFILE ){
228633 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
228634 }
228635#endif
228636#ifdef fts5YYERRORSYMBOL
228637 /* A syntax error has occurred.
228638 ** The response to an error depends upon whether or not the
228639 ** grammar defines an error token "ERROR".
228640 **
228641 ** This is what we do if the grammar does define ERROR:
228642 **
228643 ** * Call the %syntax_error function.
228644 **
228645 ** * Begin popping the stack until we enter a state where
228646 ** it is legal to shift the error symbol, then shift
228647 ** the error symbol.
228648 **
228649 ** * Set the error count to three.
228650 **
228651 ** * Begin accepting and shifting new tokens. No new error
228652 ** processing will occur until three tokens have been
228653 ** shifted successfully.
228654 **
228655 */
228656 if( fts5yypParser->fts5yyerrcnt<0 ){
228657 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
228658 }
228659 fts5yymx = fts5yypParser->fts5yytos->major;
228660 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
228661#ifndef NDEBUG
228662 if( fts5yyTraceFILE ){
228663 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
228664 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
228665 }
228666#endif
228667 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
228668 fts5yymajor = fts5YYNOCODE;
228669 }else{
228670 while( fts5yypParser->fts5yytos > fts5yypParser->fts5yystack ){
228671 fts5yyact = fts5yy_find_reduce_action(fts5yypParser->fts5yytos->stateno,
228672 fts5YYERRORSYMBOL);
228673 if( fts5yyact<=fts5YY_MAX_SHIFTREDUCE ) break;
228674 fts5yy_pop_parser_stack(fts5yypParser);
228675 }
228676 if( fts5yypParser->fts5yytos <= fts5yypParser->fts5yystack || fts5yymajor==0 ){
228677 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
228678 fts5yy_parse_failed(fts5yypParser);
228679#ifndef fts5YYNOERRORRECOVERY
228680 fts5yypParser->fts5yyerrcnt = -1;
228681#endif
228682 fts5yymajor = fts5YYNOCODE;
228683 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
228684 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
228685 }
228686 }
228687 fts5yypParser->fts5yyerrcnt = 3;
228688 fts5yyerrorhit = 1;
228689 if( fts5yymajor==fts5YYNOCODE ) break;
228690 fts5yyact = fts5yypParser->fts5yytos->stateno;
228691#elif defined(fts5YYNOERRORRECOVERY)
228692 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
228693 ** do any kind of error recovery. Instead, simply invoke the syntax
228694 ** error routine and continue going as if nothing had happened.
228695 **
228696 ** Applications can set this macro (for example inside %include) if
228697 ** they intend to abandon the parse upon the first syntax error seen.
228698 */
228699 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
228700 fts5yy_destructor(fts5yypParser,fts5yymajor: (fts5YYCODETYPE)fts5yymajor,fts5yypminor: &fts5yyminorunion);
228701 break;
228702#else /* fts5YYERRORSYMBOL is not defined */
228703 /* This is what we do if the grammar does not define ERROR:
228704 **
228705 ** * Report an error message, and throw away the input token.
228706 **
228707 ** * If the input token is $, then fail the parse.
228708 **
228709 ** As before, subsequent error messages are suppressed until
228710 ** three input tokens have been successfully shifted.
228711 */
228712 if( fts5yypParser->fts5yyerrcnt<=0 ){
228713 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
228714 }
228715 fts5yypParser->fts5yyerrcnt = 3;
228716 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
228717 if( fts5yyendofinput ){
228718 fts5yy_parse_failed(fts5yypParser);
228719#ifndef fts5YYNOERRORRECOVERY
228720 fts5yypParser->fts5yyerrcnt = -1;
228721#endif
228722 }
228723 break;
228724#endif
228725 }
228726 }
228727#ifndef NDEBUG
228728 if( fts5yyTraceFILE ){
228729 fts5yyStackEntry *i;
228730 char cDiv = '[';
228731 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
228732 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
228733 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
228734 cDiv = ' ';
228735 }
228736 fprintf(fts5yyTraceFILE,"]\n");
228737 }
228738#endif
228739 return;
228740}
228741
228742/*
228743** Return the fallback token corresponding to canonical token iToken, or
228744** 0 if iToken has no fallback.
228745*/
228746static int sqlite3Fts5ParserFallback(int iToken){
228747#ifdef fts5YYFALLBACK
228748 assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) );
228749 return fts5yyFallback[iToken];
228750#else
228751 (void)iToken;
228752 return 0;
228753#endif
228754}
228755
228756/*
228757** 2014 May 31
228758**
228759** The author disclaims copyright to this source code. In place of
228760** a legal notice, here is a blessing:
228761**
228762** May you do good and not evil.
228763** May you find forgiveness for yourself and forgive others.
228764** May you share freely, never taking more than you give.
228765**
228766******************************************************************************
228767*/
228768
228769
228770/* #include "fts5Int.h" */
228771#include <math.h> /* amalgamator: keep */
228772
228773/*
228774** Object used to iterate through all "coalesced phrase instances" in
228775** a single column of the current row. If the phrase instances in the
228776** column being considered do not overlap, this object simply iterates
228777** through them. Or, if they do overlap (share one or more tokens in
228778** common), each set of overlapping instances is treated as a single
228779** match. See documentation for the highlight() auxiliary function for
228780** details.
228781**
228782** Usage is:
228783**
228784** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
228785** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
228786** rc = fts5CInstIterNext(&iter)
228787** ){
228788** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
228789** }
228790**
228791*/
228792typedef struct CInstIter CInstIter;
228793struct CInstIter {
228794 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
228795 Fts5Context *pFts; /* First arg to pass to pApi functions */
228796 int iCol; /* Column to search */
228797 int iInst; /* Next phrase instance index */
228798 int nInst; /* Total number of phrase instances */
228799
228800 /* Output variables */
228801 int iStart; /* First token in coalesced phrase instance */
228802 int iEnd; /* Last token in coalesced phrase instance */
228803};
228804
228805/*
228806** Advance the iterator to the next coalesced phrase instance. Return
228807** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
228808*/
228809static int fts5CInstIterNext(CInstIter *pIter){
228810 int rc = SQLITE_OK;
228811 pIter->iStart = -1;
228812 pIter->iEnd = -1;
228813
228814 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
228815 int ip; int ic; int io;
228816 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
228817 if( rc==SQLITE_OK ){
228818 if( ic==pIter->iCol ){
228819 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
228820 if( pIter->iStart<0 ){
228821 pIter->iStart = io;
228822 pIter->iEnd = iEnd;
228823 }else if( io<=pIter->iEnd ){
228824 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
228825 }else{
228826 break;
228827 }
228828 }
228829 pIter->iInst++;
228830 }
228831 }
228832
228833 return rc;
228834}
228835
228836/*
228837** Initialize the iterator object indicated by the final parameter to
228838** iterate through coalesced phrase instances in column iCol.
228839*/
228840static int fts5CInstIterInit(
228841 const Fts5ExtensionApi *pApi,
228842 Fts5Context *pFts,
228843 int iCol,
228844 CInstIter *pIter
228845){
228846 int rc;
228847
228848 memset(s: pIter, c: 0, n: sizeof(CInstIter));
228849 pIter->pApi = pApi;
228850 pIter->pFts = pFts;
228851 pIter->iCol = iCol;
228852 rc = pApi->xInstCount(pFts, &pIter->nInst);
228853
228854 if( rc==SQLITE_OK ){
228855 rc = fts5CInstIterNext(pIter);
228856 }
228857
228858 return rc;
228859}
228860
228861
228862
228863/*************************************************************************
228864** Start of highlight() implementation.
228865*/
228866typedef struct HighlightContext HighlightContext;
228867struct HighlightContext {
228868 CInstIter iter; /* Coalesced Instance Iterator */
228869 int iPos; /* Current token offset in zIn[] */
228870 int iRangeStart; /* First token to include */
228871 int iRangeEnd; /* If non-zero, last token to include */
228872 const char *zOpen; /* Opening highlight */
228873 const char *zClose; /* Closing highlight */
228874 const char *zIn; /* Input text */
228875 int nIn; /* Size of input text in bytes */
228876 int iOff; /* Current offset within zIn[] */
228877 char *zOut; /* Output value */
228878};
228879
228880/*
228881** Append text to the HighlightContext output string - p->zOut. Argument
228882** z points to a buffer containing n bytes of text to append. If n is
228883** negative, everything up until the first '\0' is appended to the output.
228884**
228885** If *pRc is set to any value other than SQLITE_OK when this function is
228886** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
228887** *pRc is set to an error code before returning.
228888*/
228889static void fts5HighlightAppend(
228890 int *pRc,
228891 HighlightContext *p,
228892 const char *z, int n
228893){
228894 if( *pRc==SQLITE_OK && z ){
228895 if( n<0 ) n = (int)strlen(s: z);
228896 p->zOut = sqlite3_mprintf(zFormat: "%z%.*s", p->zOut, n, z);
228897 if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
228898 }
228899}
228900
228901/*
228902** Tokenizer callback used by implementation of highlight() function.
228903*/
228904static int fts5HighlightCb(
228905 void *pContext, /* Pointer to HighlightContext object */
228906 int tflags, /* Mask of FTS5_TOKEN_* flags */
228907 const char *pToken, /* Buffer containing token */
228908 int nToken, /* Size of token in bytes */
228909 int iStartOff, /* Start offset of token */
228910 int iEndOff /* End offset of token */
228911){
228912 HighlightContext *p = (HighlightContext*)pContext;
228913 int rc = SQLITE_OK;
228914 int iPos;
228915
228916 UNUSED_PARAM2(pToken, nToken);
228917
228918 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
228919 iPos = p->iPos++;
228920
228921 if( p->iRangeEnd>=0 ){
228922 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
228923 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
228924 }
228925
228926 if( iPos==p->iter.iStart ){
228927 fts5HighlightAppend(pRc: &rc, p, z: &p->zIn[p->iOff], n: iStartOff - p->iOff);
228928 fts5HighlightAppend(pRc: &rc, p, z: p->zOpen, n: -1);
228929 p->iOff = iStartOff;
228930 }
228931
228932 if( iPos==p->iter.iEnd ){
228933 if( p->iRangeEnd>=0 && p->iter.iStart<p->iRangeStart ){
228934 fts5HighlightAppend(pRc: &rc, p, z: p->zOpen, n: -1);
228935 }
228936 fts5HighlightAppend(pRc: &rc, p, z: &p->zIn[p->iOff], n: iEndOff - p->iOff);
228937 fts5HighlightAppend(pRc: &rc, p, z: p->zClose, n: -1);
228938 p->iOff = iEndOff;
228939 if( rc==SQLITE_OK ){
228940 rc = fts5CInstIterNext(pIter: &p->iter);
228941 }
228942 }
228943
228944 if( p->iRangeEnd>=0 && iPos==p->iRangeEnd ){
228945 fts5HighlightAppend(pRc: &rc, p, z: &p->zIn[p->iOff], n: iEndOff - p->iOff);
228946 p->iOff = iEndOff;
228947 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
228948 fts5HighlightAppend(pRc: &rc, p, z: p->zClose, n: -1);
228949 }
228950 }
228951
228952 return rc;
228953}
228954
228955/*
228956** Implementation of highlight() function.
228957*/
228958static void fts5HighlightFunction(
228959 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
228960 Fts5Context *pFts, /* First arg to pass to pApi functions */
228961 sqlite3_context *pCtx, /* Context for returning result/error */
228962 int nVal, /* Number of values in apVal[] array */
228963 sqlite3_value **apVal /* Array of trailing arguments */
228964){
228965 HighlightContext ctx;
228966 int rc;
228967 int iCol;
228968
228969 if( nVal!=3 ){
228970 const char *zErr = "wrong number of arguments to function highlight()";
228971 sqlite3_result_error(pCtx, z: zErr, n: -1);
228972 return;
228973 }
228974
228975 iCol = sqlite3_value_int(pVal: apVal[0]);
228976 memset(s: &ctx, c: 0, n: sizeof(HighlightContext));
228977 ctx.zOpen = (const char*)sqlite3_value_text(pVal: apVal[1]);
228978 ctx.zClose = (const char*)sqlite3_value_text(pVal: apVal[2]);
228979 ctx.iRangeEnd = -1;
228980 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
228981
228982 if( ctx.zIn ){
228983 if( rc==SQLITE_OK ){
228984 rc = fts5CInstIterInit(pApi, pFts, iCol, pIter: &ctx.iter);
228985 }
228986
228987 if( rc==SQLITE_OK ){
228988 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
228989 }
228990 fts5HighlightAppend(pRc: &rc, p: &ctx, z: &ctx.zIn[ctx.iOff], n: ctx.nIn - ctx.iOff);
228991
228992 if( rc==SQLITE_OK ){
228993 sqlite3_result_text(pCtx, z: (const char*)ctx.zOut, n: -1, SQLITE_TRANSIENT);
228994 }
228995 sqlite3_free(p: ctx.zOut);
228996 }
228997 if( rc!=SQLITE_OK ){
228998 sqlite3_result_error_code(pCtx, errCode: rc);
228999 }
229000}
229001/*
229002** End of highlight() implementation.
229003**************************************************************************/
229004
229005/*
229006** Context object passed to the fts5SentenceFinderCb() function.
229007*/
229008typedef struct Fts5SFinder Fts5SFinder;
229009struct Fts5SFinder {
229010 int iPos; /* Current token position */
229011 int nFirstAlloc; /* Allocated size of aFirst[] */
229012 int nFirst; /* Number of entries in aFirst[] */
229013 int *aFirst; /* Array of first token in each sentence */
229014 const char *zDoc; /* Document being tokenized */
229015};
229016
229017/*
229018** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
229019** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
229020** error occurs.
229021*/
229022static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
229023 if( p->nFirstAlloc==p->nFirst ){
229024 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
229025 int *aNew;
229026
229027 aNew = (int*)sqlite3_realloc64(pOld: p->aFirst, n: nNew*sizeof(int));
229028 if( aNew==0 ) return SQLITE_NOMEM;
229029 p->aFirst = aNew;
229030 p->nFirstAlloc = nNew;
229031 }
229032 p->aFirst[p->nFirst++] = iAdd;
229033 return SQLITE_OK;
229034}
229035
229036/*
229037** This function is an xTokenize() callback used by the auxiliary snippet()
229038** function. Its job is to identify tokens that are the first in a sentence.
229039** For each such token, an entry is added to the SFinder.aFirst[] array.
229040*/
229041static int fts5SentenceFinderCb(
229042 void *pContext, /* Pointer to HighlightContext object */
229043 int tflags, /* Mask of FTS5_TOKEN_* flags */
229044 const char *pToken, /* Buffer containing token */
229045 int nToken, /* Size of token in bytes */
229046 int iStartOff, /* Start offset of token */
229047 int iEndOff /* End offset of token */
229048){
229049 int rc = SQLITE_OK;
229050
229051 UNUSED_PARAM2(pToken, nToken);
229052 UNUSED_PARAM(iEndOff);
229053
229054 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
229055 Fts5SFinder *p = (Fts5SFinder*)pContext;
229056 if( p->iPos>0 ){
229057 int i;
229058 char c = 0;
229059 for(i=iStartOff-1; i>=0; i--){
229060 c = p->zDoc[i];
229061 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
229062 }
229063 if( i!=iStartOff-1 && (c=='.' || c==':') ){
229064 rc = fts5SentenceFinderAdd(p, iAdd: p->iPos);
229065 }
229066 }else{
229067 rc = fts5SentenceFinderAdd(p, iAdd: 0);
229068 }
229069 p->iPos++;
229070 }
229071 return rc;
229072}
229073
229074static int fts5SnippetScore(
229075 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
229076 Fts5Context *pFts, /* First arg to pass to pApi functions */
229077 int nDocsize, /* Size of column in tokens */
229078 unsigned char *aSeen, /* Array with one element per query phrase */
229079 int iCol, /* Column to score */
229080 int iPos, /* Starting offset to score */
229081 int nToken, /* Max tokens per snippet */
229082 int *pnScore, /* OUT: Score */
229083 int *piPos /* OUT: Adjusted offset */
229084){
229085 int rc;
229086 int i;
229087 int ip = 0;
229088 int ic = 0;
229089 int iOff = 0;
229090 int iFirst = -1;
229091 int nInst;
229092 int nScore = 0;
229093 int iLast = 0;
229094 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
229095
229096 rc = pApi->xInstCount(pFts, &nInst);
229097 for(i=0; i<nInst && rc==SQLITE_OK; i++){
229098 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
229099 if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){
229100 nScore += (aSeen[ip] ? 1 : 1000);
229101 aSeen[ip] = 1;
229102 if( iFirst<0 ) iFirst = iOff;
229103 iLast = iOff + pApi->xPhraseSize(pFts, ip);
229104 }
229105 }
229106
229107 *pnScore = nScore;
229108 if( piPos ){
229109 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
229110 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
229111 if( iAdj<0 ) iAdj = 0;
229112 *piPos = (int)iAdj;
229113 }
229114
229115 return rc;
229116}
229117
229118/*
229119** Return the value in pVal interpreted as utf-8 text. Except, if pVal
229120** contains a NULL value, return a pointer to a static string zero
229121** bytes in length instead of a NULL pointer.
229122*/
229123static const char *fts5ValueToText(sqlite3_value *pVal){
229124 const char *zRet = (const char*)sqlite3_value_text(pVal);
229125 return zRet ? zRet : "";
229126}
229127
229128/*
229129** Implementation of snippet() function.
229130*/
229131static void fts5SnippetFunction(
229132 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
229133 Fts5Context *pFts, /* First arg to pass to pApi functions */
229134 sqlite3_context *pCtx, /* Context for returning result/error */
229135 int nVal, /* Number of values in apVal[] array */
229136 sqlite3_value **apVal /* Array of trailing arguments */
229137){
229138 HighlightContext ctx;
229139 int rc = SQLITE_OK; /* Return code */
229140 int iCol; /* 1st argument to snippet() */
229141 const char *zEllips; /* 4th argument to snippet() */
229142 int nToken; /* 5th argument to snippet() */
229143 int nInst = 0; /* Number of instance matches this row */
229144 int i; /* Used to iterate through instances */
229145 int nPhrase; /* Number of phrases in query */
229146 unsigned char *aSeen; /* Array of "seen instance" flags */
229147 int iBestCol; /* Column containing best snippet */
229148 int iBestStart = 0; /* First token of best snippet */
229149 int nBestScore = 0; /* Score of best snippet */
229150 int nColSize = 0; /* Total size of iBestCol in tokens */
229151 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
229152 int nCol;
229153
229154 if( nVal!=5 ){
229155 const char *zErr = "wrong number of arguments to function snippet()";
229156 sqlite3_result_error(pCtx, z: zErr, n: -1);
229157 return;
229158 }
229159
229160 nCol = pApi->xColumnCount(pFts);
229161 memset(s: &ctx, c: 0, n: sizeof(HighlightContext));
229162 iCol = sqlite3_value_int(pVal: apVal[0]);
229163 ctx.zOpen = fts5ValueToText(pVal: apVal[1]);
229164 ctx.zClose = fts5ValueToText(pVal: apVal[2]);
229165 ctx.iRangeEnd = -1;
229166 zEllips = fts5ValueToText(pVal: apVal[3]);
229167 nToken = sqlite3_value_int(pVal: apVal[4]);
229168
229169 iBestCol = (iCol>=0 ? iCol : 0);
229170 nPhrase = pApi->xPhraseCount(pFts);
229171 aSeen = sqlite3_malloc(n: nPhrase);
229172 if( aSeen==0 ){
229173 rc = SQLITE_NOMEM;
229174 }
229175 if( rc==SQLITE_OK ){
229176 rc = pApi->xInstCount(pFts, &nInst);
229177 }
229178
229179 memset(s: &sFinder, c: 0, n: sizeof(Fts5SFinder));
229180 for(i=0; i<nCol; i++){
229181 if( iCol<0 || iCol==i ){
229182 int nDoc;
229183 int nDocsize;
229184 int ii;
229185 sFinder.iPos = 0;
229186 sFinder.nFirst = 0;
229187 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
229188 if( rc!=SQLITE_OK ) break;
229189 rc = pApi->xTokenize(pFts,
229190 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
229191 );
229192 if( rc!=SQLITE_OK ) break;
229193 rc = pApi->xColumnSize(pFts, i, &nDocsize);
229194 if( rc!=SQLITE_OK ) break;
229195
229196 for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
229197 int ip, ic, io;
229198 int iAdj;
229199 int nScore;
229200 int jj;
229201
229202 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
229203 if( ic!=i ) continue;
229204 if( io>nDocsize ) rc = FTS5_CORRUPT;
229205 if( rc!=SQLITE_OK ) continue;
229206 memset(s: aSeen, c: 0, n: nPhrase);
229207 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, iCol: i,
229208 iPos: io, nToken, pnScore: &nScore, piPos: &iAdj
229209 );
229210 if( rc==SQLITE_OK && nScore>nBestScore ){
229211 nBestScore = nScore;
229212 iBestCol = i;
229213 iBestStart = iAdj;
229214 nColSize = nDocsize;
229215 }
229216
229217 if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
229218 for(jj=0; jj<(sFinder.nFirst-1); jj++){
229219 if( sFinder.aFirst[jj+1]>io ) break;
229220 }
229221
229222 if( sFinder.aFirst[jj]<io ){
229223 memset(s: aSeen, c: 0, n: nPhrase);
229224 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, iCol: i,
229225 iPos: sFinder.aFirst[jj], nToken, pnScore: &nScore, piPos: 0
229226 );
229227
229228 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
229229 if( rc==SQLITE_OK && nScore>nBestScore ){
229230 nBestScore = nScore;
229231 iBestCol = i;
229232 iBestStart = sFinder.aFirst[jj];
229233 nColSize = nDocsize;
229234 }
229235 }
229236 }
229237 }
229238 }
229239 }
229240
229241 if( rc==SQLITE_OK ){
229242 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
229243 }
229244 if( rc==SQLITE_OK && nColSize==0 ){
229245 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
229246 }
229247 if( ctx.zIn ){
229248 if( rc==SQLITE_OK ){
229249 rc = fts5CInstIterInit(pApi, pFts, iCol: iBestCol, pIter: &ctx.iter);
229250 }
229251
229252 ctx.iRangeStart = iBestStart;
229253 ctx.iRangeEnd = iBestStart + nToken - 1;
229254
229255 if( iBestStart>0 ){
229256 fts5HighlightAppend(pRc: &rc, p: &ctx, z: zEllips, n: -1);
229257 }
229258
229259 /* Advance iterator ctx.iter so that it points to the first coalesced
229260 ** phrase instance at or following position iBestStart. */
229261 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
229262 rc = fts5CInstIterNext(pIter: &ctx.iter);
229263 }
229264
229265 if( rc==SQLITE_OK ){
229266 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
229267 }
229268 if( ctx.iRangeEnd>=(nColSize-1) ){
229269 fts5HighlightAppend(pRc: &rc, p: &ctx, z: &ctx.zIn[ctx.iOff], n: ctx.nIn - ctx.iOff);
229270 }else{
229271 fts5HighlightAppend(pRc: &rc, p: &ctx, z: zEllips, n: -1);
229272 }
229273 }
229274 if( rc==SQLITE_OK ){
229275 sqlite3_result_text(pCtx, z: (const char*)ctx.zOut, n: -1, SQLITE_TRANSIENT);
229276 }else{
229277 sqlite3_result_error_code(pCtx, errCode: rc);
229278 }
229279 sqlite3_free(p: ctx.zOut);
229280 sqlite3_free(p: aSeen);
229281 sqlite3_free(p: sFinder.aFirst);
229282}
229283
229284/************************************************************************/
229285
229286/*
229287** The first time the bm25() function is called for a query, an instance
229288** of the following structure is allocated and populated.
229289*/
229290typedef struct Fts5Bm25Data Fts5Bm25Data;
229291struct Fts5Bm25Data {
229292 int nPhrase; /* Number of phrases in query */
229293 double avgdl; /* Average number of tokens in each row */
229294 double *aIDF; /* IDF for each phrase */
229295 double *aFreq; /* Array used to calculate phrase freq. */
229296};
229297
229298/*
229299** Callback used by fts5Bm25GetData() to count the number of rows in the
229300** table matched by each individual phrase within the query.
229301*/
229302static int fts5CountCb(
229303 const Fts5ExtensionApi *pApi,
229304 Fts5Context *pFts,
229305 void *pUserData /* Pointer to sqlite3_int64 variable */
229306){
229307 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
229308 UNUSED_PARAM2(pApi, pFts);
229309 (*pn)++;
229310 return SQLITE_OK;
229311}
229312
229313/*
229314** Set *ppData to point to the Fts5Bm25Data object for the current query.
229315** If the object has not already been allocated, allocate and populate it
229316** now.
229317*/
229318static int fts5Bm25GetData(
229319 const Fts5ExtensionApi *pApi,
229320 Fts5Context *pFts,
229321 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
229322){
229323 int rc = SQLITE_OK; /* Return code */
229324 Fts5Bm25Data *p; /* Object to return */
229325
229326 p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0);
229327 if( p==0 ){
229328 int nPhrase; /* Number of phrases in query */
229329 sqlite3_int64 nRow = 0; /* Number of rows in table */
229330 sqlite3_int64 nToken = 0; /* Number of tokens in table */
229331 sqlite3_int64 nByte; /* Bytes of space to allocate */
229332 int i;
229333
229334 /* Allocate the Fts5Bm25Data object */
229335 nPhrase = pApi->xPhraseCount(pFts);
229336 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
229337 p = (Fts5Bm25Data*)sqlite3_malloc64(n: nByte);
229338 if( p==0 ){
229339 rc = SQLITE_NOMEM;
229340 }else{
229341 memset(s: p, c: 0, n: (size_t)nByte);
229342 p->nPhrase = nPhrase;
229343 p->aIDF = (double*)&p[1];
229344 p->aFreq = &p->aIDF[nPhrase];
229345 }
229346
229347 /* Calculate the average document length for this FTS5 table */
229348 if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
229349 assert( rc!=SQLITE_OK || nRow>0 );
229350 if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
229351 if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow;
229352
229353 /* Calculate an IDF for each phrase in the query */
229354 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
229355 sqlite3_int64 nHit = 0;
229356 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
229357 if( rc==SQLITE_OK ){
229358 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
229359 ** This is done using the standard BM25 formula as found on wikipedia:
229360 **
229361 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
229362 **
229363 ** where "N" is the total number of documents in the set and nHit
229364 ** is the number that contain at least one instance of the phrase
229365 ** under consideration.
229366 **
229367 ** The problem with this is that if (N < 2*nHit), the IDF is
229368 ** negative. Which is undesirable. So the mimimum allowable IDF is
229369 ** (1e-6) - roughly the same as a term that appears in just over
229370 ** half of set of 5,000,000 documents. */
229371 double idf = log( x: (nRow - nHit + 0.5) / (nHit + 0.5) );
229372 if( idf<=0.0 ) idf = 1e-6;
229373 p->aIDF[i] = idf;
229374 }
229375 }
229376
229377 if( rc!=SQLITE_OK ){
229378 sqlite3_free(p);
229379 }else{
229380 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
229381 }
229382 if( rc!=SQLITE_OK ) p = 0;
229383 }
229384 *ppData = p;
229385 return rc;
229386}
229387
229388/*
229389** Implementation of bm25() function.
229390*/
229391static void fts5Bm25Function(
229392 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
229393 Fts5Context *pFts, /* First arg to pass to pApi functions */
229394 sqlite3_context *pCtx, /* Context for returning result/error */
229395 int nVal, /* Number of values in apVal[] array */
229396 sqlite3_value **apVal /* Array of trailing arguments */
229397){
229398 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
229399 const double b = 0.75; /* Constant "b" from BM25 formula */
229400 int rc; /* Error code */
229401 double score = 0.0; /* SQL function return value */
229402 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
229403 int i; /* Iterator variable */
229404 int nInst = 0; /* Value returned by xInstCount() */
229405 double D = 0.0; /* Total number of tokens in row */
229406 double *aFreq = 0; /* Array of phrase freq. for current row */
229407
229408 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
229409 ** for each phrase in the query for the current row. */
229410 rc = fts5Bm25GetData(pApi, pFts, ppData: &pData);
229411 if( rc==SQLITE_OK ){
229412 aFreq = pData->aFreq;
229413 memset(s: aFreq, c: 0, n: sizeof(double) * pData->nPhrase);
229414 rc = pApi->xInstCount(pFts, &nInst);
229415 }
229416 for(i=0; rc==SQLITE_OK && i<nInst; i++){
229417 int ip; int ic; int io;
229418 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
229419 if( rc==SQLITE_OK ){
229420 double w = (nVal > ic) ? sqlite3_value_double(pVal: apVal[ic]) : 1.0;
229421 aFreq[ip] += w;
229422 }
229423 }
229424
229425 /* Figure out the total size of the current row in tokens. */
229426 if( rc==SQLITE_OK ){
229427 int nTok;
229428 rc = pApi->xColumnSize(pFts, -1, &nTok);
229429 D = (double)nTok;
229430 }
229431
229432 /* Determine and return the BM25 score for the current row. Or, if an
229433 ** error has occurred, throw an exception. */
229434 if( rc==SQLITE_OK ){
229435 for(i=0; i<pData->nPhrase; i++){
229436 score += pData->aIDF[i] * (
229437 ( aFreq[i] * (k1 + 1.0) ) /
229438 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
229439 );
229440 }
229441 sqlite3_result_double(pCtx, rVal: -1.0 * score);
229442 }else{
229443 sqlite3_result_error_code(pCtx, errCode: rc);
229444 }
229445}
229446
229447static int sqlite3Fts5AuxInit(fts5_api *pApi){
229448 struct Builtin {
229449 const char *zFunc; /* Function name (nul-terminated) */
229450 void *pUserData; /* User-data pointer */
229451 fts5_extension_function xFunc;/* Callback function */
229452 void (*xDestroy)(void*); /* Destructor function */
229453 } aBuiltin [] = {
229454 { "snippet", 0, fts5SnippetFunction, 0 },
229455 { "highlight", 0, fts5HighlightFunction, 0 },
229456 { "bm25", 0, fts5Bm25Function, 0 },
229457 };
229458 int rc = SQLITE_OK; /* Return code */
229459 int i; /* To iterate through builtin functions */
229460
229461 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
229462 rc = pApi->xCreateFunction(pApi,
229463 aBuiltin[i].zFunc,
229464 aBuiltin[i].pUserData,
229465 aBuiltin[i].xFunc,
229466 aBuiltin[i].xDestroy
229467 );
229468 }
229469
229470 return rc;
229471}
229472
229473/*
229474** 2014 May 31
229475**
229476** The author disclaims copyright to this source code. In place of
229477** a legal notice, here is a blessing:
229478**
229479** May you do good and not evil.
229480** May you find forgiveness for yourself and forgive others.
229481** May you share freely, never taking more than you give.
229482**
229483******************************************************************************
229484*/
229485
229486
229487
229488/* #include "fts5Int.h" */
229489
229490static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
229491 if( (u32)pBuf->nSpace<nByte ){
229492 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
229493 u8 *pNew;
229494 while( nNew<nByte ){
229495 nNew = nNew * 2;
229496 }
229497 pNew = sqlite3_realloc64(pOld: pBuf->p, n: nNew);
229498 if( pNew==0 ){
229499 *pRc = SQLITE_NOMEM;
229500 return 1;
229501 }else{
229502 pBuf->nSpace = (int)nNew;
229503 pBuf->p = pNew;
229504 }
229505 }
229506 return 0;
229507}
229508
229509
229510/*
229511** Encode value iVal as an SQLite varint and append it to the buffer object
229512** pBuf. If an OOM error occurs, set the error code in p.
229513*/
229514static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
229515 if( fts5BufferGrow(pRc, pBuf, 9) ) return;
229516 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: iVal);
229517}
229518
229519static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
229520 aBuf[0] = (iVal>>24) & 0x00FF;
229521 aBuf[1] = (iVal>>16) & 0x00FF;
229522 aBuf[2] = (iVal>> 8) & 0x00FF;
229523 aBuf[3] = (iVal>> 0) & 0x00FF;
229524}
229525
229526static int sqlite3Fts5Get32(const u8 *aBuf){
229527 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
229528}
229529
229530/*
229531** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
229532** the error code in p. If an error has already occurred when this function
229533** is called, it is a no-op.
229534*/
229535static void sqlite3Fts5BufferAppendBlob(
229536 int *pRc,
229537 Fts5Buffer *pBuf,
229538 u32 nData,
229539 const u8 *pData
229540){
229541 if( nData ){
229542 if( fts5BufferGrow(pRc, pBuf, nData) ) return;
229543 memcpy(dest: &pBuf->p[pBuf->n], src: pData, n: nData);
229544 pBuf->n += nData;
229545 }
229546}
229547
229548/*
229549** Append the nul-terminated string zStr to the buffer pBuf. This function
229550** ensures that the byte following the buffer data is set to 0x00, even
229551** though this byte is not included in the pBuf->n count.
229552*/
229553static void sqlite3Fts5BufferAppendString(
229554 int *pRc,
229555 Fts5Buffer *pBuf,
229556 const char *zStr
229557){
229558 int nStr = (int)strlen(s: zStr);
229559 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData: nStr+1, pData: (const u8*)zStr);
229560 pBuf->n--;
229561}
229562
229563/*
229564** Argument zFmt is a printf() style format string. This function performs
229565** the printf() style processing, then appends the results to buffer pBuf.
229566**
229567** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
229568** following the buffer data is set to 0x00, even though this byte is not
229569** included in the pBuf->n count.
229570*/
229571static void sqlite3Fts5BufferAppendPrintf(
229572 int *pRc,
229573 Fts5Buffer *pBuf,
229574 char *zFmt, ...
229575){
229576 if( *pRc==SQLITE_OK ){
229577 char *zTmp;
229578 va_list ap;
229579 va_start(ap, zFmt);
229580 zTmp = sqlite3_vmprintf(zFormat: zFmt, ap);
229581 va_end(ap);
229582
229583 if( zTmp==0 ){
229584 *pRc = SQLITE_NOMEM;
229585 }else{
229586 sqlite3Fts5BufferAppendString(pRc, pBuf, zStr: zTmp);
229587 sqlite3_free(p: zTmp);
229588 }
229589 }
229590}
229591
229592static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
229593 char *zRet = 0;
229594 if( *pRc==SQLITE_OK ){
229595 va_list ap;
229596 va_start(ap, zFmt);
229597 zRet = sqlite3_vmprintf(zFormat: zFmt, ap);
229598 va_end(ap);
229599 if( zRet==0 ){
229600 *pRc = SQLITE_NOMEM;
229601 }
229602 }
229603 return zRet;
229604}
229605
229606
229607/*
229608** Free any buffer allocated by pBuf. Zero the structure before returning.
229609*/
229610static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
229611 sqlite3_free(p: pBuf->p);
229612 memset(s: pBuf, c: 0, n: sizeof(Fts5Buffer));
229613}
229614
229615/*
229616** Zero the contents of the buffer object. But do not free the associated
229617** memory allocation.
229618*/
229619static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
229620 pBuf->n = 0;
229621}
229622
229623/*
229624** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
229625** the error code in p. If an error has already occurred when this function
229626** is called, it is a no-op.
229627*/
229628static void sqlite3Fts5BufferSet(
229629 int *pRc,
229630 Fts5Buffer *pBuf,
229631 int nData,
229632 const u8 *pData
229633){
229634 pBuf->n = 0;
229635 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
229636}
229637
229638static int sqlite3Fts5PoslistNext64(
229639 const u8 *a, int n, /* Buffer containing poslist */
229640 int *pi, /* IN/OUT: Offset within a[] */
229641 i64 *piOff /* IN/OUT: Current offset */
229642){
229643 int i = *pi;
229644 if( i>=n ){
229645 /* EOF */
229646 *piOff = -1;
229647 return 1;
229648 }else{
229649 i64 iOff = *piOff;
229650 u32 iVal;
229651 fts5FastGetVarint32(a, i, iVal);
229652 if( iVal<=1 ){
229653 if( iVal==0 ){
229654 *pi = i;
229655 return 0;
229656 }
229657 fts5FastGetVarint32(a, i, iVal);
229658 iOff = ((i64)iVal) << 32;
229659 assert( iOff>=0 );
229660 fts5FastGetVarint32(a, i, iVal);
229661 if( iVal<2 ){
229662 /* This is a corrupt record. So stop parsing it here. */
229663 *piOff = -1;
229664 return 1;
229665 }
229666 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
229667 }else{
229668 *piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF);
229669 }
229670 *pi = i;
229671 assert_nc( *piOff>=iOff );
229672 return 0;
229673 }
229674}
229675
229676
229677/*
229678** Advance the iterator object passed as the only argument. Return true
229679** if the iterator reaches EOF, or false otherwise.
229680*/
229681static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
229682 if( sqlite3Fts5PoslistNext64(a: pIter->a, n: pIter->n, pi: &pIter->i, piOff: &pIter->iPos) ){
229683 pIter->bEof = 1;
229684 }
229685 return pIter->bEof;
229686}
229687
229688static int sqlite3Fts5PoslistReaderInit(
229689 const u8 *a, int n, /* Poslist buffer to iterate through */
229690 Fts5PoslistReader *pIter /* Iterator object to initialize */
229691){
229692 memset(s: pIter, c: 0, n: sizeof(*pIter));
229693 pIter->a = a;
229694 pIter->n = n;
229695 sqlite3Fts5PoslistReaderNext(pIter);
229696 return pIter->bEof;
229697}
229698
229699/*
229700** Append position iPos to the position list being accumulated in buffer
229701** pBuf, which must be already be large enough to hold the new data.
229702** The previous position written to this list is *piPrev. *piPrev is set
229703** to iPos before returning.
229704*/
229705static void sqlite3Fts5PoslistSafeAppend(
229706 Fts5Buffer *pBuf,
229707 i64 *piPrev,
229708 i64 iPos
229709){
229710 if( iPos>=*piPrev ){
229711 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
229712 if( (iPos & colmask) != (*piPrev & colmask) ){
229713 pBuf->p[pBuf->n++] = 1;
229714 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: (iPos>>32));
229715 *piPrev = (iPos & colmask);
229716 }
229717 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: (iPos-*piPrev)+2);
229718 *piPrev = iPos;
229719 }
229720}
229721
229722static int sqlite3Fts5PoslistWriterAppend(
229723 Fts5Buffer *pBuf,
229724 Fts5PoslistWriter *pWriter,
229725 i64 iPos
229726){
229727 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
229728 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
229729 sqlite3Fts5PoslistSafeAppend(pBuf, piPrev: &pWriter->iPrev, iPos);
229730 return SQLITE_OK;
229731}
229732
229733static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
229734 void *pRet = 0;
229735 if( *pRc==SQLITE_OK ){
229736 pRet = sqlite3_malloc64(n: nByte);
229737 if( pRet==0 ){
229738 if( nByte>0 ) *pRc = SQLITE_NOMEM;
229739 }else{
229740 memset(s: pRet, c: 0, n: (size_t)nByte);
229741 }
229742 }
229743 return pRet;
229744}
229745
229746/*
229747** Return a nul-terminated copy of the string indicated by pIn. If nIn
229748** is non-negative, then it is the length of the string in bytes. Otherwise,
229749** the length of the string is determined using strlen().
229750**
229751** It is the responsibility of the caller to eventually free the returned
229752** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
229753*/
229754static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
229755 char *zRet = 0;
229756 if( *pRc==SQLITE_OK ){
229757 if( nIn<0 ){
229758 nIn = (int)strlen(s: pIn);
229759 }
229760 zRet = (char*)sqlite3_malloc(n: nIn+1);
229761 if( zRet ){
229762 memcpy(dest: zRet, src: pIn, n: nIn);
229763 zRet[nIn] = '\0';
229764 }else{
229765 *pRc = SQLITE_NOMEM;
229766 }
229767 }
229768 return zRet;
229769}
229770
229771
229772/*
229773** Return true if character 't' may be part of an FTS5 bareword, or false
229774** otherwise. Characters that may be part of barewords:
229775**
229776** * All non-ASCII characters,
229777** * The 52 upper and lower case ASCII characters, and
229778** * The 10 integer ASCII characters.
229779** * The underscore character "_" (0x5F).
229780** * The unicode "subsitute" character (0x1A).
229781*/
229782static int sqlite3Fts5IsBareword(char t){
229783 u8 aBareword[128] = {
229784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
229785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
229786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
229787 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
229788 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
229789 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
229790 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
229791 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
229792 };
229793
229794 return (t & 0x80) || aBareword[(int)t];
229795}
229796
229797
229798/*************************************************************************
229799*/
229800typedef struct Fts5TermsetEntry Fts5TermsetEntry;
229801struct Fts5TermsetEntry {
229802 char *pTerm;
229803 int nTerm;
229804 int iIdx; /* Index (main or aPrefix[] entry) */
229805 Fts5TermsetEntry *pNext;
229806};
229807
229808struct Fts5Termset {
229809 Fts5TermsetEntry *apHash[512];
229810};
229811
229812static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
229813 int rc = SQLITE_OK;
229814 *pp = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Termset));
229815 return rc;
229816}
229817
229818static int sqlite3Fts5TermsetAdd(
229819 Fts5Termset *p,
229820 int iIdx,
229821 const char *pTerm, int nTerm,
229822 int *pbPresent
229823){
229824 int rc = SQLITE_OK;
229825 *pbPresent = 0;
229826 if( p ){
229827 int i;
229828 u32 hash = 13;
229829 Fts5TermsetEntry *pEntry;
229830
229831 /* Calculate a hash value for this term. This is the same hash checksum
229832 ** used by the fts5_hash.c module. This is not important for correct
229833 ** operation of the module, but is necessary to ensure that some tests
229834 ** designed to produce hash table collisions really do work. */
229835 for(i=nTerm-1; i>=0; i--){
229836 hash = (hash << 3) ^ hash ^ pTerm[i];
229837 }
229838 hash = (hash << 3) ^ hash ^ iIdx;
229839 hash = hash % ArraySize(p->apHash);
229840
229841 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
229842 if( pEntry->iIdx==iIdx
229843 && pEntry->nTerm==nTerm
229844 && memcmp(s1: pEntry->pTerm, s2: pTerm, n: nTerm)==0
229845 ){
229846 *pbPresent = 1;
229847 break;
229848 }
229849 }
229850
229851 if( pEntry==0 ){
229852 pEntry = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5TermsetEntry) + nTerm);
229853 if( pEntry ){
229854 pEntry->pTerm = (char*)&pEntry[1];
229855 pEntry->nTerm = nTerm;
229856 pEntry->iIdx = iIdx;
229857 memcpy(dest: pEntry->pTerm, src: pTerm, n: nTerm);
229858 pEntry->pNext = p->apHash[hash];
229859 p->apHash[hash] = pEntry;
229860 }
229861 }
229862 }
229863
229864 return rc;
229865}
229866
229867static void sqlite3Fts5TermsetFree(Fts5Termset *p){
229868 if( p ){
229869 u32 i;
229870 for(i=0; i<ArraySize(p->apHash); i++){
229871 Fts5TermsetEntry *pEntry = p->apHash[i];
229872 while( pEntry ){
229873 Fts5TermsetEntry *pDel = pEntry;
229874 pEntry = pEntry->pNext;
229875 sqlite3_free(p: pDel);
229876 }
229877 }
229878 sqlite3_free(p);
229879 }
229880}
229881
229882/*
229883** 2014 Jun 09
229884**
229885** The author disclaims copyright to this source code. In place of
229886** a legal notice, here is a blessing:
229887**
229888** May you do good and not evil.
229889** May you find forgiveness for yourself and forgive others.
229890** May you share freely, never taking more than you give.
229891**
229892******************************************************************************
229893**
229894** This is an SQLite module implementing full-text search.
229895*/
229896
229897
229898/* #include "fts5Int.h" */
229899
229900#define FTS5_DEFAULT_PAGE_SIZE 4050
229901#define FTS5_DEFAULT_AUTOMERGE 4
229902#define FTS5_DEFAULT_USERMERGE 4
229903#define FTS5_DEFAULT_CRISISMERGE 16
229904#define FTS5_DEFAULT_HASHSIZE (1024*1024)
229905
229906#define FTS5_DEFAULT_DELETE_AUTOMERGE 10 /* default 10% */
229907
229908/* Maximum allowed page size */
229909#define FTS5_MAX_PAGE_SIZE (64*1024)
229910
229911static int fts5_iswhitespace(char x){
229912 return (x==' ');
229913}
229914
229915static int fts5_isopenquote(char x){
229916 return (x=='"' || x=='\'' || x=='[' || x=='`');
229917}
229918
229919/*
229920** Argument pIn points to a character that is part of a nul-terminated
229921** string. Return a pointer to the first character following *pIn in
229922** the string that is not a white-space character.
229923*/
229924static const char *fts5ConfigSkipWhitespace(const char *pIn){
229925 const char *p = pIn;
229926 if( p ){
229927 while( fts5_iswhitespace(x: *p) ){ p++; }
229928 }
229929 return p;
229930}
229931
229932/*
229933** Argument pIn points to a character that is part of a nul-terminated
229934** string. Return a pointer to the first character following *pIn in
229935** the string that is not a "bareword" character.
229936*/
229937static const char *fts5ConfigSkipBareword(const char *pIn){
229938 const char *p = pIn;
229939 while ( sqlite3Fts5IsBareword(t: *p) ) p++;
229940 if( p==pIn ) p = 0;
229941 return p;
229942}
229943
229944static int fts5_isdigit(char a){
229945 return (a>='0' && a<='9');
229946}
229947
229948
229949
229950static const char *fts5ConfigSkipLiteral(const char *pIn){
229951 const char *p = pIn;
229952 switch( *p ){
229953 case 'n': case 'N':
229954 if( sqlite3_strnicmp(zLeft: "null", zRight: p, N: 4)==0 ){
229955 p = &p[4];
229956 }else{
229957 p = 0;
229958 }
229959 break;
229960
229961 case 'x': case 'X':
229962 p++;
229963 if( *p=='\'' ){
229964 p++;
229965 while( (*p>='a' && *p<='f')
229966 || (*p>='A' && *p<='F')
229967 || (*p>='0' && *p<='9')
229968 ){
229969 p++;
229970 }
229971 if( *p=='\'' && 0==((p-pIn)%2) ){
229972 p++;
229973 }else{
229974 p = 0;
229975 }
229976 }else{
229977 p = 0;
229978 }
229979 break;
229980
229981 case '\'':
229982 p++;
229983 while( p ){
229984 if( *p=='\'' ){
229985 p++;
229986 if( *p!='\'' ) break;
229987 }
229988 p++;
229989 if( *p==0 ) p = 0;
229990 }
229991 break;
229992
229993 default:
229994 /* maybe a number */
229995 if( *p=='+' || *p=='-' ) p++;
229996 while( fts5_isdigit(a: *p) ) p++;
229997
229998 /* At this point, if the literal was an integer, the parse is
229999 ** finished. Or, if it is a floating point value, it may continue
230000 ** with either a decimal point or an 'E' character. */
230001 if( *p=='.' && fts5_isdigit(a: p[1]) ){
230002 p += 2;
230003 while( fts5_isdigit(a: *p) ) p++;
230004 }
230005 if( p==pIn ) p = 0;
230006
230007 break;
230008 }
230009
230010 return p;
230011}
230012
230013/*
230014** The first character of the string pointed to by argument z is guaranteed
230015** to be an open-quote character (see function fts5_isopenquote()).
230016**
230017** This function searches for the corresponding close-quote character within
230018** the string and, if found, dequotes the string in place and adds a new
230019** nul-terminator byte.
230020**
230021** If the close-quote is found, the value returned is the byte offset of
230022** the character immediately following it. Or, if the close-quote is not
230023** found, -1 is returned. If -1 is returned, the buffer is left in an
230024** undefined state.
230025*/
230026static int fts5Dequote(char *z){
230027 char q;
230028 int iIn = 1;
230029 int iOut = 0;
230030 q = z[0];
230031
230032 /* Set stack variable q to the close-quote character */
230033 assert( q=='[' || q=='\'' || q=='"' || q=='`' );
230034 if( q=='[' ) q = ']';
230035
230036 while( z[iIn] ){
230037 if( z[iIn]==q ){
230038 if( z[iIn+1]!=q ){
230039 /* Character iIn was the close quote. */
230040 iIn++;
230041 break;
230042 }else{
230043 /* Character iIn and iIn+1 form an escaped quote character. Skip
230044 ** the input cursor past both and copy a single quote character
230045 ** to the output buffer. */
230046 iIn += 2;
230047 z[iOut++] = q;
230048 }
230049 }else{
230050 z[iOut++] = z[iIn++];
230051 }
230052 }
230053
230054 z[iOut] = '\0';
230055 return iIn;
230056}
230057
230058/*
230059** Convert an SQL-style quoted string into a normal string by removing
230060** the quote characters. The conversion is done in-place. If the
230061** input does not begin with a quote character, then this routine
230062** is a no-op.
230063**
230064** Examples:
230065**
230066** "abc" becomes abc
230067** 'xyz' becomes xyz
230068** [pqr] becomes pqr
230069** `mno` becomes mno
230070*/
230071static void sqlite3Fts5Dequote(char *z){
230072 char quote; /* Quote character (if any ) */
230073
230074 assert( 0==fts5_iswhitespace(z[0]) );
230075 quote = z[0];
230076 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
230077 fts5Dequote(z);
230078 }
230079}
230080
230081
230082struct Fts5Enum {
230083 const char *zName;
230084 int eVal;
230085};
230086typedef struct Fts5Enum Fts5Enum;
230087
230088static int fts5ConfigSetEnum(
230089 const Fts5Enum *aEnum,
230090 const char *zEnum,
230091 int *peVal
230092){
230093 int nEnum = (int)strlen(s: zEnum);
230094 int i;
230095 int iVal = -1;
230096
230097 for(i=0; aEnum[i].zName; i++){
230098 if( sqlite3_strnicmp(zLeft: aEnum[i].zName, zRight: zEnum, N: nEnum)==0 ){
230099 if( iVal>=0 ) return SQLITE_ERROR;
230100 iVal = aEnum[i].eVal;
230101 }
230102 }
230103
230104 *peVal = iVal;
230105 return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
230106}
230107
230108/*
230109** Parse a "special" CREATE VIRTUAL TABLE directive and update
230110** configuration object pConfig as appropriate.
230111**
230112** If successful, object pConfig is updated and SQLITE_OK returned. If
230113** an error occurs, an SQLite error code is returned and an error message
230114** may be left in *pzErr. It is the responsibility of the caller to
230115** eventually free any such error message using sqlite3_free().
230116*/
230117static int fts5ConfigParseSpecial(
230118 Fts5Global *pGlobal,
230119 Fts5Config *pConfig, /* Configuration object to update */
230120 const char *zCmd, /* Special command to parse */
230121 const char *zArg, /* Argument to parse */
230122 char **pzErr /* OUT: Error message */
230123){
230124 int rc = SQLITE_OK;
230125 int nCmd = (int)strlen(s: zCmd);
230126 if( sqlite3_strnicmp(zLeft: "prefix", zRight: zCmd, N: nCmd)==0 ){
230127 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
230128 const char *p;
230129 int bFirst = 1;
230130 if( pConfig->aPrefix==0 ){
230131 pConfig->aPrefix = sqlite3Fts5MallocZero(pRc: &rc, nByte);
230132 if( rc ) return rc;
230133 }
230134
230135 p = zArg;
230136 while( 1 ){
230137 int nPre = 0;
230138
230139 while( p[0]==' ' ) p++;
230140 if( bFirst==0 && p[0]==',' ){
230141 p++;
230142 while( p[0]==' ' ) p++;
230143 }else if( p[0]=='\0' ){
230144 break;
230145 }
230146 if( p[0]<'0' || p[0]>'9' ){
230147 *pzErr = sqlite3_mprintf(zFormat: "malformed prefix=... directive");
230148 rc = SQLITE_ERROR;
230149 break;
230150 }
230151
230152 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
230153 *pzErr = sqlite3_mprintf(
230154 zFormat: "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
230155 );
230156 rc = SQLITE_ERROR;
230157 break;
230158 }
230159
230160 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
230161 nPre = nPre*10 + (p[0] - '0');
230162 p++;
230163 }
230164
230165 if( nPre<=0 || nPre>=1000 ){
230166 *pzErr = sqlite3_mprintf(zFormat: "prefix length out of range (max 999)");
230167 rc = SQLITE_ERROR;
230168 break;
230169 }
230170
230171 pConfig->aPrefix[pConfig->nPrefix] = nPre;
230172 pConfig->nPrefix++;
230173 bFirst = 0;
230174 }
230175 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
230176 return rc;
230177 }
230178
230179 if( sqlite3_strnicmp(zLeft: "tokenize", zRight: zCmd, N: nCmd)==0 ){
230180 const char *p = (const char*)zArg;
230181 sqlite3_int64 nArg = strlen(s: zArg) + 1;
230182 char **azArg = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(char*) * nArg);
230183 char *pDel = sqlite3Fts5MallocZero(pRc: &rc, nByte: nArg * 2);
230184 char *pSpace = pDel;
230185
230186 if( azArg && pSpace ){
230187 if( pConfig->pTok ){
230188 *pzErr = sqlite3_mprintf(zFormat: "multiple tokenize=... directives");
230189 rc = SQLITE_ERROR;
230190 }else{
230191 for(nArg=0; p && *p; nArg++){
230192 const char *p2 = fts5ConfigSkipWhitespace(pIn: p);
230193 if( *p2=='\'' ){
230194 p = fts5ConfigSkipLiteral(pIn: p2);
230195 }else{
230196 p = fts5ConfigSkipBareword(pIn: p2);
230197 }
230198 if( p ){
230199 memcpy(dest: pSpace, src: p2, n: p-p2);
230200 azArg[nArg] = pSpace;
230201 sqlite3Fts5Dequote(z: pSpace);
230202 pSpace += (p - p2) + 1;
230203 p = fts5ConfigSkipWhitespace(pIn: p);
230204 }
230205 }
230206 if( p==0 ){
230207 *pzErr = sqlite3_mprintf(zFormat: "parse error in tokenize directive");
230208 rc = SQLITE_ERROR;
230209 }else{
230210 rc = sqlite3Fts5GetTokenizer(pGlobal,
230211 azArg: (const char**)azArg, nArg: (int)nArg, pConfig,
230212 pzErr
230213 );
230214 }
230215 }
230216 }
230217
230218 sqlite3_free(p: azArg);
230219 sqlite3_free(p: pDel);
230220 return rc;
230221 }
230222
230223 if( sqlite3_strnicmp(zLeft: "content", zRight: zCmd, N: nCmd)==0 ){
230224 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
230225 *pzErr = sqlite3_mprintf(zFormat: "multiple content=... directives");
230226 rc = SQLITE_ERROR;
230227 }else{
230228 if( zArg[0] ){
230229 pConfig->eContent = FTS5_CONTENT_EXTERNAL;
230230 pConfig->zContent = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%Q.%Q", pConfig->zDb,zArg);
230231 }else{
230232 pConfig->eContent = FTS5_CONTENT_NONE;
230233 }
230234 }
230235 return rc;
230236 }
230237
230238 if( sqlite3_strnicmp(zLeft: "contentless_delete", zRight: zCmd, N: nCmd)==0 ){
230239 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
230240 *pzErr = sqlite3_mprintf(zFormat: "malformed contentless_delete=... directive");
230241 rc = SQLITE_ERROR;
230242 }else{
230243 pConfig->bContentlessDelete = (zArg[0]=='1');
230244 }
230245 return rc;
230246 }
230247
230248 if( sqlite3_strnicmp(zLeft: "content_rowid", zRight: zCmd, N: nCmd)==0 ){
230249 if( pConfig->zContentRowid ){
230250 *pzErr = sqlite3_mprintf(zFormat: "multiple content_rowid=... directives");
230251 rc = SQLITE_ERROR;
230252 }else{
230253 pConfig->zContentRowid = sqlite3Fts5Strndup(pRc: &rc, pIn: zArg, nIn: -1);
230254 }
230255 return rc;
230256 }
230257
230258 if( sqlite3_strnicmp(zLeft: "columnsize", zRight: zCmd, N: nCmd)==0 ){
230259 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
230260 *pzErr = sqlite3_mprintf(zFormat: "malformed columnsize=... directive");
230261 rc = SQLITE_ERROR;
230262 }else{
230263 pConfig->bColumnsize = (zArg[0]=='1');
230264 }
230265 return rc;
230266 }
230267
230268 if( sqlite3_strnicmp(zLeft: "detail", zRight: zCmd, N: nCmd)==0 ){
230269 const Fts5Enum aDetail[] = {
230270 { "none", FTS5_DETAIL_NONE },
230271 { "full", FTS5_DETAIL_FULL },
230272 { "columns", FTS5_DETAIL_COLUMNS },
230273 { 0, 0 }
230274 };
230275
230276 if( (rc = fts5ConfigSetEnum(aEnum: aDetail, zEnum: zArg, peVal: &pConfig->eDetail)) ){
230277 *pzErr = sqlite3_mprintf(zFormat: "malformed detail=... directive");
230278 }
230279 return rc;
230280 }
230281
230282 *pzErr = sqlite3_mprintf(zFormat: "unrecognized option: \"%.*s\"", nCmd, zCmd);
230283 return SQLITE_ERROR;
230284}
230285
230286/*
230287** Allocate an instance of the default tokenizer ("simple") at
230288** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
230289** code if an error occurs.
230290*/
230291static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
230292 assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
230293 return sqlite3Fts5GetTokenizer(pGlobal, azArg: 0, nArg: 0, pConfig, pzErr: 0);
230294}
230295
230296/*
230297** Gobble up the first bareword or quoted word from the input buffer zIn.
230298** Return a pointer to the character immediately following the last in
230299** the gobbled word if successful, or a NULL pointer otherwise (failed
230300** to find close-quote character).
230301**
230302** Before returning, set pzOut to point to a new buffer containing a
230303** nul-terminated, dequoted copy of the gobbled word. If the word was
230304** quoted, *pbQuoted is also set to 1 before returning.
230305**
230306** If *pRc is other than SQLITE_OK when this function is called, it is
230307** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
230308** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
230309** set if a parse error (failed to find close quote) occurs.
230310*/
230311static const char *fts5ConfigGobbleWord(
230312 int *pRc, /* IN/OUT: Error code */
230313 const char *zIn, /* Buffer to gobble string/bareword from */
230314 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
230315 int *pbQuoted /* OUT: Set to true if dequoting required */
230316){
230317 const char *zRet = 0;
230318
230319 sqlite3_int64 nIn = strlen(s: zIn);
230320 char *zOut = sqlite3_malloc64(n: nIn+1);
230321
230322 assert( *pRc==SQLITE_OK );
230323 *pbQuoted = 0;
230324 *pzOut = 0;
230325
230326 if( zOut==0 ){
230327 *pRc = SQLITE_NOMEM;
230328 }else{
230329 memcpy(dest: zOut, src: zIn, n: (size_t)(nIn+1));
230330 if( fts5_isopenquote(x: zOut[0]) ){
230331 int ii = fts5Dequote(z: zOut);
230332 zRet = &zIn[ii];
230333 *pbQuoted = 1;
230334 }else{
230335 zRet = fts5ConfigSkipBareword(pIn: zIn);
230336 if( zRet ){
230337 zOut[zRet-zIn] = '\0';
230338 }
230339 }
230340 }
230341
230342 if( zRet==0 ){
230343 sqlite3_free(p: zOut);
230344 }else{
230345 *pzOut = zOut;
230346 }
230347
230348 return zRet;
230349}
230350
230351static int fts5ConfigParseColumn(
230352 Fts5Config *p,
230353 char *zCol,
230354 char *zArg,
230355 char **pzErr
230356){
230357 int rc = SQLITE_OK;
230358 if( 0==sqlite3_stricmp(zLeft: zCol, FTS5_RANK_NAME)
230359 || 0==sqlite3_stricmp(zLeft: zCol, FTS5_ROWID_NAME)
230360 ){
230361 *pzErr = sqlite3_mprintf(zFormat: "reserved fts5 column name: %s", zCol);
230362 rc = SQLITE_ERROR;
230363 }else if( zArg ){
230364 if( 0==sqlite3_stricmp(zLeft: zArg, zRight: "unindexed") ){
230365 p->abUnindexed[p->nCol] = 1;
230366 }else{
230367 *pzErr = sqlite3_mprintf(zFormat: "unrecognized column option: %s", zArg);
230368 rc = SQLITE_ERROR;
230369 }
230370 }
230371
230372 p->azCol[p->nCol++] = zCol;
230373 return rc;
230374}
230375
230376/*
230377** Populate the Fts5Config.zContentExprlist string.
230378*/
230379static int fts5ConfigMakeExprlist(Fts5Config *p){
230380 int i;
230381 int rc = SQLITE_OK;
230382 Fts5Buffer buf = {0, 0, 0};
230383
230384 sqlite3Fts5BufferAppendPrintf(pRc: &rc, pBuf: &buf, zFmt: "T.%Q", p->zContentRowid);
230385 if( p->eContent!=FTS5_CONTENT_NONE ){
230386 for(i=0; i<p->nCol; i++){
230387 if( p->eContent==FTS5_CONTENT_EXTERNAL ){
230388 sqlite3Fts5BufferAppendPrintf(pRc: &rc, pBuf: &buf, zFmt: ", T.%Q", p->azCol[i]);
230389 }else{
230390 sqlite3Fts5BufferAppendPrintf(pRc: &rc, pBuf: &buf, zFmt: ", T.c%d", i);
230391 }
230392 }
230393 }
230394
230395 assert( p->zContentExprlist==0 );
230396 p->zContentExprlist = (char*)buf.p;
230397 return rc;
230398}
230399
230400/*
230401** Arguments nArg/azArg contain the string arguments passed to the xCreate
230402** or xConnect method of the virtual table. This function attempts to
230403** allocate an instance of Fts5Config containing the results of parsing
230404** those arguments.
230405**
230406** If successful, SQLITE_OK is returned and *ppOut is set to point to the
230407** new Fts5Config object. If an error occurs, an SQLite error code is
230408** returned, *ppOut is set to NULL and an error message may be left in
230409** *pzErr. It is the responsibility of the caller to eventually free any
230410** such error message using sqlite3_free().
230411*/
230412static int sqlite3Fts5ConfigParse(
230413 Fts5Global *pGlobal,
230414 sqlite3 *db,
230415 int nArg, /* Number of arguments */
230416 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
230417 Fts5Config **ppOut, /* OUT: Results of parse */
230418 char **pzErr /* OUT: Error message */
230419){
230420 int rc = SQLITE_OK; /* Return code */
230421 Fts5Config *pRet; /* New object to return */
230422 int i;
230423 sqlite3_int64 nByte;
230424
230425 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(n: sizeof(Fts5Config));
230426 if( pRet==0 ) return SQLITE_NOMEM;
230427 memset(s: pRet, c: 0, n: sizeof(Fts5Config));
230428 pRet->db = db;
230429 pRet->iCookie = -1;
230430
230431 nByte = nArg * (sizeof(char*) + sizeof(u8));
230432 pRet->azCol = (char**)sqlite3Fts5MallocZero(pRc: &rc, nByte);
230433 pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0;
230434 pRet->zDb = sqlite3Fts5Strndup(pRc: &rc, pIn: azArg[1], nIn: -1);
230435 pRet->zName = sqlite3Fts5Strndup(pRc: &rc, pIn: azArg[2], nIn: -1);
230436 pRet->bColumnsize = 1;
230437 pRet->eDetail = FTS5_DETAIL_FULL;
230438#ifdef SQLITE_DEBUG
230439 pRet->bPrefixIndex = 1;
230440#endif
230441 if( rc==SQLITE_OK && sqlite3_stricmp(zLeft: pRet->zName, FTS5_RANK_NAME)==0 ){
230442 *pzErr = sqlite3_mprintf(zFormat: "reserved fts5 table name: %s", pRet->zName);
230443 rc = SQLITE_ERROR;
230444 }
230445
230446 assert( (pRet->abUnindexed && pRet->azCol) || rc!=SQLITE_OK );
230447 for(i=3; rc==SQLITE_OK && i<nArg; i++){
230448 const char *zOrig = azArg[i];
230449 const char *z;
230450 char *zOne = 0;
230451 char *zTwo = 0;
230452 int bOption = 0;
230453 int bMustBeCol = 0;
230454
230455 z = fts5ConfigGobbleWord(pRc: &rc, zIn: zOrig, pzOut: &zOne, pbQuoted: &bMustBeCol);
230456 z = fts5ConfigSkipWhitespace(pIn: z);
230457 if( z && *z=='=' ){
230458 bOption = 1;
230459 assert( zOne!=0 );
230460 z++;
230461 if( bMustBeCol ) z = 0;
230462 }
230463 z = fts5ConfigSkipWhitespace(pIn: z);
230464 if( z && z[0] ){
230465 int bDummy;
230466 z = fts5ConfigGobbleWord(pRc: &rc, zIn: z, pzOut: &zTwo, pbQuoted: &bDummy);
230467 if( z && z[0] ) z = 0;
230468 }
230469
230470 if( rc==SQLITE_OK ){
230471 if( z==0 ){
230472 *pzErr = sqlite3_mprintf(zFormat: "parse error in \"%s\"", zOrig);
230473 rc = SQLITE_ERROR;
230474 }else{
230475 if( bOption ){
230476 rc = fts5ConfigParseSpecial(pGlobal, pConfig: pRet,
230477 ALWAYS(zOne)?zOne:"",
230478 zArg: zTwo?zTwo:"",
230479 pzErr
230480 );
230481 }else{
230482 rc = fts5ConfigParseColumn(p: pRet, zCol: zOne, zArg: zTwo, pzErr);
230483 zOne = 0;
230484 }
230485 }
230486 }
230487
230488 sqlite3_free(p: zOne);
230489 sqlite3_free(p: zTwo);
230490 }
230491
230492 /* We only allow contentless_delete=1 if the table is indeed contentless. */
230493 if( rc==SQLITE_OK
230494 && pRet->bContentlessDelete
230495 && pRet->eContent!=FTS5_CONTENT_NONE
230496 ){
230497 *pzErr = sqlite3_mprintf(
230498 zFormat: "contentless_delete=1 requires a contentless table"
230499 );
230500 rc = SQLITE_ERROR;
230501 }
230502
230503 /* We only allow contentless_delete=1 if columnsize=0 is not present.
230504 **
230505 ** This restriction may be removed at some point.
230506 */
230507 if( rc==SQLITE_OK && pRet->bContentlessDelete && pRet->bColumnsize==0 ){
230508 *pzErr = sqlite3_mprintf(
230509 zFormat: "contentless_delete=1 is incompatible with columnsize=0"
230510 );
230511 rc = SQLITE_ERROR;
230512 }
230513
230514 /* If a tokenizer= option was successfully parsed, the tokenizer has
230515 ** already been allocated. Otherwise, allocate an instance of the default
230516 ** tokenizer (unicode61) now. */
230517 if( rc==SQLITE_OK && pRet->pTok==0 ){
230518 rc = fts5ConfigDefaultTokenizer(pGlobal, pConfig: pRet);
230519 }
230520
230521 /* If no zContent option was specified, fill in the default values. */
230522 if( rc==SQLITE_OK && pRet->zContent==0 ){
230523 const char *zTail = 0;
230524 assert( pRet->eContent==FTS5_CONTENT_NORMAL
230525 || pRet->eContent==FTS5_CONTENT_NONE
230526 );
230527 if( pRet->eContent==FTS5_CONTENT_NORMAL ){
230528 zTail = "content";
230529 }else if( pRet->bColumnsize ){
230530 zTail = "docsize";
230531 }
230532
230533 if( zTail ){
230534 pRet->zContent = sqlite3Fts5Mprintf(
230535 pRc: &rc, zFmt: "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
230536 );
230537 }
230538 }
230539
230540 if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
230541 pRet->zContentRowid = sqlite3Fts5Strndup(pRc: &rc, pIn: "rowid", nIn: -1);
230542 }
230543
230544 /* Formulate the zContentExprlist text */
230545 if( rc==SQLITE_OK ){
230546 rc = fts5ConfigMakeExprlist(p: pRet);
230547 }
230548
230549 if( rc!=SQLITE_OK ){
230550 sqlite3Fts5ConfigFree(pRet);
230551 *ppOut = 0;
230552 }
230553 return rc;
230554}
230555
230556/*
230557** Free the configuration object passed as the only argument.
230558*/
230559static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
230560 if( pConfig ){
230561 int i;
230562 if( pConfig->pTok ){
230563 pConfig->pTokApi->xDelete(pConfig->pTok);
230564 }
230565 sqlite3_free(p: pConfig->zDb);
230566 sqlite3_free(p: pConfig->zName);
230567 for(i=0; i<pConfig->nCol; i++){
230568 sqlite3_free(p: pConfig->azCol[i]);
230569 }
230570 sqlite3_free(p: pConfig->azCol);
230571 sqlite3_free(p: pConfig->aPrefix);
230572 sqlite3_free(p: pConfig->zRank);
230573 sqlite3_free(p: pConfig->zRankArgs);
230574 sqlite3_free(p: pConfig->zContent);
230575 sqlite3_free(p: pConfig->zContentRowid);
230576 sqlite3_free(p: pConfig->zContentExprlist);
230577 sqlite3_free(p: pConfig);
230578 }
230579}
230580
230581/*
230582** Call sqlite3_declare_vtab() based on the contents of the configuration
230583** object passed as the only argument. Return SQLITE_OK if successful, or
230584** an SQLite error code if an error occurs.
230585*/
230586static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
230587 int i;
230588 int rc = SQLITE_OK;
230589 char *zSql;
230590
230591 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "CREATE TABLE x(");
230592 for(i=0; zSql && i<pConfig->nCol; i++){
230593 const char *zSep = (i==0?"":", ");
230594 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
230595 }
230596 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%z, %Q HIDDEN, %s HIDDEN)",
230597 zSql, pConfig->zName, FTS5_RANK_NAME
230598 );
230599
230600 assert( zSql || rc==SQLITE_NOMEM );
230601 if( zSql ){
230602 rc = sqlite3_declare_vtab(db: pConfig->db, zCreateTable: zSql);
230603 sqlite3_free(p: zSql);
230604 }
230605
230606 return rc;
230607}
230608
230609/*
230610** Tokenize the text passed via the second and third arguments.
230611**
230612** The callback is invoked once for each token in the input text. The
230613** arguments passed to it are, in order:
230614**
230615** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
230616** const char *pToken // Pointer to buffer containing token
230617** int nToken // Size of token in bytes
230618** int iStart // Byte offset of start of token within input text
230619** int iEnd // Byte offset of end of token within input text
230620** int iPos // Position of token in input (first token is 0)
230621**
230622** If the callback returns a non-zero value the tokenization is abandoned
230623** and no further callbacks are issued.
230624**
230625** This function returns SQLITE_OK if successful or an SQLite error code
230626** if an error occurs. If the tokenization was abandoned early because
230627** the callback returned SQLITE_DONE, this is not an error and this function
230628** still returns SQLITE_OK. Or, if the tokenization was abandoned early
230629** because the callback returned another non-zero value, it is assumed
230630** to be an SQLite error code and returned to the caller.
230631*/
230632static int sqlite3Fts5Tokenize(
230633 Fts5Config *pConfig, /* FTS5 Configuration object */
230634 int flags, /* FTS5_TOKENIZE_* flags */
230635 const char *pText, int nText, /* Text to tokenize */
230636 void *pCtx, /* Context passed to xToken() */
230637 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
230638){
230639 if( pText==0 ) return SQLITE_OK;
230640 return pConfig->pTokApi->xTokenize(
230641 pConfig->pTok, pCtx, flags, pText, nText, xToken
230642 );
230643}
230644
230645/*
230646** Argument pIn points to the first character in what is expected to be
230647** a comma-separated list of SQL literals followed by a ')' character.
230648** If it actually is this, return a pointer to the ')'. Otherwise, return
230649** NULL to indicate a parse error.
230650*/
230651static const char *fts5ConfigSkipArgs(const char *pIn){
230652 const char *p = pIn;
230653
230654 while( 1 ){
230655 p = fts5ConfigSkipWhitespace(pIn: p);
230656 p = fts5ConfigSkipLiteral(pIn: p);
230657 p = fts5ConfigSkipWhitespace(pIn: p);
230658 if( p==0 || *p==')' ) break;
230659 if( *p!=',' ){
230660 p = 0;
230661 break;
230662 }
230663 p++;
230664 }
230665
230666 return p;
230667}
230668
230669/*
230670** Parameter zIn contains a rank() function specification. The format of
230671** this is:
230672**
230673** + Bareword (function name)
230674** + Open parenthesis - "("
230675** + Zero or more SQL literals in a comma separated list
230676** + Close parenthesis - ")"
230677*/
230678static int sqlite3Fts5ConfigParseRank(
230679 const char *zIn, /* Input string */
230680 char **pzRank, /* OUT: Rank function name */
230681 char **pzRankArgs /* OUT: Rank function arguments */
230682){
230683 const char *p = zIn;
230684 const char *pRank;
230685 char *zRank = 0;
230686 char *zRankArgs = 0;
230687 int rc = SQLITE_OK;
230688
230689 *pzRank = 0;
230690 *pzRankArgs = 0;
230691
230692 if( p==0 ){
230693 rc = SQLITE_ERROR;
230694 }else{
230695 p = fts5ConfigSkipWhitespace(pIn: p);
230696 pRank = p;
230697 p = fts5ConfigSkipBareword(pIn: p);
230698
230699 if( p ){
230700 zRank = sqlite3Fts5MallocZero(pRc: &rc, nByte: 1 + p - pRank);
230701 if( zRank ) memcpy(dest: zRank, src: pRank, n: p-pRank);
230702 }else{
230703 rc = SQLITE_ERROR;
230704 }
230705
230706 if( rc==SQLITE_OK ){
230707 p = fts5ConfigSkipWhitespace(pIn: p);
230708 if( *p!='(' ) rc = SQLITE_ERROR;
230709 p++;
230710 }
230711 if( rc==SQLITE_OK ){
230712 const char *pArgs;
230713 p = fts5ConfigSkipWhitespace(pIn: p);
230714 pArgs = p;
230715 if( *p!=')' ){
230716 p = fts5ConfigSkipArgs(pIn: p);
230717 if( p==0 ){
230718 rc = SQLITE_ERROR;
230719 }else{
230720 zRankArgs = sqlite3Fts5MallocZero(pRc: &rc, nByte: 1 + p - pArgs);
230721 if( zRankArgs ) memcpy(dest: zRankArgs, src: pArgs, n: p-pArgs);
230722 }
230723 }
230724 }
230725 }
230726
230727 if( rc!=SQLITE_OK ){
230728 sqlite3_free(p: zRank);
230729 assert( zRankArgs==0 );
230730 }else{
230731 *pzRank = zRank;
230732 *pzRankArgs = zRankArgs;
230733 }
230734 return rc;
230735}
230736
230737static int sqlite3Fts5ConfigSetValue(
230738 Fts5Config *pConfig,
230739 const char *zKey,
230740 sqlite3_value *pVal,
230741 int *pbBadkey
230742){
230743 int rc = SQLITE_OK;
230744
230745 if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "pgsz") ){
230746 int pgsz = 0;
230747 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230748 pgsz = sqlite3_value_int(pVal);
230749 }
230750 if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){
230751 *pbBadkey = 1;
230752 }else{
230753 pConfig->pgsz = pgsz;
230754 }
230755 }
230756
230757 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "hashsize") ){
230758 int nHashSize = -1;
230759 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230760 nHashSize = sqlite3_value_int(pVal);
230761 }
230762 if( nHashSize<=0 ){
230763 *pbBadkey = 1;
230764 }else{
230765 pConfig->nHashSize = nHashSize;
230766 }
230767 }
230768
230769 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "automerge") ){
230770 int nAutomerge = -1;
230771 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230772 nAutomerge = sqlite3_value_int(pVal);
230773 }
230774 if( nAutomerge<0 || nAutomerge>64 ){
230775 *pbBadkey = 1;
230776 }else{
230777 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
230778 pConfig->nAutomerge = nAutomerge;
230779 }
230780 }
230781
230782 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "usermerge") ){
230783 int nUsermerge = -1;
230784 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230785 nUsermerge = sqlite3_value_int(pVal);
230786 }
230787 if( nUsermerge<2 || nUsermerge>16 ){
230788 *pbBadkey = 1;
230789 }else{
230790 pConfig->nUsermerge = nUsermerge;
230791 }
230792 }
230793
230794 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "crisismerge") ){
230795 int nCrisisMerge = -1;
230796 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230797 nCrisisMerge = sqlite3_value_int(pVal);
230798 }
230799 if( nCrisisMerge<0 ){
230800 *pbBadkey = 1;
230801 }else{
230802 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
230803 if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1;
230804 pConfig->nCrisisMerge = nCrisisMerge;
230805 }
230806 }
230807
230808 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "deletemerge") ){
230809 int nVal = -1;
230810 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230811 nVal = sqlite3_value_int(pVal);
230812 }else{
230813 *pbBadkey = 1;
230814 }
230815 if( nVal<0 ) nVal = FTS5_DEFAULT_DELETE_AUTOMERGE;
230816 if( nVal>100 ) nVal = 0;
230817 pConfig->nDeleteMerge = nVal;
230818 }
230819
230820 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "rank") ){
230821 const char *zIn = (const char*)sqlite3_value_text(pVal);
230822 char *zRank;
230823 char *zRankArgs;
230824 rc = sqlite3Fts5ConfigParseRank(zIn, pzRank: &zRank, pzRankArgs: &zRankArgs);
230825 if( rc==SQLITE_OK ){
230826 sqlite3_free(p: pConfig->zRank);
230827 sqlite3_free(p: pConfig->zRankArgs);
230828 pConfig->zRank = zRank;
230829 pConfig->zRankArgs = zRankArgs;
230830 }else if( rc==SQLITE_ERROR ){
230831 rc = SQLITE_OK;
230832 *pbBadkey = 1;
230833 }
230834 }
230835
230836 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "secure-delete") ){
230837 int bVal = -1;
230838 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
230839 bVal = sqlite3_value_int(pVal);
230840 }
230841 if( bVal<0 ){
230842 *pbBadkey = 1;
230843 }else{
230844 pConfig->bSecureDelete = (bVal ? 1 : 0);
230845 }
230846 }else{
230847 *pbBadkey = 1;
230848 }
230849 return rc;
230850}
230851
230852/*
230853** Load the contents of the %_config table into memory.
230854*/
230855static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
230856 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
230857 char *zSql;
230858 sqlite3_stmt *p = 0;
230859 int rc = SQLITE_OK;
230860 int iVersion = 0;
230861
230862 /* Set default values */
230863 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
230864 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
230865 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
230866 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
230867 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
230868 pConfig->nDeleteMerge = FTS5_DEFAULT_DELETE_AUTOMERGE;
230869
230870 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: zSelect, pConfig->zDb, pConfig->zName);
230871 if( zSql ){
230872 rc = sqlite3_prepare_v2(db: pConfig->db, zSql, nBytes: -1, ppStmt: &p, pzTail: 0);
230873 sqlite3_free(p: zSql);
230874 }
230875
230876 assert( rc==SQLITE_OK || p==0 );
230877 if( rc==SQLITE_OK ){
230878 while( SQLITE_ROW==sqlite3_step(pStmt: p) ){
230879 const char *zK = (const char*)sqlite3_column_text(pStmt: p, i: 0);
230880 sqlite3_value *pVal = sqlite3_column_value(pStmt: p, i: 1);
230881 if( 0==sqlite3_stricmp(zLeft: zK, zRight: "version") ){
230882 iVersion = sqlite3_value_int(pVal);
230883 }else{
230884 int bDummy = 0;
230885 sqlite3Fts5ConfigSetValue(pConfig, zKey: zK, pVal, pbBadkey: &bDummy);
230886 }
230887 }
230888 rc = sqlite3_finalize(pStmt: p);
230889 }
230890
230891 if( rc==SQLITE_OK
230892 && iVersion!=FTS5_CURRENT_VERSION
230893 && iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE
230894 ){
230895 rc = SQLITE_ERROR;
230896 if( pConfig->pzErrmsg ){
230897 assert( 0==*pConfig->pzErrmsg );
230898 *pConfig->pzErrmsg = sqlite3_mprintf(zFormat: "invalid fts5 file format "
230899 "(found %d, expected %d or %d) - run 'rebuild'",
230900 iVersion, FTS5_CURRENT_VERSION, FTS5_CURRENT_VERSION_SECUREDELETE
230901 );
230902 }
230903 }else{
230904 pConfig->iVersion = iVersion;
230905 }
230906
230907 if( rc==SQLITE_OK ){
230908 pConfig->iCookie = iCookie;
230909 }
230910 return rc;
230911}
230912
230913/*
230914** 2014 May 31
230915**
230916** The author disclaims copyright to this source code. In place of
230917** a legal notice, here is a blessing:
230918**
230919** May you do good and not evil.
230920** May you find forgiveness for yourself and forgive others.
230921** May you share freely, never taking more than you give.
230922**
230923******************************************************************************
230924**
230925*/
230926
230927
230928
230929/* #include "fts5Int.h" */
230930/* #include "fts5parse.h" */
230931
230932#ifndef SQLITE_FTS5_MAX_EXPR_DEPTH
230933# define SQLITE_FTS5_MAX_EXPR_DEPTH 256
230934#endif
230935
230936/*
230937** All token types in the generated fts5parse.h file are greater than 0.
230938*/
230939#define FTS5_EOF 0
230940
230941#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
230942
230943typedef struct Fts5ExprTerm Fts5ExprTerm;
230944
230945/*
230946** Functions generated by lemon from fts5parse.y.
230947*/
230948static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
230949static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
230950static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
230951#ifndef NDEBUG
230952/* #include <stdio.h> */
230953static void sqlite3Fts5ParserTrace(FILE*, char*);
230954#endif
230955static int sqlite3Fts5ParserFallback(int);
230956
230957
230958struct Fts5Expr {
230959 Fts5Index *pIndex;
230960 Fts5Config *pConfig;
230961 Fts5ExprNode *pRoot;
230962 int bDesc; /* Iterate in descending rowid order */
230963 int nPhrase; /* Number of phrases in expression */
230964 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
230965};
230966
230967/*
230968** eType:
230969** Expression node type. Always one of:
230970**
230971** FTS5_AND (nChild, apChild valid)
230972** FTS5_OR (nChild, apChild valid)
230973** FTS5_NOT (nChild, apChild valid)
230974** FTS5_STRING (pNear valid)
230975** FTS5_TERM (pNear valid)
230976**
230977** iHeight:
230978** Distance from this node to furthest leaf. This is always 0 for nodes
230979** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one
230980** greater than the largest child value.
230981*/
230982struct Fts5ExprNode {
230983 int eType; /* Node type */
230984 int bEof; /* True at EOF */
230985 int bNomatch; /* True if entry is not a match */
230986 int iHeight; /* Distance to tree leaf nodes */
230987
230988 /* Next method for this node. */
230989 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
230990
230991 i64 iRowid; /* Current rowid */
230992 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
230993
230994 /* Child nodes. For a NOT node, this array always contains 2 entries. For
230995 ** AND or OR nodes, it contains 2 or more entries. */
230996 int nChild; /* Number of child nodes */
230997 Fts5ExprNode *apChild[1]; /* Array of child nodes */
230998};
230999
231000#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
231001
231002/*
231003** Invoke the xNext method of an Fts5ExprNode object. This macro should be
231004** used as if it has the same signature as the xNext() methods themselves.
231005*/
231006#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
231007
231008/*
231009** An instance of the following structure represents a single search term
231010** or term prefix.
231011*/
231012struct Fts5ExprTerm {
231013 u8 bPrefix; /* True for a prefix term */
231014 u8 bFirst; /* True if token must be first in column */
231015 char *zTerm; /* nul-terminated term */
231016 Fts5IndexIter *pIter; /* Iterator for this term */
231017 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
231018};
231019
231020/*
231021** A phrase. One or more terms that must appear in a contiguous sequence
231022** within a document for it to match.
231023*/
231024struct Fts5ExprPhrase {
231025 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
231026 Fts5Buffer poslist; /* Current position list */
231027 int nTerm; /* Number of entries in aTerm[] */
231028 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
231029};
231030
231031/*
231032** One or more phrases that must appear within a certain token distance of
231033** each other within each matching document.
231034*/
231035struct Fts5ExprNearset {
231036 int nNear; /* NEAR parameter */
231037 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
231038 int nPhrase; /* Number of entries in aPhrase[] array */
231039 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
231040};
231041
231042
231043/*
231044** Parse context.
231045*/
231046struct Fts5Parse {
231047 Fts5Config *pConfig;
231048 char *zErr;
231049 int rc;
231050 int nPhrase; /* Size of apPhrase array */
231051 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
231052 Fts5ExprNode *pExpr; /* Result of a successful parse */
231053 int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
231054};
231055
231056/*
231057** Check that the Fts5ExprNode.iHeight variables are set correctly in
231058** the expression tree passed as the only argument.
231059*/
231060#ifndef NDEBUG
231061static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){
231062 if( rc==SQLITE_OK ){
231063 if( p->eType==FTS5_TERM || p->eType==FTS5_STRING || p->eType==0 ){
231064 assert( p->iHeight==0 );
231065 }else{
231066 int ii;
231067 int iMaxChild = 0;
231068 for(ii=0; ii<p->nChild; ii++){
231069 Fts5ExprNode *pChild = p->apChild[ii];
231070 iMaxChild = MAX(iMaxChild, pChild->iHeight);
231071 assert_expr_depth_ok(SQLITE_OK, pChild);
231072 }
231073 assert( p->iHeight==iMaxChild+1 );
231074 }
231075 }
231076}
231077#else
231078# define assert_expr_depth_ok(rc, p)
231079#endif
231080
231081static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
231082 va_list ap;
231083 va_start(ap, zFmt);
231084 if( pParse->rc==SQLITE_OK ){
231085 assert( pParse->zErr==0 );
231086 pParse->zErr = sqlite3_vmprintf(zFormat: zFmt, ap);
231087 pParse->rc = SQLITE_ERROR;
231088 }
231089 va_end(ap);
231090}
231091
231092static int fts5ExprIsspace(char t){
231093 return t==' ' || t=='\t' || t=='\n' || t=='\r';
231094}
231095
231096/*
231097** Read the first token from the nul-terminated string at *pz.
231098*/
231099static int fts5ExprGetToken(
231100 Fts5Parse *pParse,
231101 const char **pz, /* IN/OUT: Pointer into buffer */
231102 Fts5Token *pToken
231103){
231104 const char *z = *pz;
231105 int tok;
231106
231107 /* Skip past any whitespace */
231108 while( fts5ExprIsspace(t: *z) ) z++;
231109
231110 pToken->p = z;
231111 pToken->n = 1;
231112 switch( *z ){
231113 case '(': tok = FTS5_LP; break;
231114 case ')': tok = FTS5_RP; break;
231115 case '{': tok = FTS5_LCP; break;
231116 case '}': tok = FTS5_RCP; break;
231117 case ':': tok = FTS5_COLON; break;
231118 case ',': tok = FTS5_COMMA; break;
231119 case '+': tok = FTS5_PLUS; break;
231120 case '*': tok = FTS5_STAR; break;
231121 case '-': tok = FTS5_MINUS; break;
231122 case '^': tok = FTS5_CARET; break;
231123 case '\0': tok = FTS5_EOF; break;
231124
231125 case '"': {
231126 const char *z2;
231127 tok = FTS5_STRING;
231128
231129 for(z2=&z[1]; 1; z2++){
231130 if( z2[0]=='"' ){
231131 z2++;
231132 if( z2[0]!='"' ) break;
231133 }
231134 if( z2[0]=='\0' ){
231135 sqlite3Fts5ParseError(pParse, zFmt: "unterminated string");
231136 return FTS5_EOF;
231137 }
231138 }
231139 pToken->n = (z2 - z);
231140 break;
231141 }
231142
231143 default: {
231144 const char *z2;
231145 if( sqlite3Fts5IsBareword(t: z[0])==0 ){
231146 sqlite3Fts5ParseError(pParse, zFmt: "fts5: syntax error near \"%.1s\"", z);
231147 return FTS5_EOF;
231148 }
231149 tok = FTS5_STRING;
231150 for(z2=&z[1]; sqlite3Fts5IsBareword(t: *z2); z2++);
231151 pToken->n = (z2 - z);
231152 if( pToken->n==2 && memcmp(s1: pToken->p, s2: "OR", n: 2)==0 ) tok = FTS5_OR;
231153 if( pToken->n==3 && memcmp(s1: pToken->p, s2: "NOT", n: 3)==0 ) tok = FTS5_NOT;
231154 if( pToken->n==3 && memcmp(s1: pToken->p, s2: "AND", n: 3)==0 ) tok = FTS5_AND;
231155 break;
231156 }
231157 }
231158
231159 *pz = &pToken->p[pToken->n];
231160 return tok;
231161}
231162
231163static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64(n: (sqlite3_int64)t);}
231164static void fts5ParseFree(void *p){ sqlite3_free(p); }
231165
231166static int sqlite3Fts5ExprNew(
231167 Fts5Config *pConfig, /* FTS5 Configuration */
231168 int bPhraseToAnd,
231169 int iCol,
231170 const char *zExpr, /* Expression text */
231171 Fts5Expr **ppNew,
231172 char **pzErr
231173){
231174 Fts5Parse sParse;
231175 Fts5Token token;
231176 const char *z = zExpr;
231177 int t; /* Next token type */
231178 void *pEngine;
231179 Fts5Expr *pNew;
231180
231181 *ppNew = 0;
231182 *pzErr = 0;
231183 memset(s: &sParse, c: 0, n: sizeof(sParse));
231184 sParse.bPhraseToAnd = bPhraseToAnd;
231185 pEngine = sqlite3Fts5ParserAlloc(mallocProc: fts5ParseAlloc);
231186 if( pEngine==0 ){ return SQLITE_NOMEM; }
231187 sParse.pConfig = pConfig;
231188
231189 do {
231190 t = fts5ExprGetToken(pParse: &sParse, pz: &z, pToken: &token);
231191 sqlite3Fts5Parser(pEngine, t, token, &sParse);
231192 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
231193 sqlite3Fts5ParserFree(pEngine, freeProc: fts5ParseFree);
231194
231195 assert_expr_depth_ok(sParse.rc, sParse.pExpr);
231196
231197 /* If the LHS of the MATCH expression was a user column, apply the
231198 ** implicit column-filter. */
231199 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
231200 int n = sizeof(Fts5Colset);
231201 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(pRc: &sParse.rc, nByte: n);
231202 if( pColset ){
231203 pColset->nCol = 1;
231204 pColset->aiCol[0] = iCol;
231205 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
231206 }
231207 }
231208
231209 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
231210 if( sParse.rc==SQLITE_OK ){
231211 *ppNew = pNew = sqlite3_malloc(n: sizeof(Fts5Expr));
231212 if( pNew==0 ){
231213 sParse.rc = SQLITE_NOMEM;
231214 sqlite3Fts5ParseNodeFree(sParse.pExpr);
231215 }else{
231216 if( !sParse.pExpr ){
231217 const int nByte = sizeof(Fts5ExprNode);
231218 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &sParse.rc, nByte);
231219 if( pNew->pRoot ){
231220 pNew->pRoot->bEof = 1;
231221 }
231222 }else{
231223 pNew->pRoot = sParse.pExpr;
231224 }
231225 pNew->pIndex = 0;
231226 pNew->pConfig = pConfig;
231227 pNew->apExprPhrase = sParse.apPhrase;
231228 pNew->nPhrase = sParse.nPhrase;
231229 pNew->bDesc = 0;
231230 sParse.apPhrase = 0;
231231 }
231232 }else{
231233 sqlite3Fts5ParseNodeFree(sParse.pExpr);
231234 }
231235
231236 sqlite3_free(p: sParse.apPhrase);
231237 *pzErr = sParse.zErr;
231238 return sParse.rc;
231239}
231240
231241/*
231242** Assuming that buffer z is at least nByte bytes in size and contains a
231243** valid utf-8 string, return the number of characters in the string.
231244*/
231245static int fts5ExprCountChar(const char *z, int nByte){
231246 int nRet = 0;
231247 int ii;
231248 for(ii=0; ii<nByte; ii++){
231249 if( (z[ii] & 0xC0)!=0x80 ) nRet++;
231250 }
231251 return nRet;
231252}
231253
231254/*
231255** This function is only called when using the special 'trigram' tokenizer.
231256** Argument zText contains the text of a LIKE or GLOB pattern matched
231257** against column iCol. This function creates and compiles an FTS5 MATCH
231258** expression that will match a superset of the rows matched by the LIKE or
231259** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
231260** code.
231261*/
231262static int sqlite3Fts5ExprPattern(
231263 Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
231264){
231265 i64 nText = strlen(s: zText);
231266 char *zExpr = (char*)sqlite3_malloc64(n: nText*4 + 1);
231267 int rc = SQLITE_OK;
231268
231269 if( zExpr==0 ){
231270 rc = SQLITE_NOMEM;
231271 }else{
231272 char aSpec[3];
231273 int iOut = 0;
231274 int i = 0;
231275 int iFirst = 0;
231276
231277 if( bGlob==0 ){
231278 aSpec[0] = '_';
231279 aSpec[1] = '%';
231280 aSpec[2] = 0;
231281 }else{
231282 aSpec[0] = '*';
231283 aSpec[1] = '?';
231284 aSpec[2] = '[';
231285 }
231286
231287 while( i<=nText ){
231288 if( i==nText
231289 || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
231290 ){
231291
231292 if( fts5ExprCountChar(z: &zText[iFirst], nByte: i-iFirst)>=3 ){
231293 int jj;
231294 zExpr[iOut++] = '"';
231295 for(jj=iFirst; jj<i; jj++){
231296 zExpr[iOut++] = zText[jj];
231297 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
231298 }
231299 zExpr[iOut++] = '"';
231300 zExpr[iOut++] = ' ';
231301 }
231302 if( zText[i]==aSpec[2] ){
231303 i += 2;
231304 if( zText[i-1]=='^' ) i++;
231305 while( i<nText && zText[i]!=']' ) i++;
231306 }
231307 iFirst = i+1;
231308 }
231309 i++;
231310 }
231311 if( iOut>0 ){
231312 int bAnd = 0;
231313 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
231314 bAnd = 1;
231315 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
231316 iCol = pConfig->nCol;
231317 }
231318 }
231319 zExpr[iOut] = '\0';
231320 rc = sqlite3Fts5ExprNew(pConfig, bPhraseToAnd: bAnd, iCol, zExpr, ppNew: pp,pzErr: pConfig->pzErrmsg);
231321 }else{
231322 *pp = 0;
231323 }
231324 sqlite3_free(p: zExpr);
231325 }
231326
231327 return rc;
231328}
231329
231330/*
231331** Free the expression node object passed as the only argument.
231332*/
231333static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
231334 if( p ){
231335 int i;
231336 for(i=0; i<p->nChild; i++){
231337 sqlite3Fts5ParseNodeFree(p: p->apChild[i]);
231338 }
231339 sqlite3Fts5ParseNearsetFree(p->pNear);
231340 sqlite3_free(p);
231341 }
231342}
231343
231344/*
231345** Free the expression object passed as the only argument.
231346*/
231347static void sqlite3Fts5ExprFree(Fts5Expr *p){
231348 if( p ){
231349 sqlite3Fts5ParseNodeFree(p: p->pRoot);
231350 sqlite3_free(p: p->apExprPhrase);
231351 sqlite3_free(p);
231352 }
231353}
231354
231355static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
231356 Fts5Parse sParse;
231357 memset(s: &sParse, c: 0, n: sizeof(sParse));
231358
231359 if( *pp1 && p2 ){
231360 Fts5Expr *p1 = *pp1;
231361 int nPhrase = p1->nPhrase + p2->nPhrase;
231362
231363 p1->pRoot = sqlite3Fts5ParseNode(pParse: &sParse, FTS5_AND, pLeft: p1->pRoot, pRight: p2->pRoot,pNear: 0);
231364 p2->pRoot = 0;
231365
231366 if( sParse.rc==SQLITE_OK ){
231367 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
231368 pOld: p1->apExprPhrase, n: nPhrase * sizeof(Fts5ExprPhrase*)
231369 );
231370 if( ap==0 ){
231371 sParse.rc = SQLITE_NOMEM;
231372 }else{
231373 int i;
231374 memmove(dest: &ap[p2->nPhrase], src: ap, n: p1->nPhrase*sizeof(Fts5ExprPhrase*));
231375 for(i=0; i<p2->nPhrase; i++){
231376 ap[i] = p2->apExprPhrase[i];
231377 }
231378 p1->nPhrase = nPhrase;
231379 p1->apExprPhrase = ap;
231380 }
231381 }
231382 sqlite3_free(p: p2->apExprPhrase);
231383 sqlite3_free(p: p2);
231384 }else if( p2 ){
231385 *pp1 = p2;
231386 }
231387
231388 return sParse.rc;
231389}
231390
231391/*
231392** Argument pTerm must be a synonym iterator. Return the current rowid
231393** that it points to.
231394*/
231395static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
231396 i64 iRet = 0;
231397 int bRetValid = 0;
231398 Fts5ExprTerm *p;
231399
231400 assert( pTerm );
231401 assert( pTerm->pSynonym );
231402 assert( bDesc==0 || bDesc==1 );
231403 for(p=pTerm; p; p=p->pSynonym){
231404 if( 0==sqlite3Fts5IterEof(p->pIter) ){
231405 i64 iRowid = p->pIter->iRowid;
231406 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
231407 iRet = iRowid;
231408 bRetValid = 1;
231409 }
231410 }
231411 }
231412
231413 if( pbEof && bRetValid==0 ) *pbEof = 1;
231414 return iRet;
231415}
231416
231417/*
231418** Argument pTerm must be a synonym iterator.
231419*/
231420static int fts5ExprSynonymList(
231421 Fts5ExprTerm *pTerm,
231422 i64 iRowid,
231423 Fts5Buffer *pBuf, /* Use this buffer for space if required */
231424 u8 **pa, int *pn
231425){
231426 Fts5PoslistReader aStatic[4];
231427 Fts5PoslistReader *aIter = aStatic;
231428 int nIter = 0;
231429 int nAlloc = 4;
231430 int rc = SQLITE_OK;
231431 Fts5ExprTerm *p;
231432
231433 assert( pTerm->pSynonym );
231434 for(p=pTerm; p; p=p->pSynonym){
231435 Fts5IndexIter *pIter = p->pIter;
231436 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
231437 if( pIter->nData==0 ) continue;
231438 if( nIter==nAlloc ){
231439 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
231440 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(n: nByte);
231441 if( aNew==0 ){
231442 rc = SQLITE_NOMEM;
231443 goto synonym_poslist_out;
231444 }
231445 memcpy(dest: aNew, src: aIter, n: sizeof(Fts5PoslistReader) * nIter);
231446 nAlloc = nAlloc*2;
231447 if( aIter!=aStatic ) sqlite3_free(p: aIter);
231448 aIter = aNew;
231449 }
231450 sqlite3Fts5PoslistReaderInit(a: pIter->pData, n: pIter->nData, pIter: &aIter[nIter]);
231451 assert( aIter[nIter].bEof==0 );
231452 nIter++;
231453 }
231454 }
231455
231456 if( nIter==1 ){
231457 *pa = (u8*)aIter[0].a;
231458 *pn = aIter[0].n;
231459 }else{
231460 Fts5PoslistWriter writer = {0};
231461 i64 iPrev = -1;
231462 fts5BufferZero(pBuf);
231463 while( 1 ){
231464 int i;
231465 i64 iMin = FTS5_LARGEST_INT64;
231466 for(i=0; i<nIter; i++){
231467 if( aIter[i].bEof==0 ){
231468 if( aIter[i].iPos==iPrev ){
231469 if( sqlite3Fts5PoslistReaderNext(pIter: &aIter[i]) ) continue;
231470 }
231471 if( aIter[i].iPos<iMin ){
231472 iMin = aIter[i].iPos;
231473 }
231474 }
231475 }
231476 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
231477 rc = sqlite3Fts5PoslistWriterAppend(pBuf, pWriter: &writer, iPos: iMin);
231478 iPrev = iMin;
231479 }
231480 if( rc==SQLITE_OK ){
231481 *pa = pBuf->p;
231482 *pn = pBuf->n;
231483 }
231484 }
231485
231486 synonym_poslist_out:
231487 if( aIter!=aStatic ) sqlite3_free(p: aIter);
231488 return rc;
231489}
231490
231491
231492/*
231493** All individual term iterators in pPhrase are guaranteed to be valid and
231494** pointing to the same rowid when this function is called. This function
231495** checks if the current rowid really is a match, and if so populates
231496** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
231497** is set to true if this is really a match, or false otherwise.
231498**
231499** SQLITE_OK is returned if an error occurs, or an SQLite error code
231500** otherwise. It is not considered an error code if the current rowid is
231501** not a match.
231502*/
231503static int fts5ExprPhraseIsMatch(
231504 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
231505 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
231506 int *pbMatch /* OUT: Set to true if really a match */
231507){
231508 Fts5PoslistWriter writer = {0};
231509 Fts5PoslistReader aStatic[4];
231510 Fts5PoslistReader *aIter = aStatic;
231511 int i;
231512 int rc = SQLITE_OK;
231513 int bFirst = pPhrase->aTerm[0].bFirst;
231514
231515 fts5BufferZero(&pPhrase->poslist);
231516
231517 /* If the aStatic[] array is not large enough, allocate a large array
231518 ** using sqlite3_malloc(). This approach could be improved upon. */
231519 if( pPhrase->nTerm>ArraySize(aStatic) ){
231520 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
231521 aIter = (Fts5PoslistReader*)sqlite3_malloc64(n: nByte);
231522 if( !aIter ) return SQLITE_NOMEM;
231523 }
231524 memset(s: aIter, c: 0, n: sizeof(Fts5PoslistReader) * pPhrase->nTerm);
231525
231526 /* Initialize a term iterator for each term in the phrase */
231527 for(i=0; i<pPhrase->nTerm; i++){
231528 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
231529 int n = 0;
231530 int bFlag = 0;
231531 u8 *a = 0;
231532 if( pTerm->pSynonym ){
231533 Fts5Buffer buf = {0, 0, 0};
231534 rc = fts5ExprSynonymList(pTerm, iRowid: pNode->iRowid, pBuf: &buf, pa: &a, pn: &n);
231535 if( rc ){
231536 sqlite3_free(p: a);
231537 goto ismatch_out;
231538 }
231539 if( a==buf.p ) bFlag = 1;
231540 }else{
231541 a = (u8*)pTerm->pIter->pData;
231542 n = pTerm->pIter->nData;
231543 }
231544 sqlite3Fts5PoslistReaderInit(a, n, pIter: &aIter[i]);
231545 aIter[i].bFlag = (u8)bFlag;
231546 if( aIter[i].bEof ) goto ismatch_out;
231547 }
231548
231549 while( 1 ){
231550 int bMatch;
231551 i64 iPos = aIter[0].iPos;
231552 do {
231553 bMatch = 1;
231554 for(i=0; i<pPhrase->nTerm; i++){
231555 Fts5PoslistReader *pPos = &aIter[i];
231556 i64 iAdj = iPos + i;
231557 if( pPos->iPos!=iAdj ){
231558 bMatch = 0;
231559 while( pPos->iPos<iAdj ){
231560 if( sqlite3Fts5PoslistReaderNext(pIter: pPos) ) goto ismatch_out;
231561 }
231562 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
231563 }
231564 }
231565 }while( bMatch==0 );
231566
231567 /* Append position iPos to the output */
231568 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
231569 rc = sqlite3Fts5PoslistWriterAppend(pBuf: &pPhrase->poslist, pWriter: &writer, iPos);
231570 if( rc!=SQLITE_OK ) goto ismatch_out;
231571 }
231572
231573 for(i=0; i<pPhrase->nTerm; i++){
231574 if( sqlite3Fts5PoslistReaderNext(pIter: &aIter[i]) ) goto ismatch_out;
231575 }
231576 }
231577
231578 ismatch_out:
231579 *pbMatch = (pPhrase->poslist.n>0);
231580 for(i=0; i<pPhrase->nTerm; i++){
231581 if( aIter[i].bFlag ) sqlite3_free(p: (u8*)aIter[i].a);
231582 }
231583 if( aIter!=aStatic ) sqlite3_free(p: aIter);
231584 return rc;
231585}
231586
231587typedef struct Fts5LookaheadReader Fts5LookaheadReader;
231588struct Fts5LookaheadReader {
231589 const u8 *a; /* Buffer containing position list */
231590 int n; /* Size of buffer a[] in bytes */
231591 int i; /* Current offset in position list */
231592 i64 iPos; /* Current position */
231593 i64 iLookahead; /* Next position */
231594};
231595
231596#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
231597
231598static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
231599 p->iPos = p->iLookahead;
231600 if( sqlite3Fts5PoslistNext64(a: p->a, n: p->n, pi: &p->i, piOff: &p->iLookahead) ){
231601 p->iLookahead = FTS5_LOOKAHEAD_EOF;
231602 }
231603 return (p->iPos==FTS5_LOOKAHEAD_EOF);
231604}
231605
231606static int fts5LookaheadReaderInit(
231607 const u8 *a, int n, /* Buffer to read position list from */
231608 Fts5LookaheadReader *p /* Iterator object to initialize */
231609){
231610 memset(s: p, c: 0, n: sizeof(Fts5LookaheadReader));
231611 p->a = a;
231612 p->n = n;
231613 fts5LookaheadReaderNext(p);
231614 return fts5LookaheadReaderNext(p);
231615}
231616
231617typedef struct Fts5NearTrimmer Fts5NearTrimmer;
231618struct Fts5NearTrimmer {
231619 Fts5LookaheadReader reader; /* Input iterator */
231620 Fts5PoslistWriter writer; /* Writer context */
231621 Fts5Buffer *pOut; /* Output poslist */
231622};
231623
231624/*
231625** The near-set object passed as the first argument contains more than
231626** one phrase. All phrases currently point to the same row. The
231627** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
231628** tests if the current row contains instances of each phrase sufficiently
231629** close together to meet the NEAR constraint. Non-zero is returned if it
231630** does, or zero otherwise.
231631**
231632** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
231633** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
231634** occurs within this function (*pRc) is set accordingly before returning.
231635** The return value is undefined in both these cases.
231636**
231637** If no error occurs and non-zero (a match) is returned, the position-list
231638** of each phrase object is edited to contain only those entries that
231639** meet the constraint before returning.
231640*/
231641static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
231642 Fts5NearTrimmer aStatic[4];
231643 Fts5NearTrimmer *a = aStatic;
231644 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
231645
231646 int i;
231647 int rc = *pRc;
231648 int bMatch;
231649
231650 assert( pNear->nPhrase>1 );
231651
231652 /* If the aStatic[] array is not large enough, allocate a large array
231653 ** using sqlite3_malloc(). This approach could be improved upon. */
231654 if( pNear->nPhrase>ArraySize(aStatic) ){
231655 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
231656 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
231657 }else{
231658 memset(s: aStatic, c: 0, n: sizeof(aStatic));
231659 }
231660 if( rc!=SQLITE_OK ){
231661 *pRc = rc;
231662 return 0;
231663 }
231664
231665 /* Initialize a lookahead iterator for each phrase. After passing the
231666 ** buffer and buffer size to the lookaside-reader init function, zero
231667 ** the phrase poslist buffer. The new poslist for the phrase (containing
231668 ** the same entries as the original with some entries removed on account
231669 ** of the NEAR constraint) is written over the original even as it is
231670 ** being read. This is safe as the entries for the new poslist are a
231671 ** subset of the old, so it is not possible for data yet to be read to
231672 ** be overwritten. */
231673 for(i=0; i<pNear->nPhrase; i++){
231674 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
231675 fts5LookaheadReaderInit(a: pPoslist->p, n: pPoslist->n, p: &a[i].reader);
231676 pPoslist->n = 0;
231677 a[i].pOut = pPoslist;
231678 }
231679
231680 while( 1 ){
231681 int iAdv;
231682 i64 iMin;
231683 i64 iMax;
231684
231685 /* This block advances the phrase iterators until they point to a set of
231686 ** entries that together comprise a match. */
231687 iMax = a[0].reader.iPos;
231688 do {
231689 bMatch = 1;
231690 for(i=0; i<pNear->nPhrase; i++){
231691 Fts5LookaheadReader *pPos = &a[i].reader;
231692 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
231693 if( pPos->iPos<iMin || pPos->iPos>iMax ){
231694 bMatch = 0;
231695 while( pPos->iPos<iMin ){
231696 if( fts5LookaheadReaderNext(p: pPos) ) goto ismatch_out;
231697 }
231698 if( pPos->iPos>iMax ) iMax = pPos->iPos;
231699 }
231700 }
231701 }while( bMatch==0 );
231702
231703 /* Add an entry to each output position list */
231704 for(i=0; i<pNear->nPhrase; i++){
231705 i64 iPos = a[i].reader.iPos;
231706 Fts5PoslistWriter *pWriter = &a[i].writer;
231707 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
231708 sqlite3Fts5PoslistWriterAppend(pBuf: a[i].pOut, pWriter, iPos);
231709 }
231710 }
231711
231712 iAdv = 0;
231713 iMin = a[0].reader.iLookahead;
231714 for(i=0; i<pNear->nPhrase; i++){
231715 if( a[i].reader.iLookahead < iMin ){
231716 iMin = a[i].reader.iLookahead;
231717 iAdv = i;
231718 }
231719 }
231720 if( fts5LookaheadReaderNext(p: &a[iAdv].reader) ) goto ismatch_out;
231721 }
231722
231723 ismatch_out: {
231724 int bRet = a[0].pOut->n>0;
231725 *pRc = rc;
231726 if( a!=aStatic ) sqlite3_free(p: a);
231727 return bRet;
231728 }
231729}
231730
231731/*
231732** Advance iterator pIter until it points to a value equal to or laster
231733** than the initial value of *piLast. If this means the iterator points
231734** to a value laster than *piLast, update *piLast to the new lastest value.
231735**
231736** If the iterator reaches EOF, set *pbEof to true before returning. If
231737** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
231738** are set, return a non-zero value. Otherwise, return zero.
231739*/
231740static int fts5ExprAdvanceto(
231741 Fts5IndexIter *pIter, /* Iterator to advance */
231742 int bDesc, /* True if iterator is "rowid DESC" */
231743 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
231744 int *pRc, /* OUT: Error code */
231745 int *pbEof /* OUT: Set to true if EOF */
231746){
231747 i64 iLast = *piLast;
231748 i64 iRowid;
231749
231750 iRowid = pIter->iRowid;
231751 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
231752 int rc = sqlite3Fts5IterNextFrom(pIter, iMatch: iLast);
231753 if( rc || sqlite3Fts5IterEof(pIter) ){
231754 *pRc = rc;
231755 *pbEof = 1;
231756 return 1;
231757 }
231758 iRowid = pIter->iRowid;
231759 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
231760 }
231761 *piLast = iRowid;
231762
231763 return 0;
231764}
231765
231766static int fts5ExprSynonymAdvanceto(
231767 Fts5ExprTerm *pTerm, /* Term iterator to advance */
231768 int bDesc, /* True if iterator is "rowid DESC" */
231769 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
231770 int *pRc /* OUT: Error code */
231771){
231772 int rc = SQLITE_OK;
231773 i64 iLast = *piLast;
231774 Fts5ExprTerm *p;
231775 int bEof = 0;
231776
231777 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
231778 if( sqlite3Fts5IterEof(p->pIter)==0 ){
231779 i64 iRowid = p->pIter->iRowid;
231780 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
231781 rc = sqlite3Fts5IterNextFrom(p->pIter, iMatch: iLast);
231782 }
231783 }
231784 }
231785
231786 if( rc!=SQLITE_OK ){
231787 *pRc = rc;
231788 bEof = 1;
231789 }else{
231790 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, pbEof: &bEof);
231791 }
231792 return bEof;
231793}
231794
231795
231796static int fts5ExprNearTest(
231797 int *pRc,
231798 Fts5Expr *pExpr, /* Expression that pNear is a part of */
231799 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
231800){
231801 Fts5ExprNearset *pNear = pNode->pNear;
231802 int rc = *pRc;
231803
231804 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
231805 Fts5ExprTerm *pTerm;
231806 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
231807 pPhrase->poslist.n = 0;
231808 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
231809 Fts5IndexIter *pIter = pTerm->pIter;
231810 if( sqlite3Fts5IterEof(pIter)==0 ){
231811 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
231812 pPhrase->poslist.n = 1;
231813 }
231814 }
231815 }
231816 return pPhrase->poslist.n;
231817 }else{
231818 int i;
231819
231820 /* Check that each phrase in the nearset matches the current row.
231821 ** Populate the pPhrase->poslist buffers at the same time. If any
231822 ** phrase is not a match, break out of the loop early. */
231823 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
231824 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
231825 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
231826 || pNear->pColset || pPhrase->aTerm[0].bFirst
231827 ){
231828 int bMatch = 0;
231829 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, pbMatch: &bMatch);
231830 if( bMatch==0 ) break;
231831 }else{
231832 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
231833 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
231834 }
231835 }
231836
231837 *pRc = rc;
231838 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
231839 return 1;
231840 }
231841 return 0;
231842 }
231843}
231844
231845
231846/*
231847** Initialize all term iterators in the pNear object. If any term is found
231848** to match no documents at all, return immediately without initializing any
231849** further iterators.
231850**
231851** If an error occurs, return an SQLite error code. Otherwise, return
231852** SQLITE_OK. It is not considered an error if some term matches zero
231853** documents.
231854*/
231855static int fts5ExprNearInitAll(
231856 Fts5Expr *pExpr,
231857 Fts5ExprNode *pNode
231858){
231859 Fts5ExprNearset *pNear = pNode->pNear;
231860 int i;
231861
231862 assert( pNode->bNomatch==0 );
231863 for(i=0; i<pNear->nPhrase; i++){
231864 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
231865 if( pPhrase->nTerm==0 ){
231866 pNode->bEof = 1;
231867 return SQLITE_OK;
231868 }else{
231869 int j;
231870 for(j=0; j<pPhrase->nTerm; j++){
231871 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
231872 Fts5ExprTerm *p;
231873 int bHit = 0;
231874
231875 for(p=pTerm; p; p=p->pSynonym){
231876 int rc;
231877 if( p->pIter ){
231878 sqlite3Fts5IterClose(p->pIter);
231879 p->pIter = 0;
231880 }
231881 rc = sqlite3Fts5IndexQuery(
231882 p: pExpr->pIndex, pToken: p->zTerm, nToken: (int)strlen(s: p->zTerm),
231883 flags: (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
231884 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
231885 pColset: pNear->pColset,
231886 ppIter: &p->pIter
231887 );
231888 assert( (rc==SQLITE_OK)==(p->pIter!=0) );
231889 if( rc!=SQLITE_OK ) return rc;
231890 if( 0==sqlite3Fts5IterEof(p->pIter) ){
231891 bHit = 1;
231892 }
231893 }
231894
231895 if( bHit==0 ){
231896 pNode->bEof = 1;
231897 return SQLITE_OK;
231898 }
231899 }
231900 }
231901 }
231902
231903 pNode->bEof = 0;
231904 return SQLITE_OK;
231905}
231906
231907/*
231908** If pExpr is an ASC iterator, this function returns a value with the
231909** same sign as:
231910**
231911** (iLhs - iRhs)
231912**
231913** Otherwise, if this is a DESC iterator, the opposite is returned:
231914**
231915** (iRhs - iLhs)
231916*/
231917static int fts5RowidCmp(
231918 Fts5Expr *pExpr,
231919 i64 iLhs,
231920 i64 iRhs
231921){
231922 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
231923 if( pExpr->bDesc==0 ){
231924 if( iLhs<iRhs ) return -1;
231925 return (iLhs > iRhs);
231926 }else{
231927 if( iLhs>iRhs ) return -1;
231928 return (iLhs < iRhs);
231929 }
231930}
231931
231932static void fts5ExprSetEof(Fts5ExprNode *pNode){
231933 int i;
231934 pNode->bEof = 1;
231935 pNode->bNomatch = 0;
231936 for(i=0; i<pNode->nChild; i++){
231937 fts5ExprSetEof(pNode: pNode->apChild[i]);
231938 }
231939}
231940
231941static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
231942 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
231943 Fts5ExprNearset *pNear = pNode->pNear;
231944 int i;
231945 for(i=0; i<pNear->nPhrase; i++){
231946 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
231947 pPhrase->poslist.n = 0;
231948 }
231949 }else{
231950 int i;
231951 for(i=0; i<pNode->nChild; i++){
231952 fts5ExprNodeZeroPoslist(pNode: pNode->apChild[i]);
231953 }
231954 }
231955}
231956
231957
231958
231959/*
231960** Compare the values currently indicated by the two nodes as follows:
231961**
231962** res = (*p1) - (*p2)
231963**
231964** Nodes that point to values that come later in the iteration order are
231965** considered to be larger. Nodes at EOF are the largest of all.
231966**
231967** This means that if the iteration order is ASC, then numerically larger
231968** rowids are considered larger. Or if it is the default DESC, numerically
231969** smaller rowids are larger.
231970*/
231971static int fts5NodeCompare(
231972 Fts5Expr *pExpr,
231973 Fts5ExprNode *p1,
231974 Fts5ExprNode *p2
231975){
231976 if( p2->bEof ) return -1;
231977 if( p1->bEof ) return +1;
231978 return fts5RowidCmp(pExpr, iLhs: p1->iRowid, iRhs: p2->iRowid);
231979}
231980
231981/*
231982** All individual term iterators in pNear are guaranteed to be valid when
231983** this function is called. This function checks if all term iterators
231984** point to the same rowid, and if not, advances them until they do.
231985** If an EOF is reached before this happens, *pbEof is set to true before
231986** returning.
231987**
231988** SQLITE_OK is returned if an error occurs, or an SQLite error code
231989** otherwise. It is not considered an error code if an iterator reaches
231990** EOF.
231991*/
231992static int fts5ExprNodeTest_STRING(
231993 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
231994 Fts5ExprNode *pNode
231995){
231996 Fts5ExprNearset *pNear = pNode->pNear;
231997 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
231998 int rc = SQLITE_OK;
231999 i64 iLast; /* Lastest rowid any iterator points to */
232000 int i, j; /* Phrase and token index, respectively */
232001 int bMatch; /* True if all terms are at the same rowid */
232002 const int bDesc = pExpr->bDesc;
232003
232004 /* Check that this node should not be FTS5_TERM */
232005 assert( pNear->nPhrase>1
232006 || pNear->apPhrase[0]->nTerm>1
232007 || pNear->apPhrase[0]->aTerm[0].pSynonym
232008 || pNear->apPhrase[0]->aTerm[0].bFirst
232009 );
232010
232011 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
232012 ** iterator skips through rowids in the default ascending order, this means
232013 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
232014 ** means the minimum rowid. */
232015 if( pLeft->aTerm[0].pSynonym ){
232016 iLast = fts5ExprSynonymRowid(pTerm: &pLeft->aTerm[0], bDesc, pbEof: 0);
232017 }else{
232018 iLast = pLeft->aTerm[0].pIter->iRowid;
232019 }
232020
232021 do {
232022 bMatch = 1;
232023 for(i=0; i<pNear->nPhrase; i++){
232024 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
232025 for(j=0; j<pPhrase->nTerm; j++){
232026 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
232027 if( pTerm->pSynonym ){
232028 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, pbEof: 0);
232029 if( iRowid==iLast ) continue;
232030 bMatch = 0;
232031 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, piLast: &iLast, pRc: &rc) ){
232032 pNode->bNomatch = 0;
232033 pNode->bEof = 1;
232034 return rc;
232035 }
232036 }else{
232037 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
232038 if( pIter->iRowid==iLast || pIter->bEof ) continue;
232039 bMatch = 0;
232040 if( fts5ExprAdvanceto(pIter, bDesc, piLast: &iLast, pRc: &rc, pbEof: &pNode->bEof) ){
232041 return rc;
232042 }
232043 }
232044 }
232045 }
232046 }while( bMatch==0 );
232047
232048 pNode->iRowid = iLast;
232049 pNode->bNomatch = ((0==fts5ExprNearTest(pRc: &rc, pExpr, pNode)) && rc==SQLITE_OK);
232050 assert( pNode->bEof==0 || pNode->bNomatch==0 );
232051
232052 return rc;
232053}
232054
232055/*
232056** Advance the first term iterator in the first phrase of pNear. Set output
232057** variable *pbEof to true if it reaches EOF or if an error occurs.
232058**
232059** Return SQLITE_OK if successful, or an SQLite error code if an error
232060** occurs.
232061*/
232062static int fts5ExprNodeNext_STRING(
232063 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
232064 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
232065 int bFromValid,
232066 i64 iFrom
232067){
232068 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
232069 int rc = SQLITE_OK;
232070
232071 pNode->bNomatch = 0;
232072 if( pTerm->pSynonym ){
232073 int bEof = 1;
232074 Fts5ExprTerm *p;
232075
232076 /* Find the firstest rowid any synonym points to. */
232077 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc: pExpr->bDesc, pbEof: 0);
232078
232079 /* Advance each iterator that currently points to iRowid. Or, if iFrom
232080 ** is valid - each iterator that points to a rowid before iFrom. */
232081 for(p=pTerm; p; p=p->pSynonym){
232082 if( sqlite3Fts5IterEof(p->pIter)==0 ){
232083 i64 ii = p->pIter->iRowid;
232084 if( ii==iRowid
232085 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
232086 ){
232087 if( bFromValid ){
232088 rc = sqlite3Fts5IterNextFrom(p->pIter, iMatch: iFrom);
232089 }else{
232090 rc = sqlite3Fts5IterNext(p->pIter);
232091 }
232092 if( rc!=SQLITE_OK ) break;
232093 if( sqlite3Fts5IterEof(p->pIter)==0 ){
232094 bEof = 0;
232095 }
232096 }else{
232097 bEof = 0;
232098 }
232099 }
232100 }
232101
232102 /* Set the EOF flag if either all synonym iterators are at EOF or an
232103 ** error has occurred. */
232104 pNode->bEof = (rc || bEof);
232105 }else{
232106 Fts5IndexIter *pIter = pTerm->pIter;
232107
232108 assert( Fts5NodeIsString(pNode) );
232109 if( bFromValid ){
232110 rc = sqlite3Fts5IterNextFrom(pIter, iMatch: iFrom);
232111 }else{
232112 rc = sqlite3Fts5IterNext(pIter);
232113 }
232114
232115 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
232116 }
232117
232118 if( pNode->bEof==0 ){
232119 assert( rc==SQLITE_OK );
232120 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
232121 }
232122
232123 return rc;
232124}
232125
232126
232127static int fts5ExprNodeTest_TERM(
232128 Fts5Expr *pExpr, /* Expression that pNear is a part of */
232129 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
232130){
232131 /* As this "NEAR" object is actually a single phrase that consists
232132 ** of a single term only, grab pointers into the poslist managed by the
232133 ** fts5_index.c iterator object. This is much faster than synthesizing
232134 ** a new poslist the way we have to for more complicated phrase or NEAR
232135 ** expressions. */
232136 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
232137 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
232138
232139 assert( pNode->eType==FTS5_TERM );
232140 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
232141 assert( pPhrase->aTerm[0].pSynonym==0 );
232142
232143 pPhrase->poslist.n = pIter->nData;
232144 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
232145 pPhrase->poslist.p = (u8*)pIter->pData;
232146 }
232147 pNode->iRowid = pIter->iRowid;
232148 pNode->bNomatch = (pPhrase->poslist.n==0);
232149 return SQLITE_OK;
232150}
232151
232152/*
232153** xNext() method for a node of type FTS5_TERM.
232154*/
232155static int fts5ExprNodeNext_TERM(
232156 Fts5Expr *pExpr,
232157 Fts5ExprNode *pNode,
232158 int bFromValid,
232159 i64 iFrom
232160){
232161 int rc;
232162 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
232163
232164 assert( pNode->bEof==0 );
232165 if( bFromValid ){
232166 rc = sqlite3Fts5IterNextFrom(pIter, iMatch: iFrom);
232167 }else{
232168 rc = sqlite3Fts5IterNext(pIter);
232169 }
232170 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
232171 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
232172 }else{
232173 pNode->bEof = 1;
232174 pNode->bNomatch = 0;
232175 }
232176 return rc;
232177}
232178
232179static void fts5ExprNodeTest_OR(
232180 Fts5Expr *pExpr, /* Expression of which pNode is a part */
232181 Fts5ExprNode *pNode /* Expression node to test */
232182){
232183 Fts5ExprNode *pNext = pNode->apChild[0];
232184 int i;
232185
232186 for(i=1; i<pNode->nChild; i++){
232187 Fts5ExprNode *pChild = pNode->apChild[i];
232188 int cmp = fts5NodeCompare(pExpr, p1: pNext, p2: pChild);
232189 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
232190 pNext = pChild;
232191 }
232192 }
232193 pNode->iRowid = pNext->iRowid;
232194 pNode->bEof = pNext->bEof;
232195 pNode->bNomatch = pNext->bNomatch;
232196}
232197
232198static int fts5ExprNodeNext_OR(
232199 Fts5Expr *pExpr,
232200 Fts5ExprNode *pNode,
232201 int bFromValid,
232202 i64 iFrom
232203){
232204 int i;
232205 i64 iLast = pNode->iRowid;
232206
232207 for(i=0; i<pNode->nChild; i++){
232208 Fts5ExprNode *p1 = pNode->apChild[i];
232209 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
232210 if( p1->bEof==0 ){
232211 if( (p1->iRowid==iLast)
232212 || (bFromValid && fts5RowidCmp(pExpr, iLhs: p1->iRowid, iRhs: iFrom)<0)
232213 ){
232214 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
232215 if( rc!=SQLITE_OK ){
232216 pNode->bNomatch = 0;
232217 return rc;
232218 }
232219 }
232220 }
232221 }
232222
232223 fts5ExprNodeTest_OR(pExpr, pNode);
232224 return SQLITE_OK;
232225}
232226
232227/*
232228** Argument pNode is an FTS5_AND node.
232229*/
232230static int fts5ExprNodeTest_AND(
232231 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
232232 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
232233){
232234 int iChild;
232235 i64 iLast = pAnd->iRowid;
232236 int rc = SQLITE_OK;
232237 int bMatch;
232238
232239 assert( pAnd->bEof==0 );
232240 do {
232241 pAnd->bNomatch = 0;
232242 bMatch = 1;
232243 for(iChild=0; iChild<pAnd->nChild; iChild++){
232244 Fts5ExprNode *pChild = pAnd->apChild[iChild];
232245 int cmp = fts5RowidCmp(pExpr, iLhs: iLast, iRhs: pChild->iRowid);
232246 if( cmp>0 ){
232247 /* Advance pChild until it points to iLast or laster */
232248 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
232249 if( rc!=SQLITE_OK ){
232250 pAnd->bNomatch = 0;
232251 return rc;
232252 }
232253 }
232254
232255 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
232256 ** the child node is guaranteed to have advanced at least as far as
232257 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
232258 ** new lastest rowid seen so far. */
232259 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
232260 if( pChild->bEof ){
232261 fts5ExprSetEof(pNode: pAnd);
232262 bMatch = 1;
232263 break;
232264 }else if( iLast!=pChild->iRowid ){
232265 bMatch = 0;
232266 iLast = pChild->iRowid;
232267 }
232268
232269 if( pChild->bNomatch ){
232270 pAnd->bNomatch = 1;
232271 }
232272 }
232273 }while( bMatch==0 );
232274
232275 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
232276 fts5ExprNodeZeroPoslist(pNode: pAnd);
232277 }
232278 pAnd->iRowid = iLast;
232279 return SQLITE_OK;
232280}
232281
232282static int fts5ExprNodeNext_AND(
232283 Fts5Expr *pExpr,
232284 Fts5ExprNode *pNode,
232285 int bFromValid,
232286 i64 iFrom
232287){
232288 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
232289 if( rc==SQLITE_OK ){
232290 rc = fts5ExprNodeTest_AND(pExpr, pAnd: pNode);
232291 }else{
232292 pNode->bNomatch = 0;
232293 }
232294 return rc;
232295}
232296
232297static int fts5ExprNodeTest_NOT(
232298 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
232299 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
232300){
232301 int rc = SQLITE_OK;
232302 Fts5ExprNode *p1 = pNode->apChild[0];
232303 Fts5ExprNode *p2 = pNode->apChild[1];
232304 assert( pNode->nChild==2 );
232305
232306 while( rc==SQLITE_OK && p1->bEof==0 ){
232307 int cmp = fts5NodeCompare(pExpr, p1, p2);
232308 if( cmp>0 ){
232309 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
232310 cmp = fts5NodeCompare(pExpr, p1, p2);
232311 }
232312 assert( rc!=SQLITE_OK || cmp<=0 );
232313 if( cmp || p2->bNomatch ) break;
232314 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
232315 }
232316 pNode->bEof = p1->bEof;
232317 pNode->bNomatch = p1->bNomatch;
232318 pNode->iRowid = p1->iRowid;
232319 if( p1->bEof ){
232320 fts5ExprNodeZeroPoslist(pNode: p2);
232321 }
232322 return rc;
232323}
232324
232325static int fts5ExprNodeNext_NOT(
232326 Fts5Expr *pExpr,
232327 Fts5ExprNode *pNode,
232328 int bFromValid,
232329 i64 iFrom
232330){
232331 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
232332 if( rc==SQLITE_OK ){
232333 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
232334 }
232335 if( rc!=SQLITE_OK ){
232336 pNode->bNomatch = 0;
232337 }
232338 return rc;
232339}
232340
232341/*
232342** If pNode currently points to a match, this function returns SQLITE_OK
232343** without modifying it. Otherwise, pNode is advanced until it does point
232344** to a match or EOF is reached.
232345*/
232346static int fts5ExprNodeTest(
232347 Fts5Expr *pExpr, /* Expression of which pNode is a part */
232348 Fts5ExprNode *pNode /* Expression node to test */
232349){
232350 int rc = SQLITE_OK;
232351 if( pNode->bEof==0 ){
232352 switch( pNode->eType ){
232353
232354 case FTS5_STRING: {
232355 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
232356 break;
232357 }
232358
232359 case FTS5_TERM: {
232360 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
232361 break;
232362 }
232363
232364 case FTS5_AND: {
232365 rc = fts5ExprNodeTest_AND(pExpr, pAnd: pNode);
232366 break;
232367 }
232368
232369 case FTS5_OR: {
232370 fts5ExprNodeTest_OR(pExpr, pNode);
232371 break;
232372 }
232373
232374 default: assert( pNode->eType==FTS5_NOT ); {
232375 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
232376 break;
232377 }
232378 }
232379 }
232380 return rc;
232381}
232382
232383
232384/*
232385** Set node pNode, which is part of expression pExpr, to point to the first
232386** match. If there are no matches, set the Node.bEof flag to indicate EOF.
232387**
232388** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
232389** It is not an error if there are no matches.
232390*/
232391static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
232392 int rc = SQLITE_OK;
232393 pNode->bEof = 0;
232394 pNode->bNomatch = 0;
232395
232396 if( Fts5NodeIsString(pNode) ){
232397 /* Initialize all term iterators in the NEAR object. */
232398 rc = fts5ExprNearInitAll(pExpr, pNode);
232399 }else if( pNode->xNext==0 ){
232400 pNode->bEof = 1;
232401 }else{
232402 int i;
232403 int nEof = 0;
232404 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
232405 Fts5ExprNode *pChild = pNode->apChild[i];
232406 rc = fts5ExprNodeFirst(pExpr, pNode: pNode->apChild[i]);
232407 assert( pChild->bEof==0 || pChild->bEof==1 );
232408 nEof += pChild->bEof;
232409 }
232410 pNode->iRowid = pNode->apChild[0]->iRowid;
232411
232412 switch( pNode->eType ){
232413 case FTS5_AND:
232414 if( nEof>0 ) fts5ExprSetEof(pNode);
232415 break;
232416
232417 case FTS5_OR:
232418 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
232419 break;
232420
232421 default:
232422 assert( pNode->eType==FTS5_NOT );
232423 pNode->bEof = pNode->apChild[0]->bEof;
232424 break;
232425 }
232426 }
232427
232428 if( rc==SQLITE_OK ){
232429 rc = fts5ExprNodeTest(pExpr, pNode);
232430 }
232431 return rc;
232432}
232433
232434
232435/*
232436** Begin iterating through the set of documents in index pIdx matched by
232437** the MATCH expression passed as the first argument. If the "bDesc"
232438** parameter is passed a non-zero value, iteration is in descending rowid
232439** order. Or, if it is zero, in ascending order.
232440**
232441** If iterating in ascending rowid order (bDesc==0), the first document
232442** visited is that with the smallest rowid that is larger than or equal
232443** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
232444** then the first document visited must have a rowid smaller than or
232445** equal to iFirst.
232446**
232447** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
232448** is not considered an error if the query does not match any documents.
232449*/
232450static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
232451 Fts5ExprNode *pRoot = p->pRoot;
232452 int rc; /* Return code */
232453
232454 p->pIndex = pIdx;
232455 p->bDesc = bDesc;
232456 rc = fts5ExprNodeFirst(pExpr: p, pNode: pRoot);
232457
232458 /* If not at EOF but the current rowid occurs earlier than iFirst in
232459 ** the iteration order, move to document iFirst or later. */
232460 if( rc==SQLITE_OK
232461 && 0==pRoot->bEof
232462 && fts5RowidCmp(pExpr: p, iLhs: pRoot->iRowid, iRhs: iFirst)<0
232463 ){
232464 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
232465 }
232466
232467 /* If the iterator is not at a real match, skip forward until it is. */
232468 while( pRoot->bNomatch && rc==SQLITE_OK ){
232469 assert( pRoot->bEof==0 );
232470 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
232471 }
232472 return rc;
232473}
232474
232475/*
232476** Move to the next document
232477**
232478** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
232479** is not considered an error if the query does not match any documents.
232480*/
232481static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
232482 int rc;
232483 Fts5ExprNode *pRoot = p->pRoot;
232484 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
232485 do {
232486 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
232487 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
232488 }while( pRoot->bNomatch );
232489 if( fts5RowidCmp(pExpr: p, iLhs: pRoot->iRowid, iRhs: iLast)>0 ){
232490 pRoot->bEof = 1;
232491 }
232492 return rc;
232493}
232494
232495static int sqlite3Fts5ExprEof(Fts5Expr *p){
232496 return p->pRoot->bEof;
232497}
232498
232499static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
232500 return p->pRoot->iRowid;
232501}
232502
232503static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
232504 int rc = SQLITE_OK;
232505 *pz = sqlite3Fts5Strndup(pRc: &rc, pIn: pToken->p, nIn: pToken->n);
232506 return rc;
232507}
232508
232509/*
232510** Free the phrase object passed as the only argument.
232511*/
232512static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
232513 if( pPhrase ){
232514 int i;
232515 for(i=0; i<pPhrase->nTerm; i++){
232516 Fts5ExprTerm *pSyn;
232517 Fts5ExprTerm *pNext;
232518 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
232519 sqlite3_free(p: pTerm->zTerm);
232520 sqlite3Fts5IterClose(pTerm->pIter);
232521 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
232522 pNext = pSyn->pSynonym;
232523 sqlite3Fts5IterClose(pSyn->pIter);
232524 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
232525 sqlite3_free(p: pSyn);
232526 }
232527 }
232528 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
232529 sqlite3_free(p: pPhrase);
232530 }
232531}
232532
232533/*
232534** Set the "bFirst" flag on the first token of the phrase passed as the
232535** only argument.
232536*/
232537static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
232538 if( pPhrase && pPhrase->nTerm ){
232539 pPhrase->aTerm[0].bFirst = 1;
232540 }
232541}
232542
232543/*
232544** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
232545** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
232546** appended to it and the results returned.
232547**
232548** If an OOM error occurs, both the pNear and pPhrase objects are freed and
232549** NULL returned.
232550*/
232551static Fts5ExprNearset *sqlite3Fts5ParseNearset(
232552 Fts5Parse *pParse, /* Parse context */
232553 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
232554 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
232555){
232556 const int SZALLOC = 8;
232557 Fts5ExprNearset *pRet = 0;
232558
232559 if( pParse->rc==SQLITE_OK ){
232560 if( pPhrase==0 ){
232561 return pNear;
232562 }
232563 if( pNear==0 ){
232564 sqlite3_int64 nByte;
232565 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
232566 pRet = sqlite3_malloc64(n: nByte);
232567 if( pRet==0 ){
232568 pParse->rc = SQLITE_NOMEM;
232569 }else{
232570 memset(s: pRet, c: 0, n: (size_t)nByte);
232571 }
232572 }else if( (pNear->nPhrase % SZALLOC)==0 ){
232573 int nNew = pNear->nPhrase + SZALLOC;
232574 sqlite3_int64 nByte;
232575
232576 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
232577 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pOld: pNear, n: nByte);
232578 if( pRet==0 ){
232579 pParse->rc = SQLITE_NOMEM;
232580 }
232581 }else{
232582 pRet = pNear;
232583 }
232584 }
232585
232586 if( pRet==0 ){
232587 assert( pParse->rc!=SQLITE_OK );
232588 sqlite3Fts5ParseNearsetFree(pNear);
232589 sqlite3Fts5ParsePhraseFree(pPhrase);
232590 }else{
232591 if( pRet->nPhrase>0 ){
232592 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
232593 assert( pParse!=0 );
232594 assert( pParse->apPhrase!=0 );
232595 assert( pParse->nPhrase>=2 );
232596 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
232597 if( pPhrase->nTerm==0 ){
232598 fts5ExprPhraseFree(pPhrase);
232599 pRet->nPhrase--;
232600 pParse->nPhrase--;
232601 pPhrase = pLast;
232602 }else if( pLast->nTerm==0 ){
232603 fts5ExprPhraseFree(pPhrase: pLast);
232604 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
232605 pParse->nPhrase--;
232606 pRet->nPhrase--;
232607 }
232608 }
232609 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
232610 }
232611 return pRet;
232612}
232613
232614typedef struct TokenCtx TokenCtx;
232615struct TokenCtx {
232616 Fts5ExprPhrase *pPhrase;
232617 int rc;
232618};
232619
232620/*
232621** Callback for tokenizing terms used by ParseTerm().
232622*/
232623static int fts5ParseTokenize(
232624 void *pContext, /* Pointer to Fts5InsertCtx object */
232625 int tflags, /* Mask of FTS5_TOKEN_* flags */
232626 const char *pToken, /* Buffer containing token */
232627 int nToken, /* Size of token in bytes */
232628 int iUnused1, /* Start offset of token */
232629 int iUnused2 /* End offset of token */
232630){
232631 int rc = SQLITE_OK;
232632 const int SZALLOC = 8;
232633 TokenCtx *pCtx = (TokenCtx*)pContext;
232634 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
232635
232636 UNUSED_PARAM2(iUnused1, iUnused2);
232637
232638 /* If an error has already occurred, this is a no-op */
232639 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
232640 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
232641
232642 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
232643 Fts5ExprTerm *pSyn;
232644 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
232645 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(n: nByte);
232646 if( pSyn==0 ){
232647 rc = SQLITE_NOMEM;
232648 }else{
232649 memset(s: pSyn, c: 0, n: (size_t)nByte);
232650 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
232651 memcpy(dest: pSyn->zTerm, src: pToken, n: nToken);
232652 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
232653 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
232654 }
232655 }else{
232656 Fts5ExprTerm *pTerm;
232657 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
232658 Fts5ExprPhrase *pNew;
232659 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
232660
232661 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pOld: pPhrase,
232662 n: sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
232663 );
232664 if( pNew==0 ){
232665 rc = SQLITE_NOMEM;
232666 }else{
232667 if( pPhrase==0 ) memset(s: pNew, c: 0, n: sizeof(Fts5ExprPhrase));
232668 pCtx->pPhrase = pPhrase = pNew;
232669 pNew->nTerm = nNew - SZALLOC;
232670 }
232671 }
232672
232673 if( rc==SQLITE_OK ){
232674 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
232675 memset(s: pTerm, c: 0, n: sizeof(Fts5ExprTerm));
232676 pTerm->zTerm = sqlite3Fts5Strndup(pRc: &rc, pIn: pToken, nIn: nToken);
232677 }
232678 }
232679
232680 pCtx->rc = rc;
232681 return rc;
232682}
232683
232684
232685/*
232686** Free the phrase object passed as the only argument.
232687*/
232688static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
232689 fts5ExprPhraseFree(pPhrase);
232690}
232691
232692/*
232693** Free the phrase object passed as the second argument.
232694*/
232695static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
232696 if( pNear ){
232697 int i;
232698 for(i=0; i<pNear->nPhrase; i++){
232699 fts5ExprPhraseFree(pPhrase: pNear->apPhrase[i]);
232700 }
232701 sqlite3_free(p: pNear->pColset);
232702 sqlite3_free(p: pNear);
232703 }
232704}
232705
232706static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
232707 assert( pParse->pExpr==0 );
232708 pParse->pExpr = p;
232709}
232710
232711static int parseGrowPhraseArray(Fts5Parse *pParse){
232712 if( (pParse->nPhrase % 8)==0 ){
232713 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
232714 Fts5ExprPhrase **apNew;
232715 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pOld: pParse->apPhrase, n: nByte);
232716 if( apNew==0 ){
232717 pParse->rc = SQLITE_NOMEM;
232718 return SQLITE_NOMEM;
232719 }
232720 pParse->apPhrase = apNew;
232721 }
232722 return SQLITE_OK;
232723}
232724
232725/*
232726** This function is called by the parser to process a string token. The
232727** string may or may not be quoted. In any case it is tokenized and a
232728** phrase object consisting of all tokens returned.
232729*/
232730static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
232731 Fts5Parse *pParse, /* Parse context */
232732 Fts5ExprPhrase *pAppend, /* Phrase to append to */
232733 Fts5Token *pToken, /* String to tokenize */
232734 int bPrefix /* True if there is a trailing "*" */
232735){
232736 Fts5Config *pConfig = pParse->pConfig;
232737 TokenCtx sCtx; /* Context object passed to callback */
232738 int rc; /* Tokenize return code */
232739 char *z = 0;
232740
232741 memset(s: &sCtx, c: 0, n: sizeof(TokenCtx));
232742 sCtx.pPhrase = pAppend;
232743
232744 rc = fts5ParseStringFromToken(pToken, pz: &z);
232745 if( rc==SQLITE_OK ){
232746 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
232747 int n;
232748 sqlite3Fts5Dequote(z);
232749 n = (int)strlen(s: z);
232750 rc = sqlite3Fts5Tokenize(pConfig, flags, pText: z, nText: n, pCtx: &sCtx, xToken: fts5ParseTokenize);
232751 }
232752 sqlite3_free(p: z);
232753 if( rc || (rc = sCtx.rc) ){
232754 pParse->rc = rc;
232755 fts5ExprPhraseFree(pPhrase: sCtx.pPhrase);
232756 sCtx.pPhrase = 0;
232757 }else{
232758
232759 if( pAppend==0 ){
232760 if( parseGrowPhraseArray(pParse) ){
232761 fts5ExprPhraseFree(pPhrase: sCtx.pPhrase);
232762 return 0;
232763 }
232764 pParse->nPhrase++;
232765 }
232766
232767 if( sCtx.pPhrase==0 ){
232768 /* This happens when parsing a token or quoted phrase that contains
232769 ** no token characters at all. (e.g ... MATCH '""'). */
232770 sCtx.pPhrase = sqlite3Fts5MallocZero(pRc: &pParse->rc, nByte: sizeof(Fts5ExprPhrase));
232771 }else if( sCtx.pPhrase->nTerm ){
232772 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
232773 }
232774 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
232775 }
232776
232777 return sCtx.pPhrase;
232778}
232779
232780/*
232781** Create a new FTS5 expression by cloning phrase iPhrase of the
232782** expression passed as the second argument.
232783*/
232784static int sqlite3Fts5ExprClonePhrase(
232785 Fts5Expr *pExpr,
232786 int iPhrase,
232787 Fts5Expr **ppNew
232788){
232789 int rc = SQLITE_OK; /* Return code */
232790 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
232791 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
232792 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
232793
232794 pOrig = pExpr->apExprPhrase[iPhrase];
232795 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Expr));
232796 if( rc==SQLITE_OK ){
232797 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(pRc: &rc,
232798 nByte: sizeof(Fts5ExprPhrase*));
232799 }
232800 if( rc==SQLITE_OK ){
232801 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &rc,
232802 nByte: sizeof(Fts5ExprNode));
232803 }
232804 if( rc==SQLITE_OK ){
232805 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(pRc: &rc,
232806 nByte: sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
232807 }
232808 if( rc==SQLITE_OK ){
232809 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
232810 if( pColsetOrig ){
232811 sqlite3_int64 nByte;
232812 Fts5Colset *pColset;
232813 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
232814 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
232815 if( pColset ){
232816 memcpy(dest: pColset, src: pColsetOrig, n: (size_t)nByte);
232817 }
232818 pNew->pRoot->pNear->pColset = pColset;
232819 }
232820 }
232821
232822 if( pOrig->nTerm ){
232823 int i; /* Used to iterate through phrase terms */
232824 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
232825 int tflags = 0;
232826 Fts5ExprTerm *p;
232827 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
232828 const char *zTerm = p->zTerm;
232829 rc = fts5ParseTokenize(pContext: (void*)&sCtx, tflags, pToken: zTerm, nToken: (int)strlen(s: zTerm),
232830 iUnused1: 0, iUnused2: 0);
232831 tflags = FTS5_TOKEN_COLOCATED;
232832 }
232833 if( rc==SQLITE_OK ){
232834 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
232835 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
232836 }
232837 }
232838 }else{
232839 /* This happens when parsing a token or quoted phrase that contains
232840 ** no token characters at all. (e.g ... MATCH '""'). */
232841 sCtx.pPhrase = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5ExprPhrase));
232842 }
232843
232844 if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
232845 /* All the allocations succeeded. Put the expression object together. */
232846 pNew->pIndex = pExpr->pIndex;
232847 pNew->pConfig = pExpr->pConfig;
232848 pNew->nPhrase = 1;
232849 pNew->apExprPhrase[0] = sCtx.pPhrase;
232850 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
232851 pNew->pRoot->pNear->nPhrase = 1;
232852 sCtx.pPhrase->pNode = pNew->pRoot;
232853
232854 if( pOrig->nTerm==1
232855 && pOrig->aTerm[0].pSynonym==0
232856 && pOrig->aTerm[0].bFirst==0
232857 ){
232858 pNew->pRoot->eType = FTS5_TERM;
232859 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
232860 }else{
232861 pNew->pRoot->eType = FTS5_STRING;
232862 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
232863 }
232864 }else{
232865 sqlite3Fts5ExprFree(p: pNew);
232866 fts5ExprPhraseFree(pPhrase: sCtx.pPhrase);
232867 pNew = 0;
232868 }
232869
232870 *ppNew = pNew;
232871 return rc;
232872}
232873
232874
232875/*
232876** Token pTok has appeared in a MATCH expression where the NEAR operator
232877** is expected. If token pTok does not contain "NEAR", store an error
232878** in the pParse object.
232879*/
232880static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
232881 if( pTok->n!=4 || memcmp(s1: "NEAR", s2: pTok->p, n: 4) ){
232882 sqlite3Fts5ParseError(
232883 pParse, zFmt: "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
232884 );
232885 }
232886}
232887
232888static void sqlite3Fts5ParseSetDistance(
232889 Fts5Parse *pParse,
232890 Fts5ExprNearset *pNear,
232891 Fts5Token *p
232892){
232893 if( pNear ){
232894 int nNear = 0;
232895 int i;
232896 if( p->n ){
232897 for(i=0; i<p->n; i++){
232898 char c = (char)p->p[i];
232899 if( c<'0' || c>'9' ){
232900 sqlite3Fts5ParseError(
232901 pParse, zFmt: "expected integer, got \"%.*s\"", p->n, p->p
232902 );
232903 return;
232904 }
232905 nNear = nNear * 10 + (p->p[i] - '0');
232906 }
232907 }else{
232908 nNear = FTS5_DEFAULT_NEARDIST;
232909 }
232910 pNear->nNear = nNear;
232911 }
232912}
232913
232914/*
232915** The second argument passed to this function may be NULL, or it may be
232916** an existing Fts5Colset object. This function returns a pointer to
232917** a new colset object containing the contents of (p) with new value column
232918** number iCol appended.
232919**
232920** If an OOM error occurs, store an error code in pParse and return NULL.
232921** The old colset object (if any) is not freed in this case.
232922*/
232923static Fts5Colset *fts5ParseColset(
232924 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
232925 Fts5Colset *p, /* Existing colset object */
232926 int iCol /* New column to add to colset object */
232927){
232928 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
232929 Fts5Colset *pNew; /* New colset object to return */
232930
232931 assert( pParse->rc==SQLITE_OK );
232932 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
232933
232934 pNew = sqlite3_realloc64(pOld: p, n: sizeof(Fts5Colset) + sizeof(int)*nCol);
232935 if( pNew==0 ){
232936 pParse->rc = SQLITE_NOMEM;
232937 }else{
232938 int *aiCol = pNew->aiCol;
232939 int i, j;
232940 for(i=0; i<nCol; i++){
232941 if( aiCol[i]==iCol ) return pNew;
232942 if( aiCol[i]>iCol ) break;
232943 }
232944 for(j=nCol; j>i; j--){
232945 aiCol[j] = aiCol[j-1];
232946 }
232947 aiCol[i] = iCol;
232948 pNew->nCol = nCol+1;
232949
232950#ifndef NDEBUG
232951 /* Check that the array is in order and contains no duplicate entries. */
232952 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
232953#endif
232954 }
232955
232956 return pNew;
232957}
232958
232959/*
232960** Allocate and return an Fts5Colset object specifying the inverse of
232961** the colset passed as the second argument. Free the colset passed
232962** as the second argument before returning.
232963*/
232964static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
232965 Fts5Colset *pRet;
232966 int nCol = pParse->pConfig->nCol;
232967
232968 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc: &pParse->rc,
232969 nByte: sizeof(Fts5Colset) + sizeof(int)*nCol
232970 );
232971 if( pRet ){
232972 int i;
232973 int iOld = 0;
232974 for(i=0; i<nCol; i++){
232975 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
232976 pRet->aiCol[pRet->nCol++] = i;
232977 }else{
232978 iOld++;
232979 }
232980 }
232981 }
232982
232983 sqlite3_free(p);
232984 return pRet;
232985}
232986
232987static Fts5Colset *sqlite3Fts5ParseColset(
232988 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
232989 Fts5Colset *pColset, /* Existing colset object */
232990 Fts5Token *p
232991){
232992 Fts5Colset *pRet = 0;
232993 int iCol;
232994 char *z; /* Dequoted copy of token p */
232995
232996 z = sqlite3Fts5Strndup(pRc: &pParse->rc, pIn: p->p, nIn: p->n);
232997 if( pParse->rc==SQLITE_OK ){
232998 Fts5Config *pConfig = pParse->pConfig;
232999 sqlite3Fts5Dequote(z);
233000 for(iCol=0; iCol<pConfig->nCol; iCol++){
233001 if( 0==sqlite3_stricmp(zLeft: pConfig->azCol[iCol], zRight: z) ) break;
233002 }
233003 if( iCol==pConfig->nCol ){
233004 sqlite3Fts5ParseError(pParse, zFmt: "no such column: %s", z);
233005 }else{
233006 pRet = fts5ParseColset(pParse, p: pColset, iCol);
233007 }
233008 sqlite3_free(p: z);
233009 }
233010
233011 if( pRet==0 ){
233012 assert( pParse->rc!=SQLITE_OK );
233013 sqlite3_free(p: pColset);
233014 }
233015
233016 return pRet;
233017}
233018
233019/*
233020** If argument pOrig is NULL, or if (*pRc) is set to anything other than
233021** SQLITE_OK when this function is called, NULL is returned.
233022**
233023** Otherwise, a copy of (*pOrig) is made into memory obtained from
233024** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
233025** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
233026*/
233027static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
233028 Fts5Colset *pRet;
233029 if( pOrig ){
233030 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
233031 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
233032 if( pRet ){
233033 memcpy(dest: pRet, src: pOrig, n: (size_t)nByte);
233034 }
233035 }else{
233036 pRet = 0;
233037 }
233038 return pRet;
233039}
233040
233041/*
233042** Remove from colset pColset any columns that are not also in colset pMerge.
233043*/
233044static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
233045 int iIn = 0; /* Next input in pColset */
233046 int iMerge = 0; /* Next input in pMerge */
233047 int iOut = 0; /* Next output slot in pColset */
233048
233049 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
233050 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
233051 if( iDiff==0 ){
233052 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
233053 iMerge++;
233054 iIn++;
233055 }else if( iDiff>0 ){
233056 iMerge++;
233057 }else{
233058 iIn++;
233059 }
233060 }
233061 pColset->nCol = iOut;
233062}
233063
233064/*
233065** Recursively apply colset pColset to expression node pNode and all of
233066** its decendents. If (*ppFree) is not NULL, it contains a spare copy
233067** of pColset. This function may use the spare copy and set (*ppFree) to
233068** zero, or it may create copies of pColset using fts5CloneColset().
233069*/
233070static void fts5ParseSetColset(
233071 Fts5Parse *pParse,
233072 Fts5ExprNode *pNode,
233073 Fts5Colset *pColset,
233074 Fts5Colset **ppFree
233075){
233076 if( pParse->rc==SQLITE_OK ){
233077 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
233078 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
233079 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
233080 );
233081 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
233082 Fts5ExprNearset *pNear = pNode->pNear;
233083 if( pNear->pColset ){
233084 fts5MergeColset(pColset: pNear->pColset, pMerge: pColset);
233085 if( pNear->pColset->nCol==0 ){
233086 pNode->eType = FTS5_EOF;
233087 pNode->xNext = 0;
233088 }
233089 }else if( *ppFree ){
233090 pNear->pColset = pColset;
233091 *ppFree = 0;
233092 }else{
233093 pNear->pColset = fts5CloneColset(pRc: &pParse->rc, pOrig: pColset);
233094 }
233095 }else{
233096 int i;
233097 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
233098 for(i=0; i<pNode->nChild; i++){
233099 fts5ParseSetColset(pParse, pNode: pNode->apChild[i], pColset, ppFree);
233100 }
233101 }
233102 }
233103}
233104
233105/*
233106** Apply colset pColset to expression node pExpr and all of its descendents.
233107*/
233108static void sqlite3Fts5ParseSetColset(
233109 Fts5Parse *pParse,
233110 Fts5ExprNode *pExpr,
233111 Fts5Colset *pColset
233112){
233113 Fts5Colset *pFree = pColset;
233114 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
233115 sqlite3Fts5ParseError(pParse,
233116 zFmt: "fts5: column queries are not supported (detail=none)"
233117 );
233118 }else{
233119 fts5ParseSetColset(pParse, pNode: pExpr, pColset, ppFree: &pFree);
233120 }
233121 sqlite3_free(p: pFree);
233122}
233123
233124static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
233125 switch( pNode->eType ){
233126 case FTS5_STRING: {
233127 Fts5ExprNearset *pNear = pNode->pNear;
233128 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
233129 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
233130 && pNear->apPhrase[0]->aTerm[0].bFirst==0
233131 ){
233132 pNode->eType = FTS5_TERM;
233133 pNode->xNext = fts5ExprNodeNext_TERM;
233134 }else{
233135 pNode->xNext = fts5ExprNodeNext_STRING;
233136 }
233137 break;
233138 };
233139
233140 case FTS5_OR: {
233141 pNode->xNext = fts5ExprNodeNext_OR;
233142 break;
233143 };
233144
233145 case FTS5_AND: {
233146 pNode->xNext = fts5ExprNodeNext_AND;
233147 break;
233148 };
233149
233150 default: assert( pNode->eType==FTS5_NOT ); {
233151 pNode->xNext = fts5ExprNodeNext_NOT;
233152 break;
233153 };
233154 }
233155}
233156
233157static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
233158 int ii = p->nChild;
233159 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
233160 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
233161 memcpy(dest: &p->apChild[p->nChild], src: pSub->apChild, n: nByte);
233162 p->nChild += pSub->nChild;
233163 sqlite3_free(p: pSub);
233164 }else{
233165 p->apChild[p->nChild++] = pSub;
233166 }
233167 for( ; ii<p->nChild; ii++){
233168 p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1);
233169 }
233170}
233171
233172/*
233173** This function is used when parsing LIKE or GLOB patterns against
233174** trigram indexes that specify either detail=column or detail=none.
233175** It converts a phrase:
233176**
233177** abc + def + ghi
233178**
233179** into an AND tree:
233180**
233181** abc AND def AND ghi
233182*/
233183static Fts5ExprNode *fts5ParsePhraseToAnd(
233184 Fts5Parse *pParse,
233185 Fts5ExprNearset *pNear
233186){
233187 int nTerm = pNear->apPhrase[0]->nTerm;
233188 int ii;
233189 int nByte;
233190 Fts5ExprNode *pRet;
233191
233192 assert( pNear->nPhrase==1 );
233193 assert( pParse->bPhraseToAnd );
233194
233195 nByte = sizeof(Fts5ExprNode) + nTerm*sizeof(Fts5ExprNode*);
233196 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &pParse->rc, nByte);
233197 if( pRet ){
233198 pRet->eType = FTS5_AND;
233199 pRet->nChild = nTerm;
233200 pRet->iHeight = 1;
233201 fts5ExprAssignXNext(pNode: pRet);
233202 pParse->nPhrase--;
233203 for(ii=0; ii<nTerm; ii++){
233204 Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
233205 pRc: &pParse->rc, nByte: sizeof(Fts5ExprPhrase)
233206 );
233207 if( pPhrase ){
233208 if( parseGrowPhraseArray(pParse) ){
233209 fts5ExprPhraseFree(pPhrase);
233210 }else{
233211 pParse->apPhrase[pParse->nPhrase++] = pPhrase;
233212 pPhrase->nTerm = 1;
233213 pPhrase->aTerm[0].zTerm = sqlite3Fts5Strndup(
233214 pRc: &pParse->rc, pIn: pNear->apPhrase[0]->aTerm[ii].zTerm, nIn: -1
233215 );
233216 pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING,
233217 pLeft: 0, pRight: 0, pNear: sqlite3Fts5ParseNearset(pParse, pNear: 0, pPhrase)
233218 );
233219 }
233220 }
233221 }
233222
233223 if( pParse->rc ){
233224 sqlite3Fts5ParseNodeFree(p: pRet);
233225 pRet = 0;
233226 }else{
233227 sqlite3Fts5ParseNearsetFree(pNear);
233228 }
233229 }
233230
233231 return pRet;
233232}
233233
233234/*
233235** Allocate and return a new expression object. If anything goes wrong (i.e.
233236** OOM error), leave an error code in pParse and return NULL.
233237*/
233238static Fts5ExprNode *sqlite3Fts5ParseNode(
233239 Fts5Parse *pParse, /* Parse context */
233240 int eType, /* FTS5_STRING, AND, OR or NOT */
233241 Fts5ExprNode *pLeft, /* Left hand child expression */
233242 Fts5ExprNode *pRight, /* Right hand child expression */
233243 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
233244){
233245 Fts5ExprNode *pRet = 0;
233246
233247 if( pParse->rc==SQLITE_OK ){
233248 int nChild = 0; /* Number of children of returned node */
233249 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
233250
233251 assert( (eType!=FTS5_STRING && !pNear)
233252 || (eType==FTS5_STRING && !pLeft && !pRight)
233253 );
233254 if( eType==FTS5_STRING && pNear==0 ) return 0;
233255 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
233256 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
233257
233258 if( eType==FTS5_STRING
233259 && pParse->bPhraseToAnd
233260 && pNear->apPhrase[0]->nTerm>1
233261 ){
233262 pRet = fts5ParsePhraseToAnd(pParse, pNear);
233263 }else{
233264 if( eType==FTS5_NOT ){
233265 nChild = 2;
233266 }else if( eType==FTS5_AND || eType==FTS5_OR ){
233267 nChild = 2;
233268 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
233269 if( pRight->eType==eType ) nChild += pRight->nChild-1;
233270 }
233271
233272 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
233273 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &pParse->rc, nByte);
233274
233275 if( pRet ){
233276 pRet->eType = eType;
233277 pRet->pNear = pNear;
233278 fts5ExprAssignXNext(pNode: pRet);
233279 if( eType==FTS5_STRING ){
233280 int iPhrase;
233281 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
233282 pNear->apPhrase[iPhrase]->pNode = pRet;
233283 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
233284 pRet->xNext = 0;
233285 pRet->eType = FTS5_EOF;
233286 }
233287 }
233288
233289 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
233290 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
233291 if( pNear->nPhrase!=1
233292 || pPhrase->nTerm>1
233293 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
233294 ){
233295 sqlite3Fts5ParseError(pParse,
233296 zFmt: "fts5: %s queries are not supported (detail!=full)",
233297 pNear->nPhrase==1 ? "phrase": "NEAR"
233298 );
233299 sqlite3_free(p: pRet);
233300 pRet = 0;
233301 }
233302 }
233303 }else{
233304 fts5ExprAddChildren(p: pRet, pSub: pLeft);
233305 fts5ExprAddChildren(p: pRet, pSub: pRight);
233306 if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH ){
233307 sqlite3Fts5ParseError(pParse,
233308 zFmt: "fts5 expression tree is too large (maximum depth %d)",
233309 SQLITE_FTS5_MAX_EXPR_DEPTH
233310 );
233311 sqlite3_free(p: pRet);
233312 pRet = 0;
233313 }
233314 }
233315 }
233316 }
233317 }
233318
233319 if( pRet==0 ){
233320 assert( pParse->rc!=SQLITE_OK );
233321 sqlite3Fts5ParseNodeFree(p: pLeft);
233322 sqlite3Fts5ParseNodeFree(p: pRight);
233323 sqlite3Fts5ParseNearsetFree(pNear);
233324 }
233325 return pRet;
233326}
233327
233328static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
233329 Fts5Parse *pParse, /* Parse context */
233330 Fts5ExprNode *pLeft, /* Left hand child expression */
233331 Fts5ExprNode *pRight /* Right hand child expression */
233332){
233333 Fts5ExprNode *pRet = 0;
233334 Fts5ExprNode *pPrev;
233335
233336 if( pParse->rc ){
233337 sqlite3Fts5ParseNodeFree(p: pLeft);
233338 sqlite3Fts5ParseNodeFree(p: pRight);
233339 }else{
233340
233341 assert( pLeft->eType==FTS5_STRING
233342 || pLeft->eType==FTS5_TERM
233343 || pLeft->eType==FTS5_EOF
233344 || pLeft->eType==FTS5_AND
233345 );
233346 assert( pRight->eType==FTS5_STRING
233347 || pRight->eType==FTS5_TERM
233348 || pRight->eType==FTS5_EOF
233349 );
233350
233351 if( pLeft->eType==FTS5_AND ){
233352 pPrev = pLeft->apChild[pLeft->nChild-1];
233353 }else{
233354 pPrev = pLeft;
233355 }
233356 assert( pPrev->eType==FTS5_STRING
233357 || pPrev->eType==FTS5_TERM
233358 || pPrev->eType==FTS5_EOF
233359 );
233360
233361 if( pRight->eType==FTS5_EOF ){
233362 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
233363 sqlite3Fts5ParseNodeFree(p: pRight);
233364 pRet = pLeft;
233365 pParse->nPhrase--;
233366 }
233367 else if( pPrev->eType==FTS5_EOF ){
233368 Fts5ExprPhrase **ap;
233369
233370 if( pPrev==pLeft ){
233371 pRet = pRight;
233372 }else{
233373 pLeft->apChild[pLeft->nChild-1] = pRight;
233374 pRet = pLeft;
233375 }
233376
233377 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
233378 assert( ap[0]==pPrev->pNear->apPhrase[0] );
233379 memmove(dest: ap, src: &ap[1], n: sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
233380 pParse->nPhrase--;
233381
233382 sqlite3Fts5ParseNodeFree(p: pPrev);
233383 }
233384 else{
233385 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, pNear: 0);
233386 }
233387 }
233388
233389 return pRet;
233390}
233391
233392#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
233393static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
233394 sqlite3_int64 nByte = 0;
233395 Fts5ExprTerm *p;
233396 char *zQuoted;
233397
233398 /* Determine the maximum amount of space required. */
233399 for(p=pTerm; p; p=p->pSynonym){
233400 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
233401 }
233402 zQuoted = sqlite3_malloc64(nByte);
233403
233404 if( zQuoted ){
233405 int i = 0;
233406 for(p=pTerm; p; p=p->pSynonym){
233407 char *zIn = p->zTerm;
233408 zQuoted[i++] = '"';
233409 while( *zIn ){
233410 if( *zIn=='"' ) zQuoted[i++] = '"';
233411 zQuoted[i++] = *zIn++;
233412 }
233413 zQuoted[i++] = '"';
233414 if( p->pSynonym ) zQuoted[i++] = '|';
233415 }
233416 if( pTerm->bPrefix ){
233417 zQuoted[i++] = ' ';
233418 zQuoted[i++] = '*';
233419 }
233420 zQuoted[i++] = '\0';
233421 }
233422 return zQuoted;
233423}
233424
233425static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
233426 char *zNew;
233427 va_list ap;
233428 va_start(ap, zFmt);
233429 zNew = sqlite3_vmprintf(zFmt, ap);
233430 va_end(ap);
233431 if( zApp && zNew ){
233432 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
233433 sqlite3_free(zNew);
233434 zNew = zNew2;
233435 }
233436 sqlite3_free(zApp);
233437 return zNew;
233438}
233439
233440/*
233441** Compose a tcl-readable representation of expression pExpr. Return a
233442** pointer to a buffer containing that representation. It is the
233443** responsibility of the caller to at some point free the buffer using
233444** sqlite3_free().
233445*/
233446static char *fts5ExprPrintTcl(
233447 Fts5Config *pConfig,
233448 const char *zNearsetCmd,
233449 Fts5ExprNode *pExpr
233450){
233451 char *zRet = 0;
233452 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
233453 Fts5ExprNearset *pNear = pExpr->pNear;
233454 int i;
233455 int iTerm;
233456
233457 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
233458 if( zRet==0 ) return 0;
233459 if( pNear->pColset ){
233460 int *aiCol = pNear->pColset->aiCol;
233461 int nCol = pNear->pColset->nCol;
233462 if( nCol==1 ){
233463 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
233464 }else{
233465 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
233466 for(i=1; i<pNear->pColset->nCol; i++){
233467 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
233468 }
233469 zRet = fts5PrintfAppend(zRet, "} ");
233470 }
233471 if( zRet==0 ) return 0;
233472 }
233473
233474 if( pNear->nPhrase>1 ){
233475 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
233476 if( zRet==0 ) return 0;
233477 }
233478
233479 zRet = fts5PrintfAppend(zRet, "--");
233480 if( zRet==0 ) return 0;
233481
233482 for(i=0; i<pNear->nPhrase; i++){
233483 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
233484
233485 zRet = fts5PrintfAppend(zRet, " {");
233486 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
233487 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
233488 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
233489 if( pPhrase->aTerm[iTerm].bPrefix ){
233490 zRet = fts5PrintfAppend(zRet, "*");
233491 }
233492 }
233493
233494 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
233495 if( zRet==0 ) return 0;
233496 }
233497
233498 }else if( pExpr->eType==0 ){
233499 zRet = sqlite3_mprintf("{}");
233500 }else{
233501 char const *zOp = 0;
233502 int i;
233503 switch( pExpr->eType ){
233504 case FTS5_AND: zOp = "AND"; break;
233505 case FTS5_NOT: zOp = "NOT"; break;
233506 default:
233507 assert( pExpr->eType==FTS5_OR );
233508 zOp = "OR";
233509 break;
233510 }
233511
233512 zRet = sqlite3_mprintf("%s", zOp);
233513 for(i=0; zRet && i<pExpr->nChild; i++){
233514 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
233515 if( !z ){
233516 sqlite3_free(zRet);
233517 zRet = 0;
233518 }else{
233519 zRet = fts5PrintfAppend(zRet, " [%z]", z);
233520 }
233521 }
233522 }
233523
233524 return zRet;
233525}
233526
233527static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
233528 char *zRet = 0;
233529 if( pExpr->eType==0 ){
233530 return sqlite3_mprintf("\"\"");
233531 }else
233532 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
233533 Fts5ExprNearset *pNear = pExpr->pNear;
233534 int i;
233535 int iTerm;
233536
233537 if( pNear->pColset ){
233538 int ii;
233539 Fts5Colset *pColset = pNear->pColset;
233540 if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
233541 for(ii=0; ii<pColset->nCol; ii++){
233542 zRet = fts5PrintfAppend(zRet, "%s%s",
233543 pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
233544 );
233545 }
233546 if( zRet ){
233547 zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
233548 }
233549 if( zRet==0 ) return 0;
233550 }
233551
233552 if( pNear->nPhrase>1 ){
233553 zRet = fts5PrintfAppend(zRet, "NEAR(");
233554 if( zRet==0 ) return 0;
233555 }
233556
233557 for(i=0; i<pNear->nPhrase; i++){
233558 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
233559 if( i!=0 ){
233560 zRet = fts5PrintfAppend(zRet, " ");
233561 if( zRet==0 ) return 0;
233562 }
233563 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
233564 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
233565 if( zTerm ){
233566 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
233567 sqlite3_free(zTerm);
233568 }
233569 if( zTerm==0 || zRet==0 ){
233570 sqlite3_free(zRet);
233571 return 0;
233572 }
233573 }
233574 }
233575
233576 if( pNear->nPhrase>1 ){
233577 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
233578 if( zRet==0 ) return 0;
233579 }
233580
233581 }else{
233582 char const *zOp = 0;
233583 int i;
233584
233585 switch( pExpr->eType ){
233586 case FTS5_AND: zOp = " AND "; break;
233587 case FTS5_NOT: zOp = " NOT "; break;
233588 default:
233589 assert( pExpr->eType==FTS5_OR );
233590 zOp = " OR ";
233591 break;
233592 }
233593
233594 for(i=0; i<pExpr->nChild; i++){
233595 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
233596 if( z==0 ){
233597 sqlite3_free(zRet);
233598 zRet = 0;
233599 }else{
233600 int e = pExpr->apChild[i]->eType;
233601 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
233602 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
233603 (i==0 ? "" : zOp),
233604 (b?"(":""), z, (b?")":"")
233605 );
233606 }
233607 if( zRet==0 ) break;
233608 }
233609 }
233610
233611 return zRet;
233612}
233613
233614/*
233615** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
233616** and fts5_expr_tcl() (bTcl!=0).
233617*/
233618static void fts5ExprFunction(
233619 sqlite3_context *pCtx, /* Function call context */
233620 int nArg, /* Number of args */
233621 sqlite3_value **apVal, /* Function arguments */
233622 int bTcl
233623){
233624 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
233625 sqlite3 *db = sqlite3_context_db_handle(pCtx);
233626 const char *zExpr = 0;
233627 char *zErr = 0;
233628 Fts5Expr *pExpr = 0;
233629 int rc;
233630 int i;
233631
233632 const char **azConfig; /* Array of arguments for Fts5Config */
233633 const char *zNearsetCmd = "nearset";
233634 int nConfig; /* Size of azConfig[] */
233635 Fts5Config *pConfig = 0;
233636 int iArg = 1;
233637
233638 if( nArg<1 ){
233639 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
233640 bTcl ? "fts5_expr_tcl" : "fts5_expr"
233641 );
233642 sqlite3_result_error(pCtx, zErr, -1);
233643 sqlite3_free(zErr);
233644 return;
233645 }
233646
233647 if( bTcl && nArg>1 ){
233648 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
233649 iArg = 2;
233650 }
233651
233652 nConfig = 3 + (nArg-iArg);
233653 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
233654 if( azConfig==0 ){
233655 sqlite3_result_error_nomem(pCtx);
233656 return;
233657 }
233658 azConfig[0] = 0;
233659 azConfig[1] = "main";
233660 azConfig[2] = "tbl";
233661 for(i=3; iArg<nArg; iArg++){
233662 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
233663 azConfig[i++] = (z ? z : "");
233664 }
233665
233666 zExpr = (const char*)sqlite3_value_text(apVal[0]);
233667 if( zExpr==0 ) zExpr = "";
233668
233669 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
233670 if( rc==SQLITE_OK ){
233671 rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
233672 }
233673 if( rc==SQLITE_OK ){
233674 char *zText;
233675 if( pExpr->pRoot->xNext==0 ){
233676 zText = sqlite3_mprintf("");
233677 }else if( bTcl ){
233678 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
233679 }else{
233680 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
233681 }
233682 if( zText==0 ){
233683 rc = SQLITE_NOMEM;
233684 }else{
233685 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
233686 sqlite3_free(zText);
233687 }
233688 }
233689
233690 if( rc!=SQLITE_OK ){
233691 if( zErr ){
233692 sqlite3_result_error(pCtx, zErr, -1);
233693 sqlite3_free(zErr);
233694 }else{
233695 sqlite3_result_error_code(pCtx, rc);
233696 }
233697 }
233698 sqlite3_free((void *)azConfig);
233699 sqlite3Fts5ConfigFree(pConfig);
233700 sqlite3Fts5ExprFree(pExpr);
233701}
233702
233703static void fts5ExprFunctionHr(
233704 sqlite3_context *pCtx, /* Function call context */
233705 int nArg, /* Number of args */
233706 sqlite3_value **apVal /* Function arguments */
233707){
233708 fts5ExprFunction(pCtx, nArg, apVal, 0);
233709}
233710static void fts5ExprFunctionTcl(
233711 sqlite3_context *pCtx, /* Function call context */
233712 int nArg, /* Number of args */
233713 sqlite3_value **apVal /* Function arguments */
233714){
233715 fts5ExprFunction(pCtx, nArg, apVal, 1);
233716}
233717
233718/*
233719** The implementation of an SQLite user-defined-function that accepts a
233720** single integer as an argument. If the integer is an alpha-numeric
233721** unicode code point, 1 is returned. Otherwise 0.
233722*/
233723static void fts5ExprIsAlnum(
233724 sqlite3_context *pCtx, /* Function call context */
233725 int nArg, /* Number of args */
233726 sqlite3_value **apVal /* Function arguments */
233727){
233728 int iCode;
233729 u8 aArr[32];
233730 if( nArg!=1 ){
233731 sqlite3_result_error(pCtx,
233732 "wrong number of arguments to function fts5_isalnum", -1
233733 );
233734 return;
233735 }
233736 memset(aArr, 0, sizeof(aArr));
233737 sqlite3Fts5UnicodeCatParse("L*", aArr);
233738 sqlite3Fts5UnicodeCatParse("N*", aArr);
233739 sqlite3Fts5UnicodeCatParse("Co", aArr);
233740 iCode = sqlite3_value_int(apVal[0]);
233741 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
233742}
233743
233744static void fts5ExprFold(
233745 sqlite3_context *pCtx, /* Function call context */
233746 int nArg, /* Number of args */
233747 sqlite3_value **apVal /* Function arguments */
233748){
233749 if( nArg!=1 && nArg!=2 ){
233750 sqlite3_result_error(pCtx,
233751 "wrong number of arguments to function fts5_fold", -1
233752 );
233753 }else{
233754 int iCode;
233755 int bRemoveDiacritics = 0;
233756 iCode = sqlite3_value_int(apVal[0]);
233757 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
233758 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
233759 }
233760}
233761#endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */
233762
233763/*
233764** This is called during initialization to register the fts5_expr() scalar
233765** UDF with the SQLite handle passed as the only argument.
233766*/
233767static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
233768#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
233769 struct Fts5ExprFunc {
233770 const char *z;
233771 void (*x)(sqlite3_context*,int,sqlite3_value**);
233772 } aFunc[] = {
233773 { "fts5_expr", fts5ExprFunctionHr },
233774 { "fts5_expr_tcl", fts5ExprFunctionTcl },
233775 { "fts5_isalnum", fts5ExprIsAlnum },
233776 { "fts5_fold", fts5ExprFold },
233777 };
233778 int i;
233779 int rc = SQLITE_OK;
233780 void *pCtx = (void*)pGlobal;
233781
233782 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
233783 struct Fts5ExprFunc *p = &aFunc[i];
233784 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
233785 }
233786#else
233787 int rc = SQLITE_OK;
233788 UNUSED_PARAM2(pGlobal,db);
233789#endif
233790
233791 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
233792 ** sqlite3Fts5ParserFallback() are unused */
233793#ifndef NDEBUG
233794 (void)sqlite3Fts5ParserTrace;
233795#endif
233796 (void)sqlite3Fts5ParserFallback;
233797
233798 return rc;
233799}
233800
233801/*
233802** Return the number of phrases in expression pExpr.
233803*/
233804static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
233805 return (pExpr ? pExpr->nPhrase : 0);
233806}
233807
233808/*
233809** Return the number of terms in the iPhrase'th phrase in pExpr.
233810*/
233811static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
233812 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
233813 return pExpr->apExprPhrase[iPhrase]->nTerm;
233814}
233815
233816/*
233817** This function is used to access the current position list for phrase
233818** iPhrase.
233819*/
233820static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
233821 int nRet;
233822 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
233823 Fts5ExprNode *pNode = pPhrase->pNode;
233824 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
233825 *pa = pPhrase->poslist.p;
233826 nRet = pPhrase->poslist.n;
233827 }else{
233828 *pa = 0;
233829 nRet = 0;
233830 }
233831 return nRet;
233832}
233833
233834struct Fts5PoslistPopulator {
233835 Fts5PoslistWriter writer;
233836 int bOk; /* True if ok to populate */
233837 int bMiss;
233838};
233839
233840/*
233841** Clear the position lists associated with all phrases in the expression
233842** passed as the first argument. Argument bLive is true if the expression
233843** might be pointing to a real entry, otherwise it has just been reset.
233844**
233845** At present this function is only used for detail=col and detail=none
233846** fts5 tables. This implies that all phrases must be at most 1 token
233847** in size, as phrase matches are not supported without detail=full.
233848*/
233849static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
233850 Fts5PoslistPopulator *pRet;
233851 pRet = sqlite3_malloc64(n: sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
233852 if( pRet ){
233853 int i;
233854 memset(s: pRet, c: 0, n: sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
233855 for(i=0; i<pExpr->nPhrase; i++){
233856 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
233857 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
233858 assert( pExpr->apExprPhrase[i]->nTerm<=1 );
233859 if( bLive &&
233860 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
233861 ){
233862 pRet[i].bMiss = 1;
233863 }else{
233864 pBuf->n = 0;
233865 }
233866 }
233867 }
233868 return pRet;
233869}
233870
233871struct Fts5ExprCtx {
233872 Fts5Expr *pExpr;
233873 Fts5PoslistPopulator *aPopulator;
233874 i64 iOff;
233875};
233876typedef struct Fts5ExprCtx Fts5ExprCtx;
233877
233878/*
233879** TODO: Make this more efficient!
233880*/
233881static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
233882 int i;
233883 for(i=0; i<pColset->nCol; i++){
233884 if( pColset->aiCol[i]==iCol ) return 1;
233885 }
233886 return 0;
233887}
233888
233889static int fts5ExprPopulatePoslistsCb(
233890 void *pCtx, /* Copy of 2nd argument to xTokenize() */
233891 int tflags, /* Mask of FTS5_TOKEN_* flags */
233892 const char *pToken, /* Pointer to buffer containing token */
233893 int nToken, /* Size of token in bytes */
233894 int iUnused1, /* Byte offset of token within input text */
233895 int iUnused2 /* Byte offset of end of token within input text */
233896){
233897 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
233898 Fts5Expr *pExpr = p->pExpr;
233899 int i;
233900
233901 UNUSED_PARAM2(iUnused1, iUnused2);
233902
233903 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
233904 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
233905 for(i=0; i<pExpr->nPhrase; i++){
233906 Fts5ExprTerm *pTerm;
233907 if( p->aPopulator[i].bOk==0 ) continue;
233908 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
233909 int nTerm = (int)strlen(s: pTerm->zTerm);
233910 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
233911 && memcmp(s1: pTerm->zTerm, s2: pToken, n: nTerm)==0
233912 ){
233913 int rc = sqlite3Fts5PoslistWriterAppend(
233914 pBuf: &pExpr->apExprPhrase[i]->poslist, pWriter: &p->aPopulator[i].writer, iPos: p->iOff
233915 );
233916 if( rc ) return rc;
233917 break;
233918 }
233919 }
233920 }
233921 return SQLITE_OK;
233922}
233923
233924static int sqlite3Fts5ExprPopulatePoslists(
233925 Fts5Config *pConfig,
233926 Fts5Expr *pExpr,
233927 Fts5PoslistPopulator *aPopulator,
233928 int iCol,
233929 const char *z, int n
233930){
233931 int i;
233932 Fts5ExprCtx sCtx;
233933 sCtx.pExpr = pExpr;
233934 sCtx.aPopulator = aPopulator;
233935 sCtx.iOff = (((i64)iCol) << 32) - 1;
233936
233937 for(i=0; i<pExpr->nPhrase; i++){
233938 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
233939 Fts5Colset *pColset = pNode->pNear->pColset;
233940 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
233941 || aPopulator[i].bMiss
233942 ){
233943 aPopulator[i].bOk = 0;
233944 }else{
233945 aPopulator[i].bOk = 1;
233946 }
233947 }
233948
233949 return sqlite3Fts5Tokenize(pConfig,
233950 FTS5_TOKENIZE_DOCUMENT, pText: z, nText: n, pCtx: (void*)&sCtx, xToken: fts5ExprPopulatePoslistsCb
233951 );
233952}
233953
233954static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
233955 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
233956 pNode->pNear->apPhrase[0]->poslist.n = 0;
233957 }else{
233958 int i;
233959 for(i=0; i<pNode->nChild; i++){
233960 fts5ExprClearPoslists(pNode: pNode->apChild[i]);
233961 }
233962 }
233963}
233964
233965static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
233966 pNode->iRowid = iRowid;
233967 pNode->bEof = 0;
233968 switch( pNode->eType ){
233969 case FTS5_TERM:
233970 case FTS5_STRING:
233971 return (pNode->pNear->apPhrase[0]->poslist.n>0);
233972
233973 case FTS5_AND: {
233974 int i;
233975 for(i=0; i<pNode->nChild; i++){
233976 if( fts5ExprCheckPoslists(pNode: pNode->apChild[i], iRowid)==0 ){
233977 fts5ExprClearPoslists(pNode);
233978 return 0;
233979 }
233980 }
233981 break;
233982 }
233983
233984 case FTS5_OR: {
233985 int i;
233986 int bRet = 0;
233987 for(i=0; i<pNode->nChild; i++){
233988 if( fts5ExprCheckPoslists(pNode: pNode->apChild[i], iRowid) ){
233989 bRet = 1;
233990 }
233991 }
233992 return bRet;
233993 }
233994
233995 default: {
233996 assert( pNode->eType==FTS5_NOT );
233997 if( 0==fts5ExprCheckPoslists(pNode: pNode->apChild[0], iRowid)
233998 || 0!=fts5ExprCheckPoslists(pNode: pNode->apChild[1], iRowid)
233999 ){
234000 fts5ExprClearPoslists(pNode);
234001 return 0;
234002 }
234003 break;
234004 }
234005 }
234006 return 1;
234007}
234008
234009static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
234010 fts5ExprCheckPoslists(pNode: pExpr->pRoot, iRowid);
234011}
234012
234013/*
234014** This function is only called for detail=columns tables.
234015*/
234016static int sqlite3Fts5ExprPhraseCollist(
234017 Fts5Expr *pExpr,
234018 int iPhrase,
234019 const u8 **ppCollist,
234020 int *pnCollist
234021){
234022 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
234023 Fts5ExprNode *pNode = pPhrase->pNode;
234024 int rc = SQLITE_OK;
234025
234026 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
234027 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
234028
234029 if( pNode->bEof==0
234030 && pNode->iRowid==pExpr->pRoot->iRowid
234031 && pPhrase->poslist.n>0
234032 ){
234033 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
234034 if( pTerm->pSynonym ){
234035 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
234036 rc = fts5ExprSynonymList(
234037 pTerm, iRowid: pNode->iRowid, pBuf, pa: (u8**)ppCollist, pn: pnCollist
234038 );
234039 }else{
234040 *ppCollist = pPhrase->aTerm[0].pIter->pData;
234041 *pnCollist = pPhrase->aTerm[0].pIter->nData;
234042 }
234043 }else{
234044 *ppCollist = 0;
234045 *pnCollist = 0;
234046 }
234047
234048 return rc;
234049}
234050
234051/*
234052** 2014 August 11
234053**
234054** The author disclaims copyright to this source code. In place of
234055** a legal notice, here is a blessing:
234056**
234057** May you do good and not evil.
234058** May you find forgiveness for yourself and forgive others.
234059** May you share freely, never taking more than you give.
234060**
234061******************************************************************************
234062**
234063*/
234064
234065
234066
234067/* #include "fts5Int.h" */
234068
234069typedef struct Fts5HashEntry Fts5HashEntry;
234070
234071/*
234072** This file contains the implementation of an in-memory hash table used
234073** to accumuluate "term -> doclist" content before it is flused to a level-0
234074** segment.
234075*/
234076
234077
234078struct Fts5Hash {
234079 int eDetail; /* Copy of Fts5Config.eDetail */
234080 int *pnByte; /* Pointer to bytes counter */
234081 int nEntry; /* Number of entries currently in hash */
234082 int nSlot; /* Size of aSlot[] array */
234083 Fts5HashEntry *pScan; /* Current ordered scan item */
234084 Fts5HashEntry **aSlot; /* Array of hash slots */
234085};
234086
234087/*
234088** Each entry in the hash table is represented by an object of the
234089** following type. Each object, its key (a nul-terminated string) and
234090** its current data are stored in a single memory allocation. The
234091** key immediately follows the object in memory. The position list
234092** data immediately follows the key data in memory.
234093**
234094** The data that follows the key is in a similar, but not identical format
234095** to the doclist data stored in the database. It is:
234096**
234097** * Rowid, as a varint
234098** * Position list, without 0x00 terminator.
234099** * Size of previous position list and rowid, as a 4 byte
234100** big-endian integer.
234101**
234102** iRowidOff:
234103** Offset of last rowid written to data area. Relative to first byte of
234104** structure.
234105**
234106** nData:
234107** Bytes of data written since iRowidOff.
234108*/
234109struct Fts5HashEntry {
234110 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
234111 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
234112
234113 int nAlloc; /* Total size of allocation */
234114 int iSzPoslist; /* Offset of space for 4-byte poslist size */
234115 int nData; /* Total bytes of data (incl. structure) */
234116 int nKey; /* Length of key in bytes */
234117 u8 bDel; /* Set delete-flag @ iSzPoslist */
234118 u8 bContent; /* Set content-flag (detail=none mode) */
234119 i16 iCol; /* Column of last value written */
234120 int iPos; /* Position of last value written */
234121 i64 iRowid; /* Rowid of last value written */
234122};
234123
234124/*
234125** Eqivalent to:
234126**
234127** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
234128*/
234129#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
234130
234131
234132/*
234133** Allocate a new hash table.
234134*/
234135static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
234136 int rc = SQLITE_OK;
234137 Fts5Hash *pNew;
234138
234139 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(n: sizeof(Fts5Hash));
234140 if( pNew==0 ){
234141 rc = SQLITE_NOMEM;
234142 }else{
234143 sqlite3_int64 nByte;
234144 memset(s: pNew, c: 0, n: sizeof(Fts5Hash));
234145 pNew->pnByte = pnByte;
234146 pNew->eDetail = pConfig->eDetail;
234147
234148 pNew->nSlot = 1024;
234149 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
234150 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(n: nByte);
234151 if( pNew->aSlot==0 ){
234152 sqlite3_free(p: pNew);
234153 *ppNew = 0;
234154 rc = SQLITE_NOMEM;
234155 }else{
234156 memset(s: pNew->aSlot, c: 0, n: (size_t)nByte);
234157 }
234158 }
234159 return rc;
234160}
234161
234162/*
234163** Free a hash table object.
234164*/
234165static void sqlite3Fts5HashFree(Fts5Hash *pHash){
234166 if( pHash ){
234167 sqlite3Fts5HashClear(pHash);
234168 sqlite3_free(p: pHash->aSlot);
234169 sqlite3_free(p: pHash);
234170 }
234171}
234172
234173/*
234174** Empty (but do not delete) a hash table.
234175*/
234176static void sqlite3Fts5HashClear(Fts5Hash *pHash){
234177 int i;
234178 for(i=0; i<pHash->nSlot; i++){
234179 Fts5HashEntry *pNext;
234180 Fts5HashEntry *pSlot;
234181 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
234182 pNext = pSlot->pHashNext;
234183 sqlite3_free(p: pSlot);
234184 }
234185 }
234186 memset(s: pHash->aSlot, c: 0, n: pHash->nSlot * sizeof(Fts5HashEntry*));
234187 pHash->nEntry = 0;
234188}
234189
234190static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
234191 int i;
234192 unsigned int h = 13;
234193 for(i=n-1; i>=0; i--){
234194 h = (h << 3) ^ h ^ p[i];
234195 }
234196 return (h % nSlot);
234197}
234198
234199static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
234200 int i;
234201 unsigned int h = 13;
234202 for(i=n-1; i>=0; i--){
234203 h = (h << 3) ^ h ^ p[i];
234204 }
234205 h = (h << 3) ^ h ^ b;
234206 return (h % nSlot);
234207}
234208
234209/*
234210** Resize the hash table by doubling the number of slots.
234211*/
234212static int fts5HashResize(Fts5Hash *pHash){
234213 int nNew = pHash->nSlot*2;
234214 int i;
234215 Fts5HashEntry **apNew;
234216 Fts5HashEntry **apOld = pHash->aSlot;
234217
234218 apNew = (Fts5HashEntry**)sqlite3_malloc64(n: nNew*sizeof(Fts5HashEntry*));
234219 if( !apNew ) return SQLITE_NOMEM;
234220 memset(s: apNew, c: 0, n: nNew*sizeof(Fts5HashEntry*));
234221
234222 for(i=0; i<pHash->nSlot; i++){
234223 while( apOld[i] ){
234224 unsigned int iHash;
234225 Fts5HashEntry *p = apOld[i];
234226 apOld[i] = p->pHashNext;
234227 iHash = fts5HashKey(nSlot: nNew, p: (u8*)fts5EntryKey(p),
234228 n: (int)strlen(fts5EntryKey(p)));
234229 p->pHashNext = apNew[iHash];
234230 apNew[iHash] = p;
234231 }
234232 }
234233
234234 sqlite3_free(p: apOld);
234235 pHash->nSlot = nNew;
234236 pHash->aSlot = apNew;
234237 return SQLITE_OK;
234238}
234239
234240static int fts5HashAddPoslistSize(
234241 Fts5Hash *pHash,
234242 Fts5HashEntry *p,
234243 Fts5HashEntry *p2
234244){
234245 int nRet = 0;
234246 if( p->iSzPoslist ){
234247 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
234248 int nData = p->nData;
234249 if( pHash->eDetail==FTS5_DETAIL_NONE ){
234250 assert( nData==p->iSzPoslist );
234251 if( p->bDel ){
234252 pPtr[nData++] = 0x00;
234253 if( p->bContent ){
234254 pPtr[nData++] = 0x00;
234255 }
234256 }
234257 }else{
234258 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
234259 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
234260
234261 assert( p->bDel==0 || p->bDel==1 );
234262 if( nPos<=127 ){
234263 pPtr[p->iSzPoslist] = (u8)nPos;
234264 }else{
234265 int nByte = sqlite3Fts5GetVarintLen(iVal: (u32)nPos);
234266 memmove(dest: &pPtr[p->iSzPoslist + nByte], src: &pPtr[p->iSzPoslist + 1], n: nSz);
234267 sqlite3Fts5PutVarint(p: &pPtr[p->iSzPoslist], v: nPos);
234268 nData += (nByte-1);
234269 }
234270 }
234271
234272 nRet = nData - p->nData;
234273 if( p2==0 ){
234274 p->iSzPoslist = 0;
234275 p->bDel = 0;
234276 p->bContent = 0;
234277 p->nData = nData;
234278 }
234279 }
234280 return nRet;
234281}
234282
234283/*
234284** Add an entry to the in-memory hash table. The key is the concatenation
234285** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
234286**
234287** (bByte || pToken) -> (iRowid,iCol,iPos)
234288**
234289** Or, if iCol is negative, then the value is a delete marker.
234290*/
234291static int sqlite3Fts5HashWrite(
234292 Fts5Hash *pHash,
234293 i64 iRowid, /* Rowid for this entry */
234294 int iCol, /* Column token appears in (-ve -> delete) */
234295 int iPos, /* Position of token within column */
234296 char bByte, /* First byte of token */
234297 const char *pToken, int nToken /* Token to add or remove to or from index */
234298){
234299 unsigned int iHash;
234300 Fts5HashEntry *p;
234301 u8 *pPtr;
234302 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
234303 int bNew; /* If non-delete entry should be written */
234304
234305 bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
234306
234307 /* Attempt to locate an existing hash entry */
234308 iHash = fts5HashKey2(nSlot: pHash->nSlot, b: (u8)bByte, p: (const u8*)pToken, n: nToken);
234309 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
234310 char *zKey = fts5EntryKey(p);
234311 if( zKey[0]==bByte
234312 && p->nKey==nToken
234313 && memcmp(s1: &zKey[1], s2: pToken, n: nToken)==0
234314 ){
234315 break;
234316 }
234317 }
234318
234319 /* If an existing hash entry cannot be found, create a new one. */
234320 if( p==0 ){
234321 /* Figure out how much space to allocate */
234322 char *zKey;
234323 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
234324 if( nByte<128 ) nByte = 128;
234325
234326 /* Grow the Fts5Hash.aSlot[] array if necessary. */
234327 if( (pHash->nEntry*2)>=pHash->nSlot ){
234328 int rc = fts5HashResize(pHash);
234329 if( rc!=SQLITE_OK ) return rc;
234330 iHash = fts5HashKey2(nSlot: pHash->nSlot, b: (u8)bByte, p: (const u8*)pToken, n: nToken);
234331 }
234332
234333 /* Allocate new Fts5HashEntry and add it to the hash table. */
234334 p = (Fts5HashEntry*)sqlite3_malloc64(n: nByte);
234335 if( !p ) return SQLITE_NOMEM;
234336 memset(s: p, c: 0, n: sizeof(Fts5HashEntry));
234337 p->nAlloc = (int)nByte;
234338 zKey = fts5EntryKey(p);
234339 zKey[0] = bByte;
234340 memcpy(dest: &zKey[1], src: pToken, n: nToken);
234341 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
234342 p->nKey = nToken;
234343 zKey[nToken+1] = '\0';
234344 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
234345 p->pHashNext = pHash->aSlot[iHash];
234346 pHash->aSlot[iHash] = p;
234347 pHash->nEntry++;
234348
234349 /* Add the first rowid field to the hash-entry */
234350 p->nData += sqlite3Fts5PutVarint(p: &((u8*)p)[p->nData], v: iRowid);
234351 p->iRowid = iRowid;
234352
234353 p->iSzPoslist = p->nData;
234354 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
234355 p->nData += 1;
234356 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
234357 }
234358
234359 }else{
234360
234361 /* Appending to an existing hash-entry. Check that there is enough
234362 ** space to append the largest possible new entry. Worst case scenario
234363 ** is:
234364 **
234365 ** + 9 bytes for a new rowid,
234366 ** + 4 byte reserved for the "poslist size" varint.
234367 ** + 1 byte for a "new column" byte,
234368 ** + 3 bytes for a new column number (16-bit max) as a varint,
234369 ** + 5 bytes for the new position offset (32-bit max).
234370 */
234371 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
234372 sqlite3_int64 nNew = p->nAlloc * 2;
234373 Fts5HashEntry *pNew;
234374 Fts5HashEntry **pp;
234375 pNew = (Fts5HashEntry*)sqlite3_realloc64(pOld: p, n: nNew);
234376 if( pNew==0 ) return SQLITE_NOMEM;
234377 pNew->nAlloc = (int)nNew;
234378 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
234379 *pp = pNew;
234380 p = pNew;
234381 }
234382 nIncr -= p->nData;
234383 }
234384 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
234385
234386 pPtr = (u8*)p;
234387
234388 /* If this is a new rowid, append the 4-byte size field for the previous
234389 ** entry, and the new rowid for this entry. */
234390 if( iRowid!=p->iRowid ){
234391 u64 iDiff = (u64)iRowid - (u64)p->iRowid;
234392 fts5HashAddPoslistSize(pHash, p, p2: 0);
234393 p->nData += sqlite3Fts5PutVarint(p: &pPtr[p->nData], v: iDiff);
234394 p->iRowid = iRowid;
234395 bNew = 1;
234396 p->iSzPoslist = p->nData;
234397 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
234398 p->nData += 1;
234399 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
234400 p->iPos = 0;
234401 }
234402 }
234403
234404 if( iCol>=0 ){
234405 if( pHash->eDetail==FTS5_DETAIL_NONE ){
234406 p->bContent = 1;
234407 }else{
234408 /* Append a new column value, if necessary */
234409 assert_nc( iCol>=p->iCol );
234410 if( iCol!=p->iCol ){
234411 if( pHash->eDetail==FTS5_DETAIL_FULL ){
234412 pPtr[p->nData++] = 0x01;
234413 p->nData += sqlite3Fts5PutVarint(p: &pPtr[p->nData], v: iCol);
234414 p->iCol = (i16)iCol;
234415 p->iPos = 0;
234416 }else{
234417 bNew = 1;
234418 p->iCol = (i16)(iPos = iCol);
234419 }
234420 }
234421
234422 /* Append the new position offset, if necessary */
234423 if( bNew ){
234424 p->nData += sqlite3Fts5PutVarint(p: &pPtr[p->nData], v: iPos - p->iPos + 2);
234425 p->iPos = iPos;
234426 }
234427 }
234428 }else{
234429 /* This is a delete. Set the delete flag. */
234430 p->bDel = 1;
234431 }
234432
234433 nIncr += p->nData;
234434 *pHash->pnByte += nIncr;
234435 return SQLITE_OK;
234436}
234437
234438
234439/*
234440** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
234441** each sorted in key order. This function merges the two lists into a
234442** single list and returns a pointer to its first element.
234443*/
234444static Fts5HashEntry *fts5HashEntryMerge(
234445 Fts5HashEntry *pLeft,
234446 Fts5HashEntry *pRight
234447){
234448 Fts5HashEntry *p1 = pLeft;
234449 Fts5HashEntry *p2 = pRight;
234450 Fts5HashEntry *pRet = 0;
234451 Fts5HashEntry **ppOut = &pRet;
234452
234453 while( p1 || p2 ){
234454 if( p1==0 ){
234455 *ppOut = p2;
234456 p2 = 0;
234457 }else if( p2==0 ){
234458 *ppOut = p1;
234459 p1 = 0;
234460 }else{
234461 int i = 0;
234462 char *zKey1 = fts5EntryKey(p1);
234463 char *zKey2 = fts5EntryKey(p2);
234464 while( zKey1[i]==zKey2[i] ) i++;
234465
234466 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
234467 /* p2 is smaller */
234468 *ppOut = p2;
234469 ppOut = &p2->pScanNext;
234470 p2 = p2->pScanNext;
234471 }else{
234472 /* p1 is smaller */
234473 *ppOut = p1;
234474 ppOut = &p1->pScanNext;
234475 p1 = p1->pScanNext;
234476 }
234477 *ppOut = 0;
234478 }
234479 }
234480
234481 return pRet;
234482}
234483
234484/*
234485** Extract all tokens from hash table iHash and link them into a list
234486** in sorted order. The hash table is cleared before returning. It is
234487** the responsibility of the caller to free the elements of the returned
234488** list.
234489*/
234490static int fts5HashEntrySort(
234491 Fts5Hash *pHash,
234492 const char *pTerm, int nTerm, /* Query prefix, if any */
234493 Fts5HashEntry **ppSorted
234494){
234495 const int nMergeSlot = 32;
234496 Fts5HashEntry **ap;
234497 Fts5HashEntry *pList;
234498 int iSlot;
234499 int i;
234500
234501 *ppSorted = 0;
234502 ap = sqlite3_malloc64(n: sizeof(Fts5HashEntry*) * nMergeSlot);
234503 if( !ap ) return SQLITE_NOMEM;
234504 memset(s: ap, c: 0, n: sizeof(Fts5HashEntry*) * nMergeSlot);
234505
234506 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
234507 Fts5HashEntry *pIter;
234508 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
234509 if( pTerm==0
234510 || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter), s2: pTerm, n: nTerm))
234511 ){
234512 Fts5HashEntry *pEntry = pIter;
234513 pEntry->pScanNext = 0;
234514 for(i=0; ap[i]; i++){
234515 pEntry = fts5HashEntryMerge(pLeft: pEntry, pRight: ap[i]);
234516 ap[i] = 0;
234517 }
234518 ap[i] = pEntry;
234519 }
234520 }
234521 }
234522
234523 pList = 0;
234524 for(i=0; i<nMergeSlot; i++){
234525 pList = fts5HashEntryMerge(pLeft: pList, pRight: ap[i]);
234526 }
234527
234528 sqlite3_free(p: ap);
234529 *ppSorted = pList;
234530 return SQLITE_OK;
234531}
234532
234533/*
234534** Query the hash table for a doclist associated with term pTerm/nTerm.
234535*/
234536static int sqlite3Fts5HashQuery(
234537 Fts5Hash *pHash, /* Hash table to query */
234538 int nPre,
234539 const char *pTerm, int nTerm, /* Query term */
234540 void **ppOut, /* OUT: Pointer to new object */
234541 int *pnDoclist /* OUT: Size of doclist in bytes */
234542){
234543 unsigned int iHash = fts5HashKey(nSlot: pHash->nSlot, p: (const u8*)pTerm, n: nTerm);
234544 char *zKey = 0;
234545 Fts5HashEntry *p;
234546
234547 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
234548 zKey = fts5EntryKey(p);
234549 assert( p->nKey+1==(int)strlen(zKey) );
234550 if( nTerm==p->nKey+1 && memcmp(s1: zKey, s2: pTerm, n: nTerm)==0 ) break;
234551 }
234552
234553 if( p ){
234554 int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
234555 int nList = p->nData - nHashPre;
234556 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(n: nPre + nList + 10));
234557 if( pRet ){
234558 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
234559 memcpy(dest: &pRet[nPre], src: &((u8*)p)[nHashPre], n: nList);
234560 nList += fts5HashAddPoslistSize(pHash, p, p2: pFaux);
234561 *pnDoclist = nList;
234562 }else{
234563 *pnDoclist = 0;
234564 return SQLITE_NOMEM;
234565 }
234566 }else{
234567 *ppOut = 0;
234568 *pnDoclist = 0;
234569 }
234570
234571 return SQLITE_OK;
234572}
234573
234574static int sqlite3Fts5HashScanInit(
234575 Fts5Hash *p, /* Hash table to query */
234576 const char *pTerm, int nTerm /* Query prefix */
234577){
234578 return fts5HashEntrySort(pHash: p, pTerm, nTerm, ppSorted: &p->pScan);
234579}
234580
234581#ifdef SQLITE_DEBUG
234582static int fts5HashCount(Fts5Hash *pHash){
234583 int nEntry = 0;
234584 int ii;
234585 for(ii=0; ii<pHash->nSlot; ii++){
234586 Fts5HashEntry *p = 0;
234587 for(p=pHash->aSlot[ii]; p; p=p->pHashNext){
234588 nEntry++;
234589 }
234590 }
234591 return nEntry;
234592}
234593#endif
234594
234595/*
234596** Return true if the hash table is empty, false otherwise.
234597*/
234598static int sqlite3Fts5HashIsEmpty(Fts5Hash *pHash){
234599 assert( pHash->nEntry==fts5HashCount(pHash) );
234600 return pHash->nEntry==0;
234601}
234602
234603static void sqlite3Fts5HashScanNext(Fts5Hash *p){
234604 assert( !sqlite3Fts5HashScanEof(p) );
234605 p->pScan = p->pScan->pScanNext;
234606}
234607
234608static int sqlite3Fts5HashScanEof(Fts5Hash *p){
234609 return (p->pScan==0);
234610}
234611
234612static void sqlite3Fts5HashScanEntry(
234613 Fts5Hash *pHash,
234614 const char **pzTerm, /* OUT: term (nul-terminated) */
234615 const u8 **ppDoclist, /* OUT: pointer to doclist */
234616 int *pnDoclist /* OUT: size of doclist in bytes */
234617){
234618 Fts5HashEntry *p;
234619 if( (p = pHash->pScan) ){
234620 char *zKey = fts5EntryKey(p);
234621 int nTerm = (int)strlen(s: zKey);
234622 fts5HashAddPoslistSize(pHash, p, p2: 0);
234623 *pzTerm = zKey;
234624 *ppDoclist = (const u8*)&zKey[nTerm+1];
234625 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
234626 }else{
234627 *pzTerm = 0;
234628 *ppDoclist = 0;
234629 *pnDoclist = 0;
234630 }
234631}
234632
234633/*
234634** 2014 May 31
234635**
234636** The author disclaims copyright to this source code. In place of
234637** a legal notice, here is a blessing:
234638**
234639** May you do good and not evil.
234640** May you find forgiveness for yourself and forgive others.
234641** May you share freely, never taking more than you give.
234642**
234643******************************************************************************
234644**
234645** Low level access to the FTS index stored in the database file. The
234646** routines in this file file implement all read and write access to the
234647** %_data table. Other parts of the system access this functionality via
234648** the interface defined in fts5Int.h.
234649*/
234650
234651
234652/* #include "fts5Int.h" */
234653
234654/*
234655** Overview:
234656**
234657** The %_data table contains all the FTS indexes for an FTS5 virtual table.
234658** As well as the main term index, there may be up to 31 prefix indexes.
234659** The format is similar to FTS3/4, except that:
234660**
234661** * all segment b-tree leaf data is stored in fixed size page records
234662** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
234663** taken to ensure it is possible to iterate in either direction through
234664** the entries in a doclist, or to seek to a specific entry within a
234665** doclist, without loading it into memory.
234666**
234667** * large doclists that span many pages have associated "doclist index"
234668** records that contain a copy of the first rowid on each page spanned by
234669** the doclist. This is used to speed up seek operations, and merges of
234670** large doclists with very small doclists.
234671**
234672** * extra fields in the "structure record" record the state of ongoing
234673** incremental merge operations.
234674**
234675*/
234676
234677
234678#define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
234679#define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
234680
234681#define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
234682
234683#define FTS5_MAIN_PREFIX '0'
234684
234685#if FTS5_MAX_PREFIX_INDEXES > 31
234686# error "FTS5_MAX_PREFIX_INDEXES is too large"
234687#endif
234688
234689#define FTS5_MAX_LEVEL 64
234690
234691/*
234692** There are two versions of the format used for the structure record:
234693**
234694** 1. the legacy format, that may be read by all fts5 versions, and
234695**
234696** 2. the V2 format, which is used by contentless_delete=1 databases.
234697**
234698** Both begin with a 4-byte "configuration cookie" value. Then, a legacy
234699** format structure record contains a varint - the number of levels in
234700** the structure. Whereas a V2 structure record contains the constant
234701** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a
234702** varint has to be at least 16256 to begin with "0xFF". And the default
234703** maximum number of levels is 64.
234704**
234705** See below for more on structure record formats.
234706*/
234707#define FTS5_STRUCTURE_V2 "\xFF\x00\x00\x01"
234708
234709/*
234710** Details:
234711**
234712** The %_data table managed by this module,
234713**
234714** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
234715**
234716** , contains the following 6 types of records. See the comments surrounding
234717** the FTS5_*_ROWID macros below for a description of how %_data rowids are
234718** assigned to each fo them.
234719**
234720** 1. Structure Records:
234721**
234722** The set of segments that make up an index - the index structure - are
234723** recorded in a single record within the %_data table. The record consists
234724** of a single 32-bit configuration cookie value followed by a list of
234725** SQLite varints.
234726**
234727** If the structure record is a V2 record, the configuration cookie is
234728** followed by the following 4 bytes: [0xFF 0x00 0x00 0x01].
234729**
234730** Next, the record continues with three varints:
234731**
234732** + number of levels,
234733** + total number of segments on all levels,
234734** + value of write counter.
234735**
234736** Then, for each level from 0 to nMax:
234737**
234738** + number of input segments in ongoing merge.
234739** + total number of segments in level.
234740** + for each segment from oldest to newest:
234741** + segment id (always > 0)
234742** + first leaf page number (often 1, always greater than 0)
234743** + final leaf page number
234744**
234745** Then, for V2 structures only:
234746**
234747** + lower origin counter value,
234748** + upper origin counter value,
234749** + the number of tombstone hash pages.
234750**
234751** 2. The Averages Record:
234752**
234753** A single record within the %_data table. The data is a list of varints.
234754** The first value is the number of rows in the index. Then, for each column
234755** from left to right, the total number of tokens in the column for all
234756** rows of the table.
234757**
234758** 3. Segment leaves:
234759**
234760** TERM/DOCLIST FORMAT:
234761**
234762** Most of each segment leaf is taken up by term/doclist data. The
234763** general format of term/doclist, starting with the first term
234764** on the leaf page, is:
234765**
234766** varint : size of first term
234767** blob: first term data
234768** doclist: first doclist
234769** zero-or-more {
234770** varint: number of bytes in common with previous term
234771** varint: number of bytes of new term data (nNew)
234772** blob: nNew bytes of new term data
234773** doclist: next doclist
234774** }
234775**
234776** doclist format:
234777**
234778** varint: first rowid
234779** poslist: first poslist
234780** zero-or-more {
234781** varint: rowid delta (always > 0)
234782** poslist: next poslist
234783** }
234784**
234785** poslist format:
234786**
234787** varint: size of poslist in bytes multiplied by 2, not including
234788** this field. Plus 1 if this entry carries the "delete" flag.
234789** collist: collist for column 0
234790** zero-or-more {
234791** 0x01 byte
234792** varint: column number (I)
234793** collist: collist for column I
234794** }
234795**
234796** collist format:
234797**
234798** varint: first offset + 2
234799** zero-or-more {
234800** varint: offset delta + 2
234801** }
234802**
234803** PAGE FORMAT
234804**
234805** Each leaf page begins with a 4-byte header containing 2 16-bit
234806** unsigned integer fields in big-endian format. They are:
234807**
234808** * The byte offset of the first rowid on the page, if it exists
234809** and occurs before the first term (otherwise 0).
234810**
234811** * The byte offset of the start of the page footer. If the page
234812** footer is 0 bytes in size, then this field is the same as the
234813** size of the leaf page in bytes.
234814**
234815** The page footer consists of a single varint for each term located
234816** on the page. Each varint is the byte offset of the current term
234817** within the page, delta-compressed against the previous value. In
234818** other words, the first varint in the footer is the byte offset of
234819** the first term, the second is the byte offset of the second less that
234820** of the first, and so on.
234821**
234822** The term/doclist format described above is accurate if the entire
234823** term/doclist data fits on a single leaf page. If this is not the case,
234824** the format is changed in two ways:
234825**
234826** + if the first rowid on a page occurs before the first term, it
234827** is stored as a literal value:
234828**
234829** varint: first rowid
234830**
234831** + the first term on each page is stored in the same way as the
234832** very first term of the segment:
234833**
234834** varint : size of first term
234835** blob: first term data
234836**
234837** 5. Segment doclist indexes:
234838**
234839** Doclist indexes are themselves b-trees, however they usually consist of
234840** a single leaf record only. The format of each doclist index leaf page
234841** is:
234842**
234843** * Flags byte. Bits are:
234844** 0x01: Clear if leaf is also the root page, otherwise set.
234845**
234846** * Page number of fts index leaf page. As a varint.
234847**
234848** * First rowid on page indicated by previous field. As a varint.
234849**
234850** * A list of varints, one for each subsequent termless page. A
234851** positive delta if the termless page contains at least one rowid,
234852** or an 0x00 byte otherwise.
234853**
234854** Internal doclist index nodes are:
234855**
234856** * Flags byte. Bits are:
234857** 0x01: Clear for root page, otherwise set.
234858**
234859** * Page number of first child page. As a varint.
234860**
234861** * Copy of first rowid on page indicated by previous field. As a varint.
234862**
234863** * A list of delta-encoded varints - the first rowid on each subsequent
234864** child page.
234865**
234866** 6. Tombstone Hash Page
234867**
234868** These records are only ever present in contentless_delete=1 tables.
234869** There are zero or more of these associated with each segment. They
234870** are used to store the tombstone rowids for rows contained in the
234871** associated segments.
234872**
234873** The set of nHashPg tombstone hash pages associated with a single
234874** segment together form a single hash table containing tombstone rowids.
234875** To find the page of the hash on which a key might be stored:
234876**
234877** iPg = (rowid % nHashPg)
234878**
234879** Then, within page iPg, which has nSlot slots:
234880**
234881** iSlot = (rowid / nHashPg) % nSlot
234882**
234883** Each tombstone hash page begins with an 8 byte header:
234884**
234885** 1-byte: Key-size (the size in bytes of each slot). Either 4 or 8.
234886** 1-byte: rowid-0-tombstone flag. This flag is only valid on the
234887** first tombstone hash page for each segment (iPg=0). If set,
234888** the hash table contains rowid 0. If clear, it does not.
234889** Rowid 0 is handled specially.
234890** 2-bytes: unused.
234891** 4-bytes: Big-endian integer containing number of entries on page.
234892**
234893** Following this are nSlot 4 or 8 byte slots (depending on the key-size
234894** in the first byte of the page header). The number of slots may be
234895** determined based on the size of the page record and the key-size:
234896**
234897** nSlot = (nByte - 8) / key-size
234898*/
234899
234900/*
234901** Rowids for the averages and structure records in the %_data table.
234902*/
234903#define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
234904#define FTS5_STRUCTURE_ROWID 10 /* The structure record */
234905
234906/*
234907** Macros determining the rowids used by segment leaves and dlidx leaves
234908** and nodes. All nodes and leaves are stored in the %_data table with large
234909** positive rowids.
234910**
234911** Each segment has a unique non-zero 16-bit id.
234912**
234913** The rowid for each segment leaf is found by passing the segment id and
234914** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
234915** sequentially starting from 1.
234916*/
234917#define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
234918#define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
234919#define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
234920#define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
234921
234922#define fts5_dri(segid, dlidx, height, pgno) ( \
234923 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
234924 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
234925 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
234926 ((i64)(pgno)) \
234927)
234928
234929#define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
234930#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
234931#define FTS5_TOMBSTONE_ROWID(segid,ipg) fts5_dri(segid+(1<<16), 0, 0, ipg)
234932
234933#ifdef SQLITE_DEBUG
234934static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
234935#endif
234936
234937
234938/*
234939** Each time a blob is read from the %_data table, it is padded with this
234940** many zero bytes. This makes it easier to decode the various record formats
234941** without overreading if the records are corrupt.
234942*/
234943#define FTS5_DATA_ZERO_PADDING 8
234944#define FTS5_DATA_PADDING 20
234945
234946typedef struct Fts5Data Fts5Data;
234947typedef struct Fts5DlidxIter Fts5DlidxIter;
234948typedef struct Fts5DlidxLvl Fts5DlidxLvl;
234949typedef struct Fts5DlidxWriter Fts5DlidxWriter;
234950typedef struct Fts5Iter Fts5Iter;
234951typedef struct Fts5PageWriter Fts5PageWriter;
234952typedef struct Fts5SegIter Fts5SegIter;
234953typedef struct Fts5DoclistIter Fts5DoclistIter;
234954typedef struct Fts5SegWriter Fts5SegWriter;
234955typedef struct Fts5Structure Fts5Structure;
234956typedef struct Fts5StructureLevel Fts5StructureLevel;
234957typedef struct Fts5StructureSegment Fts5StructureSegment;
234958
234959struct Fts5Data {
234960 u8 *p; /* Pointer to buffer containing record */
234961 int nn; /* Size of record in bytes */
234962 int szLeaf; /* Size of leaf without page-index */
234963};
234964
234965/*
234966** One object per %_data table.
234967**
234968** nContentlessDelete:
234969** The number of contentless delete operations since the most recent
234970** call to fts5IndexFlush() or fts5IndexDiscardData(). This is tracked
234971** so that extra auto-merge work can be done by fts5IndexFlush() to
234972** account for the delete operations.
234973*/
234974struct Fts5Index {
234975 Fts5Config *pConfig; /* Virtual table configuration */
234976 char *zDataTbl; /* Name of %_data table */
234977 int nWorkUnit; /* Leaf pages in a "unit" of work */
234978
234979 /*
234980 ** Variables related to the accumulation of tokens and doclists within the
234981 ** in-memory hash tables before they are flushed to disk.
234982 */
234983 Fts5Hash *pHash; /* Hash table for in-memory data */
234984 int nPendingData; /* Current bytes of pending data */
234985 i64 iWriteRowid; /* Rowid for current doc being written */
234986 int bDelete; /* Current write is a delete */
234987 int nContentlessDelete; /* Number of contentless delete ops */
234988 int nPendingRow; /* Number of INSERT in hash table */
234989
234990 /* Error state. */
234991 int rc; /* Current error code */
234992
234993 /* State used by the fts5DataXXX() functions. */
234994 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
234995 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
234996 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
234997 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
234998 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=?" */
234999 sqlite3_stmt *pIdxSelect;
235000 int nRead; /* Total number of blocks read */
235001
235002 sqlite3_stmt *pDeleteFromIdx;
235003
235004 sqlite3_stmt *pDataVersion;
235005 i64 iStructVersion; /* data_version when pStruct read */
235006 Fts5Structure *pStruct; /* Current db structure (or NULL) */
235007};
235008
235009struct Fts5DoclistIter {
235010 u8 *aEof; /* Pointer to 1 byte past end of doclist */
235011
235012 /* Output variables. aPoslist==0 at EOF */
235013 i64 iRowid;
235014 u8 *aPoslist;
235015 int nPoslist;
235016 int nSize;
235017};
235018
235019/*
235020** The contents of the "structure" record for each index are represented
235021** using an Fts5Structure record in memory. Which uses instances of the
235022** other Fts5StructureXXX types as components.
235023**
235024** nOriginCntr:
235025** This value is set to non-zero for structure records created for
235026** contentlessdelete=1 tables only. In that case it represents the
235027** origin value to apply to the next top-level segment created.
235028*/
235029struct Fts5StructureSegment {
235030 int iSegid; /* Segment id */
235031 int pgnoFirst; /* First leaf page number in segment */
235032 int pgnoLast; /* Last leaf page number in segment */
235033
235034 /* contentlessdelete=1 tables only: */
235035 u64 iOrigin1;
235036 u64 iOrigin2;
235037 int nPgTombstone; /* Number of tombstone hash table pages */
235038 u64 nEntryTombstone; /* Number of tombstone entries that "count" */
235039 u64 nEntry; /* Number of rows in this segment */
235040};
235041struct Fts5StructureLevel {
235042 int nMerge; /* Number of segments in incr-merge */
235043 int nSeg; /* Total number of segments on level */
235044 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
235045};
235046struct Fts5Structure {
235047 int nRef; /* Object reference count */
235048 u64 nWriteCounter; /* Total leaves written to level 0 */
235049 u64 nOriginCntr; /* Origin value for next top-level segment */
235050 int nSegment; /* Total segments in this structure */
235051 int nLevel; /* Number of levels in this index */
235052 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
235053};
235054
235055/*
235056** An object of type Fts5SegWriter is used to write to segments.
235057*/
235058struct Fts5PageWriter {
235059 int pgno; /* Page number for this page */
235060 int iPrevPgidx; /* Previous value written into pgidx */
235061 Fts5Buffer buf; /* Buffer containing leaf data */
235062 Fts5Buffer pgidx; /* Buffer containing page-index */
235063 Fts5Buffer term; /* Buffer containing previous term on page */
235064};
235065struct Fts5DlidxWriter {
235066 int pgno; /* Page number for this page */
235067 int bPrevValid; /* True if iPrev is valid */
235068 i64 iPrev; /* Previous rowid value written to page */
235069 Fts5Buffer buf; /* Buffer containing page data */
235070};
235071struct Fts5SegWriter {
235072 int iSegid; /* Segid to write to */
235073 Fts5PageWriter writer; /* PageWriter object */
235074 i64 iPrevRowid; /* Previous rowid written to current leaf */
235075 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
235076 u8 bFirstRowidInPage; /* True if next rowid is first in page */
235077 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
235078 u8 bFirstTermInPage; /* True if next term will be first in leaf */
235079 int nLeafWritten; /* Number of leaf pages written */
235080 int nEmpty; /* Number of contiguous term-less nodes */
235081
235082 int nDlidx; /* Allocated size of aDlidx[] array */
235083 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
235084
235085 /* Values to insert into the %_idx table */
235086 Fts5Buffer btterm; /* Next term to insert into %_idx table */
235087 int iBtPage; /* Page number corresponding to btterm */
235088};
235089
235090typedef struct Fts5CResult Fts5CResult;
235091struct Fts5CResult {
235092 u16 iFirst; /* aSeg[] index of firstest iterator */
235093 u8 bTermEq; /* True if the terms are equal */
235094};
235095
235096/*
235097** Object for iterating through a single segment, visiting each term/rowid
235098** pair in the segment.
235099**
235100** pSeg:
235101** The segment to iterate through.
235102**
235103** iLeafPgno:
235104** Current leaf page number within segment.
235105**
235106** iLeafOffset:
235107** Byte offset within the current leaf that is the first byte of the
235108** position list data (one byte passed the position-list size field).
235109**
235110** pLeaf:
235111** Buffer containing current leaf page data. Set to NULL at EOF.
235112**
235113** iTermLeafPgno, iTermLeafOffset:
235114** Leaf page number containing the last term read from the segment. And
235115** the offset immediately following the term data.
235116**
235117** flags:
235118** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
235119**
235120** FTS5_SEGITER_ONETERM:
235121** If set, set the iterator to point to EOF after the current doclist
235122** has been exhausted. Do not proceed to the next term in the segment.
235123**
235124** FTS5_SEGITER_REVERSE:
235125** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
235126** it is set, iterate through rowid in descending order instead of the
235127** default ascending order.
235128**
235129** iRowidOffset/nRowidOffset/aRowidOffset:
235130** These are used if the FTS5_SEGITER_REVERSE flag is set.
235131**
235132** For each rowid on the page corresponding to the current term, the
235133** corresponding aRowidOffset[] entry is set to the byte offset of the
235134** start of the "position-list-size" field within the page.
235135**
235136** iTermIdx:
235137** Index of current term on iTermLeafPgno.
235138**
235139** apTombstone/nTombstone:
235140** These are used for contentless_delete=1 tables only. When the cursor
235141** is first allocated, the apTombstone[] array is allocated so that it
235142** is large enough for all tombstones hash pages associated with the
235143** segment. The pages themselves are loaded lazily from the database as
235144** they are required.
235145*/
235146struct Fts5SegIter {
235147 Fts5StructureSegment *pSeg; /* Segment to iterate through */
235148 int flags; /* Mask of configuration flags */
235149 int iLeafPgno; /* Current leaf page number */
235150 Fts5Data *pLeaf; /* Current leaf data */
235151 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
235152 i64 iLeafOffset; /* Byte offset within current leaf */
235153 Fts5Data **apTombstone; /* Array of tombstone pages */
235154 int nTombstone;
235155
235156 /* Next method */
235157 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
235158
235159 /* The page and offset from which the current term was read. The offset
235160 ** is the offset of the first rowid in the current doclist. */
235161 int iTermLeafPgno;
235162 int iTermLeafOffset;
235163
235164 int iPgidxOff; /* Next offset in pgidx */
235165 int iEndofDoclist;
235166
235167 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
235168 int iRowidOffset; /* Current entry in aRowidOffset[] */
235169 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
235170 int *aRowidOffset; /* Array of offset to rowid fields */
235171
235172 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
235173
235174 /* Variables populated based on current entry. */
235175 Fts5Buffer term; /* Current term */
235176 i64 iRowid; /* Current rowid */
235177 int nPos; /* Number of bytes in current position list */
235178 u8 bDel; /* True if the delete flag is set */
235179};
235180
235181/*
235182** Argument is a pointer to an Fts5Data structure that contains a
235183** leaf page.
235184*/
235185#define ASSERT_SZLEAF_OK(x) assert( \
235186 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
235187)
235188
235189#define FTS5_SEGITER_ONETERM 0x01
235190#define FTS5_SEGITER_REVERSE 0x02
235191
235192/*
235193** Argument is a pointer to an Fts5Data structure that contains a leaf
235194** page. This macro evaluates to true if the leaf contains no terms, or
235195** false if it contains at least one term.
235196*/
235197#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
235198
235199#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
235200
235201#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
235202
235203/*
235204** Object for iterating through the merged results of one or more segments,
235205** visiting each term/rowid pair in the merged data.
235206**
235207** nSeg is always a power of two greater than or equal to the number of
235208** segments that this object is merging data from. Both the aSeg[] and
235209** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
235210** with zeroed objects - these are handled as if they were iterators opened
235211** on empty segments.
235212**
235213** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
235214** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
235215** comparison in this context is the index of the iterator that currently
235216** points to the smaller term/rowid combination. Iterators at EOF are
235217** considered to be greater than all other iterators.
235218**
235219** aFirst[1] contains the index in aSeg[] of the iterator that points to
235220** the smallest key overall. aFirst[0] is unused.
235221**
235222** poslist:
235223** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
235224** There is no way to tell if this is populated or not.
235225*/
235226struct Fts5Iter {
235227 Fts5IndexIter base; /* Base class containing output vars */
235228
235229 Fts5Index *pIndex; /* Index that owns this iterator */
235230 Fts5Buffer poslist; /* Buffer containing current poslist */
235231 Fts5Colset *pColset; /* Restrict matches to these columns */
235232
235233 /* Invoked to set output variables. */
235234 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
235235
235236 int nSeg; /* Size of aSeg[] array */
235237 int bRev; /* True to iterate in reverse order */
235238 u8 bSkipEmpty; /* True to skip deleted entries */
235239
235240 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
235241 Fts5CResult *aFirst; /* Current merge state (see above) */
235242 Fts5SegIter aSeg[1]; /* Array of segment iterators */
235243};
235244
235245
235246/*
235247** An instance of the following type is used to iterate through the contents
235248** of a doclist-index record.
235249**
235250** pData:
235251** Record containing the doclist-index data.
235252**
235253** bEof:
235254** Set to true once iterator has reached EOF.
235255**
235256** iOff:
235257** Set to the current offset within record pData.
235258*/
235259struct Fts5DlidxLvl {
235260 Fts5Data *pData; /* Data for current page of this level */
235261 int iOff; /* Current offset into pData */
235262 int bEof; /* At EOF already */
235263 int iFirstOff; /* Used by reverse iterators */
235264
235265 /* Output variables */
235266 int iLeafPgno; /* Page number of current leaf page */
235267 i64 iRowid; /* First rowid on leaf iLeafPgno */
235268};
235269struct Fts5DlidxIter {
235270 int nLvl;
235271 int iSegid;
235272 Fts5DlidxLvl aLvl[1];
235273};
235274
235275static void fts5PutU16(u8 *aOut, u16 iVal){
235276 aOut[0] = (iVal>>8);
235277 aOut[1] = (iVal&0xFF);
235278}
235279
235280static u16 fts5GetU16(const u8 *aIn){
235281 return ((u16)aIn[0] << 8) + aIn[1];
235282}
235283
235284/*
235285** The only argument points to a buffer at least 8 bytes in size. This
235286** function interprets the first 8 bytes of the buffer as a 64-bit big-endian
235287** unsigned integer and returns the result.
235288*/
235289static u64 fts5GetU64(u8 *a){
235290 return ((u64)a[0] << 56)
235291 + ((u64)a[1] << 48)
235292 + ((u64)a[2] << 40)
235293 + ((u64)a[3] << 32)
235294 + ((u64)a[4] << 24)
235295 + ((u64)a[5] << 16)
235296 + ((u64)a[6] << 8)
235297 + ((u64)a[7] << 0);
235298}
235299
235300/*
235301** The only argument points to a buffer at least 4 bytes in size. This
235302** function interprets the first 4 bytes of the buffer as a 32-bit big-endian
235303** unsigned integer and returns the result.
235304*/
235305static u32 fts5GetU32(const u8 *a){
235306 return ((u32)a[0] << 24)
235307 + ((u32)a[1] << 16)
235308 + ((u32)a[2] << 8)
235309 + ((u32)a[3] << 0);
235310}
235311
235312/*
235313** Write iVal, formated as a 64-bit big-endian unsigned integer, to the
235314** buffer indicated by the first argument.
235315*/
235316static void fts5PutU64(u8 *a, u64 iVal){
235317 a[0] = ((iVal >> 56) & 0xFF);
235318 a[1] = ((iVal >> 48) & 0xFF);
235319 a[2] = ((iVal >> 40) & 0xFF);
235320 a[3] = ((iVal >> 32) & 0xFF);
235321 a[4] = ((iVal >> 24) & 0xFF);
235322 a[5] = ((iVal >> 16) & 0xFF);
235323 a[6] = ((iVal >> 8) & 0xFF);
235324 a[7] = ((iVal >> 0) & 0xFF);
235325}
235326
235327/*
235328** Write iVal, formated as a 32-bit big-endian unsigned integer, to the
235329** buffer indicated by the first argument.
235330*/
235331static void fts5PutU32(u8 *a, u32 iVal){
235332 a[0] = ((iVal >> 24) & 0xFF);
235333 a[1] = ((iVal >> 16) & 0xFF);
235334 a[2] = ((iVal >> 8) & 0xFF);
235335 a[3] = ((iVal >> 0) & 0xFF);
235336}
235337
235338/*
235339** Allocate and return a buffer at least nByte bytes in size.
235340**
235341** If an OOM error is encountered, return NULL and set the error code in
235342** the Fts5Index handle passed as the first argument.
235343*/
235344static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
235345 return sqlite3Fts5MallocZero(pRc: &p->rc, nByte);
235346}
235347
235348/*
235349** Compare the contents of the pLeft buffer with the pRight/nRight blob.
235350**
235351** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
235352** +ve if pRight is smaller than pLeft. In other words:
235353**
235354** res = *pLeft - *pRight
235355*/
235356#ifdef SQLITE_DEBUG
235357static int fts5BufferCompareBlob(
235358 Fts5Buffer *pLeft, /* Left hand side of comparison */
235359 const u8 *pRight, int nRight /* Right hand side of comparison */
235360){
235361 int nCmp = MIN(pLeft->n, nRight);
235362 int res = memcmp(pLeft->p, pRight, nCmp);
235363 return (res==0 ? (pLeft->n - nRight) : res);
235364}
235365#endif
235366
235367/*
235368** Compare the contents of the two buffers using memcmp(). If one buffer
235369** is a prefix of the other, it is considered the lesser.
235370**
235371** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
235372** +ve if pRight is smaller than pLeft. In other words:
235373**
235374** res = *pLeft - *pRight
235375*/
235376static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
235377 int nCmp, res;
235378 nCmp = MIN(pLeft->n, pRight->n);
235379 assert( nCmp<=0 || pLeft->p!=0 );
235380 assert( nCmp<=0 || pRight->p!=0 );
235381 res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
235382 return (res==0 ? (pLeft->n - pRight->n) : res);
235383}
235384
235385static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
235386 int ret;
235387 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
235388 return ret;
235389}
235390
235391/*
235392** Close the read-only blob handle, if it is open.
235393*/
235394static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
235395 if( p->pReader ){
235396 sqlite3_blob *pReader = p->pReader;
235397 p->pReader = 0;
235398 sqlite3_blob_close(pBlob: pReader);
235399 }
235400}
235401
235402/*
235403** Retrieve a record from the %_data table.
235404**
235405** If an error occurs, NULL is returned and an error left in the
235406** Fts5Index object.
235407*/
235408static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
235409 Fts5Data *pRet = 0;
235410 if( p->rc==SQLITE_OK ){
235411 int rc = SQLITE_OK;
235412
235413 if( p->pReader ){
235414 /* This call may return SQLITE_ABORT if there has been a savepoint
235415 ** rollback since it was last used. In this case a new blob handle
235416 ** is required. */
235417 sqlite3_blob *pBlob = p->pReader;
235418 p->pReader = 0;
235419 rc = sqlite3_blob_reopen(pBlob, iRow: iRowid);
235420 assert( p->pReader==0 );
235421 p->pReader = pBlob;
235422 if( rc!=SQLITE_OK ){
235423 sqlite3Fts5IndexCloseReader(p);
235424 }
235425 if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
235426 }
235427
235428 /* If the blob handle is not open at this point, open it and seek
235429 ** to the requested entry. */
235430 if( p->pReader==0 && rc==SQLITE_OK ){
235431 Fts5Config *pConfig = p->pConfig;
235432 rc = sqlite3_blob_open(db: pConfig->db,
235433 zDb: pConfig->zDb, zTable: p->zDataTbl, zColumn: "block", iRow: iRowid, wrFlag: 0, ppBlob: &p->pReader
235434 );
235435 }
235436
235437 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
235438 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
235439 ** All the reasons those functions might return SQLITE_ERROR - missing
235440 ** table, missing row, non-blob/text in block column - indicate
235441 ** backing store corruption. */
235442 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
235443
235444 if( rc==SQLITE_OK ){
235445 u8 *aOut = 0; /* Read blob data into this buffer */
235446 int nByte = sqlite3_blob_bytes(pBlob: p->pReader);
235447 sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
235448 pRet = (Fts5Data*)sqlite3_malloc64(n: nAlloc);
235449 if( pRet ){
235450 pRet->nn = nByte;
235451 aOut = pRet->p = (u8*)&pRet[1];
235452 }else{
235453 rc = SQLITE_NOMEM;
235454 }
235455
235456 if( rc==SQLITE_OK ){
235457 rc = sqlite3_blob_read(pBlob: p->pReader, z: aOut, n: nByte, iOffset: 0);
235458 }
235459 if( rc!=SQLITE_OK ){
235460 sqlite3_free(p: pRet);
235461 pRet = 0;
235462 }else{
235463 /* TODO1: Fix this */
235464 pRet->p[nByte] = 0x00;
235465 pRet->p[nByte+1] = 0x00;
235466 pRet->szLeaf = fts5GetU16(aIn: &pRet->p[2]);
235467 }
235468 }
235469 p->rc = rc;
235470 p->nRead++;
235471 }
235472
235473 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
235474 return pRet;
235475}
235476
235477
235478/*
235479** Release a reference to data record returned by an earlier call to
235480** fts5DataRead().
235481*/
235482static void fts5DataRelease(Fts5Data *pData){
235483 sqlite3_free(p: pData);
235484}
235485
235486static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
235487 Fts5Data *pRet = fts5DataRead(p, iRowid);
235488 if( pRet ){
235489 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
235490 p->rc = FTS5_CORRUPT;
235491 fts5DataRelease(pData: pRet);
235492 pRet = 0;
235493 }
235494 }
235495 return pRet;
235496}
235497
235498static int fts5IndexPrepareStmt(
235499 Fts5Index *p,
235500 sqlite3_stmt **ppStmt,
235501 char *zSql
235502){
235503 if( p->rc==SQLITE_OK ){
235504 if( zSql ){
235505 p->rc = sqlite3_prepare_v3(db: p->pConfig->db, zSql, nBytes: -1,
235506 SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
235507 ppStmt, pzTail: 0);
235508 }else{
235509 p->rc = SQLITE_NOMEM;
235510 }
235511 }
235512 sqlite3_free(p: zSql);
235513 return p->rc;
235514}
235515
235516
235517/*
235518** INSERT OR REPLACE a record into the %_data table.
235519*/
235520static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
235521 if( p->rc!=SQLITE_OK ) return;
235522
235523 if( p->pWriter==0 ){
235524 Fts5Config *pConfig = p->pConfig;
235525 fts5IndexPrepareStmt(p, ppStmt: &p->pWriter, zSql: sqlite3_mprintf(
235526 zFormat: "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
235527 pConfig->zDb, pConfig->zName
235528 ));
235529 if( p->rc ) return;
235530 }
235531
235532 sqlite3_bind_int64(pStmt: p->pWriter, i: 1, iValue: iRowid);
235533 sqlite3_bind_blob(pStmt: p->pWriter, i: 2, zData: pData, nData, SQLITE_STATIC);
235534 sqlite3_step(pStmt: p->pWriter);
235535 p->rc = sqlite3_reset(pStmt: p->pWriter);
235536 sqlite3_bind_null(pStmt: p->pWriter, i: 2);
235537}
235538
235539/*
235540** Execute the following SQL:
235541**
235542** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
235543*/
235544static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
235545 if( p->rc!=SQLITE_OK ) return;
235546
235547 if( p->pDeleter==0 ){
235548 Fts5Config *pConfig = p->pConfig;
235549 char *zSql = sqlite3_mprintf(
235550 zFormat: "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
235551 pConfig->zDb, pConfig->zName
235552 );
235553 if( fts5IndexPrepareStmt(p, ppStmt: &p->pDeleter, zSql) ) return;
235554 }
235555
235556 sqlite3_bind_int64(pStmt: p->pDeleter, i: 1, iValue: iFirst);
235557 sqlite3_bind_int64(pStmt: p->pDeleter, i: 2, iValue: iLast);
235558 sqlite3_step(pStmt: p->pDeleter);
235559 p->rc = sqlite3_reset(pStmt: p->pDeleter);
235560}
235561
235562/*
235563** Remove all records associated with segment iSegid.
235564*/
235565static void fts5DataRemoveSegment(Fts5Index *p, Fts5StructureSegment *pSeg){
235566 int iSegid = pSeg->iSegid;
235567 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
235568 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
235569 fts5DataDelete(p, iFirst, iLast);
235570
235571 if( pSeg->nPgTombstone ){
235572 i64 iTomb1 = FTS5_TOMBSTONE_ROWID(iSegid, 0);
235573 i64 iTomb2 = FTS5_TOMBSTONE_ROWID(iSegid, pSeg->nPgTombstone-1);
235574 fts5DataDelete(p, iFirst: iTomb1, iLast: iTomb2);
235575 }
235576 if( p->pIdxDeleter==0 ){
235577 Fts5Config *pConfig = p->pConfig;
235578 fts5IndexPrepareStmt(p, ppStmt: &p->pIdxDeleter, zSql: sqlite3_mprintf(
235579 zFormat: "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
235580 pConfig->zDb, pConfig->zName
235581 ));
235582 }
235583 if( p->rc==SQLITE_OK ){
235584 sqlite3_bind_int(p: p->pIdxDeleter, i: 1, iValue: iSegid);
235585 sqlite3_step(pStmt: p->pIdxDeleter);
235586 p->rc = sqlite3_reset(pStmt: p->pIdxDeleter);
235587 }
235588}
235589
235590/*
235591** Release a reference to an Fts5Structure object returned by an earlier
235592** call to fts5StructureRead() or fts5StructureDecode().
235593*/
235594static void fts5StructureRelease(Fts5Structure *pStruct){
235595 if( pStruct && 0>=(--pStruct->nRef) ){
235596 int i;
235597 assert( pStruct->nRef==0 );
235598 for(i=0; i<pStruct->nLevel; i++){
235599 sqlite3_free(p: pStruct->aLevel[i].aSeg);
235600 }
235601 sqlite3_free(p: pStruct);
235602 }
235603}
235604
235605static void fts5StructureRef(Fts5Structure *pStruct){
235606 pStruct->nRef++;
235607}
235608
235609static void *sqlite3Fts5StructureRef(Fts5Index *p){
235610 fts5StructureRef(pStruct: p->pStruct);
235611 return (void*)p->pStruct;
235612}
235613static void sqlite3Fts5StructureRelease(void *p){
235614 if( p ){
235615 fts5StructureRelease(pStruct: (Fts5Structure*)p);
235616 }
235617}
235618static int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){
235619 if( p->pStruct!=(Fts5Structure*)pStruct ){
235620 return SQLITE_ABORT;
235621 }
235622 return SQLITE_OK;
235623}
235624
235625/*
235626** Ensure that structure object (*pp) is writable.
235627**
235628** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
235629** an error occurs, (*pRc) is set to an SQLite error code before returning.
235630*/
235631static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){
235632 Fts5Structure *p = *pp;
235633 if( *pRc==SQLITE_OK && p->nRef>1 ){
235634 i64 nByte = sizeof(Fts5Structure)+(p->nLevel-1)*sizeof(Fts5StructureLevel);
235635 Fts5Structure *pNew;
235636 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte);
235637 if( pNew ){
235638 int i;
235639 memcpy(dest: pNew, src: p, n: nByte);
235640 for(i=0; i<p->nLevel; i++) pNew->aLevel[i].aSeg = 0;
235641 for(i=0; i<p->nLevel; i++){
235642 Fts5StructureLevel *pLvl = &pNew->aLevel[i];
235643 nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg;
235644 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte);
235645 if( pLvl->aSeg==0 ){
235646 for(i=0; i<p->nLevel; i++){
235647 sqlite3_free(p: pNew->aLevel[i].aSeg);
235648 }
235649 sqlite3_free(p: pNew);
235650 return;
235651 }
235652 memcpy(dest: pLvl->aSeg, src: p->aLevel[i].aSeg, n: nByte);
235653 }
235654 p->nRef--;
235655 pNew->nRef = 1;
235656 }
235657 *pp = pNew;
235658 }
235659}
235660
235661/*
235662** Deserialize and return the structure record currently stored in serialized
235663** form within buffer pData/nData.
235664**
235665** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
235666** are over-allocated by one slot. This allows the structure contents
235667** to be more easily edited.
235668**
235669** If an error occurs, *ppOut is set to NULL and an SQLite error code
235670** returned. Otherwise, *ppOut is set to point to the new object and
235671** SQLITE_OK returned.
235672*/
235673static int fts5StructureDecode(
235674 const u8 *pData, /* Buffer containing serialized structure */
235675 int nData, /* Size of buffer pData in bytes */
235676 int *piCookie, /* Configuration cookie value */
235677 Fts5Structure **ppOut /* OUT: Deserialized object */
235678){
235679 int rc = SQLITE_OK;
235680 int i = 0;
235681 int iLvl;
235682 int nLevel = 0;
235683 int nSegment = 0;
235684 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
235685 Fts5Structure *pRet = 0; /* Structure object to return */
235686 int bStructureV2 = 0; /* True for FTS5_STRUCTURE_V2 */
235687 u64 nOriginCntr = 0; /* Largest origin value seen so far */
235688
235689 /* Grab the cookie value */
235690 if( piCookie ) *piCookie = sqlite3Fts5Get32(aBuf: pData);
235691 i = 4;
235692
235693 /* Check if this is a V2 structure record. Set bStructureV2 if it is. */
235694 if( 0==memcmp(s1: &pData[i], FTS5_STRUCTURE_V2, n: 4) ){
235695 i += 4;
235696 bStructureV2 = 1;
235697 }
235698
235699 /* Read the total number of levels and segments from the start of the
235700 ** structure record. */
235701 i += fts5GetVarint32(&pData[i], nLevel);
235702 i += fts5GetVarint32(&pData[i], nSegment);
235703 if( nLevel>FTS5_MAX_SEGMENT || nLevel<0
235704 || nSegment>FTS5_MAX_SEGMENT || nSegment<0
235705 ){
235706 return FTS5_CORRUPT;
235707 }
235708 nByte = (
235709 sizeof(Fts5Structure) + /* Main structure */
235710 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
235711 );
235712 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
235713
235714 if( pRet ){
235715 pRet->nRef = 1;
235716 pRet->nLevel = nLevel;
235717 pRet->nSegment = nSegment;
235718 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
235719
235720 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
235721 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
235722 int nTotal = 0;
235723 int iSeg;
235724
235725 if( i>=nData ){
235726 rc = FTS5_CORRUPT;
235727 }else{
235728 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
235729 i += fts5GetVarint32(&pData[i], nTotal);
235730 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
235731 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc: &rc,
235732 nByte: nTotal * sizeof(Fts5StructureSegment)
235733 );
235734 nSegment -= nTotal;
235735 }
235736
235737 if( rc==SQLITE_OK ){
235738 pLvl->nSeg = nTotal;
235739 for(iSeg=0; iSeg<nTotal; iSeg++){
235740 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
235741 if( i>=nData ){
235742 rc = FTS5_CORRUPT;
235743 break;
235744 }
235745 assert( pSeg!=0 );
235746 i += fts5GetVarint32(&pData[i], pSeg->iSegid);
235747 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
235748 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
235749 if( bStructureV2 ){
235750 i += fts5GetVarint(&pData[i], &pSeg->iOrigin1);
235751 i += fts5GetVarint(&pData[i], &pSeg->iOrigin2);
235752 i += fts5GetVarint32(&pData[i], pSeg->nPgTombstone);
235753 i += fts5GetVarint(&pData[i], &pSeg->nEntryTombstone);
235754 i += fts5GetVarint(&pData[i], &pSeg->nEntry);
235755 nOriginCntr = MAX(nOriginCntr, pSeg->iOrigin2);
235756 }
235757 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
235758 rc = FTS5_CORRUPT;
235759 break;
235760 }
235761 }
235762 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
235763 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
235764 }
235765 }
235766 if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT;
235767 if( bStructureV2 ){
235768 pRet->nOriginCntr = nOriginCntr+1;
235769 }
235770
235771 if( rc!=SQLITE_OK ){
235772 fts5StructureRelease(pStruct: pRet);
235773 pRet = 0;
235774 }
235775 }
235776
235777 *ppOut = pRet;
235778 return rc;
235779}
235780
235781/*
235782** Add a level to the Fts5Structure.aLevel[] array of structure object
235783** (*ppStruct).
235784*/
235785static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
235786 fts5StructureMakeWritable(pRc, pp: ppStruct);
235787 assert( (ppStruct!=0 && (*ppStruct)!=0) || (*pRc)!=SQLITE_OK );
235788 if( *pRc==SQLITE_OK ){
235789 Fts5Structure *pStruct = *ppStruct;
235790 int nLevel = pStruct->nLevel;
235791 sqlite3_int64 nByte = (
235792 sizeof(Fts5Structure) + /* Main structure */
235793 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
235794 );
235795
235796 pStruct = sqlite3_realloc64(pOld: pStruct, n: nByte);
235797 if( pStruct ){
235798 memset(s: &pStruct->aLevel[nLevel], c: 0, n: sizeof(Fts5StructureLevel));
235799 pStruct->nLevel++;
235800 *ppStruct = pStruct;
235801 }else{
235802 *pRc = SQLITE_NOMEM;
235803 }
235804 }
235805}
235806
235807/*
235808** Extend level iLvl so that there is room for at least nExtra more
235809** segments.
235810*/
235811static void fts5StructureExtendLevel(
235812 int *pRc,
235813 Fts5Structure *pStruct,
235814 int iLvl,
235815 int nExtra,
235816 int bInsert
235817){
235818 if( *pRc==SQLITE_OK ){
235819 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
235820 Fts5StructureSegment *aNew;
235821 sqlite3_int64 nByte;
235822
235823 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
235824 aNew = sqlite3_realloc64(pOld: pLvl->aSeg, n: nByte);
235825 if( aNew ){
235826 if( bInsert==0 ){
235827 memset(s: &aNew[pLvl->nSeg], c: 0, n: sizeof(Fts5StructureSegment) * nExtra);
235828 }else{
235829 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
235830 memmove(dest: &aNew[nExtra], src: aNew, n: nMove);
235831 memset(s: aNew, c: 0, n: sizeof(Fts5StructureSegment) * nExtra);
235832 }
235833 pLvl->aSeg = aNew;
235834 }else{
235835 *pRc = SQLITE_NOMEM;
235836 }
235837 }
235838}
235839
235840static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
235841 Fts5Structure *pRet = 0;
235842 Fts5Config *pConfig = p->pConfig;
235843 int iCookie; /* Configuration cookie */
235844 Fts5Data *pData;
235845
235846 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
235847 if( p->rc==SQLITE_OK ){
235848 /* TODO: Do we need this if the leaf-index is appended? Probably... */
235849 memset(s: &pData->p[pData->nn], c: 0, FTS5_DATA_PADDING);
235850 p->rc = fts5StructureDecode(pData: pData->p, nData: pData->nn, piCookie: &iCookie, ppOut: &pRet);
235851 if( p->rc==SQLITE_OK && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
235852 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
235853 }
235854 fts5DataRelease(pData);
235855 if( p->rc!=SQLITE_OK ){
235856 fts5StructureRelease(pStruct: pRet);
235857 pRet = 0;
235858 }
235859 }
235860
235861 return pRet;
235862}
235863
235864static i64 fts5IndexDataVersion(Fts5Index *p){
235865 i64 iVersion = 0;
235866
235867 if( p->rc==SQLITE_OK ){
235868 if( p->pDataVersion==0 ){
235869 p->rc = fts5IndexPrepareStmt(p, ppStmt: &p->pDataVersion,
235870 zSql: sqlite3_mprintf(zFormat: "PRAGMA %Q.data_version", p->pConfig->zDb)
235871 );
235872 if( p->rc ) return 0;
235873 }
235874
235875 if( SQLITE_ROW==sqlite3_step(pStmt: p->pDataVersion) ){
235876 iVersion = sqlite3_column_int64(pStmt: p->pDataVersion, i: 0);
235877 }
235878 p->rc = sqlite3_reset(pStmt: p->pDataVersion);
235879 }
235880
235881 return iVersion;
235882}
235883
235884/*
235885** Read, deserialize and return the structure record.
235886**
235887** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
235888** are over-allocated as described for function fts5StructureDecode()
235889** above.
235890**
235891** If an error occurs, NULL is returned and an error code left in the
235892** Fts5Index handle. If an error has already occurred when this function
235893** is called, it is a no-op.
235894*/
235895static Fts5Structure *fts5StructureRead(Fts5Index *p){
235896
235897 if( p->pStruct==0 ){
235898 p->iStructVersion = fts5IndexDataVersion(p);
235899 if( p->rc==SQLITE_OK ){
235900 p->pStruct = fts5StructureReadUncached(p);
235901 }
235902 }
235903
235904#if 0
235905 else{
235906 Fts5Structure *pTest = fts5StructureReadUncached(p);
235907 if( pTest ){
235908 int i, j;
235909 assert_nc( p->pStruct->nSegment==pTest->nSegment );
235910 assert_nc( p->pStruct->nLevel==pTest->nLevel );
235911 for(i=0; i<pTest->nLevel; i++){
235912 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
235913 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
235914 for(j=0; j<pTest->aLevel[i].nSeg; j++){
235915 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
235916 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
235917 assert_nc( p1->iSegid==p2->iSegid );
235918 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
235919 assert_nc( p1->pgnoLast==p2->pgnoLast );
235920 }
235921 }
235922 fts5StructureRelease(pTest);
235923 }
235924 }
235925#endif
235926
235927 if( p->rc!=SQLITE_OK ) return 0;
235928 assert( p->iStructVersion!=0 );
235929 assert( p->pStruct!=0 );
235930 fts5StructureRef(pStruct: p->pStruct);
235931 return p->pStruct;
235932}
235933
235934static void fts5StructureInvalidate(Fts5Index *p){
235935 if( p->pStruct ){
235936 fts5StructureRelease(pStruct: p->pStruct);
235937 p->pStruct = 0;
235938 }
235939}
235940
235941/*
235942** Return the total number of segments in index structure pStruct. This
235943** function is only ever used as part of assert() conditions.
235944*/
235945#ifdef SQLITE_DEBUG
235946static int fts5StructureCountSegments(Fts5Structure *pStruct){
235947 int nSegment = 0; /* Total number of segments */
235948 if( pStruct ){
235949 int iLvl; /* Used to iterate through levels */
235950 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
235951 nSegment += pStruct->aLevel[iLvl].nSeg;
235952 }
235953 }
235954
235955 return nSegment;
235956}
235957#endif
235958
235959#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
235960 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) ); \
235961 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
235962 (pBuf)->n += nBlob; \
235963}
235964
235965#define fts5BufferSafeAppendVarint(pBuf, iVal) { \
235966 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
235967 assert( (pBuf)->nSpace>=(pBuf)->n ); \
235968}
235969
235970
235971/*
235972** Serialize and store the "structure" record.
235973**
235974** If an error occurs, leave an error code in the Fts5Index object. If an
235975** error has already occurred, this function is a no-op.
235976*/
235977static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
235978 if( p->rc==SQLITE_OK ){
235979 Fts5Buffer buf; /* Buffer to serialize record into */
235980 int iLvl; /* Used to iterate through levels */
235981 int iCookie; /* Cookie value to store */
235982 int nHdr = (pStruct->nOriginCntr>0 ? (4+4+9+9+9) : (4+9+9));
235983
235984 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
235985 memset(s: &buf, c: 0, n: sizeof(Fts5Buffer));
235986
235987 /* Append the current configuration cookie */
235988 iCookie = p->pConfig->iCookie;
235989 if( iCookie<0 ) iCookie = 0;
235990
235991 if( 0==sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &buf, nByte: nHdr) ){
235992 sqlite3Fts5Put32(aBuf: buf.p, iVal: iCookie);
235993 buf.n = 4;
235994 if( pStruct->nOriginCntr>0 ){
235995 fts5BufferSafeAppendBlob(&buf, FTS5_STRUCTURE_V2, 4);
235996 }
235997 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
235998 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
235999 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
236000 }
236001
236002 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
236003 int iSeg; /* Used to iterate through segments */
236004 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
236005 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
236006 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
236007 assert( pLvl->nMerge<=pLvl->nSeg );
236008
236009 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
236010 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
236011 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iSegid);
236012 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoFirst);
236013 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoLast);
236014 if( pStruct->nOriginCntr>0 ){
236015 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin1);
236016 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin2);
236017 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nPgTombstone);
236018 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntryTombstone);
236019 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntry);
236020 }
236021 }
236022 }
236023
236024 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, pData: buf.p, nData: buf.n);
236025 fts5BufferFree(&buf);
236026 }
236027}
236028
236029#if 0
236030static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
236031static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
236032 int rc = SQLITE_OK;
236033 Fts5Buffer buf;
236034 memset(&buf, 0, sizeof(buf));
236035 fts5DebugStructure(&rc, &buf, pStruct);
236036 fprintf(stdout, "%s: %s\n", zCaption, buf.p);
236037 fflush(stdout);
236038 fts5BufferFree(&buf);
236039}
236040#else
236041# define fts5PrintStructure(x,y)
236042#endif
236043
236044static int fts5SegmentSize(Fts5StructureSegment *pSeg){
236045 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
236046}
236047
236048/*
236049** Return a copy of index structure pStruct. Except, promote as many
236050** segments as possible to level iPromote. If an OOM occurs, NULL is
236051** returned.
236052*/
236053static void fts5StructurePromoteTo(
236054 Fts5Index *p,
236055 int iPromote,
236056 int szPromote,
236057 Fts5Structure *pStruct
236058){
236059 int il, is;
236060 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
236061
236062 if( pOut->nMerge==0 ){
236063 for(il=iPromote+1; il<pStruct->nLevel; il++){
236064 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
236065 if( pLvl->nMerge ) return;
236066 for(is=pLvl->nSeg-1; is>=0; is--){
236067 int sz = fts5SegmentSize(pSeg: &pLvl->aSeg[is]);
236068 if( sz>szPromote ) return;
236069 fts5StructureExtendLevel(pRc: &p->rc, pStruct, iLvl: iPromote, nExtra: 1, bInsert: 1);
236070 if( p->rc ) return;
236071 memcpy(dest: pOut->aSeg, src: &pLvl->aSeg[is], n: sizeof(Fts5StructureSegment));
236072 pOut->nSeg++;
236073 pLvl->nSeg--;
236074 }
236075 }
236076 }
236077}
236078
236079/*
236080** A new segment has just been written to level iLvl of index structure
236081** pStruct. This function determines if any segments should be promoted
236082** as a result. Segments are promoted in two scenarios:
236083**
236084** a) If the segment just written is smaller than one or more segments
236085** within the previous populated level, it is promoted to the previous
236086** populated level.
236087**
236088** b) If the segment just written is larger than the newest segment on
236089** the next populated level, then that segment, and any other adjacent
236090** segments that are also smaller than the one just written, are
236091** promoted.
236092**
236093** If one or more segments are promoted, the structure object is updated
236094** to reflect this.
236095*/
236096static void fts5StructurePromote(
236097 Fts5Index *p, /* FTS5 backend object */
236098 int iLvl, /* Index level just updated */
236099 Fts5Structure *pStruct /* Index structure */
236100){
236101 if( p->rc==SQLITE_OK ){
236102 int iTst;
236103 int iPromote = -1;
236104 int szPromote = 0; /* Promote anything this size or smaller */
236105 Fts5StructureSegment *pSeg; /* Segment just written */
236106 int szSeg; /* Size of segment just written */
236107 int nSeg = pStruct->aLevel[iLvl].nSeg;
236108
236109 if( nSeg==0 ) return;
236110 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
236111 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
236112
236113 /* Check for condition (a) */
236114 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
236115 if( iTst>=0 ){
236116 int i;
236117 int szMax = 0;
236118 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
236119 assert( pTst->nMerge==0 );
236120 for(i=0; i<pTst->nSeg; i++){
236121 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
236122 if( sz>szMax ) szMax = sz;
236123 }
236124 if( szMax>=szSeg ){
236125 /* Condition (a) is true. Promote the newest segment on level
236126 ** iLvl to level iTst. */
236127 iPromote = iTst;
236128 szPromote = szMax;
236129 }
236130 }
236131
236132 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
236133 ** is a no-op if it is not. */
236134 if( iPromote<0 ){
236135 iPromote = iLvl;
236136 szPromote = szSeg;
236137 }
236138 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
236139 }
236140}
236141
236142
236143/*
236144** Advance the iterator passed as the only argument. If the end of the
236145** doclist-index page is reached, return non-zero.
236146*/
236147static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
236148 Fts5Data *pData = pLvl->pData;
236149
236150 if( pLvl->iOff==0 ){
236151 assert( pLvl->bEof==0 );
236152 pLvl->iOff = 1;
236153 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
236154 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
236155 pLvl->iFirstOff = pLvl->iOff;
236156 }else{
236157 int iOff;
236158 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
236159 if( pData->p[iOff] ) break;
236160 }
236161
236162 if( iOff<pData->nn ){
236163 i64 iVal;
236164 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
236165 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
236166 pLvl->iRowid += iVal;
236167 pLvl->iOff = iOff;
236168 }else{
236169 pLvl->bEof = 1;
236170 }
236171 }
236172
236173 return pLvl->bEof;
236174}
236175
236176/*
236177** Advance the iterator passed as the only argument.
236178*/
236179static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
236180 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
236181
236182 assert( iLvl<pIter->nLvl );
236183 if( fts5DlidxLvlNext(pLvl) ){
236184 if( (iLvl+1) < pIter->nLvl ){
236185 fts5DlidxIterNextR(p, pIter, iLvl: iLvl+1);
236186 if( pLvl[1].bEof==0 ){
236187 fts5DataRelease(pData: pLvl->pData);
236188 memset(s: pLvl, c: 0, n: sizeof(Fts5DlidxLvl));
236189 pLvl->pData = fts5DataRead(p,
236190 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
236191 );
236192 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
236193 }
236194 }
236195 }
236196
236197 return pIter->aLvl[0].bEof;
236198}
236199static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
236200 return fts5DlidxIterNextR(p, pIter, iLvl: 0);
236201}
236202
236203/*
236204** The iterator passed as the first argument has the following fields set
236205** as follows. This function sets up the rest of the iterator so that it
236206** points to the first rowid in the doclist-index.
236207**
236208** pData:
236209** pointer to doclist-index record,
236210**
236211** When this function is called pIter->iLeafPgno is the page number the
236212** doclist is associated with (the one featuring the term).
236213*/
236214static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
236215 int i;
236216 for(i=0; i<pIter->nLvl; i++){
236217 fts5DlidxLvlNext(pLvl: &pIter->aLvl[i]);
236218 }
236219 return pIter->aLvl[0].bEof;
236220}
236221
236222
236223static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
236224 return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
236225}
236226
236227static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
236228 int i;
236229
236230 /* Advance each level to the last entry on the last page */
236231 for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
236232 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
236233 while( fts5DlidxLvlNext(pLvl)==0 );
236234 pLvl->bEof = 0;
236235
236236 if( i>0 ){
236237 Fts5DlidxLvl *pChild = &pLvl[-1];
236238 fts5DataRelease(pData: pChild->pData);
236239 memset(s: pChild, c: 0, n: sizeof(Fts5DlidxLvl));
236240 pChild->pData = fts5DataRead(p,
236241 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
236242 );
236243 }
236244 }
236245}
236246
236247/*
236248** Move the iterator passed as the only argument to the previous entry.
236249*/
236250static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
236251 int iOff = pLvl->iOff;
236252
236253 assert( pLvl->bEof==0 );
236254 if( iOff<=pLvl->iFirstOff ){
236255 pLvl->bEof = 1;
236256 }else{
236257 u8 *a = pLvl->pData->p;
236258
236259 pLvl->iOff = 0;
236260 fts5DlidxLvlNext(pLvl);
236261 while( 1 ){
236262 int nZero = 0;
236263 int ii = pLvl->iOff;
236264 u64 delta = 0;
236265
236266 while( a[ii]==0 ){
236267 nZero++;
236268 ii++;
236269 }
236270 ii += sqlite3Fts5GetVarint(&a[ii], &delta);
236271
236272 if( ii>=iOff ) break;
236273 pLvl->iLeafPgno += nZero+1;
236274 pLvl->iRowid += delta;
236275 pLvl->iOff = ii;
236276 }
236277 }
236278
236279 return pLvl->bEof;
236280}
236281
236282static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
236283 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
236284
236285 assert( iLvl<pIter->nLvl );
236286 if( fts5DlidxLvlPrev(pLvl) ){
236287 if( (iLvl+1) < pIter->nLvl ){
236288 fts5DlidxIterPrevR(p, pIter, iLvl: iLvl+1);
236289 if( pLvl[1].bEof==0 ){
236290 fts5DataRelease(pData: pLvl->pData);
236291 memset(s: pLvl, c: 0, n: sizeof(Fts5DlidxLvl));
236292 pLvl->pData = fts5DataRead(p,
236293 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
236294 );
236295 if( pLvl->pData ){
236296 while( fts5DlidxLvlNext(pLvl)==0 );
236297 pLvl->bEof = 0;
236298 }
236299 }
236300 }
236301 }
236302
236303 return pIter->aLvl[0].bEof;
236304}
236305static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
236306 return fts5DlidxIterPrevR(p, pIter, iLvl: 0);
236307}
236308
236309/*
236310** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
236311*/
236312static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
236313 if( pIter ){
236314 int i;
236315 for(i=0; i<pIter->nLvl; i++){
236316 fts5DataRelease(pData: pIter->aLvl[i].pData);
236317 }
236318 sqlite3_free(p: pIter);
236319 }
236320}
236321
236322static Fts5DlidxIter *fts5DlidxIterInit(
236323 Fts5Index *p, /* Fts5 Backend to iterate within */
236324 int bRev, /* True for ORDER BY ASC */
236325 int iSegid, /* Segment id */
236326 int iLeafPg /* Leaf page number to load dlidx for */
236327){
236328 Fts5DlidxIter *pIter = 0;
236329 int i;
236330 int bDone = 0;
236331
236332 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
236333 sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
236334 Fts5DlidxIter *pNew;
236335
236336 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pOld: pIter, n: nByte);
236337 if( pNew==0 ){
236338 p->rc = SQLITE_NOMEM;
236339 }else{
236340 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
236341 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
236342 pIter = pNew;
236343 memset(s: pLvl, c: 0, n: sizeof(Fts5DlidxLvl));
236344 pLvl->pData = fts5DataRead(p, iRowid);
236345 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
236346 bDone = 1;
236347 }
236348 pIter->nLvl = i+1;
236349 }
236350 }
236351
236352 if( p->rc==SQLITE_OK ){
236353 pIter->iSegid = iSegid;
236354 if( bRev==0 ){
236355 fts5DlidxIterFirst(pIter);
236356 }else{
236357 fts5DlidxIterLast(p, pIter);
236358 }
236359 }
236360
236361 if( p->rc!=SQLITE_OK ){
236362 fts5DlidxIterFree(pIter);
236363 pIter = 0;
236364 }
236365
236366 return pIter;
236367}
236368
236369static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
236370 return pIter->aLvl[0].iRowid;
236371}
236372static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
236373 return pIter->aLvl[0].iLeafPgno;
236374}
236375
236376/*
236377** Load the next leaf page into the segment iterator.
236378*/
236379static void fts5SegIterNextPage(
236380 Fts5Index *p, /* FTS5 backend object */
236381 Fts5SegIter *pIter /* Iterator to advance to next page */
236382){
236383 Fts5Data *pLeaf;
236384 Fts5StructureSegment *pSeg = pIter->pSeg;
236385 fts5DataRelease(pData: pIter->pLeaf);
236386 pIter->iLeafPgno++;
236387 if( pIter->pNextLeaf ){
236388 pIter->pLeaf = pIter->pNextLeaf;
236389 pIter->pNextLeaf = 0;
236390 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
236391 pIter->pLeaf = fts5LeafRead(p,
236392 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
236393 );
236394 }else{
236395 pIter->pLeaf = 0;
236396 }
236397 pLeaf = pIter->pLeaf;
236398
236399 if( pLeaf ){
236400 pIter->iPgidxOff = pLeaf->szLeaf;
236401 if( fts5LeafIsTermless(pLeaf) ){
236402 pIter->iEndofDoclist = pLeaf->nn+1;
236403 }else{
236404 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
236405 pIter->iEndofDoclist
236406 );
236407 }
236408 }
236409}
236410
236411/*
236412** Argument p points to a buffer containing a varint to be interpreted as a
236413** position list size field. Read the varint and return the number of bytes
236414** read. Before returning, set *pnSz to the number of bytes in the position
236415** list, and *pbDel to true if the delete flag is set, or false otherwise.
236416*/
236417static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
236418 int nSz;
236419 int n = 0;
236420 fts5FastGetVarint32(p, n, nSz);
236421 assert_nc( nSz>=0 );
236422 *pnSz = nSz/2;
236423 *pbDel = nSz & 0x0001;
236424 return n;
236425}
236426
236427/*
236428** Fts5SegIter.iLeafOffset currently points to the first byte of a
236429** position-list size field. Read the value of the field and store it
236430** in the following variables:
236431**
236432** Fts5SegIter.nPos
236433** Fts5SegIter.bDel
236434**
236435** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
236436** position list content (if any).
236437*/
236438static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
236439 if( p->rc==SQLITE_OK ){
236440 int iOff = pIter->iLeafOffset; /* Offset to read at */
236441 ASSERT_SZLEAF_OK(pIter->pLeaf);
236442 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
236443 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
236444 pIter->bDel = 0;
236445 pIter->nPos = 1;
236446 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
236447 pIter->bDel = 1;
236448 iOff++;
236449 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
236450 pIter->nPos = 1;
236451 iOff++;
236452 }else{
236453 pIter->nPos = 0;
236454 }
236455 }
236456 }else{
236457 int nSz;
236458 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
236459 pIter->bDel = (nSz & 0x0001);
236460 pIter->nPos = nSz>>1;
236461 assert_nc( pIter->nPos>=0 );
236462 }
236463 pIter->iLeafOffset = iOff;
236464 }
236465}
236466
236467static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
236468 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
236469 i64 iOff = pIter->iLeafOffset;
236470
236471 ASSERT_SZLEAF_OK(pIter->pLeaf);
236472 while( iOff>=pIter->pLeaf->szLeaf ){
236473 fts5SegIterNextPage(p, pIter);
236474 if( pIter->pLeaf==0 ){
236475 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
236476 return;
236477 }
236478 iOff = 4;
236479 a = pIter->pLeaf->p;
236480 }
236481 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
236482 pIter->iLeafOffset = iOff;
236483}
236484
236485/*
236486** Fts5SegIter.iLeafOffset currently points to the first byte of the
236487** "nSuffix" field of a term. Function parameter nKeep contains the value
236488** of the "nPrefix" field (if there was one - it is passed 0 if this is
236489** the first term in the segment).
236490**
236491** This function populates:
236492**
236493** Fts5SegIter.term
236494** Fts5SegIter.rowid
236495**
236496** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
236497** the first position list. The position list belonging to document
236498** (Fts5SegIter.iRowid).
236499*/
236500static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
236501 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
236502 i64 iOff = pIter->iLeafOffset; /* Offset to read at */
236503 int nNew; /* Bytes of new data */
236504
236505 iOff += fts5GetVarint32(&a[iOff], nNew);
236506 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
236507 p->rc = FTS5_CORRUPT;
236508 return;
236509 }
236510 pIter->term.n = nKeep;
236511 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
236512 assert( pIter->term.n<=pIter->term.nSpace );
236513 iOff += nNew;
236514 pIter->iTermLeafOffset = iOff;
236515 pIter->iTermLeafPgno = pIter->iLeafPgno;
236516 pIter->iLeafOffset = iOff;
236517
236518 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
236519 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
236520 }else{
236521 int nExtra;
236522 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
236523 pIter->iEndofDoclist += nExtra;
236524 }
236525
236526 fts5SegIterLoadRowid(p, pIter);
236527}
236528
236529static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
236530static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
236531static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
236532
236533static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
236534 if( pIter->flags & FTS5_SEGITER_REVERSE ){
236535 pIter->xNext = fts5SegIterNext_Reverse;
236536 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
236537 pIter->xNext = fts5SegIterNext_None;
236538 }else{
236539 pIter->xNext = fts5SegIterNext;
236540 }
236541}
236542
236543/*
236544** Allocate a tombstone hash page array (pIter->apTombstone) for the
236545** iterator passed as the second argument. If an OOM error occurs, leave
236546** an error in the Fts5Index object.
236547*/
236548static void fts5SegIterAllocTombstone(Fts5Index *p, Fts5SegIter *pIter){
236549 const int nTomb = pIter->pSeg->nPgTombstone;
236550 if( nTomb>0 ){
236551 Fts5Data **apTomb = 0;
236552 apTomb = (Fts5Data**)sqlite3Fts5MallocZero(pRc: &p->rc, nByte: sizeof(Fts5Data)*nTomb);
236553 if( apTomb ){
236554 pIter->apTombstone = apTomb;
236555 pIter->nTombstone = nTomb;
236556 }
236557 }
236558}
236559
236560/*
236561** Initialize the iterator object pIter to iterate through the entries in
236562** segment pSeg. The iterator is left pointing to the first entry when
236563** this function returns.
236564**
236565** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
236566** an error has already occurred when this function is called, it is a no-op.
236567*/
236568static void fts5SegIterInit(
236569 Fts5Index *p, /* FTS index object */
236570 Fts5StructureSegment *pSeg, /* Description of segment */
236571 Fts5SegIter *pIter /* Object to populate */
236572){
236573 if( pSeg->pgnoFirst==0 ){
236574 /* This happens if the segment is being used as an input to an incremental
236575 ** merge and all data has already been "trimmed". See function
236576 ** fts5TrimSegments() for details. In this case leave the iterator empty.
236577 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
236578 ** at EOF already. */
236579 assert( pIter->pLeaf==0 );
236580 return;
236581 }
236582
236583 if( p->rc==SQLITE_OK ){
236584 memset(s: pIter, c: 0, n: sizeof(*pIter));
236585 fts5SegIterSetNext(p, pIter);
236586 pIter->pSeg = pSeg;
236587 pIter->iLeafPgno = pSeg->pgnoFirst-1;
236588 do {
236589 fts5SegIterNextPage(p, pIter);
236590 }while( p->rc==SQLITE_OK && pIter->pLeaf && pIter->pLeaf->nn==4 );
236591 }
236592
236593 if( p->rc==SQLITE_OK && pIter->pLeaf ){
236594 pIter->iLeafOffset = 4;
236595 assert( pIter->pLeaf!=0 );
236596 assert_nc( pIter->pLeaf->nn>4 );
236597 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
236598 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
236599 fts5SegIterLoadTerm(p, pIter, nKeep: 0);
236600 fts5SegIterLoadNPos(p, pIter);
236601 fts5SegIterAllocTombstone(p, pIter);
236602 }
236603}
236604
236605/*
236606** This function is only ever called on iterators created by calls to
236607** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
236608**
236609** The iterator is in an unusual state when this function is called: the
236610** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
236611** the position-list size field for the first relevant rowid on the page.
236612** Fts5SegIter.rowid is set, but nPos and bDel are not.
236613**
236614** This function advances the iterator so that it points to the last
236615** relevant rowid on the page and, if necessary, initializes the
236616** aRowidOffset[] and iRowidOffset variables. At this point the iterator
236617** is in its regular state - Fts5SegIter.iLeafOffset points to the first
236618** byte of the position list content associated with said rowid.
236619*/
236620static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
236621 int eDetail = p->pConfig->eDetail;
236622 int n = pIter->pLeaf->szLeaf;
236623 int i = pIter->iLeafOffset;
236624 u8 *a = pIter->pLeaf->p;
236625 int iRowidOffset = 0;
236626
236627 if( n>pIter->iEndofDoclist ){
236628 n = pIter->iEndofDoclist;
236629 }
236630
236631 ASSERT_SZLEAF_OK(pIter->pLeaf);
236632 while( 1 ){
236633 u64 iDelta = 0;
236634
236635 if( eDetail==FTS5_DETAIL_NONE ){
236636 /* todo */
236637 if( i<n && a[i]==0 ){
236638 i++;
236639 if( i<n && a[i]==0 ) i++;
236640 }
236641 }else{
236642 int nPos;
236643 int bDummy;
236644 i += fts5GetPoslistSize(p: &a[i], pnSz: &nPos, pbDel: &bDummy);
236645 i += nPos;
236646 }
236647 if( i>=n ) break;
236648 i += fts5GetVarint(&a[i], &iDelta);
236649 pIter->iRowid += iDelta;
236650
236651 /* If necessary, grow the pIter->aRowidOffset[] array. */
236652 if( iRowidOffset>=pIter->nRowidOffset ){
236653 int nNew = pIter->nRowidOffset + 8;
236654 int *aNew = (int*)sqlite3_realloc64(pOld: pIter->aRowidOffset,n: nNew*sizeof(int));
236655 if( aNew==0 ){
236656 p->rc = SQLITE_NOMEM;
236657 break;
236658 }
236659 pIter->aRowidOffset = aNew;
236660 pIter->nRowidOffset = nNew;
236661 }
236662
236663 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
236664 pIter->iLeafOffset = i;
236665 }
236666 pIter->iRowidOffset = iRowidOffset;
236667 fts5SegIterLoadNPos(p, pIter);
236668}
236669
236670/*
236671**
236672*/
236673static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
236674 assert( pIter->flags & FTS5_SEGITER_REVERSE );
236675 assert( pIter->flags & FTS5_SEGITER_ONETERM );
236676
236677 fts5DataRelease(pData: pIter->pLeaf);
236678 pIter->pLeaf = 0;
236679 while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
236680 Fts5Data *pNew;
236681 pIter->iLeafPgno--;
236682 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
236683 pIter->pSeg->iSegid, pIter->iLeafPgno
236684 ));
236685 if( pNew ){
236686 /* iTermLeafOffset may be equal to szLeaf if the term is the last
236687 ** thing on the page - i.e. the first rowid is on the following page.
236688 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
236689 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
236690 assert( pIter->pLeaf==0 );
236691 if( pIter->iTermLeafOffset<pNew->szLeaf ){
236692 pIter->pLeaf = pNew;
236693 pIter->iLeafOffset = pIter->iTermLeafOffset;
236694 }
236695 }else{
236696 int iRowidOff;
236697 iRowidOff = fts5LeafFirstRowidOff(pNew);
236698 if( iRowidOff ){
236699 if( iRowidOff>=pNew->szLeaf ){
236700 p->rc = FTS5_CORRUPT;
236701 }else{
236702 pIter->pLeaf = pNew;
236703 pIter->iLeafOffset = iRowidOff;
236704 }
236705 }
236706 }
236707
236708 if( pIter->pLeaf ){
236709 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
236710 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
236711 break;
236712 }else{
236713 fts5DataRelease(pData: pNew);
236714 }
236715 }
236716 }
236717
236718 if( pIter->pLeaf ){
236719 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
236720 fts5SegIterReverseInitPage(p, pIter);
236721 }
236722}
236723
236724/*
236725** Return true if the iterator passed as the second argument currently
236726** points to a delete marker. A delete marker is an entry with a 0 byte
236727** position-list.
236728*/
236729static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
236730 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
236731 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
236732}
236733
236734/*
236735** Advance iterator pIter to the next entry.
236736**
236737** This version of fts5SegIterNext() is only used by reverse iterators.
236738*/
236739static void fts5SegIterNext_Reverse(
236740 Fts5Index *p, /* FTS5 backend object */
236741 Fts5SegIter *pIter, /* Iterator to advance */
236742 int *pbUnused /* Unused */
236743){
236744 assert( pIter->flags & FTS5_SEGITER_REVERSE );
236745 assert( pIter->pNextLeaf==0 );
236746 UNUSED_PARAM(pbUnused);
236747
236748 if( pIter->iRowidOffset>0 ){
236749 u8 *a = pIter->pLeaf->p;
236750 int iOff;
236751 u64 iDelta;
236752
236753 pIter->iRowidOffset--;
236754 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
236755 fts5SegIterLoadNPos(p, pIter);
236756 iOff = pIter->iLeafOffset;
236757 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
236758 iOff += pIter->nPos;
236759 }
236760 fts5GetVarint(&a[iOff], &iDelta);
236761 pIter->iRowid -= iDelta;
236762 }else{
236763 fts5SegIterReverseNewPage(p, pIter);
236764 }
236765}
236766
236767/*
236768** Advance iterator pIter to the next entry.
236769**
236770** This version of fts5SegIterNext() is only used if detail=none and the
236771** iterator is not a reverse direction iterator.
236772*/
236773static void fts5SegIterNext_None(
236774 Fts5Index *p, /* FTS5 backend object */
236775 Fts5SegIter *pIter, /* Iterator to advance */
236776 int *pbNewTerm /* OUT: Set for new term */
236777){
236778 int iOff;
236779
236780 assert( p->rc==SQLITE_OK );
236781 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
236782 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
236783
236784 ASSERT_SZLEAF_OK(pIter->pLeaf);
236785 iOff = pIter->iLeafOffset;
236786
236787 /* Next entry is on the next page */
236788 while( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
236789 fts5SegIterNextPage(p, pIter);
236790 if( p->rc || pIter->pLeaf==0 ) return;
236791 pIter->iRowid = 0;
236792 iOff = 4;
236793 }
236794
236795 if( iOff<pIter->iEndofDoclist ){
236796 /* Next entry is on the current page */
236797 i64 iDelta;
236798 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
236799 pIter->iLeafOffset = iOff;
236800 pIter->iRowid += iDelta;
236801 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
236802 if( pIter->pSeg ){
236803 int nKeep = 0;
236804 if( iOff!=fts5LeafFirstTermOff(pLeaf: pIter->pLeaf) ){
236805 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
236806 }
236807 pIter->iLeafOffset = iOff;
236808 fts5SegIterLoadTerm(p, pIter, nKeep);
236809 }else{
236810 const u8 *pList = 0;
236811 const char *zTerm = 0;
236812 int nList;
236813 sqlite3Fts5HashScanNext(p: p->pHash);
236814 sqlite3Fts5HashScanEntry(pHash: p->pHash, pzTerm: &zTerm, ppDoclist: &pList, pnDoclist: &nList);
236815 if( pList==0 ) goto next_none_eof;
236816 pIter->pLeaf->p = (u8*)pList;
236817 pIter->pLeaf->nn = nList;
236818 pIter->pLeaf->szLeaf = nList;
236819 pIter->iEndofDoclist = nList;
236820 sqlite3Fts5BufferSet(pRc: &p->rc,pBuf: &pIter->term, nData: (int)strlen(s: zTerm), pData: (u8*)zTerm);
236821 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
236822 }
236823
236824 if( pbNewTerm ) *pbNewTerm = 1;
236825 }else{
236826 goto next_none_eof;
236827 }
236828
236829 fts5SegIterLoadNPos(p, pIter);
236830
236831 return;
236832 next_none_eof:
236833 fts5DataRelease(pData: pIter->pLeaf);
236834 pIter->pLeaf = 0;
236835}
236836
236837
236838/*
236839** Advance iterator pIter to the next entry.
236840**
236841** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
236842** is not considered an error if the iterator reaches EOF. If an error has
236843** already occurred when this function is called, it is a no-op.
236844*/
236845static void fts5SegIterNext(
236846 Fts5Index *p, /* FTS5 backend object */
236847 Fts5SegIter *pIter, /* Iterator to advance */
236848 int *pbNewTerm /* OUT: Set for new term */
236849){
236850 Fts5Data *pLeaf = pIter->pLeaf;
236851 int iOff;
236852 int bNewTerm = 0;
236853 int nKeep = 0;
236854 u8 *a;
236855 int n;
236856
236857 assert( pbNewTerm==0 || *pbNewTerm==0 );
236858 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
236859
236860 /* Search for the end of the position list within the current page. */
236861 a = pLeaf->p;
236862 n = pLeaf->szLeaf;
236863
236864 ASSERT_SZLEAF_OK(pLeaf);
236865 iOff = pIter->iLeafOffset + pIter->nPos;
236866
236867 if( iOff<n ){
236868 /* The next entry is on the current page. */
236869 assert_nc( iOff<=pIter->iEndofDoclist );
236870 if( iOff>=pIter->iEndofDoclist ){
236871 bNewTerm = 1;
236872 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
236873 iOff += fts5GetVarint32(&a[iOff], nKeep);
236874 }
236875 }else{
236876 u64 iDelta;
236877 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
236878 pIter->iRowid += iDelta;
236879 assert_nc( iDelta>0 );
236880 }
236881 pIter->iLeafOffset = iOff;
236882
236883 }else if( pIter->pSeg==0 ){
236884 const u8 *pList = 0;
236885 const char *zTerm = 0;
236886 int nList = 0;
236887 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
236888 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
236889 sqlite3Fts5HashScanNext(p: p->pHash);
236890 sqlite3Fts5HashScanEntry(pHash: p->pHash, pzTerm: &zTerm, ppDoclist: &pList, pnDoclist: &nList);
236891 }
236892 if( pList==0 ){
236893 fts5DataRelease(pData: pIter->pLeaf);
236894 pIter->pLeaf = 0;
236895 }else{
236896 pIter->pLeaf->p = (u8*)pList;
236897 pIter->pLeaf->nn = nList;
236898 pIter->pLeaf->szLeaf = nList;
236899 pIter->iEndofDoclist = nList+1;
236900 sqlite3Fts5BufferSet(pRc: &p->rc, pBuf: &pIter->term, nData: (int)strlen(s: zTerm),
236901 pData: (u8*)zTerm);
236902 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
236903 *pbNewTerm = 1;
236904 }
236905 }else{
236906 iOff = 0;
236907 /* Next entry is not on the current page */
236908 while( iOff==0 ){
236909 fts5SegIterNextPage(p, pIter);
236910 pLeaf = pIter->pLeaf;
236911 if( pLeaf==0 ) break;
236912 ASSERT_SZLEAF_OK(pLeaf);
236913 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
236914 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
236915 pIter->iLeafOffset = iOff;
236916
236917 if( pLeaf->nn>pLeaf->szLeaf ){
236918 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
236919 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
236920 );
236921 }
236922 }
236923 else if( pLeaf->nn>pLeaf->szLeaf ){
236924 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
236925 &pLeaf->p[pLeaf->szLeaf], iOff
236926 );
236927 pIter->iLeafOffset = iOff;
236928 pIter->iEndofDoclist = iOff;
236929 bNewTerm = 1;
236930 }
236931 assert_nc( iOff<pLeaf->szLeaf );
236932 if( iOff>pLeaf->szLeaf ){
236933 p->rc = FTS5_CORRUPT;
236934 return;
236935 }
236936 }
236937 }
236938
236939 /* Check if the iterator is now at EOF. If so, return early. */
236940 if( pIter->pLeaf ){
236941 if( bNewTerm ){
236942 if( pIter->flags & FTS5_SEGITER_ONETERM ){
236943 fts5DataRelease(pData: pIter->pLeaf);
236944 pIter->pLeaf = 0;
236945 }else{
236946 fts5SegIterLoadTerm(p, pIter, nKeep);
236947 fts5SegIterLoadNPos(p, pIter);
236948 if( pbNewTerm ) *pbNewTerm = 1;
236949 }
236950 }else{
236951 /* The following could be done by calling fts5SegIterLoadNPos(). But
236952 ** this block is particularly performance critical, so equivalent
236953 ** code is inlined. */
236954 int nSz;
236955 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn );
236956 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
236957 pIter->bDel = (nSz & 0x0001);
236958 pIter->nPos = nSz>>1;
236959 assert_nc( pIter->nPos>=0 );
236960 }
236961 }
236962}
236963
236964#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
236965
236966#define fts5IndexSkipVarint(a, iOff) { \
236967 int iEnd = iOff+9; \
236968 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
236969}
236970
236971/*
236972** Iterator pIter currently points to the first rowid in a doclist. This
236973** function sets the iterator up so that iterates in reverse order through
236974** the doclist.
236975*/
236976static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
236977 Fts5DlidxIter *pDlidx = pIter->pDlidx;
236978 Fts5Data *pLast = 0;
236979 int pgnoLast = 0;
236980
236981 if( pDlidx && p->pConfig->iVersion==FTS5_CURRENT_VERSION ){
236982 int iSegid = pIter->pSeg->iSegid;
236983 pgnoLast = fts5DlidxIterPgno(pIter: pDlidx);
236984 pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
236985 }else{
236986 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
236987
236988 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
236989 ** position-list content for the current rowid. Back it up so that it
236990 ** points to the start of the position-list size field. */
236991 int iPoslist;
236992 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
236993 iPoslist = pIter->iTermLeafOffset;
236994 }else{
236995 iPoslist = 4;
236996 }
236997 fts5IndexSkipVarint(pLeaf->p, iPoslist);
236998 pIter->iLeafOffset = iPoslist;
236999
237000 /* If this condition is true then the largest rowid for the current
237001 ** term may not be stored on the current page. So search forward to
237002 ** see where said rowid really is. */
237003 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
237004 int pgno;
237005 Fts5StructureSegment *pSeg = pIter->pSeg;
237006
237007 /* The last rowid in the doclist may not be on the current page. Search
237008 ** forward to find the page containing the last rowid. */
237009 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
237010 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
237011 Fts5Data *pNew = fts5LeafRead(p, iRowid: iAbs);
237012 if( pNew ){
237013 int iRowid, bTermless;
237014 iRowid = fts5LeafFirstRowidOff(pNew);
237015 bTermless = fts5LeafIsTermless(pNew);
237016 if( iRowid ){
237017 SWAPVAL(Fts5Data*, pNew, pLast);
237018 pgnoLast = pgno;
237019 }
237020 fts5DataRelease(pData: pNew);
237021 if( bTermless==0 ) break;
237022 }
237023 }
237024 }
237025 }
237026
237027 /* If pLast is NULL at this point, then the last rowid for this doclist
237028 ** lies on the page currently indicated by the iterator. In this case
237029 ** pIter->iLeafOffset is already set to point to the position-list size
237030 ** field associated with the first relevant rowid on the page.
237031 **
237032 ** Or, if pLast is non-NULL, then it is the page that contains the last
237033 ** rowid. In this case configure the iterator so that it points to the
237034 ** first rowid on this page.
237035 */
237036 if( pLast ){
237037 int iOff;
237038 fts5DataRelease(pData: pIter->pLeaf);
237039 pIter->pLeaf = pLast;
237040 pIter->iLeafPgno = pgnoLast;
237041 iOff = fts5LeafFirstRowidOff(pLast);
237042 if( iOff>pLast->szLeaf ){
237043 p->rc = FTS5_CORRUPT;
237044 return;
237045 }
237046 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
237047 pIter->iLeafOffset = iOff;
237048
237049 if( fts5LeafIsTermless(pLast) ){
237050 pIter->iEndofDoclist = pLast->nn+1;
237051 }else{
237052 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLeaf: pLast);
237053 }
237054 }
237055
237056 fts5SegIterReverseInitPage(p, pIter);
237057}
237058
237059/*
237060** Iterator pIter currently points to the first rowid of a doclist.
237061** There is a doclist-index associated with the final term on the current
237062** page. If the current term is the last term on the page, load the
237063** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
237064*/
237065static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
237066 int iSeg = pIter->pSeg->iSegid;
237067 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
237068 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
237069
237070 assert( pIter->flags & FTS5_SEGITER_ONETERM );
237071 assert( pIter->pDlidx==0 );
237072
237073 /* Check if the current doclist ends on this page. If it does, return
237074 ** early without loading the doclist-index (as it belongs to a different
237075 ** term. */
237076 if( pIter->iTermLeafPgno==pIter->iLeafPgno
237077 && pIter->iEndofDoclist<pLeaf->szLeaf
237078 ){
237079 return;
237080 }
237081
237082 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSegid: iSeg, iLeafPg: pIter->iTermLeafPgno);
237083}
237084
237085/*
237086** The iterator object passed as the second argument currently contains
237087** no valid values except for the Fts5SegIter.pLeaf member variable. This
237088** function searches the leaf page for a term matching (pTerm/nTerm).
237089**
237090** If the specified term is found on the page, then the iterator is left
237091** pointing to it. If argument bGe is zero and the term is not found,
237092** the iterator is left pointing at EOF.
237093**
237094** If bGe is non-zero and the specified term is not found, then the
237095** iterator is left pointing to the smallest term in the segment that
237096** is larger than the specified term, even if this term is not on the
237097** current page.
237098*/
237099static void fts5LeafSeek(
237100 Fts5Index *p, /* Leave any error code here */
237101 int bGe, /* True for a >= search */
237102 Fts5SegIter *pIter, /* Iterator to seek */
237103 const u8 *pTerm, int nTerm /* Term to search for */
237104){
237105 u32 iOff;
237106 const u8 *a = pIter->pLeaf->p;
237107 u32 n = (u32)pIter->pLeaf->nn;
237108
237109 u32 nMatch = 0;
237110 u32 nKeep = 0;
237111 u32 nNew = 0;
237112 u32 iTermOff;
237113 u32 iPgidx; /* Current offset in pgidx */
237114 int bEndOfPage = 0;
237115
237116 assert( p->rc==SQLITE_OK );
237117
237118 iPgidx = (u32)pIter->pLeaf->szLeaf;
237119 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
237120 iOff = iTermOff;
237121 if( iOff>n ){
237122 p->rc = FTS5_CORRUPT;
237123 return;
237124 }
237125
237126 while( 1 ){
237127
237128 /* Figure out how many new bytes are in this term */
237129 fts5FastGetVarint32(a, iOff, nNew);
237130 if( nKeep<nMatch ){
237131 goto search_failed;
237132 }
237133
237134 assert( nKeep>=nMatch );
237135 if( nKeep==nMatch ){
237136 u32 nCmp;
237137 u32 i;
237138 nCmp = (u32)MIN(nNew, nTerm-nMatch);
237139 for(i=0; i<nCmp; i++){
237140 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
237141 }
237142 nMatch += i;
237143
237144 if( (u32)nTerm==nMatch ){
237145 if( i==nNew ){
237146 goto search_success;
237147 }else{
237148 goto search_failed;
237149 }
237150 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
237151 goto search_failed;
237152 }
237153 }
237154
237155 if( iPgidx>=n ){
237156 bEndOfPage = 1;
237157 break;
237158 }
237159
237160 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
237161 iTermOff += nKeep;
237162 iOff = iTermOff;
237163
237164 if( iOff>=n ){
237165 p->rc = FTS5_CORRUPT;
237166 return;
237167 }
237168
237169 /* Read the nKeep field of the next term. */
237170 fts5FastGetVarint32(a, iOff, nKeep);
237171 }
237172
237173 search_failed:
237174 if( bGe==0 ){
237175 fts5DataRelease(pData: pIter->pLeaf);
237176 pIter->pLeaf = 0;
237177 return;
237178 }else if( bEndOfPage ){
237179 do {
237180 fts5SegIterNextPage(p, pIter);
237181 if( pIter->pLeaf==0 ) return;
237182 a = pIter->pLeaf->p;
237183 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
237184 iPgidx = (u32)pIter->pLeaf->szLeaf;
237185 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
237186 if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){
237187 p->rc = FTS5_CORRUPT;
237188 return;
237189 }else{
237190 nKeep = 0;
237191 iTermOff = iOff;
237192 n = (u32)pIter->pLeaf->nn;
237193 iOff += fts5GetVarint32(&a[iOff], nNew);
237194 break;
237195 }
237196 }
237197 }while( 1 );
237198 }
237199
237200 search_success:
237201 if( (i64)iOff+nNew>n || nNew<1 ){
237202 p->rc = FTS5_CORRUPT;
237203 return;
237204 }
237205 pIter->iLeafOffset = iOff + nNew;
237206 pIter->iTermLeafOffset = pIter->iLeafOffset;
237207 pIter->iTermLeafPgno = pIter->iLeafPgno;
237208
237209 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
237210 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
237211
237212 if( iPgidx>=n ){
237213 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
237214 }else{
237215 int nExtra;
237216 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
237217 pIter->iEndofDoclist = iTermOff + nExtra;
237218 }
237219 pIter->iPgidxOff = iPgidx;
237220
237221 fts5SegIterLoadRowid(p, pIter);
237222 fts5SegIterLoadNPos(p, pIter);
237223}
237224
237225static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
237226 if( p->pIdxSelect==0 ){
237227 Fts5Config *pConfig = p->pConfig;
237228 fts5IndexPrepareStmt(p, ppStmt: &p->pIdxSelect, zSql: sqlite3_mprintf(
237229 zFormat: "SELECT pgno FROM '%q'.'%q_idx' WHERE "
237230 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
237231 pConfig->zDb, pConfig->zName
237232 ));
237233 }
237234 return p->pIdxSelect;
237235}
237236
237237/*
237238** Initialize the object pIter to point to term pTerm/nTerm within segment
237239** pSeg. If there is no such term in the index, the iterator is set to EOF.
237240**
237241** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
237242** an error has already occurred when this function is called, it is a no-op.
237243*/
237244static void fts5SegIterSeekInit(
237245 Fts5Index *p, /* FTS5 backend */
237246 const u8 *pTerm, int nTerm, /* Term to seek to */
237247 int flags, /* Mask of FTS5INDEX_XXX flags */
237248 Fts5StructureSegment *pSeg, /* Description of segment */
237249 Fts5SegIter *pIter /* Object to populate */
237250){
237251 int iPg = 1;
237252 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
237253 int bDlidx = 0; /* True if there is a doclist-index */
237254 sqlite3_stmt *pIdxSelect = 0;
237255
237256 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
237257 assert( pTerm && nTerm );
237258 memset(s: pIter, c: 0, n: sizeof(*pIter));
237259 pIter->pSeg = pSeg;
237260
237261 /* This block sets stack variable iPg to the leaf page number that may
237262 ** contain term (pTerm/nTerm), if it is present in the segment. */
237263 pIdxSelect = fts5IdxSelectStmt(p);
237264 if( p->rc ) return;
237265 sqlite3_bind_int(p: pIdxSelect, i: 1, iValue: pSeg->iSegid);
237266 sqlite3_bind_blob(pStmt: pIdxSelect, i: 2, zData: pTerm, nData: nTerm, SQLITE_STATIC);
237267 if( SQLITE_ROW==sqlite3_step(pStmt: pIdxSelect) ){
237268 i64 val = sqlite3_column_int(pStmt: pIdxSelect, i: 0);
237269 iPg = (int)(val>>1);
237270 bDlidx = (val & 0x0001);
237271 }
237272 p->rc = sqlite3_reset(pStmt: pIdxSelect);
237273 sqlite3_bind_null(pStmt: pIdxSelect, i: 2);
237274
237275 if( iPg<pSeg->pgnoFirst ){
237276 iPg = pSeg->pgnoFirst;
237277 bDlidx = 0;
237278 }
237279
237280 pIter->iLeafPgno = iPg - 1;
237281 fts5SegIterNextPage(p, pIter);
237282
237283 if( pIter->pLeaf ){
237284 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
237285 }
237286
237287 if( p->rc==SQLITE_OK && bGe==0 ){
237288 pIter->flags |= FTS5_SEGITER_ONETERM;
237289 if( pIter->pLeaf ){
237290 if( flags & FTS5INDEX_QUERY_DESC ){
237291 pIter->flags |= FTS5_SEGITER_REVERSE;
237292 }
237293 if( bDlidx ){
237294 fts5SegIterLoadDlidx(p, pIter);
237295 }
237296 if( flags & FTS5INDEX_QUERY_DESC ){
237297 fts5SegIterReverse(p, pIter);
237298 }
237299 }
237300 }
237301
237302 fts5SegIterSetNext(p, pIter);
237303 fts5SegIterAllocTombstone(p, pIter);
237304
237305 /* Either:
237306 **
237307 ** 1) an error has occurred, or
237308 ** 2) the iterator points to EOF, or
237309 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
237310 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
237311 ** to an entry with a term greater than or equal to (pTerm/nTerm).
237312 */
237313 assert_nc( p->rc!=SQLITE_OK /* 1 */
237314 || pIter->pLeaf==0 /* 2 */
237315 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
237316 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
237317 );
237318}
237319
237320/*
237321** Initialize the object pIter to point to term pTerm/nTerm within the
237322** in-memory hash table. If there is no such term in the hash-table, the
237323** iterator is set to EOF.
237324**
237325** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
237326** an error has already occurred when this function is called, it is a no-op.
237327*/
237328static void fts5SegIterHashInit(
237329 Fts5Index *p, /* FTS5 backend */
237330 const u8 *pTerm, int nTerm, /* Term to seek to */
237331 int flags, /* Mask of FTS5INDEX_XXX flags */
237332 Fts5SegIter *pIter /* Object to populate */
237333){
237334 int nList = 0;
237335 const u8 *z = 0;
237336 int n = 0;
237337 Fts5Data *pLeaf = 0;
237338
237339 assert( p->pHash );
237340 assert( p->rc==SQLITE_OK );
237341
237342 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
237343 const u8 *pList = 0;
237344
237345 p->rc = sqlite3Fts5HashScanInit(p: p->pHash, pTerm: (const char*)pTerm, nTerm);
237346 sqlite3Fts5HashScanEntry(pHash: p->pHash, pzTerm: (const char**)&z, ppDoclist: &pList, pnDoclist: &nList);
237347 n = (z ? (int)strlen(s: (const char*)z) : 0);
237348 if( pList ){
237349 pLeaf = fts5IdxMalloc(p, nByte: sizeof(Fts5Data));
237350 if( pLeaf ){
237351 pLeaf->p = (u8*)pList;
237352 }
237353 }
237354 }else{
237355 p->rc = sqlite3Fts5HashQuery(pHash: p->pHash, nPre: sizeof(Fts5Data),
237356 pTerm: (const char*)pTerm, nTerm, ppOut: (void**)&pLeaf, pnDoclist: &nList
237357 );
237358 if( pLeaf ){
237359 pLeaf->p = (u8*)&pLeaf[1];
237360 }
237361 z = pTerm;
237362 n = nTerm;
237363 pIter->flags |= FTS5_SEGITER_ONETERM;
237364 }
237365
237366 if( pLeaf ){
237367 sqlite3Fts5BufferSet(pRc: &p->rc, pBuf: &pIter->term, nData: n, pData: z);
237368 pLeaf->nn = pLeaf->szLeaf = nList;
237369 pIter->pLeaf = pLeaf;
237370 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
237371 pIter->iEndofDoclist = pLeaf->nn;
237372
237373 if( flags & FTS5INDEX_QUERY_DESC ){
237374 pIter->flags |= FTS5_SEGITER_REVERSE;
237375 fts5SegIterReverseInitPage(p, pIter);
237376 }else{
237377 fts5SegIterLoadNPos(p, pIter);
237378 }
237379 }
237380
237381 fts5SegIterSetNext(p, pIter);
237382}
237383
237384/*
237385** Array ap[] contains n elements. Release each of these elements using
237386** fts5DataRelease(). Then free the array itself using sqlite3_free().
237387*/
237388static void fts5IndexFreeArray(Fts5Data **ap, int n){
237389 if( ap ){
237390 int ii;
237391 for(ii=0; ii<n; ii++){
237392 fts5DataRelease(pData: ap[ii]);
237393 }
237394 sqlite3_free(p: ap);
237395 }
237396}
237397
237398/*
237399** Zero the iterator passed as the only argument.
237400*/
237401static void fts5SegIterClear(Fts5SegIter *pIter){
237402 fts5BufferFree(&pIter->term);
237403 fts5DataRelease(pData: pIter->pLeaf);
237404 fts5DataRelease(pData: pIter->pNextLeaf);
237405 fts5IndexFreeArray(ap: pIter->apTombstone, n: pIter->nTombstone);
237406 fts5DlidxIterFree(pIter: pIter->pDlidx);
237407 sqlite3_free(p: pIter->aRowidOffset);
237408 memset(s: pIter, c: 0, n: sizeof(Fts5SegIter));
237409}
237410
237411#ifdef SQLITE_DEBUG
237412
237413/*
237414** This function is used as part of the big assert() procedure implemented by
237415** fts5AssertMultiIterSetup(). It ensures that the result currently stored
237416** in *pRes is the correct result of comparing the current positions of the
237417** two iterators.
237418*/
237419static void fts5AssertComparisonResult(
237420 Fts5Iter *pIter,
237421 Fts5SegIter *p1,
237422 Fts5SegIter *p2,
237423 Fts5CResult *pRes
237424){
237425 int i1 = p1 - pIter->aSeg;
237426 int i2 = p2 - pIter->aSeg;
237427
237428 if( p1->pLeaf || p2->pLeaf ){
237429 if( p1->pLeaf==0 ){
237430 assert( pRes->iFirst==i2 );
237431 }else if( p2->pLeaf==0 ){
237432 assert( pRes->iFirst==i1 );
237433 }else{
237434 int nMin = MIN(p1->term.n, p2->term.n);
237435 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
237436 if( res==0 ) res = p1->term.n - p2->term.n;
237437
237438 if( res==0 ){
237439 assert( pRes->bTermEq==1 );
237440 assert( p1->iRowid!=p2->iRowid );
237441 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
237442 }else{
237443 assert( pRes->bTermEq==0 );
237444 }
237445
237446 if( res<0 ){
237447 assert( pRes->iFirst==i1 );
237448 }else{
237449 assert( pRes->iFirst==i2 );
237450 }
237451 }
237452 }
237453}
237454
237455/*
237456** This function is a no-op unless SQLITE_DEBUG is defined when this module
237457** is compiled. In that case, this function is essentially an assert()
237458** statement used to verify that the contents of the pIter->aFirst[] array
237459** are correct.
237460*/
237461static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
237462 if( p->rc==SQLITE_OK ){
237463 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
237464 int i;
237465
237466 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
237467
237468 /* Check that pIter->iSwitchRowid is set correctly. */
237469 for(i=0; i<pIter->nSeg; i++){
237470 Fts5SegIter *p1 = &pIter->aSeg[i];
237471 assert( p1==pFirst
237472 || p1->pLeaf==0
237473 || fts5BufferCompare(&pFirst->term, &p1->term)
237474 || p1->iRowid==pIter->iSwitchRowid
237475 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
237476 );
237477 }
237478
237479 for(i=0; i<pIter->nSeg; i+=2){
237480 Fts5SegIter *p1 = &pIter->aSeg[i];
237481 Fts5SegIter *p2 = &pIter->aSeg[i+1];
237482 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
237483 fts5AssertComparisonResult(pIter, p1, p2, pRes);
237484 }
237485
237486 for(i=1; i<(pIter->nSeg / 2); i+=2){
237487 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
237488 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
237489 Fts5CResult *pRes = &pIter->aFirst[i];
237490 fts5AssertComparisonResult(pIter, p1, p2, pRes);
237491 }
237492 }
237493}
237494#else
237495# define fts5AssertMultiIterSetup(x,y)
237496#endif
237497
237498/*
237499** Do the comparison necessary to populate pIter->aFirst[iOut].
237500**
237501** If the returned value is non-zero, then it is the index of an entry
237502** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
237503** to a key that is a duplicate of another, higher priority,
237504** segment-iterator in the pSeg->aSeg[] array.
237505*/
237506static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
237507 int i1; /* Index of left-hand Fts5SegIter */
237508 int i2; /* Index of right-hand Fts5SegIter */
237509 int iRes;
237510 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
237511 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
237512 Fts5CResult *pRes = &pIter->aFirst[iOut];
237513
237514 assert( iOut<pIter->nSeg && iOut>0 );
237515 assert( pIter->bRev==0 || pIter->bRev==1 );
237516
237517 if( iOut>=(pIter->nSeg/2) ){
237518 i1 = (iOut - pIter->nSeg/2) * 2;
237519 i2 = i1 + 1;
237520 }else{
237521 i1 = pIter->aFirst[iOut*2].iFirst;
237522 i2 = pIter->aFirst[iOut*2+1].iFirst;
237523 }
237524 p1 = &pIter->aSeg[i1];
237525 p2 = &pIter->aSeg[i2];
237526
237527 pRes->bTermEq = 0;
237528 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
237529 iRes = i2;
237530 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
237531 iRes = i1;
237532 }else{
237533 int res = fts5BufferCompare(pLeft: &p1->term, pRight: &p2->term);
237534 if( res==0 ){
237535 assert_nc( i2>i1 );
237536 assert_nc( i2!=0 );
237537 pRes->bTermEq = 1;
237538 if( p1->iRowid==p2->iRowid ){
237539 p1->bDel = p2->bDel;
237540 return i2;
237541 }
237542 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
237543 }
237544 assert( res!=0 );
237545 if( res<0 ){
237546 iRes = i1;
237547 }else{
237548 iRes = i2;
237549 }
237550 }
237551
237552 pRes->iFirst = (u16)iRes;
237553 return 0;
237554}
237555
237556/*
237557** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
237558** It is an error if leaf iLeafPgno does not exist. Unless the db is
237559** a 'secure-delete' db, if it contains no rowids then this is also an error.
237560*/
237561static void fts5SegIterGotoPage(
237562 Fts5Index *p, /* FTS5 backend object */
237563 Fts5SegIter *pIter, /* Iterator to advance */
237564 int iLeafPgno
237565){
237566 assert( iLeafPgno>pIter->iLeafPgno );
237567
237568 if( iLeafPgno>pIter->pSeg->pgnoLast ){
237569 p->rc = FTS5_CORRUPT;
237570 }else{
237571 fts5DataRelease(pData: pIter->pNextLeaf);
237572 pIter->pNextLeaf = 0;
237573 pIter->iLeafPgno = iLeafPgno-1;
237574
237575 while( p->rc==SQLITE_OK ){
237576 int iOff;
237577 fts5SegIterNextPage(p, pIter);
237578 if( pIter->pLeaf==0 ) break;
237579 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
237580 if( iOff>0 ){
237581 u8 *a = pIter->pLeaf->p;
237582 int n = pIter->pLeaf->szLeaf;
237583 if( iOff<4 || iOff>=n ){
237584 p->rc = FTS5_CORRUPT;
237585 }else{
237586 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
237587 pIter->iLeafOffset = iOff;
237588 fts5SegIterLoadNPos(p, pIter);
237589 }
237590 break;
237591 }
237592 }
237593 }
237594}
237595
237596/*
237597** Advance the iterator passed as the second argument until it is at or
237598** past rowid iFrom. Regardless of the value of iFrom, the iterator is
237599** always advanced at least once.
237600*/
237601static void fts5SegIterNextFrom(
237602 Fts5Index *p, /* FTS5 backend object */
237603 Fts5SegIter *pIter, /* Iterator to advance */
237604 i64 iMatch /* Advance iterator at least this far */
237605){
237606 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
237607 Fts5DlidxIter *pDlidx = pIter->pDlidx;
237608 int iLeafPgno = pIter->iLeafPgno;
237609 int bMove = 1;
237610
237611 assert( pIter->flags & FTS5_SEGITER_ONETERM );
237612 assert( pIter->pDlidx );
237613 assert( pIter->pLeaf );
237614
237615 if( bRev==0 ){
237616 while( !fts5DlidxIterEof(p, pIter: pDlidx) && iMatch>fts5DlidxIterRowid(pIter: pDlidx) ){
237617 iLeafPgno = fts5DlidxIterPgno(pIter: pDlidx);
237618 fts5DlidxIterNext(p, pIter: pDlidx);
237619 }
237620 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
237621 if( iLeafPgno>pIter->iLeafPgno ){
237622 fts5SegIterGotoPage(p, pIter, iLeafPgno);
237623 bMove = 0;
237624 }
237625 }else{
237626 assert( pIter->pNextLeaf==0 );
237627 assert( iMatch<pIter->iRowid );
237628 while( !fts5DlidxIterEof(p, pIter: pDlidx) && iMatch<fts5DlidxIterRowid(pIter: pDlidx) ){
237629 fts5DlidxIterPrev(p, pIter: pDlidx);
237630 }
237631 iLeafPgno = fts5DlidxIterPgno(pIter: pDlidx);
237632
237633 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
237634
237635 if( iLeafPgno<pIter->iLeafPgno ){
237636 pIter->iLeafPgno = iLeafPgno+1;
237637 fts5SegIterReverseNewPage(p, pIter);
237638 bMove = 0;
237639 }
237640 }
237641
237642 do{
237643 if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
237644 if( pIter->pLeaf==0 ) break;
237645 if( bRev==0 && pIter->iRowid>=iMatch ) break;
237646 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
237647 bMove = 1;
237648 }while( p->rc==SQLITE_OK );
237649}
237650
237651
237652/*
237653** Free the iterator object passed as the second argument.
237654*/
237655static void fts5MultiIterFree(Fts5Iter *pIter){
237656 if( pIter ){
237657 int i;
237658 for(i=0; i<pIter->nSeg; i++){
237659 fts5SegIterClear(pIter: &pIter->aSeg[i]);
237660 }
237661 fts5BufferFree(&pIter->poslist);
237662 sqlite3_free(p: pIter);
237663 }
237664}
237665
237666static void fts5MultiIterAdvanced(
237667 Fts5Index *p, /* FTS5 backend to iterate within */
237668 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
237669 int iChanged, /* Index of sub-iterator just advanced */
237670 int iMinset /* Minimum entry in aFirst[] to set */
237671){
237672 int i;
237673 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
237674 int iEq;
237675 if( (iEq = fts5MultiIterDoCompare(pIter, iOut: i)) ){
237676 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
237677 assert( p->rc==SQLITE_OK );
237678 pSeg->xNext(p, pSeg, 0);
237679 i = pIter->nSeg + iEq;
237680 }
237681 }
237682}
237683
237684/*
237685** Sub-iterator iChanged of iterator pIter has just been advanced. It still
237686** points to the same term though - just a different rowid. This function
237687** attempts to update the contents of the pIter->aFirst[] accordingly.
237688** If it does so successfully, 0 is returned. Otherwise 1.
237689**
237690** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
237691** on the iterator instead. That function does the same as this one, except
237692** that it deals with more complicated cases as well.
237693*/
237694static int fts5MultiIterAdvanceRowid(
237695 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
237696 int iChanged, /* Index of sub-iterator just advanced */
237697 Fts5SegIter **ppFirst
237698){
237699 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
237700
237701 if( pNew->iRowid==pIter->iSwitchRowid
237702 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
237703 ){
237704 int i;
237705 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
237706 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
237707 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
237708 Fts5CResult *pRes = &pIter->aFirst[i];
237709
237710 assert( pNew->pLeaf );
237711 assert( pRes->bTermEq==0 || pOther->pLeaf );
237712
237713 if( pRes->bTermEq ){
237714 if( pNew->iRowid==pOther->iRowid ){
237715 return 1;
237716 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
237717 pIter->iSwitchRowid = pOther->iRowid;
237718 pNew = pOther;
237719 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
237720 pIter->iSwitchRowid = pOther->iRowid;
237721 }
237722 }
237723 pRes->iFirst = (u16)(pNew - pIter->aSeg);
237724 if( i==1 ) break;
237725
237726 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
237727 }
237728 }
237729
237730 *ppFirst = pNew;
237731 return 0;
237732}
237733
237734/*
237735** Set the pIter->bEof variable based on the state of the sub-iterators.
237736*/
237737static void fts5MultiIterSetEof(Fts5Iter *pIter){
237738 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
237739 pIter->base.bEof = pSeg->pLeaf==0;
237740 pIter->iSwitchRowid = pSeg->iRowid;
237741}
237742
237743/*
237744** The argument to this macro must be an Fts5Data structure containing a
237745** tombstone hash page. This macro returns the key-size of the hash-page.
237746*/
237747#define TOMBSTONE_KEYSIZE(pPg) (pPg->p[0]==4 ? 4 : 8)
237748
237749#define TOMBSTONE_NSLOT(pPg) \
237750 ((pPg->nn > 16) ? ((pPg->nn-8) / TOMBSTONE_KEYSIZE(pPg)) : 1)
237751
237752/*
237753** Query a single tombstone hash table for rowid iRowid. Return true if
237754** it is found or false otherwise. The tombstone hash table is one of
237755** nHashTable tables.
237756*/
237757static int fts5IndexTombstoneQuery(
237758 Fts5Data *pHash, /* Hash table page to query */
237759 int nHashTable, /* Number of pages attached to segment */
237760 u64 iRowid /* Rowid to query hash for */
237761){
237762 const int szKey = TOMBSTONE_KEYSIZE(pHash);
237763 const int nSlot = TOMBSTONE_NSLOT(pHash);
237764 int iSlot = (iRowid / nHashTable) % nSlot;
237765 int nCollide = nSlot;
237766
237767 if( iRowid==0 ){
237768 return pHash->p[1];
237769 }else if( szKey==4 ){
237770 u32 *aSlot = (u32*)&pHash->p[8];
237771 while( aSlot[iSlot] ){
237772 if( fts5GetU32(a: (u8*)&aSlot[iSlot])==iRowid ) return 1;
237773 if( nCollide--==0 ) break;
237774 iSlot = (iSlot+1)%nSlot;
237775 }
237776 }else{
237777 u64 *aSlot = (u64*)&pHash->p[8];
237778 while( aSlot[iSlot] ){
237779 if( fts5GetU64(a: (u8*)&aSlot[iSlot])==iRowid ) return 1;
237780 if( nCollide--==0 ) break;
237781 iSlot = (iSlot+1)%nSlot;
237782 }
237783 }
237784
237785 return 0;
237786}
237787
237788/*
237789** Return true if the iterator passed as the only argument points
237790** to an segment entry for which there is a tombstone. Return false
237791** if there is no tombstone or if the iterator is already at EOF.
237792*/
237793static int fts5MultiIterIsDeleted(Fts5Iter *pIter){
237794 int iFirst = pIter->aFirst[1].iFirst;
237795 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
237796
237797 if( pSeg->pLeaf && pSeg->nTombstone ){
237798 /* Figure out which page the rowid might be present on. */
237799 int iPg = ((u64)pSeg->iRowid) % pSeg->nTombstone;
237800 assert( iPg>=0 );
237801
237802 /* If tombstone hash page iPg has not yet been loaded from the
237803 ** database, load it now. */
237804 if( pSeg->apTombstone[iPg]==0 ){
237805 pSeg->apTombstone[iPg] = fts5DataRead(p: pIter->pIndex,
237806 FTS5_TOMBSTONE_ROWID(pSeg->pSeg->iSegid, iPg)
237807 );
237808 if( pSeg->apTombstone[iPg]==0 ) return 0;
237809 }
237810
237811 return fts5IndexTombstoneQuery(
237812 pHash: pSeg->apTombstone[iPg],
237813 nHashTable: pSeg->nTombstone,
237814 iRowid: pSeg->iRowid
237815 );
237816 }
237817
237818 return 0;
237819}
237820
237821/*
237822** Move the iterator to the next entry.
237823**
237824** If an error occurs, an error code is left in Fts5Index.rc. It is not
237825** considered an error if the iterator reaches EOF, or if it is already at
237826** EOF when this function is called.
237827*/
237828static void fts5MultiIterNext(
237829 Fts5Index *p,
237830 Fts5Iter *pIter,
237831 int bFrom, /* True if argument iFrom is valid */
237832 i64 iFrom /* Advance at least as far as this */
237833){
237834 int bUseFrom = bFrom;
237835 assert( pIter->base.bEof==0 );
237836 while( p->rc==SQLITE_OK ){
237837 int iFirst = pIter->aFirst[1].iFirst;
237838 int bNewTerm = 0;
237839 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
237840 assert( p->rc==SQLITE_OK );
237841 if( bUseFrom && pSeg->pDlidx ){
237842 fts5SegIterNextFrom(p, pIter: pSeg, iMatch: iFrom);
237843 }else{
237844 pSeg->xNext(p, pSeg, &bNewTerm);
237845 }
237846
237847 if( pSeg->pLeaf==0 || bNewTerm
237848 || fts5MultiIterAdvanceRowid(pIter, iChanged: iFirst, ppFirst: &pSeg)
237849 ){
237850 fts5MultiIterAdvanced(p, pIter, iChanged: iFirst, iMinset: 1);
237851 fts5MultiIterSetEof(pIter);
237852 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
237853 if( pSeg->pLeaf==0 ) return;
237854 }
237855
237856 fts5AssertMultiIterSetup(p, pIter);
237857 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
237858 if( (pIter->bSkipEmpty==0 || pSeg->nPos)
237859 && 0==fts5MultiIterIsDeleted(pIter)
237860 ){
237861 pIter->xSetOutputs(pIter, pSeg);
237862 return;
237863 }
237864 bUseFrom = 0;
237865 }
237866}
237867
237868static void fts5MultiIterNext2(
237869 Fts5Index *p,
237870 Fts5Iter *pIter,
237871 int *pbNewTerm /* OUT: True if *might* be new term */
237872){
237873 assert( pIter->bSkipEmpty );
237874 if( p->rc==SQLITE_OK ){
237875 *pbNewTerm = 0;
237876 do{
237877 int iFirst = pIter->aFirst[1].iFirst;
237878 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
237879 int bNewTerm = 0;
237880
237881 assert( p->rc==SQLITE_OK );
237882 pSeg->xNext(p, pSeg, &bNewTerm);
237883 if( pSeg->pLeaf==0 || bNewTerm
237884 || fts5MultiIterAdvanceRowid(pIter, iChanged: iFirst, ppFirst: &pSeg)
237885 ){
237886 fts5MultiIterAdvanced(p, pIter, iChanged: iFirst, iMinset: 1);
237887 fts5MultiIterSetEof(pIter);
237888 *pbNewTerm = 1;
237889 }
237890 fts5AssertMultiIterSetup(p, pIter);
237891
237892 }while( (fts5MultiIterIsEmpty(p, pIter) || fts5MultiIterIsDeleted(pIter))
237893 && (p->rc==SQLITE_OK)
237894 );
237895 }
237896}
237897
237898static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
237899 UNUSED_PARAM2(pUnused1, pUnused2);
237900}
237901
237902static Fts5Iter *fts5MultiIterAlloc(
237903 Fts5Index *p, /* FTS5 backend to iterate within */
237904 int nSeg
237905){
237906 Fts5Iter *pNew;
237907 int nSlot; /* Power of two >= nSeg */
237908
237909 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
237910 pNew = fts5IdxMalloc(p,
237911 nByte: sizeof(Fts5Iter) + /* pNew */
237912 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
237913 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
237914 );
237915 if( pNew ){
237916 pNew->nSeg = nSlot;
237917 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
237918 pNew->pIndex = p;
237919 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
237920 }
237921 return pNew;
237922}
237923
237924static void fts5PoslistCallback(
237925 Fts5Index *pUnused,
237926 void *pContext,
237927 const u8 *pChunk, int nChunk
237928){
237929 UNUSED_PARAM(pUnused);
237930 assert_nc( nChunk>=0 );
237931 if( nChunk>0 ){
237932 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
237933 }
237934}
237935
237936typedef struct PoslistCallbackCtx PoslistCallbackCtx;
237937struct PoslistCallbackCtx {
237938 Fts5Buffer *pBuf; /* Append to this buffer */
237939 Fts5Colset *pColset; /* Restrict matches to this column */
237940 int eState; /* See above */
237941};
237942
237943typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
237944struct PoslistOffsetsCtx {
237945 Fts5Buffer *pBuf; /* Append to this buffer */
237946 Fts5Colset *pColset; /* Restrict matches to this column */
237947 int iRead;
237948 int iWrite;
237949};
237950
237951/*
237952** TODO: Make this more efficient!
237953*/
237954static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
237955 int i;
237956 for(i=0; i<pColset->nCol; i++){
237957 if( pColset->aiCol[i]==iCol ) return 1;
237958 }
237959 return 0;
237960}
237961
237962static void fts5PoslistOffsetsCallback(
237963 Fts5Index *pUnused,
237964 void *pContext,
237965 const u8 *pChunk, int nChunk
237966){
237967 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
237968 UNUSED_PARAM(pUnused);
237969 assert_nc( nChunk>=0 );
237970 if( nChunk>0 ){
237971 int i = 0;
237972 while( i<nChunk ){
237973 int iVal;
237974 i += fts5GetVarint32(&pChunk[i], iVal);
237975 iVal += pCtx->iRead - 2;
237976 pCtx->iRead = iVal;
237977 if( fts5IndexColsetTest(pColset: pCtx->pColset, iCol: iVal) ){
237978 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
237979 pCtx->iWrite = iVal;
237980 }
237981 }
237982 }
237983}
237984
237985static void fts5PoslistFilterCallback(
237986 Fts5Index *pUnused,
237987 void *pContext,
237988 const u8 *pChunk, int nChunk
237989){
237990 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
237991 UNUSED_PARAM(pUnused);
237992 assert_nc( nChunk>=0 );
237993 if( nChunk>0 ){
237994 /* Search through to find the first varint with value 1. This is the
237995 ** start of the next columns hits. */
237996 int i = 0;
237997 int iStart = 0;
237998
237999 if( pCtx->eState==2 ){
238000 int iCol;
238001 fts5FastGetVarint32(pChunk, i, iCol);
238002 if( fts5IndexColsetTest(pColset: pCtx->pColset, iCol) ){
238003 pCtx->eState = 1;
238004 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
238005 }else{
238006 pCtx->eState = 0;
238007 }
238008 }
238009
238010 do {
238011 while( i<nChunk && pChunk[i]!=0x01 ){
238012 while( pChunk[i] & 0x80 ) i++;
238013 i++;
238014 }
238015 if( pCtx->eState ){
238016 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
238017 }
238018 if( i<nChunk ){
238019 int iCol;
238020 iStart = i;
238021 i++;
238022 if( i>=nChunk ){
238023 pCtx->eState = 2;
238024 }else{
238025 fts5FastGetVarint32(pChunk, i, iCol);
238026 pCtx->eState = fts5IndexColsetTest(pColset: pCtx->pColset, iCol);
238027 if( pCtx->eState ){
238028 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
238029 iStart = i;
238030 }
238031 }
238032 }
238033 }while( i<nChunk );
238034 }
238035}
238036
238037static void fts5ChunkIterate(
238038 Fts5Index *p, /* Index object */
238039 Fts5SegIter *pSeg, /* Poslist of this iterator */
238040 void *pCtx, /* Context pointer for xChunk callback */
238041 void (*xChunk)(Fts5Index*, void*, const u8*, int)
238042){
238043 int nRem = pSeg->nPos; /* Number of bytes still to come */
238044 Fts5Data *pData = 0;
238045 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
238046 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
238047 int pgno = pSeg->iLeafPgno;
238048 int pgnoSave = 0;
238049
238050 /* This function does not work with detail=none databases. */
238051 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
238052
238053 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
238054 pgnoSave = pgno+1;
238055 }
238056
238057 while( 1 ){
238058 xChunk(p, pCtx, pChunk, nChunk);
238059 nRem -= nChunk;
238060 fts5DataRelease(pData);
238061 if( nRem<=0 ){
238062 break;
238063 }else if( pSeg->pSeg==0 ){
238064 p->rc = FTS5_CORRUPT;
238065 return;
238066 }else{
238067 pgno++;
238068 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
238069 if( pData==0 ) break;
238070 pChunk = &pData->p[4];
238071 nChunk = MIN(nRem, pData->szLeaf - 4);
238072 if( pgno==pgnoSave ){
238073 assert( pSeg->pNextLeaf==0 );
238074 pSeg->pNextLeaf = pData;
238075 pData = 0;
238076 }
238077 }
238078 }
238079}
238080
238081/*
238082** Iterator pIter currently points to a valid entry (not EOF). This
238083** function appends the position list data for the current entry to
238084** buffer pBuf. It does not make a copy of the position-list size
238085** field.
238086*/
238087static void fts5SegiterPoslist(
238088 Fts5Index *p,
238089 Fts5SegIter *pSeg,
238090 Fts5Colset *pColset,
238091 Fts5Buffer *pBuf
238092){
238093 assert( pBuf!=0 );
238094 assert( pSeg!=0 );
238095 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
238096 assert( pBuf->p!=0 );
238097 assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING );
238098 memset(s: &pBuf->p[pBuf->n+pSeg->nPos], c: 0, FTS5_DATA_ZERO_PADDING);
238099 if( pColset==0 ){
238100 fts5ChunkIterate(p, pSeg, pCtx: (void*)pBuf, xChunk: fts5PoslistCallback);
238101 }else{
238102 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
238103 PoslistCallbackCtx sCtx;
238104 sCtx.pBuf = pBuf;
238105 sCtx.pColset = pColset;
238106 sCtx.eState = fts5IndexColsetTest(pColset, iCol: 0);
238107 assert( sCtx.eState==0 || sCtx.eState==1 );
238108 fts5ChunkIterate(p, pSeg, pCtx: (void*)&sCtx, xChunk: fts5PoslistFilterCallback);
238109 }else{
238110 PoslistOffsetsCtx sCtx;
238111 memset(s: &sCtx, c: 0, n: sizeof(sCtx));
238112 sCtx.pBuf = pBuf;
238113 sCtx.pColset = pColset;
238114 fts5ChunkIterate(p, pSeg, pCtx: (void*)&sCtx, xChunk: fts5PoslistOffsetsCallback);
238115 }
238116 }
238117 }
238118}
238119
238120/*
238121** Parameter pPos points to a buffer containing a position list, size nPos.
238122** This function filters it according to pColset (which must be non-NULL)
238123** and sets pIter->base.pData/nData to point to the new position list.
238124** If memory is required for the new position list, use buffer pIter->poslist.
238125** Or, if the new position list is a contiguous subset of the input, set
238126** pIter->base.pData/nData to point directly to it.
238127**
238128** This function is a no-op if *pRc is other than SQLITE_OK when it is
238129** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
238130** before returning.
238131*/
238132static void fts5IndexExtractColset(
238133 int *pRc,
238134 Fts5Colset *pColset, /* Colset to filter on */
238135 const u8 *pPos, int nPos, /* Position list */
238136 Fts5Iter *pIter
238137){
238138 if( *pRc==SQLITE_OK ){
238139 const u8 *p = pPos;
238140 const u8 *aCopy = p;
238141 const u8 *pEnd = &p[nPos]; /* One byte past end of position list */
238142 int i = 0;
238143 int iCurrent = 0;
238144
238145 if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, pBuf: &pIter->poslist, nByte: nPos) ){
238146 return;
238147 }
238148
238149 while( 1 ){
238150 while( pColset->aiCol[i]<iCurrent ){
238151 i++;
238152 if( i==pColset->nCol ){
238153 pIter->base.pData = pIter->poslist.p;
238154 pIter->base.nData = pIter->poslist.n;
238155 return;
238156 }
238157 }
238158
238159 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
238160 ** not part of a varint */
238161 while( p<pEnd && *p!=0x01 ){
238162 while( *p++ & 0x80 );
238163 }
238164
238165 if( pColset->aiCol[i]==iCurrent ){
238166 if( pColset->nCol==1 ){
238167 pIter->base.pData = aCopy;
238168 pIter->base.nData = p-aCopy;
238169 return;
238170 }
238171 fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy);
238172 }
238173 if( p>=pEnd ){
238174 pIter->base.pData = pIter->poslist.p;
238175 pIter->base.nData = pIter->poslist.n;
238176 return;
238177 }
238178 aCopy = p++;
238179 iCurrent = *p++;
238180 if( iCurrent & 0x80 ){
238181 p--;
238182 p += fts5GetVarint32(p, iCurrent);
238183 }
238184 }
238185 }
238186
238187}
238188
238189/*
238190** xSetOutputs callback used by detail=none tables.
238191*/
238192static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
238193 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
238194 pIter->base.iRowid = pSeg->iRowid;
238195 pIter->base.nData = pSeg->nPos;
238196}
238197
238198/*
238199** xSetOutputs callback used by detail=full and detail=col tables when no
238200** column filters are specified.
238201*/
238202static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
238203 pIter->base.iRowid = pSeg->iRowid;
238204 pIter->base.nData = pSeg->nPos;
238205
238206 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
238207 assert( pIter->pColset==0 );
238208
238209 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
238210 /* All data is stored on the current page. Populate the output
238211 ** variables to point into the body of the page object. */
238212 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
238213 }else{
238214 /* The data is distributed over two or more pages. Copy it into the
238215 ** Fts5Iter.poslist buffer and then set the output pointer to point
238216 ** to this buffer. */
238217 fts5BufferZero(&pIter->poslist);
238218 fts5SegiterPoslist(p: pIter->pIndex, pSeg, pColset: 0, pBuf: &pIter->poslist);
238219 pIter->base.pData = pIter->poslist.p;
238220 }
238221}
238222
238223/*
238224** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
238225** against no columns at all).
238226*/
238227static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
238228 UNUSED_PARAM(pSeg);
238229 pIter->base.nData = 0;
238230}
238231
238232/*
238233** xSetOutputs callback used by detail=col when there is a column filter
238234** and there are 100 or more columns. Also called as a fallback from
238235** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
238236*/
238237static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
238238 fts5BufferZero(&pIter->poslist);
238239 fts5SegiterPoslist(p: pIter->pIndex, pSeg, pColset: pIter->pColset, pBuf: &pIter->poslist);
238240 pIter->base.iRowid = pSeg->iRowid;
238241 pIter->base.pData = pIter->poslist.p;
238242 pIter->base.nData = pIter->poslist.n;
238243}
238244
238245/*
238246** xSetOutputs callback used when:
238247**
238248** * detail=col,
238249** * there is a column filter, and
238250** * the table contains 100 or fewer columns.
238251**
238252** The last point is to ensure all column numbers are stored as
238253** single-byte varints.
238254*/
238255static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
238256
238257 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
238258 assert( pIter->pColset );
238259
238260 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
238261 fts5IterSetOutputs_Col(pIter, pSeg);
238262 }else{
238263 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
238264 u8 *pEnd = (u8*)&a[pSeg->nPos];
238265 int iPrev = 0;
238266 int *aiCol = pIter->pColset->aiCol;
238267 int *aiColEnd = &aiCol[pIter->pColset->nCol];
238268
238269 u8 *aOut = pIter->poslist.p;
238270 int iPrevOut = 0;
238271
238272 pIter->base.iRowid = pSeg->iRowid;
238273
238274 while( a<pEnd ){
238275 iPrev += (int)a++[0] - 2;
238276 while( *aiCol<iPrev ){
238277 aiCol++;
238278 if( aiCol==aiColEnd ) goto setoutputs_col_out;
238279 }
238280 if( *aiCol==iPrev ){
238281 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
238282 iPrevOut = iPrev;
238283 }
238284 }
238285
238286setoutputs_col_out:
238287 pIter->base.pData = pIter->poslist.p;
238288 pIter->base.nData = aOut - pIter->poslist.p;
238289 }
238290}
238291
238292/*
238293** xSetOutputs callback used by detail=full when there is a column filter.
238294*/
238295static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
238296 Fts5Colset *pColset = pIter->pColset;
238297 pIter->base.iRowid = pSeg->iRowid;
238298
238299 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
238300 assert( pColset );
238301
238302 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
238303 /* All data is stored on the current page. Populate the output
238304 ** variables to point into the body of the page object. */
238305 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
238306 int *pRc = &pIter->pIndex->rc;
238307 fts5BufferZero(&pIter->poslist);
238308 fts5IndexExtractColset(pRc, pColset, pPos: a, nPos: pSeg->nPos, pIter);
238309 }else{
238310 /* The data is distributed over two or more pages. Copy it into the
238311 ** Fts5Iter.poslist buffer and then set the output pointer to point
238312 ** to this buffer. */
238313 fts5BufferZero(&pIter->poslist);
238314 fts5SegiterPoslist(p: pIter->pIndex, pSeg, pColset, pBuf: &pIter->poslist);
238315 pIter->base.pData = pIter->poslist.p;
238316 pIter->base.nData = pIter->poslist.n;
238317 }
238318}
238319
238320static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
238321 assert( pIter!=0 || (*pRc)!=SQLITE_OK );
238322 if( *pRc==SQLITE_OK ){
238323 Fts5Config *pConfig = pIter->pIndex->pConfig;
238324 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
238325 pIter->xSetOutputs = fts5IterSetOutputs_None;
238326 }
238327
238328 else if( pIter->pColset==0 ){
238329 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
238330 }
238331
238332 else if( pIter->pColset->nCol==0 ){
238333 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
238334 }
238335
238336 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
238337 pIter->xSetOutputs = fts5IterSetOutputs_Full;
238338 }
238339
238340 else{
238341 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
238342 if( pConfig->nCol<=100 ){
238343 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
238344 sqlite3Fts5BufferSize(pRc, pBuf: &pIter->poslist, nByte: pConfig->nCol);
238345 }else{
238346 pIter->xSetOutputs = fts5IterSetOutputs_Col;
238347 }
238348 }
238349 }
238350}
238351
238352
238353/*
238354** Allocate a new Fts5Iter object.
238355**
238356** The new object will be used to iterate through data in structure pStruct.
238357** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
238358** is zero or greater, data from the first nSegment segments on level iLevel
238359** is merged.
238360**
238361** The iterator initially points to the first term/rowid entry in the
238362** iterated data.
238363*/
238364static void fts5MultiIterNew(
238365 Fts5Index *p, /* FTS5 backend to iterate within */
238366 Fts5Structure *pStruct, /* Structure of specific index */
238367 int flags, /* FTS5INDEX_QUERY_XXX flags */
238368 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
238369 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
238370 int iLevel, /* Level to iterate (-1 for all) */
238371 int nSegment, /* Number of segments to merge (iLevel>=0) */
238372 Fts5Iter **ppOut /* New object */
238373){
238374 int nSeg = 0; /* Number of segment-iters in use */
238375 int iIter = 0; /* */
238376 int iSeg; /* Used to iterate through segments */
238377 Fts5StructureLevel *pLvl;
238378 Fts5Iter *pNew;
238379
238380 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
238381
238382 /* Allocate space for the new multi-seg-iterator. */
238383 if( p->rc==SQLITE_OK ){
238384 if( iLevel<0 ){
238385 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
238386 nSeg = pStruct->nSegment;
238387 nSeg += (p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH));
238388 }else{
238389 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
238390 }
238391 }
238392 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
238393 if( pNew==0 ){
238394 assert( p->rc!=SQLITE_OK );
238395 goto fts5MultiIterNew_post_check;
238396 }
238397 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
238398 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
238399 pNew->pColset = pColset;
238400 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
238401 fts5IterSetOutputCb(pRc: &p->rc, pIter: pNew);
238402 }
238403
238404 /* Initialize each of the component segment iterators. */
238405 if( p->rc==SQLITE_OK ){
238406 if( iLevel<0 ){
238407 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
238408 if( p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH) ){
238409 /* Add a segment iterator for the current contents of the hash table. */
238410 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
238411 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
238412 }
238413 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
238414 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
238415 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
238416 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
238417 if( pTerm==0 ){
238418 fts5SegIterInit(p, pSeg, pIter);
238419 }else{
238420 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
238421 }
238422 }
238423 }
238424 }else{
238425 pLvl = &pStruct->aLevel[iLevel];
238426 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
238427 fts5SegIterInit(p, pSeg: &pLvl->aSeg[iSeg], pIter: &pNew->aSeg[iIter++]);
238428 }
238429 }
238430 assert( iIter==nSeg );
238431 }
238432
238433 /* If the above was successful, each component iterators now points
238434 ** to the first entry in its segment. In this case initialize the
238435 ** aFirst[] array. Or, if an error has occurred, free the iterator
238436 ** object and set the output variable to NULL. */
238437 if( p->rc==SQLITE_OK ){
238438 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
238439 int iEq;
238440 if( (iEq = fts5MultiIterDoCompare(pIter: pNew, iOut: iIter)) ){
238441 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
238442 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
238443 fts5MultiIterAdvanced(p, pIter: pNew, iChanged: iEq, iMinset: iIter);
238444 }
238445 }
238446 fts5MultiIterSetEof(pIter: pNew);
238447 fts5AssertMultiIterSetup(p, pNew);
238448
238449 if( (pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter: pNew))
238450 || fts5MultiIterIsDeleted(pIter: pNew)
238451 ){
238452 fts5MultiIterNext(p, pIter: pNew, bFrom: 0, iFrom: 0);
238453 }else if( pNew->base.bEof==0 ){
238454 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
238455 pNew->xSetOutputs(pNew, pSeg);
238456 }
238457
238458 }else{
238459 fts5MultiIterFree(pIter: pNew);
238460 *ppOut = 0;
238461 }
238462
238463fts5MultiIterNew_post_check:
238464 assert( (*ppOut)!=0 || p->rc!=SQLITE_OK );
238465 return;
238466}
238467
238468/*
238469** Create an Fts5Iter that iterates through the doclist provided
238470** as the second argument.
238471*/
238472static void fts5MultiIterNew2(
238473 Fts5Index *p, /* FTS5 backend to iterate within */
238474 Fts5Data *pData, /* Doclist to iterate through */
238475 int bDesc, /* True for descending rowid order */
238476 Fts5Iter **ppOut /* New object */
238477){
238478 Fts5Iter *pNew;
238479 pNew = fts5MultiIterAlloc(p, nSeg: 2);
238480 if( pNew ){
238481 Fts5SegIter *pIter = &pNew->aSeg[1];
238482
238483 pIter->flags = FTS5_SEGITER_ONETERM;
238484 if( pData->szLeaf>0 ){
238485 pIter->pLeaf = pData;
238486 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
238487 pIter->iEndofDoclist = pData->nn;
238488 pNew->aFirst[1].iFirst = 1;
238489 if( bDesc ){
238490 pNew->bRev = 1;
238491 pIter->flags |= FTS5_SEGITER_REVERSE;
238492 fts5SegIterReverseInitPage(p, pIter);
238493 }else{
238494 fts5SegIterLoadNPos(p, pIter);
238495 }
238496 pData = 0;
238497 }else{
238498 pNew->base.bEof = 1;
238499 }
238500 fts5SegIterSetNext(p, pIter);
238501
238502 *ppOut = pNew;
238503 }
238504
238505 fts5DataRelease(pData);
238506}
238507
238508/*
238509** Return true if the iterator is at EOF or if an error has occurred.
238510** False otherwise.
238511*/
238512static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
238513 assert( pIter!=0 || p->rc!=SQLITE_OK );
238514 assert( p->rc!=SQLITE_OK
238515 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
238516 );
238517 return (p->rc || pIter->base.bEof);
238518}
238519
238520/*
238521** Return the rowid of the entry that the iterator currently points
238522** to. If the iterator points to EOF when this function is called the
238523** results are undefined.
238524*/
238525static i64 fts5MultiIterRowid(Fts5Iter *pIter){
238526 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
238527 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
238528}
238529
238530/*
238531** Move the iterator to the next entry at or following iMatch.
238532*/
238533static void fts5MultiIterNextFrom(
238534 Fts5Index *p,
238535 Fts5Iter *pIter,
238536 i64 iMatch
238537){
238538 while( 1 ){
238539 i64 iRowid;
238540 fts5MultiIterNext(p, pIter, bFrom: 1, iFrom: iMatch);
238541 if( fts5MultiIterEof(p, pIter) ) break;
238542 iRowid = fts5MultiIterRowid(pIter);
238543 if( pIter->bRev==0 && iRowid>=iMatch ) break;
238544 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
238545 }
238546}
238547
238548/*
238549** Return a pointer to a buffer containing the term associated with the
238550** entry that the iterator currently points to.
238551*/
238552static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
238553 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
238554 *pn = p->term.n;
238555 return p->term.p;
238556}
238557
238558/*
238559** Allocate a new segment-id for the structure pStruct. The new segment
238560** id must be between 1 and 65335 inclusive, and must not be used by
238561** any currently existing segment. If a free segment id cannot be found,
238562** SQLITE_FULL is returned.
238563**
238564** If an error has already occurred, this function is a no-op. 0 is
238565** returned in this case.
238566*/
238567static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
238568 int iSegid = 0;
238569
238570 if( p->rc==SQLITE_OK ){
238571 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
238572 p->rc = SQLITE_FULL;
238573 }else{
238574 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
238575 ** array is 63 elements, or 252 bytes, in size. */
238576 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
238577 int iLvl, iSeg;
238578 int i;
238579 u32 mask;
238580 memset(s: aUsed, c: 0, n: sizeof(aUsed));
238581 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
238582 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
238583 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
238584 if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
238585 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
238586 }
238587 }
238588 }
238589
238590 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
238591 mask = aUsed[i];
238592 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
238593 iSegid += 1 + i*32;
238594
238595#ifdef SQLITE_DEBUG
238596 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
238597 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
238598 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
238599 }
238600 }
238601 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
238602
238603 {
238604 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
238605 if( p->rc==SQLITE_OK ){
238606 u8 aBlob[2] = {0xff, 0xff};
238607 sqlite3_bind_int(pIdxSelect, 1, iSegid);
238608 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
238609 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
238610 p->rc = sqlite3_reset(pIdxSelect);
238611 sqlite3_bind_null(pIdxSelect, 2);
238612 }
238613 }
238614#endif
238615 }
238616 }
238617
238618 return iSegid;
238619}
238620
238621/*
238622** Discard all data currently cached in the hash-tables.
238623*/
238624static void fts5IndexDiscardData(Fts5Index *p){
238625 assert( p->pHash || p->nPendingData==0 );
238626 if( p->pHash ){
238627 sqlite3Fts5HashClear(pHash: p->pHash);
238628 p->nPendingData = 0;
238629 p->nPendingRow = 0;
238630 }
238631 p->nContentlessDelete = 0;
238632}
238633
238634/*
238635** Return the size of the prefix, in bytes, that buffer
238636** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
238637**
238638** Buffer (pNew/<length-unknown>) is guaranteed to be greater
238639** than buffer (pOld/nOld).
238640*/
238641static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
238642 int i;
238643 for(i=0; i<nOld; i++){
238644 if( pOld[i]!=pNew[i] ) break;
238645 }
238646 return i;
238647}
238648
238649static void fts5WriteDlidxClear(
238650 Fts5Index *p,
238651 Fts5SegWriter *pWriter,
238652 int bFlush /* If true, write dlidx to disk */
238653){
238654 int i;
238655 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
238656 for(i=0; i<pWriter->nDlidx; i++){
238657 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
238658 if( pDlidx->buf.n==0 ) break;
238659 if( bFlush ){
238660 assert( pDlidx->pgno!=0 );
238661 fts5DataWrite(p,
238662 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
238663 pData: pDlidx->buf.p, nData: pDlidx->buf.n
238664 );
238665 }
238666 sqlite3Fts5BufferZero(pBuf: &pDlidx->buf);
238667 pDlidx->bPrevValid = 0;
238668 }
238669}
238670
238671/*
238672** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
238673** Any new array elements are zeroed before returning.
238674*/
238675static int fts5WriteDlidxGrow(
238676 Fts5Index *p,
238677 Fts5SegWriter *pWriter,
238678 int nLvl
238679){
238680 if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
238681 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
238682 pOld: pWriter->aDlidx, n: sizeof(Fts5DlidxWriter) * nLvl
238683 );
238684 if( aDlidx==0 ){
238685 p->rc = SQLITE_NOMEM;
238686 }else{
238687 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
238688 memset(s: &aDlidx[pWriter->nDlidx], c: 0, n: nByte);
238689 pWriter->aDlidx = aDlidx;
238690 pWriter->nDlidx = nLvl;
238691 }
238692 }
238693 return p->rc;
238694}
238695
238696/*
238697** If the current doclist-index accumulating in pWriter->aDlidx[] is large
238698** enough, flush it to disk and return 1. Otherwise discard it and return
238699** zero.
238700*/
238701static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
238702 int bFlag = 0;
238703
238704 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
238705 ** to the database, also write the doclist-index to disk. */
238706 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
238707 bFlag = 1;
238708 }
238709 fts5WriteDlidxClear(p, pWriter, bFlush: bFlag);
238710 pWriter->nEmpty = 0;
238711 return bFlag;
238712}
238713
238714/*
238715** This function is called whenever processing of the doclist for the
238716** last term on leaf page (pWriter->iBtPage) is completed.
238717**
238718** The doclist-index for that term is currently stored in-memory within the
238719** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
238720** writes it out to disk. Or, if it is too small to bother with, discards
238721** it.
238722**
238723** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
238724*/
238725static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
238726 int bFlag;
238727
238728 assert( pWriter->iBtPage || pWriter->nEmpty==0 );
238729 if( pWriter->iBtPage==0 ) return;
238730 bFlag = fts5WriteFlushDlidx(p, pWriter);
238731
238732 if( p->rc==SQLITE_OK ){
238733 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
238734 /* The following was already done in fts5WriteInit(): */
238735 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
238736 sqlite3_bind_blob(pStmt: p->pIdxWriter, i: 2, zData: z, nData: pWriter->btterm.n, SQLITE_STATIC);
238737 sqlite3_bind_int64(pStmt: p->pIdxWriter, i: 3, iValue: bFlag + ((i64)pWriter->iBtPage<<1));
238738 sqlite3_step(pStmt: p->pIdxWriter);
238739 p->rc = sqlite3_reset(pStmt: p->pIdxWriter);
238740 sqlite3_bind_null(pStmt: p->pIdxWriter, i: 2);
238741 }
238742 pWriter->iBtPage = 0;
238743}
238744
238745/*
238746** This is called once for each leaf page except the first that contains
238747** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
238748** is larger than all terms written to earlier leaves, and equal to or
238749** smaller than the first term on the new leaf.
238750**
238751** If an error occurs, an error code is left in Fts5Index.rc. If an error
238752** has already occurred when this function is called, it is a no-op.
238753*/
238754static void fts5WriteBtreeTerm(
238755 Fts5Index *p, /* FTS5 backend object */
238756 Fts5SegWriter *pWriter, /* Writer object */
238757 int nTerm, const u8 *pTerm /* First term on new page */
238758){
238759 fts5WriteFlushBtree(p, pWriter);
238760 if( p->rc==SQLITE_OK ){
238761 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
238762 pWriter->iBtPage = pWriter->writer.pgno;
238763 }
238764}
238765
238766/*
238767** This function is called when flushing a leaf page that contains no
238768** terms at all to disk.
238769*/
238770static void fts5WriteBtreeNoTerm(
238771 Fts5Index *p, /* FTS5 backend object */
238772 Fts5SegWriter *pWriter /* Writer object */
238773){
238774 /* If there were no rowids on the leaf page either and the doclist-index
238775 ** has already been started, append an 0x00 byte to it. */
238776 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
238777 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
238778 assert( pDlidx->bPrevValid );
238779 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal: 0);
238780 }
238781
238782 /* Increment the "number of sequential leaves without a term" counter. */
238783 pWriter->nEmpty++;
238784}
238785
238786static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
238787 i64 iRowid;
238788 int iOff;
238789
238790 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
238791 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
238792 return iRowid;
238793}
238794
238795/*
238796** Rowid iRowid has just been appended to the current leaf page. It is the
238797** first on the page. This function appends an appropriate entry to the current
238798** doclist-index.
238799*/
238800static void fts5WriteDlidxAppend(
238801 Fts5Index *p,
238802 Fts5SegWriter *pWriter,
238803 i64 iRowid
238804){
238805 int i;
238806 int bDone = 0;
238807
238808 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
238809 i64 iVal;
238810 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
238811
238812 if( pDlidx->buf.n>=p->pConfig->pgsz ){
238813 /* The current doclist-index page is full. Write it to disk and push
238814 ** a copy of iRowid (which will become the first rowid on the next
238815 ** doclist-index leaf page) up into the next level of the b-tree
238816 ** hierarchy. If the node being flushed is currently the root node,
238817 ** also push its first rowid upwards. */
238818 pDlidx->buf.p[0] = 0x01; /* Not the root node */
238819 fts5DataWrite(p,
238820 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
238821 pData: pDlidx->buf.p, nData: pDlidx->buf.n
238822 );
238823 fts5WriteDlidxGrow(p, pWriter, nLvl: i+2);
238824 pDlidx = &pWriter->aDlidx[i];
238825 if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
238826 i64 iFirst = fts5DlidxExtractFirstRowid(pBuf: &pDlidx->buf);
238827
238828 /* This was the root node. Push its first rowid up to the new root. */
238829 pDlidx[1].pgno = pDlidx->pgno;
238830 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx[1].buf, iVal: 0);
238831 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx[1].buf, iVal: pDlidx->pgno);
238832 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx[1].buf, iVal: iFirst);
238833 pDlidx[1].bPrevValid = 1;
238834 pDlidx[1].iPrev = iFirst;
238835 }
238836
238837 sqlite3Fts5BufferZero(pBuf: &pDlidx->buf);
238838 pDlidx->bPrevValid = 0;
238839 pDlidx->pgno++;
238840 }else{
238841 bDone = 1;
238842 }
238843
238844 if( pDlidx->bPrevValid ){
238845 iVal = iRowid - pDlidx->iPrev;
238846 }else{
238847 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
238848 assert( pDlidx->buf.n==0 );
238849 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal: !bDone);
238850 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal: iPgno);
238851 iVal = iRowid;
238852 }
238853
238854 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal);
238855 pDlidx->bPrevValid = 1;
238856 pDlidx->iPrev = iRowid;
238857 }
238858}
238859
238860static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
238861 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
238862 Fts5PageWriter *pPage = &pWriter->writer;
238863 i64 iRowid;
238864
238865 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
238866
238867 /* Set the szLeaf header field. */
238868 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
238869 fts5PutU16(aOut: &pPage->buf.p[2], iVal: (u16)pPage->buf.n);
238870
238871 if( pWriter->bFirstTermInPage ){
238872 /* No term was written to this page. */
238873 assert( pPage->pgidx.n==0 );
238874 fts5WriteBtreeNoTerm(p, pWriter);
238875 }else{
238876 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
238877 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
238878 }
238879
238880 /* Write the page out to disk */
238881 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
238882 fts5DataWrite(p, iRowid, pData: pPage->buf.p, nData: pPage->buf.n);
238883
238884 /* Initialize the next page. */
238885 fts5BufferZero(&pPage->buf);
238886 fts5BufferZero(&pPage->pgidx);
238887 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
238888 pPage->iPrevPgidx = 0;
238889 pPage->pgno++;
238890
238891 /* Increase the leaves written counter */
238892 pWriter->nLeafWritten++;
238893
238894 /* The new leaf holds no terms or rowids */
238895 pWriter->bFirstTermInPage = 1;
238896 pWriter->bFirstRowidInPage = 1;
238897}
238898
238899/*
238900** Append term pTerm/nTerm to the segment being written by the writer passed
238901** as the second argument.
238902**
238903** If an error occurs, set the Fts5Index.rc error code. If an error has
238904** already occurred, this function is a no-op.
238905*/
238906static void fts5WriteAppendTerm(
238907 Fts5Index *p,
238908 Fts5SegWriter *pWriter,
238909 int nTerm, const u8 *pTerm
238910){
238911 int nPrefix; /* Bytes of prefix compression for term */
238912 Fts5PageWriter *pPage = &pWriter->writer;
238913 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
238914 int nMin = MIN(pPage->term.n, nTerm);
238915
238916 assert( p->rc==SQLITE_OK );
238917 assert( pPage->buf.n>=4 );
238918 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
238919
238920 /* If the current leaf page is full, flush it to disk. */
238921 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
238922 if( pPage->buf.n>4 ){
238923 fts5WriteFlushLeaf(p, pWriter);
238924 if( p->rc!=SQLITE_OK ) return;
238925 }
238926 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
238927 }
238928
238929 /* TODO1: Updating pgidx here. */
238930 pPgidx->n += sqlite3Fts5PutVarint(
238931 p: &pPgidx->p[pPgidx->n], v: pPage->buf.n - pPage->iPrevPgidx
238932 );
238933 pPage->iPrevPgidx = pPage->buf.n;
238934#if 0
238935 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
238936 pPgidx->n += 2;
238937#endif
238938
238939 if( pWriter->bFirstTermInPage ){
238940 nPrefix = 0;
238941 if( pPage->pgno!=1 ){
238942 /* This is the first term on a leaf that is not the leftmost leaf in
238943 ** the segment b-tree. In this case it is necessary to add a term to
238944 ** the b-tree hierarchy that is (a) larger than the largest term
238945 ** already written to the segment and (b) smaller than or equal to
238946 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
238947 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
238948 ** previous term.
238949 **
238950 ** Usually, the previous term is available in pPage->term. The exception
238951 ** is if this is the first term written in an incremental-merge step.
238952 ** In this case the previous term is not available, so just write a
238953 ** copy of (pTerm/nTerm) into the parent node. This is slightly
238954 ** inefficient, but still correct. */
238955 int n = nTerm;
238956 if( pPage->term.n ){
238957 n = 1 + fts5PrefixCompress(nOld: nMin, pOld: pPage->term.p, pNew: pTerm);
238958 }
238959 fts5WriteBtreeTerm(p, pWriter, nTerm: n, pTerm);
238960 if( p->rc!=SQLITE_OK ) return;
238961 pPage = &pWriter->writer;
238962 }
238963 }else{
238964 nPrefix = fts5PrefixCompress(nOld: nMin, pOld: pPage->term.p, pNew: pTerm);
238965 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
238966 }
238967
238968 /* Append the number of bytes of new data, then the term data itself
238969 ** to the page. */
238970 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
238971 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
238972
238973 /* Update the Fts5PageWriter.term field. */
238974 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
238975 pWriter->bFirstTermInPage = 0;
238976
238977 pWriter->bFirstRowidInPage = 0;
238978 pWriter->bFirstRowidInDoclist = 1;
238979
238980 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
238981 pWriter->aDlidx[0].pgno = pPage->pgno;
238982}
238983
238984/*
238985** Append a rowid and position-list size field to the writers output.
238986*/
238987static void fts5WriteAppendRowid(
238988 Fts5Index *p,
238989 Fts5SegWriter *pWriter,
238990 i64 iRowid
238991){
238992 if( p->rc==SQLITE_OK ){
238993 Fts5PageWriter *pPage = &pWriter->writer;
238994
238995 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
238996 fts5WriteFlushLeaf(p, pWriter);
238997 }
238998
238999 /* If this is to be the first rowid written to the page, set the
239000 ** rowid-pointer in the page-header. Also append a value to the dlidx
239001 ** buffer, in case a doclist-index is required. */
239002 if( pWriter->bFirstRowidInPage ){
239003 fts5PutU16(aOut: pPage->buf.p, iVal: (u16)pPage->buf.n);
239004 fts5WriteDlidxAppend(p, pWriter, iRowid);
239005 }
239006
239007 /* Write the rowid. */
239008 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
239009 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
239010 }else{
239011 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
239012 fts5BufferAppendVarint(&p->rc, &pPage->buf,
239013 (u64)iRowid - (u64)pWriter->iPrevRowid
239014 );
239015 }
239016 pWriter->iPrevRowid = iRowid;
239017 pWriter->bFirstRowidInDoclist = 0;
239018 pWriter->bFirstRowidInPage = 0;
239019 }
239020}
239021
239022static void fts5WriteAppendPoslistData(
239023 Fts5Index *p,
239024 Fts5SegWriter *pWriter,
239025 const u8 *aData,
239026 int nData
239027){
239028 Fts5PageWriter *pPage = &pWriter->writer;
239029 const u8 *a = aData;
239030 int n = nData;
239031
239032 assert( p->pConfig->pgsz>0 );
239033 while( p->rc==SQLITE_OK
239034 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
239035 ){
239036 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
239037 int nCopy = 0;
239038 while( nCopy<nReq ){
239039 i64 dummy;
239040 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
239041 }
239042 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
239043 a += nCopy;
239044 n -= nCopy;
239045 fts5WriteFlushLeaf(p, pWriter);
239046 }
239047 if( n>0 ){
239048 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
239049 }
239050}
239051
239052/*
239053** Flush any data cached by the writer object to the database. Free any
239054** allocations associated with the writer.
239055*/
239056static void fts5WriteFinish(
239057 Fts5Index *p,
239058 Fts5SegWriter *pWriter, /* Writer object */
239059 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
239060){
239061 int i;
239062 Fts5PageWriter *pLeaf = &pWriter->writer;
239063 if( p->rc==SQLITE_OK ){
239064 assert( pLeaf->pgno>=1 );
239065 if( pLeaf->buf.n>4 ){
239066 fts5WriteFlushLeaf(p, pWriter);
239067 }
239068 *pnLeaf = pLeaf->pgno-1;
239069 if( pLeaf->pgno>1 ){
239070 fts5WriteFlushBtree(p, pWriter);
239071 }
239072 }
239073 fts5BufferFree(&pLeaf->term);
239074 fts5BufferFree(&pLeaf->buf);
239075 fts5BufferFree(&pLeaf->pgidx);
239076 fts5BufferFree(&pWriter->btterm);
239077
239078 for(i=0; i<pWriter->nDlidx; i++){
239079 sqlite3Fts5BufferFree(pBuf: &pWriter->aDlidx[i].buf);
239080 }
239081 sqlite3_free(p: pWriter->aDlidx);
239082}
239083
239084static void fts5WriteInit(
239085 Fts5Index *p,
239086 Fts5SegWriter *pWriter,
239087 int iSegid
239088){
239089 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
239090
239091 memset(s: pWriter, c: 0, n: sizeof(Fts5SegWriter));
239092 pWriter->iSegid = iSegid;
239093
239094 fts5WriteDlidxGrow(p, pWriter, nLvl: 1);
239095 pWriter->writer.pgno = 1;
239096 pWriter->bFirstTermInPage = 1;
239097 pWriter->iBtPage = 1;
239098
239099 assert( pWriter->writer.buf.n==0 );
239100 assert( pWriter->writer.pgidx.n==0 );
239101
239102 /* Grow the two buffers to pgsz + padding bytes in size. */
239103 sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &pWriter->writer.pgidx, nByte: nBuffer);
239104 sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &pWriter->writer.buf, nByte: nBuffer);
239105
239106 if( p->pIdxWriter==0 ){
239107 Fts5Config *pConfig = p->pConfig;
239108 fts5IndexPrepareStmt(p, ppStmt: &p->pIdxWriter, zSql: sqlite3_mprintf(
239109 zFormat: "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
239110 pConfig->zDb, pConfig->zName
239111 ));
239112 }
239113
239114 if( p->rc==SQLITE_OK ){
239115 /* Initialize the 4-byte leaf-page header to 0x00. */
239116 memset(s: pWriter->writer.buf.p, c: 0, n: 4);
239117 pWriter->writer.buf.n = 4;
239118
239119 /* Bind the current output segment id to the index-writer. This is an
239120 ** optimization over binding the same value over and over as rows are
239121 ** inserted into %_idx by the current writer. */
239122 sqlite3_bind_int(p: p->pIdxWriter, i: 1, iValue: pWriter->iSegid);
239123 }
239124}
239125
239126/*
239127** Iterator pIter was used to iterate through the input segments of on an
239128** incremental merge operation. This function is called if the incremental
239129** merge step has finished but the input has not been completely exhausted.
239130*/
239131static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
239132 int i;
239133 Fts5Buffer buf;
239134 memset(s: &buf, c: 0, n: sizeof(Fts5Buffer));
239135 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){
239136 Fts5SegIter *pSeg = &pIter->aSeg[i];
239137 if( pSeg->pSeg==0 ){
239138 /* no-op */
239139 }else if( pSeg->pLeaf==0 ){
239140 /* All keys from this input segment have been transfered to the output.
239141 ** Set both the first and last page-numbers to 0 to indicate that the
239142 ** segment is now empty. */
239143 pSeg->pSeg->pgnoLast = 0;
239144 pSeg->pSeg->pgnoFirst = 0;
239145 }else{
239146 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
239147 i64 iLeafRowid;
239148 Fts5Data *pData;
239149 int iId = pSeg->pSeg->iSegid;
239150 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
239151
239152 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
239153 pData = fts5LeafRead(p, iRowid: iLeafRowid);
239154 if( pData ){
239155 if( iOff>pData->szLeaf ){
239156 /* This can occur if the pages that the segments occupy overlap - if
239157 ** a single page has been assigned to more than one segment. In
239158 ** this case a prior iteration of this loop may have corrupted the
239159 ** segment currently being trimmed. */
239160 p->rc = FTS5_CORRUPT;
239161 }else{
239162 fts5BufferZero(&buf);
239163 fts5BufferGrow(&p->rc, &buf, pData->nn);
239164 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
239165 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
239166 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
239167 fts5BufferAppendBlob(&p->rc, &buf,pData->szLeaf-iOff,&pData->p[iOff]);
239168 if( p->rc==SQLITE_OK ){
239169 /* Set the szLeaf field */
239170 fts5PutU16(aOut: &buf.p[2], iVal: (u16)buf.n);
239171 }
239172
239173 /* Set up the new page-index array */
239174 fts5BufferAppendVarint(&p->rc, &buf, 4);
239175 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
239176 && pSeg->iEndofDoclist<pData->szLeaf
239177 && pSeg->iPgidxOff<=pData->nn
239178 ){
239179 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
239180 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
239181 fts5BufferAppendBlob(&p->rc, &buf,
239182 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
239183 );
239184 }
239185
239186 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
239187 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLast: iLeafRowid);
239188 fts5DataWrite(p, iRowid: iLeafRowid, pData: buf.p, nData: buf.n);
239189 }
239190 fts5DataRelease(pData);
239191 }
239192 }
239193 }
239194 fts5BufferFree(&buf);
239195}
239196
239197static void fts5MergeChunkCallback(
239198 Fts5Index *p,
239199 void *pCtx,
239200 const u8 *pChunk, int nChunk
239201){
239202 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
239203 fts5WriteAppendPoslistData(p, pWriter, aData: pChunk, nData: nChunk);
239204}
239205
239206/*
239207**
239208*/
239209static void fts5IndexMergeLevel(
239210 Fts5Index *p, /* FTS5 backend object */
239211 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
239212 int iLvl, /* Level to read input from */
239213 int *pnRem /* Write up to this many output leaves */
239214){
239215 Fts5Structure *pStruct = *ppStruct;
239216 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
239217 Fts5StructureLevel *pLvlOut;
239218 Fts5Iter *pIter = 0; /* Iterator to read input data */
239219 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
239220 int nInput; /* Number of input segments */
239221 Fts5SegWriter writer; /* Writer object */
239222 Fts5StructureSegment *pSeg; /* Output segment */
239223 Fts5Buffer term;
239224 int bOldest; /* True if the output segment is the oldest */
239225 int eDetail = p->pConfig->eDetail;
239226 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
239227 int bTermWritten = 0; /* True if current term already output */
239228
239229 assert( iLvl<pStruct->nLevel );
239230 assert( pLvl->nMerge<=pLvl->nSeg );
239231
239232 memset(s: &writer, c: 0, n: sizeof(Fts5SegWriter));
239233 memset(s: &term, c: 0, n: sizeof(Fts5Buffer));
239234 if( pLvl->nMerge ){
239235 pLvlOut = &pStruct->aLevel[iLvl+1];
239236 assert( pLvlOut->nSeg>0 );
239237 nInput = pLvl->nMerge;
239238 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
239239
239240 fts5WriteInit(p, pWriter: &writer, iSegid: pSeg->iSegid);
239241 writer.writer.pgno = pSeg->pgnoLast+1;
239242 writer.iBtPage = 0;
239243 }else{
239244 int iSegid = fts5AllocateSegid(p, pStruct);
239245
239246 /* Extend the Fts5Structure object as required to ensure the output
239247 ** segment exists. */
239248 if( iLvl==pStruct->nLevel-1 ){
239249 fts5StructureAddLevel(pRc: &p->rc, ppStruct);
239250 pStruct = *ppStruct;
239251 }
239252 fts5StructureExtendLevel(pRc: &p->rc, pStruct, iLvl: iLvl+1, nExtra: 1, bInsert: 0);
239253 if( p->rc ) return;
239254 pLvl = &pStruct->aLevel[iLvl];
239255 pLvlOut = &pStruct->aLevel[iLvl+1];
239256
239257 fts5WriteInit(p, pWriter: &writer, iSegid);
239258
239259 /* Add the new segment to the output level */
239260 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
239261 pLvlOut->nSeg++;
239262 pSeg->pgnoFirst = 1;
239263 pSeg->iSegid = iSegid;
239264 pStruct->nSegment++;
239265
239266 /* Read input from all segments in the input level */
239267 nInput = pLvl->nSeg;
239268
239269 /* Set the range of origins that will go into the output segment. */
239270 if( pStruct->nOriginCntr>0 ){
239271 pSeg->iOrigin1 = pLvl->aSeg[0].iOrigin1;
239272 pSeg->iOrigin2 = pLvl->aSeg[pLvl->nSeg-1].iOrigin2;
239273 }
239274 }
239275 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
239276
239277 assert( iLvl>=0 );
239278 for(fts5MultiIterNew(p, pStruct, flags, pColset: 0, pTerm: 0, nTerm: 0, iLevel: iLvl, nSegment: nInput, ppOut: &pIter);
239279 fts5MultiIterEof(p, pIter)==0;
239280 fts5MultiIterNext(p, pIter, bFrom: 0, iFrom: 0)
239281 ){
239282 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
239283 int nPos; /* position-list size field value */
239284 int nTerm;
239285 const u8 *pTerm;
239286
239287 pTerm = fts5MultiIterTerm(pIter, pn: &nTerm);
239288 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
239289 if( pnRem && writer.nLeafWritten>nRem ){
239290 break;
239291 }
239292 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
239293 bTermWritten =0;
239294 }
239295
239296 /* Check for key annihilation. */
239297 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
239298
239299 if( p->rc==SQLITE_OK && bTermWritten==0 ){
239300 /* This is a new term. Append a term to the output segment. */
239301 fts5WriteAppendTerm(p, pWriter: &writer, nTerm, pTerm);
239302 bTermWritten = 1;
239303 }
239304
239305 /* Append the rowid to the output */
239306 /* WRITEPOSLISTSIZE */
239307 fts5WriteAppendRowid(p, pWriter: &writer, iRowid: fts5MultiIterRowid(pIter));
239308
239309 if( eDetail==FTS5_DETAIL_NONE ){
239310 if( pSegIter->bDel ){
239311 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
239312 if( pSegIter->nPos>0 ){
239313 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
239314 }
239315 }
239316 }else{
239317 /* Append the position-list data to the output */
239318 nPos = pSegIter->nPos*2 + pSegIter->bDel;
239319 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
239320 fts5ChunkIterate(p, pSeg: pSegIter, pCtx: (void*)&writer, xChunk: fts5MergeChunkCallback);
239321 }
239322 }
239323
239324 /* Flush the last leaf page to disk. Set the output segment b-tree height
239325 ** and last leaf page number at the same time. */
239326 fts5WriteFinish(p, pWriter: &writer, pnLeaf: &pSeg->pgnoLast);
239327
239328 assert( pIter!=0 || p->rc!=SQLITE_OK );
239329 if( fts5MultiIterEof(p, pIter) ){
239330 int i;
239331
239332 /* Remove the redundant segments from the %_data table */
239333 assert( pSeg->nEntry==0 );
239334 for(i=0; i<nInput; i++){
239335 Fts5StructureSegment *pOld = &pLvl->aSeg[i];
239336 pSeg->nEntry += (pOld->nEntry - pOld->nEntryTombstone);
239337 fts5DataRemoveSegment(p, pSeg: pOld);
239338 }
239339
239340 /* Remove the redundant segments from the input level */
239341 if( pLvl->nSeg!=nInput ){
239342 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
239343 memmove(dest: pLvl->aSeg, src: &pLvl->aSeg[nInput], n: nMove);
239344 }
239345 pStruct->nSegment -= nInput;
239346 pLvl->nSeg -= nInput;
239347 pLvl->nMerge = 0;
239348 if( pSeg->pgnoLast==0 ){
239349 pLvlOut->nSeg--;
239350 pStruct->nSegment--;
239351 }
239352 }else{
239353 assert( pSeg->pgnoLast>0 );
239354 fts5TrimSegments(p, pIter);
239355 pLvl->nMerge = nInput;
239356 }
239357
239358 fts5MultiIterFree(pIter);
239359 fts5BufferFree(&term);
239360 if( pnRem ) *pnRem -= writer.nLeafWritten;
239361}
239362
239363/*
239364** If this is not a contentless_delete=1 table, or if the 'deletemerge'
239365** configuration option is set to 0, then this function always returns -1.
239366** Otherwise, it searches the structure object passed as the second argument
239367** for a level suitable for merging due to having a large number of
239368** tombstones in the tombstone hash. If one is found, its index is returned.
239369** Otherwise, if there is no suitable level, -1.
239370*/
239371static int fts5IndexFindDeleteMerge(Fts5Index *p, Fts5Structure *pStruct){
239372 Fts5Config *pConfig = p->pConfig;
239373 int iRet = -1;
239374 if( pConfig->bContentlessDelete && pConfig->nDeleteMerge>0 ){
239375 int ii;
239376 int nBest = 0;
239377
239378 for(ii=0; ii<pStruct->nLevel; ii++){
239379 Fts5StructureLevel *pLvl = &pStruct->aLevel[ii];
239380 i64 nEntry = 0;
239381 i64 nTomb = 0;
239382 int iSeg;
239383 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
239384 nEntry += pLvl->aSeg[iSeg].nEntry;
239385 nTomb += pLvl->aSeg[iSeg].nEntryTombstone;
239386 }
239387 assert_nc( nEntry>0 || pLvl->nSeg==0 );
239388 if( nEntry>0 ){
239389 int nPercent = (nTomb * 100) / nEntry;
239390 if( nPercent>=pConfig->nDeleteMerge && nPercent>nBest ){
239391 iRet = ii;
239392 nBest = nPercent;
239393 }
239394 }
239395 }
239396 }
239397 return iRet;
239398}
239399
239400/*
239401** Do up to nPg pages of automerge work on the index.
239402**
239403** Return true if any changes were actually made, or false otherwise.
239404*/
239405static int fts5IndexMerge(
239406 Fts5Index *p, /* FTS5 backend object */
239407 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
239408 int nPg, /* Pages of work to do */
239409 int nMin /* Minimum number of segments to merge */
239410){
239411 int nRem = nPg;
239412 int bRet = 0;
239413 Fts5Structure *pStruct = *ppStruct;
239414 while( nRem>0 && p->rc==SQLITE_OK ){
239415 int iLvl; /* To iterate through levels */
239416 int iBestLvl = 0; /* Level offering the most input segments */
239417 int nBest = 0; /* Number of input segments on best level */
239418
239419 /* Set iBestLvl to the level to read input segments from. Or to -1 if
239420 ** there is no level suitable to merge segments from. */
239421 assert( pStruct->nLevel>0 );
239422 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
239423 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
239424 if( pLvl->nMerge ){
239425 if( pLvl->nMerge>nBest ){
239426 iBestLvl = iLvl;
239427 nBest = nMin;
239428 }
239429 break;
239430 }
239431 if( pLvl->nSeg>nBest ){
239432 nBest = pLvl->nSeg;
239433 iBestLvl = iLvl;
239434 }
239435 }
239436 if( nBest<nMin ){
239437 iBestLvl = fts5IndexFindDeleteMerge(p, pStruct);
239438 }
239439
239440 if( iBestLvl<0 ) break;
239441 bRet = 1;
239442 fts5IndexMergeLevel(p, ppStruct: &pStruct, iLvl: iBestLvl, pnRem: &nRem);
239443 if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
239444 fts5StructurePromote(p, iLvl: iBestLvl+1, pStruct);
239445 }
239446
239447 if( nMin==1 ) nMin = 2;
239448 }
239449 *ppStruct = pStruct;
239450 return bRet;
239451}
239452
239453/*
239454** A total of nLeaf leaf pages of data has just been flushed to a level-0
239455** segment. This function updates the write-counter accordingly and, if
239456** necessary, performs incremental merge work.
239457**
239458** If an error occurs, set the Fts5Index.rc error code. If an error has
239459** already occurred, this function is a no-op.
239460*/
239461static void fts5IndexAutomerge(
239462 Fts5Index *p, /* FTS5 backend object */
239463 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
239464 int nLeaf /* Number of output leaves just written */
239465){
239466 if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0) ){
239467 Fts5Structure *pStruct = *ppStruct;
239468 u64 nWrite; /* Initial value of write-counter */
239469 int nWork; /* Number of work-quanta to perform */
239470 int nRem; /* Number of leaf pages left to write */
239471
239472 /* Update the write-counter. While doing so, set nWork. */
239473 nWrite = pStruct->nWriteCounter;
239474 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
239475 pStruct->nWriteCounter += nLeaf;
239476 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
239477
239478 fts5IndexMerge(p, ppStruct, nPg: nRem, nMin: p->pConfig->nAutomerge);
239479 }
239480}
239481
239482static void fts5IndexCrisismerge(
239483 Fts5Index *p, /* FTS5 backend object */
239484 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
239485){
239486 const int nCrisis = p->pConfig->nCrisisMerge;
239487 Fts5Structure *pStruct = *ppStruct;
239488 if( pStruct && pStruct->nLevel>0 ){
239489 int iLvl = 0;
239490 while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
239491 fts5IndexMergeLevel(p, ppStruct: &pStruct, iLvl, pnRem: 0);
239492 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
239493 fts5StructurePromote(p, iLvl: iLvl+1, pStruct);
239494 iLvl++;
239495 }
239496 *ppStruct = pStruct;
239497 }
239498}
239499
239500static int fts5IndexReturn(Fts5Index *p){
239501 int rc = p->rc;
239502 p->rc = SQLITE_OK;
239503 return rc;
239504}
239505
239506typedef struct Fts5FlushCtx Fts5FlushCtx;
239507struct Fts5FlushCtx {
239508 Fts5Index *pIdx;
239509 Fts5SegWriter writer;
239510};
239511
239512/*
239513** Buffer aBuf[] contains a list of varints, all small enough to fit
239514** in a 32-bit integer. Return the size of the largest prefix of this
239515** list nMax bytes or less in size.
239516*/
239517static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
239518 int ret;
239519 u32 dummy;
239520 ret = fts5GetVarint32(aBuf, dummy);
239521 if( ret<nMax ){
239522 while( 1 ){
239523 int i = fts5GetVarint32(&aBuf[ret], dummy);
239524 if( (ret + i) > nMax ) break;
239525 ret += i;
239526 }
239527 }
239528 return ret;
239529}
239530
239531/*
239532** Execute the SQL statement:
239533**
239534** DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno);
239535**
239536** This is used when a secure-delete operation removes the last term
239537** from a segment leaf page. In that case the %_idx entry is removed
239538** too. This is done to ensure that if all instances of a token are
239539** removed from an fts5 database in secure-delete mode, no trace of
239540** the token itself remains in the database.
239541*/
239542static void fts5SecureDeleteIdxEntry(
239543 Fts5Index *p, /* FTS5 backend object */
239544 int iSegid, /* Id of segment to delete entry for */
239545 int iPgno /* Page number within segment */
239546){
239547 if( iPgno!=1 ){
239548 assert( p->pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE );
239549 if( p->pDeleteFromIdx==0 ){
239550 fts5IndexPrepareStmt(p, ppStmt: &p->pDeleteFromIdx, zSql: sqlite3_mprintf(
239551 zFormat: "DELETE FROM '%q'.'%q_idx' WHERE (segid, (pgno/2)) = (?1, ?2)",
239552 p->pConfig->zDb, p->pConfig->zName
239553 ));
239554 }
239555 if( p->rc==SQLITE_OK ){
239556 sqlite3_bind_int(p: p->pDeleteFromIdx, i: 1, iValue: iSegid);
239557 sqlite3_bind_int(p: p->pDeleteFromIdx, i: 2, iValue: iPgno);
239558 sqlite3_step(pStmt: p->pDeleteFromIdx);
239559 p->rc = sqlite3_reset(pStmt: p->pDeleteFromIdx);
239560 }
239561 }
239562}
239563
239564/*
239565** This is called when a secure-delete operation removes a position-list
239566** that overflows onto segment page iPgno of segment pSeg. This function
239567** rewrites node iPgno, and possibly one or more of its right-hand peers,
239568** to remove this portion of the position list.
239569**
239570** Output variable (*pbLastInDoclist) is set to true if the position-list
239571** removed is followed by a new term or the end-of-segment, or false if
239572** it is followed by another rowid/position list.
239573*/
239574static void fts5SecureDeleteOverflow(
239575 Fts5Index *p,
239576 Fts5StructureSegment *pSeg,
239577 int iPgno,
239578 int *pbLastInDoclist
239579){
239580 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
239581 int pgno;
239582 Fts5Data *pLeaf = 0;
239583 assert( iPgno!=1 );
239584
239585 *pbLastInDoclist = 1;
239586 for(pgno=iPgno; p->rc==SQLITE_OK && pgno<=pSeg->pgnoLast; pgno++){
239587 i64 iRowid = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
239588 int iNext = 0;
239589 u8 *aPg = 0;
239590
239591 pLeaf = fts5DataRead(p, iRowid);
239592 if( pLeaf==0 ) break;
239593 aPg = pLeaf->p;
239594
239595 iNext = fts5GetU16(aIn: &aPg[0]);
239596 if( iNext!=0 ){
239597 *pbLastInDoclist = 0;
239598 }
239599 if( iNext==0 && pLeaf->szLeaf!=pLeaf->nn ){
239600 fts5GetVarint32(&aPg[pLeaf->szLeaf], iNext);
239601 }
239602
239603 if( iNext==0 ){
239604 /* The page contains no terms or rowids. Replace it with an empty
239605 ** page and move on to the right-hand peer. */
239606 const u8 aEmpty[] = {0x00, 0x00, 0x00, 0x04};
239607 assert_nc( bDetailNone==0 || pLeaf->nn==4 );
239608 if( bDetailNone==0 ) fts5DataWrite(p, iRowid, pData: aEmpty, nData: sizeof(aEmpty));
239609 fts5DataRelease(pData: pLeaf);
239610 pLeaf = 0;
239611 }else if( bDetailNone ){
239612 break;
239613 }else if( iNext>=pLeaf->szLeaf || pLeaf->nn<pLeaf->szLeaf || iNext<4 ){
239614 p->rc = FTS5_CORRUPT;
239615 break;
239616 }else{
239617 int nShift = iNext - 4;
239618 int nPg;
239619
239620 int nIdx = 0;
239621 u8 *aIdx = 0;
239622
239623 /* Unless the current page footer is 0 bytes in size (in which case
239624 ** the new page footer will be as well), allocate and populate a
239625 ** buffer containing the new page footer. Set stack variables aIdx
239626 ** and nIdx accordingly. */
239627 if( pLeaf->nn>pLeaf->szLeaf ){
239628 int iFirst = 0;
239629 int i1 = pLeaf->szLeaf;
239630 int i2 = 0;
239631
239632 i1 += fts5GetVarint32(&aPg[i1], iFirst);
239633 if( iFirst<iNext ){
239634 p->rc = FTS5_CORRUPT;
239635 break;
239636 }
239637 aIdx = sqlite3Fts5MallocZero(pRc: &p->rc, nByte: (pLeaf->nn-pLeaf->szLeaf)+2);
239638 if( aIdx==0 ) break;
239639 i2 = sqlite3Fts5PutVarint(p: aIdx, v: iFirst-nShift);
239640 if( i1<pLeaf->nn ){
239641 memcpy(dest: &aIdx[i2], src: &aPg[i1], n: pLeaf->nn-i1);
239642 i2 += (pLeaf->nn-i1);
239643 }
239644 nIdx = i2;
239645 }
239646
239647 /* Modify the contents of buffer aPg[]. Set nPg to the new size
239648 ** in bytes. The new page is always smaller than the old. */
239649 nPg = pLeaf->szLeaf - nShift;
239650 memmove(dest: &aPg[4], src: &aPg[4+nShift], n: nPg-4);
239651 fts5PutU16(aOut: &aPg[2], iVal: nPg);
239652 if( fts5GetU16(aIn: &aPg[0]) ) fts5PutU16(aOut: &aPg[0], iVal: 4);
239653 if( nIdx>0 ){
239654 memcpy(dest: &aPg[nPg], src: aIdx, n: nIdx);
239655 nPg += nIdx;
239656 }
239657 sqlite3_free(p: aIdx);
239658
239659 /* Write the new page to disk and exit the loop */
239660 assert( nPg>4 || fts5GetU16(aPg)==0 );
239661 fts5DataWrite(p, iRowid, pData: aPg, nData: nPg);
239662 break;
239663 }
239664 }
239665 fts5DataRelease(pData: pLeaf);
239666}
239667
239668/*
239669** Completely remove the entry that pSeg currently points to from
239670** the database.
239671*/
239672static void fts5DoSecureDelete(
239673 Fts5Index *p,
239674 Fts5SegIter *pSeg
239675){
239676 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
239677 int iSegid = pSeg->pSeg->iSegid;
239678 u8 *aPg = pSeg->pLeaf->p;
239679 int nPg = pSeg->pLeaf->nn;
239680 int iPgIdx = pSeg->pLeaf->szLeaf;
239681
239682 u64 iDelta = 0;
239683 u64 iNextDelta = 0;
239684 int iNextOff = 0;
239685 int iOff = 0;
239686 int nIdx = 0;
239687 u8 *aIdx = 0;
239688 int bLastInDoclist = 0;
239689 int iIdx = 0;
239690 int iStart = 0;
239691 int iKeyOff = 0;
239692 int iPrevKeyOff = 0;
239693 int iDelKeyOff = 0; /* Offset of deleted key, if any */
239694
239695 nIdx = nPg-iPgIdx;
239696 aIdx = sqlite3Fts5MallocZero(pRc: &p->rc, nByte: nIdx+16);
239697 if( p->rc ) return;
239698 memcpy(dest: aIdx, src: &aPg[iPgIdx], n: nIdx);
239699
239700 /* At this point segment iterator pSeg points to the entry
239701 ** this function should remove from the b-tree segment.
239702 **
239703 ** In detail=full or detail=column mode, pSeg->iLeafOffset is the
239704 ** offset of the first byte in the position-list for the entry to
239705 ** remove. Immediately before this comes two varints that will also
239706 ** need to be removed:
239707 **
239708 ** + the rowid or delta rowid value for the entry, and
239709 ** + the size of the position list in bytes.
239710 **
239711 ** Or, in detail=none mode, there is a single varint prior to
239712 ** pSeg->iLeafOffset - the rowid or delta rowid value.
239713 **
239714 ** This block sets the following variables:
239715 **
239716 ** iStart:
239717 ** iDelta:
239718 */
239719 {
239720 int iSOP;
239721 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno ){
239722 iStart = pSeg->iTermLeafOffset;
239723 }else{
239724 iStart = fts5GetU16(aIn: &aPg[0]);
239725 }
239726
239727 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
239728 assert_nc( iSOP<=pSeg->iLeafOffset );
239729
239730 if( bDetailNone ){
239731 while( iSOP<pSeg->iLeafOffset ){
239732 if( aPg[iSOP]==0x00 ) iSOP++;
239733 if( aPg[iSOP]==0x00 ) iSOP++;
239734 iStart = iSOP;
239735 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
239736 }
239737
239738 iNextOff = iSOP;
239739 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
239740 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
239741
239742 }else{
239743 int nPos = 0;
239744 iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
239745 while( iSOP<pSeg->iLeafOffset ){
239746 iStart = iSOP + (nPos/2);
239747 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
239748 iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
239749 }
239750 assert_nc( iSOP==pSeg->iLeafOffset );
239751 iNextOff = pSeg->iLeafOffset + pSeg->nPos;
239752 }
239753 }
239754
239755 iOff = iStart;
239756 if( iNextOff>=iPgIdx ){
239757 int pgno = pSeg->iLeafPgno+1;
239758 fts5SecureDeleteOverflow(p, pSeg: pSeg->pSeg, iPgno: pgno, pbLastInDoclist: &bLastInDoclist);
239759 iNextOff = iPgIdx;
239760 }else{
239761 /* Set bLastInDoclist to true if the entry being removed is the last
239762 ** in its doclist. */
239763 for(iIdx=0, iKeyOff=0; iIdx<nIdx; /* no-op */){
239764 u32 iVal = 0;
239765 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
239766 iKeyOff += iVal;
239767 if( iKeyOff==iNextOff ){
239768 bLastInDoclist = 1;
239769 }
239770 }
239771 }
239772
239773 if( fts5GetU16(aIn: &aPg[0])==iStart && (bLastInDoclist||iNextOff==iPgIdx) ){
239774 fts5PutU16(aOut: &aPg[0], iVal: 0);
239775 }
239776
239777 if( bLastInDoclist==0 ){
239778 if( iNextOff!=iPgIdx ){
239779 iNextOff += fts5GetVarint(&aPg[iNextOff], &iNextDelta);
239780 iOff += sqlite3Fts5PutVarint(p: &aPg[iOff], v: iDelta + iNextDelta);
239781 }
239782 }else if(
239783 iStart==pSeg->iTermLeafOffset && pSeg->iLeafPgno==pSeg->iTermLeafPgno
239784 ){
239785 /* The entry being removed was the only position list in its
239786 ** doclist. Therefore the term needs to be removed as well. */
239787 int iKey = 0;
239788 for(iIdx=0, iKeyOff=0; iIdx<nIdx; iKey++){
239789 u32 iVal = 0;
239790 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
239791 if( (iKeyOff+iVal)>(u32)iStart ) break;
239792 iKeyOff += iVal;
239793 }
239794
239795 iDelKeyOff = iOff = iKeyOff;
239796 if( iNextOff!=iPgIdx ){
239797 int nPrefix = 0;
239798 int nSuffix = 0;
239799 int nPrefix2 = 0;
239800 int nSuffix2 = 0;
239801
239802 iDelKeyOff = iNextOff;
239803 iNextOff += fts5GetVarint32(&aPg[iNextOff], nPrefix2);
239804 iNextOff += fts5GetVarint32(&aPg[iNextOff], nSuffix2);
239805
239806 if( iKey!=1 ){
239807 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nPrefix);
239808 }
239809 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nSuffix);
239810
239811 nPrefix = MIN(nPrefix, nPrefix2);
239812 nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
239813
239814 if( (iKeyOff+nSuffix)>iPgIdx || (iNextOff+nSuffix2)>iPgIdx ){
239815 p->rc = FTS5_CORRUPT;
239816 }else{
239817 if( iKey!=1 ){
239818 iOff += sqlite3Fts5PutVarint(p: &aPg[iOff], v: nPrefix);
239819 }
239820 iOff += sqlite3Fts5PutVarint(p: &aPg[iOff], v: nSuffix);
239821 if( nPrefix2>pSeg->term.n ){
239822 p->rc = FTS5_CORRUPT;
239823 }else if( nPrefix2>nPrefix ){
239824 memcpy(dest: &aPg[iOff], src: &pSeg->term.p[nPrefix], n: nPrefix2-nPrefix);
239825 iOff += (nPrefix2-nPrefix);
239826 }
239827 memmove(dest: &aPg[iOff], src: &aPg[iNextOff], n: nSuffix2);
239828 iOff += nSuffix2;
239829 iNextOff += nSuffix2;
239830 }
239831 }
239832 }else if( iStart==4 ){
239833 int iPgno;
239834
239835 assert_nc( pSeg->iLeafPgno>pSeg->iTermLeafPgno );
239836 /* The entry being removed may be the only position list in
239837 ** its doclist. */
239838 for(iPgno=pSeg->iLeafPgno-1; iPgno>pSeg->iTermLeafPgno; iPgno-- ){
239839 Fts5Data *pPg = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno));
239840 int bEmpty = (pPg && pPg->nn==4);
239841 fts5DataRelease(pData: pPg);
239842 if( bEmpty==0 ) break;
239843 }
239844
239845 if( iPgno==pSeg->iTermLeafPgno ){
239846 i64 iId = FTS5_SEGMENT_ROWID(iSegid, pSeg->iTermLeafPgno);
239847 Fts5Data *pTerm = fts5DataRead(p, iRowid: iId);
239848 if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
239849 u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
239850 int nTermIdx = pTerm->nn - pTerm->szLeaf;
239851 int iTermIdx = 0;
239852 int iTermOff = 0;
239853
239854 while( 1 ){
239855 u32 iVal = 0;
239856 int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal);
239857 iTermOff += iVal;
239858 if( (iTermIdx+nByte)>=nTermIdx ) break;
239859 iTermIdx += nByte;
239860 }
239861 nTermIdx = iTermIdx;
239862
239863 memmove(dest: &pTerm->p[iTermOff], src: &pTerm->p[pTerm->szLeaf], n: nTermIdx);
239864 fts5PutU16(aOut: &pTerm->p[2], iVal: iTermOff);
239865
239866 fts5DataWrite(p, iRowid: iId, pData: pTerm->p, nData: iTermOff+nTermIdx);
239867 if( nTermIdx==0 ){
239868 fts5SecureDeleteIdxEntry(p, iSegid, iPgno: pSeg->iTermLeafPgno);
239869 }
239870 }
239871 fts5DataRelease(pData: pTerm);
239872 }
239873 }
239874
239875 if( p->rc==SQLITE_OK ){
239876 const int nMove = nPg - iNextOff; /* Number of bytes to move */
239877 int nShift = iNextOff - iOff; /* Distance to move them */
239878
239879 int iPrevKeyOut = 0;
239880 int iKeyIn = 0;
239881
239882 memmove(dest: &aPg[iOff], src: &aPg[iNextOff], n: nMove);
239883 iPgIdx -= nShift;
239884 nPg = iPgIdx;
239885 fts5PutU16(aOut: &aPg[2], iVal: iPgIdx);
239886
239887 for(iIdx=0; iIdx<nIdx; /* no-op */){
239888 u32 iVal = 0;
239889 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
239890 iKeyIn += iVal;
239891 if( iKeyIn!=iDelKeyOff ){
239892 int iKeyOut = (iKeyIn - (iKeyIn>iOff ? nShift : 0));
239893 nPg += sqlite3Fts5PutVarint(p: &aPg[nPg], v: iKeyOut - iPrevKeyOut);
239894 iPrevKeyOut = iKeyOut;
239895 }
239896 }
239897
239898 if( iPgIdx==nPg && nIdx>0 && pSeg->iLeafPgno!=1 ){
239899 fts5SecureDeleteIdxEntry(p, iSegid, iPgno: pSeg->iLeafPgno);
239900 }
239901
239902 assert_nc( nPg>4 || fts5GetU16(aPg)==0 );
239903 fts5DataWrite(p, FTS5_SEGMENT_ROWID(iSegid,pSeg->iLeafPgno), pData: aPg, nData: nPg);
239904 }
239905 sqlite3_free(p: aIdx);
239906}
239907
239908/*
239909** This is called as part of flushing a delete to disk in 'secure-delete'
239910** mode. It edits the segments within the database described by argument
239911** pStruct to remove the entries for term zTerm, rowid iRowid.
239912*/
239913static void fts5FlushSecureDelete(
239914 Fts5Index *p,
239915 Fts5Structure *pStruct,
239916 const char *zTerm,
239917 i64 iRowid
239918){
239919 const int f = FTS5INDEX_QUERY_SKIPHASH;
239920 int nTerm = (int)strlen(s: zTerm);
239921 Fts5Iter *pIter = 0; /* Used to find term instance */
239922
239923 fts5MultiIterNew(p, pStruct, flags: f, pColset: 0, pTerm: (const u8*)zTerm, nTerm, iLevel: -1, nSegment: 0, ppOut: &pIter);
239924 if( fts5MultiIterEof(p, pIter)==0 ){
239925 i64 iThis = fts5MultiIterRowid(pIter);
239926 if( iThis<iRowid ){
239927 fts5MultiIterNextFrom(p, pIter, iMatch: iRowid);
239928 }
239929
239930 if( p->rc==SQLITE_OK
239931 && fts5MultiIterEof(p, pIter)==0
239932 && iRowid==fts5MultiIterRowid(pIter)
239933 ){
239934 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
239935 fts5DoSecureDelete(p, pSeg);
239936 }
239937 }
239938
239939 fts5MultiIterFree(pIter);
239940}
239941
239942
239943/*
239944** Flush the contents of in-memory hash table iHash to a new level-0
239945** segment on disk. Also update the corresponding structure record.
239946**
239947** If an error occurs, set the Fts5Index.rc error code. If an error has
239948** already occurred, this function is a no-op.
239949*/
239950static void fts5FlushOneHash(Fts5Index *p){
239951 Fts5Hash *pHash = p->pHash;
239952 Fts5Structure *pStruct;
239953 int iSegid;
239954 int pgnoLast = 0; /* Last leaf page number in segment */
239955
239956 /* Obtain a reference to the index structure and allocate a new segment-id
239957 ** for the new level-0 segment. */
239958 pStruct = fts5StructureRead(p);
239959 fts5StructureInvalidate(p);
239960
239961 if( sqlite3Fts5HashIsEmpty(pHash)==0 ){
239962 iSegid = fts5AllocateSegid(p, pStruct);
239963 if( iSegid ){
239964 const int pgsz = p->pConfig->pgsz;
239965 int eDetail = p->pConfig->eDetail;
239966 int bSecureDelete = p->pConfig->bSecureDelete;
239967 Fts5StructureSegment *pSeg; /* New segment within pStruct */
239968 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
239969 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
239970
239971 Fts5SegWriter writer;
239972 fts5WriteInit(p, pWriter: &writer, iSegid);
239973
239974 pBuf = &writer.writer.buf;
239975 pPgidx = &writer.writer.pgidx;
239976
239977 /* fts5WriteInit() should have initialized the buffers to (most likely)
239978 ** the maximum space required. */
239979 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
239980 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
239981
239982 /* Begin scanning through hash table entries. This loop runs once for each
239983 ** term/doclist currently stored within the hash table. */
239984 if( p->rc==SQLITE_OK ){
239985 p->rc = sqlite3Fts5HashScanInit(p: pHash, pTerm: 0, nTerm: 0);
239986 }
239987 while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(p: pHash) ){
239988 const char *zTerm; /* Buffer containing term */
239989 int nTerm; /* Size of zTerm in bytes */
239990 const u8 *pDoclist; /* Pointer to doclist for this term */
239991 int nDoclist; /* Size of doclist in bytes */
239992
239993 /* Get the term and doclist for this entry. */
239994 sqlite3Fts5HashScanEntry(pHash, pzTerm: &zTerm, ppDoclist: &pDoclist, pnDoclist: &nDoclist);
239995 nTerm = (int)strlen(s: zTerm);
239996 if( bSecureDelete==0 ){
239997 fts5WriteAppendTerm(p, pWriter: &writer, nTerm, pTerm: (const u8*)zTerm);
239998 if( p->rc!=SQLITE_OK ) break;
239999 assert( writer.bFirstRowidInPage==0 );
240000 }
240001
240002 if( !bSecureDelete && pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
240003 /* The entire doclist will fit on the current leaf. */
240004 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
240005 }else{
240006 int bTermWritten = !bSecureDelete;
240007 i64 iRowid = 0;
240008 i64 iPrev = 0;
240009 int iOff = 0;
240010
240011 /* The entire doclist will not fit on this leaf. The following
240012 ** loop iterates through the poslists that make up the current
240013 ** doclist. */
240014 while( p->rc==SQLITE_OK && iOff<nDoclist ){
240015 u64 iDelta = 0;
240016 iOff += fts5GetVarint(&pDoclist[iOff], &iDelta);
240017 iRowid += iDelta;
240018
240019 /* If in secure delete mode, and if this entry in the poslist is
240020 ** in fact a delete, then edit the existing segments directly
240021 ** using fts5FlushSecureDelete(). */
240022 if( bSecureDelete ){
240023 if( eDetail==FTS5_DETAIL_NONE ){
240024 if( iOff<nDoclist && pDoclist[iOff]==0x00 ){
240025 fts5FlushSecureDelete(p, pStruct, zTerm, iRowid);
240026 iOff++;
240027 if( iOff<nDoclist && pDoclist[iOff]==0x00 ){
240028 iOff++;
240029 nDoclist = 0;
240030 }else{
240031 continue;
240032 }
240033 }
240034 }else if( (pDoclist[iOff] & 0x01) ){
240035 fts5FlushSecureDelete(p, pStruct, zTerm, iRowid);
240036 if( p->rc!=SQLITE_OK || pDoclist[iOff]==0x01 ){
240037 iOff++;
240038 continue;
240039 }
240040 }
240041 }
240042
240043 if( p->rc==SQLITE_OK && bTermWritten==0 ){
240044 fts5WriteAppendTerm(p, pWriter: &writer, nTerm, pTerm: (const u8*)zTerm);
240045 bTermWritten = 1;
240046 assert( p->rc!=SQLITE_OK || writer.bFirstRowidInPage==0 );
240047 }
240048
240049 if( writer.bFirstRowidInPage ){
240050 fts5PutU16(aOut: &pBuf->p[0], iVal: (u16)pBuf->n); /* first rowid on page */
240051 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: iRowid);
240052 writer.bFirstRowidInPage = 0;
240053 fts5WriteDlidxAppend(p, pWriter: &writer, iRowid);
240054 }else{
240055 u64 iRowidDelta = (u64)iRowid - (u64)iPrev;
240056 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: iRowidDelta);
240057 }
240058 if( p->rc!=SQLITE_OK ) break;
240059 assert( pBuf->n<=pBuf->nSpace );
240060 iPrev = iRowid;
240061
240062 if( eDetail==FTS5_DETAIL_NONE ){
240063 if( iOff<nDoclist && pDoclist[iOff]==0 ){
240064 pBuf->p[pBuf->n++] = 0;
240065 iOff++;
240066 if( iOff<nDoclist && pDoclist[iOff]==0 ){
240067 pBuf->p[pBuf->n++] = 0;
240068 iOff++;
240069 }
240070 }
240071 if( (pBuf->n + pPgidx->n)>=pgsz ){
240072 fts5WriteFlushLeaf(p, pWriter: &writer);
240073 }
240074 }else{
240075 int bDummy;
240076 int nPos;
240077 int nCopy = fts5GetPoslistSize(p: &pDoclist[iOff], pnSz: &nPos, pbDel: &bDummy);
240078 nCopy += nPos;
240079 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
240080 /* The entire poslist will fit on the current leaf. So copy
240081 ** it in one go. */
240082 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
240083 }else{
240084 /* The entire poslist will not fit on this leaf. So it needs
240085 ** to be broken into sections. The only qualification being
240086 ** that each varint must be stored contiguously. */
240087 const u8 *pPoslist = &pDoclist[iOff];
240088 int iPos = 0;
240089 while( p->rc==SQLITE_OK ){
240090 int nSpace = pgsz - pBuf->n - pPgidx->n;
240091 int n = 0;
240092 if( (nCopy - iPos)<=nSpace ){
240093 n = nCopy - iPos;
240094 }else{
240095 n = fts5PoslistPrefix(aBuf: &pPoslist[iPos], nMax: nSpace);
240096 }
240097 assert( n>0 );
240098 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
240099 iPos += n;
240100 if( (pBuf->n + pPgidx->n)>=pgsz ){
240101 fts5WriteFlushLeaf(p, pWriter: &writer);
240102 }
240103 if( iPos>=nCopy ) break;
240104 }
240105 }
240106 iOff += nCopy;
240107 }
240108 }
240109 }
240110
240111 /* TODO2: Doclist terminator written here. */
240112 /* pBuf->p[pBuf->n++] = '\0'; */
240113 assert( pBuf->n<=pBuf->nSpace );
240114 if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(p: pHash);
240115 }
240116 sqlite3Fts5HashClear(pHash);
240117 fts5WriteFinish(p, pWriter: &writer, pnLeaf: &pgnoLast);
240118
240119 assert( p->rc!=SQLITE_OK || bSecureDelete || pgnoLast>0 );
240120 if( pgnoLast>0 ){
240121 /* Update the Fts5Structure. It is written back to the database by the
240122 ** fts5StructureRelease() call below. */
240123 if( pStruct->nLevel==0 ){
240124 fts5StructureAddLevel(pRc: &p->rc, ppStruct: &pStruct);
240125 }
240126 fts5StructureExtendLevel(pRc: &p->rc, pStruct, iLvl: 0, nExtra: 1, bInsert: 0);
240127 if( p->rc==SQLITE_OK ){
240128 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
240129 pSeg->iSegid = iSegid;
240130 pSeg->pgnoFirst = 1;
240131 pSeg->pgnoLast = pgnoLast;
240132 if( pStruct->nOriginCntr>0 ){
240133 pSeg->iOrigin1 = pStruct->nOriginCntr;
240134 pSeg->iOrigin2 = pStruct->nOriginCntr;
240135 pSeg->nEntry = p->nPendingRow;
240136 pStruct->nOriginCntr++;
240137 }
240138 pStruct->nSegment++;
240139 }
240140 fts5StructurePromote(p, iLvl: 0, pStruct);
240141 }
240142 }
240143 }
240144
240145 fts5IndexAutomerge(p, ppStruct: &pStruct, nLeaf: pgnoLast + p->nContentlessDelete);
240146 fts5IndexCrisismerge(p, ppStruct: &pStruct);
240147 fts5StructureWrite(p, pStruct);
240148 fts5StructureRelease(pStruct);
240149 p->nContentlessDelete = 0;
240150}
240151
240152/*
240153** Flush any data stored in the in-memory hash tables to the database.
240154*/
240155static void fts5IndexFlush(Fts5Index *p){
240156 /* Unless it is empty, flush the hash table to disk */
240157 if( p->nPendingData || p->nContentlessDelete ){
240158 assert( p->pHash );
240159 fts5FlushOneHash(p);
240160 p->nPendingData = 0;
240161 p->nPendingRow = 0;
240162 }
240163}
240164
240165static Fts5Structure *fts5IndexOptimizeStruct(
240166 Fts5Index *p,
240167 Fts5Structure *pStruct
240168){
240169 Fts5Structure *pNew = 0;
240170 sqlite3_int64 nByte = sizeof(Fts5Structure);
240171 int nSeg = pStruct->nSegment;
240172 int i;
240173
240174 /* Figure out if this structure requires optimization. A structure does
240175 ** not require optimization if either:
240176 **
240177 ** 1. it consists of fewer than two segments, or
240178 ** 2. all segments are on the same level, or
240179 ** 3. all segments except one are currently inputs to a merge operation.
240180 **
240181 ** In the first case, if there are no tombstone hash pages, return NULL. In
240182 ** the second, increment the ref-count on *pStruct and return a copy of the
240183 ** pointer to it.
240184 */
240185 if( nSeg==0 ) return 0;
240186 for(i=0; i<pStruct->nLevel; i++){
240187 int nThis = pStruct->aLevel[i].nSeg;
240188 int nMerge = pStruct->aLevel[i].nMerge;
240189 if( nThis>0 && (nThis==nSeg || (nThis==nSeg-1 && nMerge==nThis)) ){
240190 if( nSeg==1 && nThis==1 && pStruct->aLevel[i].aSeg[0].nPgTombstone==0 ){
240191 return 0;
240192 }
240193 fts5StructureRef(pStruct);
240194 return pStruct;
240195 }
240196 assert( pStruct->aLevel[i].nMerge<=nThis );
240197 }
240198
240199 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
240200 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc: &p->rc, nByte);
240201
240202 if( pNew ){
240203 Fts5StructureLevel *pLvl;
240204 nByte = nSeg * sizeof(Fts5StructureSegment);
240205 pNew->nLevel = MIN(pStruct->nLevel+1, FTS5_MAX_LEVEL);
240206 pNew->nRef = 1;
240207 pNew->nWriteCounter = pStruct->nWriteCounter;
240208 pNew->nOriginCntr = pStruct->nOriginCntr;
240209 pLvl = &pNew->aLevel[pNew->nLevel-1];
240210 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc: &p->rc, nByte);
240211 if( pLvl->aSeg ){
240212 int iLvl, iSeg;
240213 int iSegOut = 0;
240214 /* Iterate through all segments, from oldest to newest. Add them to
240215 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
240216 ** segment in the data structure. */
240217 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
240218 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
240219 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
240220 iSegOut++;
240221 }
240222 }
240223 pNew->nSegment = pLvl->nSeg = nSeg;
240224 }else{
240225 sqlite3_free(p: pNew);
240226 pNew = 0;
240227 }
240228 }
240229
240230 return pNew;
240231}
240232
240233static int sqlite3Fts5IndexOptimize(Fts5Index *p){
240234 Fts5Structure *pStruct;
240235 Fts5Structure *pNew = 0;
240236
240237 assert( p->rc==SQLITE_OK );
240238 fts5IndexFlush(p);
240239 assert( p->nContentlessDelete==0 );
240240 pStruct = fts5StructureRead(p);
240241 fts5StructureInvalidate(p);
240242
240243 if( pStruct ){
240244 pNew = fts5IndexOptimizeStruct(p, pStruct);
240245 }
240246 fts5StructureRelease(pStruct);
240247
240248 assert( pNew==0 || pNew->nSegment>0 );
240249 if( pNew ){
240250 int iLvl;
240251 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
240252 while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
240253 int nRem = FTS5_OPT_WORK_UNIT;
240254 fts5IndexMergeLevel(p, ppStruct: &pNew, iLvl, pnRem: &nRem);
240255 }
240256
240257 fts5StructureWrite(p, pStruct: pNew);
240258 fts5StructureRelease(pStruct: pNew);
240259 }
240260
240261 return fts5IndexReturn(p);
240262}
240263
240264/*
240265** This is called to implement the special "VALUES('merge', $nMerge)"
240266** INSERT command.
240267*/
240268static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
240269 Fts5Structure *pStruct = 0;
240270
240271 fts5IndexFlush(p);
240272 pStruct = fts5StructureRead(p);
240273 if( pStruct ){
240274 int nMin = p->pConfig->nUsermerge;
240275 fts5StructureInvalidate(p);
240276 if( nMerge<0 ){
240277 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
240278 fts5StructureRelease(pStruct);
240279 pStruct = pNew;
240280 nMin = 1;
240281 nMerge = nMerge*-1;
240282 }
240283 if( pStruct && pStruct->nLevel ){
240284 if( fts5IndexMerge(p, ppStruct: &pStruct, nPg: nMerge, nMin) ){
240285 fts5StructureWrite(p, pStruct);
240286 }
240287 }
240288 fts5StructureRelease(pStruct);
240289 }
240290 return fts5IndexReturn(p);
240291}
240292
240293static void fts5AppendRowid(
240294 Fts5Index *p,
240295 u64 iDelta,
240296 Fts5Iter *pUnused,
240297 Fts5Buffer *pBuf
240298){
240299 UNUSED_PARAM(pUnused);
240300 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
240301}
240302
240303static void fts5AppendPoslist(
240304 Fts5Index *p,
240305 u64 iDelta,
240306 Fts5Iter *pMulti,
240307 Fts5Buffer *pBuf
240308){
240309 int nData = pMulti->base.nData;
240310 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
240311 assert( nData>0 );
240312 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
240313 fts5BufferSafeAppendVarint(pBuf, iDelta);
240314 fts5BufferSafeAppendVarint(pBuf, nData*2);
240315 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
240316 memset(s: &pBuf->p[pBuf->n], c: 0, FTS5_DATA_ZERO_PADDING);
240317 }
240318}
240319
240320
240321static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
240322 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
240323
240324 assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) );
240325 if( p>=pIter->aEof ){
240326 pIter->aPoslist = 0;
240327 }else{
240328 i64 iDelta;
240329
240330 p += fts5GetVarint(p, (u64*)&iDelta);
240331 pIter->iRowid += iDelta;
240332
240333 /* Read position list size */
240334 if( p[0] & 0x80 ){
240335 int nPos;
240336 pIter->nSize = fts5GetVarint32(p, nPos);
240337 pIter->nPoslist = (nPos>>1);
240338 }else{
240339 pIter->nPoslist = ((int)(p[0])) >> 1;
240340 pIter->nSize = 1;
240341 }
240342
240343 pIter->aPoslist = p;
240344 if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){
240345 pIter->aPoslist = 0;
240346 }
240347 }
240348}
240349
240350static void fts5DoclistIterInit(
240351 Fts5Buffer *pBuf,
240352 Fts5DoclistIter *pIter
240353){
240354 memset(s: pIter, c: 0, n: sizeof(*pIter));
240355 if( pBuf->n>0 ){
240356 pIter->aPoslist = pBuf->p;
240357 pIter->aEof = &pBuf->p[pBuf->n];
240358 fts5DoclistIterNext(pIter);
240359 }
240360}
240361
240362#if 0
240363/*
240364** Append a doclist to buffer pBuf.
240365**
240366** This function assumes that space within the buffer has already been
240367** allocated.
240368*/
240369static void fts5MergeAppendDocid(
240370 Fts5Buffer *pBuf, /* Buffer to write to */
240371 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
240372 i64 iRowid /* Rowid to append */
240373){
240374 assert( pBuf->n!=0 || (*piLastRowid)==0 );
240375 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
240376 *piLastRowid = iRowid;
240377}
240378#endif
240379
240380#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
240381 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
240382 fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)); \
240383 (iLastRowid) = (iRowid); \
240384}
240385
240386/*
240387** Swap the contents of buffer *p1 with that of *p2.
240388*/
240389static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
240390 Fts5Buffer tmp = *p1;
240391 *p1 = *p2;
240392 *p2 = tmp;
240393}
240394
240395static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
240396 int i = *piOff;
240397 if( i>=pBuf->n ){
240398 *piOff = -1;
240399 }else{
240400 u64 iVal;
240401 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
240402 *piRowid += iVal;
240403 }
240404}
240405
240406/*
240407** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
240408** In this case the buffers consist of a delta-encoded list of rowids only.
240409*/
240410static void fts5MergeRowidLists(
240411 Fts5Index *p, /* FTS5 backend object */
240412 Fts5Buffer *p1, /* First list to merge */
240413 int nBuf, /* Number of entries in apBuf[] */
240414 Fts5Buffer *aBuf /* Array of other lists to merge into p1 */
240415){
240416 int i1 = 0;
240417 int i2 = 0;
240418 i64 iRowid1 = 0;
240419 i64 iRowid2 = 0;
240420 i64 iOut = 0;
240421 Fts5Buffer *p2 = &aBuf[0];
240422 Fts5Buffer out;
240423
240424 (void)nBuf;
240425 memset(s: &out, c: 0, n: sizeof(out));
240426 assert( nBuf==1 );
240427 sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &out, nByte: p1->n + p2->n);
240428 if( p->rc ) return;
240429
240430 fts5NextRowid(pBuf: p1, piOff: &i1, piRowid: &iRowid1);
240431 fts5NextRowid(pBuf: p2, piOff: &i2, piRowid: &iRowid2);
240432 while( i1>=0 || i2>=0 ){
240433 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
240434 assert( iOut==0 || iRowid1>iOut );
240435 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
240436 iOut = iRowid1;
240437 fts5NextRowid(pBuf: p1, piOff: &i1, piRowid: &iRowid1);
240438 }else{
240439 assert( iOut==0 || iRowid2>iOut );
240440 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
240441 iOut = iRowid2;
240442 if( i1>=0 && iRowid1==iRowid2 ){
240443 fts5NextRowid(pBuf: p1, piOff: &i1, piRowid: &iRowid1);
240444 }
240445 fts5NextRowid(pBuf: p2, piOff: &i2, piRowid: &iRowid2);
240446 }
240447 }
240448
240449 fts5BufferSwap(p1: &out, p2: p1);
240450 fts5BufferFree(&out);
240451}
240452
240453typedef struct PrefixMerger PrefixMerger;
240454struct PrefixMerger {
240455 Fts5DoclistIter iter; /* Doclist iterator */
240456 i64 iPos; /* For iterating through a position list */
240457 int iOff;
240458 u8 *aPos;
240459 PrefixMerger *pNext; /* Next in docid/poslist order */
240460};
240461
240462static void fts5PrefixMergerInsertByRowid(
240463 PrefixMerger **ppHead,
240464 PrefixMerger *p
240465){
240466 if( p->iter.aPoslist ){
240467 PrefixMerger **pp = ppHead;
240468 while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){
240469 pp = &(*pp)->pNext;
240470 }
240471 p->pNext = *pp;
240472 *pp = p;
240473 }
240474}
240475
240476static void fts5PrefixMergerInsertByPosition(
240477 PrefixMerger **ppHead,
240478 PrefixMerger *p
240479){
240480 if( p->iPos>=0 ){
240481 PrefixMerger **pp = ppHead;
240482 while( *pp && p->iPos>(*pp)->iPos ){
240483 pp = &(*pp)->pNext;
240484 }
240485 p->pNext = *pp;
240486 *pp = p;
240487 }
240488}
240489
240490
240491/*
240492** Array aBuf[] contains nBuf doclists. These are all merged in with the
240493** doclist in buffer p1.
240494*/
240495static void fts5MergePrefixLists(
240496 Fts5Index *p, /* FTS5 backend object */
240497 Fts5Buffer *p1, /* First list to merge */
240498 int nBuf, /* Number of buffers in array aBuf[] */
240499 Fts5Buffer *aBuf /* Other lists to merge in */
240500){
240501#define fts5PrefixMergerNextPosition(p) \
240502 sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos)
240503#define FTS5_MERGE_NLIST 16
240504 PrefixMerger aMerger[FTS5_MERGE_NLIST];
240505 PrefixMerger *pHead = 0;
240506 int i;
240507 int nOut = 0;
240508 Fts5Buffer out = {0, 0, 0};
240509 Fts5Buffer tmp = {0, 0, 0};
240510 i64 iLastRowid = 0;
240511
240512 /* Initialize a doclist-iterator for each input buffer. Arrange them in
240513 ** a linked-list starting at pHead in ascending order of rowid. Avoid
240514 ** linking any iterators already at EOF into the linked list at all. */
240515 assert( nBuf+1<=(int)(sizeof(aMerger)/sizeof(aMerger[0])) );
240516 memset(s: aMerger, c: 0, n: sizeof(PrefixMerger)*(nBuf+1));
240517 pHead = &aMerger[nBuf];
240518 fts5DoclistIterInit(pBuf: p1, pIter: &pHead->iter);
240519 for(i=0; i<nBuf; i++){
240520 fts5DoclistIterInit(pBuf: &aBuf[i], pIter: &aMerger[i].iter);
240521 fts5PrefixMergerInsertByRowid(ppHead: &pHead, p: &aMerger[i]);
240522 nOut += aBuf[i].n;
240523 }
240524 if( nOut==0 ) return;
240525 nOut += p1->n + 9 + 10*nBuf;
240526
240527 /* The maximum size of the output is equal to the sum of the
240528 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
240529 ** first rowid in one input is a large negative number, and the first in
240530 ** the other a non-negative number, the delta for the non-negative
240531 ** number will be larger on disk than the literal integer value
240532 ** was.
240533 **
240534 ** Or, if the input position-lists are corrupt, then the output might
240535 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
240536 ** (the value PoslistNext64() uses for EOF) as a position and appending
240537 ** it to the output. This can happen at most once for each input
240538 ** position-list, hence (nBuf+1) 10 byte paddings. */
240539 if( sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &out, nByte: nOut) ) return;
240540
240541 while( pHead ){
240542 fts5MergeAppendDocid(&out, iLastRowid, pHead->iter.iRowid);
240543
240544 if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){
240545 /* Merge data from two or more poslists */
240546 i64 iPrev = 0;
240547 int nTmp = FTS5_DATA_ZERO_PADDING;
240548 int nMerge = 0;
240549 PrefixMerger *pSave = pHead;
240550 PrefixMerger *pThis = 0;
240551 int nTail = 0;
240552
240553 pHead = 0;
240554 while( pSave && pSave->iter.iRowid==iLastRowid ){
240555 PrefixMerger *pNext = pSave->pNext;
240556 pSave->iOff = 0;
240557 pSave->iPos = 0;
240558 pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize];
240559 fts5PrefixMergerNextPosition(pSave);
240560 nTmp += pSave->iter.nPoslist + 10;
240561 nMerge++;
240562 fts5PrefixMergerInsertByPosition(ppHead: &pHead, p: pSave);
240563 pSave = pNext;
240564 }
240565
240566 if( pHead==0 || pHead->pNext==0 ){
240567 p->rc = FTS5_CORRUPT;
240568 break;
240569 }
240570
240571 /* See the earlier comment in this function for an explanation of why
240572 ** corrupt input position lists might cause the output to consume
240573 ** at most nMerge*10 bytes of unexpected space. */
240574 if( sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &tmp, nByte: nTmp+nMerge*10) ){
240575 break;
240576 }
240577 fts5BufferZero(&tmp);
240578
240579 pThis = pHead;
240580 pHead = pThis->pNext;
240581 sqlite3Fts5PoslistSafeAppend(pBuf: &tmp, piPrev: &iPrev, iPos: pThis->iPos);
240582 fts5PrefixMergerNextPosition(pThis);
240583 fts5PrefixMergerInsertByPosition(ppHead: &pHead, p: pThis);
240584
240585 while( pHead->pNext ){
240586 pThis = pHead;
240587 if( pThis->iPos!=iPrev ){
240588 sqlite3Fts5PoslistSafeAppend(pBuf: &tmp, piPrev: &iPrev, iPos: pThis->iPos);
240589 }
240590 fts5PrefixMergerNextPosition(pThis);
240591 pHead = pThis->pNext;
240592 fts5PrefixMergerInsertByPosition(ppHead: &pHead, p: pThis);
240593 }
240594
240595 if( pHead->iPos!=iPrev ){
240596 sqlite3Fts5PoslistSafeAppend(pBuf: &tmp, piPrev: &iPrev, iPos: pHead->iPos);
240597 }
240598 nTail = pHead->iter.nPoslist - pHead->iOff;
240599
240600 /* WRITEPOSLISTSIZE */
240601 assert_nc( tmp.n+nTail<=nTmp );
240602 assert( tmp.n+nTail<=nTmp+nMerge*10 );
240603 if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING ){
240604 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
240605 break;
240606 }
240607 fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2);
240608 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
240609 if( nTail>0 ){
240610 fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail);
240611 }
240612
240613 pHead = pSave;
240614 for(i=0; i<nBuf+1; i++){
240615 PrefixMerger *pX = &aMerger[i];
240616 if( pX->iter.aPoslist && pX->iter.iRowid==iLastRowid ){
240617 fts5DoclistIterNext(pIter: &pX->iter);
240618 fts5PrefixMergerInsertByRowid(ppHead: &pHead, p: pX);
240619 }
240620 }
240621
240622 }else{
240623 /* Copy poslist from pHead to output */
240624 PrefixMerger *pThis = pHead;
240625 Fts5DoclistIter *pI = &pThis->iter;
240626 fts5BufferSafeAppendBlob(&out, pI->aPoslist, pI->nPoslist+pI->nSize);
240627 fts5DoclistIterNext(pIter: pI);
240628 pHead = pThis->pNext;
240629 fts5PrefixMergerInsertByRowid(ppHead: &pHead, p: pThis);
240630 }
240631 }
240632
240633 fts5BufferFree(p1);
240634 fts5BufferFree(&tmp);
240635 memset(s: &out.p[out.n], c: 0, FTS5_DATA_ZERO_PADDING);
240636 *p1 = out;
240637}
240638
240639static void fts5SetupPrefixIter(
240640 Fts5Index *p, /* Index to read from */
240641 int bDesc, /* True for "ORDER BY rowid DESC" */
240642 int iIdx, /* Index to scan for data */
240643 u8 *pToken, /* Buffer containing prefix to match */
240644 int nToken, /* Size of buffer pToken in bytes */
240645 Fts5Colset *pColset, /* Restrict matches to these columns */
240646 Fts5Iter **ppIter /* OUT: New iterator */
240647){
240648 Fts5Structure *pStruct;
240649 Fts5Buffer *aBuf;
240650 int nBuf = 32;
240651 int nMerge = 1;
240652
240653 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
240654 void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
240655 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
240656 xMerge = fts5MergeRowidLists;
240657 xAppend = fts5AppendRowid;
240658 }else{
240659 nMerge = FTS5_MERGE_NLIST-1;
240660 nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
240661 xMerge = fts5MergePrefixLists;
240662 xAppend = fts5AppendPoslist;
240663 }
240664
240665 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, nByte: sizeof(Fts5Buffer)*nBuf);
240666 pStruct = fts5StructureRead(p);
240667
240668 if( aBuf && pStruct ){
240669 const int flags = FTS5INDEX_QUERY_SCAN
240670 | FTS5INDEX_QUERY_SKIPEMPTY
240671 | FTS5INDEX_QUERY_NOOUTPUT;
240672 int i;
240673 i64 iLastRowid = 0;
240674 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
240675 Fts5Data *pData;
240676 Fts5Buffer doclist;
240677 int bNewTerm = 1;
240678
240679 memset(s: &doclist, c: 0, n: sizeof(doclist));
240680 if( iIdx!=0 ){
240681 int dummy = 0;
240682 const int f2 = FTS5INDEX_QUERY_SKIPEMPTY|FTS5INDEX_QUERY_NOOUTPUT;
240683 pToken[0] = FTS5_MAIN_PREFIX;
240684 fts5MultiIterNew(p, pStruct, flags: f2, pColset, pTerm: pToken, nTerm: nToken, iLevel: -1, nSegment: 0, ppOut: &p1);
240685 fts5IterSetOutputCb(pRc: &p->rc, pIter: p1);
240686 for(;
240687 fts5MultiIterEof(p, pIter: p1)==0;
240688 fts5MultiIterNext2(p, pIter: p1, pbNewTerm: &dummy)
240689 ){
240690 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
240691 p1->xSetOutputs(p1, pSeg);
240692 if( p1->base.nData ){
240693 xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
240694 iLastRowid = p1->base.iRowid;
240695 }
240696 }
240697 fts5MultiIterFree(pIter: p1);
240698 }
240699
240700 pToken[0] = FTS5_MAIN_PREFIX + iIdx;
240701 fts5MultiIterNew(p, pStruct, flags, pColset, pTerm: pToken, nTerm: nToken, iLevel: -1, nSegment: 0, ppOut: &p1);
240702 fts5IterSetOutputCb(pRc: &p->rc, pIter: p1);
240703 for( /* no-op */ ;
240704 fts5MultiIterEof(p, pIter: p1)==0;
240705 fts5MultiIterNext2(p, pIter: p1, pbNewTerm: &bNewTerm)
240706 ){
240707 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
240708 int nTerm = pSeg->term.n;
240709 const u8 *pTerm = pSeg->term.p;
240710 p1->xSetOutputs(p1, pSeg);
240711
240712 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
240713 if( bNewTerm ){
240714 if( nTerm<nToken || memcmp(s1: pToken, s2: pTerm, n: nToken) ) break;
240715 }
240716
240717 if( p1->base.nData==0 ) continue;
240718
240719 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
240720 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
240721 int i1 = i*nMerge;
240722 int iStore;
240723 assert( i1+nMerge<=nBuf );
240724 for(iStore=i1; iStore<i1+nMerge; iStore++){
240725 if( aBuf[iStore].n==0 ){
240726 fts5BufferSwap(p1: &doclist, p2: &aBuf[iStore]);
240727 fts5BufferZero(&doclist);
240728 break;
240729 }
240730 }
240731 if( iStore==i1+nMerge ){
240732 xMerge(p, &doclist, nMerge, &aBuf[i1]);
240733 for(iStore=i1; iStore<i1+nMerge; iStore++){
240734 fts5BufferZero(&aBuf[iStore]);
240735 }
240736 }
240737 }
240738 iLastRowid = 0;
240739 }
240740
240741 xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
240742 iLastRowid = p1->base.iRowid;
240743 }
240744
240745 assert( (nBuf%nMerge)==0 );
240746 for(i=0; i<nBuf; i+=nMerge){
240747 int iFree;
240748 if( p->rc==SQLITE_OK ){
240749 xMerge(p, &doclist, nMerge, &aBuf[i]);
240750 }
240751 for(iFree=i; iFree<i+nMerge; iFree++){
240752 fts5BufferFree(&aBuf[iFree]);
240753 }
240754 }
240755 fts5MultiIterFree(pIter: p1);
240756
240757 pData = fts5IdxMalloc(p, nByte: sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING);
240758 if( pData ){
240759 pData->p = (u8*)&pData[1];
240760 pData->nn = pData->szLeaf = doclist.n;
240761 if( doclist.n ) memcpy(dest: pData->p, src: doclist.p, n: doclist.n);
240762 fts5MultiIterNew2(p, pData, bDesc, ppOut: ppIter);
240763 }
240764 fts5BufferFree(&doclist);
240765 }
240766
240767 fts5StructureRelease(pStruct);
240768 sqlite3_free(p: aBuf);
240769}
240770
240771
240772/*
240773** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
240774** to the document with rowid iRowid.
240775*/
240776static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
240777 assert( p->rc==SQLITE_OK );
240778
240779 /* Allocate the hash table if it has not already been allocated */
240780 if( p->pHash==0 ){
240781 p->rc = sqlite3Fts5HashNew(pConfig: p->pConfig, ppNew: &p->pHash, pnByte: &p->nPendingData);
240782 }
240783
240784 /* Flush the hash table to disk if required */
240785 if( iRowid<p->iWriteRowid
240786 || (iRowid==p->iWriteRowid && p->bDelete==0)
240787 || (p->nPendingData > p->pConfig->nHashSize)
240788 ){
240789 fts5IndexFlush(p);
240790 }
240791
240792 p->iWriteRowid = iRowid;
240793 p->bDelete = bDelete;
240794 if( bDelete==0 ){
240795 p->nPendingRow++;
240796 }
240797 return fts5IndexReturn(p);
240798}
240799
240800/*
240801** Commit data to disk.
240802*/
240803static int sqlite3Fts5IndexSync(Fts5Index *p){
240804 assert( p->rc==SQLITE_OK );
240805 fts5IndexFlush(p);
240806 sqlite3Fts5IndexCloseReader(p);
240807 return fts5IndexReturn(p);
240808}
240809
240810/*
240811** Discard any data stored in the in-memory hash tables. Do not write it
240812** to the database. Additionally, assume that the contents of the %_data
240813** table may have changed on disk. So any in-memory caches of %_data
240814** records must be invalidated.
240815*/
240816static int sqlite3Fts5IndexRollback(Fts5Index *p){
240817 sqlite3Fts5IndexCloseReader(p);
240818 fts5IndexDiscardData(p);
240819 fts5StructureInvalidate(p);
240820 /* assert( p->rc==SQLITE_OK ); */
240821 return SQLITE_OK;
240822}
240823
240824/*
240825** The %_data table is completely empty when this function is called. This
240826** function populates it with the initial structure objects for each index,
240827** and the initial version of the "averages" record (a zero-byte blob).
240828*/
240829static int sqlite3Fts5IndexReinit(Fts5Index *p){
240830 Fts5Structure s;
240831 fts5StructureInvalidate(p);
240832 fts5IndexDiscardData(p);
240833 memset(s: &s, c: 0, n: sizeof(Fts5Structure));
240834 if( p->pConfig->bContentlessDelete ){
240835 s.nOriginCntr = 1;
240836 }
240837 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData: (const u8*)"", nData: 0);
240838 fts5StructureWrite(p, pStruct: &s);
240839 return fts5IndexReturn(p);
240840}
240841
240842/*
240843** Open a new Fts5Index handle. If the bCreate argument is true, create
240844** and initialize the underlying %_data table.
240845**
240846** If successful, set *pp to point to the new object and return SQLITE_OK.
240847** Otherwise, set *pp to NULL and return an SQLite error code.
240848*/
240849static int sqlite3Fts5IndexOpen(
240850 Fts5Config *pConfig,
240851 int bCreate,
240852 Fts5Index **pp,
240853 char **pzErr
240854){
240855 int rc = SQLITE_OK;
240856 Fts5Index *p; /* New object */
240857
240858 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Index));
240859 if( rc==SQLITE_OK ){
240860 p->pConfig = pConfig;
240861 p->nWorkUnit = FTS5_WORK_UNIT;
240862 p->zDataTbl = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%s_data", pConfig->zName);
240863 if( p->zDataTbl && bCreate ){
240864 rc = sqlite3Fts5CreateTable(
240865 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
240866 );
240867 if( rc==SQLITE_OK ){
240868 rc = sqlite3Fts5CreateTable(pConfig, "idx",
240869 "segid, term, pgno, PRIMARY KEY(segid, term)",
240870 1, pzErr
240871 );
240872 }
240873 if( rc==SQLITE_OK ){
240874 rc = sqlite3Fts5IndexReinit(p);
240875 }
240876 }
240877 }
240878
240879 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
240880 if( rc ){
240881 sqlite3Fts5IndexClose(p);
240882 *pp = 0;
240883 }
240884 return rc;
240885}
240886
240887/*
240888** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
240889*/
240890static int sqlite3Fts5IndexClose(Fts5Index *p){
240891 int rc = SQLITE_OK;
240892 if( p ){
240893 assert( p->pReader==0 );
240894 fts5StructureInvalidate(p);
240895 sqlite3_finalize(pStmt: p->pWriter);
240896 sqlite3_finalize(pStmt: p->pDeleter);
240897 sqlite3_finalize(pStmt: p->pIdxWriter);
240898 sqlite3_finalize(pStmt: p->pIdxDeleter);
240899 sqlite3_finalize(pStmt: p->pIdxSelect);
240900 sqlite3_finalize(pStmt: p->pDataVersion);
240901 sqlite3_finalize(pStmt: p->pDeleteFromIdx);
240902 sqlite3Fts5HashFree(pHash: p->pHash);
240903 sqlite3_free(p: p->zDataTbl);
240904 sqlite3_free(p);
240905 }
240906 return rc;
240907}
240908
240909/*
240910** Argument p points to a buffer containing utf-8 text that is n bytes in
240911** size. Return the number of bytes in the nChar character prefix of the
240912** buffer, or 0 if there are less than nChar characters in total.
240913*/
240914static int sqlite3Fts5IndexCharlenToBytelen(
240915 const char *p,
240916 int nByte,
240917 int nChar
240918){
240919 int n = 0;
240920 int i;
240921 for(i=0; i<nChar; i++){
240922 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
240923 if( (unsigned char)p[n++]>=0xc0 ){
240924 if( n>=nByte ) return 0;
240925 while( (p[n] & 0xc0)==0x80 ){
240926 n++;
240927 if( n>=nByte ){
240928 if( i+1==nChar ) break;
240929 return 0;
240930 }
240931 }
240932 }
240933 }
240934 return n;
240935}
240936
240937/*
240938** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
240939** unicode characters in the string.
240940*/
240941static int fts5IndexCharlen(const char *pIn, int nIn){
240942 int nChar = 0;
240943 int i = 0;
240944 while( i<nIn ){
240945 if( (unsigned char)pIn[i++]>=0xc0 ){
240946 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
240947 }
240948 nChar++;
240949 }
240950 return nChar;
240951}
240952
240953/*
240954** Insert or remove data to or from the index. Each time a document is
240955** added to or removed from the index, this function is called one or more
240956** times.
240957**
240958** For an insert, it must be called once for each token in the new document.
240959** If the operation is a delete, it must be called (at least) once for each
240960** unique token in the document with an iCol value less than zero. The iPos
240961** argument is ignored for a delete.
240962*/
240963static int sqlite3Fts5IndexWrite(
240964 Fts5Index *p, /* Index to write to */
240965 int iCol, /* Column token appears in (-ve -> delete) */
240966 int iPos, /* Position of token within column */
240967 const char *pToken, int nToken /* Token to add or remove to or from index */
240968){
240969 int i; /* Used to iterate through indexes */
240970 int rc = SQLITE_OK; /* Return code */
240971 Fts5Config *pConfig = p->pConfig;
240972
240973 assert( p->rc==SQLITE_OK );
240974 assert( (iCol<0)==p->bDelete );
240975
240976 /* Add the entry to the main terms index. */
240977 rc = sqlite3Fts5HashWrite(
240978 pHash: p->pHash, iRowid: p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
240979 );
240980
240981 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
240982 const int nChar = pConfig->aPrefix[i];
240983 int nByte = sqlite3Fts5IndexCharlenToBytelen(p: pToken, nByte: nToken, nChar);
240984 if( nByte ){
240985 rc = sqlite3Fts5HashWrite(pHash: p->pHash,
240986 iRowid: p->iWriteRowid, iCol, iPos, bByte: (char)(FTS5_MAIN_PREFIX+i+1), pToken,
240987 nToken: nByte
240988 );
240989 }
240990 }
240991
240992 return rc;
240993}
240994
240995/*
240996** Open a new iterator to iterate though all rowid that match the
240997** specified token or token prefix.
240998*/
240999static int sqlite3Fts5IndexQuery(
241000 Fts5Index *p, /* FTS index to query */
241001 const char *pToken, int nToken, /* Token (or prefix) to query for */
241002 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
241003 Fts5Colset *pColset, /* Match these columns only */
241004 Fts5IndexIter **ppIter /* OUT: New iterator object */
241005){
241006 Fts5Config *pConfig = p->pConfig;
241007 Fts5Iter *pRet = 0;
241008 Fts5Buffer buf = {0, 0, 0};
241009
241010 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
241011 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
241012
241013 if( sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &buf, nByte: nToken+1)==0 ){
241014 int iIdx = 0; /* Index to search */
241015 int iPrefixIdx = 0; /* +1 prefix index */
241016 if( nToken>0 ) memcpy(dest: &buf.p[1], src: pToken, n: nToken);
241017
241018 /* Figure out which index to search and set iIdx accordingly. If this
241019 ** is a prefix query for which there is no prefix index, set iIdx to
241020 ** greater than pConfig->nPrefix to indicate that the query will be
241021 ** satisfied by scanning multiple terms in the main index.
241022 **
241023 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
241024 ** prefix-query. Instead of using a prefix-index (if one exists),
241025 ** evaluate the prefix query using the main FTS index. This is used
241026 ** for internal sanity checking by the integrity-check in debug
241027 ** mode only. */
241028#ifdef SQLITE_DEBUG
241029 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
241030 assert( flags & FTS5INDEX_QUERY_PREFIX );
241031 iIdx = 1+pConfig->nPrefix;
241032 }else
241033#endif
241034 if( flags & FTS5INDEX_QUERY_PREFIX ){
241035 int nChar = fts5IndexCharlen(pIn: pToken, nIn: nToken);
241036 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
241037 int nIdxChar = pConfig->aPrefix[iIdx-1];
241038 if( nIdxChar==nChar ) break;
241039 if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
241040 }
241041 }
241042
241043 if( iIdx<=pConfig->nPrefix ){
241044 /* Straight index lookup */
241045 Fts5Structure *pStruct = fts5StructureRead(p);
241046 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
241047 if( pStruct ){
241048 fts5MultiIterNew(p, pStruct, flags: flags | FTS5INDEX_QUERY_SKIPEMPTY,
241049 pColset, pTerm: buf.p, nTerm: nToken+1, iLevel: -1, nSegment: 0, ppOut: &pRet
241050 );
241051 fts5StructureRelease(pStruct);
241052 }
241053 }else{
241054 /* Scan multiple terms in the main index */
241055 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
241056 fts5SetupPrefixIter(p, bDesc, iIdx: iPrefixIdx, pToken: buf.p, nToken: nToken+1, pColset,ppIter: &pRet);
241057 if( pRet==0 ){
241058 assert( p->rc!=SQLITE_OK );
241059 }else{
241060 assert( pRet->pColset==0 );
241061 fts5IterSetOutputCb(pRc: &p->rc, pIter: pRet);
241062 if( p->rc==SQLITE_OK ){
241063 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
241064 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
241065 }
241066 }
241067 }
241068
241069 if( p->rc ){
241070 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
241071 pRet = 0;
241072 sqlite3Fts5IndexCloseReader(p);
241073 }
241074
241075 *ppIter = (Fts5IndexIter*)pRet;
241076 sqlite3Fts5BufferFree(pBuf: &buf);
241077 }
241078 return fts5IndexReturn(p);
241079}
241080
241081/*
241082** Return true if the iterator passed as the only argument is at EOF.
241083*/
241084/*
241085** Move to the next matching rowid.
241086*/
241087static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
241088 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
241089 assert( pIter->pIndex->rc==SQLITE_OK );
241090 fts5MultiIterNext(p: pIter->pIndex, pIter, bFrom: 0, iFrom: 0);
241091 return fts5IndexReturn(p: pIter->pIndex);
241092}
241093
241094/*
241095** Move to the next matching term/rowid. Used by the fts5vocab module.
241096*/
241097static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
241098 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
241099 Fts5Index *p = pIter->pIndex;
241100
241101 assert( pIter->pIndex->rc==SQLITE_OK );
241102
241103 fts5MultiIterNext(p, pIter, bFrom: 0, iFrom: 0);
241104 if( p->rc==SQLITE_OK ){
241105 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
241106 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
241107 fts5DataRelease(pData: pSeg->pLeaf);
241108 pSeg->pLeaf = 0;
241109 pIter->base.bEof = 1;
241110 }
241111 }
241112
241113 return fts5IndexReturn(p: pIter->pIndex);
241114}
241115
241116/*
241117** Move to the next matching rowid that occurs at or after iMatch. The
241118** definition of "at or after" depends on whether this iterator iterates
241119** in ascending or descending rowid order.
241120*/
241121static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
241122 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
241123 fts5MultiIterNextFrom(p: pIter->pIndex, pIter, iMatch);
241124 return fts5IndexReturn(p: pIter->pIndex);
241125}
241126
241127/*
241128** Return the current term.
241129*/
241130static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
241131 int n;
241132 const char *z = (const char*)fts5MultiIterTerm(pIter: (Fts5Iter*)pIndexIter, pn: &n);
241133 assert_nc( z || n<=1 );
241134 *pn = n-1;
241135 return (z ? &z[1] : 0);
241136}
241137
241138/*
241139** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
241140*/
241141static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
241142 if( pIndexIter ){
241143 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
241144 Fts5Index *pIndex = pIter->pIndex;
241145 fts5MultiIterFree(pIter);
241146 sqlite3Fts5IndexCloseReader(p: pIndex);
241147 }
241148}
241149
241150/*
241151** Read and decode the "averages" record from the database.
241152**
241153** Parameter anSize must point to an array of size nCol, where nCol is
241154** the number of user defined columns in the FTS table.
241155*/
241156static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
241157 int nCol = p->pConfig->nCol;
241158 Fts5Data *pData;
241159
241160 *pnRow = 0;
241161 memset(s: anSize, c: 0, n: sizeof(i64) * nCol);
241162 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
241163 if( p->rc==SQLITE_OK && pData->nn ){
241164 int i = 0;
241165 int iCol;
241166 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
241167 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
241168 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
241169 }
241170 }
241171
241172 fts5DataRelease(pData);
241173 return fts5IndexReturn(p);
241174}
241175
241176/*
241177** Replace the current "averages" record with the contents of the buffer
241178** supplied as the second argument.
241179*/
241180static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
241181 assert( p->rc==SQLITE_OK );
241182 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
241183 return fts5IndexReturn(p);
241184}
241185
241186/*
241187** Return the total number of blocks this module has read from the %_data
241188** table since it was created.
241189*/
241190static int sqlite3Fts5IndexReads(Fts5Index *p){
241191 return p->nRead;
241192}
241193
241194/*
241195** Set the 32-bit cookie value stored at the start of all structure
241196** records to the value passed as the second argument.
241197**
241198** Return SQLITE_OK if successful, or an SQLite error code if an error
241199** occurs.
241200*/
241201static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
241202 int rc; /* Return code */
241203 Fts5Config *pConfig = p->pConfig; /* Configuration object */
241204 u8 aCookie[4]; /* Binary representation of iNew */
241205 sqlite3_blob *pBlob = 0;
241206
241207 assert( p->rc==SQLITE_OK );
241208 sqlite3Fts5Put32(aBuf: aCookie, iVal: iNew);
241209
241210 rc = sqlite3_blob_open(db: pConfig->db, zDb: pConfig->zDb, zTable: p->zDataTbl,
241211 zColumn: "block", FTS5_STRUCTURE_ROWID, wrFlag: 1, ppBlob: &pBlob
241212 );
241213 if( rc==SQLITE_OK ){
241214 sqlite3_blob_write(pBlob, z: aCookie, n: 4, iOffset: 0);
241215 rc = sqlite3_blob_close(pBlob);
241216 }
241217
241218 return rc;
241219}
241220
241221static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
241222 Fts5Structure *pStruct;
241223 pStruct = fts5StructureRead(p);
241224 fts5StructureRelease(pStruct);
241225 return fts5IndexReturn(p);
241226}
241227
241228/*
241229** Retrieve the origin value that will be used for the segment currently
241230** being accumulated in the in-memory hash table when it is flushed to
241231** disk. If successful, SQLITE_OK is returned and (*piOrigin) set to
241232** the queried value. Or, if an error occurs, an error code is returned
241233** and the final value of (*piOrigin) is undefined.
241234*/
241235static int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin){
241236 Fts5Structure *pStruct;
241237 pStruct = fts5StructureRead(p);
241238 if( pStruct ){
241239 *piOrigin = pStruct->nOriginCntr;
241240 fts5StructureRelease(pStruct);
241241 }
241242 return fts5IndexReturn(p);
241243}
241244
241245/*
241246** Buffer pPg contains a page of a tombstone hash table - one of nPg pages
241247** associated with the same segment. This function adds rowid iRowid to
241248** the hash table. The caller is required to guarantee that there is at
241249** least one free slot on the page.
241250**
241251** If parameter bForce is false and the hash table is deemed to be full
241252** (more than half of the slots are occupied), then non-zero is returned
241253** and iRowid not inserted. Or, if bForce is true or if the hash table page
241254** is not full, iRowid is inserted and zero returned.
241255*/
241256static int fts5IndexTombstoneAddToPage(
241257 Fts5Data *pPg,
241258 int bForce,
241259 int nPg,
241260 u64 iRowid
241261){
241262 const int szKey = TOMBSTONE_KEYSIZE(pPg);
241263 const int nSlot = TOMBSTONE_NSLOT(pPg);
241264 const int nElem = fts5GetU32(a: &pPg->p[4]);
241265 int iSlot = (iRowid / nPg) % nSlot;
241266 int nCollide = nSlot;
241267
241268 if( szKey==4 && iRowid>0xFFFFFFFF ) return 2;
241269 if( iRowid==0 ){
241270 pPg->p[1] = 0x01;
241271 return 0;
241272 }
241273
241274 if( bForce==0 && nElem>=(nSlot/2) ){
241275 return 1;
241276 }
241277
241278 fts5PutU32(a: &pPg->p[4], iVal: nElem+1);
241279 if( szKey==4 ){
241280 u32 *aSlot = (u32*)&pPg->p[8];
241281 while( aSlot[iSlot] ){
241282 iSlot = (iSlot + 1) % nSlot;
241283 if( nCollide--==0 ) return 0;
241284 }
241285 fts5PutU32(a: (u8*)&aSlot[iSlot], iVal: (u32)iRowid);
241286 }else{
241287 u64 *aSlot = (u64*)&pPg->p[8];
241288 while( aSlot[iSlot] ){
241289 iSlot = (iSlot + 1) % nSlot;
241290 if( nCollide--==0 ) return 0;
241291 }
241292 fts5PutU64(a: (u8*)&aSlot[iSlot], iVal: iRowid);
241293 }
241294
241295 return 0;
241296}
241297
241298/*
241299** This function attempts to build a new hash containing all the keys
241300** currently in the tombstone hash table for segment pSeg. The new
241301** hash will be stored in the nOut buffers passed in array apOut[].
241302** All pages of the new hash use key-size szKey (4 or 8).
241303**
241304** Return 0 if the hash is successfully rebuilt into the nOut pages.
241305** Or non-zero if it is not (because one page became overfull). In this
241306** case the caller should retry with a larger nOut parameter.
241307**
241308** Parameter pData1 is page iPg1 of the hash table being rebuilt.
241309*/
241310static int fts5IndexTombstoneRehash(
241311 Fts5Index *p,
241312 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
241313 Fts5Data *pData1, /* One page of current hash - or NULL */
241314 int iPg1, /* Which page of the current hash is pData1 */
241315 int szKey, /* 4 or 8, the keysize */
241316 int nOut, /* Number of output pages */
241317 Fts5Data **apOut /* Array of output hash pages */
241318){
241319 int ii;
241320 int res = 0;
241321
241322 /* Initialize the headers of all the output pages */
241323 for(ii=0; ii<nOut; ii++){
241324 apOut[ii]->p[0] = szKey;
241325 fts5PutU32(a: &apOut[ii]->p[4], iVal: 0);
241326 }
241327
241328 /* Loop through the current pages of the hash table. */
241329 for(ii=0; res==0 && ii<pSeg->nPgTombstone; ii++){
241330 Fts5Data *pData = 0; /* Page ii of the current hash table */
241331 Fts5Data *pFree = 0; /* Free this at the end of the loop */
241332
241333 if( iPg1==ii ){
241334 pData = pData1;
241335 }else{
241336 pFree = pData = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii));
241337 }
241338
241339 if( pData ){
241340 int szKeyIn = TOMBSTONE_KEYSIZE(pData);
241341 int nSlotIn = (pData->nn - 8) / szKeyIn;
241342 int iIn;
241343 for(iIn=0; iIn<nSlotIn; iIn++){
241344 u64 iVal = 0;
241345
241346 /* Read the value from slot iIn of the input page into iVal. */
241347 if( szKeyIn==4 ){
241348 u32 *aSlot = (u32*)&pData->p[8];
241349 if( aSlot[iIn] ) iVal = fts5GetU32(a: (u8*)&aSlot[iIn]);
241350 }else{
241351 u64 *aSlot = (u64*)&pData->p[8];
241352 if( aSlot[iIn] ) iVal = fts5GetU64(a: (u8*)&aSlot[iIn]);
241353 }
241354
241355 /* If iVal is not 0 at this point, insert it into the new hash table */
241356 if( iVal ){
241357 Fts5Data *pPg = apOut[(iVal % nOut)];
241358 res = fts5IndexTombstoneAddToPage(pPg, bForce: 0, nPg: nOut, iRowid: iVal);
241359 if( res ) break;
241360 }
241361 }
241362
241363 /* If this is page 0 of the old hash, copy the rowid-0-flag from the
241364 ** old hash to the new. */
241365 if( ii==0 ){
241366 apOut[0]->p[1] = pData->p[1];
241367 }
241368 }
241369 fts5DataRelease(pData: pFree);
241370 }
241371
241372 return res;
241373}
241374
241375/*
241376** This is called to rebuild the hash table belonging to segment pSeg.
241377** If parameter pData1 is not NULL, then one page of the existing hash table
241378** has already been loaded - pData1, which is page iPg1. The key-size for
241379** the new hash table is szKey (4 or 8).
241380**
241381** If successful, the new hash table is not written to disk. Instead,
241382** output parameter (*pnOut) is set to the number of pages in the new
241383** hash table, and (*papOut) to point to an array of buffers containing
241384** the new page data.
241385**
241386** If an error occurs, an error code is left in the Fts5Index object and
241387** both output parameters set to 0 before returning.
241388*/
241389static void fts5IndexTombstoneRebuild(
241390 Fts5Index *p,
241391 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
241392 Fts5Data *pData1, /* One page of current hash - or NULL */
241393 int iPg1, /* Which page of the current hash is pData1 */
241394 int szKey, /* 4 or 8, the keysize */
241395 int *pnOut, /* OUT: Number of output pages */
241396 Fts5Data ***papOut /* OUT: Output hash pages */
241397){
241398 const int MINSLOT = 32;
241399 int nSlotPerPage = MAX(MINSLOT, (p->pConfig->pgsz - 8) / szKey);
241400 int nSlot = 0; /* Number of slots in each output page */
241401 int nOut = 0;
241402
241403 /* Figure out how many output pages (nOut) and how many slots per
241404 ** page (nSlot). There are three possibilities:
241405 **
241406 ** 1. The hash table does not yet exist. In this case the new hash
241407 ** table will consist of a single page with MINSLOT slots.
241408 **
241409 ** 2. The hash table exists but is currently a single page. In this
241410 ** case an attempt is made to grow the page to accommodate the new
241411 ** entry. The page is allowed to grow up to nSlotPerPage (see above)
241412 ** slots.
241413 **
241414 ** 3. The hash table already consists of more than one page, or of
241415 ** a single page already so large that it cannot be grown. In this
241416 ** case the new hash consists of (nPg*2+1) pages of nSlotPerPage
241417 ** slots each, where nPg is the current number of pages in the
241418 ** hash table.
241419 */
241420 if( pSeg->nPgTombstone==0 ){
241421 /* Case 1. */
241422 nOut = 1;
241423 nSlot = MINSLOT;
241424 }else if( pSeg->nPgTombstone==1 ){
241425 /* Case 2. */
241426 int nElem = (int)fts5GetU32(a: &pData1->p[4]);
241427 assert( pData1 && iPg1==0 );
241428 nOut = 1;
241429 nSlot = MAX(nElem*4, MINSLOT);
241430 if( nSlot>nSlotPerPage ) nOut = 0;
241431 }
241432 if( nOut==0 ){
241433 /* Case 3. */
241434 nOut = (pSeg->nPgTombstone * 2 + 1);
241435 nSlot = nSlotPerPage;
241436 }
241437
241438 /* Allocate the required array and output pages */
241439 while( 1 ){
241440 int res = 0;
241441 int ii = 0;
241442 int szPage = 0;
241443 Fts5Data **apOut = 0;
241444
241445 /* Allocate space for the new hash table */
241446 assert( nSlot>=MINSLOT );
241447 apOut = (Fts5Data**)sqlite3Fts5MallocZero(pRc: &p->rc, nByte: sizeof(Fts5Data*) * nOut);
241448 szPage = 8 + nSlot*szKey;
241449 for(ii=0; ii<nOut; ii++){
241450 Fts5Data *pNew = (Fts5Data*)sqlite3Fts5MallocZero(pRc: &p->rc,
241451 nByte: sizeof(Fts5Data)+szPage
241452 );
241453 if( pNew ){
241454 pNew->nn = szPage;
241455 pNew->p = (u8*)&pNew[1];
241456 apOut[ii] = pNew;
241457 }
241458 }
241459
241460 /* Rebuild the hash table. */
241461 if( p->rc==SQLITE_OK ){
241462 res = fts5IndexTombstoneRehash(p, pSeg, pData1, iPg1, szKey, nOut, apOut);
241463 }
241464 if( res==0 ){
241465 if( p->rc ){
241466 fts5IndexFreeArray(ap: apOut, n: nOut);
241467 apOut = 0;
241468 nOut = 0;
241469 }
241470 *pnOut = nOut;
241471 *papOut = apOut;
241472 break;
241473 }
241474
241475 /* If control flows to here, it was not possible to rebuild the hash
241476 ** table. Free all buffers and then try again with more pages. */
241477 assert( p->rc==SQLITE_OK );
241478 fts5IndexFreeArray(ap: apOut, n: nOut);
241479 nSlot = nSlotPerPage;
241480 nOut = nOut*2 + 1;
241481 }
241482}
241483
241484
241485/*
241486** Add a tombstone for rowid iRowid to segment pSeg.
241487*/
241488static void fts5IndexTombstoneAdd(
241489 Fts5Index *p,
241490 Fts5StructureSegment *pSeg,
241491 u64 iRowid
241492){
241493 Fts5Data *pPg = 0;
241494 int iPg = -1;
241495 int szKey = 0;
241496 int nHash = 0;
241497 Fts5Data **apHash = 0;
241498
241499 p->nContentlessDelete++;
241500
241501 if( pSeg->nPgTombstone>0 ){
241502 iPg = iRowid % pSeg->nPgTombstone;
241503 pPg = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg));
241504 if( pPg==0 ){
241505 assert( p->rc!=SQLITE_OK );
241506 return;
241507 }
241508
241509 if( 0==fts5IndexTombstoneAddToPage(pPg, bForce: 0, nPg: pSeg->nPgTombstone, iRowid) ){
241510 fts5DataWrite(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg), pData: pPg->p, nData: pPg->nn);
241511 fts5DataRelease(pData: pPg);
241512 return;
241513 }
241514 }
241515
241516 /* Have to rebuild the hash table. First figure out the key-size (4 or 8). */
241517 szKey = pPg ? TOMBSTONE_KEYSIZE(pPg) : 4;
241518 if( iRowid>0xFFFFFFFF ) szKey = 8;
241519
241520 /* Rebuild the hash table */
241521 fts5IndexTombstoneRebuild(p, pSeg, pData1: pPg, iPg1: iPg, szKey, pnOut: &nHash, papOut: &apHash);
241522 assert( p->rc==SQLITE_OK || (nHash==0 && apHash==0) );
241523
241524 /* If all has succeeded, write the new rowid into one of the new hash
241525 ** table pages, then write them all out to disk. */
241526 if( nHash ){
241527 int ii = 0;
241528 fts5IndexTombstoneAddToPage(pPg: apHash[iRowid % nHash], bForce: 1, nPg: nHash, iRowid);
241529 for(ii=0; ii<nHash; ii++){
241530 i64 iTombstoneRowid = FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii);
241531 fts5DataWrite(p, iRowid: iTombstoneRowid, pData: apHash[ii]->p, nData: apHash[ii]->nn);
241532 }
241533 pSeg->nPgTombstone = nHash;
241534 fts5StructureWrite(p, pStruct: p->pStruct);
241535 }
241536
241537 fts5DataRelease(pData: pPg);
241538 fts5IndexFreeArray(ap: apHash, n: nHash);
241539}
241540
241541/*
241542** Add iRowid to the tombstone list of the segment or segments that contain
241543** rows from origin iOrigin. Return SQLITE_OK if successful, or an SQLite
241544** error code otherwise.
241545*/
241546static int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid){
241547 Fts5Structure *pStruct;
241548 pStruct = fts5StructureRead(p);
241549 if( pStruct ){
241550 int bFound = 0; /* True after pSeg->nEntryTombstone incr. */
241551 int iLvl;
241552 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
241553 int iSeg;
241554 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
241555 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
241556 if( pSeg->iOrigin1<=(u64)iOrigin && pSeg->iOrigin2>=(u64)iOrigin ){
241557 if( bFound==0 ){
241558 pSeg->nEntryTombstone++;
241559 bFound = 1;
241560 }
241561 fts5IndexTombstoneAdd(p, pSeg, iRowid);
241562 }
241563 }
241564 }
241565 fts5StructureRelease(pStruct);
241566 }
241567 return fts5IndexReturn(p);
241568}
241569
241570/*************************************************************************
241571**************************************************************************
241572** Below this point is the implementation of the integrity-check
241573** functionality.
241574*/
241575
241576/*
241577** Return a simple checksum value based on the arguments.
241578*/
241579static u64 sqlite3Fts5IndexEntryCksum(
241580 i64 iRowid,
241581 int iCol,
241582 int iPos,
241583 int iIdx,
241584 const char *pTerm,
241585 int nTerm
241586){
241587 int i;
241588 u64 ret = iRowid;
241589 ret += (ret<<3) + iCol;
241590 ret += (ret<<3) + iPos;
241591 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
241592 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
241593 return ret;
241594}
241595
241596#ifdef SQLITE_DEBUG
241597/*
241598** This function is purely an internal test. It does not contribute to
241599** FTS functionality, or even the integrity-check, in any way.
241600**
241601** Instead, it tests that the same set of pgno/rowid combinations are
241602** visited regardless of whether the doclist-index identified by parameters
241603** iSegid/iLeaf is iterated in forwards or reverse order.
241604*/
241605static void fts5TestDlidxReverse(
241606 Fts5Index *p,
241607 int iSegid, /* Segment id to load from */
241608 int iLeaf /* Load doclist-index for this leaf */
241609){
241610 Fts5DlidxIter *pDlidx = 0;
241611 u64 cksum1 = 13;
241612 u64 cksum2 = 13;
241613
241614 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
241615 fts5DlidxIterEof(p, pDlidx)==0;
241616 fts5DlidxIterNext(p, pDlidx)
241617 ){
241618 i64 iRowid = fts5DlidxIterRowid(pDlidx);
241619 int pgno = fts5DlidxIterPgno(pDlidx);
241620 assert( pgno>iLeaf );
241621 cksum1 += iRowid + ((i64)pgno<<32);
241622 }
241623 fts5DlidxIterFree(pDlidx);
241624 pDlidx = 0;
241625
241626 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
241627 fts5DlidxIterEof(p, pDlidx)==0;
241628 fts5DlidxIterPrev(p, pDlidx)
241629 ){
241630 i64 iRowid = fts5DlidxIterRowid(pDlidx);
241631 int pgno = fts5DlidxIterPgno(pDlidx);
241632 assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
241633 cksum2 += iRowid + ((i64)pgno<<32);
241634 }
241635 fts5DlidxIterFree(pDlidx);
241636 pDlidx = 0;
241637
241638 if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
241639}
241640
241641static int fts5QueryCksum(
241642 Fts5Index *p, /* Fts5 index object */
241643 int iIdx,
241644 const char *z, /* Index key to query for */
241645 int n, /* Size of index key in bytes */
241646 int flags, /* Flags for Fts5IndexQuery */
241647 u64 *pCksum /* IN/OUT: Checksum value */
241648){
241649 int eDetail = p->pConfig->eDetail;
241650 u64 cksum = *pCksum;
241651 Fts5IndexIter *pIter = 0;
241652 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
241653
241654 while( rc==SQLITE_OK && ALWAYS(pIter!=0) && 0==sqlite3Fts5IterEof(pIter) ){
241655 i64 rowid = pIter->iRowid;
241656
241657 if( eDetail==FTS5_DETAIL_NONE ){
241658 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
241659 }else{
241660 Fts5PoslistReader sReader;
241661 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
241662 sReader.bEof==0;
241663 sqlite3Fts5PoslistReaderNext(&sReader)
241664 ){
241665 int iCol = FTS5_POS2COLUMN(sReader.iPos);
241666 int iOff = FTS5_POS2OFFSET(sReader.iPos);
241667 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
241668 }
241669 }
241670 if( rc==SQLITE_OK ){
241671 rc = sqlite3Fts5IterNext(pIter);
241672 }
241673 }
241674 sqlite3Fts5IterClose(pIter);
241675
241676 *pCksum = cksum;
241677 return rc;
241678}
241679
241680/*
241681** Check if buffer z[], size n bytes, contains as series of valid utf-8
241682** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
241683** contain valid utf-8, return non-zero.
241684*/
241685static int fts5TestUtf8(const char *z, int n){
241686 int i = 0;
241687 assert_nc( n>0 );
241688 while( i<n ){
241689 if( (z[i] & 0x80)==0x00 ){
241690 i++;
241691 }else
241692 if( (z[i] & 0xE0)==0xC0 ){
241693 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
241694 i += 2;
241695 }else
241696 if( (z[i] & 0xF0)==0xE0 ){
241697 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
241698 i += 3;
241699 }else
241700 if( (z[i] & 0xF8)==0xF0 ){
241701 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
241702 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
241703 i += 3;
241704 }else{
241705 return 1;
241706 }
241707 }
241708
241709 return 0;
241710}
241711
241712/*
241713** This function is also purely an internal test. It does not contribute to
241714** FTS functionality, or even the integrity-check, in any way.
241715*/
241716static void fts5TestTerm(
241717 Fts5Index *p,
241718 Fts5Buffer *pPrev, /* Previous term */
241719 const char *z, int n, /* Possibly new term to test */
241720 u64 expected,
241721 u64 *pCksum
241722){
241723 int rc = p->rc;
241724 if( pPrev->n==0 ){
241725 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
241726 }else
241727 if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
241728 u64 cksum3 = *pCksum;
241729 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
241730 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
241731 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
241732 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
241733 u64 ck1 = 0;
241734 u64 ck2 = 0;
241735
241736 /* Check that the results returned for ASC and DESC queries are
241737 ** the same. If not, call this corruption. */
241738 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
241739 if( rc==SQLITE_OK ){
241740 int f = flags|FTS5INDEX_QUERY_DESC;
241741 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
241742 }
241743 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
241744
241745 /* If this is a prefix query, check that the results returned if the
241746 ** the index is disabled are the same. In both ASC and DESC order.
241747 **
241748 ** This check may only be performed if the hash table is empty. This
241749 ** is because the hash table only supports a single scan query at
241750 ** a time, and the multi-iter loop from which this function is called
241751 ** is already performing such a scan.
241752 **
241753 ** Also only do this if buffer zTerm contains nTerm bytes of valid
241754 ** utf-8. Otherwise, the last part of the buffer contents might contain
241755 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
241756 ** character stored in the main fts index, which will cause the
241757 ** test to fail. */
241758 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
241759 if( iIdx>0 && rc==SQLITE_OK ){
241760 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
241761 ck2 = 0;
241762 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
241763 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
241764 }
241765 if( iIdx>0 && rc==SQLITE_OK ){
241766 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
241767 ck2 = 0;
241768 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
241769 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
241770 }
241771 }
241772
241773 cksum3 ^= ck1;
241774 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
241775
241776 if( rc==SQLITE_OK && cksum3!=expected ){
241777 rc = FTS5_CORRUPT;
241778 }
241779 *pCksum = cksum3;
241780 }
241781 p->rc = rc;
241782}
241783
241784#else
241785# define fts5TestDlidxReverse(x,y,z)
241786# define fts5TestTerm(u,v,w,x,y,z)
241787#endif
241788
241789/*
241790** Check that:
241791**
241792** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
241793** contain zero terms.
241794** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
241795** contain zero rowids.
241796*/
241797static void fts5IndexIntegrityCheckEmpty(
241798 Fts5Index *p,
241799 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
241800 int iFirst,
241801 int iNoRowid,
241802 int iLast
241803){
241804 int i;
241805
241806 /* Now check that the iter.nEmpty leaves following the current leaf
241807 ** (a) exist and (b) contain no terms. */
241808 for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
241809 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
241810 if( pLeaf ){
241811 if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
241812 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
241813 }
241814 fts5DataRelease(pData: pLeaf);
241815 }
241816}
241817
241818static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
241819 int iTermOff = 0;
241820 int ii;
241821
241822 Fts5Buffer buf1 = {0,0,0};
241823 Fts5Buffer buf2 = {0,0,0};
241824
241825 ii = pLeaf->szLeaf;
241826 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
241827 int res;
241828 int iOff;
241829 int nIncr;
241830
241831 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
241832 iTermOff += nIncr;
241833 iOff = iTermOff;
241834
241835 if( iOff>=pLeaf->szLeaf ){
241836 p->rc = FTS5_CORRUPT;
241837 }else if( iTermOff==nIncr ){
241838 int nByte;
241839 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
241840 if( (iOff+nByte)>pLeaf->szLeaf ){
241841 p->rc = FTS5_CORRUPT;
241842 }else{
241843 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
241844 }
241845 }else{
241846 int nKeep, nByte;
241847 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
241848 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
241849 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
241850 p->rc = FTS5_CORRUPT;
241851 }else{
241852 buf1.n = nKeep;
241853 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
241854 }
241855
241856 if( p->rc==SQLITE_OK ){
241857 res = fts5BufferCompare(pLeft: &buf1, pRight: &buf2);
241858 if( res<=0 ) p->rc = FTS5_CORRUPT;
241859 }
241860 }
241861 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
241862 }
241863
241864 fts5BufferFree(&buf1);
241865 fts5BufferFree(&buf2);
241866}
241867
241868static void fts5IndexIntegrityCheckSegment(
241869 Fts5Index *p, /* FTS5 backend object */
241870 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
241871){
241872 Fts5Config *pConfig = p->pConfig;
241873 int bSecureDelete = (pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE);
241874 sqlite3_stmt *pStmt = 0;
241875 int rc2;
241876 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
241877 int iDlidxPrevLeaf = pSeg->pgnoLast;
241878
241879 if( pSeg->pgnoFirst==0 ) return;
241880
241881 fts5IndexPrepareStmt(p, ppStmt: &pStmt, zSql: sqlite3_mprintf(
241882 zFormat: "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
241883 "ORDER BY 1, 2",
241884 pConfig->zDb, pConfig->zName, pSeg->iSegid
241885 ));
241886
241887 /* Iterate through the b-tree hierarchy. */
241888 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
241889 i64 iRow; /* Rowid for this leaf */
241890 Fts5Data *pLeaf; /* Data for this leaf */
241891
241892 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, i: 1);
241893 int nIdxTerm = sqlite3_column_bytes(pStmt, i: 1);
241894 int iIdxLeaf = sqlite3_column_int(pStmt, i: 2);
241895 int bIdxDlidx = sqlite3_column_int(pStmt, i: 3);
241896
241897 /* If the leaf in question has already been trimmed from the segment,
241898 ** ignore this b-tree entry. Otherwise, load it into memory. */
241899 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
241900 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
241901 pLeaf = fts5LeafRead(p, iRowid: iRow);
241902 if( pLeaf==0 ) break;
241903
241904 /* Check that the leaf contains at least one term, and that it is equal
241905 ** to or larger than the split-key in zIdxTerm. Also check that if there
241906 ** is also a rowid pointer within the leaf page header, it points to a
241907 ** location before the term. */
241908 if( pLeaf->nn<=pLeaf->szLeaf ){
241909
241910 if( nIdxTerm==0
241911 && pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE
241912 && pLeaf->nn==pLeaf->szLeaf
241913 && pLeaf->nn==4
241914 ){
241915 /* special case - the very first page in a segment keeps its %_idx
241916 ** entry even if all the terms are removed from it by secure-delete
241917 ** operations. */
241918 }else{
241919 p->rc = FTS5_CORRUPT;
241920 }
241921
241922 }else{
241923 int iOff; /* Offset of first term on leaf */
241924 int iRowidOff; /* Offset of first rowid on leaf */
241925 int nTerm; /* Size of term on leaf in bytes */
241926 int res; /* Comparison of term and split-key */
241927
241928 iOff = fts5LeafFirstTermOff(pLeaf);
241929 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
241930 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
241931 p->rc = FTS5_CORRUPT;
241932 }else{
241933 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
241934 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
241935 if( res==0 ) res = nTerm - nIdxTerm;
241936 if( res<0 ) p->rc = FTS5_CORRUPT;
241937 }
241938
241939 fts5IntegrityCheckPgidx(p, pLeaf);
241940 }
241941 fts5DataRelease(pData: pLeaf);
241942 if( p->rc ) break;
241943
241944 /* Now check that the iter.nEmpty leaves following the current leaf
241945 ** (a) exist and (b) contain no terms. */
241946 fts5IndexIntegrityCheckEmpty(
241947 p, pSeg, iFirst: iIdxPrevLeaf+1, iNoRowid: iDlidxPrevLeaf+1, iLast: iIdxLeaf-1
241948 );
241949 if( p->rc ) break;
241950
241951 /* If there is a doclist-index, check that it looks right. */
241952 if( bIdxDlidx ){
241953 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
241954 int iPrevLeaf = iIdxLeaf;
241955 int iSegid = pSeg->iSegid;
241956 int iPg = 0;
241957 i64 iKey;
241958
241959 for(pDlidx=fts5DlidxIterInit(p, bRev: 0, iSegid, iLeafPg: iIdxLeaf);
241960 fts5DlidxIterEof(p, pIter: pDlidx)==0;
241961 fts5DlidxIterNext(p, pIter: pDlidx)
241962 ){
241963
241964 /* Check any rowid-less pages that occur before the current leaf. */
241965 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pIter: pDlidx); iPg++){
241966 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
241967 pLeaf = fts5DataRead(p, iRowid: iKey);
241968 if( pLeaf ){
241969 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
241970 fts5DataRelease(pData: pLeaf);
241971 }
241972 }
241973 iPrevLeaf = fts5DlidxIterPgno(pIter: pDlidx);
241974
241975 /* Check that the leaf page indicated by the iterator really does
241976 ** contain the rowid suggested by the same. */
241977 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
241978 pLeaf = fts5DataRead(p, iRowid: iKey);
241979 if( pLeaf ){
241980 i64 iRowid;
241981 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
241982 ASSERT_SZLEAF_OK(pLeaf);
241983 if( iRowidOff>=pLeaf->szLeaf ){
241984 p->rc = FTS5_CORRUPT;
241985 }else if( bSecureDelete==0 || iRowidOff>0 ){
241986 i64 iDlRowid = fts5DlidxIterRowid(pIter: pDlidx);
241987 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
241988 if( iRowid<iDlRowid || (bSecureDelete==0 && iRowid!=iDlRowid) ){
241989 p->rc = FTS5_CORRUPT;
241990 }
241991 }
241992 fts5DataRelease(pData: pLeaf);
241993 }
241994 }
241995
241996 iDlidxPrevLeaf = iPg;
241997 fts5DlidxIterFree(pIter: pDlidx);
241998 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
241999 }else{
242000 iDlidxPrevLeaf = pSeg->pgnoLast;
242001 /* TODO: Check there is no doclist index */
242002 }
242003
242004 iIdxPrevLeaf = iIdxLeaf;
242005 }
242006
242007 rc2 = sqlite3_finalize(pStmt);
242008 if( p->rc==SQLITE_OK ) p->rc = rc2;
242009
242010 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
242011#if 0
242012 if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
242013 p->rc = FTS5_CORRUPT;
242014 }
242015#endif
242016}
242017
242018
242019/*
242020** Run internal checks to ensure that the FTS index (a) is internally
242021** consistent and (b) contains entries for which the XOR of the checksums
242022** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
242023**
242024** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
242025** checksum does not match. Return SQLITE_OK if all checks pass without
242026** error, or some other SQLite error code if another error (e.g. OOM)
242027** occurs.
242028*/
242029static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum, int bUseCksum){
242030 int eDetail = p->pConfig->eDetail;
242031 u64 cksum2 = 0; /* Checksum based on contents of indexes */
242032 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
242033 Fts5Iter *pIter; /* Used to iterate through entire index */
242034 Fts5Structure *pStruct; /* Index structure */
242035 int iLvl, iSeg;
242036
242037#ifdef SQLITE_DEBUG
242038 /* Used by extra internal tests only run if NDEBUG is not defined */
242039 u64 cksum3 = 0; /* Checksum based on contents of indexes */
242040 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
242041#endif
242042 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
242043
242044 /* Load the FTS index structure */
242045 pStruct = fts5StructureRead(p);
242046 if( pStruct==0 ){
242047 assert( p->rc!=SQLITE_OK );
242048 return fts5IndexReturn(p);
242049 }
242050
242051 /* Check that the internal nodes of each segment match the leaves */
242052 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
242053 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
242054 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
242055 fts5IndexIntegrityCheckSegment(p, pSeg);
242056 }
242057 }
242058
242059 /* The cksum argument passed to this function is a checksum calculated
242060 ** based on all expected entries in the FTS index (including prefix index
242061 ** entries). This block checks that a checksum calculated based on the
242062 ** actual contents of FTS index is identical.
242063 **
242064 ** Two versions of the same checksum are calculated. The first (stack
242065 ** variable cksum2) based on entries extracted from the full-text index
242066 ** while doing a linear scan of each individual index in turn.
242067 **
242068 ** As each term visited by the linear scans, a separate query for the
242069 ** same term is performed. cksum3 is calculated based on the entries
242070 ** extracted by these queries.
242071 */
242072 for(fts5MultiIterNew(p, pStruct, flags, pColset: 0, pTerm: 0, nTerm: 0, iLevel: -1, nSegment: 0, ppOut: &pIter);
242073 fts5MultiIterEof(p, pIter)==0;
242074 fts5MultiIterNext(p, pIter, bFrom: 0, iFrom: 0)
242075 ){
242076 int n; /* Size of term in bytes */
242077 i64 iPos = 0; /* Position read from poslist */
242078 int iOff = 0; /* Offset within poslist */
242079 i64 iRowid = fts5MultiIterRowid(pIter);
242080 char *z = (char*)fts5MultiIterTerm(pIter, pn: &n);
242081
242082 /* If this is a new term, query for it. Update cksum3 with the results. */
242083 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
242084 if( p->rc ) break;
242085
242086 if( eDetail==FTS5_DETAIL_NONE ){
242087 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
242088 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol: 0, iPos: 0, iIdx: -1, pTerm: z, nTerm: n);
242089 }
242090 }else{
242091 poslist.n = 0;
242092 fts5SegiterPoslist(p, pSeg: &pIter->aSeg[pIter->aFirst[1].iFirst], pColset: 0, pBuf: &poslist);
242093 fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0");
242094 while( 0==sqlite3Fts5PoslistNext64(a: poslist.p, n: poslist.n, pi: &iOff, piOff: &iPos) ){
242095 int iCol = FTS5_POS2COLUMN(iPos);
242096 int iTokOff = FTS5_POS2OFFSET(iPos);
242097 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iPos: iTokOff, iIdx: -1, pTerm: z, nTerm: n);
242098 }
242099 }
242100 }
242101 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
242102
242103 fts5MultiIterFree(pIter);
242104 if( p->rc==SQLITE_OK && bUseCksum && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
242105
242106 fts5StructureRelease(pStruct);
242107#ifdef SQLITE_DEBUG
242108 fts5BufferFree(&term);
242109#endif
242110 fts5BufferFree(&poslist);
242111 return fts5IndexReturn(p);
242112}
242113
242114/*************************************************************************
242115**************************************************************************
242116** Below this point is the implementation of the fts5_decode() scalar
242117** function only.
242118*/
242119
242120#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242121/*
242122** Decode a segment-data rowid from the %_data table. This function is
242123** the opposite of macro FTS5_SEGMENT_ROWID().
242124*/
242125static void fts5DecodeRowid(
242126 i64 iRowid, /* Rowid from %_data table */
242127 int *pbTombstone, /* OUT: Tombstone hash flag */
242128 int *piSegid, /* OUT: Segment id */
242129 int *pbDlidx, /* OUT: Dlidx flag */
242130 int *piHeight, /* OUT: Height */
242131 int *piPgno /* OUT: Page number */
242132){
242133 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
242134 iRowid >>= FTS5_DATA_PAGE_B;
242135
242136 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
242137 iRowid >>= FTS5_DATA_HEIGHT_B;
242138
242139 *pbDlidx = (int)(iRowid & 0x0001);
242140 iRowid >>= FTS5_DATA_DLI_B;
242141
242142 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
242143 iRowid >>= FTS5_DATA_ID_B;
242144
242145 *pbTombstone = (int)(iRowid & 0x0001);
242146}
242147#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242148
242149#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242150static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
242151 int iSegid, iHeight, iPgno, bDlidx, bTomb; /* Rowid compenents */
242152 fts5DecodeRowid(iKey, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
242153
242154 if( iSegid==0 ){
242155 if( iKey==FTS5_AVERAGES_ROWID ){
242156 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
242157 }else{
242158 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
242159 }
242160 }
242161 else{
242162 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%s%ssegid=%d h=%d pgno=%d}",
242163 bDlidx ? "dlidx " : "",
242164 bTomb ? "tombstone " : "",
242165 iSegid, iHeight, iPgno
242166 );
242167 }
242168}
242169#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242170
242171#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242172static void fts5DebugStructure(
242173 int *pRc, /* IN/OUT: error code */
242174 Fts5Buffer *pBuf,
242175 Fts5Structure *p
242176){
242177 int iLvl, iSeg; /* Iterate through levels, segments */
242178
242179 for(iLvl=0; iLvl<p->nLevel; iLvl++){
242180 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
242181 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
242182 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
242183 );
242184 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
242185 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
242186 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d",
242187 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
242188 );
242189 if( pSeg->iOrigin1>0 ){
242190 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " origin=%lld..%lld",
242191 pSeg->iOrigin1, pSeg->iOrigin2
242192 );
242193 }
242194 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
242195 }
242196 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
242197 }
242198}
242199#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242200
242201#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242202/*
242203** This is part of the fts5_decode() debugging aid.
242204**
242205** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
242206** function appends a human-readable representation of the same object
242207** to the buffer passed as the second argument.
242208*/
242209static void fts5DecodeStructure(
242210 int *pRc, /* IN/OUT: error code */
242211 Fts5Buffer *pBuf,
242212 const u8 *pBlob, int nBlob
242213){
242214 int rc; /* Return code */
242215 Fts5Structure *p = 0; /* Decoded structure object */
242216
242217 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
242218 if( rc!=SQLITE_OK ){
242219 *pRc = rc;
242220 return;
242221 }
242222
242223 fts5DebugStructure(pRc, pBuf, p);
242224 fts5StructureRelease(p);
242225}
242226#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242227
242228#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242229/*
242230** This is part of the fts5_decode() debugging aid.
242231**
242232** Arguments pBlob/nBlob contain an "averages" record. This function
242233** appends a human-readable representation of record to the buffer passed
242234** as the second argument.
242235*/
242236static void fts5DecodeAverages(
242237 int *pRc, /* IN/OUT: error code */
242238 Fts5Buffer *pBuf,
242239 const u8 *pBlob, int nBlob
242240){
242241 int i = 0;
242242 const char *zSpace = "";
242243
242244 while( i<nBlob ){
242245 u64 iVal;
242246 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
242247 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
242248 zSpace = " ";
242249 }
242250}
242251#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242252
242253#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242254/*
242255** Buffer (a/n) is assumed to contain a list of serialized varints. Read
242256** each varint and append its string representation to buffer pBuf. Return
242257** after either the input buffer is exhausted or a 0 value is read.
242258**
242259** The return value is the number of bytes read from the input buffer.
242260*/
242261static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
242262 int iOff = 0;
242263 while( iOff<n ){
242264 int iVal;
242265 iOff += fts5GetVarint32(&a[iOff], iVal);
242266 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
242267 }
242268 return iOff;
242269}
242270#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242271
242272#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242273/*
242274** The start of buffer (a/n) contains the start of a doclist. The doclist
242275** may or may not finish within the buffer. This function appends a text
242276** representation of the part of the doclist that is present to buffer
242277** pBuf.
242278**
242279** The return value is the number of bytes read from the input buffer.
242280*/
242281static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
242282 i64 iDocid = 0;
242283 int iOff = 0;
242284
242285 if( n>0 ){
242286 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
242287 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
242288 }
242289 while( iOff<n ){
242290 int nPos;
242291 int bDel;
242292 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
242293 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
242294 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
242295 if( iOff<n ){
242296 i64 iDelta;
242297 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
242298 iDocid += iDelta;
242299 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
242300 }
242301 }
242302
242303 return iOff;
242304}
242305#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242306
242307#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242308/*
242309** This function is part of the fts5_decode() debugging function. It is
242310** only ever used with detail=none tables.
242311**
242312** Buffer (pData/nData) contains a doclist in the format used by detail=none
242313** tables. This function appends a human-readable version of that list to
242314** buffer pBuf.
242315**
242316** If *pRc is other than SQLITE_OK when this function is called, it is a
242317** no-op. If an OOM or other error occurs within this function, *pRc is
242318** set to an SQLite error code before returning. The final state of buffer
242319** pBuf is undefined in this case.
242320*/
242321static void fts5DecodeRowidList(
242322 int *pRc, /* IN/OUT: Error code */
242323 Fts5Buffer *pBuf, /* Buffer to append text to */
242324 const u8 *pData, int nData /* Data to decode list-of-rowids from */
242325){
242326 int i = 0;
242327 i64 iRowid = 0;
242328
242329 while( i<nData ){
242330 const char *zApp = "";
242331 u64 iVal;
242332 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
242333 iRowid += iVal;
242334
242335 if( i<nData && pData[i]==0x00 ){
242336 i++;
242337 if( i<nData && pData[i]==0x00 ){
242338 i++;
242339 zApp = "+";
242340 }else{
242341 zApp = "*";
242342 }
242343 }
242344
242345 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
242346 }
242347}
242348#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242349
242350#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242351/*
242352** The implementation of user-defined scalar function fts5_decode().
242353*/
242354static void fts5DecodeFunction(
242355 sqlite3_context *pCtx, /* Function call context */
242356 int nArg, /* Number of args (always 2) */
242357 sqlite3_value **apVal /* Function arguments */
242358){
242359 i64 iRowid; /* Rowid for record being decoded */
242360 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
242361 int bTomb;
242362 const u8 *aBlob; int n; /* Record to decode */
242363 u8 *a = 0;
242364 Fts5Buffer s; /* Build up text to return here */
242365 int rc = SQLITE_OK; /* Return code */
242366 sqlite3_int64 nSpace = 0;
242367 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
242368
242369 assert( nArg==2 );
242370 UNUSED_PARAM(nArg);
242371 memset(&s, 0, sizeof(Fts5Buffer));
242372 iRowid = sqlite3_value_int64(apVal[0]);
242373
242374 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
242375 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
242376 ** buffer overreads even if the record is corrupt. */
242377 n = sqlite3_value_bytes(apVal[1]);
242378 aBlob = sqlite3_value_blob(apVal[1]);
242379 nSpace = n + FTS5_DATA_ZERO_PADDING;
242380 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
242381 if( a==0 ) goto decode_out;
242382 if( n>0 ) memcpy(a, aBlob, n);
242383
242384 fts5DecodeRowid(iRowid, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
242385
242386 fts5DebugRowid(&rc, &s, iRowid);
242387 if( bDlidx ){
242388 Fts5Data dlidx;
242389 Fts5DlidxLvl lvl;
242390
242391 dlidx.p = a;
242392 dlidx.nn = n;
242393
242394 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
242395 lvl.pData = &dlidx;
242396 lvl.iLeafPgno = iPgno;
242397
242398 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
242399 sqlite3Fts5BufferAppendPrintf(&rc, &s,
242400 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
242401 );
242402 }
242403 }else if( bTomb ){
242404 u32 nElem = fts5GetU32(&a[4]);
242405 int szKey = (aBlob[0]==4 || aBlob[0]==8) ? aBlob[0] : 8;
242406 int nSlot = (n - 8) / szKey;
242407 int ii;
242408 sqlite3Fts5BufferAppendPrintf(&rc, &s, " nElem=%d", (int)nElem);
242409 if( aBlob[1] ){
242410 sqlite3Fts5BufferAppendPrintf(&rc, &s, " 0");
242411 }
242412 for(ii=0; ii<nSlot; ii++){
242413 u64 iVal = 0;
242414 if( szKey==4 ){
242415 u32 *aSlot = (u32*)&aBlob[8];
242416 if( aSlot[ii] ) iVal = fts5GetU32((u8*)&aSlot[ii]);
242417 }else{
242418 u64 *aSlot = (u64*)&aBlob[8];
242419 if( aSlot[ii] ) iVal = fts5GetU64((u8*)&aSlot[ii]);
242420 }
242421 if( iVal!=0 ){
242422 sqlite3Fts5BufferAppendPrintf(&rc, &s, " %lld", (i64)iVal);
242423 }
242424 }
242425 }else if( iSegid==0 ){
242426 if( iRowid==FTS5_AVERAGES_ROWID ){
242427 fts5DecodeAverages(&rc, &s, a, n);
242428 }else{
242429 fts5DecodeStructure(&rc, &s, a, n);
242430 }
242431 }else if( eDetailNone ){
242432 Fts5Buffer term; /* Current term read from page */
242433 int szLeaf;
242434 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
242435 int iTermOff;
242436 int nKeep = 0;
242437 int iOff;
242438
242439 memset(&term, 0, sizeof(Fts5Buffer));
242440
242441 /* Decode any entries that occur before the first term. */
242442 if( szLeaf<n ){
242443 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
242444 }else{
242445 iTermOff = szLeaf;
242446 }
242447 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
242448
242449 iOff = iTermOff;
242450 while( iOff<szLeaf && rc==SQLITE_OK ){
242451 int nAppend;
242452
242453 /* Read the term data for the next term*/
242454 iOff += fts5GetVarint32(&a[iOff], nAppend);
242455 term.n = nKeep;
242456 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
242457 sqlite3Fts5BufferAppendPrintf(
242458 &rc, &s, " term=%.*s", term.n, (const char*)term.p
242459 );
242460 iOff += nAppend;
242461
242462 /* Figure out where the doclist for this term ends */
242463 if( iPgidxOff<n ){
242464 int nIncr;
242465 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
242466 iTermOff += nIncr;
242467 }else{
242468 iTermOff = szLeaf;
242469 }
242470 if( iTermOff>szLeaf ){
242471 rc = FTS5_CORRUPT;
242472 }else{
242473 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
242474 }
242475 iOff = iTermOff;
242476 if( iOff<szLeaf ){
242477 iOff += fts5GetVarint32(&a[iOff], nKeep);
242478 }
242479 }
242480
242481 fts5BufferFree(&term);
242482 }else{
242483 Fts5Buffer term; /* Current term read from page */
242484 int szLeaf; /* Offset of pgidx in a[] */
242485 int iPgidxOff;
242486 int iPgidxPrev = 0; /* Previous value read from pgidx */
242487 int iTermOff = 0;
242488 int iRowidOff = 0;
242489 int iOff;
242490 int nDoclist;
242491
242492 memset(&term, 0, sizeof(Fts5Buffer));
242493
242494 if( n<4 ){
242495 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
242496 goto decode_out;
242497 }else{
242498 iRowidOff = fts5GetU16(&a[0]);
242499 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
242500 if( iPgidxOff<n ){
242501 fts5GetVarint32(&a[iPgidxOff], iTermOff);
242502 }else if( iPgidxOff>n ){
242503 rc = FTS5_CORRUPT;
242504 goto decode_out;
242505 }
242506 }
242507
242508 /* Decode the position list tail at the start of the page */
242509 if( iRowidOff!=0 ){
242510 iOff = iRowidOff;
242511 }else if( iTermOff!=0 ){
242512 iOff = iTermOff;
242513 }else{
242514 iOff = szLeaf;
242515 }
242516 if( iOff>n ){
242517 rc = FTS5_CORRUPT;
242518 goto decode_out;
242519 }
242520 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
242521
242522 /* Decode any more doclist data that appears on the page before the
242523 ** first term. */
242524 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
242525 if( nDoclist+iOff>n ){
242526 rc = FTS5_CORRUPT;
242527 goto decode_out;
242528 }
242529 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
242530
242531 while( iPgidxOff<n && rc==SQLITE_OK ){
242532 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
242533 int nByte; /* Bytes of data */
242534 int iEnd;
242535
242536 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
242537 iPgidxPrev += nByte;
242538 iOff = iPgidxPrev;
242539
242540 if( iPgidxOff<n ){
242541 fts5GetVarint32(&a[iPgidxOff], nByte);
242542 iEnd = iPgidxPrev + nByte;
242543 }else{
242544 iEnd = szLeaf;
242545 }
242546 if( iEnd>szLeaf ){
242547 rc = FTS5_CORRUPT;
242548 break;
242549 }
242550
242551 if( bFirst==0 ){
242552 iOff += fts5GetVarint32(&a[iOff], nByte);
242553 if( nByte>term.n ){
242554 rc = FTS5_CORRUPT;
242555 break;
242556 }
242557 term.n = nByte;
242558 }
242559 iOff += fts5GetVarint32(&a[iOff], nByte);
242560 if( iOff+nByte>n ){
242561 rc = FTS5_CORRUPT;
242562 break;
242563 }
242564 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
242565 iOff += nByte;
242566
242567 sqlite3Fts5BufferAppendPrintf(
242568 &rc, &s, " term=%.*s", term.n, (const char*)term.p
242569 );
242570 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
242571 }
242572
242573 fts5BufferFree(&term);
242574 }
242575
242576 decode_out:
242577 sqlite3_free(a);
242578 if( rc==SQLITE_OK ){
242579 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
242580 }else{
242581 sqlite3_result_error_code(pCtx, rc);
242582 }
242583 fts5BufferFree(&s);
242584}
242585#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242586
242587#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242588/*
242589** The implementation of user-defined scalar function fts5_rowid().
242590*/
242591static void fts5RowidFunction(
242592 sqlite3_context *pCtx, /* Function call context */
242593 int nArg, /* Number of args (always 2) */
242594 sqlite3_value **apVal /* Function arguments */
242595){
242596 const char *zArg;
242597 if( nArg==0 ){
242598 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
242599 }else{
242600 zArg = (const char*)sqlite3_value_text(apVal[0]);
242601 if( 0==sqlite3_stricmp(zArg, "segment") ){
242602 i64 iRowid;
242603 int segid, pgno;
242604 if( nArg!=3 ){
242605 sqlite3_result_error(pCtx,
242606 "should be: fts5_rowid('segment', segid, pgno))", -1
242607 );
242608 }else{
242609 segid = sqlite3_value_int(apVal[1]);
242610 pgno = sqlite3_value_int(apVal[2]);
242611 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
242612 sqlite3_result_int64(pCtx, iRowid);
242613 }
242614 }else{
242615 sqlite3_result_error(pCtx,
242616 "first arg to fts5_rowid() must be 'segment'" , -1
242617 );
242618 }
242619 }
242620}
242621#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242622
242623#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242624
242625typedef struct Fts5StructVtab Fts5StructVtab;
242626struct Fts5StructVtab {
242627 sqlite3_vtab base;
242628};
242629
242630typedef struct Fts5StructVcsr Fts5StructVcsr;
242631struct Fts5StructVcsr {
242632 sqlite3_vtab_cursor base;
242633 Fts5Structure *pStruct;
242634 int iLevel;
242635 int iSeg;
242636 int iRowid;
242637};
242638
242639/*
242640** Create a new fts5_structure() table-valued function.
242641*/
242642static int fts5structConnectMethod(
242643 sqlite3 *db,
242644 void *pAux,
242645 int argc, const char *const*argv,
242646 sqlite3_vtab **ppVtab,
242647 char **pzErr
242648){
242649 Fts5StructVtab *pNew = 0;
242650 int rc = SQLITE_OK;
242651
242652 rc = sqlite3_declare_vtab(db,
242653 "CREATE TABLE xyz("
242654 "level, segment, merge, segid, leaf1, leaf2, loc1, loc2, "
242655 "npgtombstone, nentrytombstone, nentry, struct HIDDEN);"
242656 );
242657 if( rc==SQLITE_OK ){
242658 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
242659 }
242660
242661 *ppVtab = (sqlite3_vtab*)pNew;
242662 return rc;
242663}
242664
242665/*
242666** We must have a single struct=? constraint that will be passed through
242667** into the xFilter method. If there is no valid stmt=? constraint,
242668** then return an SQLITE_CONSTRAINT error.
242669*/
242670static int fts5structBestIndexMethod(
242671 sqlite3_vtab *tab,
242672 sqlite3_index_info *pIdxInfo
242673){
242674 int i;
242675 int rc = SQLITE_CONSTRAINT;
242676 struct sqlite3_index_constraint *p;
242677 pIdxInfo->estimatedCost = (double)100;
242678 pIdxInfo->estimatedRows = 100;
242679 pIdxInfo->idxNum = 0;
242680 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
242681 if( p->usable==0 ) continue;
242682 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==11 ){
242683 rc = SQLITE_OK;
242684 pIdxInfo->aConstraintUsage[i].omit = 1;
242685 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
242686 break;
242687 }
242688 }
242689 return rc;
242690}
242691
242692/*
242693** This method is the destructor for bytecodevtab objects.
242694*/
242695static int fts5structDisconnectMethod(sqlite3_vtab *pVtab){
242696 Fts5StructVtab *p = (Fts5StructVtab*)pVtab;
242697 sqlite3_free(p);
242698 return SQLITE_OK;
242699}
242700
242701/*
242702** Constructor for a new bytecodevtab_cursor object.
242703*/
242704static int fts5structOpenMethod(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
242705 int rc = SQLITE_OK;
242706 Fts5StructVcsr *pNew = 0;
242707
242708 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
242709 *ppCsr = (sqlite3_vtab_cursor*)pNew;
242710
242711 return SQLITE_OK;
242712}
242713
242714/*
242715** Destructor for a bytecodevtab_cursor.
242716*/
242717static int fts5structCloseMethod(sqlite3_vtab_cursor *cur){
242718 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
242719 fts5StructureRelease(pCsr->pStruct);
242720 sqlite3_free(pCsr);
242721 return SQLITE_OK;
242722}
242723
242724
242725/*
242726** Advance a bytecodevtab_cursor to its next row of output.
242727*/
242728static int fts5structNextMethod(sqlite3_vtab_cursor *cur){
242729 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
242730 Fts5Structure *p = pCsr->pStruct;
242731
242732 assert( pCsr->pStruct );
242733 pCsr->iSeg++;
242734 pCsr->iRowid++;
242735 while( pCsr->iLevel<p->nLevel && pCsr->iSeg>=p->aLevel[pCsr->iLevel].nSeg ){
242736 pCsr->iLevel++;
242737 pCsr->iSeg = 0;
242738 }
242739 if( pCsr->iLevel>=p->nLevel ){
242740 fts5StructureRelease(pCsr->pStruct);
242741 pCsr->pStruct = 0;
242742 }
242743 return SQLITE_OK;
242744}
242745
242746/*
242747** Return TRUE if the cursor has been moved off of the last
242748** row of output.
242749*/
242750static int fts5structEofMethod(sqlite3_vtab_cursor *cur){
242751 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
242752 return pCsr->pStruct==0;
242753}
242754
242755static int fts5structRowidMethod(
242756 sqlite3_vtab_cursor *cur,
242757 sqlite_int64 *piRowid
242758){
242759 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
242760 *piRowid = pCsr->iRowid;
242761 return SQLITE_OK;
242762}
242763
242764/*
242765** Return values of columns for the row at which the bytecodevtab_cursor
242766** is currently pointing.
242767*/
242768static int fts5structColumnMethod(
242769 sqlite3_vtab_cursor *cur, /* The cursor */
242770 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
242771 int i /* Which column to return */
242772){
242773 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
242774 Fts5Structure *p = pCsr->pStruct;
242775 Fts5StructureSegment *pSeg = &p->aLevel[pCsr->iLevel].aSeg[pCsr->iSeg];
242776
242777 switch( i ){
242778 case 0: /* level */
242779 sqlite3_result_int(ctx, pCsr->iLevel);
242780 break;
242781 case 1: /* segment */
242782 sqlite3_result_int(ctx, pCsr->iSeg);
242783 break;
242784 case 2: /* merge */
242785 sqlite3_result_int(ctx, pCsr->iSeg < p->aLevel[pCsr->iLevel].nMerge);
242786 break;
242787 case 3: /* segid */
242788 sqlite3_result_int(ctx, pSeg->iSegid);
242789 break;
242790 case 4: /* leaf1 */
242791 sqlite3_result_int(ctx, pSeg->pgnoFirst);
242792 break;
242793 case 5: /* leaf2 */
242794 sqlite3_result_int(ctx, pSeg->pgnoLast);
242795 break;
242796 case 6: /* origin1 */
242797 sqlite3_result_int64(ctx, pSeg->iOrigin1);
242798 break;
242799 case 7: /* origin2 */
242800 sqlite3_result_int64(ctx, pSeg->iOrigin2);
242801 break;
242802 case 8: /* npgtombstone */
242803 sqlite3_result_int(ctx, pSeg->nPgTombstone);
242804 break;
242805 case 9: /* nentrytombstone */
242806 sqlite3_result_int64(ctx, pSeg->nEntryTombstone);
242807 break;
242808 case 10: /* nentry */
242809 sqlite3_result_int64(ctx, pSeg->nEntry);
242810 break;
242811 }
242812 return SQLITE_OK;
242813}
242814
242815/*
242816** Initialize a cursor.
242817**
242818** idxNum==0 means show all subprograms
242819** idxNum==1 means show only the main bytecode and omit subprograms.
242820*/
242821static int fts5structFilterMethod(
242822 sqlite3_vtab_cursor *pVtabCursor,
242823 int idxNum, const char *idxStr,
242824 int argc, sqlite3_value **argv
242825){
242826 Fts5StructVcsr *pCsr = (Fts5StructVcsr *)pVtabCursor;
242827 int rc = SQLITE_OK;
242828
242829 const u8 *aBlob = 0;
242830 int nBlob = 0;
242831
242832 assert( argc==1 );
242833 fts5StructureRelease(pCsr->pStruct);
242834 pCsr->pStruct = 0;
242835
242836 nBlob = sqlite3_value_bytes(argv[0]);
242837 aBlob = (const u8*)sqlite3_value_blob(argv[0]);
242838 rc = fts5StructureDecode(aBlob, nBlob, 0, &pCsr->pStruct);
242839 if( rc==SQLITE_OK ){
242840 pCsr->iLevel = 0;
242841 pCsr->iRowid = 0;
242842 pCsr->iSeg = -1;
242843 rc = fts5structNextMethod(pVtabCursor);
242844 }
242845
242846 return rc;
242847}
242848
242849#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
242850
242851/*
242852** This is called as part of registering the FTS5 module with database
242853** connection db. It registers several user-defined scalar functions useful
242854** with FTS5.
242855**
242856** If successful, SQLITE_OK is returned. If an error occurs, some other
242857** SQLite error code is returned instead.
242858*/
242859static int sqlite3Fts5IndexInit(sqlite3 *db){
242860#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242861 int rc = sqlite3_create_function(
242862 db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
242863 );
242864
242865 if( rc==SQLITE_OK ){
242866 rc = sqlite3_create_function(
242867 db, "fts5_decode_none", 2,
242868 SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
242869 );
242870 }
242871
242872 if( rc==SQLITE_OK ){
242873 rc = sqlite3_create_function(
242874 db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
242875 );
242876 }
242877
242878 if( rc==SQLITE_OK ){
242879 static const sqlite3_module fts5structure_module = {
242880 0, /* iVersion */
242881 0, /* xCreate */
242882 fts5structConnectMethod, /* xConnect */
242883 fts5structBestIndexMethod, /* xBestIndex */
242884 fts5structDisconnectMethod, /* xDisconnect */
242885 0, /* xDestroy */
242886 fts5structOpenMethod, /* xOpen */
242887 fts5structCloseMethod, /* xClose */
242888 fts5structFilterMethod, /* xFilter */
242889 fts5structNextMethod, /* xNext */
242890 fts5structEofMethod, /* xEof */
242891 fts5structColumnMethod, /* xColumn */
242892 fts5structRowidMethod, /* xRowid */
242893 0, /* xUpdate */
242894 0, /* xBegin */
242895 0, /* xSync */
242896 0, /* xCommit */
242897 0, /* xRollback */
242898 0, /* xFindFunction */
242899 0, /* xRename */
242900 0, /* xSavepoint */
242901 0, /* xRelease */
242902 0, /* xRollbackTo */
242903 0 /* xShadowName */
242904 };
242905 rc = sqlite3_create_module(db, "fts5_structure", &fts5structure_module, 0);
242906 }
242907 return rc;
242908#else
242909 return SQLITE_OK;
242910 UNUSED_PARAM(db);
242911#endif
242912}
242913
242914
242915static int sqlite3Fts5IndexReset(Fts5Index *p){
242916 assert( p->pStruct==0 || p->iStructVersion!=0 );
242917 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
242918 fts5StructureInvalidate(p);
242919 }
242920 return fts5IndexReturn(p);
242921}
242922
242923/*
242924** 2014 Jun 09
242925**
242926** The author disclaims copyright to this source code. In place of
242927** a legal notice, here is a blessing:
242928**
242929** May you do good and not evil.
242930** May you find forgiveness for yourself and forgive others.
242931** May you share freely, never taking more than you give.
242932**
242933******************************************************************************
242934**
242935** This is an SQLite module implementing full-text search.
242936*/
242937
242938
242939/* #include "fts5Int.h" */
242940
242941/*
242942** This variable is set to false when running tests for which the on disk
242943** structures should not be corrupt. Otherwise, true. If it is false, extra
242944** assert() conditions in the fts5 code are activated - conditions that are
242945** only true if it is guaranteed that the fts5 database is not corrupt.
242946*/
242947#ifdef SQLITE_DEBUG
242948SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
242949#endif
242950
242951
242952typedef struct Fts5Auxdata Fts5Auxdata;
242953typedef struct Fts5Auxiliary Fts5Auxiliary;
242954typedef struct Fts5Cursor Fts5Cursor;
242955typedef struct Fts5FullTable Fts5FullTable;
242956typedef struct Fts5Sorter Fts5Sorter;
242957typedef struct Fts5TokenizerModule Fts5TokenizerModule;
242958
242959/*
242960** NOTES ON TRANSACTIONS:
242961**
242962** SQLite invokes the following virtual table methods as transactions are
242963** opened and closed by the user:
242964**
242965** xBegin(): Start of a new transaction.
242966** xSync(): Initial part of two-phase commit.
242967** xCommit(): Final part of two-phase commit.
242968** xRollback(): Rollback the transaction.
242969**
242970** Anything that is required as part of a commit that may fail is performed
242971** in the xSync() callback. Current versions of SQLite ignore any errors
242972** returned by xCommit().
242973**
242974** And as sub-transactions are opened/closed:
242975**
242976** xSavepoint(int S): Open savepoint S.
242977** xRelease(int S): Commit and close savepoint S.
242978** xRollbackTo(int S): Rollback to start of savepoint S.
242979**
242980** During a write-transaction the fts5_index.c module may cache some data
242981** in-memory. It is flushed to disk whenever xSync(), xRelease() or
242982** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
242983** is called.
242984**
242985** Additionally, if SQLITE_DEBUG is defined, an instance of the following
242986** structure is used to record the current transaction state. This information
242987** is not required, but it is used in the assert() statements executed by
242988** function fts5CheckTransactionState() (see below).
242989*/
242990struct Fts5TransactionState {
242991 int eState; /* 0==closed, 1==open, 2==synced */
242992 int iSavepoint; /* Number of open savepoints (0 -> none) */
242993};
242994
242995/*
242996** A single object of this type is allocated when the FTS5 module is
242997** registered with a database handle. It is used to store pointers to
242998** all registered FTS5 extensions - tokenizers and auxiliary functions.
242999*/
243000struct Fts5Global {
243001 fts5_api api; /* User visible part of object (see fts5.h) */
243002 sqlite3 *db; /* Associated database connection */
243003 i64 iNextId; /* Used to allocate unique cursor ids */
243004 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
243005 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
243006 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
243007 Fts5Cursor *pCsr; /* First in list of all open cursors */
243008};
243009
243010/*
243011** Each auxiliary function registered with the FTS5 module is represented
243012** by an object of the following type. All such objects are stored as part
243013** of the Fts5Global.pAux list.
243014*/
243015struct Fts5Auxiliary {
243016 Fts5Global *pGlobal; /* Global context for this function */
243017 char *zFunc; /* Function name (nul-terminated) */
243018 void *pUserData; /* User-data pointer */
243019 fts5_extension_function xFunc; /* Callback function */
243020 void (*xDestroy)(void*); /* Destructor function */
243021 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
243022};
243023
243024/*
243025** Each tokenizer module registered with the FTS5 module is represented
243026** by an object of the following type. All such objects are stored as part
243027** of the Fts5Global.pTok list.
243028*/
243029struct Fts5TokenizerModule {
243030 char *zName; /* Name of tokenizer */
243031 void *pUserData; /* User pointer passed to xCreate() */
243032 fts5_tokenizer x; /* Tokenizer functions */
243033 void (*xDestroy)(void*); /* Destructor function */
243034 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
243035};
243036
243037struct Fts5FullTable {
243038 Fts5Table p; /* Public class members from fts5Int.h */
243039 Fts5Storage *pStorage; /* Document store */
243040 Fts5Global *pGlobal; /* Global (connection wide) data */
243041 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
243042#ifdef SQLITE_DEBUG
243043 struct Fts5TransactionState ts;
243044#endif
243045};
243046
243047struct Fts5MatchPhrase {
243048 Fts5Buffer *pPoslist; /* Pointer to current poslist */
243049 int nTerm; /* Size of phrase in terms */
243050};
243051
243052/*
243053** pStmt:
243054** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
243055**
243056** aIdx[]:
243057** There is one entry in the aIdx[] array for each phrase in the query,
243058** the value of which is the offset within aPoslist[] following the last
243059** byte of the position list for the corresponding phrase.
243060*/
243061struct Fts5Sorter {
243062 sqlite3_stmt *pStmt;
243063 i64 iRowid; /* Current rowid */
243064 const u8 *aPoslist; /* Position lists for current row */
243065 int nIdx; /* Number of entries in aIdx[] */
243066 int aIdx[1]; /* Offsets into aPoslist for current row */
243067};
243068
243069
243070/*
243071** Virtual-table cursor object.
243072**
243073** iSpecial:
243074** If this is a 'special' query (refer to function fts5SpecialMatch()),
243075** then this variable contains the result of the query.
243076**
243077** iFirstRowid, iLastRowid:
243078** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
243079** cursor iterates in ascending order of rowids, iFirstRowid is the lower
243080** limit of rowids to return, and iLastRowid the upper. In other words, the
243081** WHERE clause in the user's query might have been:
243082**
243083** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
243084**
243085** If the cursor iterates in descending order of rowid, iFirstRowid
243086** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
243087** the lower.
243088*/
243089struct Fts5Cursor {
243090 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
243091 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
243092 int *aColumnSize; /* Values for xColumnSize() */
243093 i64 iCsrId; /* Cursor id */
243094
243095 /* Zero from this point onwards on cursor reset */
243096 int ePlan; /* FTS5_PLAN_XXX value */
243097 int bDesc; /* True for "ORDER BY rowid DESC" queries */
243098 i64 iFirstRowid; /* Return no rowids earlier than this */
243099 i64 iLastRowid; /* Return no rowids later than this */
243100 sqlite3_stmt *pStmt; /* Statement used to read %_content */
243101 Fts5Expr *pExpr; /* Expression for MATCH queries */
243102 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
243103 int csrflags; /* Mask of cursor flags (see below) */
243104 i64 iSpecial; /* Result of special query */
243105
243106 /* "rank" function. Populated on demand from vtab.xColumn(). */
243107 char *zRank; /* Custom rank function */
243108 char *zRankArgs; /* Custom rank function args */
243109 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
243110 int nRankArg; /* Number of trailing arguments for rank() */
243111 sqlite3_value **apRankArg; /* Array of trailing arguments */
243112 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
243113
243114 /* Auxiliary data storage */
243115 Fts5Auxiliary *pAux; /* Currently executing extension function */
243116 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
243117
243118 /* Cache used by auxiliary functions xInst() and xInstCount() */
243119 Fts5PoslistReader *aInstIter; /* One for each phrase */
243120 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
243121 int nInstCount; /* Number of phrase instances */
243122 int *aInst; /* 3 integers per phrase instance */
243123};
243124
243125/*
243126** Bits that make up the "idxNum" parameter passed indirectly by
243127** xBestIndex() to xFilter().
243128*/
243129#define FTS5_BI_MATCH 0x0001 /* <tbl> MATCH ? */
243130#define FTS5_BI_RANK 0x0002 /* rank MATCH ? */
243131#define FTS5_BI_ROWID_EQ 0x0004 /* rowid == ? */
243132#define FTS5_BI_ROWID_LE 0x0008 /* rowid <= ? */
243133#define FTS5_BI_ROWID_GE 0x0010 /* rowid >= ? */
243134
243135#define FTS5_BI_ORDER_RANK 0x0020
243136#define FTS5_BI_ORDER_ROWID 0x0040
243137#define FTS5_BI_ORDER_DESC 0x0080
243138
243139/*
243140** Values for Fts5Cursor.csrflags
243141*/
243142#define FTS5CSR_EOF 0x01
243143#define FTS5CSR_REQUIRE_CONTENT 0x02
243144#define FTS5CSR_REQUIRE_DOCSIZE 0x04
243145#define FTS5CSR_REQUIRE_INST 0x08
243146#define FTS5CSR_FREE_ZRANK 0x10
243147#define FTS5CSR_REQUIRE_RESEEK 0x20
243148#define FTS5CSR_REQUIRE_POSLIST 0x40
243149
243150#define BitFlagAllTest(x,y) (((x) & (y))==(y))
243151#define BitFlagTest(x,y) (((x) & (y))!=0)
243152
243153
243154/*
243155** Macros to Set(), Clear() and Test() cursor flags.
243156*/
243157#define CsrFlagSet(pCsr, flag) ((pCsr)->csrflags |= (flag))
243158#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
243159#define CsrFlagTest(pCsr, flag) ((pCsr)->csrflags & (flag))
243160
243161struct Fts5Auxdata {
243162 Fts5Auxiliary *pAux; /* Extension to which this belongs */
243163 void *pPtr; /* Pointer value */
243164 void(*xDelete)(void*); /* Destructor */
243165 Fts5Auxdata *pNext; /* Next object in linked list */
243166};
243167
243168#ifdef SQLITE_DEBUG
243169#define FTS5_BEGIN 1
243170#define FTS5_SYNC 2
243171#define FTS5_COMMIT 3
243172#define FTS5_ROLLBACK 4
243173#define FTS5_SAVEPOINT 5
243174#define FTS5_RELEASE 6
243175#define FTS5_ROLLBACKTO 7
243176static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
243177 switch( op ){
243178 case FTS5_BEGIN:
243179 assert( p->ts.eState==0 );
243180 p->ts.eState = 1;
243181 p->ts.iSavepoint = -1;
243182 break;
243183
243184 case FTS5_SYNC:
243185 assert( p->ts.eState==1 || p->ts.eState==2 );
243186 p->ts.eState = 2;
243187 break;
243188
243189 case FTS5_COMMIT:
243190 assert( p->ts.eState==2 );
243191 p->ts.eState = 0;
243192 break;
243193
243194 case FTS5_ROLLBACK:
243195 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
243196 p->ts.eState = 0;
243197 break;
243198
243199 case FTS5_SAVEPOINT:
243200 assert( p->ts.eState>=1 );
243201 assert( iSavepoint>=0 );
243202 assert( iSavepoint>=p->ts.iSavepoint );
243203 p->ts.iSavepoint = iSavepoint;
243204 break;
243205
243206 case FTS5_RELEASE:
243207 assert( p->ts.eState>=1 );
243208 assert( iSavepoint>=0 );
243209 assert( iSavepoint<=p->ts.iSavepoint );
243210 p->ts.iSavepoint = iSavepoint-1;
243211 break;
243212
243213 case FTS5_ROLLBACKTO:
243214 assert( p->ts.eState>=1 );
243215 assert( iSavepoint>=-1 );
243216 /* The following assert() can fail if another vtab strikes an error
243217 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
243218 ** having called xSavepoint() on this vtab. */
243219 /* assert( iSavepoint<=p->ts.iSavepoint ); */
243220 p->ts.iSavepoint = iSavepoint;
243221 break;
243222 }
243223}
243224#else
243225# define fts5CheckTransactionState(x,y,z)
243226#endif
243227
243228/*
243229** Return true if pTab is a contentless table.
243230*/
243231static int fts5IsContentless(Fts5FullTable *pTab){
243232 return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE;
243233}
243234
243235/*
243236** Delete a virtual table handle allocated by fts5InitVtab().
243237*/
243238static void fts5FreeVtab(Fts5FullTable *pTab){
243239 if( pTab ){
243240 sqlite3Fts5IndexClose(p: pTab->p.pIndex);
243241 sqlite3Fts5StorageClose(p: pTab->pStorage);
243242 sqlite3Fts5ConfigFree(pConfig: pTab->p.pConfig);
243243 sqlite3_free(p: pTab);
243244 }
243245}
243246
243247/*
243248** The xDisconnect() virtual table method.
243249*/
243250static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
243251 fts5FreeVtab(pTab: (Fts5FullTable*)pVtab);
243252 return SQLITE_OK;
243253}
243254
243255/*
243256** The xDestroy() virtual table method.
243257*/
243258static int fts5DestroyMethod(sqlite3_vtab *pVtab){
243259 Fts5Table *pTab = (Fts5Table*)pVtab;
243260 int rc = sqlite3Fts5DropAll(pTab->pConfig);
243261 if( rc==SQLITE_OK ){
243262 fts5FreeVtab(pTab: (Fts5FullTable*)pVtab);
243263 }
243264 return rc;
243265}
243266
243267/*
243268** This function is the implementation of both the xConnect and xCreate
243269** methods of the FTS3 virtual table.
243270**
243271** The argv[] array contains the following:
243272**
243273** argv[0] -> module name ("fts5")
243274** argv[1] -> database name
243275** argv[2] -> table name
243276** argv[...] -> "column name" and other module argument fields.
243277*/
243278static int fts5InitVtab(
243279 int bCreate, /* True for xCreate, false for xConnect */
243280 sqlite3 *db, /* The SQLite database connection */
243281 void *pAux, /* Hash table containing tokenizers */
243282 int argc, /* Number of elements in argv array */
243283 const char * const *argv, /* xCreate/xConnect argument array */
243284 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
243285 char **pzErr /* Write any error message here */
243286){
243287 Fts5Global *pGlobal = (Fts5Global*)pAux;
243288 const char **azConfig = (const char**)argv;
243289 int rc = SQLITE_OK; /* Return code */
243290 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
243291 Fts5FullTable *pTab = 0; /* New virtual table object */
243292
243293 /* Allocate the new vtab object and parse the configuration */
243294 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5FullTable));
243295 if( rc==SQLITE_OK ){
243296 rc = sqlite3Fts5ConfigParse(pGlobal, db, nArg: argc, azArg: azConfig, ppOut: &pConfig, pzErr);
243297 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
243298 }
243299 if( rc==SQLITE_OK ){
243300 pTab->p.pConfig = pConfig;
243301 pTab->pGlobal = pGlobal;
243302 }
243303
243304 /* Open the index sub-system */
243305 if( rc==SQLITE_OK ){
243306 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, pp: &pTab->p.pIndex, pzErr);
243307 }
243308
243309 /* Open the storage sub-system */
243310 if( rc==SQLITE_OK ){
243311 rc = sqlite3Fts5StorageOpen(
243312 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
243313 );
243314 }
243315
243316 /* Call sqlite3_declare_vtab() */
243317 if( rc==SQLITE_OK ){
243318 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
243319 }
243320
243321 /* Load the initial configuration */
243322 if( rc==SQLITE_OK ){
243323 assert( pConfig->pzErrmsg==0 );
243324 pConfig->pzErrmsg = pzErr;
243325 rc = sqlite3Fts5IndexLoadConfig(p: pTab->p.pIndex);
243326 sqlite3Fts5IndexRollback(p: pTab->p.pIndex);
243327 pConfig->pzErrmsg = 0;
243328 }
243329
243330 if( rc!=SQLITE_OK ){
243331 fts5FreeVtab(pTab);
243332 pTab = 0;
243333 }else if( bCreate ){
243334 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
243335 }
243336 *ppVTab = (sqlite3_vtab*)pTab;
243337 return rc;
243338}
243339
243340/*
243341** The xConnect() and xCreate() methods for the virtual table. All the
243342** work is done in function fts5InitVtab().
243343*/
243344static int fts5ConnectMethod(
243345 sqlite3 *db, /* Database connection */
243346 void *pAux, /* Pointer to tokenizer hash table */
243347 int argc, /* Number of elements in argv array */
243348 const char * const *argv, /* xCreate/xConnect argument array */
243349 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
243350 char **pzErr /* OUT: sqlite3_malloc'd error message */
243351){
243352 return fts5InitVtab(bCreate: 0, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
243353}
243354static int fts5CreateMethod(
243355 sqlite3 *db, /* Database connection */
243356 void *pAux, /* Pointer to tokenizer hash table */
243357 int argc, /* Number of elements in argv array */
243358 const char * const *argv, /* xCreate/xConnect argument array */
243359 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
243360 char **pzErr /* OUT: sqlite3_malloc'd error message */
243361){
243362 return fts5InitVtab(bCreate: 1, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
243363}
243364
243365/*
243366** The different query plans.
243367*/
243368#define FTS5_PLAN_MATCH 1 /* (<tbl> MATCH ?) */
243369#define FTS5_PLAN_SOURCE 2 /* A source cursor for SORTED_MATCH */
243370#define FTS5_PLAN_SPECIAL 3 /* An internal query */
243371#define FTS5_PLAN_SORTED_MATCH 4 /* (<tbl> MATCH ? ORDER BY rank) */
243372#define FTS5_PLAN_SCAN 5 /* No usable constraint */
243373#define FTS5_PLAN_ROWID 6 /* (rowid = ?) */
243374
243375/*
243376** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
243377** extension is currently being used by a version of SQLite too old to
243378** support index-info flags. In that case this function is a no-op.
243379*/
243380static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
243381#if SQLITE_VERSION_NUMBER>=3008012
243382#ifndef SQLITE_CORE
243383 if( sqlite3_libversion_number()>=3008012 )
243384#endif
243385 {
243386 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
243387 }
243388#endif
243389}
243390
243391static int fts5UsePatternMatch(
243392 Fts5Config *pConfig,
243393 struct sqlite3_index_constraint *p
243394){
243395 assert( FTS5_PATTERN_GLOB==SQLITE_INDEX_CONSTRAINT_GLOB );
243396 assert( FTS5_PATTERN_LIKE==SQLITE_INDEX_CONSTRAINT_LIKE );
243397 if( pConfig->ePattern==FTS5_PATTERN_GLOB && p->op==FTS5_PATTERN_GLOB ){
243398 return 1;
243399 }
243400 if( pConfig->ePattern==FTS5_PATTERN_LIKE
243401 && (p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB)
243402 ){
243403 return 1;
243404 }
243405 return 0;
243406}
243407
243408/*
243409** Implementation of the xBestIndex method for FTS5 tables. Within the
243410** WHERE constraint, it searches for the following:
243411**
243412** 1. A MATCH constraint against the table column.
243413** 2. A MATCH constraint against the "rank" column.
243414** 3. A MATCH constraint against some other column.
243415** 4. An == constraint against the rowid column.
243416** 5. A < or <= constraint against the rowid column.
243417** 6. A > or >= constraint against the rowid column.
243418**
243419** Within the ORDER BY, the following are supported:
243420**
243421** 5. ORDER BY rank [ASC|DESC]
243422** 6. ORDER BY rowid [ASC|DESC]
243423**
243424** Information for the xFilter call is passed via both the idxNum and
243425** idxStr variables. Specifically, idxNum is a bitmask of the following
243426** flags used to encode the ORDER BY clause:
243427**
243428** FTS5_BI_ORDER_RANK
243429** FTS5_BI_ORDER_ROWID
243430** FTS5_BI_ORDER_DESC
243431**
243432** idxStr is used to encode data from the WHERE clause. For each argument
243433** passed to the xFilter method, the following is appended to idxStr:
243434**
243435** Match against table column: "m"
243436** Match against rank column: "r"
243437** Match against other column: "M<column-number>"
243438** LIKE against other column: "L<column-number>"
243439** GLOB against other column: "G<column-number>"
243440** Equality constraint against the rowid: "="
243441** A < or <= against the rowid: "<"
243442** A > or >= against the rowid: ">"
243443**
243444** This function ensures that there is at most one "r" or "=". And that if
243445** there exists an "=" then there is no "<" or ">".
243446**
243447** Costs are assigned as follows:
243448**
243449** a) If an unusable MATCH operator is present in the WHERE clause, the
243450** cost is unconditionally set to 1e50 (a really big number).
243451**
243452** a) If a MATCH operator is present, the cost depends on the other
243453** constraints also present. As follows:
243454**
243455** * No other constraints: cost=1000.0
243456** * One rowid range constraint: cost=750.0
243457** * Both rowid range constraints: cost=500.0
243458** * An == rowid constraint: cost=100.0
243459**
243460** b) Otherwise, if there is no MATCH:
243461**
243462** * No other constraints: cost=1000000.0
243463** * One rowid range constraint: cost=750000.0
243464** * Both rowid range constraints: cost=250000.0
243465** * An == rowid constraint: cost=10.0
243466**
243467** Costs are not modified by the ORDER BY clause.
243468*/
243469static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
243470 Fts5Table *pTab = (Fts5Table*)pVTab;
243471 Fts5Config *pConfig = pTab->pConfig;
243472 const int nCol = pConfig->nCol;
243473 int idxFlags = 0; /* Parameter passed through to xFilter() */
243474 int i;
243475
243476 char *idxStr;
243477 int iIdxStr = 0;
243478 int iCons = 0;
243479
243480 int bSeenEq = 0;
243481 int bSeenGt = 0;
243482 int bSeenLt = 0;
243483 int bSeenMatch = 0;
243484 int bSeenRank = 0;
243485
243486
243487 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
243488 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
243489 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
243490 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
243491 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
243492
243493 if( pConfig->bLock ){
243494 pTab->base.zErrMsg = sqlite3_mprintf(
243495 zFormat: "recursively defined fts5 content table"
243496 );
243497 return SQLITE_ERROR;
243498 }
243499
243500 idxStr = (char*)sqlite3_malloc(n: pInfo->nConstraint * 8 + 1);
243501 if( idxStr==0 ) return SQLITE_NOMEM;
243502 pInfo->idxStr = idxStr;
243503 pInfo->needToFreeIdxStr = 1;
243504
243505 for(i=0; i<pInfo->nConstraint; i++){
243506 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
243507 int iCol = p->iColumn;
243508 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
243509 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
243510 ){
243511 /* A MATCH operator or equivalent */
243512 if( p->usable==0 || iCol<0 ){
243513 /* As there exists an unusable MATCH constraint this is an
243514 ** unusable plan. Set a prohibitively high cost. */
243515 pInfo->estimatedCost = 1e50;
243516 assert( iIdxStr < pInfo->nConstraint*6 + 1 );
243517 idxStr[iIdxStr] = 0;
243518 return SQLITE_OK;
243519 }else{
243520 if( iCol==nCol+1 ){
243521 if( bSeenRank ) continue;
243522 idxStr[iIdxStr++] = 'r';
243523 bSeenRank = 1;
243524 }else if( iCol>=0 ){
243525 bSeenMatch = 1;
243526 idxStr[iIdxStr++] = 'M';
243527 sqlite3_snprintf(n: 6, zBuf: &idxStr[iIdxStr], zFormat: "%d", iCol);
243528 idxStr += strlen(s: &idxStr[iIdxStr]);
243529 assert( idxStr[iIdxStr]=='\0' );
243530 }
243531 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
243532 pInfo->aConstraintUsage[i].omit = 1;
243533 }
243534 }else if( p->usable ){
243535 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
243536 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB );
243537 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G';
243538 sqlite3_snprintf(n: 6, zBuf: &idxStr[iIdxStr], zFormat: "%d", iCol);
243539 idxStr += strlen(s: &idxStr[iIdxStr]);
243540 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
243541 assert( idxStr[iIdxStr]=='\0' );
243542 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){
243543 idxStr[iIdxStr++] = '=';
243544 bSeenEq = 1;
243545 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
243546 }
243547 }
243548 }
243549
243550 if( bSeenEq==0 ){
243551 for(i=0; i<pInfo->nConstraint; i++){
243552 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
243553 if( p->iColumn<0 && p->usable ){
243554 int op = p->op;
243555 if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
243556 if( bSeenLt ) continue;
243557 idxStr[iIdxStr++] = '<';
243558 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
243559 bSeenLt = 1;
243560 }else
243561 if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
243562 if( bSeenGt ) continue;
243563 idxStr[iIdxStr++] = '>';
243564 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
243565 bSeenGt = 1;
243566 }
243567 }
243568 }
243569 }
243570 idxStr[iIdxStr] = '\0';
243571
243572 /* Set idxFlags flags for the ORDER BY clause */
243573 if( pInfo->nOrderBy==1 ){
243574 int iSort = pInfo->aOrderBy[0].iColumn;
243575 if( iSort==(pConfig->nCol+1) && bSeenMatch ){
243576 idxFlags |= FTS5_BI_ORDER_RANK;
243577 }else if( iSort==-1 ){
243578 idxFlags |= FTS5_BI_ORDER_ROWID;
243579 }
243580 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
243581 pInfo->orderByConsumed = 1;
243582 if( pInfo->aOrderBy[0].desc ){
243583 idxFlags |= FTS5_BI_ORDER_DESC;
243584 }
243585 }
243586 }
243587
243588 /* Calculate the estimated cost based on the flags set in idxFlags. */
243589 if( bSeenEq ){
243590 pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
243591 if( bSeenMatch==0 ) fts5SetUniqueFlag(pIdxInfo: pInfo);
243592 }else if( bSeenLt && bSeenGt ){
243593 pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
243594 }else if( bSeenLt || bSeenGt ){
243595 pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
243596 }else{
243597 pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
243598 }
243599
243600 pInfo->idxNum = idxFlags;
243601 return SQLITE_OK;
243602}
243603
243604static int fts5NewTransaction(Fts5FullTable *pTab){
243605 Fts5Cursor *pCsr;
243606 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
243607 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
243608 }
243609 return sqlite3Fts5StorageReset(p: pTab->pStorage);
243610}
243611
243612/*
243613** Implementation of xOpen method.
243614*/
243615static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
243616 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
243617 Fts5Config *pConfig = pTab->p.pConfig;
243618 Fts5Cursor *pCsr = 0; /* New cursor object */
243619 sqlite3_int64 nByte; /* Bytes of space to allocate */
243620 int rc; /* Return code */
243621
243622 rc = fts5NewTransaction(pTab);
243623 if( rc==SQLITE_OK ){
243624 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
243625 pCsr = (Fts5Cursor*)sqlite3_malloc64(n: nByte);
243626 if( pCsr ){
243627 Fts5Global *pGlobal = pTab->pGlobal;
243628 memset(s: pCsr, c: 0, n: (size_t)nByte);
243629 pCsr->aColumnSize = (int*)&pCsr[1];
243630 pCsr->pNext = pGlobal->pCsr;
243631 pGlobal->pCsr = pCsr;
243632 pCsr->iCsrId = ++pGlobal->iNextId;
243633 }else{
243634 rc = SQLITE_NOMEM;
243635 }
243636 }
243637 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
243638 return rc;
243639}
243640
243641static int fts5StmtType(Fts5Cursor *pCsr){
243642 if( pCsr->ePlan==FTS5_PLAN_SCAN ){
243643 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
243644 }
243645 return FTS5_STMT_LOOKUP;
243646}
243647
243648/*
243649** This function is called after the cursor passed as the only argument
243650** is moved to point at a different row. It clears all cached data
243651** specific to the previous row stored by the cursor object.
243652*/
243653static void fts5CsrNewrow(Fts5Cursor *pCsr){
243654 CsrFlagSet(pCsr,
243655 FTS5CSR_REQUIRE_CONTENT
243656 | FTS5CSR_REQUIRE_DOCSIZE
243657 | FTS5CSR_REQUIRE_INST
243658 | FTS5CSR_REQUIRE_POSLIST
243659 );
243660}
243661
243662static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
243663 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
243664 Fts5Auxdata *pData;
243665 Fts5Auxdata *pNext;
243666
243667 sqlite3_free(p: pCsr->aInstIter);
243668 sqlite3_free(p: pCsr->aInst);
243669 if( pCsr->pStmt ){
243670 int eStmt = fts5StmtType(pCsr);
243671 sqlite3Fts5StorageStmtRelease(p: pTab->pStorage, eStmt, pCsr->pStmt);
243672 }
243673 if( pCsr->pSorter ){
243674 Fts5Sorter *pSorter = pCsr->pSorter;
243675 sqlite3_finalize(pStmt: pSorter->pStmt);
243676 sqlite3_free(p: pSorter);
243677 }
243678
243679 if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
243680 sqlite3Fts5ExprFree(p: pCsr->pExpr);
243681 }
243682
243683 for(pData=pCsr->pAuxdata; pData; pData=pNext){
243684 pNext = pData->pNext;
243685 if( pData->xDelete ) pData->xDelete(pData->pPtr);
243686 sqlite3_free(p: pData);
243687 }
243688
243689 sqlite3_finalize(pStmt: pCsr->pRankArgStmt);
243690 sqlite3_free(p: pCsr->apRankArg);
243691
243692 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
243693 sqlite3_free(p: pCsr->zRank);
243694 sqlite3_free(p: pCsr->zRankArgs);
243695 }
243696
243697 sqlite3Fts5IndexCloseReader(p: pTab->p.pIndex);
243698 memset(s: &pCsr->ePlan, c: 0, n: sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
243699}
243700
243701
243702/*
243703** Close the cursor. For additional information see the documentation
243704** on the xClose method of the virtual table interface.
243705*/
243706static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
243707 if( pCursor ){
243708 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
243709 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
243710 Fts5Cursor **pp;
243711
243712 fts5FreeCursorComponents(pCsr);
243713 /* Remove the cursor from the Fts5Global.pCsr list */
243714 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
243715 *pp = pCsr->pNext;
243716
243717 sqlite3_free(p: pCsr);
243718 }
243719 return SQLITE_OK;
243720}
243721
243722static int fts5SorterNext(Fts5Cursor *pCsr){
243723 Fts5Sorter *pSorter = pCsr->pSorter;
243724 int rc;
243725
243726 rc = sqlite3_step(pStmt: pSorter->pStmt);
243727 if( rc==SQLITE_DONE ){
243728 rc = SQLITE_OK;
243729 CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT);
243730 }else if( rc==SQLITE_ROW ){
243731 const u8 *a;
243732 const u8 *aBlob;
243733 int nBlob;
243734 int i;
243735 int iOff = 0;
243736 rc = SQLITE_OK;
243737
243738 pSorter->iRowid = sqlite3_column_int64(pStmt: pSorter->pStmt, i: 0);
243739 nBlob = sqlite3_column_bytes(pStmt: pSorter->pStmt, i: 1);
243740 aBlob = a = sqlite3_column_blob(pStmt: pSorter->pStmt, i: 1);
243741
243742 /* nBlob==0 in detail=none mode. */
243743 if( nBlob>0 ){
243744 for(i=0; i<(pSorter->nIdx-1); i++){
243745 int iVal;
243746 a += fts5GetVarint32(a, iVal);
243747 iOff += iVal;
243748 pSorter->aIdx[i] = iOff;
243749 }
243750 pSorter->aIdx[i] = &aBlob[nBlob] - a;
243751 pSorter->aPoslist = a;
243752 }
243753
243754 fts5CsrNewrow(pCsr);
243755 }
243756
243757 return rc;
243758}
243759
243760
243761/*
243762** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
243763** open on table pTab.
243764*/
243765static void fts5TripCursors(Fts5FullTable *pTab){
243766 Fts5Cursor *pCsr;
243767 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
243768 if( pCsr->ePlan==FTS5_PLAN_MATCH
243769 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
243770 ){
243771 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
243772 }
243773 }
243774}
243775
243776/*
243777** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
243778** argument, close and reopen all Fts5IndexIter iterators that the cursor
243779** is using. Then attempt to move the cursor to a rowid equal to or laster
243780** (in the cursors sort order - ASC or DESC) than the current rowid.
243781**
243782** If the new rowid is not equal to the old, set output parameter *pbSkip
243783** to 1 before returning. Otherwise, leave it unchanged.
243784**
243785** Return SQLITE_OK if successful or if no reseek was required, or an
243786** error code if an error occurred.
243787*/
243788static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
243789 int rc = SQLITE_OK;
243790 assert( *pbSkip==0 );
243791 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
243792 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
243793 int bDesc = pCsr->bDesc;
243794 i64 iRowid = sqlite3Fts5ExprRowid(p: pCsr->pExpr);
243795
243796 rc = sqlite3Fts5ExprFirst(p: pCsr->pExpr, pIdx: pTab->p.pIndex, iFirst: iRowid, bDesc);
243797 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(p: pCsr->pExpr) ){
243798 *pbSkip = 1;
243799 }
243800
243801 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
243802 fts5CsrNewrow(pCsr);
243803 if( sqlite3Fts5ExprEof(p: pCsr->pExpr) ){
243804 CsrFlagSet(pCsr, FTS5CSR_EOF);
243805 *pbSkip = 1;
243806 }
243807 }
243808 return rc;
243809}
243810
243811
243812/*
243813** Advance the cursor to the next row in the table that matches the
243814** search criteria.
243815**
243816** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
243817** even if we reach end-of-file. The fts5EofMethod() will be called
243818** subsequently to determine whether or not an EOF was hit.
243819*/
243820static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
243821 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
243822 int rc;
243823
243824 assert( (pCsr->ePlan<3)==
243825 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
243826 );
243827 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
243828
243829 if( pCsr->ePlan<3 ){
243830 int bSkip = 0;
243831 if( (rc = fts5CursorReseek(pCsr, pbSkip: &bSkip)) || bSkip ) return rc;
243832 rc = sqlite3Fts5ExprNext(p: pCsr->pExpr, iLast: pCsr->iLastRowid);
243833 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
243834 fts5CsrNewrow(pCsr);
243835 }else{
243836 switch( pCsr->ePlan ){
243837 case FTS5_PLAN_SPECIAL: {
243838 CsrFlagSet(pCsr, FTS5CSR_EOF);
243839 rc = SQLITE_OK;
243840 break;
243841 }
243842
243843 case FTS5_PLAN_SORTED_MATCH: {
243844 rc = fts5SorterNext(pCsr);
243845 break;
243846 }
243847
243848 default: {
243849 Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
243850 pConfig->bLock++;
243851 rc = sqlite3_step(pStmt: pCsr->pStmt);
243852 pConfig->bLock--;
243853 if( rc!=SQLITE_ROW ){
243854 CsrFlagSet(pCsr, FTS5CSR_EOF);
243855 rc = sqlite3_reset(pStmt: pCsr->pStmt);
243856 if( rc!=SQLITE_OK ){
243857 pCursor->pVtab->zErrMsg = sqlite3_mprintf(
243858 zFormat: "%s", sqlite3_errmsg(db: pConfig->db)
243859 );
243860 }
243861 }else{
243862 rc = SQLITE_OK;
243863 }
243864 break;
243865 }
243866 }
243867 }
243868
243869 return rc;
243870}
243871
243872
243873static int fts5PrepareStatement(
243874 sqlite3_stmt **ppStmt,
243875 Fts5Config *pConfig,
243876 const char *zFmt,
243877 ...
243878){
243879 sqlite3_stmt *pRet = 0;
243880 int rc;
243881 char *zSql;
243882 va_list ap;
243883
243884 va_start(ap, zFmt);
243885 zSql = sqlite3_vmprintf(zFormat: zFmt, ap);
243886 if( zSql==0 ){
243887 rc = SQLITE_NOMEM;
243888 }else{
243889 rc = sqlite3_prepare_v3(db: pConfig->db, zSql, nBytes: -1,
243890 SQLITE_PREPARE_PERSISTENT, ppStmt: &pRet, pzTail: 0);
243891 if( rc!=SQLITE_OK ){
243892 *pConfig->pzErrmsg = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db: pConfig->db));
243893 }
243894 sqlite3_free(p: zSql);
243895 }
243896
243897 va_end(ap);
243898 *ppStmt = pRet;
243899 return rc;
243900}
243901
243902static int fts5CursorFirstSorted(
243903 Fts5FullTable *pTab,
243904 Fts5Cursor *pCsr,
243905 int bDesc
243906){
243907 Fts5Config *pConfig = pTab->p.pConfig;
243908 Fts5Sorter *pSorter;
243909 int nPhrase;
243910 sqlite3_int64 nByte;
243911 int rc;
243912 const char *zRank = pCsr->zRank;
243913 const char *zRankArgs = pCsr->zRankArgs;
243914
243915 nPhrase = sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
243916 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
243917 pSorter = (Fts5Sorter*)sqlite3_malloc64(n: nByte);
243918 if( pSorter==0 ) return SQLITE_NOMEM;
243919 memset(s: pSorter, c: 0, n: (size_t)nByte);
243920 pSorter->nIdx = nPhrase;
243921
243922 /* TODO: It would be better to have some system for reusing statement
243923 ** handles here, rather than preparing a new one for each query. But that
243924 ** is not possible as SQLite reference counts the virtual table objects.
243925 ** And since the statement required here reads from this very virtual
243926 ** table, saving it creates a circular reference.
243927 **
243928 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
243929 rc = fts5PrepareStatement(ppStmt: &pSorter->pStmt, pConfig,
243930 zFmt: "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
243931 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
243932 (zRankArgs ? ", " : ""),
243933 (zRankArgs ? zRankArgs : ""),
243934 bDesc ? "DESC" : "ASC"
243935 );
243936
243937 pCsr->pSorter = pSorter;
243938 if( rc==SQLITE_OK ){
243939 assert( pTab->pSortCsr==0 );
243940 pTab->pSortCsr = pCsr;
243941 rc = fts5SorterNext(pCsr);
243942 pTab->pSortCsr = 0;
243943 }
243944
243945 if( rc!=SQLITE_OK ){
243946 sqlite3_finalize(pStmt: pSorter->pStmt);
243947 sqlite3_free(p: pSorter);
243948 pCsr->pSorter = 0;
243949 }
243950
243951 return rc;
243952}
243953
243954static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
243955 int rc;
243956 Fts5Expr *pExpr = pCsr->pExpr;
243957 rc = sqlite3Fts5ExprFirst(p: pExpr, pIdx: pTab->p.pIndex, iFirst: pCsr->iFirstRowid, bDesc);
243958 if( sqlite3Fts5ExprEof(p: pExpr) ){
243959 CsrFlagSet(pCsr, FTS5CSR_EOF);
243960 }
243961 fts5CsrNewrow(pCsr);
243962 return rc;
243963}
243964
243965/*
243966** Process a "special" query. A special query is identified as one with a
243967** MATCH expression that begins with a '*' character. The remainder of
243968** the text passed to the MATCH operator are used as the special query
243969** parameters.
243970*/
243971static int fts5SpecialMatch(
243972 Fts5FullTable *pTab,
243973 Fts5Cursor *pCsr,
243974 const char *zQuery
243975){
243976 int rc = SQLITE_OK; /* Return code */
243977 const char *z = zQuery; /* Special query text */
243978 int n; /* Number of bytes in text at z */
243979
243980 while( z[0]==' ' ) z++;
243981 for(n=0; z[n] && z[n]!=' '; n++);
243982
243983 assert( pTab->p.base.zErrMsg==0 );
243984 pCsr->ePlan = FTS5_PLAN_SPECIAL;
243985
243986 if( n==5 && 0==sqlite3_strnicmp(zLeft: "reads", zRight: z, N: n) ){
243987 pCsr->iSpecial = sqlite3Fts5IndexReads(p: pTab->p.pIndex);
243988 }
243989 else if( n==2 && 0==sqlite3_strnicmp(zLeft: "id", zRight: z, N: n) ){
243990 pCsr->iSpecial = pCsr->iCsrId;
243991 }
243992 else{
243993 /* An unrecognized directive. Return an error message. */
243994 pTab->p.base.zErrMsg = sqlite3_mprintf(zFormat: "unknown special query: %.*s", n, z);
243995 rc = SQLITE_ERROR;
243996 }
243997
243998 return rc;
243999}
244000
244001/*
244002** Search for an auxiliary function named zName that can be used with table
244003** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
244004** structure. Otherwise, if no such function exists, return NULL.
244005*/
244006static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
244007 Fts5Auxiliary *pAux;
244008
244009 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
244010 if( sqlite3_stricmp(zLeft: zName, zRight: pAux->zFunc)==0 ) return pAux;
244011 }
244012
244013 /* No function of the specified name was found. Return 0. */
244014 return 0;
244015}
244016
244017
244018static int fts5FindRankFunction(Fts5Cursor *pCsr){
244019 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
244020 Fts5Config *pConfig = pTab->p.pConfig;
244021 int rc = SQLITE_OK;
244022 Fts5Auxiliary *pAux = 0;
244023 const char *zRank = pCsr->zRank;
244024 const char *zRankArgs = pCsr->zRankArgs;
244025
244026 if( zRankArgs ){
244027 char *zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "SELECT %s", zRankArgs);
244028 if( zSql ){
244029 sqlite3_stmt *pStmt = 0;
244030 rc = sqlite3_prepare_v3(db: pConfig->db, zSql, nBytes: -1,
244031 SQLITE_PREPARE_PERSISTENT, ppStmt: &pStmt, pzTail: 0);
244032 sqlite3_free(p: zSql);
244033 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
244034 if( rc==SQLITE_OK ){
244035 if( SQLITE_ROW==sqlite3_step(pStmt) ){
244036 sqlite3_int64 nByte;
244037 pCsr->nRankArg = sqlite3_column_count(pStmt);
244038 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
244039 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(pRc: &rc, nByte);
244040 if( rc==SQLITE_OK ){
244041 int i;
244042 for(i=0; i<pCsr->nRankArg; i++){
244043 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
244044 }
244045 }
244046 pCsr->pRankArgStmt = pStmt;
244047 }else{
244048 rc = sqlite3_finalize(pStmt);
244049 assert( rc!=SQLITE_OK );
244050 }
244051 }
244052 }
244053 }
244054
244055 if( rc==SQLITE_OK ){
244056 pAux = fts5FindAuxiliary(pTab, zName: zRank);
244057 if( pAux==0 ){
244058 assert( pTab->p.base.zErrMsg==0 );
244059 pTab->p.base.zErrMsg = sqlite3_mprintf(zFormat: "no such function: %s", zRank);
244060 rc = SQLITE_ERROR;
244061 }
244062 }
244063
244064 pCsr->pRank = pAux;
244065 return rc;
244066}
244067
244068
244069static int fts5CursorParseRank(
244070 Fts5Config *pConfig,
244071 Fts5Cursor *pCsr,
244072 sqlite3_value *pRank
244073){
244074 int rc = SQLITE_OK;
244075 if( pRank ){
244076 const char *z = (const char*)sqlite3_value_text(pVal: pRank);
244077 char *zRank = 0;
244078 char *zRankArgs = 0;
244079
244080 if( z==0 ){
244081 if( sqlite3_value_type(pVal: pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
244082 }else{
244083 rc = sqlite3Fts5ConfigParseRank(zIn: z, pzRank: &zRank, pzRankArgs: &zRankArgs);
244084 }
244085 if( rc==SQLITE_OK ){
244086 pCsr->zRank = zRank;
244087 pCsr->zRankArgs = zRankArgs;
244088 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
244089 }else if( rc==SQLITE_ERROR ){
244090 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
244091 zFormat: "parse error in rank function: %s", z
244092 );
244093 }
244094 }else{
244095 if( pConfig->zRank ){
244096 pCsr->zRank = (char*)pConfig->zRank;
244097 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
244098 }else{
244099 pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
244100 pCsr->zRankArgs = 0;
244101 }
244102 }
244103 return rc;
244104}
244105
244106static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
244107 if( pVal ){
244108 int eType = sqlite3_value_numeric_type(pVal);
244109 if( eType==SQLITE_INTEGER ){
244110 return sqlite3_value_int64(pVal);
244111 }
244112 }
244113 return iDefault;
244114}
244115
244116/*
244117** This is the xFilter interface for the virtual table. See
244118** the virtual table xFilter method documentation for additional
244119** information.
244120**
244121** There are three possible query strategies:
244122**
244123** 1. Full-text search using a MATCH operator.
244124** 2. A by-rowid lookup.
244125** 3. A full-table scan.
244126*/
244127static int fts5FilterMethod(
244128 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
244129 int idxNum, /* Strategy index */
244130 const char *idxStr, /* Unused */
244131 int nVal, /* Number of elements in apVal */
244132 sqlite3_value **apVal /* Arguments for the indexing scheme */
244133){
244134 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
244135 Fts5Config *pConfig = pTab->p.pConfig;
244136 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
244137 int rc = SQLITE_OK; /* Error code */
244138 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
244139 int bOrderByRank; /* True if ORDER BY rank */
244140 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
244141 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
244142 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
244143 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
244144 int iCol; /* Column on LHS of MATCH operator */
244145 char **pzErrmsg = pConfig->pzErrmsg;
244146 int i;
244147 int iIdxStr = 0;
244148 Fts5Expr *pExpr = 0;
244149
244150 if( pConfig->bLock ){
244151 pTab->p.base.zErrMsg = sqlite3_mprintf(
244152 zFormat: "recursively defined fts5 content table"
244153 );
244154 return SQLITE_ERROR;
244155 }
244156
244157 if( pCsr->ePlan ){
244158 fts5FreeCursorComponents(pCsr);
244159 memset(s: &pCsr->ePlan, c: 0, n: sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
244160 }
244161
244162 assert( pCsr->pStmt==0 );
244163 assert( pCsr->pExpr==0 );
244164 assert( pCsr->csrflags==0 );
244165 assert( pCsr->pRank==0 );
244166 assert( pCsr->zRank==0 );
244167 assert( pCsr->zRankArgs==0 );
244168 assert( pTab->pSortCsr==0 || nVal==0 );
244169
244170 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
244171 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
244172
244173 /* Decode the arguments passed through to this function. */
244174 for(i=0; i<nVal; i++){
244175 switch( idxStr[iIdxStr++] ){
244176 case 'r':
244177 pRank = apVal[i];
244178 break;
244179 case 'M': {
244180 const char *zText = (const char*)sqlite3_value_text(pVal: apVal[i]);
244181 if( zText==0 ) zText = "";
244182 iCol = 0;
244183 do{
244184 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
244185 iIdxStr++;
244186 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
244187
244188 if( zText[0]=='*' ){
244189 /* The user has issued a query of the form "MATCH '*...'". This
244190 ** indicates that the MATCH expression is not a full text query,
244191 ** but a request for an internal parameter. */
244192 rc = fts5SpecialMatch(pTab, pCsr, zQuery: &zText[1]);
244193 goto filter_out;
244194 }else{
244195 char **pzErr = &pTab->p.base.zErrMsg;
244196 rc = sqlite3Fts5ExprNew(pConfig, bPhraseToAnd: 0, iCol, zExpr: zText, ppNew: &pExpr, pzErr);
244197 if( rc==SQLITE_OK ){
244198 rc = sqlite3Fts5ExprAnd(pp1: &pCsr->pExpr, p2: pExpr);
244199 pExpr = 0;
244200 }
244201 if( rc!=SQLITE_OK ) goto filter_out;
244202 }
244203
244204 break;
244205 }
244206 case 'L':
244207 case 'G': {
244208 int bGlob = (idxStr[iIdxStr-1]=='G');
244209 const char *zText = (const char*)sqlite3_value_text(pVal: apVal[i]);
244210 iCol = 0;
244211 do{
244212 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
244213 iIdxStr++;
244214 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
244215 if( zText ){
244216 rc = sqlite3Fts5ExprPattern(pConfig, bGlob, iCol, zText, pp: &pExpr);
244217 }
244218 if( rc==SQLITE_OK ){
244219 rc = sqlite3Fts5ExprAnd(pp1: &pCsr->pExpr, p2: pExpr);
244220 pExpr = 0;
244221 }
244222 if( rc!=SQLITE_OK ) goto filter_out;
244223 break;
244224 }
244225 case '=':
244226 pRowidEq = apVal[i];
244227 break;
244228 case '<':
244229 pRowidLe = apVal[i];
244230 break;
244231 default: assert( idxStr[iIdxStr-1]=='>' );
244232 pRowidGe = apVal[i];
244233 break;
244234 }
244235 }
244236 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
244237 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
244238
244239 /* Set the cursor upper and lower rowid limits. Only some strategies
244240 ** actually use them. This is ok, as the xBestIndex() method leaves the
244241 ** sqlite3_index_constraint.omit flag clear for range constraints
244242 ** on the rowid field. */
244243 if( pRowidEq ){
244244 pRowidLe = pRowidGe = pRowidEq;
244245 }
244246 if( bDesc ){
244247 pCsr->iFirstRowid = fts5GetRowidLimit(pVal: pRowidLe, LARGEST_INT64);
244248 pCsr->iLastRowid = fts5GetRowidLimit(pVal: pRowidGe, SMALLEST_INT64);
244249 }else{
244250 pCsr->iLastRowid = fts5GetRowidLimit(pVal: pRowidLe, LARGEST_INT64);
244251 pCsr->iFirstRowid = fts5GetRowidLimit(pVal: pRowidGe, SMALLEST_INT64);
244252 }
244253
244254 if( pTab->pSortCsr ){
244255 /* If pSortCsr is non-NULL, then this call is being made as part of
244256 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
244257 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
244258 ** return results to the user for this query. The current cursor
244259 ** (pCursor) is used to execute the query issued by function
244260 ** fts5CursorFirstSorted() above. */
244261 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
244262 assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
244263 assert( pCsr->iLastRowid==LARGEST_INT64 );
244264 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
244265 if( pTab->pSortCsr->bDesc ){
244266 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
244267 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
244268 }else{
244269 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
244270 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
244271 }
244272 pCsr->ePlan = FTS5_PLAN_SOURCE;
244273 pCsr->pExpr = pTab->pSortCsr->pExpr;
244274 rc = fts5CursorFirst(pTab, pCsr, bDesc);
244275 }else if( pCsr->pExpr ){
244276 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
244277 if( rc==SQLITE_OK ){
244278 if( bOrderByRank ){
244279 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
244280 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
244281 }else{
244282 pCsr->ePlan = FTS5_PLAN_MATCH;
244283 rc = fts5CursorFirst(pTab, pCsr, bDesc);
244284 }
244285 }
244286 }else if( pConfig->zContent==0 ){
244287 *pConfig->pzErrmsg = sqlite3_mprintf(
244288 zFormat: "%s: table does not support scanning", pConfig->zName
244289 );
244290 rc = SQLITE_ERROR;
244291 }else{
244292 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
244293 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
244294 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
244295 rc = sqlite3Fts5StorageStmt(
244296 p: pTab->pStorage, eStmt: fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
244297 );
244298 if( rc==SQLITE_OK ){
244299 if( pRowidEq!=0 ){
244300 assert( pCsr->ePlan==FTS5_PLAN_ROWID );
244301 sqlite3_bind_value(pStmt: pCsr->pStmt, i: 1, pValue: pRowidEq);
244302 }else{
244303 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 1, iValue: pCsr->iFirstRowid);
244304 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 2, iValue: pCsr->iLastRowid);
244305 }
244306 rc = fts5NextMethod(pCursor);
244307 }
244308 }
244309
244310 filter_out:
244311 sqlite3Fts5ExprFree(p: pExpr);
244312 pConfig->pzErrmsg = pzErrmsg;
244313 return rc;
244314}
244315
244316/*
244317** This is the xEof method of the virtual table. SQLite calls this
244318** routine to find out if it has reached the end of a result set.
244319*/
244320static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
244321 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
244322 return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
244323}
244324
244325/*
244326** Return the rowid that the cursor currently points to.
244327*/
244328static i64 fts5CursorRowid(Fts5Cursor *pCsr){
244329 assert( pCsr->ePlan==FTS5_PLAN_MATCH
244330 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
244331 || pCsr->ePlan==FTS5_PLAN_SOURCE
244332 );
244333 if( pCsr->pSorter ){
244334 return pCsr->pSorter->iRowid;
244335 }else{
244336 return sqlite3Fts5ExprRowid(p: pCsr->pExpr);
244337 }
244338}
244339
244340/*
244341** This is the xRowid method. The SQLite core calls this routine to
244342** retrieve the rowid for the current row of the result set. fts5
244343** exposes %_content.rowid as the rowid for the virtual table. The
244344** rowid should be written to *pRowid.
244345*/
244346static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
244347 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
244348 int ePlan = pCsr->ePlan;
244349
244350 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
244351 switch( ePlan ){
244352 case FTS5_PLAN_SPECIAL:
244353 *pRowid = 0;
244354 break;
244355
244356 case FTS5_PLAN_SOURCE:
244357 case FTS5_PLAN_MATCH:
244358 case FTS5_PLAN_SORTED_MATCH:
244359 *pRowid = fts5CursorRowid(pCsr);
244360 break;
244361
244362 default:
244363 *pRowid = sqlite3_column_int64(pStmt: pCsr->pStmt, i: 0);
244364 break;
244365 }
244366
244367 return SQLITE_OK;
244368}
244369
244370/*
244371** If the cursor requires seeking (bSeekRequired flag is set), seek it.
244372** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
244373**
244374** If argument bErrormsg is true and an error occurs, an error message may
244375** be left in sqlite3_vtab.zErrMsg.
244376*/
244377static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
244378 int rc = SQLITE_OK;
244379
244380 /* If the cursor does not yet have a statement handle, obtain one now. */
244381 if( pCsr->pStmt==0 ){
244382 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
244383 int eStmt = fts5StmtType(pCsr);
244384 rc = sqlite3Fts5StorageStmt(
244385 p: pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
244386 );
244387 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 );
244388 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
244389 }
244390
244391 if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
244392 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
244393 assert( pCsr->pExpr );
244394 sqlite3_reset(pStmt: pCsr->pStmt);
244395 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 1, iValue: fts5CursorRowid(pCsr));
244396 pTab->pConfig->bLock++;
244397 rc = sqlite3_step(pStmt: pCsr->pStmt);
244398 pTab->pConfig->bLock--;
244399 if( rc==SQLITE_ROW ){
244400 rc = SQLITE_OK;
244401 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
244402 }else{
244403 rc = sqlite3_reset(pStmt: pCsr->pStmt);
244404 if( rc==SQLITE_OK ){
244405 rc = FTS5_CORRUPT;
244406 }else if( pTab->pConfig->pzErrmsg ){
244407 *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
244408 zFormat: "%s", sqlite3_errmsg(db: pTab->pConfig->db)
244409 );
244410 }
244411 }
244412 }
244413 return rc;
244414}
244415
244416static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
244417 va_list ap; /* ... printf arguments */
244418 va_start(ap, zFormat);
244419 assert( p->p.base.zErrMsg==0 );
244420 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
244421 va_end(ap);
244422}
244423
244424/*
244425** This function is called to handle an FTS INSERT command. In other words,
244426** an INSERT statement of the form:
244427**
244428** INSERT INTO fts(fts) VALUES($pCmd)
244429** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
244430**
244431** Argument pVal is the value assigned to column "fts" by the INSERT
244432** statement. This function returns SQLITE_OK if successful, or an SQLite
244433** error code if an error occurs.
244434**
244435** The commands implemented by this function are documented in the "Special
244436** INSERT Directives" section of the documentation. It should be updated if
244437** more commands are added to this function.
244438*/
244439static int fts5SpecialInsert(
244440 Fts5FullTable *pTab, /* Fts5 table object */
244441 const char *zCmd, /* Text inserted into table-name column */
244442 sqlite3_value *pVal /* Value inserted into rank column */
244443){
244444 Fts5Config *pConfig = pTab->p.pConfig;
244445 int rc = SQLITE_OK;
244446 int bError = 0;
244447
244448 if( 0==sqlite3_stricmp(zLeft: "delete-all", zRight: zCmd) ){
244449 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
244450 fts5SetVtabError(p: pTab,
244451 zFormat: "'delete-all' may only be used with a "
244452 "contentless or external content fts5 table"
244453 );
244454 rc = SQLITE_ERROR;
244455 }else{
244456 rc = sqlite3Fts5StorageDeleteAll(p: pTab->pStorage);
244457 }
244458 }else if( 0==sqlite3_stricmp(zLeft: "rebuild", zRight: zCmd) ){
244459 if( pConfig->eContent==FTS5_CONTENT_NONE ){
244460 fts5SetVtabError(p: pTab,
244461 zFormat: "'rebuild' may not be used with a contentless fts5 table"
244462 );
244463 rc = SQLITE_ERROR;
244464 }else{
244465 rc = sqlite3Fts5StorageRebuild(p: pTab->pStorage);
244466 }
244467 }else if( 0==sqlite3_stricmp(zLeft: "optimize", zRight: zCmd) ){
244468 rc = sqlite3Fts5StorageOptimize(p: pTab->pStorage);
244469 }else if( 0==sqlite3_stricmp(zLeft: "merge", zRight: zCmd) ){
244470 int nMerge = sqlite3_value_int(pVal);
244471 rc = sqlite3Fts5StorageMerge(p: pTab->pStorage, nMerge);
244472 }else if( 0==sqlite3_stricmp(zLeft: "integrity-check", zRight: zCmd) ){
244473 int iArg = sqlite3_value_int(pVal);
244474 rc = sqlite3Fts5StorageIntegrity(p: pTab->pStorage, iArg);
244475#ifdef SQLITE_DEBUG
244476 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
244477 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
244478#endif
244479 }else{
244480 rc = sqlite3Fts5IndexLoadConfig(p: pTab->p.pIndex);
244481 if( rc==SQLITE_OK ){
244482 rc = sqlite3Fts5ConfigSetValue(pConfig: pTab->p.pConfig, zKey: zCmd, pVal, pbBadkey: &bError);
244483 }
244484 if( rc==SQLITE_OK ){
244485 if( bError ){
244486 rc = SQLITE_ERROR;
244487 }else{
244488 rc = sqlite3Fts5StorageConfigValue(p: pTab->pStorage, zCmd, pVal, 0);
244489 }
244490 }
244491 }
244492 return rc;
244493}
244494
244495static int fts5SpecialDelete(
244496 Fts5FullTable *pTab,
244497 sqlite3_value **apVal
244498){
244499 int rc = SQLITE_OK;
244500 int eType1 = sqlite3_value_type(pVal: apVal[1]);
244501 if( eType1==SQLITE_INTEGER ){
244502 sqlite3_int64 iDel = sqlite3_value_int64(pVal: apVal[1]);
244503 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iDel, &apVal[2]);
244504 }
244505 return rc;
244506}
244507
244508static void fts5StorageInsert(
244509 int *pRc,
244510 Fts5FullTable *pTab,
244511 sqlite3_value **apVal,
244512 i64 *piRowid
244513){
244514 int rc = *pRc;
244515 if( rc==SQLITE_OK ){
244516 rc = sqlite3Fts5StorageContentInsert(p: pTab->pStorage, apVal, piRowid);
244517 }
244518 if( rc==SQLITE_OK ){
244519 rc = sqlite3Fts5StorageIndexInsert(p: pTab->pStorage, apVal, *piRowid);
244520 }
244521 *pRc = rc;
244522}
244523
244524/*
244525** This function is the implementation of the xUpdate callback used by
244526** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
244527** inserted, updated or deleted.
244528**
244529** A delete specifies a single argument - the rowid of the row to remove.
244530**
244531** Update and insert operations pass:
244532**
244533** 1. The "old" rowid, or NULL.
244534** 2. The "new" rowid.
244535** 3. Values for each of the nCol matchable columns.
244536** 4. Values for the two hidden columns (<tablename> and "rank").
244537*/
244538static int fts5UpdateMethod(
244539 sqlite3_vtab *pVtab, /* Virtual table handle */
244540 int nArg, /* Size of argument array */
244541 sqlite3_value **apVal, /* Array of arguments */
244542 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
244543){
244544 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
244545 Fts5Config *pConfig = pTab->p.pConfig;
244546 int eType0; /* value_type() of apVal[0] */
244547 int rc = SQLITE_OK; /* Return code */
244548 int bUpdateOrDelete = 0;
244549
244550 /* A transaction must be open when this is called. */
244551 assert( pTab->ts.eState==1 || pTab->ts.eState==2 );
244552
244553 assert( pVtab->zErrMsg==0 );
244554 assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
244555 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER
244556 || sqlite3_value_type(apVal[0])==SQLITE_NULL
244557 );
244558 assert( pTab->p.pConfig->pzErrmsg==0 );
244559 if( pConfig->pgsz==0 ){
244560 rc = sqlite3Fts5IndexLoadConfig(p: pTab->p.pIndex);
244561 if( rc!=SQLITE_OK ) return rc;
244562 }
244563
244564 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
244565
244566 /* Put any active cursors into REQUIRE_SEEK state. */
244567 fts5TripCursors(pTab);
244568
244569 eType0 = sqlite3_value_type(pVal: apVal[0]);
244570 if( eType0==SQLITE_NULL
244571 && sqlite3_value_type(pVal: apVal[2+pConfig->nCol])!=SQLITE_NULL
244572 ){
244573 /* A "special" INSERT op. These are handled separately. */
244574 const char *z = (const char*)sqlite3_value_text(pVal: apVal[2+pConfig->nCol]);
244575 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
244576 && 0==sqlite3_stricmp(zLeft: "delete", zRight: z)
244577 ){
244578 if( pConfig->bContentlessDelete ){
244579 fts5SetVtabError(p: pTab,
244580 zFormat: "'delete' may not be used with a contentless_delete=1 table"
244581 );
244582 rc = SQLITE_ERROR;
244583 }else{
244584 rc = fts5SpecialDelete(pTab, apVal);
244585 }
244586 }else{
244587 rc = fts5SpecialInsert(pTab, zCmd: z, pVal: apVal[2 + pConfig->nCol + 1]);
244588 }
244589 }else{
244590 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
244591 ** any conflict on the rowid value must be detected before any
244592 ** modifications are made to the database file. There are 4 cases:
244593 **
244594 ** 1) DELETE
244595 ** 2) UPDATE (rowid not modified)
244596 ** 3) UPDATE (rowid modified)
244597 ** 4) INSERT
244598 **
244599 ** Cases 3 and 4 may violate the rowid constraint.
244600 */
244601 int eConflict = SQLITE_ABORT;
244602 if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->bContentlessDelete ){
244603 eConflict = sqlite3_vtab_on_conflict(db: pConfig->db);
244604 }
244605
244606 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
244607 assert( nArg!=1 || eType0==SQLITE_INTEGER );
244608
244609 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
244610 ** This is not suported. Except - DELETE is supported if the CREATE
244611 ** VIRTUAL TABLE statement contained "contentless_delete=1". */
244612 if( eType0==SQLITE_INTEGER
244613 && pConfig->eContent==FTS5_CONTENT_NONE
244614 && pConfig->bContentlessDelete==0
244615 ){
244616 pTab->p.base.zErrMsg = sqlite3_mprintf(
244617 zFormat: "cannot %s contentless fts5 table: %s",
244618 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
244619 );
244620 rc = SQLITE_ERROR;
244621 }
244622
244623 /* DELETE */
244624 else if( nArg==1 ){
244625 i64 iDel = sqlite3_value_int64(pVal: apVal[0]); /* Rowid to delete */
244626 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iDel, 0);
244627 bUpdateOrDelete = 1;
244628 }
244629
244630 /* INSERT or UPDATE */
244631 else{
244632 int eType1 = sqlite3_value_numeric_type(pVal: apVal[1]);
244633
244634 if( eType1!=SQLITE_INTEGER && eType1!=SQLITE_NULL ){
244635 rc = SQLITE_MISMATCH;
244636 }
244637
244638 else if( eType0!=SQLITE_INTEGER ){
244639 /* If this is a REPLACE, first remove the current entry (if any) */
244640 if( eConflict==SQLITE_REPLACE && eType1==SQLITE_INTEGER ){
244641 i64 iNew = sqlite3_value_int64(pVal: apVal[1]); /* Rowid to delete */
244642 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iNew, 0);
244643 bUpdateOrDelete = 1;
244644 }
244645 fts5StorageInsert(pRc: &rc, pTab, apVal, piRowid: pRowid);
244646 }
244647
244648 /* UPDATE */
244649 else{
244650 i64 iOld = sqlite3_value_int64(pVal: apVal[0]); /* Old rowid */
244651 i64 iNew = sqlite3_value_int64(pVal: apVal[1]); /* New rowid */
244652 if( eType1==SQLITE_INTEGER && iOld!=iNew ){
244653 if( eConflict==SQLITE_REPLACE ){
244654 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iOld, 0);
244655 if( rc==SQLITE_OK ){
244656 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iNew, 0);
244657 }
244658 fts5StorageInsert(pRc: &rc, pTab, apVal, piRowid: pRowid);
244659 }else{
244660 rc = sqlite3Fts5StorageContentInsert(p: pTab->pStorage, apVal, pRowid);
244661 if( rc==SQLITE_OK ){
244662 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iOld, 0);
244663 }
244664 if( rc==SQLITE_OK ){
244665 rc = sqlite3Fts5StorageIndexInsert(p: pTab->pStorage, apVal,*pRowid);
244666 }
244667 }
244668 }else{
244669 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iOld, 0);
244670 fts5StorageInsert(pRc: &rc, pTab, apVal, piRowid: pRowid);
244671 }
244672 bUpdateOrDelete = 1;
244673 }
244674 }
244675 }
244676
244677 if( rc==SQLITE_OK
244678 && bUpdateOrDelete
244679 && pConfig->bSecureDelete
244680 && pConfig->iVersion==FTS5_CURRENT_VERSION
244681 ){
244682 rc = sqlite3Fts5StorageConfigValue(
244683 p: pTab->pStorage, "version", 0, FTS5_CURRENT_VERSION_SECUREDELETE
244684 );
244685 if( rc==SQLITE_OK ){
244686 pConfig->iVersion = FTS5_CURRENT_VERSION_SECUREDELETE;
244687 }
244688 }
244689
244690 pTab->p.pConfig->pzErrmsg = 0;
244691 return rc;
244692}
244693
244694/*
244695** Implementation of xSync() method.
244696*/
244697static int fts5SyncMethod(sqlite3_vtab *pVtab){
244698 int rc;
244699 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
244700 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
244701 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
244702 rc = sqlite3Fts5FlushToDisk(&pTab->p);
244703 pTab->p.pConfig->pzErrmsg = 0;
244704 return rc;
244705}
244706
244707/*
244708** Implementation of xBegin() method.
244709*/
244710static int fts5BeginMethod(sqlite3_vtab *pVtab){
244711 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
244712 fts5NewTransaction(pTab: (Fts5FullTable*)pVtab);
244713 return SQLITE_OK;
244714}
244715
244716/*
244717** Implementation of xCommit() method. This is a no-op. The contents of
244718** the pending-terms hash-table have already been flushed into the database
244719** by fts5SyncMethod().
244720*/
244721static int fts5CommitMethod(sqlite3_vtab *pVtab){
244722 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
244723 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
244724 return SQLITE_OK;
244725}
244726
244727/*
244728** Implementation of xRollback(). Discard the contents of the pending-terms
244729** hash-table. Any changes made to the database are reverted by SQLite.
244730*/
244731static int fts5RollbackMethod(sqlite3_vtab *pVtab){
244732 int rc;
244733 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
244734 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
244735 rc = sqlite3Fts5StorageRollback(p: pTab->pStorage);
244736 return rc;
244737}
244738
244739static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
244740
244741static void *fts5ApiUserData(Fts5Context *pCtx){
244742 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244743 return pCsr->pAux->pUserData;
244744}
244745
244746static int fts5ApiColumnCount(Fts5Context *pCtx){
244747 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244748 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
244749}
244750
244751static int fts5ApiColumnTotalSize(
244752 Fts5Context *pCtx,
244753 int iCol,
244754 sqlite3_int64 *pnToken
244755){
244756 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244757 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
244758 return sqlite3Fts5StorageSize(p: pTab->pStorage, iCol, pnAvg: pnToken);
244759}
244760
244761static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
244762 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244763 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
244764 return sqlite3Fts5StorageRowCount(p: pTab->pStorage, pnRow);
244765}
244766
244767static int fts5ApiTokenize(
244768 Fts5Context *pCtx,
244769 const char *pText, int nText,
244770 void *pUserData,
244771 int (*xToken)(void*, int, const char*, int, int, int)
244772){
244773 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244774 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
244775 return sqlite3Fts5Tokenize(
244776 pConfig: pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pCtx: pUserData, xToken
244777 );
244778}
244779
244780static int fts5ApiPhraseCount(Fts5Context *pCtx){
244781 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244782 return sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
244783}
244784
244785static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
244786 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244787 return sqlite3Fts5ExprPhraseSize(pExpr: pCsr->pExpr, iPhrase);
244788}
244789
244790static int fts5ApiColumnText(
244791 Fts5Context *pCtx,
244792 int iCol,
244793 const char **pz,
244794 int *pn
244795){
244796 int rc = SQLITE_OK;
244797 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244798 if( fts5IsContentless(pTab: (Fts5FullTable*)(pCsr->base.pVtab))
244799 || pCsr->ePlan==FTS5_PLAN_SPECIAL
244800 ){
244801 *pz = 0;
244802 *pn = 0;
244803 }else{
244804 rc = fts5SeekCursor(pCsr, bErrormsg: 0);
244805 if( rc==SQLITE_OK ){
244806 *pz = (const char*)sqlite3_column_text(pStmt: pCsr->pStmt, i: iCol+1);
244807 *pn = sqlite3_column_bytes(pStmt: pCsr->pStmt, i: iCol+1);
244808 }
244809 }
244810 return rc;
244811}
244812
244813static int fts5CsrPoslist(
244814 Fts5Cursor *pCsr,
244815 int iPhrase,
244816 const u8 **pa,
244817 int *pn
244818){
244819 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
244820 int rc = SQLITE_OK;
244821 int bLive = (pCsr->pSorter==0);
244822
244823 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
244824
244825 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
244826 Fts5PoslistPopulator *aPopulator;
244827 int i;
244828 aPopulator = sqlite3Fts5ExprClearPoslists(pExpr: pCsr->pExpr, bLive);
244829 if( aPopulator==0 ) rc = SQLITE_NOMEM;
244830 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
244831 int n; const char *z;
244832 rc = fts5ApiColumnText(pCtx: (Fts5Context*)pCsr, iCol: i, pz: &z, pn: &n);
244833 if( rc==SQLITE_OK ){
244834 rc = sqlite3Fts5ExprPopulatePoslists(
244835 pConfig, pExpr: pCsr->pExpr, aPopulator, iCol: i, z, n
244836 );
244837 }
244838 }
244839 sqlite3_free(p: aPopulator);
244840
244841 if( pCsr->pSorter ){
244842 sqlite3Fts5ExprCheckPoslists(pExpr: pCsr->pExpr, iRowid: pCsr->pSorter->iRowid);
244843 }
244844 }
244845 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
244846 }
244847
244848 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
244849 Fts5Sorter *pSorter = pCsr->pSorter;
244850 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
244851 *pn = pSorter->aIdx[iPhrase] - i1;
244852 *pa = &pSorter->aPoslist[i1];
244853 }else{
244854 *pn = sqlite3Fts5ExprPoslist(pExpr: pCsr->pExpr, iPhrase, pa);
244855 }
244856
244857 return rc;
244858}
244859
244860/*
244861** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
244862** correctly for the current view. Return SQLITE_OK if successful, or an
244863** SQLite error code otherwise.
244864*/
244865static int fts5CacheInstArray(Fts5Cursor *pCsr){
244866 int rc = SQLITE_OK;
244867 Fts5PoslistReader *aIter; /* One iterator for each phrase */
244868 int nIter; /* Number of iterators/phrases */
244869 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
244870
244871 nIter = sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
244872 if( pCsr->aInstIter==0 ){
244873 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
244874 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
244875 }
244876 aIter = pCsr->aInstIter;
244877
244878 if( aIter ){
244879 int nInst = 0; /* Number instances seen so far */
244880 int i;
244881
244882 /* Initialize all iterators */
244883 for(i=0; i<nIter && rc==SQLITE_OK; i++){
244884 const u8 *a;
244885 int n;
244886 rc = fts5CsrPoslist(pCsr, iPhrase: i, pa: &a, pn: &n);
244887 if( rc==SQLITE_OK ){
244888 sqlite3Fts5PoslistReaderInit(a, n, pIter: &aIter[i]);
244889 }
244890 }
244891
244892 if( rc==SQLITE_OK ){
244893 while( 1 ){
244894 int *aInst;
244895 int iBest = -1;
244896 for(i=0; i<nIter; i++){
244897 if( (aIter[i].bEof==0)
244898 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
244899 ){
244900 iBest = i;
244901 }
244902 }
244903 if( iBest<0 ) break;
244904
244905 nInst++;
244906 if( nInst>=pCsr->nInstAlloc ){
244907 int nNewSize = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
244908 aInst = (int*)sqlite3_realloc64(
244909 pOld: pCsr->aInst, n: nNewSize*sizeof(int)*3
244910 );
244911 if( aInst ){
244912 pCsr->aInst = aInst;
244913 pCsr->nInstAlloc = nNewSize;
244914 }else{
244915 nInst--;
244916 rc = SQLITE_NOMEM;
244917 break;
244918 }
244919 }
244920
244921 aInst = &pCsr->aInst[3 * (nInst-1)];
244922 aInst[0] = iBest;
244923 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
244924 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
244925 if( aInst[1]<0 || aInst[1]>=nCol ){
244926 rc = FTS5_CORRUPT;
244927 break;
244928 }
244929 sqlite3Fts5PoslistReaderNext(pIter: &aIter[iBest]);
244930 }
244931 }
244932
244933 pCsr->nInstCount = nInst;
244934 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
244935 }
244936 return rc;
244937}
244938
244939static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
244940 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244941 int rc = SQLITE_OK;
244942 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
244943 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
244944 *pnInst = pCsr->nInstCount;
244945 }
244946 return rc;
244947}
244948
244949static int fts5ApiInst(
244950 Fts5Context *pCtx,
244951 int iIdx,
244952 int *piPhrase,
244953 int *piCol,
244954 int *piOff
244955){
244956 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
244957 int rc = SQLITE_OK;
244958 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
244959 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr))
244960 ){
244961 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
244962 rc = SQLITE_RANGE;
244963#if 0
244964 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
244965 *piPhrase = pCsr->aInst[iIdx*3];
244966 *piCol = pCsr->aInst[iIdx*3 + 2];
244967 *piOff = -1;
244968#endif
244969 }else{
244970 *piPhrase = pCsr->aInst[iIdx*3];
244971 *piCol = pCsr->aInst[iIdx*3 + 1];
244972 *piOff = pCsr->aInst[iIdx*3 + 2];
244973 }
244974 }
244975 return rc;
244976}
244977
244978static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
244979 return fts5CursorRowid(pCsr: (Fts5Cursor*)pCtx);
244980}
244981
244982static int fts5ColumnSizeCb(
244983 void *pContext, /* Pointer to int */
244984 int tflags,
244985 const char *pUnused, /* Buffer containing token */
244986 int nUnused, /* Size of token in bytes */
244987 int iUnused1, /* Start offset of token */
244988 int iUnused2 /* End offset of token */
244989){
244990 int *pCnt = (int*)pContext;
244991 UNUSED_PARAM2(pUnused, nUnused);
244992 UNUSED_PARAM2(iUnused1, iUnused2);
244993 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
244994 (*pCnt)++;
244995 }
244996 return SQLITE_OK;
244997}
244998
244999static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
245000 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245001 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
245002 Fts5Config *pConfig = pTab->p.pConfig;
245003 int rc = SQLITE_OK;
245004
245005 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
245006 if( pConfig->bColumnsize ){
245007 i64 iRowid = fts5CursorRowid(pCsr);
245008 rc = sqlite3Fts5StorageDocsize(p: pTab->pStorage, iRowid, aCol: pCsr->aColumnSize);
245009 }else if( pConfig->zContent==0 ){
245010 int i;
245011 for(i=0; i<pConfig->nCol; i++){
245012 if( pConfig->abUnindexed[i]==0 ){
245013 pCsr->aColumnSize[i] = -1;
245014 }
245015 }
245016 }else{
245017 int i;
245018 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
245019 if( pConfig->abUnindexed[i]==0 ){
245020 const char *z; int n;
245021 void *p = (void*)(&pCsr->aColumnSize[i]);
245022 pCsr->aColumnSize[i] = 0;
245023 rc = fts5ApiColumnText(pCtx, iCol: i, pz: &z, pn: &n);
245024 if( rc==SQLITE_OK ){
245025 rc = sqlite3Fts5Tokenize(
245026 pConfig, FTS5_TOKENIZE_AUX, pText: z, nText: n, pCtx: p, xToken: fts5ColumnSizeCb
245027 );
245028 }
245029 }
245030 }
245031 }
245032 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
245033 }
245034 if( iCol<0 ){
245035 int i;
245036 *pnToken = 0;
245037 for(i=0; i<pConfig->nCol; i++){
245038 *pnToken += pCsr->aColumnSize[i];
245039 }
245040 }else if( iCol<pConfig->nCol ){
245041 *pnToken = pCsr->aColumnSize[iCol];
245042 }else{
245043 *pnToken = 0;
245044 rc = SQLITE_RANGE;
245045 }
245046 return rc;
245047}
245048
245049/*
245050** Implementation of the xSetAuxdata() method.
245051*/
245052static int fts5ApiSetAuxdata(
245053 Fts5Context *pCtx, /* Fts5 context */
245054 void *pPtr, /* Pointer to save as auxdata */
245055 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
245056){
245057 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245058 Fts5Auxdata *pData;
245059
245060 /* Search through the cursors list of Fts5Auxdata objects for one that
245061 ** corresponds to the currently executing auxiliary function. */
245062 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
245063 if( pData->pAux==pCsr->pAux ) break;
245064 }
245065
245066 if( pData ){
245067 if( pData->xDelete ){
245068 pData->xDelete(pData->pPtr);
245069 }
245070 }else{
245071 int rc = SQLITE_OK;
245072 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Auxdata));
245073 if( pData==0 ){
245074 if( xDelete ) xDelete(pPtr);
245075 return rc;
245076 }
245077 pData->pAux = pCsr->pAux;
245078 pData->pNext = pCsr->pAuxdata;
245079 pCsr->pAuxdata = pData;
245080 }
245081
245082 pData->xDelete = xDelete;
245083 pData->pPtr = pPtr;
245084 return SQLITE_OK;
245085}
245086
245087static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
245088 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245089 Fts5Auxdata *pData;
245090 void *pRet = 0;
245091
245092 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
245093 if( pData->pAux==pCsr->pAux ) break;
245094 }
245095
245096 if( pData ){
245097 pRet = pData->pPtr;
245098 if( bClear ){
245099 pData->pPtr = 0;
245100 pData->xDelete = 0;
245101 }
245102 }
245103
245104 return pRet;
245105}
245106
245107static void fts5ApiPhraseNext(
245108 Fts5Context *pUnused,
245109 Fts5PhraseIter *pIter,
245110 int *piCol, int *piOff
245111){
245112 UNUSED_PARAM(pUnused);
245113 if( pIter->a>=pIter->b ){
245114 *piCol = -1;
245115 *piOff = -1;
245116 }else{
245117 int iVal;
245118 pIter->a += fts5GetVarint32(pIter->a, iVal);
245119 if( iVal==1 ){
245120 pIter->a += fts5GetVarint32(pIter->a, iVal);
245121 *piCol = iVal;
245122 *piOff = 0;
245123 pIter->a += fts5GetVarint32(pIter->a, iVal);
245124 }
245125 *piOff += (iVal-2);
245126 }
245127}
245128
245129static int fts5ApiPhraseFirst(
245130 Fts5Context *pCtx,
245131 int iPhrase,
245132 Fts5PhraseIter *pIter,
245133 int *piCol, int *piOff
245134){
245135 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245136 int n;
245137 int rc = fts5CsrPoslist(pCsr, iPhrase, pa: &pIter->a, pn: &n);
245138 if( rc==SQLITE_OK ){
245139 assert( pIter->a || n==0 );
245140 pIter->b = (pIter->a ? &pIter->a[n] : 0);
245141 *piCol = 0;
245142 *piOff = 0;
245143 fts5ApiPhraseNext(pUnused: pCtx, pIter, piCol, piOff);
245144 }
245145 return rc;
245146}
245147
245148static void fts5ApiPhraseNextColumn(
245149 Fts5Context *pCtx,
245150 Fts5PhraseIter *pIter,
245151 int *piCol
245152){
245153 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245154 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
245155
245156 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
245157 if( pIter->a>=pIter->b ){
245158 *piCol = -1;
245159 }else{
245160 int iIncr;
245161 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
245162 *piCol += (iIncr-2);
245163 }
245164 }else{
245165 while( 1 ){
245166 int dummy;
245167 if( pIter->a>=pIter->b ){
245168 *piCol = -1;
245169 return;
245170 }
245171 if( pIter->a[0]==0x01 ) break;
245172 pIter->a += fts5GetVarint32(pIter->a, dummy);
245173 }
245174 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
245175 }
245176}
245177
245178static int fts5ApiPhraseFirstColumn(
245179 Fts5Context *pCtx,
245180 int iPhrase,
245181 Fts5PhraseIter *pIter,
245182 int *piCol
245183){
245184 int rc = SQLITE_OK;
245185 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245186 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
245187
245188 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
245189 Fts5Sorter *pSorter = pCsr->pSorter;
245190 int n;
245191 if( pSorter ){
245192 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
245193 n = pSorter->aIdx[iPhrase] - i1;
245194 pIter->a = &pSorter->aPoslist[i1];
245195 }else{
245196 rc = sqlite3Fts5ExprPhraseCollist(pExpr: pCsr->pExpr, iPhrase, ppCollist: &pIter->a, pnCollist: &n);
245197 }
245198 if( rc==SQLITE_OK ){
245199 assert( pIter->a || n==0 );
245200 pIter->b = (pIter->a ? &pIter->a[n] : 0);
245201 *piCol = 0;
245202 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
245203 }
245204 }else{
245205 int n;
245206 rc = fts5CsrPoslist(pCsr, iPhrase, pa: &pIter->a, pn: &n);
245207 if( rc==SQLITE_OK ){
245208 assert( pIter->a || n==0 );
245209 pIter->b = (pIter->a ? &pIter->a[n] : 0);
245210 if( n<=0 ){
245211 *piCol = -1;
245212 }else if( pIter->a[0]==0x01 ){
245213 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
245214 }else{
245215 *piCol = 0;
245216 }
245217 }
245218 }
245219
245220 return rc;
245221}
245222
245223
245224static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
245225 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
245226);
245227
245228static const Fts5ExtensionApi sFts5Api = {
245229 2, /* iVersion */
245230 fts5ApiUserData,
245231 fts5ApiColumnCount,
245232 fts5ApiRowCount,
245233 fts5ApiColumnTotalSize,
245234 fts5ApiTokenize,
245235 fts5ApiPhraseCount,
245236 fts5ApiPhraseSize,
245237 fts5ApiInstCount,
245238 fts5ApiInst,
245239 fts5ApiRowid,
245240 fts5ApiColumnText,
245241 fts5ApiColumnSize,
245242 fts5ApiQueryPhrase,
245243 fts5ApiSetAuxdata,
245244 fts5ApiGetAuxdata,
245245 fts5ApiPhraseFirst,
245246 fts5ApiPhraseNext,
245247 fts5ApiPhraseFirstColumn,
245248 fts5ApiPhraseNextColumn,
245249};
245250
245251/*
245252** Implementation of API function xQueryPhrase().
245253*/
245254static int fts5ApiQueryPhrase(
245255 Fts5Context *pCtx,
245256 int iPhrase,
245257 void *pUserData,
245258 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
245259){
245260 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
245261 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
245262 int rc;
245263 Fts5Cursor *pNew = 0;
245264
245265 rc = fts5OpenMethod(pVTab: pCsr->base.pVtab, ppCsr: (sqlite3_vtab_cursor**)&pNew);
245266 if( rc==SQLITE_OK ){
245267 pNew->ePlan = FTS5_PLAN_MATCH;
245268 pNew->iFirstRowid = SMALLEST_INT64;
245269 pNew->iLastRowid = LARGEST_INT64;
245270 pNew->base.pVtab = (sqlite3_vtab*)pTab;
245271 rc = sqlite3Fts5ExprClonePhrase(pExpr: pCsr->pExpr, iPhrase, ppNew: &pNew->pExpr);
245272 }
245273
245274 if( rc==SQLITE_OK ){
245275 for(rc = fts5CursorFirst(pTab, pCsr: pNew, bDesc: 0);
245276 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
245277 rc = fts5NextMethod(pCursor: (sqlite3_vtab_cursor*)pNew)
245278 ){
245279 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
245280 if( rc!=SQLITE_OK ){
245281 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
245282 break;
245283 }
245284 }
245285 }
245286
245287 fts5CloseMethod(pCursor: (sqlite3_vtab_cursor*)pNew);
245288 return rc;
245289}
245290
245291static void fts5ApiInvoke(
245292 Fts5Auxiliary *pAux,
245293 Fts5Cursor *pCsr,
245294 sqlite3_context *context,
245295 int argc,
245296 sqlite3_value **argv
245297){
245298 assert( pCsr->pAux==0 );
245299 pCsr->pAux = pAux;
245300 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
245301 pCsr->pAux = 0;
245302}
245303
245304static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
245305 Fts5Cursor *pCsr;
245306 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
245307 if( pCsr->iCsrId==iCsrId ) break;
245308 }
245309 return pCsr;
245310}
245311
245312static void fts5ApiCallback(
245313 sqlite3_context *context,
245314 int argc,
245315 sqlite3_value **argv
245316){
245317
245318 Fts5Auxiliary *pAux;
245319 Fts5Cursor *pCsr;
245320 i64 iCsrId;
245321
245322 assert( argc>=1 );
245323 pAux = (Fts5Auxiliary*)sqlite3_user_data(p: context);
245324 iCsrId = sqlite3_value_int64(pVal: argv[0]);
245325
245326 pCsr = fts5CursorFromCsrid(pGlobal: pAux->pGlobal, iCsrId);
245327 if( pCsr==0 || pCsr->ePlan==0 ){
245328 char *zErr = sqlite3_mprintf(zFormat: "no such cursor: %lld", iCsrId);
245329 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
245330 sqlite3_free(p: zErr);
245331 }else{
245332 fts5ApiInvoke(pAux, pCsr, context, argc: argc-1, argv: &argv[1]);
245333 }
245334}
245335
245336
245337/*
245338** Given cursor id iId, return a pointer to the corresponding Fts5Table
245339** object. Or NULL If the cursor id does not exist.
245340*/
245341static Fts5Table *sqlite3Fts5TableFromCsrid(
245342 Fts5Global *pGlobal, /* FTS5 global context for db handle */
245343 i64 iCsrId /* Id of cursor to find */
245344){
245345 Fts5Cursor *pCsr;
245346 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
245347 if( pCsr ){
245348 return (Fts5Table*)pCsr->base.pVtab;
245349 }
245350 return 0;
245351}
245352
245353/*
245354** Return a "position-list blob" corresponding to the current position of
245355** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
245356** the current position-list for each phrase in the query associated with
245357** cursor pCsr.
245358**
245359** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
245360** the number of phrases in the query. Following the varints are the
245361** concatenated position lists for each phrase, in order.
245362**
245363** The first varint (if it exists) contains the size of the position list
245364** for phrase 0. The second (same disclaimer) contains the size of position
245365** list 1. And so on. There is no size field for the final position list,
245366** as it can be derived from the total size of the blob.
245367*/
245368static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
245369 int i;
245370 int rc = SQLITE_OK;
245371 int nPhrase = sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
245372 Fts5Buffer val;
245373
245374 memset(s: &val, c: 0, n: sizeof(Fts5Buffer));
245375 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
245376 case FTS5_DETAIL_FULL:
245377
245378 /* Append the varints */
245379 for(i=0; i<(nPhrase-1); i++){
245380 const u8 *dummy;
245381 int nByte = sqlite3Fts5ExprPoslist(pExpr: pCsr->pExpr, iPhrase: i, pa: &dummy);
245382 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &val, iVal: nByte);
245383 }
245384
245385 /* Append the position lists */
245386 for(i=0; i<nPhrase; i++){
245387 const u8 *pPoslist;
245388 int nPoslist;
245389 nPoslist = sqlite3Fts5ExprPoslist(pExpr: pCsr->pExpr, iPhrase: i, pa: &pPoslist);
245390 sqlite3Fts5BufferAppendBlob(pRc: &rc, pBuf: &val, nData: nPoslist, pData: pPoslist);
245391 }
245392 break;
245393
245394 case FTS5_DETAIL_COLUMNS:
245395
245396 /* Append the varints */
245397 for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
245398 const u8 *dummy;
245399 int nByte;
245400 rc = sqlite3Fts5ExprPhraseCollist(pExpr: pCsr->pExpr, iPhrase: i, ppCollist: &dummy, pnCollist: &nByte);
245401 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &val, iVal: nByte);
245402 }
245403
245404 /* Append the position lists */
245405 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
245406 const u8 *pPoslist;
245407 int nPoslist;
245408 rc = sqlite3Fts5ExprPhraseCollist(pExpr: pCsr->pExpr, iPhrase: i, ppCollist: &pPoslist, pnCollist: &nPoslist);
245409 sqlite3Fts5BufferAppendBlob(pRc: &rc, pBuf: &val, nData: nPoslist, pData: pPoslist);
245410 }
245411 break;
245412
245413 default:
245414 break;
245415 }
245416
245417 sqlite3_result_blob(pCtx, z: val.p, n: val.n, xDel: sqlite3_free);
245418 return rc;
245419}
245420
245421/*
245422** This is the xColumn method, called by SQLite to request a value from
245423** the row that the supplied cursor currently points to.
245424*/
245425static int fts5ColumnMethod(
245426 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
245427 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
245428 int iCol /* Index of column to read value from */
245429){
245430 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
245431 Fts5Config *pConfig = pTab->p.pConfig;
245432 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
245433 int rc = SQLITE_OK;
245434
245435 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
245436
245437 if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
245438 if( iCol==pConfig->nCol ){
245439 sqlite3_result_int64(pCtx, iVal: pCsr->iSpecial);
245440 }
245441 }else
245442
245443 if( iCol==pConfig->nCol ){
245444 /* User is requesting the value of the special column with the same name
245445 ** as the table. Return the cursor integer id number. This value is only
245446 ** useful in that it may be passed as the first argument to an FTS5
245447 ** auxiliary function. */
245448 sqlite3_result_int64(pCtx, iVal: pCsr->iCsrId);
245449 }else if( iCol==pConfig->nCol+1 ){
245450
245451 /* The value of the "rank" column. */
245452 if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
245453 fts5PoslistBlob(pCtx, pCsr);
245454 }else if(
245455 pCsr->ePlan==FTS5_PLAN_MATCH
245456 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
245457 ){
245458 if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
245459 fts5ApiInvoke(pAux: pCsr->pRank, pCsr, context: pCtx, argc: pCsr->nRankArg, argv: pCsr->apRankArg);
245460 }
245461 }
245462 }else if( !fts5IsContentless(pTab) ){
245463 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
245464 rc = fts5SeekCursor(pCsr, bErrormsg: 1);
245465 if( rc==SQLITE_OK ){
245466 sqlite3_result_value(pCtx, pValue: sqlite3_column_value(pStmt: pCsr->pStmt, i: iCol+1));
245467 }
245468 pConfig->pzErrmsg = 0;
245469 }else if( pConfig->bContentlessDelete && sqlite3_vtab_nochange(p: pCtx) ){
245470 char *zErr = sqlite3_mprintf(zFormat: "cannot UPDATE a subset of "
245471 "columns on fts5 contentless-delete table: %s", pConfig->zName
245472 );
245473 sqlite3_result_error(pCtx, z: zErr, n: -1);
245474 sqlite3_free(p: zErr);
245475 }
245476 return rc;
245477}
245478
245479
245480/*
245481** This routine implements the xFindFunction method for the FTS3
245482** virtual table.
245483*/
245484static int fts5FindFunctionMethod(
245485 sqlite3_vtab *pVtab, /* Virtual table handle */
245486 int nUnused, /* Number of SQL function arguments */
245487 const char *zName, /* Name of SQL function */
245488 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
245489 void **ppArg /* OUT: User data for *pxFunc */
245490){
245491 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
245492 Fts5Auxiliary *pAux;
245493
245494 UNUSED_PARAM(nUnused);
245495 pAux = fts5FindAuxiliary(pTab, zName);
245496 if( pAux ){
245497 *pxFunc = fts5ApiCallback;
245498 *ppArg = (void*)pAux;
245499 return 1;
245500 }
245501
245502 /* No function of the specified name was found. Return 0. */
245503 return 0;
245504}
245505
245506/*
245507** Implementation of FTS5 xRename method. Rename an fts5 table.
245508*/
245509static int fts5RenameMethod(
245510 sqlite3_vtab *pVtab, /* Virtual table handle */
245511 const char *zName /* New name of table */
245512){
245513 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
245514 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
245515}
245516
245517static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
245518 fts5TripCursors(pTab: (Fts5FullTable*)pTab);
245519 return sqlite3Fts5StorageSync(p: ((Fts5FullTable*)pTab)->pStorage);
245520}
245521
245522/*
245523** The xSavepoint() method.
245524**
245525** Flush the contents of the pending-terms table to disk.
245526*/
245527static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
245528 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
245529 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
245530 return sqlite3Fts5FlushToDisk(pTab: (Fts5Table*)pVtab);
245531}
245532
245533/*
245534** The xRelease() method.
245535**
245536** This is a no-op.
245537*/
245538static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
245539 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
245540 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
245541 return sqlite3Fts5FlushToDisk(pTab: (Fts5Table*)pVtab);
245542}
245543
245544/*
245545** The xRollbackTo() method.
245546**
245547** Discard the contents of the pending terms table.
245548*/
245549static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
245550 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
245551 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
245552 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
245553 fts5TripCursors(pTab);
245554 pTab->p.pConfig->pgsz = 0;
245555 return sqlite3Fts5StorageRollback(p: pTab->pStorage);
245556}
245557
245558/*
245559** Register a new auxiliary function with global context pGlobal.
245560*/
245561static int fts5CreateAux(
245562 fts5_api *pApi, /* Global context (one per db handle) */
245563 const char *zName, /* Name of new function */
245564 void *pUserData, /* User data for aux. function */
245565 fts5_extension_function xFunc, /* Aux. function implementation */
245566 void(*xDestroy)(void*) /* Destructor for pUserData */
245567){
245568 Fts5Global *pGlobal = (Fts5Global*)pApi;
245569 int rc = sqlite3_overload_function(db: pGlobal->db, zName, nArg: -1);
245570 if( rc==SQLITE_OK ){
245571 Fts5Auxiliary *pAux;
245572 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
245573 sqlite3_int64 nByte; /* Bytes of space to allocate */
245574
245575 nName = strlen(s: zName) + 1;
245576 nByte = sizeof(Fts5Auxiliary) + nName;
245577 pAux = (Fts5Auxiliary*)sqlite3_malloc64(n: nByte);
245578 if( pAux ){
245579 memset(s: pAux, c: 0, n: (size_t)nByte);
245580 pAux->zFunc = (char*)&pAux[1];
245581 memcpy(dest: pAux->zFunc, src: zName, n: nName);
245582 pAux->pGlobal = pGlobal;
245583 pAux->pUserData = pUserData;
245584 pAux->xFunc = xFunc;
245585 pAux->xDestroy = xDestroy;
245586 pAux->pNext = pGlobal->pAux;
245587 pGlobal->pAux = pAux;
245588 }else{
245589 rc = SQLITE_NOMEM;
245590 }
245591 }
245592
245593 return rc;
245594}
245595
245596/*
245597** Register a new tokenizer. This is the implementation of the
245598** fts5_api.xCreateTokenizer() method.
245599*/
245600static int fts5CreateTokenizer(
245601 fts5_api *pApi, /* Global context (one per db handle) */
245602 const char *zName, /* Name of new function */
245603 void *pUserData, /* User data for aux. function */
245604 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
245605 void(*xDestroy)(void*) /* Destructor for pUserData */
245606){
245607 Fts5Global *pGlobal = (Fts5Global*)pApi;
245608 Fts5TokenizerModule *pNew;
245609 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
245610 sqlite3_int64 nByte; /* Bytes of space to allocate */
245611 int rc = SQLITE_OK;
245612
245613 nName = strlen(s: zName) + 1;
245614 nByte = sizeof(Fts5TokenizerModule) + nName;
245615 pNew = (Fts5TokenizerModule*)sqlite3_malloc64(n: nByte);
245616 if( pNew ){
245617 memset(s: pNew, c: 0, n: (size_t)nByte);
245618 pNew->zName = (char*)&pNew[1];
245619 memcpy(dest: pNew->zName, src: zName, n: nName);
245620 pNew->pUserData = pUserData;
245621 pNew->x = *pTokenizer;
245622 pNew->xDestroy = xDestroy;
245623 pNew->pNext = pGlobal->pTok;
245624 pGlobal->pTok = pNew;
245625 if( pNew->pNext==0 ){
245626 pGlobal->pDfltTok = pNew;
245627 }
245628 }else{
245629 rc = SQLITE_NOMEM;
245630 }
245631
245632 return rc;
245633}
245634
245635static Fts5TokenizerModule *fts5LocateTokenizer(
245636 Fts5Global *pGlobal,
245637 const char *zName
245638){
245639 Fts5TokenizerModule *pMod = 0;
245640
245641 if( zName==0 ){
245642 pMod = pGlobal->pDfltTok;
245643 }else{
245644 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
245645 if( sqlite3_stricmp(zLeft: zName, zRight: pMod->zName)==0 ) break;
245646 }
245647 }
245648
245649 return pMod;
245650}
245651
245652/*
245653** Find a tokenizer. This is the implementation of the
245654** fts5_api.xFindTokenizer() method.
245655*/
245656static int fts5FindTokenizer(
245657 fts5_api *pApi, /* Global context (one per db handle) */
245658 const char *zName, /* Name of new function */
245659 void **ppUserData,
245660 fts5_tokenizer *pTokenizer /* Populate this object */
245661){
245662 int rc = SQLITE_OK;
245663 Fts5TokenizerModule *pMod;
245664
245665 pMod = fts5LocateTokenizer(pGlobal: (Fts5Global*)pApi, zName);
245666 if( pMod ){
245667 *pTokenizer = pMod->x;
245668 *ppUserData = pMod->pUserData;
245669 }else{
245670 memset(s: pTokenizer, c: 0, n: sizeof(fts5_tokenizer));
245671 rc = SQLITE_ERROR;
245672 }
245673
245674 return rc;
245675}
245676
245677static int sqlite3Fts5GetTokenizer(
245678 Fts5Global *pGlobal,
245679 const char **azArg,
245680 int nArg,
245681 Fts5Config *pConfig,
245682 char **pzErr
245683){
245684 Fts5TokenizerModule *pMod;
245685 int rc = SQLITE_OK;
245686
245687 pMod = fts5LocateTokenizer(pGlobal, zName: nArg==0 ? 0 : azArg[0]);
245688 if( pMod==0 ){
245689 assert( nArg>0 );
245690 rc = SQLITE_ERROR;
245691 *pzErr = sqlite3_mprintf(zFormat: "no such tokenizer: %s", azArg[0]);
245692 }else{
245693 rc = pMod->x.xCreate(
245694 pMod->pUserData, (azArg?&azArg[1]:0), (nArg?nArg-1:0), &pConfig->pTok
245695 );
245696 pConfig->pTokApi = &pMod->x;
245697 if( rc!=SQLITE_OK ){
245698 if( pzErr ) *pzErr = sqlite3_mprintf(zFormat: "error in tokenizer constructor");
245699 }else{
245700 pConfig->ePattern = sqlite3Fts5TokenizerPattern(
245701 xCreate: pMod->x.xCreate, pTok: pConfig->pTok
245702 );
245703 }
245704 }
245705
245706 if( rc!=SQLITE_OK ){
245707 pConfig->pTokApi = 0;
245708 pConfig->pTok = 0;
245709 }
245710
245711 return rc;
245712}
245713
245714static void fts5ModuleDestroy(void *pCtx){
245715 Fts5TokenizerModule *pTok, *pNextTok;
245716 Fts5Auxiliary *pAux, *pNextAux;
245717 Fts5Global *pGlobal = (Fts5Global*)pCtx;
245718
245719 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
245720 pNextAux = pAux->pNext;
245721 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
245722 sqlite3_free(p: pAux);
245723 }
245724
245725 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
245726 pNextTok = pTok->pNext;
245727 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
245728 sqlite3_free(p: pTok);
245729 }
245730
245731 sqlite3_free(p: pGlobal);
245732}
245733
245734static void fts5Fts5Func(
245735 sqlite3_context *pCtx, /* Function call context */
245736 int nArg, /* Number of args */
245737 sqlite3_value **apArg /* Function arguments */
245738){
245739 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(p: pCtx);
245740 fts5_api **ppApi;
245741 UNUSED_PARAM(nArg);
245742 assert( nArg==1 );
245743 ppApi = (fts5_api**)sqlite3_value_pointer(pVal: apArg[0], zPType: "fts5_api_ptr");
245744 if( ppApi ) *ppApi = &pGlobal->api;
245745}
245746
245747/*
245748** Implementation of fts5_source_id() function.
245749*/
245750static void fts5SourceIdFunc(
245751 sqlite3_context *pCtx, /* Function call context */
245752 int nArg, /* Number of args */
245753 sqlite3_value **apUnused /* Function arguments */
245754){
245755 assert( nArg==0 );
245756 UNUSED_PARAM2(nArg, apUnused);
245757 sqlite3_result_text(pCtx, z: "fts5: 2023-09-11 12:01:27 2d3a40c05c49e1a49264912b1a05bc2143ac0e7c3df588276ce80a4cbc9bd1b0", n: -1, SQLITE_TRANSIENT);
245758}
245759
245760/*
245761** Return true if zName is the extension on one of the shadow tables used
245762** by this module.
245763*/
245764static int fts5ShadowName(const char *zName){
245765 static const char *azName[] = {
245766 "config", "content", "data", "docsize", "idx"
245767 };
245768 unsigned int i;
245769 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
245770 if( sqlite3_stricmp(zLeft: zName, zRight: azName[i])==0 ) return 1;
245771 }
245772 return 0;
245773}
245774
245775static int fts5Init(sqlite3 *db){
245776 static const sqlite3_module fts5Mod = {
245777 /* iVersion */ 3,
245778 /* xCreate */ fts5CreateMethod,
245779 /* xConnect */ fts5ConnectMethod,
245780 /* xBestIndex */ fts5BestIndexMethod,
245781 /* xDisconnect */ fts5DisconnectMethod,
245782 /* xDestroy */ fts5DestroyMethod,
245783 /* xOpen */ fts5OpenMethod,
245784 /* xClose */ fts5CloseMethod,
245785 /* xFilter */ fts5FilterMethod,
245786 /* xNext */ fts5NextMethod,
245787 /* xEof */ fts5EofMethod,
245788 /* xColumn */ fts5ColumnMethod,
245789 /* xRowid */ fts5RowidMethod,
245790 /* xUpdate */ fts5UpdateMethod,
245791 /* xBegin */ fts5BeginMethod,
245792 /* xSync */ fts5SyncMethod,
245793 /* xCommit */ fts5CommitMethod,
245794 /* xRollback */ fts5RollbackMethod,
245795 /* xFindFunction */ fts5FindFunctionMethod,
245796 /* xRename */ fts5RenameMethod,
245797 /* xSavepoint */ fts5SavepointMethod,
245798 /* xRelease */ fts5ReleaseMethod,
245799 /* xRollbackTo */ fts5RollbackToMethod,
245800 /* xShadowName */ fts5ShadowName
245801 };
245802
245803 int rc;
245804 Fts5Global *pGlobal = 0;
245805
245806 pGlobal = (Fts5Global*)sqlite3_malloc(n: sizeof(Fts5Global));
245807 if( pGlobal==0 ){
245808 rc = SQLITE_NOMEM;
245809 }else{
245810 void *p = (void*)pGlobal;
245811 memset(s: pGlobal, c: 0, n: sizeof(Fts5Global));
245812 pGlobal->db = db;
245813 pGlobal->api.iVersion = 2;
245814 pGlobal->api.xCreateFunction = fts5CreateAux;
245815 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
245816 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
245817 rc = sqlite3_create_module_v2(db, zName: "fts5", pModule: &fts5Mod, pAux: p, xDestroy: fts5ModuleDestroy);
245818 if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
245819 if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
245820 if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(pApi: &pGlobal->api);
245821 if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
245822 if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
245823 if( rc==SQLITE_OK ){
245824 rc = sqlite3_create_function(
245825 db, zFunc: "fts5", nArg: 1, SQLITE_UTF8, p, xSFunc: fts5Fts5Func, xStep: 0, xFinal: 0
245826 );
245827 }
245828 if( rc==SQLITE_OK ){
245829 rc = sqlite3_create_function(
245830 db, zFunc: "fts5_source_id", nArg: 0,
245831 SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS,
245832 p, xSFunc: fts5SourceIdFunc, xStep: 0, xFinal: 0
245833 );
245834 }
245835 }
245836
245837 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
245838 ** fts5_test_mi.c is compiled and linked into the executable. And call
245839 ** its entry point to enable the matchinfo() demo. */
245840#ifdef SQLITE_FTS5_ENABLE_TEST_MI
245841 if( rc==SQLITE_OK ){
245842 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
245843 rc = sqlite3Fts5TestRegisterMatchinfo(db);
245844 }
245845#endif
245846
245847 return rc;
245848}
245849
245850/*
245851** The following functions are used to register the module with SQLite. If
245852** this module is being built as part of the SQLite core (SQLITE_CORE is
245853** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
245854**
245855** Or, if this module is being built as a loadable extension,
245856** sqlite3Fts5Init() is omitted and the two standard entry points
245857** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
245858*/
245859#ifndef SQLITE_CORE
245860#ifdef _WIN32
245861__declspec(dllexport)
245862#endif
245863SQLITE_API int sqlite3_fts_init(
245864 sqlite3 *db,
245865 char **pzErrMsg,
245866 const sqlite3_api_routines *pApi
245867){
245868 SQLITE_EXTENSION_INIT2(pApi);
245869 (void)pzErrMsg; /* Unused parameter */
245870 return fts5Init(db);
245871}
245872
245873#ifdef _WIN32
245874__declspec(dllexport)
245875#endif
245876SQLITE_API int sqlite3_fts5_init(
245877 sqlite3 *db,
245878 char **pzErrMsg,
245879 const sqlite3_api_routines *pApi
245880){
245881 SQLITE_EXTENSION_INIT2(pApi);
245882 (void)pzErrMsg; /* Unused parameter */
245883 return fts5Init(db);
245884}
245885#else
245886SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
245887 return fts5Init(db);
245888}
245889#endif
245890
245891/*
245892** 2014 May 31
245893**
245894** The author disclaims copyright to this source code. In place of
245895** a legal notice, here is a blessing:
245896**
245897** May you do good and not evil.
245898** May you find forgiveness for yourself and forgive others.
245899** May you share freely, never taking more than you give.
245900**
245901******************************************************************************
245902**
245903*/
245904
245905
245906
245907/* #include "fts5Int.h" */
245908
245909struct Fts5Storage {
245910 Fts5Config *pConfig;
245911 Fts5Index *pIndex;
245912 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
245913 i64 nTotalRow; /* Total number of rows in FTS table */
245914 i64 *aTotalSize; /* Total sizes of each column */
245915 sqlite3_stmt *aStmt[11];
245916};
245917
245918
245919#if FTS5_STMT_SCAN_ASC!=0
245920# error "FTS5_STMT_SCAN_ASC mismatch"
245921#endif
245922#if FTS5_STMT_SCAN_DESC!=1
245923# error "FTS5_STMT_SCAN_DESC mismatch"
245924#endif
245925#if FTS5_STMT_LOOKUP!=2
245926# error "FTS5_STMT_LOOKUP mismatch"
245927#endif
245928
245929#define FTS5_STMT_INSERT_CONTENT 3
245930#define FTS5_STMT_REPLACE_CONTENT 4
245931#define FTS5_STMT_DELETE_CONTENT 5
245932#define FTS5_STMT_REPLACE_DOCSIZE 6
245933#define FTS5_STMT_DELETE_DOCSIZE 7
245934#define FTS5_STMT_LOOKUP_DOCSIZE 8
245935#define FTS5_STMT_REPLACE_CONFIG 9
245936#define FTS5_STMT_SCAN 10
245937
245938/*
245939** Prepare the two insert statements - Fts5Storage.pInsertContent and
245940** Fts5Storage.pInsertDocsize - if they have not already been prepared.
245941** Return SQLITE_OK if successful, or an SQLite error code if an error
245942** occurs.
245943*/
245944static int fts5StorageGetStmt(
245945 Fts5Storage *p, /* Storage handle */
245946 int eStmt, /* FTS5_STMT_XXX constant */
245947 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
245948 char **pzErrMsg /* OUT: Error message (if any) */
245949){
245950 int rc = SQLITE_OK;
245951
245952 /* If there is no %_docsize table, there should be no requests for
245953 ** statements to operate on it. */
245954 assert( p->pConfig->bColumnsize || (
245955 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
245956 && eStmt!=FTS5_STMT_DELETE_DOCSIZE
245957 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
245958 ));
245959
245960 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
245961 if( p->aStmt[eStmt]==0 ){
245962 const char *azStmt[] = {
245963 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
245964 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
245965 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
245966
245967 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
245968 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
245969 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
245970 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?%s)", /* REPLACE_DOCSIZE */
245971 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
245972
245973 "SELECT sz%s FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
245974
245975 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
245976 "SELECT %s FROM %s AS T", /* SCAN */
245977 };
245978 Fts5Config *pC = p->pConfig;
245979 char *zSql = 0;
245980
245981 switch( eStmt ){
245982 case FTS5_STMT_SCAN:
245983 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt],
245984 pC->zContentExprlist, pC->zContent
245985 );
245986 break;
245987
245988 case FTS5_STMT_SCAN_ASC:
245989 case FTS5_STMT_SCAN_DESC:
245990 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zContentExprlist,
245991 pC->zContent, pC->zContentRowid, pC->zContentRowid,
245992 pC->zContentRowid
245993 );
245994 break;
245995
245996 case FTS5_STMT_LOOKUP:
245997 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt],
245998 pC->zContentExprlist, pC->zContent, pC->zContentRowid
245999 );
246000 break;
246001
246002 case FTS5_STMT_INSERT_CONTENT:
246003 case FTS5_STMT_REPLACE_CONTENT: {
246004 int nCol = pC->nCol + 1;
246005 char *zBind;
246006 int i;
246007
246008 zBind = sqlite3_malloc64(n: 1 + nCol*2);
246009 if( zBind ){
246010 for(i=0; i<nCol; i++){
246011 zBind[i*2] = '?';
246012 zBind[i*2 + 1] = ',';
246013 }
246014 zBind[i*2-1] = '\0';
246015 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zDb, pC->zName, zBind);
246016 sqlite3_free(p: zBind);
246017 }
246018 break;
246019 }
246020
246021 case FTS5_STMT_REPLACE_DOCSIZE:
246022 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zDb, pC->zName,
246023 (pC->bContentlessDelete ? ",?" : "")
246024 );
246025 break;
246026
246027 case FTS5_STMT_LOOKUP_DOCSIZE:
246028 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt],
246029 (pC->bContentlessDelete ? ",origin" : ""),
246030 pC->zDb, pC->zName
246031 );
246032 break;
246033
246034 default:
246035 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zDb, pC->zName);
246036 break;
246037 }
246038
246039 if( zSql==0 ){
246040 rc = SQLITE_NOMEM;
246041 }else{
246042 int f = SQLITE_PREPARE_PERSISTENT;
246043 if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
246044 p->pConfig->bLock++;
246045 rc = sqlite3_prepare_v3(db: pC->db, zSql, nBytes: -1, prepFlags: f, ppStmt: &p->aStmt[eStmt], pzTail: 0);
246046 p->pConfig->bLock--;
246047 sqlite3_free(p: zSql);
246048 if( rc!=SQLITE_OK && pzErrMsg ){
246049 *pzErrMsg = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db: pC->db));
246050 }
246051 }
246052 }
246053
246054 *ppStmt = p->aStmt[eStmt];
246055 sqlite3_reset(pStmt: *ppStmt);
246056 return rc;
246057}
246058
246059
246060static int fts5ExecPrintf(
246061 sqlite3 *db,
246062 char **pzErr,
246063 const char *zFormat,
246064 ...
246065){
246066 int rc;
246067 va_list ap; /* ... printf arguments */
246068 char *zSql;
246069
246070 va_start(ap, zFormat);
246071 zSql = sqlite3_vmprintf(zFormat, ap);
246072
246073 if( zSql==0 ){
246074 rc = SQLITE_NOMEM;
246075 }else{
246076 rc = sqlite3_exec(db, zSql, xCallback: 0, pArg: 0, pzErrMsg: pzErr);
246077 sqlite3_free(p: zSql);
246078 }
246079
246080 va_end(ap);
246081 return rc;
246082}
246083
246084/*
246085** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
246086** code otherwise.
246087*/
246088static int sqlite3Fts5DropAll(Fts5Config *pConfig){
246089 int rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
246090 zFormat: "DROP TABLE IF EXISTS %Q.'%q_data';"
246091 "DROP TABLE IF EXISTS %Q.'%q_idx';"
246092 "DROP TABLE IF EXISTS %Q.'%q_config';",
246093 pConfig->zDb, pConfig->zName,
246094 pConfig->zDb, pConfig->zName,
246095 pConfig->zDb, pConfig->zName
246096 );
246097 if( rc==SQLITE_OK && pConfig->bColumnsize ){
246098 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
246099 zFormat: "DROP TABLE IF EXISTS %Q.'%q_docsize';",
246100 pConfig->zDb, pConfig->zName
246101 );
246102 }
246103 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
246104 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
246105 zFormat: "DROP TABLE IF EXISTS %Q.'%q_content';",
246106 pConfig->zDb, pConfig->zName
246107 );
246108 }
246109 return rc;
246110}
246111
246112static void fts5StorageRenameOne(
246113 Fts5Config *pConfig, /* Current FTS5 configuration */
246114 int *pRc, /* IN/OUT: Error code */
246115 const char *zTail, /* Tail of table name e.g. "data", "config" */
246116 const char *zName /* New name of FTS5 table */
246117){
246118 if( *pRc==SQLITE_OK ){
246119 *pRc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
246120 zFormat: "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
246121 pConfig->zDb, pConfig->zName, zTail, zName, zTail
246122 );
246123 }
246124}
246125
246126static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
246127 Fts5Config *pConfig = pStorage->pConfig;
246128 int rc = sqlite3Fts5StorageSync(p: pStorage);
246129
246130 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "data", zName);
246131 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "idx", zName);
246132 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "config", zName);
246133 if( pConfig->bColumnsize ){
246134 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "docsize", zName);
246135 }
246136 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
246137 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "content", zName);
246138 }
246139 return rc;
246140}
246141
246142/*
246143** Create the shadow table named zPost, with definition zDefn. Return
246144** SQLITE_OK if successful, or an SQLite error code otherwise.
246145*/
246146static int sqlite3Fts5CreateTable(
246147 Fts5Config *pConfig, /* FTS5 configuration */
246148 const char *zPost, /* Shadow table to create (e.g. "content") */
246149 const char *zDefn, /* Columns etc. for shadow table */
246150 int bWithout, /* True for without rowid */
246151 char **pzErr /* OUT: Error message */
246152){
246153 int rc;
246154 char *zErr = 0;
246155
246156 rc = fts5ExecPrintf(db: pConfig->db, pzErr: &zErr, zFormat: "CREATE TABLE %Q.'%q_%q'(%s)%s",
246157 pConfig->zDb, pConfig->zName, zPost, zDefn,
246158#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
246159 bWithout?" WITHOUT ROWID":
246160#endif
246161 ""
246162 );
246163 if( zErr ){
246164 *pzErr = sqlite3_mprintf(
246165 zFormat: "fts5: error creating shadow table %q_%s: %s",
246166 pConfig->zName, zPost, zErr
246167 );
246168 sqlite3_free(p: zErr);
246169 }
246170
246171 return rc;
246172}
246173
246174/*
246175** Open a new Fts5Index handle. If the bCreate argument is true, create
246176** and initialize the underlying tables
246177**
246178** If successful, set *pp to point to the new object and return SQLITE_OK.
246179** Otherwise, set *pp to NULL and return an SQLite error code.
246180*/
246181static int sqlite3Fts5StorageOpen(
246182 Fts5Config *pConfig,
246183 Fts5Index *pIndex,
246184 int bCreate,
246185 Fts5Storage **pp,
246186 char **pzErr /* OUT: Error message */
246187){
246188 int rc = SQLITE_OK;
246189 Fts5Storage *p; /* New object */
246190 sqlite3_int64 nByte; /* Bytes of space to allocate */
246191
246192 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
246193 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
246194 *pp = p = (Fts5Storage*)sqlite3_malloc64(n: nByte);
246195 if( !p ) return SQLITE_NOMEM;
246196
246197 memset(s: p, c: 0, n: (size_t)nByte);
246198 p->aTotalSize = (i64*)&p[1];
246199 p->pConfig = pConfig;
246200 p->pIndex = pIndex;
246201
246202 if( bCreate ){
246203 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
246204 int nDefn = 32 + pConfig->nCol*10;
246205 char *zDefn = sqlite3_malloc64(n: 32 + (sqlite3_int64)pConfig->nCol * 10);
246206 if( zDefn==0 ){
246207 rc = SQLITE_NOMEM;
246208 }else{
246209 int i;
246210 int iOff;
246211 sqlite3_snprintf(n: nDefn, zBuf: zDefn, zFormat: "id INTEGER PRIMARY KEY");
246212 iOff = (int)strlen(s: zDefn);
246213 for(i=0; i<pConfig->nCol; i++){
246214 sqlite3_snprintf(n: nDefn-iOff, zBuf: &zDefn[iOff], zFormat: ", c%d", i);
246215 iOff += (int)strlen(s: &zDefn[iOff]);
246216 }
246217 rc = sqlite3Fts5CreateTable(pConfig, zPost: "content", zDefn, bWithout: 0, pzErr);
246218 }
246219 sqlite3_free(p: zDefn);
246220 }
246221
246222 if( rc==SQLITE_OK && pConfig->bColumnsize ){
246223 const char *zCols = "id INTEGER PRIMARY KEY, sz BLOB";
246224 if( pConfig->bContentlessDelete ){
246225 zCols = "id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER";
246226 }
246227 rc = sqlite3Fts5CreateTable(pConfig, zPost: "docsize", zDefn: zCols, bWithout: 0, pzErr);
246228 }
246229 if( rc==SQLITE_OK ){
246230 rc = sqlite3Fts5CreateTable(
246231 pConfig, zPost: "config", zDefn: "k PRIMARY KEY, v", bWithout: 1, pzErr
246232 );
246233 }
246234 if( rc==SQLITE_OK ){
246235 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
246236 }
246237 }
246238
246239 if( rc ){
246240 sqlite3Fts5StorageClose(p);
246241 *pp = 0;
246242 }
246243 return rc;
246244}
246245
246246/*
246247** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
246248*/
246249static int sqlite3Fts5StorageClose(Fts5Storage *p){
246250 int rc = SQLITE_OK;
246251 if( p ){
246252 int i;
246253
246254 /* Finalize all SQL statements */
246255 for(i=0; i<ArraySize(p->aStmt); i++){
246256 sqlite3_finalize(pStmt: p->aStmt[i]);
246257 }
246258
246259 sqlite3_free(p);
246260 }
246261 return rc;
246262}
246263
246264typedef struct Fts5InsertCtx Fts5InsertCtx;
246265struct Fts5InsertCtx {
246266 Fts5Storage *pStorage;
246267 int iCol;
246268 int szCol; /* Size of column value in tokens */
246269};
246270
246271/*
246272** Tokenization callback used when inserting tokens into the FTS index.
246273*/
246274static int fts5StorageInsertCallback(
246275 void *pContext, /* Pointer to Fts5InsertCtx object */
246276 int tflags,
246277 const char *pToken, /* Buffer containing token */
246278 int nToken, /* Size of token in bytes */
246279 int iUnused1, /* Start offset of token */
246280 int iUnused2 /* End offset of token */
246281){
246282 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
246283 Fts5Index *pIdx = pCtx->pStorage->pIndex;
246284 UNUSED_PARAM2(iUnused1, iUnused2);
246285 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
246286 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
246287 pCtx->szCol++;
246288 }
246289 return sqlite3Fts5IndexWrite(p: pIdx, iCol: pCtx->iCol, iPos: pCtx->szCol-1, pToken, nToken);
246290}
246291
246292/*
246293** If a row with rowid iDel is present in the %_content table, add the
246294** delete-markers to the FTS index necessary to delete it. Do not actually
246295** remove the %_content row at this time though.
246296*/
246297static int fts5StorageDeleteFromIndex(
246298 Fts5Storage *p,
246299 i64 iDel,
246300 sqlite3_value **apVal
246301){
246302 Fts5Config *pConfig = p->pConfig;
246303 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
246304 int rc = SQLITE_OK; /* Return code */
246305 int rc2; /* sqlite3_reset() return code */
246306 int iCol;
246307 Fts5InsertCtx ctx;
246308
246309 if( apVal==0 ){
246310 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, ppStmt: &pSeek, pzErrMsg: 0);
246311 if( rc!=SQLITE_OK ) return rc;
246312 sqlite3_bind_int64(pStmt: pSeek, i: 1, iValue: iDel);
246313 if( sqlite3_step(pStmt: pSeek)!=SQLITE_ROW ){
246314 return sqlite3_reset(pStmt: pSeek);
246315 }
246316 }
246317
246318 ctx.pStorage = p;
246319 ctx.iCol = -1;
246320 for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
246321 if( pConfig->abUnindexed[iCol-1]==0 ){
246322 const char *zText;
246323 int nText;
246324 assert( pSeek==0 || apVal==0 );
246325 assert( pSeek!=0 || apVal!=0 );
246326 if( pSeek ){
246327 zText = (const char*)sqlite3_column_text(pStmt: pSeek, i: iCol);
246328 nText = sqlite3_column_bytes(pStmt: pSeek, i: iCol);
246329 }else if( ALWAYS(apVal) ){
246330 zText = (const char*)sqlite3_value_text(pVal: apVal[iCol-1]);
246331 nText = sqlite3_value_bytes(pVal: apVal[iCol-1]);
246332 }else{
246333 continue;
246334 }
246335 ctx.szCol = 0;
246336 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
246337 pText: zText, nText, pCtx: (void*)&ctx, xToken: fts5StorageInsertCallback
246338 );
246339 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
246340 if( p->aTotalSize[iCol-1]<0 ){
246341 rc = FTS5_CORRUPT;
246342 }
246343 }
246344 }
246345 if( rc==SQLITE_OK && p->nTotalRow<1 ){
246346 rc = FTS5_CORRUPT;
246347 }else{
246348 p->nTotalRow--;
246349 }
246350
246351 rc2 = sqlite3_reset(pStmt: pSeek);
246352 if( rc==SQLITE_OK ) rc = rc2;
246353 return rc;
246354}
246355
246356/*
246357** This function is called to process a DELETE on a contentless_delete=1
246358** table. It adds the tombstone required to delete the entry with rowid
246359** iDel. If successful, SQLITE_OK is returned. Or, if an error occurs,
246360** an SQLite error code.
246361*/
246362static int fts5StorageContentlessDelete(Fts5Storage *p, i64 iDel){
246363 i64 iOrigin = 0;
246364 sqlite3_stmt *pLookup = 0;
246365 int rc = SQLITE_OK;
246366
246367 assert( p->pConfig->bContentlessDelete );
246368 assert( p->pConfig->eContent==FTS5_CONTENT_NONE );
246369
246370 /* Look up the origin of the document in the %_docsize table. Store
246371 ** this in stack variable iOrigin. */
246372 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, ppStmt: &pLookup, pzErrMsg: 0);
246373 if( rc==SQLITE_OK ){
246374 sqlite3_bind_int64(pStmt: pLookup, i: 1, iValue: iDel);
246375 if( SQLITE_ROW==sqlite3_step(pStmt: pLookup) ){
246376 iOrigin = sqlite3_column_int64(pStmt: pLookup, i: 1);
246377 }
246378 rc = sqlite3_reset(pStmt: pLookup);
246379 }
246380
246381 if( rc==SQLITE_OK && iOrigin!=0 ){
246382 rc = sqlite3Fts5IndexContentlessDelete(p: p->pIndex, iOrigin, iRowid: iDel);
246383 }
246384
246385 return rc;
246386}
246387
246388/*
246389** Insert a record into the %_docsize table. Specifically, do:
246390**
246391** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
246392**
246393** If there is no %_docsize table (as happens if the columnsize=0 option
246394** is specified when the FTS5 table is created), this function is a no-op.
246395*/
246396static int fts5StorageInsertDocsize(
246397 Fts5Storage *p, /* Storage module to write to */
246398 i64 iRowid, /* id value */
246399 Fts5Buffer *pBuf /* sz value */
246400){
246401 int rc = SQLITE_OK;
246402 if( p->pConfig->bColumnsize ){
246403 sqlite3_stmt *pReplace = 0;
246404 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, ppStmt: &pReplace, pzErrMsg: 0);
246405 if( rc==SQLITE_OK ){
246406 sqlite3_bind_int64(pStmt: pReplace, i: 1, iValue: iRowid);
246407 if( p->pConfig->bContentlessDelete ){
246408 i64 iOrigin = 0;
246409 rc = sqlite3Fts5IndexGetOrigin(p: p->pIndex, piOrigin: &iOrigin);
246410 sqlite3_bind_int64(pStmt: pReplace, i: 3, iValue: iOrigin);
246411 }
246412 if( rc==SQLITE_OK ){
246413 sqlite3_bind_blob(pStmt: pReplace, i: 2, zData: pBuf->p, nData: pBuf->n, SQLITE_STATIC);
246414 sqlite3_step(pStmt: pReplace);
246415 rc = sqlite3_reset(pStmt: pReplace);
246416 sqlite3_bind_null(pStmt: pReplace, i: 2);
246417 }
246418 }
246419 }
246420 return rc;
246421}
246422
246423/*
246424** Load the contents of the "averages" record from disk into the
246425** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
246426** argument bCache is true, set the p->bTotalsValid flag to indicate
246427** that the contents of aTotalSize[] and nTotalRow are valid until
246428** further notice.
246429**
246430** Return SQLITE_OK if successful, or an SQLite error code if an error
246431** occurs.
246432*/
246433static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
246434 int rc = SQLITE_OK;
246435 if( p->bTotalsValid==0 ){
246436 rc = sqlite3Fts5IndexGetAverages(p: p->pIndex, pnRow: &p->nTotalRow, anSize: p->aTotalSize);
246437 p->bTotalsValid = bCache;
246438 }
246439 return rc;
246440}
246441
246442/*
246443** Store the current contents of the p->nTotalRow and p->aTotalSize[]
246444** variables in the "averages" record on disk.
246445**
246446** Return SQLITE_OK if successful, or an SQLite error code if an error
246447** occurs.
246448*/
246449static int fts5StorageSaveTotals(Fts5Storage *p){
246450 int nCol = p->pConfig->nCol;
246451 int i;
246452 Fts5Buffer buf;
246453 int rc = SQLITE_OK;
246454 memset(s: &buf, c: 0, n: sizeof(buf));
246455
246456 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: p->nTotalRow);
246457 for(i=0; i<nCol; i++){
246458 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: p->aTotalSize[i]);
246459 }
246460 if( rc==SQLITE_OK ){
246461 rc = sqlite3Fts5IndexSetAverages(p: p->pIndex, pData: buf.p, nData: buf.n);
246462 }
246463 sqlite3_free(p: buf.p);
246464
246465 return rc;
246466}
246467
246468/*
246469** Remove a row from the FTS table.
246470*/
246471static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
246472 Fts5Config *pConfig = p->pConfig;
246473 int rc;
246474 sqlite3_stmt *pDel = 0;
246475
246476 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
246477 rc = fts5StorageLoadTotals(p, bCache: 1);
246478
246479 /* Delete the index records */
246480 if( rc==SQLITE_OK ){
246481 rc = sqlite3Fts5IndexBeginWrite(p: p->pIndex, bDelete: 1, iRowid: iDel);
246482 }
246483
246484 if( rc==SQLITE_OK ){
246485 if( p->pConfig->bContentlessDelete ){
246486 rc = fts5StorageContentlessDelete(p, iDel);
246487 }else{
246488 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
246489 }
246490 }
246491
246492 /* Delete the %_docsize record */
246493 if( rc==SQLITE_OK && pConfig->bColumnsize ){
246494 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, ppStmt: &pDel, pzErrMsg: 0);
246495 if( rc==SQLITE_OK ){
246496 sqlite3_bind_int64(pStmt: pDel, i: 1, iValue: iDel);
246497 sqlite3_step(pStmt: pDel);
246498 rc = sqlite3_reset(pStmt: pDel);
246499 }
246500 }
246501
246502 /* Delete the %_content record */
246503 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
246504 if( rc==SQLITE_OK ){
246505 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, ppStmt: &pDel, pzErrMsg: 0);
246506 }
246507 if( rc==SQLITE_OK ){
246508 sqlite3_bind_int64(pStmt: pDel, i: 1, iValue: iDel);
246509 sqlite3_step(pStmt: pDel);
246510 rc = sqlite3_reset(pStmt: pDel);
246511 }
246512 }
246513
246514 return rc;
246515}
246516
246517/*
246518** Delete all entries in the FTS5 index.
246519*/
246520static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
246521 Fts5Config *pConfig = p->pConfig;
246522 int rc;
246523
246524 p->bTotalsValid = 0;
246525
246526 /* Delete the contents of the %_data and %_docsize tables. */
246527 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
246528 zFormat: "DELETE FROM %Q.'%q_data';"
246529 "DELETE FROM %Q.'%q_idx';",
246530 pConfig->zDb, pConfig->zName,
246531 pConfig->zDb, pConfig->zName
246532 );
246533 if( rc==SQLITE_OK && pConfig->bColumnsize ){
246534 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
246535 zFormat: "DELETE FROM %Q.'%q_docsize';",
246536 pConfig->zDb, pConfig->zName
246537 );
246538 }
246539
246540 /* Reinitialize the %_data table. This call creates the initial structure
246541 ** and averages records. */
246542 if( rc==SQLITE_OK ){
246543 rc = sqlite3Fts5IndexReinit(p: p->pIndex);
246544 }
246545 if( rc==SQLITE_OK ){
246546 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
246547 }
246548 return rc;
246549}
246550
246551static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
246552 Fts5Buffer buf = {0,0,0};
246553 Fts5Config *pConfig = p->pConfig;
246554 sqlite3_stmt *pScan = 0;
246555 Fts5InsertCtx ctx;
246556 int rc, rc2;
246557
246558 memset(s: &ctx, c: 0, n: sizeof(Fts5InsertCtx));
246559 ctx.pStorage = p;
246560 rc = sqlite3Fts5StorageDeleteAll(p);
246561 if( rc==SQLITE_OK ){
246562 rc = fts5StorageLoadTotals(p, bCache: 1);
246563 }
246564
246565 if( rc==SQLITE_OK ){
246566 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, ppStmt: &pScan, pzErrMsg: 0);
246567 }
246568
246569 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt: pScan) ){
246570 i64 iRowid = sqlite3_column_int64(pStmt: pScan, i: 0);
246571
246572 sqlite3Fts5BufferZero(pBuf: &buf);
246573 rc = sqlite3Fts5IndexBeginWrite(p: p->pIndex, bDelete: 0, iRowid);
246574 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
246575 ctx.szCol = 0;
246576 if( pConfig->abUnindexed[ctx.iCol]==0 ){
246577 const char *zText = (const char*)sqlite3_column_text(pStmt: pScan, i: ctx.iCol+1);
246578 int nText = sqlite3_column_bytes(pStmt: pScan, i: ctx.iCol+1);
246579 rc = sqlite3Fts5Tokenize(pConfig,
246580 FTS5_TOKENIZE_DOCUMENT,
246581 pText: zText, nText,
246582 pCtx: (void*)&ctx,
246583 xToken: fts5StorageInsertCallback
246584 );
246585 }
246586 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: ctx.szCol);
246587 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
246588 }
246589 p->nTotalRow++;
246590
246591 if( rc==SQLITE_OK ){
246592 rc = fts5StorageInsertDocsize(p, iRowid, pBuf: &buf);
246593 }
246594 }
246595 sqlite3_free(p: buf.p);
246596 rc2 = sqlite3_reset(pStmt: pScan);
246597 if( rc==SQLITE_OK ) rc = rc2;
246598
246599 /* Write the averages record */
246600 if( rc==SQLITE_OK ){
246601 rc = fts5StorageSaveTotals(p);
246602 }
246603 return rc;
246604}
246605
246606static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
246607 return sqlite3Fts5IndexOptimize(p: p->pIndex);
246608}
246609
246610static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
246611 return sqlite3Fts5IndexMerge(p: p->pIndex, nMerge);
246612}
246613
246614static int sqlite3Fts5StorageReset(Fts5Storage *p){
246615 return sqlite3Fts5IndexReset(p: p->pIndex);
246616}
246617
246618/*
246619** Allocate a new rowid. This is used for "external content" tables when
246620** a NULL value is inserted into the rowid column. The new rowid is allocated
246621** by inserting a dummy row into the %_docsize table. The dummy will be
246622** overwritten later.
246623**
246624** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
246625** this case the user is required to provide a rowid explicitly.
246626*/
246627static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
246628 int rc = SQLITE_MISMATCH;
246629 if( p->pConfig->bColumnsize ){
246630 sqlite3_stmt *pReplace = 0;
246631 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, ppStmt: &pReplace, pzErrMsg: 0);
246632 if( rc==SQLITE_OK ){
246633 sqlite3_bind_null(pStmt: pReplace, i: 1);
246634 sqlite3_bind_null(pStmt: pReplace, i: 2);
246635 sqlite3_step(pStmt: pReplace);
246636 rc = sqlite3_reset(pStmt: pReplace);
246637 }
246638 if( rc==SQLITE_OK ){
246639 *piRowid = sqlite3_last_insert_rowid(db: p->pConfig->db);
246640 }
246641 }
246642 return rc;
246643}
246644
246645/*
246646** Insert a new row into the FTS content table.
246647*/
246648static int sqlite3Fts5StorageContentInsert(
246649 Fts5Storage *p,
246650 sqlite3_value **apVal,
246651 i64 *piRowid
246652){
246653 Fts5Config *pConfig = p->pConfig;
246654 int rc = SQLITE_OK;
246655
246656 /* Insert the new row into the %_content table. */
246657 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
246658 if( sqlite3_value_type(pVal: apVal[1])==SQLITE_INTEGER ){
246659 *piRowid = sqlite3_value_int64(pVal: apVal[1]);
246660 }else{
246661 rc = fts5StorageNewRowid(p, piRowid);
246662 }
246663 }else{
246664 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
246665 int i; /* Counter variable */
246666 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, ppStmt: &pInsert, pzErrMsg: 0);
246667 for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
246668 rc = sqlite3_bind_value(pStmt: pInsert, i, pValue: apVal[i]);
246669 }
246670 if( rc==SQLITE_OK ){
246671 sqlite3_step(pStmt: pInsert);
246672 rc = sqlite3_reset(pStmt: pInsert);
246673 }
246674 *piRowid = sqlite3_last_insert_rowid(db: pConfig->db);
246675 }
246676
246677 return rc;
246678}
246679
246680/*
246681** Insert new entries into the FTS index and %_docsize table.
246682*/
246683static int sqlite3Fts5StorageIndexInsert(
246684 Fts5Storage *p,
246685 sqlite3_value **apVal,
246686 i64 iRowid
246687){
246688 Fts5Config *pConfig = p->pConfig;
246689 int rc = SQLITE_OK; /* Return code */
246690 Fts5InsertCtx ctx; /* Tokenization callback context object */
246691 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
246692
246693 memset(s: &buf, c: 0, n: sizeof(Fts5Buffer));
246694 ctx.pStorage = p;
246695 rc = fts5StorageLoadTotals(p, bCache: 1);
246696
246697 if( rc==SQLITE_OK ){
246698 rc = sqlite3Fts5IndexBeginWrite(p: p->pIndex, bDelete: 0, iRowid);
246699 }
246700 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
246701 ctx.szCol = 0;
246702 if( pConfig->abUnindexed[ctx.iCol]==0 ){
246703 const char *zText = (const char*)sqlite3_value_text(pVal: apVal[ctx.iCol+2]);
246704 int nText = sqlite3_value_bytes(pVal: apVal[ctx.iCol+2]);
246705 rc = sqlite3Fts5Tokenize(pConfig,
246706 FTS5_TOKENIZE_DOCUMENT,
246707 pText: zText, nText,
246708 pCtx: (void*)&ctx,
246709 xToken: fts5StorageInsertCallback
246710 );
246711 }
246712 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: ctx.szCol);
246713 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
246714 }
246715 p->nTotalRow++;
246716
246717 /* Write the %_docsize record */
246718 if( rc==SQLITE_OK ){
246719 rc = fts5StorageInsertDocsize(p, iRowid, pBuf: &buf);
246720 }
246721 sqlite3_free(p: buf.p);
246722
246723 return rc;
246724}
246725
246726static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
246727 Fts5Config *pConfig = p->pConfig;
246728 char *zSql;
246729 int rc;
246730
246731 zSql = sqlite3_mprintf(zFormat: "SELECT count(*) FROM %Q.'%q_%s'",
246732 pConfig->zDb, pConfig->zName, zSuffix
246733 );
246734 if( zSql==0 ){
246735 rc = SQLITE_NOMEM;
246736 }else{
246737 sqlite3_stmt *pCnt = 0;
246738 rc = sqlite3_prepare_v2(db: pConfig->db, zSql, nBytes: -1, ppStmt: &pCnt, pzTail: 0);
246739 if( rc==SQLITE_OK ){
246740 if( SQLITE_ROW==sqlite3_step(pStmt: pCnt) ){
246741 *pnRow = sqlite3_column_int64(pStmt: pCnt, i: 0);
246742 }
246743 rc = sqlite3_finalize(pStmt: pCnt);
246744 }
246745 }
246746
246747 sqlite3_free(p: zSql);
246748 return rc;
246749}
246750
246751/*
246752** Context object used by sqlite3Fts5StorageIntegrity().
246753*/
246754typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
246755struct Fts5IntegrityCtx {
246756 i64 iRowid;
246757 int iCol;
246758 int szCol;
246759 u64 cksum;
246760 Fts5Termset *pTermset;
246761 Fts5Config *pConfig;
246762};
246763
246764
246765/*
246766** Tokenization callback used by integrity check.
246767*/
246768static int fts5StorageIntegrityCallback(
246769 void *pContext, /* Pointer to Fts5IntegrityCtx object */
246770 int tflags,
246771 const char *pToken, /* Buffer containing token */
246772 int nToken, /* Size of token in bytes */
246773 int iUnused1, /* Start offset of token */
246774 int iUnused2 /* End offset of token */
246775){
246776 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
246777 Fts5Termset *pTermset = pCtx->pTermset;
246778 int bPresent;
246779 int ii;
246780 int rc = SQLITE_OK;
246781 int iPos;
246782 int iCol;
246783
246784 UNUSED_PARAM2(iUnused1, iUnused2);
246785 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
246786
246787 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
246788 pCtx->szCol++;
246789 }
246790
246791 switch( pCtx->pConfig->eDetail ){
246792 case FTS5_DETAIL_FULL:
246793 iPos = pCtx->szCol-1;
246794 iCol = pCtx->iCol;
246795 break;
246796
246797 case FTS5_DETAIL_COLUMNS:
246798 iPos = pCtx->iCol;
246799 iCol = 0;
246800 break;
246801
246802 default:
246803 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
246804 iPos = 0;
246805 iCol = 0;
246806 break;
246807 }
246808
246809 rc = sqlite3Fts5TermsetAdd(p: pTermset, iIdx: 0, pTerm: pToken, nTerm: nToken, pbPresent: &bPresent);
246810 if( rc==SQLITE_OK && bPresent==0 ){
246811 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
246812 iRowid: pCtx->iRowid, iCol, iPos, iIdx: 0, pTerm: pToken, nTerm: nToken
246813 );
246814 }
246815
246816 for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
246817 const int nChar = pCtx->pConfig->aPrefix[ii];
246818 int nByte = sqlite3Fts5IndexCharlenToBytelen(p: pToken, nByte: nToken, nChar);
246819 if( nByte ){
246820 rc = sqlite3Fts5TermsetAdd(p: pTermset, iIdx: ii+1, pTerm: pToken, nTerm: nByte, pbPresent: &bPresent);
246821 if( bPresent==0 ){
246822 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
246823 iRowid: pCtx->iRowid, iCol, iPos, iIdx: ii+1, pTerm: pToken, nTerm: nByte
246824 );
246825 }
246826 }
246827 }
246828
246829 return rc;
246830}
246831
246832/*
246833** Check that the contents of the FTS index match that of the %_content
246834** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
246835** some other SQLite error code if an error occurs while attempting to
246836** determine this.
246837*/
246838static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){
246839 Fts5Config *pConfig = p->pConfig;
246840 int rc = SQLITE_OK; /* Return code */
246841 int *aColSize; /* Array of size pConfig->nCol */
246842 i64 *aTotalSize; /* Array of size pConfig->nCol */
246843 Fts5IntegrityCtx ctx;
246844 sqlite3_stmt *pScan;
246845 int bUseCksum;
246846
246847 memset(s: &ctx, c: 0, n: sizeof(Fts5IntegrityCtx));
246848 ctx.pConfig = p->pConfig;
246849 aTotalSize = (i64*)sqlite3_malloc64(n: pConfig->nCol*(sizeof(int)+sizeof(i64)));
246850 if( !aTotalSize ) return SQLITE_NOMEM;
246851 aColSize = (int*)&aTotalSize[pConfig->nCol];
246852 memset(s: aTotalSize, c: 0, n: sizeof(i64) * pConfig->nCol);
246853
246854 bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL
246855 || (pConfig->eContent==FTS5_CONTENT_EXTERNAL && iArg)
246856 );
246857 if( bUseCksum ){
246858 /* Generate the expected index checksum based on the contents of the
246859 ** %_content table. This block stores the checksum in ctx.cksum. */
246860 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, ppStmt: &pScan, pzErrMsg: 0);
246861 if( rc==SQLITE_OK ){
246862 int rc2;
246863 while( SQLITE_ROW==sqlite3_step(pStmt: pScan) ){
246864 int i;
246865 ctx.iRowid = sqlite3_column_int64(pStmt: pScan, i: 0);
246866 ctx.szCol = 0;
246867 if( pConfig->bColumnsize ){
246868 rc = sqlite3Fts5StorageDocsize(p, iRowid: ctx.iRowid, aCol: aColSize);
246869 }
246870 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
246871 rc = sqlite3Fts5TermsetNew(pp: &ctx.pTermset);
246872 }
246873 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
246874 if( pConfig->abUnindexed[i] ) continue;
246875 ctx.iCol = i;
246876 ctx.szCol = 0;
246877 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
246878 rc = sqlite3Fts5TermsetNew(pp: &ctx.pTermset);
246879 }
246880 if( rc==SQLITE_OK ){
246881 const char *zText = (const char*)sqlite3_column_text(pStmt: pScan, i: i+1);
246882 int nText = sqlite3_column_bytes(pStmt: pScan, i: i+1);
246883 rc = sqlite3Fts5Tokenize(pConfig,
246884 FTS5_TOKENIZE_DOCUMENT,
246885 pText: zText, nText,
246886 pCtx: (void*)&ctx,
246887 xToken: fts5StorageIntegrityCallback
246888 );
246889 }
246890 if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
246891 rc = FTS5_CORRUPT;
246892 }
246893 aTotalSize[i] += ctx.szCol;
246894 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
246895 sqlite3Fts5TermsetFree(p: ctx.pTermset);
246896 ctx.pTermset = 0;
246897 }
246898 }
246899 sqlite3Fts5TermsetFree(p: ctx.pTermset);
246900 ctx.pTermset = 0;
246901
246902 if( rc!=SQLITE_OK ) break;
246903 }
246904 rc2 = sqlite3_reset(pStmt: pScan);
246905 if( rc==SQLITE_OK ) rc = rc2;
246906 }
246907
246908 /* Test that the "totals" (sometimes called "averages") record looks Ok */
246909 if( rc==SQLITE_OK ){
246910 int i;
246911 rc = fts5StorageLoadTotals(p, bCache: 0);
246912 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
246913 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
246914 }
246915 }
246916
246917 /* Check that the %_docsize and %_content tables contain the expected
246918 ** number of rows. */
246919 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
246920 i64 nRow = 0;
246921 rc = fts5StorageCount(p, zSuffix: "content", pnRow: &nRow);
246922 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
246923 }
246924 if( rc==SQLITE_OK && pConfig->bColumnsize ){
246925 i64 nRow = 0;
246926 rc = fts5StorageCount(p, zSuffix: "docsize", pnRow: &nRow);
246927 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
246928 }
246929 }
246930
246931 /* Pass the expected checksum down to the FTS index module. It will
246932 ** verify, amongst other things, that it matches the checksum generated by
246933 ** inspecting the index itself. */
246934 if( rc==SQLITE_OK ){
246935 rc = sqlite3Fts5IndexIntegrityCheck(p: p->pIndex, cksum: ctx.cksum, bUseCksum);
246936 }
246937
246938 sqlite3_free(p: aTotalSize);
246939 return rc;
246940}
246941
246942/*
246943** Obtain an SQLite statement handle that may be used to read data from the
246944** %_content table.
246945*/
246946static int sqlite3Fts5StorageStmt(
246947 Fts5Storage *p,
246948 int eStmt,
246949 sqlite3_stmt **pp,
246950 char **pzErrMsg
246951){
246952 int rc;
246953 assert( eStmt==FTS5_STMT_SCAN_ASC
246954 || eStmt==FTS5_STMT_SCAN_DESC
246955 || eStmt==FTS5_STMT_LOOKUP
246956 );
246957 rc = fts5StorageGetStmt(p, eStmt, ppStmt: pp, pzErrMsg);
246958 if( rc==SQLITE_OK ){
246959 assert( p->aStmt[eStmt]==*pp );
246960 p->aStmt[eStmt] = 0;
246961 }
246962 return rc;
246963}
246964
246965/*
246966** Release an SQLite statement handle obtained via an earlier call to
246967** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
246968** must match that passed to the sqlite3Fts5StorageStmt() call.
246969*/
246970static void sqlite3Fts5StorageStmtRelease(
246971 Fts5Storage *p,
246972 int eStmt,
246973 sqlite3_stmt *pStmt
246974){
246975 assert( eStmt==FTS5_STMT_SCAN_ASC
246976 || eStmt==FTS5_STMT_SCAN_DESC
246977 || eStmt==FTS5_STMT_LOOKUP
246978 );
246979 if( p->aStmt[eStmt]==0 ){
246980 sqlite3_reset(pStmt);
246981 p->aStmt[eStmt] = pStmt;
246982 }else{
246983 sqlite3_finalize(pStmt);
246984 }
246985}
246986
246987static int fts5StorageDecodeSizeArray(
246988 int *aCol, int nCol, /* Array to populate */
246989 const u8 *aBlob, int nBlob /* Record to read varints from */
246990){
246991 int i;
246992 int iOff = 0;
246993 for(i=0; i<nCol; i++){
246994 if( iOff>=nBlob ) return 1;
246995 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
246996 }
246997 return (iOff!=nBlob);
246998}
246999
247000/*
247001** Argument aCol points to an array of integers containing one entry for
247002** each table column. This function reads the %_docsize record for the
247003** specified rowid and populates aCol[] with the results.
247004**
247005** An SQLite error code is returned if an error occurs, or SQLITE_OK
247006** otherwise.
247007*/
247008static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
247009 int nCol = p->pConfig->nCol; /* Number of user columns in table */
247010 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
247011 int rc; /* Return Code */
247012
247013 assert( p->pConfig->bColumnsize );
247014 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, ppStmt: &pLookup, pzErrMsg: 0);
247015 if( pLookup ){
247016 int bCorrupt = 1;
247017 assert( rc==SQLITE_OK );
247018 sqlite3_bind_int64(pStmt: pLookup, i: 1, iValue: iRowid);
247019 if( SQLITE_ROW==sqlite3_step(pStmt: pLookup) ){
247020 const u8 *aBlob = sqlite3_column_blob(pStmt: pLookup, i: 0);
247021 int nBlob = sqlite3_column_bytes(pStmt: pLookup, i: 0);
247022 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
247023 bCorrupt = 0;
247024 }
247025 }
247026 rc = sqlite3_reset(pStmt: pLookup);
247027 if( bCorrupt && rc==SQLITE_OK ){
247028 rc = FTS5_CORRUPT;
247029 }
247030 }else{
247031 assert( rc!=SQLITE_OK );
247032 }
247033
247034 return rc;
247035}
247036
247037static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
247038 int rc = fts5StorageLoadTotals(p, bCache: 0);
247039 if( rc==SQLITE_OK ){
247040 *pnToken = 0;
247041 if( iCol<0 ){
247042 int i;
247043 for(i=0; i<p->pConfig->nCol; i++){
247044 *pnToken += p->aTotalSize[i];
247045 }
247046 }else if( iCol<p->pConfig->nCol ){
247047 *pnToken = p->aTotalSize[iCol];
247048 }else{
247049 rc = SQLITE_RANGE;
247050 }
247051 }
247052 return rc;
247053}
247054
247055static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
247056 int rc = fts5StorageLoadTotals(p, bCache: 0);
247057 if( rc==SQLITE_OK ){
247058 /* nTotalRow being zero does not necessarily indicate a corrupt
247059 ** database - it might be that the FTS5 table really does contain zero
247060 ** rows. However this function is only called from the xRowCount() API,
247061 ** and there is no way for that API to be invoked if the table contains
247062 ** no rows. Hence the FTS5_CORRUPT return. */
247063 *pnRow = p->nTotalRow;
247064 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
247065 }
247066 return rc;
247067}
247068
247069/*
247070** Flush any data currently held in-memory to disk.
247071*/
247072static int sqlite3Fts5StorageSync(Fts5Storage *p){
247073 int rc = SQLITE_OK;
247074 i64 iLastRowid = sqlite3_last_insert_rowid(db: p->pConfig->db);
247075 if( p->bTotalsValid ){
247076 rc = fts5StorageSaveTotals(p);
247077 p->bTotalsValid = 0;
247078 }
247079 if( rc==SQLITE_OK ){
247080 rc = sqlite3Fts5IndexSync(p: p->pIndex);
247081 }
247082 sqlite3_set_last_insert_rowid(db: p->pConfig->db, iRowid: iLastRowid);
247083 return rc;
247084}
247085
247086static int sqlite3Fts5StorageRollback(Fts5Storage *p){
247087 p->bTotalsValid = 0;
247088 return sqlite3Fts5IndexRollback(p: p->pIndex);
247089}
247090
247091static int sqlite3Fts5StorageConfigValue(
247092 Fts5Storage *p,
247093 const char *z,
247094 sqlite3_value *pVal,
247095 int iVal
247096){
247097 sqlite3_stmt *pReplace = 0;
247098 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, ppStmt: &pReplace, pzErrMsg: 0);
247099 if( rc==SQLITE_OK ){
247100 sqlite3_bind_text(pStmt: pReplace, i: 1, zData: z, nData: -1, SQLITE_STATIC);
247101 if( pVal ){
247102 sqlite3_bind_value(pStmt: pReplace, i: 2, pValue: pVal);
247103 }else{
247104 sqlite3_bind_int(p: pReplace, i: 2, iValue: iVal);
247105 }
247106 sqlite3_step(pStmt: pReplace);
247107 rc = sqlite3_reset(pStmt: pReplace);
247108 sqlite3_bind_null(pStmt: pReplace, i: 1);
247109 }
247110 if( rc==SQLITE_OK && pVal ){
247111 int iNew = p->pConfig->iCookie + 1;
247112 rc = sqlite3Fts5IndexSetCookie(p: p->pIndex, iNew);
247113 if( rc==SQLITE_OK ){
247114 p->pConfig->iCookie = iNew;
247115 }
247116 }
247117 return rc;
247118}
247119
247120/*
247121** 2014 May 31
247122**
247123** The author disclaims copyright to this source code. In place of
247124** a legal notice, here is a blessing:
247125**
247126** May you do good and not evil.
247127** May you find forgiveness for yourself and forgive others.
247128** May you share freely, never taking more than you give.
247129**
247130******************************************************************************
247131*/
247132
247133
247134/* #include "fts5Int.h" */
247135
247136/**************************************************************************
247137** Start of ascii tokenizer implementation.
247138*/
247139
247140/*
247141** For tokenizers with no "unicode" modifier, the set of token characters
247142** is the same as the set of ASCII range alphanumeric characters.
247143*/
247144static unsigned char aAsciiTokenChar[128] = {
247145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
247146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
247147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
247148 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
247149 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
247150 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
247151 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
247152 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
247153};
247154
247155typedef struct AsciiTokenizer AsciiTokenizer;
247156struct AsciiTokenizer {
247157 unsigned char aTokenChar[128];
247158};
247159
247160static void fts5AsciiAddExceptions(
247161 AsciiTokenizer *p,
247162 const char *zArg,
247163 int bTokenChars
247164){
247165 int i;
247166 for(i=0; zArg[i]; i++){
247167 if( (zArg[i] & 0x80)==0 ){
247168 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
247169 }
247170 }
247171}
247172
247173/*
247174** Delete a "ascii" tokenizer.
247175*/
247176static void fts5AsciiDelete(Fts5Tokenizer *p){
247177 sqlite3_free(p);
247178}
247179
247180/*
247181** Create an "ascii" tokenizer.
247182*/
247183static int fts5AsciiCreate(
247184 void *pUnused,
247185 const char **azArg, int nArg,
247186 Fts5Tokenizer **ppOut
247187){
247188 int rc = SQLITE_OK;
247189 AsciiTokenizer *p = 0;
247190 UNUSED_PARAM(pUnused);
247191 if( nArg%2 ){
247192 rc = SQLITE_ERROR;
247193 }else{
247194 p = sqlite3_malloc(n: sizeof(AsciiTokenizer));
247195 if( p==0 ){
247196 rc = SQLITE_NOMEM;
247197 }else{
247198 int i;
247199 memset(s: p, c: 0, n: sizeof(AsciiTokenizer));
247200 memcpy(dest: p->aTokenChar, src: aAsciiTokenChar, n: sizeof(aAsciiTokenChar));
247201 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
247202 const char *zArg = azArg[i+1];
247203 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "tokenchars") ){
247204 fts5AsciiAddExceptions(p, zArg, bTokenChars: 1);
247205 }else
247206 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "separators") ){
247207 fts5AsciiAddExceptions(p, zArg, bTokenChars: 0);
247208 }else{
247209 rc = SQLITE_ERROR;
247210 }
247211 }
247212 if( rc!=SQLITE_OK ){
247213 fts5AsciiDelete(p: (Fts5Tokenizer*)p);
247214 p = 0;
247215 }
247216 }
247217 }
247218
247219 *ppOut = (Fts5Tokenizer*)p;
247220 return rc;
247221}
247222
247223
247224static void asciiFold(char *aOut, const char *aIn, int nByte){
247225 int i;
247226 for(i=0; i<nByte; i++){
247227 char c = aIn[i];
247228 if( c>='A' && c<='Z' ) c += 32;
247229 aOut[i] = c;
247230 }
247231}
247232
247233/*
247234** Tokenize some text using the ascii tokenizer.
247235*/
247236static int fts5AsciiTokenize(
247237 Fts5Tokenizer *pTokenizer,
247238 void *pCtx,
247239 int iUnused,
247240 const char *pText, int nText,
247241 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
247242){
247243 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
247244 int rc = SQLITE_OK;
247245 int ie;
247246 int is = 0;
247247
247248 char aFold[64];
247249 int nFold = sizeof(aFold);
247250 char *pFold = aFold;
247251 unsigned char *a = p->aTokenChar;
247252
247253 UNUSED_PARAM(iUnused);
247254
247255 while( is<nText && rc==SQLITE_OK ){
247256 int nByte;
247257
247258 /* Skip any leading divider characters. */
247259 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
247260 is++;
247261 }
247262 if( is==nText ) break;
247263
247264 /* Count the token characters */
247265 ie = is+1;
247266 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
247267 ie++;
247268 }
247269
247270 /* Fold to lower case */
247271 nByte = ie-is;
247272 if( nByte>nFold ){
247273 if( pFold!=aFold ) sqlite3_free(p: pFold);
247274 pFold = sqlite3_malloc64(n: (sqlite3_int64)nByte*2);
247275 if( pFold==0 ){
247276 rc = SQLITE_NOMEM;
247277 break;
247278 }
247279 nFold = nByte*2;
247280 }
247281 asciiFold(aOut: pFold, aIn: &pText[is], nByte);
247282
247283 /* Invoke the token callback */
247284 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
247285 is = ie+1;
247286 }
247287
247288 if( pFold!=aFold ) sqlite3_free(p: pFold);
247289 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
247290 return rc;
247291}
247292
247293/**************************************************************************
247294** Start of unicode61 tokenizer implementation.
247295*/
247296
247297
247298/*
247299** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
247300** from the sqlite3 source file utf.c. If this file is compiled as part
247301** of the amalgamation, they are not required.
247302*/
247303#ifndef SQLITE_AMALGAMATION
247304
247305static const unsigned char sqlite3Utf8Trans1[] = {
247306 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
247307 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
247308 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
247309 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
247310 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
247311 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
247312 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
247313 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
247314};
247315
247316#define READ_UTF8(zIn, zTerm, c) \
247317 c = *(zIn++); \
247318 if( c>=0xc0 ){ \
247319 c = sqlite3Utf8Trans1[c-0xc0]; \
247320 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
247321 c = (c<<6) + (0x3f & *(zIn++)); \
247322 } \
247323 if( c<0x80 \
247324 || (c&0xFFFFF800)==0xD800 \
247325 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
247326 }
247327
247328
247329#define WRITE_UTF8(zOut, c) { \
247330 if( c<0x00080 ){ \
247331 *zOut++ = (unsigned char)(c&0xFF); \
247332 } \
247333 else if( c<0x00800 ){ \
247334 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
247335 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
247336 } \
247337 else if( c<0x10000 ){ \
247338 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
247339 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
247340 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
247341 }else{ \
247342 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
247343 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
247344 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
247345 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
247346 } \
247347}
247348
247349#endif /* ifndef SQLITE_AMALGAMATION */
247350
247351typedef struct Unicode61Tokenizer Unicode61Tokenizer;
247352struct Unicode61Tokenizer {
247353 unsigned char aTokenChar[128]; /* ASCII range token characters */
247354 char *aFold; /* Buffer to fold text into */
247355 int nFold; /* Size of aFold[] in bytes */
247356 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
247357 int nException;
247358 int *aiException;
247359
247360 unsigned char aCategory[32]; /* True for token char categories */
247361};
247362
247363/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
247364#define FTS5_REMOVE_DIACRITICS_NONE 0
247365#define FTS5_REMOVE_DIACRITICS_SIMPLE 1
247366#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
247367
247368static int fts5UnicodeAddExceptions(
247369 Unicode61Tokenizer *p, /* Tokenizer object */
247370 const char *z, /* Characters to treat as exceptions */
247371 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
247372){
247373 int rc = SQLITE_OK;
247374 int n = (int)strlen(s: z);
247375 int *aNew;
247376
247377 if( n>0 ){
247378 aNew = (int*)sqlite3_realloc64(pOld: p->aiException,
247379 n: (n+p->nException)*sizeof(int));
247380 if( aNew ){
247381 int nNew = p->nException;
247382 const unsigned char *zCsr = (const unsigned char*)z;
247383 const unsigned char *zTerm = (const unsigned char*)&z[n];
247384 while( zCsr<zTerm ){
247385 u32 iCode;
247386 int bToken;
247387 READ_UTF8(zCsr, zTerm, iCode);
247388 if( iCode<128 ){
247389 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
247390 }else{
247391 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
247392 assert( (bToken==0 || bToken==1) );
247393 assert( (bTokenChars==0 || bTokenChars==1) );
247394 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(c: iCode)==0 ){
247395 int i;
247396 for(i=0; i<nNew; i++){
247397 if( (u32)aNew[i]>iCode ) break;
247398 }
247399 memmove(dest: &aNew[i+1], src: &aNew[i], n: (nNew-i)*sizeof(int));
247400 aNew[i] = iCode;
247401 nNew++;
247402 }
247403 }
247404 }
247405 p->aiException = aNew;
247406 p->nException = nNew;
247407 }else{
247408 rc = SQLITE_NOMEM;
247409 }
247410 }
247411
247412 return rc;
247413}
247414
247415/*
247416** Return true if the p->aiException[] array contains the value iCode.
247417*/
247418static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
247419 if( p->nException>0 ){
247420 int *a = p->aiException;
247421 int iLo = 0;
247422 int iHi = p->nException-1;
247423
247424 while( iHi>=iLo ){
247425 int iTest = (iHi + iLo) / 2;
247426 if( iCode==a[iTest] ){
247427 return 1;
247428 }else if( iCode>a[iTest] ){
247429 iLo = iTest+1;
247430 }else{
247431 iHi = iTest-1;
247432 }
247433 }
247434 }
247435
247436 return 0;
247437}
247438
247439/*
247440** Delete a "unicode61" tokenizer.
247441*/
247442static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
247443 if( pTok ){
247444 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
247445 sqlite3_free(p: p->aiException);
247446 sqlite3_free(p: p->aFold);
247447 sqlite3_free(p);
247448 }
247449 return;
247450}
247451
247452static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
247453 const char *z = zCat;
247454
247455 while( *z ){
247456 while( *z==' ' || *z=='\t' ) z++;
247457 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
247458 return SQLITE_ERROR;
247459 }
247460 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
247461 }
247462
247463 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
247464 return SQLITE_OK;
247465}
247466
247467/*
247468** Create a "unicode61" tokenizer.
247469*/
247470static int fts5UnicodeCreate(
247471 void *pUnused,
247472 const char **azArg, int nArg,
247473 Fts5Tokenizer **ppOut
247474){
247475 int rc = SQLITE_OK; /* Return code */
247476 Unicode61Tokenizer *p = 0; /* New tokenizer object */
247477
247478 UNUSED_PARAM(pUnused);
247479
247480 if( nArg%2 ){
247481 rc = SQLITE_ERROR;
247482 }else{
247483 p = (Unicode61Tokenizer*)sqlite3_malloc(n: sizeof(Unicode61Tokenizer));
247484 if( p ){
247485 const char *zCat = "L* N* Co";
247486 int i;
247487 memset(s: p, c: 0, n: sizeof(Unicode61Tokenizer));
247488
247489 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
247490 p->nFold = 64;
247491 p->aFold = sqlite3_malloc64(n: p->nFold * sizeof(char));
247492 if( p->aFold==0 ){
247493 rc = SQLITE_NOMEM;
247494 }
247495
247496 /* Search for a "categories" argument */
247497 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
247498 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "categories") ){
247499 zCat = azArg[i+1];
247500 }
247501 }
247502
247503 if( rc==SQLITE_OK ){
247504 rc = unicodeSetCategories(p, zCat);
247505 }
247506
247507 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
247508 const char *zArg = azArg[i+1];
247509 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "remove_diacritics") ){
247510 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
247511 rc = SQLITE_ERROR;
247512 }else{
247513 p->eRemoveDiacritic = (zArg[0] - '0');
247514 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE
247515 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE
247516 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX
247517 );
247518 }
247519 }else
247520 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "tokenchars") ){
247521 rc = fts5UnicodeAddExceptions(p, z: zArg, bTokenChars: 1);
247522 }else
247523 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "separators") ){
247524 rc = fts5UnicodeAddExceptions(p, z: zArg, bTokenChars: 0);
247525 }else
247526 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "categories") ){
247527 /* no-op */
247528 }else{
247529 rc = SQLITE_ERROR;
247530 }
247531 }
247532
247533 }else{
247534 rc = SQLITE_NOMEM;
247535 }
247536 if( rc!=SQLITE_OK ){
247537 fts5UnicodeDelete(pTok: (Fts5Tokenizer*)p);
247538 p = 0;
247539 }
247540 *ppOut = (Fts5Tokenizer*)p;
247541 }
247542 return rc;
247543}
247544
247545/*
247546** Return true if, for the purposes of tokenizing with the tokenizer
247547** passed as the first argument, codepoint iCode is considered a token
247548** character (not a separator).
247549*/
247550static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
247551 return (
247552 p->aCategory[sqlite3Fts5UnicodeCategory(iCode: (u32)iCode)]
247553 ^ fts5UnicodeIsException(p, iCode)
247554 );
247555}
247556
247557static int fts5UnicodeTokenize(
247558 Fts5Tokenizer *pTokenizer,
247559 void *pCtx,
247560 int iUnused,
247561 const char *pText, int nText,
247562 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
247563){
247564 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
247565 int rc = SQLITE_OK;
247566 unsigned char *a = p->aTokenChar;
247567
247568 unsigned char *zTerm = (unsigned char*)&pText[nText];
247569 unsigned char *zCsr = (unsigned char *)pText;
247570
247571 /* Output buffer */
247572 char *aFold = p->aFold;
247573 int nFold = p->nFold;
247574 const char *pEnd = &aFold[nFold-6];
247575
247576 UNUSED_PARAM(iUnused);
247577
247578 /* Each iteration of this loop gobbles up a contiguous run of separators,
247579 ** then the next token. */
247580 while( rc==SQLITE_OK ){
247581 u32 iCode; /* non-ASCII codepoint read from input */
247582 char *zOut = aFold;
247583 int is;
247584 int ie;
247585
247586 /* Skip any separator characters. */
247587 while( 1 ){
247588 if( zCsr>=zTerm ) goto tokenize_done;
247589 if( *zCsr & 0x80 ) {
247590 /* A character outside of the ascii range. Skip past it if it is
247591 ** a separator character. Or break out of the loop if it is not. */
247592 is = zCsr - (unsigned char*)pText;
247593 READ_UTF8(zCsr, zTerm, iCode);
247594 if( fts5UnicodeIsAlnum(p, iCode) ){
247595 goto non_ascii_tokenchar;
247596 }
247597 }else{
247598 if( a[*zCsr] ){
247599 is = zCsr - (unsigned char*)pText;
247600 goto ascii_tokenchar;
247601 }
247602 zCsr++;
247603 }
247604 }
247605
247606 /* Run through the tokenchars. Fold them into the output buffer along
247607 ** the way. */
247608 while( zCsr<zTerm ){
247609
247610 /* Grow the output buffer so that there is sufficient space to fit the
247611 ** largest possible utf-8 character. */
247612 if( zOut>pEnd ){
247613 aFold = sqlite3_malloc64(n: (sqlite3_int64)nFold*2);
247614 if( aFold==0 ){
247615 rc = SQLITE_NOMEM;
247616 goto tokenize_done;
247617 }
247618 zOut = &aFold[zOut - p->aFold];
247619 memcpy(dest: aFold, src: p->aFold, n: nFold);
247620 sqlite3_free(p: p->aFold);
247621 p->aFold = aFold;
247622 p->nFold = nFold = nFold*2;
247623 pEnd = &aFold[nFold-6];
247624 }
247625
247626 if( *zCsr & 0x80 ){
247627 /* An non-ascii-range character. Fold it into the output buffer if
247628 ** it is a token character, or break out of the loop if it is not. */
247629 READ_UTF8(zCsr, zTerm, iCode);
247630 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(c: iCode) ){
247631 non_ascii_tokenchar:
247632 iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: p->eRemoveDiacritic);
247633 if( iCode ) WRITE_UTF8(zOut, iCode);
247634 }else{
247635 break;
247636 }
247637 }else if( a[*zCsr]==0 ){
247638 /* An ascii-range separator character. End of token. */
247639 break;
247640 }else{
247641 ascii_tokenchar:
247642 if( *zCsr>='A' && *zCsr<='Z' ){
247643 *zOut++ = *zCsr + 32;
247644 }else{
247645 *zOut++ = *zCsr;
247646 }
247647 zCsr++;
247648 }
247649 ie = zCsr - (unsigned char*)pText;
247650 }
247651
247652 /* Invoke the token callback */
247653 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
247654 }
247655
247656 tokenize_done:
247657 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
247658 return rc;
247659}
247660
247661/**************************************************************************
247662** Start of porter stemmer implementation.
247663*/
247664
247665/* Any tokens larger than this (in bytes) are passed through without
247666** stemming. */
247667#define FTS5_PORTER_MAX_TOKEN 64
247668
247669typedef struct PorterTokenizer PorterTokenizer;
247670struct PorterTokenizer {
247671 fts5_tokenizer tokenizer; /* Parent tokenizer module */
247672 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
247673 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
247674};
247675
247676/*
247677** Delete a "porter" tokenizer.
247678*/
247679static void fts5PorterDelete(Fts5Tokenizer *pTok){
247680 if( pTok ){
247681 PorterTokenizer *p = (PorterTokenizer*)pTok;
247682 if( p->pTokenizer ){
247683 p->tokenizer.xDelete(p->pTokenizer);
247684 }
247685 sqlite3_free(p);
247686 }
247687}
247688
247689/*
247690** Create a "porter" tokenizer.
247691*/
247692static int fts5PorterCreate(
247693 void *pCtx,
247694 const char **azArg, int nArg,
247695 Fts5Tokenizer **ppOut
247696){
247697 fts5_api *pApi = (fts5_api*)pCtx;
247698 int rc = SQLITE_OK;
247699 PorterTokenizer *pRet;
247700 void *pUserdata = 0;
247701 const char *zBase = "unicode61";
247702
247703 if( nArg>0 ){
247704 zBase = azArg[0];
247705 }
247706
247707 pRet = (PorterTokenizer*)sqlite3_malloc(n: sizeof(PorterTokenizer));
247708 if( pRet ){
247709 memset(s: pRet, c: 0, n: sizeof(PorterTokenizer));
247710 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
247711 }else{
247712 rc = SQLITE_NOMEM;
247713 }
247714 if( rc==SQLITE_OK ){
247715 int nArg2 = (nArg>0 ? nArg-1 : 0);
247716 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
247717 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
247718 }
247719
247720 if( rc!=SQLITE_OK ){
247721 fts5PorterDelete(pTok: (Fts5Tokenizer*)pRet);
247722 pRet = 0;
247723 }
247724 *ppOut = (Fts5Tokenizer*)pRet;
247725 return rc;
247726}
247727
247728typedef struct PorterContext PorterContext;
247729struct PorterContext {
247730 void *pCtx;
247731 int (*xToken)(void*, int, const char*, int, int, int);
247732 char *aBuf;
247733};
247734
247735typedef struct PorterRule PorterRule;
247736struct PorterRule {
247737 const char *zSuffix;
247738 int nSuffix;
247739 int (*xCond)(char *zStem, int nStem);
247740 const char *zOutput;
247741 int nOutput;
247742};
247743
247744#if 0
247745static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
247746 int ret = -1;
247747 int nBuf = *pnBuf;
247748 PorterRule *p;
247749
247750 for(p=aRule; p->zSuffix; p++){
247751 assert( strlen(p->zSuffix)==p->nSuffix );
247752 assert( strlen(p->zOutput)==p->nOutput );
247753 if( nBuf<p->nSuffix ) continue;
247754 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
247755 }
247756
247757 if( p->zSuffix ){
247758 int nStem = nBuf - p->nSuffix;
247759 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
247760 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
247761 *pnBuf = nStem + p->nOutput;
247762 ret = p - aRule;
247763 }
247764 }
247765
247766 return ret;
247767}
247768#endif
247769
247770static int fts5PorterIsVowel(char c, int bYIsVowel){
247771 return (
247772 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
247773 );
247774}
247775
247776static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
247777 int i;
247778 int bCons = bPrevCons;
247779
247780 /* Scan for a vowel */
247781 for(i=0; i<nStem; i++){
247782 if( 0==(bCons = !fts5PorterIsVowel(c: zStem[i], bYIsVowel: bCons)) ) break;
247783 }
247784
247785 /* Scan for a consonent */
247786 for(i++; i<nStem; i++){
247787 if( (bCons = !fts5PorterIsVowel(c: zStem[i], bYIsVowel: bCons)) ) return i+1;
247788 }
247789 return 0;
247790}
247791
247792/* porter rule condition: (m > 0) */
247793static int fts5Porter_MGt0(char *zStem, int nStem){
247794 return !!fts5PorterGobbleVC(zStem, nStem, bPrevCons: 0);
247795}
247796
247797/* porter rule condition: (m > 1) */
247798static int fts5Porter_MGt1(char *zStem, int nStem){
247799 int n;
247800 n = fts5PorterGobbleVC(zStem, nStem, bPrevCons: 0);
247801 if( n && fts5PorterGobbleVC(zStem: &zStem[n], nStem: nStem-n, bPrevCons: 1) ){
247802 return 1;
247803 }
247804 return 0;
247805}
247806
247807/* porter rule condition: (m = 1) */
247808static int fts5Porter_MEq1(char *zStem, int nStem){
247809 int n;
247810 n = fts5PorterGobbleVC(zStem, nStem, bPrevCons: 0);
247811 if( n && 0==fts5PorterGobbleVC(zStem: &zStem[n], nStem: nStem-n, bPrevCons: 1) ){
247812 return 1;
247813 }
247814 return 0;
247815}
247816
247817/* porter rule condition: (*o) */
247818static int fts5Porter_Ostar(char *zStem, int nStem){
247819 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
247820 return 0;
247821 }else{
247822 int i;
247823 int mask = 0;
247824 int bCons = 0;
247825 for(i=0; i<nStem; i++){
247826 bCons = !fts5PorterIsVowel(c: zStem[i], bYIsVowel: bCons);
247827 assert( bCons==0 || bCons==1 );
247828 mask = (mask << 1) + bCons;
247829 }
247830 return ((mask & 0x0007)==0x0005);
247831 }
247832}
247833
247834/* porter rule condition: (m > 1 and (*S or *T)) */
247835static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
247836 assert( nStem>0 );
247837 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
247838 && fts5Porter_MGt1(zStem, nStem);
247839}
247840
247841/* porter rule condition: (*v*) */
247842static int fts5Porter_Vowel(char *zStem, int nStem){
247843 int i;
247844 for(i=0; i<nStem; i++){
247845 if( fts5PorterIsVowel(c: zStem[i], bYIsVowel: i>0) ){
247846 return 1;
247847 }
247848 }
247849 return 0;
247850}
247851
247852
247853/**************************************************************************
247854***************************************************************************
247855** GENERATED CODE STARTS HERE (mkportersteps.tcl)
247856*/
247857
247858static int fts5PorterStep4(char *aBuf, int *pnBuf){
247859 int ret = 0;
247860 int nBuf = *pnBuf;
247861 switch( aBuf[nBuf-2] ){
247862
247863 case 'a':
247864 if( nBuf>2 && 0==memcmp(s1: "al", s2: &aBuf[nBuf-2], n: 2) ){
247865 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
247866 *pnBuf = nBuf - 2;
247867 }
247868 }
247869 break;
247870
247871 case 'c':
247872 if( nBuf>4 && 0==memcmp(s1: "ance", s2: &aBuf[nBuf-4], n: 4) ){
247873 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
247874 *pnBuf = nBuf - 4;
247875 }
247876 }else if( nBuf>4 && 0==memcmp(s1: "ence", s2: &aBuf[nBuf-4], n: 4) ){
247877 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
247878 *pnBuf = nBuf - 4;
247879 }
247880 }
247881 break;
247882
247883 case 'e':
247884 if( nBuf>2 && 0==memcmp(s1: "er", s2: &aBuf[nBuf-2], n: 2) ){
247885 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
247886 *pnBuf = nBuf - 2;
247887 }
247888 }
247889 break;
247890
247891 case 'i':
247892 if( nBuf>2 && 0==memcmp(s1: "ic", s2: &aBuf[nBuf-2], n: 2) ){
247893 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
247894 *pnBuf = nBuf - 2;
247895 }
247896 }
247897 break;
247898
247899 case 'l':
247900 if( nBuf>4 && 0==memcmp(s1: "able", s2: &aBuf[nBuf-4], n: 4) ){
247901 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
247902 *pnBuf = nBuf - 4;
247903 }
247904 }else if( nBuf>4 && 0==memcmp(s1: "ible", s2: &aBuf[nBuf-4], n: 4) ){
247905 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
247906 *pnBuf = nBuf - 4;
247907 }
247908 }
247909 break;
247910
247911 case 'n':
247912 if( nBuf>3 && 0==memcmp(s1: "ant", s2: &aBuf[nBuf-3], n: 3) ){
247913 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247914 *pnBuf = nBuf - 3;
247915 }
247916 }else if( nBuf>5 && 0==memcmp(s1: "ement", s2: &aBuf[nBuf-5], n: 5) ){
247917 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-5) ){
247918 *pnBuf = nBuf - 5;
247919 }
247920 }else if( nBuf>4 && 0==memcmp(s1: "ment", s2: &aBuf[nBuf-4], n: 4) ){
247921 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
247922 *pnBuf = nBuf - 4;
247923 }
247924 }else if( nBuf>3 && 0==memcmp(s1: "ent", s2: &aBuf[nBuf-3], n: 3) ){
247925 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247926 *pnBuf = nBuf - 3;
247927 }
247928 }
247929 break;
247930
247931 case 'o':
247932 if( nBuf>3 && 0==memcmp(s1: "ion", s2: &aBuf[nBuf-3], n: 3) ){
247933 if( fts5Porter_MGt1_and_S_or_T(zStem: aBuf, nStem: nBuf-3) ){
247934 *pnBuf = nBuf - 3;
247935 }
247936 }else if( nBuf>2 && 0==memcmp(s1: "ou", s2: &aBuf[nBuf-2], n: 2) ){
247937 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
247938 *pnBuf = nBuf - 2;
247939 }
247940 }
247941 break;
247942
247943 case 's':
247944 if( nBuf>3 && 0==memcmp(s1: "ism", s2: &aBuf[nBuf-3], n: 3) ){
247945 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247946 *pnBuf = nBuf - 3;
247947 }
247948 }
247949 break;
247950
247951 case 't':
247952 if( nBuf>3 && 0==memcmp(s1: "ate", s2: &aBuf[nBuf-3], n: 3) ){
247953 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247954 *pnBuf = nBuf - 3;
247955 }
247956 }else if( nBuf>3 && 0==memcmp(s1: "iti", s2: &aBuf[nBuf-3], n: 3) ){
247957 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247958 *pnBuf = nBuf - 3;
247959 }
247960 }
247961 break;
247962
247963 case 'u':
247964 if( nBuf>3 && 0==memcmp(s1: "ous", s2: &aBuf[nBuf-3], n: 3) ){
247965 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247966 *pnBuf = nBuf - 3;
247967 }
247968 }
247969 break;
247970
247971 case 'v':
247972 if( nBuf>3 && 0==memcmp(s1: "ive", s2: &aBuf[nBuf-3], n: 3) ){
247973 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247974 *pnBuf = nBuf - 3;
247975 }
247976 }
247977 break;
247978
247979 case 'z':
247980 if( nBuf>3 && 0==memcmp(s1: "ize", s2: &aBuf[nBuf-3], n: 3) ){
247981 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
247982 *pnBuf = nBuf - 3;
247983 }
247984 }
247985 break;
247986
247987 }
247988 return ret;
247989}
247990
247991
247992static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
247993 int ret = 0;
247994 int nBuf = *pnBuf;
247995 switch( aBuf[nBuf-2] ){
247996
247997 case 'a':
247998 if( nBuf>2 && 0==memcmp(s1: "at", s2: &aBuf[nBuf-2], n: 2) ){
247999 memcpy(dest: &aBuf[nBuf-2], src: "ate", n: 3);
248000 *pnBuf = nBuf - 2 + 3;
248001 ret = 1;
248002 }
248003 break;
248004
248005 case 'b':
248006 if( nBuf>2 && 0==memcmp(s1: "bl", s2: &aBuf[nBuf-2], n: 2) ){
248007 memcpy(dest: &aBuf[nBuf-2], src: "ble", n: 3);
248008 *pnBuf = nBuf - 2 + 3;
248009 ret = 1;
248010 }
248011 break;
248012
248013 case 'i':
248014 if( nBuf>2 && 0==memcmp(s1: "iz", s2: &aBuf[nBuf-2], n: 2) ){
248015 memcpy(dest: &aBuf[nBuf-2], src: "ize", n: 3);
248016 *pnBuf = nBuf - 2 + 3;
248017 ret = 1;
248018 }
248019 break;
248020
248021 }
248022 return ret;
248023}
248024
248025
248026static int fts5PorterStep2(char *aBuf, int *pnBuf){
248027 int ret = 0;
248028 int nBuf = *pnBuf;
248029 switch( aBuf[nBuf-2] ){
248030
248031 case 'a':
248032 if( nBuf>7 && 0==memcmp(s1: "ational", s2: &aBuf[nBuf-7], n: 7) ){
248033 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
248034 memcpy(dest: &aBuf[nBuf-7], src: "ate", n: 3);
248035 *pnBuf = nBuf - 7 + 3;
248036 }
248037 }else if( nBuf>6 && 0==memcmp(s1: "tional", s2: &aBuf[nBuf-6], n: 6) ){
248038 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-6) ){
248039 memcpy(dest: &aBuf[nBuf-6], src: "tion", n: 4);
248040 *pnBuf = nBuf - 6 + 4;
248041 }
248042 }
248043 break;
248044
248045 case 'c':
248046 if( nBuf>4 && 0==memcmp(s1: "enci", s2: &aBuf[nBuf-4], n: 4) ){
248047 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248048 memcpy(dest: &aBuf[nBuf-4], src: "ence", n: 4);
248049 *pnBuf = nBuf - 4 + 4;
248050 }
248051 }else if( nBuf>4 && 0==memcmp(s1: "anci", s2: &aBuf[nBuf-4], n: 4) ){
248052 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248053 memcpy(dest: &aBuf[nBuf-4], src: "ance", n: 4);
248054 *pnBuf = nBuf - 4 + 4;
248055 }
248056 }
248057 break;
248058
248059 case 'e':
248060 if( nBuf>4 && 0==memcmp(s1: "izer", s2: &aBuf[nBuf-4], n: 4) ){
248061 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248062 memcpy(dest: &aBuf[nBuf-4], src: "ize", n: 3);
248063 *pnBuf = nBuf - 4 + 3;
248064 }
248065 }
248066 break;
248067
248068 case 'g':
248069 if( nBuf>4 && 0==memcmp(s1: "logi", s2: &aBuf[nBuf-4], n: 4) ){
248070 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248071 memcpy(dest: &aBuf[nBuf-4], src: "log", n: 3);
248072 *pnBuf = nBuf - 4 + 3;
248073 }
248074 }
248075 break;
248076
248077 case 'l':
248078 if( nBuf>3 && 0==memcmp(s1: "bli", s2: &aBuf[nBuf-3], n: 3) ){
248079 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
248080 memcpy(dest: &aBuf[nBuf-3], src: "ble", n: 3);
248081 *pnBuf = nBuf - 3 + 3;
248082 }
248083 }else if( nBuf>4 && 0==memcmp(s1: "alli", s2: &aBuf[nBuf-4], n: 4) ){
248084 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248085 memcpy(dest: &aBuf[nBuf-4], src: "al", n: 2);
248086 *pnBuf = nBuf - 4 + 2;
248087 }
248088 }else if( nBuf>5 && 0==memcmp(s1: "entli", s2: &aBuf[nBuf-5], n: 5) ){
248089 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248090 memcpy(dest: &aBuf[nBuf-5], src: "ent", n: 3);
248091 *pnBuf = nBuf - 5 + 3;
248092 }
248093 }else if( nBuf>3 && 0==memcmp(s1: "eli", s2: &aBuf[nBuf-3], n: 3) ){
248094 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
248095 memcpy(dest: &aBuf[nBuf-3], src: "e", n: 1);
248096 *pnBuf = nBuf - 3 + 1;
248097 }
248098 }else if( nBuf>5 && 0==memcmp(s1: "ousli", s2: &aBuf[nBuf-5], n: 5) ){
248099 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248100 memcpy(dest: &aBuf[nBuf-5], src: "ous", n: 3);
248101 *pnBuf = nBuf - 5 + 3;
248102 }
248103 }
248104 break;
248105
248106 case 'o':
248107 if( nBuf>7 && 0==memcmp(s1: "ization", s2: &aBuf[nBuf-7], n: 7) ){
248108 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
248109 memcpy(dest: &aBuf[nBuf-7], src: "ize", n: 3);
248110 *pnBuf = nBuf - 7 + 3;
248111 }
248112 }else if( nBuf>5 && 0==memcmp(s1: "ation", s2: &aBuf[nBuf-5], n: 5) ){
248113 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248114 memcpy(dest: &aBuf[nBuf-5], src: "ate", n: 3);
248115 *pnBuf = nBuf - 5 + 3;
248116 }
248117 }else if( nBuf>4 && 0==memcmp(s1: "ator", s2: &aBuf[nBuf-4], n: 4) ){
248118 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248119 memcpy(dest: &aBuf[nBuf-4], src: "ate", n: 3);
248120 *pnBuf = nBuf - 4 + 3;
248121 }
248122 }
248123 break;
248124
248125 case 's':
248126 if( nBuf>5 && 0==memcmp(s1: "alism", s2: &aBuf[nBuf-5], n: 5) ){
248127 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248128 memcpy(dest: &aBuf[nBuf-5], src: "al", n: 2);
248129 *pnBuf = nBuf - 5 + 2;
248130 }
248131 }else if( nBuf>7 && 0==memcmp(s1: "iveness", s2: &aBuf[nBuf-7], n: 7) ){
248132 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
248133 memcpy(dest: &aBuf[nBuf-7], src: "ive", n: 3);
248134 *pnBuf = nBuf - 7 + 3;
248135 }
248136 }else if( nBuf>7 && 0==memcmp(s1: "fulness", s2: &aBuf[nBuf-7], n: 7) ){
248137 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
248138 memcpy(dest: &aBuf[nBuf-7], src: "ful", n: 3);
248139 *pnBuf = nBuf - 7 + 3;
248140 }
248141 }else if( nBuf>7 && 0==memcmp(s1: "ousness", s2: &aBuf[nBuf-7], n: 7) ){
248142 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
248143 memcpy(dest: &aBuf[nBuf-7], src: "ous", n: 3);
248144 *pnBuf = nBuf - 7 + 3;
248145 }
248146 }
248147 break;
248148
248149 case 't':
248150 if( nBuf>5 && 0==memcmp(s1: "aliti", s2: &aBuf[nBuf-5], n: 5) ){
248151 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248152 memcpy(dest: &aBuf[nBuf-5], src: "al", n: 2);
248153 *pnBuf = nBuf - 5 + 2;
248154 }
248155 }else if( nBuf>5 && 0==memcmp(s1: "iviti", s2: &aBuf[nBuf-5], n: 5) ){
248156 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248157 memcpy(dest: &aBuf[nBuf-5], src: "ive", n: 3);
248158 *pnBuf = nBuf - 5 + 3;
248159 }
248160 }else if( nBuf>6 && 0==memcmp(s1: "biliti", s2: &aBuf[nBuf-6], n: 6) ){
248161 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-6) ){
248162 memcpy(dest: &aBuf[nBuf-6], src: "ble", n: 3);
248163 *pnBuf = nBuf - 6 + 3;
248164 }
248165 }
248166 break;
248167
248168 }
248169 return ret;
248170}
248171
248172
248173static int fts5PorterStep3(char *aBuf, int *pnBuf){
248174 int ret = 0;
248175 int nBuf = *pnBuf;
248176 switch( aBuf[nBuf-2] ){
248177
248178 case 'a':
248179 if( nBuf>4 && 0==memcmp(s1: "ical", s2: &aBuf[nBuf-4], n: 4) ){
248180 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248181 memcpy(dest: &aBuf[nBuf-4], src: "ic", n: 2);
248182 *pnBuf = nBuf - 4 + 2;
248183 }
248184 }
248185 break;
248186
248187 case 's':
248188 if( nBuf>4 && 0==memcmp(s1: "ness", s2: &aBuf[nBuf-4], n: 4) ){
248189 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
248190 *pnBuf = nBuf - 4;
248191 }
248192 }
248193 break;
248194
248195 case 't':
248196 if( nBuf>5 && 0==memcmp(s1: "icate", s2: &aBuf[nBuf-5], n: 5) ){
248197 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248198 memcpy(dest: &aBuf[nBuf-5], src: "ic", n: 2);
248199 *pnBuf = nBuf - 5 + 2;
248200 }
248201 }else if( nBuf>5 && 0==memcmp(s1: "iciti", s2: &aBuf[nBuf-5], n: 5) ){
248202 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248203 memcpy(dest: &aBuf[nBuf-5], src: "ic", n: 2);
248204 *pnBuf = nBuf - 5 + 2;
248205 }
248206 }
248207 break;
248208
248209 case 'u':
248210 if( nBuf>3 && 0==memcmp(s1: "ful", s2: &aBuf[nBuf-3], n: 3) ){
248211 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
248212 *pnBuf = nBuf - 3;
248213 }
248214 }
248215 break;
248216
248217 case 'v':
248218 if( nBuf>5 && 0==memcmp(s1: "ative", s2: &aBuf[nBuf-5], n: 5) ){
248219 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248220 *pnBuf = nBuf - 5;
248221 }
248222 }
248223 break;
248224
248225 case 'z':
248226 if( nBuf>5 && 0==memcmp(s1: "alize", s2: &aBuf[nBuf-5], n: 5) ){
248227 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
248228 memcpy(dest: &aBuf[nBuf-5], src: "al", n: 2);
248229 *pnBuf = nBuf - 5 + 2;
248230 }
248231 }
248232 break;
248233
248234 }
248235 return ret;
248236}
248237
248238
248239static int fts5PorterStep1B(char *aBuf, int *pnBuf){
248240 int ret = 0;
248241 int nBuf = *pnBuf;
248242 switch( aBuf[nBuf-2] ){
248243
248244 case 'e':
248245 if( nBuf>3 && 0==memcmp(s1: "eed", s2: &aBuf[nBuf-3], n: 3) ){
248246 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
248247 memcpy(dest: &aBuf[nBuf-3], src: "ee", n: 2);
248248 *pnBuf = nBuf - 3 + 2;
248249 }
248250 }else if( nBuf>2 && 0==memcmp(s1: "ed", s2: &aBuf[nBuf-2], n: 2) ){
248251 if( fts5Porter_Vowel(zStem: aBuf, nStem: nBuf-2) ){
248252 *pnBuf = nBuf - 2;
248253 ret = 1;
248254 }
248255 }
248256 break;
248257
248258 case 'n':
248259 if( nBuf>3 && 0==memcmp(s1: "ing", s2: &aBuf[nBuf-3], n: 3) ){
248260 if( fts5Porter_Vowel(zStem: aBuf, nStem: nBuf-3) ){
248261 *pnBuf = nBuf - 3;
248262 ret = 1;
248263 }
248264 }
248265 break;
248266
248267 }
248268 return ret;
248269}
248270
248271/*
248272** GENERATED CODE ENDS HERE (mkportersteps.tcl)
248273***************************************************************************
248274**************************************************************************/
248275
248276static void fts5PorterStep1A(char *aBuf, int *pnBuf){
248277 int nBuf = *pnBuf;
248278 if( aBuf[nBuf-1]=='s' ){
248279 if( aBuf[nBuf-2]=='e' ){
248280 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
248281 || (nBuf>3 && aBuf[nBuf-3]=='i' )
248282 ){
248283 *pnBuf = nBuf-2;
248284 }else{
248285 *pnBuf = nBuf-1;
248286 }
248287 }
248288 else if( aBuf[nBuf-2]!='s' ){
248289 *pnBuf = nBuf-1;
248290 }
248291 }
248292}
248293
248294static int fts5PorterCb(
248295 void *pCtx,
248296 int tflags,
248297 const char *pToken,
248298 int nToken,
248299 int iStart,
248300 int iEnd
248301){
248302 PorterContext *p = (PorterContext*)pCtx;
248303
248304 char *aBuf;
248305 int nBuf;
248306
248307 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
248308 aBuf = p->aBuf;
248309 nBuf = nToken;
248310 memcpy(dest: aBuf, src: pToken, n: nBuf);
248311
248312 /* Step 1. */
248313 fts5PorterStep1A(aBuf, pnBuf: &nBuf);
248314 if( fts5PorterStep1B(aBuf, pnBuf: &nBuf) ){
248315 if( fts5PorterStep1B2(aBuf, pnBuf: &nBuf)==0 ){
248316 char c = aBuf[nBuf-1];
248317 if( fts5PorterIsVowel(c, bYIsVowel: 0)==0
248318 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
248319 ){
248320 nBuf--;
248321 }else if( fts5Porter_MEq1(zStem: aBuf, nStem: nBuf) && fts5Porter_Ostar(zStem: aBuf, nStem: nBuf) ){
248322 aBuf[nBuf++] = 'e';
248323 }
248324 }
248325 }
248326
248327 /* Step 1C. */
248328 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(zStem: aBuf, nStem: nBuf-1) ){
248329 aBuf[nBuf-1] = 'i';
248330 }
248331
248332 /* Steps 2 through 4. */
248333 fts5PorterStep2(aBuf, pnBuf: &nBuf);
248334 fts5PorterStep3(aBuf, pnBuf: &nBuf);
248335 fts5PorterStep4(aBuf, pnBuf: &nBuf);
248336
248337 /* Step 5a. */
248338 assert( nBuf>0 );
248339 if( aBuf[nBuf-1]=='e' ){
248340 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-1)
248341 || (fts5Porter_MEq1(zStem: aBuf, nStem: nBuf-1) && !fts5Porter_Ostar(zStem: aBuf, nStem: nBuf-1))
248342 ){
248343 nBuf--;
248344 }
248345 }
248346
248347 /* Step 5b. */
248348 if( nBuf>1 && aBuf[nBuf-1]=='l'
248349 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-1)
248350 ){
248351 nBuf--;
248352 }
248353
248354 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
248355
248356 pass_through:
248357 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
248358}
248359
248360/*
248361** Tokenize using the porter tokenizer.
248362*/
248363static int fts5PorterTokenize(
248364 Fts5Tokenizer *pTokenizer,
248365 void *pCtx,
248366 int flags,
248367 const char *pText, int nText,
248368 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
248369){
248370 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
248371 PorterContext sCtx;
248372 sCtx.xToken = xToken;
248373 sCtx.pCtx = pCtx;
248374 sCtx.aBuf = p->aBuf;
248375 return p->tokenizer.xTokenize(
248376 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
248377 );
248378}
248379
248380/**************************************************************************
248381** Start of trigram implementation.
248382*/
248383typedef struct TrigramTokenizer TrigramTokenizer;
248384struct TrigramTokenizer {
248385 int bFold; /* True to fold to lower-case */
248386};
248387
248388/*
248389** Free a trigram tokenizer.
248390*/
248391static void fts5TriDelete(Fts5Tokenizer *p){
248392 sqlite3_free(p);
248393}
248394
248395/*
248396** Allocate a trigram tokenizer.
248397*/
248398static int fts5TriCreate(
248399 void *pUnused,
248400 const char **azArg,
248401 int nArg,
248402 Fts5Tokenizer **ppOut
248403){
248404 int rc = SQLITE_OK;
248405 TrigramTokenizer *pNew = (TrigramTokenizer*)sqlite3_malloc(n: sizeof(*pNew));
248406 UNUSED_PARAM(pUnused);
248407 if( pNew==0 ){
248408 rc = SQLITE_NOMEM;
248409 }else{
248410 int i;
248411 pNew->bFold = 1;
248412 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
248413 const char *zArg = azArg[i+1];
248414 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "case_sensitive") ){
248415 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
248416 rc = SQLITE_ERROR;
248417 }else{
248418 pNew->bFold = (zArg[0]=='0');
248419 }
248420 }else{
248421 rc = SQLITE_ERROR;
248422 }
248423 }
248424 if( rc!=SQLITE_OK ){
248425 fts5TriDelete(p: (Fts5Tokenizer*)pNew);
248426 pNew = 0;
248427 }
248428 }
248429 *ppOut = (Fts5Tokenizer*)pNew;
248430 return rc;
248431}
248432
248433/*
248434** Trigram tokenizer tokenize routine.
248435*/
248436static int fts5TriTokenize(
248437 Fts5Tokenizer *pTok,
248438 void *pCtx,
248439 int unusedFlags,
248440 const char *pText, int nText,
248441 int (*xToken)(void*, int, const char*, int, int, int)
248442){
248443 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
248444 int rc = SQLITE_OK;
248445 char aBuf[32];
248446 const unsigned char *zIn = (const unsigned char*)pText;
248447 const unsigned char *zEof = &zIn[nText];
248448 u32 iCode;
248449
248450 UNUSED_PARAM(unusedFlags);
248451 while( 1 ){
248452 char *zOut = aBuf;
248453 int iStart = zIn - (const unsigned char*)pText;
248454 const unsigned char *zNext;
248455
248456 READ_UTF8(zIn, zEof, iCode);
248457 if( iCode==0 ) break;
248458 zNext = zIn;
248459 if( zIn<zEof ){
248460 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: 0);
248461 WRITE_UTF8(zOut, iCode);
248462 READ_UTF8(zIn, zEof, iCode);
248463 if( iCode==0 ) break;
248464 }else{
248465 break;
248466 }
248467 if( zIn<zEof ){
248468 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: 0);
248469 WRITE_UTF8(zOut, iCode);
248470 READ_UTF8(zIn, zEof, iCode);
248471 if( iCode==0 ) break;
248472 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: 0);
248473 WRITE_UTF8(zOut, iCode);
248474 }else{
248475 break;
248476 }
248477 rc = xToken(pCtx, 0, aBuf, zOut-aBuf, iStart, iStart + zOut-aBuf);
248478 if( rc!=SQLITE_OK ) break;
248479 zIn = zNext;
248480 }
248481
248482 return rc;
248483}
248484
248485/*
248486** Argument xCreate is a pointer to a constructor function for a tokenizer.
248487** pTok is a tokenizer previously created using the same method. This function
248488** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB
248489** indicating the style of pattern matching that the tokenizer can support.
248490** In practice, this is:
248491**
248492** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB
248493** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE
248494** all other tokenizers - FTS5_PATTERN_NONE
248495*/
248496static int sqlite3Fts5TokenizerPattern(
248497 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
248498 Fts5Tokenizer *pTok
248499){
248500 if( xCreate==fts5TriCreate ){
248501 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
248502 return p->bFold ? FTS5_PATTERN_LIKE : FTS5_PATTERN_GLOB;
248503 }
248504 return FTS5_PATTERN_NONE;
248505}
248506
248507/*
248508** Register all built-in tokenizers with FTS5.
248509*/
248510static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
248511 struct BuiltinTokenizer {
248512 const char *zName;
248513 fts5_tokenizer x;
248514 } aBuiltin[] = {
248515 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
248516 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
248517 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
248518 { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}},
248519 };
248520
248521 int rc = SQLITE_OK; /* Return code */
248522 int i; /* To iterate through builtin functions */
248523
248524 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
248525 rc = pApi->xCreateTokenizer(pApi,
248526 aBuiltin[i].zName,
248527 (void*)pApi,
248528 &aBuiltin[i].x,
248529 0
248530 );
248531 }
248532
248533 return rc;
248534}
248535
248536/*
248537** 2012-05-25
248538**
248539** The author disclaims copyright to this source code. In place of
248540** a legal notice, here is a blessing:
248541**
248542** May you do good and not evil.
248543** May you find forgiveness for yourself and forgive others.
248544** May you share freely, never taking more than you give.
248545**
248546******************************************************************************
248547*/
248548
248549/*
248550** DO NOT EDIT THIS MACHINE GENERATED FILE.
248551*/
248552
248553
248554/* #include <assert.h> */
248555
248556
248557
248558/*
248559** If the argument is a codepoint corresponding to a lowercase letter
248560** in the ASCII range with a diacritic added, return the codepoint
248561** of the ASCII letter only. For example, if passed 235 - "LATIN
248562** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
248563** E"). The resuls of passing a codepoint that corresponds to an
248564** uppercase letter are undefined.
248565*/
248566static int fts5_remove_diacritic(int c, int bComplex){
248567 unsigned short aDia[] = {
248568 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
248569 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
248570 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
248571 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
248572 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
248573 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
248574 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
248575 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
248576 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
248577 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
248578 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
248579 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
248580 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
248581 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
248582 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
248583 63182, 63242, 63274, 63310, 63368, 63390,
248584 };
248585#define HIBIT ((unsigned char)0x80)
248586 unsigned char aChar[] = {
248587 '\0', 'a', 'c', 'e', 'i', 'n',
248588 'o', 'u', 'y', 'y', 'a', 'c',
248589 'd', 'e', 'e', 'g', 'h', 'i',
248590 'j', 'k', 'l', 'n', 'o', 'r',
248591 's', 't', 'u', 'u', 'w', 'y',
248592 'z', 'o', 'u', 'a', 'i', 'o',
248593 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
248594 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
248595 'e', 'i', 'o', 'r', 'u', 's',
248596 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
248597 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
248598 '\0', '\0', '\0', '\0', 'a', 'b',
248599 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
248600 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
248601 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
248602 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
248603 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
248604 'w', 'x', 'y', 'z', 'h', 't',
248605 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
248606 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
248607 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
248608 };
248609
248610 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
248611 int iRes = 0;
248612 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
248613 int iLo = 0;
248614 while( iHi>=iLo ){
248615 int iTest = (iHi + iLo) / 2;
248616 if( key >= aDia[iTest] ){
248617 iRes = iTest;
248618 iLo = iTest+1;
248619 }else{
248620 iHi = iTest-1;
248621 }
248622 }
248623 assert( key>=aDia[iRes] );
248624 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
248625 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
248626}
248627
248628
248629/*
248630** Return true if the argument interpreted as a unicode codepoint
248631** is a diacritical modifier character.
248632*/
248633static int sqlite3Fts5UnicodeIsdiacritic(int c){
248634 unsigned int mask0 = 0x08029FDF;
248635 unsigned int mask1 = 0x000361F8;
248636 if( c<768 || c>817 ) return 0;
248637 return (c < 768+32) ?
248638 (mask0 & ((unsigned int)1 << (c-768))) :
248639 (mask1 & ((unsigned int)1 << (c-768-32)));
248640}
248641
248642
248643/*
248644** Interpret the argument as a unicode codepoint. If the codepoint
248645** is an upper case character that has a lower case equivalent,
248646** return the codepoint corresponding to the lower case version.
248647** Otherwise, return a copy of the argument.
248648**
248649** The results are undefined if the value passed to this function
248650** is less than zero.
248651*/
248652static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
248653 /* Each entry in the following array defines a rule for folding a range
248654 ** of codepoints to lower case. The rule applies to a range of nRange
248655 ** codepoints starting at codepoint iCode.
248656 **
248657 ** If the least significant bit in flags is clear, then the rule applies
248658 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
248659 ** need to be folded). Or, if it is set, then the rule only applies to
248660 ** every second codepoint in the range, starting with codepoint C.
248661 **
248662 ** The 7 most significant bits in flags are an index into the aiOff[]
248663 ** array. If a specific codepoint C does require folding, then its lower
248664 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
248665 **
248666 ** The contents of this array are generated by parsing the CaseFolding.txt
248667 ** file distributed as part of the "Unicode Character Database". See
248668 ** http://www.unicode.org for details.
248669 */
248670 static const struct TableEntry {
248671 unsigned short iCode;
248672 unsigned char flags;
248673 unsigned char nRange;
248674 } aEntry[] = {
248675 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
248676 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
248677 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
248678 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
248679 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
248680 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
248681 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
248682 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
248683 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
248684 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
248685 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
248686 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
248687 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
248688 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
248689 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
248690 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
248691 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
248692 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
248693 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
248694 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
248695 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
248696 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
248697 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
248698 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
248699 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
248700 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
248701 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
248702 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
248703 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
248704 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
248705 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
248706 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
248707 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
248708 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
248709 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
248710 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
248711 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
248712 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
248713 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
248714 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
248715 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
248716 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
248717 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
248718 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
248719 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
248720 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
248721 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
248722 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
248723 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
248724 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
248725 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
248726 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
248727 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
248728 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
248729 {65313, 14, 26},
248730 };
248731 static const unsigned short aiOff[] = {
248732 1, 2, 8, 15, 16, 26, 28, 32,
248733 37, 38, 40, 48, 63, 64, 69, 71,
248734 79, 80, 116, 202, 203, 205, 206, 207,
248735 209, 210, 211, 213, 214, 217, 218, 219,
248736 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
248737 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
248738 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
248739 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
248740 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
248741 65514, 65521, 65527, 65528, 65529,
248742 };
248743
248744 int ret = c;
248745
248746 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
248747
248748 if( c<128 ){
248749 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
248750 }else if( c<65536 ){
248751 const struct TableEntry *p;
248752 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
248753 int iLo = 0;
248754 int iRes = -1;
248755
248756 assert( c>aEntry[0].iCode );
248757 while( iHi>=iLo ){
248758 int iTest = (iHi + iLo) / 2;
248759 int cmp = (c - aEntry[iTest].iCode);
248760 if( cmp>=0 ){
248761 iRes = iTest;
248762 iLo = iTest+1;
248763 }else{
248764 iHi = iTest-1;
248765 }
248766 }
248767
248768 assert( iRes>=0 && c>=aEntry[iRes].iCode );
248769 p = &aEntry[iRes];
248770 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
248771 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
248772 assert( ret>0 );
248773 }
248774
248775 if( eRemoveDiacritic ){
248776 ret = fts5_remove_diacritic(c: ret, bComplex: eRemoveDiacritic==2);
248777 }
248778 }
248779
248780 else if( c>=66560 && c<66600 ){
248781 ret = c + 40;
248782 }
248783
248784 return ret;
248785}
248786
248787
248788static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
248789 aArray[0] = 1;
248790 switch( zCat[0] ){
248791 case 'C':
248792 switch( zCat[1] ){
248793 case 'c': aArray[1] = 1; break;
248794 case 'f': aArray[2] = 1; break;
248795 case 'n': aArray[3] = 1; break;
248796 case 's': aArray[4] = 1; break;
248797 case 'o': aArray[31] = 1; break;
248798 case '*':
248799 aArray[1] = 1;
248800 aArray[2] = 1;
248801 aArray[3] = 1;
248802 aArray[4] = 1;
248803 aArray[31] = 1;
248804 break;
248805 default: return 1; }
248806 break;
248807
248808 case 'L':
248809 switch( zCat[1] ){
248810 case 'l': aArray[5] = 1; break;
248811 case 'm': aArray[6] = 1; break;
248812 case 'o': aArray[7] = 1; break;
248813 case 't': aArray[8] = 1; break;
248814 case 'u': aArray[9] = 1; break;
248815 case 'C': aArray[30] = 1; break;
248816 case '*':
248817 aArray[5] = 1;
248818 aArray[6] = 1;
248819 aArray[7] = 1;
248820 aArray[8] = 1;
248821 aArray[9] = 1;
248822 aArray[30] = 1;
248823 break;
248824 default: return 1; }
248825 break;
248826
248827 case 'M':
248828 switch( zCat[1] ){
248829 case 'c': aArray[10] = 1; break;
248830 case 'e': aArray[11] = 1; break;
248831 case 'n': aArray[12] = 1; break;
248832 case '*':
248833 aArray[10] = 1;
248834 aArray[11] = 1;
248835 aArray[12] = 1;
248836 break;
248837 default: return 1; }
248838 break;
248839
248840 case 'N':
248841 switch( zCat[1] ){
248842 case 'd': aArray[13] = 1; break;
248843 case 'l': aArray[14] = 1; break;
248844 case 'o': aArray[15] = 1; break;
248845 case '*':
248846 aArray[13] = 1;
248847 aArray[14] = 1;
248848 aArray[15] = 1;
248849 break;
248850 default: return 1; }
248851 break;
248852
248853 case 'P':
248854 switch( zCat[1] ){
248855 case 'c': aArray[16] = 1; break;
248856 case 'd': aArray[17] = 1; break;
248857 case 'e': aArray[18] = 1; break;
248858 case 'f': aArray[19] = 1; break;
248859 case 'i': aArray[20] = 1; break;
248860 case 'o': aArray[21] = 1; break;
248861 case 's': aArray[22] = 1; break;
248862 case '*':
248863 aArray[16] = 1;
248864 aArray[17] = 1;
248865 aArray[18] = 1;
248866 aArray[19] = 1;
248867 aArray[20] = 1;
248868 aArray[21] = 1;
248869 aArray[22] = 1;
248870 break;
248871 default: return 1; }
248872 break;
248873
248874 case 'S':
248875 switch( zCat[1] ){
248876 case 'c': aArray[23] = 1; break;
248877 case 'k': aArray[24] = 1; break;
248878 case 'm': aArray[25] = 1; break;
248879 case 'o': aArray[26] = 1; break;
248880 case '*':
248881 aArray[23] = 1;
248882 aArray[24] = 1;
248883 aArray[25] = 1;
248884 aArray[26] = 1;
248885 break;
248886 default: return 1; }
248887 break;
248888
248889 case 'Z':
248890 switch( zCat[1] ){
248891 case 'l': aArray[27] = 1; break;
248892 case 'p': aArray[28] = 1; break;
248893 case 's': aArray[29] = 1; break;
248894 case '*':
248895 aArray[27] = 1;
248896 aArray[28] = 1;
248897 aArray[29] = 1;
248898 break;
248899 default: return 1; }
248900 break;
248901
248902 }
248903 return 0;
248904}
248905
248906static u16 aFts5UnicodeBlock[] = {
248907 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
248908 1760, 1760, 1760, 1760, 1760, 1763, 1765,
248909 };
248910static u16 aFts5UnicodeMap[] = {
248911 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
248912 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
248913 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
248914 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
248915 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
248916 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
248917 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
248918 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
248919 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
248920 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
248921 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
248922 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
248923 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
248924 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
248925 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
248926 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
248927 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
248928 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
248929 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
248930 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
248931 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
248932 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
248933 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
248934 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
248935 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
248936 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
248937 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
248938 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
248939 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
248940 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
248941 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
248942 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
248943 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
248944 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
248945 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
248946 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
248947 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
248948 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
248949 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
248950 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
248951 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
248952 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
248953 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
248954 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
248955 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
248956 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
248957 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
248958 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
248959 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
248960 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
248961 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
248962 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
248963 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
248964 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
248965 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
248966 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
248967 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
248968 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
248969 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
248970 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
248971 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
248972 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
248973 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
248974 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
248975 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
248976 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
248977 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
248978 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
248979 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
248980 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
248981 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
248982 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
248983 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
248984 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
248985 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
248986 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
248987 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
248988 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
248989 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
248990 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
248991 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
248992 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
248993 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
248994 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
248995 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
248996 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
248997 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
248998 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
248999 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
249000 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
249001 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
249002 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
249003 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
249004 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
249005 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
249006 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
249007 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
249008 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
249009 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
249010 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
249011 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
249012 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
249013 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
249014 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
249015 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
249016 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
249017 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
249018 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
249019 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
249020 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
249021 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
249022 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
249023 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
249024 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
249025 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
249026 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
249027 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
249028 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
249029 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
249030 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
249031 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
249032 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
249033 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
249034 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
249035 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
249036 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
249037 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
249038 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
249039 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
249040 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
249041 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
249042 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
249043 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
249044 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
249045 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
249046 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
249047 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
249048 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
249049 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
249050 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
249051 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
249052 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
249053 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
249054 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
249055 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
249056 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
249057 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
249058 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
249059 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
249060 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
249061 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
249062 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
249063 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
249064 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
249065 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
249066 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
249067 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
249068 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
249069 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
249070 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
249071 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
249072 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
249073 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
249074 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
249075 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
249076 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
249077 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
249078 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
249079 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
249080 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
249081 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
249082 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
249083 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
249084 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
249085 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
249086 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
249087 1, 32, 256, 0, 65533,
249088 };
249089static u16 aFts5UnicodeData[] = {
249090 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
249091 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
249092 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
249093 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
249094 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
249095 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
249096 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
249097 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
249098 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
249099 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
249100 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
249101 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
249102 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
249103 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
249104 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
249105 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
249106 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
249107 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
249108 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
249109 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
249110 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
249111 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
249112 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
249113 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
249114 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
249115 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
249116 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
249117 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
249118 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
249119 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
249120 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
249121 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
249122 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
249123 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
249124 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
249125 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
249126 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
249127 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
249128 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
249129 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
249130 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
249131 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
249132 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
249133 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
249134 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
249135 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
249136 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
249137 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
249138 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
249139 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
249140 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
249141 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
249142 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
249143 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
249144 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
249145 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
249146 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
249147 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
249148 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
249149 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
249150 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
249151 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
249152 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
249153 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
249154 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
249155 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
249156 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
249157 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
249158 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
249159 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
249160 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
249161 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
249162 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
249163 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
249164 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
249165 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
249166 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
249167 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
249168 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
249169 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
249170 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
249171 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
249172 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
249173 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
249174 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
249175 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
249176 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
249177 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
249178 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
249179 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
249180 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
249181 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
249182 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
249183 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
249184 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
249185 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
249186 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
249187 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
249188 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
249189 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
249190 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
249191 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
249192 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
249193 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
249194 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
249195 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
249196 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
249197 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
249198 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
249199 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
249200 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
249201 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
249202 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
249203 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
249204 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
249205 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
249206 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
249207 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
249208 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
249209 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
249210 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
249211 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
249212 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
249213 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
249214 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
249215 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
249216 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
249217 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
249218 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
249219 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
249220 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
249221 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
249222 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
249223 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
249224 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
249225 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
249226 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
249227 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
249228 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
249229 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
249230 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
249231 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
249232 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
249233 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
249234 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
249235 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
249236 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
249237 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
249238 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
249239 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
249240 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
249241 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
249242 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
249243 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
249244 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
249245 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
249246 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
249247 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
249248 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
249249 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
249250 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
249251 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
249252 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
249253 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
249254 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
249255 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
249256 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
249257 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
249258 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
249259 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
249260 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
249261 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
249262 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
249263 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
249264 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
249265 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
249266 34, 3074, 7692, 63, 63,
249267 };
249268
249269static int sqlite3Fts5UnicodeCategory(u32 iCode) {
249270 int iRes = -1;
249271 int iHi;
249272 int iLo;
249273 int ret;
249274 u16 iKey;
249275
249276 if( iCode>=(1<<20) ){
249277 return 0;
249278 }
249279 iLo = aFts5UnicodeBlock[(iCode>>16)];
249280 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
249281 iKey = (iCode & 0xFFFF);
249282 while( iHi>iLo ){
249283 int iTest = (iHi + iLo) / 2;
249284 assert( iTest>=iLo && iTest<iHi );
249285 if( iKey>=aFts5UnicodeMap[iTest] ){
249286 iRes = iTest;
249287 iLo = iTest+1;
249288 }else{
249289 iHi = iTest;
249290 }
249291 }
249292
249293 if( iRes<0 ) return 0;
249294 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
249295 ret = aFts5UnicodeData[iRes] & 0x1F;
249296 if( ret!=30 ) return ret;
249297 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
249298}
249299
249300static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
249301 int i = 0;
249302 int iTbl = 0;
249303 while( i<128 ){
249304 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
249305 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
249306 for(; i<128 && i<n; i++){
249307 aAscii[i] = (u8)bToken;
249308 }
249309 iTbl++;
249310 }
249311 aAscii[0] = 0; /* 0x00 is never a token character */
249312}
249313
249314
249315/*
249316** 2015 May 30
249317**
249318** The author disclaims copyright to this source code. In place of
249319** a legal notice, here is a blessing:
249320**
249321** May you do good and not evil.
249322** May you find forgiveness for yourself and forgive others.
249323** May you share freely, never taking more than you give.
249324**
249325******************************************************************************
249326**
249327** Routines for varint serialization and deserialization.
249328*/
249329
249330
249331/* #include "fts5Int.h" */
249332
249333/*
249334** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
249335** Except, this version does handle the single byte case that the core
249336** version depends on being handled before its function is called.
249337*/
249338static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
249339 u32 a,b;
249340
249341 /* The 1-byte case. Overwhelmingly the most common. */
249342 a = *p;
249343 /* a: p0 (unmasked) */
249344 if (!(a&0x80))
249345 {
249346 /* Values between 0 and 127 */
249347 *v = a;
249348 return 1;
249349 }
249350
249351 /* The 2-byte case */
249352 p++;
249353 b = *p;
249354 /* b: p1 (unmasked) */
249355 if (!(b&0x80))
249356 {
249357 /* Values between 128 and 16383 */
249358 a &= 0x7f;
249359 a = a<<7;
249360 *v = a | b;
249361 return 2;
249362 }
249363
249364 /* The 3-byte case */
249365 p++;
249366 a = a<<14;
249367 a |= *p;
249368 /* a: p0<<14 | p2 (unmasked) */
249369 if (!(a&0x80))
249370 {
249371 /* Values between 16384 and 2097151 */
249372 a &= (0x7f<<14)|(0x7f);
249373 b &= 0x7f;
249374 b = b<<7;
249375 *v = a | b;
249376 return 3;
249377 }
249378
249379 /* A 32-bit varint is used to store size information in btrees.
249380 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
249381 ** A 3-byte varint is sufficient, for example, to record the size
249382 ** of a 1048569-byte BLOB or string.
249383 **
249384 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
249385 ** rare larger cases can be handled by the slower 64-bit varint
249386 ** routine.
249387 */
249388 {
249389 u64 v64;
249390 u8 n;
249391 p -= 2;
249392 n = sqlite3Fts5GetVarint(p, &v64);
249393 *v = ((u32)v64) & 0x7FFFFFFF;
249394 assert( n>3 && n<=9 );
249395 return n;
249396 }
249397}
249398
249399
249400/*
249401** Bitmasks used by sqlite3GetVarint(). These precomputed constants
249402** are defined here rather than simply putting the constant expressions
249403** inline in order to work around bugs in the RVT compiler.
249404**
249405** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
249406**
249407** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
249408*/
249409#define SLOT_2_0 0x001fc07f
249410#define SLOT_4_2_0 0xf01fc07f
249411
249412/*
249413** Read a 64-bit variable-length integer from memory starting at p[0].
249414** Return the number of bytes read. The value is stored in *v.
249415*/
249416static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
249417 u32 a,b,s;
249418
249419 a = *p;
249420 /* a: p0 (unmasked) */
249421 if (!(a&0x80))
249422 {
249423 *v = a;
249424 return 1;
249425 }
249426
249427 p++;
249428 b = *p;
249429 /* b: p1 (unmasked) */
249430 if (!(b&0x80))
249431 {
249432 a &= 0x7f;
249433 a = a<<7;
249434 a |= b;
249435 *v = a;
249436 return 2;
249437 }
249438
249439 /* Verify that constants are precomputed correctly */
249440 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
249441 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
249442
249443 p++;
249444 a = a<<14;
249445 a |= *p;
249446 /* a: p0<<14 | p2 (unmasked) */
249447 if (!(a&0x80))
249448 {
249449 a &= SLOT_2_0;
249450 b &= 0x7f;
249451 b = b<<7;
249452 a |= b;
249453 *v = a;
249454 return 3;
249455 }
249456
249457 /* CSE1 from below */
249458 a &= SLOT_2_0;
249459 p++;
249460 b = b<<14;
249461 b |= *p;
249462 /* b: p1<<14 | p3 (unmasked) */
249463 if (!(b&0x80))
249464 {
249465 b &= SLOT_2_0;
249466 /* moved CSE1 up */
249467 /* a &= (0x7f<<14)|(0x7f); */
249468 a = a<<7;
249469 a |= b;
249470 *v = a;
249471 return 4;
249472 }
249473
249474 /* a: p0<<14 | p2 (masked) */
249475 /* b: p1<<14 | p3 (unmasked) */
249476 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
249477 /* moved CSE1 up */
249478 /* a &= (0x7f<<14)|(0x7f); */
249479 b &= SLOT_2_0;
249480 s = a;
249481 /* s: p0<<14 | p2 (masked) */
249482
249483 p++;
249484 a = a<<14;
249485 a |= *p;
249486 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
249487 if (!(a&0x80))
249488 {
249489 /* we can skip these cause they were (effectively) done above in calc'ing s */
249490 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
249491 /* b &= (0x7f<<14)|(0x7f); */
249492 b = b<<7;
249493 a |= b;
249494 s = s>>18;
249495 *v = ((u64)s)<<32 | a;
249496 return 5;
249497 }
249498
249499 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
249500 s = s<<7;
249501 s |= b;
249502 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
249503
249504 p++;
249505 b = b<<14;
249506 b |= *p;
249507 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
249508 if (!(b&0x80))
249509 {
249510 /* we can skip this cause it was (effectively) done above in calc'ing s */
249511 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
249512 a &= SLOT_2_0;
249513 a = a<<7;
249514 a |= b;
249515 s = s>>18;
249516 *v = ((u64)s)<<32 | a;
249517 return 6;
249518 }
249519
249520 p++;
249521 a = a<<14;
249522 a |= *p;
249523 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
249524 if (!(a&0x80))
249525 {
249526 a &= SLOT_4_2_0;
249527 b &= SLOT_2_0;
249528 b = b<<7;
249529 a |= b;
249530 s = s>>11;
249531 *v = ((u64)s)<<32 | a;
249532 return 7;
249533 }
249534
249535 /* CSE2 from below */
249536 a &= SLOT_2_0;
249537 p++;
249538 b = b<<14;
249539 b |= *p;
249540 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
249541 if (!(b&0x80))
249542 {
249543 b &= SLOT_4_2_0;
249544 /* moved CSE2 up */
249545 /* a &= (0x7f<<14)|(0x7f); */
249546 a = a<<7;
249547 a |= b;
249548 s = s>>4;
249549 *v = ((u64)s)<<32 | a;
249550 return 8;
249551 }
249552
249553 p++;
249554 a = a<<15;
249555 a |= *p;
249556 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
249557
249558 /* moved CSE2 up */
249559 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
249560 b &= SLOT_2_0;
249561 b = b<<8;
249562 a |= b;
249563
249564 s = s<<4;
249565 b = p[-4];
249566 b &= 0x7f;
249567 b = b>>3;
249568 s |= b;
249569
249570 *v = ((u64)s)<<32 | a;
249571
249572 return 9;
249573}
249574
249575/*
249576** The variable-length integer encoding is as follows:
249577**
249578** KEY:
249579** A = 0xxxxxxx 7 bits of data and one flag bit
249580** B = 1xxxxxxx 7 bits of data and one flag bit
249581** C = xxxxxxxx 8 bits of data
249582**
249583** 7 bits - A
249584** 14 bits - BA
249585** 21 bits - BBA
249586** 28 bits - BBBA
249587** 35 bits - BBBBA
249588** 42 bits - BBBBBA
249589** 49 bits - BBBBBBA
249590** 56 bits - BBBBBBBA
249591** 64 bits - BBBBBBBBC
249592*/
249593
249594#ifdef SQLITE_NOINLINE
249595# define FTS5_NOINLINE SQLITE_NOINLINE
249596#else
249597# define FTS5_NOINLINE
249598#endif
249599
249600/*
249601** Write a 64-bit variable-length integer to memory starting at p[0].
249602** The length of data write will be between 1 and 9 bytes. The number
249603** of bytes written is returned.
249604**
249605** A variable-length integer consists of the lower 7 bits of each byte
249606** for all bytes that have the 8th bit set and one byte with the 8th
249607** bit clear. Except, if we get to the 9th byte, it stores the full
249608** 8 bits and is the last byte.
249609*/
249610static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
249611 int i, j, n;
249612 u8 buf[10];
249613 if( v & (((u64)0xff000000)<<32) ){
249614 p[8] = (u8)v;
249615 v >>= 8;
249616 for(i=7; i>=0; i--){
249617 p[i] = (u8)((v & 0x7f) | 0x80);
249618 v >>= 7;
249619 }
249620 return 9;
249621 }
249622 n = 0;
249623 do{
249624 buf[n++] = (u8)((v & 0x7f) | 0x80);
249625 v >>= 7;
249626 }while( v!=0 );
249627 buf[0] &= 0x7f;
249628 assert( n<=9 );
249629 for(i=0, j=n-1; j>=0; j--, i++){
249630 p[i] = buf[j];
249631 }
249632 return n;
249633}
249634
249635static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
249636 if( v<=0x7f ){
249637 p[0] = v&0x7f;
249638 return 1;
249639 }
249640 if( v<=0x3fff ){
249641 p[0] = ((v>>7)&0x7f)|0x80;
249642 p[1] = v&0x7f;
249643 return 2;
249644 }
249645 return fts5PutVarint64(p,v);
249646}
249647
249648
249649static int sqlite3Fts5GetVarintLen(u32 iVal){
249650#if 0
249651 if( iVal<(1 << 7 ) ) return 1;
249652#endif
249653 assert( iVal>=(1 << 7) );
249654 if( iVal<(1 << 14) ) return 2;
249655 if( iVal<(1 << 21) ) return 3;
249656 if( iVal<(1 << 28) ) return 4;
249657 return 5;
249658}
249659
249660/*
249661** 2015 May 08
249662**
249663** The author disclaims copyright to this source code. In place of
249664** a legal notice, here is a blessing:
249665**
249666** May you do good and not evil.
249667** May you find forgiveness for yourself and forgive others.
249668** May you share freely, never taking more than you give.
249669**
249670******************************************************************************
249671**
249672** This is an SQLite virtual table module implementing direct access to an
249673** existing FTS5 index. The module may create several different types of
249674** tables:
249675**
249676** col:
249677** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
249678**
249679** One row for each term/column combination. The value of $doc is set to
249680** the number of fts5 rows that contain at least one instance of term
249681** $term within column $col. Field $cnt is set to the total number of
249682** instances of term $term in column $col (in any row of the fts5 table).
249683**
249684** row:
249685** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
249686**
249687** One row for each term in the database. The value of $doc is set to
249688** the number of fts5 rows that contain at least one instance of term
249689** $term. Field $cnt is set to the total number of instances of term
249690** $term in the database.
249691**
249692** instance:
249693** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
249694**
249695** One row for each term instance in the database.
249696*/
249697
249698
249699/* #include "fts5Int.h" */
249700
249701
249702typedef struct Fts5VocabTable Fts5VocabTable;
249703typedef struct Fts5VocabCursor Fts5VocabCursor;
249704
249705struct Fts5VocabTable {
249706 sqlite3_vtab base;
249707 char *zFts5Tbl; /* Name of fts5 table */
249708 char *zFts5Db; /* Db containing fts5 table */
249709 sqlite3 *db; /* Database handle */
249710 Fts5Global *pGlobal; /* FTS5 global object for this database */
249711 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
249712 unsigned bBusy; /* True if busy */
249713};
249714
249715struct Fts5VocabCursor {
249716 sqlite3_vtab_cursor base;
249717 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
249718 Fts5Table *pFts5; /* Associated FTS5 table */
249719
249720 int bEof; /* True if this cursor is at EOF */
249721 Fts5IndexIter *pIter; /* Term/rowid iterator object */
249722 void *pStruct; /* From sqlite3Fts5StructureRef() */
249723
249724 int nLeTerm; /* Size of zLeTerm in bytes */
249725 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
249726
249727 /* These are used by 'col' tables only */
249728 int iCol;
249729 i64 *aCnt;
249730 i64 *aDoc;
249731
249732 /* Output values used by all tables. */
249733 i64 rowid; /* This table's current rowid value */
249734 Fts5Buffer term; /* Current value of 'term' column */
249735
249736 /* Output values Used by 'instance' tables only */
249737 i64 iInstPos;
249738 int iInstOff;
249739};
249740
249741#define FTS5_VOCAB_COL 0
249742#define FTS5_VOCAB_ROW 1
249743#define FTS5_VOCAB_INSTANCE 2
249744
249745#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
249746#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
249747#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
249748
249749/*
249750** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
249751*/
249752#define FTS5_VOCAB_TERM_EQ 0x01
249753#define FTS5_VOCAB_TERM_GE 0x02
249754#define FTS5_VOCAB_TERM_LE 0x04
249755
249756
249757/*
249758** Translate a string containing an fts5vocab table type to an
249759** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
249760** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
249761** and return SQLITE_ERROR.
249762*/
249763static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
249764 int rc = SQLITE_OK;
249765 char *zCopy = sqlite3Fts5Strndup(pRc: &rc, pIn: zType, nIn: -1);
249766 if( rc==SQLITE_OK ){
249767 sqlite3Fts5Dequote(z: zCopy);
249768 if( sqlite3_stricmp(zLeft: zCopy, zRight: "col")==0 ){
249769 *peType = FTS5_VOCAB_COL;
249770 }else
249771
249772 if( sqlite3_stricmp(zLeft: zCopy, zRight: "row")==0 ){
249773 *peType = FTS5_VOCAB_ROW;
249774 }else
249775 if( sqlite3_stricmp(zLeft: zCopy, zRight: "instance")==0 ){
249776 *peType = FTS5_VOCAB_INSTANCE;
249777 }else
249778 {
249779 *pzErr = sqlite3_mprintf(zFormat: "fts5vocab: unknown table type: %Q", zCopy);
249780 rc = SQLITE_ERROR;
249781 }
249782 sqlite3_free(p: zCopy);
249783 }
249784
249785 return rc;
249786}
249787
249788
249789/*
249790** The xDisconnect() virtual table method.
249791*/
249792static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
249793 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
249794 sqlite3_free(p: pTab);
249795 return SQLITE_OK;
249796}
249797
249798/*
249799** The xDestroy() virtual table method.
249800*/
249801static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
249802 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
249803 sqlite3_free(p: pTab);
249804 return SQLITE_OK;
249805}
249806
249807/*
249808** This function is the implementation of both the xConnect and xCreate
249809** methods of the FTS3 virtual table.
249810**
249811** The argv[] array contains the following:
249812**
249813** argv[0] -> module name ("fts5vocab")
249814** argv[1] -> database name
249815** argv[2] -> table name
249816**
249817** then:
249818**
249819** argv[3] -> name of fts5 table
249820** argv[4] -> type of fts5vocab table
249821**
249822** or, for tables in the TEMP schema only.
249823**
249824** argv[3] -> name of fts5 tables database
249825** argv[4] -> name of fts5 table
249826** argv[5] -> type of fts5vocab table
249827*/
249828static int fts5VocabInitVtab(
249829 sqlite3 *db, /* The SQLite database connection */
249830 void *pAux, /* Pointer to Fts5Global object */
249831 int argc, /* Number of elements in argv array */
249832 const char * const *argv, /* xCreate/xConnect argument array */
249833 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
249834 char **pzErr /* Write any error message here */
249835){
249836 const char *azSchema[] = {
249837 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
249838 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
249839 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
249840 };
249841
249842 Fts5VocabTable *pRet = 0;
249843 int rc = SQLITE_OK; /* Return code */
249844 int bDb;
249845
249846 bDb = (argc==6 && strlen(s: argv[1])==4 && memcmp(s1: "temp", s2: argv[1], n: 4)==0);
249847
249848 if( argc!=5 && bDb==0 ){
249849 *pzErr = sqlite3_mprintf(zFormat: "wrong number of vtable arguments");
249850 rc = SQLITE_ERROR;
249851 }else{
249852 int nByte; /* Bytes of space to allocate */
249853 const char *zDb = bDb ? argv[3] : argv[1];
249854 const char *zTab = bDb ? argv[4] : argv[3];
249855 const char *zType = bDb ? argv[5] : argv[4];
249856 int nDb = (int)strlen(s: zDb)+1;
249857 int nTab = (int)strlen(s: zTab)+1;
249858 int eType = 0;
249859
249860 rc = fts5VocabTableType(zType, pzErr, peType: &eType);
249861 if( rc==SQLITE_OK ){
249862 assert( eType>=0 && eType<ArraySize(azSchema) );
249863 rc = sqlite3_declare_vtab(db, zCreateTable: azSchema[eType]);
249864 }
249865
249866 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
249867 pRet = sqlite3Fts5MallocZero(pRc: &rc, nByte);
249868 if( pRet ){
249869 pRet->pGlobal = (Fts5Global*)pAux;
249870 pRet->eType = eType;
249871 pRet->db = db;
249872 pRet->zFts5Tbl = (char*)&pRet[1];
249873 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
249874 memcpy(dest: pRet->zFts5Tbl, src: zTab, n: nTab);
249875 memcpy(dest: pRet->zFts5Db, src: zDb, n: nDb);
249876 sqlite3Fts5Dequote(z: pRet->zFts5Tbl);
249877 sqlite3Fts5Dequote(z: pRet->zFts5Db);
249878 }
249879 }
249880
249881 *ppVTab = (sqlite3_vtab*)pRet;
249882 return rc;
249883}
249884
249885
249886/*
249887** The xConnect() and xCreate() methods for the virtual table. All the
249888** work is done in function fts5VocabInitVtab().
249889*/
249890static int fts5VocabConnectMethod(
249891 sqlite3 *db, /* Database connection */
249892 void *pAux, /* Pointer to tokenizer hash table */
249893 int argc, /* Number of elements in argv array */
249894 const char * const *argv, /* xCreate/xConnect argument array */
249895 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
249896 char **pzErr /* OUT: sqlite3_malloc'd error message */
249897){
249898 return fts5VocabInitVtab(db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
249899}
249900static int fts5VocabCreateMethod(
249901 sqlite3 *db, /* Database connection */
249902 void *pAux, /* Pointer to tokenizer hash table */
249903 int argc, /* Number of elements in argv array */
249904 const char * const *argv, /* xCreate/xConnect argument array */
249905 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
249906 char **pzErr /* OUT: sqlite3_malloc'd error message */
249907){
249908 return fts5VocabInitVtab(db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
249909}
249910
249911/*
249912** Implementation of the xBestIndex method.
249913**
249914** Only constraints of the form:
249915**
249916** term <= ?
249917** term == ?
249918** term >= ?
249919**
249920** are interpreted. Less-than and less-than-or-equal are treated
249921** identically, as are greater-than and greater-than-or-equal.
249922*/
249923static int fts5VocabBestIndexMethod(
249924 sqlite3_vtab *pUnused,
249925 sqlite3_index_info *pInfo
249926){
249927 int i;
249928 int iTermEq = -1;
249929 int iTermGe = -1;
249930 int iTermLe = -1;
249931 int idxNum = 0;
249932 int nArg = 0;
249933
249934 UNUSED_PARAM(pUnused);
249935
249936 for(i=0; i<pInfo->nConstraint; i++){
249937 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
249938 if( p->usable==0 ) continue;
249939 if( p->iColumn==0 ){ /* term column */
249940 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
249941 if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
249942 if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
249943 if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
249944 if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
249945 }
249946 }
249947
249948 if( iTermEq>=0 ){
249949 idxNum |= FTS5_VOCAB_TERM_EQ;
249950 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
249951 pInfo->estimatedCost = 100;
249952 }else{
249953 pInfo->estimatedCost = 1000000;
249954 if( iTermGe>=0 ){
249955 idxNum |= FTS5_VOCAB_TERM_GE;
249956 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
249957 pInfo->estimatedCost = pInfo->estimatedCost / 2;
249958 }
249959 if( iTermLe>=0 ){
249960 idxNum |= FTS5_VOCAB_TERM_LE;
249961 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
249962 pInfo->estimatedCost = pInfo->estimatedCost / 2;
249963 }
249964 }
249965
249966 /* This virtual table always delivers results in ascending order of
249967 ** the "term" column (column 0). So if the user has requested this
249968 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
249969 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
249970 ** are already in sorted order. */
249971 if( pInfo->nOrderBy==1
249972 && pInfo->aOrderBy[0].iColumn==0
249973 && pInfo->aOrderBy[0].desc==0
249974 ){
249975 pInfo->orderByConsumed = 1;
249976 }
249977
249978 pInfo->idxNum = idxNum;
249979 return SQLITE_OK;
249980}
249981
249982/*
249983** Implementation of xOpen method.
249984*/
249985static int fts5VocabOpenMethod(
249986 sqlite3_vtab *pVTab,
249987 sqlite3_vtab_cursor **ppCsr
249988){
249989 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
249990 Fts5Table *pFts5 = 0;
249991 Fts5VocabCursor *pCsr = 0;
249992 int rc = SQLITE_OK;
249993 sqlite3_stmt *pStmt = 0;
249994 char *zSql = 0;
249995
249996 if( pTab->bBusy ){
249997 pVTab->zErrMsg = sqlite3_mprintf(
249998 zFormat: "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
249999 );
250000 return SQLITE_ERROR;
250001 }
250002 zSql = sqlite3Fts5Mprintf(pRc: &rc,
250003 zFmt: "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
250004 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
250005 );
250006 if( zSql ){
250007 rc = sqlite3_prepare_v2(db: pTab->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
250008 }
250009 sqlite3_free(p: zSql);
250010 assert( rc==SQLITE_OK || pStmt==0 );
250011 if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
250012
250013 pTab->bBusy = 1;
250014 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
250015 i64 iId = sqlite3_column_int64(pStmt, i: 0);
250016 pFts5 = sqlite3Fts5TableFromCsrid(pGlobal: pTab->pGlobal, iCsrId: iId);
250017 }
250018 pTab->bBusy = 0;
250019
250020 if( rc==SQLITE_OK ){
250021 if( pFts5==0 ){
250022 rc = sqlite3_finalize(pStmt);
250023 pStmt = 0;
250024 if( rc==SQLITE_OK ){
250025 pVTab->zErrMsg = sqlite3_mprintf(
250026 zFormat: "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
250027 );
250028 rc = SQLITE_ERROR;
250029 }
250030 }else{
250031 rc = sqlite3Fts5FlushToDisk(pTab: pFts5);
250032 }
250033 }
250034
250035 if( rc==SQLITE_OK ){
250036 i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
250037 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
250038 }
250039
250040 if( pCsr ){
250041 pCsr->pFts5 = pFts5;
250042 pCsr->pStmt = pStmt;
250043 pCsr->aCnt = (i64*)&pCsr[1];
250044 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
250045 }else{
250046 sqlite3_finalize(pStmt);
250047 }
250048
250049 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
250050 return rc;
250051}
250052
250053static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
250054 pCsr->rowid = 0;
250055 sqlite3Fts5IterClose(pIndexIter: pCsr->pIter);
250056 sqlite3Fts5StructureRelease(p: pCsr->pStruct);
250057 pCsr->pStruct = 0;
250058 pCsr->pIter = 0;
250059 sqlite3_free(p: pCsr->zLeTerm);
250060 pCsr->nLeTerm = -1;
250061 pCsr->zLeTerm = 0;
250062 pCsr->bEof = 0;
250063}
250064
250065/*
250066** Close the cursor. For additional information see the documentation
250067** on the xClose method of the virtual table interface.
250068*/
250069static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
250070 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
250071 fts5VocabResetCursor(pCsr);
250072 sqlite3Fts5BufferFree(pBuf: &pCsr->term);
250073 sqlite3_finalize(pStmt: pCsr->pStmt);
250074 sqlite3_free(p: pCsr);
250075 return SQLITE_OK;
250076}
250077
250078static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
250079 int rc = SQLITE_OK;
250080
250081 if( sqlite3Fts5IterEof(pCsr->pIter) ){
250082 pCsr->bEof = 1;
250083 }else{
250084 const char *zTerm;
250085 int nTerm;
250086 zTerm = sqlite3Fts5IterTerm(pIndexIter: pCsr->pIter, pn: &nTerm);
250087 if( pCsr->nLeTerm>=0 ){
250088 int nCmp = MIN(nTerm, pCsr->nLeTerm);
250089 int bCmp = memcmp(s1: pCsr->zLeTerm, s2: zTerm, n: nCmp);
250090 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
250091 pCsr->bEof = 1;
250092 }
250093 }
250094
250095 sqlite3Fts5BufferSet(pRc: &rc, pBuf: &pCsr->term, nData: nTerm, pData: (const u8*)zTerm);
250096 }
250097 return rc;
250098}
250099
250100static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
250101 int eDetail = pCsr->pFts5->pConfig->eDetail;
250102 int rc = SQLITE_OK;
250103 Fts5IndexIter *pIter = pCsr->pIter;
250104 i64 *pp = &pCsr->iInstPos;
250105 int *po = &pCsr->iInstOff;
250106
250107 assert( sqlite3Fts5IterEof(pIter)==0 );
250108 assert( pCsr->bEof==0 );
250109 while( eDetail==FTS5_DETAIL_NONE
250110 || sqlite3Fts5PoslistNext64(a: pIter->pData, n: pIter->nData, pi: po, piOff: pp)
250111 ){
250112 pCsr->iInstPos = 0;
250113 pCsr->iInstOff = 0;
250114
250115 rc = sqlite3Fts5IterNextScan(pIndexIter: pCsr->pIter);
250116 if( rc==SQLITE_OK ){
250117 rc = fts5VocabInstanceNewTerm(pCsr);
250118 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
250119 }
250120 if( rc ){
250121 pCsr->bEof = 1;
250122 break;
250123 }
250124 }
250125
250126 return rc;
250127}
250128
250129/*
250130** Advance the cursor to the next row in the table.
250131*/
250132static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
250133 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
250134 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
250135 int nCol = pCsr->pFts5->pConfig->nCol;
250136 int rc;
250137
250138 rc = sqlite3Fts5StructureTest(p: pCsr->pFts5->pIndex, pStruct: pCsr->pStruct);
250139 if( rc!=SQLITE_OK ) return rc;
250140 pCsr->rowid++;
250141
250142 if( pTab->eType==FTS5_VOCAB_INSTANCE ){
250143 return fts5VocabInstanceNext(pCsr);
250144 }
250145
250146 if( pTab->eType==FTS5_VOCAB_COL ){
250147 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
250148 if( pCsr->aDoc[pCsr->iCol] ) break;
250149 }
250150 }
250151
250152 if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
250153 if( sqlite3Fts5IterEof(pCsr->pIter) ){
250154 pCsr->bEof = 1;
250155 }else{
250156 const char *zTerm;
250157 int nTerm;
250158
250159 zTerm = sqlite3Fts5IterTerm(pIndexIter: pCsr->pIter, pn: &nTerm);
250160 assert( nTerm>=0 );
250161 if( pCsr->nLeTerm>=0 ){
250162 int nCmp = MIN(nTerm, pCsr->nLeTerm);
250163 int bCmp = memcmp(s1: pCsr->zLeTerm, s2: zTerm, n: nCmp);
250164 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
250165 pCsr->bEof = 1;
250166 return SQLITE_OK;
250167 }
250168 }
250169
250170 sqlite3Fts5BufferSet(pRc: &rc, pBuf: &pCsr->term, nData: nTerm, pData: (const u8*)zTerm);
250171 memset(s: pCsr->aCnt, c: 0, n: nCol * sizeof(i64));
250172 memset(s: pCsr->aDoc, c: 0, n: nCol * sizeof(i64));
250173 pCsr->iCol = 0;
250174
250175 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
250176 while( rc==SQLITE_OK ){
250177 int eDetail = pCsr->pFts5->pConfig->eDetail;
250178 const u8 *pPos; int nPos; /* Position list */
250179 i64 iPos = 0; /* 64-bit position read from poslist */
250180 int iOff = 0; /* Current offset within position list */
250181
250182 pPos = pCsr->pIter->pData;
250183 nPos = pCsr->pIter->nData;
250184
250185 switch( pTab->eType ){
250186 case FTS5_VOCAB_ROW:
250187 if( eDetail==FTS5_DETAIL_FULL ){
250188 while( 0==sqlite3Fts5PoslistNext64(a: pPos, n: nPos, pi: &iOff, piOff: &iPos) ){
250189 pCsr->aCnt[0]++;
250190 }
250191 }
250192 pCsr->aDoc[0]++;
250193 break;
250194
250195 case FTS5_VOCAB_COL:
250196 if( eDetail==FTS5_DETAIL_FULL ){
250197 int iCol = -1;
250198 while( 0==sqlite3Fts5PoslistNext64(a: pPos, n: nPos, pi: &iOff, piOff: &iPos) ){
250199 int ii = FTS5_POS2COLUMN(iPos);
250200 if( iCol!=ii ){
250201 if( ii>=nCol ){
250202 rc = FTS5_CORRUPT;
250203 break;
250204 }
250205 pCsr->aDoc[ii]++;
250206 iCol = ii;
250207 }
250208 pCsr->aCnt[ii]++;
250209 }
250210 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
250211 while( 0==sqlite3Fts5PoslistNext64(a: pPos, n: nPos, pi: &iOff,piOff: &iPos) ){
250212 assert_nc( iPos>=0 && iPos<nCol );
250213 if( iPos>=nCol ){
250214 rc = FTS5_CORRUPT;
250215 break;
250216 }
250217 pCsr->aDoc[iPos]++;
250218 }
250219 }else{
250220 assert( eDetail==FTS5_DETAIL_NONE );
250221 pCsr->aDoc[0]++;
250222 }
250223 break;
250224
250225 default:
250226 assert( pTab->eType==FTS5_VOCAB_INSTANCE );
250227 break;
250228 }
250229
250230 if( rc==SQLITE_OK ){
250231 rc = sqlite3Fts5IterNextScan(pIndexIter: pCsr->pIter);
250232 }
250233 if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
250234
250235 if( rc==SQLITE_OK ){
250236 zTerm = sqlite3Fts5IterTerm(pIndexIter: pCsr->pIter, pn: &nTerm);
250237 if( nTerm!=pCsr->term.n
250238 || (nTerm>0 && memcmp(s1: zTerm, s2: pCsr->term.p, n: nTerm))
250239 ){
250240 break;
250241 }
250242 if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
250243 }
250244 }
250245 }
250246 }
250247
250248 if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
250249 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
250250 if( pCsr->iCol==nCol ){
250251 rc = FTS5_CORRUPT;
250252 }
250253 }
250254 return rc;
250255}
250256
250257/*
250258** This is the xFilter implementation for the virtual table.
250259*/
250260static int fts5VocabFilterMethod(
250261 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
250262 int idxNum, /* Strategy index */
250263 const char *zUnused, /* Unused */
250264 int nUnused, /* Number of elements in apVal */
250265 sqlite3_value **apVal /* Arguments for the indexing scheme */
250266){
250267 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
250268 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
250269 int eType = pTab->eType;
250270 int rc = SQLITE_OK;
250271
250272 int iVal = 0;
250273 int f = FTS5INDEX_QUERY_SCAN;
250274 const char *zTerm = 0;
250275 int nTerm = 0;
250276
250277 sqlite3_value *pEq = 0;
250278 sqlite3_value *pGe = 0;
250279 sqlite3_value *pLe = 0;
250280
250281 UNUSED_PARAM2(zUnused, nUnused);
250282
250283 fts5VocabResetCursor(pCsr);
250284 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
250285 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
250286 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
250287
250288 if( pEq ){
250289 zTerm = (const char *)sqlite3_value_text(pVal: pEq);
250290 nTerm = sqlite3_value_bytes(pVal: pEq);
250291 f = 0;
250292 }else{
250293 if( pGe ){
250294 zTerm = (const char *)sqlite3_value_text(pVal: pGe);
250295 nTerm = sqlite3_value_bytes(pVal: pGe);
250296 }
250297 if( pLe ){
250298 const char *zCopy = (const char *)sqlite3_value_text(pVal: pLe);
250299 if( zCopy==0 ) zCopy = "";
250300 pCsr->nLeTerm = sqlite3_value_bytes(pVal: pLe);
250301 pCsr->zLeTerm = sqlite3_malloc(n: pCsr->nLeTerm+1);
250302 if( pCsr->zLeTerm==0 ){
250303 rc = SQLITE_NOMEM;
250304 }else{
250305 memcpy(dest: pCsr->zLeTerm, src: zCopy, n: pCsr->nLeTerm+1);
250306 }
250307 }
250308 }
250309
250310 if( rc==SQLITE_OK ){
250311 Fts5Index *pIndex = pCsr->pFts5->pIndex;
250312 rc = sqlite3Fts5IndexQuery(p: pIndex, pToken: zTerm, nToken: nTerm, flags: f, pColset: 0, ppIter: &pCsr->pIter);
250313 if( rc==SQLITE_OK ){
250314 pCsr->pStruct = sqlite3Fts5StructureRef(p: pIndex);
250315 }
250316 }
250317 if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
250318 rc = fts5VocabInstanceNewTerm(pCsr);
250319 }
250320 if( rc==SQLITE_OK && !pCsr->bEof
250321 && (eType!=FTS5_VOCAB_INSTANCE
250322 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE)
250323 ){
250324 rc = fts5VocabNextMethod(pCursor);
250325 }
250326
250327 return rc;
250328}
250329
250330/*
250331** This is the xEof method of the virtual table. SQLite calls this
250332** routine to find out if it has reached the end of a result set.
250333*/
250334static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
250335 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
250336 return pCsr->bEof;
250337}
250338
250339static int fts5VocabColumnMethod(
250340 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
250341 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
250342 int iCol /* Index of column to read value from */
250343){
250344 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
250345 int eDetail = pCsr->pFts5->pConfig->eDetail;
250346 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
250347 i64 iVal = 0;
250348
250349 if( iCol==0 ){
250350 sqlite3_result_text(
250351 pCtx, z: (const char*)pCsr->term.p, n: pCsr->term.n, SQLITE_TRANSIENT
250352 );
250353 }else if( eType==FTS5_VOCAB_COL ){
250354 assert( iCol==1 || iCol==2 || iCol==3 );
250355 if( iCol==1 ){
250356 if( eDetail!=FTS5_DETAIL_NONE ){
250357 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
250358 sqlite3_result_text(pCtx, z, n: -1, SQLITE_STATIC);
250359 }
250360 }else if( iCol==2 ){
250361 iVal = pCsr->aDoc[pCsr->iCol];
250362 }else{
250363 iVal = pCsr->aCnt[pCsr->iCol];
250364 }
250365 }else if( eType==FTS5_VOCAB_ROW ){
250366 assert( iCol==1 || iCol==2 );
250367 if( iCol==1 ){
250368 iVal = pCsr->aDoc[0];
250369 }else{
250370 iVal = pCsr->aCnt[0];
250371 }
250372 }else{
250373 assert( eType==FTS5_VOCAB_INSTANCE );
250374 switch( iCol ){
250375 case 1:
250376 sqlite3_result_int64(pCtx, iVal: pCsr->pIter->iRowid);
250377 break;
250378 case 2: {
250379 int ii = -1;
250380 if( eDetail==FTS5_DETAIL_FULL ){
250381 ii = FTS5_POS2COLUMN(pCsr->iInstPos);
250382 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
250383 ii = (int)pCsr->iInstPos;
250384 }
250385 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
250386 const char *z = pCsr->pFts5->pConfig->azCol[ii];
250387 sqlite3_result_text(pCtx, z, n: -1, SQLITE_STATIC);
250388 }
250389 break;
250390 }
250391 default: {
250392 assert( iCol==3 );
250393 if( eDetail==FTS5_DETAIL_FULL ){
250394 int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
250395 sqlite3_result_int(pCtx, iVal: ii);
250396 }
250397 break;
250398 }
250399 }
250400 }
250401
250402 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
250403 return SQLITE_OK;
250404}
250405
250406/*
250407** This is the xRowid method. The SQLite core calls this routine to
250408** retrieve the rowid for the current row of the result set. The
250409** rowid should be written to *pRowid.
250410*/
250411static int fts5VocabRowidMethod(
250412 sqlite3_vtab_cursor *pCursor,
250413 sqlite_int64 *pRowid
250414){
250415 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
250416 *pRowid = pCsr->rowid;
250417 return SQLITE_OK;
250418}
250419
250420static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
250421 static const sqlite3_module fts5Vocab = {
250422 /* iVersion */ 2,
250423 /* xCreate */ fts5VocabCreateMethod,
250424 /* xConnect */ fts5VocabConnectMethod,
250425 /* xBestIndex */ fts5VocabBestIndexMethod,
250426 /* xDisconnect */ fts5VocabDisconnectMethod,
250427 /* xDestroy */ fts5VocabDestroyMethod,
250428 /* xOpen */ fts5VocabOpenMethod,
250429 /* xClose */ fts5VocabCloseMethod,
250430 /* xFilter */ fts5VocabFilterMethod,
250431 /* xNext */ fts5VocabNextMethod,
250432 /* xEof */ fts5VocabEofMethod,
250433 /* xColumn */ fts5VocabColumnMethod,
250434 /* xRowid */ fts5VocabRowidMethod,
250435 /* xUpdate */ 0,
250436 /* xBegin */ 0,
250437 /* xSync */ 0,
250438 /* xCommit */ 0,
250439 /* xRollback */ 0,
250440 /* xFindFunction */ 0,
250441 /* xRename */ 0,
250442 /* xSavepoint */ 0,
250443 /* xRelease */ 0,
250444 /* xRollbackTo */ 0,
250445 /* xShadowName */ 0
250446 };
250447 void *p = (void*)pGlobal;
250448
250449 return sqlite3_create_module_v2(db, zName: "fts5vocab", pModule: &fts5Vocab, pAux: p, xDestroy: 0);
250450}
250451
250452
250453
250454#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
250455
250456/************** End of fts5.c ************************************************/
250457/************** Begin file stmt.c ********************************************/
250458/*
250459** 2017-05-31
250460**
250461** The author disclaims copyright to this source code. In place of
250462** a legal notice, here is a blessing:
250463**
250464** May you do good and not evil.
250465** May you find forgiveness for yourself and forgive others.
250466** May you share freely, never taking more than you give.
250467**
250468*************************************************************************
250469**
250470** This file demonstrates an eponymous virtual table that returns information
250471** about all prepared statements for the database connection.
250472**
250473** Usage example:
250474**
250475** .load ./stmt
250476** .mode line
250477** .header on
250478** SELECT * FROM stmt;
250479*/
250480#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
250481#if !defined(SQLITEINT_H)
250482/* #include "sqlite3ext.h" */
250483#endif
250484SQLITE_EXTENSION_INIT1
250485/* #include <assert.h> */
250486/* #include <string.h> */
250487
250488#ifndef SQLITE_OMIT_VIRTUALTABLE
250489
250490
250491#define STMT_NUM_INTEGER_COLUMN 10
250492typedef struct StmtRow StmtRow;
250493struct StmtRow {
250494 sqlite3_int64 iRowid; /* Rowid value */
250495 char *zSql; /* column "sql" */
250496 int aCol[STMT_NUM_INTEGER_COLUMN+1]; /* all other column values */
250497 StmtRow *pNext; /* Next row to return */
250498};
250499
250500/* stmt_vtab is a subclass of sqlite3_vtab which will
250501** serve as the underlying representation of a stmt virtual table
250502*/
250503typedef struct stmt_vtab stmt_vtab;
250504struct stmt_vtab {
250505 sqlite3_vtab base; /* Base class - must be first */
250506 sqlite3 *db; /* Database connection for this stmt vtab */
250507};
250508
250509/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
250510** serve as the underlying representation of a cursor that scans
250511** over rows of the result
250512*/
250513typedef struct stmt_cursor stmt_cursor;
250514struct stmt_cursor {
250515 sqlite3_vtab_cursor base; /* Base class - must be first */
250516 sqlite3 *db; /* Database connection for this cursor */
250517 StmtRow *pRow; /* Current row */
250518};
250519
250520/*
250521** The stmtConnect() method is invoked to create a new
250522** stmt_vtab that describes the stmt virtual table.
250523**
250524** Think of this routine as the constructor for stmt_vtab objects.
250525**
250526** All this routine needs to do is:
250527**
250528** (1) Allocate the stmt_vtab object and initialize all fields.
250529**
250530** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
250531** result set of queries against stmt will look like.
250532*/
250533static int stmtConnect(
250534 sqlite3 *db,
250535 void *pAux,
250536 int argc, const char *const*argv,
250537 sqlite3_vtab **ppVtab,
250538 char **pzErr
250539){
250540 stmt_vtab *pNew;
250541 int rc;
250542
250543/* Column numbers */
250544#define STMT_COLUMN_SQL 0 /* SQL for the statement */
250545#define STMT_COLUMN_NCOL 1 /* Number of result columns */
250546#define STMT_COLUMN_RO 2 /* True if read-only */
250547#define STMT_COLUMN_BUSY 3 /* True if currently busy */
250548#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
250549#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
250550#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
250551#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
250552#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
250553#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
250554#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
250555
250556
250557 (void)pAux;
250558 (void)argc;
250559 (void)argv;
250560 (void)pzErr;
250561 rc = sqlite3_declare_vtab(db,
250562 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
250563 "reprep,run,mem)");
250564 if( rc==SQLITE_OK ){
250565 pNew = sqlite3_malloc64( sizeof(*pNew) );
250566 *ppVtab = (sqlite3_vtab*)pNew;
250567 if( pNew==0 ) return SQLITE_NOMEM;
250568 memset(pNew, 0, sizeof(*pNew));
250569 pNew->db = db;
250570 }
250571 return rc;
250572}
250573
250574/*
250575** This method is the destructor for stmt_cursor objects.
250576*/
250577static int stmtDisconnect(sqlite3_vtab *pVtab){
250578 sqlite3_free(pVtab);
250579 return SQLITE_OK;
250580}
250581
250582/*
250583** Constructor for a new stmt_cursor object.
250584*/
250585static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
250586 stmt_cursor *pCur;
250587 pCur = sqlite3_malloc64( sizeof(*pCur) );
250588 if( pCur==0 ) return SQLITE_NOMEM;
250589 memset(pCur, 0, sizeof(*pCur));
250590 pCur->db = ((stmt_vtab*)p)->db;
250591 *ppCursor = &pCur->base;
250592 return SQLITE_OK;
250593}
250594
250595static void stmtCsrReset(stmt_cursor *pCur){
250596 StmtRow *pRow = 0;
250597 StmtRow *pNext = 0;
250598 for(pRow=pCur->pRow; pRow; pRow=pNext){
250599 pNext = pRow->pNext;
250600 sqlite3_free(pRow);
250601 }
250602 pCur->pRow = 0;
250603}
250604
250605/*
250606** Destructor for a stmt_cursor.
250607*/
250608static int stmtClose(sqlite3_vtab_cursor *cur){
250609 stmtCsrReset((stmt_cursor*)cur);
250610 sqlite3_free(cur);
250611 return SQLITE_OK;
250612}
250613
250614
250615/*
250616** Advance a stmt_cursor to its next row of output.
250617*/
250618static int stmtNext(sqlite3_vtab_cursor *cur){
250619 stmt_cursor *pCur = (stmt_cursor*)cur;
250620 StmtRow *pNext = pCur->pRow->pNext;
250621 sqlite3_free(pCur->pRow);
250622 pCur->pRow = pNext;
250623 return SQLITE_OK;
250624}
250625
250626/*
250627** Return values of columns for the row at which the stmt_cursor
250628** is currently pointing.
250629*/
250630static int stmtColumn(
250631 sqlite3_vtab_cursor *cur, /* The cursor */
250632 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
250633 int i /* Which column to return */
250634){
250635 stmt_cursor *pCur = (stmt_cursor*)cur;
250636 StmtRow *pRow = pCur->pRow;
250637 if( i==STMT_COLUMN_SQL ){
250638 sqlite3_result_text(ctx, pRow->zSql, -1, SQLITE_TRANSIENT);
250639 }else{
250640 sqlite3_result_int(ctx, pRow->aCol[i]);
250641 }
250642 return SQLITE_OK;
250643}
250644
250645/*
250646** Return the rowid for the current row. In this implementation, the
250647** rowid is the same as the output value.
250648*/
250649static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
250650 stmt_cursor *pCur = (stmt_cursor*)cur;
250651 *pRowid = pCur->pRow->iRowid;
250652 return SQLITE_OK;
250653}
250654
250655/*
250656** Return TRUE if the cursor has been moved off of the last
250657** row of output.
250658*/
250659static int stmtEof(sqlite3_vtab_cursor *cur){
250660 stmt_cursor *pCur = (stmt_cursor*)cur;
250661 return pCur->pRow==0;
250662}
250663
250664/*
250665** This method is called to "rewind" the stmt_cursor object back
250666** to the first row of output. This method is always called at least
250667** once prior to any call to stmtColumn() or stmtRowid() or
250668** stmtEof().
250669*/
250670static int stmtFilter(
250671 sqlite3_vtab_cursor *pVtabCursor,
250672 int idxNum, const char *idxStr,
250673 int argc, sqlite3_value **argv
250674){
250675 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
250676 sqlite3_stmt *p = 0;
250677 sqlite3_int64 iRowid = 1;
250678 StmtRow **ppRow = 0;
250679
250680 (void)idxNum;
250681 (void)idxStr;
250682 (void)argc;
250683 (void)argv;
250684 stmtCsrReset(pCur);
250685 ppRow = &pCur->pRow;
250686 for(p=sqlite3_next_stmt(pCur->db, 0); p; p=sqlite3_next_stmt(pCur->db, p)){
250687 const char *zSql = sqlite3_sql(p);
250688 sqlite3_int64 nSql = zSql ? strlen(zSql)+1 : 0;
250689 StmtRow *pNew = (StmtRow*)sqlite3_malloc64(sizeof(StmtRow) + nSql);
250690
250691 if( pNew==0 ) return SQLITE_NOMEM;
250692 memset(pNew, 0, sizeof(StmtRow));
250693 if( zSql ){
250694 pNew->zSql = (char*)&pNew[1];
250695 memcpy(pNew->zSql, zSql, nSql);
250696 }
250697 pNew->aCol[STMT_COLUMN_NCOL] = sqlite3_column_count(p);
250698 pNew->aCol[STMT_COLUMN_RO] = sqlite3_stmt_readonly(p);
250699 pNew->aCol[STMT_COLUMN_BUSY] = sqlite3_stmt_busy(p);
250700 pNew->aCol[STMT_COLUMN_NSCAN] = sqlite3_stmt_status(
250701 p, SQLITE_STMTSTATUS_FULLSCAN_STEP, 0
250702 );
250703 pNew->aCol[STMT_COLUMN_NSORT] = sqlite3_stmt_status(
250704 p, SQLITE_STMTSTATUS_SORT, 0
250705 );
250706 pNew->aCol[STMT_COLUMN_NAIDX] = sqlite3_stmt_status(
250707 p, SQLITE_STMTSTATUS_AUTOINDEX, 0
250708 );
250709 pNew->aCol[STMT_COLUMN_NSTEP] = sqlite3_stmt_status(
250710 p, SQLITE_STMTSTATUS_VM_STEP, 0
250711 );
250712 pNew->aCol[STMT_COLUMN_REPREP] = sqlite3_stmt_status(
250713 p, SQLITE_STMTSTATUS_REPREPARE, 0
250714 );
250715 pNew->aCol[STMT_COLUMN_RUN] = sqlite3_stmt_status(
250716 p, SQLITE_STMTSTATUS_RUN, 0
250717 );
250718 pNew->aCol[STMT_COLUMN_MEM] = sqlite3_stmt_status(
250719 p, SQLITE_STMTSTATUS_MEMUSED, 0
250720 );
250721 pNew->iRowid = iRowid++;
250722 *ppRow = pNew;
250723 ppRow = &pNew->pNext;
250724 }
250725
250726 return SQLITE_OK;
250727}
250728
250729/*
250730** SQLite will invoke this method one or more times while planning a query
250731** that uses the stmt virtual table. This routine needs to create
250732** a query plan for each invocation and compute an estimated cost for that
250733** plan.
250734*/
250735static int stmtBestIndex(
250736 sqlite3_vtab *tab,
250737 sqlite3_index_info *pIdxInfo
250738){
250739 (void)tab;
250740 pIdxInfo->estimatedCost = (double)500;
250741 pIdxInfo->estimatedRows = 500;
250742 return SQLITE_OK;
250743}
250744
250745/*
250746** This following structure defines all the methods for the
250747** stmt virtual table.
250748*/
250749static sqlite3_module stmtModule = {
250750 0, /* iVersion */
250751 0, /* xCreate */
250752 stmtConnect, /* xConnect */
250753 stmtBestIndex, /* xBestIndex */
250754 stmtDisconnect, /* xDisconnect */
250755 0, /* xDestroy */
250756 stmtOpen, /* xOpen - open a cursor */
250757 stmtClose, /* xClose - close a cursor */
250758 stmtFilter, /* xFilter - configure scan constraints */
250759 stmtNext, /* xNext - advance a cursor */
250760 stmtEof, /* xEof - check for end of scan */
250761 stmtColumn, /* xColumn - read data */
250762 stmtRowid, /* xRowid - read data */
250763 0, /* xUpdate */
250764 0, /* xBegin */
250765 0, /* xSync */
250766 0, /* xCommit */
250767 0, /* xRollback */
250768 0, /* xFindMethod */
250769 0, /* xRename */
250770 0, /* xSavepoint */
250771 0, /* xRelease */
250772 0, /* xRollbackTo */
250773 0, /* xShadowName */
250774};
250775
250776#endif /* SQLITE_OMIT_VIRTUALTABLE */
250777
250778SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
250779 int rc = SQLITE_OK;
250780#ifndef SQLITE_OMIT_VIRTUALTABLE
250781 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
250782#endif
250783 return rc;
250784}
250785
250786#ifndef SQLITE_CORE
250787#ifdef _WIN32
250788__declspec(dllexport)
250789#endif
250790SQLITE_API int sqlite3_stmt_init(
250791 sqlite3 *db,
250792 char **pzErrMsg,
250793 const sqlite3_api_routines *pApi
250794){
250795 int rc = SQLITE_OK;
250796 SQLITE_EXTENSION_INIT2(pApi);
250797#ifndef SQLITE_OMIT_VIRTUALTABLE
250798 rc = sqlite3StmtVtabInit(db);
250799#endif
250800 return rc;
250801}
250802#endif /* SQLITE_CORE */
250803#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
250804
250805/************** End of stmt.c ************************************************/
250806/* Return the source-id for this library */
250807SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
250808/************************** End of sqlite3.c ******************************/
250809

source code of qtbase/src/3rdparty/sqlite/sqlite3.c